Home | History | Annotate | Download | only in src

Lines Matching defs:rateCtrl

32 double ComputeFrameMAD(AVCCommonObj *video, AVCRateControl *rateCtrl);
34 void targetBitCalculation(AVCEncObject *encvid, AVCCommonObj *video, AVCRateControl *rateCtrl, MultiPass *pMP);
37 AVCRateControl *rateCtrl, MultiPass *pMP);
39 void updateRC_PostProc(AVCRateControl *rateCtrl, MultiPass *pMP);
45 int GetAvgFrameQP(AVCRateControl *rateCtrl)
47 return rateCtrl->Qc;
50 AVCEnc_Status RCDetermineFrameNum(AVCEncObject *encvid, AVCRateControl *rateCtrl, uint32 modTime, uint *frameNum)
64 if (rateCtrl->first_frame)
89 currFrameNum = (int32)(((modTime - modTimeRef) * rateCtrl->frame_rate + 200) / 1000); /* add small roundings */
98 if (frameInc < rateCtrl->skip_next_frame + 1)
103 RCUpdateBuffer(video, rateCtrl, frameInc - rateCtrl->skip_next_frame); /* in case more frames dropped */
108 if ((*frameNum >= (uint)rateCtrl->idrPeriod && rateCtrl->idrPeriod > 0) || (*frameNum > video->MaxFrameNum)) /* first frame or IDR*/
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);
140 void RCUpdateBuffer(AVCCommonObj *video, AVCRateControl *rateCtrl, int frameInc)
143 MultiPass *pMP = rateCtrl->pMP;
147 if (rateCtrl->rcEnable == TRUE)
151 tmp = rateCtrl->bitsPerFrame * (frameInc - 1);
152 rateCtrl->VBV_fullness -= tmp;
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));
171 AVCRateControl *rateCtrl = encvid->rateCtrl;
176 rateCtrl->basicUnit = video->PicSizeInMbs;
178 rateCtrl->MADofMB = (double*) avcHandle->CBAVC_Malloc(encvid->avcHandle->userData,
181 if (!rateCtrl->MADofMB)
186 if (rateCtrl->rcEnable == TRUE)
188 rateCtrl->pMP = (MultiPass*) avcHandle->CBAVC_Malloc(encvid->avcHandle->userData, sizeof(MultiPass), DEFAULT_ATTR);
189 if (!rateCtrl->pMP)
193 rateCtrl->pMP->encoded_frames = -1; /* forget about the very first I frame */
196 rateCtrl->pMP->pRDSamples = (RDInfo **)avcHandle->CBAVC_Malloc(encvid->avcHandle->userData, (30 * sizeof(RDInfo *)), DEFAULT_ATTR);
197 if (!rateCtrl->pMP->pRDSamples)
204 rateCtrl->pMP->pRDSamples[i] = (RDInfo *)avcHandle->CBAVC_Malloc(encvid->avcHandle->userData, (32 * sizeof(RDInfo)), DEFAULT_ATTR);
205 if (!rateCtrl->pMP->pRDSamples[i])
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);
214 rateCtrl->pMP->framePos = -1;
217 rateCtrl->bitsPerFrame = (int32)(rateCtrl->bitRate / rateCtrl->frame_rate);
220 rateCtrl->skip_next_frame = 0; /* must be initialized */
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;
227 rateCtrl->TMN_TH = rateCtrl->bitsPerFrame;
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;
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));
238 rateCtrl->low_bound = -rateCtrl->Bs / 2;
239 rateCtrl->VBV_fullness_offset = 0;
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;
247 bpp = 1.0 * rateCtrl->bitRate / (rateCtrl->frame_rate * (video->PicSizeInMbs << 8));
267 if (rateCtrl->initQP == 0)
277 rateCtrl->initQP = qp;
280 rateCtrl->Qc = rateCtrl->initQP;
296 AVCRateControl *rateCtrl = encvid->rateCtrl;
299 if (rateCtrl->MADofMB)
301 avcHandle->CBAVC_Free(avcHandle->userData, rateCtrl->MADofMB);
304 if (rateCtrl->pMP)
306 if (rateCtrl->pMP->pRDSamples)
310 if (rateCtrl->pMP->pRDSamples[i])
312 avcHandle->CBAVC_Free(avcHandle->userData, rateCtrl->pMP->pRDSamples[i]);
315 avcHandle->CBAVC_Free(avcHandle->userData, rateCtrl->pMP->pRDSamples);
317 avcHandle->CBAVC_Free(avcHandle->userData, rateCtrl->pMP);
336 AVCRateControl *rateCtrl = encvid->rateCtrl;
338 MultiPass *pMP = rateCtrl->pMP;
340 if (rateCtrl->rcEnable == TRUE)
343 if (rateCtrl->encoded_frames == 0)
345 video->QPy = rateCtrl->Qc = rateCtrl->initQP;
349 calculateQuantizer_Multipass(encvid, video, rateCtrl, pMP);
350 video->QPy = rateCtrl->Qc;
353 rateCtrl->NumberofHeaderBits = 0;
354 rateCtrl->NumberofTextureBits = 0;
355 rateCtrl->numFrameBits = 0; // reset
360 if (rateCtrl->T == 0)
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 */
367 pMP->target_bits = rateCtrl->T = rateCtrl->TMN_TH = (int)(rateCtrl->TMN_TH * (1.0 + pMP->counter_BTdst * 0.1));
372 pMP->target_bits = rateCtrl->T;
375 pMP->mad = (OsclFloat)rateCtrl->totalSAD / video->PicSizeInMbs; //ComputeFrameMAD(video, rateCtrl);
378 pMP->bitrate = rateCtrl->bitRate; /* calculated in RCVopQPSetting */
379 pMP->framerate = rateCtrl->frame_rate;
387 video->QPy = rateCtrl->initQP;
406 AVCRateControl *rateCtrl, MultiPass *pMP)
414 targetBitCalculation(encvid, video, rateCtrl, pMP);
416 if (rateCtrl->T <= 0 || rateCtrl->totalSAD == 0)
418 if (rateCtrl->T < 0) rateCtrl->Qc = RC_MAX_QUANT;
424 curr_target = rateCtrl->T;
425 curr_mad = (OsclFloat)rateCtrl->totalSAD / video->PicSizeInMbs;
429 if (rateCtrl->skip_next_frame == -1) // previous was skipped
478 //rateCtrl->Qc = (Int)(prev_QP * sqrt(prev_actual_bits/curr_target) + 0.4);
485 // rateCtrl->Qc =(Int)(prev_QP * M4VENC_SQRT(prev_RD/curr_RD) + 0.9);
496 /// if((curr_mad < 8) && (rateCtrl->Qc < 12)) rateCtrl->Qc = 12;
497 // else if((curr_mad < 128) && (rateCtrl->Qc < 3)) rateCtrl->Qc = 3;
499 rateCtrl->Qc = Qstep2QP(Qstep);
501 if (rateCtrl->Qc < RC_MIN_QUANT) rateCtrl->Qc = RC_MIN_QUANT;
502 if (rateCtrl->Qc > RC_MAX_QUANT) rateCtrl->Qc = RC_MAX_QUANT;
509 ((OsclFloat)rateCtrl->Qc <= aver_QP*1.1 || curr_mad <= average_mad*1.1) &&
512 rateCtrl->TMN_TH -= (int)(pMP->target_bits_per_frame / 10.0);
513 rateCtrl->T = rateCtrl->TMN_TH - rateCtrl->TMN_W;
520 void targetBitCalculation(AVCEncObject *encvid, AVCCommonObj *video, AVCRateControl *rateCtrl, MultiPass *pMP)
528 updateRC_PostProc(rateCtrl, pMP);
539 curr_mad = (OsclFloat)rateCtrl->totalSAD / video->PicSizeInMbs;
603 bound = (int)((rateCtrl->Bs / 2 - rateCtrl->VBV_fullness) * 0.6 / (pMP->target_bits_per_frame / 10)); /* rateCtrl->Bs */
620 if (AVC_ABS(prev_counter_diff) >= rateCtrl->max_BitVariance_num || AVC_ABS(curr_counter_diff) >= rateCtrl->max_BitVariance_num)
623 if (curr_counter_diff > rateCtrl->max_BitVariance_num && diff_counter_BTdst)
625 diff_counter_BTdst = (rateCtrl->max_BitVariance_num - prev_counter_diff) + diff_counter_BTsrc;
629 else if (curr_counter_diff < -rateCtrl->max_BitVariance_num && diff_counter_BTsrc)
631 diff_counter_BTsrc = diff_counter_BTdst - (-rateCtrl->max_BitVariance_num - prev_counter_diff);
638 rateCtrl->TMN_TH = (int)(pMP->target_bits_per_frame);
643 rateCtrl->TMN_TH -= (int)(pMP->target_bits_per_frame * diff_counter_BTsrc * 0.1);
648 rateCtrl->TMN_TH += (int)(pMP->target_bits_per_frame * diff_counter_BTdst * 0.1);
659 rateCtrl->T = rateCtrl->TMN_TH - rateCtrl->TMN_W;
664 void updateRC_PostProc(AVCRateControl *rateCtrl, MultiPass *pMP)
666 if (rateCtrl->skip_next_frame > 0) /* skip next frame */
668 pMP->counter_BTsrc += 10 * rateCtrl->skip_next_frame;
671 else if (rateCtrl->skip_next_frame == -1) /* skip current frame */
682 //if(rateCtrl->VBV_fullness < -rateCtrl->Bs/2) /* rateCtrl->Bs */
683 if (rateCtrl->VBV_fullness < rateCtrl->low_bound)
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));
745 void RCPostMB(AVCCommonObj *video, AVCRateControl *rateCtrl, int num_header_bits, int num_texture_bits)
748 rateCtrl->numMBHeaderBits = num_header_bits;
749 rateCtrl->numMBTextureBits = num_texture_bits;
750 rateCtrl->NumberofHeaderBits += rateCtrl->numMBHeaderBits;
751 rateCtrl->NumberofTextureBits += rateCtrl->numMBTextureBits;
766 AVCRateControl *rateCtrl = encvid->rateCtrl;
769 if (rateCtrl->rcEnable == TRUE)
776 rateCtrl->MADofMB[video->mbNum] = AVCSAD_Macroblock_C(orgL,
781 rateCtrl->MADofMB[video->mbNum] = encvid->i4_sad / 256.;
795 AVCRateControl *rateCtrl = encvid->rateCtrl;
797 MultiPass *pMP = rateCtrl->pMP;
803 if (rateCtrl->rcEnable == TRUE)
805 pMP->actual_bits = rateCtrl->numFrameBits;
806 pMP->mad = (OsclFloat)rateCtrl->totalSAD / video->PicSizeInMbs; //ComputeFrameMAD(video, rateCtrl);
819 diff_BTCounter = (int)((OsclFloat)(rateCtrl->TMN_TH - rateCtrl->TMN_W - pMP->actual_bits) /
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;
830 rateCtrl->Rc = rateCtrl->numFrameBits; /* Total Bits for current frame */
831 rateCtrl->Hc = rateCtrl->NumberofHeaderBits; /* Total Bits in Header and Motion Vector */
834 updateRateControl(rateCtrl, nal_type);
835 if (rateCtrl->skip_next_frame == -1) // skip current frame
841 rateCtrl->first_frame = 0; // reset here after we encode the first frame.
857 void updateRateControl(AVCRateControl *rateCtrl, int nal_type)
860 MultiPass *pMP = rateCtrl->pMP;
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;
868 rateCtrl->encoded_frames++;
871 rateCtrl->skip_next_frame = 0;
873 if ((rateCtrl->VBV_fullness > rateCtrl->Bs / 2) && nal_type != AVC_NALTYPE_IDR) /* skip the current frame */ /* rateCtrl->Bs */
875 rateCtrl->TMN_W -= (rateCtrl->Rc - rateCtrl->TMN_TH);
876 rateCtrl->VBV_fullness -= rateCtrl->Rc;
877 rateCtrl->skip_next_frame = -1;
879 else if ((OsclFloat)(rateCtrl->VBV_fullness - rateCtrl->VBV_fullness_offset) > (rateCtrl->Bs / 2 - rateCtrl->VBV_fullness_offset)*0.95) /* skip next frame */
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));
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)
888 rateCtrlrateCtrl->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));
898 double ComputeFrameMAD(AVCCommonObj *video, AVCRateControl *rateCtrl)
904 TotalMAD += rateCtrl->MADofMB[i];