]> git.sesse.net Git - x264/blob - encoder/cavlc.c
Fix potential crash in the case that the input statsfile is too short
[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  *
9  * This program is free software; you can redistribute it and/or modify
10  * it under the terms of the GNU General Public License as published by
11  * the Free Software Foundation; either version 2 of the License, or
12  * (at your option) any later version.
13  *
14  * This program is distributed in the hope that it will be useful,
15  * but WITHOUT ANY WARRANTY; without even the implied warranty of
16  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
17  * GNU General Public License for more details.
18  *
19  * You should have received a copy of the GNU General Public License
20  * along with this program; if not, write to the Free Software
21  * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA  02111, USA.
22  *****************************************************************************/
23
24 #include "common/common.h"
25 #include "macroblock.h"
26
27 static const uint8_t intra4x4_cbp_to_golomb[48]=
28 {
29   3, 29, 30, 17, 31, 18, 37,  8, 32, 38, 19,  9, 20, 10, 11,  2,
30  16, 33, 34, 21, 35, 22, 39,  4, 36, 40, 23,  5, 24,  6,  7,  1,
31  41, 42, 43, 25, 44, 26, 46, 12, 45, 47, 27, 13, 28, 14, 15,  0
32 };
33 static const uint8_t inter_cbp_to_golomb[48]=
34 {
35   0,  2,  3,  7,  4,  8, 17, 13,  5, 18,  9, 14, 10, 15, 16, 11,
36   1, 32, 33, 36, 34, 37, 44, 40, 35, 45, 38, 41, 39, 42, 43, 19,
37   6, 24, 25, 20, 26, 21, 46, 28, 27, 47, 22, 29, 23, 30, 31, 12
38 };
39 static const uint8_t mb_type_b_to_golomb[3][9]=
40 {
41     { 4,  8, 12, 10,  6, 14, 16, 18, 20 }, /* D_16x8 */
42     { 5,  9, 13, 11,  7, 15, 17, 19, 21 }, /* D_8x16 */
43     { 1, -1, -1, -1,  2, -1, -1, -1,  3 }  /* D_16x16 */
44 };
45 static const uint8_t sub_mb_type_p_to_golomb[4]=
46 {
47     3, 1, 2, 0
48 };
49 static const uint8_t sub_mb_type_b_to_golomb[13]=
50 {
51     10,  4,  5,  1, 11,  6,  7,  2, 12,  8,  9,  3,  0
52 };
53
54 #define BLOCK_INDEX_CHROMA_DC   (-1)
55 #define BLOCK_INDEX_LUMA_DC     (-2)
56
57 static inline void bs_write_vlc( bs_t *s, vlc_t v )
58 {
59     bs_write( s, v.i_size, v.i_bits );
60 }
61
62 /****************************************************************************
63  * block_residual_write_cavlc:
64  ****************************************************************************/
65 static void block_residual_write_cavlc( x264_t *h, bs_t *s, int i_idx, int16_t *l, int i_count )
66 {
67     int level[16], run[16];
68     int i_total, i_trailing;
69     int i_total_zero;
70     int i_last;
71     unsigned int i_sign;
72     int i;
73     int i_suffix_length;
74
75     /* first find i_last */
76     for( i_last = i_count-1; i_last >= 3; i_last -= 4 )
77         if( *(uint64_t*)(l+i_last-3) )
78             break;
79     while( i_last >= 0 && l[i_last] == 0 )
80         i_last--;
81
82     i_sign = 0;
83     i_total = 0;
84     i_trailing = 0;
85     i_total_zero = i_last + 1;
86
87     if( i_last >= 0 )
88     {
89         int idx = 0;
90
91         /* level and run and total */
92         while( i_last >= 0 )
93         {
94             int r = 0;
95             level[idx] = l[i_last];
96             while( --i_last >= 0 && l[i_last] == 0 )
97                 r++;
98             run[idx++] = r;
99         }
100
101         i_total = idx;
102         i_total_zero -= idx;
103
104         i_trailing = X264_MIN(3, idx);
105         for( idx = 0; idx < i_trailing; idx++ )
106         {
107             if( (unsigned)(level[idx]+1) > 2 )
108             {
109                 i_trailing = idx;
110                 break;
111             }
112             i_sign <<= 1;
113             i_sign |= level[idx] < 0;
114         }
115     }
116
117     /* total/trailing */
118     if( i_idx == BLOCK_INDEX_CHROMA_DC )
119         bs_write_vlc( s, x264_coeff_token[4][i_total*4+i_trailing] );
120     else
121     {
122         /* x264_mb_predict_non_zero_code return 0 <-> (16+16+1)>>1 = 16 */
123         static const int ct_index[17] = {0,0,1,1,2,2,2,2,3,3,3,3,3,3,3,3,3 };
124         int nC = x264_mb_predict_non_zero_code( h, i_idx == BLOCK_INDEX_LUMA_DC ? 0 : i_idx );
125         bs_write_vlc( s, x264_coeff_token[ct_index[nC]][i_total*4+i_trailing] );
126     }
127
128     if( i_total <= 0 )
129         return;
130
131     i_suffix_length = i_total > 10 && i_trailing < 3 ? 1 : 0;
132     if( i_trailing > 0 )
133         bs_write( s, i_trailing, i_sign );
134     for( i = i_trailing; i < i_total; i++ )
135     {
136         int mask = level[i] >> 15;
137         int abs_level = (level[i]^mask)-mask;
138         int i_level_code = abs_level*2-mask-2;
139
140         if( i == i_trailing && i_trailing < 3 )
141             i_level_code -= 2; /* as level[i] can't be 1 for the first one if i_trailing < 3 */
142
143         if( ( i_level_code >> i_suffix_length ) < 14 )
144             bs_write( s, (i_level_code >> i_suffix_length) + 1 + i_suffix_length,
145                      (1<<i_suffix_length) + (i_level_code & ((1<<i_suffix_length)-1)) );
146         else if( i_suffix_length == 0 && i_level_code < 30 )
147             bs_write( s, 19, (1<<4) + (i_level_code - 14) );
148         else if( i_suffix_length > 0 && ( i_level_code >> i_suffix_length ) == 14 )
149             bs_write( s, 15 + i_suffix_length,
150                       (1<<i_suffix_length) + (i_level_code & ((1<<i_suffix_length)-1)) );
151         else
152         {
153             int i_level_prefix = 15;
154             i_level_code -= 15 << i_suffix_length;
155             if( i_suffix_length == 0 )
156                 i_level_code -= 15;
157
158             /* If the prefix size exceeds 15, High Profile is required. */
159             if( i_level_code >= 1<<12 )
160             {
161                 if( h->sps->i_profile_idc >= PROFILE_HIGH )
162                 {
163                     while( i_level_code > 1<<(i_level_prefix-3) )
164                     {
165                         i_level_code -= 1<<(i_level_prefix-3);
166                         i_level_prefix++;
167                     }
168                 }
169                 else
170                 {
171 #ifdef RDO_SKIP_BS
172                     /* Weight highly against overflows. */
173                     s->i_bits_encoded += 1000000;
174 #else
175                     x264_log(h, X264_LOG_WARNING, "OVERFLOW levelcode=%d is only allowed in High Profile", i_level_code );
176                     /* clip level, preserving sign */
177                     i_level_code = (1<<12) - 2 + (i_level_code & 1);
178 #endif
179                 }
180             }
181             bs_write( s, i_level_prefix + 1, 1 );
182             bs_write( s, i_level_prefix - 3, i_level_code & ((1<<(i_level_prefix-3))-1) );
183         }
184
185         if( i_suffix_length == 0 )
186             i_suffix_length++;
187         if( abs_level > (3 << (i_suffix_length-1)) && i_suffix_length < 6 )
188             i_suffix_length++;
189     }
190
191     if( i_total < i_count )
192     {
193         if( i_idx == BLOCK_INDEX_CHROMA_DC )
194             bs_write_vlc( s, x264_total_zeros_dc[i_total-1][i_total_zero] );
195         else
196             bs_write_vlc( s, x264_total_zeros[i_total-1][i_total_zero] );
197     }
198
199     for( i = 0; i < i_total-1 && i_total_zero > 0; i++ )
200     {
201         int i_zl = X264_MIN( i_total_zero - 1, 6 );
202         bs_write_vlc( s, x264_run_before[i_zl][run[i]] );
203         i_total_zero -= run[i];
204     }
205 }
206
207 static void cavlc_qp_delta( x264_t *h, bs_t *s )
208 {
209     int i_dqp = h->mb.i_qp - h->mb.i_last_qp;
210
211     /* Avoid writing a delta quant if we have an empty i16x16 block, e.g. in a completely flat background area */
212     if( h->mb.i_type == I_16x16 && !(h->mb.i_cbp_luma | h->mb.i_cbp_chroma)
213         && !array_non_zero(h->dct.luma16x16_dc) )
214     {
215 #ifndef RDO_SKIP_BS
216         h->mb.i_qp = h->mb.i_last_qp;
217 #endif
218         i_dqp = 0;
219     }
220
221     if( i_dqp )
222     {
223         if( i_dqp < -26 )
224             i_dqp += 52;
225         else if( i_dqp > 25 )
226             i_dqp -= 52;
227     }
228     bs_write_se( s, i_dqp );
229 }
230
231 static void cavlc_mb_mvd( x264_t *h, bs_t *s, int i_list, int idx, int width )
232 {
233     DECLARE_ALIGNED_4( int16_t mvp[2] );
234     x264_mb_predict_mv( h, i_list, idx, width, mvp );
235     bs_write_se( s, h->mb.cache.mv[i_list][x264_scan8[idx]][0] - mvp[0] );
236     bs_write_se( s, h->mb.cache.mv[i_list][x264_scan8[idx]][1] - mvp[1] );
237 }
238
239 static void cavlc_mb8x8_mvd( x264_t *h, bs_t *s, int i_list, int i )
240 {
241     if( !x264_mb_partition_listX_table[i_list][ h->mb.i_sub_partition[i] ] )
242         return;
243
244     switch( h->mb.i_sub_partition[i] )
245     {
246         case D_L0_8x8:
247         case D_L1_8x8:
248         case D_BI_8x8:
249             cavlc_mb_mvd( h, s, i_list, 4*i, 2 );
250             break;
251         case D_L0_8x4:
252         case D_L1_8x4:
253         case D_BI_8x4:
254             cavlc_mb_mvd( h, s, i_list, 4*i+0, 2 );
255             cavlc_mb_mvd( h, s, i_list, 4*i+2, 2 );
256             break;
257         case D_L0_4x8:
258         case D_L1_4x8:
259         case D_BI_4x8:
260             cavlc_mb_mvd( h, s, i_list, 4*i+0, 1 );
261             cavlc_mb_mvd( h, s, i_list, 4*i+1, 1 );
262             break;
263         case D_L0_4x4:
264         case D_L1_4x4:
265         case D_BI_4x4:
266             cavlc_mb_mvd( h, s, i_list, 4*i+0, 1 );
267             cavlc_mb_mvd( h, s, i_list, 4*i+1, 1 );
268             cavlc_mb_mvd( h, s, i_list, 4*i+2, 1 );
269             cavlc_mb_mvd( h, s, i_list, 4*i+3, 1 );
270             break;
271     }
272 }
273
274 static inline void x264_macroblock_luma_write_cavlc( x264_t *h, bs_t *s, int i8start, int i8end )
275 {
276     int i8, i4;
277     if( h->mb.b_transform_8x8 )
278     {
279         /* shuffle 8x8 dct coeffs into 4x4 lists */
280         for( i8 = i8start; i8 <= i8end; i8++ )
281             if( h->mb.i_cbp_luma & (1 << i8) )
282                 h->zigzagf.interleave_8x8_cavlc( h->dct.luma4x4[i8*4], h->dct.luma8x8[i8] );
283     }
284
285     for( i8 = i8start; i8 <= i8end; i8++ )
286         if( h->mb.i_cbp_luma & (1 << i8) )
287             for( i4 = 0; i4 < 4; i4++ )
288             {
289                 h->mb.cache.non_zero_count[x264_scan8[i4+i8*4]] = array_non_zero_count( h->dct.luma4x4[i4+i8*4] );
290                 block_residual_write_cavlc( h, s, i4+i8*4, h->dct.luma4x4[i4+i8*4], 16 );
291             }
292 }
293
294 /*****************************************************************************
295  * x264_macroblock_write:
296  *****************************************************************************/
297 void x264_macroblock_write_cavlc( x264_t *h, bs_t *s )
298 {
299     const int i_mb_type = h->mb.i_type;
300     int i_mb_i_offset;
301     int i;
302
303 #ifndef RDO_SKIP_BS
304     const int i_mb_pos_start = bs_pos( s );
305     int       i_mb_pos_tex;
306 #endif
307
308     switch( h->sh.i_type )
309     {
310         case SLICE_TYPE_I:
311             i_mb_i_offset = 0;
312             break;
313         case SLICE_TYPE_P:
314             i_mb_i_offset = 5;
315             break;
316         case SLICE_TYPE_B:
317             i_mb_i_offset = 23;
318             break;
319         default:
320             x264_log(h, X264_LOG_ERROR, "internal error or slice unsupported\n" );
321             return;
322     }
323
324     if( h->sh.b_mbaff
325         && (!(h->mb.i_mb_y & 1) || IS_SKIP(h->mb.type[h->mb.i_mb_xy - h->mb.i_mb_stride])) )
326     {
327         bs_write1( s, h->mb.b_interlaced );
328     }
329
330 #ifndef RDO_SKIP_BS
331     if( i_mb_type == I_PCM)
332     {
333         bs_write_ue( s, i_mb_i_offset + 25 );
334         i_mb_pos_tex = bs_pos( s );
335         h->stat.frame.i_mv_bits += i_mb_pos_tex - i_mb_pos_start;
336
337         bs_align_0( s );
338
339         memcpy( s->p, h->mb.pic.p_fenc[0], 256 );
340         s->p += 256;
341         for( i = 0; i < 8; i++ )
342             memcpy( s->p + i*8, h->mb.pic.p_fenc[1] + i*FENC_STRIDE, 8 );
343         s->p += 64;
344         for( i = 0; i < 8; i++ )
345             memcpy( s->p + i*8, h->mb.pic.p_fenc[2] + i*FENC_STRIDE, 8 );
346         s->p += 64;
347
348         /* if PCM is chosen, we need to store reconstructed frame data */
349         h->mc.copy[PIXEL_16x16]( h->mb.pic.p_fdec[0], FDEC_STRIDE, h->mb.pic.p_fenc[0], FENC_STRIDE, 16 );
350         h->mc.copy[PIXEL_8x8]  ( h->mb.pic.p_fdec[1], FDEC_STRIDE, h->mb.pic.p_fenc[1], FENC_STRIDE, 8 );
351         h->mc.copy[PIXEL_8x8]  ( h->mb.pic.p_fdec[2], FDEC_STRIDE, h->mb.pic.p_fenc[2], FENC_STRIDE, 8 );
352
353         h->stat.frame.i_tex_bits += bs_pos(s) - i_mb_pos_tex;
354         return;
355     }
356 #endif
357
358     /* Write:
359       - type
360       - prediction
361       - mv */
362     if( i_mb_type == I_4x4 || i_mb_type == I_8x8 )
363     {
364         int di = i_mb_type == I_8x8 ? 4 : 1;
365         bs_write_ue( s, i_mb_i_offset + 0 );
366         if( h->pps->b_transform_8x8_mode )
367             bs_write1( s, h->mb.b_transform_8x8 );
368
369         /* Prediction: Luma */
370         for( i = 0; i < 16; i += di )
371         {
372             int i_pred = x264_mb_predict_intra4x4_mode( h, i );
373             int i_mode = x264_mb_pred_mode4x4_fix( h->mb.cache.intra4x4_pred_mode[x264_scan8[i]] );
374
375             if( i_pred == i_mode )
376                 bs_write1( s, 1 );  /* b_prev_intra4x4_pred_mode */
377             else
378                 bs_write( s, 4, i_mode - (i_mode > i_pred) );
379         }
380         bs_write_ue( s, x264_mb_pred_mode8x8c_fix[ h->mb.i_chroma_pred_mode ] );
381     }
382     else if( i_mb_type == I_16x16 )
383     {
384         bs_write_ue( s, i_mb_i_offset + 1 + x264_mb_pred_mode16x16_fix[h->mb.i_intra16x16_pred_mode] +
385                         h->mb.i_cbp_chroma * 4 + ( h->mb.i_cbp_luma == 0 ? 0 : 12 ) );
386         bs_write_ue( s, x264_mb_pred_mode8x8c_fix[ h->mb.i_chroma_pred_mode ] );
387     }
388     else if( i_mb_type == P_L0 )
389     {
390         DECLARE_ALIGNED_4( int16_t mvp[2] );
391
392         if( h->mb.i_partition == D_16x16 )
393         {
394             bs_write_ue( s, 0 );
395
396             if( h->mb.pic.i_fref[0] > 1 )
397                 bs_write_te( s, h->mb.pic.i_fref[0] - 1, h->mb.cache.ref[0][x264_scan8[0]] );
398             x264_mb_predict_mv( h, 0, 0, 4, mvp );
399             bs_write_se( s, h->mb.cache.mv[0][x264_scan8[0]][0] - mvp[0] );
400             bs_write_se( s, h->mb.cache.mv[0][x264_scan8[0]][1] - mvp[1] );
401         }
402         else if( h->mb.i_partition == D_16x8 )
403         {
404             bs_write_ue( s, 1 );
405             if( h->mb.pic.i_fref[0] > 1 )
406             {
407                 bs_write_te( s, h->mb.pic.i_fref[0] - 1, h->mb.cache.ref[0][x264_scan8[0]] );
408                 bs_write_te( s, h->mb.pic.i_fref[0] - 1, h->mb.cache.ref[0][x264_scan8[8]] );
409             }
410
411             x264_mb_predict_mv( h, 0, 0, 4, mvp );
412             bs_write_se( s, h->mb.cache.mv[0][x264_scan8[0]][0] - mvp[0] );
413             bs_write_se( s, h->mb.cache.mv[0][x264_scan8[0]][1] - mvp[1] );
414
415             x264_mb_predict_mv( h, 0, 8, 4, mvp );
416             bs_write_se( s, h->mb.cache.mv[0][x264_scan8[8]][0] - mvp[0] );
417             bs_write_se( s, h->mb.cache.mv[0][x264_scan8[8]][1] - mvp[1] );
418         }
419         else if( h->mb.i_partition == D_8x16 )
420         {
421             bs_write_ue( s, 2 );
422             if( h->mb.pic.i_fref[0] > 1 )
423             {
424                 bs_write_te( s, h->mb.pic.i_fref[0] - 1, h->mb.cache.ref[0][x264_scan8[0]] );
425                 bs_write_te( s, h->mb.pic.i_fref[0] - 1, h->mb.cache.ref[0][x264_scan8[4]] );
426             }
427
428             x264_mb_predict_mv( h, 0, 0, 2, mvp );
429             bs_write_se( s, h->mb.cache.mv[0][x264_scan8[0]][0] - mvp[0] );
430             bs_write_se( s, h->mb.cache.mv[0][x264_scan8[0]][1] - mvp[1] );
431
432             x264_mb_predict_mv( h, 0, 4, 2, mvp );
433             bs_write_se( s, h->mb.cache.mv[0][x264_scan8[4]][0] - mvp[0] );
434             bs_write_se( s, h->mb.cache.mv[0][x264_scan8[4]][1] - mvp[1] );
435         }
436     }
437     else if( i_mb_type == P_8x8 )
438     {
439         int b_sub_ref0;
440         if( (h->mb.cache.ref[0][x264_scan8[0]] | h->mb.cache.ref[0][x264_scan8[ 4]] |
441              h->mb.cache.ref[0][x264_scan8[8]] | h->mb.cache.ref[0][x264_scan8[12]]) == 0 )
442         {
443             bs_write_ue( s, 4 );
444             b_sub_ref0 = 0;
445         }
446         else
447         {
448             bs_write_ue( s, 3 );
449             b_sub_ref0 = 1;
450         }
451
452         /* sub mb type */
453         if( h->param.analyse.inter & X264_ANALYSE_PSUB8x8 )
454             for( i = 0; i < 4; i++ )
455                 bs_write_ue( s, sub_mb_type_p_to_golomb[ h->mb.i_sub_partition[i] ] );
456         else
457             bs_write( s, 4, 0xf );
458
459         /* ref0 */
460         if( h->mb.pic.i_fref[0] > 1 && b_sub_ref0 )
461         {
462             bs_write_te( s, h->mb.pic.i_fref[0] - 1, h->mb.cache.ref[0][x264_scan8[0]] );
463             bs_write_te( s, h->mb.pic.i_fref[0] - 1, h->mb.cache.ref[0][x264_scan8[4]] );
464             bs_write_te( s, h->mb.pic.i_fref[0] - 1, h->mb.cache.ref[0][x264_scan8[8]] );
465             bs_write_te( s, h->mb.pic.i_fref[0] - 1, h->mb.cache.ref[0][x264_scan8[12]] );
466         }
467
468         for( i = 0; i < 4; i++ )
469             cavlc_mb8x8_mvd( h, s, 0, i );
470     }
471     else if( i_mb_type == B_8x8 )
472     {
473         bs_write_ue( s, 22 );
474
475         /* sub mb type */
476         for( i = 0; i < 4; i++ )
477             bs_write_ue( s, sub_mb_type_b_to_golomb[ h->mb.i_sub_partition[i] ] );
478
479         /* ref */
480         for( i = 0; i < 4; i++ )
481             if( x264_mb_partition_listX_table[0][ h->mb.i_sub_partition[i] ] )
482                 bs_write_te( s, h->mb.pic.i_fref[0] - 1, h->mb.cache.ref[0][x264_scan8[i*4]] );
483         for( i = 0; i < 4; i++ )
484             if( x264_mb_partition_listX_table[1][ h->mb.i_sub_partition[i] ] )
485                 bs_write_te( s, h->mb.pic.i_fref[1] - 1, h->mb.cache.ref[1][x264_scan8[i*4]] );
486
487         /* mvd */
488         for( i = 0; i < 4; i++ )
489             cavlc_mb8x8_mvd( h, s, 0, i );
490         for( i = 0; i < 4; i++ )
491             cavlc_mb8x8_mvd( h, s, 1, i );
492     }
493     else if( i_mb_type != B_DIRECT )
494     {
495         /* All B mode */
496         /* Motion Vector */
497         int i_list;
498         DECLARE_ALIGNED_4( int16_t mvp[2] );
499
500         int b_list[2][2];
501
502         /* init ref list utilisations */
503         for( i = 0; i < 2; i++ )
504         {
505             b_list[0][i] = x264_mb_type_list0_table[i_mb_type][i];
506             b_list[1][i] = x264_mb_type_list1_table[i_mb_type][i];
507         }
508
509         bs_write_ue( s, mb_type_b_to_golomb[ h->mb.i_partition - D_16x8 ][ i_mb_type - B_L0_L0 ] );
510
511         for( i_list = 0; i_list < 2; i_list++ )
512         {
513             const int i_ref_max = (i_list == 0 ? h->mb.pic.i_fref[0] : h->mb.pic.i_fref[1]) - 1;
514
515             if( i_ref_max )
516                 switch( h->mb.i_partition )
517                 {
518                     case D_16x16:
519                         if( b_list[i_list][0] ) bs_write_te( s, i_ref_max, h->mb.cache.ref[i_list][x264_scan8[0]] );
520                         break;
521                     case D_16x8:
522                         if( b_list[i_list][0] ) bs_write_te( s, i_ref_max, h->mb.cache.ref[i_list][x264_scan8[0]] );
523                         if( b_list[i_list][1] ) bs_write_te( s, i_ref_max, h->mb.cache.ref[i_list][x264_scan8[8]] );
524                         break;
525                     case D_8x16:
526                         if( b_list[i_list][0] ) bs_write_te( s, i_ref_max, h->mb.cache.ref[i_list][x264_scan8[0]] );
527                         if( b_list[i_list][1] ) bs_write_te( s, i_ref_max, h->mb.cache.ref[i_list][x264_scan8[4]] );
528                         break;
529                 }
530         }
531         for( i_list = 0; i_list < 2; i_list++ )
532         {
533             switch( h->mb.i_partition )
534             {
535                 case D_16x16:
536                     if( b_list[i_list][0] )
537                     {
538                         x264_mb_predict_mv( h, i_list, 0, 4, mvp );
539                         bs_write_se( s, h->mb.cache.mv[i_list][x264_scan8[0]][0] - mvp[0] );
540                         bs_write_se( s, h->mb.cache.mv[i_list][x264_scan8[0]][1] - mvp[1] );
541                     }
542                     break;
543                 case D_16x8:
544                     if( b_list[i_list][0] )
545                     {
546                         x264_mb_predict_mv( h, i_list, 0, 4, mvp );
547                         bs_write_se( s, h->mb.cache.mv[i_list][x264_scan8[0]][0] - mvp[0] );
548                         bs_write_se( s, h->mb.cache.mv[i_list][x264_scan8[0]][1] - mvp[1] );
549                     }
550                     if( b_list[i_list][1] )
551                     {
552                         x264_mb_predict_mv( h, i_list, 8, 4, mvp );
553                         bs_write_se( s, h->mb.cache.mv[i_list][x264_scan8[8]][0] - mvp[0] );
554                         bs_write_se( s, h->mb.cache.mv[i_list][x264_scan8[8]][1] - mvp[1] );
555                     }
556                     break;
557                 case D_8x16:
558                     if( b_list[i_list][0] )
559                     {
560                         x264_mb_predict_mv( h, i_list, 0, 2, mvp );
561                         bs_write_se( s, h->mb.cache.mv[i_list][x264_scan8[0]][0] - mvp[0] );
562                         bs_write_se( s, h->mb.cache.mv[i_list][x264_scan8[0]][1] - mvp[1] );
563                     }
564                     if( b_list[i_list][1] )
565                     {
566                         x264_mb_predict_mv( h, i_list, 4, 2, mvp );
567                         bs_write_se( s, h->mb.cache.mv[i_list][x264_scan8[4]][0] - mvp[0] );
568                         bs_write_se( s, h->mb.cache.mv[i_list][x264_scan8[4]][1] - mvp[1] );
569                     }
570                     break;
571             }
572         }
573     }
574     else if( i_mb_type == B_DIRECT )
575         bs_write_ue( s, 0 );
576     else
577     {
578         x264_log(h, X264_LOG_ERROR, "invalid/unhandled mb_type\n" );
579         return;
580     }
581
582 #ifndef RDO_SKIP_BS
583     i_mb_pos_tex = bs_pos( s );
584     h->stat.frame.i_mv_bits += i_mb_pos_tex - i_mb_pos_start;
585 #endif
586
587     /* Coded block patern */
588     if( i_mb_type == I_4x4 || i_mb_type == I_8x8 )
589         bs_write_ue( s, intra4x4_cbp_to_golomb[( h->mb.i_cbp_chroma << 4 )|h->mb.i_cbp_luma] );
590     else if( i_mb_type != I_16x16 )
591         bs_write_ue( s, inter_cbp_to_golomb[( h->mb.i_cbp_chroma << 4 )|h->mb.i_cbp_luma] );
592
593     /* transform size 8x8 flag */
594     if( x264_mb_transform_8x8_allowed( h ) && h->mb.i_cbp_luma )
595         bs_write1( s, h->mb.b_transform_8x8 );
596
597     /* write residual */
598     if( i_mb_type == I_16x16 )
599     {
600         cavlc_qp_delta( h, s );
601
602         /* DC Luma */
603         block_residual_write_cavlc( h, s, BLOCK_INDEX_LUMA_DC , h->dct.luma16x16_dc, 16 );
604
605         /* AC Luma */
606         if( h->mb.i_cbp_luma )
607             for( i = 0; i < 16; i++ )
608             {
609                 h->mb.cache.non_zero_count[x264_scan8[i]] = array_non_zero_count( h->dct.luma4x4[i] );
610                 block_residual_write_cavlc( h, s, i, h->dct.luma4x4[i]+1, 15 );
611             }
612     }
613     else if( h->mb.i_cbp_luma | h->mb.i_cbp_chroma )
614     {
615         cavlc_qp_delta( h, s );
616         x264_macroblock_luma_write_cavlc( h, s, 0, 3 );
617     }
618     if( h->mb.i_cbp_chroma )
619     {
620         /* Chroma DC residual present */
621         block_residual_write_cavlc( h, s, BLOCK_INDEX_CHROMA_DC, h->dct.chroma_dc[0], 4 );
622         block_residual_write_cavlc( h, s, BLOCK_INDEX_CHROMA_DC, h->dct.chroma_dc[1], 4 );
623         if( h->mb.i_cbp_chroma&0x02 ) /* Chroma AC residual present */
624             for( i = 16; i < 24; i++ )
625             {
626                 h->mb.cache.non_zero_count[x264_scan8[i]] = array_non_zero_count( h->dct.luma4x4[i] );
627                 block_residual_write_cavlc( h, s, i, h->dct.luma4x4[i]+1, 15 );
628             }
629     }
630
631 #ifndef RDO_SKIP_BS
632     h->stat.frame.i_tex_bits += bs_pos(s) - i_mb_pos_tex;
633 #endif
634 }
635
636 #ifdef RDO_SKIP_BS
637 /*****************************************************************************
638  * RD only; doesn't generate a valid bitstream
639  * doesn't write cbp or chroma dc (I don't know how much this matters)
640  * doesn't write ref or subpartition (never varies between calls, so no point in doing so)
641  * works on all partition sizes except 16x16
642  * for sub8x8, call once per 8x8 block
643  *****************************************************************************/
644 static int x264_partition_size_cavlc( x264_t *h, int i8, int i_pixel )
645 {
646     bs_t s;
647     const int i_mb_type = h->mb.i_type;
648     int b_8x16 = h->mb.i_partition == D_8x16;
649     int j;
650
651     s.i_bits_encoded = 0;
652
653     if( i_mb_type == P_8x8 )
654         cavlc_mb8x8_mvd( h, &s, 0, i8 );
655     else if( i_mb_type == P_L0 )
656         cavlc_mb_mvd( h, &s, 0, 4*i8, 4>>b_8x16 );
657     else if( i_mb_type > B_DIRECT && i_mb_type < B_8x8 )
658     {
659         if( x264_mb_type_list0_table[ i_mb_type ][!!i8] ) cavlc_mb_mvd( h, &s, 0, 4*i8, 4>>b_8x16 );
660         if( x264_mb_type_list1_table[ i_mb_type ][!!i8] ) cavlc_mb_mvd( h, &s, 1, 4*i8, 4>>b_8x16 );
661     }
662     else if( i_mb_type == B_8x8 )
663     {
664         cavlc_mb8x8_mvd( h, &s, 0, i8 );
665         cavlc_mb8x8_mvd( h, &s, 1, i8 );
666     }
667     else
668     {
669         x264_log(h, X264_LOG_ERROR, "invalid/unhandled mb_type\n" );
670         return 0;
671     }
672
673     for( j = (i_pixel < PIXEL_8x8); j >= 0; j-- )
674     {
675         x264_macroblock_luma_write_cavlc( h, &s, i8, i8 );
676         h->mb.cache.non_zero_count[x264_scan8[16+i8]] = array_non_zero_count( h->dct.luma4x4[16+i8] );
677         block_residual_write_cavlc( h, &s, 16+i8, h->dct.luma4x4[16+i8]+1, 15 );
678         h->mb.cache.non_zero_count[x264_scan8[20+i8]] = array_non_zero_count( h->dct.luma4x4[20+i8] );
679         block_residual_write_cavlc( h, &s, 20+i8, h->dct.luma4x4[20+i8]+1, 15 );
680         i8 += x264_pixel_size[i_pixel].h >> 3;
681     }
682
683     return s.i_bits_encoded;
684 }
685
686 static int x264_subpartition_size_cavlc( x264_t *h, int i4, int i_pixel )
687 {
688     bs_t s;
689     int b_8x4 = i_pixel == PIXEL_8x4;
690     s.i_bits_encoded = 0;
691     cavlc_mb_mvd( h, &s, 0, i4, 1+b_8x4 );
692     h->mb.cache.non_zero_count[x264_scan8[i4]] = array_non_zero_count( h->dct.luma4x4[i4] );
693     block_residual_write_cavlc( h, &s, i4, h->dct.luma4x4[i4], 16 );
694     if( i_pixel != PIXEL_4x4 )
695     {
696         i4 += 2-b_8x4;
697         h->mb.cache.non_zero_count[x264_scan8[i4]] = array_non_zero_count( h->dct.luma4x4[i4] );
698         block_residual_write_cavlc( h, &s, i4, h->dct.luma4x4[i4], 16 );
699     }
700
701     return s.i_bits_encoded;
702 }
703
704 static int cavlc_intra4x4_pred_size( x264_t *h, int i4, int i_mode )
705 {
706     if( x264_mb_predict_intra4x4_mode( h, i4 ) == x264_mb_pred_mode4x4_fix( i_mode ) )
707         return 1;
708     else
709         return 4;
710 }
711
712 static int x264_partition_i8x8_size_cavlc( x264_t *h, int i8, int i_mode )
713 {
714     int i4, i;
715     h->out.bs.i_bits_encoded = cavlc_intra4x4_pred_size( h, 4*i8, i_mode );
716     for( i4 = 0; i4 < 4; i4++ )
717     {
718         for( i = 0; i < 16; i++ )
719             h->dct.luma4x4[i4+i8*4][i] = h->dct.luma8x8[i8][i4+i*4];
720         h->mb.cache.non_zero_count[x264_scan8[i4+i8*4]] = array_non_zero_count( h->dct.luma4x4[i4+i8*4] );
721         block_residual_write_cavlc( h, &h->out.bs, i4+i8*4, h->dct.luma4x4[i4+i8*4], 16 );
722     }
723     return h->out.bs.i_bits_encoded;
724 }
725
726 static int x264_partition_i4x4_size_cavlc( x264_t *h, int i4, int i_mode )
727 {
728     h->out.bs.i_bits_encoded = cavlc_intra4x4_pred_size( h, i4, i_mode );
729     h->mb.cache.non_zero_count[x264_scan8[i4]] = array_non_zero_count( h->dct.luma4x4[i4] );
730     block_residual_write_cavlc( h, &h->out.bs, i4, h->dct.luma4x4[i4], 16 );
731     return h->out.bs.i_bits_encoded;
732 }
733
734 static int x264_i8x8_chroma_size_cavlc( x264_t *h )
735 {
736     h->out.bs.i_bits_encoded = bs_size_ue( x264_mb_pred_mode8x8c_fix[ h->mb.i_chroma_pred_mode ] );
737     if( h->mb.i_cbp_chroma )
738     {
739         block_residual_write_cavlc( h, &h->out.bs, BLOCK_INDEX_CHROMA_DC, h->dct.chroma_dc[0], 4 );
740         block_residual_write_cavlc( h, &h->out.bs, BLOCK_INDEX_CHROMA_DC, h->dct.chroma_dc[1], 4 );
741
742         if( h->mb.i_cbp_chroma == 2 )
743         {
744             int i;
745             for( i = 16; i < 24; i++ )
746             {
747                 h->mb.cache.non_zero_count[x264_scan8[i]] = array_non_zero_count( h->dct.luma4x4[i] );
748                 block_residual_write_cavlc( h, &h->out.bs, i, h->dct.luma4x4[i]+1, 15 );
749             }
750         }
751     }
752     return h->out.bs.i_bits_encoded;
753 }
754 #endif