<div dir="ltr"><br><div class="gmail_extra"><br><div class="gmail_quote">On Tue, Apr 4, 2017 at 6:34 PM, <span dir="ltr"><<a href="mailto:aruna@multicorewareinc.com" target="_blank">aruna@multicorewareinc.com</a>></span> wrote:<br><blockquote class="gmail_quote" style="margin:0px 0px 0px 0.8ex;border-left:1px solid rgb(204,204,204);padding-left:1ex"># HG changeset patch<br>
# User Aruna Matheswaran <<a href="mailto:aruna@multicorewareinc.com">aruna@multicorewareinc.com</a>><br>
# Date 1487757265 -19800<br>
# Wed Feb 22 15:24:25 2017 +0530<br>
# Node ID 576ad673d9b352c45853b7ffeeacce<wbr>7d23268bb8<br>
# Parent 08a05ca9fd16c9f5efb1ce4d8389bd<wbr>a8a63f5f7d<br>
Add dynamic rate-control reconfiguration<br>
<br>
diff -r 08a05ca9fd16 -r 576ad673d9b3 source/common/frame.cpp<br>
--- a/source/common/frame.cpp Mon Mar 27 12:35:20 2017 +0530<br>
+++ b/source/common/frame.cpp Wed Feb 22 15:24:25 2017 +0530<br>
@@ -47,6 +47,7 @@<br>
memset(&m_lowres, 0, sizeof(m_lowres));<br>
m_rcData = NULL;<br>
m_encodeStartTime = 0;<br>
+ m_reconfigureRc = false;<br>
}<br>
<br>
bool Frame::create(x265_param *param, float* quantOffsets)<br>
diff -r 08a05ca9fd16 -r 576ad673d9b3 source/common/frame.h<br>
--- a/source/common/frame.h Mon Mar 27 12:35:20 2017 +0530<br>
+++ b/source/common/frame.h Wed Feb 22 15:24:25 2017 +0530<br>
@@ -84,6 +84,7 @@<br>
Lowres m_lowres;<br>
bool m_lowresInit; // lowres init complete (pre-analysis)<br>
bool m_bChromaExtended; // orig chroma planes motion extended for weight analysis<br>
+ bool m_reconfigureRc;<br>
<br>
float* m_quantOffsets; // points to quantOffsets in x265_picture<br>
x265_sei m_userSEI;<br>
diff -r 08a05ca9fd16 -r 576ad673d9b3 source/encoder/api.cpp<br>
--- a/source/encoder/api.cpp Mon Mar 27 12:35:20 2017 +0530<br>
+++ b/source/encoder/api.cpp Wed Feb 22 15:24:25 2017 +0530<br>
@@ -171,7 +171,7 @@<br>
<br>
x265_param save;<br>
Encoder* encoder = static_cast<Encoder*>(enc);<br>
- if (encoder->m_reconfigure) /* Reconfigure in progress */<br>
+ if (encoder->m_reconfigure || encoder->m_reconfigureRc) /* Reconfigure in progress */<br>
return 1;<br>
memcpy(&save, encoder->m_latestParam, sizeof(x265_param));<br>
int ret = encoder->reconfigureParam(<wbr>encoder->m_latestParam, param_in);<br>
@@ -197,7 +197,22 @@<br>
return -1;<br>
}<br>
}<br>
- encoder->m_reconfigure = true;<br>
+ if (encoder->m_reconfigureRc)<br>
+ {<br>
+ VPS saveVPS;<br>
+ memcpy(&saveVPS.ptl, &encoder->m_vps.ptl, sizeof(saveVPS.ptl));<br>
+ determineLevel(*encoder->m_<wbr>latestParam, encoder->m_vps);<br>
+ if (saveVPS.ptl.profileIdc != encoder->m_vps.ptl.profileIdc || saveVPS.ptl.levelIdc != encoder->m_vps.ptl.levelIdc<br>
+ || saveVPS.ptl.tierFlag != encoder->m_vps.ptl.tierFlag)<br>
+ {<br>
+ x265_log(encoder->m_param, X265_LOG_WARNING, "Profile/Level/Tier has changed from %d/%d/%s to %d/%d/%s.Cannot reconfigure rate-control.\n",<br>
+ saveVPS.ptl.profileIdc, saveVPS.ptl.levelIdc, saveVPS.ptl.tierFlag ? "High" : "Main", encoder->m_vps.ptl.profileIdc,<br>
+ encoder->m_vps.ptl.levelIdc, encoder->m_vps.ptl.tierFlag ? "High" : "Main");<br>
+ encoder->m_reconfigureRc = false;<br>
+ }<br>
+ }<br>
+ else<br>
+ encoder->m_reconfigure = true;<br></blockquote><div>why set only m_reconfigure as true, where are you setting m_reconfigureRc to true? </div><blockquote class="gmail_quote" style="margin:0px 0px 0px 0.8ex;border-left:1px solid rgb(204,204,204);padding-left:1ex">
encoder-><wbr>printReconfigureParams();<br>
}<br>
return ret;<br>
diff -r 08a05ca9fd16 -r 576ad673d9b3 source/encoder/encoder.cpp<br>
--- a/source/encoder/encoder.cpp Mon Mar 27 12:35:20 2017 +0530<br>
+++ b/source/encoder/encoder.cpp Wed Feb 22 15:24:25 2017 +0530<br>
@@ -56,6 +56,7 @@<br>
{<br>
m_aborted = false;<br>
m_reconfigure = false;<br>
+ m_reconfigureRc = false;<br>
m_encodedFrameNum = 0;<br>
m_pocLast = -1;<br>
m_curEncoder = 0;<br>
@@ -607,7 +608,7 @@<br>
{<br>
inFrame = new Frame;<br>
inFrame->m_encodeStartTime = x265_mdate();<br>
- x265_param* p = m_reconfigure ? m_latestParam : m_param;<br></blockquote><div>no need for this change - when reconfigureRc is true, m_reconfigure will also be true..so dont need to change these </div><blockquote class="gmail_quote" style="margin:0px 0px 0px 0.8ex;border-left:1px solid rgb(204,204,204);padding-left:1ex">
+ x265_param* p = (m_reconfigure || m_reconfigureRc) ? m_latestParam : m_param;<br>
if (inFrame->create(p, pic_in->quantOffsets))<br>
{<br>
/* the first PicYuv created is asked to generate the CU and block unit offset<br>
@@ -674,7 +675,7 @@<br>
inFrame->m_userData = pic_in->userData;<br>
inFrame->m_pts = pic_in->pts;<br>
inFrame->m_forceqp = pic_in->forceqp;<br>
- inFrame->m_param = m_reconfigure ? m_latestParam : m_param;<br></blockquote><div>same as above. for any reconfigure, set m_reconfigure to true, additionally, set m_reconfigureRc to true for</div><div>Rate control reconfigure changes. no need for the above change then </div><blockquote class="gmail_quote" style="margin:0px 0px 0px 0.8ex;border-left:1px solid rgb(204,204,204);padding-left:1ex">
+ inFrame->m_param = (m_reconfigure || m_reconfigureRc) ? m_latestParam : m_param;<br>
<br>
if (pic_in->userSEI.numPayloads)<br>
{<br>
@@ -740,6 +741,8 @@<br>
inFrame->m_lowres.bScenecut = !!inFrame->m_analysisData.<wbr>bScenecut;<br>
inFrame->m_lowres.satdCost = inFrame->m_analysisData.<wbr>satdCost;<br>
}<br>
+ if (m_reconfigureRc)<br>
+ inFrame->m_reconfigureRc = true;<br>
<br>
m_lookahead->addPicture(*<wbr>inFrame, sliceType);<br>
m_numDelayedPic++;<br>
@@ -940,6 +943,15 @@<br>
if (m_param->rc.bStatRead)<br>
readAnalysis2PassFile(&<wbr>frameEnc->m_analysis2Pass, frameEnc->m_poc, frameEnc->m_lowres.sliceType);<br>
}<br>
+<br>
+ if (frameEnc->m_reconfigureRc && m_reconfigureRc)<br>
+ {<br>
+ memcpy(m_param, m_latestParam, sizeof(x265_param));<br>
+ m_rateControl->reconfigureRC()<wbr>;<br>
+ m_reconfigureRc = false;<br>
+ }<br>
+ if (frameEnc->m_reconfigureRc && !m_reconfigureRc)<br>
+ frameEnc->m_reconfigureRc = false;<br>
if (curEncoder->m_reconfigure)<br>
{<br>
/* One round robin cycle of FE reconfigure is complete */<br>
@@ -1090,6 +1102,25 @@<br>
encParam->bIntraInBFrames = param->bIntraInBFrames;<br>
if (param->scalingLists && !encParam->scalingLists)<br>
encParam->scalingLists = strdup(param->scalingLists);<br></blockquote><div>Can you turn vbv off in reconfigure, if it was ON to beign with? need that check as well </div><blockquote class="gmail_quote" style="margin:0px 0px 0px 0.8ex;border-left:1px solid rgb(204,204,204);padding-left:1ex">
+ /* VBV can't be turned ON if it wasn't ON to begin with */<br>
+ if (param->rc.vbvMaxBitrate > 0 && param->rc.vbvBufferSize > 0 &&<br>
+ encParam->rc.vbvMaxBitrate > 0 && encParam->rc.vbvBufferSize > 0)<br>
+ {<br>
+ m_reconfigureRc |= encParam->rc.vbvMaxBitrate != param->rc.vbvMaxBitrate;<br>
+ m_reconfigureRc |= encParam->rc.vbvBufferSize != param->rc.vbvBufferSize;<br>
+ if (m_reconfigureRc && m_param->bEmitHRDSEI)<br>
+ x265_log(m_param, X265_LOG_WARNING, "VBV parameters cannot be changed when HRD is in use.\n");<br>
+ else<br>
+ {<br>
+ encParam->rc.vbvMaxBitrate = param->rc.vbvMaxBitrate;<br>
+ encParam->rc.vbvBufferSize = param->rc.vbvBufferSize;</blockquote><blockquote class="gmail_quote" style="margin:0px 0px 0px 0.8ex;border-left:1px solid rgb(204,204,204);padding-left:1ex">
+ }<br>
+ }<br>
+ m_reconfigureRc |= encParam->rc.bitrate != param->rc.bitrate;<br>
+ encParam->rc.bitrate = param->rc.bitrate;<br>
+ m_reconfigureRc |= encParam->rc.rfConstant != param->rc.rfConstant;<br>
+ encParam->rc.rfConstant = param->rc.rfConstant;<br>
+<br>
/* To add: Loop Filter/deblocking controls, transform skip, signhide require PPS to be resent */<br>
/* To add: SAO, temporal MVP, AMP, TU depths require SPS to be resent, at every CVS boundary */<br>
return x265_check_params(encParam);<br>
@@ -3090,7 +3121,7 @@<br>
<br>
void Encoder::<wbr>printReconfigureParams()<br>
{<br>
- if (!m_reconfigure)<br>
+ if (!(m_reconfigure || m_reconfigureRc))<br>
return;<br>
x265_param* oldParam = m_param;<br>
x265_param* newParam = m_latestParam;<br>
@@ -3112,6 +3143,10 @@<br>
TOOLCMP(oldParam-><wbr>maxNumMergeCand, newParam->maxNumMergeCand, "max-merge=%d to %d\n");<br>
TOOLCMP(oldParam-><wbr>bIntraInBFrames, newParam->bIntraInBFrames, "b-intra=%d to %d\n");<br>
TOOLCMP(oldParam-><wbr>scalingLists, newParam->scalingLists, "scalinglists=%s to %s\n");<br>
+ TOOLCMP(oldParam->rc.<wbr>vbvMaxBitrate, newParam->rc.vbvMaxBitrate, "vbv-maxrate=%d to %d\n");<br>
+ TOOLCMP(oldParam->rc.<wbr>vbvBufferSize, newParam->rc.vbvBufferSize, "vbv-bufsize=%d to %d\n");<br>
+ TOOLCMP(oldParam->rc.bitrate, newParam->rc.bitrate, "bitrate=%d to %d\n");<br>
+ TOOLCMP(oldParam->rc.<wbr>rfConstant, newParam->rc.rfConstant, "crf=%f to %f\n");<br>
}<br>
<br>
bool Encoder::computeSPSRPSIndex()<br>
diff -r 08a05ca9fd16 -r 576ad673d9b3 source/encoder/encoder.h<br>
--- a/source/encoder/encoder.h Mon Mar 27 12:35:20 2017 +0530<br>
+++ b/source/encoder/encoder.h Wed Feb 22 15:24:25 2017 +0530<br>
@@ -153,6 +153,7 @@<br>
bool m_bZeroLatency; // x265_encoder_encode() returns NALs for the input picture, zero lag<br>
bool m_aborted; // fatal error detected<br>
bool m_reconfigure; // Encoder reconfigure in progress<br>
+ bool m_reconfigureRc;<br>
<br>
/* Begin intra refresh when one not in progress or else begin one as soon as the current<br>
* one is done. Requires bIntraRefresh to be set.*/<br>
diff -r 08a05ca9fd16 -r 576ad673d9b3 source/encoder/ratecontrol.cpp<br>
--- a/source/encoder/ratecontrol.<wbr>cpp Mon Mar 27 12:35:20 2017 +0530<br>
+++ b/source/encoder/ratecontrol.<wbr>cpp Wed Feb 22 15:24:25 2017 +0530<br>
@@ -683,6 +683,56 @@<br>
return true;<br>
}<br>
<br>
+void RateControl::reconfigureRC()<br>
+{<br>
+ if (m_isVbv)<br>
+ {<br>
+ m_param->rc.vbvBufferSize = x265_clip3(0, 2000000, m_param->rc.vbvBufferSize);<br>
+ m_param->rc.vbvMaxBitrate = x265_clip3(0, 2000000, m_param->rc.vbvMaxBitrate);<br>
+ if (m_param->rc.vbvMaxBitrate < m_param->rc.bitrate &&<br>
+ m_param->rc.rateControlMode == X265_RC_ABR)<br>
+ {<br>
+ x265_log(m_param, X265_LOG_WARNING, "max bitrate less than average bitrate, assuming CBR\n");<br>
+ m_param->rc.bitrate = m_param->rc.vbvMaxBitrate;<br>
+ }<br>
+<br>
+ if (m_param->rc.vbvBufferSize < (int)(m_param->rc.<wbr>vbvMaxBitrate / m_fps))<br>
+ {<br>
+ m_param->rc.vbvBufferSize = (int)(m_param->rc.<wbr>vbvMaxBitrate / m_fps);<br>
+ x265_log(m_param, X265_LOG_WARNING, "VBV buffer size cannot be smaller than one frame, using %d kbit\n",<br>
+ m_param->rc.vbvBufferSize);<br>
+ }<br>
+ int vbvBufferSize = m_param->rc.vbvBufferSize * 1000;<br>
+ int vbvMaxBitrate = m_param->rc.vbvMaxBitrate * 1000;<br>
+ m_bufferRate = vbvMaxBitrate / m_fps;<br>
+ m_vbvMaxRate = vbvMaxBitrate;<br>
+ m_bufferSize = vbvBufferSize;<br>
+ m_singleFrameVbv = m_bufferRate * 1.1 > m_bufferSize;<br>
+ }<br>
+ if (m_param->rc.rateControlMode == X265_RC_CRF)<br>
+ {<br>
+ #define CRF_INIT_QP (int)m_param->rc.rfConstant<br>
+ m_param->rc.bitrate = 0;<br>
+ double baseCplx = m_ncu * (m_param->bframes ? 120 : 80);<br>
+ double mbtree_offset = m_param->rc.cuTree ? (1.0 - m_param->rc.qCompress) * 13.5 : 0;<br>
+ m_rateFactorConstant = pow(baseCplx, 1 - m_qCompress) /<br>
+ x265_qp2qScale(m_param->rc.<wbr>rfConstant + mbtree_offset);<br>
+ if (m_param->rc.rfConstantMax)<br>
+ {<br>
+ m_rateFactorMaxIncrement = m_param->rc.rfConstantMax - m_param->rc.rfConstant;<br>
+ if (m_rateFactorMaxIncrement <= 0)<br>
+ {<br>
+ x265_log(m_param, X265_LOG_WARNING, "CRF max must be greater than CRF\n");<br>
+ m_rateFactorMaxIncrement = 0;<br>
+ }<br>
+ }<br>
+ if (m_param->rc.rfConstantMin)<br>
+ m_rateFactorMaxDecrement = m_param->rc.rfConstant - m_param->rc.rfConstantMin;<br>
+ }<br>
+ m_bitrate = m_param->rc.bitrate * 1000;<br>
+}<br>
+<br>
+<br>
void RateControl::initHRD(SPS& sps)<br>
{<br>
int vbvBufferSize = m_param->rc.vbvBufferSize * 1000;<br>
diff -r 08a05ca9fd16 -r 576ad673d9b3 source/encoder/ratecontrol.h<br>
--- a/source/encoder/ratecontrol.h Mon Mar 27 12:35:20 2017 +0530<br>
+++ b/source/encoder/ratecontrol.h Wed Feb 22 15:24:25 2017 +0530<br>
@@ -234,6 +234,7 @@<br>
RateControl(x265_param& p);<br>
bool init(const SPS& sps);<br>
void initHRD(SPS& sps);<br>
+ void reconfigureRC();<br>
<br>
void setFinalFrameCount(int count);<br>
void terminate(); /* un-block all waiting functions so encoder may close */<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>
</blockquote></div><br></div></div>