From 1b8caef6ccc7ab35465d03c0a581fa939d6f7477 Mon Sep 17 00:00:00 2001 From: xiaotianshicomcast Date: Wed, 11 Jul 2018 16:59:52 -0700 Subject: [PATCH] Add test function ReorderRefList2 which is not enabled for later use. --- codec/decoder/core/inc/manage_dec_ref.h | 1 + codec/decoder/core/inc/picture.h | 2 + codec/decoder/core/src/decoder_core.cpp | 7 +- codec/decoder/core/src/manage_dec_ref.cpp | 148 ++++++++++++++++++++-- 4 files changed, 145 insertions(+), 13 deletions(-) diff --git a/codec/decoder/core/inc/manage_dec_ref.h b/codec/decoder/core/inc/manage_dec_ref.h index e8e791b48c..7be76f291b 100644 --- a/codec/decoder/core/inc/manage_dec_ref.h +++ b/codec/decoder/core/inc/manage_dec_ref.h @@ -51,6 +51,7 @@ void WelsResetRefPic (PWelsDecoderContext pCtx); int32_t WelsInitRefList (PWelsDecoderContext pCtx, int32_t iPoc); int32_t WelsInitBSliceRefList (PWelsDecoderContext pCtx, int32_t iPoc); int32_t WelsReorderRefList (PWelsDecoderContext pCtx); +int32_t WelsReorderRefList2 (PWelsDecoderContext pCtx); int32_t WelsMarkAsRef (PWelsDecoderContext pCtx); } // namespace WelsDec diff --git a/codec/decoder/core/inc/picture.h b/codec/decoder/core/inc/picture.h index 66e90ec192..46bdd3b360 100644 --- a/codec/decoder/core/inc/picture.h +++ b/codec/decoder/core/inc/picture.h @@ -78,7 +78,9 @@ struct SPicture { uint8_t uiQualityId; int32_t iFrameNum; // frame number //for ref pic management + int32_t iFrameWrapNum; // frame wrap number //for ref pic management int32_t iLongTermFrameIdx; //id for long term ref pic + uint32_t uiLongTermPicNum; //long_term_pic_num int32_t iSpsId; //against mosaic caused by cross-IDR interval reference. int32_t iPpsId; diff --git a/codec/decoder/core/src/decoder_core.cpp b/codec/decoder/core/src/decoder_core.cpp index 12dd9ebfdf..0dd8eef061 100644 --- a/codec/decoder/core/src/decoder_core.cpp +++ b/codec/decoder/core/src/decoder_core.cpp @@ -2327,7 +2327,12 @@ int32_t InitRefPicList (PWelsDecoderContext pCtx, const uint8_t kuiNRi, int32_t } else iRet = WelsInitRefList (pCtx, iPoc); if ((pCtx->eSliceType != I_SLICE && pCtx->eSliceType != SI_SLICE)) { - iRet = WelsReorderRefList (pCtx); +#if 0 + if (pCtx->pSps->uiProfileIdc != 66 && pCtx->pPps->bEntropyCodingModeFlag) + iRet = WelsReorderRefList2 (pCtx); + else +#endif + iRet = WelsReorderRefList (pCtx); } return iRet; diff --git a/codec/decoder/core/src/manage_dec_ref.cpp b/codec/decoder/core/src/manage_dec_ref.cpp index e1bd6fd258..7c7f5acd6e 100644 --- a/codec/decoder/core/src/manage_dec_ref.cpp +++ b/codec/decoder/core/src/manage_dec_ref.cpp @@ -56,8 +56,9 @@ static int32_t MMCOProcess (PWelsDecoderContext pCtx, uint32_t uiMmcoType, static int32_t SlidingWindow (PWelsDecoderContext pCtx); static int32_t AddShortTermToList (PRefPic pRefPic, PPicture pPic); -static int32_t AddLongTermToList (PRefPic pRefPic, PPicture pPic, int32_t iLongTermFrameIdx); -static int32_t MarkAsLongTerm (PRefPic pRefPic, int32_t iFrameNum, int32_t iLongTermFrameIdx); +static int32_t AddLongTermToList (PRefPic pRefPic, PPicture pPic, int32_t iLongTermFrameIdx, uint32_t uiLongTermPicNum); +static int32_t MarkAsLongTerm (PRefPic pRefPic, int32_t iFrameNum, int32_t iLongTermFrameIdx, + uint32_t uiLongTermPicNum); static int32_t WelsCheckAndRecoverForFutureDecoding (PWelsDecoderContext pCtx); #ifdef LONG_TERM_REF int32_t GetLTRFrameIndex (PRefPic pRefPic, int32_t iAncLTRFrameNum); @@ -69,8 +70,10 @@ static void SetUnRef (PPicture pRef) { pRef->bUsedAsRef = false; pRef->bIsLongRef = false; pRef->iFrameNum = -1; + pRef->iFrameWrapNum = -1; //pRef->iFramePoc = 0; pRef->iLongTermFrameIdx = -1; + pRef->uiLongTermPicNum = 0; pRef->uiQualityId = -1; pRef->uiTemporalId = -1; pRef->uiSpatialId = -1; @@ -156,6 +159,23 @@ static int32_t WelsCheckAndRecoverForFutureDecoding (PWelsDecoderContext pCtx) { return ERR_NONE; } +static void WrapShortRefPicNum (PWelsDecoderContext pCtx) { + int32_t i; + PSliceHeader pSliceHeader = &pCtx->pCurDqLayer->sLayerInfo.sSliceInLayer.sSliceHeaderExt.sSliceHeader; + int32_t iMaxPicNum = 1 << pSliceHeader->pSps->uiLog2MaxFrameNum; + PPicture* ppShoreRefList = pCtx->sRefPic.pShortRefList[LIST_0]; + int32_t iShortRefCount = pCtx->sRefPic.uiShortRefCount[LIST_0]; + //wrap pic num + for (i = 0; i < iShortRefCount; i++) { + if (ppShoreRefList[i]) { + if (ppShoreRefList[i]->iFrameNum > pSliceHeader->iFrameNum) + ppShoreRefList[i]->iFrameWrapNum = ppShoreRefList[i]->iFrameNum - iMaxPicNum; + else + ppShoreRefList[i]->iFrameWrapNum = ppShoreRefList[i]->iFrameNum; + } + } +} + /** * fills the pRefPic.pRefList LIST_0 and LIST_0 for B-Slice. */ @@ -164,6 +184,8 @@ int32_t WelsInitBSliceRefList (PWelsDecoderContext pCtx, int32_t iPoc) { int32_t err = WelsCheckAndRecoverForFutureDecoding (pCtx); if (err != ERR_NONE) return err; + WrapShortRefPicNum (pCtx); + PPicture* ppShoreRefList = pCtx->sRefPic.pShortRefList[LIST_0]; PPicture* ppLongRefList = pCtx->sRefPic.pLongRefList[LIST_0]; memset (pCtx->sRefPic.pRefList[LIST_0], 0, MAX_DPB_COUNT * sizeof (PPicture)); @@ -289,6 +311,8 @@ int32_t WelsInitRefList (PWelsDecoderContext pCtx, int32_t iPoc) { int32_t err = WelsCheckAndRecoverForFutureDecoding (pCtx); if (err != ERR_NONE) return err; + WrapShortRefPicNum (pCtx); + PPicture* ppShoreRefList = pCtx->sRefPic.pShortRefList[LIST_0]; PPicture* ppLongRefList = pCtx->sRefPic.pLongRefList[LIST_0]; memset (pCtx->sRefPic.pRefList[LIST_0], 0, MAX_DPB_COUNT * sizeof (PPicture)); @@ -319,10 +343,6 @@ int32_t WelsReorderRefList (PWelsDecoderContext pCtx) { PSliceHeader pSliceHeader = &pCtx->pCurDqLayer->sLayerInfo.sSliceInLayer.sSliceHeaderExt.sSliceHeader; int32_t ListCount = 1; if (pCtx->eSliceType == B_SLICE) ListCount = 2; -// PPicture* ppShoreRefList = pCtx->sRefPic.pShortRefList[LIST_0]; -// int32_t iShortRefCount = pCtx->sRefPic.uiShortRefCount[LIST_0]; -// PPicture* ppLongRefList = pCtx->sRefPic.pLongRefList[LIST_0]; -// int32_t iLongRefCount = pCtx->sRefPic.uiLongRefCount[LIST_0]; for (int32_t listIdx = 0; listIdx < ListCount; ++listIdx) { PPicture pPic = NULL; PPicture* ppRefList = pCtx->sRefPic.pRefList[listIdx]; @@ -404,6 +424,107 @@ int32_t WelsReorderRefList (PWelsDecoderContext pCtx) { return ERR_NONE; } +//WelsReorderRefList2 is the test code +int32_t WelsReorderRefList2 (PWelsDecoderContext pCtx) { + + if (pCtx->eSliceType == I_SLICE || pCtx->eSliceType == SI_SLICE) { + return ERR_NONE; + } + + PRefPicListReorderSyn pRefPicListReorderSyn = pCtx->pCurDqLayer->pRefPicListReordering; + PSliceHeader pSliceHeader = &pCtx->pCurDqLayer->sLayerInfo.sSliceInLayer.sSliceHeaderExt.sSliceHeader; + + PPicture* ppShoreRefList = pCtx->sRefPic.pShortRefList[LIST_0]; + int32_t iShortRefCount = pCtx->sRefPic.uiShortRefCount[LIST_0]; + PPicture* ppLongRefList = pCtx->sRefPic.pLongRefList[LIST_0]; + int32_t iLongRefCount = pCtx->sRefPic.uiLongRefCount[LIST_0]; + int32_t i = 0; + int32_t j = 0; + int32_t k = 0; + int32_t iMaxRefIdx = pCtx->pSps->iNumRefFrames; + const int32_t iCurFrameNum = pSliceHeader->iFrameNum; + const int32_t iMaxPicNum = 1 << pSliceHeader->pSps->uiLog2MaxFrameNum; + int32_t iListCount = 1; + if (pCtx->eSliceType == B_SLICE) iListCount = 2; + for (int32_t listIdx = 0; listIdx < iListCount; ++listIdx) { + PPicture* ppRefList = pCtx->sRefPic.pRefList[listIdx]; + int32_t iCount = 0; + int32_t iRefCount = pSliceHeader->uiRefCount[listIdx]; + int32_t iAbsDiffPicNum = -1; + + if (pRefPicListReorderSyn->bRefPicListReorderingFlag[listIdx]) { + int32_t iPredFrameNum = iCurFrameNum; + for (i = 0; pRefPicListReorderSyn->sReorderingSyn[listIdx][i].uiReorderingOfPicNumsIdc != 3; i++) { + if (iCount >= iMaxRefIdx) + break; + + for (j = iRefCount; j > iCount; j--) + ppRefList[j] = ppRefList[j - 1]; + + uint16_t uiReorderingOfPicNumsIdc = + pRefPicListReorderSyn->sReorderingSyn[listIdx][i].uiReorderingOfPicNumsIdc; + + if (uiReorderingOfPicNumsIdc < 2) { // reorder short references + iAbsDiffPicNum = (int32_t) (pRefPicListReorderSyn->sReorderingSyn[listIdx][i].uiAbsDiffPicNumMinus1 + 1); + if (uiReorderingOfPicNumsIdc == 0) { + if (iPredFrameNum - iAbsDiffPicNum < 0) + iPredFrameNum -= (iAbsDiffPicNum - iMaxPicNum); + else + iPredFrameNum -= iAbsDiffPicNum; + } else { + if (iPredFrameNum + iAbsDiffPicNum >= iMaxPicNum) + iPredFrameNum += (iAbsDiffPicNum - iMaxPicNum); + else + iPredFrameNum += iAbsDiffPicNum; + } + + if (iPredFrameNum > iCurFrameNum) { + iPredFrameNum -= iMaxPicNum; + } + + for (j = 0; j < iShortRefCount; j++) { + if (ppShoreRefList[j]) { + if (ppShoreRefList[j]->iFrameWrapNum == iPredFrameNum) { + ppRefList[iCount++] = ppShoreRefList[j]; + break; + } + } + } + k = iCount; + for (j = k; j <= iRefCount; j++) { + if (ppRefList[j] != NULL) { + if (ppRefList[j]->bIsLongRef || ppRefList[j]->iFrameWrapNum != iPredFrameNum) + ppRefList[k++] = ppRefList[j]; + } + } + } else { // reorder long term references uiReorderingOfPicNumsIdc == 2 + iPredFrameNum = pRefPicListReorderSyn->sReorderingSyn[listIdx][i].uiLongTermPicNum; + for (j = 0; j < iLongRefCount; j++) { + if (ppLongRefList[j] != NULL) { + if (ppLongRefList[j]->uiLongTermPicNum == (uint32_t)iPredFrameNum) { + ppRefList[iCount++] = ppLongRefList[j]; + break; + } + } + } + k = iCount; + for (j = k; j <= iRefCount; j++) { + if (ppRefList[j] != NULL) { + if (!ppRefList[j]->bIsLongRef || ppLongRefList[j]->uiLongTermPicNum != (uint32_t)iPredFrameNum) + ppRefList[k++] = ppRefList[j]; + } + } + } + } + } + + for (i = WELS_MAX (1, WELS_MAX (iCount, pCtx->sRefPic.uiRefCount[listIdx])); i < iRefCount; i++) + ppRefList[i] = ppRefList[i - 1]; + pCtx->sRefPic.uiRefCount[listIdx] = (uint8_t)WELS_MIN (WELS_MAX (iCount, pCtx->sRefPic.uiRefCount[listIdx]), iRefCount); + } + return ERR_NONE; +} + int32_t WelsMarkAsRef (PWelsDecoderContext pCtx) { PRefPic pRefPic = &pCtx->sRefPic; PRefPicMarking pRefPicMarking = pCtx->pCurDqLayer->pRefPicMarking; @@ -428,7 +549,7 @@ int32_t WelsMarkAsRef (PWelsDecoderContext pCtx) { if (bIsIDRAU) { if (pRefPicMarking->bLongTermRefFlag) { pCtx->sRefPic.iMaxLongTermFrameIdx = 0; - AddLongTermToList (pRefPic, pCtx->pDec, 0); + AddLongTermToList (pRefPic, pCtx->pDec, 0, 0); } else { pCtx->sRefPic.iMaxLongTermFrameIdx = -1; } @@ -539,7 +660,7 @@ static int32_t MMCOProcess (PWelsDecoderContext pCtx, uint32_t uiMmcoType, pCtx->iFrameNumOfAuMarkedLtr); #endif - MarkAsLongTerm (pRefPic, iShortFrameNum, iLongTermFrameIdx); + MarkAsLongTerm (pRefPic, iShortFrameNum, iLongTermFrameIdx, uiLongTermPicNum); break; case MMCO_SET_MAX_LONG: pRefPic->iMaxLongTermFrameIdx = iMaxLongTermFrameIdx; @@ -567,7 +688,7 @@ static int32_t MMCOProcess (PWelsDecoderContext pCtx, uint32_t uiMmcoType, WelsLog (& (pCtx->sLogCtx), WELS_LOG_INFO, "ex_mark_avc():::MMCO_LONG:::LTR marking....iFrameNum: %d", pCtx->iFrameNum); #endif - iRet = AddLongTermToList (pRefPic, pCtx->pDec, iLongTermFrameIdx); + iRet = AddLongTermToList (pRefPic, pCtx->pDec, iLongTermFrameIdx, uiLongTermPicNum); break; default : break; @@ -682,12 +803,14 @@ static int32_t AddShortTermToList (PRefPic pRefPic, PPicture pPic) { return ERR_NONE; } -static int32_t AddLongTermToList (PRefPic pRefPic, PPicture pPic, int32_t iLongTermFrameIdx) { +static int32_t AddLongTermToList (PRefPic pRefPic, PPicture pPic, int32_t iLongTermFrameIdx, + uint32_t uiLongTermPicNum) { int32_t i = 0; pPic->bUsedAsRef = true; pPic->bIsLongRef = true; pPic->iLongTermFrameIdx = iLongTermFrameIdx; + pPic->uiLongTermPicNum = uiLongTermPicNum; if (pRefPic->uiLongRefCount[LIST_0] == 0) { pRefPic->pLongRefList[LIST_0][pRefPic->uiLongRefCount[LIST_0]] = pPic; } else { @@ -705,7 +828,8 @@ static int32_t AddLongTermToList (PRefPic pRefPic, PPicture pPic, int32_t iLongT return ERR_NONE; } -static int32_t MarkAsLongTerm (PRefPic pRefPic, int32_t iFrameNum, int32_t iLongTermFrameIdx) { +static int32_t MarkAsLongTerm (PRefPic pRefPic, int32_t iFrameNum, int32_t iLongTermFrameIdx, + uint32_t uiLongTermPicNum) { PPicture pPic = NULL; int32_t i = 0; int32_t iRet = ERR_NONE; @@ -714,7 +838,7 @@ static int32_t MarkAsLongTerm (PRefPic pRefPic, int32_t iFrameNum, int32_t iLong for (i = 0; i < pRefPic->uiRefCount[LIST_0]; i++) { pPic = pRefPic->pRefList[LIST_0][i]; if (pPic->iFrameNum == iFrameNum && !pPic->bIsLongRef) { - iRet = AddLongTermToList (pRefPic, pPic, iLongTermFrameIdx); + iRet = AddLongTermToList (pRefPic, pPic, iLongTermFrameIdx, uiLongTermPicNum); break; } }