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