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>
9 * This program is free software; you can redistribute it and/or modify
10 * it under the terms of the GNU General Public License as published by
11 * the Free Software Foundation; either version 2 of the License, or
12 * (at your option) any later version.
14 * This program is distributed in the hope that it will be useful,
15 * but WITHOUT ANY WARRANTY; without even the implied warranty of
16 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
17 * GNU General Public License for more details.
19 * You should have received a copy of the GNU General Public License
20 * along with this program; if not, write to the Free Software
21 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02111, USA.
22 *****************************************************************************/
24 #include "common/common.h"
25 #include "macroblock.h"
27 static const uint8_t intra4x4_cbp_to_golomb[48]=
29 3, 29, 30, 17, 31, 18, 37, 8, 32, 38, 19, 9, 20, 10, 11, 2,
30 16, 33, 34, 21, 35, 22, 39, 4, 36, 40, 23, 5, 24, 6, 7, 1,
31 41, 42, 43, 25, 44, 26, 46, 12, 45, 47, 27, 13, 28, 14, 15, 0
33 static const uint8_t inter_cbp_to_golomb[48]=
35 0, 2, 3, 7, 4, 8, 17, 13, 5, 18, 9, 14, 10, 15, 16, 11,
36 1, 32, 33, 36, 34, 37, 44, 40, 35, 45, 38, 41, 39, 42, 43, 19,
37 6, 24, 25, 20, 26, 21, 46, 28, 27, 47, 22, 29, 23, 30, 31, 12
39 static const uint8_t mb_type_b_to_golomb[3][9]=
41 { 4, 8, 12, 10, 6, 14, 16, 18, 20 }, /* D_16x8 */
42 { 5, 9, 13, 11, 7, 15, 17, 19, 21 }, /* D_8x16 */
43 { 1, -1, -1, -1, 2, -1, -1, -1, 3 } /* D_16x16 */
45 static const uint8_t sub_mb_type_p_to_golomb[4]=
49 static const uint8_t sub_mb_type_b_to_golomb[13]=
51 10, 4, 5, 1, 11, 6, 7, 2, 12, 8, 9, 3, 0
54 #define BLOCK_INDEX_CHROMA_DC (-1)
55 #define BLOCK_INDEX_LUMA_DC (-2)
57 static inline void bs_write_vlc( bs_t *s, vlc_t v )
59 bs_write( s, v.i_size, v.i_bits );
62 /****************************************************************************
63 * block_residual_write_cavlc:
64 ****************************************************************************/
65 static void block_residual_write_cavlc( x264_t *h, bs_t *s, int i_idx, int16_t *l, int i_count )
67 int level[16], run[16];
68 int i_total, i_trailing;
75 /* first find i_last */
76 for( i_last = i_count-1; i_last >= 3; i_last -= 4 )
77 if( *(uint64_t*)(l+i_last-3) )
79 while( i_last >= 0 && l[i_last] == 0 )
85 i_total_zero = i_last + 1;
91 /* level and run and total */
95 level[idx] = l[i_last];
96 while( --i_last >= 0 && l[i_last] == 0 )
104 i_trailing = X264_MIN(3, idx);
105 for( idx = 0; idx < i_trailing; idx++ )
107 if( (unsigned)(level[idx]+1) > 2 )
113 i_sign |= level[idx] < 0;
118 if( i_idx == BLOCK_INDEX_CHROMA_DC )
119 bs_write_vlc( s, x264_coeff_token[4][i_total*4+i_trailing] );
122 /* x264_mb_predict_non_zero_code return 0 <-> (16+16+1)>>1 = 16 */
123 static const int ct_index[17] = {0,0,1,1,2,2,2,2,3,3,3,3,3,3,3,3,3 };
124 int nC = x264_mb_predict_non_zero_code( h, i_idx == BLOCK_INDEX_LUMA_DC ? 0 : i_idx );
125 bs_write_vlc( s, x264_coeff_token[ct_index[nC]][i_total*4+i_trailing] );
131 i_suffix_length = i_total > 10 && i_trailing < 3 ? 1 : 0;
133 bs_write( s, i_trailing, i_sign );
134 for( i = i_trailing; i < i_total; i++ )
136 int mask = level[i] >> 15;
137 int abs_level = (level[i]^mask)-mask;
138 int i_level_code = abs_level*2-mask-2;
140 if( i == i_trailing && i_trailing < 3 )
141 i_level_code -= 2; /* as level[i] can't be 1 for the first one if i_trailing < 3 */
143 if( ( i_level_code >> i_suffix_length ) < 14 )
144 bs_write( s, (i_level_code >> i_suffix_length) + 1 + i_suffix_length,
145 (1<<i_suffix_length) + (i_level_code & ((1<<i_suffix_length)-1)) );
146 else if( i_suffix_length == 0 && i_level_code < 30 )
147 bs_write( s, 19, (1<<4) + (i_level_code - 14) );
148 else if( i_suffix_length > 0 && ( i_level_code >> i_suffix_length ) == 14 )
149 bs_write( s, 15 + i_suffix_length,
150 (1<<i_suffix_length) + (i_level_code & ((1<<i_suffix_length)-1)) );
153 int i_level_prefix = 15;
154 i_level_code -= 15 << i_suffix_length;
155 if( i_suffix_length == 0 )
158 /* If the prefix size exceeds 15, High Profile is required. */
159 if( i_level_code >= 1<<12 )
161 if( h->sps->i_profile_idc >= PROFILE_HIGH )
163 while( i_level_code > 1<<(i_level_prefix-3) )
165 i_level_code -= 1<<(i_level_prefix-3);
172 /* Weight highly against overflows. */
173 s->i_bits_encoded += 1000000;
175 x264_log(h, X264_LOG_WARNING, "OVERFLOW levelcode=%d is only allowed in High Profile", i_level_code );
176 /* clip level, preserving sign */
177 i_level_code = (1<<12) - 2 + (i_level_code & 1);
181 bs_write( s, i_level_prefix + 1, 1 );
182 bs_write( s, i_level_prefix - 3, i_level_code & ((1<<(i_level_prefix-3))-1) );
185 if( i_suffix_length == 0 )
187 if( abs_level > (3 << (i_suffix_length-1)) && i_suffix_length < 6 )
191 if( i_total < i_count )
193 if( i_idx == BLOCK_INDEX_CHROMA_DC )
194 bs_write_vlc( s, x264_total_zeros_dc[i_total-1][i_total_zero] );
196 bs_write_vlc( s, x264_total_zeros[i_total-1][i_total_zero] );
199 for( i = 0; i < i_total-1 && i_total_zero > 0; i++ )
201 int i_zl = X264_MIN( i_total_zero - 1, 6 );
202 bs_write_vlc( s, x264_run_before[i_zl][run[i]] );
203 i_total_zero -= run[i];
207 static void cavlc_qp_delta( x264_t *h, bs_t *s )
209 int i_dqp = h->mb.i_qp - h->mb.i_last_qp;
211 /* Avoid writing a delta quant if we have an empty i16x16 block, e.g. in a completely flat background area */
212 if( h->mb.i_type == I_16x16 && !(h->mb.i_cbp_luma | h->mb.i_cbp_chroma)
213 && !array_non_zero(h->dct.luma16x16_dc) )
216 h->mb.i_qp = h->mb.i_last_qp;
225 else if( i_dqp > 25 )
228 bs_write_se( s, i_dqp );
231 static void cavlc_mb_mvd( x264_t *h, bs_t *s, int i_list, int idx, int width )
233 DECLARE_ALIGNED_4( int16_t mvp[2] );
234 x264_mb_predict_mv( h, i_list, idx, width, mvp );
235 bs_write_se( s, h->mb.cache.mv[i_list][x264_scan8[idx]][0] - mvp[0] );
236 bs_write_se( s, h->mb.cache.mv[i_list][x264_scan8[idx]][1] - mvp[1] );
239 static void cavlc_mb8x8_mvd( x264_t *h, bs_t *s, int i_list, int i )
241 if( !x264_mb_partition_listX_table[i_list][ h->mb.i_sub_partition[i] ] )
244 switch( h->mb.i_sub_partition[i] )
249 cavlc_mb_mvd( h, s, i_list, 4*i, 2 );
254 cavlc_mb_mvd( h, s, i_list, 4*i+0, 2 );
255 cavlc_mb_mvd( h, s, i_list, 4*i+2, 2 );
260 cavlc_mb_mvd( h, s, i_list, 4*i+0, 1 );
261 cavlc_mb_mvd( h, s, i_list, 4*i+1, 1 );
266 cavlc_mb_mvd( h, s, i_list, 4*i+0, 1 );
267 cavlc_mb_mvd( h, s, i_list, 4*i+1, 1 );
268 cavlc_mb_mvd( h, s, i_list, 4*i+2, 1 );
269 cavlc_mb_mvd( h, s, i_list, 4*i+3, 1 );
274 static inline void x264_macroblock_luma_write_cavlc( x264_t *h, bs_t *s, int i8start, int i8end )
277 if( h->mb.b_transform_8x8 )
279 /* shuffle 8x8 dct coeffs into 4x4 lists */
280 for( i8 = i8start; i8 <= i8end; i8++ )
281 if( h->mb.i_cbp_luma & (1 << i8) )
282 h->zigzagf.interleave_8x8_cavlc( h->dct.luma4x4[i8*4], h->dct.luma8x8[i8] );
285 for( i8 = i8start; i8 <= i8end; i8++ )
286 if( h->mb.i_cbp_luma & (1 << i8) )
287 for( i4 = 0; i4 < 4; i4++ )
289 h->mb.cache.non_zero_count[x264_scan8[i4+i8*4]] = array_non_zero_count( h->dct.luma4x4[i4+i8*4] );
290 block_residual_write_cavlc( h, s, i4+i8*4, h->dct.luma4x4[i4+i8*4], 16 );
294 /*****************************************************************************
295 * x264_macroblock_write:
296 *****************************************************************************/
297 void x264_macroblock_write_cavlc( x264_t *h, bs_t *s )
299 const int i_mb_type = h->mb.i_type;
304 const int i_mb_pos_start = bs_pos( s );
308 switch( h->sh.i_type )
320 x264_log(h, X264_LOG_ERROR, "internal error or slice unsupported\n" );
325 && (!(h->mb.i_mb_y & 1) || IS_SKIP(h->mb.type[h->mb.i_mb_xy - h->mb.i_mb_stride])) )
327 bs_write1( s, h->mb.b_interlaced );
331 if( i_mb_type == I_PCM)
333 bs_write_ue( s, i_mb_i_offset + 25 );
334 i_mb_pos_tex = bs_pos( s );
335 h->stat.frame.i_mv_bits += i_mb_pos_tex - i_mb_pos_start;
339 memcpy( s->p, h->mb.pic.p_fenc[0], 256 );
341 for( i = 0; i < 8; i++ )
342 memcpy( s->p + i*8, h->mb.pic.p_fenc[1] + i*FENC_STRIDE, 8 );
344 for( i = 0; i < 8; i++ )
345 memcpy( s->p + i*8, h->mb.pic.p_fenc[2] + i*FENC_STRIDE, 8 );
348 /* if PCM is chosen, we need to store reconstructed frame data */
349 h->mc.copy[PIXEL_16x16]( h->mb.pic.p_fdec[0], FDEC_STRIDE, h->mb.pic.p_fenc[0], FENC_STRIDE, 16 );
350 h->mc.copy[PIXEL_8x8] ( h->mb.pic.p_fdec[1], FDEC_STRIDE, h->mb.pic.p_fenc[1], FENC_STRIDE, 8 );
351 h->mc.copy[PIXEL_8x8] ( h->mb.pic.p_fdec[2], FDEC_STRIDE, h->mb.pic.p_fenc[2], FENC_STRIDE, 8 );
353 h->stat.frame.i_tex_bits += bs_pos(s) - i_mb_pos_tex;
362 if( i_mb_type == I_4x4 || i_mb_type == I_8x8 )
364 int di = i_mb_type == I_8x8 ? 4 : 1;
365 bs_write_ue( s, i_mb_i_offset + 0 );
366 if( h->pps->b_transform_8x8_mode )
367 bs_write1( s, h->mb.b_transform_8x8 );
369 /* Prediction: Luma */
370 for( i = 0; i < 16; i += di )
372 int i_pred = x264_mb_predict_intra4x4_mode( h, i );
373 int i_mode = x264_mb_pred_mode4x4_fix( h->mb.cache.intra4x4_pred_mode[x264_scan8[i]] );
375 if( i_pred == i_mode )
376 bs_write1( s, 1 ); /* b_prev_intra4x4_pred_mode */
378 bs_write( s, 4, i_mode - (i_mode > i_pred) );
380 bs_write_ue( s, x264_mb_pred_mode8x8c_fix[ h->mb.i_chroma_pred_mode ] );
382 else if( i_mb_type == I_16x16 )
384 bs_write_ue( s, i_mb_i_offset + 1 + x264_mb_pred_mode16x16_fix[h->mb.i_intra16x16_pred_mode] +
385 h->mb.i_cbp_chroma * 4 + ( h->mb.i_cbp_luma == 0 ? 0 : 12 ) );
386 bs_write_ue( s, x264_mb_pred_mode8x8c_fix[ h->mb.i_chroma_pred_mode ] );
388 else if( i_mb_type == P_L0 )
390 DECLARE_ALIGNED_4( int16_t mvp[2] );
392 if( h->mb.i_partition == D_16x16 )
396 if( h->mb.pic.i_fref[0] > 1 )
397 bs_write_te( s, h->mb.pic.i_fref[0] - 1, h->mb.cache.ref[0][x264_scan8[0]] );
398 x264_mb_predict_mv( h, 0, 0, 4, mvp );
399 bs_write_se( s, h->mb.cache.mv[0][x264_scan8[0]][0] - mvp[0] );
400 bs_write_se( s, h->mb.cache.mv[0][x264_scan8[0]][1] - mvp[1] );
402 else if( h->mb.i_partition == D_16x8 )
405 if( h->mb.pic.i_fref[0] > 1 )
407 bs_write_te( s, h->mb.pic.i_fref[0] - 1, h->mb.cache.ref[0][x264_scan8[0]] );
408 bs_write_te( s, h->mb.pic.i_fref[0] - 1, h->mb.cache.ref[0][x264_scan8[8]] );
411 x264_mb_predict_mv( h, 0, 0, 4, mvp );
412 bs_write_se( s, h->mb.cache.mv[0][x264_scan8[0]][0] - mvp[0] );
413 bs_write_se( s, h->mb.cache.mv[0][x264_scan8[0]][1] - mvp[1] );
415 x264_mb_predict_mv( h, 0, 8, 4, mvp );
416 bs_write_se( s, h->mb.cache.mv[0][x264_scan8[8]][0] - mvp[0] );
417 bs_write_se( s, h->mb.cache.mv[0][x264_scan8[8]][1] - mvp[1] );
419 else if( h->mb.i_partition == D_8x16 )
422 if( h->mb.pic.i_fref[0] > 1 )
424 bs_write_te( s, h->mb.pic.i_fref[0] - 1, h->mb.cache.ref[0][x264_scan8[0]] );
425 bs_write_te( s, h->mb.pic.i_fref[0] - 1, h->mb.cache.ref[0][x264_scan8[4]] );
428 x264_mb_predict_mv( h, 0, 0, 2, mvp );
429 bs_write_se( s, h->mb.cache.mv[0][x264_scan8[0]][0] - mvp[0] );
430 bs_write_se( s, h->mb.cache.mv[0][x264_scan8[0]][1] - mvp[1] );
432 x264_mb_predict_mv( h, 0, 4, 2, mvp );
433 bs_write_se( s, h->mb.cache.mv[0][x264_scan8[4]][0] - mvp[0] );
434 bs_write_se( s, h->mb.cache.mv[0][x264_scan8[4]][1] - mvp[1] );
437 else if( i_mb_type == P_8x8 )
440 if( (h->mb.cache.ref[0][x264_scan8[0]] | h->mb.cache.ref[0][x264_scan8[ 4]] |
441 h->mb.cache.ref[0][x264_scan8[8]] | h->mb.cache.ref[0][x264_scan8[12]]) == 0 )
453 if( h->param.analyse.inter & X264_ANALYSE_PSUB8x8 )
454 for( i = 0; i < 4; i++ )
455 bs_write_ue( s, sub_mb_type_p_to_golomb[ h->mb.i_sub_partition[i] ] );
457 bs_write( s, 4, 0xf );
460 if( h->mb.pic.i_fref[0] > 1 && b_sub_ref0 )
462 bs_write_te( s, h->mb.pic.i_fref[0] - 1, h->mb.cache.ref[0][x264_scan8[0]] );
463 bs_write_te( s, h->mb.pic.i_fref[0] - 1, h->mb.cache.ref[0][x264_scan8[4]] );
464 bs_write_te( s, h->mb.pic.i_fref[0] - 1, h->mb.cache.ref[0][x264_scan8[8]] );
465 bs_write_te( s, h->mb.pic.i_fref[0] - 1, h->mb.cache.ref[0][x264_scan8[12]] );
468 for( i = 0; i < 4; i++ )
469 cavlc_mb8x8_mvd( h, s, 0, i );
471 else if( i_mb_type == B_8x8 )
473 bs_write_ue( s, 22 );
476 for( i = 0; i < 4; i++ )
477 bs_write_ue( s, sub_mb_type_b_to_golomb[ h->mb.i_sub_partition[i] ] );
480 for( i = 0; i < 4; i++ )
481 if( x264_mb_partition_listX_table[0][ h->mb.i_sub_partition[i] ] )
482 bs_write_te( s, h->mb.pic.i_fref[0] - 1, h->mb.cache.ref[0][x264_scan8[i*4]] );
483 for( i = 0; i < 4; i++ )
484 if( x264_mb_partition_listX_table[1][ h->mb.i_sub_partition[i] ] )
485 bs_write_te( s, h->mb.pic.i_fref[1] - 1, h->mb.cache.ref[1][x264_scan8[i*4]] );
488 for( i = 0; i < 4; i++ )
489 cavlc_mb8x8_mvd( h, s, 0, i );
490 for( i = 0; i < 4; i++ )
491 cavlc_mb8x8_mvd( h, s, 1, i );
493 else if( i_mb_type != B_DIRECT )
498 DECLARE_ALIGNED_4( int16_t mvp[2] );
502 /* init ref list utilisations */
503 for( i = 0; i < 2; i++ )
505 b_list[0][i] = x264_mb_type_list0_table[i_mb_type][i];
506 b_list[1][i] = x264_mb_type_list1_table[i_mb_type][i];
509 bs_write_ue( s, mb_type_b_to_golomb[ h->mb.i_partition - D_16x8 ][ i_mb_type - B_L0_L0 ] );
511 for( i_list = 0; i_list < 2; i_list++ )
513 const int i_ref_max = (i_list == 0 ? h->mb.pic.i_fref[0] : h->mb.pic.i_fref[1]) - 1;
516 switch( h->mb.i_partition )
519 if( b_list[i_list][0] ) bs_write_te( s, i_ref_max, h->mb.cache.ref[i_list][x264_scan8[0]] );
522 if( b_list[i_list][0] ) bs_write_te( s, i_ref_max, h->mb.cache.ref[i_list][x264_scan8[0]] );
523 if( b_list[i_list][1] ) bs_write_te( s, i_ref_max, h->mb.cache.ref[i_list][x264_scan8[8]] );
526 if( b_list[i_list][0] ) bs_write_te( s, i_ref_max, h->mb.cache.ref[i_list][x264_scan8[0]] );
527 if( b_list[i_list][1] ) bs_write_te( s, i_ref_max, h->mb.cache.ref[i_list][x264_scan8[4]] );
531 for( i_list = 0; i_list < 2; i_list++ )
533 switch( h->mb.i_partition )
536 if( b_list[i_list][0] )
538 x264_mb_predict_mv( h, i_list, 0, 4, mvp );
539 bs_write_se( s, h->mb.cache.mv[i_list][x264_scan8[0]][0] - mvp[0] );
540 bs_write_se( s, h->mb.cache.mv[i_list][x264_scan8[0]][1] - mvp[1] );
544 if( b_list[i_list][0] )
546 x264_mb_predict_mv( h, i_list, 0, 4, mvp );
547 bs_write_se( s, h->mb.cache.mv[i_list][x264_scan8[0]][0] - mvp[0] );
548 bs_write_se( s, h->mb.cache.mv[i_list][x264_scan8[0]][1] - mvp[1] );
550 if( b_list[i_list][1] )
552 x264_mb_predict_mv( h, i_list, 8, 4, mvp );
553 bs_write_se( s, h->mb.cache.mv[i_list][x264_scan8[8]][0] - mvp[0] );
554 bs_write_se( s, h->mb.cache.mv[i_list][x264_scan8[8]][1] - mvp[1] );
558 if( b_list[i_list][0] )
560 x264_mb_predict_mv( h, i_list, 0, 2, mvp );
561 bs_write_se( s, h->mb.cache.mv[i_list][x264_scan8[0]][0] - mvp[0] );
562 bs_write_se( s, h->mb.cache.mv[i_list][x264_scan8[0]][1] - mvp[1] );
564 if( b_list[i_list][1] )
566 x264_mb_predict_mv( h, i_list, 4, 2, mvp );
567 bs_write_se( s, h->mb.cache.mv[i_list][x264_scan8[4]][0] - mvp[0] );
568 bs_write_se( s, h->mb.cache.mv[i_list][x264_scan8[4]][1] - mvp[1] );
574 else if( i_mb_type == B_DIRECT )
578 x264_log(h, X264_LOG_ERROR, "invalid/unhandled mb_type\n" );
583 i_mb_pos_tex = bs_pos( s );
584 h->stat.frame.i_mv_bits += i_mb_pos_tex - i_mb_pos_start;
587 /* Coded block patern */
588 if( i_mb_type == I_4x4 || i_mb_type == I_8x8 )
589 bs_write_ue( s, intra4x4_cbp_to_golomb[( h->mb.i_cbp_chroma << 4 )|h->mb.i_cbp_luma] );
590 else if( i_mb_type != I_16x16 )
591 bs_write_ue( s, inter_cbp_to_golomb[( h->mb.i_cbp_chroma << 4 )|h->mb.i_cbp_luma] );
593 /* transform size 8x8 flag */
594 if( x264_mb_transform_8x8_allowed( h ) && h->mb.i_cbp_luma )
595 bs_write1( s, h->mb.b_transform_8x8 );
598 if( i_mb_type == I_16x16 )
600 cavlc_qp_delta( h, s );
603 block_residual_write_cavlc( h, s, BLOCK_INDEX_LUMA_DC , h->dct.luma16x16_dc, 16 );
606 if( h->mb.i_cbp_luma )
607 for( i = 0; i < 16; i++ )
609 h->mb.cache.non_zero_count[x264_scan8[i]] = array_non_zero_count( h->dct.luma4x4[i] );
610 block_residual_write_cavlc( h, s, i, h->dct.luma4x4[i]+1, 15 );
613 else if( h->mb.i_cbp_luma | h->mb.i_cbp_chroma )
615 cavlc_qp_delta( h, s );
616 x264_macroblock_luma_write_cavlc( h, s, 0, 3 );
618 if( h->mb.i_cbp_chroma )
620 /* Chroma DC residual present */
621 block_residual_write_cavlc( h, s, BLOCK_INDEX_CHROMA_DC, h->dct.chroma_dc[0], 4 );
622 block_residual_write_cavlc( h, s, BLOCK_INDEX_CHROMA_DC, h->dct.chroma_dc[1], 4 );
623 if( h->mb.i_cbp_chroma&0x02 ) /* Chroma AC residual present */
624 for( i = 16; i < 24; i++ )
626 h->mb.cache.non_zero_count[x264_scan8[i]] = array_non_zero_count( h->dct.luma4x4[i] );
627 block_residual_write_cavlc( h, s, i, h->dct.luma4x4[i]+1, 15 );
632 h->stat.frame.i_tex_bits += bs_pos(s) - i_mb_pos_tex;
637 /*****************************************************************************
638 * RD only; doesn't generate a valid bitstream
639 * doesn't write cbp or chroma dc (I don't know how much this matters)
640 * doesn't write ref or subpartition (never varies between calls, so no point in doing so)
641 * works on all partition sizes except 16x16
642 * for sub8x8, call once per 8x8 block
643 *****************************************************************************/
644 static int x264_partition_size_cavlc( x264_t *h, int i8, int i_pixel )
647 const int i_mb_type = h->mb.i_type;
648 int b_8x16 = h->mb.i_partition == D_8x16;
651 s.i_bits_encoded = 0;
653 if( i_mb_type == P_8x8 )
654 cavlc_mb8x8_mvd( h, &s, 0, i8 );
655 else if( i_mb_type == P_L0 )
656 cavlc_mb_mvd( h, &s, 0, 4*i8, 4>>b_8x16 );
657 else if( i_mb_type > B_DIRECT && i_mb_type < B_8x8 )
659 if( x264_mb_type_list0_table[ i_mb_type ][!!i8] ) cavlc_mb_mvd( h, &s, 0, 4*i8, 4>>b_8x16 );
660 if( x264_mb_type_list1_table[ i_mb_type ][!!i8] ) cavlc_mb_mvd( h, &s, 1, 4*i8, 4>>b_8x16 );
662 else if( i_mb_type == B_8x8 )
664 cavlc_mb8x8_mvd( h, &s, 0, i8 );
665 cavlc_mb8x8_mvd( h, &s, 1, i8 );
669 x264_log(h, X264_LOG_ERROR, "invalid/unhandled mb_type\n" );
673 for( j = (i_pixel < PIXEL_8x8); j >= 0; j-- )
675 x264_macroblock_luma_write_cavlc( h, &s, i8, i8 );
676 h->mb.cache.non_zero_count[x264_scan8[16+i8]] = array_non_zero_count( h->dct.luma4x4[16+i8] );
677 block_residual_write_cavlc( h, &s, 16+i8, h->dct.luma4x4[16+i8]+1, 15 );
678 h->mb.cache.non_zero_count[x264_scan8[20+i8]] = array_non_zero_count( h->dct.luma4x4[20+i8] );
679 block_residual_write_cavlc( h, &s, 20+i8, h->dct.luma4x4[20+i8]+1, 15 );
680 i8 += x264_pixel_size[i_pixel].h >> 3;
683 return s.i_bits_encoded;
686 static int x264_subpartition_size_cavlc( x264_t *h, int i4, int i_pixel )
689 int b_8x4 = i_pixel == PIXEL_8x4;
690 s.i_bits_encoded = 0;
691 cavlc_mb_mvd( h, &s, 0, i4, 1+b_8x4 );
692 h->mb.cache.non_zero_count[x264_scan8[i4]] = array_non_zero_count( h->dct.luma4x4[i4] );
693 block_residual_write_cavlc( h, &s, i4, h->dct.luma4x4[i4], 16 );
694 if( i_pixel != PIXEL_4x4 )
697 h->mb.cache.non_zero_count[x264_scan8[i4]] = array_non_zero_count( h->dct.luma4x4[i4] );
698 block_residual_write_cavlc( h, &s, i4, h->dct.luma4x4[i4], 16 );
701 return s.i_bits_encoded;
704 static int cavlc_intra4x4_pred_size( x264_t *h, int i4, int i_mode )
706 if( x264_mb_predict_intra4x4_mode( h, i4 ) == x264_mb_pred_mode4x4_fix( i_mode ) )
712 static int x264_partition_i8x8_size_cavlc( x264_t *h, int i8, int i_mode )
715 h->out.bs.i_bits_encoded = cavlc_intra4x4_pred_size( h, 4*i8, i_mode );
716 for( i4 = 0; i4 < 4; i4++ )
718 for( i = 0; i < 16; i++ )
719 h->dct.luma4x4[i4+i8*4][i] = h->dct.luma8x8[i8][i4+i*4];
720 h->mb.cache.non_zero_count[x264_scan8[i4+i8*4]] = array_non_zero_count( h->dct.luma4x4[i4+i8*4] );
721 block_residual_write_cavlc( h, &h->out.bs, i4+i8*4, h->dct.luma4x4[i4+i8*4], 16 );
723 return h->out.bs.i_bits_encoded;
726 static int x264_partition_i4x4_size_cavlc( x264_t *h, int i4, int i_mode )
728 h->out.bs.i_bits_encoded = cavlc_intra4x4_pred_size( h, i4, i_mode );
729 h->mb.cache.non_zero_count[x264_scan8[i4]] = array_non_zero_count( h->dct.luma4x4[i4] );
730 block_residual_write_cavlc( h, &h->out.bs, i4, h->dct.luma4x4[i4], 16 );
731 return h->out.bs.i_bits_encoded;
734 static int x264_i8x8_chroma_size_cavlc( x264_t *h )
736 h->out.bs.i_bits_encoded = bs_size_ue( x264_mb_pred_mode8x8c_fix[ h->mb.i_chroma_pred_mode ] );
737 if( h->mb.i_cbp_chroma )
739 block_residual_write_cavlc( h, &h->out.bs, BLOCK_INDEX_CHROMA_DC, h->dct.chroma_dc[0], 4 );
740 block_residual_write_cavlc( h, &h->out.bs, BLOCK_INDEX_CHROMA_DC, h->dct.chroma_dc[1], 4 );
742 if( h->mb.i_cbp_chroma == 2 )
745 for( i = 16; i < 24; i++ )
747 h->mb.cache.non_zero_count[x264_scan8[i]] = array_non_zero_count( h->dct.luma4x4[i] );
748 block_residual_write_cavlc( h, &h->out.bs, i, h->dct.luma4x4[i]+1, 15 );
752 return h->out.bs.i_bits_encoded;