void RCInitFrameQP(AVCEncObject *encvid) { AVCCommonObj *video = encvid->common; AVCRateControl *rateCtrl = encvid->rateCtrl; AVCPicParamSet *picParam = video->currPicParams; MultiPass *pMP = rateCtrl->pMP; if (rateCtrl->rcEnable == TRUE) { /* frame layer rate control */ if (rateCtrl->encoded_frames == 0) { video->QPy = rateCtrl->Qc = rateCtrl->initQP; } else { calculateQuantizer_Multipass(encvid, video, rateCtrl, pMP); video->QPy = rateCtrl->Qc; } rateCtrl->NumberofHeaderBits = 0; rateCtrl->NumberofTextureBits = 0; rateCtrl->numFrameBits = 0; // reset /* update pMP->framePos */ if (++pMP->framePos == pMP->frameRange) pMP->framePos = 0; if (rateCtrl->T == 0) { pMP->counter_BTdst = (int)(rateCtrl->frame_rate * 7.5 + 0.5); /* 0.75s time frame */ 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 */ 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 */ pMP->counter_BTdst = AVC_MIN(pMP->counter_BTdst, 20); /* Limit the target to be smaller than 3C */ pMP->target_bits = rateCtrl->T = rateCtrl->TMN_TH = (int)(rateCtrl->TMN_TH * (1.0 + pMP->counter_BTdst * 0.1)); pMP->diff_counter = pMP->counter_BTdst; } /* collect the necessary data: target bits, actual bits, mad and QP */ pMP->target_bits = rateCtrl->T; pMP->QP = video->QPy; pMP->mad = (OsclFloat)rateCtrl->totalSAD / video->PicSizeInMbs; //ComputeFrameMAD(video, rateCtrl); if (pMP->mad < MAD_MIN) pMP->mad = MAD_MIN; /* MAD_MIN is defined as 1 in mp4def.h */ pMP->bitrate = rateCtrl->bitRate; /* calculated in RCVopQPSetting */ pMP->framerate = rateCtrl->frame_rate; /* first pass encoding */ pMP->nRe_Quantized = 0; } // rcEnable else { video->QPy = rateCtrl->initQP; } // printf(" %d ",video->QPy); if (video->CurrPicNum == 0 && encvid->outOfBandParamSet == FALSE) { picParam->pic_init_qs_minus26 = 0; picParam->pic_init_qp_minus26 = video->QPy - 26; } // need this for motion estimation encvid->lambda_mode = QP2QUANT[AVC_MAX(0, video->QPy-SHIFT_QP)]; encvid->lambda_motion = LAMBDA_FACTOR(encvid->lambda_mode); return ; }
PV_STATUS RC_VopQPSetting(VideoEncData *video, rateControl *prc[]) { Int currLayer = video->currLayer; Vol *currVol = video->vol[currLayer]; Vop *currVop = video->currVop; #ifdef TEST_MBBASED_QP int i; #endif rateControl *rc = video->rc[currLayer]; MultiPass *pMP = video->pMP[currLayer]; OSCL_UNUSED_ARG(prc); if (video->encParams->RC_Type == CONSTANT_Q) { M4VENC_MEMSET(video->QPMB, currVop->quantizer, sizeof(UChar)*currVol->nTotalMB); return PV_SUCCESS; } else { if (video->rc[currLayer]->encoded_frames == 0) /* rc[currLayer]->totalFrameNumber*/ { M4VENC_MEMSET(video->QPMB, currVop->quantizer, sizeof(UChar)*currVol->nTotalMB); video->rc[currLayer]->Qc = video->encParams->InitQuantIvop[currLayer]; } else { calculateQuantizer_Multipass((void*) video); currVop->quantizer = video->rc[currLayer]->Qc; #ifdef TEST_MBBASED_QP i = currVol->nTotalMB; /* testing changing QP at MB level */ while (i) { i--; video->QPMB[i] = (i & 1) ? currVop->quantizer - 1 : currVop->quantizer + 1; } #else M4VENC_MEMSET(video->QPMB, currVop->quantizer, sizeof(UChar)*currVol->nTotalMB); #endif } video->header_bits = 0; } /* update pMP->framePos */ if (++pMP->framePos == pMP->frameRange) pMP->framePos = 0; if (rc->T == 0) { pMP->counter_BTdst = (Int)(video->encParams->LayerFrameRate[video->currLayer] * 7.5 + 0.5); /* 0.75s time frame */ pMP->counter_BTdst = PV_MIN(pMP->counter_BTdst, (Int)(rc->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 */ pMP->counter_BTdst = PV_MAX(pMP->counter_BTdst, (Int)((rc->Bs / 2 - rc->VBV_fullness) * 0.30 / (rc->TMN_TH / 10.0) + 0.5)); /* At least 30% of VBV buffer size/2 */ pMP->counter_BTdst = PV_MIN(pMP->counter_BTdst, 20); /* Limit the target to be smaller than 3C */ pMP->target_bits = rc->T = rc->TMN_TH = (Int)(rc->TMN_TH * (1.0 + pMP->counter_BTdst * 0.1)); pMP->diff_counter = pMP->counter_BTdst; } /* collect the necessary data: target bits, actual bits, mad and QP */ pMP->target_bits = rc->T; pMP->QP = currVop->quantizer; pMP->mad = video->sumMAD / (float)currVol->nTotalMB; if (pMP->mad < MAD_MIN) pMP->mad = MAD_MIN; /* MAD_MIN is defined as 1 in mp4def.h */ pMP->bitrate = rc->bitrate; /* calculated in RCVopQPSetting */ pMP->framerate = rc->framerate; /* first pass encoding */ pMP->nRe_Quantized = 0; return PV_SUCCESS; }