<div dir="ltr"><br><div class="gmail_extra"><br><div class="gmail_quote">On Thu, May 17, 2018 at 6:22 PM,  <span dir="ltr"><<a href="mailto:mahesh@multicorewareinc.com" target="_blank">mahesh@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 Mahesh Pittala<br>
# Date 1526553420 -19800<br>
#      Thu May 17 16:07:00 2018 +0530<br>
# Branch stable<br>
# Node ID d3d9943b60cf058c1b22ab7afb5c0f<wbr>79b98b9769<br>
# Parent  3cef29225ef431c820c8e5593b00c3<wbr>c225bfffdc<br>
Fix VBV Lookahead in analysis load to achieve target bitrate<br>
<br>
Details:<br>
If save and the load encodes have the same max CU size then number of CTU rows<br>
will be doubled and total number of CTU's are 4 times compared to<br>
analysis save encode so copying one CTU's vbv cost to 4 CTU's in load and one<br>
vbv row's satdcost to 2 consecutive rows(cost is multiplied by 2 here)<br>
<br>
diff -r 3cef29225ef4 -r d3d9943b60cf source/encoder/encoder.cpp<br>
--- a/source/encoder/encoder.cpp        Thu May 17 12:18:34 2018 +0530<br>
+++ b/source/encoder/encoder.cpp        Thu May 17 16:07:00 2018 +0530<br>
@@ -1233,26 +1233,27 @@<br>
                             int vbvCount = m_param->lookaheadDepth + m_param->bframes + 2;<br>
                             for (int index = 0; index < vbvCount; index++)<br>
                             {<br>
-                                pic_out->analysisData.<wbr>lookahead.plannedSatd[index] = outFrame->m_lowres.<wbr>plannedSatd[index] * factor;<br>
+                                pic_out->analysisData.<wbr>lookahead.plannedSatd[index] = outFrame->m_lowres.<wbr>plannedSatd[index];<br>
                                 pic_out->analysisData.<wbr>lookahead.plannedType[index] = outFrame->m_lowres.<wbr>plannedType[index];<br>
                             }<br>
                             for (uint32_t index = 0; index < pic_out->analysisData.<wbr>numCuInHeight; index++)<br>
                             {<br>
-                                outFrame->m_analysisData.<wbr>lookahead.intraSatdForVbv[<wbr>index] = outFrame->m_encData->m_<wbr>rowStat[index].intraSatdForVbv * factor;<br>
-                                outFrame->m_analysisData.<wbr>lookahead.satdForVbv[index] = outFrame->m_encData->m_<wbr>rowStat[index].satdForVbv * factor;<br>
+                                outFrame->m_analysisData.<wbr>lookahead.intraSatdForVbv[<wbr>index] = outFrame->m_encData->m_<wbr>rowStat[index].<wbr>intraSatdForVbv;<br>
+                                outFrame->m_analysisData.<wbr>lookahead.satdForVbv[index] = outFrame->m_encData->m_<wbr>rowStat[index].satdForVbv;<br>
                             }<br>
                             pic_out->analysisData.<wbr>lookahead.intraSatdForVbv = outFrame->m_analysisData.<wbr>lookahead.intraSatdForVbv;<br>
                             pic_out->analysisData.<wbr>lookahead.satdForVbv = outFrame->m_analysisData.<wbr>lookahead.satdForVbv;<br>
                             for (uint32_t index = 0; index < pic_out->analysisData.<wbr>numCUsInFrame; index++)<br>
                             {<br>
-                                outFrame->m_analysisData.<wbr>lookahead.intraVbvCost[index] = outFrame->m_encData->m_cuStat[<wbr>index].intraVbvCost * factor;<br>
-                                outFrame->m_analysisData.<wbr>lookahead.vbvCost[index] = outFrame->m_encData->m_cuStat[<wbr>index].vbvCost * factor;<br>
+                                outFrame->m_analysisData.<wbr>lookahead.intraVbvCost[index] = outFrame->m_encData->m_cuStat[<wbr>index].intraVbvCost;<br>
+                                outFrame->m_analysisData.<wbr>lookahead.vbvCost[index] = outFrame->m_encData->m_cuStat[<wbr>index].vbvCost;<br>
                             }<br>
                             pic_out->analysisData.<wbr>lookahead.intraVbvCost = outFrame->m_analysisData.<wbr>lookahead.intraVbvCost;<br>
                             pic_out->analysisData.<wbr>lookahead.vbvCost = outFrame->m_analysisData.<wbr>lookahead.vbvCost;<br>
                         }<br>
                     }<br>
                     writeAnalysisFile(&pic_out-><wbr>analysisData, *outFrame->m_encData);<br>
+                    pic_out->analysisData.<wbr>saveParam = pic_out->analysisData.<wbr>saveParam;<br>
                     if (m_param->bUseAnalysisFile)<br>
                         freeAnalysis(&pic_out-><wbr>analysisData);<br>
                 }<br>
@@ -3411,6 +3412,21 @@<br>
         X265_FREAD(analysis-><wbr>lookahead.vbvCost, sizeof(uint32_t), analysis->numCUsInFrame, m_analysisFileIn, picData->lookahead.vbvCost);<br>
         X265_FREAD(analysis-><wbr>lookahead.satdForVbv, sizeof(uint32_t), analysis->numCuInHeight, m_analysisFileIn, picData->lookahead.satdForVbv)<wbr>;<br>
         X265_FREAD(analysis-><wbr>lookahead.intraSatdForVbv, sizeof(uint32_t), analysis->numCuInHeight, m_analysisFileIn, picData->lookahead.<wbr>intraSatdForVbv);<br>
+<br>
+        int vbvCount = m_param->lookaheadDepth + m_param->bframes + 2;<br>
+        for (int index = 0; index < vbvCount; index++)<br>
+            analysis->lookahead.<wbr>plannedSatd[index] = picData->lookahead.<wbr>plannedSatd[index] * (2 * m_param->scaleFactor);<br>
+<br>
+        for (uint32_t i = 0; i < analysis->numCuInHeight; i++)<br>
+        {<br>
+            analysis->lookahead.<wbr>satdForVbv[i] = analysis->lookahead.<wbr>satdForVbv[i] * (2* m_param->scaleFactor);<br>
+            analysis->lookahead.<wbr>intraSatdForVbv[i] = analysis->lookahead.<wbr>intraSatdForVbv[i] * (2 * m_param->scaleFactor);<br>
+        }<br>
+        for (uint32_t i = 0; i < analysis->numCUsInFrame; i++)<br>
+        {<br>
+            analysis->lookahead.vbvCost[i] = analysis->lookahead.vbvCost[i] * (2 * m_param->scaleFactor);<br>
+            analysis->lookahead.<wbr>intraVbvCost[i] = analysis->lookahead.<wbr>intraVbvCost[i] * (2 * m_param->scaleFactor);<br>
+        }<br>
     }<br>
     if (analysis->sliceType == X265_TYPE_IDR || analysis->sliceType == X265_TYPE_I)<br>
     {<br>
@@ -3663,21 +3679,71 @@<br>
<br>
     int numPartitions = analysis->numPartitions;<br>
     int numCUsInFrame = analysis->numCUsInFrame;<br>
+    int numCuInHeight = analysis->numCuInHeight;<br>
     /* Allocate memory for scaled resoultion's numPartitions and numCUsInFrame*/<br>
     analysis->numPartitions = m_param->num4x4Partitions;<br>
     analysis->numCUsInFrame = cuLoc.heightInCU * cuLoc.widthInCU;<br>
+    analysis->numCuInHeight = cuLoc.heightInCU;<br>
<br>
     /* Memory is allocated for inter and intra analysis data based on the slicetype */<br>
     allocAnalysis(analysis);<br>
<br>
     analysis->numPartitions = numPartitions * factor;<br>
     analysis->numCUsInFrame = numCUsInFrame;<br>
+    analysis->numCuInHeight = numCuInHeight;<br>
     if (m_param->bDisableLookahead && m_rateControl->m_isVbv)<br>
     {<br>
-        X265_FREAD(analysis-><wbr>lookahead.intraVbvCost, sizeof(uint32_t), analysis->numCUsInFrame, m_analysisFileIn, picData->lookahead.<wbr>intraVbvCost);<br>
-        X265_FREAD(analysis-><wbr>lookahead.vbvCost, sizeof(uint32_t), analysis->numCUsInFrame, m_analysisFileIn, picData->lookahead.vbvCost);<br>
-        X265_FREAD(analysis-><wbr>lookahead.satdForVbv, sizeof(uint32_t), analysis->numCuInHeight, m_analysisFileIn, picData->lookahead.satdForVbv)<wbr>;<br>
-        X265_FREAD(analysis-><wbr>lookahead.intraSatdForVbv, sizeof(uint32_t), analysis->numCuInHeight, m_analysisFileIn, picData->lookahead.<wbr>intraSatdForVbv);<br>
+        uint32_t width = analysis->numCUsInFrame / analysis->numCuInHeight;<br>
+        bool skipLastRow = (analysis->numCuInHeight * 2) > cuLoc.heightInCU;<br>
+        bool skipLastCol = (width * 2) > cuLoc.widthInCU;<br>
+        uint32_t *intraVbvCostBuf = NULL, *vbvCostBuf = NULL, *satdForVbvBuf = NULL, *intraSatdForVbvBuf = NULL;<br>
+        intraVbvCostBuf = X265_MALLOC(uint32_t, analysis->numCUsInFrame);<br>
+        vbvCostBuf = X265_MALLOC(uint32_t, analysis->numCUsInFrame);<br>
+        satdForVbvBuf = X265_MALLOC(uint32_t, analysis->numCuInHeight);<br>
+        intraSatdForVbvBuf = X265_MALLOC(uint32_t, analysis->numCuInHeight);<br>
+<br>
+        X265_FREAD(intraVbvCostBuf, sizeof(uint32_t), analysis->numCUsInFrame, m_analysisFileIn, picData->lookahead.<wbr>intraVbvCost);<br>
+        X265_FREAD(vbvCostBuf, sizeof(uint32_t), analysis->numCUsInFrame, m_analysisFileIn, picData->lookahead.vbvCost);<br>
+        X265_FREAD(satdForVbvBuf, sizeof(uint32_t), analysis->numCuInHeight, m_analysisFileIn, picData->lookahead.satdForVbv)<wbr>;<br>
+        X265_FREAD(intraSatdForVbvBuf, sizeof(uint32_t), analysis->numCuInHeight, m_analysisFileIn, picData->lookahead.<wbr>intraSatdForVbv);<br>
+<br>
+        int k = 0;<br>
+        for (uint32_t i = 0; i < analysis->numCuInHeight; i++)<br>
+        {<br>
+            analysis->lookahead.<wbr>satdForVbv[m_param-><wbr>scaleFactor * i] = satdForVbvBuf[i] * m_param->scaleFactor;<br>
+            analysis->lookahead.<wbr>intraSatdForVbv[m_param-><wbr>scaleFactor * i] = intraSatdForVbvBuf[i] * m_param->scaleFactor;<br>
+            if (!(i == (analysis->numCuInHeight - 1) && skipLastRow))<br>
+            {<br>
+                analysis->lookahead.<wbr>satdForVbv[(m_param-><wbr>scaleFactor * i) + 1] = satdForVbvBuf[i] * m_param->scaleFactor;<br>
+                analysis->lookahead.<wbr>intraSatdForVbv[(m_param-><wbr>scaleFactor * i) + 1] = intraSatdForVbvBuf[i] * m_param->scaleFactor;<br>
+            }<br>
+<br>
+            for (uint32_t j = 0; j < width; j++, k++)<br>
+            {<br>
+                analysis->lookahead.vbvCost[(i * m_param->scaleFactor * cuLoc.widthInCU) + (j * m_param->scaleFactor)] = vbvCostBuf[k];<br>
+                analysis->lookahead.<wbr>intraVbvCost[(i * m_param->scaleFactor * cuLoc.widthInCU) + (j * m_param->scaleFactor)] = intraVbvCostBuf[k];<br>
+<br>
+                if (!(j == (width - 1) && skipLastCol))<br>
+                {<br>
+                    analysis->lookahead.vbvCost[(i * m_param->scaleFactor * cuLoc.widthInCU) + (j * m_param->scaleFactor) + 1] = vbvCostBuf[k];<br>
+                    analysis->lookahead.<wbr>intraVbvCost[(i * m_param->scaleFactor * cuLoc.widthInCU) + (j * m_param->scaleFactor) + 1] = intraVbvCostBuf[k];<br>
+                }<br>
+                if (!(i == (analysis->numCuInHeight - 1) && skipLastRow))<br>
+                {<br>
+                    analysis->lookahead.vbvCost[(i * m_param->scaleFactor * cuLoc.widthInCU) + cuLoc.widthInCU + (j * m_param->scaleFactor)] = vbvCostBuf[k];<br>
+                    analysis->lookahead.<wbr>intraVbvCost[(i * m_param->scaleFactor * cuLoc.widthInCU) + cuLoc.widthInCU + (j * m_param->scaleFactor)] = intraVbvCostBuf[k];<br>
+                    if (!(j == (width - 1) && skipLastCol))<br>
+                    {<br>
+                        analysis->lookahead.vbvCost[(i * m_param->scaleFactor * cuLoc.widthInCU) + cuLoc.widthInCU + (j * m_param->scaleFactor) + 1] = vbvCostBuf[k];<br>
+                        analysis->lookahead.<wbr>intraVbvCost[(i * m_param->scaleFactor * cuLoc.widthInCU) + cuLoc.widthInCU + (j * m_param->scaleFactor) + 1] = intraVbvCostBuf[k];<br>
+                    }<br>
+                }<br>
+            }<br>
+        }<br>
+        X265_FREE(satdForVbvBuf);<br>
+        X265_FREE(intraSatdForVbvBuf);<br>
+        X265_FREE(intraVbvCostBuf);<br>
+        X265_FREE(vbvCostBuf);<br>
     }<br>
<br>
     if (analysis->sliceType == X265_TYPE_IDR || analysis->sliceType == X265_TYPE_I)<br>
@@ -3912,6 +3978,7 @@<br>
     /* Restore to the current encode's numPartitions and numCUsInFrame */<br>
     analysis->numPartitions = m_param->num4x4Partitions;<br>
     analysis->numCUsInFrame = cuLoc.heightInCU * cuLoc.widthInCU;<br>
+    analysis->numCuInHeight = cuLoc.heightInCU;<br>
 #undef X265_FREAD<br>
 }<br>
<br>
@@ -3944,23 +4011,23 @@<br>
     }\<br>
     count++;\<br>
<br>
-    x265_analysis_validate saveParam = analysis->saveParam;<br>
+    x265_analysis_validate *saveParam = &analysis->saveParam;<br>
     FILE*     fileOffset = NULL;<br>
     int       readValue = 0;<br>
     int       count = 0;<br>
<br>
-    X265_PARAM_VALIDATE(saveParam.<wbr>maxNumReferences, sizeof(int), 1, &m_param->maxNumReferences);<br>
-    X265_PARAM_VALIDATE(saveParam.<wbr>analysisReuseLevel, sizeof(int), 1, &m_param->analysisReuseLevel);<br>
-    X265_PARAM_VALIDATE(saveParam.<wbr>scaleFactor, sizeof(int), 1, &m_param->scaleFactor);<br>
-    X265_PARAM_VALIDATE(saveParam.<wbr>keyframeMax, sizeof(int), 1, &m_param->keyframeMax);<br>
-    X265_PARAM_VALIDATE(saveParam.<wbr>keyframeMin, sizeof(int), 1, &m_param->keyframeMin);<br>
-    X265_PARAM_VALIDATE(saveParam.<wbr>openGOP, sizeof(int), 1, &m_param->bOpenGOP);<br>
-    X265_PARAM_VALIDATE(saveParam.<wbr>bframes, sizeof(int), 1, &m_param->bframes);<br>
-    X265_PARAM_VALIDATE(saveParam.<wbr>bPyramid, sizeof(int), 1, &m_param->bBPyramid);<br>
+    X265_PARAM_VALIDATE(saveParam-<wbr>>maxNumReferences, sizeof(int), 1, &m_param->maxNumReferences);<br>
+    X265_PARAM_VALIDATE(saveParam-<wbr>>analysisReuseLevel, sizeof(int), 1, &m_param->analysisReuseLevel);<br>
+    X265_PARAM_VALIDATE(saveParam-<wbr>>scaleFactor, sizeof(int), 1, &m_param->scaleFactor);<br>
+    X265_PARAM_VALIDATE(saveParam-<wbr>>keyframeMax, sizeof(int), 1, &m_param->keyframeMax);<br>
+    X265_PARAM_VALIDATE(saveParam-<wbr>>keyframeMin, sizeof(int), 1, &m_param->keyframeMin);<br>
+    X265_PARAM_VALIDATE(saveParam-<wbr>>openGOP, sizeof(int), 1, &m_param->bOpenGOP);<br>
+    X265_PARAM_VALIDATE(saveParam-<wbr>>bframes, sizeof(int), 1, &m_param->bframes);<br>
+    X265_PARAM_VALIDATE(saveParam-<wbr>>bPyramid, sizeof(int), 1, &m_param->bBPyramid);<br>
     /* Enable m_saveCTUSize if the save and load encodes have the same maxCU size */<br>
     if (writeFlag)<br>
     {<br>
-        X265_PARAM_VALIDATE(saveParam.<wbr>maxCUSize, sizeof(int), 1, &m_param->maxCUSize);<br>
+        X265_PARAM_VALIDATE(saveParam-<wbr>>maxCUSize, sizeof(int), 1, &m_param->maxCUSize);<br>
     }<br>
     else<br>
     {<br>
@@ -3971,7 +4038,7 @@<br>
             m_aborted = true;<br>
         }<br>
         else if (!m_param->bUseAnalysisFile)<br>
-            readValue = saveParam.maxCUSize;<br>
+            readValue = saveParam->maxCUSize;<br>
<br>
         m_saveCTUSize = 0;<br>
         if (m_param->scaleFactor && g_log2Size[m_param->maxCUSize] == g_log2Size[readValue])<br>
@@ -3983,10 +4050,10 @@<br>
         }<br>
         count++;<br>
     }<br>
-    X265_PARAM_VALIDATE(saveParam.<wbr>minCUSize, sizeof(int), 1, &m_param->minCUSize);<br>
-    X265_PARAM_VALIDATE(saveParam.<wbr>radl, sizeof(int), 1, &m_param->radl);<br>
-    X265_PARAM_VALIDATE(saveParam.<wbr>lookaheadDepth, sizeof(int), 1, &m_param->lookaheadDepth);<br>
-    X265_PARAM_VALIDATE(saveParam.<wbr>gopLookahead, sizeof(int), 1, &m_param->gopLookahead);<br>
+    X265_PARAM_VALIDATE(saveParam-<wbr>>minCUSize, sizeof(int), 1, &m_param->minCUSize);<br>
+    X265_PARAM_VALIDATE(saveParam-<wbr>>radl, sizeof(int), 1, &m_param->radl);<br>
+    X265_PARAM_VALIDATE(saveParam-<wbr>>lookaheadDepth, sizeof(int), 1, &m_param->lookaheadDepth);<br>
+    X265_PARAM_VALIDATE(saveParam-<wbr>>gopLookahead, sizeof(int), 1, &m_param->gopLookahead);<br>
     return (count * sizeof(int));<br>
 #undef X265_PARAM_VALIDATE<br>
 }<br>
<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>
<br></blockquote></div><br></div><div class="gmail_extra">Pushed.</div></div>