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 *****************************************************************************/
27 #include "common/common.h"
28 #include "common/vlc.h"
29 #include "macroblock.h"
31 static const uint8_t intra4x4_cbp_to_golomb[48]=
33 3, 29, 30, 17, 31, 18, 37, 8, 32, 38, 19, 9, 20, 10, 11, 2,
34 16, 33, 34, 21, 35, 22, 39, 4, 36, 40, 23, 5, 24, 6, 7, 1,
35 41, 42, 43, 25, 44, 26, 46, 12, 45, 47, 27, 13, 28, 14, 15, 0
37 static const uint8_t inter_cbp_to_golomb[48]=
39 0, 2, 3, 7, 4, 8, 17, 13, 5, 18, 9, 14, 10, 15, 16, 11,
40 1, 32, 33, 36, 34, 37, 44, 40, 35, 45, 38, 41, 39, 42, 43, 19,
41 6, 24, 25, 20, 26, 21, 46, 28, 27, 47, 22, 29, 23, 30, 31, 12
43 static const uint8_t mb_type_b_to_golomb[3][9]=
45 { 4, 8, 12, 10, 6, 14, 16, 18, 20 }, /* D_16x8 */
46 { 5, 9, 13, 11, 7, 15, 17, 19, 21 }, /* D_8x16 */
47 { 1, -1, -1, -1, 2, -1, -1, -1, 3 } /* D_16x16 */
49 static const uint8_t sub_mb_type_p_to_golomb[4]=
53 static const uint8_t sub_mb_type_b_to_golomb[13]=
55 10, 4, 5, 1, 11, 6, 7, 2, 12, 8, 9, 3, 0
58 #define BLOCK_INDEX_CHROMA_DC (-1)
59 #define BLOCK_INDEX_LUMA_DC (-2)
61 static inline void bs_write_vlc( bs_t *s, vlc_t v )
63 bs_write( s, v.i_size, v.i_bits );
66 /****************************************************************************
67 * block_residual_write_cavlc:
68 ****************************************************************************/
69 static void block_residual_write_cavlc( x264_t *h, bs_t *s, int i_idx, int *l, int i_count )
71 int level[16], run[16];
72 int i_total, i_trailing;
81 /* first find i_last */
83 while( i_last >= 0 && l[i_last] == 0 )
98 /* level and run and total */
101 level[idx] = l[i_last--];
104 while( i_last >= 0 && l[i_last] == 0 )
111 i_total_zero += run[idx];
113 if( b_trailing && abs( level[idx] ) == 1 && i_trailing < 3 )
133 if( i_idx == BLOCK_INDEX_CHROMA_DC )
135 bs_write_vlc( s, x264_coeff_token[4][i_total*4+i_trailing] );
139 /* x264_mb_predict_non_zero_code return 0 <-> (16+16+1)>>1 = 16 */
140 static const int ct_index[17] = {0,0,1,1,2,2,2,2,3,3,3,3,3,3,3,3,3 };
143 if( i_idx == BLOCK_INDEX_LUMA_DC )
145 nC = x264_mb_predict_non_zero_code( h, 0 );
149 nC = x264_mb_predict_non_zero_code( h, i_idx );
152 bs_write_vlc( s, x264_coeff_token[ct_index[nC]][i_total*4+i_trailing] );
160 i_suffix_length = i_total > 10 && i_trailing < 3 ? 1 : 0;
163 bs_write( s, i_trailing, i_sign );
165 for( i = i_trailing; i < i_total; i++ )
169 /* calculate level code */
172 i_level_code = -2*level[i] - 1;
174 else /* if( level[i] > 0 ) */
176 i_level_code = 2 * level[i] - 2;
178 if( i == i_trailing && i_trailing < 3 )
180 i_level_code -=2; /* as level[i] can't be 1 for the first one if i_trailing < 3 */
183 if( ( i_level_code >> i_suffix_length ) < 14 )
185 bs_write_vlc( s, x264_level_prefix[i_level_code >> i_suffix_length] );
186 if( i_suffix_length > 0 )
188 bs_write( s, i_suffix_length, i_level_code );
191 else if( i_suffix_length == 0 && i_level_code < 30 )
193 bs_write_vlc( s, x264_level_prefix[14] );
194 bs_write( s, 4, i_level_code - 14 );
196 else if( i_suffix_length > 0 && ( i_level_code >> i_suffix_length ) == 14 )
198 bs_write_vlc( s, x264_level_prefix[14] );
199 bs_write( s, i_suffix_length, i_level_code );
203 bs_write_vlc( s, x264_level_prefix[15] );
204 i_level_code -= 15 << i_suffix_length;
205 if( i_suffix_length == 0 )
210 if( i_level_code >= ( 1 << 12 ) || i_level_code < 0 )
212 x264_log(h, X264_LOG_WARNING, "OVERFLOW levelcode=%d\n", i_level_code );
215 bs_write( s, 12, i_level_code ); /* check overflow ?? */
218 if( i_suffix_length == 0 )
222 if( abs( level[i] ) > ( 3 << ( i_suffix_length - 1 ) ) && i_suffix_length < 6 )
228 if( i_total < i_count )
230 if( i_idx == BLOCK_INDEX_CHROMA_DC )
232 bs_write_vlc( s, x264_total_zeros_dc[i_total-1][i_total_zero] );
236 bs_write_vlc( s, x264_total_zeros[i_total-1][i_total_zero] );
240 for( i = 0, i_zero_left = i_total_zero; i < i_total - 1; i++ )
244 if( i_zero_left <= 0 )
249 i_zl = X264_MIN( i_zero_left - 1, 6 );
251 bs_write_vlc( s, x264_run_before[i_zl][run[i]] );
253 i_zero_left -= run[i];
257 static void cavlc_qp_delta( x264_t *h, bs_t *s )
259 int i_dqp = h->mb.i_qp - h->mb.i_last_qp;
264 else if( i_dqp > 25 )
267 bs_write_se( s, i_dqp );
270 static void cavlc_mb_mvd( x264_t *h, bs_t *s, int i_list, int idx, int width )
273 x264_mb_predict_mv( h, i_list, idx, width, mvp );
274 bs_write_se( s, h->mb.cache.mv[i_list][x264_scan8[idx]][0] - mvp[0] );
275 bs_write_se( s, h->mb.cache.mv[i_list][x264_scan8[idx]][1] - mvp[1] );
278 static void cavlc_mb8x8_mvd( x264_t *h, bs_t *s, int i_list, int i )
280 if( !x264_mb_partition_listX_table[i_list][ h->mb.i_sub_partition[i] ] )
283 switch( h->mb.i_sub_partition[i] )
288 cavlc_mb_mvd( h, s, i_list, 4*i, 2 );
293 cavlc_mb_mvd( h, s, i_list, 4*i+0, 2 );
294 cavlc_mb_mvd( h, s, i_list, 4*i+2, 2 );
299 cavlc_mb_mvd( h, s, i_list, 4*i+0, 1 );
300 cavlc_mb_mvd( h, s, i_list, 4*i+1, 1 );
305 cavlc_mb_mvd( h, s, i_list, 4*i+0, 1 );
306 cavlc_mb_mvd( h, s, i_list, 4*i+1, 1 );
307 cavlc_mb_mvd( h, s, i_list, 4*i+2, 1 );
308 cavlc_mb_mvd( h, s, i_list, 4*i+3, 1 );
313 static inline void x264_macroblock_luma_write_cavlc( x264_t *h, bs_t *s, int i8start, int i8end )
316 if( h->mb.b_transform_8x8 )
318 /* shuffle 8x8 dct coeffs into 4x4 lists */
319 for( i8 = i8start; i8 <= i8end; i8++ )
320 if( h->mb.i_cbp_luma & (1 << i8) )
321 for( i4 = 0; i4 < 4; i4++ )
323 for( i = 0; i < 16; i++ )
324 h->dct.block[i4+i8*4].luma4x4[i] = h->dct.luma8x8[i8][i4+i*4];
325 h->mb.cache.non_zero_count[x264_scan8[i4+i8*4]] =
326 array_non_zero_count( h->dct.block[i4+i8*4].luma4x4, 16 );
330 for( i8 = i8start; i8 <= i8end; i8++ )
331 if( h->mb.i_cbp_luma & (1 << i8) )
332 for( i4 = 0; i4 < 4; i4++ )
333 block_residual_write_cavlc( h, s, i4+i8*4, h->dct.block[i4+i8*4].luma4x4, 16 );
336 /*****************************************************************************
337 * x264_macroblock_write:
338 *****************************************************************************/
339 void x264_macroblock_write_cavlc( x264_t *h, bs_t *s )
341 const int i_mb_type = h->mb.i_type;
346 const int i_mb_pos_start = bs_pos( s );
350 switch( h->sh.i_type )
362 x264_log(h, X264_LOG_ERROR, "internal error or slice unsupported\n" );
367 && (!(h->mb.i_mb_y & 1) || IS_SKIP(h->mb.type[h->mb.i_mb_xy - h->mb.i_mb_stride])) )
369 bs_write1( s, h->mb.b_interlaced );
376 if( i_mb_type == I_PCM )
379 bs_write_ue( s, i_mb_i_offset + 25 );
382 s->i_bits_encoded += 384*8;
386 for( i = 0; i < 16*16; i++ )
388 const int x = 16 * h->mb.i_mb_x + (i % 16);
389 const int y = 16 * h->mb.i_mb_y + (i / 16);
390 bs_write( s, 8, h->fenc->plane[0][y*h->mb.pic.i_stride[0]+x] );
393 for( i = 0; i < 8*8; i++ )
395 const int x = 8 * h->mb.i_mb_x + (i % 8);
396 const int y = 8 * h->mb.i_mb_y + (i / 8);
397 bs_write( s, 8, h->fenc->plane[1][y*h->mb.pic.i_stride[1]+x] );
400 for( i = 0; i < 8*8; i++ )
402 const int x = 8 * h->mb.i_mb_x + (i % 8);
403 const int y = 8 * h->mb.i_mb_y + (i / 8);
404 bs_write( s, 8, h->fenc->plane[2][y*h->mb.pic.i_stride[2]+x] );
409 else if( i_mb_type == I_4x4 || i_mb_type == I_8x8 )
411 int di = i_mb_type == I_8x8 ? 4 : 1;
412 bs_write_ue( s, i_mb_i_offset + 0 );
413 if( h->pps->b_transform_8x8_mode )
414 bs_write1( s, h->mb.b_transform_8x8 );
416 /* Prediction: Luma */
417 for( i = 0; i < 16; i += di )
419 int i_pred = x264_mb_predict_intra4x4_mode( h, i );
420 int i_mode = x264_mb_pred_mode4x4_fix( h->mb.cache.intra4x4_pred_mode[x264_scan8[i]] );
422 if( i_pred == i_mode)
424 bs_write1( s, 1 ); /* b_prev_intra4x4_pred_mode */
428 bs_write1( s, 0 ); /* b_prev_intra4x4_pred_mode */
429 if( i_mode < i_pred )
431 bs_write( s, 3, i_mode );
435 bs_write( s, 3, i_mode - 1 );
439 bs_write_ue( s, x264_mb_pred_mode8x8c_fix[ h->mb.i_chroma_pred_mode ] );
441 else if( i_mb_type == I_16x16 )
443 bs_write_ue( s, i_mb_i_offset + 1 + x264_mb_pred_mode16x16_fix[h->mb.i_intra16x16_pred_mode] +
444 h->mb.i_cbp_chroma * 4 + ( h->mb.i_cbp_luma == 0 ? 0 : 12 ) );
445 bs_write_ue( s, x264_mb_pred_mode8x8c_fix[ h->mb.i_chroma_pred_mode ] );
447 else if( i_mb_type == P_L0 )
451 if( h->mb.i_partition == D_16x16 )
455 if( h->mb.pic.i_fref[0] > 1 )
457 bs_write_te( s, h->mb.pic.i_fref[0] - 1, h->mb.cache.ref[0][x264_scan8[0]] );
459 x264_mb_predict_mv( h, 0, 0, 4, mvp );
460 bs_write_se( s, h->mb.cache.mv[0][x264_scan8[0]][0] - mvp[0] );
461 bs_write_se( s, h->mb.cache.mv[0][x264_scan8[0]][1] - mvp[1] );
463 else if( h->mb.i_partition == D_16x8 )
466 if( h->mb.pic.i_fref[0] > 1 )
468 bs_write_te( s, h->mb.pic.i_fref[0] - 1, h->mb.cache.ref[0][x264_scan8[0]] );
469 bs_write_te( s, h->mb.pic.i_fref[0] - 1, h->mb.cache.ref[0][x264_scan8[8]] );
472 x264_mb_predict_mv( h, 0, 0, 4, mvp );
473 bs_write_se( s, h->mb.cache.mv[0][x264_scan8[0]][0] - mvp[0] );
474 bs_write_se( s, h->mb.cache.mv[0][x264_scan8[0]][1] - mvp[1] );
476 x264_mb_predict_mv( h, 0, 8, 4, mvp );
477 bs_write_se( s, h->mb.cache.mv[0][x264_scan8[8]][0] - mvp[0] );
478 bs_write_se( s, h->mb.cache.mv[0][x264_scan8[8]][1] - mvp[1] );
480 else if( h->mb.i_partition == D_8x16 )
483 if( h->mb.pic.i_fref[0] > 1 )
485 bs_write_te( s, h->mb.pic.i_fref[0] - 1, h->mb.cache.ref[0][x264_scan8[0]] );
486 bs_write_te( s, h->mb.pic.i_fref[0] - 1, h->mb.cache.ref[0][x264_scan8[4]] );
489 x264_mb_predict_mv( h, 0, 0, 2, mvp );
490 bs_write_se( s, h->mb.cache.mv[0][x264_scan8[0]][0] - mvp[0] );
491 bs_write_se( s, h->mb.cache.mv[0][x264_scan8[0]][1] - mvp[1] );
493 x264_mb_predict_mv( h, 0, 4, 2, mvp );
494 bs_write_se( s, h->mb.cache.mv[0][x264_scan8[4]][0] - mvp[0] );
495 bs_write_se( s, h->mb.cache.mv[0][x264_scan8[4]][1] - mvp[1] );
498 else if( i_mb_type == P_8x8 )
502 if( h->mb.cache.ref[0][x264_scan8[0]] == 0 && h->mb.cache.ref[0][x264_scan8[4]] == 0 &&
503 h->mb.cache.ref[0][x264_scan8[8]] == 0 && h->mb.cache.ref[0][x264_scan8[12]] == 0 )
514 for( i = 0; i < 4; i++ )
516 bs_write_ue( s, sub_mb_type_p_to_golomb[ h->mb.i_sub_partition[i] ] );
519 if( h->mb.pic.i_fref[0] > 1 && b_sub_ref0 )
521 bs_write_te( s, h->mb.pic.i_fref[0] - 1, h->mb.cache.ref[0][x264_scan8[0]] );
522 bs_write_te( s, h->mb.pic.i_fref[0] - 1, h->mb.cache.ref[0][x264_scan8[4]] );
523 bs_write_te( s, h->mb.pic.i_fref[0] - 1, h->mb.cache.ref[0][x264_scan8[8]] );
524 bs_write_te( s, h->mb.pic.i_fref[0] - 1, h->mb.cache.ref[0][x264_scan8[12]] );
527 for( i = 0; i < 4; i++ )
528 cavlc_mb8x8_mvd( h, s, 0, i );
530 else if( i_mb_type == B_8x8 )
532 bs_write_ue( s, 22 );
535 for( i = 0; i < 4; i++ )
537 bs_write_ue( s, sub_mb_type_b_to_golomb[ h->mb.i_sub_partition[i] ] );
540 for( i = 0; i < 4; i++ )
542 if( x264_mb_partition_listX_table[0][ h->mb.i_sub_partition[i] ] )
544 bs_write_te( s, h->mb.pic.i_fref[0] - 1, h->mb.cache.ref[0][x264_scan8[i*4]] );
547 for( i = 0; i < 4; i++ )
549 if( x264_mb_partition_listX_table[1][ h->mb.i_sub_partition[i] ] )
551 bs_write_te( s, h->mb.pic.i_fref[1] - 1, h->mb.cache.ref[1][x264_scan8[i*4]] );
555 for( i = 0; i < 4; i++ )
556 cavlc_mb8x8_mvd( h, s, 0, i );
557 for( i = 0; i < 4; i++ )
558 cavlc_mb8x8_mvd( h, s, 1, i );
560 else if( i_mb_type != B_DIRECT )
569 /* init ref list utilisations */
570 for( i = 0; i < 2; i++ )
572 b_list[0][i] = x264_mb_type_list0_table[i_mb_type][i];
573 b_list[1][i] = x264_mb_type_list1_table[i_mb_type][i];
577 bs_write_ue( s, mb_type_b_to_golomb[ h->mb.i_partition - D_16x8 ][ i_mb_type - B_L0_L0 ] );
579 for( i_list = 0; i_list < 2; i_list++ )
581 const int i_ref_max = i_list == 0 ? h->mb.pic.i_fref[0] : h->mb.pic.i_fref[1];
585 switch( h->mb.i_partition )
588 if( b_list[i_list][0] ) bs_write_te( s, i_ref_max - 1, h->mb.cache.ref[i_list][x264_scan8[0]] );
591 if( b_list[i_list][0] ) bs_write_te( s, i_ref_max - 1, h->mb.cache.ref[i_list][x264_scan8[0]] );
592 if( b_list[i_list][1] ) bs_write_te( s, i_ref_max - 1, h->mb.cache.ref[i_list][x264_scan8[8]] );
595 if( b_list[i_list][0] ) bs_write_te( s, i_ref_max - 1, h->mb.cache.ref[i_list][x264_scan8[0]] );
596 if( b_list[i_list][1] ) bs_write_te( s, i_ref_max - 1, h->mb.cache.ref[i_list][x264_scan8[4]] );
601 for( i_list = 0; i_list < 2; i_list++ )
603 switch( h->mb.i_partition )
606 if( b_list[i_list][0] )
608 x264_mb_predict_mv( h, i_list, 0, 4, mvp );
609 bs_write_se( s, h->mb.cache.mv[i_list][x264_scan8[0]][0] - mvp[0] );
610 bs_write_se( s, h->mb.cache.mv[i_list][x264_scan8[0]][1] - mvp[1] );
614 if( b_list[i_list][0] )
616 x264_mb_predict_mv( h, i_list, 0, 4, mvp );
617 bs_write_se( s, h->mb.cache.mv[i_list][x264_scan8[0]][0] - mvp[0] );
618 bs_write_se( s, h->mb.cache.mv[i_list][x264_scan8[0]][1] - mvp[1] );
620 if( b_list[i_list][1] )
622 x264_mb_predict_mv( h, i_list, 8, 4, mvp );
623 bs_write_se( s, h->mb.cache.mv[i_list][x264_scan8[8]][0] - mvp[0] );
624 bs_write_se( s, h->mb.cache.mv[i_list][x264_scan8[8]][1] - mvp[1] );
628 if( b_list[i_list][0] )
630 x264_mb_predict_mv( h, i_list, 0, 2, mvp );
631 bs_write_se( s, h->mb.cache.mv[i_list][x264_scan8[0]][0] - mvp[0] );
632 bs_write_se( s, h->mb.cache.mv[i_list][x264_scan8[0]][1] - mvp[1] );
634 if( b_list[i_list][1] )
636 x264_mb_predict_mv( h, i_list, 4, 2, mvp );
637 bs_write_se( s, h->mb.cache.mv[i_list][x264_scan8[4]][0] - mvp[0] );
638 bs_write_se( s, h->mb.cache.mv[i_list][x264_scan8[4]][1] - mvp[1] );
644 else if( i_mb_type == B_DIRECT )
650 x264_log(h, X264_LOG_ERROR, "invalid/unhandled mb_type\n" );
655 i_mb_pos_tex = bs_pos( s );
656 h->stat.frame.i_hdr_bits += i_mb_pos_tex - i_mb_pos_start;
659 /* Coded block patern */
660 if( i_mb_type == I_4x4 || i_mb_type == I_8x8 )
662 bs_write_ue( s, intra4x4_cbp_to_golomb[( h->mb.i_cbp_chroma << 4 )|h->mb.i_cbp_luma] );
664 else if( i_mb_type != I_16x16 )
666 bs_write_ue( s, inter_cbp_to_golomb[( h->mb.i_cbp_chroma << 4 )|h->mb.i_cbp_luma] );
669 /* transform size 8x8 flag */
670 if( h->mb.cache.b_transform_8x8_allowed && h->mb.i_cbp_luma && !IS_INTRA(i_mb_type) )
672 bs_write1( s, h->mb.b_transform_8x8 );
676 if( i_mb_type == I_16x16 )
678 cavlc_qp_delta( h, s );
681 block_residual_write_cavlc( h, s, BLOCK_INDEX_LUMA_DC , h->dct.luma16x16_dc, 16 );
684 if( h->mb.i_cbp_luma != 0 )
685 for( i = 0; i < 16; i++ )
686 block_residual_write_cavlc( h, s, i, h->dct.block[i].residual_ac, 15 );
688 else if( h->mb.i_cbp_luma != 0 || h->mb.i_cbp_chroma != 0 )
690 cavlc_qp_delta( h, s );
691 x264_macroblock_luma_write_cavlc( h, s, 0, 3 );
693 if( h->mb.i_cbp_chroma != 0 )
695 /* Chroma DC residual present */
696 block_residual_write_cavlc( h, s, BLOCK_INDEX_CHROMA_DC, h->dct.chroma_dc[0], 4 );
697 block_residual_write_cavlc( h, s, BLOCK_INDEX_CHROMA_DC, h->dct.chroma_dc[1], 4 );
698 if( h->mb.i_cbp_chroma&0x02 ) /* Chroma AC residual present */
699 for( i = 0; i < 8; i++ )
700 block_residual_write_cavlc( h, s, 16 + i, h->dct.block[16+i].residual_ac, 15 );
704 if( IS_INTRA( i_mb_type ) )
705 h->stat.frame.i_itex_bits += bs_pos(s) - i_mb_pos_tex;
707 h->stat.frame.i_ptex_bits += bs_pos(s) - i_mb_pos_tex;
712 /*****************************************************************************
713 * RD only; doesn't generate a valid bitstream
714 * doesn't write cbp or chroma dc (I don't know how much this matters)
715 * works on all partition sizes except 16x16
716 * for sub8x8, call once per 8x8 block
717 *****************************************************************************/
718 int x264_partition_size_cavlc( x264_t *h, int i8, int i_pixel )
721 const int i_mb_type = h->mb.i_type;
724 s.i_bits_encoded = 0;
726 if( i_mb_type == P_8x8 )
728 bs_write_ue( &s, sub_mb_type_p_to_golomb[ h->mb.i_sub_partition[i8] ] );
729 if( h->mb.pic.i_fref[0] > 1 )
730 bs_write_te( &s, h->mb.pic.i_fref[0] - 1, h->mb.cache.ref[0][x264_scan8[4*i8]] );
731 cavlc_mb8x8_mvd( h, &s, 0, i8 );
733 else if( i_mb_type == P_L0 )
735 if( h->mb.pic.i_fref[0] > 1 )
736 bs_write_te( &s, h->mb.pic.i_fref[0] - 1, h->mb.cache.ref[0][x264_scan8[4*i8]] );
737 if( h->mb.i_partition == D_16x8 )
738 cavlc_mb_mvd( h, &s, 0, 4*i8, 4 );
740 cavlc_mb_mvd( h, &s, 0, 4*i8, 2 );
742 else if( i_mb_type == B_8x8 )
744 bs_write_ue( &s, sub_mb_type_b_to_golomb[ h->mb.i_sub_partition[i8] ] );
746 if( h->mb.pic.i_fref[0] > 1
747 && x264_mb_partition_listX_table[0][ h->mb.i_sub_partition[i8] ] )
748 bs_write_te( &s, h->mb.pic.i_fref[0] - 1, h->mb.cache.ref[0][x264_scan8[4*i8]] );
749 if( h->mb.pic.i_fref[1] > 1
750 && x264_mb_partition_listX_table[1][ h->mb.i_sub_partition[i8] ] )
751 bs_write_te( &s, h->mb.pic.i_fref[1] - 1, h->mb.cache.ref[1][x264_scan8[4*i8]] );
753 cavlc_mb8x8_mvd( h, &s, 0, i8 );
754 cavlc_mb8x8_mvd( h, &s, 1, i8 );
758 x264_log(h, X264_LOG_ERROR, "invalid/unhandled mb_type\n" );
762 for( j = (i_pixel < PIXEL_8x8); j >= 0; j-- )
764 x264_macroblock_luma_write_cavlc( h, &s, i8, i8 );
766 block_residual_write_cavlc( h, &s, i8, h->dct.block[16+i8 ].residual_ac, 15 );
767 block_residual_write_cavlc( h, &s, i8+4, h->dct.block[16+i8+4].residual_ac, 15 );
769 i8 += x264_pixel_size[i_pixel].h >> 3;
772 return s.i_bits_encoded;
775 static int cavlc_intra4x4_pred_size( x264_t *h, int i4, int i_mode )
777 if( x264_mb_predict_intra4x4_mode( h, i4 ) == x264_mb_pred_mode4x4_fix( i_mode ) )
783 static int x264_partition_i8x8_size_cavlc( x264_t *h, int i8, int i_mode )
786 h->out.bs.i_bits_encoded = cavlc_intra4x4_pred_size( h, 4*i8, i_mode );
787 for( i4 = 0; i4 < 4; i4++ )
789 for( i = 0; i < 16; i++ )
790 h->dct.block[i4+i8*4].luma4x4[i] = h->dct.luma8x8[i8][i4+i*4];
791 h->mb.cache.non_zero_count[x264_scan8[i4+i8*4]] =
792 array_non_zero_count( h->dct.block[i4+i8*4].luma4x4, 16 );
793 block_residual_write_cavlc( h, &h->out.bs, i4+i8*4, h->dct.block[i4+i8*4].luma4x4, 16 );
795 return h->out.bs.i_bits_encoded;
798 static int x264_partition_i4x4_size_cavlc( x264_t *h, int i4, int i_mode )
800 h->out.bs.i_bits_encoded = cavlc_intra4x4_pred_size( h, i4, i_mode );
801 block_residual_write_cavlc( h, &h->out.bs, i4, h->dct.block[i4].luma4x4, 16 );
802 return h->out.bs.i_bits_encoded;