[x265] [PATCH 2 of 2] Using weighted lowres ref frames in cost estimation in lookahead

shazeb at multicorewareinc.com shazeb at multicorewareinc.com
Thu Nov 14 14:11:55 CET 2013


# HG changeset patch
# User Shazeb Nawaz Khan <shazeb at multicorewareinc.com>
# Date 1384434393 -19800
#      Thu Nov 14 18:36:33 2013 +0530
# Node ID bd5fc8839d367cc62e178521def61596ada185b2
# Parent  dea83349f7865ee0c8a8b0572e9dbf771993fc14
Using weighted lowres ref frames in cost estimation in lookahead

diff -r dea83349f786 -r bd5fc8839d36 source/common/lowres.cpp
--- a/source/common/lowres.cpp	Thu Nov 14 18:04:11 2013 +0530
+++ b/source/common/lowres.cpp	Thu Nov 14 18:36:33 2013 +0530
@@ -40,6 +40,7 @@
     int cuWidth = (width + X265_LOWRES_CU_SIZE - 1) >> X265_LOWRES_CU_BITS;
     int cuHeight = (lines + X265_LOWRES_CU_SIZE - 1) >> X265_LOWRES_CU_BITS;
     int cuCount = cuWidth * cuHeight;
+    extHeight = lines + 2 * orig->getLumaMarginY();
 
     /* rounding the width to multiple of lowres CU size */
     width = cuWidth * X265_LOWRES_CU_SIZE;
@@ -115,6 +116,19 @@
     X265_FREE(invQscaleFactor);
 }
 
+void Lowres::initWeighted(Lowres *ref, wpScalingParam *w)
+{
+    isWeighted = true;
+    int correction = (IF_INTERNAL_PREC - X265_DEPTH);
+    for (int i = 0; i < 4; i++)
+    {
+        // Adding (IF_INTERNAL_PREC - X265_DEPTH) to cancel effect of pixel to short conversion inside the primitive
+        primitives.weightpUniPixel(ref->buffer[i], this->buffer[i], lumaStride, lumaStride, lumaStride, extHeight, w->inputWeight, (1 << (w->log2WeightDenom - 1 + correction)), (w->log2WeightDenom + correction), w->inputOffset);
+    }
+
+    fpelPlane = lowresPlane[0];
+}
+
 // (re) initialize lowres state
 void Lowres::init(TComPicYuv *orig, int poc, int type, int bframes)
 {
diff -r dea83349f786 -r bd5fc8839d36 source/common/lowres.h
--- a/source/common/lowres.h	Thu Nov 14 18:04:11 2013 +0530
+++ b/source/common/lowres.h	Thu Nov 14 18:36:33 2013 +0530
@@ -33,6 +33,7 @@
 
 class TComPic;
 class TComPicYuv;
+typedef struct WpScalingParam wpScalingParam;
 
 struct ReferencePlanes
 {
@@ -61,7 +62,7 @@
 
             MV qmvB = qmv + MV((qmv.x & 1) * 2, (qmv.y & 1) * 2);
             int hpelB = (qmvB.y & 2) | ((qmvB.x & 2) >> 1);
-            
+
             pixel *frefB = lowresPlane[hpelB] + blockOffset + (qmvB.x >> 2) + (qmvB.y >> 2) * lumaStride;
             primitives.pixelavg_pp[LUMA_8x8](buf, outstride, frefA, lumaStride, frefB, lumaStride, 32);
             return buf;
@@ -100,6 +101,7 @@
 struct Lowres : public ReferencePlanes
 {
     pixel *buffer[4];
+    int extHeight;
 
     int    frameNum;         // Presentation frame number
     int    sliceType;        // Slice type decided by lookahead
@@ -132,6 +134,7 @@
     void create(TComPic *pic, int bframes, int32_t *aqMode);
     void destroy(int bframes);
     void init(TComPicYuv *orig, int poc, int sliceType, int bframes);
+    void initWeighted(Lowres *ref, wpScalingParam *w);
 };
 }
 
diff -r dea83349f786 -r bd5fc8839d36 source/encoder/slicetype.cpp
--- a/source/encoder/slicetype.cpp	Thu Nov 14 18:04:11 2013 +0530
+++ b/source/encoder/slicetype.cpp	Thu Nov 14 18:36:33 2013 +0530
@@ -79,12 +79,15 @@
     widthInCU = ((cfg->param.sourceWidth / 2) + X265_LOWRES_CU_SIZE - 1) >> X265_LOWRES_CU_BITS;
     heightInCU = ((cfg->param.sourceHeight / 2) + X265_LOWRES_CU_SIZE - 1) >> X265_LOWRES_CU_BITS;
 
+    weightedRef.buffer[0] = NULL;
+
     lhrows = new LookaheadRow[heightInCU];
     for (int i = 0; i < heightInCU; i++)
     {
         lhrows[i].widthInCU = widthInCU;
         lhrows[i].heightInCU = heightInCU;
         lhrows[i].frames = frames;
+        lhrows[i].weightedRef = &weightedRef;
     }
 }
 
@@ -127,7 +130,11 @@
 void Lookahead::addPicture(TComPic *pic, int sliceType)
 {
     pic->m_lowres.init(pic->getPicYuvOrg(), pic->getSlice()->getPOC(), sliceType, cfg->param.bframes);
-
+    if (!weightedRef.buffer[0])
+    {
+        // Just using width/height data from the pic to create a standalone Lowres object
+        weightedRef.create(pic, cfg->param.bframes, &cfg->param.rc.aqMode);
+    }
     inputQueue.pushBack(*pic);
     if (inputQueue.size() >= cfg->param.lookaheadDepth)
         slicetypeDecide();
@@ -330,6 +337,7 @@
     else
     {
         SET_WEIGHT(w, 1, minscale, mindenom, minoff);
+        weightedRef.initWeighted(frames[p0], &w);
     }
 }
 
@@ -339,6 +347,7 @@
 {
     int score = 0;
     Lowres *fenc = frames[b];
+    weightedRef.isWeighted = false;
 
     if (fenc->costEst[b - p0][p1 - b] >= 0 && fenc->rowSatds[b - p0][p1 - b][0] != -1)
         score = fenc->costEst[b - p0][p1 - b];
@@ -444,6 +453,11 @@
     Lowres *fref1 = frames[p1];
     Lowres *fenc  = frames[b];
 
+    if (weightedRef->isWeighted)
+    {
+        fref0 = weightedRef;
+    }
+
     const int bBidir = (b < p1);
     const int cuXY = cux + cuy * widthInCU;
     const int cuSize = X265_LOWRES_CU_SIZE;
diff -r dea83349f786 -r bd5fc8839d36 source/encoder/slicetype.h
--- a/source/encoder/slicetype.h	Thu Nov 14 18:04:11 2013 +0530
+++ b/source/encoder/slicetype.h	Thu Nov 14 18:36:33 2013 +0530
@@ -83,6 +83,8 @@
     int              widthInCU;       // width of lowres frame in downscale CUs
     int              heightInCU;      // height of lowres frame in downscale CUs
 
+    Lowres weightedRef;
+
     PicList inputQueue;  // input pictures in order received
     PicList outputQueue; // pictures to be encoded, in encode order
 


More information about the x265-devel mailing list