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\n", 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, int nC )
117 static const uint8_t ctz_index[8] = {3,0,1,0,2,0,1,0};
118 x264_run_level_t runlevel;
119 int i_trailing, i_total_zero, i_suffix_length, i;
123 if( !h->mb.cache.non_zero_count[x264_scan8[i_idx]] )
125 bs_write_vlc( s, x264_coeff0_token[nC] );
129 /* level and run and total */
130 /* set these to 2 to allow branchless i_trailing calculation */
131 runlevel.level[1] = 2;
132 runlevel.level[2] = 2;
133 i_total = h->quantf.coeff_level_run[i_ctxBlockCat]( l, &runlevel );
134 i_total_zero = runlevel.last + 1 - i_total;
136 h->mb.cache.non_zero_count[x264_scan8[i_idx]] = i_total;
138 i_trailing = ((((runlevel.level[0]+1) | (1-runlevel.level[0])) >> 31) & 1) // abs(runlevel.level[0])>1
139 | ((((runlevel.level[1]+1) | (1-runlevel.level[1])) >> 31) & 2)
140 | ((((runlevel.level[2]+1) | (1-runlevel.level[2])) >> 31) & 4);
141 i_trailing = ctz_index[i_trailing];
142 i_sign = ((runlevel.level[2] >> 31) & 1)
143 | ((runlevel.level[1] >> 31) & 2)
144 | ((runlevel.level[0] >> 31) & 4);
145 i_sign >>= 3-i_trailing;
148 bs_write_vlc( s, x264_coeff_token[nC][i_total*4+i_trailing-4] );
150 i_suffix_length = i_total > 10 && i_trailing < 3;
151 if( i_trailing > 0 || RDO_SKIP_BS )
152 bs_write( s, i_trailing, i_sign );
154 if( i_trailing < i_total )
156 int16_t val = runlevel.level[i_trailing];
157 int16_t val_original = runlevel.level[i_trailing]+LEVEL_TABLE_SIZE/2;
159 val -= (val>>15)|1; /* as runlevel.level[i] can't be 1 for the first one if i_trailing < 3 */
160 val += LEVEL_TABLE_SIZE/2;
162 if( (unsigned)val_original < LEVEL_TABLE_SIZE )
164 bs_write_vlc( s, x264_level_token[i_suffix_length][val] );
165 i_suffix_length = x264_level_token[i_suffix_length][val_original].i_next;
168 i_suffix_length = block_residual_write_cavlc_escape( h, s, i_suffix_length, val-LEVEL_TABLE_SIZE/2 );
169 for( i = i_trailing+1; i < i_total; i++ )
171 val = runlevel.level[i] + LEVEL_TABLE_SIZE/2;
172 if( (unsigned)val < LEVEL_TABLE_SIZE )
174 bs_write_vlc( s, x264_level_token[i_suffix_length][val] );
175 i_suffix_length = x264_level_token[i_suffix_length][val].i_next;
178 i_suffix_length = block_residual_write_cavlc_escape( h, s, i_suffix_length, val-LEVEL_TABLE_SIZE/2 );
182 if( i_total < i_count )
185 bs_write_vlc( s, x264_total_zeros_dc[i_total-1][i_total_zero] );
187 bs_write_vlc( s, x264_total_zeros[i_total-1][i_total_zero] );
190 for( i = 0; i < i_total-1 && i_total_zero > 0; i++ )
192 int i_zl = X264_MIN( i_total_zero - 1, 6 );
193 bs_write_vlc( s, x264_run_before[i_zl][runlevel.run[i]] );
194 i_total_zero -= runlevel.run[i];
198 static const uint8_t ct_index[17] = {0,0,1,1,2,2,2,2,3,3,3,3,3,3,3,3,3};
200 #define block_residual_write_cavlc(h,s,cat,idx,l,count)\
202 int nC = cat == DCT_CHROMA_DC ? 4 : ct_index[x264_mb_predict_non_zero_code( h, cat == DCT_LUMA_DC ? 0 : idx )];\
203 block_residual_write_cavlc(h,s,cat,idx,l,count,nC);\
206 static void cavlc_qp_delta( x264_t *h, bs_t *s )
208 int i_dqp = h->mb.i_qp - h->mb.i_last_qp;
210 /* Avoid writing a delta quant if we have an empty i16x16 block, e.g. in a completely flat background area */
211 if( h->mb.i_type == I_16x16 && !(h->mb.i_cbp_luma | h->mb.i_cbp_chroma)
212 && !h->mb.cache.non_zero_count[x264_scan8[24]] )
215 h->mb.i_qp = h->mb.i_last_qp;
224 else if( i_dqp > 25 )
227 bs_write_se( s, i_dqp );
230 static void cavlc_mb_mvd( x264_t *h, bs_t *s, int i_list, int idx, int width )
232 DECLARE_ALIGNED_4( int16_t mvp[2] );
233 x264_mb_predict_mv( h, i_list, idx, width, mvp );
234 bs_write_se( s, h->mb.cache.mv[i_list][x264_scan8[idx]][0] - mvp[0] );
235 bs_write_se( s, h->mb.cache.mv[i_list][x264_scan8[idx]][1] - mvp[1] );
238 static void cavlc_mb8x8_mvd( x264_t *h, bs_t *s, int i_list, int i )
240 if( !x264_mb_partition_listX_table[i_list][ h->mb.i_sub_partition[i] ] )
243 switch( h->mb.i_sub_partition[i] )
248 cavlc_mb_mvd( h, s, i_list, 4*i, 2 );
253 cavlc_mb_mvd( h, s, i_list, 4*i+0, 2 );
254 cavlc_mb_mvd( h, s, i_list, 4*i+2, 2 );
259 cavlc_mb_mvd( h, s, i_list, 4*i+0, 1 );
260 cavlc_mb_mvd( h, s, i_list, 4*i+1, 1 );
265 cavlc_mb_mvd( h, s, i_list, 4*i+0, 1 );
266 cavlc_mb_mvd( h, s, i_list, 4*i+1, 1 );
267 cavlc_mb_mvd( h, s, i_list, 4*i+2, 1 );
268 cavlc_mb_mvd( h, s, i_list, 4*i+3, 1 );
273 static inline void x264_macroblock_luma_write_cavlc( x264_t *h, bs_t *s, int i8start, int i8end )
276 if( h->mb.b_transform_8x8 )
278 /* shuffle 8x8 dct coeffs into 4x4 lists */
279 for( i8 = i8start; i8 <= i8end; i8++ )
280 if( h->mb.i_cbp_luma & (1 << i8) )
281 h->zigzagf.interleave_8x8_cavlc( h->dct.luma4x4[i8*4], h->dct.luma8x8[i8], &h->mb.cache.non_zero_count[x264_scan8[i8*4]] );
284 for( i8 = i8start; i8 <= i8end; i8++ )
285 if( h->mb.i_cbp_luma & (1 << i8) )
286 for( i4 = 0; i4 < 4; i4++ )
287 block_residual_write_cavlc( h, s, DCT_LUMA_4x4, i4+i8*4, h->dct.luma4x4[i4+i8*4], 16 );
290 /*****************************************************************************
291 * x264_macroblock_write:
292 *****************************************************************************/
293 void x264_macroblock_write_cavlc( x264_t *h, bs_t *s )
295 const int i_mb_type = h->mb.i_type;
300 const int i_mb_pos_start = bs_pos( s );
304 switch( h->sh.i_type )
316 x264_log(h, X264_LOG_ERROR, "internal error or slice unsupported\n" );
321 && (!(h->mb.i_mb_y & 1) || IS_SKIP(h->mb.type[h->mb.i_mb_xy - h->mb.i_mb_stride])) )
323 bs_write1( s, h->mb.b_interlaced );
327 if( i_mb_type == I_PCM )
329 bs_write_ue( s, i_mb_i_offset + 25 );
330 i_mb_pos_tex = bs_pos( s );
331 h->stat.frame.i_mv_bits += i_mb_pos_tex - i_mb_pos_start;
335 memcpy( s->p, h->mb.pic.p_fenc[0], 256 );
337 for( i = 0; i < 8; i++ )
338 memcpy( s->p + i*8, h->mb.pic.p_fenc[1] + i*FENC_STRIDE, 8 );
340 for( i = 0; i < 8; i++ )
341 memcpy( s->p + i*8, h->mb.pic.p_fenc[2] + i*FENC_STRIDE, 8 );
344 /* if PCM is chosen, we need to store reconstructed frame data */
345 h->mc.copy[PIXEL_16x16]( h->mb.pic.p_fdec[0], FDEC_STRIDE, h->mb.pic.p_fenc[0], FENC_STRIDE, 16 );
346 h->mc.copy[PIXEL_8x8] ( h->mb.pic.p_fdec[1], FDEC_STRIDE, h->mb.pic.p_fenc[1], FENC_STRIDE, 8 );
347 h->mc.copy[PIXEL_8x8] ( h->mb.pic.p_fdec[2], FDEC_STRIDE, h->mb.pic.p_fenc[2], FENC_STRIDE, 8 );
349 h->stat.frame.i_tex_bits += bs_pos(s) - i_mb_pos_tex;
358 if( i_mb_type == I_4x4 || i_mb_type == I_8x8 )
360 int di = i_mb_type == I_8x8 ? 4 : 1;
361 bs_write_ue( s, i_mb_i_offset + 0 );
362 if( h->pps->b_transform_8x8_mode )
363 bs_write1( s, h->mb.b_transform_8x8 );
365 /* Prediction: Luma */
366 for( i = 0; i < 16; i += di )
368 int i_pred = x264_mb_predict_intra4x4_mode( h, i );
369 int i_mode = x264_mb_pred_mode4x4_fix( h->mb.cache.intra4x4_pred_mode[x264_scan8[i]] );
371 if( i_pred == i_mode )
372 bs_write1( s, 1 ); /* b_prev_intra4x4_pred_mode */
374 bs_write( s, 4, i_mode - (i_mode > i_pred) );
376 bs_write_ue( s, x264_mb_pred_mode8x8c_fix[ h->mb.i_chroma_pred_mode ] );
378 else if( i_mb_type == I_16x16 )
380 bs_write_ue( s, i_mb_i_offset + 1 + x264_mb_pred_mode16x16_fix[h->mb.i_intra16x16_pred_mode] +
381 h->mb.i_cbp_chroma * 4 + ( h->mb.i_cbp_luma == 0 ? 0 : 12 ) );
382 bs_write_ue( s, x264_mb_pred_mode8x8c_fix[ h->mb.i_chroma_pred_mode ] );
384 else if( i_mb_type == P_L0 )
386 DECLARE_ALIGNED_4( int16_t mvp[2] );
388 if( h->mb.i_partition == D_16x16 )
392 if( h->mb.pic.i_fref[0] > 1 )
393 bs_write_te( s, h->mb.pic.i_fref[0] - 1, h->mb.cache.ref[0][x264_scan8[0]] );
394 x264_mb_predict_mv( h, 0, 0, 4, mvp );
395 bs_write_se( s, h->mb.cache.mv[0][x264_scan8[0]][0] - mvp[0] );
396 bs_write_se( s, h->mb.cache.mv[0][x264_scan8[0]][1] - mvp[1] );
398 else if( h->mb.i_partition == D_16x8 )
401 if( h->mb.pic.i_fref[0] > 1 )
403 bs_write_te( s, h->mb.pic.i_fref[0] - 1, h->mb.cache.ref[0][x264_scan8[0]] );
404 bs_write_te( s, h->mb.pic.i_fref[0] - 1, h->mb.cache.ref[0][x264_scan8[8]] );
407 x264_mb_predict_mv( h, 0, 0, 4, mvp );
408 bs_write_se( s, h->mb.cache.mv[0][x264_scan8[0]][0] - mvp[0] );
409 bs_write_se( s, h->mb.cache.mv[0][x264_scan8[0]][1] - mvp[1] );
411 x264_mb_predict_mv( h, 0, 8, 4, mvp );
412 bs_write_se( s, h->mb.cache.mv[0][x264_scan8[8]][0] - mvp[0] );
413 bs_write_se( s, h->mb.cache.mv[0][x264_scan8[8]][1] - mvp[1] );
415 else if( h->mb.i_partition == D_8x16 )
418 if( h->mb.pic.i_fref[0] > 1 )
420 bs_write_te( s, h->mb.pic.i_fref[0] - 1, h->mb.cache.ref[0][x264_scan8[0]] );
421 bs_write_te( s, h->mb.pic.i_fref[0] - 1, h->mb.cache.ref[0][x264_scan8[4]] );
424 x264_mb_predict_mv( h, 0, 0, 2, mvp );
425 bs_write_se( s, h->mb.cache.mv[0][x264_scan8[0]][0] - mvp[0] );
426 bs_write_se( s, h->mb.cache.mv[0][x264_scan8[0]][1] - mvp[1] );
428 x264_mb_predict_mv( h, 0, 4, 2, mvp );
429 bs_write_se( s, h->mb.cache.mv[0][x264_scan8[4]][0] - mvp[0] );
430 bs_write_se( s, h->mb.cache.mv[0][x264_scan8[4]][1] - mvp[1] );
433 else if( i_mb_type == P_8x8 )
436 if( (h->mb.cache.ref[0][x264_scan8[0]] | h->mb.cache.ref[0][x264_scan8[ 4]] |
437 h->mb.cache.ref[0][x264_scan8[8]] | h->mb.cache.ref[0][x264_scan8[12]]) == 0 )
449 if( h->param.analyse.inter & X264_ANALYSE_PSUB8x8 )
450 for( i = 0; i < 4; i++ )
451 bs_write_ue( s, sub_mb_type_p_to_golomb[ h->mb.i_sub_partition[i] ] );
453 bs_write( s, 4, 0xf );
458 bs_write_te( s, h->mb.pic.i_fref[0] - 1, h->mb.cache.ref[0][x264_scan8[0]] );
459 bs_write_te( s, h->mb.pic.i_fref[0] - 1, h->mb.cache.ref[0][x264_scan8[4]] );
460 bs_write_te( s, h->mb.pic.i_fref[0] - 1, h->mb.cache.ref[0][x264_scan8[8]] );
461 bs_write_te( s, h->mb.pic.i_fref[0] - 1, h->mb.cache.ref[0][x264_scan8[12]] );
464 for( i = 0; i < 4; i++ )
465 cavlc_mb8x8_mvd( h, s, 0, i );
467 else if( i_mb_type == B_8x8 )
469 bs_write_ue( s, 22 );
472 for( i = 0; i < 4; i++ )
473 bs_write_ue( s, sub_mb_type_b_to_golomb[ h->mb.i_sub_partition[i] ] );
476 if( h->mb.pic.i_fref[0] > 1 )
477 for( i = 0; i < 4; i++ )
478 if( x264_mb_partition_listX_table[0][ h->mb.i_sub_partition[i] ] )
479 bs_write_te( s, h->mb.pic.i_fref[0] - 1, h->mb.cache.ref[0][x264_scan8[i*4]] );
480 if( h->mb.pic.i_fref[1] > 1 )
481 for( i = 0; i < 4; i++ )
482 if( x264_mb_partition_listX_table[1][ h->mb.i_sub_partition[i] ] )
483 bs_write_te( s, h->mb.pic.i_fref[1] - 1, h->mb.cache.ref[1][x264_scan8[i*4]] );
486 for( i = 0; i < 4; i++ )
487 cavlc_mb8x8_mvd( h, s, 0, i );
488 for( i = 0; i < 4; i++ )
489 cavlc_mb8x8_mvd( h, s, 1, i );
491 else if( i_mb_type != B_DIRECT )
496 DECLARE_ALIGNED_4( int16_t mvp[2] );
497 const uint8_t (*b_list)[2] = x264_mb_type_list_table[i_mb_type];
499 bs_write_ue( s, mb_type_b_to_golomb[ h->mb.i_partition - D_16x8 ][ i_mb_type - B_L0_L0 ] );
501 for( i_list = 0; i_list < 2; i_list++ )
503 const int i_ref_max = (i_list == 0 ? h->mb.pic.i_fref[0] : h->mb.pic.i_fref[1]) - 1;
506 switch( h->mb.i_partition )
509 if( b_list[i_list][0] ) bs_write_te( s, i_ref_max, h->mb.cache.ref[i_list][x264_scan8[0]] );
512 if( b_list[i_list][0] ) bs_write_te( s, i_ref_max, h->mb.cache.ref[i_list][x264_scan8[0]] );
513 if( b_list[i_list][1] ) bs_write_te( s, i_ref_max, h->mb.cache.ref[i_list][x264_scan8[8]] );
516 if( b_list[i_list][0] ) bs_write_te( s, i_ref_max, h->mb.cache.ref[i_list][x264_scan8[0]] );
517 if( b_list[i_list][1] ) bs_write_te( s, i_ref_max, h->mb.cache.ref[i_list][x264_scan8[4]] );
521 for( i_list = 0; i_list < 2; i_list++ )
523 switch( h->mb.i_partition )
526 if( b_list[i_list][0] )
528 x264_mb_predict_mv( h, i_list, 0, 4, mvp );
529 bs_write_se( s, h->mb.cache.mv[i_list][x264_scan8[0]][0] - mvp[0] );
530 bs_write_se( s, h->mb.cache.mv[i_list][x264_scan8[0]][1] - mvp[1] );
534 if( b_list[i_list][0] )
536 x264_mb_predict_mv( h, i_list, 0, 4, mvp );
537 bs_write_se( s, h->mb.cache.mv[i_list][x264_scan8[0]][0] - mvp[0] );
538 bs_write_se( s, h->mb.cache.mv[i_list][x264_scan8[0]][1] - mvp[1] );
540 if( b_list[i_list][1] )
542 x264_mb_predict_mv( h, i_list, 8, 4, mvp );
543 bs_write_se( s, h->mb.cache.mv[i_list][x264_scan8[8]][0] - mvp[0] );
544 bs_write_se( s, h->mb.cache.mv[i_list][x264_scan8[8]][1] - mvp[1] );
548 if( b_list[i_list][0] )
550 x264_mb_predict_mv( h, i_list, 0, 2, mvp );
551 bs_write_se( s, h->mb.cache.mv[i_list][x264_scan8[0]][0] - mvp[0] );
552 bs_write_se( s, h->mb.cache.mv[i_list][x264_scan8[0]][1] - mvp[1] );
554 if( b_list[i_list][1] )
556 x264_mb_predict_mv( h, i_list, 4, 2, mvp );
557 bs_write_se( s, h->mb.cache.mv[i_list][x264_scan8[4]][0] - mvp[0] );
558 bs_write_se( s, h->mb.cache.mv[i_list][x264_scan8[4]][1] - mvp[1] );
564 else if( i_mb_type == B_DIRECT )
568 x264_log(h, X264_LOG_ERROR, "invalid/unhandled mb_type\n" );
573 i_mb_pos_tex = bs_pos( s );
574 h->stat.frame.i_mv_bits += i_mb_pos_tex - i_mb_pos_start;
577 /* Coded block patern */
578 if( i_mb_type == I_4x4 || i_mb_type == I_8x8 )
579 bs_write_ue( s, intra4x4_cbp_to_golomb[( h->mb.i_cbp_chroma << 4 )|h->mb.i_cbp_luma] );
580 else if( i_mb_type != I_16x16 )
581 bs_write_ue( s, inter_cbp_to_golomb[( h->mb.i_cbp_chroma << 4 )|h->mb.i_cbp_luma] );
583 /* transform size 8x8 flag */
584 if( x264_mb_transform_8x8_allowed( h ) && h->mb.i_cbp_luma )
585 bs_write1( s, h->mb.b_transform_8x8 );
588 if( i_mb_type == I_16x16 )
590 cavlc_qp_delta( h, s );
593 block_residual_write_cavlc( h, s, DCT_LUMA_DC, 24 , h->dct.luma16x16_dc, 16 );
596 if( h->mb.i_cbp_luma )
597 for( i = 0; i < 16; i++ )
598 block_residual_write_cavlc( h, s, DCT_LUMA_AC, i, h->dct.luma4x4[i]+1, 15 );
600 else if( h->mb.i_cbp_luma | h->mb.i_cbp_chroma )
602 cavlc_qp_delta( h, s );
603 x264_macroblock_luma_write_cavlc( h, s, 0, 3 );
605 if( h->mb.i_cbp_chroma )
607 /* Chroma DC residual present */
608 block_residual_write_cavlc( h, s, DCT_CHROMA_DC, 25, h->dct.chroma_dc[0], 4 );
609 block_residual_write_cavlc( h, s, DCT_CHROMA_DC, 26, h->dct.chroma_dc[1], 4 );
610 if( h->mb.i_cbp_chroma&0x02 ) /* Chroma AC residual present */
611 for( i = 16; i < 24; i++ )
612 block_residual_write_cavlc( h, s, DCT_CHROMA_AC, i, h->dct.luma4x4[i]+1, 15 );
616 h->stat.frame.i_tex_bits += bs_pos(s) - i_mb_pos_tex;
621 /*****************************************************************************
622 * RD only; doesn't generate a valid bitstream
623 * doesn't write cbp or chroma dc (I don't know how much this matters)
624 * doesn't write ref or subpartition (never varies between calls, so no point in doing so)
625 * works on all partition sizes except 16x16
626 * for sub8x8, call once per 8x8 block
627 *****************************************************************************/
628 static int x264_partition_size_cavlc( x264_t *h, int i8, int i_pixel )
631 const int i_mb_type = h->mb.i_type;
632 int b_8x16 = h->mb.i_partition == D_8x16;
635 s.i_bits_encoded = 0;
637 if( i_mb_type == P_8x8 )
638 cavlc_mb8x8_mvd( h, &s, 0, i8 );
639 else if( i_mb_type == P_L0 )
640 cavlc_mb_mvd( h, &s, 0, 4*i8, 4>>b_8x16 );
641 else if( i_mb_type > B_DIRECT && i_mb_type < B_8x8 )
643 if( x264_mb_type_list_table[ i_mb_type ][0][!!i8] ) cavlc_mb_mvd( h, &s, 0, 4*i8, 4>>b_8x16 );
644 if( x264_mb_type_list_table[ i_mb_type ][1][!!i8] ) cavlc_mb_mvd( h, &s, 1, 4*i8, 4>>b_8x16 );
646 else if( i_mb_type == B_8x8 )
648 cavlc_mb8x8_mvd( h, &s, 0, i8 );
649 cavlc_mb8x8_mvd( h, &s, 1, i8 );
653 x264_log(h, X264_LOG_ERROR, "invalid/unhandled mb_type\n" );
657 for( j = (i_pixel < PIXEL_8x8); j >= 0; j-- )
659 x264_macroblock_luma_write_cavlc( h, &s, i8, i8 );
660 block_residual_write_cavlc( h, &s, DCT_CHROMA_AC, 16+i8, h->dct.luma4x4[16+i8]+1, 15 );
661 block_residual_write_cavlc( h, &s, DCT_CHROMA_AC, 20+i8, h->dct.luma4x4[20+i8]+1, 15 );
662 i8 += x264_pixel_size[i_pixel].h >> 3;
665 return s.i_bits_encoded;
668 static int x264_subpartition_size_cavlc( x264_t *h, int i4, int i_pixel )
671 int b_8x4 = i_pixel == PIXEL_8x4;
672 s.i_bits_encoded = 0;
673 cavlc_mb_mvd( h, &s, 0, i4, 1+b_8x4 );
674 block_residual_write_cavlc( h, &s, DCT_LUMA_4x4, i4, h->dct.luma4x4[i4], 16 );
675 if( i_pixel != PIXEL_4x4 )
678 block_residual_write_cavlc( h, &s, DCT_LUMA_4x4, i4, h->dct.luma4x4[i4], 16 );
681 return s.i_bits_encoded;
684 static int cavlc_intra4x4_pred_size( x264_t *h, int i4, int i_mode )
686 if( x264_mb_predict_intra4x4_mode( h, i4 ) == x264_mb_pred_mode4x4_fix( i_mode ) )
692 static int x264_partition_i8x8_size_cavlc( x264_t *h, int i8, int i_mode )
694 h->out.bs.i_bits_encoded = cavlc_intra4x4_pred_size( h, 4*i8, i_mode );
695 bs_write_ue( &h->out.bs, intra4x4_cbp_to_golomb[( h->mb.i_cbp_chroma << 4 )|h->mb.i_cbp_luma] );
696 x264_macroblock_luma_write_cavlc( h, &h->out.bs, i8, i8 );
697 return h->out.bs.i_bits_encoded;
700 static int x264_partition_i4x4_size_cavlc( x264_t *h, int i4, int i_mode )
702 h->out.bs.i_bits_encoded = cavlc_intra4x4_pred_size( h, i4, i_mode );
703 block_residual_write_cavlc( h, &h->out.bs, DCT_LUMA_4x4, i4, h->dct.luma4x4[i4], 16 );
704 return h->out.bs.i_bits_encoded;
707 static int x264_i8x8_chroma_size_cavlc( x264_t *h )
709 h->out.bs.i_bits_encoded = bs_size_ue( x264_mb_pred_mode8x8c_fix[ h->mb.i_chroma_pred_mode ] );
710 if( h->mb.i_cbp_chroma )
712 block_residual_write_cavlc( h, &h->out.bs, DCT_CHROMA_DC, 25, h->dct.chroma_dc[0], 4 );
713 block_residual_write_cavlc( h, &h->out.bs, DCT_CHROMA_DC, 26, h->dct.chroma_dc[1], 4 );
715 if( h->mb.i_cbp_chroma == 2 )
718 for( i = 16; i < 24; i++ )
719 block_residual_write_cavlc( h, &h->out.bs, DCT_CHROMA_AC, i, h->dct.luma4x4[i]+1, 15 );
722 return h->out.bs.i_bits_encoded;