]> git.sesse.net Git - vlc/blob - src/video_parser/vpar_blocks.c
La plupart des causes de segfault de la video ont �t� �limin�es, les messages
[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
16 #include "config.h"
17 #include "common.h"
18 #include "mtime.h"
19 #include "vlc_thread.h"
20
21 #include "intf_msg.h"
22 #include "debug.h"                    /* ?? temporaire, requis par netlist.h */
23
24 #include "input.h"
25 #include "input_netlist.h"
26 #include "decoder_fifo.h"
27 #include "video.h"
28 #include "video_output.h"
29
30 #include "vdec_idct.h"
31 #include "video_decoder.h"
32 #include "vdec_motion.h"
33
34 #include "vpar_blocks.h"
35 #include "vpar_headers.h"
36 #include "vpar_synchro.h"
37 #include "video_parser.h"
38 #include "video_fifo.h"
39
40
41
42
43
44
45
46 static int i_count = 0;
47
48
49
50
51
52
53 /*
54  * Welcome to vpar_blocks.c ! Here's where the heavy processor-critical parsing
55  * task is done. This file is divided in several parts :
56  *  - Initialization of the lookup tables
57  *  - Decoding of coded blocks
58  *  - Decoding of motion vectors
59  *  - Decoding of the other macroblock structures
60  *  - Picture data parsing management (slices and error handling)
61  * It's a pretty long file. Good luck and have a nice day.
62  */
63
64
65 /*
66  * Initialization tables
67  */
68
69     /* Table for coded_block_pattern resolution */
70 static lookup_t     pl_coded_pattern[512] =
71     { {MB_ERROR, 0}, {0, 9}, {39, 9}, {27, 9}, {59, 9}, {55, 9}, {47, 9}, {31, 9},
72     {58, 8}, {58, 8}, {54, 8}, {54, 8}, {46, 8}, {46, 8}, {30, 8}, {30, 8},
73     {57, 8}, {57, 8}, {53, 8}, {53, 8}, {45, 8}, {45, 8}, {29, 8}, {29, 8},
74     {38, 8}, {38, 8}, {26, 8}, {26, 8}, {37, 8}, {37, 8}, {25, 8}, {25, 8},
75     {43, 8}, {43, 8}, {23, 8}, {23, 8}, {51, 8}, {51, 8}, {15, 8}, {15, 8},
76     {42, 8}, {42, 8}, {22, 8}, {22, 8}, {50, 8}, {50, 8}, {14, 8}, {14, 8},
77     {41, 8}, {41, 8}, {21, 8}, {21, 8}, {49, 8}, {49, 8}, {13, 8}, {13, 8},
78     {35, 8}, {35, 8}, {19, 8}, {19, 8}, {11, 8}, {11, 8}, {7, 8}, {7, 8},
79     {34, 7}, {34, 7}, {34, 7}, {34, 7}, {18, 7}, {18, 7}, {18, 7}, {18, 7},
80     {10, 7}, {10, 7}, {10, 7}, {10, 7}, {6, 7}, {6, 7}, {6, 7}, {6, 7},
81     {33, 7}, {33, 7}, {33, 7}, {33, 7}, {17, 7}, {17, 7}, {17, 7}, {17, 7},
82     {9, 7}, {9, 7}, {9, 7}, {9, 7}, {5, 7}, {5, 7}, {5, 7}, {5, 7},
83     {63, 6}, {63, 6}, {63, 6}, {63, 6}, {63, 6}, {63, 6}, {63, 6}, {63, 6},
84     {3, 6}, {3, 6}, {3, 6}, {3, 6}, {3, 6}, {3, 6}, {3, 6}, {3, 6},
85     {36, 6}, {36, 6}, {36, 6}, {36, 6}, {36, 6}, {36, 6}, {36, 6}, {36, 6},
86     {24, 6}, {24, 6}, {24, 6}, {24, 6}, {24, 6}, {24, 6}, {24, 6}, {24, 6},
87     {62, 5}, {62, 5}, {62, 5}, {62, 5}, {62, 5}, {62, 5}, {62, 5}, {62, 5},
88     {62, 5}, {62, 5}, {62, 5}, {62, 5}, {62, 5}, {62, 5}, {62, 5}, {62, 5},
89     {2, 5}, {2, 5}, {2, 5}, {2, 5}, {2, 5}, {2, 5}, {2, 5}, {2, 5},
90     {2, 5}, {2, 5}, {2, 5}, {2, 5}, {2, 5}, {2, 5}, {2, 5}, {2, 5},
91     {61, 5}, {61, 5}, {61, 5}, {61, 5}, {61, 5}, {61, 5}, {61, 5}, {61, 5},
92     {61, 5}, {61, 5}, {61, 5}, {61, 5}, {61, 5}, {61, 5}, {61, 5}, {61, 5},
93     {1, 5}, {1, 5}, {1, 5}, {1, 5}, {1, 5}, {1, 5}, {1, 5}, {1, 5},
94     {1, 5}, {1, 5}, {1, 5}, {1, 5}, {1, 5}, {1, 5}, {1, 5}, {1, 5},
95     {56, 5}, {56, 5}, {56, 5}, {56, 5}, {56, 5}, {56, 5}, {56, 5}, {56, 5},
96     {56, 5}, {56, 5}, {56, 5}, {56, 5}, {56, 5}, {56, 5}, {56, 5}, {56, 5},
97     {52, 5}, {52, 5}, {52, 5}, {52, 5}, {52, 5}, {52, 5}, {52, 5}, {52, 5},
98     {52, 5}, {52, 5}, {52, 5}, {52, 5}, {52, 5}, {52, 5}, {52, 5}, {52, 5},
99     {44, 5}, {44, 5}, {44, 5}, {44, 5}, {44, 5}, {44, 5}, {44, 5}, {44, 5},
100     {44, 5}, {44, 5}, {44, 5}, {44, 5}, {44, 5}, {44, 5}, {44, 5}, {44, 5},
101     {28, 5}, {28, 5}, {28, 5}, {28, 5}, {28, 5}, {28, 5}, {28, 5}, {28, 5},
102     {28, 5}, {28, 5}, {28, 5}, {28, 5}, {28, 5}, {28, 5}, {28, 5}, {28, 5},
103     {40, 5}, {40, 5}, {40, 5}, {40, 5}, {40, 5}, {40, 5}, {40, 5}, {40, 5},
104     {40, 5}, {40, 5}, {40, 5}, {40, 5}, {40, 5}, {40, 5}, {40, 5}, {40, 5},
105     {20, 5}, {20, 5}, {20, 5}, {20, 5}, {20, 5}, {20, 5}, {20, 5}, {20, 5},
106     {20, 5}, {20, 5}, {20, 5}, {20, 5}, {20, 5}, {20, 5}, {20, 5}, {20, 5},
107     {48, 5}, {48, 5}, {48, 5}, {48, 5}, {48, 5}, {48, 5}, {48, 5}, {48, 5},
108     {48, 5}, {48, 5}, {48, 5}, {48, 5}, {48, 5}, {48, 5}, {48, 5}, {48, 5},
109     {12, 5}, {12, 5}, {12, 5}, {12, 5}, {12, 5}, {12, 5}, {12, 5}, {12, 5},
110     {12, 5}, {12, 5}, {12, 5}, {12, 5}, {12, 5}, {12, 5}, {12, 5}, {12, 5},
111     {32, 4}, {32, 4}, {32, 4}, {32, 4}, {32, 4}, {32, 4}, {32, 4}, {32, 4},
112     {32, 4}, {32, 4}, {32, 4}, {32, 4}, {32, 4}, {32, 4}, {32, 4}, {32, 4},
113     {32, 4}, {32, 4}, {32, 4}, {32, 4}, {32, 4}, {32, 4}, {32, 4}, {32, 4},
114     {32, 4}, {32, 4}, {32, 4}, {32, 4}, {32, 4}, {32, 4}, {32, 4}, {32, 4},
115     {16, 4}, {16, 4}, {16, 4}, {16, 4}, {16, 4}, {16, 4}, {16, 4}, {16, 4},
116     {16, 4}, {16, 4}, {16, 4}, {16, 4}, {16, 4}, {16, 4}, {16, 4}, {16, 4},
117     {16, 4}, {16, 4}, {16, 4}, {16, 4}, {16, 4}, {16, 4}, {16, 4}, {16, 4},
118     {16, 4}, {16, 4}, {16, 4}, {16, 4}, {16, 4}, {16, 4}, {16, 4}, {16, 4},
119     {8, 4}, {8, 4}, {8, 4}, {8, 4}, {8, 4}, {8, 4}, {8, 4}, {8, 4},
120     {8, 4}, {8, 4}, {8, 4}, {8, 4}, {8, 4}, {8, 4}, {8, 4}, {8, 4},
121     {8, 4}, {8, 4}, {8, 4}, {8, 4}, {8, 4}, {8, 4}, {8, 4}, {8, 4},
122     {8, 4}, {8, 4}, {8, 4}, {8, 4}, {8, 4}, {8, 4}, {8, 4}, {8, 4},
123     {4, 4}, {4, 4}, {4, 4}, {4, 4}, {4, 4}, {4, 4}, {4, 4}, {4, 4},
124     {4, 4}, {4, 4}, {4, 4}, {4, 4}, {4, 4}, {4, 4}, {4, 4}, {4, 4},
125     {4, 4}, {4, 4}, {4, 4}, {4, 4}, {4, 4}, {4, 4}, {4, 4}, {4, 4},
126     {4, 4}, {4, 4}, {4, 4}, {4, 4}, {4, 4}, {4, 4}, {4, 4}, {4, 4},
127     {60, 3}, {60, 3}, {60, 3}, {60, 3}, {60, 3}, {60, 3}, {60, 3}, {60, 3},
128     {60, 3}, {60, 3}, {60, 3}, {60, 3}, {60, 3}, {60, 3}, {60, 3}, {60, 3},
129     {60, 3}, {60, 3}, {60, 3}, {60, 3}, {60, 3}, {60, 3}, {60, 3}, {60, 3},
130     {60, 3}, {60, 3}, {60, 3}, {60, 3}, {60, 3}, {60, 3}, {60, 3}, {60, 3},
131     {60, 3}, {60, 3}, {60, 3}, {60, 3}, {60, 3}, {60, 3}, {60, 3}, {60, 3},
132     {60, 3}, {60, 3}, {60, 3}, {60, 3}, {60, 3}, {60, 3}, {60, 3}, {60, 3},
133     {60, 3}, {60, 3}, {60, 3}, {60, 3}, {60, 3}, {60, 3}, {60, 3}, {60, 3},
134     {60, 3}, {60, 3}, {60, 3}, {60, 3}, {60, 3}, {60, 3}, {60, 3}, {60, 3} };
135
136     /* Tables for dc DCT coefficients
137      * Tables are cut in two parts to reduce memory occupation
138      */
139
140     /* Table B-12, dct_dc_size_luminance, codes 00xxx ... 11110 */
141 static lookup_t     pl_dct_dc_lum_init_table_1[32] =
142     { {1, 2}, {1, 2}, {1, 2}, {1, 2}, {1, 2}, {1, 2}, {1, 2}, {1, 2},
143       {2, 2}, {2, 2}, {2, 2}, {2, 2}, {2, 2}, {2, 2}, {2, 2}, {2, 2},
144       {0, 3}, {0, 3}, {0, 3}, {0, 3}, {3, 3}, {3, 3}, {3, 3}, {3, 3},
145       {4, 3}, {4, 3}, {4, 3}, {4, 3}, {5, 4}, {5, 4}, {6, 5}, {MB_ERROR, 0}
146     };
147
148     /* Table B-12, dct_dc_size_luminance, codes 111110xxx ... 111111111 */
149 static lookup_t     pl_dct_dc_lum_init_table_2[32] =
150     { {7, 6}, {7, 6}, {7, 6}, {7, 6}, {7, 6}, {7, 6}, {7, 6}, {7, 6},
151       {8, 7}, {8, 7}, {8, 7}, {8, 7}, {9, 8}, {9, 8}, {10,9}, {11,9},
152       {MB_ERROR, 0}, {MB_ERROR, 0}, {MB_ERROR, 0}, {MB_ERROR, 0},
153       {MB_ERROR, 0}, {MB_ERROR, 0}, {MB_ERROR, 0}, {MB_ERROR, 0},
154       {MB_ERROR, 0}, {MB_ERROR, 0}, {MB_ERROR, 0}, {MB_ERROR, 0},
155       {MB_ERROR, 0}, {MB_ERROR, 0}, {MB_ERROR, 0}, {MB_ERROR, 0}
156     };
157
158     /* Table B-13, dct_dc_size_chrominance, codes 00xxx ... 11110 */
159 static lookup_t     pl_dct_dc_chrom_init_table_1[32] =
160     { {0, 2}, {0, 2}, {0, 2}, {0, 2}, {0, 2}, {0, 2}, {0, 2}, {0, 2},
161       {1, 2}, {1, 2}, {1, 2}, {1, 2}, {1, 2}, {1, 2}, {1, 2}, {1, 2},
162       {2, 2}, {2, 2}, {2, 2}, {2, 2}, {2, 2}, {2, 2}, {2, 2}, {2, 2},
163       {3, 3}, {3, 3}, {3, 3}, {3, 3}, {4, 4}, {4, 4}, {5, 5}, {MB_ERROR, 0}
164     };
165
166     /* Table B-13, dct_dc_size_chrominance, codes 111110xxxx ... 1111111111 */
167 static lookup_t     pl_dct_dc_chrom_init_table_2[32] =
168     { {6, 6}, {6, 6}, {6, 6}, {6, 6}, {6, 6}, {6, 6}, {6, 6}, {6, 6},
169       {6, 6}, {6, 6}, {6, 6}, {6, 6}, {6, 6}, {6, 6}, {6, 6}, {6, 6},
170       {7, 7}, {7, 7}, {7, 7}, {7, 7}, {7, 7}, {7, 7}, {7, 7}, {7, 7},
171       {8, 8}, {8, 8}, {8, 8}, {8, 8}, {9, 9}, {9, 9}, {10,10}, {11,10}
172     };
173
174
175     /* Tables for ac DCT coefficients. There are cut in many parts to save space */
176     /* Table B-14, DCT coefficients table zero,
177      * codes 0100 ... 1xxx (used for first (DC) coefficient)
178      */
179 static dct_lookup_t pl_DCT_tab_dc[12] =
180     {
181         {0,2,4}, {2,1,4}, {1,1,3}, {1,1,3},
182         {0,1,1}, {0,1,1}, {0,1,1}, {0,1,1},
183         {0,1,1}, {0,1,1}, {0,1,1}, {0,1,1}
184     };
185
186     /* Table B-14, DCT coefficients table zero,
187      * codes 0100 ... 1xxx (used for all other coefficients)
188      */
189 static dct_lookup_t pl_DCT_tab_ac[12] =
190     {
191         {0,2,4},  {2,1,4},  {1,1,3},  {1,1,3},
192         {DCT_EOB,0,2}, {DCT_EOB,0,2}, {DCT_EOB,0,2}, {DCT_EOB,0,2}, /* EOB */
193         {0,1,2},  {0,1,2},  {0,1,2},  {0,1,2}
194     };
195
196     /* Table B-14, DCT coefficients table zero,
197      * codes 000001xx ... 00111xxx
198      */
199 static dct_lookup_t pl_DCT_tab0[60] =
200     {
201         {DCT_ESCAPE,0,6}, {DCT_ESCAPE,0,6}, {DCT_ESCAPE,0,6}, {DCT_ESCAPE,0,6},
202         /* Escape */
203         {2,2,7}, {2,2,7}, {9,1,7}, {9,1,7},
204         {0,4,7}, {0,4,7}, {8,1,7}, {8,1,7},
205         {7,1,6}, {7,1,6}, {7,1,6}, {7,1,6},
206         {6,1,6}, {6,1,6}, {6,1,6}, {6,1,6},
207         {1,2,6}, {1,2,6}, {1,2,6}, {1,2,6},
208         {5,1,6}, {5,1,6}, {5,1,6}, {5,1,6},
209         {13,1,8}, {0,6,8}, {12,1,8}, {11,1,8},
210         {3,2,8}, {1,3,8}, {0,5,8}, {10,1,8},
211         {0,3,5}, {0,3,5}, {0,3,5}, {0,3,5},
212         {0,3,5}, {0,3,5}, {0,3,5}, {0,3,5},
213         {4,1,5}, {4,1,5}, {4,1,5}, {4,1,5},
214         {4,1,5}, {4,1,5}, {4,1,5}, {4,1,5},
215         {3,1,5}, {3,1,5}, {3,1,5}, {3,1,5},
216         {3,1,5}, {3,1,5}, {3,1,5}, {3,1,5}
217     };
218
219     /* Table B-15, DCT coefficients table one,
220      * codes 000001xx ... 11111111
221      */
222 static dct_lookup_t pl_DCT_tab0a[252] =
223     {
224         {65,0,6}, {65,0,6}, {65,0,6}, {65,0,6}, /* Escape */
225         {7,1,7}, {7,1,7}, {8,1,7}, {8,1,7},
226         {6,1,7}, {6,1,7}, {2,2,7}, {2,2,7},
227         {0,7,6}, {0,7,6}, {0,7,6}, {0,7,6},
228         {0,6,6}, {0,6,6}, {0,6,6}, {0,6,6},
229         {4,1,6}, {4,1,6}, {4,1,6}, {4,1,6},
230         {5,1,6}, {5,1,6}, {5,1,6}, {5,1,6},
231         {1,5,8}, {11,1,8}, {0,11,8}, {0,10,8},
232         {13,1,8}, {12,1,8}, {3,2,8}, {1,4,8},
233         {2,1,5}, {2,1,5}, {2,1,5}, {2,1,5},
234         {2,1,5}, {2,1,5}, {2,1,5}, {2,1,5},
235         {1,2,5}, {1,2,5}, {1,2,5}, {1,2,5},
236         {1,2,5}, {1,2,5}, {1,2,5}, {1,2,5},
237         {3,1,5}, {3,1,5}, {3,1,5}, {3,1,5},
238         {3,1,5}, {3,1,5}, {3,1,5}, {3,1,5},
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         {1,1,3}, {1,1,3}, {1,1,3}, {1,1,3},
246         {1,1,3}, {1,1,3}, {1,1,3}, {1,1,3},
247         {64,0,4}, {64,0,4}, {64,0,4}, {64,0,4}, /* EOB */
248         {64,0,4}, {64,0,4}, {64,0,4}, {64,0,4},
249         {64,0,4}, {64,0,4}, {64,0,4}, {64,0,4},
250         {64,0,4}, {64,0,4}, {64,0,4}, {64,0,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,3,4}, {0,3,4}, {0,3,4}, {0,3,4},
254         {0,3,4}, {0,3,4}, {0,3,4}, {0,3,4},
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,1,2}, {0,1,2}, {0,1,2}, {0,1,2},
269         {0,1,2}, {0,1,2}, {0,1,2}, {0,1,2},
270         {0,1,2}, {0,1,2}, {0,1,2}, {0,1,2},
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,2,3}, {0,2,3}, {0,2,3}, {0,2,3},
277         {0,2,3}, {0,2,3}, {0,2,3}, {0,2,3},
278         {0,2,3}, {0,2,3}, {0,2,3}, {0,2,3},
279         {0,4,5}, {0,4,5}, {0,4,5}, {0,4,5},
280         {0,4,5}, {0,4,5}, {0,4,5}, {0,4,5},
281         {0,5,5}, {0,5,5}, {0,5,5}, {0,5,5},
282         {0,5,5}, {0,5,5}, {0,5,5}, {0,5,5},
283         {9,1,7}, {9,1,7}, {1,3,7}, {1,3,7},
284         {10,1,7}, {10,1,7}, {0,8,7}, {0,8,7},
285         {0,9,7}, {0,9,7}, {0,12,8}, {0,13,8},
286         {2,3,8}, {4,2,8}, {0,14,8}, {0,15,8}
287     };
288
289     /* Table B-14, DCT coefficients table zero,
290      * codes 0000001000 ... 0000001111
291      */
292 static dct_lookup_t pl_DCT_tab1[8] =
293     {
294         {16,1,10}, {5,2,10}, {0,7,10}, {2,3,10},
295         {1,4,10}, {15,1,10}, {14,1,10}, {4,2,10}
296     };
297
298     /* Table B-15, DCT coefficients table one,
299      * codes 000000100x ... 000000111x
300      */
301 static dct_lookup_t pl_DCT_tab1a[8] =
302     {
303         {5,2,9}, {5,2,9}, {14,1,9}, {14,1,9},
304         {2,4,10}, {16,1,10}, {15,1,9}, {15,1,9}
305     };
306
307     /* Table B-14/15, DCT coefficients table zero / one,
308      * codes 000000010000 ... 000000011111
309      */
310 static dct_lookup_t pl_DCT_tab2[16] =
311     {
312         {0,11,12}, {8,2,12}, {4,3,12}, {0,10,12},
313         {2,4,12}, {7,2,12}, {21,1,12}, {20,1,12},
314         {0,9,12}, {19,1,12}, {18,1,12}, {1,5,12},
315         {3,3,12}, {0,8,12}, {6,2,12}, {17,1,12}
316     };
317
318     /* Table B-14/15, DCT coefficients table zero / one,
319      * codes 0000000010000 ... 0000000011111
320      */
321 static dct_lookup_t pl_DCT_tab3[16] =
322     {
323         {10,2,13}, {9,2,13}, {5,3,13}, {3,4,13},
324         {2,5,13}, {1,7,13}, {1,6,13}, {0,15,13},
325         {0,14,13}, {0,13,13}, {0,12,13}, {26,1,13},
326         {25,1,13}, {24,1,13}, {23,1,13}, {22,1,13}
327     };
328
329     /* Table B-14/15, DCT coefficients table zero / one,
330      * codes 00000000010000 ... 00000000011111
331      */
332 static dct_lookup_t pl_DCT_tab4[16] =
333     {
334         {0,31,14}, {0,30,14}, {0,29,14}, {0,28,14},
335         {0,27,14}, {0,26,14}, {0,25,14}, {0,24,14},
336         {0,23,14}, {0,22,14}, {0,21,14}, {0,20,14},
337         {0,19,14}, {0,18,14}, {0,17,14}, {0,16,14}
338     };
339
340     /* Table B-14/15, DCT coefficients table zero / one,
341      *   codes 000000000010000 ... 000000000011111
342      */
343 static dct_lookup_t pl_DCT_tab5[16] =
344     {
345     {0,40,15}, {0,39,15}, {0,38,15}, {0,37,15},
346     {0,36,15}, {0,35,15}, {0,34,15}, {0,33,15},
347     {0,32,15}, {1,14,15}, {1,13,15}, {1,12,15},
348     {1,11,15}, {1,10,15}, {1,9,15}, {1,8,15}
349     };
350
351     /* Table B-14/15, DCT coefficients table zero / one,
352      * codes 0000000000010000 ... 0000000000011111
353      */
354 static dct_lookup_t pl_DCT_tab6[16] =
355     {
356         {1,18,16}, {1,17,16}, {1,16,16}, {1,15,16},
357         {6,3,16}, {16,2,16}, {15,2,16}, {14,2,16},
358         {13,2,16}, {12,2,16}, {11,2,16}, {31,1,16},
359         {30,1,16}, {29,1,16}, {28,1,16}, {27,1,16}
360     };
361
362
363 /*
364  * Initialization of lookup tables
365  */
366
367 /*****************************************************************************
368  * vpar_InitCrop : Initialize the crop table for saturation
369  *                 (ISO/IEC 13818-2 section 7.4.3)
370  *****************************************************************************/
371 #if defined(MPEG2_COMPLIANT) && !defined(VDEC_DFT)
372 void vpar_InitCrop( vpar_thread_t * p_vpar )
373 {
374     int i_dummy;
375
376     p_vpar->pi_crop = p_vpar->pi_crop_buf + 4096;
377
378     for( i_dummy = -4096; i_dummy < -2048; i_dummy++ )
379     {
380         p_vpar->pi_crop[i_dummy] = -2048;
381     }
382     for( ; i_dummy < 2047; i_dummy++ )
383     {
384         p_vpar->pi_crop[i_dummy] = i_dummy;
385     }
386     for( ; i_dummy < 4095; i_dummy++ )
387     {
388         p_vpar->pi_crop[i_dummy] = 2047;
389     }
390 }
391 #endif
392
393 /*****************************************************************************
394  * vpar_InitMbAddrInc : Initialize the lookup table for mb_addr_inc
395  *****************************************************************************/
396
397 /* Function for filling up the lookup table for mb_addr_inc */
398 static void __inline__ FillMbAddrIncTable( vpar_thread_t * p_vpar,
399                                     int i_start, int i_end, int i_step,
400                                     int * pi_value, int i_length )
401 {
402     int i_pos, i_offset;
403     for( i_pos = i_start ; i_pos < i_end ; i_pos += i_step )
404     {
405         for( i_offset = 0 ; i_offset < i_step ; i_offset ++ )
406         {
407             p_vpar->pl_mb_addr_inc[i_pos + i_offset].i_value = * pi_value;
408             p_vpar->pl_mb_addr_inc[i_pos + i_offset].i_length = i_length;
409         }
410         (*pi_value)--;
411     }
412 }
413
414 /* Function that initialize the table using the last one */
415 void vpar_InitMbAddrInc( vpar_thread_t * p_vpar )
416 {
417     int i_dummy;
418     int i_value;
419
420     for( i_dummy = 0 ; i_dummy < 8 ; i_dummy++ )
421     {
422         p_vpar->pl_mb_addr_inc[i_dummy].i_value = MB_ERROR;
423         p_vpar->pl_mb_addr_inc[i_dummy].i_length = 0;
424     }
425
426     p_vpar->pl_mb_addr_inc[8].i_value = MB_ADDRINC_ESCAPE;
427     p_vpar->pl_mb_addr_inc[8].i_length = 11;
428
429     for( i_dummy = 9 ; i_dummy < 15 ; i_dummy ++ )
430     {
431         p_vpar->pl_mb_addr_inc[i_dummy].i_value =  MB_ERROR;
432         p_vpar->pl_mb_addr_inc[i_dummy].i_length = 0;
433     }
434
435     p_vpar->pl_mb_addr_inc[15].i_value = MB_ADDRINC_STUFFING;
436     p_vpar->pl_mb_addr_inc[15].i_length = 11;
437
438     for( i_dummy = 16; i_dummy < 24; i_dummy++ )
439     {
440         p_vpar->pl_mb_addr_inc[i_dummy].i_value =  MB_ERROR;
441         p_vpar->pl_mb_addr_inc[i_dummy].i_length = 0;
442     }
443
444     i_value = 33;
445
446     FillMbAddrIncTable( p_vpar, 24, 36, 1, &i_value, 11 );
447     FillMbAddrIncTable( p_vpar, 36, 48, 2, &i_value, 10 );
448     FillMbAddrIncTable( p_vpar, 48, 96, 8, &i_value, 8 );
449     FillMbAddrIncTable( p_vpar, 96, 128, 16, &i_value, 7 );
450     FillMbAddrIncTable( p_vpar, 128, 256, 64, &i_value, 5 );
451     FillMbAddrIncTable( p_vpar, 256, 512, 128, &i_value, 4 );
452     FillMbAddrIncTable( p_vpar, 512, 1024, 256, &i_value, 3 );
453     FillMbAddrIncTable( p_vpar, 1024, 2048, 1024, &i_value, 1 );
454 }
455
456 /*****************************************************************************
457  * vpar_Init*MBType : Initialize lookup table for the Macroblock type
458  *****************************************************************************/
459
460 /* Fonction for filling up the tables */
461 static void __inline__ FillMBType( vpar_thread_t * p_vpar,
462                                    int           i_mb_type,
463                                    int           i_start,
464                                    int           i_end,
465                                    int           i_value,
466                                    int           i_length )
467 {
468     int i_dummy;
469
470     for( i_dummy = i_start ; i_dummy < i_end ; i_dummy++ )
471     {
472         p_vpar->ppl_mb_type[i_mb_type][i_dummy].i_value = i_value;
473         p_vpar->ppl_mb_type[i_mb_type][i_dummy].i_length = i_length;
474     }
475 }
476
477 /* Fonction that fills the table for P MB_Type */
478 void vpar_InitPMBType( vpar_thread_t * p_vpar )
479 {
480     FillMBType( p_vpar, 0, 32, 64, MB_MOTION_FORWARD|MB_PATTERN, 1 );
481     FillMBType( p_vpar, 0, 16, 32, MB_PATTERN, 2 );
482     FillMBType( p_vpar, 0, 8, 16, MB_MOTION_FORWARD, 3 );
483     FillMBType( p_vpar, 0, 6, 8, MB_INTRA, 5 );
484     FillMBType( p_vpar, 0, 4, 6, MB_QUANT|MB_MOTION_FORWARD|MB_PATTERN, 5 );
485     FillMBType( p_vpar, 0, 2, 4, MB_QUANT|MB_PATTERN, 5 );
486     p_vpar->ppl_mb_type[0][1].i_value = MB_QUANT|MB_INTRA;
487     p_vpar->ppl_mb_type[0][1].i_length = 6;
488     p_vpar->ppl_mb_type[0][0].i_value = MB_ERROR;
489     p_vpar->ppl_mb_type[0][0].i_length = 0;
490 }
491
492 /* Fonction that fills the table for B MB_Type */
493 void vpar_InitBMBType( vpar_thread_t * p_vpar )
494 {
495     FillMBType( p_vpar, 1, 48, 64, MB_MOTION_FORWARD
496                                   |MB_MOTION_BACKWARD|MB_PATTERN, 2 );
497     FillMBType( p_vpar, 1, 32, 48, MB_MOTION_FORWARD|MB_MOTION_BACKWARD, 2 );
498     FillMBType( p_vpar, 1, 24, 32, MB_MOTION_BACKWARD|MB_PATTERN, 3 );
499     FillMBType( p_vpar, 1, 16, 24, MB_MOTION_BACKWARD, 3 );
500     FillMBType( p_vpar, 1, 12, 16, MB_MOTION_FORWARD|MB_PATTERN, 4 );
501     FillMBType( p_vpar, 1, 8, 12, MB_MOTION_FORWARD, 4 );
502     FillMBType( p_vpar, 1, 6, 8, MB_INTRA, 5 );
503     FillMBType( p_vpar, 1, 4, 6, MB_QUANT|MB_MOTION_FORWARD
504                                 |MB_MOTION_BACKWARD|MB_PATTERN, 5 );
505     p_vpar->ppl_mb_type[1][3].i_value = MB_QUANT|MB_MOTION_FORWARD|MB_PATTERN;
506     p_vpar->ppl_mb_type[1][3].i_length = 6;
507     p_vpar->ppl_mb_type[1][2].i_value = MB_QUANT|MB_MOTION_BACKWARD|MB_PATTERN;
508     p_vpar->ppl_mb_type[1][2].i_length = 6;
509     p_vpar->ppl_mb_type[1][1].i_value = MB_QUANT|MB_INTRA;
510     p_vpar->ppl_mb_type[1][1].i_length = 6;
511     p_vpar->ppl_mb_type[1][0].i_value =MB_ERROR;
512     p_vpar->ppl_mb_type[1][0].i_length = 0;
513 }
514
515
516 /*****************************************************************************
517  * vpar_InitDCTTables : Initialize tables giving the length of the dct
518  *                      coefficient from the vlc code
519  *****************************************************************************/
520
521 /* First fonction for filling the table */
522 static void __inline__ FillDCTTable( dct_lookup_t * p_tab_dest, dct_lookup_t * p_tab_src,
523                                      int i_step, int i_nb_elem, int i_offset )
524 {
525     int i_dummy, i_dummy2;
526
527     for( i_dummy=0 ; i_dummy < i_nb_elem ; i_dummy++ )
528     {
529         for( i_dummy2=0 ; i_dummy2 < i_step ; i_dummy2++ )
530         {
531             p_tab_dest[(i_dummy+i_offset)*i_step+i_dummy2] = p_tab_src[i_dummy];
532         }
533     }
534 }
535
536
537 /* Fonction that actually fills the table or create the pointers */
538 void vpar_InitDCTTables( vpar_thread_t * p_vpar )
539 {
540     /* Tables are cut in two parts to reduce memory occupation */
541     p_vpar->pppl_dct_dc_size[0][0] = pl_dct_dc_lum_init_table_1;
542     p_vpar->pppl_dct_dc_size[0][1] = pl_dct_dc_lum_init_table_2;
543     p_vpar->pppl_dct_dc_size[1][0] = pl_dct_dc_chrom_init_table_1;
544     p_vpar->pppl_dct_dc_size[1][1] = pl_dct_dc_chrom_init_table_2;
545
546     /* ??? MB_ERROR is replaced by 0 because if we use -1 we 
547      * can block in DecodeMPEG2Intra and others */
548     memset( p_vpar->ppl_dct_coef[0], 0, 16 );
549     memset( p_vpar->ppl_dct_coef[1], 0, 16 );
550     
551     /* For table B14 & B15, we have a pointer to tables */
552     /* We fill the table thanks to the fonction defined above */
553     FillDCTTable( p_vpar->ppl_dct_coef[0], pl_DCT_tab0, 256, 60,  4 );
554     FillDCTTable( p_vpar->ppl_dct_coef[0], pl_DCT_tab1,  64,  8,  8 );
555     FillDCTTable( p_vpar->ppl_dct_coef[0], pl_DCT_tab2,  16, 16, 16 );
556     FillDCTTable( p_vpar->ppl_dct_coef[0], pl_DCT_tab3,   8, 16, 16 );
557     FillDCTTable( p_vpar->ppl_dct_coef[0], pl_DCT_tab4,   4, 16, 16 );
558     FillDCTTable( p_vpar->ppl_dct_coef[0], pl_DCT_tab5,   2, 16, 16 );
559     FillDCTTable( p_vpar->ppl_dct_coef[0], pl_DCT_tab6,   1, 16, 16 );
560
561     FillDCTTable( p_vpar->ppl_dct_coef[1], pl_DCT_tab0a, 256, 60, 4 );
562     FillDCTTable( p_vpar->ppl_dct_coef[1], pl_DCT_tab1a,  64,  8,  8 );
563     FillDCTTable( p_vpar->ppl_dct_coef[1], pl_DCT_tab2,   16, 16, 16 );
564     FillDCTTable( p_vpar->ppl_dct_coef[1], pl_DCT_tab3,    8, 16, 16 );
565     FillDCTTable( p_vpar->ppl_dct_coef[1], pl_DCT_tab4,    4, 16, 16 );
566     FillDCTTable( p_vpar->ppl_dct_coef[1], pl_DCT_tab5,    2, 16, 16 );
567     FillDCTTable( p_vpar->ppl_dct_coef[1], pl_DCT_tab6,    1, 16, 16 );
568 }
569
570
571 /*
572  * Block parsing
573  */
574
575 /*****************************************************************************
576  * DecodeMPEG1NonIntra : decode MPEG-1 non-intra blocks
577  *****************************************************************************/
578 static __inline__ void DecodeMPEG1NonIntra( vpar_thread_t * p_vpar,
579                                             macroblock_t * p_mb, int i_b,
580                                             int i_chroma_format )
581 {
582
583     if( p_vpar->picture.i_coding_type == D_CODING_TYPE )
584     {
585         /* Remove end_of_macroblock (always 1, prevents startcode emulation)
586          * ISO/IEC 11172-2 section 2.4.2.7 and 2.4.3.6 */
587         RemoveBits( &p_vpar->bit_stream, 1 );
588     }
589 }
590
591 /*****************************************************************************
592  * DecodeMPEG1Intra : decode MPEG-1 intra blocks
593  *****************************************************************************/
594 static __inline__ void DecodeMPEG1Intra( vpar_thread_t * p_vpar,
595                                          macroblock_t * p_mb, int i_b ,
596                                          int i_chroma_format )
597 {
598
599     if( p_vpar->picture.i_coding_type == D_CODING_TYPE )
600     {
601         /* Remove end_of_macroblock (always 1, prevents startcode emulation)
602          * ISO/IEC 11172-2 section 2.4.2.7 and 2.4.3.6 */
603         RemoveBits( &p_vpar->bit_stream, 1 );
604     }
605 }
606
607 /*****************************************************************************
608  * DecodeMPEG2NonIntra : decode MPEG-2 non-intra blocks
609  *****************************************************************************/
610 static __inline__ void DecodeMPEG2NonIntra( vpar_thread_t * p_vpar,
611                                             macroblock_t * p_mb, int i_b,
612                                             int i_chroma_format )
613 {
614     int         i_parse;
615     int         i_nc;
616     int         i_cc;
617     int         i_coef;
618     int         i_type;
619     int         i_code;
620     int         i_length;
621     int         i_pos;
622     int         i_run;
623     int         i_level;
624     boolean_t   b_sign;
625     int *       pi_quant;
626
627     /* Lookup Table for the chromatic component */
628     static int pi_cc_index[12] = { 0, 0, 0, 0, 1, 2, 1, 2, 1, 2 };
629
630     i_cc = pi_cc_index[i_b];
631
632     /* Determine whether it is luminance or not (chrominance) */
633     i_type = ( i_cc + 1 ) >> 1;
634
635     /* Give a pointer to the quantization matrices for intra blocks */
636     if( (i_chroma_format == CHROMA_420) || (!i_type) )
637     {
638         pi_quant = p_vpar->sequence.nonintra_quant.pi_matrix;
639     }
640     else
641     {
642         pi_quant = p_vpar->sequence.chroma_nonintra_quant.pi_matrix;
643     }
644
645     /* Decoding of the AC coefficients */
646
647     i_nc = 0;
648     i_coef = 0;
649     for( i_parse = 0; !p_vpar->b_die; i_parse++ )
650     {
651         i_code = ShowBits( &p_vpar->bit_stream, 16 );
652         if( i_code >= 16384 )
653         {
654             if( i_parse == 0 )
655             {
656                 i_run =     pl_DCT_tab_dc[(i_code>>12)-4].i_run;
657                 i_level =   pl_DCT_tab_dc[(i_code>>12)-4].i_level;
658                 i_length =  pl_DCT_tab_dc[(i_code>>12)-4].i_length;
659             }
660             else
661             {
662                 i_run =     pl_DCT_tab_ac[(i_code>>12)-4].i_run;
663                 i_level =   pl_DCT_tab_ac[(i_code>>12)-4].i_level;
664                 i_length =  pl_DCT_tab_ac[(i_code>>12)-4].i_length;
665              }
666         }
667         else if( i_code >= 1024 )
668         {
669             i_run =     pl_DCT_tab0[(i_code>>8)-4].i_run;
670             i_length =  pl_DCT_tab0[(i_code>>8)-4].i_length;
671             i_level =   pl_DCT_tab0[(i_code>>8)-4].i_level;
672         }
673         else
674         {
675             i_run =     p_vpar->ppl_dct_coef[0][i_code].i_run;
676             i_length =  p_vpar->ppl_dct_coef[0][i_code].i_length;
677             i_level =   p_vpar->ppl_dct_coef[0][i_code].i_level;
678         }
679
680
681         RemoveBits( &p_vpar->bit_stream, i_length );
682
683         switch( i_run )
684         {
685             case DCT_ESCAPE:
686                 i_run = GetBits( &p_vpar->bit_stream, 6 );
687                 i_level = GetBits( &p_vpar->bit_stream, 12 );
688                 i_level = (b_sign = ( i_level > 2047 )) ? 4096 - i_level
689                                                         : i_level;
690                 break;
691             case DCT_EOB:
692 #ifdef HAVE_MMX
693                 /* The MMX IDCT has a precision problem with non-intra
694                  * blocks. */
695                 p_mb->ppi_blocks[i_b][0] += 4;
696 #endif
697                 if( i_nc <= 1 )
698                 {
699                     p_mb->pf_idct[i_b] = vdec_SparseIDCT;
700                     p_mb->pi_sparse_pos[i_b] = i_coef;
701                 }
702                 else
703                 {
704                     p_mb->pf_idct[i_b] = vdec_IDCT;
705                 }
706                 return;
707
708                 break;
709             default:
710                 b_sign = GetBits( &p_vpar->bit_stream, 1 );
711         }
712         i_coef = i_parse;
713         i_parse += i_run;
714         i_nc ++;
715
716         if( i_parse >= 64 )
717         {
718             break;
719         }
720
721         i_pos = pi_scan[p_vpar->picture.b_alternate_scan][i_parse];
722         i_level = ( ((i_level << 1) + 1) * p_vpar->mb.i_quantizer_scale
723                     * pi_quant[i_pos] ) >> 5;
724         p_mb->ppi_blocks[i_b][i_pos] = b_sign ? -i_level : i_level;
725     }
726
727     intf_ErrMsg("vpar error: DCT coeff (non-intra) is out of bounds\n");
728     p_vpar->picture.b_error = 1;
729 }
730
731 /*****************************************************************************
732  * DecodeMPEG2Intra : decode MPEG-2 intra blocks
733  *****************************************************************************/
734 static __inline__ void DecodeMPEG2Intra( vpar_thread_t * p_vpar,
735                                          macroblock_t * p_mb, int i_b,
736                                          int i_chroma_format )
737 {
738     int         i_parse;
739     int         i_nc;
740     int         i_cc;
741     int         i_coef;
742     int         i_type;
743     int         i_code;
744     int         i_length;
745     int         i_pos;
746     int         i_dct_dc_size;
747     int         i_dct_dc_diff;
748     int         i_run;
749     int         i_level;
750     boolean_t   b_vlc_intra;
751     boolean_t   b_sign;
752     int *       pi_quant;
753
754     /* Lookup Table for the chromatic component */
755     static int pi_cc_index[12] = { 0, 0, 0, 0, 1, 2, 1, 2, 1, 2 };
756     i_cc = pi_cc_index[i_b];
757
758     /* Determine whether it is luminance or not (chrominance) */
759     i_type = ( i_cc + 1 ) >> 1;
760
761     /* Give a pointer to the quantization matrices for intra blocks */
762     if( (i_chroma_format == CHROMA_420) || (!i_type) )
763     {
764         pi_quant = p_vpar->sequence.intra_quant.pi_matrix;
765     }
766     else
767     {
768         pi_quant = p_vpar->sequence.chroma_intra_quant.pi_matrix;
769     }
770
771 #if 0
772     /* Decoding of the DC intra coefficient */
773     /* The nb of bits to parse depends on i_type */
774     i_code = ShowBits( &p_vpar->bit_stream, 9 + i_type );
775
776     /* To reduce memory occupation, there are two lookup tables
777      * See InitDCT above */
778     i_code5 = i_code >> (4+i_type);
779
780     /* Shall we lookup in the first or in the second table ? */
781     i_select = ( i_code5 == 31 );
782     /* Offset value for looking in the second table */
783     i_offset = 0x1f0 + ( i_type * 0x1f0 );
784     i_pos = ( i_code5 * ( ! i_select ) ) + ( ( i_code - i_offset ) * i_select );
785     i_dct_dc_size = p_vpar->pppl_dct_dc_size[i_type][i_select][i_pos].i_value;
786 #endif
787
788     if( !i_type/*i_b < 4*/ )
789     {
790         /* decode length */
791         i_code = ShowBits(&p_vpar->bit_stream, 5);
792         if (i_code<31)
793         {
794             i_dct_dc_size = pl_dct_dc_lum_init_table_1[i_code].i_value;
795             i_length = pl_dct_dc_lum_init_table_1[i_code].i_length;
796             RemoveBits( &p_vpar->bit_stream, i_length);
797         }
798         else
799         {
800             i_code = ShowBits(&p_vpar->bit_stream, 9) - 0x1f0;
801             i_dct_dc_size = pl_dct_dc_lum_init_table_2[i_code].i_value;
802             i_length = pl_dct_dc_lum_init_table_2[i_code].i_length;
803             RemoveBits( &p_vpar->bit_stream, i_length);
804         }
805     }
806     else
807     {
808         /* decode length */
809         i_code = ShowBits(&p_vpar->bit_stream, 5);
810
811         if (i_code<31)
812         {
813             i_dct_dc_size = pl_dct_dc_chrom_init_table_1[i_code].i_value;
814             i_length = pl_dct_dc_chrom_init_table_1[i_code].i_length;
815             RemoveBits(&p_vpar->bit_stream, i_length);
816         }
817         else
818         {
819             i_code = ShowBits(&p_vpar->bit_stream, 10) - 0x3e0;
820             i_dct_dc_size = pl_dct_dc_chrom_init_table_2[i_code].i_value;
821             i_length = pl_dct_dc_chrom_init_table_2[i_code].i_length;
822             RemoveBits( &p_vpar->bit_stream, i_length);
823         }
824     }
825     if (i_dct_dc_size==0)
826         i_dct_dc_diff = 0;
827     else
828     {
829         i_dct_dc_diff = GetBits( &p_vpar->bit_stream, i_dct_dc_size);
830         if ((i_dct_dc_diff & (1<<(i_dct_dc_size-1)))==0)
831             i_dct_dc_diff-= (1<<i_dct_dc_size) - 1;
832     }
833
834     /* Dump the variable length code */
835     //RemoveBits( &p_vpar->bit_stream,
836     //          p_vpar->pppl_dct_dc_size[i_type][i_select][i_pos].i_length );
837
838     /* Read the actual code with the good length */
839     p_vpar->mb.pi_dc_dct_pred[i_cc] += i_dct_dc_diff;
840
841     p_mb->ppi_blocks[i_b][0] = ( p_vpar->mb.pi_dc_dct_pred[i_cc] <<
842                                ( 3 - p_vpar->picture.i_intra_dc_precision ) );
843
844     i_nc = ( p_vpar->mb.pi_dc_dct_pred[i_cc] != 0 );
845
846     /* Decoding of the AC coefficients */
847
848     i_coef = 0;
849     b_vlc_intra = p_vpar->picture.b_intra_vlc_format;
850     for( i_parse = 1; !p_vpar->b_die/*i_parse < 64*/; i_parse++ )
851     {
852         i_code = ShowBits( &p_vpar->bit_stream, 16 );
853         if( i_code >= 16384 )
854         {
855             if( b_vlc_intra )
856             {
857                 i_run =     pl_DCT_tab0a[(i_code>>8)-4].i_run;
858                 i_level =   pl_DCT_tab0a[(i_code>>8)-4].i_level;
859                 i_length =  pl_DCT_tab0a[(i_code>>8)-4].i_length;
860 //fprintf( stderr, "**********> %d, %d, %d *******\n", i_run, i_level, (i_code>>8)-4 );
861             }
862             else
863             {
864                 i_run =     pl_DCT_tab_ac[(i_code>>12)-4].i_run;
865                 i_level =   pl_DCT_tab_ac[(i_code>>12)-4].i_level;
866                 i_length =  pl_DCT_tab_ac[(i_code>>12)-4].i_length;
867              }
868         }
869         else
870         {
871             i_run =     p_vpar->ppl_dct_coef[b_vlc_intra][i_code].i_run;
872             i_length =  p_vpar->ppl_dct_coef[b_vlc_intra][i_code].i_length;
873             i_level =   p_vpar->ppl_dct_coef[b_vlc_intra][i_code].i_level;
874         }
875
876 #if 0
877         {
878             int code = i_code;
879             int intra_vlc_format = b_vlc_intra;
880             dct_lookup_t tab;
881
882     if (code>=16384 && !intra_vlc_format)
883       tab = pl_DCT_tab_ac[(code>>12)-4];
884     else if (code>=1024)
885     {
886       if (intra_vlc_format)
887         tab = pl_DCT_tab0a[(code>>8)-4];
888       else
889         tab = pl_DCT_tab0[(code>>8)-4];
890     }
891     else if (code>=512)
892     {
893       if (intra_vlc_format)
894         tab = pl_DCT_tab1a[(code>>6)-8];
895       else
896         tab = pl_DCT_tab1[(code>>6)-8];
897     }
898     else if (code>=256)
899       tab = pl_DCT_tab2[(code>>4)-16];
900     else if (code>=128)
901       tab = pl_DCT_tab3[(code>>3)-16];
902     else if (code>=64)
903       tab = pl_DCT_tab4[(code>>2)-16];
904     else if (code>=32)
905       tab = pl_DCT_tab5[(code>>1)-16];
906     else if (code>=16)
907       tab = pl_DCT_tab6[code-16];
908     else
909     {
910        fprintf( stderr, "invalid Huffman code in Decode_MPEG2_Intra_Block()\n");
911     }
912
913     if( (i_run != tab.i_run) || (i_length != tab.i_length) || (i_level != tab.i_level) )
914     {
915         fprintf( stderr, "ET M....... !!!\n" );
916         exit(0);
917     }
918         }
919 #endif
920
921
922
923
924         RemoveBits( &p_vpar->bit_stream, i_length );
925
926         switch( i_run )
927         {
928             case DCT_ESCAPE:
929                 i_run = GetBits( &p_vpar->bit_stream, 6 );
930                 i_level = GetBits( &p_vpar->bit_stream, 12 );
931                 /*p_mb->ppi_blocks[i_b][i_parse] = ( b_sign = ( i_level > 2047 ) )
932                                                           ? ( -4096 + i_level )
933                                                           : i_level;*/
934                 i_level = (b_sign = ( i_level > 2047 )) ? 4096 - i_level
935                                                         : i_level;
936                 break;
937             case DCT_EOB:
938                 if( i_nc <= 1 )
939                 {
940                     p_mb->pf_idct[i_b] = vdec_SparseIDCT;
941                     p_mb->pi_sparse_pos[i_b] = i_coef;
942                 }
943                 else
944                 {
945                     p_mb->pf_idct[i_b] = vdec_IDCT;
946                 }
947                 return;
948
949                 break;
950             default:
951                 b_sign = GetBits( &p_vpar->bit_stream, 1 );
952         }
953
954         i_coef = i_parse;
955         i_parse += i_run;
956         i_nc ++;
957
958         if( i_parse >= 64 )
959         {
960             break;
961         }
962
963         i_pos = pi_scan[p_vpar->picture.b_alternate_scan][i_parse];
964         i_level = ( i_level *
965                     p_vpar->mb.i_quantizer_scale *
966                     pi_quant[i_pos] ) >> 4;
967         p_mb->ppi_blocks[i_b][i_pos] = b_sign ? -i_level : i_level;
968     }
969
970     intf_ErrMsg("vpar error: DCT coeff (intra) is out of bounds\n");
971     p_vpar->picture.b_error = 1;
972 }
973
974
975 /*
976  * Motion vectors
977  */
978
979 /****************************************************************************
980  * MotionCode : Parse the next motion code
981  ****************************************************************************/
982 static __inline__ int MotionCode( vpar_thread_t * p_vpar )
983 {
984     int i_code;
985     static lookup_t pl_mv_tab0[8] =
986         { {-1,0}, {3,3}, {2,2}, {2,2}, {1,1}, {1,1}, {1,1}, {1,1} };
987     /* Table B-10, motion_code, codes 0000011 ... 000011x */
988     static lookup_t pl_mv_tab1[8] =
989         { {-1,0}, {-1,0}, {-1,0}, {7,6}, {6,6}, {5,6}, {4,5}, {4,5} };
990     /* Table B-10, motion_code, codes 0000001100 ... 000001011x */
991     static lookup_t pl_mv_tab2[12] = {
992         {16,9}, {15,9}, {14,9}, {13,9},
993         {12,9}, {11,9}, {10,8}, {10,8},
994         {9,8},  {9,8},  {8,8},  {8,8} };
995
996     if( GetBits( &p_vpar->bit_stream, 1 ) )
997     {
998         return 0;
999     }
1000     if( (i_code = ShowBits( &p_vpar->bit_stream, 9) ) >= 64 )
1001     {
1002         i_code >>= 6;
1003         RemoveBits( &p_vpar->bit_stream, pl_mv_tab0[i_code].i_length );
1004         return( GetBits( &p_vpar->bit_stream, 1 ) ?
1005             -pl_mv_tab0[i_code].i_value : pl_mv_tab0[i_code].i_value );
1006     }
1007
1008     if( i_code >= 24 )
1009     {
1010         i_code >>= 3;
1011         RemoveBits( &p_vpar->bit_stream, pl_mv_tab1[i_code].i_length );
1012         return( GetBits( &p_vpar->bit_stream, 1 ) ?
1013             -pl_mv_tab1[i_code].i_value : pl_mv_tab1[i_code].i_value );
1014     }
1015
1016     if( (i_code -= 12) < 0 )
1017     {
1018         p_vpar->picture.b_error = 1;
1019         intf_DbgMsg( "vpar debug: Invalid motion_vector code\n" );
1020         return 0;
1021     }
1022
1023     RemoveBits( &p_vpar->bit_stream, pl_mv_tab2[i_code].i_length );
1024     return( GetBits( &p_vpar->bit_stream, 1 ) ?
1025         -pl_mv_tab2[i_code].i_value : pl_mv_tab2[i_code].i_value );
1026 }
1027
1028 /****************************************************************************
1029  * DecodeMotionVector : Decode a motion_vector
1030  ****************************************************************************/
1031 static __inline__ void DecodeMotionVector( int * pi_prediction, int i_r_size,
1032         int i_motion_code, int i_motion_residual, int i_full_pel )
1033 {
1034     int i_limit, i_vector;
1035
1036     /* ISO/IEC 13818-1 section 7.6.3.1 */
1037     i_limit = 16 << i_r_size;
1038     i_vector = *pi_prediction >> i_full_pel;
1039
1040     if( i_motion_code > 0 )
1041     {
1042         i_vector += ((i_motion_code-1) << i_r_size) + i_motion_residual + 1;
1043         if( i_vector >= i_limit )
1044             i_vector -= i_limit + i_limit;
1045     }
1046     else if( i_motion_code < 0 )
1047     {
1048         i_vector -= ((-i_motion_code-1) << i_r_size) + i_motion_residual + 1;
1049         if( i_vector < -i_limit )
1050             i_vector += i_limit + i_limit;
1051     }
1052     *pi_prediction = i_vector << i_full_pel;
1053 }
1054
1055 /****************************************************************************
1056  * MotionVector : Parse the next motion_vector field
1057  ****************************************************************************/
1058 static __inline__ void MotionVector( vpar_thread_t * p_vpar,
1059                                      macroblock_t * p_mb, int i_r,
1060                                      int i_s, int i_full_pel, int i_structure )
1061 {
1062     int i_motion_code, i_motion_residual;
1063     int i_r_size;
1064     int pi_dm_vector[2];
1065
1066     i_r_size = p_vpar->picture.ppi_f_code[i_s][0] - 1;
1067     i_motion_code = MotionCode( p_vpar );
1068     i_motion_residual = (i_r_size != 0 && i_motion_code != 0) ?
1069                         GetBits( &p_vpar->bit_stream, i_r_size) : 0;
1070     DecodeMotionVector( &p_vpar->mb.pppi_pmv[i_r][i_s][0], i_r_size,
1071                         i_motion_code, i_motion_residual, i_full_pel );
1072     p_mb->pppi_motion_vectors[i_r][i_s][0] = p_vpar->mb.pppi_pmv[i_r][i_s][0];
1073
1074     if( p_vpar->mb.b_dmv )
1075     {
1076         if( GetBits(&p_vpar->bit_stream, 1) )
1077         {
1078             pi_dm_vector[0] = GetBits( &p_vpar->bit_stream, 1 ) ? -1 : 1;
1079         }
1080         else
1081         {
1082             pi_dm_vector[0] = 0;
1083         }
1084     }
1085
1086     i_r_size = p_vpar->picture.ppi_f_code[i_s][1]-1;
1087     i_motion_code = MotionCode( p_vpar );
1088     i_motion_residual = (i_r_size != 0 && i_motion_code != 0) ?
1089                         GetBits( &p_vpar->bit_stream, i_r_size) : 0;
1090
1091
1092     if( (p_vpar->mb.i_mv_format == MOTION_FIELD)
1093         && (i_structure == FRAME_STRUCTURE) )
1094     {
1095          p_vpar->mb.pppi_pmv[i_r][i_s][1] >>= 1;
1096     }
1097
1098     DecodeMotionVector( &p_vpar->mb.pppi_pmv[i_r][i_s][1], i_r_size,
1099                         i_motion_code, i_motion_residual, i_full_pel );
1100
1101     if( (p_vpar->mb.i_mv_format == MOTION_FIELD)
1102         && (i_structure == FRAME_STRUCTURE) )
1103          p_vpar->mb.pppi_pmv[i_r][i_s][1] <<= 1;
1104
1105     p_mb->pppi_motion_vectors[i_r][i_s][1] = p_vpar->mb.pppi_pmv[i_r][i_s][1];
1106
1107     if( p_vpar->mb.b_dmv )
1108     {
1109         if( GetBits(&p_vpar->bit_stream, 1) )
1110         {
1111             pi_dm_vector[1] = GetBits( &p_vpar->bit_stream, 1 ) ? -1 : 1;
1112         }
1113         else
1114         {
1115             pi_dm_vector[1] = 0;
1116         }
1117
1118         /* Dual Prime Arithmetic (ISO/IEC 13818-2 section 7.6.3.6). */
1119
1120 #define i_mv_x  p_mb->pppi_motion_vectors[0][0][0]
1121         if( i_structure == FRAME_STRUCTURE )
1122         {
1123 #define i_mv_y  (p_mb->pppi_motion_vectors[0][0][1] << 1)
1124             if( p_vpar->picture.b_top_field_first )
1125             {
1126                 /* vector for prediction of top field from bottom field */
1127                 p_mb->ppi_dmv[0][0] = ((i_mv_x + (i_mv_x > 0)) >> 1) + pi_dm_vector[0];
1128                 p_mb->ppi_dmv[0][1] = ((i_mv_y + (i_mv_y > 0)) >> 1) + pi_dm_vector[1] - 1;
1129
1130                 /* vector for prediction of bottom field from top field */
1131                 p_mb->ppi_dmv[1][0] = ((3*i_mv_x + (i_mv_x > 0)) >> 1) + pi_dm_vector[0];
1132                 p_mb->ppi_dmv[1][1] = ((3*i_mv_y + (i_mv_y > 0)) >> 1) + pi_dm_vector[1] + 1;
1133             }
1134             else
1135             {
1136                 /* vector for prediction of top field from bottom field */
1137                 p_mb->ppi_dmv[0][0] = ((3*i_mv_x + (i_mv_x > 0)) >> 1) + pi_dm_vector[0];
1138                 p_mb->ppi_dmv[0][1] = ((3*i_mv_y + (i_mv_y > 0)) >> 1) + pi_dm_vector[1] - 1;
1139
1140                 /* vector for prediction of bottom field from top field */
1141                 p_mb->ppi_dmv[1][0] = ((i_mv_x + (i_mv_x > 0)) >> 1) + pi_dm_vector[0];
1142                 p_mb->ppi_dmv[1][1] = ((i_mv_y + (i_mv_y > 0)) >> 1) + pi_dm_vector[1] + 1;
1143             }
1144 #undef i_mv_y
1145         }
1146         else
1147         {
1148 #define i_mv_y  p_mb->pppi_motion_vectors[0][0][1]
1149             /* vector for prediction from field of opposite 'parity' */
1150             p_mb->ppi_dmv[0][0] = ((i_mv_x + (i_mv_x > 0)) >> 1) + pi_dm_vector[0];
1151             p_mb->ppi_dmv[0][1] = ((i_mv_y + (i_mv_y > 0)) >> 1) + pi_dm_vector[1];
1152
1153             /* correct for vertical field shift */
1154             if( p_vpar->picture.i_structure == TOP_FIELD )
1155                 p_mb->ppi_dmv[0][1]--;
1156             else
1157                 p_mb->ppi_dmv[0][1]++;
1158 #undef i_mv_y
1159         }
1160 #undef i_mv_x
1161     }
1162 }
1163
1164 /*****************************************************************************
1165  * DecodeMVMPEG1 : Parse the next MPEG-1 motion vectors
1166  *****************************************************************************/
1167 static __inline__ void DecodeMVMPEG1( vpar_thread_t * p_vpar,
1168                             macroblock_t * p_mb, int i_s, int i_structure )
1169 {
1170     MotionVector( p_vpar, p_mb, 0, i_s,
1171                   p_vpar->picture.pb_full_pel_vector[i_s], i_structure );
1172 }
1173
1174 /*****************************************************************************
1175  * DecodeMVMPEG2 : Parse the next MPEG-2 motion_vectors field
1176  *****************************************************************************/
1177 static __inline__ void DecodeMVMPEG2( vpar_thread_t * p_vpar,
1178                             macroblock_t * p_mb, int i_s, int i_structure )
1179 {
1180     if( p_vpar->mb.i_mv_count == 1 )
1181     {
1182         if( p_vpar->mb.i_mv_format == MOTION_FIELD && !p_vpar->mb.b_dmv )
1183         {
1184             p_mb->ppi_field_select[0][i_s] = p_mb->ppi_field_select[1][i_s]
1185                                             = GetBits( &p_vpar->bit_stream, 1 );
1186         }
1187         MotionVector( p_vpar, p_mb, 0, i_s, 0, i_structure );
1188         p_vpar->mb.pppi_pmv[1][i_s][0] = p_vpar->mb.pppi_pmv[0][i_s][0];
1189         p_vpar->mb.pppi_pmv[1][i_s][1] = p_vpar->mb.pppi_pmv[0][i_s][1];
1190         p_mb->pppi_motion_vectors[1][i_s][0] = p_vpar->mb.pppi_pmv[0][i_s][0];
1191         p_mb->pppi_motion_vectors[1][i_s][1] = p_vpar->mb.pppi_pmv[0][i_s][1];
1192     }
1193     else
1194     {
1195         p_mb->ppi_field_select[0][i_s] = GetBits( &p_vpar->bit_stream, 1 );
1196         MotionVector( p_vpar, p_mb, 0, i_s, 0, i_structure );
1197         p_mb->ppi_field_select[1][i_s] = GetBits( &p_vpar->bit_stream, 1 );
1198         MotionVector( p_vpar, p_mb, 1, i_s, 0, i_structure );
1199     }
1200 }
1201
1202
1203 /*
1204  * Macroblock information structures
1205  */
1206
1207 /*****************************************************************************
1208  * MacroblockAddressIncrement : Get the macroblock_address_increment field
1209  *****************************************************************************/
1210 static __inline__ int MacroblockAddressIncrement( vpar_thread_t * p_vpar )
1211 {
1212     int i_addr_inc = 0;
1213     /* Index in the lookup table mb_addr_inc */
1214     int    i_index = ShowBits( &p_vpar->bit_stream, 11 );
1215
1216     /* Test the presence of the escape character */
1217     while( i_index == 8 )
1218     {
1219         RemoveBits( &p_vpar->bit_stream, 11 );
1220         i_addr_inc += 33;
1221         i_index = ShowBits( &p_vpar->bit_stream, 11 );
1222     }
1223
1224     /* Affect the value from the lookup table */
1225     i_addr_inc += p_vpar->pl_mb_addr_inc[i_index].i_value;
1226
1227     /* Dump the good number of bits */
1228     RemoveBits( &p_vpar->bit_stream, p_vpar->pl_mb_addr_inc[i_index].i_length );
1229
1230     return i_addr_inc;
1231 }
1232
1233 /*****************************************************************************
1234  * IMBType : macroblock_type in I pictures
1235  *****************************************************************************/
1236 static __inline__ int IMBType( vpar_thread_t * p_vpar )
1237 {
1238     /* Take two bits for testing */
1239     int                 i_type = ShowBits( &p_vpar->bit_stream, 2 );
1240
1241     /* Lookup table for macroblock_type */
1242     static lookup_t     pl_mb_Itype[4] = { {MB_ERROR, 0},
1243                                            {MB_QUANT|MB_INTRA, 2},
1244                                            {MB_INTRA, 1},
1245                                            {MB_INTRA, 1} };
1246     /* Dump the good number of bits */
1247     RemoveBits( &p_vpar->bit_stream, pl_mb_Itype[i_type].i_length );
1248     return pl_mb_Itype[i_type].i_value;
1249 }
1250
1251 /*****************************************************************************
1252  * PMBType : macroblock_type in P pictures
1253  *****************************************************************************/
1254 static __inline__ int PMBType( vpar_thread_t * p_vpar )
1255 {
1256     /* Testing on 6 bits */
1257     int                i_type = ShowBits( &p_vpar->bit_stream, 6 );
1258
1259     /* Dump the good number of bits */
1260     RemoveBits( &p_vpar->bit_stream, p_vpar->ppl_mb_type[0][i_type].i_length );
1261     /* return the value from the lookup table for P type */
1262     return p_vpar->ppl_mb_type[0][i_type].i_value;
1263 }
1264
1265 /*****************************************************************************
1266  * BMBType : macroblock_type in B pictures
1267  *****************************************************************************/
1268 static __inline__ int BMBType( vpar_thread_t * p_vpar )
1269 {
1270      /* Testing on 6 bits */
1271     int                i_type = ShowBits( &p_vpar->bit_stream, 6 );
1272
1273     /* Dump the good number of bits */
1274     RemoveBits( &p_vpar->bit_stream, p_vpar->ppl_mb_type[1][i_type].i_length );
1275
1276     /* return the value from the lookup table for B type */
1277     return p_vpar->ppl_mb_type[1][i_type].i_value;
1278 }
1279
1280 /*****************************************************************************
1281  * DMBType : macroblock_type in D pictures
1282  *****************************************************************************/
1283 static __inline__ int DMBType( vpar_thread_t * p_vpar )
1284 {
1285     return GetBits( &p_vpar->bit_stream, 1 );
1286 }
1287
1288 /*****************************************************************************
1289  * CodedPattern420 : coded_block_pattern with 4:2:0 chroma
1290  *****************************************************************************/
1291 static __inline__ int CodedPattern420( vpar_thread_t * p_vpar )
1292 {
1293     /* Take the max 9 bits length vlc code for testing */
1294     int      i_vlc = ShowBits( &p_vpar->bit_stream, 9 );
1295
1296     /* Trash the good number of bits read in the lookup table */
1297     RemoveBits( &p_vpar->bit_stream, pl_coded_pattern[i_vlc].i_length );
1298
1299     /* return the value from the vlc table */
1300     return pl_coded_pattern[i_vlc].i_value;
1301 }
1302
1303 /*****************************************************************************
1304  * CodedPattern422 : coded_block_pattern with 4:2:2 chroma
1305  *****************************************************************************/
1306 static __inline__ int CodedPattern422( vpar_thread_t * p_vpar )
1307 {
1308     int      i_vlc = ShowBits( &p_vpar->bit_stream, 9 );
1309
1310     RemoveBits( &p_vpar->bit_stream, pl_coded_pattern[i_vlc].i_length );
1311
1312     /* Supplementary 2 bits long code for 4:2:2 format */
1313     return pl_coded_pattern[i_vlc].i_value |
1314            (GetBits( &p_vpar->bit_stream, 2 ) << 6);
1315 }
1316
1317 /*****************************************************************************
1318  * CodedPattern444 : coded_block_pattern with 4:4:4 chroma
1319  *****************************************************************************/
1320 static __inline__ int CodedPattern444( vpar_thread_t * p_vpar )
1321 {
1322     int      i_vlc = ShowBits( &p_vpar->bit_stream, 9 );
1323
1324     RemoveBits( &p_vpar->bit_stream, pl_coded_pattern[i_vlc].i_length );
1325
1326     return pl_coded_pattern[i_vlc].i_value |
1327            (GetBits( &p_vpar->bit_stream, 6 ) << 6);
1328 }
1329
1330 /*****************************************************************************
1331  * InitMacroblock : Initialize macroblock values
1332  *****************************************************************************/
1333 static __inline__ void InitMacroblock( vpar_thread_t * p_vpar,
1334                                        macroblock_t * p_mb, int i_coding_type,
1335                                        int i_chroma_format,
1336                                        int i_structure,
1337                                        boolean_t b_second_field )
1338 {
1339     p_mb->i_chroma_nb_blocks = 1 << i_chroma_format;
1340     p_mb->p_picture = p_vpar->picture.p_picture;
1341
1342     if( i_coding_type == B_CODING_TYPE )
1343         p_mb->p_backward = p_vpar->sequence.p_backward;
1344     else
1345         p_mb->p_backward = NULL;
1346     if( (i_coding_type == P_CODING_TYPE) || (i_coding_type == B_CODING_TYPE) )
1347         p_mb->p_forward = p_vpar->sequence.p_forward;
1348     else
1349         p_mb->p_forward = NULL;
1350
1351     p_mb->i_l_x = p_vpar->mb.i_l_x;
1352     p_mb->i_c_x = p_vpar->mb.i_c_x;
1353     p_mb->i_motion_l_y = p_vpar->mb.i_l_y;
1354     p_mb->i_motion_c_y = p_vpar->mb.i_c_y;
1355     if( (p_mb->b_motion_field = (i_structure == BOTTOM_FIELD)) )
1356     {
1357         p_mb->i_motion_l_y--;
1358         p_mb->i_motion_c_y--;
1359     }
1360     p_mb->i_addb_l_stride = (p_mb->i_l_stride = p_vpar->picture.i_l_stride) - 8;
1361     p_mb->i_addb_c_stride = (p_mb->i_c_stride = p_vpar->picture.i_c_stride) - 8;
1362     p_mb->b_P_second = ( b_second_field && i_coding_type == P_CODING_TYPE );
1363 }
1364
1365 /*****************************************************************************
1366  * UpdateContext : Update the p_vpar contextual values
1367  *****************************************************************************/
1368 static __inline__ void UpdateContext( vpar_thread_t * p_vpar, int i_structure )
1369 {
1370     /* Update macroblock real position. */
1371     p_vpar->mb.i_l_x += 16;
1372     p_vpar->mb.i_l_y += (p_vpar->mb.i_l_x / p_vpar->sequence.i_width)
1373                         * (2 - (i_structure == FRAME_STRUCTURE)) * 16;
1374     p_vpar->mb.i_l_x %= p_vpar->sequence.i_width;
1375
1376     p_vpar->mb.i_c_x += p_vpar->sequence.i_chroma_mb_width;
1377     p_vpar->mb.i_c_y += (p_vpar->mb.i_c_x / p_vpar->sequence.i_chroma_width)
1378                         * (2 - (i_structure == FRAME_STRUCTURE))
1379                         * p_vpar->sequence.i_chroma_mb_height;
1380     p_vpar->mb.i_c_x %= p_vpar->sequence.i_chroma_width;
1381 }
1382
1383 /*****************************************************************************
1384  * SkippedMacroblock : Generate a skipped macroblock with NULL motion vector
1385  *****************************************************************************/
1386 static __inline__ void SkippedMacroblock( vpar_thread_t * p_vpar, int i_mb,
1387                                           int i_mb_base, int i_coding_type,
1388                                           int i_chroma_format,
1389                                           int i_structure,
1390                                           boolean_t b_second_field )
1391 {
1392     macroblock_t *  p_mb;
1393
1394     static f_motion_t   pf_motion_skipped[4][4] =
1395     {
1396         {NULL, NULL, NULL, NULL},
1397         {NULL, vdec_MotionFieldField420, vdec_MotionFieldField420,
1398             vdec_MotionFrameFrame420},
1399         {NULL, vdec_MotionFieldField422, vdec_MotionFieldField422,
1400             vdec_MotionFrameFrame422},
1401         {NULL, vdec_MotionFieldField444, vdec_MotionFieldField444,
1402             vdec_MotionFrameFrame444},
1403     };
1404
1405     if( i_coding_type == I_CODING_TYPE )
1406     {
1407         intf_DbgMsg("vpar error: skipped macroblock in I-picture\n");
1408         p_vpar->picture.b_error = 1;
1409         return;
1410     }
1411
1412     if( (p_mb = vpar_NewMacroblock( &p_vpar->vfifo )) == NULL )
1413     {
1414         /* b_die == 1 */
1415         return;
1416     }
1417 #ifdef VDEC_SMP
1418     p_vpar->picture.pp_mb[i_mb_base + i_mb] = p_mb;
1419 #endif
1420
1421     InitMacroblock( p_vpar, p_mb, i_coding_type, i_chroma_format,
1422                     i_structure, b_second_field );
1423
1424     /* Motion type is picture structure. */
1425     p_mb->pf_motion = pf_motion_skipped[i_chroma_format]
1426                                        [i_structure];
1427     p_mb->i_mb_type = MB_MOTION_FORWARD;
1428     p_mb->i_coded_block_pattern = 0;
1429     memset( p_mb->pppi_motion_vectors, 0, 8*sizeof(int) );
1430
1431     /* Set the field we use for motion compensation */
1432     p_mb->ppi_field_select[0][0] = p_mb->ppi_field_select[0][1]
1433                                  = ( i_structure == BOTTOM_FIELD );
1434
1435     UpdateContext( p_vpar, i_structure );
1436
1437 #ifndef VDEC_SMP
1438     /* Decode the macroblock NOW ! */
1439     vdec_DecodeMacroblock( p_vpar->pp_vdec[0], p_mb );
1440 #endif
1441 }
1442
1443 /*****************************************************************************
1444  * MacroblockModes : Get the macroblock_modes structure
1445  *****************************************************************************/
1446 static __inline__ void MacroblockModes( vpar_thread_t * p_vpar,
1447                                         macroblock_t * p_mb,
1448                                         int i_chroma_format,
1449                                         int i_coding_type,
1450                                         int i_structure )
1451 {
1452     static int          ppi_mv_count[2][4] = { {0, 1, 2, 1}, {0, 2, 1, 1} };
1453     static int          ppi_mv_format[2][4] = { {0, 1, 1, 1}, {0, 1, 2, 1} };
1454
1455     /* Get macroblock_type. */
1456     switch( i_coding_type )
1457     {
1458     case P_CODING_TYPE:
1459         p_mb->i_mb_type = PMBType( p_vpar );
1460         break;
1461     case B_CODING_TYPE:
1462         p_mb->i_mb_type = BMBType( p_vpar );
1463         break;
1464     case I_CODING_TYPE:
1465         p_mb->i_mb_type = IMBType( p_vpar );
1466         break;
1467     case D_CODING_TYPE:
1468         p_mb->i_mb_type = DMBType( p_vpar );
1469     }
1470
1471     /* SCALABILITY : warning, we don't know if spatial_temporal_weight_code
1472      * has to be dropped, take care if you use scalable streams. */
1473     /* RemoveBits( &p_vpar->bit_stream, 2 ); */
1474
1475     if( (i_coding_type == P_CODING_TYPE || i_coding_type == B_CODING_TYPE)
1476         && (p_mb->i_mb_type & (MB_MOTION_FORWARD | MB_MOTION_BACKWARD)) )
1477     {
1478         if( !(i_structure == FRAME_STRUCTURE
1479                && p_vpar->picture.b_frame_pred_frame_dct) )
1480         {
1481             p_vpar->mb.i_motion_type = GetBits( &p_vpar->bit_stream, 2 );
1482         }
1483         else
1484         {
1485             p_vpar->mb.i_motion_type = MOTION_FRAME;
1486         }
1487
1488         /* ???? */
1489         p_vpar->mb.i_mv_count = ppi_mv_count[i_structure == FRAME_STRUCTURE]
1490                                             [p_vpar->mb.i_motion_type];
1491         p_vpar->mb.i_mv_format = ppi_mv_format[i_structure == FRAME_STRUCTURE]
1492                                               [p_vpar->mb.i_motion_type];
1493         p_vpar->mb.b_dmv = p_vpar->mb.i_motion_type == MOTION_DMV;
1494     }
1495
1496     p_vpar->mb.b_dct_type = 0;
1497     if( (i_structure == FRAME_STRUCTURE) &&
1498         (!p_vpar->picture.b_frame_pred_frame_dct) &&
1499         (p_mb->i_mb_type & (MB_PATTERN|MB_INTRA)) )
1500     {
1501         if( (p_vpar->mb.b_dct_type = GetBits( &p_vpar->bit_stream, 1 )) )
1502         {
1503             /* The DCT is coded on fields. Jump one line between each
1504              * sample. */
1505             p_mb->i_addb_l_stride <<= 1;
1506             p_mb->i_addb_l_stride += 8;
1507             /* With CHROMA_420, the DCT is necessarily frame-coded. */
1508             if( i_chroma_format != CHROMA_420 )
1509             {
1510                 p_mb->i_addb_c_stride <<= 1;
1511                 p_mb->i_addb_c_stride += 8;
1512             }
1513         }
1514     }
1515 }
1516
1517 /*****************************************************************************
1518  * ParseMacroblock : Parse the next macroblock
1519  *****************************************************************************/
1520 #define PARSEERROR                                                      \
1521 if( p_vpar->picture.b_error )                                           \
1522 {                                                                       \
1523     /* Mark this block as skipped (better than green blocks), and       \
1524      * go to the next slice. */                                         \
1525     (*pi_mb_address)--;                                                 \
1526     vpar_DestroyMacroblock( &p_vpar->vfifo, p_mb );                     \
1527     return;                                                             \
1528 }
1529
1530
1531 #define PARSEBLOCKS( MPEG1FUNC, MPEG2FUNC )                             \
1532 {                                                                       \
1533     i_mask = 1 << (3 + (1 << i_chroma_format));                         \
1534                                                                         \
1535     /* luminance */                                                     \
1536     p_data1 = p_mb->p_picture->p_y                                      \
1537         + p_mb->i_l_x + p_vpar->mb.i_l_y*(p_vpar->sequence.i_width);    \
1538                                                                         \
1539     for( i_b = 0 ; i_b < 4 ; i_b++, i_mask >>= 1 )                      \
1540     {                                                                   \
1541         if( p_mb->i_coded_block_pattern & i_mask )                      \
1542         {                                                               \
1543             memset( p_mb->ppi_blocks[i_b], 0, 64*sizeof(dctelem_t) );   \
1544             if( b_mpeg2 )                                               \
1545                 MPEG2FUNC( p_vpar, p_mb, i_b, i_chroma_format );        \
1546             else                                                        \
1547                 MPEG1FUNC( p_vpar, p_mb, i_b, i_chroma_format );        \
1548                                                                         \
1549             /* Calculate block coordinates. */                          \
1550             p_mb->p_data[i_b] = p_data1                                 \
1551                                 + pi_y[p_vpar->mb.b_dct_type][i_b]      \
1552                                 * p_vpar->sequence.i_width              \
1553                                 + pi_x[i_b];                            \
1554                                                                         \
1555             PARSEERROR                                                  \
1556         }                                                               \
1557     }                                                                   \
1558                                                                         \
1559     /* chrominance */                                                   \
1560     p_data1 = p_mb->p_picture->p_u                                      \
1561               + p_mb->i_c_x                                             \
1562               + p_vpar->mb.i_c_y                                        \
1563                 * (p_vpar->sequence.i_chroma_width);                    \
1564     p_data2 = p_mb->p_picture->p_v                                      \
1565                + p_mb->i_c_x                                            \
1566                + p_vpar->mb.i_c_y                                       \
1567                 * (p_vpar->sequence.i_chroma_width);                    \
1568                                                                         \
1569     for( i_b = 4; i_b < 4 + (1 << i_chroma_format);                     \
1570          i_b++, i_mask >>= 1 )                                          \
1571     {                                                                   \
1572         yuv_data_t *    pp_data[2] = {p_data1, p_data2};                \
1573                                                                         \
1574         if( p_mb->i_coded_block_pattern & i_mask )                      \
1575         {                                                               \
1576             memset( p_mb->ppi_blocks[i_b], 0, 64*sizeof(dctelem_t) );   \
1577             if( b_mpeg2 )                                               \
1578                 MPEG2FUNC( p_vpar, p_mb, i_b, i_chroma_format );        \
1579             else                                                        \
1580                 MPEG1FUNC( p_vpar, p_mb, i_b, i_chroma_format );        \
1581                                                                         \
1582             /* Calculate block coordinates. */                          \
1583             p_mb->p_data[i_b] = pp_data[i_b & 1]                        \
1584                                  + pi_y[p_vpar->mb.b_dct_type][i_b]     \
1585                                    * p_vpar->sequence.i_chroma_width    \
1586                                  + pi_x[i_b];                           \
1587                                                                         \
1588             PARSEERROR                                                  \
1589         }                                                               \
1590     }                                                                   \
1591 }
1592
1593 static __inline__ void ParseMacroblock(
1594                            vpar_thread_t * p_vpar,
1595                            int * pi_mb_address,     /* previous address to be
1596                                                      * used for mb_addr_incr */
1597                            int i_mb_previous,          /* actual previous mb */
1598                            int i_mb_base,     /* non-zero if field structure */
1599                            /* The following parameters are explicit in
1600                             * optimized routines : */
1601                            boolean_t b_mpeg2,             /* you know what ? */
1602                            int i_coding_type,                /* I, P, B or D */
1603                            int i_chroma_format,     /* 4:2:0, 4:2:2 or 4:4:4 */
1604                            int i_structure,    /* T(OP), B(OTTOM) or F(RAME) */
1605                            boolean_t b_second_field )     /* second field of a
1606                                                            * field picture   */
1607 {
1608     static f_motion_t   pppf_motion[4][2][4] =
1609       {
1610         { {NULL, NULL, NULL, NULL},
1611           {NULL, NULL, NULL, NULL}
1612         },
1613         { {NULL, vdec_MotionFieldField420, vdec_MotionField16x8420,
1614             vdec_MotionFieldDMV420},
1615           {NULL, vdec_MotionFrameField420, vdec_MotionFrameFrame420,
1616             vdec_MotionFrameDMV420}
1617         },
1618         { {NULL, vdec_MotionFieldField422, vdec_MotionField16x8422,
1619             vdec_MotionFieldDMV422},
1620           {NULL, vdec_MotionFrameField422, vdec_MotionFrameFrame422,
1621             vdec_MotionFrameDMV422}
1622         },
1623         { {NULL, vdec_MotionFieldField444, vdec_MotionField16x8444,
1624             vdec_MotionFieldDMV444},
1625           {NULL, vdec_MotionFrameField444, vdec_MotionFrameFrame444,
1626             vdec_MotionFrameDMV444}
1627         }
1628       };
1629     static int      pi_x[12] = {0,8,0,8,0,0,0,0,8,8,8,8};
1630     static int      pi_y[2][12] = { {0,0,8,8,0,0,8,8,0,0,8,8},
1631                                     {0,0,1,1,0,0,1,1,0,0,1,1} };
1632     int             i_mb, i_b, i_mask;
1633     int i_inc;
1634     macroblock_t *  p_mb;
1635     yuv_data_t *    p_data1;
1636     yuv_data_t *    p_data2;
1637
1638 i_count++;
1639
1640     i_inc = MacroblockAddressIncrement( p_vpar );
1641     *pi_mb_address += i_inc;
1642
1643     if( i_inc < 0 )
1644     {
1645         fprintf( stderr, "vpar error: bad address increment (%d)\n", i_inc );
1646         p_vpar->picture.b_error = 1;
1647         return;
1648     }
1649     
1650     if( *pi_mb_address - i_mb_previous - 1 )
1651     {
1652         /* Skipped macroblock (ISO/IEC 13818-2 7.6.6). */
1653
1654         /* Reset DC predictors (7.2.1). */
1655         p_vpar->mb.pi_dc_dct_pred[0] = p_vpar->mb.pi_dc_dct_pred[1]
1656             = p_vpar->mb.pi_dc_dct_pred[2]
1657             = 1 << (7 + p_vpar->picture.i_intra_dc_precision);
1658
1659         if( i_coding_type == P_CODING_TYPE )
1660         {
1661             /* Reset motion vector predictors (ISO/IEC 13818-2 7.6.3.4). */
1662             memset( p_vpar->mb.pppi_pmv, 0, 8*sizeof(int) );
1663         }
1664
1665         for( i_mb = i_mb_previous + 1; i_mb < *pi_mb_address; i_mb++ )
1666         {
1667             SkippedMacroblock( p_vpar, i_mb, i_mb_base, i_coding_type,
1668                                i_chroma_format, i_structure, b_second_field );
1669         }
1670     }
1671
1672     /* Get a macroblock structure. */
1673     if( (p_mb = vpar_NewMacroblock( &p_vpar->vfifo )) == NULL )
1674     {
1675         /* b_die == 1 */
1676         return;
1677     }
1678 #ifdef VDEC_SMP
1679     p_vpar->picture.pp_mb[i_mb_base + *pi_mb_address] = p_mb;
1680 #endif
1681
1682     InitMacroblock( p_vpar, p_mb, i_coding_type, i_chroma_format,
1683                     i_structure, b_second_field );
1684
1685     /* Parse off macroblock_modes structure. */
1686     MacroblockModes( p_vpar, p_mb, i_chroma_format, i_coding_type,
1687                      i_structure );
1688
1689     if( p_mb->i_mb_type & MB_QUANT )
1690     {
1691         LoadQuantizerScale( p_vpar );
1692     }
1693
1694     if( (i_coding_type == P_CODING_TYPE || i_coding_type == B_CODING_TYPE)
1695          && (p_mb->i_mb_type & MB_MOTION_FORWARD) )
1696     {
1697         if( b_mpeg2 )
1698             DecodeMVMPEG2( p_vpar, p_mb, 0, i_structure );
1699         else
1700             DecodeMVMPEG1( p_vpar, p_mb, 0, i_structure );
1701         PARSEERROR
1702     }
1703
1704     if( (i_coding_type == B_CODING_TYPE)
1705          && (p_mb->i_mb_type & MB_MOTION_BACKWARD) )
1706     {
1707         if( b_mpeg2 )
1708             DecodeMVMPEG2( p_vpar, p_mb, 1, i_structure );
1709         else
1710             DecodeMVMPEG1( p_vpar, p_mb, 1, i_structure );
1711         PARSEERROR
1712     }
1713
1714     if( i_coding_type == P_CODING_TYPE
1715          && !(p_mb->i_mb_type & (MB_MOTION_FORWARD|MB_INTRA)) )
1716     {
1717         /* Special No-MC macroblock in P pictures (7.6.3.5). */
1718         p_mb->i_mb_type |= MB_MOTION_FORWARD;
1719         memset( p_vpar->mb.pppi_pmv, 0, 8*sizeof(int) );
1720         memset( p_mb->pppi_motion_vectors, 0, 8*sizeof(int) );
1721         p_vpar->mb.i_motion_type = 1 + (i_structure == FRAME_STRUCTURE);
1722         p_mb->ppi_field_select[0][0] = (i_structure == BOTTOM_FIELD);
1723     }
1724
1725     if( (i_coding_type != I_CODING_TYPE) && !(p_mb->i_mb_type & MB_INTRA) )
1726     {
1727         /* Reset DC predictors (7.2.1). */
1728         p_vpar->mb.pi_dc_dct_pred[0] = p_vpar->mb.pi_dc_dct_pred[1]
1729             = p_vpar->mb.pi_dc_dct_pred[2]
1730             = 1 << (7 + p_vpar->picture.i_intra_dc_precision);
1731
1732         /* Motion function pointer. */
1733         p_mb->pf_motion = pppf_motion[i_chroma_format]
1734                                      [i_structure == FRAME_STRUCTURE]
1735                                      [p_vpar->mb.i_motion_type];
1736
1737         if( p_mb->i_mb_type & MB_PATTERN )
1738         {
1739             switch( i_chroma_format )
1740             {
1741             case CHROMA_420:
1742                 p_mb->i_coded_block_pattern = CodedPattern420( p_vpar );
1743                 break;
1744             case CHROMA_422:
1745                 p_mb->i_coded_block_pattern = CodedPattern422( p_vpar );
1746                 break;
1747             case CHROMA_444:
1748                 p_mb->i_coded_block_pattern = CodedPattern444( p_vpar );
1749             }
1750         }
1751         else
1752         {
1753             p_mb->i_coded_block_pattern = 0;
1754         }
1755
1756         /*
1757          * Effectively decode blocks.
1758          */
1759         PARSEBLOCKS( DecodeMPEG1NonIntra, DecodeMPEG2NonIntra )
1760     }
1761     else
1762     {
1763         if( !p_vpar->picture.b_concealment_mv )
1764         {
1765             /* Reset MV predictors. */
1766             memset( p_vpar->mb.pppi_pmv, 0, 8*sizeof(int) );
1767         }
1768         else
1769         {
1770             if( b_mpeg2 )
1771                 DecodeMVMPEG2( p_vpar, p_mb, 0, i_structure );
1772             else
1773                 DecodeMVMPEG1( p_vpar, p_mb, 0, i_structure );
1774             RemoveBits( &p_vpar->bit_stream, 1 );
1775         }
1776
1777         if( p_mb->i_mb_type & MB_PATTERN )
1778         {
1779             switch( i_chroma_format )
1780             {
1781             case CHROMA_420:
1782                 p_mb->i_coded_block_pattern = CodedPattern420( p_vpar );
1783                 break;
1784             case CHROMA_422:
1785                 p_mb->i_coded_block_pattern = CodedPattern422( p_vpar );
1786                 break;
1787             case CHROMA_444:
1788                 p_mb->i_coded_block_pattern = CodedPattern444( p_vpar );
1789             }
1790         }
1791         else
1792         {
1793             p_mb->i_coded_block_pattern =
1794                                 (1 << (4 + (1 << i_chroma_format))) - 1;
1795         }
1796
1797         /*
1798          * Effectively decode blocks.
1799          */
1800         PARSEBLOCKS( DecodeMPEG1Intra, DecodeMPEG2Intra )
1801     }
1802
1803     if( !p_vpar->picture.b_error )
1804     {
1805         UpdateContext( p_vpar, i_structure );
1806 #ifndef VDEC_SMP
1807         /* Decode the macroblock NOW ! */
1808         vdec_DecodeMacroblock( p_vpar->pp_vdec[0], p_mb );
1809 #endif
1810     }
1811     else
1812     {
1813         /* Mark this block as skipped (better than green blocks), and go
1814          * to the next slice. */
1815         (*pi_mb_address)--;
1816         vpar_DestroyMacroblock( &p_vpar->vfifo, p_mb );
1817     }
1818 }
1819
1820 /*
1821  * Picture data parsing management
1822  */
1823
1824 /*****************************************************************************
1825  * SliceHeader : Parse the next slice structure
1826  *****************************************************************************/
1827 static __inline__ void SliceHeader( vpar_thread_t * p_vpar,
1828                                     int * pi_mb_address, int i_mb_base,
1829                                     u32 i_vert_code, boolean_t b_high,
1830                                     boolean_t b_dp_scalable,
1831                                     boolean_t b_mpeg2, int i_coding_type,
1832                                     int i_chroma_format, int i_structure,
1833                                     boolean_t b_second_field )
1834 {
1835     int                     i_mb_address_save = *pi_mb_address;
1836
1837     p_vpar->picture.b_error = 0;
1838
1839     if( b_high )
1840     {
1841         /* Picture with more than 2800 lines. */
1842         i_vert_code += GetBits( &p_vpar->bit_stream, 3 ) << 7;
1843     }
1844     if( b_dp_scalable )
1845     {
1846         /* DATA_PARTITIONING scalability. */
1847         RemoveBits( &p_vpar->bit_stream, 7 ); /* priority_breakpoint */
1848     }
1849
1850     LoadQuantizerScale( p_vpar );
1851
1852     if( GetBits( &p_vpar->bit_stream, 1 ) )
1853     {
1854         /* intra_slice, slice_id */
1855         RemoveBits( &p_vpar->bit_stream, 8 );
1856         /* extra_information_slice */
1857         while( GetBits( &p_vpar->bit_stream, 1 ) )
1858         {
1859             RemoveBits( &p_vpar->bit_stream, 8 );
1860         }
1861     }    
1862     *pi_mb_address = (i_vert_code - 1)*p_vpar->sequence.i_mb_width;
1863
1864     if( *pi_mb_address < i_mb_address_save )
1865     {
1866         fprintf( stderr, "vpar error: slices do not follow, maybe a PES has been trashed\n" );
1867         p_vpar->picture.b_error = 1;
1868         return;
1869     }
1870
1871     /* Reset DC coefficients predictors (ISO/IEC 13818-2 7.2.1). */
1872     p_vpar->mb.pi_dc_dct_pred[0] = p_vpar->mb.pi_dc_dct_pred[1]
1873         = p_vpar->mb.pi_dc_dct_pred[2]
1874         = 1 << (7 + p_vpar->picture.i_intra_dc_precision);
1875
1876     /* Reset motion vector predictors (ISO/IEC 13818-2 7.6.3.4). */
1877     memset( p_vpar->mb.pppi_pmv, 0, 8*sizeof(int) );
1878
1879     do
1880     {
1881         if( i_mb_address_save >= p_vpar->sequence.i_mb_size )
1882         {
1883             p_vpar->picture.b_error = 1;
1884             return;            
1885         }
1886         ParseMacroblock( p_vpar, pi_mb_address, i_mb_address_save,
1887                          i_mb_base, b_mpeg2, i_coding_type,
1888                          i_chroma_format, i_structure,
1889                          b_second_field );
1890         i_mb_address_save = *pi_mb_address;
1891         if( p_vpar->picture.b_error )
1892         {
1893             return;
1894         }
1895     }
1896     while( ShowBits( &p_vpar->bit_stream, 23 ) && !p_vpar->b_die );
1897     NextStartCode( p_vpar );
1898 }
1899
1900 /*****************************************************************************
1901  * PictureData : Parse off all macroblocks (ISO/IEC 13818-2 6.2.3.7)
1902  *****************************************************************************/
1903 static __inline__ void PictureData( vpar_thread_t * p_vpar, int i_mb_base,
1904                                     boolean_t b_high, boolean_t b_dp_scalable,
1905                                     boolean_t b_mpeg2, int i_coding_type,
1906                                     int i_chroma_format, int i_structure,
1907                                     boolean_t b_second_field )
1908 {
1909     int         i_mb_address = 0;
1910     u32         i_dummy;
1911
1912     NextStartCode( p_vpar );
1913     while( ((i_coding_type != I_CODING_TYPE && i_coding_type != D_CODING_TYPE)
1914              || !p_vpar->picture.b_error)
1915            && i_mb_address+i_mb_base < p_vpar->sequence.i_mb_size
1916            && !p_vpar->b_die )
1917     {
1918         if( ((i_dummy = ShowBits( &p_vpar->bit_stream, 32 ))
1919                  < SLICE_START_CODE_MIN) ||
1920             (i_dummy > SLICE_START_CODE_MAX) )
1921         {
1922             intf_DbgMsg("vpar debug: premature end of picture\n");
1923             p_vpar->picture.b_error = 1;
1924             break;
1925         }
1926         RemoveBits32( &p_vpar->bit_stream );
1927
1928         /* Decode slice data. */
1929         SliceHeader( p_vpar, &i_mb_address, i_mb_base, i_dummy & 255,
1930                      b_high, b_dp_scalable, b_mpeg2, i_coding_type,
1931                      i_chroma_format, i_structure, b_second_field );
1932     }
1933
1934     /* Try to recover from error. If we missed less than half the
1935      * number of macroblocks of the picture, mark the missed ones
1936      * as skipped. */
1937     if( (i_coding_type == P_CODING_TYPE || i_coding_type == B_CODING_TYPE)
1938         && p_vpar->picture.b_error &&
1939         ( (i_mb_address-i_mb_base) > (p_vpar->sequence.i_mb_size >> 1)
1940            || (i_structure != FRAME_STRUCTURE
1941                && (i_mb_address-i_mb_base) > (p_vpar->sequence.i_mb_size >> 2) ) ) )
1942     {
1943         int         i_mb;
1944
1945         p_vpar->picture.b_error = 0;
1946         for( i_mb = i_mb_address + 1;
1947              i_mb < (p_vpar->sequence.i_mb_size
1948                      << (i_structure != FRAME_STRUCTURE));
1949              i_mb++ )
1950         {
1951             SkippedMacroblock( p_vpar, i_mb, i_mb_base, i_coding_type,
1952                                i_chroma_format, i_structure, b_second_field );
1953         }
1954     }
1955 }
1956
1957 /*****************************************************************************
1958  * vpar_PictureDataVWXYZ : Parse the next macroblock ; specific functions
1959  *****************************************************************************
1960  * V = MPEG2 ?
1961  * W = coding type ?
1962  * X = chroma format ?
1963  * Y = structure ?
1964  * Z = second field ?
1965  *****************************************************************************/
1966 void vpar_PictureDataGENERIC( vpar_thread_t * p_vpar, int i_mb_base )
1967 {
1968     PictureData( p_vpar, i_mb_base, (p_vpar->sequence.i_height > 2800),
1969                  (p_vpar->sequence.i_scalable_mode == SC_DP),
1970                  p_vpar->sequence.b_mpeg2, p_vpar->picture.i_coding_type,
1971                  p_vpar->sequence.i_chroma_format,
1972                  p_vpar->picture.i_structure,
1973                  (p_vpar->picture.i_structure !=
1974                     p_vpar->picture.i_current_structure) );
1975 }
1976
1977 #if (VPAR_OPTIM_LEVEL > 0)
1978 /* Optimizations for frame pictures */
1979 void vpar_PictureData2I420F0( vpar_thread_t * p_vpar, int i_mb_base )
1980 {
1981     PictureData( p_vpar, 0, 0, 0, 1, I_CODING_TYPE, CHROMA_420,
1982                  FRAME_STRUCTURE, 0 );
1983 }
1984
1985 void vpar_PictureData2P420F0( vpar_thread_t * p_vpar, int i_mb_base )
1986 {
1987     PictureData( p_vpar, 0, 0, 0, 1, P_CODING_TYPE, CHROMA_420,
1988                  FRAME_STRUCTURE, 0 );
1989 }
1990
1991 void vpar_PictureData2B420F0( vpar_thread_t * p_vpar, int i_mb_base )
1992 {
1993     PictureData( p_vpar, 0, 0, 0, 1, B_CODING_TYPE, CHROMA_420,
1994                  FRAME_STRUCTURE, 0 );
1995 }
1996 #endif
1997
1998 #if (VPAR_OPTIM_LEVEL > 1)
1999 /* Optimizations for field pictures */
2000 void vpar_PictureData2I420TZ( vpar_thread_t * p_vpar, int i_mb_base )
2001 {
2002     PictureData( p_vpar, i_mb_base, 0, 0, 1, I_CODING_TYPE, CHROMA_420,
2003                  TOP_FIELD, (p_vpar->picture.i_structure !=
2004                                 p_vpar->picture.i_current_structure) );
2005 }
2006
2007 void vpar_PictureData2P420TZ( vpar_thread_t * p_vpar, int i_mb_base )
2008 {
2009     PictureData( p_vpar, i_mb_base, 0, 0, 1, P_CODING_TYPE, CHROMA_420,
2010                  TOP_FIELD, (p_vpar->picture.i_structure !=
2011                                 p_vpar->picture.i_current_structure) );
2012 }
2013
2014 void vpar_PictureData2B420TZ( vpar_thread_t * p_vpar, int i_mb_base )
2015 {
2016     PictureData( p_vpar, i_mb_base, 0, 0, 1, B_CODING_TYPE, CHROMA_420,
2017                  TOP_FIELD, (p_vpar->picture.i_structure !=
2018                                 p_vpar->picture.i_current_structure) );
2019 }
2020
2021 void vpar_PictureData2I420BZ( vpar_thread_t * p_vpar, int i_mb_base )
2022 {
2023     PictureData( p_vpar, i_mb_base, 0, 0, 1, I_CODING_TYPE, CHROMA_420,
2024                  BOTTOM_FIELD, (p_vpar->picture.i_structure !=
2025                                 p_vpar->picture.i_current_structure) );
2026 }
2027
2028 void vpar_PictureData2P420BZ( vpar_thread_t * p_vpar, int i_mb_base )
2029 {
2030     PictureData( p_vpar, i_mb_base, 0, 0, 1, P_CODING_TYPE, CHROMA_420,
2031                  BOTTOM_FIELD, (p_vpar->picture.i_structure !=
2032                                 p_vpar->picture.i_current_structure) );
2033 }
2034
2035 void vpar_PictureData2B420BZ( vpar_thread_t * p_vpar, int i_mb_base )
2036 {
2037     PictureData( p_vpar, i_mb_base, 0, 0, 1, B_CODING_TYPE, CHROMA_420,
2038                  BOTTOM_FIELD, (p_vpar->picture.i_structure !=
2039                                 p_vpar->picture.i_current_structure) );
2040 }
2041 #endif