Update the discussion of themeing in the manual, and put a note in the wps tags appen...
[kugel-rb.git] / apps / plugins / mpegplayer / slice.c
blob926333d5d07595ffc6fa8f8f36bf8918a13753b4
1 /*
2 * slice.c
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
24 * $Id$
25 * libmpeg2 sync history:
26 * 2008-07-01 - CVS revision 1.55
29 #include "plugin.h"
31 #include "mpeg2dec_config.h"
33 #include "mpeg2.h"
34 #include "attributes.h"
35 #include "mpeg2_internal.h"
37 #include "vlc.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)
45 int macroblock_modes;
46 const MBtab * tab;
48 switch (decoder->coding_type)
50 case I_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;
64 case P_TYPE:
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;
86 else
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;
103 case B_TYPE:
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;
124 else
126 if (macroblock_modes & MACROBLOCK_INTRA)
127 goto 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))
134 intra:
135 macroblock_modes |= UBITS (bit_buf, 1) * DCT_TYPE_INTERLACED;
136 DUMPBITS (bit_buf, bits, 1);
138 return macroblock_modes;
141 case D_TYPE:
142 DUMPBITS (bit_buf, bits, 1);
143 return MACROBLOCK_INTRA;
145 default:
146 return 0;
148 #undef bit_buf
149 #undef bits
150 #undef bit_ptr
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];
175 #undef bit_buf
176 #undef bits
177 #undef bit_ptr
180 static inline int get_motion_delta (mpeg2_decoder_t * const decoder,
181 const int f_code)
183 #define bit_buf (decoder->bitstream_buf)
184 #define bits (decoder->bitstream_bits)
185 #define bit_ptr (decoder->bitstream_ptr)
187 int delta;
188 int sign;
189 const MVtab * tab;
191 if (bit_buf & 0x80000000)
193 DUMPBITS (bit_buf, bits, 1);
194 return 0;
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);
204 bit_buf <<= 1;
206 if (f_code)
207 delta += UBITS (bit_buf, f_code);
208 bit_buf <<= f_code;
210 return (delta ^ sign) - sign;
212 else
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);
220 bit_buf <<= 1;
222 if (f_code)
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;
232 #undef bit_buf
233 #undef bits
234 #undef bit_ptr
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)
248 const DMVtab * tab;
250 tab = DMV_2 + UBITS (bit_buf, 2);
251 DUMPBITS (bit_buf, bits, tab->len);
252 return tab->dmv;
254 #undef bit_buf
255 #undef bits
256 #undef bit_ptr
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)
265 const CBPtab * tab;
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);
273 return tab->cbp;
275 else
277 tab = CBP_9 + UBITS (bit_buf, 9);
278 DUMPBITS (bit_buf, bits, tab->len);
279 return tab->cbp;
282 #undef bit_buf
283 #undef bits
284 #undef bit_ptr
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)
293 const DCtab * tab;
294 int size;
295 int dc_diff;
297 if (bit_buf < 0xf8000000)
299 tab = DC_lum_5 + UBITS (bit_buf, 5);
300 size = tab->size;
302 if (size)
304 bits += tab->len + size;
305 bit_buf <<= tab->len;
306 dc_diff =
307 UBITS (bit_buf, size) - UBITS (SBITS (~bit_buf, 1), size);
308 bit_buf <<= size;
309 return dc_diff << decoder->intra_dc_precision;
311 else
313 DUMPBITS (bit_buf, bits, 3);
314 return 0;
317 else
319 tab = DC_long + (UBITS (bit_buf, 9) - 0x1e0);
320 size = tab->size;
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;
328 #undef bit_buf
329 #undef bits
330 #undef bit_ptr
333 #if MPEG2_COLOR
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)
340 const DCtab * tab;
341 int size;
342 int dc_diff;
344 if (bit_buf < 0xf8000000)
346 tab = DC_chrom_5 + UBITS (bit_buf, 5);
347 size = tab->size;
349 if (size)
351 bits += tab->len + size;
352 bit_buf <<= tab->len;
353 dc_diff =
354 UBITS (bit_buf, size) - UBITS (SBITS (~bit_buf, 1), size);
355 bit_buf <<= size;
356 return dc_diff << decoder->intra_dc_precision;
358 else
360 DUMPBITS (bit_buf, bits, 2);
361 return 0;
364 else
366 tab = DC_long + (UBITS (bit_buf, 10) - 0x3e0);
367 size = tab->size;
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;
375 #undef bit_buf
376 #undef bits
377 #undef bit_ptr
379 #endif /* MPEG2_COLOR */
381 #define SATURATE(val) \
382 do { \
383 val <<= 4; \
384 if (unlikely (val != (int16_t) val)) \
385 val = (SBITS (val, 1) ^ 2047) << 4; \
386 } while (0)
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];
397 int i = 0;
398 int j;
399 int val;
400 const DCTtab * tab;
402 NEEDBITS (bit_buf, bits, bit_ptr);
404 while (1)
406 if (bit_buf >= 0x28000000)
408 tab = DCT_B14AC_5 + (UBITS (bit_buf, 5) - 5);
410 i += tab->run;
411 if (i >= 64)
412 break; /* end of block */
414 normal_code:
415 j = scan[i];
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);
423 SATURATE (val);
424 dest[j] = val;
425 mismatch ^= val;
427 bit_buf <<= 1;
428 NEEDBITS (bit_buf, bits, bit_ptr);
430 continue;
432 else if (bit_buf >= 0x04000000)
434 tab = DCT_B14_8 + (UBITS (bit_buf, 8) - 4);
436 i += tab->run;
437 if (i < 64)
438 goto normal_code;
440 /* escape code */
442 i += UBITS (bit_buf << 6, 6) - 64;
443 if (i >= 64)
444 break; /* illegal, check needed to avoid buffer overflow */
446 j = scan[i];
448 DUMPBITS (bit_buf, bits, 12);
449 NEEDBITS (bit_buf, bits, bit_ptr);
450 val = (SBITS (bit_buf, 12) * quant_matrix[j]) / 16;
452 SATURATE (val);
453 dest[j] = val;
454 mismatch ^= val;
456 DUMPBITS (bit_buf, bits, 12);
457 NEEDBITS (bit_buf, bits, bit_ptr);
459 continue;
461 else if (bit_buf >= 0x02000000)
463 tab = DCT_B14_10 + (UBITS (bit_buf, 10) - 8);
464 i += tab->run;
465 if (i < 64)
466 goto normal_code;
468 else if (bit_buf >= 0x00800000)
470 tab = DCT_13 + (UBITS (bit_buf, 13) - 16);
471 i += tab->run;
472 if (i < 64)
473 goto normal_code;
475 else if (bit_buf >= 0x00200000)
477 tab = DCT_15 + (UBITS (bit_buf, 15) - 16);
478 i += tab->run;
479 if (i < 64)
480 goto normal_code;
482 else
484 tab = DCT_16 + UBITS (bit_buf, 16);
485 bit_buf <<= 16;
486 GETWORD (bit_buf, bits + 16, bit_ptr);
487 i += tab->run;
488 if (i < 64)
489 goto normal_code;
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];
510 int i = 0;
511 int j;
512 int val;
513 const DCTtab * tab;
515 NEEDBITS (bit_buf, bits, bit_ptr);
517 while (1)
519 if (bit_buf >= 0x04000000)
521 tab = DCT_B15_8 + (UBITS (bit_buf, 8) - 4);
523 i += tab->run;
525 if (i < 64)
527 normal_code:
528 j = scan[i];
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);
536 SATURATE (val);
537 dest[j] = val;
538 mismatch ^= val;
540 bit_buf <<= 1;
541 NEEDBITS (bit_buf, bits, bit_ptr);
543 continue;
545 else
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 */
552 /* escape code */
554 i += UBITS (bit_buf << 6, 6) - 64;
555 if (i >= 64)
556 break; /* illegal, check against buffer overflow */
558 j = scan[i];
560 DUMPBITS (bit_buf, bits, 12);
561 NEEDBITS (bit_buf, bits, bit_ptr);
562 val = (SBITS (bit_buf, 12) * quant_matrix[j]) / 16;
564 SATURATE (val);
565 dest[j] = val;
566 mismatch ^= val;
568 DUMPBITS (bit_buf, bits, 12);
569 NEEDBITS (bit_buf, bits, bit_ptr);
571 continue;
574 else if (bit_buf >= 0x02000000)
576 tab = DCT_B15_10 + (UBITS (bit_buf, 10) - 8);
577 i += tab->run;
578 if (i < 64)
579 goto normal_code;
581 else if (bit_buf >= 0x00800000)
583 tab = DCT_13 + (UBITS (bit_buf, 13) - 16);
584 i += tab->run;
585 if (i < 64)
586 goto normal_code;
588 else if (bit_buf >= 0x00200000)
590 tab = DCT_15 + (UBITS (bit_buf, 15) - 16);
591 i += tab->run;
592 if (i < 64)
593 goto normal_code;
595 else
597 tab = DCT_16 + UBITS (bit_buf, 16);
598 bit_buf <<= 16;
599 GETWORD (bit_buf, bits + 16, bit_ptr);
600 i += tab->run;
601 if (i < 64)
602 goto normal_code;
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;
622 int mismatch = -1;
623 int i = -1;
624 int j;
625 int val;
626 const DCTtab * tab;
628 NEEDBITS (bit_buf, bits, bit_ptr);
630 if (bit_buf >= 0x28000000)
632 tab = DCT_B14DC_5 + (UBITS (bit_buf, 5) - 5);
633 goto entry_1;
635 else
637 goto entry_2;
640 while (1)
642 if (bit_buf >= 0x28000000)
644 tab = DCT_B14AC_5 + (UBITS (bit_buf, 5) - 5);
646 entry_1:
647 i += tab->run;
648 if (i >= 64)
649 break; /* end of block */
651 normal_code:
652 j = scan[i];
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);
660 SATURATE (val);
661 dest[j] = val;
662 mismatch ^= val;
664 bit_buf <<= 1;
665 NEEDBITS (bit_buf, bits, bit_ptr);
667 continue;
670 entry_2:
671 if (bit_buf >= 0x04000000)
673 tab = DCT_B14_8 + (UBITS (bit_buf, 8) - 4);
675 i += tab->run;
676 if (i < 64)
677 goto normal_code;
679 /* escape code */
681 i += UBITS (bit_buf << 6, 6) - 64;
682 if (i >= 64)
683 break; /* illegal, check needed to avoid buffer overflow */
685 j = scan[i];
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;
692 SATURATE (val);
693 dest[j] = val;
694 mismatch ^= val;
696 DUMPBITS (bit_buf, bits, 12);
697 NEEDBITS (bit_buf, bits, bit_ptr);
699 continue;
701 else if (bit_buf >= 0x02000000)
703 tab = DCT_B14_10 + (UBITS (bit_buf, 10) - 8);
704 i += tab->run;
705 if (i < 64)
706 goto normal_code;
708 else if (bit_buf >= 0x00800000)
710 tab = DCT_13 + (UBITS (bit_buf, 13) - 16);
711 i += tab->run;
712 if (i < 64)
713 goto normal_code;
715 else if (bit_buf >= 0x00200000)
717 tab = DCT_15 + (UBITS (bit_buf, 15) - 16);
718 i += tab->run;
719 if (i < 64)
720 goto normal_code;
722 else
724 tab = DCT_16 + UBITS (bit_buf, 16);
725 bit_buf <<= 16;
726 GETWORD (bit_buf, bits + 16, bit_ptr);
727 i += tab->run;
728 if (i < 64)
729 goto normal_code;
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;
739 return i;
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;
750 int i = 0;
751 int j;
752 int val;
753 const DCTtab * tab;
755 NEEDBITS (bit_buf, bits, bit_ptr);
757 while (1)
759 if (bit_buf >= 0x28000000)
761 tab = DCT_B14AC_5 + (UBITS (bit_buf, 5) - 5);
763 i += tab->run;
764 if (i >= 64)
765 break; /* end of block */
767 normal_code:
768 j = scan[i];
769 bit_buf <<= tab->len;
770 bits += tab->len + 1;
771 val = (tab->level * quant_matrix[j]) >> 4;
773 /* oddification */
774 val = (val - 1) | 1;
776 /* if (bitstream_get (1)) val = -val; */
777 val = (val ^ SBITS (bit_buf, 1)) - SBITS (bit_buf, 1);
779 SATURATE (val);
780 dest[j] = val;
782 bit_buf <<= 1;
783 NEEDBITS (bit_buf, bits, bit_ptr);
785 continue;
787 else if (bit_buf >= 0x04000000)
789 tab = DCT_B14_8 + (UBITS (bit_buf, 8) - 4);
791 i += tab->run;
792 if (i < 64)
793 goto normal_code;
795 /* escape code */
797 i += UBITS (bit_buf << 6, 6) - 64;
798 if (i >= 64)
799 break; /* illegal, check needed to avoid buffer overflow */
801 j = scan[i];
803 DUMPBITS (bit_buf, bits, 12);
804 NEEDBITS (bit_buf, bits, bit_ptr);
805 val = SBITS (bit_buf, 8);
807 if (! (val & 0x7f))
809 DUMPBITS (bit_buf, bits, 8);
810 val = UBITS (bit_buf, 8) + 2 * val;
813 val = (val * quant_matrix[j]) / 16;
815 /* oddification */
816 val = (val + ~SBITS (val, 1)) | 1;
818 SATURATE (val);
819 dest[j] = val;
821 DUMPBITS (bit_buf, bits, 8);
822 NEEDBITS (bit_buf, bits, bit_ptr);
824 continue;
826 else if (bit_buf >= 0x02000000)
828 tab = DCT_B14_10 + (UBITS (bit_buf, 10) - 8);
829 i += tab->run;
830 if (i < 64)
831 goto normal_code;
833 else if (bit_buf >= 0x00800000)
835 tab = DCT_13 + (UBITS (bit_buf, 13) - 16);
836 i += tab->run;
837 if (i < 64)
838 goto normal_code;
840 else if (bit_buf >= 0x00200000)
842 tab = DCT_15 + (UBITS (bit_buf, 15) - 16);
843 i += tab->run;
844 if (i < 64)
845 goto normal_code;
847 else
849 tab = DCT_16 + UBITS (bit_buf, 16);
850 bit_buf <<= 16;
851 GETWORD (bit_buf, bits + 16, bit_ptr);
852 i += tab->run;
853 if (i < 64)
854 goto normal_code;
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;
873 int i = -1;
874 int j;
875 int val;
876 const DCTtab * tab;
878 NEEDBITS (bit_buf, bits, bit_ptr);
879 if (bit_buf >= 0x28000000)
881 tab = DCT_B14DC_5 + (UBITS (bit_buf, 5) - 5);
882 goto entry_1;
884 else
886 goto entry_2;
889 while (1)
891 if (bit_buf >= 0x28000000)
893 tab = DCT_B14AC_5 + (UBITS (bit_buf, 5) - 5);
895 entry_1:
896 i += tab->run;
897 if (i >= 64)
898 break; /* end of block */
900 normal_code:
901 j = scan[i];
902 bit_buf <<= tab->len;
903 bits += tab->len + 1;
904 val = ((2 * tab->level + 1) * quant_matrix[j]) >> 5;
906 /* oddification */
907 val = (val - 1) | 1;
909 /* if (bitstream_get (1)) val = -val; */
910 val = (val ^ SBITS (bit_buf, 1)) - SBITS (bit_buf, 1);
912 SATURATE (val);
913 dest[j] = val;
915 bit_buf <<= 1;
916 NEEDBITS (bit_buf, bits, bit_ptr);
918 continue;
921 entry_2:
922 if (bit_buf >= 0x04000000)
924 tab = DCT_B14_8 + (UBITS (bit_buf, 8) - 4);
926 i += tab->run;
927 if (i < 64)
928 goto normal_code;
930 /* escape code */
932 i += UBITS (bit_buf << 6, 6) - 64;
933 if (i >= 64)
934 break; /* illegal, check needed to avoid buffer overflow */
936 j = scan[i];
938 DUMPBITS (bit_buf, bits, 12);
939 NEEDBITS (bit_buf, bits, bit_ptr);
940 val = SBITS (bit_buf, 8);
942 if (! (val & 0x7f))
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;
951 /* oddification */
952 val = (val + ~SBITS (val, 1)) | 1;
954 SATURATE (val);
955 dest[j] = val;
957 DUMPBITS (bit_buf, bits, 8);
958 NEEDBITS (bit_buf, bits, bit_ptr);
960 continue;
963 else if (bit_buf >= 0x02000000)
965 tab = DCT_B14_10 + (UBITS (bit_buf, 10) - 8);
966 i += tab->run;
967 if (i < 64)
968 goto normal_code;
970 else if (bit_buf >= 0x00800000)
972 tab = DCT_13 + (UBITS (bit_buf, 13) - 16);
973 i += tab->run;
974 if (i < 64)
975 goto normal_code;
977 else if (bit_buf >= 0x00200000)
979 tab = DCT_15 + (UBITS (bit_buf, 15) - 16);
980 i += tab->run;
981 if (i < 64)
982 goto normal_code;
984 else
986 tab = DCT_16 + UBITS (bit_buf, 16);
987 bit_buf <<= 16;
988 GETWORD (bit_buf, bits + 16, bit_ptr);
989 i += tab->run;
990 if (i < 64)
991 goto normal_code;
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;
1000 return i;
1003 static inline void slice_intra_DCT (mpeg2_decoder_t * const decoder,
1004 const int cc,
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 */
1013 if (cc == 0)
1015 decoder->dc_dct_pred[0] += get_luma_dc_dct_diff (decoder);
1016 decoder->DCTblock[0] = decoder->dc_dct_pred[0];
1019 #if MPEG2_COLOR
1020 else
1022 decoder->dc_dct_pred[cc] += get_chroma_dc_dct_diff (decoder);
1023 decoder->DCTblock[0] = decoder->dc_dct_pred[cc];
1025 #endif
1027 if (decoder->mpeg1)
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]);
1036 else
1038 get_intra_block_B14 (decoder, decoder->quantizer_matrix[cc ? 2 : 0]);
1041 mpeg2_idct_copy (decoder->DCTblock, dest, stride);
1043 #undef bit_buf
1044 #undef bits
1045 #undef bit_ptr
1048 static inline void slice_non_intra_DCT (mpeg2_decoder_t * const decoder,
1049 const int cc,
1050 uint8_t * const dest, const int stride)
1052 int last;
1054 if (decoder->mpeg1)
1056 last = get_mpeg1_non_intra_block (decoder);
1058 else
1060 last = get_non_intra_block (decoder,
1061 decoder->quantizer_matrix[cc ? 3 : 1]);
1064 mpeg2_idct_add (last, decoder->DCTblock, dest, stride);
1067 #if !MPEG2_COLOR
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;
1073 int i = 0;
1074 const DCTtab * tab;
1076 NEEDBITS (bit_buf, bits, bit_ptr);
1078 while (1)
1080 if (bit_buf >= 0x28000000)
1082 tab = DCT_B14AC_5 + (UBITS (bit_buf, 5) - 5);
1084 i += tab->run;
1085 if (i >= 64)
1086 break; /* end of block */
1088 normal_code:
1089 bit_buf <<= tab->len + 1;
1090 bits += tab->len + 1;
1091 NEEDBITS (bit_buf, bits, bit_ptr);
1092 continue;
1094 else if (bit_buf >= 0x04000000)
1096 tab = DCT_B14_8 + (UBITS (bit_buf, 8) - 4);
1098 i += tab->run;
1099 if (i < 64)
1100 goto normal_code;
1102 /* escape code */
1104 i += UBITS (bit_buf << 6, 6) - 64;
1105 if (i >= 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);
1117 continue;
1119 else if (bit_buf >= 0x02000000)
1121 tab = DCT_B14_10 + (UBITS (bit_buf, 10) - 8);
1122 i += tab->run;
1123 if (i < 64)
1124 goto normal_code;
1126 else if (bit_buf >= 0x00800000)
1128 tab = DCT_13 + (UBITS (bit_buf, 13) - 16);
1129 i += tab->run;
1130 if (i < 64)
1131 goto normal_code;
1133 else if (bit_buf >= 0x00200000)
1135 tab = DCT_15 + (UBITS (bit_buf, 15) - 16);
1136 i += tab->run;
1137 if (i < 64)
1138 goto normal_code;
1140 else
1142 tab = DCT_16 + UBITS (bit_buf, 16);
1143 bit_buf <<= 16;
1144 GETWORD (bit_buf, bits + 16, bit_ptr);
1145 i += tab->run;
1146 if (i < 64)
1147 goto normal_code;
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;
1163 int i = 0;
1164 const DCTtab * tab;
1166 NEEDBITS (bit_buf, bits, bit_ptr);
1168 while (1)
1170 if (bit_buf >= 0x28000000)
1172 tab = DCT_B14AC_5 + (UBITS (bit_buf, 5) - 5);
1174 i += tab->run;
1175 if (i >= 64)
1176 break; /* end of block */
1178 normal_code:
1179 bit_buf <<= tab->len + 1;
1180 bits += tab->len + 1;
1181 NEEDBITS (bit_buf, bits, bit_ptr);
1182 continue;
1184 else if (bit_buf >= 0x04000000)
1186 tab = DCT_B14_8 + (UBITS (bit_buf, 8) - 4);
1188 i += tab->run;
1189 if (i < 64)
1190 goto normal_code;
1192 /* escape code */
1194 i += UBITS (bit_buf << 6, 6) - 64;
1195 if (i >= 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);
1202 continue;
1204 else if (bit_buf >= 0x02000000)
1206 tab = DCT_B14_10 + (UBITS (bit_buf, 10) - 8);
1207 i += tab->run;
1208 if (i < 64)
1209 goto normal_code;
1211 else if (bit_buf >= 0x00800000)
1213 tab = DCT_13 + (UBITS (bit_buf, 13) - 16);
1214 i += tab->run;
1215 if (i < 64)
1216 goto normal_code;
1218 else if (bit_buf >= 0x00200000)
1220 tab = DCT_15 + (UBITS (bit_buf, 15) - 16);
1221 i += tab->run;
1222 if (i < 64)
1223 goto normal_code;
1225 else
1227 tab = DCT_16 + UBITS (bit_buf, 16);
1228 bit_buf <<= 16;
1229 GETWORD (bit_buf, bits + 16, bit_ptr);
1230 i += tab->run;
1231 if (i < 64)
1232 goto normal_code;
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;
1248 int i = 0;
1249 const DCTtab * tab;
1251 NEEDBITS (bit_buf, bits, bit_ptr);
1253 while (1)
1255 if (bit_buf >= 0x04000000)
1257 tab = DCT_B15_8 + (UBITS (bit_buf, 8) - 4);
1259 i += tab->run;
1261 if (i < 64)
1263 normal_code:
1264 bit_buf <<= tab->len + 1;
1265 bits += tab->len + 1;
1266 NEEDBITS (bit_buf, bits, bit_ptr);
1267 continue;
1269 else
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 */
1276 /* escape code */
1278 i += UBITS (bit_buf << 6, 6) - 64;
1279 if (i >= 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);
1286 continue;
1289 else if (bit_buf >= 0x02000000)
1291 tab = DCT_B15_10 + (UBITS (bit_buf, 10) - 8);
1292 i += tab->run;
1293 if (i < 64)
1294 goto normal_code;
1296 else if (bit_buf >= 0x00800000)
1298 tab = DCT_13 + (UBITS (bit_buf, 13) - 16);
1299 i += tab->run;
1300 if (i < 64)
1301 goto normal_code;
1303 else if (bit_buf >= 0x00200000)
1305 tab = DCT_15 + (UBITS (bit_buf, 15) - 16);
1306 i += tab->run;
1307 if (i < 64)
1308 goto normal_code;
1310 else
1312 tab = DCT_16 + UBITS (bit_buf, 16);
1313 bit_buf <<= 16;
1314 GETWORD (bit_buf, bits + 16, bit_ptr);
1315 i += tab->run;
1316 if (i < 64)
1317 goto normal_code;
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;
1333 int i = -1;
1334 const DCTtab * tab;
1336 NEEDBITS (bit_buf, bits, bit_ptr);
1338 if (bit_buf >= 0x28000000)
1340 tab = DCT_B14DC_5 + (UBITS (bit_buf, 5) - 5);
1341 goto entry_1;
1343 else
1345 goto entry_2;
1348 while (1)
1350 if (bit_buf >= 0x28000000)
1352 tab = DCT_B14AC_5 + (UBITS (bit_buf, 5) - 5);
1354 entry_1:
1355 i += tab->run;
1356 if (i >= 64)
1357 break; /* end of block */
1359 normal_code:
1360 bit_buf <<= tab->len + 1;
1361 bits += tab->len + 1;
1362 NEEDBITS (bit_buf, bits, bit_ptr);
1364 continue;
1367 entry_2:
1368 if (bit_buf >= 0x04000000)
1370 tab = DCT_B14_8 + (UBITS (bit_buf, 8) - 4);
1372 i += tab->run;
1373 if (i < 64)
1374 goto normal_code;
1376 /* escape code */
1378 i += UBITS (bit_buf << 6, 6) - 64;
1379 if (i >= 64)
1380 break; /* illegal, check needed to avoid buffer overflow */
1382 if (decoder->mpeg1)
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);
1392 else
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);
1400 continue;
1402 else if (bit_buf >= 0x02000000)
1404 tab = DCT_B14_10 + (UBITS (bit_buf, 10) - 8);
1405 i += tab->run;
1406 if (i < 64)
1407 goto normal_code;
1409 else if (bit_buf >= 0x00800000)
1411 tab = DCT_13 + (UBITS (bit_buf, 13) - 16);
1412 i += tab->run;
1413 if (i < 64)
1414 goto normal_code;
1416 else if (bit_buf >= 0x00200000)
1418 tab = DCT_15 + (UBITS (bit_buf, 15) - 16);
1419 i += tab->run;
1420 if (i < 64)
1421 goto normal_code;
1423 else
1425 tab = DCT_16 + UBITS (bit_buf, 16);
1426 bit_buf <<= 16;
1427 GETWORD (bit_buf, bits + 16, bit_ptr);
1428 i += tab->run;
1429 if (i < 64)
1430 goto normal_code;
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)
1447 const DCtab * tab;
1448 int size;
1450 if (bit_buf < 0xf8000000)
1452 tab = DC_chrom_5 + UBITS (bit_buf, 5);
1453 size = tab->size;
1455 if (size)
1457 bits += tab->len + size;
1458 bit_buf <<= tab->len;
1459 bit_buf <<= size;
1461 else
1463 DUMPBITS (bit_buf, bits, 2);
1466 else
1468 tab = DC_long + (UBITS (bit_buf, 10) - 0x3e0);
1469 size = tab->size;
1470 DUMPBITS (bit_buf, bits, tab->len + 1);
1471 NEEDBITS (bit_buf, bits, bit_ptr);
1472 DUMPBITS (bit_buf, bits, size);
1475 #undef bit_buf
1476 #undef bits
1477 #undef bit_ptr
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] =
1485 0x00000030,
1486 0xc0000030,
1487 0xfc000030,
1490 uint32_t cbp = coded_block_pattern &
1491 cbp_mask[MIN((unsigned)decoder->chroma_format, 2u)];
1493 while (cbp)
1495 skip_non_intra_block (decoder);
1496 cbp &= (cbp - 1);
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)
1508 i = 8;
1510 while (i-- > 0)
1512 NEEDBITS (bit_buf, bits, bit_ptr);
1514 skip_chroma_dc_dct_diff (decoder);
1516 if (decoder->mpeg1)
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);
1525 else
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);
1537 #undef bit_buf
1538 #undef bits
1539 #undef bit_ptr
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); \
1564 if (MPEG2_COLOR) \
1566 motion_x /= 2; \
1567 motion_y /= 2; \
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 + \
1600 decoder->offset, \
1601 (ref[0] + (pos_x >> 1) + \
1602 ((pos_y op) + src_field) * decoder->stride), \
1603 2 * decoder->stride, 8); \
1605 if (MPEG2_COLOR) \
1607 motion_x /= 2; \
1608 motion_y /= 2; \
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); \
1646 if (MPEG2_COLOR) \
1648 motion_x /= 2; \
1649 motion_y /= 2; \
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); \
1674 if (MPEG2_COLOR) \
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); \
1707 if (MPEG2_COLOR) \
1709 offset = (offset + (motion_x & (motion_x < 0))) >> 1; \
1710 motion_x /= 2; \
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); \
1745 if (MPEG2_COLOR) \
1747 offset = (offset + (motion_x & (motion_x < 0))) >> 1; \
1748 motion_x /= 2; \
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); \
1784 if (MPEG2_COLOR) \
1786 offset = (offset + (motion_x & (motion_x < 0))) >> 1; \
1787 motion_x /= 2; \
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); \
1809 if (MPEG2_COLOR) \
1811 offset >>= 1; \
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); \
1840 if (MPEG2_COLOR) \
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); \
1872 if (MPEG2_COLOR) \
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); \
1907 if (MPEG2_COLOR) \
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); \
1927 if (MPEG2_COLOR) \
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; \
2039 (void)table; \
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; \
2168 (void)table; \
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,
2189 MOTION_ZERO_420)
2190 MOTION_FUNCTIONS (422, MOTION_422, MOTION_FIELD_422, MOTION_DMV_422,
2191 MOTION_ZERO_422)
2192 MOTION_FUNCTIONS (444, MOTION_444, MOTION_FIELD_444, MOTION_DMV_444,
2193 MOTION_ZERO_444)
2195 /* like motion_frame, but parsing without actual motion compensation */
2196 static void motion_fr_conceal (mpeg2_decoder_t * const decoder)
2198 int tmp;
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)
2217 int tmp;
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 */
2241 #undef bit_buf
2242 #undef bits
2243 #undef bit_ptr
2245 #define MOTION_CALL(routine, direction) \
2246 do { \
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)); \
2256 } while (0)
2258 #define NEXT_MACROBLOCK \
2259 do { \
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) \
2270 break; \
2273 decoder->dest[0] += decoder->slice_stride; \
2274 if (MPEG2_COLOR) \
2276 decoder->dest[1] += decoder->slice_uv_stride; \
2277 decoder->dest[2] += decoder->slice_uv_stride; \
2279 } while (0); \
2281 decoder->v_offset += 16; \
2283 if (decoder->v_offset > decoder->limit_y) \
2284 return; \
2286 decoder->offset = 0; \
2288 } while (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;
2303 #if MPEG2_COLOR
2304 decoder->picture_dest[1] = current_fbuf[1] + (offset >> 1);
2305 decoder->picture_dest[2] = current_fbuf[2] + (offset >> 1);
2306 #endif
2308 decoder->f_motion.ref[0][0] = forward_fbuf[0] + offset;
2309 #if MPEG2_COLOR
2310 decoder->f_motion.ref[0][1] = forward_fbuf[1] + (offset >> 1);
2311 decoder->f_motion.ref[0][2] = forward_fbuf[2] + (offset >> 1);
2312 #endif
2314 decoder->b_motion.ref[0][0] = backward_fbuf[0] + offset;
2315 #if MPEG2_COLOR
2316 decoder->b_motion.ref[0][1] = backward_fbuf[1] + (offset >> 1);
2317 decoder->b_motion.ref[0][2] = backward_fbuf[2] + (offset >> 1);
2318 #endif
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;
2333 #if MPEG2_COLOR
2334 decoder->f_motion.ref[1][1] = forward_fbuf[1] + (offset >> 1);
2335 decoder->f_motion.ref[1][2] = forward_fbuf[2] + (offset >> 1);
2336 #endif
2337 decoder->b_motion.ref[1][0] = backward_fbuf[0] + offset;
2338 #if MPEG2_COLOR
2339 decoder->b_motion.ref[1][1] = backward_fbuf[1] + (offset >> 1);
2340 decoder->b_motion.ref[1][2] = backward_fbuf[2] + (offset >> 1);
2341 #endif
2342 stride <<= 1;
2343 height >>= 1;
2346 decoder->stride = stride;
2347 decoder->slice_stride = 16 * stride;
2348 #if MPEG2_COLOR
2349 decoder->uv_stride = stride >> 1;
2350 decoder->slice_uv_stride =
2351 decoder->slice_stride >> (2 - decoder->chroma_format);
2352 #endif
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;
2358 if (decoder->mpeg1)
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;
2382 else
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;
2391 else
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;
2409 else
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)
2426 int offset;
2427 const MBAtab * mba;
2429 #if MPEG2_COLOR
2430 decoder->dc_dct_pred[0] = decoder->dc_dct_pred[1] =
2431 decoder->dc_dct_pred[2] = 16384;
2432 #else
2433 decoder->dc_dct_pred[0] = 16384;
2434 #endif
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;
2448 offset = 0;
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;
2456 #if MPEG2_COLOR
2457 offset >>= (2 - decoder->chroma_format);
2458 decoder->dest[1] = decoder->picture_dest[1] + offset;
2459 decoder->dest[2] = decoder->picture_dest[2] + offset;
2460 #endif
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 */
2472 offset = 0;
2473 while (1)
2475 if (bit_buf >= 0x08000000)
2477 mba = MBA_5 + (UBITS (bit_buf, 6) - 2);
2478 break;
2480 else if (bit_buf >= 0x01800000)
2482 mba = MBA_11 + (UBITS (bit_buf, 12) - 24);
2483 break;
2485 else
2487 switch (UBITS (bit_buf, 12))
2489 case 8: /* macroblock_escape */
2490 offset += 33;
2491 DUMPBITS (bit_buf, bits, 11);
2492 NEEDBITS (bit_buf, bits, bit_ptr);
2493 continue;
2494 case 15: /* macroblock_stuffing (MPEG1 only) */
2495 bit_buf &= 0xfffff;
2496 DUMPBITS (bit_buf, bits, 11);
2497 NEEDBITS (bit_buf, bits, bit_ptr);
2498 continue;
2499 default: /* error */
2500 return 1;
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;
2515 #if MPEG2_COLOR
2516 decoder->dest[1] += decoder->slice_uv_stride;
2517 decoder->dest[2] += decoder->slice_uv_stride;
2518 #endif
2521 decoder->v_offset += 16;
2524 if (decoder->v_offset > decoder->limit_y)
2525 return 1;
2527 return 0;
2529 #undef bit_buf
2530 #undef bits
2531 #undef bit_ptr
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))
2544 return;
2546 while (1)
2548 int macroblock_modes;
2549 int mba_inc;
2550 const MBAtab * mba;
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;
2563 int offset;
2564 uint8_t * dest_y;
2566 if (decoder->concealment_motion_vectors)
2568 if (decoder->picture_structure == FRAME_PICTURE)
2569 motion_fr_conceal (decoder);
2570 else
2571 motion_fi_conceal (decoder);
2573 else
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;
2586 else
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);
2599 #if MPEG2_COLOR
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);
2618 DCT_stride >>= 1;
2619 DCT_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);
2626 else
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,
2638 DCT_stride);
2639 slice_intra_DCT (decoder, 2, dest_v + DCT_offset + 8,
2640 DCT_stride);
2642 #else
2643 skip_chroma_intra(decoder);
2644 #endif /* MPEG2_COLOR */
2646 else
2648 motion_parser_t * parser;
2650 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;
2664 else
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,
2682 DCT_stride);
2684 if (coded_block_pattern & 4)
2685 slice_non_intra_DCT (decoder, 0, dest_y + DCT_offset,
2686 DCT_stride);
2688 if (coded_block_pattern & 8)
2689 slice_non_intra_DCT (decoder, 0,
2690 dest_y + DCT_offset + 8,
2691 DCT_stride);
2692 #if MPEG2_COLOR
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))
2706 int offset;
2707 uint8_t * dest_y;
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,
2720 DCT_stride);
2722 if (coded_block_pattern & 4)
2723 slice_non_intra_DCT (decoder, 0, dest_y + DCT_offset,
2724 DCT_stride);
2726 if (coded_block_pattern & 8)
2727 slice_non_intra_DCT (decoder, 0,
2728 dest_y + DCT_offset + 8,
2729 DCT_stride);
2730 #if MPEG2_COLOR
2731 DCT_stride >>= 1;
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),
2737 DCT_stride);
2739 if (coded_block_pattern & 32)
2740 slice_non_intra_DCT (decoder, 2,
2741 decoder->dest[2] + (offset >> 1),
2742 DCT_stride);
2744 if (coded_block_pattern & (2 << 30))
2745 slice_non_intra_DCT (decoder, 1,
2746 decoder->dest[1] + DCT_offset,
2747 DCT_stride);
2749 if (coded_block_pattern & (1 << 30))
2750 slice_non_intra_DCT (decoder, 2,
2751 decoder->dest[2] + DCT_offset,
2752 DCT_stride);
2753 #endif /* MPEG2_COLOR */
2755 else
2757 int offset = decoder->offset;
2758 uint8_t * dest_y = decoder->dest[0] + offset;
2759 #if MPEG2_COLOR
2760 uint8_t * dest_u = decoder->dest[1] + offset;
2761 uint8_t * dest_v = decoder->dest[2] + offset;
2762 #endif
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,
2771 DCT_stride);
2773 if (coded_block_pattern & 4)
2774 slice_non_intra_DCT (decoder, 0, dest_y + DCT_offset,
2775 DCT_stride);
2777 if (coded_block_pattern & 8)
2778 slice_non_intra_DCT (decoder, 0,
2779 dest_y + DCT_offset + 8,
2780 DCT_stride);
2781 #if MPEG2_COLOR
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,
2790 DCT_stride);
2792 if (coded_block_pattern & (16 << 26))
2793 slice_non_intra_DCT (decoder, 2, dest_v + DCT_offset,
2794 DCT_stride);
2796 if (coded_block_pattern & (8 << 26))
2797 slice_non_intra_DCT (decoder, 1, dest_u + 8,
2798 DCT_stride);
2800 if (coded_block_pattern & (4 << 26))
2801 slice_non_intra_DCT (decoder, 2, dest_v + 8,
2802 DCT_stride);
2804 if (coded_block_pattern & (2 << 26))
2805 slice_non_intra_DCT (decoder, 1,
2806 dest_u + DCT_offset + 8,
2807 DCT_stride);
2809 if (coded_block_pattern & (1 << 26))
2810 slice_non_intra_DCT (decoder, 2,
2811 dest_v + DCT_offset + 8,
2812 DCT_stride);
2813 #endif /* MPEG2_COLOR */
2815 #if !MPEG2_COLOR
2816 skip_chroma_non_intra(decoder, coded_block_pattern);
2817 #endif
2820 #if MPEG2_COLOR
2821 decoder->dc_dct_pred[0] = decoder->dc_dct_pred[1] =
2822 decoder->dc_dct_pred[2] = 16384;
2823 #else
2824 decoder->dc_dct_pred[0] = 16384;
2825 #endif
2828 NEXT_MACROBLOCK;
2830 NEEDBITS (bit_buf, bits, bit_ptr);
2831 mba_inc = 0;
2833 while (1)
2835 if (bit_buf >= 0x10000000)
2837 mba = MBA_5 + (UBITS (bit_buf, 5) - 2);
2838 break;
2840 else if (bit_buf >= 0x03000000)
2842 mba = MBA_11 + (UBITS (bit_buf, 11) - 24);
2843 break;
2845 else
2847 switch (UBITS (bit_buf, 11))
2849 case 8: /* macroblock_escape */
2850 mba_inc += 33;
2851 /* pass through */
2852 case 15: /* macroblock_stuffing (MPEG1 only) */
2853 DUMPBITS (bit_buf, bits, 11);
2854 NEEDBITS (bit_buf, bits, bit_ptr);
2855 continue;
2856 default: /* end of slice, or error */
2857 return;
2862 DUMPBITS (bit_buf, bits, mba->len);
2863 mba_inc += mba->mba;
2865 if (mba_inc)
2867 #if MPEG2_COLOR
2868 decoder->dc_dct_pred[0] = decoder->dc_dct_pred[1] =
2869 decoder->dc_dct_pred[2] = 16384;
2870 #else
2871 decoder->dc_dct_pred[0] = 16384;
2872 #endif
2873 if (decoder->coding_type == P_TYPE)
2877 MOTION_CALL (decoder->motion_parser[0],
2878 MACROBLOCK_MOTION_FORWARD);
2879 NEXT_MACROBLOCK;
2881 while (--mba_inc);
2883 else
2887 MOTION_CALL (decoder->motion_parser[4], macroblock_modes);
2888 NEXT_MACROBLOCK;
2890 while (--mba_inc);
2895 #undef bit_buf
2896 #undef bits
2897 #undef bit_ptr