Lines Matching refs:pMP
34 void targetBitCalculation(AVCEncObject *encvid, AVCCommonObj *video, AVCRateControl *rateCtrl, MultiPass *pMP);
37 AVCRateControl *rateCtrl, MultiPass *pMP);
39 void updateRC_PostProc(AVCRateControl *rateCtrl, MultiPass *pMP);
41 void AVCSaveRDSamples(MultiPass *pMP, int counter_samples);
143 MultiPass *pMP = rateCtrl->pMP;
153 pMP->counter_BTsrc += 10 * (frameInc - 1);
160 pMP->counter_BTsrc = pMP->counter_BTdst + (int)((OsclFloat)(rateCtrl->Bs / 2 - rateCtrl->low_bound) / 2.0 / (pMP->target_bits_per_frame / 10));
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;
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));
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;
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);
338 MultiPass *pMP = rateCtrl->pMP;
349 calculateQuantizer_Multipass(encvid, video, rateCtrl, pMP);
357 /* update pMP->framePos */
358 if (++pMP->framePos == pMP->frameRange) pMP->framePos = 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 */
365 pMP->counter_BTdst = AVC_MIN(pMP->counter_BTdst, 20); /* Limit the target to be smaller than 3C */
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;
372 pMP->target_bits = rateCtrl->T;
373 pMP->QP = video->QPy;
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 */
378 pMP->bitrate = rateCtrl->bitRate; /* calculated in RCVopQPSetting */
379 pMP->framerate = rateCtrl->frame_rate;
382 pMP->nRe_Quantized = 0;
406 AVCRateControl *rateCtrl, MultiPass *pMP)
414 targetBitCalculation(encvid, video, rateCtrl, pMP);
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;
441 while (i < pMP->frameRange && prev_mad < 0.001) /* find first one with nonzero prev_mad */
443 prev_mad = pMP->pRDSamples[i][0].mad;
447 if (i < pMP->frameRange)
449 prev_actual_bits = pMP->pRDSamples[i-1][0].actual_bits;
451 for (j = 0; i < pMP->frameRange; i++)
453 if (pMP->pRDSamples[i][0].mad != 0 &&
454 AVC_ABS(prev_mad - curr_mad) > AVC_ABS(pMP->pRDSamples[i][0].mad - curr_mad))
456 prev_mad = pMP->pRDSamples[i][0].mad;
457 prev_actual_bits = pMP->pRDSamples[i][0].actual_bits;
461 prev_QP = QP2Qstep(pMP->pRDSamples[j][0].QP);
463 for (i = 1; i < pMP->samplesPerFrame[j]; i++)
465 if (AVC_ABS(prev_actual_bits - curr_target) > AVC_ABS(pMP->pRDSamples[j][i].actual_bits - curr_target))
467 prev_actual_bits = pMP->pRDSamples[j][i].actual_bits;
468 prev_QP = QP2Qstep(pMP->pRDSamples[j][i].QP);
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 &&
510 pMP->counter_BTsrc <= (pMP->counter_BTdst + (int)(pMP->framerate*1.0 + 0.5)))
512 rateCtrl->TMN_TH -= (int)(pMP->target_bits_per_frame / 10.0);
514 pMP->counter_BTsrc++;
515 pMP->diff_counter--;
520 void targetBitCalculation(AVCEncObject *encvid, AVCCommonObj *video, AVCRateControl *rateCtrl, MultiPass *pMP)
525 /* BT = Bit Transfer, for pMP->counter_BTsrc, pMP->counter_BTdst */
527 /* some stuff about frame dropping remained here to be done because pMP cannot be inserted into updateRateControl()*/
528 updateRC_PostProc(rateCtrl, pMP);
530 /* update pMP->counter_BTsrc and pMP->counter_BTdst to avoid interger overflow */
531 if (pMP->counter_BTsrc > 1000 && pMP->counter_BTdst > 1000)
533 pMP->counter_BTsrc -= 1000;
534 pMP->counter_BTdst -= 1000;
542 pMP->diff_counter = 0;
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);
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);
556 if (pMP->overlapped_win_size == 0)
559 if (curr_mad > pMP->aver_mad*1.1)
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;
565 diff_counter_BTdst = (int)(curr_mad / (pMP->aver_mad + 0.0001) * 10 + 0.4) - 10;
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);
573 curr_mad <= pMP->aver_mad*1.1 && pMP->counter_BTsrc < pMP->counter_BTdst)
577 else if (pMP->overlapped_win_size > 0)
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)
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;
586 diff_counter_BTdst = (int)(curr_mad / (pMP->aver_mad_prev + 0.0001) * 10 + 0.4) - 10;
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);
594 curr_mad <= pMP->aver_mad_prev*1.1 && pMP->counter_BTsrc < pMP->counter_BTdst)
597 if (--pMP->overlapped_win_size <= 0) pMP->overlapped_win_size = 0;
603 bound = (int)((rateCtrl->Bs / 2 - rateCtrl->VBV_fullness) * 0.6 / (pMP->target_bits_per_frame / 10)); /* rateCtrl->Bs */
617 prev_counter_diff = pMP->counter_BTdst - pMP->counter_BTsrc;
638 rateCtrl->TMN_TH = (int)(pMP->target_bits_per_frame);
639 pMP->diff_counter = 0;
643 rateCtrl->TMN_TH -= (int)(pMP->target_bits_per_frame * diff_counter_BTsrc * 0.1);
644 pMP->diff_counter = -diff_counter_BTsrc;
648 rateCtrl->TMN_TH += (int)(pMP->target_bits_per_frame * diff_counter_BTdst * 0.1);
649 pMP->diff_counter = diff_counter_BTdst;
653 /*4.update pMP->counter_BTsrc, pMP->counter_BTdst */
654 pMP->counter_BTsrc += diff_counter_BTsrc;
655 pMP->counter_BTdst += diff_counter_BTdst;
664 void updateRC_PostProc(AVCRateControl *rateCtrl, MultiPass *pMP)
668 pMP->counter_BTsrc += 10 * rateCtrl->skip_next_frame;
673 pMP->counter_BTdst -= pMP->diff_counter;
674 pMP->counter_BTsrc += 10;
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 --;
687 pMP->counter_BTsrc = pMP->counter_BTdst + (int)((OsclFloat)(rateCtrl->Bs / 2 - rateCtrl->low_bound) / 2.0 / (pMP->target_bits_per_frame / 10));
797 MultiPass *pMP = rateCtrl->pMP;
805 pMP->actual_bits = rateCtrl->numFrameBits;
806 pMP->mad = (OsclFloat)rateCtrl->totalSAD / video->PicSizeInMbs; //ComputeFrameMAD(video, rateCtrl);
808 AVCSaveRDSamples(pMP, 0);
810 pMP->encoded_frames++;
812 /* for pMP->samplesPerFrame */
813 pMP->samplesPerFrame[pMP->framePos] = 0;
815 pMP->sum_QP += pMP->QP;
817 /* update pMP->counter_BTsrc, pMP->counter_BTdst */
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);
822 pMP->counter_BTsrc += diff_BTCounter; /* pMP->actual_bits is smaller */
824 pMP->counter_BTdst -= diff_BTCounter; /* pMP->actual_bits is bigger */
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;
846 void AVCSaveRDSamples(MultiPass *pMP, int counter_samples)
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);
860 MultiPass *pMP = rateCtrl->pMP;
883 pMP->counter_BTsrc -= (int)((OsclFloat)(rateCtrl->Bs / 2 - rateCtrl->low_bound) / 2.0 / (pMP->target_bits_per_frame / 10));
890 pMP->counter_BTsrc -= (int)((OsclFloat)(rateCtrl->Bs / 2 - rateCtrl->low_bound) / 2.0 / (pMP->target_bits_per_frame / 10));