History

fix low memory crash issue

huili2 committed 9 years ago

Commit d1c0a931 parent 48c2fe116 files · +401 −386Browse files

Showing the changes to codec/encoder/core/src/slice_multi_threading.cpp (1 of 16 files). Show all

Unified · Split

codec/encoder/core/src/slice_multi_threading.cpp+30−35
@@ -87,7 +87,7 @@ void UpdateMbListNeighborParallel (SDqLayer* pCurDq, · 86 unmodified lines
87 const int32_t kiEndMbInSlice = iIdx + pUpdateSlice->iCountMbNumInSlice - 1;87 const int32_t kiEndMbInSlice = iIdx + pUpdateSlice->iCountMbNumInSlice - 1;
88 88
89 do {89 do {
90 UpdateMbNeighbor(pCurDq, &pMbList[iIdx], kiMbWidth, uiSliceIdc);90 UpdateMbNeighbor (pCurDq, &pMbList[iIdx], kiMbWidth, uiSliceIdc);
91 ++ iIdx;91 ++ iIdx;
92 } while (iIdx <= kiEndMbInSlice);92 } while (iIdx <= kiEndMbInSlice);
93}93}
@@ -118,7 +118,7 @@ void CalcSliceComplexRatio (SDqLayer* pCurDq) { · 24 unmodified lines
118}118}
119 119
120int32_t NeedDynamicAdjust (SSlice* pSliceInLayer, const int32_t iSliceNum) {120int32_t NeedDynamicAdjust (SSlice* pSliceInLayer, const int32_t iSliceNum) {
121 if ( NULL == pSliceInLayer )121 if (NULL == pSliceInLayer)
122 return false;122 return false;
123 123
124 uint32_t uiTotalConsume = 0;124 uint32_t uiTotalConsume = 0;
@@ -207,7 +207,8 @@ void DynamicAdjustSlicing (sWelsEncCtx* pCtx, · 82 unmodified lines
207 207
208 WelsEmms();208 WelsEmms();
209 209
210 MT_TRACE_LOG (&(pCtx->sLogCtx), WELS_LOG_DEBUG, "[MT] DynamicAdjustSlicing(), iDid= %d, iCountNumMb= %d", iCurDid, kiCountNumMb);210 MT_TRACE_LOG (& (pCtx->sLogCtx), WELS_LOG_DEBUG, "[MT] DynamicAdjustSlicing(), iDid= %d, iCountNumMb= %d", iCurDid,
211 kiCountNumMb);
211 212
212 iSliceIdx = 0;213 iSliceIdx = 0;
213 while (iSliceIdx + 1 < kiCountSliceNum) {214 while (iSliceIdx + 1 < kiCountSliceNum) {
@@ -232,7 +233,7 @@ void DynamicAdjustSlicing (sWelsEncCtx* pCtx, · 18 unmodified lines
232 return;233 return;
233 }234 }
234 iRunLen[iSliceIdx] = iNumMbAssigning;235 iRunLen[iSliceIdx] = iNumMbAssigning;
235 MT_TRACE_LOG (&(pCtx->sLogCtx), WELS_LOG_DEBUG,236 MT_TRACE_LOG (& (pCtx->sLogCtx), WELS_LOG_DEBUG,
236 "[MT] DynamicAdjustSlicing(), uiSliceIdx= %d, iSliceComplexRatio= %.2f, slice_run_org= %d, slice_run_adj= %d",237 "[MT] DynamicAdjustSlicing(), uiSliceIdx= %d, iSliceComplexRatio= %.2f, slice_run_org= %d, slice_run_adj= %d",
237 iSliceIdx, pSliceInLayer[iSliceIdx].iSliceComplexRatio * 1.0f / INT_MULTIPLY,238 iSliceIdx, pSliceInLayer[iSliceIdx].iSliceComplexRatio * 1.0f / INT_MULTIPLY,
238 pSliceInLayer[iSliceIdx].iCountMbNumInSlice,239 pSliceInLayer[iSliceIdx].iCountMbNumInSlice,
@@ -241,9 +242,10 @@ void DynamicAdjustSlicing (sWelsEncCtx* pCtx, · 2 unmodified lines
241 iMaximalMbNum = iMbNumLeft - (kiCountSliceNum - iSliceIdx - 1) * iMinimalMbNum; // get maximal num_mb in left parts242 iMaximalMbNum = iMbNumLeft - (kiCountSliceNum - iSliceIdx - 1) * iMinimalMbNum; // get maximal num_mb in left parts
242 }243 }
243 iRunLen[iSliceIdx] = iMbNumLeft;244 iRunLen[iSliceIdx] = iMbNumLeft;
244 MT_TRACE_LOG (&(pCtx->sLogCtx), WELS_LOG_DEBUG,245 MT_TRACE_LOG (& (pCtx->sLogCtx), WELS_LOG_DEBUG,
245 "[MT] DynamicAdjustSlicing(), iSliceIdx= %d, pSliceComplexRatio= %.2f, slice_run_org= %d, slice_run_adj= %d",246 "[MT] DynamicAdjustSlicing(), iSliceIdx= %d, pSliceComplexRatio= %.2f, slice_run_org= %d, slice_run_adj= %d",
246 iSliceIdx, pSliceInLayer[iSliceIdx].iSliceComplexRatio * 1.0f / INT_MULTIPLY, pSliceInLayer[iSliceIdx].iCountMbNumInSlice, iMbNumLeft);247 iSliceIdx, pSliceInLayer[iSliceIdx].iSliceComplexRatio * 1.0f / INT_MULTIPLY,
248 pSliceInLayer[iSliceIdx].iCountMbNumInSlice, iMbNumLeft);
247 249
248 pCurDqLayer->bNeedAdjustingSlicing = !DynamicAdjustSlicePEncCtxAll (pCurDqLayer, iRunLen);250 pCurDqLayer->bNeedAdjustingSlicing = !DynamicAdjustSlicePEncCtxAll (pCurDqLayer, iRunLen);
249}251}
@@ -274,11 +276,12 @@ int32_t RequestMtResource (sWelsEncCtx** ppCtx, SWelsSvcCodingParam* pCodingPara · 24 unmodified lines
274 iMaxSliceNumInThread = WELS_MIN ((*ppCtx)->iMaxSliceCount, (int) iMaxSliceNumInThread);276 iMaxSliceNumInThread = WELS_MIN ((*ppCtx)->iMaxSliceCount, (int) iMaxSliceNumInThread);
275 277
276 pSmt = (SSliceThreading*)pMa->WelsMalloc (sizeof (SSliceThreading), "SSliceThreading");278 pSmt = (SSliceThreading*)pMa->WelsMalloc (sizeof (SSliceThreading), "SSliceThreading");
277 WELS_VERIFY_RETURN_PROC_IF (1, (NULL == pSmt), FreeMemorySvc (ppCtx))279 WELS_VERIFY_RETURN_IF (1, (NULL == pSmt))
280 memset (pSmt, 0, sizeof (SSliceThreading));
278 (*ppCtx)->pSliceThreading = pSmt;281 (*ppCtx)->pSliceThreading = pSmt;
279 pSmt->pThreadPEncCtx = (SSliceThreadPrivateData*)pMa->WelsMalloc (sizeof (SSliceThreadPrivateData) * iThreadNum,282 pSmt->pThreadPEncCtx = (SSliceThreadPrivateData*)pMa->WelsMalloc (sizeof (SSliceThreadPrivateData) * iThreadNum,
280 "pThreadPEncCtx");283 "pThreadPEncCtx");
281 WELS_VERIFY_RETURN_PROC_IF (1, (NULL == pSmt->pThreadPEncCtx), FreeMemorySvc (ppCtx))284 WELS_VERIFY_RETURN_IF (1, (NULL == pSmt->pThreadPEncCtx))
282 285
283#ifdef _WIN32286#ifdef _WIN32
284 // Dummy event namespace, the windows events don't actually use this287 // Dummy event namespace, the windows events don't actually use this
@@ -333,38 +336,29 @@ int32_t RequestMtResource (sWelsEncCtx** ppCtx, SWelsSvcCodingParam* pCodingPara · 48 unmodified lines
333 (void*)pSmt->pReadySliceCodingEvent[iIdx], name, err, errno);336 (void*)pSmt->pReadySliceCodingEvent[iIdx], name, err, errno);
334 ++ iIdx;337 ++ iIdx;
335 }338 }
336 for (; iIdx < MAX_THREADS_NUM; iIdx++) {
337 pSmt->pThreadBsBuffer[iIdx] = NULL;
338 }
339 339
340 WelsSnprintf (name, SEM_NAME_MAX, "scm%s", pSmt->eventNamespace);340 WelsSnprintf (name, SEM_NAME_MAX, "scm%s", pSmt->eventNamespace);
341 err = WelsEventOpen (&pSmt->pSliceCodedMasterEvent, name);341 err = WelsEventOpen (&pSmt->pSliceCodedMasterEvent, name);
342 MT_TRACE_LOG (pLogCtx, WELS_LOG_INFO, "[MT] Open pSliceCodedMasterEvent named(%s) ret%d err%d", name, err, errno);342 MT_TRACE_LOG (pLogCtx, WELS_LOG_INFO, "[MT] Open pSliceCodedMasterEvent named(%s) ret%d err%d", name, err, errno);
343 343
344 iReturn = WelsMutexInit (&pSmt->mutexSliceNumUpdate);344 iReturn = WelsMutexInit (&pSmt->mutexSliceNumUpdate);
345 WELS_VERIFY_RETURN_PROC_IF (1, (WELS_THREAD_ERROR_OK != iReturn), FreeMemorySvc (ppCtx))345 WELS_VERIFY_RETURN_IF (1, (WELS_THREAD_ERROR_OK != iReturn))
346 346
347 (*ppCtx)->pTaskManage = IWelsTaskManage::CreateTaskManage (*ppCtx, iNumSpatialLayers, bDynamicSlice);347 (*ppCtx)->pTaskManage = IWelsTaskManage::CreateTaskManage (*ppCtx, iNumSpatialLayers, bDynamicSlice);
348 WELS_VERIFY_RETURN_PROC_IF (iReturn, (NULL == (*ppCtx)->pTaskManage), FreeMemorySvc (ppCtx))348 WELS_VERIFY_RETURN_IF (1, (NULL == (*ppCtx)->pTaskManage))
349
350 int32_t iThreadBufferNum = WELS_MIN ((*ppCtx)->pTaskManage->GetThreadPoolThreadNum(), MAX_THREADS_NUM);
349 351
350 int32_t iThreadBufferNum = WELS_MIN((*ppCtx)->pTaskManage->GetThreadPoolThreadNum(), MAX_THREADS_NUM);352 for (iIdx = 0; iIdx < iThreadBufferNum; iIdx++) {
351 for (iIdx = 0;iIdx < iThreadBufferNum; iIdx++) {
352 pSmt->pThreadBsBuffer[iIdx] = (uint8_t*)pMa->WelsMalloc (iCountBsLen, "pSmt->pThreadBsBuffer");353 pSmt->pThreadBsBuffer[iIdx] = (uint8_t*)pMa->WelsMalloc (iCountBsLen, "pSmt->pThreadBsBuffer");
353 WELS_VERIFY_RETURN_PROC_IF (1, (NULL == pSmt->pThreadBsBuffer[iIdx]), FreeMemorySvc (ppCtx))354 WELS_VERIFY_RETURN_IF (1, (NULL == pSmt->pThreadBsBuffer[iIdx]))
354 }
355 if (iThreadBufferNum < MAX_THREADS_NUM) {
356 for (iIdx = iThreadBufferNum; iIdx < MAX_THREADS_NUM; iIdx++) {
357 pSmt->pThreadBsBuffer[iIdx] = NULL;
358 }
359 }355 }
360
361 memset (&pSmt->bThreadBsBufferUsage, 0, MAX_THREADS_NUM * sizeof (bool));
362 iReturn = WelsMutexInit (&pSmt->mutexThreadBsBufferUsage);356 iReturn = WelsMutexInit (&pSmt->mutexThreadBsBufferUsage);
363 WELS_VERIFY_RETURN_PROC_IF (1, (WELS_THREAD_ERROR_OK != iReturn), FreeMemorySvc (ppCtx))357 WELS_VERIFY_RETURN_IF (1, (WELS_THREAD_ERROR_OK != iReturn))
364 iReturn = WelsMutexInit (&pSmt->mutexEvent);358 iReturn = WelsMutexInit (&pSmt->mutexEvent);
365 WELS_VERIFY_RETURN_PROC_IF (1, (WELS_THREAD_ERROR_OK != iReturn), FreeMemorySvc (ppCtx));359 WELS_VERIFY_RETURN_IF (1, (WELS_THREAD_ERROR_OK != iReturn))
366 iReturn = WelsMutexInit (& (*ppCtx)->mutexEncoderError);360 iReturn = WelsMutexInit (& (*ppCtx)->mutexEncoderError);
367 WELS_VERIFY_RETURN_PROC_IF (1, (WELS_THREAD_ERROR_OK != iReturn), FreeMemorySvc (ppCtx))361 WELS_VERIFY_RETURN_IF (1, (WELS_THREAD_ERROR_OK != iReturn))
368 362
369 MT_TRACE_LOG (pLogCtx, WELS_LOG_INFO, "RequestMtResource(), iThreadNum=%d, iMultipleThreadIdc= %d",363 MT_TRACE_LOG (pLogCtx, WELS_LOG_INFO, "RequestMtResource(), iThreadNum=%d, iMultipleThreadIdc= %d",
370 pPara->iMultipleThreadIdc,364 pPara->iMultipleThreadIdc,
@@ -427,7 +421,7 @@ void ReleaseMtResource (sWelsEncCtx** ppCtx) { · 56 unmodified lines
427 memset (&pSmt->bThreadBsBufferUsage, 0, MAX_THREADS_NUM * sizeof (bool));421 memset (&pSmt->bThreadBsBufferUsage, 0, MAX_THREADS_NUM * sizeof (bool));
428 422
429 if ((*ppCtx)->pTaskManage != NULL) {423 if ((*ppCtx)->pTaskManage != NULL) {
430 WELS_DELETE_OP((*ppCtx)->pTaskManage);424 WELS_DELETE_OP ((*ppCtx)->pTaskManage);
431 }425 }
432 426
433#ifdef MT_DEBUG427#ifdef MT_DEBUG
@@ -516,7 +510,7 @@ int32_t AppendSliceToFrameBs (sWelsEncCtx* pCtx, SLayerBSInfo* pLbi, const int32 · 82 unmodified lines
516 return iLayerSize;510 return iLayerSize;
517}511}
518 512
519int32_t WriteSliceBs (sWelsEncCtx* pCtx,SWelsSliceBs* pSliceBs,const int32_t iSliceIdx,int32_t& iSliceSize) {513int32_t WriteSliceBs (sWelsEncCtx* pCtx, SWelsSliceBs* pSliceBs, const int32_t iSliceIdx, int32_t& iSliceSize) {
520 const int32_t kiNalCnt = pSliceBs->iNalIndex;514 const int32_t kiNalCnt = pSliceBs->iNalIndex;
521 int32_t iNalIdx = 0;515 int32_t iNalIdx = 0;
522 int32_t iNalSize = 0;516 int32_t iNalSize = 0;
@@ -581,12 +575,13 @@ WELS_THREAD_ROUTINE_TYPE CodingSliceThreadProc (void* arg) { · 58 unmodified lines
581 WelsThreadSetName ("OpenH264Enc_CodingSliceThreadProc");575 WelsThreadSetName ("OpenH264Enc_CodingSliceThreadProc");
582 576
583 do {577 do {
584 MT_TRACE_LOG (&(pEncPEncCtx->sLogCtx), WELS_LOG_INFO,578 MT_TRACE_LOG (& (pEncPEncCtx->sLogCtx), WELS_LOG_INFO,
585 "[MT] CodingSliceThreadProc(), try to call WelsMultipleEventsWaitSingleBlocking(pEventsList= %p %p %p), pEncPEncCtx= %p!",579 "[MT] CodingSliceThreadProc(), try to call WelsMultipleEventsWaitSingleBlocking(pEventsList= %p %p %p), pEncPEncCtx= %p!",
586 pEventsList[0], pEventsList[1], pEventsList[1], (void*)pEncPEncCtx);580 pEventsList[0], pEventsList[1], pEventsList[1], (void*)pEncPEncCtx);
587 iWaitRet = WelsMultipleEventsWaitSingleBlocking (iEventCount,581 iWaitRet = WelsMultipleEventsWaitSingleBlocking (iEventCount,
588 &pEventsList[0],582 &pEventsList[0],
589 &pEncPEncCtx->pSliceThreading->pThreadMasterEvent[iEventIdx],&pEncPEncCtx->pSliceThreading->mutexEvent); // blocking until at least one event is signalled583 &pEncPEncCtx->pSliceThreading->pThreadMasterEvent[iEventIdx],
584 &pEncPEncCtx->pSliceThreading->mutexEvent); // blocking until at least one event is signalled
590 if (WELS_THREAD_ERROR_WAIT_OBJECT_0 == iWaitRet) { // start pSlice coding signal waited585 if (WELS_THREAD_ERROR_WAIT_OBJECT_0 == iWaitRet) { // start pSlice coding signal waited
591 //int iLayerIndex = pEncPEncCtx->pOut->iLayerBsIndex;586 //int iLayerIndex = pEncPEncCtx->pOut->iLayerBsIndex;
592 //SFrameBSInfo* pFrameBsInfo = pPrivateData->pFrameBsInfo;587 //SFrameBSInfo* pFrameBsInfo = pPrivateData->pFrameBsInfo;
@@ -658,7 +653,7 @@ WELS_THREAD_ROUTINE_TYPE CodingSliceThreadProc (void* arg) { · 65 unmodified lines
658 pEncPEncCtx->pFuncList->pfDeblocking.pfDeblockingFilterSlice (pCurDq, pEncPEncCtx->pFuncList, iSliceIdx);653 pEncPEncCtx->pFuncList->pfDeblocking.pfDeblockingFilterSlice (pCurDq, pEncPEncCtx->pFuncList, iSliceIdx);
659 654
660 if (bDsaFlag) {655 if (bDsaFlag) {
661 pEncPEncCtx->pCurDqLayer->sLayerInfo.pSliceInLayer[iSliceIdx].uiSliceConsumeTime = (uint32_t) (656 pEncPEncCtx->pCurDqLayer->sLayerInfo.pSliceInLayer[iSliceIdx].uiSliceConsumeTime = (uint32_t) (
662 WelsTime() - iSliceStart);657 WelsTime() - iSliceStart);
663 MT_TRACE_LOG (& (pEncPEncCtx->sLogCtx), WELS_LOG_INFO,658 MT_TRACE_LOG (& (pEncPEncCtx->sLogCtx), WELS_LOG_INFO,
664 "[MT] CodingSliceThreadProc(), coding_idx %d, uiSliceIdx %d, uiSliceConsumeTime %d, iSliceSize %d, iFirstMbInSlice %d, count_num_mb_in_slice %d",659 "[MT] CodingSliceThreadProc(), coding_idx %d, uiSliceIdx %d, uiSliceConsumeTime %d, iSliceSize %d, iFirstMbInSlice %d, count_num_mb_in_slice %d",
@@ -693,7 +688,7 @@ WELS_THREAD_ROUTINE_TYPE CodingSliceThreadProc (void* arg) { · 28 unmodified lines
693 const int32_t kiFirstMbInPartition = pPrivateData->iStartMbIndex; // inclusive688 const int32_t kiFirstMbInPartition = pPrivateData->iStartMbIndex; // inclusive
694 const int32_t kiEndMbInPartition = pPrivateData->iEndMbIndex; // exclusive689 const int32_t kiEndMbInPartition = pPrivateData->iEndMbIndex; // exclusive
695 int32_t iAnyMbLeftInPartition = kiEndMbInPartition - kiFirstMbInPartition;690 int32_t iAnyMbLeftInPartition = kiEndMbInPartition - kiFirstMbInPartition;
696 SSpatialLayerInternal *pParamInternal = &pCodingParam->sDependencyLayers[kiCurDid];691 SSpatialLayerInternal* pParamInternal = &pCodingParam->sDependencyLayers[kiCurDid];
697 iSliceIdx = pPrivateData->iSliceIndex;692 iSliceIdx = pPrivateData->iSliceIndex;
698 SSliceHeaderExt* pStartSliceHeaderExt = &pCurDq->sLayerInfo.pSliceInLayer[iSliceIdx].sSliceHeaderExt;693 SSliceHeaderExt* pStartSliceHeaderExt = &pCurDq->sLayerInfo.pSliceInLayer[iSliceIdx].sSliceHeaderExt;
699 pStartSliceHeaderExt->sSliceHeader.iFirstMbInSlice = kiFirstMbInPartition;694 pStartSliceHeaderExt->sSliceHeader.iFirstMbInSlice = kiFirstMbInPartition;
@@ -774,7 +769,7 @@ WELS_THREAD_ROUTINE_TYPE CodingSliceThreadProc (void* arg) { · 74 unmodified lines
774 );769 );
775#endif//SLICE_INFO_OUTPUT770#endif//SLICE_INFO_OUTPUT
776 771
777 MT_TRACE_LOG (&(pEncPEncCtx->sLogCtx), WELS_LOG_INFO,772 MT_TRACE_LOG (& (pEncPEncCtx->sLogCtx), WELS_LOG_INFO,
778 "[MT] CodingSliceThreadProc(), coding_idx %d, iPartitionId %d, uiSliceIdx %d, iSliceSize %d, count_mb_slice %d, iEndMbInPartition %d, pCurDq->pLastCodedMbIdxOfPartition[%d] %d\n",773 "[MT] CodingSliceThreadProc(), coding_idx %d, iPartitionId %d, uiSliceIdx %d, iSliceSize %d, count_mb_slice %d, iEndMbInPartition %d, pCurDq->pLastCodedMbIdxOfPartition[%d] %d\n",
779 pEncPEncCtx->iCodingIndex, kiPartitionId, iSliceIdx, iSliceSize,774 pEncPEncCtx->iCodingIndex, kiPartitionId, iSliceIdx, iSliceSize,
780 pCurDq->sLayerInfo.pSliceInLayer[iSliceIdx].iCountMbNumInSlice,775 pCurDq->sLayerInfo.pSliceInLayer[iSliceIdx].iCountMbNumInSlice,
@@ -833,7 +828,7 @@ int32_t CreateSliceThreads (sWelsEncCtx* pCtx) { · 52 unmodified lines
833 828
834 ++ iIdx;829 ++ iIdx;
835 }830 }
836 MT_TRACE_LOG (&(pCtx->sLogCtx), WELS_LOG_INFO, "CreateSliceThreads() exit..");831 MT_TRACE_LOG (& (pCtx->sLogCtx), WELS_LOG_INFO, "CreateSliceThreads() exit..");
837 return 0;832 return 0;
838}833}
839 834
@@ -1007,7 +1002,7 @@ void TrackSliceConsumeTime (sWelsEncCtx* pCtx, int32_t* pDidList, const int32_t · 167 unmodified lines
1007 int32_t iMaxI = 0;1002 int32_t iMaxI = 0;
1008 while (i < kuiCountSliceNum) {1003 while (i < kuiCountSliceNum) {
1009 fprintf (pCtx->pSliceThreading->pFSliceDiff, "%6d us consume_time coding_idx %d iDid %d pSlice %d\n",1004 fprintf (pCtx->pSliceThreading->pFSliceDiff, "%6d us consume_time coding_idx %d iDid %d pSlice %d\n",
1010 pSliceInLayer[i].uiSliceConsumeTime, pCtx->iCodingIndex, kiDid, i /*/ 1000*/);1005 pSliceInLayer[i].uiSliceConsumeTime, pCtx->iCodingIndex, kiDid, i /*/ 1000*/);
1011 if (pSliceInLayer[i].uiSliceConsumeTime > uiMaxT) {1006 if (pSliceInLayer[i].uiSliceConsumeTime > uiMaxT) {
1012 uiMaxT = pSliceInLayer[i].uiSliceConsumeTime;1007 uiMaxT = pSliceInLayer[i].uiSliceConsumeTime;
1013 iMaxI = i;1008 iMaxI = i;

Keyboard shortcuts

General

Show keyboard shortcuts
?
Focus the current search
/

Issues and pull requests

Open filters
f

Issues, pull requests and Inbox

Select next item
j↓
Select previous item
k↑
Open selected item
Enter
Dismiss filters or clear selection
Esc

Repository code

Go to file
t