1 /*****************************************************************************
2 * cavlc.c: cavlc bitstream writing
3 *****************************************************************************
4 * Copyright (C) 2003-2016 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.
24 * This program is also available under a commercial proprietary license.
25 * For more information, contact us at licensing@x264.com.
26 *****************************************************************************/
28 #include "common/common.h"
29 #include "macroblock.h"
35 /* [400,420][inter,intra] */
36 static const uint8_t cbp_to_golomb[2][2][48] =
38 {{ 0, 1, 2, 5, 3, 6, 14, 10, 4, 15, 7, 11, 8, 12, 13, 9 },
39 { 1, 10, 11, 6, 12, 7, 14, 2, 13, 15, 8, 3, 9, 4, 5, 0 }},
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 },
43 { 3, 29, 30, 17, 31, 18, 37, 8, 32, 38, 19, 9, 20, 10, 11, 2,
44 16, 33, 34, 21, 35, 22, 39, 4, 36, 40, 23, 5, 24, 6, 7, 1,
45 41, 42, 43, 25, 44, 26, 46, 12, 45, 47, 27, 13, 28, 14, 15, 0 }}
48 static const uint8_t mb_type_b_to_golomb[3][9]=
50 { 4, 8, 12, 10, 6, 14, 16, 18, 20 }, /* D_16x8 */
51 { 5, 9, 13, 11, 7, 15, 17, 19, 21 }, /* D_8x16 */
52 { 1, -1, -1, -1, 2, -1, -1, -1, 3 } /* D_16x16 */
55 static const uint8_t subpartition_p_to_golomb[4]=
60 static const uint8_t subpartition_b_to_golomb[13]=
62 10, 4, 5, 1, 11, 6, 7, 2, 12, 8, 9, 3, 0
65 #define bs_write_vlc(s,v) bs_write( s, (v).i_size, (v).i_bits )
67 /****************************************************************************
68 * x264_cavlc_block_residual:
69 ****************************************************************************/
70 static inline int x264_cavlc_block_residual_escape( x264_t *h, int i_suffix_length, int level )
73 static const uint16_t next_suffix[7] = { 0, 3, 6, 12, 24, 48, 0xffff };
74 int i_level_prefix = 15;
75 int mask = level >> 31;
76 int abs_level = (level^mask)-mask;
77 int i_level_code = abs_level*2-mask-2;
78 if( ( i_level_code >> i_suffix_length ) < 15 )
80 bs_write( s, (i_level_code >> i_suffix_length) + 1 + i_suffix_length,
81 (1<<i_suffix_length) + (i_level_code & ((1<<i_suffix_length)-1)) );
85 i_level_code -= 15 << i_suffix_length;
86 if( i_suffix_length == 0 )
89 /* If the prefix size exceeds 15, High Profile is required. */
90 if( i_level_code >= 1<<12 )
92 if( h->sps->i_profile_idc >= PROFILE_HIGH )
94 while( i_level_code > 1<<(i_level_prefix-3) )
96 i_level_code -= 1<<(i_level_prefix-3);
103 /* Weight highly against overflows. */
104 s->i_bits_encoded += 2000;
106 /* We've had an overflow; note it down and re-encode the MB later. */
107 h->mb.b_overflow = 1;
111 bs_write( s, i_level_prefix + 1, 1 );
112 bs_write( s, i_level_prefix - 3, i_level_code & ((1<<(i_level_prefix-3))-1) );
114 if( i_suffix_length == 0 )
116 if( abs_level > next_suffix[i_suffix_length] )
118 return i_suffix_length;
121 static int x264_cavlc_block_residual_internal( x264_t *h, int ctx_block_cat, dctcoef *l, int nC )
123 bs_t *s = &h->out.bs;
124 static const uint8_t ctz_index[8] = {3,0,1,0,2,0,1,0};
125 static const uint8_t count_cat[14] = {16, 15, 16, 0, 15, 64, 16, 15, 16, 64, 16, 15, 16, 64};
126 x264_run_level_t runlevel;
127 int i_total, i_trailing, i_total_zero, i_suffix_length;
130 /* level and run and total */
131 i_total = h->quantf.coeff_level_run[ctx_block_cat]( l, &runlevel );
132 x264_prefetch( &x264_run_before[runlevel.mask] );
133 i_total_zero = runlevel.last + 1 - i_total;
135 /* branchless i_trailing calculation */
136 runlevel.level[i_total+0] = 2;
137 runlevel.level[i_total+1] = 2;
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-1][i_trailing] );
150 i_suffix_length = i_total > 10 && i_trailing < 3;
151 bs_write( s, i_trailing, i_sign );
153 if( i_trailing < i_total )
155 int val = runlevel.level[i_trailing];
156 int val_original = runlevel.level[i_trailing]+LEVEL_TABLE_SIZE/2;
157 val -= ((val>>31)|1) & -(i_trailing < 3); /* as runlevel.level[i] can't be 1 for the first one if i_trailing < 3 */
158 val += LEVEL_TABLE_SIZE/2;
160 if( (unsigned)val_original < LEVEL_TABLE_SIZE )
162 bs_write_vlc( s, x264_level_token[i_suffix_length][val] );
163 i_suffix_length = x264_level_token[i_suffix_length][val_original].i_next;
166 i_suffix_length = x264_cavlc_block_residual_escape( h, i_suffix_length, val-LEVEL_TABLE_SIZE/2 );
167 for( int i = i_trailing+1; i < i_total; i++ )
169 val = runlevel.level[i] + LEVEL_TABLE_SIZE/2;
170 if( (unsigned)val < LEVEL_TABLE_SIZE )
172 bs_write_vlc( s, x264_level_token[i_suffix_length][val] );
173 i_suffix_length = x264_level_token[i_suffix_length][val].i_next;
176 i_suffix_length = x264_cavlc_block_residual_escape( h, i_suffix_length, val-LEVEL_TABLE_SIZE/2 );
180 if( ctx_block_cat == DCT_CHROMA_DC )
182 if( i_total < 8>>CHROMA_V_SHIFT )
184 vlc_t total_zeros = CHROMA_FORMAT == CHROMA_420 ? x264_total_zeros_2x2_dc[i_total-1][i_total_zero]
185 : x264_total_zeros_2x4_dc[i_total-1][i_total_zero];
186 bs_write_vlc( s, total_zeros );
189 else if( (uint8_t)i_total < count_cat[ctx_block_cat] )
190 bs_write_vlc( s, x264_total_zeros[i_total-1][i_total_zero] );
192 int zero_run_code = x264_run_before[runlevel.mask];
193 bs_write( s, zero_run_code&0x1f, zero_run_code>>5 );
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 x264_cavlc_block_residual(h,cat,idx,l)\
202 int nC = cat == DCT_CHROMA_DC ? 5 - CHROMA_V_SHIFT\
203 : ct_index[x264_mb_predict_non_zero_code( h, cat == DCT_LUMA_DC ? (idx - LUMA_DC)*16 : idx )];\
204 uint8_t *nnz = &h->mb.cache.non_zero_count[x264_scan8[idx]];\
206 bs_write_vlc( &h->out.bs, x264_coeff0_token[nC] );\
208 *nnz = x264_cavlc_block_residual_internal(h,cat,l,nC);\
211 static void x264_cavlc_qp_delta( x264_t *h )
213 bs_t *s = &h->out.bs;
214 int i_dqp = h->mb.i_qp - h->mb.i_last_qp;
216 /* Avoid writing a delta quant if we have an empty i16x16 block, e.g. in a completely
217 * flat background area. Don't do this if it would raise the quantizer, since that could
218 * cause unexpected deblocking artifacts. */
219 if( h->mb.i_type == I_16x16 && !(h->mb.i_cbp_luma | h->mb.i_cbp_chroma)
220 && !h->mb.cache.non_zero_count[x264_scan8[LUMA_DC]]
221 && !h->mb.cache.non_zero_count[x264_scan8[CHROMA_DC+0]]
222 && !h->mb.cache.non_zero_count[x264_scan8[CHROMA_DC+1]]
223 && h->mb.i_qp > h->mb.i_last_qp )
226 h->mb.i_qp = h->mb.i_last_qp;
233 if( i_dqp < -(QP_MAX_SPEC+1)/2 )
234 i_dqp += QP_MAX_SPEC+1;
235 else if( i_dqp > QP_MAX_SPEC/2 )
236 i_dqp -= QP_MAX_SPEC+1;
238 bs_write_se( s, i_dqp );
241 static void x264_cavlc_mvd( x264_t *h, int i_list, int idx, int width )
243 bs_t *s = &h->out.bs;
244 ALIGNED_4( int16_t mvp[2] );
245 x264_mb_predict_mv( h, i_list, idx, width, mvp );
246 bs_write_se( s, h->mb.cache.mv[i_list][x264_scan8[idx]][0] - mvp[0] );
247 bs_write_se( s, h->mb.cache.mv[i_list][x264_scan8[idx]][1] - mvp[1] );
250 static inline void x264_cavlc_8x8_mvd( x264_t *h, int i )
252 switch( h->mb.i_sub_partition[i] )
255 x264_cavlc_mvd( h, 0, 4*i, 2 );
258 x264_cavlc_mvd( h, 0, 4*i+0, 2 );
259 x264_cavlc_mvd( h, 0, 4*i+2, 2 );
262 x264_cavlc_mvd( h, 0, 4*i+0, 1 );
263 x264_cavlc_mvd( h, 0, 4*i+1, 1 );
266 x264_cavlc_mvd( h, 0, 4*i+0, 1 );
267 x264_cavlc_mvd( h, 0, 4*i+1, 1 );
268 x264_cavlc_mvd( h, 0, 4*i+2, 1 );
269 x264_cavlc_mvd( h, 0, 4*i+3, 1 );
274 static ALWAYS_INLINE void x264_cavlc_macroblock_luma_residual( x264_t *h, int plane_count )
276 if( h->mb.b_transform_8x8 )
278 /* shuffle 8x8 dct coeffs into 4x4 lists */
279 for( int p = 0; p < plane_count; p++ )
280 for( int i8 = 0; i8 < 4; i8++ )
281 if( h->mb.cache.non_zero_count[x264_scan8[p*16+i8*4]] )
282 h->zigzagf.interleave_8x8_cavlc( h->dct.luma4x4[p*16+i8*4], h->dct.luma8x8[p*4+i8],
283 &h->mb.cache.non_zero_count[x264_scan8[p*16+i8*4]] );
286 for( int p = 0; p < plane_count; p++ )
287 FOREACH_BIT( i8, 0, h->mb.i_cbp_luma )
288 for( int i4 = 0; i4 < 4; i4++ )
289 x264_cavlc_block_residual( h, DCT_LUMA_4x4, i4+i8*4+p*16, h->dct.luma4x4[i4+i8*4+p*16] );
293 static ALWAYS_INLINE void x264_cavlc_partition_luma_residual( x264_t *h, int i8, int p )
295 if( h->mb.b_transform_8x8 && h->mb.cache.non_zero_count[x264_scan8[i8*4]] )
296 h->zigzagf.interleave_8x8_cavlc( h->dct.luma4x4[i8*4+p*16], h->dct.luma8x8[i8+p*4],
297 &h->mb.cache.non_zero_count[x264_scan8[i8*4+p*16]] );
299 if( h->mb.i_cbp_luma & (1 << i8) )
300 for( int i4 = 0; i4 < 4; i4++ )
301 x264_cavlc_block_residual( h, DCT_LUMA_4x4, i4+i8*4+p*16, h->dct.luma4x4[i4+i8*4+p*16] );
305 static void x264_cavlc_mb_header_i( x264_t *h, int i_mb_type, int i_mb_i_offset, int chroma )
307 bs_t *s = &h->out.bs;
308 if( i_mb_type == I_16x16 )
310 bs_write_ue( s, i_mb_i_offset + 1 + x264_mb_pred_mode16x16_fix[h->mb.i_intra16x16_pred_mode] +
311 h->mb.i_cbp_chroma * 4 + ( h->mb.i_cbp_luma == 0 ? 0 : 12 ) );
313 else //if( i_mb_type == I_4x4 || i_mb_type == I_8x8 )
315 int di = i_mb_type == I_8x8 ? 4 : 1;
316 bs_write_ue( s, i_mb_i_offset + 0 );
317 if( h->pps->b_transform_8x8_mode )
318 bs_write1( s, h->mb.b_transform_8x8 );
320 /* Prediction: Luma */
321 for( int i = 0; i < 16; i += di )
323 int i_pred = x264_mb_predict_intra4x4_mode( h, i );
324 int i_mode = x264_mb_pred_mode4x4_fix( h->mb.cache.intra4x4_pred_mode[x264_scan8[i]] );
326 if( i_pred == i_mode )
327 bs_write1( s, 1 ); /* b_prev_intra4x4_pred_mode */
329 bs_write( s, 4, i_mode - (i_mode > i_pred) );
334 bs_write_ue( s, x264_mb_chroma_pred_mode_fix[h->mb.i_chroma_pred_mode] );
337 static ALWAYS_INLINE void x264_cavlc_mb_header_p( x264_t *h, int i_mb_type, int chroma )
339 bs_t *s = &h->out.bs;
340 if( i_mb_type == P_L0 )
342 if( h->mb.i_partition == D_16x16 )
346 if( h->mb.pic.i_fref[0] > 1 )
347 bs_write_te( s, h->mb.pic.i_fref[0] - 1, h->mb.cache.ref[0][x264_scan8[0]] );
348 x264_cavlc_mvd( h, 0, 0, 4 );
350 else if( h->mb.i_partition == D_16x8 )
353 if( h->mb.pic.i_fref[0] > 1 )
355 bs_write_te( s, h->mb.pic.i_fref[0] - 1, h->mb.cache.ref[0][x264_scan8[0]] );
356 bs_write_te( s, h->mb.pic.i_fref[0] - 1, h->mb.cache.ref[0][x264_scan8[8]] );
358 x264_cavlc_mvd( h, 0, 0, 4 );
359 x264_cavlc_mvd( h, 0, 8, 4 );
361 else if( h->mb.i_partition == D_8x16 )
364 if( h->mb.pic.i_fref[0] > 1 )
366 bs_write_te( s, h->mb.pic.i_fref[0] - 1, h->mb.cache.ref[0][x264_scan8[0]] );
367 bs_write_te( s, h->mb.pic.i_fref[0] - 1, h->mb.cache.ref[0][x264_scan8[4]] );
369 x264_cavlc_mvd( h, 0, 0, 2 );
370 x264_cavlc_mvd( h, 0, 4, 2 );
373 else if( i_mb_type == P_8x8 )
376 if( (h->mb.cache.ref[0][x264_scan8[0]] | h->mb.cache.ref[0][x264_scan8[ 4]] |
377 h->mb.cache.ref[0][x264_scan8[8]] | h->mb.cache.ref[0][x264_scan8[12]]) == 0 )
389 if( h->param.analyse.inter & X264_ANALYSE_PSUB8x8 )
390 for( int i = 0; i < 4; i++ )
391 bs_write_ue( s, subpartition_p_to_golomb[ h->mb.i_sub_partition[i] ] );
393 bs_write( s, 4, 0xf );
398 bs_write_te( s, h->mb.pic.i_fref[0] - 1, h->mb.cache.ref[0][x264_scan8[0]] );
399 bs_write_te( s, h->mb.pic.i_fref[0] - 1, h->mb.cache.ref[0][x264_scan8[4]] );
400 bs_write_te( s, h->mb.pic.i_fref[0] - 1, h->mb.cache.ref[0][x264_scan8[8]] );
401 bs_write_te( s, h->mb.pic.i_fref[0] - 1, h->mb.cache.ref[0][x264_scan8[12]] );
404 for( int i = 0; i < 4; i++ )
405 x264_cavlc_8x8_mvd( h, i );
407 else //if( IS_INTRA( i_mb_type ) )
408 x264_cavlc_mb_header_i( h, i_mb_type, 5, chroma );
411 static ALWAYS_INLINE void x264_cavlc_mb_header_b( x264_t *h, int i_mb_type, int chroma )
413 bs_t *s = &h->out.bs;
414 if( i_mb_type == B_8x8 )
416 bs_write_ue( s, 22 );
419 for( int i = 0; i < 4; i++ )
420 bs_write_ue( s, subpartition_b_to_golomb[ h->mb.i_sub_partition[i] ] );
423 if( h->mb.pic.i_fref[0] > 1 )
424 for( int i = 0; i < 4; i++ )
425 if( x264_mb_partition_listX_table[0][ h->mb.i_sub_partition[i] ] )
426 bs_write_te( s, h->mb.pic.i_fref[0] - 1, h->mb.cache.ref[0][x264_scan8[i*4]] );
427 if( h->mb.pic.i_fref[1] > 1 )
428 for( int i = 0; i < 4; i++ )
429 if( x264_mb_partition_listX_table[1][ h->mb.i_sub_partition[i] ] )
430 bs_write_te( s, h->mb.pic.i_fref[1] - 1, h->mb.cache.ref[1][x264_scan8[i*4]] );
433 for( int i = 0; i < 4; i++ )
434 if( x264_mb_partition_listX_table[0][ h->mb.i_sub_partition[i] ] )
435 x264_cavlc_mvd( h, 0, 4*i, 2 );
436 for( int i = 0; i < 4; i++ )
437 if( x264_mb_partition_listX_table[1][ h->mb.i_sub_partition[i] ] )
438 x264_cavlc_mvd( h, 1, 4*i, 2 );
440 else if( i_mb_type >= B_L0_L0 && i_mb_type <= B_BI_BI )
444 const uint8_t (*b_list)[2] = x264_mb_type_list_table[i_mb_type];
445 const int i_ref0_max = h->mb.pic.i_fref[0] - 1;
446 const int i_ref1_max = h->mb.pic.i_fref[1] - 1;
448 bs_write_ue( s, mb_type_b_to_golomb[ h->mb.i_partition - D_16x8 ][ i_mb_type - B_L0_L0 ] );
449 if( h->mb.i_partition == D_16x16 )
451 if( i_ref0_max && b_list[0][0] ) bs_write_te( s, i_ref0_max, h->mb.cache.ref[0][x264_scan8[0]] );
452 if( i_ref1_max && b_list[1][0] ) bs_write_te( s, i_ref1_max, h->mb.cache.ref[1][x264_scan8[0]] );
453 if( b_list[0][0] ) x264_cavlc_mvd( h, 0, 0, 4 );
454 if( b_list[1][0] ) x264_cavlc_mvd( h, 1, 0, 4 );
458 if( i_ref0_max && b_list[0][0] ) bs_write_te( s, i_ref0_max, h->mb.cache.ref[0][x264_scan8[ 0]] );
459 if( i_ref0_max && b_list[0][1] ) bs_write_te( s, i_ref0_max, h->mb.cache.ref[0][x264_scan8[12]] );
460 if( i_ref1_max && b_list[1][0] ) bs_write_te( s, i_ref1_max, h->mb.cache.ref[1][x264_scan8[ 0]] );
461 if( i_ref1_max && b_list[1][1] ) bs_write_te( s, i_ref1_max, h->mb.cache.ref[1][x264_scan8[12]] );
462 if( h->mb.i_partition == D_16x8 )
464 if( b_list[0][0] ) x264_cavlc_mvd( h, 0, 0, 4 );
465 if( b_list[0][1] ) x264_cavlc_mvd( h, 0, 8, 4 );
466 if( b_list[1][0] ) x264_cavlc_mvd( h, 1, 0, 4 );
467 if( b_list[1][1] ) x264_cavlc_mvd( h, 1, 8, 4 );
469 else //if( h->mb.i_partition == D_8x16 )
471 if( b_list[0][0] ) x264_cavlc_mvd( h, 0, 0, 2 );
472 if( b_list[0][1] ) x264_cavlc_mvd( h, 0, 4, 2 );
473 if( b_list[1][0] ) x264_cavlc_mvd( h, 1, 0, 2 );
474 if( b_list[1][1] ) x264_cavlc_mvd( h, 1, 4, 2 );
478 else if( i_mb_type == B_DIRECT )
480 else //if( IS_INTRA( i_mb_type ) )
481 x264_cavlc_mb_header_i( h, i_mb_type, 23, chroma );
484 /*****************************************************************************
485 * x264_macroblock_write:
486 *****************************************************************************/
487 void x264_macroblock_write_cavlc( x264_t *h )
489 bs_t *s = &h->out.bs;
490 const int i_mb_type = h->mb.i_type;
491 int plane_count = CHROMA444 ? 3 : 1;
492 int chroma = !CHROMA444;
495 s->i_bits_encoded = 0;
497 const int i_mb_pos_start = bs_pos( s );
502 && (!(h->mb.i_mb_y & 1) || IS_SKIP(h->mb.type[h->mb.i_mb_xy - h->mb.i_mb_stride])) )
504 bs_write1( s, MB_INTERLACED );
506 h->mb.field_decoding_flag = MB_INTERLACED;
511 if( i_mb_type == I_PCM )
513 static const uint8_t i_offsets[3] = {5,23,0};
514 uint8_t *p_start = s->p_start;
515 bs_write_ue( s, i_offsets[h->sh.i_type] + 25 );
516 i_mb_pos_tex = bs_pos( s );
517 h->stat.frame.i_mv_bits += i_mb_pos_tex - i_mb_pos_start;
521 for( int p = 0; p < plane_count; p++ )
522 for( int i = 0; i < 256; i++ )
523 bs_write( s, BIT_DEPTH, h->mb.pic.p_fenc[p][i] );
525 for( int ch = 1; ch < 3; ch++ )
526 for( int i = 0; i < 16>>CHROMA_V_SHIFT; i++ )
527 for( int j = 0; j < 8; j++ )
528 bs_write( s, BIT_DEPTH, h->mb.pic.p_fenc[ch][i*FENC_STRIDE+j] );
530 bs_init( s, s->p, s->p_end - s->p );
531 s->p_start = p_start;
533 h->stat.frame.i_tex_bits += bs_pos(s) - i_mb_pos_tex;
538 if( h->sh.i_type == SLICE_TYPE_P )
539 x264_cavlc_mb_header_p( h, i_mb_type, chroma );
540 else if( h->sh.i_type == SLICE_TYPE_B )
541 x264_cavlc_mb_header_b( h, i_mb_type, chroma );
542 else //if( h->sh.i_type == SLICE_TYPE_I )
543 x264_cavlc_mb_header_i( h, i_mb_type, 0, chroma );
546 i_mb_pos_tex = bs_pos( s );
547 h->stat.frame.i_mv_bits += i_mb_pos_tex - i_mb_pos_start;
550 /* Coded block pattern */
551 if( i_mb_type != I_16x16 )
552 bs_write_ue( s, cbp_to_golomb[chroma][IS_INTRA(i_mb_type)][(h->mb.i_cbp_chroma << 4)|h->mb.i_cbp_luma] );
554 /* transform size 8x8 flag */
555 if( x264_mb_transform_8x8_allowed( h ) && h->mb.i_cbp_luma )
556 bs_write1( s, h->mb.b_transform_8x8 );
558 if( i_mb_type == I_16x16 )
560 x264_cavlc_qp_delta( h );
563 for( int p = 0; p < plane_count; p++ )
565 x264_cavlc_block_residual( h, DCT_LUMA_DC, LUMA_DC+p, h->dct.luma16x16_dc[p] );
568 if( h->mb.i_cbp_luma )
569 for( int i = p*16; i < p*16+16; i++ )
570 x264_cavlc_block_residual( h, DCT_LUMA_AC, i, h->dct.luma4x4[i]+1 );
573 else if( h->mb.i_cbp_luma | h->mb.i_cbp_chroma )
575 x264_cavlc_qp_delta( h );
576 x264_cavlc_macroblock_luma_residual( h, plane_count );
578 if( h->mb.i_cbp_chroma )
580 /* Chroma DC residual present */
581 x264_cavlc_block_residual( h, DCT_CHROMA_DC, CHROMA_DC+0, h->dct.chroma_dc[0] );
582 x264_cavlc_block_residual( h, DCT_CHROMA_DC, CHROMA_DC+1, h->dct.chroma_dc[1] );
583 if( h->mb.i_cbp_chroma == 2 ) /* Chroma AC residual present */
585 int step = 8 << CHROMA_V_SHIFT;
586 for( int i = 16; i < 3*16; i += step )
587 for( int j = i; j < i+4; j++ )
588 x264_cavlc_block_residual( h, DCT_CHROMA_AC, j, h->dct.luma4x4[j]+1 );
593 h->stat.frame.i_tex_bits += bs_pos(s) - i_mb_pos_tex;
598 /*****************************************************************************
599 * RD only; doesn't generate a valid bitstream
600 * doesn't write cbp or chroma dc (I don't know how much this matters)
601 * doesn't write ref (never varies between calls, so no point in doing so)
602 * only writes subpartition for p8x8, needed for sub-8x8 mode decision RDO
603 * works on all partition sizes except 16x16
604 *****************************************************************************/
605 static int x264_partition_size_cavlc( x264_t *h, int i8, int i_pixel )
607 bs_t *s = &h->out.bs;
608 const int i_mb_type = h->mb.i_type;
609 int b_8x16 = h->mb.i_partition == D_8x16;
610 int plane_count = CHROMA444 ? 3 : 1;
613 if( i_mb_type == P_8x8 )
615 x264_cavlc_8x8_mvd( h, i8 );
616 bs_write_ue( s, subpartition_p_to_golomb[ h->mb.i_sub_partition[i8] ] );
618 else if( i_mb_type == P_L0 )
619 x264_cavlc_mvd( h, 0, 4*i8, 4>>b_8x16 );
620 else if( i_mb_type > B_DIRECT && i_mb_type < B_8x8 )
622 if( x264_mb_type_list_table[ i_mb_type ][0][!!i8] ) x264_cavlc_mvd( h, 0, 4*i8, 4>>b_8x16 );
623 if( x264_mb_type_list_table[ i_mb_type ][1][!!i8] ) x264_cavlc_mvd( h, 1, 4*i8, 4>>b_8x16 );
625 else //if( i_mb_type == B_8x8 )
627 if( x264_mb_partition_listX_table[0][ h->mb.i_sub_partition[i8] ] )
628 x264_cavlc_mvd( h, 0, 4*i8, 2 );
629 if( x264_mb_partition_listX_table[1][ h->mb.i_sub_partition[i8] ] )
630 x264_cavlc_mvd( h, 1, 4*i8, 2 );
633 for( j = (i_pixel < PIXEL_8x8); j >= 0; j-- )
635 for( int p = 0; p < plane_count; p++ )
636 x264_cavlc_partition_luma_residual( h, i8, p );
637 if( h->mb.i_cbp_chroma )
639 if( CHROMA_FORMAT == CHROMA_422 )
641 int offset = (5*i8) & 0x09;
642 x264_cavlc_block_residual( h, DCT_CHROMA_AC, 16+offset, h->dct.luma4x4[16+offset]+1 );
643 x264_cavlc_block_residual( h, DCT_CHROMA_AC, 18+offset, h->dct.luma4x4[18+offset]+1 );
644 x264_cavlc_block_residual( h, DCT_CHROMA_AC, 32+offset, h->dct.luma4x4[32+offset]+1 );
645 x264_cavlc_block_residual( h, DCT_CHROMA_AC, 34+offset, h->dct.luma4x4[34+offset]+1 );
649 x264_cavlc_block_residual( h, DCT_CHROMA_AC, 16+i8, h->dct.luma4x4[16+i8]+1 );
650 x264_cavlc_block_residual( h, DCT_CHROMA_AC, 32+i8, h->dct.luma4x4[32+i8]+1 );
653 i8 += x264_pixel_size[i_pixel].h >> 3;
656 return h->out.bs.i_bits_encoded;
659 static int x264_subpartition_size_cavlc( x264_t *h, int i4, int i_pixel )
661 int plane_count = CHROMA444 ? 3 : 1;
662 int b_8x4 = i_pixel == PIXEL_8x4;
663 h->out.bs.i_bits_encoded = 0;
664 x264_cavlc_mvd( h, 0, i4, 1+b_8x4 );
665 for( int p = 0; p < plane_count; p++ )
667 x264_cavlc_block_residual( h, DCT_LUMA_4x4, p*16+i4, h->dct.luma4x4[p*16+i4] );
668 if( i_pixel != PIXEL_4x4 )
669 x264_cavlc_block_residual( h, DCT_LUMA_4x4, p*16+i4+2-b_8x4, h->dct.luma4x4[p*16+i4+2-b_8x4] );
672 return h->out.bs.i_bits_encoded;
675 static int x264_cavlc_intra4x4_pred_size( x264_t *h, int i4, int i_mode )
677 if( x264_mb_predict_intra4x4_mode( h, i4 ) == x264_mb_pred_mode4x4_fix( i_mode ) )
683 static int x264_partition_i8x8_size_cavlc( x264_t *h, int i8, int i_mode )
685 int plane_count = CHROMA444 ? 3 : 1;
686 h->out.bs.i_bits_encoded = x264_cavlc_intra4x4_pred_size( h, 4*i8, i_mode );
687 bs_write_ue( &h->out.bs, cbp_to_golomb[!CHROMA444][1][(h->mb.i_cbp_chroma << 4)|h->mb.i_cbp_luma] );
688 for( int p = 0; p < plane_count; p++ )
689 x264_cavlc_partition_luma_residual( h, i8, p );
690 return h->out.bs.i_bits_encoded;
693 static int x264_partition_i4x4_size_cavlc( x264_t *h, int i4, int i_mode )
695 int plane_count = CHROMA444 ? 3 : 1;
696 h->out.bs.i_bits_encoded = x264_cavlc_intra4x4_pred_size( h, i4, i_mode );
697 for( int p = 0; p < plane_count; p++ )
698 x264_cavlc_block_residual( h, DCT_LUMA_4x4, p*16+i4, h->dct.luma4x4[p*16+i4] );
699 return h->out.bs.i_bits_encoded;
702 static int x264_chroma_size_cavlc( x264_t *h )
704 h->out.bs.i_bits_encoded = bs_size_ue( x264_mb_chroma_pred_mode_fix[h->mb.i_chroma_pred_mode] );
705 if( h->mb.i_cbp_chroma )
707 x264_cavlc_block_residual( h, DCT_CHROMA_DC, CHROMA_DC+0, h->dct.chroma_dc[0] );
708 x264_cavlc_block_residual( h, DCT_CHROMA_DC, CHROMA_DC+1, h->dct.chroma_dc[1] );
710 if( h->mb.i_cbp_chroma == 2 )
712 int step = 8 << CHROMA_V_SHIFT;
713 for( int i = 16; i < 3*16; i += step )
714 for( int j = i; j < i+4; j++ )
715 x264_cavlc_block_residual( h, DCT_CHROMA_AC, j, h->dct.luma4x4[j]+1 );
718 return h->out.bs.i_bits_encoded;