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