shithub: openh264

Download patch

ref: f47be08065944e50bd43fcc1a149ae6d6b6f74d0
parent: d85b1f6863b6ada77023f7cb8c95db91129f17f3
parent: 1247006cbb0111a31261a734ed0324482ffe06df
author: HaiboZhu <haibozhu@cisco.com>
date: Wed Nov 25 07:04:00 EST 2015

Merge pull request #2271 from sijchen/rf1

[Encoder] refactor multi-thread logic and add error-dealing

--- a/codec/encoder/core/src/encoder_ext.cpp
+++ b/codec/encoder/core/src/encoder_ext.cpp
@@ -4037,6 +4037,12 @@
 
         pCtx->pTaskManage->ExecuteTasks();
         iLayerSize = AppendSliceToFrameBs (pCtx, pLayerBsInfo, iSliceCount);
+        if (pCtx->iEncoderError) {
+          WelsLog (pLogCtx, WELS_LOG_ERROR,
+                   "WelsEncoderEncodeExt(), multi-slice (mode %d) encoding error!",
+                   pParam->sSliceArgument.uiSliceMode);
+          return pCtx->iEncoderError;
+        }
       }
       // THREAD_FULLY_FIRE_MODE && SM_SIZELIMITED_SLICE
       else if ((SM_SIZELIMITED_SLICE == pParam->sSliceArgument.uiSliceMode) && (pSvcParam->iMultipleThreadIdc > 1)) {
--- a/codec/encoder/core/src/slice_multi_threading.cpp
+++ b/codec/encoder/core/src/slice_multi_threading.cpp
@@ -604,23 +604,19 @@
         pSliceBs = &pCtx->pSliceBs[iSliceIdx];
         if (pSliceBs != NULL && pSliceBs->uiBsPos > 0) {
           if (iSliceIdx > 0) {
-            int32_t iNalIdx = 0;
-            const int32_t iCountNal = pSliceBs->iNalIndex;
-
             memmove (pCtx->pFrameBs + pCtx->iPosBsBuffer, pSliceBs->pBs, pSliceBs->uiBsPos); // confirmed_safe_unsafe_usage
             pCtx->iPosBsBuffer += pSliceBs->uiBsPos;
+          }
 
-            iLayerSize += pSliceBs->uiBsPos;
-
-            while (iNalIdx < iCountNal) {
-              pLbi->pNalLengthInByte[iNalIdxBase + iNalIdx] = pSliceBs->iNalLen[iNalIdx];
-              ++ iNalIdx;
-            }
-            pLbi->iNalCount += iCountNal;
-            iNalIdxBase     += iCountNal;
-          } else {
-            iLayerSize += pSliceBs->uiBsPos;
+          const int32_t iCountNal = pSliceBs->iNalIndex;
+          iLayerSize += pSliceBs->uiBsPos;
+          int32_t iNalIdx = 0;
+          while (iNalIdx < iCountNal) {
+            pLbi->pNalLengthInByte[iNalIdxBase + iNalIdx] = pSliceBs->iNalLen[iNalIdx];
+            ++ iNalIdx;
           }
+          pLbi->iNalCount += iCountNal;
+          iNalIdxBase     += iCountNal;
         }
 
         iSliceIdx += kiPartitionCnt;
@@ -707,9 +703,9 @@
                &pEventsList[0],
                &pEncPEncCtx->pSliceThreading->pThreadMasterEvent[iEventIdx]); // blocking until at least one event is signalled
     if (WELS_THREAD_ERROR_WAIT_OBJECT_0 == iWaitRet) { // start pSlice coding signal waited
-      int             iLayerIndex  = pEncPEncCtx->pOut->iLayerBsIndex;
-      SFrameBSInfo*   pFrameBsInfo = pPrivateData->pFrameBsInfo;
-      SLayerBSInfo*   pLbi = &pFrameBsInfo->sLayerInfo [iLayerIndex];
+      //int             iLayerIndex  = pEncPEncCtx->pOut->iLayerBsIndex;
+      //SFrameBSInfo*   pFrameBsInfo = pPrivateData->pFrameBsInfo;
+      //SLayerBSInfo*   pLbi = &pFrameBsInfo->sLayerInfo [iLayerIndex];
 
       const int32_t kiCurDid            = pEncPEncCtx->uiDependencyId;
       SWelsSvcCodingParam* pCodingParam = pEncPEncCtx->pSvcParam;
@@ -874,29 +870,18 @@
 
           WelsUnloadNalForSlice (pSliceBs);
 
-          if (0 == iSliceIdx) {
-            iReturn = WriteSliceBs (pEncPEncCtx, pLbi->pBsBuf,
-                                    &pLbi->pNalLengthInByte[pLbi->iNalCount],
-                                    pEncPEncCtx->iFrameBsSize - pEncPEncCtx->iPosBsBuffer,
+          int32_t iLeftBufferSize = (iSliceIdx > 0) ? (pSliceBs->uiSize - pSliceBs->uiBsPos) : (pEncPEncCtx->iFrameBsSize - pEncPEncCtx->iPosBsBuffer);
+          iReturn = WriteSliceBs (pEncPEncCtx, pSliceBs->pBs, &pSliceBs->iNalLen[0],
+                                    iLeftBufferSize,
                                     iSliceIdx, iSliceSize);
-            pLbi->iNalCount    += pSliceBs->iNalIndex;
-            if (ENC_RETURN_SUCCESS != iReturn) {
-              uiThrdRet = iReturn;
-              WELS_THREAD_SIGNAL_AND_BREAK (pEncPEncCtx->pSliceThreading->pSliceCodedEvent,
-                                            pEncPEncCtx->pSliceThreading->pSliceCodedMasterEvent,
-                                            iEventIdx);
-            }
+          if (ENC_RETURN_SUCCESS != iReturn) {
+            uiThrdRet = iReturn;
+            WELS_THREAD_SIGNAL_AND_BREAK (pEncPEncCtx->pSliceThreading->pSliceCodedEvent,
+                                          pEncPEncCtx->pSliceThreading->pSliceCodedMasterEvent,
+                                          iEventIdx);
+          }
+          if (0 == iSliceIdx) {
             pEncPEncCtx->iPosBsBuffer += iSliceSize;
-          } else {
-            iReturn = WriteSliceBs (pEncPEncCtx, pSliceBs->pBs, &pSliceBs->iNalLen[0],
-                                    pSliceBs->uiSize - pSliceBs->uiBsPos,
-                                    iSliceIdx, iSliceSize);
-            if (ENC_RETURN_SUCCESS != iReturn) {
-              uiThrdRet = iReturn;
-              WELS_THREAD_SIGNAL_AND_BREAK (pEncPEncCtx->pSliceThreading->pSliceCodedEvent,
-                                            pEncPEncCtx->pSliceThreading->pSliceCodedMasterEvent,
-                                            iEventIdx);
-            }
           }
 
           pEncPEncCtx->pFuncList->pfDeblocking.pfDeblockingFilterSlice (pCurDq, pEncPEncCtx->pFuncList, iSliceIdx);