Showing the changes to codec/encoder/core/src/svc_encode_slice.cpp (1 of 14 files). Show all
Unified · Split
| @@ -460,7 +460,25 @@ void OutputPMbWithoutConstructCsRsNoCopy (sWelsEncCtx* pCtx, SDqLayer* pDq, SSli · 459 unmodified lines | ||
| 460 | 460 | pfIdctFour4x4 (pDecV, kiDecStrideChroma, pDecV, kiDecStrideChroma, pScaledTcoeff + 320); |
| 461 | 461 | } |
| 462 | 462 | } |
| 463 | − | |
| 463 | +inline void StashMBStatus (SDynamicSlicingStack* pDss, SBitStringAux* pBs, SSlice* pSlice, int32_t iMbSkipRun = 0) { | |
| 464 | + pDss->pBsStackBufPtr = pBs->pBufPtr; | |
| 465 | + pDss->uiBsStackCurBits = pBs->uiCurBits; | |
| 466 | + pDss->iBsStackLeftBits = pBs->iLeftBits; | |
| 467 | + pDss->uiLastMbQp = pSlice->uiLastMbQp; | |
| 468 | + pDss->iMbSkipRunStack = iMbSkipRun; | |
| 469 | +} | |
| 470 | +void StashPopMBStatus (SDynamicSlicingStack* pDss, SBitStringAux* pBs, SSlice* pSlice, int32_t* pMbSkipRun = 0) { | |
| 471 | + pBs->pBufPtr = pDss->pBsStackBufPtr; | |
| 472 | + pBs->uiCurBits = pDss->uiBsStackCurBits; | |
| 473 | + pBs->iLeftBits = pDss->iBsStackLeftBits; | |
| 474 | + pSlice->uiLastMbQp = pDss->uiLastMbQp; | |
| 475 | + if(pMbSkipRun) | |
| 476 | + *pMbSkipRun = pDss->iMbSkipRunStack; | |
| 477 | +} | |
| 478 | +void UpdateQpForOverflow (SMB* pCurMb, uint8_t kuiChromaQpIndexOffset) { | |
| 479 | + pCurMb->uiLumaQp += DELTA_QP; | |
| 480 | + pCurMb->uiChromaQp = g_kuiChromaQpTable[CLIP3_QP_0_51 (pCurMb->uiLumaQp + kuiChromaQpIndexOffset)]; | |
| 481 | +} | |
| 464 | 482 | // for intra non-dynamic pSlice |
| 465 | 483 | //encapsulate two kinds of reconstruction: |
| 466 | 484 | //first. store base or highest Dependency Layer with only one quality (without CS RS reconstruction) |
| @@ -477,22 +495,31 @@ int32_t WelsISliceMdEnc (sWelsEncCtx* pEncCtx, SSlice* pSlice) { //pMd + encodin · 10 unmodified lines | ||
| 477 | 495 | const int32_t kiTotalNumMb = pCurLayer->iMbWidth * pCurLayer->iMbHeight; |
| 478 | 496 | int32_t iCurMbIdx = 0, iNumMbCoded = 0; |
| 479 | 497 | const int32_t kiSliceIdx = pSlice->uiSliceIdx; |
| 498 | + const uint8_t kuiChromaQpIndexOffset = pCurLayer->sLayerInfo.pPpsP->uiChromaQpIndexOffset; | |
| 480 | 499 | |
| 481 | 500 | SWelsMD sMd; |
| 482 | 501 | int32_t iEncReturn = ENC_RETURN_SUCCESS; |
| 483 | − | |
| 502 | + SBitStringAux* pBs = pSlice->pSliceBsa; | |
| 503 | + SDynamicSlicingStack sDss; | |
| 484 | 504 | for (; ;) { |
| 505 | + StashMBStatus (&sDss, pBs, pSlice); | |
| 485 | 506 | iCurMbIdx = iNextMbIdx; |
| 486 | 507 | pCurMb = &pMbList[ iCurMbIdx ]; |
| 508 | + | |
| 487 | 509 | pEncCtx->pFuncList->pfRc.pfWelsRcMbInit (pEncCtx, pCurMb, pSlice); |
| 510 | + WelsMdIntraInit (pEncCtx, pCurMb, pMbCache, kiSliceFirstMbXY); | |
| 488 | 511 | |
| 512 | +TRY_REENCODING: | |
| 489 | 513 | sMd.iLambda = g_kiQpCostTable[pCurMb->uiLumaQp]; |
| 490 | − | |
| 491 | − WelsMdIntraInit (pEncCtx, pCurMb, pMbCache, kiSliceFirstMbXY); | |
| 492 | 514 | WelsMdIntraMb (pEncCtx, &sMd, pCurMb, pMbCache); |
| 493 | 515 | UpdateNonZeroCountCache (pCurMb, pMbCache); |
| 494 | 516 | |
| 495 | 517 | iEncReturn = WelsSpatialWriteMbSyn (pEncCtx, pSlice, pCurMb); |
| 518 | + if (iEncReturn == ENC_RETURN_VLCOVERFLOWFOUND) { | |
| 519 | + StashPopMBStatus (&sDss, pBs, pSlice); | |
| 520 | + UpdateQpForOverflow (pCurMb, kuiChromaQpIndexOffset); | |
| 521 | + goto TRY_REENCODING; | |
| 522 | + } | |
| 496 | 523 | if (ENC_RETURN_SUCCESS != iEncReturn) |
| 497 | 524 | return iEncReturn; |
| 498 | 525 | |
| @@ -505,7 +532,6 @@ int32_t WelsISliceMdEnc (sWelsEncCtx* pEncCtx, SSlice* pSlice) { //pMd + encodin · 6 unmodified lines | ||
| 505 | 532 | pEncCtx->pFuncList->pfRc.pfWelsRcMbInfoUpdate (pEncCtx, pCurMb, sMd.iCostLuma, pSlice); |
| 506 | 533 | |
| 507 | 534 | ++iNumMbCoded; |
| 508 | − | |
| 509 | 535 | iNextMbIdx = WelsGetNextMbOfSlice (pSliceCtx, iCurMbIdx); |
| 510 | 536 | if (iNextMbIdx == -1 || iNextMbIdx >= kiTotalNumMb || iNumMbCoded >= kiTotalNumMb) { |
| 511 | 537 | break; |
| @@ -540,24 +566,27 @@ int32_t WelsISliceMdEncDynamic (sWelsEncCtx* pEncCtx, SSlice* pSlice) { //pMd + · 28 unmodified lines | ||
| 540 | 566 | for (; ;) { |
| 541 | 567 | iCurMbIdx = iNextMbIdx; |
| 542 | 568 | pCurMb = &pMbList[ iCurMbIdx ]; |
| 569 | + | |
| 570 | + StashMBStatus (&sDss, pBs, pSlice); | |
| 543 | 571 | pEncCtx->pFuncList->pfRc.pfWelsRcMbInit (pEncCtx, pCurMb, pSlice); |
| 544 | 572 | // if already reaches the largest number of slices, set QPs to the upper bound |
| 545 | 573 | if (pSlice->bDynamicSlicingSliceSizeCtrlFlag) { |
| 546 | 574 | pCurMb->uiLumaQp = pEncCtx->pWelsSvcRc[pEncCtx->uiDependencyId].iMaxQp; |
| 547 | 575 | pCurMb->uiChromaQp = g_kuiChromaQpTable[CLIP3_QP_0_51 (pCurMb->uiLumaQp + kuiChromaQpIndexOffset)]; |
| 548 | 576 | } |
| 577 | + WelsMdIntraInit (pEncCtx, pCurMb, pMbCache, kiSliceFirstMbXY); | |
| 549 | 578 | |
| 579 | +TRY_REENCODING: | |
| 550 | 580 | sMd.iLambda = g_kiQpCostTable[pCurMb->uiLumaQp]; |
| 551 | − | |
| 552 | − WelsMdIntraInit (pEncCtx, pCurMb, pMbCache, kiSliceFirstMbXY); | |
| 553 | 581 | WelsMdIntraMb (pEncCtx, &sMd, pCurMb, pMbCache); |
| 554 | 582 | UpdateNonZeroCountCache (pCurMb, pMbCache); |
| 555 | − //stack pBs pointer | |
| 556 | − sDss.pBsStackBufPtr = pBs->pBufPtr; | |
| 557 | − sDss.uiBsStackCurBits = pBs->uiCurBits; | |
| 558 | − sDss.iBsStackLeftBits = pBs->iLeftBits; | |
| 559 | 583 | |
| 560 | 584 | iEncReturn = WelsSpatialWriteMbSyn (pEncCtx, pSlice, pCurMb); |
| 585 | + if (iEncReturn == ENC_RETURN_VLCOVERFLOWFOUND) { | |
| 586 | + StashPopMBStatus (&sDss, pBs, pSlice); | |
| 587 | + UpdateQpForOverflow (pCurMb, kuiChromaQpIndexOffset); | |
| 588 | + goto TRY_REENCODING; | |
| 589 | + } | |
| 561 | 590 | if (ENC_RETURN_SUCCESS != iEncReturn) |
| 562 | 591 | return iEncReturn; |
| 563 | 592 | |
| @@ -926,18 +955,22 @@ int32_t WelsMdInterMbLoop (sWelsEncCtx* pEncCtx, SSlice* pSlice, void* pWelsMd, · 362 unmodified lines | ||
| 926 | 955 | const int32_t kiSliceIdx = pSlice->uiSliceIdx; |
| 927 | 956 | const uint8_t kuiChromaQpIndexOffset = pCurLayer->sLayerInfo.pPpsP->uiChromaQpIndexOffset; |
| 928 | 957 | int32_t iEncReturn = ENC_RETURN_SUCCESS; |
| 929 | − | |
| 958 | + SDynamicSlicingStack sDss; | |
| 930 | 959 | for (;;) { |
| 960 | + StashMBStatus (&sDss, pBs, pSlice, iMbSkipRun); | |
| 931 | 961 | //point to current pMb |
| 932 | 962 | iCurMbIdx = iNextMbIdx; |
| 933 | 963 | pCurMb = &pMbList[ iCurMbIdx ]; |
| 934 | 964 | |
| 965 | + | |
| 935 | 966 | //step(1): set QP for the current MB |
| 936 | 967 | pEncCtx->pFuncList->pfRc.pfWelsRcMbInit (pEncCtx, pCurMb, pSlice); |
| 937 | 968 | |
| 938 | 969 | //step (2). save some vale for future use, initial pWelsMd |
| 939 | 970 | WelsMdIntraInit (pEncCtx, pCurMb, pMbCache, kiSliceFirstMbXY); |
| 940 | 971 | WelsMdInterInit (pEncCtx, pSlice, pCurMb, kiSliceFirstMbXY); |
| 972 | + | |
| 973 | +TRY_REENCODING: | |
| 941 | 974 | WelsInitInterMDStruc (pCurMb, pMvdCostTableInter, kiMvdInterTableStride, pMd); |
| 942 | 975 | pEncCtx->pFuncList->pfInterMd (pEncCtx, pMd, pSlice, pCurMb, pMbCache); |
| 943 | 976 | //mb_qp |
| @@ -961,6 +994,11 @@ int32_t WelsMdInterMbLoop (sWelsEncCtx* pEncCtx, SSlice* pSlice, void* pWelsMd, · 17 unmodified lines | ||
| 961 | 994 | BsWriteUE (pBs, iMbSkipRun); |
| 962 | 995 | iMbSkipRun = 0; |
| 963 | 996 | iEncReturn = WelsSpatialWriteMbSyn (pEncCtx, pSlice, pCurMb); |
| 997 | + if (iEncReturn == ENC_RETURN_VLCOVERFLOWFOUND) { | |
| 998 | + StashPopMBStatus (&sDss, pBs, pSlice, &iMbSkipRun); | |
| 999 | + UpdateQpForOverflow (pCurMb, kuiChromaQpIndexOffset); | |
| 1000 | + goto TRY_REENCODING; | |
| 1001 | + } | |
| 964 | 1002 | if (ENC_RETURN_SUCCESS != iEncReturn) |
| 965 | 1003 | return iEncReturn; |
| 966 | 1004 | } |
| @@ -1018,6 +1056,10 @@ int32_t WelsMdInterMbLoopOverDynamicSlice (sWelsEncCtx* pEncCtx, SSlice* pSlice, · 51 unmodified lines | ||
| 1018 | 1056 | SDynamicSlicingStack sDss; |
| 1019 | 1057 | sDss.iStartPos = BsGetBitsPos (pBs); |
| 1020 | 1058 | for (;;) { |
| 1059 | + //DYNAMIC_SLICING_ONE_THREAD - MultiD | |
| 1060 | + //stack pBs pointer | |
| 1061 | + StashMBStatus (&sDss, pBs, pSlice, iMbSkipRun); | |
| 1062 | + | |
| 1021 | 1063 | //point to current pMb |
| 1022 | 1064 | iCurMbIdx = iNextMbIdx; |
| 1023 | 1065 | pCurMb = &pMbList[ iCurMbIdx ]; |
| @@ -1036,6 +1078,8 @@ int32_t WelsMdInterMbLoopOverDynamicSlice (sWelsEncCtx* pEncCtx, SSlice* pSlice, · 12 unmodified lines | ||
| 1036 | 1078 | //step (2). save some vale for future use, initial pWelsMd |
| 1037 | 1079 | WelsMdIntraInit (pEncCtx, pCurMb, pMbCache, kiSliceFirstMbXY); |
| 1038 | 1080 | WelsMdInterInit (pEncCtx, pSlice, pCurMb, kiSliceFirstMbXY); |
| 1081 | + | |
| 1082 | +TRY_REENCODING: | |
| 1039 | 1083 | WelsInitInterMDStruc (pCurMb, pMvdCostTableInter, kiMvdInterTableStride, pMd); |
| 1040 | 1084 | pEncCtx->pFuncList->pfInterMd (pEncCtx, pMd, pSlice, pCurMb, pMbCache); |
| 1041 | 1085 | //mb_qp |
| @@ -1051,15 +1095,6 @@ int32_t WelsMdInterMbLoopOverDynamicSlice (sWelsEncCtx* pEncCtx, SSlice* pSlice, · 9 unmodified lines | ||
| 1051 | 1095 | |
| 1052 | 1096 | //step (6): begin to write bit stream; if the pSlice size is controlled, the writing may be skipped |
| 1053 | 1097 | |
| 1054 | − //DYNAMIC_SLICING_ONE_THREAD - MultiD | |
| 1055 | − //stack pBs pointer | |
| 1056 | − sDss.pBsStackBufPtr = pBs->pBufPtr; | |
| 1057 | − sDss.uiBsStackCurBits = pBs->uiCurBits; | |
| 1058 | − sDss.iBsStackLeftBits = pBs->iLeftBits; | |
| 1059 | − //stack Pskip status | |
| 1060 | − sDss.iMbSkipRunStack = iMbSkipRun; | |
| 1061 | − //DYNAMIC_SLICING_ONE_THREAD - MultiD | |
| 1062 | − | |
| 1063 | 1098 | if (IS_SKIP (pCurMb->uiMbType)) { |
| 1064 | 1099 | pCurMb->uiLumaQp = pSlice->uiLastMbQp; |
| 1065 | 1100 | pCurMb->uiChromaQp = g_kuiChromaQpTable[CLIP3_QP_0_51 (pCurMb->uiLumaQp + kuiChromaQpIndexOffset)]; |
| @@ -1069,6 +1104,11 @@ int32_t WelsMdInterMbLoopOverDynamicSlice (sWelsEncCtx* pEncCtx, SSlice* pSlice, · 3 unmodified lines | ||
| 1069 | 1104 | BsWriteUE (pBs, iMbSkipRun); |
| 1070 | 1105 | iMbSkipRun = 0; |
| 1071 | 1106 | iEncReturn = WelsSpatialWriteMbSyn (pEncCtx, pSlice, pCurMb); |
| 1107 | + if (iEncReturn == ENC_RETURN_VLCOVERFLOWFOUND) { | |
| 1108 | + StashPopMBStatus (&sDss, pBs, pSlice, &iMbSkipRun); | |
| 1109 | + UpdateQpForOverflow (pCurMb, kuiChromaQpIndexOffset); | |
| 1110 | + goto TRY_REENCODING; | |
| 1111 | + } | |
| 1072 | 1112 | if (ENC_RETURN_SUCCESS != iEncReturn) |
| 1073 | 1113 | return iEncReturn; |
| 1074 | 1114 | } |