3 * Copyright (C) 2000-2003 Michel Lespinasse <walken@zoy.org>
4 * Copyright (C) 2003 Peter Gubanov <peter@elecard.net.ru>
5 * Copyright (C) 1999-2000 Aaron Holtzman <aholtzma@ess.engr.uvic.ca>
7 * This file is part of mpeg2dec, a free MPEG-2 video stream decoder.
8 * See http://libmpeg2.sourceforge.net/ for updates.
10 * mpeg2dec is free software; you can redistribute it and/or modify
11 * it under the terms of the GNU General Public License as published by
12 * the Free Software Foundation; either version 2 of the License, or
13 * (at your option) any later version.
15 * mpeg2dec is distributed in the hope that it will be useful,
16 * but WITHOUT ANY WARRANTY; without even the implied warranty of
17 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
18 * GNU General Public License for more details.
20 * You should have received a copy of the GNU General Public License
21 * along with this program; if not, write to the Free Software
22 * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
27 #include "mpeg2dec_config.h"
30 #include "attributes.h"
31 #include "mpeg2_internal.h"
35 static inline int get_macroblock_modes (mpeg2_decoder_t
* const decoder
)
37 #define bit_buf (decoder->bitstream_buf)
38 #define bits (decoder->bitstream_bits)
39 #define bit_ptr (decoder->bitstream_ptr)
44 switch (decoder
->coding_type
)
47 tab
= MB_I
+ UBITS (bit_buf
, 1);
48 DUMPBITS (bit_buf
, bits
, tab
->len
);
49 macroblock_modes
= tab
->modes
;
51 if (!(decoder
->frame_pred_frame_dct
) &&
52 decoder
->picture_structure
== FRAME_PICTURE
)
54 macroblock_modes
|= UBITS (bit_buf
, 1) * DCT_TYPE_INTERLACED
;
55 DUMPBITS (bit_buf
, bits
, 1);
58 return macroblock_modes
;
61 tab
= MB_P
+ UBITS (bit_buf
, 5);
62 DUMPBITS (bit_buf
, bits
, tab
->len
);
63 macroblock_modes
= tab
->modes
;
65 if (decoder
->picture_structure
!= FRAME_PICTURE
)
67 if (macroblock_modes
& MACROBLOCK_MOTION_FORWARD
)
69 macroblock_modes
|= UBITS (bit_buf
, 2) << MOTION_TYPE_SHIFT
;
70 DUMPBITS (bit_buf
, bits
, 2);
73 return macroblock_modes
| MACROBLOCK_MOTION_FORWARD
;
75 else if (decoder
->frame_pred_frame_dct
)
77 if (macroblock_modes
& MACROBLOCK_MOTION_FORWARD
)
78 macroblock_modes
|= MC_FRAME
<< MOTION_TYPE_SHIFT
;
80 return macroblock_modes
| MACROBLOCK_MOTION_FORWARD
;
84 if (macroblock_modes
& MACROBLOCK_MOTION_FORWARD
)
86 macroblock_modes
|= UBITS (bit_buf
, 2) << MOTION_TYPE_SHIFT
;
87 DUMPBITS (bit_buf
, bits
, 2);
90 if (macroblock_modes
& (MACROBLOCK_INTRA
| MACROBLOCK_PATTERN
))
92 macroblock_modes
|= UBITS (bit_buf
, 1) * DCT_TYPE_INTERLACED
;
93 DUMPBITS (bit_buf
, bits
, 1);
96 return macroblock_modes
| MACROBLOCK_MOTION_FORWARD
;
100 tab
= MB_B
+ UBITS (bit_buf
, 6);
101 DUMPBITS (bit_buf
, bits
, tab
->len
);
102 macroblock_modes
= tab
->modes
;
104 if (decoder
->picture_structure
!= FRAME_PICTURE
)
106 if (! (macroblock_modes
& MACROBLOCK_INTRA
))
108 macroblock_modes
|= UBITS (bit_buf
, 2) << MOTION_TYPE_SHIFT
;
109 DUMPBITS (bit_buf
, bits
, 2);
112 return macroblock_modes
;
114 else if (decoder
->frame_pred_frame_dct
)
116 /* if (! (macroblock_modes & MACROBLOCK_INTRA)) */
117 macroblock_modes
|= MC_FRAME
<< MOTION_TYPE_SHIFT
;
118 return macroblock_modes
;
122 if (macroblock_modes
& MACROBLOCK_INTRA
)
125 macroblock_modes
|= UBITS (bit_buf
, 2) << MOTION_TYPE_SHIFT
;
126 DUMPBITS (bit_buf
, bits
, 2);
128 if (macroblock_modes
& (MACROBLOCK_INTRA
| MACROBLOCK_PATTERN
))
131 macroblock_modes
|= UBITS (bit_buf
, 1) * DCT_TYPE_INTERLACED
;
132 DUMPBITS (bit_buf
, bits
, 1);
134 return macroblock_modes
;
138 DUMPBITS (bit_buf
, bits
, 1);
139 return MACROBLOCK_INTRA
;
149 static inline void get_quantizer_scale (mpeg2_decoder_t
* const decoder
)
151 #define bit_buf (decoder->bitstream_buf)
152 #define bits (decoder->bitstream_bits)
153 #define bit_ptr (decoder->bitstream_ptr)
155 int quantizer_scale_code
;
157 quantizer_scale_code
= UBITS (bit_buf
, 5);
158 DUMPBITS (bit_buf
, bits
, 5);
160 decoder
->quantizer_matrix
[0] =
161 decoder
->quantizer_prescale
[0][quantizer_scale_code
];
163 decoder
->quantizer_matrix
[1] =
164 decoder
->quantizer_prescale
[1][quantizer_scale_code
];
166 decoder
->quantizer_matrix
[2] =
167 decoder
->chroma_quantizer
[0][quantizer_scale_code
];
169 decoder
->quantizer_matrix
[3] =
170 decoder
->chroma_quantizer
[1][quantizer_scale_code
];
176 static inline int get_motion_delta (mpeg2_decoder_t
* const decoder
,
179 #define bit_buf (decoder->bitstream_buf)
180 #define bits (decoder->bitstream_bits)
181 #define bit_ptr (decoder->bitstream_ptr)
187 if (bit_buf
& 0x80000000)
189 DUMPBITS (bit_buf
, bits
, 1);
192 else if (bit_buf
>= 0x0c000000)
194 tab
= MV_4
+ UBITS (bit_buf
, 4);
195 delta
= (tab
->delta
<< f_code
) + 1;
196 bits
+= tab
->len
+ f_code
+ 1;
197 bit_buf
<<= tab
->len
;
199 sign
= SBITS (bit_buf
, 1);
203 delta
+= UBITS (bit_buf
, f_code
);
206 return (delta
^ sign
) - sign
;
210 tab
= MV_10
+ UBITS (bit_buf
, 10);
211 delta
= (tab
->delta
<< f_code
) + 1;
212 bits
+= tab
->len
+ 1;
213 bit_buf
<<= tab
->len
;
215 sign
= SBITS (bit_buf
, 1);
220 NEEDBITS (bit_buf
, bits
, bit_ptr
);
221 delta
+= UBITS (bit_buf
, f_code
);
222 DUMPBITS (bit_buf
, bits
, f_code
);
225 return (delta
^ sign
) - sign
;
233 static inline int bound_motion_vector (const int vector
, const int f_code
)
235 return ((int32_t)vector
<< (27 - f_code
)) >> (27 - f_code
);
238 static inline int get_dmv (mpeg2_decoder_t
* const decoder
)
240 #define bit_buf (decoder->bitstream_buf)
241 #define bits (decoder->bitstream_bits)
242 #define bit_ptr (decoder->bitstream_ptr)
246 tab
= DMV_2
+ UBITS (bit_buf
, 2);
247 DUMPBITS (bit_buf
, bits
, tab
->len
);
255 static inline int get_coded_block_pattern (mpeg2_decoder_t
* const decoder
)
257 #define bit_buf (decoder->bitstream_buf)
258 #define bits (decoder->bitstream_bits)
259 #define bit_ptr (decoder->bitstream_ptr)
263 NEEDBITS (bit_buf
, bits
, bit_ptr
);
265 if (bit_buf
>= 0x20000000)
267 tab
= CBP_7
+ (UBITS (bit_buf
, 7) - 16);
268 DUMPBITS (bit_buf
, bits
, tab
->len
);
273 tab
= CBP_9
+ UBITS (bit_buf
, 9);
274 DUMPBITS (bit_buf
, bits
, tab
->len
);
283 static inline int get_luma_dc_dct_diff (mpeg2_decoder_t
* const decoder
)
285 #define bit_buf (decoder->bitstream_buf)
286 #define bits (decoder->bitstream_bits)
287 #define bit_ptr (decoder->bitstream_ptr)
293 if (bit_buf
< 0xf8000000)
295 tab
= DC_lum_5
+ UBITS (bit_buf
, 5);
300 bits
+= tab
->len
+ size
;
301 bit_buf
<<= tab
->len
;
303 UBITS (bit_buf
, size
) - UBITS (SBITS (~bit_buf
, 1), size
);
305 return dc_diff
<< decoder
->intra_dc_precision
;
309 DUMPBITS (bit_buf
, bits
, 3);
315 tab
= DC_long
+ (UBITS (bit_buf
, 9) - 0x1e0);
317 DUMPBITS (bit_buf
, bits
, tab
->len
);
318 NEEDBITS (bit_buf
, bits
, bit_ptr
);
319 dc_diff
= UBITS (bit_buf
, size
) - UBITS (SBITS (~bit_buf
, 1), size
);
320 DUMPBITS (bit_buf
, bits
, size
);
321 return dc_diff
<< decoder
->intra_dc_precision
;
330 static inline int get_chroma_dc_dct_diff (mpeg2_decoder_t
* const decoder
)
332 #define bit_buf (decoder->bitstream_buf)
333 #define bits (decoder->bitstream_bits)
334 #define bit_ptr (decoder->bitstream_ptr)
340 if (bit_buf
< 0xf8000000)
342 tab
= DC_chrom_5
+ UBITS (bit_buf
, 5);
347 bits
+= tab
->len
+ size
;
348 bit_buf
<<= tab
->len
;
350 UBITS (bit_buf
, size
) - UBITS (SBITS (~bit_buf
, 1), size
);
352 return dc_diff
<< decoder
->intra_dc_precision
;
356 DUMPBITS (bit_buf
, bits
, 2);
362 tab
= DC_long
+ (UBITS (bit_buf
, 10) - 0x3e0);
364 DUMPBITS (bit_buf
, bits
, tab
->len
+ 1);
365 NEEDBITS (bit_buf
, bits
, bit_ptr
);
366 dc_diff
= UBITS (bit_buf
, size
) - UBITS (SBITS (~bit_buf
, 1), size
);
367 DUMPBITS (bit_buf
, bits
, size
);
368 return dc_diff
<< decoder
->intra_dc_precision
;
375 #endif /* MPEG2_COLOR */
377 #define SATURATE(val) \
380 if (unlikely (val != (int16_t) val)) \
381 val = (SBITS (val, 1) ^ 2047) << 4; \
384 static void get_intra_block_B14 (mpeg2_decoder_t
* const decoder
,
385 const uint16_t * const quant_matrix
)
387 uint32_t bit_buf
= decoder
->bitstream_buf
;
388 int bits
= decoder
->bitstream_bits
;
389 const uint8_t * bit_ptr
= decoder
->bitstream_ptr
;
390 const uint8_t * const scan
= decoder
->scan
;
391 int16_t * const dest
= decoder
->DCTblock
;
392 int mismatch
= ~dest
[0];
398 NEEDBITS (bit_buf
, bits
, bit_ptr
);
402 if (bit_buf
>= 0x28000000)
404 tab
= DCT_B14AC_5
+ (UBITS (bit_buf
, 5) - 5);
408 break; /* end of block */
412 bit_buf
<<= tab
->len
;
413 bits
+= tab
->len
+ 1;
414 val
= (tab
->level
* quant_matrix
[j
]) >> 4;
416 /* if (bitstream_get (1)) val = -val; */
417 val
= (val
^ SBITS (bit_buf
, 1)) - SBITS (bit_buf
, 1);
424 NEEDBITS (bit_buf
, bits
, bit_ptr
);
428 else if (bit_buf
>= 0x04000000)
430 tab
= DCT_B14_8
+ (UBITS (bit_buf
, 8) - 4);
438 i
+= UBITS (bit_buf
<< 6, 6) - 64;
440 break; /* illegal, check needed to avoid buffer overflow */
444 DUMPBITS (bit_buf
, bits
, 12);
445 NEEDBITS (bit_buf
, bits
, bit_ptr
);
446 val
= (SBITS (bit_buf
, 12) * quant_matrix
[j
]) / 16;
452 DUMPBITS (bit_buf
, bits
, 12);
453 NEEDBITS (bit_buf
, bits
, bit_ptr
);
457 else if (bit_buf
>= 0x02000000)
459 tab
= DCT_B14_10
+ (UBITS (bit_buf
, 10) - 8);
464 else if (bit_buf
>= 0x00800000)
466 tab
= DCT_13
+ (UBITS (bit_buf
, 13) - 16);
471 else if (bit_buf
>= 0x00200000)
473 tab
= DCT_15
+ (UBITS (bit_buf
, 15) - 16);
480 tab
= DCT_16
+ UBITS (bit_buf
, 16);
482 GETWORD (bit_buf
, bits
+ 16, bit_ptr
);
487 break; /* illegal, check needed to avoid buffer overflow */
490 dest
[63] ^= mismatch
& 16;
491 DUMPBITS (bit_buf
, bits
, 2); /* dump end of block code */
492 decoder
->bitstream_buf
= bit_buf
;
493 decoder
->bitstream_bits
= bits
;
494 decoder
->bitstream_ptr
= bit_ptr
;
497 static void get_intra_block_B15 (mpeg2_decoder_t
* const decoder
,
498 const uint16_t * const quant_matrix
)
500 uint32_t bit_buf
= decoder
->bitstream_buf
;
501 int bits
= decoder
->bitstream_bits
;
502 const uint8_t * bit_ptr
= decoder
->bitstream_ptr
;
503 const uint8_t * const scan
= decoder
->scan
;
504 int16_t * const dest
= decoder
->DCTblock
;
505 int mismatch
= ~dest
[0];
511 NEEDBITS (bit_buf
, bits
, bit_ptr
);
515 if (bit_buf
>= 0x04000000)
517 tab
= DCT_B15_8
+ (UBITS (bit_buf
, 8) - 4);
525 bit_buf
<<= tab
->len
;
526 bits
+= tab
->len
+ 1;
527 val
= (tab
->level
* quant_matrix
[j
]) >> 4;
529 /* if (bitstream_get (1)) val = -val; */
530 val
= (val
^ SBITS (bit_buf
, 1)) - SBITS (bit_buf
, 1);
537 NEEDBITS (bit_buf
, bits
, bit_ptr
);
543 /* end of block. I commented out this code because if we */
544 /* dont exit here we will still exit at the later test :) */
546 /* if (i >= 128) break; */ /* end of block */
550 i
+= UBITS (bit_buf
<< 6, 6) - 64;
552 break; /* illegal, check against buffer overflow */
556 DUMPBITS (bit_buf
, bits
, 12);
557 NEEDBITS (bit_buf
, bits
, bit_ptr
);
558 val
= (SBITS (bit_buf
, 12) * quant_matrix
[j
]) / 16;
564 DUMPBITS (bit_buf
, bits
, 12);
565 NEEDBITS (bit_buf
, bits
, bit_ptr
);
570 else if (bit_buf
>= 0x02000000)
572 tab
= DCT_B15_10
+ (UBITS (bit_buf
, 10) - 8);
577 else if (bit_buf
>= 0x00800000)
579 tab
= DCT_13
+ (UBITS (bit_buf
, 13) - 16);
584 else if (bit_buf
>= 0x00200000)
586 tab
= DCT_15
+ (UBITS (bit_buf
, 15) - 16);
593 tab
= DCT_16
+ UBITS (bit_buf
, 16);
595 GETWORD (bit_buf
, bits
+ 16, bit_ptr
);
600 break; /* illegal, check needed to avoid buffer overflow */
603 dest
[63] ^= mismatch
& 16;
604 DUMPBITS (bit_buf
, bits
, 4); /* dump end of block code */
605 decoder
->bitstream_buf
= bit_buf
;
606 decoder
->bitstream_bits
= bits
;
607 decoder
->bitstream_ptr
= bit_ptr
;
610 static int get_non_intra_block (mpeg2_decoder_t
* const decoder
,
611 const uint16_t * const quant_matrix
)
613 uint32_t bit_buf
= decoder
->bitstream_buf
;
614 int bits
= decoder
->bitstream_bits
;
615 const uint8_t * bit_ptr
= decoder
->bitstream_ptr
;
616 const uint8_t * const scan
= decoder
->scan
;
617 int16_t * const dest
= decoder
->DCTblock
;
624 NEEDBITS (bit_buf
, bits
, bit_ptr
);
626 if (bit_buf
>= 0x28000000)
628 tab
= DCT_B14DC_5
+ (UBITS (bit_buf
, 5) - 5);
638 if (bit_buf
>= 0x28000000)
640 tab
= DCT_B14AC_5
+ (UBITS (bit_buf
, 5) - 5);
645 break; /* end of block */
649 bit_buf
<<= tab
->len
;
650 bits
+= tab
->len
+ 1;
651 val
= ((2 * tab
->level
+ 1) * quant_matrix
[j
]) >> 5;
653 /* if (bitstream_get (1)) val = -val; */
654 val
= (val
^ SBITS (bit_buf
, 1)) - SBITS (bit_buf
, 1);
661 NEEDBITS (bit_buf
, bits
, bit_ptr
);
667 if (bit_buf
>= 0x04000000)
669 tab
= DCT_B14_8
+ (UBITS (bit_buf
, 8) - 4);
677 i
+= UBITS (bit_buf
<< 6, 6) - 64;
679 break; /* illegal, check needed to avoid buffer overflow */
683 DUMPBITS (bit_buf
, bits
, 12);
684 NEEDBITS (bit_buf
, bits
, bit_ptr
);
685 val
= 2 * (SBITS (bit_buf
, 12) + SBITS (bit_buf
, 1)) + 1;
686 val
= (val
* quant_matrix
[j
]) / 32;
692 DUMPBITS (bit_buf
, bits
, 12);
693 NEEDBITS (bit_buf
, bits
, bit_ptr
);
697 else if (bit_buf
>= 0x02000000)
699 tab
= DCT_B14_10
+ (UBITS (bit_buf
, 10) - 8);
704 else if (bit_buf
>= 0x00800000)
706 tab
= DCT_13
+ (UBITS (bit_buf
, 13) - 16);
711 else if (bit_buf
>= 0x00200000)
713 tab
= DCT_15
+ (UBITS (bit_buf
, 15) - 16);
720 tab
= DCT_16
+ UBITS (bit_buf
, 16);
722 GETWORD (bit_buf
, bits
+ 16, bit_ptr
);
727 break; /* illegal, check needed to avoid buffer overflow */
730 dest
[63] ^= mismatch
& 16;
731 DUMPBITS (bit_buf
, bits
, 2); /* dump end of block code */
732 decoder
->bitstream_buf
= bit_buf
;
733 decoder
->bitstream_bits
= bits
;
734 decoder
->bitstream_ptr
= bit_ptr
;
738 static void get_mpeg1_intra_block (mpeg2_decoder_t
* const decoder
)
740 uint32_t bit_buf
= decoder
->bitstream_buf
;
741 int bits
= decoder
->bitstream_bits
;
742 const uint8_t * bit_ptr
= decoder
->bitstream_ptr
;
743 const uint8_t * const scan
= decoder
->scan
;
744 const uint16_t * const quant_matrix
= decoder
->quantizer_matrix
[0];
745 int16_t * const dest
= decoder
->DCTblock
;
751 NEEDBITS (bit_buf
, bits
, bit_ptr
);
755 if (bit_buf
>= 0x28000000)
757 tab
= DCT_B14AC_5
+ (UBITS (bit_buf
, 5) - 5);
761 break; /* end of block */
765 bit_buf
<<= tab
->len
;
766 bits
+= tab
->len
+ 1;
767 val
= (tab
->level
* quant_matrix
[j
]) >> 4;
772 /* if (bitstream_get (1)) val = -val; */
773 val
= (val
^ SBITS (bit_buf
, 1)) - SBITS (bit_buf
, 1);
779 NEEDBITS (bit_buf
, bits
, bit_ptr
);
783 else if (bit_buf
>= 0x04000000)
785 tab
= DCT_B14_8
+ (UBITS (bit_buf
, 8) - 4);
793 i
+= UBITS (bit_buf
<< 6, 6) - 64;
795 break; /* illegal, check needed to avoid buffer overflow */
799 DUMPBITS (bit_buf
, bits
, 12);
800 NEEDBITS (bit_buf
, bits
, bit_ptr
);
801 val
= SBITS (bit_buf
, 8);
805 DUMPBITS (bit_buf
, bits
, 8);
806 val
= UBITS (bit_buf
, 8) + 2 * val
;
809 val
= (val
* quant_matrix
[j
]) / 16;
812 val
= (val
+ ~SBITS (val
, 1)) | 1;
817 DUMPBITS (bit_buf
, bits
, 8);
818 NEEDBITS (bit_buf
, bits
, bit_ptr
);
822 else if (bit_buf
>= 0x02000000)
824 tab
= DCT_B14_10
+ (UBITS (bit_buf
, 10) - 8);
829 else if (bit_buf
>= 0x00800000)
831 tab
= DCT_13
+ (UBITS (bit_buf
, 13) - 16);
836 else if (bit_buf
>= 0x00200000)
838 tab
= DCT_15
+ (UBITS (bit_buf
, 15) - 16);
845 tab
= DCT_16
+ UBITS (bit_buf
, 16);
847 GETWORD (bit_buf
, bits
+ 16, bit_ptr
);
852 break; /* illegal, check needed to avoid buffer overflow */
855 DUMPBITS (bit_buf
, bits
, 2); /* dump end of block code */
856 decoder
->bitstream_buf
= bit_buf
;
857 decoder
->bitstream_bits
= bits
;
858 decoder
->bitstream_ptr
= bit_ptr
;
861 static int get_mpeg1_non_intra_block (mpeg2_decoder_t
* const decoder
)
863 uint32_t bit_buf
= decoder
->bitstream_buf
;
864 int bits
= decoder
->bitstream_bits
;
865 const uint8_t * bit_ptr
= decoder
->bitstream_ptr
;
866 const uint8_t * const scan
= decoder
->scan
;
867 const uint16_t * const quant_matrix
= decoder
->quantizer_matrix
[1];
868 int16_t * const dest
= decoder
->DCTblock
;
874 NEEDBITS (bit_buf
, bits
, bit_ptr
);
875 if (bit_buf
>= 0x28000000)
877 tab
= DCT_B14DC_5
+ (UBITS (bit_buf
, 5) - 5);
887 if (bit_buf
>= 0x28000000)
889 tab
= DCT_B14AC_5
+ (UBITS (bit_buf
, 5) - 5);
894 break; /* end of block */
898 bit_buf
<<= tab
->len
;
899 bits
+= tab
->len
+ 1;
900 val
= ((2 * tab
->level
+ 1) * quant_matrix
[j
]) >> 5;
905 /* if (bitstream_get (1)) val = -val; */
906 val
= (val
^ SBITS (bit_buf
, 1)) - SBITS (bit_buf
, 1);
912 NEEDBITS (bit_buf
, bits
, bit_ptr
);
918 if (bit_buf
>= 0x04000000)
920 tab
= DCT_B14_8
+ (UBITS (bit_buf
, 8) - 4);
928 i
+= UBITS (bit_buf
<< 6, 6) - 64;
930 break; /* illegal, check needed to avoid buffer overflow */
934 DUMPBITS (bit_buf
, bits
, 12);
935 NEEDBITS (bit_buf
, bits
, bit_ptr
);
936 val
= SBITS (bit_buf
, 8);
940 DUMPBITS (bit_buf
, bits
, 8);
941 val
= UBITS (bit_buf
, 8) + 2 * val
;
944 val
= 2 * (val
+ SBITS (val
, 1)) + 1;
945 val
= (val
* quant_matrix
[j
]) / 32;
948 val
= (val
+ ~SBITS (val
, 1)) | 1;
953 DUMPBITS (bit_buf
, bits
, 8);
954 NEEDBITS (bit_buf
, bits
, bit_ptr
);
959 else if (bit_buf
>= 0x02000000)
961 tab
= DCT_B14_10
+ (UBITS (bit_buf
, 10) - 8);
966 else if (bit_buf
>= 0x00800000)
968 tab
= DCT_13
+ (UBITS (bit_buf
, 13) - 16);
973 else if (bit_buf
>= 0x00200000)
975 tab
= DCT_15
+ (UBITS (bit_buf
, 15) - 16);
982 tab
= DCT_16
+ UBITS (bit_buf
, 16);
984 GETWORD (bit_buf
, bits
+ 16, bit_ptr
);
989 break; /* illegal, check needed to avoid buffer overflow */
992 DUMPBITS (bit_buf
, bits
, 2); /* dump end of block code */
993 decoder
->bitstream_buf
= bit_buf
;
994 decoder
->bitstream_bits
= bits
;
995 decoder
->bitstream_ptr
= bit_ptr
;
999 static inline void slice_intra_DCT (mpeg2_decoder_t
* const decoder
,
1001 uint8_t * const dest
, const int stride
)
1003 #define bit_buf (decoder->bitstream_buf)
1004 #define bits (decoder->bitstream_bits)
1005 #define bit_ptr (decoder->bitstream_ptr)
1007 NEEDBITS (bit_buf
, bits
, bit_ptr
);
1008 /* Get the intra DC coefficient and inverse quantize it */
1011 decoder
->dc_dct_pred
[0] += get_luma_dc_dct_diff (decoder
);
1012 decoder
->DCTblock
[0] = decoder
->dc_dct_pred
[0];
1018 decoder
->dc_dct_pred
[cc
] += get_chroma_dc_dct_diff (decoder
);
1019 decoder
->DCTblock
[0] = decoder
->dc_dct_pred
[cc
];
1025 if (decoder
->coding_type
!= D_TYPE
)
1026 get_mpeg1_intra_block (decoder
);
1028 else if (decoder
->intra_vlc_format
)
1030 get_intra_block_B15 (decoder
, decoder
->quantizer_matrix
[cc
? 2 : 0]);
1034 get_intra_block_B14 (decoder
, decoder
->quantizer_matrix
[cc
? 2 : 0]);
1037 mpeg2_idct_copy (decoder
->DCTblock
, dest
, stride
);
1044 static inline void slice_non_intra_DCT (mpeg2_decoder_t
* const decoder
,
1046 uint8_t * const dest
, const int stride
)
1052 last
= get_mpeg1_non_intra_block (decoder
);
1056 last
= get_non_intra_block (decoder
,
1057 decoder
->quantizer_matrix
[cc
? 3 : 1]);
1060 mpeg2_idct_add (last
, decoder
->DCTblock
, dest
, stride
);
1064 static void skip_mpeg1_intra_block (mpeg2_decoder_t
* const decoder
)
1066 uint32_t bit_buf
= decoder
->bitstream_buf
;
1067 int bits
= decoder
->bitstream_bits
;
1068 const uint8_t * bit_ptr
= decoder
->bitstream_ptr
;
1072 NEEDBITS (bit_buf
, bits
, bit_ptr
);
1076 if (bit_buf
>= 0x28000000)
1078 tab
= DCT_B14AC_5
+ (UBITS (bit_buf
, 5) - 5);
1082 break; /* end of block */
1085 bit_buf
<<= tab
->len
+ 1;
1086 bits
+= tab
->len
+ 1;
1087 NEEDBITS (bit_buf
, bits
, bit_ptr
);
1090 else if (bit_buf
>= 0x04000000)
1092 tab
= DCT_B14_8
+ (UBITS (bit_buf
, 8) - 4);
1100 i
+= UBITS (bit_buf
<< 6, 6) - 64;
1102 break; /* illegal, check needed to avoid buffer overflow */
1104 DUMPBITS (bit_buf
, bits
, 12);
1105 NEEDBITS (bit_buf
, bits
, bit_ptr
);
1107 if (!(SBITS (bit_buf
, 8) & 0x7f))
1108 DUMPBITS (bit_buf
, bits
, 8);
1110 DUMPBITS (bit_buf
, bits
, 8);
1111 NEEDBITS (bit_buf
, bits
, bit_ptr
);
1115 else if (bit_buf
>= 0x02000000)
1117 tab
= DCT_B14_10
+ (UBITS (bit_buf
, 10) - 8);
1122 else if (bit_buf
>= 0x00800000)
1124 tab
= DCT_13
+ (UBITS (bit_buf
, 13) - 16);
1129 else if (bit_buf
>= 0x00200000)
1131 tab
= DCT_15
+ (UBITS (bit_buf
, 15) - 16);
1138 tab
= DCT_16
+ UBITS (bit_buf
, 16);
1140 GETWORD (bit_buf
, bits
+ 16, bit_ptr
);
1145 break; /* illegal, check needed to avoid buffer overflow */
1148 DUMPBITS (bit_buf
, bits
, 2); /* dump end of block code */
1149 decoder
->bitstream_buf
= bit_buf
;
1150 decoder
->bitstream_bits
= bits
;
1151 decoder
->bitstream_ptr
= bit_ptr
;
1154 static void skip_intra_block_B14 (mpeg2_decoder_t
* const decoder
)
1156 uint32_t bit_buf
= decoder
->bitstream_buf
;
1157 int bits
= decoder
->bitstream_bits
;
1158 const uint8_t * bit_ptr
= decoder
->bitstream_ptr
;
1162 NEEDBITS (bit_buf
, bits
, bit_ptr
);
1166 if (bit_buf
>= 0x28000000)
1168 tab
= DCT_B14AC_5
+ (UBITS (bit_buf
, 5) - 5);
1172 break; /* end of block */
1175 bit_buf
<<= tab
->len
+ 1;
1176 bits
+= tab
->len
+ 1;
1177 NEEDBITS (bit_buf
, bits
, bit_ptr
);
1180 else if (bit_buf
>= 0x04000000)
1182 tab
= DCT_B14_8
+ (UBITS (bit_buf
, 8) - 4);
1190 i
+= UBITS (bit_buf
<< 6, 6) - 64;
1192 break; /* illegal, check needed to avoid buffer overflow */
1194 DUMPBITS (bit_buf
, bits
, 12); /* Can't dump more than 16 atm */
1195 NEEDBITS (bit_buf
, bits
, bit_ptr
);
1196 DUMPBITS (bit_buf
, bits
, 12);
1197 NEEDBITS (bit_buf
, bits
, bit_ptr
);
1200 else if (bit_buf
>= 0x02000000)
1202 tab
= DCT_B14_10
+ (UBITS (bit_buf
, 10) - 8);
1207 else if (bit_buf
>= 0x00800000)
1209 tab
= DCT_13
+ (UBITS (bit_buf
, 13) - 16);
1214 else if (bit_buf
>= 0x00200000)
1216 tab
= DCT_15
+ (UBITS (bit_buf
, 15) - 16);
1223 tab
= DCT_16
+ UBITS (bit_buf
, 16);
1225 GETWORD (bit_buf
, bits
+ 16, bit_ptr
);
1230 break; /* illegal, check needed to avoid buffer overflow */
1233 DUMPBITS (bit_buf
, bits
, 2); /* dump end of block code */
1234 decoder
->bitstream_buf
= bit_buf
;
1235 decoder
->bitstream_bits
= bits
;
1236 decoder
->bitstream_ptr
= bit_ptr
;
1239 static void skip_intra_block_B15 (mpeg2_decoder_t
* const decoder
)
1241 uint32_t bit_buf
= decoder
->bitstream_buf
;
1242 int bits
= decoder
->bitstream_bits
;
1243 const uint8_t * bit_ptr
= decoder
->bitstream_ptr
;
1247 NEEDBITS (bit_buf
, bits
, bit_ptr
);
1251 if (bit_buf
>= 0x04000000)
1253 tab
= DCT_B15_8
+ (UBITS (bit_buf
, 8) - 4);
1260 bit_buf
<<= tab
->len
+ 1;
1261 bits
+= tab
->len
+ 1;
1262 NEEDBITS (bit_buf
, bits
, bit_ptr
);
1267 /* end of block. I commented out this code because if we */
1268 /* dont exit here we will still exit at the later test :) */
1270 /* if (i >= 128) break; */ /* end of block */
1274 i
+= UBITS (bit_buf
<< 6, 6) - 64;
1276 break; /* illegal, check against buffer overflow */
1278 DUMPBITS (bit_buf
, bits
, 12); /* Can't dump more than 16 atm */
1279 NEEDBITS (bit_buf
, bits
, bit_ptr
);
1280 DUMPBITS (bit_buf
, bits
, 12);
1281 NEEDBITS (bit_buf
, bits
, bit_ptr
);
1285 else if (bit_buf
>= 0x02000000)
1287 tab
= DCT_B15_10
+ (UBITS (bit_buf
, 10) - 8);
1292 else if (bit_buf
>= 0x00800000)
1294 tab
= DCT_13
+ (UBITS (bit_buf
, 13) - 16);
1299 else if (bit_buf
>= 0x00200000)
1301 tab
= DCT_15
+ (UBITS (bit_buf
, 15) - 16);
1308 tab
= DCT_16
+ UBITS (bit_buf
, 16);
1310 GETWORD (bit_buf
, bits
+ 16, bit_ptr
);
1315 break; /* illegal, check needed to avoid buffer overflow */
1318 DUMPBITS (bit_buf
, bits
, 4); /* dump end of block code */
1319 decoder
->bitstream_buf
= bit_buf
;
1320 decoder
->bitstream_bits
= bits
;
1321 decoder
->bitstream_ptr
= bit_ptr
;
1324 static void skip_non_intra_block (mpeg2_decoder_t
* const decoder
)
1326 uint32_t bit_buf
= decoder
->bitstream_buf
;
1327 int bits
= decoder
->bitstream_bits
;
1328 const uint8_t * bit_ptr
= decoder
->bitstream_ptr
;
1332 NEEDBITS (bit_buf
, bits
, bit_ptr
);
1334 if (bit_buf
>= 0x28000000)
1336 tab
= DCT_B14DC_5
+ (UBITS (bit_buf
, 5) - 5);
1346 if (bit_buf
>= 0x28000000)
1348 tab
= DCT_B14AC_5
+ (UBITS (bit_buf
, 5) - 5);
1353 break; /* end of block */
1356 bit_buf
<<= tab
->len
+ 1;
1357 bits
+= tab
->len
+ 1;
1358 NEEDBITS (bit_buf
, bits
, bit_ptr
);
1364 if (bit_buf
>= 0x04000000)
1366 tab
= DCT_B14_8
+ (UBITS (bit_buf
, 8) - 4);
1374 i
+= UBITS (bit_buf
<< 6, 6) - 64;
1376 break; /* illegal, check needed to avoid buffer overflow */
1380 DUMPBITS (bit_buf
, bits
, 12);
1381 NEEDBITS (bit_buf
, bits
, bit_ptr
);
1383 if (!(SBITS (bit_buf
, 8) & 0x7f))
1384 DUMPBITS (bit_buf
, bits
, 8);
1386 DUMPBITS (bit_buf
, bits
, 8);
1390 DUMPBITS (bit_buf
, bits
, 12);
1391 NEEDBITS (bit_buf
, bits
, bit_ptr
);
1392 DUMPBITS (bit_buf
, bits
, 12);
1395 NEEDBITS (bit_buf
, bits
, bit_ptr
);
1398 else if (bit_buf
>= 0x02000000)
1400 tab
= DCT_B14_10
+ (UBITS (bit_buf
, 10) - 8);
1405 else if (bit_buf
>= 0x00800000)
1407 tab
= DCT_13
+ (UBITS (bit_buf
, 13) - 16);
1412 else if (bit_buf
>= 0x00200000)
1414 tab
= DCT_15
+ (UBITS (bit_buf
, 15) - 16);
1421 tab
= DCT_16
+ UBITS (bit_buf
, 16);
1423 GETWORD (bit_buf
, bits
+ 16, bit_ptr
);
1428 break; /* illegal, check needed to avoid buffer overflow */
1431 DUMPBITS (bit_buf
, bits
, 2); /* dump end of block code */
1432 decoder
->bitstream_buf
= bit_buf
;
1433 decoder
->bitstream_bits
= bits
;
1434 decoder
->bitstream_ptr
= bit_ptr
;
1437 static void skip_chroma_dc_dct_diff (mpeg2_decoder_t
* const decoder
)
1439 #define bit_buf (decoder->bitstream_buf)
1440 #define bits (decoder->bitstream_bits)
1441 #define bit_ptr (decoder->bitstream_ptr)
1446 if (bit_buf
< 0xf8000000)
1448 tab
= DC_chrom_5
+ UBITS (bit_buf
, 5);
1453 bits
+= tab
->len
+ size
;
1454 bit_buf
<<= tab
->len
;
1459 DUMPBITS (bit_buf
, bits
, 2);
1464 tab
= DC_long
+ (UBITS (bit_buf
, 10) - 0x3e0);
1466 DUMPBITS (bit_buf
, bits
, tab
->len
+ 1);
1467 NEEDBITS (bit_buf
, bits
, bit_ptr
);
1468 DUMPBITS (bit_buf
, bits
, size
);
1476 static void skip_chroma_non_intra (mpeg2_decoder_t
* const decoder
,
1477 uint32_t coded_block_pattern
)
1479 static const uint32_t cbp_mask
[3] =
1486 uint32_t cbp
= coded_block_pattern
&
1487 cbp_mask
[MIN((unsigned)decoder
->chroma_format
, 2u)];
1491 skip_non_intra_block (decoder
);
1496 static void skip_chroma_intra (mpeg2_decoder_t
* const decoder
)
1498 #define bit_buf (decoder->bitstream_buf)
1499 #define bits (decoder->bitstream_bits)
1500 #define bit_ptr (decoder->bitstream_ptr)
1501 int i
= 2 << decoder
->chroma_format
;
1503 if ((unsigned)i
> 8)
1508 NEEDBITS (bit_buf
, bits
, bit_ptr
);
1510 skip_chroma_dc_dct_diff (decoder
);
1514 if (decoder
->coding_type
!= D_TYPE
)
1515 skip_mpeg1_intra_block (decoder
);
1517 else if (decoder
->intra_vlc_format
)
1519 skip_intra_block_B15 (decoder
);
1523 skip_intra_block_B14 (decoder
);
1527 if (decoder
->chroma_format
== 0 && decoder
->coding_type
== D_TYPE
)
1529 NEEDBITS (bit_buf
, bits
, bit_ptr
);
1530 DUMPBITS (bit_buf
, bits
, 1);
1537 #endif /* !MPEG2_COLOR */
1539 #define MOTION_420(table, ref, motion_x, motion_y, size, y) \
1540 pos_x = 2 * decoder->offset + motion_x; \
1541 pos_y = 2 * decoder->v_offset + motion_y + 2 * y; \
1543 if (unlikely (pos_x > decoder->limit_x)) \
1545 pos_x = ((int)pos_x < 0) ? 0 : decoder->limit_x; \
1546 motion_x = pos_x - 2 * decoder->offset; \
1549 if (unlikely (pos_y > decoder->limit_y_ ## size)) \
1551 pos_y = ((int)pos_y < 0) ? 0 : decoder->limit_y_ ## size; \
1552 motion_y = pos_y - 2 * decoder->v_offset - 2 * y; \
1555 xy_half = ((pos_y & 1) << 1) | (pos_x & 1); \
1556 table[xy_half] (decoder->dest[0] + y * decoder->stride + decoder->offset, \
1557 ref[0] + (pos_x >> 1) + (pos_y >> 1) * decoder->stride, \
1558 decoder->stride, size); \
1564 xy_half = ((motion_y & 1) << 1) | (motion_x & 1); \
1565 offset = ((decoder->offset + motion_x) >> 1) + \
1566 ((((decoder->v_offset + motion_y) >> 1) + y/2) * \
1567 decoder->uv_stride); \
1569 table[4+xy_half] (decoder->dest[1] + y/2 * decoder->uv_stride + \
1570 (decoder->offset >> 1), ref[1] + offset, \
1571 decoder->uv_stride, size/2); \
1572 table[4+xy_half] (decoder->dest[2] + y/2 * decoder->uv_stride + \
1573 (decoder->offset >> 1), ref[2] + offset, \
1574 decoder->uv_stride, size/2); \
1577 #define MOTION_FIELD_420(table, ref, motion_x, motion_y, \
1578 dest_field, op, src_field) \
1579 pos_x = 2 * decoder->offset + motion_x; \
1580 pos_y = decoder->v_offset + motion_y; \
1582 if (unlikely (pos_x > decoder->limit_x)) \
1584 pos_x = ((int)pos_x < 0) ? 0 : decoder->limit_x; \
1585 motion_x = pos_x - 2 * decoder->offset; \
1588 if (unlikely (pos_y > decoder->limit_y)) \
1590 pos_y = ((int)pos_y < 0) ? 0 : decoder->limit_y; \
1591 motion_y = pos_y - decoder->v_offset; \
1594 xy_half = ((pos_y & 1) << 1) | (pos_x & 1); \
1595 table[xy_half] (decoder->dest[0] + dest_field * decoder->stride + \
1597 (ref[0] + (pos_x >> 1) + \
1598 ((pos_y op) + src_field) * decoder->stride), \
1599 2 * decoder->stride, 8); \
1605 xy_half = ((motion_y & 1) << 1) | (motion_x & 1); \
1606 offset = ((decoder->offset + motion_x) >> 1) + \
1607 (((decoder->v_offset >> 1) + (motion_y op) + src_field) * \
1608 decoder->uv_stride); \
1610 table[4+xy_half] (decoder->dest[1] + dest_field * decoder->uv_stride + \
1611 (decoder->offset >> 1), ref[1] + offset, \
1612 2 * decoder->uv_stride, 4); \
1613 table[4+xy_half] (decoder->dest[2] + dest_field * decoder->uv_stride + \
1614 (decoder->offset >> 1), ref[2] + offset, \
1615 2 * decoder->uv_stride, 4); \
1618 #define MOTION_DMV_420(table, ref, motion_x, motion_y) \
1619 pos_x = 2 * decoder->offset + motion_x; \
1620 pos_y = decoder->v_offset + motion_y; \
1622 if (unlikely (pos_x > decoder->limit_x)) \
1624 pos_x = ((int)pos_x < 0) ? 0 : decoder->limit_x; \
1625 motion_x = pos_x - 2 * decoder->offset; \
1628 if (unlikely (pos_y > decoder->limit_y)) \
1630 pos_y = ((int)pos_y < 0) ? 0 : decoder->limit_y; \
1631 motion_y = pos_y - decoder->v_offset; \
1634 xy_half = ((pos_y & 1) << 1) | (pos_x & 1); \
1635 offset = (pos_x >> 1) + (pos_y & ~1) * decoder->stride; \
1636 table[xy_half] (decoder->dest[0] + decoder->offset, \
1637 ref[0] + offset, 2 * decoder->stride, 8); \
1638 table[xy_half] (decoder->dest[0] + decoder->stride + decoder->offset, \
1639 ref[0] + decoder->stride + offset, \
1640 2 * decoder->stride, 8); \
1646 xy_half = ((motion_y & 1) << 1) | (motion_x & 1); \
1647 offset = ((decoder->offset + motion_x) >> 1) + \
1648 (((decoder->v_offset >> 1) + (motion_y & ~1)) * \
1649 decoder->uv_stride); \
1651 table[4+xy_half] (decoder->dest[1] + (decoder->offset >> 1), \
1652 ref[1] + offset, 2 * decoder->uv_stride, 4); \
1653 table[4+xy_half] (decoder->dest[1] + decoder->uv_stride + \
1654 (decoder->offset >> 1), \
1655 ref[1] + decoder->uv_stride + offset, \
1656 2 * decoder->uv_stride, 4); \
1657 table[4+xy_half] (decoder->dest[2] + (decoder->offset >> 1), \
1658 ref[2] + offset, 2 * decoder->uv_stride, 4); \
1659 table[4+xy_half] (decoder->dest[2] + decoder->uv_stride + \
1660 (decoder->offset >> 1), \
1661 ref[2] + decoder->uv_stride + offset, \
1662 2 * decoder->uv_stride, 4); \
1665 #define MOTION_ZERO_420(table, ref) \
1666 table[0] (decoder->dest[0] + decoder->offset, \
1667 (ref[0] + decoder->offset + \
1668 decoder->v_offset * decoder->stride), decoder->stride, 16); \
1672 offset = ((decoder->offset >> 1) + \
1673 (decoder->v_offset >> 1) * decoder->uv_stride); \
1675 table[4] (decoder->dest[1] + (decoder->offset >> 1), \
1676 ref[1] + offset, decoder->uv_stride, 8); \
1677 table[4] (decoder->dest[2] + (decoder->offset >> 1), \
1678 ref[2] + offset, decoder->uv_stride, 8); \
1681 #define MOTION_422(table, ref, motion_x, motion_y, size, y) \
1682 pos_x = 2 * decoder->offset + motion_x; \
1683 pos_y = 2 * decoder->v_offset + motion_y + 2 * y; \
1685 if (unlikely (pos_x > decoder->limit_x)) \
1687 pos_x = ((int)pos_x < 0) ? 0 : decoder->limit_x; \
1688 motion_x = pos_x - 2 * decoder->offset; \
1691 if (unlikely (pos_y > decoder->limit_y_ ## size)) \
1693 pos_y = ((int)pos_y < 0) ? 0 : decoder->limit_y_ ## size; \
1694 motion_y = pos_y - 2 * decoder->v_offset - 2 * y; \
1697 xy_half = ((pos_y & 1) << 1) | (pos_x & 1); \
1698 offset = (pos_x >> 1) + (pos_y >> 1) * decoder->stride; \
1700 table[xy_half] (decoder->dest[0] + y * decoder->stride + decoder->offset, \
1701 ref[0] + offset, decoder->stride, size); \
1705 offset = (offset + (motion_x & (motion_x < 0))) >> 1; \
1707 xy_half = ((pos_y & 1) << 1) | (motion_x & 1); \
1709 table[4+xy_half] (decoder->dest[1] + y * decoder->uv_stride + \
1710 (decoder->offset >> 1), ref[1] + offset, \
1711 decoder->uv_stride, size); \
1712 table[4+xy_half] (decoder->dest[2] + y * decoder->uv_stride + \
1713 (decoder->offset >> 1), ref[2] + offset, \
1714 decoder->uv_stride, size); \
1717 #define MOTION_FIELD_422(table, ref, motion_x, motion_y, \
1718 dest_field, op, src_field) \
1719 pos_x = 2 * decoder->offset + motion_x; \
1720 pos_y = decoder->v_offset + motion_y; \
1722 if (unlikely (pos_x > decoder->limit_x)) \
1724 pos_x = ((int)pos_x < 0) ? 0 : decoder->limit_x; \
1725 motion_x = pos_x - 2 * decoder->offset; \
1728 if (unlikely (pos_y > decoder->limit_y)) \
1730 pos_y = ((int)pos_y < 0) ? 0 : decoder->limit_y; \
1731 motion_y = pos_y - decoder->v_offset; \
1734 xy_half = ((pos_y & 1) << 1) | (pos_x & 1); \
1735 offset = (pos_x >> 1) + ((pos_y op) + src_field) * decoder->stride; \
1737 table[xy_half] (decoder->dest[0] + dest_field * decoder->stride + \
1738 decoder->offset, ref[0] + offset, \
1739 2 * decoder->stride, 8); \
1743 offset = (offset + (motion_x & (motion_x < 0))) >> 1; \
1745 xy_half = ((pos_y & 1) << 1) | (motion_x & 1); \
1747 table[4+xy_half] (decoder->dest[1] + dest_field * decoder->uv_stride + \
1748 (decoder->offset >> 1), ref[1] + offset, \
1749 2 * decoder->uv_stride, 8); \
1750 table[4+xy_half] (decoder->dest[2] + dest_field * decoder->uv_stride + \
1751 (decoder->offset >> 1), ref[2] + offset, \
1752 2 * decoder->uv_stride, 8); \
1755 #define MOTION_DMV_422(table, ref, motion_x, motion_y) \
1756 pos_x = 2 * decoder->offset + motion_x; \
1757 pos_y = decoder->v_offset + motion_y; \
1759 if (unlikely (pos_x > decoder->limit_x)) \
1761 pos_x = ((int)pos_x < 0) ? 0 : decoder->limit_x; \
1762 motion_x = pos_x - 2 * decoder->offset; \
1765 if (unlikely (pos_y > decoder->limit_y)) \
1767 pos_y = ((int)pos_y < 0) ? 0 : decoder->limit_y; \
1768 motion_y = pos_y - decoder->v_offset; \
1771 xy_half = ((pos_y & 1) << 1) | (pos_x & 1); \
1772 offset = (pos_x >> 1) + (pos_y & ~1) * decoder->stride; \
1774 table[xy_half] (decoder->dest[0] + decoder->offset, \
1775 ref[0] + offset, 2 * decoder->stride, 8); \
1776 table[xy_half] (decoder->dest[0] + decoder->stride + decoder->offset, \
1777 ref[0] + decoder->stride + offset, \
1778 2 * decoder->stride, 8); \
1782 offset = (offset + (motion_x & (motion_x < 0))) >> 1; \
1784 xy_half = ((pos_y & 1) << 1) | (motion_x & 1); \
1786 table[4+xy_half] (decoder->dest[1] + (decoder->offset >> 1), \
1787 ref[1] + offset, 2 * decoder->uv_stride, 8); \
1788 table[4+xy_half] (decoder->dest[1] + decoder->uv_stride + \
1789 (decoder->offset >> 1), \
1790 ref[1] + decoder->uv_stride + offset, \
1791 2 * decoder->uv_stride, 8); \
1792 table[4+xy_half] (decoder->dest[2] + (decoder->offset >> 1), \
1793 ref[2] + offset, 2 * decoder->uv_stride, 8); \
1794 table[4+xy_half] (decoder->dest[2] + decoder->uv_stride + \
1795 (decoder->offset >> 1), \
1796 ref[2] + decoder->uv_stride + offset, \
1797 2 * decoder->uv_stride, 8); \
1800 #define MOTION_ZERO_422(table, ref) \
1801 offset = decoder->offset + decoder->v_offset * decoder->stride; \
1802 table[0] (decoder->dest[0] + decoder->offset, \
1803 ref[0] + offset, decoder->stride, 16); \
1808 table[4] (decoder->dest[1] + (decoder->offset >> 1), \
1809 ref[1] + offset, decoder->uv_stride, 16); \
1810 table[4] (decoder->dest[2] + (decoder->offset >> 1), \
1811 ref[2] + offset, decoder->uv_stride, 16); \
1814 #define MOTION_444(table, ref, motion_x, motion_y, size, y) \
1815 pos_x = 2 * decoder->offset + motion_x; \
1816 pos_y = 2 * decoder->v_offset + motion_y + 2 * y; \
1818 if (unlikely (pos_x > decoder->limit_x)) \
1820 pos_x = ((int)pos_x < 0) ? 0 : decoder->limit_x; \
1821 motion_x = pos_x - 2 * decoder->offset; \
1824 if (unlikely (pos_y > decoder->limit_y_ ## size)) \
1826 pos_y = ((int)pos_y < 0) ? 0 : decoder->limit_y_ ## size; \
1827 motion_y = pos_y - 2 * decoder->v_offset - 2 * y; \
1830 xy_half = ((pos_y & 1) << 1) | (pos_x & 1); \
1831 offset = (pos_x >> 1) + (pos_y >> 1) * decoder->stride; \
1833 table[xy_half] (decoder->dest[0] + y * decoder->stride + decoder->offset, \
1834 ref[0] + offset, decoder->stride, size); \
1838 table[xy_half] (decoder->dest[1] + y * decoder->stride + decoder->offset, \
1839 ref[1] + offset, decoder->stride, size); \
1840 table[xy_half] (decoder->dest[2] + y * decoder->stride + decoder->offset, \
1841 ref[2] + offset, decoder->stride, size); \
1844 #define MOTION_FIELD_444(table, ref, motion_x, motion_y, \
1845 dest_field, op, src_field) \
1846 pos_x = 2 * decoder->offset + motion_x; \
1847 pos_y = decoder->v_offset + motion_y; \
1849 if (unlikely (pos_x > decoder->limit_x)) \
1851 pos_x = ((int)pos_x < 0) ? 0 : decoder->limit_x; \
1852 motion_x = pos_x - 2 * decoder->offset; \
1855 if (unlikely (pos_y > decoder->limit_y)) \
1857 pos_y = ((int)pos_y < 0) ? 0 : decoder->limit_y; \
1858 motion_y = pos_y - decoder->v_offset; \
1861 xy_half = ((pos_y & 1) << 1) | (pos_x & 1); \
1862 offset = (pos_x >> 1) + ((pos_y op) + src_field) * decoder->stride; \
1864 table[xy_half] (decoder->dest[0] + dest_field * decoder->stride + \
1865 decoder->offset, ref[0] + offset, \
1866 2 * decoder->stride, 8); \
1870 table[xy_half] (decoder->dest[1] + dest_field * decoder->stride + \
1871 decoder->offset, ref[1] + offset, \
1872 2 * decoder->stride, 8); \
1873 table[xy_half] (decoder->dest[2] + dest_field * decoder->stride + \
1874 decoder->offset, ref[2] + offset, \
1875 2 * decoder->stride, 8); \
1878 #define MOTION_DMV_444(table, ref, motion_x, motion_y) \
1879 pos_x = 2 * decoder->offset + motion_x; \
1880 pos_y = decoder->v_offset + motion_y; \
1882 if (unlikely (pos_x > decoder->limit_x)) \
1884 pos_x = ((int)pos_x < 0) ? 0 : decoder->limit_x; \
1885 motion_x = pos_x - 2 * decoder->offset; \
1888 if (unlikely (pos_y > decoder->limit_y)) \
1890 pos_y = ((int)pos_y < 0) ? 0 : decoder->limit_y; \
1891 motion_y = pos_y - decoder->v_offset; \
1894 xy_half = ((pos_y & 1) << 1) | (pos_x & 1); \
1895 offset = (pos_x >> 1) + (pos_y & ~1) * decoder->stride; \
1897 table[xy_half] (decoder->dest[0] + decoder->offset, \
1898 ref[0] + offset, 2 * decoder->stride, 8); \
1899 table[xy_half] (decoder->dest[0] + decoder->stride + decoder->offset, \
1900 ref[0] + decoder->stride + offset, \
1901 2 * decoder->stride, 8); \
1905 table[xy_half] (decoder->dest[1] + decoder->offset, \
1906 ref[1] + offset, 2 * decoder->stride, 8); \
1907 table[xy_half] (decoder->dest[1] + decoder->stride + decoder->offset, \
1908 ref[1] + decoder->stride + offset, \
1909 2 * decoder->stride, 8); \
1910 table[xy_half] (decoder->dest[2] + decoder->offset, \
1911 ref[2] + offset, 2 * decoder->stride, 8); \
1912 table[xy_half] (decoder->dest[2] + decoder->stride + decoder->offset, \
1913 ref[2] + decoder->stride + offset, \
1914 2 * decoder->stride, 8); \
1917 #define MOTION_ZERO_444(table, ref) \
1918 offset = decoder->offset + decoder->v_offset * decoder->stride; \
1920 table[0] (decoder->dest[0] + decoder->offset, \
1921 ref[0] + offset, decoder->stride, 16); \
1925 table[4] (decoder->dest[1] + decoder->offset, \
1926 ref[1] + offset, decoder->stride, 16); \
1927 table[4] (decoder->dest[2] + (decoder->offset >> 1), \
1928 ref[2] + offset, decoder->stride, 16); \
1931 #define bit_buf (decoder->bitstream_buf)
1932 #define bits (decoder->bitstream_bits)
1933 #define bit_ptr (decoder->bitstream_ptr)
1935 static void motion_mp1 (mpeg2_decoder_t
* const decoder
,
1936 motion_t
* const motion
,
1937 mpeg2_mc_fct
* const * const table
)
1939 int motion_x
, motion_y
;
1940 unsigned int pos_x
, pos_y
, xy_half
, offset
;
1942 NEEDBITS (bit_buf
, bits
, bit_ptr
);
1943 motion_x
= motion
->pmv
[0][0] +
1944 (get_motion_delta (decoder
,
1945 motion
->f_code
[0]) << motion
->f_code
[1]);
1946 motion_x
= bound_motion_vector (motion_x
,
1947 motion
->f_code
[0] + motion
->f_code
[1]);
1948 motion
->pmv
[0][0] = motion_x
;
1950 NEEDBITS (bit_buf
, bits
, bit_ptr
);
1951 motion_y
= motion
->pmv
[0][1] +
1952 (get_motion_delta (decoder
,
1953 motion
->f_code
[0]) << motion
->f_code
[1]);
1954 motion_y
= bound_motion_vector (motion_y
,
1955 motion
->f_code
[0] + motion
->f_code
[1]);
1956 motion
->pmv
[0][1] = motion_y
;
1958 MOTION_420 (table
, motion
->ref
[0], motion_x
, motion_y
, 16, 0);
1961 #define MOTION_FUNCTIONS(FORMAT, MOTION, MOTION_FIELD, \
1962 MOTION_DMV, MOTION_ZERO) \
1964 static void motion_fr_frame_##FORMAT (mpeg2_decoder_t * const decoder, \
1965 motion_t * const motion, \
1966 mpeg2_mc_fct * const * const table) \
1968 int motion_x, motion_y; \
1969 unsigned int pos_x, pos_y, xy_half, offset; \
1971 NEEDBITS (bit_buf, bits, bit_ptr); \
1972 motion_x = motion->pmv[0][0] + get_motion_delta (decoder, \
1973 motion->f_code[0]); \
1974 motion_x = bound_motion_vector (motion_x, motion->f_code[0]); \
1975 motion->pmv[1][0] = motion->pmv[0][0] = motion_x; \
1977 NEEDBITS (bit_buf, bits, bit_ptr); \
1978 motion_y = motion->pmv[0][1] + get_motion_delta (decoder, \
1979 motion->f_code[1]); \
1980 motion_y = bound_motion_vector (motion_y, motion->f_code[1]); \
1981 motion->pmv[1][1] = motion->pmv[0][1] = motion_y; \
1983 MOTION (table, motion->ref[0], motion_x, motion_y, 16, 0); \
1986 static void motion_fr_field_##FORMAT (mpeg2_decoder_t * const decoder, \
1987 motion_t * const motion, \
1988 mpeg2_mc_fct * const * const table) \
1990 int motion_x, motion_y, field; \
1991 unsigned int pos_x, pos_y, xy_half, offset; \
1993 NEEDBITS (bit_buf, bits, bit_ptr); \
1994 field = UBITS (bit_buf, 1); \
1995 DUMPBITS (bit_buf, bits, 1); \
1997 motion_x = motion->pmv[0][0] + get_motion_delta (decoder, \
1998 motion->f_code[0]); \
1999 motion_x = bound_motion_vector (motion_x, motion->f_code[0]); \
2000 motion->pmv[0][0] = motion_x; \
2002 NEEDBITS (bit_buf, bits, bit_ptr); \
2003 motion_y = ((motion->pmv[0][1] >> 1) + \
2004 get_motion_delta (decoder, motion->f_code[1])); \
2005 /* motion_y = bound_motion_vector (motion_y, motion->f_code[1]); */ \
2006 motion->pmv[0][1] = motion_y << 1; \
2008 MOTION_FIELD (table, motion->ref[0], motion_x, motion_y, 0, & ~1, field); \
2010 NEEDBITS (bit_buf, bits, bit_ptr); \
2011 field = UBITS (bit_buf, 1); \
2012 DUMPBITS (bit_buf, bits, 1); \
2014 motion_x = motion->pmv[1][0] + get_motion_delta (decoder, \
2015 motion->f_code[0]); \
2016 motion_x = bound_motion_vector (motion_x, motion->f_code[0]); \
2017 motion->pmv[1][0] = motion_x; \
2019 NEEDBITS (bit_buf, bits, bit_ptr); \
2020 motion_y = ((motion->pmv[1][1] >> 1) + \
2021 get_motion_delta (decoder, motion->f_code[1])); \
2022 /* motion_y = bound_motion_vector (motion_y, motion->f_code[1]); */ \
2023 motion->pmv[1][1] = motion_y << 1; \
2025 MOTION_FIELD (table, motion->ref[0], motion_x, motion_y, 1, & ~1, field); \
2028 static void motion_fr_dmv_##FORMAT (mpeg2_decoder_t * const decoder, \
2029 motion_t * const motion, \
2030 mpeg2_mc_fct * const * const table) \
2032 int motion_x, motion_y, dmv_x, dmv_y, m, other_x, other_y; \
2033 unsigned int pos_x, pos_y, xy_half, offset; \
2036 NEEDBITS (bit_buf, bits, bit_ptr); \
2037 motion_x = motion->pmv[0][0] + get_motion_delta (decoder, \
2038 motion->f_code[0]); \
2039 motion_x = bound_motion_vector (motion_x, motion->f_code[0]); \
2040 motion->pmv[1][0] = motion->pmv[0][0] = motion_x; \
2041 NEEDBITS (bit_buf, bits, bit_ptr); \
2042 dmv_x = get_dmv (decoder); \
2044 motion_y = ((motion->pmv[0][1] >> 1) + \
2045 get_motion_delta (decoder, motion->f_code[1])); \
2046 /* motion_y = bound_motion_vector (motion_y, motion->f_code[1]); */ \
2047 motion->pmv[1][1] = motion->pmv[0][1] = motion_y << 1; \
2048 dmv_y = get_dmv (decoder); \
2050 m = decoder->top_field_first ? 1 : 3; \
2051 other_x = ((motion_x * m + (motion_x > 0)) >> 1) + dmv_x; \
2052 other_y = ((motion_y * m + (motion_y > 0)) >> 1) + dmv_y - 1; \
2053 MOTION_FIELD (mpeg2_mc.put, motion->ref[0], other_x, other_y, 0, | 1, 0); \
2055 m = decoder->top_field_first ? 3 : 1; \
2056 other_x = ((motion_x * m + (motion_x > 0)) >> 1) + dmv_x; \
2057 other_y = ((motion_y * m + (motion_y > 0)) >> 1) + dmv_y + 1; \
2058 MOTION_FIELD (mpeg2_mc.put, motion->ref[0], other_x, other_y, 1, & ~1, 0);\
2060 MOTION_DMV (mpeg2_mc.avg, motion->ref[0], motion_x, motion_y); \
2063 static void motion_reuse_##FORMAT (mpeg2_decoder_t * const decoder, \
2064 motion_t * const motion, \
2065 mpeg2_mc_fct * const * const table) \
2067 int motion_x, motion_y; \
2068 unsigned int pos_x, pos_y, xy_half, offset; \
2070 motion_x = motion->pmv[0][0]; \
2071 motion_y = motion->pmv[0][1]; \
2073 MOTION (table, motion->ref[0], motion_x, motion_y, 16, 0); \
2076 static void motion_zero_##FORMAT (mpeg2_decoder_t * const decoder, \
2077 motion_t * const motion, \
2078 mpeg2_mc_fct * const * const table) \
2080 unsigned int offset; \
2082 motion->pmv[0][0] = motion->pmv[0][1] = 0; \
2083 motion->pmv[1][0] = motion->pmv[1][1] = 0; \
2085 MOTION_ZERO (table, motion->ref[0]); \
2088 static void motion_fi_field_##FORMAT (mpeg2_decoder_t * const decoder, \
2089 motion_t * const motion, \
2090 mpeg2_mc_fct * const * const table) \
2092 int motion_x, motion_y; \
2093 uint8_t ** ref_field; \
2094 unsigned int pos_x, pos_y, xy_half, offset; \
2096 NEEDBITS (bit_buf, bits, bit_ptr); \
2097 ref_field = motion->ref2[UBITS (bit_buf, 1)]; \
2098 DUMPBITS (bit_buf, bits, 1); \
2100 motion_x = motion->pmv[0][0] + get_motion_delta (decoder, \
2101 motion->f_code[0]); \
2102 motion_x = bound_motion_vector (motion_x, motion->f_code[0]); \
2103 motion->pmv[1][0] = motion->pmv[0][0] = motion_x; \
2105 NEEDBITS (bit_buf, bits, bit_ptr); \
2106 motion_y = motion->pmv[0][1] + get_motion_delta (decoder, \
2107 motion->f_code[1]); \
2108 motion_y = bound_motion_vector (motion_y, motion->f_code[1]); \
2109 motion->pmv[1][1] = motion->pmv[0][1] = motion_y; \
2111 MOTION (table, ref_field, motion_x, motion_y, 16, 0); \
2114 static void motion_fi_16x8_##FORMAT (mpeg2_decoder_t * const decoder, \
2115 motion_t * const motion, \
2116 mpeg2_mc_fct * const * const table) \
2118 int motion_x, motion_y; \
2119 uint8_t ** ref_field; \
2120 unsigned int pos_x, pos_y, xy_half, offset; \
2122 NEEDBITS (bit_buf, bits, bit_ptr); \
2123 ref_field = motion->ref2[UBITS (bit_buf, 1)]; \
2124 DUMPBITS (bit_buf, bits, 1); \
2126 motion_x = motion->pmv[0][0] + get_motion_delta (decoder, \
2127 motion->f_code[0]); \
2128 motion_x = bound_motion_vector (motion_x, motion->f_code[0]); \
2129 motion->pmv[0][0] = motion_x; \
2131 NEEDBITS (bit_buf, bits, bit_ptr); \
2132 motion_y = motion->pmv[0][1] + get_motion_delta (decoder, \
2133 motion->f_code[1]); \
2134 motion_y = bound_motion_vector (motion_y, motion->f_code[1]); \
2135 motion->pmv[0][1] = motion_y; \
2137 MOTION (table, ref_field, motion_x, motion_y, 8, 0); \
2139 NEEDBITS (bit_buf, bits, bit_ptr); \
2140 ref_field = motion->ref2[UBITS (bit_buf, 1)]; \
2141 DUMPBITS (bit_buf, bits, 1); \
2143 motion_x = motion->pmv[1][0] + get_motion_delta (decoder, \
2144 motion->f_code[0]); \
2145 motion_x = bound_motion_vector (motion_x, motion->f_code[0]); \
2146 motion->pmv[1][0] = motion_x; \
2148 NEEDBITS (bit_buf, bits, bit_ptr); \
2149 motion_y = motion->pmv[1][1] + get_motion_delta (decoder, \
2150 motion->f_code[1]); \
2151 motion_y = bound_motion_vector (motion_y, motion->f_code[1]); \
2152 motion->pmv[1][1] = motion_y; \
2154 MOTION (table, ref_field, motion_x, motion_y, 8, 8); \
2157 static void motion_fi_dmv_##FORMAT (mpeg2_decoder_t * const decoder, \
2158 motion_t * const motion, \
2159 mpeg2_mc_fct * const * const table) \
2161 int motion_x, motion_y, other_x, other_y; \
2162 unsigned int pos_x, pos_y, xy_half, offset; \
2165 NEEDBITS (bit_buf, bits, bit_ptr); \
2166 motion_x = motion->pmv[0][0] + get_motion_delta (decoder, \
2167 motion->f_code[0]); \
2168 motion_x = bound_motion_vector (motion_x, motion->f_code[0]); \
2169 motion->pmv[1][0] = motion->pmv[0][0] = motion_x; \
2170 NEEDBITS (bit_buf, bits, bit_ptr); \
2171 other_x = ((motion_x + (motion_x > 0)) >> 1) + get_dmv (decoder); \
2173 motion_y = motion->pmv[0][1] + get_motion_delta (decoder, \
2174 motion->f_code[1]); \
2175 motion_y = bound_motion_vector (motion_y, motion->f_code[1]); \
2176 motion->pmv[1][1] = motion->pmv[0][1] = motion_y; \
2177 other_y = (((motion_y + (motion_y > 0)) >> 1) + get_dmv (decoder) + \
2178 decoder->dmv_offset); \
2180 MOTION (mpeg2_mc.put, motion->ref[0], motion_x, motion_y, 16, 0); \
2181 MOTION (mpeg2_mc.avg, motion->ref[1], other_x, other_y, 16, 0); \
2184 MOTION_FUNCTIONS (420, MOTION_420, MOTION_FIELD_420, MOTION_DMV_420,
2186 MOTION_FUNCTIONS (422, MOTION_422
, MOTION_FIELD_422
, MOTION_DMV_422
,
2188 MOTION_FUNCTIONS (444, MOTION_444
, MOTION_FIELD_444
, MOTION_DMV_444
,
2191 /* like motion_frame, but parsing without actual motion compensation */
2192 static void motion_fr_conceal (mpeg2_decoder_t
* const decoder
)
2196 NEEDBITS (bit_buf
, bits
, bit_ptr
);
2197 tmp
= (decoder
->f_motion
.pmv
[0][0] +
2198 get_motion_delta (decoder
, decoder
->f_motion
.f_code
[0]));
2199 tmp
= bound_motion_vector (tmp
, decoder
->f_motion
.f_code
[0]);
2200 decoder
->f_motion
.pmv
[1][0] = decoder
->f_motion
.pmv
[0][0] = tmp
;
2202 NEEDBITS (bit_buf
, bits
, bit_ptr
);
2203 tmp
= (decoder
->f_motion
.pmv
[0][1] +
2204 get_motion_delta (decoder
, decoder
->f_motion
.f_code
[1]));
2205 tmp
= bound_motion_vector (tmp
, decoder
->f_motion
.f_code
[1]);
2206 decoder
->f_motion
.pmv
[1][1] = decoder
->f_motion
.pmv
[0][1] = tmp
;
2208 DUMPBITS (bit_buf
, bits
, 1); /* remove marker_bit */
2211 static void motion_fi_conceal (mpeg2_decoder_t
* const decoder
)
2215 NEEDBITS (bit_buf
, bits
, bit_ptr
);
2216 DUMPBITS (bit_buf
, bits
, 1); /* remove field_select */
2218 tmp
= decoder
->f_motion
.pmv
[0][0] +
2219 get_motion_delta (decoder
, decoder
->f_motion
.f_code
[0]);
2220 tmp
= bound_motion_vector (tmp
, decoder
->f_motion
.f_code
[0]);
2222 decoder
->f_motion
.pmv
[1][0] =
2223 decoder
->f_motion
.pmv
[0][0] = tmp
;
2225 NEEDBITS (bit_buf
, bits
, bit_ptr
);
2227 tmp
= (decoder
->f_motion
.pmv
[0][1] +
2228 get_motion_delta (decoder
, decoder
->f_motion
.f_code
[1]));
2229 tmp
= bound_motion_vector (tmp
, decoder
->f_motion
.f_code
[1]);
2231 decoder
->f_motion
.pmv
[1][1] =
2232 decoder
->f_motion
.pmv
[0][1] = tmp
;
2234 DUMPBITS (bit_buf
, bits
, 1); /* remove marker_bit */
2241 #define MOTION_CALL(routine, direction) \
2243 if ((direction) & MACROBLOCK_MOTION_FORWARD) \
2244 routine (decoder, &decoder->f_motion, mpeg2_mc.put); \
2246 if ((direction) & MACROBLOCK_MOTION_BACKWARD) \
2248 routine (decoder, &decoder->b_motion, \
2249 ((direction) & MACROBLOCK_MOTION_FORWARD ? \
2250 mpeg2_mc.avg : mpeg2_mc.put)); \
2254 #define NEXT_MACROBLOCK \
2256 decoder->offset += 16; \
2258 if (decoder->offset == decoder->width) \
2260 do { /* just so we can use the break statement */ \
2261 if (decoder->convert) \
2263 decoder->convert (decoder->convert_id, decoder->dest, \
2264 decoder->v_offset); \
2265 if (decoder->coding_type == B_TYPE) \
2269 decoder->dest[0] += decoder->slice_stride; \
2272 decoder->dest[1] += decoder->slice_uv_stride; \
2273 decoder->dest[2] += decoder->slice_uv_stride; \
2277 decoder->v_offset += 16; \
2279 if (decoder->v_offset > decoder->limit_y) \
2282 decoder->offset = 0; \
2286 void mpeg2_init_fbuf (mpeg2_decoder_t
* decoder
,
2287 uint8_t * current_fbuf
[MPEG2_COMPONENTS
],
2288 uint8_t * forward_fbuf
[MPEG2_COMPONENTS
],
2289 uint8_t * backward_fbuf
[MPEG2_COMPONENTS
])
2291 int offset
, stride
, height
, bottom_field
;
2293 stride
= decoder
->stride_frame
;
2294 bottom_field
= (decoder
->picture_structure
== BOTTOM_FIELD
);
2295 offset
= bottom_field
? stride
: 0;
2296 height
= decoder
->height
;
2298 decoder
->picture_dest
[0] = current_fbuf
[0] + offset
;
2300 decoder
->picture_dest
[1] = current_fbuf
[1] + (offset
>> 1);
2301 decoder
->picture_dest
[2] = current_fbuf
[2] + (offset
>> 1);
2304 decoder
->f_motion
.ref
[0][0] = forward_fbuf
[0] + offset
;
2306 decoder
->f_motion
.ref
[0][1] = forward_fbuf
[1] + (offset
>> 1);
2307 decoder
->f_motion
.ref
[0][2] = forward_fbuf
[2] + (offset
>> 1);
2310 decoder
->b_motion
.ref
[0][0] = backward_fbuf
[0] + offset
;
2312 decoder
->b_motion
.ref
[0][1] = backward_fbuf
[1] + (offset
>> 1);
2313 decoder
->b_motion
.ref
[0][2] = backward_fbuf
[2] + (offset
>> 1);
2316 if (decoder
->picture_structure
!= FRAME_PICTURE
)
2318 decoder
->dmv_offset
= bottom_field
? 1 : -1;
2319 decoder
->f_motion
.ref2
[0] = decoder
->f_motion
.ref
[bottom_field
];
2320 decoder
->f_motion
.ref2
[1] = decoder
->f_motion
.ref
[!bottom_field
];
2321 decoder
->b_motion
.ref2
[0] = decoder
->b_motion
.ref
[bottom_field
];
2322 decoder
->b_motion
.ref2
[1] = decoder
->b_motion
.ref
[!bottom_field
];
2323 offset
= stride
- offset
;
2325 if (decoder
->second_field
&& (decoder
->coding_type
!= B_TYPE
))
2326 forward_fbuf
= current_fbuf
;
2328 decoder
->f_motion
.ref
[1][0] = forward_fbuf
[0] + offset
;
2330 decoder
->f_motion
.ref
[1][1] = forward_fbuf
[1] + (offset
>> 1);
2331 decoder
->f_motion
.ref
[1][2] = forward_fbuf
[2] + (offset
>> 1);
2333 decoder
->b_motion
.ref
[1][0] = backward_fbuf
[0] + offset
;
2335 decoder
->b_motion
.ref
[1][1] = backward_fbuf
[1] + (offset
>> 1);
2336 decoder
->b_motion
.ref
[1][2] = backward_fbuf
[2] + (offset
>> 1);
2342 decoder
->stride
= stride
;
2343 decoder
->slice_stride
= 16 * stride
;
2345 decoder
->uv_stride
= stride
>> 1;
2346 decoder
->slice_uv_stride
=
2347 decoder
->slice_stride
>> (2 - decoder
->chroma_format
);
2349 decoder
->limit_x
= 2 * decoder
->width
- 32;
2350 decoder
->limit_y_16
= 2 * height
- 32;
2351 decoder
->limit_y_8
= 2 * height
- 16;
2352 decoder
->limit_y
= height
- 16;
2356 decoder
->motion_parser
[0] = motion_zero_420
;
2357 decoder
->motion_parser
[MC_FRAME
] = motion_mp1
;
2358 decoder
->motion_parser
[4] = motion_reuse_420
;
2360 else if (decoder
->picture_structure
== FRAME_PICTURE
)
2362 if (decoder
->chroma_format
== 0)
2364 decoder
->motion_parser
[0] = motion_zero_420
;
2365 decoder
->motion_parser
[MC_FIELD
] = motion_fr_field_420
;
2366 decoder
->motion_parser
[MC_FRAME
] = motion_fr_frame_420
;
2367 decoder
->motion_parser
[MC_DMV
] = motion_fr_dmv_420
;
2368 decoder
->motion_parser
[4] = motion_reuse_420
;
2370 else if (decoder
->chroma_format
== 1)
2372 decoder
->motion_parser
[0] = motion_zero_422
;
2373 decoder
->motion_parser
[MC_FIELD
] = motion_fr_field_422
;
2374 decoder
->motion_parser
[MC_FRAME
] = motion_fr_frame_422
;
2375 decoder
->motion_parser
[MC_DMV
] = motion_fr_dmv_422
;
2376 decoder
->motion_parser
[4] = motion_reuse_422
;
2380 decoder
->motion_parser
[0] = motion_zero_444
;
2381 decoder
->motion_parser
[MC_FIELD
] = motion_fr_field_444
;
2382 decoder
->motion_parser
[MC_FRAME
] = motion_fr_frame_444
;
2383 decoder
->motion_parser
[MC_DMV
] = motion_fr_dmv_444
;
2384 decoder
->motion_parser
[4] = motion_reuse_444
;
2389 if (decoder
->chroma_format
== 0)
2391 decoder
->motion_parser
[0] = motion_zero_420
;
2392 decoder
->motion_parser
[MC_FIELD
] = motion_fi_field_420
;
2393 decoder
->motion_parser
[MC_16X8
] = motion_fi_16x8_420
;
2394 decoder
->motion_parser
[MC_DMV
] = motion_fi_dmv_420
;
2395 decoder
->motion_parser
[4] = motion_reuse_420
;
2397 else if (decoder
->chroma_format
== 1)
2399 decoder
->motion_parser
[0] = motion_zero_422
;
2400 decoder
->motion_parser
[MC_FIELD
] = motion_fi_field_422
;
2401 decoder
->motion_parser
[MC_16X8
] = motion_fi_16x8_422
;
2402 decoder
->motion_parser
[MC_DMV
] = motion_fi_dmv_422
;
2403 decoder
->motion_parser
[4] = motion_reuse_422
;
2407 decoder
->motion_parser
[0] = motion_zero_444
;
2408 decoder
->motion_parser
[MC_FIELD
] = motion_fi_field_444
;
2409 decoder
->motion_parser
[MC_16X8
] = motion_fi_16x8_444
;
2410 decoder
->motion_parser
[MC_DMV
] = motion_fi_dmv_444
;
2411 decoder
->motion_parser
[4] = motion_reuse_444
;
2416 static inline int slice_init (mpeg2_decoder_t
* const decoder
, int code
)
2418 #define bit_buf (decoder->bitstream_buf)
2419 #define bits (decoder->bitstream_bits)
2420 #define bit_ptr (decoder->bitstream_ptr)
2426 decoder
->dc_dct_pred
[0] = decoder
->dc_dct_pred
[1] =
2427 decoder
->dc_dct_pred
[2] = 16384;
2429 decoder
->dc_dct_pred
[0] = 16384;
2432 decoder
->f_motion
.pmv
[0][0] = decoder
->f_motion
.pmv
[0][1] = 0;
2433 decoder
->f_motion
.pmv
[1][0] = decoder
->f_motion
.pmv
[1][1] = 0;
2434 decoder
->b_motion
.pmv
[0][0] = decoder
->b_motion
.pmv
[0][1] = 0;
2435 decoder
->b_motion
.pmv
[1][0] = decoder
->b_motion
.pmv
[1][1] = 0;
2437 if (decoder
->vertical_position_extension
)
2439 code
+= UBITS (bit_buf
, 3) << 7;
2440 DUMPBITS (bit_buf
, bits
, 3);
2443 decoder
->v_offset
= (code
- 1) * 16;
2446 if (!(decoder
->convert
) || decoder
->coding_type
!= B_TYPE
)
2448 offset
= (code
- 1) * decoder
->slice_stride
;
2451 decoder
->dest
[0] = decoder
->picture_dest
[0] + offset
;
2453 offset
>>= (2 - decoder
->chroma_format
);
2454 decoder
->dest
[1] = decoder
->picture_dest
[1] + offset
;
2455 decoder
->dest
[2] = decoder
->picture_dest
[2] + offset
;
2458 get_quantizer_scale (decoder
);
2460 /* ignore intra_slice and all the extra data */
2461 while (bit_buf
& 0x80000000)
2463 DUMPBITS (bit_buf
, bits
, 9);
2464 NEEDBITS (bit_buf
, bits
, bit_ptr
);
2467 /* decode initial macroblock address increment */
2471 if (bit_buf
>= 0x08000000)
2473 mba
= MBA_5
+ (UBITS (bit_buf
, 6) - 2);
2476 else if (bit_buf
>= 0x01800000)
2478 mba
= MBA_11
+ (UBITS (bit_buf
, 12) - 24);
2483 switch (UBITS (bit_buf
, 12))
2485 case 8: /* macroblock_escape */
2487 DUMPBITS (bit_buf
, bits
, 11);
2488 NEEDBITS (bit_buf
, bits
, bit_ptr
);
2490 case 15: /* macroblock_stuffing (MPEG1 only) */
2492 DUMPBITS (bit_buf
, bits
, 11);
2493 NEEDBITS (bit_buf
, bits
, bit_ptr
);
2495 default: /* error */
2501 DUMPBITS (bit_buf
, bits
, mba
->len
+ 1);
2502 decoder
->offset
= (offset
+ mba
->mba
) << 4;
2504 while (decoder
->offset
- decoder
->width
>= 0)
2506 decoder
->offset
-= decoder
->width
;
2508 if (!(decoder
->convert
) || decoder
->coding_type
!= B_TYPE
)
2510 decoder
->dest
[0] += decoder
->slice_stride
;
2512 decoder
->dest
[1] += decoder
->slice_uv_stride
;
2513 decoder
->dest
[2] += decoder
->slice_uv_stride
;
2517 decoder
->v_offset
+= 16;
2520 if (decoder
->v_offset
> decoder
->limit_y
)
2530 void mpeg2_slice (mpeg2_decoder_t
* const decoder
, const int code
,
2531 const uint8_t * const buffer
)
2533 #define bit_buf (decoder->bitstream_buf)
2534 #define bits (decoder->bitstream_bits)
2535 #define bit_ptr (decoder->bitstream_ptr)
2537 bitstream_init (decoder
, buffer
);
2539 if (slice_init (decoder
, code
))
2544 int macroblock_modes
;
2548 NEEDBITS (bit_buf
, bits
, bit_ptr
);
2550 macroblock_modes
= get_macroblock_modes (decoder
);
2552 /* maybe integrate MACROBLOCK_QUANT test into get_macroblock_modes ? */
2553 if (macroblock_modes
& MACROBLOCK_QUANT
)
2554 get_quantizer_scale (decoder
);
2556 if (macroblock_modes
& MACROBLOCK_INTRA
)
2558 int DCT_offset
, DCT_stride
;
2562 if (decoder
->concealment_motion_vectors
)
2564 if (decoder
->picture_structure
== FRAME_PICTURE
)
2565 motion_fr_conceal (decoder
);
2567 motion_fi_conceal (decoder
);
2571 decoder
->f_motion
.pmv
[0][0] = decoder
->f_motion
.pmv
[0][1] = 0;
2572 decoder
->f_motion
.pmv
[1][0] = decoder
->f_motion
.pmv
[1][1] = 0;
2573 decoder
->b_motion
.pmv
[0][0] = decoder
->b_motion
.pmv
[0][1] = 0;
2574 decoder
->b_motion
.pmv
[1][0] = decoder
->b_motion
.pmv
[1][1] = 0;
2577 if (macroblock_modes
& DCT_TYPE_INTERLACED
)
2579 DCT_offset
= decoder
->stride
;
2580 DCT_stride
= decoder
->stride
* 2;
2584 DCT_offset
= decoder
->stride
* 8;
2585 DCT_stride
= decoder
->stride
;
2588 offset
= decoder
->offset
;
2589 dest_y
= decoder
->dest
[0] + offset
;
2590 slice_intra_DCT (decoder
, 0, dest_y
, DCT_stride
);
2591 slice_intra_DCT (decoder
, 0, dest_y
+ 8, DCT_stride
);
2592 slice_intra_DCT (decoder
, 0, dest_y
+ DCT_offset
, DCT_stride
);
2593 slice_intra_DCT (decoder
, 0, dest_y
+ DCT_offset
+ 8, DCT_stride
);
2596 if (likely (decoder
->chroma_format
== 0))
2598 slice_intra_DCT (decoder
, 1, decoder
->dest
[1] + (offset
>> 1),
2599 decoder
->uv_stride
);
2600 slice_intra_DCT (decoder
, 2, decoder
->dest
[2] + (offset
>> 1),
2601 decoder
->uv_stride
);
2603 if (decoder
->coding_type
== D_TYPE
)
2605 NEEDBITS (bit_buf
, bits
, bit_ptr
);
2606 DUMPBITS (bit_buf
, bits
, 1);
2609 else if (likely (decoder
->chroma_format
== 1))
2611 uint8_t * dest_u
= decoder
->dest
[1] + (offset
>> 1);
2612 uint8_t * dest_v
= decoder
->dest
[2] + (offset
>> 1);
2617 slice_intra_DCT (decoder
, 1, dest_u
, DCT_stride
);
2618 slice_intra_DCT (decoder
, 2, dest_v
, DCT_stride
);
2619 slice_intra_DCT (decoder
, 1, dest_u
+ DCT_offset
, DCT_stride
);
2620 slice_intra_DCT (decoder
, 2, dest_v
+ DCT_offset
, DCT_stride
);
2624 uint8_t * dest_u
= decoder
->dest
[1] + offset
;
2625 uint8_t * dest_v
= decoder
->dest
[2] + offset
;
2627 slice_intra_DCT (decoder
, 1, dest_u
, DCT_stride
);
2628 slice_intra_DCT (decoder
, 2, dest_v
, DCT_stride
);
2629 slice_intra_DCT (decoder
, 1, dest_u
+ DCT_offset
, DCT_stride
);
2630 slice_intra_DCT (decoder
, 2, dest_v
+ DCT_offset
, DCT_stride
);
2631 slice_intra_DCT (decoder
, 1, dest_u
+ 8, DCT_stride
);
2632 slice_intra_DCT (decoder
, 2, dest_v
+ 8, DCT_stride
);
2633 slice_intra_DCT (decoder
, 1, dest_u
+ DCT_offset
+ 8,
2635 slice_intra_DCT (decoder
, 2, dest_v
+ DCT_offset
+ 8,
2639 skip_chroma_intra(decoder
);
2640 #endif /* MPEG2_COLOR */
2644 motion_parser_t
* parser
;
2647 decoder
->motion_parser
[macroblock_modes
>> MOTION_TYPE_SHIFT
];
2648 MOTION_CALL (parser
, macroblock_modes
);
2650 if (macroblock_modes
& MACROBLOCK_PATTERN
)
2652 int coded_block_pattern
;
2653 int DCT_offset
, DCT_stride
;
2655 if (macroblock_modes
& DCT_TYPE_INTERLACED
)
2657 DCT_offset
= decoder
->stride
;
2658 DCT_stride
= decoder
->stride
* 2;
2662 DCT_offset
= decoder
->stride
* 8;
2663 DCT_stride
= decoder
->stride
;
2666 coded_block_pattern
= get_coded_block_pattern (decoder
);
2668 if (likely (decoder
->chroma_format
== 0))
2670 int offset
= decoder
->offset
;
2671 uint8_t * dest_y
= decoder
->dest
[0] + offset
;
2673 if (coded_block_pattern
& 1)
2674 slice_non_intra_DCT (decoder
, 0, dest_y
, DCT_stride
);
2676 if (coded_block_pattern
& 2)
2677 slice_non_intra_DCT (decoder
, 0, dest_y
+ 8,
2680 if (coded_block_pattern
& 4)
2681 slice_non_intra_DCT (decoder
, 0, dest_y
+ DCT_offset
,
2684 if (coded_block_pattern
& 8)
2685 slice_non_intra_DCT (decoder
, 0,
2686 dest_y
+ DCT_offset
+ 8,
2689 if (coded_block_pattern
& 16)
2690 slice_non_intra_DCT (decoder
, 1,
2691 decoder
->dest
[1] + (offset
>> 1),
2692 decoder
->uv_stride
);
2694 if (coded_block_pattern
& 32)
2695 slice_non_intra_DCT (decoder
, 2,
2696 decoder
->dest
[2] + (offset
>> 1),
2697 decoder
->uv_stride
);
2698 #endif /* MPEG2_COLOR */
2700 else if (likely (decoder
->chroma_format
== 1))
2705 coded_block_pattern
|= bit_buf
& (3 << 30);
2706 DUMPBITS (bit_buf
, bits
, 2);
2708 offset
= decoder
->offset
;
2709 dest_y
= decoder
->dest
[0] + offset
;
2711 if (coded_block_pattern
& 1)
2712 slice_non_intra_DCT (decoder
, 0, dest_y
, DCT_stride
);
2714 if (coded_block_pattern
& 2)
2715 slice_non_intra_DCT (decoder
, 0, dest_y
+ 8,
2718 if (coded_block_pattern
& 4)
2719 slice_non_intra_DCT (decoder
, 0, dest_y
+ DCT_offset
,
2722 if (coded_block_pattern
& 8)
2723 slice_non_intra_DCT (decoder
, 0,
2724 dest_y
+ DCT_offset
+ 8,
2728 DCT_offset
= (DCT_offset
+ offset
) >> 1;
2730 if (coded_block_pattern
& 16)
2731 slice_non_intra_DCT (decoder
, 1,
2732 decoder
->dest
[1] + (offset
>> 1),
2735 if (coded_block_pattern
& 32)
2736 slice_non_intra_DCT (decoder
, 2,
2737 decoder
->dest
[2] + (offset
>> 1),
2740 if (coded_block_pattern
& (2 << 30))
2741 slice_non_intra_DCT (decoder
, 1,
2742 decoder
->dest
[1] + DCT_offset
,
2745 if (coded_block_pattern
& (1 << 30))
2746 slice_non_intra_DCT (decoder
, 2,
2747 decoder
->dest
[2] + DCT_offset
,
2749 #endif /* MPEG2_COLOR */
2753 int offset
= decoder
->offset
;
2754 uint8_t * dest_y
= decoder
->dest
[0] + offset
;
2756 uint8_t * dest_u
= decoder
->dest
[1] + offset
;
2757 uint8_t * dest_v
= decoder
->dest
[2] + offset
;
2759 coded_block_pattern
|= bit_buf
& (63 << 26);
2760 DUMPBITS (bit_buf
, bits
, 6);
2762 if (coded_block_pattern
& 1)
2763 slice_non_intra_DCT (decoder
, 0, dest_y
, DCT_stride
);
2765 if (coded_block_pattern
& 2)
2766 slice_non_intra_DCT (decoder
, 0, dest_y
+ 8,
2769 if (coded_block_pattern
& 4)
2770 slice_non_intra_DCT (decoder
, 0, dest_y
+ DCT_offset
,
2773 if (coded_block_pattern
& 8)
2774 slice_non_intra_DCT (decoder
, 0,
2775 dest_y
+ DCT_offset
+ 8,
2778 if (coded_block_pattern
& 16)
2779 slice_non_intra_DCT (decoder
, 1, dest_u
, DCT_stride
);
2781 if (coded_block_pattern
& 32)
2782 slice_non_intra_DCT (decoder
, 2, dest_v
, DCT_stride
);
2784 if (coded_block_pattern
& (32 << 26))
2785 slice_non_intra_DCT (decoder
, 1, dest_u
+ DCT_offset
,
2788 if (coded_block_pattern
& (16 << 26))
2789 slice_non_intra_DCT (decoder
, 2, dest_v
+ DCT_offset
,
2792 if (coded_block_pattern
& (8 << 26))
2793 slice_non_intra_DCT (decoder
, 1, dest_u
+ 8,
2796 if (coded_block_pattern
& (4 << 26))
2797 slice_non_intra_DCT (decoder
, 2, dest_v
+ 8,
2800 if (coded_block_pattern
& (2 << 26))
2801 slice_non_intra_DCT (decoder
, 1,
2802 dest_u
+ DCT_offset
+ 8,
2805 if (coded_block_pattern
& (1 << 26))
2806 slice_non_intra_DCT (decoder
, 2,
2807 dest_v
+ DCT_offset
+ 8,
2809 #endif /* MPEG2_COLOR */
2812 skip_chroma_non_intra(decoder
, coded_block_pattern
);
2817 decoder
->dc_dct_pred
[0] = decoder
->dc_dct_pred
[1] =
2818 decoder
->dc_dct_pred
[2] = 16384;
2820 decoder
->dc_dct_pred
[0] = 16384;
2826 NEEDBITS (bit_buf
, bits
, bit_ptr
);
2831 if (bit_buf
>= 0x10000000)
2833 mba
= MBA_5
+ (UBITS (bit_buf
, 5) - 2);
2836 else if (bit_buf
>= 0x03000000)
2838 mba
= MBA_11
+ (UBITS (bit_buf
, 11) - 24);
2843 switch (UBITS (bit_buf
, 11))
2845 case 8: /* macroblock_escape */
2848 case 15: /* macroblock_stuffing (MPEG1 only) */
2849 DUMPBITS (bit_buf
, bits
, 11);
2850 NEEDBITS (bit_buf
, bits
, bit_ptr
);
2852 default: /* end of slice, or error */
2858 DUMPBITS (bit_buf
, bits
, mba
->len
);
2859 mba_inc
+= mba
->mba
;
2864 decoder
->dc_dct_pred
[0] = decoder
->dc_dct_pred
[1] =
2865 decoder
->dc_dct_pred
[2] = 16384;
2867 decoder
->dc_dct_pred
[0] = 16384;
2869 if (decoder
->coding_type
== P_TYPE
)
2873 MOTION_CALL (decoder
->motion_parser
[0],
2874 MACROBLOCK_MOTION_FORWARD
);
2883 MOTION_CALL (decoder
->motion_parser
[4], macroblock_modes
);