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