1 /* ------------------------------------------------------------------ 2 * Copyright (C) 1998-2009 PacketVideo 3 * 4 * Licensed under the Apache License, Version 2.0 (the "License"); 5 * you may not use this file except in compliance with the License. 6 * You may obtain a copy of the License at 7 * 8 * http://www.apache.org/licenses/LICENSE-2.0 9 * 10 * Unless required by applicable law or agreed to in writing, software 11 * distributed under the License is distributed on an "AS IS" BASIS, 12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either 13 * express or implied. 14 * See the License for the specific language governing permissions 15 * and limitations under the License. 16 * ------------------------------------------------------------------- 17 */ 18 #include "avcenc_lib.h" 19 #include <math.h> 20 21 /* rate control variables */ 22 #define RC_MAX_QUANT 51 23 #define RC_MIN_QUANT 0 //cap to 10 to prevent rate fluctuation 24 25 #define MAD_MIN 1 /* handle the case of devision by zero in RC */ 26 27 28 /* local functions */ 29 double QP2Qstep(int QP); 30 int Qstep2QP(double Qstep); 31 32 double ComputeFrameMAD(AVCCommonObj *video, AVCRateControl *rateCtrl); 33 34 void targetBitCalculation(AVCEncObject *encvid, AVCCommonObj *video, AVCRateControl *rateCtrl, MultiPass *pMP); 35 36 void calculateQuantizer_Multipass(AVCEncObject *encvid, AVCCommonObj *video, 37 AVCRateControl *rateCtrl, MultiPass *pMP); 38 39 void updateRC_PostProc(AVCRateControl *rateCtrl, MultiPass *pMP); 40 41 void AVCSaveRDSamples(MultiPass *pMP, int counter_samples); 42 43 void updateRateControl(AVCRateControl *rateControl, int nal_type); 44 45 int GetAvgFrameQP(AVCRateControl *rateCtrl) 46 { 47 return rateCtrl->Qc; 48 } 49 50 AVCEnc_Status RCDetermineFrameNum(AVCEncObject *encvid, AVCRateControl *rateCtrl, uint32 modTime, uint *frameNum) 51 { 52 AVCCommonObj *video = encvid->common; 53 AVCSliceHeader *sliceHdr = video->sliceHdr; 54 uint32 modTimeRef = encvid->modTimeRef; 55 int32 currFrameNum ; 56 int frameInc; 57 58 59 /* check with the buffer fullness to make sure that we have enough bits to encode this frame */ 60 /* we can use a threshold to guarantee minimum picture quality */ 61 /**********************************/ 62 63 /* for now, the default is to encode every frame, To Be Changed */ 64 if (rateCtrl->first_frame) 65 { 66 encvid->modTimeRef = modTime; 67 encvid->wrapModTime = 0; 68 encvid->prevFrameNum = 0; 69 encvid->prevProcFrameNum = 0; 70 71 *frameNum = 0; 72 73 /* set frame type to IDR-frame */ 74 video->nal_unit_type = AVC_NALTYPE_IDR; 75 sliceHdr->slice_type = AVC_I_ALL_SLICE; 76 video->slice_type = AVC_I_SLICE; 77 78 return AVCENC_SUCCESS; 79 } 80 else 81 { 82 if (modTime < modTimeRef) /* modTime wrapped around */ 83 { 84 encvid->wrapModTime += ((uint32)0xFFFFFFFF - modTimeRef) + 1; 85 encvid->modTimeRef = modTimeRef = 0; 86 } 87 modTime += encvid->wrapModTime; /* wrapModTime is non zero after wrap-around */ 88 89 currFrameNum = (int32)(((modTime - modTimeRef) * rateCtrl->frame_rate + 200) / 1000); /* add small roundings */ 90 91 if (currFrameNum <= (int32)encvid->prevProcFrameNum) 92 { 93 return AVCENC_FAIL; /* this is a late frame do not encode it */ 94 } 95 96 frameInc = currFrameNum - encvid->prevProcFrameNum; 97 98 if (frameInc < rateCtrl->skip_next_frame + 1) 99 { 100 return AVCENC_FAIL; /* frame skip required to maintain the target bit rate. */ 101 } 102 103 RCUpdateBuffer(video, rateCtrl, frameInc - rateCtrl->skip_next_frame); /* in case more frames dropped */ 104 105 *frameNum = currFrameNum; 106 107 /* This part would be similar to DetermineVopType of m4venc */ 108 if ((*frameNum >= (uint)rateCtrl->idrPeriod && rateCtrl->idrPeriod > 0) || (*frameNum > video->MaxFrameNum)) /* first frame or IDR*/ 109 { 110 /* set frame type to IDR-frame */ 111 if (rateCtrl->idrPeriod) 112 { 113 encvid->modTimeRef += (uint32)(rateCtrl->idrPeriod * 1000 / rateCtrl->frame_rate); 114 *frameNum -= rateCtrl->idrPeriod; 115 } 116 else 117 { 118 encvid->modTimeRef += (uint32)(video->MaxFrameNum * 1000 / rateCtrl->frame_rate); 119 *frameNum -= video->MaxFrameNum; 120 } 121 122 video->nal_unit_type = AVC_NALTYPE_IDR; 123 sliceHdr->slice_type = AVC_I_ALL_SLICE; 124 video->slice_type = AVC_I_SLICE; 125 encvid->prevProcFrameNum = *frameNum; 126 } 127 else 128 { 129 video->nal_unit_type = AVC_NALTYPE_SLICE; 130 sliceHdr->slice_type = AVC_P_ALL_SLICE; 131 video->slice_type = AVC_P_SLICE; 132 encvid->prevProcFrameNum = currFrameNum; 133 } 134 135 } 136 137 return AVCENC_SUCCESS; 138 } 139 140 void RCUpdateBuffer(AVCCommonObj *video, AVCRateControl *rateCtrl, int frameInc) 141 { 142 int tmp; 143 MultiPass *pMP = rateCtrl->pMP; 144 145 OSCL_UNUSED_ARG(video); 146 147 if (rateCtrl->rcEnable == TRUE) 148 { 149 if (frameInc > 1) 150 { 151 tmp = rateCtrl->bitsPerFrame * (frameInc - 1); 152 rateCtrl->VBV_fullness -= tmp; 153 pMP->counter_BTsrc += 10 * (frameInc - 1); 154 155 /* Check buffer underflow */ 156 if (rateCtrl->VBV_fullness < rateCtrl->low_bound) 157 { 158 rateCtrl->VBV_fullness = rateCtrl->low_bound; // -rateCtrl->Bs/2; 159 rateCtrl->TMN_W = rateCtrl->VBV_fullness - rateCtrl->low_bound; 160 pMP->counter_BTsrc = pMP->counter_BTdst + (int)((OsclFloat)(rateCtrl->Bs / 2 - rateCtrl->low_bound) / 2.0 / (pMP->target_bits_per_frame / 10)); 161 } 162 } 163 } 164 } 165 166 167 AVCEnc_Status InitRateControlModule(AVCHandle *avcHandle) 168 { 169 AVCEncObject *encvid = (AVCEncObject*) avcHandle->AVCObject; 170 AVCCommonObj *video = encvid->common; 171 AVCRateControl *rateCtrl = encvid->rateCtrl; 172 double L1, L2, L3, bpp; 173 int qp; 174 int i, j; 175 176 rateCtrl->basicUnit = video->PicSizeInMbs; 177 178 rateCtrl->MADofMB = (double*) avcHandle->CBAVC_Malloc(encvid->avcHandle->userData, 179 video->PicSizeInMbs * sizeof(double), DEFAULT_ATTR); 180 181 if (!rateCtrl->MADofMB) 182 { 183 goto CLEANUP_RC; 184 } 185 186 if (rateCtrl->rcEnable == TRUE) 187 { 188 rateCtrl->pMP = (MultiPass*) avcHandle->CBAVC_Malloc(encvid->avcHandle->userData, sizeof(MultiPass), DEFAULT_ATTR); 189 if (!rateCtrl->pMP) 190 { 191 goto CLEANUP_RC; 192 } 193 rateCtrl->pMP->encoded_frames = -1; /* forget about the very first I frame */ 194 195 /* RDInfo **pRDSamples */ 196 rateCtrl->pMP->pRDSamples = (RDInfo **)avcHandle->CBAVC_Malloc(encvid->avcHandle->userData, (30 * sizeof(RDInfo *)), DEFAULT_ATTR); 197 if (!rateCtrl->pMP->pRDSamples) 198 { 199 goto CLEANUP_RC; 200 } 201 202 for (i = 0; i < 30; i++) 203 { 204 rateCtrl->pMP->pRDSamples[i] = (RDInfo *)avcHandle->CBAVC_Malloc(encvid->avcHandle->userData, (32 * sizeof(RDInfo)), DEFAULT_ATTR); 205 if (!rateCtrl->pMP->pRDSamples[i]) 206 { 207 goto CLEANUP_RC; 208 } 209 } 210 rateCtrl->pMP->frameRange = (int)(rateCtrl->frame_rate * 1.0); /* 1.0s time frame*/ 211 rateCtrl->pMP->frameRange = AVC_MAX(rateCtrl->pMP->frameRange, 5); 212 rateCtrl->pMP->frameRange = AVC_MIN(rateCtrl->pMP->frameRange, 30); 213 214 rateCtrl->pMP->framePos = -1; 215 216 217 rateCtrl->bitsPerFrame = (int32)(rateCtrl->bitRate / rateCtrl->frame_rate); 218 219 /* BX rate control */ 220 rateCtrl->skip_next_frame = 0; /* must be initialized */ 221 222 rateCtrl->Bs = rateCtrl->cpbSize; 223 rateCtrl->TMN_W = 0; 224 rateCtrl->VBV_fullness = (int)(rateCtrl->Bs * 0.5); /* rateCtrl->Bs */ 225 rateCtrl->encoded_frames = 0; 226 227 rateCtrl->TMN_TH = rateCtrl->bitsPerFrame; 228 229 rateCtrl->max_BitVariance_num = (int)((OsclFloat)(rateCtrl->Bs - rateCtrl->VBV_fullness) / (rateCtrl->bitsPerFrame / 10.0)) - 5; 230 if (rateCtrl->max_BitVariance_num < 0) rateCtrl->max_BitVariance_num += 5; 231 232 // Set the initial buffer fullness 233 /* According to the spec, the initial buffer fullness needs to be set to 1/3 */ 234 rateCtrl->VBV_fullness = (int)(rateCtrl->Bs / 3.0 - rateCtrl->Bs / 2.0); /* the buffer range is [-Bs/2, Bs/2] */ 235 rateCtrl->pMP->counter_BTsrc = (int)((rateCtrl->Bs / 2.0 - rateCtrl->Bs / 3.0) / (rateCtrl->bitsPerFrame / 10.0)); 236 rateCtrl->TMN_W = (int)(rateCtrl->VBV_fullness + rateCtrl->pMP->counter_BTsrc * (rateCtrl->bitsPerFrame / 10.0)); 237 238 rateCtrl->low_bound = -rateCtrl->Bs / 2; 239 rateCtrl->VBV_fullness_offset = 0; 240 241 /* Setting the bitrate and framerate */ 242 rateCtrl->pMP->bitrate = rateCtrl->bitRate; 243 rateCtrl->pMP->framerate = rateCtrl->frame_rate; 244 rateCtrl->pMP->target_bits_per_frame = rateCtrl->pMP->bitrate / rateCtrl->pMP->framerate; 245 246 /*compute the initial QP*/ 247 bpp = 1.0 * rateCtrl->bitRate / (rateCtrl->frame_rate * (video->PicSizeInMbs << 8)); 248 if (video->PicWidthInSamplesL == 176) 249 { 250 L1 = 0.1; 251 L2 = 0.3; 252 L3 = 0.6; 253 } 254 else if (video->PicWidthInSamplesL == 352) 255 { 256 L1 = 0.2; 257 L2 = 0.6; 258 L3 = 1.2; 259 } 260 else 261 { 262 L1 = 0.6; 263 L2 = 1.4; 264 L3 = 2.4; 265 } 266 267 if (rateCtrl->initQP == 0) 268 { 269 if (bpp <= L1) 270 qp = 35; 271 else if (bpp <= L2) 272 qp = 25; 273 else if (bpp <= L3) 274 qp = 20; 275 else 276 qp = 15; 277 rateCtrl->initQP = qp; 278 } 279 280 rateCtrl->Qc = rateCtrl->initQP; 281 } 282 283 return AVCENC_SUCCESS; 284 285 CLEANUP_RC: 286 287 CleanupRateControlModule(avcHandle); 288 return AVCENC_MEMORY_FAIL; 289 290 } 291 292 293 void CleanupRateControlModule(AVCHandle *avcHandle) 294 { 295 AVCEncObject *encvid = (AVCEncObject*) avcHandle->AVCObject; 296 AVCRateControl *rateCtrl = encvid->rateCtrl; 297 int i; 298 299 if (rateCtrl->MADofMB) 300 { 301 avcHandle->CBAVC_Free(avcHandle->userData, rateCtrl->MADofMB); 302 } 303 304 if (rateCtrl->pMP) 305 { 306 if (rateCtrl->pMP->pRDSamples) 307 { 308 for (i = 0; i < 30; i++) 309 { 310 if (rateCtrl->pMP->pRDSamples[i]) 311 { 312 avcHandle->CBAVC_Free(avcHandle->userData, rateCtrl->pMP->pRDSamples[i]); 313 } 314 } 315 avcHandle->CBAVC_Free(avcHandle->userData, rateCtrl->pMP->pRDSamples); 316 } 317 avcHandle->CBAVC_Free(avcHandle->userData, rateCtrl->pMP); 318 } 319 320 return ; 321 } 322 323 void RCInitGOP(AVCEncObject *encvid) 324 { 325 /* in BX RC, there's no GOP-level RC */ 326 327 OSCL_UNUSED_ARG(encvid); 328 329 return ; 330 } 331 332 333 void RCInitFrameQP(AVCEncObject *encvid) 334 { 335 AVCCommonObj *video = encvid->common; 336 AVCRateControl *rateCtrl = encvid->rateCtrl; 337 AVCPicParamSet *picParam = video->currPicParams; 338 MultiPass *pMP = rateCtrl->pMP; 339 340 if (rateCtrl->rcEnable == TRUE) 341 { 342 /* frame layer rate control */ 343 if (rateCtrl->encoded_frames == 0) 344 { 345 video->QPy = rateCtrl->Qc = rateCtrl->initQP; 346 } 347 else 348 { 349 calculateQuantizer_Multipass(encvid, video, rateCtrl, pMP); 350 video->QPy = rateCtrl->Qc; 351 } 352 353 rateCtrl->NumberofHeaderBits = 0; 354 rateCtrl->NumberofTextureBits = 0; 355 rateCtrl->numFrameBits = 0; // reset 356 357 /* update pMP->framePos */ 358 if (++pMP->framePos == pMP->frameRange) pMP->framePos = 0; 359 360 if (rateCtrl->T == 0) 361 { 362 pMP->counter_BTdst = (int)(rateCtrl->frame_rate * 7.5 + 0.5); /* 0.75s time frame */ 363 pMP->counter_BTdst = AVC_MIN(pMP->counter_BTdst, (int)(rateCtrl->max_BitVariance_num / 2 * 0.40)); /* 0.75s time frame may go beyond VBV buffer if we set the buffer size smaller than 0.75s */ 364 pMP->counter_BTdst = AVC_MAX(pMP->counter_BTdst, (int)((rateCtrl->Bs / 2 - rateCtrl->VBV_fullness) * 0.30 / (rateCtrl->TMN_TH / 10.0) + 0.5)); /* At least 30% of VBV buffer size/2 */ 365 pMP->counter_BTdst = AVC_MIN(pMP->counter_BTdst, 20); /* Limit the target to be smaller than 3C */ 366 367 pMP->target_bits = rateCtrl->T = rateCtrl->TMN_TH = (int)(rateCtrl->TMN_TH * (1.0 + pMP->counter_BTdst * 0.1)); 368 pMP->diff_counter = pMP->counter_BTdst; 369 } 370 371 /* collect the necessary data: target bits, actual bits, mad and QP */ 372 pMP->target_bits = rateCtrl->T; 373 pMP->QP = video->QPy; 374 375 pMP->mad = (OsclFloat)rateCtrl->totalSAD / video->PicSizeInMbs; //ComputeFrameMAD(video, rateCtrl); 376 if (pMP->mad < MAD_MIN) pMP->mad = MAD_MIN; /* MAD_MIN is defined as 1 in mp4def.h */ 377 378 pMP->bitrate = rateCtrl->bitRate; /* calculated in RCVopQPSetting */ 379 pMP->framerate = rateCtrl->frame_rate; 380 381 /* first pass encoding */ 382 pMP->nRe_Quantized = 0; 383 384 } // rcEnable 385 else 386 { 387 video->QPy = rateCtrl->initQP; 388 } 389 390 // printf(" %d ",video->QPy); 391 392 if (video->CurrPicNum == 0 && encvid->outOfBandParamSet == FALSE) 393 { 394 picParam->pic_init_qs_minus26 = 0; 395 picParam->pic_init_qp_minus26 = video->QPy - 26; 396 } 397 398 // need this for motion estimation 399 encvid->lambda_mode = QP2QUANT[AVC_MAX(0, video->QPy-SHIFT_QP)]; 400 encvid->lambda_motion = LAMBDA_FACTOR(encvid->lambda_mode); 401 return ; 402 } 403 404 /* Mad based variable bit allocation + QP calculation with a new quadratic method */ 405 void calculateQuantizer_Multipass(AVCEncObject *encvid, AVCCommonObj *video, 406 AVCRateControl *rateCtrl, MultiPass *pMP) 407 { 408 int prev_actual_bits = 0, curr_target, /*pos=0,*/i, j; 409 OsclFloat Qstep, prev_QP = 0.625; 410 411 OsclFloat curr_mad, prev_mad, curr_RD, prev_RD, average_mad, aver_QP; 412 413 /* Mad based variable bit allocation */ 414 targetBitCalculation(encvid, video, rateCtrl, pMP); 415 416 if (rateCtrl->T <= 0 || rateCtrl->totalSAD == 0) 417 { 418 if (rateCtrl->T < 0) rateCtrl->Qc = RC_MAX_QUANT; 419 return; 420 } 421 422 /* ---------------------------------------------------------------------------------------------------*/ 423 /* current frame QP estimation */ 424 curr_target = rateCtrl->T; 425 curr_mad = (OsclFloat)rateCtrl->totalSAD / video->PicSizeInMbs; 426 if (curr_mad < MAD_MIN) curr_mad = MAD_MIN; /* MAD_MIN is defined as 1 in mp4def.h */ 427 curr_RD = (OsclFloat)curr_target / curr_mad; 428 429 if (rateCtrl->skip_next_frame == -1) // previous was skipped 430 { 431 i = pMP->framePos; 432 prev_mad = pMP->pRDSamples[i][0].mad; 433 prev_QP = pMP->pRDSamples[i][0].QP; 434 prev_actual_bits = pMP->pRDSamples[i][0].actual_bits; 435 } 436 else 437 { 438 /* Another version of search the optimal point */ 439 prev_mad = 0.0; 440 i = 0; 441 while (i < pMP->frameRange && prev_mad < 0.001) /* find first one with nonzero prev_mad */ 442 { 443 prev_mad = pMP->pRDSamples[i][0].mad; 444 i++; 445 } 446 447 if (i < pMP->frameRange) 448 { 449 prev_actual_bits = pMP->pRDSamples[i-1][0].actual_bits; 450 451 for (j = 0; i < pMP->frameRange; i++) 452 { 453 if (pMP->pRDSamples[i][0].mad != 0 && 454 AVC_ABS(prev_mad - curr_mad) > AVC_ABS(pMP->pRDSamples[i][0].mad - curr_mad)) 455 { 456 prev_mad = pMP->pRDSamples[i][0].mad; 457 prev_actual_bits = pMP->pRDSamples[i][0].actual_bits; 458 j = i; 459 } 460 } 461 prev_QP = QP2Qstep(pMP->pRDSamples[j][0].QP); 462 463 for (i = 1; i < pMP->samplesPerFrame[j]; i++) 464 { 465 if (AVC_ABS(prev_actual_bits - curr_target) > AVC_ABS(pMP->pRDSamples[j][i].actual_bits - curr_target)) 466 { 467 prev_actual_bits = pMP->pRDSamples[j][i].actual_bits; 468 prev_QP = QP2Qstep(pMP->pRDSamples[j][i].QP); 469 } 470 } 471 } 472 } 473 474 // quadratic approximation 475 if (prev_mad > 0.001) // only when prev_mad is greater than 0, otherwise keep using the same QP 476 { 477 prev_RD = (OsclFloat)prev_actual_bits / prev_mad; 478 //rateCtrl->Qc = (Int)(prev_QP * sqrt(prev_actual_bits/curr_target) + 0.4); 479 if (prev_QP == 0.625) // added this to allow getting out of QP = 0 easily 480 { 481 Qstep = (int)(prev_RD / curr_RD + 0.5); 482 } 483 else 484 { 485 // rateCtrl->Qc =(Int)(prev_QP * M4VENC_SQRT(prev_RD/curr_RD) + 0.9); 486 487 if (prev_RD / curr_RD > 0.5 && prev_RD / curr_RD < 2.0) 488 Qstep = (int)(prev_QP * (sqrt(prev_RD / curr_RD) + prev_RD / curr_RD) / 2.0 + 0.9); /* Quadratic and linear approximation */ 489 else 490 Qstep = (int)(prev_QP * (sqrt(prev_RD / curr_RD) + pow(prev_RD / curr_RD, 1.0 / 3.0)) / 2.0 + 0.9); 491 } 492 // lower bound on Qc should be a function of curr_mad 493 // When mad is already low, lower bound on Qc doesn't have to be small. 494 // Note, this doesn't work well for low complexity clip encoded at high bit rate 495 // it doesn't hit the target bit rate due to this QP lower bound. 496 /// if((curr_mad < 8) && (rateCtrl->Qc < 12)) rateCtrl->Qc = 12; 497 // else if((curr_mad < 128) && (rateCtrl->Qc < 3)) rateCtrl->Qc = 3; 498 499 rateCtrl->Qc = Qstep2QP(Qstep); 500 501 if (rateCtrl->Qc < RC_MIN_QUANT) rateCtrl->Qc = RC_MIN_QUANT; 502 if (rateCtrl->Qc > RC_MAX_QUANT) rateCtrl->Qc = RC_MAX_QUANT; 503 } 504 505 /* active bit resource protection */ 506 aver_QP = (pMP->encoded_frames == 0 ? 0 : pMP->sum_QP / (OsclFloat)pMP->encoded_frames); 507 average_mad = (pMP->encoded_frames == 0 ? 0 : pMP->sum_mad / (OsclFloat)pMP->encoded_frames); /* this function is called from the scond encoded frame*/ 508 if (pMP->diff_counter == 0 && 509 ((OsclFloat)rateCtrl->Qc <= aver_QP*1.1 || curr_mad <= average_mad*1.1) && 510 pMP->counter_BTsrc <= (pMP->counter_BTdst + (int)(pMP->framerate*1.0 + 0.5))) 511 { 512 rateCtrl->TMN_TH -= (int)(pMP->target_bits_per_frame / 10.0); 513 rateCtrl->T = rateCtrl->TMN_TH - rateCtrl->TMN_W; 514 pMP->counter_BTsrc++; 515 pMP->diff_counter--; 516 } 517 518 } 519 520 void targetBitCalculation(AVCEncObject *encvid, AVCCommonObj *video, AVCRateControl *rateCtrl, MultiPass *pMP) 521 { 522 OSCL_UNUSED_ARG(encvid); 523 OsclFloat curr_mad;//, average_mad; 524 int diff_counter_BTsrc, diff_counter_BTdst, prev_counter_diff, curr_counter_diff, bound; 525 /* BT = Bit Transfer, for pMP->counter_BTsrc, pMP->counter_BTdst */ 526 527 /* some stuff about frame dropping remained here to be done because pMP cannot be inserted into updateRateControl()*/ 528 updateRC_PostProc(rateCtrl, pMP); 529 530 /* update pMP->counter_BTsrc and pMP->counter_BTdst to avoid interger overflow */ 531 if (pMP->counter_BTsrc > 1000 && pMP->counter_BTdst > 1000) 532 { 533 pMP->counter_BTsrc -= 1000; 534 pMP->counter_BTdst -= 1000; 535 } 536 537 /* ---------------------------------------------------------------------------------------------------*/ 538 /* target calculation */ 539 curr_mad = (OsclFloat)rateCtrl->totalSAD / video->PicSizeInMbs; 540 if (curr_mad < MAD_MIN) curr_mad = MAD_MIN; /* MAD_MIN is defined as 1 in mp4def.h */ 541 diff_counter_BTsrc = diff_counter_BTdst = 0; 542 pMP->diff_counter = 0; 543 544 545 /*1.calculate average mad */ 546 pMP->sum_mad += curr_mad; 547 //average_mad = (pMP->encoded_frames < 1 ? curr_mad : pMP->sum_mad/(OsclFloat)(pMP->encoded_frames+1)); /* this function is called from the scond encoded frame*/ 548 //pMP->aver_mad = average_mad; 549 if (pMP->encoded_frames >= 0) /* pMP->encoded_frames is set to -1 initially, so forget about the very first I frame */ 550 pMP->aver_mad = (pMP->aver_mad * pMP->encoded_frames + curr_mad) / (pMP->encoded_frames + 1); 551 552 if (pMP->overlapped_win_size > 0 && pMP->encoded_frames_prev >= 0) 553 pMP->aver_mad_prev = (pMP->aver_mad_prev * pMP->encoded_frames_prev + curr_mad) / (pMP->encoded_frames_prev + 1); 554 555 /*2.average_mad, mad ==> diff_counter_BTsrc, diff_counter_BTdst */ 556 if (pMP->overlapped_win_size == 0) 557 { 558 /* original verison */ 559 if (curr_mad > pMP->aver_mad*1.1) 560 { 561 if (curr_mad / (pMP->aver_mad + 0.0001) > 2) 562 diff_counter_BTdst = (int)(sqrt(curr_mad / (pMP->aver_mad + 0.0001)) * 10 + 0.4) - 10; 563 //diff_counter_BTdst = (int)((sqrt(curr_mad/pMP->aver_mad)*2+curr_mad/pMP->aver_mad)/(3*0.1) + 0.4) - 10; 564 else 565 diff_counter_BTdst = (int)(curr_mad / (pMP->aver_mad + 0.0001) * 10 + 0.4) - 10; 566 } 567 else /* curr_mad <= average_mad*1.1 */ 568 //diff_counter_BTsrc = 10 - (int)((sqrt(curr_mad/pMP->aver_mad) + pow(curr_mad/pMP->aver_mad, 1.0/3.0))/(2.0*0.1) + 0.4); 569 diff_counter_BTsrc = 10 - (int)(sqrt(curr_mad / (pMP->aver_mad + 0.0001)) * 10 + 0.5); 570 571 /* actively fill in the possible gap */ 572 if (diff_counter_BTsrc == 0 && diff_counter_BTdst == 0 && 573 curr_mad <= pMP->aver_mad*1.1 && pMP->counter_BTsrc < pMP->counter_BTdst) 574 diff_counter_BTsrc = 1; 575 576 } 577 else if (pMP->overlapped_win_size > 0) 578 { 579 /* transition time: use previous average mad "pMP->aver_mad_prev" instead of the current average mad "pMP->aver_mad" */ 580 if (curr_mad > pMP->aver_mad_prev*1.1) 581 { 582 if (curr_mad / pMP->aver_mad_prev > 2) 583 diff_counter_BTdst = (int)(sqrt(curr_mad / (pMP->aver_mad_prev + 0.0001)) * 10 + 0.4) - 10; 584 //diff_counter_BTdst = (int)((M4VENC_SQRT(curr_mad/pMP->aver_mad_prev)*2+curr_mad/pMP->aver_mad_prev)/(3*0.1) + 0.4) - 10; 585 else 586 diff_counter_BTdst = (int)(curr_mad / (pMP->aver_mad_prev + 0.0001) * 10 + 0.4) - 10; 587 } 588 else /* curr_mad <= average_mad*1.1 */ 589 //diff_counter_BTsrc = 10 - (Int)((sqrt(curr_mad/pMP->aver_mad_prev) + pow(curr_mad/pMP->aver_mad_prev, 1.0/3.0))/(2.0*0.1) + 0.4); 590 diff_counter_BTsrc = 10 - (int)(sqrt(curr_mad / (pMP->aver_mad_prev + 0.0001)) * 10 + 0.5); 591 592 /* actively fill in the possible gap */ 593 if (diff_counter_BTsrc == 0 && diff_counter_BTdst == 0 && 594 curr_mad <= pMP->aver_mad_prev*1.1 && pMP->counter_BTsrc < pMP->counter_BTdst) 595 diff_counter_BTsrc = 1; 596 597 if (--pMP->overlapped_win_size <= 0) pMP->overlapped_win_size = 0; 598 } 599 600 601 /* if difference is too much, do clipping */ 602 /* First, set the upper bound for current bit allocation variance: 80% of available buffer */ 603 bound = (int)((rateCtrl->Bs / 2 - rateCtrl->VBV_fullness) * 0.6 / (pMP->target_bits_per_frame / 10)); /* rateCtrl->Bs */ 604 diff_counter_BTsrc = AVC_MIN(diff_counter_BTsrc, bound); 605 diff_counter_BTdst = AVC_MIN(diff_counter_BTdst, bound); 606 607 /* Second, set another upper bound for current bit allocation: 4-5*bitrate/framerate */ 608 bound = 50; 609 // if(video->encParams->RC_Type == CBR_LOWDELAY) 610 // not necessary bound = 10; -- For Low delay */ 611 612 diff_counter_BTsrc = AVC_MIN(diff_counter_BTsrc, bound); 613 diff_counter_BTdst = AVC_MIN(diff_counter_BTdst, bound); 614 615 616 /* Third, check the buffer */ 617 prev_counter_diff = pMP->counter_BTdst - pMP->counter_BTsrc; 618 curr_counter_diff = prev_counter_diff + (diff_counter_BTdst - diff_counter_BTsrc); 619 620 if (AVC_ABS(prev_counter_diff) >= rateCtrl->max_BitVariance_num || AVC_ABS(curr_counter_diff) >= rateCtrl->max_BitVariance_num) 621 { //diff_counter_BTsrc = diff_counter_BTdst = 0; 622 623 if (curr_counter_diff > rateCtrl->max_BitVariance_num && diff_counter_BTdst) 624 { 625 diff_counter_BTdst = (rateCtrl->max_BitVariance_num - prev_counter_diff) + diff_counter_BTsrc; 626 if (diff_counter_BTdst < 0) diff_counter_BTdst = 0; 627 } 628 629 else if (curr_counter_diff < -rateCtrl->max_BitVariance_num && diff_counter_BTsrc) 630 { 631 diff_counter_BTsrc = diff_counter_BTdst - (-rateCtrl->max_BitVariance_num - prev_counter_diff); 632 if (diff_counter_BTsrc < 0) diff_counter_BTsrc = 0; 633 } 634 } 635 636 637 /*3.diff_counter_BTsrc, diff_counter_BTdst ==> TMN_TH */ 638 rateCtrl->TMN_TH = (int)(pMP->target_bits_per_frame); 639 pMP->diff_counter = 0; 640 641 if (diff_counter_BTsrc) 642 { 643 rateCtrl->TMN_TH -= (int)(pMP->target_bits_per_frame * diff_counter_BTsrc * 0.1); 644 pMP->diff_counter = -diff_counter_BTsrc; 645 } 646 else if (diff_counter_BTdst) 647 { 648 rateCtrl->TMN_TH += (int)(pMP->target_bits_per_frame * diff_counter_BTdst * 0.1); 649 pMP->diff_counter = diff_counter_BTdst; 650 } 651 652 653 /*4.update pMP->counter_BTsrc, pMP->counter_BTdst */ 654 pMP->counter_BTsrc += diff_counter_BTsrc; 655 pMP->counter_BTdst += diff_counter_BTdst; 656 657 658 /*5.target bit calculation */ 659 rateCtrl->T = rateCtrl->TMN_TH - rateCtrl->TMN_W; 660 661 return ; 662 } 663 664 void updateRC_PostProc(AVCRateControl *rateCtrl, MultiPass *pMP) 665 { 666 if (rateCtrl->skip_next_frame > 0) /* skip next frame */ 667 { 668 pMP->counter_BTsrc += 10 * rateCtrl->skip_next_frame; 669 670 } 671 else if (rateCtrl->skip_next_frame == -1) /* skip current frame */ 672 { 673 pMP->counter_BTdst -= pMP->diff_counter; 674 pMP->counter_BTsrc += 10; 675 676 pMP->sum_mad -= pMP->mad; 677 pMP->aver_mad = (pMP->aver_mad * pMP->encoded_frames - pMP->mad) / (pMP->encoded_frames - 1 + 0.0001); 678 pMP->sum_QP -= pMP->QP; 679 pMP->encoded_frames --; 680 } 681 /* some stuff in update VBV_fullness remains here */ 682 //if(rateCtrl->VBV_fullness < -rateCtrl->Bs/2) /* rateCtrl->Bs */ 683 if (rateCtrl->VBV_fullness < rateCtrl->low_bound) 684 { 685 rateCtrl->VBV_fullness = rateCtrl->low_bound; // -rateCtrl->Bs/2; 686 rateCtrl->TMN_W = rateCtrl->VBV_fullness - rateCtrl->low_bound; 687 pMP->counter_BTsrc = pMP->counter_BTdst + (int)((OsclFloat)(rateCtrl->Bs / 2 - rateCtrl->low_bound) / 2.0 / (pMP->target_bits_per_frame / 10)); 688 } 689 } 690 691 692 void RCInitChromaQP(AVCEncObject *encvid) 693 { 694 AVCCommonObj *video = encvid->common; 695 AVCMacroblock *currMB = video->currMB; 696 int q_bits; 697 698 /* we have to do the same thing for AVC_CLIP3(0,51,video->QSy) */ 699 700 video->QPy_div_6 = (currMB->QPy * 43) >> 8; 701 video->QPy_mod_6 = currMB->QPy - 6 * video->QPy_div_6; 702 currMB->QPc = video->QPc = mapQPi2QPc[AVC_CLIP3(0, 51, currMB->QPy + video->currPicParams->chroma_qp_index_offset)]; 703 video->QPc_div_6 = (video->QPc * 43) >> 8; 704 video->QPc_mod_6 = video->QPc - 6 * video->QPc_div_6; 705 706 /* pre-calculate this to save computation */ 707 q_bits = 4 + video->QPy_div_6; 708 if (video->slice_type == AVC_I_SLICE) 709 { 710 encvid->qp_const = 682 << q_bits; // intra 711 } 712 else 713 { 714 encvid->qp_const = 342 << q_bits; // inter 715 } 716 717 q_bits = 4 + video->QPc_div_6; 718 if (video->slice_type == AVC_I_SLICE) 719 { 720 encvid->qp_const_c = 682 << q_bits; // intra 721 } 722 else 723 { 724 encvid->qp_const_c = 342 << q_bits; // inter 725 } 726 727 encvid->lambda_mode = QP2QUANT[AVC_MAX(0, currMB->QPy-SHIFT_QP)]; 728 encvid->lambda_motion = LAMBDA_FACTOR(encvid->lambda_mode); 729 730 return ; 731 } 732 733 734 void RCInitMBQP(AVCEncObject *encvid) 735 { 736 AVCCommonObj *video = encvid->common; 737 AVCMacroblock *currMB = video->currMB; 738 739 currMB->QPy = video->QPy; /* set to previous value or picture level */ 740 741 RCInitChromaQP(encvid); 742 743 } 744 745 void RCPostMB(AVCCommonObj *video, AVCRateControl *rateCtrl, int num_header_bits, int num_texture_bits) 746 { 747 OSCL_UNUSED_ARG(video); 748 rateCtrl->numMBHeaderBits = num_header_bits; 749 rateCtrl->numMBTextureBits = num_texture_bits; 750 rateCtrl->NumberofHeaderBits += rateCtrl->numMBHeaderBits; 751 rateCtrl->NumberofTextureBits += rateCtrl->numMBTextureBits; 752 } 753 754 void RCRestoreQP(AVCMacroblock *currMB, AVCCommonObj *video, AVCEncObject *encvid) 755 { 756 currMB->QPy = video->QPy; /* use previous QP */ 757 RCInitChromaQP(encvid); 758 759 return ; 760 } 761 762 763 void RCCalculateMAD(AVCEncObject *encvid, AVCMacroblock *currMB, uint8 *orgL, int orgPitch) 764 { 765 AVCCommonObj *video = encvid->common; 766 AVCRateControl *rateCtrl = encvid->rateCtrl; 767 uint32 dmin_lx; 768 769 if (rateCtrl->rcEnable == TRUE) 770 { 771 if (currMB->mb_intra) 772 { 773 if (currMB->mbMode == AVC_I16) 774 { 775 dmin_lx = (0xFFFF << 16) | orgPitch; 776 rateCtrl->MADofMB[video->mbNum] = AVCSAD_Macroblock_C(orgL, 777 encvid->pred_i16[currMB->i16Mode], dmin_lx, NULL); 778 } 779 else /* i4 */ 780 { 781 rateCtrl->MADofMB[video->mbNum] = encvid->i4_sad / 256.; 782 } 783 } 784 /* for INTER, we have already saved it with the MV search */ 785 } 786 787 return ; 788 } 789 790 791 792 AVCEnc_Status RCUpdateFrame(AVCEncObject *encvid) 793 { 794 AVCCommonObj *video = encvid->common; 795 AVCRateControl *rateCtrl = encvid->rateCtrl; 796 AVCEnc_Status status = AVCENC_SUCCESS; 797 MultiPass *pMP = rateCtrl->pMP; 798 int diff_BTCounter; 799 int nal_type = video->nal_unit_type; 800 801 /* update the complexity weight of I, P, B frame */ 802 803 if (rateCtrl->rcEnable == TRUE) 804 { 805 pMP->actual_bits = rateCtrl->numFrameBits; 806 pMP->mad = (OsclFloat)rateCtrl->totalSAD / video->PicSizeInMbs; //ComputeFrameMAD(video, rateCtrl); 807 808 AVCSaveRDSamples(pMP, 0); 809 810 pMP->encoded_frames++; 811 812 /* for pMP->samplesPerFrame */ 813 pMP->samplesPerFrame[pMP->framePos] = 0; 814 815 pMP->sum_QP += pMP->QP; 816 817 /* update pMP->counter_BTsrc, pMP->counter_BTdst */ 818 /* re-allocate the target bit again and then stop encoding */ 819 diff_BTCounter = (int)((OsclFloat)(rateCtrl->TMN_TH - rateCtrl->TMN_W - pMP->actual_bits) / 820 (pMP->bitrate / (pMP->framerate + 0.0001) + 0.0001) / 0.1); 821 if (diff_BTCounter >= 0) 822 pMP->counter_BTsrc += diff_BTCounter; /* pMP->actual_bits is smaller */ 823 else 824 pMP->counter_BTdst -= diff_BTCounter; /* pMP->actual_bits is bigger */ 825 826 rateCtrl->TMN_TH -= (int)((OsclFloat)pMP->bitrate / (pMP->framerate + 0.0001) * (diff_BTCounter * 0.1)); 827 rateCtrl->T = pMP->target_bits = rateCtrl->TMN_TH - rateCtrl->TMN_W; 828 pMP->diff_counter -= diff_BTCounter; 829 830 rateCtrl->Rc = rateCtrl->numFrameBits; /* Total Bits for current frame */ 831 rateCtrl->Hc = rateCtrl->NumberofHeaderBits; /* Total Bits in Header and Motion Vector */ 832 833 /* BX_RC */ 834 updateRateControl(rateCtrl, nal_type); 835 if (rateCtrl->skip_next_frame == -1) // skip current frame 836 { 837 status = AVCENC_SKIPPED_PICTURE; 838 } 839 } 840 841 rateCtrl->first_frame = 0; // reset here after we encode the first frame. 842 843 return status; 844 } 845 846 void AVCSaveRDSamples(MultiPass *pMP, int counter_samples) 847 { 848 /* for pMP->pRDSamples */ 849 pMP->pRDSamples[pMP->framePos][counter_samples].QP = pMP->QP; 850 pMP->pRDSamples[pMP->framePos][counter_samples].actual_bits = pMP->actual_bits; 851 pMP->pRDSamples[pMP->framePos][counter_samples].mad = pMP->mad; 852 pMP->pRDSamples[pMP->framePos][counter_samples].R_D = (OsclFloat)pMP->actual_bits / (pMP->mad + 0.0001); 853 854 return ; 855 } 856 857 void updateRateControl(AVCRateControl *rateCtrl, int nal_type) 858 { 859 int frame_bits; 860 MultiPass *pMP = rateCtrl->pMP; 861 862 /* BX rate contro\l */ 863 frame_bits = (int)(rateCtrl->bitRate / rateCtrl->frame_rate); 864 rateCtrl->TMN_W += (rateCtrl->Rc - rateCtrl->TMN_TH); 865 rateCtrl->VBV_fullness += (rateCtrl->Rc - frame_bits); //rateCtrl->Rp); 866 //if(rateCtrl->VBV_fullness < 0) rateCtrl->VBV_fullness = -1; 867 868 rateCtrl->encoded_frames++; 869 870 /* frame dropping */ 871 rateCtrl->skip_next_frame = 0; 872 873 if ((rateCtrl->VBV_fullness > rateCtrl->Bs / 2) && nal_type != AVC_NALTYPE_IDR) /* skip the current frame */ /* rateCtrl->Bs */ 874 { 875 rateCtrl->TMN_W -= (rateCtrl->Rc - rateCtrl->TMN_TH); 876 rateCtrl->VBV_fullness -= rateCtrl->Rc; 877 rateCtrl->skip_next_frame = -1; 878 } 879 else if ((OsclFloat)(rateCtrl->VBV_fullness - rateCtrl->VBV_fullness_offset) > (rateCtrl->Bs / 2 - rateCtrl->VBV_fullness_offset)*0.95) /* skip next frame */ 880 { 881 rateCtrl->VBV_fullness -= frame_bits; //rateCtrl->Rp; 882 rateCtrl->skip_next_frame = 1; 883 pMP->counter_BTsrc -= (int)((OsclFloat)(rateCtrl->Bs / 2 - rateCtrl->low_bound) / 2.0 / (pMP->target_bits_per_frame / 10)); 884 /* BX_1, skip more than 1 frames */ 885 //while(rateCtrl->VBV_fullness > rateCtrl->Bs*0.475) 886 while ((rateCtrl->VBV_fullness - rateCtrl->VBV_fullness_offset) > (rateCtrl->Bs / 2 - rateCtrl->VBV_fullness_offset)*0.95) 887 { 888 rateCtrl->VBV_fullness -= frame_bits; //rateCtrl->Rp; 889 rateCtrl->skip_next_frame++; 890 pMP->counter_BTsrc -= (int)((OsclFloat)(rateCtrl->Bs / 2 - rateCtrl->low_bound) / 2.0 / (pMP->target_bits_per_frame / 10)); 891 } 892 893 /* END BX_1 */ 894 } 895 } 896 897 898 double ComputeFrameMAD(AVCCommonObj *video, AVCRateControl *rateCtrl) 899 { 900 double TotalMAD; 901 int i; 902 TotalMAD = 0.0; 903 for (i = 0; i < (int)video->PicSizeInMbs; i++) 904 TotalMAD += rateCtrl->MADofMB[i]; 905 TotalMAD /= video->PicSizeInMbs; 906 return TotalMAD; 907 } 908 909 910 911 912 913 /* convert from QP to Qstep */ 914 double QP2Qstep(int QP) 915 { 916 int i; 917 double Qstep; 918 static const double QP2QSTEP[6] = { 0.625, 0.6875, 0.8125, 0.875, 1.0, 1.125 }; 919 920 Qstep = QP2QSTEP[QP % 6]; 921 for (i = 0; i < (QP / 6); i++) 922 Qstep *= 2; 923 924 return Qstep; 925 } 926 927 /* convert from step size to QP */ 928 int Qstep2QP(double Qstep) 929 { 930 int q_per = 0, q_rem = 0; 931 932 // assert( Qstep >= QP2Qstep(0) && Qstep <= QP2Qstep(51) ); 933 if (Qstep < QP2Qstep(0)) 934 return 0; 935 else if (Qstep > QP2Qstep(51)) 936 return 51; 937 938 while (Qstep > QP2Qstep(5)) 939 { 940 Qstep /= 2; 941 q_per += 1; 942 } 943 944 if (Qstep <= (0.625 + 0.6875) / 2) 945 { 946 Qstep = 0.625; 947 q_rem = 0; 948 } 949 else if (Qstep <= (0.6875 + 0.8125) / 2) 950 { 951 Qstep = 0.6875; 952 q_rem = 1; 953 } 954 else if (Qstep <= (0.8125 + 0.875) / 2) 955 { 956 Qstep = 0.8125; 957 q_rem = 2; 958 } 959 else if (Qstep <= (0.875 + 1.0) / 2) 960 { 961 Qstep = 0.875; 962 q_rem = 3; 963 } 964 else if (Qstep <= (1.0 + 1.125) / 2) 965 { 966 Qstep = 1.0; 967 q_rem = 4; 968 } 969 else 970 { 971 Qstep = 1.125; 972 q_rem = 5; 973 } 974 975 return (q_per * 6 + q_rem); 976 } 977 978 979 980