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