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_encode_ue_bypass( x264_cabac_t *cb, int exp_bits, int val )
30 cb->f8_bits_encoded += ( bs_size_ue( val + (1<<exp_bits)-1 ) - exp_bits ) << 8;
33 for( k = exp_bits; val >= (1<<k); k++ )
35 x264_cabac_encode_bypass( cb, 1 );
38 x264_cabac_encode_bypass( cb, 0 );
40 x264_cabac_encode_bypass( cb, (val >> k)&0x01 );
44 static inline void x264_cabac_mb_type_intra( x264_t *h, x264_cabac_t *cb, int i_mb_type,
45 int ctx0, int ctx1, int ctx2, int ctx3, int ctx4, int ctx5 )
47 if( i_mb_type == I_4x4 || i_mb_type == I_8x8 )
49 x264_cabac_encode_decision( cb, ctx0, 0 );
51 else if( i_mb_type == I_PCM )
53 x264_cabac_encode_decision( cb, ctx0, 1 );
54 x264_cabac_encode_terminal( cb, 1 );
55 x264_cabac_encode_flush( cb );
59 int i_pred = x264_mb_pred_mode16x16_fix[h->mb.i_intra16x16_pred_mode];
61 x264_cabac_encode_decision( cb, ctx0, 1 );
62 x264_cabac_encode_terminal( cb, 0 );
64 x264_cabac_encode_decision( cb, ctx1, ( h->mb.i_cbp_luma == 0 ? 0 : 1 ));
65 if( h->mb.i_cbp_chroma == 0 )
67 x264_cabac_encode_decision( cb, ctx2, 0 );
71 x264_cabac_encode_decision( cb, ctx2, 1 );
72 x264_cabac_encode_decision( cb, ctx3, ( h->mb.i_cbp_chroma == 1 ? 0 : 1 ) );
74 x264_cabac_encode_decision( cb, ctx4, ( (i_pred / 2) ? 1 : 0 ));
75 x264_cabac_encode_decision( cb, ctx5, ( (i_pred % 2) ? 1 : 0 ));
79 static void x264_cabac_mb_type( x264_t *h, x264_cabac_t *cb )
81 const int i_mb_type = h->mb.i_type;
84 (!(h->mb.i_mb_y & 1) || IS_SKIP(h->mb.type[h->mb.i_mb_xy - h->mb.i_mb_stride])) )
86 x264_cabac_encode_decision( cb, 70 + h->mb.cache.i_neighbour_interlaced, h->mb.b_interlaced );
89 if( h->sh.i_type == SLICE_TYPE_I )
92 if( h->mb.i_mb_type_left >= 0 && h->mb.i_mb_type_left != I_4x4 )
96 if( h->mb.i_mb_type_top >= 0 && h->mb.i_mb_type_top != I_4x4 )
101 x264_cabac_mb_type_intra( h, cb, i_mb_type, 3+ctx, 3+3, 3+4, 3+5, 3+6, 3+7 );
103 else if( h->sh.i_type == SLICE_TYPE_P )
105 /* prefix: 14, suffix: 17 */
106 if( i_mb_type == P_L0 )
108 if( h->mb.i_partition == D_16x16 )
110 x264_cabac_encode_decision( cb, 14, 0 );
111 x264_cabac_encode_decision( cb, 15, 0 );
112 x264_cabac_encode_decision( cb, 16, 0 );
114 else if( h->mb.i_partition == D_16x8 )
116 x264_cabac_encode_decision( cb, 14, 0 );
117 x264_cabac_encode_decision( cb, 15, 1 );
118 x264_cabac_encode_decision( cb, 17, 1 );
120 else if( h->mb.i_partition == D_8x16 )
122 x264_cabac_encode_decision( cb, 14, 0 );
123 x264_cabac_encode_decision( cb, 15, 1 );
124 x264_cabac_encode_decision( cb, 17, 0 );
127 else if( i_mb_type == P_8x8 )
129 x264_cabac_encode_decision( cb, 14, 0 );
130 x264_cabac_encode_decision( cb, 15, 0 );
131 x264_cabac_encode_decision( cb, 16, 1 );
136 x264_cabac_encode_decision( cb, 14, 1 );
139 x264_cabac_mb_type_intra( h, cb, i_mb_type, 17+0, 17+1, 17+2, 17+2, 17+3, 17+3 );
142 else if( h->sh.i_type == SLICE_TYPE_B )
145 if( h->mb.i_mb_type_left >= 0 && h->mb.i_mb_type_left != B_SKIP && h->mb.i_mb_type_left != B_DIRECT )
149 if( h->mb.i_mb_type_top >= 0 && h->mb.i_mb_type_top != B_SKIP && h->mb.i_mb_type_top != B_DIRECT )
154 if( i_mb_type == B_DIRECT )
156 x264_cabac_encode_decision( cb, 27+ctx, 0 );
158 else if( i_mb_type == B_8x8 )
160 x264_cabac_encode_decision( cb, 27+ctx, 1 );
161 x264_cabac_encode_decision( cb, 27+3, 1 );
162 x264_cabac_encode_decision( cb, 27+4, 1 );
164 x264_cabac_encode_decision( cb, 27+5, 1 );
165 x264_cabac_encode_decision( cb, 27+5, 1 );
166 x264_cabac_encode_decision( cb, 27+5, 1 );
168 else if( IS_INTRA( i_mb_type ) )
171 x264_cabac_encode_decision( cb, 27+ctx, 1 );
172 x264_cabac_encode_decision( cb, 27+3, 1 );
173 x264_cabac_encode_decision( cb, 27+4, 1 );
175 x264_cabac_encode_decision( cb, 27+5, 1 );
176 x264_cabac_encode_decision( cb, 27+5, 0 );
177 x264_cabac_encode_decision( cb, 27+5, 1 );
180 x264_cabac_mb_type_intra( h, cb, i_mb_type, 32+0, 32+1, 32+2, 32+2, 32+3, 32+3 );
184 static const int i_mb_len[9*3] =
196 static const int i_mb_bits[9*3][7] =
198 { 1,1,0,0,0,1 }, { 1,1,0,0,1,0, }, { 1,0,0 }, /* L0 L0 */
199 { 1,1,0,1,0,1 }, { 1,1,0,1,1,0 }, {0}, /* L0 L1 */
200 { 1,1,1,0,0,0,0 }, { 1,1,1,0,0,0,1 }, {0}, /* L0 BI */
201 { 1,1,0,1,1,1 }, { 1,1,1,1,1,0 }, {0}, /* L1 L0 */
202 { 1,1,0,0,1,1 }, { 1,1,0,1,0,0 }, { 1,0,1 }, /* L1 L1 */
203 { 1,1,1,0,0,1,0 }, { 1,1,1,0,0,1,1 }, {0}, /* L1 BI */
204 { 1,1,1,0,1,0,0 }, { 1,1,1,0,1,0,1 }, {0}, /* BI L0 */
205 { 1,1,1,0,1,1,0 }, { 1,1,1,0,1,1,1 }, {0}, /* BI L1 */
206 { 1,1,1,1,0,0,0 }, { 1,1,1,1,0,0,1 }, { 1,1,0,0,0,0 }, /* BI BI */
209 const int idx = (i_mb_type - B_L0_L0) * 3 + (h->mb.i_partition - D_16x8);
212 x264_cabac_encode_decision( cb, 27+ctx, i_mb_bits[idx][0] );
213 x264_cabac_encode_decision( cb, 27+3, i_mb_bits[idx][1] );
214 x264_cabac_encode_decision( cb, 27+5-i_mb_bits[idx][1], i_mb_bits[idx][2] );
215 for( i = 3; i < i_mb_len[idx]; i++ )
216 x264_cabac_encode_decision( cb, 27+5, i_mb_bits[idx][i] );
221 x264_log(h, X264_LOG_ERROR, "unknown SLICE_TYPE unsupported in x264_macroblock_write_cabac\n" );
225 static void x264_cabac_mb_intra4x4_pred_mode( x264_cabac_t *cb, int i_pred, int i_mode )
227 if( i_pred == i_mode )
229 /* b_prev_intra4x4_pred_mode */
230 x264_cabac_encode_decision( cb, 68, 1 );
234 /* b_prev_intra4x4_pred_mode */
235 x264_cabac_encode_decision( cb, 68, 0 );
236 if( i_mode > i_pred )
240 x264_cabac_encode_decision( cb, 69, (i_mode )&0x01 );
241 x264_cabac_encode_decision( cb, 69, (i_mode >> 1)&0x01 );
242 x264_cabac_encode_decision( cb, 69, (i_mode >> 2)&0x01 );
246 static void x264_cabac_mb_intra_chroma_pred_mode( x264_t *h, x264_cabac_t *cb )
248 const int i_mode = x264_mb_pred_mode8x8c_fix[ h->mb.i_chroma_pred_mode ];
251 /* No need to test for I4x4 or I_16x16 as cache_save handle that */
252 if( (h->mb.i_neighbour & MB_LEFT) && h->mb.chroma_pred_mode[h->mb.i_mb_xy - 1] != 0 )
256 if( (h->mb.i_neighbour & MB_TOP) && h->mb.chroma_pred_mode[h->mb.i_mb_top_xy] != 0 )
261 x264_cabac_encode_decision( cb, 64 + ctx, i_mode > 0 );
264 x264_cabac_encode_decision( cb, 64 + 3, i_mode > 1 );
267 x264_cabac_encode_decision( cb, 64 + 3, i_mode > 2 );
272 static void x264_cabac_mb_cbp_luma( x264_t *h, x264_cabac_t *cb )
274 /* TODO: clean up and optimize */
276 for( i8x8 = 0; i8x8 < 4; i8x8++ )
280 int x = block_idx_x[4*i8x8];
281 int y = block_idx_y[4*i8x8];
285 i_mba_xy = h->mb.i_mb_xy;
286 else if( h->mb.i_neighbour & MB_LEFT )
287 i_mba_xy = h->mb.i_mb_xy - 1;
290 i_mbb_xy = h->mb.i_mb_xy;
291 else if( h->mb.i_neighbour & MB_TOP )
292 i_mbb_xy = h->mb.i_mb_top_xy;
295 /* No need to test for PCM and SKIP */
298 const int i8x8a = block_idx_xy[(x-1)&0x03][y]/4;
299 if( ((h->mb.cbp[i_mba_xy] >> i8x8a)&0x01) == 0 )
307 const int i8x8b = block_idx_xy[x][(y-1)&0x03]/4;
308 if( ((h->mb.cbp[i_mbb_xy] >> i8x8b)&0x01) == 0 )
314 x264_cabac_encode_decision( cb, 73 + ctx, (h->mb.i_cbp_luma >> i8x8)&0x01 );
318 static void x264_cabac_mb_cbp_chroma( x264_t *h, x264_cabac_t *cb )
324 /* No need to test for SKIP/PCM */
325 if( h->mb.i_neighbour & MB_LEFT )
327 cbp_a = (h->mb.cbp[h->mb.i_mb_xy - 1] >> 4)&0x3;
330 if( h->mb.i_neighbour & MB_TOP )
332 cbp_b = (h->mb.cbp[h->mb.i_mb_top_xy] >> 4)&0x3;
336 if( cbp_a > 0 ) ctx++;
337 if( cbp_b > 0 ) ctx += 2;
338 if( h->mb.i_cbp_chroma == 0 )
340 x264_cabac_encode_decision( cb, 77 + ctx, 0 );
344 x264_cabac_encode_decision( cb, 77 + ctx, 1 );
347 if( cbp_a == 2 ) ctx++;
348 if( cbp_b == 2 ) ctx += 2;
349 x264_cabac_encode_decision( cb, 77 + ctx, h->mb.i_cbp_chroma > 1 );
353 /* TODO check it with != qp per mb */
354 static void x264_cabac_mb_qp_delta( x264_t *h, x264_cabac_t *cb )
356 int i_mbn_xy = h->mb.i_mb_prev_xy;
357 int i_dqp = h->mb.i_qp - h->mb.i_last_qp;
360 /* No need to test for PCM / SKIP */
361 if( h->mb.i_neighbour && h->mb.i_last_dqp != 0 &&
362 ( h->mb.type[i_mbn_xy] == I_16x16 || (h->mb.cbp[i_mbn_xy]&0x3f) ) )
369 int val = i_dqp <= 0 ? (-2*i_dqp) : (2*i_dqp - 1);
370 /* dqp is interpreted modulo 52 */
371 if( val >= 51 && val != 52 )
375 x264_cabac_encode_decision( cb, 60 + ctx, 1 );
382 x264_cabac_encode_decision( cb, 60 + ctx, 0 );
385 void x264_cabac_mb_skip( x264_t *h, int b_skip )
389 if( h->mb.i_mb_type_left >= 0 && !IS_SKIP( h->mb.i_mb_type_left ) )
393 if( h->mb.i_mb_type_top >= 0 && !IS_SKIP( h->mb.i_mb_type_top ) )
398 ctx += (h->sh.i_type == SLICE_TYPE_P) ? 11 : 24;
399 x264_cabac_encode_decision( &h->cabac, ctx, b_skip );
402 static inline void x264_cabac_mb_sub_p_partition( x264_cabac_t *cb, int i_sub )
404 if( i_sub == D_L0_8x8 )
406 x264_cabac_encode_decision( cb, 21, 1 );
408 else if( i_sub == D_L0_8x4 )
410 x264_cabac_encode_decision( cb, 21, 0 );
411 x264_cabac_encode_decision( cb, 22, 0 );
413 else if( i_sub == D_L0_4x8 )
415 x264_cabac_encode_decision( cb, 21, 0 );
416 x264_cabac_encode_decision( cb, 22, 1 );
417 x264_cabac_encode_decision( cb, 23, 1 );
419 else if( i_sub == D_L0_4x4 )
421 x264_cabac_encode_decision( cb, 21, 0 );
422 x264_cabac_encode_decision( cb, 22, 1 );
423 x264_cabac_encode_decision( cb, 23, 0 );
427 static inline void x264_cabac_mb_sub_b_partition( x264_cabac_t *cb, int i_sub )
429 #define WRITE_SUB_3(a,b,c) {\
430 x264_cabac_encode_decision( cb, 36, a );\
431 x264_cabac_encode_decision( cb, 37, b );\
432 x264_cabac_encode_decision( cb, 39, c );\
434 #define WRITE_SUB_5(a,b,c,d,e) {\
435 x264_cabac_encode_decision( cb, 36, a );\
436 x264_cabac_encode_decision( cb, 37, b );\
437 x264_cabac_encode_decision( cb, 38, c );\
438 x264_cabac_encode_decision( cb, 39, d );\
439 x264_cabac_encode_decision( cb, 39, e );\
441 #define WRITE_SUB_6(a,b,c,d,e,f) {\
442 WRITE_SUB_5(a,b,c,d,e)\
443 x264_cabac_encode_decision( cb, 39, f );\
449 x264_cabac_encode_decision( cb, 36, 0 );
451 case D_L0_8x8: WRITE_SUB_3(1,0,0); break;
452 case D_L1_8x8: WRITE_SUB_3(1,0,1); break;
453 case D_BI_8x8: WRITE_SUB_5(1,1,0,0,0); break;
454 case D_L0_8x4: WRITE_SUB_5(1,1,0,0,1); break;
455 case D_L0_4x8: WRITE_SUB_5(1,1,0,1,0); break;
456 case D_L1_8x4: WRITE_SUB_5(1,1,0,1,1); break;
457 case D_L1_4x8: WRITE_SUB_6(1,1,1,0,0,0); break;
458 case D_BI_8x4: WRITE_SUB_6(1,1,1,0,0,1); break;
459 case D_BI_4x8: WRITE_SUB_6(1,1,1,0,1,0); break;
460 case D_L0_4x4: WRITE_SUB_6(1,1,1,0,1,1); break;
461 case D_L1_4x4: WRITE_SUB_5(1,1,1,1,0); break;
462 case D_BI_4x4: WRITE_SUB_5(1,1,1,1,1); break;
466 static inline void x264_cabac_mb_transform_size( x264_t *h, x264_cabac_t *cb )
468 int ctx = 399 + h->mb.cache.i_neighbour_transform_size;
469 x264_cabac_encode_decision( cb, ctx, h->mb.b_transform_8x8 );
472 static inline void x264_cabac_mb_ref( x264_t *h, x264_cabac_t *cb, int i_list, int idx )
474 const int i8 = x264_scan8[idx];
475 const int i_refa = h->mb.cache.ref[i_list][i8 - 1];
476 const int i_refb = h->mb.cache.ref[i_list][i8 - 8];
477 int i_ref = h->mb.cache.ref[i_list][i8];
480 if( i_refa > 0 && !h->mb.cache.skip[i8 - 1])
482 if( i_refb > 0 && !h->mb.cache.skip[i8 - 8])
487 x264_cabac_encode_decision( cb, 54 + ctx, 1 );
495 x264_cabac_encode_decision( cb, 54 + ctx, 0 );
500 static inline void x264_cabac_mb_mvd_cpn( x264_t *h, x264_cabac_t *cb, int i_list, int idx, int l, int mvd )
502 const int amvd = abs( h->mb.cache.mvd[i_list][x264_scan8[idx] - 1][l] ) +
503 abs( h->mb.cache.mvd[i_list][x264_scan8[idx] - 8][l] );
504 const int i_abs = abs( mvd );
505 const int i_prefix = X264_MIN( i_abs, 9 );
506 const int ctxbase = (l == 0 ? 40 : 47);
518 for( i = 0; i < i_prefix; i++ )
520 x264_cabac_encode_decision( cb, ctxbase + ctx, 1 );
527 x264_cabac_encode_decision( cb, ctxbase + ctx, 0 );
529 x264_cabac_encode_ue_bypass( cb, 3, i_abs - 9 );
533 x264_cabac_encode_bypass( cb, mvd < 0 );
536 static inline void x264_cabac_mb_mvd( x264_t *h, x264_cabac_t *cb, int i_list, int idx, int width, int height )
542 x264_mb_predict_mv( h, i_list, idx, width, mvp );
543 mdx = h->mb.cache.mv[i_list][x264_scan8[idx]][0] - mvp[0];
544 mdy = h->mb.cache.mv[i_list][x264_scan8[idx]][1] - mvp[1];
547 x264_cabac_mb_mvd_cpn( h, cb, i_list, idx, 0, mdx );
548 x264_cabac_mb_mvd_cpn( h, cb, i_list, idx, 1, mdy );
551 x264_macroblock_cache_mvd( h, block_idx_x[idx], block_idx_y[idx], width, height, i_list, mdx, mdy );
554 static inline void x264_cabac_mb8x8_mvd( x264_t *h, x264_cabac_t *cb, int i_list, int i )
556 if( !x264_mb_partition_listX_table[i_list][ h->mb.i_sub_partition[i] ] )
559 switch( h->mb.i_sub_partition[i] )
564 x264_cabac_mb_mvd( h, cb, i_list, 4*i, 2, 2 );
569 x264_cabac_mb_mvd( h, cb, i_list, 4*i+0, 2, 1 );
570 x264_cabac_mb_mvd( h, cb, i_list, 4*i+2, 2, 1 );
575 x264_cabac_mb_mvd( h, cb, i_list, 4*i+0, 1, 2 );
576 x264_cabac_mb_mvd( h, cb, i_list, 4*i+1, 1, 2 );
581 x264_cabac_mb_mvd( h, cb, i_list, 4*i+0, 1, 1 );
582 x264_cabac_mb_mvd( h, cb, i_list, 4*i+1, 1, 1 );
583 x264_cabac_mb_mvd( h, cb, i_list, 4*i+2, 1, 1 );
584 x264_cabac_mb_mvd( h, cb, i_list, 4*i+3, 1, 1 );
589 static int x264_cabac_mb_cbf_ctxidxinc( x264_t *h, int i_cat, int i_idx )
597 if( i_cat == DCT_LUMA_DC )
599 if( h->mb.i_neighbour & MB_LEFT )
601 i_mba_xy = h->mb.i_mb_xy - 1;
602 if( h->mb.i_mb_type_left == I_16x16 )
603 i_nza = h->mb.cbp[i_mba_xy] & 0x100;
605 if( h->mb.i_neighbour & MB_TOP )
607 i_mbb_xy = h->mb.i_mb_top_xy;
608 if( h->mb.i_mb_type_top == I_16x16 )
609 i_nzb = h->mb.cbp[i_mbb_xy] & 0x100;
612 else if( i_cat == DCT_LUMA_AC || i_cat == DCT_LUMA_4x4 )
614 if( i_idx & ~10 ) // block_idx_x > 0
615 i_mba_xy = h->mb.i_mb_xy;
616 else if( h->mb.i_neighbour & MB_LEFT )
617 i_mba_xy = h->mb.i_mb_xy - 1;
619 if( i_idx & ~5 ) // block_idx_y > 0
620 i_mbb_xy = h->mb.i_mb_xy;
621 else if( h->mb.i_neighbour & MB_TOP )
622 i_mbb_xy = h->mb.i_mb_top_xy;
624 /* no need to test for skip/pcm */
626 i_nza = h->mb.cache.non_zero_count[x264_scan8[i_idx] - 1];
628 i_nzb = h->mb.cache.non_zero_count[x264_scan8[i_idx] - 8];
630 else if( i_cat == DCT_CHROMA_DC )
632 /* no need to test skip/pcm */
633 if( h->mb.i_neighbour & MB_LEFT )
635 i_mba_xy = h->mb.i_mb_xy - 1;
636 i_nza = h->mb.cbp[i_mba_xy] & (0x200 << i_idx);
638 if( h->mb.i_neighbour & MB_TOP )
640 i_mbb_xy = h->mb.i_mb_top_xy;
641 i_nzb = h->mb.cbp[i_mbb_xy] & (0x200 << i_idx);
644 else if( i_cat == DCT_CHROMA_AC )
647 i_mba_xy = h->mb.i_mb_xy;
648 else if( h->mb.i_neighbour & MB_LEFT )
649 i_mba_xy = h->mb.i_mb_xy - 1;
652 i_mbb_xy = h->mb.i_mb_xy;
653 else if( h->mb.i_neighbour & MB_TOP )
654 i_mbb_xy = h->mb.i_mb_top_xy;
656 /* no need to test skip/pcm */
658 i_nza = h->mb.cache.non_zero_count[x264_scan8[16+i_idx] - 1];
660 i_nzb = h->mb.cache.non_zero_count[x264_scan8[16+i_idx] - 8];
663 if( IS_INTRA( h->mb.i_type ) )
680 static const int significant_coeff_flag_offset[2][6] = {
681 { 105, 120, 134, 149, 152, 402 },
682 { 277, 292, 306, 321, 324, 436 }
684 static const int last_coeff_flag_offset[2][6] = {
685 { 166, 181, 195, 210, 213, 417 },
686 { 338, 353, 367, 382, 385, 451 }
688 static const int coeff_abs_level_m1_offset[6] =
689 { 227, 237, 247, 257, 266, 426 };
690 static const int significant_coeff_flag_offset_8x8[2][63] =
692 0, 1, 2, 3, 4, 5, 5, 4, 4, 3, 3, 4, 4, 4, 5, 5,
693 4, 4, 4, 4, 3, 3, 6, 7, 7, 7, 8, 9,10, 9, 8, 7,
694 7, 6,11,12,13,11, 6, 7, 8, 9,14,10, 9, 8, 6,11,
695 12,13,11, 6, 9,14,10, 9,11,12,13,11,14,10,12
697 0, 1, 1, 2, 2, 3, 3, 4, 5, 6, 7, 7, 7, 8, 4, 5,
698 6, 9,10,10, 8,11,12,11, 9, 9,10,10, 8,11,12,11,
699 9, 9,10,10, 8,11,12,11, 9, 9,10,10, 8,13,13, 9,
700 9,10,10, 8,13,13, 9, 9,10,10,14,14,14,14,14
702 static const int last_coeff_flag_offset_8x8[63] = {
703 0, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1,
704 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2,
705 3, 3, 3, 3, 3, 3, 3, 3, 4, 4, 4, 4, 4, 4, 4, 4,
706 5, 5, 5, 5, 6, 6, 6, 6, 7, 7, 7, 7, 8, 8, 8
708 static const int identity[16] =
709 { 0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15 };
711 static void block_residual_write_cabac( x264_t *h, x264_cabac_t *cb, int i_ctxBlockCat, int i_idx, int *l, int i_count )
713 const int i_ctx_sig = significant_coeff_flag_offset[h->mb.b_interlaced][i_ctxBlockCat];
714 const int i_ctx_last = last_coeff_flag_offset[h->mb.b_interlaced][i_ctxBlockCat];
715 const int i_ctx_level = coeff_abs_level_m1_offset[i_ctxBlockCat];
717 int i_coeff_abs_m1[64];
718 int i_coeff_sign[64];
724 int i_abslevelgt1 = 0;
728 const int *significant_coeff_flag_offset;
729 const int *last_coeff_flag_offset;
731 /* i_ctxBlockCat: 0-> DC 16x16 i_idx = 0
732 * 1-> AC 16x16 i_idx = luma4x4idx
733 * 2-> Luma4x4 i_idx = luma4x4idx
734 * 3-> DC Chroma i_idx = iCbCr
735 * 4-> AC Chroma i_idx = 4 * iCbCr + chroma4x4idx
736 * 5-> Luma8x8 i_idx = luma8x8idx
739 for( i = 0; i < i_count; i++ )
743 i_coeff_abs_m1[i_coeff] = abs( l[i] ) - 1;
744 i_coeff_sign[i_coeff] = ( l[i] < 0 );
753 /* coded block flag */
754 x264_cabac_encode_decision( cb, 85 + x264_cabac_mb_cbf_ctxidxinc( h, i_ctxBlockCat, i_idx ), i_coeff != 0 );
759 significant_coeff_flag_offset = (i_ctxBlockCat == DCT_LUMA_8x8)
760 ? significant_coeff_flag_offset_8x8[h->mb.b_interlaced]
762 last_coeff_flag_offset = (i_ctxBlockCat == DCT_LUMA_8x8)
763 ? last_coeff_flag_offset_8x8 : identity;
765 i_sigmap_size = X264_MIN( i_last+1, i_count-1 );
766 for( i = 0; i < i_sigmap_size; i++ )
768 x264_cabac_encode_decision( cb, i_ctx_sig + significant_coeff_flag_offset[i], l[i] != 0 );
770 x264_cabac_encode_decision( cb, i_ctx_last + last_coeff_flag_offset[i], i == i_last );
773 for( i = i_coeff - 1; i >= 0; i-- )
775 /* write coeff_abs - 1 */
776 const int i_prefix = X264_MIN( i_coeff_abs_m1[i], 14 );
777 const int i_ctxIdxInc = (i_abslevelgt1 ? 0 : X264_MIN( 4, i_abslevel1 + 1 )) + i_ctx_level;
778 x264_cabac_encode_decision( cb, i_ctxIdxInc, i_prefix != 0 );
782 const int i_ctxIdxInc = 5 + X264_MIN( 4, i_abslevelgt1 ) + i_ctx_level;
784 cb->f8_bits_encoded += cabac_prefix_size[i_prefix][cb->state[i_ctxIdxInc]];
785 cb->state[i_ctxIdxInc] = cabac_prefix_transition[i_prefix][cb->state[i_ctxIdxInc]];
788 for( j = 0; j < i_prefix - 1; j++ )
789 x264_cabac_encode_decision( cb, i_ctxIdxInc, 1 );
791 x264_cabac_encode_decision( cb, i_ctxIdxInc, 0 );
794 x264_cabac_encode_ue_bypass( cb, 0, i_coeff_abs_m1[i] - 14 );
805 x264_cabac_encode_bypass( cb, i_coeff_sign[i] );
811 void x264_macroblock_write_cabac( x264_t *h, x264_cabac_t *cb )
813 const int i_mb_type = h->mb.i_type;
818 const int i_mb_pos_start = x264_cabac_pos( cb );
822 /* Write the MB type */
823 x264_cabac_mb_type( h, cb );
825 /* PCM special block type UNTESTED */
826 if( i_mb_type == I_PCM )
829 cb->f8_bits_encoded += (384*8) << 8;
831 if( cb->p + 385 >= cb->p_end )
832 return; //FIXME throw an error
834 for( i = 0; i < 16; i++ )
836 memcpy( cb->p, h->fenc->plane[0] + i*h->mb.pic.i_stride[0], 16 );
840 for( i = 0; i < 8; i++ )
842 memcpy( cb->p, h->fenc->plane[1] + i*h->mb.pic.i_stride[1], 8 );
846 for( i = 0; i < 8; i++ )
848 memcpy( cb->p, h->fenc->plane[2] + i*h->mb.pic.i_stride[2], 8 );
851 x264_cabac_encode_init( cb, cb->p, cb->p_end );
856 if( IS_INTRA( i_mb_type ) )
858 if( h->pps->b_transform_8x8_mode && i_mb_type != I_16x16 )
859 x264_cabac_mb_transform_size( h, cb );
861 if( i_mb_type != I_16x16 )
863 int di = (i_mb_type == I_8x8) ? 4 : 1;
864 for( i = 0; i < 16; i += di )
866 const int i_pred = x264_mb_predict_intra4x4_mode( h, i );
867 const int i_mode = x264_mb_pred_mode4x4_fix( h->mb.cache.intra4x4_pred_mode[x264_scan8[i]] );
868 x264_cabac_mb_intra4x4_pred_mode( cb, i_pred, i_mode );
872 x264_cabac_mb_intra_chroma_pred_mode( h, cb );
874 else if( i_mb_type == P_L0 )
876 if( h->mb.i_partition == D_16x16 )
878 if( h->mb.pic.i_fref[0] > 1 )
880 x264_cabac_mb_ref( h, cb, 0, 0 );
882 x264_cabac_mb_mvd( h, cb, 0, 0, 4, 4 );
884 else if( h->mb.i_partition == D_16x8 )
886 if( h->mb.pic.i_fref[0] > 1 )
888 x264_cabac_mb_ref( h, cb, 0, 0 );
889 x264_cabac_mb_ref( h, cb, 0, 8 );
891 x264_cabac_mb_mvd( h, cb, 0, 0, 4, 2 );
892 x264_cabac_mb_mvd( h, cb, 0, 8, 4, 2 );
894 else if( h->mb.i_partition == D_8x16 )
896 if( h->mb.pic.i_fref[0] > 1 )
898 x264_cabac_mb_ref( h, cb, 0, 0 );
899 x264_cabac_mb_ref( h, cb, 0, 4 );
901 x264_cabac_mb_mvd( h, cb, 0, 0, 2, 4 );
902 x264_cabac_mb_mvd( h, cb, 0, 4, 2, 4 );
905 else if( i_mb_type == P_8x8 )
908 x264_cabac_mb_sub_p_partition( cb, h->mb.i_sub_partition[0] );
909 x264_cabac_mb_sub_p_partition( cb, h->mb.i_sub_partition[1] );
910 x264_cabac_mb_sub_p_partition( cb, h->mb.i_sub_partition[2] );
911 x264_cabac_mb_sub_p_partition( cb, h->mb.i_sub_partition[3] );
914 if( h->mb.pic.i_fref[0] > 1 )
916 x264_cabac_mb_ref( h, cb, 0, 0 );
917 x264_cabac_mb_ref( h, cb, 0, 4 );
918 x264_cabac_mb_ref( h, cb, 0, 8 );
919 x264_cabac_mb_ref( h, cb, 0, 12 );
922 for( i = 0; i < 4; i++ )
923 x264_cabac_mb8x8_mvd( h, cb, 0, i );
925 else if( i_mb_type == B_8x8 )
928 x264_cabac_mb_sub_b_partition( cb, h->mb.i_sub_partition[0] );
929 x264_cabac_mb_sub_b_partition( cb, h->mb.i_sub_partition[1] );
930 x264_cabac_mb_sub_b_partition( cb, h->mb.i_sub_partition[2] );
931 x264_cabac_mb_sub_b_partition( cb, h->mb.i_sub_partition[3] );
934 for( i_list = 0; i_list < 2; i_list++ )
936 if( ( i_list ? h->mb.pic.i_fref[1] : h->mb.pic.i_fref[0] ) == 1 )
938 for( i = 0; i < 4; i++ )
939 if( x264_mb_partition_listX_table[i_list][ h->mb.i_sub_partition[i] ] )
940 x264_cabac_mb_ref( h, cb, i_list, 4*i );
943 for( i = 0; i < 4; i++ )
944 x264_cabac_mb8x8_mvd( h, cb, 0, i );
945 for( i = 0; i < 4; i++ )
946 x264_cabac_mb8x8_mvd( h, cb, 1, i );
948 else if( i_mb_type != B_DIRECT )
953 /* init ref list utilisations */
954 for( i = 0; i < 2; i++ )
956 b_list[0][i] = x264_mb_type_list0_table[i_mb_type][i];
957 b_list[1][i] = x264_mb_type_list1_table[i_mb_type][i];
960 for( i_list = 0; i_list < 2; i_list++ )
962 const int i_ref_max = i_list == 0 ? h->mb.pic.i_fref[0] : h->mb.pic.i_fref[1];
966 if( h->mb.i_partition == D_16x16 )
968 if( b_list[i_list][0] ) x264_cabac_mb_ref( h, cb, i_list, 0 );
970 else if( h->mb.i_partition == D_16x8 )
972 if( b_list[i_list][0] ) x264_cabac_mb_ref( h, cb, i_list, 0 );
973 if( b_list[i_list][1] ) x264_cabac_mb_ref( h, cb, i_list, 8 );
975 else if( h->mb.i_partition == D_8x16 )
977 if( b_list[i_list][0] ) x264_cabac_mb_ref( h, cb, i_list, 0 );
978 if( b_list[i_list][1] ) x264_cabac_mb_ref( h, cb, i_list, 4 );
982 for( i_list = 0; i_list < 2; i_list++ )
984 if( h->mb.i_partition == D_16x16 )
986 if( b_list[i_list][0] ) x264_cabac_mb_mvd( h, cb, i_list, 0, 4, 4 );
988 else if( h->mb.i_partition == D_16x8 )
990 if( b_list[i_list][0] ) x264_cabac_mb_mvd( h, cb, i_list, 0, 4, 2 );
991 if( b_list[i_list][1] ) x264_cabac_mb_mvd( h, cb, i_list, 8, 4, 2 );
993 else if( h->mb.i_partition == D_8x16 )
995 if( b_list[i_list][0] ) x264_cabac_mb_mvd( h, cb, i_list, 0, 2, 4 );
996 if( b_list[i_list][1] ) x264_cabac_mb_mvd( h, cb, i_list, 4, 2, 4 );
1002 i_mb_pos_tex = x264_cabac_pos( cb );
1003 h->stat.frame.i_hdr_bits += i_mb_pos_tex - i_mb_pos_start;
1006 if( i_mb_type != I_16x16 )
1008 x264_cabac_mb_cbp_luma( h, cb );
1009 x264_cabac_mb_cbp_chroma( h, cb );
1012 if( h->mb.cache.b_transform_8x8_allowed && h->mb.i_cbp_luma && !IS_INTRA(i_mb_type) )
1014 x264_cabac_mb_transform_size( h, cb );
1017 if( h->mb.i_cbp_luma > 0 || h->mb.i_cbp_chroma > 0 || i_mb_type == I_16x16 )
1019 x264_cabac_mb_qp_delta( h, cb );
1021 /* write residual */
1022 if( i_mb_type == I_16x16 )
1025 block_residual_write_cabac( h, cb, DCT_LUMA_DC, 0, h->dct.luma16x16_dc, 16 );
1028 if( h->mb.i_cbp_luma != 0 )
1029 for( i = 0; i < 16; i++ )
1030 block_residual_write_cabac( h, cb, DCT_LUMA_AC, i, h->dct.block[i].residual_ac, 15 );
1032 else if( h->mb.b_transform_8x8 )
1034 for( i = 0; i < 4; i++ )
1035 if( h->mb.i_cbp_luma & ( 1 << i ) )
1036 block_residual_write_cabac( h, cb, DCT_LUMA_8x8, i, h->dct.luma8x8[i], 64 );
1040 for( i = 0; i < 16; i++ )
1041 if( h->mb.i_cbp_luma & ( 1 << ( i / 4 ) ) )
1042 block_residual_write_cabac( h, cb, DCT_LUMA_4x4, i, h->dct.block[i].luma4x4, 16 );
1045 if( h->mb.i_cbp_chroma &0x03 ) /* Chroma DC residual present */
1047 block_residual_write_cabac( h, cb, DCT_CHROMA_DC, 0, h->dct.chroma_dc[0], 4 );
1048 block_residual_write_cabac( h, cb, DCT_CHROMA_DC, 1, h->dct.chroma_dc[1], 4 );
1050 if( h->mb.i_cbp_chroma&0x02 ) /* Chroma AC residual present */
1052 for( i = 0; i < 8; i++ )
1053 block_residual_write_cabac( h, cb, DCT_CHROMA_AC, i, h->dct.block[16+i].residual_ac, 15 );
1058 if( IS_INTRA( i_mb_type ) )
1059 h->stat.frame.i_itex_bits += x264_cabac_pos( cb ) - i_mb_pos_tex;
1061 h->stat.frame.i_ptex_bits += x264_cabac_pos( cb ) - i_mb_pos_tex;
1066 /*****************************************************************************
1067 * RD only; doesn't generate a valid bitstream
1068 * doesn't write cbp or chroma dc (I don't know how much this matters)
1069 * works on all partition sizes except 16x16
1070 * for sub8x8, call once per 8x8 block
1071 *****************************************************************************/
1072 void x264_partition_size_cabac( x264_t *h, x264_cabac_t *cb, int i8, int i_pixel )
1074 const int i_mb_type = h->mb.i_type;
1077 if( i_mb_type == P_8x8 )
1079 x264_cabac_mb_sub_p_partition( cb, h->mb.i_sub_partition[i8] );
1080 if( h->mb.pic.i_fref[0] > 1 )
1081 x264_cabac_mb_ref( h, cb, 0, 4*i8 );
1082 x264_cabac_mb8x8_mvd( h, cb, 0, i8 );
1084 else if( i_mb_type == P_L0 )
1086 if( h->mb.pic.i_fref[0] > 1 )
1087 x264_cabac_mb_ref( h, cb, 0, 4*i8 );
1088 if( h->mb.i_partition == D_16x8 )
1089 x264_cabac_mb_mvd( h, cb, 0, 4*i8, 4, 2 );
1091 x264_cabac_mb_mvd( h, cb, 0, 4*i8, 2, 4 );
1093 else if( i_mb_type == B_8x8 )
1095 x264_cabac_mb_sub_b_partition( cb, h->mb.i_sub_partition[i8] );
1097 if( h->mb.pic.i_fref[0] > 1
1098 && x264_mb_partition_listX_table[0][ h->mb.i_sub_partition[i8] ] )
1099 x264_cabac_mb_ref( h, cb, 0, 4*i8 );
1100 if( h->mb.pic.i_fref[1] > 1
1101 && x264_mb_partition_listX_table[1][ h->mb.i_sub_partition[i8] ] )
1102 x264_cabac_mb_ref( h, cb, 1, 4*i8 );
1104 x264_cabac_mb8x8_mvd( h, cb, 0, i8 );
1105 x264_cabac_mb8x8_mvd( h, cb, 1, i8 );
1109 x264_log(h, X264_LOG_ERROR, "invalid/unhandled mb_type\n" );
1113 for( j = (i_pixel < PIXEL_8x8); j >= 0; j-- )
1115 if( h->mb.i_cbp_luma & (1 << i8) )
1117 if( h->mb.b_transform_8x8 )
1118 block_residual_write_cabac( h, cb, DCT_LUMA_8x8, i8, h->dct.luma8x8[i8], 64 );
1122 for( i4 = 0; i4 < 4; i4++ )
1123 block_residual_write_cabac( h, cb, DCT_LUMA_4x4, i4+i8*4, h->dct.block[i4+i8*4].luma4x4, 16 );
1127 block_residual_write_cabac( h, cb, DCT_CHROMA_AC, i8, h->dct.block[16+i8 ].residual_ac, 15 );
1128 block_residual_write_cabac( h, cb, DCT_CHROMA_AC, i8+4, h->dct.block[16+i8+4].residual_ac, 15 );
1130 i8 += x264_pixel_size[i_pixel].h >> 3;
1134 static void x264_partition_i8x8_size_cabac( x264_t *h, x264_cabac_t *cb, int i8, int i_mode )
1136 const int i_pred = x264_mb_predict_intra4x4_mode( h, 4*i8 );
1137 i_mode = x264_mb_pred_mode4x4_fix( i_mode );
1138 x264_cabac_mb_intra4x4_pred_mode( cb, i_pred, i_mode );
1139 block_residual_write_cabac( h, cb, DCT_LUMA_8x8, 4*i8, h->dct.luma8x8[i8], 64 );
1142 static void x264_partition_i4x4_size_cabac( x264_t *h, x264_cabac_t *cb, int i4, int i_mode )
1144 const int i_pred = x264_mb_predict_intra4x4_mode( h, i4 );
1145 i_mode = x264_mb_pred_mode4x4_fix( i_mode );
1146 x264_cabac_mb_intra4x4_pred_mode( cb, i_pred, i_mode );
1147 block_residual_write_cabac( h, cb, DCT_LUMA_4x4, i4, h->dct.block[i4].luma4x4, 16 );
1150 static void x264_i8x8_chroma_size_cabac( x264_t *h, x264_cabac_t *cb )
1152 x264_cabac_mb_intra_chroma_pred_mode( h, cb );
1153 if( h->mb.i_cbp_chroma > 0 )
1155 block_residual_write_cabac( h, cb, DCT_CHROMA_DC, 0, h->dct.chroma_dc[0], 4 );
1156 block_residual_write_cabac( h, cb, DCT_CHROMA_DC, 1, h->dct.chroma_dc[1], 4 );
1158 if( h->mb.i_cbp_chroma == 2 )
1161 for( i = 0; i < 8; i++ )
1162 block_residual_write_cabac( h, cb, DCT_CHROMA_AC, i, h->dct.block[16+i].residual_ac, 15 );