]> git.sesse.net Git - x264/blob - encoder/cabac.c
fix cabac context for nonzero delta_qp of the 2nd mb of a frame in interlaced mode
[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 == 0 ? 0 : 1 ));
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 ? 0 : 1 ) );
72         }
73         x264_cabac_encode_decision( cb, ctx4, ( (i_pred / 2) ? 1 : 0 ));
74         x264_cabac_encode_decision( cb, ctx5, ( (i_pred % 2) ? 1 : 0 ));
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 void x264_cabac_mb_skip( x264_t *h, int b_skip )
385 {
386     int ctx = 0;
387
388     if( h->mb.i_mb_type_left >= 0 && !IS_SKIP( h->mb.i_mb_type_left ) )
389     {
390         ctx++;
391     }
392     if( h->mb.i_mb_type_top >= 0 && !IS_SKIP( h->mb.i_mb_type_top ) )
393     {
394         ctx++;
395     }
396
397     ctx += (h->sh.i_type == SLICE_TYPE_P) ? 11 : 24;
398     x264_cabac_encode_decision( &h->cabac, ctx, b_skip );
399 }
400
401 static inline void x264_cabac_mb_sub_p_partition( x264_cabac_t *cb, int i_sub )
402 {
403     if( i_sub == D_L0_8x8 )
404     {
405         x264_cabac_encode_decision( cb, 21, 1 );
406     }
407     else if( i_sub == D_L0_8x4 )
408     {
409         x264_cabac_encode_decision( cb, 21, 0 );
410         x264_cabac_encode_decision( cb, 22, 0 );
411     }
412     else if( i_sub == D_L0_4x8 )
413     {
414         x264_cabac_encode_decision( cb, 21, 0 );
415         x264_cabac_encode_decision( cb, 22, 1 );
416         x264_cabac_encode_decision( cb, 23, 1 );
417     }
418     else if( i_sub == D_L0_4x4 )
419     {
420         x264_cabac_encode_decision( cb, 21, 0 );
421         x264_cabac_encode_decision( cb, 22, 1 );
422         x264_cabac_encode_decision( cb, 23, 0 );
423     }
424 }
425
426 static inline void x264_cabac_mb_sub_b_partition( x264_cabac_t *cb, int i_sub )
427 {
428 #define WRITE_SUB_3(a,b,c) {\
429         x264_cabac_encode_decision( cb, 36, a );\
430         x264_cabac_encode_decision( cb, 37, b );\
431         x264_cabac_encode_decision( cb, 39, c );\
432     }
433 #define WRITE_SUB_5(a,b,c,d,e) {\
434         x264_cabac_encode_decision( cb, 36, a );\
435         x264_cabac_encode_decision( cb, 37, b );\
436         x264_cabac_encode_decision( cb, 38, c );\
437         x264_cabac_encode_decision( cb, 39, d );\
438         x264_cabac_encode_decision( cb, 39, e );\
439     }
440 #define WRITE_SUB_6(a,b,c,d,e,f) {\
441         WRITE_SUB_5(a,b,c,d,e)\
442         x264_cabac_encode_decision( cb, 39, f );\
443     }
444
445     switch( i_sub )
446     {
447         case D_DIRECT_8x8:
448             x264_cabac_encode_decision( cb, 36, 0 );
449             break;
450         case D_L0_8x8: WRITE_SUB_3(1,0,0); break;
451         case D_L1_8x8: WRITE_SUB_3(1,0,1); break;
452         case D_BI_8x8: WRITE_SUB_5(1,1,0,0,0); break;
453         case D_L0_8x4: WRITE_SUB_5(1,1,0,0,1); break;
454         case D_L0_4x8: WRITE_SUB_5(1,1,0,1,0); break;
455         case D_L1_8x4: WRITE_SUB_5(1,1,0,1,1); break;
456         case D_L1_4x8: WRITE_SUB_6(1,1,1,0,0,0); break;
457         case D_BI_8x4: WRITE_SUB_6(1,1,1,0,0,1); break;
458         case D_BI_4x8: WRITE_SUB_6(1,1,1,0,1,0); break;
459         case D_L0_4x4: WRITE_SUB_6(1,1,1,0,1,1); break;
460         case D_L1_4x4: WRITE_SUB_5(1,1,1,1,0); break;
461         case D_BI_4x4: WRITE_SUB_5(1,1,1,1,1); break;
462     }
463 }
464
465 static inline void x264_cabac_mb_transform_size( x264_t *h, x264_cabac_t *cb )
466 {
467     int ctx = 399 + h->mb.cache.i_neighbour_transform_size;
468     x264_cabac_encode_decision( cb, ctx, h->mb.b_transform_8x8 );
469 }
470
471 static inline void x264_cabac_mb_ref( x264_t *h, x264_cabac_t *cb, int i_list, int idx )
472 {
473     const int i8 = x264_scan8[idx];
474     const int i_refa = h->mb.cache.ref[i_list][i8 - 1];
475     const int i_refb = h->mb.cache.ref[i_list][i8 - 8];
476     int i_ref  = h->mb.cache.ref[i_list][i8];
477     int ctx  = 0;
478
479     if( i_refa > 0 && !h->mb.cache.skip[i8 - 1])
480         ctx++;
481     if( i_refb > 0 && !h->mb.cache.skip[i8 - 8])
482         ctx += 2;
483
484     while( i_ref > 0 )
485     {
486         x264_cabac_encode_decision( cb, 54 + ctx, 1 );
487         if( ctx < 4 )
488             ctx = 4;
489         else
490             ctx = 5;
491
492         i_ref--;
493     }
494     x264_cabac_encode_decision( cb, 54 + ctx, 0 );
495 }
496
497
498
499 static inline void x264_cabac_mb_mvd_cpn( x264_t *h, x264_cabac_t *cb, int i_list, int idx, int l, int mvd )
500 {
501     const int amvd = abs( h->mb.cache.mvd[i_list][x264_scan8[idx] - 1][l] ) +
502                      abs( h->mb.cache.mvd[i_list][x264_scan8[idx] - 8][l] );
503     const int i_abs = abs( mvd );
504     const int i_prefix = X264_MIN( i_abs, 9 );
505     const int ctxbase = (l == 0 ? 40 : 47);
506     int ctx;
507     int i;
508
509
510     if( amvd < 3 )
511         ctx = 0;
512     else if( amvd > 32 )
513         ctx = 2;
514     else
515         ctx = 1;
516
517     for( i = 0; i < i_prefix; i++ )
518     {
519         x264_cabac_encode_decision( cb, ctxbase + ctx, 1 );
520         if( ctx < 3 )
521             ctx = 3;
522         else if( ctx < 6 )
523             ctx++;
524     }
525     if( i_prefix < 9 )
526         x264_cabac_encode_decision( cb, ctxbase + ctx, 0 );
527     else
528         x264_cabac_encode_ue_bypass( cb, 3, i_abs - 9 );
529
530     /* sign */
531     if( mvd )
532         x264_cabac_encode_bypass( cb, mvd < 0 );
533 }
534
535 static inline void x264_cabac_mb_mvd( x264_t *h, x264_cabac_t *cb, int i_list, int idx, int width, int height )
536 {
537     int mvp[2];
538     int mdx, mdy;
539
540     /* Calculate mvd */
541     x264_mb_predict_mv( h, i_list, idx, width, mvp );
542     mdx = h->mb.cache.mv[i_list][x264_scan8[idx]][0] - mvp[0];
543     mdy = h->mb.cache.mv[i_list][x264_scan8[idx]][1] - mvp[1];
544
545     /* encode */
546     x264_cabac_mb_mvd_cpn( h, cb, i_list, idx, 0, mdx );
547     x264_cabac_mb_mvd_cpn( h, cb, i_list, idx, 1, mdy );
548
549     /* save value */
550     x264_macroblock_cache_mvd( h, block_idx_x[idx], block_idx_y[idx], width, height, i_list, mdx, mdy );
551 }
552
553 static inline void x264_cabac_mb8x8_mvd( x264_t *h, x264_cabac_t *cb, int i_list, int i )
554 {
555     if( !x264_mb_partition_listX_table[i_list][ h->mb.i_sub_partition[i] ] )
556         return;
557
558     switch( h->mb.i_sub_partition[i] )
559     {
560         case D_L0_8x8:
561         case D_L1_8x8:
562         case D_BI_8x8:
563             x264_cabac_mb_mvd( h, cb, i_list, 4*i, 2, 2 );
564             break;
565         case D_L0_8x4:
566         case D_L1_8x4:
567         case D_BI_8x4:
568             x264_cabac_mb_mvd( h, cb, i_list, 4*i+0, 2, 1 );
569             x264_cabac_mb_mvd( h, cb, i_list, 4*i+2, 2, 1 );
570             break;
571         case D_L0_4x8:
572         case D_L1_4x8:
573         case D_BI_4x8:
574             x264_cabac_mb_mvd( h, cb, i_list, 4*i+0, 1, 2 );
575             x264_cabac_mb_mvd( h, cb, i_list, 4*i+1, 1, 2 );
576             break;
577         case D_L0_4x4:
578         case D_L1_4x4:
579         case D_BI_4x4:
580             x264_cabac_mb_mvd( h, cb, i_list, 4*i+0, 1, 1 );
581             x264_cabac_mb_mvd( h, cb, i_list, 4*i+1, 1, 1 );
582             x264_cabac_mb_mvd( h, cb, i_list, 4*i+2, 1, 1 );
583             x264_cabac_mb_mvd( h, cb, i_list, 4*i+3, 1, 1 );
584             break;
585     }
586 }
587
588 static int x264_cabac_mb_cbf_ctxidxinc( x264_t *h, int i_cat, int i_idx )
589 {
590     int i_mba_xy = -1;
591     int i_mbb_xy = -1;
592     int i_nza = 0;
593     int i_nzb = 0;
594     int ctx;
595
596     if( i_cat == DCT_LUMA_DC )
597     {
598         if( h->mb.i_neighbour & MB_LEFT )
599         {
600             i_mba_xy = h->mb.i_mb_xy - 1;
601             if( h->mb.i_mb_type_left == I_16x16 )
602                 i_nza = h->mb.cbp[i_mba_xy] & 0x100;
603         }
604         if( h->mb.i_neighbour & MB_TOP )
605         {
606             i_mbb_xy = h->mb.i_mb_top_xy;
607             if( h->mb.i_mb_type_top == I_16x16 )
608                 i_nzb = h->mb.cbp[i_mbb_xy] & 0x100;
609         }
610     }
611     else if( i_cat == DCT_LUMA_AC || i_cat == DCT_LUMA_4x4 )
612     {
613         if( i_idx & ~10 ) // block_idx_x > 0
614             i_mba_xy = h->mb.i_mb_xy;
615         else if( h->mb.i_neighbour & MB_LEFT )
616             i_mba_xy = h->mb.i_mb_xy - 1;
617
618         if( i_idx & ~5 ) // block_idx_y > 0
619             i_mbb_xy = h->mb.i_mb_xy;
620         else if( h->mb.i_neighbour & MB_TOP )
621             i_mbb_xy = h->mb.i_mb_top_xy;
622
623         /* no need to test for skip/pcm */
624         if( i_mba_xy >= 0 )
625             i_nza = h->mb.cache.non_zero_count[x264_scan8[i_idx] - 1];
626         if( i_mbb_xy >= 0 )
627             i_nzb = h->mb.cache.non_zero_count[x264_scan8[i_idx] - 8];
628     }
629     else if( i_cat == DCT_CHROMA_DC )
630     {
631         /* no need to test skip/pcm */
632         if( h->mb.i_neighbour & MB_LEFT )
633         {
634             i_mba_xy = h->mb.i_mb_xy - 1;
635             i_nza = h->mb.cbp[i_mba_xy] & (0x200 << i_idx);
636         }
637         if( h->mb.i_neighbour & MB_TOP )
638         {
639             i_mbb_xy = h->mb.i_mb_top_xy;
640             i_nzb = h->mb.cbp[i_mbb_xy] & (0x200 << i_idx);
641         }
642     }
643     else if( i_cat == DCT_CHROMA_AC )
644     {
645         if( i_idx & 1 )
646             i_mba_xy = h->mb.i_mb_xy;
647         else if( h->mb.i_neighbour & MB_LEFT )
648             i_mba_xy = h->mb.i_mb_xy - 1;
649
650         if( i_idx & 2 )
651             i_mbb_xy = h->mb.i_mb_xy;
652         else if( h->mb.i_neighbour & MB_TOP )
653             i_mbb_xy = h->mb.i_mb_top_xy;
654
655         /* no need to test skip/pcm */
656         if( i_mba_xy >= 0 )
657             i_nza = h->mb.cache.non_zero_count[x264_scan8[16+i_idx] - 1];
658         if( i_mbb_xy >= 0 )
659             i_nzb = h->mb.cache.non_zero_count[x264_scan8[16+i_idx] - 8];
660     }
661
662     if( IS_INTRA( h->mb.i_type ) )
663     {
664         if( i_mba_xy < 0 )
665             i_nza = 1;
666         if( i_mbb_xy < 0 )
667             i_nzb = 1;
668     }
669
670     ctx = 4 * i_cat;
671     if( i_nza )
672         ctx += 1;
673     if( i_nzb )
674         ctx += 2;
675     return ctx;
676 }
677
678
679 static const int significant_coeff_flag_offset[2][6] = {
680     { 105, 120, 134, 149, 152, 402 },
681     { 277, 292, 306, 321, 324, 436 }
682 };
683 static const int last_coeff_flag_offset[2][6] = {
684     { 166, 181, 195, 210, 213, 417 },
685     { 338, 353, 367, 382, 385, 451 }
686 };
687 static const int coeff_abs_level_m1_offset[6] =
688     { 227, 237, 247, 257, 266, 426 };
689 static const int significant_coeff_flag_offset_8x8[2][63] =
690 {{
691     0, 1, 2, 3, 4, 5, 5, 4, 4, 3, 3, 4, 4, 4, 5, 5,
692     4, 4, 4, 4, 3, 3, 6, 7, 7, 7, 8, 9,10, 9, 8, 7,
693     7, 6,11,12,13,11, 6, 7, 8, 9,14,10, 9, 8, 6,11,
694    12,13,11, 6, 9,14,10, 9,11,12,13,11,14,10,12
695 },{
696     0, 1, 1, 2, 2, 3, 3, 4, 5, 6, 7, 7, 7, 8, 4, 5,
697     6, 9,10,10, 8,11,12,11, 9, 9,10,10, 8,11,12,11,
698     9, 9,10,10, 8,11,12,11, 9, 9,10,10, 8,13,13, 9,
699     9,10,10, 8,13,13, 9, 9,10,10,14,14,14,14,14
700 }};
701 static const int last_coeff_flag_offset_8x8[63] = {
702     0, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1,
703     2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2,
704     3, 3, 3, 3, 3, 3, 3, 3, 4, 4, 4, 4, 4, 4, 4, 4,
705     5, 5, 5, 5, 6, 6, 6, 6, 7, 7, 7, 7, 8, 8, 8
706 };
707 static const int identity[16] =
708     { 0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15 };
709
710 static void block_residual_write_cabac( x264_t *h, x264_cabac_t *cb, int i_ctxBlockCat, int i_idx, int *l, int i_count )
711 {
712     const int i_ctx_sig = significant_coeff_flag_offset[h->mb.b_interlaced][i_ctxBlockCat];
713     const int i_ctx_last = last_coeff_flag_offset[h->mb.b_interlaced][i_ctxBlockCat];
714     const int i_ctx_level = coeff_abs_level_m1_offset[i_ctxBlockCat];
715
716     int i_coeff_abs_m1[64];
717     int i_coeff_sign[64];
718     int i_coeff = 0;
719     int i_last  = 0;
720     int i_sigmap_size;
721
722     int i_abslevel1 = 0;
723     int i_abslevelgt1 = 0;
724
725     int i;
726
727     const int *significant_coeff_flag_offset;
728     const int *last_coeff_flag_offset;
729
730     /* i_ctxBlockCat: 0-> DC 16x16  i_idx = 0
731      *                1-> AC 16x16  i_idx = luma4x4idx
732      *                2-> Luma4x4   i_idx = luma4x4idx
733      *                3-> DC Chroma i_idx = iCbCr
734      *                4-> AC Chroma i_idx = 4 * iCbCr + chroma4x4idx
735      *                5-> Luma8x8   i_idx = luma8x8idx
736      */
737
738     for( i = 0; i < i_count; i++ )
739     {
740         if( l[i] != 0 )
741         {
742             i_coeff_abs_m1[i_coeff] = abs( l[i] ) - 1;
743             i_coeff_sign[i_coeff]   = ( l[i] < 0 );
744             i_coeff++;
745
746             i_last = i;
747         }
748     }
749
750     if( i_count != 64 )
751     {
752         /* coded block flag */
753         x264_cabac_encode_decision( cb, 85 + x264_cabac_mb_cbf_ctxidxinc( h, i_ctxBlockCat, i_idx ), i_coeff != 0 );
754         if( i_coeff == 0 )
755             return;
756     }
757
758     significant_coeff_flag_offset = (i_ctxBlockCat == DCT_LUMA_8x8)
759                                   ? significant_coeff_flag_offset_8x8[h->mb.b_interlaced]
760                                   : identity;
761     last_coeff_flag_offset = (i_ctxBlockCat == DCT_LUMA_8x8)
762                            ? last_coeff_flag_offset_8x8 : identity;
763
764     i_sigmap_size = X264_MIN( i_last+1, i_count-1 );
765     for( i = 0; i < i_sigmap_size; i++ )
766     {
767         x264_cabac_encode_decision( cb, i_ctx_sig + significant_coeff_flag_offset[i], l[i] != 0 );
768         if( l[i] != 0 )
769             x264_cabac_encode_decision( cb, i_ctx_last + last_coeff_flag_offset[i], i == i_last );
770     }
771
772     for( i = i_coeff - 1; i >= 0; i-- )
773     {
774         /* write coeff_abs - 1 */
775         const int i_prefix = X264_MIN( i_coeff_abs_m1[i], 14 );
776         const int i_ctxIdxInc = (i_abslevelgt1 ? 0 : X264_MIN( 4, i_abslevel1 + 1 )) + i_ctx_level;
777         x264_cabac_encode_decision( cb, i_ctxIdxInc, i_prefix != 0 );
778
779         if( i_prefix != 0 )
780         {
781             const int i_ctxIdxInc = 5 + X264_MIN( 4, i_abslevelgt1 ) + i_ctx_level;
782 #ifdef RDO_SKIP_BS
783             cb->f8_bits_encoded += cabac_prefix_size[i_prefix][cb->state[i_ctxIdxInc]];
784             cb->state[i_ctxIdxInc] = cabac_prefix_transition[i_prefix][cb->state[i_ctxIdxInc]];
785 #else
786             int j;
787             for( j = 0; j < i_prefix - 1; j++ )
788                 x264_cabac_encode_decision( cb, i_ctxIdxInc, 1 );
789             if( i_prefix < 14 )
790                 x264_cabac_encode_decision( cb, i_ctxIdxInc, 0 );
791 #endif
792             if( i_prefix >= 14 )
793                 x264_cabac_encode_ue_bypass( cb, 0, i_coeff_abs_m1[i] - 14 );
794
795             i_abslevelgt1++;
796         }
797         else
798             i_abslevel1++;
799
800         /* write sign */
801 #ifdef RDO_SKIP_BS
802         if( i_prefix == 0 )
803 #endif
804         x264_cabac_encode_bypass( cb, i_coeff_sign[i] );
805     }
806 }
807
808
809
810 void x264_macroblock_write_cabac( x264_t *h, x264_cabac_t *cb )
811 {
812     const int i_mb_type = h->mb.i_type;
813     int i_list;
814     int i;
815
816 #ifndef RDO_SKIP_BS
817     const int i_mb_pos_start = x264_cabac_pos( cb );
818     int       i_mb_pos_tex;
819 #endif
820
821     /* Write the MB type */
822     x264_cabac_mb_type( h, cb );
823
824     /* PCM special block type UNTESTED */
825     if( i_mb_type == I_PCM )
826     {
827 #ifdef RDO_SKIP_BS
828         cb->f8_bits_encoded += (384*8) << 8;
829 #else
830         if( cb->p + 385 >= cb->p_end )
831             return; //FIXME throw an error
832         /* Luma */
833         for( i = 0; i < 16; i++ )
834         {
835             memcpy( cb->p, h->fenc->plane[0] + i*h->mb.pic.i_stride[0], 16 );
836             cb->p += 16;
837         }
838         /* Cb */
839         for( i = 0; i < 8; i++ )
840         {
841             memcpy( cb->p, h->fenc->plane[1] + i*h->mb.pic.i_stride[1], 8 );
842             cb->p += 8;
843         }
844         /* Cr */
845         for( i = 0; i < 8; i++ )
846         {
847             memcpy( cb->p, h->fenc->plane[2] + i*h->mb.pic.i_stride[2], 8 );
848             cb->p += 8;
849         }
850         x264_cabac_encode_init( cb, cb->p, cb->p_end );
851 #endif
852         return;
853     }
854
855     if( IS_INTRA( i_mb_type ) )
856     {
857         if( h->pps->b_transform_8x8_mode && i_mb_type != I_16x16 )
858             x264_cabac_mb_transform_size( h, cb );
859
860         if( i_mb_type != I_16x16 )
861         {
862             int di = (i_mb_type == I_8x8) ? 4 : 1;
863             for( i = 0; i < 16; i += di )
864             {
865                 const int i_pred = x264_mb_predict_intra4x4_mode( h, i );
866                 const int i_mode = x264_mb_pred_mode4x4_fix( h->mb.cache.intra4x4_pred_mode[x264_scan8[i]] );
867                 x264_cabac_mb_intra4x4_pred_mode( cb, i_pred, i_mode );
868             }
869         }
870
871         x264_cabac_mb_intra_chroma_pred_mode( h, cb );
872     }
873     else if( i_mb_type == P_L0 )
874     {
875         if( h->mb.i_partition == D_16x16 )
876         {
877             if( h->mb.pic.i_fref[0] > 1 )
878             {
879                 x264_cabac_mb_ref( h, cb, 0, 0 );
880             }
881             x264_cabac_mb_mvd( h, cb, 0, 0, 4, 4 );
882         }
883         else if( h->mb.i_partition == D_16x8 )
884         {
885             if( h->mb.pic.i_fref[0] > 1 )
886             {
887                 x264_cabac_mb_ref( h, cb, 0, 0 );
888                 x264_cabac_mb_ref( h, cb, 0, 8 );
889             }
890             x264_cabac_mb_mvd( h, cb, 0, 0, 4, 2 );
891             x264_cabac_mb_mvd( h, cb, 0, 8, 4, 2 );
892         }
893         else if( h->mb.i_partition == D_8x16 )
894         {
895             if( h->mb.pic.i_fref[0] > 1 )
896             {
897                 x264_cabac_mb_ref( h, cb, 0, 0 );
898                 x264_cabac_mb_ref( h, cb, 0, 4 );
899             }
900             x264_cabac_mb_mvd( h, cb, 0, 0, 2, 4 );
901             x264_cabac_mb_mvd( h, cb, 0, 4, 2, 4 );
902         }
903     }
904     else if( i_mb_type == P_8x8 )
905     {
906         /* sub mb type */
907         x264_cabac_mb_sub_p_partition( cb, h->mb.i_sub_partition[0] );
908         x264_cabac_mb_sub_p_partition( cb, h->mb.i_sub_partition[1] );
909         x264_cabac_mb_sub_p_partition( cb, h->mb.i_sub_partition[2] );
910         x264_cabac_mb_sub_p_partition( cb, h->mb.i_sub_partition[3] );
911
912         /* ref 0 */
913         if( h->mb.pic.i_fref[0] > 1 )
914         {
915             x264_cabac_mb_ref( h, cb, 0, 0 );
916             x264_cabac_mb_ref( h, cb, 0, 4 );
917             x264_cabac_mb_ref( h, cb, 0, 8 );
918             x264_cabac_mb_ref( h, cb, 0, 12 );
919         }
920
921         for( i = 0; i < 4; i++ )
922             x264_cabac_mb8x8_mvd( h, cb, 0, i );
923     }
924     else if( i_mb_type == B_8x8 )
925     {
926         /* sub mb type */
927         x264_cabac_mb_sub_b_partition( cb, h->mb.i_sub_partition[0] );
928         x264_cabac_mb_sub_b_partition( cb, h->mb.i_sub_partition[1] );
929         x264_cabac_mb_sub_b_partition( cb, h->mb.i_sub_partition[2] );
930         x264_cabac_mb_sub_b_partition( cb, h->mb.i_sub_partition[3] );
931
932         /* ref */
933         for( i_list = 0; i_list < 2; i_list++ )
934         {
935             if( ( i_list ? h->mb.pic.i_fref[1] : h->mb.pic.i_fref[0] ) == 1 )
936                 continue;
937             for( i = 0; i < 4; i++ )
938                 if( x264_mb_partition_listX_table[i_list][ h->mb.i_sub_partition[i] ] )
939                     x264_cabac_mb_ref( h, cb, i_list, 4*i );
940         }
941
942         for( i = 0; i < 4; i++ )
943             x264_cabac_mb8x8_mvd( h, cb, 0, i );
944         for( i = 0; i < 4; i++ )
945             x264_cabac_mb8x8_mvd( h, cb, 1, i );
946     }
947     else if( i_mb_type != B_DIRECT )
948     {
949         /* All B mode */
950         int b_list[2][2];
951
952         /* init ref list utilisations */
953         for( i = 0; i < 2; i++ )
954         {
955             b_list[0][i] = x264_mb_type_list0_table[i_mb_type][i];
956             b_list[1][i] = x264_mb_type_list1_table[i_mb_type][i];
957         }
958
959         for( i_list = 0; i_list < 2; i_list++ )
960         {
961             const int i_ref_max = i_list == 0 ? h->mb.pic.i_fref[0] : h->mb.pic.i_fref[1];
962
963             if( i_ref_max > 1 )
964             {
965                 if( h->mb.i_partition == D_16x16 )
966                 {
967                     if( b_list[i_list][0] ) x264_cabac_mb_ref( h, cb, i_list, 0 );
968                 }
969                 else if( h->mb.i_partition == D_16x8 )
970                 {
971                     if( b_list[i_list][0] ) x264_cabac_mb_ref( h, cb, i_list, 0 );
972                     if( b_list[i_list][1] ) x264_cabac_mb_ref( h, cb, i_list, 8 );
973                 }
974                 else if( h->mb.i_partition == D_8x16 )
975                 {
976                     if( b_list[i_list][0] ) x264_cabac_mb_ref( h, cb, i_list, 0 );
977                     if( b_list[i_list][1] ) x264_cabac_mb_ref( h, cb, i_list, 4 );
978                 }
979             }
980         }
981         for( i_list = 0; i_list < 2; i_list++ )
982         {
983             if( h->mb.i_partition == D_16x16 )
984             {
985                 if( b_list[i_list][0] ) x264_cabac_mb_mvd( h, cb, i_list, 0, 4, 4 );
986             }
987             else if( h->mb.i_partition == D_16x8 )
988             {
989                 if( b_list[i_list][0] ) x264_cabac_mb_mvd( h, cb, i_list, 0, 4, 2 );
990                 if( b_list[i_list][1] ) x264_cabac_mb_mvd( h, cb, i_list, 8, 4, 2 );
991             }
992             else if( h->mb.i_partition == D_8x16 )
993             {
994                 if( b_list[i_list][0] ) x264_cabac_mb_mvd( h, cb, i_list, 0, 2, 4 );
995                 if( b_list[i_list][1] ) x264_cabac_mb_mvd( h, cb, i_list, 4, 2, 4 );
996             }
997         }
998     }
999
1000 #ifndef RDO_SKIP_BS
1001     i_mb_pos_tex = x264_cabac_pos( cb );
1002     h->stat.frame.i_hdr_bits += i_mb_pos_tex - i_mb_pos_start;
1003 #endif
1004
1005     if( i_mb_type != I_16x16 )
1006     {
1007         x264_cabac_mb_cbp_luma( h, cb );
1008         x264_cabac_mb_cbp_chroma( h, cb );
1009     }
1010
1011     if( x264_mb_transform_8x8_allowed( h ) && h->mb.i_cbp_luma )
1012     {
1013         x264_cabac_mb_transform_size( h, cb );
1014     }
1015
1016     if( h->mb.i_cbp_luma > 0 || h->mb.i_cbp_chroma > 0 || i_mb_type == I_16x16 )
1017     {
1018         x264_cabac_mb_qp_delta( h, cb );
1019
1020         /* write residual */
1021         if( i_mb_type == I_16x16 )
1022         {
1023             /* DC Luma */
1024             block_residual_write_cabac( h, cb, DCT_LUMA_DC, 0, h->dct.luma16x16_dc, 16 );
1025
1026             /* AC Luma */
1027             if( h->mb.i_cbp_luma != 0 )
1028                 for( i = 0; i < 16; i++ )
1029                     block_residual_write_cabac( h, cb, DCT_LUMA_AC, i, h->dct.block[i].residual_ac, 15 );
1030         }
1031         else if( h->mb.b_transform_8x8 )
1032         {
1033             for( i = 0; i < 4; i++ )
1034                 if( h->mb.i_cbp_luma & ( 1 << i ) )
1035                     block_residual_write_cabac( h, cb, DCT_LUMA_8x8, i, h->dct.luma8x8[i], 64 );
1036         }
1037         else
1038         {
1039             for( i = 0; i < 16; i++ )
1040                 if( h->mb.i_cbp_luma & ( 1 << ( i / 4 ) ) )
1041                     block_residual_write_cabac( h, cb, DCT_LUMA_4x4, i, h->dct.block[i].luma4x4, 16 );
1042         }
1043
1044         if( h->mb.i_cbp_chroma &0x03 )    /* Chroma DC residual present */
1045         {
1046             block_residual_write_cabac( h, cb, DCT_CHROMA_DC, 0, h->dct.chroma_dc[0], 4 );
1047             block_residual_write_cabac( h, cb, DCT_CHROMA_DC, 1, h->dct.chroma_dc[1], 4 );
1048         }
1049         if( h->mb.i_cbp_chroma&0x02 ) /* Chroma AC residual present */
1050         {
1051             for( i = 0; i < 8; i++ )
1052                 block_residual_write_cabac( h, cb, DCT_CHROMA_AC, i, h->dct.block[16+i].residual_ac, 15 );
1053         }
1054     }
1055
1056 #ifndef RDO_SKIP_BS
1057     if( IS_INTRA( i_mb_type ) )
1058         h->stat.frame.i_itex_bits += x264_cabac_pos( cb ) - i_mb_pos_tex;
1059     else
1060         h->stat.frame.i_ptex_bits += x264_cabac_pos( cb ) - i_mb_pos_tex;
1061 #endif
1062 }
1063
1064 #ifdef RDO_SKIP_BS
1065 /*****************************************************************************
1066  * RD only; doesn't generate a valid bitstream
1067  * doesn't write cbp or chroma dc (I don't know how much this matters)
1068  * works on all partition sizes except 16x16
1069  * for sub8x8, call once per 8x8 block
1070  *****************************************************************************/
1071 void x264_partition_size_cabac( x264_t *h, x264_cabac_t *cb, int i8, int i_pixel )
1072 {
1073     const int i_mb_type = h->mb.i_type;
1074     int j;
1075
1076     if( i_mb_type == P_8x8 )
1077     {
1078         x264_cabac_mb_sub_p_partition( cb, h->mb.i_sub_partition[i8] );
1079         if( h->mb.pic.i_fref[0] > 1 )
1080             x264_cabac_mb_ref( h, cb, 0, 4*i8 );
1081         x264_cabac_mb8x8_mvd( h, cb, 0, i8 );
1082     }
1083     else if( i_mb_type == P_L0 )
1084     {
1085         if( h->mb.pic.i_fref[0] > 1 )
1086             x264_cabac_mb_ref( h, cb, 0, 4*i8 );
1087         if( h->mb.i_partition == D_16x8 )
1088             x264_cabac_mb_mvd( h, cb, 0, 4*i8, 4, 2 );
1089         else //8x16
1090             x264_cabac_mb_mvd( h, cb, 0, 4*i8, 2, 4 );
1091     }
1092     else if( i_mb_type == B_8x8 )
1093     {
1094         x264_cabac_mb_sub_b_partition( cb, h->mb.i_sub_partition[i8] );
1095
1096         if( h->mb.pic.i_fref[0] > 1
1097             && x264_mb_partition_listX_table[0][ h->mb.i_sub_partition[i8] ] )
1098             x264_cabac_mb_ref( h, cb, 0, 4*i8 );
1099         if( h->mb.pic.i_fref[1] > 1
1100             && x264_mb_partition_listX_table[1][ h->mb.i_sub_partition[i8] ] )
1101             x264_cabac_mb_ref( h, cb, 1, 4*i8 );
1102
1103         x264_cabac_mb8x8_mvd( h, cb, 0, i8 );
1104         x264_cabac_mb8x8_mvd( h, cb, 1, i8 );
1105     }
1106     else
1107     {
1108         x264_log(h, X264_LOG_ERROR, "invalid/unhandled mb_type\n" );
1109         return;
1110     }
1111
1112     for( j = (i_pixel < PIXEL_8x8); j >= 0; j-- )
1113     {
1114         if( h->mb.i_cbp_luma & (1 << i8) )
1115         {
1116             if( h->mb.b_transform_8x8 )
1117                 block_residual_write_cabac( h, cb, DCT_LUMA_8x8, i8, h->dct.luma8x8[i8], 64 );
1118             else
1119             {
1120                 int i4;
1121                 for( i4 = 0; i4 < 4; i4++ )
1122                     block_residual_write_cabac( h, cb, DCT_LUMA_4x4, i4+i8*4, h->dct.block[i4+i8*4].luma4x4, 16 );
1123             }
1124         }
1125
1126         block_residual_write_cabac( h, cb, DCT_CHROMA_AC, i8,   h->dct.block[16+i8  ].residual_ac, 15 );
1127         block_residual_write_cabac( h, cb, DCT_CHROMA_AC, i8+4, h->dct.block[16+i8+4].residual_ac, 15 );
1128
1129         i8 += x264_pixel_size[i_pixel].h >> 3;
1130     }
1131 }
1132
1133 static void x264_partition_i8x8_size_cabac( x264_t *h, x264_cabac_t *cb, int i8, int i_mode )
1134 {
1135     const int i_pred = x264_mb_predict_intra4x4_mode( h, 4*i8 );
1136     i_mode = x264_mb_pred_mode4x4_fix( i_mode );
1137     x264_cabac_mb_intra4x4_pred_mode( cb, i_pred, i_mode );
1138     block_residual_write_cabac( h, cb, DCT_LUMA_8x8, 4*i8, h->dct.luma8x8[i8], 64 );
1139 }
1140
1141 static void x264_partition_i4x4_size_cabac( x264_t *h, x264_cabac_t *cb, int i4, int i_mode )
1142 {
1143     const int i_pred = x264_mb_predict_intra4x4_mode( h, i4 );
1144     i_mode = x264_mb_pred_mode4x4_fix( i_mode );
1145     x264_cabac_mb_intra4x4_pred_mode( cb, i_pred, i_mode );
1146     block_residual_write_cabac( h, cb, DCT_LUMA_4x4, i4, h->dct.block[i4].luma4x4, 16 );
1147 }
1148
1149 static void x264_i8x8_chroma_size_cabac( x264_t *h, x264_cabac_t *cb )
1150 {
1151     x264_cabac_mb_intra_chroma_pred_mode( h, cb );
1152     if( h->mb.i_cbp_chroma > 0 )
1153     {
1154         block_residual_write_cabac( h, cb, DCT_CHROMA_DC, 0, h->dct.chroma_dc[0], 4 );
1155         block_residual_write_cabac( h, cb, DCT_CHROMA_DC, 1, h->dct.chroma_dc[1], 4 );
1156
1157         if( h->mb.i_cbp_chroma == 2 )
1158         {
1159             int i;
1160             for( i = 0; i < 8; i++ )
1161                 block_residual_write_cabac( h, cb, DCT_CHROMA_AC, i, h->dct.block[16+i].residual_ac, 15 );
1162         }
1163     }
1164 }
1165 #endif