1 /*****************************************************************************
2 * cavlc.c: cavlc bitstream writing
3 *****************************************************************************
4 * Copyright (C) 2003-2011 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.
24 * This program is also available under a commercial proprietary license.
25 * For more information, contact us at licensing@x264.com.
26 *****************************************************************************/
28 #include "common/common.h"
29 #include "macroblock.h"
35 static const uint8_t intra4x4_cbp_to_golomb[48]=
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
41 static const uint8_t inter_cbp_to_golomb[48]=
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
47 static const uint8_t mb_type_b_to_golomb[3][9]=
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 */
53 static const uint8_t sub_mb_type_p_to_golomb[4]=
57 static const uint8_t sub_mb_type_b_to_golomb[13]=
59 10, 4, 5, 1, 11, 6, 7, 2, 12, 8, 9, 3, 0
62 #define bs_write_vlc(s,v) bs_write( s, (v).i_size, (v).i_bits )
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 )
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 )
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)) );
82 i_level_code -= 15 << i_suffix_length;
83 if( i_suffix_length == 0 )
86 /* If the prefix size exceeds 15, High Profile is required. */
87 if( i_level_code >= 1<<12 )
89 if( h->sps->i_profile_idc >= PROFILE_HIGH )
91 while( i_level_code > 1<<(i_level_prefix-3) )
93 i_level_code -= 1<<(i_level_prefix-3);
100 /* Weight highly against overflows. */
101 s->i_bits_encoded += 2000;
103 /* We've had an overflow; note it down and re-encode the MB later. */
104 h->mb.b_overflow = 1;
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) );
111 if( i_suffix_length == 0 )
113 if( abs_level > next_suffix[i_suffix_length] )
115 return i_suffix_length;
118 static int block_residual_write_cavlc_internal( x264_t *h, int ctx_block_cat, dctcoef *l, int nC )
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;
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;
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;
145 bs_write_vlc( s, x264_coeff_token[nC][i_total-1][i_trailing] );
147 i_suffix_length = i_total > 10 && i_trailing < 3;
148 bs_write( s, i_trailing, i_sign );
150 if( i_trailing < i_total )
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;
157 if( (unsigned)val_original < LEVEL_TABLE_SIZE )
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;
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++ )
166 val = runlevel.level[i] + LEVEL_TABLE_SIZE/2;
167 if( (unsigned)val < LEVEL_TABLE_SIZE )
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;
173 i_suffix_length = block_residual_write_cavlc_escape( h, i_suffix_length, val-LEVEL_TABLE_SIZE/2 );
177 if( (uint8_t)i_total < count_cat[ctx_block_cat] )
179 if( ctx_block_cat == DCT_CHROMA_DC )
180 bs_write_vlc( s, x264_total_zeros_dc[i_total-1][i_total_zero] );
182 bs_write_vlc( s, x264_total_zeros[i_total-1][i_total_zero] );
185 for( int i = 0; i < i_total-1 && i_total_zero > 0; i++ )
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];
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};
197 #define block_residual_write_cavlc(h,cat,idx,l)\
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]];\
202 bs_write_vlc( &h->out.bs, x264_coeff0_token[nC] );\
204 *nnz = block_residual_write_cavlc_internal(h,cat,l,nC);\
207 static void cavlc_qp_delta( x264_t *h )
209 bs_t *s = &h->out.bs;
210 int i_dqp = h->mb.i_qp - h->mb.i_last_qp;
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]] )
217 h->mb.i_qp = h->mb.i_last_qp;
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;
229 bs_write_se( s, i_dqp );
232 static void cavlc_mb_mvd( x264_t *h, int i_list, int idx, int width )
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] );
241 static inline void cavlc_mb8x8_mvd( x264_t *h, int i )
243 switch( h->mb.i_sub_partition[i] )
246 cavlc_mb_mvd( h, 0, 4*i, 2 );
249 cavlc_mb_mvd( h, 0, 4*i+0, 2 );
250 cavlc_mb_mvd( h, 0, 4*i+2, 2 );
253 cavlc_mb_mvd( h, 0, 4*i+0, 1 );
254 cavlc_mb_mvd( h, 0, 4*i+1, 1 );
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 );
265 static inline void x264_macroblock_luma_write_cavlc( x264_t *h, int i8start, int i8end )
267 if( h->mb.b_transform_8x8 )
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]] );
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] );
281 /*****************************************************************************
282 * x264_macroblock_write:
283 *****************************************************************************/
284 void x264_macroblock_write_cavlc( x264_t *h )
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];
292 s->i_bits_encoded = 0;
294 const int i_mb_pos_start = bs_pos( s );
299 && (!(h->mb.i_mb_y & 1) || IS_SKIP(h->mb.type[h->mb.i_mb_xy - h->mb.i_mb_stride])) )
301 bs_write1( s, h->mb.b_interlaced );
305 if( i_mb_type == I_PCM )
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;
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] );
321 bs_init( s, s->p, s->p_end - s->p );
322 s->p_start = p_start;
324 h->stat.frame.i_tex_bits += bs_pos(s) - i_mb_pos_tex;
333 if( i_mb_type == I_4x4 || i_mb_type == I_8x8 )
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 );
340 /* Prediction: Luma */
341 for( int i = 0; i < 16; i += di )
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]] );
346 if( i_pred == i_mode )
347 bs_write1( s, 1 ); /* b_prev_intra4x4_pred_mode */
349 bs_write( s, 4, i_mode - (i_mode > i_pred) );
351 bs_write_ue( s, x264_mb_pred_mode8x8c_fix[ h->mb.i_chroma_pred_mode ] );
353 else if( i_mb_type == I_16x16 )
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 ] );
359 else if( i_mb_type == P_L0 )
361 if( h->mb.i_partition == D_16x16 )
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 );
369 else if( h->mb.i_partition == D_16x8 )
372 if( h->mb.pic.i_fref[0] > 1 )
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]] );
377 cavlc_mb_mvd( h, 0, 0, 4 );
378 cavlc_mb_mvd( h, 0, 8, 4 );
380 else if( h->mb.i_partition == D_8x16 )
383 if( h->mb.pic.i_fref[0] > 1 )
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]] );
388 cavlc_mb_mvd( h, 0, 0, 2 );
389 cavlc_mb_mvd( h, 0, 4, 2 );
392 else if( i_mb_type == P_8x8 )
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 )
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] ] );
412 bs_write( s, 4, 0xf );
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]] );
423 for( int i = 0; i < 4; i++ )
424 cavlc_mb8x8_mvd( h, i );
426 else if( i_mb_type == B_8x8 )
428 bs_write_ue( s, 22 );
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] ] );
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]] );
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 );
452 else if( i_mb_type != B_DIRECT )
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;
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 )
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 );
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 )
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 );
481 else //if( h->mb.i_partition == D_8x16 )
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 );
490 else //if( i_mb_type == B_DIRECT )
494 i_mb_pos_tex = bs_pos( s );
495 h->stat.frame.i_mv_bits += i_mb_pos_tex - i_mb_pos_start;
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] );
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 );
509 if( i_mb_type == I_16x16 )
514 block_residual_write_cavlc( h, DCT_LUMA_DC, 24 , h->dct.luma16x16_dc );
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 );
521 else if( h->mb.i_cbp_luma | h->mb.i_cbp_chroma )
524 x264_macroblock_luma_write_cavlc( h, 0, 3 );
526 if( h->mb.i_cbp_chroma )
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 );
537 h->stat.frame.i_tex_bits += bs_pos(s) - i_mb_pos_tex;
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 )
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;
556 if( i_mb_type == P_8x8 )
558 cavlc_mb8x8_mvd( h, i8 );
559 bs_write_ue( s, sub_mb_type_p_to_golomb[ h->mb.i_sub_partition[i8] ] );
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 )
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 );
568 else //if( i_mb_type == B_8x8 )
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 );
576 for( j = (i_pixel < PIXEL_8x8); j >= 0; j-- )
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;
584 return h->out.bs.i_bits_encoded;
587 static int x264_subpartition_size_cavlc( x264_t *h, int i4, int i_pixel )
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 )
596 block_residual_write_cavlc( h, DCT_LUMA_4x4, i4, h->dct.luma4x4[i4] );
599 return h->out.bs.i_bits_encoded;
602 static int cavlc_intra4x4_pred_size( x264_t *h, int i4, int i_mode )
604 if( x264_mb_predict_intra4x4_mode( h, i4 ) == x264_mb_pred_mode4x4_fix( i_mode ) )
610 static int x264_partition_i8x8_size_cavlc( x264_t *h, int i8, int i_mode )
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;
618 static int x264_partition_i4x4_size_cavlc( x264_t *h, int i4, int i_mode )
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;
625 static int x264_i8x8_chroma_size_cavlc( x264_t *h )
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 )
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] );
633 if( h->mb.i_cbp_chroma == 2 )
635 for( int i = 16; i < 24; i++ )
636 block_residual_write_cavlc( h, DCT_CHROMA_AC, i, h->dct.luma4x4[i]+1 );
639 return h->out.bs.i_bits_encoded;