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 *****************************************************************************/
28 #include "common/common.h"
29 #include "macroblock.h"
31 static const uint8_t block_idx_x[16] =
33 0, 1, 0, 1, 2, 3, 2, 3, 0, 1, 0, 1, 2, 3, 2, 3
35 static const uint8_t block_idx_y[16] =
37 0, 0, 1, 1, 0, 0, 1, 1, 2, 2, 3, 3, 2, 2, 3, 3
39 static const uint8_t block_idx_xy[4][4] =
47 static inline void x264_cabac_mb_type_intra( x264_t *h, int i_mb_type,
48 int ctx0, int ctx1, int ctx2, int ctx3, int ctx4, int ctx5 )
50 if( i_mb_type == I_4x4 || i_mb_type == I_8x8 )
52 x264_cabac_encode_decision( &h->cabac, ctx0, 0 );
54 else if( i_mb_type == I_PCM )
56 x264_cabac_encode_decision( &h->cabac, ctx0, 1 );
57 x264_cabac_encode_terminal( &h->cabac, 1 );
61 x264_cabac_encode_decision( &h->cabac, ctx0, 1 );
62 x264_cabac_encode_terminal( &h->cabac, 0 );
64 x264_cabac_encode_decision( &h->cabac, ctx1, ( h->mb.i_cbp_luma == 0 ? 0 : 1 ));
65 if( h->mb.i_cbp_chroma == 0 )
67 x264_cabac_encode_decision( &h->cabac, ctx2, 0 );
71 x264_cabac_encode_decision( &h->cabac, ctx2, 1 );
72 x264_cabac_encode_decision( &h->cabac, ctx3, ( h->mb.i_cbp_chroma == 1 ? 0 : 1 ) );
74 x264_cabac_encode_decision( &h->cabac, ctx4, ( (h->mb.i_intra16x16_pred_mode / 2) ? 1 : 0 ));
75 x264_cabac_encode_decision( &h->cabac, ctx5, ( (h->mb.i_intra16x16_pred_mode % 2) ? 1 : 0 ));
79 static void x264_cabac_mb_type( x264_t *h )
81 const int i_mb_type = h->mb.i_type;
83 if( h->sh.i_type == SLICE_TYPE_I )
86 if( h->mb.i_mb_type_left >= 0 && h->mb.i_mb_type_left != I_4x4 )
90 if( h->mb.i_mb_type_top >= 0 && h->mb.i_mb_type_top != I_4x4 )
95 x264_cabac_mb_type_intra( h, i_mb_type, 3+ctx, 3+3, 3+4, 3+5, 3+6, 3+7 );
97 else if( h->sh.i_type == SLICE_TYPE_P )
99 /* prefix: 14, suffix: 17 */
100 if( i_mb_type == P_L0 )
102 if( h->mb.i_partition == D_16x16 )
104 x264_cabac_encode_decision( &h->cabac, 14, 0 );
105 x264_cabac_encode_decision( &h->cabac, 15, 0 );
106 x264_cabac_encode_decision( &h->cabac, 16, 0 );
108 else if( h->mb.i_partition == D_16x8 )
110 x264_cabac_encode_decision( &h->cabac, 14, 0 );
111 x264_cabac_encode_decision( &h->cabac, 15, 1 );
112 x264_cabac_encode_decision( &h->cabac, 17, 1 );
114 else if( h->mb.i_partition == D_8x16 )
116 x264_cabac_encode_decision( &h->cabac, 14, 0 );
117 x264_cabac_encode_decision( &h->cabac, 15, 1 );
118 x264_cabac_encode_decision( &h->cabac, 17, 0 );
121 else if( i_mb_type == P_8x8 )
123 x264_cabac_encode_decision( &h->cabac, 14, 0 );
124 x264_cabac_encode_decision( &h->cabac, 15, 0 );
125 x264_cabac_encode_decision( &h->cabac, 16, 1 );
130 x264_cabac_encode_decision( &h->cabac, 14, 1 );
133 x264_cabac_mb_type_intra( h, i_mb_type, 17+0, 17+1, 17+2, 17+2, 17+3, 17+3 );
136 else if( h->sh.i_type == SLICE_TYPE_B )
139 if( h->mb.i_mb_type_left >= 0 && h->mb.i_mb_type_left != B_SKIP && h->mb.i_mb_type_left != B_DIRECT )
143 if( h->mb.i_mb_type_top >= 0 && h->mb.i_mb_type_top != B_SKIP && h->mb.i_mb_type_top != B_DIRECT )
148 if( i_mb_type == B_DIRECT )
150 x264_cabac_encode_decision( &h->cabac, 27+ctx, 0 );
152 else if( i_mb_type == B_8x8 )
154 x264_cabac_encode_decision( &h->cabac, 27+ctx, 1 );
155 x264_cabac_encode_decision( &h->cabac, 27+3, 1 );
156 x264_cabac_encode_decision( &h->cabac, 27+4, 1 );
158 x264_cabac_encode_decision( &h->cabac, 27+5, 1 );
159 x264_cabac_encode_decision( &h->cabac, 27+5, 1 );
160 x264_cabac_encode_decision( &h->cabac, 27+5, 1 );
162 else if( IS_INTRA( i_mb_type ) )
165 x264_cabac_encode_decision( &h->cabac, 27+ctx, 1 );
166 x264_cabac_encode_decision( &h->cabac, 27+3, 1 );
167 x264_cabac_encode_decision( &h->cabac, 27+4, 1 );
169 x264_cabac_encode_decision( &h->cabac, 27+5, 1 );
170 x264_cabac_encode_decision( &h->cabac, 27+5, 0 );
171 x264_cabac_encode_decision( &h->cabac, 27+5, 1 );
174 x264_cabac_mb_type_intra( h, i_mb_type, 32+0, 32+1, 32+2, 32+2, 32+3, 32+3 );
178 static const int i_mb_len[21] =
191 static const int i_mb_bits[21][7] =
193 { 1, 0, 0, }, { 1, 1, 0, 0, 0, 1, }, { 1, 1, 0, 0, 1, 0, }, /* L0 L0 */
194 { 1, 0, 1, }, { 1, 1, 0, 0, 1, 1, }, { 1, 1, 0, 1, 0, 0, }, /* L1 L1 */
195 { 1, 1, 0, 0, 0, 0 ,}, { 1, 1, 1, 1, 0, 0 , 0 }, { 1, 1, 1, 1, 0, 0 , 1 },/* BI BI */
197 { 1, 1, 0, 1, 0, 1, }, { 1, 1, 0, 1, 1, 0, }, /* L0 L1 */
198 { 1, 1, 0, 1, 1, 1, }, { 1, 1, 1, 1, 1, 0, }, /* L1 L0 */
199 { 1, 1, 1, 0, 0, 0, 0 }, { 1, 1, 1, 0, 0, 0, 1 }, /* L0 BI */
200 { 1, 1, 1, 0, 0, 1, 0 }, { 1, 1, 1, 0, 0, 1, 1 }, /* L1 BI */
201 { 1, 1, 1, 0, 1, 0, 0 }, { 1, 1, 1, 0, 1, 0, 1 }, /* BI L0 */
202 { 1, 1, 1, 0, 1, 1, 0 }, { 1, 1, 1, 0, 1, 1, 1 } /* BI L1 */
205 const int i_partition = h->mb.i_partition;
210 /* D_16x16, D_16x8, D_8x16 */
211 case B_BI_BI: idx += 3;
212 case B_L1_L1: idx += 3;
214 if( i_partition == D_16x8 )
216 else if( i_partition == D_8x16 )
221 case B_BI_L1: idx += 2;
222 case B_BI_L0: idx += 2;
223 case B_L1_BI: idx += 2;
224 case B_L0_BI: idx += 2;
225 case B_L1_L0: idx += 2;
228 if( i_partition == D_8x16 )
232 x264_log(h, X264_LOG_ERROR, "error in B mb type\n" );
236 x264_cabac_encode_decision( &h->cabac, 27+ctx, i_mb_bits[idx][0] );
237 x264_cabac_encode_decision( &h->cabac, 27+3, i_mb_bits[idx][1] );
238 x264_cabac_encode_decision( &h->cabac, 27+(i_mb_bits[idx][1] != 0 ? 4 : 5), i_mb_bits[idx][2] );
239 for( i = 3; i < i_mb_len[idx]; i++ )
241 x264_cabac_encode_decision( &h->cabac, 27+5, i_mb_bits[idx][i] );
247 x264_log(h, X264_LOG_ERROR, "unknown SLICE_TYPE unsupported in x264_macroblock_write_cabac\n" );
251 static void x264_cabac_mb_intra4x4_pred_mode( x264_t *h, int i_pred, int i_mode )
253 if( i_pred == i_mode )
255 /* b_prev_intra4x4_pred_mode */
256 x264_cabac_encode_decision( &h->cabac, 68, 1 );
260 /* b_prev_intra4x4_pred_mode */
261 x264_cabac_encode_decision( &h->cabac, 68, 0 );
262 if( i_mode > i_pred )
266 x264_cabac_encode_decision( &h->cabac, 69, (i_mode )&0x01 );
267 x264_cabac_encode_decision( &h->cabac, 69, (i_mode >> 1)&0x01 );
268 x264_cabac_encode_decision( &h->cabac, 69, (i_mode >> 2)&0x01 );
272 static void x264_cabac_mb_intra_chroma_pred_mode( x264_t *h )
274 const int i_mode = h->mb.i_chroma_pred_mode;
277 /* No need to test for I4x4 or I_16x16 as cache_save handle that */
278 if( (h->mb.i_neighbour & MB_LEFT) && h->mb.chroma_pred_mode[h->mb.i_mb_xy - 1] != 0 )
282 if( (h->mb.i_neighbour & MB_TOP) && h->mb.chroma_pred_mode[h->mb.i_mb_xy - h->mb.i_mb_stride] != 0 )
289 x264_cabac_encode_decision( &h->cabac, 64 + ctx, 0 );
293 x264_cabac_encode_decision( &h->cabac, 64 + ctx, 1 );
294 x264_cabac_encode_decision( &h->cabac, 64 + 3, ( i_mode == 1 ? 0 : 1 ) );
297 x264_cabac_encode_decision( &h->cabac, 64 + 3, ( i_mode == 2 ? 0 : 1 ) );
302 static void x264_cabac_mb_cbp_luma( x264_t *h )
304 /* TODO: clean up and optimize */
306 for( i8x8 = 0; i8x8 < 4; i8x8++ )
310 int x = block_idx_x[4*i8x8];
311 int y = block_idx_y[4*i8x8];
315 i_mba_xy = h->mb.i_mb_xy;
316 else if( h->mb.i_neighbour & MB_LEFT )
317 i_mba_xy = h->mb.i_mb_xy - 1;
320 i_mbb_xy = h->mb.i_mb_xy;
321 else if( h->mb.i_neighbour & MB_TOP )
322 i_mbb_xy = h->mb.i_mb_xy - h->mb.i_mb_stride;
325 /* No need to test for PCM and SKIP */
328 const int i8x8a = block_idx_xy[(x-1)&0x03][y]/4;
329 if( ((h->mb.cbp[i_mba_xy] >> i8x8a)&0x01) == 0 )
337 const int i8x8b = block_idx_xy[x][(y-1)&0x03]/4;
338 if( ((h->mb.cbp[i_mbb_xy] >> i8x8b)&0x01) == 0 )
344 x264_cabac_encode_decision( &h->cabac, 73 + ctx, (h->mb.i_cbp_luma >> i8x8)&0x01 );
348 static void x264_cabac_mb_cbp_chroma( x264_t *h )
354 /* No need to test for SKIP/PCM */
355 if( h->mb.i_neighbour & MB_LEFT )
357 cbp_a = (h->mb.cbp[h->mb.i_mb_xy - 1] >> 4)&0x3;
360 if( h->mb.i_neighbour & MB_TOP )
362 cbp_b = (h->mb.cbp[h->mb.i_mb_xy - h->mb.i_mb_stride] >> 4)&0x3;
366 if( cbp_a > 0 ) ctx++;
367 if( cbp_b > 0 ) ctx += 2;
368 if( h->mb.i_cbp_chroma == 0 )
370 x264_cabac_encode_decision( &h->cabac, 77 + ctx, 0 );
374 x264_cabac_encode_decision( &h->cabac, 77 + ctx, 1 );
377 if( cbp_a == 2 ) ctx++;
378 if( cbp_b == 2 ) ctx += 2;
379 x264_cabac_encode_decision( &h->cabac, 77 + ctx, h->mb.i_cbp_chroma > 1 ? 1 : 0 );
383 /* TODO check it with != qp per mb */
384 static void x264_cabac_mb_qp_delta( x264_t *h )
386 int i_mbn_xy = h->mb.i_mb_xy - 1;
387 int i_dqp = h->mb.qp[h->mb.i_mb_xy] - h->mb.i_last_qp;
388 int val = i_dqp <= 0 ? (-2*i_dqp) : (2*i_dqp - 1);
391 /* No need to test for PCM / SKIP */
392 if( i_mbn_xy >= h->sh.i_first_mb && h->mb.i_last_dqp != 0 &&
393 ( h->mb.type[i_mbn_xy] == I_16x16 || (h->mb.cbp[i_mbn_xy]&0x3f) ) )
400 x264_cabac_encode_decision( &h->cabac, 60 + ctx, 1 );
407 x264_cabac_encode_decision( &h->cabac, 60 + ctx, 0 );
410 void x264_cabac_mb_skip( x264_t *h, int b_skip )
414 if( h->mb.i_mb_type_left >= 0 && !IS_SKIP( h->mb.i_mb_type_left ) )
418 if( h->mb.i_mb_type_top >= 0 && !IS_SKIP( h->mb.i_mb_type_top ) )
423 if( h->sh.i_type == SLICE_TYPE_P )
424 x264_cabac_encode_decision( &h->cabac, 11 + ctx, b_skip ? 1 : 0 );
425 else /* SLICE_TYPE_B */
426 x264_cabac_encode_decision( &h->cabac, 24 + ctx, b_skip ? 1 : 0 );
429 static inline void x264_cabac_mb_sub_p_partition( x264_t *h, int i_sub )
431 if( i_sub == D_L0_8x8 )
433 x264_cabac_encode_decision( &h->cabac, 21, 1 );
435 else if( i_sub == D_L0_8x4 )
437 x264_cabac_encode_decision( &h->cabac, 21, 0 );
438 x264_cabac_encode_decision( &h->cabac, 22, 0 );
440 else if( i_sub == D_L0_4x8 )
442 x264_cabac_encode_decision( &h->cabac, 21, 0 );
443 x264_cabac_encode_decision( &h->cabac, 22, 1 );
444 x264_cabac_encode_decision( &h->cabac, 23, 1 );
446 else if( i_sub == D_L0_4x4 )
448 x264_cabac_encode_decision( &h->cabac, 21, 0 );
449 x264_cabac_encode_decision( &h->cabac, 22, 1 );
450 x264_cabac_encode_decision( &h->cabac, 23, 0 );
454 static inline void x264_cabac_mb_sub_b_partition( x264_t *h, int i_sub )
456 if( i_sub == D_DIRECT_8x8 )
458 x264_cabac_encode_decision( &h->cabac, 36, 0 );
460 else if( i_sub == D_L0_8x8 )
462 x264_cabac_encode_decision( &h->cabac, 36, 1 );
463 x264_cabac_encode_decision( &h->cabac, 37, 0 );
464 x264_cabac_encode_decision( &h->cabac, 39, 0 );
466 else if( i_sub == D_L1_8x8 )
468 x264_cabac_encode_decision( &h->cabac, 36, 1 );
469 x264_cabac_encode_decision( &h->cabac, 37, 0 );
470 x264_cabac_encode_decision( &h->cabac, 39, 1 );
472 else if( i_sub == D_BI_8x8 )
474 x264_cabac_encode_decision( &h->cabac, 36, 1 );
475 x264_cabac_encode_decision( &h->cabac, 37, 1 );
476 x264_cabac_encode_decision( &h->cabac, 38, 0 );
477 x264_cabac_encode_decision( &h->cabac, 39, 0 );
478 x264_cabac_encode_decision( &h->cabac, 39, 0 );
480 else if( i_sub == D_L0_8x4 )
482 x264_cabac_encode_decision( &h->cabac, 36, 1 );
483 x264_cabac_encode_decision( &h->cabac, 37, 1 );
484 x264_cabac_encode_decision( &h->cabac, 38, 0 );
485 x264_cabac_encode_decision( &h->cabac, 39, 0 );
486 x264_cabac_encode_decision( &h->cabac, 39, 1 );
488 else if( i_sub == D_L0_4x8 )
490 x264_cabac_encode_decision( &h->cabac, 36, 1 );
491 x264_cabac_encode_decision( &h->cabac, 37, 1 );
492 x264_cabac_encode_decision( &h->cabac, 38, 0 );
493 x264_cabac_encode_decision( &h->cabac, 39, 1 );
494 x264_cabac_encode_decision( &h->cabac, 39, 0 );
496 else if( i_sub == D_L1_8x4 )
498 x264_cabac_encode_decision( &h->cabac, 36, 1 );
499 x264_cabac_encode_decision( &h->cabac, 37, 1 );
500 x264_cabac_encode_decision( &h->cabac, 38, 0 );
501 x264_cabac_encode_decision( &h->cabac, 39, 1 );
502 x264_cabac_encode_decision( &h->cabac, 39, 1 );
504 else if( i_sub == D_L1_4x8 )
506 x264_cabac_encode_decision( &h->cabac, 36, 1 );
507 x264_cabac_encode_decision( &h->cabac, 37, 1 );
508 x264_cabac_encode_decision( &h->cabac, 38, 1 );
509 x264_cabac_encode_decision( &h->cabac, 39, 0 );
510 x264_cabac_encode_decision( &h->cabac, 39, 0 );
511 x264_cabac_encode_decision( &h->cabac, 39, 0 );
513 else if( i_sub == D_BI_8x4 )
515 x264_cabac_encode_decision( &h->cabac, 36, 1 );
516 x264_cabac_encode_decision( &h->cabac, 37, 1 );
517 x264_cabac_encode_decision( &h->cabac, 38, 1 );
518 x264_cabac_encode_decision( &h->cabac, 39, 0 );
519 x264_cabac_encode_decision( &h->cabac, 39, 0 );
520 x264_cabac_encode_decision( &h->cabac, 39, 1 );
522 else if( i_sub == D_BI_4x8 )
524 x264_cabac_encode_decision( &h->cabac, 36, 1 );
525 x264_cabac_encode_decision( &h->cabac, 37, 1 );
526 x264_cabac_encode_decision( &h->cabac, 38, 1 );
527 x264_cabac_encode_decision( &h->cabac, 39, 0 );
528 x264_cabac_encode_decision( &h->cabac, 39, 1 );
529 x264_cabac_encode_decision( &h->cabac, 39, 0 );
531 else if( i_sub == D_L0_4x4 )
533 x264_cabac_encode_decision( &h->cabac, 36, 1 );
534 x264_cabac_encode_decision( &h->cabac, 37, 1 );
535 x264_cabac_encode_decision( &h->cabac, 38, 1 );
536 x264_cabac_encode_decision( &h->cabac, 39, 0 );
537 x264_cabac_encode_decision( &h->cabac, 39, 1 );
538 x264_cabac_encode_decision( &h->cabac, 39, 1 );
540 else if( i_sub == D_L1_4x4 )
542 x264_cabac_encode_decision( &h->cabac, 36, 1 );
543 x264_cabac_encode_decision( &h->cabac, 37, 1 );
544 x264_cabac_encode_decision( &h->cabac, 38, 1 );
545 x264_cabac_encode_decision( &h->cabac, 39, 1 );
546 x264_cabac_encode_decision( &h->cabac, 39, 0 );
548 else if( i_sub == D_BI_4x4 )
550 x264_cabac_encode_decision( &h->cabac, 36, 1 );
551 x264_cabac_encode_decision( &h->cabac, 37, 1 );
552 x264_cabac_encode_decision( &h->cabac, 38, 1 );
553 x264_cabac_encode_decision( &h->cabac, 39, 1 );
554 x264_cabac_encode_decision( &h->cabac, 39, 1 );
558 static inline void x264_cabac_mb_transform_size( x264_t *h )
560 int ctx = 399 + h->mb.cache.i_neighbour_transform_size;
561 x264_cabac_encode_decision( &h->cabac, ctx, h->mb.b_transform_8x8 );
564 static inline void x264_cabac_mb_ref( x264_t *h, int i_list, int idx )
566 const int i8 = x264_scan8[idx];
567 const int i_refa = h->mb.cache.ref[i_list][i8 - 1];
568 const int i_refb = h->mb.cache.ref[i_list][i8 - 8];
569 int i_ref = h->mb.cache.ref[i_list][i8];
572 if( i_refa > 0 && !h->mb.cache.skip[i8 - 1])
574 if( i_refb > 0 && !h->mb.cache.skip[i8 - 8])
579 x264_cabac_encode_decision( &h->cabac, 54 + ctx, 1 );
587 x264_cabac_encode_decision( &h->cabac, 54 + ctx, 0 );
592 static inline void x264_cabac_mb_mvd_cpn( x264_t *h, int i_list, int idx, int l, int mvd )
594 const int amvd = abs( h->mb.cache.mvd[i_list][x264_scan8[idx] - 1][l] ) +
595 abs( h->mb.cache.mvd[i_list][x264_scan8[idx] - 8][l] );
596 const int i_abs = abs( mvd );
597 const int i_prefix = X264_MIN( i_abs, 9 );
598 const int ctxbase = (l == 0 ? 40 : 47);
610 for( i = 0; i < i_prefix; i++ )
612 x264_cabac_encode_decision( &h->cabac, ctxbase + ctx, 1 );
620 x264_cabac_encode_decision( &h->cabac, ctxbase + ctx, 0 );
625 int i_suffix = i_abs - 9;
628 while( i_suffix >= (1<<k) )
630 x264_cabac_encode_bypass( &h->cabac, 1 );
634 x264_cabac_encode_bypass( &h->cabac, 0 );
637 x264_cabac_encode_bypass( &h->cabac, (i_suffix >> k)&0x01 );
643 x264_cabac_encode_bypass( &h->cabac, 0 );
645 x264_cabac_encode_bypass( &h->cabac, 1 );
648 static inline void x264_cabac_mb_mvd( x264_t *h, int i_list, int idx, int width, int height )
654 x264_mb_predict_mv( h, i_list, idx, width, mvp );
655 mdx = h->mb.cache.mv[i_list][x264_scan8[idx]][0] - mvp[0];
656 mdy = h->mb.cache.mv[i_list][x264_scan8[idx]][1] - mvp[1];
659 x264_cabac_mb_mvd_cpn( h, i_list, idx, 0, mdx );
660 x264_cabac_mb_mvd_cpn( h, i_list, idx, 1, mdy );
663 x264_macroblock_cache_mvd( h, block_idx_x[idx], block_idx_y[idx], width, height, i_list, mdx, mdy );
666 static inline void x264_cabac_mb8x8_mvd( x264_t *h, int i_list )
669 for( i = 0; i < 4; i++ )
671 if( !x264_mb_partition_listX_table[i_list][ h->mb.i_sub_partition[i] ] )
676 switch( h->mb.i_sub_partition[i] )
681 x264_cabac_mb_mvd( h, i_list, 4*i, 2, 2 );
686 x264_cabac_mb_mvd( h, i_list, 4*i+0, 2, 1 );
687 x264_cabac_mb_mvd( h, i_list, 4*i+2, 2, 1 );
692 x264_cabac_mb_mvd( h, i_list, 4*i+0, 1, 2 );
693 x264_cabac_mb_mvd( h, i_list, 4*i+1, 1, 2 );
698 x264_cabac_mb_mvd( h, i_list, 4*i+0, 1, 1 );
699 x264_cabac_mb_mvd( h, i_list, 4*i+1, 1, 1 );
700 x264_cabac_mb_mvd( h, i_list, 4*i+2, 1, 1 );
701 x264_cabac_mb_mvd( h, i_list, 4*i+3, 1, 1 );
707 static int x264_cabac_mb_cbf_ctxidxinc( x264_t *h, int i_cat, int i_idx )
709 /* TODO: clean up/optimize */
718 if( h->mb.i_neighbour & MB_LEFT )
720 i_mba_xy = h->mb.i_mb_xy -1;
721 if( h->mb.type[i_mba_xy] == I_16x16 )
723 i_nza = h->mb.cbp[i_mba_xy]&0x100;
726 if( h->mb.i_neighbour & MB_TOP )
728 i_mbb_xy = h->mb.i_mb_xy - h->mb.i_mb_stride;
729 if( h->mb.type[i_mbb_xy] == I_16x16 )
731 i_nzb = h->mb.cbp[i_mbb_xy]&0x100;
735 else if( i_cat == 1 || i_cat == 2 )
737 int x = block_idx_x[i_idx];
738 int y = block_idx_y[i_idx];
741 i_mba_xy = h->mb.i_mb_xy;
742 else if( h->mb.i_neighbour & MB_LEFT )
743 i_mba_xy = h->mb.i_mb_xy -1;
746 i_mbb_xy = h->mb.i_mb_xy;
747 else if( h->mb.i_neighbour & MB_TOP )
748 i_mbb_xy = h->mb.i_mb_xy - h->mb.i_mb_stride;
750 /* no need to test for skip/pcm */
753 const int i8x8a = block_idx_xy[(x-1)&0x03][y]/4;
754 if( (h->mb.cbp[i_mba_xy]&0x0f)>> i8x8a )
756 i_nza = h->mb.cache.non_zero_count[x264_scan8[i_idx] - 1];
761 const int i8x8b = block_idx_xy[x][(y-1)&0x03]/4;
762 if( (h->mb.cbp[i_mbb_xy]&0x0f)>> i8x8b )
764 i_nzb = h->mb.cache.non_zero_count[x264_scan8[i_idx] - 8];
768 else if( i_cat == 3 )
770 /* no need to test skip/pcm */
771 if( h->mb.i_neighbour & MB_LEFT )
773 i_mba_xy = h->mb.i_mb_xy -1;
774 if( h->mb.cbp[i_mba_xy]&0x30 )
776 i_nza = h->mb.cbp[i_mba_xy]&( 0x02 << ( 8 + i_idx) );
779 if( h->mb.i_neighbour & MB_TOP )
781 i_mbb_xy = h->mb.i_mb_xy - h->mb.i_mb_stride;
782 if( h->mb.cbp[i_mbb_xy]&0x30 )
784 i_nzb = h->mb.cbp[i_mbb_xy]&( 0x02 << ( 8 + i_idx) );
788 else if( i_cat == 4 )
792 if( idxc == 1 || idxc == 3 )
793 i_mba_xy = h->mb.i_mb_xy;
794 else if( h->mb.i_neighbour & MB_LEFT )
795 i_mba_xy = h->mb.i_mb_xy - 1;
797 if( idxc == 2 || idxc == 3 )
798 i_mbb_xy = h->mb.i_mb_xy;
799 else if( h->mb.i_neighbour & MB_TOP )
800 i_mbb_xy = h->mb.i_mb_xy - h->mb.i_mb_stride;
802 /* no need to test skip/pcm */
803 if( i_mba_xy >= 0 && (h->mb.cbp[i_mba_xy]&0x30) == 0x20 )
805 i_nza = h->mb.cache.non_zero_count[x264_scan8[16+i_idx] - 1];
807 if( i_mbb_xy >= 0 && (h->mb.cbp[i_mbb_xy]&0x30) == 0x20 )
809 i_nzb = h->mb.cache.non_zero_count[x264_scan8[16+i_idx] - 8];
813 if( ( i_mba_xy < 0 && IS_INTRA( h->mb.i_type ) ) || i_nza > 0 )
817 if( ( i_mbb_xy < 0 && IS_INTRA( h->mb.i_type ) ) || i_nzb > 0 )
822 return 4 * i_cat + ctx;
826 static void block_residual_write_cabac( x264_t *h, int i_ctxBlockCat, int i_idx, int *l, int i_count )
828 static const int significant_coeff_flag_offset[6] = { 0, 15, 29, 44, 47, 297 };
829 static const int last_significant_coeff_flag_offset[6] = { 0, 15, 29, 44, 47, 251 };
830 static const int coeff_abs_level_m1_offset[6] = { 0, 10, 20, 30, 39, 199 };
831 static const int significant_coeff_flag_offset_8x8[63] = {
832 0, 1, 2, 3, 4, 5, 5, 4, 4, 3, 3, 4, 4, 4, 5, 5,
833 4, 4, 4, 4, 3, 3, 6, 7, 7, 7, 8, 9,10, 9, 8, 7,
834 7, 6,11,12,13,11, 6, 7, 8, 9,14,10, 9, 8, 6,11,
835 12,13,11, 6, 9,14,10, 9,11,12,13,11,14,10,12
837 static const int last_significant_coeff_flag_offset_8x8[63] = {
838 0, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1,
839 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2,
840 3, 3, 3, 3, 3, 3, 3, 3, 4, 4, 4, 4, 4, 4, 4, 4,
841 5, 5, 5, 5, 6, 6, 6, 6, 7, 7, 7, 7, 8, 8, 8
844 int i_coeff_abs_m1[64];
845 int i_coeff_sign[64];
850 int i_abslevelgt1 = 0;
854 /* i_ctxBlockCat: 0-> DC 16x16 i_idx = 0
855 * 1-> AC 16x16 i_idx = luma4x4idx
856 * 2-> Luma4x4 i_idx = luma4x4idx
857 * 3-> DC Chroma i_idx = iCbCr
858 * 4-> AC Chroma i_idx = 4 * iCbCr + chroma4x4idx
859 * 5-> Luma8x8 i_idx = luma8x8idx
862 for( i = 0; i < i_count; i++ )
866 i_coeff_abs_m1[i_coeff] = abs( l[i] ) - 1;
867 i_coeff_sign[i_coeff] = ( l[i] < 0 );
876 /* coded block flag */
877 x264_cabac_encode_decision( &h->cabac, 85 + x264_cabac_mb_cbf_ctxidxinc( h, i_ctxBlockCat, i_idx ), i_coeff != 0 );
882 for( i = 0; i < i_count - 1; i++ )
885 int i_last_ctxIdxInc;
887 if( i_ctxBlockCat == 5 )
889 i_sig_ctxIdxInc = significant_coeff_flag_offset_8x8[i];
890 i_last_ctxIdxInc = last_significant_coeff_flag_offset_8x8[i];
893 i_sig_ctxIdxInc = i_last_ctxIdxInc = i;
897 x264_cabac_encode_decision( &h->cabac, 105 + significant_coeff_flag_offset[i_ctxBlockCat] + i_sig_ctxIdxInc, 1 );
898 x264_cabac_encode_decision( &h->cabac, 166 + last_significant_coeff_flag_offset[i_ctxBlockCat] + i_last_ctxIdxInc, i == i_last ? 1 : 0 );
902 x264_cabac_encode_decision( &h->cabac, 105 + significant_coeff_flag_offset[i_ctxBlockCat] + i_sig_ctxIdxInc, 0 );
910 for( i = i_coeff - 1; i >= 0; i-- )
915 /* write coeff_abs - 1 */
918 i_prefix = X264_MIN( i_coeff_abs_m1[i], 14 );
920 i_ctxIdxInc = (i_abslevelgt1 != 0 ? 0 : X264_MIN( 4, i_abslevel1 + 1 )) + coeff_abs_level_m1_offset[i_ctxBlockCat];
923 x264_cabac_encode_decision( &h->cabac, 227 + i_ctxIdxInc, 0 );
928 x264_cabac_encode_decision( &h->cabac, 227 + i_ctxIdxInc, 1 );
929 i_ctxIdxInc = 5 + X264_MIN( 4, i_abslevelgt1 ) + coeff_abs_level_m1_offset[i_ctxBlockCat];
930 for( j = 0; j < i_prefix - 1; j++ )
931 x264_cabac_encode_decision( &h->cabac, 227 + i_ctxIdxInc, 1 );
933 x264_cabac_encode_decision( &h->cabac, 227 + i_ctxIdxInc, 0 );
936 if( i_coeff_abs_m1[i] >= 14 )
939 int i_suffix = i_coeff_abs_m1[i] - 14;
941 while( i_suffix >= (1<<k) )
943 x264_cabac_encode_bypass( &h->cabac, 1 );
947 x264_cabac_encode_bypass( &h->cabac, 0 );
949 x264_cabac_encode_bypass( &h->cabac, (i_suffix >> k)&0x01 );
953 x264_cabac_encode_bypass( &h->cabac, i_coeff_sign[i] );
955 if( i_coeff_abs_m1[i] == 0 )
964 void x264_macroblock_write_cabac( x264_t *h, bs_t *s )
966 const int i_mb_type = h->mb.i_type;
967 const int i_mb_pos_start = bs_pos( s );
973 /* Write the MB type */
974 x264_cabac_mb_type( h );
976 /* PCM special block type UNTESTED */
977 if( i_mb_type == I_PCM )
979 bs_align_0( s ); /* not sure */
981 for( i = 0; i < 16*16; i++ )
983 const int x = 16 * h->mb.i_mb_x + (i % 16);
984 const int y = 16 * h->mb.i_mb_y + (i / 16);
985 bs_write( s, 8, h->fenc->plane[0][y*h->mb.pic.i_stride[0]+x] );
988 for( i = 0; i < 8*8; i++ )
990 const int x = 8 * h->mb.i_mb_x + (i % 8);
991 const int y = 8 * h->mb.i_mb_y + (i / 8);
992 bs_write( s, 8, h->fenc->plane[1][y*h->mb.pic.i_stride[1]+x] );
995 for( i = 0; i < 8*8; i++ )
997 const int x = 8 * h->mb.i_mb_x + (i % 8);
998 const int y = 8 * h->mb.i_mb_y + (i / 8);
999 bs_write( s, 8, h->fenc->plane[2][y*h->mb.pic.i_stride[2]+x] );
1001 x264_cabac_encode_init( &h->cabac, s );
1005 if( IS_INTRA( i_mb_type ) )
1007 if( h->pps->b_transform_8x8_mode && i_mb_type != I_16x16 )
1008 x264_cabac_mb_transform_size( h );
1010 if( i_mb_type != I_16x16 )
1012 int di = (i_mb_type == I_8x8) ? 4 : 1;
1013 for( i = 0; i < 16; i += di )
1015 const int i_pred = x264_mb_predict_intra4x4_mode( h, i );
1016 const int i_mode = h->mb.cache.intra4x4_pred_mode[x264_scan8[i]];
1017 x264_cabac_mb_intra4x4_pred_mode( h, i_pred, i_mode );
1021 x264_cabac_mb_intra_chroma_pred_mode( h );
1023 else if( i_mb_type == P_L0 )
1025 if( h->mb.i_partition == D_16x16 )
1027 if( h->sh.i_num_ref_idx_l0_active > 1 )
1029 x264_cabac_mb_ref( h, 0, 0 );
1031 x264_cabac_mb_mvd( h, 0, 0, 4, 4 );
1033 else if( h->mb.i_partition == D_16x8 )
1035 if( h->sh.i_num_ref_idx_l0_active > 1 )
1037 x264_cabac_mb_ref( h, 0, 0 );
1038 x264_cabac_mb_ref( h, 0, 8 );
1040 x264_cabac_mb_mvd( h, 0, 0, 4, 2 );
1041 x264_cabac_mb_mvd( h, 0, 8, 4, 2 );
1043 else if( h->mb.i_partition == D_8x16 )
1045 if( h->sh.i_num_ref_idx_l0_active > 1 )
1047 x264_cabac_mb_ref( h, 0, 0 );
1048 x264_cabac_mb_ref( h, 0, 4 );
1050 x264_cabac_mb_mvd( h, 0, 0, 2, 4 );
1051 x264_cabac_mb_mvd( h, 0, 4, 2, 4 );
1054 else if( i_mb_type == P_8x8 )
1057 x264_cabac_mb_sub_p_partition( h, h->mb.i_sub_partition[0] );
1058 x264_cabac_mb_sub_p_partition( h, h->mb.i_sub_partition[1] );
1059 x264_cabac_mb_sub_p_partition( h, h->mb.i_sub_partition[2] );
1060 x264_cabac_mb_sub_p_partition( h, h->mb.i_sub_partition[3] );
1063 if( h->sh.i_num_ref_idx_l0_active > 1 )
1065 x264_cabac_mb_ref( h, 0, 0 );
1066 x264_cabac_mb_ref( h, 0, 4 );
1067 x264_cabac_mb_ref( h, 0, 8 );
1068 x264_cabac_mb_ref( h, 0, 12 );
1071 x264_cabac_mb8x8_mvd( h, 0 );
1073 else if( i_mb_type == B_8x8 )
1076 x264_cabac_mb_sub_b_partition( h, h->mb.i_sub_partition[0] );
1077 x264_cabac_mb_sub_b_partition( h, h->mb.i_sub_partition[1] );
1078 x264_cabac_mb_sub_b_partition( h, h->mb.i_sub_partition[2] );
1079 x264_cabac_mb_sub_b_partition( h, h->mb.i_sub_partition[3] );
1082 for( i_list = 0; i_list < 2; i_list++ )
1084 if( ( i_list ? h->sh.i_num_ref_idx_l1_active : h->sh.i_num_ref_idx_l0_active ) == 1 )
1086 for( i = 0; i < 4; i++ )
1087 if( x264_mb_partition_listX_table[i_list][ h->mb.i_sub_partition[i] ] )
1088 x264_cabac_mb_ref( h, i_list, 4*i );
1091 x264_cabac_mb8x8_mvd( h, 0 );
1092 x264_cabac_mb8x8_mvd( h, 1 );
1094 else if( i_mb_type != B_DIRECT )
1099 /* init ref list utilisations */
1100 for( i = 0; i < 2; i++ )
1102 b_list[0][i] = x264_mb_type_list0_table[i_mb_type][i];
1103 b_list[1][i] = x264_mb_type_list1_table[i_mb_type][i];
1106 for( i_list = 0; i_list < 2; i_list++ )
1108 const int i_ref_max = i_list == 0 ? h->sh.i_num_ref_idx_l0_active : h->sh.i_num_ref_idx_l1_active;
1112 if( h->mb.i_partition == D_16x16 )
1114 if( b_list[i_list][0] ) x264_cabac_mb_ref( h, i_list, 0 );
1116 else if( h->mb.i_partition == D_16x8 )
1118 if( b_list[i_list][0] ) x264_cabac_mb_ref( h, i_list, 0 );
1119 if( b_list[i_list][1] ) x264_cabac_mb_ref( h, i_list, 8 );
1121 else if( h->mb.i_partition == D_8x16 )
1123 if( b_list[i_list][0] ) x264_cabac_mb_ref( h, i_list, 0 );
1124 if( b_list[i_list][1] ) x264_cabac_mb_ref( h, i_list, 4 );
1128 for( i_list = 0; i_list < 2; i_list++ )
1130 if( h->mb.i_partition == D_16x16 )
1132 if( b_list[i_list][0] ) x264_cabac_mb_mvd( h, i_list, 0, 4, 4 );
1134 else if( h->mb.i_partition == D_16x8 )
1136 if( b_list[i_list][0] ) x264_cabac_mb_mvd( h, i_list, 0, 4, 2 );
1137 if( b_list[i_list][1] ) x264_cabac_mb_mvd( h, i_list, 8, 4, 2 );
1139 else if( h->mb.i_partition == D_8x16 )
1141 if( b_list[i_list][0] ) x264_cabac_mb_mvd( h, i_list, 0, 2, 4 );
1142 if( b_list[i_list][1] ) x264_cabac_mb_mvd( h, i_list, 4, 2, 4 );
1147 i_mb_pos_tex = bs_pos( s );
1148 h->stat.frame.i_hdr_bits += i_mb_pos_tex - i_mb_pos_start;
1150 if( i_mb_type != I_16x16 )
1152 x264_cabac_mb_cbp_luma( h );
1153 x264_cabac_mb_cbp_chroma( h );
1156 if( h->mb.cache.b_transform_8x8_allowed && h->mb.i_cbp_luma && !IS_INTRA(i_mb_type) )
1158 x264_cabac_mb_transform_size( h );
1161 if( h->mb.i_cbp_luma > 0 || h->mb.i_cbp_chroma > 0 || i_mb_type == I_16x16 )
1163 x264_cabac_mb_qp_delta( h );
1165 /* write residual */
1166 if( i_mb_type == I_16x16 )
1169 block_residual_write_cabac( h, 0, 0, h->dct.luma16x16_dc, 16 );
1172 if( h->mb.i_cbp_luma != 0 )
1173 for( i = 0; i < 16; i++ )
1174 block_residual_write_cabac( h, 1, i, h->dct.block[i].residual_ac, 15 );
1176 else if( h->mb.b_transform_8x8 )
1178 for( i = 0; i < 4; i++ )
1179 if( h->mb.i_cbp_luma & ( 1 << i ) )
1180 block_residual_write_cabac( h, 5, i, h->dct.luma8x8[i], 64 );
1184 for( i = 0; i < 16; i++ )
1185 if( h->mb.i_cbp_luma & ( 1 << ( i / 4 ) ) )
1186 block_residual_write_cabac( h, 2, i, h->dct.block[i].luma4x4, 16 );
1189 if( h->mb.i_cbp_chroma &0x03 ) /* Chroma DC residual present */
1191 block_residual_write_cabac( h, 3, 0, h->dct.chroma_dc[0], 4 );
1192 block_residual_write_cabac( h, 3, 1, h->dct.chroma_dc[1], 4 );
1194 if( h->mb.i_cbp_chroma&0x02 ) /* Chroma AC residual present */
1196 for( i = 0; i < 8; i++ )
1197 block_residual_write_cabac( h, 4, i, h->dct.block[16+i].residual_ac, 15 );
1201 if( IS_INTRA( i_mb_type ) )
1202 h->stat.frame.i_itex_bits += bs_pos(s) - i_mb_pos_tex;
1204 h->stat.frame.i_ptex_bits += bs_pos(s) - i_mb_pos_tex;