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