shithub: openh264

Download patch

ref: f77d19042dbaad1426cbe13876fe430c334c9929
parent: d1e25d748fd5f7d9c2c5dba93c0ba3d7d1cb73ad
author: Huade Shi (huashi) <[email protected]>
date: Tue Mar 28 11:03:57 EDT 2017

SliceBufferReallocate:RBC#1797:New slice buffer reallocate design and update UT cases

--- a/codec/encoder/core/src/encoder_ext.cpp
+++ b/codec/encoder/core/src/encoder_ext.cpp
@@ -3716,14 +3716,6 @@
         pLayerBsInfo->eFrameType    = eFrameType;
         pLayerBsInfo->iSubSeqId = GetSubSequenceId (pCtx, eFrameType);
 
-        int32_t iRet = InitAllSlicesInThread(pCtx);
-        if (iRet) {
-          WelsLog (pLogCtx, WELS_LOG_ERROR,
-                "WelsEncoderEncodeExt(), multi-slice (mode %d) InitAllSlicesInThread() error!",
-                   pParam->sSliceArgument.uiSliceMode);
-          return ENC_RETURN_UNEXPECTED;
-        }
-
         pCtx->pTaskManage->ExecuteTasks();
         if (pCtx->iEncoderError) {
           WelsLog (pLogCtx, WELS_LOG_ERROR,
@@ -3730,14 +3722,6 @@
                    "WelsEncoderEncodeExt(), multi-slice (mode %d) encoding error!",
                    pParam->sSliceArgument.uiSliceMode);
           return pCtx->iEncoderError;
-        }
-
-        iRet = SliceLayerInfoUpdate (pCtx, pFbi, pLayerBsInfo, pParam->sSliceArgument.uiSliceMode);
-        if (iRet) {
-          WelsLog (pLogCtx, WELS_LOG_ERROR,
-                   "WelsEncoderEncodeExt(), multi-slice (mode %d) SliceLayerInfoUpdate() error!",
-                   pParam->sSliceArgument.uiSliceMode);
-          return ENC_RETURN_UNEXPECTED;
         }
 
         iLayerSize = AppendSliceToFrameBs (pCtx, pLayerBsInfo, iSliceCount);
--- a/codec/encoder/core/src/svc_encode_slice.cpp
+++ b/codec/encoder/core/src/svc_encode_slice.cpp
@@ -996,11 +996,14 @@
                               const int32_t kiThreadIdx,
                               const int32_t kiDlayerIdx,
                               const int32_t kiSliceIdx) {
-  const int32_t kiCodedNumInThread = pCtx->pCurDqLayer->sSliceThreadInfo[kiThreadIdx].iCodedSliceNum;
 
-  assert(kiCodedNumInThread <= pCtx->pCurDqLayer->sSliceThreadInfo[kiThreadIdx].iMaxSliceNum -1 );
-
-  pSlice = &pCtx->pCurDqLayer->sSliceThreadInfo [kiThreadIdx].pSliceInThread[kiCodedNumInThread];
+  if (pCtx->pCurDqLayer->bThreadSlcBufferFlag) {
+      const int32_t kiCodedNumInThread = pCtx->pCurDqLayer->sSliceThreadInfo[kiThreadIdx].iCodedSliceNum;
+      assert(kiCodedNumInThread <= pCtx->pCurDqLayer->sSliceThreadInfo[kiThreadIdx].iMaxSliceNum -1 );
+      pSlice = &pCtx->pCurDqLayer->sSliceThreadInfo [kiThreadIdx].pSliceInThread[kiCodedNumInThread];
+  } else {
+      pSlice = &pCtx->pCurDqLayer->sSliceThreadInfo [0].pSliceInThread[kiSliceIdx];
+  }
   pSlice->iSliceIdx   = kiSliceIdx;
   pSlice->uiThreadIdx = kiThreadIdx;
 
@@ -1018,18 +1021,22 @@
                              CMemoryAlign* pMa) {
   int32_t iThreadNum      = pCtx->pSvcParam->iMultipleThreadIdc;
   int32_t iMaxSliceNum    = 0;
+  int32_t iSlcBufferNum   = 0;
   int32_t iIdx            = 0;
   int32_t iRet            = 0;
-  SliceModeEnum eSlicMode = pCtx->pSvcParam->sSpatialLayers[kiDlayerIndex].sSliceArgument.uiSliceMode;
 
   assert (iThreadNum > 0);
-  if( SM_SIZELIMITED_SLICE == eSlicMode && iThreadNum > 1) {
-     iMaxSliceNum = pDqLayer->iMaxSliceNum / iThreadNum + 1;
+
+  //for fixed slice num case, no need to reallocate, so one slice buffer for all thread
+  if( pDqLayer->bThreadSlcBufferFlag) {
+    iMaxSliceNum  = pDqLayer->iMaxSliceNum / iThreadNum + 1;
+    iSlcBufferNum = iThreadNum;
   } else {
-    iMaxSliceNum = pDqLayer->iMaxSliceNum;
+    iMaxSliceNum  = pDqLayer->iMaxSliceNum;
+    iSlcBufferNum = 1;
   }
 
-  while (iIdx < iThreadNum) {
+  while (iIdx < iSlcBufferNum) {
     pDqLayer->sSliceThreadInfo[iIdx].iMaxSliceNum   = iMaxSliceNum;
     pDqLayer->sSliceThreadInfo[iIdx].iCodedSliceNum = 0;
     pDqLayer->sSliceThreadInfo[iIdx].pSliceInThread = (SSlice*)pMa->WelsMallocz (sizeof (SSlice) * iMaxSliceNum, "pSliceInThread");
@@ -1055,6 +1062,7 @@
     pDqLayer->sSliceThreadInfo[iIdx].iCodedSliceNum = 0;
     pDqLayer->sSliceThreadInfo[iIdx].pSliceInThread = NULL;
   }
+
   return ENC_RETURN_SUCCESS;
 }
 
@@ -1108,7 +1116,7 @@
     return ENC_RETURN_MEMALLOCERR;
   }
 
-  iRet = InitSliceBoundaryInfo (pDqLayer, &pCtx->pSvcParam->sSpatialLayers[kiDlayerIndex].sSliceArgument, iMaxSliceNum);
+  iRet = InitSliceBoundaryInfo (pDqLayer, pSliceArgument, iMaxSliceNum);
   if (ENC_RETURN_SUCCESS != iRet) {
     return iRet;
   }
--- a/codec/encoder/core/src/wels_task_encoder.cpp
+++ b/codec/encoder/core/src/wels_task_encoder.cpp
@@ -192,7 +192,7 @@
 #if MT_DEBUG_BS_WR
   m_pSliceBs->bSliceCodedFlag = true;
 #endif//MT_DEBUG_BS_WR
-    m_pCtx->pCurDqLayer->sSliceThreadInfo[m_iThreadIdx].iCodedSliceNum ++;
+
   return ENC_RETURN_SUCCESS;
 }
 
--- a/test/encoder/EncUT_SliceBufferReallocate.cpp
+++ b/test/encoder/EncUT_SliceBufferReallocate.cpp
@@ -206,7 +206,9 @@
     SWelsFuncPtrList sEncFunctionList;
     pCtx->pFuncList = &sEncFunctionList;
 
+    //always multi thread cases
     pCtx->pSvcParam->iMultipleThreadIdc = (rand() % MAX_THREADS_NUM) + 1;
+    pCtx->pSvcParam->iMultipleThreadIdc = (pCtx->pSvcParam->iMultipleThreadIdc <= 1 )? 2 : pCtx->pSvcParam->iMultipleThreadIdc;
     pCtx->iActiveThreadsNum = pCtx->pSvcParam->iMultipleThreadIdc;
     pCtx->pSvcParam->iSpatialLayerNum = 1;
     pCtx->pSvcParam->bSimulcastAVC = (bool)rand() % 2;
@@ -366,13 +368,17 @@
 }
 
 void CSliceBufferReallocatTest::SimulateEncodedOneSlice(const int32_t kiSlcIdx, const int32_t kiThreadIdx) {
-    int32_t iCodedSlcNumInThrd = m_EncContext.pCurDqLayer->sSliceThreadInfo[kiThreadIdx].iCodedSliceNum;
+    if(m_EncContext.pCurDqLayer->bThreadSlcBufferFlag) {
+        int32_t iCodedSlcNumInThrd = m_EncContext.pCurDqLayer->sSliceThreadInfo[kiThreadIdx].iCodedSliceNum;
 
-    EXPECT_TRUE(NULL != m_EncContext.pCurDqLayer->sSliceThreadInfo[kiThreadIdx].pSliceInThread);
-    EXPECT_TRUE(NULL != &m_EncContext.pCurDqLayer->sSliceThreadInfo[kiThreadIdx].pSliceInThread[iCodedSlcNumInThrd]);
+        EXPECT_TRUE(NULL != m_EncContext.pCurDqLayer->sSliceThreadInfo[kiThreadIdx].pSliceInThread);
+        EXPECT_TRUE(NULL != &m_EncContext.pCurDqLayer->sSliceThreadInfo[kiThreadIdx].pSliceInThread[iCodedSlcNumInThrd]);
 
-    m_EncContext.pCurDqLayer->sSliceThreadInfo[kiThreadIdx].pSliceInThread[iCodedSlcNumInThrd].iSliceIdx = kiSlcIdx;
-    m_EncContext.pCurDqLayer->sSliceThreadInfo[kiThreadIdx].iCodedSliceNum ++;
+        m_EncContext.pCurDqLayer->sSliceThreadInfo[kiThreadIdx].pSliceInThread[iCodedSlcNumInThrd].iSliceIdx = kiSlcIdx;
+        m_EncContext.pCurDqLayer->sSliceThreadInfo[kiThreadIdx].iCodedSliceNum ++;
+    } else {
+        m_EncContext.pCurDqLayer->sSliceThreadInfo[0].pSliceInThread[kiSlcIdx].iSliceIdx = kiSlcIdx;
+    }
 }
 
 void CSliceBufferReallocatTest::SimulateSliceInOnePartition(const int32_t kiPartNum,
@@ -497,7 +503,7 @@
     int32_t iMaxSliceNumNew = 0;
     int32_t iSlcBuffNumInThrd = 0;
 
-    InitParamForTestCase(iLayerIdx);
+    InitParamForSizeLimitSlcModeCase(iLayerIdx);
     pCtx->pCurDqLayer = pCtx->ppDqLayerList[iLayerIdx];
     iRet = InitAllSlicesInThread(pCtx);
     EXPECT_TRUE(cmResultSuccess == iRet);
@@ -560,7 +566,7 @@
     sWelsEncCtx* pCtx = &m_EncContext;
     SSliceArgument* pSliceArgument = &pCtx->pSvcParam->sSpatialLayers[iLayerIdx].sSliceArgument;
 
-    InitParamForTestCase(iLayerIdx);
+    InitParamForSizeLimitSlcModeCase(iLayerIdx);
 
     pCtx->pCurDqLayer = pCtx->ppDqLayerList[iLayerIdx];
     int32_t iRet = InitAllSlicesInThread(pCtx);
@@ -591,7 +597,7 @@
     SFrameBSInfo FrameBsInfo;
     SLayerBSInfo* pLayerBsInfo = NULL;
 
-    InitParamForTestCase(iLayerIdx);
+    InitParamForSizeLimitSlcModeCase(iLayerIdx);
     pCtx->pCurDqLayer = pCtx->ppDqLayerList[iLayerIdx];
     iRet = InitAllSlicesInThread(pCtx);
     EXPECT_TRUE(cmResultSuccess == iRet);