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