1 /*****************************************************************************
2 * cavlc.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 const uint8_t intra4x4_cbp_to_golomb[48]=
34 3, 29, 30, 17, 31, 18, 37, 8, 32, 38, 19, 9, 20, 10, 11, 2,
35 16, 33, 34, 21, 35, 22, 39, 4, 36, 40, 23, 5, 24, 6, 7, 1,
36 41, 42, 43, 25, 44, 26, 46, 12, 45, 47, 27, 13, 28, 14, 15, 0
38 static const uint8_t inter_cbp_to_golomb[48]=
40 0, 2, 3, 7, 4, 8, 17, 13, 5, 18, 9, 14, 10, 15, 16, 11,
41 1, 32, 33, 36, 34, 37, 44, 40, 35, 45, 38, 41, 39, 42, 43, 19,
42 6, 24, 25, 20, 26, 21, 46, 28, 27, 47, 22, 29, 23, 30, 31, 12
44 static const uint8_t mb_type_b_to_golomb[3][9]=
46 { 4, 8, 12, 10, 6, 14, 16, 18, 20 }, /* D_16x8 */
47 { 5, 9, 13, 11, 7, 15, 17, 19, 21 }, /* D_8x16 */
48 { 1, -1, -1, -1, 2, -1, -1, -1, 3 } /* D_16x16 */
50 static const uint8_t sub_mb_type_p_to_golomb[4]=
54 static const uint8_t sub_mb_type_b_to_golomb[13]=
56 10, 4, 5, 1, 11, 6, 7, 2, 12, 8, 9, 3, 0
59 #define bs_write_vlc(s,v) bs_write( s, (v).i_size, (v).i_bits )
61 /****************************************************************************
62 * block_residual_write_cavlc:
63 ****************************************************************************/
64 static inline int block_residual_write_cavlc_escape( x264_t *h, bs_t *s, int i_suffix_length, int level )
66 static const uint16_t next_suffix[7] = { 0, 3, 6, 12, 24, 48, 0xffff };
67 int i_level_prefix = 15;
68 int mask = level >> 15;
69 int abs_level = (level^mask)-mask;
70 int i_level_code = abs_level*2-mask-2;
71 if( ( i_level_code >> i_suffix_length ) < 15 )
73 bs_write( s, (i_level_code >> i_suffix_length) + 1 + i_suffix_length,
74 (1<<i_suffix_length) + (i_level_code & ((1<<i_suffix_length)-1)) );
78 i_level_code -= 15 << i_suffix_length;
79 if( i_suffix_length == 0 )
82 /* If the prefix size exceeds 15, High Profile is required. */
83 if( i_level_code >= 1<<12 )
85 if( h->sps->i_profile_idc >= PROFILE_HIGH )
87 while( i_level_code > 1<<(i_level_prefix-3) )
89 i_level_code -= 1<<(i_level_prefix-3);
96 /* Weight highly against overflows. */
97 s->i_bits_encoded += 1000000;
99 x264_log(h, X264_LOG_WARNING, "OVERFLOW levelcode=%d is only allowed in High Profile", i_level_code );
100 /* clip level, preserving sign */
101 i_level_code = (1<<12) - 2 + (i_level_code & 1);
105 bs_write( s, i_level_prefix + 1, 1 );
106 bs_write( s, i_level_prefix - 3, i_level_code & ((1<<(i_level_prefix-3))-1) );
108 if( i_suffix_length == 0 )
110 if( abs_level > next_suffix[i_suffix_length] )
112 return i_suffix_length;
115 static void block_residual_write_cavlc( x264_t *h, bs_t *s, int i_ctxBlockCat, int i_idx, int16_t *l, int i_count )
117 static const uint8_t ct_index[17] = {0,0,1,1,2,2,2,2,3,3,3,3,3,3,3,3,3};
118 static const uint8_t ctz_index[8] = {3,0,1,0,2,0,1,0};
119 int level[16], run[16];
120 int i_trailing, i_total_zero, i_last, i_suffix_length, i;
123 /* x264_mb_predict_non_zero_code return 0 <-> (16+16+1)>>1 = 16 */
124 int nC = i_idx >= 25 ? 4 : ct_index[x264_mb_predict_non_zero_code( h, i_idx == 24 ? 0 : i_idx )];
126 if( !h->mb.cache.non_zero_count[x264_scan8[i_idx]] )
128 bs_write_vlc( s, x264_coeff_token[nC][0] );
132 i_last = h->quantf.coeff_last[i_ctxBlockCat](l);
133 i_total_zero = i_last + 1;
135 /* level and run and total */
136 /* set these to 2 to allow branchless i_trailing calculation */
142 level[i_total] = l[i_last];
143 while( --i_last >= 0 && l[i_last] == 0 )
146 } while( i_last >= 0 );
148 h->mb.cache.non_zero_count[x264_scan8[i_idx]] = i_total;
150 i_total_zero -= i_total;
151 i_trailing = ((((level[0]+1) | (1-level[0])) >> 31) & 1) // abs(level[0])>1
152 | ((((level[1]+1) | (1-level[1])) >> 31) & 2)
153 | ((((level[2]+1) | (1-level[2])) >> 31) & 4);
154 i_trailing = ctz_index[i_trailing];
155 i_sign = ((level[2] >> 31) & 1)
156 | ((level[1] >> 31) & 2)
157 | ((level[0] >> 31) & 4);
158 i_sign >>= 3-i_trailing;
161 bs_write_vlc( s, x264_coeff_token[nC][i_total*4+i_trailing] );
163 i_suffix_length = i_total > 10 && i_trailing < 3;
164 if( i_trailing > 0 || RDO_SKIP_BS )
165 bs_write( s, i_trailing, i_sign );
167 if( i_trailing < i_total )
169 int16_t val = level[i_trailing];
170 int16_t val_original = level[i_trailing]+LEVEL_TABLE_SIZE/2;
172 val -= (val>>15)|1; /* as level[i] can't be 1 for the first one if i_trailing < 3 */
173 val += LEVEL_TABLE_SIZE/2;
175 if( (unsigned)val_original < LEVEL_TABLE_SIZE )
177 bs_write_vlc( s, x264_level_token[i_suffix_length][val] );
178 i_suffix_length = x264_level_token[i_suffix_length][val_original].i_next;
181 i_suffix_length = block_residual_write_cavlc_escape( h, s, i_suffix_length, val-LEVEL_TABLE_SIZE/2 );
182 for( i = i_trailing+1; i < i_total; i++ )
184 val = level[i] + LEVEL_TABLE_SIZE/2;
185 if( (unsigned)val < LEVEL_TABLE_SIZE )
187 bs_write_vlc( s, x264_level_token[i_suffix_length][val] );
188 i_suffix_length = x264_level_token[i_suffix_length][val].i_next;
191 i_suffix_length = block_residual_write_cavlc_escape( h, s, i_suffix_length, val-LEVEL_TABLE_SIZE/2 );
195 if( i_total < i_count )
198 bs_write_vlc( s, x264_total_zeros_dc[i_total-1][i_total_zero] );
200 bs_write_vlc( s, x264_total_zeros[i_total-1][i_total_zero] );
203 for( i = 0; i < i_total-1 && i_total_zero > 0; i++ )
205 int i_zl = X264_MIN( i_total_zero - 1, 6 );
206 bs_write_vlc( s, x264_run_before[i_zl][run[i]] );
207 i_total_zero -= run[i];
211 static void cavlc_qp_delta( x264_t *h, bs_t *s )
213 int i_dqp = h->mb.i_qp - h->mb.i_last_qp;
215 /* Avoid writing a delta quant if we have an empty i16x16 block, e.g. in a completely flat background area */
216 if( h->mb.i_type == I_16x16 && !(h->mb.i_cbp_luma | h->mb.i_cbp_chroma)
217 && !h->mb.cache.non_zero_count[x264_scan8[24]] )
220 h->mb.i_qp = h->mb.i_last_qp;
229 else if( i_dqp > 25 )
232 bs_write_se( s, i_dqp );
235 static void cavlc_mb_mvd( x264_t *h, bs_t *s, int i_list, int idx, int width )
237 DECLARE_ALIGNED_4( int16_t mvp[2] );
238 x264_mb_predict_mv( h, i_list, idx, width, mvp );
239 bs_write_se( s, h->mb.cache.mv[i_list][x264_scan8[idx]][0] - mvp[0] );
240 bs_write_se( s, h->mb.cache.mv[i_list][x264_scan8[idx]][1] - mvp[1] );
243 static void cavlc_mb8x8_mvd( x264_t *h, bs_t *s, int i_list, int i )
245 if( !x264_mb_partition_listX_table[i_list][ h->mb.i_sub_partition[i] ] )
248 switch( h->mb.i_sub_partition[i] )
253 cavlc_mb_mvd( h, s, i_list, 4*i, 2 );
258 cavlc_mb_mvd( h, s, i_list, 4*i+0, 2 );
259 cavlc_mb_mvd( h, s, i_list, 4*i+2, 2 );
264 cavlc_mb_mvd( h, s, i_list, 4*i+0, 1 );
265 cavlc_mb_mvd( h, s, i_list, 4*i+1, 1 );
270 cavlc_mb_mvd( h, s, i_list, 4*i+0, 1 );
271 cavlc_mb_mvd( h, s, i_list, 4*i+1, 1 );
272 cavlc_mb_mvd( h, s, i_list, 4*i+2, 1 );
273 cavlc_mb_mvd( h, s, i_list, 4*i+3, 1 );
278 static inline void x264_macroblock_luma_write_cavlc( x264_t *h, bs_t *s, int i8start, int i8end )
281 if( h->mb.b_transform_8x8 )
283 /* shuffle 8x8 dct coeffs into 4x4 lists */
284 for( i8 = i8start; i8 <= i8end; i8++ )
285 if( h->mb.i_cbp_luma & (1 << i8) )
287 h->zigzagf.interleave_8x8_cavlc( h->dct.luma4x4[i8*4], h->dct.luma8x8[i8] );
288 for( i4 = 0; i4 < 4; i4++ )
289 h->mb.cache.non_zero_count[x264_scan8[i4+i8*4]] = array_non_zero( h->dct.luma4x4[i4+i8*4] );
293 for( i8 = i8start; i8 <= i8end; i8++ )
294 if( h->mb.i_cbp_luma & (1 << i8) )
295 for( i4 = 0; i4 < 4; i4++ )
296 block_residual_write_cavlc( h, s, DCT_LUMA_4x4, i4+i8*4, h->dct.luma4x4[i4+i8*4], 16 );
299 /*****************************************************************************
300 * x264_macroblock_write:
301 *****************************************************************************/
302 void x264_macroblock_write_cavlc( x264_t *h, bs_t *s )
304 const int i_mb_type = h->mb.i_type;
309 const int i_mb_pos_start = bs_pos( s );
313 switch( h->sh.i_type )
325 x264_log(h, X264_LOG_ERROR, "internal error or slice unsupported\n" );
330 && (!(h->mb.i_mb_y & 1) || IS_SKIP(h->mb.type[h->mb.i_mb_xy - h->mb.i_mb_stride])) )
332 bs_write1( s, h->mb.b_interlaced );
336 if( i_mb_type == I_PCM)
338 bs_write_ue( s, i_mb_i_offset + 25 );
339 i_mb_pos_tex = bs_pos( s );
340 h->stat.frame.i_mv_bits += i_mb_pos_tex - i_mb_pos_start;
344 memcpy( s->p, h->mb.pic.p_fenc[0], 256 );
346 for( i = 0; i < 8; i++ )
347 memcpy( s->p + i*8, h->mb.pic.p_fenc[1] + i*FENC_STRIDE, 8 );
349 for( i = 0; i < 8; i++ )
350 memcpy( s->p + i*8, h->mb.pic.p_fenc[2] + i*FENC_STRIDE, 8 );
353 /* if PCM is chosen, we need to store reconstructed frame data */
354 h->mc.copy[PIXEL_16x16]( h->mb.pic.p_fdec[0], FDEC_STRIDE, h->mb.pic.p_fenc[0], FENC_STRIDE, 16 );
355 h->mc.copy[PIXEL_8x8] ( h->mb.pic.p_fdec[1], FDEC_STRIDE, h->mb.pic.p_fenc[1], FENC_STRIDE, 8 );
356 h->mc.copy[PIXEL_8x8] ( h->mb.pic.p_fdec[2], FDEC_STRIDE, h->mb.pic.p_fenc[2], FENC_STRIDE, 8 );
358 h->stat.frame.i_tex_bits += bs_pos(s) - i_mb_pos_tex;
367 if( i_mb_type == I_4x4 || i_mb_type == I_8x8 )
369 int di = i_mb_type == I_8x8 ? 4 : 1;
370 bs_write_ue( s, i_mb_i_offset + 0 );
371 if( h->pps->b_transform_8x8_mode )
372 bs_write1( s, h->mb.b_transform_8x8 );
374 /* Prediction: Luma */
375 for( i = 0; i < 16; i += di )
377 int i_pred = x264_mb_predict_intra4x4_mode( h, i );
378 int i_mode = x264_mb_pred_mode4x4_fix( h->mb.cache.intra4x4_pred_mode[x264_scan8[i]] );
380 if( i_pred == i_mode )
381 bs_write1( s, 1 ); /* b_prev_intra4x4_pred_mode */
383 bs_write( s, 4, i_mode - (i_mode > i_pred) );
385 bs_write_ue( s, x264_mb_pred_mode8x8c_fix[ h->mb.i_chroma_pred_mode ] );
387 else if( i_mb_type == I_16x16 )
389 bs_write_ue( s, i_mb_i_offset + 1 + x264_mb_pred_mode16x16_fix[h->mb.i_intra16x16_pred_mode] +
390 h->mb.i_cbp_chroma * 4 + ( h->mb.i_cbp_luma == 0 ? 0 : 12 ) );
391 bs_write_ue( s, x264_mb_pred_mode8x8c_fix[ h->mb.i_chroma_pred_mode ] );
393 else if( i_mb_type == P_L0 )
395 DECLARE_ALIGNED_4( int16_t mvp[2] );
397 if( h->mb.i_partition == D_16x16 )
401 if( h->mb.pic.i_fref[0] > 1 )
402 bs_write_te( s, h->mb.pic.i_fref[0] - 1, h->mb.cache.ref[0][x264_scan8[0]] );
403 x264_mb_predict_mv( h, 0, 0, 4, mvp );
404 bs_write_se( s, h->mb.cache.mv[0][x264_scan8[0]][0] - mvp[0] );
405 bs_write_se( s, h->mb.cache.mv[0][x264_scan8[0]][1] - mvp[1] );
407 else if( h->mb.i_partition == D_16x8 )
410 if( h->mb.pic.i_fref[0] > 1 )
412 bs_write_te( s, h->mb.pic.i_fref[0] - 1, h->mb.cache.ref[0][x264_scan8[0]] );
413 bs_write_te( s, h->mb.pic.i_fref[0] - 1, h->mb.cache.ref[0][x264_scan8[8]] );
416 x264_mb_predict_mv( h, 0, 0, 4, mvp );
417 bs_write_se( s, h->mb.cache.mv[0][x264_scan8[0]][0] - mvp[0] );
418 bs_write_se( s, h->mb.cache.mv[0][x264_scan8[0]][1] - mvp[1] );
420 x264_mb_predict_mv( h, 0, 8, 4, mvp );
421 bs_write_se( s, h->mb.cache.mv[0][x264_scan8[8]][0] - mvp[0] );
422 bs_write_se( s, h->mb.cache.mv[0][x264_scan8[8]][1] - mvp[1] );
424 else if( h->mb.i_partition == D_8x16 )
427 if( h->mb.pic.i_fref[0] > 1 )
429 bs_write_te( s, h->mb.pic.i_fref[0] - 1, h->mb.cache.ref[0][x264_scan8[0]] );
430 bs_write_te( s, h->mb.pic.i_fref[0] - 1, h->mb.cache.ref[0][x264_scan8[4]] );
433 x264_mb_predict_mv( h, 0, 0, 2, mvp );
434 bs_write_se( s, h->mb.cache.mv[0][x264_scan8[0]][0] - mvp[0] );
435 bs_write_se( s, h->mb.cache.mv[0][x264_scan8[0]][1] - mvp[1] );
437 x264_mb_predict_mv( h, 0, 4, 2, mvp );
438 bs_write_se( s, h->mb.cache.mv[0][x264_scan8[4]][0] - mvp[0] );
439 bs_write_se( s, h->mb.cache.mv[0][x264_scan8[4]][1] - mvp[1] );
442 else if( i_mb_type == P_8x8 )
445 if( (h->mb.cache.ref[0][x264_scan8[0]] | h->mb.cache.ref[0][x264_scan8[ 4]] |
446 h->mb.cache.ref[0][x264_scan8[8]] | h->mb.cache.ref[0][x264_scan8[12]]) == 0 )
458 if( h->param.analyse.inter & X264_ANALYSE_PSUB8x8 )
459 for( i = 0; i < 4; i++ )
460 bs_write_ue( s, sub_mb_type_p_to_golomb[ h->mb.i_sub_partition[i] ] );
462 bs_write( s, 4, 0xf );
465 if( h->mb.pic.i_fref[0] > 1 && b_sub_ref0 )
467 bs_write_te( s, h->mb.pic.i_fref[0] - 1, h->mb.cache.ref[0][x264_scan8[0]] );
468 bs_write_te( s, h->mb.pic.i_fref[0] - 1, h->mb.cache.ref[0][x264_scan8[4]] );
469 bs_write_te( s, h->mb.pic.i_fref[0] - 1, h->mb.cache.ref[0][x264_scan8[8]] );
470 bs_write_te( s, h->mb.pic.i_fref[0] - 1, h->mb.cache.ref[0][x264_scan8[12]] );
473 for( i = 0; i < 4; i++ )
474 cavlc_mb8x8_mvd( h, s, 0, i );
476 else if( i_mb_type == B_8x8 )
478 bs_write_ue( s, 22 );
481 for( i = 0; i < 4; i++ )
482 bs_write_ue( s, sub_mb_type_b_to_golomb[ h->mb.i_sub_partition[i] ] );
485 for( i = 0; i < 4; i++ )
486 if( x264_mb_partition_listX_table[0][ h->mb.i_sub_partition[i] ] )
487 bs_write_te( s, h->mb.pic.i_fref[0] - 1, h->mb.cache.ref[0][x264_scan8[i*4]] );
488 for( i = 0; i < 4; i++ )
489 if( x264_mb_partition_listX_table[1][ h->mb.i_sub_partition[i] ] )
490 bs_write_te( s, h->mb.pic.i_fref[1] - 1, h->mb.cache.ref[1][x264_scan8[i*4]] );
493 for( i = 0; i < 4; i++ )
494 cavlc_mb8x8_mvd( h, s, 0, i );
495 for( i = 0; i < 4; i++ )
496 cavlc_mb8x8_mvd( h, s, 1, i );
498 else if( i_mb_type != B_DIRECT )
503 DECLARE_ALIGNED_4( int16_t mvp[2] );
504 const uint8_t (*b_list)[2] = x264_mb_type_list_table[i_mb_type];
506 bs_write_ue( s, mb_type_b_to_golomb[ h->mb.i_partition - D_16x8 ][ i_mb_type - B_L0_L0 ] );
508 for( i_list = 0; i_list < 2; i_list++ )
510 const int i_ref_max = (i_list == 0 ? h->mb.pic.i_fref[0] : h->mb.pic.i_fref[1]) - 1;
513 switch( h->mb.i_partition )
516 if( b_list[i_list][0] ) bs_write_te( s, i_ref_max, h->mb.cache.ref[i_list][x264_scan8[0]] );
519 if( b_list[i_list][0] ) bs_write_te( s, i_ref_max, h->mb.cache.ref[i_list][x264_scan8[0]] );
520 if( b_list[i_list][1] ) bs_write_te( s, i_ref_max, h->mb.cache.ref[i_list][x264_scan8[8]] );
523 if( b_list[i_list][0] ) bs_write_te( s, i_ref_max, h->mb.cache.ref[i_list][x264_scan8[0]] );
524 if( b_list[i_list][1] ) bs_write_te( s, i_ref_max, h->mb.cache.ref[i_list][x264_scan8[4]] );
528 for( i_list = 0; i_list < 2; i_list++ )
530 switch( h->mb.i_partition )
533 if( b_list[i_list][0] )
535 x264_mb_predict_mv( h, i_list, 0, 4, mvp );
536 bs_write_se( s, h->mb.cache.mv[i_list][x264_scan8[0]][0] - mvp[0] );
537 bs_write_se( s, h->mb.cache.mv[i_list][x264_scan8[0]][1] - mvp[1] );
541 if( b_list[i_list][0] )
543 x264_mb_predict_mv( h, i_list, 0, 4, mvp );
544 bs_write_se( s, h->mb.cache.mv[i_list][x264_scan8[0]][0] - mvp[0] );
545 bs_write_se( s, h->mb.cache.mv[i_list][x264_scan8[0]][1] - mvp[1] );
547 if( b_list[i_list][1] )
549 x264_mb_predict_mv( h, i_list, 8, 4, mvp );
550 bs_write_se( s, h->mb.cache.mv[i_list][x264_scan8[8]][0] - mvp[0] );
551 bs_write_se( s, h->mb.cache.mv[i_list][x264_scan8[8]][1] - mvp[1] );
555 if( b_list[i_list][0] )
557 x264_mb_predict_mv( h, i_list, 0, 2, mvp );
558 bs_write_se( s, h->mb.cache.mv[i_list][x264_scan8[0]][0] - mvp[0] );
559 bs_write_se( s, h->mb.cache.mv[i_list][x264_scan8[0]][1] - mvp[1] );
561 if( b_list[i_list][1] )
563 x264_mb_predict_mv( h, i_list, 4, 2, mvp );
564 bs_write_se( s, h->mb.cache.mv[i_list][x264_scan8[4]][0] - mvp[0] );
565 bs_write_se( s, h->mb.cache.mv[i_list][x264_scan8[4]][1] - mvp[1] );
571 else if( i_mb_type == B_DIRECT )
575 x264_log(h, X264_LOG_ERROR, "invalid/unhandled mb_type\n" );
580 i_mb_pos_tex = bs_pos( s );
581 h->stat.frame.i_mv_bits += i_mb_pos_tex - i_mb_pos_start;
584 /* Coded block patern */
585 if( i_mb_type == I_4x4 || i_mb_type == I_8x8 )
586 bs_write_ue( s, intra4x4_cbp_to_golomb[( h->mb.i_cbp_chroma << 4 )|h->mb.i_cbp_luma] );
587 else if( i_mb_type != I_16x16 )
588 bs_write_ue( s, inter_cbp_to_golomb[( h->mb.i_cbp_chroma << 4 )|h->mb.i_cbp_luma] );
590 /* transform size 8x8 flag */
591 if( x264_mb_transform_8x8_allowed( h ) && h->mb.i_cbp_luma )
592 bs_write1( s, h->mb.b_transform_8x8 );
595 if( i_mb_type == I_16x16 )
597 cavlc_qp_delta( h, s );
600 block_residual_write_cavlc( h, s, DCT_LUMA_DC, 24 , h->dct.luma16x16_dc, 16 );
603 if( h->mb.i_cbp_luma )
604 for( i = 0; i < 16; i++ )
605 block_residual_write_cavlc( h, s, DCT_LUMA_AC, i, h->dct.luma4x4[i]+1, 15 );
607 else if( h->mb.i_cbp_luma | h->mb.i_cbp_chroma )
609 cavlc_qp_delta( h, s );
610 x264_macroblock_luma_write_cavlc( h, s, 0, 3 );
612 if( h->mb.i_cbp_chroma )
614 /* Chroma DC residual present */
615 block_residual_write_cavlc( h, s, DCT_CHROMA_DC, 25, h->dct.chroma_dc[0], 4 );
616 block_residual_write_cavlc( h, s, DCT_CHROMA_DC, 26, h->dct.chroma_dc[1], 4 );
617 if( h->mb.i_cbp_chroma&0x02 ) /* Chroma AC residual present */
618 for( i = 16; i < 24; i++ )
619 block_residual_write_cavlc( h, s, DCT_CHROMA_AC, i, h->dct.luma4x4[i]+1, 15 );
623 h->stat.frame.i_tex_bits += bs_pos(s) - i_mb_pos_tex;
628 /*****************************************************************************
629 * RD only; doesn't generate a valid bitstream
630 * doesn't write cbp or chroma dc (I don't know how much this matters)
631 * doesn't write ref or subpartition (never varies between calls, so no point in doing so)
632 * works on all partition sizes except 16x16
633 * for sub8x8, call once per 8x8 block
634 *****************************************************************************/
635 static int x264_partition_size_cavlc( x264_t *h, int i8, int i_pixel )
638 const int i_mb_type = h->mb.i_type;
639 int b_8x16 = h->mb.i_partition == D_8x16;
642 s.i_bits_encoded = 0;
644 if( i_mb_type == P_8x8 )
645 cavlc_mb8x8_mvd( h, &s, 0, i8 );
646 else if( i_mb_type == P_L0 )
647 cavlc_mb_mvd( h, &s, 0, 4*i8, 4>>b_8x16 );
648 else if( i_mb_type > B_DIRECT && i_mb_type < B_8x8 )
650 if( x264_mb_type_list_table[ i_mb_type ][0][!!i8] ) cavlc_mb_mvd( h, &s, 0, 4*i8, 4>>b_8x16 );
651 if( x264_mb_type_list_table[ i_mb_type ][1][!!i8] ) cavlc_mb_mvd( h, &s, 1, 4*i8, 4>>b_8x16 );
653 else if( i_mb_type == B_8x8 )
655 cavlc_mb8x8_mvd( h, &s, 0, i8 );
656 cavlc_mb8x8_mvd( h, &s, 1, i8 );
660 x264_log(h, X264_LOG_ERROR, "invalid/unhandled mb_type\n" );
664 for( j = (i_pixel < PIXEL_8x8); j >= 0; j-- )
666 x264_macroblock_luma_write_cavlc( h, &s, i8, i8 );
667 block_residual_write_cavlc( h, &s, DCT_CHROMA_AC, 16+i8, h->dct.luma4x4[16+i8]+1, 15 );
668 block_residual_write_cavlc( h, &s, DCT_CHROMA_AC, 20+i8, h->dct.luma4x4[20+i8]+1, 15 );
669 i8 += x264_pixel_size[i_pixel].h >> 3;
672 return s.i_bits_encoded;
675 static int x264_subpartition_size_cavlc( x264_t *h, int i4, int i_pixel )
678 int b_8x4 = i_pixel == PIXEL_8x4;
679 s.i_bits_encoded = 0;
680 cavlc_mb_mvd( h, &s, 0, i4, 1+b_8x4 );
681 block_residual_write_cavlc( h, &s, DCT_LUMA_4x4, i4, h->dct.luma4x4[i4], 16 );
682 if( i_pixel != PIXEL_4x4 )
685 block_residual_write_cavlc( h, &s, DCT_LUMA_4x4, i4, h->dct.luma4x4[i4], 16 );
688 return s.i_bits_encoded;
691 static int cavlc_intra4x4_pred_size( x264_t *h, int i4, int i_mode )
693 if( x264_mb_predict_intra4x4_mode( h, i4 ) == x264_mb_pred_mode4x4_fix( i_mode ) )
699 static int x264_partition_i8x8_size_cavlc( x264_t *h, int i8, int i_mode )
702 h->out.bs.i_bits_encoded = cavlc_intra4x4_pred_size( h, 4*i8, i_mode );
703 h->zigzagf.interleave_8x8_cavlc( h->dct.luma4x4[i8*4], h->dct.luma8x8[i8] );
704 for( i4 = 0; i4 < 4; i4++ )
706 h->mb.cache.non_zero_count[x264_scan8[i4+i8*4]] = array_non_zero( h->dct.luma4x4[i4+i8*4] );
707 block_residual_write_cavlc( h, &h->out.bs, DCT_LUMA_4x4, i4+i8*4, h->dct.luma4x4[i4+i8*4], 16 );
709 return h->out.bs.i_bits_encoded;
712 static int x264_partition_i4x4_size_cavlc( x264_t *h, int i4, int i_mode )
714 h->out.bs.i_bits_encoded = cavlc_intra4x4_pred_size( h, i4, i_mode );
715 h->mb.cache.non_zero_count[x264_scan8[i4]] = array_non_zero( h->dct.luma4x4[i4] );
716 block_residual_write_cavlc( h, &h->out.bs, DCT_LUMA_4x4, i4, h->dct.luma4x4[i4], 16 );
717 return h->out.bs.i_bits_encoded;
720 static int x264_i8x8_chroma_size_cavlc( x264_t *h )
722 h->out.bs.i_bits_encoded = bs_size_ue( x264_mb_pred_mode8x8c_fix[ h->mb.i_chroma_pred_mode ] );
723 if( h->mb.i_cbp_chroma )
725 block_residual_write_cavlc( h, &h->out.bs, DCT_CHROMA_DC, 25, h->dct.chroma_dc[0], 4 );
726 block_residual_write_cavlc( h, &h->out.bs, DCT_CHROMA_DC, 26, h->dct.chroma_dc[1], 4 );
728 if( h->mb.i_cbp_chroma == 2 )
731 for( i = 16; i < 24; i++ )
732 block_residual_write_cavlc( h, &h->out.bs, DCT_CHROMA_AC, i, h->dct.luma4x4[i]+1, 15 );
735 return h->out.bs.i_bits_encoded;