Showing the changes to codec/encoder/core/src/ratectl.cpp (1 of 5 files). Show all
Unified · Split
| @@ -390,7 +390,8 @@ void RcInitIdrQp (sWelsEncCtx* pEncCtx) { · 389 unmodified lines | |||
| 390 | SSpatialLayerConfig* pDLayerParam = &pEncCtx->pSvcParam->sSpatialLayers[pEncCtx->uiDependencyId]; | 390 | SSpatialLayerConfig* pDLayerParam = &pEncCtx->pSvcParam->sSpatialLayers[pEncCtx->uiDependencyId]; |
| 391 | SSpatialLayerInternal* pDLayerParamInternal = &pEncCtx->pSvcParam->sDependencyLayers[pEncCtx->uiDependencyId]; | 391 | SSpatialLayerInternal* pDLayerParamInternal = &pEncCtx->pSvcParam->sDependencyLayers[pEncCtx->uiDependencyId]; |
| 392 | if (pDLayerParamInternal->fOutputFrameRate > EPSN && pDLayerParam->iVideoWidth && pDLayerParam->iVideoHeight) | 392 | if (pDLayerParamInternal->fOutputFrameRate > EPSN && pDLayerParam->iVideoWidth && pDLayerParam->iVideoHeight) |
| 393 | dBpp = (double) (pDLayerParam->iSpatialBitrate) / (double) (pDLayerParamInternal->fOutputFrameRate * pDLayerParam->iVideoWidth * | 393 | dBpp = (double) (pDLayerParam->iSpatialBitrate) / (double) (pDLayerParamInternal->fOutputFrameRate * |
| 394 | pDLayerParam->iVideoWidth * | ||
| 394 | pDLayerParam->iVideoHeight); | 395 | pDLayerParam->iVideoHeight); |
| 395 | else | 396 | else |
| 396 | dBpp = 0.1; | 397 | dBpp = 0.1; |
| @@ -672,7 +673,8 @@ void RcCalculateGomQp (sWelsEncCtx* pEncCtx, SMB* pCurMb, int32_t iSliceId) { · 275 unmodified lines | |||
| 672 | 673 | ||
| 673 | pSOverRc->iCalculatedQpSlice = WELS_CLIP3 (pSOverRc->iCalculatedQpSlice, | 674 | pSOverRc->iCalculatedQpSlice = WELS_CLIP3 (pSOverRc->iCalculatedQpSlice, |
| 674 | pEncCtx->iGlobalQp - pWelsSvcRc->iQpRangeLowerInFrame, pEncCtx->iGlobalQp + pWelsSvcRc->iQpRangeUpperInFrame); | 675 | pEncCtx->iGlobalQp - pWelsSvcRc->iQpRangeLowerInFrame, pEncCtx->iGlobalQp + pWelsSvcRc->iQpRangeUpperInFrame); |
| 675 | if (! ((pEncCtx->pSvcParam->iRCMode == RC_BITRATE_MODE) && (pEncCtx->pSvcParam->bEnableFrameSkip == false))) | 676 | if (! (((pEncCtx->pSvcParam->iRCMode == RC_BITRATE_MODE) || (pEncCtx->pSvcParam->iRCMode == RC_TIMESTAMP_MODE)) |
| 677 | && (pEncCtx->pSvcParam->bEnableFrameSkip == false))) | ||
| 676 | pSOverRc->iCalculatedQpSlice = WELS_CLIP3 (pSOverRc->iCalculatedQpSlice, pWelsSvcRc->iMinQp, pWelsSvcRc->iMaxQp); | 678 | pSOverRc->iCalculatedQpSlice = WELS_CLIP3 (pSOverRc->iCalculatedQpSlice, pWelsSvcRc->iMinQp, pWelsSvcRc->iMaxQp); |
| 677 | 679 | ||
| 678 | pSOverRc->iGomBitsSlice = 0; | 680 | pSOverRc->iGomBitsSlice = 0; |
| @@ -705,12 +707,12 @@ void RcVBufferCalculationSkip (sWelsEncCtx* pEncCtx) { · 26 unmodified lines | |||
| 705 | pEncCtx->iSkipFrameFlag = 1; | 707 | pEncCtx->iSkipFrameFlag = 1; |
| 706 | } | 708 | } |
| 707 | } | 709 | } |
| 708 | |||
| 709 | void WelsRcFrameDelayJudge (sWelsEncCtx* pEncCtx, EVideoFrameType eFrameType, long long uiTimeStamp) { | 710 | void WelsRcFrameDelayJudge (sWelsEncCtx* pEncCtx, EVideoFrameType eFrameType, long long uiTimeStamp) { |
| 710 | SWelsSvcRc* pWelsSvcRc = &pEncCtx->pWelsSvcRc[pEncCtx->uiDependencyId]; | 711 | SWelsSvcRc* pWelsSvcRc = &pEncCtx->pWelsSvcRc[pEncCtx->uiDependencyId]; |
| 711 | SSpatialLayerConfig* pDLayerParam = &pEncCtx->pSvcParam->sSpatialLayers[pEncCtx->uiDependencyId]; | 712 | SSpatialLayerConfig* pDLayerParam = &pEncCtx->pSvcParam->sSpatialLayers[pEncCtx->uiDependencyId]; |
| 712 | SSpatialLayerInternal* pDLayerParamInternal = &pEncCtx->pSvcParam->sDependencyLayers[pEncCtx->uiDependencyId]; | 713 | SSpatialLayerInternal* pDLayerParamInternal = &pEncCtx->pSvcParam->sDependencyLayers[pEncCtx->uiDependencyId]; |
| 713 | 714 | if (!pEncCtx->pSvcParam->bEnableFrameSkip) | |
| 715 | return; | ||
| 714 | const int32_t iSentBits = WELS_ROUND (pDLayerParam->iSpatialBitrate / pDLayerParamInternal->fOutputFrameRate); | 716 | const int32_t iSentBits = WELS_ROUND (pDLayerParam->iSpatialBitrate / pDLayerParamInternal->fOutputFrameRate); |
| 715 | const int32_t kiOutputMaxBits = WELS_DIV_ROUND (pWelsSvcRc->iMaxBitsPerFrame, INT_MULTIPLY); | 717 | const int32_t kiOutputMaxBits = WELS_DIV_ROUND (pWelsSvcRc->iMaxBitsPerFrame, INT_MULTIPLY); |
| 716 | 718 | ||
| @@ -772,22 +774,22 @@ bool CheckFrameSkipBasedMaxbr (sWelsEncCtx* pEncCtx, int32_t iSpatialNum, EVideo · 55 unmodified lines | |||
| 772 | const uint32_t uiTimeStamp) { | 774 | const uint32_t uiTimeStamp) { |
| 773 | SSpatialPicIndex* pSpatialIndexMap = &pEncCtx->sSpatialIndexMap[0]; | 775 | SSpatialPicIndex* pSpatialIndexMap = &pEncCtx->sSpatialIndexMap[0]; |
| 774 | bool bSkipMustFlag = false; | 776 | bool bSkipMustFlag = false; |
| 775 | if (pEncCtx->pSvcParam->bEnableFrameSkip) { | 777 | if (!pEncCtx->pFuncList->pfRc.pfWelsRcPicDelayJudge) |
| 776 | if ((RC_QUALITY_MODE == pEncCtx->pSvcParam->iRCMode) || (RC_BITRATE_MODE == pEncCtx->pSvcParam->iRCMode)|| (RC_TIMESTAMP_MODE == pEncCtx->pSvcParam->iRCMode)) { | 778 | return false; |
| 777 | 779 | for (int32_t i = 0; i < iSpatialNum; i++) { | |
| 778 | for (int32_t i = 0; i < iSpatialNum; i++) { | 780 | if (UNSPECIFIED_BIT_RATE == pEncCtx->pSvcParam->sSpatialLayers[i].iMaxSpatialBitrate) { |
| 779 | if (UNSPECIFIED_BIT_RATE == pEncCtx->pSvcParam->sSpatialLayers[i].iMaxSpatialBitrate) { | 781 | break; |
| 780 | break; | ||
| 781 | } | ||
| 782 | pEncCtx->uiDependencyId = (uint8_t) (pSpatialIndexMap + i)->iDid; | ||
| 783 | pEncCtx->pFuncList->pfRc.pfWelsRcPicDelayJudge (pEncCtx, eFrameType, uiTimeStamp); | ||
| 784 | if (true == pEncCtx->pWelsSvcRc[pEncCtx->uiDependencyId].bSkipFlag) { | ||
| 785 | bSkipMustFlag = true; | ||
| 786 | pEncCtx->iContinualSkipFrames++; | ||
| 787 | break; | ||
| 788 | } | ||
| 789 | } | ||
| 790 | } | 782 | } |
| 783 | pEncCtx->uiDependencyId = (uint8_t) (pSpatialIndexMap + i)->iDid; | ||
| 784 | |||
| 785 | pEncCtx->pFuncList->pfRc.pfWelsRcPicDelayJudge (pEncCtx, eFrameType, uiTimeStamp); | ||
| 786 | if (true == pEncCtx->pWelsSvcRc[pEncCtx->uiDependencyId].bSkipFlag) { | ||
| 787 | bSkipMustFlag = true; | ||
| 788 | pEncCtx->iContinualSkipFrames++; | ||
| 789 | break; | ||
| 790 | } | ||
| 791 | |||
| 792 | |||
| 791 | } | 793 | } |
| 792 | return bSkipMustFlag; | 794 | return bSkipMustFlag; |
| 793 | } | 795 | } |
| @@ -815,7 +817,6 @@ void UpdateBufferWhenFrameSkipped (sWelsEncCtx* pEncCtx, int32_t iSpatialNum) { · 21 unmodified lines | |||
| 815 | } | 817 | } |
| 816 | pEncCtx->iContinualSkipFrames++; | 818 | pEncCtx->iContinualSkipFrames++; |
| 817 | } | 819 | } |
| 818 | |||
| 819 | void UpdateMaxBrCheckWindowStatus (sWelsEncCtx* pEncCtx, int32_t iSpatialNum, const long long uiTimeStamp) { | 820 | void UpdateMaxBrCheckWindowStatus (sWelsEncCtx* pEncCtx, int32_t iSpatialNum, const long long uiTimeStamp) { |
| 820 | SSpatialPicIndex* pSpatialIndexMap = &pEncCtx->sSpatialIndexMap[0]; | 821 | SSpatialPicIndex* pSpatialIndexMap = &pEncCtx->sSpatialIndexMap[0]; |
| 821 | if (pEncCtx->bCheckWindowStatusRefreshFlag) { | 822 | if (pEncCtx->bCheckWindowStatusRefreshFlag) { |
| @@ -885,10 +886,9 @@ void RcTraceFrameBits (sWelsEncCtx* pEncCtx, long long uiTimeStamp) { · 63 unmodified lines | |||
| 885 | pWelsSvcRc->iPredFrameBit = pWelsSvcRc->iFrameDqBits; | 886 | pWelsSvcRc->iPredFrameBit = pWelsSvcRc->iFrameDqBits; |
| 886 | 887 | ||
| 887 | WelsLog (& (pEncCtx->sLogCtx), WELS_LOG_DEBUG, | 888 | WelsLog (& (pEncCtx->sLogCtx), WELS_LOG_DEBUG, |
| 888 | "[Rc] Frame timestamp = %lld, Frame type =%d, dependency ID = %d, encoding_qp =%d, average qp = %3d, max qp = %3d, min qp = %3d, index = %8d,\ | 889 | "[Rc] Frame timestamp = %lld, Frame type =%d, encoding_qp = %d, average qp = %3d, max qp = %3d, min qp = %3d, index = %8d,\ |
| 889 | iTid = %1d, used = %8d, bitsperframe = %8d, target = %8d, remaingbits = %8d, skipbuffersize = %8d", | 890 | iTid = %1d, used = %8d, bitsperframe = %8d, target = %8d, remaingbits = %8d, skipbuffersize = %8d", |
| 890 | uiTimeStamp, pEncCtx->eSliceType, pEncCtx->uiDependencyId, pEncCtx->iGlobalQp, pWelsSvcRc->iAverageFrameQp, | 891 | uiTimeStamp, pEncCtx->eSliceType, pEncCtx->iGlobalQp, pWelsSvcRc->iAverageFrameQp, pWelsSvcRc->iMaxFrameQp, |
| 891 | pWelsSvcRc->iMaxFrameQp, | ||
| 892 | pWelsSvcRc->iMinFrameQp, | 892 | pWelsSvcRc->iMinFrameQp, |
| 893 | pEncCtx->iFrameIndex, pEncCtx->uiTemporalId, pWelsSvcRc->iFrameDqBits, WELS_DIV_ROUND (pWelsSvcRc->iBitsPerFrame, | 893 | pEncCtx->iFrameIndex, pEncCtx->uiTemporalId, pWelsSvcRc->iFrameDqBits, WELS_DIV_ROUND (pWelsSvcRc->iBitsPerFrame, |
| 894 | INT_MULTIPLY), | 894 | INT_MULTIPLY), |
| @@ -933,6 +933,8 @@ void RcUpdateIntraComplexity (sWelsEncCtx* pEncCtx) { · 38 unmodified lines | |||
| 933 | pWelsSvcRc->iIdrNum++; | 933 | pWelsSvcRc->iIdrNum++; |
| 934 | if (pWelsSvcRc->iIdrNum > 255) | 934 | if (pWelsSvcRc->iIdrNum > 255) |
| 935 | pWelsSvcRc->iIdrNum = 255; | 935 | pWelsSvcRc->iIdrNum = 255; |
| 936 | WelsLog (& (pEncCtx->sLogCtx), WELS_LOG_INFO, "RcUpdateIntraComplexity iFrameDqBits = %d,iQStep= %d,iIntraCmplx = %d", | ||
| 937 | pWelsSvcRc->iFrameDqBits, pWelsSvcRc->iQStep, pWelsSvcRc->iIntraComplexity); | ||
| 936 | } | 938 | } |
| 937 | 939 | ||
| 938 | void RcUpdateFrameComplexity (sWelsEncCtx* pEncCtx) { | 940 | void RcUpdateFrameComplexity (sWelsEncCtx* pEncCtx) { |
| @@ -956,6 +958,9 @@ void RcUpdateFrameComplexity (sWelsEncCtx* pEncCtx) { · 17 unmodified lines | |||
| 956 | pTOverRc->iPFrameNum++; | 958 | pTOverRc->iPFrameNum++; |
| 957 | if (pTOverRc->iPFrameNum > 255) | 959 | if (pTOverRc->iPFrameNum > 255) |
| 958 | pTOverRc->iPFrameNum = 255; | 960 | pTOverRc->iPFrameNum = 255; |
| 961 | WelsLog (& (pEncCtx->sLogCtx), WELS_LOG_DEBUG, | ||
| 962 | "RcUpdateFrameComplexity iFrameDqBits = %d,iQStep= %d,pTOverRc->iLinearCmplx = %lld", pWelsSvcRc->iFrameDqBits, | ||
| 963 | pWelsSvcRc->iQStep, pTOverRc->iLinearCmplx); | ||
| 959 | } | 964 | } |
| 960 | 965 | ||
| 961 | int32_t RcCalculateCascadingQp (struct TagWelsEncCtx* pEncCtx, int32_t iQp) { | 966 | int32_t RcCalculateCascadingQp (struct TagWelsEncCtx* pEncCtx, int32_t iQp) { |
| @@ -971,7 +976,7 @@ int32_t RcCalculateCascadingQp (struct TagWelsEncCtx* pEncCtx, int32_t iQp) { · 9 unmodified lines | |||
| 971 | return iTemporalQp; | 976 | return iTemporalQp; |
| 972 | } | 977 | } |
| 973 | 978 | ||
| 974 | void WelsRcPictureInitGom (sWelsEncCtx* pEncCtx) { | 979 | void WelsRcPictureInitGom (sWelsEncCtx* pEncCtx, long long uiTimeStamp) { |
| 975 | SWelsSvcRc* pWelsSvcRc = &pEncCtx->pWelsSvcRc[pEncCtx->uiDependencyId]; | 980 | SWelsSvcRc* pWelsSvcRc = &pEncCtx->pWelsSvcRc[pEncCtx->uiDependencyId]; |
| 976 | 981 | ||
| 977 | if (pEncCtx->eSliceType == I_SLICE) { | 982 | if (pEncCtx->eSliceType == I_SLICE) { |
| @@ -1001,8 +1006,6 @@ void WelsRcPictureInitGom (sWelsEncCtx* pEncCtx) { · 23 unmodified lines | |||
| 1001 | 1006 | ||
| 1002 | } | 1007 | } |
| 1003 | 1008 | ||
| 1004 | |||
| 1005 | |||
| 1006 | void WelsRcPictureInfoUpdateGom (sWelsEncCtx* pEncCtx, int32_t iLayerSize) { | 1009 | void WelsRcPictureInfoUpdateGom (sWelsEncCtx* pEncCtx, int32_t iLayerSize) { |
| 1007 | SWelsSvcRc* pWelsSvcRc = &pEncCtx->pWelsSvcRc[pEncCtx->uiDependencyId]; | 1010 | SWelsSvcRc* pWelsSvcRc = &pEncCtx->pWelsSvcRc[pEncCtx->uiDependencyId]; |
| 1008 | int32_t iCodedBits = (iLayerSize << 3); | 1011 | int32_t iCodedBits = (iLayerSize << 3); |
| @@ -1074,7 +1077,7 @@ void WelsRcMbInfoUpdateGom (sWelsEncCtx* pEncCtx, SMB* pCurMb, int32_t iCostLuma · 65 unmodified lines | |||
| 1074 | } | 1077 | } |
| 1075 | } | 1078 | } |
| 1076 | 1079 | ||
| 1077 | void WelsRcPictureInitDisable (sWelsEncCtx* pEncCtx) { | 1080 | void WelsRcPictureInitDisable (sWelsEncCtx* pEncCtx, long long uiTimeStamp) { |
| 1078 | SWelsSvcRc* pWelsSvcRc = &pEncCtx->pWelsSvcRc[pEncCtx->uiDependencyId]; | 1081 | SWelsSvcRc* pWelsSvcRc = &pEncCtx->pWelsSvcRc[pEncCtx->uiDependencyId]; |
| 1079 | SSpatialLayerConfig* pDLayerParam = &pEncCtx->pSvcParam->sSpatialLayers[pEncCtx->uiDependencyId]; | 1082 | SSpatialLayerConfig* pDLayerParam = &pEncCtx->pSvcParam->sSpatialLayers[pEncCtx->uiDependencyId]; |
| 1080 | const int32_t kiQp = pDLayerParam->iDLayerQp; | 1083 | const int32_t kiQp = pDLayerParam->iDLayerQp; |
| @@ -1114,7 +1117,7 @@ void WelsRcMbInitDisable (sWelsEncCtx* pEncCtx, SMB* pCurMb, SSlice* pSlice) { · 33 unmodified lines | |||
| 1114 | void WelsRcMbInfoUpdateDisable (sWelsEncCtx* pEncCtx, SMB* pCurMb, int32_t iCostLuma, SSlice* pSlice) { | 1117 | void WelsRcMbInfoUpdateDisable (sWelsEncCtx* pEncCtx, SMB* pCurMb, int32_t iCostLuma, SSlice* pSlice) { |
| 1115 | } | 1118 | } |
| 1116 | 1119 | ||
| 1117 | void WelRcPictureInitBufferBasedQp (sWelsEncCtx* pEncCtx) { | 1120 | void WelRcPictureInitBufferBasedQp (sWelsEncCtx* pEncCtx, long long uiTimeStamp) { |
| 1118 | SVAAFrameInfo* pVaa = static_cast<SVAAFrameInfo*> (pEncCtx->pVaa); | 1121 | SVAAFrameInfo* pVaa = static_cast<SVAAFrameInfo*> (pEncCtx->pVaa); |
| 1119 | 1122 | ||
| 1120 | int32_t iMinQp = MIN_SCREEN_QP; | 1123 | int32_t iMinQp = MIN_SCREEN_QP; |
| @@ -1130,18 +1133,7 @@ void WelRcPictureInitBufferBasedQp (sWelsEncCtx* pEncCtx) { · 9 unmodified lines | |||
| 1130 | pEncCtx->iGlobalQp += 2; | 1133 | pEncCtx->iGlobalQp += 2; |
| 1131 | pEncCtx->iGlobalQp = WELS_CLIP3 (pEncCtx->iGlobalQp, iMinQp, MAX_SCREEN_QP); | 1134 | pEncCtx->iGlobalQp = WELS_CLIP3 (pEncCtx->iGlobalQp, iMinQp, MAX_SCREEN_QP); |
| 1132 | } | 1135 | } |
| 1133 | 1136 | void WelRcPictureInitScc (sWelsEncCtx* pEncCtx, long long uiTimeStamp) { | |
| 1134 | void InitRcModuleScc (sWelsEncCtx* pEncCtx) { | ||
| 1135 | SWelsSvcRc* pWelsSvcRc = &pEncCtx->pWelsSvcRc[pEncCtx->uiDependencyId]; | ||
| 1136 | pWelsSvcRc->iBaseQp = 30; | ||
| 1137 | |||
| 1138 | pWelsSvcRc->iBufferFullnessSkip = 0; | ||
| 1139 | pWelsSvcRc->uiLastTimeStamp = 0; | ||
| 1140 | |||
| 1141 | pWelsSvcRc->iCost2BitsIntra = INT_MULTIPLY; | ||
| 1142 | pWelsSvcRc->iAvgCost2Bits = INT_MULTIPLY; | ||
| 1143 | } | ||
| 1144 | void WelRcPictureInitScc (sWelsEncCtx* pEncCtx) { | ||
| 1145 | SWelsSvcRc* pWelsSvcRc = &pEncCtx->pWelsSvcRc[pEncCtx->uiDependencyId]; | 1137 | SWelsSvcRc* pWelsSvcRc = &pEncCtx->pWelsSvcRc[pEncCtx->uiDependencyId]; |
| 1146 | SVAAFrameInfoExt* pVaa = static_cast<SVAAFrameInfoExt*> (pEncCtx->pVaa); | 1138 | SVAAFrameInfoExt* pVaa = static_cast<SVAAFrameInfoExt*> (pEncCtx->pVaa); |
| 1147 | SSpatialLayerConfig* pDLayerConfig = &pEncCtx->pSvcParam->sSpatialLayers[pEncCtx->uiDependencyId]; | 1139 | SSpatialLayerConfig* pDLayerConfig = &pEncCtx->pSvcParam->sSpatialLayers[pEncCtx->uiDependencyId]; |
| @@ -1199,40 +1191,9 @@ void WelRcPictureInitScc (sWelsEncCtx* pEncCtx) { · 51 unmodified lines | |||
| 1199 | } | 1191 | } |
| 1200 | pWelsSvcRc->iAverageFrameQp = pEncCtx->iGlobalQp; | 1192 | pWelsSvcRc->iAverageFrameQp = pEncCtx->iGlobalQp; |
| 1201 | WelsLog (& (pEncCtx->sLogCtx), WELS_LOG_DEBUG, "WelRcPictureInitScc iLumaQp = %d\n", pEncCtx->iGlobalQp); | 1193 | WelsLog (& (pEncCtx->sLogCtx), WELS_LOG_DEBUG, "WelRcPictureInitScc iLumaQp = %d\n", pEncCtx->iGlobalQp); |
| 1202 | |||
| 1203 | } | ||
| 1204 | void WelsRcFrameDelayJudgeScc (sWelsEncCtx* pEncCtx, EVideoFrameType eFrameType, long long uiTimeStamp) { | ||
| 1205 | SWelsSvcRc* pWelsSvcRc = &pEncCtx->pWelsSvcRc[0]; | ||
| 1206 | SSpatialLayerConfig* pDLayerConfig = &pEncCtx->pSvcParam->sSpatialLayers[pEncCtx->uiDependencyId]; | ||
| 1207 | |||
| 1208 | if (pDLayerConfig->iSpatialBitrate > pDLayerConfig->iMaxSpatialBitrate) | ||
| 1209 | pDLayerConfig->iSpatialBitrate = pDLayerConfig->iMaxSpatialBitrate; | ||
| 1210 | |||
| 1211 | int32_t iBitRate = pDLayerConfig->iSpatialBitrate; | ||
| 1212 | int32_t iEncTimeInv = (pWelsSvcRc->uiLastTimeStamp == 0) ? 0 : (int32_t) (uiTimeStamp - pWelsSvcRc->uiLastTimeStamp); | ||
| 1213 | int32_t iSentBits = (int32_t) ((double)iBitRate * iEncTimeInv * (1.0E-3) + 0.5); | ||
| 1214 | iSentBits = WELS_MAX (iSentBits, 0); | ||
| 1215 | |||
| 1216 | const int32_t iVbufferThRatio = (eFrameType == videoFrameTypeI | ||
| 1217 | || eFrameType == videoFrameTypeIDR) ? VIRTUAL_BUFFER_HIGH_TH : VIRTUAL_BUFFER_LOW_TH; | ||
| 1218 | const int32_t iVbufferTh = WELS_DIV_ROUND ((((int64_t)iBitRate) * iVbufferThRatio), INT_MULTIPLY); | ||
| 1219 | |||
| 1220 | pWelsSvcRc->iBufferFullnessSkip -= iSentBits; | ||
| 1221 | pWelsSvcRc->iBufferFullnessSkip = WELS_MAX (0, pWelsSvcRc->iBufferFullnessSkip); | ||
| 1222 | |||
| 1223 | pWelsSvcRc->bSkipFlag = true; | ||
| 1224 | if (pWelsSvcRc->iBufferFullnessSkip < iVbufferTh) { | ||
| 1225 | pWelsSvcRc->bSkipFlag = false; | ||
| 1226 | } | ||
| 1227 | if (pWelsSvcRc->bSkipFlag) { | ||
| 1228 | pWelsSvcRc->iSkipFrameNum++; | ||
| 1229 | WelsLog (& (pEncCtx->sLogCtx), WELS_LOG_INFO, | ||
| 1230 | "SCC iSkipFrameNum = %d,buffer = %d,threadhold = %d,bitrate = %d,timestamp=%lld\n", pWelsSvcRc->iSkipFrameNum, | ||
| 1231 | pWelsSvcRc->iBufferFullnessSkip, iVbufferTh, iBitRate, uiTimeStamp); | ||
| 1232 | } | ||
| 1233 | pWelsSvcRc->uiLastTimeStamp = uiTimeStamp; | 1194 | pWelsSvcRc->uiLastTimeStamp = uiTimeStamp; |
| 1234 | } | ||
| 1235 | 1195 | ||
| 1196 | } | ||
| 1236 | void WelsRcDropFrameUpdate (sWelsEncCtx* pEncCtx, uint32_t iDropSize) { | 1197 | void WelsRcDropFrameUpdate (sWelsEncCtx* pEncCtx, uint32_t iDropSize) { |
| 1237 | SWelsSvcRc* pWelsSvcRc = &pEncCtx->pWelsSvcRc[0]; | 1198 | SWelsSvcRc* pWelsSvcRc = &pEncCtx->pWelsSvcRc[0]; |
| 1238 | 1199 | ||
| @@ -1243,7 +1204,7 @@ void WelsRcDropFrameUpdate (sWelsEncCtx* pEncCtx, uint32_t iDropSize) { · 4 unmodified lines | |||
| 1243 | } | 1204 | } |
| 1244 | 1205 | ||
| 1245 | void WelsRcPictureInfoUpdateScc (sWelsEncCtx* pEncCtx, int32_t iNalSize) { | 1206 | void WelsRcPictureInfoUpdateScc (sWelsEncCtx* pEncCtx, int32_t iNalSize) { |
| 1246 | SWelsSvcRc* pWelsSvcRc = &pEncCtx->pWelsSvcRc[0]; | 1207 | SWelsSvcRc* pWelsSvcRc = &pEncCtx->pWelsSvcRc[pEncCtx->uiDependencyId]; |
| 1247 | int32_t iFrameBits = (iNalSize << 3); | 1208 | int32_t iFrameBits = (iNalSize << 3); |
| 1248 | pWelsSvcRc->iBufferFullnessSkip += iFrameBits; | 1209 | pWelsSvcRc->iBufferFullnessSkip += iFrameBits; |
| 1249 | 1210 | ||
| @@ -1266,9 +1227,178 @@ void WelsRcMbInitScc (sWelsEncCtx* pEncCtx, SMB* pCurMb, SSlice* pSlice) { · 16 unmodified lines | |||
| 1266 | pCurMb->uiChromaQp = g_kuiChromaQpTable[WELS_CLIP3 (pCurMb->uiLumaQp + pEncCtx->pPps->uiChromaQpIndexOffset, 0, 51)]; | 1227 | pCurMb->uiChromaQp = g_kuiChromaQpTable[WELS_CLIP3 (pCurMb->uiLumaQp + pEncCtx->pPps->uiChromaQpIndexOffset, 0, 51)]; |
| 1267 | } | 1228 | } |
| 1268 | 1229 | ||
| 1230 | void InitRcModuleTimeStamp (sWelsEncCtx* pEncCtx) { | ||
| 1231 | SWelsSvcRc* pWelsSvcRc = &pEncCtx->pWelsSvcRc[pEncCtx->uiDependencyId]; | ||
| 1232 | pWelsSvcRc->iBaseQp = 30; | ||
| 1233 | |||
| 1234 | pWelsSvcRc->iBufferFullnessSkip = 0; | ||
| 1235 | pWelsSvcRc->uiLastTimeStamp = 0; | ||
| 1236 | |||
| 1237 | pWelsSvcRc->iCost2BitsIntra = INT_MULTIPLY; | ||
| 1238 | pWelsSvcRc->iAvgCost2Bits = INT_MULTIPLY; | ||
| 1239 | pWelsSvcRc->iSkipBufferRatio = SKIP_RATIO; | ||
| 1240 | } | ||
| 1241 | void WelsRcFrameDelayJudgeTimeStamp (sWelsEncCtx* pEncCtx, EVideoFrameType eFrameType, long long uiTimeStamp) { | ||
| 1242 | SWelsSvcRc* pWelsSvcRc = &pEncCtx->pWelsSvcRc[pEncCtx->uiDependencyId]; | ||
| 1243 | SSpatialLayerConfig* pDLayerConfig = &pEncCtx->pSvcParam->sSpatialLayers[pEncCtx->uiDependencyId]; | ||
| 1244 | |||
| 1245 | if (pDLayerConfig->iSpatialBitrate > pDLayerConfig->iMaxSpatialBitrate) | ||
| 1246 | pDLayerConfig->iSpatialBitrate = pDLayerConfig->iMaxSpatialBitrate; | ||
| 1247 | |||
| 1248 | int32_t iBitRate = pDLayerConfig->iSpatialBitrate; | ||
| 1249 | int32_t iEncTimeInv = (pWelsSvcRc->uiLastTimeStamp == 0) ? 0 : (int32_t) (uiTimeStamp - pWelsSvcRc->uiLastTimeStamp); | ||
| 1250 | int32_t iSentBits = (int32_t) ((double)iBitRate * iEncTimeInv * (1.0E-3) + 0.5); | ||
| 1251 | iSentBits = WELS_MAX (iSentBits, 0); | ||
| 1252 | |||
| 1253 | //When bitrate is changed, pBuffer size should be updated | ||
| 1254 | pWelsSvcRc->iBufferSizeSkip = WELS_DIV_ROUND (pDLayerConfig->iSpatialBitrate * pWelsSvcRc->iSkipBufferRatio, | ||
| 1255 | INT_MULTIPLY); | ||
| 1256 | pWelsSvcRc->iBufferSizePadding = WELS_DIV_ROUND (pDLayerConfig->iSpatialBitrate * PADDING_BUFFER_RATIO, INT_MULTIPLY); | ||
| 1257 | |||
| 1258 | pWelsSvcRc->iBufferFullnessSkip -= iSentBits; | ||
| 1259 | pWelsSvcRc->iBufferFullnessSkip = WELS_MAX (0, pWelsSvcRc->iBufferFullnessSkip); | ||
| 1260 | |||
| 1261 | if (pEncCtx->pSvcParam->bEnableFrameSkip) { | ||
| 1262 | pWelsSvcRc->bSkipFlag = true; | ||
| 1263 | if (pWelsSvcRc->iBufferFullnessSkip < pWelsSvcRc->iBufferSizeSkip) { | ||
| 1264 | pWelsSvcRc->bSkipFlag = false; | ||
| 1265 | } | ||
| 1266 | if (pWelsSvcRc->bSkipFlag) { | ||
| 1267 | pWelsSvcRc->iSkipFrameNum++; | ||
| 1268 | pWelsSvcRc->uiLastTimeStamp = uiTimeStamp; | ||
| 1269 | } | ||
| 1270 | } | ||
| 1271 | WelsLog (& (pEncCtx->sLogCtx), WELS_LOG_DEBUG, | ||
| 1272 | "WelsRcFrameDelayJudgeTimeStamp iSkipFrameNum = %d,buffer = %d,threadhold = %d,bitrate = %d,iSentBits = %d,lasttimestamp = %lld,timestamp=%lld\n", | ||
| 1273 | pWelsSvcRc->iSkipFrameNum, pWelsSvcRc->iBufferFullnessSkip, pWelsSvcRc->iBufferSizeSkip, iBitRate, iSentBits, | ||
| 1274 | pWelsSvcRc->uiLastTimeStamp, uiTimeStamp); | ||
| 1275 | } | ||
| 1276 | void WelsRcPictureInitGomTimeStamp (sWelsEncCtx* pEncCtx, long long uiTimeStamp) { | ||
| 1277 | SWelsSvcRc* pWelsSvcRc = &pEncCtx->pWelsSvcRc[pEncCtx->uiDependencyId]; | ||
| 1278 | SSpatialLayerConfig* pDLayerParam = &pEncCtx->pSvcParam->sSpatialLayers[pEncCtx->uiDependencyId]; | ||
| 1279 | int32_t iLumaQp = pWelsSvcRc->iLastCalculatedQScale; | ||
| 1280 | //decide one frame bits allocated | ||
| 1281 | if (pEncCtx->eSliceType == I_SLICE) { | ||
| 1282 | if (0 == pWelsSvcRc->iIdrNum) { //iIdrNum == 0 means encoder has been initialed | ||
| 1283 | RcInitRefreshParameter (pEncCtx); | ||
| 1284 | double dBpp = 0.05; | ||
| 1285 | if ((pDLayerParam->fFrameRate > EPSN) && (pDLayerParam->iVideoWidth && pDLayerParam->iVideoHeight)) | ||
| 1286 | dBpp = (double) (pDLayerParam->iSpatialBitrate) / (double) (pDLayerParam->fFrameRate * pDLayerParam->iVideoWidth * | ||
| 1287 | pDLayerParam->iVideoHeight); | ||
| 1288 | pWelsSvcRc->iInitialQp = 50 - dBpp * 10 * 4; | ||
| 1289 | pWelsSvcRc->iInitialQp = WELS_CLIP3 (pWelsSvcRc->iInitialQp, MIN_IDR_QP, MAX_IDR_QP); | ||
| 1290 | |||
| 1291 | iLumaQp = pWelsSvcRc->iInitialQp; | ||
| 1292 | pWelsSvcRc->iTargetBits = (double) (pDLayerParam->iSpatialBitrate) / (double) (pDLayerParam->fFrameRate) * | ||
| 1293 | IDR_BITRATE_RATIO; | ||
| 1294 | |||
| 1295 | WelsLog (& (pEncCtx->sLogCtx), WELS_LOG_DEBUG, | ||
| 1296 | "[Rc] First IDR iSpatialBitrate = %d,iBufferFullnessSkip = %d,iTargetBits= %d,dBpp = %f,initQp = %d", | ||
| 1297 | pDLayerParam->iSpatialBitrate, pWelsSvcRc->iBufferFullnessSkip, pWelsSvcRc->iTargetBits, dBpp, | ||
| 1298 | pWelsSvcRc->iInitialQp); | ||
| 1299 | |||
| 1300 | } else { | ||
| 1301 | |||
| 1302 | int32_t iMaxTh = pWelsSvcRc->iBufferSizeSkip - pWelsSvcRc->iBufferFullnessSkip; | ||
| 1303 | int32_t iMinTh = iMaxTh / 2; | ||
| 1304 | pWelsSvcRc->iTargetBits = (double) (pDLayerParam->iSpatialBitrate) / (double) (pDLayerParam->fFrameRate) * | ||
| 1305 | IDR_BITRATE_RATIO; | ||
| 1306 | if (iMaxTh > 0) { | ||
| 1307 | pWelsSvcRc->iTargetBits = WELS_CLIP3 (pWelsSvcRc->iTargetBits, iMinTh, iMaxTh); | ||
| 1308 | |||
| 1309 | pWelsSvcRc->iQStep = WELS_DIV_ROUND ((((int64_t)pWelsSvcRc->iIntraComplexity) * | ||
| 1310 | pWelsSvcRc->iCost2BitsIntra), pWelsSvcRc->iTargetBits); | ||
| 1311 | iLumaQp = RcConvertQStep2Qp (pWelsSvcRc->iQStep); | ||
| 1312 | |||
| 1313 | iLumaQp = WELS_CLIP3 (iLumaQp, pEncCtx->iGlobalQp - DELTA_QP_BGD_THD, pEncCtx->iGlobalQp + DELTA_QP_BGD_THD); | ||
| 1314 | |||
| 1315 | } else { | ||
| 1316 | iLumaQp = pWelsSvcRc->iLastCalculatedQScale + DELTA_QP_BGD_THD; | ||
| 1317 | } | ||
| 1318 | iLumaQp = WELS_CLIP3 (iLumaQp, MIN_IDR_QP, MAX_IDR_QP); | ||
| 1319 | WelsLog (& (pEncCtx->sLogCtx), WELS_LOG_DEBUG, | ||
| 1320 | "[Rc]I iLumaQp = %d,iQStep = %d,iTargetBits = %d,iBufferFullnessSkip =%d,iMaxTh=%d,iMinTh = %d,iFrameComplexity= %d", | ||
| 1321 | iLumaQp, pWelsSvcRc->iQStep, pWelsSvcRc->iTargetBits, pWelsSvcRc->iBufferFullnessSkip, iMaxTh, iMinTh, | ||
| 1322 | pWelsSvcRc->iIntraComplexity); | ||
| 1323 | |||
| 1324 | } | ||
| 1325 | |||
| 1326 | } else { | ||
| 1327 | int32_t iTl = pEncCtx->uiTemporalId; | ||
| 1328 | SRCTemporal* pTOverRc = &pWelsSvcRc->pTemporalOverRc[iTl]; | ||
| 1329 | int32_t iMaxTh = pWelsSvcRc->iBufferSizeSkip - pWelsSvcRc->iBufferFullnessSkip; | ||
| 1330 | int32_t iMinTh = iMaxTh / (iTl + 2); | ||
| 1331 | |||
| 1332 | SSpatialLayerInternal* pDLayerParamInternal = &pEncCtx->pSvcParam->sDependencyLayers[pEncCtx->uiDependencyId]; | ||
| 1333 | const int32_t kiGopSize = (1 << pDLayerParamInternal->iDecompositionStages); | ||
| 1334 | int32_t iAverageFrameSize = (double) (pDLayerParam->iSpatialBitrate) / (double) (pDLayerParam->fFrameRate); | ||
| 1335 | const int32_t kiGopBits = iAverageFrameSize * kiGopSize; | ||
| 1336 | int64_t iCmplxRatio = WELS_DIV_ROUND64 (pEncCtx->pVaa->sComplexityAnalysisParam.iFrameComplexity * INT_MULTIPLY, | ||
| 1337 | pTOverRc->iFrameCmplxMean); | ||
| 1338 | iCmplxRatio = WELS_CLIP3 (iCmplxRatio, INT_MULTIPLY - FRAME_CMPLX_RATIO_RANGE, INT_MULTIPLY + FRAME_CMPLX_RATIO_RANGE); | ||
| 1339 | |||
| 1340 | |||
| 1341 | pWelsSvcRc->iTargetBits = WELS_DIV_ROUND (pTOverRc->iTlayerWeight * kiGopBits, INT_MULTIPLY * 10 * 2); | ||
| 1342 | if (iMaxTh > 0) { | ||
| 1343 | pWelsSvcRc->iTargetBits = WELS_CLIP3 (pWelsSvcRc->iTargetBits, iMinTh, iMaxTh); | ||
| 1344 | if (0 == pTOverRc->iPFrameNum) | ||
| 1345 | iLumaQp = pWelsSvcRc->iInitialQp; | ||
| 1346 | else { | ||
| 1347 | |||
| 1348 | pWelsSvcRc->iQStep = WELS_DIV_ROUND ((pTOverRc->iLinearCmplx * iCmplxRatio), (pWelsSvcRc->iTargetBits * INT_MULTIPLY)); | ||
| 1349 | iLumaQp = RcConvertQStep2Qp (pWelsSvcRc->iQStep); | ||
| 1350 | iLumaQp = WELS_CLIP3 (iLumaQp, pEncCtx->iGlobalQp - DELTA_QP_BGD_THD, pEncCtx->iGlobalQp + DELTA_QP_BGD_THD); | ||
| 1351 | } | ||
| 1352 | } else { | ||
| 1353 | iLumaQp = pWelsSvcRc->iLastCalculatedQScale + DELTA_QP_BGD_THD; | ||
| 1354 | } | ||
| 1355 | |||
| 1356 | iLumaQp = WELS_CLIP3 (iLumaQp, GOM_MIN_QP_MODE, GOM_MAX_QP_MODE); | ||
| 1357 | |||
| 1358 | WelsLog (& (pEncCtx->sLogCtx), WELS_LOG_DEBUG, | ||
| 1359 | "[Rc]P iTl = %d,iLumaQp = %d,iQStep = %d,iTargetBits = %d,iBufferFullnessSkip =%d,iMaxTh=%d,iMinTh = %d,iFrameComplexity= %d,iCmplxRatio=%lld", | ||
| 1360 | iTl, iLumaQp, pWelsSvcRc->iQStep, pWelsSvcRc->iTargetBits, pWelsSvcRc->iBufferFullnessSkip, iMaxTh, iMinTh, | ||
| 1361 | pEncCtx->pVaa->sComplexityAnalysisParam.iFrameComplexity, iCmplxRatio); | ||
| 1362 | } | ||
| 1363 | |||
| 1364 | pWelsSvcRc->iQStep = RcConvertQp2QStep (iLumaQp); | ||
| 1365 | pWelsSvcRc->iLastCalculatedQScale = iLumaQp; | ||
| 1366 | pEncCtx->iGlobalQp = iLumaQp; | ||
| 1367 | |||
| 1368 | RcInitSliceInformation (pEncCtx); | ||
| 1369 | RcInitGomParameters (pEncCtx); | ||
| 1370 | float fInstantFps = (uiTimeStamp - pWelsSvcRc->uiLastTimeStamp) > 0 ? (1000.0 / (uiTimeStamp - | ||
| 1371 | pWelsSvcRc->uiLastTimeStamp)) : 0; | ||
| 1372 | WelsLog (& (pEncCtx->sLogCtx), WELS_LOG_DEBUG, | ||
| 1373 | "[Rc]pEncCtx->iGlobalQp= %d,uiTimeStamp = %lld,uiLastTimeStamp = %lld,InstantFps = %f,settingFps = %f", | ||
| 1374 | pEncCtx->iGlobalQp, uiTimeStamp, pWelsSvcRc->uiLastTimeStamp, | ||
| 1375 | fInstantFps, pDLayerParam->fFrameRate); | ||
| 1376 | pWelsSvcRc->uiLastTimeStamp = uiTimeStamp; | ||
| 1377 | |||
| 1378 | } | ||
| 1379 | |||
| 1380 | void WelsRcPictureInfoUpdateGomTimeStamp (sWelsEncCtx* pEncCtx, int32_t iLayerSize) { | ||
| 1381 | SWelsSvcRc* pWelsSvcRc = &pEncCtx->pWelsSvcRc[pEncCtx->uiDependencyId]; | ||
| 1382 | int32_t iCodedBits = (iLayerSize << 3); | ||
| 1383 | |||
| 1384 | RcUpdatePictureQpBits (pEncCtx, iCodedBits); | ||
| 1385 | if (pEncCtx->eSliceType == P_SLICE) { | ||
| 1386 | RcUpdateFrameComplexity (pEncCtx); | ||
| 1387 | } else { | ||
| 1388 | RcUpdateIntraComplexity (pEncCtx); | ||
| 1389 | } | ||
| 1390 | |||
| 1391 | pWelsSvcRc->iRemainingBits -= pWelsSvcRc->iFrameDqBits; | ||
| 1392 | //condition 1: whole pBuffer fullness | ||
| 1393 | pWelsSvcRc->iBufferFullnessSkip += pWelsSvcRc->iFrameDqBits; | ||
| 1394 | |||
| 1395 | |||
| 1396 | if (pEncCtx->pSvcParam->iPaddingFlag) | ||
| 1397 | RcVBufferCalculationPadding (pEncCtx); | ||
| 1398 | pWelsSvcRc->iFrameCodedInVGop++; | ||
| 1399 | } | ||
| 1269 | void WelsRcInitModule (sWelsEncCtx* pEncCtx, RC_MODES iRcMode) { | 1400 | void WelsRcInitModule (sWelsEncCtx* pEncCtx, RC_MODES iRcMode) { |
| 1270 | SWelsRcFunc* pRcf = &pEncCtx->pFuncList->pfRc; | 1401 | SWelsRcFunc* pRcf = &pEncCtx->pFuncList->pfRc; |
| 1271 | |||
| 1272 | switch (iRcMode) { | 1402 | switch (iRcMode) { |
| 1273 | case RC_OFF_MODE: | 1403 | case RC_OFF_MODE: |
| 1274 | pRcf->pfWelsRcPictureInit = WelsRcPictureInitDisable; | 1404 | pRcf->pfWelsRcPictureInit = WelsRcPictureInitDisable; |
| @@ -1303,19 +1433,21 @@ void WelsRcInitModule (sWelsEncCtx* pEncCtx, RC_MODES iRcMode) { · 28 unmodified lines | |||
| 1303 | case RC_TIMESTAMP_MODE: | 1433 | case RC_TIMESTAMP_MODE: |
| 1304 | if (pEncCtx->pSvcParam->iUsageType == SCREEN_CONTENT_REAL_TIME) { | 1434 | if (pEncCtx->pSvcParam->iUsageType == SCREEN_CONTENT_REAL_TIME) { |
| 1305 | pRcf->pfWelsRcPictureInit = WelRcPictureInitScc; | 1435 | pRcf->pfWelsRcPictureInit = WelRcPictureInitScc; |
| 1306 | pRcf->pfWelsRcPicDelayJudge = WelsRcFrameDelayJudgeScc; | ||
| 1307 | pRcf->pfWelsRcPictureInfoUpdate = WelsRcPictureInfoUpdateScc; | 1436 | pRcf->pfWelsRcPictureInfoUpdate = WelsRcPictureInfoUpdateScc; |
| 1308 | pRcf->pfWelsRcMbInit = WelsRcMbInitScc; | 1437 | pRcf->pfWelsRcMbInit = WelsRcMbInitScc; |
| 1309 | pRcf->pfWelsRcMbInfoUpdate = WelsRcMbInfoUpdateDisable; | 1438 | pRcf->pfWelsRcMbInfoUpdate = WelsRcMbInfoUpdateDisable; |
| 1310 | pRcf->pfWelsCheckSkipBasedMaxbr = CheckFrameSkipBasedMaxbr; | ||
| 1311 | pRcf->pfWelsUpdateBufferWhenSkip = NULL; | ||
| 1312 | pRcf->pfWelsUpdateMaxBrWindowStatus = NULL; | ||
| 1313 | |||
| 1314 | InitRcModuleScc (pEncCtx); | ||
| 1315 | 1439 | ||
| 1316 | } else { | 1440 | } else { |
| 1317 | WelsLog (& (pEncCtx->sLogCtx), WELS_LOG_INFO, "to be done in the future"); | 1441 | pRcf->pfWelsRcPictureInit = WelsRcPictureInitGomTimeStamp; |
| 1442 | pRcf->pfWelsRcPictureInfoUpdate = WelsRcPictureInfoUpdateGomTimeStamp; | ||
| 1443 | pRcf->pfWelsRcMbInit = WelsRcMbInitGom; | ||
| 1444 | pRcf->pfWelsRcMbInfoUpdate = WelsRcMbInfoUpdateGom; | ||
| 1318 | } | 1445 | } |
| 1446 | pRcf->pfWelsRcPicDelayJudge = WelsRcFrameDelayJudgeTimeStamp; | ||
| 1447 | pRcf->pfWelsCheckSkipBasedMaxbr = CheckFrameSkipBasedMaxbr; | ||
| 1448 | pRcf->pfWelsUpdateBufferWhenSkip = NULL; | ||
| 1449 | pRcf->pfWelsUpdateMaxBrWindowStatus = NULL; | ||
| 1450 | InitRcModuleTimeStamp (pEncCtx); | ||
| 1319 | break; | 1451 | break; |
| 1320 | case RC_QUALITY_MODE: | 1452 | case RC_QUALITY_MODE: |
| 1321 | default: | 1453 | default: |