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