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

Ximing Cheng chengximing1989 at gmail.com
Fri Oct 16 10:20:39 CEST 2015


This patch reduce the calculation by saving the SATD moving sum last
time (frame), then the next frame can calculate its SATD moving sum by
the last moving sum and the replaced value in the slidingWindow, so
that each frame in rate ctrl does not need to do a loop for at most
s_slidingWindowFrames times to calculate its SATD moving sum.

Suppose slidingWindow size is 6, value is x y z a b c

rc frame (m_sliderPos)                moving sum
0                                               0
1                                               0.5x
2                                               0.5^2x+0.5y
3                                               0.5^3x+0.5^2y+0.5z
4                                               0.5^4x+0.5^3y+0.5^2z+0.5a
5                                               0.5^5x+0.5^4y+0.5^3z+0.5^2a+0.5b
6
0.5^5x+0.5^4y+0.5^3z+0.5^2a+0.5b+c
7                                               0.5
(0.5^4y+0.5^3z+0.5^2a+0.5b+c)+x_1   == 0.5
(0.5^5x+0.5^4y+0.5^3z+0.5^2a+0.5b+c -  0.5^5x) + x_1

which x_1 is the replaced value of x

On Mon, Oct 12, 2015 at 10:26 AM, Ximing Cheng
<chengximing1989 at foxmail.com> wrote:
> # HG changeset patch
> # User Ximing Cheng <ximingcheng at tencent.com>
> # Date 1444616617 -28800
> #      Mon Oct 12 10:23:37 2015 +0800
> # Node ID 0d1cfbb2716edcf9ba5b8ccc8b0fd585f4de59d7
> # Parent  667253981f61f18c36bf5c7f607fbf6ea8cc6474
> rc: Remove the redundant calculation on slidingWindow
>
> diff -r 667253981f61 -r 0d1cfbb2716e source/encoder/ratecontrol.cpp
> --- a/source/encoder/ratecontrol.cpp    Mon Oct 12 10:18:55 2015 +0800
> +++ b/source/encoder/ratecontrol.cpp    Mon Oct 12 10:23:37 2015 +0800
> @@ -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 -r 667253981f61 -r 0d1cfbb2716e source/encoder/ratecontrol.h
> --- a/source/encoder/ratecontrol.h      Mon Oct 12 10:18:55 2015 +0800
> +++ b/source/encoder/ratecontrol.h      Mon Oct 12 10:23:37 2015 +0800
> @@ -167,6 +167,8 @@
>      int64_t m_satdCostWindow[50];
>      int64_t m_encodedBitsWindow[50];
>      int     m_sliderPos;
> +    int64_t m_lastRemovedSatdCost;
> +    double  m_movingAvgSum;
>
>      /* To detect a pattern of low detailed static frames in single pass ABR using satdcosts */
>      int64_t m_lastBsliceSatdCost;
>
>
> _______________________________________________
> x265-devel mailing list
> x265-devel at videolan.org
> https://mailman.videolan.org/listinfo/x265-devel


More information about the x265-devel mailing list