<div dir="auto">Please ignore this patch. I have an updated patch to share.</div><div class="gmail_extra"><br><div class="gmail_quote">On 21 Nov 2017 11:19 a.m., <<a href="mailto:aruna@multicorewareinc.com">aruna@multicorewareinc.com</a>> wrote:<br type="attribution"><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 bc70f09675f1277e7867a5e09d7afb<wbr>8b3e44742a<br>
# Parent 16ea92bf3627c6de43d583554df294<wbr>dbbfd8fa8a<br>
Extend gop boundary by doing gop lookahead<br>
<br>
diff -r 16ea92bf3627 -r bc70f09675f1 doc/reST/cli.rst<br>
--- a/doc/reST/cli.rst Thu Nov 02 09:40:41 2017 +0530<br>
+++ b/doc/reST/cli.rst Tue Nov 21 10:48:24 2017 +0530<br>
@@ -1373,6 +1373,13 @@<br>
Default 20<br>
<br>
**Range of values:** Between the maximum consecutive bframe count (:option:`--bframes`) and 250<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 16ea92bf3627 -r bc70f09675f1 source/CMakeLists.txt<br>
--- a/source/CMakeLists.txt Thu Nov 02 09:40:41 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 16ea92bf3627 -r bc70f09675f1 source/common/param.cpp<br>
--- a/source/common/param.cpp Thu Nov 02 09:40:41 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 16ea92bf3627 -r bc70f09675f1 source/encoder/slicetype.cpp<br>
--- a/source/encoder/slicetype.cpp Thu Nov 02 09:40:41 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->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>
#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 (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>
@@ -1575,10 +1607,12 @@<br>
}<br>
if (m_param->bAQMotion)<br>
aqMotion(frames, bKeyframe);<br>
-<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>
@@ -1588,8 +1622,8 @@<br>
<br>
if (bIsVbvLookahead)<br>
vbvLookahead(frames, numFrames, bKeyframe);<br>
+ int maxp1 = X265_MIN(m_param->bframes + 1, origNumFrames);<br>
<br>
- int maxp1 = X265_MIN(m_param->bframes + 1, origNumFrames);<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 +1647,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 +1751,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 16ea92bf3627 -r bc70f09675f1 source/encoder/slicetype.h<br>
--- a/source/encoder/slicetype.h Thu Nov 02 09:40:41 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 16ea92bf3627 -r bc70f09675f1 source/test/regression-tests.<wbr>txt<br>
--- a/source/test/regression-<wbr>tests.txt Thu Nov 02 09:40:41 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 16ea92bf3627 -r bc70f09675f1 source/x265.h<br>
--- a/source/x265.h Thu Nov 02 09:40:41 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 16ea92bf3627 -r bc70f09675f1 source/x265cli.h<br>
--- a/source/x265cli.h Thu Nov 02 09:40:41 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></div>