]> git.sesse.net Git - vlc/blob - src/video_parser/vpar_blocks.c
* Ajout d'un flag b_mpeg2 qui manquait dans le parser
[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 #include <X11/Xlib.h>
16 #include <X11/extensions/XShm.h>
17
18 #include "config.h"
19 #include "common.h"
20 #include "mtime.h"
21 #include "vlc_thread.h"
22
23 #include "intf_msg.h"
24 #include "debug.h"                    /* ?? temporaire, requis par netlist.h */
25
26 #include "input.h"
27 #include "input_netlist.h"
28 #include "decoder_fifo.h"
29 #include "video.h"
30 #include "video_output.h"
31
32 #include "vdec_idct.h"
33 #include "video_decoder.h"
34 #include "vdec_motion.h"
35
36 #include "vpar_blocks.h"
37 #include "vpar_headers.h"
38 #include "video_fifo.h"
39 #include "vpar_synchro.h"
40 #include "video_parser.h"
41
42 /*
43  * Local prototypes
44  */
45 static __inline__ void InitMacroblock( vpar_thread_t * p_vpar,
46                                        macroblock_t * p_mb );
47 static __inline__ int MacroblockAddressIncrement( vpar_thread_t * p_vpar );
48 static __inline__ void MacroblockModes( vpar_thread_t * p_vpar,
49                                         macroblock_t * p_mb );
50 typedef void (*f_decode_block_t)( vpar_thread_t *, macroblock_t *, int );
51 static void vpar_DecodeMPEG1Non( vpar_thread_t * p_vpar, macroblock_t * p_mb, int i_b );
52 static void vpar_DecodeMPEG1Intra( vpar_thread_t * p_vpar, macroblock_t * p_mb, int i_b );
53 static void vpar_DecodeMPEG2Non( vpar_thread_t * p_vpar, macroblock_t * p_mb, int i_b );
54 static void vpar_DecodeMPEG2Intra( vpar_thread_t * p_vpar, macroblock_t * p_mb, int i_b );
55
56 /*
57  * Initialisation tables
58  */
59     /* Table for coded_block_pattern resolution */
60 lookup_t     pl_coded_pattern_init_table[512] = 
61     { {MB_ERROR, 0}, {0, 9}, {39, 9}, {27, 9}, {59, 9}, {55, 9}, {47, 9}, {31, 9},
62     {58, 8}, {58, 8}, {54, 8}, {54, 8}, {46, 8}, {46, 8}, {30, 8}, {30, 8},
63     {57, 8}, {57, 8}, {53, 8}, {53, 8}, {45, 8}, {45, 8}, {29, 8}, {29, 8},
64     {38, 8}, {38, 8}, {26, 8}, {26, 8}, {37, 8}, {37, 8}, {25, 8}, {25, 8},
65     {43, 8}, {43, 8}, {23, 8}, {23, 8}, {51, 8}, {51, 8}, {15, 8}, {15, 8},
66     {42, 8}, {42, 8}, {22, 8}, {22, 8}, {50, 8}, {50, 8}, {14, 8}, {14, 8},
67     {41, 8}, {41, 8}, {21, 8}, {21, 8}, {49, 8}, {49, 8}, {13, 8}, {13, 8},
68     {35, 8}, {35, 8}, {19, 8}, {19, 8}, {11, 8}, {11, 8}, {7, 8}, {7, 8},
69     {34, 7}, {34, 7}, {34, 7}, {34, 7}, {18, 7}, {18, 7}, {18, 7}, {18, 7},
70     {10, 7}, {10, 7}, {10, 7}, {10, 7}, {6, 7}, {6, 7}, {6, 7}, {6, 7}, 
71     {33, 7}, {33, 7}, {33, 7}, {33, 7}, {17, 7}, {17, 7}, {17, 7}, {17, 7}, 
72     {9, 7}, {9, 7}, {9, 7}, {9, 7}, {5, 7}, {5, 7}, {5, 7}, {5, 7}, 
73     {63, 6}, {63, 6}, {63, 6}, {63, 6}, {63, 6}, {63, 6}, {63, 6}, {63, 6}, 
74     {3, 6}, {3, 6}, {3, 6}, {3, 6}, {3, 6}, {3, 6}, {3, 6}, {3, 6}, 
75     {36, 6}, {36, 6}, {36, 6}, {36, 6}, {36, 6}, {36, 6}, {36, 6}, {36, 6}, 
76     {24, 6}, {24, 6}, {24, 6}, {24, 6}, {24, 6}, {24, 6}, {24, 6}, {24, 6}, 
77     {62, 5}, {62, 5}, {62, 5}, {62, 5}, {62, 5}, {62, 5}, {62, 5}, {62, 5},
78     {62, 5}, {62, 5}, {62, 5}, {62, 5}, {62, 5}, {62, 5}, {62, 5}, {62, 5},
79     {2, 5}, {2, 5}, {2, 5}, {2, 5}, {2, 5}, {2, 5}, {2, 5}, {2, 5}, 
80     {2, 5}, {2, 5}, {2, 5}, {2, 5}, {2, 5}, {2, 5}, {2, 5}, {2, 5}, 
81     {61, 5}, {61, 5}, {61, 5}, {61, 5}, {61, 5}, {61, 5}, {61, 5}, {61, 5}, 
82     {61, 5}, {61, 5}, {61, 5}, {61, 5}, {61, 5}, {61, 5}, {61, 5}, {61, 5}, 
83     {1, 5}, {1, 5}, {1, 5}, {1, 5}, {1, 5}, {1, 5}, {1, 5}, {1, 5}, 
84     {1, 5}, {1, 5}, {1, 5}, {1, 5}, {1, 5}, {1, 5}, {1, 5}, {1, 5}, 
85     {56, 5}, {56, 5}, {56, 5}, {56, 5}, {56, 5}, {56, 5}, {56, 5}, {56, 5}, 
86     {56, 5}, {56, 5}, {56, 5}, {56, 5}, {56, 5}, {56, 5}, {56, 5}, {56, 5}, 
87     {52, 5}, {52, 5}, {52, 5}, {52, 5}, {52, 5}, {52, 5}, {52, 5}, {52, 5}, 
88     {52, 5}, {52, 5}, {52, 5}, {52, 5}, {52, 5}, {52, 5}, {52, 5}, {52, 5}, 
89     {44, 5}, {44, 5}, {44, 5}, {44, 5}, {44, 5}, {44, 5}, {44, 5}, {44, 5}, 
90     {44, 5}, {44, 5}, {44, 5}, {44, 5}, {44, 5}, {44, 5}, {44, 5}, {44, 5}, 
91     {28, 5}, {28, 5}, {28, 5}, {28, 5}, {28, 5}, {28, 5}, {28, 5}, {28, 5}, 
92     {28, 5}, {28, 5}, {28, 5}, {28, 5}, {28, 5}, {28, 5}, {28, 5}, {28, 5}, 
93     {40, 5}, {40, 5}, {40, 5}, {40, 5}, {40, 5}, {40, 5}, {40, 5}, {40, 5}, 
94     {40, 5}, {40, 5}, {40, 5}, {40, 5}, {40, 5}, {40, 5}, {40, 5}, {40, 5}, 
95     {20, 5}, {20, 5}, {20, 5}, {20, 5}, {20, 5}, {20, 5}, {20, 5}, {20, 5}, 
96     {20, 5}, {20, 5}, {20, 5}, {20, 5}, {20, 5}, {20, 5}, {20, 5}, {20, 5}, 
97     {48, 5}, {48, 5}, {48, 5}, {48, 5}, {48, 5}, {48, 5}, {48, 5}, {48, 5}, 
98     {48, 5}, {48, 5}, {48, 5}, {48, 5}, {48, 5}, {48, 5}, {48, 5}, {48, 5}, 
99     {12, 5}, {12, 5}, {12, 5}, {12, 5}, {12, 5}, {12, 5}, {12, 5}, {12, 5}, 
100     {12, 5}, {12, 5}, {12, 5}, {12, 5}, {12, 5}, {12, 5}, {12, 5}, {12, 5}, 
101     {32, 4}, {32, 4}, {32, 4}, {32, 4}, {32, 4}, {32, 4}, {32, 4}, {32, 4}, 
102     {32, 4}, {32, 4}, {32, 4}, {32, 4}, {32, 4}, {32, 4}, {32, 4}, {32, 4}, 
103     {32, 4}, {32, 4}, {32, 4}, {32, 4}, {32, 4}, {32, 4}, {32, 4}, {32, 4}, 
104     {32, 4}, {32, 4}, {32, 4}, {32, 4}, {32, 4}, {32, 4}, {32, 4}, {32, 4}, 
105     {16, 4}, {16, 4}, {16, 4}, {16, 4}, {16, 4}, {16, 4}, {16, 4}, {16, 4}, 
106     {16, 4}, {16, 4}, {16, 4}, {16, 4}, {16, 4}, {16, 4}, {16, 4}, {16, 4}, 
107     {16, 4}, {16, 4}, {16, 4}, {16, 4}, {16, 4}, {16, 4}, {16, 4}, {16, 4}, 
108     {16, 4}, {16, 4}, {16, 4}, {16, 4}, {16, 4}, {16, 4}, {16, 4}, {16, 4}, 
109     {8, 4}, {8, 4}, {8, 4}, {8, 4}, {8, 4}, {8, 4}, {8, 4}, {8, 4}, 
110     {8, 4}, {8, 4}, {8, 4}, {8, 4}, {8, 4}, {8, 4}, {8, 4}, {8, 4}, 
111     {8, 4}, {8, 4}, {8, 4}, {8, 4}, {8, 4}, {8, 4}, {8, 4}, {8, 4}, 
112     {8, 4}, {8, 4}, {8, 4}, {8, 4}, {8, 4}, {8, 4}, {8, 4}, {8, 4},
113     {4, 4}, {4, 4}, {4, 4}, {4, 4}, {4, 4}, {4, 4}, {4, 4}, {4, 4},
114     {4, 4}, {4, 4}, {4, 4}, {4, 4}, {4, 4}, {4, 4}, {4, 4}, {4, 4}, 
115     {4, 4}, {4, 4}, {4, 4}, {4, 4}, {4, 4}, {4, 4}, {4, 4}, {4, 4}, 
116     {4, 4}, {4, 4}, {4, 4}, {4, 4}, {4, 4}, {4, 4}, {4, 4}, {4, 4},
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     {60, 3}, {60, 3}, {60, 3}, {60, 3}, {60, 3}, {60, 3}, {60, 3}, {60, 3}, 
123     {60, 3}, {60, 3}, {60, 3}, {60, 3}, {60, 3}, {60, 3}, {60, 3}, {60, 3}, 
124     {60, 3}, {60, 3}, {60, 3}, {60, 3}, {60, 3}, {60, 3}, {60, 3}, {60, 3} };
125
126     /* Tables for dc DCT coefficients 
127      * Tables are cut in two parts to reduce memory occupation
128      */
129     
130     /* Table B-12, dct_dc_size_luminance, codes 00xxx ... 11110 */
131 lookup_t     pl_dct_dc_lum_init_table_1[32] =
132     { {1, 2}, {1, 2}, {1, 2}, {1, 2}, {1, 2}, {1, 2}, {1, 2}, {1, 2},
133       {2, 2}, {2, 2}, {2, 2}, {2, 2}, {2, 2}, {2, 2}, {2, 2}, {2, 2},
134       {0, 3}, {0, 3}, {0, 3}, {0, 3}, {3, 3}, {3, 3}, {3, 3}, {3, 3},
135       {4, 3}, {4, 3}, {4, 3}, {4, 3}, {5, 4}, {5, 4}, {6, 5}, {MB_ERROR, 0}
136     };
137
138     /* Table B-12, dct_dc_size_luminance, codes 111110xxx ... 111111111 */
139 lookup_t     pl_dct_dc_lum_init_table_2[32] =
140     { {7, 6}, {7, 6}, {7, 6}, {7, 6}, {7, 6}, {7, 6}, {7, 6}, {7, 6},
141       {8, 7}, {8, 7}, {8, 7}, {8, 7}, {9, 8}, {9, 8}, {10,9}, {11,9},
142       {MB_ERROR, 0}, {MB_ERROR, 0}, {MB_ERROR, 0}, {MB_ERROR, 0},
143       {MB_ERROR, 0}, {MB_ERROR, 0}, {MB_ERROR, 0}, {MB_ERROR, 0},
144       {MB_ERROR, 0}, {MB_ERROR, 0}, {MB_ERROR, 0}, {MB_ERROR, 0},
145       {MB_ERROR, 0}, {MB_ERROR, 0}, {MB_ERROR, 0}, {MB_ERROR, 0}
146     };
147
148     /* Table B-13, dct_dc_size_chrominance, codes 00xxx ... 11110 */
149 lookup_t     pl_dct_dc_chrom_init_table_1[32] =
150     { {0, 2}, {0, 2}, {0, 2}, {0, 2}, {0, 2}, {0, 2}, {0, 2}, {0, 2},
151       {1, 2}, {1, 2}, {1, 2}, {1, 2}, {1, 2}, {1, 2}, {1, 2}, {1, 2},
152       {2, 2}, {2, 2}, {2, 2}, {2, 2}, {2, 2}, {2, 2}, {2, 2}, {2, 2},
153       {3, 3}, {3, 3}, {3, 3}, {3, 3}, {4, 4}, {4, 4}, {5, 5}, {MB_ERROR, 0}
154     };
155
156     /* Table B-13, dct_dc_size_chrominance, codes 111110xxxx ... 1111111111 */
157 lookup_t     pl_dct_dc_chrom_init_table_2[32] =
158     { {6, 6}, {6, 6}, {6, 6}, {6, 6}, {6, 6}, {6, 6}, {6, 6}, {6, 6},
159       {6, 6}, {6, 6}, {6, 6}, {6, 6}, {6, 6}, {6, 6}, {6, 6}, {6, 6},
160       {7, 7}, {7, 7}, {7, 7}, {7, 7}, {7, 7}, {7, 7}, {7, 7}, {7, 7},
161       {8, 8}, {8, 8}, {8, 8}, {8, 8}, {9, 9}, {9, 9}, {10,10}, {11,10}
162     };
163
164 /* Tables for ac DCT coefficients. There are cut in many parts to save space */
165     /* Table B-14, DCT coefficients table zero,
166      * codes 0100 ... 1xxx (used for first (DC) coefficient)
167      */
168 dct_lookup_t pl_DCT_tab_dc[12] =
169     {
170         {0,2,4}, {2,1,4}, {1,1,3}, {1,1,3},
171         {0,1,1}, {0,1,1}, {0,1,1}, {0,1,1},
172         {0,1,1}, {0,1,1}, {0,1,1}, {0,1,1}
173     };
174
175     /* Table B-14, DCT coefficients table zero,
176      * codes 0100 ... 1xxx (used for all other coefficients)
177      */
178 dct_lookup_t pl_DCT_tab_ac[12] =
179     {
180         {0,2,4},  {2,1,4},  {1,1,3},  {1,1,3},
181         {DCT_EOB,0,2}, {DCT_EOB,0,2}, {DCT_EOB,0,2}, {DCT_EOB,0,2}, /* EOB */
182         {0,1,2},  {0,1,2},  {0,1,2},  {0,1,2}
183     };
184
185     /* Table B-14, DCT coefficients table zero,
186      * codes 000001xx ... 00111xxx
187      */
188 dct_lookup_t pl_DCT_tab0[60] =
189     {
190         {DCT_ESCAPE,0,6}, {DCT_ESCAPE,0,6}, {DCT_ESCAPE,0,6}, {DCT_ESCAPE,0,6},
191         /* Escape */
192         {2,2,7}, {2,2,7}, {9,1,7}, {9,1,7},
193         {0,4,7}, {0,4,7}, {8,1,7}, {8,1,7},
194         {7,1,6}, {7,1,6}, {7,1,6}, {7,1,6},
195         {6,1,6}, {6,1,6}, {6,1,6}, {6,1,6},
196         {1,2,6}, {1,2,6}, {1,2,6}, {1,2,6},
197         {5,1,6}, {5,1,6}, {5,1,6}, {5,1,6},
198         {13,1,8}, {0,6,8}, {12,1,8}, {11,1,8},
199         {3,2,8}, {1,3,8}, {0,5,8}, {10,1,8},
200         {0,3,5}, {0,3,5}, {0,3,5}, {0,3,5},
201         {0,3,5}, {0,3,5}, {0,3,5}, {0,3,5},
202         {4,1,5}, {4,1,5}, {4,1,5}, {4,1,5},
203         {4,1,5}, {4,1,5}, {4,1,5}, {4,1,5},
204         {3,1,5}, {3,1,5}, {3,1,5}, {3,1,5},
205         {3,1,5}, {3,1,5}, {3,1,5}, {3,1,5}
206     };
207
208     /* Table B-15, DCT coefficients table one,
209      * codes 000001xx ... 11111111
210      */
211 dct_lookup_t pl_DCT_tab0a[252] =
212     {
213         {DCT_ESCAPE,0,6}, {DCT_ESCAPE,0,6}, {DCT_ESCAPE,0,6}, {DCT_ESCAPE,0,6},
214         /* Escape */
215         {7,1,7}, {7,1,7}, {8,1,7}, {8,1,7},
216         {6,1,7}, {6,1,7}, {2,2,7}, {2,2,7},
217         {0,7,6}, {0,7,6}, {0,7,6}, {0,7,6},
218         {0,6,6}, {0,6,6}, {0,6,6}, {0,6,6},
219         {4,1,6}, {4,1,6}, {4,1,6}, {4,1,6},
220         {5,1,6}, {5,1,6}, {5,1,6}, {5,1,6},
221         {1,5,8}, {11,1,8}, {0,11,8}, {0,10,8},
222         {13,1,8}, {12,1,8}, {3,2,8}, {1,4,8},
223         {2,1,5}, {2,1,5}, {2,1,5}, {2,1,5},
224         {2,1,5}, {2,1,5}, {2,1,5}, {2,1,5},
225         {1,2,5}, {1,2,5}, {1,2,5}, {1,2,5},
226         {1,2,5}, {1,2,5}, {1,2,5}, {1,2,5},
227         {3,1,5}, {3,1,5}, {3,1,5}, {3,1,5},
228         {3,1,5}, {3,1,5}, {3,1,5}, {3,1,5},
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         {1,1,3}, {1,1,3}, {1,1,3}, {1,1,3},
235         {1,1,3}, {1,1,3}, {1,1,3}, {1,1,3},
236         {1,1,3}, {1,1,3}, {1,1,3}, {1,1,3},
237         {DCT_EOB,0,4}, {DCT_EOB,0,4}, {DCT_EOB,0,4}, {DCT_EOB,0,4}, /* EOB */
238         {DCT_EOB,0,4}, {DCT_EOB,0,4}, {DCT_EOB,0,4}, {DCT_EOB,0,4},
239         {DCT_EOB,0,4}, {DCT_EOB,0,4}, {DCT_EOB,0,4}, {DCT_EOB,0,4},
240         {DCT_EOB,0,4}, {DCT_EOB,0,4}, {DCT_EOB,0,4}, {DCT_EOB,0,4},
241         {0,3,4}, {0,3,4}, {0,3,4}, {0,3,4},
242         {0,3,4}, {0,3,4}, {0,3,4}, {0,3,4},
243         {0,3,4}, {0,3,4}, {0,3,4}, {0,3,4},
244         {0,3,4}, {0,3,4}, {0,3,4}, {0,3,4},
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,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,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,2,3}, {0,2,3}, {0,2,3}, {0,2,3},
267         {0,2,3}, {0,2,3}, {0,2,3}, {0,2,3},
268         {0,4,5}, {0,4,5}, {0,4,5}, {0,4,5},
269         {0,4,5}, {0,4,5}, {0,4,5}, {0,4,5},
270         {0,5,5}, {0,5,5}, {0,5,5}, {0,5,5},
271         {0,5,5}, {0,5,5}, {0,5,5}, {0,5,5},
272         {9,1,7}, {9,1,7}, {1,3,7}, {1,3,7},
273         {10,1,7}, {10,1,7}, {0,8,7}, {0,8,7},
274         {0,9,7}, {0,9,7}, {0,12,8}, {0,13,8},
275         {2,3,8}, {4,2,8}, {0,14,8}, {0,15,8}
276     };
277
278     /* Table B-14, DCT coefficients table zero,
279      * codes 0000001000 ... 0000001111
280      */
281 dct_lookup_t pl_DCT_tab1[8] =
282     {
283         {16,1,10}, {5,2,10}, {0,7,10}, {2,3,10},
284         {1,4,10}, {15,1,10}, {14,1,10}, {4,2,10}
285     };
286
287     /* Table B-15, DCT coefficients table one,
288      * codes 000000100x ... 000000111x
289      */
290 dct_lookup_t pl_DCT_tab1a[8] =
291     {
292         {5,2,9}, {5,2,9}, {14,1,9}, {14,1,9},
293         {2,4,10}, {16,1,10}, {15,1,9}, {15,1,9}
294     };
295
296     /* Table B-14/15, DCT coefficients table zero / one,
297      * codes 000000010000 ... 000000011111
298      */
299 dct_lookup_t pl_DCT_tab2[16] =
300     {
301         {0,11,12}, {8,2,12}, {4,3,12}, {0,10,12},
302         {2,4,12}, {7,2,12}, {21,1,12}, {20,1,12},
303         {0,9,12}, {19,1,12}, {18,1,12}, {1,5,12},
304         {3,3,12}, {0,8,12}, {6,2,12}, {17,1,12}
305     };
306
307     /* Table B-14/15, DCT coefficients table zero / one,
308      * codes 0000000010000 ... 0000000011111
309      */
310 dct_lookup_t pl_DCT_tab3[16] =
311     {
312         {10,2,13}, {9,2,13}, {5,3,13}, {3,4,13},
313         {2,5,13}, {1,7,13}, {1,6,13}, {0,15,13},
314         {0,14,13}, {0,13,13}, {0,12,13}, {26,1,13},
315         {25,1,13}, {24,1,13}, {23,1,13}, {22,1,13}
316     };
317
318     /* Table B-14/15, DCT coefficients table zero / one,
319      * codes 00000000010000 ... 00000000011111
320      */
321 dct_lookup_t pl_DCT_tab4[16] =
322     {
323         {0,31,14}, {0,30,14}, {0,29,14}, {0,28,14},
324         {0,27,14}, {0,26,14}, {0,25,14}, {0,24,14},
325         {0,23,14}, {0,22,14}, {0,21,14}, {0,20,14},
326         {0,19,14}, {0,18,14}, {0,17,14}, {0,16,14}
327     };
328
329     /* Table B-14/15, DCT coefficients table zero / one,
330      *   codes 000000000010000 ... 000000000011111
331      */
332 dct_lookup_t pl_DCT_tab5[16] =
333     {
334     {0,40,15}, {0,39,15}, {0,38,15}, {0,37,15},
335     {0,36,15}, {0,35,15}, {0,34,15}, {0,33,15},
336     {0,32,15}, {1,14,15}, {1,13,15}, {1,12,15},
337     {1,11,15}, {1,10,15}, {1,9,15}, {1,8,15}
338     };
339
340     /* Table B-14/15, DCT coefficients table zero / one,
341      * codes 0000000000010000 ... 0000000000011111
342      */
343 dct_lookup_t pl_DCT_tab6[16] =
344     {
345         {1,18,16}, {1,17,16}, {1,16,16}, {1,15,16},
346         {6,3,16}, {16,2,16}, {15,2,16}, {14,2,16},
347         {13,2,16}, {12,2,16}, {11,2,16}, {31,1,16},
348         {30,1,16}, {29,1,16}, {28,1,16}, {27,1,16}
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 + 32768;
364
365     for( i_dummy = -32768; 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 < 32767; i_dummy++ )
374     {
375         p_vpar->pi_crop[i_dummy] = 2047;
376     }
377 }
378 #endif
379
380 /*****************************************************************************
381  * InitMbAddrInc : Initialize the lookup table for mb_addr_inc
382  *****************************************************************************/
383
384 /* Fonction for filling up the lookup table for mb_addr_inc */
385 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_dummy, i_dummy2;
390     for( i_dummy = i_start ; i_dummy < i_end ; i_dummy += i_step )
391     {
392         for( i_dummy2 = 0 ; i_dummy2 < i_step ; i_dummy2 ++ )
393         {
394             p_vpar->pl_mb_addr_inc[i_dummy + i_dummy2].i_value = * pi_value;
395             p_vpar->pl_mb_addr_inc[i_dummy + i_dummy2].i_length = i_length;
396         }
397     (*pi_value)--;
398     }
399 }
400     
401 /* Fonction that initialize the table using the last one */
402 void InitMbAddrInc( vpar_thread_t * p_vpar )
403 {
404     int i_dummy;
405     int i_value;
406     for( i_dummy = 0 ; i_dummy < 8 ; i_dummy++ )
407     {
408         p_vpar->pl_mb_addr_inc[i_dummy].i_value = MB_ERROR;
409         p_vpar->pl_mb_addr_inc[i_dummy].i_length = 0;
410     }
411
412     p_vpar->pl_mb_addr_inc[8].i_value = MB_ADDRINC_ESCAPE;
413     p_vpar->pl_mb_addr_inc[8].i_length = 11;
414
415     for( i_dummy = 9 ; i_dummy < 15 ; i_dummy ++ ) 
416     {
417         p_vpar->pl_mb_addr_inc[i_dummy].i_value =  MB_ERROR;
418         p_vpar->pl_mb_addr_inc[i_dummy].i_length = 0;
419     }
420
421     p_vpar->pl_mb_addr_inc[15].i_value = MB_ADDRINC_STUFFING;
422     p_vpar->pl_mb_addr_inc[15].i_length = 11;
423
424     for( i_dummy = 16; i_dummy < 24; i_dummy++ ) 
425     {
426         p_vpar->pl_mb_addr_inc[i_dummy].i_value =  MB_ERROR;
427         p_vpar->pl_mb_addr_inc[i_dummy].i_length = 0;
428     }
429     i_value = 33;
430     FillMbAddrIncTable( p_vpar, 1024, 2048, 1024, &i_value, 1 );
431     FillMbAddrIncTable( p_vpar, 512, 1024, 256, &i_value, 3 );
432     FillMbAddrIncTable( p_vpar, 256, 512, 128, &i_value, 4 );
433     FillMbAddrIncTable( p_vpar, 128, 256, 64, &i_value, 5 );
434     FillMbAddrIncTable( p_vpar, 96, 128, 16, &i_value, 7 );
435     FillMbAddrIncTable( p_vpar, 48, 96, 8, &i_value, 8 );
436     FillMbAddrIncTable( p_vpar, 36, 48, 2, &i_value, 10 );
437     FillMbAddrIncTable( p_vpar, 24, 36, 1, &i_value, 11 );
438 }
439
440 /*****************************************************************************
441  * Init*MBType : Initialize lookup table for the Macroblock type
442  *****************************************************************************/
443
444 /* Fonction for filling up the tables */
445 static void __inline__ FillMBType( vpar_thread_t * p_vpar,
446                                    int           i_mb_type,
447                                    int           i_start,
448                                    int           i_end,
449                                    int           i_value,
450                                    int           i_length )
451 {
452     int i_dummy;
453     
454     for( i_dummy = i_start ; i_dummy < i_end ; i_dummy++ )
455     {
456         p_vpar->ppl_mb_type[i_mb_type][i_dummy].i_value = i_value;
457         p_vpar->ppl_mb_type[i_mb_type][i_dummy].i_length = i_length;
458     }
459 }
460
461 /* Fonction that fills the table for P MB_Type */
462 void InitPMBType( vpar_thread_t * p_vpar )
463 {
464     FillMBType( p_vpar, 0, 32, 64, MB_MOTION_FORWARD|MB_PATTERN, 1 );
465     FillMBType( p_vpar, 0, 16, 32, MB_PATTERN, 2 );
466     FillMBType( p_vpar, 0, 8, 16, MB_MOTION_FORWARD, 3 );
467     FillMBType( p_vpar, 0, 6, 8, MB_INTRA, 5 );
468     FillMBType( p_vpar, 0, 4, 6, MB_QUANT|MB_MOTION_FORWARD|MB_PATTERN, 5 );
469     FillMBType( p_vpar, 0, 2, 4, MB_QUANT|MB_PATTERN, 5 );
470     p_vpar->ppl_mb_type[0][1].i_value = MB_QUANT|MB_INTRA;
471     p_vpar->ppl_mb_type[0][1].i_length = 6;
472     p_vpar->ppl_mb_type[0][0].i_value = MB_ERROR;
473     p_vpar->ppl_mb_type[0][0].i_length = 0;
474 }
475
476 /* Fonction that fills the table for B MB_Type */
477 void InitBMBType( vpar_thread_t * p_vpar )
478 {
479     FillMBType( p_vpar, 1, 48, 64, MB_MOTION_FORWARD
480                                   |MB_MOTION_BACKWARD|MB_PATTERN, 2 );
481     FillMBType( p_vpar, 1, 32, 48, MB_MOTION_FORWARD|MB_MOTION_BACKWARD, 2 );
482     FillMBType( p_vpar, 1, 24, 32, MB_MOTION_BACKWARD|MB_PATTERN, 3 );
483     FillMBType( p_vpar, 1, 16, 24, MB_MOTION_BACKWARD, 3 );
484     FillMBType( p_vpar, 1, 12, 16, MB_MOTION_FORWARD|MB_PATTERN, 4 );
485     FillMBType( p_vpar, 1, 8, 12, MB_MOTION_FORWARD, 4 );
486     FillMBType( p_vpar, 1, 6, 8, MB_INTRA, 5 );
487     FillMBType( p_vpar, 1, 4, 6, MB_QUANT|MB_MOTION_FORWARD
488                                 |MB_MOTION_BACKWARD|MB_PATTERN, 5 );
489     p_vpar->ppl_mb_type[1][3].i_value = MB_QUANT|MB_MOTION_FORWARD|MB_PATTERN;
490     p_vpar->ppl_mb_type[1][3].i_length = 6;
491     p_vpar->ppl_mb_type[1][2].i_value = MB_QUANT|MB_MOTION_BACKWARD|MB_PATTERN;
492     p_vpar->ppl_mb_type[1][2].i_length = 6;
493     p_vpar->ppl_mb_type[1][1].i_value = MB_QUANT|MB_INTRA;
494     p_vpar->ppl_mb_type[1][1].i_length = 6;
495     p_vpar->ppl_mb_type[1][0].i_value =MB_ERROR;
496     p_vpar->ppl_mb_type[1][0].i_length = 0;
497 }
498
499 /*****************************************************************************
500  * InitCodedPattern : Initialize the lookup table for decoding 
501  *                    coded block pattern
502  *****************************************************************************/
503 void InitCodedPattern( vpar_thread_t * p_vpar )
504 {
505     p_vpar->pl_coded_pattern = pl_coded_pattern_init_table;
506 }
507
508 /*****************************************************************************
509  * InitDCT : Initialize tables giving the length of the dct coefficient
510  *           from the vlc code
511  *****************************************************************************/
512
513 /* First fonction for filling the table */
514 static void __inline__ FillDCTTable( vpar_thread_t * p_vpar,
515                                      dct_lookup_t * pl_DCT_tab,
516                                      int i_intra, int i_dec, int i_off )
517 {
518     int i_dummy, i_dummy2;
519     int i_end;
520     i_end = sizeof(pl_DCT_tab) / sizeof(dct_lookup_t);
521     for( i_dummy = 0; i_dummy < i_end; i_dummy++ )
522     {
523         for( i_dummy2 = 0; i_dummy2 < ( 1 << i_dec ); i_dummy2++ )
524         {
525             p_vpar->pppl_dct_coef
526                 [i_intra][( ( i_dummy + i_off ) << i_dec ) + i_dummy2] =
527                                                          &pl_DCT_tab[i_dummy];
528         }
529     }
530 }
531
532
533 /* Fonction that actually fills the table or create the pointers */
534 void InitDCTTables( vpar_thread_t * p_vpar )
535 {
536     /* Tables are cut in two parts to reduce memory occupation */
537     
538     p_vpar->pppl_dct_dc_size[0][0] = pl_dct_dc_lum_init_table_1;
539     p_vpar->pppl_dct_dc_size[0][1] = pl_dct_dc_lum_init_table_2;
540     p_vpar->pppl_dct_dc_size[1][0] = pl_dct_dc_chrom_init_table_1;
541     p_vpar->pppl_dct_dc_size[1][1] = pl_dct_dc_chrom_init_table_2;
542     
543     /* For table B14 & B15, we have a pointer to tables */
544     /* We fill the table thanks to the fonction defined above */
545
546     FillDCTTable(  p_vpar, pl_DCT_tab_dc, 0, 12, 4 );
547     FillDCTTable(  p_vpar, pl_DCT_tab_ac, 0, 12, 4 );
548     FillDCTTable(  p_vpar, pl_DCT_tab0, 0, 8, 4 );
549     FillDCTTable(  p_vpar, pl_DCT_tab1, 0, 6, 8 );
550     FillDCTTable(  p_vpar, pl_DCT_tab2, 0, 4, 16 );
551     FillDCTTable(  p_vpar, pl_DCT_tab3, 0, 3, 16 );
552     FillDCTTable(  p_vpar, pl_DCT_tab4, 0, 2, 16 );
553     FillDCTTable(  p_vpar, pl_DCT_tab5, 0, 1, 16 );
554     FillDCTTable(  p_vpar, pl_DCT_tab6, 0, 0, 16 );
555     FillDCTTable(  p_vpar, pl_DCT_tab_dc, 1, 12, 4 );
556     FillDCTTable(  p_vpar, pl_DCT_tab_ac, 1, 12, 4 );
557     FillDCTTable(  p_vpar, pl_DCT_tab0a, 1, 8, 4 );
558     FillDCTTable(  p_vpar, pl_DCT_tab1a, 1, 6, 8);
559     FillDCTTable(  p_vpar, pl_DCT_tab2, 1, 4, 16 );
560     FillDCTTable(  p_vpar, pl_DCT_tab3, 1, 3, 16 );
561     FillDCTTable(  p_vpar, pl_DCT_tab4, 1, 2, 16 );
562     FillDCTTable(  p_vpar, pl_DCT_tab5, 1, 1, 16 );
563     FillDCTTable(  p_vpar, pl_DCT_tab6, 1, 0, 16 );
564     
565 }
566
567 /*
568  * Macroblock parsing functions
569  */
570
571 /*****************************************************************************
572  * vpar_ParseMacroblock : Parse the next macroblock
573  *****************************************************************************/
574 void vpar_ParseMacroblock( vpar_thread_t * p_vpar, int * pi_mb_address,
575                            int i_mb_previous, int i_mb_base )
576 {
577     static f_addb_t ppf_addb_intra[2] = {vdec_AddBlock, vdec_CopyBlock};
578     static f_decode_block_t pppf_decode_block[2][2] =
579                 { {vpar_DecodeMPEG1Non, vpar_DecodeMPEG1Intra},
580                   {vpar_DecodeMPEG2Non, vpar_DecodeMPEG2Intra} };
581     static int      pi_x[12] = {0,8,0,8,0,0,0,0,8,8,8,8};
582     static int      pi_y[2][12] = { {0,0,8,8,0,0,8,8,0,0,8,8},
583                                     {0,0,1,1,0,0,1,1,0,0,1,1} };
584     static int      pi_chroma_hor[4] = { 0, 1, 1, 0 };
585     static int      pi_chroma_ver[4] = { 0, 1, 0, 0 };
586
587     int             i_mb, i_b, i_mask;
588     macroblock_t *  p_mb;
589     f_addb_t        pf_addb;
590     elem_t *        p_data1;
591     elem_t *        p_data2;
592
593     *pi_mb_address += MacroblockAddressIncrement( p_vpar );
594
595     for( i_mb = i_mb_previous; i_mb < *pi_mb_address; i_mb++ )
596     {
597         /* Skipped macroblock (ISO/IEC 13818-2 7.6.6). */
598         static int          pi_dc_dct_reinit[4] = {128,256,512,1024};
599         static f_motion_t   pf_motion_skipped[4] = {NULL, vdec_MotionField,
600                                 vdec_MotionField, vdec_MotionFrame};
601
602         /* Reset DC predictors (7.2.1). */
603         p_vpar->slice.pi_dc_dct_pred[0] = p_vpar->slice.pi_dc_dct_pred[1]
604             = p_vpar->slice.pi_dc_dct_pred[2]
605             = pi_dc_dct_reinit[p_vpar->picture.i_intra_dc_precision];
606
607         if( p_vpar->picture.i_coding_type == P_CODING_TYPE )
608         {
609             /* Reset motion vector predictors (ISO/IEC 13818-2 7.6.3.4). */
610             bzero( p_vpar->slice.pppi_pmv, 8*sizeof(int) );
611         }
612
613         if( (p_mb = p_vpar->picture.pp_mb[i_mb_base + i_mb] =
614              vpar_NewMacroblock( &p_vpar->vfifo )) == NULL )
615         {
616             p_vpar->picture.b_error = 1;
617             intf_ErrMsg("vpar error: macroblock list is empty !");
618             return;
619         }
620
621         InitMacroblock( p_vpar, p_mb );
622        
623         /* No IDCT nor AddBlock. */
624         for( i_b = 0; i_b < 12; i_b++ )
625         {
626             p_mb->pf_idct[i_b] = vdec_DummyIDCT;
627             p_mb->pf_addb[i_b] = vdec_DummyBlock;
628         }
629
630         /* Motion type is picture structure. */
631         p_mb->pf_motion = pf_motion_skipped[p_vpar->picture.i_structure];
632
633         /* Set the field we use for motion compensation */
634         p_mb->ppi_field_select[0][0] = p_mb->ppi_field_select[0][1]
635                                      = ( p_vpar->picture.i_current_structure == BOTTOM_FIELD );
636         
637         /* Predict from field of same parity. */
638         /* ??? */
639     }
640
641     /* Get a macroblock structure. */
642     if( (p_mb = p_vpar->picture.pp_mb[i_mb_base + *pi_mb_address] =
643          vpar_NewMacroblock( &p_vpar->vfifo )) == NULL )
644     {
645         p_vpar->picture.b_error = 1;
646         intf_ErrMsg("vpar error: macroblock list is empty !");
647         return;
648     }
649
650     InitMacroblock( p_vpar, p_mb );
651
652     /* Parse off macroblock_modes structure. */
653     MacroblockModes( p_vpar, p_mb );
654
655     if( p_vpar->mb.i_mb_type & MB_QUANT )
656     {
657         LoadQuantizerScale( p_vpar );
658     }
659
660     if( p_vpar->mb.i_mb_type & MB_MOTION_FORWARD )
661     {
662         (*p_vpar->sequence.pf_decode_mv)( p_vpar, p_mb, 0 );
663     }
664
665     if( p_vpar->mb.i_mb_type & MB_MOTION_BACKWARD )
666     {
667         (*p_vpar->sequence.pf_decode_mv)( p_vpar, p_mb, 1 );
668     }
669
670     if( p_vpar->picture.b_concealment_mv && (p_vpar->mb.i_mb_type & MB_INTRA) )
671     {
672         DumpBits( &p_vpar->bit_stream, 1 );
673     }
674
675     if( p_vpar->mb.i_mb_type & MB_PATTERN )
676     {
677         (*p_vpar->sequence.pf_decode_pattern)( p_vpar );
678     }
679     else
680     {
681         int     pi_coded_block_pattern[2] = {0,
682                     (1 << 4+2*p_vpar->sequence.i_chroma_nb_blocks) - 1};
683         p_vpar->mb.i_coded_block_pattern = pi_coded_block_pattern
684                                     [p_vpar->mb.i_mb_type & MB_INTRA];
685     }
686
687     pf_addb = ppf_addb_intra[p_vpar->mb.i_mb_type & MB_INTRA];
688
689     /*
690      * Effectively decode blocks.
691      */
692
693     i_mask = 1 << (3 + 2*p_vpar->sequence.i_chroma_nb_blocks);
694
695     /* luminance */
696     p_data1 = (elem_t*) p_mb->p_picture->p_y;
697              // + p_mb->i_l_x + p_mb->i_l_y*(p_vpar->sequence.i_width);
698
699     for( i_b = 0 ; i_b < 4 ; i_b++, i_mask >>= 1 )
700     {
701         if( p_vpar->mb.i_coded_block_pattern & i_mask )
702         {
703             memset( p_mb->ppi_blocks[i_b], 0, 64*sizeof(elem_t) );
704             (*pppf_decode_block[p_vpar->sequence.b_mpeg2]
705                                [p_vpar->mb.i_mb_type & MB_INTRA])
706                 ( p_vpar, p_mb, i_b );
707         
708             /* decode_block has already set pf_idct and pi_sparse_pos. */
709             p_mb->pf_addb[i_b] = pf_addb;
710      
711             /* Calculate block coordinates. */
712             p_mb->p_data[i_b] = p_data1
713                                 + pi_y[p_vpar->mb.b_dct_type][i_b]
714                                 * p_vpar->sequence.i_chroma_width;
715         }
716         else
717         {
718             /* Block not coded, so no IDCT, nor AddBlock */
719             p_mb->pf_addb[i_b] = vdec_DummyBlock;
720             p_mb->pf_idct[i_b] = vdec_DummyIDCT;
721         }
722     }
723
724     /* chrominance U */
725     p_data1 = (elem_t*) p_mb->p_picture->p_u
726               + (p_mb->i_c_x >> pi_chroma_hor[p_vpar->sequence.i_chroma_format])
727               + (p_mb->i_c_y >> pi_chroma_ver[p_vpar->sequence.i_chroma_format])
728                 * (p_vpar->sequence.i_chroma_width);
729     p_data2 = (elem_t*) p_mb->p_picture->p_v
730               + (p_mb->i_c_x >> pi_chroma_hor[p_vpar->sequence.i_chroma_format])
731               + (p_mb->i_c_y >> pi_chroma_ver[p_vpar->sequence.i_chroma_format])
732                 * (p_vpar->sequence.i_chroma_width);
733
734     for( i_b = 4; i_b < 4 + 2*p_vpar->sequence.i_chroma_nb_blocks;
735          i_b++, i_mask >>= 1 )
736     {
737         elem_t *    pp_data[2] = {p_data1, p_data2};
738
739         if( p_vpar->mb.i_coded_block_pattern & i_mask )
740         {
741             memset( p_mb->ppi_blocks[i_b], 0, 64*sizeof(elem_t) );
742             (*pppf_decode_block[p_vpar->sequence.b_mpeg2]
743                                [p_vpar->mb.i_mb_type & MB_INTRA])
744                 ( p_vpar, p_mb, i_b );
745
746             /* decode_block has already set pf_idct and pi_sparse_pos. */
747             p_mb->pf_addb[i_b] = pf_addb;
748
749             /* Calculate block coordinates. */
750             p_mb->p_data[i_b] = pp_data[i_b & 1]
751                                  + pi_y[p_vpar->mb.b_dct_type][i_b]
752                                    * p_vpar->sequence.i_chroma_width;
753         }
754         else
755         {
756             /* Block not coded, so no IDCT, nor AddBlock */
757             p_mb->pf_addb[i_b] = vdec_DummyBlock;
758             p_mb->pf_idct[i_b] = vdec_DummyIDCT;
759         }
760     }
761
762     if( !( p_vpar->mb.i_mb_type & MB_INTRA ) )
763     {
764         static int          pi_dc_dct_reinit[4] = {128,256,512,1024};
765
766         /* Reset DC predictors (7.2.1). */
767         p_vpar->slice.pi_dc_dct_pred[0] = p_vpar->slice.pi_dc_dct_pred[1]
768             = p_vpar->slice.pi_dc_dct_pred[2]
769             = pi_dc_dct_reinit[p_vpar->picture.i_intra_dc_precision];
770     }
771     else if( !p_vpar->picture.b_concealment_mv )
772     {
773         /* Reset MV predictors. */
774         bzero( p_vpar->slice.pppi_pmv, 8*sizeof(int) );
775     }
776 }
777
778 /*****************************************************************************
779  * InitMacroblock : Initialize macroblock values
780  *****************************************************************************/
781 static __inline__ void InitMacroblock( vpar_thread_t * p_vpar,
782                                        macroblock_t * p_mb )
783 {
784     static f_chroma_motion_t pf_chroma_motion[4] =
785     { NULL, vdec_Motion420, vdec_Motion422, vdec_Motion444 };
786     
787     p_mb->p_picture = p_vpar->picture.p_picture;
788     p_mb->i_structure = p_vpar->picture.i_structure;
789     p_mb->i_current_structure = p_vpar->picture.i_current_structure;
790     p_mb->i_l_x = p_vpar->mb.i_l_x;
791     p_mb->i_l_y = p_vpar->mb.i_l_y;
792     p_mb->i_c_x = p_vpar->mb.i_c_x;
793     p_mb->i_c_y = p_vpar->mb.i_c_y;
794     p_mb->i_chroma_nb_blocks = p_vpar->sequence.i_chroma_nb_blocks;
795     p_mb->pf_chroma_motion = pf_chroma_motion[p_vpar->sequence.i_chroma_format];
796     p_mb->b_P_coding_type = ( p_vpar->picture.i_coding_type == P_CODING_TYPE );
797
798     p_mb->p_forward = p_vpar->sequence.p_forward;
799     p_mb->p_backward = p_vpar->sequence.p_backward;
800     
801     p_mb->i_addb_l_stride = p_mb->i_l_stride = p_vpar->picture.i_l_stride;
802     p_mb->i_addb_c_stride = p_mb->i_c_stride = p_vpar->picture.i_c_stride;
803
804     /* Update macroblock real position. */
805     p_vpar->mb.i_l_x += 16;
806     p_vpar->mb.i_l_y += (p_vpar->mb.i_l_x / p_vpar->sequence.i_width)
807                         * (2 - p_vpar->picture.b_frame_structure) * 16;
808     p_vpar->mb.i_l_x %= p_vpar->sequence.i_width;
809
810     p_vpar->mb.i_c_x += p_vpar->sequence.i_chroma_mb_width;
811     p_vpar->mb.i_c_y += (p_vpar->mb.i_c_x / p_vpar->sequence.i_chroma_width)
812                         * (2 - p_vpar->picture.b_frame_structure)
813                         * p_vpar->sequence.i_chroma_mb_height;
814     p_vpar->mb.i_c_x %= p_vpar->sequence.i_chroma_width;
815 }
816
817 /*****************************************************************************
818  * MacroblockAddressIncrement : Get the macroblock_address_increment field
819  *****************************************************************************/
820 static __inline__ int MacroblockAddressIncrement( vpar_thread_t * p_vpar )
821 {
822     /* Index in the lookup table mb_addr_inc */
823     int    i_index = ShowBits( &p_vpar->bit_stream, 11 );
824     
825     p_vpar->mb.i_addr_inc = 0;
826     
827     /* Test the presence of the escape character */
828     while( i_index == 8 )
829     {
830         DumpBits( &p_vpar->bit_stream, 11 );
831         p_vpar->mb.i_addr_inc += 33;
832         i_index = ShowBits( &p_vpar->bit_stream, 11 );
833     }
834     
835     /* Affect the value from the lookup table */
836     p_vpar->mb.i_addr_inc += p_vpar->pl_mb_addr_inc[i_index].i_value;
837     
838     /* Dump the good number of bits */
839     DumpBits( &p_vpar->bit_stream, p_vpar->pl_mb_addr_inc[i_index].i_length );
840 }
841
842 /*****************************************************************************
843  * MacroblockModes : Get the macroblock_modes structure
844  *****************************************************************************/
845 static __inline__ void MacroblockModes( vpar_thread_t * p_vpar,
846                                         macroblock_t * p_mb )
847 {
848     static f_motion_t   pf_motion[2][4] =
849         { {NULL, vdec_FieldFieldRecon, vdec_Field16x8Recon, vdec_FieldDMVRecon},
850           {NULL, vdec_FrameFieldRecon, vdec_FrameFrameRecon, vdec_FrameDMVRecon} };
851     static int          ppi_mv_count[2][4] = { {0, 1, 2, 1}, {0, 2, 1, 1} };
852     static int          ppi_mv_format[2][4] = { {0, 1, 1, 1}, {0, 1, 2, 1} };
853
854     /* Get macroblock_type. */
855     p_vpar->mb.i_mb_type = (p_vpar->picture.pf_macroblock_type)( p_vpar );
856     p_mb->i_mb_type = p_vpar->mb.i_mb_type;
857     
858     /* SCALABILITY : warning, we don't know if spatial_temporal_weight_code
859      * has to be dropped, take care if you use scalable streams. */
860     /* DumpBits( &p_vpar->bit_stream, 2 ); */
861     
862     if( p_mb->b_P_coding_type && !(p_vpar->mb.i_mb_type & (MB_MOTION_FORWARD|MB_INTRA)) )
863     {
864         /* Special No-MC macroblock in P pictures (7.6.3.5). */
865         memset( p_vpar->slice.pppi_pmv, 0, 8*sizeof(int) );
866         memset( p_mb->pppi_motion_vectors, 0, 8*sizeof(int) );
867         
868         p_vpar->mb.i_motion_type = MOTION_FRAME;
869         p_mb->ppi_field_select[0][0] = ( p_vpar->picture.i_current_structure == BOTTOM_FIELD );
870     }
871     else if( !(p_vpar->mb.i_mb_type & (MB_MOTION_FORWARD | MB_MOTION_BACKWARD))
872              || p_vpar->picture.b_frame_pred_frame_dct )
873     {
874         /* If mb_type has neither MOTION_FORWARD nor MOTION_BACKWARD, this
875          * is useless, but also harmless. */
876         p_vpar->mb.i_motion_type = MOTION_FRAME;
877     }
878     else
879     {
880         p_vpar->mb.i_motion_type = GetBits( &p_vpar->bit_stream, 2 );
881     }
882
883      if( p_vpar->mb.i_mb_type & MB_INTRA )
884     {
885         /* For the intra macroblocks, we use an empty motion
886          * compensation function */
887         p_mb->pf_motion = vdec_DummyRecon;
888     }
889     else
890     {
891         p_mb->pf_motion = pf_motion[p_vpar->picture.b_frame_structure]
892                                    [p_vpar->mb.i_motion_type];
893     }
894     p_vpar->mb.i_mv_count = ppi_mv_count[p_vpar->picture.b_frame_structure]
895                                         [p_vpar->mb.i_motion_type];
896     p_vpar->mb.i_mv_format = ppi_mv_format[p_vpar->picture.b_frame_structure]
897                                           [p_vpar->mb.i_motion_type];
898
899     p_vpar->mb.b_dct_type = 0;
900     if( (p_vpar->picture.i_structure == FRAME_STRUCTURE) &&
901         (!p_vpar->picture.b_frame_pred_frame_dct) &&
902         (p_vpar->mb.i_mb_type & (MB_PATTERN|MB_INTRA)) )
903     {
904         if( p_vpar->mb.b_dct_type = GetBits( &p_vpar->bit_stream, 1 ) )
905         {
906             /* The DCT is coded on fields. Jump one line between each
907              * sample. */
908             p_mb->i_addb_l_stride <<= 1;
909             p_mb->i_addb_l_stride += 8;
910             /* With CHROMA_420, the DCT is necessarily frame-coded. */
911             if( p_vpar->sequence.i_chroma_format != CHROMA_420 )
912             {
913                 p_mb->i_addb_c_stride <<= 1;
914                 p_mb->i_addb_c_stride += 8;
915             }
916         }
917     }
918
919     p_vpar->mb.b_dmv = p_vpar->mb.i_motion_type == MOTION_DMV;
920 }
921
922 /*****************************************************************************
923  * vpar_IMBType : macroblock_type in I pictures
924  *****************************************************************************/
925 int vpar_IMBType( vpar_thread_t * p_vpar )
926 {
927     /* Take two bits for testing */
928     int                 i_type = ShowBits( &p_vpar->bit_stream, 2 );
929     
930     /* Lookup table for macroblock_type */
931     static lookup_t     pl_mb_Itype[4] = { {MB_ERROR, 0},
932                                            {MB_QUANT|MB_INTRA, 2},
933                                            {MB_INTRA, 1},
934                                            {MB_INTRA, 1} };
935                                            
936     /* Dump the good number of bits */
937     DumpBits( &p_vpar->bit_stream, pl_mb_Itype[i_type].i_length );
938     return pl_mb_Itype[i_type].i_value;
939 }
940
941 /*****************************************************************************
942  * vpar_PMBType : macroblock_type in P pictures
943  *****************************************************************************/
944 int vpar_PMBType( vpar_thread_t * p_vpar )
945 {
946     /* Testing on 6 bits */
947     int                i_type = ShowBits( &p_vpar->bit_stream, 6 );
948     
949     /* Dump the good number of bits */
950     DumpBits( &p_vpar->bit_stream, p_vpar->ppl_mb_type[0][i_type].i_length );
951
952     /* return the value from the lookup table for P type */
953     return p_vpar->ppl_mb_type[0][i_type].i_value;
954 }
955
956 /*****************************************************************************
957  * vpar_BMBType : macroblock_type in B pictures
958  *****************************************************************************/
959 int vpar_BMBType( vpar_thread_t * p_vpar )
960 {
961      /* Testing on 6 bits */
962     int                i_type = ShowBits( &p_vpar->bit_stream, 6 );
963     
964     /* Dump the good number of bits */
965     DumpBits( &p_vpar->bit_stream, p_vpar->ppl_mb_type[1][i_type].i_length );
966     
967     /* return the value from the lookup table for B type */
968     return p_vpar->ppl_mb_type[1][i_type].i_value;
969 }
970
971 /*****************************************************************************
972  * vpar_DMBType : macroblock_type in D pictures
973  *****************************************************************************/
974 int vpar_DMBType( vpar_thread_t * p_vpar )
975 {
976     /* Taking 1 bit */
977     int               i_type = GetBits( &p_vpar->bit_stream, 1 );
978     
979     /* Lookup table */
980     static int        pi_mb_Dtype[2] = { MB_ERROR, 1 };
981     
982     return pi_mb_Dtype[i_type];
983 }
984
985 /*****************************************************************************
986  * vpar_CodedPattern420 : coded_block_pattern with 420 chroma
987  *****************************************************************************/
988 int vpar_CodedPattern420( vpar_thread_t * p_vpar )
989 {
990     /* Take the max 9 bits length vlc code for testing */
991     int      i_vlc = ShowBits( &p_vpar->bit_stream, 9 );
992     
993     /* Trash the good number of bits read in  the lookup table */
994     DumpBits( &p_vpar->bit_stream, p_vpar->pl_coded_pattern[i_vlc].i_length );
995     
996     /* return the value from the vlc table */
997     return p_vpar->pl_coded_pattern[i_vlc].i_value;
998 }
999
1000 /*****************************************************************************
1001  * vpar_CodedPattern422 : coded_block_pattern with 422 chroma
1002  *****************************************************************************/
1003 int vpar_CodedPattern422( vpar_thread_t * p_vpar )
1004 {
1005     int      i_vlc = ShowBits( &p_vpar->bit_stream, 9 );
1006     
1007     /* Supplementary 2 bits long code for 422 format */
1008     int      i_coded_block_pattern_1;
1009     
1010     DumpBits( &p_vpar->bit_stream, p_vpar->pl_coded_pattern[i_vlc].i_length );
1011     i_coded_block_pattern_1 = GetBits( &p_vpar->bit_stream, 2 );
1012     
1013     /* the code is just to be added to the value found in the table */
1014     return p_vpar->pl_coded_pattern[i_vlc].i_value |
1015            (i_coded_block_pattern_1 << 6);
1016 }
1017
1018 /*****************************************************************************
1019  * vpar_CodedPattern444 : coded_block_pattern with 444 chroma
1020  *****************************************************************************/
1021 int vpar_CodedPattern444( vpar_thread_t * p_vpar )
1022 {
1023     int      i_vlc = ShowBits( &p_vpar->bit_stream, 9 );
1024     int      i_coded_block_pattern_2;
1025     
1026     DumpBits( &p_vpar->bit_stream, p_vpar->pl_coded_pattern[i_vlc].i_length );
1027     i_coded_block_pattern_2 = GetBits( &p_vpar->bit_stream, 6 );
1028     
1029     return p_vpar->pl_coded_pattern[i_vlc].i_value |
1030            ( i_coded_block_pattern_2 << 6 );
1031 }
1032
1033 /*****************************************************************************
1034  * vpar_DecodeMPEG1Non : decode MPEG-1 non-intra blocks
1035  *****************************************************************************/
1036 static void vpar_DecodeMPEG1Non( vpar_thread_t * p_vpar, macroblock_t * p_mb, int i_b )
1037 {
1038
1039     if( p_vpar->picture.i_coding_type == D_CODING_TYPE )
1040     {
1041         /* Remove end_of_macroblock (always 1, prevents startcode emulation)
1042          * ISO/IEC 11172-2 section 2.4.2.7 and 2.4.3.6 */
1043         DumpBits( &p_vpar->bit_stream, 1 );
1044     }
1045 }
1046
1047 /*****************************************************************************
1048  * vpar_DecodeMPEG1Intra : decode MPEG-1 intra blocks
1049  *****************************************************************************/
1050 static void vpar_DecodeMPEG1Intra( vpar_thread_t * p_vpar, macroblock_t * p_mb, int i_b )
1051 {
1052
1053     if( p_vpar->picture.i_coding_type == D_CODING_TYPE )
1054     {
1055         /* Remove end_of_macroblock (always 1, prevents startcode emulation)
1056          * ISO/IEC 11172-2 section 2.4.2.7 and 2.4.3.6 */
1057         DumpBits( &p_vpar->bit_stream, 1 );
1058     }
1059 }
1060
1061 /*****************************************************************************
1062  * vpar_DecodeMPEG2Non : decode MPEG-2 non-intra blocks
1063  *****************************************************************************/
1064 static void vpar_DecodeMPEG2Non( vpar_thread_t * p_vpar, macroblock_t * p_mb, int i_b )
1065 {
1066     int         i_dummy;
1067     int         i_code;
1068     int         i_nc;
1069     int         i_coef;
1070     int         i_type;
1071     int         i_select;
1072     int         i_offset;
1073     int         i_run;
1074     int         i_level;
1075     boolean_t   b_intra;
1076     boolean_t   b_sign;
1077     /* Lookup table for the offset in the tables for ac coef decoding */
1078     static lookup_t pl_offset_table[8] = { { 12, 4 }, { 8, 4 }, { 6, 8 },
1079                                            { 4, 16 }, { 3, 16 }, { 2, 16 },
1080                                            { 1, 16 }, { 0, 16 } };
1081     /* There is no special decodding for DC coefficient in non intra blocs
1082      * except that we won't use exactly the same table B.14 note 3 & 4 */
1083     /* Decoding of the coefficients */
1084
1085 #ifndef VDEC_DCT
1086     i_nc = 1;
1087 #else
1088     i_nc = 0;
1089 #endif
1090
1091     b_intra = p_vpar->picture.b_intra_vlc_format;
1092     for( i_dummy = 1; i_dummy < 64; i_dummy++ )
1093     {
1094         i_code = ShowBits( &p_vpar->bit_stream, 16 );
1095         i_run = (*p_vpar->pppl_dct_coef[b_intra][i_code]).i_run;
1096         DumpBits( &p_vpar->bit_stream,
1097                   (*p_vpar->pppl_dct_coef[b_intra][i_code]).i_length );
1098         switch( i_run )
1099         {
1100             case DCT_ESCAPE:
1101                 i_run = GetBits( &p_vpar->bit_stream, 6 );
1102                 i_level = GetBits( &p_vpar->bit_stream, 12 );
1103                 p_mb->ppi_blocks[i_b][i_dummy] = ( b_sign = ( i_level > 2047 ) ) 
1104                                                           ? ( -4096 + i_level )
1105                                                           : i_level;
1106                 i_coef = i_dummy;
1107                 i_dummy += i_run;
1108                 i_nc ++;
1109                 break;
1110             case DCT_EOB:
1111                 i_dummy = 64;
1112                 break;
1113             default:
1114                 i_level = (*p_vpar->pppl_dct_coef[b_intra][i_code]).i_level;
1115                 b_sign = GetBits( &p_vpar->bit_stream, 1 );
1116                 p_mb->ppi_blocks[i_b][i_dummy] = b_sign ? -i_level : i_level;
1117                 i_coef = i_dummy;
1118                 i_dummy += i_run;
1119                 i_nc ++;
1120         }
1121     }
1122     if( i_nc == 1 )
1123     {
1124         p_mb->pf_idct[i_b] = vdec_SparseIDCT;
1125         p_mb->pi_sparse_pos[i_b] = i_coef;
1126     }
1127     else
1128     {
1129         p_mb->pf_idct[i_b] = vdec_IDCT;
1130     }
1131 }
1132
1133 /*****************************************************************************
1134  * vpar_DecodeMPEG2Intra : decode MPEG-2 intra blocks
1135  *****************************************************************************/
1136 static void vpar_DecodeMPEG2Intra( vpar_thread_t * p_vpar, macroblock_t * p_mb, int i_b )
1137 {
1138     int         i_dummy;
1139     int         i_nc;
1140     int         i_cc;
1141     int         i_coef;
1142     int         i_type;
1143     int         i_code;
1144     int         i_code5;
1145     int         i_select;
1146     int         i_offset;
1147     int         i_pos;
1148     int         i_dct_dc_size;
1149     int         i_dct_dc_diff;
1150     int         i_run;
1151     int         i_level;
1152     boolean_t   b_intra;
1153     boolean_t   b_sign;
1154     
1155     /* Lookup Table for the chromatic component */
1156     static int pi_cc_index[12] = { 0, 0, 0, 0, 1, 2, 1, 2, 1, 2 };
1157     
1158     /* Lookup table for the offset in the tables for ac coef decoding */
1159     static lookup_t pl_offset_table[8] = { { 12, 4 }, { 8, 4 }, { 6, 8 },
1160                                            { 4, 16 }, { 3, 16 }, { 2, 16 },
1161                                            { 1, 16 }, { 0, 16 } };
1162     i_cc = pi_cc_index[i_b];
1163     
1164     /* Determine whether it is luminance or not (chrominance) */
1165     i_type = ( i_cc + 1 ) >> 1;
1166
1167     /* Decoding of the DC intra coefficient */
1168     /* The nb of bits to parse depends on i_type */
1169     i_code = ShowBits( &p_vpar->bit_stream, 9 + i_type );
1170     
1171     /* To reduce memory occupation, there are two lookup tables
1172      * See InitDCT above */
1173     i_code5 = i_code >> 4;
1174     
1175     /* Shall we lookup in the first or in the second table ? */
1176     i_select = ( i_code5 - 1 ) / 31;
1177     /* Offset value for looking in the second table */
1178     i_offset = 0x1f0 + ( i_type * 0x1f0 );
1179     i_pos = ( i_code5 * ( ! i_select ) ) + ( ( i_code - i_offset ) * i_select );
1180     i_dct_dc_size = p_vpar->pppl_dct_dc_size[i_type][i_select][i_pos].i_value;
1181     
1182     /* Dump the variable length code */
1183     DumpBits( &p_vpar->bit_stream, 
1184               p_vpar->pppl_dct_dc_size[i_type][i_select][i_pos].i_length );
1185     
1186     /* Read the actual code with the good length */
1187     i_dct_dc_diff = GetBits( &p_vpar->bit_stream, i_dct_dc_size );
1188     p_vpar->slice.pi_dc_dct_pred[i_cc] += i_dct_dc_diff;
1189
1190 #ifndef VDEC_DFT
1191     p_mb->ppi_blocks[i_b][0] = ( p_vpar->slice.pi_dc_dct_pred[i_cc] <<
1192                                ( 3 - p_vpar->picture.i_intra_dc_precision ) );
1193     i_nc = 1;
1194 #else
1195     p_mb->ppi_blocks[i_b][0] = ( p_vpar->slice.pi_dc_dct_pred[i_cc] <<
1196                                ( 11 - p_vpar->picture.i_intra_dc_precision ) );
1197     i_nc = ( p_vpar->slice.pi_dc_dct_pred[i_cc] != 0 );
1198 #endif
1199
1200     /* Decoding of the AC coefficients */
1201     
1202     i_coef = 0;
1203     b_intra = p_vpar->picture.b_intra_vlc_format;
1204     for( i_dummy = 1; i_dummy < 64; i_dummy++ )
1205     {
1206         i_code = ShowBits( &p_vpar->bit_stream, 16 );
1207         i_run = (*p_vpar->pppl_dct_coef[b_intra][i_code]).i_run;
1208         DumpBits( &p_vpar->bit_stream,
1209                   (*p_vpar->pppl_dct_coef[b_intra][i_code]).i_length );
1210         switch( i_run )
1211         {
1212             case DCT_ESCAPE:
1213                 i_run = GetBits( &p_vpar->bit_stream, 6 );
1214                 i_level = GetBits( &p_vpar->bit_stream, 12 );
1215                 p_mb->ppi_blocks[i_b][i_dummy] = ( b_sign = ( i_level > 2047 ) ) 
1216                                                           ? ( -4096 + i_level )
1217                                                           : i_level;
1218                 i_coef = i_dummy;
1219                 i_dummy += i_run;
1220                 i_nc ++;
1221                 break;
1222             case DCT_EOB:
1223                 i_dummy = 64;
1224                 break;
1225             default:
1226                 i_level = (*p_vpar->pppl_dct_coef[b_intra][i_code]).i_level;
1227                 b_sign = GetBits( &p_vpar->bit_stream, 1 );
1228                 p_mb->ppi_blocks[i_b][i_dummy] = b_sign ? -i_level : i_level;
1229                 i_coef = i_dummy;
1230                 i_dummy += i_run;
1231                 i_nc ++;
1232         }
1233     }
1234     if( i_nc == 1 )
1235     {
1236         p_mb->pf_idct[i_b] = vdec_SparseIDCT;
1237         p_mb->pi_sparse_pos[i_b] = i_coef;
1238     }
1239     else
1240     {
1241         p_mb->pf_idct[i_b] = vdec_IDCT;
1242     }
1243 }