/* * free memory dynamically allocated during decoder */ void WelsFreeDynamicMemory (PWelsDecoderContext pCtx) { int32_t iListIdx = 0; CMemoryAlign* pMa = pCtx->pMemAlign; //free dq layer memory UninitialDqLayersContext (pCtx); //free FMO memory ResetFmoList (pCtx); //free ref-pic list & picture memory WelsResetRefPic (pCtx); for (iListIdx = LIST_0; iListIdx < LIST_A; ++ iListIdx) { PPicBuff* pPicBuff = &pCtx->pPicBuff[iListIdx]; if (NULL != pPicBuff && NULL != *pPicBuff) { DestroyPicBuff (pPicBuff, pMa); } } // added for safe memory pCtx->iImgWidthInPixel = 0; pCtx->iImgHeightInPixel = 0; pCtx->iLastImgWidthInPixel = 0; pCtx->iLastImgHeightInPixel = 0; pCtx->bFreezeOutput = true; pCtx->bHaveGotMemory = false; //free CABAC memory pMa->WelsFree (pCtx->pCabacDecEngine, "pCtx->pCabacDecEngine"); }
//Tests of WelsGetCacheLineSize Begin TEST (MemoryAlignTest, GetCacheLineSize_LoopWithin16K) { const unsigned int kuiTestBoundary16K = 16 * 1024; unsigned int uiTargetAlign = 1; while (uiTargetAlign < kuiTestBoundary16K) { CMemoryAlign cTestMa (uiTargetAlign); ASSERT_EQ ((uiTargetAlign & 0x0F) ? 16 : uiTargetAlign, cTestMa.WelsGetCacheLineSize()); ++ uiTargetAlign; } }
//Tests of WelsGetCacheLineSize End //Tests of WelsMallocAndFree Begin TEST (MemoryAlignTest, WelsMallocAndFreeOnceFunctionVerify) { const uint32_t kuiTargetAlignSize[4] = {32, 16, 64, 8}; const uint32_t kuiZero = 0; for (int i = 0; i < 4; i++) { const uint32_t kuiTestAlignSize = kuiTargetAlignSize[i]; const uint32_t kuiTestDataSize = abs (rand()); CMemoryAlign cTestMa (kuiTestAlignSize); const uint32_t uiSize = kuiTestDataSize; const char strUnitTestTag[100] = "pUnitTestData"; const uint32_t kuiUsedCacheLineSize = ((kuiTestAlignSize == 0) || (kuiTestAlignSize & 0x0F)) ? (16) : (kuiTestAlignSize); const uint32_t kuiExtraAlignSize = kuiUsedCacheLineSize - 1; const uint32_t kuiExpectedSize = sizeof (void**) + sizeof (int32_t) + kuiExtraAlignSize + uiSize; uint8_t* pUnitTestData = static_cast<uint8_t*> (cTestMa.WelsMalloc (uiSize, strUnitTestTag)); if (pUnitTestData != NULL) { ASSERT_TRUE ((((uintptr_t) (pUnitTestData)) & kuiExtraAlignSize) == 0); EXPECT_EQ (kuiExpectedSize, cTestMa.WelsGetMemoryUsage()); cTestMa.WelsFree (pUnitTestData, strUnitTestTag); EXPECT_EQ (kuiZero, cTestMa.WelsGetMemoryUsage()); } else { EXPECT_EQ (NULL, pUnitTestData); EXPECT_EQ (kuiZero, cTestMa.WelsGetMemoryUsage()); cTestMa.WelsFree (pUnitTestData, strUnitTestTag); EXPECT_EQ (kuiZero, cTestMa.WelsGetMemoryUsage()); } } }
int32_t RequestMtResource (sWelsEncCtx** ppCtx, SWelsSvcCodingParam* pCodingParam, const int32_t iCountBsLen, const int32_t iTargetSpatialBsSize) { CMemoryAlign* pMa = NULL; SWelsSvcCodingParam* pPara = NULL; SSliceThreading* pSmt = NULL; SWelsSliceBs* pSliceB = NULL; uint8_t* pBsBase = NULL; int32_t iNumSpatialLayers = 0; int32_t iThreadNum = 0; int32_t iIdx = 0; int32_t iSliceBsBufferSize = 0; int16_t iMaxSliceNum = 1; int32_t iReturn = ENC_RETURN_SUCCESS; if (NULL == ppCtx || NULL == pCodingParam || NULL == *ppCtx || iCountBsLen <= 0) return 1; pMa = (*ppCtx)->pMemAlign; pPara = pCodingParam; iNumSpatialLayers = pPara->iSpatialLayerNum; iThreadNum = pPara->iCountThreadsNum; iMaxSliceNum = (*ppCtx)->iMaxSliceCount; pSmt = (SSliceThreading*)pMa->WelsMalloc (sizeof (SSliceThreading), "SSliceThreading"); WELS_VERIFY_RETURN_PROC_IF (1, (NULL == pSmt), FreeMemorySvc (ppCtx)) (*ppCtx)->pSliceThreading = pSmt; pSmt->pThreadPEncCtx = (SSliceThreadPrivateData*)pMa->WelsMalloc (sizeof (SSliceThreadPrivateData) * iThreadNum, "pThreadPEncCtx"); WELS_VERIFY_RETURN_PROC_IF (1, (NULL == pSmt->pThreadPEncCtx), FreeMemorySvc (ppCtx)) #ifdef _WIN32 // Dummy event namespace, the windows events don't actually use this WelsSnprintf (pSmt->eventNamespace, sizeof (pSmt->eventNamespace), "%p", (void*) *ppCtx); #else WelsSnprintf (pSmt->eventNamespace, sizeof (pSmt->eventNamespace), "%p%x", (void*) *ppCtx, getpid()); #endif//!_WIN32 iIdx = 0; while (iIdx < iNumSpatialLayers) { SSliceConfig* pMso = &pPara->sSpatialLayers[iIdx].sSliceCfg; const int32_t kiSliceNum = pMso->sSliceArgument.uiSliceNum; if (((pMso->uiSliceMode == SM_FIXEDSLCNUM_SLICE) || (pMso->uiSliceMode == SM_AUTO_SLICE)) && pPara->iMultipleThreadIdc > 1 && pPara->iMultipleThreadIdc >= kiSliceNum) { pSmt->pSliceConsumeTime[iIdx] = (uint32_t*)pMa->WelsMallocz (kiSliceNum * sizeof (uint32_t), "pSliceConsumeTime[]"); WELS_VERIFY_RETURN_PROC_IF (1, (NULL == pSmt->pSliceConsumeTime[iIdx]), FreeMemorySvc (ppCtx)) pSmt->pSliceComplexRatio[iIdx] = (int32_t*)pMa->WelsMalloc (kiSliceNum * sizeof (int32_t), "pSliceComplexRatio[]"); WELS_VERIFY_RETURN_PROC_IF (1, (NULL == pSmt->pSliceComplexRatio[iIdx]), FreeMemorySvc (ppCtx)) } else {
static int32_t CreatePicBuff (PWelsDecoderContext pCtx, PPicBuff* ppPicBuf, const int32_t kiSize, const int32_t kiPicWidth, const int32_t kiPicHeight) { PPicBuff pPicBuf = NULL; int32_t iPicIdx = 0; if (kiSize <= 0 || kiPicWidth <= 0 || kiPicHeight <= 0) { return 1; } CMemoryAlign* pMa = pCtx->pMemAlign; pPicBuf = (PPicBuff)pMa->WelsMallocz (sizeof (SPicBuff), "PPicBuff"); if (NULL == pPicBuf) { return 1; } pPicBuf->ppPic = (PPicture*)pMa->WelsMallocz (kiSize * sizeof (PPicture), "PPicture*"); if (NULL == pPicBuf->ppPic) { pPicBuf->iCapacity = 0; DestroyPicBuff (&pPicBuf, pMa); return 1; } for (iPicIdx = 0; iPicIdx < kiSize; ++ iPicIdx) { PPicture pPic = AllocPicture (pCtx, kiPicWidth, kiPicHeight); if (NULL == pPic) { // init capacity first for free memory pPicBuf->iCapacity = iPicIdx; DestroyPicBuff (&pPicBuf, pMa); return 1; } pPicBuf->ppPic[iPicIdx] = pPic; } // initialize context in queue pPicBuf->iCapacity = kiSize; pPicBuf->iCurrentIdx = 0; * ppPicBuf = pPicBuf; return 0; }
TEST (MemoryAlignTest, GetCacheLineSize_MaxUINT) { CMemoryAlign cTestMa (0xFFFFFFFF); const uint32_t kuiSixteen = 16; ASSERT_EQ (kuiSixteen, cTestMa.WelsGetCacheLineSize()); }
void ReleaseMtResource (sWelsEncCtx** ppCtx) { SWelsSliceBs* pSliceB = NULL; SSliceThreading* pSmt = NULL; CMemoryAlign* pMa = NULL; int32_t iIdx = 0; int32_t iThreadNum = 0; int16_t uiSliceNum = 0; if (NULL == ppCtx || NULL == *ppCtx) return; pMa = (*ppCtx)->pMemAlign; uiSliceNum = (*ppCtx)->iMaxSliceCount; iThreadNum = (*ppCtx)->pSvcParam->iCountThreadsNum; pSmt = (*ppCtx)->pSliceThreading; if (NULL == pSmt) return; char ename[SEM_NAME_MAX] = {0}; while (iIdx < iThreadNum) { // length of semaphore name should be system constrained at least on mac 10.7 WelsSnprintf (ename, SEM_NAME_MAX, "ee%d%s", iIdx, pSmt->eventNamespace); WelsEventClose (&pSmt->pExitEncodeEvent[iIdx], ename); WelsSnprintf (ename, SEM_NAME_MAX, "tm%d%s", iIdx, pSmt->eventNamespace); WelsEventClose (&pSmt->pThreadMasterEvent[iIdx], ename); WelsSnprintf (ename, SEM_NAME_MAX, "sc%d%s", iIdx, pSmt->eventNamespace); WelsEventClose (&pSmt->pSliceCodedEvent[iIdx], ename); WelsSnprintf (ename, SEM_NAME_MAX, "rc%d%s", iIdx, pSmt->eventNamespace); WelsEventClose (&pSmt->pReadySliceCodingEvent[iIdx], ename); WelsSnprintf (ename, SEM_NAME_MAX, "ud%d%s", iIdx, pSmt->eventNamespace); WelsEventClose (&pSmt->pUpdateMbListEvent[iIdx], ename); WelsSnprintf (ename, SEM_NAME_MAX, "fu%d%s", iIdx, pSmt->eventNamespace); WelsEventClose (&pSmt->pFinUpdateMbListEvent[iIdx], ename); ++ iIdx; } WelsSnprintf (ename, SEM_NAME_MAX, "scm%s", pSmt->eventNamespace); WelsEventClose (&pSmt->pSliceCodedMasterEvent, ename); WelsMutexDestroy (&pSmt->mutexSliceNumUpdate); WelsMutexDestroy (&pSmt->mutexThreadBsBufferUsage); WelsMutexDestroy (& ((*ppCtx)->mutexEncoderError)); if (pSmt->pThreadPEncCtx != NULL) { pMa->WelsFree (pSmt->pThreadPEncCtx, "pThreadPEncCtx"); pSmt->pThreadPEncCtx = NULL; } for (int i = 0; i < MAX_THREADS_NUM; i++) { if (pSmt->pThreadBsBuffer[i]) { pMa->WelsFree (pSmt->pThreadBsBuffer[i], "pSmt->pThreadBsBuffer"); pSmt->pThreadBsBuffer[i] = NULL; } } memset (&pSmt->bThreadBsBufferUsage, 0, MAX_THREADS_NUM * sizeof (bool)); pSliceB = (*ppCtx)->pSliceBs; iIdx = 0; while (pSliceB != NULL && iIdx < uiSliceNum) { pSliceB->pBsBuffer = NULL; pSliceB->uiSize = 0; pSliceB->uiBsPos = 0; ++ iIdx; ++ pSliceB; } if ((*ppCtx)->pSliceBs != NULL) { pMa->WelsFree ((*ppCtx)->pSliceBs, "pSliceBs"); (*ppCtx)->pSliceBs = NULL; } if ((*ppCtx)->pTaskManage != NULL) { delete (*ppCtx)->pTaskManage; (*ppCtx)->pTaskManage = NULL; } #ifdef MT_DEBUG // file handle for debug if (pSmt->pFSliceDiff) { fclose (pSmt->pFSliceDiff); pSmt->pFSliceDiff = NULL; } #endif//MT_DEBUG pMa->WelsFree ((*ppCtx)->pSliceThreading, "SSliceThreading"); (*ppCtx)->pSliceThreading = NULL; }
int32_t RequestMtResource (sWelsEncCtx** ppCtx, SWelsSvcCodingParam* pCodingParam, const int32_t iCountBsLen, const int32_t iMaxSliceBufferSize, bool bDynamicSlice) { CMemoryAlign* pMa = NULL; SWelsSvcCodingParam* pPara = NULL; SSliceThreading* pSmt = NULL; int32_t iNumSpatialLayers = 0; int32_t iThreadNum = 0; int32_t iIdx = 0; int16_t iMaxSliceNum = 1; int32_t iReturn = ENC_RETURN_SUCCESS; bool bWillUseTaskManage = false; if (NULL == ppCtx || NULL == pCodingParam || NULL == *ppCtx || iCountBsLen <= 0) return 1; pMa = (*ppCtx)->pMemAlign; pPara = pCodingParam; iNumSpatialLayers = pPara->iSpatialLayerNum; iThreadNum = pPara->iCountThreadsNum; iMaxSliceNum = (*ppCtx)->iMaxSliceCount; pSmt = (SSliceThreading*)pMa->WelsMalloc (sizeof (SSliceThreading), "SSliceThreading"); WELS_VERIFY_RETURN_PROC_IF (1, (NULL == pSmt), FreeMemorySvc (ppCtx)) (*ppCtx)->pSliceThreading = pSmt; pSmt->pThreadPEncCtx = (SSliceThreadPrivateData*)pMa->WelsMalloc (sizeof (SSliceThreadPrivateData) * iThreadNum, "pThreadPEncCtx"); WELS_VERIFY_RETURN_PROC_IF (1, (NULL == pSmt->pThreadPEncCtx), FreeMemorySvc (ppCtx)) #ifdef _WIN32 // Dummy event namespace, the windows events don't actually use this WelsSnprintf (pSmt->eventNamespace, sizeof (pSmt->eventNamespace), "%p", (void*) *ppCtx); #else WelsSnprintf (pSmt->eventNamespace, sizeof (pSmt->eventNamespace), "%p%x", (void*) *ppCtx, getpid()); #endif//!_WIN32 iIdx = 0; while (iIdx < iNumSpatialLayers) { SSliceArgument* pSliceArgument = &pPara->sSpatialLayers[iIdx].sSliceArgument; if (pSliceArgument->uiSliceMode == SM_FIXEDSLCNUM_SLICE || pSliceArgument->uiSliceMode == SM_RASTER_SLICE) { bWillUseTaskManage = true; } ++ iIdx; } #ifdef MT_DEBUG // file handle for MT debug pSmt->pFSliceDiff = NULL; if (pSmt->pFSliceDiff) { fclose (pSmt->pFSliceDiff); pSmt->pFSliceDiff = NULL; } pSmt->pFSliceDiff = fopen ("slice_time.txt", "wt+"); #endif//MT_DEBUG MT_TRACE_LOG (*ppCtx, WELS_LOG_INFO, "encpEncCtx= 0x%p", (void*) *ppCtx); char name[SEM_NAME_MAX] = {0}; WELS_GCC_UNUSED WELS_THREAD_ERROR_CODE err = 0; iIdx = 0; while (iIdx < iThreadNum) { pSmt->pThreadPEncCtx[iIdx].pWelsPEncCtx = (void*) *ppCtx; pSmt->pThreadPEncCtx[iIdx].iSliceIndex = iIdx; pSmt->pThreadPEncCtx[iIdx].iThreadIndex = iIdx; pSmt->pThreadHandles[iIdx] = 0; WelsSnprintf (name, SEM_NAME_MAX, "ee%d%s", iIdx, pSmt->eventNamespace); err = WelsEventOpen (&pSmt->pExitEncodeEvent[iIdx], name); MT_TRACE_LOG (*ppCtx, WELS_LOG_INFO, "[MT] Open pExitEncodeEvent%d named(%s) ret%d err%d", iIdx, name, err, errno); WelsSnprintf (name, SEM_NAME_MAX, "tm%d%s", iIdx, pSmt->eventNamespace); err = WelsEventOpen (&pSmt->pThreadMasterEvent[iIdx], name); MT_TRACE_LOG (*ppCtx, WELS_LOG_INFO, "[MT] Open pThreadMasterEvent%d named(%s) ret%d err%d", iIdx, name, err, errno); // length of semaphore name should be system constrained at least on mac 10.7 WelsSnprintf (name, SEM_NAME_MAX, "ud%d%s", iIdx, pSmt->eventNamespace); err = WelsEventOpen (&pSmt->pUpdateMbListEvent[iIdx], name); MT_TRACE_LOG (*ppCtx, WELS_LOG_INFO, "[MT] Open pUpdateMbListEvent%d named(%s) ret%d err%d", iIdx, name, err, errno); WelsSnprintf (name, SEM_NAME_MAX, "fu%d%s", iIdx, pSmt->eventNamespace); err = WelsEventOpen (&pSmt->pFinUpdateMbListEvent[iIdx], name); MT_TRACE_LOG (*ppCtx, WELS_LOG_INFO, "[MT] Open pFinUpdateMbListEvent%d named(%s) ret%d err%d", iIdx, name, err, errno); WelsSnprintf (name, SEM_NAME_MAX, "sc%d%s", iIdx, pSmt->eventNamespace); err = WelsEventOpen (&pSmt->pSliceCodedEvent[iIdx], name); MT_TRACE_LOG (*ppCtx, WELS_LOG_INFO, "[MT] Open pSliceCodedEvent%d named(%s) ret%d err%d", iIdx, name, err, errno); WelsSnprintf (name, SEM_NAME_MAX, "rc%d%s", iIdx, pSmt->eventNamespace); err = WelsEventOpen (&pSmt->pReadySliceCodingEvent[iIdx], name); MT_TRACE_LOG (*ppCtx, WELS_LOG_INFO, "[MT] Open pReadySliceCodingEvent%d = 0x%p named(%s) ret%d err%d", iIdx, (void*)pSmt->pReadySliceCodingEvent[iIdx], name, err, errno); pSmt->pThreadBsBuffer[iIdx] = (uint8_t*)pMa->WelsMalloc (iCountBsLen, "pSmt->pThreadBsBuffer"); WELS_VERIFY_RETURN_PROC_IF (1, (NULL == pSmt->pThreadBsBuffer[iIdx]), FreeMemorySvc (ppCtx)) ++ iIdx; } for (; iIdx < MAX_THREADS_NUM; iIdx++) { pSmt->pThreadBsBuffer[iIdx] = NULL; } //previous conflict WelsSnprintf (name, SEM_NAME_MAX, "scm%s", pSmt->eventNamespace); err = WelsEventOpen (&pSmt->pSliceCodedMasterEvent, name); MT_TRACE_LOG (*ppCtx, WELS_LOG_INFO, "[MT] Open pSliceCodedMasterEvent named(%s) ret%d err%d", name, err, errno); //previous conflict ends iReturn = SetMultiSliceBuffer (ppCtx, pMa, pSmt, iMaxSliceNum, iMaxSliceBufferSize, iCountBsLen, bDynamicSlice); WELS_VERIFY_RETURN_PROC_IF (iReturn, (ENC_RETURN_SUCCESS != iReturn), FreeMemorySvc (ppCtx)) iReturn = WelsMutexInit (&pSmt->mutexSliceNumUpdate); WELS_VERIFY_RETURN_PROC_IF (1, (WELS_THREAD_ERROR_OK != iReturn), FreeMemorySvc (ppCtx)) if (bWillUseTaskManage) { (*ppCtx)->pTaskManage = IWelsTaskManage::CreateTaskManage (*ppCtx, iNumSpatialLayers, bDynamicSlice); WELS_VERIFY_RETURN_PROC_IF (iReturn, (NULL == (*ppCtx)->pTaskManage), FreeMemorySvc (ppCtx)) } memset (&pSmt->bThreadBsBufferUsage, 0, MAX_THREADS_NUM * sizeof (bool)); iReturn = WelsMutexInit (&pSmt->mutexThreadBsBufferUsage); WELS_VERIFY_RETURN_PROC_IF (1, (WELS_THREAD_ERROR_OK != iReturn), FreeMemorySvc (ppCtx)) iReturn = WelsMutexInit (& (*ppCtx)->mutexEncoderError); WELS_VERIFY_RETURN_PROC_IF (1, (WELS_THREAD_ERROR_OK != iReturn), FreeMemorySvc (ppCtx)) MT_TRACE_LOG (*ppCtx, WELS_LOG_INFO, "RequestMtResource(), iThreadNum=%d, iCountSliceNum= %d", pPara->iCountThreadsNum, iMaxSliceNum); return 0; }
/* * request memory blocks for decoder avc part */ int32_t WelsRequestMem (PWelsDecoderContext pCtx, const int32_t kiMbWidth, const int32_t kiMbHeight, bool& bReallocFlag) { const int32_t kiPicWidth = kiMbWidth << 4; const int32_t kiPicHeight = kiMbHeight << 4; int32_t iErr = ERR_NONE; int32_t iListIdx = 0; //, mb_blocks = 0; int32_t iPicQueueSize = 0; // adaptive size of picture queue, = (pSps->iNumRefFrames x 2) bReallocFlag = false; bool bNeedChangePicQueue = true; CMemoryAlign* pMa = pCtx->pMemAlign; WELS_VERIFY_RETURN_IF (ERR_INFO_INVALID_PARAM, (NULL == pCtx || kiPicWidth <= 0 || kiPicHeight <= 0)) // Fixed the issue about different gop size over last, 5/17/2010 // get picture queue size currently iPicQueueSize = GetTargetRefListSize (pCtx); // adaptive size of picture queue, = (pSps->iNumRefFrames x 2) pCtx->iPicQueueNumber = iPicQueueSize; if (pCtx->pPicBuff[LIST_0] != NULL && pCtx->pPicBuff[LIST_0]->iCapacity == iPicQueueSize) // comparing current picture queue size requested and previous allocation picture queue bNeedChangePicQueue = false; // HD based pic buffer need consider memory size consumed when switch from 720p to other lower size WELS_VERIFY_RETURN_IF (ERR_NONE, pCtx->bHaveGotMemory && (kiPicWidth == pCtx->iImgWidthInPixel && kiPicHeight == pCtx->iImgHeightInPixel) && (!bNeedChangePicQueue)) // have same scaled buffer // sync update pRefList WelsResetRefPic (pCtx); // added to sync update ref list due to pictures are free if (pCtx->bHaveGotMemory && (kiPicWidth == pCtx->iImgWidthInPixel && kiPicHeight == pCtx->iImgHeightInPixel) && pCtx->pPicBuff[LIST_0] != NULL && pCtx->pPicBuff[LIST_0]->iCapacity != iPicQueueSize) { // currently only active for LIST_0 due to have no B frames WelsLog (& (pCtx->sLogCtx), WELS_LOG_INFO, "WelsRequestMem(): memory re-alloc for no resolution change (size = %d * %d), ref list size change from %d to %d", kiPicWidth, kiPicHeight, pCtx->pPicBuff[LIST_0]->iCapacity, iPicQueueSize); if (pCtx->pPicBuff[LIST_0]->iCapacity < iPicQueueSize) { iErr = IncreasePicBuff (pCtx, &pCtx->pPicBuff[LIST_0], pCtx->pPicBuff[LIST_0]->iCapacity, kiPicWidth, kiPicHeight, iPicQueueSize); } else { iErr = DecreasePicBuff (pCtx, &pCtx->pPicBuff[LIST_0], pCtx->pPicBuff[LIST_0]->iCapacity, kiPicWidth, kiPicHeight, iPicQueueSize); } } else { if (pCtx->bHaveGotMemory) WelsLog (& (pCtx->sLogCtx), WELS_LOG_INFO, "WelsRequestMem(): memory re-alloc for resolution change, size change from %d * %d to %d * %d, ref list size change from %d to %d", pCtx->iImgWidthInPixel, pCtx->iImgHeightInPixel, kiPicWidth, kiPicHeight, pCtx->pPicBuff[LIST_0]->iCapacity, iPicQueueSize); else WelsLog (& (pCtx->sLogCtx), WELS_LOG_INFO, "WelsRequestMem(): memory alloc size = %d * %d, ref list size = %d", kiPicWidth, kiPicHeight, iPicQueueSize); // for Recycled_Pic_Queue for (iListIdx = LIST_0; iListIdx < LIST_A; ++ iListIdx) { PPicBuff* ppPic = &pCtx->pPicBuff[iListIdx]; if (NULL != ppPic && NULL != *ppPic) { DestroyPicBuff (ppPic, pMa); } } pCtx->pPreviousDecodedPictureInDpb = NULL; // currently only active for LIST_0 due to have no B frames iErr = CreatePicBuff (pCtx, &pCtx->pPicBuff[LIST_0], iPicQueueSize, kiPicWidth, kiPicHeight); } if (iErr != ERR_NONE) return iErr; pCtx->iImgWidthInPixel = kiPicWidth; // target width of image to be reconstruted while decoding pCtx->iImgHeightInPixel = kiPicHeight; // target height of image to be reconstruted while decoding pCtx->bHaveGotMemory = true; // global memory for decoder context related is requested pCtx->pDec = NULL; // need prefetch a new pic due to spatial size changed if (pCtx->pCabacDecEngine == NULL) pCtx->pCabacDecEngine = (SWelsCabacDecEngine*) pMa->WelsMallocz (sizeof (SWelsCabacDecEngine), "pCtx->pCabacDecEngine"); WELS_VERIFY_RETURN_IF (ERR_INFO_OUT_OF_MEMORY, (NULL == pCtx->pCabacDecEngine)) bReallocFlag = true; // memory re-allocation successfully finished return ERR_NONE; }
static int32_t DecreasePicBuff (PWelsDecoderContext pCtx, PPicBuff* ppPicBuf, const int32_t kiOldSize, const int32_t kiPicWidth, const int32_t kiPicHeight, const int32_t kiNewSize) { PPicBuff pPicOldBuf = *ppPicBuf; PPicBuff pPicNewBuf = NULL; int32_t iPicIdx = 0; if (kiOldSize <= 0 || kiNewSize <= 0 || kiPicWidth <= 0 || kiPicHeight <= 0) { return 1; } CMemoryAlign* pMa = pCtx->pMemAlign; pPicNewBuf = (PPicBuff)pMa->WelsMallocz (sizeof (SPicBuff), "PPicBuff"); if (NULL == pPicNewBuf) { return 1; } pPicNewBuf->ppPic = (PPicture*)pMa->WelsMallocz (kiNewSize * sizeof (PPicture), "PPicture*"); if (NULL == pPicNewBuf->ppPic) { pPicNewBuf->iCapacity = 0; DestroyPicBuff (&pPicNewBuf, pMa); return 1; } int32_t iPrevPicIdx = -1; for (iPrevPicIdx = 0; iPrevPicIdx < kiOldSize; ++iPrevPicIdx) { if (pCtx->pPreviousDecodedPictureInDpb == pPicOldBuf->ppPic[iPrevPicIdx]) { break; } } int32_t iDelIdx; if (iPrevPicIdx < kiOldSize && iPrevPicIdx >= kiNewSize) { // found pPreviousDecodedPictureInDpb, pPicNewBuf->ppPic[0] = pPicOldBuf->ppPic[iPrevPicIdx]; pPicNewBuf->iCurrentIdx = 0; memcpy (pPicNewBuf->ppPic + 1, pPicOldBuf->ppPic, (kiNewSize - 1) * sizeof (PPicture)); iDelIdx = kiNewSize - 1; } else { memcpy (pPicNewBuf->ppPic, pPicOldBuf->ppPic, kiNewSize * sizeof (PPicture)); pPicNewBuf->iCurrentIdx = iPrevPicIdx < kiNewSize ? iPrevPicIdx : 0; iDelIdx = kiNewSize; } for (iPicIdx = iDelIdx; iPicIdx < kiOldSize; iPicIdx++) { if (iPrevPicIdx != iPicIdx) { if (pPicOldBuf->ppPic[iPicIdx] != NULL) { FreePicture (pPicOldBuf->ppPic[iPicIdx], pMa); pPicOldBuf->ppPic[iPicIdx] = NULL; } } } // initialize context in queue pPicNewBuf->iCapacity = kiNewSize; *ppPicBuf = pPicNewBuf; for (int32_t i = 0; i < pPicNewBuf->iCapacity; i++) { pPicNewBuf->ppPic[i]->bUsedAsRef = false; pPicNewBuf->ppPic[i]->bIsLongRef = false; pPicNewBuf->ppPic[i]->uiRefCount = 0; pPicNewBuf->ppPic[i]->bAvailableFlag = true; pPicNewBuf->ppPic[i]->bIsComplete = false; } // remove old PicBuf if (pPicOldBuf->ppPic != NULL) { pMa->WelsFree (pPicOldBuf->ppPic, "pPicOldBuf->queue"); pPicOldBuf->ppPic = NULL; } pPicOldBuf->iCapacity = 0; pPicOldBuf->iCurrentIdx = 0; pMa->WelsFree (pPicOldBuf, "pPicOldBuf"); pPicOldBuf = NULL; return 0; }
static int32_t IncreasePicBuff (PWelsDecoderContext pCtx, PPicBuff* ppPicBuf, const int32_t kiOldSize, const int32_t kiPicWidth, const int32_t kiPicHeight, const int32_t kiNewSize) { PPicBuff pPicOldBuf = *ppPicBuf; PPicBuff pPicNewBuf = NULL; int32_t iPicIdx = 0; if (kiOldSize <= 0 || kiNewSize <= 0 || kiPicWidth <= 0 || kiPicHeight <= 0) { return 1; } CMemoryAlign* pMa = pCtx->pMemAlign; pPicNewBuf = (PPicBuff)pMa->WelsMallocz (sizeof (SPicBuff), "PPicBuff"); if (NULL == pPicNewBuf) { return 1; } pPicNewBuf->ppPic = (PPicture*)pMa->WelsMallocz (kiNewSize * sizeof (PPicture), "PPicture*"); if (NULL == pPicNewBuf->ppPic) { pPicNewBuf->iCapacity = 0; DestroyPicBuff (&pPicNewBuf, pMa); return 1; } // increase new PicBuf for (iPicIdx = kiOldSize; iPicIdx < kiNewSize; ++ iPicIdx) { PPicture pPic = AllocPicture (pCtx, kiPicWidth, kiPicHeight); if (NULL == pPic) { // Set maximum capacity as the new malloc memory at the tail pPicNewBuf->iCapacity = iPicIdx; DestroyPicBuff (&pPicNewBuf, pMa); return 1; } pPicNewBuf->ppPic[iPicIdx] = pPic; } // copy old PicBuf to new PicBuf memcpy (pPicNewBuf->ppPic, pPicOldBuf->ppPic, kiOldSize * sizeof (PPicture)); // initialize context in queue pPicNewBuf->iCapacity = kiNewSize; pPicNewBuf->iCurrentIdx = pPicOldBuf->iCurrentIdx; * ppPicBuf = pPicNewBuf; for (int32_t i = 0; i < pPicNewBuf->iCapacity; i++) { pPicNewBuf->ppPic[i]->bUsedAsRef = false; pPicNewBuf->ppPic[i]->bIsLongRef = false; pPicNewBuf->ppPic[i]->uiRefCount = 0; pPicNewBuf->ppPic[i]->bAvailableFlag = true; pPicNewBuf->ppPic[i]->bIsComplete = false; } // remove old PicBuf if (pPicOldBuf->ppPic != NULL) { pMa->WelsFree (pPicOldBuf->ppPic, "pPicOldBuf->queue"); pPicOldBuf->ppPic = NULL; } pPicOldBuf->iCapacity = 0; pPicOldBuf->iCurrentIdx = 0; pMa->WelsFree (pPicOldBuf, "pPicOldBuf"); pPicOldBuf = NULL; return 0; }