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

Aarthi Priya Thirumalai aarthi at multicorewareinc.com
Fri Dec 11 15:47:31 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 c401ace41d5d8d2eef8f82d625465de7f9b47b0e
# Parent  d72c79721e88cbd6e471d50351b46111918871ba
rc: fix rate factor values recorded in csv

diff -r d72c79721e88 -r c401ace41d5d source/encoder/ratecontrol.cpp
--- a/source/encoder/ratecontrol.cpp	Thu Dec 10 10:07:17 2015 +0530
+++ b/source/encoder/ratecontrol.cpp	Thu Dec 10 21:37:01 2015 +0530
@@ -144,6 +144,7 @@
     rce->sliceType = rce2Pass->sliceType;
     rce->qpNoVbv = rce2Pass->qpNoVbv;
     rce->newQp = rce2Pass->newQp;
+    rce->qRceq = rce2Pass->qRceq;
 }
 }  // end anonymous namespace
 /* Returns the zone for the current frame */
@@ -517,7 +518,7 @@
                 char picType;
                 int e;
                 char *next;
-                double qpRc, qpAq, qNoVbv;
+                double qpRc, qpAq, qNoVbv, qRceq;
                 next = strstr(p, ";");
                 if (next)
                     *next++ = 0;
@@ -529,8 +530,8 @@
                 }
                 rce = &m_rce2Pass[encodeOrder];
                 m_encOrder[frameNumber] = encodeOrder;
-                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;
@@ -554,6 +555,7 @@
                 rce->qpNoVbv = qNoVbv;
                 rce->qpaRc = qpRc;
                 rce->qpAq = qpAq;
+                rce->qRceq = qRceq;
                 p = next;
             }
             X265_FREE(statsBuf);
@@ -1142,6 +1144,7 @@
     }
     if (!m_isAbr && m_2pass && m_param->rc.rateControlMode == X265_RC_CRF)
     {
+        rce->qpPrev = x265_qScale2qp(rce->qScale);
         rce->qScale = rce->newQScale;
         rce->qpaRc = curEncData.m_avgQpRc = curEncData.m_avgQpAq = x265_qScale2qp(rce->newQScale);
         m_qp = int(rce->qpaRc + 0.5);
@@ -2347,20 +2350,31 @@
     {
         if (m_param->rc.rateControlMode == X265_RC_ABR && !m_param->rc.bStatRead)
             checkAndResetABR(rce, true);
-
-        if (m_param->rc.rateControlMode == X265_RC_CRF)
+    }
+    if (m_param->rc.rateControlMode == X265_RC_CRF)
+    {
+        double crfVal, qpRef = curEncData.m_avgQpRc;
+        bool is2passCrfChange = false;
+        if (m_2pass)
         {
-            if (int(curEncData.m_avgQpRc + 0.5) == slice->m_sliceQp)
-                curEncData.m_rateFactor = m_rateFactorConstant;
-            else
+            if (abs(curEncData.m_avgQpRc - rce->qpPrev) > 0.1)
             {
-                /* 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);
+                qpRef = rce->qpPrev;
+                is2passCrfChange = true;
             }
         }
+        if (is2passCrfChange || abs(qpRef - rce->qpNoVbv) > 0.5)
+        {
+            double crfFactor = rce->qRceq /x265_qp2qScale(qpRef);
+            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);
+
+        curEncData.m_rateFactor = crfVal;
     }
 
     if (m_isAbr && !m_isAbrReset)
@@ -2454,10 +2468,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,
diff -r d72c79721e88 -r c401ace41d5d source/encoder/ratecontrol.h
--- a/source/encoder/ratecontrol.h	Thu Dec 10 10:07:17 2015 +0530
+++ b/source/encoder/ratecontrol.h	Thu Dec 10 21:37:01 2015 +0530
@@ -75,6 +75,7 @@
     double  qpaRc;
     double  qpAq;
     double  qRceq;
+    double  qpPrev;
     double  frameSizePlanned;  /* frame Size decided by RateCotrol before encoding the frame */
     double  bufferRate;
     double  movingAvgSum;


More information about the x265-devel mailing list