History

Threaded decoding enhancement:

1. Distinguish non-threaded decoding with m_iThreadCount = 0 from one-thread decoding with m_iThreadCount=1. 2. Removed the use of bAvailable of PPicture and replaced by the use of iRefCount to more preciously represent the picture's being-use counts in both threaded and non-threaded mode.

xiaotiansf committed 7 years ago

Commit e7f5f6a1 parent 033c6a014 files · +125 −104Browse files

Showing the changes to codec/decoder/plus/src/welsDecoderExt.cpp (1 of 14 files). Show all

Unified · Split

codec/decoder/plus/src/welsDecoderExt.cpp+53−32
@@ -105,7 +105,9 @@ static DECODING_STATE ConstructAccessUnit (CWelsDecoder* pWelsDecoder, PWelsDec · 104 unmodified lines
105105 RESET_EVENT (&pLastThreadCtx->sSliceDecodeStart);
106106 }
107107 pThrCtx->pDec = NULL;
108− RESET_EVENT (&pThrCtx->sSliceDecodeFinsh);
108+ if (GetThreadCount (pThrCtx->pCtx) > 1) {
109+ RESET_EVENT (&pThrCtx->sSliceDecodeFinsh);
110+ }
109111 iRet |= pWelsDecoder->DecodeFrame2WithCtx (pThrCtx->pCtx, NULL, 0, pThrCtx->ppDst, &pThrCtx->sDstInfo);
110112
111113 //WelsMutexUnlock (&pWelsDecoder->m_csDecoder);
@@ -133,7 +135,8 @@ CWelsDecoder::CWelsDecoder (void) · 21 unmodified lines
133135 m_uiDecodeTimeStamp (0),
134136 m_bIsBaseline (false),
135137 m_iCpuCount (1),
136− m_iThreadCount (1),
138+ m_iThreadCount (0),
139+ m_iCtxCount (1),
137140 m_pPicBuff (NULL),
138141 m_bParamSetsLostFlag (false),
139142 m_bFreezeOutput (false),
@@ -167,8 +170,9 @@ CWelsDecoder::CWelsDecoder (void) · 27 unmodified lines
167170 if (m_iCpuCount > WELS_DEC_MAX_NUM_CPU) {
168171 m_iCpuCount = WELS_DEC_MAX_NUM_CPU;
169172 }
170− m_pDecThrCtx = new SWelsDecoderThreadCTX[m_iThreadCount];
171− memset (m_pDecThrCtx, 0, sizeof (SWelsDecoderThreadCTX)*m_iThreadCount);
173+
174+ m_pDecThrCtx = new SWelsDecoderThreadCTX[m_iCtxCount];
175+ memset (m_pDecThrCtx, 0, sizeof (SWelsDecoderThreadCTX)*m_iCtxCount);
172176 for (int32_t i = 0; i < WELS_DEC_MAX_NUM_CPU; ++i) {
173177 m_pDecThrCtxActive[i] = NULL;
174178 }
@@ -277,7 +281,7 @@ long CWelsDecoder::Uninitialize() { · 102 unmodified lines
277281 }
278282
279283 void CWelsDecoder::UninitDecoder (void) {
280− for (int32_t i = 0; i < m_iThreadCount; ++i) {
284+ for (int32_t i = 0; i < m_iCtxCount; ++i) {
281285 if (m_pDecThrCtx[i].pCtx != NULL) {
282286 if (i > 0) {
283287 WelsResetRefPicWithoutUnRef (m_pDecThrCtx[i].pCtx);
@@ -288,7 +292,7 @@ void CWelsDecoder::UninitDecoder (void) { · 4 unmodified lines
288292 }
289293
290294 void CWelsDecoder::OpenDecoderThreads() {
291− if (m_iThreadCount > 1) {
295+ if (m_iThreadCount >= 1) {
292296 m_uiDecodeTimeStamp = 0;
293297 CREATE_SEMAPHORE (&m_sIsBusy, m_iThreadCount, m_iThreadCount, NULL);
294298 WelsMutexInit (&m_csDecoder);
@@ -318,7 +322,7 @@ void CWelsDecoder::OpenDecoderThreads() { · 23 unmodified lines
318322 }
319323 }
320324 void CWelsDecoder::CloseDecoderThreads() {
321− if (m_iThreadCount > 1) {
325+ if (m_iThreadCount >= 1) {
322326 for (int32_t i = 0; i < m_iThreadCount; i++) { //waiting the completion begun slices
323327 WAIT_SEMAPHORE (&m_pDecThrCtx[i].sThreadInfo.sIsIdle, WELS_DEC_THREAD_WAIT_INFINITE);
324328 m_pDecThrCtx[i].sThreadInfo.uiCommand = WELS_DEC_THREAD_COMMAND_ABORT;
@@ -367,8 +371,8 @@ int32_t CWelsDecoder::InitDecoder (const SDecodingParam* pParam) { · 42 unmodified lines
367371 WelsLog (&m_pWelsTrace->m_sLogCtx, WELS_LOG_INFO,
368372 "CWelsDecoder::init_decoder(), openh264 codec version = %s, ParseOnly = %d",
369373 VERSION_NUMBER, (int32_t)pParam->bParseOnly);
370− if (m_iThreadCount > 1 && pParam->bParseOnly) {
371− m_iThreadCount = 1;
374+ if (m_iThreadCount >= 1 && pParam->bParseOnly) {
375+ m_iThreadCount = 0;
372376 }
373377 OpenDecoderThreads();
374378 //reset decoder context
@@ -377,9 +381,9 @@ int32_t CWelsDecoder::InitDecoder (const SDecodingParam* pParam) { · 2 unmodified lines
377381 memset (&m_sVlcTable, 0, sizeof (SVlcTable));
378382 UninitDecoder();
379383 WelsDecoderLastDecPicInfoDefaults (m_sLastDecPicInfo);
380− for (int32_t i = 0; i < m_iThreadCount; ++i) {
384+ for (int32_t i = 0; i < m_iCtxCount; ++i) {
381385 InitDecoderCtx (m_pDecThrCtx[i].pCtx, pParam);
382− if (m_iThreadCount > 1) {
386+ if (m_iThreadCount >= 1) {
383387 m_pDecThrCtx[i].pCtx->pThreadCtx = &m_pDecThrCtx[i];
384388 }
385389 }
@@ -429,7 +433,7 @@ int32_t CWelsDecoder::InitDecoderCtx (PWelsDecoderContext& pCtx, const SDecoding · 43 unmodified lines
429433
430434 int32_t CWelsDecoder::ResetDecoder (PWelsDecoderContext& pCtx) {
431435 // TBC: need to be modified when context and trace point are null
432− if (m_iThreadCount > 1) {
436+ if (m_iThreadCount >= 1) {
433437 ThreadResetDecoder (pCtx);
434438 } else {
435439 if (pCtx != NULL && m_pWelsTrace != NULL) {
@@ -472,9 +476,8 @@ long CWelsDecoder::SetOption (DECODER_OPTION eOptID, void* pOption) { · 36 unmodified lines
472476 if (eOptID == DECODER_OPTION_NUM_OF_THREADS) {
473477 if (pOption != NULL) {
474478 int32_t threadCount = * ((int32_t*)pOption);
475− if (threadCount <= 0) {
476− threadCount = 1;
477− } else if (threadCount > m_iCpuCount) {
479+ if (threadCount < 0) threadCount = 0;
480+ if (threadCount > m_iCpuCount) {
478481 threadCount = m_iCpuCount;
479482 }
480483 if (threadCount > 3) {
@@ -484,14 +487,15 @@ long CWelsDecoder::SetOption (DECODER_OPTION eOptID, void* pOption) { · 3 unmodified lines
484487 m_iThreadCount = threadCount;
485488 if (m_pDecThrCtx != NULL) {
486489 delete [] m_pDecThrCtx;
487− m_pDecThrCtx = new SWelsDecoderThreadCTX[m_iThreadCount];
488− memset (m_pDecThrCtx, 0, sizeof (SWelsDecoderThreadCTX)*m_iThreadCount);
490+ m_iCtxCount = m_iThreadCount == 0 ? 1 : m_iThreadCount;
491+ m_pDecThrCtx = new SWelsDecoderThreadCTX[m_iCtxCount];
492+ memset (m_pDecThrCtx, 0, sizeof (SWelsDecoderThreadCTX)*m_iCtxCount);
489493 }
490494 }
491495 }
492496 return cmResultSuccess;
493497 }
494− for (int32_t i = 0; i < m_iThreadCount; ++i) {
498+ for (int32_t i = 0; i < m_iCtxCount; ++i) {
495499 PWelsDecoderContext pDecContext = m_pDecThrCtx[i].pCtx;
496500 if (pDecContext == NULL && eOptID != DECODER_OPTION_TRACE_LEVEL &&
497501 eOptID != DECODER_OPTION_TRACE_CALLBACK && eOptID != DECODER_OPTION_TRACE_CALLBACK_CONTEXT)
@@ -502,6 +506,8 @@ long CWelsDecoder::SetOption (DECODER_OPTION eOptID, void* pOption) { · 4 unmodified lines
502506
503507 iVal = * ((int*)pOption); // boolean value for whether enabled End Of Stream flag
504508
509+ if (pDecContext == NULL) return dsInitialOptExpected;
510+
505511 pDecContext->bEndOfStreamFlag = iVal ? true : false;
506512
507513 return cmResultSuccess;
@@ -509,6 +515,8 @@ long CWelsDecoder::SetOption (DECODER_OPTION eOptID, void* pOption) { · 1 unmodified line
509515 if (pOption == NULL)
510516 return cmInitParaError;
511517
518+ if (pDecContext == NULL) return dsInitialOptExpected;
519+
512520 iVal = * ((int*)pOption); // int value for error concealment idc
513521 iVal = WELS_CLIP3 (iVal, (int32_t)ERROR_CON_DISABLE, (int32_t)ERROR_CON_SLICE_MV_COPY_CROSS_IDR_FREEZE_RES_CHANGE);
514522 if ((pDecContext->pParam->bParseOnly) && (iVal != (int32_t)ERROR_CON_DISABLE)) {
@@ -550,6 +558,7 @@ long CWelsDecoder::SetOption (DECODER_OPTION eOptID, void* pOption) { · 35 unmodified lines
550558 return cmInitParaError;
551559 } else if (eOptID == DECODER_OPTION_STATISTICS_LOG_INTERVAL) {
552560 if (pOption) {
561+ if (pDecContext == NULL) return dsInitialOptExpected;
553562 pDecContext->pDecoderStatistics->iStatisticsLogInterval = (* ((unsigned int*)pOption));
554563 return cmResultSuccess;
555564 }
@@ -681,7 +690,7 @@ DECODING_STATE CWelsDecoder::DecodeFrameNoDelay (const unsigned char* kpSrc, · 125 unmodified lines
681690 unsigned char** ppDst,
682691 SBufferInfo* pDstInfo) {
683692 int iRet = dsErrorFree;
684− if (m_iThreadCount > 1) {
693+ if (m_iThreadCount >= 1) {
685694 iRet = ThreadDecodeFrameInternal (kpSrc, kiSrcLen, ppDst, pDstInfo);
686695 if (m_sReoderingStatus.iNumOfPicts) {
687696 WAIT_EVENT (&m_sBufferingEvent, WELS_DEC_THREAD_WAIT_INFINITE);
@@ -742,6 +751,9 @@ DECODING_STATE CWelsDecoder::DecodeFrame2WithCtx (PWelsDecoderContext pDecContex · 54 unmodified lines
742751 }
743752 #endif//OUTPUT_BIT_STREAM
744753 pDecContext->bEndOfStreamFlag = false;
754+ if (GetThreadCount (pDecContext) <= 0) {
755+ pDecContext->uiDecodingTimeStamp = ++m_uiDecodeTimeStamp;
756+ }
745757 } else {
746758 //For application MODE, the error detection should be added for safe.
747759 //But for CONSOLE MODE, when decoding LAST AU, kiSrcLen==0 && kpSrc==NULL.
@@ -752,13 +764,13 @@ DECODING_STATE CWelsDecoder::DecodeFrame2WithCtx (PWelsDecoderContext pDecContex · 4 unmodified lines
752764 int64_t iStart, iEnd;
753765 iStart = WelsTime();
754766
755− if (pDecContext->pThreadCtx == NULL) {
767+ if (GetThreadCount (pDecContext) <= 1) {
756768 ppDst[0] = ppDst[1] = ppDst[2] = NULL;
757769 }
758770 pDecContext->iErrorCode = dsErrorFree; //initialize at the starting of AU decoding.
759771 pDecContext->iFeedbackVclNalInAu = FEEDBACK_UNKNOWN_NAL; //initialize
760772 unsigned long long uiInBsTimeStamp = pDstInfo->uiInBsTimeStamp;
761− if (pDecContext->pThreadCtx == NULL) {
773+ if (GetThreadCount (pDecContext) <= 1) {
762774 memset (pDstInfo, 0, sizeof (SBufferInfo));
763775 }
764776 pDstInfo->uiInBsTimeStamp = uiInBsTimeStamp;
@@ -856,7 +868,7 @@ DECODING_STATE CWelsDecoder::DecodeFrame2WithCtx (PWelsDecoderContext pDecContex · 91 unmodified lines
856868
857869 OutputStatisticsLog (*pDecContext->pDecoderStatistics);
858870
859− if (pDecContext->pThreadCtx != NULL) {
871+ if (GetThreadCount (pDecContext) >= 1) {
860872 WAIT_EVENT (&m_sReleaseBufferEvent, WELS_DEC_THREAD_WAIT_INFINITE);
861873 RESET_EVENT (&m_sBufferingEvent);
862874 BufferingReadyPicture (pDecContext, ppDst, pDstInfo);
@@ -882,7 +894,7 @@ DECODING_STATE CWelsDecoder::DecodeFrame2WithCtx (PWelsDecoderContext pDecContex · 19 unmodified lines
882894 iEnd = WelsTime();
883895 pDecContext->dDecTime += (iEnd - iStart) / 1e3;
884896
885− if (pDecContext->pThreadCtx != NULL) {
897+ if (GetThreadCount (pDecContext) >= 1) {
886898 WAIT_EVENT (&m_sReleaseBufferEvent, WELS_DEC_THREAD_WAIT_INFINITE);
887899 RESET_EVENT (&m_sBufferingEvent);
888900 BufferingReadyPicture (pDecContext, ppDst, pDstInfo);
@@ -904,7 +916,7 @@ DECODING_STATE CWelsDecoder::DecodeFrame2 (const unsigned char* kpSrc, · 15 unmodified lines
904916 DECODING_STATE CWelsDecoder::FlushFrame (unsigned char** ppDst,
905917 SBufferInfo* pDstInfo) {
906918 bool bEndOfStreamFlag = true;
907− for (int32_t j = 0; j < m_iThreadCount; ++j) {
919+ for (int32_t j = 0; j < m_iCtxCount; ++j) {
908920 if (!m_pDecThrCtx[j].pCtx->bEndOfStreamFlag) {
909921 bEndOfStreamFlag = false;
910922 }
@@ -934,9 +946,10 @@ DECODING_STATE CWelsDecoder::FlushFrame (unsigned char** ppDst, · 23 unmodified lines
934946 ppDst[1] = m_sPictInfoList[m_sReoderingStatus.iPictInfoIndex].pData[1];
935947 ppDst[2] = m_sPictInfoList[m_sReoderingStatus.iPictInfoIndex].pData[2];
936948 m_sPictInfoList[m_sReoderingStatus.iPictInfoIndex].iPOC = IMinInt32;
937− PPicBuff pPicBuff = m_iThreadCount == 1 ? m_pDecThrCtx[0].pCtx->pPicBuff : m_pPicBuff;
949+ PPicBuff pPicBuff = m_iThreadCount <= 1 ? m_pDecThrCtx[0].pCtx->pPicBuff : m_pPicBuff;
938950 if (m_sPictInfoList[m_sReoderingStatus.iPictInfoIndex].iPicBuffIdx < pPicBuff->iCapacity) {
939− pPicBuff->ppPic[m_sPictInfoList[m_sReoderingStatus.iPictInfoIndex].iPicBuffIdx]->bAvailableFlag = true;
951+ PPicture pPic = pPicBuff->ppPic[m_sPictInfoList[m_sReoderingStatus.iPictInfoIndex].iPicBuffIdx];
952+ --pPic->iRefCount;
940953 }
941954 m_sPictInfoList[m_sReoderingStatus.iPictInfoIndex].bLastGOP = false;
942955 m_sReoderingStatus.iMinPOC = IMinInt32;
@@ -1001,6 +1014,7 @@ void CWelsDecoder::BufferingReadyPicture (PWelsDecoderContext pCtx, unsigned cha · 58 unmodified lines
10011014 if (m_sReoderingStatus.iNumOfPicts && pCtx->pLastDecPicInfo->pPreviousDecodedPictureInDpb
10021015 && pCtx->pLastDecPicInfo->pPreviousDecodedPictureInDpb->bNewSeqBegin) {
10031016 m_sReoderingStatus.iLastGOPRemainPicts = m_sReoderingStatus.iNumOfPicts;
1017+
10041018 for (int32_t i = 0; i <= m_sReoderingStatus.iLargestBufferedPicIndex; ++i) {
10051019 if (m_sPictInfoList[i].iPOC > IMinInt32) {
10061020 m_sPictInfoList[i].bLastGOP = true;
@@ -1036,7 +1050,7 @@ void CWelsDecoder::BufferingReadyPicture (PWelsDecoderContext pCtx, unsigned cha · 29 unmodified lines
10361050 m_sPictInfoList[i].iPOC = pCtx->pSliceHeader->iPicOrderCntLsb;
10371051 m_sPictInfoList[i].uiDecodingTimeStamp = pCtx->uiDecodingTimeStamp;
10381052 m_sPictInfoList[i].iPicBuffIdx = pCtx->pLastDecPicInfo->pPreviousDecodedPictureInDpb->iPicBuffIdx;
1039− pCtx->pPicBuff->ppPic[m_sPictInfoList[i].iPicBuffIdx]->bAvailableFlag = false;
1053+ if (GetThreadCount (pCtx) <= 1) ++pCtx->pLastDecPicInfo->pPreviousDecodedPictureInDpb->iRefCount;
10401054 m_sPictInfoList[i].bLastGOP = false;
10411055 pDstInfo->iBufferStatus = 0;
10421056 ++m_sReoderingStatus.iNumOfPicts;
@@ -1051,6 +1065,9 @@ void CWelsDecoder::BufferingReadyPicture (PWelsDecoderContext pCtx, unsigned cha · 8 unmodified lines
10511065 void CWelsDecoder::ReleaseBufferedReadyPicture (PWelsDecoderContext pCtx, unsigned char** ppDst,
10521066 SBufferInfo* pDstInfo) {
10531067 PPicBuff pPicBuff = pCtx ? pCtx->pPicBuff : m_pPicBuff;
1068+ if (pCtx == NULL && m_iThreadCount <= 1) {
1069+ pCtx = m_pDecThrCtx[0].pCtx;
1070+ }
10541071 if (!m_bIsBaseline && m_sReoderingStatus.iLastGOPRemainPicts > 0) {
10551072 m_sReoderingStatus.iMinPOC = IMinInt32;
10561073 for (int32_t i = 0; i <= m_sReoderingStatus.iLargestBufferedPicIndex; ++i) {
@@ -1075,7 +1092,8 @@ void CWelsDecoder::ReleaseBufferedReadyPicture (PWelsDecoderContext pCtx, unsign · 18 unmodified lines
10751092 ppDst[1] = m_sPictInfoList[m_sReoderingStatus.iPictInfoIndex].pData[1];
10761093 ppDst[2] = m_sPictInfoList[m_sReoderingStatus.iPictInfoIndex].pData[2];
10771094 m_sPictInfoList[m_sReoderingStatus.iPictInfoIndex].iPOC = IMinInt32;
1078− pPicBuff->ppPic[m_sPictInfoList[m_sReoderingStatus.iPictInfoIndex].iPicBuffIdx]->bAvailableFlag = true;
1095+ PPicture pPic = pPicBuff->ppPic[m_sPictInfoList[m_sReoderingStatus.iPictInfoIndex].iPicBuffIdx];
1096+ --pPic->iRefCount;
10791097 m_sPictInfoList[m_sReoderingStatus.iPictInfoIndex].bLastGOP = false;
10801098 m_sReoderingStatus.iMinPOC = IMinInt32;
10811099 --m_sReoderingStatus.iNumOfPicts;
@@ -1107,7 +1125,8 @@ void CWelsDecoder::ReleaseBufferedReadyPicture (PWelsDecoderContext pCtx, unsign · 25 unmodified lines
11071125 ppDst[1] = m_sPictInfoList[m_sReoderingStatus.iPictInfoIndex].pData[1];
11081126 ppDst[2] = m_sPictInfoList[m_sReoderingStatus.iPictInfoIndex].pData[2];
11091127 m_sPictInfoList[m_sReoderingStatus.iPictInfoIndex].iPOC = IMinInt32;
1110− pPicBuff->ppPic[m_sPictInfoList[m_sReoderingStatus.iPictInfoIndex].iPicBuffIdx]->bAvailableFlag = true;
1128+ PPicture pPic = pPicBuff->ppPic[m_sPictInfoList[m_sReoderingStatus.iPictInfoIndex].iPicBuffIdx];
1129+ --pPic->iRefCount;
11111130 --m_sReoderingStatus.iNumOfPicts;
11121131 }
11131132 return;
@@ -1147,7 +1166,8 @@ void CWelsDecoder::ReleaseBufferedReadyPicture (PWelsDecoderContext pCtx, unsign · 33 unmodified lines
11471166 ppDst[1] = m_sPictInfoList[m_sReoderingStatus.iPictInfoIndex].pData[1];
11481167 ppDst[2] = m_sPictInfoList[m_sReoderingStatus.iPictInfoIndex].pData[2];
11491168 m_sPictInfoList[m_sReoderingStatus.iPictInfoIndex].iPOC = IMinInt32;
1150− pPicBuff->ppPic[m_sPictInfoList[m_sReoderingStatus.iPictInfoIndex].iPicBuffIdx]->bAvailableFlag = true;
1169+ PPicture pPic = pPicBuff->ppPic[m_sPictInfoList[m_sReoderingStatus.iPictInfoIndex].iPicBuffIdx];
1170+ --pPic->iRefCount;
11511171 m_sPictInfoList[m_sReoderingStatus.iPictInfoIndex].bLastGOP = false;
11521172 m_sReoderingStatus.iMinPOC = IMinInt32;
11531173 --m_sReoderingStatus.iNumOfPicts;
@@ -1159,7 +1179,6 @@ DECODING_STATE CWelsDecoder::ReorderPicturesInDisplay (PWelsDecoderContext pDecC · 5 unmodified lines
11591179 SBufferInfo* pDstInfo) {
11601180 DECODING_STATE iRet = dsErrorFree;
11611181 if (pDstInfo->iBufferStatus == 1) {
1162− ++pDecContext->uiDecodingTimeStamp;
11631182 m_bIsBaseline = pDecContext->pSps->uiProfileIdc == 66 || pDecContext->pSps->uiProfileIdc == 83;
11641183 if (!m_bIsBaseline) {
11651184 BufferingReadyPicture (pDecContext, ppDst, pDstInfo);
@@ -1364,7 +1383,9 @@ int CWelsDecoder::ThreadDecodeFrameInternal (const unsigned char* kpSrc, const i · 198 unmodified lines
13641383 memcpy (&m_pDecThrCtx[signal].sDstInfo, pDstInfo, sizeof (SBufferInfo));
13651384
13661385 ParseAccessUnit (m_pDecThrCtx[signal]);
1367− m_pLastDecThrCtx = &m_pDecThrCtx[signal];
1386+ if (m_iThreadCount > 1) {
1387+ m_pLastDecThrCtx = &m_pDecThrCtx[signal];
1388+ }
13681389 m_pDecThrCtx[signal].sThreadInfo.uiCommand = WELS_DEC_THREAD_COMMAND_RUN;
13691390 RELEASE_SEMAPHORE (&m_pDecThrCtx[signal].sThreadInfo.sIsActivated);
13701391

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