Showing the changes to codec/encoder/core/src/ratectl.cpp (1 of 6 files). Show all
Unified · Split
| @@ -343,11 +343,11 @@ void RcTraceVGopBitrate (sWelsEncCtx* pEncCtx) { · 342 unmodified lines | ||
| 343 | 343 | int32_t iFrameInVGop = pWelsSvcRc->iFrameCodedInVGop + pWelsSvcRc->iSkipFrameInVGop; |
| 344 | 344 | if (0 != iFrameInVGop) |
| 345 | 345 | 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); | |
| 347 | 347 | if (iTotalBits > 0) { |
| 348 | 348 | iTid = 0; |
| 349 | 349 | 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)); | |
| 351 | 351 | ++ iTid; |
| 352 | 352 | } |
| 353 | 353 | } |
| @@ -625,7 +625,6 @@ void RcGomTargetBits (sWelsEncCtx* pEncCtx, const int32_t kiSliceId) { · 271 unmodified lines | ||
| 625 | 625 | |
| 626 | 626 | iLastGomIndex = pSOverRc->iEndMbSlice / pWelsSvcRc->iNumberMbGom; |
| 627 | 627 | iLeftBits = pSOverRc->iTargetBitsSlice - pSOverRc->iFrameBitsSlice; |
| 628 | − | |
| 629 | 628 | if (iLeftBits <= 0) { |
| 630 | 629 | pSOverRc->iGomTargetBits = 0; |
| 631 | 630 | return; |
| @@ -671,9 +670,8 @@ void RcCalculateGomQp (sWelsEncCtx* pEncCtx, SMB* pCurMb, int32_t iSliceId) { · 39 unmodified lines | ||
| 671 | 670 | else if (iBitsRatio > 11900) //2^(1.5/6)*10000 |
| 672 | 671 | pSOverRc->iCalculatedQpSlice -= 2; |
| 673 | 672 | } |
| 673 | + pSOverRc->iCalculatedQpSlice = WELS_CLIP3 (pSOverRc->iCalculatedQpSlice, pWelsSvcRc->iMinQp, pWelsSvcRc->iMaxQp); | |
| 674 | 674 | |
| 675 | − pSOverRc->iCalculatedQpSlice = WELS_CLIP3 (pSOverRc->iCalculatedQpSlice, | |
| 676 | − pEncCtx->iGlobalQp - pWelsSvcRc->iQpRangeLowerInFrame, pEncCtx->iGlobalQp + pWelsSvcRc->iQpRangeUpperInFrame); | |
| 677 | 675 | if (! (((pEncCtx->pSvcParam->iRCMode == RC_BITRATE_MODE) || (pEncCtx->pSvcParam->iRCMode == RC_TIMESTAMP_MODE)) |
| 678 | 676 | && (pEncCtx->pSvcParam->bEnableFrameSkip == false))) |
| 679 | 677 | 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 | ||
| 790 | 788 | pEncCtx->iContinualSkipFrames++; |
| 791 | 789 | break; |
| 792 | 790 | } |
| 793 | − | |
| 794 | − | |
| 791 | + } | |
| 792 | + if (bSkipMustFlag) { | |
| 793 | + for (int32_t i = 0; i < iSpatialNum; i++) | |
| 794 | + pEncCtx->pWelsSvcRc[i].uiLastTimeStamp = uiTimeStamp; | |
| 795 | 795 | } |
| 796 | 796 | return bSkipMustFlag; |
| 797 | 797 | } |
| @@ -819,7 +819,7 @@ void UpdateBufferWhenFrameSkipped (sWelsEncCtx* pEncCtx, int32_t iSpatialNum) { · 21 unmodified lines | ||
| 819 | 819 | |
| 820 | 820 | } |
| 821 | 821 | pEncCtx->iContinualSkipFrames++; |
| 822 | − if ( ( pEncCtx->iContinualSkipFrames % 3 ) == 0 ) { | |
| 822 | + if ((pEncCtx->iContinualSkipFrames % 3) == 0) { | |
| 823 | 823 | //output a warning when iContinualSkipFrames is large enough, which may indicate subjective quality problem |
| 824 | 824 | //note that here iContinualSkipFrames must be >0, so the log output will be 3/6/.... |
| 825 | 825 | 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 | ||
| 916 | 916 | WelsLog (& (pEncCtx->sLogCtx), WELS_LOG_DEBUG, |
| 917 | 917 | "[Rc]Layer %d: Frame timestamp = %lld, Frame type = %d, encoding_qp = %d, average qp = %d, max qp = %d, min qp = %d, index = %d, " |
| 918 | 918 | "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, | |
| 920 | 921 | pWelsSvcRc->iMinFrameQp, |
| 921 | 922 | pEncCtx->iFrameIndex, pEncCtx->uiTemporalId, pWelsSvcRc->iFrameDqBits, pWelsSvcRc->iBitsPerFrame, |
| 922 | 923 | pWelsSvcRc->iTargetBits, pWelsSvcRc->iRemainingBits, pWelsSvcRc->iBufferSizeSkip); |
| @@ -944,6 +945,7 @@ void RcUpdatePictureQpBits (sWelsEncCtx* pEncCtx, int32_t iCodedBits) { · 21 unmodified lines | ||
| 944 | 945 | pWelsSvcRc->iAverageFrameQp = pEncCtx->iGlobalQp; |
| 945 | 946 | } |
| 946 | 947 | pWelsSvcRc->iFrameDqBits = iCodedBits; |
| 948 | + pWelsSvcRc->iLastCalculatedQScale = pWelsSvcRc->iAverageFrameQp; | |
| 947 | 949 | pWelsSvcRc->pTemporalOverRc[pEncCtx->uiTemporalId].iGopBitsDq += pWelsSvcRc->iFrameDqBits; |
| 948 | 950 | } |
| 949 | 951 | |
| @@ -951,8 +953,8 @@ void RcUpdateIntraComplexity (sWelsEncCtx* pEncCtx) { · 1 unmodified line | ||
| 951 | 953 | SWelsSvcRc* pWelsSvcRc = &pEncCtx->pWelsSvcRc[pEncCtx->uiDependencyId]; |
| 952 | 954 | int32_t iAlpha = WELS_DIV_ROUND (INT_MULTIPLY, (1 + pWelsSvcRc->iIdrNum)); |
| 953 | 955 | 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); | |
| 956 | 958 | pWelsSvcRc->iIntraComplexity = WELS_DIV_ROUND (((INT_MULTIPLY - iAlpha) * pWelsSvcRc->iIntraComplexity + iAlpha * |
| 957 | 959 | iIntraCmplx), INT_MULTIPLY); |
| 958 | 960 | pWelsSvcRc->iIntraMbCount = pWelsSvcRc->iNumberMbFrame; |
| @@ -960,9 +962,10 @@ void RcUpdateIntraComplexity (sWelsEncCtx* pEncCtx) { · 1 unmodified line | ||
| 960 | 962 | pWelsSvcRc->iIdrNum++; |
| 961 | 963 | if (pWelsSvcRc->iIdrNum > 255) |
| 962 | 964 | pWelsSvcRc->iIdrNum = 255; |
| 963 | − WelsLog (& (pEncCtx->sLogCtx), WELS_LOG_INFO, | |
| 965 | + WelsLog (& (pEncCtx->sLogCtx), WELS_LOG_DEBUG, | |
| 964 | 966 | "RcUpdateIntraComplexity iFrameDqBits = %d,iQStep= %d,iIntraCmplx = %" PRId64, |
| 965 | 967 | pWelsSvcRc->iFrameDqBits, pWelsSvcRc->iQStep, pWelsSvcRc->iIntraComplexity); |
| 968 | + | |
| 966 | 969 | } |
| 967 | 970 | |
| 968 | 971 | void RcUpdateFrameComplexity (sWelsEncCtx* pEncCtx) { |
| @@ -970,26 +973,32 @@ void RcUpdateFrameComplexity (sWelsEncCtx* pEncCtx) { · 1 unmodified line | ||
| 970 | 973 | const int32_t kiTl = pEncCtx->uiTemporalId; |
| 971 | 974 | SRCTemporal* pTOverRc = &pWelsSvcRc->pTemporalOverRc[kiTl]; |
| 972 | 975 | |
| 976 | + int32_t iQStep = RcConvertQp2QStep (pWelsSvcRc->iAverageFrameQp); | |
| 973 | 977 | if (0 == pTOverRc->iPFrameNum) { |
| 974 | − pTOverRc->iLinearCmplx = ((int64_t)pWelsSvcRc->iFrameDqBits) * pWelsSvcRc->iQStep; | |
| 978 | + pTOverRc->iLinearCmplx = ((int64_t)pWelsSvcRc->iFrameDqBits) * iQStep; | |
| 975 | 979 | } else { |
| 976 | 980 | 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)), | |
| 978 | 982 | INT_MULTIPLY); |
| 979 | 983 | } |
| 980 | 984 | int32_t iAlpha = WELS_DIV_ROUND (INT_MULTIPLY, (1 + pTOverRc->iPFrameNum)); |
| 981 | 985 | if (iAlpha < SMOOTH_FACTOR_MIN_VALUE) |
| 982 | 986 | 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), | |
| 985 | 990 | INT_MULTIPLY); |
| 986 | 991 | |
| 992 | + | |
| 987 | 993 | pTOverRc->iPFrameNum++; |
| 988 | 994 | if (pTOverRc->iPFrameNum > 255) |
| 989 | 995 | pTOverRc->iPFrameNum = 255; |
| 990 | 996 | 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); | |
| 993 | 1002 | } |
| 994 | 1003 | |
| 995 | 1004 | int32_t RcCalculateCascadingQp (struct TagWelsEncCtx* pEncCtx, int32_t iQp) { |
| @@ -1115,7 +1124,8 @@ void WelsRcPictureInitDisable (sWelsEncCtx* pEncCtx, long long uiTimeStamp) { · 119 unmodified lines | ||
| 1115 | 1124 | |
| 1116 | 1125 | if (pEncCtx->pSvcParam->bEnableAdaptiveQuant && (pEncCtx->eSliceType == P_SLICE)) { |
| 1117 | 1126 | 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); | |
| 1119 | 1129 | } else { |
| 1120 | 1130 | pEncCtx->iGlobalQp = WELS_CLIP3 (pEncCtx->iGlobalQp, 0, 51); |
| 1121 | 1131 | } |
| @@ -1231,7 +1241,7 @@ void WelsRcDropFrameUpdate (sWelsEncCtx* pEncCtx, uint32_t iDropSize) { · 109 unmodified lines | ||
| 1231 | 1241 | |
| 1232 | 1242 | pWelsSvcRc->iBufferFullnessSkip -= (int32_t)iDropSize; |
| 1233 | 1243 | 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, | |
| 1235 | 1245 | pWelsSvcRc->iBufferFullnessSkip); |
| 1236 | 1246 | } |
| 1237 | 1247 | |
| @@ -1266,8 +1276,8 @@ void InitRcModuleTimeStamp (sWelsEncCtx* pEncCtx) { · 28 unmodified lines | ||
| 1266 | 1276 | pWelsSvcRc->iBufferFullnessSkip = 0; |
| 1267 | 1277 | pWelsSvcRc->uiLastTimeStamp = 0; |
| 1268 | 1278 | |
| 1269 | − pWelsSvcRc->iCost2BitsIntra = INT_MULTIPLY; | |
| 1270 | − pWelsSvcRc->iAvgCost2Bits = INT_MULTIPLY; | |
| 1279 | + pWelsSvcRc->iCost2BitsIntra = 1; | |
| 1280 | + pWelsSvcRc->iAvgCost2Bits = 1; | |
| 1271 | 1281 | pWelsSvcRc->iSkipBufferRatio = SKIP_RATIO; |
| 1272 | 1282 | } |
| 1273 | 1283 | void WelsRcFrameDelayJudgeTimeStamp (sWelsEncCtx* pEncCtx, EVideoFrameType eFrameType, long long uiTimeStamp) { |
| @@ -1314,21 +1324,15 @@ void WelsRcPictureInitGomTimeStamp (sWelsEncCtx* pEncCtx, long long uiTimeStamp · 40 unmodified lines | ||
| 1314 | 1324 | if (pEncCtx->eSliceType == I_SLICE) { |
| 1315 | 1325 | if (0 == pWelsSvcRc->iIdrNum) { //iIdrNum == 0 means encoder has been initialed |
| 1316 | 1326 | 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); | |
| 1324 | 1328 | iLumaQp = pWelsSvcRc->iInitialQp; |
| 1325 | 1329 | pWelsSvcRc->iTargetBits = static_cast<int32_t> (((double) (pDLayerParam->iSpatialBitrate) / (double) ( |
| 1326 | 1330 | pDLayerParam->fFrameRate) * |
| 1327 | 1331 | IDR_BITRATE_RATIO)); |
| 1328 | 1332 | |
| 1329 | 1333 | 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, | |
| 1332 | 1336 | pWelsSvcRc->iInitialQp); |
| 1333 | 1337 | |
| 1334 | 1338 | } else { |
| @@ -1340,8 +1344,7 @@ void WelsRcPictureInitGomTimeStamp (sWelsEncCtx* pEncCtx, long long uiTimeStamp · 5 unmodified lines | ||
| 1340 | 1344 | if (iMaxTh > 0) { |
| 1341 | 1345 | pWelsSvcRc->iTargetBits = WELS_CLIP3 (pWelsSvcRc->iTargetBits, iMinTh, iMaxTh); |
| 1342 | 1346 | |
| 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); | |
| 1345 | 1348 | iLumaQp = RcConvertQStep2Qp (pWelsSvcRc->iQStep); |
| 1346 | 1349 | |
| 1347 | 1350 | iLumaQp = WELS_CLIP3 (iLumaQp, pWelsSvcRc->iLastCalculatedQScale - DELTA_QP_BGD_THD, |
| @@ -1373,12 +1376,11 @@ void WelsRcPictureInitGomTimeStamp (sWelsEncCtx* pEncCtx, long long uiTimeStamp · 25 unmodified lines | ||
| 1373 | 1376 | pTOverRc->iFrameCmplxMean); |
| 1374 | 1377 | iCmplxRatio = WELS_CLIP3 (iCmplxRatio, INT_MULTIPLY - FRAME_CMPLX_RATIO_RANGE, INT_MULTIPLY + FRAME_CMPLX_RATIO_RANGE); |
| 1375 | 1378 | |
| 1376 | − | |
| 1377 | 1379 | pWelsSvcRc->iTargetBits = WELS_DIV_ROUND (pTOverRc->iTlayerWeight * kiGopBits, INT_MULTIPLY * 10 * 2); |
| 1378 | 1380 | if (iMaxTh > 0) { |
| 1379 | 1381 | pWelsSvcRc->iTargetBits = WELS_CLIP3 (pWelsSvcRc->iTargetBits, iMinTh, iMaxTh); |
| 1380 | 1382 | if (0 == pTOverRc->iPFrameNum) |
| 1381 | − iLumaQp = pWelsSvcRc->iInitialQp; | |
| 1383 | + iLumaQp = pWelsSvcRc->iInitialQp + DELTA_QP_BGD_THD; | |
| 1382 | 1384 | else { |
| 1383 | 1385 | |
| 1384 | 1386 | pWelsSvcRc->iQStep = WELS_DIV_ROUND ((pTOverRc->iLinearCmplx * iCmplxRatio), (pWelsSvcRc->iTargetBits * INT_MULTIPLY)); |