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