1 /*****************************************************************************
2  * cavlc.c: cavlc bitstream writing
3  *****************************************************************************
4  * Copyright (C) 2003-2014 x264 project
5  *
6  * Authors: Laurent Aimar <fenrir@via.ecp.fr>
7  *          Loren Merritt <lorenm@u.washington.edu>
8  *          Fiona Glaser <fiona@x264.com>
9  *
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.
14  *
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.
19  *
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  *
24  * This program is also available under a commercial proprietary license.
25  * For more information, contact us at licensing@x264.com.
26  *****************************************************************************/
27 
28 #include "common/common.h"
29 #include "macroblock.h"
30 
31 #ifndef RDO_SKIP_BS
32 #define RDO_SKIP_BS 0
33 #endif
34 
35 /* [400,420][inter,intra] */
36 static const uint8_t cbp_to_golomb[2][2][48] =
37 {
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 }}
46 };
47 
48 static const uint8_t mb_type_b_to_golomb[3][9]=
49 {
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 */
53 };
54 
55 static const uint8_t subpartition_p_to_golomb[4]=
56 {
57     3, 1, 2, 0
58 };
59 
60 static const uint8_t subpartition_b_to_golomb[13]=
61 {
62     10,  4,  5,  1, 11,  6,  7,  2, 12,  8,  9,  3,  0
63 };
64 
65 #define bs_write_vlc(s,v) bs_write( s, (v).i_size, (v).i_bits )
66 
67 /****************************************************************************
68  * x264_cavlc_block_residual:
69  ****************************************************************************/
x264_cavlc_block_residual_escape(x264_t * h,int i_suffix_length,int level)70 static inline int x264_cavlc_block_residual_escape( x264_t *h, int i_suffix_length, int level )
71 {
72     bs_t *s = &h->out.bs;
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 )
79     {
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)) );
82     }
83     else
84     {
85         i_level_code -= 15 << i_suffix_length;
86         if( i_suffix_length == 0 )
87             i_level_code -= 15;
88 
89         /* If the prefix size exceeds 15, High Profile is required. */
90         if( i_level_code >= 1<<12 )
91         {
92             if( h->sps->i_profile_idc >= PROFILE_HIGH )
93             {
94                 while( i_level_code > 1<<(i_level_prefix-3) )
95                 {
96                     i_level_code -= 1<<(i_level_prefix-3);
97                     i_level_prefix++;
98                 }
99             }
100             else
101             {
102 #if RDO_SKIP_BS
103                 /* Weight highly against overflows. */
104                 s->i_bits_encoded += 2000;
105 #else
106                 /* We've had an overflow; note it down and re-encode the MB later. */
107                 h->mb.b_overflow = 1;
108 #endif
109             }
110         }
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) );
113     }
114     if( i_suffix_length == 0 )
115         i_suffix_length++;
116     if( abs_level > next_suffix[i_suffix_length] )
117         i_suffix_length++;
118     return i_suffix_length;
119 }
120 
x264_cavlc_block_residual_internal(x264_t * h,int ctx_block_cat,dctcoef * l,int nC)121 static int x264_cavlc_block_residual_internal( x264_t *h, int ctx_block_cat, dctcoef *l, int nC )
122 {
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;
128     unsigned int i_sign;
129     int zero_run_code;
130 
131     /* level and run and total */
132     i_total = h->quantf.coeff_level_run[ctx_block_cat]( l, &runlevel );
133     x264_prefetch( &x264_run_before[runlevel.mask] );
134     i_total_zero = runlevel.last + 1 - i_total;
135 
136     /* branchless i_trailing calculation */
137     runlevel.level[i_total+0] = 2;
138     runlevel.level[i_total+1] = 2;
139     i_trailing = ((((runlevel.level[0]+1) | (1-runlevel.level[0])) >> 31) & 1) // abs(runlevel.level[0])>1
140                | ((((runlevel.level[1]+1) | (1-runlevel.level[1])) >> 31) & 2)
141                | ((((runlevel.level[2]+1) | (1-runlevel.level[2])) >> 31) & 4);
142     i_trailing = ctz_index[i_trailing];
143     i_sign = ((runlevel.level[2] >> 31) & 1)
144            | ((runlevel.level[1] >> 31) & 2)
145            | ((runlevel.level[0] >> 31) & 4);
146     i_sign >>= 3-i_trailing;
147 
148     /* total/trailing */
149     bs_write_vlc( s, x264_coeff_token[nC][i_total-1][i_trailing] );
150 
151     i_suffix_length = i_total > 10 && i_trailing < 3;
152     bs_write( s, i_trailing, i_sign );
153 
154     if( i_trailing < i_total )
155     {
156         int val = runlevel.level[i_trailing];
157         int val_original = runlevel.level[i_trailing]+LEVEL_TABLE_SIZE/2;
158 		int i;
159 
160 		val -= ((val>>31)|1) & -(i_trailing < 3); /* as runlevel.level[i] can't be 1 for the first one if i_trailing < 3 */
161         val += LEVEL_TABLE_SIZE/2;
162 
163         if( (unsigned)val_original < LEVEL_TABLE_SIZE )
164         {
165             bs_write_vlc( s, x264_level_token[i_suffix_length][val] );
166             i_suffix_length = x264_level_token[i_suffix_length][val_original].i_next;
167         }
168         else
169             i_suffix_length = x264_cavlc_block_residual_escape( h, i_suffix_length, val-LEVEL_TABLE_SIZE/2 );
170         for( i = i_trailing+1; i < i_total; i++ )
171         {
172             val = runlevel.level[i] + LEVEL_TABLE_SIZE/2;
173             if( (unsigned)val < LEVEL_TABLE_SIZE )
174             {
175                 bs_write_vlc( s, x264_level_token[i_suffix_length][val] );
176                 i_suffix_length = x264_level_token[i_suffix_length][val].i_next;
177             }
178             else
179                 i_suffix_length = x264_cavlc_block_residual_escape( h, i_suffix_length, val-LEVEL_TABLE_SIZE/2 );
180         }
181     }
182 
183     if( ctx_block_cat == DCT_CHROMA_DC )
184     {
185         if( i_total < 8>>CHROMA_V_SHIFT )
186         {
187             vlc_t total_zeros = CHROMA_FORMAT == CHROMA_420 ? x264_total_zeros_2x2_dc[i_total-1][i_total_zero]
188                                                             : x264_total_zeros_2x4_dc[i_total-1][i_total_zero];
189             bs_write_vlc( s, total_zeros );
190         }
191     }
192     else if( (uint8_t)i_total < count_cat[ctx_block_cat] )
193         bs_write_vlc( s, x264_total_zeros[i_total-1][i_total_zero] );
194 
195     zero_run_code = x264_run_before[runlevel.mask];
196     bs_write( s, zero_run_code&0x1f, zero_run_code>>5 );
197 
198     return i_total;
199 }
200 
201 static const uint8_t ct_index[17] = {0,0,1,1,2,2,2,2,3,3,3,3,3,3,3,3,3};
202 
203 #define x264_cavlc_block_residual(h,cat,idx,l)\
204 {\
205     int nC = cat == DCT_CHROMA_DC ? 5 - CHROMA_V_SHIFT\
206                                   : ct_index[x264_mb_predict_non_zero_code( h, cat == DCT_LUMA_DC ? (idx - LUMA_DC)*16 : idx )];\
207     uint8_t *nnz = &h->mb.cache.non_zero_count[x264_scan8[idx]];\
208     if( !*nnz )\
209         bs_write_vlc( &h->out.bs, x264_coeff0_token[nC] );\
210     else\
211         *nnz = x264_cavlc_block_residual_internal(h,cat,l,nC);\
212 }
213 
x264_cavlc_qp_delta(x264_t * h)214 static void x264_cavlc_qp_delta( x264_t *h )
215 {
216     bs_t *s = &h->out.bs;
217     int i_dqp = h->mb.i_qp - h->mb.i_last_qp;
218 
219     /* Avoid writing a delta quant if we have an empty i16x16 block, e.g. in a completely
220      * flat background area. Don't do this if it would raise the quantizer, since that could
221      * cause unexpected deblocking artifacts. */
222     if( h->mb.i_type == I_16x16 && !(h->mb.i_cbp_luma | h->mb.i_cbp_chroma)
223         && !h->mb.cache.non_zero_count[x264_scan8[LUMA_DC]]
224         && !h->mb.cache.non_zero_count[x264_scan8[CHROMA_DC+0]]
225         && !h->mb.cache.non_zero_count[x264_scan8[CHROMA_DC+1]]
226         && h->mb.i_qp > h->mb.i_last_qp )
227     {
228 #if !RDO_SKIP_BS
229         h->mb.i_qp = h->mb.i_last_qp;
230 #endif
231         i_dqp = 0;
232     }
233 
234     if( i_dqp )
235     {
236         if( i_dqp < -(QP_MAX_SPEC+1)/2 )
237             i_dqp += QP_MAX_SPEC+1;
238         else if( i_dqp > QP_MAX_SPEC/2 )
239             i_dqp -= QP_MAX_SPEC+1;
240     }
241     bs_write_se( s, i_dqp );
242 }
243 
x264_cavlc_mvd(x264_t * h,int i_list,int idx,int width)244 static void x264_cavlc_mvd( x264_t *h, int i_list, int idx, int width )
245 {
246     bs_t *s = &h->out.bs;
247     ALIGNED_4( int16_t mvp[2] );
248     x264_mb_predict_mv( h, i_list, idx, width, mvp );
249     bs_write_se( s, h->mb.cache.mv[i_list][x264_scan8[idx]][0] - mvp[0] );
250     bs_write_se( s, h->mb.cache.mv[i_list][x264_scan8[idx]][1] - mvp[1] );
251 }
252 
x264_cavlc_8x8_mvd(x264_t * h,int i)253 static inline void x264_cavlc_8x8_mvd( x264_t *h, int i )
254 {
255     switch( h->mb.i_sub_partition[i] )
256     {
257         case D_L0_8x8:
258             x264_cavlc_mvd( h, 0, 4*i, 2 );
259             break;
260         case D_L0_8x4:
261             x264_cavlc_mvd( h, 0, 4*i+0, 2 );
262             x264_cavlc_mvd( h, 0, 4*i+2, 2 );
263             break;
264         case D_L0_4x8:
265             x264_cavlc_mvd( h, 0, 4*i+0, 1 );
266             x264_cavlc_mvd( h, 0, 4*i+1, 1 );
267             break;
268         case D_L0_4x4:
269             x264_cavlc_mvd( h, 0, 4*i+0, 1 );
270             x264_cavlc_mvd( h, 0, 4*i+1, 1 );
271             x264_cavlc_mvd( h, 0, 4*i+2, 1 );
272             x264_cavlc_mvd( h, 0, 4*i+3, 1 );
273             break;
274     }
275 }
276 
x264_cavlc_macroblock_luma_residual(x264_t * h,int plane_count)277 static ALWAYS_INLINE void x264_cavlc_macroblock_luma_residual( x264_t *h, int plane_count )
278 {
279 	int p;
280 
281 	if( h->mb.b_transform_8x8 )
282     {
283 		/* shuffle 8x8 dct coeffs into 4x4 lists */
284         for( p = 0; p < plane_count; p++ ) {
285             int i8;
286 			for( i8 = 0; i8 < 4; i8++ )
287                 if( h->mb.cache.non_zero_count[x264_scan8[p*16+i8*4]] )
288                     h->zigzagf.interleave_8x8_cavlc( h->dct.luma4x4[p*16+i8*4], h->dct.luma8x8[p*4+i8],
289                                                      &h->mb.cache.non_zero_count[x264_scan8[p*16+i8*4]] );
290 		}
291 	}
292 
293     for( p = 0; p < plane_count; p++ ) {
294 		int i8;
295         int msk;
296 		int skip;
297 
298 		FOREACH_BIT( i8, 0, h->mb.i_cbp_luma ) {
299 			int i4;
300             for( 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] );
302 		}
303 	}
304 }
305 
x264_cavlc_partition_luma_residual(x264_t * h,int i8,int p)306 static ALWAYS_INLINE void x264_cavlc_partition_luma_residual( x264_t *h, int i8, int p )
307 {
308     if( h->mb.b_transform_8x8 && h->mb.cache.non_zero_count[x264_scan8[i8*4]] )
309         h->zigzagf.interleave_8x8_cavlc( h->dct.luma4x4[i8*4+p*16], h->dct.luma8x8[i8+p*4],
310                                          &h->mb.cache.non_zero_count[x264_scan8[i8*4+p*16]] );
311 
312     if( h->mb.i_cbp_luma & (1 << i8) ) {
313 		int i4;
314 		for( i4 = 0; i4 < 4; i4++ )
315             x264_cavlc_block_residual( h, DCT_LUMA_4x4, i4+i8*4+p*16, h->dct.luma4x4[i4+i8*4+p*16] );
316 	}
317 }
318 
x264_cavlc_mb_header_i(x264_t * h,int i_mb_type,int i_mb_i_offset,int chroma)319 static void x264_cavlc_mb_header_i( x264_t *h, int i_mb_type, int i_mb_i_offset, int chroma )
320 {
321     bs_t *s = &h->out.bs;
322     if( i_mb_type == I_16x16 )
323     {
324         bs_write_ue( s, i_mb_i_offset + 1 + x264_mb_pred_mode16x16_fix[h->mb.i_intra16x16_pred_mode] +
325                         h->mb.i_cbp_chroma * 4 + ( h->mb.i_cbp_luma == 0 ? 0 : 12 ) );
326     }
327     else //if( i_mb_type == I_4x4 || i_mb_type == I_8x8 )
328     {
329         int di = i_mb_type == I_8x8 ? 4 : 1;
330 		int i;
331 
332 		bs_write_ue( s, i_mb_i_offset + 0 );
333         if( h->pps->b_transform_8x8_mode )
334             bs_write1( s, h->mb.b_transform_8x8 );
335 
336         /* Prediction: Luma */
337         for( i = 0; i < 16; i += di )
338         {
339             int i_pred = x264_mb_predict_intra4x4_mode( h, i );
340             int i_mode = x264_mb_pred_mode4x4_fix( h->mb.cache.intra4x4_pred_mode[x264_scan8[i]] );
341 
342             if( i_pred == i_mode )
343                 bs_write1( s, 1 );  /* b_prev_intra4x4_pred_mode */
344             else
345                 bs_write( s, 4, i_mode - (i_mode > i_pred) );
346         }
347 
348     }
349     if( chroma )
350         bs_write_ue( s, x264_mb_chroma_pred_mode_fix[h->mb.i_chroma_pred_mode] );
351 }
352 
x264_cavlc_mb_header_p(x264_t * h,int i_mb_type,int chroma)353 static ALWAYS_INLINE void x264_cavlc_mb_header_p( x264_t *h, int i_mb_type, int chroma )
354 {
355     bs_t *s = &h->out.bs;
356     if( i_mb_type == P_L0 )
357     {
358         if( h->mb.i_partition == D_16x16 )
359         {
360             bs_write1( s, 1 );
361 
362             if( h->mb.pic.i_fref[0] > 1 )
363                 bs_write_te( s, h->mb.pic.i_fref[0] - 1, h->mb.cache.ref[0][x264_scan8[0]] );
364             x264_cavlc_mvd( h, 0, 0, 4 );
365         }
366         else if( h->mb.i_partition == D_16x8 )
367         {
368             bs_write_ue( s, 1 );
369             if( h->mb.pic.i_fref[0] > 1 )
370             {
371                 bs_write_te( s, h->mb.pic.i_fref[0] - 1, h->mb.cache.ref[0][x264_scan8[0]] );
372                 bs_write_te( s, h->mb.pic.i_fref[0] - 1, h->mb.cache.ref[0][x264_scan8[8]] );
373             }
374             x264_cavlc_mvd( h, 0, 0, 4 );
375             x264_cavlc_mvd( h, 0, 8, 4 );
376         }
377         else if( h->mb.i_partition == D_8x16 )
378         {
379             bs_write_ue( s, 2 );
380             if( h->mb.pic.i_fref[0] > 1 )
381             {
382                 bs_write_te( s, h->mb.pic.i_fref[0] - 1, h->mb.cache.ref[0][x264_scan8[0]] );
383                 bs_write_te( s, h->mb.pic.i_fref[0] - 1, h->mb.cache.ref[0][x264_scan8[4]] );
384             }
385             x264_cavlc_mvd( h, 0, 0, 2 );
386             x264_cavlc_mvd( h, 0, 4, 2 );
387         }
388     }
389     else if( i_mb_type == P_8x8 )
390     {
391         int b_sub_ref;
392 		int i2;
393 
394 		if( (h->mb.cache.ref[0][x264_scan8[0]] | h->mb.cache.ref[0][x264_scan8[ 4]] |
395              h->mb.cache.ref[0][x264_scan8[8]] | h->mb.cache.ref[0][x264_scan8[12]]) == 0 )
396         {
397             bs_write_ue( s, 4 );
398             b_sub_ref = 0;
399         }
400         else
401         {
402             bs_write_ue( s, 3 );
403             b_sub_ref = 1;
404         }
405 
406         /* sub mb type */
407         if( h->param.analyse.inter & X264_ANALYSE_PSUB8x8 ) {
408 			int i;
409 			for( i = 0; i < 4; i++ )
410                 bs_write_ue( s, subpartition_p_to_golomb[ h->mb.i_sub_partition[i] ] );
411 		} else
412             bs_write( s, 4, 0xf );
413 
414         /* ref0 */
415         if( b_sub_ref )
416         {
417             bs_write_te( s, h->mb.pic.i_fref[0] - 1, h->mb.cache.ref[0][x264_scan8[0]] );
418             bs_write_te( s, h->mb.pic.i_fref[0] - 1, h->mb.cache.ref[0][x264_scan8[4]] );
419             bs_write_te( s, h->mb.pic.i_fref[0] - 1, h->mb.cache.ref[0][x264_scan8[8]] );
420             bs_write_te( s, h->mb.pic.i_fref[0] - 1, h->mb.cache.ref[0][x264_scan8[12]] );
421         }
422 
423 		for( i2 = 0; i2 < 4; i2++ )
424             x264_cavlc_8x8_mvd( h, i2 );
425     }
426     else //if( IS_INTRA( i_mb_type ) )
427         x264_cavlc_mb_header_i( h, i_mb_type, 5, chroma );
428 }
429 
x264_cavlc_mb_header_b(x264_t * h,int i_mb_type,int chroma)430 static ALWAYS_INLINE void x264_cavlc_mb_header_b( x264_t *h, int i_mb_type, int chroma )
431 {
432     bs_t *s = &h->out.bs;
433     if( i_mb_type == B_8x8 )
434     {
435 		int i2;
436         bs_write_ue( s, 22 );
437 
438         /* sub mb type */
439 		for( i2 = 0; i2 < 4; i2++ )
440             bs_write_ue( s, subpartition_b_to_golomb[ h->mb.i_sub_partition[i2] ] );
441 
442         /* ref */
443         if( h->mb.pic.i_fref[0] > 1 ) {
444 			int i;
445 			for( i = 0; i < 4; i++ )
446                 if( x264_mb_partition_listX_table[0][ h->mb.i_sub_partition[i] ] )
447                     bs_write_te( s, h->mb.pic.i_fref[0] - 1, h->mb.cache.ref[0][x264_scan8[i*4]] );
448 		}
449 		if( h->mb.pic.i_fref[1] > 1 ) {
450 			for( i2 = 0; i2 < 4; i2++ )
451                 if( x264_mb_partition_listX_table[1][ h->mb.i_sub_partition[i2] ] )
452                     bs_write_te( s, h->mb.pic.i_fref[1] - 1, h->mb.cache.ref[1][x264_scan8[i2*4]] );
453 		}
454         /* mvd */
455         for( i2 = 0; i2 < 4; i2++ ) {
456             if( x264_mb_partition_listX_table[0][ h->mb.i_sub_partition[i2] ] )
457                 x264_cavlc_mvd( h, 0, 4*i2, 2 );
458 		}
459 		for( i2 = 0; i2 < 4; i2++ )
460             if( x264_mb_partition_listX_table[1][ h->mb.i_sub_partition[i2] ] )
461                 x264_cavlc_mvd( h, 1, 4*i2, 2 );
462     }
463     else if( i_mb_type >= B_L0_L0 && i_mb_type <= B_BI_BI )
464     {
465         /* All B mode */
466         /* Motion Vector */
467         const uint8_t (*b_list)[2] = x264_mb_type_list_table[i_mb_type];
468         const int i_ref0_max = h->mb.pic.i_fref[0] - 1;
469         const int i_ref1_max = h->mb.pic.i_fref[1] - 1;
470 
471         bs_write_ue( s, mb_type_b_to_golomb[ h->mb.i_partition - D_16x8 ][ i_mb_type - B_L0_L0 ] );
472         if( h->mb.i_partition == D_16x16 )
473         {
474             if( i_ref0_max && b_list[0][0] ) bs_write_te( s, i_ref0_max, h->mb.cache.ref[0][x264_scan8[0]] );
475             if( i_ref1_max && b_list[1][0] ) bs_write_te( s, i_ref1_max, h->mb.cache.ref[1][x264_scan8[0]] );
476             if( b_list[0][0] ) x264_cavlc_mvd( h, 0, 0, 4 );
477             if( b_list[1][0] ) x264_cavlc_mvd( h, 1, 0, 4 );
478         }
479         else
480         {
481             if( i_ref0_max && b_list[0][0] ) bs_write_te( s, i_ref0_max, h->mb.cache.ref[0][x264_scan8[ 0]] );
482             if( i_ref0_max && b_list[0][1] ) bs_write_te( s, i_ref0_max, h->mb.cache.ref[0][x264_scan8[12]] );
483             if( i_ref1_max && b_list[1][0] ) bs_write_te( s, i_ref1_max, h->mb.cache.ref[1][x264_scan8[ 0]] );
484             if( i_ref1_max && b_list[1][1] ) bs_write_te( s, i_ref1_max, h->mb.cache.ref[1][x264_scan8[12]] );
485             if( h->mb.i_partition == D_16x8 )
486             {
487                 if( b_list[0][0] ) x264_cavlc_mvd( h, 0, 0, 4 );
488                 if( b_list[0][1] ) x264_cavlc_mvd( h, 0, 8, 4 );
489                 if( b_list[1][0] ) x264_cavlc_mvd( h, 1, 0, 4 );
490                 if( b_list[1][1] ) x264_cavlc_mvd( h, 1, 8, 4 );
491             }
492             else //if( h->mb.i_partition == D_8x16 )
493             {
494                 if( b_list[0][0] ) x264_cavlc_mvd( h, 0, 0, 2 );
495                 if( b_list[0][1] ) x264_cavlc_mvd( h, 0, 4, 2 );
496                 if( b_list[1][0] ) x264_cavlc_mvd( h, 1, 0, 2 );
497                 if( b_list[1][1] ) x264_cavlc_mvd( h, 1, 4, 2 );
498             }
499         }
500     }
501     else if( i_mb_type == B_DIRECT )
502         bs_write1( s, 1 );
503     else //if( IS_INTRA( i_mb_type ) )
504         x264_cavlc_mb_header_i( h, i_mb_type, 23, chroma );
505 }
506 
507 /*****************************************************************************
508  * x264_macroblock_write:
509  *****************************************************************************/
x264_macroblock_write_cavlc(x264_t * h)510 void x264_macroblock_write_cavlc( x264_t *h )
511 {
512     bs_t *s = &h->out.bs;
513     const int i_mb_type = h->mb.i_type;
514     int plane_count = CHROMA444 ? 3 : 1;
515     int chroma = !CHROMA444;
516 
517 #if RDO_SKIP_BS
518     s->i_bits_encoded = 0;
519 #else
520     const int i_mb_pos_start = bs_pos( s );
521     int       i_mb_pos_tex;
522 #endif
523 
524     if( SLICE_MBAFF
525         && (!(h->mb.i_mb_y & 1) || IS_SKIP(h->mb.type[h->mb.i_mb_xy - h->mb.i_mb_stride])) )
526     {
527         bs_write1( s, MB_INTERLACED );
528 #if !RDO_SKIP_BS
529         h->mb.field_decoding_flag = MB_INTERLACED;
530 #endif
531     }
532 
533 #if !RDO_SKIP_BS
534     if( i_mb_type == I_PCM )
535     {
536         static const uint8_t i_offsets[3] = {5,23,0};
537         uint8_t *p_start = s->p_start;
538 		int p;
539 
540 		bs_write_ue( s, i_offsets[h->sh.i_type] + 25 );
541         i_mb_pos_tex = bs_pos( s );
542         h->stat.frame.i_mv_bits += i_mb_pos_tex - i_mb_pos_start;
543 
544         bs_align_0( s );
545 
546         for( p = 0; p < plane_count; p++ ) {
547 			int i;
548 			for( i = 0; i < 256; i++ )
549                 bs_write( s, BIT_DEPTH, h->mb.pic.p_fenc[p][i] );
550 		}
551 		if( chroma ) {
552 			int ch;
553 			for( ch = 1; ch < 3; ch++ ) {
554 				int i;
555 				for( i = 0; i < 16>>CHROMA_V_SHIFT; i++ ) {
556 					int j;
557 					for( j = 0; j < 8; j++ )
558                         bs_write( s, BIT_DEPTH, h->mb.pic.p_fenc[ch][i*FENC_STRIDE+j] );
559 				}
560 			}
561 
562 			bs_init( s, s->p, s->p_end - s->p );
563 		}
564 		s->p_start = p_start;
565 
566         h->stat.frame.i_tex_bits += bs_pos(s) - i_mb_pos_tex;
567         return;
568     }
569 #endif
570 
571     if( h->sh.i_type == SLICE_TYPE_P )
572         x264_cavlc_mb_header_p( h, i_mb_type, chroma );
573     else if( h->sh.i_type == SLICE_TYPE_B )
574         x264_cavlc_mb_header_b( h, i_mb_type, chroma );
575     else //if( h->sh.i_type == SLICE_TYPE_I )
576         x264_cavlc_mb_header_i( h, i_mb_type, 0, chroma );
577 
578 #if !RDO_SKIP_BS
579     i_mb_pos_tex = bs_pos( s );
580     h->stat.frame.i_mv_bits += i_mb_pos_tex - i_mb_pos_start;
581 #endif
582 
583     /* Coded block pattern */
584     if( i_mb_type != I_16x16 )
585         bs_write_ue( s, cbp_to_golomb[chroma][IS_INTRA(i_mb_type)][(h->mb.i_cbp_chroma << 4)|h->mb.i_cbp_luma] );
586 
587     /* transform size 8x8 flag */
588     if( x264_mb_transform_8x8_allowed( h ) && h->mb.i_cbp_luma )
589         bs_write1( s, h->mb.b_transform_8x8 );
590 
591     if( i_mb_type == I_16x16 )
592     {
593 		int p;
594 		x264_cavlc_qp_delta( h );
595 
596         /* DC Luma */
597         for( p = 0; p < plane_count; p++ )
598         {
599             x264_cavlc_block_residual( h, DCT_LUMA_DC, LUMA_DC+p, h->dct.luma16x16_dc[p] );
600 
601             /* AC Luma */
602             if( h->mb.i_cbp_luma ) {
603 				int i;
604 				for( i = p*16; i < p*16+16; i++ )
605                     x264_cavlc_block_residual( h, DCT_LUMA_AC, i, h->dct.luma4x4[i]+1 );
606 			}
607 		}
608     }
609     else if( h->mb.i_cbp_luma | h->mb.i_cbp_chroma )
610     {
611         x264_cavlc_qp_delta( h );
612         x264_cavlc_macroblock_luma_residual( h, plane_count );
613     }
614     if( h->mb.i_cbp_chroma )
615     {
616         /* Chroma DC residual present */
617         x264_cavlc_block_residual( h, DCT_CHROMA_DC, CHROMA_DC+0, h->dct.chroma_dc[0] );
618         x264_cavlc_block_residual( h, DCT_CHROMA_DC, CHROMA_DC+1, h->dct.chroma_dc[1] );
619         if( h->mb.i_cbp_chroma == 2 ) /* Chroma AC residual present */
620         {
621             int step = 8 << CHROMA_V_SHIFT;
622 			int i;
623 			int j;
624 			for( i = 16; i < 3*16; i += step )
625                 for( j = i; j < i+4; j++ )
626                     x264_cavlc_block_residual( h, DCT_CHROMA_AC, j, h->dct.luma4x4[j]+1 );
627         }
628     }
629 
630 #if !RDO_SKIP_BS
631     h->stat.frame.i_tex_bits += bs_pos(s) - i_mb_pos_tex;
632 #endif
633 }
634 
635 #if RDO_SKIP_BS
636 /*****************************************************************************
637  * RD only; doesn't generate a valid bitstream
638  * doesn't write cbp or chroma dc (I don't know how much this matters)
639  * doesn't write ref (never varies between calls, so no point in doing so)
640  * only writes subpartition for p8x8, needed for sub-8x8 mode decision RDO
641  * works on all partition sizes except 16x16
642  *****************************************************************************/
x264_partition_size_cavlc(x264_t * h,int i8,int i_pixel)643 static int x264_partition_size_cavlc( x264_t *h, int i8, int i_pixel )
644 {
645     bs_t *s = &h->out.bs;
646     const int i_mb_type = h->mb.i_type;
647     int b_8x16 = h->mb.i_partition == D_8x16;
648     int plane_count = CHROMA444 ? 3 : 1;
649     int j;
650 
651     if( i_mb_type == P_8x8 )
652     {
653         x264_cavlc_8x8_mvd( h, i8 );
654         bs_write_ue( s, subpartition_p_to_golomb[ h->mb.i_sub_partition[i8] ] );
655     }
656     else if( i_mb_type == P_L0 )
657         x264_cavlc_mvd( h, 0, 4*i8, 4>>b_8x16 );
658     else if( i_mb_type > B_DIRECT && i_mb_type < B_8x8 )
659     {
660         if( x264_mb_type_list_table[ i_mb_type ][0][!!i8] ) x264_cavlc_mvd( h, 0, 4*i8, 4>>b_8x16 );
661         if( x264_mb_type_list_table[ i_mb_type ][1][!!i8] ) x264_cavlc_mvd( h, 1, 4*i8, 4>>b_8x16 );
662     }
663     else //if( i_mb_type == B_8x8 )
664     {
665         if( x264_mb_partition_listX_table[0][ h->mb.i_sub_partition[i8] ] )
666             x264_cavlc_mvd( h, 0, 4*i8, 2 );
667         if( x264_mb_partition_listX_table[1][ h->mb.i_sub_partition[i8] ] )
668             x264_cavlc_mvd( h, 1, 4*i8, 2 );
669     }
670 
671     for( j = (i_pixel < PIXEL_8x8); j >= 0; j-- )
672     {
673 		int p;
674 		for( p = 0; p < plane_count; p++ )
675             x264_cavlc_partition_luma_residual( h, i8, p );
676         if( h->mb.i_cbp_chroma )
677         {
678             if( CHROMA_FORMAT == CHROMA_422 )
679             {
680                 int offset = (5*i8) & 0x09;
681                 x264_cavlc_block_residual( h, DCT_CHROMA_AC, 16+offset, h->dct.luma4x4[16+offset]+1 );
682                 x264_cavlc_block_residual( h, DCT_CHROMA_AC, 18+offset, h->dct.luma4x4[18+offset]+1 );
683                 x264_cavlc_block_residual( h, DCT_CHROMA_AC, 32+offset, h->dct.luma4x4[32+offset]+1 );
684                 x264_cavlc_block_residual( h, DCT_CHROMA_AC, 34+offset, h->dct.luma4x4[34+offset]+1 );
685             }
686             else
687             {
688                 x264_cavlc_block_residual( h, DCT_CHROMA_AC, 16+i8, h->dct.luma4x4[16+i8]+1 );
689                 x264_cavlc_block_residual( h, DCT_CHROMA_AC, 32+i8, h->dct.luma4x4[32+i8]+1 );
690             }
691         }
692         i8 += x264_pixel_size[i_pixel].h >> 3;
693     }
694 
695     return h->out.bs.i_bits_encoded;
696 }
697 
x264_subpartition_size_cavlc(x264_t * h,int i4,int i_pixel)698 static int x264_subpartition_size_cavlc( x264_t *h, int i4, int i_pixel )
699 {
700     int plane_count = CHROMA444 ? 3 : 1;
701     int b_8x4 = i_pixel == PIXEL_8x4;
702 	int p;
703 
704 	h->out.bs.i_bits_encoded = 0;
705     x264_cavlc_mvd( h, 0, i4, 1+b_8x4 );
706 	for( p = 0; p < plane_count; p++ )
707     {
708         x264_cavlc_block_residual( h, DCT_LUMA_4x4, p*16+i4, h->dct.luma4x4[p*16+i4] );
709         if( i_pixel != PIXEL_4x4 )
710             x264_cavlc_block_residual( h, DCT_LUMA_4x4, p*16+i4+2-b_8x4, h->dct.luma4x4[p*16+i4+2-b_8x4] );
711     }
712 
713     return h->out.bs.i_bits_encoded;
714 }
715 
x264_cavlc_intra4x4_pred_size(x264_t * h,int i4,int i_mode)716 static int x264_cavlc_intra4x4_pred_size( x264_t *h, int i4, int i_mode )
717 {
718     if( x264_mb_predict_intra4x4_mode( h, i4 ) == x264_mb_pred_mode4x4_fix( i_mode ) )
719         return 1;
720     else
721         return 4;
722 }
723 
x264_partition_i8x8_size_cavlc(x264_t * h,int i8,int i_mode)724 static int x264_partition_i8x8_size_cavlc( x264_t *h, int i8, int i_mode )
725 {
726     int plane_count = CHROMA444 ? 3 : 1;
727 	int p;
728 
729 	h->out.bs.i_bits_encoded = x264_cavlc_intra4x4_pred_size( h, 4*i8, i_mode );
730     bs_write_ue( &h->out.bs, cbp_to_golomb[!CHROMA444][1][(h->mb.i_cbp_chroma << 4)|h->mb.i_cbp_luma] );
731     for( p = 0; p < plane_count; p++ )
732         x264_cavlc_partition_luma_residual( h, i8, p );
733     return h->out.bs.i_bits_encoded;
734 }
735 
x264_partition_i4x4_size_cavlc(x264_t * h,int i4,int i_mode)736 static int x264_partition_i4x4_size_cavlc( x264_t *h, int i4, int i_mode )
737 {
738     int plane_count = CHROMA444 ? 3 : 1;
739 	int p;
740 
741 	h->out.bs.i_bits_encoded = x264_cavlc_intra4x4_pred_size( h, i4, i_mode );
742     for( p = 0; p < plane_count; p++ )
743         x264_cavlc_block_residual( h, DCT_LUMA_4x4, p*16+i4, h->dct.luma4x4[p*16+i4] );
744     return h->out.bs.i_bits_encoded;
745 }
746 
x264_chroma_size_cavlc(x264_t * h)747 static int x264_chroma_size_cavlc( x264_t *h )
748 {
749     h->out.bs.i_bits_encoded = bs_size_ue( x264_mb_chroma_pred_mode_fix[h->mb.i_chroma_pred_mode] );
750     if( h->mb.i_cbp_chroma )
751     {
752         x264_cavlc_block_residual( h, DCT_CHROMA_DC, CHROMA_DC+0, h->dct.chroma_dc[0] );
753         x264_cavlc_block_residual( h, DCT_CHROMA_DC, CHROMA_DC+1, h->dct.chroma_dc[1] );
754 
755         if( h->mb.i_cbp_chroma == 2 )
756         {
757             int step = 8 << CHROMA_V_SHIFT;
758 			int i;
759 			int j;
760 			for( i = 16; i < 3*16; i += step )
761                 for( j = i; j < i+4; j++ )
762                     x264_cavlc_block_residual( h, DCT_CHROMA_AC, j, h->dct.luma4x4[j]+1 );
763         }
764     }
765     return h->out.bs.i_bits_encoded;
766 }
767 #endif
768