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