summaryrefslogtreecommitdiff
path: root/apps/plugins/mpegplayer/slice.c
diff options
context:
space:
mode:
Diffstat (limited to 'apps/plugins/mpegplayer/slice.c')
-rw-r--r--apps/plugins/mpegplayer/slice.c2060
1 files changed, 2060 insertions, 0 deletions
diff --git a/apps/plugins/mpegplayer/slice.c b/apps/plugins/mpegplayer/slice.c
new file mode 100644
index 0000000000..8ee341739d
--- /dev/null
+++ b/apps/plugins/mpegplayer/slice.c
@@ -0,0 +1,2060 @@
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>
6 *
7 * This file is part of mpeg2dec, a free MPEG-2 video stream decoder.
8 * See http://libmpeg2.sourceforge.net/ for updates.
9 *
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.
14 *
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.
19 *
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
23 */
24
25#include "plugin.h"
26
27#include "mpeg2dec_config.h"
28
29#include "mpeg2.h"
30#include "attributes.h"
31#include "mpeg2_internal.h"
32
33extern mpeg2_mc_t mpeg2_mc;
34extern void (* mpeg2_idct_copy) (int16_t * block, uint8_t * dest, int stride);
35extern void (* mpeg2_idct_add) (int last, int16_t * block,
36 uint8_t * dest, int stride);
37extern void (* mpeg2_cpu_state_save) (cpu_state_t * state);
38extern void (* mpeg2_cpu_state_restore) (cpu_state_t * state);
39
40#include "vlc.h"
41
42static inline int get_macroblock_modes (mpeg2_decoder_t * const decoder)
43{
44#define bit_buf (decoder->bitstream_buf)
45#define bits (decoder->bitstream_bits)
46#define bit_ptr (decoder->bitstream_ptr)
47 int macroblock_modes;
48 const MBtab * tab;
49
50 switch (decoder->coding_type) {
51 case I_TYPE:
52
53 tab = MB_I + UBITS (bit_buf, 1);
54 DUMPBITS (bit_buf, bits, tab->len);
55 macroblock_modes = tab->modes;
56
57 if ((! (decoder->frame_pred_frame_dct)) &&
58 (decoder->picture_structure == FRAME_PICTURE)) {
59 macroblock_modes |= UBITS (bit_buf, 1) * DCT_TYPE_INTERLACED;
60 DUMPBITS (bit_buf, bits, 1);
61 }
62
63 return macroblock_modes;
64
65 case P_TYPE:
66
67 tab = MB_P + UBITS (bit_buf, 5);
68 DUMPBITS (bit_buf, bits, tab->len);
69 macroblock_modes = tab->modes;
70
71 if (decoder->picture_structure != FRAME_PICTURE) {
72 if (macroblock_modes & MACROBLOCK_MOTION_FORWARD) {
73 macroblock_modes |= UBITS (bit_buf, 2) << MOTION_TYPE_SHIFT;
74 DUMPBITS (bit_buf, bits, 2);
75 }
76 return macroblock_modes | MACROBLOCK_MOTION_FORWARD;
77 } else if (decoder->frame_pred_frame_dct) {
78 if (macroblock_modes & MACROBLOCK_MOTION_FORWARD)
79 macroblock_modes |= MC_FRAME << MOTION_TYPE_SHIFT;
80 return macroblock_modes | MACROBLOCK_MOTION_FORWARD;
81 } else {
82 if (macroblock_modes & MACROBLOCK_MOTION_FORWARD) {
83 macroblock_modes |= UBITS (bit_buf, 2) << MOTION_TYPE_SHIFT;
84 DUMPBITS (bit_buf, bits, 2);
85 }
86 if (macroblock_modes & (MACROBLOCK_INTRA | MACROBLOCK_PATTERN)) {
87 macroblock_modes |= UBITS (bit_buf, 1) * DCT_TYPE_INTERLACED;
88 DUMPBITS (bit_buf, bits, 1);
89 }
90 return macroblock_modes | MACROBLOCK_MOTION_FORWARD;
91 }
92
93 case B_TYPE:
94
95 tab = MB_B + UBITS (bit_buf, 6);
96 DUMPBITS (bit_buf, bits, tab->len);
97 macroblock_modes = tab->modes;
98
99 if (decoder->picture_structure != FRAME_PICTURE) {
100 if (! (macroblock_modes & MACROBLOCK_INTRA)) {
101 macroblock_modes |= UBITS (bit_buf, 2) << MOTION_TYPE_SHIFT;
102 DUMPBITS (bit_buf, bits, 2);
103 }
104 return macroblock_modes;
105 } else if (decoder->frame_pred_frame_dct) {
106 /* if (! (macroblock_modes & MACROBLOCK_INTRA)) */
107 macroblock_modes |= MC_FRAME << MOTION_TYPE_SHIFT;
108 return macroblock_modes;
109 } else {
110 if (macroblock_modes & MACROBLOCK_INTRA)
111 goto intra;
112 macroblock_modes |= UBITS (bit_buf, 2) << MOTION_TYPE_SHIFT;
113 DUMPBITS (bit_buf, bits, 2);
114 if (macroblock_modes & (MACROBLOCK_INTRA | MACROBLOCK_PATTERN)) {
115 intra:
116 macroblock_modes |= UBITS (bit_buf, 1) * DCT_TYPE_INTERLACED;
117 DUMPBITS (bit_buf, bits, 1);
118 }
119 return macroblock_modes;
120 }
121
122 case D_TYPE:
123
124 DUMPBITS (bit_buf, bits, 1);
125 return MACROBLOCK_INTRA;
126
127 default:
128 return 0;
129 }
130#undef bit_buf
131#undef bits
132#undef bit_ptr
133}
134
135static inline void get_quantizer_scale (mpeg2_decoder_t * const decoder)
136{
137#define bit_buf (decoder->bitstream_buf)
138#define bits (decoder->bitstream_bits)
139#define bit_ptr (decoder->bitstream_ptr)
140
141 int quantizer_scale_code;
142
143 quantizer_scale_code = UBITS (bit_buf, 5);
144 DUMPBITS (bit_buf, bits, 5);
145
146 decoder->quantizer_matrix[0] =
147 decoder->quantizer_prescale[0][quantizer_scale_code];
148 decoder->quantizer_matrix[1] =
149 decoder->quantizer_prescale[1][quantizer_scale_code];
150 decoder->quantizer_matrix[2] =
151 decoder->chroma_quantizer[0][quantizer_scale_code];
152 decoder->quantizer_matrix[3] =
153 decoder->chroma_quantizer[1][quantizer_scale_code];
154#undef bit_buf
155#undef bits
156#undef bit_ptr
157}
158
159static inline int get_motion_delta (mpeg2_decoder_t * const decoder,
160 const int f_code)
161{
162#define bit_buf (decoder->bitstream_buf)
163#define bits (decoder->bitstream_bits)
164#define bit_ptr (decoder->bitstream_ptr)
165
166 int delta;
167 int sign;
168 const MVtab * tab;
169
170 if (bit_buf & 0x80000000) {
171 DUMPBITS (bit_buf, bits, 1);
172 return 0;
173 } else if (bit_buf >= 0x0c000000) {
174
175 tab = MV_4 + UBITS (bit_buf, 4);
176 delta = (tab->delta << f_code) + 1;
177 bits += tab->len + f_code + 1;
178 bit_buf <<= tab->len;
179
180 sign = SBITS (bit_buf, 1);
181 bit_buf <<= 1;
182
183 if (f_code)
184 delta += UBITS (bit_buf, f_code);
185 bit_buf <<= f_code;
186
187 return (delta ^ sign) - sign;
188
189 } else {
190
191 tab = MV_10 + UBITS (bit_buf, 10);
192 delta = (tab->delta << f_code) + 1;
193 bits += tab->len + 1;
194 bit_buf <<= tab->len;
195
196 sign = SBITS (bit_buf, 1);
197 bit_buf <<= 1;
198
199 if (f_code) {
200 NEEDBITS (bit_buf, bits, bit_ptr);
201 delta += UBITS (bit_buf, f_code);
202 DUMPBITS (bit_buf, bits, f_code);
203 }
204
205 return (delta ^ sign) - sign;
206
207 }
208#undef bit_buf
209#undef bits
210#undef bit_ptr
211}
212
213static inline int bound_motion_vector (const int vector, const int f_code)
214{
215 return ((int32_t)vector << (27 - f_code)) >> (27 - f_code);
216}
217
218static inline int get_dmv (mpeg2_decoder_t * const decoder)
219{
220#define bit_buf (decoder->bitstream_buf)
221#define bits (decoder->bitstream_bits)
222#define bit_ptr (decoder->bitstream_ptr)
223
224 const DMVtab * tab;
225
226 tab = DMV_2 + UBITS (bit_buf, 2);
227 DUMPBITS (bit_buf, bits, tab->len);
228 return tab->dmv;
229#undef bit_buf
230#undef bits
231#undef bit_ptr
232}
233
234static inline int get_coded_block_pattern (mpeg2_decoder_t * const decoder)
235{
236#define bit_buf (decoder->bitstream_buf)
237#define bits (decoder->bitstream_bits)
238#define bit_ptr (decoder->bitstream_ptr)
239
240 const CBPtab * tab;
241
242 NEEDBITS (bit_buf, bits, bit_ptr);
243
244 if (bit_buf >= 0x20000000) {
245
246 tab = CBP_7 + (UBITS (bit_buf, 7) - 16);
247 DUMPBITS (bit_buf, bits, tab->len);
248 return tab->cbp;
249
250 } else {
251
252 tab = CBP_9 + UBITS (bit_buf, 9);
253 DUMPBITS (bit_buf, bits, tab->len);
254 return tab->cbp;
255 }
256
257#undef bit_buf
258#undef bits
259#undef bit_ptr
260}
261
262static inline int get_luma_dc_dct_diff (mpeg2_decoder_t * const decoder)
263{
264#define bit_buf (decoder->bitstream_buf)
265#define bits (decoder->bitstream_bits)
266#define bit_ptr (decoder->bitstream_ptr)
267 const DCtab * tab;
268 int size;
269 int dc_diff;
270
271 if (bit_buf < 0xf8000000) {
272 tab = DC_lum_5 + UBITS (bit_buf, 5);
273 size = tab->size;
274 if (size) {
275 bits += tab->len + size;
276 bit_buf <<= tab->len;
277 dc_diff =
278 UBITS (bit_buf, size) - UBITS (SBITS (~bit_buf, 1), size);
279 bit_buf <<= size;
280 return dc_diff << decoder->intra_dc_precision;
281 } else {
282 DUMPBITS (bit_buf, bits, 3);
283 return 0;
284 }
285 } else {
286 tab = DC_long + (UBITS (bit_buf, 9) - 0x1e0);
287 size = tab->size;
288 DUMPBITS (bit_buf, bits, tab->len);
289 NEEDBITS (bit_buf, bits, bit_ptr);
290 dc_diff = UBITS (bit_buf, size) - UBITS (SBITS (~bit_buf, 1), size);
291 DUMPBITS (bit_buf, bits, size);
292 return dc_diff << decoder->intra_dc_precision;
293 }
294#undef bit_buf
295#undef bits
296#undef bit_ptr
297}
298
299static inline int get_chroma_dc_dct_diff (mpeg2_decoder_t * const decoder)
300{
301#define bit_buf (decoder->bitstream_buf)
302#define bits (decoder->bitstream_bits)
303#define bit_ptr (decoder->bitstream_ptr)
304 const DCtab * tab;
305 int size;
306 int dc_diff;
307
308 if (bit_buf < 0xf8000000) {
309 tab = DC_chrom_5 + UBITS (bit_buf, 5);
310 size = tab->size;
311 if (size) {
312 bits += tab->len + size;
313 bit_buf <<= tab->len;
314 dc_diff =
315 UBITS (bit_buf, size) - UBITS (SBITS (~bit_buf, 1), size);
316 bit_buf <<= size;
317 return dc_diff << decoder->intra_dc_precision;
318 } else {
319 DUMPBITS (bit_buf, bits, 2);
320 return 0;
321 }
322 } else {
323 tab = DC_long + (UBITS (bit_buf, 10) - 0x3e0);
324 size = tab->size;
325 DUMPBITS (bit_buf, bits, tab->len + 1);
326 NEEDBITS (bit_buf, bits, bit_ptr);
327 dc_diff = UBITS (bit_buf, size) - UBITS (SBITS (~bit_buf, 1), size);
328 DUMPBITS (bit_buf, bits, size);
329 return dc_diff << decoder->intra_dc_precision;
330 }
331#undef bit_buf
332#undef bits
333#undef bit_ptr
334}
335
336#define SATURATE(val) \
337do { \
338 val <<= 4; \
339 if (unlikely (val != (int16_t) val)) \
340 val = (SBITS (val, 1) ^ 2047) << 4; \
341} while (0)
342
343static void get_intra_block_B14 (mpeg2_decoder_t * const decoder,
344 const uint16_t * const quant_matrix)
345{
346 int i;
347 int j;
348 int val;
349 const uint8_t * const scan = decoder->scan;
350 int mismatch;
351 const DCTtab * tab;
352 uint32_t bit_buf;
353 int bits;
354 const uint8_t * bit_ptr;
355 int16_t * const dest = decoder->DCTblock;
356
357 i = 0;
358 mismatch = ~dest[0];
359
360 bit_buf = decoder->bitstream_buf;
361 bits = decoder->bitstream_bits;
362 bit_ptr = decoder->bitstream_ptr;
363
364 NEEDBITS (bit_buf, bits, bit_ptr);
365
366 while (1) {
367 if (bit_buf >= 0x28000000) {
368
369 tab = DCT_B14AC_5 + (UBITS (bit_buf, 5) - 5);
370
371 i += tab->run;
372 if (i >= 64)
373 break; /* end of block */
374
375 normal_code:
376 j = scan[i];
377 bit_buf <<= tab->len;
378 bits += tab->len + 1;
379 val = (tab->level * quant_matrix[j]) >> 4;
380
381 /* if (bitstream_get (1)) val = -val; */
382 val = (val ^ SBITS (bit_buf, 1)) - SBITS (bit_buf, 1);
383
384 SATURATE (val);
385 dest[j] = val;
386 mismatch ^= val;
387
388 bit_buf <<= 1;
389 NEEDBITS (bit_buf, bits, bit_ptr);
390
391 continue;
392
393 } else if (bit_buf >= 0x04000000) {
394
395 tab = DCT_B14_8 + (UBITS (bit_buf, 8) - 4);
396
397 i += tab->run;
398 if (i < 64)
399 goto normal_code;
400
401 /* escape code */
402
403 i += UBITS (bit_buf << 6, 6) - 64;
404 if (i >= 64)
405 break; /* illegal, check needed to avoid buffer overflow */
406
407 j = scan[i];
408
409 DUMPBITS (bit_buf, bits, 12);
410 NEEDBITS (bit_buf, bits, bit_ptr);
411 val = (SBITS (bit_buf, 12) * quant_matrix[j]) / 16;
412
413 SATURATE (val);
414 dest[j] = val;
415 mismatch ^= val;
416
417 DUMPBITS (bit_buf, bits, 12);
418 NEEDBITS (bit_buf, bits, bit_ptr);
419
420 continue;
421
422 } else if (bit_buf >= 0x02000000) {
423 tab = DCT_B14_10 + (UBITS (bit_buf, 10) - 8);
424 i += tab->run;
425 if (i < 64)
426 goto normal_code;
427 } else if (bit_buf >= 0x00800000) {
428 tab = DCT_13 + (UBITS (bit_buf, 13) - 16);
429 i += tab->run;
430 if (i < 64)
431 goto normal_code;
432 } else if (bit_buf >= 0x00200000) {
433 tab = DCT_15 + (UBITS (bit_buf, 15) - 16);
434 i += tab->run;
435 if (i < 64)
436 goto normal_code;
437 } else {
438 tab = DCT_16 + UBITS (bit_buf, 16);
439 bit_buf <<= 16;
440 GETWORD (bit_buf, bits + 16, bit_ptr);
441 i += tab->run;
442 if (i < 64)
443 goto normal_code;
444 }
445 break; /* illegal, check needed to avoid buffer overflow */
446 }
447 dest[63] ^= mismatch & 16;
448 DUMPBITS (bit_buf, bits, 2); /* dump end of block code */
449 decoder->bitstream_buf = bit_buf;
450 decoder->bitstream_bits = bits;
451 decoder->bitstream_ptr = bit_ptr;
452}
453
454static void get_intra_block_B15 (mpeg2_decoder_t * const decoder,
455 const uint16_t * const quant_matrix)
456{
457 int i;
458 int j;
459 int val;
460 const uint8_t * const scan = decoder->scan;
461 int mismatch;
462 const DCTtab * tab;
463 uint32_t bit_buf;
464 int bits;
465 const uint8_t * bit_ptr;
466 int16_t * const dest = decoder->DCTblock;
467
468 i = 0;
469 mismatch = ~dest[0];
470
471 bit_buf = decoder->bitstream_buf;
472 bits = decoder->bitstream_bits;
473 bit_ptr = decoder->bitstream_ptr;
474
475 NEEDBITS (bit_buf, bits, bit_ptr);
476
477 while (1) {
478 if (bit_buf >= 0x04000000) {
479
480 tab = DCT_B15_8 + (UBITS (bit_buf, 8) - 4);
481
482 i += tab->run;
483 if (i < 64) {
484
485 normal_code:
486 j = scan[i];
487 bit_buf <<= tab->len;
488 bits += tab->len + 1;
489 val = (tab->level * quant_matrix[j]) >> 4;
490
491 /* if (bitstream_get (1)) val = -val; */
492 val = (val ^ SBITS (bit_buf, 1)) - SBITS (bit_buf, 1);
493
494 SATURATE (val);
495 dest[j] = val;
496 mismatch ^= val;
497
498 bit_buf <<= 1;
499 NEEDBITS (bit_buf, bits, bit_ptr);
500
501 continue;
502
503 } else {
504
505 /* end of block. I commented out this code because if we */
506 /* dont exit here we will still exit at the later test :) */
507
508 /* if (i >= 128) break; */ /* end of block */
509
510 /* escape code */
511
512 i += UBITS (bit_buf << 6, 6) - 64;
513 if (i >= 64)
514 break; /* illegal, check against buffer overflow */
515
516 j = scan[i];
517
518 DUMPBITS (bit_buf, bits, 12);
519 NEEDBITS (bit_buf, bits, bit_ptr);
520 val = (SBITS (bit_buf, 12) * quant_matrix[j]) / 16;
521
522 SATURATE (val);
523 dest[j] = val;
524 mismatch ^= val;
525
526 DUMPBITS (bit_buf, bits, 12);
527 NEEDBITS (bit_buf, bits, bit_ptr);
528
529 continue;
530
531 }
532 } else if (bit_buf >= 0x02000000) {
533 tab = DCT_B15_10 + (UBITS (bit_buf, 10) - 8);
534 i += tab->run;
535 if (i < 64)
536 goto normal_code;
537 } else if (bit_buf >= 0x00800000) {
538 tab = DCT_13 + (UBITS (bit_buf, 13) - 16);
539 i += tab->run;
540 if (i < 64)
541 goto normal_code;
542 } else if (bit_buf >= 0x00200000) {
543 tab = DCT_15 + (UBITS (bit_buf, 15) - 16);
544 i += tab->run;
545 if (i < 64)
546 goto normal_code;
547 } else {
548 tab = DCT_16 + UBITS (bit_buf, 16);
549 bit_buf <<= 16;
550 GETWORD (bit_buf, bits + 16, bit_ptr);
551 i += tab->run;
552 if (i < 64)
553 goto normal_code;
554 }
555 break; /* illegal, check needed to avoid buffer overflow */
556 }
557 dest[63] ^= mismatch & 16;
558 DUMPBITS (bit_buf, bits, 4); /* dump end of block code */
559 decoder->bitstream_buf = bit_buf;
560 decoder->bitstream_bits = bits;
561 decoder->bitstream_ptr = bit_ptr;
562}
563
564static int get_non_intra_block (mpeg2_decoder_t * const decoder,
565 const uint16_t * const quant_matrix)
566{
567 int i;
568 int j;
569 int val;
570 const uint8_t * const scan = decoder->scan;
571 int mismatch;
572 const DCTtab * tab;
573 uint32_t bit_buf;
574 int bits;
575 const uint8_t * bit_ptr;
576 int16_t * const dest = decoder->DCTblock;
577
578 i = -1;
579 mismatch = -1;
580
581 bit_buf = decoder->bitstream_buf;
582 bits = decoder->bitstream_bits;
583 bit_ptr = decoder->bitstream_ptr;
584
585 NEEDBITS (bit_buf, bits, bit_ptr);
586 if (bit_buf >= 0x28000000) {
587 tab = DCT_B14DC_5 + (UBITS (bit_buf, 5) - 5);
588 goto entry_1;
589 } else
590 goto entry_2;
591
592 while (1) {
593 if (bit_buf >= 0x28000000) {
594
595 tab = DCT_B14AC_5 + (UBITS (bit_buf, 5) - 5);
596
597 entry_1:
598 i += tab->run;
599 if (i >= 64)
600 break; /* end of block */
601
602 normal_code:
603 j = scan[i];
604 bit_buf <<= tab->len;
605 bits += tab->len + 1;
606 val = ((2 * tab->level + 1) * quant_matrix[j]) >> 5;
607
608 /* if (bitstream_get (1)) val = -val; */
609 val = (val ^ SBITS (bit_buf, 1)) - SBITS (bit_buf, 1);
610
611 SATURATE (val);
612 dest[j] = val;
613 mismatch ^= val;
614
615 bit_buf <<= 1;
616 NEEDBITS (bit_buf, bits, bit_ptr);
617
618 continue;
619
620 }
621
622 entry_2:
623 if (bit_buf >= 0x04000000) {
624
625 tab = DCT_B14_8 + (UBITS (bit_buf, 8) - 4);
626
627 i += tab->run;
628 if (i < 64)
629 goto normal_code;
630
631 /* escape code */
632
633 i += UBITS (bit_buf << 6, 6) - 64;
634 if (i >= 64)
635 break; /* illegal, check needed to avoid buffer overflow */
636
637 j = scan[i];
638
639 DUMPBITS (bit_buf, bits, 12);
640 NEEDBITS (bit_buf, bits, bit_ptr);
641 val = 2 * (SBITS (bit_buf, 12) + SBITS (bit_buf, 1)) + 1;
642 val = (val * quant_matrix[j]) / 32;
643
644 SATURATE (val);
645 dest[j] = val;
646 mismatch ^= val;
647
648 DUMPBITS (bit_buf, bits, 12);
649 NEEDBITS (bit_buf, bits, bit_ptr);
650
651 continue;
652
653 } else if (bit_buf >= 0x02000000) {
654 tab = DCT_B14_10 + (UBITS (bit_buf, 10) - 8);
655 i += tab->run;
656 if (i < 64)
657 goto normal_code;
658 } else if (bit_buf >= 0x00800000) {
659 tab = DCT_13 + (UBITS (bit_buf, 13) - 16);
660 i += tab->run;
661 if (i < 64)
662 goto normal_code;
663 } else if (bit_buf >= 0x00200000) {
664 tab = DCT_15 + (UBITS (bit_buf, 15) - 16);
665 i += tab->run;
666 if (i < 64)
667 goto normal_code;
668 } else {
669 tab = DCT_16 + UBITS (bit_buf, 16);
670 bit_buf <<= 16;
671 GETWORD (bit_buf, bits + 16, bit_ptr);
672 i += tab->run;
673 if (i < 64)
674 goto normal_code;
675 }
676 break; /* illegal, check needed to avoid buffer overflow */
677 }
678 dest[63] ^= mismatch & 16;
679 DUMPBITS (bit_buf, bits, 2); /* dump end of block code */
680 decoder->bitstream_buf = bit_buf;
681 decoder->bitstream_bits = bits;
682 decoder->bitstream_ptr = bit_ptr;
683 return i;
684}
685
686static void get_mpeg1_intra_block (mpeg2_decoder_t * const decoder)
687{
688 int i;
689 int j;
690 int val;
691 const uint8_t * const scan = decoder->scan;
692 const uint16_t * const quant_matrix = decoder->quantizer_matrix[0];
693 const DCTtab * tab;
694 uint32_t bit_buf;
695 int bits;
696 const uint8_t * bit_ptr;
697 int16_t * const dest = decoder->DCTblock;
698
699 i = 0;
700
701 bit_buf = decoder->bitstream_buf;
702 bits = decoder->bitstream_bits;
703 bit_ptr = decoder->bitstream_ptr;
704
705 NEEDBITS (bit_buf, bits, bit_ptr);
706
707 while (1) {
708 if (bit_buf >= 0x28000000) {
709
710 tab = DCT_B14AC_5 + (UBITS (bit_buf, 5) - 5);
711
712 i += tab->run;
713 if (i >= 64)
714 break; /* end of block */
715
716 normal_code:
717 j = scan[i];
718 bit_buf <<= tab->len;
719 bits += tab->len + 1;
720 val = (tab->level * quant_matrix[j]) >> 4;
721
722 /* oddification */
723 val = (val - 1) | 1;
724
725 /* if (bitstream_get (1)) val = -val; */
726 val = (val ^ SBITS (bit_buf, 1)) - SBITS (bit_buf, 1);
727
728 SATURATE (val);
729 dest[j] = val;
730
731 bit_buf <<= 1;
732 NEEDBITS (bit_buf, bits, bit_ptr);
733
734 continue;
735
736 } else if (bit_buf >= 0x04000000) {
737
738 tab = DCT_B14_8 + (UBITS (bit_buf, 8) - 4);
739
740 i += tab->run;
741 if (i < 64)
742 goto normal_code;
743
744 /* escape code */
745
746 i += UBITS (bit_buf << 6, 6) - 64;
747 if (i >= 64)
748 break; /* illegal, check needed to avoid buffer overflow */
749
750 j = scan[i];
751
752 DUMPBITS (bit_buf, bits, 12);
753 NEEDBITS (bit_buf, bits, bit_ptr);
754 val = SBITS (bit_buf, 8);
755 if (! (val & 0x7f)) {
756 DUMPBITS (bit_buf, bits, 8);
757 val = UBITS (bit_buf, 8) + 2 * val;
758 }
759 val = (val * quant_matrix[j]) / 16;
760
761 /* oddification */
762 val = (val + ~SBITS (val, 1)) | 1;
763
764 SATURATE (val);
765 dest[j] = val;
766
767 DUMPBITS (bit_buf, bits, 8);
768 NEEDBITS (bit_buf, bits, bit_ptr);
769
770 continue;
771
772 } else if (bit_buf >= 0x02000000) {
773 tab = DCT_B14_10 + (UBITS (bit_buf, 10) - 8);
774 i += tab->run;
775 if (i < 64)
776 goto normal_code;
777 } else if (bit_buf >= 0x00800000) {
778 tab = DCT_13 + (UBITS (bit_buf, 13) - 16);
779 i += tab->run;
780 if (i < 64)
781 goto normal_code;
782 } else if (bit_buf >= 0x00200000) {
783 tab = DCT_15 + (UBITS (bit_buf, 15) - 16);
784 i += tab->run;
785 if (i < 64)
786 goto normal_code;
787 } else {
788 tab = DCT_16 + UBITS (bit_buf, 16);
789 bit_buf <<= 16;
790 GETWORD (bit_buf, bits + 16, bit_ptr);
791 i += tab->run;
792 if (i < 64)
793 goto normal_code;
794 }
795 break; /* illegal, check needed to avoid buffer overflow */
796 }
797 DUMPBITS (bit_buf, bits, 2); /* dump end of block code */
798 decoder->bitstream_buf = bit_buf;
799 decoder->bitstream_bits = bits;
800 decoder->bitstream_ptr = bit_ptr;
801}
802
803static int get_mpeg1_non_intra_block (mpeg2_decoder_t * const decoder)
804{
805 int i;
806 int j;
807 int val;
808 const uint8_t * const scan = decoder->scan;
809 const uint16_t * const quant_matrix = decoder->quantizer_matrix[1];
810 const DCTtab * tab;
811 uint32_t bit_buf;
812 int bits;
813 const uint8_t * bit_ptr;
814 int16_t * const dest = decoder->DCTblock;
815
816 i = -1;
817
818 bit_buf = decoder->bitstream_buf;
819 bits = decoder->bitstream_bits;
820 bit_ptr = decoder->bitstream_ptr;
821
822 NEEDBITS (bit_buf, bits, bit_ptr);
823 if (bit_buf >= 0x28000000) {
824 tab = DCT_B14DC_5 + (UBITS (bit_buf, 5) - 5);
825 goto entry_1;
826 } else
827 goto entry_2;
828
829 while (1) {
830 if (bit_buf >= 0x28000000) {
831
832 tab = DCT_B14AC_5 + (UBITS (bit_buf, 5) - 5);
833
834 entry_1:
835 i += tab->run;
836 if (i >= 64)
837 break; /* end of block */
838
839 normal_code:
840 j = scan[i];
841 bit_buf <<= tab->len;
842 bits += tab->len + 1;
843 val = ((2 * tab->level + 1) * quant_matrix[j]) >> 5;
844
845 /* oddification */
846 val = (val - 1) | 1;
847
848 /* if (bitstream_get (1)) val = -val; */
849 val = (val ^ SBITS (bit_buf, 1)) - SBITS (bit_buf, 1);
850
851 SATURATE (val);
852 dest[j] = val;
853
854 bit_buf <<= 1;
855 NEEDBITS (bit_buf, bits, bit_ptr);
856
857 continue;
858
859 }
860
861 entry_2:
862 if (bit_buf >= 0x04000000) {
863
864 tab = DCT_B14_8 + (UBITS (bit_buf, 8) - 4);
865
866 i += tab->run;
867 if (i < 64)
868 goto normal_code;
869
870 /* escape code */
871
872 i += UBITS (bit_buf << 6, 6) - 64;
873 if (i >= 64)
874 break; /* illegal, check needed to avoid buffer overflow */
875
876 j = scan[i];
877
878 DUMPBITS (bit_buf, bits, 12);
879 NEEDBITS (bit_buf, bits, bit_ptr);
880 val = SBITS (bit_buf, 8);
881 if (! (val & 0x7f)) {
882 DUMPBITS (bit_buf, bits, 8);
883 val = UBITS (bit_buf, 8) + 2 * val;
884 }
885 val = 2 * (val + SBITS (val, 1)) + 1;
886 val = (val * quant_matrix[j]) / 32;
887
888 /* oddification */
889 val = (val + ~SBITS (val, 1)) | 1;
890
891 SATURATE (val);
892 dest[j] = val;
893
894 DUMPBITS (bit_buf, bits, 8);
895 NEEDBITS (bit_buf, bits, bit_ptr);
896
897 continue;
898
899 } else if (bit_buf >= 0x02000000) {
900 tab = DCT_B14_10 + (UBITS (bit_buf, 10) - 8);
901 i += tab->run;
902 if (i < 64)
903 goto normal_code;
904 } else if (bit_buf >= 0x00800000) {
905 tab = DCT_13 + (UBITS (bit_buf, 13) - 16);
906 i += tab->run;
907 if (i < 64)
908 goto normal_code;
909 } else if (bit_buf >= 0x00200000) {
910 tab = DCT_15 + (UBITS (bit_buf, 15) - 16);
911 i += tab->run;
912 if (i < 64)
913 goto normal_code;
914 } else {
915 tab = DCT_16 + UBITS (bit_buf, 16);
916 bit_buf <<= 16;
917 GETWORD (bit_buf, bits + 16, bit_ptr);
918 i += tab->run;
919 if (i < 64)
920 goto normal_code;
921 }
922 break; /* illegal, check needed to avoid buffer overflow */
923 }
924 DUMPBITS (bit_buf, bits, 2); /* dump end of block code */
925 decoder->bitstream_buf = bit_buf;
926 decoder->bitstream_bits = bits;
927 decoder->bitstream_ptr = bit_ptr;
928 return i;
929}
930
931static inline void slice_intra_DCT (mpeg2_decoder_t * const decoder,
932 const int cc,
933 uint8_t * const dest, const int stride)
934{
935#define bit_buf (decoder->bitstream_buf)
936#define bits (decoder->bitstream_bits)
937#define bit_ptr (decoder->bitstream_ptr)
938 NEEDBITS (bit_buf, bits, bit_ptr);
939 /* Get the intra DC coefficient and inverse quantize it */
940 if (cc == 0)
941 decoder->DCTblock[0] =
942 decoder->dc_dct_pred[0] += get_luma_dc_dct_diff (decoder);
943 else
944 decoder->DCTblock[0] =
945 decoder->dc_dct_pred[cc] += get_chroma_dc_dct_diff (decoder);
946
947 if (decoder->mpeg1) {
948 if (decoder->coding_type != D_TYPE)
949 get_mpeg1_intra_block (decoder);
950 } else if (decoder->intra_vlc_format)
951 get_intra_block_B15 (decoder, decoder->quantizer_matrix[cc ? 2 : 0]);
952 else
953 get_intra_block_B14 (decoder, decoder->quantizer_matrix[cc ? 2 : 0]);
954 mpeg2_idct_copy (decoder->DCTblock, dest, stride);
955#undef bit_buf
956#undef bits
957#undef bit_ptr
958}
959
960static inline void slice_non_intra_DCT (mpeg2_decoder_t * const decoder,
961 const int cc,
962 uint8_t * const dest, const int stride)
963{
964 int last;
965
966 if (decoder->mpeg1)
967 last = get_mpeg1_non_intra_block (decoder);
968 else
969 last = get_non_intra_block (decoder,
970 decoder->quantizer_matrix[cc ? 3 : 1]);
971 mpeg2_idct_add (last, decoder->DCTblock, dest, stride);
972}
973
974#define MOTION_420(table,ref,motion_x,motion_y,size,y) \
975 pos_x = 2 * decoder->offset + motion_x; \
976 pos_y = 2 * decoder->v_offset + motion_y + 2 * y; \
977 if (unlikely (pos_x > decoder->limit_x)) { \
978 pos_x = ((int)pos_x < 0) ? 0 : decoder->limit_x; \
979 motion_x = pos_x - 2 * decoder->offset; \
980 } \
981 if (unlikely (pos_y > decoder->limit_y_ ## size)) { \
982 pos_y = ((int)pos_y < 0) ? 0 : decoder->limit_y_ ## size; \
983 motion_y = pos_y - 2 * decoder->v_offset - 2 * y; \
984 } \
985 xy_half = ((pos_y & 1) << 1) | (pos_x & 1); \
986 table[xy_half] (decoder->dest[0] + y * decoder->stride + decoder->offset, \
987 ref[0] + (pos_x >> 1) + (pos_y >> 1) * decoder->stride, \
988 decoder->stride, size); \
989 motion_x /= 2; motion_y /= 2; \
990 xy_half = ((motion_y & 1) << 1) | (motion_x & 1); \
991 offset = (((decoder->offset + motion_x) >> 1) + \
992 ((((decoder->v_offset + motion_y) >> 1) + y/2) * \
993 decoder->uv_stride)); \
994 table[4+xy_half] (decoder->dest[1] + y/2 * decoder->uv_stride + \
995 (decoder->offset >> 1), ref[1] + offset, \
996 decoder->uv_stride, size/2); \
997 table[4+xy_half] (decoder->dest[2] + y/2 * decoder->uv_stride + \
998 (decoder->offset >> 1), ref[2] + offset, \
999 decoder->uv_stride, size/2)
1000
1001#define MOTION_FIELD_420(table,ref,motion_x,motion_y,dest_field,op,src_field) \
1002 pos_x = 2 * decoder->offset + motion_x; \
1003 pos_y = decoder->v_offset + motion_y; \
1004 if (unlikely (pos_x > decoder->limit_x)) { \
1005 pos_x = ((int)pos_x < 0) ? 0 : decoder->limit_x; \
1006 motion_x = pos_x - 2 * decoder->offset; \
1007 } \
1008 if (unlikely (pos_y > decoder->limit_y)) { \
1009 pos_y = ((int)pos_y < 0) ? 0 : decoder->limit_y; \
1010 motion_y = pos_y - decoder->v_offset; \
1011 } \
1012 xy_half = ((pos_y & 1) << 1) | (pos_x & 1); \
1013 table[xy_half] (decoder->dest[0] + dest_field * decoder->stride + \
1014 decoder->offset, \
1015 (ref[0] + (pos_x >> 1) + \
1016 ((pos_y op) + src_field) * decoder->stride), \
1017 2 * decoder->stride, 8); \
1018 motion_x /= 2; motion_y /= 2; \
1019 xy_half = ((motion_y & 1) << 1) | (motion_x & 1); \
1020 offset = (((decoder->offset + motion_x) >> 1) + \
1021 (((decoder->v_offset >> 1) + (motion_y op) + src_field) * \
1022 decoder->uv_stride)); \
1023 table[4+xy_half] (decoder->dest[1] + dest_field * decoder->uv_stride + \
1024 (decoder->offset >> 1), ref[1] + offset, \
1025 2 * decoder->uv_stride, 4); \
1026 table[4+xy_half] (decoder->dest[2] + dest_field * decoder->uv_stride + \
1027 (decoder->offset >> 1), ref[2] + offset, \
1028 2 * decoder->uv_stride, 4)
1029
1030#define MOTION_DMV_420(table,ref,motion_x,motion_y) \
1031 pos_x = 2 * decoder->offset + motion_x; \
1032 pos_y = decoder->v_offset + motion_y; \
1033 if (unlikely (pos_x > decoder->limit_x)) { \
1034 pos_x = ((int)pos_x < 0) ? 0 : decoder->limit_x; \
1035 motion_x = pos_x - 2 * decoder->offset; \
1036 } \
1037 if (unlikely (pos_y > decoder->limit_y)) { \
1038 pos_y = ((int)pos_y < 0) ? 0 : decoder->limit_y; \
1039 motion_y = pos_y - decoder->v_offset; \
1040 } \
1041 xy_half = ((pos_y & 1) << 1) | (pos_x & 1); \
1042 offset = (pos_x >> 1) + (pos_y & ~1) * decoder->stride; \
1043 table[xy_half] (decoder->dest[0] + decoder->offset, \
1044 ref[0] + offset, 2 * decoder->stride, 8); \
1045 table[xy_half] (decoder->dest[0] + decoder->stride + decoder->offset, \
1046 ref[0] + decoder->stride + offset, \
1047 2 * decoder->stride, 8); \
1048 motion_x /= 2; motion_y /= 2; \
1049 xy_half = ((motion_y & 1) << 1) | (motion_x & 1); \
1050 offset = (((decoder->offset + motion_x) >> 1) + \
1051 (((decoder->v_offset >> 1) + (motion_y & ~1)) * \
1052 decoder->uv_stride)); \
1053 table[4+xy_half] (decoder->dest[1] + (decoder->offset >> 1), \
1054 ref[1] + offset, 2 * decoder->uv_stride, 4); \
1055 table[4+xy_half] (decoder->dest[1] + decoder->uv_stride + \
1056 (decoder->offset >> 1), \
1057 ref[1] + decoder->uv_stride + offset, \
1058 2 * decoder->uv_stride, 4); \
1059 table[4+xy_half] (decoder->dest[2] + (decoder->offset >> 1), \
1060 ref[2] + offset, 2 * decoder->uv_stride, 4); \
1061 table[4+xy_half] (decoder->dest[2] + decoder->uv_stride + \
1062 (decoder->offset >> 1), \
1063 ref[2] + decoder->uv_stride + offset, \
1064 2 * decoder->uv_stride, 4)
1065
1066#define MOTION_ZERO_420(table,ref) \
1067 table[0] (decoder->dest[0] + decoder->offset, \
1068 (ref[0] + decoder->offset + \
1069 decoder->v_offset * decoder->stride), decoder->stride, 16); \
1070 offset = ((decoder->offset >> 1) + \
1071 (decoder->v_offset >> 1) * decoder->uv_stride); \
1072 table[4] (decoder->dest[1] + (decoder->offset >> 1), \
1073 ref[1] + offset, decoder->uv_stride, 8); \
1074 table[4] (decoder->dest[2] + (decoder->offset >> 1), \
1075 ref[2] + offset, decoder->uv_stride, 8)
1076
1077#define MOTION_422(table,ref,motion_x,motion_y,size,y) \
1078 pos_x = 2 * decoder->offset + motion_x; \
1079 pos_y = 2 * decoder->v_offset + motion_y + 2 * y; \
1080 if (unlikely (pos_x > decoder->limit_x)) { \
1081 pos_x = ((int)pos_x < 0) ? 0 : decoder->limit_x; \
1082 motion_x = pos_x - 2 * decoder->offset; \
1083 } \
1084 if (unlikely (pos_y > decoder->limit_y_ ## size)) { \
1085 pos_y = ((int)pos_y < 0) ? 0 : decoder->limit_y_ ## size; \
1086 motion_y = pos_y - 2 * decoder->v_offset - 2 * y; \
1087 } \
1088 xy_half = ((pos_y & 1) << 1) | (pos_x & 1); \
1089 offset = (pos_x >> 1) + (pos_y >> 1) * decoder->stride; \
1090 table[xy_half] (decoder->dest[0] + y * decoder->stride + decoder->offset, \
1091 ref[0] + offset, decoder->stride, size); \
1092 offset = (offset + (motion_x & (motion_x < 0))) >> 1; \
1093 motion_x /= 2; \
1094 xy_half = ((pos_y & 1) << 1) | (motion_x & 1); \
1095 table[4+xy_half] (decoder->dest[1] + y * decoder->uv_stride + \
1096 (decoder->offset >> 1), ref[1] + offset, \
1097 decoder->uv_stride, size); \
1098 table[4+xy_half] (decoder->dest[2] + y * decoder->uv_stride + \
1099 (decoder->offset >> 1), ref[2] + offset, \
1100 decoder->uv_stride, size)
1101
1102#define MOTION_FIELD_422(table,ref,motion_x,motion_y,dest_field,op,src_field) \
1103 pos_x = 2 * decoder->offset + motion_x; \
1104 pos_y = decoder->v_offset + motion_y; \
1105 if (unlikely (pos_x > decoder->limit_x)) { \
1106 pos_x = ((int)pos_x < 0) ? 0 : decoder->limit_x; \
1107 motion_x = pos_x - 2 * decoder->offset; \
1108 } \
1109 if (unlikely (pos_y > decoder->limit_y)) { \
1110 pos_y = ((int)pos_y < 0) ? 0 : decoder->limit_y; \
1111 motion_y = pos_y - decoder->v_offset; \
1112 } \
1113 xy_half = ((pos_y & 1) << 1) | (pos_x & 1); \
1114 offset = (pos_x >> 1) + ((pos_y op) + src_field) * decoder->stride; \
1115 table[xy_half] (decoder->dest[0] + dest_field * decoder->stride + \
1116 decoder->offset, ref[0] + offset, \
1117 2 * decoder->stride, 8); \
1118 offset = (offset + (motion_x & (motion_x < 0))) >> 1; \
1119 motion_x /= 2; \
1120 xy_half = ((pos_y & 1) << 1) | (motion_x & 1); \
1121 table[4+xy_half] (decoder->dest[1] + dest_field * decoder->uv_stride + \
1122 (decoder->offset >> 1), ref[1] + offset, \
1123 2 * decoder->uv_stride, 8); \
1124 table[4+xy_half] (decoder->dest[2] + dest_field * decoder->uv_stride + \
1125 (decoder->offset >> 1), ref[2] + offset, \
1126 2 * decoder->uv_stride, 8)
1127
1128#define MOTION_DMV_422(table,ref,motion_x,motion_y) \
1129 pos_x = 2 * decoder->offset + motion_x; \
1130 pos_y = decoder->v_offset + motion_y; \
1131 if (unlikely (pos_x > decoder->limit_x)) { \
1132 pos_x = ((int)pos_x < 0) ? 0 : decoder->limit_x; \
1133 motion_x = pos_x - 2 * decoder->offset; \
1134 } \
1135 if (unlikely (pos_y > decoder->limit_y)) { \
1136 pos_y = ((int)pos_y < 0) ? 0 : decoder->limit_y; \
1137 motion_y = pos_y - decoder->v_offset; \
1138 } \
1139 xy_half = ((pos_y & 1) << 1) | (pos_x & 1); \
1140 offset = (pos_x >> 1) + (pos_y & ~1) * decoder->stride; \
1141 table[xy_half] (decoder->dest[0] + decoder->offset, \
1142 ref[0] + offset, 2 * decoder->stride, 8); \
1143 table[xy_half] (decoder->dest[0] + decoder->stride + decoder->offset, \
1144 ref[0] + decoder->stride + offset, \
1145 2 * decoder->stride, 8); \
1146 offset = (offset + (motion_x & (motion_x < 0))) >> 1; \
1147 motion_x /= 2; \
1148 xy_half = ((pos_y & 1) << 1) | (motion_x & 1); \
1149 table[4+xy_half] (decoder->dest[1] + (decoder->offset >> 1), \
1150 ref[1] + offset, 2 * decoder->uv_stride, 8); \
1151 table[4+xy_half] (decoder->dest[1] + decoder->uv_stride + \
1152 (decoder->offset >> 1), \
1153 ref[1] + decoder->uv_stride + offset, \
1154 2 * decoder->uv_stride, 8); \
1155 table[4+xy_half] (decoder->dest[2] + (decoder->offset >> 1), \
1156 ref[2] + offset, 2 * decoder->uv_stride, 8); \
1157 table[4+xy_half] (decoder->dest[2] + decoder->uv_stride + \
1158 (decoder->offset >> 1), \
1159 ref[2] + decoder->uv_stride + offset, \
1160 2 * decoder->uv_stride, 8)
1161
1162#define MOTION_ZERO_422(table,ref) \
1163 offset = decoder->offset + decoder->v_offset * decoder->stride; \
1164 table[0] (decoder->dest[0] + decoder->offset, \
1165 ref[0] + offset, decoder->stride, 16); \
1166 offset >>= 1; \
1167 table[4] (decoder->dest[1] + (decoder->offset >> 1), \
1168 ref[1] + offset, decoder->uv_stride, 16); \
1169 table[4] (decoder->dest[2] + (decoder->offset >> 1), \
1170 ref[2] + offset, decoder->uv_stride, 16)
1171
1172#define MOTION_444(table,ref,motion_x,motion_y,size,y) \
1173 pos_x = 2 * decoder->offset + motion_x; \
1174 pos_y = 2 * decoder->v_offset + motion_y + 2 * y; \
1175 if (unlikely (pos_x > decoder->limit_x)) { \
1176 pos_x = ((int)pos_x < 0) ? 0 : decoder->limit_x; \
1177 motion_x = pos_x - 2 * decoder->offset; \
1178 } \
1179 if (unlikely (pos_y > decoder->limit_y_ ## size)) { \
1180 pos_y = ((int)pos_y < 0) ? 0 : decoder->limit_y_ ## size; \
1181 motion_y = pos_y - 2 * decoder->v_offset - 2 * y; \
1182 } \
1183 xy_half = ((pos_y & 1) << 1) | (pos_x & 1); \
1184 offset = (pos_x >> 1) + (pos_y >> 1) * decoder->stride; \
1185 table[xy_half] (decoder->dest[0] + y * decoder->stride + decoder->offset, \
1186 ref[0] + offset, decoder->stride, size); \
1187 table[xy_half] (decoder->dest[1] + y * decoder->stride + decoder->offset, \
1188 ref[1] + offset, decoder->stride, size); \
1189 table[xy_half] (decoder->dest[2] + y * decoder->stride + decoder->offset, \
1190 ref[2] + offset, decoder->stride, size)
1191
1192#define MOTION_FIELD_444(table,ref,motion_x,motion_y,dest_field,op,src_field) \
1193 pos_x = 2 * decoder->offset + motion_x; \
1194 pos_y = decoder->v_offset + motion_y; \
1195 if (unlikely (pos_x > decoder->limit_x)) { \
1196 pos_x = ((int)pos_x < 0) ? 0 : decoder->limit_x; \
1197 motion_x = pos_x - 2 * decoder->offset; \
1198 } \
1199 if (unlikely (pos_y > decoder->limit_y)) { \
1200 pos_y = ((int)pos_y < 0) ? 0 : decoder->limit_y; \
1201 motion_y = pos_y - decoder->v_offset; \
1202 } \
1203 xy_half = ((pos_y & 1) << 1) | (pos_x & 1); \
1204 offset = (pos_x >> 1) + ((pos_y op) + src_field) * decoder->stride; \
1205 table[xy_half] (decoder->dest[0] + dest_field * decoder->stride + \
1206 decoder->offset, ref[0] + offset, \
1207 2 * decoder->stride, 8); \
1208 table[xy_half] (decoder->dest[1] + dest_field * decoder->stride + \
1209 decoder->offset, ref[1] + offset, \
1210 2 * decoder->stride, 8); \
1211 table[xy_half] (decoder->dest[2] + dest_field * decoder->stride + \
1212 decoder->offset, ref[2] + offset, \
1213 2 * decoder->stride, 8)
1214
1215#define MOTION_DMV_444(table,ref,motion_x,motion_y) \
1216 pos_x = 2 * decoder->offset + motion_x; \
1217 pos_y = decoder->v_offset + motion_y; \
1218 if (unlikely (pos_x > decoder->limit_x)) { \
1219 pos_x = ((int)pos_x < 0) ? 0 : decoder->limit_x; \
1220 motion_x = pos_x - 2 * decoder->offset; \
1221 } \
1222 if (unlikely (pos_y > decoder->limit_y)) { \
1223 pos_y = ((int)pos_y < 0) ? 0 : decoder->limit_y; \
1224 motion_y = pos_y - decoder->v_offset; \
1225 } \
1226 xy_half = ((pos_y & 1) << 1) | (pos_x & 1); \
1227 offset = (pos_x >> 1) + (pos_y & ~1) * decoder->stride; \
1228 table[xy_half] (decoder->dest[0] + decoder->offset, \
1229 ref[0] + offset, 2 * decoder->stride, 8); \
1230 table[xy_half] (decoder->dest[0] + decoder->stride + decoder->offset, \
1231 ref[0] + decoder->stride + offset, \
1232 2 * decoder->stride, 8); \
1233 table[xy_half] (decoder->dest[1] + decoder->offset, \
1234 ref[1] + offset, 2 * decoder->stride, 8); \
1235 table[xy_half] (decoder->dest[1] + decoder->stride + decoder->offset, \
1236 ref[1] + decoder->stride + offset, \
1237 2 * decoder->stride, 8); \
1238 table[xy_half] (decoder->dest[2] + decoder->offset, \
1239 ref[2] + offset, 2 * decoder->stride, 8); \
1240 table[xy_half] (decoder->dest[2] + decoder->stride + decoder->offset, \
1241 ref[2] + decoder->stride + offset, \
1242 2 * decoder->stride, 8)
1243
1244#define MOTION_ZERO_444(table,ref) \
1245 offset = decoder->offset + decoder->v_offset * decoder->stride; \
1246 table[0] (decoder->dest[0] + decoder->offset, \
1247 ref[0] + offset, decoder->stride, 16); \
1248 table[4] (decoder->dest[1] + decoder->offset, \
1249 ref[1] + offset, decoder->stride, 16); \
1250 table[4] (decoder->dest[2] + (decoder->offset >> 1), \
1251 ref[2] + offset, decoder->stride, 16)
1252
1253#define bit_buf (decoder->bitstream_buf)
1254#define bits (decoder->bitstream_bits)
1255#define bit_ptr (decoder->bitstream_ptr)
1256
1257static void motion_mp1 (mpeg2_decoder_t * const decoder,
1258 motion_t * const motion,
1259 mpeg2_mc_fct * const * const table)
1260{
1261 int motion_x, motion_y;
1262 unsigned int pos_x, pos_y, xy_half, offset;
1263
1264 NEEDBITS (bit_buf, bits, bit_ptr);
1265 motion_x = (motion->pmv[0][0] +
1266 (get_motion_delta (decoder,
1267 motion->f_code[0]) << motion->f_code[1]));
1268 motion_x = bound_motion_vector (motion_x,
1269 motion->f_code[0] + motion->f_code[1]);
1270 motion->pmv[0][0] = motion_x;
1271
1272 NEEDBITS (bit_buf, bits, bit_ptr);
1273 motion_y = (motion->pmv[0][1] +
1274 (get_motion_delta (decoder,
1275 motion->f_code[0]) << motion->f_code[1]));
1276 motion_y = bound_motion_vector (motion_y,
1277 motion->f_code[0] + motion->f_code[1]);
1278 motion->pmv[0][1] = motion_y;
1279
1280 MOTION_420 (table, motion->ref[0], motion_x, motion_y, 16, 0);
1281}
1282
1283#define MOTION_FUNCTIONS(FORMAT,MOTION,MOTION_FIELD,MOTION_DMV,MOTION_ZERO) \
1284 \
1285static void motion_fr_frame_##FORMAT (mpeg2_decoder_t * const decoder, \
1286 motion_t * const motion, \
1287 mpeg2_mc_fct * const * const table) \
1288{ \
1289 int motion_x, motion_y; \
1290 unsigned int pos_x, pos_y, xy_half, offset; \
1291 \
1292 NEEDBITS (bit_buf, bits, bit_ptr); \
1293 motion_x = motion->pmv[0][0] + get_motion_delta (decoder, \
1294 motion->f_code[0]); \
1295 motion_x = bound_motion_vector (motion_x, motion->f_code[0]); \
1296 motion->pmv[1][0] = motion->pmv[0][0] = motion_x; \
1297 \
1298 NEEDBITS (bit_buf, bits, bit_ptr); \
1299 motion_y = motion->pmv[0][1] + get_motion_delta (decoder, \
1300 motion->f_code[1]); \
1301 motion_y = bound_motion_vector (motion_y, motion->f_code[1]); \
1302 motion->pmv[1][1] = motion->pmv[0][1] = motion_y; \
1303 \
1304 MOTION (table, motion->ref[0], motion_x, motion_y, 16, 0); \
1305} \
1306 \
1307static void motion_fr_field_##FORMAT (mpeg2_decoder_t * const decoder, \
1308 motion_t * const motion, \
1309 mpeg2_mc_fct * const * const table) \
1310{ \
1311 int motion_x, motion_y, field; \
1312 unsigned int pos_x, pos_y, xy_half, offset; \
1313 \
1314 NEEDBITS (bit_buf, bits, bit_ptr); \
1315 field = UBITS (bit_buf, 1); \
1316 DUMPBITS (bit_buf, bits, 1); \
1317 \
1318 motion_x = motion->pmv[0][0] + get_motion_delta (decoder, \
1319 motion->f_code[0]); \
1320 motion_x = bound_motion_vector (motion_x, motion->f_code[0]); \
1321 motion->pmv[0][0] = motion_x; \
1322 \
1323 NEEDBITS (bit_buf, bits, bit_ptr); \
1324 motion_y = ((motion->pmv[0][1] >> 1) + \
1325 get_motion_delta (decoder, motion->f_code[1])); \
1326 /* motion_y = bound_motion_vector (motion_y, motion->f_code[1]); */ \
1327 motion->pmv[0][1] = motion_y << 1; \
1328 \
1329 MOTION_FIELD (table, motion->ref[0], motion_x, motion_y, 0, & ~1, field); \
1330 \
1331 NEEDBITS (bit_buf, bits, bit_ptr); \
1332 field = UBITS (bit_buf, 1); \
1333 DUMPBITS (bit_buf, bits, 1); \
1334 \
1335 motion_x = motion->pmv[1][0] + get_motion_delta (decoder, \
1336 motion->f_code[0]); \
1337 motion_x = bound_motion_vector (motion_x, motion->f_code[0]); \
1338 motion->pmv[1][0] = motion_x; \
1339 \
1340 NEEDBITS (bit_buf, bits, bit_ptr); \
1341 motion_y = ((motion->pmv[1][1] >> 1) + \
1342 get_motion_delta (decoder, motion->f_code[1])); \
1343 /* motion_y = bound_motion_vector (motion_y, motion->f_code[1]); */ \
1344 motion->pmv[1][1] = motion_y << 1; \
1345 \
1346 MOTION_FIELD (table, motion->ref[0], motion_x, motion_y, 1, & ~1, field); \
1347} \
1348 \
1349static void motion_fr_dmv_##FORMAT (mpeg2_decoder_t * const decoder, \
1350 motion_t * const motion, \
1351 mpeg2_mc_fct * const * const table) \
1352{ \
1353 int motion_x, motion_y, dmv_x, dmv_y, m, other_x, other_y; \
1354 unsigned int pos_x, pos_y, xy_half, offset; \
1355 \
1356 (void)table; \
1357 NEEDBITS (bit_buf, bits, bit_ptr); \
1358 motion_x = motion->pmv[0][0] + get_motion_delta (decoder, \
1359 motion->f_code[0]); \
1360 motion_x = bound_motion_vector (motion_x, motion->f_code[0]); \
1361 motion->pmv[1][0] = motion->pmv[0][0] = motion_x; \
1362 NEEDBITS (bit_buf, bits, bit_ptr); \
1363 dmv_x = get_dmv (decoder); \
1364 \
1365 motion_y = ((motion->pmv[0][1] >> 1) + \
1366 get_motion_delta (decoder, motion->f_code[1])); \
1367 /* motion_y = bound_motion_vector (motion_y, motion->f_code[1]); */ \
1368 motion->pmv[1][1] = motion->pmv[0][1] = motion_y << 1; \
1369 dmv_y = get_dmv (decoder); \
1370 \
1371 m = decoder->top_field_first ? 1 : 3; \
1372 other_x = ((motion_x * m + (motion_x > 0)) >> 1) + dmv_x; \
1373 other_y = ((motion_y * m + (motion_y > 0)) >> 1) + dmv_y - 1; \
1374 MOTION_FIELD (mpeg2_mc.put, motion->ref[0], other_x, other_y, 0, | 1, 0); \
1375 \
1376 m = decoder->top_field_first ? 3 : 1; \
1377 other_x = ((motion_x * m + (motion_x > 0)) >> 1) + dmv_x; \
1378 other_y = ((motion_y * m + (motion_y > 0)) >> 1) + dmv_y + 1; \
1379 MOTION_FIELD (mpeg2_mc.put, motion->ref[0], other_x, other_y, 1, & ~1, 0);\
1380 \
1381 MOTION_DMV (mpeg2_mc.avg, motion->ref[0], motion_x, motion_y); \
1382} \
1383 \
1384static void motion_reuse_##FORMAT (mpeg2_decoder_t * const decoder, \
1385 motion_t * const motion, \
1386 mpeg2_mc_fct * const * const table) \
1387{ \
1388 int motion_x, motion_y; \
1389 unsigned int pos_x, pos_y, xy_half, offset; \
1390 \
1391 motion_x = motion->pmv[0][0]; \
1392 motion_y = motion->pmv[0][1]; \
1393 \
1394 MOTION (table, motion->ref[0], motion_x, motion_y, 16, 0); \
1395} \
1396 \
1397static void motion_zero_##FORMAT (mpeg2_decoder_t * const decoder, \
1398 motion_t * const motion, \
1399 mpeg2_mc_fct * const * const table) \
1400{ \
1401 unsigned int offset; \
1402 \
1403 motion->pmv[0][0] = motion->pmv[0][1] = 0; \
1404 motion->pmv[1][0] = motion->pmv[1][1] = 0; \
1405 \
1406 MOTION_ZERO (table, motion->ref[0]); \
1407} \
1408 \
1409static void motion_fi_field_##FORMAT (mpeg2_decoder_t * const decoder, \
1410 motion_t * const motion, \
1411 mpeg2_mc_fct * const * const table) \
1412{ \
1413 int motion_x, motion_y; \
1414 uint8_t ** ref_field; \
1415 unsigned int pos_x, pos_y, xy_half, offset; \
1416 \
1417 NEEDBITS (bit_buf, bits, bit_ptr); \
1418 ref_field = motion->ref2[UBITS (bit_buf, 1)]; \
1419 DUMPBITS (bit_buf, bits, 1); \
1420 \
1421 motion_x = motion->pmv[0][0] + get_motion_delta (decoder, \
1422 motion->f_code[0]); \
1423 motion_x = bound_motion_vector (motion_x, motion->f_code[0]); \
1424 motion->pmv[1][0] = motion->pmv[0][0] = motion_x; \
1425 \
1426 NEEDBITS (bit_buf, bits, bit_ptr); \
1427 motion_y = motion->pmv[0][1] + get_motion_delta (decoder, \
1428 motion->f_code[1]); \
1429 motion_y = bound_motion_vector (motion_y, motion->f_code[1]); \
1430 motion->pmv[1][1] = motion->pmv[0][1] = motion_y; \
1431 \
1432 MOTION (table, ref_field, motion_x, motion_y, 16, 0); \
1433} \
1434 \
1435static void motion_fi_16x8_##FORMAT (mpeg2_decoder_t * const decoder, \
1436 motion_t * const motion, \
1437 mpeg2_mc_fct * const * const table) \
1438{ \
1439 int motion_x, motion_y; \
1440 uint8_t ** ref_field; \
1441 unsigned int pos_x, pos_y, xy_half, offset; \
1442 \
1443 NEEDBITS (bit_buf, bits, bit_ptr); \
1444 ref_field = motion->ref2[UBITS (bit_buf, 1)]; \
1445 DUMPBITS (bit_buf, bits, 1); \
1446 \
1447 motion_x = motion->pmv[0][0] + get_motion_delta (decoder, \
1448 motion->f_code[0]); \
1449 motion_x = bound_motion_vector (motion_x, motion->f_code[0]); \
1450 motion->pmv[0][0] = motion_x; \
1451 \
1452 NEEDBITS (bit_buf, bits, bit_ptr); \
1453 motion_y = motion->pmv[0][1] + get_motion_delta (decoder, \
1454 motion->f_code[1]); \
1455 motion_y = bound_motion_vector (motion_y, motion->f_code[1]); \
1456 motion->pmv[0][1] = motion_y; \
1457 \
1458 MOTION (table, ref_field, motion_x, motion_y, 8, 0); \
1459 \
1460 NEEDBITS (bit_buf, bits, bit_ptr); \
1461 ref_field = motion->ref2[UBITS (bit_buf, 1)]; \
1462 DUMPBITS (bit_buf, bits, 1); \
1463 \
1464 motion_x = motion->pmv[1][0] + get_motion_delta (decoder, \
1465 motion->f_code[0]); \
1466 motion_x = bound_motion_vector (motion_x, motion->f_code[0]); \
1467 motion->pmv[1][0] = motion_x; \
1468 \
1469 NEEDBITS (bit_buf, bits, bit_ptr); \
1470 motion_y = motion->pmv[1][1] + get_motion_delta (decoder, \
1471 motion->f_code[1]); \
1472 motion_y = bound_motion_vector (motion_y, motion->f_code[1]); \
1473 motion->pmv[1][1] = motion_y; \
1474 \
1475 MOTION (table, ref_field, motion_x, motion_y, 8, 8); \
1476} \
1477 \
1478static void motion_fi_dmv_##FORMAT (mpeg2_decoder_t * const decoder, \
1479 motion_t * const motion, \
1480 mpeg2_mc_fct * const * const table) \
1481{ \
1482 int motion_x, motion_y, other_x, other_y; \
1483 unsigned int pos_x, pos_y, xy_half, offset; \
1484 \
1485 (void)table; \
1486 NEEDBITS (bit_buf, bits, bit_ptr); \
1487 motion_x = motion->pmv[0][0] + get_motion_delta (decoder, \
1488 motion->f_code[0]); \
1489 motion_x = bound_motion_vector (motion_x, motion->f_code[0]); \
1490 motion->pmv[1][0] = motion->pmv[0][0] = motion_x; \
1491 NEEDBITS (bit_buf, bits, bit_ptr); \
1492 other_x = ((motion_x + (motion_x > 0)) >> 1) + get_dmv (decoder); \
1493 \
1494 motion_y = motion->pmv[0][1] + get_motion_delta (decoder, \
1495 motion->f_code[1]); \
1496 motion_y = bound_motion_vector (motion_y, motion->f_code[1]); \
1497 motion->pmv[1][1] = motion->pmv[0][1] = motion_y; \
1498 other_y = (((motion_y + (motion_y > 0)) >> 1) + get_dmv (decoder) + \
1499 decoder->dmv_offset); \
1500 \
1501 MOTION (mpeg2_mc.put, motion->ref[0], motion_x, motion_y, 16, 0); \
1502 MOTION (mpeg2_mc.avg, motion->ref[1], other_x, other_y, 16, 0); \
1503} \
1504
1505MOTION_FUNCTIONS (420, MOTION_420, MOTION_FIELD_420, MOTION_DMV_420,
1506 MOTION_ZERO_420)
1507MOTION_FUNCTIONS (422, MOTION_422, MOTION_FIELD_422, MOTION_DMV_422,
1508 MOTION_ZERO_422)
1509MOTION_FUNCTIONS (444, MOTION_444, MOTION_FIELD_444, MOTION_DMV_444,
1510 MOTION_ZERO_444)
1511
1512/* like motion_frame, but parsing without actual motion compensation */
1513static void motion_fr_conceal (mpeg2_decoder_t * const decoder)
1514{
1515 int tmp;
1516
1517 NEEDBITS (bit_buf, bits, bit_ptr);
1518 tmp = (decoder->f_motion.pmv[0][0] +
1519 get_motion_delta (decoder, decoder->f_motion.f_code[0]));
1520 tmp = bound_motion_vector (tmp, decoder->f_motion.f_code[0]);
1521 decoder->f_motion.pmv[1][0] = decoder->f_motion.pmv[0][0] = tmp;
1522
1523 NEEDBITS (bit_buf, bits, bit_ptr);
1524 tmp = (decoder->f_motion.pmv[0][1] +
1525 get_motion_delta (decoder, decoder->f_motion.f_code[1]));
1526 tmp = bound_motion_vector (tmp, decoder->f_motion.f_code[1]);
1527 decoder->f_motion.pmv[1][1] = decoder->f_motion.pmv[0][1] = tmp;
1528
1529 DUMPBITS (bit_buf, bits, 1); /* remove marker_bit */
1530}
1531
1532static void motion_fi_conceal (mpeg2_decoder_t * const decoder)
1533{
1534 int tmp;
1535
1536 NEEDBITS (bit_buf, bits, bit_ptr);
1537 DUMPBITS (bit_buf, bits, 1); /* remove field_select */
1538
1539 tmp = (decoder->f_motion.pmv[0][0] +
1540 get_motion_delta (decoder, decoder->f_motion.f_code[0]));
1541 tmp = bound_motion_vector (tmp, decoder->f_motion.f_code[0]);
1542 decoder->f_motion.pmv[1][0] = decoder->f_motion.pmv[0][0] = tmp;
1543
1544 NEEDBITS (bit_buf, bits, bit_ptr);
1545 tmp = (decoder->f_motion.pmv[0][1] +
1546 get_motion_delta (decoder, decoder->f_motion.f_code[1]));
1547 tmp = bound_motion_vector (tmp, decoder->f_motion.f_code[1]);
1548 decoder->f_motion.pmv[1][1] = decoder->f_motion.pmv[0][1] = tmp;
1549
1550 DUMPBITS (bit_buf, bits, 1); /* remove marker_bit */
1551}
1552
1553#undef bit_buf
1554#undef bits
1555#undef bit_ptr
1556
1557#define MOTION_CALL(routine,direction) \
1558do { \
1559 if ((direction) & MACROBLOCK_MOTION_FORWARD) \
1560 routine (decoder, &(decoder->f_motion), mpeg2_mc.put); \
1561 if ((direction) & MACROBLOCK_MOTION_BACKWARD) \
1562 routine (decoder, &(decoder->b_motion), \
1563 ((direction) & MACROBLOCK_MOTION_FORWARD ? \
1564 mpeg2_mc.avg : mpeg2_mc.put)); \
1565} while (0)
1566
1567#define NEXT_MACROBLOCK \
1568do { \
1569 decoder->offset += 16; \
1570 if (decoder->offset == decoder->width) { \
1571 do { /* just so we can use the break statement */ \
1572 if (decoder->convert) { \
1573 decoder->convert (decoder->convert_id, decoder->dest, \
1574 decoder->v_offset); \
1575 if (decoder->coding_type == B_TYPE) \
1576 break; \
1577 } \
1578 decoder->dest[0] += decoder->slice_stride; \
1579 decoder->dest[1] += decoder->slice_uv_stride; \
1580 decoder->dest[2] += decoder->slice_uv_stride; \
1581 } while (0); \
1582 decoder->v_offset += 16; \
1583 if (decoder->v_offset > decoder->limit_y) { \
1584 if (mpeg2_cpu_state_restore) \
1585 mpeg2_cpu_state_restore (&cpu_state); \
1586 return; \
1587 } \
1588 decoder->offset = 0; \
1589 } \
1590} while (0)
1591
1592void mpeg2_init_fbuf (mpeg2_decoder_t * decoder, uint8_t * current_fbuf[3],
1593 uint8_t * forward_fbuf[3], uint8_t * backward_fbuf[3])
1594{
1595 int offset, stride, height, bottom_field;
1596
1597 stride = decoder->stride_frame;
1598 bottom_field = (decoder->picture_structure == BOTTOM_FIELD);
1599 offset = bottom_field ? stride : 0;
1600 height = decoder->height;
1601
1602 decoder->picture_dest[0] = current_fbuf[0] + offset;
1603 decoder->picture_dest[1] = current_fbuf[1] + (offset >> 1);
1604 decoder->picture_dest[2] = current_fbuf[2] + (offset >> 1);
1605
1606 decoder->f_motion.ref[0][0] = forward_fbuf[0] + offset;
1607 decoder->f_motion.ref[0][1] = forward_fbuf[1] + (offset >> 1);
1608 decoder->f_motion.ref[0][2] = forward_fbuf[2] + (offset >> 1);
1609
1610 decoder->b_motion.ref[0][0] = backward_fbuf[0] + offset;
1611 decoder->b_motion.ref[0][1] = backward_fbuf[1] + (offset >> 1);
1612 decoder->b_motion.ref[0][2] = backward_fbuf[2] + (offset >> 1);
1613
1614 if (decoder->picture_structure != FRAME_PICTURE) {
1615 decoder->dmv_offset = bottom_field ? 1 : -1;
1616 decoder->f_motion.ref2[0] = decoder->f_motion.ref[bottom_field];
1617 decoder->f_motion.ref2[1] = decoder->f_motion.ref[!bottom_field];
1618 decoder->b_motion.ref2[0] = decoder->b_motion.ref[bottom_field];
1619 decoder->b_motion.ref2[1] = decoder->b_motion.ref[!bottom_field];
1620 offset = stride - offset;
1621
1622 if (decoder->second_field && (decoder->coding_type != B_TYPE))
1623 forward_fbuf = current_fbuf;
1624
1625 decoder->f_motion.ref[1][0] = forward_fbuf[0] + offset;
1626 decoder->f_motion.ref[1][1] = forward_fbuf[1] + (offset >> 1);
1627 decoder->f_motion.ref[1][2] = forward_fbuf[2] + (offset >> 1);
1628
1629 decoder->b_motion.ref[1][0] = backward_fbuf[0] + offset;
1630 decoder->b_motion.ref[1][1] = backward_fbuf[1] + (offset >> 1);
1631 decoder->b_motion.ref[1][2] = backward_fbuf[2] + (offset >> 1);
1632
1633 stride <<= 1;
1634 height >>= 1;
1635 }
1636
1637 decoder->stride = stride;
1638 decoder->uv_stride = stride >> 1;
1639 decoder->slice_stride = 16 * stride;
1640 decoder->slice_uv_stride =
1641 decoder->slice_stride >> (2 - decoder->chroma_format);
1642 decoder->limit_x = 2 * decoder->width - 32;
1643 decoder->limit_y_16 = 2 * height - 32;
1644 decoder->limit_y_8 = 2 * height - 16;
1645 decoder->limit_y = height - 16;
1646
1647 if (decoder->mpeg1) {
1648 decoder->motion_parser[0] = motion_zero_420;
1649 decoder->motion_parser[MC_FRAME] = motion_mp1;
1650 decoder->motion_parser[4] = motion_reuse_420;
1651 } else if (decoder->picture_structure == FRAME_PICTURE) {
1652 if (decoder->chroma_format == 0) {
1653 decoder->motion_parser[0] = motion_zero_420;
1654 decoder->motion_parser[MC_FIELD] = motion_fr_field_420;
1655 decoder->motion_parser[MC_FRAME] = motion_fr_frame_420;
1656 decoder->motion_parser[MC_DMV] = motion_fr_dmv_420;
1657 decoder->motion_parser[4] = motion_reuse_420;
1658 } else if (decoder->chroma_format == 1) {
1659 decoder->motion_parser[0] = motion_zero_422;
1660 decoder->motion_parser[MC_FIELD] = motion_fr_field_422;
1661 decoder->motion_parser[MC_FRAME] = motion_fr_frame_422;
1662 decoder->motion_parser[MC_DMV] = motion_fr_dmv_422;
1663 decoder->motion_parser[4] = motion_reuse_422;
1664 } else {
1665 decoder->motion_parser[0] = motion_zero_444;
1666 decoder->motion_parser[MC_FIELD] = motion_fr_field_444;
1667 decoder->motion_parser[MC_FRAME] = motion_fr_frame_444;
1668 decoder->motion_parser[MC_DMV] = motion_fr_dmv_444;
1669 decoder->motion_parser[4] = motion_reuse_444;
1670 }
1671 } else {
1672 if (decoder->chroma_format == 0) {
1673 decoder->motion_parser[0] = motion_zero_420;
1674 decoder->motion_parser[MC_FIELD] = motion_fi_field_420;
1675 decoder->motion_parser[MC_16X8] = motion_fi_16x8_420;
1676 decoder->motion_parser[MC_DMV] = motion_fi_dmv_420;
1677 decoder->motion_parser[4] = motion_reuse_420;
1678 } else if (decoder->chroma_format == 1) {
1679 decoder->motion_parser[0] = motion_zero_422;
1680 decoder->motion_parser[MC_FIELD] = motion_fi_field_422;
1681 decoder->motion_parser[MC_16X8] = motion_fi_16x8_422;
1682 decoder->motion_parser[MC_DMV] = motion_fi_dmv_422;
1683 decoder->motion_parser[4] = motion_reuse_422;
1684 } else {
1685 decoder->motion_parser[0] = motion_zero_444;
1686 decoder->motion_parser[MC_FIELD] = motion_fi_field_444;
1687 decoder->motion_parser[MC_16X8] = motion_fi_16x8_444;
1688 decoder->motion_parser[MC_DMV] = motion_fi_dmv_444;
1689 decoder->motion_parser[4] = motion_reuse_444;
1690 }
1691 }
1692}
1693
1694static inline int slice_init (mpeg2_decoder_t * const decoder, int code)
1695{
1696#define bit_buf (decoder->bitstream_buf)
1697#define bits (decoder->bitstream_bits)
1698#define bit_ptr (decoder->bitstream_ptr)
1699 int offset;
1700 const MBAtab * mba;
1701
1702 decoder->dc_dct_pred[0] = decoder->dc_dct_pred[1] =
1703 decoder->dc_dct_pred[2] = 16384;
1704
1705 decoder->f_motion.pmv[0][0] = decoder->f_motion.pmv[0][1] = 0;
1706 decoder->f_motion.pmv[1][0] = decoder->f_motion.pmv[1][1] = 0;
1707 decoder->b_motion.pmv[0][0] = decoder->b_motion.pmv[0][1] = 0;
1708 decoder->b_motion.pmv[1][0] = decoder->b_motion.pmv[1][1] = 0;
1709
1710 if (decoder->vertical_position_extension) {
1711 code += UBITS (bit_buf, 3) << 7;
1712 DUMPBITS (bit_buf, bits, 3);
1713 }
1714 decoder->v_offset = (code - 1) * 16;
1715 offset = 0;
1716 if (!(decoder->convert) || decoder->coding_type != B_TYPE)
1717 offset = (code - 1) * decoder->slice_stride;
1718
1719 decoder->dest[0] = decoder->picture_dest[0] + offset;
1720 offset >>= (2 - decoder->chroma_format);
1721 decoder->dest[1] = decoder->picture_dest[1] + offset;
1722 decoder->dest[2] = decoder->picture_dest[2] + offset;
1723
1724 get_quantizer_scale (decoder);
1725
1726 /* ignore intra_slice and all the extra data */
1727 while (bit_buf & 0x80000000) {
1728 DUMPBITS (bit_buf, bits, 9);
1729 NEEDBITS (bit_buf, bits, bit_ptr);
1730 }
1731
1732 /* decode initial macroblock address increment */
1733 offset = 0;
1734 while (1) {
1735 if (bit_buf >= 0x08000000) {
1736 mba = MBA_5 + (UBITS (bit_buf, 6) - 2);
1737 break;
1738 } else if (bit_buf >= 0x01800000) {
1739 mba = MBA_11 + (UBITS (bit_buf, 12) - 24);
1740 break;
1741 } else switch (UBITS (bit_buf, 12)) {
1742 case 8: /* macroblock_escape */
1743 offset += 33;
1744 DUMPBITS (bit_buf, bits, 11);
1745 NEEDBITS (bit_buf, bits, bit_ptr);
1746 continue;
1747 case 15: /* macroblock_stuffing (MPEG1 only) */
1748 bit_buf &= 0xfffff;
1749 DUMPBITS (bit_buf, bits, 11);
1750 NEEDBITS (bit_buf, bits, bit_ptr);
1751 continue;
1752 default: /* error */
1753 return 1;
1754 }
1755 }
1756 DUMPBITS (bit_buf, bits, mba->len + 1);
1757 decoder->offset = (offset + mba->mba) << 4;
1758
1759 while (decoder->offset - decoder->width >= 0) {
1760 decoder->offset -= decoder->width;
1761 if (!(decoder->convert) || decoder->coding_type != B_TYPE) {
1762 decoder->dest[0] += decoder->slice_stride;
1763 decoder->dest[1] += decoder->slice_uv_stride;
1764 decoder->dest[2] += decoder->slice_uv_stride;
1765 }
1766 decoder->v_offset += 16;
1767 }
1768 if (decoder->v_offset > decoder->limit_y)
1769 return 1;
1770
1771 return 0;
1772#undef bit_buf
1773#undef bits
1774#undef bit_ptr
1775}
1776
1777void mpeg2_slice (mpeg2_decoder_t * const decoder, const int code,
1778 const uint8_t * const buffer)
1779{
1780#define bit_buf (decoder->bitstream_buf)
1781#define bits (decoder->bitstream_bits)
1782#define bit_ptr (decoder->bitstream_ptr)
1783 cpu_state_t cpu_state;
1784
1785 bitstream_init (decoder, buffer);
1786
1787 if (slice_init (decoder, code))
1788 return;
1789
1790 if (mpeg2_cpu_state_save)
1791 mpeg2_cpu_state_save (&cpu_state);
1792
1793 while (1) {
1794 int macroblock_modes;
1795 int mba_inc;
1796 const MBAtab * mba;
1797
1798 NEEDBITS (bit_buf, bits, bit_ptr);
1799
1800 macroblock_modes = get_macroblock_modes (decoder);
1801
1802 /* maybe integrate MACROBLOCK_QUANT test into get_macroblock_modes ? */
1803 if (macroblock_modes & MACROBLOCK_QUANT)
1804 get_quantizer_scale (decoder);
1805
1806 if (macroblock_modes & MACROBLOCK_INTRA) {
1807
1808 int DCT_offset, DCT_stride;
1809 int offset;
1810 uint8_t * dest_y;
1811
1812 if (decoder->concealment_motion_vectors) {
1813 if (decoder->picture_structure == FRAME_PICTURE)
1814 motion_fr_conceal (decoder);
1815 else
1816 motion_fi_conceal (decoder);
1817 } else {
1818 decoder->f_motion.pmv[0][0] = decoder->f_motion.pmv[0][1] = 0;
1819 decoder->f_motion.pmv[1][0] = decoder->f_motion.pmv[1][1] = 0;
1820 decoder->b_motion.pmv[0][0] = decoder->b_motion.pmv[0][1] = 0;
1821 decoder->b_motion.pmv[1][0] = decoder->b_motion.pmv[1][1] = 0;
1822 }
1823
1824 if (macroblock_modes & DCT_TYPE_INTERLACED) {
1825 DCT_offset = decoder->stride;
1826 DCT_stride = decoder->stride * 2;
1827 } else {
1828 DCT_offset = decoder->stride * 8;
1829 DCT_stride = decoder->stride;
1830 }
1831
1832 offset = decoder->offset;
1833 dest_y = decoder->dest[0] + offset;
1834 slice_intra_DCT (decoder, 0, dest_y, DCT_stride);
1835 slice_intra_DCT (decoder, 0, dest_y + 8, DCT_stride);
1836 slice_intra_DCT (decoder, 0, dest_y + DCT_offset, DCT_stride);
1837 slice_intra_DCT (decoder, 0, dest_y + DCT_offset + 8, DCT_stride);
1838 if (likely (decoder->chroma_format == 0)) {
1839 slice_intra_DCT (decoder, 1, decoder->dest[1] + (offset >> 1),
1840 decoder->uv_stride);
1841 slice_intra_DCT (decoder, 2, decoder->dest[2] + (offset >> 1),
1842 decoder->uv_stride);
1843 if (decoder->coding_type == D_TYPE) {
1844 NEEDBITS (bit_buf, bits, bit_ptr);
1845 DUMPBITS (bit_buf, bits, 1);
1846 }
1847 } else if (likely (decoder->chroma_format == 1)) {
1848 uint8_t * dest_u = decoder->dest[1] + (offset >> 1);
1849 uint8_t * dest_v = decoder->dest[2] + (offset >> 1);
1850 DCT_stride >>= 1;
1851 DCT_offset >>= 1;
1852 slice_intra_DCT (decoder, 1, dest_u, DCT_stride);
1853 slice_intra_DCT (decoder, 2, dest_v, DCT_stride);
1854 slice_intra_DCT (decoder, 1, dest_u + DCT_offset, DCT_stride);
1855 slice_intra_DCT (decoder, 2, dest_v + DCT_offset, DCT_stride);
1856 } else {
1857 uint8_t * dest_u = decoder->dest[1] + offset;
1858 uint8_t * dest_v = decoder->dest[2] + offset;
1859 slice_intra_DCT (decoder, 1, dest_u, DCT_stride);
1860 slice_intra_DCT (decoder, 2, dest_v, DCT_stride);
1861 slice_intra_DCT (decoder, 1, dest_u + DCT_offset, DCT_stride);
1862 slice_intra_DCT (decoder, 2, dest_v + DCT_offset, DCT_stride);
1863 slice_intra_DCT (decoder, 1, dest_u + 8, DCT_stride);
1864 slice_intra_DCT (decoder, 2, dest_v + 8, DCT_stride);
1865 slice_intra_DCT (decoder, 1, dest_u + DCT_offset + 8,
1866 DCT_stride);
1867 slice_intra_DCT (decoder, 2, dest_v + DCT_offset + 8,
1868 DCT_stride);
1869 }
1870 } else {
1871
1872 motion_parser_t * parser;
1873
1874 parser =
1875 decoder->motion_parser[macroblock_modes >> MOTION_TYPE_SHIFT];
1876 MOTION_CALL (parser, macroblock_modes);
1877
1878 if (macroblock_modes & MACROBLOCK_PATTERN) {
1879 int coded_block_pattern;
1880 int DCT_offset, DCT_stride;
1881
1882 if (macroblock_modes & DCT_TYPE_INTERLACED) {
1883 DCT_offset = decoder->stride;
1884 DCT_stride = decoder->stride * 2;
1885 } else {
1886 DCT_offset = decoder->stride * 8;
1887 DCT_stride = decoder->stride;
1888 }
1889
1890 coded_block_pattern = get_coded_block_pattern (decoder);
1891
1892 if (likely (decoder->chroma_format == 0)) {
1893 int offset = decoder->offset;
1894 uint8_t * dest_y = decoder->dest[0] + offset;
1895 if (coded_block_pattern & 1)
1896 slice_non_intra_DCT (decoder, 0, dest_y, DCT_stride);
1897 if (coded_block_pattern & 2)
1898 slice_non_intra_DCT (decoder, 0, dest_y + 8,
1899 DCT_stride);
1900 if (coded_block_pattern & 4)
1901 slice_non_intra_DCT (decoder, 0, dest_y + DCT_offset,
1902 DCT_stride);
1903 if (coded_block_pattern & 8)
1904 slice_non_intra_DCT (decoder, 0,
1905 dest_y + DCT_offset + 8,
1906 DCT_stride);
1907 if (coded_block_pattern & 16)
1908 slice_non_intra_DCT (decoder, 1,
1909 decoder->dest[1] + (offset >> 1),
1910 decoder->uv_stride);
1911 if (coded_block_pattern & 32)
1912 slice_non_intra_DCT (decoder, 2,
1913 decoder->dest[2] + (offset >> 1),
1914 decoder->uv_stride);
1915 } else if (likely (decoder->chroma_format == 1)) {
1916 int offset;
1917 uint8_t * dest_y;
1918
1919 coded_block_pattern |= bit_buf & (3 << 30);
1920 DUMPBITS (bit_buf, bits, 2);
1921
1922 offset = decoder->offset;
1923 dest_y = decoder->dest[0] + offset;
1924 if (coded_block_pattern & 1)
1925 slice_non_intra_DCT (decoder, 0, dest_y, DCT_stride);
1926 if (coded_block_pattern & 2)
1927 slice_non_intra_DCT (decoder, 0, dest_y + 8,
1928 DCT_stride);
1929 if (coded_block_pattern & 4)
1930 slice_non_intra_DCT (decoder, 0, dest_y + DCT_offset,
1931 DCT_stride);
1932 if (coded_block_pattern & 8)
1933 slice_non_intra_DCT (decoder, 0,
1934 dest_y + DCT_offset + 8,
1935 DCT_stride);
1936
1937 DCT_stride >>= 1;
1938 DCT_offset = (DCT_offset + offset) >> 1;
1939 if (coded_block_pattern & 16)
1940 slice_non_intra_DCT (decoder, 1,
1941 decoder->dest[1] + (offset >> 1),
1942 DCT_stride);
1943 if (coded_block_pattern & 32)
1944 slice_non_intra_DCT (decoder, 2,
1945 decoder->dest[2] + (offset >> 1),
1946 DCT_stride);
1947 if (coded_block_pattern & (2 << 30))
1948 slice_non_intra_DCT (decoder, 1,
1949 decoder->dest[1] + DCT_offset,
1950 DCT_stride);
1951 if (coded_block_pattern & (1 << 30))
1952 slice_non_intra_DCT (decoder, 2,
1953 decoder->dest[2] + DCT_offset,
1954 DCT_stride);
1955 } else {
1956 int offset;
1957 uint8_t * dest_y, * dest_u, * dest_v;
1958
1959 coded_block_pattern |= bit_buf & (63 << 26);
1960 DUMPBITS (bit_buf, bits, 6);
1961
1962 offset = decoder->offset;
1963 dest_y = decoder->dest[0] + offset;
1964 dest_u = decoder->dest[1] + offset;
1965 dest_v = decoder->dest[2] + offset;
1966
1967 if (coded_block_pattern & 1)
1968 slice_non_intra_DCT (decoder, 0, dest_y, DCT_stride);
1969 if (coded_block_pattern & 2)
1970 slice_non_intra_DCT (decoder, 0, dest_y + 8,
1971 DCT_stride);
1972 if (coded_block_pattern & 4)
1973 slice_non_intra_DCT (decoder, 0, dest_y + DCT_offset,
1974 DCT_stride);
1975 if (coded_block_pattern & 8)
1976 slice_non_intra_DCT (decoder, 0,
1977 dest_y + DCT_offset + 8,
1978 DCT_stride);
1979
1980 if (coded_block_pattern & 16)
1981 slice_non_intra_DCT (decoder, 1, dest_u, DCT_stride);
1982 if (coded_block_pattern & 32)
1983 slice_non_intra_DCT (decoder, 2, dest_v, DCT_stride);
1984 if (coded_block_pattern & (32 << 26))
1985 slice_non_intra_DCT (decoder, 1, dest_u + DCT_offset,
1986 DCT_stride);
1987 if (coded_block_pattern & (16 << 26))
1988 slice_non_intra_DCT (decoder, 2, dest_v + DCT_offset,
1989 DCT_stride);
1990 if (coded_block_pattern & (8 << 26))
1991 slice_non_intra_DCT (decoder, 1, dest_u + 8,
1992 DCT_stride);
1993 if (coded_block_pattern & (4 << 26))
1994 slice_non_intra_DCT (decoder, 2, dest_v + 8,
1995 DCT_stride);
1996 if (coded_block_pattern & (2 << 26))
1997 slice_non_intra_DCT (decoder, 1,
1998 dest_u + DCT_offset + 8,
1999 DCT_stride);
2000 if (coded_block_pattern & (1 << 26))
2001 slice_non_intra_DCT (decoder, 2,
2002 dest_v + DCT_offset + 8,
2003 DCT_stride);
2004 }
2005 }
2006
2007 decoder->dc_dct_pred[0] = decoder->dc_dct_pred[1] =
2008 decoder->dc_dct_pred[2] = 16384;
2009 }
2010
2011 NEXT_MACROBLOCK;
2012
2013 NEEDBITS (bit_buf, bits, bit_ptr);
2014 mba_inc = 0;
2015 while (1) {
2016 if (bit_buf >= 0x10000000) {
2017 mba = MBA_5 + (UBITS (bit_buf, 5) - 2);
2018 break;
2019 } else if (bit_buf >= 0x03000000) {
2020 mba = MBA_11 + (UBITS (bit_buf, 11) - 24);
2021 break;
2022 } else switch (UBITS (bit_buf, 11)) {
2023 case 8: /* macroblock_escape */
2024 mba_inc += 33;
2025 /* pass through */
2026 case 15: /* macroblock_stuffing (MPEG1 only) */
2027 DUMPBITS (bit_buf, bits, 11);
2028 NEEDBITS (bit_buf, bits, bit_ptr);
2029 continue;
2030 default: /* end of slice, or error */
2031 if (mpeg2_cpu_state_restore)
2032 mpeg2_cpu_state_restore (&cpu_state);
2033 return;
2034 }
2035 }
2036 DUMPBITS (bit_buf, bits, mba->len);
2037 mba_inc += mba->mba;
2038
2039 if (mba_inc) {
2040 decoder->dc_dct_pred[0] = decoder->dc_dct_pred[1] =
2041 decoder->dc_dct_pred[2] = 16384;
2042
2043 if (decoder->coding_type == P_TYPE) {
2044 do {
2045 MOTION_CALL (decoder->motion_parser[0],
2046 MACROBLOCK_MOTION_FORWARD);
2047 NEXT_MACROBLOCK;
2048 } while (--mba_inc);
2049 } else {
2050 do {
2051 MOTION_CALL (decoder->motion_parser[4], macroblock_modes);
2052 NEXT_MACROBLOCK;
2053 } while (--mba_inc);
2054 }
2055 }
2056 }
2057#undef bit_buf
2058#undef bits
2059#undef bit_ptr
2060}