1 /* ------------------------------------------------------------------
2 * Copyright (C) 1998-2009 PacketVideo
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
8 * http://www.apache.org/licenses/LICENSE-2.0
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
14 * See the License for the specific language governing permissions
15 * and limitations under the License.
16 * -------------------------------------------------------------------
18 #include "avcenc_lib.h"
21 /* rate control variables */
22 #define RC_MAX_QUANT 51
23 #define RC_MIN_QUANT 0 //cap to 10 to prevent rate fluctuation
25 #define MAD_MIN 1 /* handle the case of devision by zero in RC */
29 double QP2Qstep(int QP);
30 int Qstep2QP(double Qstep);
32 double ComputeFrameMAD(AVCCommonObj *video, AVCRateControl *rateCtrl);
34 void targetBitCalculation(AVCEncObject *encvid, AVCCommonObj *video, AVCRateControl *rateCtrl, MultiPass *pMP);
36 void calculateQuantizer_Multipass(AVCEncObject *encvid, AVCCommonObj *video,
37 AVCRateControl *rateCtrl, MultiPass *pMP);
39 void updateRC_PostProc(AVCRateControl *rateCtrl, MultiPass *pMP);
41 void AVCSaveRDSamples(MultiPass *pMP, int counter_samples);
43 void updateRateControl(AVCRateControl *rateControl, int nal_type);
45 int GetAvgFrameQP(AVCRateControl *rateCtrl)
50 AVCEnc_Status RCDetermineFrameNum(AVCEncObject *encvid, AVCRateControl *rateCtrl, uint32 modTime, uint *frameNum)
52 AVCCommonObj *video = encvid->common;
53 AVCSliceHeader *sliceHdr = video->sliceHdr;
54 uint32 modTimeRef = encvid->modTimeRef;
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 /**********************************/
63 /* for now, the default is to encode every frame, To Be Changed */
64 if (rateCtrl->first_frame)
66 encvid->modTimeRef = modTime;
67 encvid->wrapModTime = 0;
68 encvid->prevFrameNum = 0;
69 encvid->prevProcFrameNum = 0;
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;
78 return AVCENC_SUCCESS;
82 if (modTime < modTimeRef) /* modTime wrapped around */
84 encvid->wrapModTime += ((uint32)0xFFFFFFFF - modTimeRef) + 1;
85 encvid->modTimeRef = modTimeRef = 0;
87 modTime += encvid->wrapModTime; /* wrapModTime is non zero after wrap-around */
89 currFrameNum = (int32)(((modTime - modTimeRef) * rateCtrl->frame_rate + 200) / 1000); /* add small roundings */
91 if (currFrameNum <= (int32)encvid->prevProcFrameNum)
93 return AVCENC_FAIL; /* this is a late frame do not encode it */
96 frameInc = currFrameNum - encvid->prevProcFrameNum;
98 if (frameInc < rateCtrl->skip_next_frame + 1)
100 return AVCENC_FAIL; /* frame skip required to maintain the target bit rate. */
103 RCUpdateBuffer(video, rateCtrl, frameInc - rateCtrl->skip_next_frame); /* in case more frames dropped */
105 *frameNum = currFrameNum;
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*/
110 /* set frame type to IDR-frame */
111 if (rateCtrl->idrPeriod)
113 encvid->modTimeRef += (uint32)(rateCtrl->idrPeriod * 1000 / rateCtrl->frame_rate);
114 *frameNum -= rateCtrl->idrPeriod;
118 encvid->modTimeRef += (uint32)(video->MaxFrameNum * 1000 / rateCtrl->frame_rate);
119 *frameNum -= video->MaxFrameNum;
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;
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;
137 return AVCENC_SUCCESS;
140 void RCUpdateBuffer(AVCCommonObj *video, AVCRateControl *rateCtrl, int frameInc)
143 MultiPass *pMP = rateCtrl->pMP;
145 OSCL_UNUSED_ARG(video);
147 if (rateCtrl->rcEnable == TRUE)
151 tmp = rateCtrl->bitsPerFrame * (frameInc - 1);
152 rateCtrl->VBV_fullness -= tmp;
153 pMP->counter_BTsrc += 10 * (frameInc - 1);
155 /* Check buffer underflow */
156 if (rateCtrl->VBV_fullness < rateCtrl->low_bound)
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));
167 AVCEnc_Status InitRateControlModule(AVCHandle *avcHandle)
169 AVCEncObject *encvid = (AVCEncObject*) avcHandle->AVCObject;
170 AVCCommonObj *video = encvid->common;
171 AVCRateControl *rateCtrl = encvid->rateCtrl;
172 double L1, L2, L3, bpp;
176 rateCtrl->basicUnit = video->PicSizeInMbs;
178 rateCtrl->MADofMB = (double*) avcHandle->CBAVC_Malloc(encvid->avcHandle->userData,
179 video->PicSizeInMbs * sizeof(double), DEFAULT_ATTR);
181 if (!rateCtrl->MADofMB)
186 if (rateCtrl->rcEnable == TRUE)
188 rateCtrl->pMP = (MultiPass*) avcHandle->CBAVC_Malloc(encvid->avcHandle->userData, sizeof(MultiPass), DEFAULT_ATTR);
193 memset(rateCtrl->pMP, 0, sizeof(MultiPass));
194 rateCtrl->pMP->encoded_frames = -1; /* forget about the very first I frame */
196 /* RDInfo **pRDSamples */
197 rateCtrl->pMP->pRDSamples = (RDInfo **)avcHandle->CBAVC_Malloc(encvid->avcHandle->userData, (30 * sizeof(RDInfo *)), DEFAULT_ATTR);
198 if (!rateCtrl->pMP->pRDSamples)
203 for (i = 0; i < 30; i++)
205 rateCtrl->pMP->pRDSamples[i] = (RDInfo *)avcHandle->CBAVC_Malloc(encvid->avcHandle->userData, (32 * sizeof(RDInfo)), DEFAULT_ATTR);
206 if (!rateCtrl->pMP->pRDSamples[i])
210 for (j = 0; j < 32; j++) memset(&(rateCtrl->pMP->pRDSamples[i][j]), 0, sizeof(RDInfo));
212 rateCtrl->pMP->frameRange = (int)(rateCtrl->frame_rate * 1.0); /* 1.0s time frame*/
213 rateCtrl->pMP->frameRange = AVC_MAX(rateCtrl->pMP->frameRange, 5);
214 rateCtrl->pMP->frameRange = AVC_MIN(rateCtrl->pMP->frameRange, 30);
216 rateCtrl->pMP->framePos = -1;
219 rateCtrl->bitsPerFrame = (int32)(rateCtrl->bitRate / rateCtrl->frame_rate);
221 /* BX rate control */
222 rateCtrl->skip_next_frame = 0; /* must be initialized */
224 rateCtrl->Bs = rateCtrl->cpbSize;
226 rateCtrl->VBV_fullness = (int)(rateCtrl->Bs * 0.5); /* rateCtrl->Bs */
227 rateCtrl->encoded_frames = 0;
229 rateCtrl->TMN_TH = rateCtrl->bitsPerFrame;
231 rateCtrl->max_BitVariance_num = (int)((OsclFloat)(rateCtrl->Bs - rateCtrl->VBV_fullness) / (rateCtrl->bitsPerFrame / 10.0)) - 5;
232 if (rateCtrl->max_BitVariance_num < 0) rateCtrl->max_BitVariance_num += 5;
234 // Set the initial buffer fullness
235 /* According to the spec, the initial buffer fullness needs to be set to 1/3 */
236 rateCtrl->VBV_fullness = (int)(rateCtrl->Bs / 3.0 - rateCtrl->Bs / 2.0); /* the buffer range is [-Bs/2, Bs/2] */
237 rateCtrl->pMP->counter_BTsrc = (int)((rateCtrl->Bs / 2.0 - rateCtrl->Bs / 3.0) / (rateCtrl->bitsPerFrame / 10.0));
238 rateCtrl->TMN_W = (int)(rateCtrl->VBV_fullness + rateCtrl->pMP->counter_BTsrc * (rateCtrl->bitsPerFrame / 10.0));
240 rateCtrl->low_bound = -rateCtrl->Bs / 2;
241 rateCtrl->VBV_fullness_offset = 0;
243 /* Setting the bitrate and framerate */
244 rateCtrl->pMP->bitrate = rateCtrl->bitRate;
245 rateCtrl->pMP->framerate = rateCtrl->frame_rate;
246 rateCtrl->pMP->target_bits_per_frame = rateCtrl->pMP->bitrate / rateCtrl->pMP->framerate;
248 /*compute the initial QP*/
249 bpp = 1.0 * rateCtrl->bitRate / (rateCtrl->frame_rate * (video->PicSizeInMbs << 8));
250 if (video->PicWidthInSamplesL == 176)
256 else if (video->PicWidthInSamplesL == 352)
269 if (rateCtrl->initQP == 0)
279 rateCtrl->initQP = qp;
282 rateCtrl->Qc = rateCtrl->initQP;
285 return AVCENC_SUCCESS;
289 CleanupRateControlModule(avcHandle);
290 return AVCENC_MEMORY_FAIL;
295 void CleanupRateControlModule(AVCHandle *avcHandle)
297 AVCEncObject *encvid = (AVCEncObject*) avcHandle->AVCObject;
298 AVCRateControl *rateCtrl = encvid->rateCtrl;
301 if (rateCtrl->MADofMB)
303 avcHandle->CBAVC_Free(avcHandle->userData, (int)(rateCtrl->MADofMB));
308 if (rateCtrl->pMP->pRDSamples)
310 for (i = 0; i < 30; i++)
312 if (rateCtrl->pMP->pRDSamples[i])
314 avcHandle->CBAVC_Free(avcHandle->userData, (int)rateCtrl->pMP->pRDSamples[i]);
317 avcHandle->CBAVC_Free(avcHandle->userData, (int)rateCtrl->pMP->pRDSamples);
319 avcHandle->CBAVC_Free(avcHandle->userData, (int)(rateCtrl->pMP));
325 void RCInitGOP(AVCEncObject *encvid)
327 /* in BX RC, there's no GOP-level RC */
329 OSCL_UNUSED_ARG(encvid);
335 void RCInitFrameQP(AVCEncObject *encvid)
337 AVCCommonObj *video = encvid->common;
338 AVCRateControl *rateCtrl = encvid->rateCtrl;
339 AVCPicParamSet *picParam = video->currPicParams;
340 MultiPass *pMP = rateCtrl->pMP;
342 if (rateCtrl->rcEnable == TRUE)
344 /* frame layer rate control */
345 if (rateCtrl->encoded_frames == 0)
347 video->QPy = rateCtrl->Qc = rateCtrl->initQP;
351 calculateQuantizer_Multipass(encvid, video, rateCtrl, pMP);
352 video->QPy = rateCtrl->Qc;
355 rateCtrl->NumberofHeaderBits = 0;
356 rateCtrl->NumberofTextureBits = 0;
357 rateCtrl->numFrameBits = 0; // reset
359 /* update pMP->framePos */
360 if (++pMP->framePos == pMP->frameRange) pMP->framePos = 0;
362 if (rateCtrl->T == 0)
364 pMP->counter_BTdst = (int)(rateCtrl->frame_rate * 7.5 + 0.5); /* 0.75s time frame */
365 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 */
366 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 */
367 pMP->counter_BTdst = AVC_MIN(pMP->counter_BTdst, 20); /* Limit the target to be smaller than 3C */
369 pMP->target_bits = rateCtrl->T = rateCtrl->TMN_TH = (int)(rateCtrl->TMN_TH * (1.0 + pMP->counter_BTdst * 0.1));
370 pMP->diff_counter = pMP->counter_BTdst;
373 /* collect the necessary data: target bits, actual bits, mad and QP */
374 pMP->target_bits = rateCtrl->T;
375 pMP->QP = video->QPy;
377 pMP->mad = (OsclFloat)rateCtrl->totalSAD / video->PicSizeInMbs; //ComputeFrameMAD(video, rateCtrl);
378 if (pMP->mad < MAD_MIN) pMP->mad = MAD_MIN; /* MAD_MIN is defined as 1 in mp4def.h */
380 pMP->bitrate = rateCtrl->bitRate; /* calculated in RCVopQPSetting */
381 pMP->framerate = rateCtrl->frame_rate;
383 /* first pass encoding */
384 pMP->nRe_Quantized = 0;
389 video->QPy = rateCtrl->initQP;
392 // printf(" %d ",video->QPy);
394 if (video->CurrPicNum == 0 && encvid->outOfBandParamSet == FALSE)
396 picParam->pic_init_qs_minus26 = 0;
397 picParam->pic_init_qp_minus26 = video->QPy - 26;
400 // need this for motion estimation
401 encvid->lambda_mode = QP2QUANT[AVC_MAX(0, video->QPy-SHIFT_QP)];
402 encvid->lambda_motion = LAMBDA_FACTOR(encvid->lambda_mode);
406 /* Mad based variable bit allocation + QP calculation with a new quadratic method */
407 void calculateQuantizer_Multipass(AVCEncObject *encvid, AVCCommonObj *video,
408 AVCRateControl *rateCtrl, MultiPass *pMP)
410 int prev_actual_bits = 0, curr_target, /*pos=0,*/i, j;
411 OsclFloat Qstep, prev_QP = 0.625;
413 OsclFloat curr_mad, prev_mad, curr_RD, prev_RD, average_mad, aver_QP;
415 /* Mad based variable bit allocation */
416 targetBitCalculation(encvid, video, rateCtrl, pMP);
418 if (rateCtrl->T <= 0 || rateCtrl->totalSAD == 0)
420 if (rateCtrl->T < 0) rateCtrl->Qc = RC_MAX_QUANT;
424 /* ---------------------------------------------------------------------------------------------------*/
425 /* current frame QP estimation */
426 curr_target = rateCtrl->T;
427 curr_mad = (OsclFloat)rateCtrl->totalSAD / video->PicSizeInMbs;
428 if (curr_mad < MAD_MIN) curr_mad = MAD_MIN; /* MAD_MIN is defined as 1 in mp4def.h */
429 curr_RD = (OsclFloat)curr_target / curr_mad;
431 if (rateCtrl->skip_next_frame == -1) // previous was skipped
434 prev_mad = pMP->pRDSamples[i][0].mad;
435 prev_QP = pMP->pRDSamples[i][0].QP;
436 prev_actual_bits = pMP->pRDSamples[i][0].actual_bits;
440 /* Another version of search the optimal point */
443 while (i < pMP->frameRange && prev_mad < 0.001) /* find first one with nonzero prev_mad */
445 prev_mad = pMP->pRDSamples[i][0].mad;
449 if (i < pMP->frameRange)
451 prev_actual_bits = pMP->pRDSamples[i-1][0].actual_bits;
453 for (j = 0; i < pMP->frameRange; i++)
455 if (pMP->pRDSamples[i][0].mad != 0 &&
456 AVC_ABS(prev_mad - curr_mad) > AVC_ABS(pMP->pRDSamples[i][0].mad - curr_mad))
458 prev_mad = pMP->pRDSamples[i][0].mad;
459 prev_actual_bits = pMP->pRDSamples[i][0].actual_bits;
463 prev_QP = QP2Qstep(pMP->pRDSamples[j][0].QP);
465 for (i = 1; i < pMP->samplesPerFrame[j]; i++)
467 if (AVC_ABS(prev_actual_bits - curr_target) > AVC_ABS(pMP->pRDSamples[j][i].actual_bits - curr_target))
469 prev_actual_bits = pMP->pRDSamples[j][i].actual_bits;
470 prev_QP = QP2Qstep(pMP->pRDSamples[j][i].QP);
476 // quadratic approximation
477 if (prev_mad > 0.001) // only when prev_mad is greater than 0, otherwise keep using the same QP
479 prev_RD = (OsclFloat)prev_actual_bits / prev_mad;
480 //rateCtrl->Qc = (Int)(prev_QP * sqrt(prev_actual_bits/curr_target) + 0.4);
481 if (prev_QP == 0.625) // added this to allow getting out of QP = 0 easily
483 Qstep = (int)(prev_RD / curr_RD + 0.5);
487 // rateCtrl->Qc =(Int)(prev_QP * M4VENC_SQRT(prev_RD/curr_RD) + 0.9);
489 if (prev_RD / curr_RD > 0.5 && prev_RD / curr_RD < 2.0)
490 Qstep = (int)(prev_QP * (sqrt(prev_RD / curr_RD) + prev_RD / curr_RD) / 2.0 + 0.9); /* Quadratic and linear approximation */
492 Qstep = (int)(prev_QP * (sqrt(prev_RD / curr_RD) + pow(prev_RD / curr_RD, 1.0 / 3.0)) / 2.0 + 0.9);
494 // lower bound on Qc should be a function of curr_mad
495 // When mad is already low, lower bound on Qc doesn't have to be small.
496 // Note, this doesn't work well for low complexity clip encoded at high bit rate
497 // it doesn't hit the target bit rate due to this QP lower bound.
498 /// if((curr_mad < 8) && (rateCtrl->Qc < 12)) rateCtrl->Qc = 12;
499 // else if((curr_mad < 128) && (rateCtrl->Qc < 3)) rateCtrl->Qc = 3;
501 rateCtrl->Qc = Qstep2QP(Qstep);
503 if (rateCtrl->Qc < RC_MIN_QUANT) rateCtrl->Qc = RC_MIN_QUANT;
504 if (rateCtrl->Qc > RC_MAX_QUANT) rateCtrl->Qc = RC_MAX_QUANT;
507 /* active bit resource protection */
508 aver_QP = (pMP->encoded_frames == 0 ? 0 : pMP->sum_QP / (OsclFloat)pMP->encoded_frames);
509 average_mad = (pMP->encoded_frames == 0 ? 0 : pMP->sum_mad / (OsclFloat)pMP->encoded_frames); /* this function is called from the scond encoded frame*/
510 if (pMP->diff_counter == 0 &&
511 ((OsclFloat)rateCtrl->Qc <= aver_QP*1.1 || curr_mad <= average_mad*1.1) &&
512 pMP->counter_BTsrc <= (pMP->counter_BTdst + (int)(pMP->framerate*1.0 + 0.5)))
514 rateCtrl->TMN_TH -= (int)(pMP->target_bits_per_frame / 10.0);
515 rateCtrl->T = rateCtrl->TMN_TH - rateCtrl->TMN_W;
516 pMP->counter_BTsrc++;
522 void targetBitCalculation(AVCEncObject *encvid, AVCCommonObj *video, AVCRateControl *rateCtrl, MultiPass *pMP)
524 OSCL_UNUSED_ARG(encvid);
525 OsclFloat curr_mad;//, average_mad;
526 int diff_counter_BTsrc, diff_counter_BTdst, prev_counter_diff, curr_counter_diff, bound;
527 /* BT = Bit Transfer, for pMP->counter_BTsrc, pMP->counter_BTdst */
529 /* some stuff about frame dropping remained here to be done because pMP cannot be inserted into updateRateControl()*/
530 updateRC_PostProc(rateCtrl, pMP);
532 /* update pMP->counter_BTsrc and pMP->counter_BTdst to avoid interger overflow */
533 if (pMP->counter_BTsrc > 1000 && pMP->counter_BTdst > 1000)
535 pMP->counter_BTsrc -= 1000;
536 pMP->counter_BTdst -= 1000;
539 /* ---------------------------------------------------------------------------------------------------*/
540 /* target calculation */
541 curr_mad = (OsclFloat)rateCtrl->totalSAD / video->PicSizeInMbs;
542 if (curr_mad < MAD_MIN) curr_mad = MAD_MIN; /* MAD_MIN is defined as 1 in mp4def.h */
543 diff_counter_BTsrc = diff_counter_BTdst = 0;
544 pMP->diff_counter = 0;
547 /*1.calculate average mad */
548 pMP->sum_mad += curr_mad;
549 //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*/
550 //pMP->aver_mad = average_mad;
551 if (pMP->encoded_frames >= 0) /* pMP->encoded_frames is set to -1 initially, so forget about the very first I frame */
552 pMP->aver_mad = (pMP->aver_mad * pMP->encoded_frames + curr_mad) / (pMP->encoded_frames + 1);
554 if (pMP->overlapped_win_size > 0 && pMP->encoded_frames_prev >= 0)
555 pMP->aver_mad_prev = (pMP->aver_mad_prev * pMP->encoded_frames_prev + curr_mad) / (pMP->encoded_frames_prev + 1);
557 /*2.average_mad, mad ==> diff_counter_BTsrc, diff_counter_BTdst */
558 if (pMP->overlapped_win_size == 0)
560 /* original verison */
561 if (curr_mad > pMP->aver_mad*1.1)
563 if (curr_mad / (pMP->aver_mad + 0.0001) > 2)
564 diff_counter_BTdst = (int)(sqrt(curr_mad / (pMP->aver_mad + 0.0001)) * 10 + 0.4) - 10;
565 //diff_counter_BTdst = (int)((sqrt(curr_mad/pMP->aver_mad)*2+curr_mad/pMP->aver_mad)/(3*0.1) + 0.4) - 10;
567 diff_counter_BTdst = (int)(curr_mad / (pMP->aver_mad + 0.0001) * 10 + 0.4) - 10;
569 else /* curr_mad <= average_mad*1.1 */
570 //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);
571 diff_counter_BTsrc = 10 - (int)(sqrt(curr_mad / (pMP->aver_mad + 0.0001)) * 10 + 0.5);
573 /* actively fill in the possible gap */
574 if (diff_counter_BTsrc == 0 && diff_counter_BTdst == 0 &&
575 curr_mad <= pMP->aver_mad*1.1 && pMP->counter_BTsrc < pMP->counter_BTdst)
576 diff_counter_BTsrc = 1;
579 else if (pMP->overlapped_win_size > 0)
581 /* transition time: use previous average mad "pMP->aver_mad_prev" instead of the current average mad "pMP->aver_mad" */
582 if (curr_mad > pMP->aver_mad_prev*1.1)
584 if (curr_mad / pMP->aver_mad_prev > 2)
585 diff_counter_BTdst = (int)(sqrt(curr_mad / (pMP->aver_mad_prev + 0.0001)) * 10 + 0.4) - 10;
586 //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;
588 diff_counter_BTdst = (int)(curr_mad / (pMP->aver_mad_prev + 0.0001) * 10 + 0.4) - 10;
590 else /* curr_mad <= average_mad*1.1 */
591 //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);
592 diff_counter_BTsrc = 10 - (int)(sqrt(curr_mad / (pMP->aver_mad_prev + 0.0001)) * 10 + 0.5);
594 /* actively fill in the possible gap */
595 if (diff_counter_BTsrc == 0 && diff_counter_BTdst == 0 &&
596 curr_mad <= pMP->aver_mad_prev*1.1 && pMP->counter_BTsrc < pMP->counter_BTdst)
597 diff_counter_BTsrc = 1;
599 if (--pMP->overlapped_win_size <= 0) pMP->overlapped_win_size = 0;
603 /* if difference is too much, do clipping */
604 /* First, set the upper bound for current bit allocation variance: 80% of available buffer */
605 bound = (int)((rateCtrl->Bs / 2 - rateCtrl->VBV_fullness) * 0.6 / (pMP->target_bits_per_frame / 10)); /* rateCtrl->Bs */
606 diff_counter_BTsrc = AVC_MIN(diff_counter_BTsrc, bound);
607 diff_counter_BTdst = AVC_MIN(diff_counter_BTdst, bound);
609 /* Second, set another upper bound for current bit allocation: 4-5*bitrate/framerate */
611 // if(video->encParams->RC_Type == CBR_LOWDELAY)
612 // not necessary bound = 10; -- For Low delay */
614 diff_counter_BTsrc = AVC_MIN(diff_counter_BTsrc, bound);
615 diff_counter_BTdst = AVC_MIN(diff_counter_BTdst, bound);
618 /* Third, check the buffer */
619 prev_counter_diff = pMP->counter_BTdst - pMP->counter_BTsrc;
620 curr_counter_diff = prev_counter_diff + (diff_counter_BTdst - diff_counter_BTsrc);
622 if (AVC_ABS(prev_counter_diff) >= rateCtrl->max_BitVariance_num || AVC_ABS(curr_counter_diff) >= rateCtrl->max_BitVariance_num)
623 { //diff_counter_BTsrc = diff_counter_BTdst = 0;
625 if (curr_counter_diff > rateCtrl->max_BitVariance_num && diff_counter_BTdst)
627 diff_counter_BTdst = (rateCtrl->max_BitVariance_num - prev_counter_diff) + diff_counter_BTsrc;
628 if (diff_counter_BTdst < 0) diff_counter_BTdst = 0;
631 else if (curr_counter_diff < -rateCtrl->max_BitVariance_num && diff_counter_BTsrc)
633 diff_counter_BTsrc = diff_counter_BTdst - (-rateCtrl->max_BitVariance_num - prev_counter_diff);
634 if (diff_counter_BTsrc < 0) diff_counter_BTsrc = 0;
639 /*3.diff_counter_BTsrc, diff_counter_BTdst ==> TMN_TH */
640 rateCtrl->TMN_TH = (int)(pMP->target_bits_per_frame);
641 pMP->diff_counter = 0;
643 if (diff_counter_BTsrc)
645 rateCtrl->TMN_TH -= (int)(pMP->target_bits_per_frame * diff_counter_BTsrc * 0.1);
646 pMP->diff_counter = -diff_counter_BTsrc;
648 else if (diff_counter_BTdst)
650 rateCtrl->TMN_TH += (int)(pMP->target_bits_per_frame * diff_counter_BTdst * 0.1);
651 pMP->diff_counter = diff_counter_BTdst;
655 /*4.update pMP->counter_BTsrc, pMP->counter_BTdst */
656 pMP->counter_BTsrc += diff_counter_BTsrc;
657 pMP->counter_BTdst += diff_counter_BTdst;
660 /*5.target bit calculation */
661 rateCtrl->T = rateCtrl->TMN_TH - rateCtrl->TMN_W;
666 void updateRC_PostProc(AVCRateControl *rateCtrl, MultiPass *pMP)
668 if (rateCtrl->skip_next_frame > 0) /* skip next frame */
670 pMP->counter_BTsrc += 10 * rateCtrl->skip_next_frame;
673 else if (rateCtrl->skip_next_frame == -1) /* skip current frame */
675 pMP->counter_BTdst -= pMP->diff_counter;
676 pMP->counter_BTsrc += 10;
678 pMP->sum_mad -= pMP->mad;
679 pMP->aver_mad = (pMP->aver_mad * pMP->encoded_frames - pMP->mad) / (pMP->encoded_frames - 1 + 0.0001);
680 pMP->sum_QP -= pMP->QP;
681 pMP->encoded_frames --;
683 /* some stuff in update VBV_fullness remains here */
684 //if(rateCtrl->VBV_fullness < -rateCtrl->Bs/2) /* rateCtrl->Bs */
685 if (rateCtrl->VBV_fullness < rateCtrl->low_bound)
687 rateCtrl->VBV_fullness = rateCtrl->low_bound; // -rateCtrl->Bs/2;
688 rateCtrl->TMN_W = rateCtrl->VBV_fullness - rateCtrl->low_bound;
689 pMP->counter_BTsrc = pMP->counter_BTdst + (int)((OsclFloat)(rateCtrl->Bs / 2 - rateCtrl->low_bound) / 2.0 / (pMP->target_bits_per_frame / 10));
694 void RCInitChromaQP(AVCEncObject *encvid)
696 AVCCommonObj *video = encvid->common;
697 AVCMacroblock *currMB = video->currMB;
700 /* we have to do the same thing for AVC_CLIP3(0,51,video->QSy) */
702 video->QPy_div_6 = (currMB->QPy * 43) >> 8;
703 video->QPy_mod_6 = currMB->QPy - 6 * video->QPy_div_6;
704 currMB->QPc = video->QPc = mapQPi2QPc[AVC_CLIP3(0, 51, currMB->QPy + video->currPicParams->chroma_qp_index_offset)];
705 video->QPc_div_6 = (video->QPc * 43) >> 8;
706 video->QPc_mod_6 = video->QPc - 6 * video->QPc_div_6;
708 /* pre-calculate this to save computation */
709 q_bits = 4 + video->QPy_div_6;
710 if (video->slice_type == AVC_I_SLICE)
712 encvid->qp_const = 682 << q_bits; // intra
716 encvid->qp_const = 342 << q_bits; // inter
719 q_bits = 4 + video->QPc_div_6;
720 if (video->slice_type == AVC_I_SLICE)
722 encvid->qp_const_c = 682 << q_bits; // intra
726 encvid->qp_const_c = 342 << q_bits; // inter
729 encvid->lambda_mode = QP2QUANT[AVC_MAX(0, currMB->QPy-SHIFT_QP)];
730 encvid->lambda_motion = LAMBDA_FACTOR(encvid->lambda_mode);
736 void RCInitMBQP(AVCEncObject *encvid)
738 AVCCommonObj *video = encvid->common;
739 AVCMacroblock *currMB = video->currMB;
741 currMB->QPy = video->QPy; /* set to previous value or picture level */
743 RCInitChromaQP(encvid);
747 void RCPostMB(AVCCommonObj *video, AVCRateControl *rateCtrl, int num_header_bits, int num_texture_bits)
749 OSCL_UNUSED_ARG(video);
750 rateCtrl->numMBHeaderBits = num_header_bits;
751 rateCtrl->numMBTextureBits = num_texture_bits;
752 rateCtrl->NumberofHeaderBits += rateCtrl->numMBHeaderBits;
753 rateCtrl->NumberofTextureBits += rateCtrl->numMBTextureBits;
756 void RCRestoreQP(AVCMacroblock *currMB, AVCCommonObj *video, AVCEncObject *encvid)
758 currMB->QPy = video->QPy; /* use previous QP */
759 RCInitChromaQP(encvid);
765 void RCCalculateMAD(AVCEncObject *encvid, AVCMacroblock *currMB, uint8 *orgL, int orgPitch)
767 AVCCommonObj *video = encvid->common;
768 AVCRateControl *rateCtrl = encvid->rateCtrl;
771 if (rateCtrl->rcEnable == TRUE)
773 if (currMB->mb_intra)
775 if (currMB->mbMode == AVC_I16)
777 dmin_lx = (0xFFFF << 16) | orgPitch;
778 rateCtrl->MADofMB[video->mbNum] = AVCSAD_Macroblock_C(orgL,
779 encvid->pred_i16[currMB->i16Mode], dmin_lx, NULL);
783 rateCtrl->MADofMB[video->mbNum] = encvid->i4_sad / 256.;
786 /* for INTER, we have already saved it with the MV search */
794 AVCEnc_Status RCUpdateFrame(AVCEncObject *encvid)
796 AVCCommonObj *video = encvid->common;
797 AVCRateControl *rateCtrl = encvid->rateCtrl;
798 AVCEnc_Status status = AVCENC_SUCCESS;
799 MultiPass *pMP = rateCtrl->pMP;
801 int nal_type = video->nal_unit_type;
803 /* update the complexity weight of I, P, B frame */
805 if (rateCtrl->rcEnable == TRUE)
807 pMP->actual_bits = rateCtrl->numFrameBits;
808 pMP->mad = (OsclFloat)rateCtrl->totalSAD / video->PicSizeInMbs; //ComputeFrameMAD(video, rateCtrl);
810 AVCSaveRDSamples(pMP, 0);
812 pMP->encoded_frames++;
814 /* for pMP->samplesPerFrame */
815 pMP->samplesPerFrame[pMP->framePos] = 0;
817 pMP->sum_QP += pMP->QP;
819 /* update pMP->counter_BTsrc, pMP->counter_BTdst */
820 /* re-allocate the target bit again and then stop encoding */
821 diff_BTCounter = (int)((OsclFloat)(rateCtrl->TMN_TH - rateCtrl->TMN_W - pMP->actual_bits) /
822 (pMP->bitrate / (pMP->framerate + 0.0001) + 0.0001) / 0.1);
823 if (diff_BTCounter >= 0)
824 pMP->counter_BTsrc += diff_BTCounter; /* pMP->actual_bits is smaller */
826 pMP->counter_BTdst -= diff_BTCounter; /* pMP->actual_bits is bigger */
828 rateCtrl->TMN_TH -= (int)((OsclFloat)pMP->bitrate / (pMP->framerate + 0.0001) * (diff_BTCounter * 0.1));
829 rateCtrl->T = pMP->target_bits = rateCtrl->TMN_TH - rateCtrl->TMN_W;
830 pMP->diff_counter -= diff_BTCounter;
832 rateCtrl->Rc = rateCtrl->numFrameBits; /* Total Bits for current frame */
833 rateCtrl->Hc = rateCtrl->NumberofHeaderBits; /* Total Bits in Header and Motion Vector */
836 updateRateControl(rateCtrl, nal_type);
837 if (rateCtrl->skip_next_frame == -1) // skip current frame
839 status = AVCENC_SKIPPED_PICTURE;
843 rateCtrl->first_frame = 0; // reset here after we encode the first frame.
848 void AVCSaveRDSamples(MultiPass *pMP, int counter_samples)
850 /* for pMP->pRDSamples */
851 pMP->pRDSamples[pMP->framePos][counter_samples].QP = pMP->QP;
852 pMP->pRDSamples[pMP->framePos][counter_samples].actual_bits = pMP->actual_bits;
853 pMP->pRDSamples[pMP->framePos][counter_samples].mad = pMP->mad;
854 pMP->pRDSamples[pMP->framePos][counter_samples].R_D = (OsclFloat)pMP->actual_bits / (pMP->mad + 0.0001);
859 void updateRateControl(AVCRateControl *rateCtrl, int nal_type)
862 MultiPass *pMP = rateCtrl->pMP;
864 /* BX rate contro\l */
865 frame_bits = (int)(rateCtrl->bitRate / rateCtrl->frame_rate);
866 rateCtrl->TMN_W += (rateCtrl->Rc - rateCtrl->TMN_TH);
867 rateCtrl->VBV_fullness += (rateCtrl->Rc - frame_bits); //rateCtrl->Rp);
868 //if(rateCtrl->VBV_fullness < 0) rateCtrl->VBV_fullness = -1;
870 rateCtrl->encoded_frames++;
873 rateCtrl->skip_next_frame = 0;
875 if ((rateCtrl->VBV_fullness > rateCtrl->Bs / 2) && nal_type != AVC_NALTYPE_IDR) /* skip the current frame */ /* rateCtrl->Bs */
877 rateCtrl->TMN_W -= (rateCtrl->Rc - rateCtrl->TMN_TH);
878 rateCtrl->VBV_fullness -= rateCtrl->Rc;
879 rateCtrl->skip_next_frame = -1;
881 else if ((OsclFloat)(rateCtrl->VBV_fullness - rateCtrl->VBV_fullness_offset) > (rateCtrl->Bs / 2 - rateCtrl->VBV_fullness_offset)*0.95) /* skip next frame */
883 rateCtrl->VBV_fullness -= frame_bits; //rateCtrl->Rp;
884 rateCtrl->skip_next_frame = 1;
885 pMP->counter_BTsrc -= (int)((OsclFloat)(rateCtrl->Bs / 2 - rateCtrl->low_bound) / 2.0 / (pMP->target_bits_per_frame / 10));
886 /* BX_1, skip more than 1 frames */
887 //while(rateCtrl->VBV_fullness > rateCtrl->Bs*0.475)
888 while ((rateCtrl->VBV_fullness - rateCtrl->VBV_fullness_offset) > (rateCtrl->Bs / 2 - rateCtrl->VBV_fullness_offset)*0.95)
890 rateCtrl->VBV_fullness -= frame_bits; //rateCtrl->Rp;
891 rateCtrl->skip_next_frame++;
892 pMP->counter_BTsrc -= (int)((OsclFloat)(rateCtrl->Bs / 2 - rateCtrl->low_bound) / 2.0 / (pMP->target_bits_per_frame / 10));
900 double ComputeFrameMAD(AVCCommonObj *video, AVCRateControl *rateCtrl)
905 for (i = 0; i < (int)video->PicSizeInMbs; i++)
906 TotalMAD += rateCtrl->MADofMB[i];
907 TotalMAD /= video->PicSizeInMbs;
915 /* convert from QP to Qstep */
916 double QP2Qstep(int QP)
920 static const double QP2QSTEP[6] = { 0.625, 0.6875, 0.8125, 0.875, 1.0, 1.125 };
922 Qstep = QP2QSTEP[QP % 6];
923 for (i = 0; i < (QP / 6); i++)
929 /* convert from step size to QP */
930 int Qstep2QP(double Qstep)
932 int q_per = 0, q_rem = 0;
934 // assert( Qstep >= QP2Qstep(0) && Qstep <= QP2Qstep(51) );
935 if (Qstep < QP2Qstep(0))
937 else if (Qstep > QP2Qstep(51))
940 while (Qstep > QP2Qstep(5))
946 if (Qstep <= (0.625 + 0.6875) / 2)
951 else if (Qstep <= (0.6875 + 0.8125) / 2)
956 else if (Qstep <= (0.8125 + 0.875) / 2)
961 else if (Qstep <= (0.875 + 1.0) / 2)
966 else if (Qstep <= (1.0 + 1.125) / 2)
977 return (q_per * 6 + q_rem);