From c8ef79d22eed3e9c8d67807df5b891a5e83dba17 Mon Sep 17 00:00:00 2001
From: Yaowu Xu <yaowu@google.com>
Date: Tue, 25 Oct 2011 10:25:02 -0700
Subject: [PATCH] Change to prevent encoding of effect-less 2nd order
 coefficients

similar logic to http://gerrit.chromium.org/gerrit/#change,10359

Change-Id: Ia12acd2f2b3b92ef2a601da43c2497034ef62174
---
 vp8/encoder/encodemb.c | 91 +++++++++++++++++++++++++++++++++---------
 1 file changed, 73 insertions(+), 18 deletions(-)

diff --git a/vp8/encoder/encodemb.c b/vp8/encoder/encodemb.c
index c0e0bc4fdc..73134425c7 100644
--- a/vp8/encoder/encodemb.c
+++ b/vp8/encoder/encodemb.c
@@ -606,6 +606,73 @@ static void optimize_b(MACROBLOCK *mb, int ib, int type,
     *a = *l = (d->eob != !type);
 }
 
+static void check_reset_2nd_coeffs(MACROBLOCKD *x)
+{
+    int sum=0;
+    int i;
+    BLOCKD *bd = &x->block[24];
+    int eob_pos = (bd->eob<16)? vp8_default_zig_zag1d[bd->eob]:16;
+
+    for(i=0;i<bd->eob;i++)
+    {
+        int coef = bd->dqcoeff[vp8_default_zig_zag1d[i]];
+        sum+= (coef>=0)?coef:-coef;
+    }
+    /**************************************************************************
+    our inverse hadamard transform effectively is weighted sum of all 16 inputs
+    with weight either 1 or -1. It has a last stage scaling of (sum+3)>>3. And
+    dc only idct is (dc+4)>>3. So if all the sums are between -35 and 29, the
+    output after inverse wht and idct will be all zero. A sum of absolute value
+    smaller than 35 guarantees all 16 different (+1/-1) weighted sums in wht
+    fall between -35 and +35.
+    **************************************************************************/
+    if(sum < 35)
+    {
+        vpx_memset(bd->qcoeff,0,eob_pos*2);
+        vpx_memset(bd->dqcoeff,0,eob_pos*2);
+        bd->eob = 0;
+    }
+}
+static void check_reset_8x8_2nd_coeffs(MACROBLOCKD *x)
+{
+    int sum=0;
+    int i;
+    BLOCKD *bd = &x->block[24];
+    int coef;
+
+    coef = bd->dqcoeff[0];
+    sum+= (coef>=0)?coef:-coef;
+    coef = bd->dqcoeff[1];
+    sum+= (coef>=0)?coef:-coef;
+    coef = bd->dqcoeff[4];
+    sum+= (coef>=0)?coef:-coef;
+    coef = bd->dqcoeff[8];
+    sum+= (coef>=0)?coef:-coef;
+
+    /**************************************************************************
+    our inverse haar transform effectively is weighted sum of all 4 inputs
+    with weight either 1 or -1. It has a last stage scaling of (sum)>>2. And
+    dc only idct8x8 is effectively (dc+8)>>4. So if all the sums are between
+    -31 and 31, outputs after inverse txfm be all 0s. A sum of absolute value
+    smaller than 32 guarantees all 4 different (+1/-1) weighted sums in wht
+    fall between -31 and +31.
+    **************************************************************************/
+    if(sum < 32)
+    {
+        bd->qcoeff[0] = 0;
+        bd->dqcoeff[0] = 0;
+        bd->qcoeff[1] = 0;
+        bd->dqcoeff[1] = 0;
+        bd->qcoeff[4] = 0;
+        bd->dqcoeff[4] = 0;
+        bd->qcoeff[8] = 0;
+        bd->dqcoeff[8] = 0;
+        bd->eob = 0;
+    }
+}
+
+
+
 static void optimize_mb(MACROBLOCK *x, const VP8_ENCODER_RTCD *rtcd)
 {
     int b;
@@ -642,6 +709,7 @@ static void optimize_mb(MACROBLOCK *x, const VP8_ENCODER_RTCD *rtcd)
         b=24;
         optimize_b(x, b, PLANE_TYPE_Y2,
             ta + vp8_block2above[b], tl + vp8_block2left[b], rtcd);
+        check_reset_2nd_coeffs(&x->e_mbd);
     }
 }
 
@@ -684,6 +752,7 @@ void vp8_optimize_mby(MACROBLOCK *x, const VP8_ENCODER_RTCD *rtcd)
         b=24;
         optimize_b(x, b, PLANE_TYPE_Y2,
             ta + vp8_block2above[b], tl + vp8_block2left[b], rtcd);
+        check_reset_2nd_coeffs(&x->e_mbd);
     }
 }
 
@@ -1040,16 +1109,8 @@ void optimize_mb_8x8(MACROBLOCK *x, const VP8_ENCODER_RTCD *rtcd)
             *(tl + vp8_block2left[b]);
 
     }
-
-
-    /*
-    if (has_2nd_order)
-    {
-        vp8_setup_temp_context(&t, x->e_mbd.above_context[Y2CONTEXT],
-            x->e_mbd.left_context[Y2CONTEXT], 1);
-        optimize_b(x, 24, 1, t.a, t.l, rtcd);
-    }
-    */
+    //8x8 always have 2nd roder haar block
+    check_reset_8x8_2nd_coeffs(&x->e_mbd);
 }
 
 void vp8_optimize_mby_8x8(MACROBLOCK *x, const VP8_ENCODER_RTCD *rtcd)
@@ -1114,14 +1175,8 @@ void vp8_optimize_mby_8x8(MACROBLOCK *x, const VP8_ENCODER_RTCD *rtcd)
 
     }
 
-    /*
-    if (has_2nd_order)
-    {
-        vp8_setup_temp_context(&t, x->e_mbd.above_context[Y2CONTEXT],
-            x->e_mbd.left_context[Y2CONTEXT], 1);
-        optimize_b(x, 24, 1, t.a, t.l, rtcd);
-    }
-    */
+    //8x8 always have 2nd roder haar block
+    check_reset_8x8_2nd_coeffs(&x->e_mbd);
 }
 
 void vp8_optimize_mbuv_8x8(MACROBLOCK *x, const VP8_ENCODER_RTCD *rtcd)
-- 
GitLab