Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions codec/decoder/core/inc/nalu.h
Original file line number Diff line number Diff line change
Expand Up @@ -72,6 +72,7 @@ typedef struct TagAccessUnits {
uint32_t uiStartPos;
uint32_t uiEndPos;
bool bCompletedAuFlag; // Indicate whether it is a completed AU
void* pExpandedBlock; // latest block malloced by ExpandNalUnitList, chained to the ones before it
} SAccessUnit, *PAccessUnit;

} // namespace WelsDec
Expand Down
58 changes: 40 additions & 18 deletions codec/decoder/core/src/memmgr_nal_unit.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -79,6 +79,7 @@ int32_t MemInitNalList (PAccessUnit* ppAu, const uint32_t kuiSize, CMemoryAlign*
(*ppAu)->uiStartPos = 0;
(*ppAu)->uiEndPos = 0;
(*ppAu)->bCompletedAuFlag = false;
(*ppAu)->pExpandedBlock = NULL;

return ERR_NONE;
}
Expand All @@ -87,6 +88,12 @@ int32_t MemFreeNalList (PAccessUnit* ppAu, CMemoryAlign* pMa) {
if (ppAu != NULL) {
PAccessUnit pAu = *ppAu;
if (pAu != NULL) {
uint8_t* pBlock = (uint8_t*)pAu->pExpandedBlock;
while (pBlock != NULL) { // blocks added by ExpandNalUnitList, latest first
uint8_t* pPrevBlock = * ((uint8_t**)pBlock);
pMa->WelsFree (pBlock, "Access Unit expanded");
pBlock = pPrevBlock;
}
pMa->WelsFree (pAu, "Access Unit");
*ppAu = NULL;
}
Expand All @@ -95,29 +102,44 @@ int32_t MemFreeNalList (PAccessUnit* ppAu, CMemoryAlign* pMa) {
}


/*
* ExpandNalUnitList
* Grow the list of NAL Unit pointers to kiExpSize and add new NAL Units for the new entries.
* NAL Units already in the list stay where they are until MemFreeNalList: decoding keeps pointers
* into them (pCtx->pNalCur, pCtx->pSliceHeader, the current dq layer's ref marking and reordering).
*/
int32_t ExpandNalUnitList (PAccessUnit* ppAu, const int32_t kiOrgSize, const int32_t kiExpSize, CMemoryAlign* pMa) {
if (kiExpSize <= kiOrgSize)
return ERR_INFO_INVALID_PARAM;
else {
PAccessUnit pTmp = NULL;
PAccessUnit pAu = *ppAu;
uint8_t* pBase = NULL, *pPtr = NULL;
PNalUnit* pNalUnitsList = NULL;
int32_t iIdx = 0;
int32_t iRet = ERR_NONE;
if ((iRet = MemInitNalList (&pTmp, kiExpSize, pMa)) != ERR_NONE) // request new list with expanding
return iRet;

do {
memcpy (pTmp->pNalUnitsList[iIdx], (*ppAu)->pNalUnitsList[iIdx], sizeof (SNalUnit)); //confirmed_safe_unsafe_usage
++ iIdx;
} while (iIdx < kiOrgSize);

pTmp->uiCountUnitsNum = kiExpSize;
pTmp->uiAvailUnitsNum = (*ppAu)->uiAvailUnitsNum;
pTmp->uiActualUnitsNum = (*ppAu)->uiActualUnitsNum;
pTmp->uiEndPos = (*ppAu)->uiEndPos;
pTmp->bCompletedAuFlag = (*ppAu)->bCompletedAuFlag;

MemFreeNalList (ppAu, pMa); // free old list
*ppAu = pTmp;
const uint32_t kuiSizeBlockLink = WELS_ALIGN (sizeof (uint8_t*), 16); // keeps the NAL Units after it aligned
const uint32_t kuiSizeNalUnit = sizeof (SNalUnit);
const uint32_t kuiSizeNalUnits = (kiExpSize - kiOrgSize) * kuiSizeNalUnit;
const uint32_t kuiSizeNalUnitPtr = kiExpSize * sizeof (PNalUnit);
const uint32_t kuiCountSize = kuiSizeBlockLink + kuiSizeNalUnits + kuiSizeNalUnitPtr;

pBase = (uint8_t*)pMa->WelsMallocz (kuiCountSize, "Access Unit expanded");
if (pBase == NULL)
return ERR_INFO_OUT_OF_MEMORY;
pPtr = pBase;
* ((uint8_t**)pPtr) = (uint8_t*)pAu->pExpandedBlock; // chain to the block expanded before, freed in MemFreeNalList
pPtr += kuiSizeBlockLink;
pNalUnitsList = (PNalUnit*) (pPtr + kuiSizeNalUnits);

for (iIdx = 0; iIdx < kiOrgSize; ++ iIdx) // keep the NAL Units in place, in their current order
pNalUnitsList[iIdx] = pAu->pNalUnitsList[iIdx];
for (; iIdx < kiExpSize; ++ iIdx) {
pNalUnitsList[iIdx] = (PNalUnit)pPtr;
pPtr += kuiSizeNalUnit;
}

pAu->pNalUnitsList = pNalUnitsList;
pAu->uiCountUnitsNum = kiExpSize;
pAu->pExpandedBlock = pBase;
return ERR_NONE;
}
}
Expand Down
41 changes: 41 additions & 0 deletions test/decoder/DecUT_ParseSyntax.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@
#include "decoder_core.h"
#include "error_concealment.h"
#include "fmo.h"
#include "memmgr_nal_unit.h"
#include "welsCodecTrace.h"
#include "../../common/src/welsCodecTrace.cpp"

Expand Down Expand Up @@ -766,3 +767,43 @@ TEST_F (DecoderParseSyntaxTest, WelsDecodeBsRejectsWrapIntoQueuedSlice) {
Uninit();
}

// MemGetNextNal grows the NAL Unit list once an AU holds more NAL Units than it was sized for.
// The NAL Units handed out before must stay valid and unchanged: decoding keeps pointers into
// them, such as pCtx->pSliceHeader (issue #3684).
TEST (DecoderNalUnitListTest, ExpandNalUnitListKeepsNalUnitsInPlace) {
CMemoryAlign cMemoryAlign (16);
PAccessUnit pAu = NULL;
ASSERT_EQ (ERR_NONE, MemInitNalList (&pAu, MAX_NAL_UNIT_NUM_IN_AU, &cMemoryAlign));
ASSERT_TRUE (pAu != NULL);

const uint32_t kuiOrgCount = pAu->uiCountUnitsNum;
ASSERT_EQ ((uint32_t)MAX_NAL_UNIT_NUM_IN_AU, kuiOrgCount);
PNalUnit pNalUnits[MAX_NAL_UNIT_NUM_IN_AU];
for (uint32_t i = 0; i < kuiOrgCount; i++) {
pNalUnits[i] = MemGetNextNal (&pAu, &cMemoryAlign);
ASSERT_TRUE (pNalUnits[i] != NULL);
pNalUnits[i]->sNalData.sVclNal.sSliceHeaderExt.sSliceHeader.iPicOrderCntLsb = i + 1;
}
PSliceHeader pSliceHeader = &pNalUnits[0]->sNalData.sVclNal.sSliceHeaderExt.sSliceHeader;

// Enough NAL Units to grow the list twice, the second time from an already grown list.
const uint32_t kuiMoreCount = (MAX_NAL_UNIT_NUM_IN_AU >> 1) + 1;
for (uint32_t i = 0; i < kuiMoreCount; i++) {
PNalUnit pNalUnit = MemGetNextNal (&pAu, &cMemoryAlign);
ASSERT_TRUE (pNalUnit != NULL);
for (uint32_t j = 0; j < kuiOrgCount; j++)
EXPECT_NE (pNalUnits[j], pNalUnit);
}
EXPECT_EQ (kuiOrgCount + kuiMoreCount, pAu->uiAvailUnitsNum);
EXPECT_GE (pAu->uiCountUnitsNum, pAu->uiAvailUnitsNum);

for (uint32_t i = 0; i < kuiOrgCount; i++) {
ASSERT_EQ (pNalUnits[i], pAu->pNalUnitsList[i]);
EXPECT_EQ ((int32_t) (i + 1), pNalUnits[i]->sNalData.sVclNal.sSliceHeaderExt.sSliceHeader.iPicOrderCntLsb);
}
pSliceHeader->iPicOrderCntLsb = 0; // as ParseDecRefPicMarking does on MMCO 5
EXPECT_EQ (0, pAu->pNalUnitsList[0]->sNalData.sVclNal.sSliceHeaderExt.sSliceHeader.iPicOrderCntLsb);

EXPECT_EQ (ERR_NONE, MemFreeNalList (&pAu, &cMemoryAlign));
EXPECT_TRUE (pAu == NULL);
}