History

refine RC parameters

karina li committed 11 years ago

Commit ab370cd1 parent 3c067536 files · +6966 −6964Browse files

Showing the changes to codec/encoder/core/src/ratectl.cpp (1 of 6 files). Show all

Unified · Split

codec/encoder/core/src/ratectl.cpp+37−35
@@ -343,11 +343,11 @@ void RcTraceVGopBitrate (sWelsEncCtx* pEncCtx) { · 342 unmodified lines
343343 int32_t iFrameInVGop = pWelsSvcRc->iFrameCodedInVGop + pWelsSvcRc->iSkipFrameInVGop;
344344 if (0 != iFrameInVGop)
345345 iVGopBitrate = WELS_ROUND (iTotalBits / iFrameInVGop * pWelsSvcRc->fFrameRate);
346− WelsLog (& (pEncCtx->sLogCtx), WELS_LOG_INFO, "[Rc] VGOPbitrate%d: %d ", kiDid, iVGopBitrate);
346+ WelsLog (& (pEncCtx->sLogCtx), WELS_LOG_DEBUG, "[Rc] VGOPbitrate%d: %d ", kiDid, iVGopBitrate);
347347 if (iTotalBits > 0) {
348348 iTid = 0;
349349 while (iTid <= kiHighestTid) {
350− WelsLog (& (pEncCtx->sLogCtx), WELS_LOG_INFO, "T%d=%8.3f ", iTid, (double) (pTOverRc[iTid].iGopBitsDq / iTotalBits));
350+ WelsLog (& (pEncCtx->sLogCtx), WELS_LOG_DEBUG, "T%d=%8.3f ", iTid, (double) (pTOverRc[iTid].iGopBitsDq / iTotalBits));
351351 ++ iTid;
352352 }
353353 }
@@ -625,7 +625,6 @@ void RcGomTargetBits (sWelsEncCtx* pEncCtx, const int32_t kiSliceId) { · 271 unmodified lines
625625
626626 iLastGomIndex = pSOverRc->iEndMbSlice / pWelsSvcRc->iNumberMbGom;
627627 iLeftBits = pSOverRc->iTargetBitsSlice - pSOverRc->iFrameBitsSlice;
628−
629628 if (iLeftBits <= 0) {
630629 pSOverRc->iGomTargetBits = 0;
631630 return;
@@ -671,9 +670,8 @@ void RcCalculateGomQp (sWelsEncCtx* pEncCtx, SMB* pCurMb, int32_t iSliceId) { · 39 unmodified lines
671670 else if (iBitsRatio > 11900) //2^(1.5/6)*10000
672671 pSOverRc->iCalculatedQpSlice -= 2;
673672 }
673+ pSOverRc->iCalculatedQpSlice = WELS_CLIP3 (pSOverRc->iCalculatedQpSlice, pWelsSvcRc->iMinQp, pWelsSvcRc->iMaxQp);
674674
675− pSOverRc->iCalculatedQpSlice = WELS_CLIP3 (pSOverRc->iCalculatedQpSlice,
676− pEncCtx->iGlobalQp - pWelsSvcRc->iQpRangeLowerInFrame, pEncCtx->iGlobalQp + pWelsSvcRc->iQpRangeUpperInFrame);
677675 if (! (((pEncCtx->pSvcParam->iRCMode == RC_BITRATE_MODE) || (pEncCtx->pSvcParam->iRCMode == RC_TIMESTAMP_MODE))
678676 && (pEncCtx->pSvcParam->bEnableFrameSkip == false)))
679677 pSOverRc->iCalculatedQpSlice = WELS_CLIP3 (pSOverRc->iCalculatedQpSlice, pWelsSvcRc->iMinQp, pWelsSvcRc->iMaxQp);
@@ -790,8 +788,10 @@ bool CheckFrameSkipBasedMaxbr (sWelsEncCtx* pEncCtx, int32_t iSpatialNum, EVideo · 110 unmodified lines
790788 pEncCtx->iContinualSkipFrames++;
791789 break;
792790 }
793−
794−
791+ }
792+ if (bSkipMustFlag) {
793+ for (int32_t i = 0; i < iSpatialNum; i++)
794+ pEncCtx->pWelsSvcRc[i].uiLastTimeStamp = uiTimeStamp;
795795 }
796796 return bSkipMustFlag;
797797 }
@@ -819,7 +819,7 @@ void UpdateBufferWhenFrameSkipped (sWelsEncCtx* pEncCtx, int32_t iSpatialNum) { · 21 unmodified lines
819819
820820 }
821821 pEncCtx->iContinualSkipFrames++;
822− if ( ( pEncCtx->iContinualSkipFrames % 3 ) == 0 ) {
822+ if ((pEncCtx->iContinualSkipFrames % 3) == 0) {
823823 //output a warning when iContinualSkipFrames is large enough, which may indicate subjective quality problem
824824 //note that here iContinualSkipFrames must be >0, so the log output will be 3/6/....
825825 WelsLog (& (pEncCtx->sLogCtx), WELS_LOG_WARNING, "[Rc] iContinualSkipFrames(%d) is large",
@@ -916,7 +916,8 @@ void RcTraceFrameBits (sWelsEncCtx* pEncCtx, long long uiTimeStamp) { · 90 unmodified lines
916916 WelsLog (& (pEncCtx->sLogCtx), WELS_LOG_DEBUG,
917917 "[Rc]Layer %d: Frame timestamp = %lld, Frame type = %d, encoding_qp = %d, average qp = %d, max qp = %d, min qp = %d, index = %d, "
918918 "iTid = %d, used = %d, bitsperframe = %d, target = %d, remainingbits = %d, skipbuffersize = %d",
919− pEncCtx->uiDependencyId, uiTimeStamp, pEncCtx->eSliceType, pEncCtx->iGlobalQp, pWelsSvcRc->iAverageFrameQp, pWelsSvcRc->iMaxFrameQp,
919+ pEncCtx->uiDependencyId, uiTimeStamp, pEncCtx->eSliceType, pEncCtx->iGlobalQp, pWelsSvcRc->iAverageFrameQp,
920+ pWelsSvcRc->iMaxFrameQp,
920921 pWelsSvcRc->iMinFrameQp,
921922 pEncCtx->iFrameIndex, pEncCtx->uiTemporalId, pWelsSvcRc->iFrameDqBits, pWelsSvcRc->iBitsPerFrame,
922923 pWelsSvcRc->iTargetBits, pWelsSvcRc->iRemainingBits, pWelsSvcRc->iBufferSizeSkip);
@@ -944,6 +945,7 @@ void RcUpdatePictureQpBits (sWelsEncCtx* pEncCtx, int32_t iCodedBits) { · 21 unmodified lines
944945 pWelsSvcRc->iAverageFrameQp = pEncCtx->iGlobalQp;
945946 }
946947 pWelsSvcRc->iFrameDqBits = iCodedBits;
948+ pWelsSvcRc->iLastCalculatedQScale = pWelsSvcRc->iAverageFrameQp;
947949 pWelsSvcRc->pTemporalOverRc[pEncCtx->uiTemporalId].iGopBitsDq += pWelsSvcRc->iFrameDqBits;
948950 }
949951
@@ -951,8 +953,8 @@ void RcUpdateIntraComplexity (sWelsEncCtx* pEncCtx) { · 1 unmodified line
951953 SWelsSvcRc* pWelsSvcRc = &pEncCtx->pWelsSvcRc[pEncCtx->uiDependencyId];
952954 int32_t iAlpha = WELS_DIV_ROUND (INT_MULTIPLY, (1 + pWelsSvcRc->iIdrNum));
953955 if (iAlpha < (INT_MULTIPLY / 4)) iAlpha = INT_MULTIPLY / 4;
954−
955− int64_t iIntraCmplx = pWelsSvcRc->iQStep * static_cast<int64_t> (pWelsSvcRc->iFrameDqBits);
956+ int32_t iQStep = RcConvertQp2QStep (pWelsSvcRc->iAverageFrameQp);
957+ int64_t iIntraCmplx = iQStep * static_cast<int64_t> (pWelsSvcRc->iFrameDqBits);
956958 pWelsSvcRc->iIntraComplexity = WELS_DIV_ROUND (((INT_MULTIPLY - iAlpha) * pWelsSvcRc->iIntraComplexity + iAlpha *
957959 iIntraCmplx), INT_MULTIPLY);
958960 pWelsSvcRc->iIntraMbCount = pWelsSvcRc->iNumberMbFrame;
@@ -960,9 +962,10 @@ void RcUpdateIntraComplexity (sWelsEncCtx* pEncCtx) { · 1 unmodified line
960962 pWelsSvcRc->iIdrNum++;
961963 if (pWelsSvcRc->iIdrNum > 255)
962964 pWelsSvcRc->iIdrNum = 255;
963− WelsLog (& (pEncCtx->sLogCtx), WELS_LOG_INFO,
965+ WelsLog (& (pEncCtx->sLogCtx), WELS_LOG_DEBUG,
964966 "RcUpdateIntraComplexity iFrameDqBits = %d,iQStep= %d,iIntraCmplx = %" PRId64,
965967 pWelsSvcRc->iFrameDqBits, pWelsSvcRc->iQStep, pWelsSvcRc->iIntraComplexity);
968+
966969 }
967970
968971 void RcUpdateFrameComplexity (sWelsEncCtx* pEncCtx) {
@@ -970,26 +973,32 @@ void RcUpdateFrameComplexity (sWelsEncCtx* pEncCtx) { · 1 unmodified line
970973 const int32_t kiTl = pEncCtx->uiTemporalId;
971974 SRCTemporal* pTOverRc = &pWelsSvcRc->pTemporalOverRc[kiTl];
972975
976+ int32_t iQStep = RcConvertQp2QStep (pWelsSvcRc->iAverageFrameQp);
973977 if (0 == pTOverRc->iPFrameNum) {
974− pTOverRc->iLinearCmplx = ((int64_t)pWelsSvcRc->iFrameDqBits) * pWelsSvcRc->iQStep;
978+ pTOverRc->iLinearCmplx = ((int64_t)pWelsSvcRc->iFrameDqBits) * iQStep;
975979 } else {
976980 pTOverRc->iLinearCmplx = WELS_DIV_ROUND64 ((LINEAR_MODEL_DECAY_FACTOR * (int64_t)pTOverRc->iLinearCmplx
977− + (INT_MULTIPLY - LINEAR_MODEL_DECAY_FACTOR) * (int64_t) (pWelsSvcRc->iFrameDqBits * pWelsSvcRc->iQStep)),
981+ + (INT_MULTIPLY - LINEAR_MODEL_DECAY_FACTOR) * (int64_t) (pWelsSvcRc->iFrameDqBits * iQStep)),
978982 INT_MULTIPLY);
979983 }
980984 int32_t iAlpha = WELS_DIV_ROUND (INT_MULTIPLY, (1 + pTOverRc->iPFrameNum));
981985 if (iAlpha < SMOOTH_FACTOR_MIN_VALUE)
982986 iAlpha = SMOOTH_FACTOR_MIN_VALUE;
983− pTOverRc->iFrameCmplxMean = WELS_DIV_ROUND (((INT_MULTIPLY - iAlpha) * static_cast<int64_t> (pTOverRc->iFrameCmplxMean)
984− + iAlpha * pEncCtx->pVaa->sComplexityAnalysisParam.iFrameComplexity),
987+ pTOverRc->iFrameCmplxMean = WELS_DIV_ROUND ((LINEAR_MODEL_DECAY_FACTOR * static_cast<int64_t>
988+ (pTOverRc->iFrameCmplxMean)
989+ + (INT_MULTIPLY - LINEAR_MODEL_DECAY_FACTOR) * pEncCtx->pVaa->sComplexityAnalysisParam.iFrameComplexity),
985990 INT_MULTIPLY);
986991
992+
987993 pTOverRc->iPFrameNum++;
988994 if (pTOverRc->iPFrameNum > 255)
989995 pTOverRc->iPFrameNum = 255;
990996 WelsLog (& (pEncCtx->sLogCtx), WELS_LOG_DEBUG,
991− "RcUpdateFrameComplexity iFrameDqBits = %d,iQStep= %d,pTOverRc->iLinearCmplx = %" PRId64, pWelsSvcRc->iFrameDqBits,
992− pWelsSvcRc->iQStep, pTOverRc->iLinearCmplx);
997+ "RcUpdateFrameComplexity iFrameDqBits = %d,iQStep= %d,pWelsSvcRc->iQStep= %d,pTOverRc->iLinearCmplx = %" PRId64,
998+ pWelsSvcRc->iFrameDqBits,
999+ iQStep, pWelsSvcRc->iQStep, pTOverRc->iLinearCmplx);
1000+ WelsLog (& (pEncCtx->sLogCtx), WELS_LOG_DEBUG, "iFrameCmplxMean = %d,iFrameComplexity = %lld",
1001+ pTOverRc->iFrameCmplxMean, pEncCtx->pVaa->sComplexityAnalysisParam.iFrameComplexity);
9931002 }
9941003
9951004 int32_t RcCalculateCascadingQp (struct TagWelsEncCtx* pEncCtx, int32_t iQp) {
@@ -1115,7 +1124,8 @@ void WelsRcPictureInitDisable (sWelsEncCtx* pEncCtx, long long uiTimeStamp) { · 119 unmodified lines
11151124
11161125 if (pEncCtx->pSvcParam->bEnableAdaptiveQuant && (pEncCtx->eSliceType == P_SLICE)) {
11171126 pEncCtx->iGlobalQp = WELS_CLIP3 ((pEncCtx->iGlobalQp * INT_MULTIPLY -
1118− pEncCtx->pVaa->sAdaptiveQuantParam.iAverMotionTextureIndexToDeltaQp) / INT_MULTIPLY, pWelsSvcRc->iMinQp, pWelsSvcRc->iMaxQp);
1127+ pEncCtx->pVaa->sAdaptiveQuantParam.iAverMotionTextureIndexToDeltaQp) / INT_MULTIPLY, pWelsSvcRc->iMinQp,
1128+ pWelsSvcRc->iMaxQp);
11191129 } else {
11201130 pEncCtx->iGlobalQp = WELS_CLIP3 (pEncCtx->iGlobalQp, 0, 51);
11211131 }
@@ -1231,7 +1241,7 @@ void WelsRcDropFrameUpdate (sWelsEncCtx* pEncCtx, uint32_t iDropSize) { · 109 unmodified lines
12311241
12321242 pWelsSvcRc->iBufferFullnessSkip -= (int32_t)iDropSize;
12331243 pWelsSvcRc->iBufferFullnessSkip = WELS_MAX (0, pWelsSvcRc->iBufferFullnessSkip);
1234− WelsLog (& (pEncCtx->sLogCtx), WELS_LOG_INFO, "[WelsRcDropFrameUpdate:\tdrop:%d\t%" PRId64 "\n", iDropSize,
1244+ WelsLog (& (pEncCtx->sLogCtx), WELS_LOG_DEBUG, "[WelsRcDropFrameUpdate:\tdrop:%d\t%" PRId64 "\n", iDropSize,
12351245 pWelsSvcRc->iBufferFullnessSkip);
12361246 }
12371247
@@ -1266,8 +1276,8 @@ void InitRcModuleTimeStamp (sWelsEncCtx* pEncCtx) { · 28 unmodified lines
12661276 pWelsSvcRc->iBufferFullnessSkip = 0;
12671277 pWelsSvcRc->uiLastTimeStamp = 0;
12681278
1269− pWelsSvcRc->iCost2BitsIntra = INT_MULTIPLY;
1270− pWelsSvcRc->iAvgCost2Bits = INT_MULTIPLY;
1279+ pWelsSvcRc->iCost2BitsIntra = 1;
1280+ pWelsSvcRc->iAvgCost2Bits = 1;
12711281 pWelsSvcRc->iSkipBufferRatio = SKIP_RATIO;
12721282 }
12731283 void WelsRcFrameDelayJudgeTimeStamp (sWelsEncCtx* pEncCtx, EVideoFrameType eFrameType, long long uiTimeStamp) {
@@ -1314,21 +1324,15 @@ void WelsRcPictureInitGomTimeStamp (sWelsEncCtx* pEncCtx, long long uiTimeStamp · 40 unmodified lines
13141324 if (pEncCtx->eSliceType == I_SLICE) {
13151325 if (0 == pWelsSvcRc->iIdrNum) { //iIdrNum == 0 means encoder has been initialed
13161326 RcInitRefreshParameter (pEncCtx);
1317− double dBpp = 0.05;
1318− if ((pDLayerParam->fFrameRate > EPSN) && (pDLayerParam->iVideoWidth && pDLayerParam->iVideoHeight))
1319− dBpp = (double) (pDLayerParam->iSpatialBitrate) / (double) (pDLayerParam->fFrameRate * pDLayerParam->iVideoWidth *
1320− pDLayerParam->iVideoHeight);
1321− pWelsSvcRc->iInitialQp = (int32_t) (50 - dBpp * 10 * 4);
1322− pWelsSvcRc->iInitialQp = WELS_CLIP3 (pWelsSvcRc->iInitialQp, pWelsSvcRc->iMinQp, pWelsSvcRc->iMaxQp);
1323−
1327+ RcInitIdrQp (pEncCtx);
13241328 iLumaQp = pWelsSvcRc->iInitialQp;
13251329 pWelsSvcRc->iTargetBits = static_cast<int32_t> (((double) (pDLayerParam->iSpatialBitrate) / (double) (
13261330 pDLayerParam->fFrameRate) *
13271331 IDR_BITRATE_RATIO));
13281332
13291333 WelsLog (& (pEncCtx->sLogCtx), WELS_LOG_DEBUG,
1330− "[Rc] First IDR iSpatialBitrate = %d,iBufferFullnessSkip = %" PRId64 ",iTargetBits= %d,dBpp = %f,initQp = %d",
1331− pDLayerParam->iSpatialBitrate, pWelsSvcRc->iBufferFullnessSkip, pWelsSvcRc->iTargetBits, dBpp,
1334+ "[Rc] First IDR iSpatialBitrate = %d,iBufferFullnessSkip = %" PRId64 ",iTargetBits= %d,initQp = %d",
1335+ pDLayerParam->iSpatialBitrate, pWelsSvcRc->iBufferFullnessSkip, pWelsSvcRc->iTargetBits,
13321336 pWelsSvcRc->iInitialQp);
13331337
13341338 } else {
@@ -1340,8 +1344,7 @@ void WelsRcPictureInitGomTimeStamp (sWelsEncCtx* pEncCtx, long long uiTimeStamp · 5 unmodified lines
13401344 if (iMaxTh > 0) {
13411345 pWelsSvcRc->iTargetBits = WELS_CLIP3 (pWelsSvcRc->iTargetBits, iMinTh, iMaxTh);
13421346
1343− pWelsSvcRc->iQStep = WELS_DIV_ROUND ((((int64_t)pWelsSvcRc->iIntraComplexity) *
1344− pWelsSvcRc->iCost2BitsIntra), pWelsSvcRc->iTargetBits);
1347+ pWelsSvcRc->iQStep = WELS_DIV_ROUND (pWelsSvcRc->iIntraComplexity, pWelsSvcRc->iTargetBits);
13451348 iLumaQp = RcConvertQStep2Qp (pWelsSvcRc->iQStep);
13461349
13471350 iLumaQp = WELS_CLIP3 (iLumaQp, pWelsSvcRc->iLastCalculatedQScale - DELTA_QP_BGD_THD,
@@ -1373,12 +1376,11 @@ void WelsRcPictureInitGomTimeStamp (sWelsEncCtx* pEncCtx, long long uiTimeStamp · 25 unmodified lines
13731376 pTOverRc->iFrameCmplxMean);
13741377 iCmplxRatio = WELS_CLIP3 (iCmplxRatio, INT_MULTIPLY - FRAME_CMPLX_RATIO_RANGE, INT_MULTIPLY + FRAME_CMPLX_RATIO_RANGE);
13751378
1376−
13771379 pWelsSvcRc->iTargetBits = WELS_DIV_ROUND (pTOverRc->iTlayerWeight * kiGopBits, INT_MULTIPLY * 10 * 2);
13781380 if (iMaxTh > 0) {
13791381 pWelsSvcRc->iTargetBits = WELS_CLIP3 (pWelsSvcRc->iTargetBits, iMinTh, iMaxTh);
13801382 if (0 == pTOverRc->iPFrameNum)
1381− iLumaQp = pWelsSvcRc->iInitialQp;
1383+ iLumaQp = pWelsSvcRc->iInitialQp + DELTA_QP_BGD_THD;
13821384 else {
13831385
13841386 pWelsSvcRc->iQStep = WELS_DIV_ROUND ((pTOverRc->iLinearCmplx * iCmplxRatio), (pWelsSvcRc->iTargetBits * INT_MULTIPLY));

Keyboard shortcuts

General

Show keyboard shortcuts
?
Focus the current search
/

Issues and pull requests

Open filters
f

Issues, pull requests and Inbox

Select next item
j↓
Select previous item
k↑
Open selected item
Enter
Dismiss filters or clear selection
Esc

Repository code

Go to file
t