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