ref: 6cf240237ba4a214ca5649cdfcbf7cf960127a89
parent: 8b1206001c27db9828a659f07963154d74cbc41c
parent: b76a79c7264966eccd3bb37d41c3e229eb47ad49
author: ruil2 <[email protected]>
date: Tue Feb 16 05:47:02 EST 2016
Merge pull request #2361 from sijchen/fix_free00 [Encoder] multiple protection if memory allocation failed
--- a/codec/common/src/memory_align.cpp
+++ b/codec/common/src/memory_align.cpp
@@ -70,6 +70,9 @@
const uint32_t kiPayloadSize = kuiSize;
uint8_t* pBuf = (uint8_t*) malloc (kiActualRequestedSize);
+ if (NULL == pBuf)
+ return NULL;
+
#ifdef MEMORY_CHECK
if (fpMemChkPoint == NULL) {
fpMemChkPoint = fopen ("./enc_mem_check_point.txt", "at+");
@@ -87,10 +90,6 @@
}
#endif
uint8_t* pAlignedBuffer;
-
- if (NULL == pBuf)
- return NULL;
-
pAlignedBuffer = pBuf + kiAlignedBytes + kiSizeOfVoidPointer + kiSizeOfInt;
pAlignedBuffer -= ((uintptr_t) pAlignedBuffer & kiAlignedBytes);
* ((void**) (pAlignedBuffer - kiSizeOfVoidPointer)) = pBuf;
--- a/codec/encoder/core/src/encoder_ext.cpp
+++ b/codec/encoder/core/src/encoder_ext.cpp
@@ -878,7 +878,7 @@
iOverallMbNum += iMbSize[i];
}
- (*ppCtx)->ppMbListD = static_cast<SMB**> ((*ppCtx)->pMemAlign->WelsMalloc (iNumDlayer * sizeof (SMB*), "ppMbListD"));
+ (*ppCtx)->ppMbListD = static_cast<SMB**> ((*ppCtx)->pMemAlign->WelsMallocz (iNumDlayer * sizeof (SMB*), "ppMbListD"));
(*ppCtx)->ppMbListD[0] = NULL;
WELS_VERIFY_RETURN_PROC_IF (1, (*ppCtx)->ppMbListD == NULL, FreeMemorySvc (ppCtx));
(*ppCtx)->ppMbListD[0] = static_cast<SMB*> ((*ppCtx)->pMemAlign->WelsMallocz (iOverallMbNum * sizeof (SMB),
@@ -896,23 +896,23 @@
}
int32_t AllocMbCacheAligned (SMbCache* pMbCache, CMemoryAlign* pMa) {
- pMbCache->pCoeffLevel = (int16_t*)pMa->WelsMalloc (MB_COEFF_LIST_SIZE * sizeof (int16_t), "pMbCache->pCoeffLevel");
+ pMbCache->pCoeffLevel = (int16_t*)pMa->WelsMallocz (MB_COEFF_LIST_SIZE * sizeof (int16_t), "pMbCache->pCoeffLevel");
WELS_VERIFY_RETURN_IF (1, (NULL == pMbCache->pCoeffLevel));
- pMbCache->pMemPredMb = (uint8_t*)pMa->WelsMalloc (2 * 256 * sizeof (uint8_t), "pMbCache->pMemPredMb");
+ pMbCache->pMemPredMb = (uint8_t*)pMa->WelsMallocz (2 * 256 * sizeof (uint8_t), "pMbCache->pMemPredMb");
WELS_VERIFY_RETURN_IF (1, (NULL == pMbCache->pMemPredMb));
- pMbCache->pSkipMb = (uint8_t*)pMa->WelsMalloc (384 * sizeof (uint8_t), "pMbCache->pSkipMb");
+ pMbCache->pSkipMb = (uint8_t*)pMa->WelsMallocz (384 * sizeof (uint8_t), "pMbCache->pSkipMb");
WELS_VERIFY_RETURN_IF (1, (NULL == pMbCache->pSkipMb));
- pMbCache->pMemPredBlk4 = (uint8_t*)pMa->WelsMalloc (2 * 16 * sizeof (uint8_t), "pMbCache->pMemPredBlk4");
+ pMbCache->pMemPredBlk4 = (uint8_t*)pMa->WelsMallocz (2 * 16 * sizeof (uint8_t), "pMbCache->pMemPredBlk4");
WELS_VERIFY_RETURN_IF (1, (NULL == pMbCache->pMemPredBlk4));
- pMbCache->pBufferInterPredMe = (uint8_t*)pMa->WelsMalloc (4 * 640 * sizeof (uint8_t), "pMbCache->pBufferInterPredMe");
+ pMbCache->pBufferInterPredMe = (uint8_t*)pMa->WelsMallocz (4 * 640 * sizeof (uint8_t), "pMbCache->pBufferInterPredMe");
WELS_VERIFY_RETURN_IF (1, (NULL == pMbCache->pBufferInterPredMe));
- pMbCache->pPrevIntra4x4PredModeFlag = (bool*)pMa->WelsMalloc (16 * sizeof (bool),
+ pMbCache->pPrevIntra4x4PredModeFlag = (bool*)pMa->WelsMallocz (16 * sizeof (bool),
"pMbCache->pPrevIntra4x4PredModeFlag");
WELS_VERIFY_RETURN_IF (1, (NULL == pMbCache->pPrevIntra4x4PredModeFlag));
- pMbCache->pRemIntra4x4PredModeFlag = (int8_t*)pMa->WelsMalloc (16 * sizeof (int8_t),
+ pMbCache->pRemIntra4x4PredModeFlag = (int8_t*)pMa->WelsMallocz (16 * sizeof (int8_t),
"pMbCache->pRemIntra4x4PredModeFlag");
WELS_VERIFY_RETURN_IF (1, (NULL == pMbCache->pRemIntra4x4PredModeFlag));
- pMbCache->pDct = (SDCTCoeff*)pMa->WelsMalloc (sizeof (SDCTCoeff), "pMbCache->pDct");
+ pMbCache->pDct = (SDCTCoeff*)pMa->WelsMallocz (sizeof (SDCTCoeff), "pMbCache->pDct");
WELS_VERIFY_RETURN_IF (1, (NULL == pMbCache->pDct));
return 0;
}
@@ -1294,10 +1294,10 @@
// for dynamically malloc for parameter sets memory instead of maximal items for standard to reduce size, 3/18/2010
const int32_t kiNeededSpsNum = (*ppCtx)->GetNeededSpsNum();
const int32_t kiNeededSubsetSpsNum = (*ppCtx)->GetNeededSubsetSpsNum();
- (*ppCtx)->pSpsArray = (SWelsSPS*)pMa->WelsMalloc (kiNeededSpsNum * sizeof (SWelsSPS), "pSpsArray");
+ (*ppCtx)->pSpsArray = (SWelsSPS*)pMa->WelsMallocz (kiNeededSpsNum * sizeof (SWelsSPS), "pSpsArray");
WELS_VERIFY_RETURN_PROC_IF (1, (NULL == (*ppCtx)->pSpsArray), FreeMemorySvc (ppCtx))
if (kiNeededSubsetSpsNum > 0) {
- (*ppCtx)->pSubsetArray = (SSubsetSps*)pMa->WelsMalloc (kiNeededSubsetSpsNum * sizeof (SSubsetSps), "pSubsetArray");
+ (*ppCtx)->pSubsetArray = (SSubsetSps*)pMa->WelsMallocz (kiNeededSubsetSpsNum * sizeof (SSubsetSps), "pSubsetArray");
WELS_VERIFY_RETURN_PROC_IF (1, (NULL == (*ppCtx)->pSubsetArray), FreeMemorySvc (ppCtx))
} else {
(*ppCtx)->pSubsetArray = NULL;
@@ -1317,7 +1317,7 @@
// PPS
const int32_t kiNeededPpsNum = (*ppCtx)->GetNeededPpsNum();
- (*ppCtx)->pPPSArray = (SWelsPPS*)pMa->WelsMalloc (kiNeededPpsNum * sizeof (SWelsPPS), "pPPSArray");
+ (*ppCtx)->pPPSArray = (SWelsPPS*)pMa->WelsMallocz (kiNeededPpsNum * sizeof (SWelsPPS), "pPPSArray");
WELS_VERIFY_RETURN_PROC_IF (1, (NULL == (*ppCtx)->pPPSArray), FreeMemorySvc (ppCtx))
// copy from existing if the pointer exists
@@ -1493,7 +1493,7 @@
if (kiNumSpatialLayers <= 0 || kiNumSpatialLayers > MAX_DEPENDENCY_LAYER)
return 1;
- pPtr = (SStrideTables*)pMa->WelsMalloc (sizeof (SStrideTables), "SStrideTables");
+ pPtr = (SStrideTables*)pMa->WelsMallocz (sizeof (SStrideTables), "SStrideTables");
if (NULL == pPtr)
return 1;
(*ppCtx)->pStrideTab = pPtr;
@@ -1540,7 +1540,7 @@
iNeedAllocSize = iSizeDec + iSizeEnc + (iUnit2Size << 1);
- pBase = (uint8_t*)pMa->WelsMalloc (iNeedAllocSize, "pBase");
+ pBase = (uint8_t*)pMa->WelsMallocz (iNeedAllocSize, "pBase");
if (NULL == pBase) {
return 1;
}
@@ -1639,7 +1639,7 @@
iMaxMbWidth = WELS_ALIGN (iMaxMbWidth, 4); // 4 loops for int16_t required introduced as below
iRowSize = iMaxMbWidth * sizeof (int16_t);
- pTmpRow = (int16_t*)pMa->WelsMalloc (iRowSize, "pTmpRow");
+ pTmpRow = (int16_t*)pMa->WelsMallocz (iRowSize, "pTmpRow");
if (NULL == pTmpRow) {
return 1;
}
@@ -1860,12 +1860,12 @@
MAX_REFERENCE_PICTURE_COUNT_NUM_SCREEN));
// Output
- (*ppCtx)->pOut = (SWelsEncoderOutput*)pMa->WelsMalloc (sizeof (SWelsEncoderOutput), "SWelsEncoderOutput");
+ (*ppCtx)->pOut = (SWelsEncoderOutput*)pMa->WelsMallocz (sizeof (SWelsEncoderOutput), "SWelsEncoderOutput");
WELS_VERIFY_RETURN_PROC_IF (1, (NULL == (*ppCtx)->pOut), FreeMemorySvc (ppCtx))
- (*ppCtx)->pOut->pBsBuffer = (uint8_t*)pMa->WelsMalloc (iCountBsLen, "pOut->pBsBuffer");
+ (*ppCtx)->pOut->pBsBuffer = (uint8_t*)pMa->WelsMallocz (iCountBsLen, "pOut->pBsBuffer");
WELS_VERIFY_RETURN_PROC_IF (1, (NULL == (*ppCtx)->pOut->pBsBuffer), FreeMemorySvc (ppCtx))
(*ppCtx)->pOut->uiSize = iCountBsLen;
- (*ppCtx)->pOut->sNalList = (SWelsNalRaw*)pMa->WelsMalloc (iCountNals * sizeof (SWelsNalRaw), "pOut->sNalList");
+ (*ppCtx)->pOut->sNalList = (SWelsNalRaw*)pMa->WelsMallocz (iCountNals * sizeof (SWelsNalRaw), "pOut->sNalList");
WELS_VERIFY_RETURN_PROC_IF (1, (NULL == (*ppCtx)->pOut->sNalList), FreeMemorySvc (ppCtx))
(*ppCtx)->pOut->pNalLen = (int32_t*)pMa->WelsMallocz (iCountNals * sizeof (int32_t), "pOut->pNalLen");
WELS_VERIFY_RETURN_PROC_IF (1, (NULL == (*ppCtx)->pOut->pNalLen), FreeMemorySvc (ppCtx))
@@ -1909,7 +1909,7 @@
(*ppCtx)->iGlobalQp = 26; // global qp in default
- (*ppCtx)->pLtr = (SLTRState*)pMa->WelsMalloc (kiNumDependencyLayers * sizeof (SLTRState), "SLTRState");
+ (*ppCtx)->pLtr = (SLTRState*)pMa->WelsMallocz (kiNumDependencyLayers * sizeof (SLTRState), "SLTRState");
WELS_VERIFY_RETURN_PROC_IF (1, (NULL == (*ppCtx)->pLtr), FreeMemorySvc (ppCtx))
int32_t i = 0;
for (i = 0; i < kiNumDependencyLayers; i++) {
@@ -2256,9 +2256,9 @@
pCtx->pVaa = NULL;
}
- WelsRcFreeMemory (pCtx);
// rate control module memory free
if (NULL != pCtx->pWelsSvcRc) {
+ WelsRcFreeMemory (pCtx);
pMa->WelsFree (pCtx->pWelsSvcRc, "pWelsSvcRc");
pCtx->pWelsSvcRc = NULL;
}
@@ -2434,6 +2434,51 @@
}
/*!
+ * \brief uninitialize Wels encoder core library
+ * \pParam pEncCtx sWelsEncCtx*
+ * \return none
+ */
+void WelsUninitEncoderExt (sWelsEncCtx** ppCtx) {
+ if (NULL == ppCtx || NULL == *ppCtx)
+ return;
+
+ WelsLog (& (*ppCtx)->sLogCtx, WELS_LOG_INFO,
+ "WelsUninitEncoderExt(), pCtx= %p, iMultipleThreadIdc= %d.",
+ (void*) (*ppCtx), (*ppCtx)->pSvcParam->iMultipleThreadIdc);
+
+#if defined(STAT_OUTPUT)
+ StatOverallEncodingExt (*ppCtx);
+#endif
+
+ if ((*ppCtx)->pSvcParam->iMultipleThreadIdc > 1 && (*ppCtx)->pSliceThreading != NULL) {
+ const int32_t iThreadCount = (*ppCtx)->pSvcParam->iMultipleThreadIdc;
+ int32_t iThreadIdx = 0;
+
+ while (iThreadIdx < iThreadCount) {
+ int res = 0;
+ if ((*ppCtx)->pSliceThreading->pThreadHandles[iThreadIdx]) {
+ WelsEventSignal (& (*ppCtx)->pSliceThreading->pExitEncodeEvent[iThreadIdx]);
+ WelsEventSignal (& (*ppCtx)->pSliceThreading->pThreadMasterEvent[iThreadIdx]);
+ res = WelsThreadJoin ((*ppCtx)->pSliceThreading->pThreadHandles[iThreadIdx]); // waiting thread exit
+ WelsLog (& (*ppCtx)->sLogCtx, WELS_LOG_INFO, "WelsUninitEncoderExt(), pthread_join(pThreadHandles%d) return %d..",
+ iThreadIdx,
+ res);
+ (*ppCtx)->pSliceThreading->pThreadHandles[iThreadIdx] = 0;
+ }
+ ++ iThreadIdx;
+ }
+ }
+
+ if ((*ppCtx)->pVpp) {
+ (*ppCtx)->pVpp->FreeSpatialPictures (*ppCtx);
+ delete (*ppCtx)->pVpp;
+ (*ppCtx)->pVpp = NULL;
+ }
+ FreeMemorySvc (ppCtx);
+ *ppCtx = NULL;
+}
+
+/*!
* \brief initialize Wels avc encoder core library
* \pParam ppCtx sWelsEncCtx**
* \pParam pParam SWelsSvcCodingParam*
@@ -2481,18 +2526,18 @@
pCtx->sLogCtx = *pLogCtx;
pCtx->pMemAlign = new CMemoryAlign (iCacheLineSize);
- WELS_VERIFY_RETURN_PROC_IF (1, (NULL == pCtx->pMemAlign), FreeMemorySvc (&pCtx))
+ WELS_VERIFY_RETURN_PROC_IF (1, (NULL == pCtx->pMemAlign), WelsUninitEncoderExt (&pCtx))
iRet = AllocCodingParam (&pCtx->pSvcParam, pCtx->pMemAlign);
if (iRet != 0) {
- FreeMemorySvc (&pCtx);
+ WelsUninitEncoderExt (&pCtx);
return iRet;
}
memcpy (pCtx->pSvcParam, pCodingParam, sizeof (SWelsSvcCodingParam)); // confirmed_safe_unsafe_usage
- pCtx->pFuncList = (SWelsFuncPtrList*)pCtx->pMemAlign->WelsMalloc (sizeof (SWelsFuncPtrList), "SWelsFuncPtrList");
+ pCtx->pFuncList = (SWelsFuncPtrList*)pCtx->pMemAlign->WelsMallocz (sizeof (SWelsFuncPtrList), "SWelsFuncPtrList");
if (NULL == pCtx->pFuncList) {
- FreeMemorySvc (&pCtx);
+ WelsUninitEncoderExt (&pCtx);
return 1;
}
InitFunctionPointers (pCtx, pCtx->pSvcParam, uiCpuFeatureFlags);
@@ -2502,7 +2547,7 @@
iRet = RequestMemorySvc (&pCtx, pExistingParasetList);
if (iRet != 0) {
WelsLog (pLogCtx, WELS_LOG_ERROR, "WelsInitEncoderExt(), RequestMemorySvc failed return %d.", iRet);
- FreeMemorySvc (&pCtx);
+ WelsUninitEncoderExt (&pCtx);
return iRet;
}
@@ -2514,12 +2559,12 @@
if (pCtx->pVpp == NULL) {
iRet = 1;
WelsLog (pLogCtx, WELS_LOG_ERROR, "WelsInitEncoderExt(), pOut of memory in case new CWelsPreProcess().");
- FreeMemorySvc (&pCtx);
+ WelsUninitEncoderExt (&pCtx);
return iRet;
}
if ((iRet = pCtx->pVpp->AllocSpatialPictures (pCtx, pCtx->pSvcParam)) != 0) {
WelsLog (pLogCtx, WELS_LOG_ERROR, "WelsInitEncoderExt(), pVPP alloc spatial pictures failed");
- FreeMemorySvc (&pCtx);
+ WelsUninitEncoderExt (&pCtx);
return iRet;
}
@@ -2618,51 +2663,7 @@
}
}
#endif
-/*!
- * \brief uninitialize Wels encoder core library
- * \pParam pEncCtx sWelsEncCtx*
- * \return none
- */
-void WelsUninitEncoderExt (sWelsEncCtx** ppCtx) {
- if (NULL == ppCtx || NULL == *ppCtx)
- return;
- WelsLog (& (*ppCtx)->sLogCtx, WELS_LOG_INFO,
- "WelsUninitEncoderExt(), pCtx= %p, iMultipleThreadIdc= %d.",
- (void*) (*ppCtx), (*ppCtx)->pSvcParam->iMultipleThreadIdc);
-
-#if defined(STAT_OUTPUT)
- StatOverallEncodingExt (*ppCtx);
-#endif
-
- if ((*ppCtx)->pSvcParam->iMultipleThreadIdc > 1 && (*ppCtx)->pSliceThreading != NULL) {
- const int32_t iThreadCount = (*ppCtx)->pSvcParam->iMultipleThreadIdc;
- int32_t iThreadIdx = 0;
-
- while (iThreadIdx < iThreadCount) {
- int res = 0;
- if ((*ppCtx)->pSliceThreading->pThreadHandles[iThreadIdx]) {
- WelsEventSignal (& (*ppCtx)->pSliceThreading->pExitEncodeEvent[iThreadIdx]);
- WelsEventSignal (& (*ppCtx)->pSliceThreading->pThreadMasterEvent[iThreadIdx]);
- res = WelsThreadJoin ((*ppCtx)->pSliceThreading->pThreadHandles[iThreadIdx]); // waiting thread exit
- WelsLog (& (*ppCtx)->sLogCtx, WELS_LOG_INFO, "WelsUninitEncoderExt(), pthread_join(pThreadHandles%d) return %d..",
- iThreadIdx,
- res);
- (*ppCtx)->pSliceThreading->pThreadHandles[iThreadIdx] = 0;
- }
- ++ iThreadIdx;
- }
- }
-
- if ((*ppCtx)->pVpp) {
- (*ppCtx)->pVpp->FreeSpatialPictures (*ppCtx);
- delete (*ppCtx)->pVpp;
- (*ppCtx)->pVpp = NULL;
- }
- FreeMemorySvc (ppCtx);
- *ppCtx = NULL;
-}
-
/*!
* \brief get temporal level due to configuration and coding context
*/
@@ -4786,7 +4787,7 @@
iCountNals += iMaxSliceNum * (pCtx->pSvcParam->iSpatialLayerNum + pCtx->bNeedPrefixNalFlag);
iMaxSliceNum *= SLICE_NUM_EXPAND_COEF;
- SWelsNalRaw* pNalList = (SWelsNalRaw*)pMA->WelsMalloc (iCountNals * sizeof (SWelsNalRaw), "pOut->sNalList");
+ SWelsNalRaw* pNalList = (SWelsNalRaw*)pMA->WelsMallocz (iCountNals * sizeof (SWelsNalRaw), "pOut->sNalList");
if (NULL == pNalList) {
WelsLog (& (pCtx->sLogCtx), WELS_LOG_ERROR, "CWelsH264SVCEncoder::DynSliceRealloc: pNalList is NULL");
return ENC_RETURN_MEMALLOCERR;
@@ -4795,7 +4796,7 @@
pMA->WelsFree (pCtx->pOut->sNalList, "pOut->sNalList");
pCtx->pOut->sNalList = pNalList;
- int32_t* pNalLen = (int32_t*)pMA->WelsMalloc (iCountNals * sizeof (int32_t), "pOut->pNalLen");
+ int32_t* pNalLen = (int32_t*)pMA->WelsMallocz (iCountNals * sizeof (int32_t), "pOut->pNalLen");
if (NULL == pNalLen) {
WelsLog (& (pCtx->sLogCtx), WELS_LOG_ERROR, "CWelsH264SVCEncoder::DynSliceRealloc: pNalLen is NULL");
return ENC_RETURN_MEMALLOCERR;
@@ -4857,7 +4858,7 @@
//deal with rate control variables
const int32_t kiCurDid = pCtx->uiDependencyId;
- SRCSlicing* pSlcingOverRc = (SRCSlicing*)pMA->WelsMalloc (iMaxSliceNum * sizeof (SRCSlicing), "SlicingOverRC");
+ SRCSlicing* pSlcingOverRc = (SRCSlicing*)pMA->WelsMallocz (iMaxSliceNum * sizeof (SRCSlicing), "SlicingOverRC");
if (NULL == pSlcingOverRc) {
WelsLog (& (pCtx->sLogCtx), WELS_LOG_ERROR,
"CWelsH264SVCEncoder::DynSliceRealloc: realloc pSlcingOverRc not successful");
--- a/codec/encoder/core/src/wels_preprocess.cpp
+++ b/codec/encoder/core/src/wels_preprocess.cpp
@@ -135,6 +135,7 @@
const uint8_t kuiRefNumInTemporal = kuiLayerInTemporal + pParam->iLTRRefNum;
uint8_t i = 0;
+ m_uiSpatialPicNum[iDlayerIndex] = kuiRefNumInTemporal;
do {
SPicture* pPic = AllocPicture (pMa, kiPicWidth, kiPicHeight, false, 0);
WELS_VERIFY_RETURN_IF (1, (NULL == pPic))
@@ -147,7 +148,6 @@
else
m_uiSpatialLayersInTemporal[iDlayerIndex] = kuiLayerInTemporal;
- m_uiSpatialPicNum[iDlayerIndex] = kuiRefNumInTemporal;
++ iDlayerIndex;
} while (iDlayerIndex < kiDlayerCount);