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 *****************************************************************************/
28 #include "common/common.h"
29 #include "common/vlc.h"
30 #include "macroblock.h"
32 static const uint8_t intra4x4_cbp_to_golomb[48]=
34 3, 29, 30, 17, 31, 18, 37, 8, 32, 38, 19, 9, 20, 10, 11, 2,
35 16, 33, 34, 21, 35, 22, 39, 4, 36, 40, 23, 5, 24, 6, 7, 1,
36 41, 42, 43, 25, 44, 26, 46, 12, 45, 47, 27, 13, 28, 14, 15, 0
38 static const uint8_t inter_cbp_to_golomb[48]=
40 0, 2, 3, 7, 4, 8, 17, 13, 5, 18, 9, 14, 10, 15, 16, 11,
41 1, 32, 33, 36, 34, 37, 44, 40, 35, 45, 38, 41, 39, 42, 43, 19,
42 6, 24, 25, 20, 26, 21, 46, 28, 27, 47, 22, 29, 23, 30, 31, 12
44 static const uint8_t mb_type_b_to_golomb[3][9]=
46 { 4, 8, 12, 10, 6, 14, 16, 18, 20 }, /* D_16x8 */
47 { 5, 9, 13, 11, 7, 15, 17, 19, 21 }, /* D_8x16 */
48 { 1, -1, -1, -1, 2, -1, -1, -1, 3 } /* D_16x16 */
50 static const uint8_t sub_mb_type_p_to_golomb[4]=
54 static const uint8_t sub_mb_type_b_to_golomb[13]=
56 10, 4, 5, 1, 11, 6, 7, 2, 12, 8, 9, 3, 0
59 static const uint8_t block_idx_x[16] =
61 0, 1, 0, 1, 2, 3, 2, 3, 0, 1, 0, 1, 2, 3, 2, 3
63 static const uint8_t block_idx_y[16] =
65 0, 0, 1, 1, 0, 0, 1, 1, 2, 2, 3, 3, 2, 2, 3, 3
67 static const uint8_t block_idx_xy[4][4] =
75 #define BLOCK_INDEX_CHROMA_DC (-1)
76 #define BLOCK_INDEX_LUMA_DC (-2)
78 static inline void bs_write_vlc( bs_t *s, vlc_t v )
80 bs_write( s, v.i_size, v.i_bits );
83 /****************************************************************************
84 * block_residual_write_cavlc:
85 ****************************************************************************/
86 static void block_residual_write_cavlc( x264_t *h, bs_t *s, int i_idx, int *l, int i_count )
88 int level[16], run[16];
89 int i_total, i_trailing;
98 /* first find i_last */
100 while( i_last >= 0 && l[i_last] == 0 )
115 /* level and run and total */
118 level[idx] = l[i_last--];
121 while( i_last >= 0 && l[i_last] == 0 )
128 i_total_zero += run[idx];
130 if( b_trailing && abs( level[idx] ) == 1 && i_trailing < 3 )
150 if( i_idx == BLOCK_INDEX_CHROMA_DC )
152 bs_write_vlc( s, x264_coeff_token[4][i_total*4+i_trailing] );
156 /* x264_mb_predict_non_zero_code return 0 <-> (16+16+1)>>1 = 16 */
157 static const int ct_index[17] = {0,0,1,1,2,2,2,2,3,3,3,3,3,3,3,3,3 };
160 if( i_idx == BLOCK_INDEX_LUMA_DC )
162 nC = x264_mb_predict_non_zero_code( h, 0 );
166 nC = x264_mb_predict_non_zero_code( h, i_idx );
169 bs_write_vlc( s, x264_coeff_token[ct_index[nC]][i_total*4+i_trailing] );
177 i_suffix_length = i_total > 10 && i_trailing < 3 ? 1 : 0;
180 bs_write( s, i_trailing, i_sign );
182 for( i = i_trailing; i < i_total; i++ )
186 /* calculate level code */
189 i_level_code = -2*level[i] - 1;
191 else /* if( level[i] > 0 ) */
193 i_level_code = 2 * level[i] - 2;
195 if( i == i_trailing && i_trailing < 3 )
197 i_level_code -=2; /* as level[i] can't be 1 for the first one if i_trailing < 3 */
200 if( ( i_level_code >> i_suffix_length ) < 14 )
202 bs_write_vlc( s, x264_level_prefix[i_level_code >> i_suffix_length] );
203 if( i_suffix_length > 0 )
205 bs_write( s, i_suffix_length, i_level_code );
208 else if( i_suffix_length == 0 && i_level_code < 30 )
210 bs_write_vlc( s, x264_level_prefix[14] );
211 bs_write( s, 4, i_level_code - 14 );
213 else if( i_suffix_length > 0 && ( i_level_code >> i_suffix_length ) == 14 )
215 bs_write_vlc( s, x264_level_prefix[14] );
216 bs_write( s, i_suffix_length, i_level_code );
220 bs_write_vlc( s, x264_level_prefix[15] );
221 i_level_code -= 15 << i_suffix_length;
222 if( i_suffix_length == 0 )
227 if( i_level_code >= ( 1 << 12 ) || i_level_code < 0 )
229 fprintf( stderr, "OVERFLOW levelcode=%d\n", i_level_code );
232 bs_write( s, 12, i_level_code ); /* check overflow ?? */
235 if( i_suffix_length == 0 )
239 if( abs( level[i] ) > ( 3 << ( i_suffix_length - 1 ) ) && i_suffix_length < 6 )
245 if( i_total < i_count )
247 if( i_idx == BLOCK_INDEX_CHROMA_DC )
249 bs_write_vlc( s, x264_total_zeros_dc[i_total-1][i_total_zero] );
253 bs_write_vlc( s, x264_total_zeros[i_total-1][i_total_zero] );
257 for( i = 0, i_zero_left = i_total_zero; i < i_total - 1; i++ )
261 if( i_zero_left <= 0 )
266 i_zl = X264_MIN( i_zero_left - 1, 6 );
268 bs_write_vlc( s, x264_run_before[i_zl][run[i]] );
270 i_zero_left -= run[i];
274 static void x264_sub_mb_mv_write_cavlc( x264_t *h, bs_t *s, int i_list )
277 for( i = 0; i < 4; i++ )
281 if( !x264_mb_partition_listX_table[i_list][ h->mb.i_sub_partition[i] ] )
286 switch( h->mb.i_sub_partition[i] )
291 x264_mb_predict_mv( h, i_list, 4*i, 2, mvp );
292 bs_write_se( s, h->mb.cache.mv[i_list][x264_scan8[4*i]][0] - mvp[0] );
293 bs_write_se( s, h->mb.cache.mv[i_list][x264_scan8[4*i]][1] - mvp[1] );
298 x264_mb_predict_mv( h, i_list, 4*i+0, 2, mvp );
299 bs_write_se( s, h->mb.cache.mv[i_list][x264_scan8[4*i]][0] - mvp[0] );
300 bs_write_se( s, h->mb.cache.mv[i_list][x264_scan8[4*i]][1] - mvp[1] );
302 x264_mb_predict_mv( h, i_list, 4*i+2, 2, mvp );
303 bs_write_se( s, h->mb.cache.mv[i_list][x264_scan8[4*i+2]][0] - mvp[0] );
304 bs_write_se( s, h->mb.cache.mv[i_list][x264_scan8[4*i+2]][1] - mvp[1] );
309 x264_mb_predict_mv( h, i_list, 4*i+0, 1, mvp );
310 bs_write_se( s, h->mb.cache.mv[i_list][x264_scan8[4*i]][0] - mvp[0] );
311 bs_write_se( s, h->mb.cache.mv[i_list][x264_scan8[4*i]][1] - mvp[1] );
313 x264_mb_predict_mv( h, i_list, 4*i+1, 1, mvp );
314 bs_write_se( s, h->mb.cache.mv[i_list][x264_scan8[4*i+1]][0] - mvp[0] );
315 bs_write_se( s, h->mb.cache.mv[i_list][x264_scan8[4*i+1]][1] - mvp[1] );
320 x264_mb_predict_mv( h, i_list, 4*i+0, 1, mvp );
321 bs_write_se( s, h->mb.cache.mv[i_list][x264_scan8[4*i]][0] - mvp[0] );
322 bs_write_se( s, h->mb.cache.mv[i_list][x264_scan8[4*i]][1] - mvp[1] );
324 x264_mb_predict_mv( h, i_list, 4*i+1, 1, mvp );
325 bs_write_se( s, h->mb.cache.mv[i_list][x264_scan8[4*i+1]][0] - mvp[0] );
326 bs_write_se( s, h->mb.cache.mv[i_list][x264_scan8[4*i+1]][1] - mvp[1] );
328 x264_mb_predict_mv( h, i_list, 4*i+2, 1, mvp );
329 bs_write_se( s, h->mb.cache.mv[i_list][x264_scan8[4*i+2]][0] - mvp[0] );
330 bs_write_se( s, h->mb.cache.mv[i_list][x264_scan8[4*i+2]][1] - mvp[1] );
332 x264_mb_predict_mv( h, i_list, 4*i+3, 1, mvp );
333 bs_write_se( s, h->mb.cache.mv[i_list][x264_scan8[4*i+3]][0] - mvp[0] );
334 bs_write_se( s, h->mb.cache.mv[i_list][x264_scan8[4*i+3]][1] - mvp[1] );
340 /*****************************************************************************
341 * x264_macroblock_write:
342 *****************************************************************************/
343 void x264_macroblock_write_cavlc( x264_t *h, bs_t *s )
345 const int i_mb_type = h->mb.i_type;
346 const int i_mb_pos_start = bs_pos( s );
351 switch( h->sh.i_type )
363 fprintf( stderr, "internal error or slice unsupported\n" );
371 if( i_mb_type == I_PCM )
374 bs_write_ue( s, i_mb_i_offset + 25 );
378 for( i = 0; i < 16*16; i++ )
380 const int x = 16 * h->mb.i_mb_x + (i % 16);
381 const int y = 16 * h->mb.i_mb_y + (i / 16);
382 bs_write( s, 8, h->fenc->plane[0][y*h->mb.pic.i_stride[0]+x] );
385 for( i = 0; i < 8*8; i++ )
387 const int x = 8 * h->mb.i_mb_x + (i % 8);
388 const int y = 8 * h->mb.i_mb_y + (i / 8);
389 bs_write( s, 8, h->fenc->plane[1][y*h->mb.pic.i_stride[1]+x] );
392 for( i = 0; i < 8*8; i++ )
394 const int x = 8 * h->mb.i_mb_x + (i % 8);
395 const int y = 8 * h->mb.i_mb_y + (i / 8);
396 bs_write( s, 8, h->fenc->plane[2][y*h->mb.pic.i_stride[2]+x] );
400 else if( i_mb_type == I_4x4 )
402 bs_write_ue( s, i_mb_i_offset + 0 );
404 /* Prediction: Luma */
405 for( i = 0; i < 16; i++ )
407 int i_pred = x264_mb_predict_intra4x4_mode( h, i );
408 int i_mode = h->mb.cache.intra4x4_pred_mode[x264_scan8[i]];
410 if( i_pred == i_mode)
412 bs_write1( s, 1 ); /* b_prev_intra4x4_pred_mode */
416 bs_write1( s, 0 ); /* b_prev_intra4x4_pred_mode */
417 if( i_mode < i_pred )
419 bs_write( s, 3, i_mode );
423 bs_write( s, 3, i_mode - 1 );
427 bs_write_ue( s, h->mb.i_chroma_pred_mode );
429 else if( i_mb_type == I_16x16 )
431 bs_write_ue( s, i_mb_i_offset + 1 + h->mb.i_intra16x16_pred_mode +
432 h->mb.i_cbp_chroma * 4 + ( h->mb.i_cbp_luma == 0 ? 0 : 12 ) );
433 bs_write_ue( s, h->mb.i_chroma_pred_mode );
435 else if( i_mb_type == P_L0 )
439 if( h->mb.i_partition == D_16x16 )
443 if( h->sh.i_num_ref_idx_l0_active > 1 )
445 bs_write_te( s, h->sh.i_num_ref_idx_l0_active - 1, h->mb.cache.ref[0][x264_scan8[0]] );
447 x264_mb_predict_mv( h, 0, 0, 4, mvp );
448 bs_write_se( s, h->mb.cache.mv[0][x264_scan8[0]][0] - mvp[0] );
449 bs_write_se( s, h->mb.cache.mv[0][x264_scan8[0]][1] - mvp[1] );
451 else if( h->mb.i_partition == D_16x8 )
454 if( h->sh.i_num_ref_idx_l0_active > 1 )
456 bs_write_te( s, h->sh.i_num_ref_idx_l0_active - 1, h->mb.cache.ref[0][x264_scan8[0]] );
457 bs_write_te( s, h->sh.i_num_ref_idx_l0_active - 1, h->mb.cache.ref[0][x264_scan8[8]] );
460 x264_mb_predict_mv( h, 0, 0, 4, mvp );
461 bs_write_se( s, h->mb.cache.mv[0][x264_scan8[0]][0] - mvp[0] );
462 bs_write_se( s, h->mb.cache.mv[0][x264_scan8[0]][1] - mvp[1] );
464 x264_mb_predict_mv( h, 0, 8, 4, mvp );
465 bs_write_se( s, h->mb.cache.mv[0][x264_scan8[8]][0] - mvp[0] );
466 bs_write_se( s, h->mb.cache.mv[0][x264_scan8[8]][1] - mvp[1] );
468 else if( h->mb.i_partition == D_8x16 )
471 if( h->sh.i_num_ref_idx_l0_active > 1 )
473 bs_write_te( s, h->sh.i_num_ref_idx_l0_active - 1, h->mb.cache.ref[0][x264_scan8[0]] );
474 bs_write_te( s, h->sh.i_num_ref_idx_l0_active - 1, h->mb.cache.ref[0][x264_scan8[4]] );
477 x264_mb_predict_mv( h, 0, 0, 2, mvp );
478 bs_write_se( s, h->mb.cache.mv[0][x264_scan8[0]][0] - mvp[0] );
479 bs_write_se( s, h->mb.cache.mv[0][x264_scan8[0]][1] - mvp[1] );
481 x264_mb_predict_mv( h, 0, 4, 2, mvp );
482 bs_write_se( s, h->mb.cache.mv[0][x264_scan8[4]][0] - mvp[0] );
483 bs_write_se( s, h->mb.cache.mv[0][x264_scan8[4]][1] - mvp[1] );
486 else if( i_mb_type == P_8x8 )
490 if( h->mb.cache.ref[0][x264_scan8[0]] == 0 && h->mb.cache.ref[0][x264_scan8[4]] == 0 &&
491 h->mb.cache.ref[0][x264_scan8[8]] == 0 && h->mb.cache.ref[0][x264_scan8[12]] == 0 )
502 for( i = 0; i < 4; i++ )
504 bs_write_ue( s, sub_mb_type_p_to_golomb[ h->mb.i_sub_partition[i] ] );
507 if( h->sh.i_num_ref_idx_l0_active > 1 && b_sub_ref0 )
509 bs_write_te( s, h->sh.i_num_ref_idx_l0_active - 1, h->mb.cache.ref[0][x264_scan8[0]] );
510 bs_write_te( s, h->sh.i_num_ref_idx_l0_active - 1, h->mb.cache.ref[0][x264_scan8[4]] );
511 bs_write_te( s, h->sh.i_num_ref_idx_l0_active - 1, h->mb.cache.ref[0][x264_scan8[8]] );
512 bs_write_te( s, h->sh.i_num_ref_idx_l0_active - 1, h->mb.cache.ref[0][x264_scan8[12]] );
515 x264_sub_mb_mv_write_cavlc( h, s, 0 );
517 else if( i_mb_type == B_8x8 )
519 bs_write_ue( s, 22 );
522 for( i = 0; i < 4; i++ )
524 bs_write_ue( s, sub_mb_type_b_to_golomb[ h->mb.i_sub_partition[i] ] );
527 for( i = 0; i < 4; i++ )
529 if( x264_mb_partition_listX_table[0][ h->mb.i_sub_partition[i] ] )
531 bs_write_te( s, h->sh.i_num_ref_idx_l0_active - 1, h->mb.cache.ref[0][x264_scan8[i*4]] );
534 for( i = 0; i < 4; i++ )
536 if( x264_mb_partition_listX_table[1][ h->mb.i_sub_partition[i] ] )
538 bs_write_te( s, h->sh.i_num_ref_idx_l1_active - 1, h->mb.cache.ref[1][x264_scan8[i*4]] );
542 x264_sub_mb_mv_write_cavlc( h, s, 0 );
543 x264_sub_mb_mv_write_cavlc( h, s, 1 );
545 else if( i_mb_type != B_DIRECT )
554 /* init ref list utilisations */
555 for( i = 0; i < 2; i++ )
557 b_list[0][i] = x264_mb_type_list0_table[i_mb_type][i];
558 b_list[1][i] = x264_mb_type_list1_table[i_mb_type][i];
562 bs_write_ue( s, mb_type_b_to_golomb[ h->mb.i_partition - D_16x8 ][ i_mb_type - B_L0_L0 ] );
564 for( i_list = 0; i_list < 2; i_list++ )
566 const int i_ref_max = i_list == 0 ? h->sh.i_num_ref_idx_l0_active : h->sh.i_num_ref_idx_l1_active;
570 switch( h->mb.i_partition )
573 if( b_list[i_list][0] ) bs_write_te( s, i_ref_max - 1, h->mb.cache.ref[i_list][x264_scan8[0]] );
576 if( b_list[i_list][0] ) bs_write_te( s, i_ref_max - 1, h->mb.cache.ref[i_list][x264_scan8[0]] );
577 if( b_list[i_list][1] ) bs_write_te( s, i_ref_max - 1, h->mb.cache.ref[i_list][x264_scan8[8]] );
580 if( b_list[i_list][0] ) bs_write_te( s, i_ref_max - 1, h->mb.cache.ref[i_list][x264_scan8[0]] );
581 if( b_list[i_list][1] ) bs_write_te( s, i_ref_max - 1, h->mb.cache.ref[i_list][x264_scan8[4]] );
586 for( i_list = 0; i_list < 2; i_list++ )
588 switch( h->mb.i_partition )
591 if( b_list[i_list][0] )
593 x264_mb_predict_mv( h, i_list, 0, 4, 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] );
599 if( b_list[i_list][0] )
601 x264_mb_predict_mv( h, i_list, 0, 4, mvp );
602 bs_write_se( s, h->mb.cache.mv[i_list][x264_scan8[0]][0] - mvp[0] );
603 bs_write_se( s, h->mb.cache.mv[i_list][x264_scan8[0]][1] - mvp[1] );
605 if( b_list[i_list][1] )
607 x264_mb_predict_mv( h, i_list, 8, 4, mvp );
608 bs_write_se( s, h->mb.cache.mv[i_list][x264_scan8[8]][0] - mvp[0] );
609 bs_write_se( s, h->mb.cache.mv[i_list][x264_scan8[8]][1] - mvp[1] );
613 if( b_list[i_list][0] )
615 x264_mb_predict_mv( h, i_list, 0, 2, mvp );
616 bs_write_se( s, h->mb.cache.mv[i_list][x264_scan8[0]][0] - mvp[0] );
617 bs_write_se( s, h->mb.cache.mv[i_list][x264_scan8[0]][1] - mvp[1] );
619 if( b_list[i_list][1] )
621 x264_mb_predict_mv( h, i_list, 4, 2, mvp );
622 bs_write_se( s, h->mb.cache.mv[i_list][x264_scan8[4]][0] - mvp[0] );
623 bs_write_se( s, h->mb.cache.mv[i_list][x264_scan8[4]][1] - mvp[1] );
629 else if( i_mb_type == B_DIRECT )
635 fprintf( stderr, "invalid/unhandled mb_type\n" );
639 i_mb_pos_tex = bs_pos( s );
640 h->stat.frame.i_hdr_bits += i_mb_pos_tex - i_mb_pos_start;
642 /* Coded block patern */
643 if( i_mb_type == I_4x4 )
645 bs_write_ue( s, intra4x4_cbp_to_golomb[( h->mb.i_cbp_chroma << 4 )|h->mb.i_cbp_luma] );
647 else if( i_mb_type != I_16x16 )
649 bs_write_ue( s, inter_cbp_to_golomb[( h->mb.i_cbp_chroma << 4 )|h->mb.i_cbp_luma] );
653 if( i_mb_type == I_16x16 )
655 bs_write_se( s, h->mb.qp[h->mb.i_mb_xy] - h->mb.i_last_qp );
658 block_residual_write_cavlc( h, s, BLOCK_INDEX_LUMA_DC , h->dct.luma16x16_dc, 16 );
660 if( h->mb.i_cbp_luma != 0 )
663 for( i = 0; i < 16; i++ )
665 block_residual_write_cavlc( h, s, i, h->dct.block[i].residual_ac, 15 );
669 else if( h->mb.i_cbp_luma != 0 || h->mb.i_cbp_chroma != 0 )
671 bs_write_se( s, h->mb.qp[h->mb.i_mb_xy] - h->mb.i_last_qp );
673 for( i = 0; i < 16; i++ )
675 if( h->mb.i_cbp_luma & ( 1 << ( i / 4 ) ) )
677 block_residual_write_cavlc( h, s, i, h->dct.block[i].luma4x4, 16 );
681 if( h->mb.i_cbp_chroma != 0 )
683 /* Chroma DC residual present */
684 block_residual_write_cavlc( h, s, BLOCK_INDEX_CHROMA_DC, h->dct.chroma_dc[0], 4 );
685 block_residual_write_cavlc( h, s, BLOCK_INDEX_CHROMA_DC, h->dct.chroma_dc[1], 4 );
686 if( h->mb.i_cbp_chroma&0x02 ) /* Chroma AC residual present */
688 for( i = 0; i < 8; i++ )
690 block_residual_write_cavlc( h, s, 16 + i, h->dct.block[16+i].residual_ac, 15 );
695 if( IS_INTRA( i_mb_type ) )
696 h->stat.frame.i_itex_bits += bs_pos(s) - i_mb_pos_tex;
698 h->stat.frame.i_ptex_bits += bs_pos(s) - i_mb_pos_tex;