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