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;
224 /* Avoid writing a delta quant if we have an empty i16x16 block, e.g. in a completely flat background area */
225 if( h->mb.i_type == I_16x16 && !(h->mb.i_cbp_luma | h->mb.i_cbp_chroma)
226 && !array_non_zero(h->dct.luma16x16_dc) )
229 h->mb.i_qp = h->mb.i_last_qp;
238 else if( i_dqp > 25 )
241 bs_write_se( s, i_dqp );
244 static void cavlc_mb_mvd( x264_t *h, bs_t *s, int i_list, int idx, int width )
246 DECLARE_ALIGNED_4( int16_t mvp[2] );
247 x264_mb_predict_mv( h, i_list, idx, width, mvp );
248 bs_write_se( s, h->mb.cache.mv[i_list][x264_scan8[idx]][0] - mvp[0] );
249 bs_write_se( s, h->mb.cache.mv[i_list][x264_scan8[idx]][1] - mvp[1] );
252 static void cavlc_mb8x8_mvd( x264_t *h, bs_t *s, int i_list, int i )
254 if( !x264_mb_partition_listX_table[i_list][ h->mb.i_sub_partition[i] ] )
257 switch( h->mb.i_sub_partition[i] )
262 cavlc_mb_mvd( h, s, i_list, 4*i, 2 );
267 cavlc_mb_mvd( h, s, i_list, 4*i+0, 2 );
268 cavlc_mb_mvd( h, s, i_list, 4*i+2, 2 );
273 cavlc_mb_mvd( h, s, i_list, 4*i+0, 1 );
274 cavlc_mb_mvd( h, s, i_list, 4*i+1, 1 );
279 cavlc_mb_mvd( h, s, i_list, 4*i+0, 1 );
280 cavlc_mb_mvd( h, s, i_list, 4*i+1, 1 );
281 cavlc_mb_mvd( h, s, i_list, 4*i+2, 1 );
282 cavlc_mb_mvd( h, s, i_list, 4*i+3, 1 );
287 static inline void x264_macroblock_luma_write_cavlc( x264_t *h, bs_t *s, int i8start, int i8end )
290 if( h->mb.b_transform_8x8 )
292 /* shuffle 8x8 dct coeffs into 4x4 lists */
293 for( i8 = i8start; i8 <= i8end; i8++ )
294 if( h->mb.i_cbp_luma & (1 << i8) )
295 for( i4 = 0; i4 < 4; i4++ )
296 for( i = 0; i < 16; i++ )
297 h->dct.luma4x4[i4+i8*4][i] = h->dct.luma8x8[i8][i4+i*4];
300 for( i8 = i8start; i8 <= i8end; i8++ )
301 if( h->mb.i_cbp_luma & (1 << i8) )
302 for( i4 = 0; i4 < 4; i4++ )
304 h->mb.cache.non_zero_count[x264_scan8[i4+i8*4]] = array_non_zero_count( h->dct.luma4x4[i4+i8*4], 16 );
305 block_residual_write_cavlc( h, s, i4+i8*4, h->dct.luma4x4[i4+i8*4], 16 );
309 /*****************************************************************************
310 * x264_macroblock_write:
311 *****************************************************************************/
312 void x264_macroblock_write_cavlc( x264_t *h, bs_t *s )
314 const int i_mb_type = h->mb.i_type;
319 const int i_mb_pos_start = bs_pos( s );
323 switch( h->sh.i_type )
335 x264_log(h, X264_LOG_ERROR, "internal error or slice unsupported\n" );
340 && (!(h->mb.i_mb_y & 1) || IS_SKIP(h->mb.type[h->mb.i_mb_xy - h->mb.i_mb_stride])) )
342 bs_write1( s, h->mb.b_interlaced );
349 if( i_mb_type == I_PCM )
352 bs_write_ue( s, i_mb_i_offset + 25 );
355 s->i_bits_encoded += 384*8;
359 for( i = 0; i < 16*16; i++ )
361 const int x = 16 * h->mb.i_mb_x + (i % 16);
362 const int y = 16 * h->mb.i_mb_y + (i / 16);
363 bs_write( s, 8, h->fenc->plane[0][y*h->mb.pic.i_stride[0]+x] );
366 for( i = 0; i < 8*8; i++ )
368 const int x = 8 * h->mb.i_mb_x + (i % 8);
369 const int y = 8 * h->mb.i_mb_y + (i / 8);
370 bs_write( s, 8, h->fenc->plane[1][y*h->mb.pic.i_stride[1]+x] );
373 for( i = 0; i < 8*8; i++ )
375 const int x = 8 * h->mb.i_mb_x + (i % 8);
376 const int y = 8 * h->mb.i_mb_y + (i / 8);
377 bs_write( s, 8, h->fenc->plane[2][y*h->mb.pic.i_stride[2]+x] );
382 else if( i_mb_type == I_4x4 || i_mb_type == I_8x8 )
384 int di = i_mb_type == I_8x8 ? 4 : 1;
385 bs_write_ue( s, i_mb_i_offset + 0 );
386 if( h->pps->b_transform_8x8_mode )
387 bs_write1( s, h->mb.b_transform_8x8 );
389 /* Prediction: Luma */
390 for( i = 0; i < 16; i += di )
392 int i_pred = x264_mb_predict_intra4x4_mode( h, i );
393 int i_mode = x264_mb_pred_mode4x4_fix( h->mb.cache.intra4x4_pred_mode[x264_scan8[i]] );
395 if( i_pred == i_mode)
397 bs_write1( s, 1 ); /* b_prev_intra4x4_pred_mode */
401 bs_write1( s, 0 ); /* b_prev_intra4x4_pred_mode */
402 if( i_mode < i_pred )
404 bs_write( s, 3, i_mode );
408 bs_write( s, 3, i_mode - 1 );
412 bs_write_ue( s, x264_mb_pred_mode8x8c_fix[ h->mb.i_chroma_pred_mode ] );
414 else if( i_mb_type == I_16x16 )
416 bs_write_ue( s, i_mb_i_offset + 1 + x264_mb_pred_mode16x16_fix[h->mb.i_intra16x16_pred_mode] +
417 h->mb.i_cbp_chroma * 4 + ( h->mb.i_cbp_luma == 0 ? 0 : 12 ) );
418 bs_write_ue( s, x264_mb_pred_mode8x8c_fix[ h->mb.i_chroma_pred_mode ] );
420 else if( i_mb_type == P_L0 )
422 DECLARE_ALIGNED_4( int16_t mvp[2] );
424 if( h->mb.i_partition == D_16x16 )
428 if( h->mb.pic.i_fref[0] > 1 )
430 bs_write_te( s, h->mb.pic.i_fref[0] - 1, h->mb.cache.ref[0][x264_scan8[0]] );
432 x264_mb_predict_mv( h, 0, 0, 4, mvp );
433 bs_write_se( s, h->mb.cache.mv[0][x264_scan8[0]][0] - mvp[0] );
434 bs_write_se( s, h->mb.cache.mv[0][x264_scan8[0]][1] - mvp[1] );
436 else if( h->mb.i_partition == D_16x8 )
439 if( h->mb.pic.i_fref[0] > 1 )
441 bs_write_te( s, h->mb.pic.i_fref[0] - 1, h->mb.cache.ref[0][x264_scan8[0]] );
442 bs_write_te( s, h->mb.pic.i_fref[0] - 1, h->mb.cache.ref[0][x264_scan8[8]] );
445 x264_mb_predict_mv( h, 0, 0, 4, mvp );
446 bs_write_se( s, h->mb.cache.mv[0][x264_scan8[0]][0] - mvp[0] );
447 bs_write_se( s, h->mb.cache.mv[0][x264_scan8[0]][1] - mvp[1] );
449 x264_mb_predict_mv( h, 0, 8, 4, mvp );
450 bs_write_se( s, h->mb.cache.mv[0][x264_scan8[8]][0] - mvp[0] );
451 bs_write_se( s, h->mb.cache.mv[0][x264_scan8[8]][1] - mvp[1] );
453 else if( h->mb.i_partition == D_8x16 )
456 if( h->mb.pic.i_fref[0] > 1 )
458 bs_write_te( s, h->mb.pic.i_fref[0] - 1, h->mb.cache.ref[0][x264_scan8[0]] );
459 bs_write_te( s, h->mb.pic.i_fref[0] - 1, h->mb.cache.ref[0][x264_scan8[4]] );
462 x264_mb_predict_mv( h, 0, 0, 2, mvp );
463 bs_write_se( s, h->mb.cache.mv[0][x264_scan8[0]][0] - mvp[0] );
464 bs_write_se( s, h->mb.cache.mv[0][x264_scan8[0]][1] - mvp[1] );
466 x264_mb_predict_mv( h, 0, 4, 2, mvp );
467 bs_write_se( s, h->mb.cache.mv[0][x264_scan8[4]][0] - mvp[0] );
468 bs_write_se( s, h->mb.cache.mv[0][x264_scan8[4]][1] - mvp[1] );
471 else if( i_mb_type == P_8x8 )
475 if( h->mb.cache.ref[0][x264_scan8[0]] == 0 && h->mb.cache.ref[0][x264_scan8[4]] == 0 &&
476 h->mb.cache.ref[0][x264_scan8[8]] == 0 && h->mb.cache.ref[0][x264_scan8[12]] == 0 )
487 for( i = 0; i < 4; i++ )
489 bs_write_ue( s, sub_mb_type_p_to_golomb[ h->mb.i_sub_partition[i] ] );
492 if( h->mb.pic.i_fref[0] > 1 && b_sub_ref0 )
494 bs_write_te( s, h->mb.pic.i_fref[0] - 1, h->mb.cache.ref[0][x264_scan8[0]] );
495 bs_write_te( s, h->mb.pic.i_fref[0] - 1, h->mb.cache.ref[0][x264_scan8[4]] );
496 bs_write_te( s, h->mb.pic.i_fref[0] - 1, h->mb.cache.ref[0][x264_scan8[8]] );
497 bs_write_te( s, h->mb.pic.i_fref[0] - 1, h->mb.cache.ref[0][x264_scan8[12]] );
500 for( i = 0; i < 4; i++ )
501 cavlc_mb8x8_mvd( h, s, 0, i );
503 else if( i_mb_type == B_8x8 )
505 bs_write_ue( s, 22 );
508 for( i = 0; i < 4; i++ )
510 bs_write_ue( s, sub_mb_type_b_to_golomb[ h->mb.i_sub_partition[i] ] );
513 for( i = 0; i < 4; i++ )
515 if( x264_mb_partition_listX_table[0][ h->mb.i_sub_partition[i] ] )
517 bs_write_te( s, h->mb.pic.i_fref[0] - 1, h->mb.cache.ref[0][x264_scan8[i*4]] );
520 for( i = 0; i < 4; i++ )
522 if( x264_mb_partition_listX_table[1][ h->mb.i_sub_partition[i] ] )
524 bs_write_te( s, h->mb.pic.i_fref[1] - 1, h->mb.cache.ref[1][x264_scan8[i*4]] );
528 for( i = 0; i < 4; i++ )
529 cavlc_mb8x8_mvd( h, s, 0, i );
530 for( i = 0; i < 4; i++ )
531 cavlc_mb8x8_mvd( h, s, 1, i );
533 else if( i_mb_type != B_DIRECT )
538 DECLARE_ALIGNED_4( int16_t mvp[2] );
542 /* init ref list utilisations */
543 for( i = 0; i < 2; i++ )
545 b_list[0][i] = x264_mb_type_list0_table[i_mb_type][i];
546 b_list[1][i] = x264_mb_type_list1_table[i_mb_type][i];
550 bs_write_ue( s, mb_type_b_to_golomb[ h->mb.i_partition - D_16x8 ][ i_mb_type - B_L0_L0 ] );
552 for( i_list = 0; i_list < 2; i_list++ )
554 const int i_ref_max = i_list == 0 ? h->mb.pic.i_fref[0] : h->mb.pic.i_fref[1];
558 switch( h->mb.i_partition )
561 if( b_list[i_list][0] ) bs_write_te( s, i_ref_max - 1, h->mb.cache.ref[i_list][x264_scan8[0]] );
564 if( b_list[i_list][0] ) bs_write_te( s, i_ref_max - 1, h->mb.cache.ref[i_list][x264_scan8[0]] );
565 if( b_list[i_list][1] ) bs_write_te( s, i_ref_max - 1, h->mb.cache.ref[i_list][x264_scan8[8]] );
568 if( b_list[i_list][0] ) bs_write_te( s, i_ref_max - 1, h->mb.cache.ref[i_list][x264_scan8[0]] );
569 if( b_list[i_list][1] ) bs_write_te( s, i_ref_max - 1, h->mb.cache.ref[i_list][x264_scan8[4]] );
574 for( i_list = 0; i_list < 2; i_list++ )
576 switch( h->mb.i_partition )
579 if( b_list[i_list][0] )
581 x264_mb_predict_mv( h, i_list, 0, 4, mvp );
582 bs_write_se( s, h->mb.cache.mv[i_list][x264_scan8[0]][0] - mvp[0] );
583 bs_write_se( s, h->mb.cache.mv[i_list][x264_scan8[0]][1] - mvp[1] );
587 if( b_list[i_list][0] )
589 x264_mb_predict_mv( h, i_list, 0, 4, mvp );
590 bs_write_se( s, h->mb.cache.mv[i_list][x264_scan8[0]][0] - mvp[0] );
591 bs_write_se( s, h->mb.cache.mv[i_list][x264_scan8[0]][1] - mvp[1] );
593 if( b_list[i_list][1] )
595 x264_mb_predict_mv( h, i_list, 8, 4, mvp );
596 bs_write_se( s, h->mb.cache.mv[i_list][x264_scan8[8]][0] - mvp[0] );
597 bs_write_se( s, h->mb.cache.mv[i_list][x264_scan8[8]][1] - mvp[1] );
601 if( b_list[i_list][0] )
603 x264_mb_predict_mv( h, i_list, 0, 2, mvp );
604 bs_write_se( s, h->mb.cache.mv[i_list][x264_scan8[0]][0] - mvp[0] );
605 bs_write_se( s, h->mb.cache.mv[i_list][x264_scan8[0]][1] - mvp[1] );
607 if( b_list[i_list][1] )
609 x264_mb_predict_mv( h, i_list, 4, 2, mvp );
610 bs_write_se( s, h->mb.cache.mv[i_list][x264_scan8[4]][0] - mvp[0] );
611 bs_write_se( s, h->mb.cache.mv[i_list][x264_scan8[4]][1] - mvp[1] );
617 else if( i_mb_type == B_DIRECT )
623 x264_log(h, X264_LOG_ERROR, "invalid/unhandled mb_type\n" );
628 i_mb_pos_tex = bs_pos( s );
629 h->stat.frame.i_hdr_bits += i_mb_pos_tex - i_mb_pos_start;
632 /* Coded block patern */
633 if( i_mb_type == I_4x4 || i_mb_type == I_8x8 )
635 bs_write_ue( s, intra4x4_cbp_to_golomb[( h->mb.i_cbp_chroma << 4 )|h->mb.i_cbp_luma] );
637 else if( i_mb_type != I_16x16 )
639 bs_write_ue( s, inter_cbp_to_golomb[( h->mb.i_cbp_chroma << 4 )|h->mb.i_cbp_luma] );
642 /* transform size 8x8 flag */
643 if( x264_mb_transform_8x8_allowed( h ) && h->mb.i_cbp_luma )
645 bs_write1( s, h->mb.b_transform_8x8 );
649 if( i_mb_type == I_16x16 )
651 cavlc_qp_delta( h, s );
654 block_residual_write_cavlc( h, s, BLOCK_INDEX_LUMA_DC , h->dct.luma16x16_dc, 16 );
657 if( h->mb.i_cbp_luma != 0 )
658 for( i = 0; i < 16; i++ )
660 h->mb.cache.non_zero_count[x264_scan8[i]] = array_non_zero_count( h->dct.luma4x4[i]+1, 15 );
661 block_residual_write_cavlc( h, s, i, h->dct.luma4x4[i]+1, 15 );
664 else if( h->mb.i_cbp_luma != 0 || h->mb.i_cbp_chroma != 0 )
666 cavlc_qp_delta( h, s );
667 x264_macroblock_luma_write_cavlc( h, s, 0, 3 );
669 if( h->mb.i_cbp_chroma != 0 )
671 /* Chroma DC residual present */
672 block_residual_write_cavlc( h, s, BLOCK_INDEX_CHROMA_DC, h->dct.chroma_dc[0], 4 );
673 block_residual_write_cavlc( h, s, BLOCK_INDEX_CHROMA_DC, h->dct.chroma_dc[1], 4 );
674 if( h->mb.i_cbp_chroma&0x02 ) /* Chroma AC residual present */
675 for( i = 16; i < 24; i++ )
677 h->mb.cache.non_zero_count[x264_scan8[i]] = array_non_zero_count( h->dct.luma4x4[i]+1, 15 );
678 block_residual_write_cavlc( h, s, i, h->dct.luma4x4[i]+1, 15 );
683 if( IS_INTRA( i_mb_type ) )
684 h->stat.frame.i_itex_bits += bs_pos(s) - i_mb_pos_tex;
686 h->stat.frame.i_ptex_bits += bs_pos(s) - i_mb_pos_tex;
691 /*****************************************************************************
692 * RD only; doesn't generate a valid bitstream
693 * doesn't write cbp or chroma dc (I don't know how much this matters)
694 * works on all partition sizes except 16x16
695 * for sub8x8, call once per 8x8 block
696 *****************************************************************************/
697 int x264_partition_size_cavlc( x264_t *h, int i8, int i_pixel )
700 const int i_mb_type = h->mb.i_type;
703 s.i_bits_encoded = 0;
705 if( i_mb_type == P_8x8 )
707 bs_write_ue( &s, sub_mb_type_p_to_golomb[ h->mb.i_sub_partition[i8] ] );
708 if( h->mb.pic.i_fref[0] > 1 )
709 bs_write_te( &s, h->mb.pic.i_fref[0] - 1, h->mb.cache.ref[0][x264_scan8[4*i8]] );
710 cavlc_mb8x8_mvd( h, &s, 0, i8 );
712 else if( i_mb_type == P_L0 )
714 if( h->mb.pic.i_fref[0] > 1 )
715 bs_write_te( &s, h->mb.pic.i_fref[0] - 1, h->mb.cache.ref[0][x264_scan8[4*i8]] );
716 if( h->mb.i_partition == D_16x8 )
717 cavlc_mb_mvd( h, &s, 0, 4*i8, 4 );
719 cavlc_mb_mvd( h, &s, 0, 4*i8, 2 );
721 else if( i_mb_type == B_8x8 )
723 bs_write_ue( &s, sub_mb_type_b_to_golomb[ h->mb.i_sub_partition[i8] ] );
725 if( h->mb.pic.i_fref[0] > 1
726 && x264_mb_partition_listX_table[0][ h->mb.i_sub_partition[i8] ] )
727 bs_write_te( &s, h->mb.pic.i_fref[0] - 1, h->mb.cache.ref[0][x264_scan8[4*i8]] );
728 if( h->mb.pic.i_fref[1] > 1
729 && x264_mb_partition_listX_table[1][ h->mb.i_sub_partition[i8] ] )
730 bs_write_te( &s, h->mb.pic.i_fref[1] - 1, h->mb.cache.ref[1][x264_scan8[4*i8]] );
732 cavlc_mb8x8_mvd( h, &s, 0, i8 );
733 cavlc_mb8x8_mvd( h, &s, 1, i8 );
737 x264_log(h, X264_LOG_ERROR, "invalid/unhandled mb_type\n" );
741 for( j = (i_pixel < PIXEL_8x8); j >= 0; j-- )
743 x264_macroblock_luma_write_cavlc( h, &s, i8, i8 );
744 h->mb.cache.non_zero_count[x264_scan8[16+i8]] = array_non_zero_count( h->dct.luma4x4[16+i8]+1, 15 );
745 block_residual_write_cavlc( h, &s, 16+i8, h->dct.luma4x4[16+i8]+1, 15 );
746 h->mb.cache.non_zero_count[x264_scan8[20+i8]] = array_non_zero_count( h->dct.luma4x4[20+i8]+1, 15 );
747 block_residual_write_cavlc( h, &s, 20+i8, h->dct.luma4x4[20+i8]+1, 15 );
748 i8 += x264_pixel_size[i_pixel].h >> 3;
751 return s.i_bits_encoded;
754 static int cavlc_intra4x4_pred_size( x264_t *h, int i4, int i_mode )
756 if( x264_mb_predict_intra4x4_mode( h, i4 ) == x264_mb_pred_mode4x4_fix( i_mode ) )
762 static int x264_partition_i8x8_size_cavlc( x264_t *h, int i8, int i_mode )
765 h->out.bs.i_bits_encoded = cavlc_intra4x4_pred_size( h, 4*i8, i_mode );
766 for( i4 = 0; i4 < 4; i4++ )
768 for( i = 0; i < 16; i++ )
769 h->dct.luma4x4[i4+i8*4][i] = h->dct.luma8x8[i8][i4+i*4];
770 h->mb.cache.non_zero_count[x264_scan8[i4+i8*4]] =
771 array_non_zero_count( h->dct.luma4x4[i4+i8*4], 16 );
772 block_residual_write_cavlc( h, &h->out.bs, i4+i8*4, h->dct.luma4x4[i4+i8*4], 16 );
774 return h->out.bs.i_bits_encoded;
777 static int x264_partition_i4x4_size_cavlc( x264_t *h, int i4, int i_mode )
779 h->out.bs.i_bits_encoded = cavlc_intra4x4_pred_size( h, i4, i_mode );
780 block_residual_write_cavlc( h, &h->out.bs, i4, h->dct.luma4x4[i4], 16 );
781 return h->out.bs.i_bits_encoded;
784 static int x264_i8x8_chroma_size_cavlc( x264_t *h )
786 h->out.bs.i_bits_encoded = bs_size_ue( x264_mb_pred_mode8x8c_fix[ h->mb.i_chroma_pred_mode ] );
787 if( h->mb.i_cbp_chroma != 0 )
789 block_residual_write_cavlc( h, &h->out.bs, BLOCK_INDEX_CHROMA_DC, h->dct.chroma_dc[0], 4 );
790 block_residual_write_cavlc( h, &h->out.bs, BLOCK_INDEX_CHROMA_DC, h->dct.chroma_dc[1], 4 );
792 if( h->mb.i_cbp_chroma == 2 )
795 for( i = 16; i < 24; i++ )
797 h->mb.cache.non_zero_count[x264_scan8[i]] = array_non_zero_count( h->dct.luma4x4[i]+1, 15 );
798 block_residual_write_cavlc( h, &h->out.bs, i, h->dct.luma4x4[i]+1, 15 );
802 return h->out.bs.i_bits_encoded;