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