1 /*****************************************************************************
2 * cavlc.c: h264 encoder library
3 *****************************************************************************
4 * Copyright (C) 2003-2008 x264 project
6 * Authors: Laurent Aimar <fenrir@via.ecp.fr>
7 * Loren Merritt <lorenm@u.washington.edu>
8 * Fiona Glaser <fiona@x264.com>
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.
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.
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 *****************************************************************************/
25 #include "common/common.h"
26 #include "macroblock.h"
32 static const uint8_t intra4x4_cbp_to_golomb[48]=
34 3, 29, 30, 17, 31, 18, 37, 8, 32, 38, 19, 9, 20, 10, 11, 2,
35 16, 33, 34, 21, 35, 22, 39, 4, 36, 40, 23, 5, 24, 6, 7, 1,
36 41, 42, 43, 25, 44, 26, 46, 12, 45, 47, 27, 13, 28, 14, 15, 0
38 static const uint8_t inter_cbp_to_golomb[48]=
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
44 static const uint8_t mb_type_b_to_golomb[3][9]=
46 { 4, 8, 12, 10, 6, 14, 16, 18, 20 }, /* D_16x8 */
47 { 5, 9, 13, 11, 7, 15, 17, 19, 21 }, /* D_8x16 */
48 { 1, -1, -1, -1, 2, -1, -1, -1, 3 } /* D_16x16 */
50 static const uint8_t sub_mb_type_p_to_golomb[4]=
54 static const uint8_t sub_mb_type_b_to_golomb[13]=
56 10, 4, 5, 1, 11, 6, 7, 2, 12, 8, 9, 3, 0
59 #define bs_write_vlc(s,v) bs_write( s, (v).i_size, (v).i_bits )
61 /****************************************************************************
62 * block_residual_write_cavlc:
63 ****************************************************************************/
64 static inline int block_residual_write_cavlc_escape( x264_t *h, bs_t *s, int i_suffix_length, int level )
66 static const uint16_t next_suffix[7] = { 0, 3, 6, 12, 24, 48, 0xffff };
67 int i_level_prefix = 15;
68 int mask = level >> 15;
69 int abs_level = (level^mask)-mask;
70 int i_level_code = abs_level*2-mask-2;
71 if( ( i_level_code >> i_suffix_length ) < 15 )
73 bs_write( s, (i_level_code >> i_suffix_length) + 1 + i_suffix_length,
74 (1<<i_suffix_length) + (i_level_code & ((1<<i_suffix_length)-1)) );
78 i_level_code -= 15 << i_suffix_length;
79 if( i_suffix_length == 0 )
82 /* If the prefix size exceeds 15, High Profile is required. */
83 if( i_level_code >= 1<<12 )
85 if( h->sps->i_profile_idc >= PROFILE_HIGH )
87 while( i_level_code > 1<<(i_level_prefix-3) )
89 i_level_code -= 1<<(i_level_prefix-3);
96 /* Weight highly against overflows. */
97 s->i_bits_encoded += 1000000;
99 x264_log(h, X264_LOG_WARNING, "OVERFLOW levelcode=%d is only allowed in High Profile\n", i_level_code );
100 /* clip level, preserving sign */
101 i_level_code = (1<<12) - 2 + (i_level_code & 1);
105 bs_write( s, i_level_prefix + 1, 1 );
106 bs_write( s, i_level_prefix - 3, i_level_code & ((1<<(i_level_prefix-3))-1) );
108 if( i_suffix_length == 0 )
110 if( abs_level > next_suffix[i_suffix_length] )
112 return i_suffix_length;
115 static int block_residual_write_cavlc( x264_t *h, bs_t *s, int i_ctxBlockCat, int16_t *l, int nC )
117 static const uint8_t ctz_index[8] = {3,0,1,0,2,0,1,0};
118 static const int count_cat[5] = {16, 15, 16, 4, 15};
119 x264_run_level_t runlevel;
120 int i_trailing, i_total_zero, i_suffix_length, i;
124 /* level and run and total */
125 /* set these to 2 to allow branchless i_trailing calculation */
126 runlevel.level[1] = 2;
127 runlevel.level[2] = 2;
128 i_total = h->quantf.coeff_level_run[i_ctxBlockCat]( l, &runlevel );
129 i_total_zero = runlevel.last + 1 - i_total;
131 i_trailing = ((((runlevel.level[0]+1) | (1-runlevel.level[0])) >> 31) & 1) // abs(runlevel.level[0])>1
132 | ((((runlevel.level[1]+1) | (1-runlevel.level[1])) >> 31) & 2)
133 | ((((runlevel.level[2]+1) | (1-runlevel.level[2])) >> 31) & 4);
134 i_trailing = ctz_index[i_trailing];
135 i_sign = ((runlevel.level[2] >> 31) & 1)
136 | ((runlevel.level[1] >> 31) & 2)
137 | ((runlevel.level[0] >> 31) & 4);
138 i_sign >>= 3-i_trailing;
141 bs_write_vlc( s, x264_coeff_token[nC][i_total*4+i_trailing-4] );
143 i_suffix_length = i_total > 10 && i_trailing < 3;
144 bs_write( s, i_trailing, i_sign );
146 if( i_trailing < i_total )
148 int16_t val = runlevel.level[i_trailing];
149 int16_t val_original = runlevel.level[i_trailing]+LEVEL_TABLE_SIZE/2;
151 val -= (val>>15)|1; /* as runlevel.level[i] can't be 1 for the first one if i_trailing < 3 */
152 val += LEVEL_TABLE_SIZE/2;
154 if( (unsigned)val_original < LEVEL_TABLE_SIZE )
156 bs_write_vlc( s, x264_level_token[i_suffix_length][val] );
157 i_suffix_length = x264_level_token[i_suffix_length][val_original].i_next;
160 i_suffix_length = block_residual_write_cavlc_escape( h, s, i_suffix_length, val-LEVEL_TABLE_SIZE/2 );
161 for( i = i_trailing+1; i < i_total; i++ )
163 val = runlevel.level[i] + LEVEL_TABLE_SIZE/2;
164 if( (unsigned)val < LEVEL_TABLE_SIZE )
166 bs_write_vlc( s, x264_level_token[i_suffix_length][val] );
167 i_suffix_length = x264_level_token[i_suffix_length][val].i_next;
170 i_suffix_length = block_residual_write_cavlc_escape( h, s, i_suffix_length, val-LEVEL_TABLE_SIZE/2 );
174 if( i_total < count_cat[i_ctxBlockCat] )
176 if( i_ctxBlockCat == DCT_CHROMA_DC )
177 bs_write_vlc( s, x264_total_zeros_dc[i_total-1][i_total_zero] );
179 bs_write_vlc( s, x264_total_zeros[i_total-1][i_total_zero] );
182 for( i = 0; i < i_total-1 && i_total_zero > 0; i++ )
184 int i_zl = X264_MIN( i_total_zero, 7 );
185 bs_write_vlc( s, x264_run_before[i_zl-1][runlevel.run[i]] );
186 i_total_zero -= runlevel.run[i];
192 static const uint8_t ct_index[17] = {0,0,1,1,2,2,2,2,3,3,3,3,3,3,3,3,3};
194 #define block_residual_write_cavlc(h,s,cat,idx,l)\
196 int nC = cat == DCT_CHROMA_DC ? 4 : ct_index[x264_mb_predict_non_zero_code( h, cat == DCT_LUMA_DC ? 0 : idx )];\
197 uint8_t *nnz = &h->mb.cache.non_zero_count[x264_scan8[idx]];\
199 bs_write_vlc( s, x264_coeff0_token[nC] );\
201 *nnz = block_residual_write_cavlc(h,s,cat,l,nC);\
204 static void cavlc_qp_delta( x264_t *h, bs_t *s )
206 int i_dqp = h->mb.i_qp - h->mb.i_last_qp;
208 /* Avoid writing a delta quant if we have an empty i16x16 block, e.g. in a completely flat background area */
209 if( h->mb.i_type == I_16x16 && !(h->mb.i_cbp_luma | h->mb.i_cbp_chroma)
210 && !h->mb.cache.non_zero_count[x264_scan8[24]] )
213 h->mb.i_qp = h->mb.i_last_qp;
222 else if( i_dqp > 25 )
225 bs_write_se( s, i_dqp );
228 static void cavlc_mb_mvd( x264_t *h, bs_t *s, int i_list, int idx, int width )
230 ALIGNED_4( int16_t mvp[2] );
231 x264_mb_predict_mv( h, i_list, idx, width, mvp );
232 bs_write_se( s, h->mb.cache.mv[i_list][x264_scan8[idx]][0] - mvp[0] );
233 bs_write_se( s, h->mb.cache.mv[i_list][x264_scan8[idx]][1] - mvp[1] );
236 static inline void cavlc_mb8x8_mvd( x264_t *h, bs_t *s, int i )
238 switch( h->mb.i_sub_partition[i] )
241 cavlc_mb_mvd( h, s, 0, 4*i, 2 );
244 cavlc_mb_mvd( h, s, 0, 4*i+0, 2 );
245 cavlc_mb_mvd( h, s, 0, 4*i+2, 2 );
248 cavlc_mb_mvd( h, s, 0, 4*i+0, 1 );
249 cavlc_mb_mvd( h, s, 0, 4*i+1, 1 );
252 cavlc_mb_mvd( h, s, 0, 4*i+0, 1 );
253 cavlc_mb_mvd( h, s, 0, 4*i+1, 1 );
254 cavlc_mb_mvd( h, s, 0, 4*i+2, 1 );
255 cavlc_mb_mvd( h, s, 0, 4*i+3, 1 );
260 static inline void x264_macroblock_luma_write_cavlc( x264_t *h, bs_t *s, int i8start, int i8end )
263 if( h->mb.b_transform_8x8 )
265 /* shuffle 8x8 dct coeffs into 4x4 lists */
266 for( i8 = i8start; i8 <= i8end; i8++ )
267 if( h->mb.i_cbp_luma & (1 << i8) )
268 h->zigzagf.interleave_8x8_cavlc( h->dct.luma4x4[i8*4], h->dct.luma8x8[i8], &h->mb.cache.non_zero_count[x264_scan8[i8*4]] );
271 for( i8 = i8start; i8 <= i8end; i8++ )
272 if( h->mb.i_cbp_luma & (1 << i8) )
273 for( i4 = 0; i4 < 4; i4++ )
274 block_residual_write_cavlc( h, s, DCT_LUMA_4x4, i4+i8*4, h->dct.luma4x4[i4+i8*4] );
277 /*****************************************************************************
278 * x264_macroblock_write:
279 *****************************************************************************/
280 void x264_macroblock_write_cavlc( x264_t *h, bs_t *s )
282 const int i_mb_type = h->mb.i_type;
283 static const int i_offsets[3] = {5,23,0};
284 int i_mb_i_offset = i_offsets[h->sh.i_type];
288 const int i_mb_pos_start = bs_pos( s );
293 && (!(h->mb.i_mb_y & 1) || IS_SKIP(h->mb.type[h->mb.i_mb_xy - h->mb.i_mb_stride])) )
295 bs_write1( s, h->mb.b_interlaced );
299 if( i_mb_type == I_PCM )
301 uint8_t *p_start = s->p_start;
302 bs_write_ue( s, i_mb_i_offset + 25 );
303 i_mb_pos_tex = bs_pos( s );
304 h->stat.frame.i_mv_bits += i_mb_pos_tex - i_mb_pos_start;
308 memcpy( s->p, h->mb.pic.p_fenc[0], 256 );
310 for( i = 0; i < 8; i++ )
311 memcpy( s->p + i*8, h->mb.pic.p_fenc[1] + i*FENC_STRIDE, 8 );
313 for( i = 0; i < 8; i++ )
314 memcpy( s->p + i*8, h->mb.pic.p_fenc[2] + i*FENC_STRIDE, 8 );
317 bs_init( s, s->p, s->p_end - s->p );
318 s->p_start = p_start;
320 /* if PCM is chosen, we need to store reconstructed frame data */
321 h->mc.copy[PIXEL_16x16]( h->mb.pic.p_fdec[0], FDEC_STRIDE, h->mb.pic.p_fenc[0], FENC_STRIDE, 16 );
322 h->mc.copy[PIXEL_8x8] ( h->mb.pic.p_fdec[1], FDEC_STRIDE, h->mb.pic.p_fenc[1], FENC_STRIDE, 8 );
323 h->mc.copy[PIXEL_8x8] ( h->mb.pic.p_fdec[2], FDEC_STRIDE, h->mb.pic.p_fenc[2], FENC_STRIDE, 8 );
325 h->stat.frame.i_tex_bits += bs_pos(s) - i_mb_pos_tex;
334 if( i_mb_type == I_4x4 || i_mb_type == I_8x8 )
336 int di = i_mb_type == I_8x8 ? 4 : 1;
337 bs_write_ue( s, i_mb_i_offset + 0 );
338 if( h->pps->b_transform_8x8_mode )
339 bs_write1( s, h->mb.b_transform_8x8 );
341 /* Prediction: Luma */
342 for( i = 0; i < 16; i += di )
344 int i_pred = x264_mb_predict_intra4x4_mode( h, i );
345 int i_mode = x264_mb_pred_mode4x4_fix( h->mb.cache.intra4x4_pred_mode[x264_scan8[i]] );
347 if( i_pred == i_mode )
348 bs_write1( s, 1 ); /* b_prev_intra4x4_pred_mode */
350 bs_write( s, 4, i_mode - (i_mode > i_pred) );
352 bs_write_ue( s, x264_mb_pred_mode8x8c_fix[ h->mb.i_chroma_pred_mode ] );
354 else if( i_mb_type == I_16x16 )
356 bs_write_ue( s, i_mb_i_offset + 1 + x264_mb_pred_mode16x16_fix[h->mb.i_intra16x16_pred_mode] +
357 h->mb.i_cbp_chroma * 4 + ( h->mb.i_cbp_luma == 0 ? 0 : 12 ) );
358 bs_write_ue( s, x264_mb_pred_mode8x8c_fix[ h->mb.i_chroma_pred_mode ] );
360 else if( i_mb_type == P_L0 )
362 if( h->mb.i_partition == D_16x16 )
366 if( h->mb.pic.i_fref[0] > 1 )
367 bs_write_te( s, h->mb.pic.i_fref[0] - 1, h->mb.cache.ref[0][x264_scan8[0]] );
368 cavlc_mb_mvd( h, s, 0, 0, 4 );
370 else if( h->mb.i_partition == D_16x8 )
373 if( h->mb.pic.i_fref[0] > 1 )
375 bs_write_te( s, h->mb.pic.i_fref[0] - 1, h->mb.cache.ref[0][x264_scan8[0]] );
376 bs_write_te( s, h->mb.pic.i_fref[0] - 1, h->mb.cache.ref[0][x264_scan8[8]] );
378 cavlc_mb_mvd( h, s, 0, 0, 4 );
379 cavlc_mb_mvd( h, s, 0, 8, 4 );
381 else if( h->mb.i_partition == D_8x16 )
384 if( h->mb.pic.i_fref[0] > 1 )
386 bs_write_te( s, h->mb.pic.i_fref[0] - 1, h->mb.cache.ref[0][x264_scan8[0]] );
387 bs_write_te( s, h->mb.pic.i_fref[0] - 1, h->mb.cache.ref[0][x264_scan8[4]] );
389 cavlc_mb_mvd( h, s, 0, 0, 2 );
390 cavlc_mb_mvd( h, s, 0, 4, 2 );
393 else if( i_mb_type == P_8x8 )
396 if( (h->mb.cache.ref[0][x264_scan8[0]] | h->mb.cache.ref[0][x264_scan8[ 4]] |
397 h->mb.cache.ref[0][x264_scan8[8]] | h->mb.cache.ref[0][x264_scan8[12]]) == 0 )
409 if( h->param.analyse.inter & X264_ANALYSE_PSUB8x8 )
410 for( i = 0; i < 4; i++ )
411 bs_write_ue( s, sub_mb_type_p_to_golomb[ h->mb.i_sub_partition[i] ] );
413 bs_write( s, 4, 0xf );
418 bs_write_te( s, h->mb.pic.i_fref[0] - 1, h->mb.cache.ref[0][x264_scan8[0]] );
419 bs_write_te( s, h->mb.pic.i_fref[0] - 1, h->mb.cache.ref[0][x264_scan8[4]] );
420 bs_write_te( s, h->mb.pic.i_fref[0] - 1, h->mb.cache.ref[0][x264_scan8[8]] );
421 bs_write_te( s, h->mb.pic.i_fref[0] - 1, h->mb.cache.ref[0][x264_scan8[12]] );
424 for( i = 0; i < 4; i++ )
425 cavlc_mb8x8_mvd( h, s, i );
427 else if( i_mb_type == B_8x8 )
429 bs_write_ue( s, 22 );
432 for( i = 0; i < 4; i++ )
433 bs_write_ue( s, sub_mb_type_b_to_golomb[ h->mb.i_sub_partition[i] ] );
436 if( h->mb.pic.i_fref[0] > 1 )
437 for( i = 0; i < 4; i++ )
438 if( x264_mb_partition_listX_table[0][ h->mb.i_sub_partition[i] ] )
439 bs_write_te( s, h->mb.pic.i_fref[0] - 1, h->mb.cache.ref[0][x264_scan8[i*4]] );
440 if( h->mb.pic.i_fref[1] > 1 )
441 for( i = 0; i < 4; i++ )
442 if( x264_mb_partition_listX_table[1][ h->mb.i_sub_partition[i] ] )
443 bs_write_te( s, h->mb.pic.i_fref[1] - 1, h->mb.cache.ref[1][x264_scan8[i*4]] );
446 for( i = 0; i < 4; i++ )
447 if( x264_mb_partition_listX_table[0][ h->mb.i_sub_partition[i] ] )
448 cavlc_mb_mvd( h, s, 0, 4*i, 2 );
449 for( i = 0; i < 4; i++ )
450 if( x264_mb_partition_listX_table[1][ h->mb.i_sub_partition[i] ] )
451 cavlc_mb_mvd( h, s, 1, 4*i, 2 );
453 else if( i_mb_type != B_DIRECT )
457 const uint8_t (*b_list)[2] = x264_mb_type_list_table[i_mb_type];
458 const int i_ref0_max = h->mb.pic.i_fref[0] - 1;
459 const int i_ref1_max = h->mb.pic.i_fref[1] - 1;
461 bs_write_ue( s, mb_type_b_to_golomb[ h->mb.i_partition - D_16x8 ][ i_mb_type - B_L0_L0 ] );
462 switch( h->mb.i_partition )
465 if( i_ref0_max && b_list[0][0] ) bs_write_te( s, i_ref0_max, h->mb.cache.ref[0][x264_scan8[0]] );
466 if( i_ref1_max && b_list[1][0] ) bs_write_te( s, i_ref1_max, h->mb.cache.ref[1][x264_scan8[0]] );
467 if( b_list[0][0] ) cavlc_mb_mvd( h, s, 0, 0, 4 );
468 if( b_list[1][0] ) cavlc_mb_mvd( h, s, 1, 0, 4 );
471 if( i_ref0_max && b_list[0][0] ) bs_write_te( s, i_ref0_max, h->mb.cache.ref[0][x264_scan8[0]] );
472 if( i_ref0_max && b_list[0][1] ) bs_write_te( s, i_ref0_max, h->mb.cache.ref[0][x264_scan8[8]] );
473 if( i_ref1_max && b_list[1][0] ) bs_write_te( s, i_ref1_max, h->mb.cache.ref[1][x264_scan8[0]] );
474 if( i_ref1_max && b_list[1][1] ) bs_write_te( s, i_ref1_max, h->mb.cache.ref[1][x264_scan8[8]] );
475 if( b_list[0][0] ) cavlc_mb_mvd( h, s, 0, 0, 4 );
476 if( b_list[0][1] ) cavlc_mb_mvd( h, s, 0, 8, 4 );
477 if( b_list[1][0] ) cavlc_mb_mvd( h, s, 1, 0, 4 );
478 if( b_list[1][1] ) cavlc_mb_mvd( h, s, 1, 8, 4 );
481 if( i_ref0_max && b_list[0][0] ) bs_write_te( s, i_ref0_max, h->mb.cache.ref[0][x264_scan8[0]] );
482 if( i_ref0_max && b_list[0][1] ) bs_write_te( s, i_ref0_max, h->mb.cache.ref[0][x264_scan8[4]] );
483 if( i_ref1_max && b_list[1][0] ) bs_write_te( s, i_ref1_max, h->mb.cache.ref[1][x264_scan8[0]] );
484 if( i_ref1_max && b_list[1][1] ) bs_write_te( s, i_ref1_max, h->mb.cache.ref[1][x264_scan8[4]] );
485 if( b_list[0][0] ) cavlc_mb_mvd( h, s, 0, 0, 2 );
486 if( b_list[0][1] ) cavlc_mb_mvd( h, s, 0, 4, 2 );
487 if( b_list[1][0] ) cavlc_mb_mvd( h, s, 1, 0, 2 );
488 if( b_list[1][1] ) cavlc_mb_mvd( h, s, 1, 4, 2 );
492 else //if( i_mb_type == B_DIRECT )
496 i_mb_pos_tex = bs_pos( s );
497 h->stat.frame.i_mv_bits += i_mb_pos_tex - i_mb_pos_start;
500 /* Coded block patern */
501 if( i_mb_type == I_4x4 || i_mb_type == I_8x8 )
502 bs_write_ue( s, intra4x4_cbp_to_golomb[( h->mb.i_cbp_chroma << 4 )|h->mb.i_cbp_luma] );
503 else if( i_mb_type != I_16x16 )
504 bs_write_ue( s, inter_cbp_to_golomb[( h->mb.i_cbp_chroma << 4 )|h->mb.i_cbp_luma] );
506 /* transform size 8x8 flag */
507 if( x264_mb_transform_8x8_allowed( h ) && h->mb.i_cbp_luma )
508 bs_write1( s, h->mb.b_transform_8x8 );
511 if( i_mb_type == I_16x16 )
513 cavlc_qp_delta( h, s );
516 block_residual_write_cavlc( h, s, DCT_LUMA_DC, 24 , h->dct.luma16x16_dc );
519 if( h->mb.i_cbp_luma )
520 for( i = 0; i < 16; i++ )
521 block_residual_write_cavlc( h, s, DCT_LUMA_AC, i, h->dct.luma4x4[i]+1 );
523 else if( h->mb.i_cbp_luma | h->mb.i_cbp_chroma )
525 cavlc_qp_delta( h, s );
526 x264_macroblock_luma_write_cavlc( h, s, 0, 3 );
528 if( h->mb.i_cbp_chroma )
530 /* Chroma DC residual present */
531 block_residual_write_cavlc( h, s, DCT_CHROMA_DC, 25, h->dct.chroma_dc[0] );
532 block_residual_write_cavlc( h, s, DCT_CHROMA_DC, 26, h->dct.chroma_dc[1] );
533 if( h->mb.i_cbp_chroma&0x02 ) /* Chroma AC residual present */
534 for( i = 16; i < 24; i++ )
535 block_residual_write_cavlc( h, s, DCT_CHROMA_AC, i, h->dct.luma4x4[i]+1 );
539 h->stat.frame.i_tex_bits += bs_pos(s) - i_mb_pos_tex;
544 /*****************************************************************************
545 * RD only; doesn't generate a valid bitstream
546 * doesn't write cbp or chroma dc (I don't know how much this matters)
547 * doesn't write ref (never varies between calls, so no point in doing so)
548 * only writes subpartition for p8x8, needed for sub-8x8 mode decision RDO
549 * works on all partition sizes except 16x16
550 *****************************************************************************/
551 static int x264_partition_size_cavlc( x264_t *h, int i8, int i_pixel )
553 const int i_mb_type = h->mb.i_type;
554 int b_8x16 = h->mb.i_partition == D_8x16;
556 h->out.bs.i_bits_encoded = 0;
558 if( i_mb_type == P_8x8 )
560 cavlc_mb8x8_mvd( h, &h->out.bs, i8 );
561 bs_write_ue( &h->out.bs, sub_mb_type_p_to_golomb[ h->mb.i_sub_partition[i8] ] );
563 else if( i_mb_type == P_L0 )
564 cavlc_mb_mvd( h, &h->out.bs, 0, 4*i8, 4>>b_8x16 );
565 else if( i_mb_type > B_DIRECT && i_mb_type < B_8x8 )
567 if( x264_mb_type_list_table[ i_mb_type ][0][!!i8] ) cavlc_mb_mvd( h, &h->out.bs, 0, 4*i8, 4>>b_8x16 );
568 if( x264_mb_type_list_table[ i_mb_type ][1][!!i8] ) cavlc_mb_mvd( h, &h->out.bs, 1, 4*i8, 4>>b_8x16 );
570 else //if( i_mb_type == B_8x8 )
572 if( x264_mb_partition_listX_table[0][ h->mb.i_sub_partition[i8] ] )
573 cavlc_mb_mvd( h, &h->out.bs, 0, 4*i8, 2 );
574 if( x264_mb_partition_listX_table[1][ h->mb.i_sub_partition[i8] ] )
575 cavlc_mb_mvd( h, &h->out.bs, 1, 4*i8, 2 );
578 for( j = (i_pixel < PIXEL_8x8); j >= 0; j-- )
580 x264_macroblock_luma_write_cavlc( h, &h->out.bs, i8, i8 );
581 block_residual_write_cavlc( h, &h->out.bs, DCT_CHROMA_AC, 16+i8, h->dct.luma4x4[16+i8]+1 );
582 block_residual_write_cavlc( h, &h->out.bs, DCT_CHROMA_AC, 20+i8, h->dct.luma4x4[20+i8]+1 );
583 i8 += x264_pixel_size[i_pixel].h >> 3;
586 return h->out.bs.i_bits_encoded;
589 static int x264_subpartition_size_cavlc( x264_t *h, int i4, int i_pixel )
591 int b_8x4 = i_pixel == PIXEL_8x4;
592 h->out.bs.i_bits_encoded = 0;
593 cavlc_mb_mvd( h, &h->out.bs, 0, i4, 1+b_8x4 );
594 block_residual_write_cavlc( h, &h->out.bs, DCT_LUMA_4x4, i4, h->dct.luma4x4[i4] );
595 if( i_pixel != PIXEL_4x4 )
598 block_residual_write_cavlc( h, &h->out.bs, DCT_LUMA_4x4, i4, h->dct.luma4x4[i4] );
601 return h->out.bs.i_bits_encoded;
604 static int cavlc_intra4x4_pred_size( x264_t *h, int i4, int i_mode )
606 if( x264_mb_predict_intra4x4_mode( h, i4 ) == x264_mb_pred_mode4x4_fix( i_mode ) )
612 static int x264_partition_i8x8_size_cavlc( x264_t *h, int i8, int i_mode )
614 h->out.bs.i_bits_encoded = cavlc_intra4x4_pred_size( h, 4*i8, i_mode );
615 bs_write_ue( &h->out.bs, intra4x4_cbp_to_golomb[( h->mb.i_cbp_chroma << 4 )|h->mb.i_cbp_luma] );
616 x264_macroblock_luma_write_cavlc( h, &h->out.bs, i8, i8 );
617 return h->out.bs.i_bits_encoded;
620 static int x264_partition_i4x4_size_cavlc( x264_t *h, int i4, int i_mode )
622 h->out.bs.i_bits_encoded = cavlc_intra4x4_pred_size( h, i4, i_mode );
623 block_residual_write_cavlc( h, &h->out.bs, DCT_LUMA_4x4, i4, h->dct.luma4x4[i4] );
624 return h->out.bs.i_bits_encoded;
627 static int x264_i8x8_chroma_size_cavlc( x264_t *h )
629 h->out.bs.i_bits_encoded = bs_size_ue( x264_mb_pred_mode8x8c_fix[ h->mb.i_chroma_pred_mode ] );
630 if( h->mb.i_cbp_chroma )
632 block_residual_write_cavlc( h, &h->out.bs, DCT_CHROMA_DC, 25, h->dct.chroma_dc[0] );
633 block_residual_write_cavlc( h, &h->out.bs, DCT_CHROMA_DC, 26, h->dct.chroma_dc[1] );
635 if( h->mb.i_cbp_chroma == 2 )
638 for( i = 16; i < 24; i++ )
639 block_residual_write_cavlc( h, &h->out.bs, DCT_CHROMA_AC, i, h->dct.luma4x4[i]+1 );
642 return h->out.bs.i_bits_encoded;