1 /*****************************************************************************
2 * vpar_blocks.c : blocks parsing
4 *****************************************************************************/
6 /*****************************************************************************
8 *****************************************************************************/
19 #include "vlc_thread.h"
22 #include "debug.h" /* ?? temporaire, requis par netlist.h */
25 #include "input_netlist.h"
26 #include "decoder_fifo.h"
28 #include "video_output.h"
30 #include "vdec_idct.h"
31 #include "video_decoder.h"
32 #include "vdec_motion.h"
34 #include "vpar_blocks.h"
35 #include "vpar_headers.h"
36 #include "vpar_synchro.h"
37 #include "video_parser.h"
38 #include "video_fifo.h"
41 * Welcome to vpar_blocks.c ! Here's where the heavy processor-critical parsing
42 * task is done. This file is divided in several parts :
43 * - Initialization of the lookup tables
44 * - Decoding of coded blocks
45 * - Decoding of motion vectors
46 * - Decoding of the other macroblock structures
47 * - Picture data parsing management (slices and error handling)
48 * It's a pretty long file. Good luck and have a nice day.
53 * Initialization tables
56 /* Table for coded_block_pattern resolution */
57 static lookup_t pl_coded_pattern[512] =
58 { {MB_ERROR, 0}, {0, 9}, {39, 9}, {27, 9}, {59, 9}, {55, 9}, {47, 9}, {31, 9},
59 {58, 8}, {58, 8}, {54, 8}, {54, 8}, {46, 8}, {46, 8}, {30, 8}, {30, 8},
60 {57, 8}, {57, 8}, {53, 8}, {53, 8}, {45, 8}, {45, 8}, {29, 8}, {29, 8},
61 {38, 8}, {38, 8}, {26, 8}, {26, 8}, {37, 8}, {37, 8}, {25, 8}, {25, 8},
62 {43, 8}, {43, 8}, {23, 8}, {23, 8}, {51, 8}, {51, 8}, {15, 8}, {15, 8},
63 {42, 8}, {42, 8}, {22, 8}, {22, 8}, {50, 8}, {50, 8}, {14, 8}, {14, 8},
64 {41, 8}, {41, 8}, {21, 8}, {21, 8}, {49, 8}, {49, 8}, {13, 8}, {13, 8},
65 {35, 8}, {35, 8}, {19, 8}, {19, 8}, {11, 8}, {11, 8}, {7, 8}, {7, 8},
66 {34, 7}, {34, 7}, {34, 7}, {34, 7}, {18, 7}, {18, 7}, {18, 7}, {18, 7},
67 {10, 7}, {10, 7}, {10, 7}, {10, 7}, {6, 7}, {6, 7}, {6, 7}, {6, 7},
68 {33, 7}, {33, 7}, {33, 7}, {33, 7}, {17, 7}, {17, 7}, {17, 7}, {17, 7},
69 {9, 7}, {9, 7}, {9, 7}, {9, 7}, {5, 7}, {5, 7}, {5, 7}, {5, 7},
70 {63, 6}, {63, 6}, {63, 6}, {63, 6}, {63, 6}, {63, 6}, {63, 6}, {63, 6},
71 {3, 6}, {3, 6}, {3, 6}, {3, 6}, {3, 6}, {3, 6}, {3, 6}, {3, 6},
72 {36, 6}, {36, 6}, {36, 6}, {36, 6}, {36, 6}, {36, 6}, {36, 6}, {36, 6},
73 {24, 6}, {24, 6}, {24, 6}, {24, 6}, {24, 6}, {24, 6}, {24, 6}, {24, 6},
74 {62, 5}, {62, 5}, {62, 5}, {62, 5}, {62, 5}, {62, 5}, {62, 5}, {62, 5},
75 {62, 5}, {62, 5}, {62, 5}, {62, 5}, {62, 5}, {62, 5}, {62, 5}, {62, 5},
76 {2, 5}, {2, 5}, {2, 5}, {2, 5}, {2, 5}, {2, 5}, {2, 5}, {2, 5},
77 {2, 5}, {2, 5}, {2, 5}, {2, 5}, {2, 5}, {2, 5}, {2, 5}, {2, 5},
78 {61, 5}, {61, 5}, {61, 5}, {61, 5}, {61, 5}, {61, 5}, {61, 5}, {61, 5},
79 {61, 5}, {61, 5}, {61, 5}, {61, 5}, {61, 5}, {61, 5}, {61, 5}, {61, 5},
80 {1, 5}, {1, 5}, {1, 5}, {1, 5}, {1, 5}, {1, 5}, {1, 5}, {1, 5},
81 {1, 5}, {1, 5}, {1, 5}, {1, 5}, {1, 5}, {1, 5}, {1, 5}, {1, 5},
82 {56, 5}, {56, 5}, {56, 5}, {56, 5}, {56, 5}, {56, 5}, {56, 5}, {56, 5},
83 {56, 5}, {56, 5}, {56, 5}, {56, 5}, {56, 5}, {56, 5}, {56, 5}, {56, 5},
84 {52, 5}, {52, 5}, {52, 5}, {52, 5}, {52, 5}, {52, 5}, {52, 5}, {52, 5},
85 {52, 5}, {52, 5}, {52, 5}, {52, 5}, {52, 5}, {52, 5}, {52, 5}, {52, 5},
86 {44, 5}, {44, 5}, {44, 5}, {44, 5}, {44, 5}, {44, 5}, {44, 5}, {44, 5},
87 {44, 5}, {44, 5}, {44, 5}, {44, 5}, {44, 5}, {44, 5}, {44, 5}, {44, 5},
88 {28, 5}, {28, 5}, {28, 5}, {28, 5}, {28, 5}, {28, 5}, {28, 5}, {28, 5},
89 {28, 5}, {28, 5}, {28, 5}, {28, 5}, {28, 5}, {28, 5}, {28, 5}, {28, 5},
90 {40, 5}, {40, 5}, {40, 5}, {40, 5}, {40, 5}, {40, 5}, {40, 5}, {40, 5},
91 {40, 5}, {40, 5}, {40, 5}, {40, 5}, {40, 5}, {40, 5}, {40, 5}, {40, 5},
92 {20, 5}, {20, 5}, {20, 5}, {20, 5}, {20, 5}, {20, 5}, {20, 5}, {20, 5},
93 {20, 5}, {20, 5}, {20, 5}, {20, 5}, {20, 5}, {20, 5}, {20, 5}, {20, 5},
94 {48, 5}, {48, 5}, {48, 5}, {48, 5}, {48, 5}, {48, 5}, {48, 5}, {48, 5},
95 {48, 5}, {48, 5}, {48, 5}, {48, 5}, {48, 5}, {48, 5}, {48, 5}, {48, 5},
96 {12, 5}, {12, 5}, {12, 5}, {12, 5}, {12, 5}, {12, 5}, {12, 5}, {12, 5},
97 {12, 5}, {12, 5}, {12, 5}, {12, 5}, {12, 5}, {12, 5}, {12, 5}, {12, 5},
98 {32, 4}, {32, 4}, {32, 4}, {32, 4}, {32, 4}, {32, 4}, {32, 4}, {32, 4},
99 {32, 4}, {32, 4}, {32, 4}, {32, 4}, {32, 4}, {32, 4}, {32, 4}, {32, 4},
100 {32, 4}, {32, 4}, {32, 4}, {32, 4}, {32, 4}, {32, 4}, {32, 4}, {32, 4},
101 {32, 4}, {32, 4}, {32, 4}, {32, 4}, {32, 4}, {32, 4}, {32, 4}, {32, 4},
102 {16, 4}, {16, 4}, {16, 4}, {16, 4}, {16, 4}, {16, 4}, {16, 4}, {16, 4},
103 {16, 4}, {16, 4}, {16, 4}, {16, 4}, {16, 4}, {16, 4}, {16, 4}, {16, 4},
104 {16, 4}, {16, 4}, {16, 4}, {16, 4}, {16, 4}, {16, 4}, {16, 4}, {16, 4},
105 {16, 4}, {16, 4}, {16, 4}, {16, 4}, {16, 4}, {16, 4}, {16, 4}, {16, 4},
106 {8, 4}, {8, 4}, {8, 4}, {8, 4}, {8, 4}, {8, 4}, {8, 4}, {8, 4},
107 {8, 4}, {8, 4}, {8, 4}, {8, 4}, {8, 4}, {8, 4}, {8, 4}, {8, 4},
108 {8, 4}, {8, 4}, {8, 4}, {8, 4}, {8, 4}, {8, 4}, {8, 4}, {8, 4},
109 {8, 4}, {8, 4}, {8, 4}, {8, 4}, {8, 4}, {8, 4}, {8, 4}, {8, 4},
110 {4, 4}, {4, 4}, {4, 4}, {4, 4}, {4, 4}, {4, 4}, {4, 4}, {4, 4},
111 {4, 4}, {4, 4}, {4, 4}, {4, 4}, {4, 4}, {4, 4}, {4, 4}, {4, 4},
112 {4, 4}, {4, 4}, {4, 4}, {4, 4}, {4, 4}, {4, 4}, {4, 4}, {4, 4},
113 {4, 4}, {4, 4}, {4, 4}, {4, 4}, {4, 4}, {4, 4}, {4, 4}, {4, 4},
114 {60, 3}, {60, 3}, {60, 3}, {60, 3}, {60, 3}, {60, 3}, {60, 3}, {60, 3},
115 {60, 3}, {60, 3}, {60, 3}, {60, 3}, {60, 3}, {60, 3}, {60, 3}, {60, 3},
116 {60, 3}, {60, 3}, {60, 3}, {60, 3}, {60, 3}, {60, 3}, {60, 3}, {60, 3},
117 {60, 3}, {60, 3}, {60, 3}, {60, 3}, {60, 3}, {60, 3}, {60, 3}, {60, 3},
118 {60, 3}, {60, 3}, {60, 3}, {60, 3}, {60, 3}, {60, 3}, {60, 3}, {60, 3},
119 {60, 3}, {60, 3}, {60, 3}, {60, 3}, {60, 3}, {60, 3}, {60, 3}, {60, 3},
120 {60, 3}, {60, 3}, {60, 3}, {60, 3}, {60, 3}, {60, 3}, {60, 3}, {60, 3},
121 {60, 3}, {60, 3}, {60, 3}, {60, 3}, {60, 3}, {60, 3}, {60, 3}, {60, 3} };
123 /* Tables for dc DCT coefficients
124 * Tables are cut in two parts to reduce memory occupation
127 /* Table B-12, dct_dc_size_luminance, codes 00xxx ... 11110 */
128 static lookup_t pl_dct_dc_lum_init_table_1[32] =
129 { {1, 2}, {1, 2}, {1, 2}, {1, 2}, {1, 2}, {1, 2}, {1, 2}, {1, 2},
130 {2, 2}, {2, 2}, {2, 2}, {2, 2}, {2, 2}, {2, 2}, {2, 2}, {2, 2},
131 {0, 3}, {0, 3}, {0, 3}, {0, 3}, {3, 3}, {3, 3}, {3, 3}, {3, 3},
132 {4, 3}, {4, 3}, {4, 3}, {4, 3}, {5, 4}, {5, 4}, {6, 5}, {MB_ERROR, 0}
135 /* Table B-12, dct_dc_size_luminance, codes 111110xxx ... 111111111 */
136 static lookup_t pl_dct_dc_lum_init_table_2[32] =
137 { {7, 6}, {7, 6}, {7, 6}, {7, 6}, {7, 6}, {7, 6}, {7, 6}, {7, 6},
138 {8, 7}, {8, 7}, {8, 7}, {8, 7}, {9, 8}, {9, 8}, {10,9}, {11,9},
139 {MB_ERROR, 0}, {MB_ERROR, 0}, {MB_ERROR, 0}, {MB_ERROR, 0},
140 {MB_ERROR, 0}, {MB_ERROR, 0}, {MB_ERROR, 0}, {MB_ERROR, 0},
141 {MB_ERROR, 0}, {MB_ERROR, 0}, {MB_ERROR, 0}, {MB_ERROR, 0},
142 {MB_ERROR, 0}, {MB_ERROR, 0}, {MB_ERROR, 0}, {MB_ERROR, 0}
145 /* Table B-13, dct_dc_size_chrominance, codes 00xxx ... 11110 */
146 static lookup_t pl_dct_dc_chrom_init_table_1[32] =
147 { {0, 2}, {0, 2}, {0, 2}, {0, 2}, {0, 2}, {0, 2}, {0, 2}, {0, 2},
148 {1, 2}, {1, 2}, {1, 2}, {1, 2}, {1, 2}, {1, 2}, {1, 2}, {1, 2},
149 {2, 2}, {2, 2}, {2, 2}, {2, 2}, {2, 2}, {2, 2}, {2, 2}, {2, 2},
150 {3, 3}, {3, 3}, {3, 3}, {3, 3}, {4, 4}, {4, 4}, {5, 5}, {MB_ERROR, 0}
153 /* Table B-13, dct_dc_size_chrominance, codes 111110xxxx ... 1111111111 */
154 static lookup_t pl_dct_dc_chrom_init_table_2[32] =
155 { {6, 6}, {6, 6}, {6, 6}, {6, 6}, {6, 6}, {6, 6}, {6, 6}, {6, 6},
156 {6, 6}, {6, 6}, {6, 6}, {6, 6}, {6, 6}, {6, 6}, {6, 6}, {6, 6},
157 {7, 7}, {7, 7}, {7, 7}, {7, 7}, {7, 7}, {7, 7}, {7, 7}, {7, 7},
158 {8, 8}, {8, 8}, {8, 8}, {8, 8}, {9, 9}, {9, 9}, {10,10}, {11,10}
162 /* Tables for ac DCT coefficients. There are cut in many parts to save space */
163 /* Table B-14, DCT coefficients table zero,
164 * codes 0100 ... 1xxx (used for first (DC) coefficient)
166 static dct_lookup_t pl_DCT_tab_dc[12] =
168 {0,2,4}, {2,1,4}, {1,1,3}, {1,1,3},
169 {0,1,1}, {0,1,1}, {0,1,1}, {0,1,1},
170 {0,1,1}, {0,1,1}, {0,1,1}, {0,1,1}
173 /* Table B-14, DCT coefficients table zero,
174 * codes 0100 ... 1xxx (used for all other coefficients)
176 static dct_lookup_t pl_DCT_tab_ac[12] =
178 {0,2,4}, {2,1,4}, {1,1,3}, {1,1,3},
179 {DCT_EOB,0,2}, {DCT_EOB,0,2}, {DCT_EOB,0,2}, {DCT_EOB,0,2}, /* EOB */
180 {0,1,2}, {0,1,2}, {0,1,2}, {0,1,2}
183 /* Table B-14, DCT coefficients table zero,
184 * codes 000001xx ... 00111xxx
186 static dct_lookup_t pl_DCT_tab0[60] =
188 {DCT_ESCAPE,0,6}, {DCT_ESCAPE,0,6}, {DCT_ESCAPE,0,6}, {DCT_ESCAPE,0,6},
190 {2,2,7}, {2,2,7}, {9,1,7}, {9,1,7},
191 {0,4,7}, {0,4,7}, {8,1,7}, {8,1,7},
192 {7,1,6}, {7,1,6}, {7,1,6}, {7,1,6},
193 {6,1,6}, {6,1,6}, {6,1,6}, {6,1,6},
194 {1,2,6}, {1,2,6}, {1,2,6}, {1,2,6},
195 {5,1,6}, {5,1,6}, {5,1,6}, {5,1,6},
196 {13,1,8}, {0,6,8}, {12,1,8}, {11,1,8},
197 {3,2,8}, {1,3,8}, {0,5,8}, {10,1,8},
198 {0,3,5}, {0,3,5}, {0,3,5}, {0,3,5},
199 {0,3,5}, {0,3,5}, {0,3,5}, {0,3,5},
200 {4,1,5}, {4,1,5}, {4,1,5}, {4,1,5},
201 {4,1,5}, {4,1,5}, {4,1,5}, {4,1,5},
202 {3,1,5}, {3,1,5}, {3,1,5}, {3,1,5},
203 {3,1,5}, {3,1,5}, {3,1,5}, {3,1,5}
206 /* Table B-15, DCT coefficients table one,
207 * codes 000001xx ... 11111111
209 static dct_lookup_t pl_DCT_tab0a[252] =
211 {65,0,6}, {65,0,6}, {65,0,6}, {65,0,6}, /* Escape */
212 {7,1,7}, {7,1,7}, {8,1,7}, {8,1,7},
213 {6,1,7}, {6,1,7}, {2,2,7}, {2,2,7},
214 {0,7,6}, {0,7,6}, {0,7,6}, {0,7,6},
215 {0,6,6}, {0,6,6}, {0,6,6}, {0,6,6},
216 {4,1,6}, {4,1,6}, {4,1,6}, {4,1,6},
217 {5,1,6}, {5,1,6}, {5,1,6}, {5,1,6},
218 {1,5,8}, {11,1,8}, {0,11,8}, {0,10,8},
219 {13,1,8}, {12,1,8}, {3,2,8}, {1,4,8},
220 {2,1,5}, {2,1,5}, {2,1,5}, {2,1,5},
221 {2,1,5}, {2,1,5}, {2,1,5}, {2,1,5},
222 {1,2,5}, {1,2,5}, {1,2,5}, {1,2,5},
223 {1,2,5}, {1,2,5}, {1,2,5}, {1,2,5},
224 {3,1,5}, {3,1,5}, {3,1,5}, {3,1,5},
225 {3,1,5}, {3,1,5}, {3,1,5}, {3,1,5},
226 {1,1,3}, {1,1,3}, {1,1,3}, {1,1,3},
227 {1,1,3}, {1,1,3}, {1,1,3}, {1,1,3},
228 {1,1,3}, {1,1,3}, {1,1,3}, {1,1,3},
229 {1,1,3}, {1,1,3}, {1,1,3}, {1,1,3},
230 {1,1,3}, {1,1,3}, {1,1,3}, {1,1,3},
231 {1,1,3}, {1,1,3}, {1,1,3}, {1,1,3},
232 {1,1,3}, {1,1,3}, {1,1,3}, {1,1,3},
233 {1,1,3}, {1,1,3}, {1,1,3}, {1,1,3},
234 {64,0,4}, {64,0,4}, {64,0,4}, {64,0,4}, /* EOB */
235 {64,0,4}, {64,0,4}, {64,0,4}, {64,0,4},
236 {64,0,4}, {64,0,4}, {64,0,4}, {64,0,4},
237 {64,0,4}, {64,0,4}, {64,0,4}, {64,0,4},
238 {0,3,4}, {0,3,4}, {0,3,4}, {0,3,4},
239 {0,3,4}, {0,3,4}, {0,3,4}, {0,3,4},
240 {0,3,4}, {0,3,4}, {0,3,4}, {0,3,4},
241 {0,3,4}, {0,3,4}, {0,3,4}, {0,3,4},
242 {0,1,2}, {0,1,2}, {0,1,2}, {0,1,2},
243 {0,1,2}, {0,1,2}, {0,1,2}, {0,1,2},
244 {0,1,2}, {0,1,2}, {0,1,2}, {0,1,2},
245 {0,1,2}, {0,1,2}, {0,1,2}, {0,1,2},
246 {0,1,2}, {0,1,2}, {0,1,2}, {0,1,2},
247 {0,1,2}, {0,1,2}, {0,1,2}, {0,1,2},
248 {0,1,2}, {0,1,2}, {0,1,2}, {0,1,2},
249 {0,1,2}, {0,1,2}, {0,1,2}, {0,1,2},
250 {0,1,2}, {0,1,2}, {0,1,2}, {0,1,2},
251 {0,1,2}, {0,1,2}, {0,1,2}, {0,1,2},
252 {0,1,2}, {0,1,2}, {0,1,2}, {0,1,2},
253 {0,1,2}, {0,1,2}, {0,1,2}, {0,1,2},
254 {0,1,2}, {0,1,2}, {0,1,2}, {0,1,2},
255 {0,1,2}, {0,1,2}, {0,1,2}, {0,1,2},
256 {0,1,2}, {0,1,2}, {0,1,2}, {0,1,2},
257 {0,1,2}, {0,1,2}, {0,1,2}, {0,1,2},
258 {0,2,3}, {0,2,3}, {0,2,3}, {0,2,3},
259 {0,2,3}, {0,2,3}, {0,2,3}, {0,2,3},
260 {0,2,3}, {0,2,3}, {0,2,3}, {0,2,3},
261 {0,2,3}, {0,2,3}, {0,2,3}, {0,2,3},
262 {0,2,3}, {0,2,3}, {0,2,3}, {0,2,3},
263 {0,2,3}, {0,2,3}, {0,2,3}, {0,2,3},
264 {0,2,3}, {0,2,3}, {0,2,3}, {0,2,3},
265 {0,2,3}, {0,2,3}, {0,2,3}, {0,2,3},
266 {0,4,5}, {0,4,5}, {0,4,5}, {0,4,5},
267 {0,4,5}, {0,4,5}, {0,4,5}, {0,4,5},
268 {0,5,5}, {0,5,5}, {0,5,5}, {0,5,5},
269 {0,5,5}, {0,5,5}, {0,5,5}, {0,5,5},
270 {9,1,7}, {9,1,7}, {1,3,7}, {1,3,7},
271 {10,1,7}, {10,1,7}, {0,8,7}, {0,8,7},
272 {0,9,7}, {0,9,7}, {0,12,8}, {0,13,8},
273 {2,3,8}, {4,2,8}, {0,14,8}, {0,15,8}
276 /* Table B-14, DCT coefficients table zero,
277 * codes 0000001000 ... 0000001111
279 static dct_lookup_t pl_DCT_tab1[8] =
281 {16,1,10}, {5,2,10}, {0,7,10}, {2,3,10},
282 {1,4,10}, {15,1,10}, {14,1,10}, {4,2,10}
285 /* Table B-15, DCT coefficients table one,
286 * codes 000000100x ... 000000111x
288 static dct_lookup_t pl_DCT_tab1a[8] =
290 {5,2,9}, {5,2,9}, {14,1,9}, {14,1,9},
291 {2,4,10}, {16,1,10}, {15,1,9}, {15,1,9}
294 /* Table B-14/15, DCT coefficients table zero / one,
295 * codes 000000010000 ... 000000011111
297 static dct_lookup_t pl_DCT_tab2[16] =
299 {0,11,12}, {8,2,12}, {4,3,12}, {0,10,12},
300 {2,4,12}, {7,2,12}, {21,1,12}, {20,1,12},
301 {0,9,12}, {19,1,12}, {18,1,12}, {1,5,12},
302 {3,3,12}, {0,8,12}, {6,2,12}, {17,1,12}
305 /* Table B-14/15, DCT coefficients table zero / one,
306 * codes 0000000010000 ... 0000000011111
308 static dct_lookup_t pl_DCT_tab3[16] =
310 {10,2,13}, {9,2,13}, {5,3,13}, {3,4,13},
311 {2,5,13}, {1,7,13}, {1,6,13}, {0,15,13},
312 {0,14,13}, {0,13,13}, {0,12,13}, {26,1,13},
313 {25,1,13}, {24,1,13}, {23,1,13}, {22,1,13}
316 /* Table B-14/15, DCT coefficients table zero / one,
317 * codes 00000000010000 ... 00000000011111
319 static dct_lookup_t pl_DCT_tab4[16] =
321 {0,31,14}, {0,30,14}, {0,29,14}, {0,28,14},
322 {0,27,14}, {0,26,14}, {0,25,14}, {0,24,14},
323 {0,23,14}, {0,22,14}, {0,21,14}, {0,20,14},
324 {0,19,14}, {0,18,14}, {0,17,14}, {0,16,14}
327 /* Table B-14/15, DCT coefficients table zero / one,
328 * codes 000000000010000 ... 000000000011111
330 static dct_lookup_t pl_DCT_tab5[16] =
332 {0,40,15}, {0,39,15}, {0,38,15}, {0,37,15},
333 {0,36,15}, {0,35,15}, {0,34,15}, {0,33,15},
334 {0,32,15}, {1,14,15}, {1,13,15}, {1,12,15},
335 {1,11,15}, {1,10,15}, {1,9,15}, {1,8,15}
338 /* Table B-14/15, DCT coefficients table zero / one,
339 * codes 0000000000010000 ... 0000000000011111
341 static dct_lookup_t pl_DCT_tab6[16] =
343 {1,18,16}, {1,17,16}, {1,16,16}, {1,15,16},
344 {6,3,16}, {16,2,16}, {15,2,16}, {14,2,16},
345 {13,2,16}, {12,2,16}, {11,2,16}, {31,1,16},
346 {30,1,16}, {29,1,16}, {28,1,16}, {27,1,16}
351 * Initialization of lookup tables
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 )
363 p_vpar->pi_crop = p_vpar->pi_crop_buf + 4096;
365 for( i_dummy = -4096; i_dummy < -2048; i_dummy++ )
367 p_vpar->pi_crop[i_dummy] = -2048;
369 for( ; i_dummy < 2047; i_dummy++ )
371 p_vpar->pi_crop[i_dummy] = i_dummy;
373 for( ; i_dummy < 4095; i_dummy++ )
375 p_vpar->pi_crop[i_dummy] = 2047;
380 /*****************************************************************************
381 * vpar_InitMbAddrInc : Initialize the lookup table for mb_addr_inc
382 *****************************************************************************/
384 /* Function for filling up the lookup table for mb_addr_inc */
385 static void __inline__ FillMbAddrIncTable( vpar_thread_t * p_vpar,
386 int i_start, int i_end, int i_step,
387 int * pi_value, int i_length )
390 for( i_pos = i_start ; i_pos < i_end ; i_pos += i_step )
392 for( i_offset = 0 ; i_offset < i_step ; i_offset ++ )
394 p_vpar->pl_mb_addr_inc[i_pos + i_offset].i_value = * pi_value;
395 p_vpar->pl_mb_addr_inc[i_pos + i_offset].i_length = i_length;
401 /* Function that initialize the table using the last one */
402 void vpar_InitMbAddrInc( vpar_thread_t * p_vpar )
407 for( i_dummy = 0 ; i_dummy < 8 ; i_dummy++ )
409 p_vpar->pl_mb_addr_inc[i_dummy].i_value = MB_ERROR;
410 p_vpar->pl_mb_addr_inc[i_dummy].i_length = 0;
413 p_vpar->pl_mb_addr_inc[8].i_value = MB_ADDRINC_ESCAPE;
414 p_vpar->pl_mb_addr_inc[8].i_length = 11;
416 for( i_dummy = 9 ; i_dummy < 15 ; i_dummy ++ )
418 p_vpar->pl_mb_addr_inc[i_dummy].i_value = MB_ERROR;
419 p_vpar->pl_mb_addr_inc[i_dummy].i_length = 0;
422 p_vpar->pl_mb_addr_inc[15].i_value = MB_ADDRINC_STUFFING;
423 p_vpar->pl_mb_addr_inc[15].i_length = 11;
425 for( i_dummy = 16; i_dummy < 24; i_dummy++ )
427 p_vpar->pl_mb_addr_inc[i_dummy].i_value = MB_ERROR;
428 p_vpar->pl_mb_addr_inc[i_dummy].i_length = 0;
433 FillMbAddrIncTable( p_vpar, 24, 36, 1, &i_value, 11 );
434 FillMbAddrIncTable( p_vpar, 36, 48, 2, &i_value, 10 );
435 FillMbAddrIncTable( p_vpar, 48, 96, 8, &i_value, 8 );
436 FillMbAddrIncTable( p_vpar, 96, 128, 16, &i_value, 7 );
437 FillMbAddrIncTable( p_vpar, 128, 256, 64, &i_value, 5 );
438 FillMbAddrIncTable( p_vpar, 256, 512, 128, &i_value, 4 );
439 FillMbAddrIncTable( p_vpar, 512, 1024, 256, &i_value, 3 );
440 FillMbAddrIncTable( p_vpar, 1024, 2048, 1024, &i_value, 1 );
443 /*****************************************************************************
444 * vpar_Init*MBType : Initialize lookup table for the Macroblock type
445 *****************************************************************************/
447 /* Fonction for filling up the tables */
448 static void __inline__ FillMBType( vpar_thread_t * p_vpar,
457 for( i_dummy = i_start ; i_dummy < i_end ; i_dummy++ )
459 p_vpar->ppl_mb_type[i_mb_type][i_dummy].i_value = i_value;
460 p_vpar->ppl_mb_type[i_mb_type][i_dummy].i_length = i_length;
464 /* Fonction that fills the table for P MB_Type */
465 void vpar_InitPMBType( vpar_thread_t * p_vpar )
467 FillMBType( p_vpar, 0, 32, 64, MB_MOTION_FORWARD|MB_PATTERN, 1 );
468 FillMBType( p_vpar, 0, 16, 32, MB_PATTERN, 2 );
469 FillMBType( p_vpar, 0, 8, 16, MB_MOTION_FORWARD, 3 );
470 FillMBType( p_vpar, 0, 6, 8, MB_INTRA, 5 );
471 FillMBType( p_vpar, 0, 4, 6, MB_QUANT|MB_MOTION_FORWARD|MB_PATTERN, 5 );
472 FillMBType( p_vpar, 0, 2, 4, MB_QUANT|MB_PATTERN, 5 );
473 p_vpar->ppl_mb_type[0][1].i_value = MB_QUANT|MB_INTRA;
474 p_vpar->ppl_mb_type[0][1].i_length = 6;
475 p_vpar->ppl_mb_type[0][0].i_value = MB_ERROR;
476 p_vpar->ppl_mb_type[0][0].i_length = 0;
479 /* Fonction that fills the table for B MB_Type */
480 void vpar_InitBMBType( vpar_thread_t * p_vpar )
482 FillMBType( p_vpar, 1, 48, 64, MB_MOTION_FORWARD
483 |MB_MOTION_BACKWARD|MB_PATTERN, 2 );
484 FillMBType( p_vpar, 1, 32, 48, MB_MOTION_FORWARD|MB_MOTION_BACKWARD, 2 );
485 FillMBType( p_vpar, 1, 24, 32, MB_MOTION_BACKWARD|MB_PATTERN, 3 );
486 FillMBType( p_vpar, 1, 16, 24, MB_MOTION_BACKWARD, 3 );
487 FillMBType( p_vpar, 1, 12, 16, MB_MOTION_FORWARD|MB_PATTERN, 4 );
488 FillMBType( p_vpar, 1, 8, 12, MB_MOTION_FORWARD, 4 );
489 FillMBType( p_vpar, 1, 6, 8, MB_INTRA, 5 );
490 FillMBType( p_vpar, 1, 4, 6, MB_QUANT|MB_MOTION_FORWARD
491 |MB_MOTION_BACKWARD|MB_PATTERN, 5 );
492 p_vpar->ppl_mb_type[1][3].i_value = MB_QUANT|MB_MOTION_FORWARD|MB_PATTERN;
493 p_vpar->ppl_mb_type[1][3].i_length = 6;
494 p_vpar->ppl_mb_type[1][2].i_value = MB_QUANT|MB_MOTION_BACKWARD|MB_PATTERN;
495 p_vpar->ppl_mb_type[1][2].i_length = 6;
496 p_vpar->ppl_mb_type[1][1].i_value = MB_QUANT|MB_INTRA;
497 p_vpar->ppl_mb_type[1][1].i_length = 6;
498 p_vpar->ppl_mb_type[1][0].i_value =MB_ERROR;
499 p_vpar->ppl_mb_type[1][0].i_length = 0;
503 /*****************************************************************************
504 * vpar_InitDCTTables : Initialize tables giving the length of the dct
505 * coefficient from the vlc code
506 *****************************************************************************/
508 /* First fonction for filling the table */
509 static void __inline__ FillDCTTable( dct_lookup_t * p_tab_dest, dct_lookup_t * p_tab_src,
510 int i_step, int i_nb_elem, int i_offset )
512 int i_dummy, i_dummy2;
514 for( i_dummy=0 ; i_dummy < i_nb_elem ; i_dummy++ )
516 for( i_dummy2=0 ; i_dummy2 < i_step ; i_dummy2++ )
518 p_tab_dest[(i_dummy+i_offset)*i_step+i_dummy2] = p_tab_src[i_dummy];
524 /* Fonction that actually fills the table or create the pointers */
525 void vpar_InitDCTTables( vpar_thread_t * p_vpar )
527 /* Tables are cut in two parts to reduce memory occupation */
528 p_vpar->pppl_dct_dc_size[0][0] = pl_dct_dc_lum_init_table_1;
529 p_vpar->pppl_dct_dc_size[0][1] = pl_dct_dc_lum_init_table_2;
530 p_vpar->pppl_dct_dc_size[1][0] = pl_dct_dc_chrom_init_table_1;
531 p_vpar->pppl_dct_dc_size[1][1] = pl_dct_dc_chrom_init_table_2;
533 memset( p_vpar->ppl_dct_coef[0], MB_ERROR, 16 );
534 memset( p_vpar->ppl_dct_coef[1], MB_ERROR, 16 );
536 /* For table B14 & B15, we have a pointer to tables */
537 /* We fill the table thanks to the fonction defined above */
538 FillDCTTable( p_vpar->ppl_dct_coef[0], pl_DCT_tab0, 256, 60, 4 );
539 FillDCTTable( p_vpar->ppl_dct_coef[0], pl_DCT_tab1, 64, 8, 8 );
540 FillDCTTable( p_vpar->ppl_dct_coef[0], pl_DCT_tab2, 16, 16, 16 );
541 FillDCTTable( p_vpar->ppl_dct_coef[0], pl_DCT_tab3, 8, 16, 16 );
542 FillDCTTable( p_vpar->ppl_dct_coef[0], pl_DCT_tab4, 4, 16, 16 );
543 FillDCTTable( p_vpar->ppl_dct_coef[0], pl_DCT_tab5, 2, 16, 16 );
544 FillDCTTable( p_vpar->ppl_dct_coef[0], pl_DCT_tab6, 1, 16, 16 );
546 FillDCTTable( p_vpar->ppl_dct_coef[1], pl_DCT_tab0a, 256, 60, 4 );
547 FillDCTTable( p_vpar->ppl_dct_coef[1], pl_DCT_tab1a, 64, 8, 8 );
548 FillDCTTable( p_vpar->ppl_dct_coef[1], pl_DCT_tab2, 16, 16, 16 );
549 FillDCTTable( p_vpar->ppl_dct_coef[1], pl_DCT_tab3, 8, 16, 16 );
550 FillDCTTable( p_vpar->ppl_dct_coef[1], pl_DCT_tab4, 4, 16, 16 );
551 FillDCTTable( p_vpar->ppl_dct_coef[1], pl_DCT_tab5, 2, 16, 16 );
552 FillDCTTable( p_vpar->ppl_dct_coef[1], pl_DCT_tab6, 1, 16, 16 );
560 /*****************************************************************************
561 * DecodeMPEG1NonIntra : decode MPEG-1 non-intra blocks
562 *****************************************************************************/
563 static __inline__ void DecodeMPEG1NonIntra( vpar_thread_t * p_vpar,
564 macroblock_t * p_mb, int i_b,
565 int i_chroma_format )
568 if( p_vpar->picture.i_coding_type == D_CODING_TYPE )
570 /* Remove end_of_macroblock (always 1, prevents startcode emulation)
571 * ISO/IEC 11172-2 section 2.4.2.7 and 2.4.3.6 */
572 RemoveBits( &p_vpar->bit_stream, 1 );
576 /*****************************************************************************
577 * DecodeMPEG1Intra : decode MPEG-1 intra blocks
578 *****************************************************************************/
579 static __inline__ void DecodeMPEG1Intra( vpar_thread_t * p_vpar,
580 macroblock_t * p_mb, int i_b ,
581 int i_chroma_format )
584 if( p_vpar->picture.i_coding_type == D_CODING_TYPE )
586 /* Remove end_of_macroblock (always 1, prevents startcode emulation)
587 * ISO/IEC 11172-2 section 2.4.2.7 and 2.4.3.6 */
588 RemoveBits( &p_vpar->bit_stream, 1 );
592 /*****************************************************************************
593 * DecodeMPEG2NonIntra : decode MPEG-2 non-intra blocks
594 *****************************************************************************/
595 static __inline__ void DecodeMPEG2NonIntra( vpar_thread_t * p_vpar,
596 macroblock_t * p_mb, int i_b,
597 int i_chroma_format )
612 /* Lookup Table for the chromatic component */
613 static int pi_cc_index[12] = { 0, 0, 0, 0, 1, 2, 1, 2, 1, 2 };
615 i_cc = pi_cc_index[i_b];
617 /* Determine whether it is luminance or not (chrominance) */
618 i_type = ( i_cc + 1 ) >> 1;
620 /* Give a pointer to the quantization matrices for intra blocks */
621 if( (i_chroma_format == CHROMA_420) || (!i_type) )
623 pi_quant = p_vpar->sequence.nonintra_quant.pi_matrix;
627 pi_quant = p_vpar->sequence.chroma_nonintra_quant.pi_matrix;
630 /* Decoding of the AC coefficients */
634 for( i_parse = 0; ; i_parse++ )
636 i_code = ShowBits( &p_vpar->bit_stream, 16 );
637 if( i_code >= 16384 )
641 i_run = pl_DCT_tab_dc[(i_code>>12)-4].i_run;
642 i_level = pl_DCT_tab_dc[(i_code>>12)-4].i_level;
643 i_length = pl_DCT_tab_dc[(i_code>>12)-4].i_length;
647 i_run = pl_DCT_tab_ac[(i_code>>12)-4].i_run;
648 i_level = pl_DCT_tab_ac[(i_code>>12)-4].i_level;
649 i_length = pl_DCT_tab_ac[(i_code>>12)-4].i_length;
652 else if( i_code >= 1024 )
654 i_run = pl_DCT_tab0[(i_code>>8)-4].i_run;
655 i_length = pl_DCT_tab0[(i_code>>8)-4].i_length;
656 i_level = pl_DCT_tab0[(i_code>>8)-4].i_level;
660 i_run = p_vpar->ppl_dct_coef[0][i_code].i_run;
661 i_length = p_vpar->ppl_dct_coef[0][i_code].i_length;
662 i_level = p_vpar->ppl_dct_coef[0][i_code].i_level;
666 RemoveBits( &p_vpar->bit_stream, i_length );
671 i_run = GetBits( &p_vpar->bit_stream, 6 );
672 i_level = GetBits( &p_vpar->bit_stream, 12 );
673 i_level = (b_sign = ( i_level > 2047 )) ? 4096 - i_level
679 p_mb->pf_idct[i_b] = vdec_SparseIDCT;
680 p_mb->pi_sparse_pos[i_b] = i_coef;
684 p_mb->pf_idct[i_b] = vdec_IDCT;
690 b_sign = GetBits( &p_vpar->bit_stream, 1 );
701 i_pos = pi_scan[p_vpar->picture.b_alternate_scan][i_parse];
702 i_level = ( ((i_level << 1) + 1) * p_vpar->mb.i_quantizer_scale
703 * pi_quant[i_pos] ) >> 5;
704 p_mb->ppi_blocks[i_b][i_pos] = b_sign ? -i_level : i_level;
707 intf_ErrMsg("vpar error: DCT coeff (non-intra) is out of bounds\n");
708 p_vpar->picture.b_error = 1;
711 /*****************************************************************************
712 * DecodeMPEG2Intra : decode MPEG-2 intra blocks
713 *****************************************************************************/
714 static __inline__ void DecodeMPEG2Intra( vpar_thread_t * p_vpar,
715 macroblock_t * p_mb, int i_b,
716 int i_chroma_format )
730 boolean_t b_vlc_intra;
734 /* Lookup Table for the chromatic component */
735 static int pi_cc_index[12] = { 0, 0, 0, 0, 1, 2, 1, 2, 1, 2 };
736 i_cc = pi_cc_index[i_b];
738 /* Determine whether it is luminance or not (chrominance) */
739 i_type = ( i_cc + 1 ) >> 1;
741 /* Give a pointer to the quantization matrices for intra blocks */
742 if( (i_chroma_format == CHROMA_420) || (!i_type) )
744 pi_quant = p_vpar->sequence.intra_quant.pi_matrix;
748 pi_quant = p_vpar->sequence.chroma_intra_quant.pi_matrix;
752 /* Decoding of the DC intra coefficient */
753 /* The nb of bits to parse depends on i_type */
754 i_code = ShowBits( &p_vpar->bit_stream, 9 + i_type );
756 /* To reduce memory occupation, there are two lookup tables
757 * See InitDCT above */
758 i_code5 = i_code >> (4+i_type);
760 /* Shall we lookup in the first or in the second table ? */
761 i_select = ( i_code5 == 31 );
762 /* Offset value for looking in the second table */
763 i_offset = 0x1f0 + ( i_type * 0x1f0 );
764 i_pos = ( i_code5 * ( ! i_select ) ) + ( ( i_code - i_offset ) * i_select );
765 i_dct_dc_size = p_vpar->pppl_dct_dc_size[i_type][i_select][i_pos].i_value;
768 if( !i_type/*i_b < 4*/ )
771 i_code = ShowBits(&p_vpar->bit_stream, 5);
774 i_dct_dc_size = pl_dct_dc_lum_init_table_1[i_code].i_value;
775 i_length = pl_dct_dc_lum_init_table_1[i_code].i_length;
776 RemoveBits( &p_vpar->bit_stream, i_length);
780 i_code = ShowBits(&p_vpar->bit_stream, 9) - 0x1f0;
781 i_dct_dc_size = pl_dct_dc_lum_init_table_2[i_code].i_value;
782 i_length = pl_dct_dc_lum_init_table_2[i_code].i_length;
783 RemoveBits( &p_vpar->bit_stream, i_length);
789 i_code = ShowBits(&p_vpar->bit_stream, 5);
793 i_dct_dc_size = pl_dct_dc_chrom_init_table_1[i_code].i_value;
794 i_length = pl_dct_dc_chrom_init_table_1[i_code].i_length;
795 RemoveBits(&p_vpar->bit_stream, i_length);
799 i_code = ShowBits(&p_vpar->bit_stream, 10) - 0x3e0;
800 i_dct_dc_size = pl_dct_dc_chrom_init_table_2[i_code].i_value;
801 i_length = pl_dct_dc_chrom_init_table_2[i_code].i_length;
802 RemoveBits( &p_vpar->bit_stream, i_length);
805 if (i_dct_dc_size==0)
809 i_dct_dc_diff = GetBits( &p_vpar->bit_stream, i_dct_dc_size);
810 if ((i_dct_dc_diff & (1<<(i_dct_dc_size-1)))==0)
811 i_dct_dc_diff-= (1<<i_dct_dc_size) - 1;
814 /* Dump the variable length code */
815 //RemoveBits( &p_vpar->bit_stream,
816 // p_vpar->pppl_dct_dc_size[i_type][i_select][i_pos].i_length );
818 /* Read the actual code with the good length */
819 p_vpar->mb.pi_dc_dct_pred[i_cc] += i_dct_dc_diff;
821 p_mb->ppi_blocks[i_b][0] = ( p_vpar->mb.pi_dc_dct_pred[i_cc] <<
822 ( 3 - p_vpar->picture.i_intra_dc_precision ) );
823 i_nc = ( p_vpar->mb.pi_dc_dct_pred[i_cc] != 0 );
825 /* Decoding of the AC coefficients */
828 b_vlc_intra = p_vpar->picture.b_intra_vlc_format;
829 for( i_parse = 1; /*i_parse < 64*/; i_parse++ )
831 i_code = ShowBits( &p_vpar->bit_stream, 16 );
832 if( i_code >= 16384 )
836 i_run = pl_DCT_tab0a[(i_code>>8)-4].i_run;
837 i_level = pl_DCT_tab0a[(i_code>>8)-4].i_level;
838 i_length = pl_DCT_tab0a[(i_code>>8)-4].i_length;
839 //fprintf( stderr, "**********> %d, %d, %d *******\n", i_run, i_level, (i_code>>8)-4 );
843 i_run = pl_DCT_tab_ac[(i_code>>12)-4].i_run;
844 i_level = pl_DCT_tab_ac[(i_code>>12)-4].i_level;
845 i_length = pl_DCT_tab_ac[(i_code>>12)-4].i_length;
850 i_run = p_vpar->ppl_dct_coef[b_vlc_intra][i_code].i_run;
851 i_length = p_vpar->ppl_dct_coef[b_vlc_intra][i_code].i_length;
852 i_level = p_vpar->ppl_dct_coef[b_vlc_intra][i_code].i_level;
858 int intra_vlc_format = b_vlc_intra;
861 if (code>=16384 && !intra_vlc_format)
862 tab = pl_DCT_tab_ac[(code>>12)-4];
865 if (intra_vlc_format)
866 tab = pl_DCT_tab0a[(code>>8)-4];
868 tab = pl_DCT_tab0[(code>>8)-4];
872 if (intra_vlc_format)
873 tab = pl_DCT_tab1a[(code>>6)-8];
875 tab = pl_DCT_tab1[(code>>6)-8];
878 tab = pl_DCT_tab2[(code>>4)-16];
880 tab = pl_DCT_tab3[(code>>3)-16];
882 tab = pl_DCT_tab4[(code>>2)-16];
884 tab = pl_DCT_tab5[(code>>1)-16];
886 tab = pl_DCT_tab6[code-16];
889 fprintf( stderr, "invalid Huffman code in Decode_MPEG2_Intra_Block()\n");
892 if( (i_run != tab.i_run) || (i_length != tab.i_length) || (i_level != tab.i_level) )
894 fprintf( stderr, "ET M....... !!!\n" );
903 RemoveBits( &p_vpar->bit_stream, i_length );
908 i_run = GetBits( &p_vpar->bit_stream, 6 );
909 i_level = GetBits( &p_vpar->bit_stream, 12 );
910 /*p_mb->ppi_blocks[i_b][i_parse] = ( b_sign = ( i_level > 2047 ) )
911 ? ( -4096 + i_level )
913 i_level = (b_sign = ( i_level > 2047 )) ? 4096 - i_level
919 p_mb->pf_idct[i_b] = vdec_SparseIDCT;
920 p_mb->pi_sparse_pos[i_b] = i_coef;
924 p_mb->pf_idct[i_b] = vdec_IDCT;
930 b_sign = GetBits( &p_vpar->bit_stream, 1 );
942 i_pos = pi_scan[p_vpar->picture.b_alternate_scan][i_parse];
943 i_level = ( i_level *
944 p_vpar->mb.i_quantizer_scale *
945 pi_quant[i_pos] ) >> 4;
946 p_mb->ppi_blocks[i_b][i_pos] = b_sign ? -i_level : i_level;
949 intf_ErrMsg("vpar error: DCT coeff (intra) is out of bounds\n");
950 p_vpar->picture.b_error = 1;
958 /****************************************************************************
959 * MotionCode : Parse the next motion code
960 ****************************************************************************/
961 static __inline__ int MotionCode( vpar_thread_t * p_vpar )
964 static lookup_t pl_mv_tab0[8] =
965 { {-1,0}, {3,3}, {2,2}, {2,2}, {1,1}, {1,1}, {1,1}, {1,1} };
966 /* Table B-10, motion_code, codes 0000011 ... 000011x */
967 static lookup_t pl_mv_tab1[8] =
968 { {-1,0}, {-1,0}, {-1,0}, {7,6}, {6,6}, {5,6}, {4,5}, {4,5} };
969 /* Table B-10, motion_code, codes 0000001100 ... 000001011x */
970 static lookup_t pl_mv_tab2[12] = {
971 {16,9}, {15,9}, {14,9}, {13,9},
972 {12,9}, {11,9}, {10,8}, {10,8},
973 {9,8}, {9,8}, {8,8}, {8,8} };
975 if( GetBits( &p_vpar->bit_stream, 1 ) )
979 if( (i_code = ShowBits( &p_vpar->bit_stream, 9) ) >= 64 )
982 RemoveBits( &p_vpar->bit_stream, pl_mv_tab0[i_code].i_length );
983 return( GetBits( &p_vpar->bit_stream, 1 ) ?
984 -pl_mv_tab0[i_code].i_value : pl_mv_tab0[i_code].i_value );
990 RemoveBits( &p_vpar->bit_stream, pl_mv_tab1[i_code].i_length );
991 return( GetBits( &p_vpar->bit_stream, 1 ) ?
992 -pl_mv_tab1[i_code].i_value : pl_mv_tab1[i_code].i_value );
995 if( (i_code -= 12) < 0 )
997 p_vpar->picture.b_error = 1;
998 intf_DbgMsg( "vpar debug: Invalid motion_vector code\n" );
1002 RemoveBits( &p_vpar->bit_stream, pl_mv_tab2[i_code].i_length );
1003 return( GetBits( &p_vpar->bit_stream, 1 ) ?
1004 -pl_mv_tab2[i_code].i_value : pl_mv_tab2[i_code].i_value );
1007 /****************************************************************************
1008 * DecodeMotionVector : Decode a motion_vector
1009 ****************************************************************************/
1010 static __inline__ void DecodeMotionVector( int * pi_prediction, int i_r_size,
1011 int i_motion_code, int i_motion_residual, int i_full_pel )
1013 int i_limit, i_vector;
1015 /* ISO/IEC 13818-1 section 7.6.3.1 */
1016 i_limit = 16 << i_r_size;
1017 i_vector = *pi_prediction >> i_full_pel;
1019 if( i_motion_code > 0 )
1021 i_vector += ((i_motion_code-1) << i_r_size) + i_motion_residual + 1;
1022 if( i_vector >= i_limit )
1023 i_vector -= i_limit + i_limit;
1025 else if( i_motion_code < 0 )
1027 i_vector -= ((-i_motion_code-1) << i_r_size) + i_motion_residual + 1;
1028 if( i_vector < -i_limit )
1029 i_vector += i_limit + i_limit;
1031 *pi_prediction = i_vector << i_full_pel;
1034 /****************************************************************************
1035 * MotionVector : Parse the next motion_vector field
1036 ****************************************************************************/
1037 static __inline__ void MotionVector( vpar_thread_t * p_vpar,
1038 macroblock_t * p_mb, int i_r,
1039 int i_s, int i_full_pel, int i_structure )
1041 int i_motion_code, i_motion_residual;
1043 int pi_dm_vector[2];
1045 i_r_size = p_vpar->picture.ppi_f_code[i_s][0] - 1;
1046 i_motion_code = MotionCode( p_vpar );
1047 i_motion_residual = (i_r_size != 0 && i_motion_code != 0) ?
1048 GetBits( &p_vpar->bit_stream, i_r_size) : 0;
1049 DecodeMotionVector( &p_vpar->mb.pppi_pmv[i_r][i_s][0], i_r_size,
1050 i_motion_code, i_motion_residual, i_full_pel );
1051 p_mb->pppi_motion_vectors[i_r][i_s][0] = p_vpar->mb.pppi_pmv[i_r][i_s][0];
1053 if( p_vpar->mb.b_dmv )
1055 if( GetBits(&p_vpar->bit_stream, 1) )
1057 pi_dm_vector[0] = GetBits( &p_vpar->bit_stream, 1 ) ? -1 : 1;
1061 pi_dm_vector[0] = 0;
1065 i_r_size = p_vpar->picture.ppi_f_code[i_s][1]-1;
1066 i_motion_code = MotionCode( p_vpar );
1067 i_motion_residual = (i_r_size != 0 && i_motion_code != 0) ?
1068 GetBits( &p_vpar->bit_stream, i_r_size) : 0;
1071 if( (p_vpar->mb.i_mv_format == MOTION_FIELD)
1072 && (i_structure == FRAME_STRUCTURE) )
1074 p_vpar->mb.pppi_pmv[i_r][i_s][1] >>= 1;
1077 DecodeMotionVector( &p_vpar->mb.pppi_pmv[i_r][i_s][1], i_r_size,
1078 i_motion_code, i_motion_residual, i_full_pel );
1080 if( (p_vpar->mb.i_mv_format == MOTION_FIELD)
1081 && (i_structure == FRAME_STRUCTURE) )
1082 p_vpar->mb.pppi_pmv[i_r][i_s][1] <<= 1;
1084 p_mb->pppi_motion_vectors[i_r][i_s][1] = p_vpar->mb.pppi_pmv[i_r][i_s][1];
1086 if( p_vpar->mb.b_dmv )
1088 if( GetBits(&p_vpar->bit_stream, 1) )
1090 pi_dm_vector[1] = GetBits( &p_vpar->bit_stream, 1 ) ? -1 : 1;
1094 pi_dm_vector[1] = 0;
1097 /* Dual Prime Arithmetic (ISO/IEC 13818-2 section 7.6.3.6). */
1099 #define i_mv_x p_mb->pppi_motion_vectors[0][0][0]
1100 if( i_structure == FRAME_STRUCTURE )
1102 #define i_mv_y (p_mb->pppi_motion_vectors[0][0][1] << 1)
1103 if( p_vpar->picture.b_top_field_first )
1105 /* vector for prediction of top field from bottom field */
1106 p_mb->ppi_dmv[0][0] = ((i_mv_x + (i_mv_x > 0)) >> 1) + pi_dm_vector[0];
1107 p_mb->ppi_dmv[0][1] = ((i_mv_y + (i_mv_y > 0)) >> 1) + pi_dm_vector[1] - 1;
1109 /* vector for prediction of bottom field from top field */
1110 p_mb->ppi_dmv[1][0] = ((3*i_mv_x + (i_mv_x > 0)) >> 1) + pi_dm_vector[0];
1111 p_mb->ppi_dmv[1][1] = ((3*i_mv_y + (i_mv_y > 0)) >> 1) + pi_dm_vector[1] + 1;
1115 /* vector for prediction of top field from bottom field */
1116 p_mb->ppi_dmv[0][0] = ((3*i_mv_x + (i_mv_x > 0)) >> 1) + pi_dm_vector[0];
1117 p_mb->ppi_dmv[0][1] = ((3*i_mv_y + (i_mv_y > 0)) >> 1) + pi_dm_vector[1] - 1;
1119 /* vector for prediction of bottom field from top field */
1120 p_mb->ppi_dmv[1][0] = ((i_mv_x + (i_mv_x > 0)) >> 1) + pi_dm_vector[0];
1121 p_mb->ppi_dmv[1][1] = ((i_mv_y + (i_mv_y > 0)) >> 1) + pi_dm_vector[1] + 1;
1127 #define i_mv_y p_mb->pppi_motion_vectors[0][0][1]
1128 /* vector for prediction from field of opposite 'parity' */
1129 p_mb->ppi_dmv[0][0] = ((i_mv_x + (i_mv_x > 0)) >> 1) + pi_dm_vector[0];
1130 p_mb->ppi_dmv[0][1] = ((i_mv_y + (i_mv_y > 0)) >> 1) + pi_dm_vector[1];
1132 /* correct for vertical field shift */
1133 if( p_vpar->picture.i_structure == TOP_FIELD )
1134 p_mb->ppi_dmv[0][1]--;
1136 p_mb->ppi_dmv[0][1]++;
1143 /*****************************************************************************
1144 * DecodeMVMPEG1 : Parse the next MPEG-1 motion vectors
1145 *****************************************************************************/
1146 static __inline__ void DecodeMVMPEG1( vpar_thread_t * p_vpar,
1147 macroblock_t * p_mb, int i_s, int i_structure )
1149 MotionVector( p_vpar, p_mb, 0, i_s,
1150 p_vpar->picture.pb_full_pel_vector[i_s], i_structure );
1153 /*****************************************************************************
1154 * DecodeMVMPEG2 : Parse the next MPEG-2 motion_vectors field
1155 *****************************************************************************/
1156 static __inline__ void DecodeMVMPEG2( vpar_thread_t * p_vpar,
1157 macroblock_t * p_mb, int i_s, int i_structure )
1159 if( p_vpar->mb.i_mv_count == 1 )
1161 if( p_vpar->mb.i_mv_format == MOTION_FIELD && !p_vpar->mb.b_dmv )
1163 p_mb->ppi_field_select[0][i_s] = p_mb->ppi_field_select[1][i_s]
1164 = GetBits( &p_vpar->bit_stream, 1 );
1166 MotionVector( p_vpar, p_mb, 0, i_s, 0, i_structure );
1167 p_vpar->mb.pppi_pmv[1][i_s][0] = p_vpar->mb.pppi_pmv[0][i_s][0];
1168 p_vpar->mb.pppi_pmv[1][i_s][1] = p_vpar->mb.pppi_pmv[0][i_s][1];
1169 p_mb->pppi_motion_vectors[1][i_s][0] = p_vpar->mb.pppi_pmv[0][i_s][0];
1170 p_mb->pppi_motion_vectors[1][i_s][1] = p_vpar->mb.pppi_pmv[0][i_s][1];
1174 p_mb->ppi_field_select[0][i_s] = GetBits( &p_vpar->bit_stream, 1 );
1175 MotionVector( p_vpar, p_mb, 0, i_s, 0, i_structure );
1176 p_mb->ppi_field_select[1][i_s] = GetBits( &p_vpar->bit_stream, 1 );
1177 MotionVector( p_vpar, p_mb, 1, i_s, 0, i_structure );
1183 * Macroblock information structures
1186 /*****************************************************************************
1187 * MacroblockAddressIncrement : Get the macroblock_address_increment field
1188 *****************************************************************************/
1189 static __inline__ int MacroblockAddressIncrement( vpar_thread_t * p_vpar )
1192 /* Index in the lookup table mb_addr_inc */
1193 int i_index = ShowBits( &p_vpar->bit_stream, 11 );
1195 /* Test the presence of the escape character */
1196 while( i_index == 8 )
1198 RemoveBits( &p_vpar->bit_stream, 11 );
1200 i_index = ShowBits( &p_vpar->bit_stream, 11 );
1203 /* Affect the value from the lookup table */
1204 i_addr_inc += p_vpar->pl_mb_addr_inc[i_index].i_value;
1206 /* Dump the good number of bits */
1207 RemoveBits( &p_vpar->bit_stream, p_vpar->pl_mb_addr_inc[i_index].i_length );
1212 /*****************************************************************************
1213 * IMBType : macroblock_type in I pictures
1214 *****************************************************************************/
1215 static __inline__ int IMBType( vpar_thread_t * p_vpar )
1217 /* Take two bits for testing */
1218 int i_type = ShowBits( &p_vpar->bit_stream, 2 );
1220 /* Lookup table for macroblock_type */
1221 static lookup_t pl_mb_Itype[4] = { {MB_ERROR, 0},
1222 {MB_QUANT|MB_INTRA, 2},
1225 /* Dump the good number of bits */
1226 RemoveBits( &p_vpar->bit_stream, pl_mb_Itype[i_type].i_length );
1227 return pl_mb_Itype[i_type].i_value;
1230 /*****************************************************************************
1231 * PMBType : macroblock_type in P pictures
1232 *****************************************************************************/
1233 static __inline__ int PMBType( vpar_thread_t * p_vpar )
1235 /* Testing on 6 bits */
1236 int i_type = ShowBits( &p_vpar->bit_stream, 6 );
1238 /* Dump the good number of bits */
1239 RemoveBits( &p_vpar->bit_stream, p_vpar->ppl_mb_type[0][i_type].i_length );
1240 /* return the value from the lookup table for P type */
1241 return p_vpar->ppl_mb_type[0][i_type].i_value;
1244 /*****************************************************************************
1245 * BMBType : macroblock_type in B pictures
1246 *****************************************************************************/
1247 static __inline__ int BMBType( vpar_thread_t * p_vpar )
1249 /* Testing on 6 bits */
1250 int i_type = ShowBits( &p_vpar->bit_stream, 6 );
1252 /* Dump the good number of bits */
1253 RemoveBits( &p_vpar->bit_stream, p_vpar->ppl_mb_type[1][i_type].i_length );
1255 /* return the value from the lookup table for B type */
1256 return p_vpar->ppl_mb_type[1][i_type].i_value;
1259 /*****************************************************************************
1260 * DMBType : macroblock_type in D pictures
1261 *****************************************************************************/
1262 static __inline__ int DMBType( vpar_thread_t * p_vpar )
1264 return GetBits( &p_vpar->bit_stream, 1 );
1267 /*****************************************************************************
1268 * CodedPattern420 : coded_block_pattern with 4:2:0 chroma
1269 *****************************************************************************/
1270 static __inline__ int CodedPattern420( vpar_thread_t * p_vpar )
1272 /* Take the max 9 bits length vlc code for testing */
1273 int i_vlc = ShowBits( &p_vpar->bit_stream, 9 );
1275 /* Trash the good number of bits read in the lookup table */
1276 RemoveBits( &p_vpar->bit_stream, pl_coded_pattern[i_vlc].i_length );
1278 /* return the value from the vlc table */
1279 return pl_coded_pattern[i_vlc].i_value;
1282 /*****************************************************************************
1283 * CodedPattern422 : coded_block_pattern with 4:2:2 chroma
1284 *****************************************************************************/
1285 static __inline__ int CodedPattern422( vpar_thread_t * p_vpar )
1287 int i_vlc = ShowBits( &p_vpar->bit_stream, 9 );
1289 RemoveBits( &p_vpar->bit_stream, pl_coded_pattern[i_vlc].i_length );
1291 /* Supplementary 2 bits long code for 4:2:2 format */
1292 return pl_coded_pattern[i_vlc].i_value |
1293 (GetBits( &p_vpar->bit_stream, 2 ) << 6);
1296 /*****************************************************************************
1297 * CodedPattern444 : coded_block_pattern with 4:4:4 chroma
1298 *****************************************************************************/
1299 static __inline__ int CodedPattern444( vpar_thread_t * p_vpar )
1301 int i_vlc = ShowBits( &p_vpar->bit_stream, 9 );
1303 RemoveBits( &p_vpar->bit_stream, pl_coded_pattern[i_vlc].i_length );
1305 return pl_coded_pattern[i_vlc].i_value |
1306 (GetBits( &p_vpar->bit_stream, 6 ) << 6);
1309 /*****************************************************************************
1310 * InitMacroblock : Initialize macroblock values
1311 *****************************************************************************/
1312 static __inline__ void InitMacroblock( vpar_thread_t * p_vpar,
1313 macroblock_t * p_mb, int i_coding_type,
1314 int i_chroma_format,
1316 boolean_t b_second_field )
1318 p_mb->i_chroma_nb_blocks = 1 << i_chroma_format;
1319 p_mb->p_picture = p_vpar->picture.p_picture;
1321 if( i_coding_type == B_CODING_TYPE )
1322 p_mb->p_backward = p_vpar->sequence.p_backward;
1324 p_mb->p_backward = NULL;
1325 if( (i_coding_type == P_CODING_TYPE) || (i_coding_type == B_CODING_TYPE) )
1326 p_mb->p_forward = p_vpar->sequence.p_forward;
1328 p_mb->p_forward = NULL;
1330 p_mb->i_l_x = p_vpar->mb.i_l_x;
1331 p_mb->i_c_x = p_vpar->mb.i_c_x;
1332 p_mb->i_motion_l_y = p_vpar->mb.i_l_y;
1333 p_mb->i_motion_c_y = p_vpar->mb.i_c_y;
1334 if( (p_mb->b_motion_field = (i_structure == BOTTOM_FIELD)) )
1336 p_mb->i_motion_l_y--;
1337 p_mb->i_motion_c_y--;
1339 p_mb->i_addb_l_stride = (p_mb->i_l_stride = p_vpar->picture.i_l_stride) - 8;
1340 p_mb->i_addb_c_stride = (p_mb->i_c_stride = p_vpar->picture.i_c_stride) - 8;
1341 p_mb->b_P_second = ( b_second_field && i_coding_type == P_CODING_TYPE );
1344 /*****************************************************************************
1345 * UpdateContext : Update the p_vpar contextual values
1346 *****************************************************************************/
1347 static __inline__ void UpdateContext( vpar_thread_t * p_vpar, int i_structure )
1349 /* Update macroblock real position. */
1350 p_vpar->mb.i_l_x += 16;
1351 p_vpar->mb.i_l_y += (p_vpar->mb.i_l_x / p_vpar->sequence.i_width)
1352 * (2 - (i_structure == FRAME_STRUCTURE)) * 16;
1353 p_vpar->mb.i_l_x %= p_vpar->sequence.i_width;
1355 p_vpar->mb.i_c_x += p_vpar->sequence.i_chroma_mb_width;
1356 p_vpar->mb.i_c_y += (p_vpar->mb.i_c_x / p_vpar->sequence.i_chroma_width)
1357 * (2 - (i_structure == FRAME_STRUCTURE))
1358 * p_vpar->sequence.i_chroma_mb_height;
1359 p_vpar->mb.i_c_x %= p_vpar->sequence.i_chroma_width;
1362 /*****************************************************************************
1363 * SkippedMacroblock : Generate a skipped macroblock with NULL motion vector
1364 *****************************************************************************/
1365 static __inline__ void SkippedMacroblock( vpar_thread_t * p_vpar, int i_mb,
1366 int i_mb_base, int i_coding_type,
1367 int i_chroma_format,
1369 boolean_t b_second_field )
1371 macroblock_t * p_mb;
1373 static f_motion_t pf_motion_skipped[4][4] =
1375 {NULL, NULL, NULL, NULL},
1376 {NULL, vdec_MotionFieldField420, vdec_MotionFieldField420,
1377 vdec_MotionFrameFrame420},
1378 {NULL, vdec_MotionFieldField422, vdec_MotionFieldField422,
1379 vdec_MotionFrameFrame422},
1380 {NULL, vdec_MotionFieldField444, vdec_MotionFieldField444,
1381 vdec_MotionFrameFrame444},
1384 if( (p_mb = vpar_NewMacroblock( &p_vpar->vfifo )) == NULL )
1390 p_vpar->picture.pp_mb[i_mb_base + i_mb] = p_mb;
1393 InitMacroblock( p_vpar, p_mb, i_coding_type, i_chroma_format,
1394 i_structure, b_second_field );
1396 /* Motion type is picture structure. */
1397 p_mb->pf_motion = pf_motion_skipped[i_chroma_format]
1399 p_mb->i_mb_type = MB_MOTION_FORWARD;
1400 p_mb->i_coded_block_pattern = 0;
1401 memset( p_mb->pppi_motion_vectors, 0, 8*sizeof(int) );
1403 /* Set the field we use for motion compensation */
1404 p_mb->ppi_field_select[0][0] = p_mb->ppi_field_select[0][1]
1405 = ( i_structure == BOTTOM_FIELD );
1407 UpdateContext( p_vpar, i_structure );
1410 /* Decode the macroblock NOW ! */
1411 vdec_DecodeMacroblock( p_vpar->pp_vdec[0], p_mb );
1415 /*****************************************************************************
1416 * MacroblockModes : Get the macroblock_modes structure
1417 *****************************************************************************/
1418 static __inline__ void MacroblockModes( vpar_thread_t * p_vpar,
1419 macroblock_t * p_mb,
1420 int i_chroma_format,
1424 static int ppi_mv_count[2][4] = { {0, 1, 2, 1}, {0, 2, 1, 1} };
1425 static int ppi_mv_format[2][4] = { {0, 1, 1, 1}, {0, 1, 2, 1} };
1427 /* Get macroblock_type. */
1428 switch( i_coding_type )
1431 p_mb->i_mb_type = PMBType( p_vpar );
1434 p_mb->i_mb_type = BMBType( p_vpar );
1437 p_mb->i_mb_type = IMBType( p_vpar );
1440 p_mb->i_mb_type = DMBType( p_vpar );
1443 /* SCALABILITY : warning, we don't know if spatial_temporal_weight_code
1444 * has to be dropped, take care if you use scalable streams. */
1445 /* RemoveBits( &p_vpar->bit_stream, 2 ); */
1447 if( (i_coding_type == P_CODING_TYPE || i_coding_type == B_CODING_TYPE)
1448 && (p_mb->i_mb_type & (MB_MOTION_FORWARD | MB_MOTION_BACKWARD)) )
1450 if( !(i_structure == FRAME_STRUCTURE
1451 && p_vpar->picture.b_frame_pred_frame_dct) )
1453 p_vpar->mb.i_motion_type = GetBits( &p_vpar->bit_stream, 2 );
1457 p_vpar->mb.i_motion_type = MOTION_FRAME;
1461 p_vpar->mb.i_mv_count = ppi_mv_count[i_structure == FRAME_STRUCTURE]
1462 [p_vpar->mb.i_motion_type];
1463 p_vpar->mb.i_mv_format = ppi_mv_format[i_structure == FRAME_STRUCTURE]
1464 [p_vpar->mb.i_motion_type];
1465 p_vpar->mb.b_dmv = p_vpar->mb.i_motion_type == MOTION_DMV;
1468 p_vpar->mb.b_dct_type = 0;
1469 if( (i_structure == FRAME_STRUCTURE) &&
1470 (!p_vpar->picture.b_frame_pred_frame_dct) &&
1471 (p_mb->i_mb_type & (MB_PATTERN|MB_INTRA)) )
1473 if( (p_vpar->mb.b_dct_type = GetBits( &p_vpar->bit_stream, 1 )) )
1475 /* The DCT is coded on fields. Jump one line between each
1477 p_mb->i_addb_l_stride <<= 1;
1478 p_mb->i_addb_l_stride += 8;
1479 /* With CHROMA_420, the DCT is necessarily frame-coded. */
1480 if( i_chroma_format != CHROMA_420 )
1482 p_mb->i_addb_c_stride <<= 1;
1483 p_mb->i_addb_c_stride += 8;
1489 /*****************************************************************************
1490 * ParseMacroblock : Parse the next macroblock
1491 *****************************************************************************/
1492 #define PARSEBLOCKS( MPEG1FUNC, MPEG2FUNC ) \
1494 i_mask = 1 << (3 + (1 << i_chroma_format)); \
1497 p_data1 = p_mb->p_picture->p_y \
1498 + p_mb->i_l_x + p_vpar->mb.i_l_y*(p_vpar->sequence.i_width); \
1500 for( i_b = 0 ; i_b < 4 ; i_b++, i_mask >>= 1 ) \
1502 if( p_mb->i_coded_block_pattern & i_mask ) \
1504 memset( p_mb->ppi_blocks[i_b], 0, 64*sizeof(dctelem_t) ); \
1506 MPEG2FUNC( p_vpar, p_mb, i_b, i_chroma_format ); \
1508 MPEG1FUNC( p_vpar, p_mb, i_b, i_chroma_format ); \
1510 /* Calculate block coordinates. */ \
1511 p_mb->p_data[i_b] = p_data1 \
1512 + pi_y[p_vpar->mb.b_dct_type][i_b] \
1513 * p_vpar->sequence.i_width \
1518 if( p_vpar->picture.b_error ) \
1520 /* Mark this block as skipped (better than green blocks), and \
1521 * go to the next slice. */ \
1522 (*pi_mb_address)--; \
1523 vpar_DestroyMacroblock( &p_vpar->vfifo, p_mb ); \
1528 p_data1 = p_mb->p_picture->p_u \
1530 + p_vpar->mb.i_c_y \
1531 * (p_vpar->sequence.i_chroma_width); \
1532 p_data2 = p_mb->p_picture->p_v \
1534 + p_vpar->mb.i_c_y \
1535 * (p_vpar->sequence.i_chroma_width); \
1537 for( i_b = 4; i_b < 4 + (1 << i_chroma_format); \
1538 i_b++, i_mask >>= 1 ) \
1540 yuv_data_t * pp_data[2] = {p_data1, p_data2}; \
1542 if( p_mb->i_coded_block_pattern & i_mask ) \
1544 memset( p_mb->ppi_blocks[i_b], 0, 64*sizeof(dctelem_t) ); \
1546 MPEG2FUNC( p_vpar, p_mb, i_b, i_chroma_format ); \
1548 MPEG1FUNC( p_vpar, p_mb, i_b, i_chroma_format ); \
1550 /* Calculate block coordinates. */ \
1551 p_mb->p_data[i_b] = pp_data[i_b & 1] \
1552 + pi_y[p_vpar->mb.b_dct_type][i_b] \
1553 * p_vpar->sequence.i_chroma_width \
1559 static __inline__ void ParseMacroblock(
1560 vpar_thread_t * p_vpar,
1561 int * pi_mb_address, /* previous address to be
1562 * used for mb_addr_incr */
1563 int i_mb_previous, /* actual previous mb */
1564 int i_mb_base, /* non-zero if field structure */
1565 /* The following parameters are explicit in
1566 * optimized routines : */
1567 boolean_t b_mpeg2, /* you know what ? */
1568 int i_coding_type, /* I, P, B or D */
1569 int i_chroma_format, /* 4:2:0, 4:2:2 or 4:4:4 */
1570 int i_structure, /* T(OP), B(OTTOM) or F(RAME) */
1571 boolean_t b_second_field ) /* second field of a
1574 static f_motion_t pppf_motion[4][2][4] =
1576 { {NULL, NULL, NULL, NULL},
1577 {NULL, NULL, NULL, NULL}
1579 { {NULL, vdec_MotionFieldField420, vdec_MotionField16x8420,
1580 vdec_MotionFieldDMV420},
1581 {NULL, vdec_MotionFrameField420, vdec_MotionFrameFrame420,
1582 vdec_MotionFrameDMV420}
1584 { {NULL, vdec_MotionFieldField422, vdec_MotionField16x8422,
1585 vdec_MotionFieldDMV422},
1586 {NULL, vdec_MotionFrameField422, vdec_MotionFrameFrame422,
1587 vdec_MotionFrameDMV422}
1589 { {NULL, vdec_MotionFieldField444, vdec_MotionField16x8444,
1590 vdec_MotionFieldDMV444},
1591 {NULL, vdec_MotionFrameField444, vdec_MotionFrameFrame444,
1592 vdec_MotionFrameDMV444}
1595 static int pi_x[12] = {0,8,0,8,0,0,0,0,8,8,8,8};
1596 static int pi_y[2][12] = { {0,0,8,8,0,0,8,8,0,0,8,8},
1597 {0,0,1,1,0,0,1,1,0,0,1,1} };
1599 int i_mb, i_b, i_mask;
1600 macroblock_t * p_mb;
1601 yuv_data_t * p_data1;
1602 yuv_data_t * p_data2;
1604 *pi_mb_address += MacroblockAddressIncrement( p_vpar );
1606 if( *pi_mb_address - i_mb_previous - 1 )
1608 /* Skipped macroblock (ISO/IEC 13818-2 7.6.6). */
1610 /* Reset DC predictors (7.2.1). */
1611 p_vpar->mb.pi_dc_dct_pred[0] = p_vpar->mb.pi_dc_dct_pred[1]
1612 = p_vpar->mb.pi_dc_dct_pred[2]
1613 = 1 << (7 + p_vpar->picture.i_intra_dc_precision);
1615 if( i_coding_type == P_CODING_TYPE )
1617 /* Reset motion vector predictors (ISO/IEC 13818-2 7.6.3.4). */
1618 memset( p_vpar->mb.pppi_pmv, 0, 8*sizeof(int) );
1621 for( i_mb = i_mb_previous + 1; i_mb < *pi_mb_address; i_mb++ )
1623 SkippedMacroblock( p_vpar, i_mb, i_mb_base, i_coding_type,
1624 i_chroma_format, i_structure, b_second_field );
1628 /* Get a macroblock structure. */
1629 if( (p_mb = vpar_NewMacroblock( &p_vpar->vfifo )) == NULL )
1635 p_vpar->picture.pp_mb[i_mb_base + *pi_mb_address] = p_mb;
1638 InitMacroblock( p_vpar, p_mb, i_coding_type, i_chroma_format,
1639 i_structure, b_second_field );
1641 /* Parse off macroblock_modes structure. */
1642 MacroblockModes( p_vpar, p_mb, i_chroma_format, i_coding_type,
1645 if( p_mb->i_mb_type & MB_QUANT )
1647 LoadQuantizerScale( p_vpar );
1650 if( (i_coding_type == P_CODING_TYPE || i_coding_type == B_CODING_TYPE)
1651 && (p_mb->i_mb_type & MB_MOTION_FORWARD) )
1654 DecodeMVMPEG2( p_vpar, p_mb, 0, i_structure );
1656 DecodeMVMPEG1( p_vpar, p_mb, 0, i_structure );
1659 if( (i_coding_type == B_CODING_TYPE)
1660 && (p_mb->i_mb_type & MB_MOTION_BACKWARD) )
1663 DecodeMVMPEG2( p_vpar, p_mb, 1, i_structure );
1665 DecodeMVMPEG1( p_vpar, p_mb, 1, i_structure );
1668 if( i_coding_type == P_CODING_TYPE
1669 && !(p_mb->i_mb_type & (MB_MOTION_FORWARD|MB_INTRA)) )
1671 /* Special No-MC macroblock in P pictures (7.6.3.5). */
1672 p_mb->i_mb_type |= MB_MOTION_FORWARD;
1673 memset( p_vpar->mb.pppi_pmv, 0, 8*sizeof(int) );
1674 memset( p_mb->pppi_motion_vectors, 0, 8*sizeof(int) );
1675 p_vpar->mb.i_motion_type = 1 + (i_structure == FRAME_STRUCTURE);
1676 p_mb->ppi_field_select[0][0] = (i_structure == BOTTOM_FIELD);
1679 if( (i_coding_type != I_CODING_TYPE) && !(p_mb->i_mb_type & MB_INTRA) )
1681 /* Reset DC predictors (7.2.1). */
1682 p_vpar->mb.pi_dc_dct_pred[0] = p_vpar->mb.pi_dc_dct_pred[1]
1683 = p_vpar->mb.pi_dc_dct_pred[2]
1684 = 1 << (7 + p_vpar->picture.i_intra_dc_precision);
1686 /* Motion function pointer. */
1687 p_mb->pf_motion = pppf_motion[i_chroma_format]
1688 [i_structure == FRAME_STRUCTURE]
1689 [p_vpar->mb.i_motion_type];
1691 if( p_mb->i_mb_type & MB_PATTERN )
1693 switch( i_chroma_format )
1696 p_mb->i_coded_block_pattern = CodedPattern420( p_vpar );
1699 p_mb->i_coded_block_pattern = CodedPattern422( p_vpar );
1702 p_mb->i_coded_block_pattern = CodedPattern444( p_vpar );
1707 p_mb->i_coded_block_pattern = 0;
1711 * Effectively decode blocks.
1713 PARSEBLOCKS( DecodeMPEG1NonIntra, DecodeMPEG2NonIntra )
1717 if( !p_vpar->picture.b_concealment_mv )
1719 /* Reset MV predictors. */
1720 memset( p_vpar->mb.pppi_pmv, 0, 8*sizeof(int) );
1725 DecodeMVMPEG2( p_vpar, p_mb, 0, i_structure );
1727 DecodeMVMPEG1( p_vpar, p_mb, 0, i_structure );
1728 RemoveBits( &p_vpar->bit_stream, 1 );
1731 if( p_mb->i_mb_type & MB_PATTERN )
1733 switch( i_chroma_format )
1736 p_mb->i_coded_block_pattern = CodedPattern420( p_vpar );
1739 p_mb->i_coded_block_pattern = CodedPattern422( p_vpar );
1742 p_mb->i_coded_block_pattern = CodedPattern444( p_vpar );
1747 p_mb->i_coded_block_pattern =
1748 (1 << (4 + (1 << i_chroma_format))) - 1;
1752 * Effectively decode blocks.
1754 PARSEBLOCKS( DecodeMPEG1Intra, DecodeMPEG2Intra )
1757 if( !p_vpar->picture.b_error )
1759 UpdateContext( p_vpar, i_structure );
1761 /* Decode the macroblock NOW ! */
1762 vdec_DecodeMacroblock( p_vpar->pp_vdec[0], p_mb );
1767 /* Mark this block as skipped (better than green blocks), and go
1768 * to the next slice. */
1770 vpar_DestroyMacroblock( &p_vpar->vfifo, p_mb );
1775 * Picture data parsing management
1778 /*****************************************************************************
1779 * SliceHeader : Parse the next slice structure
1780 *****************************************************************************/
1781 static __inline__ void SliceHeader( vpar_thread_t * p_vpar,
1782 int * pi_mb_address, int i_mb_base,
1783 u32 i_vert_code, boolean_t b_high,
1784 boolean_t b_dp_scalable,
1785 boolean_t b_mpeg2, int i_coding_type,
1786 int i_chroma_format, int i_structure,
1787 boolean_t b_second_field )
1789 int i_mb_address_save = *pi_mb_address;
1791 p_vpar->picture.b_error = 0;
1795 /* Picture with more than 2800 lines. */
1796 i_vert_code += GetBits( &p_vpar->bit_stream, 3 ) << 7;
1800 /* DATA_PARTITIONING scalability. */
1801 RemoveBits( &p_vpar->bit_stream, 7 ); /* priority_breakpoint */
1804 LoadQuantizerScale( p_vpar );
1806 if( GetBits( &p_vpar->bit_stream, 1 ) )
1808 /* intra_slice, slice_id */
1809 RemoveBits( &p_vpar->bit_stream, 8 );
1810 /* extra_information_slice */
1811 while( GetBits( &p_vpar->bit_stream, 1 ) )
1813 RemoveBits( &p_vpar->bit_stream, 8 );
1816 *pi_mb_address = (i_vert_code - 1)*p_vpar->sequence.i_mb_width;
1818 /* Reset DC coefficients predictors (ISO/IEC 13818-2 7.2.1). */
1819 p_vpar->mb.pi_dc_dct_pred[0] = p_vpar->mb.pi_dc_dct_pred[1]
1820 = p_vpar->mb.pi_dc_dct_pred[2]
1821 = 1 << (7 + p_vpar->picture.i_intra_dc_precision);
1823 /* Reset motion vector predictors (ISO/IEC 13818-2 7.6.3.4). */
1824 memset( p_vpar->mb.pppi_pmv, 0, 8*sizeof(int) );
1828 ParseMacroblock( p_vpar, pi_mb_address, i_mb_address_save,
1829 i_mb_base, b_mpeg2, i_coding_type,
1830 i_chroma_format, i_structure,
1832 i_mb_address_save = *pi_mb_address;
1834 while( ShowBits( &p_vpar->bit_stream, 23 ) && !p_vpar->picture.b_error
1835 && !p_vpar->b_die );
1836 NextStartCode( p_vpar );
1839 /*****************************************************************************
1840 * PictureData : Parse off all macroblocks (ISO/IEC 13818-2 6.2.3.7)
1841 *****************************************************************************/
1842 static __inline__ void PictureData( vpar_thread_t * p_vpar, int i_mb_base,
1843 boolean_t b_high, boolean_t b_dp_scalable,
1844 boolean_t b_mpeg2, int i_coding_type,
1845 int i_chroma_format, int i_structure,
1846 boolean_t b_second_field )
1848 int i_mb_address = 0;
1851 NextStartCode( p_vpar );
1852 while( (i_coding_type != I_CODING_TYPE || i_coding_type != D_CODING_TYPE
1853 || !p_vpar->picture.b_error)
1854 && i_mb_address+i_mb_base < p_vpar->sequence.i_mb_size
1857 if( ((i_dummy = ShowBits( &p_vpar->bit_stream, 32 ))
1858 < SLICE_START_CODE_MIN) ||
1859 (i_dummy > SLICE_START_CODE_MAX) )
1861 intf_DbgMsg("vpar debug: premature end of picture\n");
1862 p_vpar->picture.b_error = 1;
1865 RemoveBits32( &p_vpar->bit_stream );
1867 /* Decode slice data. */
1868 SliceHeader( p_vpar, &i_mb_address, i_mb_base, i_dummy & 255,
1869 b_high, b_dp_scalable, b_mpeg2, i_coding_type,
1870 i_chroma_format, i_structure, b_second_field );
1873 /* Try to recover from error. If we missed less than half the
1874 * number of macroblocks of the picture, mark the missed ones
1876 if( (i_coding_type == P_CODING_TYPE || i_coding_type == B_CODING_TYPE)
1877 && p_vpar->picture.b_error &&
1878 ( (i_mb_address-i_mb_base) > (p_vpar->sequence.i_mb_size >> 1)
1879 || (i_structure != FRAME_STRUCTURE
1880 && (i_mb_address-i_mb_base) > (p_vpar->sequence.i_mb_size >> 2) ) ) )
1884 p_vpar->picture.b_error = 0;
1885 for( i_mb = i_mb_address + 1;
1886 i_mb < (p_vpar->sequence.i_mb_size
1887 << (i_structure != FRAME_STRUCTURE));
1890 SkippedMacroblock( p_vpar, i_mb, i_mb_base, i_coding_type,
1891 i_chroma_format, i_structure, b_second_field );
1896 /*****************************************************************************
1897 * vpar_PictureDataVWXYZ : Parse the next macroblock ; specific functions
1898 *****************************************************************************
1901 * X = chroma format ?
1903 * Z = second field ?
1904 *****************************************************************************/
1905 void vpar_PictureDataGENERIC( vpar_thread_t * p_vpar, int i_mb_base )
1907 PictureData( p_vpar, i_mb_base, (p_vpar->sequence.i_height > 2800),
1908 (p_vpar->sequence.i_scalable_mode == SC_DP),
1909 p_vpar->sequence.b_mpeg2, p_vpar->picture.i_coding_type,
1910 p_vpar->sequence.i_chroma_format,
1911 p_vpar->picture.i_structure,
1912 (p_vpar->picture.i_structure !=
1913 p_vpar->picture.i_current_structure) );
1916 #if (VPAR_OPTIM_LEVEL > 0)
1917 /* Optimizations for frame pictures */
1918 void vpar_PictureData2I420F0( vpar_thread_t * p_vpar, int i_mb_base )
1920 PictureData( p_vpar, 0, 0, 0, 1, I_CODING_TYPE, CHROMA_420,
1921 FRAME_STRUCTURE, 0 );
1924 void vpar_PictureData2P420F0( vpar_thread_t * p_vpar, int i_mb_base )
1926 PictureData( p_vpar, 0, 0, 0, 1, P_CODING_TYPE, CHROMA_420,
1927 FRAME_STRUCTURE, 0 );
1930 void vpar_PictureData2B420F0( vpar_thread_t * p_vpar, int i_mb_base )
1932 PictureData( p_vpar, 0, 0, 0, 1, B_CODING_TYPE, CHROMA_420,
1933 FRAME_STRUCTURE, 0 );
1937 #if (VPAR_OPTIM_LEVEL > 1)
1938 /* Optimizations for field pictures */
1939 void vpar_PictureData2I420TZ( vpar_thread_t * p_vpar, int i_mb_base )
1941 PictureData( p_vpar, i_mb_base, 0, 0, 1, I_CODING_TYPE, CHROMA_420,
1942 TOP_FIELD, (p_vpar->picture.i_structure !=
1943 p_vpar->picture.i_current_structure) );
1946 void vpar_PictureData2P420TZ( vpar_thread_t * p_vpar, int i_mb_base )
1948 PictureData( p_vpar, i_mb_base, 0, 0, 1, P_CODING_TYPE, CHROMA_420,
1949 TOP_FIELD, (p_vpar->picture.i_structure !=
1950 p_vpar->picture.i_current_structure) );
1953 void vpar_PictureData2B420TZ( vpar_thread_t * p_vpar, int i_mb_base )
1955 PictureData( p_vpar, i_mb_base, 0, 0, 1, B_CODING_TYPE, CHROMA_420,
1956 TOP_FIELD, (p_vpar->picture.i_structure !=
1957 p_vpar->picture.i_current_structure) );
1960 void vpar_PictureData2I420BZ( vpar_thread_t * p_vpar, int i_mb_base )
1962 PictureData( p_vpar, i_mb_base, 0, 0, 1, I_CODING_TYPE, CHROMA_420,
1963 BOTTOM_FIELD, (p_vpar->picture.i_structure !=
1964 p_vpar->picture.i_current_structure) );
1967 void vpar_PictureData2P420BZ( vpar_thread_t * p_vpar, int i_mb_base )
1969 PictureData( p_vpar, i_mb_base, 0, 0, 1, P_CODING_TYPE, CHROMA_420,
1970 BOTTOM_FIELD, (p_vpar->picture.i_structure !=
1971 p_vpar->picture.i_current_structure) );
1974 void vpar_PictureData2B420BZ( vpar_thread_t * p_vpar, int i_mb_base )
1976 PictureData( p_vpar, i_mb_base, 0, 0, 1, B_CODING_TYPE, CHROMA_420,
1977 BOTTOM_FIELD, (p_vpar->picture.i_structure !=
1978 p_vpar->picture.i_current_structure) );