<div dir="ltr">I have resent the patch after fixing a warning. <div>Thanks,</div><div>Aruna</div></div><div class="gmail_extra"><br><div class="gmail_quote">On Fri, Dec 1, 2017 at 10:46 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 1511241504 -19800<br>
# Tue Nov 21 10:48:24 2017 +0530<br>
# Node ID 805ac1fd3e058a590ffc65126c884c<wbr>eea0473b38<br>
# Parent b1dfa312234ed72c3541831a15f307<wbr>feaf79484d<br>
Extend gop boundary by doing gop lookahead<br>
<br>
diff -r b1dfa312234e -r 805ac1fd3e05 doc/reST/cli.rst<br>
--- a/doc/reST/cli.rst Thu Nov 30 10:06:49 2017 +0530<br>
+++ b/doc/reST/cli.rst Tue Nov 21 10:48:24 2017 +0530<br>
@@ -1374,6 +1374,17 @@<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` - mini-GOP length)<br>
+<br>
+ It is recommended to have `--gop-lookahaed` less than `--min-keyint` as scenecuts beyond<br>
+ `--min-keyint` are already being coded as keyframes.<br>
+<br>
.. option:: --lookahead-slices <0..16><br>
<br>
Use multiple worker threads to measure the estimated cost of each frame<br>
diff -r b1dfa312234e -r 805ac1fd3e05 source/CMakeLists.txt<br>
--- a/source/CMakeLists.txt Thu Nov 30 10:06:49 2017 +0530<br>
+++ b/source/CMakeLists.txt Tue Nov 21 10:48:24 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 b1dfa312234e -r 805ac1fd3e05 source/common/param.cpp<br>
--- a/source/common/param.cpp Thu Nov 30 10:06:49 2017 +0530<br>
+++ b/source/common/param.cpp Tue Nov 21 10:48:24 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 b1dfa312234e -r 805ac1fd3e05 source/encoder/slicetype.cpp<br>
--- a/source/encoder/slicetype.cpp Thu Nov 30 10:06:49 2017 +0530<br>
+++ b/source/encoder/slicetype.cpp Tue Nov 21 10:48:24 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,11 @@<br>
m_numCoopSlices = 1;<br>
}<br>
<br>
+ if (param->gopLookahead && (param->gopLookahead > (param->lookaheadDepth - param->bframes - 2)))<br>
+ {<br>
+ param->gopLookahead = X265_MAX(0, param->lookaheadDepth - param->bframes - 2);<br>
+ x265_log(param, X265_LOG_WARNING, "Gop-lookahead cannot be greater than (rc-lookahead - length of the mini-gop); Clipping gop-lookahead to %d\n", param->gopLookahead);<br>
+ }<br>
#if DETAILED_CU_STATS<br>
m_slicetypeDecideElapsedTime = 0;<br>
m_preLookaheadElapsedTime = 0;<br>
@@ -1086,7 +1092,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>
@@ -1379,8 +1386,13 @@<br>
}<br>
<br>
frames[framecnt + 1] = NULL;<br>
+ int keyFrameLimit = m_param->keyframeMax + m_lastKeyframe - frames[0]->frameNum - 1;<br>
+ if (m_param->gopLookahead && keyFrameLimit <= m_param->bframes + 1)<br>
+ keyintLimit = keyFrameLimit + m_param->gopLookahead;<br>
+ else<br>
+ keyintLimit = keyFrameLimit;<br>
<br>
- keyintLimit = m_param->keyframeMax - frames[0]->frameNum + m_lastKeyframe - 1;<br>
+<br>
origNumFrames = numFrames = m_param->bIntraRefresh ? framecnt : X265_MIN(framecnt, keyintLimit);<br>
<br>
if (bIsVbvLookahead)<br>
@@ -1473,6 +1485,26 @@<br>
return;<br>
}<br>
<br>
+ if (m_param->gopLookahead && (keyFrameLimit >= 0) && (keyFrameLimit <= m_param->bframes + 1))<br>
+ {<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 && scenecutInternal(frames, j - 1, j, true) )<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 +1611,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 + 1) && !m_extendGopBoundary)<br>
+ keyintLimit = keyFrameLimit;<br>
+<br>
if (!m_param->bIntraRefresh)<br>
for (int j = keyintLimit + 1; j <= numFrames; j += m_param->keyframeMax)<br>
{<br>
@@ -1589,7 +1624,8 @@<br>
if (bIsVbvLookahead)<br>
vbvLookahead(frames, numFrames, bKeyframe);<br>
<br>
- int maxp1 = X265_MIN(m_param->bframes + 1, origNumFrames);<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 +1649,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>
@@ -1703,7 +1739,7 @@<br>
<br>
int64_t icost = frame->costEst[0][0];<br>
int64_t pcost = frame->costEst[p1 - p0][0];<br>
- int gopSize = frame->frameNum - m_lastKeyframe;<br>
+ int gopSize = (frame->frameNum - m_lastKeyframe) % m_param->keyframeMax;<br>
float threshMax = (float)(m_param-><wbr>scenecutThreshold / 100.0);<br>
<br>
/* magic numbers pulled out of thin air */<br>
diff -r b1dfa312234e -r 805ac1fd3e05 source/encoder/slicetype.h<br>
--- a/source/encoder/slicetype.h Thu Nov 30 10:06:49 2017 +0530<br>
+++ b/source/encoder/slicetype.h Tue Nov 21 10:48:24 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 b1dfa312234e -r 805ac1fd3e05 source/test/regression-tests.<wbr>txt<br>
--- a/source/test/regression-<wbr>tests.txt Thu Nov 30 10:06:49 2017 +0530<br>
+++ b/source/test/regression-<wbr>tests.txt Tue Nov 21 10:48:24 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 b1dfa312234e -r 805ac1fd3e05 source/x265.h<br>
--- a/source/x265.h Thu Nov 30 10:06:49 2017 +0530<br>
+++ b/source/x265.h Tue Nov 21 10:48:24 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 b1dfa312234e -r 805ac1fd3e05 source/x265cli.h<br>
--- a/source/x265cli.h Thu Nov 30 10:06:49 2017 +0530<br>
+++ b/source/x265cli.h Tue Nov 21 10:48:24 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>
</blockquote></div><br></div>