1 /*****************************************************************************
2 * cabac.c: h264 encoder library
3 *****************************************************************************
4 * Copyright (C) 2003 Laurent Aimar
5 * $Id: cabac.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 *****************************************************************************/
24 #include "common/common.h"
25 #include "macroblock.h"
27 static inline void x264_cabac_mb_type_intra( x264_t *h, x264_cabac_t *cb, int i_mb_type,
28 int ctx0, int ctx1, int ctx2, int ctx3, int ctx4, int ctx5 )
30 if( i_mb_type == I_4x4 || i_mb_type == I_8x8 )
32 x264_cabac_encode_decision( cb, ctx0, 0 );
34 else if( i_mb_type == I_PCM )
36 x264_cabac_encode_decision( cb, ctx0, 1 );
37 x264_cabac_encode_flush( h, cb );
41 int i_pred = x264_mb_pred_mode16x16_fix[h->mb.i_intra16x16_pred_mode];
43 x264_cabac_encode_decision( cb, ctx0, 1 );
44 x264_cabac_encode_terminal( cb );
46 x264_cabac_encode_decision( cb, ctx1, !!h->mb.i_cbp_luma );
47 if( h->mb.i_cbp_chroma == 0 )
49 x264_cabac_encode_decision( cb, ctx2, 0 );
53 x264_cabac_encode_decision( cb, ctx2, 1 );
54 x264_cabac_encode_decision( cb, ctx3, h->mb.i_cbp_chroma != 1 );
56 x264_cabac_encode_decision( cb, ctx4, i_pred>>1 );
57 x264_cabac_encode_decision( cb, ctx5, i_pred&1 );
61 static void x264_cabac_mb_type( x264_t *h, x264_cabac_t *cb )
63 const int i_mb_type = h->mb.i_type;
66 (!(h->mb.i_mb_y & 1) || IS_SKIP(h->mb.type[h->mb.i_mb_xy - h->mb.i_mb_stride])) )
68 x264_cabac_encode_decision( cb, 70 + h->mb.cache.i_neighbour_interlaced, h->mb.b_interlaced );
71 if( h->sh.i_type == SLICE_TYPE_I )
74 if( h->mb.i_mb_type_left >= 0 && h->mb.i_mb_type_left != I_4x4 )
78 if( h->mb.i_mb_type_top >= 0 && h->mb.i_mb_type_top != I_4x4 )
83 x264_cabac_mb_type_intra( h, cb, i_mb_type, 3+ctx, 3+3, 3+4, 3+5, 3+6, 3+7 );
85 else if( h->sh.i_type == SLICE_TYPE_P )
87 /* prefix: 14, suffix: 17 */
88 if( i_mb_type == P_L0 )
90 if( h->mb.i_partition == D_16x16 )
92 x264_cabac_encode_decision( cb, 14, 0 );
93 x264_cabac_encode_decision( cb, 15, 0 );
94 x264_cabac_encode_decision( cb, 16, 0 );
96 else if( h->mb.i_partition == D_16x8 )
98 x264_cabac_encode_decision( cb, 14, 0 );
99 x264_cabac_encode_decision( cb, 15, 1 );
100 x264_cabac_encode_decision( cb, 17, 1 );
102 else if( h->mb.i_partition == D_8x16 )
104 x264_cabac_encode_decision( cb, 14, 0 );
105 x264_cabac_encode_decision( cb, 15, 1 );
106 x264_cabac_encode_decision( cb, 17, 0 );
109 else if( i_mb_type == P_8x8 )
111 x264_cabac_encode_decision( cb, 14, 0 );
112 x264_cabac_encode_decision( cb, 15, 0 );
113 x264_cabac_encode_decision( cb, 16, 1 );
118 x264_cabac_encode_decision( cb, 14, 1 );
121 x264_cabac_mb_type_intra( h, cb, i_mb_type, 17+0, 17+1, 17+2, 17+2, 17+3, 17+3 );
124 else if( h->sh.i_type == SLICE_TYPE_B )
127 if( h->mb.i_mb_type_left >= 0 && h->mb.i_mb_type_left != B_SKIP && h->mb.i_mb_type_left != B_DIRECT )
131 if( h->mb.i_mb_type_top >= 0 && h->mb.i_mb_type_top != B_SKIP && h->mb.i_mb_type_top != B_DIRECT )
136 if( i_mb_type == B_DIRECT )
138 x264_cabac_encode_decision( cb, 27+ctx, 0 );
140 else if( i_mb_type == B_8x8 )
142 x264_cabac_encode_decision( cb, 27+ctx, 1 );
143 x264_cabac_encode_decision( cb, 27+3, 1 );
144 x264_cabac_encode_decision( cb, 27+4, 1 );
146 x264_cabac_encode_decision( cb, 27+5, 1 );
147 x264_cabac_encode_decision( cb, 27+5, 1 );
148 x264_cabac_encode_decision( cb, 27+5, 1 );
150 else if( IS_INTRA( i_mb_type ) )
153 x264_cabac_encode_decision( cb, 27+ctx, 1 );
154 x264_cabac_encode_decision( cb, 27+3, 1 );
155 x264_cabac_encode_decision( cb, 27+4, 1 );
157 x264_cabac_encode_decision( cb, 27+5, 1 );
158 x264_cabac_encode_decision( cb, 27+5, 0 );
159 x264_cabac_encode_decision( cb, 27+5, 1 );
162 x264_cabac_mb_type_intra( h, cb, i_mb_type, 32+0, 32+1, 32+2, 32+2, 32+3, 32+3 );
166 static const int i_mb_len[9*3] =
178 static const int i_mb_bits[9*3][7] =
180 { 1,1,0,0,0,1 }, { 1,1,0,0,1,0, }, { 1,0,0 }, /* L0 L0 */
181 { 1,1,0,1,0,1 }, { 1,1,0,1,1,0 }, {0}, /* L0 L1 */
182 { 1,1,1,0,0,0,0 }, { 1,1,1,0,0,0,1 }, {0}, /* L0 BI */
183 { 1,1,0,1,1,1 }, { 1,1,1,1,1,0 }, {0}, /* L1 L0 */
184 { 1,1,0,0,1,1 }, { 1,1,0,1,0,0 }, { 1,0,1 }, /* L1 L1 */
185 { 1,1,1,0,0,1,0 }, { 1,1,1,0,0,1,1 }, {0}, /* L1 BI */
186 { 1,1,1,0,1,0,0 }, { 1,1,1,0,1,0,1 }, {0}, /* BI L0 */
187 { 1,1,1,0,1,1,0 }, { 1,1,1,0,1,1,1 }, {0}, /* BI L1 */
188 { 1,1,1,1,0,0,0 }, { 1,1,1,1,0,0,1 }, { 1,1,0,0,0,0 }, /* BI BI */
191 const int idx = (i_mb_type - B_L0_L0) * 3 + (h->mb.i_partition - D_16x8);
194 x264_cabac_encode_decision( cb, 27+ctx, i_mb_bits[idx][0] );
195 x264_cabac_encode_decision( cb, 27+3, i_mb_bits[idx][1] );
196 x264_cabac_encode_decision( cb, 27+5-i_mb_bits[idx][1], i_mb_bits[idx][2] );
197 for( i = 3; i < i_mb_len[idx]; i++ )
198 x264_cabac_encode_decision( cb, 27+5, i_mb_bits[idx][i] );
203 x264_log(h, X264_LOG_ERROR, "unknown SLICE_TYPE unsupported in x264_macroblock_write_cabac\n" );
207 static void x264_cabac_mb_intra4x4_pred_mode( x264_cabac_t *cb, int i_pred, int i_mode )
209 if( i_pred == i_mode )
211 /* b_prev_intra4x4_pred_mode */
212 x264_cabac_encode_decision( cb, 68, 1 );
216 /* b_prev_intra4x4_pred_mode */
217 x264_cabac_encode_decision( cb, 68, 0 );
218 if( i_mode > i_pred )
222 x264_cabac_encode_decision( cb, 69, (i_mode )&0x01 );
223 x264_cabac_encode_decision( cb, 69, (i_mode >> 1)&0x01 );
224 x264_cabac_encode_decision( cb, 69, (i_mode >> 2)&0x01 );
228 static void x264_cabac_mb_intra_chroma_pred_mode( x264_t *h, x264_cabac_t *cb )
230 const int i_mode = x264_mb_pred_mode8x8c_fix[ h->mb.i_chroma_pred_mode ];
233 /* No need to test for I4x4 or I_16x16 as cache_save handle that */
234 if( (h->mb.i_neighbour & MB_LEFT) && h->mb.chroma_pred_mode[h->mb.i_mb_xy - 1] != 0 )
238 if( (h->mb.i_neighbour & MB_TOP) && h->mb.chroma_pred_mode[h->mb.i_mb_top_xy] != 0 )
243 x264_cabac_encode_decision( cb, 64 + ctx, i_mode > 0 );
246 x264_cabac_encode_decision( cb, 64 + 3, i_mode > 1 );
249 x264_cabac_encode_decision( cb, 64 + 3, i_mode > 2 );
254 static void x264_cabac_mb_cbp_luma( x264_t *h, x264_cabac_t *cb )
256 /* TODO: clean up and optimize */
258 for( i8x8 = 0; i8x8 < 4; i8x8++ )
262 int x = block_idx_x[4*i8x8];
263 int y = block_idx_y[4*i8x8];
267 i_mba_xy = h->mb.i_mb_xy;
268 else if( h->mb.i_neighbour & MB_LEFT )
269 i_mba_xy = h->mb.i_mb_xy - 1;
272 i_mbb_xy = h->mb.i_mb_xy;
273 else if( h->mb.i_neighbour & MB_TOP )
274 i_mbb_xy = h->mb.i_mb_top_xy;
277 /* No need to test for PCM and SKIP */
280 const int i8x8a = block_idx_xy[(x-1)&0x03][y]/4;
281 if( ((h->mb.cbp[i_mba_xy] >> i8x8a)&0x01) == 0 )
289 const int i8x8b = block_idx_xy[x][(y-1)&0x03]/4;
290 if( ((h->mb.cbp[i_mbb_xy] >> i8x8b)&0x01) == 0 )
296 x264_cabac_encode_decision( cb, 73 + ctx, (h->mb.i_cbp_luma >> i8x8)&0x01 );
300 static void x264_cabac_mb_cbp_chroma( x264_t *h, x264_cabac_t *cb )
306 /* No need to test for SKIP/PCM */
307 if( h->mb.i_neighbour & MB_LEFT )
309 cbp_a = (h->mb.cbp[h->mb.i_mb_xy - 1] >> 4)&0x3;
312 if( h->mb.i_neighbour & MB_TOP )
314 cbp_b = (h->mb.cbp[h->mb.i_mb_top_xy] >> 4)&0x3;
318 if( cbp_a > 0 ) ctx++;
319 if( cbp_b > 0 ) ctx += 2;
320 if( h->mb.i_cbp_chroma == 0 )
322 x264_cabac_encode_decision( cb, 77 + ctx, 0 );
326 x264_cabac_encode_decision( cb, 77 + ctx, 1 );
329 if( cbp_a == 2 ) ctx++;
330 if( cbp_b == 2 ) ctx += 2;
331 x264_cabac_encode_decision( cb, 77 + ctx, h->mb.i_cbp_chroma > 1 );
335 /* TODO check it with != qp per mb */
336 static void x264_cabac_mb_qp_delta( x264_t *h, x264_cabac_t *cb )
338 int i_mbn_xy = h->mb.i_mb_prev_xy;
339 int i_dqp = h->mb.i_qp - h->mb.i_last_qp;
342 /* No need to test for PCM / SKIP */
343 if( h->mb.i_last_dqp &&
344 ( h->mb.type[i_mbn_xy] == I_16x16 || (h->mb.cbp[i_mbn_xy]&0x3f) ) )
351 int val = i_dqp <= 0 ? (-2*i_dqp) : (2*i_dqp - 1);
352 /* dqp is interpreted modulo 52 */
353 if( val >= 51 && val != 52 )
357 x264_cabac_encode_decision( cb, 60 + ctx, 1 );
364 x264_cabac_encode_decision( cb, 60 + ctx, 0 );
368 void x264_cabac_mb_skip( x264_t *h, int b_skip )
372 if( h->mb.i_mb_type_left >= 0 && !IS_SKIP( h->mb.i_mb_type_left ) )
376 if( h->mb.i_mb_type_top >= 0 && !IS_SKIP( h->mb.i_mb_type_top ) )
381 ctx += (h->sh.i_type == SLICE_TYPE_P) ? 11 : 24;
382 x264_cabac_encode_decision( &h->cabac, ctx, b_skip );
386 static inline void x264_cabac_mb_sub_p_partition( x264_cabac_t *cb, int i_sub )
388 if( i_sub == D_L0_8x8 )
390 x264_cabac_encode_decision( cb, 21, 1 );
392 else if( i_sub == D_L0_8x4 )
394 x264_cabac_encode_decision( cb, 21, 0 );
395 x264_cabac_encode_decision( cb, 22, 0 );
397 else if( i_sub == D_L0_4x8 )
399 x264_cabac_encode_decision( cb, 21, 0 );
400 x264_cabac_encode_decision( cb, 22, 1 );
401 x264_cabac_encode_decision( cb, 23, 1 );
403 else if( i_sub == D_L0_4x4 )
405 x264_cabac_encode_decision( cb, 21, 0 );
406 x264_cabac_encode_decision( cb, 22, 1 );
407 x264_cabac_encode_decision( cb, 23, 0 );
411 static inline void x264_cabac_mb_sub_b_partition( x264_cabac_t *cb, int i_sub )
413 #define WRITE_SUB_3(a,b,c) {\
414 x264_cabac_encode_decision( cb, 36, a );\
415 x264_cabac_encode_decision( cb, 37, b );\
416 x264_cabac_encode_decision( cb, 39, c );\
418 #define WRITE_SUB_5(a,b,c,d,e) {\
419 x264_cabac_encode_decision( cb, 36, a );\
420 x264_cabac_encode_decision( cb, 37, b );\
421 x264_cabac_encode_decision( cb, 38, c );\
422 x264_cabac_encode_decision( cb, 39, d );\
423 x264_cabac_encode_decision( cb, 39, e );\
425 #define WRITE_SUB_6(a,b,c,d,e,f) {\
426 WRITE_SUB_5(a,b,c,d,e)\
427 x264_cabac_encode_decision( cb, 39, f );\
433 x264_cabac_encode_decision( cb, 36, 0 );
435 case D_L0_8x8: WRITE_SUB_3(1,0,0); break;
436 case D_L1_8x8: WRITE_SUB_3(1,0,1); break;
437 case D_BI_8x8: WRITE_SUB_5(1,1,0,0,0); break;
438 case D_L0_8x4: WRITE_SUB_5(1,1,0,0,1); break;
439 case D_L0_4x8: WRITE_SUB_5(1,1,0,1,0); break;
440 case D_L1_8x4: WRITE_SUB_5(1,1,0,1,1); break;
441 case D_L1_4x8: WRITE_SUB_6(1,1,1,0,0,0); break;
442 case D_BI_8x4: WRITE_SUB_6(1,1,1,0,0,1); break;
443 case D_BI_4x8: WRITE_SUB_6(1,1,1,0,1,0); break;
444 case D_L0_4x4: WRITE_SUB_6(1,1,1,0,1,1); break;
445 case D_L1_4x4: WRITE_SUB_5(1,1,1,1,0); break;
446 case D_BI_4x4: WRITE_SUB_5(1,1,1,1,1); break;
450 static inline void x264_cabac_mb_transform_size( x264_t *h, x264_cabac_t *cb )
452 int ctx = 399 + h->mb.cache.i_neighbour_transform_size;
453 x264_cabac_encode_decision( cb, ctx, h->mb.b_transform_8x8 );
456 static inline void x264_cabac_mb_ref( x264_t *h, x264_cabac_t *cb, int i_list, int idx )
458 const int i8 = x264_scan8[idx];
459 const int i_refa = h->mb.cache.ref[i_list][i8 - 1];
460 const int i_refb = h->mb.cache.ref[i_list][i8 - 8];
461 int i_ref = h->mb.cache.ref[i_list][i8];
464 if( i_refa > 0 && !h->mb.cache.skip[i8 - 1])
466 if( i_refb > 0 && !h->mb.cache.skip[i8 - 8])
471 x264_cabac_encode_decision( cb, 54 + ctx, 1 );
479 x264_cabac_encode_decision( cb, 54 + ctx, 0 );
484 static inline void x264_cabac_mb_mvd_cpn( x264_t *h, x264_cabac_t *cb, int i_list, int idx, int l, int mvd )
486 const int amvd = abs( h->mb.cache.mvd[i_list][x264_scan8[idx] - 1][l] ) +
487 abs( h->mb.cache.mvd[i_list][x264_scan8[idx] - 8][l] );
488 const int i_abs = abs( mvd );
489 const int i_prefix = X264_MIN( i_abs, 9 );
490 const int ctxbase = (l == 0 ? 40 : 47);
502 for( i = 0; i < i_prefix; i++ )
504 x264_cabac_encode_decision( cb, ctxbase + ctx, 1 );
511 x264_cabac_encode_decision( cb, ctxbase + ctx, 0 );
513 x264_cabac_encode_ue_bypass( cb, 3, i_abs - 9 );
517 x264_cabac_encode_bypass( cb, mvd < 0 );
520 static inline void x264_cabac_mb_mvd( x264_t *h, x264_cabac_t *cb, int i_list, int idx, int width, int height )
526 x264_mb_predict_mv( h, i_list, idx, width, mvp );
527 mdx = h->mb.cache.mv[i_list][x264_scan8[idx]][0] - mvp[0];
528 mdy = h->mb.cache.mv[i_list][x264_scan8[idx]][1] - mvp[1];
531 x264_cabac_mb_mvd_cpn( h, cb, i_list, idx, 0, mdx );
532 x264_cabac_mb_mvd_cpn( h, cb, i_list, idx, 1, mdy );
535 x264_macroblock_cache_mvd( h, block_idx_x[idx], block_idx_y[idx], width, height, i_list, mdx, mdy );
538 static inline void x264_cabac_mb8x8_mvd( x264_t *h, x264_cabac_t *cb, int i_list, int i )
540 if( !x264_mb_partition_listX_table[i_list][ h->mb.i_sub_partition[i] ] )
543 switch( h->mb.i_sub_partition[i] )
548 x264_cabac_mb_mvd( h, cb, i_list, 4*i, 2, 2 );
553 x264_cabac_mb_mvd( h, cb, i_list, 4*i+0, 2, 1 );
554 x264_cabac_mb_mvd( h, cb, i_list, 4*i+2, 2, 1 );
559 x264_cabac_mb_mvd( h, cb, i_list, 4*i+0, 1, 2 );
560 x264_cabac_mb_mvd( h, cb, i_list, 4*i+1, 1, 2 );
565 x264_cabac_mb_mvd( h, cb, i_list, 4*i+0, 1, 1 );
566 x264_cabac_mb_mvd( h, cb, i_list, 4*i+1, 1, 1 );
567 x264_cabac_mb_mvd( h, cb, i_list, 4*i+2, 1, 1 );
568 x264_cabac_mb_mvd( h, cb, i_list, 4*i+3, 1, 1 );
573 static int x264_cabac_mb_cbf_ctxidxinc( x264_t *h, int i_cat, int i_idx )
581 if( i_cat == DCT_LUMA_DC )
583 if( h->mb.i_neighbour & MB_LEFT )
585 i_mba_xy = h->mb.i_mb_xy - 1;
586 if( h->mb.i_mb_type_left == I_16x16 )
587 i_nza = h->mb.cbp[i_mba_xy] & 0x100;
589 if( h->mb.i_neighbour & MB_TOP )
591 i_mbb_xy = h->mb.i_mb_top_xy;
592 if( h->mb.i_mb_type_top == I_16x16 )
593 i_nzb = h->mb.cbp[i_mbb_xy] & 0x100;
596 else if( i_cat == DCT_LUMA_AC || i_cat == DCT_LUMA_4x4 )
598 if( i_idx & ~10 ) // block_idx_x > 0
599 i_mba_xy = h->mb.i_mb_xy;
600 else if( h->mb.i_neighbour & MB_LEFT )
601 i_mba_xy = h->mb.i_mb_xy - 1;
603 if( i_idx & ~5 ) // block_idx_y > 0
604 i_mbb_xy = h->mb.i_mb_xy;
605 else if( h->mb.i_neighbour & MB_TOP )
606 i_mbb_xy = h->mb.i_mb_top_xy;
608 /* no need to test for skip/pcm */
610 i_nza = h->mb.cache.non_zero_count[x264_scan8[i_idx] - 1];
612 i_nzb = h->mb.cache.non_zero_count[x264_scan8[i_idx] - 8];
614 else if( i_cat == DCT_CHROMA_DC )
616 /* no need to test skip/pcm */
617 if( h->mb.i_neighbour & MB_LEFT )
619 i_mba_xy = h->mb.i_mb_xy - 1;
620 i_nza = h->mb.cbp[i_mba_xy] & (0x200 << i_idx);
622 if( h->mb.i_neighbour & MB_TOP )
624 i_mbb_xy = h->mb.i_mb_top_xy;
625 i_nzb = h->mb.cbp[i_mbb_xy] & (0x200 << i_idx);
628 else if( i_cat == DCT_CHROMA_AC )
631 i_mba_xy = h->mb.i_mb_xy;
632 else if( h->mb.i_neighbour & MB_LEFT )
633 i_mba_xy = h->mb.i_mb_xy - 1;
636 i_mbb_xy = h->mb.i_mb_xy;
637 else if( h->mb.i_neighbour & MB_TOP )
638 i_mbb_xy = h->mb.i_mb_top_xy;
640 /* no need to test skip/pcm */
642 i_nza = h->mb.cache.non_zero_count[x264_scan8[i_idx] - 1];
644 i_nzb = h->mb.cache.non_zero_count[x264_scan8[i_idx] - 8];
647 if( IS_INTRA( h->mb.i_type ) )
664 static const int significant_coeff_flag_offset[2][6] = {
665 { 105, 120, 134, 149, 152, 402 },
666 { 277, 292, 306, 321, 324, 436 }
668 static const int last_coeff_flag_offset[2][6] = {
669 { 166, 181, 195, 210, 213, 417 },
670 { 338, 353, 367, 382, 385, 451 }
672 static const int coeff_abs_level_m1_offset[6] =
673 { 227, 237, 247, 257, 266, 426 };
674 static const int significant_coeff_flag_offset_8x8[2][63] =
676 0, 1, 2, 3, 4, 5, 5, 4, 4, 3, 3, 4, 4, 4, 5, 5,
677 4, 4, 4, 4, 3, 3, 6, 7, 7, 7, 8, 9,10, 9, 8, 7,
678 7, 6,11,12,13,11, 6, 7, 8, 9,14,10, 9, 8, 6,11,
679 12,13,11, 6, 9,14,10, 9,11,12,13,11,14,10,12
681 0, 1, 1, 2, 2, 3, 3, 4, 5, 6, 7, 7, 7, 8, 4, 5,
682 6, 9,10,10, 8,11,12,11, 9, 9,10,10, 8,11,12,11,
683 9, 9,10,10, 8,11,12,11, 9, 9,10,10, 8,13,13, 9,
684 9,10,10, 8,13,13, 9, 9,10,10,14,14,14,14,14
686 static const int last_coeff_flag_offset_8x8[63] = {
687 0, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1,
688 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2,
689 3, 3, 3, 3, 3, 3, 3, 3, 4, 4, 4, 4, 4, 4, 4, 4,
690 5, 5, 5, 5, 6, 6, 6, 6, 7, 7, 7, 7, 8, 8, 8
692 static const int identity[16] =
693 { 0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15 };
695 // node ctx: 0..3: abslevel1 (with abslevelgt1 == 0).
696 // 4..7: abslevelgt1 + 3 (and abslevel1 doesn't matter).
697 /* map node ctx => cabac ctx for level=1 */
698 static const int coeff_abs_level1_ctx[8] = { 1, 2, 3, 4, 0, 0, 0, 0 };
699 /* map node ctx => cabac ctx for level>1 */
700 static const int coeff_abs_levelgt1_ctx[8] = { 5, 5, 5, 5, 6, 7, 8, 9 };
701 static const int coeff_abs_level_transition[2][8] = {
702 /* update node ctx after coding a level=1 */
703 { 1, 2, 3, 3, 4, 5, 6, 7 },
704 /* update node ctx after coding a level>1 */
705 { 4, 4, 4, 4, 5, 6, 7, 7 }
708 static void block_residual_write_cabac( x264_t *h, x264_cabac_t *cb, int i_ctxBlockCat, int i_idx, int16_t *l, int i_count )
710 const int i_ctx_sig = significant_coeff_flag_offset[h->mb.b_interlaced][i_ctxBlockCat];
711 const int i_ctx_last = last_coeff_flag_offset[h->mb.b_interlaced][i_ctxBlockCat];
712 const int i_ctx_level = coeff_abs_level_m1_offset[i_ctxBlockCat];
714 int i_coeff_abs_m1[64];
715 int i_coeff_sign[64];
722 const int *significant_coeff_flag_offset;
723 const int *last_coeff_flag_offset;
725 /* i_ctxBlockCat: 0-> DC 16x16 i_idx = 0
726 * 1-> AC 16x16 i_idx = luma4x4idx
727 * 2-> Luma4x4 i_idx = luma4x4idx
728 * 3-> DC Chroma i_idx = iCbCr
729 * 4-> AC Chroma i_idx = 4 * iCbCr + chroma4x4idx
730 * 5-> Luma8x8 i_idx = luma8x8idx
733 for( i = 0; i < i_count; i++ )
737 i_coeff_abs_m1[i_coeff] = abs( l[i] ) - 1;
738 i_coeff_sign[i_coeff] = ( l[i] < 0 );
747 /* coded block flag */
748 x264_cabac_encode_decision( cb, 85 + x264_cabac_mb_cbf_ctxidxinc( h, i_ctxBlockCat, i_idx ), i_coeff != 0 );
753 significant_coeff_flag_offset = (i_ctxBlockCat == DCT_LUMA_8x8)
754 ? significant_coeff_flag_offset_8x8[h->mb.b_interlaced]
756 last_coeff_flag_offset = (i_ctxBlockCat == DCT_LUMA_8x8)
757 ? last_coeff_flag_offset_8x8 : identity;
759 i_sigmap_size = X264_MIN( i_last+1, i_count-1 );
760 for( i = 0; i < i_sigmap_size; i++ )
762 x264_cabac_encode_decision( cb, i_ctx_sig + significant_coeff_flag_offset[i], l[i] != 0 );
764 x264_cabac_encode_decision( cb, i_ctx_last + last_coeff_flag_offset[i], i == i_last );
767 for( i = i_coeff - 1; i >= 0; i-- )
769 /* write coeff_abs - 1 */
770 const int i_prefix = X264_MIN( i_coeff_abs_m1[i], 14 );
771 const int i_ctxIdxInc = coeff_abs_level1_ctx[node_ctx] + i_ctx_level;
772 x264_cabac_encode_decision( cb, i_ctxIdxInc, i_prefix != 0 );
776 const int i_ctxIdxInc = coeff_abs_levelgt1_ctx[node_ctx] + i_ctx_level;
778 cb->f8_bits_encoded += cabac_prefix_size[i_prefix][cb->state[i_ctxIdxInc]];
779 cb->state[i_ctxIdxInc] = cabac_prefix_transition[i_prefix][cb->state[i_ctxIdxInc]];
782 for( j = 0; j < i_prefix - 1; j++ )
783 x264_cabac_encode_decision( cb, i_ctxIdxInc, 1 );
785 x264_cabac_encode_decision( cb, i_ctxIdxInc, 0 );
788 x264_cabac_encode_ue_bypass( cb, 0, i_coeff_abs_m1[i] - 14 );
790 node_ctx = coeff_abs_level_transition[1][node_ctx];
793 node_ctx = coeff_abs_level_transition[0][node_ctx];
799 x264_cabac_encode_bypass( cb, i_coeff_sign[i] );
805 void x264_macroblock_write_cabac( x264_t *h, x264_cabac_t *cb )
807 const int i_mb_type = h->mb.i_type;
812 const int i_mb_pos_start = x264_cabac_pos( cb );
816 /* Write the MB type */
817 x264_cabac_mb_type( h, cb );
819 /* PCM special block type UNTESTED */
820 if( i_mb_type == I_PCM )
823 cb->f8_bits_encoded += (384*8) << 8;
825 if( cb->p + 385 >= cb->p_end )
826 return; //FIXME throw an error
828 for( i = 0; i < 16; i++ )
830 memcpy( cb->p, h->fenc->plane[0] + i*h->mb.pic.i_stride[0], 16 );
834 for( i = 0; i < 8; i++ )
836 memcpy( cb->p, h->fenc->plane[1] + i*h->mb.pic.i_stride[1], 8 );
840 for( i = 0; i < 8; i++ )
842 memcpy( cb->p, h->fenc->plane[2] + i*h->mb.pic.i_stride[2], 8 );
845 x264_cabac_encode_init( cb, cb->p, cb->p_end );
850 if( IS_INTRA( i_mb_type ) )
852 if( h->pps->b_transform_8x8_mode && i_mb_type != I_16x16 )
853 x264_cabac_mb_transform_size( h, cb );
855 if( i_mb_type != I_16x16 )
857 int di = (i_mb_type == I_8x8) ? 4 : 1;
858 for( i = 0; i < 16; i += di )
860 const int i_pred = x264_mb_predict_intra4x4_mode( h, i );
861 const int i_mode = x264_mb_pred_mode4x4_fix( h->mb.cache.intra4x4_pred_mode[x264_scan8[i]] );
862 x264_cabac_mb_intra4x4_pred_mode( cb, i_pred, i_mode );
866 x264_cabac_mb_intra_chroma_pred_mode( h, cb );
868 else if( i_mb_type == P_L0 )
870 if( h->mb.i_partition == D_16x16 )
872 if( h->mb.pic.i_fref[0] > 1 )
874 x264_cabac_mb_ref( h, cb, 0, 0 );
876 x264_cabac_mb_mvd( h, cb, 0, 0, 4, 4 );
878 else if( h->mb.i_partition == D_16x8 )
880 if( h->mb.pic.i_fref[0] > 1 )
882 x264_cabac_mb_ref( h, cb, 0, 0 );
883 x264_cabac_mb_ref( h, cb, 0, 8 );
885 x264_cabac_mb_mvd( h, cb, 0, 0, 4, 2 );
886 x264_cabac_mb_mvd( h, cb, 0, 8, 4, 2 );
888 else if( h->mb.i_partition == D_8x16 )
890 if( h->mb.pic.i_fref[0] > 1 )
892 x264_cabac_mb_ref( h, cb, 0, 0 );
893 x264_cabac_mb_ref( h, cb, 0, 4 );
895 x264_cabac_mb_mvd( h, cb, 0, 0, 2, 4 );
896 x264_cabac_mb_mvd( h, cb, 0, 4, 2, 4 );
899 else if( i_mb_type == P_8x8 )
902 x264_cabac_mb_sub_p_partition( cb, h->mb.i_sub_partition[0] );
903 x264_cabac_mb_sub_p_partition( cb, h->mb.i_sub_partition[1] );
904 x264_cabac_mb_sub_p_partition( cb, h->mb.i_sub_partition[2] );
905 x264_cabac_mb_sub_p_partition( cb, h->mb.i_sub_partition[3] );
908 if( h->mb.pic.i_fref[0] > 1 )
910 x264_cabac_mb_ref( h, cb, 0, 0 );
911 x264_cabac_mb_ref( h, cb, 0, 4 );
912 x264_cabac_mb_ref( h, cb, 0, 8 );
913 x264_cabac_mb_ref( h, cb, 0, 12 );
916 for( i = 0; i < 4; i++ )
917 x264_cabac_mb8x8_mvd( h, cb, 0, i );
919 else if( i_mb_type == B_8x8 )
922 x264_cabac_mb_sub_b_partition( cb, h->mb.i_sub_partition[0] );
923 x264_cabac_mb_sub_b_partition( cb, h->mb.i_sub_partition[1] );
924 x264_cabac_mb_sub_b_partition( cb, h->mb.i_sub_partition[2] );
925 x264_cabac_mb_sub_b_partition( cb, h->mb.i_sub_partition[3] );
928 for( i_list = 0; i_list < 2; i_list++ )
930 if( ( i_list ? h->mb.pic.i_fref[1] : h->mb.pic.i_fref[0] ) == 1 )
932 for( i = 0; i < 4; i++ )
933 if( x264_mb_partition_listX_table[i_list][ h->mb.i_sub_partition[i] ] )
934 x264_cabac_mb_ref( h, cb, i_list, 4*i );
937 for( i = 0; i < 4; i++ )
938 x264_cabac_mb8x8_mvd( h, cb, 0, i );
939 for( i = 0; i < 4; i++ )
940 x264_cabac_mb8x8_mvd( h, cb, 1, i );
942 else if( i_mb_type != B_DIRECT )
947 /* init ref list utilisations */
948 for( i = 0; i < 2; i++ )
950 b_list[0][i] = x264_mb_type_list0_table[i_mb_type][i];
951 b_list[1][i] = x264_mb_type_list1_table[i_mb_type][i];
954 for( i_list = 0; i_list < 2; i_list++ )
956 const int i_ref_max = i_list == 0 ? h->mb.pic.i_fref[0] : h->mb.pic.i_fref[1];
960 if( h->mb.i_partition == D_16x16 )
962 if( b_list[i_list][0] ) x264_cabac_mb_ref( h, cb, i_list, 0 );
964 else if( h->mb.i_partition == D_16x8 )
966 if( b_list[i_list][0] ) x264_cabac_mb_ref( h, cb, i_list, 0 );
967 if( b_list[i_list][1] ) x264_cabac_mb_ref( h, cb, i_list, 8 );
969 else if( h->mb.i_partition == D_8x16 )
971 if( b_list[i_list][0] ) x264_cabac_mb_ref( h, cb, i_list, 0 );
972 if( b_list[i_list][1] ) x264_cabac_mb_ref( h, cb, i_list, 4 );
976 for( i_list = 0; i_list < 2; i_list++ )
978 if( h->mb.i_partition == D_16x16 )
980 if( b_list[i_list][0] ) x264_cabac_mb_mvd( h, cb, i_list, 0, 4, 4 );
982 else if( h->mb.i_partition == D_16x8 )
984 if( b_list[i_list][0] ) x264_cabac_mb_mvd( h, cb, i_list, 0, 4, 2 );
985 if( b_list[i_list][1] ) x264_cabac_mb_mvd( h, cb, i_list, 8, 4, 2 );
987 else if( h->mb.i_partition == D_8x16 )
989 if( b_list[i_list][0] ) x264_cabac_mb_mvd( h, cb, i_list, 0, 2, 4 );
990 if( b_list[i_list][1] ) x264_cabac_mb_mvd( h, cb, i_list, 4, 2, 4 );
996 i_mb_pos_tex = x264_cabac_pos( cb );
997 h->stat.frame.i_hdr_bits += i_mb_pos_tex - i_mb_pos_start;
1000 if( i_mb_type != I_16x16 )
1002 x264_cabac_mb_cbp_luma( h, cb );
1003 x264_cabac_mb_cbp_chroma( h, cb );
1006 if( x264_mb_transform_8x8_allowed( h ) && h->mb.i_cbp_luma )
1008 x264_cabac_mb_transform_size( h, cb );
1011 if( h->mb.i_cbp_luma > 0 || h->mb.i_cbp_chroma > 0 || i_mb_type == I_16x16 )
1013 x264_cabac_mb_qp_delta( h, cb );
1015 /* write residual */
1016 if( i_mb_type == I_16x16 )
1019 block_residual_write_cabac( h, cb, DCT_LUMA_DC, 0, h->dct.luma16x16_dc, 16 );
1022 if( h->mb.i_cbp_luma != 0 )
1023 for( i = 0; i < 16; i++ )
1024 block_residual_write_cabac( h, cb, DCT_LUMA_AC, i, h->dct.luma4x4[i]+1, 15 );
1026 else if( h->mb.b_transform_8x8 )
1028 for( i = 0; i < 4; i++ )
1029 if( h->mb.i_cbp_luma & ( 1 << i ) )
1030 block_residual_write_cabac( h, cb, DCT_LUMA_8x8, i, h->dct.luma8x8[i], 64 );
1034 for( i = 0; i < 16; i++ )
1035 if( h->mb.i_cbp_luma & ( 1 << ( i / 4 ) ) )
1036 block_residual_write_cabac( h, cb, DCT_LUMA_4x4, i, h->dct.luma4x4[i], 16 );
1039 if( h->mb.i_cbp_chroma &0x03 ) /* Chroma DC residual present */
1041 block_residual_write_cabac( h, cb, DCT_CHROMA_DC, 0, h->dct.chroma_dc[0], 4 );
1042 block_residual_write_cabac( h, cb, DCT_CHROMA_DC, 1, h->dct.chroma_dc[1], 4 );
1044 if( h->mb.i_cbp_chroma&0x02 ) /* Chroma AC residual present */
1046 for( i = 16; i < 24; i++ )
1047 block_residual_write_cabac( h, cb, DCT_CHROMA_AC, i, h->dct.luma4x4[i]+1, 15 );
1052 if( IS_INTRA( i_mb_type ) )
1053 h->stat.frame.i_itex_bits += x264_cabac_pos( cb ) - i_mb_pos_tex;
1055 h->stat.frame.i_ptex_bits += x264_cabac_pos( cb ) - i_mb_pos_tex;
1060 /*****************************************************************************
1061 * RD only; doesn't generate a valid bitstream
1062 * doesn't write cbp or chroma dc (I don't know how much this matters)
1063 * works on all partition sizes except 16x16
1064 * for sub8x8, call once per 8x8 block
1065 *****************************************************************************/
1066 void x264_partition_size_cabac( x264_t *h, x264_cabac_t *cb, int i8, int i_pixel )
1068 const int i_mb_type = h->mb.i_type;
1071 if( i_mb_type == P_8x8 )
1073 x264_cabac_mb_sub_p_partition( cb, h->mb.i_sub_partition[i8] );
1074 if( h->mb.pic.i_fref[0] > 1 )
1075 x264_cabac_mb_ref( h, cb, 0, 4*i8 );
1076 x264_cabac_mb8x8_mvd( h, cb, 0, i8 );
1078 else if( i_mb_type == P_L0 )
1080 if( h->mb.pic.i_fref[0] > 1 )
1081 x264_cabac_mb_ref( h, cb, 0, 4*i8 );
1082 if( h->mb.i_partition == D_16x8 )
1083 x264_cabac_mb_mvd( h, cb, 0, 4*i8, 4, 2 );
1085 x264_cabac_mb_mvd( h, cb, 0, 4*i8, 2, 4 );
1087 else if( i_mb_type == B_8x8 )
1089 x264_cabac_mb_sub_b_partition( cb, h->mb.i_sub_partition[i8] );
1091 if( h->mb.pic.i_fref[0] > 1
1092 && x264_mb_partition_listX_table[0][ h->mb.i_sub_partition[i8] ] )
1093 x264_cabac_mb_ref( h, cb, 0, 4*i8 );
1094 if( h->mb.pic.i_fref[1] > 1
1095 && x264_mb_partition_listX_table[1][ h->mb.i_sub_partition[i8] ] )
1096 x264_cabac_mb_ref( h, cb, 1, 4*i8 );
1098 x264_cabac_mb8x8_mvd( h, cb, 0, i8 );
1099 x264_cabac_mb8x8_mvd( h, cb, 1, i8 );
1103 x264_log(h, X264_LOG_ERROR, "invalid/unhandled mb_type\n" );
1107 for( j = (i_pixel < PIXEL_8x8); j >= 0; j-- )
1109 if( h->mb.i_cbp_luma & (1 << i8) )
1111 if( h->mb.b_transform_8x8 )
1112 block_residual_write_cabac( h, cb, DCT_LUMA_8x8, i8, h->dct.luma8x8[i8], 64 );
1116 for( i4 = 0; i4 < 4; i4++ )
1117 block_residual_write_cabac( h, cb, DCT_LUMA_4x4, i4+i8*4, h->dct.luma4x4[i4+i8*4], 16 );
1121 block_residual_write_cabac( h, cb, DCT_CHROMA_AC, 16+i8, h->dct.luma4x4[16+i8]+1, 15 );
1122 block_residual_write_cabac( h, cb, DCT_CHROMA_AC, 20+i8, h->dct.luma4x4[20+i8]+1, 15 );
1124 i8 += x264_pixel_size[i_pixel].h >> 3;
1128 static void x264_partition_i8x8_size_cabac( x264_t *h, x264_cabac_t *cb, int i8, int i_mode )
1130 const int i_pred = x264_mb_predict_intra4x4_mode( h, 4*i8 );
1131 i_mode = x264_mb_pred_mode4x4_fix( i_mode );
1132 x264_cabac_mb_intra4x4_pred_mode( cb, i_pred, i_mode );
1133 block_residual_write_cabac( h, cb, DCT_LUMA_8x8, 4*i8, h->dct.luma8x8[i8], 64 );
1136 static void x264_partition_i4x4_size_cabac( x264_t *h, x264_cabac_t *cb, int i4, int i_mode )
1138 const int i_pred = x264_mb_predict_intra4x4_mode( h, i4 );
1139 i_mode = x264_mb_pred_mode4x4_fix( i_mode );
1140 x264_cabac_mb_intra4x4_pred_mode( cb, i_pred, i_mode );
1141 block_residual_write_cabac( h, cb, DCT_LUMA_4x4, i4, h->dct.luma4x4[i4], 16 );
1144 static void x264_i8x8_chroma_size_cabac( x264_t *h, x264_cabac_t *cb )
1146 x264_cabac_mb_intra_chroma_pred_mode( h, cb );
1147 if( h->mb.i_cbp_chroma > 0 )
1149 block_residual_write_cabac( h, cb, DCT_CHROMA_DC, 0, h->dct.chroma_dc[0], 4 );
1150 block_residual_write_cabac( h, cb, DCT_CHROMA_DC, 1, h->dct.chroma_dc[1], 4 );
1152 if( h->mb.i_cbp_chroma == 2 )
1155 for( i = 16; i < 24; i++ )
1156 block_residual_write_cabac( h, cb, DCT_CHROMA_AC, i, h->dct.luma4x4[i]+1, 15 );