Showing the changes to codec/encoder/core/src/ratectl.cpp (1 of 8 files). Show all
Unified · Split
| @@ -163,7 +163,7 @@ void RcInitSequenceParameter (sWelsEncCtx* pEncCtx) { · 162 unmodified lines | |||
| 163 | 163 | ||
| 164 | pWelsSvcRc->iSkipFrameNum = 0; | 164 | pWelsSvcRc->iSkipFrameNum = 0; |
| 165 | pWelsSvcRc->iGomSize = (pWelsSvcRc->iNumberMbFrame + pWelsSvcRc->iNumberMbGom - 1) / pWelsSvcRc->iNumberMbGom; | 165 | pWelsSvcRc->iGomSize = (pWelsSvcRc->iNumberMbFrame + pWelsSvcRc->iNumberMbGom - 1) / pWelsSvcRc->iNumberMbGom; |
| 166 | 166 | pWelsSvcRc->bEnableGomQp = true; | |
| 167 | 167 | ||
| 168 | RcInitLayerMemory (pWelsSvcRc, pEncCtx->pMemAlign, 1 + pEncCtx->pSvcParam->sDependencyLayers[j].iHighestTemporalId); | 168 | RcInitLayerMemory (pWelsSvcRc, pEncCtx->pMemAlign, 1 + pEncCtx->pSvcParam->sDependencyLayers[j].iHighestTemporalId); |
| 169 | 169 | ||
| @@ -281,7 +281,7 @@ void RcInitRefreshParameter (sWelsEncCtx* pEncCtx) { · 111 unmodified lines | |||
| 281 | //I frame R-Q Model | 281 | //I frame R-Q Model |
| 282 | pWelsSvcRc->iIntraComplexity = 0; | 282 | pWelsSvcRc->iIntraComplexity = 0; |
| 283 | pWelsSvcRc->iIntraMbCount = 0; | 283 | pWelsSvcRc->iIntraMbCount = 0; |
| 284 | 284 | pWelsSvcRc->iIntraComplxMean = 0; | |
| 285 | //P frame R-Q Model | 285 | //P frame R-Q Model |
| 286 | for (i = 0; i <= kiHighestTid; i++) { | 286 | for (i = 0; i <= kiHighestTid; i++) { |
| 287 | pTOverRc[i].iPFrameNum = 0; | 287 | pTOverRc[i].iPFrameNum = 0; |
| @@ -399,7 +399,6 @@ void RcInitIdrQp (sWelsEncCtx* pEncCtx) { · 111 unmodified lines | |||
| 399 | pDLayerParam->iVideoHeight); | 399 | pDLayerParam->iVideoHeight); |
| 400 | else | 400 | else |
| 401 | dBpp = 0.1; | 401 | dBpp = 0.1; |
| 402 | |||
| 403 | //Area*2 | 402 | //Area*2 |
| 404 | if (pDLayerParam->iVideoWidth * pDLayerParam->iVideoHeight <= 28800) // 90p video:160*90 | 403 | if (pDLayerParam->iVideoWidth * pDLayerParam->iVideoHeight <= 28800) // 90p video:160*90 |
| 405 | iBppIndex = 0; | 404 | iBppIndex = 0; |
| @@ -420,6 +419,10 @@ void RcInitIdrQp (sWelsEncCtx* pEncCtx) { · 14 unmodified lines | |||
| 420 | pEncCtx->iGlobalQp = pWelsSvcRc->iInitialQp; | 419 | pEncCtx->iGlobalQp = pWelsSvcRc->iInitialQp; |
| 421 | pWelsSvcRc->iQStep = RcConvertQp2QStep (pEncCtx->iGlobalQp); | 420 | pWelsSvcRc->iQStep = RcConvertQp2QStep (pEncCtx->iGlobalQp); |
| 422 | pWelsSvcRc->iLastCalculatedQScale = pEncCtx->iGlobalQp; | 421 | pWelsSvcRc->iLastCalculatedQScale = pEncCtx->iGlobalQp; |
| 422 | pWelsSvcRc->iMinFrameQp = WELS_CLIP3 (pEncCtx->iGlobalQp - DELTA_QP_BGD_THD, pEncCtx->pSvcParam->iMinQp, | ||
| 423 | pEncCtx->pSvcParam->iMaxQp); | ||
| 424 | pWelsSvcRc->iMaxFrameQp = WELS_CLIP3 (pEncCtx->iGlobalQp + DELTA_QP_BGD_THD, pEncCtx->pSvcParam->iMinQp, | ||
| 425 | pEncCtx->pSvcParam->iMaxQp); | ||
| 423 | } | 426 | } |
| 424 | 427 | ||
| 425 | void RcCalculateIdrQp (sWelsEncCtx* pEncCtx) { | 428 | void RcCalculateIdrQp (sWelsEncCtx* pEncCtx) { |
| @@ -429,12 +432,21 @@ void RcCalculateIdrQp (sWelsEncCtx* pEncCtx) { · 3 unmodified lines | |||
| 429 | pWelsSvcRc->iIntraComplexity = pWelsSvcRc->iIntraComplexity * pWelsSvcRc->iNumberMbFrame / | 432 | pWelsSvcRc->iIntraComplexity = pWelsSvcRc->iIntraComplexity * pWelsSvcRc->iNumberMbFrame / |
| 430 | pWelsSvcRc->iIntraMbCount; | 433 | pWelsSvcRc->iIntraMbCount; |
| 431 | } | 434 | } |
| 432 | pWelsSvcRc->iInitialQp = RcConvertQStep2Qp (WELS_DIV_ROUND (pWelsSvcRc->iIntraComplexity, | 435 | |
| 433 | pWelsSvcRc->iTargetBits)); | 436 | int64_t iCmplxRatio = WELS_DIV_ROUND64 (pEncCtx->pVaa->sComplexityAnalysisParam.iFrameComplexity * INT_MULTIPLY, |
| 437 | pWelsSvcRc->iIntraComplxMean); | ||
| 438 | iCmplxRatio = WELS_CLIP3 (iCmplxRatio, INT_MULTIPLY - FRAME_CMPLX_RATIO_RANGE, INT_MULTIPLY + FRAME_CMPLX_RATIO_RANGE); | ||
| 439 | pWelsSvcRc->iQStep = WELS_DIV_ROUND ((pWelsSvcRc->iIntraComplexity * iCmplxRatio), | ||
| 440 | (pWelsSvcRc->iTargetBits * INT_MULTIPLY)); | ||
| 441 | pWelsSvcRc->iInitialQp = RcConvertQStep2Qp (pWelsSvcRc->iQStep); | ||
| 434 | pWelsSvcRc->iInitialQp = WELS_CLIP3 (pWelsSvcRc->iInitialQp, pEncCtx->pSvcParam->iMinQp, pEncCtx->pSvcParam->iMaxQp); | 442 | pWelsSvcRc->iInitialQp = WELS_CLIP3 (pWelsSvcRc->iInitialQp, pEncCtx->pSvcParam->iMinQp, pEncCtx->pSvcParam->iMaxQp); |
| 435 | pEncCtx->iGlobalQp = pWelsSvcRc->iInitialQp; | 443 | pEncCtx->iGlobalQp = pWelsSvcRc->iInitialQp; |
| 436 | pWelsSvcRc->iQStep = RcConvertQp2QStep (pEncCtx->iGlobalQp); | 444 | pWelsSvcRc->iQStep = RcConvertQp2QStep (pEncCtx->iGlobalQp); |
| 437 | pWelsSvcRc->iLastCalculatedQScale = pEncCtx->iGlobalQp; | 445 | pWelsSvcRc->iLastCalculatedQScale = pEncCtx->iGlobalQp; |
| 446 | pWelsSvcRc->iMinFrameQp = WELS_CLIP3 (pEncCtx->iGlobalQp - DELTA_QP_BGD_THD, pEncCtx->pSvcParam->iMinQp, | ||
| 447 | pEncCtx->pSvcParam->iMaxQp); | ||
| 448 | pWelsSvcRc->iMaxFrameQp = WELS_CLIP3 (pEncCtx->iGlobalQp + DELTA_QP_BGD_THD, pEncCtx->pSvcParam->iMinQp, | ||
| 449 | pEncCtx->pSvcParam->iMaxQp); | ||
| 438 | } | 450 | } |
| 439 | 451 | ||
| 440 | 452 | ||
| @@ -443,36 +455,22 @@ void RcCalculatePictureQp (sWelsEncCtx* pEncCtx) { · 2 unmodified lines | |||
| 443 | int32_t iTl = pEncCtx->uiTemporalId; | 455 | int32_t iTl = pEncCtx->uiTemporalId; |
| 444 | SRCTemporal* pTOverRc = &pWelsSvcRc->pTemporalOverRc[iTl]; | 456 | SRCTemporal* pTOverRc = &pWelsSvcRc->pTemporalOverRc[iTl]; |
| 445 | int32_t iLumaQp = 0; | 457 | int32_t iLumaQp = 0; |
| 446 | 458 | int32_t iDeltaQpTemporal = 0; | |
| 447 | if (0 == pTOverRc->iPFrameNum) { | 459 | if (0 == pTOverRc->iPFrameNum) { |
| 448 | iLumaQp = pWelsSvcRc->iInitialQp; | 460 | iLumaQp = pWelsSvcRc->iInitialQp; |
| 449 | } else if (pWelsSvcRc->iCurrentBitsLevel == BITS_EXCEEDED) { | 461 | } else if (pWelsSvcRc->iCurrentBitsLevel == BITS_EXCEEDED) { |
| 450 | iLumaQp = MAX_LOW_BR_QP; | 462 | iLumaQp = pWelsSvcRc->iLastCalculatedQScale + DELTA_QP_BGD_THD; |
| 451 | //limit QP | 463 | //limit QP |
| 452 | int32_t iLastIdxCodecInVGop = pWelsSvcRc->iFrameCodedInVGop - 1; | 464 | int32_t iLastIdxCodecInVGop = pWelsSvcRc->iFrameCodedInVGop - 1; |
| 453 | if (iLastIdxCodecInVGop < 0) | 465 | if (iLastIdxCodecInVGop < 0) |
| 454 | iLastIdxCodecInVGop += VGOP_SIZE; | 466 | iLastIdxCodecInVGop += VGOP_SIZE; |
| 455 | int32_t iTlLast = pWelsSvcRc->iTlOfFrames[iLastIdxCodecInVGop]; | 467 | int32_t iTlLast = pWelsSvcRc->iTlOfFrames[iLastIdxCodecInVGop]; |
| 456 | int32_t iDeltaQpTemporal = iTl - iTlLast; | 468 | iDeltaQpTemporal = iTl - iTlLast; |
| 457 | if (0 == iTlLast && iTl > 0) | 469 | if (0 == iTlLast && iTl > 0) |
| 458 | iDeltaQpTemporal += 3; | 470 | iDeltaQpTemporal += 1; |
| 459 | else if (0 == iTl && iTlLast > 0) | 471 | else if (0 == iTl && iTlLast > 0) |
| 460 | iDeltaQpTemporal -= 3; | 472 | iDeltaQpTemporal -= 1; |
| 461 | |||
| 462 | iLumaQp = WELS_CLIP3 (iLumaQp, | ||
| 463 | pWelsSvcRc->iLastCalculatedQScale - pWelsSvcRc->iFrameDeltaQpLower + iDeltaQpTemporal, | ||
| 464 | pWelsSvcRc->iLastCalculatedQScale + pWelsSvcRc->iFrameDeltaQpUpper + iDeltaQpTemporal); | ||
| 465 | iLumaQp = WELS_CLIP3 (iLumaQp, pTOverRc->iMinQp, pTOverRc->iMaxQp); | ||
| 466 | |||
| 467 | if (pEncCtx->pSvcParam->bEnableAdaptiveQuant) { | ||
| 468 | iLumaQp = WELS_CLIP3 ((iLumaQp * INT_MULTIPLY - pEncCtx->pVaa->sAdaptiveQuantParam.iAverMotionTextureIndexToDeltaQp) / | ||
| 469 | INT_MULTIPLY, pTOverRc->iMinQp, pTOverRc->iMaxQp); | ||
| 470 | } | ||
| 471 | pWelsSvcRc->iQStep = RcConvertQp2QStep (iLumaQp); | ||
| 472 | pWelsSvcRc->iLastCalculatedQScale = iLumaQp; | ||
| 473 | pEncCtx->iGlobalQp = iLumaQp; | ||
| 474 | 473 | ||
| 475 | return; | ||
| 476 | } else { | 474 | } else { |
| 477 | int64_t iCmplxRatio = WELS_DIV_ROUND64 (pEncCtx->pVaa->sComplexityAnalysisParam.iFrameComplexity * INT_MULTIPLY, | 475 | int64_t iCmplxRatio = WELS_DIV_ROUND64 (pEncCtx->pVaa->sComplexityAnalysisParam.iFrameComplexity * INT_MULTIPLY, |
| 478 | pTOverRc->iFrameCmplxMean); | 476 | pTOverRc->iFrameCmplxMean); |
| @@ -480,7 +478,9 @@ void RcCalculatePictureQp (sWelsEncCtx* pEncCtx) { · 1 unmodified line | |||
| 480 | 478 | ||
| 481 | pWelsSvcRc->iQStep = WELS_DIV_ROUND ((pTOverRc->iLinearCmplx * iCmplxRatio), (pWelsSvcRc->iTargetBits * INT_MULTIPLY)); | 479 | pWelsSvcRc->iQStep = WELS_DIV_ROUND ((pTOverRc->iLinearCmplx * iCmplxRatio), (pWelsSvcRc->iTargetBits * INT_MULTIPLY)); |
| 482 | iLumaQp = RcConvertQStep2Qp (pWelsSvcRc->iQStep); | 480 | iLumaQp = RcConvertQStep2Qp (pWelsSvcRc->iQStep); |
| 483 | 481 | WelsLog (& (pEncCtx->sLogCtx), WELS_LOG_DEBUG, | |
| 482 | "iCmplxRatio = %d,frameComplexity = %lld,iFrameCmplxMean = %d,iQStep = %d,iLumaQp = %d", (int)iCmplxRatio, | ||
| 483 | pEncCtx->pVaa->sComplexityAnalysisParam.iFrameComplexity, pTOverRc->iFrameCmplxMean, pWelsSvcRc->iQStep, iLumaQp); | ||
| 484 | //limit QP | 484 | //limit QP |
| 485 | int32_t iLastIdxCodecInVGop = pWelsSvcRc->iFrameCodedInVGop - 1; | 485 | int32_t iLastIdxCodecInVGop = pWelsSvcRc->iFrameCodedInVGop - 1; |
| 486 | if (iLastIdxCodecInVGop < 0) | 486 | if (iLastIdxCodecInVGop < 0) |
| @@ -488,21 +488,22 @@ void RcCalculatePictureQp (sWelsEncCtx* pEncCtx) { · 1 unmodified line | |||
| 488 | int32_t iTlLast = pWelsSvcRc->iTlOfFrames[iLastIdxCodecInVGop]; | 488 | int32_t iTlLast = pWelsSvcRc->iTlOfFrames[iLastIdxCodecInVGop]; |
| 489 | int32_t iDeltaQpTemporal = iTl - iTlLast; | 489 | int32_t iDeltaQpTemporal = iTl - iTlLast; |
| 490 | if (0 == iTlLast && iTl > 0) | 490 | if (0 == iTlLast && iTl > 0) |
| 491 | iDeltaQpTemporal += 3; | 491 | iDeltaQpTemporal += 1; |
| 492 | else if (0 == iTl && iTlLast > 0) | 492 | else if (0 == iTl && iTlLast > 0) |
| 493 | iDeltaQpTemporal -= 3; | 493 | iDeltaQpTemporal -= 1; |
| 494 | |||
| 495 | iLumaQp = WELS_CLIP3 (iLumaQp, | ||
| 496 | pWelsSvcRc->iLastCalculatedQScale - pWelsSvcRc->iFrameDeltaQpLower + iDeltaQpTemporal, | ||
| 497 | pWelsSvcRc->iLastCalculatedQScale + pWelsSvcRc->iFrameDeltaQpUpper + iDeltaQpTemporal); | ||
| 498 | } | 494 | } |
| 495 | pWelsSvcRc->iMinFrameQp = WELS_CLIP3 (pWelsSvcRc->iLastCalculatedQScale - pWelsSvcRc->iFrameDeltaQpLower + | ||
| 496 | iDeltaQpTemporal, pTOverRc->iMinQp, pTOverRc->iMaxQp) ; | ||
| 497 | pWelsSvcRc->iMaxFrameQp = WELS_CLIP3 (pWelsSvcRc->iLastCalculatedQScale + pWelsSvcRc->iFrameDeltaQpUpper + | ||
| 498 | iDeltaQpTemporal, pTOverRc->iMinQp, pTOverRc->iMaxQp); | ||
| 499 | |||
| 500 | iLumaQp = WELS_CLIP3 (iLumaQp, pWelsSvcRc->iMinFrameQp, pWelsSvcRc->iMaxFrameQp); | ||
| 499 | 501 | ||
| 500 | iLumaQp = WELS_CLIP3 (iLumaQp, pTOverRc->iMinQp, pTOverRc->iMaxQp); | ||
| 501 | if (pEncCtx->pSvcParam->bEnableAdaptiveQuant) { | 502 | if (pEncCtx->pSvcParam->bEnableAdaptiveQuant) { |
| 502 | 503 | ||
| 503 | iLumaQp = WELS_DIV_ROUND (iLumaQp * INT_MULTIPLY - pEncCtx->pVaa->sAdaptiveQuantParam.iAverMotionTextureIndexToDeltaQp, | 504 | iLumaQp = WELS_DIV_ROUND (iLumaQp * INT_MULTIPLY - pEncCtx->pVaa->sAdaptiveQuantParam.iAverMotionTextureIndexToDeltaQp, |
| 504 | INT_MULTIPLY); | 505 | INT_MULTIPLY); |
| 505 | iLumaQp = WELS_CLIP3 (iLumaQp, pTOverRc->iMinQp, pTOverRc->iMaxQp); | 506 | iLumaQp = WELS_CLIP3 (iLumaQp, pWelsSvcRc->iMinFrameQp, pWelsSvcRc->iMaxFrameQp); |
| 506 | } | 507 | } |
| 507 | pWelsSvcRc->iQStep = RcConvertQp2QStep (iLumaQp); | 508 | pWelsSvcRc->iQStep = RcConvertQp2QStep (iLumaQp); |
| 508 | pWelsSvcRc->iLastCalculatedQScale = iLumaQp; | 509 | pWelsSvcRc->iLastCalculatedQScale = iLumaQp; |
| @@ -554,6 +555,57 @@ void RcDecideTargetBits (sWelsEncCtx* pEncCtx) { · 45 unmodified lines | |||
| 554 | pWelsSvcRc->iRemainingWeights -= pTOverRc->iTlayerWeight; | 555 | pWelsSvcRc->iRemainingWeights -= pTOverRc->iTlayerWeight; |
| 555 | } | 556 | } |
| 556 | 557 | ||
| 558 | void RcDecideTargetBitsTimestamp (sWelsEncCtx* pEncCtx) { | ||
| 559 | //decide one frame bits allocated | ||
| 560 | SWelsSvcRc* pWelsSvcRc = &pEncCtx->pWelsSvcRc[pEncCtx->uiDependencyId]; | ||
| 561 | SSpatialLayerConfig* pDLayerParam = &pEncCtx->pSvcParam->sSpatialLayers[pEncCtx->uiDependencyId]; | ||
| 562 | int32_t iTl = pEncCtx->uiTemporalId; | ||
| 563 | SRCTemporal* pTOverRc = &pWelsSvcRc->pTemporalOverRc[iTl]; | ||
| 564 | |||
| 565 | pWelsSvcRc->iCurrentBitsLevel = BITS_NORMAL; | ||
| 566 | if (pEncCtx->eSliceType == I_SLICE) { | ||
| 567 | int32_t iBufferTh = static_cast<int32_t> (pWelsSvcRc->iBufferSizeSkip - pWelsSvcRc->iBufferFullnessSkip); | ||
| 568 | if (iBufferTh <= 0) { | ||
| 569 | pWelsSvcRc->iCurrentBitsLevel = BITS_EXCEEDED; | ||
| 570 | pWelsSvcRc->iTargetBits = pTOverRc->iMinBitsTl; | ||
| 571 | } else { | ||
| 572 | int32_t iMaxTh = iBufferTh * 3 / 4; | ||
| 573 | int32_t iMinTh = iBufferTh * 2 / pDLayerParam->fFrameRate; | ||
| 574 | pWelsSvcRc->iTargetBits = static_cast<int32_t> (((double) (pDLayerParam->iSpatialBitrate) / (double) ( | ||
| 575 | pDLayerParam->fFrameRate) * IDR_BITRATE_RATIO)); | ||
| 576 | WelsLog (& (pEncCtx->sLogCtx), WELS_LOG_DEBUG, | ||
| 577 | "iMaxTh = %d,iMinTh = %d,pWelsSvcRc->iTargetBits = %d,pWelsSvcRc->iBufferSizeSkip = %d, pWelsSvcRc->iBufferFullnessSkip= %" | ||
| 578 | PRId64 , | ||
| 579 | iMaxTh, iMinTh, pWelsSvcRc->iTargetBits, pWelsSvcRc->iBufferSizeSkip, pWelsSvcRc->iBufferFullnessSkip); | ||
| 580 | pWelsSvcRc->iTargetBits = WELS_CLIP3 (pWelsSvcRc->iTargetBits, iMinTh, iMaxTh); | ||
| 581 | } | ||
| 582 | |||
| 583 | } else { | ||
| 584 | int32_t iBufferTh = static_cast<int32_t> (pWelsSvcRc->iBufferSizeSkip - pWelsSvcRc->iBufferFullnessSkip); | ||
| 585 | if (iBufferTh <= 0) { | ||
| 586 | pWelsSvcRc->iCurrentBitsLevel = BITS_EXCEEDED; | ||
| 587 | pWelsSvcRc->iTargetBits = pTOverRc->iMinBitsTl; | ||
| 588 | WelsLog (& (pEncCtx->sLogCtx), WELS_LOG_DEBUG, | ||
| 589 | "iMaxTh = %d,pWelsSvcRc->iTargetBits = %d,pWelsSvcRc->iBufferSizeSkip = %d, pWelsSvcRc->iBufferFullnessSkip= %" PRId64, | ||
| 590 | iBufferTh, pWelsSvcRc->iTargetBits, pWelsSvcRc->iBufferSizeSkip, pWelsSvcRc->iBufferFullnessSkip); | ||
| 591 | } else { | ||
| 592 | |||
| 593 | SSpatialLayerInternal* pDLayerParamInternal = &pEncCtx->pSvcParam->sDependencyLayers[pEncCtx->uiDependencyId]; | ||
| 594 | const int32_t kiGopSize = (1 << pDLayerParamInternal->iDecompositionStages); | ||
| 595 | int32_t iAverageFrameSize = (int32_t) ((double) (pDLayerParam->iSpatialBitrate) / (double) (pDLayerParam->fFrameRate)); | ||
| 596 | const int32_t kiGopBits = iAverageFrameSize * kiGopSize; | ||
| 597 | pWelsSvcRc->iTargetBits = WELS_DIV_ROUND (pTOverRc->iTlayerWeight * kiGopBits, INT_MULTIPLY * 10 * 2); | ||
| 598 | |||
| 599 | int32_t iMaxTh = iBufferTh / 2; | ||
| 600 | int32_t iMinTh = iBufferTh * 2 / pDLayerParam->fFrameRate; | ||
| 601 | WelsLog (& (pEncCtx->sLogCtx), WELS_LOG_DEBUG, | ||
| 602 | "iMaxTh = %d,iMinTh = %d,pWelsSvcRc->iTargetBits = %d,pWelsSvcRc->iBufferSizeSkip = %d, pWelsSvcRc->iBufferFullnessSkip= % " | ||
| 603 | PRId64, | ||
| 604 | iMaxTh, iMinTh, pWelsSvcRc->iTargetBits, pWelsSvcRc->iBufferSizeSkip, pWelsSvcRc->iBufferFullnessSkip); | ||
| 605 | pWelsSvcRc->iTargetBits = WELS_CLIP3 (pWelsSvcRc->iTargetBits, iMinTh, iMaxTh); | ||
| 606 | } | ||
| 607 | } | ||
| 608 | } | ||
| 557 | 609 | ||
| 558 | void RcInitGomParameters (sWelsEncCtx* pEncCtx) { | 610 | void RcInitGomParameters (sWelsEncCtx* pEncCtx) { |
| 559 | SSlice* pSliceInLayer = pEncCtx->pCurDqLayer->sLayerInfo.pSliceInLayer; | 611 | SSlice* pSliceInLayer = pEncCtx->pCurDqLayer->sLayerInfo.pSliceInLayer; |
| @@ -563,8 +615,6 @@ void RcInitGomParameters (sWelsEncCtx* pEncCtx) { · 3 unmodified lines | |||
| 563 | const int32_t kiGlobalQp = pEncCtx->iGlobalQp; | 615 | const int32_t kiGlobalQp = pEncCtx->iGlobalQp; |
| 564 | 616 | ||
| 565 | pWelsSvcRc->iAverageFrameQp = 0; | 617 | pWelsSvcRc->iAverageFrameQp = 0; |
| 566 | pWelsSvcRc->iMinFrameQp = 51; | ||
| 567 | pWelsSvcRc->iMaxFrameQp = 0; | ||
| 568 | for (int32_t i = 0; i < kiSliceNum; ++i) { | 618 | for (int32_t i = 0; i < kiSliceNum; ++i) { |
| 569 | pSOverRc = &pSliceInLayer[i].sSlicingOverRc; | 619 | pSOverRc = &pSliceInLayer[i].sSlicingOverRc; |
| 570 | pSOverRc->iComplexityIndexSlice = 0; | 620 | pSOverRc->iComplexityIndexSlice = 0; |
| @@ -581,11 +631,11 @@ void RcCalculateMbQp (sWelsEncCtx* pEncCtx, SMB* pCurMb, const int32_t kiSliceId · 10 unmodified lines | |||
| 581 | 631 | ||
| 582 | int32_t iLumaQp = pSOverRc->iCalculatedQpSlice; | 632 | int32_t iLumaQp = pSOverRc->iCalculatedQpSlice; |
| 583 | SDqLayer* pCurLayer = pEncCtx->pCurDqLayer; | 633 | SDqLayer* pCurLayer = pEncCtx->pCurDqLayer; |
| 584 | SRCTemporal* pTOverRc = &pWelsSvcRc->pTemporalOverRc[pEncCtx->uiTemporalId]; | ||
| 585 | const uint8_t kuiChromaQpIndexOffset = pCurLayer->sLayerInfo.pPpsP->uiChromaQpIndexOffset; | 634 | const uint8_t kuiChromaQpIndexOffset = pCurLayer->sLayerInfo.pPpsP->uiChromaQpIndexOffset; |
| 586 | if (pEncCtx->pSvcParam->bEnableAdaptiveQuant) { | 635 | if (pEncCtx->pSvcParam->bEnableAdaptiveQuant) { |
| 587 | iLumaQp = (int8_t)WELS_CLIP3 (iLumaQp + | 636 | iLumaQp = (int8_t)WELS_CLIP3 (iLumaQp + |
| 588 | pEncCtx->pVaa->sAdaptiveQuantParam.pMotionTextureIndexToDeltaQp[pCurMb->iMbXY], pTOverRc->iMinQp, pTOverRc->iMaxQp); | 637 | pEncCtx->pVaa->sAdaptiveQuantParam.pMotionTextureIndexToDeltaQp[pCurMb->iMbXY], pWelsSvcRc->iMinFrameQp, |
| 638 | pWelsSvcRc->iMaxFrameQp); | ||
| 589 | } | 639 | } |
| 590 | pCurMb->uiChromaQp = g_kuiChromaQpTable[CLIP3_QP_0_51 (iLumaQp + kuiChromaQpIndexOffset)]; | 640 | pCurMb->uiChromaQp = g_kuiChromaQpTable[CLIP3_QP_0_51 (iLumaQp + kuiChromaQpIndexOffset)]; |
| 591 | pCurMb->uiLumaQp = iLumaQp; | 641 | pCurMb->uiLumaQp = iLumaQp; |
| @@ -637,15 +687,15 @@ void RcGomTargetBits (sWelsEncCtx* pEncCtx, const int32_t kiSliceId) { · 45 unmodified lines | |||
| 637 | } else { | 687 | } else { |
| 638 | pWelsSvcRc_Base = RcJudgeBaseUsability (pEncCtx); | 688 | pWelsSvcRc_Base = RcJudgeBaseUsability (pEncCtx); |
| 639 | pWelsSvcRc_Base = (pWelsSvcRc_Base) ? pWelsSvcRc_Base : pWelsSvcRc; | 689 | pWelsSvcRc_Base = (pWelsSvcRc_Base) ? pWelsSvcRc_Base : pWelsSvcRc; |
| 640 | for (i = kiComplexityIndex; i <= iLastGomIndex; i++) { | 690 | for (i = kiComplexityIndex + 1; i <= iLastGomIndex; i++) { |
| 641 | iSumSad += pWelsSvcRc_Base->pCurrentFrameGomSad[i]; | 691 | iSumSad += pWelsSvcRc_Base->pCurrentFrameGomSad[i]; |
| 642 | } | 692 | } |
| 693 | |||
| 643 | if (0 == iSumSad) | 694 | if (0 == iSumSad) |
| 644 | iAllocateBits = WELS_DIV_ROUND (iLeftBits, (iLastGomIndex - kiComplexityIndex)); | 695 | iAllocateBits = WELS_DIV_ROUND (iLeftBits, (iLastGomIndex - kiComplexityIndex)); |
| 645 | else | 696 | else |
| 646 | iAllocateBits = WELS_DIV_ROUND ((int64_t)iLeftBits * pWelsSvcRc_Base->pCurrentFrameGomSad[kiComplexityIndex + 1], | 697 | iAllocateBits = WELS_DIV_ROUND ((int64_t)iLeftBits * pWelsSvcRc_Base->pCurrentFrameGomSad[kiComplexityIndex + 1], |
| 647 | iSumSad); | 698 | iSumSad); |
| 648 | |||
| 649 | } | 699 | } |
| 650 | pSOverRc->iGomTargetBits = iAllocateBits; | 700 | pSOverRc->iGomTargetBits = iAllocateBits; |
| 651 | } | 701 | } |
| @@ -660,7 +710,6 @@ void RcCalculateGomQp (sWelsEncCtx* pEncCtx, SMB* pCurMb, int32_t iSliceId) { · 8 unmodified lines | |||
| 660 | 710 | ||
| 661 | int64_t iLeftBits = pSOverRc->iTargetBitsSlice - pSOverRc->iFrameBitsSlice; | 711 | int64_t iLeftBits = pSOverRc->iTargetBitsSlice - pSOverRc->iFrameBitsSlice; |
| 662 | int64_t iTargetLeftBits = iLeftBits + pSOverRc->iGomBitsSlice - pSOverRc->iGomTargetBits; | 712 | int64_t iTargetLeftBits = iLeftBits + pSOverRc->iGomBitsSlice - pSOverRc->iGomTargetBits; |
| 663 | |||
| 664 | if ((iLeftBits <= 0) || (iTargetLeftBits <= 0)) { | 713 | if ((iLeftBits <= 0) || (iTargetLeftBits <= 0)) { |
| 665 | pSOverRc->iCalculatedQpSlice += 2; | 714 | pSOverRc->iCalculatedQpSlice += 2; |
| 666 | } else { | 715 | } else { |
| @@ -675,8 +724,9 @@ void RcCalculateGomQp (sWelsEncCtx* pEncCtx, SMB* pCurMb, int32_t iSliceId) { · 8 unmodified lines | |||
| 675 | else if (iBitsRatio > 11900) //2^(1.5/6)*10000 | 724 | else if (iBitsRatio > 11900) //2^(1.5/6)*10000 |
| 676 | pSOverRc->iCalculatedQpSlice -= 2; | 725 | pSOverRc->iCalculatedQpSlice -= 2; |
| 677 | } | 726 | } |
| 678 | pSOverRc->iCalculatedQpSlice = WELS_CLIP3 (pSOverRc->iCalculatedQpSlice, pWelsSvcRc->iMinQp, pWelsSvcRc->iMaxQp); | 727 | pSOverRc->iCalculatedQpSlice = WELS_CLIP3 (pSOverRc->iCalculatedQpSlice, pWelsSvcRc->iMinFrameQp, |
| 679 | 728 | pWelsSvcRc->iMaxFrameQp); | |
| 729 | // WelsLog (& (pEncCtx->sLogCtx), WELS_LOG_DEBUG,"iCalculatedQpSlice =%d,iBitsRatio = %d\n",pSOverRc->iCalculatedQpSlice,iBitsRatio); | ||
| 680 | pSOverRc->iGomBitsSlice = 0; | 730 | pSOverRc->iGomBitsSlice = 0; |
| 681 | 731 | ||
| 682 | } | 732 | } |
| @@ -707,6 +757,9 @@ void RcVBufferCalculationSkip (sWelsEncCtx* pEncCtx) { · 24 unmodified lines | |||
| 707 | || (dIncPercent > pWelsSvcRc->iRcVaryPercentage)) { | 757 | || (dIncPercent > pWelsSvcRc->iRcVaryPercentage)) { |
| 708 | pWelsSvcRc->bSkipFlag = true; | 758 | pWelsSvcRc->bSkipFlag = true; |
| 709 | } | 759 | } |
| 760 | WelsLog (& (pEncCtx->sLogCtx), WELS_LOG_DEBUG, | ||
| 761 | "[Rc] VBV_Skip,dIncPercent = %f,iRcVaryPercentage = %d,pWelsSvcRc->bSkipFlag = %d", dIncPercent, | ||
| 762 | pWelsSvcRc->iRcVaryPercentage, pWelsSvcRc->bSkipFlag); | ||
| 710 | } | 763 | } |
| 711 | void CheckFrameSkipBasedMaxbr (sWelsEncCtx* pEncCtx, const long long uiTimeStamp, int32_t iDidIdx) { | 764 | void CheckFrameSkipBasedMaxbr (sWelsEncCtx* pEncCtx, const long long uiTimeStamp, int32_t iDidIdx) { |
| 712 | SWelsSvcRc* pWelsSvcRc = &pEncCtx->pWelsSvcRc[iDidIdx]; | 765 | SWelsSvcRc* pWelsSvcRc = &pEncCtx->pWelsSvcRc[iDidIdx]; |
| @@ -769,7 +822,7 @@ void CheckFrameSkipBasedMaxbr (sWelsEncCtx* pEncCtx, const long long uiTimeStamp · 56 unmodified lines | |||
| 769 | } | 822 | } |
| 770 | } | 823 | } |
| 771 | 824 | ||
| 772 | bool WelsRcCheckFrameStatus (sWelsEncCtx* pEncCtx, long long uiTimeStamp, int32_t iSpatialNum,int32_t iCurDid) { | 825 | bool WelsRcCheckFrameStatus (sWelsEncCtx* pEncCtx, long long uiTimeStamp, int32_t iSpatialNum, int32_t iCurDid) { |
| 773 | 826 | ||
| 774 | bool bSkipMustFlag = false; | 827 | bool bSkipMustFlag = false; |
| 775 | 828 | ||
| @@ -787,7 +840,7 @@ bool WelsRcCheckFrameStatus (sWelsEncCtx* pEncCtx, long long uiTimeStamp, int32_ · 11 unmodified lines | |||
| 787 | } | 840 | } |
| 788 | //check max_br skip | 841 | //check max_br skip |
| 789 | if (pEncCtx->pFuncList->pfRc.pfWelsCheckSkipBasedMaxbr) { | 842 | if (pEncCtx->pFuncList->pfRc.pfWelsCheckSkipBasedMaxbr) { |
| 790 | if ((!bSkipMustFlag)&&(pEncCtx->pSvcParam->sSpatialLayers[iDidIdx].iMaxSpatialBitrate != UNSPECIFIED_BIT_RATE)) { | 843 | if ((!bSkipMustFlag) && (pEncCtx->pSvcParam->sSpatialLayers[iDidIdx].iMaxSpatialBitrate != UNSPECIFIED_BIT_RATE)) { |
| 791 | pEncCtx->pFuncList->pfRc.pfWelsCheckSkipBasedMaxbr (pEncCtx, uiTimeStamp, iDidIdx); | 844 | pEncCtx->pFuncList->pfRc.pfWelsCheckSkipBasedMaxbr (pEncCtx, uiTimeStamp, iDidIdx); |
| 792 | if (true == pEncCtx->pWelsSvcRc[iDidIdx].bSkipFlag) { | 845 | if (true == pEncCtx->pWelsSvcRc[iDidIdx].bSkipFlag) { |
| 793 | bSkipMustFlag = true; | 846 | bSkipMustFlag = true; |
| @@ -810,11 +863,11 @@ bool WelsRcCheckFrameStatus (sWelsEncCtx* pEncCtx, long long uiTimeStamp, int32_ · 16 unmodified lines | |||
| 810 | pEncCtx->pFuncList->pfRc.pfWelsRcPicDelayJudge (pEncCtx, uiTimeStamp, iDidIdx); | 863 | pEncCtx->pFuncList->pfRc.pfWelsRcPicDelayJudge (pEncCtx, uiTimeStamp, iDidIdx); |
| 811 | } | 864 | } |
| 812 | if (true == pEncCtx->pWelsSvcRc[iDidIdx].bSkipFlag) { | 865 | if (true == pEncCtx->pWelsSvcRc[iDidIdx].bSkipFlag) { |
| 813 | bSkipMustFlag = true; | 866 | bSkipMustFlag = true; |
| 814 | } | 867 | } |
| 815 | //check max_br skip | 868 | //check max_br skip |
| 816 | if (pEncCtx->pFuncList->pfRc.pfWelsCheckSkipBasedMaxbr) { | 869 | if (pEncCtx->pFuncList->pfRc.pfWelsCheckSkipBasedMaxbr) { |
| 817 | if ((!bSkipMustFlag)&&(pEncCtx->pSvcParam->sSpatialLayers[iDidIdx].iMaxSpatialBitrate != UNSPECIFIED_BIT_RATE)) { | 870 | if ((!bSkipMustFlag) && (pEncCtx->pSvcParam->sSpatialLayers[iDidIdx].iMaxSpatialBitrate != UNSPECIFIED_BIT_RATE)) { |
| 818 | pEncCtx->pFuncList->pfRc.pfWelsCheckSkipBasedMaxbr (pEncCtx, uiTimeStamp, iDidIdx); | 871 | pEncCtx->pFuncList->pfRc.pfWelsCheckSkipBasedMaxbr (pEncCtx, uiTimeStamp, iDidIdx); |
| 819 | if (true == pEncCtx->pWelsSvcRc[iDidIdx].bSkipFlag) { | 872 | if (true == pEncCtx->pWelsSvcRc[iDidIdx].bSkipFlag) { |
| 820 | bSkipMustFlag = true; | 873 | bSkipMustFlag = true; |
| @@ -847,7 +900,7 @@ void UpdateBufferWhenFrameSkipped (sWelsEncCtx* pEncCtx, int32_t iCurDid) { · 26 unmodified lines | |||
| 847 | pWelsSvcRc->iBufferMaxBRFullness[ODD_TIME_WINDOW] -= kiOutputMaxBits; | 900 | pWelsSvcRc->iBufferMaxBRFullness[ODD_TIME_WINDOW] -= kiOutputMaxBits; |
| 848 | WelsLog (& (pEncCtx->sLogCtx), WELS_LOG_DEBUG, | 901 | WelsLog (& (pEncCtx->sLogCtx), WELS_LOG_DEBUG, |
| 849 | "[Rc] iDid = %d,bits in buffer = %" PRId64 ", bits in Max bitrate buffer = %" PRId64, | 902 | "[Rc] iDid = %d,bits in buffer = %" PRId64 ", bits in Max bitrate buffer = %" PRId64, |
| 850 | iCurDid,pWelsSvcRc->iBufferFullnessSkip, pWelsSvcRc->iBufferMaxBRFullness[EVEN_TIME_WINDOW]); | 903 | iCurDid, pWelsSvcRc->iBufferFullnessSkip, pWelsSvcRc->iBufferMaxBRFullness[EVEN_TIME_WINDOW]); |
| 851 | 904 | ||
| 852 | pWelsSvcRc->iBufferFullnessSkip = WELS_MAX (pWelsSvcRc->iBufferFullnessSkip, 0); | 905 | pWelsSvcRc->iBufferFullnessSkip = WELS_MAX (pWelsSvcRc->iBufferFullnessSkip, 0); |
| 853 | 906 | ||
| @@ -859,7 +912,7 @@ void UpdateBufferWhenFrameSkipped (sWelsEncCtx* pEncCtx, int32_t iCurDid) { · 5 unmodified lines | |||
| 859 | //output a warning when iContinualSkipFrames is large enough, which may indicate subjective quality problem | 912 | //output a warning when iContinualSkipFrames is large enough, which may indicate subjective quality problem |
| 860 | //note that here iContinualSkipFrames must be >0, so the log output will be 3/6/.... | 913 | //note that here iContinualSkipFrames must be >0, so the log output will be 3/6/.... |
| 861 | WelsLog (& (pEncCtx->sLogCtx), WELS_LOG_WARNING, "[Rc] iDid = %d,iContinualSkipFrames(%d) is large", | 914 | WelsLog (& (pEncCtx->sLogCtx), WELS_LOG_WARNING, "[Rc] iDid = %d,iContinualSkipFrames(%d) is large", |
| 862 | iCurDid,pWelsSvcRc->iContinualSkipFrames); | 915 | iCurDid, pWelsSvcRc->iContinualSkipFrames); |
| 863 | } | 916 | } |
| 864 | } | 917 | } |
| 865 | void UpdateMaxBrCheckWindowStatus (sWelsEncCtx* pEncCtx, int32_t iSpatialNum, const long long uiTimeStamp) { | 918 | void UpdateMaxBrCheckWindowStatus (sWelsEncCtx* pEncCtx, int32_t iSpatialNum, const long long uiTimeStamp) { |
| @@ -992,10 +1045,22 @@ void RcUpdateIntraComplexity (sWelsEncCtx* pEncCtx) { · 126 unmodified lines | |||
| 992 | if (iAlpha < (INT_MULTIPLY / 4)) iAlpha = INT_MULTIPLY / 4; | 1045 | if (iAlpha < (INT_MULTIPLY / 4)) iAlpha = INT_MULTIPLY / 4; |
| 993 | int32_t iQStep = RcConvertQp2QStep (pWelsSvcRc->iAverageFrameQp); | 1046 | int32_t iQStep = RcConvertQp2QStep (pWelsSvcRc->iAverageFrameQp); |
| 994 | int64_t iIntraCmplx = iQStep * static_cast<int64_t> (pWelsSvcRc->iFrameDqBits); | 1047 | int64_t iIntraCmplx = iQStep * static_cast<int64_t> (pWelsSvcRc->iFrameDqBits); |
| 995 | pWelsSvcRc->iIntraComplexity = WELS_DIV_ROUND (((INT_MULTIPLY - iAlpha) * pWelsSvcRc->iIntraComplexity + iAlpha * | 1048 | if (pWelsSvcRc->iIdrNum == 0) { |
| 996 | iIntraCmplx), INT_MULTIPLY); | 1049 | pWelsSvcRc->iIntraComplexity = iIntraCmplx; |
| 997 | pWelsSvcRc->iIntraMbCount = pWelsSvcRc->iNumberMbFrame; | 1050 | pWelsSvcRc->iIntraComplxMean = pEncCtx->pVaa->sComplexityAnalysisParam.iFrameComplexity; |
| 1051 | } else { | ||
| 1052 | pWelsSvcRc->iIntraComplexity = WELS_DIV_ROUND (((LINEAR_MODEL_DECAY_FACTOR) * pWelsSvcRc->iIntraComplexity + | ||
| 1053 | (INT_MULTIPLY - LINEAR_MODEL_DECAY_FACTOR) * | ||
| 1054 | iIntraCmplx), INT_MULTIPLY); | ||
| 998 | 1055 | ||
| 1056 | |||
| 1057 | pWelsSvcRc->iIntraComplxMean = WELS_DIV_ROUND64 (((LINEAR_MODEL_DECAY_FACTOR) * static_cast<int64_t> | ||
| 1058 | (pWelsSvcRc->iIntraComplxMean) | ||
| 1059 | + (INT_MULTIPLY - LINEAR_MODEL_DECAY_FACTOR) * (pEncCtx->pVaa->sComplexityAnalysisParam.iFrameComplexity)), | ||
| 1060 | INT_MULTIPLY); | ||
| 1061 | } | ||
| 1062 | |||
| 1063 | pWelsSvcRc->iIntraMbCount = pWelsSvcRc->iNumberMbFrame; | ||
| 999 | pWelsSvcRc->iIdrNum++; | 1064 | pWelsSvcRc->iIdrNum++; |
| 1000 | if (pWelsSvcRc->iIdrNum > 255) | 1065 | if (pWelsSvcRc->iIdrNum > 255) |
| 1001 | pWelsSvcRc->iIdrNum = 255; | 1066 | pWelsSvcRc->iIdrNum = 255; |
| @@ -1011,20 +1076,21 @@ void RcUpdateFrameComplexity (sWelsEncCtx* pEncCtx) { · 9 unmodified lines | |||
| 1011 | SRCTemporal* pTOverRc = &pWelsSvcRc->pTemporalOverRc[kiTl]; | 1076 | SRCTemporal* pTOverRc = &pWelsSvcRc->pTemporalOverRc[kiTl]; |
| 1012 | 1077 | ||
| 1013 | int32_t iQStep = RcConvertQp2QStep (pWelsSvcRc->iAverageFrameQp); | 1078 | int32_t iQStep = RcConvertQp2QStep (pWelsSvcRc->iAverageFrameQp); |
| 1079 | int32_t iAlpha = WELS_DIV_ROUND (INT_MULTIPLY, (1 + pTOverRc->iPFrameNum)); | ||
| 1080 | if (iAlpha < SMOOTH_FACTOR_MIN_VALUE) | ||
| 1081 | iAlpha = SMOOTH_FACTOR_MIN_VALUE; | ||
| 1014 | if (0 == pTOverRc->iPFrameNum) { | 1082 | if (0 == pTOverRc->iPFrameNum) { |
| 1015 | pTOverRc->iLinearCmplx = ((int64_t)pWelsSvcRc->iFrameDqBits) * iQStep; | 1083 | pTOverRc->iLinearCmplx = ((int64_t)pWelsSvcRc->iFrameDqBits) * iQStep; |
| 1084 | pTOverRc->iFrameCmplxMean = pEncCtx->pVaa->sComplexityAnalysisParam.iFrameComplexity; | ||
| 1016 | } else { | 1085 | } else { |
| 1017 | pTOverRc->iLinearCmplx = WELS_DIV_ROUND64 ((LINEAR_MODEL_DECAY_FACTOR * (int64_t)pTOverRc->iLinearCmplx | 1086 | pTOverRc->iLinearCmplx = WELS_DIV_ROUND64 (((LINEAR_MODEL_DECAY_FACTOR) * (int64_t)pTOverRc->iLinearCmplx |
| 1018 | + (INT_MULTIPLY - LINEAR_MODEL_DECAY_FACTOR) * ((int64_t)pWelsSvcRc->iFrameDqBits * iQStep)), | 1087 | + (INT_MULTIPLY - LINEAR_MODEL_DECAY_FACTOR) * ((int64_t)pWelsSvcRc->iFrameDqBits * iQStep)), |
| 1019 | INT_MULTIPLY); | 1088 | INT_MULTIPLY); |
| 1089 | pTOverRc->iFrameCmplxMean = WELS_DIV_ROUND (((LINEAR_MODEL_DECAY_FACTOR) * static_cast<int64_t> | ||
| 1090 | (pTOverRc->iFrameCmplxMean) | ||
| 1091 | + (INT_MULTIPLY - LINEAR_MODEL_DECAY_FACTOR) * pEncCtx->pVaa->sComplexityAnalysisParam.iFrameComplexity), | ||
| 1092 | INT_MULTIPLY); | ||
| 1020 | } | 1093 | } |
| 1021 | int32_t iAlpha = WELS_DIV_ROUND (INT_MULTIPLY, (1 + pTOverRc->iPFrameNum)); | ||
| 1022 | if (iAlpha < SMOOTH_FACTOR_MIN_VALUE) | ||
| 1023 | iAlpha = SMOOTH_FACTOR_MIN_VALUE; | ||
| 1024 | pTOverRc->iFrameCmplxMean = WELS_DIV_ROUND ((LINEAR_MODEL_DECAY_FACTOR * static_cast<int64_t> | ||
| 1025 | (pTOverRc->iFrameCmplxMean) | ||
| 1026 | + (INT_MULTIPLY - LINEAR_MODEL_DECAY_FACTOR) * pEncCtx->pVaa->sComplexityAnalysisParam.iFrameComplexity), | ||
| 1027 | INT_MULTIPLY); | ||
| 1028 | 1094 | ||
| 1029 | 1095 | ||
| 1030 | pTOverRc->iPFrameNum++; | 1096 | pTOverRc->iPFrameNum++; |
| @@ -1066,7 +1132,19 @@ void WelsRcPictureInitGom (sWelsEncCtx* pEncCtx, long long uiTimeStamp) { · 35 unmodified lines | |||
| 1066 | if (pEncCtx->uiTemporalId == 0) { | 1132 | if (pEncCtx->uiTemporalId == 0) { |
| 1067 | RcUpdateTemporalZero (pEncCtx); | 1133 | RcUpdateTemporalZero (pEncCtx); |
| 1068 | } | 1134 | } |
| 1069 | RcDecideTargetBits (pEncCtx); | 1135 | if (pEncCtx->pSvcParam->iRCMode == RC_TIMESTAMP_MODE) { |
| 1136 | RcDecideTargetBitsTimestamp (pEncCtx); | ||
| 1137 | pWelsSvcRc->uiLastTimeStamp = uiTimeStamp; | ||
| 1138 | } else { | ||
| 1139 | RcDecideTargetBits (pEncCtx); | ||
| 1140 | } | ||
| 1141 | //turn off GOM QP when slicenum is larger 1 | ||
| 1142 | if ((pWelsSvcRc->iSliceNum > 1) || ((pEncCtx->pSvcParam->iRCMode == RC_BITRATE_MODE) | ||
| 1143 | && (pEncCtx->eSliceType == I_SLICE))) { | ||
| 1144 | pWelsSvcRc->bEnableGomQp = false; | ||
| 1145 | } else | ||
| 1146 | pWelsSvcRc->bEnableGomQp = true; | ||
| 1147 | |||
| 1070 | //decide globe_qp | 1148 | //decide globe_qp |
| 1071 | if (pEncCtx->eSliceType == I_SLICE) { | 1149 | if (pEncCtx->eSliceType == I_SLICE) { |
| 1072 | if (0 == pWelsSvcRc->iIdrNum) | 1150 | if (0 == pWelsSvcRc->iIdrNum) |
| @@ -1079,7 +1157,6 @@ void WelsRcPictureInitGom (sWelsEncCtx* pEncCtx, long long uiTimeStamp) { · 6 unmodified lines | |||
| 1079 | } | 1157 | } |
| 1080 | RcInitSliceInformation (pEncCtx); | 1158 | RcInitSliceInformation (pEncCtx); |
| 1081 | RcInitGomParameters (pEncCtx); | 1159 | RcInitGomParameters (pEncCtx); |
| 1082 | |||
| 1083 | } | 1160 | } |
| 1084 | 1161 | ||
| 1085 | void WelsRcPictureInfoUpdateGom (sWelsEncCtx* pEncCtx, int32_t iLayerSize) { | 1162 | void WelsRcPictureInfoUpdateGom (sWelsEncCtx* pEncCtx, int32_t iLayerSize) { |
| @@ -1114,21 +1191,22 @@ void WelsRcMbInitGom (sWelsEncCtx* pEncCtx, SMB* pCurMb, SSlice* pSlice) { · 28 unmodified lines | |||
| 1114 | const uint8_t kuiChromaQpIndexOffset = pCurLayer->sLayerInfo.pPpsP->uiChromaQpIndexOffset; | 1191 | const uint8_t kuiChromaQpIndexOffset = pCurLayer->sLayerInfo.pPpsP->uiChromaQpIndexOffset; |
| 1115 | 1192 | ||
| 1116 | pSOverRc->iBsPosSlice = pEncCtx->pFuncList->pfGetBsPosition (pSlice); | 1193 | pSOverRc->iBsPosSlice = pEncCtx->pFuncList->pfGetBsPosition (pSlice); |
| 1117 | if ((pEncCtx->pSvcParam->iRCMode == RC_BITRATE_MODE) && (pEncCtx->eSliceType == I_SLICE)) { | 1194 | if (pWelsSvcRc->bEnableGomQp) { |
| 1195 | //calculate gom qp and target bits at the beginning of gom | ||
| 1196 | if (0 == (pCurMb->iMbXY % pWelsSvcRc->iNumberMbGom)) { | ||
| 1197 | if (pCurMb->iMbXY != pSOverRc->iStartMbSlice) { | ||
| 1198 | pSOverRc->iComplexityIndexSlice++; | ||
| 1199 | RcCalculateGomQp (pEncCtx, pCurMb, kiSliceId); | ||
| 1200 | } | ||
| 1201 | RcGomTargetBits (pEncCtx, kiSliceId); | ||
| 1202 | } | ||
| 1203 | |||
| 1204 | RcCalculateMbQp (pEncCtx, pCurMb, kiSliceId); | ||
| 1205 | } else { | ||
| 1118 | pCurMb->uiLumaQp = pEncCtx->iGlobalQp; | 1206 | pCurMb->uiLumaQp = pEncCtx->iGlobalQp; |
| 1119 | pCurMb->uiChromaQp = g_kuiChromaQpTable[CLIP3_QP_0_51 (pCurMb->uiLumaQp + kuiChromaQpIndexOffset)]; | 1207 | pCurMb->uiChromaQp = g_kuiChromaQpTable[CLIP3_QP_0_51 (pCurMb->uiLumaQp + kuiChromaQpIndexOffset)]; |
| 1120 | return; | ||
| 1121 | } | ||
| 1122 | //calculate gom qp and target bits at the beginning of gom | ||
| 1123 | if (0 == (pCurMb->iMbXY % pWelsSvcRc->iNumberMbGom)) { | ||
| 1124 | if (pCurMb->iMbXY != pSOverRc->iStartMbSlice) { | ||
| 1125 | pSOverRc->iComplexityIndexSlice++; | ||
| 1126 | RcCalculateGomQp (pEncCtx, pCurMb, kiSliceId); | ||
| 1127 | } | ||
| 1128 | RcGomTargetBits (pEncCtx, kiSliceId); | ||
| 1129 | } | 1208 | } |
| 1130 | 1209 | ||
| 1131 | RcCalculateMbQp (pEncCtx, pCurMb, kiSliceId); | ||
| 1132 | } | 1210 | } |
| 1133 | 1211 | ||
| 1134 | void WelsRcMbInfoUpdateGom (sWelsEncCtx* pEncCtx, SMB* pCurMb, int32_t iCostLuma, SSlice* pSlice) { | 1212 | void WelsRcMbInfoUpdateGom (sWelsEncCtx* pEncCtx, SMB* pCurMb, int32_t iCostLuma, SSlice* pSlice) { |
| @@ -1144,9 +1222,6 @@ void WelsRcMbInfoUpdateGom (sWelsEncCtx* pEncCtx, SMB* pCurMb, int32_t iCostLuma · 9 unmodified lines | |||
| 1144 | pSOverRc->iGomBitsSlice += iCurMbBits; | 1222 | pSOverRc->iGomBitsSlice += iCurMbBits; |
| 1145 | 1223 | ||
| 1146 | pWelsSvcRc->pGomCost[kiComplexityIndex] += iCostLuma; | 1224 | pWelsSvcRc->pGomCost[kiComplexityIndex] += iCostLuma; |
| 1147 | |||
| 1148 | pWelsSvcRc->iMinFrameQp = WELS_MIN (pWelsSvcRc->iMinFrameQp, pCurMb->uiLumaQp); | ||
| 1149 | pWelsSvcRc->iMaxFrameQp = WELS_MAX (pWelsSvcRc->iMaxFrameQp, pCurMb->uiLumaQp); | ||
| 1150 | if (iCurMbBits > 0) { | 1225 | if (iCurMbBits > 0) { |
| 1151 | pSOverRc->iTotalQpSlice += pCurMb->uiLumaQp; | 1226 | pSOverRc->iTotalQpSlice += pCurMb->uiLumaQp; |
| 1152 | pSOverRc->iTotalMbSlice++; | 1227 | pSOverRc->iTotalMbSlice++; |
| @@ -1326,7 +1401,8 @@ void WelsRcFrameDelayJudgeTimeStamp (sWelsEncCtx* pEncCtx, long long uiTimeStamp · 173 unmodified lines | |||
| 1326 | pWelsSvcRc->iBufferSizePadding = WELS_DIV_ROUND (pDLayerConfig->iSpatialBitrate * PADDING_BUFFER_RATIO, INT_MULTIPLY); | 1401 | pWelsSvcRc->iBufferSizePadding = WELS_DIV_ROUND (pDLayerConfig->iSpatialBitrate * PADDING_BUFFER_RATIO, INT_MULTIPLY); |
| 1327 | 1402 | ||
| 1328 | pWelsSvcRc->iBufferFullnessSkip -= iSentBits; | 1403 | pWelsSvcRc->iBufferFullnessSkip -= iSentBits; |
| 1329 | pWelsSvcRc->iBufferFullnessSkip = WELS_MAX (0, pWelsSvcRc->iBufferFullnessSkip); | 1404 | pWelsSvcRc->iBufferFullnessSkip = WELS_MAX ((-1) * (pDLayerConfig->iSpatialBitrate / 4), |
| 1405 | pWelsSvcRc->iBufferFullnessSkip); | ||
| 1330 | 1406 | ||
| 1331 | if (pEncCtx->pSvcParam->bEnableFrameSkip) { | 1407 | if (pEncCtx->pSvcParam->bEnableFrameSkip) { |
| 1332 | pWelsSvcRc->bSkipFlag = true; | 1408 | pWelsSvcRc->bSkipFlag = true; |
| @@ -1340,121 +1416,10 @@ void WelsRcFrameDelayJudgeTimeStamp (sWelsEncCtx* pEncCtx, long long uiTimeStamp · 7 unmodified lines | |||
| 1340 | } | 1416 | } |
| 1341 | WelsLog (& (pEncCtx->sLogCtx), WELS_LOG_DEBUG, | 1417 | WelsLog (& (pEncCtx->sLogCtx), WELS_LOG_DEBUG, |
| 1342 | "WelsRcFrameDelayJudgeTimeStamp iDidIdx = %d,iSkipFrameNum = %d,buffer = %" PRId64 | 1418 | "WelsRcFrameDelayJudgeTimeStamp iDidIdx = %d,iSkipFrameNum = %d,buffer = %" PRId64 |
| 1343 | ",threadhold = %d,bitrate = %d,iSentBits = %d,lasttimestamp = %lld,timestamp=%lld\n", iDidIdx, | 1419 | ",threadhold = %d,bitrate = %d,iSentBits = %d,lasttimestamp = %lld,timestamp=%lld", iDidIdx, |
| 1344 | pWelsSvcRc->iSkipFrameNum, pWelsSvcRc->iBufferFullnessSkip, pWelsSvcRc->iBufferSizeSkip, iBitRate, iSentBits, | 1420 | pWelsSvcRc->iSkipFrameNum, pWelsSvcRc->iBufferFullnessSkip, pWelsSvcRc->iBufferSizeSkip, iBitRate, iSentBits, |
| 1345 | pWelsSvcRc->uiLastTimeStamp, uiTimeStamp); | 1421 | pWelsSvcRc->uiLastTimeStamp, uiTimeStamp); |
| 1346 | } | 1422 | } |
| 1347 | void WelsRcPictureInitGomTimeStamp (sWelsEncCtx* pEncCtx, long long uiTimeStamp) { | ||
| 1348 | SWelsSvcRc* pWelsSvcRc = &pEncCtx->pWelsSvcRc[pEncCtx->uiDependencyId]; | ||
| 1349 | SSpatialLayerConfig* pDLayerParam = &pEncCtx->pSvcParam->sSpatialLayers[pEncCtx->uiDependencyId]; | ||
| 1350 | int32_t iLumaQp = pWelsSvcRc->iLastCalculatedQScale; | ||
| 1351 | int32_t iTl = pEncCtx->uiTemporalId; | ||
| 1352 | SRCTemporal* pTOverRc = &pWelsSvcRc->pTemporalOverRc[iTl]; | ||
| 1353 | if (pEncCtx->eSliceType == I_SLICE) { | ||
| 1354 | if (0 == pWelsSvcRc->iIdrNum) { //iIdrNum == 0 means encoder has been initialed | ||
| 1355 | RcInitRefreshParameter (pEncCtx); | ||
| 1356 | } | ||
| 1357 | } | ||
| 1358 | if (RcJudgeBitrateFpsUpdate (pEncCtx)) { | ||
| 1359 | RcUpdateBitrateFps (pEncCtx); | ||
| 1360 | } | ||
| 1361 | if (pEncCtx->uiTemporalId == 0) { | ||
| 1362 | RcUpdateTemporalZero (pEncCtx); | ||
| 1363 | } | ||
| 1364 | //decide one frame bits allocated | ||
| 1365 | if (pEncCtx->eSliceType == I_SLICE) { | ||
| 1366 | if (0 == pWelsSvcRc->iIdrNum) { //iIdrNum == 0 means encoder has been initialed | ||
| 1367 | RcInitIdrQp (pEncCtx); | ||
| 1368 | iLumaQp = pWelsSvcRc->iInitialQp; | ||
| 1369 | pWelsSvcRc->iTargetBits = static_cast<int32_t> (((double) (pDLayerParam->iSpatialBitrate) / (double) ( | ||
| 1370 | pDLayerParam->fFrameRate) * | ||
| 1371 | IDR_BITRATE_RATIO)); | ||
| 1372 | |||
| 1373 | WelsLog (& (pEncCtx->sLogCtx), WELS_LOG_DEBUG, | ||
| 1374 | "[Rc] First IDR iSpatialBitrate = %d,iBufferFullnessSkip = %" PRId64 ",iTargetBits= %d,initQp = %d", | ||
| 1375 | pDLayerParam->iSpatialBitrate, pWelsSvcRc->iBufferFullnessSkip, pWelsSvcRc->iTargetBits, | ||
| 1376 | pWelsSvcRc->iInitialQp); | ||
| 1377 | |||
| 1378 | } else { | ||
| 1379 | int32_t iMaxTh = static_cast<int32_t> (pWelsSvcRc->iBufferSizeSkip - pWelsSvcRc->iBufferFullnessSkip); | ||
| 1380 | int32_t iMinTh = iMaxTh / 2; | ||
| 1381 | pWelsSvcRc->iTargetBits = static_cast<int32_t> (((double) (pDLayerParam->iSpatialBitrate) / (double) ( | ||
| 1382 | pDLayerParam->fFrameRate) * | ||
| 1383 | IDR_BITRATE_RATIO)); | ||
| 1384 | if (iMaxTh > 0) { | ||
| 1385 | pWelsSvcRc->iTargetBits = WELS_CLIP3 (pWelsSvcRc->iTargetBits, iMinTh, iMaxTh); | ||
| 1386 | |||
| 1387 | pWelsSvcRc->iQStep = WELS_DIV_ROUND (pWelsSvcRc->iIntraComplexity, pWelsSvcRc->iTargetBits); | ||
| 1388 | iLumaQp = RcConvertQStep2Qp (pWelsSvcRc->iQStep); | ||
| 1389 | |||
| 1390 | iLumaQp = WELS_CLIP3 (iLumaQp, pWelsSvcRc->iLastCalculatedQScale - DELTA_QP_BGD_THD, | ||
| 1391 | pWelsSvcRc->iLastCalculatedQScale + DELTA_QP_BGD_THD); | ||
| 1392 | |||
| 1393 | } else { | ||
| 1394 | iLumaQp = pEncCtx->iGlobalQp + DELTA_QP_BGD_THD; | ||
| 1395 | } | ||
| 1396 | iLumaQp = WELS_CLIP3 (iLumaQp, pTOverRc->iMinQp, pTOverRc->iMaxQp); | ||
| 1397 | WelsLog (& (pEncCtx->sLogCtx), WELS_LOG_DEBUG, | ||
| 1398 | "[Rc]I iLumaQp = %d,iQStep = %d,iTargetBits = %d,iBufferFullnessSkip =%" PRId64 | ||
| 1399 | ",iMaxTh=%d,iMinTh = %d,iFrameComplexity= %" PRId64, | ||
| 1400 | iLumaQp, pWelsSvcRc->iQStep, pWelsSvcRc->iTargetBits, pWelsSvcRc->iBufferFullnessSkip, iMaxTh, iMinTh, | ||
| 1401 | pWelsSvcRc->iIntraComplexity); | ||
| 1402 | |||
| 1403 | } | ||
| 1404 | |||
| 1405 | } else { | ||
| 1406 | int32_t iMaxTh = static_cast<int32_t> (pWelsSvcRc->iBufferSizeSkip - pWelsSvcRc->iBufferFullnessSkip); | ||
| 1407 | int32_t iMinTh = iMaxTh / (iTl + 2); | ||
| 1408 | |||
| 1409 | SSpatialLayerInternal* pDLayerParamInternal = &pEncCtx->pSvcParam->sDependencyLayers[pEncCtx->uiDependencyId]; | ||
| 1410 | const int32_t kiGopSize = (1 << pDLayerParamInternal->iDecompositionStages); | ||
| 1411 | int32_t iAverageFrameSize = (int32_t) ((double) (pDLayerParam->iSpatialBitrate) / (double) (pDLayerParam->fFrameRate)); | ||
| 1412 | const int32_t kiGopBits = iAverageFrameSize * kiGopSize; | ||
| 1413 | int64_t iCmplxRatio = WELS_DIV_ROUND64 (pEncCtx->pVaa->sComplexityAnalysisParam.iFrameComplexity * INT_MULTIPLY, | ||
| 1414 | pTOverRc->iFrameCmplxMean); | ||
| 1415 | iCmplxRatio = WELS_CLIP3 (iCmplxRatio, INT_MULTIPLY - FRAME_CMPLX_RATIO_RANGE, INT_MULTIPLY + FRAME_CMPLX_RATIO_RANGE); | ||
| 1416 | |||
| 1417 | pWelsSvcRc->iTargetBits = WELS_DIV_ROUND (pTOverRc->iTlayerWeight * kiGopBits, INT_MULTIPLY * 10 * 2); | ||
| 1418 | if (iMaxTh > 0) { | ||
| 1419 | pWelsSvcRc->iTargetBits = WELS_CLIP3 (pWelsSvcRc->iTargetBits, iMinTh, iMaxTh); | ||
| 1420 | if (0 == pTOverRc->iPFrameNum) | ||
| 1421 | iLumaQp = pWelsSvcRc->iInitialQp + DELTA_QP_BGD_THD; | ||
| 1422 | else { | ||
| 1423 | |||
| 1424 | pWelsSvcRc->iQStep = WELS_DIV_ROUND ((pTOverRc->iLinearCmplx * iCmplxRatio), (pWelsSvcRc->iTargetBits * INT_MULTIPLY)); | ||
| 1425 | iLumaQp = RcConvertQStep2Qp (pWelsSvcRc->iQStep); | ||
| 1426 | iLumaQp = WELS_CLIP3 (iLumaQp, pWelsSvcRc->iLastCalculatedQScale - DELTA_QP_BGD_THD, | ||
| 1427 | pWelsSvcRc->iLastCalculatedQScale + DELTA_QP_BGD_THD); | ||
| 1428 | } | ||
| 1429 | } else { | ||
| 1430 | iLumaQp = pEncCtx->iGlobalQp + DELTA_QP_BGD_THD; | ||
| 1431 | } | ||
| 1432 | |||
| 1433 | iLumaQp = WELS_CLIP3 (iLumaQp, pTOverRc->iMinQp, pTOverRc->iMaxQp); | ||
| 1434 | |||
| 1435 | WelsLog (& (pEncCtx->sLogCtx), WELS_LOG_DEBUG, | ||
| 1436 | "[Rc]P iTl = %d,iLumaQp = %d,iQStep = %d,iTargetBits = %d,iBufferFullnessSkip =%" PRId64 | ||
| 1437 | ",iMaxTh=%d,iMinTh = %d,iFrameComplexity= %lld,iCmplxRatio=%" PRId64, | ||
| 1438 | iTl, iLumaQp, pWelsSvcRc->iQStep, pWelsSvcRc->iTargetBits, pWelsSvcRc->iBufferFullnessSkip, iMaxTh, iMinTh, | ||
| 1439 | pEncCtx->pVaa->sComplexityAnalysisParam.iFrameComplexity, iCmplxRatio); | ||
| 1440 | } | ||
| 1441 | |||
| 1442 | pWelsSvcRc->iQStep = RcConvertQp2QStep (iLumaQp); | ||
| 1443 | pWelsSvcRc->iLastCalculatedQScale = iLumaQp; | ||
| 1444 | pEncCtx->iGlobalQp = iLumaQp; | ||
| 1445 | |||
| 1446 | RcInitSliceInformation (pEncCtx); | ||
| 1447 | RcInitGomParameters (pEncCtx); | ||
| 1448 | float fInstantFps = (uiTimeStamp - pWelsSvcRc->uiLastTimeStamp) > 0 ? (1000.0f / (uiTimeStamp - | ||
| 1449 | pWelsSvcRc->uiLastTimeStamp)) : 0; | ||
| 1450 | WelsLog (& (pEncCtx->sLogCtx), WELS_LOG_DEBUG, | ||
| 1451 | "[Rc]Tid = %d,Did = %d,pEncCtx->iGlobalQp= %d,iLumaQp = %d,uiTimeStamp = %lld,uiLastTimeStamp = %lld,InstantFps = %f,settingFps = %f", | ||
| 1452 | pEncCtx->uiTemporalId, pEncCtx->uiDependencyId, | ||
| 1453 | pEncCtx->iGlobalQp, iLumaQp, uiTimeStamp, pWelsSvcRc->uiLastTimeStamp, | ||
| 1454 | fInstantFps, pDLayerParam->fFrameRate); | ||
| 1455 | pWelsSvcRc->uiLastTimeStamp = uiTimeStamp; | ||
| 1456 | |||
| 1457 | } | ||
| 1458 | 1423 | ||
| 1459 | void WelsRcPictureInfoUpdateGomTimeStamp (sWelsEncCtx* pEncCtx, int32_t iLayerSize) { | 1424 | void WelsRcPictureInfoUpdateGomTimeStamp (sWelsEncCtx* pEncCtx, int32_t iLayerSize) { |
| 1460 | SWelsSvcRc* pWelsSvcRc = &pEncCtx->pWelsSvcRc[pEncCtx->uiDependencyId]; | 1425 | SWelsSvcRc* pWelsSvcRc = &pEncCtx->pWelsSvcRc[pEncCtx->uiDependencyId]; |
| @@ -1531,7 +1496,7 @@ void WelsRcInitFuncPointers (sWelsEncCtx* pEncCtx, RC_MODES iRcMode) { · 70 unmodified lines | |||
| 1531 | pRcf->pfWelsRcMbInfoUpdate = WelsRcMbInfoUpdateDisable; | 1496 | pRcf->pfWelsRcMbInfoUpdate = WelsRcMbInfoUpdateDisable; |
| 1532 | 1497 | ||
| 1533 | } else { | 1498 | } else { |
| 1534 | pRcf->pfWelsRcPictureInit = WelsRcPictureInitGomTimeStamp; | 1499 | pRcf->pfWelsRcPictureInit = WelsRcPictureInitGom; |
| 1535 | pRcf->pfWelsRcPictureInfoUpdate = WelsRcPictureInfoUpdateGomTimeStamp; | 1500 | pRcf->pfWelsRcPictureInfoUpdate = WelsRcPictureInfoUpdateGomTimeStamp; |
| 1536 | pRcf->pfWelsRcMbInit = WelsRcMbInitGom; | 1501 | pRcf->pfWelsRcMbInit = WelsRcMbInitGom; |
| 1537 | pRcf->pfWelsRcMbInfoUpdate = WelsRcMbInfoUpdateGom; | 1502 | pRcf->pfWelsRcMbInfoUpdate = WelsRcMbInfoUpdateGom; |