]> git.sesse.net Git - x264/blob - encoder/cabac.c
Massive overhaul of nnz/cbp calculation
[x264] / encoder / cabac.c
1 /*****************************************************************************
2  * cabac.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 inline void x264_cabac_mb_type_intra( x264_t *h, x264_cabac_t *cb, int i_mb_type,
33                     int ctx0, int ctx1, int ctx2, int ctx3, int ctx4, int ctx5 )
34 {
35     if( i_mb_type == I_4x4 || i_mb_type == I_8x8 )
36     {
37         x264_cabac_encode_decision_noup( cb, ctx0, 0 );
38     }
39     else if( i_mb_type == I_PCM )
40     {
41         x264_cabac_encode_decision_noup( cb, ctx0, 1 );
42         x264_cabac_encode_flush( h, cb );
43     }
44     else
45     {
46         int i_pred = x264_mb_pred_mode16x16_fix[h->mb.i_intra16x16_pred_mode];
47
48         x264_cabac_encode_decision_noup( cb, ctx0, 1 );
49         x264_cabac_encode_terminal( cb );
50
51         x264_cabac_encode_decision_noup( cb, ctx1, !!h->mb.i_cbp_luma );
52         if( h->mb.i_cbp_chroma == 0 )
53             x264_cabac_encode_decision_noup( cb, ctx2, 0 );
54         else
55         {
56             x264_cabac_encode_decision( cb, ctx2, 1 );
57             x264_cabac_encode_decision_noup( cb, ctx3, h->mb.i_cbp_chroma != 1 );
58         }
59         x264_cabac_encode_decision( cb, ctx4, i_pred>>1 );
60         x264_cabac_encode_decision_noup( cb, ctx5, i_pred&1 );
61     }
62 }
63
64 static void x264_cabac_mb_type( x264_t *h, x264_cabac_t *cb )
65 {
66     const int i_mb_type = h->mb.i_type;
67
68     if( h->sh.b_mbaff &&
69         (!(h->mb.i_mb_y & 1) || IS_SKIP(h->mb.type[h->mb.i_mb_xy - h->mb.i_mb_stride])) )
70     {
71         x264_cabac_encode_decision_noup( cb, 70 + h->mb.cache.i_neighbour_interlaced, h->mb.b_interlaced );
72     }
73
74     if( h->sh.i_type == SLICE_TYPE_I )
75     {
76         int ctx = 0;
77         if( h->mb.i_mb_type_left >= 0 && h->mb.i_mb_type_left != I_4x4 )
78             ctx++;
79         if( h->mb.i_mb_type_top >= 0 && h->mb.i_mb_type_top != I_4x4 )
80             ctx++;
81
82         x264_cabac_mb_type_intra( h, cb, i_mb_type, 3+ctx, 3+3, 3+4, 3+5, 3+6, 3+7 );
83     }
84     else if( h->sh.i_type == SLICE_TYPE_P )
85     {
86         /* prefix: 14, suffix: 17 */
87         if( i_mb_type == P_L0 )
88         {
89             if( h->mb.i_partition == D_16x16 )
90             {
91                 x264_cabac_encode_decision_noup( cb, 14, 0 );
92                 x264_cabac_encode_decision_noup( cb, 15, 0 );
93                 x264_cabac_encode_decision_noup( cb, 16, 0 );
94             }
95             else if( h->mb.i_partition == D_16x8 )
96             {
97                 x264_cabac_encode_decision_noup( cb, 14, 0 );
98                 x264_cabac_encode_decision_noup( cb, 15, 1 );
99                 x264_cabac_encode_decision_noup( cb, 17, 1 );
100             }
101             else if( h->mb.i_partition == D_8x16 )
102             {
103                 x264_cabac_encode_decision_noup( cb, 14, 0 );
104                 x264_cabac_encode_decision_noup( cb, 15, 1 );
105                 x264_cabac_encode_decision_noup( cb, 17, 0 );
106             }
107         }
108         else if( i_mb_type == P_8x8 )
109         {
110             x264_cabac_encode_decision_noup( cb, 14, 0 );
111             x264_cabac_encode_decision_noup( cb, 15, 0 );
112             x264_cabac_encode_decision_noup( cb, 16, 1 );
113         }
114         else /* intra */
115         {
116             /* prefix */
117             x264_cabac_encode_decision_noup( cb, 14, 1 );
118
119             /* suffix */
120             x264_cabac_mb_type_intra( h, cb, i_mb_type, 17+0, 17+1, 17+2, 17+2, 17+3, 17+3 );
121         }
122     }
123     else if( h->sh.i_type == SLICE_TYPE_B )
124     {
125         int ctx = 0;
126         if( h->mb.i_mb_type_left >= 0 && h->mb.i_mb_type_left != B_SKIP && h->mb.i_mb_type_left != B_DIRECT )
127             ctx++;
128         if( h->mb.i_mb_type_top >= 0 && h->mb.i_mb_type_top != B_SKIP && h->mb.i_mb_type_top != B_DIRECT )
129             ctx++;
130
131         if( i_mb_type == B_DIRECT )
132             x264_cabac_encode_decision_noup( cb, 27+ctx, 0 );
133         else if( i_mb_type == B_8x8 )
134         {
135             x264_cabac_encode_decision_noup( cb, 27+ctx, 1 );
136             x264_cabac_encode_decision_noup( cb, 27+3,   1 );
137             x264_cabac_encode_decision_noup( cb, 27+4,   1 );
138             x264_cabac_encode_decision( cb, 27+5,   1 );
139             x264_cabac_encode_decision( cb, 27+5,   1 );
140             x264_cabac_encode_decision_noup( cb, 27+5,   1 );
141         }
142         else if( IS_INTRA( i_mb_type ) )
143         {
144             /* prefix */
145             x264_cabac_encode_decision_noup( cb, 27+ctx, 1 );
146             x264_cabac_encode_decision_noup( cb, 27+3,   1 );
147             x264_cabac_encode_decision_noup( cb, 27+4,   1 );
148             x264_cabac_encode_decision( cb, 27+5,   1 );
149             x264_cabac_encode_decision( cb, 27+5,   0 );
150             x264_cabac_encode_decision( cb, 27+5,   1 );
151
152             /* suffix */
153             x264_cabac_mb_type_intra( h, cb, i_mb_type, 32+0, 32+1, 32+2, 32+2, 32+3, 32+3 );
154         }
155         else
156         {
157             static const int i_mb_len[9*3] =
158             {
159                 6, 6, 3,    /* L0 L0 */
160                 6, 6, 0,    /* L0 L1 */
161                 7, 7, 0,    /* L0 BI */
162                 6, 6, 0,    /* L1 L0 */
163                 6, 6, 3,    /* L1 L1 */
164                 7, 7, 0,    /* L1 BI */
165                 7, 7, 0,    /* BI L0 */
166                 7, 7, 0,    /* BI L1 */
167                 7, 7, 6,    /* BI BI */
168             };
169             static const int i_mb_bits[9*3][7] =
170             {
171                 { 1,1,0,0,0,1   }, { 1,1,0,0,1,0,  }, { 1,0,0 },       /* L0 L0 */
172                 { 1,1,0,1,0,1   }, { 1,1,0,1,1,0   }, {0},             /* L0 L1 */
173                 { 1,1,1,0,0,0,0 }, { 1,1,1,0,0,0,1 }, {0},             /* L0 BI */
174                 { 1,1,0,1,1,1   }, { 1,1,1,1,1,0   }, {0},             /* L1 L0 */
175                 { 1,1,0,0,1,1   }, { 1,1,0,1,0,0   }, { 1,0,1 },       /* L1 L1 */
176                 { 1,1,1,0,0,1,0 }, { 1,1,1,0,0,1,1 }, {0},             /* L1 BI */
177                 { 1,1,1,0,1,0,0 }, { 1,1,1,0,1,0,1 }, {0},             /* BI L0 */
178                 { 1,1,1,0,1,1,0 }, { 1,1,1,0,1,1,1 }, {0},             /* BI L1 */
179                 { 1,1,1,1,0,0,0 }, { 1,1,1,1,0,0,1 }, { 1,1,0,0,0,0 }, /* BI BI */
180             };
181
182             const int idx = (i_mb_type - B_L0_L0) * 3 + (h->mb.i_partition - D_16x8);
183             int i;
184
185             x264_cabac_encode_decision_noup( cb, 27+ctx, i_mb_bits[idx][0] );
186             x264_cabac_encode_decision_noup( cb, 27+3,   i_mb_bits[idx][1] );
187             x264_cabac_encode_decision( cb, 27+5-i_mb_bits[idx][1], i_mb_bits[idx][2] );
188             for( i = 3; i < i_mb_len[idx]; i++ )
189                 x264_cabac_encode_decision( cb, 27+5, i_mb_bits[idx][i] );
190         }
191     }
192     else
193     {
194         x264_log(h, X264_LOG_ERROR, "unknown SLICE_TYPE unsupported in x264_macroblock_write_cabac\n" );
195     }
196 }
197
198 static void x264_cabac_mb_intra4x4_pred_mode( x264_cabac_t *cb, int i_pred, int i_mode )
199 {
200     if( i_pred == i_mode )
201         x264_cabac_encode_decision( cb, 68, 1 );
202     else
203     {
204         x264_cabac_encode_decision( cb, 68, 0 );
205         if( i_mode > i_pred  )
206             i_mode--;
207         x264_cabac_encode_decision( cb, 69, (i_mode     )&0x01 );
208         x264_cabac_encode_decision( cb, 69, (i_mode >> 1)&0x01 );
209         x264_cabac_encode_decision( cb, 69, (i_mode >> 2)&0x01 );
210     }
211 }
212
213 static void x264_cabac_mb_intra_chroma_pred_mode( x264_t *h, x264_cabac_t *cb )
214 {
215     const int i_mode = x264_mb_pred_mode8x8c_fix[ h->mb.i_chroma_pred_mode ];
216     int       ctx = 0;
217
218     /* No need to test for I4x4 or I_16x16 as cache_save handle that */
219     if( (h->mb.i_neighbour & MB_LEFT) && h->mb.chroma_pred_mode[h->mb.i_mb_xy - 1] != 0 )
220         ctx++;
221     if( (h->mb.i_neighbour & MB_TOP) && h->mb.chroma_pred_mode[h->mb.i_mb_top_xy] != 0 )
222         ctx++;
223
224     x264_cabac_encode_decision_noup( cb, 64 + ctx, i_mode > 0 );
225     if( i_mode > 0 )
226     {
227         x264_cabac_encode_decision( cb, 64 + 3, i_mode > 1 );
228         if( i_mode > 1 )
229             x264_cabac_encode_decision_noup( cb, 64 + 3, i_mode > 2 );
230     }
231 }
232
233 static void x264_cabac_mb_cbp_luma( x264_t *h, x264_cabac_t *cb )
234 {
235     int cbp = h->mb.i_cbp_luma;
236     int cbp_l = h->mb.i_neighbour & MB_LEFT ? h->mb.cbp[h->mb.i_mb_xy - 1] : -1;
237     int cbp_t = h->mb.i_neighbour & MB_TOP ? h->mb.cbp[h->mb.i_mb_top_xy] : -1;
238     x264_cabac_encode_decision( cb, 76 - ((cbp_l >> 1) & 1) - ((cbp_t >> 1) & 2), (h->mb.i_cbp_luma >> 0) & 1 );
239     x264_cabac_encode_decision( cb, 76 - ((cbp   >> 0) & 1) - ((cbp_t >> 2) & 2), (h->mb.i_cbp_luma >> 1) & 1 );
240     x264_cabac_encode_decision( cb, 76 - ((cbp_l >> 3) & 1) - ((cbp   << 1) & 2), (h->mb.i_cbp_luma >> 2) & 1 );
241     x264_cabac_encode_decision_noup( cb, 76 - ((cbp   >> 2) & 1) - ((cbp   >> 0) & 2), (h->mb.i_cbp_luma >> 3) & 1 );
242 }
243
244 static void x264_cabac_mb_cbp_chroma( x264_t *h, x264_cabac_t *cb )
245 {
246     int cbp_a = -1;
247     int cbp_b = -1;
248     int ctx;
249
250     /* No need to test for SKIP/PCM */
251     if( h->mb.i_neighbour & MB_LEFT )
252         cbp_a = (h->mb.cbp[h->mb.i_mb_xy - 1] >> 4)&0x3;
253
254     if( h->mb.i_neighbour & MB_TOP )
255         cbp_b = (h->mb.cbp[h->mb.i_mb_top_xy] >> 4)&0x3;
256
257     ctx = 0;
258     if( cbp_a > 0 ) ctx++;
259     if( cbp_b > 0 ) ctx += 2;
260     if( h->mb.i_cbp_chroma == 0 )
261         x264_cabac_encode_decision_noup( cb, 77 + ctx, 0 );
262     else
263     {
264         x264_cabac_encode_decision_noup( cb, 77 + ctx, 1 );
265
266         ctx = 4;
267         if( cbp_a == 2 ) ctx++;
268         if( cbp_b == 2 ) ctx += 2;
269         x264_cabac_encode_decision_noup( cb, 77 + ctx, h->mb.i_cbp_chroma > 1 );
270     }
271 }
272
273 static void x264_cabac_mb_qp_delta( x264_t *h, x264_cabac_t *cb )
274 {
275     int i_mbn_xy = h->mb.i_mb_prev_xy;
276     int i_dqp = h->mb.i_qp - h->mb.i_last_qp;
277     int ctx;
278
279     /* Avoid writing a delta quant if we have an empty i16x16 block, e.g. in a completely flat background area */
280     if( h->mb.i_type == I_16x16 && !h->mb.cbp[h->mb.i_mb_xy] )
281     {
282 #if !RDO_SKIP_BS
283         h->mb.i_qp = h->mb.i_last_qp;
284 #endif
285         i_dqp = 0;
286     }
287
288     /* No need to test for PCM / SKIP */
289     ctx = h->mb.i_last_dqp &&
290         ( h->mb.type[i_mbn_xy] == I_16x16 || (h->mb.cbp[i_mbn_xy]&0x3f) );
291
292     if( i_dqp != 0 )
293     {
294         int val = i_dqp <= 0 ? (-2*i_dqp) : (2*i_dqp - 1);
295         /* dqp is interpreted modulo 52 */
296         if( val >= 51 && val != 52 )
297             val = 103 - val;
298         while( val-- )
299         {
300             x264_cabac_encode_decision( cb, 60 + ctx, 1 );
301             ctx = 2+(ctx>>1);
302         }
303     }
304     x264_cabac_encode_decision_noup( cb, 60 + ctx, 0 );
305 }
306
307 #if !RDO_SKIP_BS
308 void x264_cabac_mb_skip( x264_t *h, int b_skip )
309 {
310     int ctx = (h->mb.i_mb_type_left >= 0 && !IS_SKIP( h->mb.i_mb_type_left ))
311             + (h->mb.i_mb_type_top >= 0 && !IS_SKIP( h->mb.i_mb_type_top ))
312             + (h->sh.i_type == SLICE_TYPE_P ? 11 : 24);
313     x264_cabac_encode_decision( &h->cabac, ctx, b_skip );
314 }
315 #endif
316
317 static inline void x264_cabac_mb_sub_p_partition( x264_cabac_t *cb, int i_sub )
318 {
319     if( i_sub == D_L0_8x8 )
320         x264_cabac_encode_decision( cb, 21, 1 );
321     else if( i_sub == D_L0_8x4 )
322     {
323         x264_cabac_encode_decision( cb, 21, 0 );
324         x264_cabac_encode_decision( cb, 22, 0 );
325     }
326     else if( i_sub == D_L0_4x8 )
327     {
328         x264_cabac_encode_decision( cb, 21, 0 );
329         x264_cabac_encode_decision( cb, 22, 1 );
330         x264_cabac_encode_decision( cb, 23, 1 );
331     }
332     else if( i_sub == D_L0_4x4 )
333     {
334         x264_cabac_encode_decision( cb, 21, 0 );
335         x264_cabac_encode_decision( cb, 22, 1 );
336         x264_cabac_encode_decision( cb, 23, 0 );
337     }
338 }
339
340 static NOINLINE void x264_cabac_mb_sub_b_partition( x264_cabac_t *cb, int i_sub )
341 {
342     static const uint8_t part_bits[12][7] = {
343         {6,1,1,1,0,1,1}, // D_L0_4x4
344         {5,1,1,0,0,1},   // D_L0_8x4
345         {5,1,1,0,1,0},   // D_L0_4x8
346         {3,1,0,0},       // D_L0_8x8
347         {5,1,1,1,1,0},   // D_L1_4x4
348         {5,1,1,0,1,1},   // D_L1_8x4
349         {6,1,1,1,0,0,0}, // D_L1_4x8
350         {3,1,0,1},       // D_L1_8x8
351         {5,1,1,1,1,1},   // D_BI_4x4
352         {6,1,1,1,0,0,1}, // D_BI_8x4
353         {6,1,1,1,0,1,0}, // D_BI_4x8
354         {5,1,1,0,0,0},   // D_BI_8x8
355     };
356     int len;
357     if( i_sub == D_DIRECT_8x8 )
358     {
359         x264_cabac_encode_decision( cb, 36, 0 );
360         return;
361     }
362     len = part_bits[i_sub][0];
363     x264_cabac_encode_decision( cb, 36, part_bits[i_sub][1] );
364     x264_cabac_encode_decision( cb, 37, part_bits[i_sub][2] );
365     if( len == 3 )
366         x264_cabac_encode_decision( cb, 39, part_bits[i_sub][3] );
367     else
368     {
369         x264_cabac_encode_decision( cb, 38, part_bits[i_sub][3] );
370         x264_cabac_encode_decision( cb, 39, part_bits[i_sub][4] );
371         x264_cabac_encode_decision( cb, 39, part_bits[i_sub][5] );
372         if( len == 6 )
373             x264_cabac_encode_decision( cb, 39, part_bits[i_sub][6] );
374     }
375 }
376
377 static inline void x264_cabac_mb_transform_size( x264_t *h, x264_cabac_t *cb )
378 {
379     int ctx = 399 + h->mb.cache.i_neighbour_transform_size;
380     x264_cabac_encode_decision_noup( cb, ctx, h->mb.b_transform_8x8 );
381 }
382
383 static void x264_cabac_mb_ref( x264_t *h, x264_cabac_t *cb, int i_list, int idx )
384 {
385     const int i8 = x264_scan8[idx];
386     const int i_refa = h->mb.cache.ref[i_list][i8 - 1];
387     const int i_refb = h->mb.cache.ref[i_list][i8 - 8];
388     int i_ref  = h->mb.cache.ref[i_list][i8];
389     int ctx  = 0;
390
391     if( i_refa > 0 && !h->mb.cache.skip[i8 - 1])
392         ctx++;
393     if( i_refb > 0 && !h->mb.cache.skip[i8 - 8])
394         ctx += 2;
395
396     while( i_ref > 0 )
397     {
398         x264_cabac_encode_decision( cb, 54 + ctx, 1 );
399         ctx = (ctx>>2)+4;
400         i_ref--;
401     }
402     x264_cabac_encode_decision( cb, 54 + ctx, 0 );
403 }
404
405 static inline void x264_cabac_mb_mvd_cpn( x264_t *h, x264_cabac_t *cb, int i_list, int idx, int l, int mvd )
406 {
407     static const uint8_t ctxes[9] = { 0,3,4,5,6,6,6,6,6 };
408     const int amvd = abs( h->mb.cache.mvd[i_list][x264_scan8[idx] - 1][l] ) +
409                      abs( h->mb.cache.mvd[i_list][x264_scan8[idx] - 8][l] );
410     const int i_abs = abs( mvd );
411     const int ctxbase = l ? 47 : 40;
412     int ctx = (amvd>2) + (amvd>32);
413     int i;
414
415     if( i_abs == 0 )
416         x264_cabac_encode_decision( cb, ctxbase + ctx, 0 );
417     else if( i_abs < 9 )
418     {
419         x264_cabac_encode_decision( cb, ctxbase + ctx, 1 );
420 #if RDO_SKIP_BS
421         if( i_abs > 4 )
422         {
423             for( i = 1; i < 4; i++ )
424                 x264_cabac_encode_decision( cb, ctxbase + ctxes[i], 1 );
425             cb->f8_bits_encoded += cabac_size_unary[i_abs - 3][cb->state[ctxbase+6]];
426             cb->state[ctxbase+6] = cabac_transition_unary[i_abs - 3][cb->state[ctxbase+6]];
427         }
428         else
429 #endif
430         {
431             for( i = 1; i < i_abs; i++ )
432                 x264_cabac_encode_decision( cb, ctxbase + ctxes[i], 1 );
433             x264_cabac_encode_decision( cb, ctxbase + ctxes[i_abs], 0 );
434             x264_cabac_encode_bypass( cb, mvd < 0 );
435         }
436     }
437     else
438     {
439         x264_cabac_encode_decision( cb, ctxbase + ctx, 1 );
440 #if RDO_SKIP_BS
441         for( i = 1; i < 4; i++ )
442             x264_cabac_encode_decision( cb, ctxbase + ctxes[i], 1 );
443         cb->f8_bits_encoded += cabac_size_5ones[cb->state[ctxbase+6]];
444         cb->state[ctxbase+6] = cabac_transition_5ones[cb->state[ctxbase+6]];
445         x264_cabac_encode_ue_bypass( cb, 3, i_abs - 9 );
446 #else
447         for( i = 1; i < 9; i++ )
448             x264_cabac_encode_decision( cb, ctxbase + ctxes[i], 1 );
449         x264_cabac_encode_ue_bypass( cb, 3, i_abs - 9 );
450         x264_cabac_encode_bypass( cb, mvd < 0 );
451 #endif
452     }
453 }
454
455 static inline void x264_cabac_mb_mvd( x264_t *h, x264_cabac_t *cb, int i_list, int idx, int width, int height )
456 {
457     DECLARE_ALIGNED_4( int16_t mvp[2] );
458     int mdx, mdy;
459
460     /* Calculate mvd */
461     x264_mb_predict_mv( h, i_list, idx, width, mvp );
462     mdx = h->mb.cache.mv[i_list][x264_scan8[idx]][0] - mvp[0];
463     mdy = h->mb.cache.mv[i_list][x264_scan8[idx]][1] - mvp[1];
464
465     /* encode */
466     x264_cabac_mb_mvd_cpn( h, cb, i_list, idx, 0, mdx );
467     x264_cabac_mb_mvd_cpn( h, cb, i_list, idx, 1, mdy );
468
469     /* save value */
470     x264_macroblock_cache_mvd( h, block_idx_x[idx], block_idx_y[idx], width, height, i_list, pack16to32_mask(mdx,mdy) );
471 }
472
473 static inline void x264_cabac_mb8x8_mvd( x264_t *h, x264_cabac_t *cb, int i_list, int i )
474 {
475     if( !x264_mb_partition_listX_table[i_list][ h->mb.i_sub_partition[i] ] )
476         return;
477
478     switch( h->mb.i_sub_partition[i] )
479     {
480         case D_L0_8x8:
481         case D_L1_8x8:
482         case D_BI_8x8:
483             x264_cabac_mb_mvd( h, cb, i_list, 4*i, 2, 2 );
484             break;
485         case D_L0_8x4:
486         case D_L1_8x4:
487         case D_BI_8x4:
488             x264_cabac_mb_mvd( h, cb, i_list, 4*i+0, 2, 1 );
489             x264_cabac_mb_mvd( h, cb, i_list, 4*i+2, 2, 1 );
490             break;
491         case D_L0_4x8:
492         case D_L1_4x8:
493         case D_BI_4x8:
494             x264_cabac_mb_mvd( h, cb, i_list, 4*i+0, 1, 2 );
495             x264_cabac_mb_mvd( h, cb, i_list, 4*i+1, 1, 2 );
496             break;
497         case D_L0_4x4:
498         case D_L1_4x4:
499         case D_BI_4x4:
500             x264_cabac_mb_mvd( h, cb, i_list, 4*i+0, 1, 1 );
501             x264_cabac_mb_mvd( h, cb, i_list, 4*i+1, 1, 1 );
502             x264_cabac_mb_mvd( h, cb, i_list, 4*i+2, 1, 1 );
503             x264_cabac_mb_mvd( h, cb, i_list, 4*i+3, 1, 1 );
504             break;
505     }
506 }
507
508 static int x264_cabac_mb_cbf_ctxidxinc( x264_t *h, int i_cat, int i_idx )
509 {
510     /* i_ctxBlockCat: 0-> DC 16x16  i_idx = 0
511      *                1-> AC 16x16  i_idx = luma4x4idx
512      *                2-> Luma4x4   i_idx = luma4x4idx
513      *                3-> DC Chroma i_idx = iCbCr
514      *                4-> AC Chroma i_idx = 4 * iCbCr + chroma4x4idx
515      *                5-> Luma8x8   i_idx = luma8x8idx
516      */
517
518     int i_mba_xy = -1;
519     int i_mbb_xy = -1;
520     int i_nza = 0;
521     int i_nzb = 0;
522
523     switch( i_cat )
524     {
525         case DCT_LUMA_DC:
526             if( h->mb.i_neighbour & MB_LEFT )
527             {
528                 i_mba_xy = h->mb.i_mb_xy - 1;
529                 i_nza = h->mb.cbp[i_mba_xy] & 0x100;
530             }
531             if( h->mb.i_neighbour & MB_TOP )
532             {
533                 i_mbb_xy = h->mb.i_mb_top_xy;
534                 i_nzb = h->mb.cbp[i_mbb_xy] & 0x100;
535             }
536             break;
537         case DCT_LUMA_AC:
538         case DCT_LUMA_4x4:
539             if( i_idx & ~10 ) // block_idx_x > 0
540                 i_mba_xy = h->mb.i_mb_xy;
541             else if( h->mb.i_neighbour & MB_LEFT )
542                 i_mba_xy = h->mb.i_mb_xy - 1;
543
544             if( i_idx & ~5 ) // block_idx_y > 0
545                 i_mbb_xy = h->mb.i_mb_xy;
546             else if( h->mb.i_neighbour & MB_TOP )
547                 i_mbb_xy = h->mb.i_mb_top_xy;
548
549             /* no need to test for skip/pcm */
550             if( i_mba_xy >= 0 )
551                 i_nza = h->mb.cache.non_zero_count[x264_scan8[i_idx] - 1];
552             if( i_mbb_xy >= 0 )
553                 i_nzb = h->mb.cache.non_zero_count[x264_scan8[i_idx] - 8];
554             break;
555         case DCT_CHROMA_DC:
556             /* no need to test skip/pcm */
557             i_idx -= 25;
558             if( h->mb.i_neighbour & MB_LEFT )
559             {
560                 i_mba_xy = h->mb.i_mb_xy - 1;
561                 i_nza = h->mb.cbp[i_mba_xy] & (0x200 << i_idx);
562             }
563             if( h->mb.i_neighbour & MB_TOP )
564             {
565                 i_mbb_xy = h->mb.i_mb_top_xy;
566                 i_nzb = h->mb.cbp[i_mbb_xy] & (0x200 << i_idx);
567             }
568             break;
569         case DCT_CHROMA_AC:
570             if( i_idx & 1 )
571                 i_mba_xy = h->mb.i_mb_xy;
572             else if( h->mb.i_neighbour & MB_LEFT )
573                 i_mba_xy = h->mb.i_mb_xy - 1;
574
575             if( i_idx & 2 )
576                 i_mbb_xy = h->mb.i_mb_xy;
577             else if( h->mb.i_neighbour & MB_TOP )
578                 i_mbb_xy = h->mb.i_mb_top_xy;
579
580             /* no need to test skip/pcm */
581             if( i_mba_xy >= 0 )
582                 i_nza = h->mb.cache.non_zero_count[x264_scan8[i_idx] - 1];
583             if( i_mbb_xy >= 0 )
584                 i_nzb = h->mb.cache.non_zero_count[x264_scan8[i_idx] - 8];
585     }
586
587     if( IS_INTRA( h->mb.i_type ) )
588     {
589         i_nza |= i_mba_xy < 0;
590         i_nzb |= i_mbb_xy < 0;
591     }
592
593     return 4*i_cat + 2*!!i_nzb + !!i_nza;
594 }
595
596
597 static const uint16_t significant_coeff_flag_offset[2][6] = {
598     { 105, 120, 134, 149, 152, 402 },
599     { 277, 292, 306, 321, 324, 436 }
600 };
601 static const uint16_t last_coeff_flag_offset[2][6] = {
602     { 166, 181, 195, 210, 213, 417 },
603     { 338, 353, 367, 382, 385, 451 }
604 };
605 static const uint16_t coeff_abs_level_m1_offset[6] =
606     { 227, 237, 247, 257, 266, 426 };
607 static const uint8_t significant_coeff_flag_offset_8x8[2][63] =
608 {{
609     0, 1, 2, 3, 4, 5, 5, 4, 4, 3, 3, 4, 4, 4, 5, 5,
610     4, 4, 4, 4, 3, 3, 6, 7, 7, 7, 8, 9,10, 9, 8, 7,
611     7, 6,11,12,13,11, 6, 7, 8, 9,14,10, 9, 8, 6,11,
612    12,13,11, 6, 9,14,10, 9,11,12,13,11,14,10,12
613 },{
614     0, 1, 1, 2, 2, 3, 3, 4, 5, 6, 7, 7, 7, 8, 4, 5,
615     6, 9,10,10, 8,11,12,11, 9, 9,10,10, 8,11,12,11,
616     9, 9,10,10, 8,11,12,11, 9, 9,10,10, 8,13,13, 9,
617     9,10,10, 8,13,13, 9, 9,10,10,14,14,14,14,14
618 }};
619 static const uint8_t last_coeff_flag_offset_8x8[63] = {
620     0, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1,
621     2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2,
622     3, 3, 3, 3, 3, 3, 3, 3, 4, 4, 4, 4, 4, 4, 4, 4,
623     5, 5, 5, 5, 6, 6, 6, 6, 7, 7, 7, 7, 8, 8, 8
624 };
625
626 // node ctx: 0..3: abslevel1 (with abslevelgt1 == 0).
627 //           4..7: abslevelgt1 + 3 (and abslevel1 doesn't matter).
628 /* map node ctx => cabac ctx for level=1 */
629 static const int coeff_abs_level1_ctx[8] = { 1, 2, 3, 4, 0, 0, 0, 0 };
630 /* map node ctx => cabac ctx for level>1 */
631 static const int coeff_abs_levelgt1_ctx[8] = { 5, 5, 5, 5, 6, 7, 8, 9 };
632 static const uint8_t coeff_abs_level_transition[2][8] = {
633 /* update node ctx after coding a level=1 */
634     { 1, 2, 3, 3, 4, 5, 6, 7 },
635 /* update node ctx after coding a level>1 */
636     { 4, 4, 4, 4, 5, 6, 7, 7 }
637 };
638
639 #if !RDO_SKIP_BS
640 static void block_residual_write_cabac( x264_t *h, x264_cabac_t *cb, int i_ctxBlockCat, int i_idx, int16_t *l, int i_count )
641 {
642     const int i_ctx_sig = significant_coeff_flag_offset[h->mb.b_interlaced][i_ctxBlockCat];
643     const int i_ctx_last = last_coeff_flag_offset[h->mb.b_interlaced][i_ctxBlockCat];
644     const int i_ctx_level = coeff_abs_level_m1_offset[i_ctxBlockCat];
645     const uint8_t *significant_coeff_flag_offset = significant_coeff_flag_offset_8x8[h->mb.b_interlaced];
646
647     int i_coeff_abs_m1[64];
648     int i_coeff_sign[64];
649     int i_coeff = 0;
650     int i_last;
651     int i_sigmap_size;
652     int node_ctx = 0;
653     int i;
654
655     if( i_count != 64 )
656     {
657         /* coded block flag */
658         int ctx = 85 + x264_cabac_mb_cbf_ctxidxinc( h, i_ctxBlockCat, i_idx );
659         if( h->mb.cache.non_zero_count[x264_scan8[i_idx]] )
660             x264_cabac_encode_decision( cb, ctx, 1 );
661         else
662         {
663             x264_cabac_encode_decision( cb, ctx, 0 );
664             return;
665         }
666     }
667
668     i_last = h->quantf.coeff_last[i_ctxBlockCat](l);
669
670     i_sigmap_size = X264_MIN( i_last+1, i_count-1 );
671
672 #define WRITE_SIGMAP( l8x8 )\
673     for( i = 0; i < i_sigmap_size; i++ )\
674     {\
675         if( l[i] )\
676         {\
677             i_coeff_abs_m1[i_coeff] = abs(l[i]) - 1;\
678             if( !RDO_SKIP_BS )\
679                 i_coeff_sign[i_coeff] = l[i] < 0;\
680             i_coeff++;\
681             x264_cabac_encode_decision( cb, i_ctx_sig + (l8x8 ? significant_coeff_flag_offset[i] : i), 1 );\
682             x264_cabac_encode_decision( cb, i_ctx_last + (l8x8 ? last_coeff_flag_offset_8x8[i] : i), i == i_last );\
683         }\
684         else\
685             x264_cabac_encode_decision( cb, i_ctx_sig + (l8x8 ? significant_coeff_flag_offset[i] : i), 0 );\
686     }
687
688     if( i_ctxBlockCat == DCT_LUMA_8x8 )
689         WRITE_SIGMAP( 1 )
690     else
691         WRITE_SIGMAP( 0 )
692
693     if( i == i_last )
694     {
695         i_coeff_abs_m1[i_coeff] = abs(l[i]) - 1;
696         i_coeff_sign[i_coeff]   = l[i] < 0;
697         i_coeff++;
698     }
699
700     do
701     {
702         int i_prefix, ctx;
703         i_coeff--;
704
705         /* write coeff_abs - 1 */
706         i_prefix = X264_MIN( i_coeff_abs_m1[i_coeff], 14 );
707         ctx = coeff_abs_level1_ctx[node_ctx] + i_ctx_level;
708
709         if( i_prefix )
710         {
711             x264_cabac_encode_decision( cb, ctx, 1 );
712             ctx = coeff_abs_levelgt1_ctx[node_ctx] + i_ctx_level;
713             for( i = 0; i < i_prefix - 1; i++ )
714                 x264_cabac_encode_decision( cb, ctx, 1 );
715             if( i_prefix < 14 )
716                 x264_cabac_encode_decision( cb, ctx, 0 );
717             if( i_prefix >= 14 )
718                 x264_cabac_encode_ue_bypass( cb, 0, i_coeff_abs_m1[i_coeff] - 14 );
719
720             node_ctx = coeff_abs_level_transition[1][node_ctx];
721         }
722         else
723         {
724             x264_cabac_encode_decision( cb, ctx, 0 );
725             node_ctx = coeff_abs_level_transition[0][node_ctx];
726         }
727
728         x264_cabac_encode_bypass( cb, i_coeff_sign[i_coeff] );
729     } while( i_coeff > 0 );
730 }
731 #define block_residual_write_cabac_8x8( h, cb, idx, l ) block_residual_write_cabac( h, cb, DCT_LUMA_8x8, idx, l, 64 )
732
733 #else
734
735 /* Faster RDO by merging sigmap and level coding.  Note that for 8x8dct
736  * this is slightly incorrect because the sigmap is not reversible
737  * (contexts are repeated).  However, there is nearly no quality penalty
738  * for this (~0.001db) and the speed boost (~30%) is worth it. */
739 static void ALWAYS_INLINE block_residual_write_cabac_internal( x264_t *h, x264_cabac_t *cb, int i_ctxBlockCat, int i_idx, int16_t *l, int i_count, int b_8x8 )
740 {
741     const int i_ctx_sig = significant_coeff_flag_offset[h->mb.b_interlaced][i_ctxBlockCat];
742     const int i_ctx_last = last_coeff_flag_offset[h->mb.b_interlaced][i_ctxBlockCat];
743     const int i_ctx_level = coeff_abs_level_m1_offset[i_ctxBlockCat];
744     const uint8_t *significant_coeff_flag_offset = significant_coeff_flag_offset_8x8[h->mb.b_interlaced];
745     int i_last, i_coeff_abs_m1, ctx, i_prefix, i, node_ctx;
746
747     if( !b_8x8 )
748     {
749         /* coded block flag */
750         ctx = 85 + x264_cabac_mb_cbf_ctxidxinc( h, i_ctxBlockCat, i_idx );
751         if( h->mb.cache.non_zero_count[x264_scan8[i_idx]] )
752             x264_cabac_encode_decision( cb, ctx, 1 );
753         else
754         {
755             x264_cabac_encode_decision( cb, ctx, 0 );
756             return;
757         }
758     }
759
760     i_last = h->quantf.coeff_last[i_ctxBlockCat](l);
761
762     i_coeff_abs_m1 = abs(l[i_last]) - 1;
763     i_prefix = X264_MIN( i_coeff_abs_m1, 14 );
764     ctx = coeff_abs_level1_ctx[0] + i_ctx_level;
765
766     if( i_last != i_count - 1 )
767     {
768         x264_cabac_encode_decision( cb, i_ctx_sig + (b_8x8?significant_coeff_flag_offset[i_last]:i_last), 1 );
769         x264_cabac_encode_decision( cb, i_ctx_last + (b_8x8?last_coeff_flag_offset_8x8[i_last]:i_last), 1 );
770     }
771
772     if( i_prefix )
773     {
774         x264_cabac_encode_decision( cb, ctx, 1 );
775         ctx = coeff_abs_levelgt1_ctx[0] + i_ctx_level;
776         cb->f8_bits_encoded += cabac_size_unary[i_prefix][cb->state[ctx]];
777         cb->state[ctx] = cabac_transition_unary[i_prefix][cb->state[ctx]];
778         if( i_prefix >= 14 )
779             x264_cabac_encode_ue_bypass( cb, 0, i_coeff_abs_m1 - 14 );
780         node_ctx = coeff_abs_level_transition[1][0];
781     }
782     else
783     {
784         x264_cabac_encode_decision( cb, ctx, 0 );
785         node_ctx = coeff_abs_level_transition[0][0];
786         x264_cabac_encode_bypass( cb, 0 ); // sign
787     }
788
789     for( i = i_last-1 ; i >= 0; i-- )
790     {
791         if( l[i] )
792         {
793             x264_cabac_encode_decision( cb, i_ctx_sig + (b_8x8?significant_coeff_flag_offset[i]:i), 1 );
794             x264_cabac_encode_decision( cb, i_ctx_last + (b_8x8?last_coeff_flag_offset_8x8[i]:i), 0 );
795             ctx = coeff_abs_level1_ctx[node_ctx] + i_ctx_level;
796
797             if( (unsigned)(l[i]+1) > 2 )
798             {
799                 i_coeff_abs_m1 = abs(l[i]) - 1;
800                 i_prefix = X264_MIN( i_coeff_abs_m1, 14 );
801                 x264_cabac_encode_decision( cb, ctx, 1 );
802                 ctx = coeff_abs_levelgt1_ctx[node_ctx] + i_ctx_level;
803                 cb->f8_bits_encoded += cabac_size_unary[i_prefix][cb->state[ctx]];
804                 cb->state[ctx] = cabac_transition_unary[i_prefix][cb->state[ctx]];
805                 if( i_prefix >= 14 )
806                     x264_cabac_encode_ue_bypass( cb, 0, i_coeff_abs_m1 - 14 );
807                 node_ctx = coeff_abs_level_transition[1][node_ctx];
808             }
809             else
810             {
811                 x264_cabac_encode_decision( cb, ctx, 0 );
812                 node_ctx = coeff_abs_level_transition[0][node_ctx];
813                 x264_cabac_encode_bypass( cb, 0 );
814             }
815         }
816         else
817             x264_cabac_encode_decision( cb, i_ctx_sig + (b_8x8?significant_coeff_flag_offset[i]:i), 0 );
818     }
819 }
820
821 static void block_residual_write_cabac_8x8( x264_t *h, x264_cabac_t *cb, int i_idx, int16_t *l )
822 {
823     block_residual_write_cabac_internal( h, cb, DCT_LUMA_8x8, i_idx, l, 64, 1 );
824 }
825 static void block_residual_write_cabac( x264_t *h, x264_cabac_t *cb, int i_ctxBlockCat, int i_idx, int16_t *l, int i_count )
826 {
827     block_residual_write_cabac_internal( h, cb, i_ctxBlockCat, i_idx, l, i_count, 0 );
828 }
829 #endif
830
831 void x264_macroblock_write_cabac( x264_t *h, x264_cabac_t *cb )
832 {
833     const int i_mb_type = h->mb.i_type;
834     int i_list;
835     int i;
836
837 #if !RDO_SKIP_BS
838     const int i_mb_pos_start = x264_cabac_pos( cb );
839     int       i_mb_pos_tex;
840 #endif
841
842     /* Write the MB type */
843     x264_cabac_mb_type( h, cb );
844
845 #if !RDO_SKIP_BS
846     if( i_mb_type == I_PCM )
847     {
848         i_mb_pos_tex = x264_cabac_pos( cb );
849         h->stat.frame.i_mv_bits += i_mb_pos_tex - i_mb_pos_start;
850
851         memcpy( cb->p, h->mb.pic.p_fenc[0], 256 );
852         cb->p += 256;
853         for( i = 0; i < 8; i++ )
854             memcpy( cb->p + i*8, h->mb.pic.p_fenc[1] + i*FENC_STRIDE, 8 );
855         cb->p += 64;
856         for( i = 0; i < 8; i++ )
857             memcpy( cb->p + i*8, h->mb.pic.p_fenc[2] + i*FENC_STRIDE, 8 );
858         cb->p += 64;
859
860         cb->i_low   = 0;
861         cb->i_range = 0x01FE;
862         cb->i_queue = -1;
863         cb->i_bytes_outstanding = 0;
864
865         /* if PCM is chosen, we need to store reconstructed frame data */
866         h->mc.copy[PIXEL_16x16]( h->mb.pic.p_fdec[0], FDEC_STRIDE, h->mb.pic.p_fenc[0], FENC_STRIDE, 16 );
867         h->mc.copy[PIXEL_8x8]  ( h->mb.pic.p_fdec[1], FDEC_STRIDE, h->mb.pic.p_fenc[1], FENC_STRIDE, 8 );
868         h->mc.copy[PIXEL_8x8]  ( h->mb.pic.p_fdec[2], FDEC_STRIDE, h->mb.pic.p_fenc[2], FENC_STRIDE, 8 );
869
870         h->stat.frame.i_tex_bits += x264_cabac_pos( cb ) - i_mb_pos_tex;
871         return;
872     }
873 #endif
874
875     if( IS_INTRA( i_mb_type ) )
876     {
877         if( h->pps->b_transform_8x8_mode && i_mb_type != I_16x16 )
878             x264_cabac_mb_transform_size( h, cb );
879
880         if( i_mb_type != I_16x16 )
881         {
882             int di = (i_mb_type == I_8x8) ? 4 : 1;
883             for( i = 0; i < 16; i += di )
884             {
885                 const int i_pred = x264_mb_predict_intra4x4_mode( h, i );
886                 const int i_mode = x264_mb_pred_mode4x4_fix( h->mb.cache.intra4x4_pred_mode[x264_scan8[i]] );
887                 x264_cabac_mb_intra4x4_pred_mode( cb, i_pred, i_mode );
888             }
889         }
890
891         x264_cabac_mb_intra_chroma_pred_mode( h, cb );
892     }
893     else if( i_mb_type == P_L0 )
894     {
895         if( h->mb.i_partition == D_16x16 )
896         {
897             if( h->mb.pic.i_fref[0] > 1 )
898             {
899                 x264_cabac_mb_ref( h, cb, 0, 0 );
900             }
901             x264_cabac_mb_mvd( h, cb, 0, 0, 4, 4 );
902         }
903         else if( h->mb.i_partition == D_16x8 )
904         {
905             if( h->mb.pic.i_fref[0] > 1 )
906             {
907                 x264_cabac_mb_ref( h, cb, 0, 0 );
908                 x264_cabac_mb_ref( h, cb, 0, 8 );
909             }
910             x264_cabac_mb_mvd( h, cb, 0, 0, 4, 2 );
911             x264_cabac_mb_mvd( h, cb, 0, 8, 4, 2 );
912         }
913         else if( h->mb.i_partition == D_8x16 )
914         {
915             if( h->mb.pic.i_fref[0] > 1 )
916             {
917                 x264_cabac_mb_ref( h, cb, 0, 0 );
918                 x264_cabac_mb_ref( h, cb, 0, 4 );
919             }
920             x264_cabac_mb_mvd( h, cb, 0, 0, 2, 4 );
921             x264_cabac_mb_mvd( h, cb, 0, 4, 2, 4 );
922         }
923     }
924     else if( i_mb_type == P_8x8 )
925     {
926         /* sub mb type */
927         x264_cabac_mb_sub_p_partition( cb, h->mb.i_sub_partition[0] );
928         x264_cabac_mb_sub_p_partition( cb, h->mb.i_sub_partition[1] );
929         x264_cabac_mb_sub_p_partition( cb, h->mb.i_sub_partition[2] );
930         x264_cabac_mb_sub_p_partition( cb, h->mb.i_sub_partition[3] );
931
932         /* ref 0 */
933         if( h->mb.pic.i_fref[0] > 1 )
934         {
935             x264_cabac_mb_ref( h, cb, 0, 0 );
936             x264_cabac_mb_ref( h, cb, 0, 4 );
937             x264_cabac_mb_ref( h, cb, 0, 8 );
938             x264_cabac_mb_ref( h, cb, 0, 12 );
939         }
940
941         for( i = 0; i < 4; i++ )
942             x264_cabac_mb8x8_mvd( h, cb, 0, i );
943     }
944     else if( i_mb_type == B_8x8 )
945     {
946         /* sub mb type */
947         x264_cabac_mb_sub_b_partition( cb, h->mb.i_sub_partition[0] );
948         x264_cabac_mb_sub_b_partition( cb, h->mb.i_sub_partition[1] );
949         x264_cabac_mb_sub_b_partition( cb, h->mb.i_sub_partition[2] );
950         x264_cabac_mb_sub_b_partition( cb, h->mb.i_sub_partition[3] );
951
952         /* ref */
953         for( i_list = 0; i_list < 2; i_list++ )
954         {
955             if( ( i_list ? h->mb.pic.i_fref[1] : h->mb.pic.i_fref[0] ) == 1 )
956                 continue;
957             for( i = 0; i < 4; i++ )
958                 if( x264_mb_partition_listX_table[i_list][ h->mb.i_sub_partition[i] ] )
959                     x264_cabac_mb_ref( h, cb, i_list, 4*i );
960         }
961
962         for( i = 0; i < 4; i++ )
963             x264_cabac_mb8x8_mvd( h, cb, 0, i );
964         for( i = 0; i < 4; i++ )
965             x264_cabac_mb8x8_mvd( h, cb, 1, i );
966     }
967     else if( i_mb_type != B_DIRECT )
968     {
969         /* All B mode */
970         const uint8_t (*b_list)[2] = x264_mb_type_list_table[i_mb_type];
971
972         for( i_list = 0; i_list < 2; i_list++ )
973         {
974             const int i_ref_max = h->mb.pic.i_fref[i_list];
975
976             if( i_ref_max > 1 )
977             {
978                 if( h->mb.i_partition == D_16x16 )
979                 {
980                     if( b_list[i_list][0] ) x264_cabac_mb_ref( h, cb, i_list, 0 );
981                 }
982                 else if( h->mb.i_partition == D_16x8 )
983                 {
984                     if( b_list[i_list][0] ) x264_cabac_mb_ref( h, cb, i_list, 0 );
985                     if( b_list[i_list][1] ) x264_cabac_mb_ref( h, cb, i_list, 8 );
986                 }
987                 else if( h->mb.i_partition == D_8x16 )
988                 {
989                     if( b_list[i_list][0] ) x264_cabac_mb_ref( h, cb, i_list, 0 );
990                     if( b_list[i_list][1] ) x264_cabac_mb_ref( h, cb, i_list, 4 );
991                 }
992             }
993         }
994         for( i_list = 0; i_list < 2; i_list++ )
995         {
996             if( h->mb.i_partition == D_16x16 )
997             {
998                 if( b_list[i_list][0] ) x264_cabac_mb_mvd( h, cb, i_list, 0, 4, 4 );
999             }
1000             else if( h->mb.i_partition == D_16x8 )
1001             {
1002                 if( b_list[i_list][0] ) x264_cabac_mb_mvd( h, cb, i_list, 0, 4, 2 );
1003                 if( b_list[i_list][1] ) x264_cabac_mb_mvd( h, cb, i_list, 8, 4, 2 );
1004             }
1005             else if( h->mb.i_partition == D_8x16 )
1006             {
1007                 if( b_list[i_list][0] ) x264_cabac_mb_mvd( h, cb, i_list, 0, 2, 4 );
1008                 if( b_list[i_list][1] ) x264_cabac_mb_mvd( h, cb, i_list, 4, 2, 4 );
1009             }
1010         }
1011     }
1012
1013 #if !RDO_SKIP_BS
1014     i_mb_pos_tex = x264_cabac_pos( cb );
1015     h->stat.frame.i_mv_bits += i_mb_pos_tex - i_mb_pos_start;
1016 #endif
1017
1018     if( i_mb_type != I_16x16 )
1019     {
1020         x264_cabac_mb_cbp_luma( h, cb );
1021         x264_cabac_mb_cbp_chroma( h, cb );
1022     }
1023
1024     if( x264_mb_transform_8x8_allowed( h ) && h->mb.i_cbp_luma )
1025     {
1026         x264_cabac_mb_transform_size( h, cb );
1027     }
1028
1029     if( h->mb.i_cbp_luma > 0 || h->mb.i_cbp_chroma > 0 || i_mb_type == I_16x16 )
1030     {
1031         x264_cabac_mb_qp_delta( h, cb );
1032
1033         /* write residual */
1034         if( i_mb_type == I_16x16 )
1035         {
1036             /* DC Luma */
1037             block_residual_write_cabac( h, cb, DCT_LUMA_DC, 24, h->dct.luma16x16_dc, 16 );
1038
1039             /* AC Luma */
1040             if( h->mb.i_cbp_luma != 0 )
1041                 for( i = 0; i < 16; i++ )
1042                     block_residual_write_cabac( h, cb, DCT_LUMA_AC, i, h->dct.luma4x4[i]+1, 15 );
1043         }
1044         else if( h->mb.b_transform_8x8 )
1045         {
1046             for( i = 0; i < 4; i++ )
1047                 if( h->mb.i_cbp_luma & ( 1 << i ) )
1048                     block_residual_write_cabac_8x8( h, cb, i, h->dct.luma8x8[i] );
1049         }
1050         else
1051         {
1052             for( i = 0; i < 16; i++ )
1053                 if( h->mb.i_cbp_luma & ( 1 << ( i / 4 ) ) )
1054                     block_residual_write_cabac( h, cb, DCT_LUMA_4x4, i, h->dct.luma4x4[i], 16 );
1055         }
1056
1057         if( h->mb.i_cbp_chroma &0x03 )    /* Chroma DC residual present */
1058         {
1059             block_residual_write_cabac( h, cb, DCT_CHROMA_DC, 25, h->dct.chroma_dc[0], 4 );
1060             block_residual_write_cabac( h, cb, DCT_CHROMA_DC, 26, h->dct.chroma_dc[1], 4 );
1061         }
1062         if( h->mb.i_cbp_chroma&0x02 ) /* Chroma AC residual present */
1063         {
1064             for( i = 16; i < 24; i++ )
1065                 block_residual_write_cabac( h, cb, DCT_CHROMA_AC, i, h->dct.luma4x4[i]+1, 15 );
1066         }
1067     }
1068
1069 #if !RDO_SKIP_BS
1070     h->stat.frame.i_tex_bits += x264_cabac_pos( cb ) - i_mb_pos_tex;
1071 #endif
1072 }
1073
1074 #if RDO_SKIP_BS
1075 /*****************************************************************************
1076  * RD only; doesn't generate a valid bitstream
1077  * doesn't write cbp or chroma dc (I don't know how much this matters)
1078  * doesn't write ref or subpartition (never varies between calls, so no point in doing so)
1079  * works on all partition sizes except 16x16
1080  * for sub8x8, call once per 8x8 block
1081  *****************************************************************************/
1082 static void x264_partition_size_cabac( x264_t *h, x264_cabac_t *cb, int i8, int i_pixel )
1083 {
1084     const int i_mb_type = h->mb.i_type;
1085     int b_8x16 = h->mb.i_partition == D_8x16;
1086     int j;
1087
1088     if( i_mb_type == P_8x8 )
1089         x264_cabac_mb8x8_mvd( h, cb, 0, i8 );
1090     else if( i_mb_type == P_L0 )
1091         x264_cabac_mb_mvd( h, cb, 0, 4*i8, 4>>b_8x16, 2<<b_8x16 );
1092     else if( i_mb_type > B_DIRECT && i_mb_type < B_8x8 )
1093     {
1094         if( x264_mb_type_list_table[ i_mb_type ][0][!!i8] ) x264_cabac_mb_mvd( h, cb, 0, 4*i8, 4>>b_8x16, 2<<b_8x16 );
1095         if( x264_mb_type_list_table[ i_mb_type ][1][!!i8] ) x264_cabac_mb_mvd( h, cb, 1, 4*i8, 4>>b_8x16, 2<<b_8x16 );
1096     }
1097     else if( i_mb_type == B_8x8 )
1098     {
1099         x264_cabac_mb8x8_mvd( h, cb, 0, i8 );
1100         x264_cabac_mb8x8_mvd( h, cb, 1, i8 );
1101     }
1102     else
1103     {
1104         x264_log(h, X264_LOG_ERROR, "invalid/unhandled mb_type %d\n",i_mb_type );
1105         return;
1106     }
1107
1108     for( j = (i_pixel < PIXEL_8x8); j >= 0; j-- )
1109     {
1110         if( h->mb.i_cbp_luma & (1 << i8) )
1111         {
1112             if( h->mb.b_transform_8x8 )
1113                 block_residual_write_cabac_8x8( h, cb, i8, h->dct.luma8x8[i8] );
1114             else
1115             {
1116                 int i4;
1117                 for( i4 = 0; i4 < 4; i4++ )
1118                     block_residual_write_cabac( h, cb, DCT_LUMA_4x4, i4+i8*4, h->dct.luma4x4[i4+i8*4], 16 );
1119             }
1120         }
1121
1122         block_residual_write_cabac( h, cb, DCT_CHROMA_AC, 16+i8, h->dct.luma4x4[16+i8]+1, 15 );
1123         block_residual_write_cabac( h, cb, DCT_CHROMA_AC, 20+i8, h->dct.luma4x4[20+i8]+1, 15 );
1124
1125         i8 += x264_pixel_size[i_pixel].h >> 3;
1126     }
1127 }
1128
1129 static void x264_subpartition_size_cabac( x264_t *h, x264_cabac_t *cb, int i4, int i_pixel )
1130 {
1131     int b_8x4 = i_pixel == PIXEL_8x4;
1132     block_residual_write_cabac( h, cb, DCT_LUMA_4x4, i4, h->dct.luma4x4[i4], 16 );
1133     if( i_pixel == PIXEL_4x4 )
1134         x264_cabac_mb_mvd( h, cb, 0, i4, 1, 1 );
1135     else
1136     {
1137         x264_cabac_mb_mvd( h, cb, 0, i4, 1+b_8x4, 2-b_8x4 );
1138         block_residual_write_cabac( h, cb, DCT_LUMA_4x4, i4+2-b_8x4, h->dct.luma4x4[i4+2-b_8x4], 16 );
1139     }
1140 }
1141
1142 static void x264_partition_i8x8_size_cabac( x264_t *h, x264_cabac_t *cb, int i8, int i_mode )
1143 {
1144     const int i_pred = x264_mb_predict_intra4x4_mode( h, 4*i8 );
1145     i_mode = x264_mb_pred_mode4x4_fix( i_mode );
1146     x264_cabac_mb_intra4x4_pred_mode( cb, i_pred, i_mode );
1147     if( h->mb.i_cbp_luma & (1 << i8) )
1148         block_residual_write_cabac_8x8( h, cb, 4*i8, h->dct.luma8x8[i8] );
1149 }
1150
1151 static void x264_partition_i4x4_size_cabac( x264_t *h, x264_cabac_t *cb, int i4, int i_mode )
1152 {
1153     const int i_pred = x264_mb_predict_intra4x4_mode( h, i4 );
1154     i_mode = x264_mb_pred_mode4x4_fix( i_mode );
1155     x264_cabac_mb_intra4x4_pred_mode( cb, i_pred, i_mode );
1156     block_residual_write_cabac( h, cb, DCT_LUMA_4x4, i4, h->dct.luma4x4[i4], 16 );
1157 }
1158
1159 static void x264_i8x8_chroma_size_cabac( x264_t *h, x264_cabac_t *cb )
1160 {
1161     x264_cabac_mb_intra_chroma_pred_mode( h, cb );
1162     x264_cabac_mb_cbp_chroma( h, cb );
1163     if( h->mb.i_cbp_chroma > 0 )
1164     {
1165         block_residual_write_cabac( h, cb, DCT_CHROMA_DC, 25, h->dct.chroma_dc[0], 4 );
1166         block_residual_write_cabac( h, cb, DCT_CHROMA_DC, 26, h->dct.chroma_dc[1], 4 );
1167
1168         if( h->mb.i_cbp_chroma == 2 )
1169         {
1170             int i;
1171             for( i = 16; i < 24; i++ )
1172                 block_residual_write_cabac( h, cb, DCT_CHROMA_AC, i, h->dct.luma4x4[i]+1, 15 );
1173         }
1174     }
1175 }
1176 #endif