[x264-devel] commit: Faster chroma encoding (Jason Garrett-Glaser )

git version control git at videolan.org
Mon Nov 10 02:44:48 CET 2008


x264 | branch: master | Jason Garrett-Glaser <darkshikari at gmail.com> | Sun Nov  9 17:39:21 2008 -0800| [2652abeae5445ffefded5ee7d0853300d0973b37] | committer: Jason Garrett-Glaser 

Faster chroma encoding
9-12% faster chroma encode.
Move all functions for handling chroma DC that don't have assembly versions to macroblock.c and inline them, along with a few other tweaks.

> http://git.videolan.org/gitweb.cgi/x264.git/?a=commit;h=2652abeae5445ffefded5ee7d0853300d0973b37
---

 common/dct.c         |   18 --------------
 common/dct.h         |    3 --
 common/quant.c       |   23 -----------------
 common/quant.h       |    1 -
 encoder/macroblock.c |   65 ++++++++++++++++++++++++++++++++++---------------
 5 files changed, 45 insertions(+), 65 deletions(-)

diff --git a/common/dct.c b/common/dct.c
index b05f604..1b52547 100644
--- a/common/dct.c
+++ b/common/dct.c
@@ -36,21 +36,6 @@ int x264_dct8_weight2_zigzag[2][64];
  * XXX For all dct dc : input could be equal to output so ...
  */
 
-static void dct2x2dc( int16_t d[2][2] )
-{
-    int tmp[2][2];
-
-    tmp[0][0] = d[0][0] + d[0][1];
-    tmp[1][0] = d[0][0] - d[0][1];
-    tmp[0][1] = d[1][0] + d[1][1];
-    tmp[1][1] = d[1][0] - d[1][1];
-
-    d[0][0] = tmp[0][0] + tmp[0][1];
-    d[1][0] = tmp[1][0] + tmp[1][1];
-    d[0][1] = tmp[0][0] - tmp[0][1];
-    d[1][1] = tmp[1][0] - tmp[1][1];
-}
-
 static void dct4x4dc( int16_t d[4][4] )
 {
     int16_t tmp[4][4];
@@ -387,9 +372,6 @@ void x264_dct_init( int cpu, x264_dct_function_t *dctf )
     dctf->dct4x4dc  = dct4x4dc;
     dctf->idct4x4dc = idct4x4dc;
 
-    dctf->dct2x2dc  = dct2x2dc;
-    dctf->idct2x2dc = dct2x2dc;
-
 #ifdef HAVE_MMX
     if( cpu&X264_CPU_MMX )
     {
diff --git a/common/dct.h b/common/dct.h
index 3e26175..6d78bc7 100644
--- a/common/dct.h
+++ b/common/dct.h
@@ -109,9 +109,6 @@ typedef struct
     void (*dct4x4dc) ( int16_t d[4][4] );
     void (*idct4x4dc)( int16_t d[4][4] );
 
-    void (*dct2x2dc) ( int16_t d[2][2] );
-    void (*idct2x2dc)( int16_t d[2][2] );
-
 } x264_dct_function_t;
 
 typedef struct
diff --git a/common/quant.c b/common/quant.c
index d7c8c30..348274e 100644
--- a/common/quant.c
+++ b/common/quant.c
@@ -139,29 +139,6 @@ static void dequant_8x8( int16_t dct[8][8], int dequant_mf[6][8][8], int i_qp )
     }
 }
 
-void x264_mb_dequant_2x2_dc( int16_t dct[2][2], int dequant_mf[6][4][4], int i_qp )
-{
-    const int i_qbits = i_qp/6 - 5;
-
-    if( i_qbits >= 0 )
-    {
-        const int i_dmf = dequant_mf[i_qp%6][0][0] << i_qbits;
-        dct[0][0] *= i_dmf;
-        dct[0][1] *= i_dmf;
-        dct[1][0] *= i_dmf;
-        dct[1][1] *= i_dmf;
-    }
-    else
-    {
-        const int i_dmf = dequant_mf[i_qp%6][0][0];
-        // chroma DC is truncated, not rounded
-        dct[0][0] = ( dct[0][0] * i_dmf ) >> (-i_qbits);
-        dct[0][1] = ( dct[0][1] * i_dmf ) >> (-i_qbits);
-        dct[1][0] = ( dct[1][0] * i_dmf ) >> (-i_qbits);
-        dct[1][1] = ( dct[1][1] * i_dmf ) >> (-i_qbits);
-    }
-}
-
 void x264_mb_dequant_4x4_dc( int16_t dct[4][4], int dequant_mf[6][4][4], int i_qp )
 {
     const int i_qbits = i_qp/6 - 6;
diff --git a/common/quant.h b/common/quant.h
index 986d9f5..1024b4f 100644
--- a/common/quant.h
+++ b/common/quant.h
@@ -43,6 +43,5 @@ typedef struct
 void x264_quant_init( x264_t *h, int cpu, x264_quant_function_t *pf );
 
 void x264_mb_dequant_4x4_dc( int16_t dct[4][4], int dequant_mf[6][4][4], int i_qscale );
-void x264_mb_dequant_2x2_dc( int16_t dct[2][2], int dequant_mf[6][4][4], int i_qscale );
 
 #endif
diff --git a/encoder/macroblock.c b/encoder/macroblock.c
index eddf1dd..68ce833 100644
--- a/encoder/macroblock.c
+++ b/encoder/macroblock.c
@@ -25,6 +25,8 @@
 #include "common/common.h"
 #include "macroblock.h"
 
+/* These chroma DC functions don't have assembly versions and are only used here. */
+
 #define ZIG(i,y,x) level[i] = dct[x][y];
 static inline void zigzag_scan_2x2_dc( int16_t level[4], int16_t dct[2][2] )
 {
@@ -35,6 +37,41 @@ static inline void zigzag_scan_2x2_dc( int16_t level[4], int16_t dct[2][2] )
 }
 #undef ZIG
 
+static inline void idct_dequant_2x2_dc( int16_t dct[2][2], int16_t dct4x4[4][4][4], int dequant_mf[6][4][4], int i_qp )
+{
+    int d0 = dct[0][0] + dct[0][1];
+    int d1 = dct[1][0] + dct[1][1];
+    int d2 = dct[0][0] - dct[0][1];
+    int d3 = dct[1][0] - dct[1][1];
+    int dmf = dequant_mf[i_qp%6][0][0];
+    int qbits = i_qp/6 - 5;
+    if( qbits > 0 )
+    {
+        dmf <<= qbits;
+        qbits = 0;
+    }
+    dct4x4[0][0][0] = (d0 + d1) * dmf >> -qbits;
+    dct4x4[1][0][0] = (d0 - d1) * dmf >> -qbits;
+    dct4x4[2][0][0] = (d2 + d3) * dmf >> -qbits;
+    dct4x4[3][0][0] = (d2 - d3) * dmf >> -qbits;
+}
+
+static inline void dct2x2dc( int16_t d[2][2], int16_t dct4x4[4][4][4] )
+{
+    int d0 = dct4x4[0][0][0] + dct4x4[1][0][0];
+    int d1 = dct4x4[2][0][0] + dct4x4[3][0][0];
+    int d2 = dct4x4[0][0][0] - dct4x4[1][0][0];
+    int d3 = dct4x4[2][0][0] - dct4x4[3][0][0];
+    d[0][0] = d0 + d1;
+    d[1][0] = d2 + d3;
+    d[0][1] = d0 - d1;
+    d[1][1] = d2 - d3;
+    dct4x4[0][0][0] = 0;
+    dct4x4[1][0][0] = 0;
+    dct4x4[2][0][0] = 0;
+    dct4x4[3][0][0] = 0;
+}
+
 static ALWAYS_INLINE void x264_quant_4x4( x264_t *h, int16_t dct[4][4], int i_qp, int i_ctxBlockCat, int b_intra, int idx )
 {
     int i_quant_cat = b_intra ? CQM_4IY : CQM_4PY;
@@ -191,13 +228,10 @@ void x264_mb_encode_8x8_chroma( x264_t *h, int b_inter, int i_qp )
         }
 
         h->dctf.sub8x8_dct( dct4x4, p_src, p_dst );
+        dct2x2dc( dct2x2, dct4x4 );
         /* calculate dct coeffs */
         for( i = 0; i < 4; i++ )
         {
-            /* copy dc coeff */
-            dct2x2[i>>1][i&1] = dct4x4[i][0][0];
-            dct4x4[i][0][0] = 0;
-
             if( h->mb.b_trellis )
                 x264_quant_4x4_trellis( h, dct4x4[i], CQM_4IC+b_inter, i_qp, DCT_CHROMA_AC, !b_inter, 0 );
             else
@@ -208,23 +242,20 @@ void x264_mb_encode_8x8_chroma( x264_t *h, int b_inter, int i_qp )
                 i_decimate_score += h->quantf.decimate_score15( h->dct.luma4x4[16+i+ch*4] );
         }
 
-        h->dctf.dct2x2dc( dct2x2 );
         if( h->mb.b_trellis )
             x264_quant_dc_trellis( h, (int16_t*)dct2x2, CQM_4IC+b_inter, i_qp, DCT_CHROMA_DC, !b_inter );
         else
             h->quantf.quant_2x2_dc( dct2x2, h->quant4_mf[CQM_4IC+b_inter][i_qp][0]>>1, h->quant4_bias[CQM_4IC+b_inter][i_qp][0]<<1 );
-        zigzag_scan_2x2_dc( h->dct.chroma_dc[ch], dct2x2 );
-
-        /* output samples to fdec */
-        h->dctf.idct2x2dc( dct2x2 );
-        x264_mb_dequant_2x2_dc( dct2x2, h->dequant4_mf[CQM_4IC + b_inter], i_qp );  /* XXX not inversed */
 
         if( b_decimate && i_decimate_score < 7 )
         {
             /* Near null chroma 8x8 block so make it null (bits saving) */
             memset( &h->dct.luma4x4[16+ch*4], 0, 4 * sizeof( *h->dct.luma4x4 ) );
             if( !array_non_zero( dct2x2 ) )
+            {
+                memset( h->dct.chroma_dc[ch], 0, sizeof( h->dct.chroma_dc[ch] ) );
                 continue;
+            }
             memset( dct4x4, 0, sizeof( dct4x4 ) );
         }
         else
@@ -232,10 +263,9 @@ void x264_mb_encode_8x8_chroma( x264_t *h, int b_inter, int i_qp )
             for( i = 0; i < 4; i++ )
                 h->quantf.dequant_4x4( dct4x4[i], h->dequant4_mf[CQM_4IC + b_inter], i_qp );
         }
-        dct4x4[0][0][0] = dct2x2[0][0];
-        dct4x4[1][0][0] = dct2x2[0][1];
-        dct4x4[2][0][0] = dct2x2[1][0];
-        dct4x4[3][0][0] = dct2x2[1][1];
+
+        zigzag_scan_2x2_dc( h->dct.chroma_dc[ch], dct2x2 );
+        idct_dequant_2x2_dc( dct2x2, dct4x4, h->dequant4_mf[CQM_4IC + b_inter], i_qp );
         h->dctf.add8x8_idct( p_dst, dct4x4 );
     }
 
@@ -748,11 +778,7 @@ int x264_macroblock_probe_skip( x264_t *h, int b_bidir )
         h->dctf.sub8x8_dct( dct4x4, p_src, p_dst );
 
         /* calculate dct DC */
-        dct2x2[0][0] = dct4x4[0][0][0];
-        dct2x2[0][1] = dct4x4[1][0][0];
-        dct2x2[1][0] = dct4x4[2][0][0];
-        dct2x2[1][1] = dct4x4[3][0][0];
-        h->dctf.dct2x2dc( dct2x2 );
+        dct2x2dc( dct2x2, dct4x4 );
         h->quantf.quant_2x2_dc( dct2x2, h->quant4_mf[CQM_4PC][i_qp][0]>>1, h->quant4_bias[CQM_4PC][i_qp][0]<<1 );
         if( array_non_zero(dct2x2) )
             return 0;
@@ -760,7 +786,6 @@ int x264_macroblock_probe_skip( x264_t *h, int b_bidir )
         /* calculate dct coeffs */
         for( i4x4 = 0, i_decimate_mb = 0; i4x4 < 4; i4x4++ )
         {
-            dct4x4[i4x4][0][0] = 0;
             h->quantf.quant_4x4( dct4x4[i4x4], h->quant4_mf[CQM_4PC][i_qp], h->quant4_bias[CQM_4PC][i_qp] );
             if( !array_non_zero(dct4x4[i4x4]) )
                 continue;



More information about the x264-devel mailing list