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