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