1 /**************************************************************************
3 * Copyright 2011 Maarten Lankhorst
4 * Copyright 2011 Christian König
7 * Permission is hereby granted, free of charge, to any person obtaining a
8 * copy of this software and associated documentation files (the
9 * "Software"), to deal in the Software without restriction, including
10 * without limitation the rights to use, copy, modify, merge, publish,
11 * distribute, sub license, and/or sell copies of the Software, and to
12 * permit persons to whom the Software is furnished to do so, subject to
13 * the following conditions:
15 * The above copyright notice and this permission notice (including the
16 * next paragraph) shall be included in all copies or substantial portions
19 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS
20 * OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
21 * MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NON-INFRINGEMENT.
22 * IN NO EVENT SHALL TUNGSTEN GRAPHICS AND/OR ITS SUPPLIERS BE LIABLE FOR
23 * ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT,
24 * TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE
25 * SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
27 **************************************************************************/
29 #include "pipe/p_video_decoder.h"
30 #include "util/u_memory.h"
33 #include "vl_mpeg12_bitstream.h"
36 dct_End_of_Block
= 0xFF,
49 struct dct_coeff_compressed
52 struct dct_coeff coeff
;
55 /* coding table as found in the spec annex B.5 table B-1 */
56 static const struct vl_vlc_compressed macroblock_address_increment
[] = {
66 { 0x0b00, { 8, 10 } },
67 { 0x0a00, { 8, 11 } },
68 { 0x0900, { 8, 12 } },
69 { 0x0800, { 8, 13 } },
70 { 0x0700, { 8, 14 } },
71 { 0x0600, { 8, 15 } },
72 { 0x05c0, { 10, 16 } },
73 { 0x0580, { 10, 17 } },
74 { 0x0540, { 10, 18 } },
75 { 0x0500, { 10, 19 } },
76 { 0x04c0, { 10, 20 } },
77 { 0x0480, { 10, 21 } },
78 { 0x0460, { 11, 22 } },
79 { 0x0440, { 11, 23 } },
80 { 0x0420, { 11, 24 } },
81 { 0x0400, { 11, 25 } },
82 { 0x03e0, { 11, 26 } },
83 { 0x03c0, { 11, 27 } },
84 { 0x03a0, { 11, 28 } },
85 { 0x0380, { 11, 29 } },
86 { 0x0360, { 11, 30 } },
87 { 0x0340, { 11, 31 } },
88 { 0x0320, { 11, 32 } },
89 { 0x0300, { 11, 33 } }
92 #define Q PIPE_MPEG12_MB_TYPE_QUANT
93 #define F PIPE_MPEG12_MB_TYPE_MOTION_FORWARD
94 #define B PIPE_MPEG12_MB_TYPE_MOTION_BACKWARD
95 #define P PIPE_MPEG12_MB_TYPE_PATTERN
96 #define I PIPE_MPEG12_MB_TYPE_INTRA
98 /* coding table as found in the spec annex B.5 table B-2 */
99 static const struct vl_vlc_compressed macroblock_type_i
[] = {
100 { 0x8000, { 1, I
} },
101 { 0x4000, { 2, Q
|I
} }
104 /* coding table as found in the spec annex B.5 table B-3 */
105 static const struct vl_vlc_compressed macroblock_type_p
[] = {
106 { 0x8000, { 1, F
|P
} },
107 { 0x4000, { 2, P
} },
108 { 0x2000, { 3, F
} },
109 { 0x1800, { 5, I
} },
110 { 0x1000, { 5, Q
|F
|P
} },
111 { 0x0800, { 5, Q
|P
} },
112 { 0x0400, { 6, Q
|I
} }
115 /* coding table as found in the spec annex B.5 table B-4 */
116 static const struct vl_vlc_compressed macroblock_type_b
[] = {
117 { 0x8000, { 2, F
|B
} },
118 { 0xC000, { 2, F
|B
|P
} },
119 { 0x4000, { 3, B
} },
120 { 0x6000, { 3, B
|P
} },
121 { 0x2000, { 4, F
} },
122 { 0x3000, { 4, F
|P
} },
123 { 0x1800, { 5, I
} },
124 { 0x1000, { 5, Q
|F
|B
|P
} },
125 { 0x0C00, { 6, Q
|F
|P
} },
126 { 0x0800, { 6, Q
|B
|P
} },
127 { 0x0400, { 6, Q
|I
} }
136 /* coding table as found in the spec annex B.5 table B-9 */
137 static const struct vl_vlc_compressed coded_block_pattern
[] = {
138 { 0xE000, { 3, 60 } },
139 { 0xD000, { 4, 4 } },
140 { 0xC000, { 4, 8 } },
141 { 0xB000, { 4, 16 } },
142 { 0xA000, { 4, 32 } },
143 { 0x9800, { 5, 12 } },
144 { 0x9000, { 5, 48 } },
145 { 0x8800, { 5, 20 } },
146 { 0x8000, { 5, 40 } },
147 { 0x7800, { 5, 28 } },
148 { 0x7000, { 5, 44 } },
149 { 0x6800, { 5, 52 } },
150 { 0x6000, { 5, 56 } },
151 { 0x5800, { 5, 1 } },
152 { 0x5000, { 5, 61 } },
153 { 0x4800, { 5, 2 } },
154 { 0x4000, { 5, 62 } },
155 { 0x3C00, { 6, 24 } },
156 { 0x3800, { 6, 36 } },
157 { 0x3400, { 6, 3 } },
158 { 0x3000, { 6, 63 } },
159 { 0x2E00, { 7, 5 } },
160 { 0x2C00, { 7, 9 } },
161 { 0x2A00, { 7, 17 } },
162 { 0x2800, { 7, 33 } },
163 { 0x2600, { 7, 6 } },
164 { 0x2400, { 7, 10 } },
165 { 0x2200, { 7, 18 } },
166 { 0x2000, { 7, 34 } },
167 { 0x1F00, { 8, 7 } },
168 { 0x1E00, { 8, 11 } },
169 { 0x1D00, { 8, 19 } },
170 { 0x1C00, { 8, 35 } },
171 { 0x1B00, { 8, 13 } },
172 { 0x1A00, { 8, 49 } },
173 { 0x1900, { 8, 21 } },
174 { 0x1800, { 8, 41 } },
175 { 0x1700, { 8, 14 } },
176 { 0x1600, { 8, 50 } },
177 { 0x1500, { 8, 22 } },
178 { 0x1400, { 8, 42 } },
179 { 0x1300, { 8, 15 } },
180 { 0x1200, { 8, 51 } },
181 { 0x1100, { 8, 23 } },
182 { 0x1000, { 8, 43 } },
183 { 0x0F00, { 8, 25 } },
184 { 0x0E00, { 8, 37 } },
185 { 0x0D00, { 8, 26 } },
186 { 0x0C00, { 8, 38 } },
187 { 0x0B00, { 8, 29 } },
188 { 0x0A00, { 8, 45 } },
189 { 0x0900, { 8, 53 } },
190 { 0x0800, { 8, 57 } },
191 { 0x0700, { 8, 30 } },
192 { 0x0600, { 8, 46 } },
193 { 0x0500, { 8, 54 } },
194 { 0x0400, { 8, 58 } },
195 { 0x0380, { 9, 31 } },
196 { 0x0300, { 9, 47 } },
197 { 0x0280, { 9, 55 } },
198 { 0x0200, { 9, 59 } },
199 { 0x0180, { 9, 27 } },
200 { 0x0100, { 9, 39 } },
204 /* coding table as found in the spec annex B.5 table B-10 */
205 static const struct vl_vlc_compressed motion_code
[] = {
206 { 0x0320, { 11, -16 } },
207 { 0x0360, { 11, -15 } },
208 { 0x03a0, { 11, -14 } },
209 { 0x03e0, { 11, -13 } },
210 { 0x0420, { 11, -12 } },
211 { 0x0460, { 11, -11 } },
212 { 0x04c0, { 10, -10 } },
213 { 0x0540, { 10, -9 } },
214 { 0x05c0, { 10, -8 } },
215 { 0x0700, { 8, -7 } },
216 { 0x0900, { 8, -6 } },
217 { 0x0b00, { 8, -5 } },
218 { 0x0e00, { 7, -4 } },
219 { 0x1800, { 5, -3 } },
220 { 0x3000, { 4, -2 } },
221 { 0x6000, { 3, -1 } },
222 { 0x8000, { 1, 0 } },
223 { 0x4000, { 3, 1 } },
224 { 0x2000, { 4, 2 } },
225 { 0x1000, { 5, 3 } },
226 { 0x0c00, { 7, 4 } },
227 { 0x0a00, { 8, 5 } },
228 { 0x0800, { 8, 6 } },
229 { 0x0600, { 8, 7 } },
230 { 0x0580, { 10, 8 } },
231 { 0x0500, { 10, 9 } },
232 { 0x0480, { 10, 10 } },
233 { 0x0440, { 11, 11 } },
234 { 0x0400, { 11, 12 } },
235 { 0x03c0, { 11, 13 } },
236 { 0x0380, { 11, 14 } },
237 { 0x0340, { 11, 15 } },
238 { 0x0300, { 11, 16 } }
241 /* coding table as found in the spec annex B.5 table B-11 */
242 static const struct vl_vlc_compressed dmvector
[] = {
243 { 0x0000, { 1, 0 } },
244 { 0x8000, { 2, 1 } },
245 { 0xc000, { 2, -1 } }
248 /* coding table as found in the spec annex B.5 table B-12 */
249 static const struct vl_vlc_compressed dct_dc_size_luminance
[] = {
250 { 0x8000, { 3, 0 } },
251 { 0x0000, { 2, 1 } },
252 { 0x4000, { 2, 2 } },
253 { 0xA000, { 3, 3 } },
254 { 0xC000, { 3, 4 } },
255 { 0xE000, { 4, 5 } },
256 { 0xF000, { 5, 6 } },
257 { 0xF800, { 6, 7 } },
258 { 0xFC00, { 7, 8 } },
259 { 0xFE00, { 8, 9 } },
260 { 0xFF00, { 9, 10 } },
261 { 0xFF80, { 9, 11 } }
264 /* coding table as found in the spec annex B.5 table B-13 */
265 static const struct vl_vlc_compressed dct_dc_size_chrominance
[] = {
266 { 0x0000, { 2, 0 } },
267 { 0x4000, { 2, 1 } },
268 { 0x8000, { 2, 2 } },
269 { 0xC000, { 3, 3 } },
270 { 0xE000, { 4, 4 } },
271 { 0xF000, { 5, 5 } },
272 { 0xF800, { 6, 6 } },
273 { 0xFC00, { 7, 7 } },
274 { 0xFE00, { 8, 8 } },
275 { 0xFF00, { 9, 9 } },
276 { 0xFF80, { 10, 10 } },
277 { 0xFFC0, { 10, 11 } }
280 /* coding table as found in the spec annex B.5 table B-14 */
281 static const struct dct_coeff_compressed dct_coeff_tbl_zero
[] = {
282 { 0x8000, { 2, dct_End_of_Block
, 0 } },
283 { 0x8000, { 1, dct_DC
, 1 } },
284 { 0xC000, { 2, dct_AC
, 1 } },
285 { 0x6000, { 3, 1, 1 } },
286 { 0x4000, { 4, 0, 2 } },
287 { 0x5000, { 4, 2, 1 } },
288 { 0x2800, { 5, 0, 3 } },
289 { 0x3800, { 5, 3, 1 } },
290 { 0x3000, { 5, 4, 1 } },
291 { 0x1800, { 6, 1, 2 } },
292 { 0x1C00, { 6, 5, 1 } },
293 { 0x1400, { 6, 6, 1 } },
294 { 0x1000, { 6, 7, 1 } },
295 { 0x0C00, { 7, 0, 4 } },
296 { 0x0800, { 7, 2, 2 } },
297 { 0x0E00, { 7, 8, 1 } },
298 { 0x0A00, { 7, 9, 1 } },
299 { 0x0400, { 6, dct_Escape
, 0 } },
300 { 0x2600, { 8, 0, 5 } },
301 { 0x2100, { 8, 0, 6 } },
302 { 0x2500, { 8, 1, 3 } },
303 { 0x2400, { 8, 3, 2 } },
304 { 0x2700, { 8, 10, 1 } },
305 { 0x2300, { 8, 11, 1 } },
306 { 0x2200, { 8, 12, 1 } },
307 { 0x2000, { 8, 13, 1 } },
308 { 0x0280, { 10, 0, 7 } },
309 { 0x0300, { 10, 1, 4 } },
310 { 0x02C0, { 10, 2, 3 } },
311 { 0x03C0, { 10, 4, 2 } },
312 { 0x0240, { 10, 5, 2 } },
313 { 0x0380, { 10, 14, 1 } },
314 { 0x0340, { 10, 15, 1 } },
315 { 0x0200, { 10, 16, 1 } },
316 { 0x01D0, { 12, 0, 8 } },
317 { 0x0180, { 12, 0, 9 } },
318 { 0x0130, { 12, 0, 10 } },
319 { 0x0100, { 12, 0, 11 } },
320 { 0x01B0, { 12, 1, 5 } },
321 { 0x0140, { 12, 2, 4 } },
322 { 0x01C0, { 12, 3, 3 } },
323 { 0x0120, { 12, 4, 3 } },
324 { 0x01E0, { 12, 6, 2 } },
325 { 0x0150, { 12, 7, 2 } },
326 { 0x0110, { 12, 8, 2 } },
327 { 0x01F0, { 12, 17, 1 } },
328 { 0x01A0, { 12, 18, 1 } },
329 { 0x0190, { 12, 19, 1 } },
330 { 0x0170, { 12, 20, 1 } },
331 { 0x0160, { 12, 21, 1 } },
332 { 0x00D0, { 13, 0, 12 } },
333 { 0x00C8, { 13, 0, 13 } },
334 { 0x00C0, { 13, 0, 14 } },
335 { 0x00B8, { 13, 0, 15 } },
336 { 0x00B0, { 13, 1, 6 } },
337 { 0x00A8, { 13, 1, 7 } },
338 { 0x00A0, { 13, 2, 5 } },
339 { 0x0098, { 13, 3, 4 } },
340 { 0x0090, { 13, 5, 3 } },
341 { 0x0088, { 13, 9, 2 } },
342 { 0x0080, { 13, 10, 2 } },
343 { 0x00F8, { 13, 22, 1 } },
344 { 0x00F0, { 13, 23, 1 } },
345 { 0x00E8, { 13, 24, 1 } },
346 { 0x00E0, { 13, 25, 1 } },
347 { 0x00D8, { 13, 26, 1 } },
348 { 0x007C, { 14, 0, 16 } },
349 { 0x0078, { 14, 0, 17 } },
350 { 0x0074, { 14, 0, 18 } },
351 { 0x0070, { 14, 0, 19 } },
352 { 0x006C, { 14, 0, 20 } },
353 { 0x0068, { 14, 0, 21 } },
354 { 0x0064, { 14, 0, 22 } },
355 { 0x0060, { 14, 0, 23 } },
356 { 0x005C, { 14, 0, 24 } },
357 { 0x0058, { 14, 0, 25 } },
358 { 0x0054, { 14, 0, 26 } },
359 { 0x0050, { 14, 0, 27 } },
360 { 0x004C, { 14, 0, 28 } },
361 { 0x0048, { 14, 0, 29 } },
362 { 0x0044, { 14, 0, 30 } },
363 { 0x0040, { 14, 0, 31 } },
364 { 0x0030, { 15, 0, 32 } },
365 { 0x002E, { 15, 0, 33 } },
366 { 0x002C, { 15, 0, 34 } },
367 { 0x002A, { 15, 0, 35 } },
368 { 0x0028, { 15, 0, 36 } },
369 { 0x0026, { 15, 0, 37 } },
370 { 0x0024, { 15, 0, 38 } },
371 { 0x0022, { 15, 0, 39 } },
372 { 0x0020, { 15, 0, 40 } },
373 { 0x003E, { 15, 1, 8 } },
374 { 0x003C, { 15, 1, 9 } },
375 { 0x003A, { 15, 1, 10 } },
376 { 0x0038, { 15, 1, 11 } },
377 { 0x0036, { 15, 1, 12 } },
378 { 0x0034, { 15, 1, 13 } },
379 { 0x0032, { 15, 1, 14 } },
380 { 0x0013, { 16, 1, 15 } },
381 { 0x0012, { 16, 1, 16 } },
382 { 0x0011, { 16, 1, 17 } },
383 { 0x0010, { 16, 1, 18 } },
384 { 0x0014, { 16, 6, 3 } },
385 { 0x001A, { 16, 11, 2 } },
386 { 0x0019, { 16, 12, 2 } },
387 { 0x0018, { 16, 13, 2 } },
388 { 0x0017, { 16, 14, 2 } },
389 { 0x0016, { 16, 15, 2 } },
390 { 0x0015, { 16, 16, 2 } },
391 { 0x001F, { 16, 27, 1 } },
392 { 0x001E, { 16, 28, 1 } },
393 { 0x001D, { 16, 29, 1 } },
394 { 0x001C, { 16, 30, 1 } },
395 { 0x001B, { 16, 31, 1 } }
398 /* coding table as found in the spec annex B.5 table B-15 */
399 static const struct dct_coeff_compressed dct_coeff_tbl_one
[] = {
400 { 0x6000, { 4, dct_End_of_Block
, 0 } },
401 { 0x8000, { 2, 0, 1 } },
402 { 0x4000, { 3, 1, 1 } },
403 { 0xC000, { 3, 0, 2 } },
404 { 0x2800, { 5, 2, 1 } },
405 { 0x7000, { 4, 0, 3 } },
406 { 0x3800, { 5, 3, 1 } },
407 { 0x1800, { 6, 4, 1 } },
408 { 0x3000, { 5, 1, 2 } },
409 { 0x1C00, { 6, 5, 1 } },
410 { 0x0C00, { 7, 6, 1 } },
411 { 0x0800, { 7, 7, 1 } },
412 { 0xE000, { 5, 0, 4 } },
413 { 0x0E00, { 7, 2, 2 } },
414 { 0x0A00, { 7, 8, 1 } },
415 { 0xF000, { 7, 9, 1 } },
416 { 0x0400, { 6, dct_Escape
, 0 } },
417 { 0xE800, { 5, 0, 5 } },
418 { 0x1400, { 6, 0, 6 } },
419 { 0xF200, { 7, 1, 3 } },
420 { 0x2600, { 8, 3, 2 } },
421 { 0xF400, { 7, 10, 1 } },
422 { 0x2100, { 8, 11, 1 } },
423 { 0x2500, { 8, 12, 1 } },
424 { 0x2400, { 8, 13, 1 } },
425 { 0x1000, { 6, 0, 7 } },
426 { 0x2700, { 8, 1, 4 } },
427 { 0xFC00, { 8, 2, 3 } },
428 { 0xFD00, { 8, 4, 2 } },
429 { 0x0200, { 9, 5, 2 } },
430 { 0x0280, { 9, 14, 1 } },
431 { 0x0380, { 9, 15, 1 } },
432 { 0x0340, { 10, 16, 1 } },
433 { 0xF600, { 7, 0, 8 } },
434 { 0xF800, { 7, 0, 9 } },
435 { 0x2300, { 8, 0, 10 } },
436 { 0x2200, { 8, 0, 11 } },
437 { 0x2000, { 8, 1, 5 } },
438 { 0x0300, { 10, 2, 4 } },
439 { 0x01C0, { 12, 3, 3 } },
440 { 0x0120, { 12, 4, 3 } },
441 { 0x01E0, { 12, 6, 2 } },
442 { 0x0150, { 12, 7, 2 } },
443 { 0x0110, { 12, 8, 2 } },
444 { 0x01F0, { 12, 17, 1 } },
445 { 0x01A0, { 12, 18, 1 } },
446 { 0x0190, { 12, 19, 1 } },
447 { 0x0170, { 12, 20, 1 } },
448 { 0x0160, { 12, 21, 1 } },
449 { 0xFA00, { 8, 0, 12 } },
450 { 0xFB00, { 8, 0, 13 } },
451 { 0xFE00, { 8, 0, 14 } },
452 { 0xFF00, { 8, 0, 15 } },
453 { 0x00B0, { 13, 1, 6 } },
454 { 0x00A8, { 13, 1, 7 } },
455 { 0x00A0, { 13, 2, 5 } },
456 { 0x0098, { 13, 3, 4 } },
457 { 0x0090, { 13, 5, 3 } },
458 { 0x0088, { 13, 9, 2 } },
459 { 0x0080, { 13, 10, 2 } },
460 { 0x00F8, { 13, 22, 1 } },
461 { 0x00F0, { 13, 23, 1 } },
462 { 0x00E8, { 13, 24, 1 } },
463 { 0x00E0, { 13, 25, 1 } },
464 { 0x00D8, { 13, 26, 1 } },
465 { 0x007C, { 14, 0, 16 } },
466 { 0x0078, { 14, 0, 17 } },
467 { 0x0074, { 14, 0, 18 } },
468 { 0x0070, { 14, 0, 19 } },
469 { 0x006C, { 14, 0, 20 } },
470 { 0x0068, { 14, 0, 21 } },
471 { 0x0064, { 14, 0, 22 } },
472 { 0x0060, { 14, 0, 23 } },
473 { 0x005C, { 14, 0, 24 } },
474 { 0x0058, { 14, 0, 25 } },
475 { 0x0054, { 14, 0, 26 } },
476 { 0x0050, { 14, 0, 27 } },
477 { 0x004C, { 14, 0, 28 } },
478 { 0x0048, { 14, 0, 29 } },
479 { 0x0044, { 14, 0, 30 } },
480 { 0x0040, { 14, 0, 31 } },
481 { 0x0030, { 15, 0, 32 } },
482 { 0x002E, { 15, 0, 33 } },
483 { 0x002C, { 15, 0, 34 } },
484 { 0x002A, { 15, 0, 35 } },
485 { 0x0028, { 15, 0, 36 } },
486 { 0x0026, { 15, 0, 37 } },
487 { 0x0024, { 15, 0, 38 } },
488 { 0x0022, { 15, 0, 39 } },
489 { 0x0020, { 15, 0, 40 } },
490 { 0x003E, { 15, 1, 8 } },
491 { 0x003C, { 15, 1, 9 } },
492 { 0x003A, { 15, 1, 10 } },
493 { 0x0038, { 15, 1, 11 } },
494 { 0x0036, { 15, 1, 12 } },
495 { 0x0034, { 15, 1, 13 } },
496 { 0x0032, { 15, 1, 14 } },
497 { 0x0013, { 16, 1, 15 } },
498 { 0x0012, { 16, 1, 16 } },
499 { 0x0011, { 16, 1, 17 } },
500 { 0x0010, { 16, 1, 18 } },
501 { 0x0014, { 16, 6, 3 } },
502 { 0x001A, { 16, 11, 2 } },
503 { 0x0019, { 16, 12, 2 } },
504 { 0x0018, { 16, 13, 2 } },
505 { 0x0017, { 16, 14, 2 } },
506 { 0x0016, { 16, 15, 2 } },
507 { 0x0015, { 16, 16, 2 } },
508 { 0x001F, { 16, 27, 1 } },
509 { 0x001E, { 16, 28, 1 } },
510 { 0x001D, { 16, 29, 1 } },
511 { 0x001C, { 16, 30, 1 } },
512 { 0x001B, { 16, 31, 1 } }
516 static const unsigned quant_scale
[2][32] = {
517 { 0, 2, 4, 6, 8, 10, 12, 14, 16, 18, 20, 22, 24, 26, 28, 30,
518 32, 34, 36, 38, 40, 42, 44, 46, 48, 50, 52, 54, 56, 58, 60, 62 },
519 { 0, 1, 2, 3, 4, 5, 6, 7, 8, 10, 12, 14, 16, 18, 20, 22, 24,
520 28, 32, 36, 40, 44, 48, 52, 56, 64, 72, 80, 88, 96, 104, 112 }
523 static struct vl_vlc_entry tbl_B1
[1 << 11];
524 static struct vl_vlc_entry tbl_B2
[1 << 2];
525 static struct vl_vlc_entry tbl_B3
[1 << 6];
526 static struct vl_vlc_entry tbl_B4
[1 << 6];
527 static struct vl_vlc_entry tbl_B9
[1 << 9];
528 static struct vl_vlc_entry tbl_B10
[1 << 11];
529 static struct vl_vlc_entry tbl_B11
[1 << 2];
530 static struct vl_vlc_entry tbl_B12
[1 << 10];
531 static struct vl_vlc_entry tbl_B13
[1 << 10];
532 static struct dct_coeff tbl_B14_DC
[1 << 17];
533 static struct dct_coeff tbl_B14_AC
[1 << 17];
534 static struct dct_coeff tbl_B15
[1 << 17];
537 init_dct_coeff_table(struct dct_coeff
*dst
, const struct dct_coeff_compressed
*src
,
538 unsigned size
, bool is_DC
)
542 for (i
=0;i
<(1<<17);++i
) {
545 dst
[i
].run
= dct_End_of_Block
;
548 for(; size
> 0; --size
, ++src
) {
549 struct dct_coeff coeff
= src
->coeff
;
550 bool has_sign
= true;
553 case dct_End_of_Block
:
586 for(i
=0; i
<(1 << (17 - coeff
.length
)); ++i
)
587 dst
[src
->bitcode
<< 1 | i
] = coeff
;
590 coeff
.level
= -coeff
.level
;
591 for(; i
<(1 << (18 - coeff
.length
)); ++i
)
592 dst
[src
->bitcode
<< 1 | i
] = coeff
;
600 vl_vlc_init_table(tbl_B1
, Elements(tbl_B1
), macroblock_address_increment
, Elements(macroblock_address_increment
));
601 vl_vlc_init_table(tbl_B2
, Elements(tbl_B2
), macroblock_type_i
, Elements(macroblock_type_i
));
602 vl_vlc_init_table(tbl_B3
, Elements(tbl_B3
), macroblock_type_p
, Elements(macroblock_type_p
));
603 vl_vlc_init_table(tbl_B4
, Elements(tbl_B4
), macroblock_type_b
, Elements(macroblock_type_b
));
604 vl_vlc_init_table(tbl_B9
, Elements(tbl_B9
), coded_block_pattern
, Elements(coded_block_pattern
));
605 vl_vlc_init_table(tbl_B10
, Elements(tbl_B10
), motion_code
, Elements(motion_code
));
606 vl_vlc_init_table(tbl_B11
, Elements(tbl_B11
), dmvector
, Elements(dmvector
));
607 vl_vlc_init_table(tbl_B12
, Elements(tbl_B12
), dct_dc_size_luminance
, Elements(dct_dc_size_luminance
));
608 vl_vlc_init_table(tbl_B13
, Elements(tbl_B13
), dct_dc_size_chrominance
, Elements(dct_dc_size_chrominance
));
609 init_dct_coeff_table(tbl_B14_DC
, dct_coeff_tbl_zero
, Elements(dct_coeff_tbl_zero
), true);
610 init_dct_coeff_table(tbl_B14_AC
, dct_coeff_tbl_zero
, Elements(dct_coeff_tbl_zero
), false);
611 init_dct_coeff_table(tbl_B15
, dct_coeff_tbl_one
, Elements(dct_coeff_tbl_one
), false);
627 motion_vector(struct vl_mpg12_bs
*bs
, int r
, int s
, int dmv
, short delta
[2], short dmvector
[2])
630 for (t
= 0; t
< 2; ++t
) {
632 int r_size
= bs
->desc
->f_code
[s
][t
];
634 vl_vlc_fillbits(&bs
->vlc
);
635 motion_code
= vl_vlc_get_vlclbf(&bs
->vlc
, tbl_B10
, 11);
638 if (r_size
&& motion_code
) {
639 int residual
= vl_vlc_get_uimsbf(&bs
->vlc
, r_size
) + 1;
640 delta
[t
] = ((abs(motion_code
) - 1) << r_size
) + residual
;
642 delta
[t
] = -delta
[t
];
644 delta
[t
] = motion_code
;
646 dmvector
[t
] = vl_vlc_get_vlclbf(&bs
->vlc
, tbl_B11
, 2);
651 wrap(short f
, int shift
)
653 if (f
< (-16 << shift
))
654 return f
+ (32 << shift
);
655 else if (f
>= 16 << shift
)
656 return f
- (32 << shift
);
662 motion_vector_frame(struct vl_mpg12_bs
*bs
, int s
, struct pipe_mpeg12_macroblock
*mb
)
664 int dmv
= mb
->macroblock_modes
.bits
.frame_motion_type
== PIPE_MPEG12_MO_TYPE_DUAL_PRIME
;
665 short dmvector
[2], delta
[2];
667 if (mb
->macroblock_modes
.bits
.frame_motion_type
== PIPE_MPEG12_MO_TYPE_FIELD
) {
668 mb
->motion_vertical_field_select
|= vl_vlc_get_uimsbf(&bs
->vlc
, 1) << s
;
669 motion_vector(bs
, 0, s
, dmv
, delta
, dmvector
);
670 mb
->PMV
[0][s
][0] = wrap(mb
->PMV
[0][s
][0] + delta
[0], bs
->desc
->f_code
[s
][0]);
671 mb
->PMV
[0][s
][1] = wrap(DIV2DOWN(mb
->PMV
[0][s
][1]) + delta
[1], bs
->desc
->f_code
[s
][1]) * 2;
673 mb
->motion_vertical_field_select
|= vl_vlc_get_uimsbf(&bs
->vlc
, 1) << (s
+ 2);
674 motion_vector(bs
, 1, s
, dmv
, delta
, dmvector
);
675 mb
->PMV
[1][s
][0] = wrap(mb
->PMV
[1][s
][0] + delta
[0], bs
->desc
->f_code
[s
][0]);
676 mb
->PMV
[1][s
][1] = wrap(DIV2DOWN(mb
->PMV
[1][s
][1]) + delta
[1], bs
->desc
->f_code
[s
][1]) * 2;
679 motion_vector(bs
, 0, s
, dmv
, delta
, dmvector
);
680 mb
->PMV
[0][s
][0] = wrap(mb
->PMV
[0][s
][0] + delta
[0], bs
->desc
->f_code
[s
][0]);
681 mb
->PMV
[0][s
][1] = wrap(mb
->PMV
[0][s
][1] + delta
[1], bs
->desc
->f_code
[s
][1]);
686 motion_vector_field(struct vl_mpg12_bs
*bs
, int s
, struct pipe_mpeg12_macroblock
*mb
)
688 int dmv
= mb
->macroblock_modes
.bits
.field_motion_type
== PIPE_MPEG12_MO_TYPE_DUAL_PRIME
;
689 short dmvector
[2], delta
[2];
691 if (mb
->macroblock_modes
.bits
.field_motion_type
== PIPE_MPEG12_MO_TYPE_16x8
) {
692 mb
->motion_vertical_field_select
|= vl_vlc_get_uimsbf(&bs
->vlc
, 1) << s
;
693 motion_vector(bs
, 0, s
, dmv
, delta
, dmvector
);
695 mb
->motion_vertical_field_select
|= vl_vlc_get_uimsbf(&bs
->vlc
, 1) << (s
+ 2);
696 motion_vector(bs
, 1, s
, dmv
, delta
, dmvector
);
699 mb
->motion_vertical_field_select
|= vl_vlc_get_uimsbf(&bs
->vlc
, 1) << s
;
700 motion_vector(bs
, 0, s
, dmv
, delta
, dmvector
);
705 reset_predictor(struct vl_mpg12_bs
*bs
) {
706 bs
->pred_dc
[0] = bs
->pred_dc
[1] = bs
->pred_dc
[2] = 0;
710 decode_dct(struct vl_mpg12_bs
*bs
, struct pipe_mpeg12_macroblock
*mb
, int scale
)
712 static const unsigned blk2cc
[] = { 0, 0, 0, 0, 1, 2 };
713 static const struct vl_vlc_entry
*blk2dcsize
[] = {
714 tbl_B12
, tbl_B12
, tbl_B12
, tbl_B12
, tbl_B13
, tbl_B13
717 bool intra
= mb
->macroblock_type
& PIPE_MPEG12_MB_TYPE_INTRA
;
718 const struct dct_coeff
*table
= intra
? bs
->intra_dct_tbl
: tbl_B14_AC
;
719 const struct dct_coeff
*entry
;
721 short *dst
= mb
->blocks
;
723 vl_vlc_fillbits(&bs
->vlc
);
724 mb
->coded_block_pattern
= cbp
= intra
? 0x3F : vl_vlc_get_vlclbf(&bs
->vlc
, tbl_B9
, 9);
729 vl_vlc_eatbits(&bs
->vlc
, entry
->length
);
730 if (entry
->run
== dct_End_of_Block
) {
741 while(!(cbp
& 0x20)) {
746 vl_vlc_fillbits(&bs
->vlc
);
749 unsigned cc
= blk2cc
[blk
];
750 unsigned size
= vl_vlc_get_vlclbf(&bs
->vlc
, blk2dcsize
[blk
], 10);
753 int dct_diff
= vl_vlc_get_uimsbf(&bs
->vlc
, size
);
754 int half_range
= 1 << (size
- 1);
755 if (dct_diff
< half_range
)
756 dct_diff
= (dct_diff
+ 1) - (2 * half_range
);
757 bs
->pred_dc
[cc
] += dct_diff
;
760 dst
[0] = bs
->pred_dc
[cc
];
764 entry
= tbl_B14_DC
+ vl_vlc_peekbits(&bs
->vlc
, 17);
769 } else if (entry
->run
== dct_Escape
) {
770 i
+= vl_vlc_get_uimsbf(&bs
->vlc
, 6) + 1;
774 dst
[i
] = vl_vlc_get_simsbf(&bs
->vlc
, 12) * scale
;
781 dst
[i
] = entry
->level
* scale
;
784 vl_vlc_fillbits(&bs
->vlc
);
785 entry
= table
+ vl_vlc_peekbits(&bs
->vlc
, 17);
790 decode_slice(struct vl_mpg12_bs
*bs
, struct pipe_video_buffer
*target
)
792 struct pipe_mpeg12_macroblock mb
;
793 short dct_blocks
[64*6];
797 memset(&mb
, 0, sizeof(mb
));
798 mb
.base
.codec
= PIPE_VIDEO_CODEC_MPEG12
;
799 mb
.y
= vl_vlc_get_uimsbf(&bs
->vlc
, 8) - 1;
800 mb
.blocks
= dct_blocks
;
803 vl_vlc_fillbits(&bs
->vlc
);
804 dct_scale
= quant_scale
[bs
->desc
->q_scale_type
][vl_vlc_get_uimsbf(&bs
->vlc
, 5)];
806 if (vl_vlc_get_uimsbf(&bs
->vlc
, 1))
807 while (vl_vlc_get_uimsbf(&bs
->vlc
, 9) & 1)
808 vl_vlc_fillbits(&bs
->vlc
);
810 vl_vlc_fillbits(&bs
->vlc
);
811 assert(vl_vlc_bits_left(&bs
->vlc
) > 23 && vl_vlc_peekbits(&bs
->vlc
, 23));
815 if (bs
->decoder
->profile
== PIPE_VIDEO_PROFILE_MPEG1
)
816 while (vl_vlc_peekbits(&bs
->vlc
, 11) == 15) {
817 vl_vlc_eatbits(&bs
->vlc
, 11);
818 vl_vlc_fillbits(&bs
->vlc
);
821 while (vl_vlc_peekbits(&bs
->vlc
, 11) == 8) {
822 vl_vlc_eatbits(&bs
->vlc
, 11);
823 vl_vlc_fillbits(&bs
->vlc
);
826 inc
+= vl_vlc_get_vlclbf(&bs
->vlc
, tbl_B1
, 11);
830 mb
.num_skipped_macroblocks
= inc
- 1;
831 bs
->decoder
->decode_macroblock(bs
->decoder
, target
, &bs
->desc
->base
, &mb
.base
, 1);
835 switch (bs
->desc
->picture_coding_type
) {
836 case PIPE_MPEG12_PICTURE_CODING_TYPE_I
:
837 mb
.macroblock_type
= vl_vlc_get_vlclbf(&bs
->vlc
, tbl_B2
, 2);
840 case PIPE_MPEG12_PICTURE_CODING_TYPE_P
:
841 mb
.macroblock_type
= vl_vlc_get_vlclbf(&bs
->vlc
, tbl_B3
, 6);
844 case PIPE_MPEG12_PICTURE_CODING_TYPE_B
:
845 mb
.macroblock_type
= vl_vlc_get_vlclbf(&bs
->vlc
, tbl_B4
, 6);
849 mb
.macroblock_type
= 0;
854 mb
.macroblock_modes
.value
= 0;
855 if (mb
.macroblock_type
& (PIPE_MPEG12_MB_TYPE_MOTION_FORWARD
| PIPE_MPEG12_MB_TYPE_MOTION_BACKWARD
)) {
856 if (bs
->desc
->picture_structure
== PIPE_MPEG12_PICTURE_STRUCTURE_FRAME
) {
857 if (bs
->desc
->frame_pred_frame_dct
== 0)
858 mb
.macroblock_modes
.bits
.frame_motion_type
= vl_vlc_get_uimsbf(&bs
->vlc
, 2);
860 mb
.macroblock_modes
.bits
.frame_motion_type
= 2;
862 mb
.macroblock_modes
.bits
.field_motion_type
= vl_vlc_get_uimsbf(&bs
->vlc
, 2);
864 } else if ((mb
.macroblock_type
& PIPE_MPEG12_MB_TYPE_INTRA
) && bs
->desc
->concealment_motion_vectors
) {
865 if (bs
->desc
->picture_structure
== PIPE_MPEG12_PICTURE_STRUCTURE_FRAME
)
866 mb
.macroblock_modes
.bits
.frame_motion_type
= 2;
868 mb
.macroblock_modes
.bits
.field_motion_type
= 1;
871 if (bs
->desc
->picture_structure
== PIPE_MPEG12_PICTURE_STRUCTURE_FRAME
&&
872 bs
->desc
->frame_pred_frame_dct
== 0 &&
873 mb
.macroblock_type
& (PIPE_MPEG12_MB_TYPE_INTRA
| PIPE_MPEG12_MB_TYPE_PATTERN
))
874 mb
.macroblock_modes
.bits
.dct_type
= vl_vlc_get_uimsbf(&bs
->vlc
, 1);
876 if (mb
.macroblock_type
& PIPE_MPEG12_MB_TYPE_QUANT
)
877 dct_scale
= quant_scale
[bs
->desc
->q_scale_type
][vl_vlc_get_uimsbf(&bs
->vlc
, 5)];
879 if (inc
> 1 && bs
->desc
->picture_coding_type
== PIPE_MPEG12_PICTURE_CODING_TYPE_P
)
880 memset(mb
.PMV
, 0, sizeof(mb
.PMV
));
882 mb
.motion_vertical_field_select
= 0;
883 if ((mb
.macroblock_type
& PIPE_MPEG12_MB_TYPE_MOTION_FORWARD
) ||
884 (mb
.macroblock_type
& PIPE_MPEG12_MB_TYPE_INTRA
&& bs
->desc
->concealment_motion_vectors
)) {
885 if (bs
->desc
->picture_structure
== PIPE_MPEG12_PICTURE_STRUCTURE_FRAME
)
886 motion_vector_frame(bs
, 0, &mb
);
888 motion_vector_field(bs
, 0, &mb
);
891 if (mb
.macroblock_type
& PIPE_MPEG12_MB_TYPE_MOTION_BACKWARD
) {
892 if (bs
->desc
->picture_structure
== PIPE_MPEG12_PICTURE_STRUCTURE_FRAME
)
893 motion_vector_frame(bs
, 1, &mb
);
895 motion_vector_field(bs
, 1, &mb
);
898 if (mb
.macroblock_type
& PIPE_MPEG12_MB_TYPE_INTRA
&& bs
->desc
->concealment_motion_vectors
) {
899 unsigned extra
= vl_vlc_get_uimsbf(&bs
->vlc
, 1);
900 mb
.PMV
[1][0][0] = mb
.PMV
[0][0][0];
901 mb
.PMV
[1][0][1] = mb
.PMV
[0][0][1];
903 } else if (mb
.macroblock_type
& PIPE_MPEG12_MB_TYPE_INTRA
||
904 !(mb
.macroblock_type
& (PIPE_MPEG12_MB_TYPE_MOTION_FORWARD
|
905 PIPE_MPEG12_MB_TYPE_MOTION_BACKWARD
))) {
906 memset(mb
.PMV
, 0, sizeof(mb
.PMV
));
909 if ((mb
.macroblock_type
& PIPE_MPEG12_MB_TYPE_MOTION_FORWARD
&&
910 mb
.macroblock_modes
.bits
.frame_motion_type
== 2) ||
911 (mb
.macroblock_modes
.bits
.frame_motion_type
== 3)) {
912 mb
.PMV
[1][0][0] = mb
.PMV
[0][0][0];
913 mb
.PMV
[1][0][1] = mb
.PMV
[0][0][1];
916 if (mb
.macroblock_type
& PIPE_MPEG12_MB_TYPE_MOTION_BACKWARD
&&
917 mb
.macroblock_modes
.bits
.frame_motion_type
== 2) {
918 mb
.PMV
[1][1][0] = mb
.PMV
[0][1][0];
919 mb
.PMV
[1][1][1] = mb
.PMV
[0][1][1];
922 if (inc
> 1 || !(mb
.macroblock_type
& PIPE_MPEG12_MB_TYPE_INTRA
))
925 if (mb
.macroblock_type
& (PIPE_MPEG12_MB_TYPE_INTRA
| PIPE_MPEG12_MB_TYPE_PATTERN
)) {
926 memset(dct_blocks
, 0, sizeof(dct_blocks
));
927 decode_dct(bs
, &mb
, dct_scale
);
929 mb
.coded_block_pattern
= 0;
931 vl_vlc_fillbits(&bs
->vlc
);
932 } while (vl_vlc_bits_left(&bs
->vlc
) && vl_vlc_peekbits(&bs
->vlc
, 23));
934 mb
.num_skipped_macroblocks
= 0;
935 bs
->decoder
->decode_macroblock(bs
->decoder
, target
, &bs
->desc
->base
, &mb
.base
, 1);
939 vl_mpg12_bs_init(struct vl_mpg12_bs
*bs
, struct pipe_video_decoder
*decoder
)
941 static bool tables_initialized
= false;
945 memset(bs
, 0, sizeof(struct vl_mpg12_bs
));
947 bs
->decoder
= decoder
;
949 if (!tables_initialized
) {
951 tables_initialized
= true;
956 vl_mpg12_bs_decode(struct vl_mpg12_bs
*bs
,
957 struct pipe_video_buffer
*target
,
958 struct pipe_mpeg12_picture_desc
*picture
,
959 unsigned num_buffers
,
960 const void * const *buffers
,
961 const unsigned *sizes
)
966 bs
->intra_dct_tbl
= picture
->intra_vlc_format
? tbl_B15
: tbl_B14_AC
;
968 vl_vlc_init(&bs
->vlc
, num_buffers
, buffers
, sizes
);
969 while (vl_vlc_bits_left(&bs
->vlc
) > 32) {
970 uint32_t code
= vl_vlc_peekbits(&bs
->vlc
, 32);
972 if (code
>= 0x101 && code
<= 0x1AF) {
973 vl_vlc_eatbits(&bs
->vlc
, 24);
974 decode_slice(bs
, target
);
976 /* align to a byte again */
977 vl_vlc_eatbits(&bs
->vlc
, vl_vlc_valid_bits(&bs
->vlc
) & 7);
980 vl_vlc_eatbits(&bs
->vlc
, 8);
983 vl_vlc_fillbits(&bs
->vlc
);