shithub: openh264

Download patch

ref: 597b4eef73c3032175fded97504a8db6ccd33b6f
parent: 2754129064435a0542694503f0e8d517eea3d910
author: Karina <ruil2@cisco.com>
date: Tue Feb 16 11:37:14 EST 2016

fix timestamp = 0 issue when rc mode is BITRATE mode.

--- a/codec/encoder/core/inc/encoder_context.h
+++ b/codec/encoder/core/inc/encoder_context.h
@@ -235,7 +235,7 @@
   bool bDependencyRecFlag[MAX_DEPENDENCY_LAYER];
   bool bRecFlag;
 #endif
-
+  int64_t            uiLastTimestamp;
   uint32_t GetNeededSpsNum() {
     if (0 == sPSOVector.uiNeededSpsNum) {
       sPSOVector.uiNeededSpsNum = ((SPS_LISTING & pSvcParam->eSpsPpsIdStrategy) ? (MAX_SPS_COUNT) : (1));
--- a/codec/encoder/core/inc/extern.h
+++ b/codec/encoder/core/inc/extern.h
@@ -93,7 +93,7 @@
  * \param   kpSrcPic    Source picture
  * \return  EFrameType (videoFrameTypeIDR/videoFrameTypeI/videoFrameTypeP)
  */
-int32_t WelsEncoderEncodeExt (sWelsEncCtx*, SFrameBSInfo* pFbi, const SSourcePicture* kpSrcPic);
+int32_t WelsEncoderEncodeExt (sWelsEncCtx*, SFrameBSInfo* pFbi, const SSourcePicture* kpSrcPic,int64_t uiTimeStamp);
 
 int32_t WelsEncoderEncodeParameterSets (sWelsEncCtx* pCtx, void* pDst);
 
--- a/codec/encoder/core/src/encoder_ext.cpp
+++ b/codec/encoder/core/src/encoder_ext.cpp
@@ -2582,7 +2582,7 @@
 #endif//MEMORY_MONITOR
 
   pCtx->iStatisticsLogInterval = STATISTICS_LOG_INTERVAL_MS;
-
+  pCtx->uiLastTimestamp = -1;
   *ppCtx = pCtx;
 
   WelsLog (pLogCtx, WELS_LOG_DEBUG, "WelsInitEncoderExt(), pCtx= 0x%p.", (void*)pCtx);
@@ -3716,7 +3716,7 @@
  * \pParam  pSrcPic         Source Picture
  * \return  EFrameType (videoFrameTypeIDR/videoFrameTypeI/videoFrameTypeP)
  */
-int32_t WelsEncoderEncodeExt (sWelsEncCtx* pCtx, SFrameBSInfo* pFbi, const SSourcePicture* pSrcPic) {
+int32_t WelsEncoderEncodeExt (sWelsEncCtx* pCtx, SFrameBSInfo* pFbi, const SSourcePicture* pSrcPic,int64_t uiTimeStamp ) {
   if (pCtx == NULL) {
     return ENC_RETURN_MEMALLOCERR;
   }
@@ -3756,7 +3756,7 @@
   pCtx->iEncoderError = ENC_RETURN_SUCCESS;
   pCtx->bCurFrameMarkedAsSceneLtr = false;
   pFbi->iLayerNum = 0; // for initialization
-  pFbi->uiTimeStamp = pSrcPic->uiTimeStamp;
+  pFbi->uiTimeStamp = uiTimeStamp;
   for (int32_t iNalIdx = 0; iNalIdx < MAX_LAYER_NUM_OF_FRAME; iNalIdx++) {
     pFbi->sLayerInfo[iNalIdx].eFrameType = videoFrameTypeSkip;
   }
@@ -3763,7 +3763,7 @@
   // perform csc/denoise/downsample/padding, generate spatial layers
   iSpatialNum = pCtx->pVpp->BuildSpatialPicList (pCtx, pSrcPic);
   if (pCtx->pFuncList->pfRc.pfWelsUpdateMaxBrWindowStatus) {
-    pCtx->pFuncList->pfRc.pfWelsUpdateMaxBrWindowStatus (pCtx, iSpatialNum, pSrcPic->uiTimeStamp);
+    pCtx->pFuncList->pfRc.pfWelsUpdateMaxBrWindowStatus (pCtx, iSpatialNum,uiTimeStamp);
   }
 
   if (iSpatialNum < 1) {
@@ -3809,7 +3809,7 @@
       pLayerBsInfo->eFrameType = eFrameType;
       WelsLog (& (pCtx->sLogCtx), WELS_LOG_DEBUG,
                "[Rc] Frame timestamp = %lld, skip one frame due to target_br, continual skipped %d frames",
-               pSrcPic->uiTimeStamp, pCtx->iContinualSkipFrames);
+               uiTimeStamp, pCtx->iContinualSkipFrames);
       ++ iSpatialIdx;
       if (pSvcParam->bSimulcastAVC) {
         if (pCtx->pFuncList->pfRc.pfWelsUpdateBufferWhenSkip)
@@ -3830,13 +3830,13 @@
     //loop each layer to check if have skip frame when RC and frame skip enable
     if (pCtx->pFuncList->pfRc.pfWelsCheckSkipBasedMaxbr) {
       bool bSkip = pCtx->pFuncList->pfRc.pfWelsCheckSkipBasedMaxbr (pCtx, iSpatialNum, eFrameType,
-                   (uint32_t)pSrcPic->uiTimeStamp);
+                   (uint32_t)uiTimeStamp);
       if (bSkip) {
         eFrameType = videoFrameTypeSkip;
         pLayerBsInfo->eFrameType = videoFrameTypeSkip;
         WelsLog (& (pCtx->sLogCtx), WELS_LOG_DEBUG,
                  "[Rc] Frame timestamp = %lld, skip one frame due to max_br, continual skipped %d frames",
-                 pSrcPic->uiTimeStamp, pCtx->iContinualSkipFrames);
+                 uiTimeStamp, pCtx->iContinualSkipFrames);
         ++ iSpatialIdx;
         if (pSvcParam->bSimulcastAVC)
           continue;
@@ -3968,7 +3968,7 @@
                          eFrameType); //get reordering syntax used for writing slice header and transmit to encoder.
     PrefetchReferencePicture (pCtx, eFrameType); // update reference picture for current pDq layer
 
-    pCtx->pFuncList->pfRc.pfWelsRcPictureInit (pCtx, pSrcPic->uiTimeStamp);
+    pCtx->pFuncList->pfRc.pfWelsRcPictureInit (pCtx, uiTimeStamp);
     PreprocessSliceCoding (pCtx); // MUST be called after pfWelsRcPictureInit() and WelsInitCurrentLayer()
 
     //TODO Complexity Calculation here for screen content
@@ -4166,7 +4166,7 @@
     }
 
     if (NULL != pCtx->pFuncList->pfRc.pfWelsRcPostFrameSkipping
-        && pCtx->pFuncList->pfRc.pfWelsRcPostFrameSkipping (pCtx, iCurDid, pSrcPic->uiTimeStamp)) {
+        && pCtx->pFuncList->pfRc.pfWelsRcPostFrameSkipping (pCtx, iCurDid,uiTimeStamp)) {
 
       StackBackEncoderStatus (pCtx, eFrameType);
       ClearFrameBsInfo (pCtx, pFbi);
@@ -4182,7 +4182,7 @@
       WelsRcPostFrameSkippedUpdate (pCtx, iCurDid);
       WelsLog (& (pCtx->sLogCtx), WELS_LOG_INFO,
                "[Rc] Frame timestamp = %lld, skip one frame due to post skip, continual skipped %d frames",
-               pSrcPic->uiTimeStamp, pCtx->iContinualSkipFrames);
+               uiTimeStamp, pCtx->iContinualSkipFrames);
       pCtx->iEncoderError = ENC_RETURN_SUCCESS;
       return ENC_RETURN_SUCCESS;
     }
@@ -4200,7 +4200,7 @@
     }
 
     pCtx->pFuncList->pfRc.pfWelsRcPictureInfoUpdate (pCtx, iLayerSize);
-    RcTraceFrameBits (pCtx, pSrcPic->uiTimeStamp);
+    RcTraceFrameBits (pCtx,uiTimeStamp);
     pCtx->pDecPic->iFrameAverageQp = pCtx->pWelsSvcRc[iDidIdx].iAverageFrameQp;
 
     //update scc related
--- a/codec/encoder/plus/src/welsEncoderExt.cpp
+++ b/codec/encoder/plus/src/welsEncoderExt.cpp
@@ -402,7 +402,14 @@
 
 int CWelsH264SVCEncoder ::EncodeFrameInternal (const SSourcePicture*  pSrcPic, SFrameBSInfo* pBsInfo) {
   const int64_t kiBeforeFrameUs = WelsTime();
-  const int32_t kiEncoderReturn = WelsEncoderEncodeExt (m_pEncContext, pBsInfo, pSrcPic);
+  int64_t uiTimeStamp = pSrcPic->uiTimeStamp;
+  if ((m_pEncContext->uiLastTimestamp == uiTimeStamp) || ((uiTimeStamp == 0) && (m_pEncContext->uiLastTimestamp != -1))) {
+    uiTimeStamp = m_pEncContext->uiLastTimestamp + (int32_t) (1000.0 /
+                  m_pEncContext->pSvcParam->sSpatialLayers->fFrameRate);
+  }
+  m_pEncContext->uiLastTimestamp = uiTimeStamp;
+
+  const int32_t kiEncoderReturn = WelsEncoderEncodeExt (m_pEncContext, pBsInfo, pSrcPic, uiTimeStamp);
   const int64_t kiCurrentFrameMs = (WelsTime() - kiBeforeFrameUs) / 1000;
 
   if ((kiEncoderReturn == ENC_RETURN_MEMALLOCERR) || (kiEncoderReturn == ENC_RETURN_MEMOVERFLOWFOUND)
@@ -415,7 +422,7 @@
     return cmUnknownReason;
   }
 
-  UpdateStatistics (pSrcPic->uiTimeStamp, pBsInfo, kiCurrentFrameMs);
+  UpdateStatistics (uiTimeStamp, pBsInfo, kiCurrentFrameMs);
 
   ///////////////////for test
 #ifdef OUTPUT_BIT_STREAM
@@ -582,7 +589,7 @@
         if ((pLayerInfo->uiLayerType == VIDEO_CODING_LAYER) && (pLayerInfo->uiSpatialId == iDid)) {
           eFrameType = pLayerInfo->eFrameType;
           for (int32_t iNalIdx = 0; iNalIdx < pLayerInfo->iNalCount; iNalIdx++) {
-             kiCurrentFrameSize += pLayerInfo->pNalLengthInByte[iNalIdx];
+            kiCurrentFrameSize += pLayerInfo->pNalLengthInByte[iNalIdx];
           }
         }
       }