Updated our source code header to explicitly mention that we are GPL v2 or
[Rockbox.git] / apps / plugins / mpegplayer / slice.c
bloba98e85baa5a8444cff0771ff619eac89001c0edd
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
25 #include "plugin.h"
27 #include "mpeg2dec_config.h"
29 #include "mpeg2.h"
30 #include "attributes.h"
31 #include "mpeg2_internal.h"
33 #include "vlc.h"
35 static inline int get_macroblock_modes (mpeg2_decoder_t * const decoder)
37 #define bit_buf (decoder->bitstream_buf)
38 #define bits (decoder->bitstream_bits)
39 #define bit_ptr (decoder->bitstream_ptr)
41 int macroblock_modes;
42 const MBtab * tab;
44 switch (decoder->coding_type)
46 case I_TYPE:
47 tab = MB_I + UBITS (bit_buf, 1);
48 DUMPBITS (bit_buf, bits, tab->len);
49 macroblock_modes = tab->modes;
51 if (!(decoder->frame_pred_frame_dct) &&
52 decoder->picture_structure == FRAME_PICTURE)
54 macroblock_modes |= UBITS (bit_buf, 1) * DCT_TYPE_INTERLACED;
55 DUMPBITS (bit_buf, bits, 1);
58 return macroblock_modes;
60 case P_TYPE:
61 tab = MB_P + UBITS (bit_buf, 5);
62 DUMPBITS (bit_buf, bits, tab->len);
63 macroblock_modes = tab->modes;
65 if (decoder->picture_structure != FRAME_PICTURE)
67 if (macroblock_modes & MACROBLOCK_MOTION_FORWARD)
69 macroblock_modes |= UBITS (bit_buf, 2) << MOTION_TYPE_SHIFT;
70 DUMPBITS (bit_buf, bits, 2);
73 return macroblock_modes | MACROBLOCK_MOTION_FORWARD;
75 else if (decoder->frame_pred_frame_dct)
77 if (macroblock_modes & MACROBLOCK_MOTION_FORWARD)
78 macroblock_modes |= MC_FRAME << MOTION_TYPE_SHIFT;
80 return macroblock_modes | MACROBLOCK_MOTION_FORWARD;
82 else
84 if (macroblock_modes & MACROBLOCK_MOTION_FORWARD)
86 macroblock_modes |= UBITS (bit_buf, 2) << MOTION_TYPE_SHIFT;
87 DUMPBITS (bit_buf, bits, 2);
90 if (macroblock_modes & (MACROBLOCK_INTRA | MACROBLOCK_PATTERN))
92 macroblock_modes |= UBITS (bit_buf, 1) * DCT_TYPE_INTERLACED;
93 DUMPBITS (bit_buf, bits, 1);
96 return macroblock_modes | MACROBLOCK_MOTION_FORWARD;
99 case B_TYPE:
100 tab = MB_B + UBITS (bit_buf, 6);
101 DUMPBITS (bit_buf, bits, tab->len);
102 macroblock_modes = tab->modes;
104 if (decoder->picture_structure != FRAME_PICTURE)
106 if (! (macroblock_modes & MACROBLOCK_INTRA))
108 macroblock_modes |= UBITS (bit_buf, 2) << MOTION_TYPE_SHIFT;
109 DUMPBITS (bit_buf, bits, 2);
112 return macroblock_modes;
114 else if (decoder->frame_pred_frame_dct)
116 /* if (! (macroblock_modes & MACROBLOCK_INTRA)) */
117 macroblock_modes |= MC_FRAME << MOTION_TYPE_SHIFT;
118 return macroblock_modes;
120 else
122 if (macroblock_modes & MACROBLOCK_INTRA)
123 goto intra;
125 macroblock_modes |= UBITS (bit_buf, 2) << MOTION_TYPE_SHIFT;
126 DUMPBITS (bit_buf, bits, 2);
128 if (macroblock_modes & (MACROBLOCK_INTRA | MACROBLOCK_PATTERN))
130 intra:
131 macroblock_modes |= UBITS (bit_buf, 1) * DCT_TYPE_INTERLACED;
132 DUMPBITS (bit_buf, bits, 1);
134 return macroblock_modes;
137 case D_TYPE:
138 DUMPBITS (bit_buf, bits, 1);
139 return MACROBLOCK_INTRA;
141 default:
142 return 0;
144 #undef bit_buf
145 #undef bits
146 #undef bit_ptr
149 static inline void get_quantizer_scale (mpeg2_decoder_t * const decoder)
151 #define bit_buf (decoder->bitstream_buf)
152 #define bits (decoder->bitstream_bits)
153 #define bit_ptr (decoder->bitstream_ptr)
155 int quantizer_scale_code;
157 quantizer_scale_code = UBITS (bit_buf, 5);
158 DUMPBITS (bit_buf, bits, 5);
160 decoder->quantizer_matrix[0] =
161 decoder->quantizer_prescale[0][quantizer_scale_code];
163 decoder->quantizer_matrix[1] =
164 decoder->quantizer_prescale[1][quantizer_scale_code];
166 decoder->quantizer_matrix[2] =
167 decoder->chroma_quantizer[0][quantizer_scale_code];
169 decoder->quantizer_matrix[3] =
170 decoder->chroma_quantizer[1][quantizer_scale_code];
171 #undef bit_buf
172 #undef bits
173 #undef bit_ptr
176 static inline int get_motion_delta (mpeg2_decoder_t * const decoder,
177 const int f_code)
179 #define bit_buf (decoder->bitstream_buf)
180 #define bits (decoder->bitstream_bits)
181 #define bit_ptr (decoder->bitstream_ptr)
183 int delta;
184 int sign;
185 const MVtab * tab;
187 if (bit_buf & 0x80000000)
189 DUMPBITS (bit_buf, bits, 1);
190 return 0;
192 else if (bit_buf >= 0x0c000000)
194 tab = MV_4 + UBITS (bit_buf, 4);
195 delta = (tab->delta << f_code) + 1;
196 bits += tab->len + f_code + 1;
197 bit_buf <<= tab->len;
199 sign = SBITS (bit_buf, 1);
200 bit_buf <<= 1;
202 if (f_code)
203 delta += UBITS (bit_buf, f_code);
204 bit_buf <<= f_code;
206 return (delta ^ sign) - sign;
208 else
210 tab = MV_10 + UBITS (bit_buf, 10);
211 delta = (tab->delta << f_code) + 1;
212 bits += tab->len + 1;
213 bit_buf <<= tab->len;
215 sign = SBITS (bit_buf, 1);
216 bit_buf <<= 1;
218 if (f_code)
220 NEEDBITS (bit_buf, bits, bit_ptr);
221 delta += UBITS (bit_buf, f_code);
222 DUMPBITS (bit_buf, bits, f_code);
225 return (delta ^ sign) - sign;
228 #undef bit_buf
229 #undef bits
230 #undef bit_ptr
233 static inline int bound_motion_vector (const int vector, const int f_code)
235 return ((int32_t)vector << (27 - f_code)) >> (27 - f_code);
238 static inline int get_dmv (mpeg2_decoder_t * const decoder)
240 #define bit_buf (decoder->bitstream_buf)
241 #define bits (decoder->bitstream_bits)
242 #define bit_ptr (decoder->bitstream_ptr)
244 const DMVtab * tab;
246 tab = DMV_2 + UBITS (bit_buf, 2);
247 DUMPBITS (bit_buf, bits, tab->len);
248 return tab->dmv;
250 #undef bit_buf
251 #undef bits
252 #undef bit_ptr
255 static inline int get_coded_block_pattern (mpeg2_decoder_t * const decoder)
257 #define bit_buf (decoder->bitstream_buf)
258 #define bits (decoder->bitstream_bits)
259 #define bit_ptr (decoder->bitstream_ptr)
261 const CBPtab * tab;
263 NEEDBITS (bit_buf, bits, bit_ptr);
265 if (bit_buf >= 0x20000000)
267 tab = CBP_7 + (UBITS (bit_buf, 7) - 16);
268 DUMPBITS (bit_buf, bits, tab->len);
269 return tab->cbp;
271 else
273 tab = CBP_9 + UBITS (bit_buf, 9);
274 DUMPBITS (bit_buf, bits, tab->len);
275 return tab->cbp;
278 #undef bit_buf
279 #undef bits
280 #undef bit_ptr
283 static inline int get_luma_dc_dct_diff (mpeg2_decoder_t * const decoder)
285 #define bit_buf (decoder->bitstream_buf)
286 #define bits (decoder->bitstream_bits)
287 #define bit_ptr (decoder->bitstream_ptr)
289 const DCtab * tab;
290 int size;
291 int dc_diff;
293 if (bit_buf < 0xf8000000)
295 tab = DC_lum_5 + UBITS (bit_buf, 5);
296 size = tab->size;
298 if (size)
300 bits += tab->len + size;
301 bit_buf <<= tab->len;
302 dc_diff =
303 UBITS (bit_buf, size) - UBITS (SBITS (~bit_buf, 1), size);
304 bit_buf <<= size;
305 return dc_diff << decoder->intra_dc_precision;
307 else
309 DUMPBITS (bit_buf, bits, 3);
310 return 0;
313 else
315 tab = DC_long + (UBITS (bit_buf, 9) - 0x1e0);
316 size = tab->size;
317 DUMPBITS (bit_buf, bits, tab->len);
318 NEEDBITS (bit_buf, bits, bit_ptr);
319 dc_diff = UBITS (bit_buf, size) - UBITS (SBITS (~bit_buf, 1), size);
320 DUMPBITS (bit_buf, bits, size);
321 return dc_diff << decoder->intra_dc_precision;
324 #undef bit_buf
325 #undef bits
326 #undef bit_ptr
329 #if MPEG2_COLOR
330 static inline int get_chroma_dc_dct_diff (mpeg2_decoder_t * const decoder)
332 #define bit_buf (decoder->bitstream_buf)
333 #define bits (decoder->bitstream_bits)
334 #define bit_ptr (decoder->bitstream_ptr)
336 const DCtab * tab;
337 int size;
338 int dc_diff;
340 if (bit_buf < 0xf8000000)
342 tab = DC_chrom_5 + UBITS (bit_buf, 5);
343 size = tab->size;
345 if (size)
347 bits += tab->len + size;
348 bit_buf <<= tab->len;
349 dc_diff =
350 UBITS (bit_buf, size) - UBITS (SBITS (~bit_buf, 1), size);
351 bit_buf <<= size;
352 return dc_diff << decoder->intra_dc_precision;
354 else
356 DUMPBITS (bit_buf, bits, 2);
357 return 0;
360 else
362 tab = DC_long + (UBITS (bit_buf, 10) - 0x3e0);
363 size = tab->size;
364 DUMPBITS (bit_buf, bits, tab->len + 1);
365 NEEDBITS (bit_buf, bits, bit_ptr);
366 dc_diff = UBITS (bit_buf, size) - UBITS (SBITS (~bit_buf, 1), size);
367 DUMPBITS (bit_buf, bits, size);
368 return dc_diff << decoder->intra_dc_precision;
371 #undef bit_buf
372 #undef bits
373 #undef bit_ptr
375 #endif /* MPEG2_COLOR */
377 #define SATURATE(val) \
378 do { \
379 val <<= 4; \
380 if (unlikely (val != (int16_t) val)) \
381 val = (SBITS (val, 1) ^ 2047) << 4; \
382 } while (0)
384 static void get_intra_block_B14 (mpeg2_decoder_t * const decoder,
385 const uint16_t * const quant_matrix)
387 uint32_t bit_buf = decoder->bitstream_buf;
388 int bits = decoder->bitstream_bits;
389 const uint8_t * bit_ptr = decoder->bitstream_ptr;
390 const uint8_t * const scan = decoder->scan;
391 int16_t * const dest = decoder->DCTblock;
392 int mismatch = ~dest[0];
393 int i = 0;
394 int j;
395 int val;
396 const DCTtab * tab;
398 NEEDBITS (bit_buf, bits, bit_ptr);
400 while (1)
402 if (bit_buf >= 0x28000000)
404 tab = DCT_B14AC_5 + (UBITS (bit_buf, 5) - 5);
406 i += tab->run;
407 if (i >= 64)
408 break; /* end of block */
410 normal_code:
411 j = scan[i];
412 bit_buf <<= tab->len;
413 bits += tab->len + 1;
414 val = (tab->level * quant_matrix[j]) >> 4;
416 /* if (bitstream_get (1)) val = -val; */
417 val = (val ^ SBITS (bit_buf, 1)) - SBITS (bit_buf, 1);
419 SATURATE (val);
420 dest[j] = val;
421 mismatch ^= val;
423 bit_buf <<= 1;
424 NEEDBITS (bit_buf, bits, bit_ptr);
426 continue;
428 else if (bit_buf >= 0x04000000)
430 tab = DCT_B14_8 + (UBITS (bit_buf, 8) - 4);
432 i += tab->run;
433 if (i < 64)
434 goto normal_code;
436 /* escape code */
438 i += UBITS (bit_buf << 6, 6) - 64;
439 if (i >= 64)
440 break; /* illegal, check needed to avoid buffer overflow */
442 j = scan[i];
444 DUMPBITS (bit_buf, bits, 12);
445 NEEDBITS (bit_buf, bits, bit_ptr);
446 val = (SBITS (bit_buf, 12) * quant_matrix[j]) / 16;
448 SATURATE (val);
449 dest[j] = val;
450 mismatch ^= val;
452 DUMPBITS (bit_buf, bits, 12);
453 NEEDBITS (bit_buf, bits, bit_ptr);
455 continue;
457 else if (bit_buf >= 0x02000000)
459 tab = DCT_B14_10 + (UBITS (bit_buf, 10) - 8);
460 i += tab->run;
461 if (i < 64)
462 goto normal_code;
464 else if (bit_buf >= 0x00800000)
466 tab = DCT_13 + (UBITS (bit_buf, 13) - 16);
467 i += tab->run;
468 if (i < 64)
469 goto normal_code;
471 else if (bit_buf >= 0x00200000)
473 tab = DCT_15 + (UBITS (bit_buf, 15) - 16);
474 i += tab->run;
475 if (i < 64)
476 goto normal_code;
478 else
480 tab = DCT_16 + UBITS (bit_buf, 16);
481 bit_buf <<= 16;
482 GETWORD (bit_buf, bits + 16, bit_ptr);
483 i += tab->run;
484 if (i < 64)
485 goto normal_code;
487 break; /* illegal, check needed to avoid buffer overflow */
490 dest[63] ^= mismatch & 16;
491 DUMPBITS (bit_buf, bits, 2); /* dump end of block code */
492 decoder->bitstream_buf = bit_buf;
493 decoder->bitstream_bits = bits;
494 decoder->bitstream_ptr = bit_ptr;
497 static void get_intra_block_B15 (mpeg2_decoder_t * const decoder,
498 const uint16_t * const quant_matrix)
500 uint32_t bit_buf = decoder->bitstream_buf;
501 int bits = decoder->bitstream_bits;
502 const uint8_t * bit_ptr = decoder->bitstream_ptr;
503 const uint8_t * const scan = decoder->scan;
504 int16_t * const dest = decoder->DCTblock;
505 int mismatch = ~dest[0];
506 int i = 0;
507 int j;
508 int val;
509 const DCTtab * tab;
511 NEEDBITS (bit_buf, bits, bit_ptr);
513 while (1)
515 if (bit_buf >= 0x04000000)
517 tab = DCT_B15_8 + (UBITS (bit_buf, 8) - 4);
519 i += tab->run;
521 if (i < 64)
523 normal_code:
524 j = scan[i];
525 bit_buf <<= tab->len;
526 bits += tab->len + 1;
527 val = (tab->level * quant_matrix[j]) >> 4;
529 /* if (bitstream_get (1)) val = -val; */
530 val = (val ^ SBITS (bit_buf, 1)) - SBITS (bit_buf, 1);
532 SATURATE (val);
533 dest[j] = val;
534 mismatch ^= val;
536 bit_buf <<= 1;
537 NEEDBITS (bit_buf, bits, bit_ptr);
539 continue;
541 else
543 /* end of block. I commented out this code because if we */
544 /* dont exit here we will still exit at the later test :) */
546 /* if (i >= 128) break; */ /* end of block */
548 /* escape code */
550 i += UBITS (bit_buf << 6, 6) - 64;
551 if (i >= 64)
552 break; /* illegal, check against buffer overflow */
554 j = scan[i];
556 DUMPBITS (bit_buf, bits, 12);
557 NEEDBITS (bit_buf, bits, bit_ptr);
558 val = (SBITS (bit_buf, 12) * quant_matrix[j]) / 16;
560 SATURATE (val);
561 dest[j] = val;
562 mismatch ^= val;
564 DUMPBITS (bit_buf, bits, 12);
565 NEEDBITS (bit_buf, bits, bit_ptr);
567 continue;
570 else if (bit_buf >= 0x02000000)
572 tab = DCT_B15_10 + (UBITS (bit_buf, 10) - 8);
573 i += tab->run;
574 if (i < 64)
575 goto normal_code;
577 else if (bit_buf >= 0x00800000)
579 tab = DCT_13 + (UBITS (bit_buf, 13) - 16);
580 i += tab->run;
581 if (i < 64)
582 goto normal_code;
584 else if (bit_buf >= 0x00200000)
586 tab = DCT_15 + (UBITS (bit_buf, 15) - 16);
587 i += tab->run;
588 if (i < 64)
589 goto normal_code;
591 else
593 tab = DCT_16 + UBITS (bit_buf, 16);
594 bit_buf <<= 16;
595 GETWORD (bit_buf, bits + 16, bit_ptr);
596 i += tab->run;
597 if (i < 64)
598 goto normal_code;
600 break; /* illegal, check needed to avoid buffer overflow */
603 dest[63] ^= mismatch & 16;
604 DUMPBITS (bit_buf, bits, 4); /* dump end of block code */
605 decoder->bitstream_buf = bit_buf;
606 decoder->bitstream_bits = bits;
607 decoder->bitstream_ptr = bit_ptr;
610 static int get_non_intra_block (mpeg2_decoder_t * const decoder,
611 const uint16_t * const quant_matrix)
613 uint32_t bit_buf = decoder->bitstream_buf;
614 int bits = decoder->bitstream_bits;
615 const uint8_t * bit_ptr = decoder->bitstream_ptr;
616 const uint8_t * const scan = decoder->scan;
617 int16_t * const dest = decoder->DCTblock;
618 int mismatch = -1;
619 int i = -1;
620 int j;
621 int val;
622 const DCTtab * tab;
624 NEEDBITS (bit_buf, bits, bit_ptr);
626 if (bit_buf >= 0x28000000)
628 tab = DCT_B14DC_5 + (UBITS (bit_buf, 5) - 5);
629 goto entry_1;
631 else
633 goto entry_2;
636 while (1)
638 if (bit_buf >= 0x28000000)
640 tab = DCT_B14AC_5 + (UBITS (bit_buf, 5) - 5);
642 entry_1:
643 i += tab->run;
644 if (i >= 64)
645 break; /* end of block */
647 normal_code:
648 j = scan[i];
649 bit_buf <<= tab->len;
650 bits += tab->len + 1;
651 val = ((2 * tab->level + 1) * quant_matrix[j]) >> 5;
653 /* if (bitstream_get (1)) val = -val; */
654 val = (val ^ SBITS (bit_buf, 1)) - SBITS (bit_buf, 1);
656 SATURATE (val);
657 dest[j] = val;
658 mismatch ^= val;
660 bit_buf <<= 1;
661 NEEDBITS (bit_buf, bits, bit_ptr);
663 continue;
666 entry_2:
667 if (bit_buf >= 0x04000000)
669 tab = DCT_B14_8 + (UBITS (bit_buf, 8) - 4);
671 i += tab->run;
672 if (i < 64)
673 goto normal_code;
675 /* escape code */
677 i += UBITS (bit_buf << 6, 6) - 64;
678 if (i >= 64)
679 break; /* illegal, check needed to avoid buffer overflow */
681 j = scan[i];
683 DUMPBITS (bit_buf, bits, 12);
684 NEEDBITS (bit_buf, bits, bit_ptr);
685 val = 2 * (SBITS (bit_buf, 12) + SBITS (bit_buf, 1)) + 1;
686 val = (val * quant_matrix[j]) / 32;
688 SATURATE (val);
689 dest[j] = val;
690 mismatch ^= val;
692 DUMPBITS (bit_buf, bits, 12);
693 NEEDBITS (bit_buf, bits, bit_ptr);
695 continue;
697 else if (bit_buf >= 0x02000000)
699 tab = DCT_B14_10 + (UBITS (bit_buf, 10) - 8);
700 i += tab->run;
701 if (i < 64)
702 goto normal_code;
704 else if (bit_buf >= 0x00800000)
706 tab = DCT_13 + (UBITS (bit_buf, 13) - 16);
707 i += tab->run;
708 if (i < 64)
709 goto normal_code;
711 else if (bit_buf >= 0x00200000)
713 tab = DCT_15 + (UBITS (bit_buf, 15) - 16);
714 i += tab->run;
715 if (i < 64)
716 goto normal_code;
718 else
720 tab = DCT_16 + UBITS (bit_buf, 16);
721 bit_buf <<= 16;
722 GETWORD (bit_buf, bits + 16, bit_ptr);
723 i += tab->run;
724 if (i < 64)
725 goto normal_code;
727 break; /* illegal, check needed to avoid buffer overflow */
730 dest[63] ^= mismatch & 16;
731 DUMPBITS (bit_buf, bits, 2); /* dump end of block code */
732 decoder->bitstream_buf = bit_buf;
733 decoder->bitstream_bits = bits;
734 decoder->bitstream_ptr = bit_ptr;
735 return i;
738 static void get_mpeg1_intra_block (mpeg2_decoder_t * const decoder)
740 uint32_t bit_buf = decoder->bitstream_buf;
741 int bits = decoder->bitstream_bits;
742 const uint8_t * bit_ptr = decoder->bitstream_ptr;
743 const uint8_t * const scan = decoder->scan;
744 const uint16_t * const quant_matrix = decoder->quantizer_matrix[0];
745 int16_t * const dest = decoder->DCTblock;
746 int i = 0;
747 int j;
748 int val;
749 const DCTtab * tab;
751 NEEDBITS (bit_buf, bits, bit_ptr);
753 while (1)
755 if (bit_buf >= 0x28000000)
757 tab = DCT_B14AC_5 + (UBITS (bit_buf, 5) - 5);
759 i += tab->run;
760 if (i >= 64)
761 break; /* end of block */
763 normal_code:
764 j = scan[i];
765 bit_buf <<= tab->len;
766 bits += tab->len + 1;
767 val = (tab->level * quant_matrix[j]) >> 4;
769 /* oddification */
770 val = (val - 1) | 1;
772 /* if (bitstream_get (1)) val = -val; */
773 val = (val ^ SBITS (bit_buf, 1)) - SBITS (bit_buf, 1);
775 SATURATE (val);
776 dest[j] = val;
778 bit_buf <<= 1;
779 NEEDBITS (bit_buf, bits, bit_ptr);
781 continue;
783 else if (bit_buf >= 0x04000000)
785 tab = DCT_B14_8 + (UBITS (bit_buf, 8) - 4);
787 i += tab->run;
788 if (i < 64)
789 goto normal_code;
791 /* escape code */
793 i += UBITS (bit_buf << 6, 6) - 64;
794 if (i >= 64)
795 break; /* illegal, check needed to avoid buffer overflow */
797 j = scan[i];
799 DUMPBITS (bit_buf, bits, 12);
800 NEEDBITS (bit_buf, bits, bit_ptr);
801 val = SBITS (bit_buf, 8);
803 if (! (val & 0x7f))
805 DUMPBITS (bit_buf, bits, 8);
806 val = UBITS (bit_buf, 8) + 2 * val;
809 val = (val * quant_matrix[j]) / 16;
811 /* oddification */
812 val = (val + ~SBITS (val, 1)) | 1;
814 SATURATE (val);
815 dest[j] = val;
817 DUMPBITS (bit_buf, bits, 8);
818 NEEDBITS (bit_buf, bits, bit_ptr);
820 continue;
822 else if (bit_buf >= 0x02000000)
824 tab = DCT_B14_10 + (UBITS (bit_buf, 10) - 8);
825 i += tab->run;
826 if (i < 64)
827 goto normal_code;
829 else if (bit_buf >= 0x00800000)
831 tab = DCT_13 + (UBITS (bit_buf, 13) - 16);
832 i += tab->run;
833 if (i < 64)
834 goto normal_code;
836 else if (bit_buf >= 0x00200000)
838 tab = DCT_15 + (UBITS (bit_buf, 15) - 16);
839 i += tab->run;
840 if (i < 64)
841 goto normal_code;
843 else
845 tab = DCT_16 + UBITS (bit_buf, 16);
846 bit_buf <<= 16;
847 GETWORD (bit_buf, bits + 16, bit_ptr);
848 i += tab->run;
849 if (i < 64)
850 goto normal_code;
852 break; /* illegal, check needed to avoid buffer overflow */
855 DUMPBITS (bit_buf, bits, 2); /* dump end of block code */
856 decoder->bitstream_buf = bit_buf;
857 decoder->bitstream_bits = bits;
858 decoder->bitstream_ptr = bit_ptr;
861 static int get_mpeg1_non_intra_block (mpeg2_decoder_t * const decoder)
863 uint32_t bit_buf = decoder->bitstream_buf;
864 int bits = decoder->bitstream_bits;
865 const uint8_t * bit_ptr = decoder->bitstream_ptr;
866 const uint8_t * const scan = decoder->scan;
867 const uint16_t * const quant_matrix = decoder->quantizer_matrix[1];
868 int16_t * const dest = decoder->DCTblock;
869 int i = -1;
870 int j;
871 int val;
872 const DCTtab * tab;
874 NEEDBITS (bit_buf, bits, bit_ptr);
875 if (bit_buf >= 0x28000000)
877 tab = DCT_B14DC_5 + (UBITS (bit_buf, 5) - 5);
878 goto entry_1;
880 else
882 goto entry_2;
885 while (1)
887 if (bit_buf >= 0x28000000)
889 tab = DCT_B14AC_5 + (UBITS (bit_buf, 5) - 5);
891 entry_1:
892 i += tab->run;
893 if (i >= 64)
894 break; /* end of block */
896 normal_code:
897 j = scan[i];
898 bit_buf <<= tab->len;
899 bits += tab->len + 1;
900 val = ((2 * tab->level + 1) * quant_matrix[j]) >> 5;
902 /* oddification */
903 val = (val - 1) | 1;
905 /* if (bitstream_get (1)) val = -val; */
906 val = (val ^ SBITS (bit_buf, 1)) - SBITS (bit_buf, 1);
908 SATURATE (val);
909 dest[j] = val;
911 bit_buf <<= 1;
912 NEEDBITS (bit_buf, bits, bit_ptr);
914 continue;
917 entry_2:
918 if (bit_buf >= 0x04000000)
920 tab = DCT_B14_8 + (UBITS (bit_buf, 8) - 4);
922 i += tab->run;
923 if (i < 64)
924 goto normal_code;
926 /* escape code */
928 i += UBITS (bit_buf << 6, 6) - 64;
929 if (i >= 64)
930 break; /* illegal, check needed to avoid buffer overflow */
932 j = scan[i];
934 DUMPBITS (bit_buf, bits, 12);
935 NEEDBITS (bit_buf, bits, bit_ptr);
936 val = SBITS (bit_buf, 8);
938 if (! (val & 0x7f))
940 DUMPBITS (bit_buf, bits, 8);
941 val = UBITS (bit_buf, 8) + 2 * val;
944 val = 2 * (val + SBITS (val, 1)) + 1;
945 val = (val * quant_matrix[j]) / 32;
947 /* oddification */
948 val = (val + ~SBITS (val, 1)) | 1;
950 SATURATE (val);
951 dest[j] = val;
953 DUMPBITS (bit_buf, bits, 8);
954 NEEDBITS (bit_buf, bits, bit_ptr);
956 continue;
959 else if (bit_buf >= 0x02000000)
961 tab = DCT_B14_10 + (UBITS (bit_buf, 10) - 8);
962 i += tab->run;
963 if (i < 64)
964 goto normal_code;
966 else if (bit_buf >= 0x00800000)
968 tab = DCT_13 + (UBITS (bit_buf, 13) - 16);
969 i += tab->run;
970 if (i < 64)
971 goto normal_code;
973 else if (bit_buf >= 0x00200000)
975 tab = DCT_15 + (UBITS (bit_buf, 15) - 16);
976 i += tab->run;
977 if (i < 64)
978 goto normal_code;
980 else
982 tab = DCT_16 + UBITS (bit_buf, 16);
983 bit_buf <<= 16;
984 GETWORD (bit_buf, bits + 16, bit_ptr);
985 i += tab->run;
986 if (i < 64)
987 goto normal_code;
989 break; /* illegal, check needed to avoid buffer overflow */
992 DUMPBITS (bit_buf, bits, 2); /* dump end of block code */
993 decoder->bitstream_buf = bit_buf;
994 decoder->bitstream_bits = bits;
995 decoder->bitstream_ptr = bit_ptr;
996 return i;
999 static inline void slice_intra_DCT (mpeg2_decoder_t * const decoder,
1000 const int cc,
1001 uint8_t * const dest, const int stride)
1003 #define bit_buf (decoder->bitstream_buf)
1004 #define bits (decoder->bitstream_bits)
1005 #define bit_ptr (decoder->bitstream_ptr)
1007 NEEDBITS (bit_buf, bits, bit_ptr);
1008 /* Get the intra DC coefficient and inverse quantize it */
1009 if (cc == 0)
1011 decoder->dc_dct_pred[0] += get_luma_dc_dct_diff (decoder);
1012 decoder->DCTblock[0] = decoder->dc_dct_pred[0];
1015 #if MPEG2_COLOR
1016 else
1018 decoder->dc_dct_pred[cc] += get_chroma_dc_dct_diff (decoder);
1019 decoder->DCTblock[0] = decoder->dc_dct_pred[cc];
1021 #endif
1023 if (decoder->mpeg1)
1025 if (decoder->coding_type != D_TYPE)
1026 get_mpeg1_intra_block (decoder);
1028 else if (decoder->intra_vlc_format)
1030 get_intra_block_B15 (decoder, decoder->quantizer_matrix[cc ? 2 : 0]);
1032 else
1034 get_intra_block_B14 (decoder, decoder->quantizer_matrix[cc ? 2 : 0]);
1037 mpeg2_idct_copy (decoder->DCTblock, dest, stride);
1039 #undef bit_buf
1040 #undef bits
1041 #undef bit_ptr
1044 static inline void slice_non_intra_DCT (mpeg2_decoder_t * const decoder,
1045 const int cc,
1046 uint8_t * const dest, const int stride)
1048 int last;
1050 if (decoder->mpeg1)
1052 last = get_mpeg1_non_intra_block (decoder);
1054 else
1056 last = get_non_intra_block (decoder,
1057 decoder->quantizer_matrix[cc ? 3 : 1]);
1060 mpeg2_idct_add (last, decoder->DCTblock, dest, stride);
1063 #if !MPEG2_COLOR
1064 static void skip_mpeg1_intra_block (mpeg2_decoder_t * const decoder)
1066 uint32_t bit_buf = decoder->bitstream_buf;
1067 int bits = decoder->bitstream_bits;
1068 const uint8_t * bit_ptr = decoder->bitstream_ptr;
1069 int i = 0;
1070 const DCTtab * tab;
1072 NEEDBITS (bit_buf, bits, bit_ptr);
1074 while (1)
1076 if (bit_buf >= 0x28000000)
1078 tab = DCT_B14AC_5 + (UBITS (bit_buf, 5) - 5);
1080 i += tab->run;
1081 if (i >= 64)
1082 break; /* end of block */
1084 normal_code:
1085 bit_buf <<= tab->len + 1;
1086 bits += tab->len + 1;
1087 NEEDBITS (bit_buf, bits, bit_ptr);
1088 continue;
1090 else if (bit_buf >= 0x04000000)
1092 tab = DCT_B14_8 + (UBITS (bit_buf, 8) - 4);
1094 i += tab->run;
1095 if (i < 64)
1096 goto normal_code;
1098 /* escape code */
1100 i += UBITS (bit_buf << 6, 6) - 64;
1101 if (i >= 64)
1102 break; /* illegal, check needed to avoid buffer overflow */
1104 DUMPBITS (bit_buf, bits, 12);
1105 NEEDBITS (bit_buf, bits, bit_ptr);
1107 if (!(SBITS (bit_buf, 8) & 0x7f))
1108 DUMPBITS (bit_buf, bits, 8);
1110 DUMPBITS (bit_buf, bits, 8);
1111 NEEDBITS (bit_buf, bits, bit_ptr);
1113 continue;
1115 else if (bit_buf >= 0x02000000)
1117 tab = DCT_B14_10 + (UBITS (bit_buf, 10) - 8);
1118 i += tab->run;
1119 if (i < 64)
1120 goto normal_code;
1122 else if (bit_buf >= 0x00800000)
1124 tab = DCT_13 + (UBITS (bit_buf, 13) - 16);
1125 i += tab->run;
1126 if (i < 64)
1127 goto normal_code;
1129 else if (bit_buf >= 0x00200000)
1131 tab = DCT_15 + (UBITS (bit_buf, 15) - 16);
1132 i += tab->run;
1133 if (i < 64)
1134 goto normal_code;
1136 else
1138 tab = DCT_16 + UBITS (bit_buf, 16);
1139 bit_buf <<= 16;
1140 GETWORD (bit_buf, bits + 16, bit_ptr);
1141 i += tab->run;
1142 if (i < 64)
1143 goto normal_code;
1145 break; /* illegal, check needed to avoid buffer overflow */
1148 DUMPBITS (bit_buf, bits, 2); /* dump end of block code */
1149 decoder->bitstream_buf = bit_buf;
1150 decoder->bitstream_bits = bits;
1151 decoder->bitstream_ptr = bit_ptr;
1154 static void skip_intra_block_B14 (mpeg2_decoder_t * const decoder)
1156 uint32_t bit_buf = decoder->bitstream_buf;
1157 int bits = decoder->bitstream_bits;
1158 const uint8_t * bit_ptr = decoder->bitstream_ptr;
1159 int i = 0;
1160 const DCTtab * tab;
1162 NEEDBITS (bit_buf, bits, bit_ptr);
1164 while (1)
1166 if (bit_buf >= 0x28000000)
1168 tab = DCT_B14AC_5 + (UBITS (bit_buf, 5) - 5);
1170 i += tab->run;
1171 if (i >= 64)
1172 break; /* end of block */
1174 normal_code:
1175 bit_buf <<= tab->len + 1;
1176 bits += tab->len + 1;
1177 NEEDBITS (bit_buf, bits, bit_ptr);
1178 continue;
1180 else if (bit_buf >= 0x04000000)
1182 tab = DCT_B14_8 + (UBITS (bit_buf, 8) - 4);
1184 i += tab->run;
1185 if (i < 64)
1186 goto normal_code;
1188 /* escape code */
1190 i += UBITS (bit_buf << 6, 6) - 64;
1191 if (i >= 64)
1192 break; /* illegal, check needed to avoid buffer overflow */
1194 DUMPBITS (bit_buf, bits, 12); /* Can't dump more than 16 atm */
1195 NEEDBITS (bit_buf, bits, bit_ptr);
1196 DUMPBITS (bit_buf, bits, 12);
1197 NEEDBITS (bit_buf, bits, bit_ptr);
1198 continue;
1200 else if (bit_buf >= 0x02000000)
1202 tab = DCT_B14_10 + (UBITS (bit_buf, 10) - 8);
1203 i += tab->run;
1204 if (i < 64)
1205 goto normal_code;
1207 else if (bit_buf >= 0x00800000)
1209 tab = DCT_13 + (UBITS (bit_buf, 13) - 16);
1210 i += tab->run;
1211 if (i < 64)
1212 goto normal_code;
1214 else if (bit_buf >= 0x00200000)
1216 tab = DCT_15 + (UBITS (bit_buf, 15) - 16);
1217 i += tab->run;
1218 if (i < 64)
1219 goto normal_code;
1221 else
1223 tab = DCT_16 + UBITS (bit_buf, 16);
1224 bit_buf <<= 16;
1225 GETWORD (bit_buf, bits + 16, bit_ptr);
1226 i += tab->run;
1227 if (i < 64)
1228 goto normal_code;
1230 break; /* illegal, check needed to avoid buffer overflow */
1233 DUMPBITS (bit_buf, bits, 2); /* dump end of block code */
1234 decoder->bitstream_buf = bit_buf;
1235 decoder->bitstream_bits = bits;
1236 decoder->bitstream_ptr = bit_ptr;
1239 static void skip_intra_block_B15 (mpeg2_decoder_t * const decoder)
1241 uint32_t bit_buf = decoder->bitstream_buf;
1242 int bits = decoder->bitstream_bits;
1243 const uint8_t * bit_ptr = decoder->bitstream_ptr;
1244 int i = 0;
1245 const DCTtab * tab;
1247 NEEDBITS (bit_buf, bits, bit_ptr);
1249 while (1)
1251 if (bit_buf >= 0x04000000)
1253 tab = DCT_B15_8 + (UBITS (bit_buf, 8) - 4);
1255 i += tab->run;
1257 if (i < 64)
1259 normal_code:
1260 bit_buf <<= tab->len + 1;
1261 bits += tab->len + 1;
1262 NEEDBITS (bit_buf, bits, bit_ptr);
1263 continue;
1265 else
1267 /* end of block. I commented out this code because if we */
1268 /* dont exit here we will still exit at the later test :) */
1270 /* if (i >= 128) break; */ /* end of block */
1272 /* escape code */
1274 i += UBITS (bit_buf << 6, 6) - 64;
1275 if (i >= 64)
1276 break; /* illegal, check against buffer overflow */
1278 DUMPBITS (bit_buf, bits, 12); /* Can't dump more than 16 atm */
1279 NEEDBITS (bit_buf, bits, bit_ptr);
1280 DUMPBITS (bit_buf, bits, 12);
1281 NEEDBITS (bit_buf, bits, bit_ptr);
1282 continue;
1285 else if (bit_buf >= 0x02000000)
1287 tab = DCT_B15_10 + (UBITS (bit_buf, 10) - 8);
1288 i += tab->run;
1289 if (i < 64)
1290 goto normal_code;
1292 else if (bit_buf >= 0x00800000)
1294 tab = DCT_13 + (UBITS (bit_buf, 13) - 16);
1295 i += tab->run;
1296 if (i < 64)
1297 goto normal_code;
1299 else if (bit_buf >= 0x00200000)
1301 tab = DCT_15 + (UBITS (bit_buf, 15) - 16);
1302 i += tab->run;
1303 if (i < 64)
1304 goto normal_code;
1306 else
1308 tab = DCT_16 + UBITS (bit_buf, 16);
1309 bit_buf <<= 16;
1310 GETWORD (bit_buf, bits + 16, bit_ptr);
1311 i += tab->run;
1312 if (i < 64)
1313 goto normal_code;
1315 break; /* illegal, check needed to avoid buffer overflow */
1318 DUMPBITS (bit_buf, bits, 4); /* dump end of block code */
1319 decoder->bitstream_buf = bit_buf;
1320 decoder->bitstream_bits = bits;
1321 decoder->bitstream_ptr = bit_ptr;
1324 static void skip_non_intra_block (mpeg2_decoder_t * const decoder)
1326 uint32_t bit_buf = decoder->bitstream_buf;
1327 int bits = decoder->bitstream_bits;
1328 const uint8_t * bit_ptr = decoder->bitstream_ptr;
1329 int i = -1;
1330 const DCTtab * tab;
1332 NEEDBITS (bit_buf, bits, bit_ptr);
1334 if (bit_buf >= 0x28000000)
1336 tab = DCT_B14DC_5 + (UBITS (bit_buf, 5) - 5);
1337 goto entry_1;
1339 else
1341 goto entry_2;
1344 while (1)
1346 if (bit_buf >= 0x28000000)
1348 tab = DCT_B14AC_5 + (UBITS (bit_buf, 5) - 5);
1350 entry_1:
1351 i += tab->run;
1352 if (i >= 64)
1353 break; /* end of block */
1355 normal_code:
1356 bit_buf <<= tab->len + 1;
1357 bits += tab->len + 1;
1358 NEEDBITS (bit_buf, bits, bit_ptr);
1360 continue;
1363 entry_2:
1364 if (bit_buf >= 0x04000000)
1366 tab = DCT_B14_8 + (UBITS (bit_buf, 8) - 4);
1368 i += tab->run;
1369 if (i < 64)
1370 goto normal_code;
1372 /* escape code */
1374 i += UBITS (bit_buf << 6, 6) - 64;
1375 if (i >= 64)
1376 break; /* illegal, check needed to avoid buffer overflow */
1378 if (decoder->mpeg1)
1380 DUMPBITS (bit_buf, bits, 12);
1381 NEEDBITS (bit_buf, bits, bit_ptr);
1383 if (!(SBITS (bit_buf, 8) & 0x7f))
1384 DUMPBITS (bit_buf, bits, 8);
1386 DUMPBITS (bit_buf, bits, 8);
1388 else
1390 DUMPBITS (bit_buf, bits, 12);
1391 NEEDBITS (bit_buf, bits, bit_ptr);
1392 DUMPBITS (bit_buf, bits, 12);
1395 NEEDBITS (bit_buf, bits, bit_ptr);
1396 continue;
1398 else if (bit_buf >= 0x02000000)
1400 tab = DCT_B14_10 + (UBITS (bit_buf, 10) - 8);
1401 i += tab->run;
1402 if (i < 64)
1403 goto normal_code;
1405 else if (bit_buf >= 0x00800000)
1407 tab = DCT_13 + (UBITS (bit_buf, 13) - 16);
1408 i += tab->run;
1409 if (i < 64)
1410 goto normal_code;
1412 else if (bit_buf >= 0x00200000)
1414 tab = DCT_15 + (UBITS (bit_buf, 15) - 16);
1415 i += tab->run;
1416 if (i < 64)
1417 goto normal_code;
1419 else
1421 tab = DCT_16 + UBITS (bit_buf, 16);
1422 bit_buf <<= 16;
1423 GETWORD (bit_buf, bits + 16, bit_ptr);
1424 i += tab->run;
1425 if (i < 64)
1426 goto normal_code;
1428 break; /* illegal, check needed to avoid buffer overflow */
1431 DUMPBITS (bit_buf, bits, 2); /* dump end of block code */
1432 decoder->bitstream_buf = bit_buf;
1433 decoder->bitstream_bits = bits;
1434 decoder->bitstream_ptr = bit_ptr;
1437 static void skip_chroma_dc_dct_diff (mpeg2_decoder_t * const decoder)
1439 #define bit_buf (decoder->bitstream_buf)
1440 #define bits (decoder->bitstream_bits)
1441 #define bit_ptr (decoder->bitstream_ptr)
1443 const DCtab * tab;
1444 int size;
1446 if (bit_buf < 0xf8000000)
1448 tab = DC_chrom_5 + UBITS (bit_buf, 5);
1449 size = tab->size;
1451 if (size)
1453 bits += tab->len + size;
1454 bit_buf <<= tab->len;
1455 bit_buf <<= size;
1457 else
1459 DUMPBITS (bit_buf, bits, 2);
1462 else
1464 tab = DC_long + (UBITS (bit_buf, 10) - 0x3e0);
1465 size = tab->size;
1466 DUMPBITS (bit_buf, bits, tab->len + 1);
1467 NEEDBITS (bit_buf, bits, bit_ptr);
1468 DUMPBITS (bit_buf, bits, size);
1471 #undef bit_buf
1472 #undef bits
1473 #undef bit_ptr
1476 static void skip_chroma_non_intra (mpeg2_decoder_t * const decoder,
1477 uint32_t coded_block_pattern)
1479 static const uint32_t cbp_mask[3] =
1481 0x00000030,
1482 0xc0000030,
1483 0xfc000030,
1486 uint32_t cbp = coded_block_pattern &
1487 cbp_mask[MIN((unsigned)decoder->chroma_format, 2u)];
1489 while (cbp)
1491 skip_non_intra_block (decoder);
1492 cbp &= (cbp - 1);
1496 static void skip_chroma_intra (mpeg2_decoder_t * const decoder)
1498 #define bit_buf (decoder->bitstream_buf)
1499 #define bits (decoder->bitstream_bits)
1500 #define bit_ptr (decoder->bitstream_ptr)
1501 int i = 2 << decoder->chroma_format;
1503 if ((unsigned)i > 8)
1504 i = 8;
1506 while (i-- > 0)
1508 NEEDBITS (bit_buf, bits, bit_ptr);
1510 skip_chroma_dc_dct_diff (decoder);
1512 if (decoder->mpeg1)
1514 if (decoder->coding_type != D_TYPE)
1515 skip_mpeg1_intra_block (decoder);
1517 else if (decoder->intra_vlc_format)
1519 skip_intra_block_B15 (decoder);
1521 else
1523 skip_intra_block_B14 (decoder);
1527 if (decoder->chroma_format == 0 && decoder->coding_type == D_TYPE)
1529 NEEDBITS (bit_buf, bits, bit_ptr);
1530 DUMPBITS (bit_buf, bits, 1);
1533 #undef bit_buf
1534 #undef bits
1535 #undef bit_ptr
1537 #endif /* !MPEG2_COLOR */
1539 #define MOTION_420(table, ref, motion_x, motion_y, size, y) \
1540 pos_x = 2 * decoder->offset + motion_x; \
1541 pos_y = 2 * decoder->v_offset + motion_y + 2 * y; \
1543 if (unlikely (pos_x > decoder->limit_x)) \
1545 pos_x = ((int)pos_x < 0) ? 0 : decoder->limit_x; \
1546 motion_x = pos_x - 2 * decoder->offset; \
1549 if (unlikely (pos_y > decoder->limit_y_ ## size)) \
1551 pos_y = ((int)pos_y < 0) ? 0 : decoder->limit_y_ ## size; \
1552 motion_y = pos_y - 2 * decoder->v_offset - 2 * y; \
1555 xy_half = ((pos_y & 1) << 1) | (pos_x & 1); \
1556 table[xy_half] (decoder->dest[0] + y * decoder->stride + decoder->offset, \
1557 ref[0] + (pos_x >> 1) + (pos_y >> 1) * decoder->stride, \
1558 decoder->stride, size); \
1560 if (MPEG2_COLOR) \
1562 motion_x /= 2; \
1563 motion_y /= 2; \
1564 xy_half = ((motion_y & 1) << 1) | (motion_x & 1); \
1565 offset = ((decoder->offset + motion_x) >> 1) + \
1566 ((((decoder->v_offset + motion_y) >> 1) + y/2) * \
1567 decoder->uv_stride); \
1569 table[4+xy_half] (decoder->dest[1] + y/2 * decoder->uv_stride + \
1570 (decoder->offset >> 1), ref[1] + offset, \
1571 decoder->uv_stride, size/2); \
1572 table[4+xy_half] (decoder->dest[2] + y/2 * decoder->uv_stride + \
1573 (decoder->offset >> 1), ref[2] + offset, \
1574 decoder->uv_stride, size/2); \
1577 #define MOTION_FIELD_420(table, ref, motion_x, motion_y, \
1578 dest_field, op, src_field) \
1579 pos_x = 2 * decoder->offset + motion_x; \
1580 pos_y = decoder->v_offset + motion_y; \
1582 if (unlikely (pos_x > decoder->limit_x)) \
1584 pos_x = ((int)pos_x < 0) ? 0 : decoder->limit_x; \
1585 motion_x = pos_x - 2 * decoder->offset; \
1588 if (unlikely (pos_y > decoder->limit_y)) \
1590 pos_y = ((int)pos_y < 0) ? 0 : decoder->limit_y; \
1591 motion_y = pos_y - decoder->v_offset; \
1594 xy_half = ((pos_y & 1) << 1) | (pos_x & 1); \
1595 table[xy_half] (decoder->dest[0] + dest_field * decoder->stride + \
1596 decoder->offset, \
1597 (ref[0] + (pos_x >> 1) + \
1598 ((pos_y op) + src_field) * decoder->stride), \
1599 2 * decoder->stride, 8); \
1601 if (MPEG2_COLOR) \
1603 motion_x /= 2; \
1604 motion_y /= 2; \
1605 xy_half = ((motion_y & 1) << 1) | (motion_x & 1); \
1606 offset = ((decoder->offset + motion_x) >> 1) + \
1607 (((decoder->v_offset >> 1) + (motion_y op) + src_field) * \
1608 decoder->uv_stride); \
1610 table[4+xy_half] (decoder->dest[1] + dest_field * decoder->uv_stride + \
1611 (decoder->offset >> 1), ref[1] + offset, \
1612 2 * decoder->uv_stride, 4); \
1613 table[4+xy_half] (decoder->dest[2] + dest_field * decoder->uv_stride + \
1614 (decoder->offset >> 1), ref[2] + offset, \
1615 2 * decoder->uv_stride, 4); \
1618 #define MOTION_DMV_420(table, ref, motion_x, motion_y) \
1619 pos_x = 2 * decoder->offset + motion_x; \
1620 pos_y = decoder->v_offset + motion_y; \
1622 if (unlikely (pos_x > decoder->limit_x)) \
1624 pos_x = ((int)pos_x < 0) ? 0 : decoder->limit_x; \
1625 motion_x = pos_x - 2 * decoder->offset; \
1628 if (unlikely (pos_y > decoder->limit_y)) \
1630 pos_y = ((int)pos_y < 0) ? 0 : decoder->limit_y; \
1631 motion_y = pos_y - decoder->v_offset; \
1634 xy_half = ((pos_y & 1) << 1) | (pos_x & 1); \
1635 offset = (pos_x >> 1) + (pos_y & ~1) * decoder->stride; \
1636 table[xy_half] (decoder->dest[0] + decoder->offset, \
1637 ref[0] + offset, 2 * decoder->stride, 8); \
1638 table[xy_half] (decoder->dest[0] + decoder->stride + decoder->offset, \
1639 ref[0] + decoder->stride + offset, \
1640 2 * decoder->stride, 8); \
1642 if (MPEG2_COLOR) \
1644 motion_x /= 2; \
1645 motion_y /= 2; \
1646 xy_half = ((motion_y & 1) << 1) | (motion_x & 1); \
1647 offset = ((decoder->offset + motion_x) >> 1) + \
1648 (((decoder->v_offset >> 1) + (motion_y & ~1)) * \
1649 decoder->uv_stride); \
1651 table[4+xy_half] (decoder->dest[1] + (decoder->offset >> 1), \
1652 ref[1] + offset, 2 * decoder->uv_stride, 4); \
1653 table[4+xy_half] (decoder->dest[1] + decoder->uv_stride + \
1654 (decoder->offset >> 1), \
1655 ref[1] + decoder->uv_stride + offset, \
1656 2 * decoder->uv_stride, 4); \
1657 table[4+xy_half] (decoder->dest[2] + (decoder->offset >> 1), \
1658 ref[2] + offset, 2 * decoder->uv_stride, 4); \
1659 table[4+xy_half] (decoder->dest[2] + decoder->uv_stride + \
1660 (decoder->offset >> 1), \
1661 ref[2] + decoder->uv_stride + offset, \
1662 2 * decoder->uv_stride, 4); \
1665 #define MOTION_ZERO_420(table, ref) \
1666 table[0] (decoder->dest[0] + decoder->offset, \
1667 (ref[0] + decoder->offset + \
1668 decoder->v_offset * decoder->stride), decoder->stride, 16); \
1670 if (MPEG2_COLOR) \
1672 offset = ((decoder->offset >> 1) + \
1673 (decoder->v_offset >> 1) * decoder->uv_stride); \
1675 table[4] (decoder->dest[1] + (decoder->offset >> 1), \
1676 ref[1] + offset, decoder->uv_stride, 8); \
1677 table[4] (decoder->dest[2] + (decoder->offset >> 1), \
1678 ref[2] + offset, decoder->uv_stride, 8); \
1681 #define MOTION_422(table, ref, motion_x, motion_y, size, y) \
1682 pos_x = 2 * decoder->offset + motion_x; \
1683 pos_y = 2 * decoder->v_offset + motion_y + 2 * y; \
1685 if (unlikely (pos_x > decoder->limit_x)) \
1687 pos_x = ((int)pos_x < 0) ? 0 : decoder->limit_x; \
1688 motion_x = pos_x - 2 * decoder->offset; \
1691 if (unlikely (pos_y > decoder->limit_y_ ## size)) \
1693 pos_y = ((int)pos_y < 0) ? 0 : decoder->limit_y_ ## size; \
1694 motion_y = pos_y - 2 * decoder->v_offset - 2 * y; \
1697 xy_half = ((pos_y & 1) << 1) | (pos_x & 1); \
1698 offset = (pos_x >> 1) + (pos_y >> 1) * decoder->stride; \
1700 table[xy_half] (decoder->dest[0] + y * decoder->stride + decoder->offset, \
1701 ref[0] + offset, decoder->stride, size); \
1703 if (MPEG2_COLOR) \
1705 offset = (offset + (motion_x & (motion_x < 0))) >> 1; \
1706 motion_x /= 2; \
1707 xy_half = ((pos_y & 1) << 1) | (motion_x & 1); \
1709 table[4+xy_half] (decoder->dest[1] + y * decoder->uv_stride + \
1710 (decoder->offset >> 1), ref[1] + offset, \
1711 decoder->uv_stride, size); \
1712 table[4+xy_half] (decoder->dest[2] + y * decoder->uv_stride + \
1713 (decoder->offset >> 1), ref[2] + offset, \
1714 decoder->uv_stride, size); \
1717 #define MOTION_FIELD_422(table, ref, motion_x, motion_y, \
1718 dest_field, op, src_field) \
1719 pos_x = 2 * decoder->offset + motion_x; \
1720 pos_y = decoder->v_offset + motion_y; \
1722 if (unlikely (pos_x > decoder->limit_x)) \
1724 pos_x = ((int)pos_x < 0) ? 0 : decoder->limit_x; \
1725 motion_x = pos_x - 2 * decoder->offset; \
1728 if (unlikely (pos_y > decoder->limit_y)) \
1730 pos_y = ((int)pos_y < 0) ? 0 : decoder->limit_y; \
1731 motion_y = pos_y - decoder->v_offset; \
1734 xy_half = ((pos_y & 1) << 1) | (pos_x & 1); \
1735 offset = (pos_x >> 1) + ((pos_y op) + src_field) * decoder->stride; \
1737 table[xy_half] (decoder->dest[0] + dest_field * decoder->stride + \
1738 decoder->offset, ref[0] + offset, \
1739 2 * decoder->stride, 8); \
1741 if (MPEG2_COLOR) \
1743 offset = (offset + (motion_x & (motion_x < 0))) >> 1; \
1744 motion_x /= 2; \
1745 xy_half = ((pos_y & 1) << 1) | (motion_x & 1); \
1747 table[4+xy_half] (decoder->dest[1] + dest_field * decoder->uv_stride + \
1748 (decoder->offset >> 1), ref[1] + offset, \
1749 2 * decoder->uv_stride, 8); \
1750 table[4+xy_half] (decoder->dest[2] + dest_field * decoder->uv_stride + \
1751 (decoder->offset >> 1), ref[2] + offset, \
1752 2 * decoder->uv_stride, 8); \
1755 #define MOTION_DMV_422(table, ref, motion_x, motion_y) \
1756 pos_x = 2 * decoder->offset + motion_x; \
1757 pos_y = decoder->v_offset + motion_y; \
1759 if (unlikely (pos_x > decoder->limit_x)) \
1761 pos_x = ((int)pos_x < 0) ? 0 : decoder->limit_x; \
1762 motion_x = pos_x - 2 * decoder->offset; \
1765 if (unlikely (pos_y > decoder->limit_y)) \
1767 pos_y = ((int)pos_y < 0) ? 0 : decoder->limit_y; \
1768 motion_y = pos_y - decoder->v_offset; \
1771 xy_half = ((pos_y & 1) << 1) | (pos_x & 1); \
1772 offset = (pos_x >> 1) + (pos_y & ~1) * decoder->stride; \
1774 table[xy_half] (decoder->dest[0] + decoder->offset, \
1775 ref[0] + offset, 2 * decoder->stride, 8); \
1776 table[xy_half] (decoder->dest[0] + decoder->stride + decoder->offset, \
1777 ref[0] + decoder->stride + offset, \
1778 2 * decoder->stride, 8); \
1780 if (MPEG2_COLOR) \
1782 offset = (offset + (motion_x & (motion_x < 0))) >> 1; \
1783 motion_x /= 2; \
1784 xy_half = ((pos_y & 1) << 1) | (motion_x & 1); \
1786 table[4+xy_half] (decoder->dest[1] + (decoder->offset >> 1), \
1787 ref[1] + offset, 2 * decoder->uv_stride, 8); \
1788 table[4+xy_half] (decoder->dest[1] + decoder->uv_stride + \
1789 (decoder->offset >> 1), \
1790 ref[1] + decoder->uv_stride + offset, \
1791 2 * decoder->uv_stride, 8); \
1792 table[4+xy_half] (decoder->dest[2] + (decoder->offset >> 1), \
1793 ref[2] + offset, 2 * decoder->uv_stride, 8); \
1794 table[4+xy_half] (decoder->dest[2] + decoder->uv_stride + \
1795 (decoder->offset >> 1), \
1796 ref[2] + decoder->uv_stride + offset, \
1797 2 * decoder->uv_stride, 8); \
1800 #define MOTION_ZERO_422(table, ref) \
1801 offset = decoder->offset + decoder->v_offset * decoder->stride; \
1802 table[0] (decoder->dest[0] + decoder->offset, \
1803 ref[0] + offset, decoder->stride, 16); \
1805 if (MPEG2_COLOR) \
1807 offset >>= 1; \
1808 table[4] (decoder->dest[1] + (decoder->offset >> 1), \
1809 ref[1] + offset, decoder->uv_stride, 16); \
1810 table[4] (decoder->dest[2] + (decoder->offset >> 1), \
1811 ref[2] + offset, decoder->uv_stride, 16); \
1814 #define MOTION_444(table, ref, motion_x, motion_y, size, y) \
1815 pos_x = 2 * decoder->offset + motion_x; \
1816 pos_y = 2 * decoder->v_offset + motion_y + 2 * y; \
1818 if (unlikely (pos_x > decoder->limit_x)) \
1820 pos_x = ((int)pos_x < 0) ? 0 : decoder->limit_x; \
1821 motion_x = pos_x - 2 * decoder->offset; \
1824 if (unlikely (pos_y > decoder->limit_y_ ## size)) \
1826 pos_y = ((int)pos_y < 0) ? 0 : decoder->limit_y_ ## size; \
1827 motion_y = pos_y - 2 * decoder->v_offset - 2 * y; \
1830 xy_half = ((pos_y & 1) << 1) | (pos_x & 1); \
1831 offset = (pos_x >> 1) + (pos_y >> 1) * decoder->stride; \
1833 table[xy_half] (decoder->dest[0] + y * decoder->stride + decoder->offset, \
1834 ref[0] + offset, decoder->stride, size); \
1836 if (MPEG2_COLOR) \
1838 table[xy_half] (decoder->dest[1] + y * decoder->stride + decoder->offset, \
1839 ref[1] + offset, decoder->stride, size); \
1840 table[xy_half] (decoder->dest[2] + y * decoder->stride + decoder->offset, \
1841 ref[2] + offset, decoder->stride, size); \
1844 #define MOTION_FIELD_444(table, ref, motion_x, motion_y, \
1845 dest_field, op, src_field) \
1846 pos_x = 2 * decoder->offset + motion_x; \
1847 pos_y = decoder->v_offset + motion_y; \
1849 if (unlikely (pos_x > decoder->limit_x)) \
1851 pos_x = ((int)pos_x < 0) ? 0 : decoder->limit_x; \
1852 motion_x = pos_x - 2 * decoder->offset; \
1855 if (unlikely (pos_y > decoder->limit_y)) \
1857 pos_y = ((int)pos_y < 0) ? 0 : decoder->limit_y; \
1858 motion_y = pos_y - decoder->v_offset; \
1861 xy_half = ((pos_y & 1) << 1) | (pos_x & 1); \
1862 offset = (pos_x >> 1) + ((pos_y op) + src_field) * decoder->stride; \
1864 table[xy_half] (decoder->dest[0] + dest_field * decoder->stride + \
1865 decoder->offset, ref[0] + offset, \
1866 2 * decoder->stride, 8); \
1868 if (MPEG2_COLOR) \
1870 table[xy_half] (decoder->dest[1] + dest_field * decoder->stride + \
1871 decoder->offset, ref[1] + offset, \
1872 2 * decoder->stride, 8); \
1873 table[xy_half] (decoder->dest[2] + dest_field * decoder->stride + \
1874 decoder->offset, ref[2] + offset, \
1875 2 * decoder->stride, 8); \
1878 #define MOTION_DMV_444(table, ref, motion_x, motion_y) \
1879 pos_x = 2 * decoder->offset + motion_x; \
1880 pos_y = decoder->v_offset + motion_y; \
1882 if (unlikely (pos_x > decoder->limit_x)) \
1884 pos_x = ((int)pos_x < 0) ? 0 : decoder->limit_x; \
1885 motion_x = pos_x - 2 * decoder->offset; \
1888 if (unlikely (pos_y > decoder->limit_y)) \
1890 pos_y = ((int)pos_y < 0) ? 0 : decoder->limit_y; \
1891 motion_y = pos_y - decoder->v_offset; \
1894 xy_half = ((pos_y & 1) << 1) | (pos_x & 1); \
1895 offset = (pos_x >> 1) + (pos_y & ~1) * decoder->stride; \
1897 table[xy_half] (decoder->dest[0] + decoder->offset, \
1898 ref[0] + offset, 2 * decoder->stride, 8); \
1899 table[xy_half] (decoder->dest[0] + decoder->stride + decoder->offset, \
1900 ref[0] + decoder->stride + offset, \
1901 2 * decoder->stride, 8); \
1903 if (MPEG2_COLOR) \
1905 table[xy_half] (decoder->dest[1] + decoder->offset, \
1906 ref[1] + offset, 2 * decoder->stride, 8); \
1907 table[xy_half] (decoder->dest[1] + decoder->stride + decoder->offset, \
1908 ref[1] + decoder->stride + offset, \
1909 2 * decoder->stride, 8); \
1910 table[xy_half] (decoder->dest[2] + decoder->offset, \
1911 ref[2] + offset, 2 * decoder->stride, 8); \
1912 table[xy_half] (decoder->dest[2] + decoder->stride + decoder->offset, \
1913 ref[2] + decoder->stride + offset, \
1914 2 * decoder->stride, 8); \
1917 #define MOTION_ZERO_444(table, ref) \
1918 offset = decoder->offset + decoder->v_offset * decoder->stride; \
1920 table[0] (decoder->dest[0] + decoder->offset, \
1921 ref[0] + offset, decoder->stride, 16); \
1923 if (MPEG2_COLOR) \
1925 table[4] (decoder->dest[1] + decoder->offset, \
1926 ref[1] + offset, decoder->stride, 16); \
1927 table[4] (decoder->dest[2] + (decoder->offset >> 1), \
1928 ref[2] + offset, decoder->stride, 16); \
1931 #define bit_buf (decoder->bitstream_buf)
1932 #define bits (decoder->bitstream_bits)
1933 #define bit_ptr (decoder->bitstream_ptr)
1935 static void motion_mp1 (mpeg2_decoder_t * const decoder,
1936 motion_t * const motion,
1937 mpeg2_mc_fct * const * const table)
1939 int motion_x, motion_y;
1940 unsigned int pos_x, pos_y, xy_half, offset;
1942 NEEDBITS (bit_buf, bits, bit_ptr);
1943 motion_x = motion->pmv[0][0] +
1944 (get_motion_delta (decoder,
1945 motion->f_code[0]) << motion->f_code[1]);
1946 motion_x = bound_motion_vector (motion_x,
1947 motion->f_code[0] + motion->f_code[1]);
1948 motion->pmv[0][0] = motion_x;
1950 NEEDBITS (bit_buf, bits, bit_ptr);
1951 motion_y = motion->pmv[0][1] +
1952 (get_motion_delta (decoder,
1953 motion->f_code[0]) << motion->f_code[1]);
1954 motion_y = bound_motion_vector (motion_y,
1955 motion->f_code[0] + motion->f_code[1]);
1956 motion->pmv[0][1] = motion_y;
1958 MOTION_420 (table, motion->ref[0], motion_x, motion_y, 16, 0);
1961 #define MOTION_FUNCTIONS(FORMAT, MOTION, MOTION_FIELD, \
1962 MOTION_DMV, MOTION_ZERO) \
1964 static void motion_fr_frame_##FORMAT (mpeg2_decoder_t * const decoder, \
1965 motion_t * const motion, \
1966 mpeg2_mc_fct * const * const table) \
1968 int motion_x, motion_y; \
1969 unsigned int pos_x, pos_y, xy_half, offset; \
1971 NEEDBITS (bit_buf, bits, bit_ptr); \
1972 motion_x = motion->pmv[0][0] + get_motion_delta (decoder, \
1973 motion->f_code[0]); \
1974 motion_x = bound_motion_vector (motion_x, motion->f_code[0]); \
1975 motion->pmv[1][0] = motion->pmv[0][0] = motion_x; \
1977 NEEDBITS (bit_buf, bits, bit_ptr); \
1978 motion_y = motion->pmv[0][1] + get_motion_delta (decoder, \
1979 motion->f_code[1]); \
1980 motion_y = bound_motion_vector (motion_y, motion->f_code[1]); \
1981 motion->pmv[1][1] = motion->pmv[0][1] = motion_y; \
1983 MOTION (table, motion->ref[0], motion_x, motion_y, 16, 0); \
1986 static void motion_fr_field_##FORMAT (mpeg2_decoder_t * const decoder, \
1987 motion_t * const motion, \
1988 mpeg2_mc_fct * const * const table) \
1990 int motion_x, motion_y, field; \
1991 unsigned int pos_x, pos_y, xy_half, offset; \
1993 NEEDBITS (bit_buf, bits, bit_ptr); \
1994 field = UBITS (bit_buf, 1); \
1995 DUMPBITS (bit_buf, bits, 1); \
1997 motion_x = motion->pmv[0][0] + get_motion_delta (decoder, \
1998 motion->f_code[0]); \
1999 motion_x = bound_motion_vector (motion_x, motion->f_code[0]); \
2000 motion->pmv[0][0] = motion_x; \
2002 NEEDBITS (bit_buf, bits, bit_ptr); \
2003 motion_y = ((motion->pmv[0][1] >> 1) + \
2004 get_motion_delta (decoder, motion->f_code[1])); \
2005 /* motion_y = bound_motion_vector (motion_y, motion->f_code[1]); */ \
2006 motion->pmv[0][1] = motion_y << 1; \
2008 MOTION_FIELD (table, motion->ref[0], motion_x, motion_y, 0, & ~1, field); \
2010 NEEDBITS (bit_buf, bits, bit_ptr); \
2011 field = UBITS (bit_buf, 1); \
2012 DUMPBITS (bit_buf, bits, 1); \
2014 motion_x = motion->pmv[1][0] + get_motion_delta (decoder, \
2015 motion->f_code[0]); \
2016 motion_x = bound_motion_vector (motion_x, motion->f_code[0]); \
2017 motion->pmv[1][0] = motion_x; \
2019 NEEDBITS (bit_buf, bits, bit_ptr); \
2020 motion_y = ((motion->pmv[1][1] >> 1) + \
2021 get_motion_delta (decoder, motion->f_code[1])); \
2022 /* motion_y = bound_motion_vector (motion_y, motion->f_code[1]); */ \
2023 motion->pmv[1][1] = motion_y << 1; \
2025 MOTION_FIELD (table, motion->ref[0], motion_x, motion_y, 1, & ~1, field); \
2028 static void motion_fr_dmv_##FORMAT (mpeg2_decoder_t * const decoder, \
2029 motion_t * const motion, \
2030 mpeg2_mc_fct * const * const table) \
2032 int motion_x, motion_y, dmv_x, dmv_y, m, other_x, other_y; \
2033 unsigned int pos_x, pos_y, xy_half, offset; \
2035 (void)table; \
2036 NEEDBITS (bit_buf, bits, bit_ptr); \
2037 motion_x = motion->pmv[0][0] + get_motion_delta (decoder, \
2038 motion->f_code[0]); \
2039 motion_x = bound_motion_vector (motion_x, motion->f_code[0]); \
2040 motion->pmv[1][0] = motion->pmv[0][0] = motion_x; \
2041 NEEDBITS (bit_buf, bits, bit_ptr); \
2042 dmv_x = get_dmv (decoder); \
2044 motion_y = ((motion->pmv[0][1] >> 1) + \
2045 get_motion_delta (decoder, motion->f_code[1])); \
2046 /* motion_y = bound_motion_vector (motion_y, motion->f_code[1]); */ \
2047 motion->pmv[1][1] = motion->pmv[0][1] = motion_y << 1; \
2048 dmv_y = get_dmv (decoder); \
2050 m = decoder->top_field_first ? 1 : 3; \
2051 other_x = ((motion_x * m + (motion_x > 0)) >> 1) + dmv_x; \
2052 other_y = ((motion_y * m + (motion_y > 0)) >> 1) + dmv_y - 1; \
2053 MOTION_FIELD (mpeg2_mc.put, motion->ref[0], other_x, other_y, 0, | 1, 0); \
2055 m = decoder->top_field_first ? 3 : 1; \
2056 other_x = ((motion_x * m + (motion_x > 0)) >> 1) + dmv_x; \
2057 other_y = ((motion_y * m + (motion_y > 0)) >> 1) + dmv_y + 1; \
2058 MOTION_FIELD (mpeg2_mc.put, motion->ref[0], other_x, other_y, 1, & ~1, 0);\
2060 MOTION_DMV (mpeg2_mc.avg, motion->ref[0], motion_x, motion_y); \
2063 static void motion_reuse_##FORMAT (mpeg2_decoder_t * const decoder, \
2064 motion_t * const motion, \
2065 mpeg2_mc_fct * const * const table) \
2067 int motion_x, motion_y; \
2068 unsigned int pos_x, pos_y, xy_half, offset; \
2070 motion_x = motion->pmv[0][0]; \
2071 motion_y = motion->pmv[0][1]; \
2073 MOTION (table, motion->ref[0], motion_x, motion_y, 16, 0); \
2076 static void motion_zero_##FORMAT (mpeg2_decoder_t * const decoder, \
2077 motion_t * const motion, \
2078 mpeg2_mc_fct * const * const table) \
2080 unsigned int offset; \
2082 motion->pmv[0][0] = motion->pmv[0][1] = 0; \
2083 motion->pmv[1][0] = motion->pmv[1][1] = 0; \
2085 MOTION_ZERO (table, motion->ref[0]); \
2088 static void motion_fi_field_##FORMAT (mpeg2_decoder_t * const decoder, \
2089 motion_t * const motion, \
2090 mpeg2_mc_fct * const * const table) \
2092 int motion_x, motion_y; \
2093 uint8_t ** ref_field; \
2094 unsigned int pos_x, pos_y, xy_half, offset; \
2096 NEEDBITS (bit_buf, bits, bit_ptr); \
2097 ref_field = motion->ref2[UBITS (bit_buf, 1)]; \
2098 DUMPBITS (bit_buf, bits, 1); \
2100 motion_x = motion->pmv[0][0] + get_motion_delta (decoder, \
2101 motion->f_code[0]); \
2102 motion_x = bound_motion_vector (motion_x, motion->f_code[0]); \
2103 motion->pmv[1][0] = motion->pmv[0][0] = motion_x; \
2105 NEEDBITS (bit_buf, bits, bit_ptr); \
2106 motion_y = motion->pmv[0][1] + get_motion_delta (decoder, \
2107 motion->f_code[1]); \
2108 motion_y = bound_motion_vector (motion_y, motion->f_code[1]); \
2109 motion->pmv[1][1] = motion->pmv[0][1] = motion_y; \
2111 MOTION (table, ref_field, motion_x, motion_y, 16, 0); \
2114 static void motion_fi_16x8_##FORMAT (mpeg2_decoder_t * const decoder, \
2115 motion_t * const motion, \
2116 mpeg2_mc_fct * const * const table) \
2118 int motion_x, motion_y; \
2119 uint8_t ** ref_field; \
2120 unsigned int pos_x, pos_y, xy_half, offset; \
2122 NEEDBITS (bit_buf, bits, bit_ptr); \
2123 ref_field = motion->ref2[UBITS (bit_buf, 1)]; \
2124 DUMPBITS (bit_buf, bits, 1); \
2126 motion_x = motion->pmv[0][0] + get_motion_delta (decoder, \
2127 motion->f_code[0]); \
2128 motion_x = bound_motion_vector (motion_x, motion->f_code[0]); \
2129 motion->pmv[0][0] = motion_x; \
2131 NEEDBITS (bit_buf, bits, bit_ptr); \
2132 motion_y = motion->pmv[0][1] + get_motion_delta (decoder, \
2133 motion->f_code[1]); \
2134 motion_y = bound_motion_vector (motion_y, motion->f_code[1]); \
2135 motion->pmv[0][1] = motion_y; \
2137 MOTION (table, ref_field, motion_x, motion_y, 8, 0); \
2139 NEEDBITS (bit_buf, bits, bit_ptr); \
2140 ref_field = motion->ref2[UBITS (bit_buf, 1)]; \
2141 DUMPBITS (bit_buf, bits, 1); \
2143 motion_x = motion->pmv[1][0] + get_motion_delta (decoder, \
2144 motion->f_code[0]); \
2145 motion_x = bound_motion_vector (motion_x, motion->f_code[0]); \
2146 motion->pmv[1][0] = motion_x; \
2148 NEEDBITS (bit_buf, bits, bit_ptr); \
2149 motion_y = motion->pmv[1][1] + get_motion_delta (decoder, \
2150 motion->f_code[1]); \
2151 motion_y = bound_motion_vector (motion_y, motion->f_code[1]); \
2152 motion->pmv[1][1] = motion_y; \
2154 MOTION (table, ref_field, motion_x, motion_y, 8, 8); \
2157 static void motion_fi_dmv_##FORMAT (mpeg2_decoder_t * const decoder, \
2158 motion_t * const motion, \
2159 mpeg2_mc_fct * const * const table) \
2161 int motion_x, motion_y, other_x, other_y; \
2162 unsigned int pos_x, pos_y, xy_half, offset; \
2164 (void)table; \
2165 NEEDBITS (bit_buf, bits, bit_ptr); \
2166 motion_x = motion->pmv[0][0] + get_motion_delta (decoder, \
2167 motion->f_code[0]); \
2168 motion_x = bound_motion_vector (motion_x, motion->f_code[0]); \
2169 motion->pmv[1][0] = motion->pmv[0][0] = motion_x; \
2170 NEEDBITS (bit_buf, bits, bit_ptr); \
2171 other_x = ((motion_x + (motion_x > 0)) >> 1) + get_dmv (decoder); \
2173 motion_y = motion->pmv[0][1] + get_motion_delta (decoder, \
2174 motion->f_code[1]); \
2175 motion_y = bound_motion_vector (motion_y, motion->f_code[1]); \
2176 motion->pmv[1][1] = motion->pmv[0][1] = motion_y; \
2177 other_y = (((motion_y + (motion_y > 0)) >> 1) + get_dmv (decoder) + \
2178 decoder->dmv_offset); \
2180 MOTION (mpeg2_mc.put, motion->ref[0], motion_x, motion_y, 16, 0); \
2181 MOTION (mpeg2_mc.avg, motion->ref[1], other_x, other_y, 16, 0); \
2184 MOTION_FUNCTIONS (420, MOTION_420, MOTION_FIELD_420, MOTION_DMV_420,
2185 MOTION_ZERO_420)
2186 MOTION_FUNCTIONS (422, MOTION_422, MOTION_FIELD_422, MOTION_DMV_422,
2187 MOTION_ZERO_422)
2188 MOTION_FUNCTIONS (444, MOTION_444, MOTION_FIELD_444, MOTION_DMV_444,
2189 MOTION_ZERO_444)
2191 /* like motion_frame, but parsing without actual motion compensation */
2192 static void motion_fr_conceal (mpeg2_decoder_t * const decoder)
2194 int tmp;
2196 NEEDBITS (bit_buf, bits, bit_ptr);
2197 tmp = (decoder->f_motion.pmv[0][0] +
2198 get_motion_delta (decoder, decoder->f_motion.f_code[0]));
2199 tmp = bound_motion_vector (tmp, decoder->f_motion.f_code[0]);
2200 decoder->f_motion.pmv[1][0] = decoder->f_motion.pmv[0][0] = tmp;
2202 NEEDBITS (bit_buf, bits, bit_ptr);
2203 tmp = (decoder->f_motion.pmv[0][1] +
2204 get_motion_delta (decoder, decoder->f_motion.f_code[1]));
2205 tmp = bound_motion_vector (tmp, decoder->f_motion.f_code[1]);
2206 decoder->f_motion.pmv[1][1] = decoder->f_motion.pmv[0][1] = tmp;
2208 DUMPBITS (bit_buf, bits, 1); /* remove marker_bit */
2211 static void motion_fi_conceal (mpeg2_decoder_t * const decoder)
2213 int tmp;
2215 NEEDBITS (bit_buf, bits, bit_ptr);
2216 DUMPBITS (bit_buf, bits, 1); /* remove field_select */
2218 tmp = decoder->f_motion.pmv[0][0] +
2219 get_motion_delta (decoder, decoder->f_motion.f_code[0]);
2220 tmp = bound_motion_vector (tmp, decoder->f_motion.f_code[0]);
2222 decoder->f_motion.pmv[1][0] =
2223 decoder->f_motion.pmv[0][0] = tmp;
2225 NEEDBITS (bit_buf, bits, bit_ptr);
2227 tmp = (decoder->f_motion.pmv[0][1] +
2228 get_motion_delta (decoder, decoder->f_motion.f_code[1]));
2229 tmp = bound_motion_vector (tmp, decoder->f_motion.f_code[1]);
2231 decoder->f_motion.pmv[1][1] =
2232 decoder->f_motion.pmv[0][1] = tmp;
2234 DUMPBITS (bit_buf, bits, 1); /* remove marker_bit */
2237 #undef bit_buf
2238 #undef bits
2239 #undef bit_ptr
2241 #define MOTION_CALL(routine, direction) \
2242 do { \
2243 if ((direction) & MACROBLOCK_MOTION_FORWARD) \
2244 routine (decoder, &decoder->f_motion, mpeg2_mc.put); \
2246 if ((direction) & MACROBLOCK_MOTION_BACKWARD) \
2248 routine (decoder, &decoder->b_motion, \
2249 ((direction) & MACROBLOCK_MOTION_FORWARD ? \
2250 mpeg2_mc.avg : mpeg2_mc.put)); \
2252 } while (0)
2254 #define NEXT_MACROBLOCK \
2255 do { \
2256 decoder->offset += 16; \
2258 if (decoder->offset == decoder->width) \
2260 do { /* just so we can use the break statement */ \
2261 if (decoder->convert) \
2263 decoder->convert (decoder->convert_id, decoder->dest, \
2264 decoder->v_offset); \
2265 if (decoder->coding_type == B_TYPE) \
2266 break; \
2269 decoder->dest[0] += decoder->slice_stride; \
2270 if (MPEG2_COLOR) \
2272 decoder->dest[1] += decoder->slice_uv_stride; \
2273 decoder->dest[2] += decoder->slice_uv_stride; \
2275 } while (0); \
2277 decoder->v_offset += 16; \
2279 if (decoder->v_offset > decoder->limit_y) \
2280 return; \
2282 decoder->offset = 0; \
2284 } while (0)
2286 void mpeg2_init_fbuf (mpeg2_decoder_t * decoder,
2287 uint8_t * current_fbuf[MPEG2_COMPONENTS],
2288 uint8_t * forward_fbuf[MPEG2_COMPONENTS],
2289 uint8_t * backward_fbuf[MPEG2_COMPONENTS])
2291 int offset, stride, height, bottom_field;
2293 stride = decoder->stride_frame;
2294 bottom_field = (decoder->picture_structure == BOTTOM_FIELD);
2295 offset = bottom_field ? stride : 0;
2296 height = decoder->height;
2298 decoder->picture_dest[0] = current_fbuf[0] + offset;
2299 #if MPEG2_COLOR
2300 decoder->picture_dest[1] = current_fbuf[1] + (offset >> 1);
2301 decoder->picture_dest[2] = current_fbuf[2] + (offset >> 1);
2302 #endif
2304 decoder->f_motion.ref[0][0] = forward_fbuf[0] + offset;
2305 #if MPEG2_COLOR
2306 decoder->f_motion.ref[0][1] = forward_fbuf[1] + (offset >> 1);
2307 decoder->f_motion.ref[0][2] = forward_fbuf[2] + (offset >> 1);
2308 #endif
2310 decoder->b_motion.ref[0][0] = backward_fbuf[0] + offset;
2311 #if MPEG2_COLOR
2312 decoder->b_motion.ref[0][1] = backward_fbuf[1] + (offset >> 1);
2313 decoder->b_motion.ref[0][2] = backward_fbuf[2] + (offset >> 1);
2314 #endif
2316 if (decoder->picture_structure != FRAME_PICTURE)
2318 decoder->dmv_offset = bottom_field ? 1 : -1;
2319 decoder->f_motion.ref2[0] = decoder->f_motion.ref[bottom_field];
2320 decoder->f_motion.ref2[1] = decoder->f_motion.ref[!bottom_field];
2321 decoder->b_motion.ref2[0] = decoder->b_motion.ref[bottom_field];
2322 decoder->b_motion.ref2[1] = decoder->b_motion.ref[!bottom_field];
2323 offset = stride - offset;
2325 if (decoder->second_field && (decoder->coding_type != B_TYPE))
2326 forward_fbuf = current_fbuf;
2328 decoder->f_motion.ref[1][0] = forward_fbuf[0] + offset;
2329 #if MPEG2_COLOR
2330 decoder->f_motion.ref[1][1] = forward_fbuf[1] + (offset >> 1);
2331 decoder->f_motion.ref[1][2] = forward_fbuf[2] + (offset >> 1);
2332 #endif
2333 decoder->b_motion.ref[1][0] = backward_fbuf[0] + offset;
2334 #if MPEG2_COLOR
2335 decoder->b_motion.ref[1][1] = backward_fbuf[1] + (offset >> 1);
2336 decoder->b_motion.ref[1][2] = backward_fbuf[2] + (offset >> 1);
2337 #endif
2338 stride <<= 1;
2339 height >>= 1;
2342 decoder->stride = stride;
2343 decoder->slice_stride = 16 * stride;
2344 #if MPEG2_COLOR
2345 decoder->uv_stride = stride >> 1;
2346 decoder->slice_uv_stride =
2347 decoder->slice_stride >> (2 - decoder->chroma_format);
2348 #endif
2349 decoder->limit_x = 2 * decoder->width - 32;
2350 decoder->limit_y_16 = 2 * height - 32;
2351 decoder->limit_y_8 = 2 * height - 16;
2352 decoder->limit_y = height - 16;
2354 if (decoder->mpeg1)
2356 decoder->motion_parser[0] = motion_zero_420;
2357 decoder->motion_parser[MC_FRAME] = motion_mp1;
2358 decoder->motion_parser[4] = motion_reuse_420;
2360 else if (decoder->picture_structure == FRAME_PICTURE)
2362 if (decoder->chroma_format == 0)
2364 decoder->motion_parser[0] = motion_zero_420;
2365 decoder->motion_parser[MC_FIELD] = motion_fr_field_420;
2366 decoder->motion_parser[MC_FRAME] = motion_fr_frame_420;
2367 decoder->motion_parser[MC_DMV] = motion_fr_dmv_420;
2368 decoder->motion_parser[4] = motion_reuse_420;
2370 else if (decoder->chroma_format == 1)
2372 decoder->motion_parser[0] = motion_zero_422;
2373 decoder->motion_parser[MC_FIELD] = motion_fr_field_422;
2374 decoder->motion_parser[MC_FRAME] = motion_fr_frame_422;
2375 decoder->motion_parser[MC_DMV] = motion_fr_dmv_422;
2376 decoder->motion_parser[4] = motion_reuse_422;
2378 else
2380 decoder->motion_parser[0] = motion_zero_444;
2381 decoder->motion_parser[MC_FIELD] = motion_fr_field_444;
2382 decoder->motion_parser[MC_FRAME] = motion_fr_frame_444;
2383 decoder->motion_parser[MC_DMV] = motion_fr_dmv_444;
2384 decoder->motion_parser[4] = motion_reuse_444;
2387 else
2389 if (decoder->chroma_format == 0)
2391 decoder->motion_parser[0] = motion_zero_420;
2392 decoder->motion_parser[MC_FIELD] = motion_fi_field_420;
2393 decoder->motion_parser[MC_16X8] = motion_fi_16x8_420;
2394 decoder->motion_parser[MC_DMV] = motion_fi_dmv_420;
2395 decoder->motion_parser[4] = motion_reuse_420;
2397 else if (decoder->chroma_format == 1)
2399 decoder->motion_parser[0] = motion_zero_422;
2400 decoder->motion_parser[MC_FIELD] = motion_fi_field_422;
2401 decoder->motion_parser[MC_16X8] = motion_fi_16x8_422;
2402 decoder->motion_parser[MC_DMV] = motion_fi_dmv_422;
2403 decoder->motion_parser[4] = motion_reuse_422;
2405 else
2407 decoder->motion_parser[0] = motion_zero_444;
2408 decoder->motion_parser[MC_FIELD] = motion_fi_field_444;
2409 decoder->motion_parser[MC_16X8] = motion_fi_16x8_444;
2410 decoder->motion_parser[MC_DMV] = motion_fi_dmv_444;
2411 decoder->motion_parser[4] = motion_reuse_444;
2416 static inline int slice_init (mpeg2_decoder_t * const decoder, int code)
2418 #define bit_buf (decoder->bitstream_buf)
2419 #define bits (decoder->bitstream_bits)
2420 #define bit_ptr (decoder->bitstream_ptr)
2422 int offset;
2423 const MBAtab * mba;
2425 #if MPEG2_COLOR
2426 decoder->dc_dct_pred[0] = decoder->dc_dct_pred[1] =
2427 decoder->dc_dct_pred[2] = 16384;
2428 #else
2429 decoder->dc_dct_pred[0] = 16384;
2430 #endif
2432 decoder->f_motion.pmv[0][0] = decoder->f_motion.pmv[0][1] = 0;
2433 decoder->f_motion.pmv[1][0] = decoder->f_motion.pmv[1][1] = 0;
2434 decoder->b_motion.pmv[0][0] = decoder->b_motion.pmv[0][1] = 0;
2435 decoder->b_motion.pmv[1][0] = decoder->b_motion.pmv[1][1] = 0;
2437 if (decoder->vertical_position_extension)
2439 code += UBITS (bit_buf, 3) << 7;
2440 DUMPBITS (bit_buf, bits, 3);
2443 decoder->v_offset = (code - 1) * 16;
2444 offset = 0;
2446 if (!(decoder->convert) || decoder->coding_type != B_TYPE)
2448 offset = (code - 1) * decoder->slice_stride;
2451 decoder->dest[0] = decoder->picture_dest[0] + offset;
2452 #if MPEG2_COLOR
2453 offset >>= (2 - decoder->chroma_format);
2454 decoder->dest[1] = decoder->picture_dest[1] + offset;
2455 decoder->dest[2] = decoder->picture_dest[2] + offset;
2456 #endif
2458 get_quantizer_scale (decoder);
2460 /* ignore intra_slice and all the extra data */
2461 while (bit_buf & 0x80000000)
2463 DUMPBITS (bit_buf, bits, 9);
2464 NEEDBITS (bit_buf, bits, bit_ptr);
2467 /* decode initial macroblock address increment */
2468 offset = 0;
2469 while (1)
2471 if (bit_buf >= 0x08000000)
2473 mba = MBA_5 + (UBITS (bit_buf, 6) - 2);
2474 break;
2476 else if (bit_buf >= 0x01800000)
2478 mba = MBA_11 + (UBITS (bit_buf, 12) - 24);
2479 break;
2481 else
2483 switch (UBITS (bit_buf, 12))
2485 case 8: /* macroblock_escape */
2486 offset += 33;
2487 DUMPBITS (bit_buf, bits, 11);
2488 NEEDBITS (bit_buf, bits, bit_ptr);
2489 continue;
2490 case 15: /* macroblock_stuffing (MPEG1 only) */
2491 bit_buf &= 0xfffff;
2492 DUMPBITS (bit_buf, bits, 11);
2493 NEEDBITS (bit_buf, bits, bit_ptr);
2494 continue;
2495 default: /* error */
2496 return 1;
2501 DUMPBITS (bit_buf, bits, mba->len + 1);
2502 decoder->offset = (offset + mba->mba) << 4;
2504 while (decoder->offset - decoder->width >= 0)
2506 decoder->offset -= decoder->width;
2508 if (!(decoder->convert) || decoder->coding_type != B_TYPE)
2510 decoder->dest[0] += decoder->slice_stride;
2511 #if MPEG2_COLOR
2512 decoder->dest[1] += decoder->slice_uv_stride;
2513 decoder->dest[2] += decoder->slice_uv_stride;
2514 #endif
2517 decoder->v_offset += 16;
2520 if (decoder->v_offset > decoder->limit_y)
2521 return 1;
2523 return 0;
2525 #undef bit_buf
2526 #undef bits
2527 #undef bit_ptr
2530 void mpeg2_slice (mpeg2_decoder_t * const decoder, const int code,
2531 const uint8_t * const buffer)
2533 #define bit_buf (decoder->bitstream_buf)
2534 #define bits (decoder->bitstream_bits)
2535 #define bit_ptr (decoder->bitstream_ptr)
2537 bitstream_init (decoder, buffer);
2539 if (slice_init (decoder, code))
2540 return;
2542 while (1)
2544 int macroblock_modes;
2545 int mba_inc;
2546 const MBAtab * mba;
2548 NEEDBITS (bit_buf, bits, bit_ptr);
2550 macroblock_modes = get_macroblock_modes (decoder);
2552 /* maybe integrate MACROBLOCK_QUANT test into get_macroblock_modes ? */
2553 if (macroblock_modes & MACROBLOCK_QUANT)
2554 get_quantizer_scale (decoder);
2556 if (macroblock_modes & MACROBLOCK_INTRA)
2558 int DCT_offset, DCT_stride;
2559 int offset;
2560 uint8_t * dest_y;
2562 if (decoder->concealment_motion_vectors)
2564 if (decoder->picture_structure == FRAME_PICTURE)
2565 motion_fr_conceal (decoder);
2566 else
2567 motion_fi_conceal (decoder);
2569 else
2571 decoder->f_motion.pmv[0][0] = decoder->f_motion.pmv[0][1] = 0;
2572 decoder->f_motion.pmv[1][0] = decoder->f_motion.pmv[1][1] = 0;
2573 decoder->b_motion.pmv[0][0] = decoder->b_motion.pmv[0][1] = 0;
2574 decoder->b_motion.pmv[1][0] = decoder->b_motion.pmv[1][1] = 0;
2577 if (macroblock_modes & DCT_TYPE_INTERLACED)
2579 DCT_offset = decoder->stride;
2580 DCT_stride = decoder->stride * 2;
2582 else
2584 DCT_offset = decoder->stride * 8;
2585 DCT_stride = decoder->stride;
2588 offset = decoder->offset;
2589 dest_y = decoder->dest[0] + offset;
2590 slice_intra_DCT (decoder, 0, dest_y, DCT_stride);
2591 slice_intra_DCT (decoder, 0, dest_y + 8, DCT_stride);
2592 slice_intra_DCT (decoder, 0, dest_y + DCT_offset, DCT_stride);
2593 slice_intra_DCT (decoder, 0, dest_y + DCT_offset + 8, DCT_stride);
2595 #if MPEG2_COLOR
2596 if (likely (decoder->chroma_format == 0))
2598 slice_intra_DCT (decoder, 1, decoder->dest[1] + (offset >> 1),
2599 decoder->uv_stride);
2600 slice_intra_DCT (decoder, 2, decoder->dest[2] + (offset >> 1),
2601 decoder->uv_stride);
2603 if (decoder->coding_type == D_TYPE)
2605 NEEDBITS (bit_buf, bits, bit_ptr);
2606 DUMPBITS (bit_buf, bits, 1);
2609 else if (likely (decoder->chroma_format == 1))
2611 uint8_t * dest_u = decoder->dest[1] + (offset >> 1);
2612 uint8_t * dest_v = decoder->dest[2] + (offset >> 1);
2614 DCT_stride >>= 1;
2615 DCT_offset >>= 1;
2617 slice_intra_DCT (decoder, 1, dest_u, DCT_stride);
2618 slice_intra_DCT (decoder, 2, dest_v, DCT_stride);
2619 slice_intra_DCT (decoder, 1, dest_u + DCT_offset, DCT_stride);
2620 slice_intra_DCT (decoder, 2, dest_v + DCT_offset, DCT_stride);
2622 else
2624 uint8_t * dest_u = decoder->dest[1] + offset;
2625 uint8_t * dest_v = decoder->dest[2] + offset;
2627 slice_intra_DCT (decoder, 1, dest_u, DCT_stride);
2628 slice_intra_DCT (decoder, 2, dest_v, DCT_stride);
2629 slice_intra_DCT (decoder, 1, dest_u + DCT_offset, DCT_stride);
2630 slice_intra_DCT (decoder, 2, dest_v + DCT_offset, DCT_stride);
2631 slice_intra_DCT (decoder, 1, dest_u + 8, DCT_stride);
2632 slice_intra_DCT (decoder, 2, dest_v + 8, DCT_stride);
2633 slice_intra_DCT (decoder, 1, dest_u + DCT_offset + 8,
2634 DCT_stride);
2635 slice_intra_DCT (decoder, 2, dest_v + DCT_offset + 8,
2636 DCT_stride);
2638 #else
2639 skip_chroma_intra(decoder);
2640 #endif /* MPEG2_COLOR */
2642 else
2644 motion_parser_t * parser;
2646 parser =
2647 decoder->motion_parser[macroblock_modes >> MOTION_TYPE_SHIFT];
2648 MOTION_CALL (parser, macroblock_modes);
2650 if (macroblock_modes & MACROBLOCK_PATTERN)
2652 int coded_block_pattern;
2653 int DCT_offset, DCT_stride;
2655 if (macroblock_modes & DCT_TYPE_INTERLACED)
2657 DCT_offset = decoder->stride;
2658 DCT_stride = decoder->stride * 2;
2660 else
2662 DCT_offset = decoder->stride * 8;
2663 DCT_stride = decoder->stride;
2666 coded_block_pattern = get_coded_block_pattern (decoder);
2668 if (likely (decoder->chroma_format == 0))
2670 int offset = decoder->offset;
2671 uint8_t * dest_y = decoder->dest[0] + offset;
2673 if (coded_block_pattern & 1)
2674 slice_non_intra_DCT (decoder, 0, dest_y, DCT_stride);
2676 if (coded_block_pattern & 2)
2677 slice_non_intra_DCT (decoder, 0, dest_y + 8,
2678 DCT_stride);
2680 if (coded_block_pattern & 4)
2681 slice_non_intra_DCT (decoder, 0, dest_y + DCT_offset,
2682 DCT_stride);
2684 if (coded_block_pattern & 8)
2685 slice_non_intra_DCT (decoder, 0,
2686 dest_y + DCT_offset + 8,
2687 DCT_stride);
2688 #if MPEG2_COLOR
2689 if (coded_block_pattern & 16)
2690 slice_non_intra_DCT (decoder, 1,
2691 decoder->dest[1] + (offset >> 1),
2692 decoder->uv_stride);
2694 if (coded_block_pattern & 32)
2695 slice_non_intra_DCT (decoder, 2,
2696 decoder->dest[2] + (offset >> 1),
2697 decoder->uv_stride);
2698 #endif /* MPEG2_COLOR */
2700 else if (likely (decoder->chroma_format == 1))
2702 int offset;
2703 uint8_t * dest_y;
2705 coded_block_pattern |= bit_buf & (3 << 30);
2706 DUMPBITS (bit_buf, bits, 2);
2708 offset = decoder->offset;
2709 dest_y = decoder->dest[0] + offset;
2711 if (coded_block_pattern & 1)
2712 slice_non_intra_DCT (decoder, 0, dest_y, DCT_stride);
2714 if (coded_block_pattern & 2)
2715 slice_non_intra_DCT (decoder, 0, dest_y + 8,
2716 DCT_stride);
2718 if (coded_block_pattern & 4)
2719 slice_non_intra_DCT (decoder, 0, dest_y + DCT_offset,
2720 DCT_stride);
2722 if (coded_block_pattern & 8)
2723 slice_non_intra_DCT (decoder, 0,
2724 dest_y + DCT_offset + 8,
2725 DCT_stride);
2726 #if MPEG2_COLOR
2727 DCT_stride >>= 1;
2728 DCT_offset = (DCT_offset + offset) >> 1;
2730 if (coded_block_pattern & 16)
2731 slice_non_intra_DCT (decoder, 1,
2732 decoder->dest[1] + (offset >> 1),
2733 DCT_stride);
2735 if (coded_block_pattern & 32)
2736 slice_non_intra_DCT (decoder, 2,
2737 decoder->dest[2] + (offset >> 1),
2738 DCT_stride);
2740 if (coded_block_pattern & (2 << 30))
2741 slice_non_intra_DCT (decoder, 1,
2742 decoder->dest[1] + DCT_offset,
2743 DCT_stride);
2745 if (coded_block_pattern & (1 << 30))
2746 slice_non_intra_DCT (decoder, 2,
2747 decoder->dest[2] + DCT_offset,
2748 DCT_stride);
2749 #endif /* MPEG2_COLOR */
2751 else
2753 int offset = decoder->offset;
2754 uint8_t * dest_y = decoder->dest[0] + offset;
2755 #if MPEG2_COLOR
2756 uint8_t * dest_u = decoder->dest[1] + offset;
2757 uint8_t * dest_v = decoder->dest[2] + offset;
2758 #endif
2759 coded_block_pattern |= bit_buf & (63 << 26);
2760 DUMPBITS (bit_buf, bits, 6);
2762 if (coded_block_pattern & 1)
2763 slice_non_intra_DCT (decoder, 0, dest_y, DCT_stride);
2765 if (coded_block_pattern & 2)
2766 slice_non_intra_DCT (decoder, 0, dest_y + 8,
2767 DCT_stride);
2769 if (coded_block_pattern & 4)
2770 slice_non_intra_DCT (decoder, 0, dest_y + DCT_offset,
2771 DCT_stride);
2773 if (coded_block_pattern & 8)
2774 slice_non_intra_DCT (decoder, 0,
2775 dest_y + DCT_offset + 8,
2776 DCT_stride);
2777 #if MPEG2_COLOR
2778 if (coded_block_pattern & 16)
2779 slice_non_intra_DCT (decoder, 1, dest_u, DCT_stride);
2781 if (coded_block_pattern & 32)
2782 slice_non_intra_DCT (decoder, 2, dest_v, DCT_stride);
2784 if (coded_block_pattern & (32 << 26))
2785 slice_non_intra_DCT (decoder, 1, dest_u + DCT_offset,
2786 DCT_stride);
2788 if (coded_block_pattern & (16 << 26))
2789 slice_non_intra_DCT (decoder, 2, dest_v + DCT_offset,
2790 DCT_stride);
2792 if (coded_block_pattern & (8 << 26))
2793 slice_non_intra_DCT (decoder, 1, dest_u + 8,
2794 DCT_stride);
2796 if (coded_block_pattern & (4 << 26))
2797 slice_non_intra_DCT (decoder, 2, dest_v + 8,
2798 DCT_stride);
2800 if (coded_block_pattern & (2 << 26))
2801 slice_non_intra_DCT (decoder, 1,
2802 dest_u + DCT_offset + 8,
2803 DCT_stride);
2805 if (coded_block_pattern & (1 << 26))
2806 slice_non_intra_DCT (decoder, 2,
2807 dest_v + DCT_offset + 8,
2808 DCT_stride);
2809 #endif /* MPEG2_COLOR */
2811 #if !MPEG2_COLOR
2812 skip_chroma_non_intra(decoder, coded_block_pattern);
2813 #endif
2816 #if MPEG2_COLOR
2817 decoder->dc_dct_pred[0] = decoder->dc_dct_pred[1] =
2818 decoder->dc_dct_pred[2] = 16384;
2819 #else
2820 decoder->dc_dct_pred[0] = 16384;
2821 #endif
2824 NEXT_MACROBLOCK;
2826 NEEDBITS (bit_buf, bits, bit_ptr);
2827 mba_inc = 0;
2829 while (1)
2831 if (bit_buf >= 0x10000000)
2833 mba = MBA_5 + (UBITS (bit_buf, 5) - 2);
2834 break;
2836 else if (bit_buf >= 0x03000000)
2838 mba = MBA_11 + (UBITS (bit_buf, 11) - 24);
2839 break;
2841 else
2843 switch (UBITS (bit_buf, 11))
2845 case 8: /* macroblock_escape */
2846 mba_inc += 33;
2847 /* pass through */
2848 case 15: /* macroblock_stuffing (MPEG1 only) */
2849 DUMPBITS (bit_buf, bits, 11);
2850 NEEDBITS (bit_buf, bits, bit_ptr);
2851 continue;
2852 default: /* end of slice, or error */
2853 return;
2858 DUMPBITS (bit_buf, bits, mba->len);
2859 mba_inc += mba->mba;
2861 if (mba_inc)
2863 #if MPEG2_COLOR
2864 decoder->dc_dct_pred[0] = decoder->dc_dct_pred[1] =
2865 decoder->dc_dct_pred[2] = 16384;
2866 #else
2867 decoder->dc_dct_pred[0] = 16384;
2868 #endif
2869 if (decoder->coding_type == P_TYPE)
2873 MOTION_CALL (decoder->motion_parser[0],
2874 MACROBLOCK_MOTION_FORWARD);
2875 NEXT_MACROBLOCK;
2877 while (--mba_inc);
2879 else
2883 MOTION_CALL (decoder->motion_parser[4], macroblock_modes);
2884 NEXT_MACROBLOCK;
2886 while (--mba_inc);
2891 #undef bit_buf
2892 #undef bits
2893 #undef bit_ptr