shithub: openh264

Download patch

ref: 36cfb3bd32a287fc4abebe447fae9a5080f1100c
parent: 4cdee3b2d1f1ae34cb9c4f31baf890c0f0e941a2
author: ruil2 <[email protected]>
date: Mon Jul 14 09:57:03 EDT 2014

add buffer based rc

--- a/codec/api/svc/codec_app_def.h
+++ b/codec/api/svc/codec_app_def.h
@@ -198,6 +198,7 @@
   RC_QUALITY_MODE = 0,      //Quality mode
   RC_BITRATE_MODE = 1,   //Bitrate mode
   RC_LOW_BW_MODE = 2, //bitrate limited mode
+  RC_BUFFERBASED_MODE = 3,//no bitrate control,only using buffer status,adjust the video quality
   RC_OFF_MODE = -1,    // rate control off mode
 } RC_MODES;
 
--- a/codec/encoder/core/inc/rc.h
+++ b/codec/encoder/core/inc/rc.h
@@ -52,8 +52,6 @@
 //trace
 #define GOM_TRACE_FLAG 0
 #define GOM_H_SCC               8
-#define    WELS_RC_DISABLE        0
-#define    WELS_RC_GOM            1
 
 enum {
 BITS_NORMAL,
@@ -71,6 +69,8 @@
 MAX_LOW_BR_QP			= 42,
 MIN_IDR_QP            = 26,
 MAX_IDR_QP            = 32,
+MIN_SCREEN_QP         = 26,
+MAX_SCREEN_QP         = 32,
 DELTA_QP              = 2,
 DELTA_QP_BGD_THD      = 3,
 
@@ -232,7 +232,7 @@
 PWelsRCMBInfoUpdateFunc			pfWelsRcMbInfoUpdate;
 } SWelsRcFunc;
  
-void WelsRcInitModule (void* pCtx,  int32_t iModule);
+void WelsRcInitModule (void* pCtx,RC_MODES iRcMode);
 void WelsRcFreeMemory (void* pCtx);
  
 }
--- a/codec/encoder/core/src/encoder_ext.cpp
+++ b/codec/encoder/core/src/encoder_ext.cpp
@@ -2042,7 +2042,7 @@
   if (pCodingParam->iMultipleThreadIdc > 1)
     iRet = CreateSliceThreads (pCtx);
 
-  WelsRcInitModule (pCtx,  pCtx->pSvcParam->iRCMode != RC_OFF_MODE ? WELS_RC_GOM : WELS_RC_DISABLE);
+  WelsRcInitModule (pCtx,  pCtx->pSvcParam->iRCMode);
 
   pCtx->pVpp = new CWelsPreProcess (pCtx);
   if (pCtx->pVpp == NULL) {
@@ -2948,17 +2948,19 @@
 bool CheckFrameSkipBasedMaxbr (sWelsEncCtx* pCtx, int32_t iSpatialNum) {
   SSpatialPicIndex* pSpatialIndexMap = &pCtx->sSpatialIndexMap[0];
   bool bSkipMustFlag = false;
-
-  if (RC_OFF_MODE != pCtx->pSvcParam->iRCMode && true == pCtx->pSvcParam->bEnableFrameSkip) {
-    for (int32_t i = 0; i < iSpatialNum; i++) {
-      if (0 == pCtx->pSvcParam->sSpatialLayers[i].iMaxSpatialBitrate) {
-        break;
-      }
-      pCtx->uiDependencyId = (uint8_t) (pSpatialIndexMap + i)->iDid;
-      pCtx->pFuncList->pfRc.pfWelsRcPicDelayJudge (pCtx);
-      if (true == pCtx->pWelsSvcRc[pCtx->uiDependencyId].bSkipFlag) {
-        bSkipMustFlag = true;
-        break;
+  if (pCtx->pSvcParam->bEnableFrameSkip) {
+    if ((RC_QUALITY_MODE == pCtx->pSvcParam->iRCMode) || (RC_BITRATE_MODE == pCtx->pSvcParam->iRCMode)
+        || (RC_LOW_BW_MODE == pCtx->pSvcParam->iRCMode)) {
+      for (int32_t i = 0; i < iSpatialNum; i++) {
+        if (0 == pCtx->pSvcParam->sSpatialLayers[i].iMaxSpatialBitrate) {
+          break;
+        }
+        pCtx->uiDependencyId = (uint8_t) (pSpatialIndexMap + i)->iDid;
+        pCtx->pFuncList->pfRc.pfWelsRcPicDelayJudge (pCtx);
+        if (true == pCtx->pWelsSvcRc[pCtx->uiDependencyId].bSkipFlag) {
+          bSkipMustFlag = true;
+          break;
+        }
       }
     }
   }
--- a/codec/encoder/core/src/ratectl.cpp
+++ b/codec/encoder/core/src/ratectl.cpp
@@ -999,13 +999,28 @@
 void  WelsRcMbInfoUpdateDisable (void* pCtx, SMB* pCurMb, int32_t iCostLuma, SSlice* pSlice) {
 }
 
+void WelRcPictureInitBufferBasedQp (void* pCtx) {
+  sWelsEncCtx* pEncCtx = (sWelsEncCtx*)pCtx;
+  SWelsSvcRc* pWelsSvcRc = &pEncCtx->pWelsSvcRc[pEncCtx->uiDependencyId];
+  SVAAFrameInfo* pVaa			= static_cast<SVAAFrameInfo*> (pEncCtx->pVaa);
 
-void  WelsRcInitModule (void* pCtx,  int32_t iModule) {
+  int32_t iMinQp = MIN_SCREEN_QP;
+  if (pVaa->eSceneChangeIdc == LARGE_CHANGED_SCENE)
+    iMinQp = MIN_SCREEN_QP + 2;
+  else if (pVaa->eSceneChangeIdc == MEDIUM_CHANGED_SCENE)
+    iMinQp = MIN_SCREEN_QP + 1;
+  else
+    iMinQp = MIN_SCREEN_QP;
+
+  pEncCtx->iGlobalQp += pEncCtx->iDropNumber;
+  pEncCtx->iGlobalQp = WELS_CLIP3 (pEncCtx->iGlobalQp, MIN_SCREEN_QP, MAX_SCREEN_QP);
+}
+void  WelsRcInitModule (void* pCtx, RC_MODES iRcMode) {
   sWelsEncCtx* pEncCtx = (sWelsEncCtx*)pCtx;
   SWelsRcFunc*   pRcf = &pEncCtx->pFuncList->pfRc;
 
-  switch (iModule) {
-  case WELS_RC_DISABLE:
+  switch (iRcMode) {
+  case RC_OFF_MODE:
     pRcf->pfWelsRcPictureInit = WelsRcPictureInitDisable;
     pRcf->pfWelsRcPicDelayJudge = NULL;
     pRcf->pfWelsRcPictureInfoUpdate = WelsRcPictureInfoUpdateDisable;
@@ -1012,7 +1027,16 @@
     pRcf->pfWelsRcMbInit = WelsRcMbInitDisable;
     pRcf->pfWelsRcMbInfoUpdate = WelsRcMbInfoUpdateDisable;
     break;
-  case WELS_RC_GOM:
+  case RC_BUFFERBASED_MODE:
+    pRcf->pfWelsRcPictureInit = WelRcPictureInitBufferBasedQp;
+    pRcf->pfWelsRcPicDelayJudge = NULL;
+    pRcf->pfWelsRcPictureInfoUpdate = WelsRcPictureInfoUpdateDisable;
+    pRcf->pfWelsRcMbInit = WelsRcMbInitDisable;
+    pRcf->pfWelsRcMbInfoUpdate = WelsRcMbInfoUpdateDisable;
+    break;
+  case RC_QUALITY_MODE:
+  case RC_BITRATE_MODE:
+  case RC_LOW_BW_MODE:
   default:
     pRcf->pfWelsRcPictureInit = WelsRcPictureInitGom;
     pRcf->pfWelsRcPicDelayJudge = WelsRcFrameDelayJudge;
--- a/testbin/welsenc.cfg
+++ b/testbin/welsenc.cfg
@@ -29,7 +29,8 @@
 MultipleThreadIdc			    1	# 0: auto(dynamic imp. internal encoder); 1: multiple threads imp. disabled; > 1: count number of threads;
 
 #============================== RATE CONTROL ==============================
-RCMode			        0				    # 0: quality mode;  1: bitrate mode;  2: bitrate limited mode;  -1: rc off mode
+RCMode			        0				        # 0: quality mode;  1: bitrate mode;  2: bitrate limited mode;
+                                                # 3: buffer based mode,can't control bitrate; -1: rc off mode;
 TargetBitrate			5000				    # Unit: kbps, controled by EnableRC also
 MaxOverallBitrate       6000                    # Unit: kbps, max bitrate overall
 EnableFrameSkip			1		#Enable Frame Skip
--- a/testbin/welsenc_arbitrary_res.cfg
+++ b/testbin/welsenc_arbitrary_res.cfg
@@ -30,7 +30,8 @@
 MultipleThreadIdc			    1	# 0: auto(dynamic imp. internal encoder); 1: multiple threads imp. disabled; > 1: count number of threads;
 
 #============================== RATE CONTROL ==============================
-RCMode                          0                                   # 0: quality mode;  1: bitrate mode;  2: bitrate limited mode;  -1: rc off mode
+RCMode			        0				        # 0: quality mode;  1: bitrate mode;  2: bitrate limited mode;
+                                                # 3: buffer based mode,can't control bitrate; -1: rc off mode;
 TargetBitrate			5000				    # Unit: kbps, controled by EnableRC also
 MaxOverallBitrate       6000                    # Unit: kbps, max bitrate overall
 
--- a/testbin/welsenc_ios.cfg
+++ b/testbin/welsenc_ios.cfg
@@ -30,7 +30,8 @@
 MultipleThreadIdc			    1	# 0: auto(dynamic imp. internal encoder); 1: multiple threads imp. disabled; > 1: count number of threads;
 
 #============================== RATE CONTROL ==============================
-RCMode                          0                                   # 0: quality mode;  1: bitrate mode;  2: bitrate limited mode;  -1: rc off mode
+RCMode			        0				        # 0: quality mode;  1: bitrate mode;  2: bitrate limited mode;
+                                                # 3: buffer based mode,can't control bitrate; -1: rc off mode;
 TargetBitrate			5000				    # Unit: kbps, controled by EnableRC also
 MaxOverallBitrate       6000                 # Unit: kbps, max bitrate overall
 EnableFrameSkip			1		#Enable Frame Skip
--- a/testbin/welsenc_vd_1d.cfg
+++ b/testbin/welsenc_vd_1d.cfg
@@ -30,7 +30,8 @@
 MultipleThreadIdc			    1	# 0: auto(dynamic imp. internal encoder); 1: multiple threads imp. disabled; > 1: count number of threads;
 
 #============================== RATE CONTROL ==============================
-RCMode                          0                                   # 0: quality mode;  1: bitrate mode;  2: bitrate limited mode;  -1: rc off mode
+RCMode			        0				        # 0: quality mode;  1: bitrate mode;  2: bitrate limited mode;
+                                                # 3: buffer based mode,can't control bitrate; -1: rc off mode;
 TargetBitrate			5000				    # Unit: kbps, controled by EnableRC also
 MaxOverallBitrate       6000                    # Unit: kbps, max bitrate overall
 
--- a/testbin/welsenc_vd_rc.cfg
+++ b/testbin/welsenc_vd_rc.cfg
@@ -30,7 +30,8 @@
 MultipleThreadIdc			    1	# 0: auto(dynamic imp. internal encoder); 1: multiple threads imp. disabled; > 1: count number of threads;
 
 #============================== RATE CONTROL ==============================
-RCMode                          0                                   # 0: quality mode;  1: bitrate mode;  2: bitrate limited mode;  -1: rc off mode
+RCMode			        0				    # 0: quality mode;  1: bitrate mode;  2: bitrate limited mode; 
+                                            # 3: buffer based mode,can't control bitrate; -1: rc off mode;
 TargetBitrate			600				    # Unit: kbps, controled by EnableRC also
 MaxOverallBitrate       800                 # Unit: kbps, max bitrate overall