<div dir="ltr"><div class="gmail_extra"><br><div class="gmail_quote">On Mon, Nov 20, 2017 at 11:12 AM,  <span dir="ltr"><<a href="mailto:aruna@multicorewareinc.com" target="_blank">aruna@multicorewareinc.com</a>></span> wrote:<br><blockquote class="gmail_quote" style="margin:0 0 0 .8ex;border-left:1px #ccc solid;padding-left:1ex"># HG changeset patch<br>
# User Aruna Matheswaran <<a href="mailto:aruna@multicorewareinc.com">aruna@multicorewareinc.com</a>><br>
# Date 1511155419 -19800<br>
#      Mon Nov 20 10:53:39 2017 +0530<br>
# Node ID d37d703039dae201c5a7114ff71e6c<wbr>4d4196347c<br>
# Parent  0f0d608e732be425d71169b4cf0563<wbr>adbfb7729b<br>
Extend gop boundary by doing gop lookahead<br></blockquote><div><br></div><div>Patch doesn't apply on default tip. Please fix and resend.</div><div> </div><blockquote class="gmail_quote" style="margin:0 0 0 .8ex;border-left:1px #ccc solid;padding-left:1ex">
<br>
diff -r 0f0d608e732b -r d37d703039da doc/reST/cli.rst<br>
--- a/doc/reST/cli.rst  Fri Nov 17 19:23:14 2017 +0530<br>
+++ b/doc/reST/cli.rst  Mon Nov 20 10:53:39 2017 +0530<br>
@@ -1373,6 +1373,14 @@<br>
        Default 20<br>
<br>
        **Range of values:** Between the maximum consecutive bframe count (:option:`--bframes`) and 250<br>
+<br>
+.. option:: --gop-lookahead <integer><br>
+<br>
+       Number of frames for GOP boundary decision lookahead. If a scenecut frame is found<br>
+       within this from the gop boundary set by `--keyint`, the GOP will be extented until such a point,<br>
+       otherwise the GOP will be terminated as set by `--keyint`. Default 0.<br>
+<br>
+       **Range of values:** Between 0 and `--rc-lookahead`.<br>
<br>
 .. option:: --lookahead-slices <0..16><br>
<br>
diff -r 0f0d608e732b -r d37d703039da source/CMakeLists.txt<br>
--- a/source/CMakeLists.txt     Fri Nov 17 19:23:14 2017 +0530<br>
+++ b/source/CMakeLists.txt     Mon Nov 20 10:53:39 2017 +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 146)<br>
+set(X265_BUILD 147)<br>
 configure_file("${PROJECT_<wbr>SOURCE_DIR}/<a href="http://x265.def.in" rel="noreferrer" target="_blank">x265.def.in</a>"<br>
                "${PROJECT_BINARY_DIR}/x265.<wbr>def")<br>
 configure_file("${PROJECT_<wbr>SOURCE_DIR}/<a href="http://x265_config.h.in" rel="noreferrer" target="_blank">x265_config.h.in</a>"<br>
diff -r 0f0d608e732b -r d37d703039da source/common/param.cpp<br>
--- a/source/common/param.cpp   Fri Nov 17 19:23:14 2017 +0530<br>
+++ b/source/common/param.cpp   Mon Nov 20 10:53:39 2017 +0530<br>
@@ -144,6 +144,7 @@<br>
     /* Coding Structure */<br>
     param->keyframeMin = 0;<br>
     param->keyframeMax = 250;<br>
+    param->gopLookahead = 0;<br>
     param->bOpenGOP = 1;<br>
     param->bframes = 4;<br>
     param->lookaheadDepth = 20;<br>
@@ -1004,6 +1005,7 @@<br>
                 bError = true;<br>
             }<br>
          }<br>
+        OPT("gop-lookahead") p->gopLookahead = atoi(value);<br>
         else<br>
             return X265_PARAM_BAD_NAME;<br>
     }<br>
@@ -1314,6 +1316,8 @@<br>
           "Valid penalty for 32x32 intra TU in non-I slices. 0:disabled 1:RD-penalty 2:maximum");<br>
     CHECK(param->keyframeMax < -1,<br>
           "Invalid max IDR period in frames. value should be greater than -1");<br>
+    CHECK(param->gopLookahead < -1,<br>
+          "GOP lookahead must be greater than -1");<br>
     CHECK(param-><wbr>decodedPictureHashSEI < 0 || param->decodedPictureHashSEI > 3,<br>
           "Invalid hash option. Decoded Picture Hash SEI 0: disabled, 1: MD5, 2: CRC, 3: Checksum");<br>
     CHECK(param->rc.vbvBufferSize < 0,<br>
@@ -1561,6 +1565,7 @@<br>
     BOOL(p->bOpenGOP, "open-gop");<br>
     s += sprintf(s, " min-keyint=%d", p->keyframeMin);<br>
     s += sprintf(s, " keyint=%d", p->keyframeMax);<br>
+    s += sprintf(s, " gop-lookahead=%d", p->gopLookahead);<br>
     s += sprintf(s, " bframes=%d", p->bframes);<br>
     s += sprintf(s, " b-adapt=%d", p->bFrameAdaptive);<br>
     BOOL(p->bBPyramid, "b-pyramid");<br>
diff -r 0f0d608e732b -r d37d703039da source/encoder/slicetype.cpp<br>
--- a/source/encoder/slicetype.cpp      Fri Nov 17 19:23:14 2017 +0530<br>
+++ b/source/encoder/slicetype.cpp      Mon Nov 20 10:53:39 2017 +0530<br>
@@ -589,6 +589,7 @@<br>
     m_outputSignalRequired = false;<br>
     m_isActive = true;<br>
     m_inputCount = 0;<br>
+    m_extendGopBoundary = false;<br>
<br>
     m_8x8Height = ((m_param->sourceHeight / 2) + X265_LOWRES_CU_SIZE - 1) >> X265_LOWRES_CU_BITS;<br>
     m_8x8Width = ((m_param->sourceWidth / 2) + X265_LOWRES_CU_SIZE - 1) >> X265_LOWRES_CU_BITS;<br>
@@ -647,6 +648,12 @@<br>
         m_numCoopSlices = 1;<br>
     }<br>
<br>
+    if (param->gopLookahead > param->lookaheadDepth)<br>
+    {<br>
+        x265_log(param, X265_LOG_WARNING, "Gop-lookahead cannot be greater than rc-lookahead; Clipping gop-lookahead to rc-lookahead\n");<br>
+        param->gopLookahead = param->lookaheadDepth;<br>
+    }<br>
+<br>
 #if DETAILED_CU_STATS<br>
     m_slicetypeDecideElapsedTime = 0;<br>
     m_preLookaheadElapsedTime = 0;<br>
@@ -1086,7 +1093,8 @@<br>
             x265_log(m_param, X265_LOG_WARNING, "B-ref at frame %d incompatible with B-pyramid and %d reference frames\n",<br>
                      frm.sliceType, m_param->maxNumReferences);<br>
         }<br>
-        if ((!m_param->bIntraRefresh || frm.frameNum == 0) && frm.frameNum - m_lastKeyframe >= m_param->keyframeMax)<br>
+        if ((!m_param->bIntraRefresh || frm.frameNum == 0) && frm.frameNum - m_lastKeyframe >= m_param->keyframeMax &&<br>
+            (!m_extendGopBoundary || frm.frameNum - m_lastKeyframe >= m_param->keyframeMax + m_param->gopLookahead))<br>
         {<br>
             if (frm.sliceType == X265_TYPE_AUTO || frm.sliceType == X265_TYPE_I)<br>
                 frm.sliceType = m_param->bOpenGOP && m_lastKeyframe >= 0 ? X265_TYPE_I : X265_TYPE_IDR;<br>
@@ -1380,7 +1388,14 @@<br>
<br>
     frames[framecnt + 1] = NULL;<br>
<br>
-    keyintLimit = m_param->keyframeMax - frames[0]->frameNum + m_lastKeyframe - 1;<br>
+    int keyFrameLimit = m_param->keyframeMax + m_lastKeyframe - frames[1]->frameNum;<br>
+<br>
+    if (m_param->gopLookahead && keyFrameLimit < m_param->bframes + 2)<br>
+        keyintLimit = m_param->keyframeMax + m_param->gopLookahead - frames[0]->frameNum + m_lastKeyframe - 1;<br>
+    else<br>
+        keyintLimit = m_param->keyframeMax - frames[0]->frameNum + m_lastKeyframe - 1;<br>
+<br>
+<br></blockquote><div><br></div><div>Use keyFrameLimit to evaluate keyintLimit so that the evaluation on the RHS of the assignment can be simplified.</div><div><br></div><blockquote class="gmail_quote" style="margin:0 0 0 .8ex;border-left:1px #ccc solid;padding-left:1ex">
     origNumFrames = numFrames = m_param->bIntraRefresh ? framecnt : X265_MIN(framecnt, keyintLimit);<br>
<br>
     if (bIsVbvLookahead)<br>
@@ -1473,6 +1488,26 @@<br>
         return;<br>
     }<br>
<br>
+    if (m_param->gopLookahead && keyFrameLimit >= 0 && keyFrameLimit < m_param->bframes + 2)<br></blockquote><div><br></div><div>Use parenthesis here to make each conditional check explicit.</div><div> </div><blockquote class="gmail_quote" style="margin:0 0 0 .8ex;border-left:1px #ccc solid;padding-left:1ex">
+    {<br>
+        bool sceneTransition = m_isSceneTransition;<br>
+        m_extendGopBoundary = false;<br>
+        for (int i = m_param->bframes + 1; i < origNumFrames; i += m_param->bframes + 1)<br>
+        {<br>
+            scenecut(frames, i, i + 1, true, origNumFrames);<br>
+            for (int j = i + 1; j <= X265_MIN(i + m_param->bframes + 1, origNumFrames); j++)<br>
+            {<br>
+                if (frames[j]->bScenecut && m_isSceneTransition && scenecut(frames, j - 1, j, false, origNumFrames))<br>
+                {<br>
+                    m_extendGopBoundary = true;<br>
+                    break;<br>
+                }<br>
+            }<br>
+            if (m_extendGopBoundary)<br>
+                break;<br>
+        }<br>
+        m_isSceneTransition = sceneTransition;<br>
+    }<br>
     if (m_param->bframes)<br>
     {<br>
         if (m_param->bFrameAdaptive == X265_B_ADAPT_TRELLIS)<br>
@@ -1579,6 +1614,9 @@<br>
     if (m_param->rc.cuTree)<br>
         cuTree(frames, X265_MIN(numFrames, m_param->keyframeMax), bKeyframe);<br>
<br>
+    if (m_param->gopLookahead && keyFrameLimit >= 0 && keyFrameLimit < m_param->bframes + 2 && !m_extendGopBoundary)<br>
+        keyintLimit = m_param->keyframeMax - frames[0]->frameNum + m_lastKeyframe - 1;<br>
+<br>
     if (!m_param->bIntraRefresh)<br>
         for (int j = keyintLimit + 1; j <= numFrames; j += m_param->keyframeMax)<br>
         {<br>
@@ -1588,8 +1626,9 @@<br>
<br>
     if (bIsVbvLookahead)<br>
         vbvLookahead(frames, numFrames, bKeyframe);<br>
-<br>
-     int maxp1 = X265_MIN(m_param->bframes + 1, origNumFrames);<br>
+<br>
+    int maxp1 = X265_MIN(m_param->bframes + 1, origNumFrames);<br>
+<br>
     /* Restore frame types for all frames that haven't actually been decided yet. */<br>
     for (int j = resetStart; j <= numFrames; j++)<br>
     {<br>
@@ -1613,8 +1652,8 @@<br>
         bool fluctuate = false;<br>
         bool noScenecuts = false;<br>
         int64_t avgSatdCost = 0;<br>
-        if (frames[0]->costEst[1][0] > -1)<br>
-            avgSatdCost = frames[0]->costEst[1][0];<br>
+        if (frames[p0]->costEst[p1 - p0][0] > -1)<br>
+            avgSatdCost = frames[p0]->costEst[p1 - p0][0];<br>
         int cnt = 1;<br>
         /* Where A and B are scenes: AAAAAABBBAAAAAA<br>
          * If BBB is shorter than (maxp1-p0), it is detected as a flash<br>
@@ -1717,14 +1756,21 @@<br>
             bias = threshMin / 4;<br>
         else if (gopSize <= m_param->keyframeMin)<br>
             bias = threshMin * gopSize / m_param->keyframeMin;<br>
-        else<br>
+        else if (gopSize <= m_param->keyframeMax)<br>
         {<br>
             bias = threshMin<br>
                 + (threshMax - threshMin)<br>
                 * (gopSize - m_param->keyframeMin)<br>
                 / (m_param->keyframeMax - m_param->keyframeMin);<br>
         }<br>
-    }<br>
+        else<br>
+        {<br>
+            bias = threshMin<br>
+                + (threshMax - threshMin)<br>
+                * (gopSize - m_param->keyframeMax)<br>
+                / (m_param->gopLookahead);<br>
+        }<br>
+ }<br>
     bool res = pcost >= (1.0 - bias) * icost;<br>
     if (res && bRealScenecut)<br>
     {<br>
diff -r 0f0d608e732b -r d37d703039da source/encoder/slicetype.h<br>
--- a/source/encoder/slicetype.h        Fri Nov 17 19:23:14 2017 +0530<br>
+++ b/source/encoder/slicetype.h        Mon Nov 20 10:53:39 2017 +0530<br>
@@ -132,6 +132,7 @@<br>
     bool          m_filled;<br>
     bool          m_isSceneTransition;<br>
     int           m_numPools;<br>
+    bool          m_extendGopBoundary;<br>
     Lookahead(x265_param *param, ThreadPool *pool);<br>
 #if DETAILED_CU_STATS<br>
     int64_t       m_slicetypeDecideElapsedTime;<br>
diff -r 0f0d608e732b -r d37d703039da source/test/regression-tests.<wbr>txt<br>
--- a/source/test/regression-<wbr>tests.txt  Fri Nov 17 19:23:14 2017 +0530<br>
+++ b/source/test/regression-<wbr>tests.txt  Mon Nov 20 10:53:39 2017 +0530<br>
@@ -150,6 +150,7 @@<br>
 Kimono1_1920x1080_24_400.yuv,-<wbr>-preset medium --rdoq-level 0 --limit-refs 3 --slices 2<br>
 Kimono1_1920x1080_24_400.yuv,-<wbr>-preset veryslow --crf 4 --cu-lossless --slices 2 --limit-refs 3 --limit-modes<br>
 Kimono1_1920x1080_24_400.yuv,-<wbr>-preset placebo --ctu 32 --max-tu-size 8 --limit-tu 2<br>
+big_buck_bunny_360p24.y4m, --keyint 60 --min-keyint 40 --gop-lookahead 20<br>
<br>
 # Main12 intraCost overflow bug test<br>
 720p50_parkrun_ter.y4m,--<wbr>preset medium<br>
diff -r 0f0d608e732b -r d37d703039da source/test/smoke-tests.txt<br>
--- a/source/test/smoke-tests.txt       Fri Nov 17 19:23:14 2017 +0530<br>
+++ b/source/test/smoke-tests.txt       Mon Nov 20 10:53:39 2017 +0530<br>
@@ -20,5 +20,6 @@<br>
 DucksAndLegs_1920x1080_60_<wbr>10bit_422.yuv,--preset=fast --weightb --interlace bff<br>
 DucksAndLegs_1920x1080_60_<wbr>10bit_422.yuv,--preset=<wbr>veryslow --limit-ref 1 --limit-mode --tskip --limit-tu 1<br>
 CrowdRun_1920x1080_50_10bit_<wbr>444.yuv,--preset=superfast --bitrate 7000 --sao --limit-sao<br>
+RaceHorses_416x240_30_10bit.<wbr>yuv, --preset=fast --bitrate 500 --gop-lookahead 10<br></blockquote><div><br></div><div>Lets not add a separate line in smoke test for this feature. Just piggy-back on another command line to limit run-time of smoke tests. A separate test in regression is ok.</div><div> </div><blockquote class="gmail_quote" style="margin:0 0 0 .8ex;border-left:1px #ccc solid;padding-left:1ex">
 # Main12 intraCost overflow bug test<br>
 720p50_parkrun_ter.y4m,--<wbr>preset medium<br>
diff -r 0f0d608e732b -r d37d703039da source/x265.h<br>
--- a/source/x265.h     Fri Nov 17 19:23:14 2017 +0530<br>
+++ b/source/x265.h     Mon Nov 20 10:53:39 2017 +0530<br>
@@ -1535,6 +1535,11 @@<br>
<br>
     /* Allow the encoder to have a copy of the planes of x265_picture in Frame */<br>
     int       bCopyPicToFrame;<br>
+<br>
+    /*Number of frames for GOP boundary decision lookahead.If a scenecut frame is found<br>
+    * within this from the gop boundary set by keyint, the GOP will be extented until such a point,<br>
+    * otherwise the GOP will be terminated as set by keyint*/<br>
+    int       gopLookahead;<br>
 } x265_param;<br>
<br>
 /* x265_param_alloc:<br>
diff -r 0f0d608e732b -r d37d703039da source/x265cli.h<br>
--- a/source/x265cli.h  Fri Nov 17 19:23:14 2017 +0530<br>
+++ b/source/x265cli.h  Mon Nov 20 10:53:39 2017 +0530<br>
@@ -119,6 +119,7 @@<br>
     { "open-gop",             no_argument, NULL, 0 },<br>
     { "keyint",         required_argument, NULL, 'I' },<br>
     { "min-keyint",     required_argument, NULL, 'i' },<br>
+    { "gop-lookahead",  required_argument, NULL, 0 },<br>
     { "scenecut",       required_argument, NULL, 0 },<br>
     { "no-scenecut",          no_argument, NULL, 0 },<br>
     { "scenecut-bias",  required_argument, NULL, 0 },<br>
@@ -418,6 +419,7 @@<br>
     H0("   --[no-]open-gop               Enable open-GOP, allows I slices to be non-IDR. Default %s\n", OPT(param->bOpenGOP));<br>
     H0("-I/--keyint <integer>            Max IDR period in frames. -1 for infinite-gop. Default %d\n", param->keyframeMax);<br>
     H0("-i/--min-keyint <integer>        Scenecuts closer together than this are coded as I, not IDR. Default: auto\n");<br>
+    H0("   --gop-lookahead <integer>     Extends gop boundary if a scenecut is found within this from keyint boundary. Default 0\n");<br>
     H0("   --no-scenecut                 Disable adaptive I-frame decision\n");<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>
<br>______________________________<wbr>_________________<br>
x265-devel mailing list<br>
<a href="mailto:x265-devel@videolan.org">x265-devel@videolan.org</a><br>
<a href="https://mailman.videolan.org/listinfo/x265-devel" rel="noreferrer" target="_blank">https://mailman.videolan.org/<wbr>listinfo/x265-devel</a><br>
<br></blockquote></div><br></div></div>