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 == 0 ? 0 : 1 ));
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 ? 0 : 1 ) );
73 x264_cabac_encode_decision( cb, ctx4, ( (i_pred / 2) ? 1 : 0 ));
74 x264_cabac_encode_decision( cb, ctx5, ( (i_pred % 2) ? 1 : 0 ));
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 );
384 void x264_cabac_mb_skip( x264_t *h, int b_skip )
388 if( h->mb.i_mb_type_left >= 0 && !IS_SKIP( h->mb.i_mb_type_left ) )
392 if( h->mb.i_mb_type_top >= 0 && !IS_SKIP( h->mb.i_mb_type_top ) )
397 ctx += (h->sh.i_type == SLICE_TYPE_P) ? 11 : 24;
398 x264_cabac_encode_decision( &h->cabac, ctx, b_skip );
401 static inline void x264_cabac_mb_sub_p_partition( x264_cabac_t *cb, int i_sub )
403 if( i_sub == D_L0_8x8 )
405 x264_cabac_encode_decision( cb, 21, 1 );
407 else if( i_sub == D_L0_8x4 )
409 x264_cabac_encode_decision( cb, 21, 0 );
410 x264_cabac_encode_decision( cb, 22, 0 );
412 else if( i_sub == D_L0_4x8 )
414 x264_cabac_encode_decision( cb, 21, 0 );
415 x264_cabac_encode_decision( cb, 22, 1 );
416 x264_cabac_encode_decision( cb, 23, 1 );
418 else if( i_sub == D_L0_4x4 )
420 x264_cabac_encode_decision( cb, 21, 0 );
421 x264_cabac_encode_decision( cb, 22, 1 );
422 x264_cabac_encode_decision( cb, 23, 0 );
426 static inline void x264_cabac_mb_sub_b_partition( x264_cabac_t *cb, int i_sub )
428 #define WRITE_SUB_3(a,b,c) {\
429 x264_cabac_encode_decision( cb, 36, a );\
430 x264_cabac_encode_decision( cb, 37, b );\
431 x264_cabac_encode_decision( cb, 39, c );\
433 #define WRITE_SUB_5(a,b,c,d,e) {\
434 x264_cabac_encode_decision( cb, 36, a );\
435 x264_cabac_encode_decision( cb, 37, b );\
436 x264_cabac_encode_decision( cb, 38, c );\
437 x264_cabac_encode_decision( cb, 39, d );\
438 x264_cabac_encode_decision( cb, 39, e );\
440 #define WRITE_SUB_6(a,b,c,d,e,f) {\
441 WRITE_SUB_5(a,b,c,d,e)\
442 x264_cabac_encode_decision( cb, 39, f );\
448 x264_cabac_encode_decision( cb, 36, 0 );
450 case D_L0_8x8: WRITE_SUB_3(1,0,0); break;
451 case D_L1_8x8: WRITE_SUB_3(1,0,1); break;
452 case D_BI_8x8: WRITE_SUB_5(1,1,0,0,0); break;
453 case D_L0_8x4: WRITE_SUB_5(1,1,0,0,1); break;
454 case D_L0_4x8: WRITE_SUB_5(1,1,0,1,0); break;
455 case D_L1_8x4: WRITE_SUB_5(1,1,0,1,1); break;
456 case D_L1_4x8: WRITE_SUB_6(1,1,1,0,0,0); break;
457 case D_BI_8x4: WRITE_SUB_6(1,1,1,0,0,1); break;
458 case D_BI_4x8: WRITE_SUB_6(1,1,1,0,1,0); break;
459 case D_L0_4x4: WRITE_SUB_6(1,1,1,0,1,1); break;
460 case D_L1_4x4: WRITE_SUB_5(1,1,1,1,0); break;
461 case D_BI_4x4: WRITE_SUB_5(1,1,1,1,1); break;
465 static inline void x264_cabac_mb_transform_size( x264_t *h, x264_cabac_t *cb )
467 int ctx = 399 + h->mb.cache.i_neighbour_transform_size;
468 x264_cabac_encode_decision( cb, ctx, h->mb.b_transform_8x8 );
471 static inline void x264_cabac_mb_ref( x264_t *h, x264_cabac_t *cb, int i_list, int idx )
473 const int i8 = x264_scan8[idx];
474 const int i_refa = h->mb.cache.ref[i_list][i8 - 1];
475 const int i_refb = h->mb.cache.ref[i_list][i8 - 8];
476 int i_ref = h->mb.cache.ref[i_list][i8];
479 if( i_refa > 0 && !h->mb.cache.skip[i8 - 1])
481 if( i_refb > 0 && !h->mb.cache.skip[i8 - 8])
486 x264_cabac_encode_decision( cb, 54 + ctx, 1 );
494 x264_cabac_encode_decision( cb, 54 + ctx, 0 );
499 static inline void x264_cabac_mb_mvd_cpn( x264_t *h, x264_cabac_t *cb, int i_list, int idx, int l, int mvd )
501 const int amvd = abs( h->mb.cache.mvd[i_list][x264_scan8[idx] - 1][l] ) +
502 abs( h->mb.cache.mvd[i_list][x264_scan8[idx] - 8][l] );
503 const int i_abs = abs( mvd );
504 const int i_prefix = X264_MIN( i_abs, 9 );
505 const int ctxbase = (l == 0 ? 40 : 47);
517 for( i = 0; i < i_prefix; i++ )
519 x264_cabac_encode_decision( cb, ctxbase + ctx, 1 );
526 x264_cabac_encode_decision( cb, ctxbase + ctx, 0 );
528 x264_cabac_encode_ue_bypass( cb, 3, i_abs - 9 );
532 x264_cabac_encode_bypass( cb, mvd < 0 );
535 static inline void x264_cabac_mb_mvd( x264_t *h, x264_cabac_t *cb, int i_list, int idx, int width, int height )
541 x264_mb_predict_mv( h, i_list, idx, width, mvp );
542 mdx = h->mb.cache.mv[i_list][x264_scan8[idx]][0] - mvp[0];
543 mdy = h->mb.cache.mv[i_list][x264_scan8[idx]][1] - mvp[1];
546 x264_cabac_mb_mvd_cpn( h, cb, i_list, idx, 0, mdx );
547 x264_cabac_mb_mvd_cpn( h, cb, i_list, idx, 1, mdy );
550 x264_macroblock_cache_mvd( h, block_idx_x[idx], block_idx_y[idx], width, height, i_list, mdx, mdy );
553 static inline void x264_cabac_mb8x8_mvd( x264_t *h, x264_cabac_t *cb, int i_list, int i )
555 if( !x264_mb_partition_listX_table[i_list][ h->mb.i_sub_partition[i] ] )
558 switch( h->mb.i_sub_partition[i] )
563 x264_cabac_mb_mvd( h, cb, i_list, 4*i, 2, 2 );
568 x264_cabac_mb_mvd( h, cb, i_list, 4*i+0, 2, 1 );
569 x264_cabac_mb_mvd( h, cb, i_list, 4*i+2, 2, 1 );
574 x264_cabac_mb_mvd( h, cb, i_list, 4*i+0, 1, 2 );
575 x264_cabac_mb_mvd( h, cb, i_list, 4*i+1, 1, 2 );
580 x264_cabac_mb_mvd( h, cb, i_list, 4*i+0, 1, 1 );
581 x264_cabac_mb_mvd( h, cb, i_list, 4*i+1, 1, 1 );
582 x264_cabac_mb_mvd( h, cb, i_list, 4*i+2, 1, 1 );
583 x264_cabac_mb_mvd( h, cb, i_list, 4*i+3, 1, 1 );
588 static int x264_cabac_mb_cbf_ctxidxinc( x264_t *h, int i_cat, int i_idx )
596 if( i_cat == DCT_LUMA_DC )
598 if( h->mb.i_neighbour & MB_LEFT )
600 i_mba_xy = h->mb.i_mb_xy - 1;
601 if( h->mb.i_mb_type_left == I_16x16 )
602 i_nza = h->mb.cbp[i_mba_xy] & 0x100;
604 if( h->mb.i_neighbour & MB_TOP )
606 i_mbb_xy = h->mb.i_mb_top_xy;
607 if( h->mb.i_mb_type_top == I_16x16 )
608 i_nzb = h->mb.cbp[i_mbb_xy] & 0x100;
611 else if( i_cat == DCT_LUMA_AC || i_cat == DCT_LUMA_4x4 )
613 if( i_idx & ~10 ) // block_idx_x > 0
614 i_mba_xy = h->mb.i_mb_xy;
615 else if( h->mb.i_neighbour & MB_LEFT )
616 i_mba_xy = h->mb.i_mb_xy - 1;
618 if( i_idx & ~5 ) // block_idx_y > 0
619 i_mbb_xy = h->mb.i_mb_xy;
620 else if( h->mb.i_neighbour & MB_TOP )
621 i_mbb_xy = h->mb.i_mb_top_xy;
623 /* no need to test for skip/pcm */
625 i_nza = h->mb.cache.non_zero_count[x264_scan8[i_idx] - 1];
627 i_nzb = h->mb.cache.non_zero_count[x264_scan8[i_idx] - 8];
629 else if( i_cat == DCT_CHROMA_DC )
631 /* no need to test skip/pcm */
632 if( h->mb.i_neighbour & MB_LEFT )
634 i_mba_xy = h->mb.i_mb_xy - 1;
635 i_nza = h->mb.cbp[i_mba_xy] & (0x200 << i_idx);
637 if( h->mb.i_neighbour & MB_TOP )
639 i_mbb_xy = h->mb.i_mb_top_xy;
640 i_nzb = h->mb.cbp[i_mbb_xy] & (0x200 << i_idx);
643 else if( i_cat == DCT_CHROMA_AC )
646 i_mba_xy = h->mb.i_mb_xy;
647 else if( h->mb.i_neighbour & MB_LEFT )
648 i_mba_xy = h->mb.i_mb_xy - 1;
651 i_mbb_xy = h->mb.i_mb_xy;
652 else if( h->mb.i_neighbour & MB_TOP )
653 i_mbb_xy = h->mb.i_mb_top_xy;
655 /* no need to test skip/pcm */
657 i_nza = h->mb.cache.non_zero_count[x264_scan8[16+i_idx] - 1];
659 i_nzb = h->mb.cache.non_zero_count[x264_scan8[16+i_idx] - 8];
662 if( IS_INTRA( h->mb.i_type ) )
679 static const int significant_coeff_flag_offset[2][6] = {
680 { 105, 120, 134, 149, 152, 402 },
681 { 277, 292, 306, 321, 324, 436 }
683 static const int last_coeff_flag_offset[2][6] = {
684 { 166, 181, 195, 210, 213, 417 },
685 { 338, 353, 367, 382, 385, 451 }
687 static const int coeff_abs_level_m1_offset[6] =
688 { 227, 237, 247, 257, 266, 426 };
689 static const int significant_coeff_flag_offset_8x8[2][63] =
691 0, 1, 2, 3, 4, 5, 5, 4, 4, 3, 3, 4, 4, 4, 5, 5,
692 4, 4, 4, 4, 3, 3, 6, 7, 7, 7, 8, 9,10, 9, 8, 7,
693 7, 6,11,12,13,11, 6, 7, 8, 9,14,10, 9, 8, 6,11,
694 12,13,11, 6, 9,14,10, 9,11,12,13,11,14,10,12
696 0, 1, 1, 2, 2, 3, 3, 4, 5, 6, 7, 7, 7, 8, 4, 5,
697 6, 9,10,10, 8,11,12,11, 9, 9,10,10, 8,11,12,11,
698 9, 9,10,10, 8,11,12,11, 9, 9,10,10, 8,13,13, 9,
699 9,10,10, 8,13,13, 9, 9,10,10,14,14,14,14,14
701 static const int last_coeff_flag_offset_8x8[63] = {
702 0, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1,
703 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2,
704 3, 3, 3, 3, 3, 3, 3, 3, 4, 4, 4, 4, 4, 4, 4, 4,
705 5, 5, 5, 5, 6, 6, 6, 6, 7, 7, 7, 7, 8, 8, 8
707 static const int identity[16] =
708 { 0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15 };
710 static void block_residual_write_cabac( x264_t *h, x264_cabac_t *cb, int i_ctxBlockCat, int i_idx, int *l, int i_count )
712 const int i_ctx_sig = significant_coeff_flag_offset[h->mb.b_interlaced][i_ctxBlockCat];
713 const int i_ctx_last = last_coeff_flag_offset[h->mb.b_interlaced][i_ctxBlockCat];
714 const int i_ctx_level = coeff_abs_level_m1_offset[i_ctxBlockCat];
716 int i_coeff_abs_m1[64];
717 int i_coeff_sign[64];
723 int i_abslevelgt1 = 0;
727 const int *significant_coeff_flag_offset;
728 const int *last_coeff_flag_offset;
730 /* i_ctxBlockCat: 0-> DC 16x16 i_idx = 0
731 * 1-> AC 16x16 i_idx = luma4x4idx
732 * 2-> Luma4x4 i_idx = luma4x4idx
733 * 3-> DC Chroma i_idx = iCbCr
734 * 4-> AC Chroma i_idx = 4 * iCbCr + chroma4x4idx
735 * 5-> Luma8x8 i_idx = luma8x8idx
738 for( i = 0; i < i_count; i++ )
742 i_coeff_abs_m1[i_coeff] = abs( l[i] ) - 1;
743 i_coeff_sign[i_coeff] = ( l[i] < 0 );
752 /* coded block flag */
753 x264_cabac_encode_decision( cb, 85 + x264_cabac_mb_cbf_ctxidxinc( h, i_ctxBlockCat, i_idx ), i_coeff != 0 );
758 significant_coeff_flag_offset = (i_ctxBlockCat == DCT_LUMA_8x8)
759 ? significant_coeff_flag_offset_8x8[h->mb.b_interlaced]
761 last_coeff_flag_offset = (i_ctxBlockCat == DCT_LUMA_8x8)
762 ? last_coeff_flag_offset_8x8 : identity;
764 i_sigmap_size = X264_MIN( i_last+1, i_count-1 );
765 for( i = 0; i < i_sigmap_size; i++ )
767 x264_cabac_encode_decision( cb, i_ctx_sig + significant_coeff_flag_offset[i], l[i] != 0 );
769 x264_cabac_encode_decision( cb, i_ctx_last + last_coeff_flag_offset[i], i == i_last );
772 for( i = i_coeff - 1; i >= 0; i-- )
774 /* write coeff_abs - 1 */
775 const int i_prefix = X264_MIN( i_coeff_abs_m1[i], 14 );
776 const int i_ctxIdxInc = (i_abslevelgt1 ? 0 : X264_MIN( 4, i_abslevel1 + 1 )) + i_ctx_level;
777 x264_cabac_encode_decision( cb, i_ctxIdxInc, i_prefix != 0 );
781 const int i_ctxIdxInc = 5 + X264_MIN( 4, i_abslevelgt1 ) + i_ctx_level;
783 cb->f8_bits_encoded += cabac_prefix_size[i_prefix][cb->state[i_ctxIdxInc]];
784 cb->state[i_ctxIdxInc] = cabac_prefix_transition[i_prefix][cb->state[i_ctxIdxInc]];
787 for( j = 0; j < i_prefix - 1; j++ )
788 x264_cabac_encode_decision( cb, i_ctxIdxInc, 1 );
790 x264_cabac_encode_decision( cb, i_ctxIdxInc, 0 );
793 x264_cabac_encode_ue_bypass( cb, 0, i_coeff_abs_m1[i] - 14 );
804 x264_cabac_encode_bypass( cb, i_coeff_sign[i] );
810 void x264_macroblock_write_cabac( x264_t *h, x264_cabac_t *cb )
812 const int i_mb_type = h->mb.i_type;
817 const int i_mb_pos_start = x264_cabac_pos( cb );
821 /* Write the MB type */
822 x264_cabac_mb_type( h, cb );
824 /* PCM special block type UNTESTED */
825 if( i_mb_type == I_PCM )
828 cb->f8_bits_encoded += (384*8) << 8;
830 if( cb->p + 385 >= cb->p_end )
831 return; //FIXME throw an error
833 for( i = 0; i < 16; i++ )
835 memcpy( cb->p, h->fenc->plane[0] + i*h->mb.pic.i_stride[0], 16 );
839 for( i = 0; i < 8; i++ )
841 memcpy( cb->p, h->fenc->plane[1] + i*h->mb.pic.i_stride[1], 8 );
845 for( i = 0; i < 8; i++ )
847 memcpy( cb->p, h->fenc->plane[2] + i*h->mb.pic.i_stride[2], 8 );
850 x264_cabac_encode_init( cb, cb->p, cb->p_end );
855 if( IS_INTRA( i_mb_type ) )
857 if( h->pps->b_transform_8x8_mode && i_mb_type != I_16x16 )
858 x264_cabac_mb_transform_size( h, cb );
860 if( i_mb_type != I_16x16 )
862 int di = (i_mb_type == I_8x8) ? 4 : 1;
863 for( i = 0; i < 16; i += di )
865 const int i_pred = x264_mb_predict_intra4x4_mode( h, i );
866 const int i_mode = x264_mb_pred_mode4x4_fix( h->mb.cache.intra4x4_pred_mode[x264_scan8[i]] );
867 x264_cabac_mb_intra4x4_pred_mode( cb, i_pred, i_mode );
871 x264_cabac_mb_intra_chroma_pred_mode( h, cb );
873 else if( i_mb_type == P_L0 )
875 if( h->mb.i_partition == D_16x16 )
877 if( h->mb.pic.i_fref[0] > 1 )
879 x264_cabac_mb_ref( h, cb, 0, 0 );
881 x264_cabac_mb_mvd( h, cb, 0, 0, 4, 4 );
883 else if( h->mb.i_partition == D_16x8 )
885 if( h->mb.pic.i_fref[0] > 1 )
887 x264_cabac_mb_ref( h, cb, 0, 0 );
888 x264_cabac_mb_ref( h, cb, 0, 8 );
890 x264_cabac_mb_mvd( h, cb, 0, 0, 4, 2 );
891 x264_cabac_mb_mvd( h, cb, 0, 8, 4, 2 );
893 else if( h->mb.i_partition == D_8x16 )
895 if( h->mb.pic.i_fref[0] > 1 )
897 x264_cabac_mb_ref( h, cb, 0, 0 );
898 x264_cabac_mb_ref( h, cb, 0, 4 );
900 x264_cabac_mb_mvd( h, cb, 0, 0, 2, 4 );
901 x264_cabac_mb_mvd( h, cb, 0, 4, 2, 4 );
904 else if( i_mb_type == P_8x8 )
907 x264_cabac_mb_sub_p_partition( cb, h->mb.i_sub_partition[0] );
908 x264_cabac_mb_sub_p_partition( cb, h->mb.i_sub_partition[1] );
909 x264_cabac_mb_sub_p_partition( cb, h->mb.i_sub_partition[2] );
910 x264_cabac_mb_sub_p_partition( cb, h->mb.i_sub_partition[3] );
913 if( h->mb.pic.i_fref[0] > 1 )
915 x264_cabac_mb_ref( h, cb, 0, 0 );
916 x264_cabac_mb_ref( h, cb, 0, 4 );
917 x264_cabac_mb_ref( h, cb, 0, 8 );
918 x264_cabac_mb_ref( h, cb, 0, 12 );
921 for( i = 0; i < 4; i++ )
922 x264_cabac_mb8x8_mvd( h, cb, 0, i );
924 else if( i_mb_type == B_8x8 )
927 x264_cabac_mb_sub_b_partition( cb, h->mb.i_sub_partition[0] );
928 x264_cabac_mb_sub_b_partition( cb, h->mb.i_sub_partition[1] );
929 x264_cabac_mb_sub_b_partition( cb, h->mb.i_sub_partition[2] );
930 x264_cabac_mb_sub_b_partition( cb, h->mb.i_sub_partition[3] );
933 for( i_list = 0; i_list < 2; i_list++ )
935 if( ( i_list ? h->mb.pic.i_fref[1] : h->mb.pic.i_fref[0] ) == 1 )
937 for( i = 0; i < 4; i++ )
938 if( x264_mb_partition_listX_table[i_list][ h->mb.i_sub_partition[i] ] )
939 x264_cabac_mb_ref( h, cb, i_list, 4*i );
942 for( i = 0; i < 4; i++ )
943 x264_cabac_mb8x8_mvd( h, cb, 0, i );
944 for( i = 0; i < 4; i++ )
945 x264_cabac_mb8x8_mvd( h, cb, 1, i );
947 else if( i_mb_type != B_DIRECT )
952 /* init ref list utilisations */
953 for( i = 0; i < 2; i++ )
955 b_list[0][i] = x264_mb_type_list0_table[i_mb_type][i];
956 b_list[1][i] = x264_mb_type_list1_table[i_mb_type][i];
959 for( i_list = 0; i_list < 2; i_list++ )
961 const int i_ref_max = i_list == 0 ? h->mb.pic.i_fref[0] : h->mb.pic.i_fref[1];
965 if( h->mb.i_partition == D_16x16 )
967 if( b_list[i_list][0] ) x264_cabac_mb_ref( h, cb, i_list, 0 );
969 else if( h->mb.i_partition == D_16x8 )
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, 8 );
974 else if( h->mb.i_partition == D_8x16 )
976 if( b_list[i_list][0] ) x264_cabac_mb_ref( h, cb, i_list, 0 );
977 if( b_list[i_list][1] ) x264_cabac_mb_ref( h, cb, i_list, 4 );
981 for( i_list = 0; i_list < 2; i_list++ )
983 if( h->mb.i_partition == D_16x16 )
985 if( b_list[i_list][0] ) x264_cabac_mb_mvd( h, cb, i_list, 0, 4, 4 );
987 else if( h->mb.i_partition == D_16x8 )
989 if( b_list[i_list][0] ) x264_cabac_mb_mvd( h, cb, i_list, 0, 4, 2 );
990 if( b_list[i_list][1] ) x264_cabac_mb_mvd( h, cb, i_list, 8, 4, 2 );
992 else if( h->mb.i_partition == D_8x16 )
994 if( b_list[i_list][0] ) x264_cabac_mb_mvd( h, cb, i_list, 0, 2, 4 );
995 if( b_list[i_list][1] ) x264_cabac_mb_mvd( h, cb, i_list, 4, 2, 4 );
1001 i_mb_pos_tex = x264_cabac_pos( cb );
1002 h->stat.frame.i_hdr_bits += i_mb_pos_tex - i_mb_pos_start;
1005 if( i_mb_type != I_16x16 )
1007 x264_cabac_mb_cbp_luma( h, cb );
1008 x264_cabac_mb_cbp_chroma( h, cb );
1011 if( x264_mb_transform_8x8_allowed( h ) && h->mb.i_cbp_luma )
1013 x264_cabac_mb_transform_size( h, cb );
1016 if( h->mb.i_cbp_luma > 0 || h->mb.i_cbp_chroma > 0 || i_mb_type == I_16x16 )
1018 x264_cabac_mb_qp_delta( h, cb );
1020 /* write residual */
1021 if( i_mb_type == I_16x16 )
1024 block_residual_write_cabac( h, cb, DCT_LUMA_DC, 0, h->dct.luma16x16_dc, 16 );
1027 if( h->mb.i_cbp_luma != 0 )
1028 for( i = 0; i < 16; i++ )
1029 block_residual_write_cabac( h, cb, DCT_LUMA_AC, i, h->dct.block[i].residual_ac, 15 );
1031 else if( h->mb.b_transform_8x8 )
1033 for( i = 0; i < 4; i++ )
1034 if( h->mb.i_cbp_luma & ( 1 << i ) )
1035 block_residual_write_cabac( h, cb, DCT_LUMA_8x8, i, h->dct.luma8x8[i], 64 );
1039 for( i = 0; i < 16; i++ )
1040 if( h->mb.i_cbp_luma & ( 1 << ( i / 4 ) ) )
1041 block_residual_write_cabac( h, cb, DCT_LUMA_4x4, i, h->dct.block[i].luma4x4, 16 );
1044 if( h->mb.i_cbp_chroma &0x03 ) /* Chroma DC residual present */
1046 block_residual_write_cabac( h, cb, DCT_CHROMA_DC, 0, h->dct.chroma_dc[0], 4 );
1047 block_residual_write_cabac( h, cb, DCT_CHROMA_DC, 1, h->dct.chroma_dc[1], 4 );
1049 if( h->mb.i_cbp_chroma&0x02 ) /* Chroma AC residual present */
1051 for( i = 0; i < 8; i++ )
1052 block_residual_write_cabac( h, cb, DCT_CHROMA_AC, i, h->dct.block[16+i].residual_ac, 15 );
1057 if( IS_INTRA( i_mb_type ) )
1058 h->stat.frame.i_itex_bits += x264_cabac_pos( cb ) - i_mb_pos_tex;
1060 h->stat.frame.i_ptex_bits += x264_cabac_pos( cb ) - i_mb_pos_tex;
1065 /*****************************************************************************
1066 * RD only; doesn't generate a valid bitstream
1067 * doesn't write cbp or chroma dc (I don't know how much this matters)
1068 * works on all partition sizes except 16x16
1069 * for sub8x8, call once per 8x8 block
1070 *****************************************************************************/
1071 void x264_partition_size_cabac( x264_t *h, x264_cabac_t *cb, int i8, int i_pixel )
1073 const int i_mb_type = h->mb.i_type;
1076 if( i_mb_type == P_8x8 )
1078 x264_cabac_mb_sub_p_partition( cb, h->mb.i_sub_partition[i8] );
1079 if( h->mb.pic.i_fref[0] > 1 )
1080 x264_cabac_mb_ref( h, cb, 0, 4*i8 );
1081 x264_cabac_mb8x8_mvd( h, cb, 0, i8 );
1083 else if( i_mb_type == P_L0 )
1085 if( h->mb.pic.i_fref[0] > 1 )
1086 x264_cabac_mb_ref( h, cb, 0, 4*i8 );
1087 if( h->mb.i_partition == D_16x8 )
1088 x264_cabac_mb_mvd( h, cb, 0, 4*i8, 4, 2 );
1090 x264_cabac_mb_mvd( h, cb, 0, 4*i8, 2, 4 );
1092 else if( i_mb_type == B_8x8 )
1094 x264_cabac_mb_sub_b_partition( cb, h->mb.i_sub_partition[i8] );
1096 if( h->mb.pic.i_fref[0] > 1
1097 && x264_mb_partition_listX_table[0][ h->mb.i_sub_partition[i8] ] )
1098 x264_cabac_mb_ref( h, cb, 0, 4*i8 );
1099 if( h->mb.pic.i_fref[1] > 1
1100 && x264_mb_partition_listX_table[1][ h->mb.i_sub_partition[i8] ] )
1101 x264_cabac_mb_ref( h, cb, 1, 4*i8 );
1103 x264_cabac_mb8x8_mvd( h, cb, 0, i8 );
1104 x264_cabac_mb8x8_mvd( h, cb, 1, i8 );
1108 x264_log(h, X264_LOG_ERROR, "invalid/unhandled mb_type\n" );
1112 for( j = (i_pixel < PIXEL_8x8); j >= 0; j-- )
1114 if( h->mb.i_cbp_luma & (1 << i8) )
1116 if( h->mb.b_transform_8x8 )
1117 block_residual_write_cabac( h, cb, DCT_LUMA_8x8, i8, h->dct.luma8x8[i8], 64 );
1121 for( i4 = 0; i4 < 4; i4++ )
1122 block_residual_write_cabac( h, cb, DCT_LUMA_4x4, i4+i8*4, h->dct.block[i4+i8*4].luma4x4, 16 );
1126 block_residual_write_cabac( h, cb, DCT_CHROMA_AC, i8, h->dct.block[16+i8 ].residual_ac, 15 );
1127 block_residual_write_cabac( h, cb, DCT_CHROMA_AC, i8+4, h->dct.block[16+i8+4].residual_ac, 15 );
1129 i8 += x264_pixel_size[i_pixel].h >> 3;
1133 static void x264_partition_i8x8_size_cabac( x264_t *h, x264_cabac_t *cb, int i8, int i_mode )
1135 const int i_pred = x264_mb_predict_intra4x4_mode( h, 4*i8 );
1136 i_mode = x264_mb_pred_mode4x4_fix( i_mode );
1137 x264_cabac_mb_intra4x4_pred_mode( cb, i_pred, i_mode );
1138 block_residual_write_cabac( h, cb, DCT_LUMA_8x8, 4*i8, h->dct.luma8x8[i8], 64 );
1141 static void x264_partition_i4x4_size_cabac( x264_t *h, x264_cabac_t *cb, int i4, int i_mode )
1143 const int i_pred = x264_mb_predict_intra4x4_mode( h, i4 );
1144 i_mode = x264_mb_pred_mode4x4_fix( i_mode );
1145 x264_cabac_mb_intra4x4_pred_mode( cb, i_pred, i_mode );
1146 block_residual_write_cabac( h, cb, DCT_LUMA_4x4, i4, h->dct.block[i4].luma4x4, 16 );
1149 static void x264_i8x8_chroma_size_cabac( x264_t *h, x264_cabac_t *cb )
1151 x264_cabac_mb_intra_chroma_pred_mode( h, cb );
1152 if( h->mb.i_cbp_chroma > 0 )
1154 block_residual_write_cabac( h, cb, DCT_CHROMA_DC, 0, h->dct.chroma_dc[0], 4 );
1155 block_residual_write_cabac( h, cb, DCT_CHROMA_DC, 1, h->dct.chroma_dc[1], 4 );
1157 if( h->mb.i_cbp_chroma == 2 )
1160 for( i = 0; i < 8; i++ )
1161 block_residual_write_cabac( h, cb, DCT_CHROMA_AC, i, h->dct.block[16+i].residual_ac, 15 );