<div dir="ltr">Thanks Aarthi. Can you quickly jog my memory on what we discussed about CRF calculations?<br></div><div class="gmail_extra"><br><div class="gmail_quote">On Fri, Dec 11, 2015 at 8:17 PM, Aarthi Priya Thirumalai <span dir="ltr"><<a href="mailto:aarthi@multicorewareinc.com" target="_blank">aarthi@multicorewareinc.com</a>></span> wrote:<br><blockquote class="gmail_quote" style="margin:0 0 0 .8ex;border-left:1px #ccc solid;padding-left:1ex"><span class=""># HG changeset patch<br>
# User Aarthi Priya Thirumalai <<a href="mailto:aarthi@multicorewareinc.com">aarthi@multicorewareinc.com</a>><br>
# Date 1449763621 -19800<br>
#      Thu Dec 10 21:37:01 2015 +0530<br>
</span># Node ID c401ace41d5d8d2eef8f82d625465de7f9b47b0e<br>
# Parent  d72c79721e88cbd6e471d50351b46111918871ba<br>
<span class="">rc: fix rate factor values recorded in csv<br>
<br>
</span>diff -r d72c79721e88 -r c401ace41d5d source/encoder/ratecontrol.cpp<br>
--- a/source/encoder/ratecontrol.cpp    Thu Dec 10 10:07:17 2015 +0530<br>
<span class="">+++ b/source/encoder/ratecontrol.cpp    Thu Dec 10 21:37:01 2015 +0530<br>
</span>@@ -144,6 +144,7 @@<br>
<span class="">     rce->sliceType = rce2Pass->sliceType;<br>
</span>     rce->qpNoVbv = rce2Pass->qpNoVbv;<br>
     rce->newQp = rce2Pass->newQp;<br>
<span class="">+    rce->qRceq = rce2Pass->qRceq;<br>
 }<br>
 }  // end anonymous namespace<br>
</span> /* Returns the zone for the current frame */<br>
@@ -517,7 +518,7 @@<br>
<span class="">                 char picType;<br>
                 int e;<br>
                 char *next;<br>
-                double qpRc, qpAq, qNoVbv;<br>
+                double qpRc, qpAq, qNoVbv, qRceq;<br>
                 next = strstr(p, ";");<br>
                 if (next)<br>
                     *next++ = 0;<br>
</span>@@ -529,8 +530,8 @@<br>
                 }<br>
                 rce = &m_rce2Pass[encodeOrder];<br>
                 m_encOrder[frameNumber] = encodeOrder;<br>
<span class="">-                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",<br>
-                       &picType, &qpRc, &qpAq, &qNoVbv, &rce->coeffBits,<br>
+                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",<br>
+                       &picType, &qpRc, &qpAq, &qNoVbv, &qRceq, &rce->coeffBits,<br>
                        &rce->mvBits, &rce->miscBits, &rce->iCuCount, &rce->pCuCount,<br>
                        &rce->skipCuCount);<br>
                 rce->keptAsRef = true;<br>
</span>@@ -554,6 +555,7 @@<br>
<span class="">                 rce->qpNoVbv = qNoVbv;<br>
                 rce->qpaRc = qpRc;<br>
                 rce->qpAq = qpAq;<br>
+                rce->qRceq = qRceq;<br>
                 p = next;<br>
             }<br>
             X265_FREE(statsBuf);<br>
</span>@@ -1142,6 +1144,7 @@<br>
     }<br>
     if (!m_isAbr && m_2pass && m_param->rc.rateControlMode == X265_RC_CRF)<br>
     {<br>
+        rce->qpPrev = x265_qScale2qp(rce->qScale);<br>
         rce->qScale = rce->newQScale;<br>
         rce->qpaRc = curEncData.m_avgQpRc = curEncData.m_avgQpAq = x265_qScale2qp(rce->newQScale);<br>
         m_qp = int(rce->qpaRc + 0.5);<br>
@@ -2347,20 +2350,31 @@<br>
<span class="">     {<br>
         if (m_param->rc.rateControlMode == X265_RC_ABR && !m_param->rc.bStatRead)<br>
             checkAndResetABR(rce, true);<br>
</span>-<br>
<span class="">-        if (m_param->rc.rateControlMode == X265_RC_CRF)<br>
</span><span class="">+    }<br>
+    if (m_param->rc.rateControlMode == X265_RC_CRF)<br>
+    {<br>
</span>+        double crfVal, qpRef = curEncData.m_avgQpRc;<br>
+        bool is2passCrfChange = false;<br>
+        if (m_2pass)<br>
<span class="">         {<br>
-            if (int(curEncData.m_avgQpRc + 0.5) == slice->m_sliceQp)<br>
-                curEncData.m_rateFactor = m_rateFactorConstant;<br>
-            else<br>
</span>+            if (abs(curEncData.m_avgQpRc - rce->qpPrev) > 0.1)<br>
<span class="">             {<br>
-                /* If vbv changed the frame QP recalculate the rate-factor */<br>
-                double baseCplx = m_ncu * (m_param->bframes ? 120 : 80);<br>
-                double mbtree_offset = m_param->rc.cuTree ? (1.0 - m_param->rc.qCompress) * 13.5 : 0;<br>
-                curEncData.m_rateFactor = pow(baseCplx, 1 - m_qCompress) /<br>
-                    x265_qp2qScale(int(curEncData.m_avgQpRc + 0.5) + mbtree_offset);<br>
</span>+                qpRef = rce->qpPrev;<br>
+                is2passCrfChange = true;<br>
             }<br>
         }<br>
+        if (is2passCrfChange || abs(qpRef - rce->qpNoVbv) > 0.5)<br>
+        {<br>
+            double crfFactor = rce->qRceq /x265_qp2qScale(qpRef);<br>
+            double baseCplx = m_ncu * (m_param->bframes ? 120 : 80);<br>
<span class="">+            double mbtree_offset = m_param->rc.cuTree ? (1.0 - m_param->rc.qCompress) * 13.5 : 0;<br>
+            crfVal = x265_qScale2qp(pow(baseCplx, 1 - m_qCompress) / crfFactor) - mbtree_offset;<br>
+        }<br>
+        else<br>
+            crfVal = rce->sliceType == I_SLICE ? m_param->rc.rfConstant - m_ipOffset :<br>
+            (rce->sliceType == B_SLICE ? m_param->rc.rfConstant + m_pbOffset : m_param->rc.rfConstant);<br>
</span>+<br>
<span class="">+        curEncData.m_rateFactor = crfVal;<br>
     }<br>
<br>
     if (m_isAbr && !m_isAbrReset)<br>
</span>@@ -2454,10 +2468,10 @@<br>
<span class="">         : rce->sliceType == P_SLICE ? 'P'<br>
         : IS_REFERENCED(curFrame) ? 'B' : 'b';<br>
     if (fprintf(m_statFileOut,<br>
-                "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",<br>
+                "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",<br>
                 rce->poc, rce->encodeOrder,<br>
                 cType, curEncData.m_avgQpRc, curEncData.m_avgQpAq,<br>
-                rce->qpNoVbv,<br>
+                rce->qpNoVbv, rce->qRceq,<br>
                 curFrame->m_encData->m_frameStats.coeffBits,<br>
                 curFrame->m_encData->m_frameStats.mvBits,<br>
                 curFrame->m_encData->m_frameStats.miscBits,<br>
</span>diff -r d72c79721e88 -r c401ace41d5d source/encoder/ratecontrol.h<br>
--- a/source/encoder/ratecontrol.h      Thu Dec 10 10:07:17 2015 +0530<br>
+++ b/source/encoder/ratecontrol.h      Thu Dec 10 21:37:01 2015 +0530<br>
@@ -75,6 +75,7 @@<br>
     double  qpaRc;<br>
     double  qpAq;<br>
     double  qRceq;<br>
+    double  qpPrev;<br>
     double  frameSizePlanned;  /* frame Size decided by RateCotrol before encoding the frame */<br>
     double  bufferRate;<br>
     double  movingAvgSum;<br>
<div class="HOEnZb"><div class="h5">_______________________________________________<br>
x265-devel mailing list<br>
<a href="mailto:x265-devel@videolan.org">x265-devel@videolan.org</a><br>
<a href="https://mailman.videolan.org/listinfo/x265-devel" rel="noreferrer" target="_blank">https://mailman.videolan.org/listinfo/x265-devel</a><br>
</div></div></blockquote></div><br><br clear="all"><br>-- <br><div class="gmail_signature"><div dir="ltr"><div><div>Deepthi Nandakumar<br></div>Engineering Manager, x265<br></div>Multicoreware, Inc<br></div></div>
</div>