[x265] [PATCH] Enable Boundary Aware Frame Quantizer Selection

Niranjan Bala niranjan at multicorewareinc.com
Thu Nov 7 07:34:02 CET 2019


# HG changeset patch
# User Niranjan <niranjan at multicorewareinc.com>
# Date 1572519207 -19800
#      Thu Oct 31 16:23:27 2019 +0530
# Node ID 38865a56650c4bdc15c1c932adf658d1a3174d93
# Parent  e4fea011918a09e42bfa6d5c295cf0386c0bec7b
Enable Boundary Aware Frame Quantizer Selection

This patch does the following:
1)Reduce bits for frames inside the scenecut-window.
2)Add options "--scenecut-aware-qp", "--scenecut-window" and
"--max-qp-delta" to
enable boundary aware frame quantization, to set window size (optional) and
to
set offset(optional).

diff -r e4fea011918a -r 38865a56650c doc/reST/cli.rst
--- a/doc/reST/cli.rst Mon Oct 21 14:27:15 2019 +0530
+++ b/doc/reST/cli.rst Thu Oct 31 16:23:27 2019 +0530
@@ -1898,6 +1898,27 @@

  **CLI ONLY**

+.. option:: --scenecut-aware-qp, --no-scenecut-aware-qp
+
+   Increase QP for inter-frames after a scenecut is detected, for a
specified duration,
+   which is set by the --scenecut-window. Decrease QP for scenecut
I-Frames.
+   Default: disabled.
+
+.. option:: --scenecut-window <integer>
+
+   The duration(in milliseconds) for which the QP is incremented for
inter-frames
+   when --scenecut-aware-qp is enabled.
+   Default 500ms.
+
+   **Range of values:** 0 to 1000
+
+.. option:: --max-qp-delta <integer>
+
+   The offset by which QP is incremented for inter-frames
+   when --scenecut-aware-qp is enabled. Default 5.
+
+   **Range of values:**  0 to 10
+
 Quantization Options
 ====================

diff -r e4fea011918a -r 38865a56650c source/CMakeLists.txt
--- a/source/CMakeLists.txt Mon Oct 21 14:27:15 2019 +0530
+++ b/source/CMakeLists.txt Thu Oct 31 16:23:27 2019 +0530
@@ -29,7 +29,7 @@
 option(STATIC_LINK_CRT "Statically link C runtime for release builds" OFF)
 mark_as_advanced(FPROFILE_USE FPROFILE_GENERATE NATIVE_BUILD)
 # X265_BUILD must be incremented each time the public API is changed
-set(X265_BUILD 181)
+set(X265_BUILD 182)
 configure_file("${PROJECT_SOURCE_DIR}/x265.def.in"
                "${PROJECT_BINARY_DIR}/x265.def")
 configure_file("${PROJECT_SOURCE_DIR}/x265_config.h.in"
diff -r e4fea011918a -r 38865a56650c source/common/param.cpp
--- a/source/common/param.cpp Mon Oct 21 14:27:15 2019 +0530
+++ b/source/common/param.cpp Thu Oct 31 16:23:27 2019 +0530
@@ -175,6 +175,9 @@
     param->chunkEnd = 0;
     param->bEnableHRDConcatFlag = 0;
     param->bEnableFades = 0;
+    param->bEnableSceneCutAwareQp = 0;
+    param->scenecutWindow = 500;
+    param->maxQpDelta = 5;

     /* Intra Coding Tools */
     param->bEnableConstrainedIntra = 0;
@@ -1294,6 +1297,9 @@
             p->selectiveSAO = atoi(value);
         }
         OPT("fades") p->bEnableFades = atobool(value);
+        OPT("scenecut-aware-qp") p->bEnableSceneCutAwareQp =
atobool(value);
+        OPT("scenecut-window") p->scenecutWindow = atoi(value);
+        OPT("max-qp-delta") p->maxQpDelta = atoi(value);
         OPT("field") p->bField = atobool( value );
         OPT("cll") p->bEmitCLL = atobool(value);
         OPT("frame-dup") p->bEnableFrameDuplication = atobool(value);
@@ -1703,6 +1709,10 @@
     }
     CHECK(param->selectiveSAO < 0 || param->selectiveSAO > 4,
         "Invalid SAO tune level. Value must be between 0 and 4
(inclusive)");
+    CHECK(param->scenecutWindow < 0 || param->scenecutWindow > 1000,
+        "Invalid scenecut Window duration. Value must be between 0 and
1000(inclusive)");
+    CHECK(param->maxQpDelta < 0 || param->maxQpDelta > 10,
+        "Invalid maxQpDelta value. Value must be between 0 and 10
(inclusive)");
 #if !X86_64
     CHECK(param->searchMethod == X265_SEA && (param->sourceWidth > 840 ||
param->sourceHeight > 480),
         "SEA motion search does not support resolutions greater than 480p
in 32 bit build");
@@ -2126,6 +2136,9 @@
     BOOL(p->bEnableSvtHevc, "svt");
     BOOL(p->bField, "field");
     s += sprintf(s, " qp-adaptation-range=%.2f", p->rc.qpAdaptationRange);
+    BOOL(p->bEnableSceneCutAwareQp, "scenecut-aware-qp");
+    if (p->bEnableSceneCutAwareQp)
+        s += sprintf(s, " scenecut-window=%d max-qp-delta=%d",
p->scenecutWindow, p->maxQpDelta);
 #undef BOOL
     return buf;
 }
@@ -2463,6 +2476,9 @@
     dst->dolbyProfile = src->dolbyProfile;
     dst->bEnableSvtHevc = src->bEnableSvtHevc;
     dst->bEnableFades = src->bEnableFades;
+    dst->bEnableSceneCutAwareQp = src->bEnableSceneCutAwareQp;
+    dst->scenecutWindow = src->scenecutWindow;
+    dst->maxQpDelta = src->maxQpDelta;
     dst->bField = src->bField;

 #ifdef SVT_HEVC
diff -r e4fea011918a -r 38865a56650c source/encoder/encoder.cpp
--- a/source/encoder/encoder.cpp Mon Oct 21 14:27:15 2019 +0530
+++ b/source/encoder/encoder.cpp Thu Oct 31 16:23:27 2019 +0530
@@ -1891,7 +1891,11 @@
         /* pop a single frame from decided list, then provide to frame
encoder
          * curEncoder is guaranteed to be idle at this point */
         if (!pass)
+        {
             frameEnc = m_lookahead->getDecidedPicture();
+            if (m_param->bEnableSceneCutAwareQp && frameEnc &&
frameEnc->m_lowres.bScenecut)
+                m_rateControl->m_lastScenecut = frameEnc->m_poc;
+        }
         if (frameEnc && !pass && (!m_param->chunkEnd || (m_encodedFrameNum
< m_param->chunkEnd)))
         {
             if (m_param->analysisMultiPassRefine ||
m_param->analysisMultiPassDistortion)
diff -r e4fea011918a -r 38865a56650c source/encoder/ratecontrol.cpp
--- a/source/encoder/ratecontrol.cpp Mon Oct 21 14:27:15 2019 +0530
+++ b/source/encoder/ratecontrol.cpp Thu Oct 31 16:23:27 2019 +0530
@@ -173,6 +173,8 @@
     m_lastPredictorReset = 0;
     m_avgPFrameQp = 0;
     m_isFirstMiniGop = false;
+    m_lastScenecut = -1;
+    m_lastScenecutAwareIFrame = -1;
     if (m_param->rc.rateControlMode == X265_RC_CRF)
     {
         m_param->rc.qp = (int)m_param->rc.rfConstant;
@@ -1843,6 +1845,13 @@
             }
             rce->qpNoVbv = q;
         }
+        /* Scenecut Aware QP offsets*/
+        if (m_param->bEnableSceneCutAwareQp)
+        {
+            qScale = scenecutAwareQp(curFrame, qScale);
+            q = x265_qScale2qp(qScale);
+            rce->qpNoVbv = q;
+        }
         if (m_isVbv)
         {
             lmin = m_lastQScaleFor[P_SLICE] / m_lstep;
@@ -2097,6 +2106,12 @@
                 q = tuneQSacleForZone(rce, q);
                 q = x265_clip3(lqmin, lqmax, q);
             }
+            /* Scenecut Aware QP offsets*/
+            if (m_param->bEnableSceneCutAwareQp)
+            {
+                q = scenecutAwareQp(curFrame, q);
+                rce->qpNoVbv = x265_qScale2qp(q);
+            }
             q = clipQscale(curFrame, rce, q);


@@ -3120,3 +3135,52 @@
         buf = strstr(src, "~");
     }
 }
+
+double RateControl::scenecutAwareQp(Frame* curFrame, double q)
+{
+    uint32_t maxWindowSize = uint32_t((m_param->scenecutWindow / 1000.0) *
(m_param->fpsNum / m_param->fpsDenom) + 0.5);
+    uint32_t windowSize = maxWindowSize / 3;
+    int lastScenecut = m_top->m_rateControl->m_lastScenecut;
+    int lastIFrame = m_top->m_rateControl->m_lastScenecutAwareIFrame;
+    double maxQpDelta = x265_qp2qScale(double(m_param->maxQpDelta));
+    double iSliceDelta = x265_qp2qScale(double(I_SLICE_DELTA));
+    double sliceTypeDelta = SLICE_TYPE_DELTA * maxQpDelta;
+    double window2Delta = WINDOW2_DELTA * maxQpDelta;
+    double window3Delta = WINDOW3_DELTA * maxQpDelta;
+
+    bool isFrameInsideWindow = curFrame->m_poc > lastScenecut &&
curFrame->m_poc <= (lastScenecut + int(maxWindowSize));
+
+    if (isFrameInsideWindow &&
IS_X265_TYPE_I(curFrame->m_lowres.sliceType))
+    {
+        m_top->m_rateControl->m_lastScenecutAwareIFrame = curFrame->m_poc;
+    }
+    else if (isFrameInsideWindow && (curFrame->m_lowres.sliceType ==
X265_TYPE_P))
+    {
+        if (!(lastIFrame > lastScenecut && lastIFrame <= (lastScenecut +
int(maxWindowSize))
+            && curFrame->m_poc > lastIFrame))
+        {
+            q += maxQpDelta - sliceTypeDelta;
+            if (((curFrame->m_poc) > (lastScenecut + int(windowSize))) &&
((curFrame->m_poc) <= (lastScenecut + 2 * int(windowSize))))
+                q -= window2Delta;
+            else if (curFrame->m_poc > lastScenecut + 2 * int(windowSize))
+                q -= window3Delta;
+        }
+    }
+    else if (isFrameInsideWindow &&
IS_X265_TYPE_B(curFrame->m_lowres.sliceType))
+    {
+        if (!(lastIFrame > lastScenecut && lastIFrame <= (lastScenecut +
int(maxWindowSize))
+            && curFrame->m_poc > lastIFrame))
+        {
+            q += maxQpDelta;
+            if (curFrame->m_lowres.sliceType == X265_TYPE_B)
+                q += sliceTypeDelta;
+            if (((curFrame->m_poc) > (lastScenecut + int(windowSize))) &&
((curFrame->m_poc) <= (lastScenecut + 2 * int(windowSize))))
+                q -= window2Delta;
+            else if (curFrame->m_poc > lastScenecut + 2 * int(windowSize))
+                q -= window3Delta;
+        }
+    }
+    if (IS_X265_TYPE_I(curFrame->m_lowres.sliceType) &&
curFrame->m_lowres.bScenecut)
+        q = q - iSliceDelta;
+    return q;
+}
diff -r e4fea011918a -r 38865a56650c source/encoder/ratecontrol.h
--- a/source/encoder/ratecontrol.h Mon Oct 21 14:27:15 2019 +0530
+++ b/source/encoder/ratecontrol.h Thu Oct 31 16:23:27 2019 +0530
@@ -46,6 +46,13 @@
 #define MIN_AMORTIZE_FRACTION 0.2
 #define CLIP_DURATION(f) x265_clip3(MIN_FRAME_DURATION,
MAX_FRAME_DURATION, f)

+/*Scenecut Aware QP*/
+#define I_SLICE_DELTA           2   /* Subtracted from base QP for the
scenecut I frames*/
+#define SLICE_TYPE_DELTA        0.3 /* The offset decremented or
incremented for P-frames or b-frames respectively*/
+#define WINDOW1_DELTA           0   /* The offset for the frames coming in
the window-1*/
+#define WINDOW2_DELTA           0.3 /* The offset for the frames coming in
the window-2*/
+#define WINDOW3_DELTA           0.6 /* The offset for the frames coming in
the window-3*/
+
 struct Predictor
 {
     double coeffMin;
@@ -142,6 +149,8 @@
     bool   m_initVbv;
     int    m_lastAbrResetPoc;

+    int    m_lastScenecut;
+    int    m_lastScenecutAwareIFrame;
     double m_rateTolerance;
     double m_frameDuration;     /* current frame duration in seconds */
     double m_bitrate;
@@ -261,6 +270,8 @@
     int writeRateControlFrameStats(Frame* curFrame, RateControlEntry* rce);
     bool   initPass2();

+    double scenecutAwareQp(Frame* curFrame, double q);
+
 protected:

     static const int   s_slidingWindowFrames;
diff -r e4fea011918a -r 38865a56650c source/test/regression-tests.txt
--- a/source/test/regression-tests.txt Mon Oct 21 14:27:15 2019 +0530
+++ b/source/test/regression-tests.txt Thu Oct 31 16:23:27 2019 +0530
@@ -158,6 +158,7 @@
 ducks_take_off_420_1_720p50.y4m,--preset medium --selective-sao 4 --sao
--crf 20
 Traffic_4096x2048_30p.y4m, --preset medium --frame-dup --dup-threshold 60
--hrd --bitrate 10000 --vbv-bufsize 15000 --vbv-maxrate 12000
 Kimono1_1920x1080_24_400.yuv,--preset superfast --qp 28 --zones 0,139,q=32
+Island_960x540_420p_8bit_24fps.yuv,--no-cutree --aq-mode 0 --bitrate 6000
--scenecut-aware-qp

 # Main12 intraCost overflow bug test
 720p50_parkrun_ter.y4m,--preset medium
diff -r e4fea011918a -r 38865a56650c source/x265.h
--- a/source/x265.h Mon Oct 21 14:27:15 2019 +0530
+++ b/source/x265.h Thu Oct 31 16:23:27 2019 +0530
@@ -1832,6 +1832,18 @@
     /*Flag to indicate if rate-control history has to be reset during zone
reconfiguration.
       Default 1 (Enabled). API only. */
     int       bResetZoneConfig;
+
+    /* Increase QP for inter-frames after a scenecut is detected, for a
specified duration.
+    * Default is disabled. */
+    int       bEnableSceneCutAwareQp;
+
+    /*The duration(in milliseconds) for which the QP is incremented  for
inter-frames when bEnableSceneCutAwareQp is set.
+     * Default 500ms.*/
+    int       scenecutWindow;
+
+    /* The offset by which QP is incremented for inter-frames when
bEnableSceneCutAwareQp is set.
+     * Default is +5. */
+    int       maxQpDelta;
 } x265_param;
 /* x265_param_alloc:
  *  Allocates an x265_param instance. The returned param structure is not
diff -r e4fea011918a -r 38865a56650c source/x265cli.h
--- a/source/x265cli.h Mon Oct 21 14:27:15 2019 +0530
+++ b/source/x265cli.h Thu Oct 31 16:23:27 2019 +0530
@@ -131,6 +131,10 @@
     { "scenecut-bias",  required_argument, NULL, 0 },
     { "fades",                no_argument, NULL, 0 },
     { "no-fades",             no_argument, NULL, 0 },
+    { "scenecut-aware-qp",    no_argument, NULL, 0 },
+    { "no-scenecut-aware-qp", no_argument, NULL, 0 },
+    { "scenecut-window",required_argument, NULL, 0 },
+    { "max-qp-delta",   required_argument, NULL, 0 },
     { "radl",           required_argument, NULL, 0 },
     { "ctu-info",       required_argument, NULL, 0 },
     { "intra-refresh",        no_argument, NULL, 0 },
@@ -487,6 +491,9 @@
     H0("   --scenecut <integer>          How aggressively to insert extra
I-frames. Default %d\n", param->scenecutThreshold);
     H1("   --scenecut-bias <0..100.0>    Bias for scenecut detection.
Default %.2f\n", param->scenecutBias);
     H0("   --[no-]fades                  Enable detection and handling of
fade-in regions. Default %s\n", OPT(param->bEnableFades));
+    H1("   --[no-]scenecut-aware-qp      Enable increasing QP for frames
inside the scenecut window after scenecut. Default %s\n",
OPT(param->bEnableSceneCutAwareQp));
+    H1("   --scenecut-window <0..1000>   QP incremental duration(in
milliseconds) when scenecut-aware-qp is enabled. Default %d\n",
param->scenecutWindow);
+    H1("   --max-qp-delta <0..10>        QP offset to increment with base
QP for inter-frames. Default %d\n", param->maxQpDelta);
     H0("   --radl <integer>              Number of RADL pictures allowed
in front of IDR. Default %d\n", param->radl);
     H0("   --intra-refresh               Use Periodic Intra Refresh
instead of IDR frames\n");
     H0("   --rc-lookahead <integer>      Number of frames for frame-type
lookahead (determines encoder latency) Default %d\n",
param->lookaheadDepth);
Thanks & Regards
*Niranjan Kumar B*
Video Codec Engineer
Media & AI Analytics
+91 958 511 1449
<https://multicorewareinc.com/>


On Thu, Nov 7, 2019 at 12:02 PM Niranjan Bala <niranjan at multicorewareinc.com>
wrote:

> Please ignore the above patch(sent on Nov 5 2019).
>
> Thanks & Regards
> *Niranjan Kumar B*
> Video Codec Engineer
> Media & AI Analytics
> +91 958 511 1449
> <https://multicorewareinc.com/>
>
>
> On Tue, Nov 5, 2019 at 7:14 PM Niranjan Bala <
> niranjan at multicorewareinc.com> wrote:
>
>> # HG changeset patch
>> # User Niranjan <niranjan at multicorewareinc.com>
>> # Date 1572519207 -19800
>> #      Thu Oct 31 16:23:27 2019 +0530
>> # Node ID aa1501c6ab8063693fbcd520dd8a772946dfa39f
>> # Parent  e4fea011918a09e42bfa6d5c295cf0386c0bec7b
>> Enable Boundary Aware Frame Quantizer Selection
>>
>> CLI options:
>> 1)scenecut-aware-qp
>> 2)scenecut-window
>> 3)max-qp-delta
>>
>> diff -r e4fea011918a -r aa1501c6ab80 doc/reST/cli.rst
>> --- a/doc/reST/cli.rst Mon Oct 21 14:27:15 2019 +0530
>> +++ b/doc/reST/cli.rst Thu Oct 31 16:23:27 2019 +0530
>> @@ -1898,6 +1898,27 @@
>>
>>   **CLI ONLY**
>>
>> +.. option:: --scenecut-aware-qp, --no-scenecut-aware-qp
>> +
>> +   Increase QP for inter-frames after a scenecut is detected, for a
>> specified duration,
>> +   which is set by the --scenecut-window. Decrease QP for scenecut
>> I-Frames.
>> +   Default: disabled.
>> +
>> +.. option:: --scenecut-window <integer>
>> +
>> +   The duration(in milliseconds) for which the QP is incremented for
>> inter-frames
>> +   when --scenecut-aware-qp is enabled.
>> +   Default 500ms.
>> +
>> +   **Range of values:** 0 to 1000
>> +
>> +.. option:: --max-qp-delta <integer>
>> +
>> +   The base offset by which QP is incremented for inter-frames
>> +   when --scenecut-aware-qp is enabled. Default 5.
>> +
>> +   **Range of values:**  0 to 10
>> +
>>  Quantization Options
>>  ====================
>>
>> diff -r e4fea011918a -r aa1501c6ab80 source/CMakeLists.txt
>> --- a/source/CMakeLists.txt Mon Oct 21 14:27:15 2019 +0530
>> +++ b/source/CMakeLists.txt Thu Oct 31 16:23:27 2019 +0530
>> @@ -29,7 +29,7 @@
>>  option(STATIC_LINK_CRT "Statically link C runtime for release builds"
>> OFF)
>>  mark_as_advanced(FPROFILE_USE FPROFILE_GENERATE NATIVE_BUILD)
>>  # X265_BUILD must be incremented each time the public API is changed
>> -set(X265_BUILD 181)
>> +set(X265_BUILD 182)
>>  configure_file("${PROJECT_SOURCE_DIR}/x265.def.in"
>>                 "${PROJECT_BINARY_DIR}/x265.def")
>>  configure_file("${PROJECT_SOURCE_DIR}/x265_config.h.in"
>> diff -r e4fea011918a -r aa1501c6ab80 source/common/param.cpp
>> --- a/source/common/param.cpp Mon Oct 21 14:27:15 2019 +0530
>> +++ b/source/common/param.cpp Thu Oct 31 16:23:27 2019 +0530
>> @@ -175,6 +175,9 @@
>>      param->chunkEnd = 0;
>>      param->bEnableHRDConcatFlag = 0;
>>      param->bEnableFades = 0;
>> +    param->bEnableSceneCutAwareQp = 0;
>> +    param->scenecutWindow = 500;
>> +    param->maxQpDelta = 5;
>>
>>      /* Intra Coding Tools */
>>      param->bEnableConstrainedIntra = 0;
>> @@ -1294,6 +1297,9 @@
>>              p->selectiveSAO = atoi(value);
>>          }
>>          OPT("fades") p->bEnableFades = atobool(value);
>> +        OPT("scenecut-aware-qp") p->bEnableSceneCutAwareQp =
>> atobool(value);
>> +        OPT("scenecut-window") p->scenecutWindow = atoi(value);
>> +        OPT("max-qp-delta") p->maxQpDelta = atoi(value);
>>          OPT("field") p->bField = atobool( value );
>>          OPT("cll") p->bEmitCLL = atobool(value);
>>          OPT("frame-dup") p->bEnableFrameDuplication = atobool(value);
>> @@ -1703,6 +1709,10 @@
>>      }
>>      CHECK(param->selectiveSAO < 0 || param->selectiveSAO > 4,
>>          "Invalid SAO tune level. Value must be between 0 and 4
>> (inclusive)");
>> +    CHECK(param->scenecutWindow < 0 || param->scenecutWindow > 1000,
>> +        "Invalid scenecut Window duration. Value must be between 0 and
>> 1000(inclusive)");
>> +    CHECK(param->maxQpDelta < 0 || param->maxQpDelta > 10,
>> +        "Invalid maxQpDelta value. Value must be between 0 and 10
>> (inclusive)");
>>  #if !X86_64
>>      CHECK(param->searchMethod == X265_SEA && (param->sourceWidth > 840
>> || param->sourceHeight > 480),
>>          "SEA motion search does not support resolutions greater than
>> 480p in 32 bit build");
>> @@ -2126,6 +2136,9 @@
>>      BOOL(p->bEnableSvtHevc, "svt");
>>      BOOL(p->bField, "field");
>>      s += sprintf(s, " qp-adaptation-range=%.2f",
>> p->rc.qpAdaptationRange);
>> +    BOOL(p->bEnableSceneCutAwareQp, "scenecut-aware-qp");
>> +    if (p->bEnableSceneCutAwareQp)
>> +        s += sprintf(s, " scenecut-window=%d max-qp-delta=%d",
>> p->scenecutWindow, p->maxQpDelta);
>>  #undef BOOL
>>      return buf;
>>  }
>> @@ -2463,6 +2476,9 @@
>>      dst->dolbyProfile = src->dolbyProfile;
>>      dst->bEnableSvtHevc = src->bEnableSvtHevc;
>>      dst->bEnableFades = src->bEnableFades;
>> +    dst->bEnableSceneCutAwareQp = src->bEnableSceneCutAwareQp;
>> +    dst->scenecutWindow = src->scenecutWindow;
>> +    dst->maxQpDelta = src->maxQpDelta;
>>      dst->bField = src->bField;
>>
>>  #ifdef SVT_HEVC
>> diff -r e4fea011918a -r aa1501c6ab80 source/encoder/encoder.cpp
>> --- a/source/encoder/encoder.cpp Mon Oct 21 14:27:15 2019 +0530
>> +++ b/source/encoder/encoder.cpp Thu Oct 31 16:23:27 2019 +0530
>> @@ -1891,7 +1891,11 @@
>>          /* pop a single frame from decided list, then provide to frame
>> encoder
>>           * curEncoder is guaranteed to be idle at this point */
>>          if (!pass)
>> +        {
>>              frameEnc = m_lookahead->getDecidedPicture();
>> +            if (m_param->bEnableSceneCutAwareQp && frameEnc &&
>> frameEnc->m_lowres.bScenecut)
>> +                m_rateControl->m_lastScenecut = frameEnc->m_poc;
>> +        }
>>          if (frameEnc && !pass && (!m_param->chunkEnd ||
>> (m_encodedFrameNum < m_param->chunkEnd)))
>>          {
>>              if (m_param->analysisMultiPassRefine ||
>> m_param->analysisMultiPassDistortion)
>> diff -r e4fea011918a -r aa1501c6ab80 source/encoder/ratecontrol.cpp
>> --- a/source/encoder/ratecontrol.cpp Mon Oct 21 14:27:15 2019 +0530
>> +++ b/source/encoder/ratecontrol.cpp Thu Oct 31 16:23:27 2019 +0530
>> @@ -173,6 +173,8 @@
>>      m_lastPredictorReset = 0;
>>      m_avgPFrameQp = 0;
>>      m_isFirstMiniGop = false;
>> +    m_lastScenecut = -1;
>> +    m_lastScenecutAwareIFrame = -1;
>>      if (m_param->rc.rateControlMode == X265_RC_CRF)
>>      {
>>          m_param->rc.qp = (int)m_param->rc.rfConstant;
>> @@ -1843,6 +1845,13 @@
>>              }
>>              rce->qpNoVbv = q;
>>          }
>> +        /* Scenecut Aware QP offsets*/
>> +        if (m_param->bEnableSceneCutAwareQp)
>> +        {
>> +            qScale = scenecutAwareQp(curFrame, qScale);
>> +            q = x265_qScale2qp(qScale);
>> +            rce->qpNoVbv = q;
>> +        }
>>          if (m_isVbv)
>>          {
>>              lmin = m_lastQScaleFor[P_SLICE] / m_lstep;
>> @@ -2097,6 +2106,12 @@
>>                  q = tuneQSacleForZone(rce, q);
>>                  q = x265_clip3(lqmin, lqmax, q);
>>              }
>> +            /* Scenecut Aware QP offsets*/
>> +            if (m_param->bEnableSceneCutAwareQp)
>> +            {
>> +                q = scenecutAwareQp(curFrame, q);
>> +                rce->qpNoVbv = x265_qScale2qp(q);
>> +            }
>>              q = clipQscale(curFrame, rce, q);
>>
>>
>> @@ -3120,3 +3135,52 @@
>>          buf = strstr(src, "~");
>>      }
>>  }
>> +
>> +double RateControl::scenecutAwareQp(Frame* curFrame, double q)
>> +{
>> +    uint32_t maxWindowSize = uint32_t((m_param->scenecutWindow / 1000.0)
>> * (m_param->fpsNum / m_param->fpsDenom) + 0.5);
>> +    uint32_t windowSize = maxWindowSize / 3;
>> +    int lastScenecut = m_top->m_rateControl->m_lastScenecut;
>> +    int lastIFrame = m_top->m_rateControl->m_lastScenecutAwareIFrame;
>> +    double maxQpDelta = x265_qp2qScale(double(m_param->maxQpDelta));
>> +    double iSliceDelta = x265_qp2qScale(double(I_SLICE_DELTA));
>> +    double sliceTypeDelta = SLICE_TYPE_DELTA * maxQpDelta;
>> +    double window2Delta = WINDOW2_DELTA * maxQpDelta;
>> +    double window3Delta = WINDOW3_DELTA * maxQpDelta;
>> +
>> +    bool isFrameInsideWindow = curFrame->m_poc > lastScenecut &&
>> curFrame->m_poc <= (lastScenecut + int(maxWindowSize));
>> +
>> +    if (isFrameInsideWindow &&
>> IS_X265_TYPE_I(curFrame->m_lowres.sliceType))
>> +    {
>> +        m_top->m_rateControl->m_lastScenecutAwareIFrame =
>> curFrame->m_poc;
>> +    }
>> +    else if (isFrameInsideWindow && (curFrame->m_lowres.sliceType ==
>> X265_TYPE_P))
>> +    {
>> +        if (!(lastIFrame > lastScenecut && lastIFrame <= (lastScenecut +
>> int(maxWindowSize))
>> +            && curFrame->m_poc > lastIFrame))
>> +        {
>> +            q += maxQpDelta - sliceTypeDelta;
>> +            if (((curFrame->m_poc) > (lastScenecut + int(windowSize)))
>> && ((curFrame->m_poc) <= (lastScenecut + 2 * int(windowSize))))
>> +                q -= window2Delta;
>> +            else if (curFrame->m_poc > lastScenecut + 2 *
>> int(windowSize))
>> +                q -= window3Delta;
>> +        }
>> +    }
>> +    else if (isFrameInsideWindow &&
>> IS_X265_TYPE_B(curFrame->m_lowres.sliceType))
>> +    {
>> +        if (!(lastIFrame > lastScenecut && lastIFrame <= (lastScenecut +
>> int(maxWindowSize))
>> +            && curFrame->m_poc > lastIFrame))
>> +        {
>> +            q += maxQpDelta;
>> +            if (curFrame->m_lowres.sliceType == X265_TYPE_B)
>> +                q += sliceTypeDelta;
>> +            if (((curFrame->m_poc) > (lastScenecut + int(windowSize)))
>> && ((curFrame->m_poc) <= (lastScenecut + 2 * int(windowSize))))
>> +                q -= window2Delta;
>> +            else if (curFrame->m_poc > lastScenecut + 2 *
>> int(windowSize))
>> +                q -= window3Delta;
>> +        }
>> +    }
>> +    if (IS_X265_TYPE_I(curFrame->m_lowres.sliceType) &&
>> curFrame->m_lowres.bScenecut)
>> +        q = q - iSliceDelta;
>> +    return q;
>> +}
>> diff -r e4fea011918a -r aa1501c6ab80 source/encoder/ratecontrol.h
>> --- a/source/encoder/ratecontrol.h Mon Oct 21 14:27:15 2019 +0530
>> +++ b/source/encoder/ratecontrol.h Thu Oct 31 16:23:27 2019 +0530
>> @@ -46,6 +46,13 @@
>>  #define MIN_AMORTIZE_FRACTION 0.2
>>  #define CLIP_DURATION(f) x265_clip3(MIN_FRAME_DURATION,
>> MAX_FRAME_DURATION, f)
>>
>> +/*Scenecut Aware QP*/
>> +#define I_SLICE_DELTA           2   /* Subtracted from base QP for the
>> scenecut I frames*/
>> +#define SLICE_TYPE_DELTA        0.3 /* The offset decremented or
>> incremented for P-frames or b-frames respectively*/
>> +#define WINDOW1_DELTA           0   /* The offset for the frames coming
>> in the window-1*/
>> +#define WINDOW2_DELTA           0.3 /* The offset for the frames coming
>> in the window-2*/
>> +#define WINDOW3_DELTA           0.6 /* The offset for the frames coming
>> in the window-3*/
>> +
>>  struct Predictor
>>  {
>>      double coeffMin;
>> @@ -142,6 +149,8 @@
>>      bool   m_initVbv;
>>      int    m_lastAbrResetPoc;
>>
>> +    int    m_lastScenecut;
>> +    int    m_lastScenecutAwareIFrame;
>>      double m_rateTolerance;
>>      double m_frameDuration;     /* current frame duration in seconds */
>>      double m_bitrate;
>> @@ -261,6 +270,8 @@
>>      int writeRateControlFrameStats(Frame* curFrame, RateControlEntry*
>> rce);
>>      bool   initPass2();
>>
>> +    double scenecutAwareQp(Frame* curFrame, double q);
>> +
>>  protected:
>>
>>      static const int   s_slidingWindowFrames;
>> diff -r e4fea011918a -r aa1501c6ab80 source/test/regression-tests.txt
>> --- a/source/test/regression-tests.txt Mon Oct 21 14:27:15 2019 +0530
>> +++ b/source/test/regression-tests.txt Thu Oct 31 16:23:27 2019 +0530
>> @@ -158,6 +158,7 @@
>>  ducks_take_off_420_1_720p50.y4m,--preset medium --selective-sao 4 --sao
>> --crf 20
>>  Traffic_4096x2048_30p.y4m, --preset medium --frame-dup --dup-threshold
>> 60 --hrd --bitrate 10000 --vbv-bufsize 15000 --vbv-maxrate 12000
>>  Kimono1_1920x1080_24_400.yuv,--preset superfast --qp 28 --zones
>> 0,139,q=32
>> +Island_960x540_420p_8bit_24fps.yuv,--no-cutree --aq-mode 0 --bitrate
>> 6000 --scenecut-aware-qp
>>
>>  # Main12 intraCost overflow bug test
>>  720p50_parkrun_ter.y4m,--preset medium
>> diff -r e4fea011918a -r aa1501c6ab80 source/x265.h
>> --- a/source/x265.h Mon Oct 21 14:27:15 2019 +0530
>> +++ b/source/x265.h Thu Oct 31 16:23:27 2019 +0530
>> @@ -1832,6 +1832,18 @@
>>      /*Flag to indicate if rate-control history has to be reset during
>> zone reconfiguration.
>>        Default 1 (Enabled). API only. */
>>      int       bResetZoneConfig;
>> +
>> +    /* Increase QP for inter-frames after a scenecut is detected, for a
>> specified duration.
>> +    * Default is disabled. */
>> +    int       bEnableSceneCutAwareQp;
>> +
>> +    /*The duration(in milliseconds) for which the QP is incremented  for
>> inter-frames when bEnableSceneCutAwareQp is set.
>> +     * Default 500ms.*/
>> +    int       scenecutWindow;
>> +
>> +    /* The offset by which QP is incremented for inter-frames when
>> bEnableSceneCutAwareQp is set.
>> +     * Default is +5. */
>> +    int       maxQpDelta;
>>  } x265_param;
>>  /* x265_param_alloc:
>>   *  Allocates an x265_param instance. The returned param structure is not
>> diff -r e4fea011918a -r aa1501c6ab80 source/x265cli.h
>> --- a/source/x265cli.h Mon Oct 21 14:27:15 2019 +0530
>> +++ b/source/x265cli.h Thu Oct 31 16:23:27 2019 +0530
>> @@ -131,6 +131,10 @@
>>      { "scenecut-bias",  required_argument, NULL, 0 },
>>      { "fades",                no_argument, NULL, 0 },
>>      { "no-fades",             no_argument, NULL, 0 },
>> +    { "scenecut-aware-qp",    no_argument, NULL, 0 },
>> +    { "no-scenecut-aware-qp", no_argument, NULL, 0 },
>> +    { "scenecut-window",required_argument, NULL, 0 },
>> +    { "max-qp-delta",   required_argument, NULL, 0 },
>>      { "radl",           required_argument, NULL, 0 },
>>      { "ctu-info",       required_argument, NULL, 0 },
>>      { "intra-refresh",        no_argument, NULL, 0 },
>> @@ -487,6 +491,9 @@
>>      H0("   --scenecut <integer>          How aggressively to insert
>> extra I-frames. Default %d\n", param->scenecutThreshold);
>>      H1("   --scenecut-bias <0..100.0>    Bias for scenecut detection.
>> Default %.2f\n", param->scenecutBias);
>>      H0("   --[no-]fades                  Enable detection and handling
>> of fade-in regions. Default %s\n", OPT(param->bEnableFades));
>> +    H1("   --[no-]scenecut-aware-qp      Enable increasing QP for frames
>> inside the scenecut window after scenecut. Default %s\n",
>> OPT(param->bEnableSceneCutAwareQp));
>> +    H1("   --scenecut-window <0..1000>   QP incremental duration(in
>> milliseconds) when scenecut-aware-qp is enabled. Default %d\n",
>> param->scenecutWindow);
>> +    H1("   --max-qp-delta <0..10>        QP offset to increment with
>> base QP for inter-frames. Default %d\n", param->maxQpDelta);
>>      H0("   --radl <integer>              Number of RADL pictures allowed
>> in front of IDR. Default %d\n", param->radl);
>>      H0("   --intra-refresh               Use Periodic Intra Refresh
>> instead of IDR frames\n");
>>      H0("   --rc-lookahead <integer>      Number of frames for frame-type
>> lookahead (determines encoder latency) Default %d\n",
>> param->lookaheadDepth);
>> Thanks & Regards
>> *Niranjan Kumar B*
>> Video Codec Engineer
>> Media & AI Analytics
>> +91 958 511 1449
>> <https://multicorewareinc.com/>
>>
>
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://mailman.videolan.org/pipermail/x265-devel/attachments/20191107/1a333cf6/attachment-0001.html>
-------------- next part --------------
A non-text attachment was scrubbed...
Name: BAQ.patch
Type: application/octet-stream
Size: 14068 bytes
Desc: not available
URL: <http://mailman.videolan.org/pipermail/x265-devel/attachments/20191107/1a333cf6/attachment-0001.obj>


More information about the x265-devel mailing list