<html><head><meta http-equiv="Content-Type" content="text/html charset=us-ascii"></head><body style="word-wrap: break-word; -webkit-nbsp-mode: space; -webkit-line-break: after-white-space;" class=""><div style="margin: 0px; font-size: 11px; font-family: Menlo;" class=""># HG changeset patch</div><div style="margin: 0px; font-size: 11px; font-family: Menlo;" class=""># User amarcu5 <<a href="mailto:adampetermarcus@gmail.com" class="">adampetermarcus@gmail.com</a>></div><div style="margin: 0px; font-size: 11px; font-family: Menlo;" class=""># Date 1419207231 0</div><div style="margin: 0px; font-size: 11px; font-family: Menlo;" class=""># Node ID 188f80215b37a34289f4124614bf66211fe63314</div><div style="margin: 0px; font-size: 11px; font-family: Menlo;" class=""># Parent 8d2f418829c894c25da79daa861f16c61e5060d7</div><div style="margin: 0px; font-size: 11px; font-family: Menlo;" class="">Support for tweaking rate control using zones</div><div style="margin: 0px; font-size: 11px; font-family: Menlo; min-height: 13px;" class=""><br class=""></div><div style="margin: 0px; font-size: 11px; font-family: Menlo;" class="">diff --git a/source/common/param.cpp b/source/common/param.cpp</div><div style="margin: 0px; font-size: 11px; font-family: Menlo;" class="">--- a/source/common/param.cpp</div><div style="margin: 0px; font-size: 11px; font-family: Menlo;" class="">+++ b/source/common/param.cpp</div><div style="margin: 0px; font-size: 11px; font-family: Menlo;" class="">@@ -205,6 +205,8 @@</div><div style="margin: 0px; font-size: 11px; font-family: Menlo;" class=""> param->rc.statFileName = NULL;</div><div style="margin: 0px; font-size: 11px; font-family: Menlo;" class=""> param->rc.complexityBlur = 20;</div><div style="margin: 0px; font-size: 11px; font-family: Menlo;" class=""> param->rc.qblur = 0.5;</div><div style="margin: 0px; font-size: 11px; font-family: Menlo;" class="">+ param->rc.zoneCount = 0;</div><div style="margin: 0px; font-size: 11px; font-family: Menlo;" class="">+ param->rc.zones = NULL;</div><div style="margin: 0px; font-size: 11px; font-family: Menlo;" class=""> param->rc.bEnableSlowFirstPass = 0;</div><p style="margin: 0px; font-size: 11px; font-family: Menlo; min-height: 13px;" class=""> <br class="webkit-block-placeholder"></p><div style="margin: 0px; font-size: 11px; font-family: Menlo;" class=""> /* Video Usability Information (VUI) */</div><div style="margin: 0px; font-size: 11px; font-family: Menlo;" class="">@@ -694,6 +696,35 @@</div><div style="margin: 0px; font-size: 11px; font-family: Menlo;" class=""> p->rc.qp = atoi(value);</div><div style="margin: 0px; font-size: 11px; font-family: Menlo;" class=""> p->rc.rateControlMode = X265_RC_CQP;</div><div style="margin: 0px; font-size: 11px; font-family: Menlo;" class=""> }</div><div style="margin: 0px; font-size: 11px; font-family: Menlo;" class="">+ OPT("zones")</div><div style="margin: 0px; font-size: 11px; font-family: Menlo;" class="">+ {</div><div style="margin: 0px; font-size: 11px; font-family: Menlo;" class="">+ p->rc.zoneCount = 1;</div><div style="margin: 0px; font-size: 11px; font-family: Menlo;" class="">+ const char* c;</div><div style="margin: 0px; font-size: 11px; font-family: Menlo;" class="">+ for (c = value; *c; c++) {</div><div style="margin: 0px; font-size: 11px; font-family: Menlo;" class="">+ p->rc.zoneCount += (*c == '/');</div><div style="margin: 0px; font-size: 11px; font-family: Menlo;" class="">+ }</div><div style="margin: 0px; font-size: 11px; font-family: Menlo;" class="">+ p->rc.zones = X265_MALLOC(x265_zone, p->rc.zoneCount);</div><div style="margin: 0px; font-size: 11px; font-family: Menlo;" class="">+ c = value;</div><div style="margin: 0px; font-size: 11px; font-family: Menlo;" class="">+ for (int i = 0; i < p->rc.zoneCount; i++ )</div><div style="margin: 0px; font-size: 11px; font-family: Menlo;" class="">+ {</div><div style="margin: 0px; font-size: 11px; font-family: Menlo;" class="">+ int len;</div><div style="margin: 0px; font-size: 11px; font-family: Menlo;" class="">+ if (3 == sscanf(c, "%d,%d,q=%d%n", &p->rc.zones[i].startFrame, &p->rc.zones[i].endFrame, &p->rc.zones[i].qp, &len))</div><div style="margin: 0px; font-size: 11px; font-family: Menlo;" class="">+ {</div><div style="margin: 0px; font-size: 11px; font-family: Menlo;" class="">+ p->rc.zones[i].bForceQp = 1;</div><div style="margin: 0px; font-size: 11px; font-family: Menlo;" class="">+ }</div><div style="margin: 0px; font-size: 11px; font-family: Menlo;" class="">+ else if (3 == sscanf(c, "%d,%d,b=%f%n", &p->rc.zones[i].startFrame, &p->rc.zones[i].endFrame, &p->rc.zones[i].bitrateFactor, &len))</div><div style="margin: 0px; font-size: 11px; font-family: Menlo;" class="">+ {</div><div style="margin: 0px; font-size: 11px; font-family: Menlo;" class="">+ p->rc.zones[i].bForceQp = 0;</div><div style="margin: 0px; font-size: 11px; font-family: Menlo;" class="">+ }</div><div style="margin: 0px; font-size: 11px; font-family: Menlo;" class="">+ else</div><div style="margin: 0px; font-size: 11px; font-family: Menlo;" class="">+ {</div><div style="margin: 0px; font-size: 11px; font-family: Menlo;" class="">+ bError = true;</div><div style="margin: 0px; font-size: 11px; font-family: Menlo;" class="">+ break;</div><div style="margin: 0px; font-size: 11px; font-family: Menlo;" class="">+ }</div><div style="margin: 0px; font-size: 11px; font-family: Menlo;" class="">+ c += len + 1;</div><div style="margin: 0px; font-size: 11px; font-family: Menlo;" class="">+ }</div><div style="margin: 0px; font-size: 11px; font-family: Menlo;" class="">+ </div><div style="margin: 0px; font-size: 11px; font-family: Menlo;" class="">+ }</div><div style="margin: 0px; font-size: 11px; font-family: Menlo;" class=""> OPT("input-res") bError |= sscanf(value, "%dx%d", &p->sourceWidth, &p->sourceHeight) != 2;</div><div style="margin: 0px; font-size: 11px; font-family: Menlo;" class=""> OPT("input-csp") p->internalCsp = parseName(value, x265_source_csp_names, bError);</div><div style="margin: 0px; font-size: 11px; font-family: Menlo;" class=""> OPT("me") p->searchMethod = parseName(value, x265_motion_est_names, bError);</div><div style="margin: 0px; font-size: 11px; font-family: Menlo;" class="">diff --git a/source/encoder/ratecontrol.cpp b/source/encoder/ratecontrol.cpp</div><div style="margin: 0px; font-size: 11px; font-family: Menlo;" class="">--- a/source/encoder/ratecontrol.cpp</div><div style="margin: 0px; font-size: 11px; font-family: Menlo;" class="">+++ b/source/encoder/ratecontrol.cpp</div><div style="margin: 0px; font-size: 11px; font-family: Menlo;" class="">@@ -169,6 +169,18 @@</div><div style="margin: 0px; font-size: 11px; font-family: Menlo;" class=""> return acEnergyVar(curFrame, primitives.var[BLOCK_16x16](src, srcStride), 8, bChroma);</div><div style="margin: 0px; font-size: 11px; font-family: Menlo;" class=""> }</div><p style="margin: 0px; font-size: 11px; font-family: Menlo; min-height: 13px;" class=""> <br class="webkit-block-placeholder"></p><div style="margin: 0px; font-size: 11px; font-family: Menlo;" class="">+/* Returns the zone for the current frame */</div><div style="margin: 0px; font-size: 11px; font-family: Menlo;" class="">+x265_zone* RateControl::getZone()</div><div style="margin: 0px; font-size: 11px; font-family: Menlo;" class="">+{</div><div style="margin: 0px; font-size: 11px; font-family: Menlo;" class="">+ for (int i = m_param->rc.zoneCount - 1; i >= 0; i--)</div><div style="margin: 0px; font-size: 11px; font-family: Menlo;" class="">+ {</div><div style="margin: 0px; font-size: 11px; font-family: Menlo;" class="">+ x265_zone *z = &m_param->rc.zones[i];</div><div style="margin: 0px; font-size: 11px; font-family: Menlo;" class="">+ if (m_framesDone + 1 >= z->startFrame && m_framesDone < z->endFrame)</div><div style="margin: 0px; font-size: 11px; font-family: Menlo;" class="">+ return z;</div><div style="margin: 0px; font-size: 11px; font-family: Menlo;" class="">+ }</div><div style="margin: 0px; font-size: 11px; font-family: Menlo;" class="">+ return NULL;</div><div style="margin: 0px; font-size: 11px; font-family: Menlo;" class="">+}</div><div style="margin: 0px; font-size: 11px; font-family: Menlo;" class="">+</div><div style="margin: 0px; font-size: 11px; font-family: Menlo;" class=""> /* Find the total AC energy of each block in all planes */</div><div style="margin: 0px; font-size: 11px; font-family: Menlo;" class=""> uint32_t RateControl::acEnergyCu(Frame* curFrame, uint32_t block_x, uint32_t block_y)</div><div style="margin: 0px; font-size: 11px; font-family: Menlo;" class=""> {</div><div style="margin: 0px; font-size: 11px; font-family: Menlo;" class="">@@ -1153,6 +1165,15 @@</div><div style="margin: 0px; font-size: 11px; font-family: Menlo;" class=""> else</div><div style="margin: 0px; font-size: 11px; font-family: Menlo;" class=""> m_qp = m_qpConstant[m_sliceType];</div><div style="margin: 0px; font-size: 11px; font-family: Menlo;" class=""> curEncData.m_avgQpAq = curEncData.m_avgQpRc = m_qp;</div><div style="margin: 0px; font-size: 11px; font-family: Menlo;" class="">+ </div><div style="margin: 0px; font-size: 11px; font-family: Menlo;" class="">+ x265_zone* zone = getZone();</div><div style="margin: 0px; font-size: 11px; font-family: Menlo;" class="">+ if (zone)</div><div style="margin: 0px; font-size: 11px; font-family: Menlo;" class="">+ {</div><div style="margin: 0px; font-size: 11px; font-family: Menlo;" class="">+ if (zone->bForceQp)</div><div style="margin: 0px; font-size: 11px; font-family: Menlo;" class="">+ m_qp += zone->qp - m_qpConstant[P_SLICE];</div><div style="margin: 0px; font-size: 11px; font-family: Menlo;" class="">+ else</div><div style="margin: 0px; font-size: 11px; font-family: Menlo;" class="">+ m_qp -= 6.0 * X265_LOG2(zone->bitrateFactor);</div><div style="margin: 0px; font-size: 11px; font-family: Menlo;" class="">+ }</div><div style="margin: 0px; font-size: 11px; font-family: Menlo;" class=""> }</div><div style="margin: 0px; font-size: 11px; font-family: Menlo;" class=""> if (m_sliceType != B_SLICE)</div><div style="margin: 0px; font-size: 11px; font-family: Menlo;" class=""> {</div><div style="margin: 0px; font-size: 11px; font-family: Menlo;" class="">@@ -1242,6 +1263,14 @@</div><div style="margin: 0px; font-size: 11px; font-family: Menlo;" class=""> m_accumPNorm = mask * (1 + m_accumPNorm);</div><div style="margin: 0px; font-size: 11px; font-family: Menlo;" class=""> }</div><p style="margin: 0px; font-size: 11px; font-family: Menlo; min-height: 13px;" class=""> <br class="webkit-block-placeholder"></p><div style="margin: 0px; font-size: 11px; font-family: Menlo;" class="">+ x265_zone* zone = getZone();</div><div style="margin: 0px; font-size: 11px; font-family: Menlo;" class="">+ if (zone)</div><div style="margin: 0px; font-size: 11px; font-family: Menlo;" class="">+ {</div><div style="margin: 0px; font-size: 11px; font-family: Menlo;" class="">+ if (zone->bForceQp)</div><div style="margin: 0px; font-size: 11px; font-family: Menlo;" class="">+ q = x265_qp2qScale(zone->qp);</div><div style="margin: 0px; font-size: 11px; font-family: Menlo;" class="">+ else</div><div style="margin: 0px; font-size: 11px; font-family: Menlo;" class="">+ q /= zone->bitrateFactor;</div><div style="margin: 0px; font-size: 11px; font-family: Menlo;" class="">+ }</div><div style="margin: 0px; font-size: 11px; font-family: Menlo;" class=""> return q;</div><div style="margin: 0px; font-size: 11px; font-family: Menlo;" class=""> }</div><p style="margin: 0px; font-size: 11px; font-family: Menlo; min-height: 13px;" class=""> <br class="webkit-block-placeholder"></p><div style="margin: 0px; font-size: 11px; font-family: Menlo;" class="">@@ -2071,6 +2100,15 @@</div><div style="margin: 0px; font-size: 11px; font-family: Menlo;" class=""> m_lastRceq = q;</div><div style="margin: 0px; font-size: 11px; font-family: Menlo;" class=""> q /= rateFactor;</div><div style="margin: 0px; font-size: 11px; font-family: Menlo;" class=""> }</div><div style="margin: 0px; font-size: 11px; font-family: Menlo;" class="">+ </div><div style="margin: 0px; font-size: 11px; font-family: Menlo;" class="">+ x265_zone* zone = getZone();</div><div style="margin: 0px; font-size: 11px; font-family: Menlo;" class="">+ if (zone)</div><div style="margin: 0px; font-size: 11px; font-family: Menlo;" class="">+ {</div><div style="margin: 0px; font-size: 11px; font-family: Menlo;" class="">+ if (zone->bForceQp)</div><div style="margin: 0px; font-size: 11px; font-family: Menlo;" class="">+ q = x265_qp2qScale(zone->qp);</div><div style="margin: 0px; font-size: 11px; font-family: Menlo;" class="">+ else</div><div style="margin: 0px; font-size: 11px; font-family: Menlo;" class="">+ q /= zone->bitrateFactor;</div><div style="margin: 0px; font-size: 11px; font-family: Menlo;" class="">+ }</div><div style="margin: 0px; font-size: 11px; font-family: Menlo;" class=""> return q;</div><div style="margin: 0px; font-size: 11px; font-family: Menlo;" class=""> }</div><p style="margin: 0px; font-size: 11px; font-family: Menlo; min-height: 13px;" class=""> <br class="webkit-block-placeholder"></p><div style="margin: 0px; font-size: 11px; font-family: Menlo;" class="">@@ -2361,5 +2399,7 @@</div><div style="margin: 0px; font-size: 11px; font-family: Menlo;" class=""> X265_FREE(m_rce2Pass);</div><div style="margin: 0px; font-size: 11px; font-family: Menlo;" class=""> for (int i = 0; i < 2; i++)</div><div style="margin: 0px; font-size: 11px; font-family: Menlo;" class=""> X265_FREE(m_cuTreeStats.qpBuffer[i]);</div><div style="margin: 0px; font-size: 11px; font-family: Menlo;" class="">+ </div><div style="margin: 0px; font-size: 11px; font-family: Menlo;" class="">+ X265_FREE(m_param->rc.zones);</div><div style="margin: 0px; font-size: 11px; font-family: Menlo;" class=""> }</div><p style="margin: 0px; font-size: 11px; font-family: Menlo; min-height: 13px;" class=""> <br class="webkit-block-placeholder"></p><div style="margin: 0px; font-size: 11px; font-family: Menlo;" class="">diff --git a/source/encoder/ratecontrol.h b/source/encoder/ratecontrol.h</div><div style="margin: 0px; font-size: 11px; font-family: Menlo;" class="">--- a/source/encoder/ratecontrol.h</div><div style="margin: 0px; font-size: 11px; font-family: Menlo;" class="">+++ b/source/encoder/ratecontrol.h</div><div style="margin: 0px; font-size: 11px; font-family: Menlo;" class="">@@ -247,6 +247,7 @@</div><div style="margin: 0px; font-size: 11px; font-family: Menlo;" class=""> int m_amortizeFrames;</div><div style="margin: 0px; font-size: 11px; font-family: Menlo;" class=""> double m_amortizeFraction;</div><p style="margin: 0px; font-size: 11px; font-family: Menlo; min-height: 13px;" class=""> <br class="webkit-block-placeholder"></p><div style="margin: 0px; font-size: 11px; font-family: Menlo;" class="">+ x265_zone* getZone();</div><div style="margin: 0px; font-size: 11px; font-family: Menlo;" class=""> double getQScale(RateControlEntry *rce, double rateFactor);</div><div style="margin: 0px; font-size: 11px; font-family: Menlo;" class=""> double rateEstimateQscale(Frame* pic, RateControlEntry *rce); // main logic for calculating QP based on ABR</div><div style="margin: 0px; font-size: 11px; font-family: Menlo;" class=""> void accumPQpUpdate();</div><div style="margin: 0px; font-size: 11px; font-family: Menlo;" class="">diff --git a/source/x265.cpp b/source/x265.cpp</div><div style="margin: 0px; font-size: 11px; font-family: Menlo;" class="">--- a/source/x265.cpp</div><div style="margin: 0px; font-size: 11px; font-family: Menlo;" class="">+++ b/source/x265.cpp</div><div style="margin: 0px; font-size: 11px; font-family: Menlo;" class="">@@ -150,6 +150,7 @@</div><div style="margin: 0px; font-size: 11px; font-family: Menlo;" class=""> { "vbv-init", required_argument, NULL, 0 },</div><div style="margin: 0px; font-size: 11px; font-family: Menlo;" class=""> { "bitrate", required_argument, NULL, 0 },</div><div style="margin: 0px; font-size: 11px; font-family: Menlo;" class=""> { "qp", required_argument, NULL, 'q' },</div><div style="margin: 0px; font-size: 11px; font-family: Menlo;" class="">+ { "zones", required_argument, NULL, 0 },</div><div style="margin: 0px; font-size: 11px; font-family: Menlo;" class=""> { "aq-mode", required_argument, NULL, 0 },</div><div style="margin: 0px; font-size: 11px; font-family: Menlo;" class=""> { "aq-strength", required_argument, NULL, 0 },</div><div style="margin: 0px; font-size: 11px; font-family: Menlo;" class=""> { "ipratio", required_argument, NULL, 0 },</div><div style="margin: 0px; font-size: 11px; font-family: Menlo;" class="">@@ -435,6 +436,12 @@</div><div style="margin: 0px; font-size: 11px; font-family: Menlo;" class=""> H0(" --b-adapt <0..2> 0 - none, 1 - fast, 2 - full (trellis) adaptive B frame scheduling. Default %d\n", param->bFrameAdaptive);</div><div style="margin: 0px; font-size: 11px; font-family: Menlo;" class=""> H0(" --[no-]b-pyramid Use B-frames as references. Default %s\n", OPT(param->bBPyramid));</div><div style="margin: 0px; font-size: 11px; font-family: Menlo;" class=""> H0(" --ref <integer> max number of L0 references to be allowed (1 .. 16) Default %d\n", param->maxNumReferences);</div><div style="margin: 0px; font-size: 11px; font-family: Menlo;" class="">+ H1(" --zones <zone0>/<zone1>/... Tweak the bitrate of regions of the video\n");</div><div style="margin: 0px; font-size: 11px; font-family: Menlo;" class="">+ H1(" Each zone is of the form\n");</div><div style="margin: 0px; font-size: 11px; font-family: Menlo;" class="">+ H1(" <start frame>,<end frame>,<option>\n");</div><div style="margin: 0px; font-size: 11px; font-family: Menlo;" class="">+ H1(" where <option> is either\n");</div><div style="margin: 0px; font-size: 11px; font-family: Menlo;" class="">+ H1(" q=<integer> (force QP)\n");</div><div style="margin: 0px; font-size: 11px; font-family: Menlo;" class="">+ H1(" or b=<float> (bitrate multiplier)\n");</div><div style="margin: 0px; font-size: 11px; font-family: Menlo;" class=""> H1(" --qpfile <string> Force frametypes and QPs for some or all frames\n");</div><div style="margin: 0px; font-size: 11px; font-family: Menlo;" class=""> H1(" Format of each line: framenumber frametype QP\n");</div><div style="margin: 0px; font-size: 11px; font-family: Menlo;" class=""> H1(" QP is optional (none lets x265 choose). Frametypes: I,i,P,B,b.\n");</div><div style="margin: 0px; font-size: 11px; font-family: Menlo;" class="">diff --git a/source/x265.h b/source/x265.h</div><div style="margin: 0px; font-size: 11px; font-family: Menlo;" class="">--- a/source/x265.h</div><div style="margin: 0px; font-size: 11px; font-family: Menlo;" class="">+++ b/source/x265.h</div><div style="margin: 0px; font-size: 11px; font-family: Menlo;" class="">@@ -328,6 +328,16 @@</div><div style="margin: 0px; font-size: 11px; font-family: Menlo;" class=""> static const char * const x265_interlace_names[] = { "prog", "tff", "bff", 0 };</div><div style="margin: 0px; font-size: 11px; font-family: Menlo;" class=""> static const char * const x265_analysis_names[] = { "off", "save", "load", 0 };</div><p style="margin: 0px; font-size: 11px; font-family: Menlo; min-height: 13px;" class=""> <br class="webkit-block-placeholder"></p><div style="margin: 0px; font-size: 11px; font-family: Menlo;" class="">+/* Zones: override ratecontrol for specific sections of the video.</div><div style="margin: 0px; font-size: 11px; font-family: Menlo;" class="">+ * If zones overlap, whichever comes later in the list takes precedence. */</div><div style="margin: 0px; font-size: 11px; font-family: Menlo;" class="">+typedef struct x265_zone</div><div style="margin: 0px; font-size: 11px; font-family: Menlo;" class="">+{</div><div style="margin: 0px; font-size: 11px; font-family: Menlo;" class="">+ int startFrame, endFrame; /* range of frame numbers */</div><div style="margin: 0px; font-size: 11px; font-family: Menlo;" class="">+ int bForceQp; /* whether to use qp vs bitrate factor */</div><div style="margin: 0px; font-size: 11px; font-family: Menlo;" class="">+ int qp;</div><div style="margin: 0px; font-size: 11px; font-family: Menlo;" class="">+ float bitrateFactor;</div><div style="margin: 0px; font-size: 11px; font-family: Menlo;" class="">+} x265_zone;</div><div style="margin: 0px; font-size: 11px; font-family: Menlo;" class="">+ </div><div style="margin: 0px; font-size: 11px; font-family: Menlo;" class=""> /* x265 input parameters</div><div style="margin: 0px; font-size: 11px; font-family: Menlo;" class=""> *</div><div style="margin: 0px; font-size: 11px; font-family: Menlo;" class=""> * For version safety you may use x265_param_alloc/free() to manage the</div><div style="margin: 0px; font-size: 11px; font-family: Menlo;" class="">@@ -867,6 +877,10 @@</div><p style="margin: 0px; font-size: 11px; font-family: Menlo; min-height: 13px;" class=""> <br class="webkit-block-placeholder"></p><div style="margin: 0px; font-size: 11px; font-family: Menlo;" class=""> /* Enable slow and a more detailed first pass encode in multi pass rate control */</div><div style="margin: 0px; font-size: 11px; font-family: Menlo;" class=""> int bEnableSlowFirstPass;</div><div style="margin: 0px; font-size: 11px; font-family: Menlo;" class="">+ </div><div style="margin: 0px; font-size: 11px; font-family: Menlo;" class="">+ /* ratecontrol overrides */</div><div style="margin: 0px; font-size: 11px; font-family: Menlo;" class="">+ int zoneCount;</div><div style="margin: 0px; font-size: 11px; font-family: Menlo;" class="">+ x265_zone* zones;</div><p style="margin: 0px; font-size: 11px; font-family: Menlo; min-height: 13px;" class=""> <br class="webkit-block-placeholder"></p><div style="margin: 0px; font-size: 11px; font-family: Menlo;" class=""> /* specify a text file which contains MAX_MAX_QP + 1 floating point</div><div style="margin: 0px; font-size: 11px; font-family: Menlo;" class=""> * values to be copied into x265_lambda_tab and a second set of</div><div class=""><br class=""></div></body></html>