shithub: openh264

Download patch

ref: 1d4816658c35b0e9548e8e589364022f23d7a720
parent: 61b9ddce8e2e8437babffe6e838b2acc873dc994
author: Karina <[email protected]>
date: Fri Nov 25 05:58:44 EST 2016

fix issues with dynamic slice mode and cabac

--- a/codec/encoder/core/inc/encoder_context.h
+++ b/codec/encoder/core/inc/encoder_context.h
@@ -237,7 +237,7 @@
   bool bDependencyRecFlag[MAX_DEPENDENCY_LAYER];
 #endif
   int64_t            uiLastTimestamp;
-
+  uint8_t*           pDynamicBsBuffer[MAX_THREADS_NUM];
 } sWelsEncCtx/*, *PWelsEncCtx*/;
 }
 #endif//sWelsEncCtx_H__
--- a/codec/encoder/core/inc/svc_enc_slice_segment.h
+++ b/codec/encoder/core/inc/svc_enc_slice_segment.h
@@ -102,6 +102,7 @@
 SCabacCtx       sStoredCabac;
 int32_t         iMbSkipRunStack;
 uint8_t         uiLastMbQp;
+uint8_t*        pRestoreBuffer;
 } SDynamicSlicingStack;
 
 /*!
--- a/codec/encoder/core/src/encoder_ext.cpp
+++ b/codec/encoder/core/src/encoder_ext.cpp
@@ -1748,6 +1748,13 @@
   (*ppCtx)->iFrameBsSize = iTotalLength;
   (*ppCtx)->iPosBsBuffer = 0;
 
+  // for dynamic slice mode&& CABAC,allocate slice buffer to restore slice data
+  if (bDynamicSlice && pParam->iEntropyCodingModeFlag) {
+    for (int32_t iIdx = 0; iIdx < MAX_THREADS_NUM; iIdx++) {
+      (*ppCtx)->pDynamicBsBuffer[iIdx] = (uint8_t*)pMa->WelsMalloc (iMaxSliceBufferSize, "DynamicSliceBs");
+      WELS_VERIFY_RETURN_PROC_IF (1, (NULL == (*ppCtx)->pDynamicBsBuffer[iIdx]), FreeMemorySvc (ppCtx))
+    }
+  }
   // for pSlice bs buffers
   if (pParam->iMultipleThreadIdc > 1
       && RequestMtResource (ppCtx, pParam, iCountBsLen, iMaxSliceBufferSize, bDynamicSlice)) {
@@ -1957,7 +1964,11 @@
       pMa->WelsFree (pCtx->pFrameBs, "pFrameBs");
       pCtx->pFrameBs = NULL;
     }
+    for (int32_t iIdx = 0; iIdx < MAX_THREADS_NUM; iIdx++) {
+      pMa->WelsFree (pCtx->pDynamicBsBuffer[iIdx], "DynamicSliceBs");
+      pCtx->pDynamicBsBuffer[iIdx] = NULL;
 
+    }
     // pSpsArray
     if (NULL != pCtx->pSpsArray) {
       pMa->WelsFree (pCtx->pSpsArray, "pSpsArray");
--- a/codec/encoder/core/src/set_mb_syn_cavlc.cpp
+++ b/codec/encoder/core/src/set_mb_syn_cavlc.cpp
@@ -250,6 +250,10 @@
 void StashMBStatusCabac (SDynamicSlicingStack* pDss, SSlice* pSlice, int32_t iMbSkipRun) {
   SCabacCtx* pCtx = &pSlice->sCabacCtx;
   memcpy (&pDss->sStoredCabac, pCtx, sizeof (SCabacCtx));
+  if (pDss->pRestoreBuffer) {
+    int32_t iLen = GetBsPosCabac (pSlice) - pDss->iStartPos;
+    memcpy (pDss->pRestoreBuffer, pCtx->m_pBufStart, iLen);
+  }
   pDss->uiLastMbQp =  pSlice->uiLastMbQp;
   pDss->iMbSkipRunStack = iMbSkipRun;
 }
@@ -256,14 +260,20 @@
 int32_t StashPopMBStatusCabac (SDynamicSlicingStack* pDss, SSlice* pSlice) {
   SCabacCtx* pCtx = &pSlice->sCabacCtx;
   memcpy (pCtx, &pDss->sStoredCabac, sizeof (SCabacCtx));
+
+  if (pDss->pRestoreBuffer) {
+    int32_t iLen = GetBsPosCabac (pSlice) - pDss->iStartPos;
+    memcpy (pCtx->m_pBufStart, pDss->pRestoreBuffer, iLen);
+  }
   pSlice->uiLastMbQp = pDss->uiLastMbQp;
   return pDss->iMbSkipRunStack;
 }
-int32_t GetBsPosCavlc(SSlice *pSlice){
+int32_t GetBsPosCavlc (SSlice* pSlice) {
   return BsGetBitsPos (pSlice->pSliceBsa);
 }
-int32_t GetBsPosCabac(SSlice *pSlice){
-  return (int32_t) ((pSlice->sCabacCtx.m_pBufCur - pSlice->sCabacCtx.m_pBufStart) << 3) + (pSlice->sCabacCtx.m_iLowBitCnt - 9);
+int32_t GetBsPosCabac (SSlice* pSlice) {
+  return (int32_t) ((pSlice->sCabacCtx.m_pBufCur - pSlice->sCabacCtx.m_pBufStart) << 3) +
+         (pSlice->sCabacCtx.m_iLowBitCnt - 9);
 }
 void WelsWriteSliceEndSyn (SSlice* pSlice, bool bEntropyCodingModeFlag) {
   SBitStringAux* pBs = pSlice->pSliceBsa;
--- a/codec/encoder/core/src/svc_encode_slice.cpp
+++ b/codec/encoder/core/src/svc_encode_slice.cpp
@@ -538,6 +538,8 @@
   SDynamicSlicingStack sDss;
   if (pEncCtx->pSvcParam->iEntropyCodingModeFlag) {
     WelsInitSliceCabac (pEncCtx, pSlice);
+    sDss.pRestoreBuffer = NULL;
+    sDss.iStartPos = sDss.iCurrentPos = 0;
   }
   for (; ;) {
     pEncCtx->pFuncList->pfStashMBStatus (&sDss, pSlice, 0);
@@ -602,8 +604,9 @@
   SDynamicSlicingStack sDss;
   if (pEncCtx->pSvcParam->iEntropyCodingModeFlag) {
     WelsInitSliceCabac (pEncCtx, pSlice);
-    sDss.iStartPos = 0;
-  }else {
+    sDss.pRestoreBuffer = pEncCtx->pDynamicBsBuffer[kiPartitionId];
+    sDss.iStartPos = sDss.iCurrentPos = 0;
+  } else {
     sDss.iStartPos = BsGetBitsPos (pBs);
   }
   for (; ;) {
@@ -912,7 +915,8 @@
   SNalUnitHeaderExt* pNalHeadExt        = &pCurLayer->sLayerInfo.sNalHeaderExt;
   SSlice* pCurSlice                     = &pCurLayer->sLayerInfo.pSliceInLayer[kiSliceIdx];
   SBitStringAux* pBs                    = pCurSlice->pSliceBsa;
-  const int32_t kiDynamicSliceFlag      = (pEncCtx->pSvcParam->sSpatialLayers[pEncCtx->uiDependencyId].sSliceArgument.uiSliceMode
+  const int32_t kiDynamicSliceFlag      =
+    (pEncCtx->pSvcParam->sSpatialLayers[pEncCtx->uiDependencyId].sSliceArgument.uiSliceMode
      ==
      SM_SIZELIMITED_SLICE);
 
@@ -957,7 +961,7 @@
   SMB* pMb = &pMbList[iIdx];
 
   do {
-    UpdateMbNeighbor(pCurDq, pMb, kiMbWidth, WelsMbToSliceIdc (pCurDq, pMb->iMbXY));
+    UpdateMbNeighbor (pCurDq, pMb, kiMbWidth, WelsMbToSliceIdc (pCurDq, pMb->iMbXY));
     ++ pMb;
     ++ iIdx;
   } while ((iIdx < kiEndMbNeedUpdate) &&
@@ -1107,6 +1111,8 @@
   SDynamicSlicingStack sDss;
   if (pEncCtx->pSvcParam->iEntropyCodingModeFlag) {
     WelsInitSliceCabac (pEncCtx, pSlice);
+    sDss.pRestoreBuffer = NULL;
+    sDss.iStartPos = sDss.iCurrentPos = 0;
   }
   pSlice->iMbSkipRun = 0;
   for (;;) {
@@ -1200,8 +1206,9 @@
   SDynamicSlicingStack sDss;
   if (pEncCtx->pSvcParam->iEntropyCodingModeFlag) {
     WelsInitSliceCabac (pEncCtx, pSlice);
-    sDss.iStartPos = 0;
-  }else {
+    sDss.iStartPos = sDss.iCurrentPos = 0;
+    sDss.pRestoreBuffer = pEncCtx->pDynamicBsBuffer[kiPartitionId];
+  } else {
     sDss.iStartPos = BsGetBitsPos (pBs);
   }
   pSlice->iMbSkipRun = 0;