[x265] [PATCH] rc: Remove the redundant calculation on slidingWindow

Ximing Cheng chengximing1989 at gmail.com
Fri Oct 9 18:04:27 CEST 2015


# HG changeset patch
# User Ximing Cheng <ximingcheng at tencent.com>
# Date 1444406456 -28800
#      Sat Oct 10 00:00:56 2015 +0800
# Node ID 276e3a0d90a4e3b38a482c3c1789befc9cdde945
# Parent  4176ffb657db00f173ba15a8d54c821f6811f514
Remove the redundant calculation on slidingWindow

diff --git a/source/encoder/ratecontrol.cpp b/source/encoder/ratecontrol.cpp
--- a/source/encoder/ratecontrol.cpp
+++ b/source/encoder/ratecontrol.cpp
@@ -219,6 +219,7 @@
     m_cutreeStatFileOut = m_cutreeStatFileIn = NULL;
     m_rce2Pass = NULL;
     m_lastBsliceSatdCost = 0;
+    m_movingAvgSum = 0.0;

     // vbv initialization
     m_param->rc.vbvBufferSize = x265_clip3(0, 2000000,
m_param->rc.vbvBufferSize);
@@ -1347,18 +1348,28 @@
     {
         if (m_isAbr)
         {
-            double slidingWindowCplxSum = 0;
-            int start = m_sliderPos > s_slidingWindowFrames ?  m_sliderPos
: 0;
-            for (int cnt = 0; cnt < s_slidingWindowFrames; cnt++, start++)
+            int pos = m_sliderPos % s_slidingWindowFrames;
+            int addPos = (pos + s_slidingWindowFrames - 1) %
s_slidingWindowFrames;
+            if (m_sliderPos > s_slidingWindowFrames)
             {
-                int pos = start % s_slidingWindowFrames;
-                slidingWindowCplxSum *= 0.5;
-                if (!m_satdCostWindow[pos])
-                    break;
-                slidingWindowCplxSum += m_satdCostWindow[pos];
+                const static double base = pow(0.5, s_slidingWindowFrames
- 1);
+                m_movingAvgSum -= m_lastRemovedSatdCost * base;
+                m_movingAvgSum *= 0.5;
+                m_movingAvgSum += m_satdCostWindow[addPos];
             }
-            rce->movingAvgSum = slidingWindowCplxSum;
-            m_satdCostWindow[m_sliderPos % s_slidingWindowFrames] =
rce->lastSatd;
+            else if (m_sliderPos == s_slidingWindowFrames)
+            {
+                m_movingAvgSum += m_satdCostWindow[addPos];
+            }
+            else if (m_sliderPos > 0)
+            {
+                m_movingAvgSum += m_satdCostWindow[addPos];
+                m_movingAvgSum *= 0.5;
+            }
+
+            rce->movingAvgSum = m_movingAvgSum;
+            m_lastRemovedSatdCost = m_satdCostWindow[pos];
+            m_satdCostWindow[pos] = rce->lastSatd;
             m_sliderPos++;
         }
     }
diff --git a/source/encoder/ratecontrol.h b/source/encoder/ratecontrol.h
--- a/source/encoder/ratecontrol.h
+++ b/source/encoder/ratecontrol.h
@@ -174,6 +174,8 @@
     bool    m_isPatternPresent;
     bool    m_isSceneTransition;
     int     m_lastPredictorReset;
+    int64_t m_lastRemovedSatdCost;
+    double  m_movingAvgSum;

     /* a common variable on which rateControlStart, rateControlEnd and
rateControUpdateStats waits to
      * sync the calls to these functions. For example
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://mailman.videolan.org/pipermail/x265-devel/attachments/20151010/3b9ef51a/attachment-0001.html>


More information about the x265-devel mailing list