]> git.sesse.net Git - x264/blob - decoder/macroblock.c
support pkg-config.
[x264] / decoder / macroblock.c
1 /*****************************************************************************
2  * macroblock.c: h264 decoder library
3  *****************************************************************************
4  * Copyright (C) 2003 Laurent Aimar
5  * $Id: macroblock.c,v 1.1 2004/06/03 19:27:07 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 #include <stdint.h>
28
29 #include "common/common.h"
30 #include "common/vlc.h"
31 #include "vlc.h"
32 #include "macroblock.h"
33
34 static const uint8_t block_idx_x[16] =
35 {
36     0, 1, 0, 1, 2, 3, 2, 3, 0, 1, 0, 1, 2, 3, 2, 3
37 };
38 static const uint8_t block_idx_y[16] =
39 {
40     0, 0, 1, 1, 0, 0, 1, 1, 2, 2, 3, 3, 2, 2, 3, 3
41 };
42 static const uint8_t block_idx_xy[4][4] =
43 {
44     { 0, 2, 8,  10},
45     { 1, 3, 9,  11},
46     { 4, 6, 12, 14},
47     { 5, 7, 13, 15}
48 };
49
50 static const int golomb_to_intra4x4_cbp[48]=
51 {
52     47, 31, 15,  0, 23, 27, 29, 30,  7, 11, 13, 14, 39, 43, 45, 46,
53     16,  3,  5, 10, 12, 19, 21, 26, 28, 35, 37, 42, 44,  1,  2,  4,
54      8, 17, 18, 20, 24,  6,  9, 22, 25, 32, 33, 34, 36, 40, 38, 41
55 };
56 static const int golomb_to_inter_cbp[48]=
57 {
58      0, 16,  1,  2,  4,  8, 32,  3,  5, 10, 12, 15, 47,  7, 11, 13,
59     14,  6,  9, 31, 35, 37, 42, 44, 33, 34, 36, 40, 39, 43, 45, 46,
60     17, 18, 20, 24, 19, 21, 26, 28, 23, 27, 29, 30, 22, 25, 38, 41
61 };
62
63 static const int i_chroma_qp_table[52] =
64 {
65      0,  1,  2,  3,  4,  5,  6,  7,  8,  9,
66     10, 11, 12, 13, 14, 15, 16, 17, 18, 19,
67     20, 21, 22, 23, 24, 25, 26, 27, 28, 29,
68     29, 30, 31, 32, 32, 33, 34, 34, 35, 35,
69     36, 36, 37, 37, 37, 38, 38, 38, 39, 39,
70     39, 39
71 };
72
73
74 void x264_mb_partition_ref_set( x264_macroblock_t *mb, int i_list, int i_part, int i_ref )
75 {
76     int x,  y;
77     int w,  h;
78     int dx, dy;
79
80     x264_mb_partition_getxy( mb, i_part, 0, &x, &y );
81     if( mb->i_partition == D_16x16 )
82     {
83         w = 4; h = 4;
84     }
85     else if( mb->i_partition == D_16x8 )
86     {
87         w = 4; h = 2;
88     }
89     else if( mb->i_partition == D_8x16 )
90     {
91         w = 2; h = 4;
92     }
93     else
94     {
95         /* D_8x8 */
96         w = 2; h = 2;
97     }
98
99     for( dx = 0; dx < w; dx++ )
100     {
101         for( dy = 0; dy < h; dy++ )
102         {
103             mb->partition[x+dx][y+dy].i_ref[i_list] = i_ref;
104         }
105     }
106 }
107
108 void x264_mb_partition_mv_set( x264_macroblock_t *mb, int i_list, int i_part, int i_sub, int mv[2] )
109 {
110     int x,  y;
111     int w,  h;
112     int dx, dy;
113
114     x264_mb_partition_getxy( mb, i_part, i_sub, &x, &y );
115     x264_mb_partition_size ( mb, i_part, i_sub, &w, &h );
116
117     for( dx = 0; dx < w; dx++ )
118     {
119         for( dy = 0; dy < h; dy++ )
120         {
121             mb->partition[x+dx][y+dy].mv[i_list][0] = mv[0];
122             mb->partition[x+dx][y+dy].mv[i_list][1] = mv[1];
123         }
124     }
125 }
126
127
128 int x264_macroblock_read_cabac( x264_t *h, bs_t *s, x264_macroblock_t *mb )
129 {
130     return -1;
131 }
132
133 static int x264_macroblock_decode_ipcm( x264_t *h, bs_t *s, x264_macroblock_t *mb )
134 {
135     /* TODO */
136     return -1;
137 }
138
139
140 #define BLOCK_INDEX_CHROMA_DC   (-1)
141 #define BLOCK_INDEX_LUMA_DC     (-2)
142
143 static int bs_read_vlc( bs_t *s, x264_vlc_table_t *table )
144 {
145     int i_nb_bits;
146     int i_value = 0;
147     int i_bits;
148     int i_index;
149     int i_level = 0;
150
151     i_index = bs_show( s, table->i_lookup_bits );
152     if( i_index >= table->i_lookup )
153     {
154         return( -1 );
155     }
156     i_value = table->lookup[i_index].i_value;
157     i_bits  = table->lookup[i_index].i_size;
158
159     while( i_bits < 0 )
160     {
161         i_level++;
162         if( i_level > 5 )
163         {
164             return( -1 );        // FIXME what to do ?
165         }
166         bs_skip( s, table->i_lookup_bits );
167         i_nb_bits = -i_bits;
168
169         i_index = bs_show( s, i_nb_bits ) + i_value;
170         if( i_index >= table->i_lookup )
171         {
172             return( -1 );
173         }
174         i_value = table->lookup[i_index].i_value;
175         i_bits  = table->lookup[i_index].i_size;
176     }
177     bs_skip( s, i_bits );
178
179     return( i_value );
180 }
181
182 static int block_residual_read_cavlc( x264_t *h, bs_t *s, x264_macroblock_t *mb,
183                                       int i_idx, int *l, int i_count )
184 {
185     int i;
186     int level[16], run[16];
187     int i_coeff;
188
189     int i_total, i_trailing;
190     int i_suffix_length;
191     int i_zero_left;
192
193     for( i = 0; i < i_count; i++ )
194     {
195         l[i] = 0;
196     }
197
198     /* total/trailing */
199     if( i_idx == BLOCK_INDEX_CHROMA_DC )
200     {
201         int i_tt;
202
203         if( ( i_tt = bs_read_vlc( s, h->x264_coeff_token_lookup[4] )) < 0 )
204         {
205             return -1;
206         }
207
208         i_total = i_tt / 4;
209         i_trailing = i_tt % 4;
210     }
211     else
212     {
213         /* x264_mb_predict_non_zero_code return 0 <-> (16+16+1)>>1 = 16 */
214         static const int ct_index[17] = {0,0,1,1,2,2,2,2,3,3,3,3,3,3,3,3,3 };
215         int nC;
216         int i_tt;
217
218         if( i_idx == BLOCK_INDEX_LUMA_DC )
219         {
220             nC = x264_mb_predict_non_zero_code( h, mb, 0 );
221         }
222         else
223         {
224             nC = x264_mb_predict_non_zero_code( h, mb, i_idx );
225         }
226
227         if( ( i_tt = bs_read_vlc( s, h->x264_coeff_token_lookup[ct_index[nC]] ) ) < 0 )
228         {
229             return -1;
230         }
231
232         i_total = i_tt / 4;
233         i_trailing = i_tt % 4;
234     }
235
236     if( i_idx >= 0 )
237     {
238         mb->block[i_idx].i_non_zero_count = i_total;
239     }
240
241     if( i_total <= 0 )
242     {
243         return 0;
244     }
245
246     i_suffix_length = i_total > 10 && i_trailing < 3 ? 1 : 0;
247
248     for( i = 0; i < i_trailing; i++ )
249     {
250         level[i] = 1 - 2 * bs_read1( s );
251     }
252
253     for( ; i < i_total; i++ )
254     {
255         int i_prefix;
256         int i_level_code;
257
258         i_prefix = bs_read_vlc( s, h->x264_level_prefix_lookup );
259
260         if( i_prefix == -1 )
261         {
262             return -1;
263         }
264         else if( i_prefix < 14 )
265         {
266             if( i_suffix_length > 0 )
267             {
268                 i_level_code = (i_prefix << i_suffix_length) + bs_read( s, i_suffix_length );
269             }
270             else
271             {
272                 i_level_code = i_prefix;
273             }
274         }
275         else if( i_prefix == 14 )
276         {
277             if( i_suffix_length > 0 )
278             {
279                 i_level_code = (i_prefix << i_suffix_length) + bs_read( s, i_suffix_length );
280             }
281             else
282             {
283                 i_level_code = i_prefix + bs_read( s, 4 );
284             }
285         }
286         else /* if( i_prefix == 15 ) */
287         {
288             i_level_code = (i_prefix << i_suffix_length) + bs_read( s, 12 );
289             if( i_suffix_length == 0 )
290             {
291                 i_level_code += 15;
292             }
293         }
294         if( i == i_trailing && i_trailing < 3 )
295         {
296             i_level_code += 2;
297         }
298         /* Optimise */
299         level[i] = i_level_code&0x01 ? -((i_level_code+1)/2) : (i_level_code+2)/2;
300
301         if( i_suffix_length == 0 )
302         {
303             i_suffix_length++;
304         }
305         if( abs( level[i] ) > ( 3 << ( i_suffix_length - 1 ) ) && i_suffix_length < 6 )
306         {
307             i_suffix_length++;
308         }
309     }
310
311     if( i_total < i_count )
312     {
313         if( i_idx == BLOCK_INDEX_CHROMA_DC )
314         {
315             i_zero_left = bs_read_vlc( s, h->x264_total_zeros_dc_lookup[i_total-1] );
316         }
317         else
318         {
319             i_zero_left = bs_read_vlc( s, h->x264_total_zeros_lookup[i_total-1] );
320         }
321         if( i_zero_left < 0 )
322         {
323             return -1;
324         }
325     }
326     else
327     {
328         i_zero_left = 0;
329     }
330
331     for( i = 0; i < i_total - 1; i++ )
332     {
333         if( i_zero_left <= 0 )
334         {
335             break;
336         }
337         run[i] = bs_read_vlc( s, h->x264_run_before_lookup[X264_MIN( i_zero_left - 1, 6 )] );
338
339         if( run[i] < 0 )
340         {
341             return -1;
342         }
343         i_zero_left -= run[i];
344     }
345     if( i_zero_left < 0 )
346     {
347         return -1;
348     }
349
350     for( ; i < i_total - 1; i++ )
351     {
352         run[i] = 0;
353     }
354     run[i_total-1] = i_zero_left;
355
356     i_coeff = -1;
357     for( i = i_total - 1; i >= 0; i-- )
358     {
359         i_coeff += run[i] + 1;
360         l[i_coeff] = level[i];
361     }
362
363     return 0;
364 }
365
366 static inline void array_zero_set( int *l, int i_count )
367 {
368     int i;
369
370     for( i = 0; i < i_count; i++ )
371     {
372         l[i] = 0;
373     }
374 }
375
376 int x264_macroblock_read_cavlc( x264_t *h, bs_t *s, x264_macroblock_t *mb )
377 {
378     int i_mb_i_offset;
379     int i_mb_p_offset;
380     int b_sub_ref0 = 0;
381     int i_type;
382     int i;
383
384     /* read the mb type */
385     switch( h->sh.i_type )
386     {
387         case SLICE_TYPE_I:
388             i_mb_p_offset = 0;  /* shut up gcc */
389             i_mb_i_offset = 0;
390             break;
391         case SLICE_TYPE_P:
392             i_mb_p_offset = 0;
393             i_mb_i_offset = 5;
394             break;
395         case SLICE_TYPE_B:
396             i_mb_p_offset = 23;
397             i_mb_i_offset = 23 + 5;
398             break;
399         default:
400             fprintf( stderr, "internal error or slice unsupported\n" );
401             return -1;
402     }
403
404     i_type = bs_read_ue( s );
405
406     if( i_type < i_mb_i_offset )
407     {
408         if( i_type < i_mb_p_offset )
409         {
410             fprintf( stderr, "unsupported mb type(B*)\n" );
411             /* TODO for B frame */
412             return -1;
413         }
414         else
415         {
416             i_type -= i_mb_p_offset;
417
418             if( i_type == 0 )
419             {
420                 mb->i_type = P_L0;
421                 mb->i_partition = D_16x16;
422             }
423             else if( i_type == 1 )
424             {
425                 mb->i_type = P_L0;
426                 mb->i_partition = D_16x8;
427             }
428             else if( i_type == 2 )
429             {
430                 mb->i_type = P_L0;
431                 mb->i_partition = D_8x16;
432             }
433             else if( i_type == 3 || i_type == 4 )
434             {
435                 mb->i_type = P_8x8;
436                 mb->i_partition = D_8x8;
437                 b_sub_ref0 = i_type == 4 ? 1 : 0;
438             }
439             else
440             {
441                 fprintf( stderr, "invalid mb type\n" );
442                 return -1;
443             }
444         }
445     }
446     else
447     {
448         i_type -= i_mb_i_offset;
449
450         if( i_type == 0 )
451         {
452             mb->i_type = I_4x4;
453         }
454         else if( i_type < 25 )
455         {
456             mb->i_type = I_16x16;
457
458             mb->i_intra16x16_pred_mode = (i_type - 1)%4;
459             mb->i_cbp_chroma = ( (i_type-1) / 4 )%3;
460             mb->i_cbp_luma   = ((i_type-1) / 12) ? 0x0f : 0x00;
461         }
462         else if( i_type == 25 )
463         {
464             mb->i_type = I_PCM;
465         }
466         else
467         {
468             fprintf( stderr, "invalid mb type (%d)\n", i_type );
469             return -1;
470         }
471     }
472
473     if( mb->i_type == I_PCM )
474     {
475         return x264_macroblock_decode_ipcm( h, s, mb );
476     }
477
478     if( IS_INTRA( mb->i_type ) )
479     {
480         if( mb->i_type == I_4x4 )
481         {
482             for( i = 0; i < 16; i++ )
483             {
484                 int b_coded;
485
486                 b_coded = bs_read1( s );
487
488                 if( b_coded )
489                 {
490                     mb->block[i].i_intra4x4_pred_mode = x264_mb_predict_intra4x4_mode( h, mb, i );
491                 }
492                 else
493                 {
494                     int i_predicted_mode = x264_mb_predict_intra4x4_mode( h, mb, i );
495                     int i_mode = bs_read( s, 3 );
496
497                     if( i_mode >= i_predicted_mode )
498                     {
499                         mb->block[i].i_intra4x4_pred_mode = i_mode + 1;
500                     }
501                     else
502                     {
503                         mb->block[i].i_intra4x4_pred_mode = i_mode;
504                     }
505                 }
506             }
507         }
508
509         mb->i_chroma_pred_mode = bs_read_ue( s );
510     }
511     else if( mb->i_type == P_8x8 || mb->i_type == B_8x8)
512     {
513         /* FIXME won't work for B_8x8 */
514
515         for( i = 0; i < 4; i++ )
516         {
517             int i_sub_partition;
518
519             i_sub_partition = bs_read_ue( s );
520             switch( i_sub_partition )
521             {
522                 case 0:
523                     mb->i_sub_partition[i] = D_L0_8x8;
524                     break;
525                 case 1:
526                     mb->i_sub_partition[i] = D_L0_8x4;
527                     break;
528                 case 2:
529                     mb->i_sub_partition[i] = D_L0_4x8;
530                     break;
531                 case 3:
532                     mb->i_sub_partition[i] = D_L0_4x4;
533                     break;
534                 default:
535                     fprintf( stderr, "invalid i_sub_partition\n" );
536                     return -1;
537             }
538         }
539         for( i = 0; i < 4; i++ )
540         {
541             int i_ref;
542
543             i_ref = b_sub_ref0 ? 0 : bs_read_te( s, h->sh.i_num_ref_idx_l0_active - 1 );
544             x264_mb_partition_ref_set( mb, 0, i, i_ref );
545         }
546         for( i = 0; i < 4; i++ )
547         {
548             int i_sub;
549             int i_ref;
550
551             x264_mb_partition_get( mb, 0, i, 0, &i_ref, NULL, NULL );
552
553             for( i_sub = 0; i_sub < x264_mb_partition_count_table[mb->i_sub_partition[i]]; i_sub++ )
554             {
555                 int mv[2];
556
557                 x264_mb_predict_mv( mb, 0, i, i_sub, mv );
558                 mv[0] += bs_read_se( s );
559                 mv[1] += bs_read_se( s );
560
561                 x264_mb_partition_mv_set( mb, 0, i, i_sub, mv );
562             }
563         }
564     }
565     else if( mb->i_type != B_DIRECT )
566     {
567         /* FIXME will work only for P block */
568
569         /* FIXME using x264_mb_partition_set/x264_mb_partition_get here are too unoptimised
570          * I should introduce ref and mv get/set */
571
572         /* Motion Vector */
573         int i_part = x264_mb_partition_count_table[mb->i_partition];
574
575         for( i = 0; i < i_part; i++ )
576         {
577             int i_ref;
578
579             i_ref = bs_read_te( s, h->sh.i_num_ref_idx_l0_active - 1 );
580
581             x264_mb_partition_ref_set( mb, 0, i, i_ref );
582         }
583
584         for( i = 0; i < i_part; i++ )
585         {
586             int mv[2];
587
588             x264_mb_predict_mv( mb, 0, i, 0, mv );
589
590             mv[0] += bs_read_se( s );
591             mv[1] += bs_read_se( s );
592
593             x264_mb_partition_mv_set( mb, 0, i, 0, mv );
594         }
595     }
596
597     if( mb->i_type != I_16x16 )
598     {
599         int i_cbp;
600
601         i_cbp = bs_read_ue( s );
602         if( i_cbp >= 48 )
603         {
604             fprintf( stderr, "invalid cbp\n" );
605             return -1;
606         }
607
608         if( mb->i_type == I_4x4 )
609         {
610             i_cbp = golomb_to_intra4x4_cbp[i_cbp];
611         }
612         else
613         {
614             i_cbp = golomb_to_inter_cbp[i_cbp];
615         }
616         mb->i_cbp_luma   = i_cbp&0x0f;
617         mb->i_cbp_chroma = i_cbp >> 4;
618     }
619
620     if( mb->i_cbp_luma > 0 || mb->i_cbp_chroma > 0 || mb->i_type == I_16x16 )
621     {
622         mb->i_qp = bs_read_se( s ) + h->pps->i_pic_init_qp + h->sh.i_qp_delta;
623
624         /* write residual */
625         if( mb->i_type == I_16x16 )
626         {
627             /* DC Luma */
628             if( block_residual_read_cavlc( h, s, mb, BLOCK_INDEX_LUMA_DC , mb->luma16x16_dc, 16 ) < 0 )
629             {
630                 return -1;
631             }
632
633             if( mb->i_cbp_luma != 0 )
634             {
635                 /* AC Luma */
636                 for( i = 0; i < 16; i++ )
637                 {
638                     if( block_residual_read_cavlc( h, s, mb, i, mb->block[i].residual_ac, 15 ) < 0 )
639                     {
640                         return -1;
641                     }
642                 }
643             }
644             else
645             {
646                 for( i = 0; i < 16; i++ )
647                 {
648                     mb->block[i].i_non_zero_count = 0;
649                     array_zero_set( mb->block[i].residual_ac, 15 );
650                 }
651             }
652         }
653         else
654         {
655             for( i = 0; i < 16; i++ )
656             {
657                 if( mb->i_cbp_luma & ( 1 << ( i / 4 ) ) )
658                 {
659                     if( block_residual_read_cavlc( h, s, mb, i, mb->block[i].luma4x4, 16 ) < 0 )
660                     {
661                         return -1;
662                     }
663                 }
664                 else
665                 {
666                     mb->block[i].i_non_zero_count = 0;
667                     array_zero_set( mb->block[i].luma4x4, 16 );
668                 }
669             }
670         }
671
672         if( mb->i_cbp_chroma &0x03 )    /* Chroma DC residual present */
673         {
674             if( block_residual_read_cavlc( h, s, mb, BLOCK_INDEX_CHROMA_DC, mb->chroma_dc[0], 4 ) < 0 ||
675                 block_residual_read_cavlc( h, s, mb, BLOCK_INDEX_CHROMA_DC, mb->chroma_dc[1], 4 ) < 0 )
676             {
677                 return -1;
678             }
679         }
680         else
681         {
682             array_zero_set( mb->chroma_dc[0], 4 );
683             array_zero_set( mb->chroma_dc[1], 4 );
684         }
685         if( mb->i_cbp_chroma&0x02 ) /* Chroma AC residual present */
686         {
687             for( i = 0; i < 8; i++ )
688             {
689                 if( block_residual_read_cavlc( h, s, mb, 16 + i, mb->block[16+i].residual_ac, 15 ) < 0 )
690                 {
691                     return -1;
692                 }
693             }
694         }
695         else
696         {
697             for( i = 0; i < 8; i++ )
698             {
699                 mb->block[16+i].i_non_zero_count = 0;
700                 array_zero_set( mb->block[16+i].residual_ac, 15 );
701             }
702         }
703     }
704     else
705     {
706         mb->i_qp = h->pps->i_pic_init_qp + h->sh.i_qp_delta;
707         for( i = 0; i < 16; i++ )
708         {
709             mb->block[i].i_non_zero_count = 0;
710             array_zero_set( mb->block[i].luma4x4, 16 );
711         }
712         array_zero_set( mb->chroma_dc[0], 4 );
713         array_zero_set( mb->chroma_dc[1], 4 );
714         for( i = 0; i < 8; i++ )
715         {
716             array_zero_set( mb->block[16+i].residual_ac, 15 );
717             mb->block[16+i].i_non_zero_count = 0;
718         }
719     }
720
721     //fprintf( stderr, "mb read type=%d\n", mb->i_type );
722
723     return 0;
724 }
725
726
727
728
729 static int x264_mb_pred_mode16x16_valid( x264_macroblock_t *mb, int i_mode )
730 {
731     if( ( mb->i_neighbour & (MB_LEFT|MB_TOP) ) == (MB_LEFT|MB_TOP) )
732     {
733         return i_mode;
734     }
735     else if( ( mb->i_neighbour & MB_LEFT ) )
736     {
737         if( i_mode == I_PRED_16x16_DC )
738         {
739             return I_PRED_16x16_DC_LEFT;
740         }
741         else if( i_mode == I_PRED_16x16_H )
742         {
743             return I_PRED_16x16_H;
744         }
745
746         fprintf( stderr, "invalid 16x16 prediction\n" );
747         return I_PRED_16x16_DC_LEFT;
748     }
749     else if( ( mb->i_neighbour & MB_TOP ) )
750     {
751         if( i_mode == I_PRED_16x16_DC )
752         {
753             return I_PRED_16x16_DC_TOP;
754         }
755         else if( i_mode == I_PRED_16x16_V )
756         {
757             return I_PRED_16x16_V;
758         }
759
760         fprintf( stderr, "invalid 16x16 prediction\n" );
761         return I_PRED_16x16_DC_TOP;
762     }
763     else
764     {
765         return I_PRED_16x16_DC_128;
766     }
767 }
768
769 static int x264_mb_pred_mode8x8_valid( x264_macroblock_t *mb, int i_mode )
770 {
771     if( ( mb->i_neighbour & (MB_LEFT|MB_TOP) ) == (MB_LEFT|MB_TOP) )
772     {
773         return i_mode;
774     }
775     else if( ( mb->i_neighbour & MB_LEFT ) )
776     {
777         if( i_mode == I_PRED_CHROMA_DC )
778         {
779             return I_PRED_CHROMA_DC_LEFT;
780         }
781         else if( i_mode == I_PRED_CHROMA_H )
782         {
783             return I_PRED_CHROMA_H;
784         }
785
786         fprintf( stderr, "invalid 8x8 prediction\n" );
787         return I_PRED_CHROMA_DC_LEFT;
788     }
789     else if( ( mb->i_neighbour & MB_TOP ) )
790     {
791         if( i_mode == I_PRED_CHROMA_DC )
792         {
793             return I_PRED_CHROMA_DC_TOP;
794         }
795         else if( i_mode == I_PRED_CHROMA_V )
796         {
797             return I_PRED_CHROMA_V;
798         }
799
800         fprintf( stderr, "invalid 8x8 prediction\n" );
801         return I_PRED_CHROMA_DC_TOP;
802     }
803     else
804     {
805         return I_PRED_CHROMA_DC_128;
806     }
807 }
808
809 static int x264_mb_pred_mode4x4_valid( x264_macroblock_t *mb, int idx, int i_mode, int *pb_emu )
810 {
811     int b_a, b_b, b_c;
812     static const int needmb[16] =
813     {
814         MB_LEFT|MB_TOP, MB_TOP,
815         MB_LEFT,        MB_PRIVATE,
816         MB_TOP,         MB_TOP|MB_TOPRIGHT,
817         0,              MB_PRIVATE,
818         MB_LEFT,        0,
819         MB_LEFT,        MB_PRIVATE,
820         0,              MB_PRIVATE,
821         0,              MB_PRIVATE
822     };
823     int b_emu = 0;
824
825     *pb_emu = 0;
826
827     b_a = (needmb[idx]&mb->i_neighbour&MB_LEFT) == (needmb[idx]&MB_LEFT);
828     b_b = (needmb[idx]&mb->i_neighbour&MB_TOP) == (needmb[idx]&MB_TOP);
829     b_c = (needmb[idx]&mb->i_neighbour&(MB_TOPRIGHT|MB_PRIVATE)) == (needmb[idx]&(MB_TOPRIGHT|MB_PRIVATE));
830
831     if( b_c == 0 && b_b )
832     {
833         b_emu = 1;
834         b_c = 1;
835     }
836
837     /* handle I_PRED_4x4_DC */
838     if( i_mode == I_PRED_4x4_DC )
839     {
840         if( b_a && b_b )
841         {
842             return I_PRED_4x4_DC;
843         }
844         else if( b_a )
845         {
846             return I_PRED_4x4_DC_LEFT;
847         }
848         else if( b_b )
849         {
850             return I_PRED_4x4_DC_TOP;
851         }
852         return I_PRED_4x4_DC_128;
853     }
854
855     /* handle 1 dir needed only */
856     if( ( b_a && i_mode == I_PRED_4x4_H ) ||
857         ( b_b && i_mode == I_PRED_4x4_V ) )
858     {
859         return i_mode;
860     }
861
862     /* handle b_c case (b_b always true) */
863     if( b_c && ( i_mode == I_PRED_4x4_DDL || i_mode == I_PRED_4x4_VL ) )
864     {
865         *pb_emu = b_emu;
866         return i_mode;
867     }
868
869     if( b_a && b_b )
870     {
871         /* I_PRED_4x4_DDR, I_PRED_4x4_VR, I_PRED_4x4_HD, I_PRED_4x4_HU */
872         return i_mode;
873     }
874
875     fprintf( stderr, "invalid 4x4 predict mode(%d, mb:%x-%x idx:%d\n", i_mode, mb->i_mb_x, mb->i_mb_y, idx );
876     return I_PRED_CHROMA_DC_128;    /* unefficient */
877 }
878
879 /****************************************************************************
880  * UnScan functions
881  ****************************************************************************/
882 static const int scan_zigzag_x[16]={0, 1, 0, 0, 1, 2, 3, 2, 1, 0, 1, 2, 3, 3, 2, 3};
883 static const int scan_zigzag_y[16]={0, 0, 1, 2, 1, 0, 0, 1, 2, 3, 3, 2, 1, 2, 3, 3};
884
885 static inline void unscan_zigzag_4x4full( int16_t dct[4][4], int level[16] )
886 {
887     int i;
888
889     for( i = 0; i < 16; i++ )
890     {
891         dct[scan_zigzag_y[i]][scan_zigzag_x[i]] = level[i];
892     }
893 }
894 static inline void unscan_zigzag_4x4( int16_t dct[4][4], int level[15] )
895 {
896     int i;
897
898     for( i = 1; i < 16; i++ )
899     {
900         dct[scan_zigzag_y[i]][scan_zigzag_x[i]] = level[i - 1];
901     }
902 }
903
904 static inline void unscan_zigzag_2x2_dc( int16_t dct[2][2], int level[4] )
905 {
906     dct[0][0] = level[0];
907     dct[0][1] = level[1];
908     dct[1][0] = level[2];
909     dct[1][1] = level[3];
910 }
911
912
913 int x264_macroblock_decode( x264_t *h, x264_macroblock_t *mb )
914 {
915     x264_mb_context_t *ctx = mb->context;
916
917     int i_qscale;
918     int ch;
919     int i;
920
921     if( !IS_INTRA(mb->i_type ) )
922     {
923         /* Motion compensation */
924         x264_mb_mc( h, mb );
925     }
926
927     /* luma */
928     i_qscale = mb->i_qp;
929     if( mb->i_type == I_16x16 )
930     {
931         int     i_mode = x264_mb_pred_mode16x16_valid( mb, mb->i_intra16x16_pred_mode );
932         int16_t luma[16][4][4];
933         int16_t dct4x4[16+1][4][4];
934
935
936         /* do the right prediction */
937         h->predict_16x16[i_mode]( ctx->p_fdec[0], ctx->i_fdec[0] );
938
939         /* get dc coeffs */
940         unscan_zigzag_4x4full( dct4x4[0], mb->luma16x16_dc );
941         h->dctf.idct4x4dc( dct4x4[0], dct4x4[0] );
942         x264_mb_dequant_4x4_dc( dct4x4[0], i_qscale );
943
944         /* decode the 16x16 macroblock */
945         for( i = 0; i < 16; i++ )
946         {
947             unscan_zigzag_4x4( dct4x4[1+i], mb->block[i].residual_ac );
948             x264_mb_dequant_4x4( dct4x4[1+i], i_qscale );
949
950             /* copy dc coeff */
951             dct4x4[1+i][0][0] = dct4x4[0][block_idx_y[i]][block_idx_x[i]];
952
953             h->dctf.idct4x4( luma[i], dct4x4[i+1] );
954         }
955         /* put pixels to fdec */
956         h->pixf.add16x16( ctx->p_fdec[0], ctx->i_fdec[0], luma );
957     }
958     else if( mb->i_type == I_4x4 )
959     {
960         for( i = 0; i < 16; i++ )
961         {
962             int16_t luma[4][4];
963             int16_t dct4x4[4][4];
964
965             uint8_t *p_dst_by;
966             int     i_mode;
967             int     b_emu;
968
969             /* Do the right prediction */
970             p_dst_by = ctx->p_fdec[0] + 4 * block_idx_x[i] + 4 * block_idx_y[i] * ctx->i_fdec[0];
971             i_mode   = x264_mb_pred_mode4x4_valid( mb, i, mb->block[i].i_intra4x4_pred_mode, &b_emu );
972             if( b_emu )
973             {
974                 fprintf( stderr, "mmmh b_emu\n" );
975                 memset( &p_dst_by[4], p_dst_by[3], 4 );
976             }
977             h->predict_4x4[i_mode]( p_dst_by, ctx->i_fdec[0] );
978
979             if( mb->block[i].i_non_zero_count > 0 )
980             {
981                 /* decode one 4x4 block */
982                 unscan_zigzag_4x4full( dct4x4, mb->block[i].luma4x4 );
983
984                 x264_mb_dequant_4x4( dct4x4, i_qscale );
985
986                 h->dctf.idct4x4( luma, dct4x4 );
987
988                 h->pixf.add4x4( p_dst_by, ctx->i_fdec[0], luma );
989             }
990         }
991     }
992     else /* Inter mb */
993     {
994         for( i = 0; i < 16; i++ )
995         {
996             uint8_t *p_dst_by;
997             int16_t luma[4][4];
998             int16_t dct4x4[4][4];
999
1000             if( mb->block[i].i_non_zero_count > 0 )
1001             {
1002                 unscan_zigzag_4x4full( dct4x4, mb->block[i].luma4x4 );
1003                 x264_mb_dequant_4x4( dct4x4, i_qscale );
1004
1005                 h->dctf.idct4x4( luma, dct4x4 );
1006
1007                 p_dst_by = ctx->p_fdec[0] + 4 * block_idx_x[i] + 4 * block_idx_y[i] * ctx->i_fdec[0];
1008                 h->pixf.add4x4( p_dst_by, ctx->i_fdec[0], luma );
1009             }
1010         }
1011     }
1012
1013     /* chroma */
1014     i_qscale = i_chroma_qp_table[x264_clip3( i_qscale + h->pps->i_chroma_qp_index_offset, 0, 51 )];
1015     if( IS_INTRA( mb->i_type ) )
1016     {
1017         int i_mode = x264_mb_pred_mode8x8_valid( mb, mb->i_chroma_pred_mode );
1018         /* do the right prediction */
1019         h->predict_8x8[i_mode]( ctx->p_fdec[1], ctx->i_fdec[1] );
1020         h->predict_8x8[i_mode]( ctx->p_fdec[2], ctx->i_fdec[2] );
1021     }
1022
1023     if( mb->i_cbp_chroma != 0 )
1024     {
1025         for( ch = 0; ch < 2; ch++ )
1026         {
1027             int16_t chroma[4][4][4];
1028             int16_t dct2x2[2][2];
1029             int16_t dct4x4[4][4][4];
1030
1031             /* get dc chroma */
1032             unscan_zigzag_2x2_dc( dct2x2, mb->chroma_dc[ch] );
1033             h->dctf.idct2x2dc( dct2x2, dct2x2 );
1034             x264_mb_dequant_2x2_dc( dct2x2, i_qscale );
1035
1036             for( i = 0; i < 4; i++ )
1037             {
1038                 unscan_zigzag_4x4( dct4x4[i], mb->block[16+i+ch*4].residual_ac );
1039                 x264_mb_dequant_4x4( dct4x4[i], i_qscale );
1040
1041                 /* copy dc coeff */
1042                 dct4x4[i][0][0] = dct2x2[block_idx_y[i]][block_idx_x[i]];
1043
1044                 h->dctf.idct4x4( chroma[i], dct4x4[i] );
1045             }
1046             h->pixf.add8x8( ctx->p_fdec[1+ch], ctx->i_fdec[1+ch], chroma );
1047         }
1048     }
1049
1050     return 0;
1051 }
1052
1053 void x264_macroblock_decode_skip( x264_t *h, x264_macroblock_t *mb )
1054 {
1055     int i;
1056     int x, y;
1057     int mv[2];
1058
1059     /* decode it as a 16x16 with no luma/chroma */
1060     mb->i_type = P_L0;
1061     mb->i_partition = D_16x16;
1062     mb->i_cbp_luma = 0;
1063     mb->i_cbp_chroma = 0;
1064     for( i = 0; i < 16 + 8; i++ )
1065     {
1066         mb->block[i].i_non_zero_count = 0;
1067     }
1068     for( i = 0; i < 16; i++ )
1069     {
1070         array_zero_set( mb->block[i].luma4x4, 16 );
1071     }
1072     array_zero_set( mb->chroma_dc[0], 4 );
1073     array_zero_set( mb->chroma_dc[1], 4 );
1074     for( i = 0; i < 8; i++ )
1075     {
1076         array_zero_set( mb->block[16+i].residual_ac, 15 );
1077     }
1078
1079     /* set ref0 */
1080     for( x = 0; x < 4; x++ )
1081     {
1082         for( y = 0; y < 4; y++ )
1083         {
1084             mb->partition[x][y].i_ref[0] = 0;
1085         }
1086     }
1087     /* get mv */
1088     x264_mb_predict_mv_pskip( mb, mv );
1089
1090     x264_mb_partition_mv_set( mb, 0, 0, 0, mv );
1091
1092     /* Motion compensation */
1093     x264_mb_mc( h, mb );
1094
1095     mb->i_type = P_SKIP;
1096 }
1097