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