]> git.sesse.net Git - x264/blob - encoder/cavlc.c
Remove some pointless error handling code in cabac/cavlc
[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 int block_residual_write_cavlc( x264_t *h, bs_t *s, int i_ctxBlockCat, int16_t *l, int nC )
116 {
117     static const uint8_t ctz_index[8] = {3,0,1,0,2,0,1,0};
118     static const int count_cat[5] = {16, 15, 16, 4, 15};
119     x264_run_level_t runlevel;
120     int i_trailing, i_total_zero, i_suffix_length, i;
121     int i_total = 0;
122     unsigned int i_sign;
123
124     /* level and run and total */
125     /* set these to 2 to allow branchless i_trailing calculation */
126     runlevel.level[1] = 2;
127     runlevel.level[2] = 2;
128     i_total = h->quantf.coeff_level_run[i_ctxBlockCat]( l, &runlevel );
129     i_total_zero = runlevel.last + 1 - i_total;
130
131     i_trailing = ((((runlevel.level[0]+1) | (1-runlevel.level[0])) >> 31) & 1) // abs(runlevel.level[0])>1
132                | ((((runlevel.level[1]+1) | (1-runlevel.level[1])) >> 31) & 2)
133                | ((((runlevel.level[2]+1) | (1-runlevel.level[2])) >> 31) & 4);
134     i_trailing = ctz_index[i_trailing];
135     i_sign = ((runlevel.level[2] >> 31) & 1)
136            | ((runlevel.level[1] >> 31) & 2)
137            | ((runlevel.level[0] >> 31) & 4);
138     i_sign >>= 3-i_trailing;
139
140     /* total/trailing */
141     bs_write_vlc( s, x264_coeff_token[nC][i_total*4+i_trailing-4] );
142
143     i_suffix_length = i_total > 10 && i_trailing < 3;
144     bs_write( s, i_trailing, i_sign );
145
146     if( i_trailing < i_total )
147     {
148         int16_t val = runlevel.level[i_trailing];
149         int16_t val_original = runlevel.level[i_trailing]+LEVEL_TABLE_SIZE/2;
150         if( i_trailing < 3 )
151             val -= (val>>15)|1; /* as runlevel.level[i] can't be 1 for the first one if i_trailing < 3 */
152         val += LEVEL_TABLE_SIZE/2;
153
154         if( (unsigned)val_original < LEVEL_TABLE_SIZE )
155         {
156             bs_write_vlc( s, x264_level_token[i_suffix_length][val] );
157             i_suffix_length = x264_level_token[i_suffix_length][val_original].i_next;
158         }
159         else
160             i_suffix_length = block_residual_write_cavlc_escape( h, s, i_suffix_length, val-LEVEL_TABLE_SIZE/2 );
161         for( i = i_trailing+1; i < i_total; i++ )
162         {
163             val = runlevel.level[i] + LEVEL_TABLE_SIZE/2;
164             if( (unsigned)val < LEVEL_TABLE_SIZE )
165             {
166                 bs_write_vlc( s, x264_level_token[i_suffix_length][val] );
167                 i_suffix_length = x264_level_token[i_suffix_length][val].i_next;
168             }
169             else
170                 i_suffix_length = block_residual_write_cavlc_escape( h, s, i_suffix_length, val-LEVEL_TABLE_SIZE/2 );
171         }
172     }
173
174     if( i_total < count_cat[i_ctxBlockCat] )
175     {
176         if( i_ctxBlockCat == DCT_CHROMA_DC )
177             bs_write_vlc( s, x264_total_zeros_dc[i_total-1][i_total_zero] );
178         else
179             bs_write_vlc( s, x264_total_zeros[i_total-1][i_total_zero] );
180     }
181
182     for( i = 0; i < i_total-1 && i_total_zero > 0; i++ )
183     {
184         int i_zl = X264_MIN( i_total_zero, 7 );
185         bs_write_vlc( s, x264_run_before[i_zl-1][runlevel.run[i]] );
186         i_total_zero -= runlevel.run[i];
187     }
188
189     return i_total;
190 }
191
192 static const uint8_t ct_index[17] = {0,0,1,1,2,2,2,2,3,3,3,3,3,3,3,3,3};
193
194 #define block_residual_write_cavlc(h,s,cat,idx,l)\
195 {\
196     int nC = cat == DCT_CHROMA_DC ? 4 : ct_index[x264_mb_predict_non_zero_code( h, cat == DCT_LUMA_DC ? 0 : idx )];\
197     uint8_t *nnz = &h->mb.cache.non_zero_count[x264_scan8[idx]];\
198     if( !*nnz )\
199         bs_write_vlc( s, x264_coeff0_token[nC] );\
200     else\
201         *nnz = block_residual_write_cavlc(h,s,cat,l,nC);\
202 }
203
204 static void cavlc_qp_delta( x264_t *h, bs_t *s )
205 {
206     int i_dqp = h->mb.i_qp - h->mb.i_last_qp;
207
208     /* Avoid writing a delta quant if we have an empty i16x16 block, e.g. in a completely flat background area */
209     if( h->mb.i_type == I_16x16 && !(h->mb.i_cbp_luma | h->mb.i_cbp_chroma)
210         && !h->mb.cache.non_zero_count[x264_scan8[24]] )
211     {
212 #if !RDO_SKIP_BS
213         h->mb.i_qp = h->mb.i_last_qp;
214 #endif
215         i_dqp = 0;
216     }
217
218     if( i_dqp )
219     {
220         if( i_dqp < -26 )
221             i_dqp += 52;
222         else if( i_dqp > 25 )
223             i_dqp -= 52;
224     }
225     bs_write_se( s, i_dqp );
226 }
227
228 static void cavlc_mb_mvd( x264_t *h, bs_t *s, int i_list, int idx, int width )
229 {
230     DECLARE_ALIGNED_4( int16_t mvp[2] );
231     x264_mb_predict_mv( h, i_list, idx, width, mvp );
232     bs_write_se( s, h->mb.cache.mv[i_list][x264_scan8[idx]][0] - mvp[0] );
233     bs_write_se( s, h->mb.cache.mv[i_list][x264_scan8[idx]][1] - mvp[1] );
234 }
235
236 static void cavlc_mb8x8_mvd( x264_t *h, bs_t *s, int i_list, int i )
237 {
238     if( !x264_mb_partition_listX_table[i_list][ h->mb.i_sub_partition[i] ] )
239         return;
240
241     switch( h->mb.i_sub_partition[i] )
242     {
243         case D_L0_8x8:
244         case D_L1_8x8:
245         case D_BI_8x8:
246             cavlc_mb_mvd( h, s, i_list, 4*i, 2 );
247             break;
248         case D_L0_8x4:
249         case D_L1_8x4:
250         case D_BI_8x4:
251             cavlc_mb_mvd( h, s, i_list, 4*i+0, 2 );
252             cavlc_mb_mvd( h, s, i_list, 4*i+2, 2 );
253             break;
254         case D_L0_4x8:
255         case D_L1_4x8:
256         case D_BI_4x8:
257             cavlc_mb_mvd( h, s, i_list, 4*i+0, 1 );
258             cavlc_mb_mvd( h, s, i_list, 4*i+1, 1 );
259             break;
260         case D_L0_4x4:
261         case D_L1_4x4:
262         case D_BI_4x4:
263             cavlc_mb_mvd( h, s, i_list, 4*i+0, 1 );
264             cavlc_mb_mvd( h, s, i_list, 4*i+1, 1 );
265             cavlc_mb_mvd( h, s, i_list, 4*i+2, 1 );
266             cavlc_mb_mvd( h, s, i_list, 4*i+3, 1 );
267             break;
268     }
269 }
270
271 static inline void x264_macroblock_luma_write_cavlc( x264_t *h, bs_t *s, int i8start, int i8end )
272 {
273     int i8, i4;
274     if( h->mb.b_transform_8x8 )
275     {
276         /* shuffle 8x8 dct coeffs into 4x4 lists */
277         for( i8 = i8start; i8 <= i8end; i8++ )
278             if( h->mb.i_cbp_luma & (1 << i8) )
279                 h->zigzagf.interleave_8x8_cavlc( h->dct.luma4x4[i8*4], h->dct.luma8x8[i8], &h->mb.cache.non_zero_count[x264_scan8[i8*4]] );
280     }
281
282     for( i8 = i8start; i8 <= i8end; i8++ )
283         if( h->mb.i_cbp_luma & (1 << i8) )
284             for( i4 = 0; i4 < 4; i4++ )
285                 block_residual_write_cavlc( h, s, DCT_LUMA_4x4, i4+i8*4, h->dct.luma4x4[i4+i8*4] );
286 }
287
288 /*****************************************************************************
289  * x264_macroblock_write:
290  *****************************************************************************/
291 void x264_macroblock_write_cavlc( x264_t *h, bs_t *s )
292 {
293     const int i_mb_type = h->mb.i_type;
294     static const int i_offsets[3] = {5,23,0};
295     int i_mb_i_offset = i_offsets[h->sh.i_type];
296     int i;
297
298 #if !RDO_SKIP_BS
299     const int i_mb_pos_start = bs_pos( s );
300     int       i_mb_pos_tex;
301 #endif
302
303     if( h->sh.b_mbaff
304         && (!(h->mb.i_mb_y & 1) || IS_SKIP(h->mb.type[h->mb.i_mb_xy - h->mb.i_mb_stride])) )
305     {
306         bs_write1( s, h->mb.b_interlaced );
307     }
308
309 #if !RDO_SKIP_BS
310     if( i_mb_type == I_PCM )
311     {
312         bs_write_ue( s, i_mb_i_offset + 25 );
313         i_mb_pos_tex = bs_pos( s );
314         h->stat.frame.i_mv_bits += i_mb_pos_tex - i_mb_pos_start;
315
316         bs_align_0( s );
317
318         memcpy( s->p, h->mb.pic.p_fenc[0], 256 );
319         s->p += 256;
320         for( i = 0; i < 8; i++ )
321             memcpy( s->p + i*8, h->mb.pic.p_fenc[1] + i*FENC_STRIDE, 8 );
322         s->p += 64;
323         for( i = 0; i < 8; i++ )
324             memcpy( s->p + i*8, h->mb.pic.p_fenc[2] + i*FENC_STRIDE, 8 );
325         s->p += 64;
326
327         /* if PCM is chosen, we need to store reconstructed frame data */
328         h->mc.copy[PIXEL_16x16]( h->mb.pic.p_fdec[0], FDEC_STRIDE, h->mb.pic.p_fenc[0], FENC_STRIDE, 16 );
329         h->mc.copy[PIXEL_8x8]  ( h->mb.pic.p_fdec[1], FDEC_STRIDE, h->mb.pic.p_fenc[1], FENC_STRIDE, 8 );
330         h->mc.copy[PIXEL_8x8]  ( h->mb.pic.p_fdec[2], FDEC_STRIDE, h->mb.pic.p_fenc[2], FENC_STRIDE, 8 );
331
332         h->stat.frame.i_tex_bits += bs_pos(s) - i_mb_pos_tex;
333         return;
334     }
335 #endif
336
337     /* Write:
338       - type
339       - prediction
340       - mv */
341     if( i_mb_type == I_4x4 || i_mb_type == I_8x8 )
342     {
343         int di = i_mb_type == I_8x8 ? 4 : 1;
344         bs_write_ue( s, i_mb_i_offset + 0 );
345         if( h->pps->b_transform_8x8_mode )
346             bs_write1( s, h->mb.b_transform_8x8 );
347
348         /* Prediction: Luma */
349         for( i = 0; i < 16; i += di )
350         {
351             int i_pred = x264_mb_predict_intra4x4_mode( h, i );
352             int i_mode = x264_mb_pred_mode4x4_fix( h->mb.cache.intra4x4_pred_mode[x264_scan8[i]] );
353
354             if( i_pred == i_mode )
355                 bs_write1( s, 1 );  /* b_prev_intra4x4_pred_mode */
356             else
357                 bs_write( s, 4, i_mode - (i_mode > i_pred) );
358         }
359         bs_write_ue( s, x264_mb_pred_mode8x8c_fix[ h->mb.i_chroma_pred_mode ] );
360     }
361     else if( i_mb_type == I_16x16 )
362     {
363         bs_write_ue( s, i_mb_i_offset + 1 + x264_mb_pred_mode16x16_fix[h->mb.i_intra16x16_pred_mode] +
364                         h->mb.i_cbp_chroma * 4 + ( h->mb.i_cbp_luma == 0 ? 0 : 12 ) );
365         bs_write_ue( s, x264_mb_pred_mode8x8c_fix[ h->mb.i_chroma_pred_mode ] );
366     }
367     else if( i_mb_type == P_L0 )
368     {
369         DECLARE_ALIGNED_4( int16_t mvp[2] );
370
371         if( h->mb.i_partition == D_16x16 )
372         {
373             bs_write1( s, 1 );
374
375             if( h->mb.pic.i_fref[0] > 1 )
376                 bs_write_te( s, h->mb.pic.i_fref[0] - 1, h->mb.cache.ref[0][x264_scan8[0]] );
377             x264_mb_predict_mv( h, 0, 0, 4, mvp );
378             bs_write_se( s, h->mb.cache.mv[0][x264_scan8[0]][0] - mvp[0] );
379             bs_write_se( s, h->mb.cache.mv[0][x264_scan8[0]][1] - mvp[1] );
380         }
381         else if( h->mb.i_partition == D_16x8 )
382         {
383             bs_write_ue( s, 1 );
384             if( h->mb.pic.i_fref[0] > 1 )
385             {
386                 bs_write_te( s, h->mb.pic.i_fref[0] - 1, h->mb.cache.ref[0][x264_scan8[0]] );
387                 bs_write_te( s, h->mb.pic.i_fref[0] - 1, h->mb.cache.ref[0][x264_scan8[8]] );
388             }
389
390             x264_mb_predict_mv( h, 0, 0, 4, mvp );
391             bs_write_se( s, h->mb.cache.mv[0][x264_scan8[0]][0] - mvp[0] );
392             bs_write_se( s, h->mb.cache.mv[0][x264_scan8[0]][1] - mvp[1] );
393
394             x264_mb_predict_mv( h, 0, 8, 4, mvp );
395             bs_write_se( s, h->mb.cache.mv[0][x264_scan8[8]][0] - mvp[0] );
396             bs_write_se( s, h->mb.cache.mv[0][x264_scan8[8]][1] - mvp[1] );
397         }
398         else if( h->mb.i_partition == D_8x16 )
399         {
400             bs_write_ue( s, 2 );
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[4]] );
405             }
406
407             x264_mb_predict_mv( h, 0, 0, 2, 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, 4, 2, mvp );
412             bs_write_se( s, h->mb.cache.mv[0][x264_scan8[4]][0] - mvp[0] );
413             bs_write_se( s, h->mb.cache.mv[0][x264_scan8[4]][1] - mvp[1] );
414         }
415     }
416     else if( i_mb_type == P_8x8 )
417     {
418         int b_sub_ref;
419         if( (h->mb.cache.ref[0][x264_scan8[0]] | h->mb.cache.ref[0][x264_scan8[ 4]] |
420              h->mb.cache.ref[0][x264_scan8[8]] | h->mb.cache.ref[0][x264_scan8[12]]) == 0 )
421         {
422             bs_write_ue( s, 4 );
423             b_sub_ref = 0;
424         }
425         else
426         {
427             bs_write_ue( s, 3 );
428             b_sub_ref = 1;
429         }
430
431         /* sub mb type */
432         if( h->param.analyse.inter & X264_ANALYSE_PSUB8x8 )
433             for( i = 0; i < 4; i++ )
434                 bs_write_ue( s, sub_mb_type_p_to_golomb[ h->mb.i_sub_partition[i] ] );
435         else
436             bs_write( s, 4, 0xf );
437
438         /* ref0 */
439         if( b_sub_ref )
440         {
441             bs_write_te( s, h->mb.pic.i_fref[0] - 1, h->mb.cache.ref[0][x264_scan8[0]] );
442             bs_write_te( s, h->mb.pic.i_fref[0] - 1, h->mb.cache.ref[0][x264_scan8[4]] );
443             bs_write_te( s, h->mb.pic.i_fref[0] - 1, h->mb.cache.ref[0][x264_scan8[8]] );
444             bs_write_te( s, h->mb.pic.i_fref[0] - 1, h->mb.cache.ref[0][x264_scan8[12]] );
445         }
446
447         for( i = 0; i < 4; i++ )
448             cavlc_mb8x8_mvd( h, s, 0, i );
449     }
450     else if( i_mb_type == B_8x8 )
451     {
452         bs_write_ue( s, 22 );
453
454         /* sub mb type */
455         for( i = 0; i < 4; i++ )
456             bs_write_ue( s, sub_mb_type_b_to_golomb[ h->mb.i_sub_partition[i] ] );
457
458         /* ref */
459         if( h->mb.pic.i_fref[0] > 1 )
460             for( i = 0; i < 4; i++ )
461                 if( x264_mb_partition_listX_table[0][ h->mb.i_sub_partition[i] ] )
462                     bs_write_te( s, h->mb.pic.i_fref[0] - 1, h->mb.cache.ref[0][x264_scan8[i*4]] );
463         if( h->mb.pic.i_fref[1] > 1 )
464             for( i = 0; i < 4; i++ )
465                 if( x264_mb_partition_listX_table[1][ h->mb.i_sub_partition[i] ] )
466                     bs_write_te( s, h->mb.pic.i_fref[1] - 1, h->mb.cache.ref[1][x264_scan8[i*4]] );
467
468         /* mvd */
469         for( i = 0; i < 4; i++ )
470             cavlc_mb8x8_mvd( h, s, 0, i );
471         for( i = 0; i < 4; i++ )
472             cavlc_mb8x8_mvd( h, s, 1, i );
473     }
474     else if( i_mb_type != B_DIRECT )
475     {
476         /* All B mode */
477         /* Motion Vector */
478         int i_list;
479         DECLARE_ALIGNED_4( int16_t mvp[2] );
480         const uint8_t (*b_list)[2] = x264_mb_type_list_table[i_mb_type];
481
482         bs_write_ue( s, mb_type_b_to_golomb[ h->mb.i_partition - D_16x8 ][ i_mb_type - B_L0_L0 ] );
483
484         for( i_list = 0; i_list < 2; i_list++ )
485         {
486             const int i_ref_max = (i_list == 0 ? h->mb.pic.i_fref[0] : h->mb.pic.i_fref[1]) - 1;
487
488             if( i_ref_max )
489                 switch( h->mb.i_partition )
490                 {
491                     case D_16x16:
492                         if( b_list[i_list][0] ) bs_write_te( s, i_ref_max, h->mb.cache.ref[i_list][x264_scan8[0]] );
493                         break;
494                     case D_16x8:
495                         if( b_list[i_list][0] ) bs_write_te( s, i_ref_max, h->mb.cache.ref[i_list][x264_scan8[0]] );
496                         if( b_list[i_list][1] ) bs_write_te( s, i_ref_max, h->mb.cache.ref[i_list][x264_scan8[8]] );
497                         break;
498                     case D_8x16:
499                         if( b_list[i_list][0] ) bs_write_te( s, i_ref_max, h->mb.cache.ref[i_list][x264_scan8[0]] );
500                         if( b_list[i_list][1] ) bs_write_te( s, i_ref_max, h->mb.cache.ref[i_list][x264_scan8[4]] );
501                         break;
502                 }
503         }
504         for( i_list = 0; i_list < 2; i_list++ )
505         {
506             switch( h->mb.i_partition )
507             {
508                 case D_16x16:
509                     if( b_list[i_list][0] )
510                     {
511                         x264_mb_predict_mv( h, i_list, 0, 4, mvp );
512                         bs_write_se( s, h->mb.cache.mv[i_list][x264_scan8[0]][0] - mvp[0] );
513                         bs_write_se( s, h->mb.cache.mv[i_list][x264_scan8[0]][1] - mvp[1] );
514                     }
515                     break;
516                 case D_16x8:
517                     if( b_list[i_list][0] )
518                     {
519                         x264_mb_predict_mv( h, i_list, 0, 4, mvp );
520                         bs_write_se( s, h->mb.cache.mv[i_list][x264_scan8[0]][0] - mvp[0] );
521                         bs_write_se( s, h->mb.cache.mv[i_list][x264_scan8[0]][1] - mvp[1] );
522                     }
523                     if( b_list[i_list][1] )
524                     {
525                         x264_mb_predict_mv( h, i_list, 8, 4, mvp );
526                         bs_write_se( s, h->mb.cache.mv[i_list][x264_scan8[8]][0] - mvp[0] );
527                         bs_write_se( s, h->mb.cache.mv[i_list][x264_scan8[8]][1] - mvp[1] );
528                     }
529                     break;
530                 case D_8x16:
531                     if( b_list[i_list][0] )
532                     {
533                         x264_mb_predict_mv( h, i_list, 0, 2, mvp );
534                         bs_write_se( s, h->mb.cache.mv[i_list][x264_scan8[0]][0] - mvp[0] );
535                         bs_write_se( s, h->mb.cache.mv[i_list][x264_scan8[0]][1] - mvp[1] );
536                     }
537                     if( b_list[i_list][1] )
538                     {
539                         x264_mb_predict_mv( h, i_list, 4, 2, mvp );
540                         bs_write_se( s, h->mb.cache.mv[i_list][x264_scan8[4]][0] - mvp[0] );
541                         bs_write_se( s, h->mb.cache.mv[i_list][x264_scan8[4]][1] - mvp[1] );
542                     }
543                     break;
544             }
545         }
546     }
547     else //if( i_mb_type == B_DIRECT )
548         bs_write1( s, 1 );
549
550 #if !RDO_SKIP_BS
551     i_mb_pos_tex = bs_pos( s );
552     h->stat.frame.i_mv_bits += i_mb_pos_tex - i_mb_pos_start;
553 #endif
554
555     /* Coded block patern */
556     if( i_mb_type == I_4x4 || i_mb_type == I_8x8 )
557         bs_write_ue( s, intra4x4_cbp_to_golomb[( h->mb.i_cbp_chroma << 4 )|h->mb.i_cbp_luma] );
558     else if( i_mb_type != I_16x16 )
559         bs_write_ue( s, inter_cbp_to_golomb[( h->mb.i_cbp_chroma << 4 )|h->mb.i_cbp_luma] );
560
561     /* transform size 8x8 flag */
562     if( x264_mb_transform_8x8_allowed( h ) && h->mb.i_cbp_luma )
563         bs_write1( s, h->mb.b_transform_8x8 );
564
565     /* write residual */
566     if( i_mb_type == I_16x16 )
567     {
568         cavlc_qp_delta( h, s );
569
570         /* DC Luma */
571         block_residual_write_cavlc( h, s, DCT_LUMA_DC, 24 , h->dct.luma16x16_dc );
572
573         /* AC Luma */
574         if( h->mb.i_cbp_luma )
575             for( i = 0; i < 16; i++ )
576                 block_residual_write_cavlc( h, s, DCT_LUMA_AC, i, h->dct.luma4x4[i]+1 );
577     }
578     else if( h->mb.i_cbp_luma | h->mb.i_cbp_chroma )
579     {
580         cavlc_qp_delta( h, s );
581         x264_macroblock_luma_write_cavlc( h, s, 0, 3 );
582     }
583     if( h->mb.i_cbp_chroma )
584     {
585         /* Chroma DC residual present */
586         block_residual_write_cavlc( h, s, DCT_CHROMA_DC, 25, h->dct.chroma_dc[0] );
587         block_residual_write_cavlc( h, s, DCT_CHROMA_DC, 26, h->dct.chroma_dc[1] );
588         if( h->mb.i_cbp_chroma&0x02 ) /* Chroma AC residual present */
589             for( i = 16; i < 24; i++ )
590                 block_residual_write_cavlc( h, s, DCT_CHROMA_AC, i, h->dct.luma4x4[i]+1 );
591     }
592
593 #if !RDO_SKIP_BS
594     h->stat.frame.i_tex_bits += bs_pos(s) - i_mb_pos_tex;
595 #endif
596 }
597
598 #if RDO_SKIP_BS
599 /*****************************************************************************
600  * RD only; doesn't generate a valid bitstream
601  * doesn't write cbp or chroma dc (I don't know how much this matters)
602  * doesn't write ref or subpartition (never varies between calls, so no point in doing so)
603  * works on all partition sizes except 16x16
604  * for sub8x8, call once per 8x8 block
605  *****************************************************************************/
606 static int x264_partition_size_cavlc( x264_t *h, int i8, int i_pixel )
607 {
608     const int i_mb_type = h->mb.i_type;
609     int b_8x16 = h->mb.i_partition == D_8x16;
610     int j;
611     h->out.bs.i_bits_encoded = 0;
612
613     if( i_mb_type == P_8x8 )
614         cavlc_mb8x8_mvd( h, &h->out.bs, 0, i8 );
615     else if( i_mb_type == P_L0 )
616         cavlc_mb_mvd( h, &h->out.bs, 0, 4*i8, 4>>b_8x16 );
617     else if( i_mb_type > B_DIRECT && i_mb_type < B_8x8 )
618     {
619         if( x264_mb_type_list_table[ i_mb_type ][0][!!i8] ) cavlc_mb_mvd( h, &h->out.bs, 0, 4*i8, 4>>b_8x16 );
620         if( x264_mb_type_list_table[ i_mb_type ][1][!!i8] ) cavlc_mb_mvd( h, &h->out.bs, 1, 4*i8, 4>>b_8x16 );
621     }
622     else //if( i_mb_type == B_8x8 )
623     {
624         cavlc_mb8x8_mvd( h, &h->out.bs, 0, i8 );
625         cavlc_mb8x8_mvd( h, &h->out.bs, 1, i8 );
626     }
627
628     for( j = (i_pixel < PIXEL_8x8); j >= 0; j-- )
629     {
630         x264_macroblock_luma_write_cavlc( h, &h->out.bs, i8, i8 );
631         block_residual_write_cavlc( h, &h->out.bs, DCT_CHROMA_AC, 16+i8, h->dct.luma4x4[16+i8]+1 );
632         block_residual_write_cavlc( h, &h->out.bs, DCT_CHROMA_AC, 20+i8, h->dct.luma4x4[20+i8]+1 );
633         i8 += x264_pixel_size[i_pixel].h >> 3;
634     }
635
636     return h->out.bs.i_bits_encoded;
637 }
638
639 static int x264_subpartition_size_cavlc( x264_t *h, int i4, int i_pixel )
640 {
641     int b_8x4 = i_pixel == PIXEL_8x4;
642     h->out.bs.i_bits_encoded = 0;
643     cavlc_mb_mvd( h, &h->out.bs, 0, i4, 1+b_8x4 );
644     block_residual_write_cavlc( h, &h->out.bs, DCT_LUMA_4x4, i4, h->dct.luma4x4[i4] );
645     if( i_pixel != PIXEL_4x4 )
646     {
647         i4 += 2-b_8x4;
648         block_residual_write_cavlc( h, &h->out.bs, DCT_LUMA_4x4, i4, h->dct.luma4x4[i4] );
649     }
650
651     return h->out.bs.i_bits_encoded;
652 }
653
654 static int cavlc_intra4x4_pred_size( x264_t *h, int i4, int i_mode )
655 {
656     if( x264_mb_predict_intra4x4_mode( h, i4 ) == x264_mb_pred_mode4x4_fix( i_mode ) )
657         return 1;
658     else
659         return 4;
660 }
661
662 static int x264_partition_i8x8_size_cavlc( x264_t *h, int i8, int i_mode )
663 {
664     h->out.bs.i_bits_encoded = cavlc_intra4x4_pred_size( h, 4*i8, i_mode );
665     bs_write_ue( &h->out.bs, intra4x4_cbp_to_golomb[( h->mb.i_cbp_chroma << 4 )|h->mb.i_cbp_luma] );
666     x264_macroblock_luma_write_cavlc( h, &h->out.bs, i8, i8 );
667     return h->out.bs.i_bits_encoded;
668 }
669
670 static int x264_partition_i4x4_size_cavlc( x264_t *h, int i4, int i_mode )
671 {
672     h->out.bs.i_bits_encoded = cavlc_intra4x4_pred_size( h, i4, i_mode );
673     block_residual_write_cavlc( h, &h->out.bs, DCT_LUMA_4x4, i4, h->dct.luma4x4[i4] );
674     return h->out.bs.i_bits_encoded;
675 }
676
677 static int x264_i8x8_chroma_size_cavlc( x264_t *h )
678 {
679     h->out.bs.i_bits_encoded = bs_size_ue( x264_mb_pred_mode8x8c_fix[ h->mb.i_chroma_pred_mode ] );
680     if( h->mb.i_cbp_chroma )
681     {
682         block_residual_write_cavlc( h, &h->out.bs, DCT_CHROMA_DC, 25, h->dct.chroma_dc[0] );
683         block_residual_write_cavlc( h, &h->out.bs, DCT_CHROMA_DC, 26, h->dct.chroma_dc[1] );
684
685         if( h->mb.i_cbp_chroma == 2 )
686         {
687             int i;
688             for( i = 16; i < 24; i++ )
689                 block_residual_write_cavlc( h, &h->out.bs, DCT_CHROMA_AC, i, h->dct.luma4x4[i]+1 );
690         }
691     }
692     return h->out.bs.i_bits_encoded;
693 }
694 #endif