]> git.sesse.net Git - x264/blobdiff - encoder/cavlc.c
wrong modulus when delta_qp = +26
[x264] / encoder / cavlc.c
index 11a542ebaa406c0b5f0457966e24d38865853ca8..fc662907bd2890de3fce4d13a663a96d4c79f50f 100644 (file)
@@ -21,7 +21,6 @@
  * Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA  02111, USA.
  *****************************************************************************/
 
-#include <stdlib.h>
 #include <stdio.h>
 #include <string.h>
 
@@ -56,22 +55,6 @@ static const uint8_t sub_mb_type_b_to_golomb[13]=
     10,  4,  5,  1, 11,  6,  7,  2, 12,  8,  9,  3,  0
 };
 
-static const uint8_t block_idx_x[16] =
-{
-    0, 1, 0, 1, 2, 3, 2, 3, 0, 1, 0, 1, 2, 3, 2, 3
-};
-static const uint8_t block_idx_y[16] =
-{
-    0, 0, 1, 1, 0, 0, 1, 1, 2, 2, 3, 3, 2, 2, 3, 3
-};
-static const uint8_t block_idx_xy[4][4] =
-{
-    { 0, 2, 8,  10},
-    { 1, 3, 9,  11},
-    { 4, 6, 12, 14},
-    { 5, 7, 13, 15}
-};
-
 #define BLOCK_INDEX_CHROMA_DC   (-1)
 #define BLOCK_INDEX_LUMA_DC     (-2)
 
@@ -271,6 +254,19 @@ static void block_residual_write_cavlc( x264_t *h, bs_t *s, int i_idx, int *l, i
     }
 }
 
+static void cavlc_qp_delta( x264_t *h, bs_t *s )
+{
+    int i_dqp = h->mb.i_qp - h->mb.i_last_qp;
+    if( i_dqp )
+    {
+        if( i_dqp < -26 )
+            i_dqp += 52;
+        else if( i_dqp > 25 )
+            i_dqp -= 52;
+    }
+    bs_write_se( s, i_dqp );
+}
+
 static void x264_sub_mb_mv_write_cavlc( x264_t *h, bs_t *s, int i_list )
 {
     int i;
@@ -337,17 +333,43 @@ static void x264_sub_mb_mv_write_cavlc( x264_t *h, bs_t *s, int i_list )
     }
 }
 
+static void x264_macroblock_luma_write_cavlc( x264_t *h, bs_t *s )
+{
+    int i8, i4, i;
+    if( h->mb.b_transform_8x8 )
+    {
+        /* shuffle 8x8 dct coeffs into 4x4 lists */
+        for( i8 = 0; i8 < 4; i8++ )
+            if( h->mb.i_cbp_luma & (1 << i8) )
+                for( i4 = 0; i4 < 4; i4++ )
+                {
+                    for( i = 0; i < 16; i++ )
+                        h->dct.block[i4+i8*4].luma4x4[i] = h->dct.luma8x8[i8][i4+i*4];
+                    h->mb.cache.non_zero_count[x264_scan8[i4+i8*4]] =
+                        array_non_zero_count( h->dct.block[i4+i8*4].luma4x4, 16 );
+                }
+    }
+
+    for( i8 = 0; i8 < 4; i8++ )
+        if( h->mb.i_cbp_luma & (1 << i8) )
+            for( i4 = 0; i4 < 4; i4++ )
+                block_residual_write_cavlc( h, s, i4+i8*4, h->dct.block[i4+i8*4].luma4x4, 16 );
+}
+
 /*****************************************************************************
  * x264_macroblock_write:
  *****************************************************************************/
 void x264_macroblock_write_cavlc( x264_t *h, bs_t *s )
 {
     const int i_mb_type = h->mb.i_type;
-    const int i_mb_pos_start = bs_pos( s );
-    int       i_mb_pos_tex;
     int i_mb_i_offset;
     int i;
 
+#ifndef RDO_SKIP_BS
+    const int i_mb_pos_start = bs_pos( s );
+    int       i_mb_pos_tex;
+#endif
+
     switch( h->sh.i_type )
     {
         case SLICE_TYPE_I:
@@ -373,6 +395,9 @@ void x264_macroblock_write_cavlc( x264_t *h, bs_t *s )
         /* Untested */
         bs_write_ue( s, i_mb_i_offset + 25 );
 
+#ifdef RDO_SKIP_BS
+        s->i_bits_encoded += 384*8;
+#else
         bs_align_0( s );
         /* Luma */
         for( i = 0; i < 16*16; i++ )
@@ -395,6 +420,7 @@ void x264_macroblock_write_cavlc( x264_t *h, bs_t *s )
             const int y = 8 * h->mb.i_mb_y + (i / 8);
             bs_write( s, 8, h->fenc->plane[2][y*h->mb.pic.i_stride[2]+x] );
         }
+#endif
         return;
     }
     else if( i_mb_type == I_4x4 || i_mb_type == I_8x8 )
@@ -408,7 +434,7 @@ void x264_macroblock_write_cavlc( x264_t *h, bs_t *s )
         for( i = 0; i < 16; i += di )
         {
             int i_pred = x264_mb_predict_intra4x4_mode( h, i );
-            int i_mode = h->mb.cache.intra4x4_pred_mode[x264_scan8[i]];
+            int i_mode = x264_mb_pred_mode4x4_fix( h->mb.cache.intra4x4_pred_mode[x264_scan8[i]] );
 
             if( i_pred == i_mode)
             {
@@ -427,13 +453,13 @@ void x264_macroblock_write_cavlc( x264_t *h, bs_t *s )
                 }
             }
         }
-        bs_write_ue( s, h->mb.i_chroma_pred_mode );
+        bs_write_ue( s, x264_mb_pred_mode8x8c_fix[ h->mb.i_chroma_pred_mode ] );
     }
     else if( i_mb_type == I_16x16 )
     {
-        bs_write_ue( s, i_mb_i_offset + 1 + h->mb.i_intra16x16_pred_mode +
+        bs_write_ue( s, i_mb_i_offset + 1 + x264_mb_pred_mode16x16_fix[h->mb.i_intra16x16_pred_mode] +
                         h->mb.i_cbp_chroma * 4 + ( h->mb.i_cbp_luma == 0 ? 0 : 12 ) );
-        bs_write_ue( s, h->mb.i_chroma_pred_mode );
+        bs_write_ue( s, x264_mb_pred_mode8x8c_fix[ h->mb.i_chroma_pred_mode ] );
     }
     else if( i_mb_type == P_L0 )
     {
@@ -639,8 +665,10 @@ void x264_macroblock_write_cavlc( x264_t *h, bs_t *s )
         return;
     }
 
+#ifndef RDO_SKIP_BS
     i_mb_pos_tex = bs_pos( s );
     h->stat.frame.i_hdr_bits += i_mb_pos_tex - i_mb_pos_start;
+#endif
 
     /* Coded block patern */
     if( i_mb_type == I_4x4 || i_mb_type == I_8x8 )
@@ -653,8 +681,7 @@ void x264_macroblock_write_cavlc( x264_t *h, bs_t *s )
     }
 
     /* transform size 8x8 flag */
-    if( h->pps->b_transform_8x8_mode && h->mb.i_cbp_luma && !IS_INTRA(i_mb_type)
-        && x264_mb_transform_8x8_allowed( h, i_mb_type ) )
+    if( h->mb.cache.b_transform_8x8_allowed && h->mb.i_cbp_luma && !IS_INTRA(i_mb_type) )
     {
         bs_write1( s, h->mb.b_transform_8x8 );
     }
@@ -662,44 +689,20 @@ void x264_macroblock_write_cavlc( x264_t *h, bs_t *s )
     /* write residual */
     if( i_mb_type == I_16x16 )
     {
-        bs_write_se( s, h->mb.qp[h->mb.i_mb_xy] - h->mb.i_last_qp );
+        cavlc_qp_delta( h, s );
 
         /* DC Luma */
         block_residual_write_cavlc( h, s, BLOCK_INDEX_LUMA_DC , h->dct.luma16x16_dc, 16 );
 
+        /* AC Luma */
         if( h->mb.i_cbp_luma != 0 )
-        {
-            /* AC Luma */
             for( i = 0; i < 16; i++ )
-            {
                 block_residual_write_cavlc( h, s, i, h->dct.block[i].residual_ac, 15 );
-            }
-        }
     }
     else if( h->mb.i_cbp_luma != 0 || h->mb.i_cbp_chroma != 0 )
     {
-        bs_write_se( s, h->mb.qp[h->mb.i_mb_xy] - h->mb.i_last_qp );
-
-        /* shuffle 8x8 dct coeffs into 4x4 lists */
-        if( h->mb.b_transform_8x8 )
-        {
-            int i4;
-            for( i4 = 0; i4 < 16; i4++ )
-            {
-                for( i = 0; i < 16; i++ )
-                    h->dct.block[i4].luma4x4[i] = h->dct.luma8x8[i4>>2][(i4&3)+i*4];
-                h->mb.cache.non_zero_count[x264_scan8[i4]] =
-                    array_non_zero_count( h->dct.block[i4].luma4x4, 16 );
-            }
-        }
-
-        for( i = 0; i < 16; i++ )
-        {
-            if( h->mb.i_cbp_luma & ( 1 << ( i / 4 ) ) )
-            {
-                block_residual_write_cavlc( h, s, i, h->dct.block[i].luma4x4, 16 );
-            }
-        }
+        cavlc_qp_delta( h, s );
+        x264_macroblock_luma_write_cavlc( h, s );
     }
     if( h->mb.i_cbp_chroma != 0 )
     {
@@ -707,16 +710,14 @@ void x264_macroblock_write_cavlc( x264_t *h, bs_t *s )
         block_residual_write_cavlc( h, s, BLOCK_INDEX_CHROMA_DC, h->dct.chroma_dc[0], 4 );
         block_residual_write_cavlc( h, s, BLOCK_INDEX_CHROMA_DC, h->dct.chroma_dc[1], 4 );
         if( h->mb.i_cbp_chroma&0x02 ) /* Chroma AC residual present */
-        {
             for( i = 0; i < 8; i++ )
-            {
                 block_residual_write_cavlc( h, s, 16 + i, h->dct.block[16+i].residual_ac, 15 );
-            }
-        }
     }
 
+#ifndef RDO_SKIP_BS
     if( IS_INTRA( i_mb_type ) )
         h->stat.frame.i_itex_bits += bs_pos(s) - i_mb_pos_tex;
     else
         h->stat.frame.i_ptex_bits += bs_pos(s) - i_mb_pos_tex;
+#endif
 }