]> git.sesse.net Git - vlc/blob - src/video_parser/vpar_blocks.c
17d23f27799a6098c3dd5ef7a8509d625b6ea8e7
[vlc] / src / video_parser / vpar_blocks.c
1 /*****************************************************************************
2  * vpar_blocks.c : blocks parsing
3  * (c)1999 VideoLAN
4  *****************************************************************************/
5
6 /*****************************************************************************
7  * Preamble
8  *****************************************************************************/
9 #include <errno.h>
10 #include <stdlib.h>
11 #include <stdio.h>
12 #include <unistd.h>
13 #include <string.h>
14 #include <sys/uio.h>
15 #include <X11/Xlib.h>
16 #include <X11/extensions/XShm.h>
17
18 #include "config.h"
19 #include "common.h"
20 #include "mtime.h"
21 #include "vlc_thread.h"
22
23 #include "intf_msg.h"
24 #include "debug.h"                    /* ?? temporaire, requis par netlist.h */
25
26 #include "input.h"
27 #include "input_netlist.h"
28 #include "decoder_fifo.h"
29 #include "video.h"
30 #include "video_output.h"
31
32 #include "vdec_idct.h"
33 #include "video_decoder.h"
34 #include "vdec_motion.h"
35
36 #include "vpar_blocks.h"
37 #include "vpar_headers.h"
38 #include "video_fifo.h"
39 #include "vpar_synchro.h"
40 #include "video_parser.h"
41
42 /*
43  * Local prototypes
44  */
45 static __inline__ void InitMacroblock( vpar_thread_t * p_vpar,
46                                        macroblock_t * p_mb );
47 static __inline__ int MacroblockAddressIncrement( vpar_thread_t * p_vpar );
48 static __inline__ void MacroblockModes( vpar_thread_t * p_vpar,
49                                         macroblock_t * p_mb );
50 typedef void (*f_decode_block_t)( vpar_thread_t *, macroblock_t *, int );
51 static void vpar_DecodeMPEG1Non( vpar_thread_t * p_vpar, macroblock_t * p_mb, int i_b );
52 static void vpar_DecodeMPEG1Intra( vpar_thread_t * p_vpar, macroblock_t * p_mb, int i_b );
53 static void vpar_DecodeMPEG2Non( vpar_thread_t * p_vpar, macroblock_t * p_mb, int i_b );
54 static void vpar_DecodeMPEG2Intra( vpar_thread_t * p_vpar, macroblock_t * p_mb, int i_b );
55
56 /*
57  * Initialisation tables
58  */
59     /* Table for coded_block_pattern resolution */
60 lookup_t     pl_coded_pattern_init_table[512] = 
61     { {MB_ERROR, 0}, {0, 9}, {39, 9}, {27, 9}, {59, 9}, {55, 9}, {47, 9}, {31, 9},
62     {58, 8}, {58, 8}, {54, 8}, {54, 8}, {46, 8}, {46, 8}, {30, 8}, {30, 8},
63     {57, 8}, {57, 8}, {53, 8}, {53, 8}, {45, 8}, {45, 8}, {29, 8}, {29, 8},
64     {38, 8}, {38, 8}, {26, 8}, {26, 8}, {37, 8}, {37, 8}, {25, 8}, {25, 8},
65     {43, 8}, {43, 8}, {23, 8}, {23, 8}, {51, 8}, {51, 8}, {15, 8}, {15, 8},
66     {42, 8}, {42, 8}, {22, 8}, {22, 8}, {50, 8}, {50, 8}, {14, 8}, {14, 8},
67     {41, 8}, {41, 8}, {21, 8}, {21, 8}, {49, 8}, {49, 8}, {13, 8}, {13, 8},
68     {35, 8}, {35, 8}, {19, 8}, {19, 8}, {11, 8}, {11, 8}, {7, 8}, {7, 8},
69     {34, 7}, {34, 7}, {34, 7}, {34, 7}, {18, 7}, {18, 7}, {18, 7}, {18, 7},
70     {10, 7}, {10, 7}, {10, 7}, {10, 7}, {6, 7}, {6, 7}, {6, 7}, {6, 7}, 
71     {33, 7}, {33, 7}, {33, 7}, {33, 7}, {17, 7}, {17, 7}, {17, 7}, {17, 7}, 
72     {9, 7}, {9, 7}, {9, 7}, {9, 7}, {5, 7}, {5, 7}, {5, 7}, {5, 7}, 
73     {63, 6}, {63, 6}, {63, 6}, {63, 6}, {63, 6}, {63, 6}, {63, 6}, {63, 6}, 
74     {3, 6}, {3, 6}, {3, 6}, {3, 6}, {3, 6}, {3, 6}, {3, 6}, {3, 6}, 
75     {36, 6}, {36, 6}, {36, 6}, {36, 6}, {36, 6}, {36, 6}, {36, 6}, {36, 6}, 
76     {24, 6}, {24, 6}, {24, 6}, {24, 6}, {24, 6}, {24, 6}, {24, 6}, {24, 6}, 
77     {62, 5}, {62, 5}, {62, 5}, {62, 5}, {62, 5}, {62, 5}, {62, 5}, {62, 5},
78     {62, 5}, {62, 5}, {62, 5}, {62, 5}, {62, 5}, {62, 5}, {62, 5}, {62, 5},
79     {2, 5}, {2, 5}, {2, 5}, {2, 5}, {2, 5}, {2, 5}, {2, 5}, {2, 5}, 
80     {2, 5}, {2, 5}, {2, 5}, {2, 5}, {2, 5}, {2, 5}, {2, 5}, {2, 5}, 
81     {61, 5}, {61, 5}, {61, 5}, {61, 5}, {61, 5}, {61, 5}, {61, 5}, {61, 5}, 
82     {61, 5}, {61, 5}, {61, 5}, {61, 5}, {61, 5}, {61, 5}, {61, 5}, {61, 5}, 
83     {1, 5}, {1, 5}, {1, 5}, {1, 5}, {1, 5}, {1, 5}, {1, 5}, {1, 5}, 
84     {1, 5}, {1, 5}, {1, 5}, {1, 5}, {1, 5}, {1, 5}, {1, 5}, {1, 5}, 
85     {56, 5}, {56, 5}, {56, 5}, {56, 5}, {56, 5}, {56, 5}, {56, 5}, {56, 5}, 
86     {56, 5}, {56, 5}, {56, 5}, {56, 5}, {56, 5}, {56, 5}, {56, 5}, {56, 5}, 
87     {52, 5}, {52, 5}, {52, 5}, {52, 5}, {52, 5}, {52, 5}, {52, 5}, {52, 5}, 
88     {52, 5}, {52, 5}, {52, 5}, {52, 5}, {52, 5}, {52, 5}, {52, 5}, {52, 5}, 
89     {44, 5}, {44, 5}, {44, 5}, {44, 5}, {44, 5}, {44, 5}, {44, 5}, {44, 5}, 
90     {44, 5}, {44, 5}, {44, 5}, {44, 5}, {44, 5}, {44, 5}, {44, 5}, {44, 5}, 
91     {28, 5}, {28, 5}, {28, 5}, {28, 5}, {28, 5}, {28, 5}, {28, 5}, {28, 5}, 
92     {28, 5}, {28, 5}, {28, 5}, {28, 5}, {28, 5}, {28, 5}, {28, 5}, {28, 5}, 
93     {40, 5}, {40, 5}, {40, 5}, {40, 5}, {40, 5}, {40, 5}, {40, 5}, {40, 5}, 
94     {40, 5}, {40, 5}, {40, 5}, {40, 5}, {40, 5}, {40, 5}, {40, 5}, {40, 5}, 
95     {20, 5}, {20, 5}, {20, 5}, {20, 5}, {20, 5}, {20, 5}, {20, 5}, {20, 5}, 
96     {20, 5}, {20, 5}, {20, 5}, {20, 5}, {20, 5}, {20, 5}, {20, 5}, {20, 5}, 
97     {48, 5}, {48, 5}, {48, 5}, {48, 5}, {48, 5}, {48, 5}, {48, 5}, {48, 5}, 
98     {48, 5}, {48, 5}, {48, 5}, {48, 5}, {48, 5}, {48, 5}, {48, 5}, {48, 5}, 
99     {12, 5}, {12, 5}, {12, 5}, {12, 5}, {12, 5}, {12, 5}, {12, 5}, {12, 5}, 
100     {12, 5}, {12, 5}, {12, 5}, {12, 5}, {12, 5}, {12, 5}, {12, 5}, {12, 5}, 
101     {32, 4}, {32, 4}, {32, 4}, {32, 4}, {32, 4}, {32, 4}, {32, 4}, {32, 4}, 
102     {32, 4}, {32, 4}, {32, 4}, {32, 4}, {32, 4}, {32, 4}, {32, 4}, {32, 4}, 
103     {32, 4}, {32, 4}, {32, 4}, {32, 4}, {32, 4}, {32, 4}, {32, 4}, {32, 4}, 
104     {32, 4}, {32, 4}, {32, 4}, {32, 4}, {32, 4}, {32, 4}, {32, 4}, {32, 4}, 
105     {16, 4}, {16, 4}, {16, 4}, {16, 4}, {16, 4}, {16, 4}, {16, 4}, {16, 4}, 
106     {16, 4}, {16, 4}, {16, 4}, {16, 4}, {16, 4}, {16, 4}, {16, 4}, {16, 4}, 
107     {16, 4}, {16, 4}, {16, 4}, {16, 4}, {16, 4}, {16, 4}, {16, 4}, {16, 4}, 
108     {16, 4}, {16, 4}, {16, 4}, {16, 4}, {16, 4}, {16, 4}, {16, 4}, {16, 4}, 
109     {8, 4}, {8, 4}, {8, 4}, {8, 4}, {8, 4}, {8, 4}, {8, 4}, {8, 4}, 
110     {8, 4}, {8, 4}, {8, 4}, {8, 4}, {8, 4}, {8, 4}, {8, 4}, {8, 4}, 
111     {8, 4}, {8, 4}, {8, 4}, {8, 4}, {8, 4}, {8, 4}, {8, 4}, {8, 4}, 
112     {8, 4}, {8, 4}, {8, 4}, {8, 4}, {8, 4}, {8, 4}, {8, 4}, {8, 4},
113     {4, 4}, {4, 4}, {4, 4}, {4, 4}, {4, 4}, {4, 4}, {4, 4}, {4, 4},
114     {4, 4}, {4, 4}, {4, 4}, {4, 4}, {4, 4}, {4, 4}, {4, 4}, {4, 4}, 
115     {4, 4}, {4, 4}, {4, 4}, {4, 4}, {4, 4}, {4, 4}, {4, 4}, {4, 4}, 
116     {4, 4}, {4, 4}, {4, 4}, {4, 4}, {4, 4}, {4, 4}, {4, 4}, {4, 4},
117     {60, 3}, {60, 3}, {60, 3}, {60, 3}, {60, 3}, {60, 3}, {60, 3}, {60, 3}, 
118     {60, 3}, {60, 3}, {60, 3}, {60, 3}, {60, 3}, {60, 3}, {60, 3}, {60, 3}, 
119     {60, 3}, {60, 3}, {60, 3}, {60, 3}, {60, 3}, {60, 3}, {60, 3}, {60, 3}, 
120     {60, 3}, {60, 3}, {60, 3}, {60, 3}, {60, 3}, {60, 3}, {60, 3}, {60, 3}, 
121     {60, 3}, {60, 3}, {60, 3}, {60, 3}, {60, 3}, {60, 3}, {60, 3}, {60, 3}, 
122     {60, 3}, {60, 3}, {60, 3}, {60, 3}, {60, 3}, {60, 3}, {60, 3}, {60, 3}, 
123     {60, 3}, {60, 3}, {60, 3}, {60, 3}, {60, 3}, {60, 3}, {60, 3}, {60, 3}, 
124     {60, 3}, {60, 3}, {60, 3}, {60, 3}, {60, 3}, {60, 3}, {60, 3}, {60, 3} };
125
126     /* Tables for dc DCT coefficients 
127      * Tables are cut in two parts to reduce memory occupation
128      */
129     
130     /* Table B-12, dct_dc_size_luminance, codes 00xxx ... 11110 */
131 lookup_t     pl_dct_dc_lum_init_table_1[32] =
132     { {1, 2}, {1, 2}, {1, 2}, {1, 2}, {1, 2}, {1, 2}, {1, 2}, {1, 2},
133       {2, 2}, {2, 2}, {2, 2}, {2, 2}, {2, 2}, {2, 2}, {2, 2}, {2, 2},
134       {0, 3}, {0, 3}, {0, 3}, {0, 3}, {3, 3}, {3, 3}, {3, 3}, {3, 3},
135       {4, 3}, {4, 3}, {4, 3}, {4, 3}, {5, 4}, {5, 4}, {6, 5}, {MB_ERROR, 0}
136     };
137
138     /* Table B-12, dct_dc_size_luminance, codes 111110xxx ... 111111111 */
139 lookup_t     pl_dct_dc_lum_init_table_2[32] =
140     { {7, 6}, {7, 6}, {7, 6}, {7, 6}, {7, 6}, {7, 6}, {7, 6}, {7, 6},
141       {8, 7}, {8, 7}, {8, 7}, {8, 7}, {9, 8}, {9, 8}, {10,9}, {11,9},
142       {MB_ERROR, 0}, {MB_ERROR, 0}, {MB_ERROR, 0}, {MB_ERROR, 0},
143       {MB_ERROR, 0}, {MB_ERROR, 0}, {MB_ERROR, 0}, {MB_ERROR, 0},
144       {MB_ERROR, 0}, {MB_ERROR, 0}, {MB_ERROR, 0}, {MB_ERROR, 0},
145       {MB_ERROR, 0}, {MB_ERROR, 0}, {MB_ERROR, 0}, {MB_ERROR, 0}
146     };
147
148     /* Table B-13, dct_dc_size_chrominance, codes 00xxx ... 11110 */
149 lookup_t     pl_dct_dc_chrom_init_table_1[32] =
150     { {0, 2}, {0, 2}, {0, 2}, {0, 2}, {0, 2}, {0, 2}, {0, 2}, {0, 2},
151       {1, 2}, {1, 2}, {1, 2}, {1, 2}, {1, 2}, {1, 2}, {1, 2}, {1, 2},
152       {2, 2}, {2, 2}, {2, 2}, {2, 2}, {2, 2}, {2, 2}, {2, 2}, {2, 2},
153       {3, 3}, {3, 3}, {3, 3}, {3, 3}, {4, 4}, {4, 4}, {5, 5}, {MB_ERROR, 0}
154     };
155
156     /* Table B-13, dct_dc_size_chrominance, codes 111110xxxx ... 1111111111 */
157 lookup_t     pl_dct_dc_chrom_init_table_2[32] =
158     { {6, 6}, {6, 6}, {6, 6}, {6, 6}, {6, 6}, {6, 6}, {6, 6}, {6, 6},
159       {6, 6}, {6, 6}, {6, 6}, {6, 6}, {6, 6}, {6, 6}, {6, 6}, {6, 6},
160       {7, 7}, {7, 7}, {7, 7}, {7, 7}, {7, 7}, {7, 7}, {7, 7}, {7, 7},
161       {8, 8}, {8, 8}, {8, 8}, {8, 8}, {9, 9}, {9, 9}, {10,10}, {11,10}
162     };
163     
164     /* 
165      * Structure to store the tables B14 & B15 
166      * Is constructed from the tables below 
167      */
168     dct_lookup_t            ppl_dct_coef[2][16384];
169
170
171     /* Tables for ac DCT coefficients. There are cut in many parts to save space */
172     /* Table B-14, DCT coefficients table zero,
173      * codes 0100 ... 1xxx (used for first (DC) coefficient)
174      */
175 dct_lookup_t pl_DCT_tab_dc[12] =
176     {
177         {0,2,4}, {2,1,4}, {1,1,3}, {1,1,3},
178         {0,1,1}, {0,1,1}, {0,1,1}, {0,1,1},
179         {0,1,1}, {0,1,1}, {0,1,1}, {0,1,1}
180     };
181
182     /* Table B-14, DCT coefficients table zero,
183      * codes 0100 ... 1xxx (used for all other coefficients)
184      */
185 dct_lookup_t pl_DCT_tab_ac[12] =
186     {
187         {0,2,4},  {2,1,4},  {1,1,3},  {1,1,3},
188         {DCT_EOB,0,2}, {DCT_EOB,0,2}, {DCT_EOB,0,2}, {DCT_EOB,0,2}, /* EOB */
189         {0,1,2},  {0,1,2},  {0,1,2},  {0,1,2}
190     };
191
192     /* Table B-14, DCT coefficients table zero,
193      * codes 000001xx ... 00111xxx
194      */
195 dct_lookup_t pl_DCT_tab0[60] =
196     {
197         {DCT_ESCAPE,0,6}, {DCT_ESCAPE,0,6}, {DCT_ESCAPE,0,6}, {DCT_ESCAPE,0,6},
198         /* Escape */
199         {2,2,7}, {2,2,7}, {9,1,7}, {9,1,7},
200         {0,4,7}, {0,4,7}, {8,1,7}, {8,1,7},
201         {7,1,6}, {7,1,6}, {7,1,6}, {7,1,6},
202         {6,1,6}, {6,1,6}, {6,1,6}, {6,1,6},
203         {1,2,6}, {1,2,6}, {1,2,6}, {1,2,6},
204         {5,1,6}, {5,1,6}, {5,1,6}, {5,1,6},
205         {13,1,8}, {0,6,8}, {12,1,8}, {11,1,8},
206         {3,2,8}, {1,3,8}, {0,5,8}, {10,1,8},
207         {0,3,5}, {0,3,5}, {0,3,5}, {0,3,5},
208         {0,3,5}, {0,3,5}, {0,3,5}, {0,3,5},
209         {4,1,5}, {4,1,5}, {4,1,5}, {4,1,5},
210         {4,1,5}, {4,1,5}, {4,1,5}, {4,1,5},
211         {3,1,5}, {3,1,5}, {3,1,5}, {3,1,5},
212         {3,1,5}, {3,1,5}, {3,1,5}, {3,1,5}
213     };
214
215     /* Table B-15, DCT coefficients table one,
216      * codes 000001xx ... 11111111
217      */
218 #if 0
219 dct_lookup_t pl_DCT_tab0a[252] =
220     {
221         {DCT_ESCAPE,0,6}, {DCT_ESCAPE,0,6}, {DCT_ESCAPE,0,6}, {DCT_ESCAPE,0,6},
222         /* Escape */
223         {7,1,7}, {7,1,7}, {8,1,7}, {8,1,7},
224         {6,1,7}, {6,1,7}, {2,2,7}, {2,2,7},
225         {0,7,6}, {0,7,6}, {0,7,6}, {0,7,6},
226         {0,6,6}, {0,6,6}, {0,6,6}, {0,6,6},
227         {4,1,6}, {4,1,6}, {4,1,6}, {4,1,6},
228         {5,1,6}, {5,1,6}, {5,1,6}, {5,1,6},
229         {1,5,8}, {11,1,8}, {0,11,8}, {0,10,8},
230         {13,1,8}, {12,1,8}, {3,2,8}, {1,4,8},
231         {2,1,5}, {2,1,5}, {2,1,5}, {2,1,5},
232         {2,1,5}, {2,1,5}, {2,1,5}, {2,1,5},
233         {1,2,5}, {1,2,5}, {1,2,5}, {1,2,5},
234         {1,2,5}, {1,2,5}, {1,2,5}, {1,2,5},
235         {3,1,5}, {3,1,5}, {3,1,5}, {3,1,5},
236         {3,1,5}, {3,1,5}, {3,1,5}, {3,1,5},
237         {1,1,3}, {1,1,3}, {1,1,3}, {1,1,3},
238         {1,1,3}, {1,1,3}, {1,1,3}, {1,1,3},
239         {1,1,3}, {1,1,3}, {1,1,3}, {1,1,3},
240         {1,1,3}, {1,1,3}, {1,1,3}, {1,1,3},
241         {1,1,3}, {1,1,3}, {1,1,3}, {1,1,3},
242         {1,1,3}, {1,1,3}, {1,1,3}, {1,1,3},
243         {1,1,3}, {1,1,3}, {1,1,3}, {1,1,3},
244         {1,1,3}, {1,1,3}, {1,1,3}, {1,1,3},
245         {DCT_EOB,0,4}, {DCT_EOB,0,4}, {DCT_EOB,0,4}, {DCT_EOB,0,4}, /* EOB */
246         {DCT_EOB,0,4}, {DCT_EOB,0,4}, {DCT_EOB,0,4}, {DCT_EOB,0,4},
247         {DCT_EOB,0,4}, {DCT_EOB,0,4}, {DCT_EOB,0,4}, {DCT_EOB,0,4},
248         {DCT_EOB,0,4}, {DCT_EOB,0,4}, {DCT_EOB,0,4}, {DCT_EOB,0,4},
249         {0,3,4}, {0,3,4}, {0,3,4}, {0,3,4},
250         {0,3,4}, {0,3,4}, {0,3,4}, {0,3,4},
251         {0,3,4}, {0,3,4}, {0,3,4}, {0,3,4},
252         {0,3,4}, {0,3,4}, {0,3,4}, {0,3,4},
253         {0,1,2}, {0,1,2}, {0,1,2}, {0,1,2},
254         {0,1,2}, {0,1,2}, {0,1,2}, {0,1,2},
255         {0,1,2}, {0,1,2}, {0,1,2}, {0,1,2},
256         {0,1,2}, {0,1,2}, {0,1,2}, {0,1,2},
257         {0,1,2}, {0,1,2}, {0,1,2}, {0,1,2},
258         {0,1,2}, {0,1,2}, {0,1,2}, {0,1,2},
259         {0,1,2}, {0,1,2}, {0,1,2}, {0,1,2},
260         {0,1,2}, {0,1,2}, {0,1,2}, {0,1,2},
261         {0,1,2}, {0,1,2}, {0,1,2}, {0,1,2},
262         {0,1,2}, {0,1,2}, {0,1,2}, {0,1,2},
263         {0,1,2}, {0,1,2}, {0,1,2}, {0,1,2},
264         {0,1,2}, {0,1,2}, {0,1,2}, {0,1,2},
265         {0,1,2}, {0,1,2}, {0,1,2}, {0,1,2},
266         {0,1,2}, {0,1,2}, {0,1,2}, {0,1,2},
267         {0,1,2}, {0,1,2}, {0,1,2}, {0,1,2},
268         {0,2,3}, {0,2,3}, {0,2,3}, {0,2,3},
269         {0,2,3}, {0,2,3}, {0,2,3}, {0,2,3},
270         {0,2,3}, {0,2,3}, {0,2,3}, {0,2,3},
271         {0,2,3}, {0,2,3}, {0,2,3}, {0,2,3},
272         {0,2,3}, {0,2,3}, {0,2,3}, {0,2,3},
273         {0,2,3}, {0,2,3}, {0,2,3}, {0,2,3},
274         {0,2,3}, {0,2,3}, {0,2,3}, {0,2,3},
275         {0,2,3}, {0,2,3}, {0,2,3}, {0,2,3},
276         {0,4,5}, {0,4,5}, {0,4,5}, {0,4,5},
277         {0,4,5}, {0,4,5}, {0,4,5}, {0,4,5},
278         {0,5,5}, {0,5,5}, {0,5,5}, {0,5,5},
279         {0,5,5}, {0,5,5}, {0,5,5}, {0,5,5},
280         {9,1,7}, {9,1,7}, {1,3,7}, {1,3,7},
281         {10,1,7}, {10,1,7}, {0,8,7}, {0,8,7},
282         {0,9,7}, {0,9,7}, {0,12,8}, {0,13,8},
283         {2,3,8}, {4,2,8}, {0,14,8}, {0,15,8}
284     };
285 #endif
286
287 dct_lookup_t pl_DCT_tab0a[252] =
288 {
289   {65,0,6}, {65,0,6}, {65,0,6}, {65,0,6}, /* Escape */
290   {7,1,7}, {7,1,7}, {8,1,7}, {8,1,7},
291   {6,1,7}, {6,1,7}, {2,2,7}, {2,2,7},
292   {0,7,6}, {0,7,6}, {0,7,6}, {0,7,6},
293   {0,6,6}, {0,6,6}, {0,6,6}, {0,6,6},
294   {4,1,6}, {4,1,6}, {4,1,6}, {4,1,6},
295   {5,1,6}, {5,1,6}, {5,1,6}, {5,1,6},
296   {1,5,8}, {11,1,8}, {0,11,8}, {0,10,8},
297   {13,1,8}, {12,1,8}, {3,2,8}, {1,4,8},
298   {2,1,5}, {2,1,5}, {2,1,5}, {2,1,5},
299   {2,1,5}, {2,1,5}, {2,1,5}, {2,1,5},
300   {1,2,5}, {1,2,5}, {1,2,5}, {1,2,5},
301   {1,2,5}, {1,2,5}, {1,2,5}, {1,2,5},
302   {3,1,5}, {3,1,5}, {3,1,5}, {3,1,5},
303   {3,1,5}, {3,1,5}, {3,1,5}, {3,1,5},
304   {1,1,3}, {1,1,3}, {1,1,3}, {1,1,3},
305   {1,1,3}, {1,1,3}, {1,1,3}, {1,1,3},
306   {1,1,3}, {1,1,3}, {1,1,3}, {1,1,3},
307   {1,1,3}, {1,1,3}, {1,1,3}, {1,1,3},
308   {1,1,3}, {1,1,3}, {1,1,3}, {1,1,3},
309   {1,1,3}, {1,1,3}, {1,1,3}, {1,1,3},
310   {1,1,3}, {1,1,3}, {1,1,3}, {1,1,3},
311   {1,1,3}, {1,1,3}, {1,1,3}, {1,1,3},
312   {64,0,4}, {64,0,4}, {64,0,4}, {64,0,4}, /* EOB */
313   {64,0,4}, {64,0,4}, {64,0,4}, {64,0,4},
314   {64,0,4}, {64,0,4}, {64,0,4}, {64,0,4},
315   {64,0,4}, {64,0,4}, {64,0,4}, {64,0,4},
316   {0,3,4}, {0,3,4}, {0,3,4}, {0,3,4},
317   {0,3,4}, {0,3,4}, {0,3,4}, {0,3,4},
318   {0,3,4}, {0,3,4}, {0,3,4}, {0,3,4},
319   {0,3,4}, {0,3,4}, {0,3,4}, {0,3,4},
320   {0,1,2}, {0,1,2}, {0,1,2}, {0,1,2},
321   {0,1,2}, {0,1,2}, {0,1,2}, {0,1,2},
322   {0,1,2}, {0,1,2}, {0,1,2}, {0,1,2},
323   {0,1,2}, {0,1,2}, {0,1,2}, {0,1,2},
324   {0,1,2}, {0,1,2}, {0,1,2}, {0,1,2},
325   {0,1,2}, {0,1,2}, {0,1,2}, {0,1,2},
326   {0,1,2}, {0,1,2}, {0,1,2}, {0,1,2},
327   {0,1,2}, {0,1,2}, {0,1,2}, {0,1,2},
328   {0,1,2}, {0,1,2}, {0,1,2}, {0,1,2},
329   {0,1,2}, {0,1,2}, {0,1,2}, {0,1,2},
330   {0,1,2}, {0,1,2}, {0,1,2}, {0,1,2},
331   {0,1,2}, {0,1,2}, {0,1,2}, {0,1,2},
332   {0,1,2}, {0,1,2}, {0,1,2}, {0,1,2},
333   {0,1,2}, {0,1,2}, {0,1,2}, {0,1,2},
334   {0,1,2}, {0,1,2}, {0,1,2}, {0,1,2},
335   {0,1,2}, {0,1,2}, {0,1,2}, {0,1,2},
336   {0,2,3}, {0,2,3}, {0,2,3}, {0,2,3},
337   {0,2,3}, {0,2,3}, {0,2,3}, {0,2,3},
338   {0,2,3}, {0,2,3}, {0,2,3}, {0,2,3},
339   {0,2,3}, {0,2,3}, {0,2,3}, {0,2,3},
340   {0,2,3}, {0,2,3}, {0,2,3}, {0,2,3},
341   {0,2,3}, {0,2,3}, {0,2,3}, {0,2,3},
342   {0,2,3}, {0,2,3}, {0,2,3}, {0,2,3},
343   {0,2,3}, {0,2,3}, {0,2,3}, {0,2,3},
344   {0,4,5}, {0,4,5}, {0,4,5}, {0,4,5},
345   {0,4,5}, {0,4,5}, {0,4,5}, {0,4,5},
346   {0,5,5}, {0,5,5}, {0,5,5}, {0,5,5},
347   {0,5,5}, {0,5,5}, {0,5,5}, {0,5,5},
348   {9,1,7}, {9,1,7}, {1,3,7}, {1,3,7},
349   {10,1,7}, {10,1,7}, {0,8,7}, {0,8,7},
350   {0,9,7}, {0,9,7}, {0,12,8}, {0,13,8},
351   {2,3,8}, {4,2,8}, {0,14,8}, {0,15,8}
352 };
353
354
355
356
357     /* Table B-14, DCT coefficients table zero,
358      * codes 0000001000 ... 0000001111
359      */
360 dct_lookup_t pl_DCT_tab1[8] =
361     {
362         {16,1,10}, {5,2,10}, {0,7,10}, {2,3,10},
363         {1,4,10}, {15,1,10}, {14,1,10}, {4,2,10}
364     };
365
366     /* Table B-15, DCT coefficients table one,
367      * codes 000000100x ... 000000111x
368      */
369 dct_lookup_t pl_DCT_tab1a[8] =
370     {
371         {5,2,9}, {5,2,9}, {14,1,9}, {14,1,9},
372         {2,4,10}, {16,1,10}, {15,1,9}, {15,1,9}
373     };
374
375     /* Table B-14/15, DCT coefficients table zero / one,
376      * codes 000000010000 ... 000000011111
377      */
378 dct_lookup_t pl_DCT_tab2[16] =
379     {
380         {0,11,12}, {8,2,12}, {4,3,12}, {0,10,12},
381         {2,4,12}, {7,2,12}, {21,1,12}, {20,1,12},
382         {0,9,12}, {19,1,12}, {18,1,12}, {1,5,12},
383         {3,3,12}, {0,8,12}, {6,2,12}, {17,1,12}
384     };
385
386     /* Table B-14/15, DCT coefficients table zero / one,
387      * codes 0000000010000 ... 0000000011111
388      */
389 dct_lookup_t pl_DCT_tab3[16] =
390     {
391         {10,2,13}, {9,2,13}, {5,3,13}, {3,4,13},
392         {2,5,13}, {1,7,13}, {1,6,13}, {0,15,13},
393         {0,14,13}, {0,13,13}, {0,12,13}, {26,1,13},
394         {25,1,13}, {24,1,13}, {23,1,13}, {22,1,13}
395     };
396
397     /* Table B-14/15, DCT coefficients table zero / one,
398      * codes 00000000010000 ... 00000000011111
399      */
400 dct_lookup_t pl_DCT_tab4[16] =
401     {
402         {0,31,14}, {0,30,14}, {0,29,14}, {0,28,14},
403         {0,27,14}, {0,26,14}, {0,25,14}, {0,24,14},
404         {0,23,14}, {0,22,14}, {0,21,14}, {0,20,14},
405         {0,19,14}, {0,18,14}, {0,17,14}, {0,16,14}
406     };
407
408     /* Table B-14/15, DCT coefficients table zero / one,
409      *   codes 000000000010000 ... 000000000011111
410      */
411 dct_lookup_t pl_DCT_tab5[16] =
412     {
413     {0,40,15}, {0,39,15}, {0,38,15}, {0,37,15},
414     {0,36,15}, {0,35,15}, {0,34,15}, {0,33,15},
415     {0,32,15}, {1,14,15}, {1,13,15}, {1,12,15},
416     {1,11,15}, {1,10,15}, {1,9,15}, {1,8,15}
417     };
418
419     /* Table B-14/15, DCT coefficients table zero / one,
420      * codes 0000000000010000 ... 0000000000011111
421      */
422 dct_lookup_t pl_DCT_tab6[16] =
423     {
424         {1,18,16}, {1,17,16}, {1,16,16}, {1,15,16},
425         {6,3,16}, {16,2,16}, {15,2,16}, {14,2,16},
426         {13,2,16}, {12,2,16}, {11,2,16}, {31,1,16},
427         {30,1,16}, {29,1,16}, {28,1,16}, {27,1,16}
428     };
429 /*
430  * Initialization of lookup tables
431  */
432
433 /*****************************************************************************
434  * vpar_InitCrop : Initialize the crop table for saturation
435  *                 (ISO/IEC 13818-2 section 7.4.3)
436  *****************************************************************************/
437 #if defined(MPEG2_COMPLIANT) && !defined(VDEC_DFT)
438 void vpar_InitCrop( vpar_thread_t * p_vpar )
439 {
440     int i_dummy;
441
442     p_vpar->pi_crop = p_vpar->pi_crop_buf + 4096;
443
444     for( i_dummy = -4096; i_dummy < -2048; i_dummy++ )
445     {
446         p_vpar->pi_crop[i_dummy] = -2048;
447     }
448     for( ; i_dummy < 2047; i_dummy++ )
449     {
450         p_vpar->pi_crop[i_dummy] = i_dummy;
451     }
452     for( ; i_dummy < 4095; i_dummy++ )
453     {
454         p_vpar->pi_crop[i_dummy] = 2047;
455     }
456 }
457 #endif
458
459 /*****************************************************************************
460  * InitMbAddrInc : Initialize the lookup table for mb_addr_inc
461  *****************************************************************************/
462
463 /* Function for filling up the lookup table for mb_addr_inc */
464 void __inline__ FillMbAddrIncTable( vpar_thread_t * p_vpar,
465                                     int i_start, int i_end, int i_step, 
466                                     int * pi_value, int i_length )
467 {
468     int i_dummy, i_dummy2;
469     for( i_dummy = i_start ; i_dummy < i_end ; i_dummy += i_step )
470     {
471         for( i_dummy2 = 0 ; i_dummy2 < i_step ; i_dummy2 ++ )
472         {
473             p_vpar->pl_mb_addr_inc[i_dummy + i_dummy2].i_value = * pi_value;
474             p_vpar->pl_mb_addr_inc[i_dummy + i_dummy2].i_length = i_length;
475         }
476         (*pi_value)--;
477     }
478 }
479     
480 /* Function that initialize the table using the last one */
481 void vpar_InitMbAddrInc( vpar_thread_t * p_vpar )
482 {
483     int i_dummy;
484     int i_value;
485     
486     for( i_dummy = 0 ; i_dummy < 8 ; i_dummy++ )
487     {
488         p_vpar->pl_mb_addr_inc[i_dummy].i_value = MB_ERROR;
489         p_vpar->pl_mb_addr_inc[i_dummy].i_length = 0;
490     }
491
492     p_vpar->pl_mb_addr_inc[8].i_value = MB_ADDRINC_ESCAPE;
493     p_vpar->pl_mb_addr_inc[8].i_length = 11;
494
495     for( i_dummy = 9 ; i_dummy < 15 ; i_dummy ++ ) 
496     {
497         p_vpar->pl_mb_addr_inc[i_dummy].i_value =  MB_ERROR;
498         p_vpar->pl_mb_addr_inc[i_dummy].i_length = 0;
499     }
500
501     p_vpar->pl_mb_addr_inc[15].i_value = MB_ADDRINC_STUFFING;
502     p_vpar->pl_mb_addr_inc[15].i_length = 11;
503
504     for( i_dummy = 16; i_dummy < 24; i_dummy++ ) 
505     {
506         p_vpar->pl_mb_addr_inc[i_dummy].i_value =  MB_ERROR;
507         p_vpar->pl_mb_addr_inc[i_dummy].i_length = 0;
508     }
509     
510     i_value = 33;
511     
512     FillMbAddrIncTable( p_vpar, 24, 36, 1, &i_value, 11 );
513     FillMbAddrIncTable( p_vpar, 36, 48, 2, &i_value, 10 );
514     FillMbAddrIncTable( p_vpar, 48, 96, 8, &i_value, 8 );
515     FillMbAddrIncTable( p_vpar, 96, 128, 16, &i_value, 7 );
516     FillMbAddrIncTable( p_vpar, 128, 256, 64, &i_value, 5 );
517     FillMbAddrIncTable( p_vpar, 256, 512, 128, &i_value, 4 );
518     FillMbAddrIncTable( p_vpar, 512, 1024, 256, &i_value, 3 );
519     FillMbAddrIncTable( p_vpar, 1024, 2048, 1024, &i_value, 1 );
520 }
521
522 /*****************************************************************************
523  * Init*MBType : Initialize lookup table for the Macroblock type
524  *****************************************************************************/
525
526 /* Fonction for filling up the tables */
527 static void __inline__ FillMBType( vpar_thread_t * p_vpar,
528                                    int           i_mb_type,
529                                    int           i_start,
530                                    int           i_end,
531                                    int           i_value,
532                                    int           i_length )
533 {
534     int i_dummy;
535     
536     for( i_dummy = i_start ; i_dummy < i_end ; i_dummy++ )
537     {
538         p_vpar->ppl_mb_type[i_mb_type][i_dummy].i_value = i_value;
539         p_vpar->ppl_mb_type[i_mb_type][i_dummy].i_length = i_length;
540     }
541 }
542
543 /* Fonction that fills the table for P MB_Type */
544 void vpar_InitPMBType( vpar_thread_t * p_vpar )
545 {
546     FillMBType( p_vpar, 0, 32, 64, MB_MOTION_FORWARD|MB_PATTERN, 1 );
547     FillMBType( p_vpar, 0, 16, 32, MB_PATTERN, 2 );
548     FillMBType( p_vpar, 0, 8, 16, MB_MOTION_FORWARD, 3 );
549     FillMBType( p_vpar, 0, 6, 8, MB_INTRA, 5 );
550     FillMBType( p_vpar, 0, 4, 6, MB_QUANT|MB_MOTION_FORWARD|MB_PATTERN, 5 );
551     FillMBType( p_vpar, 0, 2, 4, MB_QUANT|MB_PATTERN, 5 );
552     p_vpar->ppl_mb_type[0][1].i_value = MB_QUANT|MB_INTRA;
553     p_vpar->ppl_mb_type[0][1].i_length = 6;
554     p_vpar->ppl_mb_type[0][0].i_value = MB_ERROR;
555     p_vpar->ppl_mb_type[0][0].i_length = 0;
556 }
557
558 /* Fonction that fills the table for B MB_Type */
559 void vpar_InitBMBType( vpar_thread_t * p_vpar )
560 {
561     FillMBType( p_vpar, 1, 48, 64, MB_MOTION_FORWARD
562                                   |MB_MOTION_BACKWARD|MB_PATTERN, 2 );
563     FillMBType( p_vpar, 1, 32, 48, MB_MOTION_FORWARD|MB_MOTION_BACKWARD, 2 );
564     FillMBType( p_vpar, 1, 24, 32, MB_MOTION_BACKWARD|MB_PATTERN, 3 );
565     FillMBType( p_vpar, 1, 16, 24, MB_MOTION_BACKWARD, 3 );
566     FillMBType( p_vpar, 1, 12, 16, MB_MOTION_FORWARD|MB_PATTERN, 4 );
567     FillMBType( p_vpar, 1, 8, 12, MB_MOTION_FORWARD, 4 );
568     FillMBType( p_vpar, 1, 6, 8, MB_INTRA, 5 );
569     FillMBType( p_vpar, 1, 4, 6, MB_QUANT|MB_MOTION_FORWARD
570                                 |MB_MOTION_BACKWARD|MB_PATTERN, 5 );
571     p_vpar->ppl_mb_type[1][3].i_value = MB_QUANT|MB_MOTION_FORWARD|MB_PATTERN;
572     p_vpar->ppl_mb_type[1][3].i_length = 6;
573     p_vpar->ppl_mb_type[1][2].i_value = MB_QUANT|MB_MOTION_BACKWARD|MB_PATTERN;
574     p_vpar->ppl_mb_type[1][2].i_length = 6;
575     p_vpar->ppl_mb_type[1][1].i_value = MB_QUANT|MB_INTRA;
576     p_vpar->ppl_mb_type[1][1].i_length = 6;
577     p_vpar->ppl_mb_type[1][0].i_value =MB_ERROR;
578     p_vpar->ppl_mb_type[1][0].i_length = 0;
579 }
580
581 /*****************************************************************************
582  * InitCodedPattern : Initialize the lookup table for decoding 
583  *                    coded block pattern
584  *****************************************************************************/
585 void vpar_InitCodedPattern( vpar_thread_t * p_vpar )
586 {
587     p_vpar->pl_coded_pattern = (lookup_t*) pl_coded_pattern_init_table;
588 }
589
590 /*****************************************************************************
591  * InitDCT : Initialize tables giving the length of the dct coefficient
592  *           from the vlc code
593  *****************************************************************************/
594
595 /* First fonction for filling the table */
596 static void __inline__ FillDCTTable( dct_lookup_t * p_tab_dest, dct_lookup_t * p_tab_src,
597                                      int i_step, int i_nb_elem, int i_offset )
598 {
599     int i_dummy, i_dummy2;
600
601     for( i_dummy=0 ; i_dummy < i_nb_elem ; i_dummy++ )
602     {
603         for( i_dummy2=0 ; i_dummy2 < i_step ; i_dummy2++ )
604         {
605             p_tab_dest[(i_dummy+i_offset)*i_step+i_dummy2] = p_tab_src[i_dummy];
606         }
607     }
608 }
609
610
611 /* Fonction that actually fills the table or create the pointers */
612 void vpar_InitDCTTables( vpar_thread_t * p_vpar )
613 {
614     /* Tables are cut in two parts to reduce memory occupation */    
615     p_vpar->pppl_dct_dc_size[0][0] = pl_dct_dc_lum_init_table_1;
616     p_vpar->pppl_dct_dc_size[0][1] = pl_dct_dc_lum_init_table_2;
617     p_vpar->pppl_dct_dc_size[1][0] = pl_dct_dc_chrom_init_table_1;
618     p_vpar->pppl_dct_dc_size[1][1] = pl_dct_dc_chrom_init_table_2;
619
620     memset( ppl_dct_coef[0], MB_ERROR, 16 );
621     memset( ppl_dct_coef[1], MB_ERROR, 16 );
622     
623     /* For table B14 & B15, we have a pointer to tables */
624     /* We fill the table thanks to the fonction defined above */
625     FillDCTTable( ppl_dct_coef[0], pl_DCT_tab0, 256, 60,  4 );
626     FillDCTTable( ppl_dct_coef[0], pl_DCT_tab1,  64,  8,  8 );
627     FillDCTTable( ppl_dct_coef[0], pl_DCT_tab2,  16, 16, 16 );
628     FillDCTTable( ppl_dct_coef[0], pl_DCT_tab3,   8, 16, 16 );
629     FillDCTTable( ppl_dct_coef[0], pl_DCT_tab4,   4, 16, 16 );
630     FillDCTTable( ppl_dct_coef[0], pl_DCT_tab5,   2, 16, 16 );
631     FillDCTTable( ppl_dct_coef[0], pl_DCT_tab6,   1, 16, 16 );
632
633     FillDCTTable( ppl_dct_coef[1], pl_DCT_tab0a, 256, 60, 4 );
634     FillDCTTable( ppl_dct_coef[1], pl_DCT_tab1a,  64,  8,  8 );
635     FillDCTTable( ppl_dct_coef[1], pl_DCT_tab2,   16, 16, 16 );
636     FillDCTTable( ppl_dct_coef[1], pl_DCT_tab3,    8, 16, 16 );
637     FillDCTTable( ppl_dct_coef[1], pl_DCT_tab4,    4, 16, 16 );
638     FillDCTTable( ppl_dct_coef[1], pl_DCT_tab5,    2, 16, 16 );
639     FillDCTTable( ppl_dct_coef[1], pl_DCT_tab6,    1, 16, 16 );
640 }
641
642 /*
643  * Macroblock parsing functions
644  */
645
646 /*****************************************************************************
647  * vpar_ParseMacroblock : Parse the next macroblock
648  *****************************************************************************/
649 void vpar_ParseMacroblock( vpar_thread_t * p_vpar, int * pi_mb_address,
650                            int i_mb_previous, int i_mb_base )
651 {
652     static f_addb_t ppf_addb_intra[2] = {vdec_AddBlock, vdec_CopyBlock};
653     static f_decode_block_t pppf_decode_block[2][2] =
654                 { {vpar_DecodeMPEG1Non, vpar_DecodeMPEG1Intra},
655                   {vpar_DecodeMPEG2Non, vpar_DecodeMPEG2Intra} };
656     static int      pi_x[12] = {0,8,0,8,0,0,0,0,8,8,8,8};
657     static int      pi_y[2][12] = { {0,0,8,8,0,0,8,8,0,0,8,8},
658                                     {0,0,1,1,0,0,1,1,0,0,1,1} };
659     static int      pi_chroma_hor[4] = { 0, 1, 1, 0 };
660     static int      pi_chroma_ver[4] = { 0, 1, 0, 0 };
661
662     int             i_mb, i_b, i_mask;
663     macroblock_t *  p_mb;
664     f_addb_t        pf_addb;
665     elem_t *        p_data1;
666     elem_t *        p_data2;
667
668     /************* DEBUG *************/
669     static i_count = 0;
670     int i_inc;
671     boolean_t b_stop = 0;
672
673     i_inc = MacroblockAddressIncrement( p_vpar );
674    
675     *pi_mb_address += i_inc;
676
677     //*pi_mb_address += MacroblockAddressIncrement( p_vpar );
678     b_stop = i_inc > 1;
679     fprintf( stderr, "inc : %d (%d)\n", *pi_mb_address, i_inc );
680 if( 0  )//i_count > 8 )
681 {
682     exit(0);
683 }
684 i_count++;
685
686 if( 0 )        
687     //i_count == 249)
688     // i_count != *pi_mb_address)
689     //b_stop )
690 {
691     fprintf( stderr, "i_count = %d (%d)\n", i_count, i_inc );
692      fprintf( stderr, "%x", GetBits( &p_vpar->bit_stream, 16 ) );
693      fprintf( stderr, "%x ", GetBits( &p_vpar->bit_stream, 16 ) );
694      fprintf( stderr, "%x", GetBits( &p_vpar->bit_stream, 16 ) );
695      fprintf( stderr, "%x\n", GetBits( &p_vpar->bit_stream, 16 ) );
696      fprintf( stderr, "%x", GetBits( &p_vpar->bit_stream, 16 ) );
697      fprintf( stderr, "%x ", GetBits( &p_vpar->bit_stream, 16 ) );
698      fprintf( stderr, "%x", GetBits( &p_vpar->bit_stream, 16 ) );
699      fprintf( stderr, "%x\n", GetBits( &p_vpar->bit_stream, 16 ) );
700      exit(0);
701 }
702
703     for( i_mb = i_mb_previous; i_mb < *pi_mb_address; i_mb++ )
704     {
705         /* Skipped macroblock (ISO/IEC 13818-2 7.6.6). */
706         static int          pi_dc_dct_reinit[4] = {128,256,512,1024};
707         static f_motion_t   pf_motion_skipped[4] = {NULL, vdec_MotionField,
708                                 vdec_MotionField, vdec_MotionFrame};
709
710         /* Reset DC predictors (7.2.1). */
711         p_vpar->slice.pi_dc_dct_pred[0] = p_vpar->slice.pi_dc_dct_pred[1]
712             = p_vpar->slice.pi_dc_dct_pred[2]
713             = pi_dc_dct_reinit[p_vpar->picture.i_intra_dc_precision];
714
715         if( p_vpar->picture.i_coding_type == P_CODING_TYPE )
716         {
717             /* Reset motion vector predictors (ISO/IEC 13818-2 7.6.3.4). */
718             bzero( p_vpar->slice.pppi_pmv, 8*sizeof(int) );
719         }
720
721         if( (p_mb = p_vpar->picture.pp_mb[i_mb_base + i_mb] =
722              vpar_NewMacroblock( &p_vpar->vfifo )) == NULL )
723         {
724             p_vpar->picture.b_error = 1;
725             intf_ErrMsg("vpar error: macroblock list is empty !\n");
726             return;
727         }
728
729         InitMacroblock( p_vpar, p_mb );
730        
731         /* No IDCT nor AddBlock. */
732         for( i_b = 0; i_b < 12; i_b++ )
733         {
734             p_mb->pf_idct[i_b] = vdec_DummyIDCT;
735             p_mb->pf_addb[i_b] = vdec_DummyBlock;
736         }
737
738         /* Motion type is picture structure. */
739         p_mb->pf_motion = pf_motion_skipped[p_vpar->picture.i_structure];
740
741         /* Set the field we use for motion compensation */
742         p_mb->ppi_field_select[0][0] = p_mb->ppi_field_select[0][1]
743                                      = ( p_vpar->picture.i_current_structure == BOTTOM_FIELD );
744     }
745 //fprintf(stderr, "MB1\n");
746     /* Get a macroblock structure. */
747     if( (p_mb = p_vpar->picture.pp_mb[i_mb_base + *pi_mb_address] =
748          vpar_NewMacroblock( &p_vpar->vfifo )) == NULL )
749     {
750         p_vpar->picture.b_error = 1;
751         intf_ErrMsg("vpar error: macroblock list is empty !\n");
752         return;
753     }
754
755     InitMacroblock( p_vpar, p_mb );
756
757     /* Parse off macroblock_modes structure. */
758     MacroblockModes( p_vpar, p_mb );
759
760     if( p_vpar->mb.i_mb_type & MB_QUANT )
761     {
762         LoadQuantizerScale( p_vpar );
763     }
764
765     if( p_vpar->mb.i_mb_type & MB_MOTION_FORWARD )
766     {
767 fprintf( stderr, "motion !\n" );
768         (*p_vpar->sequence.pf_decode_mv)( p_vpar, p_mb, 0 );
769     }
770
771     if( p_vpar->mb.i_mb_type & MB_MOTION_BACKWARD )
772     {
773 fprintf( stderr, "motion2 !\n" );    
774         (*p_vpar->sequence.pf_decode_mv)( p_vpar, p_mb, 1 );
775     }
776
777     if( p_vpar->picture.b_concealment_mv && (p_vpar->mb.i_mb_type & MB_INTRA) )
778     {
779         RemoveBits( &p_vpar->bit_stream, 1 );
780     }
781
782     if( p_vpar->mb.i_mb_type & MB_PATTERN )
783     {
784         (*p_vpar->sequence.pf_decode_pattern)( p_vpar );
785     }
786     else
787     {
788         int     pi_coded_block_pattern[2] = {0,
789                     (1 << 4+p_vpar->sequence.i_chroma_nb_blocks) - 1};
790         p_vpar->mb.i_coded_block_pattern = pi_coded_block_pattern
791                                     [p_vpar->mb.i_mb_type & MB_INTRA];
792     }
793
794     pf_addb = ppf_addb_intra[p_vpar->mb.i_mb_type & MB_INTRA];
795
796     /*
797      * Effectively decode blocks.
798      */
799 //fprintf(stderr, "MB2\n");
800     i_mask = 1 << (3 + p_vpar->sequence.i_chroma_nb_blocks);
801
802     /* luminance */
803     p_data1 = (elem_t*) p_mb->p_picture->p_y;
804               + p_mb->i_l_x + p_mb->i_l_y*(p_vpar->sequence.i_width);
805
806     for( i_b = 0 ; i_b < 4 ; i_b++, i_mask >>= 1 )
807     {
808         if( p_vpar->mb.i_coded_block_pattern & i_mask )
809         {
810             memset( p_mb->ppi_blocks[i_b], 0, 64*sizeof(elem_t) );
811             (*pppf_decode_block[p_vpar->sequence.b_mpeg2]
812                                [p_vpar->mb.i_mb_type & MB_INTRA])
813                 ( p_vpar, p_mb, i_b );
814         
815             /* decode_block has already set pf_idct and pi_sparse_pos. */
816             p_mb->pf_addb[i_b] = pf_addb;
817      
818             /* Calculate block coordinates. */
819             p_mb->p_data[i_b] = p_data1
820                                 + pi_y[p_vpar->mb.b_dct_type][i_b]
821                                 * p_vpar->sequence.i_width;
822         }
823         else
824         {
825             /* Block not coded, so no IDCT, nor AddBlock */
826             p_mb->pf_addb[i_b] = vdec_DummyBlock;
827             p_mb->pf_idct[i_b] = vdec_DummyIDCT;
828         }
829  if( 0 )    
830     //i_count == 482 && i_b == 2 )
831     // i_count != *pi_mb_address)
832     //b_stop )
833 {
834     fprintf( stderr, "i_count = %d (%d)\n", i_count, i_inc );
835      fprintf( stderr, "%4x", GetBits( &p_vpar->bit_stream, 16 ) );
836      fprintf( stderr, "%4x ", GetBits( &p_vpar->bit_stream, 16 ) );
837      fprintf( stderr, "%4x", GetBits( &p_vpar->bit_stream, 16 ) );
838      fprintf( stderr, "%4x\n", GetBits( &p_vpar->bit_stream, 16 ) );
839      fprintf( stderr, "%4x", GetBits( &p_vpar->bit_stream, 16 ) );
840      fprintf( stderr, "%4x ", GetBits( &p_vpar->bit_stream, 16 ) );
841      fprintf( stderr, "%4x", GetBits( &p_vpar->bit_stream, 16 ) );
842      fprintf( stderr, "%4x\n", GetBits( &p_vpar->bit_stream, 16 ) );
843      fprintf( stderr, "%4x", GetBits( &p_vpar->bit_stream, 16 ) );
844      fprintf( stderr, "%4x ", GetBits( &p_vpar->bit_stream, 16 ) );
845      fprintf( stderr, "%4x", GetBits( &p_vpar->bit_stream, 16 ) );
846      fprintf( stderr, "%4x\n", GetBits( &p_vpar->bit_stream, 16 ) );
847      fprintf( stderr, "%4x", GetBits( &p_vpar->bit_stream, 16 ) );
848      fprintf( stderr, "%4x ", GetBits( &p_vpar->bit_stream, 16 ) );
849      fprintf( stderr, "%4x", GetBits( &p_vpar->bit_stream, 16 ) );
850      fprintf( stderr, "%4x\n", GetBits( &p_vpar->bit_stream, 16 ) );
851      fprintf( stderr, "%4x", GetBits( &p_vpar->bit_stream, 16 ) );
852      fprintf( stderr, "%4x ", GetBits( &p_vpar->bit_stream, 16 ) );
853      fprintf( stderr, "%4x", GetBits( &p_vpar->bit_stream, 16 ) );
854      fprintf( stderr, "%4x\n", GetBits( &p_vpar->bit_stream, 16 ) );
855      exit(0);
856 }
857     }
858
859     /* chrominance */
860     p_data1 = (elem_t*) p_mb->p_picture->p_u
861               + (p_mb->i_c_x >> pi_chroma_hor[p_vpar->sequence.i_chroma_format])
862               + (p_mb->i_c_y >> pi_chroma_ver[p_vpar->sequence.i_chroma_format])
863                 * (p_vpar->sequence.i_chroma_width);
864     p_data2 = (elem_t*) p_mb->p_picture->p_v
865               + (p_mb->i_c_x >> pi_chroma_hor[p_vpar->sequence.i_chroma_format])
866               + (p_mb->i_c_y >> pi_chroma_ver[p_vpar->sequence.i_chroma_format])
867                 * (p_vpar->sequence.i_chroma_width);
868     
869     for( i_b = 4; i_b < 4 + p_vpar->sequence.i_chroma_nb_blocks;
870          i_b++, i_mask >>= 1 )
871     {
872         elem_t *    pp_data[2] = {p_data1, p_data2};
873
874         if( p_vpar->mb.i_coded_block_pattern & i_mask )
875         {
876             memset( p_mb->ppi_blocks[i_b], 0, 64*sizeof(elem_t) );
877             (*pppf_decode_block[p_vpar->sequence.b_mpeg2]
878                                [p_vpar->mb.i_mb_type & MB_INTRA])
879                 ( p_vpar, p_mb, i_b );
880
881             /* decode_block has already set pf_idct and pi_sparse_pos. */
882             p_mb->pf_addb[i_b] = pf_addb;
883
884             /* Calculate block coordinates. */
885             p_mb->p_data[i_b] = pp_data[i_b & 1]
886                                  + pi_y[p_vpar->mb.b_dct_type][i_b]
887                                    * p_vpar->sequence.i_chroma_width;
888         }
889         else
890         {
891             /* Block not coded, so no IDCT, nor AddBlock */
892             p_mb->pf_addb[i_b] = vdec_DummyBlock;
893             p_mb->pf_idct[i_b] = vdec_DummyIDCT;
894         }
895     }
896
897     if( !( p_vpar->mb.i_mb_type & MB_INTRA ) )
898     {
899         static int          pi_dc_dct_reinit[4] = {128,256,512,1024};
900
901         /* Reset DC predictors (7.2.1). */
902         p_vpar->slice.pi_dc_dct_pred[0] = p_vpar->slice.pi_dc_dct_pred[1]
903             = p_vpar->slice.pi_dc_dct_pred[2]
904             = pi_dc_dct_reinit[p_vpar->picture.i_intra_dc_precision];
905     }
906     else if( !p_vpar->picture.b_concealment_mv )
907     {
908         /* Reset MV predictors. */
909         bzero( p_vpar->slice.pppi_pmv, 8*sizeof(int) );
910     }
911  if( 0 )        
912     //i_count == 249)
913     // i_count != *pi_mb_address)
914     //b_stop )
915 {
916     fprintf( stderr, "i_count = %d (%d)\n", i_count, i_inc );
917      fprintf( stderr, "%x", GetBits( &p_vpar->bit_stream, 16 ) );
918      fprintf( stderr, "%x ", GetBits( &p_vpar->bit_stream, 16 ) );
919      fprintf( stderr, "%x", GetBits( &p_vpar->bit_stream, 16 ) );
920      fprintf( stderr, "%x\n", GetBits( &p_vpar->bit_stream, 16 ) );
921      fprintf( stderr, "%x", GetBits( &p_vpar->bit_stream, 16 ) );
922      fprintf( stderr, "%x ", GetBits( &p_vpar->bit_stream, 16 ) );
923      fprintf( stderr, "%x", GetBits( &p_vpar->bit_stream, 16 ) );
924      fprintf( stderr, "%x\n", GetBits( &p_vpar->bit_stream, 16 ) );
925      exit(0);
926 }
927 }
928
929
930 /*****************************************************************************
931  * InitMacroblock : Initialize macroblock values
932  *****************************************************************************/
933 static __inline__ void InitMacroblock( vpar_thread_t * p_vpar,
934                                        macroblock_t * p_mb )
935 {
936     static f_chroma_motion_t pf_chroma_motion[4] =
937     { NULL, vdec_Motion420, vdec_Motion422, vdec_Motion444 };
938     
939     p_mb->p_picture = p_vpar->picture.p_picture;
940     p_mb->i_structure = p_vpar->picture.i_structure;
941     p_mb->i_current_structure = p_vpar->picture.i_current_structure;
942     p_mb->i_l_x = p_vpar->mb.i_l_x;
943     p_mb->i_l_y = p_vpar->mb.i_l_y;
944     p_mb->i_c_x = p_vpar->mb.i_c_x;
945     p_mb->i_c_y = p_vpar->mb.i_c_y;
946     p_mb->i_chroma_nb_blocks = p_vpar->sequence.i_chroma_nb_blocks;
947     p_mb->pf_chroma_motion = pf_chroma_motion[p_vpar->sequence.i_chroma_format];
948     p_mb->b_P_coding_type = ( p_vpar->picture.i_coding_type == P_CODING_TYPE );
949
950     p_mb->p_forward = p_vpar->sequence.p_forward;
951     p_mb->p_backward = p_vpar->sequence.p_backward;
952     
953     p_mb->i_addb_l_stride = p_mb->i_l_stride = p_vpar->picture.i_l_stride;
954     p_mb->i_addb_c_stride = p_mb->i_c_stride = p_vpar->picture.i_c_stride;
955
956     /* Update macroblock real position. */
957     p_vpar->mb.i_l_x += 16;
958     p_vpar->mb.i_l_y += (p_vpar->mb.i_l_x / p_vpar->sequence.i_width)
959                         * (2 - p_vpar->picture.b_frame_structure) * 16;
960     p_vpar->mb.i_l_x %= p_vpar->sequence.i_width;
961
962     p_vpar->mb.i_c_x += p_vpar->sequence.i_chroma_mb_width;
963     p_vpar->mb.i_c_y += (p_vpar->mb.i_c_x / p_vpar->sequence.i_chroma_width)
964                         * (2 - p_vpar->picture.b_frame_structure)
965                         * p_vpar->sequence.i_chroma_mb_height;
966     p_vpar->mb.i_c_x %= p_vpar->sequence.i_chroma_width;
967 }
968
969 /*****************************************************************************
970  * MacroblockAddressIncrement : Get the macroblock_address_increment field
971  *****************************************************************************/
972 static __inline__ int MacroblockAddressIncrement( vpar_thread_t * p_vpar )
973 {
974     int i_addr_inc = 0;
975     /* Index in the lookup table mb_addr_inc */
976     int    i_index = ShowBits( &p_vpar->bit_stream, 11 );
977
978     /* Test the presence of the escape character */
979     while( i_index == 8 )
980     {
981         RemoveBits( &p_vpar->bit_stream, 11 );
982         i_addr_inc += 33;
983         i_index = ShowBits( &p_vpar->bit_stream, 11 );
984     }
985     
986     /* Affect the value from the lookup table */
987     i_addr_inc += p_vpar->pl_mb_addr_inc[i_index].i_value;
988     
989     /* Dump the good number of bits */
990     RemoveBits( &p_vpar->bit_stream, p_vpar->pl_mb_addr_inc[i_index].i_length );
991
992     return i_addr_inc;
993 }
994
995 /*****************************************************************************
996  * MacroblockModes : Get the macroblock_modes structure
997  *****************************************************************************/
998 static __inline__ void MacroblockModes( vpar_thread_t * p_vpar,
999                                         macroblock_t * p_mb )
1000 {
1001     static f_motion_t   pf_motion[2][4] =
1002         { {NULL, vdec_FieldFieldRecon, vdec_Field16x8Recon, vdec_FieldDMVRecon},
1003           {NULL, vdec_FrameFieldRecon, vdec_FrameFrameRecon, vdec_FrameDMVRecon} };
1004     static int          ppi_mv_count[2][4] = { {0, 1, 2, 1}, {0, 2, 1, 1} };
1005     static int          ppi_mv_format[2][4] = { {0, 1, 1, 1}, {0, 1, 2, 1} };
1006
1007     /* Get macroblock_type. */
1008     p_vpar->mb.i_mb_type = (p_vpar->picture.pf_macroblock_type)( p_vpar );
1009     p_mb->i_mb_type = p_vpar->mb.i_mb_type;
1010
1011 //fprintf( stderr, "MB type : %d\n", p_mb->i_mb_type );
1012     
1013     /* SCALABILITY : warning, we don't know if spatial_temporal_weight_code
1014      * has to be dropped, take care if you use scalable streams. */
1015     /* RemoveBits( &p_vpar->bit_stream, 2 ); */
1016     
1017     if( !(p_vpar->mb.i_mb_type & (MB_MOTION_FORWARD | MB_MOTION_BACKWARD)) )
1018     {
1019         /* If mb_type has neither MOTION_FORWARD nor MOTION_BACKWARD, this
1020          * is useless, but also harmless. */
1021         p_vpar->mb.i_motion_type = MOTION_FRAME;
1022     }
1023     else
1024     {
1025         if( p_vpar->picture.i_structure == FRAME_STRUCTURE
1026             && p_vpar->picture.b_frame_pred_frame_dct )
1027         {
1028             p_vpar->mb.i_motion_type = MOTION_FRAME;
1029         }
1030         else
1031         {
1032             p_vpar->mb.i_motion_type = GetBits( &p_vpar->bit_stream, 2 );
1033         }
1034     }
1035    
1036     if( p_mb->b_P_coding_type && !(p_vpar->mb.i_mb_type & (MB_MOTION_FORWARD|MB_INTRA)) )
1037     {
1038         /* Special No-MC macroblock in P pictures (7.6.3.5). */
1039         memset( p_vpar->slice.pppi_pmv, 0, 8*sizeof(int) );
1040         memset( p_mb->pppi_motion_vectors, 0, 8*sizeof(int) );
1041         
1042         p_vpar->mb.i_motion_type = MOTION_FRAME;
1043         p_mb->ppi_field_select[0][0] = ( p_vpar->picture.i_current_structure == BOTTOM_FIELD );
1044     }
1045
1046      if( p_vpar->mb.i_mb_type & MB_INTRA )
1047     {
1048         /* For the intra macroblocks, we use an empty motion
1049          * compensation function */
1050         p_mb->pf_motion = vdec_DummyRecon;
1051     }
1052     else
1053     {
1054         p_mb->pf_motion = pf_motion[p_vpar->picture.b_frame_structure]
1055                                    [p_vpar->mb.i_motion_type];
1056     }
1057     p_vpar->mb.i_mv_count = ppi_mv_count[p_vpar->picture.b_frame_structure]
1058                                         [p_vpar->mb.i_motion_type];
1059     p_vpar->mb.i_mv_format = ppi_mv_format[p_vpar->picture.b_frame_structure]
1060                                           [p_vpar->mb.i_motion_type];
1061
1062     p_vpar->mb.b_dct_type = 0;
1063     if( (p_vpar->picture.i_structure == FRAME_STRUCTURE) &&
1064         (!p_vpar->picture.b_frame_pred_frame_dct) &&
1065         (p_vpar->mb.i_mb_type & (MB_PATTERN|MB_INTRA)) )
1066     {
1067         if( (p_vpar->mb.b_dct_type = GetBits( &p_vpar->bit_stream, 1 )) )
1068         {
1069             /* The DCT is coded on fields. Jump one line between each
1070              * sample. */
1071             p_mb->i_addb_l_stride <<= 1;
1072                 p_mb->i_addb_l_stride += 8;
1073             /* With CHROMA_420, the DCT is necessarily frame-coded. */
1074             if( p_vpar->sequence.i_chroma_format != CHROMA_420 )
1075             {
1076                     p_mb->i_addb_c_stride <<= 1;
1077                     p_mb->i_addb_c_stride += 8;
1078             }
1079         }
1080     }
1081     p_vpar->mb.b_dmv = p_vpar->mb.i_motion_type == MOTION_DMV;
1082 }
1083
1084 /*****************************************************************************
1085  * vpar_IMBType : macroblock_type in I pictures
1086  *****************************************************************************/
1087 int vpar_IMBType( vpar_thread_t * p_vpar )
1088 {
1089     /* Take two bits for testing */
1090     int                 i_type = ShowBits( &p_vpar->bit_stream, 2 );
1091     
1092     /* Lookup table for macroblock_type */
1093     static lookup_t     pl_mb_Itype[4] = { {MB_ERROR, 0},
1094                                            {MB_QUANT|MB_INTRA, 2},
1095                                            {MB_INTRA, 1},
1096                                            {MB_INTRA, 1} };
1097     /* Dump the good number of bits */
1098     RemoveBits( &p_vpar->bit_stream, pl_mb_Itype[i_type].i_length );
1099     return pl_mb_Itype[i_type].i_value;
1100 }
1101
1102 /*****************************************************************************
1103  * vpar_PMBType : macroblock_type in P pictures
1104  *****************************************************************************/
1105 int vpar_PMBType( vpar_thread_t * p_vpar )
1106 {
1107     /* Testing on 6 bits */
1108     int                i_type = ShowBits( &p_vpar->bit_stream, 6 );
1109     
1110     /* Dump the good number of bits */
1111     RemoveBits( &p_vpar->bit_stream, p_vpar->ppl_mb_type[0][i_type].i_length );
1112
1113     /* return the value from the lookup table for P type */
1114     return p_vpar->ppl_mb_type[0][i_type].i_value;
1115 }
1116
1117 /*****************************************************************************
1118  * vpar_BMBType : macroblock_type in B pictures
1119  *****************************************************************************/
1120 int vpar_BMBType( vpar_thread_t * p_vpar )
1121 {
1122      /* Testing on 6 bits */
1123     int                i_type = ShowBits( &p_vpar->bit_stream, 6 );
1124     
1125     /* Dump the good number of bits */
1126     RemoveBits( &p_vpar->bit_stream, p_vpar->ppl_mb_type[1][i_type].i_length );
1127     
1128     /* return the value from the lookup table for B type */
1129     return p_vpar->ppl_mb_type[1][i_type].i_value;
1130 }
1131
1132 /*****************************************************************************
1133  * vpar_DMBType : macroblock_type in D pictures
1134  *****************************************************************************/
1135 int vpar_DMBType( vpar_thread_t * p_vpar )
1136 {
1137     /* Taking 1 bit */
1138     int               i_type = GetBits( &p_vpar->bit_stream, 1 );
1139     
1140     /* Lookup table */
1141     static int        pi_mb_Dtype[2] = { MB_ERROR, 1 };
1142     
1143     return pi_mb_Dtype[i_type];
1144 }
1145
1146 /*****************************************************************************
1147  * vpar_CodedPattern420 : coded_block_pattern with 420 chroma
1148  *****************************************************************************/
1149 int vpar_CodedPattern420( vpar_thread_t * p_vpar )
1150 {
1151     /* Take the max 9 bits length vlc code for testing */
1152     int      i_vlc = ShowBits( &p_vpar->bit_stream, 9 );
1153     
1154     /* Trash the good number of bits read in  the lookup table */
1155     RemoveBits( &p_vpar->bit_stream, p_vpar->pl_coded_pattern[i_vlc].i_length );
1156     
1157     /* return the value from the vlc table */
1158     return p_vpar->pl_coded_pattern[i_vlc].i_value;
1159 }
1160
1161 /*****************************************************************************
1162  * vpar_CodedPattern422 : coded_block_pattern with 422 chroma
1163  *****************************************************************************/
1164 int vpar_CodedPattern422( vpar_thread_t * p_vpar )
1165 {
1166     int      i_vlc = ShowBits( &p_vpar->bit_stream, 9 );
1167     
1168     /* Supplementary 2 bits long code for 422 format */
1169     int      i_coded_block_pattern_1;
1170     
1171     RemoveBits( &p_vpar->bit_stream, p_vpar->pl_coded_pattern[i_vlc].i_length );
1172     i_coded_block_pattern_1 = GetBits( &p_vpar->bit_stream, 2 );
1173     
1174     /* the code is just to be added to the value found in the table */
1175     return p_vpar->pl_coded_pattern[i_vlc].i_value |
1176            (i_coded_block_pattern_1 << 6);
1177 }
1178
1179 /*****************************************************************************
1180  * vpar_CodedPattern444 : coded_block_pattern with 444 chroma
1181  *****************************************************************************/
1182 int vpar_CodedPattern444( vpar_thread_t * p_vpar )
1183 {
1184     int      i_vlc = ShowBits( &p_vpar->bit_stream, 9 );
1185     int      i_coded_block_pattern_2;
1186     
1187     RemoveBits( &p_vpar->bit_stream, p_vpar->pl_coded_pattern[i_vlc].i_length );
1188     i_coded_block_pattern_2 = GetBits( &p_vpar->bit_stream, 6 );
1189     
1190     return p_vpar->pl_coded_pattern[i_vlc].i_value |
1191            ( i_coded_block_pattern_2 << 6 );
1192 }
1193
1194 /*****************************************************************************
1195  * vpar_DecodeMPEG1Non : decode MPEG-1 non-intra blocks
1196  *****************************************************************************/
1197 static void vpar_DecodeMPEG1Non( vpar_thread_t * p_vpar, macroblock_t * p_mb, int i_b )
1198 {
1199
1200     if( p_vpar->picture.i_coding_type == D_CODING_TYPE )
1201     {
1202         /* Remove end_of_macroblock (always 1, prevents startcode emulation)
1203          * ISO/IEC 11172-2 section 2.4.2.7 and 2.4.3.6 */
1204         RemoveBits( &p_vpar->bit_stream, 1 );
1205     }
1206 }
1207
1208 /*****************************************************************************
1209  * vpar_DecodeMPEG1Intra : decode MPEG-1 intra blocks
1210  *****************************************************************************/
1211 static void vpar_DecodeMPEG1Intra( vpar_thread_t * p_vpar, macroblock_t * p_mb, int i_b )
1212 {
1213
1214     if( p_vpar->picture.i_coding_type == D_CODING_TYPE )
1215     {
1216         /* Remove end_of_macroblock (always 1, prevents startcode emulation)
1217          * ISO/IEC 11172-2 section 2.4.2.7 and 2.4.3.6 */
1218         RemoveBits( &p_vpar->bit_stream, 1 );
1219     }
1220 }
1221
1222 /*****************************************************************************
1223  * vpar_DecodeMPEG2Non : decode MPEG-2 non-intra blocks
1224  *****************************************************************************/
1225 static void vpar_DecodeMPEG2Non( vpar_thread_t * p_vpar, macroblock_t * p_mb, int i_b )
1226 {
1227 #if 0
1228     int         i_dummy;
1229     int         i_code;
1230     int         i_nc;
1231     int         i_coef;
1232     int         i_run;
1233     int         i_level;
1234     boolean_t   b_intra;
1235     boolean_t   b_sign;
1236     
1237     /* There is no special decodding for DC coefficient in non intra blocs
1238      * except that we won't use exactly the same table B.14 note 3 & 4 */
1239     /* Decoding of the coefficients */
1240
1241     i_nc = 0;
1242     i_coef = 0;
1243
1244     b_intra = p_vpar->picture.b_intra_vlc_format;
1245     for( i_dummy = 0; i_dummy < 64; i_dummy++ )
1246     {
1247         i_code = ShowBits( &p_vpar->bit_stream, 16 );
1248         i_run = (*p_vpar->pppl_dct_coef[b_intra][i_code]).i_run;
1249         RemoveBits( &p_vpar->bit_stream,
1250                   (*p_vpar->pppl_dct_coef[b_intra][i_code]).i_length );
1251         switch( i_run )
1252         {
1253             case DCT_ESCAPE:
1254                 i_run = GetBits( &p_vpar->bit_stream, 6 );
1255                 i_level = GetBits( &p_vpar->bit_stream, 12 );
1256                 p_mb->ppi_blocks[i_b][i_dummy] = ( b_sign = ( i_level > 2047 ) ) 
1257                                                           ? ( -4096 + i_level )
1258                                                           : i_level;
1259                 i_coef = i_dummy;
1260                 i_dummy += i_run;
1261                 i_nc ++;
1262                 break;
1263             case DCT_EOB:
1264                 i_dummy = 64;
1265                 break;
1266             default:
1267                 i_level = (*p_vpar->pppl_dct_coef[b_intra][i_code]).i_level;
1268                 b_sign = GetBits( &p_vpar->bit_stream, 1 );
1269                 p_mb->ppi_blocks[i_b][i_dummy] = b_sign ? -i_level : i_level;
1270                 i_coef = i_dummy;
1271                 i_dummy += i_run;
1272                 i_nc ++;
1273         }
1274     }
1275     if( i_nc == 1 )
1276     {
1277         p_mb->pf_idct[i_b] = vdec_SparseIDCT;
1278         p_mb->pi_sparse_pos[i_b] = i_coef;
1279     }
1280     else
1281     {
1282         p_mb->pf_idct[i_b] = vdec_IDCT;
1283     }
1284 #endif
1285 }
1286
1287 /*****************************************************************************
1288  * vpar_DecodeMPEG2Intra : decode MPEG-2 intra blocks
1289  *****************************************************************************/
1290 static void vpar_DecodeMPEG2Intra( vpar_thread_t * p_vpar, macroblock_t * p_mb, int i_b )
1291 {
1292     int         i_dummy;
1293     int         i_nc;
1294     int         i_cc;
1295     int         i_coef;
1296     int         i_type;
1297     int         i_code;
1298     int         i_length;
1299     int         i_code5;
1300     int         i_select;
1301     int         i_offset;
1302     int         i_pos;
1303     int         i_dct_dc_size;
1304     int         i_dct_dc_diff;
1305     int         i_run;
1306     int         i_level;
1307     boolean_t   b_vlc_intra;
1308     boolean_t   b_sign;
1309     
1310     /* Lookup Table for the chromatic component */
1311     static int pi_cc_index[12] = { 0, 0, 0, 0, 1, 2, 1, 2, 1, 2 };
1312     
1313     i_cc = pi_cc_index[i_b];
1314 #if 0    
1315     /* Determine whether it is luminance or not (chrominance) */
1316     i_type = ( i_cc + 1 ) >> 1;
1317     /* Decoding of the DC intra coefficient */
1318     /* The nb of bits to parse depends on i_type */
1319     i_code = ShowBits( &p_vpar->bit_stream, 9 + i_type );
1320     
1321     /* To reduce memory occupation, there are two lookup tables
1322      * See InitDCT above */
1323     i_code5 = i_code >> (4+i_type);
1324     
1325     /* Shall we lookup in the first or in the second table ? */
1326     i_select = ( i_code5 == 31 );
1327     /* Offset value for looking in the second table */
1328     i_offset = 0x1f0 + ( i_type * 0x1f0 );
1329     i_pos = ( i_code5 * ( ! i_select ) ) + ( ( i_code - i_offset ) * i_select );
1330     i_dct_dc_size = p_vpar->pppl_dct_dc_size[i_type][i_select][i_pos].i_value;
1331 #endif
1332     if( i_b < 4 )
1333     {
1334         int code, size, length, dct_diff;
1335
1336         /* decode length */
1337         code = ShowBits(&p_vpar->bit_stream, 5);
1338         if (code<31)
1339         {
1340             size = pl_dct_dc_lum_init_table_1[code].i_value;
1341             length = pl_dct_dc_lum_init_table_1[code].i_length;
1342             RemoveBits(&p_vpar->bit_stream, length);
1343         }
1344         else
1345         {
1346             code = ShowBits(&p_vpar->bit_stream, 9) - 0x1f0;
1347             size = pl_dct_dc_lum_init_table_2[code].i_value;
1348             length = pl_dct_dc_lum_init_table_2[code].i_length;
1349            RemoveBits( &p_vpar->bit_stream, length);
1350        }
1351
1352         if (size==0)
1353             dct_diff = 0;
1354         else
1355         {
1356             dct_diff = GetBits( &p_vpar->bit_stream, size);
1357             if ((dct_diff & (1<<(size-1)))==0)
1358                 dct_diff-= (1<<size) - 1;
1359         }
1360
1361         i_dct_dc_diff = dct_diff;
1362     }
1363     else
1364     {
1365         int code, size, length, dct_diff;
1366
1367         /* decode length */
1368         code = ShowBits(&p_vpar->bit_stream, 5);
1369
1370         if (code<31)
1371         {
1372             size = pl_dct_dc_chrom_init_table_1[code].i_value;
1373             length = pl_dct_dc_chrom_init_table_1[code].i_length;
1374             RemoveBits(&p_vpar->bit_stream, length);
1375         }
1376         else
1377         {
1378             code = ShowBits(&p_vpar->bit_stream, 10) - 0x3e0;
1379             size = pl_dct_dc_chrom_init_table_2[code].i_value;
1380             length = pl_dct_dc_chrom_init_table_2[code].i_length;
1381             RemoveBits( &p_vpar->bit_stream, length);
1382         }
1383
1384         if (size==0)
1385             dct_diff = 0;
1386         else
1387         {
1388             dct_diff = GetBits( &p_vpar->bit_stream, size);
1389             if ((dct_diff & (1<<(size-1)))==0)
1390                 dct_diff-= (1<<size) - 1;
1391         }
1392
1393         i_dct_dc_diff = dct_diff;
1394     }
1395
1396     /* Dump the variable length code */
1397     //RemoveBits( &p_vpar->bit_stream, 
1398     //          p_vpar->pppl_dct_dc_size[i_type][i_select][i_pos].i_length );
1399     
1400     /* Read the actual code with the good length */
1401     p_vpar->slice.pi_dc_dct_pred[i_cc] += i_dct_dc_diff;
1402
1403     p_mb->ppi_blocks[i_b][0] = ( p_vpar->slice.pi_dc_dct_pred[i_cc] <<
1404                                ( 3 - p_vpar->picture.i_intra_dc_precision ) );
1405     i_nc = ( p_vpar->slice.pi_dc_dct_pred[i_cc] != 0 );
1406
1407 //fprintf( stderr, "coucou\n" );
1408     /* Decoding of the AC coefficients */
1409     
1410     i_coef = 0;
1411     b_vlc_intra = p_vpar->picture.b_intra_vlc_format;
1412     for( i_dummy = 1; /*i_dummy < 64*/; i_dummy++ )
1413     {
1414         i_code = ShowBits( &p_vpar->bit_stream, 16 );
1415         if( i_code >= 16384 )
1416         {
1417             if( b_vlc_intra )
1418             {
1419                 i_run =     pl_DCT_tab0a[(i_code>>8)-4].i_run;
1420                 i_level =   pl_DCT_tab0a[(i_code>>8)-4].i_level;
1421                 i_length =  pl_DCT_tab0a[(i_code>>8)-4].i_length;
1422 //fprintf( stderr, "**********> %d, %d, %d *******\n", i_run, i_level, (i_code>>8)-4 );
1423             }
1424             else
1425             {
1426                 i_run =     pl_DCT_tab_ac[(i_code>>12)-4].i_run;
1427                 i_level =   pl_DCT_tab_ac[(i_code>>12)-4].i_level;
1428                 i_length =  pl_DCT_tab_ac[(i_code>>12)-4].i_length;
1429              }
1430         }
1431         else
1432         {
1433             i_run =     ppl_dct_coef[b_vlc_intra][i_code].i_run;
1434             i_length =  ppl_dct_coef[b_vlc_intra][i_code].i_length;
1435             i_level =   ppl_dct_coef[b_vlc_intra][i_code].i_level;
1436         }
1437
1438         {
1439             int code = i_code;
1440             int intra_vlc_format = b_vlc_intra;
1441             dct_lookup_t tab;
1442             
1443     if (code>=16384 && !intra_vlc_format)
1444       tab = pl_DCT_tab_ac[(code>>12)-4];
1445     else if (code>=1024)
1446     {
1447       if (intra_vlc_format)
1448         tab = pl_DCT_tab0a[(code>>8)-4];
1449       else
1450         tab = pl_DCT_tab0[(code>>8)-4];
1451     }
1452     else if (code>=512)
1453     {
1454       if (intra_vlc_format)
1455         tab = pl_DCT_tab1a[(code>>6)-8];
1456       else
1457         tab = pl_DCT_tab1[(code>>6)-8];
1458     }
1459     else if (code>=256)
1460       tab = pl_DCT_tab2[(code>>4)-16];
1461     else if (code>=128)
1462       tab = pl_DCT_tab3[(code>>3)-16];
1463     else if (code>=64)
1464       tab = pl_DCT_tab4[(code>>2)-16];
1465     else if (code>=32)
1466       tab = pl_DCT_tab5[(code>>1)-16];
1467     else if (code>=16)
1468       tab = pl_DCT_tab6[code-16];
1469     else
1470     {
1471        fprintf( stderr, "invalid Huffman code in Decode_MPEG2_Intra_Block()\n");
1472     }
1473
1474     if( (i_run != tab.i_run) || (i_length != tab.i_length) || (i_level != tab.i_level) )
1475     {
1476         fprintf( stderr, "ET M....... !!!\n" );
1477         exit(0);
1478     }
1479         }
1480
1481
1482
1483
1484         
1485         RemoveBits( &p_vpar->bit_stream, i_length );
1486
1487        switch( i_run )
1488         {
1489             case DCT_ESCAPE:
1490                 i_run = GetBits( &p_vpar->bit_stream, 6 );
1491                 i_level = GetBits( &p_vpar->bit_stream, 12 );
1492                 p_mb->ppi_blocks[i_b][i_dummy] = ( b_sign = ( i_level > 2047 ) ) 
1493                                                           ? ( -4096 + i_level )
1494                                                           : i_level;
1495                 i_coef = i_dummy;
1496                 i_dummy += i_run;
1497                 i_nc ++;
1498                 break;
1499             case DCT_EOB:
1500                 if( i_nc <= 1 )
1501                 {
1502                     p_mb->pf_idct[i_b] = vdec_SparseIDCT;
1503                     p_mb->pi_sparse_pos[i_b] = i_coef;
1504                 }
1505                 else
1506                 {
1507                     p_mb->pf_idct[i_b] = vdec_IDCT;
1508                 }
1509                 return;
1510
1511                 break;
1512             default:
1513                 b_sign = GetBits( &p_vpar->bit_stream, 1 );
1514                 p_mb->ppi_blocks[i_b][i_dummy] = b_sign ? -i_level : i_level;
1515                 i_coef = i_dummy;
1516                 i_dummy += i_run;
1517                 i_nc ++;
1518         }
1519 // fprintf( stderr, "i_code : %d (%d), run : %d, %d, %d (%4x) ", i_code ,  b_vlc_intra,
1520 //                  i_run, i_level, i_dummy, ShowBits( &p_vpar->bit_stream, 16 ) );
1521
1522 //fprintf( stderr, "- %4x\n",ShowBits( &p_vpar->bit_stream, 16 ) ); 
1523     }
1524     
1525     if( i_nc <= 1 )
1526     {
1527         p_mb->pf_idct[i_b] = vdec_SparseIDCT;
1528         p_mb->pi_sparse_pos[i_b] = i_coef;
1529     }
1530     else
1531     {
1532         p_mb->pf_idct[i_b] = vdec_IDCT;
1533     }
1534 fprintf( stderr, "MPEG2 end (%d)\n", i_b );
1535 exit(0);
1536 }