00001
00002
00003
00004
00005
00006
00007
00008
00009
00010
00011
00012
00013
00014
00015
00016
00017
00018
00019
00020 #include <math.h>
00021 #include <limits.h>
00022
00023 #include "global.h"
00024 #include "ratectl.h"
00025
00026
00027 static const float THETA = 1.3636F;
00028 static const float OMEGA = 0.9F;
00029 static const float MINVALUE = 4.0F;
00030
00031
00032
00033
00034
00035
00036
00037 void rc_alloc_quadratic( ImageParameters *p_Img, InputParameters *p_Inp, RCQuadratic **p_quad )
00038 {
00039 int rcBufSize = p_Img->FrameSizeInMbs / p_Inp->basicunit;
00040
00041 RCQuadratic *lprc;
00042
00043 (*p_quad) = (RCQuadratic *) malloc ( sizeof( RCQuadratic ) );
00044 if (NULL==(*p_quad))
00045 {
00046 no_mem_exit("rc_alloc_quadratic: (*p_quad)");
00047 }
00048 lprc = *p_quad;
00049
00050 lprc->PreviousFrameMAD = 1.0;
00051 lprc->CurrentFrameMAD = 1.0;
00052 lprc->Pprev_bits = 0;
00053 lprc->Target = 0;
00054 lprc->TargetField = 0;
00055 lprc->LowerBound = 0;
00056 lprc->UpperBound1 = INT_MAX;
00057 lprc->UpperBound2 = INT_MAX;
00058 lprc->Wp = 0.0;
00059 lprc->Wb = 0.0;
00060 lprc->AveWb = 0.0;
00061 lprc->PAveFrameQP = p_Inp->qp[0][I_SLICE] + p_Img->bitdepth_luma_qp_scale;
00062 lprc->m_Qc = lprc->PAveFrameQP;
00063 lprc->FieldQPBuffer = lprc->PAveFrameQP;
00064 lprc->FrameQPBuffer = lprc->PAveFrameQP;
00065 lprc->PAverageQp = lprc->PAveFrameQP;
00066 lprc->MyInitialQp = lprc->PAveFrameQP;
00067 lprc->AveWb = 0.0;
00068
00069 lprc->BUPFMAD = (double*) calloc ((rcBufSize), sizeof (double));
00070 if (NULL==lprc->BUPFMAD)
00071 {
00072 no_mem_exit("rc_alloc_quadratic: lprc->BUPFMAD");
00073 }
00074
00075 lprc->BUCFMAD = (double*) calloc ((rcBufSize), sizeof (double));
00076 if (NULL==lprc->BUCFMAD)
00077 {
00078 no_mem_exit("rc_alloc_quadratic: lprc->BUCFMAD");
00079 }
00080
00081 lprc->FCBUCFMAD = (double*) calloc ((rcBufSize), sizeof (double));
00082 if (NULL==lprc->FCBUCFMAD)
00083 {
00084 no_mem_exit("rc_alloc_quadratic: lprc->FCBUCFMAD");
00085 }
00086
00087 lprc->FCBUPFMAD = (double*) calloc ((rcBufSize), sizeof (double));
00088 if (NULL==lprc->FCBUPFMAD)
00089 {
00090 no_mem_exit("rc_alloc_quadratic: lprc->FCBUPFMAD");
00091 }
00092 }
00093
00094
00095
00096
00097
00098
00099
00100
00101 void rc_copy_quadratic( ImageParameters *p_Img, InputParameters *p_Inp, RCQuadratic *dst, RCQuadratic *src )
00102 {
00103 int rcBufSize = p_Img->FrameSizeInMbs / p_Inp->basicunit;
00104
00105 double *tmpBUPFMAD = dst->BUPFMAD;
00106 double *tmpBUCFMAD = dst->BUCFMAD;
00107 double *tmpFCBUPFMAD = dst->FCBUPFMAD;
00108 double *tmpFCBUCFMAD = dst->FCBUCFMAD;
00109
00110
00111 memcpy( (void *)dst, (void *)src, sizeof(RCQuadratic) );
00112
00113
00114 dst->BUPFMAD = tmpBUPFMAD;
00115 dst->BUCFMAD = tmpBUCFMAD;
00116 dst->FCBUPFMAD = tmpFCBUPFMAD;
00117 dst->FCBUCFMAD = tmpFCBUCFMAD;
00118
00119
00120 memcpy( (void *)dst->BUPFMAD, (void *)src->BUPFMAD, (rcBufSize) * sizeof (double) );
00121 memcpy( (void *)dst->BUCFMAD, (void *)src->BUCFMAD, (rcBufSize) * sizeof (double) );
00122 memcpy( (void *)dst->FCBUPFMAD, (void *)src->FCBUPFMAD, (rcBufSize) * sizeof (double) );
00123 memcpy( (void *)dst->FCBUCFMAD, (void *)src->FCBUCFMAD, (rcBufSize) * sizeof (double) );
00124 }
00125
00126
00127
00128
00129
00130
00131
00132
00133 void rc_free_quadratic(RCQuadratic **p_quad)
00134 {
00135 if (NULL!=(*p_quad)->BUPFMAD)
00136 {
00137 free ((*p_quad)->BUPFMAD);
00138 (*p_quad)->BUPFMAD = NULL;
00139 }
00140 if (NULL!=(*p_quad)->BUCFMAD)
00141 {
00142 free ((*p_quad)->BUCFMAD);
00143 (*p_quad)->BUCFMAD = NULL;
00144 }
00145 if (NULL!=(*p_quad)->FCBUCFMAD)
00146 {
00147 free ((*p_quad)->FCBUCFMAD);
00148 (*p_quad)->FCBUCFMAD = NULL;
00149 }
00150 if (NULL!=(*p_quad)->FCBUPFMAD)
00151 {
00152 free ((*p_quad)->FCBUPFMAD);
00153 (*p_quad)->FCBUPFMAD = NULL;
00154 }
00155 if (NULL!=(*p_quad))
00156 {
00157 free ((*p_quad));
00158 (*p_quad) = NULL;
00159 }
00160 }
00161
00162
00163
00164
00165
00166
00167
00168
00169
00170 void rc_init_seq(ImageParameters *p_Img, InputParameters *p_Inp, RCQuadratic *p_quad, RCGeneric *p_gen)
00171 {
00172 double L1,L2,L3,bpp;
00173 int qp, i;
00174
00175 switch ( p_Inp->RCUpdateMode )
00176 {
00177 case RC_MODE_0:
00178 p_Img->updateQP = updateQPRC0;
00179 break;
00180 case RC_MODE_1:
00181 p_Img->updateQP = updateQPRC1;
00182 break;
00183 case RC_MODE_2:
00184 p_Img->updateQP = updateQPRC2;
00185 break;
00186 case RC_MODE_3:
00187 p_Img->updateQP = updateQPRC3;
00188 break;
00189 default:
00190 p_Img->updateQP = updateQPRC0;
00191 break;
00192 }
00193
00194 p_Img->rc_update_pict_frame_ptr = rc_update_pict_frame;
00195 p_Img->rc_update_picture_ptr = rc_update_picture;
00196 p_Img->rc_init_pict_ptr = rc_init_pict;
00197
00198 p_quad->Xp=0;
00199 p_quad->Xb=0;
00200
00201 p_quad->bit_rate = (float) p_Inp->bit_rate;
00202 p_quad->frame_rate = p_Img->framerate;
00203 p_quad->PrevBitRate = p_quad->bit_rate;
00204
00205
00206 if(p_Inp->basicunit > p_Img->FrameSizeInMbs)
00207 p_Inp->basicunit = p_Img->FrameSizeInMbs;
00208 if(p_Inp->basicunit < p_Img->FrameSizeInMbs)
00209 p_quad->TotalNumberofBasicUnit = p_Img->FrameSizeInMbs/p_Inp->basicunit;
00210 else
00211 p_quad->TotalNumberofBasicUnit = 1;
00212
00213
00214 p_gen->CurrentBufferFullness = 0;
00215 p_quad->GOPTargetBufferLevel = (double) p_gen->CurrentBufferFullness;
00216
00217
00218 p_quad->m_windowSize = 0;
00219 p_quad->MADm_windowSize = 0;
00220 p_gen->NumberofCodedBFrame = 0;
00221 p_quad->NumberofCodedPFrame = 0;
00222 p_gen->NumberofGOP = 0;
00223
00224 p_gen->RemainingBits = 0;
00225
00226 if(p_Inp->NumberBFrames>0)
00227 {
00228 p_quad->GAMMAP=0.25;
00229 p_quad->BETAP=0.9;
00230 }
00231 else
00232 {
00233 p_quad->GAMMAP=0.5;
00234 p_quad->BETAP=0.5;
00235 }
00236
00237
00238 p_quad->PPreHeader=0;
00239
00240 p_quad->Pm_X1 = p_quad->bit_rate * 1.0;
00241 p_quad->Pm_X2 = 0.0;
00242
00243 p_quad->PMADPictureC1 = 1.0;
00244 p_quad->PMADPictureC2 = 0.0;
00245
00246
00247 for(i=0;i<21;i++)
00248 {
00249 p_quad->Pm_rgQp[i] = 0;
00250 p_quad->Pm_rgRp[i] = 0.0;
00251 p_quad->PPictureMAD[i] = 0.0;
00252 }
00253
00254
00255 p_quad->PMaxQpChange = p_Inp->RCMaxQPChange;
00256
00257
00258 p_quad->PAveHeaderBits1 = 0;
00259 p_quad->PAveHeaderBits3 = 0;
00260 p_quad->DDquant = (p_quad->TotalNumberofBasicUnit>=9 ? 1 : 2);
00261
00262 p_quad->MBPerRow = p_Img->PicWidthInMbs;
00263
00264
00265 p_gen->FieldControl=0;
00266
00267 if (p_Inp->SeinitialQP==0)
00268 {
00269
00270 bpp = 1.0*p_quad->bit_rate /(p_quad->frame_rate*p_Img->size);
00271
00272 if (p_Img->width == 176)
00273 {
00274 L1 = 0.1;
00275 L2 = 0.3;
00276 L3 = 0.6;
00277 }
00278 else if (p_Img->width == 352)
00279 {
00280 L1 = 0.2;
00281 L2 = 0.6;
00282 L3 = 1.2;
00283 }
00284 else
00285 {
00286 L1 = 0.6;
00287 L2 = 1.4;
00288 L3 = 2.4;
00289 }
00290 if (bpp<= L1)
00291 qp = 35;
00292 else if(bpp<=L2)
00293 qp = 25;
00294 else if(bpp<=L3)
00295 qp = 20;
00296 else
00297 qp = 10;
00298 p_Inp->SeinitialQP = qp;
00299 }
00300
00301
00302 p_quad->bitdepth_qp_scale = p_Img->bitdepth_luma_qp_scale;
00303 }
00304
00305
00306
00307
00308
00309
00310
00311
00312 void rc_init_GOP(ImageParameters *p_Img, InputParameters *p_Inp, RCQuadratic *p_quad, RCGeneric *p_gen, int np, int nb)
00313 {
00314 Boolean Overum=FALSE;
00315 int OverBits, OverDuantQp;
00316 int GOPDquant;
00317 int64 AllocatedBits;
00318
00319
00320 switch( p_Inp->RCUpdateMode )
00321 {
00322 case RC_MODE_3:
00323 {
00324 int sum = 0, tmp, level, levels = 0, num_frames[RC_MAX_TEMPORAL_LEVELS];
00325 float numer, denom;
00326 int gop = p_Inp->NumberBFrames + 1;
00327 memset( num_frames, 0, RC_MAX_TEMPORAL_LEVELS * sizeof(int) );
00328
00329 if ( p_Inp->NumberBFrames )
00330 {
00331 if ( p_Inp->HierarchicalCoding == 1 )
00332 {
00333 levels = 2;
00334 num_frames[0] = p_Inp->NumberBFrames >> 1;
00335 num_frames[1] = (p_Inp->NumberBFrames - num_frames[0]) >= 0 ? (p_Inp->NumberBFrames - num_frames[0]) : 0;
00336 }
00337 else if ( p_Inp->HierarchicalCoding == 2 )
00338 {
00339
00340 tmp = gop;
00341 while ( tmp )
00342 {
00343 sum += tmp & 1;
00344 tmp >>= 1;
00345 }
00346 assert( sum == 1 );
00347
00348
00349 levels = 0;
00350 tmp = gop;
00351 while ( tmp > 1 )
00352 {
00353 tmp >>= 1;
00354 num_frames[levels] = 1 << levels;
00355 levels++;
00356 }
00357 assert( levels >= 1 && levels <= RC_MAX_TEMPORAL_LEVELS );
00358 }
00359 else if ( p_Inp->HierarchicalCoding == 3 )
00360 {
00361 fprintf(stderr, "\n RCUpdateMode=3 and HierarchicalCoding == 3 are currently not supported");
00362 exit(1);
00363 }
00364 else
00365 {
00366 levels = 1;
00367 num_frames[0] = p_Inp->NumberBFrames;
00368 }
00369 p_gen->temporal_levels = levels;
00370 }
00371 else
00372 {
00373 for ( level = 0; level < RC_MAX_TEMPORAL_LEVELS; level++ )
00374 {
00375 p_Inp->RCBSliceBitRatio[level] = 0.0F;
00376 }
00377 p_gen->temporal_levels = 0;
00378 }
00379
00380 numer = (float)(( (!p_Inp->intra_period ? 1 : p_Inp->intra_period) * gop) * ((double)p_Inp->bit_rate / p_Inp->source.frame_rate));
00381 denom = 0.0F;
00382
00383 for ( level = 0; level < levels; level++ )
00384 {
00385 denom += (float)(num_frames[level] * p_Inp->RCBSliceBitRatio[level]);
00386 p_gen->hierNb[level] = num_frames[level] * np;
00387 }
00388 denom += 1.0F;
00389 if ( p_Inp->intra_period >= 1 )
00390 {
00391 denom *= (float)p_Inp->intra_period;
00392 denom += (float)p_Inp->RCISliceBitRatio - 1.0F;
00393 }
00394
00395
00396 p_gen->RCPSliceBits = (int) floor( numer / denom + 0.5F );
00397 p_gen->RCISliceBits = (p_Inp->intra_period) ? (int)(p_Inp->RCISliceBitRatio * p_gen->RCPSliceBits + 0.5) : 0;
00398
00399 for ( level = 0; level < levels; level++ )
00400 {
00401 p_gen->RCBSliceBits[level] = (int)floor(p_Inp->RCBSliceBitRatio[level] * p_gen->RCPSliceBits + 0.5);
00402 }
00403
00404 p_gen->NISlice = (p_Inp->intra_period) ? ((p_Inp->no_frm_base - 1) / p_Inp->intra_period) : 0;
00405 p_gen->NPSlice = p_Inp->no_frm_base - 1 - p_gen->NISlice;
00406 }
00407 break;
00408 default:
00409 break;
00410 }
00411
00412
00413
00414
00415 if(p_gen->RemainingBits<0)
00416 Overum=TRUE;
00417 OverBits=-(int)(p_gen->RemainingBits);
00418
00419
00420 p_quad->LowerBound = (int)(p_gen->RemainingBits + p_quad->bit_rate / p_quad->frame_rate);
00421 p_quad->UpperBound1 = (int)(p_gen->RemainingBits + (p_quad->bit_rate * 2.048));
00422
00423
00424 AllocatedBits = (int64) floor((1 + np + nb) * p_quad->bit_rate / p_quad->frame_rate + 0.5);
00425 p_gen->RemainingBits += AllocatedBits;
00426 p_quad->Np = np;
00427 p_quad->Nb = nb;
00428
00429 OverDuantQp=(int)(8 * OverBits/AllocatedBits+0.5);
00430 p_quad->GOPOverdue=FALSE;
00431
00432
00433
00434 if ( !p_Inp->PicInterlace && p_Inp->MbInterlace && p_Inp->basicunit == p_Img->FrameSizeInMbs )
00435 p_gen->NoGranularFieldRC = 0;
00436 else
00437 p_gen->NoGranularFieldRC = 1;
00438
00439
00440 p_quad->TotalPFrame=np;
00441 p_gen->NumberofGOP++;
00442 if(p_gen->NumberofGOP==1)
00443 {
00444 p_quad->MyInitialQp = p_Inp->SeinitialQP + p_quad->bitdepth_qp_scale;
00445 p_quad->CurrLastQP = p_quad->MyInitialQp - 1;
00446 p_quad->QPLastGOP = p_quad->MyInitialQp;
00447
00448 p_quad->PAveFrameQP = p_quad->MyInitialQp;
00449 p_quad->m_Qc = p_quad->PAveFrameQP;
00450 p_quad->FieldQPBuffer = p_quad->PAveFrameQP;
00451 p_quad->FrameQPBuffer = p_quad->PAveFrameQP;
00452 p_quad->PAverageQp = p_quad->PAveFrameQP;
00453 }
00454 else
00455 {
00456
00457 if( p_Inp->PicInterlace == ADAPTIVE_CODING || p_Inp->MbInterlace )
00458 {
00459 if (p_gen->FieldFrame == 1)
00460 {
00461 p_quad->TotalQpforPPicture += p_quad->FrameQPBuffer;
00462 p_quad->QPLastPFrame = p_quad->FrameQPBuffer;
00463 }
00464 else
00465 {
00466 p_quad->TotalQpforPPicture += p_quad->FieldQPBuffer;
00467 p_quad->QPLastPFrame = p_quad->FieldQPBuffer;
00468 }
00469 }
00470
00471 p_quad->PAverageQp=(int)(1.0 * p_quad->TotalQpforPPicture / p_quad->NumberofPPicture+0.5);
00472
00473 GOPDquant=(int)((1.0*(np+nb+1)/15.0) + 0.5);
00474 if(GOPDquant>2)
00475 GOPDquant=2;
00476
00477 p_quad->PAverageQp -= GOPDquant;
00478
00479 if (p_quad->PAverageQp > (p_quad->QPLastPFrame - 2))
00480 p_quad->PAverageQp--;
00481
00482
00483 p_quad->PAverageQp = iClip3(p_quad->QPLastGOP - 2, p_quad->QPLastGOP + 2, p_quad->PAverageQp);
00484
00485 p_quad->PAverageQp = iClip3(p_Img->RCMinQP + p_quad->bitdepth_qp_scale, p_Img->RCMaxQP + p_quad->bitdepth_qp_scale, p_quad->PAverageQp);
00486
00487 p_quad->MyInitialQp = p_quad->PAverageQp;
00488 p_quad->Pm_Qp = p_quad->PAverageQp;
00489 p_quad->PAveFrameQP = p_quad->PAverageQp;
00490 p_quad->QPLastGOP = p_quad->MyInitialQp;
00491 p_quad->PrevLastQP = p_quad->CurrLastQP;
00492 p_quad->CurrLastQP = p_quad->MyInitialQp - 1;
00493 }
00494
00495 p_quad->TotalQpforPPicture=0;
00496 p_quad->NumberofPPicture=0;
00497 p_quad->NumberofBFrames=0;
00498 }
00499
00500
00501
00502
00503
00504
00505
00506
00507
00508 void rc_init_pict(ImageParameters *p_Img, InputParameters *p_Inp, RCQuadratic *p_quad, RCGeneric *p_gen, int fieldpic,int topfield,int targetcomputation, float mult)
00509 {
00510 int tmp_T;
00511
00512
00513 if(p_Inp->MbInterlace)
00514 p_quad->TotalNumberofBasicUnit = p_Img->FrameSizeInMbs / p_Img->BasicUnit;
00515 else
00516 p_quad->TotalNumberofBasicUnit = p_Img->FrameSizeInMbs / p_Inp->basicunit;
00517
00518 p_Img->NumberofCodedMacroBlocks = 0;
00519
00520
00521
00522
00523 if(p_Inp->channel_type==1)
00524 {
00525 if(p_quad->NumberofCodedPFrame==58)
00526 p_quad->bit_rate *= 1.5;
00527 else if(p_quad->NumberofCodedPFrame==59)
00528 p_quad->PrevBitRate = p_quad->bit_rate;
00529 }
00530
00531
00532 if((fieldpic||topfield) && targetcomputation)
00533 {
00534 if ( p_Img->type == P_SLICE || (p_Inp->RCUpdateMode == RC_MODE_1 && (p_Img->number !=0)) )
00535 {
00536
00537
00538 if(p_quad->PrevBitRate!=p_quad->bit_rate)
00539 p_gen->RemainingBits +=(int) floor((p_quad->bit_rate-p_quad->PrevBitRate)*(p_quad->Np + p_quad->Nb)/p_quad->frame_rate+0.5);
00540
00541
00542
00543 if(p_Img->BasicUnit == p_Img->FrameSizeInMbs)
00544 {
00545 if(p_quad->NumberofPPicture==1)
00546 {
00547 p_quad->TargetBufferLevel = (double) p_gen->CurrentBufferFullness;
00548 p_quad->DeltaP = (p_gen->CurrentBufferFullness - p_quad->GOPTargetBufferLevel) / (p_quad->TotalPFrame-1);
00549 p_quad->TargetBufferLevel -= p_quad->DeltaP;
00550 }
00551 else if(p_quad->NumberofPPicture>1)
00552 p_quad->TargetBufferLevel -= p_quad->DeltaP;
00553 }
00554
00555 else
00556 {
00557 if(p_quad->NumberofCodedPFrame>0)
00558 {
00559
00560 if(((p_Inp->PicInterlace==ADAPTIVE_CODING)||(p_Inp->MbInterlace))&&(p_gen->FieldControl==1))
00561 memcpy((void *)p_quad->FCBUPFMAD,(void *)p_quad->FCBUCFMAD, p_quad->TotalNumberofBasicUnit * sizeof(double));
00562 else
00563 memcpy((void *)p_quad->BUPFMAD,(void *)p_quad->BUCFMAD, p_quad->TotalNumberofBasicUnit * sizeof(double));
00564 }
00565
00566 if(p_gen->NumberofGOP==1)
00567 {
00568 if(p_quad->NumberofPPicture==1)
00569 {
00570 p_quad->TargetBufferLevel = (double) p_gen->CurrentBufferFullness;
00571 p_quad->DeltaP = (p_gen->CurrentBufferFullness - p_quad->GOPTargetBufferLevel)/(p_quad->TotalPFrame - 1);
00572 p_quad->TargetBufferLevel -= p_quad->DeltaP;
00573 }
00574 else if(p_quad->NumberofPPicture>1)
00575 p_quad->TargetBufferLevel -= p_quad->DeltaP;
00576 }
00577 else if(p_gen->NumberofGOP>1)
00578 {
00579 if(p_quad->NumberofPPicture==0)
00580 {
00581 p_quad->TargetBufferLevel = (double) p_gen->CurrentBufferFullness;
00582 p_quad->DeltaP = (p_gen->CurrentBufferFullness - p_quad->GOPTargetBufferLevel) / p_quad->TotalPFrame;
00583 p_quad->TargetBufferLevel -= p_quad->DeltaP;
00584 }
00585 else if(p_quad->NumberofPPicture>0)
00586 p_quad->TargetBufferLevel -= p_quad->DeltaP;
00587 }
00588 }
00589
00590 if(p_quad->NumberofCodedPFrame==1)
00591 p_quad->AveWp = p_quad->Wp;
00592
00593 if((p_quad->NumberofCodedPFrame<8)&&(p_quad->NumberofCodedPFrame>1))
00594 p_quad->AveWp = (p_quad->AveWp + p_quad->Wp * (p_quad->NumberofCodedPFrame-1))/p_quad->NumberofCodedPFrame;
00595 else if(p_quad->NumberofCodedPFrame>1)
00596 p_quad->AveWp = (p_quad->Wp + 7 * p_quad->AveWp) / 8;
00597
00598
00599 if(p_Inp->NumberBFrames>0)
00600 {
00601
00602 p_quad->TargetBufferLevel += (p_quad->AveWp * (p_Inp->NumberBFrames + 1)*p_quad->bit_rate\
00603 /(p_quad->frame_rate*(p_quad->AveWp+p_quad->AveWb*p_Inp->NumberBFrames))-p_quad->bit_rate/p_quad->frame_rate);
00604 }
00605 }
00606 else if ( p_Img->type == B_SLICE )
00607 {
00608
00609 if(p_quad->PrevBitRate != p_quad->bit_rate)
00610 p_gen->RemainingBits +=(int) floor((p_quad->bit_rate-p_quad->PrevBitRate) * (p_quad->Np + p_quad->Nb) / p_quad->frame_rate+0.5);
00611 if(p_gen->NumberofCodedBFrame == 1)
00612 {
00613 if(p_quad->NumberofCodedPFrame == 1)
00614 {
00615 p_quad->AveWp = p_quad->Wp;
00616 }
00617 p_quad->AveWb = p_quad->Wb;
00618 }
00619 else if(p_gen->NumberofCodedBFrame > 1)
00620 {
00621
00622 if(p_gen->NumberofCodedBFrame<8)
00623 p_quad->AveWb = (p_quad->AveWb + p_quad->Wb*(p_gen->NumberofCodedBFrame-1)) / p_gen->NumberofCodedBFrame;
00624 else
00625 p_quad->AveWb = (p_quad->Wb + 7 * p_quad->AveWb) / 8;
00626 }
00627 }
00628
00629 if( p_Img->type == P_SLICE || ( (p_Img->number != 0) && (p_Inp->RCUpdateMode == RC_MODE_1 || p_Inp->RCUpdateMode == RC_MODE_3 ) ) )
00630 {
00631
00632 if(p_Img->BasicUnit == p_Img->FrameSizeInMbs || (p_Inp->RCUpdateMode == RC_MODE_3) )
00633 {
00634 if(p_quad->NumberofCodedPFrame>0)
00635 {
00636 if (p_Inp->RCUpdateMode == RC_MODE_3)
00637 {
00638 int level_idx = (p_Img->type == B_SLICE && p_Inp->HierarchicalCoding) ? (p_gen->temporal_levels - 1 - p_Img->gop_structure[p_Img->b_frame_to_code-1].hierarchy_layer) : 0;
00639 int bitrate = (p_Img->type == B_SLICE) ? p_gen->RCBSliceBits[ level_idx ]
00640 : ( p_Img->type == P_SLICE ? p_gen->RCPSliceBits : p_gen->RCISliceBits );
00641 int level, denom = p_gen->NISlice * p_gen->RCISliceBits + p_gen->NPSlice * p_gen->RCPSliceBits;
00642 if ( p_Inp->HierarchicalCoding )
00643 {
00644 for ( level = 0; level < p_gen->temporal_levels; level++ )
00645 denom += p_gen->hierNb[ level ] * p_gen->RCBSliceBits[ level ];
00646 }
00647 else
00648 {
00649 denom += p_gen->hierNb[0] * p_gen->RCBSliceBits[0];
00650 }
00651
00652 p_quad->Target = (int) floor( (float)(1.0 * bitrate * p_gen->RemainingBits) / (float)denom + 0.5F );
00653
00654 tmp_T = imax(0, (int) floor( (double)bitrate - p_quad->GAMMAP * (p_gen->CurrentBufferFullness-p_quad->TargetBufferLevel) + 0.5) );
00655
00656
00657 switch( p_Img->type )
00658 {
00659 case B_SLICE:
00660 p_quad->Target = (int) floor( (float)p_quad->Target / p_Inp->RCBoverPRatio + 0.5F);
00661 break;
00662 case I_SLICE:
00663 p_quad->Target = (int) floor( (float)p_quad->Target / (p_Inp->RCIoverPRatio * 4.0) + 0.5F);
00664 break;
00665 case P_SLICE:
00666 default:
00667 break;
00668 }
00669 }
00670 else
00671 {
00672 p_quad->Target = (int) floor( p_quad->Wp * p_gen->RemainingBits / (p_quad->Np * p_quad->Wp + p_quad->Nb * p_quad->Wb) + 0.5);
00673 tmp_T = imax(0, (int) floor(p_quad->bit_rate / p_quad->frame_rate - p_quad->GAMMAP * (p_gen->CurrentBufferFullness-p_quad->TargetBufferLevel) + 0.5));
00674 p_quad->Target = (int) floor(p_quad->BETAP * (p_quad->Target - tmp_T) + tmp_T + 0.5);
00675 }
00676 }
00677 }
00678
00679 else
00680 {
00681 if(((p_gen->NumberofGOP == 1)&&(p_quad->NumberofCodedPFrame>0))
00682 || (p_gen->NumberofGOP > 1))
00683 {
00684 p_quad->Target = (int) (floor( p_quad->Wp * p_gen->RemainingBits / (p_quad->Np * p_quad->Wp + p_quad->Nb * p_quad->Wb) + 0.5));
00685 tmp_T = imax(0, (int) (floor(p_quad->bit_rate / p_quad->frame_rate - p_quad->GAMMAP * (p_gen->CurrentBufferFullness-p_quad->TargetBufferLevel) + 0.5)));
00686 p_quad->Target = (int) (floor(p_quad->BETAP * (p_quad->Target - tmp_T) + tmp_T + 0.5));
00687 }
00688 }
00689 p_quad->Target = (int)(mult * p_quad->Target);
00690
00691
00692 if ( p_Inp->RCUpdateMode != RC_MODE_3 || p_Img->type == P_SLICE )
00693 p_quad->Target = iClip3(p_quad->LowerBound, p_quad->UpperBound2, p_quad->Target);
00694 if((topfield) || (fieldpic && ((p_Inp->PicInterlace==ADAPTIVE_CODING)||(p_Inp->MbInterlace))))
00695 p_quad->TargetField=p_quad->Target;
00696 }
00697 }
00698
00699 if(fieldpic || topfield)
00700 {
00701
00702 p_gen->NumberofHeaderBits = 0;
00703 p_gen->NumberofTextureBits = 0;
00704
00705
00706 if(p_Img->BasicUnit < p_Img->FrameSizeInMbs)
00707 {
00708 p_quad->TotalFrameQP = 0;
00709 p_gen->NumberofBasicUnitHeaderBits = 0;
00710 p_gen->NumberofBasicUnitTextureBits = 0;
00711 p_gen->TotalMADBasicUnit = 0;
00712 if(p_gen->FieldControl==0)
00713 p_quad->NumberofBasicUnit = p_quad->TotalNumberofBasicUnit;
00714 else
00715 p_quad->NumberofBasicUnit = p_quad->TotalNumberofBasicUnit >> 1;
00716 }
00717 }
00718
00719 if( ( p_Img->type==P_SLICE || (p_Inp->RCUpdateMode == RC_MODE_1 && (p_Img->number != 0)) ) && p_Img->BasicUnit < p_Img->FrameSizeInMbs && p_gen->FieldControl == 1 )
00720 {
00721
00722 if(topfield)
00723 {
00724 p_quad->bits_topfield=0;
00725 p_quad->Target=(int)(p_quad->TargetField*0.6);
00726 }
00727
00728 else
00729 {
00730 p_quad->Target=p_quad->TargetField-p_quad->bits_topfield;
00731 p_gen->NumberofBasicUnitHeaderBits=0;
00732 p_gen->NumberofBasicUnitTextureBits=0;
00733 p_gen->TotalMADBasicUnit=0;
00734 p_quad->NumberofBasicUnit=p_quad->TotalNumberofBasicUnit >> 1;
00735 }
00736 }
00737 }
00738
00739
00740
00741
00742
00743
00744
00745
00746
00747
00748
00749
00750
00751
00752
00753
00754
00755
00756
00757 void rc_update_pict(ImageParameters *p_Img, InputParameters *p_Inp, RCQuadratic *p_quad, RCGeneric *p_gen, int nbits)
00758 {
00759 int delta_bits = (nbits - (int)floor(p_quad->bit_rate / p_quad->frame_rate + 0.5F) );
00760
00761 p_gen->RemainingBits -= nbits;
00762 p_gen->CurrentBufferFullness += delta_bits;
00763
00764
00765 p_quad->LowerBound -= (int) delta_bits;
00766 p_quad->UpperBound1 -= (int) delta_bits;
00767 p_quad->UpperBound2 = (int)(OMEGA * p_quad->UpperBound1);
00768
00769
00770 if( p_Img->type==P_SLICE || (p_Inp->RCUpdateMode == RC_MODE_1 && p_Img->frm_number) )
00771 {
00772 updateRCModel(p_Img, p_Inp, p_quad, p_gen);
00773 if ( p_Inp->RCUpdateMode == RC_MODE_3 )
00774 p_quad->PreviousWholeFrameMAD = ComputeFrameMAD(p_Img);
00775 }
00776 }
00777
00778
00779
00780
00781
00782
00783
00784
00785
00786
00787
00788
00789
00790
00791
00792
00793 void rc_update_picture( ImageParameters *p_Img, InputParameters *p_Inp, int bits )
00794 {
00795 rc_update_pict(p_Img, p_Inp, p_Img->p_rc_quad, p_Img->p_rc_gen, bits);
00796 }
00797
00798 int updateComplexity( ImageParameters *p_Img, RCQuadratic *p_quad, RCGeneric *p_gen, Boolean is_updated, int nbits )
00799 {
00800 double Avem_Qc;
00801
00802
00803 if(p_Img->BasicUnit == p_Img->FrameSizeInMbs)
00804 return ((int) floor(nbits * p_quad->m_Qc + 0.5));
00805
00806 else
00807 {
00808 if( is_updated )
00809 {
00810 if( p_gen->NoGranularFieldRC == 0 || p_gen->FieldControl == 0 )
00811 {
00812 Avem_Qc = (double)p_quad->TotalFrameQP / (double)p_quad->TotalNumberofBasicUnit;
00813 return ((int)floor(nbits * Avem_Qc + 0.5));
00814 }
00815 }
00816 else if( p_Img->type == B_SLICE )
00817 return ((int) floor(nbits * p_quad->m_Qc + 0.5));
00818 }
00819 return 0;
00820 }
00821
00822 void updatePparams( RCQuadratic *p_quad, RCGeneric *p_gen, int complexity )
00823 {
00824 p_quad->Xp = complexity;
00825 p_quad->Np--;
00826 p_quad->Wp = p_quad->Xp;
00827 p_quad->Pm_Hp = p_gen->NumberofHeaderBits;
00828 p_quad->NumberofCodedPFrame++;
00829 p_quad->NumberofPPicture++;
00830 }
00831
00832 void updateBparams( RCQuadratic *p_quad, RCGeneric *p_gen, int complexity )
00833 {
00834 p_quad->Xb = complexity;
00835 p_quad->Nb--;
00836 p_quad->Wb = p_quad->Xb / THETA;
00837 p_quad->NumberofBFrames++;
00838 p_gen->NumberofCodedBFrame++;
00839 }
00840
00841
00842
00843
00844
00845
00846
00847
00848
00849
00850
00851
00852
00853
00854
00855
00856
00857
00858 void rc_update_pict_frame(ImageParameters *p_Img, InputParameters *p_Inp, RCQuadratic *p_quad, RCGeneric *p_gen, int nbits)
00859 {
00860
00861 int complexity = 0;
00862
00863 switch( p_Inp->RCUpdateMode )
00864 {
00865 case RC_MODE_0:
00866 case RC_MODE_2:
00867 default:
00868 complexity = updateComplexity( p_Img, p_quad, p_gen, (Boolean) (p_Img->type == P_SLICE), nbits );
00869 if ( p_Img->type == P_SLICE )
00870 {
00871 if( p_gen->NoGranularFieldRC == 0 || p_gen->FieldControl == 0 )
00872 updatePparams( p_quad, p_gen, complexity );
00873 else
00874 p_gen->NoGranularFieldRC = 0;
00875 }
00876 else if ( p_Img->type == B_SLICE )
00877 updateBparams( p_quad, p_gen, complexity );
00878 break;
00879 case RC_MODE_1:
00880 complexity = updateComplexity( p_Img, p_quad, p_gen, (Boolean) (p_Img->number != 0), nbits );
00881 if ( p_Img->number != 0 )
00882 {
00883 if( p_gen->NoGranularFieldRC == 0 || p_gen->FieldControl == 0 )
00884 updatePparams( p_quad, p_gen, complexity );
00885 else
00886 p_gen->NoGranularFieldRC = 0;
00887 }
00888 break;
00889 case RC_MODE_3:
00890 complexity = updateComplexity( p_Img, p_quad, p_gen, (Boolean) (p_Img->type == P_SLICE), nbits );
00891 if (p_Img->type == I_SLICE && (p_Img->number != 0))
00892 p_gen->NISlice--;
00893
00894 if ( p_Img->type == P_SLICE )
00895 {
00896 if( p_gen->NoGranularFieldRC == 0 || p_gen->FieldControl == 0 )
00897 {
00898 updatePparams( p_quad, p_gen, complexity );
00899 p_gen->NPSlice--;
00900 }
00901 else
00902 p_gen->NoGranularFieldRC = 0;
00903 }
00904 else if ( p_Img->type == B_SLICE )
00905 {
00906 updateBparams( p_quad, p_gen, complexity );
00907 p_gen->hierNb[ p_Inp->HierarchicalCoding ? (p_gen->temporal_levels - 1 - p_Img->gop_structure[p_Img->b_frame_to_code-1].hierarchy_layer) : 0 ]--;
00908 }
00909 break;
00910 }
00911 }
00912
00913
00914
00915
00916
00917
00918
00919
00920
00921 void updateRCModel (ImageParameters *p_Img, InputParameters *p_Inp, RCQuadratic *p_quad, RCGeneric *p_gen)
00922 {
00923 int n_windowSize;
00924 int i;
00925 double std = 0.0, threshold;
00926 int m_Nc = p_quad->NumberofCodedPFrame;
00927 Boolean MADModelFlag = FALSE;
00928 static Boolean m_rgRejected[RC_MODEL_HISTORY];
00929 static double error [RC_MODEL_HISTORY];
00930
00931 if( p_Img->type == P_SLICE || (p_Inp->RCUpdateMode == RC_MODE_1 && (p_Img->number != 0)) )
00932 {
00933
00934 if(p_Img->BasicUnit == p_Img->FrameSizeInMbs)
00935 {
00936 p_quad->CurrentFrameMAD = ComputeFrameMAD(p_Img);
00937 m_Nc=p_quad->NumberofCodedPFrame;
00938 }
00939
00940 else
00941 {
00942
00943 p_quad->CurrentFrameMAD = (double) ((p_gen->TotalMADBasicUnit >> 8)/p_Img->BasicUnit);
00944 p_gen->TotalMADBasicUnit=0;
00945
00946
00947 p_quad->CodedBasicUnit=p_quad->TotalNumberofBasicUnit-p_quad->NumberofBasicUnit;
00948 if(p_quad->CodedBasicUnit > 0)
00949 {
00950 p_quad->PAveHeaderBits1=(int)((double)(p_quad->PAveHeaderBits1*(p_quad->CodedBasicUnit-1)+
00951 p_gen->NumberofBasicUnitHeaderBits)/p_quad->CodedBasicUnit+0.5);
00952 if(p_quad->PAveHeaderBits3 == 0)
00953 p_quad->PAveHeaderBits2 = p_quad->PAveHeaderBits1;
00954 else
00955 {
00956 p_quad->PAveHeaderBits2 = (int)((double)(p_quad->PAveHeaderBits1 * p_quad->CodedBasicUnit+
00957 p_quad->PAveHeaderBits3 * p_quad->NumberofBasicUnit)/p_quad->TotalNumberofBasicUnit+0.5);
00958 }
00959 }
00960
00961 if ((p_quad->NumberofBasicUnit >= p_quad->TotalNumberofBasicUnit) || (p_quad->NumberofBasicUnit<0))
00962 {
00963 fprintf(stderr, "Write into invalid memory in updateRCModel at frame %d, p_quad->NumberofBasicUnit %d\n",
00964 p_Img->framepoc, p_quad->NumberofBasicUnit);
00965 }
00966
00967
00968 if(((p_Inp->PicInterlace == ADAPTIVE_CODING) || (p_Inp->MbInterlace)) && (p_gen->FieldControl == 1))
00969 p_quad->FCBUCFMAD[p_quad->TotalNumberofBasicUnit-1-p_quad->NumberofBasicUnit]=p_quad->CurrentFrameMAD;
00970 else
00971 p_quad->BUCFMAD[p_quad->TotalNumberofBasicUnit-1-p_quad->NumberofBasicUnit]=p_quad->CurrentFrameMAD;
00972
00973 if(p_quad->NumberofBasicUnit != 0)
00974 m_Nc = p_quad->NumberofCodedPFrame * p_quad->TotalNumberofBasicUnit + p_quad->CodedBasicUnit;
00975 else
00976 m_Nc = (p_quad->NumberofCodedPFrame-1) * p_quad->TotalNumberofBasicUnit + p_quad->CodedBasicUnit;
00977 }
00978
00979 if(m_Nc > 1)
00980 MADModelFlag=TRUE;
00981
00982 p_quad->PPreHeader = p_gen->NumberofHeaderBits;
00983 for (i = (RC_MODEL_HISTORY-2); i > 0; i--)
00984 {
00985 p_quad->Pm_rgQp[i] = p_quad->Pm_rgQp[i - 1];
00986 p_quad->m_rgQp[i] = p_quad->Pm_rgQp[i];
00987 p_quad->Pm_rgRp[i] = p_quad->Pm_rgRp[i - 1];
00988 p_quad->m_rgRp[i] = p_quad->Pm_rgRp[i];
00989 }
00990 p_quad->Pm_rgQp[0] = QP2Qstep(p_quad->m_Qc);
00991
00992 if(p_Img->BasicUnit == p_Img->FrameSizeInMbs)
00993 p_quad->Pm_rgRp[0] = p_gen->NumberofTextureBits*1.0/p_quad->CurrentFrameMAD;
00994
00995 else
00996 p_quad->Pm_rgRp[0] = p_gen->NumberofBasicUnitTextureBits*1.0/p_quad->CurrentFrameMAD;
00997
00998 p_quad->m_rgQp[0] = p_quad->Pm_rgQp[0];
00999 p_quad->m_rgRp[0] = p_quad->Pm_rgRp[0];
01000 p_quad->m_X1 = p_quad->Pm_X1;
01001 p_quad->m_X2 = p_quad->Pm_X2;
01002
01003
01004 n_windowSize = (p_quad->CurrentFrameMAD>p_quad->PreviousFrameMAD)
01005 ? (int)(p_quad->PreviousFrameMAD/p_quad->CurrentFrameMAD * (RC_MODEL_HISTORY-1) )
01006 : (int)(p_quad->CurrentFrameMAD/p_quad->PreviousFrameMAD *(RC_MODEL_HISTORY-1));
01007 n_windowSize=iClip3(1, m_Nc, n_windowSize);
01008 n_windowSize=imin(n_windowSize,p_quad->m_windowSize+1);
01009 n_windowSize=imin(n_windowSize,(RC_MODEL_HISTORY-1));
01010
01011
01012 p_quad->m_windowSize=n_windowSize;
01013
01014 for (i = 0; i < (RC_MODEL_HISTORY-1); i++)
01015 {
01016 m_rgRejected[i] = FALSE;
01017 }
01018
01019
01020 RCModelEstimator (p_Img, p_Inp, p_quad, n_windowSize, m_rgRejected);
01021
01022 n_windowSize = p_quad->m_windowSize;
01023
01024
01025 for (i = 0; i < (int) n_windowSize; i++)
01026 {
01027 error[i] = p_quad->m_X1 / p_quad->m_rgQp[i] + p_quad->m_X2 / (p_quad->m_rgQp[i] * p_quad->m_rgQp[i]) - p_quad->m_rgRp[i];
01028 std += error[i] * error[i];
01029 }
01030 threshold = (n_windowSize == 2) ? 0 : sqrt (std / n_windowSize);
01031 for (i = 0; i < (int) n_windowSize; i++)
01032 {
01033 if (fabs(error[i]) > threshold)
01034 m_rgRejected[i] = TRUE;
01035 }
01036
01037 m_rgRejected[0] = FALSE;
01038
01039
01040 RCModelEstimator (p_Img, p_Inp, p_quad, n_windowSize, m_rgRejected);
01041
01042 if( MADModelFlag )
01043 updateMADModel(p_Img, p_Inp, p_quad, p_gen);
01044 else if( p_Img->type == P_SLICE || (p_Inp->RCUpdateMode == RC_MODE_1 && (p_Img->number != 0)) )
01045 p_quad->PPictureMAD[0] = p_quad->CurrentFrameMAD;
01046 }
01047 }
01048
01049
01050
01051
01052
01053
01054
01055
01056 void RCModelEstimator (ImageParameters *p_Img, InputParameters *p_Inp, RCQuadratic *p_quad, int n_windowSize, Boolean *m_rgRejected)
01057 {
01058 int n_realSize = n_windowSize;
01059 int i;
01060 double oneSampleQ = 0;
01061 double a00 = 0.0, a01 = 0.0, a10 = 0.0, a11 = 0.0, b0 = 0.0, b1 = 0.0;
01062 double MatrixValue;
01063 Boolean estimateX2 = FALSE;
01064
01065 for (i = 0; i < n_windowSize; i++)
01066 {
01067 if (m_rgRejected[i])
01068 n_realSize--;
01069 }
01070
01071
01072 p_quad->m_X1 = p_quad->m_X2 = 0.0;
01073
01074 for (i = 0; i < n_windowSize; i++)
01075 {
01076 if (!m_rgRejected[i])
01077 oneSampleQ = p_quad->m_rgQp[i];
01078 }
01079 for (i = 0; i < n_windowSize; i++)
01080 {
01081 if ((p_quad->m_rgQp[i] != oneSampleQ) && !m_rgRejected[i])
01082 estimateX2 = TRUE;
01083 if (!m_rgRejected[i])
01084 p_quad->m_X1 += (p_quad->m_rgQp[i] * p_quad->m_rgRp[i]) / n_realSize;
01085 }
01086
01087
01088 if ((n_realSize >= 1) && estimateX2)
01089 {
01090 for (i = 0; i < n_windowSize; i++)
01091 {
01092 if (!m_rgRejected[i])
01093 {
01094 a00 = a00 + 1.0;
01095 a01 += 1.0 / p_quad->m_rgQp[i];
01096 a10 = a01;
01097 a11 += 1.0 / (p_quad->m_rgQp[i] * p_quad->m_rgQp[i]);
01098 b0 += p_quad->m_rgQp[i] * p_quad->m_rgRp[i];
01099 b1 += p_quad->m_rgRp[i];
01100 }
01101 }
01102
01103 MatrixValue=a00*a11-a01*a10;
01104 if(fabs(MatrixValue) > 0.000001)
01105 {
01106 p_quad->m_X1 = (b0 * a11 - b1 * a01) / MatrixValue;
01107 p_quad->m_X2 = (b1 * a00 - b0 * a10) / MatrixValue;
01108 }
01109 else
01110 {
01111 p_quad->m_X1 = b0 / a00;
01112 p_quad->m_X2 = 0.0;
01113 }
01114 }
01115 if( p_Img->type == P_SLICE || (p_Inp->RCUpdateMode == RC_MODE_1 && (p_Img->number != 0)) )
01116 {
01117 p_quad->Pm_X1 = p_quad->m_X1;
01118 p_quad->Pm_X2 = p_quad->m_X2;
01119 }
01120 }
01121
01122
01123
01124
01125
01126
01127
01128
01129 void updateMADModel (ImageParameters *p_Img, InputParameters *p_Inp, RCQuadratic *p_quad, RCGeneric *p_gen)
01130 {
01131 int n_windowSize;
01132 int i;
01133 double std = 0.0, threshold;
01134 int m_Nc = p_quad->NumberofCodedPFrame;
01135 static Boolean PictureRejected[RC_MODEL_HISTORY];
01136 static double error [RC_MODEL_HISTORY];
01137
01138 if(p_quad->NumberofCodedPFrame>0)
01139 {
01140
01141
01142 if(p_Img->BasicUnit == p_Img->FrameSizeInMbs)
01143 m_Nc = p_quad->NumberofCodedPFrame;
01144 else
01145 m_Nc=p_quad->NumberofCodedPFrame*p_quad->TotalNumberofBasicUnit+p_quad->CodedBasicUnit;
01146
01147 for (i = (RC_MODEL_HISTORY-2); i > 0; i--)
01148 {
01149 p_quad->PPictureMAD[i] = p_quad->PPictureMAD[i - 1];
01150 p_quad->PictureMAD[i] = p_quad->PPictureMAD[i];
01151 p_quad->ReferenceMAD[i] = p_quad->ReferenceMAD[i-1];
01152 }
01153 p_quad->PPictureMAD[0] = p_quad->CurrentFrameMAD;
01154 p_quad->PictureMAD[0] = p_quad->PPictureMAD[0];
01155
01156 if(p_Img->BasicUnit == p_Img->FrameSizeInMbs)
01157 p_quad->ReferenceMAD[0]=p_quad->PictureMAD[1];
01158 else
01159 {
01160 if(((p_Inp->PicInterlace==ADAPTIVE_CODING)||(p_Inp->MbInterlace)) &&(p_gen->FieldControl==1))
01161 p_quad->ReferenceMAD[0]=p_quad->FCBUPFMAD[p_quad->TotalNumberofBasicUnit-1-p_quad->NumberofBasicUnit];
01162 else
01163 p_quad->ReferenceMAD[0]=p_quad->BUPFMAD[p_quad->TotalNumberofBasicUnit-1-p_quad->NumberofBasicUnit];
01164 }
01165 p_quad->MADPictureC1 = p_quad->PMADPictureC1;
01166 p_quad->MADPictureC2 = p_quad->PMADPictureC2;
01167
01168
01169 n_windowSize = (p_quad->CurrentFrameMAD > p_quad->PreviousFrameMAD)
01170 ? (int) ((float)(RC_MODEL_HISTORY-1) * p_quad->PreviousFrameMAD / p_quad->CurrentFrameMAD)
01171 : (int) ((float)(RC_MODEL_HISTORY-1) * p_quad->CurrentFrameMAD / p_quad->PreviousFrameMAD);
01172 n_windowSize = iClip3(1, (m_Nc-1), n_windowSize);
01173 n_windowSize=imin(n_windowSize, imin(20, p_quad->MADm_windowSize + 1));
01174
01175
01176 p_quad->MADm_windowSize=n_windowSize;
01177
01178 for (i = 0; i < (RC_MODEL_HISTORY-1); i++)
01179 {
01180 PictureRejected[i] = FALSE;
01181 }
01182
01183
01184 if( p_Img->type == P_SLICE || (p_Inp->RCUpdateMode == RC_MODE_1 && (p_Img->number != 0)) )
01185 p_quad->PreviousFrameMAD=p_quad->CurrentFrameMAD;
01186
01187
01188 MADModelEstimator (p_Img, p_Inp, p_quad, n_windowSize, PictureRejected);
01189
01190
01191 for (i = 0; i < n_windowSize; i++)
01192 {
01193 error[i] = p_quad->MADPictureC1 * p_quad->ReferenceMAD[i] + p_quad->MADPictureC2 - p_quad->PictureMAD[i];
01194 std += (error[i] * error[i]);
01195 }
01196
01197 threshold = (n_windowSize == 2) ? 0 : sqrt (std / n_windowSize);
01198 for (i = 0; i < n_windowSize; i++)
01199 {
01200 if (fabs(error[i]) > threshold)
01201 PictureRejected[i] = TRUE;
01202 }
01203
01204 PictureRejected[0] = FALSE;
01205
01206
01207 MADModelEstimator (p_Img, p_Inp, p_quad, n_windowSize, PictureRejected);
01208 }
01209 }
01210
01211
01212
01213
01214
01215
01216
01217
01218
01219 void MADModelEstimator (ImageParameters *p_Img, InputParameters *p_Inp, RCQuadratic *p_quad, int n_windowSize, Boolean *PictureRejected)
01220 {
01221 int n_realSize = n_windowSize;
01222 int i;
01223 double oneSampleQ = 0.0;
01224 double a00 = 0.0, a01 = 0.0, a10 = 0.0, a11 = 0.0, b0 = 0.0, b1 = 0.0;
01225 double MatrixValue;
01226 Boolean estimateX2 = FALSE;
01227
01228 for (i = 0; i < n_windowSize; i++)
01229 {
01230 if (PictureRejected[i])
01231 n_realSize--;
01232 }
01233
01234
01235 p_quad->MADPictureC1 = p_quad->MADPictureC2 = 0.0;
01236
01237 for (i = 0; i < n_windowSize; i++)
01238 {
01239 if (!PictureRejected[i])
01240 oneSampleQ = p_quad->PictureMAD[i];
01241 }
01242
01243 for (i = 0; i < n_windowSize; i++)
01244 {
01245 if ((p_quad->PictureMAD[i] != oneSampleQ) && !PictureRejected[i])
01246 estimateX2 = TRUE;
01247 if (!PictureRejected[i])
01248 p_quad->MADPictureC1 += p_quad->PictureMAD[i] / (p_quad->ReferenceMAD[i]*n_realSize);
01249 }
01250
01251
01252 if ((n_realSize >= 1) && estimateX2)
01253 {
01254 for (i = 0; i < n_windowSize; i++)
01255 {
01256 if (!PictureRejected[i])
01257 {
01258 a00 = a00 + 1.0;
01259 a01 += p_quad->ReferenceMAD[i];
01260 a10 = a01;
01261 a11 += p_quad->ReferenceMAD[i] * p_quad->ReferenceMAD[i];
01262 b0 += p_quad->PictureMAD[i];
01263 b1 += p_quad->PictureMAD[i] * p_quad->ReferenceMAD[i];
01264 }
01265 }
01266
01267 MatrixValue = a00 * a11 - a01 * a10;
01268 if(fabs(MatrixValue) > 0.000001)
01269 {
01270 p_quad->MADPictureC2 = (b0 * a11 - b1 * a01) / MatrixValue;
01271 p_quad->MADPictureC1 = (b1 * a00 - b0 * a10) / MatrixValue;
01272 }
01273 else
01274 {
01275 p_quad->MADPictureC1 = b0/a01;
01276 p_quad->MADPictureC2 = 0.0;
01277 }
01278 }
01279 if( p_Img->type == P_SLICE || (p_Inp->RCUpdateMode == RC_MODE_1 && (p_Img->number != 0)) )
01280 {
01281 p_quad->PMADPictureC1 = p_quad->MADPictureC1;
01282 p_quad->PMADPictureC2 = p_quad->MADPictureC2;
01283 }
01284 }
01285
01286
01287
01288
01289
01290
01291
01292
01293 int updateQPRC0(ImageParameters *p_Img, InputParameters *p_Inp, RCQuadratic *p_quad, RCGeneric *p_gen, int topfield)
01294 {
01295 int m_Bits;
01296 int BFrameNumber;
01297 int StepSize;
01298 int SumofBasicUnit;
01299 int MaxQpChange, m_Qp, m_Hp;
01300
01301
01302 if( p_Img->BasicUnit == p_Img->FrameSizeInMbs )
01303 {
01304
01305
01306
01307
01308 if((topfield) || (p_gen->FieldControl==0))
01309 {
01310 if (p_Img->type==I_SLICE)
01311 {
01312 p_quad->m_Qc = p_quad->MyInitialQp;
01313 return p_quad->m_Qc;
01314 }
01315 else if(p_Img->type == B_SLICE)
01316 {
01317 if(p_Inp->NumberBFrames==1)
01318 {
01319 if((p_Inp->PicInterlace==ADAPTIVE_CODING)||(p_Inp->MbInterlace))
01320 updateQPInterlace( p_quad, p_gen );
01321
01322 p_quad->m_Qc = imin(p_quad->PrevLastQP, p_quad->CurrLastQP) + 2;
01323 p_quad->m_Qc = imax(p_quad->m_Qc, imax(p_quad->PrevLastQP, p_quad->CurrLastQP));
01324 p_quad->m_Qc = imax(p_quad->m_Qc, p_quad->CurrLastQP + 1);
01325 p_quad->m_Qc = iClip3(p_Img->RCMinQP + p_quad->bitdepth_qp_scale, p_Img->RCMaxQP + p_quad->bitdepth_qp_scale, p_quad->m_Qc);
01326 }
01327 else
01328 {
01329 BFrameNumber = (p_quad->NumberofBFrames + 1) % p_Inp->NumberBFrames;
01330 if(BFrameNumber==0)
01331 BFrameNumber = p_Inp->NumberBFrames;
01332
01333
01334 if(BFrameNumber==1)
01335 {
01336 if((p_Inp->PicInterlace==ADAPTIVE_CODING)||(p_Inp->MbInterlace))
01337 updateQPInterlace( p_quad, p_gen );
01338 }
01339
01340 if((p_quad->CurrLastQP-p_quad->PrevLastQP)<=(-2*p_Inp->NumberBFrames-3))
01341 StepSize=-3;
01342 else if((p_quad->CurrLastQP-p_quad->PrevLastQP)==(-2*p_Inp->NumberBFrames-2))
01343 StepSize=-2;
01344 else if((p_quad->CurrLastQP-p_quad->PrevLastQP)==(-2*p_Inp->NumberBFrames-1))
01345 StepSize=-1;
01346 else if((p_quad->CurrLastQP-p_quad->PrevLastQP)==(-2*p_Inp->NumberBFrames))
01347 StepSize=0;
01348 else if((p_quad->CurrLastQP-p_quad->PrevLastQP)==(-2*p_Inp->NumberBFrames+1))
01349 StepSize=1;
01350 else
01351 StepSize=2;
01352
01353 p_quad->m_Qc = p_quad->PrevLastQP + StepSize;
01354 p_quad->m_Qc += iClip3( -2 * (BFrameNumber - 1), 2*(BFrameNumber-1),
01355 (BFrameNumber-1)*(p_quad->CurrLastQP-p_quad->PrevLastQP)/(p_Inp->NumberBFrames-1));
01356 p_quad->m_Qc = iClip3(p_Img->RCMinQP + p_quad->bitdepth_qp_scale, p_Img->RCMaxQP + p_quad->bitdepth_qp_scale, p_quad->m_Qc);
01357 }
01358 return p_quad->m_Qc;
01359 }
01360 else if( p_Img->type == P_SLICE && p_quad->NumberofPPicture == 0 )
01361 {
01362 p_quad->m_Qc=p_quad->MyInitialQp;
01363
01364 if(p_gen->FieldControl==0)
01365 updateQPNonPicAFF( p_Img->active_sps, p_quad );
01366 return p_quad->m_Qc;
01367 }
01368 else
01369 {
01370
01371 if( ( p_Inp->PicInterlace == ADAPTIVE_CODING || p_Inp->MbInterlace ) && p_gen->FieldControl == 0 )
01372 updateQPInterlaceBU( p_quad, p_gen );
01373
01374 p_quad->m_X1 = p_quad->Pm_X1;
01375 p_quad->m_X2 = p_quad->Pm_X2;
01376 p_quad->MADPictureC1 = p_quad->PMADPictureC1;
01377 p_quad->MADPictureC2 = p_quad->PMADPictureC2;
01378 p_quad->PreviousPictureMAD = p_quad->PPictureMAD[0];
01379
01380 MaxQpChange = p_quad->PMaxQpChange;
01381 m_Qp = p_quad->Pm_Qp;
01382 m_Hp = p_quad->PPreHeader;
01383
01384
01385 p_quad->CurrentFrameMAD = p_quad->MADPictureC1*p_quad->PreviousPictureMAD + p_quad->MADPictureC2;
01386
01387
01388 if(p_quad->Target < 0)
01389 {
01390 p_quad->m_Qc=m_Qp+MaxQpChange;
01391 p_quad->m_Qc = iClip3(p_Img->RCMinQP + p_quad->bitdepth_qp_scale, p_Img->RCMaxQP + p_quad->bitdepth_qp_scale, p_quad->m_Qc);
01392 }
01393 else
01394 {
01395 m_Bits = p_quad->Target-m_Hp;
01396 m_Bits = imax(m_Bits, (int)(p_quad->bit_rate/(MINVALUE*p_quad->frame_rate)));
01397
01398 updateModelQPFrame( p_quad, m_Bits );
01399
01400 p_quad->m_Qc = iClip3(p_Img->RCMinQP + p_quad->bitdepth_qp_scale, p_Img->RCMaxQP + p_quad->bitdepth_qp_scale, p_quad->m_Qc);
01401 p_quad->m_Qc = iClip3(m_Qp-MaxQpChange, m_Qp+MaxQpChange, p_quad->m_Qc);
01402 }
01403
01404 if( p_gen->FieldControl == 0 )
01405 updateQPNonPicAFF( p_Img->active_sps, p_quad );
01406
01407 return p_quad->m_Qc;
01408 }
01409 }
01410
01411 else
01412 {
01413 if( p_Img->type == P_SLICE && p_gen->NoGranularFieldRC == 0 )
01414 updateBottomField( p_Inp, p_quad );
01415 return p_quad->m_Qc;
01416 }
01417 }
01418
01419 else
01420 {
01421
01422 if (p_Img->type == I_SLICE)
01423 {
01424 p_quad->m_Qc = p_quad->MyInitialQp;
01425 return p_quad->m_Qc;
01426 }
01427 else if( p_Img->type == B_SLICE )
01428 {
01429
01430 if((topfield)||(p_gen->FieldControl==0))
01431 {
01432 if(p_Inp->NumberBFrames==1)
01433 {
01434
01435 if((p_Inp->PicInterlace==ADAPTIVE_CODING)||(p_Inp->MbInterlace))
01436 updateQPInterlace( p_quad, p_gen );
01437
01438 if(p_quad->PrevLastQP==p_quad->CurrLastQP)
01439 p_quad->m_Qc=p_quad->PrevLastQP+2;
01440 else
01441 p_quad->m_Qc = ((p_quad->PrevLastQP+p_quad->CurrLastQP) >> 1) + 1;
01442 p_quad->m_Qc = iClip3(p_Img->RCMinQP + p_quad->bitdepth_qp_scale, p_Img->RCMaxQP + p_quad->bitdepth_qp_scale, p_quad->m_Qc);
01443 }
01444 else
01445 {
01446 BFrameNumber=(p_quad->NumberofBFrames+1)%p_Inp->NumberBFrames;
01447 if(BFrameNumber==0)
01448 BFrameNumber=p_Inp->NumberBFrames;
01449
01450
01451 if(BFrameNumber==1)
01452 {
01453 if((p_Inp->PicInterlace==ADAPTIVE_CODING)||(p_Inp->MbInterlace))
01454 updateQPInterlace( p_quad, p_gen );
01455 }
01456
01457 if((p_quad->CurrLastQP-p_quad->PrevLastQP)<=(-2*p_Inp->NumberBFrames-3))
01458 StepSize=-3;
01459 else if((p_quad->CurrLastQP-p_quad->PrevLastQP)==(-2*p_Inp->NumberBFrames-2))
01460 StepSize=-2;
01461 else if((p_quad->CurrLastQP-p_quad->PrevLastQP)==(-2*p_Inp->NumberBFrames-1))
01462 StepSize=-1;
01463 else if((p_quad->CurrLastQP-p_quad->PrevLastQP)==(-2*p_Inp->NumberBFrames))
01464 StepSize=0;
01465 else if((p_quad->CurrLastQP-p_quad->PrevLastQP)==(-2*p_Inp->NumberBFrames+1))
01466 StepSize=1;
01467 else
01468 StepSize=2;
01469 p_quad->m_Qc=p_quad->PrevLastQP+StepSize;
01470 p_quad->m_Qc +=
01471 iClip3( -2*(BFrameNumber-1), 2*(BFrameNumber-1), (BFrameNumber-1)*(p_quad->CurrLastQP-p_quad->PrevLastQP)/(p_Inp->NumberBFrames-1) );
01472 p_quad->m_Qc = iClip3(p_Img->RCMinQP + p_quad->bitdepth_qp_scale, p_Img->RCMaxQP + p_quad->bitdepth_qp_scale, p_quad->m_Qc);
01473 }
01474 return p_quad->m_Qc;
01475 }
01476
01477 else
01478 {
01479 return p_quad->m_Qc;
01480 }
01481 }
01482 else if( p_Img->type == P_SLICE )
01483 {
01484 if( (p_gen->NumberofGOP == 1) && (p_quad->NumberofPPicture == 0) )
01485 {
01486 if((p_gen->FieldControl==0)||((p_gen->FieldControl==1) && (p_gen->NoGranularFieldRC==0)))
01487 return updateFirstP( p_Img, p_Inp, p_quad, p_gen, topfield );
01488 }
01489 else
01490 {
01491 p_quad->m_X1=p_quad->Pm_X1;
01492 p_quad->m_X2=p_quad->Pm_X2;
01493 p_quad->MADPictureC1=p_quad->PMADPictureC1;
01494 p_quad->MADPictureC2=p_quad->PMADPictureC2;
01495
01496 m_Qp=p_quad->Pm_Qp;
01497
01498 if(p_gen->FieldControl==0)
01499 SumofBasicUnit=p_quad->TotalNumberofBasicUnit;
01500 else
01501 SumofBasicUnit=p_quad->TotalNumberofBasicUnit>>1;
01502
01503
01504 if(p_quad->NumberofBasicUnit==SumofBasicUnit)
01505 return updateFirstBU( p_Img, p_Inp, p_quad, p_gen, topfield );
01506 else
01507 {
01508
01509 p_quad->Target -= (p_gen->NumberofBasicUnitHeaderBits + p_gen->NumberofBasicUnitTextureBits);
01510 p_gen->NumberofBasicUnitHeaderBits = 0;
01511 p_gen->NumberofBasicUnitTextureBits = 0;
01512 if(p_quad->Target<0)
01513 return updateNegativeTarget( p_Img, p_Inp, p_quad, p_gen, topfield, m_Qp );
01514 else
01515 {
01516
01517 predictCurrPicMAD( p_Inp, p_quad, p_gen );
01518
01519
01520 updateModelQPBU( p_Img, p_Inp, p_quad, m_Qp );
01521
01522 p_quad->TotalFrameQP +=p_quad->m_Qc;
01523 p_quad->Pm_Qp=p_quad->m_Qc;
01524 p_quad->NumberofBasicUnit--;
01525 if( p_quad->NumberofBasicUnit == 0 && p_Img->type == P_SLICE )
01526 updateLastBU( p_Img, p_Inp, p_quad, p_gen, topfield );
01527
01528 return p_quad->m_Qc;
01529 }
01530 }
01531 }
01532 }
01533 }
01534 return p_quad->m_Qc;
01535 }
01536
01537
01538
01539
01540
01541
01542
01543
01544 int updateQPRC1(ImageParameters *p_Img, InputParameters *p_Inp, RCQuadratic *p_quad, RCGeneric *p_gen, int topfield)
01545 {
01546 int m_Bits;
01547 int SumofBasicUnit;
01548 int MaxQpChange, m_Qp, m_Hp;
01549
01550
01551 if( p_Img->BasicUnit == p_Img->FrameSizeInMbs )
01552 {
01553
01554
01555
01556
01557 if((topfield) || (p_gen->FieldControl==0))
01558 {
01559 if (p_Img->number == 0)
01560 {
01561 p_quad->m_Qc = p_quad->MyInitialQp;
01562 return p_quad->m_Qc;
01563 }
01564 else if( p_quad->NumberofPPicture == 0 && (p_Img->number != 0))
01565 {
01566 p_quad->m_Qc=p_quad->MyInitialQp;
01567
01568 if(p_gen->FieldControl==0)
01569 updateQPNonPicAFF( p_Img->active_sps, p_quad );
01570 return p_quad->m_Qc;
01571 }
01572 else
01573 {
01574
01575 if( ( p_Inp->PicInterlace == ADAPTIVE_CODING || p_Inp->MbInterlace ) && p_gen->FieldControl == 0 )
01576 updateQPInterlaceBU( p_quad, p_gen );
01577
01578 p_quad->m_X1 = p_quad->Pm_X1;
01579 p_quad->m_X2 = p_quad->Pm_X2;
01580 p_quad->MADPictureC1 = p_quad->PMADPictureC1;
01581 p_quad->MADPictureC2 = p_quad->PMADPictureC2;
01582 p_quad->PreviousPictureMAD = p_quad->PPictureMAD[0];
01583
01584 MaxQpChange = p_quad->PMaxQpChange;
01585 m_Qp = p_quad->Pm_Qp;
01586 m_Hp = p_quad->PPreHeader;
01587
01588
01589 p_quad->CurrentFrameMAD=p_quad->MADPictureC1*p_quad->PreviousPictureMAD + p_quad->MADPictureC2;
01590
01591
01592 if(p_quad->Target < 0)
01593 {
01594 p_quad->m_Qc=m_Qp+MaxQpChange;
01595 p_quad->m_Qc = iClip3(p_Img->RCMinQP + p_quad->bitdepth_qp_scale, p_Img->RCMaxQP + p_quad->bitdepth_qp_scale, p_quad->m_Qc);
01596 }
01597 else
01598 {
01599 m_Bits = p_quad->Target-m_Hp;
01600 m_Bits = imax(m_Bits, (int)(p_quad->bit_rate/(MINVALUE*p_quad->frame_rate)));
01601
01602 updateModelQPFrame( p_quad, m_Bits );
01603
01604 p_quad->m_Qc = iClip3(p_Img->RCMinQP + p_quad->bitdepth_qp_scale, p_Img->RCMaxQP + p_quad->bitdepth_qp_scale, p_quad->m_Qc);
01605 p_quad->m_Qc = iClip3(m_Qp-MaxQpChange, m_Qp+MaxQpChange, p_quad->m_Qc);
01606 }
01607
01608 if( p_gen->FieldControl == 0 )
01609 updateQPNonPicAFF( p_Img->active_sps, p_quad );
01610
01611 return p_quad->m_Qc;
01612 }
01613 }
01614
01615 else
01616 {
01617 if( p_gen->NoGranularFieldRC == 0 )
01618 updateBottomField( p_Inp, p_quad );
01619 return p_quad->m_Qc;
01620 }
01621 }
01622
01623 else
01624 {
01625
01626 if (p_Img->number == 0)
01627 {
01628 p_quad->m_Qc = p_quad->MyInitialQp;
01629 return p_quad->m_Qc;
01630 }
01631 else
01632 {
01633 if((p_gen->NumberofGOP==1)&&(p_quad->NumberofPPicture==0))
01634 {
01635 if((p_gen->FieldControl==0)||((p_gen->FieldControl==1) && (p_gen->NoGranularFieldRC==0)))
01636 return updateFirstP( p_Img, p_Inp, p_quad, p_gen, topfield );
01637 }
01638 else
01639 {
01640 p_quad->m_X1=p_quad->Pm_X1;
01641 p_quad->m_X2=p_quad->Pm_X2;
01642 p_quad->MADPictureC1=p_quad->PMADPictureC1;
01643 p_quad->MADPictureC2=p_quad->PMADPictureC2;
01644
01645 m_Qp=p_quad->Pm_Qp;
01646
01647 if(p_gen->FieldControl==0)
01648 SumofBasicUnit=p_quad->TotalNumberofBasicUnit;
01649 else
01650 SumofBasicUnit=p_quad->TotalNumberofBasicUnit>>1;
01651
01652
01653 if(p_quad->NumberofBasicUnit==SumofBasicUnit)
01654 return updateFirstBU( p_Img, p_Inp, p_quad, p_gen, topfield );
01655 else
01656 {
01657
01658 p_quad->Target -= (p_gen->NumberofBasicUnitHeaderBits + p_gen->NumberofBasicUnitTextureBits);
01659 p_gen->NumberofBasicUnitHeaderBits = 0;
01660 p_gen->NumberofBasicUnitTextureBits = 0;
01661 if(p_quad->Target<0)
01662 return updateNegativeTarget( p_Img, p_Inp, p_quad, p_gen, topfield, m_Qp );
01663 else
01664 {
01665
01666 predictCurrPicMAD( p_Inp, p_quad, p_gen );
01667
01668
01669 updateModelQPBU( p_Img, p_Inp, p_quad, m_Qp );
01670
01671 p_quad->TotalFrameQP +=p_quad->m_Qc;
01672 p_quad->Pm_Qp=p_quad->m_Qc;
01673 p_quad->NumberofBasicUnit--;
01674 if((p_quad->NumberofBasicUnit==0) && (p_Img->number != 0))
01675 updateLastBU( p_Img, p_Inp, p_quad, p_gen, topfield );
01676
01677 return p_quad->m_Qc;
01678 }
01679 }
01680 }
01681 }
01682 }
01683 return p_quad->m_Qc;
01684 }
01685
01686
01687
01688
01689
01690
01691
01692
01693 int updateQPRC2(ImageParameters *p_Img, InputParameters *p_Inp, RCQuadratic *p_quad, RCGeneric *p_gen, int topfield)
01694 {
01695 int m_Bits;
01696 int SumofBasicUnit;
01697 int MaxQpChange, m_Qp, m_Hp;
01698
01699
01700 if( p_Img->BasicUnit == p_Img->FrameSizeInMbs )
01701 {
01702
01703
01704
01705
01706 if((topfield) || (p_gen->FieldControl==0))
01707 {
01708 if (p_Img->number == 0)
01709 {
01710 p_quad->m_Qc = p_quad->MyInitialQp;
01711 return p_quad->m_Qc;
01712 }
01713 else if (p_Img->type==I_SLICE)
01714 {
01715 if((p_Inp->PicInterlace==ADAPTIVE_CODING)||(p_Inp->MbInterlace))
01716 updateQPInterlace( p_quad, p_gen );
01717
01718 p_quad->m_Qc = p_quad->CurrLastQP;
01719 return p_quad->m_Qc;
01720 }
01721 else if(p_Img->type == B_SLICE)
01722 {
01723 int prevQP = imax(p_quad->PrevLastQP, p_quad->CurrLastQP);
01724
01725
01726 if((p_Inp->PicInterlace==ADAPTIVE_CODING)||(p_Inp->MbInterlace))
01727 updateQPInterlace( p_quad, p_gen );
01728
01729 if (p_Inp->HierarchicalCoding)
01730 {
01731 if (p_Img->b_frame_to_code == 0)
01732 p_quad->m_Qc = prevQP;
01733 else
01734 p_quad->m_Qc = prevQP + p_Img->GopLevels - p_Img->gop_structure[p_Img->b_frame_to_code-1].hierarchy_layer;
01735 }
01736 else
01737 p_quad->m_Qc = prevQP + 2 - p_Img->nal_reference_idc;
01738 p_quad->m_Qc = iClip3(p_Img->RCMinQP + p_quad->bitdepth_qp_scale, p_Img->RCMaxQP + p_quad->bitdepth_qp_scale, p_quad->m_Qc);
01739
01740 return p_quad->m_Qc;
01741 }
01742 else if( p_Img->type == P_SLICE && p_quad->NumberofPPicture == 0 )
01743 {
01744 p_quad->m_Qc=p_quad->MyInitialQp;
01745
01746 if(p_gen->FieldControl==0)
01747 updateQPNonPicAFF( p_Img->active_sps, p_quad );
01748 return p_quad->m_Qc;
01749 }
01750 else
01751 {
01752
01753 if( ( p_Inp->PicInterlace == ADAPTIVE_CODING || p_Inp->MbInterlace ) && p_gen->FieldControl == 0 )
01754 updateQPInterlaceBU( p_quad, p_gen );
01755
01756 p_quad->m_X1 = p_quad->Pm_X1;
01757 p_quad->m_X2 = p_quad->Pm_X2;
01758 p_quad->MADPictureC1 = p_quad->PMADPictureC1;
01759 p_quad->MADPictureC2 = p_quad->PMADPictureC2;
01760 p_quad->PreviousPictureMAD = p_quad->PPictureMAD[0];
01761
01762 MaxQpChange = p_quad->PMaxQpChange;
01763 m_Qp = p_quad->Pm_Qp;
01764 m_Hp = p_quad->PPreHeader;
01765
01766
01767 p_quad->CurrentFrameMAD=p_quad->MADPictureC1*p_quad->PreviousPictureMAD + p_quad->MADPictureC2;
01768
01769
01770 if(p_quad->Target < 0)
01771 {
01772 p_quad->m_Qc=m_Qp+MaxQpChange;
01773 p_quad->m_Qc = iClip3(p_Img->RCMinQP + p_quad->bitdepth_qp_scale, p_Img->RCMaxQP + p_quad->bitdepth_qp_scale, p_quad->m_Qc);
01774 }
01775 else
01776 {
01777 m_Bits = p_quad->Target-m_Hp;
01778 m_Bits = imax(m_Bits, (int)(p_quad->bit_rate/(MINVALUE*p_quad->frame_rate)));
01779
01780 updateModelQPFrame( p_quad, m_Bits );
01781
01782 p_quad->m_Qc = iClip3(p_Img->RCMinQP + p_quad->bitdepth_qp_scale, p_Img->RCMaxQP + p_quad->bitdepth_qp_scale, p_quad->m_Qc);
01783 p_quad->m_Qc = iClip3(m_Qp-MaxQpChange, m_Qp+MaxQpChange, p_quad->m_Qc);
01784 }
01785
01786 if( p_gen->FieldControl == 0 )
01787 updateQPNonPicAFF( p_Img->active_sps, p_quad );
01788
01789 return p_quad->m_Qc;
01790 }
01791 }
01792
01793 else
01794 {
01795 if( p_Img->type==P_SLICE && p_gen->NoGranularFieldRC == 0 )
01796 updateBottomField( p_Inp, p_quad );
01797 return p_quad->m_Qc;
01798 }
01799 }
01800
01801 else
01802 {
01803
01804 if (p_Img->number == 0)
01805 {
01806 p_quad->m_Qc = p_quad->MyInitialQp;
01807 return p_quad->m_Qc;
01808 }
01809 else if (p_Img->type==I_SLICE)
01810 {
01811
01812 if((p_Inp->PicInterlace==ADAPTIVE_CODING)||(p_Inp->MbInterlace))
01813 updateQPInterlace( p_quad, p_gen );
01814
01815 p_quad->m_Qc = p_quad->PrevLastQP;
01816 p_quad->PrevLastQP = p_quad->CurrLastQP;
01817 p_quad->CurrLastQP = p_quad->PrevLastQP;
01818 p_quad->PAveFrameQP = p_quad->CurrLastQP;
01819
01820 return p_quad->m_Qc;
01821 }
01822 else if(p_Img->type == B_SLICE)
01823 {
01824 int prevQP = imax(p_quad->PrevLastQP, p_quad->CurrLastQP);
01825 if((p_Inp->PicInterlace==ADAPTIVE_CODING)||(p_Inp->MbInterlace))
01826 updateQPInterlace( p_quad, p_gen );
01827
01828 if (p_Inp->HierarchicalCoding)
01829 {
01830
01831 if (p_Img->b_frame_to_code == 0)
01832 p_quad->m_Qc = prevQP;
01833 else
01834 p_quad->m_Qc = prevQP + p_Img->GopLevels - p_Img->gop_structure[p_Img->b_frame_to_code-1].hierarchy_layer;
01835 }
01836 else
01837 p_quad->m_Qc = prevQP + 2 - p_Img->nal_reference_idc;
01838 p_quad->m_Qc = iClip3(p_Img->RCMinQP + p_quad->bitdepth_qp_scale, p_Img->RCMaxQP + p_quad->bitdepth_qp_scale, p_quad->m_Qc);
01839
01840 return p_quad->m_Qc;
01841
01842 }
01843 else if( p_Img->type == P_SLICE )
01844 {
01845 if((p_gen->NumberofGOP==1)&&(p_quad->NumberofPPicture==0))
01846 {
01847 if((p_gen->FieldControl==0)||((p_gen->FieldControl==1) && (p_gen->NoGranularFieldRC==0)))
01848 return updateFirstP( p_Img, p_Inp, p_quad, p_gen, topfield );
01849 }
01850 else
01851 {
01852 p_quad->m_X1=p_quad->Pm_X1;
01853 p_quad->m_X2=p_quad->Pm_X2;
01854 p_quad->MADPictureC1=p_quad->PMADPictureC1;
01855 p_quad->MADPictureC2=p_quad->PMADPictureC2;
01856
01857 m_Qp=p_quad->Pm_Qp;
01858
01859 if(p_gen->FieldControl==0)
01860 SumofBasicUnit=p_quad->TotalNumberofBasicUnit;
01861 else
01862 SumofBasicUnit=p_quad->TotalNumberofBasicUnit>>1;
01863
01864
01865 if(p_quad->NumberofBasicUnit==SumofBasicUnit)
01866 return updateFirstBU( p_Img, p_Inp, p_quad, p_gen, topfield );
01867 else
01868 {
01869
01870 p_quad->Target -= (p_gen->NumberofBasicUnitHeaderBits + p_gen->NumberofBasicUnitTextureBits);
01871 p_gen->NumberofBasicUnitHeaderBits = 0;
01872 p_gen->NumberofBasicUnitTextureBits = 0;
01873 if(p_quad->Target<0)
01874 return updateNegativeTarget( p_Img, p_Inp, p_quad, p_gen, topfield, m_Qp );
01875 else
01876 {
01877
01878 predictCurrPicMAD( p_Inp, p_quad, p_gen );
01879
01880
01881 updateModelQPBU( p_Img, p_Inp, p_quad, m_Qp );
01882
01883 p_quad->TotalFrameQP +=p_quad->m_Qc;
01884 p_quad->Pm_Qp=p_quad->m_Qc;
01885 p_quad->NumberofBasicUnit--;
01886 if((p_quad->NumberofBasicUnit==0) && p_Img->type == P_SLICE )
01887 updateLastBU( p_Img, p_Inp, p_quad, p_gen, topfield );
01888
01889 return p_quad->m_Qc;
01890 }
01891 }
01892 }
01893 }
01894 }
01895 return p_quad->m_Qc;
01896 }
01897
01898
01899
01900
01901
01902
01903
01904
01905 int updateQPRC3(ImageParameters *p_Img, InputParameters *p_Inp, RCQuadratic *p_quad, RCGeneric *p_gen, int topfield)
01906 {
01907 int m_Bits;
01908 int SumofBasicUnit;
01909 int MaxQpChange, m_Qp, m_Hp;
01910
01911
01912 if( p_Img->BasicUnit == p_Img->FrameSizeInMbs || p_Img->type != P_SLICE )
01913 {
01914
01915
01916
01917
01918 if((topfield) || (p_gen->FieldControl==0))
01919 {
01920 if (p_Img->number == 0)
01921 {
01922 if((p_Inp->PicInterlace == ADAPTIVE_CODING) || (p_Inp->MbInterlace))
01923 updateQPInterlace( p_quad, p_gen );
01924 p_quad->m_Qc = p_quad->MyInitialQp;
01925 return p_quad->m_Qc;
01926 }
01927 else if( p_Img->type == P_SLICE && p_quad->NumberofPPicture == 0 )
01928 {
01929 p_quad->m_Qc=p_quad->MyInitialQp;
01930
01931 if(p_gen->FieldControl==0)
01932 updateQPNonPicAFF( p_Img->active_sps, p_quad );
01933 return p_quad->m_Qc;
01934 }
01935 else
01936 {
01937 if( ( (p_Img->type == B_SLICE && p_Img->b_frame_to_code == 1) || p_Img->type == I_SLICE) && ((p_Inp->PicInterlace == ADAPTIVE_CODING) || (p_Inp->MbInterlace)) )
01938 updateQPInterlace( p_quad, p_gen );
01939
01940 if( p_Img->type == P_SLICE && ( p_Inp->PicInterlace == ADAPTIVE_CODING || p_Inp->MbInterlace ) && p_gen->FieldControl == 0 )
01941 updateQPInterlaceBU( p_quad, p_gen );
01942
01943 p_quad->m_X1 = p_quad->Pm_X1;
01944 p_quad->m_X2 = p_quad->Pm_X2;
01945 p_quad->MADPictureC1 = p_quad->PMADPictureC1;
01946 p_quad->MADPictureC2 = p_quad->PMADPictureC2;
01947 p_quad->PreviousPictureMAD = p_quad->PPictureMAD[0];
01948
01949 MaxQpChange = p_quad->PMaxQpChange;
01950 m_Qp = p_quad->Pm_Qp;
01951 m_Hp = p_quad->PPreHeader;
01952
01953 if ( p_Img->BasicUnit < p_Img->FrameSizeInMbs && p_Img->type != P_SLICE )
01954 {
01955
01956
01957
01958 p_quad->PreviousPictureMAD = p_quad->PreviousWholeFrameMAD;
01959 }
01960 if ( p_Img->type == I_SLICE )
01961 m_Hp = 0;
01962
01963
01964 p_quad->CurrentFrameMAD=p_quad->MADPictureC1*p_quad->PreviousPictureMAD + p_quad->MADPictureC2;
01965
01966
01967 if(p_quad->Target < 0)
01968 {
01969 p_quad->m_Qc=m_Qp+MaxQpChange;
01970 p_quad->m_Qc = iClip3(p_Img->RCMinQP + p_quad->bitdepth_qp_scale, p_Img->RCMaxQP + p_quad->bitdepth_qp_scale, p_quad->m_Qc);
01971 }
01972 else
01973 {
01974 if ( p_Img->type != P_SLICE )
01975 {
01976 if ( p_Img->BasicUnit < p_Img->FrameSizeInMbs )
01977 m_Bits =(p_quad->Target-m_Hp)/p_quad->TotalNumberofBasicUnit;
01978 else
01979 m_Bits =p_quad->Target-m_Hp;
01980 }
01981 else {
01982 m_Bits = p_quad->Target-m_Hp;
01983 m_Bits = imax(m_Bits, (int)(p_quad->bit_rate/(MINVALUE*p_quad->frame_rate)));
01984 }
01985 updateModelQPFrame( p_quad, m_Bits );
01986
01987 p_quad->m_Qc = iClip3(p_Img->RCMinQP + p_quad->bitdepth_qp_scale, p_Img->RCMaxQP + p_quad->bitdepth_qp_scale, p_quad->m_Qc);
01988 if ( p_Img->type == P_SLICE )
01989 p_quad->m_Qc = iClip3(m_Qp-MaxQpChange, m_Qp+MaxQpChange, p_quad->m_Qc);
01990 }
01991
01992 if( p_Img->type == P_SLICE && p_gen->FieldControl == 0 )
01993 updateQPNonPicAFF( p_Img->active_sps, p_quad );
01994
01995 if ( p_Img->type == B_SLICE )
01996 {
01997
01998 int prevqp = ((p_quad->PrevLastQP+p_quad->CurrLastQP) >> 1) + 1;
01999 if ( p_Inp->HierarchicalCoding && p_Img->b_frame_to_code)
02000 p_quad->m_Qc -= p_Img->gop_structure[p_Img->b_frame_to_code-1].hierarchy_layer;
02001
02002 p_quad->m_Qc = iClip3(prevqp - (p_Inp->HierarchicalCoding ? 0 : 5), prevqp + 5, p_quad->m_Qc);
02003 p_quad->m_Qc = iClip3(p_Img->RCMinQP + p_quad->bitdepth_qp_scale, p_Img->RCMaxQP + p_quad->bitdepth_qp_scale, p_quad->m_Qc);
02004 }
02005 return p_quad->m_Qc;
02006 }
02007 }
02008
02009 else
02010 {
02011 if( p_Img->type==P_SLICE && p_gen->NoGranularFieldRC == 0 )
02012 updateBottomField( p_Inp, p_quad );
02013 return p_quad->m_Qc;
02014 }
02015 }
02016
02017 else
02018 {
02019
02020 if (p_Img->number == 0)
02021 {
02022 if((p_Inp->PicInterlace == ADAPTIVE_CODING) || (p_Inp->MbInterlace))
02023 updateQPInterlace( p_quad, p_gen );
02024 p_quad->m_Qc = p_quad->MyInitialQp;
02025 return p_quad->m_Qc;
02026 }
02027 else if( p_Img->type == P_SLICE )
02028 {
02029 if((p_gen->NumberofGOP==1)&&(p_quad->NumberofPPicture==0))
02030 {
02031 if((p_gen->FieldControl==0)||((p_gen->FieldControl==1) && (p_gen->NoGranularFieldRC==0)))
02032 return updateFirstP( p_Img, p_Inp, p_quad, p_gen, topfield );
02033 }
02034 else
02035 {
02036 if( ( (p_Img->type == B_SLICE && p_Img->b_frame_to_code == 1) || p_Img->type == I_SLICE) && ((p_Inp->PicInterlace == ADAPTIVE_CODING) || (p_Inp->MbInterlace)) )
02037 updateQPInterlace( p_quad, p_gen );
02038 p_quad->m_X1=p_quad->Pm_X1;
02039 p_quad->m_X2=p_quad->Pm_X2;
02040 p_quad->MADPictureC1=p_quad->PMADPictureC1;
02041 p_quad->MADPictureC2=p_quad->PMADPictureC2;
02042
02043 m_Qp=p_quad->Pm_Qp;
02044
02045 if(p_gen->FieldControl==0)
02046 SumofBasicUnit=p_quad->TotalNumberofBasicUnit;
02047 else
02048 SumofBasicUnit=p_quad->TotalNumberofBasicUnit>>1;
02049
02050
02051 if(p_quad->NumberofBasicUnit==SumofBasicUnit)
02052 return updateFirstBU( p_Img, p_Inp, p_quad, p_gen, topfield );
02053 else
02054 {
02055
02056 p_quad->Target -= (p_gen->NumberofBasicUnitHeaderBits + p_gen->NumberofBasicUnitTextureBits);
02057 p_gen->NumberofBasicUnitHeaderBits = 0;
02058 p_gen->NumberofBasicUnitTextureBits = 0;
02059 if(p_quad->Target<0)
02060 return updateNegativeTarget( p_Img, p_Inp, p_quad, p_gen, topfield, m_Qp );
02061 else
02062 {
02063
02064 predictCurrPicMAD( p_Inp, p_quad, p_gen );
02065
02066
02067 updateModelQPBU( p_Img, p_Inp, p_quad, m_Qp );
02068
02069 p_quad->TotalFrameQP +=p_quad->m_Qc;
02070 p_quad->Pm_Qp=p_quad->m_Qc;
02071 p_quad->NumberofBasicUnit--;
02072 if((p_quad->NumberofBasicUnit==0) && p_Img->type == P_SLICE )
02073 updateLastBU( p_Img, p_Inp, p_quad, p_gen, topfield );
02074
02075 return p_quad->m_Qc;
02076 }
02077 }
02078 }
02079 }
02080 }
02081 return p_quad->m_Qc;
02082 }
02083
02084
02085
02086
02087
02088
02089
02090
02091 void updateQPInterlace( RCQuadratic *p_quad, RCGeneric *p_gen )
02092 {
02093 if(p_gen->FieldControl==0)
02094 {
02095
02096 if(p_gen->FieldFrame==1)
02097 {
02098 p_quad->PrevLastQP=p_quad->CurrLastQP;
02099 p_quad->CurrLastQP=p_quad->FrameQPBuffer;
02100 }
02101
02102 else
02103 {
02104 p_quad->PrevLastQP=p_quad->CurrLastQP;
02105 p_quad->CurrLastQP=p_quad->FieldQPBuffer;
02106 }
02107 }
02108 }
02109
02110
02111
02112
02113
02114
02115
02116
02117 void updateQPNonPicAFF( seq_parameter_set_rbsp_t *active_sps, RCQuadratic *p_quad )
02118 {
02119 if(active_sps->frame_mbs_only_flag)
02120 {
02121 p_quad->TotalQpforPPicture +=p_quad->m_Qc;
02122 p_quad->PrevLastQP=p_quad->CurrLastQP;
02123 p_quad->CurrLastQP=p_quad->m_Qc;
02124 p_quad->Pm_Qp=p_quad->m_Qc;
02125 }
02126
02127 else
02128 p_quad->FrameQPBuffer=p_quad->m_Qc;
02129 }
02130
02131
02132
02133
02134
02135
02136
02137 void updateBottomField( InputParameters *p_Inp, RCQuadratic *p_quad )
02138 {
02139
02140 if(p_Inp->PicInterlace==FIELD_CODING)
02141 {
02142 p_quad->TotalQpforPPicture +=p_quad->m_Qc;
02143 p_quad->PrevLastQP=p_quad->CurrLastQP+1;
02144 p_quad->CurrLastQP=p_quad->m_Qc;
02145 p_quad->Pm_Qp=p_quad->m_Qc;
02146 }
02147
02148 else
02149 p_quad->FieldQPBuffer=p_quad->m_Qc;
02150 }
02151
02152
02153
02154
02155
02156
02157
02158 int updateFirstP( ImageParameters *p_Img, InputParameters *p_Inp, RCQuadratic *p_quad, RCGeneric *p_gen, int topfield )
02159 {
02160
02161
02162 p_quad->m_Qc=p_quad->MyInitialQp;
02163 p_gen->NumberofBasicUnitHeaderBits=0;
02164 p_gen->NumberofBasicUnitTextureBits=0;
02165 p_quad->NumberofBasicUnit--;
02166
02167 if((!topfield)&&(p_quad->NumberofBasicUnit==0))
02168 {
02169
02170 if((p_Img->active_sps->frame_mbs_only_flag)||(p_Inp->PicInterlace==FIELD_CODING))
02171 {
02172 p_quad->TotalQpforPPicture +=p_quad->m_Qc;
02173 p_quad->PrevLastQP=p_quad->CurrLastQP;
02174 p_quad->CurrLastQP=p_quad->m_Qc;
02175 p_quad->PAveFrameQP=p_quad->m_Qc;
02176 p_quad->PAveHeaderBits3=p_quad->PAveHeaderBits2;
02177 }
02178
02179 else if((p_Inp->PicInterlace==ADAPTIVE_CODING)||(p_Inp->MbInterlace))
02180 {
02181 if(p_gen->FieldControl==0)
02182 {
02183 p_quad->FrameQPBuffer=p_quad->m_Qc;
02184 p_quad->FrameAveHeaderBits=p_quad->PAveHeaderBits2;
02185 }
02186 else
02187 {
02188 p_quad->FieldQPBuffer=p_quad->m_Qc;
02189 p_quad->FieldAveHeaderBits=p_quad->PAveHeaderBits2;
02190 }
02191 }
02192 }
02193 p_quad->Pm_Qp=p_quad->m_Qc;
02194 p_quad->TotalFrameQP +=p_quad->m_Qc;
02195 return p_quad->m_Qc;
02196 }
02197
02198
02199
02200
02201
02202
02203
02204 int updateNegativeTarget( ImageParameters *p_Img, InputParameters *p_Inp, RCQuadratic *p_quad, RCGeneric *p_gen, int topfield, int m_Qp )
02205 {
02206 int PAverageQP;
02207
02208 if(p_quad->GOPOverdue==TRUE)
02209 p_quad->m_Qc=m_Qp+2;
02210 else
02211 p_quad->m_Qc=m_Qp+p_quad->DDquant;
02212
02213 p_quad->m_Qc = imin(p_quad->m_Qc, p_Img->RCMaxQP + p_quad->bitdepth_qp_scale);
02214 if(p_Inp->basicunit>=p_quad->MBPerRow)
02215 p_quad->m_Qc = imin(p_quad->m_Qc, p_quad->PAveFrameQP + 6);
02216 else
02217 p_quad->m_Qc = imin(p_quad->m_Qc, p_quad->PAveFrameQP + 3);
02218
02219 p_quad->TotalFrameQP +=p_quad->m_Qc;
02220 p_quad->NumberofBasicUnit--;
02221 if(p_quad->NumberofBasicUnit==0)
02222 {
02223 if((!topfield)||(p_gen->FieldControl==0))
02224 {
02225
02226 if((p_Img->active_sps->frame_mbs_only_flag)||(p_Inp->PicInterlace==FIELD_CODING))
02227 {
02228 PAverageQP=(int)((double)p_quad->TotalFrameQP/(double)p_quad->TotalNumberofBasicUnit+0.5);
02229 if (p_quad->NumberofPPicture == (p_Inp->intra_period - 2))
02230 p_quad->QPLastPFrame = PAverageQP;
02231
02232 p_quad->TotalQpforPPicture +=PAverageQP;
02233 if(p_quad->GOPOverdue==TRUE)
02234 {
02235 p_quad->PrevLastQP=p_quad->CurrLastQP+1;
02236 p_quad->CurrLastQP=PAverageQP;
02237 }
02238 else
02239 {
02240 if((p_quad->NumberofPPicture==0)&&(p_gen->NumberofGOP>1))
02241 {
02242 p_quad->PrevLastQP=p_quad->CurrLastQP;
02243 p_quad->CurrLastQP=PAverageQP;
02244 }
02245 else if(p_quad->NumberofPPicture>0)
02246 {
02247 p_quad->PrevLastQP=p_quad->CurrLastQP+1;
02248 p_quad->CurrLastQP=PAverageQP;
02249 }
02250 }
02251 p_quad->PAveFrameQP=PAverageQP;
02252 p_quad->PAveHeaderBits3=p_quad->PAveHeaderBits2;
02253 }
02254
02255 else if((p_Inp->PicInterlace==ADAPTIVE_CODING)||(p_Inp->MbInterlace))
02256 {
02257 if(p_gen->FieldControl==0)
02258 {
02259 PAverageQP=(int)((double)p_quad->TotalFrameQP/(double)p_quad->TotalNumberofBasicUnit+0.5);
02260 p_quad->FrameQPBuffer=PAverageQP;
02261 p_quad->FrameAveHeaderBits=p_quad->PAveHeaderBits2;
02262 }
02263 else
02264 {
02265 PAverageQP=(int)((double)p_quad->TotalFrameQP/(double)p_quad->TotalNumberofBasicUnit+0.5);
02266 p_quad->FieldQPBuffer=PAverageQP;
02267 p_quad->FieldAveHeaderBits=p_quad->PAveHeaderBits2;
02268 }
02269 }
02270 }
02271 }
02272 if(p_quad->GOPOverdue==TRUE)
02273 p_quad->Pm_Qp=p_quad->PAveFrameQP;
02274 else
02275 p_quad->Pm_Qp=p_quad->m_Qc;
02276
02277 return p_quad->m_Qc;
02278 }
02279
02280
02281
02282
02283
02284
02285
02286 int updateFirstBU( ImageParameters *p_Img, InputParameters *p_Inp, RCQuadratic *p_quad, RCGeneric *p_gen, int topfield )
02287 {
02288
02289 if(((p_Inp->PicInterlace==ADAPTIVE_CODING)||(p_Inp->MbInterlace))&&(p_gen->FieldControl==0))
02290 {
02291
02292 if(p_gen->FieldFrame==1)
02293 {
02294 if(p_quad->NumberofPPicture>0)
02295 p_quad->TotalQpforPPicture +=p_quad->FrameQPBuffer;
02296 p_quad->PAveFrameQP=p_quad->FrameQPBuffer;
02297 p_quad->PAveHeaderBits3=p_quad->FrameAveHeaderBits;
02298 }
02299
02300 else
02301 {
02302 if(p_quad->NumberofPPicture>0)
02303 p_quad->TotalQpforPPicture +=p_quad->FieldQPBuffer;
02304 p_quad->PAveFrameQP=p_quad->FieldQPBuffer;
02305 p_quad->PAveHeaderBits3=p_quad->FieldAveHeaderBits;
02306 }
02307 }
02308
02309 if(p_quad->Target<=0)
02310 {
02311 p_quad->m_Qc = p_quad->PAveFrameQP + 2;
02312 if(p_quad->m_Qc > (p_Img->RCMaxQP + p_quad->bitdepth_qp_scale))
02313 p_quad->m_Qc = p_Img->RCMaxQP + p_quad->bitdepth_qp_scale;
02314
02315 if(topfield||(p_gen->FieldControl==0))
02316 p_quad->GOPOverdue=TRUE;
02317 }
02318 else
02319 {
02320 p_quad->m_Qc=p_quad->PAveFrameQP;
02321 }
02322 p_quad->TotalFrameQP +=p_quad->m_Qc;
02323 p_quad->NumberofBasicUnit--;
02324 p_quad->Pm_Qp = p_quad->PAveFrameQP;
02325
02326 return p_quad->m_Qc;
02327 }
02328
02329
02330
02331
02332
02333
02334
02335 void updateLastBU( ImageParameters *p_Img, InputParameters *p_Inp, RCQuadratic *p_quad, RCGeneric *p_gen, int topfield )
02336 {
02337 int PAverageQP;
02338
02339 if((!topfield)||(p_gen->FieldControl==0))
02340 {
02341
02342 if((p_Img->active_sps->frame_mbs_only_flag)||(p_Inp->PicInterlace==FIELD_CODING))
02343 {
02344 PAverageQP=(int)((double)p_quad->TotalFrameQP/(double) p_quad->TotalNumberofBasicUnit+0.5);
02345 if (p_quad->NumberofPPicture == (p_Inp->intra_period - 2))
02346 p_quad->QPLastPFrame = PAverageQP;
02347
02348 p_quad->TotalQpforPPicture +=PAverageQP;
02349 p_quad->PrevLastQP=p_quad->CurrLastQP;
02350 p_quad->CurrLastQP=PAverageQP;
02351 p_quad->PAveFrameQP=PAverageQP;
02352 p_quad->PAveHeaderBits3=p_quad->PAveHeaderBits2;
02353 }
02354 else if((p_Inp->PicInterlace==ADAPTIVE_CODING)||(p_Inp->MbInterlace))
02355 {
02356 if(p_gen->FieldControl==0)
02357 {
02358 PAverageQP=(int)((double) p_quad->TotalFrameQP/(double)p_quad->TotalNumberofBasicUnit+0.5);
02359 p_quad->FrameQPBuffer=PAverageQP;
02360 p_quad->FrameAveHeaderBits=p_quad->PAveHeaderBits2;
02361 }
02362 else
02363 {
02364 PAverageQP=(int)((double) p_quad->TotalFrameQP/(double) p_quad->TotalNumberofBasicUnit+0.5);
02365 p_quad->FieldQPBuffer=PAverageQP;
02366 p_quad->FieldAveHeaderBits=p_quad->PAveHeaderBits2;
02367 }
02368 }
02369 }
02370 }
02371
02372
02373
02374
02375
02376
02377
02378 void predictCurrPicMAD( InputParameters *p_Inp, RCQuadratic *p_quad, RCGeneric *p_gen )
02379 {
02380 int i;
02381 if(((p_Inp->PicInterlace==ADAPTIVE_CODING)||(p_Inp->MbInterlace))&&(p_gen->FieldControl==1))
02382 {
02383 p_quad->CurrentFrameMAD=p_quad->MADPictureC1*p_quad->FCBUPFMAD[p_quad->TotalNumberofBasicUnit-p_quad->NumberofBasicUnit]+p_quad->MADPictureC2;
02384 p_quad->TotalBUMAD=0;
02385 for(i=p_quad->TotalNumberofBasicUnit-1; i>=(p_quad->TotalNumberofBasicUnit-p_quad->NumberofBasicUnit);i--)
02386 {
02387 p_quad->CurrentBUMAD=p_quad->MADPictureC1*p_quad->FCBUPFMAD[i]+p_quad->MADPictureC2;
02388 p_quad->TotalBUMAD +=p_quad->CurrentBUMAD*p_quad->CurrentBUMAD;
02389 }
02390 }
02391 else
02392 {
02393 p_quad->CurrentFrameMAD=p_quad->MADPictureC1*p_quad->BUPFMAD[p_quad->TotalNumberofBasicUnit-p_quad->NumberofBasicUnit]+p_quad->MADPictureC2;
02394 p_quad->TotalBUMAD=0;
02395 for(i=p_quad->TotalNumberofBasicUnit-1; i>=(p_quad->TotalNumberofBasicUnit-p_quad->NumberofBasicUnit);i--)
02396 {
02397 p_quad->CurrentBUMAD=p_quad->MADPictureC1*p_quad->BUPFMAD[i]+p_quad->MADPictureC2;
02398 p_quad->TotalBUMAD +=p_quad->CurrentBUMAD*p_quad->CurrentBUMAD;
02399 }
02400 }
02401 }
02402
02403
02404
02405
02406
02407
02408
02409 void updateModelQPBU( ImageParameters *p_Img, InputParameters *p_Inp, RCQuadratic *p_quad, int m_Qp )
02410 {
02411 double dtmp, m_Qstep;
02412 int m_Bits;
02413
02414 m_Bits =(int)(p_quad->Target * p_quad->CurrentFrameMAD * p_quad->CurrentFrameMAD / p_quad->TotalBUMAD);
02415
02416 m_Bits -=p_quad->PAveHeaderBits2;
02417
02418 m_Bits=imax(m_Bits,(int)(p_quad->bit_rate/(MINVALUE*p_quad->frame_rate*p_quad->TotalNumberofBasicUnit)));
02419
02420 dtmp = p_quad->CurrentFrameMAD * p_quad->CurrentFrameMAD * p_quad->m_X1 * p_quad->m_X1 \
02421 + 4 * p_quad->m_X2 * p_quad->CurrentFrameMAD * m_Bits;
02422 if ((p_quad->m_X2 == 0.0) || (dtmp < 0) || ((sqrt (dtmp) - p_quad->m_X1 * p_quad->CurrentFrameMAD) <= 0.0))
02423 m_Qstep = (float)(p_quad->m_X1 * p_quad->CurrentFrameMAD / (double) m_Bits);
02424 else
02425 m_Qstep = (float) ((2 * p_quad->m_X2 * p_quad->CurrentFrameMAD) / (sqrt (dtmp) - p_quad->m_X1 * p_quad->CurrentFrameMAD));
02426
02427 p_quad->m_Qc = Qstep2QP(m_Qstep, p_quad->bitdepth_qp_scale);
02428 p_quad->m_Qc = imin(m_Qp+p_quad->DDquant, p_quad->m_Qc);
02429
02430 if(p_Inp->basicunit>=p_quad->MBPerRow)
02431 p_quad->m_Qc = imin(p_quad->PAveFrameQP+6, p_quad->m_Qc);
02432 else
02433 p_quad->m_Qc = imin(p_quad->PAveFrameQP+3, p_quad->m_Qc);
02434
02435 p_quad->m_Qc = iClip3(m_Qp-p_quad->DDquant, p_Img->RCMaxQP + p_quad->bitdepth_qp_scale, p_quad->m_Qc);
02436 if(p_Inp->basicunit>=p_quad->MBPerRow)
02437 p_quad->m_Qc = imax(p_quad->PAveFrameQP-6, p_quad->m_Qc);
02438 else
02439 p_quad->m_Qc = imax(p_quad->PAveFrameQP-3, p_quad->m_Qc);
02440
02441 p_quad->m_Qc = imax(p_Img->RCMinQP + p_quad->bitdepth_qp_scale, p_quad->m_Qc);
02442 }
02443
02444
02445
02446
02447
02448
02449
02450 void updateQPInterlaceBU( RCQuadratic *p_quad, RCGeneric *p_gen )
02451 {
02452
02453 if(p_gen->FieldFrame==1)
02454 {
02455 p_quad->TotalQpforPPicture +=p_quad->FrameQPBuffer;
02456 p_quad->Pm_Qp=p_quad->FrameQPBuffer;
02457 }
02458
02459 else
02460 {
02461 p_quad->TotalQpforPPicture +=p_quad->FieldQPBuffer;
02462 p_quad->Pm_Qp=p_quad->FieldQPBuffer;
02463 }
02464 }
02465
02466
02467
02468
02469
02470
02471
02472 void updateModelQPFrame( RCQuadratic *p_quad, int m_Bits )
02473 {
02474 double dtmp, m_Qstep;
02475
02476 dtmp = p_quad->CurrentFrameMAD * p_quad->m_X1 * p_quad->CurrentFrameMAD * p_quad->m_X1
02477 + 4 * p_quad->m_X2 * p_quad->CurrentFrameMAD * m_Bits;
02478 if ((p_quad->m_X2 == 0.0) || (dtmp < 0) || ((sqrt (dtmp) - p_quad->m_X1 * p_quad->CurrentFrameMAD) <= 0.0))
02479 m_Qstep = (float) (p_quad->m_X1 * p_quad->CurrentFrameMAD / (double) m_Bits);
02480 else
02481 m_Qstep = (float) ((2 * p_quad->m_X2 * p_quad->CurrentFrameMAD) / (sqrt (dtmp) - p_quad->m_X1 * p_quad->CurrentFrameMAD));
02482
02483 p_quad->m_Qc = Qstep2QP(m_Qstep, p_quad->bitdepth_qp_scale);
02484 }
02485
02486
02487
02488
02489
02490
02491
02492 int rc_handle_mb( Macroblock *currMB, int prev_mb )
02493 {
02494 ImageParameters *p_Img = currMB->p_Img;
02495 InputParameters *p_Inp = currMB->p_Inp;
02496 Macroblock *prevMB = currMB->PrevMB;
02497 int mb_qp = p_Img->qp;
02498 RCGeneric *p_gen = p_Img->p_rc_gen;
02499 RCQuadratic *p_quad = p_Img->p_rc_quad;
02500
02501 if (prev_mb > -1)
02502 {
02503 if ( p_Inp->MbInterlace == ADAPTIVE_CODING && !p_Img->bot_MB && currMB->mb_field )
02504 mb_qp = prevMB->qp;
02505 }
02506
02507
02508 if (p_Inp->basicunit != p_Img->FrameSizeInMbs)
02509 {
02510
02511 if ( ((p_Img->type == I_SLICE || p_Img->type == B_SLICE) && p_Inp->RCUpdateMode != RC_MODE_1 ) || !(p_Img->number) )
02512 {
02513 return mb_qp;
02514 }
02515 else if ( p_Img->type == P_SLICE || p_Inp->RCUpdateMode == RC_MODE_1 )
02516 {
02517 if (!p_Img->write_macroblock)
02518 {
02519 if (prev_mb > -1)
02520 {
02521 if (!((p_Inp->MbInterlace) && p_Img->bot_MB))
02522 {
02523 if (prevMB->prev_cbp != 1)
02524 {
02525 mb_qp = prevMB->prev_qp;
02526 }
02527 }
02528 }
02529 }
02530
02531
02532
02533 if (!p_Img->write_macroblock)
02534 {
02535 if(!((p_Inp->MbInterlace) && p_Img->bot_MB))
02536 {
02537 if(p_Inp->RCUpdateMode <= MAX_RC_MODE && (p_Img->NumberofCodedMacroBlocks > 0) && (p_Img->NumberofCodedMacroBlocks % p_Img->BasicUnit == 0))
02538 {
02539 updateRCModel(p_Img, p_Inp, p_quad, p_gen);
02540
02541 if(p_Img->active_sps->frame_mbs_only_flag)
02542 {
02543 p_Img->BasicUnitQP = p_Img->updateQP(p_Img, p_Inp, p_quad, p_gen, p_gen->TopFieldFlag) - p_quad->bitdepth_qp_scale;
02544
02545 }
02546
02547 else if(p_Inp->MbInterlace || ((p_Inp->PicInterlace!=FRAME_CODING) && (p_gen->NoGranularFieldRC==0)))
02548 {
02549 p_Img->BasicUnitQP = p_Img->updateQP(p_Img, p_Inp, p_quad, p_gen, p_gen->TopFieldFlag) - p_quad->bitdepth_qp_scale;
02550 }
02551 }
02552
02553 if(p_Img->current_mb_nr==0)
02554 p_Img->BasicUnitQP = mb_qp;
02555
02556
02557 mb_qp = p_Img->BasicUnitQP;
02558 mb_qp = iClip3(MIN_QP - p_Img->bitdepth_luma_qp_scale, MAX_QP, mb_qp);
02559 }
02560 }
02561 }
02562 }
02563 return mb_qp;
02564 }
02565
02566
02567
02568
02569
02570
02571
02572 void rc_init_top_field ( ImageParameters *p_Img, InputParameters *p_Inp )
02573 {
02574 RCGeneric *p_gen = p_Img->p_rc_gen;
02575 RCQuadratic *p_quad = p_Img->p_rc_quad;
02576
02577 p_Img->BasicUnit = p_Inp->basicunit;
02578 p_gen->TopFieldFlag = 1;
02579 p_Img->rc_init_pict_ptr(p_Img, p_Inp, p_quad, p_gen, 0, 1, (p_Inp->PicInterlace == FIELD_CODING), 1.0F);
02580 p_Img->qp = p_Img->updateQP(p_Img, p_Inp, p_quad, p_gen, 1) - p_quad->bitdepth_qp_scale;
02581 }
02582
02583
02584
02585
02586
02587
02588
02589 void rc_init_bottom_field ( ImageParameters *p_Img, InputParameters *p_Inp, int TopFieldBits )
02590 {
02591 RCGeneric *p_gen = p_Img->p_rc_gen;
02592 RCQuadratic *p_quad = p_Img->p_rc_quad;
02593
02594 p_quad->bits_topfield = TopFieldBits;
02595 p_gen->TopFieldFlag = 0;
02596 p_Img->rc_init_pict_ptr(p_Img, p_Inp, p_quad, p_gen, 0,0,0, 1.0F);
02597 p_Img->qp = p_Img->updateQP(p_Img, p_Inp, p_quad, p_gen, 0) - p_quad->bitdepth_qp_scale;
02598 }
02599
02600
02601
02602
02603
02604
02605
02606 void rc_init_frame_rdpic( ImageParameters *p_Img, InputParameters *p_Inp, float rateRatio )
02607 {
02608 RCGeneric *p_gen = p_Img->p_rc_gen;
02609 RCGeneric *p_gen_init = p_Img->p_rc_gen_init;
02610 RCQuadratic *p_quad = p_Img->p_rc_quad;
02611 RCQuadratic *p_quad_init = p_Img->p_rc_quad_init;
02612
02613 switch (p_Inp->RCUpdateMode)
02614 {
02615 case RC_MODE_0: case RC_MODE_1: case RC_MODE_2: case RC_MODE_3:
02616
02617 rc_copy_quadratic( p_Img, p_Inp, p_quad, p_quad_init );
02618 rc_copy_generic( p_Img, p_gen, p_gen_init );
02619 p_Img->rc_init_pict_ptr(p_Img, p_Inp, p_quad, p_gen, 1, 0, 1, rateRatio );
02620 p_Img->qp = p_Img->updateQP(p_Img, p_Inp, p_quad, p_gen, 0) - p_quad->bitdepth_qp_scale;
02621 break;
02622 default:
02623 break;
02624 }
02625 }
02626
02627
02628
02629
02630
02631
02632
02633 void rc_allocate_memory( ImageParameters *p_Img, InputParameters *p_Inp )
02634 {
02635 switch (p_Inp->RCUpdateMode)
02636 {
02637 case RC_MODE_0: case RC_MODE_1: case RC_MODE_2: case RC_MODE_3:
02638 rc_alloc_generic( p_Img, &p_Img->p_rc_gen );
02639 rc_alloc_quadratic( p_Img, p_Inp, &p_Img->p_rc_quad );
02640
02641 if ( p_Inp->RDPictureDecision || p_Inp->MbInterlace == ADAPTIVE_CODING )
02642 {
02643 rc_alloc_generic( p_Img, &p_Img->p_rc_gen_init );
02644 rc_alloc_quadratic( p_Img, p_Inp, &p_Img->p_rc_quad_init );
02645 rc_alloc_generic( p_Img, &p_Img->p_rc_gen_best );
02646 rc_alloc_quadratic( p_Img, p_Inp, &p_Img->p_rc_quad_best );
02647 }
02648 break;
02649 default:
02650 break;
02651 }
02652 }
02653
02654
02655
02656
02657
02658
02659
02660 void rc_free_memory( ImageParameters *p_Img, InputParameters *p_Inp )
02661 {
02662 switch (p_Inp->RCUpdateMode)
02663 {
02664 case RC_MODE_0: case RC_MODE_1: case RC_MODE_2: case RC_MODE_3:
02665 rc_free_generic( &p_Img->p_rc_gen );
02666 rc_free_quadratic( &p_Img->p_rc_quad );
02667
02668 if ( p_Inp->RDPictureDecision || p_Inp->MbInterlace == ADAPTIVE_CODING )
02669 {
02670 rc_free_generic( &p_Img->p_rc_gen_init );
02671 rc_free_quadratic( &p_Img->p_rc_quad_init );
02672 rc_free_generic( &p_Img->p_rc_gen_best );
02673 rc_free_quadratic( &p_Img->p_rc_quad_best );
02674 }
02675 break;
02676 default:
02677 break;
02678 }
02679 }
02680
02681
02682
02683
02684
02685
02686
02687 void rc_update_mb_stats(Macroblock *currMB)
02688 {
02689 ImageParameters *p_Img = currMB->p_Img;
02690 InputParameters *p_Inp = currMB->p_Inp;
02691 BitCounter *mbBits = &currMB->bits;
02692 RCGeneric *p_gen = p_Img->p_rc_gen;
02693
02694
02695 p_Img->NumberofMBHeaderBits = mbBits->mb_mode + mbBits->mb_inter
02696 + mbBits->mb_cbp + mbBits->mb_delta_quant;
02697 p_Img->NumberofMBTextureBits = mbBits->mb_y_coeff + mbBits->mb_uv_coeff;
02698
02699 switch (p_Inp->RCUpdateMode)
02700 {
02701 case RC_MODE_0: case RC_MODE_1: case RC_MODE_2: case RC_MODE_3:
02702 p_gen->NumberofTextureBits += p_Img->NumberofMBTextureBits;
02703 p_gen->NumberofHeaderBits += p_Img->NumberofMBHeaderBits;
02704
02705 if(p_Img->BasicUnit < p_Img->FrameSizeInMbs)
02706 {
02707 p_gen->NumberofBasicUnitHeaderBits += p_Img->NumberofMBHeaderBits;
02708 p_gen->NumberofBasicUnitTextureBits += p_Img->NumberofMBTextureBits;
02709 }
02710 break;
02711 default:
02712 break;
02713 }
02714 }
02715
02716
02717
02718
02719
02720
02721
02722 void rc_save_state( ImageParameters *p_Img, InputParameters *p_Inp )
02723 {
02724 switch (p_Inp->RCUpdateMode)
02725 {
02726 case RC_MODE_0: case RC_MODE_1: case RC_MODE_2: case RC_MODE_3:
02727 rc_copy_quadratic( p_Img, p_Inp, p_Img->p_rc_quad_best, p_Img->p_rc_quad );
02728 rc_copy_generic( p_Img, p_Img->p_rc_gen_best, p_Img->p_rc_gen );
02729 break;
02730 default:
02731 break;
02732 }
02733 }
02734
02735
02736
02737
02738
02739
02740
02741 void rc_restore_state( ImageParameters *p_Img, InputParameters *p_Inp )
02742 {
02743 switch (p_Inp->RCUpdateMode)
02744 {
02745 case RC_MODE_0: case RC_MODE_1: case RC_MODE_2: case RC_MODE_3:
02746 rc_copy_quadratic( p_Img, p_Inp, p_Img->p_rc_quad, p_Img->p_rc_quad_best );
02747 rc_copy_generic( p_Img, p_Img->p_rc_gen, p_Img->p_rc_gen_best );
02748 break;
02749 default:
02750 break;
02751 }
02752 }