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

Niranjan Bala niranjan at multicorewareinc.com
Tue Sep 24 11:48:09 CEST 2019


# HG changeset patch
# User Niranjan <niranjan at multicorewareinc.com>
# Date 1568357212 -19800
#      Fri Sep 13 12:16:52 2019 +0530
# Node ID 7e94ed7157f435a38eb7dbdaa17b4bad8832548b
# Parent  c4b098f973e6b0ee4aee3bf0d7b54da4e2734d42
Enable Boundary Aware Frame Quantizer Selection

CLI options:
1)scenecut-aware-qp
2)max-qp-delta

diff -r c4b098f973e6 -r 7e94ed7157f4 doc/reST/cli.rst
--- a/doc/reST/cli.rst Tue Aug 13 10:51:21 2019 +0530
+++ b/doc/reST/cli.rst Fri Sep 13 12:16:52 2019 +0530
@@ -1885,6 +1885,18 @@

  **CLI ONLY**

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

diff -r c4b098f973e6 -r 7e94ed7157f4 source/CMakeLists.txt
--- a/source/CMakeLists.txt Tue Aug 13 10:51:21 2019 +0530
+++ b/source/CMakeLists.txt Fri Sep 13 12:16:52 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 179)
+set(X265_BUILD 180)
 configure_file("${PROJECT_SOURCE_DIR}/x265.def.in"
                "${PROJECT_BINARY_DIR}/x265.def")
 configure_file("${PROJECT_SOURCE_DIR}/x265_config.h.in"
diff -r c4b098f973e6 -r 7e94ed7157f4 source/common/param.cpp
--- a/source/common/param.cpp Tue Aug 13 10:51:21 2019 +0530
+++ b/source/common/param.cpp Fri Sep 13 12:16:52 2019 +0530
@@ -174,6 +174,8 @@
     param->chunkEnd = 0;
     param->bEnableHRDConcatFlag = 0;
     param->bEnableFades = 0;
+    param->bEnableSceneCutAwareQp = 0;
+    param->maxQpDelta = 5;

     /* Intra Coding Tools */
     param->bEnableConstrainedIntra = 0;
@@ -1292,6 +1294,8 @@
             p->selectiveSAO = atoi(value);
         }
         OPT("fades") p->bEnableFades = atobool(value);
+        OPT("scenecut-aware-qp") p->bEnableSceneCutAwareQp =
atobool(value);
+        OPT("max-qp-delta") p->maxQpDelta = atoi(value);
         OPT("field") p->bField = atobool( value );
         OPT("cll") p->bEmitCLL = atobool(value);
         OPT("hme") p->bEnableHME = atobool(value);
@@ -1697,6 +1701,8 @@
     }
     CHECK(param->selectiveSAO < 0 || param->selectiveSAO > 4,
         "Invalid SAO tune level. Value must be between 0 and 4
(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");
@@ -2449,6 +2455,8 @@
     dst->dolbyProfile = src->dolbyProfile;
     dst->bEnableSvtHevc = src->bEnableSvtHevc;
     dst->bEnableFades = src->bEnableFades;
+    dst->bEnableSceneCutAwareQp = src->bEnableSceneCutAwareQp;
+    dst->maxQpDelta = src->maxQpDelta;
     dst->bField = src->bField;

 #ifdef SVT_HEVC
diff -r c4b098f973e6 -r 7e94ed7157f4 source/encoder/encoder.cpp
--- a/source/encoder/encoder.cpp Tue Aug 13 10:51:21 2019 +0530
+++ b/source/encoder/encoder.cpp Fri Sep 13 12:16:52 2019 +0530
@@ -1457,7 +1457,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_scenecut[m_rateControl->m_totalScenecut++] =
frameEnc->m_poc;
+        }
         if (frameEnc && !pass && (!m_param->chunkEnd || (m_encodedFrameNum
< m_param->chunkEnd)))
         {
             if (m_param->analysisMultiPassRefine ||
m_param->analysisMultiPassDistortion)
diff -r c4b098f973e6 -r 7e94ed7157f4 source/encoder/ratecontrol.cpp
--- a/source/encoder/ratecontrol.cpp Tue Aug 13 10:51:21 2019 +0530
+++ b/source/encoder/ratecontrol.cpp Fri Sep 13 12:16:52 2019 +0530
@@ -171,6 +171,10 @@
     m_lastPredictorReset = 0;
     m_avgPFrameQp = 0;
     m_isFirstMiniGop = false;
+    m_totalScenecut = 0;
+    m_totalScenecutAwareIFrames = 0;
+    m_scenecut = X265_MALLOC(int, m_param->totalFrames);
+    m_scenecutAwareIFrames = X265_MALLOC(int, m_param->totalFrames);
     if (m_param->rc.rateControlMode == X265_RC_CRF)
     {
         m_param->rc.qp = (int)m_param->rc.rfConstant;
@@ -1383,6 +1387,80 @@

         double q = x265_qScale2qp(rateEstimateQscale(curFrame, rce));
         q = x265_clip3((double)m_param->rc.qpMin,
(double)m_param->rc.qpMax, q);
+
+        /* Force additional QP after scenecuts*/
+        if (m_param->bEnableSceneCutAwareQp)
+        {
+            int maxWindowSize = int(MAX_WINDOW_DURATION * (m_param->fpsNum
/ m_param->fpsDenom) + 0.5);
+            int windowSize = maxWindowSize / 3;
+
+            for (int num = 0; num < enc->m_rateControl->m_totalScenecut;
num++)
+            {
+                bool isFrameInsideWindow = curFrame->m_poc >
enc->m_rateControl->m_scenecut[num] &&
+                    curFrame->m_poc <=
(enc->m_rateControl->m_scenecut[num] + maxWindowSize);
+
+                if (isFrameInsideWindow &&
IS_X265_TYPE_I(curFrame->m_lowres.sliceType))
+                {
+
 enc->m_rateControl->m_scenecutAwareIFrames[enc->m_rateControl->m_totalScenecutAwareIFrames++]
= curFrame->m_poc;
+                    break;
+                }
+                else if (isFrameInsideWindow &&
(curFrame->m_lowres.sliceType == X265_TYPE_P))
+                {
+                    if (enc->m_rateControl->m_totalScenecutAwareIFrames >
0)
+                    {
+                        if
(enc->m_rateControl->m_scenecutAwareIFrames[enc->m_rateControl->m_totalScenecutAwareIFrames
- 1] > enc->m_rateControl->m_scenecut[num] &&
+
 enc->m_rateControl->m_scenecutAwareIFrames[enc->m_rateControl->m_totalScenecutAwareIFrames
- 1] <= (enc->m_rateControl->m_scenecut[num] + maxWindowSize)
+                            && curFrame->m_poc >
enc->m_rateControl->m_scenecutAwareIFrames[enc->m_rateControl->m_totalScenecutAwareIFrames
- 1])
+                        {
+                            break;
+                        }
+                    }
+                    q += m_param->maxQpDelta - int(SLICE_TYPE_DELTA *
m_param->maxQpDelta);
+                    if (((curFrame->m_poc) >
(enc->m_rateControl->m_scenecut[num] + windowSize)) &&
+                        ((curFrame->m_poc) <=
(enc->m_rateControl->m_scenecut[num] + 2 * windowSize)))
+                    {
+                        q -= int(WINDOW2_DELTA * m_param->maxQpDelta);
+                    }
+                    else if (curFrame->m_poc >
enc->m_rateControl->m_scenecut[num] + 2 * windowSize)
+                    {
+                        q -= int(WINDOW3_DELTA * m_param->maxQpDelta);
+                    }
+                    break;
+                }
+                else if (isFrameInsideWindow &&
IS_X265_TYPE_B(curFrame->m_lowres.sliceType))
+                {
+                    if (enc->m_rateControl->m_totalScenecutAwareIFrames >
0)
+                    {
+                        if
(enc->m_rateControl->m_scenecutAwareIFrames[enc->m_rateControl->m_totalScenecutAwareIFrames
- 1] > enc->m_rateControl->m_scenecut[num] &&
+
 enc->m_rateControl->m_scenecutAwareIFrames[enc->m_rateControl->m_totalScenecutAwareIFrames
- 1] <= (enc->m_rateControl->m_scenecut[num] + maxWindowSize)
+                            && curFrame->m_poc >
enc->m_rateControl->m_scenecutAwareIFrames[enc->m_rateControl->m_totalScenecutAwareIFrames
- 1])
+                        {
+                            break;
+                        }
+                    }
+                    q += m_param->maxQpDelta;
+                    if (curFrame->m_lowres.sliceType == X265_TYPE_B)
+                    {
+                        q += int(SLICE_TYPE_DELTA * m_param->maxQpDelta);
+                    }
+                    if (((curFrame->m_poc) >
(enc->m_rateControl->m_scenecut[num] + windowSize)) &&
+                        ((curFrame->m_poc) <=
(enc->m_rateControl->m_scenecut[num] + 2 * windowSize)))
+                    {
+                        q -= int(WINDOW2_DELTA * m_param->maxQpDelta);
+                    }
+                    else if (curFrame->m_poc >
enc->m_rateControl->m_scenecut[num] + 2 * windowSize)
+                    {
+                        q -= int(WINDOW3_DELTA * m_param->maxQpDelta);
+                    }
+                    break;
+                }
+            }
+            if (IS_X265_TYPE_I(curFrame->m_lowres.sliceType) &&
curFrame->m_lowres.bScenecut)
+            {
+                q = q - I_SLICE_DELTA;
+            }
+        }
+
         m_qp = int(q + 0.5);
         q = m_isGrainEnabled ? m_qp : q;
         rce->qpaRc = curEncData.m_avgQpRc = curEncData.m_avgQpAq = q;
@@ -1402,6 +1480,79 @@
             m_qp = (m_qpConstant[B_SLICE] + m_qpConstant[P_SLICE]) / 2;
         else
             m_qp = m_qpConstant[m_sliceType];
+
+        /* Force additional QP after scenecuts*/
+        if (m_param->bEnableSceneCutAwareQp)
+        {
+            int maxWindowSize = int(MAX_WINDOW_DURATION * (m_param->fpsNum
/ m_param->fpsDenom) + 0.5);
+            int windowSize = maxWindowSize / 3;
+            for (int num = 0; num < enc->m_rateControl->m_totalScenecut;
num++)
+            {
+                bool isFrameInsideWindow = curFrame->m_poc >
enc->m_rateControl->m_scenecut[num] &&
+                    curFrame->m_poc <=
(enc->m_rateControl->m_scenecut[num] + maxWindowSize);
+
+                if (isFrameInsideWindow &&
IS_X265_TYPE_I(curFrame->m_lowres.sliceType))
+                {
+
 enc->m_rateControl->m_scenecutAwareIFrames[enc->m_rateControl->m_totalScenecutAwareIFrames++]
= curFrame->m_poc;
+                    break;
+                }
+                else if (isFrameInsideWindow &&
(curFrame->m_lowres.sliceType == X265_TYPE_P))
+                {
+                    if (enc->m_rateControl->m_totalScenecutAwareIFrames >
0)
+                    {
+                        if
(enc->m_rateControl->m_scenecutAwareIFrames[enc->m_rateControl->m_totalScenecutAwareIFrames
- 1] > enc->m_rateControl->m_scenecut[num] &&
+
 enc->m_rateControl->m_scenecutAwareIFrames[enc->m_rateControl->m_totalScenecutAwareIFrames
- 1] <= (enc->m_rateControl->m_scenecut[num] + maxWindowSize)
+                            && curFrame->m_poc >
enc->m_rateControl->m_scenecutAwareIFrames[enc->m_rateControl->m_totalScenecutAwareIFrames
- 1])
+                        {
+                            break;
+                        }
+                    }
+                    m_qp += m_param->maxQpDelta - int(SLICE_TYPE_DELTA *
m_param->maxQpDelta);
+                    if (((curFrame->m_poc) >
(enc->m_rateControl->m_scenecut[num] + windowSize)) &&
+                        ((curFrame->m_poc) <=
(enc->m_rateControl->m_scenecut[num] + 2 * windowSize)))
+                    {
+                        m_qp -= int(WINDOW2_DELTA * m_param->maxQpDelta);
+                    }
+                    else if (curFrame->m_poc >
enc->m_rateControl->m_scenecut[num] + 2 * windowSize)
+                    {
+                        m_qp -= int(WINDOW3_DELTA * m_param->maxQpDelta);
+                    }
+                    break;
+                }
+                else if (isFrameInsideWindow &&
IS_X265_TYPE_B(curFrame->m_lowres.sliceType))
+                {
+                    if (enc->m_rateControl->m_totalScenecutAwareIFrames >
0)
+                    {
+                        if
(enc->m_rateControl->m_scenecutAwareIFrames[enc->m_rateControl->m_totalScenecutAwareIFrames
- 1] > enc->m_rateControl->m_scenecut[num] &&
+
 enc->m_rateControl->m_scenecutAwareIFrames[enc->m_rateControl->m_totalScenecutAwareIFrames
- 1] <= (enc->m_rateControl->m_scenecut[num] + maxWindowSize)
+                            && curFrame->m_poc >
enc->m_rateControl->m_scenecutAwareIFrames[enc->m_rateControl->m_totalScenecutAwareIFrames
- 1])
+                        {
+                            break;
+                        }
+                    }
+                    m_qp += m_param->maxQpDelta;
+                    if (curFrame->m_lowres.sliceType == X265_TYPE_B)
+                    {
+                        m_qp += int(SLICE_TYPE_DELTA *
m_param->maxQpDelta);
+                    }
+                    if (((curFrame->m_poc) >
(enc->m_rateControl->m_scenecut[num] + windowSize)) &&
+                        ((curFrame->m_poc) <=
(enc->m_rateControl->m_scenecut[num] + 2 * windowSize)))
+                    {
+                        m_qp -= int(WINDOW2_DELTA * m_param->maxQpDelta);
+                    }
+                    else if (curFrame->m_poc >
enc->m_rateControl->m_scenecut[num] + 2 * windowSize)
+                    {
+                        m_qp -= int(WINDOW3_DELTA * m_param->maxQpDelta);
+                    }
+                    break;
+                }
+            }
+            if (IS_X265_TYPE_I(curFrame->m_lowres.sliceType) &&
curFrame->m_lowres.bScenecut)
+            {
+                m_qp = m_qp - I_SLICE_DELTA;
+            }
+        }
+
         curEncData.m_avgQpAq = curEncData.m_avgQpRc = m_qp;

         x265_zone* zone = getZone();
diff -r c4b098f973e6 -r 7e94ed7157f4 source/encoder/ratecontrol.h
--- a/source/encoder/ratecontrol.h Tue Aug 13 10:51:21 2019 +0530
+++ b/source/encoder/ratecontrol.h Fri Sep 13 12:16:52 2019 +0530
@@ -46,6 +46,14 @@
 #define MIN_AMORTIZE_FRACTION 0.2
 #define CLIP_DURATION(f) x265_clip3(MIN_FRAME_DURATION,
MAX_FRAME_DURATION, f)

+/*Scenecut Aware QP*/
+#define MAX_WINDOW_DURATION     0.5 /*Duration of the scenecut window in
seconds*/
+#define I_SLICE_DELTA           2   /*Subtracted from the QP for the
scenecut I frames*/
+#define SLICE_TYPE_DELTA        0.3 /* The factor by which maxQpDelta is
incremented or decremented for the b-frames or P-frames respectively, after
a scenecut is detected */
+#define WINDOW1_DELTA           0   /* The factor by which QP is
decremented for the frames coming in the window-1 after a scenecut is
detected*/
+#define WINDOW2_DELTA           0.3 /* The factor by which QP is
decremented for the frames coming in the window-2 after a scenecut is
detected*/
+#define WINDOW3_DELTA           0.6 /* The factor by which QP is
decremented for the frames coming in the window-3 after a scenecut is
detected*/
+
 struct Predictor
 {
     double coeffMin;
@@ -138,6 +146,10 @@
     bool   m_initVbv;
     int    m_lastAbrResetPoc;

+    int    *m_scenecut;
+    int    m_totalScenecut;
+    int    *m_scenecutAwareIFrames;
+    int    m_totalScenecutAwareIFrames;
     double m_rateTolerance;
     double m_frameDuration;     /* current frame duration in seconds */
     double m_bitrate;
diff -r c4b098f973e6 -r 7e94ed7157f4 source/test/regression-tests.txt
--- a/source/test/regression-tests.txt Tue Aug 13 10:51:21 2019 +0530
+++ b/source/test/regression-tests.txt Fri Sep 13 12:16:52 2019 +0530
@@ -156,6 +156,7 @@
 720p50_parkrun_ter.y4m,--preset medium --bitrate 400 --hme
 ducks_take_off_420_1_720p50.y4m,--preset medium --aq-mode 4 --crf 22
--no-cutree
 ducks_take_off_420_1_720p50.y4m,--preset medium --selective-sao 4 --sao
--crf 20
+Kimono1_1920x1080_24_400.yuv,--no-cutree --aq-mode 0 --bitrate 6000
--scenecut-aware-qp

 # Main12 intraCost overflow bug test
 720p50_parkrun_ter.y4m,--preset medium
diff -r c4b098f973e6 -r 7e94ed7157f4 source/x265.h
--- a/source/x265.h Tue Aug 13 10:51:21 2019 +0530
+++ b/source/x265.h Fri Sep 13 12:16:52 2019 +0530
@@ -588,6 +588,7 @@
 #define X265_ANALYSIS_SAVE 1
 #define X265_ANALYSIS_LOAD 2

+
 typedef struct x265_cli_csp
 {
     int planes;
@@ -1798,6 +1799,14 @@

     /*Emit content light level info SEI*/
     int         bEmitCLL;
+
+    /* Increase QP for inter-frames after a scenecut is detected for a
specified duration.
+     * Default is disabled. */
+    int       bEnableSceneCutAwareQp;
+
+    /* The value by which QP is incremented for inter-frames after a
scenecut is detected
+     * 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 c4b098f973e6 -r 7e94ed7157f4 source/x265cli.h
--- a/source/x265cli.h Tue Aug 13 10:51:21 2019 +0530
+++ b/source/x265cli.h Fri Sep 13 12:16:52 2019 +0530
@@ -131,6 +131,9 @@
     { "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 },
+    { "max-qp-delta",   required_argument, NULL, 0 },
     { "radl",           required_argument, NULL, 0 },
     { "ctu-info",       required_argument, NULL, 0 },
     { "intra-refresh",        no_argument, NULL, 0 },
@@ -484,6 +487,8 @@
     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
after scenecut for a certain period. Default %s\n",
OPT(param->bEnableSceneCutAwareQp));
+    H1("   --max-qp-delta <0..10>        QP incremental value to add-on
with original 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,
Niranjan Kumar
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://mailman.videolan.org/pipermail/x265-devel/attachments/20190924/355dab17/attachment-0001.html>
-------------- next part --------------
A non-text attachment was scrubbed...
Name: BAQ.patch
Type: application/octet-stream
Size: 18507 bytes
Desc: not available
URL: <http://mailman.videolan.org/pipermail/x265-devel/attachments/20190924/355dab17/attachment-0001.obj>


More information about the x265-devel mailing list