]> git.sesse.net Git - x264/blob - encoder/cavlc.c
CAVLC optimizations
[x264] / encoder / cavlc.c
1 /*****************************************************************************
2  * cavlc.c: h264 encoder library
3  *****************************************************************************
4  * Copyright (C) 2003-2008 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
25 #include "common/common.h"
26 #include "macroblock.h"
27
28 #ifndef RDO_SKIP_BS
29 #define RDO_SKIP_BS 0
30 #endif
31
32 static const uint8_t intra4x4_cbp_to_golomb[48]=
33 {
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
37 };
38 static const uint8_t inter_cbp_to_golomb[48]=
39 {
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 };
44 static const uint8_t mb_type_b_to_golomb[3][9]=
45 {
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 */
49 };
50 static const uint8_t sub_mb_type_p_to_golomb[4]=
51 {
52     3, 1, 2, 0
53 };
54 static const uint8_t sub_mb_type_b_to_golomb[13]=
55 {
56     10,  4,  5,  1, 11,  6,  7,  2, 12,  8,  9,  3,  0
57 };
58
59 #define bs_write_vlc(s,v) bs_write( s, (v).i_size, (v).i_bits )
60
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 )
65 {
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 )
72     {
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)) );
75     }
76     else
77     {
78         i_level_code -= 15 << i_suffix_length;
79         if( i_suffix_length == 0 )
80             i_level_code -= 15;
81
82         /* If the prefix size exceeds 15, High Profile is required. */
83         if( i_level_code >= 1<<12 )
84         {
85             if( h->sps->i_profile_idc >= PROFILE_HIGH )
86             {
87                 while( i_level_code > 1<<(i_level_prefix-3) )
88                 {
89                     i_level_code -= 1<<(i_level_prefix-3);
90                     i_level_prefix++;
91                 }
92             }
93             else
94             {
95 #if RDO_SKIP_BS
96                 /* Weight highly against overflows. */
97                 s->i_bits_encoded += 1000000;
98 #else
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);
102 #endif
103             }
104         }
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) );
107     }
108     if( i_suffix_length == 0 )
109         i_suffix_length++;
110     if( abs_level > next_suffix[i_suffix_length] )
111         i_suffix_length++;
112     return i_suffix_length;
113 }
114
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 )
116 {
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;
120     int i_total = 0;
121     unsigned int i_sign;
122
123     if( !h->mb.cache.non_zero_count[x264_scan8[i_idx]] )
124     {
125         bs_write_vlc( s, x264_coeff0_token[nC] );
126         return;
127     }
128
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;
135
136     h->mb.cache.non_zero_count[x264_scan8[i_idx]] = i_total;
137
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;
146
147     /* total/trailing */
148     bs_write_vlc( s, x264_coeff_token[nC][i_total*4+i_trailing-4] );
149
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 );
153
154     if( i_trailing < i_total )
155     {
156         int16_t val = runlevel.level[i_trailing];
157         int16_t val_original = runlevel.level[i_trailing]+LEVEL_TABLE_SIZE/2;
158         if( i_trailing < 3 )
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;
161
162         if( (unsigned)val_original < LEVEL_TABLE_SIZE )
163         {
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;
166         }
167         else
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++ )
170         {
171             val = runlevel.level[i] + LEVEL_TABLE_SIZE/2;
172             if( (unsigned)val < LEVEL_TABLE_SIZE )
173             {
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;
176             }
177             else
178                 i_suffix_length = block_residual_write_cavlc_escape( h, s, i_suffix_length, val-LEVEL_TABLE_SIZE/2 );
179         }
180     }
181
182     if( i_total < i_count )
183     {
184         if( i_idx >= 25 )
185             bs_write_vlc( s, x264_total_zeros_dc[i_total-1][i_total_zero] );
186         else
187             bs_write_vlc( s, x264_total_zeros[i_total-1][i_total_zero] );
188     }
189
190     for( i = 0; i < i_total-1 && i_total_zero > 0; i++ )
191     {
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];
195     }
196 }
197
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};
199
200 #define block_residual_write_cavlc(h,s,cat,idx,l,count)\
201 {\
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);\
204 }
205
206 static void cavlc_qp_delta( x264_t *h, bs_t *s )
207 {
208     int i_dqp = h->mb.i_qp - h->mb.i_last_qp;
209
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]] )
213     {
214 #if !RDO_SKIP_BS
215         h->mb.i_qp = h->mb.i_last_qp;
216 #endif
217         i_dqp = 0;
218     }
219
220     if( i_dqp )
221     {
222         if( i_dqp < -26 )
223             i_dqp += 52;
224         else if( i_dqp > 25 )
225             i_dqp -= 52;
226     }
227     bs_write_se( s, i_dqp );
228 }
229
230 static void cavlc_mb_mvd( x264_t *h, bs_t *s, int i_list, int idx, int width )
231 {
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] );
236 }
237
238 static void cavlc_mb8x8_mvd( x264_t *h, bs_t *s, int i_list, int i )
239 {
240     if( !x264_mb_partition_listX_table[i_list][ h->mb.i_sub_partition[i] ] )
241         return;
242
243     switch( h->mb.i_sub_partition[i] )
244     {
245         case D_L0_8x8:
246         case D_L1_8x8:
247         case D_BI_8x8:
248             cavlc_mb_mvd( h, s, i_list, 4*i, 2 );
249             break;
250         case D_L0_8x4:
251         case D_L1_8x4:
252         case D_BI_8x4:
253             cavlc_mb_mvd( h, s, i_list, 4*i+0, 2 );
254             cavlc_mb_mvd( h, s, i_list, 4*i+2, 2 );
255             break;
256         case D_L0_4x8:
257         case D_L1_4x8:
258         case D_BI_4x8:
259             cavlc_mb_mvd( h, s, i_list, 4*i+0, 1 );
260             cavlc_mb_mvd( h, s, i_list, 4*i+1, 1 );
261             break;
262         case D_L0_4x4:
263         case D_L1_4x4:
264         case D_BI_4x4:
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 );
269             break;
270     }
271 }
272
273 static inline void x264_macroblock_luma_write_cavlc( x264_t *h, bs_t *s, int i8start, int i8end )
274 {
275     int i8, i4;
276     if( h->mb.b_transform_8x8 )
277     {
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]] );
282     }
283
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 );
288 }
289
290 /*****************************************************************************
291  * x264_macroblock_write:
292  *****************************************************************************/
293 void x264_macroblock_write_cavlc( x264_t *h, bs_t *s )
294 {
295     const int i_mb_type = h->mb.i_type;
296     int i_mb_i_offset;
297     int i;
298
299 #if !RDO_SKIP_BS
300     const int i_mb_pos_start = bs_pos( s );
301     int       i_mb_pos_tex;
302 #endif
303
304     switch( h->sh.i_type )
305     {
306         case SLICE_TYPE_I:
307             i_mb_i_offset = 0;
308             break;
309         case SLICE_TYPE_P:
310             i_mb_i_offset = 5;
311             break;
312         case SLICE_TYPE_B:
313             i_mb_i_offset = 23;
314             break;
315         default:
316             x264_log(h, X264_LOG_ERROR, "internal error or slice unsupported\n" );
317             return;
318     }
319
320     if( h->sh.b_mbaff
321         && (!(h->mb.i_mb_y & 1) || IS_SKIP(h->mb.type[h->mb.i_mb_xy - h->mb.i_mb_stride])) )
322     {
323         bs_write1( s, h->mb.b_interlaced );
324     }
325
326 #if !RDO_SKIP_BS
327     if( i_mb_type == I_PCM )
328     {
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;
332
333         bs_align_0( s );
334
335         memcpy( s->p, h->mb.pic.p_fenc[0], 256 );
336         s->p += 256;
337         for( i = 0; i < 8; i++ )
338             memcpy( s->p + i*8, h->mb.pic.p_fenc[1] + i*FENC_STRIDE, 8 );
339         s->p += 64;
340         for( i = 0; i < 8; i++ )
341             memcpy( s->p + i*8, h->mb.pic.p_fenc[2] + i*FENC_STRIDE, 8 );
342         s->p += 64;
343
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 );
348
349         h->stat.frame.i_tex_bits += bs_pos(s) - i_mb_pos_tex;
350         return;
351     }
352 #endif
353
354     /* Write:
355       - type
356       - prediction
357       - mv */
358     if( i_mb_type == I_4x4 || i_mb_type == I_8x8 )
359     {
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 );
364
365         /* Prediction: Luma */
366         for( i = 0; i < 16; i += di )
367         {
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]] );
370
371             if( i_pred == i_mode )
372                 bs_write1( s, 1 );  /* b_prev_intra4x4_pred_mode */
373             else
374                 bs_write( s, 4, i_mode - (i_mode > i_pred) );
375         }
376         bs_write_ue( s, x264_mb_pred_mode8x8c_fix[ h->mb.i_chroma_pred_mode ] );
377     }
378     else if( i_mb_type == I_16x16 )
379     {
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 ] );
383     }
384     else if( i_mb_type == P_L0 )
385     {
386         DECLARE_ALIGNED_4( int16_t mvp[2] );
387
388         if( h->mb.i_partition == D_16x16 )
389         {
390             bs_write1( s, 1 );
391
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] );
397         }
398         else if( h->mb.i_partition == D_16x8 )
399         {
400             bs_write_ue( s, 1 );
401             if( h->mb.pic.i_fref[0] > 1 )
402             {
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]] );
405             }
406
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] );
410
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] );
414         }
415         else if( h->mb.i_partition == D_8x16 )
416         {
417             bs_write_ue( s, 2 );
418             if( h->mb.pic.i_fref[0] > 1 )
419             {
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]] );
422             }
423
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] );
427
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] );
431         }
432     }
433     else if( i_mb_type == P_8x8 )
434     {
435         int b_sub_ref;
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 )
438         {
439             bs_write_ue( s, 4 );
440             b_sub_ref = 0;
441         }
442         else
443         {
444             bs_write_ue( s, 3 );
445             b_sub_ref = 1;
446         }
447
448         /* sub mb type */
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] ] );
452         else
453             bs_write( s, 4, 0xf );
454
455         /* ref0 */
456         if( b_sub_ref )
457         {
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]] );
462         }
463
464         for( i = 0; i < 4; i++ )
465             cavlc_mb8x8_mvd( h, s, 0, i );
466     }
467     else if( i_mb_type == B_8x8 )
468     {
469         bs_write_ue( s, 22 );
470
471         /* sub mb type */
472         for( i = 0; i < 4; i++ )
473             bs_write_ue( s, sub_mb_type_b_to_golomb[ h->mb.i_sub_partition[i] ] );
474
475         /* ref */
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]] );
484
485         /* mvd */
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 );
490     }
491     else if( i_mb_type != B_DIRECT )
492     {
493         /* All B mode */
494         /* Motion Vector */
495         int i_list;
496         DECLARE_ALIGNED_4( int16_t mvp[2] );
497         const uint8_t (*b_list)[2] = x264_mb_type_list_table[i_mb_type];
498
499         bs_write_ue( s, mb_type_b_to_golomb[ h->mb.i_partition - D_16x8 ][ i_mb_type - B_L0_L0 ] );
500
501         for( i_list = 0; i_list < 2; i_list++ )
502         {
503             const int i_ref_max = (i_list == 0 ? h->mb.pic.i_fref[0] : h->mb.pic.i_fref[1]) - 1;
504
505             if( i_ref_max )
506                 switch( h->mb.i_partition )
507                 {
508                     case D_16x16:
509                         if( b_list[i_list][0] ) bs_write_te( s, i_ref_max, h->mb.cache.ref[i_list][x264_scan8[0]] );
510                         break;
511                     case D_16x8:
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]] );
514                         break;
515                     case D_8x16:
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]] );
518                         break;
519                 }
520         }
521         for( i_list = 0; i_list < 2; i_list++ )
522         {
523             switch( h->mb.i_partition )
524             {
525                 case D_16x16:
526                     if( b_list[i_list][0] )
527                     {
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] );
531                     }
532                     break;
533                 case D_16x8:
534                     if( b_list[i_list][0] )
535                     {
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] );
539                     }
540                     if( b_list[i_list][1] )
541                     {
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] );
545                     }
546                     break;
547                 case D_8x16:
548                     if( b_list[i_list][0] )
549                     {
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] );
553                     }
554                     if( b_list[i_list][1] )
555                     {
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] );
559                     }
560                     break;
561             }
562         }
563     }
564     else if( i_mb_type == B_DIRECT )
565         bs_write1( s, 1 );
566     else
567     {
568         x264_log(h, X264_LOG_ERROR, "invalid/unhandled mb_type\n" );
569         return;
570     }
571
572 #if !RDO_SKIP_BS
573     i_mb_pos_tex = bs_pos( s );
574     h->stat.frame.i_mv_bits += i_mb_pos_tex - i_mb_pos_start;
575 #endif
576
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] );
582
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 );
586
587     /* write residual */
588     if( i_mb_type == I_16x16 )
589     {
590         cavlc_qp_delta( h, s );
591
592         /* DC Luma */
593         block_residual_write_cavlc( h, s, DCT_LUMA_DC, 24 , h->dct.luma16x16_dc, 16 );
594
595         /* AC Luma */
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 );
599     }
600     else if( h->mb.i_cbp_luma | h->mb.i_cbp_chroma )
601     {
602         cavlc_qp_delta( h, s );
603         x264_macroblock_luma_write_cavlc( h, s, 0, 3 );
604     }
605     if( h->mb.i_cbp_chroma )
606     {
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 );
613     }
614
615 #if !RDO_SKIP_BS
616     h->stat.frame.i_tex_bits += bs_pos(s) - i_mb_pos_tex;
617 #endif
618 }
619
620 #if RDO_SKIP_BS
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 )
629 {
630     bs_t s;
631     const int i_mb_type = h->mb.i_type;
632     int b_8x16 = h->mb.i_partition == D_8x16;
633     int j;
634
635     s.i_bits_encoded = 0;
636
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 )
642     {
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 );
645     }
646     else if( i_mb_type == B_8x8 )
647     {
648         cavlc_mb8x8_mvd( h, &s, 0, i8 );
649         cavlc_mb8x8_mvd( h, &s, 1, i8 );
650     }
651     else
652     {
653         x264_log(h, X264_LOG_ERROR, "invalid/unhandled mb_type\n" );
654         return 0;
655     }
656
657     for( j = (i_pixel < PIXEL_8x8); j >= 0; j-- )
658     {
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;
663     }
664
665     return s.i_bits_encoded;
666 }
667
668 static int x264_subpartition_size_cavlc( x264_t *h, int i4, int i_pixel )
669 {
670     bs_t s;
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 )
676     {
677         i4 += 2-b_8x4;
678         block_residual_write_cavlc( h, &s, DCT_LUMA_4x4, i4, h->dct.luma4x4[i4], 16 );
679     }
680
681     return s.i_bits_encoded;
682 }
683
684 static int cavlc_intra4x4_pred_size( x264_t *h, int i4, int i_mode )
685 {
686     if( x264_mb_predict_intra4x4_mode( h, i4 ) == x264_mb_pred_mode4x4_fix( i_mode ) )
687         return 1;
688     else
689         return 4;
690 }
691
692 static int x264_partition_i8x8_size_cavlc( x264_t *h, int i8, int i_mode )
693 {
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;
698 }
699
700 static int x264_partition_i4x4_size_cavlc( x264_t *h, int i4, int i_mode )
701 {
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;
705 }
706
707 static int x264_i8x8_chroma_size_cavlc( x264_t *h )
708 {
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 )
711     {
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 );
714
715         if( h->mb.i_cbp_chroma == 2 )
716         {
717             int i;
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 );
720         }
721     }
722     return h->out.bs.i_bits_encoded;
723 }
724 #endif