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