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 "../core/common.h"
29 #include "../core/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
45 static const uint8_t block_idx_x[16] =
47 0, 1, 0, 1, 2, 3, 2, 3, 0, 1, 0, 1, 2, 3, 2, 3
49 static const uint8_t block_idx_y[16] =
51 0, 0, 1, 1, 0, 0, 1, 1, 2, 2, 3, 3, 2, 2, 3, 3
53 static const uint8_t block_idx_xy[4][4] =
61 #define BLOCK_INDEX_CHROMA_DC (-1)
62 #define BLOCK_INDEX_LUMA_DC (-2)
64 static inline void bs_write_vlc( bs_t *s, vlc_t v )
66 bs_write( s, v.i_size, v.i_bits );
69 /****************************************************************************
70 * block_residual_write_cavlc:
71 ****************************************************************************/
72 static void block_residual_write_cavlc( x264_t *h, bs_t *s, int i_idx, int *l, int i_count )
74 int level[16], run[16];
75 int i_total, i_trailing;
84 /* first find i_last */
86 while( i_last >= 0 && l[i_last] == 0 )
101 /* level and run and total */
104 level[idx] = l[i_last--];
107 while( i_last >= 0 && l[i_last] == 0 )
114 i_total_zero += run[idx];
116 if( b_trailing && abs( level[idx] ) == 1 && i_trailing < 3 )
136 if( i_idx == BLOCK_INDEX_CHROMA_DC )
138 bs_write_vlc( s, x264_coeff_token[4][i_total*4+i_trailing] );
142 /* x264_mb_predict_non_zero_code return 0 <-> (16+16+1)>>1 = 16 */
143 static const int ct_index[17] = {0,0,1,1,2,2,2,2,3,3,3,3,3,3,3,3,3 };
146 if( i_idx == BLOCK_INDEX_LUMA_DC )
148 nC = x264_mb_predict_non_zero_code( h, 0 );
152 nC = x264_mb_predict_non_zero_code( h, i_idx );
155 bs_write_vlc( s, x264_coeff_token[ct_index[nC]][i_total*4+i_trailing] );
163 i_suffix_length = i_total > 10 && i_trailing < 3 ? 1 : 0;
166 bs_write( s, i_trailing, i_sign );
168 for( i = i_trailing; i < i_total; i++ )
172 /* calculate level code */
175 i_level_code = -2*level[i] - 1;
177 else /* if( level[i] > 0 ) */
179 i_level_code = 2 * level[i] - 2;
181 if( i == i_trailing && i_trailing < 3 )
183 i_level_code -=2; /* as level[i] can't be 1 for the first one if i_trailing < 3 */
186 if( ( i_level_code >> i_suffix_length ) < 14 )
188 bs_write_vlc( s, x264_level_prefix[i_level_code >> i_suffix_length] );
189 if( i_suffix_length > 0 )
191 bs_write( s, i_suffix_length, i_level_code );
194 else if( i_suffix_length == 0 && i_level_code < 30 )
196 bs_write_vlc( s, x264_level_prefix[14] );
197 bs_write( s, 4, i_level_code - 14 );
199 else if( i_suffix_length > 0 && ( i_level_code >> i_suffix_length ) == 14 )
201 bs_write_vlc( s, x264_level_prefix[14] );
202 bs_write( s, i_suffix_length, i_level_code );
206 bs_write_vlc( s, x264_level_prefix[15] );
207 i_level_code -= 15 << i_suffix_length;
208 if( i_suffix_length == 0 )
213 if( i_level_code >= ( 1 << 12 ) || i_level_code < 0 )
215 fprintf( stderr, "OVERFLOW levelcode=%d\n", i_level_code );
218 bs_write( s, 12, i_level_code ); /* check overflow ?? */
221 if( i_suffix_length == 0 )
225 if( abs( level[i] ) > ( 3 << ( i_suffix_length - 1 ) ) && i_suffix_length < 6 )
231 if( i_total < i_count )
233 if( i_idx == BLOCK_INDEX_CHROMA_DC )
235 bs_write_vlc( s, x264_total_zeros_dc[i_total-1][i_total_zero] );
239 bs_write_vlc( s, x264_total_zeros[i_total-1][i_total_zero] );
243 for( i = 0, i_zero_left = i_total_zero; i < i_total - 1; i++ )
247 if( i_zero_left <= 0 )
252 i_zl = X264_MIN( i_zero_left - 1, 6 );
254 bs_write_vlc( s, x264_run_before[i_zl][run[i]] );
256 i_zero_left -= run[i];
260 /*****************************************************************************
261 * x264_macroblock_write:
262 *****************************************************************************/
263 void x264_macroblock_write_cavlc( x264_t *h, bs_t *s )
265 const int i_mb_type = h->mb.i_type;
269 switch( h->sh.i_type )
281 fprintf( stderr, "internal error or slice unsupported\n" );
289 if( i_mb_type == I_PCM )
292 bs_write_ue( s, i_mb_i_offset + 25 );
296 for( i = 0; i < 16*16; i++ )
298 const int x = 16 * h->mb.i_mb_x + (i % 16);
299 const int y = 16 * h->mb.i_mb_y + (i / 16);
300 bs_write( s, 8, h->fenc->plane[0][y*h->mb.pic.i_stride[0]+x] );
303 for( i = 0; i < 8*8; i++ )
305 const int x = 8 * h->mb.i_mb_x + (i % 8);
306 const int y = 8 * h->mb.i_mb_y + (i / 8);
307 bs_write( s, 8, h->fenc->plane[1][y*h->mb.pic.i_stride[1]+x] );
310 for( i = 0; i < 8*8; i++ )
312 const int x = 8 * h->mb.i_mb_x + (i % 8);
313 const int y = 8 * h->mb.i_mb_y + (i / 8);
314 bs_write( s, 8, h->fenc->plane[2][y*h->mb.pic.i_stride[2]+x] );
318 else if( i_mb_type == I_4x4 )
320 bs_write_ue( s, i_mb_i_offset + 0 );
322 /* Prediction: Luma */
323 for( i = 0; i < 16; i++ )
325 int i_pred = x264_mb_predict_intra4x4_mode( h, i );
326 int i_mode = h->mb.cache.intra4x4_pred_mode[x264_scan8[i]];
328 if( i_pred == i_mode)
330 bs_write1( s, 1 ); /* b_prev_intra4x4_pred_mode */
334 bs_write1( s, 0 ); /* b_prev_intra4x4_pred_mode */
335 if( i_mode < i_pred )
337 bs_write( s, 3, i_mode );
341 bs_write( s, 3, i_mode - 1 );
345 bs_write_ue( s, h->mb.i_chroma_pred_mode );
347 else if( i_mb_type == I_16x16 )
349 bs_write_ue( s, i_mb_i_offset + 1 + h->mb.i_intra16x16_pred_mode +
350 h->mb.i_cbp_chroma * 4 + ( h->mb.i_cbp_luma == 0 ? 0 : 12 ) );
351 bs_write_ue( s, h->mb.i_chroma_pred_mode );
353 else if( i_mb_type == P_L0 )
357 if( h->mb.i_partition == D_16x16 )
361 if( h->sh.i_num_ref_idx_l0_active > 1 )
363 bs_write_te( s, h->sh.i_num_ref_idx_l0_active - 1, h->mb.cache.ref[0][x264_scan8[0]] );
365 x264_mb_predict_mv( h, 0, 0, 4, mvp );
366 bs_write_se( s, h->mb.cache.mv[0][x264_scan8[0]][0] - mvp[0] );
367 bs_write_se( s, h->mb.cache.mv[0][x264_scan8[0]][1] - mvp[1] );
369 else if( h->mb.i_partition == D_16x8 )
372 if( h->sh.i_num_ref_idx_l0_active > 1 )
374 bs_write_te( s, h->sh.i_num_ref_idx_l0_active - 1, h->mb.cache.ref[0][x264_scan8[0]] );
375 bs_write_te( s, h->sh.i_num_ref_idx_l0_active - 1, h->mb.cache.ref[0][x264_scan8[8]] );
378 x264_mb_predict_mv( h, 0, 0, 4, mvp );
379 bs_write_se( s, h->mb.cache.mv[0][x264_scan8[0]][0] - mvp[0] );
380 bs_write_se( s, h->mb.cache.mv[0][x264_scan8[0]][1] - mvp[1] );
382 x264_mb_predict_mv( h, 0, 8, 4, mvp );
383 bs_write_se( s, h->mb.cache.mv[0][x264_scan8[8]][0] - mvp[0] );
384 bs_write_se( s, h->mb.cache.mv[0][x264_scan8[8]][1] - mvp[1] );
386 else if( h->mb.i_partition == D_8x16 )
389 if( h->sh.i_num_ref_idx_l0_active > 1 )
391 bs_write_te( s, h->sh.i_num_ref_idx_l0_active - 1, h->mb.cache.ref[0][x264_scan8[0]] );
392 bs_write_te( s, h->sh.i_num_ref_idx_l0_active - 1, h->mb.cache.ref[0][x264_scan8[4]] );
395 x264_mb_predict_mv( h, 0, 0, 2, mvp );
396 bs_write_se( s, h->mb.cache.mv[0][x264_scan8[0]][0] - mvp[0] );
397 bs_write_se( s, h->mb.cache.mv[0][x264_scan8[0]][1] - mvp[1] );
399 x264_mb_predict_mv( h, 0, 4, 2, mvp );
400 bs_write_se( s, h->mb.cache.mv[0][x264_scan8[4]][0] - mvp[0] );
401 bs_write_se( s, h->mb.cache.mv[0][x264_scan8[4]][1] - mvp[1] );
404 else if( i_mb_type == P_8x8 )
408 if( h->mb.cache.ref[0][x264_scan8[0]] == 0 && h->mb.cache.ref[0][x264_scan8[4]] == 0 &&
409 h->mb.cache.ref[0][x264_scan8[8]] == 0 && h->mb.cache.ref[0][x264_scan8[12]] == 0 )
420 for( i = 0; i < 4; i++ )
422 switch( h->mb.i_sub_partition[i] )
439 if( h->sh.i_num_ref_idx_l0_active > 1 && b_sub_ref0 )
441 bs_write_te( s, h->sh.i_num_ref_idx_l0_active - 1, h->mb.cache.ref[0][x264_scan8[0]] );
442 bs_write_te( s, h->sh.i_num_ref_idx_l0_active - 1, h->mb.cache.ref[0][x264_scan8[4]] );
443 bs_write_te( s, h->sh.i_num_ref_idx_l0_active - 1, h->mb.cache.ref[0][x264_scan8[8]] );
444 bs_write_te( s, h->sh.i_num_ref_idx_l0_active - 1, h->mb.cache.ref[0][x264_scan8[12]] );
446 for( i = 0; i < 4; i++ )
450 switch( h->mb.i_sub_partition[i] )
453 x264_mb_predict_mv( h, 0, 4*i, 2, mvp );
454 bs_write_se( s, h->mb.cache.mv[0][x264_scan8[4*i]][0] - mvp[0] );
455 bs_write_se( s, h->mb.cache.mv[0][x264_scan8[4*i]][1] - mvp[1] );
458 x264_mb_predict_mv( h, 0, 4*i+0, 2, mvp );
459 bs_write_se( s, h->mb.cache.mv[0][x264_scan8[4*i]][0] - mvp[0] );
460 bs_write_se( s, h->mb.cache.mv[0][x264_scan8[4*i]][1] - mvp[1] );
462 x264_mb_predict_mv( h, 0, 4*i+2, 2, mvp );
463 bs_write_se( s, h->mb.cache.mv[0][x264_scan8[4*i+2]][0] - mvp[0] );
464 bs_write_se( s, h->mb.cache.mv[0][x264_scan8[4*i+2]][1] - mvp[1] );
467 x264_mb_predict_mv( h, 0, 4*i+0, 1, mvp );
468 bs_write_se( s, h->mb.cache.mv[0][x264_scan8[4*i]][0] - mvp[0] );
469 bs_write_se( s, h->mb.cache.mv[0][x264_scan8[4*i]][1] - mvp[1] );
471 x264_mb_predict_mv( h, 0, 4*i+1, 1, mvp );
472 bs_write_se( s, h->mb.cache.mv[0][x264_scan8[4*i+1]][0] - mvp[0] );
473 bs_write_se( s, h->mb.cache.mv[0][x264_scan8[4*i+1]][1] - mvp[1] );
476 x264_mb_predict_mv( h, 0, 4*i+0, 1, mvp );
477 bs_write_se( s, h->mb.cache.mv[0][x264_scan8[4*i]][0] - mvp[0] );
478 bs_write_se( s, h->mb.cache.mv[0][x264_scan8[4*i]][1] - mvp[1] );
480 x264_mb_predict_mv( h, 0, 4*i+1, 1, mvp );
481 bs_write_se( s, h->mb.cache.mv[0][x264_scan8[4*i+1]][0] - mvp[0] );
482 bs_write_se( s, h->mb.cache.mv[0][x264_scan8[4*i+1]][1] - mvp[1] );
484 x264_mb_predict_mv( h, 0, 4*i+2, 1, mvp );
485 bs_write_se( s, h->mb.cache.mv[0][x264_scan8[4*i+2]][0] - mvp[0] );
486 bs_write_se( s, h->mb.cache.mv[0][x264_scan8[4*i+2]][1] - mvp[1] );
488 x264_mb_predict_mv( h, 0, 4*i+3, 1, mvp );
489 bs_write_se( s, h->mb.cache.mv[0][x264_scan8[4*i+3]][0] - mvp[0] );
490 bs_write_se( s, h->mb.cache.mv[0][x264_scan8[4*i+3]][1] - mvp[1] );
495 else if( i_mb_type == B_8x8 )
497 fprintf( stderr, "invalid/unhandled mb_type (B_8x8)\n" );
500 else if( i_mb_type != B_DIRECT )
509 /* init ref list utilisations */
510 for( i = 0; i < 2; i++ )
512 b_list[0][i] = x264_mb_type_list0_table[i_mb_type][i];
513 b_list[1][i] = x264_mb_type_list1_table[i_mb_type][i];
517 if( h->mb.i_partition == D_16x16 )
519 if( b_list[0][0] && b_list[1][0] )
523 else if( b_list[1][0] )
534 if( i_mb_type == B_BI_BI )
536 bs_write_ue( s, 20 + (h->mb.i_partition == D_16x8 ? 0 : 1 ) );
538 else if( b_list[0][0] && b_list[1][0] )
541 bs_write_ue( s, 16 + (b_list[0][1]?0:2) + (h->mb.i_partition == D_16x8?0:1) );
543 else if( b_list[0][1] && b_list[1][1] )
546 bs_write_ue( s, 12 + (b_list[0][1]?0:2) + (h->mb.i_partition == D_16x8?0:1) );
548 else if( b_list[1][1] )
551 bs_write_ue( s, 6 + (b_list[0][0]?2:0) + (h->mb.i_partition == D_16x8?0:1) );
553 else if( b_list[0][1] )
556 bs_write_ue( s, 4 + (b_list[0][0]?0:6) + (h->mb.i_partition == D_16x8?0:1) );
560 for( i_list = 0; i_list < 2; i_list++ )
562 const int i_ref_max = i_list == 0 ? h->sh.i_num_ref_idx_l0_active : h->sh.i_num_ref_idx_l1_active;
566 switch( h->mb.i_partition )
569 if( b_list[i_list][0] ) bs_write_te( s, i_ref_max - 1, h->mb.cache.ref[i_list][x264_scan8[0]] );
572 if( b_list[i_list][0] ) bs_write_te( s, i_ref_max - 1, h->mb.cache.ref[i_list][x264_scan8[0]] );
573 if( b_list[i_list][1] ) bs_write_te( s, i_ref_max - 1, h->mb.cache.ref[i_list][x264_scan8[8]] );
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[4]] );
582 for( i_list = 0; i_list < 2; i_list++ )
584 switch( h->mb.i_partition )
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] );
595 if( b_list[i_list][0] )
597 x264_mb_predict_mv( h, i_list, 0, 4, mvp );
598 bs_write_se( s, h->mb.cache.mv[i_list][x264_scan8[0]][0] - mvp[0] );
599 bs_write_se( s, h->mb.cache.mv[i_list][x264_scan8[0]][1] - mvp[1] );
601 if( b_list[i_list][1] )
603 x264_mb_predict_mv( h, i_list, 8, 4, mvp );
604 bs_write_se( s, h->mb.cache.mv[i_list][x264_scan8[8]][0] - mvp[0] );
605 bs_write_se( s, h->mb.cache.mv[i_list][x264_scan8[8]][1] - mvp[1] );
609 if( b_list[i_list][0] )
611 x264_mb_predict_mv( h, i_list, 0, 2, mvp );
612 bs_write_se( s, h->mb.cache.mv[i_list][x264_scan8[0]][0] - mvp[0] );
613 bs_write_se( s, h->mb.cache.mv[i_list][x264_scan8[0]][1] - mvp[1] );
615 if( b_list[i_list][1] )
617 x264_mb_predict_mv( h, i_list, 4, 2, mvp );
618 bs_write_se( s, h->mb.cache.mv[i_list][x264_scan8[4]][0] - mvp[0] );
619 bs_write_se( s, h->mb.cache.mv[i_list][x264_scan8[4]][1] - mvp[1] );
625 else if( i_mb_type == B_DIRECT )
631 fprintf( stderr, "invalid/unhandled mb_type\n" );
635 /* Coded block patern */
636 if( i_mb_type == I_4x4 )
638 bs_write_ue( s, intra4x4_cbp_to_golomb[( h->mb.i_cbp_chroma << 4 )|h->mb.i_cbp_luma] );
640 else if( i_mb_type != I_16x16 )
642 bs_write_ue( s, inter_cbp_to_golomb[( h->mb.i_cbp_chroma << 4 )|h->mb.i_cbp_luma] );
646 if( i_mb_type == I_16x16 )
648 bs_write_se( s, h->mb.qp[h->mb.i_mb_xy] - h->mb.i_last_qp );
651 block_residual_write_cavlc( h, s, BLOCK_INDEX_LUMA_DC , h->dct.luma16x16_dc, 16 );
653 if( h->mb.i_cbp_luma != 0 )
656 for( i = 0; i < 16; i++ )
658 block_residual_write_cavlc( h, s, i, h->dct.block[i].residual_ac, 15 );
662 else if( h->mb.i_cbp_luma != 0 || h->mb.i_cbp_chroma != 0 )
664 bs_write_se( s, h->mb.qp[h->mb.i_mb_xy] - h->mb.i_last_qp );
666 for( i = 0; i < 16; i++ )
668 if( h->mb.i_cbp_luma & ( 1 << ( i / 4 ) ) )
670 block_residual_write_cavlc( h, s, i, h->dct.block[i].luma4x4, 16 );
674 if( h->mb.i_cbp_chroma != 0 )
676 /* Chroma DC residual present */
677 block_residual_write_cavlc( h, s, BLOCK_INDEX_CHROMA_DC, h->dct.chroma_dc[0], 4 );
678 block_residual_write_cavlc( h, s, BLOCK_INDEX_CHROMA_DC, h->dct.chroma_dc[1], 4 );
679 if( h->mb.i_cbp_chroma&0x02 ) /* Chroma AC residual present */
681 for( i = 0; i < 8; i++ )
683 block_residual_write_cavlc( h, s, 16 + i, h->dct.block[16+i].residual_ac, 15 );