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