A modern Music Player Daemon based on Rockbox open source high quality audio player
libadwaita audio rust zig deno mpris rockbox mpd
at master 2898 lines 116 kB view raw
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 * $Id$ 25 * libmpeg2 sync history: 26 * 2008-07-01 - CVS revision 1.55 27 */ 28 29#include "plugin.h" 30 31#include "mpeg2dec_config.h" 32 33#include "mpeg2.h" 34#include "attributes.h" 35#include "mpeg2_internal.h" 36 37#include "vlc.h" 38 39static inline int get_macroblock_modes (mpeg2_decoder_t * const decoder) 40{ 41#define bit_buf (decoder->bitstream_buf) 42#define bits (decoder->bitstream_bits) 43#define bit_ptr (decoder->bitstream_ptr) 44 45 int macroblock_modes; 46 const MBtab * tab; 47 48 switch (decoder->coding_type) 49 { 50 case I_TYPE: 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 { 58 macroblock_modes |= UBITS (bit_buf, 1) * DCT_TYPE_INTERLACED; 59 DUMPBITS (bit_buf, bits, 1); 60 } 61 62 return macroblock_modes; 63 64 case P_TYPE: 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 { 71 if (macroblock_modes & MACROBLOCK_MOTION_FORWARD) 72 { 73 macroblock_modes |= UBITS (bit_buf, 2) << MOTION_TYPE_SHIFT; 74 DUMPBITS (bit_buf, bits, 2); 75 } 76 77 return macroblock_modes | MACROBLOCK_MOTION_FORWARD; 78 } 79 else if (decoder->frame_pred_frame_dct) 80 { 81 if (macroblock_modes & MACROBLOCK_MOTION_FORWARD) 82 macroblock_modes |= MC_FRAME << MOTION_TYPE_SHIFT; 83 84 return macroblock_modes | MACROBLOCK_MOTION_FORWARD; 85 } 86 else 87 { 88 if (macroblock_modes & MACROBLOCK_MOTION_FORWARD) 89 { 90 macroblock_modes |= UBITS (bit_buf, 2) << MOTION_TYPE_SHIFT; 91 DUMPBITS (bit_buf, bits, 2); 92 } 93 94 if (macroblock_modes & (MACROBLOCK_INTRA | MACROBLOCK_PATTERN)) 95 { 96 macroblock_modes |= UBITS (bit_buf, 1) * DCT_TYPE_INTERLACED; 97 DUMPBITS (bit_buf, bits, 1); 98 } 99 100 return macroblock_modes | MACROBLOCK_MOTION_FORWARD; 101 } 102 103 case B_TYPE: 104 tab = MB_B + UBITS (bit_buf, 6); 105 DUMPBITS (bit_buf, bits, tab->len); 106 macroblock_modes = tab->modes; 107 108 if (decoder->picture_structure != FRAME_PICTURE) 109 { 110 if (! (macroblock_modes & MACROBLOCK_INTRA)) 111 { 112 macroblock_modes |= UBITS (bit_buf, 2) << MOTION_TYPE_SHIFT; 113 DUMPBITS (bit_buf, bits, 2); 114 } 115 116 return macroblock_modes; 117 } 118 else if (decoder->frame_pred_frame_dct) 119 { 120 /* if (! (macroblock_modes & MACROBLOCK_INTRA)) */ 121 macroblock_modes |= MC_FRAME << MOTION_TYPE_SHIFT; 122 return macroblock_modes; 123 } 124 else 125 { 126 if (macroblock_modes & MACROBLOCK_INTRA) 127 goto intra; 128 129 macroblock_modes |= UBITS (bit_buf, 2) << MOTION_TYPE_SHIFT; 130 DUMPBITS (bit_buf, bits, 2); 131 132 if (macroblock_modes & (MACROBLOCK_INTRA | MACROBLOCK_PATTERN)) 133 { 134 intra: 135 macroblock_modes |= UBITS (bit_buf, 1) * DCT_TYPE_INTERLACED; 136 DUMPBITS (bit_buf, bits, 1); 137 } 138 return macroblock_modes; 139 } 140 141 case D_TYPE: 142 DUMPBITS (bit_buf, bits, 1); 143 return MACROBLOCK_INTRA; 144 145 default: 146 return 0; 147 } 148#undef bit_buf 149#undef bits 150#undef bit_ptr 151} 152 153static inline void get_quantizer_scale (mpeg2_decoder_t * const decoder) 154{ 155#define bit_buf (decoder->bitstream_buf) 156#define bits (decoder->bitstream_bits) 157#define bit_ptr (decoder->bitstream_ptr) 158 159 int quantizer_scale_code; 160 161 quantizer_scale_code = UBITS (bit_buf, 5); 162 DUMPBITS (bit_buf, bits, 5); 163 164 decoder->quantizer_matrix[0] = 165 decoder->quantizer_prescale[0][quantizer_scale_code]; 166 167 decoder->quantizer_matrix[1] = 168 decoder->quantizer_prescale[1][quantizer_scale_code]; 169 170 decoder->quantizer_matrix[2] = 171 decoder->chroma_quantizer[0][quantizer_scale_code]; 172 173 decoder->quantizer_matrix[3] = 174 decoder->chroma_quantizer[1][quantizer_scale_code]; 175#undef bit_buf 176#undef bits 177#undef bit_ptr 178} 179 180static inline int get_motion_delta (mpeg2_decoder_t * const decoder, 181 const int f_code) 182{ 183#define bit_buf (decoder->bitstream_buf) 184#define bits (decoder->bitstream_bits) 185#define bit_ptr (decoder->bitstream_ptr) 186 187 int delta; 188 int sign; 189 const MVtab * tab; 190 191 if (bit_buf & 0x80000000) 192 { 193 DUMPBITS (bit_buf, bits, 1); 194 return 0; 195 } 196 else if (bit_buf >= 0x0c000000) 197 { 198 tab = MV_4 + UBITS (bit_buf, 4); 199 delta = (tab->delta << f_code) + 1; 200 bits += tab->len + f_code + 1; 201 bit_buf <<= tab->len; 202 203 sign = SBITS (bit_buf, 1); 204 bit_buf <<= 1; 205 206 if (f_code) 207 delta += UBITS (bit_buf, f_code); 208 bit_buf <<= f_code; 209 210 return (delta ^ sign) - sign; 211 } 212 else 213 { 214 tab = MV_10 + UBITS (bit_buf, 10); 215 delta = (tab->delta << f_code) + 1; 216 bits += tab->len + 1; 217 bit_buf <<= tab->len; 218 219 sign = SBITS (bit_buf, 1); 220 bit_buf <<= 1; 221 222 if (f_code) 223 { 224 NEEDBITS (bit_buf, bits, bit_ptr); 225 delta += UBITS (bit_buf, f_code); 226 DUMPBITS (bit_buf, bits, f_code); 227 } 228 229 return (delta ^ sign) - sign; 230 231 } 232#undef bit_buf 233#undef bits 234#undef bit_ptr 235} 236 237static inline int bound_motion_vector (const int vector, const int f_code) 238{ 239 return ((int32_t)vector << (27 - f_code)) >> (27 - f_code); 240} 241 242static inline int get_dmv (mpeg2_decoder_t * const decoder) 243{ 244#define bit_buf (decoder->bitstream_buf) 245#define bits (decoder->bitstream_bits) 246#define bit_ptr (decoder->bitstream_ptr) 247 248 const DMVtab * tab; 249 250 tab = DMV_2 + UBITS (bit_buf, 2); 251 DUMPBITS (bit_buf, bits, tab->len); 252 return tab->dmv; 253 254#undef bit_buf 255#undef bits 256#undef bit_ptr 257} 258 259static inline int get_coded_block_pattern (mpeg2_decoder_t * const decoder) 260{ 261#define bit_buf (decoder->bitstream_buf) 262#define bits (decoder->bitstream_bits) 263#define bit_ptr (decoder->bitstream_ptr) 264 265 const CBPtab * tab; 266 267 NEEDBITS (bit_buf, bits, bit_ptr); 268 269 if (bit_buf >= 0x20000000) 270 { 271 tab = CBP_7 + (UBITS (bit_buf, 7) - 16); 272 DUMPBITS (bit_buf, bits, tab->len); 273 return tab->cbp; 274 } 275 else 276 { 277 tab = CBP_9 + UBITS (bit_buf, 9); 278 DUMPBITS (bit_buf, bits, tab->len); 279 return tab->cbp; 280 } 281 282#undef bit_buf 283#undef bits 284#undef bit_ptr 285} 286 287static inline int get_luma_dc_dct_diff (mpeg2_decoder_t * const decoder) 288{ 289#define bit_buf (decoder->bitstream_buf) 290#define bits (decoder->bitstream_bits) 291#define bit_ptr (decoder->bitstream_ptr) 292 293 const DCtab * tab; 294 int size; 295 int dc_diff; 296 297 if (bit_buf < 0xf8000000) 298 { 299 tab = DC_lum_5 + UBITS (bit_buf, 5); 300 size = tab->size; 301 302 if (size) 303 { 304 bits += tab->len + size; 305 bit_buf <<= tab->len; 306 dc_diff = 307 UBITS (bit_buf, size) - UBITS (SBITS (~bit_buf, 1), size); 308 bit_buf <<= size; 309 return dc_diff << decoder->intra_dc_precision; 310 } 311 else 312 { 313 DUMPBITS (bit_buf, bits, 3); 314 return 0; 315 } 316 } 317 else 318 { 319 tab = DC_long + (UBITS (bit_buf, 9) - 0x1e0); 320 size = tab->size; 321 DUMPBITS (bit_buf, bits, tab->len); 322 NEEDBITS (bit_buf, bits, bit_ptr); 323 dc_diff = UBITS (bit_buf, size) - UBITS (SBITS (~bit_buf, 1), size); 324 DUMPBITS (bit_buf, bits, size); 325 return dc_diff << decoder->intra_dc_precision; 326 } 327 328#undef bit_buf 329#undef bits 330#undef bit_ptr 331} 332 333#if MPEG2_COLOR 334static inline int get_chroma_dc_dct_diff (mpeg2_decoder_t * const decoder) 335{ 336#define bit_buf (decoder->bitstream_buf) 337#define bits (decoder->bitstream_bits) 338#define bit_ptr (decoder->bitstream_ptr) 339 340 const DCtab * tab; 341 int size; 342 int dc_diff; 343 344 if (bit_buf < 0xf8000000) 345 { 346 tab = DC_chrom_5 + UBITS (bit_buf, 5); 347 size = tab->size; 348 349 if (size) 350 { 351 bits += tab->len + size; 352 bit_buf <<= tab->len; 353 dc_diff = 354 UBITS (bit_buf, size) - UBITS (SBITS (~bit_buf, 1), size); 355 bit_buf <<= size; 356 return dc_diff << decoder->intra_dc_precision; 357 } 358 else 359 { 360 DUMPBITS (bit_buf, bits, 2); 361 return 0; 362 } 363 } 364 else 365 { 366 tab = DC_long + (UBITS (bit_buf, 10) - 0x3e0); 367 size = tab->size; 368 DUMPBITS (bit_buf, bits, tab->len + 1); 369 NEEDBITS (bit_buf, bits, bit_ptr); 370 dc_diff = UBITS (bit_buf, size) - UBITS (SBITS (~bit_buf, 1), size); 371 DUMPBITS (bit_buf, bits, size); 372 return dc_diff << decoder->intra_dc_precision; 373 } 374 375#undef bit_buf 376#undef bits 377#undef bit_ptr 378} 379#endif /* MPEG2_COLOR */ 380 381#define SATURATE(val) \ 382 do { \ 383 val <<= 4; \ 384 if (unlikely (val != (int16_t) val)) \ 385 val = (SBITS (val, 1) ^ 2047) << 4; \ 386 } while (0) 387 388static void get_intra_block_B14 (mpeg2_decoder_t * const decoder, 389 const uint16_t * const quant_matrix) 390{ 391 uint32_t bit_buf = decoder->bitstream_buf; 392 int bits = decoder->bitstream_bits; 393 const uint8_t * bit_ptr = decoder->bitstream_ptr; 394 const uint8_t * const scan = decoder->scan; 395 int16_t * const dest = decoder->DCTblock; 396 int mismatch = ~dest[0]; 397 int i = 0; 398 int j; 399 int val; 400 const DCTtab * tab; 401 402 NEEDBITS (bit_buf, bits, bit_ptr); 403 404 while (1) 405 { 406 if (bit_buf >= 0x28000000) 407 { 408 tab = DCT_B14AC_5 + (UBITS (bit_buf, 5) - 5); 409 410 i += tab->run; 411 if (i >= 64) 412 break; /* end of block */ 413 414 normal_code: 415 j = scan[i]; 416 bit_buf <<= tab->len; 417 bits += tab->len + 1; 418 val = (tab->level * quant_matrix[j]) >> 4; 419 420 /* if (bitstream_get (1)) val = -val; */ 421 val = (val ^ SBITS (bit_buf, 1)) - SBITS (bit_buf, 1); 422 423 SATURATE (val); 424 dest[j] = val; 425 mismatch ^= val; 426 427 bit_buf <<= 1; 428 NEEDBITS (bit_buf, bits, bit_ptr); 429 430 continue; 431 } 432 else if (bit_buf >= 0x04000000) 433 { 434 tab = DCT_B14_8 + (UBITS (bit_buf, 8) - 4); 435 436 i += tab->run; 437 if (i < 64) 438 goto normal_code; 439 440 /* escape code */ 441 442 i += UBITS (bit_buf << 6, 6) - 64; 443 if (i >= 64) 444 break; /* illegal, check needed to avoid buffer overflow */ 445 446 j = scan[i]; 447 448 DUMPBITS (bit_buf, bits, 12); 449 NEEDBITS (bit_buf, bits, bit_ptr); 450 val = (SBITS (bit_buf, 12) * quant_matrix[j]) / 16; 451 452 SATURATE (val); 453 dest[j] = val; 454 mismatch ^= val; 455 456 DUMPBITS (bit_buf, bits, 12); 457 NEEDBITS (bit_buf, bits, bit_ptr); 458 459 continue; 460 } 461 else if (bit_buf >= 0x02000000) 462 { 463 tab = DCT_B14_10 + (UBITS (bit_buf, 10) - 8); 464 i += tab->run; 465 if (i < 64) 466 goto normal_code; 467 } 468 else if (bit_buf >= 0x00800000) 469 { 470 tab = DCT_13 + (UBITS (bit_buf, 13) - 16); 471 i += tab->run; 472 if (i < 64) 473 goto normal_code; 474 } 475 else if (bit_buf >= 0x00200000) 476 { 477 tab = DCT_15 + (UBITS (bit_buf, 15) - 16); 478 i += tab->run; 479 if (i < 64) 480 goto normal_code; 481 } 482 else 483 { 484 tab = DCT_16 + UBITS (bit_buf, 16); 485 bit_buf <<= 16; 486 GETWORD (bit_buf, bits + 16, bit_ptr); 487 i += tab->run; 488 if (i < 64) 489 goto normal_code; 490 } 491 break; /* illegal, check needed to avoid buffer overflow */ 492 } 493 494 dest[63] ^= mismatch & 16; 495 DUMPBITS (bit_buf, bits, tab->len); /* dump end of block code */ 496 decoder->bitstream_buf = bit_buf; 497 decoder->bitstream_bits = bits; 498 decoder->bitstream_ptr = bit_ptr; 499} 500 501static void get_intra_block_B15 (mpeg2_decoder_t * const decoder, 502 const uint16_t * const quant_matrix) 503{ 504 uint32_t bit_buf = decoder->bitstream_buf; 505 int bits = decoder->bitstream_bits; 506 const uint8_t * bit_ptr = decoder->bitstream_ptr; 507 const uint8_t * const scan = decoder->scan; 508 int16_t * const dest = decoder->DCTblock; 509 int mismatch = ~dest[0]; 510 int i = 0; 511 int j; 512 int val; 513 const DCTtab * tab; 514 515 NEEDBITS (bit_buf, bits, bit_ptr); 516 517 while (1) 518 { 519 if (bit_buf >= 0x04000000) 520 { 521 tab = DCT_B15_8 + (UBITS (bit_buf, 8) - 4); 522 523 i += tab->run; 524 525 if (i < 64) 526 { 527 normal_code: 528 j = scan[i]; 529 bit_buf <<= tab->len; 530 bits += tab->len + 1; 531 val = (tab->level * quant_matrix[j]) >> 4; 532 533 /* if (bitstream_get (1)) val = -val; */ 534 val = (val ^ SBITS (bit_buf, 1)) - SBITS (bit_buf, 1); 535 536 SATURATE (val); 537 dest[j] = val; 538 mismatch ^= val; 539 540 bit_buf <<= 1; 541 NEEDBITS (bit_buf, bits, bit_ptr); 542 543 continue; 544 } 545 else 546 { 547 /* end of block. I commented out this code because if we */ 548 /* dont exit here we will still exit at the later test :) */ 549 550 /* if (i >= 128) break; */ /* end of block */ 551 552 /* escape code */ 553 554 i += UBITS (bit_buf << 6, 6) - 64; 555 if (i >= 64) 556 break; /* illegal, check against buffer overflow */ 557 558 j = scan[i]; 559 560 DUMPBITS (bit_buf, bits, 12); 561 NEEDBITS (bit_buf, bits, bit_ptr); 562 val = (SBITS (bit_buf, 12) * quant_matrix[j]) / 16; 563 564 SATURATE (val); 565 dest[j] = val; 566 mismatch ^= val; 567 568 DUMPBITS (bit_buf, bits, 12); 569 NEEDBITS (bit_buf, bits, bit_ptr); 570 571 continue; 572 } 573 } 574 else if (bit_buf >= 0x02000000) 575 { 576 tab = DCT_B15_10 + (UBITS (bit_buf, 10) - 8); 577 i += tab->run; 578 if (i < 64) 579 goto normal_code; 580 } 581 else if (bit_buf >= 0x00800000) 582 { 583 tab = DCT_13 + (UBITS (bit_buf, 13) - 16); 584 i += tab->run; 585 if (i < 64) 586 goto normal_code; 587 } 588 else if (bit_buf >= 0x00200000) 589 { 590 tab = DCT_15 + (UBITS (bit_buf, 15) - 16); 591 i += tab->run; 592 if (i < 64) 593 goto normal_code; 594 } 595 else 596 { 597 tab = DCT_16 + UBITS (bit_buf, 16); 598 bit_buf <<= 16; 599 GETWORD (bit_buf, bits + 16, bit_ptr); 600 i += tab->run; 601 if (i < 64) 602 goto normal_code; 603 } 604 break; /* illegal, check needed to avoid buffer overflow */ 605 } 606 607 dest[63] ^= mismatch & 16; 608 DUMPBITS (bit_buf, bits, tab->len); /* dump end of block code */ 609 decoder->bitstream_buf = bit_buf; 610 decoder->bitstream_bits = bits; 611 decoder->bitstream_ptr = bit_ptr; 612} 613 614static int get_non_intra_block (mpeg2_decoder_t * const decoder, 615 const uint16_t * const quant_matrix) 616{ 617 uint32_t bit_buf = decoder->bitstream_buf; 618 int bits = decoder->bitstream_bits; 619 const uint8_t * bit_ptr = decoder->bitstream_ptr; 620 const uint8_t * const scan = decoder->scan; 621 int16_t * const dest = decoder->DCTblock; 622 int mismatch = -1; 623 int i = -1; 624 int j; 625 int val; 626 const DCTtab * tab; 627 628 NEEDBITS (bit_buf, bits, bit_ptr); 629 630 if (bit_buf >= 0x28000000) 631 { 632 tab = DCT_B14DC_5 + (UBITS (bit_buf, 5) - 5); 633 goto entry_1; 634 } 635 else 636 { 637 goto entry_2; 638 } 639 640 while (1) 641 { 642 if (bit_buf >= 0x28000000) 643 { 644 tab = DCT_B14AC_5 + (UBITS (bit_buf, 5) - 5); 645 646 entry_1: 647 i += tab->run; 648 if (i >= 64) 649 break; /* end of block */ 650 651 normal_code: 652 j = scan[i]; 653 bit_buf <<= tab->len; 654 bits += tab->len + 1; 655 val = ((2 * tab->level + 1) * quant_matrix[j]) >> 5; 656 657 /* if (bitstream_get (1)) val = -val; */ 658 val = (val ^ SBITS (bit_buf, 1)) - SBITS (bit_buf, 1); 659 660 SATURATE (val); 661 dest[j] = val; 662 mismatch ^= val; 663 664 bit_buf <<= 1; 665 NEEDBITS (bit_buf, bits, bit_ptr); 666 667 continue; 668 } 669 670 entry_2: 671 if (bit_buf >= 0x04000000) 672 { 673 tab = DCT_B14_8 + (UBITS (bit_buf, 8) - 4); 674 675 i += tab->run; 676 if (i < 64) 677 goto normal_code; 678 679 /* escape code */ 680 681 i += UBITS (bit_buf << 6, 6) - 64; 682 if (i >= 64) 683 break; /* illegal, check needed to avoid buffer overflow */ 684 685 j = scan[i]; 686 687 DUMPBITS (bit_buf, bits, 12); 688 NEEDBITS (bit_buf, bits, bit_ptr); 689 val = 2 * (SBITS (bit_buf, 12) + SBITS (bit_buf, 1)) + 1; 690 val = (val * quant_matrix[j]) / 32; 691 692 SATURATE (val); 693 dest[j] = val; 694 mismatch ^= val; 695 696 DUMPBITS (bit_buf, bits, 12); 697 NEEDBITS (bit_buf, bits, bit_ptr); 698 699 continue; 700 } 701 else if (bit_buf >= 0x02000000) 702 { 703 tab = DCT_B14_10 + (UBITS (bit_buf, 10) - 8); 704 i += tab->run; 705 if (i < 64) 706 goto normal_code; 707 } 708 else if (bit_buf >= 0x00800000) 709 { 710 tab = DCT_13 + (UBITS (bit_buf, 13) - 16); 711 i += tab->run; 712 if (i < 64) 713 goto normal_code; 714 } 715 else if (bit_buf >= 0x00200000) 716 { 717 tab = DCT_15 + (UBITS (bit_buf, 15) - 16); 718 i += tab->run; 719 if (i < 64) 720 goto normal_code; 721 } 722 else 723 { 724 tab = DCT_16 + UBITS (bit_buf, 16); 725 bit_buf <<= 16; 726 GETWORD (bit_buf, bits + 16, bit_ptr); 727 i += tab->run; 728 if (i < 64) 729 goto normal_code; 730 } 731 break; /* illegal, check needed to avoid buffer overflow */ 732 } 733 734 dest[63] ^= mismatch & 16; 735 DUMPBITS (bit_buf, bits, tab->len); /* dump end of block code */ 736 decoder->bitstream_buf = bit_buf; 737 decoder->bitstream_bits = bits; 738 decoder->bitstream_ptr = bit_ptr; 739 return i; 740} 741 742static void get_mpeg1_intra_block (mpeg2_decoder_t * const decoder) 743{ 744 uint32_t bit_buf = decoder->bitstream_buf; 745 int bits = decoder->bitstream_bits; 746 const uint8_t * bit_ptr = decoder->bitstream_ptr; 747 const uint8_t * const scan = decoder->scan; 748 const uint16_t * const quant_matrix = decoder->quantizer_matrix[0]; 749 int16_t * const dest = decoder->DCTblock; 750 int i = 0; 751 int j; 752 int val; 753 const DCTtab * tab; 754 755 NEEDBITS (bit_buf, bits, bit_ptr); 756 757 while (1) 758 { 759 if (bit_buf >= 0x28000000) 760 { 761 tab = DCT_B14AC_5 + (UBITS (bit_buf, 5) - 5); 762 763 i += tab->run; 764 if (i >= 64) 765 break; /* end of block */ 766 767 normal_code: 768 j = scan[i]; 769 bit_buf <<= tab->len; 770 bits += tab->len + 1; 771 val = (tab->level * quant_matrix[j]) >> 4; 772 773 /* oddification */ 774 val = (val - 1) | 1; 775 776 /* if (bitstream_get (1)) val = -val; */ 777 val = (val ^ SBITS (bit_buf, 1)) - SBITS (bit_buf, 1); 778 779 SATURATE (val); 780 dest[j] = val; 781 782 bit_buf <<= 1; 783 NEEDBITS (bit_buf, bits, bit_ptr); 784 785 continue; 786 } 787 else if (bit_buf >= 0x04000000) 788 { 789 tab = DCT_B14_8 + (UBITS (bit_buf, 8) - 4); 790 791 i += tab->run; 792 if (i < 64) 793 goto normal_code; 794 795 /* escape code */ 796 797 i += UBITS (bit_buf << 6, 6) - 64; 798 if (i >= 64) 799 break; /* illegal, check needed to avoid buffer overflow */ 800 801 j = scan[i]; 802 803 DUMPBITS (bit_buf, bits, 12); 804 NEEDBITS (bit_buf, bits, bit_ptr); 805 val = SBITS (bit_buf, 8); 806 807 if (! (val & 0x7f)) 808 { 809 DUMPBITS (bit_buf, bits, 8); 810 val = UBITS (bit_buf, 8) + 2 * val; 811 } 812 813 val = (val * quant_matrix[j]) / 16; 814 815 /* oddification */ 816 val = (val + ~SBITS (val, 1)) | 1; 817 818 SATURATE (val); 819 dest[j] = val; 820 821 DUMPBITS (bit_buf, bits, 8); 822 NEEDBITS (bit_buf, bits, bit_ptr); 823 824 continue; 825 } 826 else if (bit_buf >= 0x02000000) 827 { 828 tab = DCT_B14_10 + (UBITS (bit_buf, 10) - 8); 829 i += tab->run; 830 if (i < 64) 831 goto normal_code; 832 } 833 else if (bit_buf >= 0x00800000) 834 { 835 tab = DCT_13 + (UBITS (bit_buf, 13) - 16); 836 i += tab->run; 837 if (i < 64) 838 goto normal_code; 839 } 840 else if (bit_buf >= 0x00200000) 841 { 842 tab = DCT_15 + (UBITS (bit_buf, 15) - 16); 843 i += tab->run; 844 if (i < 64) 845 goto normal_code; 846 } 847 else 848 { 849 tab = DCT_16 + UBITS (bit_buf, 16); 850 bit_buf <<= 16; 851 GETWORD (bit_buf, bits + 16, bit_ptr); 852 i += tab->run; 853 if (i < 64) 854 goto normal_code; 855 } 856 break; /* illegal, check needed to avoid buffer overflow */ 857 } 858 859 DUMPBITS (bit_buf, bits, tab->len); /* dump end of block code */ 860 decoder->bitstream_buf = bit_buf; 861 decoder->bitstream_bits = bits; 862 decoder->bitstream_ptr = bit_ptr; 863} 864 865static int get_mpeg1_non_intra_block (mpeg2_decoder_t * const decoder) 866{ 867 uint32_t bit_buf = decoder->bitstream_buf; 868 int bits = decoder->bitstream_bits; 869 const uint8_t * bit_ptr = decoder->bitstream_ptr; 870 const uint8_t * const scan = decoder->scan; 871 const uint16_t * const quant_matrix = decoder->quantizer_matrix[1]; 872 int16_t * const dest = decoder->DCTblock; 873 int i = -1; 874 int j; 875 int val; 876 const DCTtab * tab; 877 878 NEEDBITS (bit_buf, bits, bit_ptr); 879 if (bit_buf >= 0x28000000) 880 { 881 tab = DCT_B14DC_5 + (UBITS (bit_buf, 5) - 5); 882 goto entry_1; 883 } 884 else 885 { 886 goto entry_2; 887 } 888 889 while (1) 890 { 891 if (bit_buf >= 0x28000000) 892 { 893 tab = DCT_B14AC_5 + (UBITS (bit_buf, 5) - 5); 894 895 entry_1: 896 i += tab->run; 897 if (i >= 64) 898 break; /* end of block */ 899 900 normal_code: 901 j = scan[i]; 902 bit_buf <<= tab->len; 903 bits += tab->len + 1; 904 val = ((2 * tab->level + 1) * quant_matrix[j]) >> 5; 905 906 /* oddification */ 907 val = (val - 1) | 1; 908 909 /* if (bitstream_get (1)) val = -val; */ 910 val = (val ^ SBITS (bit_buf, 1)) - SBITS (bit_buf, 1); 911 912 SATURATE (val); 913 dest[j] = val; 914 915 bit_buf <<= 1; 916 NEEDBITS (bit_buf, bits, bit_ptr); 917 918 continue; 919 } 920 921 entry_2: 922 if (bit_buf >= 0x04000000) 923 { 924 tab = DCT_B14_8 + (UBITS (bit_buf, 8) - 4); 925 926 i += tab->run; 927 if (i < 64) 928 goto normal_code; 929 930 /* escape code */ 931 932 i += UBITS (bit_buf << 6, 6) - 64; 933 if (i >= 64) 934 break; /* illegal, check needed to avoid buffer overflow */ 935 936 j = scan[i]; 937 938 DUMPBITS (bit_buf, bits, 12); 939 NEEDBITS (bit_buf, bits, bit_ptr); 940 val = SBITS (bit_buf, 8); 941 942 if (! (val & 0x7f)) 943 { 944 DUMPBITS (bit_buf, bits, 8); 945 val = UBITS (bit_buf, 8) + 2 * val; 946 } 947 948 val = 2 * (val + SBITS (val, 1)) + 1; 949 val = (val * quant_matrix[j]) / 32; 950 951 /* oddification */ 952 val = (val + ~SBITS (val, 1)) | 1; 953 954 SATURATE (val); 955 dest[j] = val; 956 957 DUMPBITS (bit_buf, bits, 8); 958 NEEDBITS (bit_buf, bits, bit_ptr); 959 960 continue; 961 962 } 963 else if (bit_buf >= 0x02000000) 964 { 965 tab = DCT_B14_10 + (UBITS (bit_buf, 10) - 8); 966 i += tab->run; 967 if (i < 64) 968 goto normal_code; 969 } 970 else if (bit_buf >= 0x00800000) 971 { 972 tab = DCT_13 + (UBITS (bit_buf, 13) - 16); 973 i += tab->run; 974 if (i < 64) 975 goto normal_code; 976 } 977 else if (bit_buf >= 0x00200000) 978 { 979 tab = DCT_15 + (UBITS (bit_buf, 15) - 16); 980 i += tab->run; 981 if (i < 64) 982 goto normal_code; 983 } 984 else 985 { 986 tab = DCT_16 + UBITS (bit_buf, 16); 987 bit_buf <<= 16; 988 GETWORD (bit_buf, bits + 16, bit_ptr); 989 i += tab->run; 990 if (i < 64) 991 goto normal_code; 992 } 993 break; /* illegal, check needed to avoid buffer overflow */ 994 } 995 996 DUMPBITS (bit_buf, bits, tab->len); /* dump end of block code */ 997 decoder->bitstream_buf = bit_buf; 998 decoder->bitstream_bits = bits; 999 decoder->bitstream_ptr = bit_ptr; 1000 return i; 1001} 1002 1003static inline void slice_intra_DCT (mpeg2_decoder_t * const decoder, 1004 const int cc, 1005 uint8_t * const dest, const int stride) 1006{ 1007#define bit_buf (decoder->bitstream_buf) 1008#define bits (decoder->bitstream_bits) 1009#define bit_ptr (decoder->bitstream_ptr) 1010 1011 NEEDBITS (bit_buf, bits, bit_ptr); 1012 /* Get the intra DC coefficient and inverse quantize it */ 1013 if (cc == 0) 1014 { 1015 decoder->dc_dct_pred[0] += get_luma_dc_dct_diff (decoder); 1016 decoder->DCTblock[0] = decoder->dc_dct_pred[0]; 1017 1018 } 1019#if MPEG2_COLOR 1020 else 1021 { 1022 decoder->dc_dct_pred[cc] += get_chroma_dc_dct_diff (decoder); 1023 decoder->DCTblock[0] = decoder->dc_dct_pred[cc]; 1024 } 1025#endif 1026 1027 if (decoder->mpeg1) 1028 { 1029 if (decoder->coding_type != D_TYPE) 1030 get_mpeg1_intra_block (decoder); 1031 } 1032 else if (decoder->intra_vlc_format) 1033 { 1034 get_intra_block_B15 (decoder, decoder->quantizer_matrix[cc ? 2 : 0]); 1035 } 1036 else 1037 { 1038 get_intra_block_B14 (decoder, decoder->quantizer_matrix[cc ? 2 : 0]); 1039 } 1040 1041 mpeg2_idct_copy (decoder->DCTblock, dest, stride); 1042 1043#undef bit_buf 1044#undef bits 1045#undef bit_ptr 1046} 1047 1048static inline void slice_non_intra_DCT (mpeg2_decoder_t * const decoder, 1049 const int cc, 1050 uint8_t * const dest, const int stride) 1051{ 1052 int last; 1053 1054 if (decoder->mpeg1) 1055 { 1056 last = get_mpeg1_non_intra_block (decoder); 1057 } 1058 else 1059 { 1060 last = get_non_intra_block (decoder, 1061 decoder->quantizer_matrix[cc ? 3 : 1]); 1062 } 1063 1064 mpeg2_idct_add (last, decoder->DCTblock, dest, stride); 1065} 1066 1067#if !MPEG2_COLOR 1068static void skip_mpeg1_intra_block (mpeg2_decoder_t * const decoder) 1069{ 1070 uint32_t bit_buf = decoder->bitstream_buf; 1071 int bits = decoder->bitstream_bits; 1072 const uint8_t * bit_ptr = decoder->bitstream_ptr; 1073 int i = 0; 1074 const DCTtab * tab; 1075 1076 NEEDBITS (bit_buf, bits, bit_ptr); 1077 1078 while (1) 1079 { 1080 if (bit_buf >= 0x28000000) 1081 { 1082 tab = DCT_B14AC_5 + (UBITS (bit_buf, 5) - 5); 1083 1084 i += tab->run; 1085 if (i >= 64) 1086 break; /* end of block */ 1087 1088 normal_code: 1089 bit_buf <<= tab->len + 1; 1090 bits += tab->len + 1; 1091 NEEDBITS (bit_buf, bits, bit_ptr); 1092 continue; 1093 } 1094 else if (bit_buf >= 0x04000000) 1095 { 1096 tab = DCT_B14_8 + (UBITS (bit_buf, 8) - 4); 1097 1098 i += tab->run; 1099 if (i < 64) 1100 goto normal_code; 1101 1102 /* escape code */ 1103 1104 i += UBITS (bit_buf << 6, 6) - 64; 1105 if (i >= 64) 1106 break; /* illegal, check needed to avoid buffer overflow */ 1107 1108 DUMPBITS (bit_buf, bits, 12); 1109 NEEDBITS (bit_buf, bits, bit_ptr); 1110 1111 if (!(SBITS (bit_buf, 8) & 0x7f)) 1112 DUMPBITS (bit_buf, bits, 8); 1113 1114 DUMPBITS (bit_buf, bits, 8); 1115 NEEDBITS (bit_buf, bits, bit_ptr); 1116 1117 continue; 1118 } 1119 else if (bit_buf >= 0x02000000) 1120 { 1121 tab = DCT_B14_10 + (UBITS (bit_buf, 10) - 8); 1122 i += tab->run; 1123 if (i < 64) 1124 goto normal_code; 1125 } 1126 else if (bit_buf >= 0x00800000) 1127 { 1128 tab = DCT_13 + (UBITS (bit_buf, 13) - 16); 1129 i += tab->run; 1130 if (i < 64) 1131 goto normal_code; 1132 } 1133 else if (bit_buf >= 0x00200000) 1134 { 1135 tab = DCT_15 + (UBITS (bit_buf, 15) - 16); 1136 i += tab->run; 1137 if (i < 64) 1138 goto normal_code; 1139 } 1140 else 1141 { 1142 tab = DCT_16 + UBITS (bit_buf, 16); 1143 bit_buf <<= 16; 1144 GETWORD (bit_buf, bits + 16, bit_ptr); 1145 i += tab->run; 1146 if (i < 64) 1147 goto normal_code; 1148 } 1149 break; /* illegal, check needed to avoid buffer overflow */ 1150 } 1151 1152 DUMPBITS (bit_buf, bits, 2); /* dump end of block code */ 1153 decoder->bitstream_buf = bit_buf; 1154 decoder->bitstream_bits = bits; 1155 decoder->bitstream_ptr = bit_ptr; 1156} 1157 1158static void skip_intra_block_B14 (mpeg2_decoder_t * const decoder) 1159{ 1160 uint32_t bit_buf = decoder->bitstream_buf; 1161 int bits = decoder->bitstream_bits; 1162 const uint8_t * bit_ptr = decoder->bitstream_ptr; 1163 int i = 0; 1164 const DCTtab * tab; 1165 1166 NEEDBITS (bit_buf, bits, bit_ptr); 1167 1168 while (1) 1169 { 1170 if (bit_buf >= 0x28000000) 1171 { 1172 tab = DCT_B14AC_5 + (UBITS (bit_buf, 5) - 5); 1173 1174 i += tab->run; 1175 if (i >= 64) 1176 break; /* end of block */ 1177 1178 normal_code: 1179 bit_buf <<= tab->len + 1; 1180 bits += tab->len + 1; 1181 NEEDBITS (bit_buf, bits, bit_ptr); 1182 continue; 1183 } 1184 else if (bit_buf >= 0x04000000) 1185 { 1186 tab = DCT_B14_8 + (UBITS (bit_buf, 8) - 4); 1187 1188 i += tab->run; 1189 if (i < 64) 1190 goto normal_code; 1191 1192 /* escape code */ 1193 1194 i += UBITS (bit_buf << 6, 6) - 64; 1195 if (i >= 64) 1196 break; /* illegal, check needed to avoid buffer overflow */ 1197 1198 DUMPBITS (bit_buf, bits, 12); /* Can't dump more than 16 atm */ 1199 NEEDBITS (bit_buf, bits, bit_ptr); 1200 DUMPBITS (bit_buf, bits, 12); 1201 NEEDBITS (bit_buf, bits, bit_ptr); 1202 continue; 1203 } 1204 else if (bit_buf >= 0x02000000) 1205 { 1206 tab = DCT_B14_10 + (UBITS (bit_buf, 10) - 8); 1207 i += tab->run; 1208 if (i < 64) 1209 goto normal_code; 1210 } 1211 else if (bit_buf >= 0x00800000) 1212 { 1213 tab = DCT_13 + (UBITS (bit_buf, 13) - 16); 1214 i += tab->run; 1215 if (i < 64) 1216 goto normal_code; 1217 } 1218 else if (bit_buf >= 0x00200000) 1219 { 1220 tab = DCT_15 + (UBITS (bit_buf, 15) - 16); 1221 i += tab->run; 1222 if (i < 64) 1223 goto normal_code; 1224 } 1225 else 1226 { 1227 tab = DCT_16 + UBITS (bit_buf, 16); 1228 bit_buf <<= 16; 1229 GETWORD (bit_buf, bits + 16, bit_ptr); 1230 i += tab->run; 1231 if (i < 64) 1232 goto normal_code; 1233 } 1234 break; /* illegal, check needed to avoid buffer overflow */ 1235 } 1236 1237 DUMPBITS (bit_buf, bits, 2); /* dump end of block code */ 1238 decoder->bitstream_buf = bit_buf; 1239 decoder->bitstream_bits = bits; 1240 decoder->bitstream_ptr = bit_ptr; 1241} 1242 1243static void skip_intra_block_B15 (mpeg2_decoder_t * const decoder) 1244{ 1245 uint32_t bit_buf = decoder->bitstream_buf; 1246 int bits = decoder->bitstream_bits; 1247 const uint8_t * bit_ptr = decoder->bitstream_ptr; 1248 int i = 0; 1249 const DCTtab * tab; 1250 1251 NEEDBITS (bit_buf, bits, bit_ptr); 1252 1253 while (1) 1254 { 1255 if (bit_buf >= 0x04000000) 1256 { 1257 tab = DCT_B15_8 + (UBITS (bit_buf, 8) - 4); 1258 1259 i += tab->run; 1260 1261 if (i < 64) 1262 { 1263 normal_code: 1264 bit_buf <<= tab->len + 1; 1265 bits += tab->len + 1; 1266 NEEDBITS (bit_buf, bits, bit_ptr); 1267 continue; 1268 } 1269 else 1270 { 1271 /* end of block. I commented out this code because if we */ 1272 /* dont exit here we will still exit at the later test :) */ 1273 1274 /* if (i >= 128) break; */ /* end of block */ 1275 1276 /* escape code */ 1277 1278 i += UBITS (bit_buf << 6, 6) - 64; 1279 if (i >= 64) 1280 break; /* illegal, check against buffer overflow */ 1281 1282 DUMPBITS (bit_buf, bits, 12); /* Can't dump more than 16 atm */ 1283 NEEDBITS (bit_buf, bits, bit_ptr); 1284 DUMPBITS (bit_buf, bits, 12); 1285 NEEDBITS (bit_buf, bits, bit_ptr); 1286 continue; 1287 } 1288 } 1289 else if (bit_buf >= 0x02000000) 1290 { 1291 tab = DCT_B15_10 + (UBITS (bit_buf, 10) - 8); 1292 i += tab->run; 1293 if (i < 64) 1294 goto normal_code; 1295 } 1296 else if (bit_buf >= 0x00800000) 1297 { 1298 tab = DCT_13 + (UBITS (bit_buf, 13) - 16); 1299 i += tab->run; 1300 if (i < 64) 1301 goto normal_code; 1302 } 1303 else if (bit_buf >= 0x00200000) 1304 { 1305 tab = DCT_15 + (UBITS (bit_buf, 15) - 16); 1306 i += tab->run; 1307 if (i < 64) 1308 goto normal_code; 1309 } 1310 else 1311 { 1312 tab = DCT_16 + UBITS (bit_buf, 16); 1313 bit_buf <<= 16; 1314 GETWORD (bit_buf, bits + 16, bit_ptr); 1315 i += tab->run; 1316 if (i < 64) 1317 goto normal_code; 1318 } 1319 break; /* illegal, check needed to avoid buffer overflow */ 1320 } 1321 1322 DUMPBITS (bit_buf, bits, 4); /* dump end of block code */ 1323 decoder->bitstream_buf = bit_buf; 1324 decoder->bitstream_bits = bits; 1325 decoder->bitstream_ptr = bit_ptr; 1326} 1327 1328static void skip_non_intra_block (mpeg2_decoder_t * const decoder) 1329{ 1330 uint32_t bit_buf = decoder->bitstream_buf; 1331 int bits = decoder->bitstream_bits; 1332 const uint8_t * bit_ptr = decoder->bitstream_ptr; 1333 int i = -1; 1334 const DCTtab * tab; 1335 1336 NEEDBITS (bit_buf, bits, bit_ptr); 1337 1338 if (bit_buf >= 0x28000000) 1339 { 1340 tab = DCT_B14DC_5 + (UBITS (bit_buf, 5) - 5); 1341 goto entry_1; 1342 } 1343 else 1344 { 1345 goto entry_2; 1346 } 1347 1348 while (1) 1349 { 1350 if (bit_buf >= 0x28000000) 1351 { 1352 tab = DCT_B14AC_5 + (UBITS (bit_buf, 5) - 5); 1353 1354 entry_1: 1355 i += tab->run; 1356 if (i >= 64) 1357 break; /* end of block */ 1358 1359 normal_code: 1360 bit_buf <<= tab->len + 1; 1361 bits += tab->len + 1; 1362 NEEDBITS (bit_buf, bits, bit_ptr); 1363 1364 continue; 1365 } 1366 1367 entry_2: 1368 if (bit_buf >= 0x04000000) 1369 { 1370 tab = DCT_B14_8 + (UBITS (bit_buf, 8) - 4); 1371 1372 i += tab->run; 1373 if (i < 64) 1374 goto normal_code; 1375 1376 /* escape code */ 1377 1378 i += UBITS (bit_buf << 6, 6) - 64; 1379 if (i >= 64) 1380 break; /* illegal, check needed to avoid buffer overflow */ 1381 1382 if (decoder->mpeg1) 1383 { 1384 DUMPBITS (bit_buf, bits, 12); 1385 NEEDBITS (bit_buf, bits, bit_ptr); 1386 1387 if (!(SBITS (bit_buf, 8) & 0x7f)) 1388 DUMPBITS (bit_buf, bits, 8); 1389 1390 DUMPBITS (bit_buf, bits, 8); 1391 } 1392 else 1393 { 1394 DUMPBITS (bit_buf, bits, 12); 1395 NEEDBITS (bit_buf, bits, bit_ptr); 1396 DUMPBITS (bit_buf, bits, 12); 1397 } 1398 1399 NEEDBITS (bit_buf, bits, bit_ptr); 1400 continue; 1401 } 1402 else if (bit_buf >= 0x02000000) 1403 { 1404 tab = DCT_B14_10 + (UBITS (bit_buf, 10) - 8); 1405 i += tab->run; 1406 if (i < 64) 1407 goto normal_code; 1408 } 1409 else if (bit_buf >= 0x00800000) 1410 { 1411 tab = DCT_13 + (UBITS (bit_buf, 13) - 16); 1412 i += tab->run; 1413 if (i < 64) 1414 goto normal_code; 1415 } 1416 else if (bit_buf >= 0x00200000) 1417 { 1418 tab = DCT_15 + (UBITS (bit_buf, 15) - 16); 1419 i += tab->run; 1420 if (i < 64) 1421 goto normal_code; 1422 } 1423 else 1424 { 1425 tab = DCT_16 + UBITS (bit_buf, 16); 1426 bit_buf <<= 16; 1427 GETWORD (bit_buf, bits + 16, bit_ptr); 1428 i += tab->run; 1429 if (i < 64) 1430 goto normal_code; 1431 } 1432 break; /* illegal, check needed to avoid buffer overflow */ 1433 } 1434 1435 DUMPBITS (bit_buf, bits, 2); /* dump end of block code */ 1436 decoder->bitstream_buf = bit_buf; 1437 decoder->bitstream_bits = bits; 1438 decoder->bitstream_ptr = bit_ptr; 1439} 1440 1441static void skip_chroma_dc_dct_diff (mpeg2_decoder_t * const decoder) 1442{ 1443#define bit_buf (decoder->bitstream_buf) 1444#define bits (decoder->bitstream_bits) 1445#define bit_ptr (decoder->bitstream_ptr) 1446 1447 const DCtab * tab; 1448 int size; 1449 1450 if (bit_buf < 0xf8000000) 1451 { 1452 tab = DC_chrom_5 + UBITS (bit_buf, 5); 1453 size = tab->size; 1454 1455 if (size) 1456 { 1457 bits += tab->len + size; 1458 bit_buf <<= tab->len; 1459 bit_buf <<= size; 1460 } 1461 else 1462 { 1463 DUMPBITS (bit_buf, bits, 2); 1464 } 1465 } 1466 else 1467 { 1468 tab = DC_long + (UBITS (bit_buf, 10) - 0x3e0); 1469 size = tab->size; 1470 DUMPBITS (bit_buf, bits, tab->len + 1); 1471 NEEDBITS (bit_buf, bits, bit_ptr); 1472 DUMPBITS (bit_buf, bits, size); 1473 } 1474 1475#undef bit_buf 1476#undef bits 1477#undef bit_ptr 1478} 1479 1480static void skip_chroma_non_intra (mpeg2_decoder_t * const decoder, 1481 uint32_t coded_block_pattern) 1482{ 1483 static const uint32_t cbp_mask[3] = 1484 { 1485 0x00000030, 1486 0xc0000030, 1487 0xfc000030, 1488 }; 1489 1490 uint32_t cbp = coded_block_pattern & 1491 cbp_mask[MIN((unsigned)decoder->chroma_format, 2u)]; 1492 1493 while (cbp) 1494 { 1495 skip_non_intra_block (decoder); 1496 cbp &= (cbp - 1); 1497 } 1498} 1499 1500static void skip_chroma_intra (mpeg2_decoder_t * const decoder) 1501{ 1502#define bit_buf (decoder->bitstream_buf) 1503#define bits (decoder->bitstream_bits) 1504#define bit_ptr (decoder->bitstream_ptr) 1505 int i = 2 << decoder->chroma_format; 1506 1507 if ((unsigned)i > 8) 1508 i = 8; 1509 1510 while (i-- > 0) 1511 { 1512 NEEDBITS (bit_buf, bits, bit_ptr); 1513 1514 skip_chroma_dc_dct_diff (decoder); 1515 1516 if (decoder->mpeg1) 1517 { 1518 if (decoder->coding_type != D_TYPE) 1519 skip_mpeg1_intra_block (decoder); 1520 } 1521 else if (decoder->intra_vlc_format) 1522 { 1523 skip_intra_block_B15 (decoder); 1524 } 1525 else 1526 { 1527 skip_intra_block_B14 (decoder); 1528 } 1529 } 1530 1531 if (decoder->chroma_format == 0 && decoder->coding_type == D_TYPE) 1532 { 1533 NEEDBITS (bit_buf, bits, bit_ptr); 1534 DUMPBITS (bit_buf, bits, 1); 1535 } 1536 1537#undef bit_buf 1538#undef bits 1539#undef bit_ptr 1540} 1541#endif /* !MPEG2_COLOR */ 1542 1543#define MOTION_420(table, ref, motion_x, motion_y, size, y) \ 1544 pos_x = 2 * decoder->offset + motion_x; \ 1545 pos_y = 2 * decoder->v_offset + motion_y + 2 * y; \ 1546 \ 1547 if (unlikely (pos_x > decoder->limit_x)) \ 1548 { \ 1549 pos_x = ((int)pos_x < 0) ? 0 : decoder->limit_x; \ 1550 motion_x = pos_x - 2 * decoder->offset; \ 1551 } \ 1552 \ 1553 if (unlikely (pos_y > decoder->limit_y_ ## size)) \ 1554 { \ 1555 pos_y = ((int)pos_y < 0) ? 0 : decoder->limit_y_ ## size; \ 1556 motion_y = pos_y - 2 * decoder->v_offset - 2 * y; \ 1557 } \ 1558 \ 1559 xy_half = ((pos_y & 1) << 1) | (pos_x & 1); \ 1560 table[xy_half] (decoder->dest[0] + y * decoder->stride + decoder->offset, \ 1561 ref[0] + (pos_x >> 1) + (pos_y >> 1) * decoder->stride, \ 1562 decoder->stride, size); \ 1563 \ 1564 if (MPEG2_COLOR) \ 1565 { \ 1566 motion_x /= 2; \ 1567 motion_y /= 2; \ 1568 xy_half = ((motion_y & 1) << 1) | (motion_x & 1); \ 1569 offset = ((decoder->offset + motion_x) >> 1) + \ 1570 ((((decoder->v_offset + motion_y) >> 1) + y/2) * \ 1571 decoder->uv_stride); \ 1572 \ 1573 table[4+xy_half] (decoder->dest[1] + y/2 * decoder->uv_stride + \ 1574 (decoder->offset >> 1), ref[1] + offset, \ 1575 decoder->uv_stride, size/2); \ 1576 table[4+xy_half] (decoder->dest[2] + y/2 * decoder->uv_stride + \ 1577 (decoder->offset >> 1), ref[2] + offset, \ 1578 decoder->uv_stride, size/2); \ 1579 } 1580 1581#define MOTION_FIELD_420(table, ref, motion_x, motion_y, \ 1582 dest_field, op, src_field) \ 1583 pos_x = 2 * decoder->offset + motion_x; \ 1584 pos_y = decoder->v_offset + motion_y; \ 1585 \ 1586 if (unlikely (pos_x > decoder->limit_x)) \ 1587 { \ 1588 pos_x = ((int)pos_x < 0) ? 0 : decoder->limit_x; \ 1589 motion_x = pos_x - 2 * decoder->offset; \ 1590 } \ 1591 \ 1592 if (unlikely (pos_y > decoder->limit_y)) \ 1593 { \ 1594 pos_y = ((int)pos_y < 0) ? 0 : decoder->limit_y; \ 1595 motion_y = pos_y - decoder->v_offset; \ 1596 } \ 1597 \ 1598 xy_half = ((pos_y & 1) << 1) | (pos_x & 1); \ 1599 table[xy_half] (decoder->dest[0] + dest_field * decoder->stride + \ 1600 decoder->offset, \ 1601 (ref[0] + (pos_x >> 1) + \ 1602 ((pos_y op) + src_field) * decoder->stride), \ 1603 2 * decoder->stride, 8); \ 1604 \ 1605 if (MPEG2_COLOR) \ 1606 { \ 1607 motion_x /= 2; \ 1608 motion_y /= 2; \ 1609 xy_half = ((motion_y & 1) << 1) | (motion_x & 1); \ 1610 offset = ((decoder->offset + motion_x) >> 1) + \ 1611 (((decoder->v_offset >> 1) + (motion_y op) + src_field) * \ 1612 decoder->uv_stride); \ 1613 \ 1614 table[4+xy_half] (decoder->dest[1] + dest_field * decoder->uv_stride + \ 1615 (decoder->offset >> 1), ref[1] + offset, \ 1616 2 * decoder->uv_stride, 4); \ 1617 table[4+xy_half] (decoder->dest[2] + dest_field * decoder->uv_stride + \ 1618 (decoder->offset >> 1), ref[2] + offset, \ 1619 2 * decoder->uv_stride, 4); \ 1620 } 1621 1622#define MOTION_DMV_420(table, ref, motion_x, motion_y) \ 1623 pos_x = 2 * decoder->offset + motion_x; \ 1624 pos_y = decoder->v_offset + motion_y; \ 1625 \ 1626 if (unlikely (pos_x > decoder->limit_x)) \ 1627 { \ 1628 pos_x = ((int)pos_x < 0) ? 0 : decoder->limit_x; \ 1629 motion_x = pos_x - 2 * decoder->offset; \ 1630 } \ 1631 \ 1632 if (unlikely (pos_y > decoder->limit_y)) \ 1633 { \ 1634 pos_y = ((int)pos_y < 0) ? 0 : decoder->limit_y; \ 1635 motion_y = pos_y - decoder->v_offset; \ 1636 } \ 1637 \ 1638 xy_half = ((pos_y & 1) << 1) | (pos_x & 1); \ 1639 offset = (pos_x >> 1) + (pos_y & ~1) * decoder->stride; \ 1640 table[xy_half] (decoder->dest[0] + decoder->offset, \ 1641 ref[0] + offset, 2 * decoder->stride, 8); \ 1642 table[xy_half] (decoder->dest[0] + decoder->stride + decoder->offset, \ 1643 ref[0] + decoder->stride + offset, \ 1644 2 * decoder->stride, 8); \ 1645 \ 1646 if (MPEG2_COLOR) \ 1647 { \ 1648 motion_x /= 2; \ 1649 motion_y /= 2; \ 1650 xy_half = ((motion_y & 1) << 1) | (motion_x & 1); \ 1651 offset = ((decoder->offset + motion_x) >> 1) + \ 1652 (((decoder->v_offset >> 1) + (motion_y & ~1)) * \ 1653 decoder->uv_stride); \ 1654 \ 1655 table[4+xy_half] (decoder->dest[1] + (decoder->offset >> 1), \ 1656 ref[1] + offset, 2 * decoder->uv_stride, 4); \ 1657 table[4+xy_half] (decoder->dest[1] + decoder->uv_stride + \ 1658 (decoder->offset >> 1), \ 1659 ref[1] + decoder->uv_stride + offset, \ 1660 2 * decoder->uv_stride, 4); \ 1661 table[4+xy_half] (decoder->dest[2] + (decoder->offset >> 1), \ 1662 ref[2] + offset, 2 * decoder->uv_stride, 4); \ 1663 table[4+xy_half] (decoder->dest[2] + decoder->uv_stride + \ 1664 (decoder->offset >> 1), \ 1665 ref[2] + decoder->uv_stride + offset, \ 1666 2 * decoder->uv_stride, 4); \ 1667 } 1668 1669#define MOTION_ZERO_420(table, ref) \ 1670 table[0] (decoder->dest[0] + decoder->offset, \ 1671 (ref[0] + decoder->offset + \ 1672 decoder->v_offset * decoder->stride), decoder->stride, 16); \ 1673 \ 1674 if (MPEG2_COLOR) \ 1675 { \ 1676 offset = ((decoder->offset >> 1) + \ 1677 (decoder->v_offset >> 1) * decoder->uv_stride); \ 1678 \ 1679 table[4] (decoder->dest[1] + (decoder->offset >> 1), \ 1680 ref[1] + offset, decoder->uv_stride, 8); \ 1681 table[4] (decoder->dest[2] + (decoder->offset >> 1), \ 1682 ref[2] + offset, decoder->uv_stride, 8); \ 1683 } 1684 1685#define MOTION_422(table, ref, motion_x, motion_y, size, y) \ 1686 pos_x = 2 * decoder->offset + motion_x; \ 1687 pos_y = 2 * decoder->v_offset + motion_y + 2 * y; \ 1688 \ 1689 if (unlikely (pos_x > decoder->limit_x)) \ 1690 { \ 1691 pos_x = ((int)pos_x < 0) ? 0 : decoder->limit_x; \ 1692 motion_x = pos_x - 2 * decoder->offset; \ 1693 } \ 1694 \ 1695 if (unlikely (pos_y > decoder->limit_y_ ## size)) \ 1696 { \ 1697 pos_y = ((int)pos_y < 0) ? 0 : decoder->limit_y_ ## size; \ 1698 motion_y = pos_y - 2 * decoder->v_offset - 2 * y; \ 1699 } \ 1700 \ 1701 xy_half = ((pos_y & 1) << 1) | (pos_x & 1); \ 1702 offset = (pos_x >> 1) + (pos_y >> 1) * decoder->stride; \ 1703 \ 1704 table[xy_half] (decoder->dest[0] + y * decoder->stride + decoder->offset, \ 1705 ref[0] + offset, decoder->stride, size); \ 1706 \ 1707 if (MPEG2_COLOR) \ 1708 { \ 1709 offset = (offset + (motion_x & (motion_x < 0))) >> 1; \ 1710 motion_x /= 2; \ 1711 xy_half = ((pos_y & 1) << 1) | (motion_x & 1); \ 1712 \ 1713 table[4+xy_half] (decoder->dest[1] + y * decoder->uv_stride + \ 1714 (decoder->offset >> 1), ref[1] + offset, \ 1715 decoder->uv_stride, size); \ 1716 table[4+xy_half] (decoder->dest[2] + y * decoder->uv_stride + \ 1717 (decoder->offset >> 1), ref[2] + offset, \ 1718 decoder->uv_stride, size); \ 1719 } 1720 1721#define MOTION_FIELD_422(table, ref, motion_x, motion_y, \ 1722 dest_field, op, src_field) \ 1723 pos_x = 2 * decoder->offset + motion_x; \ 1724 pos_y = decoder->v_offset + motion_y; \ 1725 \ 1726 if (unlikely (pos_x > decoder->limit_x)) \ 1727 { \ 1728 pos_x = ((int)pos_x < 0) ? 0 : decoder->limit_x; \ 1729 motion_x = pos_x - 2 * decoder->offset; \ 1730 } \ 1731 \ 1732 if (unlikely (pos_y > decoder->limit_y)) \ 1733 { \ 1734 pos_y = ((int)pos_y < 0) ? 0 : decoder->limit_y; \ 1735 motion_y = pos_y - decoder->v_offset; \ 1736 } \ 1737 \ 1738 xy_half = ((pos_y & 1) << 1) | (pos_x & 1); \ 1739 offset = (pos_x >> 1) + ((pos_y op) + src_field) * decoder->stride; \ 1740 \ 1741 table[xy_half] (decoder->dest[0] + dest_field * decoder->stride + \ 1742 decoder->offset, ref[0] + offset, \ 1743 2 * decoder->stride, 8); \ 1744 \ 1745 if (MPEG2_COLOR) \ 1746 { \ 1747 offset = (offset + (motion_x & (motion_x < 0))) >> 1; \ 1748 motion_x /= 2; \ 1749 xy_half = ((pos_y & 1) << 1) | (motion_x & 1); \ 1750 \ 1751 table[4+xy_half] (decoder->dest[1] + dest_field * decoder->uv_stride + \ 1752 (decoder->offset >> 1), ref[1] + offset, \ 1753 2 * decoder->uv_stride, 8); \ 1754 table[4+xy_half] (decoder->dest[2] + dest_field * decoder->uv_stride + \ 1755 (decoder->offset >> 1), ref[2] + offset, \ 1756 2 * decoder->uv_stride, 8); \ 1757 } 1758 1759#define MOTION_DMV_422(table, ref, motion_x, motion_y) \ 1760 pos_x = 2 * decoder->offset + motion_x; \ 1761 pos_y = decoder->v_offset + motion_y; \ 1762 \ 1763 if (unlikely (pos_x > decoder->limit_x)) \ 1764 { \ 1765 pos_x = ((int)pos_x < 0) ? 0 : decoder->limit_x; \ 1766 motion_x = pos_x - 2 * decoder->offset; \ 1767 } \ 1768 \ 1769 if (unlikely (pos_y > decoder->limit_y)) \ 1770 { \ 1771 pos_y = ((int)pos_y < 0) ? 0 : decoder->limit_y; \ 1772 motion_y = pos_y - decoder->v_offset; \ 1773 } \ 1774 \ 1775 xy_half = ((pos_y & 1) << 1) | (pos_x & 1); \ 1776 offset = (pos_x >> 1) + (pos_y & ~1) * decoder->stride; \ 1777 \ 1778 table[xy_half] (decoder->dest[0] + decoder->offset, \ 1779 ref[0] + offset, 2 * decoder->stride, 8); \ 1780 table[xy_half] (decoder->dest[0] + decoder->stride + decoder->offset, \ 1781 ref[0] + decoder->stride + offset, \ 1782 2 * decoder->stride, 8); \ 1783 \ 1784 if (MPEG2_COLOR) \ 1785 { \ 1786 offset = (offset + (motion_x & (motion_x < 0))) >> 1; \ 1787 motion_x /= 2; \ 1788 xy_half = ((pos_y & 1) << 1) | (motion_x & 1); \ 1789 \ 1790 table[4+xy_half] (decoder->dest[1] + (decoder->offset >> 1), \ 1791 ref[1] + offset, 2 * decoder->uv_stride, 8); \ 1792 table[4+xy_half] (decoder->dest[1] + decoder->uv_stride + \ 1793 (decoder->offset >> 1), \ 1794 ref[1] + decoder->uv_stride + offset, \ 1795 2 * decoder->uv_stride, 8); \ 1796 table[4+xy_half] (decoder->dest[2] + (decoder->offset >> 1), \ 1797 ref[2] + offset, 2 * decoder->uv_stride, 8); \ 1798 table[4+xy_half] (decoder->dest[2] + decoder->uv_stride + \ 1799 (decoder->offset >> 1), \ 1800 ref[2] + decoder->uv_stride + offset, \ 1801 2 * decoder->uv_stride, 8); \ 1802 } 1803 1804#define MOTION_ZERO_422(table, ref) \ 1805 offset = decoder->offset + decoder->v_offset * decoder->stride; \ 1806 table[0] (decoder->dest[0] + decoder->offset, \ 1807 ref[0] + offset, decoder->stride, 16); \ 1808 \ 1809 if (MPEG2_COLOR) \ 1810 { \ 1811 offset >>= 1; \ 1812 table[4] (decoder->dest[1] + (decoder->offset >> 1), \ 1813 ref[1] + offset, decoder->uv_stride, 16); \ 1814 table[4] (decoder->dest[2] + (decoder->offset >> 1), \ 1815 ref[2] + offset, decoder->uv_stride, 16); \ 1816 } 1817 1818#define MOTION_444(table, ref, motion_x, motion_y, size, y) \ 1819 pos_x = 2 * decoder->offset + motion_x; \ 1820 pos_y = 2 * decoder->v_offset + motion_y + 2 * y; \ 1821 \ 1822 if (unlikely (pos_x > decoder->limit_x)) \ 1823 { \ 1824 pos_x = ((int)pos_x < 0) ? 0 : decoder->limit_x; \ 1825 motion_x = pos_x - 2 * decoder->offset; \ 1826 } \ 1827 \ 1828 if (unlikely (pos_y > decoder->limit_y_ ## size)) \ 1829 { \ 1830 pos_y = ((int)pos_y < 0) ? 0 : decoder->limit_y_ ## size; \ 1831 motion_y = pos_y - 2 * decoder->v_offset - 2 * y; \ 1832 } \ 1833 \ 1834 xy_half = ((pos_y & 1) << 1) | (pos_x & 1); \ 1835 offset = (pos_x >> 1) + (pos_y >> 1) * decoder->stride; \ 1836 \ 1837 table[xy_half] (decoder->dest[0] + y * decoder->stride + decoder->offset, \ 1838 ref[0] + offset, decoder->stride, size); \ 1839 \ 1840 if (MPEG2_COLOR) \ 1841 { \ 1842 table[xy_half] (decoder->dest[1] + y * decoder->stride + decoder->offset, \ 1843 ref[1] + offset, decoder->stride, size); \ 1844 table[xy_half] (decoder->dest[2] + y * decoder->stride + decoder->offset, \ 1845 ref[2] + offset, decoder->stride, size); \ 1846 } 1847 1848#define MOTION_FIELD_444(table, ref, motion_x, motion_y, \ 1849 dest_field, op, src_field) \ 1850 pos_x = 2 * decoder->offset + motion_x; \ 1851 pos_y = decoder->v_offset + motion_y; \ 1852 \ 1853 if (unlikely (pos_x > decoder->limit_x)) \ 1854 { \ 1855 pos_x = ((int)pos_x < 0) ? 0 : decoder->limit_x; \ 1856 motion_x = pos_x - 2 * decoder->offset; \ 1857 } \ 1858 \ 1859 if (unlikely (pos_y > decoder->limit_y)) \ 1860 { \ 1861 pos_y = ((int)pos_y < 0) ? 0 : decoder->limit_y; \ 1862 motion_y = pos_y - decoder->v_offset; \ 1863 } \ 1864 \ 1865 xy_half = ((pos_y & 1) << 1) | (pos_x & 1); \ 1866 offset = (pos_x >> 1) + ((pos_y op) + src_field) * decoder->stride; \ 1867 \ 1868 table[xy_half] (decoder->dest[0] + dest_field * decoder->stride + \ 1869 decoder->offset, ref[0] + offset, \ 1870 2 * decoder->stride, 8); \ 1871 \ 1872 if (MPEG2_COLOR) \ 1873 { \ 1874 table[xy_half] (decoder->dest[1] + dest_field * decoder->stride + \ 1875 decoder->offset, ref[1] + offset, \ 1876 2 * decoder->stride, 8); \ 1877 table[xy_half] (decoder->dest[2] + dest_field * decoder->stride + \ 1878 decoder->offset, ref[2] + offset, \ 1879 2 * decoder->stride, 8); \ 1880 } 1881 1882#define MOTION_DMV_444(table, ref, motion_x, motion_y) \ 1883 pos_x = 2 * decoder->offset + motion_x; \ 1884 pos_y = decoder->v_offset + motion_y; \ 1885 \ 1886 if (unlikely (pos_x > decoder->limit_x)) \ 1887 { \ 1888 pos_x = ((int)pos_x < 0) ? 0 : decoder->limit_x; \ 1889 motion_x = pos_x - 2 * decoder->offset; \ 1890 } \ 1891 \ 1892 if (unlikely (pos_y > decoder->limit_y)) \ 1893 { \ 1894 pos_y = ((int)pos_y < 0) ? 0 : decoder->limit_y; \ 1895 motion_y = pos_y - decoder->v_offset; \ 1896 } \ 1897 \ 1898 xy_half = ((pos_y & 1) << 1) | (pos_x & 1); \ 1899 offset = (pos_x >> 1) + (pos_y & ~1) * decoder->stride; \ 1900 \ 1901 table[xy_half] (decoder->dest[0] + decoder->offset, \ 1902 ref[0] + offset, 2 * decoder->stride, 8); \ 1903 table[xy_half] (decoder->dest[0] + decoder->stride + decoder->offset, \ 1904 ref[0] + decoder->stride + offset, \ 1905 2 * decoder->stride, 8); \ 1906 \ 1907 if (MPEG2_COLOR) \ 1908 { \ 1909 table[xy_half] (decoder->dest[1] + decoder->offset, \ 1910 ref[1] + offset, 2 * decoder->stride, 8); \ 1911 table[xy_half] (decoder->dest[1] + decoder->stride + decoder->offset, \ 1912 ref[1] + decoder->stride + offset, \ 1913 2 * decoder->stride, 8); \ 1914 table[xy_half] (decoder->dest[2] + decoder->offset, \ 1915 ref[2] + offset, 2 * decoder->stride, 8); \ 1916 table[xy_half] (decoder->dest[2] + decoder->stride + decoder->offset, \ 1917 ref[2] + decoder->stride + offset, \ 1918 2 * decoder->stride, 8); \ 1919 } 1920 1921#define MOTION_ZERO_444(table, ref) \ 1922 offset = decoder->offset + decoder->v_offset * decoder->stride; \ 1923 \ 1924 table[0] (decoder->dest[0] + decoder->offset, \ 1925 ref[0] + offset, decoder->stride, 16); \ 1926 \ 1927 if (MPEG2_COLOR) \ 1928 { \ 1929 table[4] (decoder->dest[1] + decoder->offset, \ 1930 ref[1] + offset, decoder->stride, 16); \ 1931 table[4] (decoder->dest[2] + decoder->offset, \ 1932 ref[2] + offset, decoder->stride, 16); \ 1933 } 1934 1935#define bit_buf (decoder->bitstream_buf) 1936#define bits (decoder->bitstream_bits) 1937#define bit_ptr (decoder->bitstream_ptr) 1938 1939static void motion_mp1 (mpeg2_decoder_t * const decoder, 1940 motion_t * const motion, 1941 mpeg2_mc_fct * const * const table) 1942{ 1943 int motion_x, motion_y; 1944 unsigned int pos_x, pos_y, xy_half, offset; 1945 1946 NEEDBITS (bit_buf, bits, bit_ptr); 1947 motion_x = motion->pmv[0][0] + 1948 (get_motion_delta (decoder, 1949 motion->f_code[0]) << motion->f_code[1]); 1950 motion_x = bound_motion_vector (motion_x, 1951 motion->f_code[0] + motion->f_code[1]); 1952 motion->pmv[0][0] = motion_x; 1953 1954 NEEDBITS (bit_buf, bits, bit_ptr); 1955 motion_y = motion->pmv[0][1] + 1956 (get_motion_delta (decoder, 1957 motion->f_code[0]) << motion->f_code[1]); 1958 motion_y = bound_motion_vector (motion_y, 1959 motion->f_code[0] + motion->f_code[1]); 1960 motion->pmv[0][1] = motion_y; 1961 1962 MOTION_420 (table, motion->ref[0], motion_x, motion_y, 16, 0); 1963} 1964 1965#define MOTION_FUNCTIONS(FORMAT, MOTION, MOTION_FIELD, \ 1966 MOTION_DMV, MOTION_ZERO) \ 1967 \ 1968static void motion_fr_frame_##FORMAT (mpeg2_decoder_t * const decoder, \ 1969 motion_t * const motion, \ 1970 mpeg2_mc_fct * const * const table) \ 1971{ \ 1972 int motion_x, motion_y; \ 1973 unsigned int pos_x, pos_y, xy_half, offset; \ 1974 \ 1975 NEEDBITS (bit_buf, bits, bit_ptr); \ 1976 motion_x = motion->pmv[0][0] + get_motion_delta (decoder, \ 1977 motion->f_code[0]); \ 1978 motion_x = bound_motion_vector (motion_x, motion->f_code[0]); \ 1979 motion->pmv[1][0] = motion->pmv[0][0] = motion_x; \ 1980 \ 1981 NEEDBITS (bit_buf, bits, bit_ptr); \ 1982 motion_y = motion->pmv[0][1] + get_motion_delta (decoder, \ 1983 motion->f_code[1]); \ 1984 motion_y = bound_motion_vector (motion_y, motion->f_code[1]); \ 1985 motion->pmv[1][1] = motion->pmv[0][1] = motion_y; \ 1986 \ 1987 MOTION (table, motion->ref[0], motion_x, motion_y, 16, 0); \ 1988} \ 1989 \ 1990static void motion_fr_field_##FORMAT (mpeg2_decoder_t * const decoder, \ 1991 motion_t * const motion, \ 1992 mpeg2_mc_fct * const * const table) \ 1993{ \ 1994 int motion_x, motion_y, field; \ 1995 unsigned int pos_x, pos_y, xy_half, offset; \ 1996 \ 1997 NEEDBITS (bit_buf, bits, bit_ptr); \ 1998 field = UBITS (bit_buf, 1); \ 1999 DUMPBITS (bit_buf, bits, 1); \ 2000 \ 2001 motion_x = motion->pmv[0][0] + get_motion_delta (decoder, \ 2002 motion->f_code[0]); \ 2003 motion_x = bound_motion_vector (motion_x, motion->f_code[0]); \ 2004 motion->pmv[0][0] = motion_x; \ 2005 \ 2006 NEEDBITS (bit_buf, bits, bit_ptr); \ 2007 motion_y = ((motion->pmv[0][1] >> 1) + \ 2008 get_motion_delta (decoder, motion->f_code[1])); \ 2009 /* motion_y = bound_motion_vector (motion_y, motion->f_code[1]); */ \ 2010 motion->pmv[0][1] = motion_y << 1; \ 2011 \ 2012 MOTION_FIELD (table, motion->ref[0], motion_x, motion_y, 0, & ~1, field); \ 2013 \ 2014 NEEDBITS (bit_buf, bits, bit_ptr); \ 2015 field = UBITS (bit_buf, 1); \ 2016 DUMPBITS (bit_buf, bits, 1); \ 2017 \ 2018 motion_x = motion->pmv[1][0] + get_motion_delta (decoder, \ 2019 motion->f_code[0]); \ 2020 motion_x = bound_motion_vector (motion_x, motion->f_code[0]); \ 2021 motion->pmv[1][0] = motion_x; \ 2022 \ 2023 NEEDBITS (bit_buf, bits, bit_ptr); \ 2024 motion_y = ((motion->pmv[1][1] >> 1) + \ 2025 get_motion_delta (decoder, motion->f_code[1])); \ 2026 /* motion_y = bound_motion_vector (motion_y, motion->f_code[1]); */ \ 2027 motion->pmv[1][1] = motion_y << 1; \ 2028 \ 2029 MOTION_FIELD (table, motion->ref[0], motion_x, motion_y, 1, & ~1, field); \ 2030} \ 2031 \ 2032static void motion_fr_dmv_##FORMAT (mpeg2_decoder_t * const decoder, \ 2033 motion_t * const motion, \ 2034 mpeg2_mc_fct * const * const table) \ 2035{ \ 2036 int motion_x, motion_y, dmv_x, dmv_y, m, other_x, other_y; \ 2037 unsigned int pos_x, pos_y, xy_half, offset; \ 2038 \ 2039 (void)table; \ 2040 NEEDBITS (bit_buf, bits, bit_ptr); \ 2041 motion_x = motion->pmv[0][0] + get_motion_delta (decoder, \ 2042 motion->f_code[0]); \ 2043 motion_x = bound_motion_vector (motion_x, motion->f_code[0]); \ 2044 motion->pmv[1][0] = motion->pmv[0][0] = motion_x; \ 2045 NEEDBITS (bit_buf, bits, bit_ptr); \ 2046 dmv_x = get_dmv (decoder); \ 2047 \ 2048 motion_y = ((motion->pmv[0][1] >> 1) + \ 2049 get_motion_delta (decoder, motion->f_code[1])); \ 2050 /* motion_y = bound_motion_vector (motion_y, motion->f_code[1]); */ \ 2051 motion->pmv[1][1] = motion->pmv[0][1] = motion_y << 1; \ 2052 dmv_y = get_dmv (decoder); \ 2053 \ 2054 m = decoder->top_field_first ? 1 : 3; \ 2055 other_x = ((motion_x * m + (motion_x > 0)) >> 1) + dmv_x; \ 2056 other_y = ((motion_y * m + (motion_y > 0)) >> 1) + dmv_y - 1; \ 2057 MOTION_FIELD (mpeg2_mc.put, motion->ref[0], other_x, other_y, 0, | 1, 0); \ 2058 \ 2059 m = decoder->top_field_first ? 3 : 1; \ 2060 other_x = ((motion_x * m + (motion_x > 0)) >> 1) + dmv_x; \ 2061 other_y = ((motion_y * m + (motion_y > 0)) >> 1) + dmv_y + 1; \ 2062 MOTION_FIELD (mpeg2_mc.put, motion->ref[0], other_x, other_y, 1, & ~1, 0);\ 2063 \ 2064 MOTION_DMV (mpeg2_mc.avg, motion->ref[0], motion_x, motion_y); \ 2065} \ 2066 \ 2067static void motion_reuse_##FORMAT (mpeg2_decoder_t * const decoder, \ 2068 motion_t * const motion, \ 2069 mpeg2_mc_fct * const * const table) \ 2070{ \ 2071 int motion_x, motion_y; \ 2072 unsigned int pos_x, pos_y, xy_half, offset; \ 2073 \ 2074 motion_x = motion->pmv[0][0]; \ 2075 motion_y = motion->pmv[0][1]; \ 2076 \ 2077 MOTION (table, motion->ref[0], motion_x, motion_y, 16, 0); \ 2078} \ 2079 \ 2080static void motion_zero_##FORMAT (mpeg2_decoder_t * const decoder, \ 2081 motion_t * const motion, \ 2082 mpeg2_mc_fct * const * const table) \ 2083{ \ 2084 unsigned int offset; \ 2085 \ 2086 motion->pmv[0][0] = motion->pmv[0][1] = 0; \ 2087 motion->pmv[1][0] = motion->pmv[1][1] = 0; \ 2088 \ 2089 MOTION_ZERO (table, motion->ref[0]); \ 2090} \ 2091 \ 2092static void motion_fi_field_##FORMAT (mpeg2_decoder_t * const decoder, \ 2093 motion_t * const motion, \ 2094 mpeg2_mc_fct * const * const table) \ 2095{ \ 2096 int motion_x, motion_y; \ 2097 uint8_t ** ref_field; \ 2098 unsigned int pos_x, pos_y, xy_half, offset; \ 2099 \ 2100 NEEDBITS (bit_buf, bits, bit_ptr); \ 2101 ref_field = motion->ref2[UBITS (bit_buf, 1)]; \ 2102 DUMPBITS (bit_buf, bits, 1); \ 2103 \ 2104 motion_x = motion->pmv[0][0] + get_motion_delta (decoder, \ 2105 motion->f_code[0]); \ 2106 motion_x = bound_motion_vector (motion_x, motion->f_code[0]); \ 2107 motion->pmv[1][0] = motion->pmv[0][0] = motion_x; \ 2108 \ 2109 NEEDBITS (bit_buf, bits, bit_ptr); \ 2110 motion_y = motion->pmv[0][1] + get_motion_delta (decoder, \ 2111 motion->f_code[1]); \ 2112 motion_y = bound_motion_vector (motion_y, motion->f_code[1]); \ 2113 motion->pmv[1][1] = motion->pmv[0][1] = motion_y; \ 2114 \ 2115 MOTION (table, ref_field, motion_x, motion_y, 16, 0); \ 2116} \ 2117 \ 2118static void motion_fi_16x8_##FORMAT (mpeg2_decoder_t * const decoder, \ 2119 motion_t * const motion, \ 2120 mpeg2_mc_fct * const * const table) \ 2121{ \ 2122 int motion_x, motion_y; \ 2123 uint8_t ** ref_field; \ 2124 unsigned int pos_x, pos_y, xy_half, offset; \ 2125 \ 2126 NEEDBITS (bit_buf, bits, bit_ptr); \ 2127 ref_field = motion->ref2[UBITS (bit_buf, 1)]; \ 2128 DUMPBITS (bit_buf, bits, 1); \ 2129 \ 2130 motion_x = motion->pmv[0][0] + get_motion_delta (decoder, \ 2131 motion->f_code[0]); \ 2132 motion_x = bound_motion_vector (motion_x, motion->f_code[0]); \ 2133 motion->pmv[0][0] = motion_x; \ 2134 \ 2135 NEEDBITS (bit_buf, bits, bit_ptr); \ 2136 motion_y = motion->pmv[0][1] + get_motion_delta (decoder, \ 2137 motion->f_code[1]); \ 2138 motion_y = bound_motion_vector (motion_y, motion->f_code[1]); \ 2139 motion->pmv[0][1] = motion_y; \ 2140 \ 2141 MOTION (table, ref_field, motion_x, motion_y, 8, 0); \ 2142 \ 2143 NEEDBITS (bit_buf, bits, bit_ptr); \ 2144 ref_field = motion->ref2[UBITS (bit_buf, 1)]; \ 2145 DUMPBITS (bit_buf, bits, 1); \ 2146 \ 2147 motion_x = motion->pmv[1][0] + get_motion_delta (decoder, \ 2148 motion->f_code[0]); \ 2149 motion_x = bound_motion_vector (motion_x, motion->f_code[0]); \ 2150 motion->pmv[1][0] = motion_x; \ 2151 \ 2152 NEEDBITS (bit_buf, bits, bit_ptr); \ 2153 motion_y = motion->pmv[1][1] + get_motion_delta (decoder, \ 2154 motion->f_code[1]); \ 2155 motion_y = bound_motion_vector (motion_y, motion->f_code[1]); \ 2156 motion->pmv[1][1] = motion_y; \ 2157 \ 2158 MOTION (table, ref_field, motion_x, motion_y, 8, 8); \ 2159} \ 2160 \ 2161static void motion_fi_dmv_##FORMAT (mpeg2_decoder_t * const decoder, \ 2162 motion_t * const motion, \ 2163 mpeg2_mc_fct * const * const table) \ 2164{ \ 2165 int motion_x, motion_y, other_x, other_y; \ 2166 unsigned int pos_x, pos_y, xy_half, offset; \ 2167 \ 2168 (void)table; \ 2169 NEEDBITS (bit_buf, bits, bit_ptr); \ 2170 motion_x = motion->pmv[0][0] + get_motion_delta (decoder, \ 2171 motion->f_code[0]); \ 2172 motion_x = bound_motion_vector (motion_x, motion->f_code[0]); \ 2173 motion->pmv[1][0] = motion->pmv[0][0] = motion_x; \ 2174 NEEDBITS (bit_buf, bits, bit_ptr); \ 2175 other_x = ((motion_x + (motion_x > 0)) >> 1) + get_dmv (decoder); \ 2176 \ 2177 motion_y = motion->pmv[0][1] + get_motion_delta (decoder, \ 2178 motion->f_code[1]); \ 2179 motion_y = bound_motion_vector (motion_y, motion->f_code[1]); \ 2180 motion->pmv[1][1] = motion->pmv[0][1] = motion_y; \ 2181 other_y = (((motion_y + (motion_y > 0)) >> 1) + get_dmv (decoder) + \ 2182 decoder->dmv_offset); \ 2183 \ 2184 MOTION (mpeg2_mc.put, motion->ref[0], motion_x, motion_y, 16, 0); \ 2185 MOTION (mpeg2_mc.avg, motion->ref[1], other_x, other_y, 16, 0); \ 2186} \ 2187 2188MOTION_FUNCTIONS (420, MOTION_420, MOTION_FIELD_420, MOTION_DMV_420, 2189 MOTION_ZERO_420) 2190MOTION_FUNCTIONS (422, MOTION_422, MOTION_FIELD_422, MOTION_DMV_422, 2191 MOTION_ZERO_422) 2192MOTION_FUNCTIONS (444, MOTION_444, MOTION_FIELD_444, MOTION_DMV_444, 2193 MOTION_ZERO_444) 2194 2195/* like motion_frame, but parsing without actual motion compensation */ 2196static void motion_fr_conceal (mpeg2_decoder_t * const decoder) 2197{ 2198 int tmp; 2199 2200 NEEDBITS (bit_buf, bits, bit_ptr); 2201 tmp = (decoder->f_motion.pmv[0][0] + 2202 get_motion_delta (decoder, decoder->f_motion.f_code[0])); 2203 tmp = bound_motion_vector (tmp, decoder->f_motion.f_code[0]); 2204 decoder->f_motion.pmv[1][0] = decoder->f_motion.pmv[0][0] = tmp; 2205 2206 NEEDBITS (bit_buf, bits, bit_ptr); 2207 tmp = (decoder->f_motion.pmv[0][1] + 2208 get_motion_delta (decoder, decoder->f_motion.f_code[1])); 2209 tmp = bound_motion_vector (tmp, decoder->f_motion.f_code[1]); 2210 decoder->f_motion.pmv[1][1] = decoder->f_motion.pmv[0][1] = tmp; 2211 2212 DUMPBITS (bit_buf, bits, 1); /* remove marker_bit */ 2213} 2214 2215static void motion_fi_conceal (mpeg2_decoder_t * const decoder) 2216{ 2217 int tmp; 2218 2219 NEEDBITS (bit_buf, bits, bit_ptr); 2220 DUMPBITS (bit_buf, bits, 1); /* remove field_select */ 2221 2222 tmp = decoder->f_motion.pmv[0][0] + 2223 get_motion_delta (decoder, decoder->f_motion.f_code[0]); 2224 tmp = bound_motion_vector (tmp, decoder->f_motion.f_code[0]); 2225 2226 decoder->f_motion.pmv[1][0] = 2227 decoder->f_motion.pmv[0][0] = tmp; 2228 2229 NEEDBITS (bit_buf, bits, bit_ptr); 2230 2231 tmp = (decoder->f_motion.pmv[0][1] + 2232 get_motion_delta (decoder, decoder->f_motion.f_code[1])); 2233 tmp = bound_motion_vector (tmp, decoder->f_motion.f_code[1]); 2234 2235 decoder->f_motion.pmv[1][1] = 2236 decoder->f_motion.pmv[0][1] = tmp; 2237 2238 DUMPBITS (bit_buf, bits, 1); /* remove marker_bit */ 2239} 2240 2241#undef bit_buf 2242#undef bits 2243#undef bit_ptr 2244 2245#define MOTION_CALL(routine, direction) \ 2246do { \ 2247 if ((direction) & MACROBLOCK_MOTION_FORWARD) \ 2248 routine (decoder, &decoder->f_motion, mpeg2_mc.put); \ 2249 \ 2250 if ((direction) & MACROBLOCK_MOTION_BACKWARD) \ 2251 { \ 2252 routine (decoder, &decoder->b_motion, \ 2253 ((direction) & MACROBLOCK_MOTION_FORWARD ? \ 2254 mpeg2_mc.avg : mpeg2_mc.put)); \ 2255 } \ 2256} while (0) 2257 2258#define NEXT_MACROBLOCK \ 2259do { \ 2260 decoder->offset += 16; \ 2261 \ 2262 if (decoder->offset == decoder->width) \ 2263 { \ 2264 do { /* just so we can use the break statement */ \ 2265 if (decoder->convert) \ 2266 { \ 2267 decoder->convert (decoder->convert_id, decoder->dest, \ 2268 decoder->v_offset); \ 2269 if (decoder->coding_type == B_TYPE) \ 2270 break; \ 2271 } \ 2272 \ 2273 decoder->dest[0] += decoder->slice_stride; \ 2274 if (MPEG2_COLOR) \ 2275 { \ 2276 decoder->dest[1] += decoder->slice_uv_stride; \ 2277 decoder->dest[2] += decoder->slice_uv_stride; \ 2278 } \ 2279 } while (0); \ 2280 \ 2281 decoder->v_offset += 16; \ 2282 \ 2283 if (decoder->v_offset > decoder->limit_y) \ 2284 return; \ 2285 \ 2286 decoder->offset = 0; \ 2287 } \ 2288} while (0) 2289 2290void mpeg2_init_fbuf (mpeg2_decoder_t * decoder, 2291 uint8_t * current_fbuf[MPEG2_COMPONENTS], 2292 uint8_t * forward_fbuf[MPEG2_COMPONENTS], 2293 uint8_t * backward_fbuf[MPEG2_COMPONENTS]) 2294{ 2295 int offset, stride, height, bottom_field; 2296 2297 stride = decoder->stride_frame; 2298 bottom_field = (decoder->picture_structure == BOTTOM_FIELD); 2299 offset = bottom_field ? stride : 0; 2300 height = decoder->height; 2301 2302 decoder->picture_dest[0] = current_fbuf[0] + offset; 2303#if MPEG2_COLOR 2304 decoder->picture_dest[1] = current_fbuf[1] + (offset >> 1); 2305 decoder->picture_dest[2] = current_fbuf[2] + (offset >> 1); 2306#endif 2307 2308 decoder->f_motion.ref[0][0] = forward_fbuf[0] + offset; 2309#if MPEG2_COLOR 2310 decoder->f_motion.ref[0][1] = forward_fbuf[1] + (offset >> 1); 2311 decoder->f_motion.ref[0][2] = forward_fbuf[2] + (offset >> 1); 2312#endif 2313 2314 decoder->b_motion.ref[0][0] = backward_fbuf[0] + offset; 2315#if MPEG2_COLOR 2316 decoder->b_motion.ref[0][1] = backward_fbuf[1] + (offset >> 1); 2317 decoder->b_motion.ref[0][2] = backward_fbuf[2] + (offset >> 1); 2318#endif 2319 2320 if (decoder->picture_structure != FRAME_PICTURE) 2321 { 2322 decoder->dmv_offset = bottom_field ? 1 : -1; 2323 decoder->f_motion.ref2[0] = decoder->f_motion.ref[bottom_field]; 2324 decoder->f_motion.ref2[1] = decoder->f_motion.ref[!bottom_field]; 2325 decoder->b_motion.ref2[0] = decoder->b_motion.ref[bottom_field]; 2326 decoder->b_motion.ref2[1] = decoder->b_motion.ref[!bottom_field]; 2327 offset = stride - offset; 2328 2329 if (decoder->second_field && (decoder->coding_type != B_TYPE)) 2330 forward_fbuf = current_fbuf; 2331 2332 decoder->f_motion.ref[1][0] = forward_fbuf[0] + offset; 2333#if MPEG2_COLOR 2334 decoder->f_motion.ref[1][1] = forward_fbuf[1] + (offset >> 1); 2335 decoder->f_motion.ref[1][2] = forward_fbuf[2] + (offset >> 1); 2336#endif 2337 decoder->b_motion.ref[1][0] = backward_fbuf[0] + offset; 2338#if MPEG2_COLOR 2339 decoder->b_motion.ref[1][1] = backward_fbuf[1] + (offset >> 1); 2340 decoder->b_motion.ref[1][2] = backward_fbuf[2] + (offset >> 1); 2341#endif 2342 stride <<= 1; 2343 height >>= 1; 2344 } 2345 2346 decoder->stride = stride; 2347 decoder->slice_stride = 16 * stride; 2348#if MPEG2_COLOR 2349 decoder->uv_stride = stride >> 1; 2350 decoder->slice_uv_stride = 2351 decoder->slice_stride >> (2 - decoder->chroma_format); 2352#endif 2353 decoder->limit_x = 2 * decoder->width - 32; 2354 decoder->limit_y_16 = 2 * height - 32; 2355 decoder->limit_y_8 = 2 * height - 16; 2356 decoder->limit_y = height - 16; 2357 2358 if (decoder->mpeg1) 2359 { 2360 decoder->motion_parser[0] = motion_zero_420; 2361 decoder->motion_parser[MC_FRAME] = motion_mp1; 2362 decoder->motion_parser[4] = motion_reuse_420; 2363 } 2364 else if (decoder->picture_structure == FRAME_PICTURE) 2365 { 2366 if (decoder->chroma_format == 0) 2367 { 2368 decoder->motion_parser[0] = motion_zero_420; 2369 decoder->motion_parser[MC_FIELD] = motion_fr_field_420; 2370 decoder->motion_parser[MC_FRAME] = motion_fr_frame_420; 2371 decoder->motion_parser[MC_DMV] = motion_fr_dmv_420; 2372 decoder->motion_parser[4] = motion_reuse_420; 2373 } 2374 else if (decoder->chroma_format == 1) 2375 { 2376 decoder->motion_parser[0] = motion_zero_422; 2377 decoder->motion_parser[MC_FIELD] = motion_fr_field_422; 2378 decoder->motion_parser[MC_FRAME] = motion_fr_frame_422; 2379 decoder->motion_parser[MC_DMV] = motion_fr_dmv_422; 2380 decoder->motion_parser[4] = motion_reuse_422; 2381 } 2382 else 2383 { 2384 decoder->motion_parser[0] = motion_zero_444; 2385 decoder->motion_parser[MC_FIELD] = motion_fr_field_444; 2386 decoder->motion_parser[MC_FRAME] = motion_fr_frame_444; 2387 decoder->motion_parser[MC_DMV] = motion_fr_dmv_444; 2388 decoder->motion_parser[4] = motion_reuse_444; 2389 } 2390 } 2391 else 2392 { 2393 if (decoder->chroma_format == 0) 2394 { 2395 decoder->motion_parser[0] = motion_zero_420; 2396 decoder->motion_parser[MC_FIELD] = motion_fi_field_420; 2397 decoder->motion_parser[MC_16X8] = motion_fi_16x8_420; 2398 decoder->motion_parser[MC_DMV] = motion_fi_dmv_420; 2399 decoder->motion_parser[4] = motion_reuse_420; 2400 } 2401 else if (decoder->chroma_format == 1) 2402 { 2403 decoder->motion_parser[0] = motion_zero_422; 2404 decoder->motion_parser[MC_FIELD] = motion_fi_field_422; 2405 decoder->motion_parser[MC_16X8] = motion_fi_16x8_422; 2406 decoder->motion_parser[MC_DMV] = motion_fi_dmv_422; 2407 decoder->motion_parser[4] = motion_reuse_422; 2408 } 2409 else 2410 { 2411 decoder->motion_parser[0] = motion_zero_444; 2412 decoder->motion_parser[MC_FIELD] = motion_fi_field_444; 2413 decoder->motion_parser[MC_16X8] = motion_fi_16x8_444; 2414 decoder->motion_parser[MC_DMV] = motion_fi_dmv_444; 2415 decoder->motion_parser[4] = motion_reuse_444; 2416 } 2417 } 2418} 2419 2420static inline int slice_init (mpeg2_decoder_t * const decoder, int code) 2421{ 2422#define bit_buf (decoder->bitstream_buf) 2423#define bits (decoder->bitstream_bits) 2424#define bit_ptr (decoder->bitstream_ptr) 2425 2426 int offset; 2427 const MBAtab * mba; 2428 2429#if MPEG2_COLOR 2430 decoder->dc_dct_pred[0] = decoder->dc_dct_pred[1] = 2431 decoder->dc_dct_pred[2] = 16384; 2432#else 2433 decoder->dc_dct_pred[0] = 16384; 2434#endif 2435 2436 decoder->f_motion.pmv[0][0] = decoder->f_motion.pmv[0][1] = 0; 2437 decoder->f_motion.pmv[1][0] = decoder->f_motion.pmv[1][1] = 0; 2438 decoder->b_motion.pmv[0][0] = decoder->b_motion.pmv[0][1] = 0; 2439 decoder->b_motion.pmv[1][0] = decoder->b_motion.pmv[1][1] = 0; 2440 2441 if (decoder->vertical_position_extension) 2442 { 2443 code += UBITS (bit_buf, 3) << 7; 2444 DUMPBITS (bit_buf, bits, 3); 2445 } 2446 2447 decoder->v_offset = (code - 1) * 16; 2448 offset = 0; 2449 2450 if (!(decoder->convert) || decoder->coding_type != B_TYPE) 2451 { 2452 offset = (code - 1) * decoder->slice_stride; 2453 } 2454 2455 decoder->dest[0] = decoder->picture_dest[0] + offset; 2456#if MPEG2_COLOR 2457 offset >>= (2 - decoder->chroma_format); 2458 decoder->dest[1] = decoder->picture_dest[1] + offset; 2459 decoder->dest[2] = decoder->picture_dest[2] + offset; 2460#endif 2461 2462 get_quantizer_scale (decoder); 2463 2464 /* ignore intra_slice and all the extra data */ 2465 while (bit_buf & 0x80000000) 2466 { 2467 DUMPBITS (bit_buf, bits, 9); 2468 NEEDBITS (bit_buf, bits, bit_ptr); 2469 } 2470 2471 /* decode initial macroblock address increment */ 2472 offset = 0; 2473 while (1) 2474 { 2475 if (bit_buf >= 0x08000000) 2476 { 2477 mba = MBA_5 + (UBITS (bit_buf, 6) - 2); 2478 break; 2479 } 2480 else if (bit_buf >= 0x01800000) 2481 { 2482 mba = MBA_11 + (UBITS (bit_buf, 12) - 24); 2483 break; 2484 } 2485 else 2486 { 2487 switch (UBITS (bit_buf, 12)) 2488 { 2489 case 8: /* macroblock_escape */ 2490 offset += 33; 2491 DUMPBITS (bit_buf, bits, 11); 2492 NEEDBITS (bit_buf, bits, bit_ptr); 2493 continue; 2494 case 15: /* macroblock_stuffing (MPEG1 only) */ 2495 bit_buf &= 0xfffff; 2496 DUMPBITS (bit_buf, bits, 11); 2497 NEEDBITS (bit_buf, bits, bit_ptr); 2498 continue; 2499 default: /* error */ 2500 return 1; 2501 } 2502 } 2503 } 2504 2505 DUMPBITS (bit_buf, bits, mba->len + 1); 2506 decoder->offset = (offset + mba->mba) << 4; 2507 2508 while (decoder->offset - decoder->width >= 0) 2509 { 2510 decoder->offset -= decoder->width; 2511 2512 if (!(decoder->convert) || decoder->coding_type != B_TYPE) 2513 { 2514 decoder->dest[0] += decoder->slice_stride; 2515#if MPEG2_COLOR 2516 decoder->dest[1] += decoder->slice_uv_stride; 2517 decoder->dest[2] += decoder->slice_uv_stride; 2518#endif 2519 } 2520 2521 decoder->v_offset += 16; 2522 } 2523 2524 if (decoder->v_offset > decoder->limit_y) 2525 return 1; 2526 2527 return 0; 2528 2529#undef bit_buf 2530#undef bits 2531#undef bit_ptr 2532} 2533 2534void mpeg2_slice (mpeg2_decoder_t * const decoder, const int code, 2535 const uint8_t * const buffer) 2536{ 2537#define bit_buf (decoder->bitstream_buf) 2538#define bits (decoder->bitstream_bits) 2539#define bit_ptr (decoder->bitstream_ptr) 2540 2541 bitstream_init (decoder, buffer); 2542 2543 if (slice_init (decoder, code)) 2544 return; 2545 2546 while (1) 2547 { 2548 int macroblock_modes; 2549 int mba_inc; 2550 const MBAtab * mba; 2551 2552 NEEDBITS (bit_buf, bits, bit_ptr); 2553 2554 macroblock_modes = get_macroblock_modes (decoder); 2555 2556 /* maybe integrate MACROBLOCK_QUANT test into get_macroblock_modes ? */ 2557 if (macroblock_modes & MACROBLOCK_QUANT) 2558 get_quantizer_scale (decoder); 2559 2560 if (macroblock_modes & MACROBLOCK_INTRA) 2561 { 2562 int DCT_offset, DCT_stride; 2563 int offset; 2564 uint8_t * dest_y; 2565 2566 if (decoder->concealment_motion_vectors) 2567 { 2568 if (decoder->picture_structure == FRAME_PICTURE) 2569 motion_fr_conceal (decoder); 2570 else 2571 motion_fi_conceal (decoder); 2572 } 2573 else 2574 { 2575 decoder->f_motion.pmv[0][0] = decoder->f_motion.pmv[0][1] = 0; 2576 decoder->f_motion.pmv[1][0] = decoder->f_motion.pmv[1][1] = 0; 2577 decoder->b_motion.pmv[0][0] = decoder->b_motion.pmv[0][1] = 0; 2578 decoder->b_motion.pmv[1][0] = decoder->b_motion.pmv[1][1] = 0; 2579 } 2580 2581 if (macroblock_modes & DCT_TYPE_INTERLACED) 2582 { 2583 DCT_offset = decoder->stride; 2584 DCT_stride = decoder->stride * 2; 2585 } 2586 else 2587 { 2588 DCT_offset = decoder->stride * 8; 2589 DCT_stride = decoder->stride; 2590 } 2591 2592 offset = decoder->offset; 2593 dest_y = decoder->dest[0] + offset; 2594 slice_intra_DCT (decoder, 0, dest_y, DCT_stride); 2595 slice_intra_DCT (decoder, 0, dest_y + 8, DCT_stride); 2596 slice_intra_DCT (decoder, 0, dest_y + DCT_offset, DCT_stride); 2597 slice_intra_DCT (decoder, 0, dest_y + DCT_offset + 8, DCT_stride); 2598 2599#if MPEG2_COLOR 2600 if (likely (decoder->chroma_format == 0)) 2601 { 2602 slice_intra_DCT (decoder, 1, decoder->dest[1] + (offset >> 1), 2603 decoder->uv_stride); 2604 slice_intra_DCT (decoder, 2, decoder->dest[2] + (offset >> 1), 2605 decoder->uv_stride); 2606 2607 if (decoder->coding_type == D_TYPE) 2608 { 2609 NEEDBITS (bit_buf, bits, bit_ptr); 2610 DUMPBITS (bit_buf, bits, 1); 2611 } 2612 } 2613 else if (likely (decoder->chroma_format == 1)) 2614 { 2615 uint8_t * dest_u = decoder->dest[1] + (offset >> 1); 2616 uint8_t * dest_v = decoder->dest[2] + (offset >> 1); 2617 2618 DCT_stride >>= 1; 2619 DCT_offset >>= 1; 2620 2621 slice_intra_DCT (decoder, 1, dest_u, DCT_stride); 2622 slice_intra_DCT (decoder, 2, dest_v, DCT_stride); 2623 slice_intra_DCT (decoder, 1, dest_u + DCT_offset, DCT_stride); 2624 slice_intra_DCT (decoder, 2, dest_v + DCT_offset, DCT_stride); 2625 } 2626 else 2627 { 2628 uint8_t * dest_u = decoder->dest[1] + offset; 2629 uint8_t * dest_v = decoder->dest[2] + offset; 2630 2631 slice_intra_DCT (decoder, 1, dest_u, DCT_stride); 2632 slice_intra_DCT (decoder, 2, dest_v, DCT_stride); 2633 slice_intra_DCT (decoder, 1, dest_u + DCT_offset, DCT_stride); 2634 slice_intra_DCT (decoder, 2, dest_v + DCT_offset, DCT_stride); 2635 slice_intra_DCT (decoder, 1, dest_u + 8, DCT_stride); 2636 slice_intra_DCT (decoder, 2, dest_v + 8, DCT_stride); 2637 slice_intra_DCT (decoder, 1, dest_u + DCT_offset + 8, 2638 DCT_stride); 2639 slice_intra_DCT (decoder, 2, dest_v + DCT_offset + 8, 2640 DCT_stride); 2641 } 2642#else 2643 skip_chroma_intra(decoder); 2644#endif /* MPEG2_COLOR */ 2645 } 2646 else 2647 { 2648 motion_parser_t * parser; 2649 2650 parser = 2651 decoder->motion_parser[macroblock_modes >> MOTION_TYPE_SHIFT]; 2652 MOTION_CALL (parser, macroblock_modes); 2653 2654 if (macroblock_modes & MACROBLOCK_PATTERN) 2655 { 2656 int coded_block_pattern; 2657 int DCT_offset, DCT_stride; 2658 2659 if (macroblock_modes & DCT_TYPE_INTERLACED) 2660 { 2661 DCT_offset = decoder->stride; 2662 DCT_stride = decoder->stride * 2; 2663 } 2664 else 2665 { 2666 DCT_offset = decoder->stride * 8; 2667 DCT_stride = decoder->stride; 2668 } 2669 2670 coded_block_pattern = get_coded_block_pattern (decoder); 2671 2672 if (likely (decoder->chroma_format == 0)) 2673 { 2674 int offset = decoder->offset; 2675 uint8_t * dest_y = decoder->dest[0] + offset; 2676 2677 if (coded_block_pattern & 1) 2678 slice_non_intra_DCT (decoder, 0, dest_y, DCT_stride); 2679 2680 if (coded_block_pattern & 2) 2681 slice_non_intra_DCT (decoder, 0, dest_y + 8, 2682 DCT_stride); 2683 2684 if (coded_block_pattern & 4) 2685 slice_non_intra_DCT (decoder, 0, dest_y + DCT_offset, 2686 DCT_stride); 2687 2688 if (coded_block_pattern & 8) 2689 slice_non_intra_DCT (decoder, 0, 2690 dest_y + DCT_offset + 8, 2691 DCT_stride); 2692#if MPEG2_COLOR 2693 if (coded_block_pattern & 16) 2694 slice_non_intra_DCT (decoder, 1, 2695 decoder->dest[1] + (offset >> 1), 2696 decoder->uv_stride); 2697 2698 if (coded_block_pattern & 32) 2699 slice_non_intra_DCT (decoder, 2, 2700 decoder->dest[2] + (offset >> 1), 2701 decoder->uv_stride); 2702#endif /* MPEG2_COLOR */ 2703 } 2704 else if (likely (decoder->chroma_format == 1)) 2705 { 2706 int offset; 2707 uint8_t * dest_y; 2708 2709 coded_block_pattern |= bit_buf & (3 << 30); 2710 DUMPBITS (bit_buf, bits, 2); 2711 2712 offset = decoder->offset; 2713 dest_y = decoder->dest[0] + offset; 2714 2715 if (coded_block_pattern & 1) 2716 slice_non_intra_DCT (decoder, 0, dest_y, DCT_stride); 2717 2718 if (coded_block_pattern & 2) 2719 slice_non_intra_DCT (decoder, 0, dest_y + 8, 2720 DCT_stride); 2721 2722 if (coded_block_pattern & 4) 2723 slice_non_intra_DCT (decoder, 0, dest_y + DCT_offset, 2724 DCT_stride); 2725 2726 if (coded_block_pattern & 8) 2727 slice_non_intra_DCT (decoder, 0, 2728 dest_y + DCT_offset + 8, 2729 DCT_stride); 2730#if MPEG2_COLOR 2731 DCT_stride >>= 1; 2732 DCT_offset = (DCT_offset + offset) >> 1; 2733 2734 if (coded_block_pattern & 16) 2735 slice_non_intra_DCT (decoder, 1, 2736 decoder->dest[1] + (offset >> 1), 2737 DCT_stride); 2738 2739 if (coded_block_pattern & 32) 2740 slice_non_intra_DCT (decoder, 2, 2741 decoder->dest[2] + (offset >> 1), 2742 DCT_stride); 2743 2744 if (coded_block_pattern & (2 << 30)) 2745 slice_non_intra_DCT (decoder, 1, 2746 decoder->dest[1] + DCT_offset, 2747 DCT_stride); 2748 2749 if (coded_block_pattern & (1 << 30)) 2750 slice_non_intra_DCT (decoder, 2, 2751 decoder->dest[2] + DCT_offset, 2752 DCT_stride); 2753#endif /* MPEG2_COLOR */ 2754 } 2755 else 2756 { 2757 int offset = decoder->offset; 2758 uint8_t * dest_y = decoder->dest[0] + offset; 2759#if MPEG2_COLOR 2760 uint8_t * dest_u = decoder->dest[1] + offset; 2761 uint8_t * dest_v = decoder->dest[2] + offset; 2762#endif 2763 coded_block_pattern |= bit_buf & (63 << 26); 2764 DUMPBITS (bit_buf, bits, 6); 2765 2766 if (coded_block_pattern & 1) 2767 slice_non_intra_DCT (decoder, 0, dest_y, DCT_stride); 2768 2769 if (coded_block_pattern & 2) 2770 slice_non_intra_DCT (decoder, 0, dest_y + 8, 2771 DCT_stride); 2772 2773 if (coded_block_pattern & 4) 2774 slice_non_intra_DCT (decoder, 0, dest_y + DCT_offset, 2775 DCT_stride); 2776 2777 if (coded_block_pattern & 8) 2778 slice_non_intra_DCT (decoder, 0, 2779 dest_y + DCT_offset + 8, 2780 DCT_stride); 2781#if MPEG2_COLOR 2782 if (coded_block_pattern & 16) 2783 slice_non_intra_DCT (decoder, 1, dest_u, DCT_stride); 2784 2785 if (coded_block_pattern & 32) 2786 slice_non_intra_DCT (decoder, 2, dest_v, DCT_stride); 2787 2788 if (coded_block_pattern & (32 << 26)) 2789 slice_non_intra_DCT (decoder, 1, dest_u + DCT_offset, 2790 DCT_stride); 2791 2792 if (coded_block_pattern & (16 << 26)) 2793 slice_non_intra_DCT (decoder, 2, dest_v + DCT_offset, 2794 DCT_stride); 2795 2796 if (coded_block_pattern & (8 << 26)) 2797 slice_non_intra_DCT (decoder, 1, dest_u + 8, 2798 DCT_stride); 2799 2800 if (coded_block_pattern & (4 << 26)) 2801 slice_non_intra_DCT (decoder, 2, dest_v + 8, 2802 DCT_stride); 2803 2804 if (coded_block_pattern & (2 << 26)) 2805 slice_non_intra_DCT (decoder, 1, 2806 dest_u + DCT_offset + 8, 2807 DCT_stride); 2808 2809 if (coded_block_pattern & (1 << 26)) 2810 slice_non_intra_DCT (decoder, 2, 2811 dest_v + DCT_offset + 8, 2812 DCT_stride); 2813#endif /* MPEG2_COLOR */ 2814 } 2815#if !MPEG2_COLOR 2816 skip_chroma_non_intra(decoder, coded_block_pattern); 2817#endif 2818 } 2819 2820#if MPEG2_COLOR 2821 decoder->dc_dct_pred[0] = decoder->dc_dct_pred[1] = 2822 decoder->dc_dct_pred[2] = 16384; 2823#else 2824 decoder->dc_dct_pred[0] = 16384; 2825#endif 2826 } 2827 2828 NEXT_MACROBLOCK; 2829 2830 NEEDBITS (bit_buf, bits, bit_ptr); 2831 mba_inc = 0; 2832 2833 while (1) 2834 { 2835 if (bit_buf >= 0x10000000) 2836 { 2837 mba = MBA_5 + (UBITS (bit_buf, 5) - 2); 2838 break; 2839 } 2840 else if (bit_buf >= 0x03000000) 2841 { 2842 mba = MBA_11 + (UBITS (bit_buf, 11) - 24); 2843 break; 2844 } 2845 else 2846 { 2847 switch (UBITS (bit_buf, 11)) 2848 { 2849 case 8: /* macroblock_escape */ 2850 mba_inc += 33; 2851 /* pass through */ 2852 case 15: /* macroblock_stuffing (MPEG1 only) */ 2853 DUMPBITS (bit_buf, bits, 11); 2854 NEEDBITS (bit_buf, bits, bit_ptr); 2855 continue; 2856 default: /* end of slice, or error */ 2857 return; 2858 } 2859 } 2860 } 2861 2862 DUMPBITS (bit_buf, bits, mba->len); 2863 mba_inc += mba->mba; 2864 2865 if (mba_inc) 2866 { 2867#if MPEG2_COLOR 2868 decoder->dc_dct_pred[0] = decoder->dc_dct_pred[1] = 2869 decoder->dc_dct_pred[2] = 16384; 2870#else 2871 decoder->dc_dct_pred[0] = 16384; 2872#endif 2873 if (decoder->coding_type == P_TYPE) 2874 { 2875 do 2876 { 2877 MOTION_CALL (decoder->motion_parser[0], 2878 MACROBLOCK_MOTION_FORWARD); 2879 NEXT_MACROBLOCK; 2880 } 2881 while (--mba_inc); 2882 } 2883 else 2884 { 2885 do 2886 { 2887 MOTION_CALL (decoder->motion_parser[4], macroblock_modes); 2888 NEXT_MACROBLOCK; 2889 } 2890 while (--mba_inc); 2891 } 2892 } 2893 } 2894 2895#undef bit_buf 2896#undef bits 2897#undef bit_ptr 2898}