]> git.sesse.net Git - x264/blobdiff - encoder/cavlc.c
Allow 8x8dct+cavlc+lossless with subme>=6
[x264] / encoder / cavlc.c
index cee6f4e4b65e2127e9bf12c5c4d32a5879c779d6..c5798108ff866a2768002c5af05439c52f00dfe8 100644 (file)
@@ -1,7 +1,7 @@
 /*****************************************************************************
- * cavlc.c: h264 encoder library
+ * cavlc.c: cavlc bitstream writing
  *****************************************************************************
- * Copyright (C) 2003-2008 x264 project
+ * Copyright (C) 2003-2010 x264 project
  *
  * Authors: Laurent Aimar <fenrir@via.ecp.fr>
  *          Loren Merritt <lorenm@u.washington.edu>
@@ -20,6 +20,9 @@
  * You should have received a copy of the GNU General Public License
  * along with this program; if not, write to the Free Software
  * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA  02111, USA.
+ *
+ * This program is also available under a commercial proprietary license.
+ * For more information, contact us at licensing@x264.com.
  *****************************************************************************/
 
 #include "common/common.h"
@@ -66,7 +69,7 @@ static inline int block_residual_write_cavlc_escape( x264_t *h, int i_suffix_len
     bs_t *s = &h->out.bs;
     static const uint16_t next_suffix[7] = { 0, 3, 6, 12, 24, 48, 0xffff };
     int i_level_prefix = 15;
-    int mask = level >> 15;
+    int mask = level >> 31;
     int abs_level = (level^mask)-mask;
     int i_level_code = abs_level*2-mask-2;
     if( ( i_level_code >> i_suffix_length ) < 15 )
@@ -95,7 +98,7 @@ static inline int block_residual_write_cavlc_escape( x264_t *h, int i_suffix_len
             {
 #if RDO_SKIP_BS
                 /* Weight highly against overflows. */
-                s->i_bits_encoded += 1000000;
+                s->i_bits_encoded += 2000;
 #else
                 x264_log(h, X264_LOG_WARNING, "OVERFLOW levelcode=%d is only allowed in High Profile\n", i_level_code );
                 /* clip level, preserving sign */
@@ -113,13 +116,13 @@ static inline int block_residual_write_cavlc_escape( x264_t *h, int i_suffix_len
     return i_suffix_length;
 }
 
-static int block_residual_write_cavlc( x264_t *h, int i_ctxBlockCat, int16_t *l, int nC )
+static int block_residual_write_cavlc_internal( x264_t *h, int ctx_block_cat, dctcoef *l, int nC )
 {
     bs_t *s = &h->out.bs;
     static const uint8_t ctz_index[8] = {3,0,1,0,2,0,1,0};
-    static const int count_cat[5] = {16, 15, 16, 4, 15};
+    static const uint8_t count_cat[5] = {16, 15, 16, 4, 15};
     x264_run_level_t runlevel;
-    int i_trailing, i_total_zero, i_suffix_length, i;
+    int i_trailing, i_total_zero, i_suffix_length;
     int i_total = 0;
     unsigned int i_sign;
 
@@ -127,7 +130,7 @@ static int block_residual_write_cavlc( x264_t *h, int i_ctxBlockCat, int16_t *l,
     /* set these to 2 to allow branchless i_trailing calculation */
     runlevel.level[1] = 2;
     runlevel.level[2] = 2;
-    i_total = h->quantf.coeff_level_run[i_ctxBlockCat]( l, &runlevel );
+    i_total = h->quantf.coeff_level_run[ctx_block_cat]( l, &runlevel );
     i_total_zero = runlevel.last + 1 - i_total;
 
     i_trailing = ((((runlevel.level[0]+1) | (1-runlevel.level[0])) >> 31) & 1) // abs(runlevel.level[0])>1
@@ -140,17 +143,16 @@ static int block_residual_write_cavlc( x264_t *h, int i_ctxBlockCat, int16_t *l,
     i_sign >>= 3-i_trailing;
 
     /* total/trailing */
-    bs_write_vlc( s, x264_coeff_token[nC][i_total*4+i_trailing-4] );
+    bs_write_vlc( s, x264_coeff_token[nC][i_total-1][i_trailing] );
 
     i_suffix_length = i_total > 10 && i_trailing < 3;
     bs_write( s, i_trailing, i_sign );
 
     if( i_trailing < i_total )
     {
-        int16_t val = runlevel.level[i_trailing];
-        int16_t val_original = runlevel.level[i_trailing]+LEVEL_TABLE_SIZE/2;
-        if( i_trailing < 3 )
-            val -= (val>>15)|1; /* as runlevel.level[i] can't be 1 for the first one if i_trailing < 3 */
+        int val = runlevel.level[i_trailing];
+        int val_original = runlevel.level[i_trailing]+LEVEL_TABLE_SIZE/2;
+        val -= ((val>>31)|1) & -(i_trailing < 3); /* as runlevel.level[i] can't be 1 for the first one if i_trailing < 3 */
         val += LEVEL_TABLE_SIZE/2;
 
         if( (unsigned)val_original < LEVEL_TABLE_SIZE )
@@ -160,7 +162,7 @@ static int block_residual_write_cavlc( x264_t *h, int i_ctxBlockCat, int16_t *l,
         }
         else
             i_suffix_length = block_residual_write_cavlc_escape( h, i_suffix_length, val-LEVEL_TABLE_SIZE/2 );
-        for( i = i_trailing+1; i < i_total; i++ )
+        for( int i = i_trailing+1; i < i_total; i++ )
         {
             val = runlevel.level[i] + LEVEL_TABLE_SIZE/2;
             if( (unsigned)val < LEVEL_TABLE_SIZE )
@@ -173,15 +175,15 @@ static int block_residual_write_cavlc( x264_t *h, int i_ctxBlockCat, int16_t *l,
         }
     }
 
-    if( i_total < count_cat[i_ctxBlockCat] )
+    if( (uint8_t)i_total < count_cat[ctx_block_cat] )
     {
-        if( i_ctxBlockCat == DCT_CHROMA_DC )
+        if( ctx_block_cat == DCT_CHROMA_DC )
             bs_write_vlc( s, x264_total_zeros_dc[i_total-1][i_total_zero] );
         else
             bs_write_vlc( s, x264_total_zeros[i_total-1][i_total_zero] );
     }
 
-    for( i = 0; i < i_total-1 && i_total_zero > 0; i++ )
+    for( int i = 0; i < i_total-1 && i_total_zero > 0; i++ )
     {
         int i_zl = X264_MIN( i_total_zero, 7 );
         bs_write_vlc( s, x264_run_before[i_zl-1][runlevel.run[i]] );
@@ -200,7 +202,7 @@ static const uint8_t ct_index[17] = {0,0,1,1,2,2,2,2,3,3,3,3,3,3,3,3,3};
     if( !*nnz )\
         bs_write_vlc( &h->out.bs, x264_coeff0_token[nC] );\
     else\
-        *nnz = block_residual_write_cavlc(h,cat,l,nC);\
+        *nnz = block_residual_write_cavlc_internal(h,cat,l,nC);\
 }
 
 static void cavlc_qp_delta( x264_t *h )
@@ -220,10 +222,10 @@ static void cavlc_qp_delta( x264_t *h )
 
     if( i_dqp )
     {
-        if( i_dqp < -26 )
-            i_dqp += 52;
-        else if( i_dqp > 25 )
-            i_dqp -= 52;
+        if( i_dqp < -(QP_MAX+1)/2 )
+            i_dqp += QP_MAX+1;
+        else if( i_dqp > QP_MAX/2 )
+            i_dqp -= QP_MAX+1;
     }
     bs_write_se( s, i_dqp );
 }
@@ -263,18 +265,17 @@ static inline void cavlc_mb8x8_mvd( x264_t *h, int i )
 
 static inline void x264_macroblock_luma_write_cavlc( x264_t *h, int i8start, int i8end )
 {
-    int i8, i4;
     if( h->mb.b_transform_8x8 )
     {
         /* shuffle 8x8 dct coeffs into 4x4 lists */
-        for( i8 = i8start; i8 <= i8end; i8++ )
+        for( int i8 = i8start; i8 <= i8end; i8++ )
             if( h->mb.i_cbp_luma & (1 << i8) )
                 h->zigzagf.interleave_8x8_cavlc( h->dct.luma4x4[i8*4], h->dct.luma8x8[i8], &h->mb.cache.non_zero_count[x264_scan8[i8*4]] );
     }
 
-    for( i8 = i8start; i8 <= i8end; i8++ )
+    for( int i8 = i8start; i8 <= i8end; i8++ )
         if( h->mb.i_cbp_luma & (1 << i8) )
-            for( i4 = 0; i4 < 4; i4++ )
+            for( int i4 = 0; i4 < 4; i4++ )
                 block_residual_write_cavlc( h, DCT_LUMA_4x4, i4+i8*4, h->dct.luma4x4[i4+i8*4] );
 }
 
@@ -287,7 +288,6 @@ void x264_macroblock_write_cavlc( x264_t *h )
     const int i_mb_type = h->mb.i_type;
     static const uint8_t i_offsets[3] = {5,23,0};
     int i_mb_i_offset = i_offsets[h->sh.i_type];
-    int i;
 
 #if RDO_SKIP_BS
     s->i_bits_encoded = 0;
@@ -312,23 +312,16 @@ void x264_macroblock_write_cavlc( x264_t *h )
 
         bs_align_0( s );
 
-        memcpy( s->p, h->mb.pic.p_fenc[0], 256 );
-        s->p += 256;
-        for( i = 0; i < 8; i++ )
-            memcpy( s->p + i*8, h->mb.pic.p_fenc[1] + i*FENC_STRIDE, 8 );
-        s->p += 64;
-        for( i = 0; i < 8; i++ )
-            memcpy( s->p + i*8, h->mb.pic.p_fenc[2] + i*FENC_STRIDE, 8 );
-        s->p += 64;
+        for( int i = 0; i < 256; i++ )
+            bs_write( s, BIT_DEPTH, h->mb.pic.p_fenc[0][i] );
+        for( int ch = 1; ch < 3; ch++ )
+            for( int i = 0; i < 8; i++ )
+                for( int j = 0; j < 8; j++ )
+                    bs_write( s, BIT_DEPTH, h->mb.pic.p_fenc[ch][i*FENC_STRIDE+j] );
 
         bs_init( s, s->p, s->p_end - s->p );
         s->p_start = p_start;
 
-        /* if PCM is chosen, we need to store reconstructed frame data */
-        h->mc.copy[PIXEL_16x16]( h->mb.pic.p_fdec[0], FDEC_STRIDE, h->mb.pic.p_fenc[0], FENC_STRIDE, 16 );
-        h->mc.copy[PIXEL_8x8]  ( h->mb.pic.p_fdec[1], FDEC_STRIDE, h->mb.pic.p_fenc[1], FENC_STRIDE, 8 );
-        h->mc.copy[PIXEL_8x8]  ( h->mb.pic.p_fdec[2], FDEC_STRIDE, h->mb.pic.p_fenc[2], FENC_STRIDE, 8 );
-
         h->stat.frame.i_tex_bits += bs_pos(s) - i_mb_pos_tex;
         return;
     }
@@ -346,7 +339,7 @@ void x264_macroblock_write_cavlc( x264_t *h )
             bs_write1( s, h->mb.b_transform_8x8 );
 
         /* Prediction: Luma */
-        for( i = 0; i < 16; i += di )
+        for( int i = 0; i < 16; i += di )
         {
             int i_pred = x264_mb_predict_intra4x4_mode( h, i );
             int i_mode = x264_mb_pred_mode4x4_fix( h->mb.cache.intra4x4_pred_mode[x264_scan8[i]] );
@@ -414,7 +407,7 @@ void x264_macroblock_write_cavlc( x264_t *h )
 
         /* sub mb type */
         if( h->param.analyse.inter & X264_ANALYSE_PSUB8x8 )
-            for( i = 0; i < 4; i++ )
+            for( int i = 0; i < 4; i++ )
                 bs_write_ue( s, sub_mb_type_p_to_golomb[ h->mb.i_sub_partition[i] ] );
         else
             bs_write( s, 4, 0xf );
@@ -428,7 +421,7 @@ void x264_macroblock_write_cavlc( x264_t *h )
             bs_write_te( s, h->mb.pic.i_fref[0] - 1, h->mb.cache.ref[0][x264_scan8[12]] );
         }
 
-        for( i = 0; i < 4; i++ )
+        for( int i = 0; i < 4; i++ )
             cavlc_mb8x8_mvd( h, i );
     }
     else if( i_mb_type == B_8x8 )
@@ -436,24 +429,24 @@ void x264_macroblock_write_cavlc( x264_t *h )
         bs_write_ue( s, 22 );
 
         /* sub mb type */
-        for( i = 0; i < 4; i++ )
+        for( int i = 0; i < 4; i++ )
             bs_write_ue( s, sub_mb_type_b_to_golomb[ h->mb.i_sub_partition[i] ] );
 
         /* ref */
         if( h->mb.pic.i_fref[0] > 1 )
-            for( i = 0; i < 4; i++ )
+            for( int i = 0; i < 4; i++ )
                 if( x264_mb_partition_listX_table[0][ h->mb.i_sub_partition[i] ] )
                     bs_write_te( s, h->mb.pic.i_fref[0] - 1, h->mb.cache.ref[0][x264_scan8[i*4]] );
         if( h->mb.pic.i_fref[1] > 1 )
-            for( i = 0; i < 4; i++ )
+            for( int i = 0; i < 4; i++ )
                 if( x264_mb_partition_listX_table[1][ h->mb.i_sub_partition[i] ] )
                     bs_write_te( s, h->mb.pic.i_fref[1] - 1, h->mb.cache.ref[1][x264_scan8[i*4]] );
 
         /* mvd */
-        for( i = 0; i < 4; i++ )
+        for( int i = 0; i < 4; i++ )
             if( x264_mb_partition_listX_table[0][ h->mb.i_sub_partition[i] ] )
                 cavlc_mb_mvd( h, 0, 4*i, 2 );
-        for( i = 0; i < 4; i++ )
+        for( int i = 0; i < 4; i++ )
             if( x264_mb_partition_listX_table[1][ h->mb.i_sub_partition[i] ] )
                 cavlc_mb_mvd( h, 1, 4*i, 2 );
     }
@@ -523,7 +516,7 @@ void x264_macroblock_write_cavlc( x264_t *h )
 
         /* AC Luma */
         if( h->mb.i_cbp_luma )
-            for( i = 0; i < 16; i++ )
+            for( int i = 0; i < 16; i++ )
                 block_residual_write_cavlc( h, DCT_LUMA_AC, i, h->dct.luma4x4[i]+1 );
     }
     else if( h->mb.i_cbp_luma | h->mb.i_cbp_chroma )
@@ -537,7 +530,7 @@ void x264_macroblock_write_cavlc( x264_t *h )
         block_residual_write_cavlc( h, DCT_CHROMA_DC, 25, h->dct.chroma_dc[0] );
         block_residual_write_cavlc( h, DCT_CHROMA_DC, 26, h->dct.chroma_dc[1] );
         if( h->mb.i_cbp_chroma&0x02 ) /* Chroma AC residual present */
-            for( i = 16; i < 24; i++ )
+            for( int i = 16; i < 24; i++ )
                 block_residual_write_cavlc( h, DCT_CHROMA_AC, i, h->dct.luma4x4[i]+1 );
     }
 
@@ -640,8 +633,7 @@ static int x264_i8x8_chroma_size_cavlc( x264_t *h )
 
         if( h->mb.i_cbp_chroma == 2 )
         {
-            int i;
-            for( i = 16; i < 24; i++ )
+            for( int i = 16; i < 24; i++ )
                 block_residual_write_cavlc( h, DCT_CHROMA_AC, i, h->dct.luma4x4[i]+1 );
         }
     }