Mercurial > mplayer.hg
annotate libmpeg2/slice.c @ 12063:e11fc0ef72db
The attached patch fixes a minor build problem on GNU/k*BSD: getch2.c should
check for Glibc, not GNU.
patch by Robert Millan <zeratul2@wanadoo.es>
author | diego |
---|---|
date | Wed, 24 Mar 2004 17:51:35 +0000 |
parents | 15fe3fba5b1d |
children | d0a8810e155c |
rev | line source |
---|---|
1 | 1 /* |
2 * slice.c | |
9852 | 3 * Copyright (C) 2000-2002 Michel Lespinasse <walken@zoy.org> |
4 * Copyright (C) 1999-2000 Aaron Holtzman <aholtzma@ess.engr.uvic.ca> | |
1 | 5 * |
6 * This file is part of mpeg2dec, a free MPEG-2 video stream decoder. | |
9852 | 7 * See http://libmpeg2.sourceforge.net/ for updates. |
1 | 8 * |
9 * mpeg2dec is free software; you can redistribute it and/or modify | |
10 * it under the terms of the GNU General Public License as published by | |
11 * the Free Software Foundation; either version 2 of the License, or | |
12 * (at your option) any later version. | |
13 * | |
14 * mpeg2dec is distributed in the hope that it will be useful, | |
15 * but WITHOUT ANY WARRANTY; without even the implied warranty of | |
16 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the | |
17 * GNU General Public License for more details. | |
18 * | |
19 * You should have received a copy of the GNU General Public License | |
20 * along with this program; if not, write to the Free Software | |
21 * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA | |
22 */ | |
23 | |
24 #include "config.h" | |
25 | |
26 #include <inttypes.h> | |
27 | |
9852 | 28 #include "mpeg2.h" |
1 | 29 #include "mpeg2_internal.h" |
30 #include "attributes.h" | |
31 | |
9852 | 32 extern mpeg2_mc_t mpeg2_mc; |
33 extern void (* mpeg2_idct_copy) (int16_t * block, uint8_t * dest, int stride); | |
34 extern void (* mpeg2_idct_add) (int last, int16_t * block, | |
35 uint8_t * dest, int stride); | |
36 extern void (* mpeg2_cpu_state_save) (cpu_state_t * state); | |
37 extern void (* mpeg2_cpu_state_restore) (cpu_state_t * state); | |
2756
b4d0cc1fd14b
workaround for MBC/MBR difference (wrong libavcodec-mplayer version pair)
arpi
parents:
142
diff
changeset
|
38 |
1 | 39 #include "vlc.h" |
40 | |
41 static int non_linear_quantizer_scale [] = { | |
42 0, 1, 2, 3, 4, 5, 6, 7, | |
43 8, 10, 12, 14, 16, 18, 20, 22, | |
44 24, 28, 32, 36, 40, 44, 48, 52, | |
45 56, 64, 72, 80, 88, 96, 104, 112 | |
46 }; | |
47 | |
9852 | 48 static inline int get_macroblock_modes (decoder_t * const decoder) |
1 | 49 { |
9852 | 50 #define bit_buf (decoder->bitstream_buf) |
51 #define bits (decoder->bitstream_bits) | |
52 #define bit_ptr (decoder->bitstream_ptr) | |
1 | 53 int macroblock_modes; |
9852 | 54 const MBtab * tab; |
1 | 55 |
9852 | 56 switch (decoder->coding_type) { |
1 | 57 case I_TYPE: |
58 | |
59 tab = MB_I + UBITS (bit_buf, 1); | |
60 DUMPBITS (bit_buf, bits, tab->len); | |
61 macroblock_modes = tab->modes; | |
62 | |
9852 | 63 if ((! (decoder->frame_pred_frame_dct)) && |
64 (decoder->picture_structure == FRAME_PICTURE)) { | |
1 | 65 macroblock_modes |= UBITS (bit_buf, 1) * DCT_TYPE_INTERLACED; |
66 DUMPBITS (bit_buf, bits, 1); | |
67 } | |
68 | |
69 return macroblock_modes; | |
70 | |
71 case P_TYPE: | |
72 | |
73 tab = MB_P + UBITS (bit_buf, 5); | |
74 DUMPBITS (bit_buf, bits, tab->len); | |
75 macroblock_modes = tab->modes; | |
76 | |
9852 | 77 if (decoder->picture_structure != FRAME_PICTURE) { |
1 | 78 if (macroblock_modes & MACROBLOCK_MOTION_FORWARD) { |
79 macroblock_modes |= UBITS (bit_buf, 2) * MOTION_TYPE_BASE; | |
80 DUMPBITS (bit_buf, bits, 2); | |
81 } | |
82 return macroblock_modes; | |
9852 | 83 } else if (decoder->frame_pred_frame_dct) { |
1 | 84 if (macroblock_modes & MACROBLOCK_MOTION_FORWARD) |
85 macroblock_modes |= MC_FRAME; | |
86 return macroblock_modes; | |
87 } else { | |
88 if (macroblock_modes & MACROBLOCK_MOTION_FORWARD) { | |
89 macroblock_modes |= UBITS (bit_buf, 2) * MOTION_TYPE_BASE; | |
90 DUMPBITS (bit_buf, bits, 2); | |
91 } | |
92 if (macroblock_modes & (MACROBLOCK_INTRA | MACROBLOCK_PATTERN)) { | |
93 macroblock_modes |= UBITS (bit_buf, 1) * DCT_TYPE_INTERLACED; | |
94 DUMPBITS (bit_buf, bits, 1); | |
95 } | |
96 return macroblock_modes; | |
97 } | |
98 | |
99 case B_TYPE: | |
100 | |
101 tab = MB_B + UBITS (bit_buf, 6); | |
102 DUMPBITS (bit_buf, bits, tab->len); | |
103 macroblock_modes = tab->modes; | |
104 | |
9852 | 105 if (decoder->picture_structure != FRAME_PICTURE) { |
1 | 106 if (! (macroblock_modes & MACROBLOCK_INTRA)) { |
107 macroblock_modes |= UBITS (bit_buf, 2) * MOTION_TYPE_BASE; | |
108 DUMPBITS (bit_buf, bits, 2); | |
109 } | |
110 return macroblock_modes; | |
9852 | 111 } else if (decoder->frame_pred_frame_dct) { |
36 | 112 /* if (! (macroblock_modes & MACROBLOCK_INTRA)) */ |
1 | 113 macroblock_modes |= MC_FRAME; |
114 return macroblock_modes; | |
115 } else { | |
116 if (macroblock_modes & MACROBLOCK_INTRA) | |
117 goto intra; | |
118 macroblock_modes |= UBITS (bit_buf, 2) * MOTION_TYPE_BASE; | |
119 DUMPBITS (bit_buf, bits, 2); | |
120 if (macroblock_modes & (MACROBLOCK_INTRA | MACROBLOCK_PATTERN)) { | |
121 intra: | |
122 macroblock_modes |= UBITS (bit_buf, 1) * DCT_TYPE_INTERLACED; | |
123 DUMPBITS (bit_buf, bits, 1); | |
124 } | |
125 return macroblock_modes; | |
126 } | |
127 | |
128 case D_TYPE: | |
129 | |
130 DUMPBITS (bit_buf, bits, 1); | |
131 return MACROBLOCK_INTRA; | |
132 | |
133 default: | |
134 return 0; | |
135 } | |
136 #undef bit_buf | |
137 #undef bits | |
138 #undef bit_ptr | |
139 } | |
140 | |
9852 | 141 static inline int get_quantizer_scale (decoder_t * const decoder) |
1 | 142 { |
9852 | 143 #define bit_buf (decoder->bitstream_buf) |
144 #define bits (decoder->bitstream_bits) | |
145 #define bit_ptr (decoder->bitstream_ptr) | |
1 | 146 |
147 int quantizer_scale_code; | |
148 | |
149 quantizer_scale_code = UBITS (bit_buf, 5); | |
150 DUMPBITS (bit_buf, bits, 5); | |
151 | |
9852 | 152 if (decoder->q_scale_type) |
1 | 153 return non_linear_quantizer_scale [quantizer_scale_code]; |
154 else | |
155 return quantizer_scale_code << 1; | |
156 #undef bit_buf | |
157 #undef bits | |
158 #undef bit_ptr | |
159 } | |
160 | |
9852 | 161 static inline int get_motion_delta (decoder_t * const decoder, |
162 const int f_code) | |
1 | 163 { |
9852 | 164 #define bit_buf (decoder->bitstream_buf) |
165 #define bits (decoder->bitstream_bits) | |
166 #define bit_ptr (decoder->bitstream_ptr) | |
1 | 167 |
168 int delta; | |
169 int sign; | |
9852 | 170 const MVtab * tab; |
1 | 171 |
172 if (bit_buf & 0x80000000) { | |
173 DUMPBITS (bit_buf, bits, 1); | |
174 return 0; | |
175 } else if (bit_buf >= 0x0c000000) { | |
176 | |
177 tab = MV_4 + UBITS (bit_buf, 4); | |
178 delta = (tab->delta << f_code) + 1; | |
179 bits += tab->len + f_code + 1; | |
180 bit_buf <<= tab->len; | |
181 | |
182 sign = SBITS (bit_buf, 1); | |
183 bit_buf <<= 1; | |
184 | |
185 if (f_code) | |
186 delta += UBITS (bit_buf, f_code); | |
187 bit_buf <<= f_code; | |
188 | |
189 return (delta ^ sign) - sign; | |
190 | |
191 } else { | |
192 | |
193 tab = MV_10 + UBITS (bit_buf, 10); | |
194 delta = (tab->delta << f_code) + 1; | |
195 bits += tab->len + 1; | |
196 bit_buf <<= tab->len; | |
197 | |
198 sign = SBITS (bit_buf, 1); | |
199 bit_buf <<= 1; | |
200 | |
201 if (f_code) { | |
202 NEEDBITS (bit_buf, bits, bit_ptr); | |
203 delta += UBITS (bit_buf, f_code); | |
204 DUMPBITS (bit_buf, bits, f_code); | |
205 } | |
206 | |
207 return (delta ^ sign) - sign; | |
208 | |
209 } | |
210 #undef bit_buf | |
211 #undef bits | |
212 #undef bit_ptr | |
213 } | |
214 | |
9852 | 215 static inline int bound_motion_vector (const int vector, const int f_code) |
1 | 216 { |
9852 | 217 #if 0 |
218 unsigned int limit; | |
219 int sign; | |
1 | 220 |
221 limit = 16 << f_code; | |
222 | |
9852 | 223 if ((unsigned int)(vector + limit) < 2 * limit) |
224 return vector; | |
225 else { | |
226 sign = ((int32_t)vector) >> 31; | |
227 return vector - ((2 * limit) ^ sign) + sign; | |
228 } | |
1 | 229 #else |
9852 | 230 return ((int32_t)vector << (27 - f_code)) >> (27 - f_code); |
1 | 231 #endif |
232 } | |
233 | |
9852 | 234 static inline int get_dmv (decoder_t * const decoder) |
1 | 235 { |
9852 | 236 #define bit_buf (decoder->bitstream_buf) |
237 #define bits (decoder->bitstream_bits) | |
238 #define bit_ptr (decoder->bitstream_ptr) | |
1 | 239 |
9852 | 240 const DMVtab * tab; |
1 | 241 |
242 tab = DMV_2 + UBITS (bit_buf, 2); | |
243 DUMPBITS (bit_buf, bits, tab->len); | |
244 return tab->dmv; | |
245 #undef bit_buf | |
246 #undef bits | |
247 #undef bit_ptr | |
248 } | |
249 | |
9852 | 250 static inline int get_coded_block_pattern (decoder_t * const decoder) |
1 | 251 { |
9852 | 252 #define bit_buf (decoder->bitstream_buf) |
253 #define bits (decoder->bitstream_bits) | |
254 #define bit_ptr (decoder->bitstream_ptr) | |
1 | 255 |
9852 | 256 const CBPtab * tab; |
1 | 257 |
258 NEEDBITS (bit_buf, bits, bit_ptr); | |
259 | |
260 if (bit_buf >= 0x20000000) { | |
261 | |
9852 | 262 tab = CBP_7 + (UBITS (bit_buf, 7) - 16); |
1 | 263 DUMPBITS (bit_buf, bits, tab->len); |
264 return tab->cbp; | |
265 | |
266 } else { | |
267 | |
268 tab = CBP_9 + UBITS (bit_buf, 9); | |
269 DUMPBITS (bit_buf, bits, tab->len); | |
270 return tab->cbp; | |
271 } | |
272 | |
273 #undef bit_buf | |
274 #undef bits | |
275 #undef bit_ptr | |
276 } | |
277 | |
9852 | 278 static inline int get_luma_dc_dct_diff (decoder_t * const decoder) |
1 | 279 { |
9852 | 280 #define bit_buf (decoder->bitstream_buf) |
281 #define bits (decoder->bitstream_bits) | |
282 #define bit_ptr (decoder->bitstream_ptr) | |
283 const DCtab * tab; | |
1 | 284 int size; |
285 int dc_diff; | |
286 | |
287 if (bit_buf < 0xf8000000) { | |
288 tab = DC_lum_5 + UBITS (bit_buf, 5); | |
289 size = tab->size; | |
290 if (size) { | |
291 bits += tab->len + size; | |
292 bit_buf <<= tab->len; | |
293 dc_diff = | |
294 UBITS (bit_buf, size) - UBITS (SBITS (~bit_buf, 1), size); | |
295 bit_buf <<= size; | |
296 return dc_diff; | |
297 } else { | |
298 DUMPBITS (bit_buf, bits, 3); | |
299 return 0; | |
300 } | |
301 } else { | |
9852 | 302 tab = DC_long + (UBITS (bit_buf, 9) - 0x1e0); |
1 | 303 size = tab->size; |
304 DUMPBITS (bit_buf, bits, tab->len); | |
305 NEEDBITS (bit_buf, bits, bit_ptr); | |
306 dc_diff = UBITS (bit_buf, size) - UBITS (SBITS (~bit_buf, 1), size); | |
307 DUMPBITS (bit_buf, bits, size); | |
308 return dc_diff; | |
309 } | |
310 #undef bit_buf | |
311 #undef bits | |
312 #undef bit_ptr | |
313 } | |
314 | |
9852 | 315 static inline int get_chroma_dc_dct_diff (decoder_t * const decoder) |
1 | 316 { |
9852 | 317 #define bit_buf (decoder->bitstream_buf) |
318 #define bits (decoder->bitstream_bits) | |
319 #define bit_ptr (decoder->bitstream_ptr) | |
320 const DCtab * tab; | |
1 | 321 int size; |
322 int dc_diff; | |
323 | |
324 if (bit_buf < 0xf8000000) { | |
325 tab = DC_chrom_5 + UBITS (bit_buf, 5); | |
326 size = tab->size; | |
327 if (size) { | |
328 bits += tab->len + size; | |
329 bit_buf <<= tab->len; | |
330 dc_diff = | |
331 UBITS (bit_buf, size) - UBITS (SBITS (~bit_buf, 1), size); | |
332 bit_buf <<= size; | |
333 return dc_diff; | |
334 } else { | |
335 DUMPBITS (bit_buf, bits, 2); | |
336 return 0; | |
337 } | |
338 } else { | |
9852 | 339 tab = DC_long + (UBITS (bit_buf, 10) - 0x3e0); |
1 | 340 size = tab->size; |
341 DUMPBITS (bit_buf, bits, tab->len + 1); | |
342 NEEDBITS (bit_buf, bits, bit_ptr); | |
343 dc_diff = UBITS (bit_buf, size) - UBITS (SBITS (~bit_buf, 1), size); | |
344 DUMPBITS (bit_buf, bits, size); | |
345 return dc_diff; | |
346 } | |
347 #undef bit_buf | |
348 #undef bits | |
349 #undef bit_ptr | |
350 } | |
351 | |
9852 | 352 #define SATURATE(val) \ |
353 do { \ | |
354 if (unlikely ((uint32_t)(val + 2048) > 4095)) \ | |
355 val = SBITS (val, 1) ^ 2047; \ | |
1 | 356 } while (0) |
357 | |
9852 | 358 static void get_intra_block_B14 (decoder_t * const decoder) |
1 | 359 { |
360 int i; | |
361 int j; | |
362 int val; | |
9852 | 363 const uint8_t * scan = decoder->scan; |
364 const uint8_t * quant_matrix = decoder->intra_quantizer_matrix; | |
365 int quantizer_scale = decoder->quantizer_scale; | |
1 | 366 int mismatch; |
9852 | 367 const DCTtab * tab; |
1 | 368 uint32_t bit_buf; |
369 int bits; | |
9852 | 370 const uint8_t * bit_ptr; |
36 | 371 int16_t * dest; |
1 | 372 |
9852 | 373 dest = decoder->DCTblock; |
1 | 374 i = 0; |
375 mismatch = ~dest[0]; | |
376 | |
9852 | 377 bit_buf = decoder->bitstream_buf; |
378 bits = decoder->bitstream_bits; | |
379 bit_ptr = decoder->bitstream_ptr; | |
1 | 380 |
381 NEEDBITS (bit_buf, bits, bit_ptr); | |
382 | |
383 while (1) { | |
384 if (bit_buf >= 0x28000000) { | |
385 | |
9852 | 386 tab = DCT_B14AC_5 + (UBITS (bit_buf, 5) - 5); |
1 | 387 |
388 i += tab->run; | |
389 if (i >= 64) | |
36 | 390 break; /* end of block */ |
1 | 391 |
392 normal_code: | |
393 j = scan[i]; | |
394 bit_buf <<= tab->len; | |
395 bits += tab->len + 1; | |
396 val = (tab->level * quantizer_scale * quant_matrix[j]) >> 4; | |
397 | |
36 | 398 /* if (bitstream_get (1)) val = -val; */ |
1 | 399 val = (val ^ SBITS (bit_buf, 1)) - SBITS (bit_buf, 1); |
400 | |
401 SATURATE (val); | |
402 dest[j] = val; | |
403 mismatch ^= val; | |
404 | |
405 bit_buf <<= 1; | |
406 NEEDBITS (bit_buf, bits, bit_ptr); | |
407 | |
408 continue; | |
409 | |
410 } else if (bit_buf >= 0x04000000) { | |
411 | |
9852 | 412 tab = DCT_B14_8 + (UBITS (bit_buf, 8) - 4); |
1 | 413 |
414 i += tab->run; | |
415 if (i < 64) | |
416 goto normal_code; | |
417 | |
36 | 418 /* escape code */ |
1 | 419 |
420 i += UBITS (bit_buf << 6, 6) - 64; | |
421 if (i >= 64) | |
36 | 422 break; /* illegal, check needed to avoid buffer overflow */ |
1 | 423 |
424 j = scan[i]; | |
425 | |
426 DUMPBITS (bit_buf, bits, 12); | |
427 NEEDBITS (bit_buf, bits, bit_ptr); | |
428 val = (SBITS (bit_buf, 12) * | |
429 quantizer_scale * quant_matrix[j]) / 16; | |
430 | |
431 SATURATE (val); | |
432 dest[j] = val; | |
433 mismatch ^= val; | |
434 | |
435 DUMPBITS (bit_buf, bits, 12); | |
436 NEEDBITS (bit_buf, bits, bit_ptr); | |
437 | |
438 continue; | |
439 | |
440 } else if (bit_buf >= 0x02000000) { | |
9852 | 441 tab = DCT_B14_10 + (UBITS (bit_buf, 10) - 8); |
1 | 442 i += tab->run; |
443 if (i < 64) | |
444 goto normal_code; | |
445 } else if (bit_buf >= 0x00800000) { | |
9852 | 446 tab = DCT_13 + (UBITS (bit_buf, 13) - 16); |
1 | 447 i += tab->run; |
448 if (i < 64) | |
449 goto normal_code; | |
450 } else if (bit_buf >= 0x00200000) { | |
9852 | 451 tab = DCT_15 + (UBITS (bit_buf, 15) - 16); |
1 | 452 i += tab->run; |
453 if (i < 64) | |
454 goto normal_code; | |
455 } else { | |
456 tab = DCT_16 + UBITS (bit_buf, 16); | |
457 bit_buf <<= 16; | |
458 GETWORD (bit_buf, bits + 16, bit_ptr); | |
459 i += tab->run; | |
460 if (i < 64) | |
461 goto normal_code; | |
462 } | |
36 | 463 break; /* illegal, check needed to avoid buffer overflow */ |
1 | 464 } |
465 dest[63] ^= mismatch & 1; | |
36 | 466 DUMPBITS (bit_buf, bits, 2); /* dump end of block code */ |
9852 | 467 decoder->bitstream_buf = bit_buf; |
468 decoder->bitstream_bits = bits; | |
469 decoder->bitstream_ptr = bit_ptr; | |
1 | 470 } |
471 | |
9852 | 472 static void get_intra_block_B15 (decoder_t * const decoder) |
1 | 473 { |
474 int i; | |
475 int j; | |
476 int val; | |
9852 | 477 const uint8_t * scan = decoder->scan; |
478 const uint8_t * quant_matrix = decoder->intra_quantizer_matrix; | |
479 int quantizer_scale = decoder->quantizer_scale; | |
1 | 480 int mismatch; |
9852 | 481 const DCTtab * tab; |
1 | 482 uint32_t bit_buf; |
483 int bits; | |
9852 | 484 const uint8_t * bit_ptr; |
36 | 485 int16_t * dest; |
1 | 486 |
9852 | 487 dest = decoder->DCTblock; |
1 | 488 i = 0; |
489 mismatch = ~dest[0]; | |
490 | |
9852 | 491 bit_buf = decoder->bitstream_buf; |
492 bits = decoder->bitstream_bits; | |
493 bit_ptr = decoder->bitstream_ptr; | |
1 | 494 |
495 NEEDBITS (bit_buf, bits, bit_ptr); | |
496 | |
497 while (1) { | |
498 if (bit_buf >= 0x04000000) { | |
499 | |
9852 | 500 tab = DCT_B15_8 + (UBITS (bit_buf, 8) - 4); |
1 | 501 |
502 i += tab->run; | |
503 if (i < 64) { | |
504 | |
505 normal_code: | |
506 j = scan[i]; | |
507 bit_buf <<= tab->len; | |
508 bits += tab->len + 1; | |
509 val = (tab->level * quantizer_scale * quant_matrix[j]) >> 4; | |
510 | |
36 | 511 /* if (bitstream_get (1)) val = -val; */ |
1 | 512 val = (val ^ SBITS (bit_buf, 1)) - SBITS (bit_buf, 1); |
513 | |
514 SATURATE (val); | |
515 dest[j] = val; | |
516 mismatch ^= val; | |
517 | |
518 bit_buf <<= 1; | |
519 NEEDBITS (bit_buf, bits, bit_ptr); | |
520 | |
521 continue; | |
522 | |
523 } else { | |
524 | |
36 | 525 /* end of block. I commented out this code because if we */ |
526 /* dont exit here we will still exit at the later test :) */ | |
1 | 527 |
36 | 528 /* if (i >= 128) break; */ /* end of block */ |
1 | 529 |
36 | 530 /* escape code */ |
1 | 531 |
532 i += UBITS (bit_buf << 6, 6) - 64; | |
533 if (i >= 64) | |
36 | 534 break; /* illegal, check against buffer overflow */ |
1 | 535 |
536 j = scan[i]; | |
537 | |
538 DUMPBITS (bit_buf, bits, 12); | |
539 NEEDBITS (bit_buf, bits, bit_ptr); | |
540 val = (SBITS (bit_buf, 12) * | |
541 quantizer_scale * quant_matrix[j]) / 16; | |
542 | |
543 SATURATE (val); | |
544 dest[j] = val; | |
545 mismatch ^= val; | |
546 | |
547 DUMPBITS (bit_buf, bits, 12); | |
548 NEEDBITS (bit_buf, bits, bit_ptr); | |
549 | |
550 continue; | |
551 | |
552 } | |
553 } else if (bit_buf >= 0x02000000) { | |
9852 | 554 tab = DCT_B15_10 + (UBITS (bit_buf, 10) - 8); |
1 | 555 i += tab->run; |
556 if (i < 64) | |
557 goto normal_code; | |
558 } else if (bit_buf >= 0x00800000) { | |
9852 | 559 tab = DCT_13 + (UBITS (bit_buf, 13) - 16); |
1 | 560 i += tab->run; |
561 if (i < 64) | |
562 goto normal_code; | |
563 } else if (bit_buf >= 0x00200000) { | |
9852 | 564 tab = DCT_15 + (UBITS (bit_buf, 15) - 16); |
1 | 565 i += tab->run; |
566 if (i < 64) | |
567 goto normal_code; | |
568 } else { | |
569 tab = DCT_16 + UBITS (bit_buf, 16); | |
570 bit_buf <<= 16; | |
571 GETWORD (bit_buf, bits + 16, bit_ptr); | |
572 i += tab->run; | |
573 if (i < 64) | |
574 goto normal_code; | |
575 } | |
36 | 576 break; /* illegal, check needed to avoid buffer overflow */ |
1 | 577 } |
578 dest[63] ^= mismatch & 1; | |
36 | 579 DUMPBITS (bit_buf, bits, 4); /* dump end of block code */ |
9852 | 580 decoder->bitstream_buf = bit_buf; |
581 decoder->bitstream_bits = bits; | |
582 decoder->bitstream_ptr = bit_ptr; | |
1 | 583 } |
584 | |
9852 | 585 static int get_non_intra_block (decoder_t * const decoder) |
1 | 586 { |
587 int i; | |
588 int j; | |
589 int val; | |
9852 | 590 const uint8_t * scan = decoder->scan; |
591 const uint8_t * quant_matrix = decoder->non_intra_quantizer_matrix; | |
592 int quantizer_scale = decoder->quantizer_scale; | |
1 | 593 int mismatch; |
9852 | 594 const DCTtab * tab; |
1 | 595 uint32_t bit_buf; |
596 int bits; | |
9852 | 597 const uint8_t * bit_ptr; |
36 | 598 int16_t * dest; |
1 | 599 |
600 i = -1; | |
601 mismatch = 1; | |
9852 | 602 dest = decoder->DCTblock; |
1 | 603 |
9852 | 604 bit_buf = decoder->bitstream_buf; |
605 bits = decoder->bitstream_bits; | |
606 bit_ptr = decoder->bitstream_ptr; | |
1 | 607 |
608 NEEDBITS (bit_buf, bits, bit_ptr); | |
609 if (bit_buf >= 0x28000000) { | |
9852 | 610 tab = DCT_B14DC_5 + (UBITS (bit_buf, 5) - 5); |
1 | 611 goto entry_1; |
612 } else | |
613 goto entry_2; | |
614 | |
615 while (1) { | |
616 if (bit_buf >= 0x28000000) { | |
617 | |
9852 | 618 tab = DCT_B14AC_5 + (UBITS (bit_buf, 5) - 5); |
1 | 619 |
620 entry_1: | |
621 i += tab->run; | |
622 if (i >= 64) | |
36 | 623 break; /* end of block */ |
1 | 624 |
625 normal_code: | |
626 j = scan[i]; | |
627 bit_buf <<= tab->len; | |
628 bits += tab->len + 1; | |
629 val = ((2*tab->level+1) * quantizer_scale * quant_matrix[j]) >> 5; | |
630 | |
36 | 631 /* if (bitstream_get (1)) val = -val; */ |
1 | 632 val = (val ^ SBITS (bit_buf, 1)) - SBITS (bit_buf, 1); |
633 | |
634 SATURATE (val); | |
635 dest[j] = val; | |
636 mismatch ^= val; | |
637 | |
638 bit_buf <<= 1; | |
639 NEEDBITS (bit_buf, bits, bit_ptr); | |
640 | |
641 continue; | |
642 | |
643 } | |
644 | |
645 entry_2: | |
646 if (bit_buf >= 0x04000000) { | |
647 | |
9852 | 648 tab = DCT_B14_8 + (UBITS (bit_buf, 8) - 4); |
1 | 649 |
650 i += tab->run; | |
651 if (i < 64) | |
652 goto normal_code; | |
653 | |
36 | 654 /* escape code */ |
1 | 655 |
656 i += UBITS (bit_buf << 6, 6) - 64; | |
657 if (i >= 64) | |
36 | 658 break; /* illegal, check needed to avoid buffer overflow */ |
1 | 659 |
660 j = scan[i]; | |
661 | |
662 DUMPBITS (bit_buf, bits, 12); | |
663 NEEDBITS (bit_buf, bits, bit_ptr); | |
664 val = 2 * (SBITS (bit_buf, 12) + SBITS (bit_buf, 1)) + 1; | |
665 val = (val * quantizer_scale * quant_matrix[j]) / 32; | |
666 | |
667 SATURATE (val); | |
668 dest[j] = val; | |
669 mismatch ^= val; | |
670 | |
671 DUMPBITS (bit_buf, bits, 12); | |
672 NEEDBITS (bit_buf, bits, bit_ptr); | |
673 | |
674 continue; | |
675 | |
676 } else if (bit_buf >= 0x02000000) { | |
9852 | 677 tab = DCT_B14_10 + (UBITS (bit_buf, 10) - 8); |
1 | 678 i += tab->run; |
679 if (i < 64) | |
680 goto normal_code; | |
681 } else if (bit_buf >= 0x00800000) { | |
9852 | 682 tab = DCT_13 + (UBITS (bit_buf, 13) - 16); |
1 | 683 i += tab->run; |
684 if (i < 64) | |
685 goto normal_code; | |
686 } else if (bit_buf >= 0x00200000) { | |
9852 | 687 tab = DCT_15 + (UBITS (bit_buf, 15) - 16); |
1 | 688 i += tab->run; |
689 if (i < 64) | |
690 goto normal_code; | |
691 } else { | |
692 tab = DCT_16 + UBITS (bit_buf, 16); | |
693 bit_buf <<= 16; | |
694 GETWORD (bit_buf, bits + 16, bit_ptr); | |
695 i += tab->run; | |
696 if (i < 64) | |
697 goto normal_code; | |
698 } | |
36 | 699 break; /* illegal, check needed to avoid buffer overflow */ |
1 | 700 } |
701 dest[63] ^= mismatch & 1; | |
36 | 702 DUMPBITS (bit_buf, bits, 2); /* dump end of block code */ |
9852 | 703 decoder->bitstream_buf = bit_buf; |
704 decoder->bitstream_bits = bits; | |
705 decoder->bitstream_ptr = bit_ptr; | |
706 return i; | |
1 | 707 } |
708 | |
9852 | 709 static void get_mpeg1_intra_block (decoder_t * const decoder) |
1 | 710 { |
711 int i; | |
712 int j; | |
713 int val; | |
9852 | 714 const uint8_t * scan = decoder->scan; |
715 const uint8_t * quant_matrix = decoder->intra_quantizer_matrix; | |
716 int quantizer_scale = decoder->quantizer_scale; | |
717 const DCTtab * tab; | |
1 | 718 uint32_t bit_buf; |
719 int bits; | |
9852 | 720 const uint8_t * bit_ptr; |
36 | 721 int16_t * dest; |
1 | 722 |
723 i = 0; | |
9852 | 724 dest = decoder->DCTblock; |
1 | 725 |
9852 | 726 bit_buf = decoder->bitstream_buf; |
727 bits = decoder->bitstream_bits; | |
728 bit_ptr = decoder->bitstream_ptr; | |
1 | 729 |
730 NEEDBITS (bit_buf, bits, bit_ptr); | |
731 | |
732 while (1) { | |
733 if (bit_buf >= 0x28000000) { | |
734 | |
9852 | 735 tab = DCT_B14AC_5 + (UBITS (bit_buf, 5) - 5); |
1 | 736 |
737 i += tab->run; | |
738 if (i >= 64) | |
36 | 739 break; /* end of block */ |
1 | 740 |
741 normal_code: | |
742 j = scan[i]; | |
743 bit_buf <<= tab->len; | |
744 bits += tab->len + 1; | |
745 val = (tab->level * quantizer_scale * quant_matrix[j]) >> 4; | |
746 | |
36 | 747 /* oddification */ |
1 | 748 val = (val - 1) | 1; |
749 | |
36 | 750 /* if (bitstream_get (1)) val = -val; */ |
1 | 751 val = (val ^ SBITS (bit_buf, 1)) - SBITS (bit_buf, 1); |
752 | |
753 SATURATE (val); | |
754 dest[j] = val; | |
755 | |
756 bit_buf <<= 1; | |
757 NEEDBITS (bit_buf, bits, bit_ptr); | |
758 | |
759 continue; | |
760 | |
761 } else if (bit_buf >= 0x04000000) { | |
762 | |
9852 | 763 tab = DCT_B14_8 + (UBITS (bit_buf, 8) - 4); |
1 | 764 |
765 i += tab->run; | |
766 if (i < 64) | |
767 goto normal_code; | |
768 | |
36 | 769 /* escape code */ |
1 | 770 |
771 i += UBITS (bit_buf << 6, 6) - 64; | |
772 if (i >= 64) | |
36 | 773 break; /* illegal, check needed to avoid buffer overflow */ |
1 | 774 |
775 j = scan[i]; | |
776 | |
777 DUMPBITS (bit_buf, bits, 12); | |
778 NEEDBITS (bit_buf, bits, bit_ptr); | |
779 val = SBITS (bit_buf, 8); | |
780 if (! (val & 0x7f)) { | |
781 DUMPBITS (bit_buf, bits, 8); | |
782 val = UBITS (bit_buf, 8) + 2 * val; | |
783 } | |
784 val = (val * quantizer_scale * quant_matrix[j]) / 16; | |
785 | |
36 | 786 /* oddification */ |
1 | 787 val = (val + ~SBITS (val, 1)) | 1; |
788 | |
789 SATURATE (val); | |
790 dest[j] = val; | |
791 | |
792 DUMPBITS (bit_buf, bits, 8); | |
793 NEEDBITS (bit_buf, bits, bit_ptr); | |
794 | |
795 continue; | |
796 | |
797 } else if (bit_buf >= 0x02000000) { | |
9852 | 798 tab = DCT_B14_10 + (UBITS (bit_buf, 10) - 8); |
1 | 799 i += tab->run; |
800 if (i < 64) | |
801 goto normal_code; | |
802 } else if (bit_buf >= 0x00800000) { | |
9852 | 803 tab = DCT_13 + (UBITS (bit_buf, 13) - 16); |
1 | 804 i += tab->run; |
805 if (i < 64) | |
806 goto normal_code; | |
807 } else if (bit_buf >= 0x00200000) { | |
9852 | 808 tab = DCT_15 + (UBITS (bit_buf, 15) - 16); |
1 | 809 i += tab->run; |
810 if (i < 64) | |
811 goto normal_code; | |
812 } else { | |
813 tab = DCT_16 + UBITS (bit_buf, 16); | |
814 bit_buf <<= 16; | |
815 GETWORD (bit_buf, bits + 16, bit_ptr); | |
816 i += tab->run; | |
817 if (i < 64) | |
818 goto normal_code; | |
819 } | |
36 | 820 break; /* illegal, check needed to avoid buffer overflow */ |
1 | 821 } |
36 | 822 DUMPBITS (bit_buf, bits, 2); /* dump end of block code */ |
9852 | 823 decoder->bitstream_buf = bit_buf; |
824 decoder->bitstream_bits = bits; | |
825 decoder->bitstream_ptr = bit_ptr; | |
1 | 826 } |
827 | |
9852 | 828 static int get_mpeg1_non_intra_block (decoder_t * const decoder) |
1 | 829 { |
830 int i; | |
831 int j; | |
832 int val; | |
9852 | 833 const uint8_t * scan = decoder->scan; |
834 const uint8_t * quant_matrix = decoder->non_intra_quantizer_matrix; | |
835 int quantizer_scale = decoder->quantizer_scale; | |
836 const DCTtab * tab; | |
1 | 837 uint32_t bit_buf; |
838 int bits; | |
9852 | 839 const uint8_t * bit_ptr; |
36 | 840 int16_t * dest; |
1 | 841 |
842 i = -1; | |
9852 | 843 dest = decoder->DCTblock; |
1 | 844 |
9852 | 845 bit_buf = decoder->bitstream_buf; |
846 bits = decoder->bitstream_bits; | |
847 bit_ptr = decoder->bitstream_ptr; | |
1 | 848 |
849 NEEDBITS (bit_buf, bits, bit_ptr); | |
850 if (bit_buf >= 0x28000000) { | |
9852 | 851 tab = DCT_B14DC_5 + (UBITS (bit_buf, 5) - 5); |
1 | 852 goto entry_1; |
853 } else | |
854 goto entry_2; | |
855 | |
856 while (1) { | |
857 if (bit_buf >= 0x28000000) { | |
858 | |
9852 | 859 tab = DCT_B14AC_5 + (UBITS (bit_buf, 5) - 5); |
1 | 860 |
861 entry_1: | |
862 i += tab->run; | |
863 if (i >= 64) | |
36 | 864 break; /* end of block */ |
1 | 865 |
866 normal_code: | |
867 j = scan[i]; | |
868 bit_buf <<= tab->len; | |
869 bits += tab->len + 1; | |
870 val = ((2*tab->level+1) * quantizer_scale * quant_matrix[j]) >> 5; | |
871 | |
36 | 872 /* oddification */ |
1 | 873 val = (val - 1) | 1; |
874 | |
36 | 875 /* if (bitstream_get (1)) val = -val; */ |
1 | 876 val = (val ^ SBITS (bit_buf, 1)) - SBITS (bit_buf, 1); |
877 | |
878 SATURATE (val); | |
879 dest[j] = val; | |
880 | |
881 bit_buf <<= 1; | |
882 NEEDBITS (bit_buf, bits, bit_ptr); | |
883 | |
884 continue; | |
885 | |
886 } | |
887 | |
888 entry_2: | |
889 if (bit_buf >= 0x04000000) { | |
890 | |
9852 | 891 tab = DCT_B14_8 + (UBITS (bit_buf, 8) - 4); |
1 | 892 |
893 i += tab->run; | |
894 if (i < 64) | |
895 goto normal_code; | |
896 | |
36 | 897 /* escape code */ |
1 | 898 |
899 i += UBITS (bit_buf << 6, 6) - 64; | |
900 if (i >= 64) | |
36 | 901 break; /* illegal, check needed to avoid buffer overflow */ |
1 | 902 |
903 j = scan[i]; | |
904 | |
905 DUMPBITS (bit_buf, bits, 12); | |
906 NEEDBITS (bit_buf, bits, bit_ptr); | |
907 val = SBITS (bit_buf, 8); | |
908 if (! (val & 0x7f)) { | |
909 DUMPBITS (bit_buf, bits, 8); | |
910 val = UBITS (bit_buf, 8) + 2 * val; | |
911 } | |
912 val = 2 * (val + SBITS (val, 1)) + 1; | |
913 val = (val * quantizer_scale * quant_matrix[j]) / 32; | |
914 | |
36 | 915 /* oddification */ |
1 | 916 val = (val + ~SBITS (val, 1)) | 1; |
917 | |
918 SATURATE (val); | |
919 dest[j] = val; | |
920 | |
921 DUMPBITS (bit_buf, bits, 8); | |
922 NEEDBITS (bit_buf, bits, bit_ptr); | |
923 | |
924 continue; | |
925 | |
926 } else if (bit_buf >= 0x02000000) { | |
9852 | 927 tab = DCT_B14_10 + (UBITS (bit_buf, 10) - 8); |
1 | 928 i += tab->run; |
929 if (i < 64) | |
930 goto normal_code; | |
931 } else if (bit_buf >= 0x00800000) { | |
9852 | 932 tab = DCT_13 + (UBITS (bit_buf, 13) - 16); |
1 | 933 i += tab->run; |
934 if (i < 64) | |
935 goto normal_code; | |
936 } else if (bit_buf >= 0x00200000) { | |
9852 | 937 tab = DCT_15 + (UBITS (bit_buf, 15) - 16); |
1 | 938 i += tab->run; |
939 if (i < 64) | |
940 goto normal_code; | |
941 } else { | |
942 tab = DCT_16 + UBITS (bit_buf, 16); | |
943 bit_buf <<= 16; | |
944 GETWORD (bit_buf, bits + 16, bit_ptr); | |
945 i += tab->run; | |
946 if (i < 64) | |
947 goto normal_code; | |
948 } | |
36 | 949 break; /* illegal, check needed to avoid buffer overflow */ |
1 | 950 } |
36 | 951 DUMPBITS (bit_buf, bits, 2); /* dump end of block code */ |
9852 | 952 decoder->bitstream_buf = bit_buf; |
953 decoder->bitstream_bits = bits; | |
954 decoder->bitstream_ptr = bit_ptr; | |
955 return i; | |
1 | 956 } |
957 | |
9852 | 958 static inline void slice_intra_DCT (decoder_t * const decoder, const int cc, |
959 uint8_t * const dest, const int stride) | |
1 | 960 { |
9852 | 961 #define bit_buf (decoder->bitstream_buf) |
962 #define bits (decoder->bitstream_bits) | |
963 #define bit_ptr (decoder->bitstream_ptr) | |
1 | 964 NEEDBITS (bit_buf, bits, bit_ptr); |
36 | 965 /* Get the intra DC coefficient and inverse quantize it */ |
1 | 966 if (cc == 0) |
9852 | 967 decoder->dc_dct_pred[0] += get_luma_dc_dct_diff (decoder); |
1 | 968 else |
9852 | 969 decoder->dc_dct_pred[cc] += get_chroma_dc_dct_diff (decoder); |
970 decoder->DCTblock[0] = | |
971 decoder->dc_dct_pred[cc] << (3 - decoder->intra_dc_precision); | |
1 | 972 |
9852 | 973 if (decoder->mpeg1) { |
974 if (decoder->coding_type != D_TYPE) | |
975 get_mpeg1_intra_block (decoder); | |
976 } else if (decoder->intra_vlc_format) | |
977 get_intra_block_B15 (decoder); | |
1 | 978 else |
9852 | 979 get_intra_block_B14 (decoder); |
980 mpeg2_idct_copy (decoder->DCTblock, dest, stride); | |
1 | 981 #undef bit_buf |
982 #undef bits | |
983 #undef bit_ptr | |
984 } | |
985 | |
9852 | 986 static inline void slice_non_intra_DCT (decoder_t * const decoder, |
987 uint8_t * const dest, const int stride) | |
1 | 988 { |
9852 | 989 int last; |
990 | |
991 if (decoder->mpeg1) | |
992 last = get_mpeg1_non_intra_block (decoder); | |
1 | 993 else |
9852 | 994 last = get_non_intra_block (decoder); |
995 mpeg2_idct_add (last, decoder->DCTblock, dest, stride); | |
1 | 996 } |
997 | |
9852 | 998 #define MOTION(table,ref,motion_x,motion_y,size,y) \ |
999 pos_x = 2 * decoder->offset + motion_x; \ | |
1000 pos_y = 2 * decoder->v_offset + motion_y + 2 * y; \ | |
1001 if ((pos_x > decoder->limit_x) || (pos_y > decoder->limit_y_ ## size)) \ | |
1002 return; \ | |
1003 xy_half = ((pos_y & 1) << 1) | (pos_x & 1); \ | |
1004 table[xy_half] (decoder->dest[0] + y * decoder->stride + decoder->offset, \ | |
1005 ref[0] + (pos_x >> 1) + (pos_y >> 1) * decoder->stride, \ | |
1006 decoder->stride, size); \ | |
1007 motion_x /= 2; motion_y /= 2; \ | |
1008 xy_half = ((motion_y & 1) << 1) | (motion_x & 1); \ | |
1009 offset = (((decoder->offset + motion_x) >> 1) + \ | |
1010 ((((decoder->v_offset + motion_y) >> 1) + y/2) * \ | |
1011 decoder->uv_stride)); \ | |
1012 table[4+xy_half] (decoder->dest[1] + y/2 * decoder->uv_stride + \ | |
1013 (decoder->offset >> 1), ref[1] + offset, \ | |
1014 decoder->uv_stride, size/2); \ | |
1015 table[4+xy_half] (decoder->dest[2] + y/2 * decoder->uv_stride + \ | |
1016 (decoder->offset >> 1), ref[2] + offset, \ | |
1017 decoder->uv_stride, size/2) | |
49 | 1018 |
9852 | 1019 #define MOTION_FIELD(table,ref,motion_x,motion_y,dest_field,op,src_field) \ |
1020 pos_x = 2 * decoder->offset + motion_x; \ | |
1021 pos_y = decoder->v_offset + motion_y; \ | |
1022 if ((pos_x > decoder->limit_x) || (pos_y > decoder->limit_y)) \ | |
1023 return; \ | |
1024 xy_half = ((pos_y & 1) << 1) | (pos_x & 1); \ | |
1025 table[xy_half] (decoder->dest[0] + dest_field * decoder->stride + \ | |
1026 decoder->offset, \ | |
1027 (ref[0] + (pos_x >> 1) + \ | |
1028 ((pos_y op) + src_field) * decoder->stride), \ | |
1029 2 * decoder->stride, 8); \ | |
1030 motion_x /= 2; motion_y /= 2; \ | |
1031 xy_half = ((motion_y & 1) << 1) | (motion_x & 1); \ | |
1032 offset = (((decoder->offset + motion_x) >> 1) + \ | |
1033 (((decoder->v_offset >> 1) + (motion_y op) + src_field) * \ | |
1034 decoder->uv_stride)); \ | |
1035 table[4+xy_half] (decoder->dest[1] + dest_field * decoder->uv_stride + \ | |
1036 (decoder->offset >> 1), ref[1] + offset, \ | |
1037 2 * decoder->uv_stride, 4); \ | |
1038 table[4+xy_half] (decoder->dest[2] + dest_field * decoder->uv_stride + \ | |
1039 (decoder->offset >> 1), ref[2] + offset, \ | |
1040 2 * decoder->uv_stride, 4) | |
1 | 1041 |
9852 | 1042 static void motion_mp1 (decoder_t * const decoder, motion_t * const motion, |
1043 mpeg2_mc_fct * const * const table) | |
1 | 1044 { |
9852 | 1045 #define bit_buf (decoder->bitstream_buf) |
1046 #define bits (decoder->bitstream_bits) | |
1047 #define bit_ptr (decoder->bitstream_ptr) | |
1 | 1048 int motion_x, motion_y; |
9852 | 1049 unsigned int pos_x, pos_y, xy_half, offset; |
1 | 1050 |
1051 NEEDBITS (bit_buf, bits, bit_ptr); | |
9852 | 1052 motion_x = (motion->pmv[0][0] + |
1053 (get_motion_delta (decoder, | |
1054 motion->f_code[0]) << motion->f_code[1])); | |
1055 motion_x = bound_motion_vector (motion_x, | |
1056 motion->f_code[0] + motion->f_code[1]); | |
1 | 1057 motion->pmv[0][0] = motion_x; |
1058 | |
1059 NEEDBITS (bit_buf, bits, bit_ptr); | |
9852 | 1060 motion_y = (motion->pmv[0][1] + |
1061 (get_motion_delta (decoder, | |
1062 motion->f_code[0]) << motion->f_code[1])); | |
1063 motion_y = bound_motion_vector (motion_y, | |
1064 motion->f_code[0] + motion->f_code[1]); | |
1 | 1065 motion->pmv[0][1] = motion_y; |
1066 | |
9852 | 1067 MOTION (table, motion->ref[0], motion_x, motion_y, 16, 0); |
1068 #undef bit_buf | |
1069 #undef bits | |
1070 #undef bit_ptr | |
1071 } | |
1 | 1072 |
9852 | 1073 static void motion_fr_frame (decoder_t * const decoder, |
1074 motion_t * const motion, | |
1075 mpeg2_mc_fct * const * const table) | |
1076 { | |
1077 #define bit_buf (decoder->bitstream_buf) | |
1078 #define bits (decoder->bitstream_bits) | |
1079 #define bit_ptr (decoder->bitstream_ptr) | |
1080 int motion_x, motion_y; | |
1081 unsigned int pos_x, pos_y, xy_half, offset; | |
1082 | |
1083 NEEDBITS (bit_buf, bits, bit_ptr); | |
1084 motion_x = motion->pmv[0][0] + get_motion_delta (decoder, | |
1085 motion->f_code[0]); | |
1086 motion_x = bound_motion_vector (motion_x, motion->f_code[0]); | |
1087 motion->pmv[1][0] = motion->pmv[0][0] = motion_x; | |
1088 | |
1089 NEEDBITS (bit_buf, bits, bit_ptr); | |
1090 motion_y = motion->pmv[0][1] + get_motion_delta (decoder, | |
1091 motion->f_code[1]); | |
1092 motion_y = bound_motion_vector (motion_y, motion->f_code[1]); | |
1093 motion->pmv[1][1] = motion->pmv[0][1] = motion_y; | |
1094 | |
1095 MOTION (table, motion->ref[0], motion_x, motion_y, 16, 0); | |
1 | 1096 #undef bit_buf |
1097 #undef bits | |
1098 #undef bit_ptr | |
1099 } | |
1100 | |
9852 | 1101 static void motion_fr_field (decoder_t * const decoder, |
1102 motion_t * const motion, | |
1103 mpeg2_mc_fct * const * const table) | |
1 | 1104 { |
9852 | 1105 #define bit_buf (decoder->bitstream_buf) |
1106 #define bits (decoder->bitstream_bits) | |
1107 #define bit_ptr (decoder->bitstream_ptr) | |
1108 int motion_x, motion_y, field; | |
1109 unsigned int pos_x, pos_y, xy_half, offset; | |
1 | 1110 |
1111 NEEDBITS (bit_buf, bits, bit_ptr); | |
9852 | 1112 field = UBITS (bit_buf, 1); |
1 | 1113 DUMPBITS (bit_buf, bits, 1); |
1114 | |
9852 | 1115 motion_x = motion->pmv[0][0] + get_motion_delta (decoder, |
36 | 1116 motion->f_code[0]); |
1 | 1117 motion_x = bound_motion_vector (motion_x, motion->f_code[0]); |
1118 motion->pmv[0][0] = motion_x; | |
1119 | |
1120 NEEDBITS (bit_buf, bits, bit_ptr); | |
9852 | 1121 motion_y = (motion->pmv[0][1] >> 1) + get_motion_delta (decoder, |
1 | 1122 motion->f_code[1]); |
36 | 1123 /* motion_y = bound_motion_vector (motion_y, motion->f_code[1]); */ |
1 | 1124 motion->pmv[0][1] = motion_y << 1; |
1125 | |
9852 | 1126 MOTION_FIELD (table, motion->ref[0], motion_x, motion_y, 0, & ~1, field); |
1 | 1127 |
1128 NEEDBITS (bit_buf, bits, bit_ptr); | |
9852 | 1129 field = UBITS (bit_buf, 1); |
1 | 1130 DUMPBITS (bit_buf, bits, 1); |
1131 | |
9852 | 1132 motion_x = motion->pmv[1][0] + get_motion_delta (decoder, |
36 | 1133 motion->f_code[0]); |
1 | 1134 motion_x = bound_motion_vector (motion_x, motion->f_code[0]); |
1135 motion->pmv[1][0] = motion_x; | |
1136 | |
1137 NEEDBITS (bit_buf, bits, bit_ptr); | |
9852 | 1138 motion_y = (motion->pmv[1][1] >> 1) + get_motion_delta (decoder, |
1 | 1139 motion->f_code[1]); |
36 | 1140 /* motion_y = bound_motion_vector (motion_y, motion->f_code[1]); */ |
1 | 1141 motion->pmv[1][1] = motion_y << 1; |
1142 | |
9852 | 1143 MOTION_FIELD (table, motion->ref[0], motion_x, motion_y, 1, & ~1, field); |
1 | 1144 #undef bit_buf |
1145 #undef bits | |
1146 #undef bit_ptr | |
1147 } | |
1148 | |
9852 | 1149 static void motion_fr_dmv (decoder_t * const decoder, motion_t * const motion, |
1150 mpeg2_mc_fct * const * const table) | |
1 | 1151 { |
9852 | 1152 #define bit_buf (decoder->bitstream_buf) |
1153 #define bits (decoder->bitstream_bits) | |
1154 #define bit_ptr (decoder->bitstream_ptr) | |
1155 int motion_x, motion_y, dmv_x, dmv_y, m, other_x, other_y; | |
1156 unsigned int pos_x, pos_y, xy_half, offset; | |
1 | 1157 |
1158 NEEDBITS (bit_buf, bits, bit_ptr); | |
9852 | 1159 motion_x = motion->pmv[0][0] + get_motion_delta (decoder, |
36 | 1160 motion->f_code[0]); |
1 | 1161 motion_x = bound_motion_vector (motion_x, motion->f_code[0]); |
1162 motion->pmv[1][0] = motion->pmv[0][0] = motion_x; | |
9852 | 1163 NEEDBITS (bit_buf, bits, bit_ptr); |
1164 dmv_x = get_dmv (decoder); | |
1 | 1165 |
9852 | 1166 motion_y = (motion->pmv[0][1] >> 1) + get_motion_delta (decoder, |
1 | 1167 motion->f_code[1]); |
36 | 1168 /* motion_y = bound_motion_vector (motion_y, motion->f_code[1]); */ |
1 | 1169 motion->pmv[1][1] = motion->pmv[0][1] = motion_y << 1; |
9852 | 1170 dmv_y = get_dmv (decoder); |
1 | 1171 |
9852 | 1172 m = decoder->top_field_first ? 1 : 3; |
1 | 1173 other_x = ((motion_x * m + (motion_x > 0)) >> 1) + dmv_x; |
1174 other_y = ((motion_y * m + (motion_y > 0)) >> 1) + dmv_y - 1; | |
9852 | 1175 MOTION_FIELD (mpeg2_mc.put, motion->ref[0], other_x, other_y, 0, | 1, 0); |
1 | 1176 |
9852 | 1177 m = decoder->top_field_first ? 3 : 1; |
1 | 1178 other_x = ((motion_x * m + (motion_x > 0)) >> 1) + dmv_x; |
1179 other_y = ((motion_y * m + (motion_y > 0)) >> 1) + dmv_y + 1; | |
9852 | 1180 MOTION_FIELD (mpeg2_mc.put, motion->ref[0], other_x, other_y, 1, & ~1, 0); |
1181 | |
1182 xy_half = ((motion_y & 1) << 1) | (motion_x & 1); | |
1183 offset = (decoder->offset + (motion_x >> 1) + | |
1184 (decoder->v_offset + (motion_y & ~1)) * decoder->stride); | |
1185 mpeg2_mc.avg[xy_half] | |
1186 (decoder->dest[0] + decoder->offset, | |
1187 motion->ref[0][0] + offset, 2 * decoder->stride, 8); | |
1188 mpeg2_mc.avg[xy_half] | |
1189 (decoder->dest[0] + decoder->stride + decoder->offset, | |
1190 motion->ref[0][0] + decoder->stride + offset, 2 * decoder->stride, 8); | |
1191 motion_x /= 2; motion_y /= 2; | |
1192 xy_half = ((motion_y & 1) << 1) | (motion_x & 1); | |
1193 offset = (((decoder->offset + motion_x) >> 1) + | |
1194 (((decoder->v_offset >> 1) + (motion_y & ~1)) * | |
1195 decoder->uv_stride)); | |
1196 mpeg2_mc.avg[4+xy_half] | |
1197 (decoder->dest[1] + (decoder->offset >> 1), | |
1198 motion->ref[0][1] + offset, 2 * decoder->uv_stride, 4); | |
1199 mpeg2_mc.avg[4+xy_half] | |
1200 (decoder->dest[1] + decoder->uv_stride + (decoder->offset >> 1), | |
1201 motion->ref[0][1] + decoder->uv_stride + offset, | |
1202 2 * decoder->uv_stride, 4); | |
1203 mpeg2_mc.avg[4+xy_half] | |
1204 (decoder->dest[2] + (decoder->offset >> 1), | |
1205 motion->ref[0][2] + offset, 2 * decoder->uv_stride, 4); | |
1206 mpeg2_mc.avg[4+xy_half] | |
1207 (decoder->dest[2] + decoder->uv_stride + (decoder->offset >> 1), | |
1208 motion->ref[0][2] + decoder->uv_stride + offset, | |
1209 2 * decoder->uv_stride, 4); | |
1 | 1210 #undef bit_buf |
1211 #undef bits | |
1212 #undef bit_ptr | |
1213 } | |
1214 | |
9852 | 1215 static inline void motion_reuse (const decoder_t * const decoder, |
1216 const motion_t * const motion, | |
1217 mpeg2_mc_fct * const * const table) | |
1 | 1218 { |
9852 | 1219 int motion_x, motion_y; |
1220 unsigned int pos_x, pos_y, xy_half, offset; | |
1221 | |
1222 motion_x = motion->pmv[0][0]; | |
1223 motion_y = motion->pmv[0][1]; | |
1224 | |
1225 MOTION (table, motion->ref[0], motion_x, motion_y, 16, 0); | |
1 | 1226 } |
1227 | |
9852 | 1228 static inline void motion_zero (const decoder_t * const decoder, |
1229 const motion_t * const motion, | |
1230 mpeg2_mc_fct * const * const table) | |
1 | 1231 { |
9852 | 1232 unsigned int offset; |
1233 | |
1234 table[0] (decoder->dest[0] + decoder->offset, | |
1235 (motion->ref[0][0] + decoder->offset + | |
1236 decoder->v_offset * decoder->stride), | |
1237 decoder->stride, 16); | |
1238 | |
1239 offset = ((decoder->offset >> 1) + | |
1240 (decoder->v_offset >> 1) * decoder->uv_stride); | |
1241 table[4] (decoder->dest[1] + (decoder->offset >> 1), | |
1242 motion->ref[0][1] + offset, decoder->uv_stride, 8); | |
1243 table[4] (decoder->dest[2] + (decoder->offset >> 1), | |
1244 motion->ref[0][2] + offset, decoder->uv_stride, 8); | |
1 | 1245 } |
1246 | |
36 | 1247 /* like motion_frame, but parsing without actual motion compensation */ |
9852 | 1248 static void motion_fr_conceal (decoder_t * const decoder) |
1 | 1249 { |
9852 | 1250 #define bit_buf (decoder->bitstream_buf) |
1251 #define bits (decoder->bitstream_bits) | |
1252 #define bit_ptr (decoder->bitstream_ptr) | |
1 | 1253 int tmp; |
1254 | |
1255 NEEDBITS (bit_buf, bits, bit_ptr); | |
9852 | 1256 tmp = (decoder->f_motion.pmv[0][0] + |
1257 get_motion_delta (decoder, decoder->f_motion.f_code[0])); | |
1258 tmp = bound_motion_vector (tmp, decoder->f_motion.f_code[0]); | |
1259 decoder->f_motion.pmv[1][0] = decoder->f_motion.pmv[0][0] = tmp; | |
1 | 1260 |
1261 NEEDBITS (bit_buf, bits, bit_ptr); | |
9852 | 1262 tmp = (decoder->f_motion.pmv[0][1] + |
1263 get_motion_delta (decoder, decoder->f_motion.f_code[1])); | |
1264 tmp = bound_motion_vector (tmp, decoder->f_motion.f_code[1]); | |
1265 decoder->f_motion.pmv[1][1] = decoder->f_motion.pmv[0][1] = tmp; | |
1 | 1266 |
36 | 1267 DUMPBITS (bit_buf, bits, 1); /* remove marker_bit */ |
1 | 1268 #undef bit_buf |
1269 #undef bits | |
1270 #undef bit_ptr | |
1271 } | |
1272 | |
9852 | 1273 static void motion_fi_field (decoder_t * const decoder, |
1274 motion_t * const motion, | |
1275 mpeg2_mc_fct * const * const table) | |
1 | 1276 { |
9852 | 1277 #define bit_buf (decoder->bitstream_buf) |
1278 #define bits (decoder->bitstream_bits) | |
1279 #define bit_ptr (decoder->bitstream_ptr) | |
1 | 1280 int motion_x, motion_y; |
9852 | 1281 uint8_t ** ref_field; |
1282 unsigned int pos_x, pos_y, xy_half, offset; | |
1 | 1283 |
1284 NEEDBITS (bit_buf, bits, bit_ptr); | |
9852 | 1285 ref_field = motion->ref2[UBITS (bit_buf, 1)]; |
1 | 1286 DUMPBITS (bit_buf, bits, 1); |
1287 | |
9852 | 1288 motion_x = motion->pmv[0][0] + get_motion_delta (decoder, |
36 | 1289 motion->f_code[0]); |
1 | 1290 motion_x = bound_motion_vector (motion_x, motion->f_code[0]); |
1291 motion->pmv[1][0] = motion->pmv[0][0] = motion_x; | |
1292 | |
1293 NEEDBITS (bit_buf, bits, bit_ptr); | |
9852 | 1294 motion_y = motion->pmv[0][1] + get_motion_delta (decoder, |
36 | 1295 motion->f_code[1]); |
1 | 1296 motion_y = bound_motion_vector (motion_y, motion->f_code[1]); |
1297 motion->pmv[1][1] = motion->pmv[0][1] = motion_y; | |
1298 | |
9852 | 1299 MOTION (table, ref_field, motion_x, motion_y, 16, 0); |
1 | 1300 #undef bit_buf |
1301 #undef bits | |
1302 #undef bit_ptr | |
1303 } | |
1304 | |
9852 | 1305 static void motion_fi_16x8 (decoder_t * const decoder, motion_t * const motion, |
1306 mpeg2_mc_fct * const * const table) | |
1 | 1307 { |
9852 | 1308 #define bit_buf (decoder->bitstream_buf) |
1309 #define bits (decoder->bitstream_bits) | |
1310 #define bit_ptr (decoder->bitstream_ptr) | |
1 | 1311 int motion_x, motion_y; |
9852 | 1312 uint8_t ** ref_field; |
1313 unsigned int pos_x, pos_y, xy_half, offset; | |
1 | 1314 |
1315 NEEDBITS (bit_buf, bits, bit_ptr); | |
9852 | 1316 ref_field = motion->ref2[UBITS (bit_buf, 1)]; |
1 | 1317 DUMPBITS (bit_buf, bits, 1); |
1318 | |
9852 | 1319 motion_x = motion->pmv[0][0] + get_motion_delta (decoder, |
36 | 1320 motion->f_code[0]); |
1 | 1321 motion_x = bound_motion_vector (motion_x, motion->f_code[0]); |
1322 motion->pmv[0][0] = motion_x; | |
1323 | |
1324 NEEDBITS (bit_buf, bits, bit_ptr); | |
9852 | 1325 motion_y = motion->pmv[0][1] + get_motion_delta (decoder, |
36 | 1326 motion->f_code[1]); |
1 | 1327 motion_y = bound_motion_vector (motion_y, motion->f_code[1]); |
1328 motion->pmv[0][1] = motion_y; | |
1329 | |
9852 | 1330 MOTION (table, ref_field, motion_x, motion_y, 8, 0); |
1 | 1331 |
1332 NEEDBITS (bit_buf, bits, bit_ptr); | |
9852 | 1333 ref_field = motion->ref2[UBITS (bit_buf, 1)]; |
1 | 1334 DUMPBITS (bit_buf, bits, 1); |
1335 | |
9852 | 1336 motion_x = motion->pmv[1][0] + get_motion_delta (decoder, |
36 | 1337 motion->f_code[0]); |
1 | 1338 motion_x = bound_motion_vector (motion_x, motion->f_code[0]); |
1339 motion->pmv[1][0] = motion_x; | |
1340 | |
1341 NEEDBITS (bit_buf, bits, bit_ptr); | |
9852 | 1342 motion_y = motion->pmv[1][1] + get_motion_delta (decoder, |
36 | 1343 motion->f_code[1]); |
1 | 1344 motion_y = bound_motion_vector (motion_y, motion->f_code[1]); |
1345 motion->pmv[1][1] = motion_y; | |
1346 | |
9852 | 1347 MOTION (table, ref_field, motion_x, motion_y, 8, 8); |
1 | 1348 #undef bit_buf |
1349 #undef bits | |
1350 #undef bit_ptr | |
1351 } | |
1352 | |
9852 | 1353 static void motion_fi_dmv (decoder_t * const decoder, motion_t * const motion, |
1354 mpeg2_mc_fct * const * const table) | |
1 | 1355 { |
9852 | 1356 #define bit_buf (decoder->bitstream_buf) |
1357 #define bits (decoder->bitstream_bits) | |
1358 #define bit_ptr (decoder->bitstream_ptr) | |
1359 int motion_x, motion_y, other_x, other_y; | |
1360 unsigned int pos_x, pos_y, xy_half, offset; | |
1 | 1361 |
1362 NEEDBITS (bit_buf, bits, bit_ptr); | |
9852 | 1363 motion_x = motion->pmv[0][0] + get_motion_delta (decoder, |
36 | 1364 motion->f_code[0]); |
1 | 1365 motion_x = bound_motion_vector (motion_x, motion->f_code[0]); |
1366 motion->pmv[1][0] = motion->pmv[0][0] = motion_x; | |
9852 | 1367 NEEDBITS (bit_buf, bits, bit_ptr); |
1368 other_x = ((motion_x + (motion_x > 0)) >> 1) + get_dmv (decoder); | |
1 | 1369 |
9852 | 1370 motion_y = motion->pmv[0][1] + get_motion_delta (decoder, |
36 | 1371 motion->f_code[1]); |
1 | 1372 motion_y = bound_motion_vector (motion_y, motion->f_code[1]); |
1373 motion->pmv[1][1] = motion->pmv[0][1] = motion_y; | |
9852 | 1374 other_y = (((motion_y + (motion_y > 0)) >> 1) + get_dmv (decoder) + |
1375 decoder->dmv_offset); | |
1 | 1376 |
9852 | 1377 MOTION (mpeg2_mc.put, motion->ref[0], motion_x, motion_y, 16, 0); |
1378 MOTION (mpeg2_mc.avg, motion->ref[1], other_x, other_y, 16, 0); | |
1 | 1379 #undef bit_buf |
1380 #undef bits | |
1381 #undef bit_ptr | |
1382 } | |
1383 | |
9852 | 1384 static void motion_fi_conceal (decoder_t * const decoder) |
1 | 1385 { |
9852 | 1386 #define bit_buf (decoder->bitstream_buf) |
1387 #define bits (decoder->bitstream_bits) | |
1388 #define bit_ptr (decoder->bitstream_ptr) | |
1 | 1389 int tmp; |
1390 | |
1391 NEEDBITS (bit_buf, bits, bit_ptr); | |
36 | 1392 DUMPBITS (bit_buf, bits, 1); /* remove field_select */ |
1 | 1393 |
9852 | 1394 tmp = (decoder->f_motion.pmv[0][0] + |
1395 get_motion_delta (decoder, decoder->f_motion.f_code[0])); | |
1396 tmp = bound_motion_vector (tmp, decoder->f_motion.f_code[0]); | |
1397 decoder->f_motion.pmv[1][0] = decoder->f_motion.pmv[0][0] = tmp; | |
1 | 1398 |
1399 NEEDBITS (bit_buf, bits, bit_ptr); | |
9852 | 1400 tmp = (decoder->f_motion.pmv[0][1] + |
1401 get_motion_delta (decoder, decoder->f_motion.f_code[1])); | |
1402 tmp = bound_motion_vector (tmp, decoder->f_motion.f_code[1]); | |
1403 decoder->f_motion.pmv[1][1] = decoder->f_motion.pmv[0][1] = tmp; | |
1 | 1404 |
36 | 1405 DUMPBITS (bit_buf, bits, 1); /* remove marker_bit */ |
1 | 1406 #undef bit_buf |
1407 #undef bits | |
1408 #undef bit_ptr | |
1409 } | |
1410 | |
9852 | 1411 #define MOTION_CALL(routine,direction) \ |
1412 do { \ | |
1413 if ((direction) & MACROBLOCK_MOTION_FORWARD) \ | |
1414 routine (decoder, &(decoder->f_motion), mpeg2_mc.put); \ | |
1415 if ((direction) & MACROBLOCK_MOTION_BACKWARD) \ | |
1416 routine (decoder, &(decoder->b_motion), \ | |
1417 ((direction) & MACROBLOCK_MOTION_FORWARD ? \ | |
1418 mpeg2_mc.avg : mpeg2_mc.put)); \ | |
1 | 1419 } while (0) |
1420 | |
9852 | 1421 #define NEXT_MACROBLOCK \ |
36 | 1422 do { \ |
9855 | 1423 if(decoder->quant_store) \ |
1424 decoder->quant_store[decoder->quant_stride*(decoder->v_offset>>4) \ | |
1425 +(decoder->offset>>4)] = decoder->quantizer_scale; \ | |
9852 | 1426 decoder->offset += 16; \ |
1427 if (decoder->offset == decoder->width) { \ | |
36 | 1428 do { /* just so we can use the break statement */ \ |
9852 | 1429 if (decoder->convert) { \ |
1430 decoder->convert (decoder->fbuf_id, decoder->dest, \ | |
1431 decoder->v_offset); \ | |
1432 if (decoder->coding_type == B_TYPE) \ | |
36 | 1433 break; \ |
1434 } \ | |
9852 | 1435 decoder->dest[0] += 16 * decoder->stride; \ |
1436 decoder->dest[1] += 4 * decoder->stride; \ | |
1437 decoder->dest[2] += 4 * decoder->stride; \ | |
36 | 1438 } while (0); \ |
9852 | 1439 decoder->v_offset += 16; \ |
1440 if (decoder->v_offset > decoder->limit_y) { \ | |
1441 if (mpeg2_cpu_state_restore) \ | |
1442 mpeg2_cpu_state_restore (&cpu_state); \ | |
1443 return; \ | |
1444 } \ | |
1445 decoder->offset = 0; \ | |
36 | 1446 } \ |
1 | 1447 } while (0) |
1448 | |
9852 | 1449 void mpeg2_init_fbuf (decoder_t * decoder, uint8_t * current_fbuf[3], |
1450 uint8_t * forward_fbuf[3], uint8_t * backward_fbuf[3]) | |
1 | 1451 { |
9852 | 1452 int offset, stride, height, bottom_field; |
1453 | |
1454 stride = decoder->width; | |
1455 bottom_field = (decoder->picture_structure == BOTTOM_FIELD); | |
1456 offset = bottom_field ? stride : 0; | |
1457 height = decoder->height; | |
1 | 1458 |
9852 | 1459 decoder->picture_dest[0] = current_fbuf[0] + offset; |
1460 decoder->picture_dest[1] = current_fbuf[1] + (offset >> 1); | |
1461 decoder->picture_dest[2] = current_fbuf[2] + (offset >> 1); | |
1462 | |
1463 decoder->f_motion.ref[0][0] = forward_fbuf[0] + offset; | |
1464 decoder->f_motion.ref[0][1] = forward_fbuf[1] + (offset >> 1); | |
1465 decoder->f_motion.ref[0][2] = forward_fbuf[2] + (offset >> 1); | |
1466 | |
1467 decoder->b_motion.ref[0][0] = backward_fbuf[0] + offset; | |
1468 decoder->b_motion.ref[0][1] = backward_fbuf[1] + (offset >> 1); | |
1469 decoder->b_motion.ref[0][2] = backward_fbuf[2] + (offset >> 1); | |
1 | 1470 |
9852 | 1471 if (decoder->picture_structure != FRAME_PICTURE) { |
1472 decoder->dmv_offset = bottom_field ? 1 : -1; | |
1473 decoder->f_motion.ref2[0] = decoder->f_motion.ref[bottom_field]; | |
1474 decoder->f_motion.ref2[1] = decoder->f_motion.ref[!bottom_field]; | |
1475 decoder->b_motion.ref2[0] = decoder->b_motion.ref[bottom_field]; | |
1476 decoder->b_motion.ref2[1] = decoder->b_motion.ref[!bottom_field]; | |
1477 offset = stride - offset; | |
1478 | |
1479 if (decoder->second_field && (decoder->coding_type != B_TYPE)) | |
1480 forward_fbuf = current_fbuf; | |
49 | 1481 |
9852 | 1482 decoder->f_motion.ref[1][0] = forward_fbuf[0] + offset; |
1483 decoder->f_motion.ref[1][1] = forward_fbuf[1] + (offset >> 1); | |
1484 decoder->f_motion.ref[1][2] = forward_fbuf[2] + (offset >> 1); | |
49 | 1485 |
9852 | 1486 decoder->b_motion.ref[1][0] = backward_fbuf[0] + offset; |
1487 decoder->b_motion.ref[1][1] = backward_fbuf[1] + (offset >> 1); | |
1488 decoder->b_motion.ref[1][2] = backward_fbuf[2] + (offset >> 1); | |
1489 | |
1490 stride <<= 1; | |
1491 height >>= 1; | |
1 | 1492 } |
1493 | |
9852 | 1494 decoder->stride = stride; |
1495 decoder->uv_stride = stride >> 1; | |
1496 decoder->limit_x = 2 * decoder->width - 32; | |
1497 decoder->limit_y_16 = 2 * height - 32; | |
1498 decoder->limit_y_8 = 2 * height - 16; | |
1499 decoder->limit_y = height - 16; | |
1500 } | |
49 | 1501 |
9852 | 1502 static inline int slice_init (decoder_t * const decoder, int code) |
1503 { | |
1504 #define bit_buf (decoder->bitstream_buf) | |
1505 #define bits (decoder->bitstream_bits) | |
1506 #define bit_ptr (decoder->bitstream_ptr) | |
1507 int offset; | |
1508 const MBAtab * mba; | |
49 | 1509 |
9852 | 1510 decoder->dc_dct_pred[0] = decoder->dc_dct_pred[1] = |
1511 decoder->dc_dct_pred[2] = 128 << decoder->intra_dc_precision; | |
1 | 1512 |
9852 | 1513 decoder->f_motion.pmv[0][0] = decoder->f_motion.pmv[0][1] = 0; |
1514 decoder->f_motion.pmv[1][0] = decoder->f_motion.pmv[1][1] = 0; | |
1515 decoder->b_motion.pmv[0][0] = decoder->b_motion.pmv[0][1] = 0; | |
1516 decoder->b_motion.pmv[1][0] = decoder->b_motion.pmv[1][1] = 0; | |
1 | 1517 |
9852 | 1518 if (decoder->vertical_position_extension) { |
1519 code += UBITS (bit_buf, 3) << 7; | |
1520 DUMPBITS (bit_buf, bits, 3); | |
1 | 1521 } |
9852 | 1522 decoder->v_offset = (code - 1) * 16; |
1523 offset = 0; | |
1524 if (!(decoder->convert) || decoder->coding_type != B_TYPE) | |
1525 offset = (code - 1) * decoder->stride * 4; | |
1 | 1526 |
9852 | 1527 decoder->dest[0] = decoder->picture_dest[0] + offset * 4; |
1528 decoder->dest[1] = decoder->picture_dest[1] + offset; | |
1529 decoder->dest[2] = decoder->picture_dest[2] + offset; | |
1 | 1530 |
9852 | 1531 decoder->quantizer_scale = get_quantizer_scale (decoder); |
1 | 1532 |
36 | 1533 /* ignore intra_slice and all the extra data */ |
1 | 1534 while (bit_buf & 0x80000000) { |
1535 DUMPBITS (bit_buf, bits, 9); | |
1536 NEEDBITS (bit_buf, bits, bit_ptr); | |
1537 } | |
9852 | 1538 |
1539 /* decode initial macroblock address increment */ | |
1540 offset = 0; | |
1541 while (1) { | |
1542 if (bit_buf >= 0x08000000) { | |
1543 mba = MBA_5 + (UBITS (bit_buf, 6) - 2); | |
1544 break; | |
1545 } else if (bit_buf >= 0x01800000) { | |
1546 mba = MBA_11 + (UBITS (bit_buf, 12) - 24); | |
1547 break; | |
1548 } else switch (UBITS (bit_buf, 12)) { | |
1549 case 8: /* macroblock_escape */ | |
1550 offset += 33; | |
1551 DUMPBITS (bit_buf, bits, 11); | |
1552 NEEDBITS (bit_buf, bits, bit_ptr); | |
1553 continue; | |
1554 case 15: /* macroblock_stuffing (MPEG1 only) */ | |
1555 bit_buf &= 0xfffff; | |
1556 DUMPBITS (bit_buf, bits, 11); | |
1557 NEEDBITS (bit_buf, bits, bit_ptr); | |
1558 continue; | |
1559 default: /* error */ | |
1560 return 1; | |
1561 } | |
1562 } | |
1563 DUMPBITS (bit_buf, bits, mba->len + 1); | |
1564 decoder->offset = (offset + mba->mba) << 4; | |
1 | 1565 |
9852 | 1566 while (decoder->offset - decoder->width >= 0) { |
1567 decoder->offset -= decoder->width; | |
1568 if (!(decoder->convert) || decoder->coding_type != B_TYPE) { | |
1569 decoder->dest[0] += 16 * decoder->stride; | |
1570 decoder->dest[1] += 4 * decoder->stride; | |
1571 decoder->dest[2] += 4 * decoder->stride; | |
1572 } | |
1573 decoder->v_offset += 16; | |
1574 } | |
1575 if (decoder->v_offset > decoder->limit_y) | |
1576 return 1; | |
1577 | |
1578 return 0; | |
1579 #undef bit_buf | |
1580 #undef bits | |
1581 #undef bit_ptr | |
1582 } | |
1583 | |
1584 void mpeg2_slice (decoder_t * const decoder, const int code, | |
1585 const uint8_t * const buffer) | |
1586 { | |
1587 #define bit_buf (decoder->bitstream_buf) | |
1588 #define bits (decoder->bitstream_bits) | |
1589 #define bit_ptr (decoder->bitstream_ptr) | |
1590 cpu_state_t cpu_state; | |
1591 | |
1592 bitstream_init (decoder, buffer); | |
1593 | |
1594 if (slice_init (decoder, code)) | |
1595 return; | |
1596 | |
1597 if (mpeg2_cpu_state_save) | |
1598 mpeg2_cpu_state_save (&cpu_state); | |
1 | 1599 |
1600 while (1) { | |
9852 | 1601 int macroblock_modes; |
1602 int mba_inc; | |
1603 const MBAtab * mba; | |
1604 | |
1 | 1605 NEEDBITS (bit_buf, bits, bit_ptr); |
1606 | |
9852 | 1607 macroblock_modes = get_macroblock_modes (decoder); |
1 | 1608 |
36 | 1609 /* maybe integrate MACROBLOCK_QUANT test into get_macroblock_modes ? */ |
1 | 1610 if (macroblock_modes & MACROBLOCK_QUANT) |
9852 | 1611 decoder->quantizer_scale = get_quantizer_scale (decoder); |
1 | 1612 |
1613 if (macroblock_modes & MACROBLOCK_INTRA) { | |
1614 | |
1615 int DCT_offset, DCT_stride; | |
9852 | 1616 int offset; |
1617 uint8_t * dest_y; | |
1 | 1618 |
9852 | 1619 if (decoder->concealment_motion_vectors) { |
1620 if (decoder->picture_structure == FRAME_PICTURE) | |
1621 motion_fr_conceal (decoder); | |
1 | 1622 else |
9852 | 1623 motion_fi_conceal (decoder); |
1 | 1624 } else { |
9852 | 1625 decoder->f_motion.pmv[0][0] = decoder->f_motion.pmv[0][1] = 0; |
1626 decoder->f_motion.pmv[1][0] = decoder->f_motion.pmv[1][1] = 0; | |
1627 decoder->b_motion.pmv[0][0] = decoder->b_motion.pmv[0][1] = 0; | |
1628 decoder->b_motion.pmv[1][0] = decoder->b_motion.pmv[1][1] = 0; | |
1 | 1629 } |
1630 | |
1631 if (macroblock_modes & DCT_TYPE_INTERLACED) { | |
9852 | 1632 DCT_offset = decoder->stride; |
1633 DCT_stride = decoder->stride * 2; | |
1 | 1634 } else { |
9852 | 1635 DCT_offset = decoder->stride * 8; |
1636 DCT_stride = decoder->stride; | |
1 | 1637 } |
1638 | |
9852 | 1639 offset = decoder->offset; |
1640 dest_y = decoder->dest[0] + offset; | |
1641 slice_intra_DCT (decoder, 0, dest_y, DCT_stride); | |
1642 slice_intra_DCT (decoder, 0, dest_y + 8, DCT_stride); | |
1643 slice_intra_DCT (decoder, 0, dest_y + DCT_offset, DCT_stride); | |
1644 slice_intra_DCT (decoder, 0, dest_y + DCT_offset + 8, DCT_stride); | |
1645 slice_intra_DCT (decoder, 1, decoder->dest[1] + (offset >> 1), | |
1646 decoder->uv_stride); | |
1647 slice_intra_DCT (decoder, 2, decoder->dest[2] + (offset >> 1), | |
1648 decoder->uv_stride); | |
1 | 1649 |
9852 | 1650 if (decoder->coding_type == D_TYPE) { |
1 | 1651 NEEDBITS (bit_buf, bits, bit_ptr); |
1652 DUMPBITS (bit_buf, bits, 1); | |
1653 } | |
1654 } else { | |
1655 | |
9852 | 1656 if (decoder->picture_structure == FRAME_PICTURE) |
1 | 1657 switch (macroblock_modes & MOTION_TYPE_MASK) { |
1658 case MC_FRAME: | |
9852 | 1659 if (decoder->mpeg1) |
1660 MOTION_CALL (motion_mp1, macroblock_modes); | |
1661 else | |
1662 MOTION_CALL (motion_fr_frame, macroblock_modes); | |
1 | 1663 break; |
1664 | |
1665 case MC_FIELD: | |
9852 | 1666 MOTION_CALL (motion_fr_field, macroblock_modes); |
1 | 1667 break; |
1668 | |
1669 case MC_DMV: | |
9852 | 1670 MOTION_CALL (motion_fr_dmv, MACROBLOCK_MOTION_FORWARD); |
1 | 1671 break; |
1672 | |
1673 case 0: | |
36 | 1674 /* non-intra mb without forward mv in a P picture */ |
9852 | 1675 decoder->f_motion.pmv[0][0] = 0; |
1676 decoder->f_motion.pmv[0][1] = 0; | |
1677 decoder->f_motion.pmv[1][0] = 0; | |
1678 decoder->f_motion.pmv[1][1] = 0; | |
1679 MOTION_CALL (motion_zero, MACROBLOCK_MOTION_FORWARD); | |
1 | 1680 break; |
1681 } | |
1682 else | |
1683 switch (macroblock_modes & MOTION_TYPE_MASK) { | |
1684 case MC_FIELD: | |
9852 | 1685 MOTION_CALL (motion_fi_field, macroblock_modes); |
1 | 1686 break; |
1687 | |
1688 case MC_16X8: | |
9852 | 1689 MOTION_CALL (motion_fi_16x8, macroblock_modes); |
1 | 1690 break; |
1691 | |
1692 case MC_DMV: | |
9852 | 1693 MOTION_CALL (motion_fi_dmv, MACROBLOCK_MOTION_FORWARD); |
1 | 1694 break; |
1695 | |
1696 case 0: | |
36 | 1697 /* non-intra mb without forward mv in a P picture */ |
9852 | 1698 decoder->f_motion.pmv[0][0] = 0; |
1699 decoder->f_motion.pmv[0][1] = 0; | |
1700 decoder->f_motion.pmv[1][0] = 0; | |
1701 decoder->f_motion.pmv[1][1] = 0; | |
1702 MOTION_CALL (motion_zero, MACROBLOCK_MOTION_FORWARD); | |
1 | 1703 break; |
1704 } | |
1705 | |
1706 if (macroblock_modes & MACROBLOCK_PATTERN) { | |
1707 int coded_block_pattern; | |
1708 int DCT_offset, DCT_stride; | |
9852 | 1709 int offset; |
1710 uint8_t * dest_y; | |
1 | 1711 |
1712 if (macroblock_modes & DCT_TYPE_INTERLACED) { | |
9852 | 1713 DCT_offset = decoder->stride; |
1714 DCT_stride = decoder->stride * 2; | |
1 | 1715 } else { |
9852 | 1716 DCT_offset = decoder->stride * 8; |
1717 DCT_stride = decoder->stride; | |
1 | 1718 } |
1719 | |
9852 | 1720 coded_block_pattern = get_coded_block_pattern (decoder); |
1 | 1721 |
9852 | 1722 offset = decoder->offset; |
1723 dest_y = decoder->dest[0] + offset; | |
1 | 1724 if (coded_block_pattern & 0x20) |
9852 | 1725 slice_non_intra_DCT (decoder, dest_y, DCT_stride); |
1 | 1726 if (coded_block_pattern & 0x10) |
9852 | 1727 slice_non_intra_DCT (decoder, dest_y + 8, DCT_stride); |
1 | 1728 if (coded_block_pattern & 0x08) |
9852 | 1729 slice_non_intra_DCT (decoder, dest_y + DCT_offset, |
1 | 1730 DCT_stride); |
1731 if (coded_block_pattern & 0x04) | |
9852 | 1732 slice_non_intra_DCT (decoder, dest_y + DCT_offset + 8, |
1 | 1733 DCT_stride); |
1734 if (coded_block_pattern & 0x2) | |
9852 | 1735 slice_non_intra_DCT (decoder, |
1736 decoder->dest[1] + (offset >> 1), | |
1737 decoder->uv_stride); | |
1 | 1738 if (coded_block_pattern & 0x1) |
9852 | 1739 slice_non_intra_DCT (decoder, |
1740 decoder->dest[2] + (offset >> 1), | |
1741 decoder->uv_stride); | |
1 | 1742 } |
1743 | |
9852 | 1744 decoder->dc_dct_pred[0] = decoder->dc_dct_pred[1] = |
1745 decoder->dc_dct_pred[2] = 128 << decoder->intra_dc_precision; | |
1 | 1746 } |
1747 | |
9852 | 1748 NEXT_MACROBLOCK; |
1 | 1749 |
1750 NEEDBITS (bit_buf, bits, bit_ptr); | |
9852 | 1751 mba_inc = 0; |
1752 while (1) { | |
1753 if (bit_buf >= 0x10000000) { | |
1754 mba = MBA_5 + (UBITS (bit_buf, 5) - 2); | |
1755 break; | |
1756 } else if (bit_buf >= 0x03000000) { | |
1757 mba = MBA_11 + (UBITS (bit_buf, 11) - 24); | |
1758 break; | |
1759 } else switch (UBITS (bit_buf, 11)) { | |
1760 case 8: /* macroblock_escape */ | |
1761 mba_inc += 33; | |
1762 /* pass through */ | |
1763 case 15: /* macroblock_stuffing (MPEG1 only) */ | |
1764 DUMPBITS (bit_buf, bits, 11); | |
1765 NEEDBITS (bit_buf, bits, bit_ptr); | |
142 | 1766 continue; |
9852 | 1767 default: /* end of slice, or error */ |
1768 if (mpeg2_cpu_state_restore) | |
1769 mpeg2_cpu_state_restore (&cpu_state); | |
1770 return; | |
1771 } | |
1772 } | |
1773 DUMPBITS (bit_buf, bits, mba->len); | |
1774 mba_inc += mba->mba; | |
1 | 1775 |
9852 | 1776 if (mba_inc) { |
1777 decoder->dc_dct_pred[0] = decoder->dc_dct_pred[1] = | |
1778 decoder->dc_dct_pred[2] = 128 << decoder->intra_dc_precision; | |
1 | 1779 |
9852 | 1780 if (decoder->coding_type == P_TYPE) { |
1781 decoder->f_motion.pmv[0][0] = decoder->f_motion.pmv[0][1] = 0; | |
1782 decoder->f_motion.pmv[1][0] = decoder->f_motion.pmv[1][1] = 0; | |
1 | 1783 |
1784 do { | |
9852 | 1785 MOTION_CALL (motion_zero, MACROBLOCK_MOTION_FORWARD); |
1786 NEXT_MACROBLOCK; | |
1 | 1787 } while (--mba_inc); |
1788 } else { | |
1789 do { | |
9852 | 1790 MOTION_CALL (motion_reuse, macroblock_modes); |
1791 NEXT_MACROBLOCK; | |
1 | 1792 } while (--mba_inc); |
1793 } | |
1794 } | |
1795 } | |
1796 #undef bit_buf | |
1797 #undef bits | |
1798 #undef bit_ptr | |
1799 } |