[x265] [PATCH 2 of 2] rc: restrict low tolerance for bitrate fluctuations to strict-cbr option

aarthi at multicorewareinc.com aarthi at multicorewareinc.com
Wed Jan 7 14:41:04 CET 2015


# HG changeset patch
# User Aarthi Thirumalai
# Date 1419064540 -19800
#      Sat Dec 20 14:05:40 2014 +0530
# Node ID 5d658640bcf115f2add4da1619eeaec50177973f
# Parent  9b30c4b16dfc4c673b57ee4c567fa8956a41e2fc
rc: restrict low tolerance for bitrate fluctuations to strict-cbr option

diff -r 9b30c4b16dfc -r 5d658640bcf1 source/encoder/ratecontrol.cpp
--- a/source/encoder/ratecontrol.cpp	Tue Dec 23 16:32:30 2014 +0530
+++ b/source/encoder/ratecontrol.cpp	Sat Dec 20 14:05:40 2014 +0530
@@ -1759,7 +1759,7 @@
                 for (int j = 0; bufferFillCur >= 0; j++)
                 {
                     int type = curFrame->m_lowres.plannedType[j];
-                    if (type == X265_TYPE_AUTO)
+                    if (type == X265_TYPE_AUTO || totalDuration >= 1.0)
                         break;
                     totalDuration += m_frameDuration;
                     double wantedFrameSize = m_vbvMaxRate * m_frameDuration;
@@ -1773,17 +1773,21 @@
                     bufferFillCur -= curBits;
                 }
 
-                /* Try to get the buffer no more than 80% filled, but don't set an impossible goal. */
-                double tol = isIFramePresent ? 1 / totalDuration : totalDuration < 0.5 ? 2 : 1;
-                targetFill = X265_MIN(m_bufferFill + totalDuration * m_vbvMaxRate * 0.5 , m_bufferSize * (1 - 0.8 * totalDuration * tol));
+                /* Try to get the buffer at least 50% filled, but don't set an impossible goal. */
+                double finalDur = 1;
+                if (m_param->rc.bStrictCbr)
+                {
+                    finalDur = Clip3(0.4, 1.0, totalDuration);
+                }
+                targetFill = X265_MIN(m_bufferFill + totalDuration * m_vbvMaxRate * 0.5 , m_bufferSize * (1 - 0.5 * finalDur));
                 if (bufferFillCur < targetFill)
                 {
                     q *= 1.01;
                     loopTerminate |= 1;
                     continue;
                 }
-                /* Try to get the buffer atleast 50% filled, but don't set an impossible goal. */
-                targetFill = Clip3(m_bufferSize - (m_bufferSize * totalDuration * 0.5), m_bufferSize, m_bufferFill - totalDuration * m_vbvMaxRate * 0.5);
+                /* Try to get the buffer not more than 80% filled, but don't set an impossible goal. */
+                targetFill = Clip3(m_bufferSize * (1 - 0.2 * finalDur) , m_bufferSize, m_bufferFill - totalDuration * m_vbvMaxRate * 0.5);
                 if (m_isCbr && bufferFillCur > targetFill)
                 {
                     q /= 1.01;
@@ -1976,7 +1980,7 @@
     if (row < sps.numCuInHeight - 1)
     {
         /* More threads means we have to be more cautious in letting ratecontrol use up extra bits. */
-        double rcTol = (bufferLeftPlanned * 0.2) / m_param->frameNumThreads * m_param->rc.rateTolerance;
+        double rcTol = bufferLeftPlanned / m_param->frameNumThreads * m_param->rc.rateTolerance;
         int32_t encodedBitsSoFar = 0;
         double accFrameBits = predictRowsSizeSum(curFrame, rce, qpVbv, encodedBitsSoFar);
 
@@ -1994,7 +1998,7 @@
 
         while (qpVbv < qpMax
                && ((accFrameBits > rce->frameSizePlanned + rcTol) ||
-                   (rce->bufferFill - accFrameBits < bufferLeftPlanned * 0.2) ||
+                   (rce->bufferFill - accFrameBits < bufferLeftPlanned * 0.5) ||
                    (accFrameBits > rce->frameSizePlanned && qpVbv < rce->qpNoVbv)))
         {
             qpVbv += stepSize;


More information about the x265-devel mailing list