[x265] [X265][PATCH][RELEASE_3.5] Modify Window offsets relative to slice-types for --scenecut-aware-qp

Niranjan Bala niranjan at multicorewareinc.com
Fri Feb 5 09:58:24 UTC 2021


>From 50d1b4a2ad5e3c817b60141b6371d3dd59c89e9c Mon Sep 17 00:00:00 2001
From: Niranjan <niranjan at multicorewareinc.com>
Date: Mon, 1 Feb 2021 14:24:19 +0530
Subject: [PATCH] Modify Window offsets relative to slice-types for
 --scenecut-aware-qp

---
 source/common/param.cpp        | 75 ++++++++++++++++++++++------------
 source/encoder/ratecontrol.cpp | 36 ++++++++--------
 source/encoder/ratecontrol.h   |  6 +--
 3 files changed, 71 insertions(+), 46 deletions(-)

diff --git a/source/common/param.cpp b/source/common/param.cpp
index 8a6a974f1..8695ad686 100755
--- a/source/common/param.cpp
+++ b/source/common/param.cpp
@@ -1355,41 +1355,62 @@ int x265_param_parse(x265_param* p, const char*
name, const char* value)

             if (p->bEnableSceneCutAwareQp == FORWARD)
             {
-                sscanf(value, "%d,%lf,%lf", &window1, &refQpDelta1,
&nonRefQpDelta1);
-                if (window1 > 0)
-                    p->fwdScenecutWindow = window1;
-                if (refQpDelta1 > 0)
-                    p->fwdRefQpDelta = refQpDelta1;
-                if (nonRefQpDelta1 > 0)
-                    p->fwdNonRefQpDelta = nonRefQpDelta1;
+                if (3 == sscanf(value, "%d,%lf,%lf", &window1,
&refQpDelta1, &nonRefQpDelta1))
+                {
+                    if (window1 > 0)
+                        p->fwdScenecutWindow = window1;
+                    if (refQpDelta1 > 0)
+                        p->fwdRefQpDelta = refQpDelta1;
+                    if (nonRefQpDelta1 > 0)
+                        p->fwdNonRefQpDelta = nonRefQpDelta1;
+                }
+                else
+                {
+                    x265_log(NULL, X265_LOG_ERROR, "Specify all the
necessary offsets for masking-strength \n");
+                    bError = true;
+                }
             }
             else if (p->bEnableSceneCutAwareQp == BACKWARD)
             {
-                sscanf(value, "%d,%lf,%lf", &window1, &refQpDelta1,
&nonRefQpDelta1);
-                if (window1 > 0)
-                    p->bwdScenecutWindow = window1;
-                if (refQpDelta1 > 0)
-                    p->bwdRefQpDelta = refQpDelta1;
-                if (nonRefQpDelta1 > 0)
-                    p->bwdNonRefQpDelta = nonRefQpDelta1;
+                if (3 == sscanf(value, "%d,%lf,%lf", &window1,
&refQpDelta1, &nonRefQpDelta1))
+                {
+                    if (window1 > 0)
+                        p->bwdScenecutWindow = window1;
+                    if (refQpDelta1 > 0)
+                        p->bwdRefQpDelta = refQpDelta1;
+                    if (nonRefQpDelta1 > 0)
+                        p->bwdNonRefQpDelta = nonRefQpDelta1;
+                }
+                else
+                {
+                    x265_log(NULL, X265_LOG_ERROR, "Specify all the
necessary offsets for masking-strength \n");
+                    bError = true;
+                }
             }
             else if (p->bEnableSceneCutAwareQp == BI_DIRECTIONAL)
             {
                 int window2;
                 double refQpDelta2, nonRefQpDelta2;
-                sscanf(value, "%d,%lf,%lf,%d,%lf,%lf", &window1,
&refQpDelta1, &nonRefQpDelta1, &window2, &refQpDelta2, &nonRefQpDelta2);
-                if (window1 > 0)
-                    p->fwdScenecutWindow = window1;
-                if (refQpDelta1 > 0)
-                    p->fwdRefQpDelta = refQpDelta1;
-                if (nonRefQpDelta1 > 0)
-                    p->fwdNonRefQpDelta = nonRefQpDelta1;
-                if (window2 > 0)
-                    p->bwdScenecutWindow = window2;
-                if (refQpDelta2 > 0)
-                    p->bwdRefQpDelta = refQpDelta2;
-                if (nonRefQpDelta2 > 0)
-                    p->bwdNonRefQpDelta = nonRefQpDelta2;
+                if (6 == sscanf(value, "%d,%lf,%lf,%d,%lf,%lf", &window1,
&refQpDelta1, &nonRefQpDelta1, &window2, &refQpDelta2, &nonRefQpDelta2))
+                {
+                    if (window1 > 0)
+                        p->fwdScenecutWindow = window1;
+                    if (refQpDelta1 > 0)
+                        p->fwdRefQpDelta = refQpDelta1;
+                    if (nonRefQpDelta1 > 0)
+                        p->fwdNonRefQpDelta = nonRefQpDelta1;
+                    if (window2 > 0)
+                        p->bwdScenecutWindow = window2;
+                    if (refQpDelta2 > 0)
+                        p->bwdRefQpDelta = refQpDelta2;
+                    if (nonRefQpDelta2 > 0)
+                        p->bwdNonRefQpDelta = nonRefQpDelta2;
+                }
+                else
+                {
+                    x265_log(NULL, X265_LOG_ERROR, "Specify all the
necessary offsets for masking-strength \n");
+                    bError = true;
+                }
             }
         }
         OPT("field") p->bField = atobool( value );
diff --git a/source/encoder/ratecontrol.cpp b/source/encoder/ratecontrol.cpp
index 1b46c0148..a4756de39 100644
--- a/source/encoder/ratecontrol.cpp
+++ b/source/encoder/ratecontrol.cpp
@@ -3193,9 +3193,8 @@ double RateControl::forwardMasking(Frame* curFrame,
double q)
     double fwdRefQpDelta = double(m_param->fwdRefQpDelta);
     double fwdNonRefQpDelta = double(m_param->fwdNonRefQpDelta);
     double sliceTypeDelta = SLICE_TYPE_DELTA * fwdRefQpDelta;
-    double window2Delta = WINDOW2_DELTA * fwdRefQpDelta;
-    double window3Delta = WINDOW3_DELTA * fwdRefQpDelta;

+    //Check whether the current frame is within the forward window
     if (curFrame->m_poc > lastScenecut && curFrame->m_poc <= (lastScenecut
+ int(maxWindowSize)))
         curFrame->m_isInsideWindow = FORWARD_WINDOW;
     if (curFrame->m_isInsideWindow == FORWARD_WINDOW)
@@ -3209,11 +3208,13 @@ double RateControl::forwardMasking(Frame* curFrame,
double q)
             if (!(lastIFrame > lastScenecut && lastIFrame <= (lastScenecut
+ int(maxWindowSize))
                 && curFrame->m_poc >= lastIFrame))
             {
-                qp += fwdRefQpDelta - sliceTypeDelta;
-                if (((curFrame->m_poc) > (lastScenecut + int(windowSize)))
&& ((curFrame->m_poc) <= (lastScenecut + 2 * int(windowSize))))
-                    qp -= window2Delta;
+                //Add offsets corresponding to the window in which the
P-frame occurs
+                if (curFrame->m_poc <= (lastScenecut + int(windowSize)))
+                    qp += WINDOW1_DELTA * (fwdRefQpDelta - sliceTypeDelta);
+                else if (((curFrame->m_poc) > (lastScenecut +
int(windowSize))) && ((curFrame->m_poc) <= (lastScenecut + 2 *
int(windowSize))))
+                    qp += WINDOW2_DELTA * (fwdRefQpDelta - sliceTypeDelta);
                 else if (curFrame->m_poc > lastScenecut + 2 *
int(windowSize))
-                    qp -= window3Delta;
+                    qp += WINDOW3_DELTA * (fwdRefQpDelta - sliceTypeDelta);
             }
         }
         else if (curFrame->m_lowres.sliceType == X265_TYPE_BREF)
@@ -3221,11 +3222,13 @@ double RateControl::forwardMasking(Frame* curFrame,
double q)
             if (!(lastIFrame > lastScenecut && lastIFrame <= (lastScenecut
+ int(maxWindowSize))
                 && curFrame->m_poc >= lastIFrame))
             {
-                qp += fwdRefQpDelta;
-                if (((curFrame->m_poc) > (lastScenecut + int(windowSize)))
&& ((curFrame->m_poc) <= (lastScenecut + 2 * int(windowSize))))
-                    qp -= window2Delta;
+                //Add offsets corresponding to the window in which the
B-frame occurs
+                if (curFrame->m_poc <= (lastScenecut + int(windowSize)))
+                    qp += WINDOW1_DELTA * fwdRefQpDelta;
+                else if (((curFrame->m_poc) > (lastScenecut +
int(windowSize))) && ((curFrame->m_poc) <= (lastScenecut + 2 *
int(windowSize))))
+                    qp += WINDOW2_DELTA * fwdRefQpDelta;
                 else if (curFrame->m_poc > lastScenecut + 2 *
int(windowSize))
-                    qp -= window3Delta;
+                    qp += WINDOW3_DELTA * fwdRefQpDelta;
             }
         }
         else if (curFrame->m_lowres.sliceType == X265_TYPE_B)
@@ -3233,11 +3236,13 @@ double RateControl::forwardMasking(Frame* curFrame,
double q)
             if (!(lastIFrame > lastScenecut && lastIFrame <= (lastScenecut
+ int(maxWindowSize))
                 && curFrame->m_poc >= lastIFrame))
             {
-                qp += fwdNonRefQpDelta;
-                if (((curFrame->m_poc) > (lastScenecut + int(windowSize)))
&& ((curFrame->m_poc) <= (lastScenecut + 2 * int(windowSize))))
-                    qp -= window2Delta;
+                //Add offsets corresponding to the window in which the
b-frame occurs
+                if (curFrame->m_poc <= (lastScenecut + int(windowSize)))
+                    qp += WINDOW1_DELTA * fwdNonRefQpDelta;
+                else if (((curFrame->m_poc) > (lastScenecut +
int(windowSize))) && ((curFrame->m_poc) <= (lastScenecut + 2 *
int(windowSize))))
+                    qp += WINDOW2_DELTA * fwdNonRefQpDelta;
                 else if (curFrame->m_poc > lastScenecut + 2 *
int(windowSize))
-                    qp -= window3Delta;
+                    qp += WINDOW3_DELTA * fwdNonRefQpDelta;
             }
         }
     }
@@ -3248,14 +3253,13 @@ double RateControl::backwardMasking(Frame*
curFrame, double q)
 {
     double qp = x265_qScale2qp(q);
     double fwdRefQpDelta = double(m_param->fwdRefQpDelta);
-    double window3Delta = WINDOW3_DELTA * fwdRefQpDelta;
     double bwdRefQpDelta = double(m_param->bwdRefQpDelta);
     double bwdNonRefQpDelta = double(m_param->bwdNonRefQpDelta);

     if (curFrame->m_isInsideWindow == BACKWARD_WINDOW)
     {
         if (bwdRefQpDelta < 0)
-            bwdRefQpDelta = fwdRefQpDelta - window3Delta;
+            bwdRefQpDelta = WINDOW3_DELTA * fwdRefQpDelta;
         double sliceTypeDelta = SLICE_TYPE_DELTA * bwdRefQpDelta;
         if (bwdNonRefQpDelta < 0)
             bwdNonRefQpDelta = bwdRefQpDelta + sliceTypeDelta;
diff --git a/source/encoder/ratecontrol.h b/source/encoder/ratecontrol.h
index 148269b6f..996465eeb 100644
--- a/source/encoder/ratecontrol.h
+++ b/source/encoder/ratecontrol.h
@@ -47,9 +47,9 @@ struct SPS;
 #define CLIP_DURATION(f) x265_clip3(MIN_FRAME_DURATION,
MAX_FRAME_DURATION, f)

 /*Scenecut Aware QP*/
-#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*/
+#define WINDOW1_DELTA           1.0 /* The offset for the frames coming in
the window-1*/
+#define WINDOW2_DELTA           0.7 /* The offset for the frames coming in
the window-2*/
+#define WINDOW3_DELTA           0.4 /* The offset for the frames coming in
the window-3*/

 struct Predictor
 {
-- 
2.18.0.windows.1


-- 

Thanks & Regards
*Niranjan Kumar B*
Video Codec Engineer
Media & AI Analytics
+91 958 511 1449
<https://multicorewareinc.com/>
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://mailman.videolan.org/pipermail/x265-devel/attachments/20210205/077df002/attachment-0001.html>
-------------- next part --------------
A non-text attachment was scrubbed...
Name: x265.diff
Type: application/octet-stream
Size: 10881 bytes
Desc: not available
URL: <http://mailman.videolan.org/pipermail/x265-devel/attachments/20210205/077df002/attachment-0001.obj>


More information about the x265-devel mailing list