]> git.sesse.net Git - x264/blob - encoder/cavlc.c
Use a large LUT for CAVLC zero-run bit codes
[x264] / encoder / cavlc.c
1 /*****************************************************************************
2  * cavlc.c: cavlc bitstream writing
3  *****************************************************************************
4  * Copyright (C) 2003-2011 x264 project
5  *
6  * Authors: Laurent Aimar <fenrir@via.ecp.fr>
7  *          Loren Merritt <lorenm@u.washington.edu>
8  *          Fiona Glaser <fiona@x264.com>
9  *
10  * This program is free software; you can redistribute it and/or modify
11  * it under the terms of the GNU General Public License as published by
12  * the Free Software Foundation; either version 2 of the License, or
13  * (at your option) any later version.
14  *
15  * This program is distributed in the hope that it will be useful,
16  * but WITHOUT ANY WARRANTY; without even the implied warranty of
17  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
18  * GNU General Public License for more details.
19  *
20  * You should have received a copy of the GNU General Public License
21  * along with this program; if not, write to the Free Software
22  * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA  02111, USA.
23  *
24  * This program is also available under a commercial proprietary license.
25  * For more information, contact us at licensing@x264.com.
26  *****************************************************************************/
27
28 #include "common/common.h"
29 #include "macroblock.h"
30
31 #ifndef RDO_SKIP_BS
32 #define RDO_SKIP_BS 0
33 #endif
34
35 /* [400,420][inter,intra] */
36 static const uint8_t cbp_to_golomb[2][2][48] =
37 {
38     {{ 0,  1,  2,  5,  3,  6, 14, 10,  4, 15,  7, 11,  8, 12, 13,  9 },
39      { 1, 10, 11,  6, 12,  7, 14,  2, 13, 15,  8,  3,  9,  4,  5,  0 }},
40     {{ 0,  2,  3,  7,  4,  8, 17, 13,  5, 18,  9, 14, 10, 15, 16, 11,
41        1, 32, 33, 36, 34, 37, 44, 40, 35, 45, 38, 41, 39, 42, 43, 19,
42        6, 24, 25, 20, 26, 21, 46, 28, 27, 47, 22, 29, 23, 30, 31, 12 },
43      { 3, 29, 30, 17, 31, 18, 37,  8, 32, 38, 19,  9, 20, 10, 11,  2,
44       16, 33, 34, 21, 35, 22, 39,  4, 36, 40, 23,  5, 24,  6,  7,  1,
45       41, 42, 43, 25, 44, 26, 46, 12, 45, 47, 27, 13, 28, 14, 15,  0 }}
46 };
47
48 static const uint8_t mb_type_b_to_golomb[3][9]=
49 {
50     { 4,  8, 12, 10,  6, 14, 16, 18, 20 }, /* D_16x8 */
51     { 5,  9, 13, 11,  7, 15, 17, 19, 21 }, /* D_8x16 */
52     { 1, -1, -1, -1,  2, -1, -1, -1,  3 }  /* D_16x16 */
53 };
54
55 static const uint8_t subpartition_p_to_golomb[4]=
56 {
57     3, 1, 2, 0
58 };
59
60 static const uint8_t subpartition_b_to_golomb[13]=
61 {
62     10,  4,  5,  1, 11,  6,  7,  2, 12,  8,  9,  3,  0
63 };
64
65 #define bs_write_vlc(s,v) bs_write( s, (v).i_size, (v).i_bits )
66
67 /****************************************************************************
68  * x264_cavlc_block_residual:
69  ****************************************************************************/
70 static inline int x264_cavlc_block_residual_escape( x264_t *h, int i_suffix_length, int level )
71 {
72     bs_t *s = &h->out.bs;
73     static const uint16_t next_suffix[7] = { 0, 3, 6, 12, 24, 48, 0xffff };
74     int i_level_prefix = 15;
75     int mask = level >> 31;
76     int abs_level = (level^mask)-mask;
77     int i_level_code = abs_level*2-mask-2;
78     if( ( i_level_code >> i_suffix_length ) < 15 )
79     {
80         bs_write( s, (i_level_code >> i_suffix_length) + 1 + i_suffix_length,
81                  (1<<i_suffix_length) + (i_level_code & ((1<<i_suffix_length)-1)) );
82     }
83     else
84     {
85         i_level_code -= 15 << i_suffix_length;
86         if( i_suffix_length == 0 )
87             i_level_code -= 15;
88
89         /* If the prefix size exceeds 15, High Profile is required. */
90         if( i_level_code >= 1<<12 )
91         {
92             if( h->sps->i_profile_idc >= PROFILE_HIGH )
93             {
94                 while( i_level_code > 1<<(i_level_prefix-3) )
95                 {
96                     i_level_code -= 1<<(i_level_prefix-3);
97                     i_level_prefix++;
98                 }
99             }
100             else
101             {
102 #if RDO_SKIP_BS
103                 /* Weight highly against overflows. */
104                 s->i_bits_encoded += 2000;
105 #else
106                 /* We've had an overflow; note it down and re-encode the MB later. */
107                 h->mb.b_overflow = 1;
108 #endif
109             }
110         }
111         bs_write( s, i_level_prefix + 1, 1 );
112         bs_write( s, i_level_prefix - 3, i_level_code & ((1<<(i_level_prefix-3))-1) );
113     }
114     if( i_suffix_length == 0 )
115         i_suffix_length++;
116     if( abs_level > next_suffix[i_suffix_length] )
117         i_suffix_length++;
118     return i_suffix_length;
119 }
120
121 static int x264_cavlc_block_residual_internal( x264_t *h, int ctx_block_cat, dctcoef *l, int nC )
122 {
123     bs_t *s = &h->out.bs;
124     static const uint8_t ctz_index[8] = {3,0,1,0,2,0,1,0};
125     static const uint8_t count_cat[14] = {16, 15, 16, 0, 15, 64, 16, 15, 16, 64, 16, 15, 16, 64};
126     x264_run_level_t runlevel;
127     int i_total, i_trailing, i_total_zero, i_suffix_length;
128     unsigned int i_sign;
129
130     /* level and run and total */
131     /* set these to 2 to allow branchless i_trailing calculation */
132     runlevel.level[1] = 2;
133     runlevel.level[2] = 2;
134     i_total = h->quantf.coeff_level_run[ctx_block_cat]( l, &runlevel );
135     x264_prefetch( &x264_run_before[runlevel.mask] );
136     i_total_zero = runlevel.last + 1 - i_total;
137
138     i_trailing = ((((runlevel.level[0]+1) | (1-runlevel.level[0])) >> 31) & 1) // abs(runlevel.level[0])>1
139                | ((((runlevel.level[1]+1) | (1-runlevel.level[1])) >> 31) & 2)
140                | ((((runlevel.level[2]+1) | (1-runlevel.level[2])) >> 31) & 4);
141     i_trailing = ctz_index[i_trailing];
142     i_sign = ((runlevel.level[2] >> 31) & 1)
143            | ((runlevel.level[1] >> 31) & 2)
144            | ((runlevel.level[0] >> 31) & 4);
145     i_sign >>= 3-i_trailing;
146
147     /* total/trailing */
148     bs_write_vlc( s, x264_coeff_token[nC][i_total-1][i_trailing] );
149
150     i_suffix_length = i_total > 10 && i_trailing < 3;
151     bs_write( s, i_trailing, i_sign );
152
153     if( i_trailing < i_total )
154     {
155         int val = runlevel.level[i_trailing];
156         int val_original = runlevel.level[i_trailing]+LEVEL_TABLE_SIZE/2;
157         val -= ((val>>31)|1) & -(i_trailing < 3); /* as runlevel.level[i] can't be 1 for the first one if i_trailing < 3 */
158         val += LEVEL_TABLE_SIZE/2;
159
160         if( (unsigned)val_original < LEVEL_TABLE_SIZE )
161         {
162             bs_write_vlc( s, x264_level_token[i_suffix_length][val] );
163             i_suffix_length = x264_level_token[i_suffix_length][val_original].i_next;
164         }
165         else
166             i_suffix_length = x264_cavlc_block_residual_escape( h, i_suffix_length, val-LEVEL_TABLE_SIZE/2 );
167         for( int i = i_trailing+1; i < i_total; i++ )
168         {
169             val = runlevel.level[i] + LEVEL_TABLE_SIZE/2;
170             if( (unsigned)val < LEVEL_TABLE_SIZE )
171             {
172                 bs_write_vlc( s, x264_level_token[i_suffix_length][val] );
173                 i_suffix_length = x264_level_token[i_suffix_length][val].i_next;
174             }
175             else
176                 i_suffix_length = x264_cavlc_block_residual_escape( h, i_suffix_length, val-LEVEL_TABLE_SIZE/2 );
177         }
178     }
179
180     if( ctx_block_cat == DCT_CHROMA_DC )
181     {
182         if( i_total < 8>>CHROMA_V_SHIFT )
183         {
184             vlc_t total_zeros = CHROMA_FORMAT == CHROMA_420 ? x264_total_zeros_2x2_dc[i_total-1][i_total_zero]
185                                                             : x264_total_zeros_2x4_dc[i_total-1][i_total_zero];
186             bs_write_vlc( s, total_zeros );
187         }
188     }
189     else if( (uint8_t)i_total < count_cat[ctx_block_cat] )
190         bs_write_vlc( s, x264_total_zeros[i_total-1][i_total_zero] );
191
192     int zero_run_code = x264_run_before[runlevel.mask];
193     bs_write( s, zero_run_code&0x1f, zero_run_code>>5 );
194
195     return i_total;
196 }
197
198 static const uint8_t ct_index[17] = {0,0,1,1,2,2,2,2,3,3,3,3,3,3,3,3,3};
199
200 #define x264_cavlc_block_residual(h,cat,idx,l)\
201 {\
202     int nC = cat == DCT_CHROMA_DC ? 5 - CHROMA_V_SHIFT\
203                                   : ct_index[x264_mb_predict_non_zero_code( h, cat == DCT_LUMA_DC ? (idx - LUMA_DC)*16 : idx )];\
204     uint8_t *nnz = &h->mb.cache.non_zero_count[x264_scan8[idx]];\
205     if( !*nnz )\
206         bs_write_vlc( &h->out.bs, x264_coeff0_token[nC] );\
207     else\
208         *nnz = x264_cavlc_block_residual_internal(h,cat,l,nC);\
209 }
210
211 static void x264_cavlc_qp_delta( x264_t *h )
212 {
213     bs_t *s = &h->out.bs;
214     int i_dqp = h->mb.i_qp - h->mb.i_last_qp;
215
216     /* Avoid writing a delta quant if we have an empty i16x16 block, e.g. in a completely flat background area */
217     if( h->mb.i_type == I_16x16 && !(h->mb.i_cbp_luma | h->mb.i_cbp_chroma)
218         && !h->mb.cache.non_zero_count[x264_scan8[LUMA_DC]]
219         && !h->mb.cache.non_zero_count[x264_scan8[CHROMA_DC+0]]
220         && !h->mb.cache.non_zero_count[x264_scan8[CHROMA_DC+1]] )
221     {
222 #if !RDO_SKIP_BS
223         h->mb.i_qp = h->mb.i_last_qp;
224 #endif
225         i_dqp = 0;
226     }
227
228     if( i_dqp )
229     {
230         if( i_dqp < -(QP_MAX_SPEC+1)/2 )
231             i_dqp += QP_MAX_SPEC+1;
232         else if( i_dqp > QP_MAX_SPEC/2 )
233             i_dqp -= QP_MAX_SPEC+1;
234     }
235     bs_write_se( s, i_dqp );
236 }
237
238 static void x264_cavlc_mvd( x264_t *h, int i_list, int idx, int width )
239 {
240     bs_t *s = &h->out.bs;
241     ALIGNED_4( int16_t mvp[2] );
242     x264_mb_predict_mv( h, i_list, idx, width, mvp );
243     bs_write_se( s, h->mb.cache.mv[i_list][x264_scan8[idx]][0] - mvp[0] );
244     bs_write_se( s, h->mb.cache.mv[i_list][x264_scan8[idx]][1] - mvp[1] );
245 }
246
247 static inline void x264_cavlc_8x8_mvd( x264_t *h, int i )
248 {
249     switch( h->mb.i_sub_partition[i] )
250     {
251         case D_L0_8x8:
252             x264_cavlc_mvd( h, 0, 4*i, 2 );
253             break;
254         case D_L0_8x4:
255             x264_cavlc_mvd( h, 0, 4*i+0, 2 );
256             x264_cavlc_mvd( h, 0, 4*i+2, 2 );
257             break;
258         case D_L0_4x8:
259             x264_cavlc_mvd( h, 0, 4*i+0, 1 );
260             x264_cavlc_mvd( h, 0, 4*i+1, 1 );
261             break;
262         case D_L0_4x4:
263             x264_cavlc_mvd( h, 0, 4*i+0, 1 );
264             x264_cavlc_mvd( h, 0, 4*i+1, 1 );
265             x264_cavlc_mvd( h, 0, 4*i+2, 1 );
266             x264_cavlc_mvd( h, 0, 4*i+3, 1 );
267             break;
268     }
269 }
270
271 static inline void x264_cavlc_macroblock_luma_residual( x264_t *h, int i8start, int i8end )
272 {
273     if( h->mb.b_transform_8x8 )
274     {
275         /* shuffle 8x8 dct coeffs into 4x4 lists */
276         for( int i8 = i8start; i8 <= i8end; i8++ )
277             if( h->mb.cache.non_zero_count[x264_scan8[i8*4]] )
278                 h->zigzagf.interleave_8x8_cavlc( h->dct.luma4x4[i8*4], h->dct.luma8x8[i8], &h->mb.cache.non_zero_count[x264_scan8[i8*4]] );
279     }
280
281     for( int i8 = i8start; i8 <= i8end; i8++ )
282         if( h->mb.i_cbp_luma & (1 << (i8&3)) )
283             for( int i4 = 0; i4 < 4; i4++ )
284                 x264_cavlc_block_residual( h, DCT_LUMA_4x4, i4+i8*4, h->dct.luma4x4[i4+i8*4] );
285 }
286
287 static void x264_cavlc_mb_header_i( x264_t *h, int i_mb_type, int i_mb_i_offset, int chroma )
288 {
289     bs_t *s = &h->out.bs;
290     if( i_mb_type == I_16x16 )
291     {
292         bs_write_ue( s, i_mb_i_offset + 1 + x264_mb_pred_mode16x16_fix[h->mb.i_intra16x16_pred_mode] +
293                         h->mb.i_cbp_chroma * 4 + ( h->mb.i_cbp_luma == 0 ? 0 : 12 ) );
294     }
295     else //if( i_mb_type == I_4x4 || i_mb_type == I_8x8 )
296     {
297         int di = i_mb_type == I_8x8 ? 4 : 1;
298         bs_write_ue( s, i_mb_i_offset + 0 );
299         if( h->pps->b_transform_8x8_mode )
300             bs_write1( s, h->mb.b_transform_8x8 );
301
302         /* Prediction: Luma */
303         for( int i = 0; i < 16; i += di )
304         {
305             int i_pred = x264_mb_predict_intra4x4_mode( h, i );
306             int i_mode = x264_mb_pred_mode4x4_fix( h->mb.cache.intra4x4_pred_mode[x264_scan8[i]] );
307
308             if( i_pred == i_mode )
309                 bs_write1( s, 1 );  /* b_prev_intra4x4_pred_mode */
310             else
311                 bs_write( s, 4, i_mode - (i_mode > i_pred) );
312         }
313
314     }
315     if( chroma )
316         bs_write_ue( s, x264_mb_chroma_pred_mode_fix[h->mb.i_chroma_pred_mode] );
317 }
318
319 static ALWAYS_INLINE void x264_cavlc_mb_header_p( x264_t *h, int i_mb_type, int chroma )
320 {
321     bs_t *s = &h->out.bs;
322     if( i_mb_type == P_L0 )
323     {
324         if( h->mb.i_partition == D_16x16 )
325         {
326             bs_write1( s, 1 );
327
328             if( h->mb.pic.i_fref[0] > 1 )
329                 bs_write_te( s, h->mb.pic.i_fref[0] - 1, h->mb.cache.ref[0][x264_scan8[0]] );
330             x264_cavlc_mvd( h, 0, 0, 4 );
331         }
332         else if( h->mb.i_partition == D_16x8 )
333         {
334             bs_write_ue( s, 1 );
335             if( h->mb.pic.i_fref[0] > 1 )
336             {
337                 bs_write_te( s, h->mb.pic.i_fref[0] - 1, h->mb.cache.ref[0][x264_scan8[0]] );
338                 bs_write_te( s, h->mb.pic.i_fref[0] - 1, h->mb.cache.ref[0][x264_scan8[8]] );
339             }
340             x264_cavlc_mvd( h, 0, 0, 4 );
341             x264_cavlc_mvd( h, 0, 8, 4 );
342         }
343         else if( h->mb.i_partition == D_8x16 )
344         {
345             bs_write_ue( s, 2 );
346             if( h->mb.pic.i_fref[0] > 1 )
347             {
348                 bs_write_te( s, h->mb.pic.i_fref[0] - 1, h->mb.cache.ref[0][x264_scan8[0]] );
349                 bs_write_te( s, h->mb.pic.i_fref[0] - 1, h->mb.cache.ref[0][x264_scan8[4]] );
350             }
351             x264_cavlc_mvd( h, 0, 0, 2 );
352             x264_cavlc_mvd( h, 0, 4, 2 );
353         }
354     }
355     else if( i_mb_type == P_8x8 )
356     {
357         int b_sub_ref;
358         if( (h->mb.cache.ref[0][x264_scan8[0]] | h->mb.cache.ref[0][x264_scan8[ 4]] |
359              h->mb.cache.ref[0][x264_scan8[8]] | h->mb.cache.ref[0][x264_scan8[12]]) == 0 )
360         {
361             bs_write_ue( s, 4 );
362             b_sub_ref = 0;
363         }
364         else
365         {
366             bs_write_ue( s, 3 );
367             b_sub_ref = 1;
368         }
369
370         /* sub mb type */
371         if( h->param.analyse.inter & X264_ANALYSE_PSUB8x8 )
372             for( int i = 0; i < 4; i++ )
373                 bs_write_ue( s, subpartition_p_to_golomb[ h->mb.i_sub_partition[i] ] );
374         else
375             bs_write( s, 4, 0xf );
376
377         /* ref0 */
378         if( b_sub_ref )
379         {
380             bs_write_te( s, h->mb.pic.i_fref[0] - 1, h->mb.cache.ref[0][x264_scan8[0]] );
381             bs_write_te( s, h->mb.pic.i_fref[0] - 1, h->mb.cache.ref[0][x264_scan8[4]] );
382             bs_write_te( s, h->mb.pic.i_fref[0] - 1, h->mb.cache.ref[0][x264_scan8[8]] );
383             bs_write_te( s, h->mb.pic.i_fref[0] - 1, h->mb.cache.ref[0][x264_scan8[12]] );
384         }
385
386         for( int i = 0; i < 4; i++ )
387             x264_cavlc_8x8_mvd( h, i );
388     }
389     else //if( IS_INTRA( i_mb_type ) )
390         x264_cavlc_mb_header_i( h, i_mb_type, 5, chroma );
391 }
392
393 static ALWAYS_INLINE void x264_cavlc_mb_header_b( x264_t *h, int i_mb_type, int chroma )
394 {
395     bs_t *s = &h->out.bs;
396     if( i_mb_type == B_8x8 )
397     {
398         bs_write_ue( s, 22 );
399
400         /* sub mb type */
401         for( int i = 0; i < 4; i++ )
402             bs_write_ue( s, subpartition_b_to_golomb[ h->mb.i_sub_partition[i] ] );
403
404         /* ref */
405         if( h->mb.pic.i_fref[0] > 1 )
406             for( int i = 0; i < 4; i++ )
407                 if( x264_mb_partition_listX_table[0][ h->mb.i_sub_partition[i] ] )
408                     bs_write_te( s, h->mb.pic.i_fref[0] - 1, h->mb.cache.ref[0][x264_scan8[i*4]] );
409         if( h->mb.pic.i_fref[1] > 1 )
410             for( int i = 0; i < 4; i++ )
411                 if( x264_mb_partition_listX_table[1][ h->mb.i_sub_partition[i] ] )
412                     bs_write_te( s, h->mb.pic.i_fref[1] - 1, h->mb.cache.ref[1][x264_scan8[i*4]] );
413
414         /* mvd */
415         for( int i = 0; i < 4; i++ )
416             if( x264_mb_partition_listX_table[0][ h->mb.i_sub_partition[i] ] )
417                 x264_cavlc_mvd( h, 0, 4*i, 2 );
418         for( int i = 0; i < 4; i++ )
419             if( x264_mb_partition_listX_table[1][ h->mb.i_sub_partition[i] ] )
420                 x264_cavlc_mvd( h, 1, 4*i, 2 );
421     }
422     else if( i_mb_type >= B_L0_L0 && i_mb_type <= B_BI_BI )
423     {
424         /* All B mode */
425         /* Motion Vector */
426         const uint8_t (*b_list)[2] = x264_mb_type_list_table[i_mb_type];
427         const int i_ref0_max = h->mb.pic.i_fref[0] - 1;
428         const int i_ref1_max = h->mb.pic.i_fref[1] - 1;
429
430         bs_write_ue( s, mb_type_b_to_golomb[ h->mb.i_partition - D_16x8 ][ i_mb_type - B_L0_L0 ] );
431         if( h->mb.i_partition == D_16x16 )
432         {
433             if( i_ref0_max && b_list[0][0] ) bs_write_te( s, i_ref0_max, h->mb.cache.ref[0][x264_scan8[0]] );
434             if( i_ref1_max && b_list[1][0] ) bs_write_te( s, i_ref1_max, h->mb.cache.ref[1][x264_scan8[0]] );
435             if( b_list[0][0] ) x264_cavlc_mvd( h, 0, 0, 4 );
436             if( b_list[1][0] ) x264_cavlc_mvd( h, 1, 0, 4 );
437         }
438         else
439         {
440             if( i_ref0_max && b_list[0][0] ) bs_write_te( s, i_ref0_max, h->mb.cache.ref[0][x264_scan8[ 0]] );
441             if( i_ref0_max && b_list[0][1] ) bs_write_te( s, i_ref0_max, h->mb.cache.ref[0][x264_scan8[12]] );
442             if( i_ref1_max && b_list[1][0] ) bs_write_te( s, i_ref1_max, h->mb.cache.ref[1][x264_scan8[ 0]] );
443             if( i_ref1_max && b_list[1][1] ) bs_write_te( s, i_ref1_max, h->mb.cache.ref[1][x264_scan8[12]] );
444             if( h->mb.i_partition == D_16x8 )
445             {
446                 if( b_list[0][0] ) x264_cavlc_mvd( h, 0, 0, 4 );
447                 if( b_list[0][1] ) x264_cavlc_mvd( h, 0, 8, 4 );
448                 if( b_list[1][0] ) x264_cavlc_mvd( h, 1, 0, 4 );
449                 if( b_list[1][1] ) x264_cavlc_mvd( h, 1, 8, 4 );
450             }
451             else //if( h->mb.i_partition == D_8x16 )
452             {
453                 if( b_list[0][0] ) x264_cavlc_mvd( h, 0, 0, 2 );
454                 if( b_list[0][1] ) x264_cavlc_mvd( h, 0, 4, 2 );
455                 if( b_list[1][0] ) x264_cavlc_mvd( h, 1, 0, 2 );
456                 if( b_list[1][1] ) x264_cavlc_mvd( h, 1, 4, 2 );
457             }
458         }
459     }
460     else if( i_mb_type == B_DIRECT )
461         bs_write1( s, 1 );
462     else //if( IS_INTRA( i_mb_type ) )
463         x264_cavlc_mb_header_i( h, i_mb_type, 23, chroma );
464 }
465
466 /*****************************************************************************
467  * x264_macroblock_write:
468  *****************************************************************************/
469 void x264_macroblock_write_cavlc( x264_t *h )
470 {
471     bs_t *s = &h->out.bs;
472     const int i_mb_type = h->mb.i_type;
473     int plane_count = CHROMA444 ? 3 : 1;
474     int chroma = !CHROMA444;
475
476 #if RDO_SKIP_BS
477     s->i_bits_encoded = 0;
478 #else
479     const int i_mb_pos_start = bs_pos( s );
480     int       i_mb_pos_tex;
481 #endif
482
483     if( SLICE_MBAFF
484         && (!(h->mb.i_mb_y & 1) || IS_SKIP(h->mb.type[h->mb.i_mb_xy - h->mb.i_mb_stride])) )
485     {
486         bs_write1( s, MB_INTERLACED );
487     }
488
489 #if !RDO_SKIP_BS
490     if( i_mb_type == I_PCM )
491     {
492         static const uint8_t i_offsets[3] = {5,23,0};
493         uint8_t *p_start = s->p_start;
494         bs_write_ue( s, i_offsets[h->sh.i_type] + 25 );
495         i_mb_pos_tex = bs_pos( s );
496         h->stat.frame.i_mv_bits += i_mb_pos_tex - i_mb_pos_start;
497
498         bs_align_0( s );
499
500         for( int p = 0; p < plane_count; p++ )
501             for( int i = 0; i < 256; i++ )
502                 bs_write( s, BIT_DEPTH, h->mb.pic.p_fenc[p][i] );
503         if( chroma )
504             for( int ch = 1; ch < 3; ch++ )
505                 for( int i = 0; i < 16>>CHROMA_V_SHIFT; i++ )
506                     for( int j = 0; j < 8; j++ )
507                         bs_write( s, BIT_DEPTH, h->mb.pic.p_fenc[ch][i*FENC_STRIDE+j] );
508
509         bs_init( s, s->p, s->p_end - s->p );
510         s->p_start = p_start;
511
512         h->stat.frame.i_tex_bits += bs_pos(s) - i_mb_pos_tex;
513         return;
514     }
515 #endif
516
517     if( h->sh.i_type == SLICE_TYPE_P )
518         x264_cavlc_mb_header_p( h, i_mb_type, chroma );
519     else if( h->sh.i_type == SLICE_TYPE_B )
520         x264_cavlc_mb_header_b( h, i_mb_type, chroma );
521     else //if( h->sh.i_type == SLICE_TYPE_I )
522         x264_cavlc_mb_header_i( h, i_mb_type, 0, chroma );
523
524 #if !RDO_SKIP_BS
525     i_mb_pos_tex = bs_pos( s );
526     h->stat.frame.i_mv_bits += i_mb_pos_tex - i_mb_pos_start;
527 #endif
528
529     /* Coded block pattern */
530     if( i_mb_type != I_16x16 )
531         bs_write_ue( s, cbp_to_golomb[chroma][IS_INTRA(i_mb_type)][(h->mb.i_cbp_chroma << 4)|h->mb.i_cbp_luma] );
532
533     /* transform size 8x8 flag */
534     if( x264_mb_transform_8x8_allowed( h ) && h->mb.i_cbp_luma )
535         bs_write1( s, h->mb.b_transform_8x8 );
536
537     if( i_mb_type == I_16x16 )
538     {
539         x264_cavlc_qp_delta( h );
540
541         /* DC Luma */
542         for( int p = 0; p < plane_count; p++ )
543         {
544             x264_cavlc_block_residual( h, DCT_LUMA_DC, LUMA_DC+p, h->dct.luma16x16_dc[p] );
545
546             /* AC Luma */
547             if( h->mb.i_cbp_luma )
548                 for( int i = p*16; i < p*16+16; i++ )
549                     x264_cavlc_block_residual( h, DCT_LUMA_AC, i, h->dct.luma4x4[i]+1 );
550         }
551     }
552     else if( h->mb.i_cbp_luma | h->mb.i_cbp_chroma )
553     {
554         x264_cavlc_qp_delta( h );
555         x264_cavlc_macroblock_luma_residual( h, 0, plane_count*4-1 );
556     }
557     if( h->mb.i_cbp_chroma )
558     {
559         /* Chroma DC residual present */
560         x264_cavlc_block_residual( h, DCT_CHROMA_DC, CHROMA_DC+0, h->dct.chroma_dc[0] );
561         x264_cavlc_block_residual( h, DCT_CHROMA_DC, CHROMA_DC+1, h->dct.chroma_dc[1] );
562         if( h->mb.i_cbp_chroma == 2 ) /* Chroma AC residual present */
563         {
564             int step = 8 << CHROMA_V_SHIFT;
565             for( int i = 16; i < 3*16; i += step )
566                 for( int j = i; j < i+4; j++ )
567                     x264_cavlc_block_residual( h, DCT_CHROMA_AC, j, h->dct.luma4x4[j]+1 );
568         }
569     }
570
571 #if !RDO_SKIP_BS
572     h->stat.frame.i_tex_bits += bs_pos(s) - i_mb_pos_tex;
573 #endif
574 }
575
576 #if RDO_SKIP_BS
577 /*****************************************************************************
578  * RD only; doesn't generate a valid bitstream
579  * doesn't write cbp or chroma dc (I don't know how much this matters)
580  * doesn't write ref (never varies between calls, so no point in doing so)
581  * only writes subpartition for p8x8, needed for sub-8x8 mode decision RDO
582  * works on all partition sizes except 16x16
583  *****************************************************************************/
584 static int x264_partition_size_cavlc( x264_t *h, int i8, int i_pixel )
585 {
586     bs_t *s = &h->out.bs;
587     const int i_mb_type = h->mb.i_type;
588     int b_8x16 = h->mb.i_partition == D_8x16;
589     int plane_count = CHROMA444 ? 3 : 1;
590     int j;
591
592     if( i_mb_type == P_8x8 )
593     {
594         x264_cavlc_8x8_mvd( h, i8 );
595         bs_write_ue( s, subpartition_p_to_golomb[ h->mb.i_sub_partition[i8] ] );
596     }
597     else if( i_mb_type == P_L0 )
598         x264_cavlc_mvd( h, 0, 4*i8, 4>>b_8x16 );
599     else if( i_mb_type > B_DIRECT && i_mb_type < B_8x8 )
600     {
601         if( x264_mb_type_list_table[ i_mb_type ][0][!!i8] ) x264_cavlc_mvd( h, 0, 4*i8, 4>>b_8x16 );
602         if( x264_mb_type_list_table[ i_mb_type ][1][!!i8] ) x264_cavlc_mvd( h, 1, 4*i8, 4>>b_8x16 );
603     }
604     else //if( i_mb_type == B_8x8 )
605     {
606         if( x264_mb_partition_listX_table[0][ h->mb.i_sub_partition[i8] ] )
607             x264_cavlc_mvd( h, 0, 4*i8, 2 );
608         if( x264_mb_partition_listX_table[1][ h->mb.i_sub_partition[i8] ] )
609             x264_cavlc_mvd( h, 1, 4*i8, 2 );
610     }
611
612     for( j = (i_pixel < PIXEL_8x8); j >= 0; j-- )
613     {
614         for( int p = 0; p < plane_count; p++ )
615             x264_cavlc_macroblock_luma_residual( h, p*4+i8, p*4+i8 );
616         if( h->mb.i_cbp_chroma )
617         {
618             if( CHROMA_FORMAT == CHROMA_422 )
619             {
620                 int offset = (5*i8) & 0x09;
621                 x264_cavlc_block_residual( h, DCT_CHROMA_AC, 16+offset, h->dct.luma4x4[16+offset]+1 );
622                 x264_cavlc_block_residual( h, DCT_CHROMA_AC, 18+offset, h->dct.luma4x4[18+offset]+1 );
623                 x264_cavlc_block_residual( h, DCT_CHROMA_AC, 32+offset, h->dct.luma4x4[32+offset]+1 );
624                 x264_cavlc_block_residual( h, DCT_CHROMA_AC, 34+offset, h->dct.luma4x4[34+offset]+1 );
625             }
626             else
627             {
628                 x264_cavlc_block_residual( h, DCT_CHROMA_AC, 16+i8, h->dct.luma4x4[16+i8]+1 );
629                 x264_cavlc_block_residual( h, DCT_CHROMA_AC, 32+i8, h->dct.luma4x4[32+i8]+1 );
630             }
631         }
632         i8 += x264_pixel_size[i_pixel].h >> 3;
633     }
634
635     return h->out.bs.i_bits_encoded;
636 }
637
638 static int x264_subpartition_size_cavlc( x264_t *h, int i4, int i_pixel )
639 {
640     int plane_count = CHROMA444 ? 3 : 1;
641     int b_8x4 = i_pixel == PIXEL_8x4;
642     h->out.bs.i_bits_encoded = 0;
643     x264_cavlc_mvd( h, 0, i4, 1+b_8x4 );
644     for( int p = 0; p < plane_count; p++ )
645     {
646         x264_cavlc_block_residual( h, DCT_LUMA_4x4, p*16+i4, h->dct.luma4x4[p*16+i4] );
647         if( i_pixel != PIXEL_4x4 )
648             x264_cavlc_block_residual( h, DCT_LUMA_4x4, p*16+i4+2-b_8x4, h->dct.luma4x4[p*16+i4+2-b_8x4] );
649     }
650
651     return h->out.bs.i_bits_encoded;
652 }
653
654 static int x264_cavlc_intra4x4_pred_size( x264_t *h, int i4, int i_mode )
655 {
656     if( x264_mb_predict_intra4x4_mode( h, i4 ) == x264_mb_pred_mode4x4_fix( i_mode ) )
657         return 1;
658     else
659         return 4;
660 }
661
662 static int x264_partition_i8x8_size_cavlc( x264_t *h, int i8, int i_mode )
663 {
664     int plane_count = CHROMA444 ? 3 : 1;
665     h->out.bs.i_bits_encoded = x264_cavlc_intra4x4_pred_size( h, 4*i8, i_mode );
666     bs_write_ue( &h->out.bs, cbp_to_golomb[!CHROMA444][1][(h->mb.i_cbp_chroma << 4)|h->mb.i_cbp_luma] );
667     for( int p = 0; p < plane_count; p++ )
668         x264_cavlc_macroblock_luma_residual( h, p*4+i8, p*4+i8 );
669     return h->out.bs.i_bits_encoded;
670 }
671
672 static int x264_partition_i4x4_size_cavlc( x264_t *h, int i4, int i_mode )
673 {
674     int plane_count = CHROMA444 ? 3 : 1;
675     h->out.bs.i_bits_encoded = x264_cavlc_intra4x4_pred_size( h, i4, i_mode );
676     for( int p = 0; p < plane_count; p++ )
677         x264_cavlc_block_residual( h, DCT_LUMA_4x4, p*16+i4, h->dct.luma4x4[p*16+i4] );
678     return h->out.bs.i_bits_encoded;
679 }
680
681 static int x264_chroma_size_cavlc( x264_t *h )
682 {
683     h->out.bs.i_bits_encoded = bs_size_ue( x264_mb_chroma_pred_mode_fix[h->mb.i_chroma_pred_mode] );
684     if( h->mb.i_cbp_chroma )
685     {
686         x264_cavlc_block_residual( h, DCT_CHROMA_DC, CHROMA_DC+0, h->dct.chroma_dc[0] );
687         x264_cavlc_block_residual( h, DCT_CHROMA_DC, CHROMA_DC+1, h->dct.chroma_dc[1] );
688
689         if( h->mb.i_cbp_chroma == 2 )
690         {
691             int step = 8 << CHROMA_V_SHIFT;
692             for( int i = 16; i < 3*16; i += step )
693                 for( int j = i; j < i+4; j++ )
694                     x264_cavlc_block_residual( h, DCT_CHROMA_AC, j, h->dct.luma4x4[j]+1 );
695         }
696     }
697     return h->out.bs.i_bits_encoded;
698 }
699 #endif