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