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