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