]> git.sesse.net Git - x264/blob - encoder/cavlc.c
SATD-based decision for 8x8 transform in inter-MBs.
[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                 x264_log(h, X264_LOG_ERROR, "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 void x264_macroblock_luma_write_cavlc( x264_t *h, bs_t *s )
341 {
342     int i8, i4, i;
343     if( h->mb.b_transform_8x8 )
344     {
345         /* shuffle 8x8 dct coeffs into 4x4 lists */
346         for( i8 = 0; i8 < 4; i8++ )
347             if( h->mb.i_cbp_luma & (1 << i8) )
348                 for( i4 = 0; i4 < 4; i4++ )
349                 {
350                     for( i = 0; i < 16; i++ )
351                         h->dct.block[i4+i8*4].luma4x4[i] = h->dct.luma8x8[i8][i4+i*4];
352                     h->mb.cache.non_zero_count[x264_scan8[i4+i8*4]] =
353                         array_non_zero_count( h->dct.block[i4+i8*4].luma4x4, 16 );
354                 }
355     }
356
357     for( i8 = 0; i8 < 4; i8++ )
358         if( h->mb.i_cbp_luma & (1 << i8) )
359             for( i4 = 0; i4 < 4; i4++ )
360                 block_residual_write_cavlc( h, s, i4+i8*4, h->dct.block[i4+i8*4].luma4x4, 16 );
361 }
362
363 /*****************************************************************************
364  * x264_macroblock_write:
365  *****************************************************************************/
366 void x264_macroblock_write_cavlc( x264_t *h, bs_t *s )
367 {
368     const int i_mb_type = h->mb.i_type;
369     const int i_mb_pos_start = bs_pos( s );
370     int       i_mb_pos_tex;
371     int i_mb_i_offset;
372     int i;
373
374     switch( h->sh.i_type )
375     {
376         case SLICE_TYPE_I:
377             i_mb_i_offset = 0;
378             break;
379         case SLICE_TYPE_P:
380             i_mb_i_offset = 5;
381             break;
382         case SLICE_TYPE_B:
383             i_mb_i_offset = 23;
384             break;
385         default:
386             x264_log(h, X264_LOG_ERROR, "internal error or slice unsupported\n" );
387             return;
388     }
389
390     /* Write:
391       - type
392       - prediction
393       - mv */
394     if( i_mb_type == I_PCM )
395     {
396         /* Untested */
397         bs_write_ue( s, i_mb_i_offset + 25 );
398
399         bs_align_0( s );
400         /* Luma */
401         for( i = 0; i < 16*16; i++ )
402         {
403             const int x = 16 * h->mb.i_mb_x + (i % 16);
404             const int y = 16 * h->mb.i_mb_y + (i / 16);
405             bs_write( s, 8, h->fenc->plane[0][y*h->mb.pic.i_stride[0]+x] );
406         }
407         /* Cb */
408         for( i = 0; i < 8*8; i++ )
409         {
410             const int x = 8 * h->mb.i_mb_x + (i % 8);
411             const int y = 8 * h->mb.i_mb_y + (i / 8);
412             bs_write( s, 8, h->fenc->plane[1][y*h->mb.pic.i_stride[1]+x] );
413         }
414         /* Cr */
415         for( i = 0; i < 8*8; i++ )
416         {
417             const int x = 8 * h->mb.i_mb_x + (i % 8);
418             const int y = 8 * h->mb.i_mb_y + (i / 8);
419             bs_write( s, 8, h->fenc->plane[2][y*h->mb.pic.i_stride[2]+x] );
420         }
421         return;
422     }
423     else if( i_mb_type == I_4x4 || i_mb_type == I_8x8 )
424     {
425         int di = i_mb_type == I_8x8 ? 4 : 1;
426         bs_write_ue( s, i_mb_i_offset + 0 );
427         if( h->pps->b_transform_8x8_mode )
428             bs_write1( s, h->mb.b_transform_8x8 );
429
430         /* Prediction: Luma */
431         for( i = 0; i < 16; i += di )
432         {
433             int i_pred = x264_mb_predict_intra4x4_mode( h, i );
434             int i_mode = h->mb.cache.intra4x4_pred_mode[x264_scan8[i]];
435
436             if( i_pred == i_mode)
437             {
438                 bs_write1( s, 1 );  /* b_prev_intra4x4_pred_mode */
439             }
440             else
441             {
442                 bs_write1( s, 0 );  /* b_prev_intra4x4_pred_mode */
443                 if( i_mode < i_pred )
444                 {
445                     bs_write( s, 3, i_mode );
446                 }
447                 else
448                 {
449                     bs_write( s, 3, i_mode - 1 );
450                 }
451             }
452         }
453         bs_write_ue( s, h->mb.i_chroma_pred_mode );
454     }
455     else if( i_mb_type == I_16x16 )
456     {
457         bs_write_ue( s, i_mb_i_offset + 1 + h->mb.i_intra16x16_pred_mode +
458                         h->mb.i_cbp_chroma * 4 + ( h->mb.i_cbp_luma == 0 ? 0 : 12 ) );
459         bs_write_ue( s, h->mb.i_chroma_pred_mode );
460     }
461     else if( i_mb_type == P_L0 )
462     {
463         int mvp[2];
464
465         if( h->mb.i_partition == D_16x16 )
466         {
467             bs_write_ue( s, 0 );
468
469             if( h->sh.i_num_ref_idx_l0_active > 1 )
470             {
471                 bs_write_te( s, h->sh.i_num_ref_idx_l0_active - 1, h->mb.cache.ref[0][x264_scan8[0]] );
472             }
473             x264_mb_predict_mv( h, 0, 0, 4, mvp );
474             bs_write_se( s, h->mb.cache.mv[0][x264_scan8[0]][0] - mvp[0] );
475             bs_write_se( s, h->mb.cache.mv[0][x264_scan8[0]][1] - mvp[1] );
476         }
477         else if( h->mb.i_partition == D_16x8 )
478         {
479             bs_write_ue( s, 1 );
480             if( h->sh.i_num_ref_idx_l0_active > 1 )
481             {
482                 bs_write_te( s, h->sh.i_num_ref_idx_l0_active - 1, h->mb.cache.ref[0][x264_scan8[0]] );
483                 bs_write_te( s, h->sh.i_num_ref_idx_l0_active - 1, h->mb.cache.ref[0][x264_scan8[8]] );
484             }
485
486             x264_mb_predict_mv( h, 0, 0, 4, mvp );
487             bs_write_se( s, h->mb.cache.mv[0][x264_scan8[0]][0] - mvp[0] );
488             bs_write_se( s, h->mb.cache.mv[0][x264_scan8[0]][1] - mvp[1] );
489
490             x264_mb_predict_mv( h, 0, 8, 4, mvp );
491             bs_write_se( s, h->mb.cache.mv[0][x264_scan8[8]][0] - mvp[0] );
492             bs_write_se( s, h->mb.cache.mv[0][x264_scan8[8]][1] - mvp[1] );
493         }
494         else if( h->mb.i_partition == D_8x16 )
495         {
496             bs_write_ue( s, 2 );
497             if( h->sh.i_num_ref_idx_l0_active > 1 )
498             {
499                 bs_write_te( s, h->sh.i_num_ref_idx_l0_active - 1, h->mb.cache.ref[0][x264_scan8[0]] );
500                 bs_write_te( s, h->sh.i_num_ref_idx_l0_active - 1, h->mb.cache.ref[0][x264_scan8[4]] );
501             }
502
503             x264_mb_predict_mv( h, 0, 0, 2, mvp );
504             bs_write_se( s, h->mb.cache.mv[0][x264_scan8[0]][0] - mvp[0] );
505             bs_write_se( s, h->mb.cache.mv[0][x264_scan8[0]][1] - mvp[1] );
506
507             x264_mb_predict_mv( h, 0, 4, 2, mvp );
508             bs_write_se( s, h->mb.cache.mv[0][x264_scan8[4]][0] - mvp[0] );
509             bs_write_se( s, h->mb.cache.mv[0][x264_scan8[4]][1] - mvp[1] );
510         }
511     }
512     else if( i_mb_type == P_8x8 )
513     {
514         int b_sub_ref0;
515
516         if( h->mb.cache.ref[0][x264_scan8[0]] == 0 && h->mb.cache.ref[0][x264_scan8[4]] == 0 &&
517             h->mb.cache.ref[0][x264_scan8[8]] == 0 && h->mb.cache.ref[0][x264_scan8[12]] == 0 )
518         {
519             bs_write_ue( s, 4 );
520             b_sub_ref0 = 0;
521         }
522         else
523         {
524             bs_write_ue( s, 3 );
525             b_sub_ref0 = 1;
526         }
527         /* sub mb type */
528         for( i = 0; i < 4; i++ )
529         {
530             bs_write_ue( s, sub_mb_type_p_to_golomb[ h->mb.i_sub_partition[i] ] );
531         }
532         /* ref0 */
533         if( h->sh.i_num_ref_idx_l0_active > 1 && b_sub_ref0 )
534         {
535             bs_write_te( s, h->sh.i_num_ref_idx_l0_active - 1, h->mb.cache.ref[0][x264_scan8[0]] );
536             bs_write_te( s, h->sh.i_num_ref_idx_l0_active - 1, h->mb.cache.ref[0][x264_scan8[4]] );
537             bs_write_te( s, h->sh.i_num_ref_idx_l0_active - 1, h->mb.cache.ref[0][x264_scan8[8]] );
538             bs_write_te( s, h->sh.i_num_ref_idx_l0_active - 1, h->mb.cache.ref[0][x264_scan8[12]] );
539         }
540
541         x264_sub_mb_mv_write_cavlc( h, s, 0 );
542     }
543     else if( i_mb_type == B_8x8 )
544     {
545         bs_write_ue( s, 22 );
546
547         /* sub mb type */
548         for( i = 0; i < 4; i++ )
549         {
550             bs_write_ue( s, sub_mb_type_b_to_golomb[ h->mb.i_sub_partition[i] ] );
551         }
552         /* ref */
553         for( i = 0; i < 4; i++ )
554         {
555             if( x264_mb_partition_listX_table[0][ h->mb.i_sub_partition[i] ] )
556             {
557                 bs_write_te( s, h->sh.i_num_ref_idx_l0_active - 1, h->mb.cache.ref[0][x264_scan8[i*4]] );
558             }
559         }
560         for( i = 0; i < 4; i++ )
561         {
562             if( x264_mb_partition_listX_table[1][ h->mb.i_sub_partition[i] ] )
563             {
564                 bs_write_te( s, h->sh.i_num_ref_idx_l1_active - 1, h->mb.cache.ref[1][x264_scan8[i*4]] );
565             }
566         }
567         /* mvd */
568         x264_sub_mb_mv_write_cavlc( h, s, 0 );
569         x264_sub_mb_mv_write_cavlc( h, s, 1 );
570     }
571     else if( i_mb_type != B_DIRECT )
572     {
573         /* All B mode */
574         /* Motion Vector */
575         int i_list;
576         int mvp[2];
577
578         int b_list[2][2];
579
580         /* init ref list utilisations */
581         for( i = 0; i < 2; i++ )
582         {
583             b_list[0][i] = x264_mb_type_list0_table[i_mb_type][i];
584             b_list[1][i] = x264_mb_type_list1_table[i_mb_type][i];
585         }
586
587
588         bs_write_ue( s, mb_type_b_to_golomb[ h->mb.i_partition - D_16x8 ][ i_mb_type - B_L0_L0 ] );
589
590         for( i_list = 0; i_list < 2; i_list++ )
591         {
592             const int i_ref_max = i_list == 0 ? h->sh.i_num_ref_idx_l0_active : h->sh.i_num_ref_idx_l1_active;
593
594             if( i_ref_max > 1 )
595             {
596                 switch( h->mb.i_partition )
597                 {
598                     case D_16x16:
599                         if( b_list[i_list][0] ) bs_write_te( s, i_ref_max - 1, h->mb.cache.ref[i_list][x264_scan8[0]] );
600                         break;
601                     case D_16x8:
602                         if( b_list[i_list][0] ) bs_write_te( s, i_ref_max - 1, h->mb.cache.ref[i_list][x264_scan8[0]] );
603                         if( b_list[i_list][1] ) bs_write_te( s, i_ref_max - 1, h->mb.cache.ref[i_list][x264_scan8[8]] );
604                         break;
605                     case D_8x16:
606                         if( b_list[i_list][0] ) bs_write_te( s, i_ref_max - 1, h->mb.cache.ref[i_list][x264_scan8[0]] );
607                         if( b_list[i_list][1] ) bs_write_te( s, i_ref_max - 1, h->mb.cache.ref[i_list][x264_scan8[4]] );
608                         break;
609                 }
610             }
611         }
612         for( i_list = 0; i_list < 2; i_list++ )
613         {
614             switch( h->mb.i_partition )
615             {
616                 case D_16x16:
617                     if( b_list[i_list][0] )
618                     {
619                         x264_mb_predict_mv( h, i_list, 0, 4, mvp );
620                         bs_write_se( s, h->mb.cache.mv[i_list][x264_scan8[0]][0] - mvp[0] );
621                         bs_write_se( s, h->mb.cache.mv[i_list][x264_scan8[0]][1] - mvp[1] );
622                     }
623                     break;
624                 case D_16x8:
625                     if( b_list[i_list][0] )
626                     {
627                         x264_mb_predict_mv( h, i_list, 0, 4, mvp );
628                         bs_write_se( s, h->mb.cache.mv[i_list][x264_scan8[0]][0] - mvp[0] );
629                         bs_write_se( s, h->mb.cache.mv[i_list][x264_scan8[0]][1] - mvp[1] );
630                     }
631                     if( b_list[i_list][1] )
632                     {
633                         x264_mb_predict_mv( h, i_list, 8, 4, mvp );
634                         bs_write_se( s, h->mb.cache.mv[i_list][x264_scan8[8]][0] - mvp[0] );
635                         bs_write_se( s, h->mb.cache.mv[i_list][x264_scan8[8]][1] - mvp[1] );
636                     }
637                     break;
638                 case D_8x16:
639                     if( b_list[i_list][0] )
640                     {
641                         x264_mb_predict_mv( h, i_list, 0, 2, mvp );
642                         bs_write_se( s, h->mb.cache.mv[i_list][x264_scan8[0]][0] - mvp[0] );
643                         bs_write_se( s, h->mb.cache.mv[i_list][x264_scan8[0]][1] - mvp[1] );
644                     }
645                     if( b_list[i_list][1] )
646                     {
647                         x264_mb_predict_mv( h, i_list, 4, 2, mvp );
648                         bs_write_se( s, h->mb.cache.mv[i_list][x264_scan8[4]][0] - mvp[0] );
649                         bs_write_se( s, h->mb.cache.mv[i_list][x264_scan8[4]][1] - mvp[1] );
650                     }
651                     break;
652             }
653         }
654     }
655     else if( i_mb_type == B_DIRECT )
656     {
657         bs_write_ue( s, 0 );
658     }
659     else
660     {
661         x264_log(h, X264_LOG_ERROR, "invalid/unhandled mb_type\n" );
662         return;
663     }
664
665     i_mb_pos_tex = bs_pos( s );
666     h->stat.frame.i_hdr_bits += i_mb_pos_tex - i_mb_pos_start;
667
668     /* Coded block patern */
669     if( i_mb_type == I_4x4 || i_mb_type == I_8x8 )
670     {
671         bs_write_ue( s, intra4x4_cbp_to_golomb[( h->mb.i_cbp_chroma << 4 )|h->mb.i_cbp_luma] );
672     }
673     else if( i_mb_type != I_16x16 )
674     {
675         bs_write_ue( s, inter_cbp_to_golomb[( h->mb.i_cbp_chroma << 4 )|h->mb.i_cbp_luma] );
676     }
677
678     /* transform size 8x8 flag */
679     if( h->mb.cache.b_transform_8x8_allowed && h->mb.i_cbp_luma && !IS_INTRA(i_mb_type) )
680     {
681         bs_write1( s, h->mb.b_transform_8x8 );
682     }
683
684     /* write residual */
685     if( i_mb_type == I_16x16 )
686     {
687         bs_write_se( s, h->mb.qp[h->mb.i_mb_xy] - h->mb.i_last_qp );
688
689         /* DC Luma */
690         block_residual_write_cavlc( h, s, BLOCK_INDEX_LUMA_DC , h->dct.luma16x16_dc, 16 );
691
692         /* AC Luma */
693         if( h->mb.i_cbp_luma != 0 )
694             for( i = 0; i < 16; i++ )
695                 block_residual_write_cavlc( h, s, i, h->dct.block[i].residual_ac, 15 );
696     }
697     else if( h->mb.i_cbp_luma != 0 || h->mb.i_cbp_chroma != 0 )
698     {
699         bs_write_se( s, h->mb.qp[h->mb.i_mb_xy] - h->mb.i_last_qp );
700         x264_macroblock_luma_write_cavlc( h, s );
701     }
702     if( h->mb.i_cbp_chroma != 0 )
703     {
704         /* Chroma DC residual present */
705         block_residual_write_cavlc( h, s, BLOCK_INDEX_CHROMA_DC, h->dct.chroma_dc[0], 4 );
706         block_residual_write_cavlc( h, s, BLOCK_INDEX_CHROMA_DC, h->dct.chroma_dc[1], 4 );
707         if( h->mb.i_cbp_chroma&0x02 ) /* Chroma AC residual present */
708             for( i = 0; i < 8; i++ )
709                 block_residual_write_cavlc( h, s, 16 + i, h->dct.block[16+i].residual_ac, 15 );
710     }
711
712     if( IS_INTRA( i_mb_type ) )
713         h->stat.frame.i_itex_bits += bs_pos(s) - i_mb_pos_tex;
714     else
715         h->stat.frame.i_ptex_bits += bs_pos(s) - i_mb_pos_tex;
716 }