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