[x265] [PATCH 1 of 3 RFC] rc: define function for updating rate control statistics in processRowEncoder

santhoshini at multicorewareinc.com santhoshini at multicorewareinc.com
Fri Jul 11 13:24:44 CEST 2014


# HG changeset patch
# User Santhoshini Sekar <santhoshini at multicorewareinc.com>
# Date 1405070640 -19800
#      Fri Jul 11 14:54:00 2014 +0530
# Node ID 902e1d37847f0bc222fdc05dcd4a7f6dd26b0751
# Parent  e171ad8bc8a3611b75e084587f4c07c41427c73d
rc: define function for updating rate control statistics in processRowEncoder

diff -r e171ad8bc8a3 -r 902e1d37847f source/encoder/ratecontrol.cpp
--- a/source/encoder/ratecontrol.cpp	Fri Jul 11 01:44:43 2014 -0500
+++ b/source/encoder/ratecontrol.cpp	Fri Jul 11 14:54:00 2014 +0530
@@ -274,6 +274,7 @@
     m_residualCost = 0;
     m_rateFactorMaxIncrement = 0;
     m_rateFactorMaxDecrement = 0;
+    m_startEndOrder.set(0);
 
     if (m_param->rc.rateControlMode == X265_RC_CRF)
     {
@@ -781,6 +782,46 @@
     }
 }
 
+void RateControl::rateControlUpdateStats(RateControlEntry* rce)
+{
+    ScopedLock scope(m_lock);
+
+    if (rce->sliceType == I_SLICE)
+    {
+        /* previous I still had a residual; roll it into the new loan */
+        if (m_residualFrames)
+            rce->rowTotalBits += m_residualCost * m_residualFrames;
+
+        m_residualFrames = X265_MIN(s_amortizeFrames, m_param->keyframeMax);
+        m_residualCost = (int)((rce->rowTotalBits * s_amortizeFraction) / m_residualFrames);
+        rce->rowTotalBits -= m_residualCost * m_residualFrames;
+    }
+    else if (m_residualFrames)
+    {
+         rce->rowTotalBits += m_residualCost;
+    }
+
+    if (rce->sliceType != B_SLICE)
+    {
+        rce->rowCplxrSum =  rce->rowTotalBits * x265_qp2qScale(rce->qpaRc) / rce->qRceq;
+    }
+    else
+    {
+        rce->rowCplxrSum = rce->rowTotalBits * x265_qp2qScale(rce->qpaRc) / (rce->qRceq * fabs(m_param->rc.pbFactor));
+    }
+
+    m_cplxrSum += rce->rowCplxrSum;
+    m_totalBits += rce->rowTotalBits;
+    m_startEndOrder.incr();
+
+    /* delay incrementing m_startEndOrder until here to sync with rateControlStart() */
+    if (rce->encodeOrder < m_param->frameNumThreads - 1)
+    {
+        m_startEndOrder.incr(); // faked rateControlEnd calls for negative frames
+    }
+
+}
+
 void RateControl::checkAndResetABR(RateControlEntry* rce, bool isFrameDone)
 {
     double abrBuffer = 2 * m_param->rc.rateTolerance * m_bitrate;
diff -r e171ad8bc8a3 -r 902e1d37847f source/encoder/ratecontrol.h
--- a/source/encoder/ratecontrol.h	Fri Jul 11 01:44:43 2014 -0500
+++ b/source/encoder/ratecontrol.h	Fri Jul 11 14:54:00 2014 +0530
@@ -68,6 +68,8 @@
     double frameSizePlanned;  /* frame Size decided by RateCotrol before encoding the frame */
     double bufferRate;
     double movingAvgSum;
+    double   rowCplxrSum;
+    int64_t  rowTotalBits;  /* update cplxrsum and totalbits at the end of 2 rows */
     double qpNoVbv;
     double bufferFill;
     Predictor rowPreds[3][2];
@@ -135,6 +137,8 @@
     int64_t  m_totalBits;        /* total bits used for already encoded frames */
     int      m_framesDone;       /* # of frames passed through RateCotrol already */
 
+    ThreadSafeInteger m_startEndOrder;
+
     /* hrd stuff */
     SEIBufferingPeriod m_bufPeriodSEI;
     double   m_nominalRemovalTime;
@@ -158,11 +162,13 @@
     // to be called for each frame to process RateControl and set QP
     void rateControlStart(Frame* pic, Lookahead *, RateControlEntry* rce, Encoder* enc);
     void calcAdaptiveQuantFrame(Frame *pic);
+    void rateControlUpdateStats(RateControlEntry* rce);
     int rateControlEnd(Frame* pic, int64_t bits, RateControlEntry* rce, FrameStats* stats);
     int rowDiagonalVbvRateControl(Frame* pic, uint32_t row, RateControlEntry* rce, double& qpVbv);
     void hrdFullness(SEIBufferingPeriod* sei);
     bool init(TComSPS* sps);
     void initHRD(TComSPS* sps);
+    Lock                m_lock;
 protected:
 
     static const double s_amortizeFraction;


More information about the x265-devel mailing list