shithub: openh264

Download patch

ref: f638546d81c93fbddcc0323a5dff548885c00324
parent: 7cfce53f53c653d2e0ad0238fa8e95f9b023b5ba
parent: 9015bd40494a1cf6e655f0648be90cff5f0d0d0a
author: huili2 <[email protected]>
date: Sat Jul 5 13:17:32 EDT 2014

Merge pull request #1089 from ruil2/rc_fix_1

    add some protection on rc module

--- a/codec/encoder/core/src/ratectl.cpp
+++ b/codec/encoder/core/src/ratectl.cpp
@@ -549,8 +549,11 @@
   if (pEncCtx->eSliceType == I_SLICE) {
     pWelsSvcRc->iTargetBits = WELS_DIV_ROUND (pWelsSvcRc->iBitsPerFrame * IDR_BITRATE_RATIO, INT_MULTIPLY);
   } else {
-    pWelsSvcRc->iTargetBits = (int32_t) ((int64_t)pWelsSvcRc->iRemainingBits * pTOverRc->iTlayerWeight /
-                                         pWelsSvcRc->iRemainingWeights);
+    if (pWelsSvcRc->iRemainingWeights > pTOverRc->iTlayerWeight)
+      pWelsSvcRc->iTargetBits = (int32_t) ((int64_t)pWelsSvcRc->iRemainingBits * pTOverRc->iTlayerWeight /
+                                           pWelsSvcRc->iRemainingWeights);
+    else //this case should be not hit. needs to more test case to verify this
+      pWelsSvcRc->iTargetBits = pWelsSvcRc->iRemainingBits;
     if ((pWelsSvcRc->iTargetBits <= 0) && (pEncCtx->pSvcParam->iRCMode == RC_LOW_BW_MODE)) {
       pWelsSvcRc->iCurrentBitsLevel = BITS_EXCEEDED;
     } else if ((pWelsSvcRc->iTargetBits <= pTOverRc->iMinBitsTl) && (pEncCtx->pSvcParam->iRCMode == RC_LOW_BW_MODE)) {
@@ -698,7 +701,8 @@
   for (int32_t i = pWelsSvcRc->iFrameCodedInVGop + 1; i < VGOP_SIZE; i++)
     iVGopBitsPred += pTOverRc[pWelsSvcRc->iTlOfFrames[i]].iMinBitsTl;
   iVGopBitsPred -= pWelsSvcRc->iRemainingBits;
-  double dIncPercent = iVGopBitsPred * 100.0 * INT_MULTIPLY / (pWelsSvcRc->iBitsPerFrame * VGOP_SIZE) - (double)VGOP_BITS_PERCENTAGE_DIFF;
+  double dIncPercent = iVGopBitsPred * 100.0 * INT_MULTIPLY / (pWelsSvcRc->iBitsPerFrame * VGOP_SIZE) -
+                       (double)VGOP_BITS_PERCENTAGE_DIFF;
 
   if ((pWelsSvcRc->iBufferFullnessSkip > pWelsSvcRc->iBufferSizeSkip
        &&	pWelsSvcRc->iAverageFrameQp > pWelsSvcRc->iSkipQpValue)