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