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
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
);
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)
56 switch (decoder
->coding_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
;
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
;
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
;
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
;
116 if (macroblock_modes
& MACROBLOCK_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
)) {
122 macroblock_modes
|= UBITS (bit_buf
, 1) * DCT_TYPE_INTERLACED
;
123 DUMPBITS (bit_buf
, bits
, 1);
125 return macroblock_modes
;
130 DUMPBITS (bit_buf
, bits
, 1);
131 return MACROBLOCK_INTRA
;
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
];
155 return quantizer_scale_code
<< 1;
161 static inline int get_motion_delta (decoder_t
* const decoder
,
164 #define bit_buf (decoder->bitstream_buf)
165 #define bits (decoder->bitstream_bits)
166 #define bit_ptr (decoder->bitstream_ptr)
172 if (bit_buf
& 0x80000000) {
173 DUMPBITS (bit_buf
, bits
, 1);
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);
186 delta
+= UBITS (bit_buf
, f_code
);
189 return (delta
^ sign
) - sign
;
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);
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
;
215 static inline int bound_motion_vector (const int vector
, const int f_code
)
221 limit
= 16 << f_code
;
223 if ((unsigned int)(vector
+ limit
) < 2 * limit
)
226 sign
= ((int32_t)vector
) >> 31;
227 return vector
- ((2 * limit
) ^ sign
) + sign
;
230 return ((int32_t)vector
<< (27 - f_code
)) >> (27 - f_code
);
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)
242 tab
= DMV_2
+ UBITS (bit_buf
, 2);
243 DUMPBITS (bit_buf
, bits
, tab
->len
);
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)
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
);
268 tab
= CBP_9
+ UBITS (bit_buf
, 9);
269 DUMPBITS (bit_buf
, bits
, tab
->len
);
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)
287 if (bit_buf
< 0xf8000000) {
288 tab
= DC_lum_5
+ UBITS (bit_buf
, 5);
291 bits
+= tab
->len
+ size
;
292 bit_buf
<<= tab
->len
;
294 UBITS (bit_buf
, size
) - UBITS (SBITS (~bit_buf
, 1), size
);
298 DUMPBITS (bit_buf
, bits
, 3);
302 tab
= DC_long
+ (UBITS (bit_buf
, 9) - 0x1e0);
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
);
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)
324 if (bit_buf
< 0xf8000000) {
325 tab
= DC_chrom_5
+ UBITS (bit_buf
, 5);
328 bits
+= tab
->len
+ size
;
329 bit_buf
<<= tab
->len
;
331 UBITS (bit_buf
, size
) - UBITS (SBITS (~bit_buf
, 1), size
);
335 DUMPBITS (bit_buf
, bits
, 2);
339 tab
= DC_long
+ (UBITS (bit_buf
, 10) - 0x3e0);
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
);
352 #define SATURATE(val) \
354 if (unlikely ((uint32_t)(val + 2048) > 4095)) \
355 val = SBITS (val, 1) ^ 2047; \
358 static void get_intra_block_B14 (decoder_t
* const decoder
)
363 const uint8_t * scan
= decoder
->scan
;
364 const uint8_t * quant_matrix
= decoder
->intra_quantizer_matrix
;
365 int quantizer_scale
= decoder
->quantizer_scale
;
370 const uint8_t * bit_ptr
;
373 dest
= decoder
->DCTblock
;
377 bit_buf
= decoder
->bitstream_buf
;
378 bits
= decoder
->bitstream_bits
;
379 bit_ptr
= decoder
->bitstream_ptr
;
381 NEEDBITS (bit_buf
, bits
, bit_ptr
);
384 if (bit_buf
>= 0x28000000) {
386 tab
= DCT_B14AC_5
+ (UBITS (bit_buf
, 5) - 5);
390 break; /* end of block */
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);
406 NEEDBITS (bit_buf
, bits
, bit_ptr
);
410 } else if (bit_buf
>= 0x04000000) {
412 tab
= DCT_B14_8
+ (UBITS (bit_buf
, 8) - 4);
420 i
+= UBITS (bit_buf
<< 6, 6) - 64;
422 break; /* illegal, check needed to avoid buffer overflow */
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;
435 DUMPBITS (bit_buf
, bits
, 12);
436 NEEDBITS (bit_buf
, bits
, bit_ptr
);
440 } else if (bit_buf
>= 0x02000000) {
441 tab
= DCT_B14_10
+ (UBITS (bit_buf
, 10) - 8);
445 } else if (bit_buf
>= 0x00800000) {
446 tab
= DCT_13
+ (UBITS (bit_buf
, 13) - 16);
450 } else if (bit_buf
>= 0x00200000) {
451 tab
= DCT_15
+ (UBITS (bit_buf
, 15) - 16);
456 tab
= DCT_16
+ UBITS (bit_buf
, 16);
458 GETWORD (bit_buf
, bits
+ 16, bit_ptr
);
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
)
477 const uint8_t * scan
= decoder
->scan
;
478 const uint8_t * quant_matrix
= decoder
->intra_quantizer_matrix
;
479 int quantizer_scale
= decoder
->quantizer_scale
;
484 const uint8_t * bit_ptr
;
487 dest
= decoder
->DCTblock
;
491 bit_buf
= decoder
->bitstream_buf
;
492 bits
= decoder
->bitstream_bits
;
493 bit_ptr
= decoder
->bitstream_ptr
;
495 NEEDBITS (bit_buf
, bits
, bit_ptr
);
498 if (bit_buf
>= 0x04000000) {
500 tab
= DCT_B15_8
+ (UBITS (bit_buf
, 8) - 4);
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);
519 NEEDBITS (bit_buf
, bits
, bit_ptr
);
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 */
532 i
+= UBITS (bit_buf
<< 6, 6) - 64;
534 break; /* illegal, check against buffer overflow */
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;
547 DUMPBITS (bit_buf
, bits
, 12);
548 NEEDBITS (bit_buf
, bits
, bit_ptr
);
553 } else if (bit_buf
>= 0x02000000) {
554 tab
= DCT_B15_10
+ (UBITS (bit_buf
, 10) - 8);
558 } else if (bit_buf
>= 0x00800000) {
559 tab
= DCT_13
+ (UBITS (bit_buf
, 13) - 16);
563 } else if (bit_buf
>= 0x00200000) {
564 tab
= DCT_15
+ (UBITS (bit_buf
, 15) - 16);
569 tab
= DCT_16
+ UBITS (bit_buf
, 16);
571 GETWORD (bit_buf
, bits
+ 16, bit_ptr
);
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
)
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
;
597 const uint8_t * bit_ptr
;
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);
616 if (bit_buf
>= 0x28000000) {
618 tab
= DCT_B14AC_5
+ (UBITS (bit_buf
, 5) - 5);
623 break; /* end of block */
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);
639 NEEDBITS (bit_buf
, bits
, bit_ptr
);
646 if (bit_buf
>= 0x04000000) {
648 tab
= DCT_B14_8
+ (UBITS (bit_buf
, 8) - 4);
656 i
+= UBITS (bit_buf
<< 6, 6) - 64;
658 break; /* illegal, check needed to avoid buffer overflow */
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;
671 DUMPBITS (bit_buf
, bits
, 12);
672 NEEDBITS (bit_buf
, bits
, bit_ptr
);
676 } else if (bit_buf
>= 0x02000000) {
677 tab
= DCT_B14_10
+ (UBITS (bit_buf
, 10) - 8);
681 } else if (bit_buf
>= 0x00800000) {
682 tab
= DCT_13
+ (UBITS (bit_buf
, 13) - 16);
686 } else if (bit_buf
>= 0x00200000) {
687 tab
= DCT_15
+ (UBITS (bit_buf
, 15) - 16);
692 tab
= DCT_16
+ UBITS (bit_buf
, 16);
694 GETWORD (bit_buf
, bits
+ 16, bit_ptr
);
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
;
709 static void get_mpeg1_intra_block (decoder_t
* const decoder
)
714 const uint8_t * scan
= decoder
->scan
;
715 const uint8_t * quant_matrix
= decoder
->intra_quantizer_matrix
;
716 int quantizer_scale
= decoder
->quantizer_scale
;
720 const uint8_t * bit_ptr
;
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
);
733 if (bit_buf
>= 0x28000000) {
735 tab
= DCT_B14AC_5
+ (UBITS (bit_buf
, 5) - 5);
739 break; /* end of block */
743 bit_buf
<<= tab
->len
;
744 bits
+= tab
->len
+ 1;
745 val
= (tab
->level
* quantizer_scale
* quant_matrix
[j
]) >> 4;
750 /* if (bitstream_get (1)) val = -val; */
751 val
= (val
^ SBITS (bit_buf
, 1)) - SBITS (bit_buf
, 1);
757 NEEDBITS (bit_buf
, bits
, bit_ptr
);
761 } else if (bit_buf
>= 0x04000000) {
763 tab
= DCT_B14_8
+ (UBITS (bit_buf
, 8) - 4);
771 i
+= UBITS (bit_buf
<< 6, 6) - 64;
773 break; /* illegal, check needed to avoid buffer overflow */
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;
787 val
= (val
+ ~SBITS (val
, 1)) | 1;
792 DUMPBITS (bit_buf
, bits
, 8);
793 NEEDBITS (bit_buf
, bits
, bit_ptr
);
797 } else if (bit_buf
>= 0x02000000) {
798 tab
= DCT_B14_10
+ (UBITS (bit_buf
, 10) - 8);
802 } else if (bit_buf
>= 0x00800000) {
803 tab
= DCT_13
+ (UBITS (bit_buf
, 13) - 16);
807 } else if (bit_buf
>= 0x00200000) {
808 tab
= DCT_15
+ (UBITS (bit_buf
, 15) - 16);
813 tab
= DCT_16
+ UBITS (bit_buf
, 16);
815 GETWORD (bit_buf
, bits
+ 16, bit_ptr
);
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
)
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
;
839 const uint8_t * bit_ptr
;
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);
857 if (bit_buf
>= 0x28000000) {
859 tab
= DCT_B14AC_5
+ (UBITS (bit_buf
, 5) - 5);
864 break; /* end of block */
868 bit_buf
<<= tab
->len
;
869 bits
+= tab
->len
+ 1;
870 val
= ((2*tab
->level
+1) * quantizer_scale
* quant_matrix
[j
]) >> 5;
875 /* if (bitstream_get (1)) val = -val; */
876 val
= (val
^ SBITS (bit_buf
, 1)) - SBITS (bit_buf
, 1);
882 NEEDBITS (bit_buf
, bits
, bit_ptr
);
889 if (bit_buf
>= 0x04000000) {
891 tab
= DCT_B14_8
+ (UBITS (bit_buf
, 8) - 4);
899 i
+= UBITS (bit_buf
<< 6, 6) - 64;
901 break; /* illegal, check needed to avoid buffer overflow */
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;
916 val
= (val
+ ~SBITS (val
, 1)) | 1;
921 DUMPBITS (bit_buf
, bits
, 8);
922 NEEDBITS (bit_buf
, bits
, bit_ptr
);
926 } else if (bit_buf
>= 0x02000000) {
927 tab
= DCT_B14_10
+ (UBITS (bit_buf
, 10) - 8);
931 } else if (bit_buf
>= 0x00800000) {
932 tab
= DCT_13
+ (UBITS (bit_buf
, 13) - 16);
936 } else if (bit_buf
>= 0x00200000) {
937 tab
= DCT_15
+ (UBITS (bit_buf
, 15) - 16);
942 tab
= DCT_16
+ UBITS (bit_buf
, 16);
944 GETWORD (bit_buf
, bits
+ 16, bit_ptr
);
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
;
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 */
967 decoder
->dc_dct_pred
[0] += get_luma_dc_dct_diff (decoder
);
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
);
979 get_intra_block_B14 (decoder
);
980 mpeg2_idct_copy (decoder
->DCTblock
, dest
, stride
);
986 static inline void slice_non_intra_DCT (decoder_t
* const decoder
,
987 uint8_t * const dest
, const int stride
)
992 last
= get_mpeg1_non_intra_block (decoder
);
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)) \
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)) \
1024 xy_half = ((pos_y & 1) << 1) | (pos_x & 1); \
1025 table[xy_half] (decoder->dest[0] + dest_field * decoder->stride + \
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);
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
,
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
,
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);
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
,
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
,
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
,
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
,
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
);
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
,
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
,
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);
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)
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 */
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
,
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
,
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);
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
,
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
,
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
,
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
,
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);
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
,
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
,
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);
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)
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 */
1411 #define MOTION_CALL(routine,direction) \
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)); \
1421 #define NEXT_MACROBLOCK \
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) \
1435 decoder->dest[0] += 16 * decoder->stride; \
1436 decoder->dest[1] += 4 * decoder->stride; \
1437 decoder->dest[2] += 4 * decoder->stride; \
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); \
1445 decoder->offset = 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);
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)
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;
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 */
1542 if (bit_buf
>= 0x08000000) {
1543 mba
= MBA_5
+ (UBITS (bit_buf
, 6) - 2);
1545 } else if (bit_buf
>= 0x01800000) {
1546 mba
= MBA_11
+ (UBITS (bit_buf
, 12) - 24);
1548 } else switch (UBITS (bit_buf
, 12)) {
1549 case 8: /* macroblock_escape */
1551 DUMPBITS (bit_buf
, bits
, 11);
1552 NEEDBITS (bit_buf
, bits
, bit_ptr
);
1554 case 15: /* macroblock_stuffing (MPEG1 only) */
1556 DUMPBITS (bit_buf
, bits
, 11);
1557 NEEDBITS (bit_buf
, bits
, bit_ptr
);
1559 default: /* error */
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
)
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
))
1597 if (mpeg2_cpu_state_save
)
1598 mpeg2_cpu_state_save (&cpu_state
);
1601 int macroblock_modes
;
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
;
1619 if (decoder
->concealment_motion_vectors
) {
1620 if (decoder
->picture_structure
== FRAME_PICTURE
)
1621 motion_fr_conceal (decoder
);
1623 motion_fi_conceal (decoder
);
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;
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);
1656 if (decoder
->picture_structure
== FRAME_PICTURE
)
1657 switch (macroblock_modes
& MOTION_TYPE_MASK
) {
1660 MOTION_CALL (motion_mp1
, macroblock_modes
);
1662 MOTION_CALL (motion_fr_frame
, macroblock_modes
);
1666 MOTION_CALL (motion_fr_field
, macroblock_modes
);
1670 MOTION_CALL (motion_fr_dmv
, MACROBLOCK_MOTION_FORWARD
);
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
);
1683 switch (macroblock_modes
& MOTION_TYPE_MASK
) {
1685 MOTION_CALL (motion_fi_field
, macroblock_modes
);
1689 MOTION_CALL (motion_fi_16x8
, macroblock_modes
);
1693 MOTION_CALL (motion_fi_dmv
, MACROBLOCK_MOTION_FORWARD
);
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
);
1706 if (macroblock_modes
& MACROBLOCK_PATTERN
) {
1707 int coded_block_pattern
;
1708 int DCT_offset
, DCT_stride
;
1712 if (macroblock_modes
& DCT_TYPE_INTERLACED
) {
1713 DCT_offset
= decoder
->stride
;
1714 DCT_stride
= decoder
->stride
* 2;
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
,
1731 if (coded_block_pattern
& 0x04)
1732 slice_non_intra_DCT (decoder
, dest_y
+ DCT_offset
+ 8,
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
;
1750 NEEDBITS (bit_buf
, bits
, bit_ptr
);
1753 if (bit_buf
>= 0x10000000) {
1754 mba
= MBA_5
+ (UBITS (bit_buf
, 5) - 2);
1756 } else if (bit_buf
>= 0x03000000) {
1757 mba
= MBA_11
+ (UBITS (bit_buf
, 11) - 24);
1759 } else switch (UBITS (bit_buf
, 11)) {
1760 case 8: /* macroblock_escape */
1763 case 15: /* macroblock_stuffing (MPEG1 only) */
1764 DUMPBITS (bit_buf
, bits
, 11);
1765 NEEDBITS (bit_buf
, bits
, bit_ptr
);
1767 default: /* end of slice, or error */
1768 if (mpeg2_cpu_state_restore
)
1769 mpeg2_cpu_state_restore (&cpu_state
);
1773 DUMPBITS (bit_buf
, bits
, mba
->len
);
1774 mba_inc
+= mba
->mba
;
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;
1785 MOTION_CALL (motion_zero
, MACROBLOCK_MOTION_FORWARD
);
1787 } while (--mba_inc
);
1790 MOTION_CALL (motion_reuse
, macroblock_modes
);
1792 } while (--mba_inc
);