]> git.sesse.net Git - x264/blob - encoder/cabac.c
rename 'core/' to 'common/', which avoids conflicts with libtool
[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 )
616         ctx++;
617     if( i_refb > 0 )
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 int x264_cabac_mb_cbf_ctxidxinc( x264_t *h, int i_cat, int i_idx )
710 {
711     /* TODO: clean up/optimize */
712     int i_mba_xy = -1;
713     int i_mbb_xy = -1;
714     int i_nza = -1;
715     int i_nzb = -1;
716     int ctx = 0;
717
718     if( i_cat == 0 )
719     {
720         if( h->mb.i_mb_x > 0 )
721         {
722             i_mba_xy = h->mb.i_mb_xy -1;
723             if( h->mb.type[i_mba_xy] == I_16x16 )
724             {
725                 i_nza = h->mb.cbp[i_mba_xy]&0x100;
726             }
727         }
728         if( h->mb.i_mb_y > 0 )
729         {
730             i_mbb_xy = h->mb.i_mb_xy - h->mb.i_mb_stride;
731             if( h->mb.type[i_mbb_xy] == I_16x16 )
732             {
733                 i_nzb = h->mb.cbp[i_mbb_xy]&0x100;
734             }
735         }
736     }
737     else if( i_cat == 1 || i_cat == 2 )
738     {
739         int x = block_idx_x[i_idx];
740         int y = block_idx_y[i_idx];
741
742         if( x > 0 )
743             i_mba_xy = h->mb.i_mb_xy;
744         else if( h->mb.i_mb_x > 0 )
745             i_mba_xy = h->mb.i_mb_xy -1;
746
747         if( y > 0 )
748             i_mbb_xy = h->mb.i_mb_xy;
749         else if( h->mb.i_mb_y > 0 )
750             i_mbb_xy = h->mb.i_mb_xy - h->mb.i_mb_stride;
751
752         /* no need to test for skip/pcm */
753         if( i_mba_xy >= 0 )
754         {
755             const int i8x8a = block_idx_xy[(x-1)&0x03][y]/4;
756             if( (h->mb.cbp[i_mba_xy]&0x0f)>> i8x8a )
757             {
758                 i_nza = h->mb.cache.non_zero_count[x264_scan8[i_idx] - 1];
759             }
760         }
761         if( i_mbb_xy >= 0 )
762         {
763             const int i8x8b = block_idx_xy[x][(y-1)&0x03]/4;
764             if( (h->mb.cbp[i_mbb_xy]&0x0f)>> i8x8b )
765             {
766                 i_nzb = h->mb.cache.non_zero_count[x264_scan8[i_idx] - 8];
767             }
768         }
769     }
770     else if( i_cat == 3 )
771     {
772         /* no need to test skip/pcm */
773         if( h->mb.i_mb_x > 0 )
774         {
775             i_mba_xy = h->mb.i_mb_xy -1;
776             if( h->mb.cbp[i_mba_xy]&0x30 )
777             {
778                 i_nza = h->mb.cbp[i_mba_xy]&( 0x02 << ( 8 + i_idx) );
779             }
780         }
781         if( h->mb.i_mb_y > 0 )
782         {
783             i_mbb_xy = h->mb.i_mb_xy - h->mb.i_mb_stride;
784             if( h->mb.cbp[i_mbb_xy]&0x30 )
785             {
786                 i_nzb = h->mb.cbp[i_mbb_xy]&( 0x02 << ( 8 + i_idx) );
787             }
788         }
789     }
790     else if( i_cat == 4 )
791     {
792         int idxc = i_idx% 4;
793
794         if( idxc == 1 || idxc == 3 )
795             i_mba_xy = h->mb.i_mb_xy;
796         else if( h->mb.i_mb_x > 0 )
797             i_mba_xy = h->mb.i_mb_xy - 1;
798
799         if( idxc == 2 || idxc == 3 )
800             i_mbb_xy = h->mb.i_mb_xy;
801         else if( h->mb.i_mb_y > 0 )
802             i_mbb_xy = h->mb.i_mb_xy - h->mb.i_mb_stride;
803
804         /* no need to test skip/pcm */
805         if( i_mba_xy >= 0 && (h->mb.cbp[i_mba_xy]&0x30) == 0x20 )
806         {
807             i_nza = h->mb.cache.non_zero_count[x264_scan8[16+i_idx] - 1];
808         }
809         if( i_mbb_xy >= 0 && (h->mb.cbp[i_mbb_xy]&0x30) == 0x20 )
810         {
811             i_nzb = h->mb.cache.non_zero_count[x264_scan8[16+i_idx] - 8];
812         }
813     }
814
815     if( ( i_mba_xy < 0  && IS_INTRA( h->mb.i_type ) ) || i_nza > 0 )
816     {
817         ctx++;
818     }
819     if( ( i_mbb_xy < 0  && IS_INTRA( h->mb.i_type ) ) || i_nzb > 0 )
820     {
821         ctx += 2;
822     }
823
824     return 4 * i_cat + ctx;
825 }
826
827
828 static void block_residual_write_cabac( x264_t *h, int i_ctxBlockCat, int i_idx, int *l, int i_count )
829 {
830     static const int significant_coeff_flag_offset[5] = { 0, 15, 29, 44, 47 };
831     static const int last_significant_coeff_flag_offset[5] = { 0, 15, 29, 44, 47 };
832     static const int coeff_abs_level_m1_offset[5] = { 0, 10, 20, 30, 39 };
833
834     int i_coeff_abs_m1[16];
835     int i_coeff_sign[16];
836     int i_coeff = 0;
837     int i_last  = 0;
838
839     int i_abslevel1 = 0;
840     int i_abslevelgt1 = 0;
841
842     int i;
843
844     /* i_ctxBlockCat: 0-> DC 16x16  i_idx = 0
845      *                1-> AC 16x16  i_idx = luma4x4idx
846      *                2-> Luma4x4   i_idx = luma4x4idx
847      *                3-> DC Chroma i_idx = iCbCr
848      *                4-> AC Chroma i_idx = 4 * iCbCr + chroma4x4idx
849      */
850
851     //fprintf( stderr, "l[] = " );
852     for( i = 0; i < i_count; i++ )
853     {
854         //fprintf( stderr, "%d ", l[i] );
855         if( l[i] != 0 )
856         {
857             i_coeff_abs_m1[i_coeff] = abs( l[i] ) - 1;
858             i_coeff_sign[i_coeff]   = ( l[i] < 0 ? 1 : 0);
859             i_coeff++;
860
861             i_last = i;
862         }
863     }
864     //fprintf( stderr, "\n" );
865
866     if( i_coeff == 0 )
867     {
868         /* codec block flag */
869         x264_cabac_encode_decision( &h->cabac,  85 + x264_cabac_mb_cbf_ctxidxinc( h, i_ctxBlockCat, i_idx ), 0 );
870         return;
871     }
872
873     /* block coded */
874     x264_cabac_encode_decision( &h->cabac,  85 + x264_cabac_mb_cbf_ctxidxinc( h, i_ctxBlockCat, i_idx ), 1 );
875     for( i = 0; i < i_count - 1; i++ )
876     {
877         int i_ctxIdxInc;
878
879         i_ctxIdxInc = X264_MIN( i, i_count - 2 );
880
881         if( l[i] != 0 )
882         {
883             x264_cabac_encode_decision( &h->cabac, 105 + significant_coeff_flag_offset[i_ctxBlockCat] + i_ctxIdxInc, 1 );
884             x264_cabac_encode_decision( &h->cabac, 166 + last_significant_coeff_flag_offset[i_ctxBlockCat] + i_ctxIdxInc, i == i_last ? 1 : 0 );
885         }
886         else
887         {
888             x264_cabac_encode_decision( &h->cabac, 105 + significant_coeff_flag_offset[i_ctxBlockCat] + i_ctxIdxInc, 0 );
889         }
890         if( i == i_last )
891         {
892             break;
893         }
894     }
895
896     for( i = i_coeff - 1; i >= 0; i-- )
897     {
898         int i_prefix;
899         int i_ctxIdxInc;
900
901         /* write coeff_abs - 1 */
902
903         /* prefix */
904         i_prefix = X264_MIN( i_coeff_abs_m1[i], 14 );
905
906         i_ctxIdxInc = (i_abslevelgt1 != 0 ? 0 : X264_MIN( 4, i_abslevel1 + 1 )) + coeff_abs_level_m1_offset[i_ctxBlockCat];
907         if( i_prefix == 0 )
908         {
909             x264_cabac_encode_decision( &h->cabac,  227 + i_ctxIdxInc, 0 );
910         }
911         else
912         {
913             int j;
914             x264_cabac_encode_decision( &h->cabac,  227 + i_ctxIdxInc, 1 );
915             i_ctxIdxInc = 5 + X264_MIN( 4, i_abslevelgt1 ) + coeff_abs_level_m1_offset[i_ctxBlockCat];
916             for( j = 0; j < i_prefix - 1; j++ )
917             {
918                 x264_cabac_encode_decision( &h->cabac,  227 + i_ctxIdxInc, 1 );
919             }
920             if( i_prefix < 14 )
921             {
922                 x264_cabac_encode_decision( &h->cabac,  227 + i_ctxIdxInc, 0 );
923             }
924         }
925         /* suffix */
926         if( i_coeff_abs_m1[i] >= 14 )
927         {
928             int k = 0;
929             int i_suffix = i_coeff_abs_m1[i] - 14;
930
931             while( i_suffix >= (1<<k) )
932             {
933                 x264_cabac_encode_bypass( &h->cabac, 1 );
934                 i_suffix -= 1 << k;
935                 k++;
936             }
937             x264_cabac_encode_bypass( &h->cabac, 0 );
938             while( k-- )
939             {
940                 x264_cabac_encode_bypass( &h->cabac, (i_suffix >> k)&0x01 );
941             }
942         }
943
944         /* write sign */
945         x264_cabac_encode_bypass( &h->cabac, i_coeff_sign[i] );
946
947
948         if( i_coeff_abs_m1[i] == 0 )
949         {
950             i_abslevel1++;
951         }
952         else
953         {
954             i_abslevelgt1++;
955         }
956     }
957 }
958
959
960
961 void x264_macroblock_write_cabac( x264_t *h, bs_t *s )
962 {
963     const int i_mb_type = h->mb.i_type;
964     const int i_mb_pos_start = bs_pos( s );
965     int       i_mb_pos_tex;
966
967     int i;
968
969     /* Write the MB type */
970     x264_cabac_mb_type( h );
971
972     /* PCM special block type UNTESTED */
973     if( i_mb_type == I_PCM )
974     {
975         bs_align_0( s );    /* not sure */
976         /* Luma */
977         for( i = 0; i < 16*16; i++ )
978         {
979             const int x = 16 * h->mb.i_mb_x + (i % 16);
980             const int y = 16 * h->mb.i_mb_y + (i / 16);
981             bs_write( s, 8, h->fenc->plane[0][y*h->mb.pic.i_stride[0]+x] );
982         }
983         /* Cb */
984         for( i = 0; i < 8*8; i++ )
985         {
986             const int x = 8 * h->mb.i_mb_x + (i % 8);
987             const int y = 8 * h->mb.i_mb_y + (i / 8);
988             bs_write( s, 8, h->fenc->plane[1][y*h->mb.pic.i_stride[1]+x] );
989         }
990         /* Cr */
991         for( i = 0; i < 8*8; i++ )
992         {
993             const int x = 8 * h->mb.i_mb_x + (i % 8);
994             const int y = 8 * h->mb.i_mb_y + (i / 8);
995             bs_write( s, 8, h->fenc->plane[2][y*h->mb.pic.i_stride[2]+x] );
996         }
997         x264_cabac_encode_init( &h->cabac, s );
998         return;
999     }
1000
1001     if( IS_INTRA( i_mb_type ) )
1002     {
1003         /* Prediction */
1004         if( i_mb_type == I_4x4 )
1005         {
1006             for( i = 0; i < 16; i++ )
1007             {
1008                 const int i_pred = x264_mb_predict_intra4x4_mode( h, i );
1009                 const int i_mode = h->mb.cache.intra4x4_pred_mode[x264_scan8[i]];
1010                 x264_cabac_mb_intra4x4_pred_mode( h, i_pred, i_mode );
1011             }
1012         }
1013         x264_cabac_mb_intra8x8_pred_mode( h );
1014     }
1015     else if( i_mb_type == P_L0 )
1016     {
1017         if( h->mb.i_partition == D_16x16 )
1018         {
1019             if( h->sh.i_num_ref_idx_l0_active > 1 )
1020             {
1021                 x264_cabac_mb_ref( h, 0, 0 );
1022             }
1023             x264_cabac_mb_mvd( h, 0, 0, 4, 4 );
1024         }
1025         else if( h->mb.i_partition == D_16x8 )
1026         {
1027             if( h->sh.i_num_ref_idx_l0_active > 1 )
1028             {
1029                 x264_cabac_mb_ref( h, 0, 0 );
1030                 x264_cabac_mb_ref( h, 0, 8 );
1031             }
1032             x264_cabac_mb_mvd( h, 0, 0, 4, 2 );
1033             x264_cabac_mb_mvd( h, 0, 8, 4, 2 );
1034         }
1035         else if( h->mb.i_partition == D_8x16 )
1036         {
1037             if( h->sh.i_num_ref_idx_l0_active > 1 )
1038             {
1039                 x264_cabac_mb_ref( h, 0, 0 );
1040                 x264_cabac_mb_ref( h, 0, 4 );
1041             }
1042             x264_cabac_mb_mvd( h, 0, 0, 2, 4 );
1043             x264_cabac_mb_mvd( h, 0, 4, 2, 4 );
1044         }
1045     }
1046     else if( i_mb_type == P_8x8 )
1047     {
1048         /* sub mb type */
1049         x264_cabac_mb_sub_p_partition( h, h->mb.i_sub_partition[0] );
1050         x264_cabac_mb_sub_p_partition( h, h->mb.i_sub_partition[1] );
1051         x264_cabac_mb_sub_p_partition( h, h->mb.i_sub_partition[2] );
1052         x264_cabac_mb_sub_p_partition( h, h->mb.i_sub_partition[3] );
1053
1054         /* ref 0 */
1055         if( h->sh.i_num_ref_idx_l0_active > 1 )
1056         {
1057             x264_cabac_mb_ref( h, 0, 0 );
1058             x264_cabac_mb_ref( h, 0, 4 );
1059             x264_cabac_mb_ref( h, 0, 8 );
1060             x264_cabac_mb_ref( h, 0, 12 );
1061         }
1062
1063         for( i = 0; i < 4; i++ )
1064         {
1065             switch( h->mb.i_sub_partition[i] )
1066             {
1067                 case D_L0_8x8:
1068                     x264_cabac_mb_mvd( h, 0, 4*i, 2, 2 );
1069                     break;
1070                 case D_L0_8x4:
1071                     x264_cabac_mb_mvd( h, 0, 4*i+0, 2, 1 );
1072                     x264_cabac_mb_mvd( h, 0, 4*i+2, 2, 1 );
1073                     break;
1074                 case D_L0_4x8:
1075                     x264_cabac_mb_mvd( h, 0, 4*i+0, 1, 2 );
1076                     x264_cabac_mb_mvd( h, 0, 4*i+1, 1, 2 );
1077                     break;
1078                 case D_L0_4x4:
1079                     x264_cabac_mb_mvd( h, 0, 4*i+0, 1, 1 );
1080                     x264_cabac_mb_mvd( h, 0, 4*i+1, 1, 1 );
1081                     x264_cabac_mb_mvd( h, 0, 4*i+2, 1, 1 );
1082                     x264_cabac_mb_mvd( h, 0, 4*i+3, 1, 1 );
1083                     break;
1084             }
1085         }
1086     }
1087     else if( i_mb_type == B_8x8 )
1088     {
1089         /* TODO */
1090         fprintf( stderr, "Arggg B_8x8\n" );
1091         return;
1092     }
1093     else if( i_mb_type != B_DIRECT )
1094     {
1095         /* All B mode */
1096         int i_list;
1097         int b_list[2][2];
1098
1099         /* init ref list utilisations */
1100         for( i = 0; i < 2; i++ )
1101         {
1102             b_list[0][i] = x264_mb_type_list0_table[i_mb_type][i];
1103             b_list[1][i] = x264_mb_type_list1_table[i_mb_type][i];
1104         }
1105
1106         for( i_list = 0; i_list < 2; i_list++ )
1107         {
1108             const int i_ref_max = i_list == 0 ? h->sh.i_num_ref_idx_l0_active : h->sh.i_num_ref_idx_l1_active;
1109
1110             if( i_ref_max > 1 )
1111             {
1112                 if( h->mb.i_partition == D_16x16 )
1113                 {
1114                     if( b_list[i_list][0] ) x264_cabac_mb_ref( h, i_list, 0 );
1115                 }
1116                 else if( h->mb.i_partition == D_16x8 )
1117                 {
1118                     if( b_list[i_list][0] ) x264_cabac_mb_ref( h, i_list, 0 );
1119                     if( b_list[i_list][1] ) x264_cabac_mb_ref( h, i_list, 8 );
1120                 }
1121                 else if( h->mb.i_partition == D_8x16 )
1122                 {
1123                     if( b_list[i_list][0] ) x264_cabac_mb_ref( h, i_list, 0 );
1124                     if( b_list[i_list][1] ) x264_cabac_mb_ref( h, i_list, 4 );
1125                 }
1126             }
1127         }
1128         for( i_list = 0; i_list < 2; i_list++ )
1129         {
1130             if( h->mb.i_partition == D_16x16 )
1131             {
1132                 if( b_list[i_list][0] ) x264_cabac_mb_mvd( h, i_list, 0, 4, 4 );
1133             }
1134             else if( h->mb.i_partition == D_16x8 )
1135             {
1136                 if( b_list[i_list][0] ) x264_cabac_mb_mvd( h, i_list, 0, 4, 2 );
1137                 if( b_list[i_list][1] ) x264_cabac_mb_mvd( h, i_list, 8, 4, 2 );
1138             }
1139             else if( h->mb.i_partition == D_8x16 )
1140             {
1141                 if( b_list[i_list][0] ) x264_cabac_mb_mvd( h, i_list, 0, 2, 4 );
1142                 if( b_list[i_list][1] ) x264_cabac_mb_mvd( h, i_list, 4, 2, 4 );
1143             }
1144         }
1145     }
1146
1147     i_mb_pos_tex = bs_pos( s );
1148     h->stat.frame.i_hdr_bits += i_mb_pos_tex - i_mb_pos_start;
1149
1150     if( i_mb_type != I_16x16 )
1151     {
1152         x264_cabac_mb_cbp_luma( h );
1153         x264_cabac_mb_cbp_chroma( h );
1154     }
1155
1156     if( h->mb.i_cbp_luma > 0 || h->mb.i_cbp_chroma > 0 || i_mb_type == I_16x16 )
1157     {
1158         x264_cabac_mb_qp_delta( h );
1159
1160         /* write residual */
1161         if( i_mb_type == I_16x16 )
1162         {
1163             /* DC Luma */
1164             block_residual_write_cabac( h, 0, 0, h->dct.luma16x16_dc, 16 );
1165
1166             if( h->mb.i_cbp_luma != 0 )
1167             {
1168                 /* AC Luma */
1169                 for( i = 0; i < 16; i++ )
1170                 {
1171                     block_residual_write_cabac( h, 1, i, h->dct.block[i].residual_ac, 15 );
1172                 }
1173             }
1174         }
1175         else
1176         {
1177             for( i = 0; i < 16; i++ )
1178             {
1179                 if( h->mb.i_cbp_luma & ( 1 << ( i / 4 ) ) )
1180                 {
1181                     block_residual_write_cabac( h, 2, i, h->dct.block[i].luma4x4, 16 );
1182                 }
1183             }
1184         }
1185
1186         if( h->mb.i_cbp_chroma &0x03 )    /* Chroma DC residual present */
1187         {
1188             block_residual_write_cabac( h, 3, 0, h->dct.chroma_dc[0], 4 );
1189             block_residual_write_cabac( h, 3, 1, h->dct.chroma_dc[1], 4 );
1190         }
1191         if( h->mb.i_cbp_chroma&0x02 ) /* Chroma AC residual present */
1192         {
1193             for( i = 0; i < 8; i++ )
1194             {
1195                 block_residual_write_cabac( h, 4, i, h->dct.block[16+i].residual_ac, 15 );
1196             }
1197         }
1198     }
1199
1200     if( IS_INTRA( i_mb_type ) )
1201         h->stat.frame.i_itex_bits += bs_pos(s) - i_mb_pos_tex;
1202     else
1203         h->stat.frame.i_ptex_bits += bs_pos(s) - i_mb_pos_tex;
1204 }
1205