<div dir="ltr">Please ignore this patch.</div><div class="gmail_extra"><br><div class="gmail_quote">On Fri, Dec 1, 2017 at 8:34 PM,  <span dir="ltr"><<a href="mailto:ashok@multicorewareinc.com" target="_blank">ashok@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 Ashok Kumar Mishra <<a href="mailto:ashok@multicorewareinc.com">ashok@multicorewareinc.com</a>><br>
# Date 1510144762 -19800<br>
#      Wed Nov 08 18:09:22 2017 +0530<br>
# Node ID 5bf57563452b17c48486ab73f3fcfa<wbr>4ce4d639ba<br>
# Parent  b1dfa312234ed72c3541831a15f307<wbr>feaf79484d<br>
LookaheadTLD: frameVariance is not used<br>
<br>
diff -r b1dfa312234e -r 5bf57563452b source/common/frame.cpp<br>
--- a/source/common/frame.cpp   Thu Nov 30 10:06:49 2017 +0530<br>
+++ b/source/common/frame.cpp   Wed Nov 08 18:09:22 2017 +0530<br>
@@ -84,21 +84,16 @@<br>
         m_analysisData.interData = NULL;<br>
         m_analysis2Pass.<wbr>analysisFramedata = NULL;<br>
     }<br>
-<br>
-    if (m_fencPic->create(param, !!m_param->bCopyPicToFrame) && m_lowres.create(m_fencPic, param->bframes, !!param->rc.aqMode || !!param->bAQMotion, param->rc.qgSize))<br>
+    if (m_fencPic->create(param, !!m_param->bCopyPicToFrame) && m_lowres.create(param, m_fencPic, param->rc.qgSize))<br>
     {<br>
         X265_CHECK((m_reconColCount == NULL), "m_reconColCount was initialized");<br>
         m_numRows = (m_fencPic->m_picHeight + param->maxCUSize - 1)  / param->maxCUSize;<br>
         m_reconRowFlag = new ThreadSafeInteger[m_numRows];<br>
         m_reconColCount = new ThreadSafeInteger[m_numRows];<br>
-<br>
         if (quantOffsets)<br>
         {<br>
-            int32_t cuCount;<br>
-            if (param->rc.qgSize == 8)<br>
-                cuCount = m_lowres.maxBlocksInRowFullRes * m_lowres.<wbr>maxBlocksInColFullRes;<br>
-            else<br>
-                cuCount = m_lowres.maxBlocksInRow * m_lowres.maxBlocksInCol;<br>
+                       int32_t cuCount = (param->rc.qgSize == 8) ? m_lowres.maxBlocksInRowFullRes * m_lowres.maxBlocksInColFullRes :<br>
+                                                                       m_lowres.maxBlocksInRowLowRes * m_lowres.maxBlocksInColLowRes;<br>
             m_quantOffsets = new float[cuCount];<br>
         }<br>
         return true;<br>
diff -r b1dfa312234e -r 5bf57563452b source/common/lowres.cpp<br>
--- a/source/common/lowres.cpp  Thu Nov 30 10:06:49 2017 +0530<br>
+++ b/source/common/lowres.cpp  Wed Nov 08 18:09:22 2017 +0530<br>
@@ -24,50 +24,58 @@<br>
 #include "picyuv.h"<br>
 #include "lowres.h"<br>
 #include "mv.h"<br>
-<br>
 using namespace X265_NS;<br>
-<br>
-bool Lowres::create(PicYuv *origPic, int _bframes, bool bAQEnabled, uint32_t qgSize)<br>
+void TEncPicQPAdaptationLayer::<wbr>create(int iWidth, int iHeight, uint32_t uiAQPartWidth, uint32_t uiAQPartHeight)<br>
+{<br>
+       m_AQPartWidth = uiAQPartWidth;<br>
+       m_AQPartHeight = uiAQPartHeight;<br>
+       m_NumAQPartInWidth = (iWidth + m_AQPartWidth - 1) / m_AQPartWidth;<br>
+       m_NumAQPartInHeight = (iHeight + m_AQPartHeight - 1) / m_AQPartHeight;<br>
+       m_dActivity = new double[m_NumAQPartInWidth * m_NumAQPartInHeight];<br>
+}<br>
+bool Lowres::create(x265_param* param, PicYuv *origPic, uint32_t qgSize)<br>
 {<br>
     isLowres = true;<br>
-    bframes = _bframes;<br>
+       bframes = param->bframes;<br>
     width = origPic->m_picWidth / 2;<br>
     lines = origPic->m_picHeight / 2;<br>
     lumaStride = width + 2 * origPic->m_lumaMarginX;<br>
     if (lumaStride & 31)<br>
         lumaStride += 32 - (lumaStride & 31);<br>
-    maxBlocksInRow = (width + X265_LOWRES_CU_SIZE - 1) >> X265_LOWRES_CU_BITS;<br>
-    maxBlocksInCol = (lines + X265_LOWRES_CU_SIZE - 1) >> X265_LOWRES_CU_BITS;<br>
-    maxBlocksInRowFullRes = maxBlocksInRow * 2;<br>
-    maxBlocksInColFullRes = maxBlocksInCol * 2;<br>
-    int cuCount = maxBlocksInRow * maxBlocksInCol;<br>
-    int cuCountFullRes;<br>
-    if (qgSize == 8)<br>
-        cuCountFullRes = maxBlocksInRowFullRes * maxBlocksInColFullRes;<br>
-    else<br>
-        cuCountFullRes = cuCount;<br>
-<br>
+    maxBlocksInRowLowRes = (width + X265_LOWRES_CU_SIZE - 1) >> X265_LOWRES_CU_BITS;<br>
+    maxBlocksInColLowRes = (lines + X265_LOWRES_CU_SIZE - 1) >> X265_LOWRES_CU_BITS;<br>
+    maxBlocksInRowFullRes = maxBlocksInRowLowRes * 2;<br>
+    maxBlocksInColFullRes = maxBlocksInColLowRes * 2;<br>
+    int cuCountLowRes = maxBlocksInRowLowRes * maxBlocksInColLowRes;<br>
+       int cuCountFullRes = (qgSize > 8) ? maxBlocksInRowLowRes * maxBlocksInColLowRes :<br>
+                                               maxBlocksInRowFullRes * maxBlocksInColFullRes;<br>
     /* rounding the width to multiple of lowres CU size */<br>
-    width = maxBlocksInRow * X265_LOWRES_CU_SIZE;<br>
-    lines = maxBlocksInCol * X265_LOWRES_CU_SIZE;<br>
-<br>
+       width = maxBlocksInRowLowRes * X265_LOWRES_CU_SIZE;<br>
+       lines = maxBlocksInColLowRes * X265_LOWRES_CU_SIZE;<br>
     size_t planesize = lumaStride * (lines + 2 * origPic->m_lumaMarginY);<br>
     size_t padoffset = lumaStride * origPic->m_lumaMarginY + origPic->m_lumaMarginX;<br>
-    if (bAQEnabled)<br>
+       if (!!param->rc.aqMode || !!param->bAQMotion)<br>
     {<br>
         CHECKED_MALLOC_ZERO(<wbr>qpAqOffset, double, cuCountFullRes);<br>
         CHECKED_MALLOC_ZERO(<wbr>qpAqMotionOffset, double, cuCountFullRes);<br>
         CHECKED_MALLOC_ZERO(<wbr>invQscaleFactor, int, cuCountFullRes);<br>
         CHECKED_MALLOC_ZERO(<wbr>qpCuTreeOffset, double, cuCountFullRes);<br>
-        CHECKED_MALLOC_ZERO(<wbr>blockVariance, uint32_t, cuCountFullRes);<br>
         if (qgSize == 8)<br>
-            CHECKED_MALLOC_ZERO(<wbr>invQscaleFactor8x8, int, cuCount);<br>
+                       CHECKED_MALLOC_ZERO(<wbr>invQscaleFactor8x8, int, cuCountLowRes);<br>
     }<br>
-    CHECKED_MALLOC(propagateCost, uint16_t, cuCount);<br>
-<br>
+       CHECKED_MALLOC(propagateCost, uint16_t, cuCountLowRes);<br>
     /* allocate lowres buffers */<br>
     CHECKED_MALLOC_ZERO(buffer[0], pixel, 4 * planesize);<br>
+       m_MaxAQDepth = g_log2Size[param->maxCUSize] - g_log2Size[qgSize] + 1;<br>
<br>
+       if (m_MaxAQDepth > 0)<br>
+       {<br>
+               m_acAQLayer = new TEncPicQPAdaptationLayer[m_<wbr>MaxAQDepth];<br>
+               for (uint32_t d = 0; d < m_MaxAQDepth; d++)<br>
+               {<br>
+                       m_acAQLayer[d].create(origPic-<wbr>>m_picWidth, origPic->m_picHeight, param->maxCUSize >> d, param->maxCUSize >> d);<br>
+               }<br>
+       }<br>
     buffer[1] = buffer[0] + planesize;<br>
     buffer[2] = buffer[1] + planesize;<br>
     buffer[3] = buffer[2] + planesize;<br>
@@ -76,29 +84,24 @@<br>
     lowresPlane[1] = buffer[1] + padoffset;<br>
     lowresPlane[2] = buffer[2] + padoffset;<br>
     lowresPlane[3] = buffer[3] + padoffset;<br>
-<br>
-    CHECKED_MALLOC(intraCost, int32_t, cuCount);<br>
-    CHECKED_MALLOC(intraMode, uint8_t, cuCount);<br>
-<br>
+       CHECKED_MALLOC(intraCost, int32_t, cuCountLowRes);<br>
+       CHECKED_MALLOC(intraMode, uint8_t, cuCountLowRes);<br>
     for (int i = 0; i < bframes + 2; i++)<br>
     {<br>
         for (int j = 0; j < bframes + 2; j++)<br>
         {<br>
-            CHECKED_MALLOC(rowSatds[i][j], int32_t, maxBlocksInCol);<br>
-            CHECKED_MALLOC(lowresCosts[i][<wbr>j], uint16_t, cuCount);<br>
+                       CHECKED_MALLOC(rowSatds[i][j], int32_t, maxBlocksInColLowRes);<br>
+                       CHECKED_MALLOC(lowresCosts[i][<wbr>j], uint16_t, cuCountLowRes);<br>
         }<br>
     }<br>
-<br>
     for (int i = 0; i < bframes + 1; i++)<br>
     {<br>
-        CHECKED_MALLOC(lowresMvs[0][i]<wbr>, MV, cuCount);<br>
-        CHECKED_MALLOC(lowresMvs[1][i]<wbr>, MV, cuCount);<br>
-        CHECKED_MALLOC(lowresMvCosts[<wbr>0][i], int32_t, cuCount);<br>
-        CHECKED_MALLOC(lowresMvCosts[<wbr>1][i], int32_t, cuCount);<br>
+               CHECKED_MALLOC(lowresMvs[0][i]<wbr>, MV, cuCountLowRes);<br>
+               CHECKED_MALLOC(lowresMvs[1][i]<wbr>, MV, cuCountLowRes);<br>
+               CHECKED_MALLOC(lowresMvCosts[<wbr>0][i], int32_t, cuCountLowRes);<br>
+               CHECKED_MALLOC(lowresMvCosts[<wbr>1][i], int32_t, cuCountLowRes);<br>
     }<br>
-<br>
     return true;<br>
-<br>
 fail:<br>
     return false;<br>
 }<br>
@@ -130,10 +133,8 @@<br>
     X265_FREE(invQscaleFactor);<br>
     X265_FREE(qpCuTreeOffset);<br>
     X265_FREE(propagateCost);<br>
-    X265_FREE(blockVariance);<br>
     X265_FREE(invQscaleFactor8x8);<br>
 }<br>
-<br>
 // (re) initialize lowres state<br>
 void Lowres::init(PicYuv *origPic, int poc)<br>
 {<br>
diff -r b1dfa312234e -r 5bf57563452b source/common/lowres.h<br>
--- a/source/common/lowres.h    Thu Nov 30 10:06:49 2017 +0530<br>
+++ b/source/common/lowres.h    Wed Nov 08 18:09:22 2017 +0530<br>
@@ -102,7 +102,18 @@<br>
         }<br>
     }<br>
 };<br>
+struct TEncPicQPAdaptationLayer<br>
+{<br>
+       uint32_t m_AQPartWidth;<br>
+       uint32_t m_AQPartHeight;<br>
+       uint32_t m_NumAQPartInWidth;<br>
+       uint32_t m_NumAQPartInHeight;<br>
+       double*  m_dActivity;<br>
+       double   m_dAvgActivity;<br>
<br>
+       void  create(int width, int height, uint32_t AQPartWidth, uint32_t AQPartHeight);<br>
+       void  destroy();<br>
+};<br>
 /* lowres buffers, sizes and strides */<br>
 struct Lowres : public ReferencePlanes<br>
 {<br>
@@ -132,34 +143,30 @@<br>
     uint16_t* lowresCosts[X265_BFRAME_MAX + 2][X265_BFRAME_MAX + 2];<br>
     int32_t*  lowresMvCosts[2][X265_BFRAME_<wbr>MAX + 1];<br>
     MV*       lowresMvs[2][X265_BFRAME_MAX + 1];<br>
-    uint32_t  maxBlocksInRow;<br>
-    uint32_t  maxBlocksInCol;<br>
+    uint32_t  maxBlocksInRowLowRes;<br>
+    uint32_t  maxBlocksInColLowRes;<br>
     uint32_t  maxBlocksInRowFullRes;<br>
     uint32_t  maxBlocksInColFullRes;<br>
-<br>
     /* used for vbvLookahead */<br>
     int       plannedType[X265_LOOKAHEAD_MAX + 1];<br>
     int64_t   plannedSatd[X265_LOOKAHEAD_MAX + 1];<br>
     int       indB;<br>
     int       bframes;<br>
-<br>
     /* rate control / adaptive quant data */<br>
-    double*   qpAqOffset;      // AQ QP offset values for each 16x16 CU<br>
-    double*   qpCuTreeOffset;  // cuTree QP offset values for each 16x16 CU<br>
+    double*   qpAqOffset;         // AQ QP offset values for each 16x16 CU<br>
+    double*   qpCuTreeOffset;     // cuTree QP offset values for each 16x16 CU<br>
     double*   qpAqMotionOffset;<br>
-    int*      invQscaleFactor; // qScale values for qp Aq Offsets<br>
+    int*      invQscaleFactor;    // qScale values for qp Aq Offsets<br>
     int*      invQscaleFactor8x8; // temporary buffer for qg-size 8<br>
-    uint32_t* blockVariance;<br>
-    uint64_t  wp_ssd[3];       // This is different than SSDY, this is sum(pixel^2) - sum(pixel)^2 for entire frame<br>
+    uint64_t  wp_ssd[3];          // This is different than SSDY, this is sum(pixel^2) - sum(pixel)^2 for entire frame<br>
     uint64_t  wp_sum[3];<br>
-    uint64_t  frameVariance;<br>
-<br>
+       TEncPicQPAdaptationLayer* m_acAQLayer;<br>
+       uint32_t m_MaxAQDepth;<br>
     /* cutree intermediate data */<br>
     uint16_t* propagateCost;<br>
     double    weightedCostDelta[X265_BFRAME_<wbr>MAX + 2];<br>
     ReferencePlanes weightedRef[X265_BFRAME_MAX + 2];<br>
-<br>
-    bool create(PicYuv *origPic, int _bframes, bool bAqEnabled, uint32_t qgSize);<br>
+       bool create(x265_param* param, PicYuv *origPic, uint32_t qgSize);<br>
     void destroy();<br>
     void init(PicYuv *origPic, int poc);<br>
 };<br>
diff -r b1dfa312234e -r 5bf57563452b source/encoder/encoder.cpp<br>
--- a/source/encoder/encoder.cpp        Thu Nov 30 10:06:49 2017 +0530<br>
+++ b/source/encoder/encoder.cpp        Wed Nov 08 18:09:22 2017 +0530<br>
@@ -1009,10 +1009,9 @@<br>
             if (m_param->rc.qgSize == 8)<br>
                 cuCount = inFrame->m_lowres.<wbr>maxBlocksInRowFullRes * inFrame->m_lowres.<wbr>maxBlocksInColFullRes;<br>
             else<br>
-                cuCount = inFrame->m_lowres.<wbr>maxBlocksInRow * inFrame->m_lowres.<wbr>maxBlocksInCol;<br>
+                               cuCount = inFrame->m_lowres.<wbr>maxBlocksInRowLowRes * inFrame->m_lowres.<wbr>maxBlocksInColLowRes;<br>
             memcpy(inFrame->m_<wbr>quantOffsets, pic_in->quantOffsets, cuCount * sizeof(float));<br>
         }<br>
-<br>
         if (m_pocLast == 0)<br>
             m_firstPts = inFrame->m_pts;<br>
         if (m_bframeDelay && m_pocLast == m_bframeDelay)<br>
diff -r b1dfa312234e -r 5bf57563452b source/encoder/sao.cpp<br>
--- a/source/encoder/sao.cpp    Thu Nov 30 10:06:49 2017 +0530<br>
+++ b/source/encoder/sao.cpp    Wed Nov 08 18:09:22 2017 +0530<br>
@@ -136,12 +136,9 @@<br>
<br>
         CHECKED_MALLOC(m_<wbr>clipTableBase,  pixel, maxY + 2 * rangeExt);<br>
         m_clipTable = &(m_clipTableBase[rangeExt]);<br>
-<br>
         // Share with fast clip lookup table<br>
-<br>
         for (int i = 0; i < rangeExt; i++)<br>
             m_clipTableBase[i] = 0;<br>
-<br>
         for (int i = 0; i < maxY; i++)<br>
             m_clipTable[i] = (pixel)i;<br>
<br>
diff -r b1dfa312234e -r 5bf57563452b source/encoder/search.cpp<br>
--- a/source/encoder/search.cpp Thu Nov 30 10:06:49 2017 +0530<br>
+++ b/source/encoder/search.cpp Wed Nov 08 18:09:22 2017 +0530<br>
@@ -1951,17 +1951,14 @@<br>
     if (mvs[0].x == 0x7FFF)<br>
         /* this motion search was not estimated by lookahead */<br>
         return 0;<br>
-<br>
     uint32_t block_x = (cu.m_cuPelX + g_zscanToPelX[pu.puAbsPartIdx] + pu.width / 2) >> 4;<br>
     uint32_t block_y = (cu.m_cuPelY + g_zscanToPelY[pu.puAbsPartIdx] + pu.height / 2) >> 4;<br>
-    uint32_t idx = block_y * m_frame->m_lowres.<wbr>maxBlocksInRow + block_x;<br>
-<br>
-    X265_CHECK(block_x < m_frame->m_lowres.<wbr>maxBlocksInRow, "block_x is too high\n");<br>
-    X265_CHECK(block_y < m_frame->m_lowres.<wbr>maxBlocksInCol, "block_y is too high\n");<br>
-<br>
+       uint32_t idx = block_y * m_frame->m_lowres.<wbr>maxBlocksInRowLowRes + block_x;<br>
+<br>
+       X265_CHECK(block_x < m_frame->m_lowres.<wbr>maxBlocksInRowLowRes, "block_x is too high\n");<br>
+       X265_CHECK(block_y < m_frame->m_lowres.<wbr>maxBlocksInColLowRes, "block_y is too high\n");<br>
     return mvs[idx] << 1; /* scale up lowres mv */<br>
 }<br>
-<br>
 /* Pick between the two AMVP candidates which is the best one to use as<br>
  * MVP for the motion search, based on SAD cost */<br>
 int Search::selectMVP(const CUData& cu, const PredictionUnit& pu, const MV amvp[AMVP_NUM_CANDS], int list, int ref)<br>
diff -r b1dfa312234e -r 5bf57563452b source/encoder/slicetype.cpp<br>
--- a/source/encoder/slicetype.cpp      Thu Nov 30 10:06:49 2017 +0530<br>
+++ b/source/encoder/slicetype.cpp      Wed Nov 08 18:09:22 2017 +0530<br>
@@ -120,7 +120,93 @@<br>
     x265_emms();<br>
     return (uint32_t)sum_ssd;<br>
 }<br>
+void LookaheadTLD::xPreanalyze(<wbr>Frame* curFrame)<br>
+{<br>
+       const uint32_t iWidth = curFrame->m_fencPic->m_<wbr>picWidth;<br>
+       const uint32_t iHeight = curFrame->m_fencPic->m_<wbr>picHeight;<br>
+       const intptr_t iStride = curFrame->m_fencPic->m_stride;<br>
<br>
+       for (uint32_t d = 0; d < curFrame->m_lowres.m_<wbr>MaxAQDepth; d++)<br>
+       {<br>
+               const pixel* src = curFrame->m_fencPic->m_picOrg[<wbr>0];;<br>
+               TEncPicQPAdaptationLayer pcAQLayer = curFrame->m_lowres.m_<wbr>acAQLayer[d];<br>
+               const uint32_t uiAQPartWidth = pcAQLayer.m_AQPartWidth;<br>
+               const uint32_t uiAQPartHeight = pcAQLayer.m_AQPartHeight;<br>
+               double* pcAQU = pcAQLayer.m_dActivity;<br>
+<br>
+               double dSumAct = 0.0;<br>
+               for (uint32_t y = 0; y < iHeight; y += uiAQPartHeight)<br>
+               {<br>
+                       const uint32_t uiCurrAQPartHeight = min(uiAQPartHeight, iHeight - y);<br>
+                       for (uint32_t x = 0; x < iWidth; x += uiAQPartWidth, pcAQU++)<br>
+                       {<br>
+                               const uint32_t uiCurrAQPartWidth = min(uiAQPartWidth, iWidth - x);<br>
+                               const pixel* pBlkY = &src[x];<br>
+                               uint64_t uiSum[4] = { 0, 0, 0, 0 };<br>
+                               uint64_t uiSumSq[4] = { 0, 0, 0, 0 };<br>
+                               uint32_t by = 0;<br>
+                               for (; by < uiCurrAQPartHeight >> 1; by++)<br>
+                               {<br>
+                                       uint32_t bx = 0;<br>
+                                       for (; bx < uiCurrAQPartWidth >> 1; bx++)<br>
+                                       {<br>
+                                               uiSum[0] += pBlkY[bx];<br>
+                                               uiSumSq[0] += pBlkY[bx] * pBlkY[bx];<br>
+                                       }<br>
+                                       for (; bx < uiCurrAQPartWidth; bx++)<br>
+                                       {<br>
+                                               uiSum[1] += pBlkY[bx];<br>
+                                               uiSumSq[1] += pBlkY[bx] * pBlkY[bx];<br>
+                                       }<br>
+                                       pBlkY += iStride;<br>
+                               }<br>
+                               for (; by < uiCurrAQPartHeight; by++)<br>
+                               {<br>
+                                       uint32_t bx = 0;<br>
+                                       for (; bx < uiCurrAQPartWidth >> 1; bx++)<br>
+                                       {<br>
+                                               uiSum[2] += pBlkY[bx];<br>
+                                               uiSumSq[2] += pBlkY[bx] * pBlkY[bx];<br>
+                                       }<br>
+                                       for (; bx < uiCurrAQPartWidth; bx++)<br>
+                                       {<br>
+                                               uiSum[3] += pBlkY[bx];<br>
+                                               uiSumSq[3] += pBlkY[bx] * pBlkY[bx];<br>
+                                       }<br>
+                                       pBlkY += iStride;<br>
+                               }<br>
+<br>
+                               assert((uiCurrAQPartWidth & 1) == 0);<br>
+                               assert((uiCurrAQPartHeight & 1) == 0);<br>
+                               const uint32_t pixelWidthOfQuadrants = uiCurrAQPartWidth >> 1;<br>
+                               const uint32_t pixelHeightOfQuadrants = uiCurrAQPartHeight >> 1;<br>
+                               const uint32_t numPixInAQPart = pixelWidthOfQuadrants * pixelHeightOfQuadrants;<br>
+<br>
+                               double dMinVar = DBL_MAX;<br>
+                               if (numPixInAQPart != 0)<br>
+                               {<br>
+                                       for (int i = 0; i < 4; i++)<br>
+                                       {<br>
+                                               const double dAverage = double(uiSum[i]) / numPixInAQPart;<br>
+                                               const double dVariance = double(uiSumSq[i]) / numPixInAQPart - dAverage * dAverage;<br>
+                                               dMinVar = min(dMinVar, dVariance);<br>
+                                       }<br>
+                               }<br>
+                               else<br>
+                               {<br>
+                                       dMinVar = 0.0;<br>
+                               }<br>
+                               double dActivity = 1.0 + dMinVar;<br>
+                               *pcAQU = dActivity;<br>
+                               dSumAct += dActivity;<br>
+                       }<br>
+                       src += iStride * uiCurrAQPartHeight;<br>
+               }<br>
+<br>
+               const double dAvgAct = dSumAct / (pcAQLayer.m_NumAQPartInWidth * pcAQLayer.m_NumAQPartInHeight)<wbr>;<br>
+               pcAQLayer.m_dAvgActivity = dAvgAct;<br>
+       }<br>
+}<br>
 void LookaheadTLD::<wbr>calcAdaptiveQuantFrame(Frame *curFrame, x265_param* param)<br>
 {<br>
     /* Actual adaptive quantization */<br>
@@ -156,14 +242,13 @@<br>
     double strength = 0.f;<br>
     if (param->rc.aqMode == X265_AQ_NONE || param->rc.aqStrength == 0)<br>
     {<br>
-        /* Need to init it anyways for CU tree */<br>
-        int cuCount = blockCount;<br>
-<br>
+//        /* Need to init it anyways for CU tree */<br>
+//        int cuCount = blockCount;<br>
         if (param->rc.aqMode && param->rc.aqStrength == 0)<br>
         {<br>
             if (quantOffsets)<br>
             {<br>
-                for (int cuxy = 0; cuxy < cuCount; cuxy++)<br>
+                               for (int cuxy = 0; cuxy < blockCount; cuxy++)<br>
                 {<br>
                     curFrame->m_lowres.<wbr>qpCuTreeOffset[cuxy] = curFrame->m_lowres.qpAqOffset[<wbr>cuxy] = quantOffsets[cuxy];<br>
                     curFrame->m_lowres.<wbr>invQscaleFactor[cuxy] = x265_exp2fix8(curFrame->m_<wbr>lowres.qpCuTreeOffset[cuxy]);<br>
@@ -171,9 +256,9 @@<br>
             }<br>
             else<br>
             {<br>
-                memset(curFrame->m_lowres.<wbr>qpCuTreeOffset, 0, cuCount * sizeof(double));<br>
-                memset(curFrame->m_lowres.<wbr>qpAqOffset, 0, cuCount * sizeof(double));<br>
-                for (int cuxy = 0; cuxy < cuCount; cuxy++)<br>
+                               memset(curFrame->m_lowres.<wbr>qpCuTreeOffset, 0, blockCount * sizeof(double));<br>
+                               memset(curFrame->m_lowres.<wbr>qpAqOffset, 0, blockCount * sizeof(double));<br>
+                               for (int cuxy = 0; cuxy < blockCount; cuxy++)<br>
                     curFrame->m_lowres.<wbr>invQscaleFactor[cuxy] = 256;<br>
             }<br>
         }<br>
@@ -188,35 +273,34 @@<br>
     }<br>
     else<br>
     {<br>
+               if (param->rc.aqMode == X265_AQ_AUTO_VARIANCE || param->rc.aqMode == X265_AQ_AUTO_VARIANCE_BIASED)<br>
+               {<br>
+                       xPreanalyze(curFrame);<br>
+               }<br>
+<br>
         blockXY = 0;<br>
         double avg_adj_pow2 = 0, avg_adj = 0, qp_adj = 0;<br>
         double bias_strength = 0.f;<br>
         if (param->rc.aqMode == X265_AQ_AUTO_VARIANCE || param->rc.aqMode == X265_AQ_AUTO_VARIANCE_BIASED)<br>
         {<br>
             double bit_depth_correction = 1.f / (1 << (2*(X265_DEPTH-8)));<br>
-            curFrame->m_lowres.<wbr>frameVariance = 0;<br>
-            uint64_t rowVariance = 0;<br>
+<br>
             for (blockY = 0; blockY < maxRow; blockY += loopIncr)<br>
             {<br>
-                rowVariance = 0;<br>
                 for (blockX = 0; blockX < maxCol; blockX += loopIncr)<br>
                 {<br>
                     uint32_t energy = acEnergyCu(curFrame, blockX, blockY, param->internalCsp, param->rc.qgSize);<br>
-                    curFrame->m_lowres.<wbr>blockVariance[blockXY] = energy;<br>
-                    rowVariance += energy;<br>
                     qp_adj = pow(energy * bit_depth_correction + 1, 0.1);<br>
                     curFrame->m_lowres.<wbr>qpCuTreeOffset[blockXY] = qp_adj;<br>
                     avg_adj += qp_adj;<br>
                     avg_adj_pow2 += qp_adj * qp_adj;<br>
                     blockXY++;<br>
                 }<br>
-                curFrame->m_lowres.<wbr>frameVariance += (rowVariance / maxCol);<br>
             }<br>
-            curFrame->m_lowres.<wbr>frameVariance /= maxRow;<br>
             avg_adj /= blockCount;<br>
             avg_adj_pow2 /= blockCount;<br>
             strength = param->rc.aqStrength * avg_adj;<br>
-            avg_adj = avg_adj - 0.5f * (avg_adj_pow2 - (modeTwoConst)) / avg_adj;<br>
+            avg_adj = avg_adj - 0.5f * (avg_adj_pow2 - modeTwoConst) / avg_adj;<br>
             bias_strength = param->rc.aqStrength;<br>
         }<br>
         else<br>
diff -r b1dfa312234e -r 5bf57563452b source/encoder/slicetype.h<br>
--- a/source/encoder/slicetype.h        Thu Nov 30 10:06:49 2017 +0530<br>
+++ b/source/encoder/slicetype.h        Wed Nov 08 18:09:22 2017 +0530<br>
@@ -82,14 +82,11 @@<br>
     }<br>
<br>
     ~LookaheadTLD() { X265_FREE(wbuffer[0]); }<br>
-<br>
     void calcAdaptiveQuantFrame(Frame *curFrame, x265_param* param);<br>
     void lowresIntraEstimate(Lowres& fenc, uint32_t qgSize);<br>
-<br>
     void weightsAnalyse(Lowres& fenc, Lowres& ref);<br>
-<br>
+       void xPreanalyze(Frame* curFrame);<br>
 protected:<br>
-<br>
     uint32_t acEnergyCu(Frame* curFrame, uint32_t blockX, uint32_t blockY, int csp, uint32_t qgSize);<br>
     uint32_t lumaSumCu(Frame* curFrame, uint32_t blockX, uint32_t blockY, uint32_t qgSize);<br>
     uint32_t weightCostLuma(Lowres& fenc, Lowres& ref, WeightParam& wp);<br>
</blockquote></div><br></div>