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