[x265] [PATCH] rc: fix rate factor values recorded in csv

Aarthi Priya Thirumalai aarthi at multicorewareinc.com
Thu Dec 10 18:18:45 CET 2015


# HG changeset patch
# User Aarthi Priya Thirumalai <aarthi at multicorewareinc.com>
# Date 1449763621 -19800
#      Thu Dec 10 21:37:01 2015 +0530
# Node ID 721d60a7988b85083f72832b58e5e98f42d5a678
# Parent  33d04da2f68830ac51151cfbda8f38fb9a7e8bb9
rc: fix rate factor values recorded in csv

diff -r 33d04da2f688 -r 721d60a7988b source/encoder/ratecontrol.cpp
--- a/source/encoder/ratecontrol.cpp	Wed Dec 09 22:24:25 2015 +0530
+++ b/source/encoder/ratecontrol.cpp	Thu Dec 10 21:37:01 2015 +0530
@@ -142,6 +142,7 @@
     rce->expectedVbv = rce2Pass->expectedVbv;
     rce->blurredComplexity = rce2Pass->blurredComplexity;
     rce->sliceType = rce2Pass->sliceType;
+    rce->qRceq = rce2Pass->qRceq;
 }
 
 }  // end anonymous namespace
@@ -509,7 +510,7 @@
                 char picType;
                 int e;
                 char *next;
-                double qpRc, qpAq, qNoVbv;
+                double qpRc, qpAq, qNoVbv, qRceq;
                 next = strstr(p, ";");
                 if (next)
                     *next++ = 0;
@@ -520,8 +521,8 @@
                     return false;
                 }
                 rce = &m_rce2Pass[frameNumber];
-                e += sscanf(p, " in:%*d out:%*d type:%c q:%lf q-aq:%lf q-noVbv:%lf tex:%d mv:%d misc:%d icu:%lf pcu:%lf scu:%lf",
-                       &picType, &qpRc, &qpAq, &qNoVbv, &rce->coeffBits,
+                e += sscanf(p, " in:%*d out:%*d type:%c q:%lf q-aq:%lf q-noVbv:%lf q-Rceq:%lf tex:%d mv:%d misc:%d icu:%lf pcu:%lf scu:%lf",
+                       &picType, &qpRc, &qpAq, &qNoVbv, &qRceq, &rce->coeffBits,
                        &rce->mvBits, &rce->miscBits, &rce->iCuCount, &rce->pCuCount,
                        &rce->skipCuCount);
                 rce->keptAsRef = true;
@@ -545,6 +546,7 @@
                 rce->qpNoVbv = qNoVbv;
                 rce->qpaRc = qpRc;
                 rce->qpAq = qpAq;
+                rce->qRceq = qRceq;
                 p = next;
             }
             X265_FREE(statsBuf);
@@ -2239,20 +2241,32 @@
     {
         if (m_param->rc.rateControlMode == X265_RC_ABR && !m_param->rc.bStatRead)
             checkAndResetABR(rce, true);
+    }
+    if (m_param->rc.rateControlMode == X265_RC_CRF)
+    {
+        double crfVal;
+        if (m_2pass || abs(curEncData.m_avgQpRc - rce->qpNoVbv) > 0.5)
+        {
+            double qpRef = curEncData.m_avgQpRc;
+            if (m_2pass)
+            {
+                double qpPrev = x265_qScale2qp(rce->qScale);
+                qpRef = abs(curEncData.m_avgQpRc - qpPrev) > 0.1 ? curEncData.m_avgQpRc : qpPrev;
+            }
+            double crfFactor = rce->qRceq /x265_qp2qScale(qpRef);
+            if(rce->sliceType == I_SLICE)
+                crfFactor /= m_param->rc.ipFactor;
+            else if (rce->sliceType == B_SLICE)
+                crfFactor *= m_param->rc.pbFactor;
+            double baseCplx = m_ncu * (m_param->bframes ? 120 : 80);
+            double mbtree_offset = m_param->rc.cuTree ? (1.0 - m_param->rc.qCompress) * 13.5 : 0;
+            crfVal = x265_qScale2qp(pow(baseCplx, 1 - m_qCompress) / crfFactor) - mbtree_offset;
+        }
+        else
+            crfVal = rce->sliceType == I_SLICE ? m_param->rc.rfConstant - m_ipOffset : 
+            (rce->sliceType == B_SLICE ? m_param->rc.rfConstant + m_pbOffset : m_param->rc.rfConstant);
 
-        if (m_param->rc.rateControlMode == X265_RC_CRF)
-        {
-            if (int(curEncData.m_avgQpRc + 0.5) == slice->m_sliceQp)
-                curEncData.m_rateFactor = m_rateFactorConstant;
-            else
-            {
-                /* If vbv changed the frame QP recalculate the rate-factor */
-                double baseCplx = m_ncu * (m_param->bframes ? 120 : 80);
-                double mbtree_offset = m_param->rc.cuTree ? (1.0 - m_param->rc.qCompress) * 13.5 : 0;
-                curEncData.m_rateFactor = pow(baseCplx, 1 - m_qCompress) /
-                    x265_qp2qScale(int(curEncData.m_avgQpRc + 0.5) + mbtree_offset);
-            }
-        }
+        curEncData.m_rateFactor = crfVal;
     }
 
     if (m_isAbr && !m_isAbrReset)
@@ -2346,10 +2360,10 @@
         : rce->sliceType == P_SLICE ? 'P'
         : IS_REFERENCED(curFrame) ? 'B' : 'b';
     if (fprintf(m_statFileOut,
-                "in:%d out:%d type:%c q:%.2f q-aq:%.2f q-noVbv:%.2f tex:%d mv:%d misc:%d icu:%.2f pcu:%.2f scu:%.2f ;\n",
+                "in:%d out:%d type:%c q:%.2f q-aq:%.2f q-noVbv:%.2f q-Rceq:%.2f tex:%d mv:%d misc:%d icu:%.2f pcu:%.2f scu:%.2f ;\n",
                 rce->poc, rce->encodeOrder,
                 cType, curEncData.m_avgQpRc, curEncData.m_avgQpAq,
-                rce->qpNoVbv,
+                rce->qpNoVbv, rce->qRceq,
                 curFrame->m_encData->m_frameStats.coeffBits,
                 curFrame->m_encData->m_frameStats.mvBits,
                 curFrame->m_encData->m_frameStats.miscBits,


More information about the x265-devel mailing list