Cosmetics.
[mplayer/glamo.git] / libmpeg2 / slice.c
blob327612e0e49e815c95fc35f93aa6e0036930cf19
1 /*
2 * slice.c
3 * Copyright (C) 2000-2002 Michel Lespinasse <walken@zoy.org>
4 * Copyright (C) 1999-2000 Aaron Holtzman <aholtzma@ess.engr.uvic.ca>
6 * This file is part of mpeg2dec, a free MPEG-2 video stream decoder.
7 * See http://libmpeg2.sourceforge.net/ for updates.
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.
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.
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
24 #include "config.h"
26 #include <inttypes.h>
28 #include "mpeg2.h"
29 #include "mpeg2_internal.h"
30 #include "attributes.h"
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);
39 #include "vlc.h"
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
48 static inline int get_macroblock_modes (decoder_t * const decoder)
50 #define bit_buf (decoder->bitstream_buf)
51 #define bits (decoder->bitstream_bits)
52 #define bit_ptr (decoder->bitstream_ptr)
53 int macroblock_modes;
54 const MBtab * tab;
56 switch (decoder->coding_type) {
57 case I_TYPE:
59 tab = MB_I + UBITS (bit_buf, 1);
60 DUMPBITS (bit_buf, bits, tab->len);
61 macroblock_modes = tab->modes;
63 if ((! (decoder->frame_pred_frame_dct)) &&
64 (decoder->picture_structure == FRAME_PICTURE)) {
65 macroblock_modes |= UBITS (bit_buf, 1) * DCT_TYPE_INTERLACED;
66 DUMPBITS (bit_buf, bits, 1);
69 return macroblock_modes;
71 case P_TYPE:
73 tab = MB_P + UBITS (bit_buf, 5);
74 DUMPBITS (bit_buf, bits, tab->len);
75 macroblock_modes = tab->modes;
77 if (decoder->picture_structure != FRAME_PICTURE) {
78 if (macroblock_modes & MACROBLOCK_MOTION_FORWARD) {
79 macroblock_modes |= UBITS (bit_buf, 2) * MOTION_TYPE_BASE;
80 DUMPBITS (bit_buf, bits, 2);
82 return macroblock_modes;
83 } else if (decoder->frame_pred_frame_dct) {
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);
92 if (macroblock_modes & (MACROBLOCK_INTRA | MACROBLOCK_PATTERN)) {
93 macroblock_modes |= UBITS (bit_buf, 1) * DCT_TYPE_INTERLACED;
94 DUMPBITS (bit_buf, bits, 1);
96 return macroblock_modes;
99 case B_TYPE:
101 tab = MB_B + UBITS (bit_buf, 6);
102 DUMPBITS (bit_buf, bits, tab->len);
103 macroblock_modes = tab->modes;
105 if (decoder->picture_structure != FRAME_PICTURE) {
106 if (! (macroblock_modes & MACROBLOCK_INTRA)) {
107 macroblock_modes |= UBITS (bit_buf, 2) * MOTION_TYPE_BASE;
108 DUMPBITS (bit_buf, bits, 2);
110 return macroblock_modes;
111 } else if (decoder->frame_pred_frame_dct) {
112 /* if (! (macroblock_modes & MACROBLOCK_INTRA)) */
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);
125 return macroblock_modes;
128 case D_TYPE:
130 DUMPBITS (bit_buf, bits, 1);
131 return MACROBLOCK_INTRA;
133 default:
134 return 0;
136 #undef bit_buf
137 #undef bits
138 #undef bit_ptr
141 static inline int get_quantizer_scale (decoder_t * const decoder)
143 #define bit_buf (decoder->bitstream_buf)
144 #define bits (decoder->bitstream_bits)
145 #define bit_ptr (decoder->bitstream_ptr)
147 int quantizer_scale_code;
149 quantizer_scale_code = UBITS (bit_buf, 5);
150 DUMPBITS (bit_buf, bits, 5);
152 if (decoder->q_scale_type)
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
161 static inline int get_motion_delta (decoder_t * const decoder,
162 const int f_code)
164 #define bit_buf (decoder->bitstream_buf)
165 #define bits (decoder->bitstream_bits)
166 #define bit_ptr (decoder->bitstream_ptr)
168 int delta;
169 int sign;
170 const MVtab * tab;
172 if (bit_buf & 0x80000000) {
173 DUMPBITS (bit_buf, bits, 1);
174 return 0;
175 } else if (bit_buf >= 0x0c000000) {
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;
182 sign = SBITS (bit_buf, 1);
183 bit_buf <<= 1;
185 if (f_code)
186 delta += UBITS (bit_buf, f_code);
187 bit_buf <<= f_code;
189 return (delta ^ sign) - sign;
191 } else {
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;
198 sign = SBITS (bit_buf, 1);
199 bit_buf <<= 1;
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);
207 return (delta ^ sign) - sign;
210 #undef bit_buf
211 #undef bits
212 #undef bit_ptr
215 static inline int bound_motion_vector (const int vector, const int f_code)
217 #if 0
218 unsigned int limit;
219 int sign;
221 limit = 16 << f_code;
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;
229 #else
230 return ((int32_t)vector << (27 - f_code)) >> (27 - f_code);
231 #endif
234 static inline int get_dmv (decoder_t * const decoder)
236 #define bit_buf (decoder->bitstream_buf)
237 #define bits (decoder->bitstream_bits)
238 #define bit_ptr (decoder->bitstream_ptr)
240 const DMVtab * tab;
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
250 static inline int get_coded_block_pattern (decoder_t * const decoder)
252 #define bit_buf (decoder->bitstream_buf)
253 #define bits (decoder->bitstream_bits)
254 #define bit_ptr (decoder->bitstream_ptr)
256 const CBPtab * tab;
258 NEEDBITS (bit_buf, bits, bit_ptr);
260 if (bit_buf >= 0x20000000) {
262 tab = CBP_7 + (UBITS (bit_buf, 7) - 16);
263 DUMPBITS (bit_buf, bits, tab->len);
264 return tab->cbp;
266 } else {
268 tab = CBP_9 + UBITS (bit_buf, 9);
269 DUMPBITS (bit_buf, bits, tab->len);
270 return tab->cbp;
273 #undef bit_buf
274 #undef bits
275 #undef bit_ptr
278 static inline int get_luma_dc_dct_diff (decoder_t * const decoder)
280 #define bit_buf (decoder->bitstream_buf)
281 #define bits (decoder->bitstream_bits)
282 #define bit_ptr (decoder->bitstream_ptr)
283 const DCtab * tab;
284 int size;
285 int dc_diff;
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;
301 } else {
302 tab = DC_long + (UBITS (bit_buf, 9) - 0x1e0);
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;
310 #undef bit_buf
311 #undef bits
312 #undef bit_ptr
315 static inline int get_chroma_dc_dct_diff (decoder_t * const decoder)
317 #define bit_buf (decoder->bitstream_buf)
318 #define bits (decoder->bitstream_bits)
319 #define bit_ptr (decoder->bitstream_ptr)
320 const DCtab * tab;
321 int size;
322 int dc_diff;
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;
338 } else {
339 tab = DC_long + (UBITS (bit_buf, 10) - 0x3e0);
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;
347 #undef bit_buf
348 #undef bits
349 #undef bit_ptr
352 #define SATURATE(val) \
353 do { \
354 if (unlikely ((uint32_t)(val + 2048) > 4095)) \
355 val = SBITS (val, 1) ^ 2047; \
356 } while (0)
358 static void get_intra_block_B14 (decoder_t * const decoder)
360 int i;
361 int j;
362 int val;
363 const uint8_t * scan = decoder->scan;
364 const uint8_t * quant_matrix = decoder->intra_quantizer_matrix;
365 int quantizer_scale = decoder->quantizer_scale;
366 int mismatch;
367 const DCTtab * tab;
368 uint32_t bit_buf;
369 int bits;
370 const uint8_t * bit_ptr;
371 int16_t * dest;
373 dest = decoder->DCTblock;
374 i = 0;
375 mismatch = ~dest[0];
377 bit_buf = decoder->bitstream_buf;
378 bits = decoder->bitstream_bits;
379 bit_ptr = decoder->bitstream_ptr;
381 NEEDBITS (bit_buf, bits, bit_ptr);
383 while (1) {
384 if (bit_buf >= 0x28000000) {
386 tab = DCT_B14AC_5 + (UBITS (bit_buf, 5) - 5);
388 i += tab->run;
389 if (i >= 64)
390 break; /* end of block */
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;
398 /* if (bitstream_get (1)) val = -val; */
399 val = (val ^ SBITS (bit_buf, 1)) - SBITS (bit_buf, 1);
401 SATURATE (val);
402 dest[j] = val;
403 mismatch ^= val;
405 bit_buf <<= 1;
406 NEEDBITS (bit_buf, bits, bit_ptr);
408 continue;
410 } else if (bit_buf >= 0x04000000) {
412 tab = DCT_B14_8 + (UBITS (bit_buf, 8) - 4);
414 i += tab->run;
415 if (i < 64)
416 goto normal_code;
418 /* escape code */
420 i += UBITS (bit_buf << 6, 6) - 64;
421 if (i >= 64)
422 break; /* illegal, check needed to avoid buffer overflow */
424 j = scan[i];
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;
431 SATURATE (val);
432 dest[j] = val;
433 mismatch ^= val;
435 DUMPBITS (bit_buf, bits, 12);
436 NEEDBITS (bit_buf, bits, bit_ptr);
438 continue;
440 } else if (bit_buf >= 0x02000000) {
441 tab = DCT_B14_10 + (UBITS (bit_buf, 10) - 8);
442 i += tab->run;
443 if (i < 64)
444 goto normal_code;
445 } else if (bit_buf >= 0x00800000) {
446 tab = DCT_13 + (UBITS (bit_buf, 13) - 16);
447 i += tab->run;
448 if (i < 64)
449 goto normal_code;
450 } else if (bit_buf >= 0x00200000) {
451 tab = DCT_15 + (UBITS (bit_buf, 15) - 16);
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;
463 break; /* illegal, check needed to avoid buffer overflow */
465 dest[63] ^= mismatch & 1;
466 DUMPBITS (bit_buf, bits, 2); /* dump end of block code */
467 decoder->bitstream_buf = bit_buf;
468 decoder->bitstream_bits = bits;
469 decoder->bitstream_ptr = bit_ptr;
472 static void get_intra_block_B15 (decoder_t * const decoder)
474 int i;
475 int j;
476 int val;
477 const uint8_t * scan = decoder->scan;
478 const uint8_t * quant_matrix = decoder->intra_quantizer_matrix;
479 int quantizer_scale = decoder->quantizer_scale;
480 int mismatch;
481 const DCTtab * tab;
482 uint32_t bit_buf;
483 int bits;
484 const uint8_t * bit_ptr;
485 int16_t * dest;
487 dest = decoder->DCTblock;
488 i = 0;
489 mismatch = ~dest[0];
491 bit_buf = decoder->bitstream_buf;
492 bits = decoder->bitstream_bits;
493 bit_ptr = decoder->bitstream_ptr;
495 NEEDBITS (bit_buf, bits, bit_ptr);
497 while (1) {
498 if (bit_buf >= 0x04000000) {
500 tab = DCT_B15_8 + (UBITS (bit_buf, 8) - 4);
502 i += tab->run;
503 if (i < 64) {
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;
511 /* if (bitstream_get (1)) val = -val; */
512 val = (val ^ SBITS (bit_buf, 1)) - SBITS (bit_buf, 1);
514 SATURATE (val);
515 dest[j] = val;
516 mismatch ^= val;
518 bit_buf <<= 1;
519 NEEDBITS (bit_buf, bits, bit_ptr);
521 continue;
523 } else {
525 /* end of block. I commented out this code because if we */
526 /* dont exit here we will still exit at the later test :) */
528 /* if (i >= 128) break; */ /* end of block */
530 /* escape code */
532 i += UBITS (bit_buf << 6, 6) - 64;
533 if (i >= 64)
534 break; /* illegal, check against buffer overflow */
536 j = scan[i];
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;
543 SATURATE (val);
544 dest[j] = val;
545 mismatch ^= val;
547 DUMPBITS (bit_buf, bits, 12);
548 NEEDBITS (bit_buf, bits, bit_ptr);
550 continue;
553 } else if (bit_buf >= 0x02000000) {
554 tab = DCT_B15_10 + (UBITS (bit_buf, 10) - 8);
555 i += tab->run;
556 if (i < 64)
557 goto normal_code;
558 } else if (bit_buf >= 0x00800000) {
559 tab = DCT_13 + (UBITS (bit_buf, 13) - 16);
560 i += tab->run;
561 if (i < 64)
562 goto normal_code;
563 } else if (bit_buf >= 0x00200000) {
564 tab = DCT_15 + (UBITS (bit_buf, 15) - 16);
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;
576 break; /* illegal, check needed to avoid buffer overflow */
578 dest[63] ^= mismatch & 1;
579 DUMPBITS (bit_buf, bits, 4); /* dump end of block code */
580 decoder->bitstream_buf = bit_buf;
581 decoder->bitstream_bits = bits;
582 decoder->bitstream_ptr = bit_ptr;
585 static int get_non_intra_block (decoder_t * const decoder)
587 int i;
588 int j;
589 int val;
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;
593 int mismatch;
594 const DCTtab * tab;
595 uint32_t bit_buf;
596 int bits;
597 const uint8_t * bit_ptr;
598 int16_t * dest;
600 i = -1;
601 mismatch = 1;
602 dest = decoder->DCTblock;
604 bit_buf = decoder->bitstream_buf;
605 bits = decoder->bitstream_bits;
606 bit_ptr = decoder->bitstream_ptr;
608 NEEDBITS (bit_buf, bits, bit_ptr);
609 if (bit_buf >= 0x28000000) {
610 tab = DCT_B14DC_5 + (UBITS (bit_buf, 5) - 5);
611 goto entry_1;
612 } else
613 goto entry_2;
615 while (1) {
616 if (bit_buf >= 0x28000000) {
618 tab = DCT_B14AC_5 + (UBITS (bit_buf, 5) - 5);
620 entry_1:
621 i += tab->run;
622 if (i >= 64)
623 break; /* end of block */
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;
631 /* if (bitstream_get (1)) val = -val; */
632 val = (val ^ SBITS (bit_buf, 1)) - SBITS (bit_buf, 1);
634 SATURATE (val);
635 dest[j] = val;
636 mismatch ^= val;
638 bit_buf <<= 1;
639 NEEDBITS (bit_buf, bits, bit_ptr);
641 continue;
645 entry_2:
646 if (bit_buf >= 0x04000000) {
648 tab = DCT_B14_8 + (UBITS (bit_buf, 8) - 4);
650 i += tab->run;
651 if (i < 64)
652 goto normal_code;
654 /* escape code */
656 i += UBITS (bit_buf << 6, 6) - 64;
657 if (i >= 64)
658 break; /* illegal, check needed to avoid buffer overflow */
660 j = scan[i];
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;
667 SATURATE (val);
668 dest[j] = val;
669 mismatch ^= val;
671 DUMPBITS (bit_buf, bits, 12);
672 NEEDBITS (bit_buf, bits, bit_ptr);
674 continue;
676 } else if (bit_buf >= 0x02000000) {
677 tab = DCT_B14_10 + (UBITS (bit_buf, 10) - 8);
678 i += tab->run;
679 if (i < 64)
680 goto normal_code;
681 } else if (bit_buf >= 0x00800000) {
682 tab = DCT_13 + (UBITS (bit_buf, 13) - 16);
683 i += tab->run;
684 if (i < 64)
685 goto normal_code;
686 } else if (bit_buf >= 0x00200000) {
687 tab = DCT_15 + (UBITS (bit_buf, 15) - 16);
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;
699 break; /* illegal, check needed to avoid buffer overflow */
701 dest[63] ^= mismatch & 1;
702 DUMPBITS (bit_buf, bits, 2); /* dump end of block code */
703 decoder->bitstream_buf = bit_buf;
704 decoder->bitstream_bits = bits;
705 decoder->bitstream_ptr = bit_ptr;
706 return i;
709 static void get_mpeg1_intra_block (decoder_t * const decoder)
711 int i;
712 int j;
713 int val;
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;
718 uint32_t bit_buf;
719 int bits;
720 const uint8_t * bit_ptr;
721 int16_t * dest;
723 i = 0;
724 dest = decoder->DCTblock;
726 bit_buf = decoder->bitstream_buf;
727 bits = decoder->bitstream_bits;
728 bit_ptr = decoder->bitstream_ptr;
730 NEEDBITS (bit_buf, bits, bit_ptr);
732 while (1) {
733 if (bit_buf >= 0x28000000) {
735 tab = DCT_B14AC_5 + (UBITS (bit_buf, 5) - 5);
737 i += tab->run;
738 if (i >= 64)
739 break; /* end of block */
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;
747 /* oddification */
748 val = (val - 1) | 1;
750 /* if (bitstream_get (1)) val = -val; */
751 val = (val ^ SBITS (bit_buf, 1)) - SBITS (bit_buf, 1);
753 SATURATE (val);
754 dest[j] = val;
756 bit_buf <<= 1;
757 NEEDBITS (bit_buf, bits, bit_ptr);
759 continue;
761 } else if (bit_buf >= 0x04000000) {
763 tab = DCT_B14_8 + (UBITS (bit_buf, 8) - 4);
765 i += tab->run;
766 if (i < 64)
767 goto normal_code;
769 /* escape code */
771 i += UBITS (bit_buf << 6, 6) - 64;
772 if (i >= 64)
773 break; /* illegal, check needed to avoid buffer overflow */
775 j = scan[i];
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;
784 val = (val * quantizer_scale * quant_matrix[j]) / 16;
786 /* oddification */
787 val = (val + ~SBITS (val, 1)) | 1;
789 SATURATE (val);
790 dest[j] = val;
792 DUMPBITS (bit_buf, bits, 8);
793 NEEDBITS (bit_buf, bits, bit_ptr);
795 continue;
797 } else if (bit_buf >= 0x02000000) {
798 tab = DCT_B14_10 + (UBITS (bit_buf, 10) - 8);
799 i += tab->run;
800 if (i < 64)
801 goto normal_code;
802 } else if (bit_buf >= 0x00800000) {
803 tab = DCT_13 + (UBITS (bit_buf, 13) - 16);
804 i += tab->run;
805 if (i < 64)
806 goto normal_code;
807 } else if (bit_buf >= 0x00200000) {
808 tab = DCT_15 + (UBITS (bit_buf, 15) - 16);
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;
820 break; /* illegal, check needed to avoid buffer overflow */
822 DUMPBITS (bit_buf, bits, 2); /* dump end of block code */
823 decoder->bitstream_buf = bit_buf;
824 decoder->bitstream_bits = bits;
825 decoder->bitstream_ptr = bit_ptr;
828 static int get_mpeg1_non_intra_block (decoder_t * const decoder)
830 int i;
831 int j;
832 int val;
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;
837 uint32_t bit_buf;
838 int bits;
839 const uint8_t * bit_ptr;
840 int16_t * dest;
842 i = -1;
843 dest = decoder->DCTblock;
845 bit_buf = decoder->bitstream_buf;
846 bits = decoder->bitstream_bits;
847 bit_ptr = decoder->bitstream_ptr;
849 NEEDBITS (bit_buf, bits, bit_ptr);
850 if (bit_buf >= 0x28000000) {
851 tab = DCT_B14DC_5 + (UBITS (bit_buf, 5) - 5);
852 goto entry_1;
853 } else
854 goto entry_2;
856 while (1) {
857 if (bit_buf >= 0x28000000) {
859 tab = DCT_B14AC_5 + (UBITS (bit_buf, 5) - 5);
861 entry_1:
862 i += tab->run;
863 if (i >= 64)
864 break; /* end of block */
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;
872 /* oddification */
873 val = (val - 1) | 1;
875 /* if (bitstream_get (1)) val = -val; */
876 val = (val ^ SBITS (bit_buf, 1)) - SBITS (bit_buf, 1);
878 SATURATE (val);
879 dest[j] = val;
881 bit_buf <<= 1;
882 NEEDBITS (bit_buf, bits, bit_ptr);
884 continue;
888 entry_2:
889 if (bit_buf >= 0x04000000) {
891 tab = DCT_B14_8 + (UBITS (bit_buf, 8) - 4);
893 i += tab->run;
894 if (i < 64)
895 goto normal_code;
897 /* escape code */
899 i += UBITS (bit_buf << 6, 6) - 64;
900 if (i >= 64)
901 break; /* illegal, check needed to avoid buffer overflow */
903 j = scan[i];
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;
912 val = 2 * (val + SBITS (val, 1)) + 1;
913 val = (val * quantizer_scale * quant_matrix[j]) / 32;
915 /* oddification */
916 val = (val + ~SBITS (val, 1)) | 1;
918 SATURATE (val);
919 dest[j] = val;
921 DUMPBITS (bit_buf, bits, 8);
922 NEEDBITS (bit_buf, bits, bit_ptr);
924 continue;
926 } else if (bit_buf >= 0x02000000) {
927 tab = DCT_B14_10 + (UBITS (bit_buf, 10) - 8);
928 i += tab->run;
929 if (i < 64)
930 goto normal_code;
931 } else if (bit_buf >= 0x00800000) {
932 tab = DCT_13 + (UBITS (bit_buf, 13) - 16);
933 i += tab->run;
934 if (i < 64)
935 goto normal_code;
936 } else if (bit_buf >= 0x00200000) {
937 tab = DCT_15 + (UBITS (bit_buf, 15) - 16);
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;
949 break; /* illegal, check needed to avoid buffer overflow */
951 DUMPBITS (bit_buf, bits, 2); /* dump end of block code */
952 decoder->bitstream_buf = bit_buf;
953 decoder->bitstream_bits = bits;
954 decoder->bitstream_ptr = bit_ptr;
955 return i;
958 static inline void slice_intra_DCT (decoder_t * const decoder, const int cc,
959 uint8_t * const dest, const int stride)
961 #define bit_buf (decoder->bitstream_buf)
962 #define bits (decoder->bitstream_bits)
963 #define bit_ptr (decoder->bitstream_ptr)
964 NEEDBITS (bit_buf, bits, bit_ptr);
965 /* Get the intra DC coefficient and inverse quantize it */
966 if (cc == 0)
967 decoder->dc_dct_pred[0] += get_luma_dc_dct_diff (decoder);
968 else
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);
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);
978 else
979 get_intra_block_B14 (decoder);
980 mpeg2_idct_copy (decoder->DCTblock, dest, stride);
981 #undef bit_buf
982 #undef bits
983 #undef bit_ptr
986 static inline void slice_non_intra_DCT (decoder_t * const decoder,
987 uint8_t * const dest, const int stride)
989 int last;
991 if (decoder->mpeg1)
992 last = get_mpeg1_non_intra_block (decoder);
993 else
994 last = get_non_intra_block (decoder);
995 mpeg2_idct_add (last, decoder->DCTblock, dest, stride);
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)
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)
1042 static void motion_mp1 (decoder_t * const decoder, motion_t * const motion,
1043 mpeg2_mc_fct * const * const table)
1045 #define bit_buf (decoder->bitstream_buf)
1046 #define bits (decoder->bitstream_bits)
1047 #define bit_ptr (decoder->bitstream_ptr)
1048 int motion_x, motion_y;
1049 unsigned int pos_x, pos_y, xy_half, offset;
1051 NEEDBITS (bit_buf, bits, bit_ptr);
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]);
1057 motion->pmv[0][0] = motion_x;
1059 NEEDBITS (bit_buf, bits, bit_ptr);
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]);
1065 motion->pmv[0][1] = motion_y;
1067 MOTION (table, motion->ref[0], motion_x, motion_y, 16, 0);
1068 #undef bit_buf
1069 #undef bits
1070 #undef bit_ptr
1073 static void motion_fr_frame (decoder_t * const decoder,
1074 motion_t * const motion,
1075 mpeg2_mc_fct * const * const table)
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;
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;
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;
1095 MOTION (table, motion->ref[0], motion_x, motion_y, 16, 0);
1096 #undef bit_buf
1097 #undef bits
1098 #undef bit_ptr
1101 static void motion_fr_field (decoder_t * const decoder,
1102 motion_t * const motion,
1103 mpeg2_mc_fct * const * const table)
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;
1111 NEEDBITS (bit_buf, bits, bit_ptr);
1112 field = UBITS (bit_buf, 1);
1113 DUMPBITS (bit_buf, bits, 1);
1115 motion_x = motion->pmv[0][0] + get_motion_delta (decoder,
1116 motion->f_code[0]);
1117 motion_x = bound_motion_vector (motion_x, motion->f_code[0]);
1118 motion->pmv[0][0] = motion_x;
1120 NEEDBITS (bit_buf, bits, bit_ptr);
1121 motion_y = (motion->pmv[0][1] >> 1) + get_motion_delta (decoder,
1122 motion->f_code[1]);
1123 /* motion_y = bound_motion_vector (motion_y, motion->f_code[1]); */
1124 motion->pmv[0][1] = motion_y << 1;
1126 MOTION_FIELD (table, motion->ref[0], motion_x, motion_y, 0, & ~1, field);
1128 NEEDBITS (bit_buf, bits, bit_ptr);
1129 field = UBITS (bit_buf, 1);
1130 DUMPBITS (bit_buf, bits, 1);
1132 motion_x = motion->pmv[1][0] + get_motion_delta (decoder,
1133 motion->f_code[0]);
1134 motion_x = bound_motion_vector (motion_x, motion->f_code[0]);
1135 motion->pmv[1][0] = motion_x;
1137 NEEDBITS (bit_buf, bits, bit_ptr);
1138 motion_y = (motion->pmv[1][1] >> 1) + get_motion_delta (decoder,
1139 motion->f_code[1]);
1140 /* motion_y = bound_motion_vector (motion_y, motion->f_code[1]); */
1141 motion->pmv[1][1] = motion_y << 1;
1143 MOTION_FIELD (table, motion->ref[0], motion_x, motion_y, 1, & ~1, field);
1144 #undef bit_buf
1145 #undef bits
1146 #undef bit_ptr
1149 static void motion_fr_dmv (decoder_t * const decoder, motion_t * const motion,
1150 mpeg2_mc_fct * const * const table)
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;
1158 NEEDBITS (bit_buf, bits, bit_ptr);
1159 motion_x = motion->pmv[0][0] + get_motion_delta (decoder,
1160 motion->f_code[0]);
1161 motion_x = bound_motion_vector (motion_x, motion->f_code[0]);
1162 motion->pmv[1][0] = motion->pmv[0][0] = motion_x;
1163 NEEDBITS (bit_buf, bits, bit_ptr);
1164 dmv_x = get_dmv (decoder);
1166 motion_y = (motion->pmv[0][1] >> 1) + get_motion_delta (decoder,
1167 motion->f_code[1]);
1168 /* motion_y = bound_motion_vector (motion_y, motion->f_code[1]); */
1169 motion->pmv[1][1] = motion->pmv[0][1] = motion_y << 1;
1170 dmv_y = get_dmv (decoder);
1172 m = decoder->top_field_first ? 1 : 3;
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;
1175 MOTION_FIELD (mpeg2_mc.put, motion->ref[0], other_x, other_y, 0, | 1, 0);
1177 m = decoder->top_field_first ? 3 : 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;
1180 MOTION_FIELD (mpeg2_mc.put, motion->ref[0], other_x, other_y, 1, & ~1, 0);
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);
1210 #undef bit_buf
1211 #undef bits
1212 #undef bit_ptr
1215 static inline void motion_reuse (const decoder_t * const decoder,
1216 const motion_t * const motion,
1217 mpeg2_mc_fct * const * const table)
1219 int motion_x, motion_y;
1220 unsigned int pos_x, pos_y, xy_half, offset;
1222 motion_x = motion->pmv[0][0];
1223 motion_y = motion->pmv[0][1];
1225 MOTION (table, motion->ref[0], motion_x, motion_y, 16, 0);
1228 static inline void motion_zero (const decoder_t * const decoder,
1229 const motion_t * const motion,
1230 mpeg2_mc_fct * const * const table)
1232 unsigned int offset;
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);
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);
1247 /* like motion_frame, but parsing without actual motion compensation */
1248 static void motion_fr_conceal (decoder_t * const decoder)
1250 #define bit_buf (decoder->bitstream_buf)
1251 #define bits (decoder->bitstream_bits)
1252 #define bit_ptr (decoder->bitstream_ptr)
1253 int tmp;
1255 NEEDBITS (bit_buf, bits, bit_ptr);
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;
1261 NEEDBITS (bit_buf, bits, bit_ptr);
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;
1267 DUMPBITS (bit_buf, bits, 1); /* remove marker_bit */
1268 #undef bit_buf
1269 #undef bits
1270 #undef bit_ptr
1273 static void motion_fi_field (decoder_t * const decoder,
1274 motion_t * const motion,
1275 mpeg2_mc_fct * const * const table)
1277 #define bit_buf (decoder->bitstream_buf)
1278 #define bits (decoder->bitstream_bits)
1279 #define bit_ptr (decoder->bitstream_ptr)
1280 int motion_x, motion_y;
1281 uint8_t ** ref_field;
1282 unsigned int pos_x, pos_y, xy_half, offset;
1284 NEEDBITS (bit_buf, bits, bit_ptr);
1285 ref_field = motion->ref2[UBITS (bit_buf, 1)];
1286 DUMPBITS (bit_buf, bits, 1);
1288 motion_x = motion->pmv[0][0] + get_motion_delta (decoder,
1289 motion->f_code[0]);
1290 motion_x = bound_motion_vector (motion_x, motion->f_code[0]);
1291 motion->pmv[1][0] = motion->pmv[0][0] = motion_x;
1293 NEEDBITS (bit_buf, bits, bit_ptr);
1294 motion_y = motion->pmv[0][1] + get_motion_delta (decoder,
1295 motion->f_code[1]);
1296 motion_y = bound_motion_vector (motion_y, motion->f_code[1]);
1297 motion->pmv[1][1] = motion->pmv[0][1] = motion_y;
1299 MOTION (table, ref_field, motion_x, motion_y, 16, 0);
1300 #undef bit_buf
1301 #undef bits
1302 #undef bit_ptr
1305 static void motion_fi_16x8 (decoder_t * const decoder, motion_t * const motion,
1306 mpeg2_mc_fct * const * const table)
1308 #define bit_buf (decoder->bitstream_buf)
1309 #define bits (decoder->bitstream_bits)
1310 #define bit_ptr (decoder->bitstream_ptr)
1311 int motion_x, motion_y;
1312 uint8_t ** ref_field;
1313 unsigned int pos_x, pos_y, xy_half, offset;
1315 NEEDBITS (bit_buf, bits, bit_ptr);
1316 ref_field = motion->ref2[UBITS (bit_buf, 1)];
1317 DUMPBITS (bit_buf, bits, 1);
1319 motion_x = motion->pmv[0][0] + get_motion_delta (decoder,
1320 motion->f_code[0]);
1321 motion_x = bound_motion_vector (motion_x, motion->f_code[0]);
1322 motion->pmv[0][0] = motion_x;
1324 NEEDBITS (bit_buf, bits, bit_ptr);
1325 motion_y = motion->pmv[0][1] + get_motion_delta (decoder,
1326 motion->f_code[1]);
1327 motion_y = bound_motion_vector (motion_y, motion->f_code[1]);
1328 motion->pmv[0][1] = motion_y;
1330 MOTION (table, ref_field, motion_x, motion_y, 8, 0);
1332 NEEDBITS (bit_buf, bits, bit_ptr);
1333 ref_field = motion->ref2[UBITS (bit_buf, 1)];
1334 DUMPBITS (bit_buf, bits, 1);
1336 motion_x = motion->pmv[1][0] + get_motion_delta (decoder,
1337 motion->f_code[0]);
1338 motion_x = bound_motion_vector (motion_x, motion->f_code[0]);
1339 motion->pmv[1][0] = motion_x;
1341 NEEDBITS (bit_buf, bits, bit_ptr);
1342 motion_y = motion->pmv[1][1] + get_motion_delta (decoder,
1343 motion->f_code[1]);
1344 motion_y = bound_motion_vector (motion_y, motion->f_code[1]);
1345 motion->pmv[1][1] = motion_y;
1347 MOTION (table, ref_field, motion_x, motion_y, 8, 8);
1348 #undef bit_buf
1349 #undef bits
1350 #undef bit_ptr
1353 static void motion_fi_dmv (decoder_t * const decoder, motion_t * const motion,
1354 mpeg2_mc_fct * const * const table)
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;
1362 NEEDBITS (bit_buf, bits, bit_ptr);
1363 motion_x = motion->pmv[0][0] + get_motion_delta (decoder,
1364 motion->f_code[0]);
1365 motion_x = bound_motion_vector (motion_x, motion->f_code[0]);
1366 motion->pmv[1][0] = motion->pmv[0][0] = motion_x;
1367 NEEDBITS (bit_buf, bits, bit_ptr);
1368 other_x = ((motion_x + (motion_x > 0)) >> 1) + get_dmv (decoder);
1370 motion_y = motion->pmv[0][1] + get_motion_delta (decoder,
1371 motion->f_code[1]);
1372 motion_y = bound_motion_vector (motion_y, motion->f_code[1]);
1373 motion->pmv[1][1] = motion->pmv[0][1] = motion_y;
1374 other_y = (((motion_y + (motion_y > 0)) >> 1) + get_dmv (decoder) +
1375 decoder->dmv_offset);
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);
1379 #undef bit_buf
1380 #undef bits
1381 #undef bit_ptr
1384 static void motion_fi_conceal (decoder_t * const decoder)
1386 #define bit_buf (decoder->bitstream_buf)
1387 #define bits (decoder->bitstream_bits)
1388 #define bit_ptr (decoder->bitstream_ptr)
1389 int tmp;
1391 NEEDBITS (bit_buf, bits, bit_ptr);
1392 DUMPBITS (bit_buf, bits, 1); /* remove field_select */
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;
1399 NEEDBITS (bit_buf, bits, bit_ptr);
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;
1405 DUMPBITS (bit_buf, bits, 1); /* remove marker_bit */
1406 #undef bit_buf
1407 #undef bits
1408 #undef bit_ptr
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)); \
1419 } while (0)
1421 #define NEXT_MACROBLOCK \
1422 do { \
1423 if(decoder->quant_store) \
1424 decoder->quant_store[decoder->quant_stride*(decoder->v_offset>>4) \
1425 +(decoder->offset>>4)] = decoder->quantizer_scale; \
1426 decoder->offset += 16; \
1427 if (decoder->offset == decoder->width) { \
1428 do { /* just so we can use the break statement */ \
1429 if (decoder->convert) { \
1430 decoder->convert (decoder->fbuf_id, decoder->dest, \
1431 decoder->v_offset); \
1432 if (decoder->coding_type == B_TYPE) \
1433 break; \
1435 decoder->dest[0] += 16 * decoder->stride; \
1436 decoder->dest[1] += 4 * decoder->stride; \
1437 decoder->dest[2] += 4 * decoder->stride; \
1438 } while (0); \
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; \
1445 decoder->offset = 0; \
1447 } while (0)
1449 void mpeg2_init_fbuf (decoder_t * decoder, uint8_t * current_fbuf[3],
1450 uint8_t * forward_fbuf[3], uint8_t * backward_fbuf[3])
1452 int offset, stride, height, bottom_field;
1454 stride = decoder->width;
1455 bottom_field = (decoder->picture_structure == BOTTOM_FIELD);
1456 offset = bottom_field ? stride : 0;
1457 height = decoder->height;
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);
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);
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);
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;
1479 if (decoder->second_field && (decoder->coding_type != B_TYPE))
1480 forward_fbuf = current_fbuf;
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);
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);
1490 stride <<= 1;
1491 height >>= 1;
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;
1502 static inline int slice_init (decoder_t * const decoder, int code)
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;
1510 decoder->dc_dct_pred[0] = decoder->dc_dct_pred[1] =
1511 decoder->dc_dct_pred[2] = 128 << decoder->intra_dc_precision;
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;
1518 if (decoder->vertical_position_extension) {
1519 code += UBITS (bit_buf, 3) << 7;
1520 DUMPBITS (bit_buf, bits, 3);
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;
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;
1531 decoder->quantizer_scale = get_quantizer_scale (decoder);
1533 /* ignore intra_slice and all the extra data */
1534 while (bit_buf & 0x80000000) {
1535 DUMPBITS (bit_buf, bits, 9);
1536 NEEDBITS (bit_buf, bits, bit_ptr);
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;
1563 DUMPBITS (bit_buf, bits, mba->len + 1);
1564 decoder->offset = (offset + mba->mba) << 4;
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;
1573 decoder->v_offset += 16;
1575 if (decoder->v_offset > decoder->limit_y)
1576 return 1;
1578 return 0;
1579 #undef bit_buf
1580 #undef bits
1581 #undef bit_ptr
1584 void mpeg2_slice (decoder_t * const decoder, const int code,
1585 const uint8_t * const buffer)
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;
1592 bitstream_init (decoder, buffer);
1594 if (slice_init (decoder, code))
1595 return;
1597 if (mpeg2_cpu_state_save)
1598 mpeg2_cpu_state_save (&cpu_state);
1600 while (1) {
1601 int macroblock_modes;
1602 int mba_inc;
1603 const MBAtab * mba;
1605 NEEDBITS (bit_buf, bits, bit_ptr);
1607 macroblock_modes = get_macroblock_modes (decoder);
1609 /* maybe integrate MACROBLOCK_QUANT test into get_macroblock_modes ? */
1610 if (macroblock_modes & MACROBLOCK_QUANT)
1611 decoder->quantizer_scale = get_quantizer_scale (decoder);
1613 if (macroblock_modes & MACROBLOCK_INTRA) {
1615 int DCT_offset, DCT_stride;
1616 int offset;
1617 uint8_t * dest_y;
1619 if (decoder->concealment_motion_vectors) {
1620 if (decoder->picture_structure == FRAME_PICTURE)
1621 motion_fr_conceal (decoder);
1622 else
1623 motion_fi_conceal (decoder);
1624 } else {
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;
1631 if (macroblock_modes & DCT_TYPE_INTERLACED) {
1632 DCT_offset = decoder->stride;
1633 DCT_stride = decoder->stride * 2;
1634 } else {
1635 DCT_offset = decoder->stride * 8;
1636 DCT_stride = decoder->stride;
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);
1650 if (decoder->coding_type == D_TYPE) {
1651 NEEDBITS (bit_buf, bits, bit_ptr);
1652 DUMPBITS (bit_buf, bits, 1);
1654 } else {
1656 if (decoder->picture_structure == FRAME_PICTURE)
1657 switch (macroblock_modes & MOTION_TYPE_MASK) {
1658 case MC_FRAME:
1659 if (decoder->mpeg1)
1660 MOTION_CALL (motion_mp1, macroblock_modes);
1661 else
1662 MOTION_CALL (motion_fr_frame, macroblock_modes);
1663 break;
1665 case MC_FIELD:
1666 MOTION_CALL (motion_fr_field, macroblock_modes);
1667 break;
1669 case MC_DMV:
1670 MOTION_CALL (motion_fr_dmv, MACROBLOCK_MOTION_FORWARD);
1671 break;
1673 case 0:
1674 /* non-intra mb without forward mv in a P picture */
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);
1680 break;
1682 else
1683 switch (macroblock_modes & MOTION_TYPE_MASK) {
1684 case MC_FIELD:
1685 MOTION_CALL (motion_fi_field, macroblock_modes);
1686 break;
1688 case MC_16X8:
1689 MOTION_CALL (motion_fi_16x8, macroblock_modes);
1690 break;
1692 case MC_DMV:
1693 MOTION_CALL (motion_fi_dmv, MACROBLOCK_MOTION_FORWARD);
1694 break;
1696 case 0:
1697 /* non-intra mb without forward mv in a P picture */
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);
1703 break;
1706 if (macroblock_modes & MACROBLOCK_PATTERN) {
1707 int coded_block_pattern;
1708 int DCT_offset, DCT_stride;
1709 int offset;
1710 uint8_t * dest_y;
1712 if (macroblock_modes & DCT_TYPE_INTERLACED) {
1713 DCT_offset = decoder->stride;
1714 DCT_stride = decoder->stride * 2;
1715 } else {
1716 DCT_offset = decoder->stride * 8;
1717 DCT_stride = decoder->stride;
1720 coded_block_pattern = get_coded_block_pattern (decoder);
1722 offset = decoder->offset;
1723 dest_y = decoder->dest[0] + offset;
1724 if (coded_block_pattern & 0x20)
1725 slice_non_intra_DCT (decoder, dest_y, DCT_stride);
1726 if (coded_block_pattern & 0x10)
1727 slice_non_intra_DCT (decoder, dest_y + 8, DCT_stride);
1728 if (coded_block_pattern & 0x08)
1729 slice_non_intra_DCT (decoder, dest_y + DCT_offset,
1730 DCT_stride);
1731 if (coded_block_pattern & 0x04)
1732 slice_non_intra_DCT (decoder, dest_y + DCT_offset + 8,
1733 DCT_stride);
1734 if (coded_block_pattern & 0x2)
1735 slice_non_intra_DCT (decoder,
1736 decoder->dest[1] + (offset >> 1),
1737 decoder->uv_stride);
1738 if (coded_block_pattern & 0x1)
1739 slice_non_intra_DCT (decoder,
1740 decoder->dest[2] + (offset >> 1),
1741 decoder->uv_stride);
1744 decoder->dc_dct_pred[0] = decoder->dc_dct_pred[1] =
1745 decoder->dc_dct_pred[2] = 128 << decoder->intra_dc_precision;
1748 NEXT_MACROBLOCK;
1750 NEEDBITS (bit_buf, bits, bit_ptr);
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);
1766 continue;
1767 default: /* end of slice, or error */
1768 if (mpeg2_cpu_state_restore)
1769 mpeg2_cpu_state_restore (&cpu_state);
1770 return;
1773 DUMPBITS (bit_buf, bits, mba->len);
1774 mba_inc += mba->mba;
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;
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;
1784 do {
1785 MOTION_CALL (motion_zero, MACROBLOCK_MOTION_FORWARD);
1786 NEXT_MACROBLOCK;
1787 } while (--mba_inc);
1788 } else {
1789 do {
1790 MOTION_CALL (motion_reuse, macroblock_modes);
1791 NEXT_MACROBLOCK;
1792 } while (--mba_inc);
1796 #undef bit_buf
1797 #undef bits
1798 #undef bit_ptr