<div dir="ltr">I have added the review comments in the following patch. Please make use of that.<br clear="all"><div><div dir="ltr" class="gmail_signature" data-smartmail="gmail_signature"><div dir="ltr"><div><div dir="ltr"><div dir="ltr"><div dir="ltr"><font color="#0c343d" face="verdana, sans-serif"><br></font></div><div dir="ltr"><font color="#0c343d" face="verdana, sans-serif">Thanks & Regards</font><div><font color="#0c343d" face="verdana, sans-serif"><b>Niranjan Kumar B</b></font></div><div><font size="1" color="#0c343d" face="verdana, sans-serif">Video Codec Engineer </font></div><div><font size="1" color="#0c343d" face="verdana, sans-serif">Media & AI Analytics</font></div><div><font face="trebuchet ms, sans-serif" color="#0c343d">+91 958 511 1449</font></div><div><a href="https://multicorewareinc.com/" style="color:rgb(17,85,204)" target="_blank"><img src="https://docs.google.com/uc?export=download&id=1kc3RJu9M8bnIf6Xa5rUw2d-eEVUsPBE5&revid=0B7tw9XJBmynaemR1VUpQUi9DVytRVW5SVkRwVTFjb1hBMUcwPQ"></a></div></div></div></div></div></div></div></div><br></div><br><div class="gmail_quote"><div dir="ltr" class="gmail_attr">On Wed, Jul 1, 2020 at 10:05 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">From ae17b54f880eb900938ff6ac780dfc3fb6c38228 Mon Sep 17 00:00:00 2001<br>From: Niranjan <<a href="mailto:niranjan@multicorewareinc.com" target="_blank">niranjan@multicorewareinc.com</a>><br>Date: Wed, 1 Jul 2020 21:23:42 +0530<br>Subject: [PATCH] Improve: Scenecut Aware Frame Quantizer Selection<br><br>This patch does the following:<br>1)Reduce bits for frames before the scenecut<br>2)Refactor Scenecut Aware Frame Quantizer Selection<br>3)Add option "--qp-delta-nonref" to set offset for<br>non-referenced inter frames(optional).<br>4)Enables Scenecut Aware Frame Quantizer Selection<br>to run only with pass 2<br>---<br> doc/reST/cli.rst                   | 20 +++++--<br> source/CMakeLists.txt              |  2 +-<br> source/common/frame.cpp            |  1 +<br> source/common/frame.h              |  2 +<br> source/common/param.cpp            | 22 ++++++--<br> source/encoder/encoder.cpp         | 37 +++++++++++-<br> source/encoder/ratecontrol.cpp     | 90 +++++++++++++++++++++---------<br> source/encoder/ratecontrol.h       |  4 +-<br> source/test/rate-control-tests.txt |  2 +<br> source/test/regression-tests.txt   |  1 -<br> source/x265.h                      | 12 ++--<br> source/x265cli.cpp                 |  3 +-<br> source/x265cli.h                   |  3 +-<br> 13 files changed, 149 insertions(+), 50 deletions(-)<br><br>diff --git a/doc/reST/cli.rst b/doc/reST/cli.rst<br>index 3b8a6e2e6..27fd29ec0 100644<br>--- a/doc/reST/cli.rst<br>+++ b/doc/reST/cli.rst<br>@@ -1948,10 +1948,11 @@ Quality, rate control and rate distortion options<br> <br> .. option:: --scenecut-aware-qp, --no-scenecut-aware-qp<br>    <br>-   Enables a ratecontrol algorithm for reducing the bits spent on the inter-frames<br>-   within the :option:`--scenecut-window` after a scenecut by increasing their QP<br>-   without any deterioration in visual quality. It also increases the quality of<br>-   scenecut I-Frames by reducing their QP. Default disabled.<br>+   It reduces the bits spent on the inter-frames within the :option:`--scenecut-window`<br>+   before and after a scenecut by increasing their QP in ratecontrol pass2 algorithm<br>+   without any deterioration in visual quality. If a scenecut falls within the window,<br>+   the QP of the inter-frames after this scenecut will not be modified. <br>+   :option:`--scenecut-aware-qp` works only with --pass 2. Default disabled.<br>    <br> .. option:: --scenecut-window <integer><br> <br>@@ -1961,12 +1962,21 @@ Quality, rate control and rate distortion options<br>    <br>    **Range of values:** 0 to 1000<br>    <br>-.. option:: --max-qp-delta <integer><br>+.. option:: --qp-delta-ref <integer><br> <br>    The offset by which QP is incremented for inter-frames<br>    when :option:`--scenecut-aware-qp` is enabled. Default 5.<br>    <br>    **Range of values:**  0 to 10<br>+   <br>+.. option:: --qp-delta-nonref <integer><br>+<br>+   The offset by which QP is incremented for non-referenced<br>+   inter-frames when :option:`--scenecut-aware-qp` is enabled.<br>+   The offset is computed from :option:`--qp-delta-ref` when it<br>+   is not explicitly specified.<br>+<br>+   **Range of values:**  0 to 10<br> <br> Quantization Options<br> ====================<br>diff --git a/source/CMakeLists.txt b/source/CMakeLists.txt<br>index ce127ce75..1d1f8cacf 100644<br>--- a/source/CMakeLists.txt<br>+++ b/source/CMakeLists.txt<br>@@ -29,7 +29,7 @@ option(NATIVE_BUILD "Target the build CPU" OFF)<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 194)<br>+set(X265_BUILD 195)<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 --git a/source/common/frame.cpp b/source/common/frame.cpp<br>index 8940cea8c..255882a9d 100644<br>--- a/source/common/frame.cpp<br>+++ b/source/common/frame.cpp<br>@@ -63,6 +63,7 @@ Frame::Frame()<br>     m_thetaPic = NULL;<br>     m_edgeBitPlane = NULL;<br>     m_edgeBitPic = NULL;<br>+    m_isInsideWindow = 0;<br> }<br> <br> bool Frame::create(x265_param *param, float* quantOffsets)<br>diff --git a/source/common/frame.h b/source/common/frame.h<br>index d96c1eeb2..dc5bbacf7 100644<br>--- a/source/common/frame.h<br>+++ b/source/common/frame.h<br>@@ -141,6 +141,8 @@ public:<br>     pixel*                 m_edgeBitPlane;<br>     pixel*                 m_edgeBitPic;<br> <br>+    int                    m_isInsideWindow;<br>+<br>     Frame();<br> <br>     bool create(x265_param *param, float* quantOffsets);<br>diff --git a/source/common/param.cpp b/source/common/param.cpp<br>index 8c0498efc..dc9d23cdd 100644<br>--- a/source/common/param.cpp<br>+++ b/source/common/param.cpp<br>@@ -180,7 +180,8 @@ void x265_param_default(x265_param* param)<br>     param->bEnableFades = 0;<br>     param->bEnableSceneCutAwareQp = 0;<br>     param->scenecutWindow = 500;<br>-    param->maxQpDelta = 5;<br>+    param->refQpDelta = 5;<br>+    param->nonRefQpDelta = param->refQpDelta + (SLICE_TYPE_DELTA * param->refQpDelta);<br> <br>     /* Intra Coding Tools */<br>     param->bEnableConstrainedIntra = 0;<br>@@ -1342,7 +1343,8 @@ int x265_param_parse(x265_param* p, const char* name, const char* value)<br>         OPT("fades") p->bEnableFades = atobool(value);<br>         OPT("scenecut-aware-qp") p->bEnableSceneCutAwareQp = atobool(value);<br>         OPT("scenecut-window") p->scenecutWindow = atoi(value);<br>-        OPT("max-qp-delta") p->maxQpDelta = atoi(value);<br>+        OPT("qp-delta-ref") p->refQpDelta = atoi(value);<br>+        OPT("qp-delta-nonref") p->nonRefQpDelta = atoi(value);<br>         OPT("field") p->bField = atobool( value );<br>         OPT("cll") p->bEmitCLL = atobool(value);<br>         OPT("frame-dup") p->bEnableFrameDuplication = atobool(value);<br>@@ -1768,10 +1770,17 @@ int x265_check_params(x265_param* param)<br>     }<br>     CHECK(param->selectiveSAO < 0 || param->selectiveSAO > 4,<br>         "Invalid SAO tune level. Value must be between 0 and 4 (inclusive)");<br>+    if (param->bEnableSceneCutAwareQp && !param->rc.bStatRead)<br>+    {<br>+        param->bEnableSceneCutAwareQp = 0;<br>+        x265_log(param, X265_LOG_WARNING, "Disabling Scenecut Aware Frame Quantizer Selection since it works only in pass 2\n");<br>+    }<br>     CHECK(param->scenecutWindow < 0 || param->scenecutWindow > 1000,<br>         "Invalid scenecut Window duration. Value must be between 0 and 1000(inclusive)");<br>-    CHECK(param->maxQpDelta < 0 || param->maxQpDelta > 10,<br>-        "Invalid maxQpDelta value. Value must be between 0 and 10 (inclusive)");<br>+    CHECK(param->refQpDelta < 0 || param->refQpDelta > 10,<br>+        "Invalid refQpDelta value. Value must be between 0 and 10 (inclusive)");<br>+    CHECK(param->nonRefQpDelta < 0 || param->nonRefQpDelta > 10,<br>+        "Invalid nonRefQpDelta value. Value must be between 0 and 10 (inclusive)");<br>     for(int level = 0; level < 3; level++)<br>         CHECK(param->hmeRange[level] < 0 || param->hmeRange[level] >= 32768,<br>             "Search Range for HME levels must be between 0 and 32768");<br>@@ -2219,7 +2228,7 @@ char *x265_param2string(x265_param* p, int padx, int pady)<br>     s += sprintf(s, " qp-adaptation-range=%.2f", p->rc.qpAdaptationRange);<br>     BOOL(p->bEnableSceneCutAwareQp, "scenecut-aware-qp");<br>     if (p->bEnableSceneCutAwareQp)<br>-        s += sprintf(s, " scenecut-window=%d max-qp-delta=%d", p->scenecutWindow, p->maxQpDelta);<br>+        s += sprintf(s, " scenecut-window=%d qp-delta-ref=%f qp-delta-nonref=%f", p->scenecutWindow, p->refQpDelta, p->nonRefQpDelta);<br>     s += sprintf(s, "conformance-window-offsets right=%d bottom=%d", p->confWinRightOffset, p->confWinBottomOffset);<br>     s += sprintf(s, " decoder-max-rate=%d", p->decoderVbvMaxRate);<br> #undef BOOL<br>@@ -2571,7 +2580,8 @@ void x265_copy_params(x265_param* dst, x265_param* src)<br>     dst->bEnableFades = src->bEnableFades;<br>     dst->bEnableSceneCutAwareQp = src->bEnableSceneCutAwareQp;<br>     dst->scenecutWindow = src->scenecutWindow;<br>-    dst->maxQpDelta = src->maxQpDelta;<br>+    dst->refQpDelta = src->refQpDelta;<br>+    dst->nonRefQpDelta = src->nonRefQpDelta;<br>     dst->bField = src->bField;<br> <br>     dst->confWinRightOffset = src->confWinRightOffset;<br>diff --git a/source/encoder/encoder.cpp b/source/encoder/encoder.cpp<br>index af5e7712f..387963ee8 100644<br>--- a/source/encoder/encoder.cpp<br>+++ b/source/encoder/encoder.cpp<br>@@ -1793,6 +1793,7 @@ int Encoder::encode(const x265_picture* pic_in, x265_picture* pic_out)<br>             inFrame->m_lowres.bScenecut = false;<br>             inFrame->m_lowres.satdCost = (int64_t)-1;<br>             inFrame->m_lowresInit = false;<br>+            inFrame->m_isInsideWindow = 0;<br>         }<br> <br>         /* Copy input picture into a Frame and PicYuv, send to lookahead */<br>@@ -1808,6 +1809,23 @@ int Encoder::encode(const x265_picture* pic_in, x265_picture* pic_out)<br>             if (m_param->radl && m_param->keyframeMax != m_param->keyframeMin)<br>                 inFrame->m_lowres.m_bIsHardScenecut = isHardSC;<br>         }<br>+<br>+        if (m_param->bEnableSceneCutAwareQp && m_param->rc.bStatRead)<br>+        {<br>+            RateControlEntry * rcEntry = NULL;<br>+            rcEntry = &(m_rateControl->m_rce2Pass[inFrame->m_poc]);<br>+            if(rcEntry->scenecut)<br>+            {<br>+                int backwardWindow = X265_MIN(int((p->fpsNum / p->fpsDenom) / 10), p->lookaheadDepth);<br>+                for (int i = 1; i <= backwardWindow; i++)<br>+                {<br>+                    int frameNum = inFrame->m_poc - i;<br>+                    Frame * frame = m_lookahead->m_inputQueue.getPOC(frameNum);<br>+                    if (frame)<br>+                        frame->m_isInsideWindow = 1;<br>+                }<br>+            }<br>+        }<br>         if (m_param->bHistBasedSceneCut && m_param->analysisSave)<br>         {<br>             memcpy(inFrame->m_analysisData.edgeHist, m_curEdgeHist, EDGE_BINS * sizeof(int32_t));<br>@@ -2224,8 +2242,23 @@ int Encoder::encode(const x265_picture* pic_in, x265_picture* pic_out)<br>             frameEnc = m_lookahead->getDecidedPicture();<br>         if (frameEnc && !pass && (!m_param->chunkEnd || (m_encodedFrameNum < m_param->chunkEnd)))<br>         {<br>-            if (m_param->bEnableSceneCutAwareQp && frameEnc->m_lowres.bScenecut)<br>-                m_rateControl->m_lastScenecut = frameEnc->m_poc;<br>+            if (m_param->bEnableSceneCutAwareQp && m_param->rc.bStatRead)<br>+            {<br>+                RateControlEntry * rcEntry;<br>+                rcEntry = &(m_rateControl->m_rce2Pass[frameEnc->m_poc]);<br>+<br>+                if (rcEntry->scenecut)<br>+                {<br>+                    if (m_rateControl->m_lastScenecut == -1)<br>+                        m_rateControl->m_lastScenecut = frameEnc->m_poc;<br>+                    else<br>+                    {<br>+                        int maxWindowSize = int((m_param->scenecutWindow / 1000.0) * (m_param->fpsNum / m_param->fpsDenom) + 0.5);<br>+                        if (frameEnc->m_poc > (m_rateControl->m_lastScenecut + maxWindowSize))<br>+                            m_rateControl->m_lastScenecut = frameEnc->m_poc;<br>+                    }<br>+                }<br>+            }<br> <br>             if (m_param->analysisMultiPassRefine || m_param->analysisMultiPassDistortion)<br>             {<br>diff --git a/source/encoder/ratecontrol.cpp b/source/encoder/ratecontrol.cpp<br>index 33e5a35a1..144b21d65 100644<br>--- a/source/encoder/ratecontrol.cpp<br>+++ b/source/encoder/ratecontrol.cpp<br>@@ -580,7 +580,7 @@ bool RateControl::init(const SPS& sps)<br>             double totalQpAq = 0;<br>             for (int i = 0; i < m_numEntries; i++)<br>             {<br>-                RateControlEntry *rce;<br>+                RateControlEntry *rce, *rcePocOrder;<br>                 int frameNumber;<br>                 int encodeOrder;<br>                 char picType;<br>@@ -597,13 +597,16 @@ bool RateControl::init(const SPS& sps)<br>                     return false;<br>                 }<br>                 rce = &m_rce2Pass[encodeOrder];<br>+                rcePocOrder = &m_rce2Pass[frameNumber];<br>                 m_encOrder[frameNumber] = encodeOrder;<br>                 if (!m_param->bMultiPassOptRPS)<br>                 {<br>-                    e += sscanf(p, " in:%*d out:%*d type:%c q:%lf q-aq:%lf q-noVbv:%lf q-Rceq:%lf tex:%d mv:%d misc:%d icu:%lf pcu:%lf scu:%lf",<br>+                    int scenecut = 0;<br>+                    e += sscanf(p, " in:%*d out:%*d type:%c q:%lf q-aq:%lf q-noVbv:%lf q-Rceq:%lf tex:%d mv:%d misc:%d icu:%lf pcu:%lf scu:%lf sc:%d",<br>                         &picType, &qpRc, &qpAq, &qNoVbv, &qRceq, &rce->coeffBits,<br>                         &rce->mvBits, &rce->miscBits, &rce->iCuCount, &rce->pCuCount,<br>-                        &rce->skipCuCount);<br>+                        &rce->skipCuCount, &scenecut);<br>+                    rcePocOrder->scenecut = scenecut != 0;<br>                 }<br>                 else<br>                 {<br>@@ -1311,7 +1314,8 @@ int RateControl::rateControlStart(Frame* curFrame, RateControlEntry* rce, Encode<br>         copyRceData(rce, &m_rce2Pass[index]);<br>     }<br>     rce->isActive = true;<br>-    rce->scenecut = false;<br>+    if (!m_param->rc.bStatRead)<br>+        rce->scenecut = false;<br>     rce->isFadeEnd = curFrame->m_lowres.bIsFadeEnd;<br>     bool isRefFrameScenecut = m_sliceType!= I_SLICE && m_curSlice->m_refFrameList[0][0]->m_lowres.bScenecut;<br>     m_isFirstMiniGop = m_sliceType == I_SLICE ? true : m_isFirstMiniGop;<br>@@ -1856,11 +1860,12 @@ double RateControl::rateEstimateQscale(Frame* curFrame, RateControlEntry *rce)<br>         {<br>             double lqmin = m_lmin[m_sliceType];<br>             double lqmax = m_lmax[m_sliceType];<br>-            qScale = scenecutAwareQp(curFrame, qScale);<br>+            qScale = scenecutAwareMasking(curFrame, qScale);<br>             qScale = x265_clip3(lqmin, lqmax, qScale);<br>             q = x265_qScale2qp(qScale);<br>             rce->qpNoVbv = q;<br>         }<br>+<br>         if (m_isVbv)<br>         {<br>             lmin = m_lastQScaleFor[P_SLICE] / m_lstep;<br>@@ -1971,6 +1976,16 @@ double RateControl::rateEstimateQscale(Frame* curFrame, RateControlEntry *rce)<br>                 m_avgPFrameQp = (m_avgPFrameQp + rce->qpNoVbv) / 2;<br>             }<br> <br>+            /* Scenecut Aware QP offsets*/<br>+            if (m_param->bEnableSceneCutAwareQp)<br>+            {<br>+                double qmin = m_lmin[m_sliceType];<br>+                double qmax = m_lmax[m_sliceType];<br>+                q = scenecutAwareMasking(curFrame, q);<br>+                q = x265_clip3(qmin, qmax, q);<br>+                rce->qpNoVbv = x265_qScale2qp(q);<br>+            }<br>+<br>             if (m_isVbv)<br>             {<br>                 /* Do not overflow vbv */<br>@@ -2120,13 +2135,12 @@ double RateControl::rateEstimateQscale(Frame* curFrame, RateControlEntry *rce)<br>             {<br>                 double qmin = m_lmin[m_sliceType];<br>                 double qmax = m_lmax[m_sliceType];<br>-                q = scenecutAwareQp(curFrame, q);<br>+                q = scenecutAwareMasking(curFrame, q);<br>                 q = x265_clip3(qmin, qmax, q);<br>                 rce->qpNoVbv = x265_qScale2qp(q);<br>             }<br>             q = clipQscale(curFrame, rce, q);<br> <br>-<br>             if (m_2pass)<br>                 rce->frameSizePlanned = qScale2bits(rce, q);<br>             else<br>@@ -2964,7 +2978,7 @@ int RateControl::writeRateControlFrameStats(Frame* curFrame, RateControlEntry* r<br>     if (!curEncData.m_param->bMultiPassOptRPS)<br>     {<br>         if (fprintf(m_statFileOut,<br>-            "in:%d out:%d type:%c q:%.2f q-aq:%.2f q-noVbv:%.2f q-Rceq:%.2f tex:%d mv:%d misc:%d icu:%.2f pcu:%.2f scu:%.2f ;\n",<br>+            "in:%d out:%d type:%c q:%.2f q-aq:%.2f q-noVbv:%.2f q-Rceq:%.2f tex:%d mv:%d misc:%d icu:%.2f pcu:%.2f scu:%.2f sc:%d ;\n",<br>             rce->poc, rce->encodeOrder,<br>             cType, curEncData.m_avgQpRc, curEncData.m_avgQpAq,<br>             rce->qpNoVbv, rce->qRceq,<br>@@ -2973,7 +2987,8 @@ int RateControl::writeRateControlFrameStats(Frame* curFrame, RateControlEntry* r<br>             curFrame->m_encData->m_frameStats.miscBits,<br>             curFrame->m_encData->m_frameStats.percent8x8Intra * m_ncu,<br>             curFrame->m_encData->m_frameStats.percent8x8Inter * m_ncu,<br>-            curFrame->m_encData->m_frameStats.percent8x8Skip  * m_ncu) < 0)<br>+            curFrame->m_encData->m_frameStats.percent8x8Skip  * m_ncu,<br>+            curFrame->m_lowres.bScenecut) < 0)<br>             goto writeFailure;<br>     }<br>     else<br>@@ -3150,52 +3165,75 @@ void RateControl::splitbUsed(char bused[], RateControlEntry *rce)<br>     }<br> }<br> <br>-double RateControl::scenecutAwareQp(Frame* curFrame, double q)<br>+double RateControl::scenecutAwareMasking(Frame* curFrame, double q)<br> {<br>     double qp = x265_qScale2qp(q);<br>     uint32_t maxWindowSize = uint32_t((m_param->scenecutWindow / 1000.0) * (m_param->fpsNum / m_param->fpsDenom) + 0.5);<br>     uint32_t windowSize = maxWindowSize / 3;<br>     int lastScenecut = m_top->m_rateControl->m_lastScenecut;<br>     int lastIFrame = m_top->m_rateControl->m_lastScenecutAwareIFrame;<br>-    double maxQpDelta = double(m_param->maxQpDelta);<br>-    double iSliceDelta = double(I_SLICE_DELTA);<br>-    double sliceTypeDelta = SLICE_TYPE_DELTA * maxQpDelta;<br>-    double window2Delta = WINDOW2_DELTA * maxQpDelta;<br>-    double window3Delta = WINDOW3_DELTA * maxQpDelta;<br>+    double refQpDelta = double(m_param->refQpDelta);<br>+    double nonRefQpDelta = double(m_param->nonRefQpDelta);<br>+    double sliceTypeDelta = SLICE_TYPE_DELTA * refQpDelta;<br>+    double window2Delta = WINDOW2_DELTA * refQpDelta;<br>+    double window3Delta = WINDOW3_DELTA * refQpDelta;<br> <br>     bool isFrameInsideWindow = curFrame->m_poc > lastScenecut && curFrame->m_poc <= (lastScenecut + int(maxWindowSize));<br>+    if (isFrameInsideWindow)<br>+        curFrame->m_isInsideWindow = 2;<br> <br>-    if (isFrameInsideWindow && IS_X265_TYPE_I(curFrame->m_lowres.sliceType))<br>+    if (isFrameInsideWindow && (IS_X265_TYPE_I(curFrame->m_lowres.sliceType) || curFrame->m_lowres.bScenecut))<br>     {<br>         m_top->m_rateControl->m_lastScenecutAwareIFrame = curFrame->m_poc;<br>     }<br>     else if (isFrameInsideWindow && (curFrame->m_lowres.sliceType == X265_TYPE_P))<br>     {<br>         if (!(lastIFrame > lastScenecut && lastIFrame <= (lastScenecut + int(maxWindowSize))<br>-            && curFrame->m_poc > lastIFrame))<br>+            && curFrame->m_poc >= lastIFrame))<br>         {<br>-            qp += maxQpDelta - sliceTypeDelta;<br>+            qp += refQpDelta - sliceTypeDelta;<br>             if (((curFrame->m_poc) > (lastScenecut + int(windowSize))) && ((curFrame->m_poc) <= (lastScenecut + 2 * int(windowSize))))<br>                 qp -= window2Delta;<br>             else if (curFrame->m_poc > lastScenecut + 2 * int(windowSize))<br>                 qp -= window3Delta;<br>         }<br>     }<br>-    else if (isFrameInsideWindow && IS_X265_TYPE_B(curFrame->m_lowres.sliceType))<br>+    else if (isFrameInsideWindow && (curFrame->m_lowres.sliceType == X265_TYPE_BREF))<br>     {<br>         if (!(lastIFrame > lastScenecut && lastIFrame <= (lastScenecut + int(maxWindowSize))<br>-            && curFrame->m_poc > lastIFrame))<br>+            && curFrame->m_poc >= lastIFrame))<br>         {<br>-            qp += maxQpDelta;<br>-            if (curFrame->m_lowres.sliceType == X265_TYPE_B)<br>-                qp += sliceTypeDelta;<br>+                qp += refQpDelta;<br>             if (((curFrame->m_poc) > (lastScenecut + int(windowSize))) && ((curFrame->m_poc) <= (lastScenecut + 2 * int(windowSize))))<br>                 qp -= window2Delta;<br>             else if (curFrame->m_poc > lastScenecut + 2 * int(windowSize))<br>                 qp -= window3Delta;<br>         }<br>     }<br>-    if (IS_X265_TYPE_I(curFrame->m_lowres.sliceType) && curFrame->m_lowres.bScenecut)<br>-        qp = qp - iSliceDelta;<br>-    return  x265_qp2qScale(qp);<br>+    else if (isFrameInsideWindow && (curFrame->m_lowres.sliceType == X265_TYPE_B))<br>+    {<br>+        if (!(lastIFrame > lastScenecut && lastIFrame <= (lastScenecut + int(maxWindowSize))<br>+            && curFrame->m_poc >= lastIFrame))<br>+        {<br>+            qp += nonRefQpDelta;<br>+            if (((curFrame->m_poc) > (lastScenecut + int(windowSize))) && ((curFrame->m_poc) <= (lastScenecut + 2 * int(windowSize))))<br>+                qp -= window2Delta;<br>+            else if (curFrame->m_poc > lastScenecut + 2 * int(windowSize))<br>+                qp -= window3Delta;<br>+        }<br>+    }<br>+<br>+    if (curFrame->m_isInsideWindow == 1)<br>+    {<br>+        refQpDelta -= window3Delta;<br>+        nonRefQpDelta -= window3Delta;<br>+        if (curFrame->m_lowres.sliceType == X265_TYPE_P)<br>+            qp += refQpDelta - sliceTypeDelta;<br>+        else if (curFrame->m_lowres.sliceType == X265_TYPE_BREF)<br>+            qp += refQpDelta;<br>+        else if (curFrame->m_lowres.sliceType == X265_TYPE_B)<br>+            qp += nonRefQpDelta;<br>+    }<br>+<br>+    return x265_qp2qScale(qp);<br> }<br>diff --git a/source/encoder/ratecontrol.h b/source/encoder/ratecontrol.h<br>index 118191bbf..809e0c620 100644<br>--- a/source/encoder/ratecontrol.h<br>+++ b/source/encoder/ratecontrol.h<br>@@ -47,8 +47,6 @@ struct SPS;<br> #define CLIP_DURATION(f) x265_clip3(MIN_FRAME_DURATION, MAX_FRAME_DURATION, f)<br> <br> /*Scenecut Aware QP*/<br>-#define I_SLICE_DELTA           2   /* Subtracted from base QP for the scenecut I frames*/<br>-#define SLICE_TYPE_DELTA        0.3 /* The offset decremented or incremented for P-frames or b-frames respectively*/<br> #define WINDOW1_DELTA           0   /* The offset for the frames coming in the window-1*/<br> #define WINDOW2_DELTA           0.3 /* The offset for the frames coming in the window-2*/<br> #define WINDOW3_DELTA           0.6 /* The offset for the frames coming in the window-3*/<br>@@ -271,7 +269,7 @@ public:<br>     int writeRateControlFrameStats(Frame* curFrame, RateControlEntry* rce);<br>     bool   initPass2();<br> <br>-    double scenecutAwareQp(Frame* curFrame, double q);<br>+    double scenecutAwareMasking(Frame* curFrame, double q);<br> <br> protected:<br> <br>diff --git a/source/test/rate-control-tests.txt b/source/test/rate-control-tests.txt<br>index 5b1f9992a..eed92f809 100644<br>--- a/source/test/rate-control-tests.txt<br>+++ b/source/test/rate-control-tests.txt<br>@@ -44,6 +44,8 @@ CrowdRun_1920x1080_50_10bit_422.yuv,--preset superfast --bitrate 2500 --pass 1 -<br> RaceHorses_416x240_30_10bit.yuv,--preset medium --crf 26 --vbv-maxrate 1000 --vbv-bufsize 1000 --pass 1::--preset fast --bitrate 1000  --vbv-maxrate 1000 --vbv-bufsize 700 --pass 3 -F4::--preset slow --bitrate 500 --vbv-maxrate 500  --vbv-bufsize 700 --pass 2 -F4<br> sita_1920x1080_30.yuv, --preset ultrafast --crf 20 --no-cutree --keyint 50 --min-keyint 50 --no-open-gop --pass 1 --vbv-bufsize 7000 --vbv-maxrate 5000:: --preset ultrafast --crf 20 --no-cutree --keyint 50 --min-keyint 50 --no-open-gop --pass 2 --vbv-bufsize 7000 --vbv-maxrate 5000 --repeat-headers<br> sita_1920x1080_30.yuv, --preset medium --crf 20 --no-cutree --keyint 50 --min-keyint 50 --no-open-gop --pass 1 --vbv-bufsize 7000 --vbv-maxrate 5000 --repeat-headers --multi-pass-opt-rps:: --preset medium --crf 20 --no-cutree --keyint 50 --min-keyint 50 --no-open-gop --pass 2 --vbv-bufsize 7000 --vbv-maxrate 5000 --repeat-headers --multi-pass-opt-rps<br>+sintel_trailer_2k_1920x1080_24.yuv,--preset medium --bitrate 6000 --no-cutree --aq-mode 0 --pass 1::--preset medium --bitrate 6000 --no-cutree --aq-mode 0 --pass 2 --scenecut-aware-qp<br>+sintel_trailer_2k_1920x1080_24.yuv,--preset medium --bitrate 6000 --no-cutree --aq-mode 0 --hist-scenecut --pass 1::--preset medium --bitrate 6000 --no-cutree --aq-mode 0 --hist-scenecut --pass 2 --scenecut-aware-qp --qp-delta-nonref 8<br> <br> # multi-pass rate control and analysis<br> ducks_take_off_1080p50.y4m,--bitrate 6000 --pass 1  --multi-pass-opt-analysis  --hash 1 --ssim --psnr:: --bitrate 6000 --pass 2  --multi-pass-opt-analysis  --hash 1 --ssim --psnr<br>diff --git a/source/test/regression-tests.txt b/source/test/regression-tests.txt<br>index 97e6e5b11..22673d469 100644<br>--- a/source/test/regression-tests.txt<br>+++ b/source/test/regression-tests.txt<br>@@ -158,7 +158,6 @@ ducks_take_off_420_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> Traffic_4096x2048_30p.y4m, --preset medium --frame-dup --dup-threshold 60 --hrd --bitrate 10000 --vbv-bufsize 15000 --vbv-maxrate 12000<br> Kimono1_1920x1080_24_400.yuv,--preset superfast --qp 28 --zones 0,139,q=32<br>-Island_960x540_24.yuv,--no-cutree --aq-mode 0 --bitrate 6000 --scenecut-aware-qp<br> sintel_trailer_2k_1920x1080_24.yuv, --preset medium --hist-scenecut --hist-threshold 0.02 --frame-dup --dup-threshold 60 --hrd --bitrate 10000 --vbv-bufsize 15000 --vbv-maxrate 12000<br> sintel_trailer_2k_1920x1080_24.yuv, --preset medium --hist-scenecut --hist-threshold 0.02<br> sintel_trailer_2k_1920x1080_24.yuv, --preset ultrafast --hist-scenecut --hist-threshold 0.02<br>diff --git a/source/x265.h b/source/x265.h<br>index 32feb2bca..9eb115a5e 100644<br>--- a/source/x265.h<br>+++ b/source/x265.h<br>@@ -607,6 +607,8 @@ typedef enum<br> #define X265_ANALYSIS_SAVE 1<br> #define X265_ANALYSIS_LOAD 2<br> <br>+#define SLICE_TYPE_DELTA        0.3 /* The offset decremented or incremented for P-frames or b-frames respectively*/<br>+<br> typedef struct x265_cli_csp<br> {<br>     int planes;<br>@@ -1843,9 +1845,8 @@ typedef struct x265_param<br>       Default 1 (Enabled). API only. */<br>     int       bResetZoneConfig;<br> <br>-    /* Enables a ratecontrol algorithm for reducing the bits spent on the inter-frames<br>-     * within the scenecutWindow after a scenecut by increasing their QP without<br>-     * any deterioration in visual quality. It also increases the quality of scenecut I-Frames by reducing their QP.<br>+    /* It reduces the bits spent on the inter-frames within the scenecutWindow before and after a scenecut<br>+     * by increasing their QP in ratecontrol pass2 algorithm without any deterioration in visual quality.<br>      * Default is disabled. */<br>     int       bEnableSceneCutAwareQp;<br> <br>@@ -1855,7 +1856,10 @@ typedef struct x265_param<br> <br>     /* The offset by which QP is incremented for inter-frames when bEnableSceneCutAwareQp is set.<br>      * Default is +5. */<br>-    int       maxQpDelta;<br>+    double       refQpDelta;<br>+<br>+    /* The offset by which QP is incremented for non-referenced inter-frames when bEnableSceneCutAwareQp is set. */<br>+    double       nonRefQpDelta;<br> <br>     /* A genuine threshold used for histogram based scene cut detection.<br>      * This threshold determines whether a frame is a scenecut or not<br>diff --git a/source/x265cli.cpp b/source/x265cli.cpp<br>index b53dc2b0b..b198e55c2 100644<br>--- a/source/x265cli.cpp<br>+++ b/source/x265cli.cpp<br>@@ -179,7 +179,8 @@ namespace X265_NS {<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 inside the scenecut window after scenecut. Default %s\n", OPT(param->bEnableSceneCutAwareQp));<br>         H1("   --scenecut-window <0..1000>   QP incremental duration(in milliseconds) when scenecut-aware-qp is enabled. Default %d\n", param->scenecutWindow);<br>-        H1("   --max-qp-delta <0..10>        QP offset to increment with base QP for inter-frames. Default %d\n", param->maxQpDelta);<br>+        H1("   --qp-delta-ref <0..10>        QP offset to increment with base QP for inter-frames. Default %f\n", param->refQpDelta);<br>+        H1("   --qp-delta-nonref <0..10>     QP offset to increment with base QP for non-referenced inter-frames. Default %f\n", param->nonRefQpDelta);<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>diff --git a/source/x265cli.h b/source/x265cli.h<br>index 08f5d3d9e..311f06935 100644<br>--- a/source/x265cli.h<br>+++ b/source/x265cli.h<br>@@ -151,7 +151,8 @@ static const struct option long_options[] =<br>     { "scenecut-aware-qp",    no_argument, NULL, 0 },<br>     { "no-scenecut-aware-qp", no_argument, NULL, 0 },<br>     { "scenecut-window",required_argument, NULL, 0 },<br>-    { "max-qp-delta",   required_argument, NULL, 0 },<br>+    { "qp-delta-ref",   required_argument, NULL, 0 },<br>+    { "qp-delta-nonref",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>-- <br>2.18.0.windows.1<br><br><div><div dir="ltr"><div dir="ltr"><div><div dir="ltr"><div dir="ltr"><div dir="ltr"><font color="#0c343d" face="verdana, sans-serif"><br></font></div><div dir="ltr"><font color="#0c343d" face="verdana, sans-serif">Thanks & Regards</font><div><font color="#0c343d" face="verdana, sans-serif"><b>Niranjan Kumar B</b></font></div><div><font size="1" color="#0c343d" face="verdana, sans-serif">Video Codec Engineer </font></div><div><font size="1" color="#0c343d" face="verdana, sans-serif">Media & AI Analytics</font></div><div><font face="trebuchet ms, sans-serif" color="#0c343d">+91 958 511 1449</font></div><div><a href="https://multicorewareinc.com/" style="color:rgb(17,85,204)" target="_blank"><img src="https://docs.google.com/uc?export=download&id=1kc3RJu9M8bnIf6Xa5rUw2d-eEVUsPBE5&revid=0B7tw9XJBmynaemR1VUpQUi9DVytRVW5SVkRwVTFjb1hBMUcwPQ"></a></div></div></div></div></div></div></div></div><br></div><br><div class="gmail_quote"><div dir="ltr" class="gmail_attr">On Wed, Jul 1, 2020 at 10:01 PM Niranjan Bala <<a href="mailto:niranjan@multicorewareinc.com" target="_blank">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">Please ignore the above patch and use the following one.  <br clear="all"><div><div dir="ltr"><div dir="ltr"><div><div dir="ltr"><div dir="ltr"><div dir="ltr"><font color="#0c343d" face="verdana, sans-serif"><br></font></div><div dir="ltr"><font color="#0c343d" face="verdana, sans-serif">Thanks & Regards</font><div><font color="#0c343d" face="verdana, sans-serif"><b>Niranjan Kumar B</b></font></div><div><font size="1" color="#0c343d" face="verdana, sans-serif">Video Codec Engineer </font></div><div><font size="1" color="#0c343d" face="verdana, sans-serif">Media & AI Analytics</font></div><div><font face="trebuchet ms, sans-serif" color="#0c343d">+91 958 511 1449</font></div><div><a href="https://multicorewareinc.com/" style="color:rgb(17,85,204)" target="_blank"><img src="https://docs.google.com/uc?export=download&id=1kc3RJu9M8bnIf6Xa5rUw2d-eEVUsPBE5&revid=0B7tw9XJBmynaemR1VUpQUi9DVytRVW5SVkRwVTFjb1hBMUcwPQ"></a></div></div></div></div></div></div></div></div><br></div><br><div class="gmail_quote"><div dir="ltr" class="gmail_attr">On Wed, Jul 1, 2020 at 1:17 PM Niranjan Bala <<a href="mailto:niranjan@multicorewareinc.com" target="_blank">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 1593491362 -19800<br>#      Tue Jun 30 09:59:22 2020 +0530<br># Node ID 38846fa560f350c02a37709d86e6d72989e533d2<br># Parent  c8bab0857b4ac70e37c70fcde419cc7a2a77badf<br>Improve: Scenecut Aware Frame Quantizer Selection<br><br>This patch does the following:<br>1)Reduce bits for frames before the scenecut<br>2)Refactor Scenecut Aware Frame Quantizer Selection<br>3)Add option "--qp-delta-nonref" to set offset for<br>non-referenced inter frames(optional).<br>4)Enables Scenecut Aware Frame Quantizer Selection<br>to run only with pass 2<br><br>diff --git a/doc/reST/cli.rst b/doc/reST/cli.rst<br>--- a/doc/reST/cli.rst<br>+++ b/doc/reST/cli.rst<br>@@ -1948,10 +1948,11 @@<br> <br> .. option:: --scenecut-aware-qp, --no-scenecut-aware-qp<br>    <br>-   Enables a ratecontrol algorithm for reducing the bits spent on the inter-frames<br>-   within the :option:`--scenecut-window` after a scenecut by increasing their QP<br>-   without any deterioration in visual quality. It also increases the quality of<br>-   scenecut I-Frames by reducing their QP. Default disabled.<br>+   It reduces the bits spent on the inter-frames within the :option:`--scenecut-window`<br>+   before and after a scenecut by increasing their QP in ratecontrol pass2 algorithm<br>+   without any deterioration in visual quality. If a scenecut falls within the window,<br>+   the QP of the inter-frames after this scenecut will not be modified. <br>+   :option:`--scenecut-aware-qp` works only with --pass 2. Default disabled.<br>    <br> .. option:: --scenecut-window <integer><br> <br>@@ -1961,12 +1962,21 @@<br>    <br>    **Range of values:** 0 to 1000<br>    <br>-.. option:: --max-qp-delta <integer><br>+.. option:: --qp-delta-ref <integer><br> <br>    The offset by which QP is incremented for inter-frames<br>    when :option:`--scenecut-aware-qp` is enabled. Default 5.<br>    <br>    **Range of values:**  0 to 10<br>+   <br>+.. option:: --qp-delta-nonref <integer><br>+<br>+   The offset by which QP is incremented for non-referenced<br>+   inter-frames when :option:`--scenecut-aware-qp` is enabled.<br>+   The offset is computed from :option:`--qp-delta-ref` when it<br>+   is not explicitly specified.<br>+<br>+   **Range of values:**  0 to 10<br> <br> Quantization Options<br> ====================<br>diff --git a/source/CMakeLists.txt b/source/CMakeLists.txt<br>--- a/source/CMakeLists.txt<br>+++ b/source/CMakeLists.txt<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 194)<br>+set(X265_BUILD 195)<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 --git a/source/common/frame.cpp b/source/common/frame.cpp<br>--- a/source/common/frame.cpp<br>+++ b/source/common/frame.cpp<br>@@ -63,6 +63,7 @@<br>     m_thetaPic = NULL;<br>     m_edgeBitPlane = NULL;<br>     m_edgeBitPic = NULL;<br>+    m_isInsideWindow = 0;<br> }<br> <br> bool Frame::create(x265_param *param, float* quantOffsets)<br>diff --git a/source/common/frame.h b/source/common/frame.h<br>--- a/source/common/frame.h<br>+++ b/source/common/frame.h<br>@@ -141,6 +141,8 @@<br>     pixel*                 m_edgeBitPlane;<br>     pixel*                 m_edgeBitPic;<br> <br>+    int                    m_isInsideWindow;<br>+<br>     Frame();<br> <br>     bool create(x265_param *param, float* quantOffsets);<br>diff --git a/source/common/param.cpp b/source/common/param.cpp<br>--- a/source/common/param.cpp<br>+++ b/source/common/param.cpp<br>@@ -180,7 +180,8 @@<br>     param->bEnableFades = 0;<br>     param->bEnableSceneCutAwareQp = 0;<br>     param->scenecutWindow = 500;<br>-    param->maxQpDelta = 5;<br>+    param->refQpDelta = 5;<br>+    param->nonRefQpDelta = param->refQpDelta + (SLICE_TYPE_DELTA * param->refQpDelta);<br> <br>     /* Intra Coding Tools */<br>     param->bEnableConstrainedIntra = 0;<br>@@ -1342,7 +1343,8 @@<br>         OPT("fades") p->bEnableFades = atobool(value);<br>         OPT("scenecut-aware-qp") p->bEnableSceneCutAwareQp = atobool(value);<br>         OPT("scenecut-window") p->scenecutWindow = atoi(value);<br>-        OPT("max-qp-delta") p->maxQpDelta = atoi(value);<br>+        OPT("qp-delta-ref") p->refQpDelta = atoi(value);<br>+        OPT("qp-delta-nonref") p->nonRefQpDelta = atoi(value);<br>         OPT("field") p->bField = atobool( value );<br>         OPT("cll") p->bEmitCLL = atobool(value);<br>         OPT("frame-dup") p->bEnableFrameDuplication = atobool(value);<br>@@ -1768,10 +1770,17 @@<br>     }<br>     CHECK(param->selectiveSAO < 0 || param->selectiveSAO > 4,<br>         "Invalid SAO tune level. Value must be between 0 and 4 (inclusive)");<br>+    if (param->bEnableSceneCutAwareQp && !param->rc.bStatRead)<br>+    {<br>+        param->bEnableSceneCutAwareQp = 0;<br>+        x265_log(param, X265_LOG_WARNING, "Disabling Scenecut Aware Frame Quantizer Selection since it works only in pass 2\n");<br>+    }<br>     CHECK(param->scenecutWindow < 0 || param->scenecutWindow > 1000,<br>         "Invalid scenecut Window duration. Value must be between 0 and 1000(inclusive)");<br>-    CHECK(param->maxQpDelta < 0 || param->maxQpDelta > 10,<br>-        "Invalid maxQpDelta value. Value must be between 0 and 10 (inclusive)");<br>+    CHECK(param->refQpDelta < 0 || param->refQpDelta > 10,<br>+        "Invalid refQpDelta value. Value must be between 0 and 10 (inclusive)");<br>+    CHECK(param->nonRefQpDelta < 0 || param->nonRefQpDelta > 10,<br>+        "Invalid nonRefQpDelta value. Value must be between 0 and 10 (inclusive)");<br>     for(int level = 0; level < 3; level++)<br>         CHECK(param->hmeRange[level] < 0 || param->hmeRange[level] >= 32768,<br>             "Search Range for HME levels must be between 0 and 32768");<br>@@ -2219,7 +2228,7 @@<br>     s += sprintf(s, " qp-adaptation-range=%.2f", p->rc.qpAdaptationRange);<br>     BOOL(p->bEnableSceneCutAwareQp, "scenecut-aware-qp");<br>     if (p->bEnableSceneCutAwareQp)<br>-        s += sprintf(s, " scenecut-window=%d max-qp-delta=%d", p->scenecutWindow, p->maxQpDelta);<br>+        s += sprintf(s, " scenecut-window=%d qp-delta-ref=%f qp-delta-nonref=%f", p->scenecutWindow, p->refQpDelta, p->nonRefQpDelta);<br>     s += sprintf(s, "conformance-window-offsets right=%d bottom=%d", p->confWinRightOffset, p->confWinBottomOffset);<br>     s += sprintf(s, " decoder-max-rate=%d", p->decoderVbvMaxRate);<br> #undef BOOL<br>@@ -2571,7 +2580,8 @@<br>     dst->bEnableFades = src->bEnableFades;<br>     dst->bEnableSceneCutAwareQp = src->bEnableSceneCutAwareQp;<br>     dst->scenecutWindow = src->scenecutWindow;<br>-    dst->maxQpDelta = src->maxQpDelta;<br>+    dst->refQpDelta = src->refQpDelta;<br>+    dst->nonRefQpDelta = src->nonRefQpDelta;<br>     dst->bField = src->bField;<br> <br>     dst->confWinRightOffset = src->confWinRightOffset;<br>diff --git a/source/encoder/encoder.cpp b/source/encoder/encoder.cpp<br>--- a/source/encoder/encoder.cpp<br>+++ b/source/encoder/encoder.cpp<br>@@ -1793,6 +1793,7 @@<br>             inFrame->m_lowres.bScenecut = false;<br>             inFrame->m_lowres.satdCost = (int64_t)-1;<br>             inFrame->m_lowresInit = false;<br>+            inFrame->m_isInsideWindow = 0;<br>         }<br> <br>         /* Copy input picture into a Frame and PicYuv, send to lookahead */<br>@@ -1808,6 +1809,23 @@<br>             if (m_param->radl && m_param->keyframeMax != m_param->keyframeMin)<br>                 inFrame->m_lowres.m_bIsHardScenecut = isHardSC;<br>         }<br>+<br>+        if (m_param->bEnableSceneCutAwareQp && m_param->rc.bStatRead)<br>+        {<br>+            RateControlEntry * rcEntry = NULL;<br>+            rcEntry = &(m_rateControl->m_rce2Pass[inFrame->m_poc]);<br>+            if(rcEntry->scenecut)<br>+            {<br>+                int backwardWindow = X265_MIN(int((p->fpsNum / p->fpsDenom) / 10), p->lookaheadDepth);<br>+                for (int i = 1; i <= backwardWindow; i++)<br>+                {<br>+                    int frameNum = inFrame->m_poc - i;<br>+                    Frame * frame = m_lookahead->m_inputQueue.getPOC(frameNum);<br>+                    if (frame)<br>+                        frame->m_isInsideWindow = 1;<br>+                }<br>+            }<br>+        }<br>         if (m_param->bHistBasedSceneCut && m_param->analysisSave)<br>         {<br>             memcpy(inFrame->m_analysisData.edgeHist, m_curEdgeHist, EDGE_BINS * sizeof(int32_t));<br>@@ -2224,8 +2242,23 @@<br>             frameEnc = m_lookahead->getDecidedPicture();<br>         if (frameEnc && !pass && (!m_param->chunkEnd || (m_encodedFrameNum < m_param->chunkEnd)))<br>         {<br>-            if (m_param->bEnableSceneCutAwareQp && frameEnc->m_lowres.bScenecut)<br>-                m_rateControl->m_lastScenecut = frameEnc->m_poc;<br>+            if (m_param->bEnableSceneCutAwareQp && m_param->rc.bStatRead)<br>+            {<br>+                RateControlEntry * rcEntry;<br>+                rcEntry = &(m_rateControl->m_rce2Pass[frameEnc->m_poc]);<br>+<br>+                if (rcEntry->scenecut)<br>+                {<br>+                    if (m_rateControl->m_lastScenecut == -1)<br>+                        m_rateControl->m_lastScenecut = frameEnc->m_poc;<br>+                    else<br>+                    {<br>+                        int maxWindowSize = int((m_param->scenecutWindow / 1000.0) * (m_param->fpsNum / m_param->fpsDenom) + 0.5);<br>+                        if (frameEnc->m_poc > (m_rateControl->m_lastScenecut + maxWindowSize))<br>+                            m_rateControl->m_lastScenecut = frameEnc->m_poc;<br>+                    }<br>+                }<br>+            }<br> <br>             if (m_param->analysisMultiPassRefine || m_param->analysisMultiPassDistortion)<br>             {<br>diff --git a/source/encoder/ratecontrol.cpp b/source/encoder/ratecontrol.cpp<br>--- a/source/encoder/ratecontrol.cpp<br>+++ b/source/encoder/ratecontrol.cpp<br>@@ -580,7 +580,7 @@<br>             double totalQpAq = 0;<br>             for (int i = 0; i < m_numEntries; i++)<br>             {<br>-                RateControlEntry *rce;<br>+                RateControlEntry *rce, *rcePocOrder;<br>                 int frameNumber;<br>                 int encodeOrder;<br>                 char picType;<br>@@ -597,13 +597,16 @@<br>                     return false;<br>                 }<br>                 rce = &m_rce2Pass[encodeOrder];<br>+                rcePocOrder = &m_rce2Pass[frameNumber];<br>                 m_encOrder[frameNumber] = encodeOrder;<br>                 if (!m_param->bMultiPassOptRPS)<br>                 {<br>-                    e += sscanf(p, " in:%*d out:%*d type:%c q:%lf q-aq:%lf q-noVbv:%lf q-Rceq:%lf tex:%d mv:%d misc:%d icu:%lf pcu:%lf scu:%lf",<br>+                    int scenecut = 0;<br>+                    e += sscanf(p, " in:%*d out:%*d type:%c q:%lf q-aq:%lf q-noVbv:%lf q-Rceq:%lf tex:%d mv:%d misc:%d icu:%lf pcu:%lf scu:%lf sc:%d",<br>                         &picType, &qpRc, &qpAq, &qNoVbv, &qRceq, &rce->coeffBits,<br>                         &rce->mvBits, &rce->miscBits, &rce->iCuCount, &rce->pCuCount,<br>-                        &rce->skipCuCount);<br>+                        &rce->skipCuCount, &scenecut);<br>+                    rcePocOrder->scenecut = scenecut != 0;<br>                 }<br>                 else<br>                 {<br>@@ -1311,7 +1314,8 @@<br>         copyRceData(rce, &m_rce2Pass[index]);<br>     }<br>     rce->isActive = true;<br>-    rce->scenecut = false;<br>+    if (!m_param->rc.bStatRead)<br>+        rce->scenecut = false;<br>     rce->isFadeEnd = curFrame->m_lowres.bIsFadeEnd;<br>     bool isRefFrameScenecut = m_sliceType!= I_SLICE && m_curSlice->m_refFrameList[0][0]->m_lowres.bScenecut;<br>     m_isFirstMiniGop = m_sliceType == I_SLICE ? true : m_isFirstMiniGop;<br>@@ -1856,11 +1860,12 @@<br>         {<br>             double lqmin = m_lmin[m_sliceType];<br>             double lqmax = m_lmax[m_sliceType];<br>-            qScale = scenecutAwareQp(curFrame, qScale);<br>+            qScale = scenecutAwareMasking(curFrame, qScale);<br>             qScale = x265_clip3(lqmin, lqmax, qScale);<br>             q = x265_qScale2qp(qScale);<br>             rce->qpNoVbv = q;<br>         }<br>+<br>         if (m_isVbv)<br>         {<br>             lmin = m_lastQScaleFor[P_SLICE] / m_lstep;<br>@@ -1971,6 +1976,16 @@<br>                 m_avgPFrameQp = (m_avgPFrameQp + rce->qpNoVbv) / 2;<br>             }<br> <br>+            /* Scenecut Aware QP offsets*/<br>+            if (m_param->bEnableSceneCutAwareQp)<br>+            {<br>+                double qmin = m_lmin[m_sliceType];<br>+                double qmax = m_lmax[m_sliceType];<br>+                q = scenecutAwareMasking(curFrame, q);<br>+                q = x265_clip3(qmin, qmax, q);<br>+                rce->qpNoVbv = x265_qScale2qp(q);<br>+            }<br>+<br>             if (m_isVbv)<br>             {<br>                 /* Do not overflow vbv */<br>@@ -2120,13 +2135,12 @@<br>             {<br>                 double qmin = m_lmin[m_sliceType];<br>                 double qmax = m_lmax[m_sliceType];<br>-                q = scenecutAwareQp(curFrame, q);<br>+                q = scenecutAwareMasking(curFrame, q);<br>                 q = x265_clip3(qmin, qmax, q);<br>                 rce->qpNoVbv = x265_qScale2qp(q);<br>             }<br>             q = clipQscale(curFrame, rce, q);<br> <br>-<br>             if (m_2pass)<br>                 rce->frameSizePlanned = qScale2bits(rce, q);<br>             else<br>@@ -2964,7 +2978,7 @@<br>     if (!curEncData.m_param->bMultiPassOptRPS)<br>     {<br>         if (fprintf(m_statFileOut,<br>-            "in:%d out:%d type:%c q:%.2f q-aq:%.2f q-noVbv:%.2f q-Rceq:%.2f tex:%d mv:%d misc:%d icu:%.2f pcu:%.2f scu:%.2f ;\n",<br>+            "in:%d out:%d type:%c q:%.2f q-aq:%.2f q-noVbv:%.2f q-Rceq:%.2f tex:%d mv:%d misc:%d icu:%.2f pcu:%.2f scu:%.2f sc:%d ;\n",<br>             rce->poc, rce->encodeOrder,<br>             cType, curEncData.m_avgQpRc, curEncData.m_avgQpAq,<br>             rce->qpNoVbv, rce->qRceq,<br>@@ -2973,7 +2987,8 @@<br>             curFrame->m_encData->m_frameStats.miscBits,<br>             curFrame->m_encData->m_frameStats.percent8x8Intra * m_ncu,<br>             curFrame->m_encData->m_frameStats.percent8x8Inter * m_ncu,<br>-            curFrame->m_encData->m_frameStats.percent8x8Skip  * m_ncu) < 0)<br>+            curFrame->m_encData->m_frameStats.percent8x8Skip  * m_ncu,<br>+            curFrame->m_lowres.bScenecut) < 0)<br>             goto writeFailure;<br>     }<br>     else<br>@@ -3150,52 +3165,75 @@<br>     }<br> }<br> <br>-double RateControl::scenecutAwareQp(Frame* curFrame, double q)<br>+double RateControl::scenecutAwareMasking(Frame* curFrame, double q)<br> {<br>     double qp = x265_qScale2qp(q);<br>     uint32_t maxWindowSize = uint32_t((m_param->scenecutWindow / 1000.0) * (m_param->fpsNum / m_param->fpsDenom) + 0.5);<br>     uint32_t windowSize = maxWindowSize / 3;<br>     int lastScenecut = m_top->m_rateControl->m_lastScenecut;<br>     int lastIFrame = m_top->m_rateControl->m_lastScenecutAwareIFrame;<br>-    double maxQpDelta = double(m_param->maxQpDelta);<br>-    double iSliceDelta = double(I_SLICE_DELTA);<br>-    double sliceTypeDelta = SLICE_TYPE_DELTA * maxQpDelta;<br>-    double window2Delta = WINDOW2_DELTA * maxQpDelta;<br>-    double window3Delta = WINDOW3_DELTA * maxQpDelta;<br>+    double refQpDelta = double(m_param->refQpDelta);<br>+    double nonRefQpDelta = double(m_param->nonRefQpDelta);<br>+    double sliceTypeDelta = SLICE_TYPE_DELTA * refQpDelta;<br>+    double window2Delta = WINDOW2_DELTA * refQpDelta;<br>+    double window3Delta = WINDOW3_DELTA * refQpDelta;<br> <br>     bool isFrameInsideWindow = curFrame->m_poc > lastScenecut && curFrame->m_poc <= (lastScenecut + int(maxWindowSize));<br>+    if (isFrameInsideWindow)<br>+        curFrame->m_isInsideWindow = 2;<br> <br>-    if (isFrameInsideWindow && IS_X265_TYPE_I(curFrame->m_lowres.sliceType))<br>+    if (isFrameInsideWindow && (IS_X265_TYPE_I(curFrame->m_lowres.sliceType) || curFrame->m_lowres.bScenecut))<br>     {<br>         m_top->m_rateControl->m_lastScenecutAwareIFrame = curFrame->m_poc;<br>     }<br>     else if (isFrameInsideWindow && (curFrame->m_lowres.sliceType == X265_TYPE_P))<br>     {<br>         if (!(lastIFrame > lastScenecut && lastIFrame <= (lastScenecut + int(maxWindowSize))<br>-            && curFrame->m_poc > lastIFrame))<br>+            && curFrame->m_poc >= lastIFrame))<br>         {<br>-            qp += maxQpDelta - sliceTypeDelta;<br>+            qp += refQpDelta - sliceTypeDelta;<br>+            if (((curFrame->m_poc) > (lastScenecut + int(windowSize))) && ((curFrame->m_poc) <= (lastScenecut + 2 * int(windowSize))))<br>+                qp -= window2Delta;<br>+            else if (curFrame->m_poc > lastScenecut + 2 * int(windowSize))<br>+                qp -= window3Delta;<br>+        }<br>+    }<br>+    else if (isFrameInsideWindow && (curFrame->m_lowres.sliceType == X265_TYPE_BREF))<br>+    {<br>+        if (!(lastIFrame > lastScenecut && lastIFrame <= (lastScenecut + int(maxWindowSize))<br>+            && curFrame->m_poc >= lastIFrame))<br>+        {<br>+                qp += refQpDelta;<br>             if (((curFrame->m_poc) > (lastScenecut + int(windowSize))) && ((curFrame->m_poc) <= (lastScenecut + 2 * int(windowSize))))<br>                 qp -= window2Delta;<br>             else if (curFrame->m_poc > lastScenecut + 2 * int(windowSize))<br>                 qp -= window3Delta;<br>         }<br>     }<br>-    else if (isFrameInsideWindow && IS_X265_TYPE_B(curFrame->m_lowres.sliceType))<br>+    else if (isFrameInsideWindow && (curFrame->m_lowres.sliceType == X265_TYPE_B))<br>     {<br>         if (!(lastIFrame > lastScenecut && lastIFrame <= (lastScenecut + int(maxWindowSize))<br>-            && curFrame->m_poc > lastIFrame))<br>+            && curFrame->m_poc >= lastIFrame))<br>         {<br>-            qp += maxQpDelta;<br>-            if (curFrame->m_lowres.sliceType == X265_TYPE_B)<br>-                qp += sliceTypeDelta;<br>+            qp += nonRefQpDelta;<br>             if (((curFrame->m_poc) > (lastScenecut + int(windowSize))) && ((curFrame->m_poc) <= (lastScenecut + 2 * int(windowSize))))<br>                 qp -= window2Delta;<br>             else if (curFrame->m_poc > lastScenecut + 2 * int(windowSize))<br>                 qp -= window3Delta;<br>         }<br>     }<br>-    if (IS_X265_TYPE_I(curFrame->m_lowres.sliceType) && curFrame->m_lowres.bScenecut)<br>-        qp = qp - iSliceDelta;<br>-    return  x265_qp2qScale(qp);<br>+<br>+    if (curFrame->m_isInsideWindow == 1)<br>+    {<br>+        refQpDelta -= window3Delta;<br>+        nonRefQpDelta -= window3Delta;<br>+        if (curFrame->m_lowres.sliceType == X265_TYPE_P)<br>+            qp += refQpDelta - sliceTypeDelta;<br>+        else if (curFrame->m_lowres.sliceType == X265_TYPE_BREF)<br>+            qp += refQpDelta;<br>+        else if (curFrame->m_lowres.sliceType == X265_TYPE_B)<br>+            qp += nonRefQpDelta;<br>+    }<br>+<br>+    return x265_qp2qScale(qp);<br> }<br>diff --git a/source/encoder/ratecontrol.h b/source/encoder/ratecontrol.h<br>--- a/source/encoder/ratecontrol.h<br>+++ b/source/encoder/ratecontrol.h<br>@@ -47,8 +47,6 @@<br> #define CLIP_DURATION(f) x265_clip3(MIN_FRAME_DURATION, MAX_FRAME_DURATION, f)<br> <br> /*Scenecut Aware QP*/<br>-#define I_SLICE_DELTA           2   /* Subtracted from base QP for the scenecut I frames*/<br>-#define SLICE_TYPE_DELTA        0.3 /* The offset decremented or incremented for P-frames or b-frames respectively*/<br> #define WINDOW1_DELTA           0   /* The offset for the frames coming in the window-1*/<br> #define WINDOW2_DELTA           0.3 /* The offset for the frames coming in the window-2*/<br> #define WINDOW3_DELTA           0.6 /* The offset for the frames coming in the window-3*/<br>@@ -271,7 +269,7 @@<br>     int writeRateControlFrameStats(Frame* curFrame, RateControlEntry* rce);<br>     bool   initPass2();<br> <br>-    double scenecutAwareQp(Frame* curFrame, double q);<br>+    double scenecutAwareMasking(Frame* curFrame, double q);<br> <br> protected:<br> <br>diff --git a/source/test/rate-control-tests.txt b/source/test/rate-control-tests.txt<br>--- a/source/test/rate-control-tests.txt<br>+++ b/source/test/rate-control-tests.txt<br>@@ -44,6 +44,8 @@<br> RaceHorses_416x240_30_10bit.yuv,--preset medium --crf 26 --vbv-maxrate 1000 --vbv-bufsize 1000 --pass 1::--preset fast --bitrate 1000  --vbv-maxrate 1000 --vbv-bufsize 700 --pass 3 -F4::--preset slow --bitrate 500 --vbv-maxrate 500  --vbv-bufsize 700 --pass 2 -F4<br> sita_1920x1080_30.yuv, --preset ultrafast --crf 20 --no-cutree --keyint 50 --min-keyint 50 --no-open-gop --pass 1 --vbv-bufsize 7000 --vbv-maxrate 5000:: --preset ultrafast --crf 20 --no-cutree --keyint 50 --min-keyint 50 --no-open-gop --pass 2 --vbv-bufsize 7000 --vbv-maxrate 5000 --repeat-headers<br> sita_1920x1080_30.yuv, --preset medium --crf 20 --no-cutree --keyint 50 --min-keyint 50 --no-open-gop --pass 1 --vbv-bufsize 7000 --vbv-maxrate 5000 --repeat-headers --multi-pass-opt-rps:: --preset medium --crf 20 --no-cutree --keyint 50 --min-keyint 50 --no-open-gop --pass 2 --vbv-bufsize 7000 --vbv-maxrate 5000 --repeat-headers --multi-pass-opt-rps<br>+sintel_trailer_2k_1920x1080_24.yuv,--preset medium --bitrate 6000 --no-cutree --aq-mode 0 --pass 1::--preset medium --bitrate 6000 --no-cutree --aq-mode 0 --pass 2 --scenecut-aware-qp<br>+sintel_trailer_2k_1920x1080_24.yuv,--preset medium --bitrate 6000 --no-cutree --aq-mode 0 --hist-scenecut --pass 1::--preset medium --bitrate 6000 --no-cutree --aq-mode 0 --hist-scenecut --pass 2 --scenecut-aware-qp --qp-delta-nonref 8<br> <br> # multi-pass rate control and analysis<br> ducks_take_off_1080p50.y4m,--bitrate 6000 --pass 1  --multi-pass-opt-analysis  --hash 1 --ssim --psnr:: --bitrate 6000 --pass 2  --multi-pass-opt-analysis  --hash 1 --ssim --psnr<br>diff --git a/source/test/regression-tests.txt b/source/test/regression-tests.txt<br>--- a/source/test/regression-tests.txt<br>+++ b/source/test/regression-tests.txt<br>@@ -158,7 +158,6 @@<br> ducks_take_off_420_1_720p50.y4m,--preset medium --selective-sao 4 --sao --crf 20<br> Traffic_4096x2048_30p.y4m, --preset medium --frame-dup --dup-threshold 60 --hrd --bitrate 10000 --vbv-bufsize 15000 --vbv-maxrate 12000<br> Kimono1_1920x1080_24_400.yuv,--preset superfast --qp 28 --zones 0,139,q=32<br>-Island_960x540_24.yuv,--no-cutree --aq-mode 0 --bitrate 6000 --scenecut-aware-qp<br> sintel_trailer_2k_1920x1080_24.yuv, --preset medium --hist-scenecut --hist-threshold 0.02 --frame-dup --dup-threshold 60 --hrd --bitrate 10000 --vbv-bufsize 15000 --vbv-maxrate 12000<br> sintel_trailer_2k_1920x1080_24.yuv, --preset medium --hist-scenecut --hist-threshold 0.02<br> sintel_trailer_2k_1920x1080_24.yuv, --preset ultrafast --hist-scenecut --hist-threshold 0.02<br>diff --git a/source/x265.h b/source/x265.h<br>--- a/source/x265.h<br>+++ b/source/x265.h<br>@@ -607,6 +607,8 @@<br> #define X265_ANALYSIS_SAVE 1<br> #define X265_ANALYSIS_LOAD 2<br> <br>+#define SLICE_TYPE_DELTA        0.3 /* The offset decremented or incremented for P-frames or b-frames respectively*/<br>+<br> typedef struct x265_cli_csp<br> {<br>     int planes;<br>@@ -1843,9 +1845,8 @@<br>       Default 1 (Enabled). API only. */<br>     int       bResetZoneConfig;<br> <br>-    /* Enables a ratecontrol algorithm for reducing the bits spent on the inter-frames<br>-     * within the scenecutWindow after a scenecut by increasing their QP without<br>-     * any deterioration in visual quality. It also increases the quality of scenecut I-Frames by reducing their QP.<br>+    /* It reduces the bits spent on the inter-frames within the scenecutWindow before and after a scenecut<br>+     * by increasing their QP in ratecontrol pass2 algorithm without any deterioration in visual quality.<br>      * Default is disabled. */<br>     int       bEnableSceneCutAwareQp;<br> <br>@@ -1855,7 +1856,10 @@<br> <br>     /* The offset by which QP is incremented for inter-frames when bEnableSceneCutAwareQp is set.<br>      * Default is +5. */<br>-    int       maxQpDelta;<br>+    double       refQpDelta;<br>+<br>+    /* The offset by which QP is incremented for non-referenced inter-frames when bEnableSceneCutAwareQp is set. */<br>+    double       nonRefQpDelta;<br> <br>     /* A genuine threshold used for histogram based scene cut detection.<br>      * This threshold determines whether a frame is a scenecut or not<br>diff --git a/source/x265cli.cpp b/source/x265cli.cpp<br>--- a/source/x265cli.cpp<br>+++ b/source/x265cli.cpp<br>@@ -179,7 +179,8 @@<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 inside the scenecut window after scenecut. Default %s\n", OPT(param->bEnableSceneCutAwareQp));<br>         H1("   --scenecut-window <0..1000>   QP incremental duration(in milliseconds) when scenecut-aware-qp is enabled. Default %d\n", param->scenecutWindow);<br>-        H1("   --max-qp-delta <0..10>        QP offset to increment with base QP for inter-frames. Default %d\n", param->maxQpDelta);<br>+        H1("   --qp-delta-ref <0..10>        QP offset to increment with base QP for inter-frames. Default %f\n", param->refQpDelta);<br>+        H1("   --qp-delta-nonref <0..10>     QP offset to increment with base QP for non-referenced inter-frames. Default %f\n", param->nonRefQpDelta);<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>diff --git a/source/x265cli.h b/source/x265cli.h<br>--- a/source/x265cli.h<br>+++ b/source/x265cli.h<br>@@ -151,7 +151,8 @@<br>     { "scenecut-aware-qp",    no_argument, NULL, 0 },<br>     { "no-scenecut-aware-qp", no_argument, NULL, 0 },<br>     { "scenecut-window",required_argument, NULL, 0 },<br>-    { "max-qp-delta",   required_argument, NULL, 0 },<br>+    { "qp-delta-ref",   required_argument, NULL, 0 },<br>+    { "qp-delta-nonref",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><div><div dir="ltr"><div dir="ltr"><div><div dir="ltr"><div dir="ltr"><div dir="ltr"><font face="verdana, sans-serif" color="#0c343d"><br></font></div><div dir="ltr"><font face="verdana, sans-serif" color="#0c343d">Thanks & Regards</font><div><font face="verdana, sans-serif" color="#0c343d"><b>Niranjan Kumar B</b></font></div><div><font face="verdana, sans-serif" color="#0c343d" size="1">Video Codec Engineer </font></div><div><font face="verdana, sans-serif" color="#0c343d" size="1">Media & AI Analytics</font></div><div><font color="#0c343d" face="trebuchet ms, sans-serif">+91 958 511 1449</font></div><div><a style="color:rgb(17,85,204)" href="https://multicorewareinc.com/" target="_blank"><img src="https://docs.google.com/uc?export=download&id=1kc3RJu9M8bnIf6Xa5rUw2d-eEVUsPBE5&revid=0B7tw9XJBmynaemR1VUpQUi9DVytRVW5SVkRwVTFjb1hBMUcwPQ"></a></div></div></div></div></div></div></div></div></div>
</blockquote></div>
</blockquote></div>
</blockquote></div>