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