[x265] [PATCH 1 of 2] fix for hash mismatch in new weightp

shazeb at multicorewareinc.com shazeb at multicorewareinc.com
Thu Jan 9 14:30:59 CET 2014


# HG changeset patch
# User Shazeb Nawaz Khan <shazeb at multicorewareinc.com>
# Date 1389269893 -19800
#      Thu Jan 09 17:48:13 2014 +0530
# Branch stable
# Node ID dee13ce46375aded150ec7a6746643d14e7a3416
# Parent  0d70188e80bcc01afc36b65cd5dd3666e92fbf5e
fix for hash mismatch in new weightp

diff -r 0d70188e80bc -r dee13ce46375 source/Lib/TLibCommon/TComSlice.h
--- a/source/Lib/TLibCommon/TComSlice.h	Wed Jan 08 13:00:53 2014 -0600
+++ b/source/Lib/TLibCommon/TComSlice.h	Thu Jan 09 17:48:13 2014 +0530
@@ -1260,10 +1260,10 @@
     int         w, o, offset, shift, round;
 
     /* makes a non-h265 weight (i.e. fix7), into an h265 weight */
-    void setFromWeightAndOffset(int weight, int _offset)
+    void setFromWeightAndOffset(int weight, int _offset, int denom = 7)
     {
         inputOffset = _offset;
-        log2WeightDenom = 7;
+        log2WeightDenom = denom;
         inputWeight = weight;
         while (log2WeightDenom > 0 && (inputWeight > 127))
         {
@@ -1557,7 +1557,7 @@
     void  getWpAcDcParam(wpACDCParam *&wp);
     void  initWpAcDcParam();
 
-    void setTileOffstForMultES(uint32_t offset){ m_tileOffstForMultES = offset; }
+    void setTileOffstForMultES(uint32_t offset) { m_tileOffstForMultES = offset; }
 
     uint32_t getTileOffstForMultES()           { return m_tileOffstForMultES; }
 
diff -r 0d70188e80bc -r dee13ce46375 source/encoder/weightPrediction.cpp
--- a/source/encoder/weightPrediction.cpp	Wed Jan 08 13:00:53 2014 -0600
+++ b/source/encoder/weightPrediction.cpp	Thu Jan 09 17:48:13 2014 +0530
@@ -29,6 +29,10 @@
 
 using namespace x265;
 
+/** clip a, such that minVal <= a <= maxVal */
+//template<typename T>
+//inline T Clip3(T minVal, T maxVal, T a) { return std::min<T>(std::max<T>(minVal, a), maxVal); } ///< general min/max clip
+
 void WeightPrediction::mcChroma()
 {
     intptr_t strd = m_refStride;
@@ -142,6 +146,27 @@
 
 void WeightPrediction::weightAnalyseEnc()
 {
+    int denom = 6;
+    bool validRangeFlag = false;
+
+    if (m_slice->getNumRefIdx(REF_PIC_LIST_0) > 3)
+    {
+        denom  = 7;
+    }
+
+    do
+    {
+        validRangeFlag = checkDenom(denom);
+        if (!validRangeFlag)
+        {
+            denom--; // decrement to satisfy the range limitation
+        }
+    }
+    while ((validRangeFlag == false) && (denom > 0));
+}
+
+bool WeightPrediction::checkDenom(int denom)
+{
     wpScalingParam w, *fw;
     Lowres *fenc, *ref;
     int numPredDir = m_slice->isInterP() ? 1 : 2;
@@ -180,6 +205,7 @@
                 float fencVar = (float)fenc->wp_ssd[yuv] + !ref->wp_ssd[yuv];
                 float refVar  = (float)ref->wp_ssd[yuv] + !ref->wp_ssd[yuv];
                 guessScale[yuv] = sqrtf((float)fencVar / refVar);
+                guessScale[yuv] = Clip3(-2.f, 1.8f, sqrtf((float)fencVar / refVar));
                 fencMean[yuv] = (float)fenc->wp_sum[yuv] / (height[yuv] * width[yuv]) / (1 << (X265_DEPTH - 8));
                 refMean[yuv]  = (float)ref->wp_sum[yuv] / (height[yuv] * width[yuv]) / (1 << (X265_DEPTH - 8));
             }
@@ -188,6 +214,7 @@
             {
                 int ic = 0;
                 SET_WEIGHT(w, 0, 1, 0, 0);
+                SET_WEIGHT(fw[yuv], 0, 1 << denom, denom, 0);
                 /* Early termination */
                 if (fabsf(refMean[yuv] - fencMean[yuv]) < 0.5f && fabsf(1.f - guessScale[yuv]) < epsilon)
                     continue;
@@ -209,19 +236,7 @@
                 unsigned int minscore = 0, origscore = 1;
                 int found = 0;
 
-                if (yuv)
-                {
-                    w.log2WeightDenom = chromaDenom;
-                    w.inputWeight = Clip3(0, 255, (int)guessScale[yuv] * (1 << w.log2WeightDenom));
-                    if (w.inputWeight > 127)
-                    {
-                        SET_WEIGHT(fw[1], 0, 64, 6, 0);
-                        SET_WEIGHT(fw[2], 0, 64, 6, 0);
-                        break;
-                    }
-                }
-                else
-                    w.setFromWeightAndOffset((int)(guessScale[yuv] * 128 + 0.5), 0);
+                w.setFromWeightAndOffset((int)(guessScale[yuv] * (1 << denom) + 0.5), 0, denom);
 
                 if (!yuv) lumaDenom = w.log2WeightDenom;
                 mindenom = w.log2WeightDenom;
@@ -329,7 +344,10 @@
                 }
 
                 if (!found || (minscale == 1 << mindenom && minoff == 0) || (float)minscore / origscore > 0.998f)
+                {
+                    SET_WEIGHT(fw[yuv], 0, 1 << mindenom, mindenom, 0);
                     continue;
+                }
                 else
                 {
                     SET_WEIGHT(w, 1, minscale, mindenom, minoff);
@@ -341,23 +359,13 @@
 
             if (check)
             {
-                numWeighted++;
-                if (fw[0].log2WeightDenom == 7)
+                if (fw[1].bPresentFlag || fw[2].bPresentFlag)
                 {
-                    fw[0].inputWeight >>= 1;
-                    fw[0].log2WeightDenom--;
+                    // Enabling in both chroma
+                    fw[1].bPresentFlag = true;
+                    fw[2].bPresentFlag = true;
                 }
 
-                int maxdenom = X265_MAX(fw[0].log2WeightDenom, X265_MAX(fw[1].log2WeightDenom, fw[2].log2WeightDenom));
-                fw[0].inputWeight <<= (maxdenom - fw[0].log2WeightDenom);
-                fw[0].log2WeightDenom += (maxdenom - fw[0].log2WeightDenom);
-                fw[1].inputWeight <<= (maxdenom - fw[1].log2WeightDenom);
-                fw[1].log2WeightDenom += (maxdenom - fw[1].log2WeightDenom);
-                fw[2].inputWeight <<= (maxdenom - fw[2].log2WeightDenom);
-                fw[2].log2WeightDenom += (maxdenom - fw[2].log2WeightDenom);
-                fw[1].bPresentFlag = true;
-                fw[2].bPresentFlag = true;
-
                 int deltaWeight;
                 bool deltaHigh = false;
                 for (int i = 0; i < 3; i++)
@@ -369,15 +377,23 @@
 
                 if (deltaHigh)
                 {
-                    SET_WEIGHT(fw[0], 0, 64, 6, 0);
-                    SET_WEIGHT(fw[1], 0, 64, 6, 0);
-                    SET_WEIGHT(fw[2], 0, 64, 6, 0);
-                    fullCheck = 0;
+                    // Checking deltaWeight range
+                    SET_WEIGHT(fw[0], 0, 1 << denom, denom, 0);
+                    SET_WEIGHT(fw[1], 0, 1 << denom, denom, 0);
+                    SET_WEIGHT(fw[2], 0, 1 << denom, denom, 0);
+                    fullCheck -= check;
+                    return false;
                 }
             }
         }
     }
 
+    if (!fullCheck)
+    {
+        return false;
+    }
+
     m_slice->setWpScaling(m_wp);
     m_slice->getPPS()->setUseWP((fullCheck > 0) ? true : false);
+    return true;
 }
diff -r 0d70188e80bc -r dee13ce46375 source/encoder/weightPrediction.h
--- a/source/encoder/weightPrediction.h	Wed Jan 08 13:00:53 2014 -0600
+++ b/source/encoder/weightPrediction.h	Thu Jan 09 17:48:13 2014 +0530
@@ -26,7 +26,6 @@
 #include "mv.h"
 
 namespace x265 {
-
 class TComSlice;
 
 class WeightPrediction
@@ -50,7 +49,7 @@
     {
         this->m_slice = slice;
         m_csp = m_slice->getPic()->getPicYuvOrg()->m_picCsp;
-        m_csp444 = (m_csp == X265_CSP_I444) ? 1: 0;
+        m_csp444 = (m_csp == X265_CSP_I444) ? 1 : 0;
         m_blockSize = 8 << m_csp444;
         m_frmHeight = m_slice->getPic()->m_lowres.lines << m_csp444;
         m_frmWidth  = m_slice->getPic()->m_lowres.width << m_csp444;
@@ -60,7 +59,7 @@
 
         m_mcbuf = NULL;
         m_inbuf = NULL;
-        m_buf = (pixel *) X265_MALLOC(pixel, m_frmHeight * m_refStride);
+        m_buf = (pixel*)X265_MALLOC(pixel, m_frmHeight * m_refStride);
 
         int numPredDir = m_slice->isInterP() ? 1 : m_slice->isInterB() ? 2 : 0;
         for (int list = 0; list < numPredDir; list++)
@@ -82,7 +81,7 @@
 
     void mcChroma();
     void weightAnalyseEnc();
+    bool checkDenom(int denom);
     uint32_t weightCost(pixel *cur, pixel *ref, wpScalingParam *w);
-
 };
-};
+}


More information about the x265-devel mailing list