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