<div dir="ltr"><div>Hi,<br><br></div>Thanks for your patch. I'm not quite sure any of our regular users need this as of now. We will keep this on standby though - until any requests come in.<br></div><div class="gmail_extra"><br><div class="gmail_quote">On Mon, Dec 22, 2014 at 6:09 AM, Adam Marcus <span dir="ltr"><<a href="mailto:adampetermarcus@gmail.com" target="_blank">adampetermarcus@gmail.com</a>></span> wrote:<br><blockquote class="gmail_quote" style="margin:0 0 0 .8ex;border-left:1px #ccc solid;padding-left:1ex"><div style="word-wrap:break-word"><div style="margin:0px;font-size:11px;font-family:Menlo"># HG changeset patch</div><div style="margin:0px;font-size:11px;font-family:Menlo"># User amarcu5 <<a href="mailto:adampetermarcus@gmail.com" target="_blank">adampetermarcus@gmail.com</a>></div><div style="margin:0px;font-size:11px;font-family:Menlo"># Date 1419207231 0</div><div style="margin:0px;font-size:11px;font-family:Menlo"># Node ID 188f80215b37a34289f4124614bf66211fe63314</div><div style="margin:0px;font-size:11px;font-family:Menlo"># Parent  8d2f418829c894c25da79daa861f16c61e5060d7</div><div style="margin:0px;font-size:11px;font-family:Menlo">Support for tweaking rate control using zones</div><div style="margin:0px;font-size:11px;font-family:Menlo;min-height:13px"><br></div><div style="margin:0px;font-size:11px;font-family:Menlo">diff --git a/source/common/param.cpp b/source/common/param.cpp</div><div style="margin:0px;font-size:11px;font-family:Menlo">--- a/source/common/param.cpp</div><div style="margin:0px;font-size:11px;font-family:Menlo">+++ b/source/common/param.cpp</div><div style="margin:0px;font-size:11px;font-family:Menlo">@@ -205,6 +205,8 @@</div><div style="margin:0px;font-size:11px;font-family:Menlo">     param->rc.statFileName = NULL;</div><div style="margin:0px;font-size:11px;font-family:Menlo">     param->rc.complexityBlur = 20;</div><div style="margin:0px;font-size:11px;font-family:Menlo">     param->rc.qblur = 0.5;</div><div style="margin:0px;font-size:11px;font-family:Menlo">+    param->rc.zoneCount = 0;</div><div style="margin:0px;font-size:11px;font-family:Menlo">+    param->rc.zones = NULL;</div><div style="margin:0px;font-size:11px;font-family:Menlo">     param->rc.bEnableSlowFirstPass = 0;</div><p style="margin:0px;font-size:11px;font-family:Menlo;min-height:13px"> <br></p><div style="margin:0px;font-size:11px;font-family:Menlo">     /* Video Usability Information (VUI) */</div><div style="margin:0px;font-size:11px;font-family:Menlo">@@ -694,6 +696,35 @@</div><div style="margin:0px;font-size:11px;font-family:Menlo">         p->rc.qp = atoi(value);</div><div style="margin:0px;font-size:11px;font-family:Menlo">         p->rc.rateControlMode = X265_RC_CQP;</div><div style="margin:0px;font-size:11px;font-family:Menlo">     }</div><div style="margin:0px;font-size:11px;font-family:Menlo">+    OPT("zones")</div><div style="margin:0px;font-size:11px;font-family:Menlo">+    {</div><div style="margin:0px;font-size:11px;font-family:Menlo">+        p->rc.zoneCount = 1;</div><div style="margin:0px;font-size:11px;font-family:Menlo">+        const char* c;</div><div style="margin:0px;font-size:11px;font-family:Menlo">+        for (c = value; *c; c++) {</div><div style="margin:0px;font-size:11px;font-family:Menlo">+            p->rc.zoneCount += (*c == '/');</div><div style="margin:0px;font-size:11px;font-family:Menlo">+        }</div><div style="margin:0px;font-size:11px;font-family:Menlo">+        p->rc.zones = X265_MALLOC(x265_zone, p->rc.zoneCount);</div><div style="margin:0px;font-size:11px;font-family:Menlo">+        c = value;</div><div style="margin:0px;font-size:11px;font-family:Menlo">+        for (int i = 0; i < p->rc.zoneCount; i++ )</div><div style="margin:0px;font-size:11px;font-family:Menlo">+        {</div><div style="margin:0px;font-size:11px;font-family:Menlo">+            int len;</div><div style="margin:0px;font-size:11px;font-family:Menlo">+            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">+            {</div><div style="margin:0px;font-size:11px;font-family:Menlo">+                p->rc.zones[i].bForceQp = 1;</div><div style="margin:0px;font-size:11px;font-family:Menlo">+            }</div><div style="margin:0px;font-size:11px;font-family:Menlo">+            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">+            {</div><div style="margin:0px;font-size:11px;font-family:Menlo">+                p->rc.zones[i].bForceQp = 0;</div><div style="margin:0px;font-size:11px;font-family:Menlo">+            }</div><div style="margin:0px;font-size:11px;font-family:Menlo">+            else</div><div style="margin:0px;font-size:11px;font-family:Menlo">+            {</div><div style="margin:0px;font-size:11px;font-family:Menlo">+                bError = true;</div><div style="margin:0px;font-size:11px;font-family:Menlo">+                break;</div><div style="margin:0px;font-size:11px;font-family:Menlo">+            }</div><div style="margin:0px;font-size:11px;font-family:Menlo">+            c += len + 1;</div><div style="margin:0px;font-size:11px;font-family:Menlo">+        }</div><div style="margin:0px;font-size:11px;font-family:Menlo">+        </div><div style="margin:0px;font-size:11px;font-family:Menlo">+    }</div><div style="margin:0px;font-size:11px;font-family:Menlo">     OPT("input-res") bError |= sscanf(value, "%dx%d", &p->sourceWidth, &p->sourceHeight) != 2;</div><div style="margin:0px;font-size:11px;font-family:Menlo">     OPT("input-csp") p->internalCsp = parseName(value, x265_source_csp_names, bError);</div><div style="margin:0px;font-size:11px;font-family:Menlo">     OPT("me")        p->searchMethod = parseName(value, x265_motion_est_names, bError);</div><div style="margin:0px;font-size:11px;font-family:Menlo">diff --git a/source/encoder/ratecontrol.cpp b/source/encoder/ratecontrol.cpp</div><div style="margin:0px;font-size:11px;font-family:Menlo">--- a/source/encoder/ratecontrol.cpp</div><div style="margin:0px;font-size:11px;font-family:Menlo">+++ b/source/encoder/ratecontrol.cpp</div><div style="margin:0px;font-size:11px;font-family:Menlo">@@ -169,6 +169,18 @@</div><div style="margin:0px;font-size:11px;font-family:Menlo">         return acEnergyVar(curFrame, primitives.var[BLOCK_16x16](src, srcStride), 8, bChroma);</div><div style="margin:0px;font-size:11px;font-family:Menlo"> }</div><p style="margin:0px;font-size:11px;font-family:Menlo;min-height:13px"> <br></p><div style="margin:0px;font-size:11px;font-family:Menlo">+/* Returns the zone for the current frame */</div><div style="margin:0px;font-size:11px;font-family:Menlo">+x265_zone* RateControl::getZone()</div><div style="margin:0px;font-size:11px;font-family:Menlo">+{</div><div style="margin:0px;font-size:11px;font-family:Menlo">+    for (int i = m_param->rc.zoneCount - 1; i >= 0; i--)</div><div style="margin:0px;font-size:11px;font-family:Menlo">+    {</div><div style="margin:0px;font-size:11px;font-family:Menlo">+        x265_zone *z = &m_param->rc.zones[i];</div><div style="margin:0px;font-size:11px;font-family:Menlo">+        if (m_framesDone + 1 >= z->startFrame && m_framesDone < z->endFrame)</div><div style="margin:0px;font-size:11px;font-family:Menlo">+            return z;</div><div style="margin:0px;font-size:11px;font-family:Menlo">+    }</div><div style="margin:0px;font-size:11px;font-family:Menlo">+    return NULL;</div><div style="margin:0px;font-size:11px;font-family:Menlo">+}</div><div style="margin:0px;font-size:11px;font-family:Menlo">+</div><div style="margin:0px;font-size:11px;font-family:Menlo"> /* Find the total AC energy of each block in all planes */</div><div style="margin:0px;font-size:11px;font-family:Menlo"> 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"> {</div><div style="margin:0px;font-size:11px;font-family:Menlo">@@ -1153,6 +1165,15 @@</div><div style="margin:0px;font-size:11px;font-family:Menlo">         else</div><div style="margin:0px;font-size:11px;font-family:Menlo">             m_qp = m_qpConstant[m_sliceType];</div><div style="margin:0px;font-size:11px;font-family:Menlo">         curEncData.m_avgQpAq = curEncData.m_avgQpRc = m_qp;</div><div style="margin:0px;font-size:11px;font-family:Menlo">+        </div><div style="margin:0px;font-size:11px;font-family:Menlo">+        x265_zone* zone = getZone();</div><div style="margin:0px;font-size:11px;font-family:Menlo">+        if (zone)</div><div style="margin:0px;font-size:11px;font-family:Menlo">+        {</div><div style="margin:0px;font-size:11px;font-family:Menlo">+            if (zone->bForceQp)</div><div style="margin:0px;font-size:11px;font-family:Menlo">+                m_qp += zone->qp - m_qpConstant[P_SLICE];</div><div style="margin:0px;font-size:11px;font-family:Menlo">+            else</div><div style="margin:0px;font-size:11px;font-family:Menlo">+                m_qp -= 6.0 * X265_LOG2(zone->bitrateFactor);</div><div style="margin:0px;font-size:11px;font-family:Menlo">+        }</div><div style="margin:0px;font-size:11px;font-family:Menlo">     }</div><div style="margin:0px;font-size:11px;font-family:Menlo">     if (m_sliceType != B_SLICE)</div><div style="margin:0px;font-size:11px;font-family:Menlo">     {</div><div style="margin:0px;font-size:11px;font-family:Menlo">@@ -1242,6 +1263,14 @@</div><div style="margin:0px;font-size:11px;font-family:Menlo">         m_accumPNorm = mask * (1 + m_accumPNorm);</div><div style="margin:0px;font-size:11px;font-family:Menlo">     }</div><p style="margin:0px;font-size:11px;font-family:Menlo;min-height:13px"> <br></p><div style="margin:0px;font-size:11px;font-family:Menlo">+    x265_zone* zone = getZone();</div><div style="margin:0px;font-size:11px;font-family:Menlo">+    if (zone)</div><div style="margin:0px;font-size:11px;font-family:Menlo">+    {</div><div style="margin:0px;font-size:11px;font-family:Menlo">+        if (zone->bForceQp)</div><div style="margin:0px;font-size:11px;font-family:Menlo">+            q = x265_qp2qScale(zone->qp);</div><div style="margin:0px;font-size:11px;font-family:Menlo">+        else</div><div style="margin:0px;font-size:11px;font-family:Menlo">+            q /= zone->bitrateFactor;</div><div style="margin:0px;font-size:11px;font-family:Menlo">+    }</div><div style="margin:0px;font-size:11px;font-family:Menlo">     return q;</div><div style="margin:0px;font-size:11px;font-family:Menlo"> }</div><p style="margin:0px;font-size:11px;font-family:Menlo;min-height:13px"> <br></p><div style="margin:0px;font-size:11px;font-family:Menlo">@@ -2071,6 +2100,15 @@</div><div style="margin:0px;font-size:11px;font-family:Menlo">         m_lastRceq = q;</div><div style="margin:0px;font-size:11px;font-family:Menlo">         q /= rateFactor;</div><div style="margin:0px;font-size:11px;font-family:Menlo">     }</div><div style="margin:0px;font-size:11px;font-family:Menlo">+    </div><div style="margin:0px;font-size:11px;font-family:Menlo">+    x265_zone* zone = getZone();</div><div style="margin:0px;font-size:11px;font-family:Menlo">+    if (zone)</div><div style="margin:0px;font-size:11px;font-family:Menlo">+    {</div><div style="margin:0px;font-size:11px;font-family:Menlo">+        if (zone->bForceQp)</div><div style="margin:0px;font-size:11px;font-family:Menlo">+            q = x265_qp2qScale(zone->qp);</div><div style="margin:0px;font-size:11px;font-family:Menlo">+        else</div><div style="margin:0px;font-size:11px;font-family:Menlo">+            q /= zone->bitrateFactor;</div><div style="margin:0px;font-size:11px;font-family:Menlo">+    }</div><div style="margin:0px;font-size:11px;font-family:Menlo">     return q;</div><div style="margin:0px;font-size:11px;font-family:Menlo"> }</div><p style="margin:0px;font-size:11px;font-family:Menlo;min-height:13px"> <br></p><div style="margin:0px;font-size:11px;font-family:Menlo">@@ -2361,5 +2399,7 @@</div><div style="margin:0px;font-size:11px;font-family:Menlo">     X265_FREE(m_rce2Pass);</div><div style="margin:0px;font-size:11px;font-family:Menlo">     for (int i = 0; i < 2; i++)</div><div style="margin:0px;font-size:11px;font-family:Menlo">         X265_FREE(m_cuTreeStats.qpBuffer[i]);</div><div style="margin:0px;font-size:11px;font-family:Menlo">+    </div><div style="margin:0px;font-size:11px;font-family:Menlo">+    X265_FREE(m_param->rc.zones);</div><div style="margin:0px;font-size:11px;font-family:Menlo"> }</div><p style="margin:0px;font-size:11px;font-family:Menlo;min-height:13px"> <br></p><div style="margin:0px;font-size:11px;font-family:Menlo">diff --git a/source/encoder/ratecontrol.h b/source/encoder/ratecontrol.h</div><div style="margin:0px;font-size:11px;font-family:Menlo">--- a/source/encoder/ratecontrol.h</div><div style="margin:0px;font-size:11px;font-family:Menlo">+++ b/source/encoder/ratecontrol.h</div><div style="margin:0px;font-size:11px;font-family:Menlo">@@ -247,6 +247,7 @@</div><div style="margin:0px;font-size:11px;font-family:Menlo">     int m_amortizeFrames;</div><div style="margin:0px;font-size:11px;font-family:Menlo">     double m_amortizeFraction;</div><p style="margin:0px;font-size:11px;font-family:Menlo;min-height:13px"> <br></p><div style="margin:0px;font-size:11px;font-family:Menlo">+    x265_zone* getZone();</div><div style="margin:0px;font-size:11px;font-family:Menlo">     double getQScale(RateControlEntry *rce, double rateFactor);</div><div style="margin:0px;font-size:11px;font-family:Menlo">     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">     void accumPQpUpdate();</div><div style="margin:0px;font-size:11px;font-family:Menlo">diff --git a/source/x265.cpp b/source/x265.cpp</div><div style="margin:0px;font-size:11px;font-family:Menlo">--- a/source/x265.cpp</div><div style="margin:0px;font-size:11px;font-family:Menlo">+++ b/source/x265.cpp</div><div style="margin:0px;font-size:11px;font-family:Menlo">@@ -150,6 +150,7 @@</div><div style="margin:0px;font-size:11px;font-family:Menlo">     { "vbv-init",       required_argument, NULL, 0 },</div><div style="margin:0px;font-size:11px;font-family:Menlo">     { "bitrate",        required_argument, NULL, 0 },</div><div style="margin:0px;font-size:11px;font-family:Menlo">     { "qp",             required_argument, NULL, 'q' },</div><div style="margin:0px;font-size:11px;font-family:Menlo">+    { "zones",          required_argument, NULL, 0 },</div><div style="margin:0px;font-size:11px;font-family:Menlo">     { "aq-mode",        required_argument, NULL, 0 },</div><div style="margin:0px;font-size:11px;font-family:Menlo">     { "aq-strength",    required_argument, NULL, 0 },</div><div style="margin:0px;font-size:11px;font-family:Menlo">     { "ipratio",        required_argument, NULL, 0 },</div><div style="margin:0px;font-size:11px;font-family:Menlo">@@ -435,6 +436,12 @@</div><div style="margin:0px;font-size:11px;font-family:Menlo">     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">     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">     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">+    H1("   --zones <zone0>/<zone1>/...   Tweak the bitrate of regions of the video\n");</div><div style="margin:0px;font-size:11px;font-family:Menlo">+    H1("                                 Each zone is of the form\n");</div><div style="margin:0px;font-size:11px;font-family:Menlo">+    H1("                                   <start frame>,<end frame>,<option>\n");</div><div style="margin:0px;font-size:11px;font-family:Menlo">+    H1("                                   where <option> is either\n");</div><div style="margin:0px;font-size:11px;font-family:Menlo">+    H1("                                       q=<integer> (force QP)\n");</div><div style="margin:0px;font-size:11px;font-family:Menlo">+    H1("                                   or  b=<float> (bitrate multiplier)\n");</div><div style="margin:0px;font-size:11px;font-family:Menlo">     H1("   --qpfile <string>             Force frametypes and QPs for some or all frames\n");</div><div style="margin:0px;font-size:11px;font-family:Menlo">     H1("                                 Format of each line: framenumber frametype QP\n");</div><div style="margin:0px;font-size:11px;font-family:Menlo">     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">diff --git a/source/x265.h b/source/x265.h</div><div style="margin:0px;font-size:11px;font-family:Menlo">--- a/source/x265.h</div><div style="margin:0px;font-size:11px;font-family:Menlo">+++ b/source/x265.h</div><div style="margin:0px;font-size:11px;font-family:Menlo">@@ -328,6 +328,16 @@</div><div style="margin:0px;font-size:11px;font-family:Menlo"> static const char * const x265_interlace_names[] = { "prog", "tff", "bff", 0 };</div><div style="margin:0px;font-size:11px;font-family:Menlo"> 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"> <br></p><div style="margin:0px;font-size:11px;font-family:Menlo">+/* Zones: override ratecontrol for specific sections of the video.</div><div style="margin:0px;font-size:11px;font-family:Menlo">+ * If zones overlap, whichever comes later in the list takes precedence. */</div><div style="margin:0px;font-size:11px;font-family:Menlo">+typedef struct x265_zone</div><div style="margin:0px;font-size:11px;font-family:Menlo">+{</div><div style="margin:0px;font-size:11px;font-family:Menlo">+    int startFrame, endFrame;   /* range of frame numbers */</div><div style="margin:0px;font-size:11px;font-family:Menlo">+    int bForceQp;               /* whether to use qp vs bitrate factor */</div><div style="margin:0px;font-size:11px;font-family:Menlo">+    int qp;</div><div style="margin:0px;font-size:11px;font-family:Menlo">+    float bitrateFactor;</div><div style="margin:0px;font-size:11px;font-family:Menlo">+} x265_zone;</div><div style="margin:0px;font-size:11px;font-family:Menlo">+    </div><div style="margin:0px;font-size:11px;font-family:Menlo"> /* x265 input parameters</div><div style="margin:0px;font-size:11px;font-family:Menlo">  *</div><div style="margin:0px;font-size:11px;font-family:Menlo">  * For version safety you may use x265_param_alloc/free() to manage the</div><div style="margin:0px;font-size:11px;font-family:Menlo">@@ -867,6 +877,10 @@</div><p style="margin:0px;font-size:11px;font-family:Menlo;min-height:13px"> <br></p><div style="margin:0px;font-size:11px;font-family:Menlo">         /* 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">         int       bEnableSlowFirstPass;</div><div style="margin:0px;font-size:11px;font-family:Menlo">+        </div><div style="margin:0px;font-size:11px;font-family:Menlo">+        /* ratecontrol overrides */</div><div style="margin:0px;font-size:11px;font-family:Menlo">+        int        zoneCount;</div><div style="margin:0px;font-size:11px;font-family:Menlo">+        x265_zone* zones;</div><p style="margin:0px;font-size:11px;font-family:Menlo;min-height:13px"> <br></p><div style="margin:0px;font-size:11px;font-family:Menlo">         /* specify a text file which contains MAX_MAX_QP + 1 floating point</div><div style="margin:0px;font-size:11px;font-family:Menlo">          * values to be copied into x265_lambda_tab and a second set of</div><div><br></div></div><br>_______________________________________________<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" target="_blank">https://mailman.videolan.org/listinfo/x265-devel</a><br>
<br></blockquote></div><br></div>