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
25 * libmpeg2 sync history:
26 * 2008-07-01 - CVS revision 1.55
31 #include "mpeg2dec_config.h"
34 #include "attributes.h"
35 #include "mpeg2_internal.h"
39 static inline int get_macroblock_modes (mpeg2_decoder_t
* const decoder
)
41 #define bit_buf (decoder->bitstream_buf)
42 #define bits (decoder->bitstream_bits)
43 #define bit_ptr (decoder->bitstream_ptr)
48 switch (decoder
->coding_type
)
51 tab
= MB_I
+ UBITS (bit_buf
, 1);
52 DUMPBITS (bit_buf
, bits
, tab
->len
);
53 macroblock_modes
= tab
->modes
;
55 if (!(decoder
->frame_pred_frame_dct
) &&
56 decoder
->picture_structure
== FRAME_PICTURE
)
58 macroblock_modes
|= UBITS (bit_buf
, 1) * DCT_TYPE_INTERLACED
;
59 DUMPBITS (bit_buf
, bits
, 1);
62 return macroblock_modes
;
65 tab
= MB_P
+ UBITS (bit_buf
, 5);
66 DUMPBITS (bit_buf
, bits
, tab
->len
);
67 macroblock_modes
= tab
->modes
;
69 if (decoder
->picture_structure
!= FRAME_PICTURE
)
71 if (macroblock_modes
& MACROBLOCK_MOTION_FORWARD
)
73 macroblock_modes
|= UBITS (bit_buf
, 2) << MOTION_TYPE_SHIFT
;
74 DUMPBITS (bit_buf
, bits
, 2);
77 return macroblock_modes
| MACROBLOCK_MOTION_FORWARD
;
79 else if (decoder
->frame_pred_frame_dct
)
81 if (macroblock_modes
& MACROBLOCK_MOTION_FORWARD
)
82 macroblock_modes
|= MC_FRAME
<< MOTION_TYPE_SHIFT
;
84 return macroblock_modes
| MACROBLOCK_MOTION_FORWARD
;
88 if (macroblock_modes
& MACROBLOCK_MOTION_FORWARD
)
90 macroblock_modes
|= UBITS (bit_buf
, 2) << MOTION_TYPE_SHIFT
;
91 DUMPBITS (bit_buf
, bits
, 2);
94 if (macroblock_modes
& (MACROBLOCK_INTRA
| MACROBLOCK_PATTERN
))
96 macroblock_modes
|= UBITS (bit_buf
, 1) * DCT_TYPE_INTERLACED
;
97 DUMPBITS (bit_buf
, bits
, 1);
100 return macroblock_modes
| MACROBLOCK_MOTION_FORWARD
;
104 tab
= MB_B
+ UBITS (bit_buf
, 6);
105 DUMPBITS (bit_buf
, bits
, tab
->len
);
106 macroblock_modes
= tab
->modes
;
108 if (decoder
->picture_structure
!= FRAME_PICTURE
)
110 if (! (macroblock_modes
& MACROBLOCK_INTRA
))
112 macroblock_modes
|= UBITS (bit_buf
, 2) << MOTION_TYPE_SHIFT
;
113 DUMPBITS (bit_buf
, bits
, 2);
116 return macroblock_modes
;
118 else if (decoder
->frame_pred_frame_dct
)
120 /* if (! (macroblock_modes & MACROBLOCK_INTRA)) */
121 macroblock_modes
|= MC_FRAME
<< MOTION_TYPE_SHIFT
;
122 return macroblock_modes
;
126 if (macroblock_modes
& MACROBLOCK_INTRA
)
129 macroblock_modes
|= UBITS (bit_buf
, 2) << MOTION_TYPE_SHIFT
;
130 DUMPBITS (bit_buf
, bits
, 2);
132 if (macroblock_modes
& (MACROBLOCK_INTRA
| MACROBLOCK_PATTERN
))
135 macroblock_modes
|= UBITS (bit_buf
, 1) * DCT_TYPE_INTERLACED
;
136 DUMPBITS (bit_buf
, bits
, 1);
138 return macroblock_modes
;
142 DUMPBITS (bit_buf
, bits
, 1);
143 return MACROBLOCK_INTRA
;
153 static inline void get_quantizer_scale (mpeg2_decoder_t
* const decoder
)
155 #define bit_buf (decoder->bitstream_buf)
156 #define bits (decoder->bitstream_bits)
157 #define bit_ptr (decoder->bitstream_ptr)
159 int quantizer_scale_code
;
161 quantizer_scale_code
= UBITS (bit_buf
, 5);
162 DUMPBITS (bit_buf
, bits
, 5);
164 decoder
->quantizer_matrix
[0] =
165 decoder
->quantizer_prescale
[0][quantizer_scale_code
];
167 decoder
->quantizer_matrix
[1] =
168 decoder
->quantizer_prescale
[1][quantizer_scale_code
];
170 decoder
->quantizer_matrix
[2] =
171 decoder
->chroma_quantizer
[0][quantizer_scale_code
];
173 decoder
->quantizer_matrix
[3] =
174 decoder
->chroma_quantizer
[1][quantizer_scale_code
];
180 static inline int get_motion_delta (mpeg2_decoder_t
* const decoder
,
183 #define bit_buf (decoder->bitstream_buf)
184 #define bits (decoder->bitstream_bits)
185 #define bit_ptr (decoder->bitstream_ptr)
191 if (bit_buf
& 0x80000000)
193 DUMPBITS (bit_buf
, bits
, 1);
196 else if (bit_buf
>= 0x0c000000)
198 tab
= MV_4
+ UBITS (bit_buf
, 4);
199 delta
= (tab
->delta
<< f_code
) + 1;
200 bits
+= tab
->len
+ f_code
+ 1;
201 bit_buf
<<= tab
->len
;
203 sign
= SBITS (bit_buf
, 1);
207 delta
+= UBITS (bit_buf
, f_code
);
210 return (delta
^ sign
) - sign
;
214 tab
= MV_10
+ UBITS (bit_buf
, 10);
215 delta
= (tab
->delta
<< f_code
) + 1;
216 bits
+= tab
->len
+ 1;
217 bit_buf
<<= tab
->len
;
219 sign
= SBITS (bit_buf
, 1);
224 NEEDBITS (bit_buf
, bits
, bit_ptr
);
225 delta
+= UBITS (bit_buf
, f_code
);
226 DUMPBITS (bit_buf
, bits
, f_code
);
229 return (delta
^ sign
) - sign
;
237 static inline int bound_motion_vector (const int vector
, const int f_code
)
239 return ((int32_t)vector
<< (27 - f_code
)) >> (27 - f_code
);
242 static inline int get_dmv (mpeg2_decoder_t
* const decoder
)
244 #define bit_buf (decoder->bitstream_buf)
245 #define bits (decoder->bitstream_bits)
246 #define bit_ptr (decoder->bitstream_ptr)
250 tab
= DMV_2
+ UBITS (bit_buf
, 2);
251 DUMPBITS (bit_buf
, bits
, tab
->len
);
259 static inline int get_coded_block_pattern (mpeg2_decoder_t
* const decoder
)
261 #define bit_buf (decoder->bitstream_buf)
262 #define bits (decoder->bitstream_bits)
263 #define bit_ptr (decoder->bitstream_ptr)
267 NEEDBITS (bit_buf
, bits
, bit_ptr
);
269 if (bit_buf
>= 0x20000000)
271 tab
= CBP_7
+ (UBITS (bit_buf
, 7) - 16);
272 DUMPBITS (bit_buf
, bits
, tab
->len
);
277 tab
= CBP_9
+ UBITS (bit_buf
, 9);
278 DUMPBITS (bit_buf
, bits
, tab
->len
);
287 static inline int get_luma_dc_dct_diff (mpeg2_decoder_t
* const decoder
)
289 #define bit_buf (decoder->bitstream_buf)
290 #define bits (decoder->bitstream_bits)
291 #define bit_ptr (decoder->bitstream_ptr)
297 if (bit_buf
< 0xf8000000)
299 tab
= DC_lum_5
+ UBITS (bit_buf
, 5);
304 bits
+= tab
->len
+ size
;
305 bit_buf
<<= tab
->len
;
307 UBITS (bit_buf
, size
) - UBITS (SBITS (~bit_buf
, 1), size
);
309 return dc_diff
<< decoder
->intra_dc_precision
;
313 DUMPBITS (bit_buf
, bits
, 3);
319 tab
= DC_long
+ (UBITS (bit_buf
, 9) - 0x1e0);
321 DUMPBITS (bit_buf
, bits
, tab
->len
);
322 NEEDBITS (bit_buf
, bits
, bit_ptr
);
323 dc_diff
= UBITS (bit_buf
, size
) - UBITS (SBITS (~bit_buf
, 1), size
);
324 DUMPBITS (bit_buf
, bits
, size
);
325 return dc_diff
<< decoder
->intra_dc_precision
;
334 static inline int get_chroma_dc_dct_diff (mpeg2_decoder_t
* const decoder
)
336 #define bit_buf (decoder->bitstream_buf)
337 #define bits (decoder->bitstream_bits)
338 #define bit_ptr (decoder->bitstream_ptr)
344 if (bit_buf
< 0xf8000000)
346 tab
= DC_chrom_5
+ UBITS (bit_buf
, 5);
351 bits
+= tab
->len
+ size
;
352 bit_buf
<<= tab
->len
;
354 UBITS (bit_buf
, size
) - UBITS (SBITS (~bit_buf
, 1), size
);
356 return dc_diff
<< decoder
->intra_dc_precision
;
360 DUMPBITS (bit_buf
, bits
, 2);
366 tab
= DC_long
+ (UBITS (bit_buf
, 10) - 0x3e0);
368 DUMPBITS (bit_buf
, bits
, tab
->len
+ 1);
369 NEEDBITS (bit_buf
, bits
, bit_ptr
);
370 dc_diff
= UBITS (bit_buf
, size
) - UBITS (SBITS (~bit_buf
, 1), size
);
371 DUMPBITS (bit_buf
, bits
, size
);
372 return dc_diff
<< decoder
->intra_dc_precision
;
379 #endif /* MPEG2_COLOR */
381 #define SATURATE(val) \
384 if (unlikely (val != (int16_t) val)) \
385 val = (SBITS (val, 1) ^ 2047) << 4; \
388 static void get_intra_block_B14 (mpeg2_decoder_t
* const decoder
,
389 const uint16_t * const quant_matrix
)
391 uint32_t bit_buf
= decoder
->bitstream_buf
;
392 int bits
= decoder
->bitstream_bits
;
393 const uint8_t * bit_ptr
= decoder
->bitstream_ptr
;
394 const uint8_t * const scan
= decoder
->scan
;
395 int16_t * const dest
= decoder
->DCTblock
;
396 int mismatch
= ~dest
[0];
402 NEEDBITS (bit_buf
, bits
, bit_ptr
);
406 if (bit_buf
>= 0x28000000)
408 tab
= DCT_B14AC_5
+ (UBITS (bit_buf
, 5) - 5);
412 break; /* end of block */
416 bit_buf
<<= tab
->len
;
417 bits
+= tab
->len
+ 1;
418 val
= (tab
->level
* quant_matrix
[j
]) >> 4;
420 /* if (bitstream_get (1)) val = -val; */
421 val
= (val
^ SBITS (bit_buf
, 1)) - SBITS (bit_buf
, 1);
428 NEEDBITS (bit_buf
, bits
, bit_ptr
);
432 else if (bit_buf
>= 0x04000000)
434 tab
= DCT_B14_8
+ (UBITS (bit_buf
, 8) - 4);
442 i
+= UBITS (bit_buf
<< 6, 6) - 64;
444 break; /* illegal, check needed to avoid buffer overflow */
448 DUMPBITS (bit_buf
, bits
, 12);
449 NEEDBITS (bit_buf
, bits
, bit_ptr
);
450 val
= (SBITS (bit_buf
, 12) * quant_matrix
[j
]) / 16;
456 DUMPBITS (bit_buf
, bits
, 12);
457 NEEDBITS (bit_buf
, bits
, bit_ptr
);
461 else if (bit_buf
>= 0x02000000)
463 tab
= DCT_B14_10
+ (UBITS (bit_buf
, 10) - 8);
468 else if (bit_buf
>= 0x00800000)
470 tab
= DCT_13
+ (UBITS (bit_buf
, 13) - 16);
475 else if (bit_buf
>= 0x00200000)
477 tab
= DCT_15
+ (UBITS (bit_buf
, 15) - 16);
484 tab
= DCT_16
+ UBITS (bit_buf
, 16);
486 GETWORD (bit_buf
, bits
+ 16, bit_ptr
);
491 break; /* illegal, check needed to avoid buffer overflow */
494 dest
[63] ^= mismatch
& 16;
495 DUMPBITS (bit_buf
, bits
, tab
->len
); /* dump end of block code */
496 decoder
->bitstream_buf
= bit_buf
;
497 decoder
->bitstream_bits
= bits
;
498 decoder
->bitstream_ptr
= bit_ptr
;
501 static void get_intra_block_B15 (mpeg2_decoder_t
* const decoder
,
502 const uint16_t * const quant_matrix
)
504 uint32_t bit_buf
= decoder
->bitstream_buf
;
505 int bits
= decoder
->bitstream_bits
;
506 const uint8_t * bit_ptr
= decoder
->bitstream_ptr
;
507 const uint8_t * const scan
= decoder
->scan
;
508 int16_t * const dest
= decoder
->DCTblock
;
509 int mismatch
= ~dest
[0];
515 NEEDBITS (bit_buf
, bits
, bit_ptr
);
519 if (bit_buf
>= 0x04000000)
521 tab
= DCT_B15_8
+ (UBITS (bit_buf
, 8) - 4);
529 bit_buf
<<= tab
->len
;
530 bits
+= tab
->len
+ 1;
531 val
= (tab
->level
* quant_matrix
[j
]) >> 4;
533 /* if (bitstream_get (1)) val = -val; */
534 val
= (val
^ SBITS (bit_buf
, 1)) - SBITS (bit_buf
, 1);
541 NEEDBITS (bit_buf
, bits
, bit_ptr
);
547 /* end of block. I commented out this code because if we */
548 /* dont exit here we will still exit at the later test :) */
550 /* if (i >= 128) break; */ /* end of block */
554 i
+= UBITS (bit_buf
<< 6, 6) - 64;
556 break; /* illegal, check against buffer overflow */
560 DUMPBITS (bit_buf
, bits
, 12);
561 NEEDBITS (bit_buf
, bits
, bit_ptr
);
562 val
= (SBITS (bit_buf
, 12) * quant_matrix
[j
]) / 16;
568 DUMPBITS (bit_buf
, bits
, 12);
569 NEEDBITS (bit_buf
, bits
, bit_ptr
);
574 else if (bit_buf
>= 0x02000000)
576 tab
= DCT_B15_10
+ (UBITS (bit_buf
, 10) - 8);
581 else if (bit_buf
>= 0x00800000)
583 tab
= DCT_13
+ (UBITS (bit_buf
, 13) - 16);
588 else if (bit_buf
>= 0x00200000)
590 tab
= DCT_15
+ (UBITS (bit_buf
, 15) - 16);
597 tab
= DCT_16
+ UBITS (bit_buf
, 16);
599 GETWORD (bit_buf
, bits
+ 16, bit_ptr
);
604 break; /* illegal, check needed to avoid buffer overflow */
607 dest
[63] ^= mismatch
& 16;
608 DUMPBITS (bit_buf
, bits
, tab
->len
); /* dump end of block code */
609 decoder
->bitstream_buf
= bit_buf
;
610 decoder
->bitstream_bits
= bits
;
611 decoder
->bitstream_ptr
= bit_ptr
;
614 static int get_non_intra_block (mpeg2_decoder_t
* const decoder
,
615 const uint16_t * const quant_matrix
)
617 uint32_t bit_buf
= decoder
->bitstream_buf
;
618 int bits
= decoder
->bitstream_bits
;
619 const uint8_t * bit_ptr
= decoder
->bitstream_ptr
;
620 const uint8_t * const scan
= decoder
->scan
;
621 int16_t * const dest
= decoder
->DCTblock
;
628 NEEDBITS (bit_buf
, bits
, bit_ptr
);
630 if (bit_buf
>= 0x28000000)
632 tab
= DCT_B14DC_5
+ (UBITS (bit_buf
, 5) - 5);
642 if (bit_buf
>= 0x28000000)
644 tab
= DCT_B14AC_5
+ (UBITS (bit_buf
, 5) - 5);
649 break; /* end of block */
653 bit_buf
<<= tab
->len
;
654 bits
+= tab
->len
+ 1;
655 val
= ((2 * tab
->level
+ 1) * quant_matrix
[j
]) >> 5;
657 /* if (bitstream_get (1)) val = -val; */
658 val
= (val
^ SBITS (bit_buf
, 1)) - SBITS (bit_buf
, 1);
665 NEEDBITS (bit_buf
, bits
, bit_ptr
);
671 if (bit_buf
>= 0x04000000)
673 tab
= DCT_B14_8
+ (UBITS (bit_buf
, 8) - 4);
681 i
+= UBITS (bit_buf
<< 6, 6) - 64;
683 break; /* illegal, check needed to avoid buffer overflow */
687 DUMPBITS (bit_buf
, bits
, 12);
688 NEEDBITS (bit_buf
, bits
, bit_ptr
);
689 val
= 2 * (SBITS (bit_buf
, 12) + SBITS (bit_buf
, 1)) + 1;
690 val
= (val
* quant_matrix
[j
]) / 32;
696 DUMPBITS (bit_buf
, bits
, 12);
697 NEEDBITS (bit_buf
, bits
, bit_ptr
);
701 else if (bit_buf
>= 0x02000000)
703 tab
= DCT_B14_10
+ (UBITS (bit_buf
, 10) - 8);
708 else if (bit_buf
>= 0x00800000)
710 tab
= DCT_13
+ (UBITS (bit_buf
, 13) - 16);
715 else if (bit_buf
>= 0x00200000)
717 tab
= DCT_15
+ (UBITS (bit_buf
, 15) - 16);
724 tab
= DCT_16
+ UBITS (bit_buf
, 16);
726 GETWORD (bit_buf
, bits
+ 16, bit_ptr
);
731 break; /* illegal, check needed to avoid buffer overflow */
734 dest
[63] ^= mismatch
& 16;
735 DUMPBITS (bit_buf
, bits
, tab
->len
); /* dump end of block code */
736 decoder
->bitstream_buf
= bit_buf
;
737 decoder
->bitstream_bits
= bits
;
738 decoder
->bitstream_ptr
= bit_ptr
;
742 static void get_mpeg1_intra_block (mpeg2_decoder_t
* const decoder
)
744 uint32_t bit_buf
= decoder
->bitstream_buf
;
745 int bits
= decoder
->bitstream_bits
;
746 const uint8_t * bit_ptr
= decoder
->bitstream_ptr
;
747 const uint8_t * const scan
= decoder
->scan
;
748 const uint16_t * const quant_matrix
= decoder
->quantizer_matrix
[0];
749 int16_t * const dest
= decoder
->DCTblock
;
755 NEEDBITS (bit_buf
, bits
, bit_ptr
);
759 if (bit_buf
>= 0x28000000)
761 tab
= DCT_B14AC_5
+ (UBITS (bit_buf
, 5) - 5);
765 break; /* end of block */
769 bit_buf
<<= tab
->len
;
770 bits
+= tab
->len
+ 1;
771 val
= (tab
->level
* quant_matrix
[j
]) >> 4;
776 /* if (bitstream_get (1)) val = -val; */
777 val
= (val
^ SBITS (bit_buf
, 1)) - SBITS (bit_buf
, 1);
783 NEEDBITS (bit_buf
, bits
, bit_ptr
);
787 else if (bit_buf
>= 0x04000000)
789 tab
= DCT_B14_8
+ (UBITS (bit_buf
, 8) - 4);
797 i
+= UBITS (bit_buf
<< 6, 6) - 64;
799 break; /* illegal, check needed to avoid buffer overflow */
803 DUMPBITS (bit_buf
, bits
, 12);
804 NEEDBITS (bit_buf
, bits
, bit_ptr
);
805 val
= SBITS (bit_buf
, 8);
809 DUMPBITS (bit_buf
, bits
, 8);
810 val
= UBITS (bit_buf
, 8) + 2 * val
;
813 val
= (val
* quant_matrix
[j
]) / 16;
816 val
= (val
+ ~SBITS (val
, 1)) | 1;
821 DUMPBITS (bit_buf
, bits
, 8);
822 NEEDBITS (bit_buf
, bits
, bit_ptr
);
826 else if (bit_buf
>= 0x02000000)
828 tab
= DCT_B14_10
+ (UBITS (bit_buf
, 10) - 8);
833 else if (bit_buf
>= 0x00800000)
835 tab
= DCT_13
+ (UBITS (bit_buf
, 13) - 16);
840 else if (bit_buf
>= 0x00200000)
842 tab
= DCT_15
+ (UBITS (bit_buf
, 15) - 16);
849 tab
= DCT_16
+ UBITS (bit_buf
, 16);
851 GETWORD (bit_buf
, bits
+ 16, bit_ptr
);
856 break; /* illegal, check needed to avoid buffer overflow */
859 DUMPBITS (bit_buf
, bits
, tab
->len
); /* dump end of block code */
860 decoder
->bitstream_buf
= bit_buf
;
861 decoder
->bitstream_bits
= bits
;
862 decoder
->bitstream_ptr
= bit_ptr
;
865 static int get_mpeg1_non_intra_block (mpeg2_decoder_t
* const decoder
)
867 uint32_t bit_buf
= decoder
->bitstream_buf
;
868 int bits
= decoder
->bitstream_bits
;
869 const uint8_t * bit_ptr
= decoder
->bitstream_ptr
;
870 const uint8_t * const scan
= decoder
->scan
;
871 const uint16_t * const quant_matrix
= decoder
->quantizer_matrix
[1];
872 int16_t * const dest
= decoder
->DCTblock
;
878 NEEDBITS (bit_buf
, bits
, bit_ptr
);
879 if (bit_buf
>= 0x28000000)
881 tab
= DCT_B14DC_5
+ (UBITS (bit_buf
, 5) - 5);
891 if (bit_buf
>= 0x28000000)
893 tab
= DCT_B14AC_5
+ (UBITS (bit_buf
, 5) - 5);
898 break; /* end of block */
902 bit_buf
<<= tab
->len
;
903 bits
+= tab
->len
+ 1;
904 val
= ((2 * tab
->level
+ 1) * quant_matrix
[j
]) >> 5;
909 /* if (bitstream_get (1)) val = -val; */
910 val
= (val
^ SBITS (bit_buf
, 1)) - SBITS (bit_buf
, 1);
916 NEEDBITS (bit_buf
, bits
, bit_ptr
);
922 if (bit_buf
>= 0x04000000)
924 tab
= DCT_B14_8
+ (UBITS (bit_buf
, 8) - 4);
932 i
+= UBITS (bit_buf
<< 6, 6) - 64;
934 break; /* illegal, check needed to avoid buffer overflow */
938 DUMPBITS (bit_buf
, bits
, 12);
939 NEEDBITS (bit_buf
, bits
, bit_ptr
);
940 val
= SBITS (bit_buf
, 8);
944 DUMPBITS (bit_buf
, bits
, 8);
945 val
= UBITS (bit_buf
, 8) + 2 * val
;
948 val
= 2 * (val
+ SBITS (val
, 1)) + 1;
949 val
= (val
* quant_matrix
[j
]) / 32;
952 val
= (val
+ ~SBITS (val
, 1)) | 1;
957 DUMPBITS (bit_buf
, bits
, 8);
958 NEEDBITS (bit_buf
, bits
, bit_ptr
);
963 else if (bit_buf
>= 0x02000000)
965 tab
= DCT_B14_10
+ (UBITS (bit_buf
, 10) - 8);
970 else if (bit_buf
>= 0x00800000)
972 tab
= DCT_13
+ (UBITS (bit_buf
, 13) - 16);
977 else if (bit_buf
>= 0x00200000)
979 tab
= DCT_15
+ (UBITS (bit_buf
, 15) - 16);
986 tab
= DCT_16
+ UBITS (bit_buf
, 16);
988 GETWORD (bit_buf
, bits
+ 16, bit_ptr
);
993 break; /* illegal, check needed to avoid buffer overflow */
996 DUMPBITS (bit_buf
, bits
, tab
->len
); /* dump end of block code */
997 decoder
->bitstream_buf
= bit_buf
;
998 decoder
->bitstream_bits
= bits
;
999 decoder
->bitstream_ptr
= bit_ptr
;
1003 static inline void slice_intra_DCT (mpeg2_decoder_t
* const decoder
,
1005 uint8_t * const dest
, const int stride
)
1007 #define bit_buf (decoder->bitstream_buf)
1008 #define bits (decoder->bitstream_bits)
1009 #define bit_ptr (decoder->bitstream_ptr)
1011 NEEDBITS (bit_buf
, bits
, bit_ptr
);
1012 /* Get the intra DC coefficient and inverse quantize it */
1015 decoder
->dc_dct_pred
[0] += get_luma_dc_dct_diff (decoder
);
1016 decoder
->DCTblock
[0] = decoder
->dc_dct_pred
[0];
1022 decoder
->dc_dct_pred
[cc
] += get_chroma_dc_dct_diff (decoder
);
1023 decoder
->DCTblock
[0] = decoder
->dc_dct_pred
[cc
];
1029 if (decoder
->coding_type
!= D_TYPE
)
1030 get_mpeg1_intra_block (decoder
);
1032 else if (decoder
->intra_vlc_format
)
1034 get_intra_block_B15 (decoder
, decoder
->quantizer_matrix
[cc
? 2 : 0]);
1038 get_intra_block_B14 (decoder
, decoder
->quantizer_matrix
[cc
? 2 : 0]);
1041 mpeg2_idct_copy (decoder
->DCTblock
, dest
, stride
);
1048 static inline void slice_non_intra_DCT (mpeg2_decoder_t
* const decoder
,
1050 uint8_t * const dest
, const int stride
)
1056 last
= get_mpeg1_non_intra_block (decoder
);
1060 last
= get_non_intra_block (decoder
,
1061 decoder
->quantizer_matrix
[cc
? 3 : 1]);
1064 mpeg2_idct_add (last
, decoder
->DCTblock
, dest
, stride
);
1068 static void skip_mpeg1_intra_block (mpeg2_decoder_t
* const decoder
)
1070 uint32_t bit_buf
= decoder
->bitstream_buf
;
1071 int bits
= decoder
->bitstream_bits
;
1072 const uint8_t * bit_ptr
= decoder
->bitstream_ptr
;
1076 NEEDBITS (bit_buf
, bits
, bit_ptr
);
1080 if (bit_buf
>= 0x28000000)
1082 tab
= DCT_B14AC_5
+ (UBITS (bit_buf
, 5) - 5);
1086 break; /* end of block */
1089 bit_buf
<<= tab
->len
+ 1;
1090 bits
+= tab
->len
+ 1;
1091 NEEDBITS (bit_buf
, bits
, bit_ptr
);
1094 else if (bit_buf
>= 0x04000000)
1096 tab
= DCT_B14_8
+ (UBITS (bit_buf
, 8) - 4);
1104 i
+= UBITS (bit_buf
<< 6, 6) - 64;
1106 break; /* illegal, check needed to avoid buffer overflow */
1108 DUMPBITS (bit_buf
, bits
, 12);
1109 NEEDBITS (bit_buf
, bits
, bit_ptr
);
1111 if (!(SBITS (bit_buf
, 8) & 0x7f))
1112 DUMPBITS (bit_buf
, bits
, 8);
1114 DUMPBITS (bit_buf
, bits
, 8);
1115 NEEDBITS (bit_buf
, bits
, bit_ptr
);
1119 else if (bit_buf
>= 0x02000000)
1121 tab
= DCT_B14_10
+ (UBITS (bit_buf
, 10) - 8);
1126 else if (bit_buf
>= 0x00800000)
1128 tab
= DCT_13
+ (UBITS (bit_buf
, 13) - 16);
1133 else if (bit_buf
>= 0x00200000)
1135 tab
= DCT_15
+ (UBITS (bit_buf
, 15) - 16);
1142 tab
= DCT_16
+ UBITS (bit_buf
, 16);
1144 GETWORD (bit_buf
, bits
+ 16, bit_ptr
);
1149 break; /* illegal, check needed to avoid buffer overflow */
1152 DUMPBITS (bit_buf
, bits
, 2); /* dump end of block code */
1153 decoder
->bitstream_buf
= bit_buf
;
1154 decoder
->bitstream_bits
= bits
;
1155 decoder
->bitstream_ptr
= bit_ptr
;
1158 static void skip_intra_block_B14 (mpeg2_decoder_t
* const decoder
)
1160 uint32_t bit_buf
= decoder
->bitstream_buf
;
1161 int bits
= decoder
->bitstream_bits
;
1162 const uint8_t * bit_ptr
= decoder
->bitstream_ptr
;
1166 NEEDBITS (bit_buf
, bits
, bit_ptr
);
1170 if (bit_buf
>= 0x28000000)
1172 tab
= DCT_B14AC_5
+ (UBITS (bit_buf
, 5) - 5);
1176 break; /* end of block */
1179 bit_buf
<<= tab
->len
+ 1;
1180 bits
+= tab
->len
+ 1;
1181 NEEDBITS (bit_buf
, bits
, bit_ptr
);
1184 else if (bit_buf
>= 0x04000000)
1186 tab
= DCT_B14_8
+ (UBITS (bit_buf
, 8) - 4);
1194 i
+= UBITS (bit_buf
<< 6, 6) - 64;
1196 break; /* illegal, check needed to avoid buffer overflow */
1198 DUMPBITS (bit_buf
, bits
, 12); /* Can't dump more than 16 atm */
1199 NEEDBITS (bit_buf
, bits
, bit_ptr
);
1200 DUMPBITS (bit_buf
, bits
, 12);
1201 NEEDBITS (bit_buf
, bits
, bit_ptr
);
1204 else if (bit_buf
>= 0x02000000)
1206 tab
= DCT_B14_10
+ (UBITS (bit_buf
, 10) - 8);
1211 else if (bit_buf
>= 0x00800000)
1213 tab
= DCT_13
+ (UBITS (bit_buf
, 13) - 16);
1218 else if (bit_buf
>= 0x00200000)
1220 tab
= DCT_15
+ (UBITS (bit_buf
, 15) - 16);
1227 tab
= DCT_16
+ UBITS (bit_buf
, 16);
1229 GETWORD (bit_buf
, bits
+ 16, bit_ptr
);
1234 break; /* illegal, check needed to avoid buffer overflow */
1237 DUMPBITS (bit_buf
, bits
, 2); /* dump end of block code */
1238 decoder
->bitstream_buf
= bit_buf
;
1239 decoder
->bitstream_bits
= bits
;
1240 decoder
->bitstream_ptr
= bit_ptr
;
1243 static void skip_intra_block_B15 (mpeg2_decoder_t
* const decoder
)
1245 uint32_t bit_buf
= decoder
->bitstream_buf
;
1246 int bits
= decoder
->bitstream_bits
;
1247 const uint8_t * bit_ptr
= decoder
->bitstream_ptr
;
1251 NEEDBITS (bit_buf
, bits
, bit_ptr
);
1255 if (bit_buf
>= 0x04000000)
1257 tab
= DCT_B15_8
+ (UBITS (bit_buf
, 8) - 4);
1264 bit_buf
<<= tab
->len
+ 1;
1265 bits
+= tab
->len
+ 1;
1266 NEEDBITS (bit_buf
, bits
, bit_ptr
);
1271 /* end of block. I commented out this code because if we */
1272 /* dont exit here we will still exit at the later test :) */
1274 /* if (i >= 128) break; */ /* end of block */
1278 i
+= UBITS (bit_buf
<< 6, 6) - 64;
1280 break; /* illegal, check against buffer overflow */
1282 DUMPBITS (bit_buf
, bits
, 12); /* Can't dump more than 16 atm */
1283 NEEDBITS (bit_buf
, bits
, bit_ptr
);
1284 DUMPBITS (bit_buf
, bits
, 12);
1285 NEEDBITS (bit_buf
, bits
, bit_ptr
);
1289 else if (bit_buf
>= 0x02000000)
1291 tab
= DCT_B15_10
+ (UBITS (bit_buf
, 10) - 8);
1296 else if (bit_buf
>= 0x00800000)
1298 tab
= DCT_13
+ (UBITS (bit_buf
, 13) - 16);
1303 else if (bit_buf
>= 0x00200000)
1305 tab
= DCT_15
+ (UBITS (bit_buf
, 15) - 16);
1312 tab
= DCT_16
+ UBITS (bit_buf
, 16);
1314 GETWORD (bit_buf
, bits
+ 16, bit_ptr
);
1319 break; /* illegal, check needed to avoid buffer overflow */
1322 DUMPBITS (bit_buf
, bits
, 4); /* dump end of block code */
1323 decoder
->bitstream_buf
= bit_buf
;
1324 decoder
->bitstream_bits
= bits
;
1325 decoder
->bitstream_ptr
= bit_ptr
;
1328 static void skip_non_intra_block (mpeg2_decoder_t
* const decoder
)
1330 uint32_t bit_buf
= decoder
->bitstream_buf
;
1331 int bits
= decoder
->bitstream_bits
;
1332 const uint8_t * bit_ptr
= decoder
->bitstream_ptr
;
1336 NEEDBITS (bit_buf
, bits
, bit_ptr
);
1338 if (bit_buf
>= 0x28000000)
1340 tab
= DCT_B14DC_5
+ (UBITS (bit_buf
, 5) - 5);
1350 if (bit_buf
>= 0x28000000)
1352 tab
= DCT_B14AC_5
+ (UBITS (bit_buf
, 5) - 5);
1357 break; /* end of block */
1360 bit_buf
<<= tab
->len
+ 1;
1361 bits
+= tab
->len
+ 1;
1362 NEEDBITS (bit_buf
, bits
, bit_ptr
);
1368 if (bit_buf
>= 0x04000000)
1370 tab
= DCT_B14_8
+ (UBITS (bit_buf
, 8) - 4);
1378 i
+= UBITS (bit_buf
<< 6, 6) - 64;
1380 break; /* illegal, check needed to avoid buffer overflow */
1384 DUMPBITS (bit_buf
, bits
, 12);
1385 NEEDBITS (bit_buf
, bits
, bit_ptr
);
1387 if (!(SBITS (bit_buf
, 8) & 0x7f))
1388 DUMPBITS (bit_buf
, bits
, 8);
1390 DUMPBITS (bit_buf
, bits
, 8);
1394 DUMPBITS (bit_buf
, bits
, 12);
1395 NEEDBITS (bit_buf
, bits
, bit_ptr
);
1396 DUMPBITS (bit_buf
, bits
, 12);
1399 NEEDBITS (bit_buf
, bits
, bit_ptr
);
1402 else if (bit_buf
>= 0x02000000)
1404 tab
= DCT_B14_10
+ (UBITS (bit_buf
, 10) - 8);
1409 else if (bit_buf
>= 0x00800000)
1411 tab
= DCT_13
+ (UBITS (bit_buf
, 13) - 16);
1416 else if (bit_buf
>= 0x00200000)
1418 tab
= DCT_15
+ (UBITS (bit_buf
, 15) - 16);
1425 tab
= DCT_16
+ UBITS (bit_buf
, 16);
1427 GETWORD (bit_buf
, bits
+ 16, bit_ptr
);
1432 break; /* illegal, check needed to avoid buffer overflow */
1435 DUMPBITS (bit_buf
, bits
, 2); /* dump end of block code */
1436 decoder
->bitstream_buf
= bit_buf
;
1437 decoder
->bitstream_bits
= bits
;
1438 decoder
->bitstream_ptr
= bit_ptr
;
1441 static void skip_chroma_dc_dct_diff (mpeg2_decoder_t
* const decoder
)
1443 #define bit_buf (decoder->bitstream_buf)
1444 #define bits (decoder->bitstream_bits)
1445 #define bit_ptr (decoder->bitstream_ptr)
1450 if (bit_buf
< 0xf8000000)
1452 tab
= DC_chrom_5
+ UBITS (bit_buf
, 5);
1457 bits
+= tab
->len
+ size
;
1458 bit_buf
<<= tab
->len
;
1463 DUMPBITS (bit_buf
, bits
, 2);
1468 tab
= DC_long
+ (UBITS (bit_buf
, 10) - 0x3e0);
1470 DUMPBITS (bit_buf
, bits
, tab
->len
+ 1);
1471 NEEDBITS (bit_buf
, bits
, bit_ptr
);
1472 DUMPBITS (bit_buf
, bits
, size
);
1480 static void skip_chroma_non_intra (mpeg2_decoder_t
* const decoder
,
1481 uint32_t coded_block_pattern
)
1483 static const uint32_t cbp_mask
[3] =
1490 uint32_t cbp
= coded_block_pattern
&
1491 cbp_mask
[MIN((unsigned)decoder
->chroma_format
, 2u)];
1495 skip_non_intra_block (decoder
);
1500 static void skip_chroma_intra (mpeg2_decoder_t
* const decoder
)
1502 #define bit_buf (decoder->bitstream_buf)
1503 #define bits (decoder->bitstream_bits)
1504 #define bit_ptr (decoder->bitstream_ptr)
1505 int i
= 2 << decoder
->chroma_format
;
1507 if ((unsigned)i
> 8)
1512 NEEDBITS (bit_buf
, bits
, bit_ptr
);
1514 skip_chroma_dc_dct_diff (decoder
);
1518 if (decoder
->coding_type
!= D_TYPE
)
1519 skip_mpeg1_intra_block (decoder
);
1521 else if (decoder
->intra_vlc_format
)
1523 skip_intra_block_B15 (decoder
);
1527 skip_intra_block_B14 (decoder
);
1531 if (decoder
->chroma_format
== 0 && decoder
->coding_type
== D_TYPE
)
1533 NEEDBITS (bit_buf
, bits
, bit_ptr
);
1534 DUMPBITS (bit_buf
, bits
, 1);
1541 #endif /* !MPEG2_COLOR */
1543 #define MOTION_420(table, ref, motion_x, motion_y, size, y) \
1544 pos_x = 2 * decoder->offset + motion_x; \
1545 pos_y = 2 * decoder->v_offset + motion_y + 2 * y; \
1547 if (unlikely (pos_x > decoder->limit_x)) \
1549 pos_x = ((int)pos_x < 0) ? 0 : decoder->limit_x; \
1550 motion_x = pos_x - 2 * decoder->offset; \
1553 if (unlikely (pos_y > decoder->limit_y_ ## size)) \
1555 pos_y = ((int)pos_y < 0) ? 0 : decoder->limit_y_ ## size; \
1556 motion_y = pos_y - 2 * decoder->v_offset - 2 * y; \
1559 xy_half = ((pos_y & 1) << 1) | (pos_x & 1); \
1560 table[xy_half] (decoder->dest[0] + y * decoder->stride + decoder->offset, \
1561 ref[0] + (pos_x >> 1) + (pos_y >> 1) * decoder->stride, \
1562 decoder->stride, size); \
1568 xy_half = ((motion_y & 1) << 1) | (motion_x & 1); \
1569 offset = ((decoder->offset + motion_x) >> 1) + \
1570 ((((decoder->v_offset + motion_y) >> 1) + y/2) * \
1571 decoder->uv_stride); \
1573 table[4+xy_half] (decoder->dest[1] + y/2 * decoder->uv_stride + \
1574 (decoder->offset >> 1), ref[1] + offset, \
1575 decoder->uv_stride, size/2); \
1576 table[4+xy_half] (decoder->dest[2] + y/2 * decoder->uv_stride + \
1577 (decoder->offset >> 1), ref[2] + offset, \
1578 decoder->uv_stride, size/2); \
1581 #define MOTION_FIELD_420(table, ref, motion_x, motion_y, \
1582 dest_field, op, src_field) \
1583 pos_x = 2 * decoder->offset + motion_x; \
1584 pos_y = decoder->v_offset + motion_y; \
1586 if (unlikely (pos_x > decoder->limit_x)) \
1588 pos_x = ((int)pos_x < 0) ? 0 : decoder->limit_x; \
1589 motion_x = pos_x - 2 * decoder->offset; \
1592 if (unlikely (pos_y > decoder->limit_y)) \
1594 pos_y = ((int)pos_y < 0) ? 0 : decoder->limit_y; \
1595 motion_y = pos_y - decoder->v_offset; \
1598 xy_half = ((pos_y & 1) << 1) | (pos_x & 1); \
1599 table[xy_half] (decoder->dest[0] + dest_field * decoder->stride + \
1601 (ref[0] + (pos_x >> 1) + \
1602 ((pos_y op) + src_field) * decoder->stride), \
1603 2 * decoder->stride, 8); \
1609 xy_half = ((motion_y & 1) << 1) | (motion_x & 1); \
1610 offset = ((decoder->offset + motion_x) >> 1) + \
1611 (((decoder->v_offset >> 1) + (motion_y op) + src_field) * \
1612 decoder->uv_stride); \
1614 table[4+xy_half] (decoder->dest[1] + dest_field * decoder->uv_stride + \
1615 (decoder->offset >> 1), ref[1] + offset, \
1616 2 * decoder->uv_stride, 4); \
1617 table[4+xy_half] (decoder->dest[2] + dest_field * decoder->uv_stride + \
1618 (decoder->offset >> 1), ref[2] + offset, \
1619 2 * decoder->uv_stride, 4); \
1622 #define MOTION_DMV_420(table, ref, motion_x, motion_y) \
1623 pos_x = 2 * decoder->offset + motion_x; \
1624 pos_y = decoder->v_offset + motion_y; \
1626 if (unlikely (pos_x > decoder->limit_x)) \
1628 pos_x = ((int)pos_x < 0) ? 0 : decoder->limit_x; \
1629 motion_x = pos_x - 2 * decoder->offset; \
1632 if (unlikely (pos_y > decoder->limit_y)) \
1634 pos_y = ((int)pos_y < 0) ? 0 : decoder->limit_y; \
1635 motion_y = pos_y - decoder->v_offset; \
1638 xy_half = ((pos_y & 1) << 1) | (pos_x & 1); \
1639 offset = (pos_x >> 1) + (pos_y & ~1) * decoder->stride; \
1640 table[xy_half] (decoder->dest[0] + decoder->offset, \
1641 ref[0] + offset, 2 * decoder->stride, 8); \
1642 table[xy_half] (decoder->dest[0] + decoder->stride + decoder->offset, \
1643 ref[0] + decoder->stride + offset, \
1644 2 * decoder->stride, 8); \
1650 xy_half = ((motion_y & 1) << 1) | (motion_x & 1); \
1651 offset = ((decoder->offset + motion_x) >> 1) + \
1652 (((decoder->v_offset >> 1) + (motion_y & ~1)) * \
1653 decoder->uv_stride); \
1655 table[4+xy_half] (decoder->dest[1] + (decoder->offset >> 1), \
1656 ref[1] + offset, 2 * decoder->uv_stride, 4); \
1657 table[4+xy_half] (decoder->dest[1] + decoder->uv_stride + \
1658 (decoder->offset >> 1), \
1659 ref[1] + decoder->uv_stride + offset, \
1660 2 * decoder->uv_stride, 4); \
1661 table[4+xy_half] (decoder->dest[2] + (decoder->offset >> 1), \
1662 ref[2] + offset, 2 * decoder->uv_stride, 4); \
1663 table[4+xy_half] (decoder->dest[2] + decoder->uv_stride + \
1664 (decoder->offset >> 1), \
1665 ref[2] + decoder->uv_stride + offset, \
1666 2 * decoder->uv_stride, 4); \
1669 #define MOTION_ZERO_420(table, ref) \
1670 table[0] (decoder->dest[0] + decoder->offset, \
1671 (ref[0] + decoder->offset + \
1672 decoder->v_offset * decoder->stride), decoder->stride, 16); \
1676 offset = ((decoder->offset >> 1) + \
1677 (decoder->v_offset >> 1) * decoder->uv_stride); \
1679 table[4] (decoder->dest[1] + (decoder->offset >> 1), \
1680 ref[1] + offset, decoder->uv_stride, 8); \
1681 table[4] (decoder->dest[2] + (decoder->offset >> 1), \
1682 ref[2] + offset, decoder->uv_stride, 8); \
1685 #define MOTION_422(table, ref, motion_x, motion_y, size, y) \
1686 pos_x = 2 * decoder->offset + motion_x; \
1687 pos_y = 2 * decoder->v_offset + motion_y + 2 * y; \
1689 if (unlikely (pos_x > decoder->limit_x)) \
1691 pos_x = ((int)pos_x < 0) ? 0 : decoder->limit_x; \
1692 motion_x = pos_x - 2 * decoder->offset; \
1695 if (unlikely (pos_y > decoder->limit_y_ ## size)) \
1697 pos_y = ((int)pos_y < 0) ? 0 : decoder->limit_y_ ## size; \
1698 motion_y = pos_y - 2 * decoder->v_offset - 2 * y; \
1701 xy_half = ((pos_y & 1) << 1) | (pos_x & 1); \
1702 offset = (pos_x >> 1) + (pos_y >> 1) * decoder->stride; \
1704 table[xy_half] (decoder->dest[0] + y * decoder->stride + decoder->offset, \
1705 ref[0] + offset, decoder->stride, size); \
1709 offset = (offset + (motion_x & (motion_x < 0))) >> 1; \
1711 xy_half = ((pos_y & 1) << 1) | (motion_x & 1); \
1713 table[4+xy_half] (decoder->dest[1] + y * decoder->uv_stride + \
1714 (decoder->offset >> 1), ref[1] + offset, \
1715 decoder->uv_stride, size); \
1716 table[4+xy_half] (decoder->dest[2] + y * decoder->uv_stride + \
1717 (decoder->offset >> 1), ref[2] + offset, \
1718 decoder->uv_stride, size); \
1721 #define MOTION_FIELD_422(table, ref, motion_x, motion_y, \
1722 dest_field, op, src_field) \
1723 pos_x = 2 * decoder->offset + motion_x; \
1724 pos_y = decoder->v_offset + motion_y; \
1726 if (unlikely (pos_x > decoder->limit_x)) \
1728 pos_x = ((int)pos_x < 0) ? 0 : decoder->limit_x; \
1729 motion_x = pos_x - 2 * decoder->offset; \
1732 if (unlikely (pos_y > decoder->limit_y)) \
1734 pos_y = ((int)pos_y < 0) ? 0 : decoder->limit_y; \
1735 motion_y = pos_y - decoder->v_offset; \
1738 xy_half = ((pos_y & 1) << 1) | (pos_x & 1); \
1739 offset = (pos_x >> 1) + ((pos_y op) + src_field) * decoder->stride; \
1741 table[xy_half] (decoder->dest[0] + dest_field * decoder->stride + \
1742 decoder->offset, ref[0] + offset, \
1743 2 * decoder->stride, 8); \
1747 offset = (offset + (motion_x & (motion_x < 0))) >> 1; \
1749 xy_half = ((pos_y & 1) << 1) | (motion_x & 1); \
1751 table[4+xy_half] (decoder->dest[1] + dest_field * decoder->uv_stride + \
1752 (decoder->offset >> 1), ref[1] + offset, \
1753 2 * decoder->uv_stride, 8); \
1754 table[4+xy_half] (decoder->dest[2] + dest_field * decoder->uv_stride + \
1755 (decoder->offset >> 1), ref[2] + offset, \
1756 2 * decoder->uv_stride, 8); \
1759 #define MOTION_DMV_422(table, ref, motion_x, motion_y) \
1760 pos_x = 2 * decoder->offset + motion_x; \
1761 pos_y = decoder->v_offset + motion_y; \
1763 if (unlikely (pos_x > decoder->limit_x)) \
1765 pos_x = ((int)pos_x < 0) ? 0 : decoder->limit_x; \
1766 motion_x = pos_x - 2 * decoder->offset; \
1769 if (unlikely (pos_y > decoder->limit_y)) \
1771 pos_y = ((int)pos_y < 0) ? 0 : decoder->limit_y; \
1772 motion_y = pos_y - decoder->v_offset; \
1775 xy_half = ((pos_y & 1) << 1) | (pos_x & 1); \
1776 offset = (pos_x >> 1) + (pos_y & ~1) * decoder->stride; \
1778 table[xy_half] (decoder->dest[0] + decoder->offset, \
1779 ref[0] + offset, 2 * decoder->stride, 8); \
1780 table[xy_half] (decoder->dest[0] + decoder->stride + decoder->offset, \
1781 ref[0] + decoder->stride + offset, \
1782 2 * decoder->stride, 8); \
1786 offset = (offset + (motion_x & (motion_x < 0))) >> 1; \
1788 xy_half = ((pos_y & 1) << 1) | (motion_x & 1); \
1790 table[4+xy_half] (decoder->dest[1] + (decoder->offset >> 1), \
1791 ref[1] + offset, 2 * decoder->uv_stride, 8); \
1792 table[4+xy_half] (decoder->dest[1] + decoder->uv_stride + \
1793 (decoder->offset >> 1), \
1794 ref[1] + decoder->uv_stride + offset, \
1795 2 * decoder->uv_stride, 8); \
1796 table[4+xy_half] (decoder->dest[2] + (decoder->offset >> 1), \
1797 ref[2] + offset, 2 * decoder->uv_stride, 8); \
1798 table[4+xy_half] (decoder->dest[2] + decoder->uv_stride + \
1799 (decoder->offset >> 1), \
1800 ref[2] + decoder->uv_stride + offset, \
1801 2 * decoder->uv_stride, 8); \
1804 #define MOTION_ZERO_422(table, ref) \
1805 offset = decoder->offset + decoder->v_offset * decoder->stride; \
1806 table[0] (decoder->dest[0] + decoder->offset, \
1807 ref[0] + offset, decoder->stride, 16); \
1812 table[4] (decoder->dest[1] + (decoder->offset >> 1), \
1813 ref[1] + offset, decoder->uv_stride, 16); \
1814 table[4] (decoder->dest[2] + (decoder->offset >> 1), \
1815 ref[2] + offset, decoder->uv_stride, 16); \
1818 #define MOTION_444(table, ref, motion_x, motion_y, size, y) \
1819 pos_x = 2 * decoder->offset + motion_x; \
1820 pos_y = 2 * decoder->v_offset + motion_y + 2 * y; \
1822 if (unlikely (pos_x > decoder->limit_x)) \
1824 pos_x = ((int)pos_x < 0) ? 0 : decoder->limit_x; \
1825 motion_x = pos_x - 2 * decoder->offset; \
1828 if (unlikely (pos_y > decoder->limit_y_ ## size)) \
1830 pos_y = ((int)pos_y < 0) ? 0 : decoder->limit_y_ ## size; \
1831 motion_y = pos_y - 2 * decoder->v_offset - 2 * y; \
1834 xy_half = ((pos_y & 1) << 1) | (pos_x & 1); \
1835 offset = (pos_x >> 1) + (pos_y >> 1) * decoder->stride; \
1837 table[xy_half] (decoder->dest[0] + y * decoder->stride + decoder->offset, \
1838 ref[0] + offset, decoder->stride, size); \
1842 table[xy_half] (decoder->dest[1] + y * decoder->stride + decoder->offset, \
1843 ref[1] + offset, decoder->stride, size); \
1844 table[xy_half] (decoder->dest[2] + y * decoder->stride + decoder->offset, \
1845 ref[2] + offset, decoder->stride, size); \
1848 #define MOTION_FIELD_444(table, ref, motion_x, motion_y, \
1849 dest_field, op, src_field) \
1850 pos_x = 2 * decoder->offset + motion_x; \
1851 pos_y = decoder->v_offset + motion_y; \
1853 if (unlikely (pos_x > decoder->limit_x)) \
1855 pos_x = ((int)pos_x < 0) ? 0 : decoder->limit_x; \
1856 motion_x = pos_x - 2 * decoder->offset; \
1859 if (unlikely (pos_y > decoder->limit_y)) \
1861 pos_y = ((int)pos_y < 0) ? 0 : decoder->limit_y; \
1862 motion_y = pos_y - decoder->v_offset; \
1865 xy_half = ((pos_y & 1) << 1) | (pos_x & 1); \
1866 offset = (pos_x >> 1) + ((pos_y op) + src_field) * decoder->stride; \
1868 table[xy_half] (decoder->dest[0] + dest_field * decoder->stride + \
1869 decoder->offset, ref[0] + offset, \
1870 2 * decoder->stride, 8); \
1874 table[xy_half] (decoder->dest[1] + dest_field * decoder->stride + \
1875 decoder->offset, ref[1] + offset, \
1876 2 * decoder->stride, 8); \
1877 table[xy_half] (decoder->dest[2] + dest_field * decoder->stride + \
1878 decoder->offset, ref[2] + offset, \
1879 2 * decoder->stride, 8); \
1882 #define MOTION_DMV_444(table, ref, motion_x, motion_y) \
1883 pos_x = 2 * decoder->offset + motion_x; \
1884 pos_y = decoder->v_offset + motion_y; \
1886 if (unlikely (pos_x > decoder->limit_x)) \
1888 pos_x = ((int)pos_x < 0) ? 0 : decoder->limit_x; \
1889 motion_x = pos_x - 2 * decoder->offset; \
1892 if (unlikely (pos_y > decoder->limit_y)) \
1894 pos_y = ((int)pos_y < 0) ? 0 : decoder->limit_y; \
1895 motion_y = pos_y - decoder->v_offset; \
1898 xy_half = ((pos_y & 1) << 1) | (pos_x & 1); \
1899 offset = (pos_x >> 1) + (pos_y & ~1) * decoder->stride; \
1901 table[xy_half] (decoder->dest[0] + decoder->offset, \
1902 ref[0] + offset, 2 * decoder->stride, 8); \
1903 table[xy_half] (decoder->dest[0] + decoder->stride + decoder->offset, \
1904 ref[0] + decoder->stride + offset, \
1905 2 * decoder->stride, 8); \
1909 table[xy_half] (decoder->dest[1] + decoder->offset, \
1910 ref[1] + offset, 2 * decoder->stride, 8); \
1911 table[xy_half] (decoder->dest[1] + decoder->stride + decoder->offset, \
1912 ref[1] + decoder->stride + offset, \
1913 2 * decoder->stride, 8); \
1914 table[xy_half] (decoder->dest[2] + decoder->offset, \
1915 ref[2] + offset, 2 * decoder->stride, 8); \
1916 table[xy_half] (decoder->dest[2] + decoder->stride + decoder->offset, \
1917 ref[2] + decoder->stride + offset, \
1918 2 * decoder->stride, 8); \
1921 #define MOTION_ZERO_444(table, ref) \
1922 offset = decoder->offset + decoder->v_offset * decoder->stride; \
1924 table[0] (decoder->dest[0] + decoder->offset, \
1925 ref[0] + offset, decoder->stride, 16); \
1929 table[4] (decoder->dest[1] + decoder->offset, \
1930 ref[1] + offset, decoder->stride, 16); \
1931 table[4] (decoder->dest[2] + decoder->offset, \
1932 ref[2] + offset, decoder->stride, 16); \
1935 #define bit_buf (decoder->bitstream_buf)
1936 #define bits (decoder->bitstream_bits)
1937 #define bit_ptr (decoder->bitstream_ptr)
1939 static void motion_mp1 (mpeg2_decoder_t
* const decoder
,
1940 motion_t
* const motion
,
1941 mpeg2_mc_fct
* const * const table
)
1943 int motion_x
, motion_y
;
1944 unsigned int pos_x
, pos_y
, xy_half
, offset
;
1946 NEEDBITS (bit_buf
, bits
, bit_ptr
);
1947 motion_x
= motion
->pmv
[0][0] +
1948 (get_motion_delta (decoder
,
1949 motion
->f_code
[0]) << motion
->f_code
[1]);
1950 motion_x
= bound_motion_vector (motion_x
,
1951 motion
->f_code
[0] + motion
->f_code
[1]);
1952 motion
->pmv
[0][0] = motion_x
;
1954 NEEDBITS (bit_buf
, bits
, bit_ptr
);
1955 motion_y
= motion
->pmv
[0][1] +
1956 (get_motion_delta (decoder
,
1957 motion
->f_code
[0]) << motion
->f_code
[1]);
1958 motion_y
= bound_motion_vector (motion_y
,
1959 motion
->f_code
[0] + motion
->f_code
[1]);
1960 motion
->pmv
[0][1] = motion_y
;
1962 MOTION_420 (table
, motion
->ref
[0], motion_x
, motion_y
, 16, 0);
1965 #define MOTION_FUNCTIONS(FORMAT, MOTION, MOTION_FIELD, \
1966 MOTION_DMV, MOTION_ZERO) \
1968 static void motion_fr_frame_##FORMAT (mpeg2_decoder_t * const decoder, \
1969 motion_t * const motion, \
1970 mpeg2_mc_fct * const * const table) \
1972 int motion_x, motion_y; \
1973 unsigned int pos_x, pos_y, xy_half, offset; \
1975 NEEDBITS (bit_buf, bits, bit_ptr); \
1976 motion_x = motion->pmv[0][0] + get_motion_delta (decoder, \
1977 motion->f_code[0]); \
1978 motion_x = bound_motion_vector (motion_x, motion->f_code[0]); \
1979 motion->pmv[1][0] = motion->pmv[0][0] = motion_x; \
1981 NEEDBITS (bit_buf, bits, bit_ptr); \
1982 motion_y = motion->pmv[0][1] + get_motion_delta (decoder, \
1983 motion->f_code[1]); \
1984 motion_y = bound_motion_vector (motion_y, motion->f_code[1]); \
1985 motion->pmv[1][1] = motion->pmv[0][1] = motion_y; \
1987 MOTION (table, motion->ref[0], motion_x, motion_y, 16, 0); \
1990 static void motion_fr_field_##FORMAT (mpeg2_decoder_t * const decoder, \
1991 motion_t * const motion, \
1992 mpeg2_mc_fct * const * const table) \
1994 int motion_x, motion_y, field; \
1995 unsigned int pos_x, pos_y, xy_half, offset; \
1997 NEEDBITS (bit_buf, bits, bit_ptr); \
1998 field = UBITS (bit_buf, 1); \
1999 DUMPBITS (bit_buf, bits, 1); \
2001 motion_x = motion->pmv[0][0] + get_motion_delta (decoder, \
2002 motion->f_code[0]); \
2003 motion_x = bound_motion_vector (motion_x, motion->f_code[0]); \
2004 motion->pmv[0][0] = motion_x; \
2006 NEEDBITS (bit_buf, bits, bit_ptr); \
2007 motion_y = ((motion->pmv[0][1] >> 1) + \
2008 get_motion_delta (decoder, motion->f_code[1])); \
2009 /* motion_y = bound_motion_vector (motion_y, motion->f_code[1]); */ \
2010 motion->pmv[0][1] = motion_y << 1; \
2012 MOTION_FIELD (table, motion->ref[0], motion_x, motion_y, 0, & ~1, field); \
2014 NEEDBITS (bit_buf, bits, bit_ptr); \
2015 field = UBITS (bit_buf, 1); \
2016 DUMPBITS (bit_buf, bits, 1); \
2018 motion_x = motion->pmv[1][0] + get_motion_delta (decoder, \
2019 motion->f_code[0]); \
2020 motion_x = bound_motion_vector (motion_x, motion->f_code[0]); \
2021 motion->pmv[1][0] = motion_x; \
2023 NEEDBITS (bit_buf, bits, bit_ptr); \
2024 motion_y = ((motion->pmv[1][1] >> 1) + \
2025 get_motion_delta (decoder, motion->f_code[1])); \
2026 /* motion_y = bound_motion_vector (motion_y, motion->f_code[1]); */ \
2027 motion->pmv[1][1] = motion_y << 1; \
2029 MOTION_FIELD (table, motion->ref[0], motion_x, motion_y, 1, & ~1, field); \
2032 static void motion_fr_dmv_##FORMAT (mpeg2_decoder_t * const decoder, \
2033 motion_t * const motion, \
2034 mpeg2_mc_fct * const * const table) \
2036 int motion_x, motion_y, dmv_x, dmv_y, m, other_x, other_y; \
2037 unsigned int pos_x, pos_y, xy_half, offset; \
2040 NEEDBITS (bit_buf, bits, bit_ptr); \
2041 motion_x = motion->pmv[0][0] + get_motion_delta (decoder, \
2042 motion->f_code[0]); \
2043 motion_x = bound_motion_vector (motion_x, motion->f_code[0]); \
2044 motion->pmv[1][0] = motion->pmv[0][0] = motion_x; \
2045 NEEDBITS (bit_buf, bits, bit_ptr); \
2046 dmv_x = get_dmv (decoder); \
2048 motion_y = ((motion->pmv[0][1] >> 1) + \
2049 get_motion_delta (decoder, motion->f_code[1])); \
2050 /* motion_y = bound_motion_vector (motion_y, motion->f_code[1]); */ \
2051 motion->pmv[1][1] = motion->pmv[0][1] = motion_y << 1; \
2052 dmv_y = get_dmv (decoder); \
2054 m = decoder->top_field_first ? 1 : 3; \
2055 other_x = ((motion_x * m + (motion_x > 0)) >> 1) + dmv_x; \
2056 other_y = ((motion_y * m + (motion_y > 0)) >> 1) + dmv_y - 1; \
2057 MOTION_FIELD (mpeg2_mc.put, motion->ref[0], other_x, other_y, 0, | 1, 0); \
2059 m = decoder->top_field_first ? 3 : 1; \
2060 other_x = ((motion_x * m + (motion_x > 0)) >> 1) + dmv_x; \
2061 other_y = ((motion_y * m + (motion_y > 0)) >> 1) + dmv_y + 1; \
2062 MOTION_FIELD (mpeg2_mc.put, motion->ref[0], other_x, other_y, 1, & ~1, 0);\
2064 MOTION_DMV (mpeg2_mc.avg, motion->ref[0], motion_x, motion_y); \
2067 static void motion_reuse_##FORMAT (mpeg2_decoder_t * const decoder, \
2068 motion_t * const motion, \
2069 mpeg2_mc_fct * const * const table) \
2071 int motion_x, motion_y; \
2072 unsigned int pos_x, pos_y, xy_half, offset; \
2074 motion_x = motion->pmv[0][0]; \
2075 motion_y = motion->pmv[0][1]; \
2077 MOTION (table, motion->ref[0], motion_x, motion_y, 16, 0); \
2080 static void motion_zero_##FORMAT (mpeg2_decoder_t * const decoder, \
2081 motion_t * const motion, \
2082 mpeg2_mc_fct * const * const table) \
2084 unsigned int offset; \
2086 motion->pmv[0][0] = motion->pmv[0][1] = 0; \
2087 motion->pmv[1][0] = motion->pmv[1][1] = 0; \
2089 MOTION_ZERO (table, motion->ref[0]); \
2092 static void motion_fi_field_##FORMAT (mpeg2_decoder_t * const decoder, \
2093 motion_t * const motion, \
2094 mpeg2_mc_fct * const * const table) \
2096 int motion_x, motion_y; \
2097 uint8_t ** ref_field; \
2098 unsigned int pos_x, pos_y, xy_half, offset; \
2100 NEEDBITS (bit_buf, bits, bit_ptr); \
2101 ref_field = motion->ref2[UBITS (bit_buf, 1)]; \
2102 DUMPBITS (bit_buf, bits, 1); \
2104 motion_x = motion->pmv[0][0] + get_motion_delta (decoder, \
2105 motion->f_code[0]); \
2106 motion_x = bound_motion_vector (motion_x, motion->f_code[0]); \
2107 motion->pmv[1][0] = motion->pmv[0][0] = motion_x; \
2109 NEEDBITS (bit_buf, bits, bit_ptr); \
2110 motion_y = motion->pmv[0][1] + get_motion_delta (decoder, \
2111 motion->f_code[1]); \
2112 motion_y = bound_motion_vector (motion_y, motion->f_code[1]); \
2113 motion->pmv[1][1] = motion->pmv[0][1] = motion_y; \
2115 MOTION (table, ref_field, motion_x, motion_y, 16, 0); \
2118 static void motion_fi_16x8_##FORMAT (mpeg2_decoder_t * const decoder, \
2119 motion_t * const motion, \
2120 mpeg2_mc_fct * const * const table) \
2122 int motion_x, motion_y; \
2123 uint8_t ** ref_field; \
2124 unsigned int pos_x, pos_y, xy_half, offset; \
2126 NEEDBITS (bit_buf, bits, bit_ptr); \
2127 ref_field = motion->ref2[UBITS (bit_buf, 1)]; \
2128 DUMPBITS (bit_buf, bits, 1); \
2130 motion_x = motion->pmv[0][0] + get_motion_delta (decoder, \
2131 motion->f_code[0]); \
2132 motion_x = bound_motion_vector (motion_x, motion->f_code[0]); \
2133 motion->pmv[0][0] = motion_x; \
2135 NEEDBITS (bit_buf, bits, bit_ptr); \
2136 motion_y = motion->pmv[0][1] + get_motion_delta (decoder, \
2137 motion->f_code[1]); \
2138 motion_y = bound_motion_vector (motion_y, motion->f_code[1]); \
2139 motion->pmv[0][1] = motion_y; \
2141 MOTION (table, ref_field, motion_x, motion_y, 8, 0); \
2143 NEEDBITS (bit_buf, bits, bit_ptr); \
2144 ref_field = motion->ref2[UBITS (bit_buf, 1)]; \
2145 DUMPBITS (bit_buf, bits, 1); \
2147 motion_x = motion->pmv[1][0] + get_motion_delta (decoder, \
2148 motion->f_code[0]); \
2149 motion_x = bound_motion_vector (motion_x, motion->f_code[0]); \
2150 motion->pmv[1][0] = motion_x; \
2152 NEEDBITS (bit_buf, bits, bit_ptr); \
2153 motion_y = motion->pmv[1][1] + get_motion_delta (decoder, \
2154 motion->f_code[1]); \
2155 motion_y = bound_motion_vector (motion_y, motion->f_code[1]); \
2156 motion->pmv[1][1] = motion_y; \
2158 MOTION (table, ref_field, motion_x, motion_y, 8, 8); \
2161 static void motion_fi_dmv_##FORMAT (mpeg2_decoder_t * const decoder, \
2162 motion_t * const motion, \
2163 mpeg2_mc_fct * const * const table) \
2165 int motion_x, motion_y, other_x, other_y; \
2166 unsigned int pos_x, pos_y, xy_half, offset; \
2169 NEEDBITS (bit_buf, bits, bit_ptr); \
2170 motion_x = motion->pmv[0][0] + get_motion_delta (decoder, \
2171 motion->f_code[0]); \
2172 motion_x = bound_motion_vector (motion_x, motion->f_code[0]); \
2173 motion->pmv[1][0] = motion->pmv[0][0] = motion_x; \
2174 NEEDBITS (bit_buf, bits, bit_ptr); \
2175 other_x = ((motion_x + (motion_x > 0)) >> 1) + get_dmv (decoder); \
2177 motion_y = motion->pmv[0][1] + get_motion_delta (decoder, \
2178 motion->f_code[1]); \
2179 motion_y = bound_motion_vector (motion_y, motion->f_code[1]); \
2180 motion->pmv[1][1] = motion->pmv[0][1] = motion_y; \
2181 other_y = (((motion_y + (motion_y > 0)) >> 1) + get_dmv (decoder) + \
2182 decoder->dmv_offset); \
2184 MOTION (mpeg2_mc.put, motion->ref[0], motion_x, motion_y, 16, 0); \
2185 MOTION (mpeg2_mc.avg, motion->ref[1], other_x, other_y, 16, 0); \
2188 MOTION_FUNCTIONS (420, MOTION_420, MOTION_FIELD_420, MOTION_DMV_420,
2190 MOTION_FUNCTIONS (422, MOTION_422
, MOTION_FIELD_422
, MOTION_DMV_422
,
2192 MOTION_FUNCTIONS (444, MOTION_444
, MOTION_FIELD_444
, MOTION_DMV_444
,
2195 /* like motion_frame, but parsing without actual motion compensation */
2196 static void motion_fr_conceal (mpeg2_decoder_t
* const decoder
)
2200 NEEDBITS (bit_buf
, bits
, bit_ptr
);
2201 tmp
= (decoder
->f_motion
.pmv
[0][0] +
2202 get_motion_delta (decoder
, decoder
->f_motion
.f_code
[0]));
2203 tmp
= bound_motion_vector (tmp
, decoder
->f_motion
.f_code
[0]);
2204 decoder
->f_motion
.pmv
[1][0] = decoder
->f_motion
.pmv
[0][0] = tmp
;
2206 NEEDBITS (bit_buf
, bits
, bit_ptr
);
2207 tmp
= (decoder
->f_motion
.pmv
[0][1] +
2208 get_motion_delta (decoder
, decoder
->f_motion
.f_code
[1]));
2209 tmp
= bound_motion_vector (tmp
, decoder
->f_motion
.f_code
[1]);
2210 decoder
->f_motion
.pmv
[1][1] = decoder
->f_motion
.pmv
[0][1] = tmp
;
2212 DUMPBITS (bit_buf
, bits
, 1); /* remove marker_bit */
2215 static void motion_fi_conceal (mpeg2_decoder_t
* const decoder
)
2219 NEEDBITS (bit_buf
, bits
, bit_ptr
);
2220 DUMPBITS (bit_buf
, bits
, 1); /* remove field_select */
2222 tmp
= decoder
->f_motion
.pmv
[0][0] +
2223 get_motion_delta (decoder
, decoder
->f_motion
.f_code
[0]);
2224 tmp
= bound_motion_vector (tmp
, decoder
->f_motion
.f_code
[0]);
2226 decoder
->f_motion
.pmv
[1][0] =
2227 decoder
->f_motion
.pmv
[0][0] = tmp
;
2229 NEEDBITS (bit_buf
, bits
, bit_ptr
);
2231 tmp
= (decoder
->f_motion
.pmv
[0][1] +
2232 get_motion_delta (decoder
, decoder
->f_motion
.f_code
[1]));
2233 tmp
= bound_motion_vector (tmp
, decoder
->f_motion
.f_code
[1]);
2235 decoder
->f_motion
.pmv
[1][1] =
2236 decoder
->f_motion
.pmv
[0][1] = tmp
;
2238 DUMPBITS (bit_buf
, bits
, 1); /* remove marker_bit */
2245 #define MOTION_CALL(routine, direction) \
2247 if ((direction) & MACROBLOCK_MOTION_FORWARD) \
2248 routine (decoder, &decoder->f_motion, mpeg2_mc.put); \
2250 if ((direction) & MACROBLOCK_MOTION_BACKWARD) \
2252 routine (decoder, &decoder->b_motion, \
2253 ((direction) & MACROBLOCK_MOTION_FORWARD ? \
2254 mpeg2_mc.avg : mpeg2_mc.put)); \
2258 #define NEXT_MACROBLOCK \
2260 decoder->offset += 16; \
2262 if (decoder->offset == decoder->width) \
2264 do { /* just so we can use the break statement */ \
2265 if (decoder->convert) \
2267 decoder->convert (decoder->convert_id, decoder->dest, \
2268 decoder->v_offset); \
2269 if (decoder->coding_type == B_TYPE) \
2273 decoder->dest[0] += decoder->slice_stride; \
2276 decoder->dest[1] += decoder->slice_uv_stride; \
2277 decoder->dest[2] += decoder->slice_uv_stride; \
2281 decoder->v_offset += 16; \
2283 if (decoder->v_offset > decoder->limit_y) \
2286 decoder->offset = 0; \
2290 void mpeg2_init_fbuf (mpeg2_decoder_t
* decoder
,
2291 uint8_t * current_fbuf
[MPEG2_COMPONENTS
],
2292 uint8_t * forward_fbuf
[MPEG2_COMPONENTS
],
2293 uint8_t * backward_fbuf
[MPEG2_COMPONENTS
])
2295 int offset
, stride
, height
, bottom_field
;
2297 stride
= decoder
->stride_frame
;
2298 bottom_field
= (decoder
->picture_structure
== BOTTOM_FIELD
);
2299 offset
= bottom_field
? stride
: 0;
2300 height
= decoder
->height
;
2302 decoder
->picture_dest
[0] = current_fbuf
[0] + offset
;
2304 decoder
->picture_dest
[1] = current_fbuf
[1] + (offset
>> 1);
2305 decoder
->picture_dest
[2] = current_fbuf
[2] + (offset
>> 1);
2308 decoder
->f_motion
.ref
[0][0] = forward_fbuf
[0] + offset
;
2310 decoder
->f_motion
.ref
[0][1] = forward_fbuf
[1] + (offset
>> 1);
2311 decoder
->f_motion
.ref
[0][2] = forward_fbuf
[2] + (offset
>> 1);
2314 decoder
->b_motion
.ref
[0][0] = backward_fbuf
[0] + offset
;
2316 decoder
->b_motion
.ref
[0][1] = backward_fbuf
[1] + (offset
>> 1);
2317 decoder
->b_motion
.ref
[0][2] = backward_fbuf
[2] + (offset
>> 1);
2320 if (decoder
->picture_structure
!= FRAME_PICTURE
)
2322 decoder
->dmv_offset
= bottom_field
? 1 : -1;
2323 decoder
->f_motion
.ref2
[0] = decoder
->f_motion
.ref
[bottom_field
];
2324 decoder
->f_motion
.ref2
[1] = decoder
->f_motion
.ref
[!bottom_field
];
2325 decoder
->b_motion
.ref2
[0] = decoder
->b_motion
.ref
[bottom_field
];
2326 decoder
->b_motion
.ref2
[1] = decoder
->b_motion
.ref
[!bottom_field
];
2327 offset
= stride
- offset
;
2329 if (decoder
->second_field
&& (decoder
->coding_type
!= B_TYPE
))
2330 forward_fbuf
= current_fbuf
;
2332 decoder
->f_motion
.ref
[1][0] = forward_fbuf
[0] + offset
;
2334 decoder
->f_motion
.ref
[1][1] = forward_fbuf
[1] + (offset
>> 1);
2335 decoder
->f_motion
.ref
[1][2] = forward_fbuf
[2] + (offset
>> 1);
2337 decoder
->b_motion
.ref
[1][0] = backward_fbuf
[0] + offset
;
2339 decoder
->b_motion
.ref
[1][1] = backward_fbuf
[1] + (offset
>> 1);
2340 decoder
->b_motion
.ref
[1][2] = backward_fbuf
[2] + (offset
>> 1);
2346 decoder
->stride
= stride
;
2347 decoder
->slice_stride
= 16 * stride
;
2349 decoder
->uv_stride
= stride
>> 1;
2350 decoder
->slice_uv_stride
=
2351 decoder
->slice_stride
>> (2 - decoder
->chroma_format
);
2353 decoder
->limit_x
= 2 * decoder
->width
- 32;
2354 decoder
->limit_y_16
= 2 * height
- 32;
2355 decoder
->limit_y_8
= 2 * height
- 16;
2356 decoder
->limit_y
= height
- 16;
2360 decoder
->motion_parser
[0] = motion_zero_420
;
2361 decoder
->motion_parser
[MC_FRAME
] = motion_mp1
;
2362 decoder
->motion_parser
[4] = motion_reuse_420
;
2364 else if (decoder
->picture_structure
== FRAME_PICTURE
)
2366 if (decoder
->chroma_format
== 0)
2368 decoder
->motion_parser
[0] = motion_zero_420
;
2369 decoder
->motion_parser
[MC_FIELD
] = motion_fr_field_420
;
2370 decoder
->motion_parser
[MC_FRAME
] = motion_fr_frame_420
;
2371 decoder
->motion_parser
[MC_DMV
] = motion_fr_dmv_420
;
2372 decoder
->motion_parser
[4] = motion_reuse_420
;
2374 else if (decoder
->chroma_format
== 1)
2376 decoder
->motion_parser
[0] = motion_zero_422
;
2377 decoder
->motion_parser
[MC_FIELD
] = motion_fr_field_422
;
2378 decoder
->motion_parser
[MC_FRAME
] = motion_fr_frame_422
;
2379 decoder
->motion_parser
[MC_DMV
] = motion_fr_dmv_422
;
2380 decoder
->motion_parser
[4] = motion_reuse_422
;
2384 decoder
->motion_parser
[0] = motion_zero_444
;
2385 decoder
->motion_parser
[MC_FIELD
] = motion_fr_field_444
;
2386 decoder
->motion_parser
[MC_FRAME
] = motion_fr_frame_444
;
2387 decoder
->motion_parser
[MC_DMV
] = motion_fr_dmv_444
;
2388 decoder
->motion_parser
[4] = motion_reuse_444
;
2393 if (decoder
->chroma_format
== 0)
2395 decoder
->motion_parser
[0] = motion_zero_420
;
2396 decoder
->motion_parser
[MC_FIELD
] = motion_fi_field_420
;
2397 decoder
->motion_parser
[MC_16X8
] = motion_fi_16x8_420
;
2398 decoder
->motion_parser
[MC_DMV
] = motion_fi_dmv_420
;
2399 decoder
->motion_parser
[4] = motion_reuse_420
;
2401 else if (decoder
->chroma_format
== 1)
2403 decoder
->motion_parser
[0] = motion_zero_422
;
2404 decoder
->motion_parser
[MC_FIELD
] = motion_fi_field_422
;
2405 decoder
->motion_parser
[MC_16X8
] = motion_fi_16x8_422
;
2406 decoder
->motion_parser
[MC_DMV
] = motion_fi_dmv_422
;
2407 decoder
->motion_parser
[4] = motion_reuse_422
;
2411 decoder
->motion_parser
[0] = motion_zero_444
;
2412 decoder
->motion_parser
[MC_FIELD
] = motion_fi_field_444
;
2413 decoder
->motion_parser
[MC_16X8
] = motion_fi_16x8_444
;
2414 decoder
->motion_parser
[MC_DMV
] = motion_fi_dmv_444
;
2415 decoder
->motion_parser
[4] = motion_reuse_444
;
2420 static inline int slice_init (mpeg2_decoder_t
* const decoder
, int code
)
2422 #define bit_buf (decoder->bitstream_buf)
2423 #define bits (decoder->bitstream_bits)
2424 #define bit_ptr (decoder->bitstream_ptr)
2430 decoder
->dc_dct_pred
[0] = decoder
->dc_dct_pred
[1] =
2431 decoder
->dc_dct_pred
[2] = 16384;
2433 decoder
->dc_dct_pred
[0] = 16384;
2436 decoder
->f_motion
.pmv
[0][0] = decoder
->f_motion
.pmv
[0][1] = 0;
2437 decoder
->f_motion
.pmv
[1][0] = decoder
->f_motion
.pmv
[1][1] = 0;
2438 decoder
->b_motion
.pmv
[0][0] = decoder
->b_motion
.pmv
[0][1] = 0;
2439 decoder
->b_motion
.pmv
[1][0] = decoder
->b_motion
.pmv
[1][1] = 0;
2441 if (decoder
->vertical_position_extension
)
2443 code
+= UBITS (bit_buf
, 3) << 7;
2444 DUMPBITS (bit_buf
, bits
, 3);
2447 decoder
->v_offset
= (code
- 1) * 16;
2450 if (!(decoder
->convert
) || decoder
->coding_type
!= B_TYPE
)
2452 offset
= (code
- 1) * decoder
->slice_stride
;
2455 decoder
->dest
[0] = decoder
->picture_dest
[0] + offset
;
2457 offset
>>= (2 - decoder
->chroma_format
);
2458 decoder
->dest
[1] = decoder
->picture_dest
[1] + offset
;
2459 decoder
->dest
[2] = decoder
->picture_dest
[2] + offset
;
2462 get_quantizer_scale (decoder
);
2464 /* ignore intra_slice and all the extra data */
2465 while (bit_buf
& 0x80000000)
2467 DUMPBITS (bit_buf
, bits
, 9);
2468 NEEDBITS (bit_buf
, bits
, bit_ptr
);
2471 /* decode initial macroblock address increment */
2475 if (bit_buf
>= 0x08000000)
2477 mba
= MBA_5
+ (UBITS (bit_buf
, 6) - 2);
2480 else if (bit_buf
>= 0x01800000)
2482 mba
= MBA_11
+ (UBITS (bit_buf
, 12) - 24);
2487 switch (UBITS (bit_buf
, 12))
2489 case 8: /* macroblock_escape */
2491 DUMPBITS (bit_buf
, bits
, 11);
2492 NEEDBITS (bit_buf
, bits
, bit_ptr
);
2494 case 15: /* macroblock_stuffing (MPEG1 only) */
2496 DUMPBITS (bit_buf
, bits
, 11);
2497 NEEDBITS (bit_buf
, bits
, bit_ptr
);
2499 default: /* error */
2505 DUMPBITS (bit_buf
, bits
, mba
->len
+ 1);
2506 decoder
->offset
= (offset
+ mba
->mba
) << 4;
2508 while (decoder
->offset
- decoder
->width
>= 0)
2510 decoder
->offset
-= decoder
->width
;
2512 if (!(decoder
->convert
) || decoder
->coding_type
!= B_TYPE
)
2514 decoder
->dest
[0] += decoder
->slice_stride
;
2516 decoder
->dest
[1] += decoder
->slice_uv_stride
;
2517 decoder
->dest
[2] += decoder
->slice_uv_stride
;
2521 decoder
->v_offset
+= 16;
2524 if (decoder
->v_offset
> decoder
->limit_y
)
2534 void mpeg2_slice (mpeg2_decoder_t
* const decoder
, const int code
,
2535 const uint8_t * const buffer
)
2537 #define bit_buf (decoder->bitstream_buf)
2538 #define bits (decoder->bitstream_bits)
2539 #define bit_ptr (decoder->bitstream_ptr)
2541 bitstream_init (decoder
, buffer
);
2543 if (slice_init (decoder
, code
))
2548 int macroblock_modes
;
2552 NEEDBITS (bit_buf
, bits
, bit_ptr
);
2554 macroblock_modes
= get_macroblock_modes (decoder
);
2556 /* maybe integrate MACROBLOCK_QUANT test into get_macroblock_modes ? */
2557 if (macroblock_modes
& MACROBLOCK_QUANT
)
2558 get_quantizer_scale (decoder
);
2560 if (macroblock_modes
& MACROBLOCK_INTRA
)
2562 int DCT_offset
, DCT_stride
;
2566 if (decoder
->concealment_motion_vectors
)
2568 if (decoder
->picture_structure
== FRAME_PICTURE
)
2569 motion_fr_conceal (decoder
);
2571 motion_fi_conceal (decoder
);
2575 decoder
->f_motion
.pmv
[0][0] = decoder
->f_motion
.pmv
[0][1] = 0;
2576 decoder
->f_motion
.pmv
[1][0] = decoder
->f_motion
.pmv
[1][1] = 0;
2577 decoder
->b_motion
.pmv
[0][0] = decoder
->b_motion
.pmv
[0][1] = 0;
2578 decoder
->b_motion
.pmv
[1][0] = decoder
->b_motion
.pmv
[1][1] = 0;
2581 if (macroblock_modes
& DCT_TYPE_INTERLACED
)
2583 DCT_offset
= decoder
->stride
;
2584 DCT_stride
= decoder
->stride
* 2;
2588 DCT_offset
= decoder
->stride
* 8;
2589 DCT_stride
= decoder
->stride
;
2592 offset
= decoder
->offset
;
2593 dest_y
= decoder
->dest
[0] + offset
;
2594 slice_intra_DCT (decoder
, 0, dest_y
, DCT_stride
);
2595 slice_intra_DCT (decoder
, 0, dest_y
+ 8, DCT_stride
);
2596 slice_intra_DCT (decoder
, 0, dest_y
+ DCT_offset
, DCT_stride
);
2597 slice_intra_DCT (decoder
, 0, dest_y
+ DCT_offset
+ 8, DCT_stride
);
2600 if (likely (decoder
->chroma_format
== 0))
2602 slice_intra_DCT (decoder
, 1, decoder
->dest
[1] + (offset
>> 1),
2603 decoder
->uv_stride
);
2604 slice_intra_DCT (decoder
, 2, decoder
->dest
[2] + (offset
>> 1),
2605 decoder
->uv_stride
);
2607 if (decoder
->coding_type
== D_TYPE
)
2609 NEEDBITS (bit_buf
, bits
, bit_ptr
);
2610 DUMPBITS (bit_buf
, bits
, 1);
2613 else if (likely (decoder
->chroma_format
== 1))
2615 uint8_t * dest_u
= decoder
->dest
[1] + (offset
>> 1);
2616 uint8_t * dest_v
= decoder
->dest
[2] + (offset
>> 1);
2621 slice_intra_DCT (decoder
, 1, dest_u
, DCT_stride
);
2622 slice_intra_DCT (decoder
, 2, dest_v
, DCT_stride
);
2623 slice_intra_DCT (decoder
, 1, dest_u
+ DCT_offset
, DCT_stride
);
2624 slice_intra_DCT (decoder
, 2, dest_v
+ DCT_offset
, DCT_stride
);
2628 uint8_t * dest_u
= decoder
->dest
[1] + offset
;
2629 uint8_t * dest_v
= decoder
->dest
[2] + offset
;
2631 slice_intra_DCT (decoder
, 1, dest_u
, DCT_stride
);
2632 slice_intra_DCT (decoder
, 2, dest_v
, DCT_stride
);
2633 slice_intra_DCT (decoder
, 1, dest_u
+ DCT_offset
, DCT_stride
);
2634 slice_intra_DCT (decoder
, 2, dest_v
+ DCT_offset
, DCT_stride
);
2635 slice_intra_DCT (decoder
, 1, dest_u
+ 8, DCT_stride
);
2636 slice_intra_DCT (decoder
, 2, dest_v
+ 8, DCT_stride
);
2637 slice_intra_DCT (decoder
, 1, dest_u
+ DCT_offset
+ 8,
2639 slice_intra_DCT (decoder
, 2, dest_v
+ DCT_offset
+ 8,
2643 skip_chroma_intra(decoder
);
2644 #endif /* MPEG2_COLOR */
2648 motion_parser_t
* parser
;
2651 decoder
->motion_parser
[macroblock_modes
>> MOTION_TYPE_SHIFT
];
2652 MOTION_CALL (parser
, macroblock_modes
);
2654 if (macroblock_modes
& MACROBLOCK_PATTERN
)
2656 int coded_block_pattern
;
2657 int DCT_offset
, DCT_stride
;
2659 if (macroblock_modes
& DCT_TYPE_INTERLACED
)
2661 DCT_offset
= decoder
->stride
;
2662 DCT_stride
= decoder
->stride
* 2;
2666 DCT_offset
= decoder
->stride
* 8;
2667 DCT_stride
= decoder
->stride
;
2670 coded_block_pattern
= get_coded_block_pattern (decoder
);
2672 if (likely (decoder
->chroma_format
== 0))
2674 int offset
= decoder
->offset
;
2675 uint8_t * dest_y
= decoder
->dest
[0] + offset
;
2677 if (coded_block_pattern
& 1)
2678 slice_non_intra_DCT (decoder
, 0, dest_y
, DCT_stride
);
2680 if (coded_block_pattern
& 2)
2681 slice_non_intra_DCT (decoder
, 0, dest_y
+ 8,
2684 if (coded_block_pattern
& 4)
2685 slice_non_intra_DCT (decoder
, 0, dest_y
+ DCT_offset
,
2688 if (coded_block_pattern
& 8)
2689 slice_non_intra_DCT (decoder
, 0,
2690 dest_y
+ DCT_offset
+ 8,
2693 if (coded_block_pattern
& 16)
2694 slice_non_intra_DCT (decoder
, 1,
2695 decoder
->dest
[1] + (offset
>> 1),
2696 decoder
->uv_stride
);
2698 if (coded_block_pattern
& 32)
2699 slice_non_intra_DCT (decoder
, 2,
2700 decoder
->dest
[2] + (offset
>> 1),
2701 decoder
->uv_stride
);
2702 #endif /* MPEG2_COLOR */
2704 else if (likely (decoder
->chroma_format
== 1))
2709 coded_block_pattern
|= bit_buf
& (3 << 30);
2710 DUMPBITS (bit_buf
, bits
, 2);
2712 offset
= decoder
->offset
;
2713 dest_y
= decoder
->dest
[0] + offset
;
2715 if (coded_block_pattern
& 1)
2716 slice_non_intra_DCT (decoder
, 0, dest_y
, DCT_stride
);
2718 if (coded_block_pattern
& 2)
2719 slice_non_intra_DCT (decoder
, 0, dest_y
+ 8,
2722 if (coded_block_pattern
& 4)
2723 slice_non_intra_DCT (decoder
, 0, dest_y
+ DCT_offset
,
2726 if (coded_block_pattern
& 8)
2727 slice_non_intra_DCT (decoder
, 0,
2728 dest_y
+ DCT_offset
+ 8,
2732 DCT_offset
= (DCT_offset
+ offset
) >> 1;
2734 if (coded_block_pattern
& 16)
2735 slice_non_intra_DCT (decoder
, 1,
2736 decoder
->dest
[1] + (offset
>> 1),
2739 if (coded_block_pattern
& 32)
2740 slice_non_intra_DCT (decoder
, 2,
2741 decoder
->dest
[2] + (offset
>> 1),
2744 if (coded_block_pattern
& (2 << 30))
2745 slice_non_intra_DCT (decoder
, 1,
2746 decoder
->dest
[1] + DCT_offset
,
2749 if (coded_block_pattern
& (1 << 30))
2750 slice_non_intra_DCT (decoder
, 2,
2751 decoder
->dest
[2] + DCT_offset
,
2753 #endif /* MPEG2_COLOR */
2757 int offset
= decoder
->offset
;
2758 uint8_t * dest_y
= decoder
->dest
[0] + offset
;
2760 uint8_t * dest_u
= decoder
->dest
[1] + offset
;
2761 uint8_t * dest_v
= decoder
->dest
[2] + offset
;
2763 coded_block_pattern
|= bit_buf
& (63 << 26);
2764 DUMPBITS (bit_buf
, bits
, 6);
2766 if (coded_block_pattern
& 1)
2767 slice_non_intra_DCT (decoder
, 0, dest_y
, DCT_stride
);
2769 if (coded_block_pattern
& 2)
2770 slice_non_intra_DCT (decoder
, 0, dest_y
+ 8,
2773 if (coded_block_pattern
& 4)
2774 slice_non_intra_DCT (decoder
, 0, dest_y
+ DCT_offset
,
2777 if (coded_block_pattern
& 8)
2778 slice_non_intra_DCT (decoder
, 0,
2779 dest_y
+ DCT_offset
+ 8,
2782 if (coded_block_pattern
& 16)
2783 slice_non_intra_DCT (decoder
, 1, dest_u
, DCT_stride
);
2785 if (coded_block_pattern
& 32)
2786 slice_non_intra_DCT (decoder
, 2, dest_v
, DCT_stride
);
2788 if (coded_block_pattern
& (32 << 26))
2789 slice_non_intra_DCT (decoder
, 1, dest_u
+ DCT_offset
,
2792 if (coded_block_pattern
& (16 << 26))
2793 slice_non_intra_DCT (decoder
, 2, dest_v
+ DCT_offset
,
2796 if (coded_block_pattern
& (8 << 26))
2797 slice_non_intra_DCT (decoder
, 1, dest_u
+ 8,
2800 if (coded_block_pattern
& (4 << 26))
2801 slice_non_intra_DCT (decoder
, 2, dest_v
+ 8,
2804 if (coded_block_pattern
& (2 << 26))
2805 slice_non_intra_DCT (decoder
, 1,
2806 dest_u
+ DCT_offset
+ 8,
2809 if (coded_block_pattern
& (1 << 26))
2810 slice_non_intra_DCT (decoder
, 2,
2811 dest_v
+ DCT_offset
+ 8,
2813 #endif /* MPEG2_COLOR */
2816 skip_chroma_non_intra(decoder
, coded_block_pattern
);
2821 decoder
->dc_dct_pred
[0] = decoder
->dc_dct_pred
[1] =
2822 decoder
->dc_dct_pred
[2] = 16384;
2824 decoder
->dc_dct_pred
[0] = 16384;
2830 NEEDBITS (bit_buf
, bits
, bit_ptr
);
2835 if (bit_buf
>= 0x10000000)
2837 mba
= MBA_5
+ (UBITS (bit_buf
, 5) - 2);
2840 else if (bit_buf
>= 0x03000000)
2842 mba
= MBA_11
+ (UBITS (bit_buf
, 11) - 24);
2847 switch (UBITS (bit_buf
, 11))
2849 case 8: /* macroblock_escape */
2852 case 15: /* macroblock_stuffing (MPEG1 only) */
2853 DUMPBITS (bit_buf
, bits
, 11);
2854 NEEDBITS (bit_buf
, bits
, bit_ptr
);
2856 default: /* end of slice, or error */
2862 DUMPBITS (bit_buf
, bits
, mba
->len
);
2863 mba_inc
+= mba
->mba
;
2868 decoder
->dc_dct_pred
[0] = decoder
->dc_dct_pred
[1] =
2869 decoder
->dc_dct_pred
[2] = 16384;
2871 decoder
->dc_dct_pred
[0] = 16384;
2873 if (decoder
->coding_type
== P_TYPE
)
2877 MOTION_CALL (decoder
->motion_parser
[0],
2878 MACROBLOCK_MOTION_FORWARD
);
2887 MOTION_CALL (decoder
->motion_parser
[4], macroblock_modes
);