]> git.sesse.net Git - x264/blob - encoder/cabac.c
10l in r443 (p4x4 chroma)
[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.i_type == SLICE_TYPE_I )
87     {
88         int ctx = 0;
89         if( h->mb.i_mb_type_left >= 0 && h->mb.i_mb_type_left != I_4x4 )
90         {
91             ctx++;
92         }
93         if( h->mb.i_mb_type_top >= 0 && h->mb.i_mb_type_top != I_4x4 )
94         {
95             ctx++;
96         }
97
98         x264_cabac_mb_type_intra( h, cb, i_mb_type, 3+ctx, 3+3, 3+4, 3+5, 3+6, 3+7 );
99     }
100     else if( h->sh.i_type == SLICE_TYPE_P )
101     {
102         /* prefix: 14, suffix: 17 */
103         if( i_mb_type == P_L0 )
104         {
105             if( h->mb.i_partition == D_16x16 )
106             {
107                 x264_cabac_encode_decision( cb, 14, 0 );
108                 x264_cabac_encode_decision( cb, 15, 0 );
109                 x264_cabac_encode_decision( cb, 16, 0 );
110             }
111             else if( h->mb.i_partition == D_16x8 )
112             {
113                 x264_cabac_encode_decision( cb, 14, 0 );
114                 x264_cabac_encode_decision( cb, 15, 1 );
115                 x264_cabac_encode_decision( cb, 17, 1 );
116             }
117             else if( h->mb.i_partition == D_8x16 )
118             {
119                 x264_cabac_encode_decision( cb, 14, 0 );
120                 x264_cabac_encode_decision( cb, 15, 1 );
121                 x264_cabac_encode_decision( cb, 17, 0 );
122             }
123         }
124         else if( i_mb_type == P_8x8 )
125         {
126             x264_cabac_encode_decision( cb, 14, 0 );
127             x264_cabac_encode_decision( cb, 15, 0 );
128             x264_cabac_encode_decision( cb, 16, 1 );
129         }
130         else /* intra */
131         {
132             /* prefix */
133             x264_cabac_encode_decision( cb, 14, 1 );
134
135             /* suffix */
136             x264_cabac_mb_type_intra( h, cb, i_mb_type, 17+0, 17+1, 17+2, 17+2, 17+3, 17+3 );
137         }
138     }
139     else if( h->sh.i_type == SLICE_TYPE_B )
140     {
141         int ctx = 0;
142         if( h->mb.i_mb_type_left >= 0 && h->mb.i_mb_type_left != B_SKIP && h->mb.i_mb_type_left != B_DIRECT )
143         {
144             ctx++;
145         }
146         if( h->mb.i_mb_type_top >= 0 && h->mb.i_mb_type_top != B_SKIP && h->mb.i_mb_type_top != B_DIRECT )
147         {
148             ctx++;
149         }
150
151         if( i_mb_type == B_DIRECT )
152         {
153             x264_cabac_encode_decision( cb, 27+ctx, 0 );
154         }
155         else if( i_mb_type == B_8x8 )
156         {
157             x264_cabac_encode_decision( cb, 27+ctx, 1 );
158             x264_cabac_encode_decision( cb, 27+3,   1 );
159             x264_cabac_encode_decision( cb, 27+4,   1 );
160
161             x264_cabac_encode_decision( cb, 27+5,   1 );
162             x264_cabac_encode_decision( cb, 27+5,   1 );
163             x264_cabac_encode_decision( cb, 27+5,   1 );
164         }
165         else if( IS_INTRA( i_mb_type ) )
166         {
167             /* prefix */
168             x264_cabac_encode_decision( cb, 27+ctx, 1 );
169             x264_cabac_encode_decision( cb, 27+3,   1 );
170             x264_cabac_encode_decision( cb, 27+4,   1 );
171
172             x264_cabac_encode_decision( cb, 27+5,   1 );
173             x264_cabac_encode_decision( cb, 27+5,   0 );
174             x264_cabac_encode_decision( cb, 27+5,   1 );
175
176             /* suffix */
177             x264_cabac_mb_type_intra( h, cb, i_mb_type, 32+0, 32+1, 32+2, 32+2, 32+3, 32+3 );
178         }
179         else
180         {
181             static const int i_mb_len[9*3] =
182             {
183                 6, 6, 3,    /* L0 L0 */
184                 6, 6, 0,    /* L0 L1 */
185                 7, 7, 0,    /* L0 BI */
186                 6, 6, 0,    /* L1 L0 */
187                 6, 6, 3,    /* L1 L1 */
188                 7, 7, 0,    /* L1 BI */
189                 7, 7, 0,    /* BI L0 */
190                 7, 7, 0,    /* BI L1 */
191                 7, 7, 6,    /* BI BI */
192             };
193             static const int i_mb_bits[9*3][7] =
194             {
195                 { 1,1,0,0,0,1   }, { 1,1,0,0,1,0,  }, { 1,0,0 },       /* L0 L0 */
196                 { 1,1,0,1,0,1   }, { 1,1,0,1,1,0   }, {0},             /* L0 L1 */
197                 { 1,1,1,0,0,0,0 }, { 1,1,1,0,0,0,1 }, {0},             /* L0 BI */
198                 { 1,1,0,1,1,1   }, { 1,1,1,1,1,0   }, {0},             /* L1 L0 */
199                 { 1,1,0,0,1,1   }, { 1,1,0,1,0,0   }, { 1,0,1 },       /* L1 L1 */
200                 { 1,1,1,0,0,1,0 }, { 1,1,1,0,0,1,1 }, {0},             /* L1 BI */
201                 { 1,1,1,0,1,0,0 }, { 1,1,1,0,1,0,1 }, {0},             /* BI L0 */
202                 { 1,1,1,0,1,1,0 }, { 1,1,1,0,1,1,1 }, {0},             /* BI L1 */
203                 { 1,1,1,1,0,0,0 }, { 1,1,1,1,0,0,1 }, { 1,1,0,0,0,0 }, /* BI BI */
204             };
205
206             const int idx = (i_mb_type - B_L0_L0) * 3 + (h->mb.i_partition - D_16x8);
207             int i;
208
209             x264_cabac_encode_decision( cb, 27+ctx, i_mb_bits[idx][0] );
210             x264_cabac_encode_decision( cb, 27+3,   i_mb_bits[idx][1] );
211             x264_cabac_encode_decision( cb, 27+5-i_mb_bits[idx][1], i_mb_bits[idx][2] );
212             for( i = 3; i < i_mb_len[idx]; i++ )
213                 x264_cabac_encode_decision( cb, 27+5, i_mb_bits[idx][i] );
214         }
215     }
216     else
217     {
218         x264_log(h, X264_LOG_ERROR, "unknown SLICE_TYPE unsupported in x264_macroblock_write_cabac\n" );
219     }
220 }
221
222 static void x264_cabac_mb_intra4x4_pred_mode( x264_cabac_t *cb, int i_pred, int i_mode )
223 {
224     if( i_pred == i_mode )
225     {
226         /* b_prev_intra4x4_pred_mode */
227         x264_cabac_encode_decision( cb, 68, 1 );
228     }
229     else
230     {
231         /* b_prev_intra4x4_pred_mode */
232         x264_cabac_encode_decision( cb, 68, 0 );
233         if( i_mode > i_pred  )
234         {
235             i_mode--;
236         }
237         x264_cabac_encode_decision( cb, 69, (i_mode     )&0x01 );
238         x264_cabac_encode_decision( cb, 69, (i_mode >> 1)&0x01 );
239         x264_cabac_encode_decision( cb, 69, (i_mode >> 2)&0x01 );
240     }
241 }
242
243 static void x264_cabac_mb_intra_chroma_pred_mode( x264_t *h, x264_cabac_t *cb )
244 {
245     const int i_mode = x264_mb_pred_mode8x8c_fix[ h->mb.i_chroma_pred_mode ];
246     int       ctx = 0;
247
248     /* No need to test for I4x4 or I_16x16 as cache_save handle that */
249     if( (h->mb.i_neighbour & MB_LEFT) && h->mb.chroma_pred_mode[h->mb.i_mb_xy - 1] != 0 )
250     {
251         ctx++;
252     }
253     if( (h->mb.i_neighbour & MB_TOP) && h->mb.chroma_pred_mode[h->mb.i_mb_xy - h->mb.i_mb_stride] != 0 )
254     {
255         ctx++;
256     }
257
258     if( i_mode == 0 )
259     {
260         x264_cabac_encode_decision( cb, 64 + ctx, 0 );
261     }
262     else
263     {
264         x264_cabac_encode_decision( cb, 64 + ctx, 1 );
265         x264_cabac_encode_decision( cb, 64 + 3, ( i_mode == 1 ? 0 : 1 ) );
266         if( i_mode > 1 )
267         {
268             x264_cabac_encode_decision( cb, 64 + 3, ( i_mode == 2 ? 0 : 1 ) );
269         }
270     }
271 }
272
273 static void x264_cabac_mb_cbp_luma( x264_t *h, x264_cabac_t *cb )
274 {
275     /* TODO: clean up and optimize */
276     int i8x8;
277     for( i8x8 = 0; i8x8 < 4; i8x8++ )
278     {
279         int i_mba_xy = -1;
280         int i_mbb_xy = -1;
281         int x = block_idx_x[4*i8x8];
282         int y = block_idx_y[4*i8x8];
283         int ctx = 0;
284
285         if( x > 0 )
286             i_mba_xy = h->mb.i_mb_xy;
287         else if( h->mb.i_neighbour & MB_LEFT )
288             i_mba_xy = h->mb.i_mb_xy - 1;
289
290         if( y > 0 )
291             i_mbb_xy = h->mb.i_mb_xy;
292         else if( h->mb.i_neighbour & MB_TOP )
293             i_mbb_xy = h->mb.i_mb_xy - h->mb.i_mb_stride;
294
295
296         /* No need to test for PCM and SKIP */
297         if( i_mba_xy >= 0 )
298         {
299             const int i8x8a = block_idx_xy[(x-1)&0x03][y]/4;
300             if( ((h->mb.cbp[i_mba_xy] >> i8x8a)&0x01) == 0 )
301             {
302                 ctx++;
303             }
304         }
305
306         if( i_mbb_xy >= 0 )
307         {
308             const int i8x8b = block_idx_xy[x][(y-1)&0x03]/4;
309             if( ((h->mb.cbp[i_mbb_xy] >> i8x8b)&0x01) == 0 )
310             {
311                 ctx += 2;
312             }
313         }
314
315         x264_cabac_encode_decision( cb, 73 + ctx, (h->mb.i_cbp_luma >> i8x8)&0x01 );
316     }
317 }
318
319 static void x264_cabac_mb_cbp_chroma( x264_t *h, x264_cabac_t *cb )
320 {
321     int cbp_a = -1;
322     int cbp_b = -1;
323     int ctx;
324
325     /* No need to test for SKIP/PCM */
326     if( h->mb.i_neighbour & MB_LEFT )
327     {
328         cbp_a = (h->mb.cbp[h->mb.i_mb_xy - 1] >> 4)&0x3;
329     }
330
331     if( h->mb.i_neighbour & MB_TOP )
332     {
333         cbp_b = (h->mb.cbp[h->mb.i_mb_xy - h->mb.i_mb_stride] >> 4)&0x3;
334     }
335
336     ctx = 0;
337     if( cbp_a > 0 ) ctx++;
338     if( cbp_b > 0 ) ctx += 2;
339     if( h->mb.i_cbp_chroma == 0 )
340     {
341         x264_cabac_encode_decision( cb, 77 + ctx, 0 );
342     }
343     else
344     {
345         x264_cabac_encode_decision( cb, 77 + ctx, 1 );
346
347         ctx = 4;
348         if( cbp_a == 2 ) ctx++;
349         if( cbp_b == 2 ) ctx += 2;
350         x264_cabac_encode_decision( cb, 77 + ctx, h->mb.i_cbp_chroma > 1 ? 1 : 0 );
351     }
352 }
353
354 /* TODO check it with != qp per mb */
355 static void x264_cabac_mb_qp_delta( x264_t *h, x264_cabac_t *cb )
356 {
357     int i_mbn_xy = h->mb.i_mb_xy - 1;
358     int i_dqp = h->mb.i_qp - h->mb.i_last_qp;
359     int ctx;
360
361     /* No need to test for PCM / SKIP */
362     if( i_mbn_xy >= h->sh.i_first_mb && h->mb.i_last_dqp != 0 &&
363         ( h->mb.type[i_mbn_xy] == I_16x16 || (h->mb.cbp[i_mbn_xy]&0x3f) ) )
364         ctx = 1;
365     else
366         ctx = 0;
367
368     if( i_dqp != 0 )
369     {
370         int val = i_dqp <= 0 ? (-2*i_dqp) : (2*i_dqp - 1);
371         /* dqp is interpreted modulo 52 */
372         if( val > 52 )
373             val = 103 - val;
374         while( val-- )
375         {
376             x264_cabac_encode_decision( cb, 60 + ctx, 1 );
377             if( ctx < 2 )
378                 ctx = 2;
379             else
380                 ctx = 3;
381         }
382     }
383     x264_cabac_encode_decision( cb, 60 + ctx, 0 );
384 }
385
386 void x264_cabac_mb_skip( x264_t *h, int b_skip )
387 {
388     int ctx = 0;
389
390     if( h->mb.i_mb_type_left >= 0 && !IS_SKIP( h->mb.i_mb_type_left ) )
391     {
392         ctx++;
393     }
394     if( h->mb.i_mb_type_top >= 0 && !IS_SKIP( h->mb.i_mb_type_top ) )
395     {
396         ctx++;
397     }
398
399     if( h->sh.i_type == SLICE_TYPE_P )
400         x264_cabac_encode_decision( &h->cabac, 11 + ctx, b_skip ? 1 : 0 );
401     else /* SLICE_TYPE_B */
402         x264_cabac_encode_decision( &h->cabac, 24 + ctx, b_skip ? 1 : 0 );
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 > 0 )
536         x264_cabac_encode_bypass( cb, 0 );
537     else if( mvd < 0 )
538         x264_cabac_encode_bypass( cb, 1 );
539 }
540
541 static inline void x264_cabac_mb_mvd( x264_t *h, x264_cabac_t *cb, int i_list, int idx, int width, int height )
542 {
543     int mvp[2];
544     int mdx, mdy;
545
546     /* Calculate mvd */
547     x264_mb_predict_mv( h, i_list, idx, width, mvp );
548     mdx = h->mb.cache.mv[i_list][x264_scan8[idx]][0] - mvp[0];
549     mdy = h->mb.cache.mv[i_list][x264_scan8[idx]][1] - mvp[1];
550
551     /* encode */
552     x264_cabac_mb_mvd_cpn( h, cb, i_list, idx, 0, mdx );
553     x264_cabac_mb_mvd_cpn( h, cb, i_list, idx, 1, mdy );
554
555     /* save value */
556     x264_macroblock_cache_mvd( h, block_idx_x[idx], block_idx_y[idx], width, height, i_list, mdx, mdy );
557 }
558
559 static inline void x264_cabac_mb8x8_mvd( x264_t *h, x264_cabac_t *cb, int i_list )
560 {
561     int i;
562     for( i = 0; i < 4; i++ )
563     {
564         if( !x264_mb_partition_listX_table[i_list][ h->mb.i_sub_partition[i] ] )
565         {
566             continue;
567         }
568
569         switch( h->mb.i_sub_partition[i] )
570         {
571             case D_L0_8x8:
572             case D_L1_8x8:
573             case D_BI_8x8:
574                 x264_cabac_mb_mvd( h, cb, i_list, 4*i, 2, 2 );
575                 break;
576             case D_L0_8x4:
577             case D_L1_8x4:
578             case D_BI_8x4:
579                 x264_cabac_mb_mvd( h, cb, i_list, 4*i+0, 2, 1 );
580                 x264_cabac_mb_mvd( h, cb, i_list, 4*i+2, 2, 1 );
581                 break;
582             case D_L0_4x8:
583             case D_L1_4x8:
584             case D_BI_4x8:
585                 x264_cabac_mb_mvd( h, cb, i_list, 4*i+0, 1, 2 );
586                 x264_cabac_mb_mvd( h, cb, i_list, 4*i+1, 1, 2 );
587                 break;
588             case D_L0_4x4:
589             case D_L1_4x4:
590             case D_BI_4x4:
591                 x264_cabac_mb_mvd( h, cb, i_list, 4*i+0, 1, 1 );
592                 x264_cabac_mb_mvd( h, cb, i_list, 4*i+1, 1, 1 );
593                 x264_cabac_mb_mvd( h, cb, i_list, 4*i+2, 1, 1 );
594                 x264_cabac_mb_mvd( h, cb, i_list, 4*i+3, 1, 1 );
595                 break;
596         }
597     }
598 }
599
600 static int x264_cabac_mb_cbf_ctxidxinc( x264_t *h, int i_cat, int i_idx )
601 {
602     /* TODO: clean up/optimize */
603     int i_mba_xy = -1;
604     int i_mbb_xy = -1;
605     int i_nza = -1;
606     int i_nzb = -1;
607     int ctx = 4 * i_cat;
608
609     if( i_cat == DCT_LUMA_DC )
610     {
611         if( h->mb.i_neighbour & MB_LEFT )
612         {
613             i_mba_xy = h->mb.i_mb_xy -1;
614             if( h->mb.type[i_mba_xy] == I_16x16 )
615             {
616                 i_nza = h->mb.cbp[i_mba_xy]&0x100;
617             }
618         }
619         if( h->mb.i_neighbour & MB_TOP )
620         {
621             i_mbb_xy = h->mb.i_mb_xy - h->mb.i_mb_stride;
622             if( h->mb.type[i_mbb_xy] == I_16x16 )
623             {
624                 i_nzb = h->mb.cbp[i_mbb_xy]&0x100;
625             }
626         }
627     }
628     else if( i_cat == DCT_LUMA_AC || i_cat == DCT_LUMA_4x4 )
629     {
630         int x = block_idx_x[i_idx];
631         int y = block_idx_y[i_idx];
632
633         if( x > 0 )
634             i_mba_xy = h->mb.i_mb_xy;
635         else if( h->mb.i_neighbour & MB_LEFT )
636             i_mba_xy = h->mb.i_mb_xy -1;
637
638         if( y > 0 )
639             i_mbb_xy = h->mb.i_mb_xy;
640         else if( h->mb.i_neighbour & MB_TOP )
641             i_mbb_xy = h->mb.i_mb_xy - h->mb.i_mb_stride;
642
643         /* no need to test for skip/pcm */
644         if( i_mba_xy >= 0 )
645         {
646             const int i8x8a = block_idx_xy[(x-1)&0x03][y]/4;
647             if( (h->mb.cbp[i_mba_xy]&0x0f)>> i8x8a )
648             {
649                 i_nza = h->mb.cache.non_zero_count[x264_scan8[i_idx] - 1];
650             }
651         }
652         if( i_mbb_xy >= 0 )
653         {
654             const int i8x8b = block_idx_xy[x][(y-1)&0x03]/4;
655             if( (h->mb.cbp[i_mbb_xy]&0x0f)>> i8x8b )
656             {
657                 i_nzb = h->mb.cache.non_zero_count[x264_scan8[i_idx] - 8];
658             }
659         }
660     }
661     else if( i_cat == DCT_CHROMA_DC )
662     {
663         /* no need to test skip/pcm */
664         if( h->mb.i_neighbour & MB_LEFT )
665         {
666             i_mba_xy = h->mb.i_mb_xy -1;
667             if( h->mb.cbp[i_mba_xy]&0x30 )
668             {
669                 i_nza = h->mb.cbp[i_mba_xy]&( 0x02 << ( 8 + i_idx) );
670             }
671         }
672         if( h->mb.i_neighbour & MB_TOP )
673         {
674             i_mbb_xy = h->mb.i_mb_xy - h->mb.i_mb_stride;
675             if( h->mb.cbp[i_mbb_xy]&0x30 )
676             {
677                 i_nzb = h->mb.cbp[i_mbb_xy]&( 0x02 << ( 8 + i_idx) );
678             }
679         }
680     }
681     else if( i_cat == DCT_CHROMA_AC )
682     {
683         if( i_idx & 1 )
684             i_mba_xy = h->mb.i_mb_xy;
685         else if( h->mb.i_neighbour & MB_LEFT )
686             i_mba_xy = h->mb.i_mb_xy - 1;
687
688         if( i_idx & 2 )
689             i_mbb_xy = h->mb.i_mb_xy;
690         else if( h->mb.i_neighbour & MB_TOP )
691             i_mbb_xy = h->mb.i_mb_xy - h->mb.i_mb_stride;
692
693         /* no need to test skip/pcm */
694         if( i_mba_xy >= 0 && (h->mb.cbp[i_mba_xy]&0x30) == 0x20 )
695         {
696             i_nza = h->mb.cache.non_zero_count[x264_scan8[16+i_idx] - 1];
697         }
698         if( i_mbb_xy >= 0 && (h->mb.cbp[i_mbb_xy]&0x30) == 0x20 )
699         {
700             i_nzb = h->mb.cache.non_zero_count[x264_scan8[16+i_idx] - 8];
701         }
702     }
703
704     if( ( i_mba_xy < 0  && IS_INTRA( h->mb.i_type ) ) || i_nza > 0 )
705     {
706         ctx++;
707     }
708     if( ( i_mbb_xy < 0  && IS_INTRA( h->mb.i_type ) ) || i_nzb > 0 )
709     {
710         ctx += 2;
711     }
712
713     return ctx;
714 }
715
716
717 static const int significant_coeff_flag_offset[6] = { 105, 120, 134, 149, 152, 402 };
718 static const int last_coeff_flag_offset[6] = { 166, 181, 195, 210, 213, 417 };
719 static const int coeff_abs_level_m1_offset[6] = { 227, 237, 247, 257, 266, 426 };
720 static const int significant_coeff_flag_offset_8x8[63] = {
721     0, 1, 2, 3, 4, 5, 5, 4, 4, 3, 3, 4, 4, 4, 5, 5,
722     4, 4, 4, 4, 3, 3, 6, 7, 7, 7, 8, 9,10, 9, 8, 7,
723     7, 6,11,12,13,11, 6, 7, 8, 9,14,10, 9, 8, 6,11,
724    12,13,11, 6, 9,14,10, 9,11,12,13,11,14,10,12
725 };
726 static const int last_coeff_flag_offset_8x8[63] = {
727     0, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1,
728     2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2,
729     3, 3, 3, 3, 3, 3, 3, 3, 4, 4, 4, 4, 4, 4, 4, 4,
730     5, 5, 5, 5, 6, 6, 6, 6, 7, 7, 7, 7, 8, 8, 8
731 };
732
733 static void block_residual_write_cabac( x264_t *h, x264_cabac_t *cb, int i_ctxBlockCat, int i_idx, int *l, int i_count )
734 {
735     const int i_ctx_sig = significant_coeff_flag_offset[i_ctxBlockCat];
736     const int i_ctx_last = last_coeff_flag_offset[i_ctxBlockCat];
737     const int i_ctx_level = coeff_abs_level_m1_offset[i_ctxBlockCat];
738
739     int i_coeff_abs_m1[64];
740     int i_coeff_sign[64];
741     int i_coeff = 0;
742     int i_last  = 0;
743     int i_sigmap_size;
744
745     int i_abslevel1 = 0;
746     int i_abslevelgt1 = 0;
747
748     int i;
749
750     /* i_ctxBlockCat: 0-> DC 16x16  i_idx = 0
751      *                1-> AC 16x16  i_idx = luma4x4idx
752      *                2-> Luma4x4   i_idx = luma4x4idx
753      *                3-> DC Chroma i_idx = iCbCr
754      *                4-> AC Chroma i_idx = 4 * iCbCr + chroma4x4idx
755      *                5-> Luma8x8   i_idx = luma8x8idx
756      */
757
758     for( i = 0; i < i_count; i++ )
759     {
760         if( l[i] != 0 )
761         {
762             i_coeff_abs_m1[i_coeff] = abs( l[i] ) - 1;
763             i_coeff_sign[i_coeff]   = ( l[i] < 0 );
764             i_coeff++;
765
766             i_last = i;
767         }
768     }
769
770     if( i_count != 64 )
771     {
772         /* coded block flag */
773         x264_cabac_encode_decision( cb, 85 + x264_cabac_mb_cbf_ctxidxinc( h, i_ctxBlockCat, i_idx ), i_coeff != 0 );
774         if( i_coeff == 0 )
775             return;
776     }
777
778     i_sigmap_size = X264_MIN( i_last+1, i_count-1 );
779     for( i = 0; i < i_sigmap_size; i++ )
780     {
781         int i_sig_ctxIdxInc;
782         int i_last_ctxIdxInc;
783
784         if( i_ctxBlockCat == DCT_LUMA_8x8 )
785         {
786             i_sig_ctxIdxInc = significant_coeff_flag_offset_8x8[i];
787             i_last_ctxIdxInc = last_coeff_flag_offset_8x8[i];
788         }
789         else
790             i_sig_ctxIdxInc = i_last_ctxIdxInc = i;
791
792         x264_cabac_encode_decision( cb, i_ctx_sig + i_sig_ctxIdxInc, l[i] != 0 );
793         if( l[i] != 0 )
794             x264_cabac_encode_decision( cb, i_ctx_last + i_last_ctxIdxInc, i == i_last );
795     }
796
797     for( i = i_coeff - 1; i >= 0; i-- )
798     {
799         /* write coeff_abs - 1 */
800         const int i_prefix = X264_MIN( i_coeff_abs_m1[i], 14 );
801         const int i_ctxIdxInc = (i_abslevelgt1 ? 0 : X264_MIN( 4, i_abslevel1 + 1 )) + i_ctx_level;
802         x264_cabac_encode_decision( cb, i_ctxIdxInc, i_prefix != 0 );
803
804         if( i_prefix != 0 )
805         {
806             const int i_ctxIdxInc = 5 + X264_MIN( 4, i_abslevelgt1 ) + i_ctx_level;
807             int j;
808             for( j = 0; j < i_prefix - 1; j++ )
809                 x264_cabac_encode_decision( cb, i_ctxIdxInc, 1 );
810             if( i_prefix < 14 )
811                 x264_cabac_encode_decision( cb, i_ctxIdxInc, 0 );
812             else /* suffix */
813                 x264_cabac_encode_ue_bypass( cb, 0, i_coeff_abs_m1[i] - 14 );
814
815             i_abslevelgt1++;
816         }
817         else
818             i_abslevel1++;
819
820         /* write sign */
821         x264_cabac_encode_bypass( cb, i_coeff_sign[i] );
822     }
823 }
824
825
826
827 void x264_macroblock_write_cabac( x264_t *h, x264_cabac_t *cb )
828 {
829     const int i_mb_type = h->mb.i_type;
830     int i_list;
831     int i;
832
833 #ifndef RDO_SKIP_BS
834     const int i_mb_pos_start = x264_cabac_pos( cb );
835     int       i_mb_pos_tex;
836 #endif
837
838     /* Write the MB type */
839     x264_cabac_mb_type( h, cb );
840
841     /* PCM special block type UNTESTED */
842     if( i_mb_type == I_PCM )
843     {
844 #ifdef RDO_SKIP_BS
845         cb->f8_bits_encoded += (384*8) << 8;
846 #else
847         bs_t *s = cb->s;
848         bs_align_0( s );    /* not sure */
849         /* Luma */
850         for( i = 0; i < 16*16; i++ )
851         {
852             const int x = 16 * h->mb.i_mb_x + (i % 16);
853             const int y = 16 * h->mb.i_mb_y + (i / 16);
854             bs_write( s, 8, h->fenc->plane[0][y*h->mb.pic.i_stride[0]+x] );
855         }
856         /* Cb */
857         for( i = 0; i < 8*8; i++ )
858         {
859             const int x = 8 * h->mb.i_mb_x + (i % 8);
860             const int y = 8 * h->mb.i_mb_y + (i / 8);
861             bs_write( s, 8, h->fenc->plane[1][y*h->mb.pic.i_stride[1]+x] );
862         }
863         /* Cr */
864         for( i = 0; i < 8*8; i++ )
865         {
866             const int x = 8 * h->mb.i_mb_x + (i % 8);
867             const int y = 8 * h->mb.i_mb_y + (i / 8);
868             bs_write( s, 8, h->fenc->plane[2][y*h->mb.pic.i_stride[2]+x] );
869         }
870         x264_cabac_encode_init( cb, s );
871 #endif
872         return;
873     }
874
875     if( IS_INTRA( i_mb_type ) )
876     {
877         if( h->pps->b_transform_8x8_mode && i_mb_type != I_16x16 )
878             x264_cabac_mb_transform_size( h, cb );
879
880         if( i_mb_type != I_16x16 )
881         {
882             int di = (i_mb_type == I_8x8) ? 4 : 1;
883             for( i = 0; i < 16; i += di )
884             {
885                 const int i_pred = x264_mb_predict_intra4x4_mode( h, i );
886                 const int i_mode = x264_mb_pred_mode4x4_fix( h->mb.cache.intra4x4_pred_mode[x264_scan8[i]] );
887                 x264_cabac_mb_intra4x4_pred_mode( cb, i_pred, i_mode );
888             }
889         }
890
891         x264_cabac_mb_intra_chroma_pred_mode( h, cb );
892     }
893     else if( i_mb_type == P_L0 )
894     {
895         if( h->mb.i_partition == D_16x16 )
896         {
897             if( h->sh.i_num_ref_idx_l0_active > 1 )
898             {
899                 x264_cabac_mb_ref( h, cb, 0, 0 );
900             }
901             x264_cabac_mb_mvd( h, cb, 0, 0, 4, 4 );
902         }
903         else if( h->mb.i_partition == D_16x8 )
904         {
905             if( h->sh.i_num_ref_idx_l0_active > 1 )
906             {
907                 x264_cabac_mb_ref( h, cb, 0, 0 );
908                 x264_cabac_mb_ref( h, cb, 0, 8 );
909             }
910             x264_cabac_mb_mvd( h, cb, 0, 0, 4, 2 );
911             x264_cabac_mb_mvd( h, cb, 0, 8, 4, 2 );
912         }
913         else if( h->mb.i_partition == D_8x16 )
914         {
915             if( h->sh.i_num_ref_idx_l0_active > 1 )
916             {
917                 x264_cabac_mb_ref( h, cb, 0, 0 );
918                 x264_cabac_mb_ref( h, cb, 0, 4 );
919             }
920             x264_cabac_mb_mvd( h, cb, 0, 0, 2, 4 );
921             x264_cabac_mb_mvd( h, cb, 0, 4, 2, 4 );
922         }
923     }
924     else if( i_mb_type == P_8x8 )
925     {
926         /* sub mb type */
927         x264_cabac_mb_sub_p_partition( cb, h->mb.i_sub_partition[0] );
928         x264_cabac_mb_sub_p_partition( cb, h->mb.i_sub_partition[1] );
929         x264_cabac_mb_sub_p_partition( cb, h->mb.i_sub_partition[2] );
930         x264_cabac_mb_sub_p_partition( cb, h->mb.i_sub_partition[3] );
931
932         /* ref 0 */
933         if( h->sh.i_num_ref_idx_l0_active > 1 )
934         {
935             x264_cabac_mb_ref( h, cb, 0, 0 );
936             x264_cabac_mb_ref( h, cb, 0, 4 );
937             x264_cabac_mb_ref( h, cb, 0, 8 );
938             x264_cabac_mb_ref( h, cb, 0, 12 );
939         }
940
941         x264_cabac_mb8x8_mvd( h, cb, 0 );
942     }
943     else if( i_mb_type == B_8x8 )
944     {
945         /* sub mb type */
946         x264_cabac_mb_sub_b_partition( cb, h->mb.i_sub_partition[0] );
947         x264_cabac_mb_sub_b_partition( cb, h->mb.i_sub_partition[1] );
948         x264_cabac_mb_sub_b_partition( cb, h->mb.i_sub_partition[2] );
949         x264_cabac_mb_sub_b_partition( cb, h->mb.i_sub_partition[3] );
950
951         /* ref */
952         for( i_list = 0; i_list < 2; i_list++ )
953         {
954             if( ( i_list ? h->sh.i_num_ref_idx_l1_active : h->sh.i_num_ref_idx_l0_active ) == 1 )
955                 continue;
956             for( i = 0; i < 4; i++ )
957                 if( x264_mb_partition_listX_table[i_list][ h->mb.i_sub_partition[i] ] )
958                     x264_cabac_mb_ref( h, cb, i_list, 4*i );
959         }
960
961         x264_cabac_mb8x8_mvd( h, cb, 0 );
962         x264_cabac_mb8x8_mvd( h, cb, 1 );
963     }
964     else if( i_mb_type != B_DIRECT )
965     {
966         /* All B mode */
967         int b_list[2][2];
968
969         /* init ref list utilisations */
970         for( i = 0; i < 2; i++ )
971         {
972             b_list[0][i] = x264_mb_type_list0_table[i_mb_type][i];
973             b_list[1][i] = x264_mb_type_list1_table[i_mb_type][i];
974         }
975
976         for( i_list = 0; i_list < 2; i_list++ )
977         {
978             const int i_ref_max = i_list == 0 ? h->sh.i_num_ref_idx_l0_active : h->sh.i_num_ref_idx_l1_active;
979
980             if( i_ref_max > 1 )
981             {
982                 if( h->mb.i_partition == D_16x16 )
983                 {
984                     if( b_list[i_list][0] ) x264_cabac_mb_ref( h, cb, i_list, 0 );
985                 }
986                 else if( h->mb.i_partition == D_16x8 )
987                 {
988                     if( b_list[i_list][0] ) x264_cabac_mb_ref( h, cb, i_list, 0 );
989                     if( b_list[i_list][1] ) x264_cabac_mb_ref( h, cb, i_list, 8 );
990                 }
991                 else if( h->mb.i_partition == D_8x16 )
992                 {
993                     if( b_list[i_list][0] ) x264_cabac_mb_ref( h, cb, i_list, 0 );
994                     if( b_list[i_list][1] ) x264_cabac_mb_ref( h, cb, i_list, 4 );
995                 }
996             }
997         }
998         for( i_list = 0; i_list < 2; i_list++ )
999         {
1000             if( h->mb.i_partition == D_16x16 )
1001             {
1002                 if( b_list[i_list][0] ) x264_cabac_mb_mvd( h, cb, i_list, 0, 4, 4 );
1003             }
1004             else if( h->mb.i_partition == D_16x8 )
1005             {
1006                 if( b_list[i_list][0] ) x264_cabac_mb_mvd( h, cb, i_list, 0, 4, 2 );
1007                 if( b_list[i_list][1] ) x264_cabac_mb_mvd( h, cb, i_list, 8, 4, 2 );
1008             }
1009             else if( h->mb.i_partition == D_8x16 )
1010             {
1011                 if( b_list[i_list][0] ) x264_cabac_mb_mvd( h, cb, i_list, 0, 2, 4 );
1012                 if( b_list[i_list][1] ) x264_cabac_mb_mvd( h, cb, i_list, 4, 2, 4 );
1013             }
1014         }
1015     }
1016
1017 #ifndef RDO_SKIP_BS
1018     i_mb_pos_tex = x264_cabac_pos( cb );
1019     h->stat.frame.i_hdr_bits += i_mb_pos_tex - i_mb_pos_start;
1020 #endif
1021
1022     if( i_mb_type != I_16x16 )
1023     {
1024         x264_cabac_mb_cbp_luma( h, cb );
1025         x264_cabac_mb_cbp_chroma( h, cb );
1026     }
1027
1028     if( h->mb.cache.b_transform_8x8_allowed && h->mb.i_cbp_luma && !IS_INTRA(i_mb_type) )
1029     {
1030         x264_cabac_mb_transform_size( h, cb );
1031     }
1032
1033     if( h->mb.i_cbp_luma > 0 || h->mb.i_cbp_chroma > 0 || i_mb_type == I_16x16 )
1034     {
1035         x264_cabac_mb_qp_delta( h, cb );
1036
1037         /* write residual */
1038         if( i_mb_type == I_16x16 )
1039         {
1040             /* DC Luma */
1041             block_residual_write_cabac( h, cb, DCT_LUMA_DC, 0, h->dct.luma16x16_dc, 16 );
1042
1043             /* AC Luma */
1044             if( h->mb.i_cbp_luma != 0 )
1045                 for( i = 0; i < 16; i++ )
1046                     block_residual_write_cabac( h, cb, DCT_LUMA_AC, i, h->dct.block[i].residual_ac, 15 );
1047         }
1048         else if( h->mb.b_transform_8x8 )
1049         {
1050             for( i = 0; i < 4; i++ )
1051                 if( h->mb.i_cbp_luma & ( 1 << i ) )
1052                     block_residual_write_cabac( h, cb, DCT_LUMA_8x8, i, h->dct.luma8x8[i], 64 );
1053         }
1054         else
1055         {
1056             for( i = 0; i < 16; i++ )
1057                 if( h->mb.i_cbp_luma & ( 1 << ( i / 4 ) ) )
1058                     block_residual_write_cabac( h, cb, DCT_LUMA_4x4, i, h->dct.block[i].luma4x4, 16 );
1059         }
1060
1061         if( h->mb.i_cbp_chroma &0x03 )    /* Chroma DC residual present */
1062         {
1063             block_residual_write_cabac( h, cb, DCT_CHROMA_DC, 0, h->dct.chroma_dc[0], 4 );
1064             block_residual_write_cabac( h, cb, DCT_CHROMA_DC, 1, h->dct.chroma_dc[1], 4 );
1065         }
1066         if( h->mb.i_cbp_chroma&0x02 ) /* Chroma AC residual present */
1067         {
1068             for( i = 0; i < 8; i++ )
1069                 block_residual_write_cabac( h, cb, DCT_CHROMA_AC, i, h->dct.block[16+i].residual_ac, 15 );
1070         }
1071     }
1072
1073 #ifndef RDO_SKIP_BS
1074     if( IS_INTRA( i_mb_type ) )
1075         h->stat.frame.i_itex_bits += x264_cabac_pos( cb ) - i_mb_pos_tex;
1076     else
1077         h->stat.frame.i_ptex_bits += x264_cabac_pos( cb ) - i_mb_pos_tex;
1078 #endif
1079 }
1080