1 /*****************************************************************************
2 * cabac.c: h264 encoder library
3 *****************************************************************************
4 * Copyright (C) 2003-2008 x264 project
6 * Authors: Laurent Aimar <fenrir@via.ecp.fr>
7 * Loren Merritt <lorenm@u.washington.edu>
8 * Fiona Glaser <fiona@x264.com>
10 * This program is free software; you can redistribute it and/or modify
11 * it under the terms of the GNU General Public License as published by
12 * the Free Software Foundation; either version 2 of the License, or
13 * (at your option) any later version.
15 * This program is distributed in the hope that it will be useful,
16 * but WITHOUT ANY WARRANTY; without even the implied warranty of
17 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
18 * GNU General Public License for more details.
20 * You should have received a copy of the GNU General Public License
21 * along with this program; if not, write to the Free Software
22 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02111, USA.
23 *****************************************************************************/
25 #include "common/common.h"
26 #include "macroblock.h"
32 static inline void x264_cabac_mb_type_intra( x264_t *h, x264_cabac_t *cb, int i_mb_type,
33 int ctx0, int ctx1, int ctx2, int ctx3, int ctx4, int ctx5 )
35 if( i_mb_type == I_4x4 || i_mb_type == I_8x8 )
37 x264_cabac_encode_decision_noup( cb, ctx0, 0 );
39 else if( i_mb_type == I_PCM )
41 x264_cabac_encode_decision_noup( cb, ctx0, 1 );
42 x264_cabac_encode_flush( h, cb );
46 int i_pred = x264_mb_pred_mode16x16_fix[h->mb.i_intra16x16_pred_mode];
48 x264_cabac_encode_decision_noup( cb, ctx0, 1 );
49 x264_cabac_encode_terminal( cb );
51 x264_cabac_encode_decision_noup( cb, ctx1, !!h->mb.i_cbp_luma );
52 if( h->mb.i_cbp_chroma == 0 )
53 x264_cabac_encode_decision_noup( cb, ctx2, 0 );
56 x264_cabac_encode_decision( cb, ctx2, 1 );
57 x264_cabac_encode_decision_noup( cb, ctx3, h->mb.i_cbp_chroma != 1 );
59 x264_cabac_encode_decision( cb, ctx4, i_pred>>1 );
60 x264_cabac_encode_decision_noup( cb, ctx5, i_pred&1 );
64 static void x264_cabac_mb_type( x264_t *h, x264_cabac_t *cb )
66 const int i_mb_type = h->mb.i_type;
69 (!(h->mb.i_mb_y & 1) || IS_SKIP(h->mb.type[h->mb.i_mb_xy - h->mb.i_mb_stride])) )
71 x264_cabac_encode_decision_noup( cb, 70 + h->mb.cache.i_neighbour_interlaced, h->mb.b_interlaced );
74 if( h->sh.i_type == SLICE_TYPE_I )
77 if( h->mb.i_mb_type_left >= 0 && h->mb.i_mb_type_left != I_4x4 )
79 if( h->mb.i_mb_type_top >= 0 && h->mb.i_mb_type_top != I_4x4 )
82 x264_cabac_mb_type_intra( h, cb, i_mb_type, 3+ctx, 3+3, 3+4, 3+5, 3+6, 3+7 );
84 else if( h->sh.i_type == SLICE_TYPE_P )
86 /* prefix: 14, suffix: 17 */
87 if( i_mb_type == P_L0 )
89 if( h->mb.i_partition == D_16x16 )
91 x264_cabac_encode_decision_noup( cb, 14, 0 );
92 x264_cabac_encode_decision_noup( cb, 15, 0 );
93 x264_cabac_encode_decision_noup( cb, 16, 0 );
95 else if( h->mb.i_partition == D_16x8 )
97 x264_cabac_encode_decision_noup( cb, 14, 0 );
98 x264_cabac_encode_decision_noup( cb, 15, 1 );
99 x264_cabac_encode_decision_noup( cb, 17, 1 );
101 else if( h->mb.i_partition == D_8x16 )
103 x264_cabac_encode_decision_noup( cb, 14, 0 );
104 x264_cabac_encode_decision_noup( cb, 15, 1 );
105 x264_cabac_encode_decision_noup( cb, 17, 0 );
108 else if( i_mb_type == P_8x8 )
110 x264_cabac_encode_decision_noup( cb, 14, 0 );
111 x264_cabac_encode_decision_noup( cb, 15, 0 );
112 x264_cabac_encode_decision_noup( cb, 16, 1 );
117 x264_cabac_encode_decision_noup( cb, 14, 1 );
120 x264_cabac_mb_type_intra( h, cb, i_mb_type, 17+0, 17+1, 17+2, 17+2, 17+3, 17+3 );
123 else if( h->sh.i_type == SLICE_TYPE_B )
126 if( h->mb.i_mb_type_left >= 0 && h->mb.i_mb_type_left != B_SKIP && h->mb.i_mb_type_left != B_DIRECT )
128 if( h->mb.i_mb_type_top >= 0 && h->mb.i_mb_type_top != B_SKIP && h->mb.i_mb_type_top != B_DIRECT )
131 if( i_mb_type == B_DIRECT )
132 x264_cabac_encode_decision_noup( cb, 27+ctx, 0 );
133 else if( i_mb_type == B_8x8 )
135 x264_cabac_encode_decision_noup( cb, 27+ctx, 1 );
136 x264_cabac_encode_decision_noup( cb, 27+3, 1 );
137 x264_cabac_encode_decision_noup( cb, 27+4, 1 );
138 x264_cabac_encode_decision( cb, 27+5, 1 );
139 x264_cabac_encode_decision( cb, 27+5, 1 );
140 x264_cabac_encode_decision_noup( cb, 27+5, 1 );
142 else if( IS_INTRA( i_mb_type ) )
145 x264_cabac_encode_decision_noup( cb, 27+ctx, 1 );
146 x264_cabac_encode_decision_noup( cb, 27+3, 1 );
147 x264_cabac_encode_decision_noup( cb, 27+4, 1 );
148 x264_cabac_encode_decision( cb, 27+5, 1 );
149 x264_cabac_encode_decision( cb, 27+5, 0 );
150 x264_cabac_encode_decision( cb, 27+5, 1 );
153 x264_cabac_mb_type_intra( h, cb, i_mb_type, 32+0, 32+1, 32+2, 32+2, 32+3, 32+3 );
157 static const int i_mb_len[9*3] =
169 static const int i_mb_bits[9*3][7] =
171 { 1,1,0,0,0,1 }, { 1,1,0,0,1,0, }, { 1,0,0 }, /* L0 L0 */
172 { 1,1,0,1,0,1 }, { 1,1,0,1,1,0 }, {0}, /* L0 L1 */
173 { 1,1,1,0,0,0,0 }, { 1,1,1,0,0,0,1 }, {0}, /* L0 BI */
174 { 1,1,0,1,1,1 }, { 1,1,1,1,1,0 }, {0}, /* L1 L0 */
175 { 1,1,0,0,1,1 }, { 1,1,0,1,0,0 }, { 1,0,1 }, /* L1 L1 */
176 { 1,1,1,0,0,1,0 }, { 1,1,1,0,0,1,1 }, {0}, /* L1 BI */
177 { 1,1,1,0,1,0,0 }, { 1,1,1,0,1,0,1 }, {0}, /* BI L0 */
178 { 1,1,1,0,1,1,0 }, { 1,1,1,0,1,1,1 }, {0}, /* BI L1 */
179 { 1,1,1,1,0,0,0 }, { 1,1,1,1,0,0,1 }, { 1,1,0,0,0,0 }, /* BI BI */
182 const int idx = (i_mb_type - B_L0_L0) * 3 + (h->mb.i_partition - D_16x8);
185 x264_cabac_encode_decision_noup( cb, 27+ctx, i_mb_bits[idx][0] );
186 x264_cabac_encode_decision_noup( cb, 27+3, i_mb_bits[idx][1] );
187 x264_cabac_encode_decision( cb, 27+5-i_mb_bits[idx][1], i_mb_bits[idx][2] );
188 for( i = 3; i < i_mb_len[idx]; i++ )
189 x264_cabac_encode_decision( cb, 27+5, i_mb_bits[idx][i] );
194 x264_log(h, X264_LOG_ERROR, "unknown SLICE_TYPE unsupported in x264_macroblock_write_cabac\n" );
198 static void x264_cabac_mb_intra4x4_pred_mode( x264_cabac_t *cb, int i_pred, int i_mode )
200 if( i_pred == i_mode )
201 x264_cabac_encode_decision( cb, 68, 1 );
204 x264_cabac_encode_decision( cb, 68, 0 );
205 if( i_mode > i_pred )
207 x264_cabac_encode_decision( cb, 69, (i_mode )&0x01 );
208 x264_cabac_encode_decision( cb, 69, (i_mode >> 1)&0x01 );
209 x264_cabac_encode_decision( cb, 69, (i_mode >> 2)&0x01 );
213 static void x264_cabac_mb_intra_chroma_pred_mode( x264_t *h, x264_cabac_t *cb )
215 const int i_mode = x264_mb_pred_mode8x8c_fix[ h->mb.i_chroma_pred_mode ];
218 /* No need to test for I4x4 or I_16x16 as cache_save handle that */
219 if( (h->mb.i_neighbour & MB_LEFT) && h->mb.chroma_pred_mode[h->mb.i_mb_xy - 1] != 0 )
221 if( (h->mb.i_neighbour & MB_TOP) && h->mb.chroma_pred_mode[h->mb.i_mb_top_xy] != 0 )
224 x264_cabac_encode_decision_noup( cb, 64 + ctx, i_mode > 0 );
227 x264_cabac_encode_decision( cb, 64 + 3, i_mode > 1 );
229 x264_cabac_encode_decision_noup( cb, 64 + 3, i_mode > 2 );
233 static void x264_cabac_mb_cbp_luma( x264_t *h, x264_cabac_t *cb )
235 int cbp = h->mb.i_cbp_luma;
236 int cbp_l = h->mb.i_neighbour & MB_LEFT ? h->mb.cbp[h->mb.i_mb_xy - 1] : -1;
237 int cbp_t = h->mb.i_neighbour & MB_TOP ? h->mb.cbp[h->mb.i_mb_top_xy] : -1;
238 x264_cabac_encode_decision( cb, 76 - ((cbp_l >> 1) & 1) - ((cbp_t >> 1) & 2), (h->mb.i_cbp_luma >> 0) & 1 );
239 x264_cabac_encode_decision( cb, 76 - ((cbp >> 0) & 1) - ((cbp_t >> 2) & 2), (h->mb.i_cbp_luma >> 1) & 1 );
240 x264_cabac_encode_decision( cb, 76 - ((cbp_l >> 3) & 1) - ((cbp << 1) & 2), (h->mb.i_cbp_luma >> 2) & 1 );
241 x264_cabac_encode_decision_noup( cb, 76 - ((cbp >> 2) & 1) - ((cbp >> 0) & 2), (h->mb.i_cbp_luma >> 3) & 1 );
244 static void x264_cabac_mb_cbp_chroma( x264_t *h, x264_cabac_t *cb )
250 /* No need to test for SKIP/PCM */
251 if( h->mb.i_neighbour & MB_LEFT )
252 cbp_a = (h->mb.cbp[h->mb.i_mb_xy - 1] >> 4)&0x3;
254 if( h->mb.i_neighbour & MB_TOP )
255 cbp_b = (h->mb.cbp[h->mb.i_mb_top_xy] >> 4)&0x3;
258 if( cbp_a > 0 ) ctx++;
259 if( cbp_b > 0 ) ctx += 2;
260 if( h->mb.i_cbp_chroma == 0 )
261 x264_cabac_encode_decision_noup( cb, 77 + ctx, 0 );
264 x264_cabac_encode_decision_noup( cb, 77 + ctx, 1 );
267 if( cbp_a == 2 ) ctx++;
268 if( cbp_b == 2 ) ctx += 2;
269 x264_cabac_encode_decision_noup( cb, 77 + ctx, h->mb.i_cbp_chroma > 1 );
273 static void x264_cabac_mb_qp_delta( x264_t *h, x264_cabac_t *cb )
275 int i_mbn_xy = h->mb.i_mb_prev_xy;
276 int i_dqp = h->mb.i_qp - h->mb.i_last_qp;
279 /* Avoid writing a delta quant if we have an empty i16x16 block, e.g. in a completely flat background area */
280 if( h->mb.i_type == I_16x16 && !h->mb.cbp[h->mb.i_mb_xy] )
283 h->mb.i_qp = h->mb.i_last_qp;
288 /* No need to test for PCM / SKIP */
289 ctx = h->mb.i_last_dqp &&
290 ( h->mb.type[i_mbn_xy] == I_16x16 || (h->mb.cbp[i_mbn_xy]&0x3f) );
294 int val = i_dqp <= 0 ? (-2*i_dqp) : (2*i_dqp - 1);
295 /* dqp is interpreted modulo 52 */
296 if( val >= 51 && val != 52 )
300 x264_cabac_encode_decision( cb, 60 + ctx, 1 );
304 x264_cabac_encode_decision_noup( cb, 60 + ctx, 0 );
308 void x264_cabac_mb_skip( x264_t *h, int b_skip )
310 int ctx = (h->mb.i_mb_type_left >= 0 && !IS_SKIP( h->mb.i_mb_type_left ))
311 + (h->mb.i_mb_type_top >= 0 && !IS_SKIP( h->mb.i_mb_type_top ))
312 + (h->sh.i_type == SLICE_TYPE_P ? 11 : 24);
313 x264_cabac_encode_decision( &h->cabac, ctx, b_skip );
317 static inline void x264_cabac_mb_sub_p_partition( x264_cabac_t *cb, int i_sub )
319 if( i_sub == D_L0_8x8 )
320 x264_cabac_encode_decision( cb, 21, 1 );
321 else if( i_sub == D_L0_8x4 )
323 x264_cabac_encode_decision( cb, 21, 0 );
324 x264_cabac_encode_decision( cb, 22, 0 );
326 else if( i_sub == D_L0_4x8 )
328 x264_cabac_encode_decision( cb, 21, 0 );
329 x264_cabac_encode_decision( cb, 22, 1 );
330 x264_cabac_encode_decision( cb, 23, 1 );
332 else if( i_sub == D_L0_4x4 )
334 x264_cabac_encode_decision( cb, 21, 0 );
335 x264_cabac_encode_decision( cb, 22, 1 );
336 x264_cabac_encode_decision( cb, 23, 0 );
340 static NOINLINE void x264_cabac_mb_sub_b_partition( x264_cabac_t *cb, int i_sub )
342 static const uint8_t part_bits[12][7] = {
343 {6,1,1,1,0,1,1}, // D_L0_4x4
344 {5,1,1,0,0,1}, // D_L0_8x4
345 {5,1,1,0,1,0}, // D_L0_4x8
346 {3,1,0,0}, // D_L0_8x8
347 {5,1,1,1,1,0}, // D_L1_4x4
348 {5,1,1,0,1,1}, // D_L1_8x4
349 {6,1,1,1,0,0,0}, // D_L1_4x8
350 {3,1,0,1}, // D_L1_8x8
351 {5,1,1,1,1,1}, // D_BI_4x4
352 {6,1,1,1,0,0,1}, // D_BI_8x4
353 {6,1,1,1,0,1,0}, // D_BI_4x8
354 {5,1,1,0,0,0}, // D_BI_8x8
357 if( i_sub == D_DIRECT_8x8 )
359 x264_cabac_encode_decision( cb, 36, 0 );
362 len = part_bits[i_sub][0];
363 x264_cabac_encode_decision( cb, 36, part_bits[i_sub][1] );
364 x264_cabac_encode_decision( cb, 37, part_bits[i_sub][2] );
366 x264_cabac_encode_decision( cb, 39, part_bits[i_sub][3] );
369 x264_cabac_encode_decision( cb, 38, part_bits[i_sub][3] );
370 x264_cabac_encode_decision( cb, 39, part_bits[i_sub][4] );
371 x264_cabac_encode_decision( cb, 39, part_bits[i_sub][5] );
373 x264_cabac_encode_decision( cb, 39, part_bits[i_sub][6] );
377 static inline void x264_cabac_mb_transform_size( x264_t *h, x264_cabac_t *cb )
379 int ctx = 399 + h->mb.cache.i_neighbour_transform_size;
380 x264_cabac_encode_decision_noup( cb, ctx, h->mb.b_transform_8x8 );
383 static void x264_cabac_mb_ref( x264_t *h, x264_cabac_t *cb, int i_list, int idx )
385 const int i8 = x264_scan8[idx];
386 const int i_refa = h->mb.cache.ref[i_list][i8 - 1];
387 const int i_refb = h->mb.cache.ref[i_list][i8 - 8];
388 int i_ref = h->mb.cache.ref[i_list][i8];
391 if( i_refa > 0 && !h->mb.cache.skip[i8 - 1])
393 if( i_refb > 0 && !h->mb.cache.skip[i8 - 8])
398 x264_cabac_encode_decision( cb, 54 + ctx, 1 );
402 x264_cabac_encode_decision( cb, 54 + ctx, 0 );
405 static inline void x264_cabac_mb_mvd_cpn( x264_t *h, x264_cabac_t *cb, int i_list, int idx, int l, int mvd )
407 static const uint8_t ctxes[9] = { 0,3,4,5,6,6,6,6,6 };
408 const int amvd = abs( h->mb.cache.mvd[i_list][x264_scan8[idx] - 1][l] ) +
409 abs( h->mb.cache.mvd[i_list][x264_scan8[idx] - 8][l] );
410 const int i_abs = abs( mvd );
411 const int ctxbase = l ? 47 : 40;
412 int ctx = (amvd>2) + (amvd>32);
416 x264_cabac_encode_decision( cb, ctxbase + ctx, 0 );
419 x264_cabac_encode_decision( cb, ctxbase + ctx, 1 );
423 for( i = 1; i < 4; i++ )
424 x264_cabac_encode_decision( cb, ctxbase + ctxes[i], 1 );
425 cb->f8_bits_encoded += cabac_size_unary[i_abs - 3][cb->state[ctxbase+6]];
426 cb->state[ctxbase+6] = cabac_transition_unary[i_abs - 3][cb->state[ctxbase+6]];
431 for( i = 1; i < i_abs; i++ )
432 x264_cabac_encode_decision( cb, ctxbase + ctxes[i], 1 );
433 x264_cabac_encode_decision( cb, ctxbase + ctxes[i_abs], 0 );
434 x264_cabac_encode_bypass( cb, mvd < 0 );
439 x264_cabac_encode_decision( cb, ctxbase + ctx, 1 );
441 for( i = 1; i < 4; i++ )
442 x264_cabac_encode_decision( cb, ctxbase + ctxes[i], 1 );
443 cb->f8_bits_encoded += cabac_size_5ones[cb->state[ctxbase+6]];
444 cb->state[ctxbase+6] = cabac_transition_5ones[cb->state[ctxbase+6]];
445 x264_cabac_encode_ue_bypass( cb, 3, i_abs - 9 );
447 for( i = 1; i < 9; i++ )
448 x264_cabac_encode_decision( cb, ctxbase + ctxes[i], 1 );
449 x264_cabac_encode_ue_bypass( cb, 3, i_abs - 9 );
450 x264_cabac_encode_bypass( cb, mvd < 0 );
455 static inline void x264_cabac_mb_mvd( x264_t *h, x264_cabac_t *cb, int i_list, int idx, int width, int height )
457 DECLARE_ALIGNED_4( int16_t mvp[2] );
461 x264_mb_predict_mv( h, i_list, idx, width, mvp );
462 mdx = h->mb.cache.mv[i_list][x264_scan8[idx]][0] - mvp[0];
463 mdy = h->mb.cache.mv[i_list][x264_scan8[idx]][1] - mvp[1];
466 x264_cabac_mb_mvd_cpn( h, cb, i_list, idx, 0, mdx );
467 x264_cabac_mb_mvd_cpn( h, cb, i_list, idx, 1, mdy );
470 x264_macroblock_cache_mvd( h, block_idx_x[idx], block_idx_y[idx], width, height, i_list, pack16to32_mask(mdx,mdy) );
473 static inline void x264_cabac_mb8x8_mvd( x264_t *h, x264_cabac_t *cb, int i_list, int i )
475 if( !x264_mb_partition_listX_table[i_list][ h->mb.i_sub_partition[i] ] )
478 switch( h->mb.i_sub_partition[i] )
483 x264_cabac_mb_mvd( h, cb, i_list, 4*i, 2, 2 );
488 x264_cabac_mb_mvd( h, cb, i_list, 4*i+0, 2, 1 );
489 x264_cabac_mb_mvd( h, cb, i_list, 4*i+2, 2, 1 );
494 x264_cabac_mb_mvd( h, cb, i_list, 4*i+0, 1, 2 );
495 x264_cabac_mb_mvd( h, cb, i_list, 4*i+1, 1, 2 );
500 x264_cabac_mb_mvd( h, cb, i_list, 4*i+0, 1, 1 );
501 x264_cabac_mb_mvd( h, cb, i_list, 4*i+1, 1, 1 );
502 x264_cabac_mb_mvd( h, cb, i_list, 4*i+2, 1, 1 );
503 x264_cabac_mb_mvd( h, cb, i_list, 4*i+3, 1, 1 );
508 static int x264_cabac_mb_cbf_ctxidxinc( x264_t *h, int i_cat, int i_idx )
510 /* i_ctxBlockCat: 0-> DC 16x16 i_idx = 0
511 * 1-> AC 16x16 i_idx = luma4x4idx
512 * 2-> Luma4x4 i_idx = luma4x4idx
513 * 3-> DC Chroma i_idx = iCbCr
514 * 4-> AC Chroma i_idx = 4 * iCbCr + chroma4x4idx
515 * 5-> Luma8x8 i_idx = luma8x8idx
526 if( h->mb.i_neighbour & MB_LEFT )
528 i_mba_xy = h->mb.i_mb_xy - 1;
529 i_nza = h->mb.cbp[i_mba_xy] & 0x100;
531 if( h->mb.i_neighbour & MB_TOP )
533 i_mbb_xy = h->mb.i_mb_top_xy;
534 i_nzb = h->mb.cbp[i_mbb_xy] & 0x100;
539 if( i_idx & ~10 ) // block_idx_x > 0
540 i_mba_xy = h->mb.i_mb_xy;
541 else if( h->mb.i_neighbour & MB_LEFT )
542 i_mba_xy = h->mb.i_mb_xy - 1;
544 if( i_idx & ~5 ) // block_idx_y > 0
545 i_mbb_xy = h->mb.i_mb_xy;
546 else if( h->mb.i_neighbour & MB_TOP )
547 i_mbb_xy = h->mb.i_mb_top_xy;
549 /* no need to test for skip/pcm */
551 i_nza = h->mb.cache.non_zero_count[x264_scan8[i_idx] - 1];
553 i_nzb = h->mb.cache.non_zero_count[x264_scan8[i_idx] - 8];
556 /* no need to test skip/pcm */
558 if( h->mb.i_neighbour & MB_LEFT )
560 i_mba_xy = h->mb.i_mb_xy - 1;
561 i_nza = h->mb.cbp[i_mba_xy] & (0x200 << i_idx);
563 if( h->mb.i_neighbour & MB_TOP )
565 i_mbb_xy = h->mb.i_mb_top_xy;
566 i_nzb = h->mb.cbp[i_mbb_xy] & (0x200 << i_idx);
571 i_mba_xy = h->mb.i_mb_xy;
572 else if( h->mb.i_neighbour & MB_LEFT )
573 i_mba_xy = h->mb.i_mb_xy - 1;
576 i_mbb_xy = h->mb.i_mb_xy;
577 else if( h->mb.i_neighbour & MB_TOP )
578 i_mbb_xy = h->mb.i_mb_top_xy;
580 /* no need to test skip/pcm */
582 i_nza = h->mb.cache.non_zero_count[x264_scan8[i_idx] - 1];
584 i_nzb = h->mb.cache.non_zero_count[x264_scan8[i_idx] - 8];
587 if( IS_INTRA( h->mb.i_type ) )
589 i_nza |= i_mba_xy < 0;
590 i_nzb |= i_mbb_xy < 0;
593 return 4*i_cat + 2*!!i_nzb + !!i_nza;
597 static const uint16_t significant_coeff_flag_offset[2][6] = {
598 { 105, 120, 134, 149, 152, 402 },
599 { 277, 292, 306, 321, 324, 436 }
601 static const uint16_t last_coeff_flag_offset[2][6] = {
602 { 166, 181, 195, 210, 213, 417 },
603 { 338, 353, 367, 382, 385, 451 }
605 static const uint16_t coeff_abs_level_m1_offset[6] =
606 { 227, 237, 247, 257, 266, 426 };
607 static const uint8_t significant_coeff_flag_offset_8x8[2][63] =
609 0, 1, 2, 3, 4, 5, 5, 4, 4, 3, 3, 4, 4, 4, 5, 5,
610 4, 4, 4, 4, 3, 3, 6, 7, 7, 7, 8, 9,10, 9, 8, 7,
611 7, 6,11,12,13,11, 6, 7, 8, 9,14,10, 9, 8, 6,11,
612 12,13,11, 6, 9,14,10, 9,11,12,13,11,14,10,12
614 0, 1, 1, 2, 2, 3, 3, 4, 5, 6, 7, 7, 7, 8, 4, 5,
615 6, 9,10,10, 8,11,12,11, 9, 9,10,10, 8,11,12,11,
616 9, 9,10,10, 8,11,12,11, 9, 9,10,10, 8,13,13, 9,
617 9,10,10, 8,13,13, 9, 9,10,10,14,14,14,14,14
619 static const uint8_t last_coeff_flag_offset_8x8[63] = {
620 0, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1,
621 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2,
622 3, 3, 3, 3, 3, 3, 3, 3, 4, 4, 4, 4, 4, 4, 4, 4,
623 5, 5, 5, 5, 6, 6, 6, 6, 7, 7, 7, 7, 8, 8, 8
626 // node ctx: 0..3: abslevel1 (with abslevelgt1 == 0).
627 // 4..7: abslevelgt1 + 3 (and abslevel1 doesn't matter).
628 /* map node ctx => cabac ctx for level=1 */
629 static const int coeff_abs_level1_ctx[8] = { 1, 2, 3, 4, 0, 0, 0, 0 };
630 /* map node ctx => cabac ctx for level>1 */
631 static const int coeff_abs_levelgt1_ctx[8] = { 5, 5, 5, 5, 6, 7, 8, 9 };
632 static const uint8_t coeff_abs_level_transition[2][8] = {
633 /* update node ctx after coding a level=1 */
634 { 1, 2, 3, 3, 4, 5, 6, 7 },
635 /* update node ctx after coding a level>1 */
636 { 4, 4, 4, 4, 5, 6, 7, 7 }
640 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 )
642 const int i_ctx_sig = significant_coeff_flag_offset[h->mb.b_interlaced][i_ctxBlockCat];
643 const int i_ctx_last = last_coeff_flag_offset[h->mb.b_interlaced][i_ctxBlockCat];
644 const int i_ctx_level = coeff_abs_level_m1_offset[i_ctxBlockCat];
645 const uint8_t *significant_coeff_flag_offset = significant_coeff_flag_offset_8x8[h->mb.b_interlaced];
647 int i_coeff_abs_m1[64];
648 int i_coeff_sign[64];
657 /* coded block flag */
658 int ctx = 85 + x264_cabac_mb_cbf_ctxidxinc( h, i_ctxBlockCat, i_idx );
659 if( h->mb.cache.non_zero_count[x264_scan8[i_idx]] )
660 x264_cabac_encode_decision( cb, ctx, 1 );
663 x264_cabac_encode_decision( cb, ctx, 0 );
668 i_last = h->quantf.coeff_last[i_ctxBlockCat](l);
670 i_sigmap_size = X264_MIN( i_last+1, i_count-1 );
672 #define WRITE_SIGMAP( l8x8 )\
673 for( i = 0; i < i_sigmap_size; i++ )\
677 i_coeff_abs_m1[i_coeff] = abs(l[i]) - 1;\
679 i_coeff_sign[i_coeff] = l[i] < 0;\
681 x264_cabac_encode_decision( cb, i_ctx_sig + (l8x8 ? significant_coeff_flag_offset[i] : i), 1 );\
682 x264_cabac_encode_decision( cb, i_ctx_last + (l8x8 ? last_coeff_flag_offset_8x8[i] : i), i == i_last );\
685 x264_cabac_encode_decision( cb, i_ctx_sig + (l8x8 ? significant_coeff_flag_offset[i] : i), 0 );\
688 if( i_ctxBlockCat == DCT_LUMA_8x8 )
695 i_coeff_abs_m1[i_coeff] = abs(l[i]) - 1;
696 i_coeff_sign[i_coeff] = l[i] < 0;
705 /* write coeff_abs - 1 */
706 i_prefix = X264_MIN( i_coeff_abs_m1[i_coeff], 14 );
707 ctx = coeff_abs_level1_ctx[node_ctx] + i_ctx_level;
711 x264_cabac_encode_decision( cb, ctx, 1 );
712 ctx = coeff_abs_levelgt1_ctx[node_ctx] + i_ctx_level;
713 for( i = 0; i < i_prefix - 1; i++ )
714 x264_cabac_encode_decision( cb, ctx, 1 );
716 x264_cabac_encode_decision( cb, ctx, 0 );
718 x264_cabac_encode_ue_bypass( cb, 0, i_coeff_abs_m1[i_coeff] - 14 );
720 node_ctx = coeff_abs_level_transition[1][node_ctx];
724 x264_cabac_encode_decision( cb, ctx, 0 );
725 node_ctx = coeff_abs_level_transition[0][node_ctx];
728 x264_cabac_encode_bypass( cb, i_coeff_sign[i_coeff] );
729 } while( i_coeff > 0 );
731 #define block_residual_write_cabac_8x8( h, cb, idx, l ) block_residual_write_cabac( h, cb, DCT_LUMA_8x8, idx, l, 64 )
735 /* Faster RDO by merging sigmap and level coding. Note that for 8x8dct
736 * this is slightly incorrect because the sigmap is not reversible
737 * (contexts are repeated). However, there is nearly no quality penalty
738 * for this (~0.001db) and the speed boost (~30%) is worth it. */
739 static void ALWAYS_INLINE block_residual_write_cabac_internal( x264_t *h, x264_cabac_t *cb, int i_ctxBlockCat, int i_idx, int16_t *l, int i_count, int b_8x8 )
741 const int i_ctx_sig = significant_coeff_flag_offset[h->mb.b_interlaced][i_ctxBlockCat];
742 const int i_ctx_last = last_coeff_flag_offset[h->mb.b_interlaced][i_ctxBlockCat];
743 const int i_ctx_level = coeff_abs_level_m1_offset[i_ctxBlockCat];
744 const uint8_t *significant_coeff_flag_offset = significant_coeff_flag_offset_8x8[h->mb.b_interlaced];
745 int i_last, i_coeff_abs_m1, ctx, i_prefix, i, node_ctx;
749 /* coded block flag */
750 ctx = 85 + x264_cabac_mb_cbf_ctxidxinc( h, i_ctxBlockCat, i_idx );
751 if( h->mb.cache.non_zero_count[x264_scan8[i_idx]] )
752 x264_cabac_encode_decision( cb, ctx, 1 );
755 x264_cabac_encode_decision( cb, ctx, 0 );
760 i_last = h->quantf.coeff_last[i_ctxBlockCat](l);
762 i_coeff_abs_m1 = abs(l[i_last]) - 1;
763 i_prefix = X264_MIN( i_coeff_abs_m1, 14 );
764 ctx = coeff_abs_level1_ctx[0] + i_ctx_level;
766 if( i_last != i_count - 1 )
768 x264_cabac_encode_decision( cb, i_ctx_sig + (b_8x8?significant_coeff_flag_offset[i_last]:i_last), 1 );
769 x264_cabac_encode_decision( cb, i_ctx_last + (b_8x8?last_coeff_flag_offset_8x8[i_last]:i_last), 1 );
774 x264_cabac_encode_decision( cb, ctx, 1 );
775 ctx = coeff_abs_levelgt1_ctx[0] + i_ctx_level;
776 cb->f8_bits_encoded += cabac_size_unary[i_prefix][cb->state[ctx]];
777 cb->state[ctx] = cabac_transition_unary[i_prefix][cb->state[ctx]];
779 x264_cabac_encode_ue_bypass( cb, 0, i_coeff_abs_m1 - 14 );
780 node_ctx = coeff_abs_level_transition[1][0];
784 x264_cabac_encode_decision( cb, ctx, 0 );
785 node_ctx = coeff_abs_level_transition[0][0];
786 x264_cabac_encode_bypass( cb, 0 ); // sign
789 for( i = i_last-1 ; i >= 0; i-- )
793 x264_cabac_encode_decision( cb, i_ctx_sig + (b_8x8?significant_coeff_flag_offset[i]:i), 1 );
794 x264_cabac_encode_decision( cb, i_ctx_last + (b_8x8?last_coeff_flag_offset_8x8[i]:i), 0 );
795 ctx = coeff_abs_level1_ctx[node_ctx] + i_ctx_level;
797 if( (unsigned)(l[i]+1) > 2 )
799 i_coeff_abs_m1 = abs(l[i]) - 1;
800 i_prefix = X264_MIN( i_coeff_abs_m1, 14 );
801 x264_cabac_encode_decision( cb, ctx, 1 );
802 ctx = coeff_abs_levelgt1_ctx[node_ctx] + i_ctx_level;
803 cb->f8_bits_encoded += cabac_size_unary[i_prefix][cb->state[ctx]];
804 cb->state[ctx] = cabac_transition_unary[i_prefix][cb->state[ctx]];
806 x264_cabac_encode_ue_bypass( cb, 0, i_coeff_abs_m1 - 14 );
807 node_ctx = coeff_abs_level_transition[1][node_ctx];
811 x264_cabac_encode_decision( cb, ctx, 0 );
812 node_ctx = coeff_abs_level_transition[0][node_ctx];
813 x264_cabac_encode_bypass( cb, 0 );
817 x264_cabac_encode_decision( cb, i_ctx_sig + (b_8x8?significant_coeff_flag_offset[i]:i), 0 );
821 static void block_residual_write_cabac_8x8( x264_t *h, x264_cabac_t *cb, int i_idx, int16_t *l )
823 block_residual_write_cabac_internal( h, cb, DCT_LUMA_8x8, i_idx, l, 64, 1 );
825 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 )
827 block_residual_write_cabac_internal( h, cb, i_ctxBlockCat, i_idx, l, i_count, 0 );
831 void x264_macroblock_write_cabac( x264_t *h, x264_cabac_t *cb )
833 const int i_mb_type = h->mb.i_type;
838 const int i_mb_pos_start = x264_cabac_pos( cb );
842 /* Write the MB type */
843 x264_cabac_mb_type( h, cb );
846 if( i_mb_type == I_PCM )
848 i_mb_pos_tex = x264_cabac_pos( cb );
849 h->stat.frame.i_mv_bits += i_mb_pos_tex - i_mb_pos_start;
851 memcpy( cb->p, h->mb.pic.p_fenc[0], 256 );
853 for( i = 0; i < 8; i++ )
854 memcpy( cb->p + i*8, h->mb.pic.p_fenc[1] + i*FENC_STRIDE, 8 );
856 for( i = 0; i < 8; i++ )
857 memcpy( cb->p + i*8, h->mb.pic.p_fenc[2] + i*FENC_STRIDE, 8 );
861 cb->i_range = 0x01FE;
863 cb->i_bytes_outstanding = 0;
865 /* if PCM is chosen, we need to store reconstructed frame data */
866 h->mc.copy[PIXEL_16x16]( h->mb.pic.p_fdec[0], FDEC_STRIDE, h->mb.pic.p_fenc[0], FENC_STRIDE, 16 );
867 h->mc.copy[PIXEL_8x8] ( h->mb.pic.p_fdec[1], FDEC_STRIDE, h->mb.pic.p_fenc[1], FENC_STRIDE, 8 );
868 h->mc.copy[PIXEL_8x8] ( h->mb.pic.p_fdec[2], FDEC_STRIDE, h->mb.pic.p_fenc[2], FENC_STRIDE, 8 );
870 h->stat.frame.i_tex_bits += x264_cabac_pos( cb ) - i_mb_pos_tex;
875 if( IS_INTRA( i_mb_type ) )
877 if( h->pps->b_transform_8x8_mode && i_mb_type != I_16x16 )
878 x264_cabac_mb_transform_size( h, cb );
880 if( i_mb_type != I_16x16 )
882 int di = (i_mb_type == I_8x8) ? 4 : 1;
883 for( i = 0; i < 16; i += di )
885 const int i_pred = x264_mb_predict_intra4x4_mode( h, i );
886 const int i_mode = x264_mb_pred_mode4x4_fix( h->mb.cache.intra4x4_pred_mode[x264_scan8[i]] );
887 x264_cabac_mb_intra4x4_pred_mode( cb, i_pred, i_mode );
891 x264_cabac_mb_intra_chroma_pred_mode( h, cb );
893 else if( i_mb_type == P_L0 )
895 if( h->mb.i_partition == D_16x16 )
897 if( h->mb.pic.i_fref[0] > 1 )
899 x264_cabac_mb_ref( h, cb, 0, 0 );
901 x264_cabac_mb_mvd( h, cb, 0, 0, 4, 4 );
903 else if( h->mb.i_partition == D_16x8 )
905 if( h->mb.pic.i_fref[0] > 1 )
907 x264_cabac_mb_ref( h, cb, 0, 0 );
908 x264_cabac_mb_ref( h, cb, 0, 8 );
910 x264_cabac_mb_mvd( h, cb, 0, 0, 4, 2 );
911 x264_cabac_mb_mvd( h, cb, 0, 8, 4, 2 );
913 else if( h->mb.i_partition == D_8x16 )
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 );
920 x264_cabac_mb_mvd( h, cb, 0, 0, 2, 4 );
921 x264_cabac_mb_mvd( h, cb, 0, 4, 2, 4 );
924 else if( i_mb_type == P_8x8 )
927 x264_cabac_mb_sub_p_partition( cb, h->mb.i_sub_partition[0] );
928 x264_cabac_mb_sub_p_partition( cb, h->mb.i_sub_partition[1] );
929 x264_cabac_mb_sub_p_partition( cb, h->mb.i_sub_partition[2] );
930 x264_cabac_mb_sub_p_partition( cb, h->mb.i_sub_partition[3] );
933 if( h->mb.pic.i_fref[0] > 1 )
935 x264_cabac_mb_ref( h, cb, 0, 0 );
936 x264_cabac_mb_ref( h, cb, 0, 4 );
937 x264_cabac_mb_ref( h, cb, 0, 8 );
938 x264_cabac_mb_ref( h, cb, 0, 12 );
941 for( i = 0; i < 4; i++ )
942 x264_cabac_mb8x8_mvd( h, cb, 0, i );
944 else if( i_mb_type == B_8x8 )
947 x264_cabac_mb_sub_b_partition( cb, h->mb.i_sub_partition[0] );
948 x264_cabac_mb_sub_b_partition( cb, h->mb.i_sub_partition[1] );
949 x264_cabac_mb_sub_b_partition( cb, h->mb.i_sub_partition[2] );
950 x264_cabac_mb_sub_b_partition( cb, h->mb.i_sub_partition[3] );
953 for( i_list = 0; i_list < 2; i_list++ )
955 if( ( i_list ? h->mb.pic.i_fref[1] : h->mb.pic.i_fref[0] ) == 1 )
957 for( i = 0; i < 4; i++ )
958 if( x264_mb_partition_listX_table[i_list][ h->mb.i_sub_partition[i] ] )
959 x264_cabac_mb_ref( h, cb, i_list, 4*i );
962 for( i = 0; i < 4; i++ )
963 x264_cabac_mb8x8_mvd( h, cb, 0, i );
964 for( i = 0; i < 4; i++ )
965 x264_cabac_mb8x8_mvd( h, cb, 1, i );
967 else if( i_mb_type != B_DIRECT )
970 const uint8_t (*b_list)[2] = x264_mb_type_list_table[i_mb_type];
972 for( i_list = 0; i_list < 2; i_list++ )
974 const int i_ref_max = h->mb.pic.i_fref[i_list];
978 if( h->mb.i_partition == D_16x16 )
980 if( b_list[i_list][0] ) x264_cabac_mb_ref( h, cb, i_list, 0 );
982 else if( h->mb.i_partition == D_16x8 )
984 if( b_list[i_list][0] ) x264_cabac_mb_ref( h, cb, i_list, 0 );
985 if( b_list[i_list][1] ) x264_cabac_mb_ref( h, cb, i_list, 8 );
987 else if( h->mb.i_partition == D_8x16 )
989 if( b_list[i_list][0] ) x264_cabac_mb_ref( h, cb, i_list, 0 );
990 if( b_list[i_list][1] ) x264_cabac_mb_ref( h, cb, i_list, 4 );
994 for( i_list = 0; i_list < 2; i_list++ )
996 if( h->mb.i_partition == D_16x16 )
998 if( b_list[i_list][0] ) x264_cabac_mb_mvd( h, cb, i_list, 0, 4, 4 );
1000 else if( h->mb.i_partition == D_16x8 )
1002 if( b_list[i_list][0] ) x264_cabac_mb_mvd( h, cb, i_list, 0, 4, 2 );
1003 if( b_list[i_list][1] ) x264_cabac_mb_mvd( h, cb, i_list, 8, 4, 2 );
1005 else if( h->mb.i_partition == D_8x16 )
1007 if( b_list[i_list][0] ) x264_cabac_mb_mvd( h, cb, i_list, 0, 2, 4 );
1008 if( b_list[i_list][1] ) x264_cabac_mb_mvd( h, cb, i_list, 4, 2, 4 );
1014 i_mb_pos_tex = x264_cabac_pos( cb );
1015 h->stat.frame.i_mv_bits += i_mb_pos_tex - i_mb_pos_start;
1018 if( i_mb_type != I_16x16 )
1020 x264_cabac_mb_cbp_luma( h, cb );
1021 x264_cabac_mb_cbp_chroma( h, cb );
1024 if( x264_mb_transform_8x8_allowed( h ) && h->mb.i_cbp_luma )
1026 x264_cabac_mb_transform_size( h, cb );
1029 if( h->mb.i_cbp_luma > 0 || h->mb.i_cbp_chroma > 0 || i_mb_type == I_16x16 )
1031 x264_cabac_mb_qp_delta( h, cb );
1033 /* write residual */
1034 if( i_mb_type == I_16x16 )
1037 block_residual_write_cabac( h, cb, DCT_LUMA_DC, 24, h->dct.luma16x16_dc, 16 );
1040 if( h->mb.i_cbp_luma != 0 )
1041 for( i = 0; i < 16; i++ )
1042 block_residual_write_cabac( h, cb, DCT_LUMA_AC, i, h->dct.luma4x4[i]+1, 15 );
1044 else if( h->mb.b_transform_8x8 )
1046 for( i = 0; i < 4; i++ )
1047 if( h->mb.i_cbp_luma & ( 1 << i ) )
1048 block_residual_write_cabac_8x8( h, cb, i, h->dct.luma8x8[i] );
1052 for( i = 0; i < 16; i++ )
1053 if( h->mb.i_cbp_luma & ( 1 << ( i / 4 ) ) )
1054 block_residual_write_cabac( h, cb, DCT_LUMA_4x4, i, h->dct.luma4x4[i], 16 );
1057 if( h->mb.i_cbp_chroma &0x03 ) /* Chroma DC residual present */
1059 block_residual_write_cabac( h, cb, DCT_CHROMA_DC, 25, h->dct.chroma_dc[0], 4 );
1060 block_residual_write_cabac( h, cb, DCT_CHROMA_DC, 26, h->dct.chroma_dc[1], 4 );
1062 if( h->mb.i_cbp_chroma&0x02 ) /* Chroma AC residual present */
1064 for( i = 16; i < 24; i++ )
1065 block_residual_write_cabac( h, cb, DCT_CHROMA_AC, i, h->dct.luma4x4[i]+1, 15 );
1070 h->stat.frame.i_tex_bits += x264_cabac_pos( cb ) - i_mb_pos_tex;
1075 /*****************************************************************************
1076 * RD only; doesn't generate a valid bitstream
1077 * doesn't write cbp or chroma dc (I don't know how much this matters)
1078 * doesn't write ref or subpartition (never varies between calls, so no point in doing so)
1079 * works on all partition sizes except 16x16
1080 * for sub8x8, call once per 8x8 block
1081 *****************************************************************************/
1082 static void x264_partition_size_cabac( x264_t *h, x264_cabac_t *cb, int i8, int i_pixel )
1084 const int i_mb_type = h->mb.i_type;
1085 int b_8x16 = h->mb.i_partition == D_8x16;
1088 if( i_mb_type == P_8x8 )
1089 x264_cabac_mb8x8_mvd( h, cb, 0, i8 );
1090 else if( i_mb_type == P_L0 )
1091 x264_cabac_mb_mvd( h, cb, 0, 4*i8, 4>>b_8x16, 2<<b_8x16 );
1092 else if( i_mb_type > B_DIRECT && i_mb_type < B_8x8 )
1094 if( x264_mb_type_list_table[ i_mb_type ][0][!!i8] ) x264_cabac_mb_mvd( h, cb, 0, 4*i8, 4>>b_8x16, 2<<b_8x16 );
1095 if( x264_mb_type_list_table[ i_mb_type ][1][!!i8] ) x264_cabac_mb_mvd( h, cb, 1, 4*i8, 4>>b_8x16, 2<<b_8x16 );
1097 else if( i_mb_type == B_8x8 )
1099 x264_cabac_mb8x8_mvd( h, cb, 0, i8 );
1100 x264_cabac_mb8x8_mvd( h, cb, 1, i8 );
1104 x264_log(h, X264_LOG_ERROR, "invalid/unhandled mb_type %d\n",i_mb_type );
1108 for( j = (i_pixel < PIXEL_8x8); j >= 0; j-- )
1110 if( h->mb.i_cbp_luma & (1 << i8) )
1112 if( h->mb.b_transform_8x8 )
1113 block_residual_write_cabac_8x8( h, cb, i8, h->dct.luma8x8[i8] );
1117 for( i4 = 0; i4 < 4; i4++ )
1118 block_residual_write_cabac( h, cb, DCT_LUMA_4x4, i4+i8*4, h->dct.luma4x4[i4+i8*4], 16 );
1122 block_residual_write_cabac( h, cb, DCT_CHROMA_AC, 16+i8, h->dct.luma4x4[16+i8]+1, 15 );
1123 block_residual_write_cabac( h, cb, DCT_CHROMA_AC, 20+i8, h->dct.luma4x4[20+i8]+1, 15 );
1125 i8 += x264_pixel_size[i_pixel].h >> 3;
1129 static void x264_subpartition_size_cabac( x264_t *h, x264_cabac_t *cb, int i4, int i_pixel )
1131 int b_8x4 = i_pixel == PIXEL_8x4;
1132 block_residual_write_cabac( h, cb, DCT_LUMA_4x4, i4, h->dct.luma4x4[i4], 16 );
1133 if( i_pixel == PIXEL_4x4 )
1134 x264_cabac_mb_mvd( h, cb, 0, i4, 1, 1 );
1137 x264_cabac_mb_mvd( h, cb, 0, i4, 1+b_8x4, 2-b_8x4 );
1138 block_residual_write_cabac( h, cb, DCT_LUMA_4x4, i4+2-b_8x4, h->dct.luma4x4[i4+2-b_8x4], 16 );
1142 static void x264_partition_i8x8_size_cabac( x264_t *h, x264_cabac_t *cb, int i8, int i_mode )
1144 const int i_pred = x264_mb_predict_intra4x4_mode( h, 4*i8 );
1145 i_mode = x264_mb_pred_mode4x4_fix( i_mode );
1146 x264_cabac_mb_intra4x4_pred_mode( cb, i_pred, i_mode );
1147 if( h->mb.i_cbp_luma & (1 << i8) )
1148 block_residual_write_cabac_8x8( h, cb, 4*i8, h->dct.luma8x8[i8] );
1151 static void x264_partition_i4x4_size_cabac( x264_t *h, x264_cabac_t *cb, int i4, int i_mode )
1153 const int i_pred = x264_mb_predict_intra4x4_mode( h, i4 );
1154 i_mode = x264_mb_pred_mode4x4_fix( i_mode );
1155 x264_cabac_mb_intra4x4_pred_mode( cb, i_pred, i_mode );
1156 block_residual_write_cabac( h, cb, DCT_LUMA_4x4, i4, h->dct.luma4x4[i4], 16 );
1159 static void x264_i8x8_chroma_size_cabac( x264_t *h, x264_cabac_t *cb )
1161 x264_cabac_mb_intra_chroma_pred_mode( h, cb );
1162 x264_cabac_mb_cbp_chroma( h, cb );
1163 if( h->mb.i_cbp_chroma > 0 )
1165 block_residual_write_cabac( h, cb, DCT_CHROMA_DC, 25, h->dct.chroma_dc[0], 4 );
1166 block_residual_write_cabac( h, cb, DCT_CHROMA_DC, 26, h->dct.chroma_dc[1], 4 );
1168 if( h->mb.i_cbp_chroma == 2 )
1171 for( i = 16; i < 24; i++ )
1172 block_residual_write_cabac( h, cb, DCT_CHROMA_AC, i, h->dct.luma4x4[i]+1, 15 );