1 /*****************************************************************************
2 * vpar_blocks.c : blocks parsing
4 *****************************************************************************/
6 /*****************************************************************************
8 *****************************************************************************/
16 #include <X11/extensions/XShm.h>
21 #include "vlc_thread.h"
24 #include "debug.h" /* ?? temporaire, requis par netlist.h */
27 #include "input_netlist.h"
28 #include "decoder_fifo.h"
30 #include "video_output.h"
32 #include "vdec_idct.h"
33 #include "video_decoder.h"
34 #include "vdec_motion.h"
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"
45 typedef void (*f_decode_block_t)( vpar_thread_t *, macroblock_t *, int );
46 static void vpar_DecodeMPEG1Non( vpar_thread_t * p_vpar, macroblock_t * p_mb, int i_b );
47 static void vpar_DecodeMPEG1Intra( vpar_thread_t * p_vpar, macroblock_t * p_mb, int i_b );
48 static void vpar_DecodeMPEG2Non( vpar_thread_t * p_vpar, macroblock_t * p_mb, int i_b );
49 static void vpar_DecodeMPEG2Intra( vpar_thread_t * p_vpar, macroblock_t * p_mb, int i_b );
52 * Initialisation tables
54 /* Table for coded_block_pattern resolution */
55 static lookup_t pl_coded_pattern_init_table[512] =
56 { {MB_ERROR, 0}, {0, 9}, {39, 9}, {27, 9}, {59, 9}, {55, 9}, {47, 9}, {31, 9},
57 {58, 8}, {58, 8}, {54, 8}, {54, 8}, {46, 8}, {46, 8}, {30, 8}, {30, 8},
58 {57, 8}, {57, 8}, {53, 8}, {53, 8}, {45, 8}, {45, 8}, {29, 8}, {29, 8},
59 {38, 8}, {38, 8}, {26, 8}, {26, 8}, {37, 8}, {37, 8}, {25, 8}, {25, 8},
60 {43, 8}, {43, 8}, {23, 8}, {23, 8}, {51, 8}, {51, 8}, {15, 8}, {15, 8},
61 {42, 8}, {42, 8}, {22, 8}, {22, 8}, {50, 8}, {50, 8}, {14, 8}, {14, 8},
62 {41, 8}, {41, 8}, {21, 8}, {21, 8}, {49, 8}, {49, 8}, {13, 8}, {13, 8},
63 {35, 8}, {35, 8}, {19, 8}, {19, 8}, {11, 8}, {11, 8}, {7, 8}, {7, 8},
64 {34, 7}, {34, 7}, {34, 7}, {34, 7}, {18, 7}, {18, 7}, {18, 7}, {18, 7},
65 {10, 7}, {10, 7}, {10, 7}, {10, 7}, {6, 7}, {6, 7}, {6, 7}, {6, 7},
66 {33, 7}, {33, 7}, {33, 7}, {33, 7}, {17, 7}, {17, 7}, {17, 7}, {17, 7},
67 {9, 7}, {9, 7}, {9, 7}, {9, 7}, {5, 7}, {5, 7}, {5, 7}, {5, 7},
68 {63, 6}, {63, 6}, {63, 6}, {63, 6}, {63, 6}, {63, 6}, {63, 6}, {63, 6},
69 {3, 6}, {3, 6}, {3, 6}, {3, 6}, {3, 6}, {3, 6}, {3, 6}, {3, 6},
70 {36, 6}, {36, 6}, {36, 6}, {36, 6}, {36, 6}, {36, 6}, {36, 6}, {36, 6},
71 {24, 6}, {24, 6}, {24, 6}, {24, 6}, {24, 6}, {24, 6}, {24, 6}, {24, 6},
72 {62, 5}, {62, 5}, {62, 5}, {62, 5}, {62, 5}, {62, 5}, {62, 5}, {62, 5},
73 {62, 5}, {62, 5}, {62, 5}, {62, 5}, {62, 5}, {62, 5}, {62, 5}, {62, 5},
74 {2, 5}, {2, 5}, {2, 5}, {2, 5}, {2, 5}, {2, 5}, {2, 5}, {2, 5},
75 {2, 5}, {2, 5}, {2, 5}, {2, 5}, {2, 5}, {2, 5}, {2, 5}, {2, 5},
76 {61, 5}, {61, 5}, {61, 5}, {61, 5}, {61, 5}, {61, 5}, {61, 5}, {61, 5},
77 {61, 5}, {61, 5}, {61, 5}, {61, 5}, {61, 5}, {61, 5}, {61, 5}, {61, 5},
78 {1, 5}, {1, 5}, {1, 5}, {1, 5}, {1, 5}, {1, 5}, {1, 5}, {1, 5},
79 {1, 5}, {1, 5}, {1, 5}, {1, 5}, {1, 5}, {1, 5}, {1, 5}, {1, 5},
80 {56, 5}, {56, 5}, {56, 5}, {56, 5}, {56, 5}, {56, 5}, {56, 5}, {56, 5},
81 {56, 5}, {56, 5}, {56, 5}, {56, 5}, {56, 5}, {56, 5}, {56, 5}, {56, 5},
82 {52, 5}, {52, 5}, {52, 5}, {52, 5}, {52, 5}, {52, 5}, {52, 5}, {52, 5},
83 {52, 5}, {52, 5}, {52, 5}, {52, 5}, {52, 5}, {52, 5}, {52, 5}, {52, 5},
84 {44, 5}, {44, 5}, {44, 5}, {44, 5}, {44, 5}, {44, 5}, {44, 5}, {44, 5},
85 {44, 5}, {44, 5}, {44, 5}, {44, 5}, {44, 5}, {44, 5}, {44, 5}, {44, 5},
86 {28, 5}, {28, 5}, {28, 5}, {28, 5}, {28, 5}, {28, 5}, {28, 5}, {28, 5},
87 {28, 5}, {28, 5}, {28, 5}, {28, 5}, {28, 5}, {28, 5}, {28, 5}, {28, 5},
88 {40, 5}, {40, 5}, {40, 5}, {40, 5}, {40, 5}, {40, 5}, {40, 5}, {40, 5},
89 {40, 5}, {40, 5}, {40, 5}, {40, 5}, {40, 5}, {40, 5}, {40, 5}, {40, 5},
90 {20, 5}, {20, 5}, {20, 5}, {20, 5}, {20, 5}, {20, 5}, {20, 5}, {20, 5},
91 {20, 5}, {20, 5}, {20, 5}, {20, 5}, {20, 5}, {20, 5}, {20, 5}, {20, 5},
92 {48, 5}, {48, 5}, {48, 5}, {48, 5}, {48, 5}, {48, 5}, {48, 5}, {48, 5},
93 {48, 5}, {48, 5}, {48, 5}, {48, 5}, {48, 5}, {48, 5}, {48, 5}, {48, 5},
94 {12, 5}, {12, 5}, {12, 5}, {12, 5}, {12, 5}, {12, 5}, {12, 5}, {12, 5},
95 {12, 5}, {12, 5}, {12, 5}, {12, 5}, {12, 5}, {12, 5}, {12, 5}, {12, 5},
96 {32, 4}, {32, 4}, {32, 4}, {32, 4}, {32, 4}, {32, 4}, {32, 4}, {32, 4},
97 {32, 4}, {32, 4}, {32, 4}, {32, 4}, {32, 4}, {32, 4}, {32, 4}, {32, 4},
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 {16, 4}, {16, 4}, {16, 4}, {16, 4}, {16, 4}, {16, 4}, {16, 4}, {16, 4},
101 {16, 4}, {16, 4}, {16, 4}, {16, 4}, {16, 4}, {16, 4}, {16, 4}, {16, 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 {8, 4}, {8, 4}, {8, 4}, {8, 4}, {8, 4}, {8, 4}, {8, 4}, {8, 4},
105 {8, 4}, {8, 4}, {8, 4}, {8, 4}, {8, 4}, {8, 4}, {8, 4}, {8, 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 {4, 4}, {4, 4}, {4, 4}, {4, 4}, {4, 4}, {4, 4}, {4, 4}, {4, 4},
109 {4, 4}, {4, 4}, {4, 4}, {4, 4}, {4, 4}, {4, 4}, {4, 4}, {4, 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 {60, 3}, {60, 3}, {60, 3}, {60, 3}, {60, 3}, {60, 3}, {60, 3}, {60, 3},
113 {60, 3}, {60, 3}, {60, 3}, {60, 3}, {60, 3}, {60, 3}, {60, 3}, {60, 3},
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} };
121 /* Tables for dc DCT coefficients
122 * Tables are cut in two parts to reduce memory occupation
125 /* Table B-12, dct_dc_size_luminance, codes 00xxx ... 11110 */
126 static lookup_t pl_dct_dc_lum_init_table_1[32] =
127 { {1, 2}, {1, 2}, {1, 2}, {1, 2}, {1, 2}, {1, 2}, {1, 2}, {1, 2},
128 {2, 2}, {2, 2}, {2, 2}, {2, 2}, {2, 2}, {2, 2}, {2, 2}, {2, 2},
129 {0, 3}, {0, 3}, {0, 3}, {0, 3}, {3, 3}, {3, 3}, {3, 3}, {3, 3},
130 {4, 3}, {4, 3}, {4, 3}, {4, 3}, {5, 4}, {5, 4}, {6, 5}, {MB_ERROR, 0}
133 /* Table B-12, dct_dc_size_luminance, codes 111110xxx ... 111111111 */
134 static lookup_t pl_dct_dc_lum_init_table_2[32] =
135 { {7, 6}, {7, 6}, {7, 6}, {7, 6}, {7, 6}, {7, 6}, {7, 6}, {7, 6},
136 {8, 7}, {8, 7}, {8, 7}, {8, 7}, {9, 8}, {9, 8}, {10,9}, {11,9},
137 {MB_ERROR, 0}, {MB_ERROR, 0}, {MB_ERROR, 0}, {MB_ERROR, 0},
138 {MB_ERROR, 0}, {MB_ERROR, 0}, {MB_ERROR, 0}, {MB_ERROR, 0},
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}
143 /* Table B-13, dct_dc_size_chrominance, codes 00xxx ... 11110 */
144 static lookup_t pl_dct_dc_chrom_init_table_1[32] =
145 { {0, 2}, {0, 2}, {0, 2}, {0, 2}, {0, 2}, {0, 2}, {0, 2}, {0, 2},
146 {1, 2}, {1, 2}, {1, 2}, {1, 2}, {1, 2}, {1, 2}, {1, 2}, {1, 2},
147 {2, 2}, {2, 2}, {2, 2}, {2, 2}, {2, 2}, {2, 2}, {2, 2}, {2, 2},
148 {3, 3}, {3, 3}, {3, 3}, {3, 3}, {4, 4}, {4, 4}, {5, 5}, {MB_ERROR, 0}
151 /* Table B-13, dct_dc_size_chrominance, codes 111110xxxx ... 1111111111 */
152 static lookup_t pl_dct_dc_chrom_init_table_2[32] =
153 { {6, 6}, {6, 6}, {6, 6}, {6, 6}, {6, 6}, {6, 6}, {6, 6}, {6, 6},
154 {6, 6}, {6, 6}, {6, 6}, {6, 6}, {6, 6}, {6, 6}, {6, 6}, {6, 6},
155 {7, 7}, {7, 7}, {7, 7}, {7, 7}, {7, 7}, {7, 7}, {7, 7}, {7, 7},
156 {8, 8}, {8, 8}, {8, 8}, {8, 8}, {9, 9}, {9, 9}, {10,10}, {11,10}
160 * Structure to store the tables B14 & B15
161 * Is constructed from the tables below
163 dct_lookup_t ppl_dct_coef[2][16384];
166 /* Tables for ac DCT coefficients. There are cut in many parts to save space */
167 /* Table B-14, DCT coefficients table zero,
168 * codes 0100 ... 1xxx (used for first (DC) coefficient)
170 static dct_lookup_t pl_DCT_tab_dc[12] =
172 {0,2,4}, {2,1,4}, {1,1,3}, {1,1,3},
173 {0,1,1}, {0,1,1}, {0,1,1}, {0,1,1},
174 {0,1,1}, {0,1,1}, {0,1,1}, {0,1,1}
177 /* Table B-14, DCT coefficients table zero,
178 * codes 0100 ... 1xxx (used for all other coefficients)
180 static dct_lookup_t pl_DCT_tab_ac[12] =
182 {0,2,4}, {2,1,4}, {1,1,3}, {1,1,3},
183 {DCT_EOB,0,2}, {DCT_EOB,0,2}, {DCT_EOB,0,2}, {DCT_EOB,0,2}, /* EOB */
184 {0,1,2}, {0,1,2}, {0,1,2}, {0,1,2}
187 /* Table B-14, DCT coefficients table zero,
188 * codes 000001xx ... 00111xxx
190 static dct_lookup_t pl_DCT_tab0[60] =
192 {DCT_ESCAPE,0,6}, {DCT_ESCAPE,0,6}, {DCT_ESCAPE,0,6}, {DCT_ESCAPE,0,6},
194 {2,2,7}, {2,2,7}, {9,1,7}, {9,1,7},
195 {0,4,7}, {0,4,7}, {8,1,7}, {8,1,7},
196 {7,1,6}, {7,1,6}, {7,1,6}, {7,1,6},
197 {6,1,6}, {6,1,6}, {6,1,6}, {6,1,6},
198 {1,2,6}, {1,2,6}, {1,2,6}, {1,2,6},
199 {5,1,6}, {5,1,6}, {5,1,6}, {5,1,6},
200 {13,1,8}, {0,6,8}, {12,1,8}, {11,1,8},
201 {3,2,8}, {1,3,8}, {0,5,8}, {10,1,8},
202 {0,3,5}, {0,3,5}, {0,3,5}, {0,3,5},
203 {0,3,5}, {0,3,5}, {0,3,5}, {0,3,5},
204 {4,1,5}, {4,1,5}, {4,1,5}, {4,1,5},
205 {4,1,5}, {4,1,5}, {4,1,5}, {4,1,5},
206 {3,1,5}, {3,1,5}, {3,1,5}, {3,1,5},
207 {3,1,5}, {3,1,5}, {3,1,5}, {3,1,5}
210 /* Table B-15, DCT coefficients table one,
211 * codes 000001xx ... 11111111
213 static dct_lookup_t pl_DCT_tab0a[252] =
215 {65,0,6}, {65,0,6}, {65,0,6}, {65,0,6}, /* Escape */
216 {7,1,7}, {7,1,7}, {8,1,7}, {8,1,7},
217 {6,1,7}, {6,1,7}, {2,2,7}, {2,2,7},
218 {0,7,6}, {0,7,6}, {0,7,6}, {0,7,6},
219 {0,6,6}, {0,6,6}, {0,6,6}, {0,6,6},
220 {4,1,6}, {4,1,6}, {4,1,6}, {4,1,6},
221 {5,1,6}, {5,1,6}, {5,1,6}, {5,1,6},
222 {1,5,8}, {11,1,8}, {0,11,8}, {0,10,8},
223 {13,1,8}, {12,1,8}, {3,2,8}, {1,4,8},
224 {2,1,5}, {2,1,5}, {2,1,5}, {2,1,5},
225 {2,1,5}, {2,1,5}, {2,1,5}, {2,1,5},
226 {1,2,5}, {1,2,5}, {1,2,5}, {1,2,5},
227 {1,2,5}, {1,2,5}, {1,2,5}, {1,2,5},
228 {3,1,5}, {3,1,5}, {3,1,5}, {3,1,5},
229 {3,1,5}, {3,1,5}, {3,1,5}, {3,1,5},
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 {1,1,3}, {1,1,3}, {1,1,3}, {1,1,3},
238 {64,0,4}, {64,0,4}, {64,0,4}, {64,0,4}, /* EOB */
239 {64,0,4}, {64,0,4}, {64,0,4}, {64,0,4},
240 {64,0,4}, {64,0,4}, {64,0,4}, {64,0,4},
241 {64,0,4}, {64,0,4}, {64,0,4}, {64,0,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,3,4}, {0,3,4}, {0,3,4}, {0,3,4},
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,1,2}, {0,1,2}, {0,1,2}, {0,1,2},
261 {0,1,2}, {0,1,2}, {0,1,2}, {0,1,2},
262 {0,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,2,3}, {0,2,3}, {0,2,3}, {0,2,3},
269 {0,2,3}, {0,2,3}, {0,2,3}, {0,2,3},
270 {0,4,5}, {0,4,5}, {0,4,5}, {0,4,5},
271 {0,4,5}, {0,4,5}, {0,4,5}, {0,4,5},
272 {0,5,5}, {0,5,5}, {0,5,5}, {0,5,5},
273 {0,5,5}, {0,5,5}, {0,5,5}, {0,5,5},
274 {9,1,7}, {9,1,7}, {1,3,7}, {1,3,7},
275 {10,1,7}, {10,1,7}, {0,8,7}, {0,8,7},
276 {0,9,7}, {0,9,7}, {0,12,8}, {0,13,8},
277 {2,3,8}, {4,2,8}, {0,14,8}, {0,15,8}
280 /* Table B-14, DCT coefficients table zero,
281 * codes 0000001000 ... 0000001111
283 static dct_lookup_t pl_DCT_tab1[8] =
285 {16,1,10}, {5,2,10}, {0,7,10}, {2,3,10},
286 {1,4,10}, {15,1,10}, {14,1,10}, {4,2,10}
289 /* Table B-15, DCT coefficients table one,
290 * codes 000000100x ... 000000111x
292 static dct_lookup_t pl_DCT_tab1a[8] =
294 {5,2,9}, {5,2,9}, {14,1,9}, {14,1,9},
295 {2,4,10}, {16,1,10}, {15,1,9}, {15,1,9}
298 /* Table B-14/15, DCT coefficients table zero / one,
299 * codes 000000010000 ... 000000011111
301 static dct_lookup_t pl_DCT_tab2[16] =
303 {0,11,12}, {8,2,12}, {4,3,12}, {0,10,12},
304 {2,4,12}, {7,2,12}, {21,1,12}, {20,1,12},
305 {0,9,12}, {19,1,12}, {18,1,12}, {1,5,12},
306 {3,3,12}, {0,8,12}, {6,2,12}, {17,1,12}
309 /* Table B-14/15, DCT coefficients table zero / one,
310 * codes 0000000010000 ... 0000000011111
312 static dct_lookup_t pl_DCT_tab3[16] =
314 {10,2,13}, {9,2,13}, {5,3,13}, {3,4,13},
315 {2,5,13}, {1,7,13}, {1,6,13}, {0,15,13},
316 {0,14,13}, {0,13,13}, {0,12,13}, {26,1,13},
317 {25,1,13}, {24,1,13}, {23,1,13}, {22,1,13}
320 /* Table B-14/15, DCT coefficients table zero / one,
321 * codes 00000000010000 ... 00000000011111
323 static dct_lookup_t pl_DCT_tab4[16] =
325 {0,31,14}, {0,30,14}, {0,29,14}, {0,28,14},
326 {0,27,14}, {0,26,14}, {0,25,14}, {0,24,14},
327 {0,23,14}, {0,22,14}, {0,21,14}, {0,20,14},
328 {0,19,14}, {0,18,14}, {0,17,14}, {0,16,14}
331 /* Table B-14/15, DCT coefficients table zero / one,
332 * codes 000000000010000 ... 000000000011111
334 static dct_lookup_t pl_DCT_tab5[16] =
336 {0,40,15}, {0,39,15}, {0,38,15}, {0,37,15},
337 {0,36,15}, {0,35,15}, {0,34,15}, {0,33,15},
338 {0,32,15}, {1,14,15}, {1,13,15}, {1,12,15},
339 {1,11,15}, {1,10,15}, {1,9,15}, {1,8,15}
342 /* Table B-14/15, DCT coefficients table zero / one,
343 * codes 0000000000010000 ... 0000000000011111
345 static dct_lookup_t pl_DCT_tab6[16] =
347 {1,18,16}, {1,17,16}, {1,16,16}, {1,15,16},
348 {6,3,16}, {16,2,16}, {15,2,16}, {14,2,16},
349 {13,2,16}, {12,2,16}, {11,2,16}, {31,1,16},
350 {30,1,16}, {29,1,16}, {28,1,16}, {27,1,16}
353 * Initialization of lookup tables
356 /*****************************************************************************
357 * vpar_InitCrop : Initialize the crop table for saturation
358 * (ISO/IEC 13818-2 section 7.4.3)
359 *****************************************************************************/
360 #if defined(MPEG2_COMPLIANT) && !defined(VDEC_DFT)
361 void vpar_InitCrop( vpar_thread_t * p_vpar )
365 p_vpar->pi_crop = p_vpar->pi_crop_buf + 4096;
367 for( i_dummy = -4096; i_dummy < -2048; i_dummy++ )
369 p_vpar->pi_crop[i_dummy] = -2048;
371 for( ; i_dummy < 2047; i_dummy++ )
373 p_vpar->pi_crop[i_dummy] = i_dummy;
375 for( ; i_dummy < 4095; i_dummy++ )
377 p_vpar->pi_crop[i_dummy] = 2047;
382 /*****************************************************************************
383 * InitMbAddrInc : Initialize the lookup table for mb_addr_inc
384 *****************************************************************************/
386 /* Function for filling up the lookup table for mb_addr_inc */
387 static void __inline__ FillMbAddrIncTable( vpar_thread_t * p_vpar,
388 int i_start, int i_end, int i_step,
389 int * pi_value, int i_length )
392 for( i_pos = i_start ; i_pos < i_end ; i_pos += i_step )
394 for( i_offset = 0 ; i_offset < i_step ; i_offset ++ )
396 p_vpar->pl_mb_addr_inc[i_pos + i_offset].i_value = * pi_value;
397 p_vpar->pl_mb_addr_inc[i_pos + i_offset].i_length = i_length;
403 /* Function that initialize the table using the last one */
404 void vpar_InitMbAddrInc( vpar_thread_t * p_vpar )
409 for( i_dummy = 0 ; i_dummy < 8 ; i_dummy++ )
411 p_vpar->pl_mb_addr_inc[i_dummy].i_value = MB_ERROR;
412 p_vpar->pl_mb_addr_inc[i_dummy].i_length = 0;
415 p_vpar->pl_mb_addr_inc[8].i_value = MB_ADDRINC_ESCAPE;
416 p_vpar->pl_mb_addr_inc[8].i_length = 11;
418 for( i_dummy = 9 ; i_dummy < 15 ; i_dummy ++ )
420 p_vpar->pl_mb_addr_inc[i_dummy].i_value = MB_ERROR;
421 p_vpar->pl_mb_addr_inc[i_dummy].i_length = 0;
424 p_vpar->pl_mb_addr_inc[15].i_value = MB_ADDRINC_STUFFING;
425 p_vpar->pl_mb_addr_inc[15].i_length = 11;
427 for( i_dummy = 16; i_dummy < 24; i_dummy++ )
429 p_vpar->pl_mb_addr_inc[i_dummy].i_value = MB_ERROR;
430 p_vpar->pl_mb_addr_inc[i_dummy].i_length = 0;
435 FillMbAddrIncTable( p_vpar, 24, 36, 1, &i_value, 11 );
436 FillMbAddrIncTable( p_vpar, 36, 48, 2, &i_value, 10 );
437 FillMbAddrIncTable( p_vpar, 48, 96, 8, &i_value, 8 );
438 FillMbAddrIncTable( p_vpar, 96, 128, 16, &i_value, 7 );
439 FillMbAddrIncTable( p_vpar, 128, 256, 64, &i_value, 5 );
440 FillMbAddrIncTable( p_vpar, 256, 512, 128, &i_value, 4 );
441 FillMbAddrIncTable( p_vpar, 512, 1024, 256, &i_value, 3 );
442 FillMbAddrIncTable( p_vpar, 1024, 2048, 1024, &i_value, 1 );
445 /*****************************************************************************
446 * Init*MBType : Initialize lookup table for the Macroblock type
447 *****************************************************************************/
449 /* Fonction for filling up the tables */
450 static void __inline__ FillMBType( vpar_thread_t * p_vpar,
459 for( i_dummy = i_start ; i_dummy < i_end ; i_dummy++ )
461 p_vpar->ppl_mb_type[i_mb_type][i_dummy].i_value = i_value;
462 p_vpar->ppl_mb_type[i_mb_type][i_dummy].i_length = i_length;
466 /* Fonction that fills the table for P MB_Type */
467 void vpar_InitPMBType( vpar_thread_t * p_vpar )
469 FillMBType( p_vpar, 0, 32, 64, MB_MOTION_FORWARD|MB_PATTERN, 1 );
470 FillMBType( p_vpar, 0, 16, 32, MB_PATTERN, 2 );
471 FillMBType( p_vpar, 0, 8, 16, MB_MOTION_FORWARD, 3 );
472 FillMBType( p_vpar, 0, 6, 8, MB_INTRA, 5 );
473 FillMBType( p_vpar, 0, 4, 6, MB_QUANT|MB_MOTION_FORWARD|MB_PATTERN, 5 );
474 FillMBType( p_vpar, 0, 2, 4, MB_QUANT|MB_PATTERN, 5 );
475 p_vpar->ppl_mb_type[0][1].i_value = MB_QUANT|MB_INTRA;
476 p_vpar->ppl_mb_type[0][1].i_length = 6;
477 p_vpar->ppl_mb_type[0][0].i_value = MB_ERROR;
478 p_vpar->ppl_mb_type[0][0].i_length = 0;
481 /* Fonction that fills the table for B MB_Type */
482 void vpar_InitBMBType( vpar_thread_t * p_vpar )
484 FillMBType( p_vpar, 1, 48, 64, MB_MOTION_FORWARD
485 |MB_MOTION_BACKWARD|MB_PATTERN, 2 );
486 FillMBType( p_vpar, 1, 32, 48, MB_MOTION_FORWARD|MB_MOTION_BACKWARD, 2 );
487 FillMBType( p_vpar, 1, 24, 32, MB_MOTION_BACKWARD|MB_PATTERN, 3 );
488 FillMBType( p_vpar, 1, 16, 24, MB_MOTION_BACKWARD, 3 );
489 FillMBType( p_vpar, 1, 12, 16, MB_MOTION_FORWARD|MB_PATTERN, 4 );
490 FillMBType( p_vpar, 1, 8, 12, MB_MOTION_FORWARD, 4 );
491 FillMBType( p_vpar, 1, 6, 8, MB_INTRA, 5 );
492 FillMBType( p_vpar, 1, 4, 6, MB_QUANT|MB_MOTION_FORWARD
493 |MB_MOTION_BACKWARD|MB_PATTERN, 5 );
494 p_vpar->ppl_mb_type[1][3].i_value = MB_QUANT|MB_MOTION_FORWARD|MB_PATTERN;
495 p_vpar->ppl_mb_type[1][3].i_length = 6;
496 p_vpar->ppl_mb_type[1][2].i_value = MB_QUANT|MB_MOTION_BACKWARD|MB_PATTERN;
497 p_vpar->ppl_mb_type[1][2].i_length = 6;
498 p_vpar->ppl_mb_type[1][1].i_value = MB_QUANT|MB_INTRA;
499 p_vpar->ppl_mb_type[1][1].i_length = 6;
500 p_vpar->ppl_mb_type[1][0].i_value =MB_ERROR;
501 p_vpar->ppl_mb_type[1][0].i_length = 0;
504 /*****************************************************************************
505 * InitCodedPattern : Initialize the lookup table for decoding
506 * coded block pattern
507 *****************************************************************************/
508 void vpar_InitCodedPattern( vpar_thread_t * p_vpar )
510 p_vpar->pl_coded_pattern = (lookup_t*) pl_coded_pattern_init_table;
513 /*****************************************************************************
514 * InitDCT : Initialize tables giving the length of the dct coefficient
516 *****************************************************************************/
518 /* First fonction for filling the table */
519 static void __inline__ FillDCTTable( dct_lookup_t * p_tab_dest, dct_lookup_t * p_tab_src,
520 int i_step, int i_nb_elem, int i_offset )
522 int i_dummy, i_dummy2;
524 for( i_dummy=0 ; i_dummy < i_nb_elem ; i_dummy++ )
526 for( i_dummy2=0 ; i_dummy2 < i_step ; i_dummy2++ )
528 p_tab_dest[(i_dummy+i_offset)*i_step+i_dummy2] = p_tab_src[i_dummy];
534 /* Fonction that actually fills the table or create the pointers */
535 void vpar_InitDCTTables( vpar_thread_t * p_vpar )
537 /* Tables are cut in two parts to reduce memory occupation */
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;
543 memset( ppl_dct_coef[0], MB_ERROR, 16 );
544 memset( ppl_dct_coef[1], MB_ERROR, 16 );
546 /* For table B14 & B15, we have a pointer to tables */
547 /* We fill the table thanks to the fonction defined above */
548 FillDCTTable( ppl_dct_coef[0], pl_DCT_tab0, 256, 60, 4 );
549 FillDCTTable( ppl_dct_coef[0], pl_DCT_tab1, 64, 8, 8 );
550 FillDCTTable( ppl_dct_coef[0], pl_DCT_tab2, 16, 16, 16 );
551 FillDCTTable( ppl_dct_coef[0], pl_DCT_tab3, 8, 16, 16 );
552 FillDCTTable( ppl_dct_coef[0], pl_DCT_tab4, 4, 16, 16 );
553 FillDCTTable( ppl_dct_coef[0], pl_DCT_tab5, 2, 16, 16 );
554 FillDCTTable( ppl_dct_coef[0], pl_DCT_tab6, 1, 16, 16 );
556 FillDCTTable( ppl_dct_coef[1], pl_DCT_tab0a, 256, 60, 4 );
557 FillDCTTable( ppl_dct_coef[1], pl_DCT_tab1a, 64, 8, 8 );
558 FillDCTTable( ppl_dct_coef[1], pl_DCT_tab2, 16, 16, 16 );
559 FillDCTTable( ppl_dct_coef[1], pl_DCT_tab3, 8, 16, 16 );
560 FillDCTTable( ppl_dct_coef[1], pl_DCT_tab4, 4, 16, 16 );
561 FillDCTTable( ppl_dct_coef[1], pl_DCT_tab5, 2, 16, 16 );
562 FillDCTTable( ppl_dct_coef[1], pl_DCT_tab6, 1, 16, 16 );
566 * Macroblock parsing functions
569 /*****************************************************************************
570 * InitMacroblock : Initialize macroblock values
571 *****************************************************************************/
572 static __inline__ void InitMacroblock( vpar_thread_t * p_vpar,
573 macroblock_t * p_mb )
575 static f_chroma_motion_t pf_chroma_motion[4] =
576 { NULL, vdec_Motion420, vdec_Motion422, vdec_Motion444 };
578 p_mb->p_picture = p_vpar->picture.p_picture;
579 p_mb->i_structure = p_vpar->picture.i_structure;
580 p_mb->i_current_structure = p_vpar->picture.i_current_structure;
581 p_mb->i_l_x = p_vpar->mb.i_l_x;
582 p_mb->i_l_y = p_vpar->mb.i_l_y;
583 p_mb->i_c_x = p_vpar->mb.i_c_x;
584 p_mb->i_c_y = p_vpar->mb.i_c_y;
585 p_mb->i_chroma_nb_blocks = p_vpar->sequence.i_chroma_nb_blocks;
586 p_mb->pf_chroma_motion = pf_chroma_motion[p_vpar->sequence.i_chroma_format];
587 p_mb->b_P_coding_type = ( p_vpar->picture.i_coding_type == P_CODING_TYPE );
589 p_mb->p_forward = p_vpar->sequence.p_forward;
590 p_mb->p_backward = p_vpar->sequence.p_backward;
592 p_mb->i_addb_l_stride = p_mb->i_l_stride = p_vpar->picture.i_l_stride;
593 p_mb->i_addb_c_stride = p_mb->i_c_stride = p_vpar->picture.i_c_stride;
595 /* Update macroblock real position. */
596 p_vpar->mb.i_l_x += 16;
597 p_vpar->mb.i_l_y += (p_vpar->mb.i_l_x / p_vpar->sequence.i_width)
598 * (2 - p_vpar->picture.b_frame_structure) * 16;
599 p_vpar->mb.i_l_x %= p_vpar->sequence.i_width;
601 p_vpar->mb.i_c_x += p_vpar->sequence.i_chroma_mb_width;
602 p_vpar->mb.i_c_y += (p_vpar->mb.i_c_x / p_vpar->sequence.i_chroma_width)
603 * (2 - p_vpar->picture.b_frame_structure)
604 * p_vpar->sequence.i_chroma_mb_height;
605 p_vpar->mb.i_c_x %= p_vpar->sequence.i_chroma_width;
608 /*****************************************************************************
609 * MacroblockAddressIncrement : Get the macroblock_address_increment field
610 *****************************************************************************/
611 static __inline__ int MacroblockAddressIncrement( vpar_thread_t * p_vpar )
614 /* Index in the lookup table mb_addr_inc */
615 int i_index = ShowBits( &p_vpar->bit_stream, 11 );
617 /* Test the presence of the escape character */
618 while( i_index == 8 )
620 RemoveBits( &p_vpar->bit_stream, 11 );
622 i_index = ShowBits( &p_vpar->bit_stream, 11 );
625 /* Affect the value from the lookup table */
626 i_addr_inc += p_vpar->pl_mb_addr_inc[i_index].i_value;
628 /* Dump the good number of bits */
629 RemoveBits( &p_vpar->bit_stream, p_vpar->pl_mb_addr_inc[i_index].i_length );
634 /*****************************************************************************
635 * MacroblockModes : Get the macroblock_modes structure
636 *****************************************************************************/
637 static __inline__ void MacroblockModes( vpar_thread_t * p_vpar,
638 macroblock_t * p_mb )
640 static f_motion_t pf_motion[2][4] =
641 { {NULL, vdec_FieldFieldRecon, vdec_Field16x8Recon, vdec_FieldDMVRecon},
642 {NULL, vdec_FrameFieldRecon, vdec_FrameFrameRecon, vdec_FrameDMVRecon} };
643 static int ppi_mv_count[2][4] = { {0, 1, 2, 1}, {0, 2, 1, 1} };
644 static int ppi_mv_format[2][4] = { {0, 1, 1, 1}, {0, 1, 2, 1} };
646 /* Get macroblock_type. */
647 p_vpar->mb.i_mb_type = (p_vpar->picture.pf_macroblock_type)( p_vpar );
648 p_mb->i_mb_type = p_vpar->mb.i_mb_type;
650 //fprintf( stderr, "MB type : %d\n", p_mb->i_mb_type );
652 /* SCALABILITY : warning, we don't know if spatial_temporal_weight_code
653 * has to be dropped, take care if you use scalable streams. */
654 /* RemoveBits( &p_vpar->bit_stream, 2 ); */
656 if( !(p_vpar->mb.i_mb_type & (MB_MOTION_FORWARD | MB_MOTION_BACKWARD)) )
658 /* If mb_type has neither MOTION_FORWARD nor MOTION_BACKWARD, this
659 * is useless, but also harmless. */
660 p_vpar->mb.i_motion_type = MOTION_FRAME;
664 if( p_vpar->picture.i_structure == FRAME_STRUCTURE
665 && p_vpar->picture.b_frame_pred_frame_dct )
667 p_vpar->mb.i_motion_type = MOTION_FRAME;
671 p_vpar->mb.i_motion_type = GetBits( &p_vpar->bit_stream, 2 );
675 if( p_mb->b_P_coding_type && !(p_vpar->mb.i_mb_type & (MB_MOTION_FORWARD|MB_INTRA)) )
677 /* Special No-MC macroblock in P pictures (7.6.3.5). */
678 memset( p_vpar->slice.pppi_pmv, 0, 8*sizeof(int) );
679 memset( p_mb->pppi_motion_vectors, 0, 8*sizeof(int) );
681 p_vpar->mb.i_motion_type = MOTION_FRAME;
682 p_mb->ppi_field_select[0][0] = ( p_vpar->picture.i_current_structure == BOTTOM_FIELD );
685 if( p_vpar->mb.i_mb_type & MB_INTRA )
687 /* For the intra macroblocks, we use an empty motion
688 * compensation function */
689 p_mb->pf_motion = vdec_DummyRecon;
693 p_mb->pf_motion = pf_motion[p_vpar->picture.b_frame_structure]
694 [p_vpar->mb.i_motion_type];
696 p_vpar->mb.i_mv_count = ppi_mv_count[p_vpar->picture.b_frame_structure]
697 [p_vpar->mb.i_motion_type];
698 p_vpar->mb.i_mv_format = ppi_mv_format[p_vpar->picture.b_frame_structure]
699 [p_vpar->mb.i_motion_type];
701 p_vpar->mb.b_dct_type = 0;
702 if( (p_vpar->picture.i_structure == FRAME_STRUCTURE) &&
703 (!p_vpar->picture.b_frame_pred_frame_dct) &&
704 (p_vpar->mb.i_mb_type & (MB_PATTERN|MB_INTRA)) )
706 if( (p_vpar->mb.b_dct_type = GetBits( &p_vpar->bit_stream, 1 )) )
708 /* The DCT is coded on fields. Jump one line between each
710 p_mb->i_addb_l_stride <<= 1;
711 p_mb->i_addb_l_stride += 8;
712 /* With CHROMA_420, the DCT is necessarily frame-coded. */
713 if( p_vpar->sequence.i_chroma_format != CHROMA_420 )
715 p_mb->i_addb_c_stride <<= 1;
716 p_mb->i_addb_c_stride += 8;
720 p_vpar->mb.b_dmv = p_vpar->mb.i_motion_type == MOTION_DMV;
723 /*****************************************************************************
724 * vpar_ParseMacroblock : Parse the next macroblock
725 *****************************************************************************/
726 void vpar_ParseMacroblock( vpar_thread_t * p_vpar, int * pi_mb_address,
727 int i_mb_previous, int i_mb_base )
729 static f_addb_t ppf_addb_intra[2] = {vdec_AddBlock, vdec_CopyBlock};
730 static f_decode_block_t pppf_decode_block[2][2] =
731 { {vpar_DecodeMPEG1Non, vpar_DecodeMPEG1Intra},
732 {vpar_DecodeMPEG2Non, vpar_DecodeMPEG2Intra} };
733 static int pi_x[12] = {0,8,0,8,0,0,0,0,8,8,8,8};
734 static int pi_y[2][12] = { {0,0,8,8,0,0,8,8,0,0,8,8},
735 {0,0,1,1,0,0,1,1,0,0,1,1} };
737 int i_mb, i_b, i_mask;
740 yuv_data_t * p_data1;
741 yuv_data_t * p_data2;
743 /************* DEBUG *************/
744 static int i_count = 0;
746 boolean_t b_stop = 0;
748 i_inc = MacroblockAddressIncrement( p_vpar );
750 *pi_mb_address += i_inc;
752 //*pi_mb_address += MacroblockAddressIncrement( p_vpar );
754 // fprintf( stderr, "inc : %d (%d)\n", *pi_mb_address, i_inc );
755 if( 0 )//i_count > 8 )
763 // i_count != *pi_mb_address)
766 fprintf( stderr, "i_count = %d (%d)\n", i_count, i_inc );
767 fprintf( stderr, "%x", GetBits( &p_vpar->bit_stream, 16 ) );
768 fprintf( stderr, "%x ", GetBits( &p_vpar->bit_stream, 16 ) );
769 fprintf( stderr, "%x", GetBits( &p_vpar->bit_stream, 16 ) );
770 fprintf( stderr, "%x\n", GetBits( &p_vpar->bit_stream, 16 ) );
771 fprintf( stderr, "%x", GetBits( &p_vpar->bit_stream, 16 ) );
772 fprintf( stderr, "%x ", GetBits( &p_vpar->bit_stream, 16 ) );
773 fprintf( stderr, "%x", GetBits( &p_vpar->bit_stream, 16 ) );
774 fprintf( stderr, "%x\n", GetBits( &p_vpar->bit_stream, 16 ) );
778 for( i_mb = i_mb_previous + 1; i_mb < *pi_mb_address; i_mb++ )
780 /* Skipped macroblock (ISO/IEC 13818-2 7.6.6). */
781 static int pi_dc_dct_reinit[4] = {128,256,512,1024};
782 static f_motion_t pf_motion_skipped[4] = {NULL, vdec_MotionField,
783 vdec_MotionField, vdec_MotionFrame};
785 fprintf(stderr, "Skipped macroblock !\n");
786 /* Reset DC predictors (7.2.1). */
787 p_vpar->slice.pi_dc_dct_pred[0] = p_vpar->slice.pi_dc_dct_pred[1]
788 = p_vpar->slice.pi_dc_dct_pred[2]
789 = pi_dc_dct_reinit[p_vpar->picture.i_intra_dc_precision];
791 if( p_vpar->picture.i_coding_type == P_CODING_TYPE )
793 /* Reset motion vector predictors (ISO/IEC 13818-2 7.6.3.4). */
794 memset( p_vpar->slice.pppi_pmv, 0, 8*sizeof(int) );
797 if( (p_mb = p_vpar->picture.pp_mb[i_mb_base + i_mb] =
798 vpar_NewMacroblock( &p_vpar->vfifo )) == NULL )
800 p_vpar->picture.b_error = 1;
801 intf_ErrMsg("vpar error: macroblock list is empty !\n");
805 InitMacroblock( p_vpar, p_mb );
807 /* No IDCT nor AddBlock. */
808 for( i_b = 0; i_b < 12; i_b++ )
810 p_mb->pf_idct[i_b] = vdec_DummyIDCT;
811 p_mb->pf_addb[i_b] = vdec_DummyBlock;
814 /* Motion type is picture structure. */
815 p_mb->pf_motion = pf_motion_skipped[p_vpar->picture.i_structure];
817 /* Set the field we use for motion compensation */
818 p_mb->ppi_field_select[0][0] = p_mb->ppi_field_select[0][1]
819 = ( p_vpar->picture.i_current_structure == BOTTOM_FIELD );
822 /* Get a macroblock structure. */
823 if( (p_mb = p_vpar->picture.pp_mb[i_mb_base + *pi_mb_address] =
824 vpar_NewMacroblock( &p_vpar->vfifo )) == NULL )
826 p_vpar->picture.b_error = 1;
827 intf_ErrMsg("vpar error: macroblock list is empty !\n");
831 InitMacroblock( p_vpar, p_mb );
833 /* Parse off macroblock_modes structure. */
834 MacroblockModes( p_vpar, p_mb );
836 if( p_vpar->mb.i_mb_type & MB_QUANT )
838 LoadQuantizerScale( p_vpar );
841 if( p_vpar->mb.i_mb_type & MB_MOTION_FORWARD )
843 fprintf( stderr, "motion !\n" );
844 (*p_vpar->sequence.pf_decode_mv)( p_vpar, p_mb, 0 );
847 if( p_vpar->mb.i_mb_type & MB_MOTION_BACKWARD )
849 fprintf( stderr, "motion2 !\n" );
850 (*p_vpar->sequence.pf_decode_mv)( p_vpar, p_mb, 1 );
853 if( p_vpar->picture.b_concealment_mv && (p_vpar->mb.i_mb_type & MB_INTRA) )
855 RemoveBits( &p_vpar->bit_stream, 1 );
858 if( p_vpar->mb.i_mb_type & MB_PATTERN )
860 (*p_vpar->sequence.pf_decode_pattern)( p_vpar );
864 int pi_coded_block_pattern[2] = {0,
865 (1 << (4+p_vpar->sequence.i_chroma_nb_blocks)) - 1};
866 p_vpar->mb.i_coded_block_pattern = pi_coded_block_pattern
867 [p_vpar->mb.i_mb_type & MB_INTRA];
870 pf_addb = ppf_addb_intra[p_vpar->mb.i_mb_type & MB_INTRA];
873 * Effectively decode blocks.
876 i_mask = 1 << (3 + p_vpar->sequence.i_chroma_nb_blocks);
879 p_data1 = p_mb->p_picture->p_y
880 + p_mb->i_l_x + p_mb->i_l_y*(p_vpar->sequence.i_width);
882 for( i_b = 0 ; i_b < 4 ; i_b++, i_mask >>= 1 )
884 if( p_vpar->mb.i_coded_block_pattern & i_mask )
886 memset( p_mb->ppi_blocks[i_b], 0, 64*sizeof(dctelem_t) );
887 (*pppf_decode_block[p_vpar->sequence.b_mpeg2]
888 [p_vpar->mb.i_mb_type & MB_INTRA])
889 ( p_vpar, p_mb, i_b );
891 /* decode_block has already set pf_idct and pi_sparse_pos. */
892 p_mb->pf_addb[i_b] = pf_addb;
894 /* Calculate block coordinates. */
895 p_mb->p_data[i_b] = p_data1
896 + pi_y[p_vpar->mb.b_dct_type][i_b]
897 * p_vpar->sequence.i_width
902 /* Block not coded, so no IDCT, nor AddBlock */
903 p_mb->pf_addb[i_b] = vdec_DummyBlock;
904 p_mb->pf_idct[i_b] = vdec_DummyIDCT;
909 p_data1 = p_mb->p_picture->p_u
912 * (p_vpar->sequence.i_chroma_width);
913 p_data2 = p_mb->p_picture->p_v
916 * (p_vpar->sequence.i_chroma_width);
918 for( i_b = 4; i_b < 4 + p_vpar->sequence.i_chroma_nb_blocks;
919 i_b++, i_mask >>= 1 )
921 yuv_data_t * pp_data[2] = {p_data1, p_data2};
923 if( p_vpar->mb.i_coded_block_pattern & i_mask )
925 memset( p_mb->ppi_blocks[i_b], 0, 64*sizeof(dctelem_t) );
926 (*pppf_decode_block[p_vpar->sequence.b_mpeg2]
927 [p_vpar->mb.i_mb_type & MB_INTRA])
928 ( p_vpar, p_mb, i_b );
930 /* decode_block has already set pf_idct and pi_sparse_pos. */
931 p_mb->pf_addb[i_b] = pf_addb;
933 /* Calculate block coordinates. */
934 p_mb->p_data[i_b] = pp_data[i_b & 1]
935 + pi_y[p_vpar->mb.b_dct_type][i_b]
936 * p_vpar->sequence.i_chroma_width
941 /* Block not coded, so no IDCT, nor AddBlock */
942 p_mb->pf_addb[i_b] = vdec_DummyBlock;
943 p_mb->pf_idct[i_b] = vdec_DummyIDCT;
947 if( !( p_vpar->mb.i_mb_type & MB_INTRA ) )
949 static int pi_dc_dct_reinit[4] = {128,256,512,1024};
951 /* Reset DC predictors (7.2.1). */
952 p_vpar->slice.pi_dc_dct_pred[0] = p_vpar->slice.pi_dc_dct_pred[1]
953 = p_vpar->slice.pi_dc_dct_pred[2]
954 = pi_dc_dct_reinit[p_vpar->picture.i_intra_dc_precision];
956 else if( !p_vpar->picture.b_concealment_mv )
958 /* Reset MV predictors. */
959 memset( p_vpar->slice.pppi_pmv, 0, 8*sizeof(int) );
963 // i_count != *pi_mb_address)
966 fprintf( stderr, "i_count = %d (%d)\n", i_count, i_inc );
967 fprintf( stderr, "%x", GetBits( &p_vpar->bit_stream, 16 ) );
968 fprintf( stderr, "%x ", GetBits( &p_vpar->bit_stream, 16 ) );
969 fprintf( stderr, "%x", GetBits( &p_vpar->bit_stream, 16 ) );
970 fprintf( stderr, "%x\n", GetBits( &p_vpar->bit_stream, 16 ) );
971 fprintf( stderr, "%x", GetBits( &p_vpar->bit_stream, 16 ) );
972 fprintf( stderr, "%x ", GetBits( &p_vpar->bit_stream, 16 ) );
973 fprintf( stderr, "%x", GetBits( &p_vpar->bit_stream, 16 ) );
974 fprintf( stderr, "%x\n", GetBits( &p_vpar->bit_stream, 16 ) );
979 /*****************************************************************************
980 * vpar_IMBType : macroblock_type in I pictures
981 *****************************************************************************/
982 int vpar_IMBType( vpar_thread_t * p_vpar )
984 /* Take two bits for testing */
985 int i_type = ShowBits( &p_vpar->bit_stream, 2 );
987 /* Lookup table for macroblock_type */
988 static lookup_t pl_mb_Itype[4] = { {MB_ERROR, 0},
989 {MB_QUANT|MB_INTRA, 2},
992 /* Dump the good number of bits */
993 RemoveBits( &p_vpar->bit_stream, pl_mb_Itype[i_type].i_length );
994 return pl_mb_Itype[i_type].i_value;
997 /*****************************************************************************
998 * vpar_PMBType : macroblock_type in P pictures
999 *****************************************************************************/
1000 int vpar_PMBType( vpar_thread_t * p_vpar )
1002 /* Testing on 6 bits */
1003 int i_type = ShowBits( &p_vpar->bit_stream, 6 );
1005 /* Dump the good number of bits */
1006 RemoveBits( &p_vpar->bit_stream, p_vpar->ppl_mb_type[0][i_type].i_length );
1008 /* return the value from the lookup table for P type */
1009 return p_vpar->ppl_mb_type[0][i_type].i_value;
1012 /*****************************************************************************
1013 * vpar_BMBType : macroblock_type in B pictures
1014 *****************************************************************************/
1015 int vpar_BMBType( vpar_thread_t * p_vpar )
1017 /* Testing on 6 bits */
1018 int i_type = ShowBits( &p_vpar->bit_stream, 6 );
1020 /* Dump the good number of bits */
1021 RemoveBits( &p_vpar->bit_stream, p_vpar->ppl_mb_type[1][i_type].i_length );
1023 /* return the value from the lookup table for B type */
1024 return p_vpar->ppl_mb_type[1][i_type].i_value;
1027 /*****************************************************************************
1028 * vpar_DMBType : macroblock_type in D pictures
1029 *****************************************************************************/
1030 int vpar_DMBType( vpar_thread_t * p_vpar )
1033 int i_type = GetBits( &p_vpar->bit_stream, 1 );
1036 static int pi_mb_Dtype[2] = { MB_ERROR, 1 };
1038 return pi_mb_Dtype[i_type];
1041 /*****************************************************************************
1042 * vpar_CodedPattern420 : coded_block_pattern with 420 chroma
1043 *****************************************************************************/
1044 int vpar_CodedPattern420( vpar_thread_t * p_vpar )
1046 /* Take the max 9 bits length vlc code for testing */
1047 int i_vlc = ShowBits( &p_vpar->bit_stream, 9 );
1049 /* Trash the good number of bits read in the lookup table */
1050 RemoveBits( &p_vpar->bit_stream, p_vpar->pl_coded_pattern[i_vlc].i_length );
1052 /* return the value from the vlc table */
1053 return p_vpar->pl_coded_pattern[i_vlc].i_value;
1056 /*****************************************************************************
1057 * vpar_CodedPattern422 : coded_block_pattern with 422 chroma
1058 *****************************************************************************/
1059 int vpar_CodedPattern422( vpar_thread_t * p_vpar )
1061 int i_vlc = ShowBits( &p_vpar->bit_stream, 9 );
1063 /* Supplementary 2 bits long code for 422 format */
1064 int i_coded_block_pattern_1;
1066 RemoveBits( &p_vpar->bit_stream, p_vpar->pl_coded_pattern[i_vlc].i_length );
1067 i_coded_block_pattern_1 = GetBits( &p_vpar->bit_stream, 2 );
1069 /* the code is just to be added to the value found in the table */
1070 return p_vpar->pl_coded_pattern[i_vlc].i_value |
1071 (i_coded_block_pattern_1 << 6);
1074 /*****************************************************************************
1075 * vpar_CodedPattern444 : coded_block_pattern with 444 chroma
1076 *****************************************************************************/
1077 int vpar_CodedPattern444( vpar_thread_t * p_vpar )
1079 int i_vlc = ShowBits( &p_vpar->bit_stream, 9 );
1080 int i_coded_block_pattern_2;
1082 RemoveBits( &p_vpar->bit_stream, p_vpar->pl_coded_pattern[i_vlc].i_length );
1083 i_coded_block_pattern_2 = GetBits( &p_vpar->bit_stream, 6 );
1085 return p_vpar->pl_coded_pattern[i_vlc].i_value |
1086 ( i_coded_block_pattern_2 << 6 );
1089 /*****************************************************************************
1090 * vpar_DecodeMPEG1Non : decode MPEG-1 non-intra blocks
1091 *****************************************************************************/
1092 static void vpar_DecodeMPEG1Non( vpar_thread_t * p_vpar, macroblock_t * p_mb, int i_b )
1095 if( p_vpar->picture.i_coding_type == D_CODING_TYPE )
1097 /* Remove end_of_macroblock (always 1, prevents startcode emulation)
1098 * ISO/IEC 11172-2 section 2.4.2.7 and 2.4.3.6 */
1099 RemoveBits( &p_vpar->bit_stream, 1 );
1103 /*****************************************************************************
1104 * vpar_DecodeMPEG1Intra : decode MPEG-1 intra blocks
1105 *****************************************************************************/
1106 static void vpar_DecodeMPEG1Intra( vpar_thread_t * p_vpar, macroblock_t * p_mb, int i_b )
1109 if( p_vpar->picture.i_coding_type == D_CODING_TYPE )
1111 /* Remove end_of_macroblock (always 1, prevents startcode emulation)
1112 * ISO/IEC 11172-2 section 2.4.2.7 and 2.4.3.6 */
1113 RemoveBits( &p_vpar->bit_stream, 1 );
1117 /*****************************************************************************
1118 * vpar_DecodeMPEG2Non : decode MPEG-2 non-intra blocks
1119 *****************************************************************************/
1120 static void vpar_DecodeMPEG2Non( vpar_thread_t * p_vpar, macroblock_t * p_mb, int i_b )
1135 /* Lookup Table for the chromatic component */
1136 static int pi_cc_index[12] = { 0, 0, 0, 0, 1, 2, 1, 2, 1, 2 };
1138 i_cc = pi_cc_index[i_b];
1140 /* Determine whether it is luminance or not (chrominance) */
1141 i_type = ( i_cc + 1 ) >> 1;
1143 /* Give a pointer to the quantization matrices for intra blocks */
1144 ppi_quant[0] = p_vpar->sequence.nonintra_quant.pi_matrix;
1145 ppi_quant[1] = p_vpar->sequence.chroma_nonintra_quant.pi_matrix;
1147 /* Decoding of the AC coefficients */
1151 for( i_parse = 0; ; i_parse++ )
1153 i_code = ShowBits( &p_vpar->bit_stream, 16 );
1154 if( i_code >= 16384 )
1158 i_run = pl_DCT_tab_dc[(i_code>>12)-4].i_run;
1159 i_level = pl_DCT_tab_dc[(i_code>>12)-4].i_level;
1160 i_length = pl_DCT_tab_dc[(i_code>>12)-4].i_length;
1164 i_run = pl_DCT_tab_ac[(i_code>>12)-4].i_run;
1165 i_level = pl_DCT_tab_ac[(i_code>>12)-4].i_level;
1166 i_length = pl_DCT_tab_ac[(i_code>>12)-4].i_length;
1171 i_run = ppl_dct_coef[0][i_code].i_run;
1172 i_length = ppl_dct_coef[0][i_code].i_length;
1173 i_level = ppl_dct_coef[0][i_code].i_level;
1177 RemoveBits( &p_vpar->bit_stream, i_length );
1182 i_run = GetBits( &p_vpar->bit_stream, 6 );
1183 i_level = GetBits( &p_vpar->bit_stream, 12 );
1184 i_level = (b_sign = ( i_level > 2047 )) ? 4096 - i_level
1190 p_mb->pf_idct[i_b] = vdec_SparseIDCT;
1191 p_mb->pi_sparse_pos[i_b] = i_coef;
1195 p_mb->pf_idct[i_b] = vdec_IDCT;
1201 b_sign = GetBits( &p_vpar->bit_stream, 1 );
1207 i_pos = pi_scan[p_vpar->picture.b_alternate_scan][i_parse];
1208 i_level = ( i_level *
1209 p_vpar->slice.i_quantizer_scale *
1210 ppi_quant[i_type][i_pos] ) >> 5;
1211 p_mb->ppi_blocks[i_b][i_parse] = b_sign ? -i_level : i_level;
1213 fprintf( stderr, "MPEG2 end (%d)\n", i_b );
1218 /*****************************************************************************
1219 * vpar_DecodeMPEG2Intra : decode MPEG-2 intra blocks
1220 *****************************************************************************/
1221 static void vpar_DecodeMPEG2Intra( vpar_thread_t * p_vpar, macroblock_t * p_mb, int i_b )
1227 int i_type, i_quant_type;
1235 boolean_t b_vlc_intra;
1239 /* Lookup Table for the chromatic component */
1240 static int pi_cc_index[12] = { 0, 0, 0, 0, 1, 2, 1, 2, 1, 2 };
1242 i_cc = pi_cc_index[i_b];
1244 /* Determine whether it is luminance or not (chrominance) */
1245 i_type = ( i_cc + 1 ) >> 1;
1246 i_quant_type = i_type | (p_vpar->sequence.i_chroma_format == CHROMA_420);
1248 /* Give a pointer to the quantization matrices for intra blocks */
1249 ppi_quant[0] = p_vpar->sequence.intra_quant.pi_matrix;
1250 ppi_quant[1] = p_vpar->sequence.chroma_intra_quant.pi_matrix;
1253 /* Decoding of the DC intra coefficient */
1254 /* The nb of bits to parse depends on i_type */
1255 i_code = ShowBits( &p_vpar->bit_stream, 9 + i_type );
1257 /* To reduce memory occupation, there are two lookup tables
1258 * See InitDCT above */
1259 i_code5 = i_code >> (4+i_type);
1261 /* Shall we lookup in the first or in the second table ? */
1262 i_select = ( i_code5 == 31 );
1263 /* Offset value for looking in the second table */
1264 i_offset = 0x1f0 + ( i_type * 0x1f0 );
1265 i_pos = ( i_code5 * ( ! i_select ) ) + ( ( i_code - i_offset ) * i_select );
1266 i_dct_dc_size = p_vpar->pppl_dct_dc_size[i_type][i_select][i_pos].i_value;
1269 if( !i_type/*i_b < 4*/ )
1272 i_code = ShowBits(&p_vpar->bit_stream, 5);
1275 i_dct_dc_size = pl_dct_dc_lum_init_table_1[i_code].i_value;
1276 i_length = pl_dct_dc_lum_init_table_1[i_code].i_length;
1277 RemoveBits( &p_vpar->bit_stream, i_length);
1281 i_code = ShowBits(&p_vpar->bit_stream, 9) - 0x1f0;
1282 i_dct_dc_size = pl_dct_dc_lum_init_table_2[i_code].i_value;
1283 i_length = pl_dct_dc_lum_init_table_2[i_code].i_length;
1284 RemoveBits( &p_vpar->bit_stream, i_length);
1290 i_code = ShowBits(&p_vpar->bit_stream, 5);
1294 i_dct_dc_size = pl_dct_dc_chrom_init_table_1[i_code].i_value;
1295 i_length = pl_dct_dc_chrom_init_table_1[i_code].i_length;
1296 RemoveBits(&p_vpar->bit_stream, i_length);
1300 i_code = ShowBits(&p_vpar->bit_stream, 10) - 0x3e0;
1301 i_dct_dc_size = pl_dct_dc_chrom_init_table_2[i_code].i_value;
1302 i_length = pl_dct_dc_chrom_init_table_2[i_code].i_length;
1303 RemoveBits( &p_vpar->bit_stream, i_length);
1306 if (i_dct_dc_size==0)
1310 i_dct_dc_diff = GetBits( &p_vpar->bit_stream, i_dct_dc_size);
1311 if ((i_dct_dc_diff & (1<<(i_dct_dc_size-1)))==0)
1312 i_dct_dc_diff-= (1<<i_dct_dc_size) - 1;
1315 /* Dump the variable length code */
1316 //RemoveBits( &p_vpar->bit_stream,
1317 // p_vpar->pppl_dct_dc_size[i_type][i_select][i_pos].i_length );
1319 /* Read the actual code with the good length */
1320 p_vpar->slice.pi_dc_dct_pred[i_cc] += i_dct_dc_diff;
1322 p_mb->ppi_blocks[i_b][0] = ( p_vpar->slice.pi_dc_dct_pred[i_cc] <<
1323 ( 3 - p_vpar->picture.i_intra_dc_precision ) );
1324 i_nc = ( p_vpar->slice.pi_dc_dct_pred[i_cc] != 0 );
1326 //fprintf( stderr, "coucou\n" );
1327 /* Decoding of the AC coefficients */
1330 b_vlc_intra = p_vpar->picture.b_intra_vlc_format;
1331 for( i_parse = 1; /*i_parse < 64*/; i_parse++ )
1333 i_code = ShowBits( &p_vpar->bit_stream, 16 );
1334 if( i_code >= 16384 )
1338 i_run = pl_DCT_tab0a[(i_code>>8)-4].i_run;
1339 i_level = pl_DCT_tab0a[(i_code>>8)-4].i_level;
1340 i_length = pl_DCT_tab0a[(i_code>>8)-4].i_length;
1341 //fprintf( stderr, "**********> %d, %d, %d *******\n", i_run, i_level, (i_code>>8)-4 );
1345 i_run = pl_DCT_tab_ac[(i_code>>12)-4].i_run;
1346 i_level = pl_DCT_tab_ac[(i_code>>12)-4].i_level;
1347 i_length = pl_DCT_tab_ac[(i_code>>12)-4].i_length;
1352 i_run = ppl_dct_coef[b_vlc_intra][i_code].i_run;
1353 i_length = ppl_dct_coef[b_vlc_intra][i_code].i_length;
1354 i_level = ppl_dct_coef[b_vlc_intra][i_code].i_level;
1360 int intra_vlc_format = b_vlc_intra;
1363 if (code>=16384 && !intra_vlc_format)
1364 tab = pl_DCT_tab_ac[(code>>12)-4];
1365 else if (code>=1024)
1367 if (intra_vlc_format)
1368 tab = pl_DCT_tab0a[(code>>8)-4];
1370 tab = pl_DCT_tab0[(code>>8)-4];
1374 if (intra_vlc_format)
1375 tab = pl_DCT_tab1a[(code>>6)-8];
1377 tab = pl_DCT_tab1[(code>>6)-8];
1380 tab = pl_DCT_tab2[(code>>4)-16];
1382 tab = pl_DCT_tab3[(code>>3)-16];
1384 tab = pl_DCT_tab4[(code>>2)-16];
1386 tab = pl_DCT_tab5[(code>>1)-16];
1388 tab = pl_DCT_tab6[code-16];
1391 fprintf( stderr, "invalid Huffman code in Decode_MPEG2_Intra_Block()\n");
1394 if( (i_run != tab.i_run) || (i_length != tab.i_length) || (i_level != tab.i_level) )
1396 fprintf( stderr, "ET M....... !!!\n" );
1405 RemoveBits( &p_vpar->bit_stream, i_length );
1410 i_run = GetBits( &p_vpar->bit_stream, 6 );
1411 i_level = GetBits( &p_vpar->bit_stream, 12 );
1412 /*p_mb->ppi_blocks[i_b][i_parse] = ( b_sign = ( i_level > 2047 ) )
1413 ? ( -4096 + i_level )
1415 i_level = (b_sign = ( i_level > 2047 )) ? 4096 - i_level
1421 p_mb->pf_idct[i_b] = vdec_SparseIDCT;
1422 p_mb->pi_sparse_pos[i_b] = i_coef;
1426 p_mb->pf_idct[i_b] = vdec_IDCT;
1432 b_sign = GetBits( &p_vpar->bit_stream, 1 );
1433 //p_mb->ppi_blocks[i_b][i_parse] = b_sign ? -i_level : i_level;
1435 // fprintf( stderr, "i_code : %d (%d), run : %d, %d, %d (%4x) ", i_code , b_vlc_intra,
1436 // i_run, i_level, i_parse, ShowBits( &p_vpar->bit_stream, 16 ) );
1438 //fprintf( stderr, "- %4x\n",ShowBits( &p_vpar->bit_stream, 16 ) );
1444 i_pos = pi_scan[p_vpar->picture.b_alternate_scan][i_parse];
1445 i_level = ( i_level *
1446 p_vpar->slice.i_quantizer_scale *
1447 ppi_quant[i_quant_type][i_pos] ) >> 4;
1448 p_mb->ppi_blocks[i_b][i_pos] = b_sign ? -i_level : i_level;
1451 fprintf( stderr, "MPEG2 end (%d)\n", i_b );