1 /*****************************************************************************
2 * cavlc.c: h264 encoder library
3 *****************************************************************************
4 * Copyright (C) 2003 Laurent Aimar
5 * $Id: cavlc.c,v 1.1 2004/06/03 19:27:08 fenrir Exp $
7 * Authors: Laurent Aimar <fenrir@via.ecp.fr>
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., 59 Temple Place - Suite 330, Boston, MA 02111, USA.
22 *****************************************************************************/
24 #include "common/common.h"
25 #include "common/vlc.h"
26 #include "macroblock.h"
28 static const uint8_t intra4x4_cbp_to_golomb[48]=
30 3, 29, 30, 17, 31, 18, 37, 8, 32, 38, 19, 9, 20, 10, 11, 2,
31 16, 33, 34, 21, 35, 22, 39, 4, 36, 40, 23, 5, 24, 6, 7, 1,
32 41, 42, 43, 25, 44, 26, 46, 12, 45, 47, 27, 13, 28, 14, 15, 0
34 static const uint8_t inter_cbp_to_golomb[48]=
36 0, 2, 3, 7, 4, 8, 17, 13, 5, 18, 9, 14, 10, 15, 16, 11,
37 1, 32, 33, 36, 34, 37, 44, 40, 35, 45, 38, 41, 39, 42, 43, 19,
38 6, 24, 25, 20, 26, 21, 46, 28, 27, 47, 22, 29, 23, 30, 31, 12
40 static const uint8_t mb_type_b_to_golomb[3][9]=
42 { 4, 8, 12, 10, 6, 14, 16, 18, 20 }, /* D_16x8 */
43 { 5, 9, 13, 11, 7, 15, 17, 19, 21 }, /* D_8x16 */
44 { 1, -1, -1, -1, 2, -1, -1, -1, 3 } /* D_16x16 */
46 static const uint8_t sub_mb_type_p_to_golomb[4]=
50 static const uint8_t sub_mb_type_b_to_golomb[13]=
52 10, 4, 5, 1, 11, 6, 7, 2, 12, 8, 9, 3, 0
55 #define BLOCK_INDEX_CHROMA_DC (-1)
56 #define BLOCK_INDEX_LUMA_DC (-2)
58 static inline void bs_write_vlc( bs_t *s, vlc_t v )
60 bs_write( s, v.i_size, v.i_bits );
63 /****************************************************************************
64 * block_residual_write_cavlc:
65 ****************************************************************************/
66 static void block_residual_write_cavlc( x264_t *h, bs_t *s, int i_idx, int16_t *l, int i_count )
68 int level[16], run[16];
69 int i_total, i_trailing;
76 /* first find i_last */
77 for( i_last = i_count-1; i_last >= 3; i_last -= 4 )
78 if( *(uint64_t*)(l+i_last-3) )
80 while( i_last >= 0 && l[i_last] == 0 )
86 i_total_zero = i_last + 1;
92 /* level and run and total */
96 level[idx] = l[i_last];
97 while( --i_last >= 0 && l[i_last] == 0 )
105 i_trailing = X264_MIN(3, idx);
106 for( idx = 0; idx < i_trailing; idx++ )
108 if( (unsigned)(level[idx]+1) > 2 )
114 i_sign |= level[idx] < 0;
119 if( i_idx == BLOCK_INDEX_CHROMA_DC )
121 bs_write_vlc( s, x264_coeff_token[4][i_total*4+i_trailing] );
125 /* x264_mb_predict_non_zero_code return 0 <-> (16+16+1)>>1 = 16 */
126 static const int ct_index[17] = {0,0,1,1,2,2,2,2,3,3,3,3,3,3,3,3,3 };
127 int nC = x264_mb_predict_non_zero_code( h, i_idx == BLOCK_INDEX_LUMA_DC ? 0 : i_idx );
128 bs_write_vlc( s, x264_coeff_token[ct_index[nC]][i_total*4+i_trailing] );
134 i_suffix_length = i_total > 10 && i_trailing < 3 ? 1 : 0;
137 bs_write( s, i_trailing, i_sign );
139 for( i = i_trailing; i < i_total; i++ )
141 int mask = level[i] >> 15;
142 int abs_level = (level[i]^mask)-mask;
143 int i_level_code = abs_level*2-mask-2;
145 if( i == i_trailing && i_trailing < 3 )
146 i_level_code -= 2; /* as level[i] can't be 1 for the first one if i_trailing < 3 */
148 if( ( i_level_code >> i_suffix_length ) < 14 )
150 bs_write( s, (i_level_code >> i_suffix_length) + 1, 1 );
151 if( i_suffix_length > 0 )
152 bs_write( s, i_suffix_length, i_level_code );
154 else if( i_suffix_length == 0 && i_level_code < 30 )
156 bs_write( s, 15, 1 );
157 bs_write( s, 4, i_level_code - 14 );
159 else if( i_suffix_length > 0 && ( i_level_code >> i_suffix_length ) == 14 )
161 bs_write( s, 15, 1 );
162 bs_write( s, i_suffix_length, i_level_code );
166 int i_level_prefix = 15;
167 i_level_code -= 15 << i_suffix_length;
168 if( i_suffix_length == 0 )
171 /* If the prefix size exceeds 15, High Profile is required. */
172 if( i_level_code >= 1<<12 )
174 if( h->sps->i_profile_idc >= PROFILE_HIGH )
176 while( i_level_code > 1<<(i_level_prefix-3) )
178 i_level_code -= 1<<(i_level_prefix-3);
185 /* Weight highly against overflows. */
186 s->i_bits_encoded += 1000000;
188 x264_log(h, X264_LOG_WARNING, "OVERFLOW levelcode=%d is only allowed in High Profile", i_level_code );
189 /* clip level, preserving sign */
190 i_level_code = (1<<12) - 2 + (i_level_code & 1);
194 bs_write( s, i_level_prefix + 1, 1 );
195 bs_write( s, i_level_prefix - 3, i_level_code );
198 if( i_suffix_length == 0 )
200 if( abs_level > (3 << (i_suffix_length-1)) && i_suffix_length < 6 )
204 if( i_total < i_count )
206 if( i_idx == BLOCK_INDEX_CHROMA_DC )
207 bs_write_vlc( s, x264_total_zeros_dc[i_total-1][i_total_zero] );
209 bs_write_vlc( s, x264_total_zeros[i_total-1][i_total_zero] );
212 for( i = 0; i < i_total-1 && i_total_zero > 0; i++ )
214 int i_zl = X264_MIN( i_total_zero - 1, 6 );
215 bs_write_vlc( s, x264_run_before[i_zl][run[i]] );
216 i_total_zero -= run[i];
220 static void cavlc_qp_delta( x264_t *h, bs_t *s )
222 int i_dqp = h->mb.i_qp - h->mb.i_last_qp;
227 else if( i_dqp > 25 )
230 bs_write_se( s, i_dqp );
233 static void cavlc_mb_mvd( x264_t *h, bs_t *s, int i_list, int idx, int width )
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 void cavlc_mb8x8_mvd( x264_t *h, bs_t *s, int i_list, int i )
243 if( !x264_mb_partition_listX_table[i_list][ h->mb.i_sub_partition[i] ] )
246 switch( h->mb.i_sub_partition[i] )
251 cavlc_mb_mvd( h, s, i_list, 4*i, 2 );
256 cavlc_mb_mvd( h, s, i_list, 4*i+0, 2 );
257 cavlc_mb_mvd( h, s, i_list, 4*i+2, 2 );
262 cavlc_mb_mvd( h, s, i_list, 4*i+0, 1 );
263 cavlc_mb_mvd( h, s, i_list, 4*i+1, 1 );
268 cavlc_mb_mvd( h, s, i_list, 4*i+0, 1 );
269 cavlc_mb_mvd( h, s, i_list, 4*i+1, 1 );
270 cavlc_mb_mvd( h, s, i_list, 4*i+2, 1 );
271 cavlc_mb_mvd( h, s, i_list, 4*i+3, 1 );
276 static inline void x264_macroblock_luma_write_cavlc( x264_t *h, bs_t *s, int i8start, int i8end )
279 if( h->mb.b_transform_8x8 )
281 /* shuffle 8x8 dct coeffs into 4x4 lists */
282 for( i8 = i8start; i8 <= i8end; i8++ )
283 if( h->mb.i_cbp_luma & (1 << i8) )
284 for( i4 = 0; i4 < 4; i4++ )
286 for( i = 0; i < 16; i++ )
287 h->dct.luma4x4[i4+i8*4][i] = h->dct.luma8x8[i8][i4+i*4];
288 h->mb.cache.non_zero_count[x264_scan8[i4+i8*4]] =
289 array_non_zero_count( h->dct.luma4x4[i4+i8*4], 16 );
293 for( i8 = i8start; i8 <= i8end; i8++ )
294 if( h->mb.i_cbp_luma & (1 << i8) )
295 for( i4 = 0; i4 < 4; i4++ )
296 block_residual_write_cavlc( h, s, i4+i8*4, h->dct.luma4x4[i4+i8*4], 16 );
299 /*****************************************************************************
300 * x264_macroblock_write:
301 *****************************************************************************/
302 void x264_macroblock_write_cavlc( x264_t *h, bs_t *s )
304 const int i_mb_type = h->mb.i_type;
309 const int i_mb_pos_start = bs_pos( s );
313 switch( h->sh.i_type )
325 x264_log(h, X264_LOG_ERROR, "internal error or slice unsupported\n" );
330 && (!(h->mb.i_mb_y & 1) || IS_SKIP(h->mb.type[h->mb.i_mb_xy - h->mb.i_mb_stride])) )
332 bs_write1( s, h->mb.b_interlaced );
339 if( i_mb_type == I_PCM )
342 bs_write_ue( s, i_mb_i_offset + 25 );
345 s->i_bits_encoded += 384*8;
349 for( i = 0; i < 16*16; i++ )
351 const int x = 16 * h->mb.i_mb_x + (i % 16);
352 const int y = 16 * h->mb.i_mb_y + (i / 16);
353 bs_write( s, 8, h->fenc->plane[0][y*h->mb.pic.i_stride[0]+x] );
356 for( i = 0; i < 8*8; i++ )
358 const int x = 8 * h->mb.i_mb_x + (i % 8);
359 const int y = 8 * h->mb.i_mb_y + (i / 8);
360 bs_write( s, 8, h->fenc->plane[1][y*h->mb.pic.i_stride[1]+x] );
363 for( i = 0; i < 8*8; i++ )
365 const int x = 8 * h->mb.i_mb_x + (i % 8);
366 const int y = 8 * h->mb.i_mb_y + (i / 8);
367 bs_write( s, 8, h->fenc->plane[2][y*h->mb.pic.i_stride[2]+x] );
372 else if( i_mb_type == I_4x4 || i_mb_type == I_8x8 )
374 int di = i_mb_type == I_8x8 ? 4 : 1;
375 bs_write_ue( s, i_mb_i_offset + 0 );
376 if( h->pps->b_transform_8x8_mode )
377 bs_write1( s, h->mb.b_transform_8x8 );
379 /* Prediction: Luma */
380 for( i = 0; i < 16; i += di )
382 int i_pred = x264_mb_predict_intra4x4_mode( h, i );
383 int i_mode = x264_mb_pred_mode4x4_fix( h->mb.cache.intra4x4_pred_mode[x264_scan8[i]] );
385 if( i_pred == i_mode)
387 bs_write1( s, 1 ); /* b_prev_intra4x4_pred_mode */
391 bs_write1( s, 0 ); /* b_prev_intra4x4_pred_mode */
392 if( i_mode < i_pred )
394 bs_write( s, 3, i_mode );
398 bs_write( s, 3, i_mode - 1 );
402 bs_write_ue( s, x264_mb_pred_mode8x8c_fix[ h->mb.i_chroma_pred_mode ] );
404 else if( i_mb_type == I_16x16 )
406 bs_write_ue( s, i_mb_i_offset + 1 + x264_mb_pred_mode16x16_fix[h->mb.i_intra16x16_pred_mode] +
407 h->mb.i_cbp_chroma * 4 + ( h->mb.i_cbp_luma == 0 ? 0 : 12 ) );
408 bs_write_ue( s, x264_mb_pred_mode8x8c_fix[ h->mb.i_chroma_pred_mode ] );
410 else if( i_mb_type == P_L0 )
414 if( h->mb.i_partition == D_16x16 )
418 if( h->mb.pic.i_fref[0] > 1 )
420 bs_write_te( s, h->mb.pic.i_fref[0] - 1, h->mb.cache.ref[0][x264_scan8[0]] );
422 x264_mb_predict_mv( h, 0, 0, 4, mvp );
423 bs_write_se( s, h->mb.cache.mv[0][x264_scan8[0]][0] - mvp[0] );
424 bs_write_se( s, h->mb.cache.mv[0][x264_scan8[0]][1] - mvp[1] );
426 else if( h->mb.i_partition == D_16x8 )
429 if( h->mb.pic.i_fref[0] > 1 )
431 bs_write_te( s, h->mb.pic.i_fref[0] - 1, h->mb.cache.ref[0][x264_scan8[0]] );
432 bs_write_te( s, h->mb.pic.i_fref[0] - 1, h->mb.cache.ref[0][x264_scan8[8]] );
435 x264_mb_predict_mv( h, 0, 0, 4, mvp );
436 bs_write_se( s, h->mb.cache.mv[0][x264_scan8[0]][0] - mvp[0] );
437 bs_write_se( s, h->mb.cache.mv[0][x264_scan8[0]][1] - mvp[1] );
439 x264_mb_predict_mv( h, 0, 8, 4, mvp );
440 bs_write_se( s, h->mb.cache.mv[0][x264_scan8[8]][0] - mvp[0] );
441 bs_write_se( s, h->mb.cache.mv[0][x264_scan8[8]][1] - mvp[1] );
443 else if( h->mb.i_partition == D_8x16 )
446 if( h->mb.pic.i_fref[0] > 1 )
448 bs_write_te( s, h->mb.pic.i_fref[0] - 1, h->mb.cache.ref[0][x264_scan8[0]] );
449 bs_write_te( s, h->mb.pic.i_fref[0] - 1, h->mb.cache.ref[0][x264_scan8[4]] );
452 x264_mb_predict_mv( h, 0, 0, 2, mvp );
453 bs_write_se( s, h->mb.cache.mv[0][x264_scan8[0]][0] - mvp[0] );
454 bs_write_se( s, h->mb.cache.mv[0][x264_scan8[0]][1] - mvp[1] );
456 x264_mb_predict_mv( h, 0, 4, 2, mvp );
457 bs_write_se( s, h->mb.cache.mv[0][x264_scan8[4]][0] - mvp[0] );
458 bs_write_se( s, h->mb.cache.mv[0][x264_scan8[4]][1] - mvp[1] );
461 else if( i_mb_type == P_8x8 )
465 if( h->mb.cache.ref[0][x264_scan8[0]] == 0 && h->mb.cache.ref[0][x264_scan8[4]] == 0 &&
466 h->mb.cache.ref[0][x264_scan8[8]] == 0 && h->mb.cache.ref[0][x264_scan8[12]] == 0 )
477 for( i = 0; i < 4; i++ )
479 bs_write_ue( s, sub_mb_type_p_to_golomb[ h->mb.i_sub_partition[i] ] );
482 if( h->mb.pic.i_fref[0] > 1 && b_sub_ref0 )
484 bs_write_te( s, h->mb.pic.i_fref[0] - 1, h->mb.cache.ref[0][x264_scan8[0]] );
485 bs_write_te( s, h->mb.pic.i_fref[0] - 1, h->mb.cache.ref[0][x264_scan8[4]] );
486 bs_write_te( s, h->mb.pic.i_fref[0] - 1, h->mb.cache.ref[0][x264_scan8[8]] );
487 bs_write_te( s, h->mb.pic.i_fref[0] - 1, h->mb.cache.ref[0][x264_scan8[12]] );
490 for( i = 0; i < 4; i++ )
491 cavlc_mb8x8_mvd( h, s, 0, i );
493 else if( i_mb_type == B_8x8 )
495 bs_write_ue( s, 22 );
498 for( i = 0; i < 4; i++ )
500 bs_write_ue( s, sub_mb_type_b_to_golomb[ h->mb.i_sub_partition[i] ] );
503 for( i = 0; i < 4; i++ )
505 if( x264_mb_partition_listX_table[0][ h->mb.i_sub_partition[i] ] )
507 bs_write_te( s, h->mb.pic.i_fref[0] - 1, h->mb.cache.ref[0][x264_scan8[i*4]] );
510 for( i = 0; i < 4; i++ )
512 if( x264_mb_partition_listX_table[1][ h->mb.i_sub_partition[i] ] )
514 bs_write_te( s, h->mb.pic.i_fref[1] - 1, h->mb.cache.ref[1][x264_scan8[i*4]] );
518 for( i = 0; i < 4; i++ )
519 cavlc_mb8x8_mvd( h, s, 0, i );
520 for( i = 0; i < 4; i++ )
521 cavlc_mb8x8_mvd( h, s, 1, i );
523 else if( i_mb_type != B_DIRECT )
532 /* init ref list utilisations */
533 for( i = 0; i < 2; i++ )
535 b_list[0][i] = x264_mb_type_list0_table[i_mb_type][i];
536 b_list[1][i] = x264_mb_type_list1_table[i_mb_type][i];
540 bs_write_ue( s, mb_type_b_to_golomb[ h->mb.i_partition - D_16x8 ][ i_mb_type - B_L0_L0 ] );
542 for( i_list = 0; i_list < 2; i_list++ )
544 const int i_ref_max = i_list == 0 ? h->mb.pic.i_fref[0] : h->mb.pic.i_fref[1];
548 switch( h->mb.i_partition )
551 if( b_list[i_list][0] ) bs_write_te( s, i_ref_max - 1, h->mb.cache.ref[i_list][x264_scan8[0]] );
554 if( b_list[i_list][0] ) bs_write_te( s, i_ref_max - 1, h->mb.cache.ref[i_list][x264_scan8[0]] );
555 if( b_list[i_list][1] ) bs_write_te( s, i_ref_max - 1, h->mb.cache.ref[i_list][x264_scan8[8]] );
558 if( b_list[i_list][0] ) bs_write_te( s, i_ref_max - 1, h->mb.cache.ref[i_list][x264_scan8[0]] );
559 if( b_list[i_list][1] ) bs_write_te( s, i_ref_max - 1, h->mb.cache.ref[i_list][x264_scan8[4]] );
564 for( i_list = 0; i_list < 2; i_list++ )
566 switch( h->mb.i_partition )
569 if( b_list[i_list][0] )
571 x264_mb_predict_mv( h, i_list, 0, 4, mvp );
572 bs_write_se( s, h->mb.cache.mv[i_list][x264_scan8[0]][0] - mvp[0] );
573 bs_write_se( s, h->mb.cache.mv[i_list][x264_scan8[0]][1] - mvp[1] );
577 if( b_list[i_list][0] )
579 x264_mb_predict_mv( h, i_list, 0, 4, mvp );
580 bs_write_se( s, h->mb.cache.mv[i_list][x264_scan8[0]][0] - mvp[0] );
581 bs_write_se( s, h->mb.cache.mv[i_list][x264_scan8[0]][1] - mvp[1] );
583 if( b_list[i_list][1] )
585 x264_mb_predict_mv( h, i_list, 8, 4, mvp );
586 bs_write_se( s, h->mb.cache.mv[i_list][x264_scan8[8]][0] - mvp[0] );
587 bs_write_se( s, h->mb.cache.mv[i_list][x264_scan8[8]][1] - mvp[1] );
591 if( b_list[i_list][0] )
593 x264_mb_predict_mv( h, i_list, 0, 2, mvp );
594 bs_write_se( s, h->mb.cache.mv[i_list][x264_scan8[0]][0] - mvp[0] );
595 bs_write_se( s, h->mb.cache.mv[i_list][x264_scan8[0]][1] - mvp[1] );
597 if( b_list[i_list][1] )
599 x264_mb_predict_mv( h, i_list, 4, 2, mvp );
600 bs_write_se( s, h->mb.cache.mv[i_list][x264_scan8[4]][0] - mvp[0] );
601 bs_write_se( s, h->mb.cache.mv[i_list][x264_scan8[4]][1] - mvp[1] );
607 else if( i_mb_type == B_DIRECT )
613 x264_log(h, X264_LOG_ERROR, "invalid/unhandled mb_type\n" );
618 i_mb_pos_tex = bs_pos( s );
619 h->stat.frame.i_hdr_bits += i_mb_pos_tex - i_mb_pos_start;
622 /* Coded block patern */
623 if( i_mb_type == I_4x4 || i_mb_type == I_8x8 )
625 bs_write_ue( s, intra4x4_cbp_to_golomb[( h->mb.i_cbp_chroma << 4 )|h->mb.i_cbp_luma] );
627 else if( i_mb_type != I_16x16 )
629 bs_write_ue( s, inter_cbp_to_golomb[( h->mb.i_cbp_chroma << 4 )|h->mb.i_cbp_luma] );
632 /* transform size 8x8 flag */
633 if( x264_mb_transform_8x8_allowed( h ) && h->mb.i_cbp_luma )
635 bs_write1( s, h->mb.b_transform_8x8 );
639 if( i_mb_type == I_16x16 )
641 cavlc_qp_delta( h, s );
644 block_residual_write_cavlc( h, s, BLOCK_INDEX_LUMA_DC , h->dct.luma16x16_dc, 16 );
647 if( h->mb.i_cbp_luma != 0 )
648 for( i = 0; i < 16; i++ )
649 block_residual_write_cavlc( h, s, i, h->dct.luma4x4[i]+1, 15 );
651 else if( h->mb.i_cbp_luma != 0 || h->mb.i_cbp_chroma != 0 )
653 cavlc_qp_delta( h, s );
654 x264_macroblock_luma_write_cavlc( h, s, 0, 3 );
656 if( h->mb.i_cbp_chroma != 0 )
658 /* Chroma DC residual present */
659 block_residual_write_cavlc( h, s, BLOCK_INDEX_CHROMA_DC, h->dct.chroma_dc[0], 4 );
660 block_residual_write_cavlc( h, s, BLOCK_INDEX_CHROMA_DC, h->dct.chroma_dc[1], 4 );
661 if( h->mb.i_cbp_chroma&0x02 ) /* Chroma AC residual present */
662 for( i = 16; i < 24; i++ )
663 block_residual_write_cavlc( h, s, i, h->dct.luma4x4[i]+1, 15 );
667 if( IS_INTRA( i_mb_type ) )
668 h->stat.frame.i_itex_bits += bs_pos(s) - i_mb_pos_tex;
670 h->stat.frame.i_ptex_bits += bs_pos(s) - i_mb_pos_tex;
675 /*****************************************************************************
676 * RD only; doesn't generate a valid bitstream
677 * doesn't write cbp or chroma dc (I don't know how much this matters)
678 * works on all partition sizes except 16x16
679 * for sub8x8, call once per 8x8 block
680 *****************************************************************************/
681 int x264_partition_size_cavlc( x264_t *h, int i8, int i_pixel )
684 const int i_mb_type = h->mb.i_type;
687 s.i_bits_encoded = 0;
689 if( i_mb_type == P_8x8 )
691 bs_write_ue( &s, sub_mb_type_p_to_golomb[ h->mb.i_sub_partition[i8] ] );
692 if( h->mb.pic.i_fref[0] > 1 )
693 bs_write_te( &s, h->mb.pic.i_fref[0] - 1, h->mb.cache.ref[0][x264_scan8[4*i8]] );
694 cavlc_mb8x8_mvd( h, &s, 0, i8 );
696 else if( i_mb_type == P_L0 )
698 if( h->mb.pic.i_fref[0] > 1 )
699 bs_write_te( &s, h->mb.pic.i_fref[0] - 1, h->mb.cache.ref[0][x264_scan8[4*i8]] );
700 if( h->mb.i_partition == D_16x8 )
701 cavlc_mb_mvd( h, &s, 0, 4*i8, 4 );
703 cavlc_mb_mvd( h, &s, 0, 4*i8, 2 );
705 else if( i_mb_type == B_8x8 )
707 bs_write_ue( &s, sub_mb_type_b_to_golomb[ h->mb.i_sub_partition[i8] ] );
709 if( h->mb.pic.i_fref[0] > 1
710 && x264_mb_partition_listX_table[0][ h->mb.i_sub_partition[i8] ] )
711 bs_write_te( &s, h->mb.pic.i_fref[0] - 1, h->mb.cache.ref[0][x264_scan8[4*i8]] );
712 if( h->mb.pic.i_fref[1] > 1
713 && x264_mb_partition_listX_table[1][ h->mb.i_sub_partition[i8] ] )
714 bs_write_te( &s, h->mb.pic.i_fref[1] - 1, h->mb.cache.ref[1][x264_scan8[4*i8]] );
716 cavlc_mb8x8_mvd( h, &s, 0, i8 );
717 cavlc_mb8x8_mvd( h, &s, 1, i8 );
721 x264_log(h, X264_LOG_ERROR, "invalid/unhandled mb_type\n" );
725 for( j = (i_pixel < PIXEL_8x8); j >= 0; j-- )
727 x264_macroblock_luma_write_cavlc( h, &s, i8, i8 );
729 block_residual_write_cavlc( h, &s, 16+i8, h->dct.luma4x4[16+i8]+1, 15 );
730 block_residual_write_cavlc( h, &s, 20+i8, h->dct.luma4x4[20+i8]+1, 15 );
732 i8 += x264_pixel_size[i_pixel].h >> 3;
735 return s.i_bits_encoded;
738 static int cavlc_intra4x4_pred_size( x264_t *h, int i4, int i_mode )
740 if( x264_mb_predict_intra4x4_mode( h, i4 ) == x264_mb_pred_mode4x4_fix( i_mode ) )
746 static int x264_partition_i8x8_size_cavlc( x264_t *h, int i8, int i_mode )
749 h->out.bs.i_bits_encoded = cavlc_intra4x4_pred_size( h, 4*i8, i_mode );
750 for( i4 = 0; i4 < 4; i4++ )
752 for( i = 0; i < 16; i++ )
753 h->dct.luma4x4[i4+i8*4][i] = h->dct.luma8x8[i8][i4+i*4];
754 h->mb.cache.non_zero_count[x264_scan8[i4+i8*4]] =
755 array_non_zero_count( h->dct.luma4x4[i4+i8*4], 16 );
756 block_residual_write_cavlc( h, &h->out.bs, i4+i8*4, h->dct.luma4x4[i4+i8*4], 16 );
758 return h->out.bs.i_bits_encoded;
761 static int x264_partition_i4x4_size_cavlc( x264_t *h, int i4, int i_mode )
763 h->out.bs.i_bits_encoded = cavlc_intra4x4_pred_size( h, i4, i_mode );
764 block_residual_write_cavlc( h, &h->out.bs, i4, h->dct.luma4x4[i4], 16 );
765 return h->out.bs.i_bits_encoded;
768 static int x264_i8x8_chroma_size_cavlc( x264_t *h )
770 h->out.bs.i_bits_encoded = bs_size_ue( x264_mb_pred_mode8x8c_fix[ h->mb.i_chroma_pred_mode ] );
771 if( h->mb.i_cbp_chroma != 0 )
773 block_residual_write_cavlc( h, &h->out.bs, BLOCK_INDEX_CHROMA_DC, h->dct.chroma_dc[0], 4 );
774 block_residual_write_cavlc( h, &h->out.bs, BLOCK_INDEX_CHROMA_DC, h->dct.chroma_dc[1], 4 );
776 if( h->mb.i_cbp_chroma == 2 )
779 for( i = 16; i < 24; i++ )
780 block_residual_write_cavlc( h, &h->out.bs, i, h->dct.luma4x4[i]+1, 15 );
783 return h->out.bs.i_bits_encoded;