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