00001
00002
00003
00004
00005
00006
00007
00008
00009
00010
00011
00012
00013
00014
00015
00016
00017
00018
00019
00020
00021
00022
00023
00024
00025
00026
00027
00028
00029
00030
00031
00032
00033
00034
00035
00036
00037
00038 #include "vop_code.h"
00039 #include "mot_est_comp.h"
00040
00041 #include "bitstream.h"
00042
00043 #include "rate_ctl.h"
00044
00045 #define SCENE_CHANGE_THREADHOLD 50
00046 #define MB_RATIO_THREADHOLD 0.40
00047
00048 extern FILE *ftrace;
00049
00050 UInt BitstreamPutVopHeader ( Vop *vop,
00051 Float time,
00052 VolConfig *vol_config
00053 );
00054 Void ImageRepetitivePadding(Image *image, Int edge);
00055 Double compute_MAD(Vop *vop);
00056
00057
00058
00059
00060
00061
00062
00063
00064
00065
00066
00067
00068
00069
00070
00071
00072
00073
00074
00075
00076
00077
00078
00079
00080
00081 Void VopCode(Vop *curr,
00082 Vop *reference,
00083 Vop *reconstruct,
00084 Vop *error,
00085 Int enable_8x8_mv,
00086 Float time,
00087 VolConfig *vol_config,
00088 int quant)
00089 {
00090 ImageF *mot_x=NULL, *mot_y=NULL;
00091 Image *MB_decisions=NULL;
00092 Int edge,f_code_for=1;
00093 Vop *error_vop=NULL;
00094 Int vop_quantizer;
00095
00096 Float mad_P = 0., mad_I = 0.;
00097 Float IntraMBRatio = 1.;
00098 Int numberMB, i, IntraMB;
00099
00100 edge = 0;
00101 f_code_for = curr->fcode_for;
00102
00103 if (curr->prediction_type == P_VOP)
00104 {
00105
00106 MotionEstimationCompensation(curr, reference,
00107 enable_8x8_mv, edge ,f_code_for,
00108 reconstruct, &mad_P, &mot_x,&mot_y,&MB_decisions);
00109
00110
00111 IntraMB = 0;
00112 numberMB = MB_decisions->x * MB_decisions->y;
00113 for (i = 0; i < numberMB; i ++)
00114 if (MB_decisions->f[i] == MBM_INTRA) IntraMB ++;
00115 IntraMBRatio = (float)IntraMB / (float)numberMB;
00116
00117 #ifdef _RC_
00118 fprintf(ftrace, "ME with MAD : %f\n", mad_P);
00119 fprintf(ftrace, "%4.2f of the MBs are I-MBs.\n", IntraMBRatio);
00120 #endif
00121 }
00122 else
00123 mad_P = SCENE_CHANGE_THREADHOLD * 2;
00124
00125 if ((mad_P < SCENE_CHANGE_THREADHOLD / 3) ||
00126 ((mad_P < SCENE_CHANGE_THREADHOLD) && (IntraMBRatio < MB_RATIO_THREADHOLD)))
00127 {
00128
00129 curr->prediction_type = P_VOP;
00130 error->prediction_type = P_VOP;
00131
00132 #ifdef _RC_
00133 fprintf(ftrace, "Coding mode : INTER\n");
00134 #endif
00135
00136 vop_quantizer = (quant > 0) ? quant : RateCtlGetQ(mad_P);
00137
00138 curr->quantizer = vop_quantizer;
00139 error->quantizer = vop_quantizer;
00140
00141 #ifdef _RC_DEBUG_
00142 fprintf(stdout, "RC: >>>>> New quantizer= %d\n", vop_quantizer);
00143 #endif
00144
00145 SubImage(curr->y_chan, reconstruct->y_chan, error->y_chan);
00146 SubImage(curr->u_chan, reconstruct->u_chan, error->u_chan);
00147 SubImage(curr->v_chan, reconstruct->v_chan, error->v_chan);
00148
00149 BitstreamPutVopHeader(curr,time,vol_config);
00150
00151 VopShapeMotText(error, reconstruct, MB_decisions,
00152 mot_x, mot_y, f_code_for,
00153 GetVopIntraACDCPredDisable(curr), reference,
00154 NULL);
00155 } else {
00156
00157
00158 curr->prediction_type = I_VOP;
00159 curr->rounding_type = 1;
00160
00161 #ifdef _RC_
00162 fprintf(ftrace, "Coding mode : INTRA\n");
00163 #endif
00164
00165
00166
00167 if (mad_I == 0.) mad_I = (Float) compute_MAD(curr);
00168
00169 vop_quantizer = (quant > 0) ? quant : RateCtlGetQ(mad_I);
00170
00171 curr->intra_quantizer = vop_quantizer;
00172 curr->rounding_type = 1;
00173
00174 BitstreamPutVopHeader(curr,time,vol_config);
00175
00176
00177 VopCodeShapeTextIntraCom(curr,
00178 reference,
00179 NULL
00180 );
00181 }
00182
00183
00184 if (MB_decisions) FreeImage(MB_decisions);
00185 if (mot_x) FreeImage(mot_x);
00186 if (mot_y) FreeImage(mot_y);
00187
00188 ImageRepetitivePadding(reference->y_chan, 16);
00189 ImageRepetitivePadding(reference->u_chan, 8);
00190 ImageRepetitivePadding(reference->v_chan, 8);
00191
00192 Bitstream_NextStartCode();
00193
00194 return;
00195 }
00196
00197
00198
00199
00200
00201
00202
00203
00204
00205
00206
00207
00208
00209
00210
00211
00212
00213
00214
00215
00216
00217
00218
00219 UInt
00220 BitstreamPutVopHeader(Vop *vop,
00221 Float time,
00222 VolConfig *vol_config)
00223 {
00224 Image *buffer = NULL;
00225 Int bits;
00226 Int time_modulo;
00227 Float time_inc;
00228
00229 Int index;
00230
00231 UInt num_bits_header=0;
00232
00233
00234
00235
00236
00237
00238
00239 BitstreamPutBits(buffer,VOP_START_CODE,VOP_START_CODE_LENGTH);
00240
00241 BitstreamPutBits(buffer,GetVopPredictionType(vop),2);
00242
00243 index = GetVolConfigModTimeBase(vol_config, 1);
00244
00245 time_modulo = (int)time - index*1000;
00246 while(time_modulo >= 1000)
00247 {
00248 BitstreamPutBits(buffer,1,1);
00249 time_modulo = time_modulo - 1000;
00250 index++;
00251 printf("time modulo : 1\n");
00252 }
00253 BitstreamPutBits(buffer,0,1);
00254
00255
00256 PutVolConfigModTimeBase(index,vol_config);
00257
00258 time_inc = (time - index*1000);
00259
00260 bits = (int)ceil(log((double)GetVopTimeIncrementResolution(vop))/log(2.0));
00261 if (bits<1) bits=1;
00262 time_inc=time_inc*GetVopTimeIncrementResolution(vop)/1000.0f;
00263
00264
00265 BitstreamPutBits(buffer,1,1);
00266
00267 BitstreamPutBits(buffer,(Int)(time_inc+0.001),bits);
00268
00269
00270 BitstreamPutBits(buffer,1,1);
00271
00272 if (GetVopWidth(vop)==0)
00273 {
00274 printf("Empty VOP at %.2f\n",time);
00275 BitstreamPutBits(buffer,0L,1L);
00276 num_bits_header += Bitstream_NextStartCode();
00277 return(num_bits_header);
00278 }
00279 else
00280 BitstreamPutBits(buffer,1L,1L);
00281
00282 if( GetVopPredictionType(vop) == P_VOP )
00283 BitstreamPutBits(buffer,GetVopRoundingType(vop),1);
00284
00285 BitstreamPutBits(buffer,GetVopIntraDCVlcThr(vop),3);
00286
00287 if (GetVopPredictionType(vop) == I_VOP)
00288 BitstreamPutBits(buffer,GetVopIntraQuantizer(vop),GetVopQuantPrecision(vop));
00289 else
00290 BitstreamPutBits(buffer,GetVopQuantizer(vop),GetVopQuantPrecision(vop));
00291
00292 if (GetVopPredictionType(vop)!=I_VOP)
00293 {
00294 BitstreamPutBits(buffer,GetVopFCodeFor(vop),3);
00295 }
00296
00297 return(num_bits_header);
00298 }
00299
00300
00301
00302 Void ImageRepetitivePadding(Image *image, Int edge)
00303 {
00304 SInt *p, left, right;
00305 Int width, height, x, y;
00306
00307 p = image->f;
00308 width = image->x;
00309 height = image->y;
00310
00311
00312 for( y=edge; y<height-edge; y++)
00313 {
00314 left = p[y*width+edge];
00315 right = p[y*width+width-edge-1];
00316 for(x=0; x<edge; x++)
00317 {
00318 p[y*width+x] = left;
00319 p[y*width+width-edge+x] = right;
00320 }
00321 }
00322
00323
00324 for(y=0; y<edge; y++)
00325 for(x=0; x<width; x++)
00326 p[y*width+x] = p[edge*width+x];
00327
00328 for(y=height-edge; y<height; y++)
00329 for(x=0; x<width; x++)
00330 p[y*width+x] = p[(height-1-edge)*width+x];
00331
00332 return;
00333 }
00334
00335
00336
00337
00338
00339
00340
00341
00342
00343
00344
00345
00346
00347
00348
00349 Double compute_MAD(
00350 Vop *error_vop
00351 )
00352 {
00353 SInt *curr_in,
00354 *curr_end;
00355 Float *curr_fin,
00356 *curr_fend;
00357 UInt sxy_in;
00358 Double mad=0.0, dc = 0.0;
00359 Int cnt=0;
00360
00361
00362 switch (GetImageType(error_vop->y_chan))
00363 {
00364 case SHORT_TYPE:
00365
00366
00367 curr_in = (SInt*)GetImageData(error_vop->y_chan);
00368 sxy_in = GetImageSize(error_vop->y_chan);
00369 curr_end = curr_in + sxy_in;
00370 cnt = 0;
00371 while (curr_in != curr_end)
00372 {
00373 dc += *curr_in;
00374 cnt++;
00375 curr_in++;
00376 }
00377 dc /= cnt;
00378
00379 curr_in = (SInt*)GetImageData(error_vop->y_chan);
00380 sxy_in = GetImageSize(error_vop->y_chan);
00381 curr_end = curr_in + sxy_in;
00382 cnt = 0;
00383 while (curr_in != curr_end)
00384 {
00385 mad += fabs(*curr_in - dc);
00386 cnt++;
00387 curr_in++;
00388 }
00389 mad /= cnt;
00390 break;
00391 case FLOAT_TYPE:
00392 curr_fin = (Float*)GetImageData(error_vop->y_chan);
00393 sxy_in = GetImageSize(error_vop->y_chan);
00394 curr_fend = curr_fin + sxy_in;
00395 cnt = 0;
00396 while (curr_fin != curr_fend)
00397 {
00398 mad += fabs(*curr_fin);
00399 cnt++;
00400 curr_fin++;
00401 }
00402 mad /= cnt;
00403 break;
00404 default: break;
00405 }
00406
00407 #ifdef _RC_
00408 fprintf(ftrace, "The MAD of the VOP to be coded is %f.\n", mad);
00409 #endif
00410
00411 return mad;
00412 }
00413
00414