<div dir="ltr">Adding CLI options. The patch has to be ignored.</div><br><div class="gmail_quote"><div dir="ltr" class="gmail_attr">On Tue, Sep 24, 2019 at 3:18 PM Niranjan Bala <<a href="mailto:niranjan@multicorewareinc.com">niranjan@multicorewareinc.com</a>> wrote:<br></div><blockquote class="gmail_quote" style="margin:0px 0px 0px 0.8ex;border-left:1px solid rgb(204,204,204);padding-left:1ex"><div dir="ltr"># HG changeset patch<br># User Niranjan <<a href="mailto:niranjan@multicorewareinc.com" target="_blank">niranjan@multicorewareinc.com</a>><br># Date 1568357212 -19800<br>#      Fri Sep 13 12:16:52 2019 +0530<br># Node ID 7e94ed7157f435a38eb7dbdaa17b4bad8832548b<br># Parent  c4b098f973e6b0ee4aee3bf0d7b54da4e2734d42<br>Enable Boundary Aware Frame Quantizer Selection<br><br>CLI options:<br>1)scenecut-aware-qp<br>2)max-qp-delta<br><br>diff -r c4b098f973e6 -r 7e94ed7157f4 doc/reST/cli.rst<br>--- a/doc/reST/cli.rst Tue Aug 13 10:51:21 2019 +0530<br>+++ b/doc/reST/cli.rst  Fri Sep 13 12:16:52 2019 +0530<br>@@ -1885,6 +1885,18 @@<br>       <br>     **CLI ONLY**<br> <br>+.. option:: --scenecut-aware-qp, --no-scenecut-aware-qp<br>+   <br>+   Increase QP for inter-frames after a scenecut is detected for a specified duration(500ms).<br>+   Decrease QP for scenecut Frames. Default: disabled.<br>+   <br>+.. option:: --max-qp-delta <integer><br>+<br>+   The base offset by which QP is incremented for inter-frames after a scenecut is detected.<br>+   Default 5.<br>+   <br>+   **Range of values:**  0 to 10<br>+<br> Quantization Options<br> ====================<br> <br>diff -r c4b098f973e6 -r 7e94ed7157f4 source/CMakeLists.txt<br>--- a/source/CMakeLists.txt        Tue Aug 13 10:51:21 2019 +0530<br>+++ b/source/CMakeLists.txt     Fri Sep 13 12:16:52 2019 +0530<br>@@ -29,7 +29,7 @@<br> option(STATIC_LINK_CRT "Statically link C runtime for release builds" OFF)<br> mark_as_advanced(FPROFILE_USE FPROFILE_GENERATE NATIVE_BUILD)<br> # X265_BUILD must be incremented each time the public API is changed<br>-set(X265_BUILD 179)<br>+set(X265_BUILD 180)<br> configure_file("${PROJECT_SOURCE_DIR}/<a href="http://x265.def.in" target="_blank">x265.def.in</a>"<br>                "${PROJECT_BINARY_DIR}/x265.def")<br> configure_file("${PROJECT_SOURCE_DIR}/<a href="http://x265_config.h.in" target="_blank">x265_config.h.in</a>"<br>diff -r c4b098f973e6 -r 7e94ed7157f4 source/common/param.cpp<br>--- a/source/common/param.cpp    Tue Aug 13 10:51:21 2019 +0530<br>+++ b/source/common/param.cpp   Fri Sep 13 12:16:52 2019 +0530<br>@@ -174,6 +174,8 @@<br>     param->chunkEnd = 0;<br>     param->bEnableHRDConcatFlag = 0;<br>     param->bEnableFades = 0;<br>+    param->bEnableSceneCutAwareQp = 0;<br>+    param->maxQpDelta = 5;<br> <br>     /* Intra Coding Tools */<br>     param->bEnableConstrainedIntra = 0;<br>@@ -1292,6 +1294,8 @@<br>             p->selectiveSAO = atoi(value);<br>         }<br>         OPT("fades") p->bEnableFades = atobool(value);<br>+        OPT("scenecut-aware-qp") p->bEnableSceneCutAwareQp = atobool(value);<br>+        OPT("max-qp-delta") p->maxQpDelta = atoi(value);<br>         OPT("field") p->bField = atobool( value );<br>         OPT("cll") p->bEmitCLL = atobool(value);<br>         OPT("hme") p->bEnableHME = atobool(value);<br>@@ -1697,6 +1701,8 @@<br>     }<br>     CHECK(param->selectiveSAO < 0 || param->selectiveSAO > 4,<br>         "Invalid SAO tune level. Value must be between 0 and 4 (inclusive)");<br>+    CHECK(param->maxQpDelta < 0 || param->maxQpDelta > 10,<br>+        "Invalid maxQpDelta value. Value must be between 0 and 10 (inclusive)");<br> #if !X86_64<br>     CHECK(param->searchMethod == X265_SEA && (param->sourceWidth > 840 || param->sourceHeight > 480),<br>         "SEA motion search does not support resolutions greater than 480p in 32 bit build");<br>@@ -2449,6 +2455,8 @@<br>     dst->dolbyProfile = src->dolbyProfile;<br>     dst->bEnableSvtHevc = src->bEnableSvtHevc;<br>     dst->bEnableFades = src->bEnableFades;<br>+    dst->bEnableSceneCutAwareQp = src->bEnableSceneCutAwareQp;<br>+    dst->maxQpDelta = src->maxQpDelta;<br>     dst->bField = src->bField;<br> <br> #ifdef SVT_HEVC<br>diff -r c4b098f973e6 -r 7e94ed7157f4 source/encoder/encoder.cpp<br>--- a/source/encoder/encoder.cpp     Tue Aug 13 10:51:21 2019 +0530<br>+++ b/source/encoder/encoder.cpp        Fri Sep 13 12:16:52 2019 +0530<br>@@ -1457,7 +1457,11 @@<br>         /* pop a single frame from decided list, then provide to frame encoder<br>          * curEncoder is guaranteed to be idle at this point */<br>         if (!pass)<br>+        {<br>             frameEnc = m_lookahead->getDecidedPicture();<br>+            if (m_param->bEnableSceneCutAwareQp && frameEnc && frameEnc->m_lowres.bScenecut)<br>+                m_rateControl->m_scenecut[m_rateControl->m_totalScenecut++] = frameEnc->m_poc;<br>+        }<br>         if (frameEnc && !pass && (!m_param->chunkEnd || (m_encodedFrameNum < m_param->chunkEnd)))<br>         {<br>             if (m_param->analysisMultiPassRefine || m_param->analysisMultiPassDistortion)<br>diff -r c4b098f973e6 -r 7e94ed7157f4 source/encoder/ratecontrol.cpp<br>--- a/source/encoder/ratecontrol.cpp  Tue Aug 13 10:51:21 2019 +0530<br>+++ b/source/encoder/ratecontrol.cpp    Fri Sep 13 12:16:52 2019 +0530<br>@@ -171,6 +171,10 @@<br>     m_lastPredictorReset = 0;<br>     m_avgPFrameQp = 0;<br>     m_isFirstMiniGop = false;<br>+    m_totalScenecut = 0;<br>+    m_totalScenecutAwareIFrames = 0;<br>+    m_scenecut = X265_MALLOC(int, m_param->totalFrames);<br>+    m_scenecutAwareIFrames = X265_MALLOC(int, m_param->totalFrames);<br>     if (m_param->rc.rateControlMode == X265_RC_CRF)<br>     {<br>         m_param->rc.qp = (int)m_param->rc.rfConstant;<br>@@ -1383,6 +1387,80 @@<br> <br>         double q = x265_qScale2qp(rateEstimateQscale(curFrame, rce));<br>         q = x265_clip3((double)m_param->rc.qpMin, (double)m_param->rc.qpMax, q);<br>+<br>+        /* Force additional QP after scenecuts*/<br>+        if (m_param->bEnableSceneCutAwareQp)<br>+        {<br>+            int maxWindowSize = int(MAX_WINDOW_DURATION * (m_param->fpsNum / m_param->fpsDenom) + 0.5);<br>+            int windowSize = maxWindowSize / 3;<br>+<br>+            for (int num = 0; num < enc->m_rateControl->m_totalScenecut; num++)<br>+            {<br>+                bool isFrameInsideWindow = curFrame->m_poc > enc->m_rateControl->m_scenecut[num] &&<br>+                    curFrame->m_poc <= (enc->m_rateControl->m_scenecut[num] + maxWindowSize);<br>+<br>+                if (isFrameInsideWindow && IS_X265_TYPE_I(curFrame->m_lowres.sliceType))<br>+                {<br>+                    enc->m_rateControl->m_scenecutAwareIFrames[enc->m_rateControl->m_totalScenecutAwareIFrames++] = curFrame->m_poc;<br>+                    break;<br>+                }<br>+                else if (isFrameInsideWindow && (curFrame->m_lowres.sliceType == X265_TYPE_P))<br>+                {<br>+                    if (enc->m_rateControl->m_totalScenecutAwareIFrames > 0)<br>+                    {<br>+                        if (enc->m_rateControl->m_scenecutAwareIFrames[enc->m_rateControl->m_totalScenecutAwareIFrames - 1] > enc->m_rateControl->m_scenecut[num] &&<br>+                            enc->m_rateControl->m_scenecutAwareIFrames[enc->m_rateControl->m_totalScenecutAwareIFrames - 1] <= (enc->m_rateControl->m_scenecut[num] + maxWindowSize)<br>+                            && curFrame->m_poc > enc->m_rateControl->m_scenecutAwareIFrames[enc->m_rateControl->m_totalScenecutAwareIFrames - 1])<br>+                        {<br>+                            break;<br>+                        }<br>+                    }<br>+                    q += m_param->maxQpDelta - int(SLICE_TYPE_DELTA * m_param->maxQpDelta);<br>+                    if (((curFrame->m_poc) > (enc->m_rateControl->m_scenecut[num] + windowSize)) &&<br>+                        ((curFrame->m_poc) <= (enc->m_rateControl->m_scenecut[num] + 2 * windowSize)))<br>+                    {<br>+                        q -= int(WINDOW2_DELTA * m_param->maxQpDelta);<br>+                    }<br>+                    else if (curFrame->m_poc > enc->m_rateControl->m_scenecut[num] + 2 * windowSize)<br>+                    {<br>+                        q -= int(WINDOW3_DELTA * m_param->maxQpDelta);<br>+                    }<br>+                    break;<br>+                }<br>+                else if (isFrameInsideWindow && IS_X265_TYPE_B(curFrame->m_lowres.sliceType))<br>+                {<br>+                    if (enc->m_rateControl->m_totalScenecutAwareIFrames > 0)<br>+                    {<br>+                        if (enc->m_rateControl->m_scenecutAwareIFrames[enc->m_rateControl->m_totalScenecutAwareIFrames - 1] > enc->m_rateControl->m_scenecut[num] &&<br>+                            enc->m_rateControl->m_scenecutAwareIFrames[enc->m_rateControl->m_totalScenecutAwareIFrames - 1] <= (enc->m_rateControl->m_scenecut[num] + maxWindowSize)<br>+                            && curFrame->m_poc > enc->m_rateControl->m_scenecutAwareIFrames[enc->m_rateControl->m_totalScenecutAwareIFrames - 1])<br>+                        {<br>+                            break;<br>+                        }<br>+                    }<br>+                    q += m_param->maxQpDelta;<br>+                    if (curFrame->m_lowres.sliceType == X265_TYPE_B)<br>+                    {<br>+                        q += int(SLICE_TYPE_DELTA * m_param->maxQpDelta);<br>+                    }<br>+                    if (((curFrame->m_poc) > (enc->m_rateControl->m_scenecut[num] + windowSize)) &&<br>+                        ((curFrame->m_poc) <= (enc->m_rateControl->m_scenecut[num] + 2 * windowSize)))<br>+                    {<br>+                        q -= int(WINDOW2_DELTA * m_param->maxQpDelta);<br>+                    }<br>+                    else if (curFrame->m_poc > enc->m_rateControl->m_scenecut[num] + 2 * windowSize)<br>+                    {<br>+                        q -= int(WINDOW3_DELTA * m_param->maxQpDelta);<br>+                    }<br>+                    break;<br>+                }<br>+            }<br>+            if (IS_X265_TYPE_I(curFrame->m_lowres.sliceType) && curFrame->m_lowres.bScenecut)<br>+            {<br>+                q = q - I_SLICE_DELTA;<br>+            }<br>+        }<br>+<br>         m_qp = int(q + 0.5);<br>         q = m_isGrainEnabled ? m_qp : q;<br>         rce->qpaRc = curEncData.m_avgQpRc = curEncData.m_avgQpAq = q;<br>@@ -1402,6 +1480,79 @@<br>             m_qp = (m_qpConstant[B_SLICE] + m_qpConstant[P_SLICE]) / 2;<br>         else<br>             m_qp = m_qpConstant[m_sliceType];<br>+<br>+        /* Force additional QP after scenecuts*/<br>+        if (m_param->bEnableSceneCutAwareQp)<br>+        {<br>+            int maxWindowSize = int(MAX_WINDOW_DURATION * (m_param->fpsNum / m_param->fpsDenom) + 0.5);<br>+            int windowSize = maxWindowSize / 3;<br>+            for (int num = 0; num < enc->m_rateControl->m_totalScenecut; num++)<br>+            {<br>+                bool isFrameInsideWindow = curFrame->m_poc > enc->m_rateControl->m_scenecut[num] &&<br>+                    curFrame->m_poc <= (enc->m_rateControl->m_scenecut[num] + maxWindowSize);<br>+<br>+                if (isFrameInsideWindow && IS_X265_TYPE_I(curFrame->m_lowres.sliceType))<br>+                {<br>+                    enc->m_rateControl->m_scenecutAwareIFrames[enc->m_rateControl->m_totalScenecutAwareIFrames++] = curFrame->m_poc;<br>+                    break;<br>+                }<br>+                else if (isFrameInsideWindow && (curFrame->m_lowres.sliceType == X265_TYPE_P))<br>+                {<br>+                    if (enc->m_rateControl->m_totalScenecutAwareIFrames > 0)<br>+                    {<br>+                        if (enc->m_rateControl->m_scenecutAwareIFrames[enc->m_rateControl->m_totalScenecutAwareIFrames - 1] > enc->m_rateControl->m_scenecut[num] &&<br>+                            enc->m_rateControl->m_scenecutAwareIFrames[enc->m_rateControl->m_totalScenecutAwareIFrames - 1] <= (enc->m_rateControl->m_scenecut[num] + maxWindowSize)<br>+                            && curFrame->m_poc > enc->m_rateControl->m_scenecutAwareIFrames[enc->m_rateControl->m_totalScenecutAwareIFrames - 1])<br>+                        {<br>+                            break;<br>+                        }<br>+                    }<br>+                    m_qp += m_param->maxQpDelta - int(SLICE_TYPE_DELTA * m_param->maxQpDelta);<br>+                    if (((curFrame->m_poc) > (enc->m_rateControl->m_scenecut[num] + windowSize)) &&<br>+                        ((curFrame->m_poc) <= (enc->m_rateControl->m_scenecut[num] + 2 * windowSize)))<br>+                    {<br>+                        m_qp -= int(WINDOW2_DELTA * m_param->maxQpDelta);<br>+                    }<br>+                    else if (curFrame->m_poc > enc->m_rateControl->m_scenecut[num] + 2 * windowSize)<br>+                    {<br>+                        m_qp -= int(WINDOW3_DELTA * m_param->maxQpDelta);<br>+                    }<br>+                    break;<br>+                }<br>+                else if (isFrameInsideWindow && IS_X265_TYPE_B(curFrame->m_lowres.sliceType))<br>+                {<br>+                    if (enc->m_rateControl->m_totalScenecutAwareIFrames > 0)<br>+                    {<br>+                        if (enc->m_rateControl->m_scenecutAwareIFrames[enc->m_rateControl->m_totalScenecutAwareIFrames - 1] > enc->m_rateControl->m_scenecut[num] &&<br>+                            enc->m_rateControl->m_scenecutAwareIFrames[enc->m_rateControl->m_totalScenecutAwareIFrames - 1] <= (enc->m_rateControl->m_scenecut[num] + maxWindowSize)<br>+                            && curFrame->m_poc > enc->m_rateControl->m_scenecutAwareIFrames[enc->m_rateControl->m_totalScenecutAwareIFrames - 1])<br>+                        {<br>+                            break;<br>+                        }<br>+                    }<br>+                    m_qp += m_param->maxQpDelta;<br>+                    if (curFrame->m_lowres.sliceType == X265_TYPE_B)<br>+                    {<br>+                        m_qp += int(SLICE_TYPE_DELTA * m_param->maxQpDelta);<br>+                    }<br>+                    if (((curFrame->m_poc) > (enc->m_rateControl->m_scenecut[num] + windowSize)) &&<br>+                        ((curFrame->m_poc) <= (enc->m_rateControl->m_scenecut[num] + 2 * windowSize)))<br>+                    {<br>+                        m_qp -= int(WINDOW2_DELTA * m_param->maxQpDelta);<br>+                    }<br>+                    else if (curFrame->m_poc > enc->m_rateControl->m_scenecut[num] + 2 * windowSize)<br>+                    {<br>+                        m_qp -= int(WINDOW3_DELTA * m_param->maxQpDelta);<br>+                    }<br>+                    break;<br>+                }<br>+            }<br>+            if (IS_X265_TYPE_I(curFrame->m_lowres.sliceType) && curFrame->m_lowres.bScenecut)<br>+            {<br>+                m_qp = m_qp - I_SLICE_DELTA;<br>+            }<br>+        }<br>+<br>         curEncData.m_avgQpAq = curEncData.m_avgQpRc = m_qp;<br>         <br>         x265_zone* zone = getZone();<br>diff -r c4b098f973e6 -r 7e94ed7157f4 source/encoder/ratecontrol.h<br>--- a/source/encoder/ratecontrol.h   Tue Aug 13 10:51:21 2019 +0530<br>+++ b/source/encoder/ratecontrol.h      Fri Sep 13 12:16:52 2019 +0530<br>@@ -46,6 +46,14 @@<br> #define MIN_AMORTIZE_FRACTION 0.2<br> #define CLIP_DURATION(f) x265_clip3(MIN_FRAME_DURATION, MAX_FRAME_DURATION, f)<br> <br>+/*Scenecut Aware QP*/<br>+#define MAX_WINDOW_DURATION     0.5 /*Duration of the scenecut window in seconds*/<br>+#define I_SLICE_DELTA           2   /*Subtracted from the QP for the scenecut I frames*/<br>+#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 */<br>+#define WINDOW1_DELTA           0   /* The factor by which QP is decremented for the frames coming in the window-1 after a scenecut is detected*/<br>+#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*/<br>+#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*/<br>+<br> struct Predictor<br> {<br>     double coeffMin;<br>@@ -138,6 +146,10 @@<br>     bool   m_initVbv;<br>     int    m_lastAbrResetPoc;<br> <br>+    int    *m_scenecut;<br>+    int    m_totalScenecut;<br>+    int    *m_scenecutAwareIFrames;<br>+    int    m_totalScenecutAwareIFrames;<br>     double m_rateTolerance;<br>     double m_frameDuration;     /* current frame duration in seconds */<br>     double m_bitrate;<br>diff -r c4b098f973e6 -r 7e94ed7157f4 source/test/regression-tests.txt<br>--- a/source/test/regression-tests.txt       Tue Aug 13 10:51:21 2019 +0530<br>+++ b/source/test/regression-tests.txt  Fri Sep 13 12:16:52 2019 +0530<br>@@ -156,6 +156,7 @@<br> 720p50_parkrun_ter.y4m,--preset medium --bitrate 400 --hme<br> ducks_take_off_420_1_720p50.y4m,--preset medium --aq-mode 4 --crf 22 --no-cutree<br> ducks_take_off_420_1_720p50.y4m,--preset medium --selective-sao 4 --sao --crf 20<br>+Kimono1_1920x1080_24_400.yuv,--no-cutree --aq-mode 0 --bitrate 6000 --scenecut-aware-qp<br> <br> # Main12 intraCost overflow bug test<br> 720p50_parkrun_ter.y4m,--preset medium<br>diff -r c4b098f973e6 -r 7e94ed7157f4 source/x265.h<br>--- a/source/x265.h      Tue Aug 13 10:51:21 2019 +0530<br>+++ b/source/x265.h     Fri Sep 13 12:16:52 2019 +0530<br>@@ -588,6 +588,7 @@<br> #define X265_ANALYSIS_SAVE 1<br> #define X265_ANALYSIS_LOAD 2<br> <br>+<br> typedef struct x265_cli_csp<br> {<br>     int planes;<br>@@ -1798,6 +1799,14 @@<br> <br>     /*Emit content light level info SEI*/<br>     int         bEmitCLL;<br>+<br>+    /* Increase QP for inter-frames after a scenecut is detected for a specified duration.<br>+     * Default is disabled. */<br>+    int       bEnableSceneCutAwareQp;<br>+<br>+    /* The value by which QP is incremented for inter-frames after a scenecut is detected <br>+     * when bEnableSceneCutAwareQp is set. Default is +5. */<br>+    int       maxQpDelta;<br> } x265_param;<br> /* x265_param_alloc:<br>  *  Allocates an x265_param instance. The returned param structure is not<br>diff -r c4b098f973e6 -r 7e94ed7157f4 source/x265cli.h<br>--- a/source/x265cli.h   Tue Aug 13 10:51:21 2019 +0530<br>+++ b/source/x265cli.h  Fri Sep 13 12:16:52 2019 +0530<br>@@ -131,6 +131,9 @@<br>     { "scenecut-bias",  required_argument, NULL, 0 },<br>     { "fades",                no_argument, NULL, 0 },<br>     { "no-fades",             no_argument, NULL, 0 },<br>+    { "scenecut-aware-qp",    no_argument, NULL, 0 },<br>+    { "no-scenecut-aware-qp", no_argument, NULL, 0 },<br>+    { "max-qp-delta",   required_argument, NULL, 0 },<br>     { "radl",           required_argument, NULL, 0 },<br>     { "ctu-info",       required_argument, NULL, 0 },<br>     { "intra-refresh",        no_argument, NULL, 0 },<br>@@ -484,6 +487,8 @@<br>     H0("   --scenecut <integer>          How aggressively to insert extra I-frames. Default %d\n", param->scenecutThreshold);<br>     H1("   --scenecut-bias <0..100.0>    Bias for scenecut detection. Default %.2f\n", param->scenecutBias);<br>     H0("   --[no-]fades                  Enable detection and handling of fade-in regions. Default %s\n", OPT(param->bEnableFades));<br>+    H1("   --[no-]scenecut-aware-qp      Enable increasing QP for frames after scenecut for a certain period. Default %s\n", OPT(param->bEnableSceneCutAwareQp));<br>+    H1("   --max-qp-delta <0..10>        QP incremental value to add-on with original QP for inter-frames. Default %d\n", param->maxQpDelta);<br>     H0("   --radl <integer>              Number of RADL pictures allowed in front of IDR. Default %d\n", param->radl);<br>     H0("   --intra-refresh               Use Periodic Intra Refresh instead of IDR frames\n");<br>     H0("   --rc-lookahead <integer>      Number of frames for frame-type lookahead (determines encoder latency) Default %d\n", param->lookaheadDepth);<br><div><br></div>-- <br><div dir="ltr" class="gmail-m_760998275523891302gmail_signature"><div dir="ltr"><div><div dir="ltr"><div><div dir="ltr"><span style="background-color:rgb(255,255,255)"><font color="#000000">Thanks,</font></span><div><span style="background-color:rgb(255,255,255)"><font color="#000000">Niranjan Kumar</font></span></div></div></div></div></div></div></div></div>
</blockquote></div><br clear="all"><div><br></div>-- <br><div dir="ltr" class="gmail_signature"><div dir="ltr"><div><div dir="ltr"><div><div dir="ltr"><span style="background-color:rgb(255,255,255)"><font color="#000000">Thanks,</font></span><div><span style="background-color:rgb(255,255,255)"><font color="#000000">Niranjan Kumar</font></span></div></div></div></div></div></div></div>