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