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_flush( h, cb );
58 int i_pred = x264_mb_pred_mode16x16_fix[h->mb.i_intra16x16_pred_mode];
60 x264_cabac_encode_decision( cb, ctx0, 1 );
61 x264_cabac_encode_terminal( cb );
63 x264_cabac_encode_decision( cb, ctx1, !!h->mb.i_cbp_luma );
64 if( h->mb.i_cbp_chroma == 0 )
66 x264_cabac_encode_decision( cb, ctx2, 0 );
70 x264_cabac_encode_decision( cb, ctx2, 1 );
71 x264_cabac_encode_decision( cb, ctx3, h->mb.i_cbp_chroma != 1 );
73 x264_cabac_encode_decision( cb, ctx4, i_pred>>1 );
74 x264_cabac_encode_decision( cb, ctx5, i_pred&1 );
78 static void x264_cabac_mb_type( x264_t *h, x264_cabac_t *cb )
80 const int i_mb_type = h->mb.i_type;
83 (!(h->mb.i_mb_y & 1) || IS_SKIP(h->mb.type[h->mb.i_mb_xy - h->mb.i_mb_stride])) )
85 x264_cabac_encode_decision( cb, 70 + h->mb.cache.i_neighbour_interlaced, h->mb.b_interlaced );
88 if( h->sh.i_type == SLICE_TYPE_I )
91 if( h->mb.i_mb_type_left >= 0 && h->mb.i_mb_type_left != I_4x4 )
95 if( h->mb.i_mb_type_top >= 0 && h->mb.i_mb_type_top != I_4x4 )
100 x264_cabac_mb_type_intra( h, cb, i_mb_type, 3+ctx, 3+3, 3+4, 3+5, 3+6, 3+7 );
102 else if( h->sh.i_type == SLICE_TYPE_P )
104 /* prefix: 14, suffix: 17 */
105 if( i_mb_type == P_L0 )
107 if( h->mb.i_partition == D_16x16 )
109 x264_cabac_encode_decision( cb, 14, 0 );
110 x264_cabac_encode_decision( cb, 15, 0 );
111 x264_cabac_encode_decision( cb, 16, 0 );
113 else if( h->mb.i_partition == D_16x8 )
115 x264_cabac_encode_decision( cb, 14, 0 );
116 x264_cabac_encode_decision( cb, 15, 1 );
117 x264_cabac_encode_decision( cb, 17, 1 );
119 else if( h->mb.i_partition == D_8x16 )
121 x264_cabac_encode_decision( cb, 14, 0 );
122 x264_cabac_encode_decision( cb, 15, 1 );
123 x264_cabac_encode_decision( cb, 17, 0 );
126 else if( i_mb_type == P_8x8 )
128 x264_cabac_encode_decision( cb, 14, 0 );
129 x264_cabac_encode_decision( cb, 15, 0 );
130 x264_cabac_encode_decision( cb, 16, 1 );
135 x264_cabac_encode_decision( cb, 14, 1 );
138 x264_cabac_mb_type_intra( h, cb, i_mb_type, 17+0, 17+1, 17+2, 17+2, 17+3, 17+3 );
141 else if( h->sh.i_type == SLICE_TYPE_B )
144 if( h->mb.i_mb_type_left >= 0 && h->mb.i_mb_type_left != B_SKIP && h->mb.i_mb_type_left != B_DIRECT )
148 if( h->mb.i_mb_type_top >= 0 && h->mb.i_mb_type_top != B_SKIP && h->mb.i_mb_type_top != B_DIRECT )
153 if( i_mb_type == B_DIRECT )
155 x264_cabac_encode_decision( cb, 27+ctx, 0 );
157 else if( i_mb_type == B_8x8 )
159 x264_cabac_encode_decision( cb, 27+ctx, 1 );
160 x264_cabac_encode_decision( cb, 27+3, 1 );
161 x264_cabac_encode_decision( cb, 27+4, 1 );
163 x264_cabac_encode_decision( cb, 27+5, 1 );
164 x264_cabac_encode_decision( cb, 27+5, 1 );
165 x264_cabac_encode_decision( cb, 27+5, 1 );
167 else if( IS_INTRA( i_mb_type ) )
170 x264_cabac_encode_decision( cb, 27+ctx, 1 );
171 x264_cabac_encode_decision( cb, 27+3, 1 );
172 x264_cabac_encode_decision( cb, 27+4, 1 );
174 x264_cabac_encode_decision( cb, 27+5, 1 );
175 x264_cabac_encode_decision( cb, 27+5, 0 );
176 x264_cabac_encode_decision( cb, 27+5, 1 );
179 x264_cabac_mb_type_intra( h, cb, i_mb_type, 32+0, 32+1, 32+2, 32+2, 32+3, 32+3 );
183 static const int i_mb_len[9*3] =
195 static const int i_mb_bits[9*3][7] =
197 { 1,1,0,0,0,1 }, { 1,1,0,0,1,0, }, { 1,0,0 }, /* L0 L0 */
198 { 1,1,0,1,0,1 }, { 1,1,0,1,1,0 }, {0}, /* L0 L1 */
199 { 1,1,1,0,0,0,0 }, { 1,1,1,0,0,0,1 }, {0}, /* L0 BI */
200 { 1,1,0,1,1,1 }, { 1,1,1,1,1,0 }, {0}, /* L1 L0 */
201 { 1,1,0,0,1,1 }, { 1,1,0,1,0,0 }, { 1,0,1 }, /* L1 L1 */
202 { 1,1,1,0,0,1,0 }, { 1,1,1,0,0,1,1 }, {0}, /* L1 BI */
203 { 1,1,1,0,1,0,0 }, { 1,1,1,0,1,0,1 }, {0}, /* BI L0 */
204 { 1,1,1,0,1,1,0 }, { 1,1,1,0,1,1,1 }, {0}, /* BI L1 */
205 { 1,1,1,1,0,0,0 }, { 1,1,1,1,0,0,1 }, { 1,1,0,0,0,0 }, /* BI BI */
208 const int idx = (i_mb_type - B_L0_L0) * 3 + (h->mb.i_partition - D_16x8);
211 x264_cabac_encode_decision( cb, 27+ctx, i_mb_bits[idx][0] );
212 x264_cabac_encode_decision( cb, 27+3, i_mb_bits[idx][1] );
213 x264_cabac_encode_decision( cb, 27+5-i_mb_bits[idx][1], i_mb_bits[idx][2] );
214 for( i = 3; i < i_mb_len[idx]; i++ )
215 x264_cabac_encode_decision( cb, 27+5, i_mb_bits[idx][i] );
220 x264_log(h, X264_LOG_ERROR, "unknown SLICE_TYPE unsupported in x264_macroblock_write_cabac\n" );
224 static void x264_cabac_mb_intra4x4_pred_mode( x264_cabac_t *cb, int i_pred, int i_mode )
226 if( i_pred == i_mode )
228 /* b_prev_intra4x4_pred_mode */
229 x264_cabac_encode_decision( cb, 68, 1 );
233 /* b_prev_intra4x4_pred_mode */
234 x264_cabac_encode_decision( cb, 68, 0 );
235 if( i_mode > i_pred )
239 x264_cabac_encode_decision( cb, 69, (i_mode )&0x01 );
240 x264_cabac_encode_decision( cb, 69, (i_mode >> 1)&0x01 );
241 x264_cabac_encode_decision( cb, 69, (i_mode >> 2)&0x01 );
245 static void x264_cabac_mb_intra_chroma_pred_mode( x264_t *h, x264_cabac_t *cb )
247 const int i_mode = x264_mb_pred_mode8x8c_fix[ h->mb.i_chroma_pred_mode ];
250 /* No need to test for I4x4 or I_16x16 as cache_save handle that */
251 if( (h->mb.i_neighbour & MB_LEFT) && h->mb.chroma_pred_mode[h->mb.i_mb_xy - 1] != 0 )
255 if( (h->mb.i_neighbour & MB_TOP) && h->mb.chroma_pred_mode[h->mb.i_mb_top_xy] != 0 )
260 x264_cabac_encode_decision( cb, 64 + ctx, i_mode > 0 );
263 x264_cabac_encode_decision( cb, 64 + 3, i_mode > 1 );
266 x264_cabac_encode_decision( cb, 64 + 3, i_mode > 2 );
271 static void x264_cabac_mb_cbp_luma( x264_t *h, x264_cabac_t *cb )
273 /* TODO: clean up and optimize */
275 for( i8x8 = 0; i8x8 < 4; i8x8++ )
279 int x = block_idx_x[4*i8x8];
280 int y = block_idx_y[4*i8x8];
284 i_mba_xy = h->mb.i_mb_xy;
285 else if( h->mb.i_neighbour & MB_LEFT )
286 i_mba_xy = h->mb.i_mb_xy - 1;
289 i_mbb_xy = h->mb.i_mb_xy;
290 else if( h->mb.i_neighbour & MB_TOP )
291 i_mbb_xy = h->mb.i_mb_top_xy;
294 /* No need to test for PCM and SKIP */
297 const int i8x8a = block_idx_xy[(x-1)&0x03][y]/4;
298 if( ((h->mb.cbp[i_mba_xy] >> i8x8a)&0x01) == 0 )
306 const int i8x8b = block_idx_xy[x][(y-1)&0x03]/4;
307 if( ((h->mb.cbp[i_mbb_xy] >> i8x8b)&0x01) == 0 )
313 x264_cabac_encode_decision( cb, 73 + ctx, (h->mb.i_cbp_luma >> i8x8)&0x01 );
317 static void x264_cabac_mb_cbp_chroma( x264_t *h, x264_cabac_t *cb )
323 /* No need to test for SKIP/PCM */
324 if( h->mb.i_neighbour & MB_LEFT )
326 cbp_a = (h->mb.cbp[h->mb.i_mb_xy - 1] >> 4)&0x3;
329 if( h->mb.i_neighbour & MB_TOP )
331 cbp_b = (h->mb.cbp[h->mb.i_mb_top_xy] >> 4)&0x3;
335 if( cbp_a > 0 ) ctx++;
336 if( cbp_b > 0 ) ctx += 2;
337 if( h->mb.i_cbp_chroma == 0 )
339 x264_cabac_encode_decision( cb, 77 + ctx, 0 );
343 x264_cabac_encode_decision( cb, 77 + ctx, 1 );
346 if( cbp_a == 2 ) ctx++;
347 if( cbp_b == 2 ) ctx += 2;
348 x264_cabac_encode_decision( cb, 77 + ctx, h->mb.i_cbp_chroma > 1 );
352 /* TODO check it with != qp per mb */
353 static void x264_cabac_mb_qp_delta( x264_t *h, x264_cabac_t *cb )
355 int i_mbn_xy = h->mb.i_mb_prev_xy;
356 int i_dqp = h->mb.i_qp - h->mb.i_last_qp;
359 /* No need to test for PCM / SKIP */
360 if( h->mb.i_last_dqp &&
361 ( h->mb.type[i_mbn_xy] == I_16x16 || (h->mb.cbp[i_mbn_xy]&0x3f) ) )
368 int val = i_dqp <= 0 ? (-2*i_dqp) : (2*i_dqp - 1);
369 /* dqp is interpreted modulo 52 */
370 if( val >= 51 && val != 52 )
374 x264_cabac_encode_decision( cb, 60 + ctx, 1 );
381 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 );
403 static inline void x264_cabac_mb_sub_p_partition( x264_cabac_t *cb, int i_sub )
405 if( i_sub == D_L0_8x8 )
407 x264_cabac_encode_decision( cb, 21, 1 );
409 else if( i_sub == D_L0_8x4 )
411 x264_cabac_encode_decision( cb, 21, 0 );
412 x264_cabac_encode_decision( cb, 22, 0 );
414 else if( i_sub == D_L0_4x8 )
416 x264_cabac_encode_decision( cb, 21, 0 );
417 x264_cabac_encode_decision( cb, 22, 1 );
418 x264_cabac_encode_decision( cb, 23, 1 );
420 else if( i_sub == D_L0_4x4 )
422 x264_cabac_encode_decision( cb, 21, 0 );
423 x264_cabac_encode_decision( cb, 22, 1 );
424 x264_cabac_encode_decision( cb, 23, 0 );
428 static inline void x264_cabac_mb_sub_b_partition( x264_cabac_t *cb, int i_sub )
430 #define WRITE_SUB_3(a,b,c) {\
431 x264_cabac_encode_decision( cb, 36, a );\
432 x264_cabac_encode_decision( cb, 37, b );\
433 x264_cabac_encode_decision( cb, 39, c );\
435 #define WRITE_SUB_5(a,b,c,d,e) {\
436 x264_cabac_encode_decision( cb, 36, a );\
437 x264_cabac_encode_decision( cb, 37, b );\
438 x264_cabac_encode_decision( cb, 38, c );\
439 x264_cabac_encode_decision( cb, 39, d );\
440 x264_cabac_encode_decision( cb, 39, e );\
442 #define WRITE_SUB_6(a,b,c,d,e,f) {\
443 WRITE_SUB_5(a,b,c,d,e)\
444 x264_cabac_encode_decision( cb, 39, f );\
450 x264_cabac_encode_decision( cb, 36, 0 );
452 case D_L0_8x8: WRITE_SUB_3(1,0,0); break;
453 case D_L1_8x8: WRITE_SUB_3(1,0,1); break;
454 case D_BI_8x8: WRITE_SUB_5(1,1,0,0,0); break;
455 case D_L0_8x4: WRITE_SUB_5(1,1,0,0,1); break;
456 case D_L0_4x8: WRITE_SUB_5(1,1,0,1,0); break;
457 case D_L1_8x4: WRITE_SUB_5(1,1,0,1,1); break;
458 case D_L1_4x8: WRITE_SUB_6(1,1,1,0,0,0); break;
459 case D_BI_8x4: WRITE_SUB_6(1,1,1,0,0,1); break;
460 case D_BI_4x8: WRITE_SUB_6(1,1,1,0,1,0); break;
461 case D_L0_4x4: WRITE_SUB_6(1,1,1,0,1,1); break;
462 case D_L1_4x4: WRITE_SUB_5(1,1,1,1,0); break;
463 case D_BI_4x4: WRITE_SUB_5(1,1,1,1,1); break;
467 static inline void x264_cabac_mb_transform_size( x264_t *h, x264_cabac_t *cb )
469 int ctx = 399 + h->mb.cache.i_neighbour_transform_size;
470 x264_cabac_encode_decision( cb, ctx, h->mb.b_transform_8x8 );
473 static inline void x264_cabac_mb_ref( x264_t *h, x264_cabac_t *cb, int i_list, int idx )
475 const int i8 = x264_scan8[idx];
476 const int i_refa = h->mb.cache.ref[i_list][i8 - 1];
477 const int i_refb = h->mb.cache.ref[i_list][i8 - 8];
478 int i_ref = h->mb.cache.ref[i_list][i8];
481 if( i_refa > 0 && !h->mb.cache.skip[i8 - 1])
483 if( i_refb > 0 && !h->mb.cache.skip[i8 - 8])
488 x264_cabac_encode_decision( cb, 54 + ctx, 1 );
496 x264_cabac_encode_decision( cb, 54 + ctx, 0 );
501 static inline void x264_cabac_mb_mvd_cpn( x264_t *h, x264_cabac_t *cb, int i_list, int idx, int l, int mvd )
503 const int amvd = abs( h->mb.cache.mvd[i_list][x264_scan8[idx] - 1][l] ) +
504 abs( h->mb.cache.mvd[i_list][x264_scan8[idx] - 8][l] );
505 const int i_abs = abs( mvd );
506 const int i_prefix = X264_MIN( i_abs, 9 );
507 const int ctxbase = (l == 0 ? 40 : 47);
519 for( i = 0; i < i_prefix; i++ )
521 x264_cabac_encode_decision( cb, ctxbase + ctx, 1 );
528 x264_cabac_encode_decision( cb, ctxbase + ctx, 0 );
530 x264_cabac_encode_ue_bypass( cb, 3, i_abs - 9 );
534 x264_cabac_encode_bypass( cb, mvd < 0 );
537 static inline void x264_cabac_mb_mvd( x264_t *h, x264_cabac_t *cb, int i_list, int idx, int width, int height )
543 x264_mb_predict_mv( h, i_list, idx, width, mvp );
544 mdx = h->mb.cache.mv[i_list][x264_scan8[idx]][0] - mvp[0];
545 mdy = h->mb.cache.mv[i_list][x264_scan8[idx]][1] - mvp[1];
548 x264_cabac_mb_mvd_cpn( h, cb, i_list, idx, 0, mdx );
549 x264_cabac_mb_mvd_cpn( h, cb, i_list, idx, 1, mdy );
552 x264_macroblock_cache_mvd( h, block_idx_x[idx], block_idx_y[idx], width, height, i_list, mdx, mdy );
555 static inline void x264_cabac_mb8x8_mvd( x264_t *h, x264_cabac_t *cb, int i_list, int i )
557 if( !x264_mb_partition_listX_table[i_list][ h->mb.i_sub_partition[i] ] )
560 switch( h->mb.i_sub_partition[i] )
565 x264_cabac_mb_mvd( h, cb, i_list, 4*i, 2, 2 );
570 x264_cabac_mb_mvd( h, cb, i_list, 4*i+0, 2, 1 );
571 x264_cabac_mb_mvd( h, cb, i_list, 4*i+2, 2, 1 );
576 x264_cabac_mb_mvd( h, cb, i_list, 4*i+0, 1, 2 );
577 x264_cabac_mb_mvd( h, cb, i_list, 4*i+1, 1, 2 );
582 x264_cabac_mb_mvd( h, cb, i_list, 4*i+0, 1, 1 );
583 x264_cabac_mb_mvd( h, cb, i_list, 4*i+1, 1, 1 );
584 x264_cabac_mb_mvd( h, cb, i_list, 4*i+2, 1, 1 );
585 x264_cabac_mb_mvd( h, cb, i_list, 4*i+3, 1, 1 );
590 static int x264_cabac_mb_cbf_ctxidxinc( x264_t *h, int i_cat, int i_idx )
598 if( i_cat == DCT_LUMA_DC )
600 if( h->mb.i_neighbour & MB_LEFT )
602 i_mba_xy = h->mb.i_mb_xy - 1;
603 if( h->mb.i_mb_type_left == I_16x16 )
604 i_nza = h->mb.cbp[i_mba_xy] & 0x100;
606 if( h->mb.i_neighbour & MB_TOP )
608 i_mbb_xy = h->mb.i_mb_top_xy;
609 if( h->mb.i_mb_type_top == I_16x16 )
610 i_nzb = h->mb.cbp[i_mbb_xy] & 0x100;
613 else if( i_cat == DCT_LUMA_AC || i_cat == DCT_LUMA_4x4 )
615 if( i_idx & ~10 ) // block_idx_x > 0
616 i_mba_xy = h->mb.i_mb_xy;
617 else if( h->mb.i_neighbour & MB_LEFT )
618 i_mba_xy = h->mb.i_mb_xy - 1;
620 if( i_idx & ~5 ) // block_idx_y > 0
621 i_mbb_xy = h->mb.i_mb_xy;
622 else if( h->mb.i_neighbour & MB_TOP )
623 i_mbb_xy = h->mb.i_mb_top_xy;
625 /* no need to test for skip/pcm */
627 i_nza = h->mb.cache.non_zero_count[x264_scan8[i_idx] - 1];
629 i_nzb = h->mb.cache.non_zero_count[x264_scan8[i_idx] - 8];
631 else if( i_cat == DCT_CHROMA_DC )
633 /* no need to test skip/pcm */
634 if( h->mb.i_neighbour & MB_LEFT )
636 i_mba_xy = h->mb.i_mb_xy - 1;
637 i_nza = h->mb.cbp[i_mba_xy] & (0x200 << i_idx);
639 if( h->mb.i_neighbour & MB_TOP )
641 i_mbb_xy = h->mb.i_mb_top_xy;
642 i_nzb = h->mb.cbp[i_mbb_xy] & (0x200 << i_idx);
645 else if( i_cat == DCT_CHROMA_AC )
648 i_mba_xy = h->mb.i_mb_xy;
649 else if( h->mb.i_neighbour & MB_LEFT )
650 i_mba_xy = h->mb.i_mb_xy - 1;
653 i_mbb_xy = h->mb.i_mb_xy;
654 else if( h->mb.i_neighbour & MB_TOP )
655 i_mbb_xy = h->mb.i_mb_top_xy;
657 /* no need to test skip/pcm */
659 i_nza = h->mb.cache.non_zero_count[x264_scan8[i_idx] - 1];
661 i_nzb = h->mb.cache.non_zero_count[x264_scan8[i_idx] - 8];
664 if( IS_INTRA( h->mb.i_type ) )
681 static const int significant_coeff_flag_offset[2][6] = {
682 { 105, 120, 134, 149, 152, 402 },
683 { 277, 292, 306, 321, 324, 436 }
685 static const int last_coeff_flag_offset[2][6] = {
686 { 166, 181, 195, 210, 213, 417 },
687 { 338, 353, 367, 382, 385, 451 }
689 static const int coeff_abs_level_m1_offset[6] =
690 { 227, 237, 247, 257, 266, 426 };
691 static const int significant_coeff_flag_offset_8x8[2][63] =
693 0, 1, 2, 3, 4, 5, 5, 4, 4, 3, 3, 4, 4, 4, 5, 5,
694 4, 4, 4, 4, 3, 3, 6, 7, 7, 7, 8, 9,10, 9, 8, 7,
695 7, 6,11,12,13,11, 6, 7, 8, 9,14,10, 9, 8, 6,11,
696 12,13,11, 6, 9,14,10, 9,11,12,13,11,14,10,12
698 0, 1, 1, 2, 2, 3, 3, 4, 5, 6, 7, 7, 7, 8, 4, 5,
699 6, 9,10,10, 8,11,12,11, 9, 9,10,10, 8,11,12,11,
700 9, 9,10,10, 8,11,12,11, 9, 9,10,10, 8,13,13, 9,
701 9,10,10, 8,13,13, 9, 9,10,10,14,14,14,14,14
703 static const int last_coeff_flag_offset_8x8[63] = {
704 0, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1,
705 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2,
706 3, 3, 3, 3, 3, 3, 3, 3, 4, 4, 4, 4, 4, 4, 4, 4,
707 5, 5, 5, 5, 6, 6, 6, 6, 7, 7, 7, 7, 8, 8, 8
709 static const int identity[16] =
710 { 0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15 };
712 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 )
714 const int i_ctx_sig = significant_coeff_flag_offset[h->mb.b_interlaced][i_ctxBlockCat];
715 const int i_ctx_last = last_coeff_flag_offset[h->mb.b_interlaced][i_ctxBlockCat];
716 const int i_ctx_level = coeff_abs_level_m1_offset[i_ctxBlockCat];
718 int i_coeff_abs_m1[64];
719 int i_coeff_sign[64];
725 int i_abslevelgt1 = 0;
729 const int *significant_coeff_flag_offset;
730 const int *last_coeff_flag_offset;
732 /* i_ctxBlockCat: 0-> DC 16x16 i_idx = 0
733 * 1-> AC 16x16 i_idx = luma4x4idx
734 * 2-> Luma4x4 i_idx = luma4x4idx
735 * 3-> DC Chroma i_idx = iCbCr
736 * 4-> AC Chroma i_idx = 4 * iCbCr + chroma4x4idx
737 * 5-> Luma8x8 i_idx = luma8x8idx
740 for( i = 0; i < i_count; i++ )
744 i_coeff_abs_m1[i_coeff] = abs( l[i] ) - 1;
745 i_coeff_sign[i_coeff] = ( l[i] < 0 );
754 /* coded block flag */
755 x264_cabac_encode_decision( cb, 85 + x264_cabac_mb_cbf_ctxidxinc( h, i_ctxBlockCat, i_idx ), i_coeff != 0 );
760 significant_coeff_flag_offset = (i_ctxBlockCat == DCT_LUMA_8x8)
761 ? significant_coeff_flag_offset_8x8[h->mb.b_interlaced]
763 last_coeff_flag_offset = (i_ctxBlockCat == DCT_LUMA_8x8)
764 ? last_coeff_flag_offset_8x8 : identity;
766 i_sigmap_size = X264_MIN( i_last+1, i_count-1 );
767 for( i = 0; i < i_sigmap_size; i++ )
769 x264_cabac_encode_decision( cb, i_ctx_sig + significant_coeff_flag_offset[i], l[i] != 0 );
771 x264_cabac_encode_decision( cb, i_ctx_last + last_coeff_flag_offset[i], i == i_last );
774 for( i = i_coeff - 1; i >= 0; i-- )
776 /* write coeff_abs - 1 */
777 const int i_prefix = X264_MIN( i_coeff_abs_m1[i], 14 );
778 const int i_ctxIdxInc = (i_abslevelgt1 ? 0 : X264_MIN( 4, i_abslevel1 + 1 )) + i_ctx_level;
779 x264_cabac_encode_decision( cb, i_ctxIdxInc, i_prefix != 0 );
783 const int i_ctxIdxInc = 5 + X264_MIN( 4, i_abslevelgt1 ) + i_ctx_level;
785 cb->f8_bits_encoded += cabac_prefix_size[i_prefix][cb->state[i_ctxIdxInc]];
786 cb->state[i_ctxIdxInc] = cabac_prefix_transition[i_prefix][cb->state[i_ctxIdxInc]];
789 for( j = 0; j < i_prefix - 1; j++ )
790 x264_cabac_encode_decision( cb, i_ctxIdxInc, 1 );
792 x264_cabac_encode_decision( cb, i_ctxIdxInc, 0 );
795 x264_cabac_encode_ue_bypass( cb, 0, i_coeff_abs_m1[i] - 14 );
806 x264_cabac_encode_bypass( cb, i_coeff_sign[i] );
812 void x264_macroblock_write_cabac( x264_t *h, x264_cabac_t *cb )
814 const int i_mb_type = h->mb.i_type;
819 const int i_mb_pos_start = x264_cabac_pos( cb );
823 /* Write the MB type */
824 x264_cabac_mb_type( h, cb );
826 /* PCM special block type UNTESTED */
827 if( i_mb_type == I_PCM )
830 cb->f8_bits_encoded += (384*8) << 8;
832 if( cb->p + 385 >= cb->p_end )
833 return; //FIXME throw an error
835 for( i = 0; i < 16; i++ )
837 memcpy( cb->p, h->fenc->plane[0] + i*h->mb.pic.i_stride[0], 16 );
841 for( i = 0; i < 8; i++ )
843 memcpy( cb->p, h->fenc->plane[1] + i*h->mb.pic.i_stride[1], 8 );
847 for( i = 0; i < 8; i++ )
849 memcpy( cb->p, h->fenc->plane[2] + i*h->mb.pic.i_stride[2], 8 );
852 x264_cabac_encode_init( cb, cb->p, cb->p_end );
857 if( IS_INTRA( i_mb_type ) )
859 if( h->pps->b_transform_8x8_mode && i_mb_type != I_16x16 )
860 x264_cabac_mb_transform_size( h, cb );
862 if( i_mb_type != I_16x16 )
864 int di = (i_mb_type == I_8x8) ? 4 : 1;
865 for( i = 0; i < 16; i += di )
867 const int i_pred = x264_mb_predict_intra4x4_mode( h, i );
868 const int i_mode = x264_mb_pred_mode4x4_fix( h->mb.cache.intra4x4_pred_mode[x264_scan8[i]] );
869 x264_cabac_mb_intra4x4_pred_mode( cb, i_pred, i_mode );
873 x264_cabac_mb_intra_chroma_pred_mode( h, cb );
875 else if( i_mb_type == P_L0 )
877 if( h->mb.i_partition == D_16x16 )
879 if( h->mb.pic.i_fref[0] > 1 )
881 x264_cabac_mb_ref( h, cb, 0, 0 );
883 x264_cabac_mb_mvd( h, cb, 0, 0, 4, 4 );
885 else if( h->mb.i_partition == D_16x8 )
887 if( h->mb.pic.i_fref[0] > 1 )
889 x264_cabac_mb_ref( h, cb, 0, 0 );
890 x264_cabac_mb_ref( h, cb, 0, 8 );
892 x264_cabac_mb_mvd( h, cb, 0, 0, 4, 2 );
893 x264_cabac_mb_mvd( h, cb, 0, 8, 4, 2 );
895 else if( h->mb.i_partition == D_8x16 )
897 if( h->mb.pic.i_fref[0] > 1 )
899 x264_cabac_mb_ref( h, cb, 0, 0 );
900 x264_cabac_mb_ref( h, cb, 0, 4 );
902 x264_cabac_mb_mvd( h, cb, 0, 0, 2, 4 );
903 x264_cabac_mb_mvd( h, cb, 0, 4, 2, 4 );
906 else if( i_mb_type == P_8x8 )
909 x264_cabac_mb_sub_p_partition( cb, h->mb.i_sub_partition[0] );
910 x264_cabac_mb_sub_p_partition( cb, h->mb.i_sub_partition[1] );
911 x264_cabac_mb_sub_p_partition( cb, h->mb.i_sub_partition[2] );
912 x264_cabac_mb_sub_p_partition( cb, h->mb.i_sub_partition[3] );
915 if( h->mb.pic.i_fref[0] > 1 )
917 x264_cabac_mb_ref( h, cb, 0, 0 );
918 x264_cabac_mb_ref( h, cb, 0, 4 );
919 x264_cabac_mb_ref( h, cb, 0, 8 );
920 x264_cabac_mb_ref( h, cb, 0, 12 );
923 for( i = 0; i < 4; i++ )
924 x264_cabac_mb8x8_mvd( h, cb, 0, i );
926 else if( i_mb_type == B_8x8 )
929 x264_cabac_mb_sub_b_partition( cb, h->mb.i_sub_partition[0] );
930 x264_cabac_mb_sub_b_partition( cb, h->mb.i_sub_partition[1] );
931 x264_cabac_mb_sub_b_partition( cb, h->mb.i_sub_partition[2] );
932 x264_cabac_mb_sub_b_partition( cb, h->mb.i_sub_partition[3] );
935 for( i_list = 0; i_list < 2; i_list++ )
937 if( ( i_list ? h->mb.pic.i_fref[1] : h->mb.pic.i_fref[0] ) == 1 )
939 for( i = 0; i < 4; i++ )
940 if( x264_mb_partition_listX_table[i_list][ h->mb.i_sub_partition[i] ] )
941 x264_cabac_mb_ref( h, cb, i_list, 4*i );
944 for( i = 0; i < 4; i++ )
945 x264_cabac_mb8x8_mvd( h, cb, 0, i );
946 for( i = 0; i < 4; i++ )
947 x264_cabac_mb8x8_mvd( h, cb, 1, i );
949 else if( i_mb_type != B_DIRECT )
954 /* init ref list utilisations */
955 for( i = 0; i < 2; i++ )
957 b_list[0][i] = x264_mb_type_list0_table[i_mb_type][i];
958 b_list[1][i] = x264_mb_type_list1_table[i_mb_type][i];
961 for( i_list = 0; i_list < 2; i_list++ )
963 const int i_ref_max = i_list == 0 ? h->mb.pic.i_fref[0] : h->mb.pic.i_fref[1];
967 if( h->mb.i_partition == D_16x16 )
969 if( b_list[i_list][0] ) x264_cabac_mb_ref( h, cb, i_list, 0 );
971 else if( h->mb.i_partition == D_16x8 )
973 if( b_list[i_list][0] ) x264_cabac_mb_ref( h, cb, i_list, 0 );
974 if( b_list[i_list][1] ) x264_cabac_mb_ref( h, cb, i_list, 8 );
976 else if( h->mb.i_partition == D_8x16 )
978 if( b_list[i_list][0] ) x264_cabac_mb_ref( h, cb, i_list, 0 );
979 if( b_list[i_list][1] ) x264_cabac_mb_ref( h, cb, i_list, 4 );
983 for( i_list = 0; i_list < 2; i_list++ )
985 if( h->mb.i_partition == D_16x16 )
987 if( b_list[i_list][0] ) x264_cabac_mb_mvd( h, cb, i_list, 0, 4, 4 );
989 else if( h->mb.i_partition == D_16x8 )
991 if( b_list[i_list][0] ) x264_cabac_mb_mvd( h, cb, i_list, 0, 4, 2 );
992 if( b_list[i_list][1] ) x264_cabac_mb_mvd( h, cb, i_list, 8, 4, 2 );
994 else if( h->mb.i_partition == D_8x16 )
996 if( b_list[i_list][0] ) x264_cabac_mb_mvd( h, cb, i_list, 0, 2, 4 );
997 if( b_list[i_list][1] ) x264_cabac_mb_mvd( h, cb, i_list, 4, 2, 4 );
1003 i_mb_pos_tex = x264_cabac_pos( cb );
1004 h->stat.frame.i_hdr_bits += i_mb_pos_tex - i_mb_pos_start;
1007 if( i_mb_type != I_16x16 )
1009 x264_cabac_mb_cbp_luma( h, cb );
1010 x264_cabac_mb_cbp_chroma( h, cb );
1013 if( x264_mb_transform_8x8_allowed( h ) && h->mb.i_cbp_luma )
1015 x264_cabac_mb_transform_size( h, cb );
1018 if( h->mb.i_cbp_luma > 0 || h->mb.i_cbp_chroma > 0 || i_mb_type == I_16x16 )
1020 x264_cabac_mb_qp_delta( h, cb );
1022 /* write residual */
1023 if( i_mb_type == I_16x16 )
1026 block_residual_write_cabac( h, cb, DCT_LUMA_DC, 0, h->dct.luma16x16_dc, 16 );
1029 if( h->mb.i_cbp_luma != 0 )
1030 for( i = 0; i < 16; i++ )
1031 block_residual_write_cabac( h, cb, DCT_LUMA_AC, i, h->dct.luma4x4[i]+1, 15 );
1033 else if( h->mb.b_transform_8x8 )
1035 for( i = 0; i < 4; i++ )
1036 if( h->mb.i_cbp_luma & ( 1 << i ) )
1037 block_residual_write_cabac( h, cb, DCT_LUMA_8x8, i, h->dct.luma8x8[i], 64 );
1041 for( i = 0; i < 16; i++ )
1042 if( h->mb.i_cbp_luma & ( 1 << ( i / 4 ) ) )
1043 block_residual_write_cabac( h, cb, DCT_LUMA_4x4, i, h->dct.luma4x4[i], 16 );
1046 if( h->mb.i_cbp_chroma &0x03 ) /* Chroma DC residual present */
1048 block_residual_write_cabac( h, cb, DCT_CHROMA_DC, 0, h->dct.chroma_dc[0], 4 );
1049 block_residual_write_cabac( h, cb, DCT_CHROMA_DC, 1, h->dct.chroma_dc[1], 4 );
1051 if( h->mb.i_cbp_chroma&0x02 ) /* Chroma AC residual present */
1053 for( i = 16; i < 24; i++ )
1054 block_residual_write_cabac( h, cb, DCT_CHROMA_AC, i, h->dct.luma4x4[i]+1, 15 );
1059 if( IS_INTRA( i_mb_type ) )
1060 h->stat.frame.i_itex_bits += x264_cabac_pos( cb ) - i_mb_pos_tex;
1062 h->stat.frame.i_ptex_bits += x264_cabac_pos( cb ) - i_mb_pos_tex;
1067 /*****************************************************************************
1068 * RD only; doesn't generate a valid bitstream
1069 * doesn't write cbp or chroma dc (I don't know how much this matters)
1070 * works on all partition sizes except 16x16
1071 * for sub8x8, call once per 8x8 block
1072 *****************************************************************************/
1073 void x264_partition_size_cabac( x264_t *h, x264_cabac_t *cb, int i8, int i_pixel )
1075 const int i_mb_type = h->mb.i_type;
1078 if( i_mb_type == P_8x8 )
1080 x264_cabac_mb_sub_p_partition( cb, h->mb.i_sub_partition[i8] );
1081 if( h->mb.pic.i_fref[0] > 1 )
1082 x264_cabac_mb_ref( h, cb, 0, 4*i8 );
1083 x264_cabac_mb8x8_mvd( h, cb, 0, i8 );
1085 else if( i_mb_type == P_L0 )
1087 if( h->mb.pic.i_fref[0] > 1 )
1088 x264_cabac_mb_ref( h, cb, 0, 4*i8 );
1089 if( h->mb.i_partition == D_16x8 )
1090 x264_cabac_mb_mvd( h, cb, 0, 4*i8, 4, 2 );
1092 x264_cabac_mb_mvd( h, cb, 0, 4*i8, 2, 4 );
1094 else if( i_mb_type == B_8x8 )
1096 x264_cabac_mb_sub_b_partition( cb, h->mb.i_sub_partition[i8] );
1098 if( h->mb.pic.i_fref[0] > 1
1099 && x264_mb_partition_listX_table[0][ h->mb.i_sub_partition[i8] ] )
1100 x264_cabac_mb_ref( h, cb, 0, 4*i8 );
1101 if( h->mb.pic.i_fref[1] > 1
1102 && x264_mb_partition_listX_table[1][ h->mb.i_sub_partition[i8] ] )
1103 x264_cabac_mb_ref( h, cb, 1, 4*i8 );
1105 x264_cabac_mb8x8_mvd( h, cb, 0, i8 );
1106 x264_cabac_mb8x8_mvd( h, cb, 1, i8 );
1110 x264_log(h, X264_LOG_ERROR, "invalid/unhandled mb_type\n" );
1114 for( j = (i_pixel < PIXEL_8x8); j >= 0; j-- )
1116 if( h->mb.i_cbp_luma & (1 << i8) )
1118 if( h->mb.b_transform_8x8 )
1119 block_residual_write_cabac( h, cb, DCT_LUMA_8x8, i8, h->dct.luma8x8[i8], 64 );
1123 for( i4 = 0; i4 < 4; i4++ )
1124 block_residual_write_cabac( h, cb, DCT_LUMA_4x4, i4+i8*4, h->dct.luma4x4[i4+i8*4], 16 );
1128 block_residual_write_cabac( h, cb, DCT_CHROMA_AC, 16+i8, h->dct.luma4x4[16+i8]+1, 15 );
1129 block_residual_write_cabac( h, cb, DCT_CHROMA_AC, 20+i8, h->dct.luma4x4[20+i8]+1, 15 );
1131 i8 += x264_pixel_size[i_pixel].h >> 3;
1135 static void x264_partition_i8x8_size_cabac( x264_t *h, x264_cabac_t *cb, int i8, int i_mode )
1137 const int i_pred = x264_mb_predict_intra4x4_mode( h, 4*i8 );
1138 i_mode = x264_mb_pred_mode4x4_fix( i_mode );
1139 x264_cabac_mb_intra4x4_pred_mode( cb, i_pred, i_mode );
1140 block_residual_write_cabac( h, cb, DCT_LUMA_8x8, 4*i8, h->dct.luma8x8[i8], 64 );
1143 static void x264_partition_i4x4_size_cabac( x264_t *h, x264_cabac_t *cb, int i4, int i_mode )
1145 const int i_pred = x264_mb_predict_intra4x4_mode( h, i4 );
1146 i_mode = x264_mb_pred_mode4x4_fix( i_mode );
1147 x264_cabac_mb_intra4x4_pred_mode( cb, i_pred, i_mode );
1148 block_residual_write_cabac( h, cb, DCT_LUMA_4x4, i4, h->dct.luma4x4[i4], 16 );
1151 static void x264_i8x8_chroma_size_cabac( x264_t *h, x264_cabac_t *cb )
1153 x264_cabac_mb_intra_chroma_pred_mode( h, cb );
1154 if( h->mb.i_cbp_chroma > 0 )
1156 block_residual_write_cabac( h, cb, DCT_CHROMA_DC, 0, h->dct.chroma_dc[0], 4 );
1157 block_residual_write_cabac( h, cb, DCT_CHROMA_DC, 1, h->dct.chroma_dc[1], 4 );
1159 if( h->mb.i_cbp_chroma == 2 )
1162 for( i = 16; i < 24; i++ )
1163 block_residual_write_cabac( h, cb, DCT_CHROMA_AC, i, h->dct.luma4x4[i]+1, 15 );