changeset 12583:04db2bfee5d6

Enable Boundary Aware Frame Quantizer Selection This patch does the following: 1)Reduce bits for frames inside the scenecut-window and increase the quality of scenecut I-frames. 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).
author Niranjan <niranjan@multicorewareinc.com>
date Thu, 31 Oct 2019 16:23:27 +0530
parents fbb3957d8b5e
children ddf9734816a8
files doc/reST/cli.rst source/CMakeLists.txt source/common/param.cpp source/encoder/encoder.cpp source/encoder/ratecontrol.cpp source/encoder/ratecontrol.h source/test/regression-tests.txt source/x265.h source/x265cli.h
diffstat 9 files changed, 145 insertions(+-), 1 deletions(-) [+]
line wrap: on
line diff
--- 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,28 @@ Quality, rate control and rate distortio
 	
 	**CLI ONLY**
 
+.. option:: --scenecut-aware-qp, --no-scenecut-aware-qp
+   
+   Enables a ratecontrol algorithm for reducing the bits spent on the inter-frames
+   within the :option:`--scenecut-window` after a scenecut by increasing their QP
+   without any deterioration in visual quality. It also increases the quality of
+   scenecut I-Frames by reducing their QP. Default disabled.
+   
+.. option:: --scenecut-window <integer>
+
+   The duration(in milliseconds) for which there is a reduction in the bits spent
+   on the inter-frames after a scenecut by increasing their QP, when
+   :option:`--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 :option:`--scenecut-aware-qp` is enabled. Default 5.
+   
+   **Range of values:**  0 to 10
+
 Quantization Options
 ====================
 
--- 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(NATIVE_BUILD "Target the build CP
 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"
--- 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 @@ void x265_param_default(x265_param* para
     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 @@ int x265_param_parse(x265_param* p, cons
             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 @@ int x265_check_params(x265_param* param)
     }
     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 @@ char *x265_param2string(x265_param* p, i
     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 @@ void x265_copy_params(x265_param* dst, x
     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
--- 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
@@ -1894,6 +1894,9 @@ int Encoder::encode(const x265_picture* 
             frameEnc = m_lookahead->getDecidedPicture();
         if (frameEnc && !pass && (!m_param->chunkEnd || (m_encodedFrameNum < m_param->chunkEnd)))
         {
+            if (m_param->bEnableSceneCutAwareQp && frameEnc->m_lowres.bScenecut)
+                m_rateControl->m_lastScenecut = frameEnc->m_poc;
+
             if (m_param->analysisMultiPassRefine || m_param->analysisMultiPassDistortion)
             {
                 uint32_t widthInCU = (m_param->sourceWidth + m_param->maxCUSize - 1) >> m_param->maxLog2CUSize;
--- 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 @@ RateControl::RateControl(x265_param& p, 
     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,16 @@ double RateControl::rateEstimateQscale(F
             }
             rce->qpNoVbv = q;
         }
+        /* Scenecut Aware QP offsets*/
+        if (m_param->bEnableSceneCutAwareQp)
+        {
+            double lqmin = m_lmin[m_sliceType];
+            double lqmax = m_lmax[m_sliceType];
+            qScale = scenecutAwareQp(curFrame, qScale);
+            qScale = x265_clip3(lqmin, lqmax, qScale);
+            q = x265_qScale2qp(qScale);
+            rce->qpNoVbv = q;
+        }
         if (m_isVbv)
         {
             lmin = m_lastQScaleFor[P_SLICE] / m_lstep;
@@ -2097,6 +2109,15 @@ double RateControl::rateEstimateQscale(F
                 q = tuneQScaleForZone(rce, q);
                 q = x265_clip3(lqmin, lqmax, q);
             }
+            /* Scenecut Aware QP offsets*/
+            if (m_param->bEnableSceneCutAwareQp)
+            {
+                double qmin = m_lmin[m_sliceType];
+                double qmax = m_lmax[m_sliceType];
+                q = scenecutAwareQp(curFrame, q);
+                q = x265_clip3(qmin, qmax, q);
+                rce->qpNoVbv = x265_qScale2qp(q);
+            }
             q = clipQscale(curFrame, rce, q);
 
 
@@ -3120,3 +3141,52 @@ void RateControl::splitbUsed(char bused[
         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;
+}
--- 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 @@ struct SPS;
 #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 @@ public:
     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 @@ public:
     int writeRateControlFrameStats(Frame* curFrame, RateControlEntry* rce);
     bool   initPass2();
 
+    double scenecutAwareQp(Frame* curFrame, double q);
+
 protected:
 
     static const int   s_slidingWindowFrames;
--- 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_720p50.y4m,--preset m
 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
--- 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,20 @@ typedef struct x265_param
     /*Flag to indicate if rate-control history has to be reset during zone reconfiguration.
       Default 1 (Enabled). API only. */
     int       bResetZoneConfig;
+
+    /* Enables a ratecontrol algorithm for reducing the bits spent on the inter-frames
+     * within the scenecutWindow after a scenecut by increasing their QP without
+     * any deterioration in visual quality. It also increases the quality of scenecut I-Frames by reducing their QP.
+     * Default is disabled. */
+    int       bEnableSceneCutAwareQp;
+
+    /*The duration(in milliseconds) for which there is a reduction in the bits spent on the inter-frames after a scenecut
+     * by increasing their QP, when bEnableSceneCutAwareQp is set. Default is 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
--- 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 @@ static const struct option long_options[
     { "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 @@ static void showHelp(x265_param *param)
     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);