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 #define BLOCK_INDEX_CHROMA_DC (-1)
60 #define BLOCK_INDEX_LUMA_DC (-2)
62 static inline void bs_write_vlc( bs_t *s, vlc_t v )
64 bs_write( s, v.i_size, v.i_bits );
67 /****************************************************************************
68 * block_residual_write_cavlc:
69 ****************************************************************************/
70 static void block_residual_write_cavlc( x264_t *h, bs_t *s, int i_idx, int *l, int i_count )
72 int level[16], run[16];
73 int i_total, i_trailing;
82 /* first find i_last */
84 while( i_last >= 0 && l[i_last] == 0 )
99 /* level and run and total */
102 level[idx] = l[i_last--];
105 while( i_last >= 0 && l[i_last] == 0 )
112 i_total_zero += run[idx];
114 if( b_trailing && abs( level[idx] ) == 1 && i_trailing < 3 )
134 if( i_idx == BLOCK_INDEX_CHROMA_DC )
136 bs_write_vlc( s, x264_coeff_token[4][i_total*4+i_trailing] );
140 /* x264_mb_predict_non_zero_code return 0 <-> (16+16+1)>>1 = 16 */
141 static const int ct_index[17] = {0,0,1,1,2,2,2,2,3,3,3,3,3,3,3,3,3 };
144 if( i_idx == BLOCK_INDEX_LUMA_DC )
146 nC = x264_mb_predict_non_zero_code( h, 0 );
150 nC = x264_mb_predict_non_zero_code( h, i_idx );
153 bs_write_vlc( s, x264_coeff_token[ct_index[nC]][i_total*4+i_trailing] );
161 i_suffix_length = i_total > 10 && i_trailing < 3 ? 1 : 0;
164 bs_write( s, i_trailing, i_sign );
166 for( i = i_trailing; i < i_total; i++ )
170 /* calculate level code */
173 i_level_code = -2*level[i] - 1;
175 else /* if( level[i] > 0 ) */
177 i_level_code = 2 * level[i] - 2;
179 if( i == i_trailing && i_trailing < 3 )
181 i_level_code -=2; /* as level[i] can't be 1 for the first one if i_trailing < 3 */
184 if( ( i_level_code >> i_suffix_length ) < 14 )
186 bs_write_vlc( s, x264_level_prefix[i_level_code >> i_suffix_length] );
187 if( i_suffix_length > 0 )
189 bs_write( s, i_suffix_length, i_level_code );
192 else if( i_suffix_length == 0 && i_level_code < 30 )
194 bs_write_vlc( s, x264_level_prefix[14] );
195 bs_write( s, 4, i_level_code - 14 );
197 else if( i_suffix_length > 0 && ( i_level_code >> i_suffix_length ) == 14 )
199 bs_write_vlc( s, x264_level_prefix[14] );
200 bs_write( s, i_suffix_length, i_level_code );
204 bs_write_vlc( s, x264_level_prefix[15] );
205 i_level_code -= 15 << i_suffix_length;
206 if( i_suffix_length == 0 )
211 if( i_level_code >= ( 1 << 12 ) || i_level_code < 0 )
213 x264_log(h, X264_LOG_ERROR, "OVERFLOW levelcode=%d\n", i_level_code );
216 bs_write( s, 12, i_level_code ); /* check overflow ?? */
219 if( i_suffix_length == 0 )
223 if( abs( level[i] ) > ( 3 << ( i_suffix_length - 1 ) ) && i_suffix_length < 6 )
229 if( i_total < i_count )
231 if( i_idx == BLOCK_INDEX_CHROMA_DC )
233 bs_write_vlc( s, x264_total_zeros_dc[i_total-1][i_total_zero] );
237 bs_write_vlc( s, x264_total_zeros[i_total-1][i_total_zero] );
241 for( i = 0, i_zero_left = i_total_zero; i < i_total - 1; i++ )
245 if( i_zero_left <= 0 )
250 i_zl = X264_MIN( i_zero_left - 1, 6 );
252 bs_write_vlc( s, x264_run_before[i_zl][run[i]] );
254 i_zero_left -= run[i];
258 static void x264_sub_mb_mv_write_cavlc( x264_t *h, bs_t *s, int i_list )
261 for( i = 0; i < 4; i++ )
265 if( !x264_mb_partition_listX_table[i_list][ h->mb.i_sub_partition[i] ] )
270 switch( h->mb.i_sub_partition[i] )
275 x264_mb_predict_mv( h, i_list, 4*i, 2, mvp );
276 bs_write_se( s, h->mb.cache.mv[i_list][x264_scan8[4*i]][0] - mvp[0] );
277 bs_write_se( s, h->mb.cache.mv[i_list][x264_scan8[4*i]][1] - mvp[1] );
282 x264_mb_predict_mv( h, i_list, 4*i+0, 2, mvp );
283 bs_write_se( s, h->mb.cache.mv[i_list][x264_scan8[4*i]][0] - mvp[0] );
284 bs_write_se( s, h->mb.cache.mv[i_list][x264_scan8[4*i]][1] - mvp[1] );
286 x264_mb_predict_mv( h, i_list, 4*i+2, 2, mvp );
287 bs_write_se( s, h->mb.cache.mv[i_list][x264_scan8[4*i+2]][0] - mvp[0] );
288 bs_write_se( s, h->mb.cache.mv[i_list][x264_scan8[4*i+2]][1] - mvp[1] );
293 x264_mb_predict_mv( h, i_list, 4*i+0, 1, mvp );
294 bs_write_se( s, h->mb.cache.mv[i_list][x264_scan8[4*i]][0] - mvp[0] );
295 bs_write_se( s, h->mb.cache.mv[i_list][x264_scan8[4*i]][1] - mvp[1] );
297 x264_mb_predict_mv( h, i_list, 4*i+1, 1, mvp );
298 bs_write_se( s, h->mb.cache.mv[i_list][x264_scan8[4*i+1]][0] - mvp[0] );
299 bs_write_se( s, h->mb.cache.mv[i_list][x264_scan8[4*i+1]][1] - mvp[1] );
304 x264_mb_predict_mv( h, i_list, 4*i+0, 1, mvp );
305 bs_write_se( s, h->mb.cache.mv[i_list][x264_scan8[4*i]][0] - mvp[0] );
306 bs_write_se( s, h->mb.cache.mv[i_list][x264_scan8[4*i]][1] - mvp[1] );
308 x264_mb_predict_mv( h, i_list, 4*i+1, 1, mvp );
309 bs_write_se( s, h->mb.cache.mv[i_list][x264_scan8[4*i+1]][0] - mvp[0] );
310 bs_write_se( s, h->mb.cache.mv[i_list][x264_scan8[4*i+1]][1] - mvp[1] );
312 x264_mb_predict_mv( h, i_list, 4*i+2, 1, mvp );
313 bs_write_se( s, h->mb.cache.mv[i_list][x264_scan8[4*i+2]][0] - mvp[0] );
314 bs_write_se( s, h->mb.cache.mv[i_list][x264_scan8[4*i+2]][1] - mvp[1] );
316 x264_mb_predict_mv( h, i_list, 4*i+3, 1, mvp );
317 bs_write_se( s, h->mb.cache.mv[i_list][x264_scan8[4*i+3]][0] - mvp[0] );
318 bs_write_se( s, h->mb.cache.mv[i_list][x264_scan8[4*i+3]][1] - mvp[1] );
324 void x264_macroblock_luma_write_cavlc( x264_t *h, bs_t *s )
327 if( h->mb.b_transform_8x8 )
329 /* shuffle 8x8 dct coeffs into 4x4 lists */
330 for( i8 = 0; i8 < 4; i8++ )
331 if( h->mb.i_cbp_luma & (1 << i8) )
332 for( i4 = 0; i4 < 4; i4++ )
334 for( i = 0; i < 16; i++ )
335 h->dct.block[i4+i8*4].luma4x4[i] = h->dct.luma8x8[i8][i4+i*4];
336 h->mb.cache.non_zero_count[x264_scan8[i4+i8*4]] =
337 array_non_zero_count( h->dct.block[i4+i8*4].luma4x4, 16 );
341 for( i8 = 0; i8 < 4; i8++ )
342 if( h->mb.i_cbp_luma & (1 << i8) )
343 for( i4 = 0; i4 < 4; i4++ )
344 block_residual_write_cavlc( h, s, i4+i8*4, h->dct.block[i4+i8*4].luma4x4, 16 );
347 /*****************************************************************************
348 * x264_macroblock_write:
349 *****************************************************************************/
350 void x264_macroblock_write_cavlc( x264_t *h, bs_t *s )
352 const int i_mb_type = h->mb.i_type;
353 const int i_mb_pos_start = bs_pos( s );
358 switch( h->sh.i_type )
370 x264_log(h, X264_LOG_ERROR, "internal error or slice unsupported\n" );
378 if( i_mb_type == I_PCM )
381 bs_write_ue( s, i_mb_i_offset + 25 );
385 for( i = 0; i < 16*16; i++ )
387 const int x = 16 * h->mb.i_mb_x + (i % 16);
388 const int y = 16 * h->mb.i_mb_y + (i / 16);
389 bs_write( s, 8, h->fenc->plane[0][y*h->mb.pic.i_stride[0]+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[1][y*h->mb.pic.i_stride[1]+x] );
399 for( i = 0; i < 8*8; i++ )
401 const int x = 8 * h->mb.i_mb_x + (i % 8);
402 const int y = 8 * h->mb.i_mb_y + (i / 8);
403 bs_write( s, 8, h->fenc->plane[2][y*h->mb.pic.i_stride[2]+x] );
407 else if( i_mb_type == I_4x4 || i_mb_type == I_8x8 )
409 int di = i_mb_type == I_8x8 ? 4 : 1;
410 bs_write_ue( s, i_mb_i_offset + 0 );
411 if( h->pps->b_transform_8x8_mode )
412 bs_write1( s, h->mb.b_transform_8x8 );
414 /* Prediction: Luma */
415 for( i = 0; i < 16; i += di )
417 int i_pred = x264_mb_predict_intra4x4_mode( h, i );
418 int i_mode = x264_mb_pred_mode4x4_fix( h->mb.cache.intra4x4_pred_mode[x264_scan8[i]] );
420 if( i_pred == i_mode)
422 bs_write1( s, 1 ); /* b_prev_intra4x4_pred_mode */
426 bs_write1( s, 0 ); /* b_prev_intra4x4_pred_mode */
427 if( i_mode < i_pred )
429 bs_write( s, 3, i_mode );
433 bs_write( s, 3, i_mode - 1 );
437 bs_write_ue( s, x264_mb_pred_mode8x8c_fix[ h->mb.i_chroma_pred_mode ] );
439 else if( i_mb_type == I_16x16 )
441 bs_write_ue( s, i_mb_i_offset + 1 + x264_mb_pred_mode16x16_fix[h->mb.i_intra16x16_pred_mode] +
442 h->mb.i_cbp_chroma * 4 + ( h->mb.i_cbp_luma == 0 ? 0 : 12 ) );
443 bs_write_ue( s, x264_mb_pred_mode8x8c_fix[ h->mb.i_chroma_pred_mode ] );
445 else if( i_mb_type == P_L0 )
449 if( h->mb.i_partition == D_16x16 )
453 if( h->sh.i_num_ref_idx_l0_active > 1 )
455 bs_write_te( s, h->sh.i_num_ref_idx_l0_active - 1, h->mb.cache.ref[0][x264_scan8[0]] );
457 x264_mb_predict_mv( h, 0, 0, 4, mvp );
458 bs_write_se( s, h->mb.cache.mv[0][x264_scan8[0]][0] - mvp[0] );
459 bs_write_se( s, h->mb.cache.mv[0][x264_scan8[0]][1] - mvp[1] );
461 else if( h->mb.i_partition == D_16x8 )
464 if( h->sh.i_num_ref_idx_l0_active > 1 )
466 bs_write_te( s, h->sh.i_num_ref_idx_l0_active - 1, h->mb.cache.ref[0][x264_scan8[0]] );
467 bs_write_te( s, h->sh.i_num_ref_idx_l0_active - 1, h->mb.cache.ref[0][x264_scan8[8]] );
470 x264_mb_predict_mv( h, 0, 0, 4, mvp );
471 bs_write_se( s, h->mb.cache.mv[0][x264_scan8[0]][0] - mvp[0] );
472 bs_write_se( s, h->mb.cache.mv[0][x264_scan8[0]][1] - mvp[1] );
474 x264_mb_predict_mv( h, 0, 8, 4, mvp );
475 bs_write_se( s, h->mb.cache.mv[0][x264_scan8[8]][0] - mvp[0] );
476 bs_write_se( s, h->mb.cache.mv[0][x264_scan8[8]][1] - mvp[1] );
478 else if( h->mb.i_partition == D_8x16 )
481 if( h->sh.i_num_ref_idx_l0_active > 1 )
483 bs_write_te( s, h->sh.i_num_ref_idx_l0_active - 1, h->mb.cache.ref[0][x264_scan8[0]] );
484 bs_write_te( s, h->sh.i_num_ref_idx_l0_active - 1, h->mb.cache.ref[0][x264_scan8[4]] );
487 x264_mb_predict_mv( h, 0, 0, 2, mvp );
488 bs_write_se( s, h->mb.cache.mv[0][x264_scan8[0]][0] - mvp[0] );
489 bs_write_se( s, h->mb.cache.mv[0][x264_scan8[0]][1] - mvp[1] );
491 x264_mb_predict_mv( h, 0, 4, 2, mvp );
492 bs_write_se( s, h->mb.cache.mv[0][x264_scan8[4]][0] - mvp[0] );
493 bs_write_se( s, h->mb.cache.mv[0][x264_scan8[4]][1] - mvp[1] );
496 else if( i_mb_type == P_8x8 )
500 if( h->mb.cache.ref[0][x264_scan8[0]] == 0 && h->mb.cache.ref[0][x264_scan8[4]] == 0 &&
501 h->mb.cache.ref[0][x264_scan8[8]] == 0 && h->mb.cache.ref[0][x264_scan8[12]] == 0 )
512 for( i = 0; i < 4; i++ )
514 bs_write_ue( s, sub_mb_type_p_to_golomb[ h->mb.i_sub_partition[i] ] );
517 if( h->sh.i_num_ref_idx_l0_active > 1 && b_sub_ref0 )
519 bs_write_te( s, h->sh.i_num_ref_idx_l0_active - 1, h->mb.cache.ref[0][x264_scan8[0]] );
520 bs_write_te( s, h->sh.i_num_ref_idx_l0_active - 1, h->mb.cache.ref[0][x264_scan8[4]] );
521 bs_write_te( s, h->sh.i_num_ref_idx_l0_active - 1, h->mb.cache.ref[0][x264_scan8[8]] );
522 bs_write_te( s, h->sh.i_num_ref_idx_l0_active - 1, h->mb.cache.ref[0][x264_scan8[12]] );
525 x264_sub_mb_mv_write_cavlc( h, s, 0 );
527 else if( i_mb_type == B_8x8 )
529 bs_write_ue( s, 22 );
532 for( i = 0; i < 4; i++ )
534 bs_write_ue( s, sub_mb_type_b_to_golomb[ h->mb.i_sub_partition[i] ] );
537 for( i = 0; i < 4; i++ )
539 if( x264_mb_partition_listX_table[0][ h->mb.i_sub_partition[i] ] )
541 bs_write_te( s, h->sh.i_num_ref_idx_l0_active - 1, h->mb.cache.ref[0][x264_scan8[i*4]] );
544 for( i = 0; i < 4; i++ )
546 if( x264_mb_partition_listX_table[1][ h->mb.i_sub_partition[i] ] )
548 bs_write_te( s, h->sh.i_num_ref_idx_l1_active - 1, h->mb.cache.ref[1][x264_scan8[i*4]] );
552 x264_sub_mb_mv_write_cavlc( h, s, 0 );
553 x264_sub_mb_mv_write_cavlc( h, s, 1 );
555 else if( i_mb_type != B_DIRECT )
564 /* init ref list utilisations */
565 for( i = 0; i < 2; i++ )
567 b_list[0][i] = x264_mb_type_list0_table[i_mb_type][i];
568 b_list[1][i] = x264_mb_type_list1_table[i_mb_type][i];
572 bs_write_ue( s, mb_type_b_to_golomb[ h->mb.i_partition - D_16x8 ][ i_mb_type - B_L0_L0 ] );
574 for( i_list = 0; i_list < 2; i_list++ )
576 const int i_ref_max = i_list == 0 ? h->sh.i_num_ref_idx_l0_active : h->sh.i_num_ref_idx_l1_active;
580 switch( h->mb.i_partition )
583 if( b_list[i_list][0] ) bs_write_te( s, i_ref_max - 1, h->mb.cache.ref[i_list][x264_scan8[0]] );
586 if( b_list[i_list][0] ) bs_write_te( s, i_ref_max - 1, h->mb.cache.ref[i_list][x264_scan8[0]] );
587 if( b_list[i_list][1] ) bs_write_te( s, i_ref_max - 1, h->mb.cache.ref[i_list][x264_scan8[8]] );
590 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][1] ) bs_write_te( s, i_ref_max - 1, h->mb.cache.ref[i_list][x264_scan8[4]] );
596 for( i_list = 0; i_list < 2; i_list++ )
598 switch( h->mb.i_partition )
601 if( b_list[i_list][0] )
603 x264_mb_predict_mv( h, i_list, 0, 4, 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] );
609 if( b_list[i_list][0] )
611 x264_mb_predict_mv( h, i_list, 0, 4, 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, 8, 4, mvp );
618 bs_write_se( s, h->mb.cache.mv[i_list][x264_scan8[8]][0] - mvp[0] );
619 bs_write_se( s, h->mb.cache.mv[i_list][x264_scan8[8]][1] - mvp[1] );
623 if( b_list[i_list][0] )
625 x264_mb_predict_mv( h, i_list, 0, 2, mvp );
626 bs_write_se( s, h->mb.cache.mv[i_list][x264_scan8[0]][0] - mvp[0] );
627 bs_write_se( s, h->mb.cache.mv[i_list][x264_scan8[0]][1] - mvp[1] );
629 if( b_list[i_list][1] )
631 x264_mb_predict_mv( h, i_list, 4, 2, mvp );
632 bs_write_se( s, h->mb.cache.mv[i_list][x264_scan8[4]][0] - mvp[0] );
633 bs_write_se( s, h->mb.cache.mv[i_list][x264_scan8[4]][1] - mvp[1] );
639 else if( i_mb_type == B_DIRECT )
645 x264_log(h, X264_LOG_ERROR, "invalid/unhandled mb_type\n" );
649 i_mb_pos_tex = bs_pos( s );
650 h->stat.frame.i_hdr_bits += i_mb_pos_tex - i_mb_pos_start;
652 /* Coded block patern */
653 if( i_mb_type == I_4x4 || i_mb_type == I_8x8 )
655 bs_write_ue( s, intra4x4_cbp_to_golomb[( h->mb.i_cbp_chroma << 4 )|h->mb.i_cbp_luma] );
657 else if( i_mb_type != I_16x16 )
659 bs_write_ue( s, inter_cbp_to_golomb[( h->mb.i_cbp_chroma << 4 )|h->mb.i_cbp_luma] );
662 /* transform size 8x8 flag */
663 if( h->mb.cache.b_transform_8x8_allowed && h->mb.i_cbp_luma && !IS_INTRA(i_mb_type) )
665 bs_write1( s, h->mb.b_transform_8x8 );
669 if( i_mb_type == I_16x16 )
671 bs_write_se( s, h->mb.i_qp - h->mb.i_last_qp );
674 block_residual_write_cavlc( h, s, BLOCK_INDEX_LUMA_DC , h->dct.luma16x16_dc, 16 );
677 if( h->mb.i_cbp_luma != 0 )
678 for( i = 0; i < 16; i++ )
679 block_residual_write_cavlc( h, s, i, h->dct.block[i].residual_ac, 15 );
681 else if( h->mb.i_cbp_luma != 0 || h->mb.i_cbp_chroma != 0 )
683 bs_write_se( s, h->mb.i_qp - h->mb.i_last_qp );
684 x264_macroblock_luma_write_cavlc( h, s );
686 if( h->mb.i_cbp_chroma != 0 )
688 /* Chroma DC residual present */
689 block_residual_write_cavlc( h, s, BLOCK_INDEX_CHROMA_DC, h->dct.chroma_dc[0], 4 );
690 block_residual_write_cavlc( h, s, BLOCK_INDEX_CHROMA_DC, h->dct.chroma_dc[1], 4 );
691 if( h->mb.i_cbp_chroma&0x02 ) /* Chroma AC residual present */
692 for( i = 0; i < 8; i++ )
693 block_residual_write_cavlc( h, s, 16 + i, h->dct.block[16+i].residual_ac, 15 );
696 if( IS_INTRA( i_mb_type ) )
697 h->stat.frame.i_itex_bits += bs_pos(s) - i_mb_pos_tex;
699 h->stat.frame.i_ptex_bits += bs_pos(s) - i_mb_pos_tex;