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