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 x264_log(h, X264_LOG_ERROR, "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 void x264_macroblock_luma_write_cavlc( x264_t *h, bs_t *s )
343 if( h->mb.b_transform_8x8 )
345 /* shuffle 8x8 dct coeffs into 4x4 lists */
346 for( i8 = 0; i8 < 4; i8++ )
347 if( h->mb.i_cbp_luma & (1 << i8) )
348 for( i4 = 0; i4 < 4; i4++ )
350 for( i = 0; i < 16; i++ )
351 h->dct.block[i4+i8*4].luma4x4[i] = h->dct.luma8x8[i8][i4+i*4];
352 h->mb.cache.non_zero_count[x264_scan8[i4+i8*4]] =
353 array_non_zero_count( h->dct.block[i4+i8*4].luma4x4, 16 );
357 for( i8 = 0; i8 < 4; i8++ )
358 if( h->mb.i_cbp_luma & (1 << i8) )
359 for( i4 = 0; i4 < 4; i4++ )
360 block_residual_write_cavlc( h, s, i4+i8*4, h->dct.block[i4+i8*4].luma4x4, 16 );
363 /*****************************************************************************
364 * x264_macroblock_write:
365 *****************************************************************************/
366 void x264_macroblock_write_cavlc( x264_t *h, bs_t *s )
368 const int i_mb_type = h->mb.i_type;
369 const int i_mb_pos_start = bs_pos( s );
374 switch( h->sh.i_type )
386 x264_log(h, X264_LOG_ERROR, "internal error or slice unsupported\n" );
394 if( i_mb_type == I_PCM )
397 bs_write_ue( s, i_mb_i_offset + 25 );
401 for( i = 0; i < 16*16; i++ )
403 const int x = 16 * h->mb.i_mb_x + (i % 16);
404 const int y = 16 * h->mb.i_mb_y + (i / 16);
405 bs_write( s, 8, h->fenc->plane[0][y*h->mb.pic.i_stride[0]+x] );
408 for( i = 0; i < 8*8; i++ )
410 const int x = 8 * h->mb.i_mb_x + (i % 8);
411 const int y = 8 * h->mb.i_mb_y + (i / 8);
412 bs_write( s, 8, h->fenc->plane[1][y*h->mb.pic.i_stride[1]+x] );
415 for( i = 0; i < 8*8; i++ )
417 const int x = 8 * h->mb.i_mb_x + (i % 8);
418 const int y = 8 * h->mb.i_mb_y + (i / 8);
419 bs_write( s, 8, h->fenc->plane[2][y*h->mb.pic.i_stride[2]+x] );
423 else if( i_mb_type == I_4x4 || i_mb_type == I_8x8 )
425 int di = i_mb_type == I_8x8 ? 4 : 1;
426 bs_write_ue( s, i_mb_i_offset + 0 );
427 if( h->pps->b_transform_8x8_mode )
428 bs_write1( s, h->mb.b_transform_8x8 );
430 /* Prediction: Luma */
431 for( i = 0; i < 16; i += di )
433 int i_pred = x264_mb_predict_intra4x4_mode( h, i );
434 int i_mode = h->mb.cache.intra4x4_pred_mode[x264_scan8[i]];
436 if( i_pred == i_mode)
438 bs_write1( s, 1 ); /* b_prev_intra4x4_pred_mode */
442 bs_write1( s, 0 ); /* b_prev_intra4x4_pred_mode */
443 if( i_mode < i_pred )
445 bs_write( s, 3, i_mode );
449 bs_write( s, 3, i_mode - 1 );
453 bs_write_ue( s, h->mb.i_chroma_pred_mode );
455 else if( i_mb_type == I_16x16 )
457 bs_write_ue( s, i_mb_i_offset + 1 + h->mb.i_intra16x16_pred_mode +
458 h->mb.i_cbp_chroma * 4 + ( h->mb.i_cbp_luma == 0 ? 0 : 12 ) );
459 bs_write_ue( s, h->mb.i_chroma_pred_mode );
461 else if( i_mb_type == P_L0 )
465 if( h->mb.i_partition == D_16x16 )
469 if( h->sh.i_num_ref_idx_l0_active > 1 )
471 bs_write_te( s, h->sh.i_num_ref_idx_l0_active - 1, h->mb.cache.ref[0][x264_scan8[0]] );
473 x264_mb_predict_mv( h, 0, 0, 4, mvp );
474 bs_write_se( s, h->mb.cache.mv[0][x264_scan8[0]][0] - mvp[0] );
475 bs_write_se( s, h->mb.cache.mv[0][x264_scan8[0]][1] - mvp[1] );
477 else if( h->mb.i_partition == D_16x8 )
480 if( h->sh.i_num_ref_idx_l0_active > 1 )
482 bs_write_te( s, h->sh.i_num_ref_idx_l0_active - 1, h->mb.cache.ref[0][x264_scan8[0]] );
483 bs_write_te( s, h->sh.i_num_ref_idx_l0_active - 1, h->mb.cache.ref[0][x264_scan8[8]] );
486 x264_mb_predict_mv( h, 0, 0, 4, mvp );
487 bs_write_se( s, h->mb.cache.mv[0][x264_scan8[0]][0] - mvp[0] );
488 bs_write_se( s, h->mb.cache.mv[0][x264_scan8[0]][1] - mvp[1] );
490 x264_mb_predict_mv( h, 0, 8, 4, mvp );
491 bs_write_se( s, h->mb.cache.mv[0][x264_scan8[8]][0] - mvp[0] );
492 bs_write_se( s, h->mb.cache.mv[0][x264_scan8[8]][1] - mvp[1] );
494 else if( h->mb.i_partition == D_8x16 )
497 if( h->sh.i_num_ref_idx_l0_active > 1 )
499 bs_write_te( s, h->sh.i_num_ref_idx_l0_active - 1, h->mb.cache.ref[0][x264_scan8[0]] );
500 bs_write_te( s, h->sh.i_num_ref_idx_l0_active - 1, h->mb.cache.ref[0][x264_scan8[4]] );
503 x264_mb_predict_mv( h, 0, 0, 2, mvp );
504 bs_write_se( s, h->mb.cache.mv[0][x264_scan8[0]][0] - mvp[0] );
505 bs_write_se( s, h->mb.cache.mv[0][x264_scan8[0]][1] - mvp[1] );
507 x264_mb_predict_mv( h, 0, 4, 2, mvp );
508 bs_write_se( s, h->mb.cache.mv[0][x264_scan8[4]][0] - mvp[0] );
509 bs_write_se( s, h->mb.cache.mv[0][x264_scan8[4]][1] - mvp[1] );
512 else if( i_mb_type == P_8x8 )
516 if( h->mb.cache.ref[0][x264_scan8[0]] == 0 && h->mb.cache.ref[0][x264_scan8[4]] == 0 &&
517 h->mb.cache.ref[0][x264_scan8[8]] == 0 && h->mb.cache.ref[0][x264_scan8[12]] == 0 )
528 for( i = 0; i < 4; i++ )
530 bs_write_ue( s, sub_mb_type_p_to_golomb[ h->mb.i_sub_partition[i] ] );
533 if( h->sh.i_num_ref_idx_l0_active > 1 && b_sub_ref0 )
535 bs_write_te( s, h->sh.i_num_ref_idx_l0_active - 1, h->mb.cache.ref[0][x264_scan8[0]] );
536 bs_write_te( s, h->sh.i_num_ref_idx_l0_active - 1, h->mb.cache.ref[0][x264_scan8[4]] );
537 bs_write_te( s, h->sh.i_num_ref_idx_l0_active - 1, h->mb.cache.ref[0][x264_scan8[8]] );
538 bs_write_te( s, h->sh.i_num_ref_idx_l0_active - 1, h->mb.cache.ref[0][x264_scan8[12]] );
541 x264_sub_mb_mv_write_cavlc( h, s, 0 );
543 else if( i_mb_type == B_8x8 )
545 bs_write_ue( s, 22 );
548 for( i = 0; i < 4; i++ )
550 bs_write_ue( s, sub_mb_type_b_to_golomb[ h->mb.i_sub_partition[i] ] );
553 for( i = 0; i < 4; i++ )
555 if( x264_mb_partition_listX_table[0][ h->mb.i_sub_partition[i] ] )
557 bs_write_te( s, h->sh.i_num_ref_idx_l0_active - 1, h->mb.cache.ref[0][x264_scan8[i*4]] );
560 for( i = 0; i < 4; i++ )
562 if( x264_mb_partition_listX_table[1][ h->mb.i_sub_partition[i] ] )
564 bs_write_te( s, h->sh.i_num_ref_idx_l1_active - 1, h->mb.cache.ref[1][x264_scan8[i*4]] );
568 x264_sub_mb_mv_write_cavlc( h, s, 0 );
569 x264_sub_mb_mv_write_cavlc( h, s, 1 );
571 else if( i_mb_type != B_DIRECT )
580 /* init ref list utilisations */
581 for( i = 0; i < 2; i++ )
583 b_list[0][i] = x264_mb_type_list0_table[i_mb_type][i];
584 b_list[1][i] = x264_mb_type_list1_table[i_mb_type][i];
588 bs_write_ue( s, mb_type_b_to_golomb[ h->mb.i_partition - D_16x8 ][ i_mb_type - B_L0_L0 ] );
590 for( i_list = 0; i_list < 2; i_list++ )
592 const int i_ref_max = i_list == 0 ? h->sh.i_num_ref_idx_l0_active : h->sh.i_num_ref_idx_l1_active;
596 switch( h->mb.i_partition )
599 if( b_list[i_list][0] ) bs_write_te( s, i_ref_max - 1, h->mb.cache.ref[i_list][x264_scan8[0]] );
602 if( b_list[i_list][0] ) bs_write_te( s, i_ref_max - 1, h->mb.cache.ref[i_list][x264_scan8[0]] );
603 if( b_list[i_list][1] ) bs_write_te( s, i_ref_max - 1, h->mb.cache.ref[i_list][x264_scan8[8]] );
606 if( b_list[i_list][0] ) bs_write_te( s, i_ref_max - 1, h->mb.cache.ref[i_list][x264_scan8[0]] );
607 if( b_list[i_list][1] ) bs_write_te( s, i_ref_max - 1, h->mb.cache.ref[i_list][x264_scan8[4]] );
612 for( i_list = 0; i_list < 2; i_list++ )
614 switch( h->mb.i_partition )
617 if( b_list[i_list][0] )
619 x264_mb_predict_mv( h, i_list, 0, 4, mvp );
620 bs_write_se( s, h->mb.cache.mv[i_list][x264_scan8[0]][0] - mvp[0] );
621 bs_write_se( s, h->mb.cache.mv[i_list][x264_scan8[0]][1] - mvp[1] );
625 if( b_list[i_list][0] )
627 x264_mb_predict_mv( h, i_list, 0, 4, mvp );
628 bs_write_se( s, h->mb.cache.mv[i_list][x264_scan8[0]][0] - mvp[0] );
629 bs_write_se( s, h->mb.cache.mv[i_list][x264_scan8[0]][1] - mvp[1] );
631 if( b_list[i_list][1] )
633 x264_mb_predict_mv( h, i_list, 8, 4, mvp );
634 bs_write_se( s, h->mb.cache.mv[i_list][x264_scan8[8]][0] - mvp[0] );
635 bs_write_se( s, h->mb.cache.mv[i_list][x264_scan8[8]][1] - mvp[1] );
639 if( b_list[i_list][0] )
641 x264_mb_predict_mv( h, i_list, 0, 2, mvp );
642 bs_write_se( s, h->mb.cache.mv[i_list][x264_scan8[0]][0] - mvp[0] );
643 bs_write_se( s, h->mb.cache.mv[i_list][x264_scan8[0]][1] - mvp[1] );
645 if( b_list[i_list][1] )
647 x264_mb_predict_mv( h, i_list, 4, 2, mvp );
648 bs_write_se( s, h->mb.cache.mv[i_list][x264_scan8[4]][0] - mvp[0] );
649 bs_write_se( s, h->mb.cache.mv[i_list][x264_scan8[4]][1] - mvp[1] );
655 else if( i_mb_type == B_DIRECT )
661 x264_log(h, X264_LOG_ERROR, "invalid/unhandled mb_type\n" );
665 i_mb_pos_tex = bs_pos( s );
666 h->stat.frame.i_hdr_bits += i_mb_pos_tex - i_mb_pos_start;
668 /* Coded block patern */
669 if( i_mb_type == I_4x4 || i_mb_type == I_8x8 )
671 bs_write_ue( s, intra4x4_cbp_to_golomb[( h->mb.i_cbp_chroma << 4 )|h->mb.i_cbp_luma] );
673 else if( i_mb_type != I_16x16 )
675 bs_write_ue( s, inter_cbp_to_golomb[( h->mb.i_cbp_chroma << 4 )|h->mb.i_cbp_luma] );
678 /* transform size 8x8 flag */
679 if( h->mb.cache.b_transform_8x8_allowed && h->mb.i_cbp_luma && !IS_INTRA(i_mb_type) )
681 bs_write1( s, h->mb.b_transform_8x8 );
685 if( i_mb_type == I_16x16 )
687 bs_write_se( s, h->mb.qp[h->mb.i_mb_xy] - h->mb.i_last_qp );
690 block_residual_write_cavlc( h, s, BLOCK_INDEX_LUMA_DC , h->dct.luma16x16_dc, 16 );
693 if( h->mb.i_cbp_luma != 0 )
694 for( i = 0; i < 16; i++ )
695 block_residual_write_cavlc( h, s, i, h->dct.block[i].residual_ac, 15 );
697 else if( h->mb.i_cbp_luma != 0 || h->mb.i_cbp_chroma != 0 )
699 bs_write_se( s, h->mb.qp[h->mb.i_mb_xy] - h->mb.i_last_qp );
700 x264_macroblock_luma_write_cavlc( h, s );
702 if( h->mb.i_cbp_chroma != 0 )
704 /* Chroma DC residual present */
705 block_residual_write_cavlc( h, s, BLOCK_INDEX_CHROMA_DC, h->dct.chroma_dc[0], 4 );
706 block_residual_write_cavlc( h, s, BLOCK_INDEX_CHROMA_DC, h->dct.chroma_dc[1], 4 );
707 if( h->mb.i_cbp_chroma&0x02 ) /* Chroma AC residual present */
708 for( i = 0; i < 8; i++ )
709 block_residual_write_cavlc( h, s, 16 + i, h->dct.block[16+i].residual_ac, 15 );
712 if( IS_INTRA( i_mb_type ) )
713 h->stat.frame.i_itex_bits += bs_pos(s) - i_mb_pos_tex;
715 h->stat.frame.i_ptex_bits += bs_pos(s) - i_mb_pos_tex;