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