]> git.sesse.net Git - x264/blob - encoder/cavlc.c
Fix bug in intra analysis in B-frames
[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  *          Fiona Glaser <fiona@x264.com>
9  *
10  * This program is free software; you can redistribute it and/or modify
11  * it under the terms of the GNU General Public License as published by
12  * the Free Software Foundation; either version 2 of the License, or
13  * (at your option) any later version.
14  *
15  * This program is distributed in the hope that it will be useful,
16  * but WITHOUT ANY WARRANTY; without even the implied warranty of
17  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
18  * GNU General Public License for more details.
19  *
20  * You should have received a copy of the GNU General Public License
21  * along with this program; if not, write to the Free Software
22  * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA  02111, USA.
23  *****************************************************************************/
24
25 #include "common/common.h"
26 #include "macroblock.h"
27
28 #ifndef RDO_SKIP_BS
29 #define RDO_SKIP_BS 0
30 #endif
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 bs_write_vlc(s,v) bs_write( s, (v).i_size, (v).i_bits )
60
61 /****************************************************************************
62  * block_residual_write_cavlc:
63  ****************************************************************************/
64 static inline int block_residual_write_cavlc_escape( x264_t *h, bs_t *s, int i_suffix_length, int level )
65 {
66     static const uint16_t next_suffix[7] = { 0, 3, 6, 12, 24, 48, 0xffff };
67     int i_level_prefix = 15;
68     int mask = level >> 15;
69     int abs_level = (level^mask)-mask;
70     int i_level_code = abs_level*2-mask-2;
71     if( ( i_level_code >> i_suffix_length ) < 15 )
72     {
73         bs_write( s, (i_level_code >> i_suffix_length) + 1 + i_suffix_length,
74                  (1<<i_suffix_length) + (i_level_code & ((1<<i_suffix_length)-1)) );
75     }
76     else
77     {
78         i_level_code -= 15 << i_suffix_length;
79         if( i_suffix_length == 0 )
80             i_level_code -= 15;
81
82         /* If the prefix size exceeds 15, High Profile is required. */
83         if( i_level_code >= 1<<12 )
84         {
85             if( h->sps->i_profile_idc >= PROFILE_HIGH )
86             {
87                 while( i_level_code > 1<<(i_level_prefix-3) )
88                 {
89                     i_level_code -= 1<<(i_level_prefix-3);
90                     i_level_prefix++;
91                 }
92             }
93             else
94             {
95 #if RDO_SKIP_BS
96                 /* Weight highly against overflows. */
97                 s->i_bits_encoded += 1000000;
98 #else
99                 x264_log(h, X264_LOG_WARNING, "OVERFLOW levelcode=%d is only allowed in High Profile\n", i_level_code );
100                 /* clip level, preserving sign */
101                 i_level_code = (1<<12) - 2 + (i_level_code & 1);
102 #endif
103             }
104         }
105         bs_write( s, i_level_prefix + 1, 1 );
106         bs_write( s, i_level_prefix - 3, i_level_code & ((1<<(i_level_prefix-3))-1) );
107     }
108     if( i_suffix_length == 0 )
109         i_suffix_length++;
110     if( abs_level > next_suffix[i_suffix_length] )
111         i_suffix_length++;
112     return i_suffix_length;
113 }
114
115 static int block_residual_write_cavlc( x264_t *h, bs_t *s, int i_ctxBlockCat, int16_t *l, int nC )
116 {
117     static const uint8_t ctz_index[8] = {3,0,1,0,2,0,1,0};
118     static const int count_cat[5] = {16, 15, 16, 4, 15};
119     x264_run_level_t runlevel;
120     int i_trailing, i_total_zero, i_suffix_length, i;
121     int i_total = 0;
122     unsigned int i_sign;
123
124     /* level and run and total */
125     /* set these to 2 to allow branchless i_trailing calculation */
126     runlevel.level[1] = 2;
127     runlevel.level[2] = 2;
128     i_total = h->quantf.coeff_level_run[i_ctxBlockCat]( l, &runlevel );
129     i_total_zero = runlevel.last + 1 - i_total;
130
131     i_trailing = ((((runlevel.level[0]+1) | (1-runlevel.level[0])) >> 31) & 1) // abs(runlevel.level[0])>1
132                | ((((runlevel.level[1]+1) | (1-runlevel.level[1])) >> 31) & 2)
133                | ((((runlevel.level[2]+1) | (1-runlevel.level[2])) >> 31) & 4);
134     i_trailing = ctz_index[i_trailing];
135     i_sign = ((runlevel.level[2] >> 31) & 1)
136            | ((runlevel.level[1] >> 31) & 2)
137            | ((runlevel.level[0] >> 31) & 4);
138     i_sign >>= 3-i_trailing;
139
140     /* total/trailing */
141     bs_write_vlc( s, x264_coeff_token[nC][i_total*4+i_trailing-4] );
142
143     i_suffix_length = i_total > 10 && i_trailing < 3;
144     bs_write( s, i_trailing, i_sign );
145
146     if( i_trailing < i_total )
147     {
148         int16_t val = runlevel.level[i_trailing];
149         int16_t val_original = runlevel.level[i_trailing]+LEVEL_TABLE_SIZE/2;
150         if( i_trailing < 3 )
151             val -= (val>>15)|1; /* as runlevel.level[i] can't be 1 for the first one if i_trailing < 3 */
152         val += LEVEL_TABLE_SIZE/2;
153
154         if( (unsigned)val_original < LEVEL_TABLE_SIZE )
155         {
156             bs_write_vlc( s, x264_level_token[i_suffix_length][val] );
157             i_suffix_length = x264_level_token[i_suffix_length][val_original].i_next;
158         }
159         else
160             i_suffix_length = block_residual_write_cavlc_escape( h, s, i_suffix_length, val-LEVEL_TABLE_SIZE/2 );
161         for( i = i_trailing+1; i < i_total; i++ )
162         {
163             val = runlevel.level[i] + LEVEL_TABLE_SIZE/2;
164             if( (unsigned)val < LEVEL_TABLE_SIZE )
165             {
166                 bs_write_vlc( s, x264_level_token[i_suffix_length][val] );
167                 i_suffix_length = x264_level_token[i_suffix_length][val].i_next;
168             }
169             else
170                 i_suffix_length = block_residual_write_cavlc_escape( h, s, i_suffix_length, val-LEVEL_TABLE_SIZE/2 );
171         }
172     }
173
174     if( i_total < count_cat[i_ctxBlockCat] )
175     {
176         if( i_ctxBlockCat == DCT_CHROMA_DC )
177             bs_write_vlc( s, x264_total_zeros_dc[i_total-1][i_total_zero] );
178         else
179             bs_write_vlc( s, x264_total_zeros[i_total-1][i_total_zero] );
180     }
181
182     for( i = 0; i < i_total-1 && i_total_zero > 0; i++ )
183     {
184         int i_zl = X264_MIN( i_total_zero, 7 );
185         bs_write_vlc( s, x264_run_before[i_zl-1][runlevel.run[i]] );
186         i_total_zero -= runlevel.run[i];
187     }
188
189     return i_total;
190 }
191
192 static const uint8_t ct_index[17] = {0,0,1,1,2,2,2,2,3,3,3,3,3,3,3,3,3};
193
194 #define block_residual_write_cavlc(h,s,cat,idx,l)\
195 {\
196     int nC = cat == DCT_CHROMA_DC ? 4 : ct_index[x264_mb_predict_non_zero_code( h, cat == DCT_LUMA_DC ? 0 : idx )];\
197     uint8_t *nnz = &h->mb.cache.non_zero_count[x264_scan8[idx]];\
198     if( !*nnz )\
199         bs_write_vlc( s, x264_coeff0_token[nC] );\
200     else\
201         *nnz = block_residual_write_cavlc(h,s,cat,l,nC);\
202 }
203
204 static void cavlc_qp_delta( x264_t *h, bs_t *s )
205 {
206     int i_dqp = h->mb.i_qp - h->mb.i_last_qp;
207
208     /* Avoid writing a delta quant if we have an empty i16x16 block, e.g. in a completely flat background area */
209     if( h->mb.i_type == I_16x16 && !(h->mb.i_cbp_luma | h->mb.i_cbp_chroma)
210         && !h->mb.cache.non_zero_count[x264_scan8[24]] )
211     {
212 #if !RDO_SKIP_BS
213         h->mb.i_qp = h->mb.i_last_qp;
214 #endif
215         i_dqp = 0;
216     }
217
218     if( i_dqp )
219     {
220         if( i_dqp < -26 )
221             i_dqp += 52;
222         else if( i_dqp > 25 )
223             i_dqp -= 52;
224     }
225     bs_write_se( s, i_dqp );
226 }
227
228 static void cavlc_mb_mvd( x264_t *h, bs_t *s, int i_list, int idx, int width )
229 {
230     ALIGNED_4( int16_t mvp[2] );
231     x264_mb_predict_mv( h, i_list, idx, width, mvp );
232     bs_write_se( s, h->mb.cache.mv[i_list][x264_scan8[idx]][0] - mvp[0] );
233     bs_write_se( s, h->mb.cache.mv[i_list][x264_scan8[idx]][1] - mvp[1] );
234 }
235
236 static inline void cavlc_mb8x8_mvd( x264_t *h, bs_t *s, int i )
237 {
238     switch( h->mb.i_sub_partition[i] )
239     {
240         case D_L0_8x8:
241             cavlc_mb_mvd( h, s, 0, 4*i, 2 );
242             break;
243         case D_L0_8x4:
244             cavlc_mb_mvd( h, s, 0, 4*i+0, 2 );
245             cavlc_mb_mvd( h, s, 0, 4*i+2, 2 );
246             break;
247         case D_L0_4x8:
248             cavlc_mb_mvd( h, s, 0, 4*i+0, 1 );
249             cavlc_mb_mvd( h, s, 0, 4*i+1, 1 );
250             break;
251         case D_L0_4x4:
252             cavlc_mb_mvd( h, s, 0, 4*i+0, 1 );
253             cavlc_mb_mvd( h, s, 0, 4*i+1, 1 );
254             cavlc_mb_mvd( h, s, 0, 4*i+2, 1 );
255             cavlc_mb_mvd( h, s, 0, 4*i+3, 1 );
256             break;
257     }
258 }
259
260 static inline void x264_macroblock_luma_write_cavlc( x264_t *h, bs_t *s, int i8start, int i8end )
261 {
262     int i8, i4;
263     if( h->mb.b_transform_8x8 )
264     {
265         /* shuffle 8x8 dct coeffs into 4x4 lists */
266         for( i8 = i8start; i8 <= i8end; i8++ )
267             if( h->mb.i_cbp_luma & (1 << i8) )
268                 h->zigzagf.interleave_8x8_cavlc( h->dct.luma4x4[i8*4], h->dct.luma8x8[i8], &h->mb.cache.non_zero_count[x264_scan8[i8*4]] );
269     }
270
271     for( i8 = i8start; i8 <= i8end; i8++ )
272         if( h->mb.i_cbp_luma & (1 << i8) )
273             for( i4 = 0; i4 < 4; i4++ )
274                 block_residual_write_cavlc( h, s, DCT_LUMA_4x4, i4+i8*4, h->dct.luma4x4[i4+i8*4] );
275 }
276
277 /*****************************************************************************
278  * x264_macroblock_write:
279  *****************************************************************************/
280 void x264_macroblock_write_cavlc( x264_t *h, bs_t *s )
281 {
282     const int i_mb_type = h->mb.i_type;
283     static const int i_offsets[3] = {5,23,0};
284     int i_mb_i_offset = i_offsets[h->sh.i_type];
285     int i;
286
287 #if !RDO_SKIP_BS
288     const int i_mb_pos_start = bs_pos( s );
289     int       i_mb_pos_tex;
290 #endif
291
292     if( h->sh.b_mbaff
293         && (!(h->mb.i_mb_y & 1) || IS_SKIP(h->mb.type[h->mb.i_mb_xy - h->mb.i_mb_stride])) )
294     {
295         bs_write1( s, h->mb.b_interlaced );
296     }
297
298 #if !RDO_SKIP_BS
299     if( i_mb_type == I_PCM )
300     {
301         uint8_t *p_start = s->p_start;
302         bs_write_ue( s, i_mb_i_offset + 25 );
303         i_mb_pos_tex = bs_pos( s );
304         h->stat.frame.i_mv_bits += i_mb_pos_tex - i_mb_pos_start;
305
306         bs_align_0( s );
307
308         memcpy( s->p, h->mb.pic.p_fenc[0], 256 );
309         s->p += 256;
310         for( i = 0; i < 8; i++ )
311             memcpy( s->p + i*8, h->mb.pic.p_fenc[1] + i*FENC_STRIDE, 8 );
312         s->p += 64;
313         for( i = 0; i < 8; i++ )
314             memcpy( s->p + i*8, h->mb.pic.p_fenc[2] + i*FENC_STRIDE, 8 );
315         s->p += 64;
316
317         bs_init( s, s->p, s->p_end - s->p );
318         s->p_start = p_start;
319
320         /* if PCM is chosen, we need to store reconstructed frame data */
321         h->mc.copy[PIXEL_16x16]( h->mb.pic.p_fdec[0], FDEC_STRIDE, h->mb.pic.p_fenc[0], FENC_STRIDE, 16 );
322         h->mc.copy[PIXEL_8x8]  ( h->mb.pic.p_fdec[1], FDEC_STRIDE, h->mb.pic.p_fenc[1], FENC_STRIDE, 8 );
323         h->mc.copy[PIXEL_8x8]  ( h->mb.pic.p_fdec[2], FDEC_STRIDE, h->mb.pic.p_fenc[2], FENC_STRIDE, 8 );
324
325         h->stat.frame.i_tex_bits += bs_pos(s) - i_mb_pos_tex;
326         return;
327     }
328 #endif
329
330     /* Write:
331       - type
332       - prediction
333       - mv */
334     if( i_mb_type == I_4x4 || i_mb_type == I_8x8 )
335     {
336         int di = i_mb_type == I_8x8 ? 4 : 1;
337         bs_write_ue( s, i_mb_i_offset + 0 );
338         if( h->pps->b_transform_8x8_mode )
339             bs_write1( s, h->mb.b_transform_8x8 );
340
341         /* Prediction: Luma */
342         for( i = 0; i < 16; i += di )
343         {
344             int i_pred = x264_mb_predict_intra4x4_mode( h, i );
345             int i_mode = x264_mb_pred_mode4x4_fix( h->mb.cache.intra4x4_pred_mode[x264_scan8[i]] );
346
347             if( i_pred == i_mode )
348                 bs_write1( s, 1 );  /* b_prev_intra4x4_pred_mode */
349             else
350                 bs_write( s, 4, i_mode - (i_mode > i_pred) );
351         }
352         bs_write_ue( s, x264_mb_pred_mode8x8c_fix[ h->mb.i_chroma_pred_mode ] );
353     }
354     else if( i_mb_type == I_16x16 )
355     {
356         bs_write_ue( s, i_mb_i_offset + 1 + x264_mb_pred_mode16x16_fix[h->mb.i_intra16x16_pred_mode] +
357                         h->mb.i_cbp_chroma * 4 + ( h->mb.i_cbp_luma == 0 ? 0 : 12 ) );
358         bs_write_ue( s, x264_mb_pred_mode8x8c_fix[ h->mb.i_chroma_pred_mode ] );
359     }
360     else if( i_mb_type == P_L0 )
361     {
362         if( h->mb.i_partition == D_16x16 )
363         {
364             bs_write1( s, 1 );
365
366             if( h->mb.pic.i_fref[0] > 1 )
367                 bs_write_te( s, h->mb.pic.i_fref[0] - 1, h->mb.cache.ref[0][x264_scan8[0]] );
368             cavlc_mb_mvd( h, s, 0, 0, 4 );
369         }
370         else if( h->mb.i_partition == D_16x8 )
371         {
372             bs_write_ue( s, 1 );
373             if( h->mb.pic.i_fref[0] > 1 )
374             {
375                 bs_write_te( s, h->mb.pic.i_fref[0] - 1, h->mb.cache.ref[0][x264_scan8[0]] );
376                 bs_write_te( s, h->mb.pic.i_fref[0] - 1, h->mb.cache.ref[0][x264_scan8[8]] );
377             }
378             cavlc_mb_mvd( h, s, 0, 0, 4 );
379             cavlc_mb_mvd( h, s, 0, 8, 4 );
380         }
381         else if( h->mb.i_partition == D_8x16 )
382         {
383             bs_write_ue( s, 2 );
384             if( h->mb.pic.i_fref[0] > 1 )
385             {
386                 bs_write_te( s, h->mb.pic.i_fref[0] - 1, h->mb.cache.ref[0][x264_scan8[0]] );
387                 bs_write_te( s, h->mb.pic.i_fref[0] - 1, h->mb.cache.ref[0][x264_scan8[4]] );
388             }
389             cavlc_mb_mvd( h, s, 0, 0, 2 );
390             cavlc_mb_mvd( h, s, 0, 4, 2 );
391         }
392     }
393     else if( i_mb_type == P_8x8 )
394     {
395         int b_sub_ref;
396         if( (h->mb.cache.ref[0][x264_scan8[0]] | h->mb.cache.ref[0][x264_scan8[ 4]] |
397              h->mb.cache.ref[0][x264_scan8[8]] | h->mb.cache.ref[0][x264_scan8[12]]) == 0 )
398         {
399             bs_write_ue( s, 4 );
400             b_sub_ref = 0;
401         }
402         else
403         {
404             bs_write_ue( s, 3 );
405             b_sub_ref = 1;
406         }
407
408         /* sub mb type */
409         if( h->param.analyse.inter & X264_ANALYSE_PSUB8x8 )
410             for( i = 0; i < 4; i++ )
411                 bs_write_ue( s, sub_mb_type_p_to_golomb[ h->mb.i_sub_partition[i] ] );
412         else
413             bs_write( s, 4, 0xf );
414
415         /* ref0 */
416         if( b_sub_ref )
417         {
418             bs_write_te( s, h->mb.pic.i_fref[0] - 1, h->mb.cache.ref[0][x264_scan8[0]] );
419             bs_write_te( s, h->mb.pic.i_fref[0] - 1, h->mb.cache.ref[0][x264_scan8[4]] );
420             bs_write_te( s, h->mb.pic.i_fref[0] - 1, h->mb.cache.ref[0][x264_scan8[8]] );
421             bs_write_te( s, h->mb.pic.i_fref[0] - 1, h->mb.cache.ref[0][x264_scan8[12]] );
422         }
423
424         for( i = 0; i < 4; i++ )
425             cavlc_mb8x8_mvd( h, s, i );
426     }
427     else if( i_mb_type == B_8x8 )
428     {
429         bs_write_ue( s, 22 );
430
431         /* sub mb type */
432         for( i = 0; i < 4; i++ )
433             bs_write_ue( s, sub_mb_type_b_to_golomb[ h->mb.i_sub_partition[i] ] );
434
435         /* ref */
436         if( h->mb.pic.i_fref[0] > 1 )
437             for( i = 0; i < 4; i++ )
438                 if( x264_mb_partition_listX_table[0][ h->mb.i_sub_partition[i] ] )
439                     bs_write_te( s, h->mb.pic.i_fref[0] - 1, h->mb.cache.ref[0][x264_scan8[i*4]] );
440         if( h->mb.pic.i_fref[1] > 1 )
441             for( i = 0; i < 4; i++ )
442                 if( x264_mb_partition_listX_table[1][ h->mb.i_sub_partition[i] ] )
443                     bs_write_te( s, h->mb.pic.i_fref[1] - 1, h->mb.cache.ref[1][x264_scan8[i*4]] );
444
445         /* mvd */
446         for( i = 0; i < 4; i++ )
447             if( x264_mb_partition_listX_table[0][ h->mb.i_sub_partition[i] ] )
448                 cavlc_mb_mvd( h, s, 0, 4*i, 2 );
449         for( i = 0; i < 4; i++ )
450             if( x264_mb_partition_listX_table[1][ h->mb.i_sub_partition[i] ] )
451                 cavlc_mb_mvd( h, s, 1, 4*i, 2 );
452     }
453     else if( i_mb_type != B_DIRECT )
454     {
455         /* All B mode */
456         /* Motion Vector */
457         int i_list;
458         const uint8_t (*b_list)[2] = x264_mb_type_list_table[i_mb_type];
459
460         bs_write_ue( s, mb_type_b_to_golomb[ h->mb.i_partition - D_16x8 ][ i_mb_type - B_L0_L0 ] );
461
462         for( i_list = 0; i_list < 2; i_list++ )
463         {
464             const int i_ref_max = (i_list == 0 ? h->mb.pic.i_fref[0] : h->mb.pic.i_fref[1]) - 1;
465
466             if( i_ref_max )
467                 switch( h->mb.i_partition )
468                 {
469                     case D_16x16:
470                         if( b_list[i_list][0] ) bs_write_te( s, i_ref_max, h->mb.cache.ref[i_list][x264_scan8[0]] );
471                         break;
472                     case D_16x8:
473                         if( b_list[i_list][0] ) bs_write_te( s, i_ref_max, h->mb.cache.ref[i_list][x264_scan8[0]] );
474                         if( b_list[i_list][1] ) bs_write_te( s, i_ref_max, h->mb.cache.ref[i_list][x264_scan8[8]] );
475                         break;
476                     case D_8x16:
477                         if( b_list[i_list][0] ) bs_write_te( s, i_ref_max, h->mb.cache.ref[i_list][x264_scan8[0]] );
478                         if( b_list[i_list][1] ) bs_write_te( s, i_ref_max, h->mb.cache.ref[i_list][x264_scan8[4]] );
479                         break;
480                 }
481         }
482         for( i_list = 0; i_list < 2; i_list++ )
483         {
484             switch( h->mb.i_partition )
485             {
486                 case D_16x16:
487                     if( b_list[i_list][0] )
488                         cavlc_mb_mvd( h, s, i_list, 0, 4 );
489                     break;
490                 case D_16x8:
491                     if( b_list[i_list][0] )
492                         cavlc_mb_mvd( h, s, i_list, 0, 4 );
493                     if( b_list[i_list][1] )
494                         cavlc_mb_mvd( h, s, i_list, 8, 4 );
495                     break;
496                 case D_8x16:
497                     if( b_list[i_list][0] )
498                         cavlc_mb_mvd( h, s, i_list, 0, 2 );
499                     if( b_list[i_list][1] )
500                         cavlc_mb_mvd( h, s, i_list, 4, 2 );
501             }
502         }
503     }
504     else //if( i_mb_type == B_DIRECT )
505         bs_write1( s, 1 );
506
507 #if !RDO_SKIP_BS
508     i_mb_pos_tex = bs_pos( s );
509     h->stat.frame.i_mv_bits += i_mb_pos_tex - i_mb_pos_start;
510 #endif
511
512     /* Coded block patern */
513     if( i_mb_type == I_4x4 || i_mb_type == I_8x8 )
514         bs_write_ue( s, intra4x4_cbp_to_golomb[( h->mb.i_cbp_chroma << 4 )|h->mb.i_cbp_luma] );
515     else if( i_mb_type != I_16x16 )
516         bs_write_ue( s, inter_cbp_to_golomb[( h->mb.i_cbp_chroma << 4 )|h->mb.i_cbp_luma] );
517
518     /* transform size 8x8 flag */
519     if( x264_mb_transform_8x8_allowed( h ) && h->mb.i_cbp_luma )
520         bs_write1( s, h->mb.b_transform_8x8 );
521
522     /* write residual */
523     if( i_mb_type == I_16x16 )
524     {
525         cavlc_qp_delta( h, s );
526
527         /* DC Luma */
528         block_residual_write_cavlc( h, s, DCT_LUMA_DC, 24 , h->dct.luma16x16_dc );
529
530         /* AC Luma */
531         if( h->mb.i_cbp_luma )
532             for( i = 0; i < 16; i++ )
533                 block_residual_write_cavlc( h, s, DCT_LUMA_AC, i, h->dct.luma4x4[i]+1 );
534     }
535     else if( h->mb.i_cbp_luma | h->mb.i_cbp_chroma )
536     {
537         cavlc_qp_delta( h, s );
538         x264_macroblock_luma_write_cavlc( h, s, 0, 3 );
539     }
540     if( h->mb.i_cbp_chroma )
541     {
542         /* Chroma DC residual present */
543         block_residual_write_cavlc( h, s, DCT_CHROMA_DC, 25, h->dct.chroma_dc[0] );
544         block_residual_write_cavlc( h, s, DCT_CHROMA_DC, 26, h->dct.chroma_dc[1] );
545         if( h->mb.i_cbp_chroma&0x02 ) /* Chroma AC residual present */
546             for( i = 16; i < 24; i++ )
547                 block_residual_write_cavlc( h, s, DCT_CHROMA_AC, i, h->dct.luma4x4[i]+1 );
548     }
549
550 #if !RDO_SKIP_BS
551     h->stat.frame.i_tex_bits += bs_pos(s) - i_mb_pos_tex;
552 #endif
553 }
554
555 #if RDO_SKIP_BS
556 /*****************************************************************************
557  * RD only; doesn't generate a valid bitstream
558  * doesn't write cbp or chroma dc (I don't know how much this matters)
559  * doesn't write ref (never varies between calls, so no point in doing so)
560  * only writes subpartition for p8x8, needed for sub-8x8 mode decision RDO
561  * works on all partition sizes except 16x16
562  *****************************************************************************/
563 static int x264_partition_size_cavlc( x264_t *h, int i8, int i_pixel )
564 {
565     const int i_mb_type = h->mb.i_type;
566     int b_8x16 = h->mb.i_partition == D_8x16;
567     int j;
568     h->out.bs.i_bits_encoded = 0;
569
570     if( i_mb_type == P_8x8 )
571     {
572         cavlc_mb8x8_mvd( h, &h->out.bs, i8 );
573         bs_write_ue( &h->out.bs, sub_mb_type_p_to_golomb[ h->mb.i_sub_partition[i8] ] );
574     }
575     else if( i_mb_type == P_L0 )
576         cavlc_mb_mvd( h, &h->out.bs, 0, 4*i8, 4>>b_8x16 );
577     else if( i_mb_type > B_DIRECT && i_mb_type < B_8x8 )
578     {
579         if( x264_mb_type_list_table[ i_mb_type ][0][!!i8] ) cavlc_mb_mvd( h, &h->out.bs, 0, 4*i8, 4>>b_8x16 );
580         if( x264_mb_type_list_table[ i_mb_type ][1][!!i8] ) cavlc_mb_mvd( h, &h->out.bs, 1, 4*i8, 4>>b_8x16 );
581     }
582     else //if( i_mb_type == B_8x8 )
583     {
584         if( x264_mb_partition_listX_table[0][ h->mb.i_sub_partition[i8] ] )
585             cavlc_mb_mvd( h, &h->out.bs, 0, 4*i8, 2 );
586         if( x264_mb_partition_listX_table[1][ h->mb.i_sub_partition[i8] ] )
587             cavlc_mb_mvd( h, &h->out.bs, 1, 4*i8, 2 );
588     }
589
590     for( j = (i_pixel < PIXEL_8x8); j >= 0; j-- )
591     {
592         x264_macroblock_luma_write_cavlc( h, &h->out.bs, i8, i8 );
593         block_residual_write_cavlc( h, &h->out.bs, DCT_CHROMA_AC, 16+i8, h->dct.luma4x4[16+i8]+1 );
594         block_residual_write_cavlc( h, &h->out.bs, DCT_CHROMA_AC, 20+i8, h->dct.luma4x4[20+i8]+1 );
595         i8 += x264_pixel_size[i_pixel].h >> 3;
596     }
597
598     return h->out.bs.i_bits_encoded;
599 }
600
601 static int x264_subpartition_size_cavlc( x264_t *h, int i4, int i_pixel )
602 {
603     int b_8x4 = i_pixel == PIXEL_8x4;
604     h->out.bs.i_bits_encoded = 0;
605     cavlc_mb_mvd( h, &h->out.bs, 0, i4, 1+b_8x4 );
606     block_residual_write_cavlc( h, &h->out.bs, DCT_LUMA_4x4, i4, h->dct.luma4x4[i4] );
607     if( i_pixel != PIXEL_4x4 )
608     {
609         i4 += 2-b_8x4;
610         block_residual_write_cavlc( h, &h->out.bs, DCT_LUMA_4x4, i4, h->dct.luma4x4[i4] );
611     }
612
613     return h->out.bs.i_bits_encoded;
614 }
615
616 static int cavlc_intra4x4_pred_size( x264_t *h, int i4, int i_mode )
617 {
618     if( x264_mb_predict_intra4x4_mode( h, i4 ) == x264_mb_pred_mode4x4_fix( i_mode ) )
619         return 1;
620     else
621         return 4;
622 }
623
624 static int x264_partition_i8x8_size_cavlc( x264_t *h, int i8, int i_mode )
625 {
626     h->out.bs.i_bits_encoded = cavlc_intra4x4_pred_size( h, 4*i8, i_mode );
627     bs_write_ue( &h->out.bs, intra4x4_cbp_to_golomb[( h->mb.i_cbp_chroma << 4 )|h->mb.i_cbp_luma] );
628     x264_macroblock_luma_write_cavlc( h, &h->out.bs, i8, i8 );
629     return h->out.bs.i_bits_encoded;
630 }
631
632 static int x264_partition_i4x4_size_cavlc( x264_t *h, int i4, int i_mode )
633 {
634     h->out.bs.i_bits_encoded = cavlc_intra4x4_pred_size( h, i4, i_mode );
635     block_residual_write_cavlc( h, &h->out.bs, DCT_LUMA_4x4, i4, h->dct.luma4x4[i4] );
636     return h->out.bs.i_bits_encoded;
637 }
638
639 static int x264_i8x8_chroma_size_cavlc( x264_t *h )
640 {
641     h->out.bs.i_bits_encoded = bs_size_ue( x264_mb_pred_mode8x8c_fix[ h->mb.i_chroma_pred_mode ] );
642     if( h->mb.i_cbp_chroma )
643     {
644         block_residual_write_cavlc( h, &h->out.bs, DCT_CHROMA_DC, 25, h->dct.chroma_dc[0] );
645         block_residual_write_cavlc( h, &h->out.bs, DCT_CHROMA_DC, 26, h->dct.chroma_dc[1] );
646
647         if( h->mb.i_cbp_chroma == 2 )
648         {
649             int i;
650             for( i = 16; i < 24; i++ )
651                 block_residual_write_cavlc( h, &h->out.bs, DCT_CHROMA_AC, i, h->dct.luma4x4[i]+1 );
652         }
653     }
654     return h->out.bs.i_bits_encoded;
655 }
656 #endif