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