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