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 void x264_cabac_mb_type( x264_t *h )
49 const int i_mb_type = h->mb.i_type;
51 if( h->sh.i_type == SLICE_TYPE_I )
54 if( h->mb.i_mb_x > 0 && h->mb.type[h->mb.i_mb_xy - 1] != I_4x4 )
58 if( h->mb.i_mb_y > 0 && h->mb.type[h->mb.i_mb_xy - h->mb.i_mb_stride] != I_4x4 )
63 if( i_mb_type == I_4x4 )
65 x264_cabac_encode_decision( &h->cabac, 3 + ctx, 0 );
67 else if( i_mb_type == I_PCM )
69 x264_cabac_encode_decision( &h->cabac, 3 + ctx, 1 );
70 x264_cabac_encode_terminal( &h->cabac, 1 );
74 x264_cabac_encode_decision( &h->cabac, 3 + ctx, 1 );
75 x264_cabac_encode_terminal( &h->cabac, 0 );
77 x264_cabac_encode_decision( &h->cabac, 3 + 3, ( h->mb.i_cbp_luma == 0 ? 0 : 1 ));
78 if( h->mb.i_cbp_chroma == 0 )
80 x264_cabac_encode_decision( &h->cabac, 3 + 4, 0 );
84 x264_cabac_encode_decision( &h->cabac, 3 + 4, 1 );
85 x264_cabac_encode_decision( &h->cabac, 3 + 5, ( h->mb.i_cbp_chroma == 1 ? 0 : 1 ) );
87 x264_cabac_encode_decision( &h->cabac, 3 + 6, ( (h->mb.i_intra16x16_pred_mode / 2) ? 1 : 0 ));
88 x264_cabac_encode_decision( &h->cabac, 3 + 7, ( (h->mb.i_intra16x16_pred_mode % 2) ? 1 : 0 ));
91 else if( h->sh.i_type == SLICE_TYPE_P )
93 /* prefix: 14, suffix: 17 */
94 if( i_mb_type == P_L0 )
96 if( h->mb.i_partition == D_16x16 )
98 x264_cabac_encode_decision( &h->cabac, 14, 0 );
99 x264_cabac_encode_decision( &h->cabac, 15, 0 );
100 x264_cabac_encode_decision( &h->cabac, 16, 0 );
102 else if( h->mb.i_partition == D_16x8 )
104 x264_cabac_encode_decision( &h->cabac, 14, 0 );
105 x264_cabac_encode_decision( &h->cabac, 15, 1 );
106 x264_cabac_encode_decision( &h->cabac, 17, 1 );
108 else if( h->mb.i_partition == D_8x16 )
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, 0 );
115 else if( i_mb_type == P_8x8 )
117 x264_cabac_encode_decision( &h->cabac, 14, 0 );
118 x264_cabac_encode_decision( &h->cabac, 15, 0 );
119 x264_cabac_encode_decision( &h->cabac, 16, 1 );
121 else if( i_mb_type == I_4x4 )
124 x264_cabac_encode_decision( &h->cabac, 14, 1 );
126 x264_cabac_encode_decision( &h->cabac, 17, 0 );
128 else if( i_mb_type == I_PCM )
131 x264_cabac_encode_decision( &h->cabac, 14, 1 );
133 x264_cabac_encode_decision( &h->cabac, 17, 1 );
134 x264_cabac_encode_terminal( &h->cabac, 1 ); /*ctxIdx == 276 */
136 else /* intra 16x16 */
139 x264_cabac_encode_decision( &h->cabac, 14, 1 );
142 x264_cabac_encode_decision( &h->cabac, 17, 1 );
143 x264_cabac_encode_terminal( &h->cabac, 0 ); /*ctxIdx == 276 */
145 x264_cabac_encode_decision( &h->cabac, 17+1, ( h->mb.i_cbp_luma == 0 ? 0 : 1 ));
146 if( h->mb.i_cbp_chroma == 0 )
148 x264_cabac_encode_decision( &h->cabac, 17+2, 0 );
152 x264_cabac_encode_decision( &h->cabac, 17+2, 1 );
153 x264_cabac_encode_decision( &h->cabac, 17+2, ( h->mb.i_cbp_chroma == 1 ? 0 : 1 ) );
155 x264_cabac_encode_decision( &h->cabac, 17+3, ( (h->mb.i_intra16x16_pred_mode / 2) ? 1 : 0 ));
156 x264_cabac_encode_decision( &h->cabac, 17+3, ( (h->mb.i_intra16x16_pred_mode % 2) ? 1 : 0 ));
159 else if( h->sh.i_type == SLICE_TYPE_B )
162 if( h->mb.i_mb_x > 0 && h->mb.type[h->mb.i_mb_xy - 1] != B_SKIP && h->mb.type[h->mb.i_mb_xy - 1] != B_DIRECT )
166 if( h->mb.i_mb_y > 0 && h->mb.type[h->mb.i_mb_xy - h->mb.i_mb_stride] != B_SKIP && h->mb.type[h->mb.i_mb_xy - h->mb.i_mb_stride] != B_DIRECT )
171 if( i_mb_type == B_DIRECT )
173 x264_cabac_encode_decision( &h->cabac, 27+ctx, 0 );
175 else if( i_mb_type == B_8x8 )
177 x264_cabac_encode_decision( &h->cabac, 27+ctx, 1 );
178 x264_cabac_encode_decision( &h->cabac, 27+3, 1 );
179 x264_cabac_encode_decision( &h->cabac, 27+4, 1 );
181 x264_cabac_encode_decision( &h->cabac, 27+5, 1 );
182 x264_cabac_encode_decision( &h->cabac, 27+5, 1 );
183 x264_cabac_encode_decision( &h->cabac, 27+5, 1 );
185 else if( IS_INTRA( i_mb_type ) )
188 x264_cabac_encode_decision( &h->cabac, 27+ctx, 1 );
189 x264_cabac_encode_decision( &h->cabac, 27+3, 1 );
190 x264_cabac_encode_decision( &h->cabac, 27+4, 1 );
192 x264_cabac_encode_decision( &h->cabac, 27+5, 1 );
193 x264_cabac_encode_decision( &h->cabac, 27+5, 0 );
194 x264_cabac_encode_decision( &h->cabac, 27+5, 1 );
197 if( i_mb_type == I_4x4 )
199 x264_cabac_encode_decision( &h->cabac, 32, 0 );
201 else if( i_mb_type == I_PCM )
203 x264_cabac_encode_decision( &h->cabac, 32, 1 );
204 x264_cabac_encode_terminal( &h->cabac, 1 );
208 x264_cabac_encode_decision( &h->cabac, 32, 1 );
209 x264_cabac_encode_terminal( &h->cabac, 0 );
212 x264_cabac_encode_decision( &h->cabac, 32+1, ( h->mb.i_cbp_luma == 0 ? 0 : 1 ));
213 if( h->mb.i_cbp_chroma == 0 )
215 x264_cabac_encode_decision( &h->cabac, 32+2, 0 );
219 x264_cabac_encode_decision( &h->cabac, 32+2, 1 );
220 x264_cabac_encode_decision( &h->cabac, 32+2, ( h->mb.i_cbp_chroma == 1 ? 0 : 1 ) );
222 x264_cabac_encode_decision( &h->cabac, 32+3, ( (h->mb.i_intra16x16_pred_mode / 2) ? 1 : 0 ));
223 x264_cabac_encode_decision( &h->cabac, 32+3, ( (h->mb.i_intra16x16_pred_mode % 2) ? 1 : 0 ));
228 static const int i_mb_len[21] =
241 static const int i_mb_bits[21][7] =
243 { 1, 0, 0, }, { 1, 1, 0, 0, 0, 1, }, { 1, 1, 0, 0, 1, 0, }, /* L0 L0 */
244 { 1, 0, 1, }, { 1, 1, 0, 0, 1, 1, }, { 1, 1, 0, 1, 0, 0, }, /* L1 L1 */
245 { 1, 1, 0, 0, 0, 0 ,}, { 1, 1, 1, 1, 0, 0 , 0 }, { 1, 1, 1, 1, 0, 0 , 1 },/* BI BI */
247 { 1, 1, 0, 1, 0, 1, }, { 1, 1, 0, 1, 1, 0, }, /* L0 L1 */
248 { 1, 1, 0, 1, 1, 1, }, { 1, 1, 1, 1, 1, 0, }, /* L1 L0 */
249 { 1, 1, 1, 0, 0, 0, 0 }, { 1, 1, 1, 0, 0, 0, 1 }, /* L0 BI */
250 { 1, 1, 1, 0, 0, 1, 0 }, { 1, 1, 1, 0, 0, 1, 1 }, /* L1 BI */
251 { 1, 1, 1, 0, 1, 0, 0 }, { 1, 1, 1, 0, 1, 0, 1 }, /* BI L0 */
252 { 1, 1, 1, 0, 1, 1, 0 }, { 1, 1, 1, 0, 1, 1, 1 } /* BI L1 */
255 const int i_partition = h->mb.i_partition;
260 /* D_16x16, D_16x8, D_8x16 */
261 case B_BI_BI: idx += 3;
262 case B_L1_L1: idx += 3;
264 if( i_partition == D_16x8 )
266 else if( i_partition == D_8x16 )
271 case B_BI_L1: idx += 2;
272 case B_BI_L0: idx += 2;
273 case B_L1_BI: idx += 2;
274 case B_L0_BI: idx += 2;
275 case B_L1_L0: idx += 2;
278 if( i_partition == D_8x16 )
282 fprintf( stderr, "error in B mb type\n" );
286 x264_cabac_encode_decision( &h->cabac, 27+ctx, i_mb_bits[idx][0] );
287 x264_cabac_encode_decision( &h->cabac, 27+3, i_mb_bits[idx][1] );
288 x264_cabac_encode_decision( &h->cabac, 27+(i_mb_bits[idx][1] != 0 ? 4 : 5), i_mb_bits[idx][2] );
289 for( i = 3; i < i_mb_len[idx]; i++ )
291 x264_cabac_encode_decision( &h->cabac, 27+5, i_mb_bits[idx][i] );
297 fprintf( stderr, "unknown SLICE_TYPE unsupported in x264_macroblock_write_cabac\n" );
301 static void x264_cabac_mb_intra4x4_pred_mode( x264_t *h, int i_pred, int i_mode )
303 if( i_pred == i_mode )
305 /* b_prev_intra4x4_pred_mode */
306 x264_cabac_encode_decision( &h->cabac, 68, 1 );
310 /* b_prev_intra4x4_pred_mode */
311 x264_cabac_encode_decision( &h->cabac, 68, 0 );
312 if( i_mode > i_pred )
316 x264_cabac_encode_decision( &h->cabac, 69, (i_mode )&0x01 );
317 x264_cabac_encode_decision( &h->cabac, 69, (i_mode >> 1)&0x01 );
318 x264_cabac_encode_decision( &h->cabac, 69, (i_mode >> 2)&0x01 );
321 static void x264_cabac_mb_intra8x8_pred_mode( x264_t *h )
323 const int i_mode = h->mb.i_chroma_pred_mode;
326 /* No need to test for I4x4 or I_16x16 as cache_save handle that */
327 if( h->mb.i_mb_x > 0 && h->mb.chroma_pred_mode[h->mb.i_mb_xy - 1] != 0 )
331 if( h->mb.i_mb_y > 0 && h->mb.chroma_pred_mode[h->mb.i_mb_xy - h->mb.i_mb_stride] != 0 )
338 x264_cabac_encode_decision( &h->cabac, 64 + ctx, 0 );
342 x264_cabac_encode_decision( &h->cabac, 64 + ctx, 1 );
343 x264_cabac_encode_decision( &h->cabac, 64 + 3, ( i_mode == 1 ? 0 : 1 ) );
346 x264_cabac_encode_decision( &h->cabac, 64 + 3, ( i_mode == 2 ? 0 : 1 ) );
351 static void x264_cabac_mb_cbp_luma( x264_t *h )
353 /* TODO: clean up and optimize */
355 for( i8x8 = 0; i8x8 < 4; i8x8++ )
359 int x = block_idx_x[4*i8x8];
360 int y = block_idx_y[4*i8x8];
364 i_mba_xy = h->mb.i_mb_xy;
365 else if( h->mb.i_mb_x > 0 )
366 i_mba_xy = h->mb.i_mb_xy - 1;
369 i_mbb_xy = h->mb.i_mb_xy;
370 else if( h->mb.i_mb_y > 0 )
371 i_mbb_xy = h->mb.i_mb_xy - h->mb.i_mb_stride;
374 /* No need to test for PCM and SKIP */
377 const int i8x8a = block_idx_xy[(x-1)&0x03][y]/4;
378 if( ((h->mb.cbp[i_mba_xy] >> i8x8a)&0x01) == 0 )
386 const int i8x8b = block_idx_xy[x][(y-1)&0x03]/4;
387 if( ((h->mb.cbp[i_mbb_xy] >> i8x8b)&0x01) == 0 )
393 x264_cabac_encode_decision( &h->cabac, 73 + ctx, (h->mb.i_cbp_luma >> i8x8)&0x01 );
397 static void x264_cabac_mb_cbp_chroma( x264_t *h )
403 /* No need to test for SKIP/PCM */
404 if( h->mb.i_mb_x > 0 )
406 cbp_a = (h->mb.cbp[h->mb.i_mb_xy - 1] >> 4)&0x3;
409 if( h->mb.i_mb_y > 0 )
411 cbp_b = (h->mb.cbp[h->mb.i_mb_xy - h->mb.i_mb_stride] >> 4)&0x3;
415 if( cbp_a > 0 ) ctx++;
416 if( cbp_b > 0 ) ctx += 2;
417 if( h->mb.i_cbp_chroma == 0 )
419 x264_cabac_encode_decision( &h->cabac, 77 + ctx, 0 );
423 x264_cabac_encode_decision( &h->cabac, 77 + ctx, 1 );
426 if( cbp_a == 2 ) ctx++;
427 if( cbp_b == 2 ) ctx += 2;
428 x264_cabac_encode_decision( &h->cabac, 77 + ctx, h->mb.i_cbp_chroma > 1 ? 1 : 0 );
432 /* TODO check it with != qp per mb */
433 static void x264_cabac_mb_qp_delta( x264_t *h )
435 int i_mbn_xy = h->mb.i_mb_xy - 1;
436 int i_dqp = h->mb.qp[h->mb.i_mb_xy] - h->mb.i_last_qp;
437 int val = i_dqp <= 0 ? (-2*i_dqp) : (2*i_dqp - 1);
440 /* No need to test for PCM / SKIP */
441 if( i_mbn_xy >= 0 && h->mb.i_last_dqp != 0 &&
442 ( h->mb.type[i_mbn_xy] == I_16x16 || (h->mb.cbp[i_mbn_xy]&0x3f) ) )
449 x264_cabac_encode_decision( &h->cabac, 60 + ctx, 1 );
456 x264_cabac_encode_decision( &h->cabac, 60 + ctx, 0 );
459 void x264_cabac_mb_skip( x264_t *h, int b_skip )
463 if( h->mb.i_mb_x > 0 && !IS_SKIP( h->mb.type[h->mb.i_mb_xy -1]) )
467 if( h->mb.i_mb_y > 0 && !IS_SKIP( h->mb.type[h->mb.i_mb_xy -h->mb.i_mb_stride]) )
472 if( h->sh.i_type == SLICE_TYPE_P )
473 x264_cabac_encode_decision( &h->cabac, 11 + ctx, b_skip ? 1 : 0 );
474 else /* SLICE_TYPE_B */
475 x264_cabac_encode_decision( &h->cabac, 24 + ctx, b_skip ? 1 : 0 );
478 static inline void x264_cabac_mb_sub_p_partition( x264_t *h, int i_sub )
480 if( i_sub == D_L0_8x8 )
482 x264_cabac_encode_decision( &h->cabac, 21, 1 );
484 else if( i_sub == D_L0_8x4 )
486 x264_cabac_encode_decision( &h->cabac, 21, 0 );
487 x264_cabac_encode_decision( &h->cabac, 22, 0 );
489 else if( i_sub == D_L0_4x8 )
491 x264_cabac_encode_decision( &h->cabac, 21, 0 );
492 x264_cabac_encode_decision( &h->cabac, 22, 1 );
493 x264_cabac_encode_decision( &h->cabac, 23, 1 );
495 else if( i_sub == D_L0_4x4 )
497 x264_cabac_encode_decision( &h->cabac, 21, 0 );
498 x264_cabac_encode_decision( &h->cabac, 22, 1 );
499 x264_cabac_encode_decision( &h->cabac, 23, 0 );
503 static inline void x264_cabac_mb_sub_b_partition( x264_t *h, int i_sub )
505 if( i_sub == D_DIRECT_8x8 )
507 x264_cabac_encode_decision( &h->cabac, 36, 0 );
509 else if( i_sub == D_L0_8x8 )
511 x264_cabac_encode_decision( &h->cabac, 36, 1 );
512 x264_cabac_encode_decision( &h->cabac, 37, 0 );
513 x264_cabac_encode_decision( &h->cabac, 39, 0 );
515 else if( i_sub == D_L1_8x8 )
517 x264_cabac_encode_decision( &h->cabac, 36, 1 );
518 x264_cabac_encode_decision( &h->cabac, 37, 0 );
519 x264_cabac_encode_decision( &h->cabac, 39, 1 );
521 else if( i_sub == D_BI_8x8 )
523 x264_cabac_encode_decision( &h->cabac, 36, 1 );
524 x264_cabac_encode_decision( &h->cabac, 37, 1 );
525 x264_cabac_encode_decision( &h->cabac, 38, 0 );
526 x264_cabac_encode_decision( &h->cabac, 39, 0 );
527 x264_cabac_encode_decision( &h->cabac, 39, 0 );
529 else if( i_sub == D_L0_8x4 )
531 x264_cabac_encode_decision( &h->cabac, 36, 1 );
532 x264_cabac_encode_decision( &h->cabac, 37, 1 );
533 x264_cabac_encode_decision( &h->cabac, 38, 0 );
534 x264_cabac_encode_decision( &h->cabac, 39, 0 );
535 x264_cabac_encode_decision( &h->cabac, 39, 1 );
537 else if( i_sub == D_L0_4x8 )
539 x264_cabac_encode_decision( &h->cabac, 36, 1 );
540 x264_cabac_encode_decision( &h->cabac, 37, 1 );
541 x264_cabac_encode_decision( &h->cabac, 38, 0 );
542 x264_cabac_encode_decision( &h->cabac, 39, 1 );
543 x264_cabac_encode_decision( &h->cabac, 39, 0 );
545 else if( i_sub == D_L1_8x4 )
547 x264_cabac_encode_decision( &h->cabac, 36, 1 );
548 x264_cabac_encode_decision( &h->cabac, 37, 1 );
549 x264_cabac_encode_decision( &h->cabac, 38, 0 );
550 x264_cabac_encode_decision( &h->cabac, 39, 1 );
551 x264_cabac_encode_decision( &h->cabac, 39, 1 );
553 else if( i_sub == D_L1_4x8 )
555 x264_cabac_encode_decision( &h->cabac, 36, 1 );
556 x264_cabac_encode_decision( &h->cabac, 37, 1 );
557 x264_cabac_encode_decision( &h->cabac, 38, 1 );
558 x264_cabac_encode_decision( &h->cabac, 39, 0 );
559 x264_cabac_encode_decision( &h->cabac, 39, 0 );
560 x264_cabac_encode_decision( &h->cabac, 39, 0 );
562 else if( i_sub == D_BI_8x4 )
564 x264_cabac_encode_decision( &h->cabac, 36, 1 );
565 x264_cabac_encode_decision( &h->cabac, 37, 1 );
566 x264_cabac_encode_decision( &h->cabac, 38, 1 );
567 x264_cabac_encode_decision( &h->cabac, 39, 0 );
568 x264_cabac_encode_decision( &h->cabac, 39, 0 );
569 x264_cabac_encode_decision( &h->cabac, 39, 1 );
571 else if( i_sub == D_BI_4x8 )
573 x264_cabac_encode_decision( &h->cabac, 36, 1 );
574 x264_cabac_encode_decision( &h->cabac, 37, 1 );
575 x264_cabac_encode_decision( &h->cabac, 38, 1 );
576 x264_cabac_encode_decision( &h->cabac, 39, 0 );
577 x264_cabac_encode_decision( &h->cabac, 39, 1 );
578 x264_cabac_encode_decision( &h->cabac, 39, 0 );
580 else if( i_sub == D_L0_4x4 )
582 x264_cabac_encode_decision( &h->cabac, 36, 1 );
583 x264_cabac_encode_decision( &h->cabac, 37, 1 );
584 x264_cabac_encode_decision( &h->cabac, 38, 1 );
585 x264_cabac_encode_decision( &h->cabac, 39, 0 );
586 x264_cabac_encode_decision( &h->cabac, 39, 1 );
587 x264_cabac_encode_decision( &h->cabac, 39, 1 );
589 else if( i_sub == D_L1_4x4 )
591 x264_cabac_encode_decision( &h->cabac, 36, 1 );
592 x264_cabac_encode_decision( &h->cabac, 37, 1 );
593 x264_cabac_encode_decision( &h->cabac, 38, 1 );
594 x264_cabac_encode_decision( &h->cabac, 39, 1 );
595 x264_cabac_encode_decision( &h->cabac, 39, 0 );
597 else if( i_sub == D_BI_4x4 )
599 x264_cabac_encode_decision( &h->cabac, 36, 1 );
600 x264_cabac_encode_decision( &h->cabac, 37, 1 );
601 x264_cabac_encode_decision( &h->cabac, 38, 1 );
602 x264_cabac_encode_decision( &h->cabac, 39, 1 );
603 x264_cabac_encode_decision( &h->cabac, 39, 1 );
607 static inline void x264_cabac_mb_ref( x264_t *h, int i_list, int idx )
609 const int i8 = x264_scan8[idx];
610 const int i_refa = h->mb.cache.ref[i_list][i8 - 1];
611 const int i_refb = h->mb.cache.ref[i_list][i8 - 8];
612 int i_ref = h->mb.cache.ref[i_list][i8];
615 if( i_refa > 0 && !h->mb.cache.skip[i8 - 1])
617 if( i_refb > 0 && !h->mb.cache.skip[i8 - 8])
622 x264_cabac_encode_decision( &h->cabac, 54 + ctx, 1 );
630 x264_cabac_encode_decision( &h->cabac, 54 + ctx, 0 );
635 static inline void x264_cabac_mb_mvd_cpn( x264_t *h, int i_list, int idx, int l, int mvd )
637 const int amvd = abs( h->mb.cache.mvd[i_list][x264_scan8[idx] - 1][l] ) +
638 abs( h->mb.cache.mvd[i_list][x264_scan8[idx] - 8][l] );
639 const int i_abs = abs( mvd );
640 const int i_prefix = X264_MIN( i_abs, 9 );
641 const int ctxbase = (l == 0 ? 40 : 47);
653 for( i = 0; i < i_prefix; i++ )
655 x264_cabac_encode_decision( &h->cabac, ctxbase + ctx, 1 );
663 x264_cabac_encode_decision( &h->cabac, ctxbase + ctx, 0 );
668 int i_suffix = i_abs - 9;
671 while( i_suffix >= (1<<k) )
673 x264_cabac_encode_bypass( &h->cabac, 1 );
677 x264_cabac_encode_bypass( &h->cabac, 0 );
680 x264_cabac_encode_bypass( &h->cabac, (i_suffix >> k)&0x01 );
686 x264_cabac_encode_bypass( &h->cabac, 0 );
688 x264_cabac_encode_bypass( &h->cabac, 1 );
691 static inline void x264_cabac_mb_mvd( x264_t *h, int i_list, int idx, int width, int height )
697 x264_mb_predict_mv( h, i_list, idx, width, mvp );
698 mdx = h->mb.cache.mv[i_list][x264_scan8[idx]][0] - mvp[0];
699 mdy = h->mb.cache.mv[i_list][x264_scan8[idx]][1] - mvp[1];
702 x264_cabac_mb_mvd_cpn( h, i_list, idx, 0, mdx );
703 x264_cabac_mb_mvd_cpn( h, i_list, idx, 1, mdy );
706 x264_macroblock_cache_mvd( h, block_idx_x[idx], block_idx_y[idx], width, height, i_list, mdx, mdy );
709 static inline void x264_cabac_mb8x8_mvd( x264_t *h, int i_list )
712 for( i = 0; i < 4; i++ )
714 if( !x264_mb_partition_listX_table[i_list][ h->mb.i_sub_partition[i] ] )
719 switch( h->mb.i_sub_partition[i] )
724 x264_cabac_mb_mvd( h, i_list, 4*i, 2, 2 );
729 x264_cabac_mb_mvd( h, i_list, 4*i+0, 2, 1 );
730 x264_cabac_mb_mvd( h, i_list, 4*i+2, 2, 1 );
735 x264_cabac_mb_mvd( h, i_list, 4*i+0, 1, 2 );
736 x264_cabac_mb_mvd( h, i_list, 4*i+1, 1, 2 );
741 x264_cabac_mb_mvd( h, i_list, 4*i+0, 1, 1 );
742 x264_cabac_mb_mvd( h, i_list, 4*i+1, 1, 1 );
743 x264_cabac_mb_mvd( h, i_list, 4*i+2, 1, 1 );
744 x264_cabac_mb_mvd( h, i_list, 4*i+3, 1, 1 );
750 static int x264_cabac_mb_cbf_ctxidxinc( x264_t *h, int i_cat, int i_idx )
752 /* TODO: clean up/optimize */
761 if( h->mb.i_mb_x > 0 )
763 i_mba_xy = h->mb.i_mb_xy -1;
764 if( h->mb.type[i_mba_xy] == I_16x16 )
766 i_nza = h->mb.cbp[i_mba_xy]&0x100;
769 if( h->mb.i_mb_y > 0 )
771 i_mbb_xy = h->mb.i_mb_xy - h->mb.i_mb_stride;
772 if( h->mb.type[i_mbb_xy] == I_16x16 )
774 i_nzb = h->mb.cbp[i_mbb_xy]&0x100;
778 else if( i_cat == 1 || i_cat == 2 )
780 int x = block_idx_x[i_idx];
781 int y = block_idx_y[i_idx];
784 i_mba_xy = h->mb.i_mb_xy;
785 else if( h->mb.i_mb_x > 0 )
786 i_mba_xy = h->mb.i_mb_xy -1;
789 i_mbb_xy = h->mb.i_mb_xy;
790 else if( h->mb.i_mb_y > 0 )
791 i_mbb_xy = h->mb.i_mb_xy - h->mb.i_mb_stride;
793 /* no need to test for skip/pcm */
796 const int i8x8a = block_idx_xy[(x-1)&0x03][y]/4;
797 if( (h->mb.cbp[i_mba_xy]&0x0f)>> i8x8a )
799 i_nza = h->mb.cache.non_zero_count[x264_scan8[i_idx] - 1];
804 const int i8x8b = block_idx_xy[x][(y-1)&0x03]/4;
805 if( (h->mb.cbp[i_mbb_xy]&0x0f)>> i8x8b )
807 i_nzb = h->mb.cache.non_zero_count[x264_scan8[i_idx] - 8];
811 else if( i_cat == 3 )
813 /* no need to test skip/pcm */
814 if( h->mb.i_mb_x > 0 )
816 i_mba_xy = h->mb.i_mb_xy -1;
817 if( h->mb.cbp[i_mba_xy]&0x30 )
819 i_nza = h->mb.cbp[i_mba_xy]&( 0x02 << ( 8 + i_idx) );
822 if( h->mb.i_mb_y > 0 )
824 i_mbb_xy = h->mb.i_mb_xy - h->mb.i_mb_stride;
825 if( h->mb.cbp[i_mbb_xy]&0x30 )
827 i_nzb = h->mb.cbp[i_mbb_xy]&( 0x02 << ( 8 + i_idx) );
831 else if( i_cat == 4 )
835 if( idxc == 1 || idxc == 3 )
836 i_mba_xy = h->mb.i_mb_xy;
837 else if( h->mb.i_mb_x > 0 )
838 i_mba_xy = h->mb.i_mb_xy - 1;
840 if( idxc == 2 || idxc == 3 )
841 i_mbb_xy = h->mb.i_mb_xy;
842 else if( h->mb.i_mb_y > 0 )
843 i_mbb_xy = h->mb.i_mb_xy - h->mb.i_mb_stride;
845 /* no need to test skip/pcm */
846 if( i_mba_xy >= 0 && (h->mb.cbp[i_mba_xy]&0x30) == 0x20 )
848 i_nza = h->mb.cache.non_zero_count[x264_scan8[16+i_idx] - 1];
850 if( i_mbb_xy >= 0 && (h->mb.cbp[i_mbb_xy]&0x30) == 0x20 )
852 i_nzb = h->mb.cache.non_zero_count[x264_scan8[16+i_idx] - 8];
856 if( ( i_mba_xy < 0 && IS_INTRA( h->mb.i_type ) ) || i_nza > 0 )
860 if( ( i_mbb_xy < 0 && IS_INTRA( h->mb.i_type ) ) || i_nzb > 0 )
865 return 4 * i_cat + ctx;
869 static void block_residual_write_cabac( x264_t *h, int i_ctxBlockCat, int i_idx, int *l, int i_count )
871 static const int significant_coeff_flag_offset[5] = { 0, 15, 29, 44, 47 };
872 static const int last_significant_coeff_flag_offset[5] = { 0, 15, 29, 44, 47 };
873 static const int coeff_abs_level_m1_offset[5] = { 0, 10, 20, 30, 39 };
875 int i_coeff_abs_m1[16];
876 int i_coeff_sign[16];
881 int i_abslevelgt1 = 0;
885 /* i_ctxBlockCat: 0-> DC 16x16 i_idx = 0
886 * 1-> AC 16x16 i_idx = luma4x4idx
887 * 2-> Luma4x4 i_idx = luma4x4idx
888 * 3-> DC Chroma i_idx = iCbCr
889 * 4-> AC Chroma i_idx = 4 * iCbCr + chroma4x4idx
892 //fprintf( stderr, "l[] = " );
893 for( i = 0; i < i_count; i++ )
895 //fprintf( stderr, "%d ", l[i] );
898 i_coeff_abs_m1[i_coeff] = abs( l[i] ) - 1;
899 i_coeff_sign[i_coeff] = ( l[i] < 0 ? 1 : 0);
905 //fprintf( stderr, "\n" );
909 /* codec block flag */
910 x264_cabac_encode_decision( &h->cabac, 85 + x264_cabac_mb_cbf_ctxidxinc( h, i_ctxBlockCat, i_idx ), 0 );
915 x264_cabac_encode_decision( &h->cabac, 85 + x264_cabac_mb_cbf_ctxidxinc( h, i_ctxBlockCat, i_idx ), 1 );
916 for( i = 0; i < i_count - 1; i++ )
920 i_ctxIdxInc = X264_MIN( i, i_count - 2 );
924 x264_cabac_encode_decision( &h->cabac, 105 + significant_coeff_flag_offset[i_ctxBlockCat] + i_ctxIdxInc, 1 );
925 x264_cabac_encode_decision( &h->cabac, 166 + last_significant_coeff_flag_offset[i_ctxBlockCat] + i_ctxIdxInc, i == i_last ? 1 : 0 );
929 x264_cabac_encode_decision( &h->cabac, 105 + significant_coeff_flag_offset[i_ctxBlockCat] + i_ctxIdxInc, 0 );
937 for( i = i_coeff - 1; i >= 0; i-- )
942 /* write coeff_abs - 1 */
945 i_prefix = X264_MIN( i_coeff_abs_m1[i], 14 );
947 i_ctxIdxInc = (i_abslevelgt1 != 0 ? 0 : X264_MIN( 4, i_abslevel1 + 1 )) + coeff_abs_level_m1_offset[i_ctxBlockCat];
950 x264_cabac_encode_decision( &h->cabac, 227 + i_ctxIdxInc, 0 );
955 x264_cabac_encode_decision( &h->cabac, 227 + i_ctxIdxInc, 1 );
956 i_ctxIdxInc = 5 + X264_MIN( 4, i_abslevelgt1 ) + coeff_abs_level_m1_offset[i_ctxBlockCat];
957 for( j = 0; j < i_prefix - 1; j++ )
959 x264_cabac_encode_decision( &h->cabac, 227 + i_ctxIdxInc, 1 );
963 x264_cabac_encode_decision( &h->cabac, 227 + i_ctxIdxInc, 0 );
967 if( i_coeff_abs_m1[i] >= 14 )
970 int i_suffix = i_coeff_abs_m1[i] - 14;
972 while( i_suffix >= (1<<k) )
974 x264_cabac_encode_bypass( &h->cabac, 1 );
978 x264_cabac_encode_bypass( &h->cabac, 0 );
981 x264_cabac_encode_bypass( &h->cabac, (i_suffix >> k)&0x01 );
986 x264_cabac_encode_bypass( &h->cabac, i_coeff_sign[i] );
989 if( i_coeff_abs_m1[i] == 0 )
1002 void x264_macroblock_write_cabac( x264_t *h, bs_t *s )
1004 const int i_mb_type = h->mb.i_type;
1005 const int i_mb_pos_start = bs_pos( s );
1011 /* Write the MB type */
1012 x264_cabac_mb_type( h );
1014 /* PCM special block type UNTESTED */
1015 if( i_mb_type == I_PCM )
1017 bs_align_0( s ); /* not sure */
1019 for( i = 0; i < 16*16; i++ )
1021 const int x = 16 * h->mb.i_mb_x + (i % 16);
1022 const int y = 16 * h->mb.i_mb_y + (i / 16);
1023 bs_write( s, 8, h->fenc->plane[0][y*h->mb.pic.i_stride[0]+x] );
1026 for( i = 0; i < 8*8; i++ )
1028 const int x = 8 * h->mb.i_mb_x + (i % 8);
1029 const int y = 8 * h->mb.i_mb_y + (i / 8);
1030 bs_write( s, 8, h->fenc->plane[1][y*h->mb.pic.i_stride[1]+x] );
1033 for( i = 0; i < 8*8; i++ )
1035 const int x = 8 * h->mb.i_mb_x + (i % 8);
1036 const int y = 8 * h->mb.i_mb_y + (i / 8);
1037 bs_write( s, 8, h->fenc->plane[2][y*h->mb.pic.i_stride[2]+x] );
1039 x264_cabac_encode_init( &h->cabac, s );
1043 if( IS_INTRA( i_mb_type ) )
1046 if( i_mb_type == I_4x4 )
1048 for( i = 0; i < 16; i++ )
1050 const int i_pred = x264_mb_predict_intra4x4_mode( h, i );
1051 const int i_mode = h->mb.cache.intra4x4_pred_mode[x264_scan8[i]];
1052 x264_cabac_mb_intra4x4_pred_mode( h, i_pred, i_mode );
1055 x264_cabac_mb_intra8x8_pred_mode( h );
1057 else if( i_mb_type == P_L0 )
1059 if( h->mb.i_partition == D_16x16 )
1061 if( h->sh.i_num_ref_idx_l0_active > 1 )
1063 x264_cabac_mb_ref( h, 0, 0 );
1065 x264_cabac_mb_mvd( h, 0, 0, 4, 4 );
1067 else if( h->mb.i_partition == D_16x8 )
1069 if( h->sh.i_num_ref_idx_l0_active > 1 )
1071 x264_cabac_mb_ref( h, 0, 0 );
1072 x264_cabac_mb_ref( h, 0, 8 );
1074 x264_cabac_mb_mvd( h, 0, 0, 4, 2 );
1075 x264_cabac_mb_mvd( h, 0, 8, 4, 2 );
1077 else if( h->mb.i_partition == D_8x16 )
1079 if( h->sh.i_num_ref_idx_l0_active > 1 )
1081 x264_cabac_mb_ref( h, 0, 0 );
1082 x264_cabac_mb_ref( h, 0, 4 );
1084 x264_cabac_mb_mvd( h, 0, 0, 2, 4 );
1085 x264_cabac_mb_mvd( h, 0, 4, 2, 4 );
1088 else if( i_mb_type == P_8x8 )
1091 x264_cabac_mb_sub_p_partition( h, h->mb.i_sub_partition[0] );
1092 x264_cabac_mb_sub_p_partition( h, h->mb.i_sub_partition[1] );
1093 x264_cabac_mb_sub_p_partition( h, h->mb.i_sub_partition[2] );
1094 x264_cabac_mb_sub_p_partition( h, h->mb.i_sub_partition[3] );
1097 if( h->sh.i_num_ref_idx_l0_active > 1 )
1099 x264_cabac_mb_ref( h, 0, 0 );
1100 x264_cabac_mb_ref( h, 0, 4 );
1101 x264_cabac_mb_ref( h, 0, 8 );
1102 x264_cabac_mb_ref( h, 0, 12 );
1105 x264_cabac_mb8x8_mvd( h, 0 );
1107 else if( i_mb_type == B_8x8 )
1110 x264_cabac_mb_sub_b_partition( h, h->mb.i_sub_partition[0] );
1111 x264_cabac_mb_sub_b_partition( h, h->mb.i_sub_partition[1] );
1112 x264_cabac_mb_sub_b_partition( h, h->mb.i_sub_partition[2] );
1113 x264_cabac_mb_sub_b_partition( h, h->mb.i_sub_partition[3] );
1116 for( i_list = 0; i_list < 2; i_list++ )
1118 if( ( i_list ? h->sh.i_num_ref_idx_l1_active : h->sh.i_num_ref_idx_l0_active ) == 1 )
1120 for( i = 0; i < 4; i++ )
1122 if( x264_mb_partition_listX_table[0][ h->mb.i_sub_partition[i] ] )
1124 x264_cabac_mb_ref( h, i_list, 4*i );
1129 x264_cabac_mb8x8_mvd( h, 0 );
1130 x264_cabac_mb8x8_mvd( h, 1 );
1132 else if( i_mb_type != B_DIRECT )
1137 /* init ref list utilisations */
1138 for( i = 0; i < 2; i++ )
1140 b_list[0][i] = x264_mb_type_list0_table[i_mb_type][i];
1141 b_list[1][i] = x264_mb_type_list1_table[i_mb_type][i];
1144 for( i_list = 0; i_list < 2; i_list++ )
1146 const int i_ref_max = i_list == 0 ? h->sh.i_num_ref_idx_l0_active : h->sh.i_num_ref_idx_l1_active;
1150 if( h->mb.i_partition == D_16x16 )
1152 if( b_list[i_list][0] ) x264_cabac_mb_ref( h, i_list, 0 );
1154 else if( h->mb.i_partition == D_16x8 )
1156 if( b_list[i_list][0] ) x264_cabac_mb_ref( h, i_list, 0 );
1157 if( b_list[i_list][1] ) x264_cabac_mb_ref( h, i_list, 8 );
1159 else if( h->mb.i_partition == D_8x16 )
1161 if( b_list[i_list][0] ) x264_cabac_mb_ref( h, i_list, 0 );
1162 if( b_list[i_list][1] ) x264_cabac_mb_ref( h, i_list, 4 );
1166 for( i_list = 0; i_list < 2; i_list++ )
1168 if( h->mb.i_partition == D_16x16 )
1170 if( b_list[i_list][0] ) x264_cabac_mb_mvd( h, i_list, 0, 4, 4 );
1172 else if( h->mb.i_partition == D_16x8 )
1174 if( b_list[i_list][0] ) x264_cabac_mb_mvd( h, i_list, 0, 4, 2 );
1175 if( b_list[i_list][1] ) x264_cabac_mb_mvd( h, i_list, 8, 4, 2 );
1177 else if( h->mb.i_partition == D_8x16 )
1179 if( b_list[i_list][0] ) x264_cabac_mb_mvd( h, i_list, 0, 2, 4 );
1180 if( b_list[i_list][1] ) x264_cabac_mb_mvd( h, i_list, 4, 2, 4 );
1185 i_mb_pos_tex = bs_pos( s );
1186 h->stat.frame.i_hdr_bits += i_mb_pos_tex - i_mb_pos_start;
1188 if( i_mb_type != I_16x16 )
1190 x264_cabac_mb_cbp_luma( h );
1191 x264_cabac_mb_cbp_chroma( h );
1194 if( h->mb.i_cbp_luma > 0 || h->mb.i_cbp_chroma > 0 || i_mb_type == I_16x16 )
1196 x264_cabac_mb_qp_delta( h );
1198 /* write residual */
1199 if( i_mb_type == I_16x16 )
1202 block_residual_write_cabac( h, 0, 0, h->dct.luma16x16_dc, 16 );
1204 if( h->mb.i_cbp_luma != 0 )
1207 for( i = 0; i < 16; i++ )
1209 block_residual_write_cabac( h, 1, i, h->dct.block[i].residual_ac, 15 );
1215 for( i = 0; i < 16; i++ )
1217 if( h->mb.i_cbp_luma & ( 1 << ( i / 4 ) ) )
1219 block_residual_write_cabac( h, 2, i, h->dct.block[i].luma4x4, 16 );
1224 if( h->mb.i_cbp_chroma &0x03 ) /* Chroma DC residual present */
1226 block_residual_write_cabac( h, 3, 0, h->dct.chroma_dc[0], 4 );
1227 block_residual_write_cabac( h, 3, 1, h->dct.chroma_dc[1], 4 );
1229 if( h->mb.i_cbp_chroma&0x02 ) /* Chroma AC residual present */
1231 for( i = 0; i < 8; i++ )
1233 block_residual_write_cabac( h, 4, i, h->dct.block[16+i].residual_ac, 15 );
1238 if( IS_INTRA( i_mb_type ) )
1239 h->stat.frame.i_itex_bits += bs_pos(s) - i_mb_pos_tex;
1241 h->stat.frame.i_ptex_bits += bs_pos(s) - i_mb_pos_tex;