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