shithub: openh264

Download patch

ref: 7b286bc6372997875d12924572558c85236b72f2
parent: 5d9b95288f47f703c2dd804cd0510de2812a236c
author: huade <[email protected]>
date: Thu Aug 27 13:24:48 EDT 2015

replace LayerBsInfo with FrameBsInfo in thread-based private data structure

--- a/codec/encoder/core/inc/extern.h
+++ b/codec/encoder/core/inc/extern.h
@@ -112,6 +112,7 @@
 int32_t WelsEncoderApplyBitRate (SLogContext* pLogCtx, SWelsSvcCodingParam* pParam, int32_t iLayer);
 int32_t WelsEncoderApplyBitVaryRang(SLogContext* pLogCtx, SWelsSvcCodingParam* pParam, int32_t iRang);
 int32_t WelsEncoderApplyLTR (SLogContext* pLogCtx, sWelsEncCtx** ppCtx, SLTRConfig* pLTRValue);
+int32_t DynSliceRealloc(sWelsEncCtx* pCtx,SFrameBSInfo* pFrameBsInfo,SLayerBSInfo* pLayerBsInfo);
 int32_t FilterLTRRecoveryRequest (sWelsEncCtx* pCtx, SLTRRecoverRequest* pLTRRecoverRequest);
 void CheckProfileSetting (SLogContext* pLogCtx,SWelsSvcCodingParam* pParam,int32_t iLayer, EProfileIdc uiProfileIdc);
 void CheckLevelSetting (SLogContext* pLogCtx,SWelsSvcCodingParam* pParam,int32_t iLayer, ELevelIdc uiLevelIdc);
--- a/codec/encoder/core/inc/mt_defs.h
+++ b/codec/encoder/core/inc/mt_defs.h
@@ -57,7 +57,7 @@
 
 typedef struct TagSliceThreadPrivateData {
 void*           pWelsPEncCtx;
-SLayerBSInfo*   pLayerBs;
+SFrameBSInfo*   pFrameBsInfo;
 int32_t         iSliceIndex;    // slice index, zero based
 int32_t         iThreadIndex;   // thread index, zero based
 
--- a/codec/encoder/core/inc/nal_encap.h
+++ b/codec/encoder/core/inc/nal_encap.h
@@ -77,7 +77,7 @@
 int32_t         iCountNals;             // count number of NAL in list
 // SVC: num_sps (MAX_D) + num_pps (MAX_D) + num_vcl (MAX_D * MAX_Q)
 int32_t         iNalIndex;              // coding NAL currently, 0 based
-
+int32_t         iLayerBsIndex;          // layer index of  bit stream for SFrameBsIfo
 // bool            bAnnexBFlag;            // annexeb flag, to figure it pOut the packetization mode whether need 4 bytes (0 0 0 1) of start code prefix
 } SWelsEncoderOutput;
 
--- a/codec/encoder/core/inc/slice_multi_threading.h
+++ b/codec/encoder/core/inc/slice_multi_threading.h
@@ -80,8 +80,8 @@
 int32_t CreateSliceThreads (sWelsEncCtx* pCtx);
 
 int32_t FiredSliceThreads (sWelsEncCtx* pCtx, SSliceThreadPrivateData* pPriData, WELS_EVENT* pEventsList,
-                           WELS_EVENT* pMasterEventsList, SLayerBSInfo* pLayerBsInfo,
-                           const uint32_t kuiNumThreads/*, int32_t *iLayerNum*/, SSliceCtx* pSliceCtx, const bool kbIsDynamicSlicingMode);
+                           WELS_EVENT* pMasterEventsList, SFrameBSInfo* pFrameBsInfo,
+                           const uint32_t kuiNumThreads, SSliceCtx* pSliceCtx, const bool kbIsDynamicSlicingMode);
 
 int32_t DynamicDetectCpuCores();
 
--- a/codec/encoder/core/src/encoder.cpp
+++ b/codec/encoder/core/src/encoder.cpp
@@ -233,6 +233,7 @@
   // for bitstream writing
   pEncCtx->iPosBsBuffer         = 0;    // reset bs pBuffer position
   pEncCtx->pOut->iNalIndex      = 0;    // reset NAL index
+  pEncCtx->pOut->iLayerBsIndex  = 0;    // reset index of Layer Bs
 
   InitBits (&pEncCtx->pOut->sBsWrite, pEncCtx->pOut->pBsBuffer, pEncCtx->pOut->uiSize);
 
--- a/codec/encoder/core/src/encoder_ext.cpp
+++ b/codec/encoder/core/src/encoder_ext.cpp
@@ -1812,8 +1812,9 @@
   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))
-  (*ppCtx)->pOut->iCountNals = iCountNals;
-  (*ppCtx)->pOut->iNalIndex = 0;
+  (*ppCtx)->pOut->iCountNals    = iCountNals;
+  (*ppCtx)->pOut->iNalIndex     = 0;
+  (*ppCtx)->pOut->iLayerBsIndex = 0;
 
 
   (*ppCtx)->pFrameBs = (uint8_t*)pMa->WelsMalloc (iTotalLength, "pFrameBs");
@@ -3520,8 +3521,10 @@
 
   //point to next pLayerBsInfo
   ++ pLayerBsInfo;
+  ++ pCtx->pOut->iLayerBsIndex;
   pLayerBsInfo->pBsBuf           = pCtx->pFrameBs + pCtx->iPosBsBuffer;
   pLayerBsInfo->pNalLengthInByte = (pLayerBsInfo - 1)->pNalLengthInByte + iCountNal;
+
   //update for external countings
   ++ iLayerNum;
   iFrameSize += iNonVclSize;
@@ -3557,6 +3560,7 @@
 
     //point to next pLayerBsInfo
     ++ pLayerBsInfo;
+    ++ pCtx->pOut->iLayerBsIndex;
     pLayerBsInfo->pBsBuf           = pCtx->pFrameBs + pCtx->iPosBsBuffer;
     pLayerBsInfo->pNalLengthInByte = (pLayerBsInfo - 1)->pNalLengthInByte + iCountNal;
     //update for external countings
@@ -3588,6 +3592,7 @@
 
     //point to next pLayerBsInfo
     ++ pLayerBsInfo;
+    ++ pCtx->pOut->iLayerBsIndex;
     pLayerBsInfo->pBsBuf           = pCtx->pFrameBs + pCtx->iPosBsBuffer;
     pLayerBsInfo->pNalLengthInByte = (pLayerBsInfo - 1)->pNalLengthInByte + iCountNal;
     //update for external countings
@@ -3636,6 +3641,7 @@
 
     //point to next pLayerBsInfo
     ++ pLayerBsInfo;
+    ++ pCtx->pOut->iLayerBsIndex;
     pLayerBsInfo->pBsBuf           = pCtx->pFrameBs + pCtx->iPosBsBuffer;
     pLayerBsInfo->pNalLengthInByte = (pLayerBsInfo - 1)->pNalLengthInByte + iCountNal;
     //update for external countings
@@ -3671,6 +3677,7 @@
 
     //point to next pLayerBsInfo
     ++ pLayerBsInfo;
+    ++ pCtx->pOut->iLayerBsIndex;
     pLayerBsInfo->pBsBuf           = pCtx->pFrameBs + pCtx->iPosBsBuffer;
     pLayerBsInfo->pNalLengthInByte = (pLayerBsInfo - 1)->pNalLengthInByte + iCountNal;
     //update for external countings
@@ -3692,8 +3699,9 @@
 void StackBackEncoderStatus (sWelsEncCtx* pEncCtx,
                              EVideoFrameType keFrameType) {
   // for bitstream writing
-  pEncCtx->iPosBsBuffer      = 0;   // reset bs pBuffer position
-  pEncCtx->pOut->iNalIndex   = 0;   // reset NAL index
+  pEncCtx->iPosBsBuffer        = 0;   // reset bs pBuffer position
+  pEncCtx->pOut->iNalIndex     = 0;   // reset NAL index
+  pEncCtx->pOut->iLayerBsIndex = 0;   // reset index of Layer Bs
 
   InitBits (&pEncCtx->pOut->sBsWrite, pEncCtx->pOut->pBsBuffer, pEncCtx->pOut->uiSize);
   if ((keFrameType == videoFrameTypeP) || (keFrameType == videoFrameTypeI)) {
@@ -4033,7 +4041,7 @@
           iRet = FiredSliceThreads (pCtx, &pCtx->pSliceThreading->pThreadPEncCtx[0],
                                     &pCtx->pSliceThreading->pReadySliceCodingEvent[0],
                                     &pCtx->pSliceThreading->pThreadMasterEvent[0],
-                                    pLayerBsInfo, iSliceCount, pCtx->pCurDqLayer->pSliceEncCtx, false);
+                                    pFbi, iSliceCount, pCtx->pCurDqLayer->pSliceEncCtx, false);
           if (iRet) {
             WelsLog (pLogCtx, WELS_LOG_ERROR,
                      "[MT] WelsEncoderEncodeExt(), FiredSliceThreads return(%d) failed and exit encoding frame, iCountThreadsNum= %d, iSliceCount= %d, uiSliceMode= %d, iMultipleThreadIdc= %d!!",
@@ -4073,7 +4081,7 @@
           iRet = FiredSliceThreads (pCtx, &pCtx->pSliceThreading->pThreadPEncCtx[0],
                                     &pCtx->pSliceThreading->pReadySliceCodingEvent[0],
                                     &pCtx->pSliceThreading->pThreadMasterEvent[0],
-                                    pLayerBsInfo, iNumThreadsRunning, pCtx->pCurDqLayer->pSliceEncCtx, false);
+                                    pFbi, iNumThreadsRunning, pCtx->pCurDqLayer->pSliceEncCtx, false);
           if (iRet) {
             WelsLog (pLogCtx, WELS_LOG_ERROR,
                      "[MT] WelsEncoderEncodeExt(), FiredSliceThreads return(%d) failed and exit encoding frame, iCountThreadsNum= %d, iSliceCount= %d, uiSliceMode= %d, iMultipleThreadIdc= %d!!",
@@ -4120,7 +4128,7 @@
         iRet = FiredSliceThreads (pCtx, &pCtx->pSliceThreading->pThreadPEncCtx[0],
                                   &pCtx->pSliceThreading->pReadySliceCodingEvent[0],
                                   &pCtx->pSliceThreading->pThreadMasterEvent[0],
-                                  pLayerBsInfo, kiPartitionCnt, pCtx->pCurDqLayer->pSliceEncCtx, true);
+                                  pFbi, kiPartitionCnt, pCtx->pCurDqLayer->pSliceEncCtx, true);
         if (iRet) {
           WelsLog (pLogCtx, WELS_LOG_ERROR,
                    "[MT] WelsEncoderEncodeExt(), FiredSliceThreads return(%d) failed and exit encoding frame, iCountThreadsNum= %d, iSliceCount= %d, uiSliceMode= %d, iMultipleThreadIdc= %d!!",
@@ -4332,6 +4340,7 @@
     iCountNal = pLayerBsInfo->iNalCount;
     ++ iLayerNum;
     ++ pLayerBsInfo;
+    ++ pCtx->pOut->iLayerBsIndex;
 
     pLayerBsInfo->pBsBuf = pCtx->pFrameBs + pCtx->iPosBsBuffer;
     pLayerBsInfo->pNalLengthInByte = (pLayerBsInfo - 1)->pNalLengthInByte + iCountNal;
@@ -4360,6 +4369,7 @@
       pLayerBsInfo->iNalCount           = 1;
       pLayerBsInfo->pNalLengthInByte[0] = iPaddingNalSize;
       ++ pLayerBsInfo;
+      ++ pCtx->pOut->iLayerBsIndex;
       pLayerBsInfo->pBsBuf           = pCtx->pFrameBs + pCtx->iPosBsBuffer;
       pLayerBsInfo->pNalLengthInByte = (pLayerBsInfo - 1)->pNalLengthInByte + 1;
       ++ iLayerNum;
@@ -4928,6 +4938,7 @@
   pCurLayer->pSliceEncCtx->iMaxSliceNumConstraint = iMaxSliceNum;
   return ENC_RETURN_SUCCESS;
 }
+
 int32_t WelsCodeOnePicPartition (sWelsEncCtx* pCtx,
                                  SFrameBSInfo* pFrameBSInfo,
                                  SLayerBSInfo* pLayerBsInfo,
--- a/codec/encoder/core/src/slice_multi_threading.cpp
+++ b/codec/encoder/core/src/slice_multi_threading.cpp
@@ -602,7 +602,7 @@
       while (iIdx < kiCountSlicesCoded) {
         pSliceBs = &pCtx->pSliceBs[iSliceIdx];
         if (pSliceBs != NULL && pSliceBs->uiBsPos > 0) {
-          if (iPartitionIdx > 0) {
+          if (iSliceIdx > 0) {
             int32_t iNalIdx = 0;
             const int32_t iCountNal = pSliceBs->iNalIndex;
 
@@ -706,7 +706,10 @@
                &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
-      SLayerBSInfo* pLbi = pPrivateData->pLayerBs;
+      int             iLayerIndex  = pEncPEncCtx->pOut->iLayerBsIndex;
+      SFrameBSInfo*   pFrameBsInfo = pPrivateData->pFrameBsInfo;
+      SLayerBSInfo*   pLbi = &pFrameBsInfo->sLayerInfo [iLayerIndex];
+
       const int32_t kiCurDid            = pEncPEncCtx->uiDependencyId;
       SWelsSvcCodingParam* pCodingParam = pEncPEncCtx->pSvcParam;
       SSpatialLayerConfig* pParamD      = &pCodingParam->sSpatialLayers[kiCurDid];
@@ -868,7 +871,7 @@
 
           WelsUnloadNalForSlice (pSliceBs);
 
-          if (0 == kiPartitionId) {
+          if (0 == iSliceIdx) {
             iReturn = WriteSliceBs (pEncPEncCtx, pLbi->pBsBuf,
                                            &pLbi->pNalLengthInByte[pLbi->iNalCount],
                                            pEncPEncCtx->iFrameBsSize - pEncPEncCtx->iPosBsBuffer,
@@ -969,16 +972,18 @@
 }
 
 int32_t FiredSliceThreads (sWelsEncCtx* pCtx, SSliceThreadPrivateData* pPriData, WELS_EVENT* pEventsList,
-                           WELS_EVENT* pMasterEventsList, SLayerBSInfo* pLbi,
+                           WELS_EVENT* pMasterEventsList, SFrameBSInfo* pFrameBsInfo,
                            const uint32_t uiNumThreads, SSliceCtx* pSliceCtx, const bool bIsDynamicSlicingMode) {
-  int32_t iEndMbIdx     = 0;
-  int32_t iIdx          = 0;
-  const int32_t kiEventCnt = uiNumThreads;
+  int32_t iEndMbIdx         = 0;
+  int32_t iIdx              = 0;
+  const int32_t kiEventCnt  = uiNumThreads;
+  int32_t       iLayerBsIdx = pCtx->pOut->iLayerBsIndex;
+  SLayerBSInfo* pLbi = &pFrameBsInfo->sLayerInfo[iLayerBsIdx];
 
-  if (pPriData == NULL || pLbi == NULL || kiEventCnt <= 0 || pEventsList == NULL) {
+  if (pPriData == NULL || pFrameBsInfo == NULL || pLbi == NULL || kiEventCnt <= 0 || pEventsList == NULL) {
     WelsLog (& (pCtx->sLogCtx), WELS_LOG_ERROR,
-             "FiredSliceThreads(), fail due pPriData == %p || pLbi == %p || iEventCnt(%d) <= 0 || pEventsList == %p!!",
-             (void*)pPriData, (void*)pLbi, uiNumThreads, (void*)pEventsList);
+             "FiredSliceThreads(), fail due pPriData == %p ||pFrameBsInfo == %p || pLbi == %p || iEventCnt(%d) <= 0 || pEventsList == %p!!",
+             (void*)pPriData, (void*)pFrameBsInfo, (void*)pLbi, uiNumThreads, (void*)pEventsList);
     return 1;
   }
 
@@ -1003,8 +1008,8 @@
 
   iIdx = 0;
   while (iIdx < kiEventCnt) {
-    pPriData[iIdx].pLayerBs = pLbi;
-    pPriData[iIdx].iSliceIndex = iIdx;
+	pPriData[iIdx].pFrameBsInfo = pFrameBsInfo;
+    pPriData[iIdx].iSliceIndex  = iIdx;
     SetOneSliceBsBufferUnderMultithread (pCtx, iIdx, iIdx);
     if (pEventsList[iIdx])
       WelsEventSignal (&pEventsList[iIdx]);