diff options
author | arpi_esp <arpi_esp@b3059339-0415-0410-9bf9-f77b7e298cf2> | 2001-02-24 20:28:24 +0000 |
---|---|---|
committer | arpi_esp <arpi_esp@b3059339-0415-0410-9bf9-f77b7e298cf2> | 2001-02-24 20:28:24 +0000 |
commit | d34041569e71fc9bd772354e94dc9d16061072a5 (patch) | |
tree | 8f481cae1c70f32d1756fbe5f39000577b73042d /libmpeg2/slice.c | |
parent | e95a95ece09bac96bdfd37322f96c6f57ef79ebc (diff) | |
download | mpv-d34041569e71fc9bd772354e94dc9d16061072a5.tar.bz2 mpv-d34041569e71fc9bd772354e94dc9d16061072a5.tar.xz |
Initial revision
git-svn-id: svn://svn.mplayerhq.hu/mplayer/trunk@2 b3059339-0415-0410-9bf9-f77b7e298cf2
Diffstat (limited to 'libmpeg2/slice.c')
-rw-r--r-- | libmpeg2/slice.c | 1797 |
1 files changed, 1797 insertions, 0 deletions
diff --git a/libmpeg2/slice.c b/libmpeg2/slice.c new file mode 100644 index 0000000000..13940b3387 --- /dev/null +++ b/libmpeg2/slice.c @@ -0,0 +1,1797 @@ +/* + * slice.c + * Copyright (C) 1999-2000 Aaron Holtzman <aholtzma@ess.engr.uvic.ca> + * + * This file is part of mpeg2dec, a free MPEG-2 video stream decoder. + * + * mpeg2dec is free software; you can redistribute it and/or modify + * it under the terms of the GNU General Public License as published by + * the Free Software Foundation; either version 2 of the License, or + * (at your option) any later version. + * + * mpeg2dec is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + * GNU General Public License for more details. + * + * You should have received a copy of the GNU General Public License + * along with this program; if not, write to the Free Software + * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA + */ + +#include "config.h" + +#include <string.h> +#include <inttypes.h> + +#include "mpeg2_internal.h" +#include "attributes.h" + +extern mc_functions_t mc_functions; +extern void (* idct_block_copy) (int16_t * block, uint8_t * dest, int stride); +extern void (* idct_block_add) (int16_t * block, uint8_t * dest, int stride); + +static int16_t DCTblock[64] ATTR_ALIGN(16); + +#include "vlc.h" + +static int non_linear_quantizer_scale [] = { + 0, 1, 2, 3, 4, 5, 6, 7, + 8, 10, 12, 14, 16, 18, 20, 22, + 24, 28, 32, 36, 40, 44, 48, 52, + 56, 64, 72, 80, 88, 96, 104, 112 +}; + +static inline int get_macroblock_modes (slice_t * slice, int picture_structure, + int picture_coding_type, + int frame_pred_frame_dct) +{ +#define bit_buf (slice->bitstream_buf) +#define bits (slice->bitstream_bits) +#define bit_ptr (slice->bitstream_ptr) + int macroblock_modes; + MBtab * tab; + + switch (picture_coding_type) { + case I_TYPE: + + tab = MB_I + UBITS (bit_buf, 1); + DUMPBITS (bit_buf, bits, tab->len); + macroblock_modes = tab->modes; + + if ((! frame_pred_frame_dct) && (picture_structure == FRAME_PICTURE)) { + macroblock_modes |= UBITS (bit_buf, 1) * DCT_TYPE_INTERLACED; + DUMPBITS (bit_buf, bits, 1); + } + + return macroblock_modes; + + case P_TYPE: + + tab = MB_P + UBITS (bit_buf, 5); + DUMPBITS (bit_buf, bits, tab->len); + macroblock_modes = tab->modes; + + if (picture_structure != FRAME_PICTURE) { + if (macroblock_modes & MACROBLOCK_MOTION_FORWARD) { + macroblock_modes |= UBITS (bit_buf, 2) * MOTION_TYPE_BASE; + DUMPBITS (bit_buf, bits, 2); + } + return macroblock_modes; + } else if (frame_pred_frame_dct) { + if (macroblock_modes & MACROBLOCK_MOTION_FORWARD) + macroblock_modes |= MC_FRAME; + return macroblock_modes; + } else { + if (macroblock_modes & MACROBLOCK_MOTION_FORWARD) { + macroblock_modes |= UBITS (bit_buf, 2) * MOTION_TYPE_BASE; + DUMPBITS (bit_buf, bits, 2); + } + if (macroblock_modes & (MACROBLOCK_INTRA | MACROBLOCK_PATTERN)) { + macroblock_modes |= UBITS (bit_buf, 1) * DCT_TYPE_INTERLACED; + DUMPBITS (bit_buf, bits, 1); + } + return macroblock_modes; + } + + case B_TYPE: + + tab = MB_B + UBITS (bit_buf, 6); + DUMPBITS (bit_buf, bits, tab->len); + macroblock_modes = tab->modes; + + if (picture_structure != FRAME_PICTURE) { + if (! (macroblock_modes & MACROBLOCK_INTRA)) { + macroblock_modes |= UBITS (bit_buf, 2) * MOTION_TYPE_BASE; + DUMPBITS (bit_buf, bits, 2); + } + return macroblock_modes; + } else if (frame_pred_frame_dct) { + //if (! (macroblock_modes & MACROBLOCK_INTRA)) + macroblock_modes |= MC_FRAME; + return macroblock_modes; + } else { + if (macroblock_modes & MACROBLOCK_INTRA) + goto intra; + macroblock_modes |= UBITS (bit_buf, 2) * MOTION_TYPE_BASE; + DUMPBITS (bit_buf, bits, 2); + if (macroblock_modes & (MACROBLOCK_INTRA | MACROBLOCK_PATTERN)) { + intra: + macroblock_modes |= UBITS (bit_buf, 1) * DCT_TYPE_INTERLACED; + DUMPBITS (bit_buf, bits, 1); + } + return macroblock_modes; + } + + case D_TYPE: + + DUMPBITS (bit_buf, bits, 1); + return MACROBLOCK_INTRA; + + default: + return 0; + } +#undef bit_buf +#undef bits +#undef bit_ptr +} + +static inline int get_quantizer_scale (slice_t * slice, int q_scale_type) +{ +#define bit_buf (slice->bitstream_buf) +#define bits (slice->bitstream_bits) +#define bit_ptr (slice->bitstream_ptr) + + int quantizer_scale_code; + + quantizer_scale_code = UBITS (bit_buf, 5); + DUMPBITS (bit_buf, bits, 5); + + if (q_scale_type) + return non_linear_quantizer_scale [quantizer_scale_code]; + else + return quantizer_scale_code << 1; +#undef bit_buf +#undef bits +#undef bit_ptr +} + +static inline int get_motion_delta (slice_t * slice, int f_code) +{ +#define bit_buf (slice->bitstream_buf) +#define bits (slice->bitstream_bits) +#define bit_ptr (slice->bitstream_ptr) + + int delta; + int sign; + MVtab * tab; + + if (bit_buf & 0x80000000) { + DUMPBITS (bit_buf, bits, 1); + return 0; + } else if (bit_buf >= 0x0c000000) { + + tab = MV_4 + UBITS (bit_buf, 4); + delta = (tab->delta << f_code) + 1; + bits += tab->len + f_code + 1; + bit_buf <<= tab->len; + + sign = SBITS (bit_buf, 1); + bit_buf <<= 1; + + if (f_code) + delta += UBITS (bit_buf, f_code); + bit_buf <<= f_code; + + return (delta ^ sign) - sign; + + } else { + + tab = MV_10 + UBITS (bit_buf, 10); + delta = (tab->delta << f_code) + 1; + bits += tab->len + 1; + bit_buf <<= tab->len; + + sign = SBITS (bit_buf, 1); + bit_buf <<= 1; + + if (f_code) { + NEEDBITS (bit_buf, bits, bit_ptr); + delta += UBITS (bit_buf, f_code); + DUMPBITS (bit_buf, bits, f_code); + } + + return (delta ^ sign) - sign; + + } +#undef bit_buf +#undef bits +#undef bit_ptr +} + +static inline int bound_motion_vector (int vector, int f_code) +{ +#if 1 + int limit; + + limit = 16 << f_code; + + if (vector >= limit) + return vector - 2*limit; + else if (vector < -limit) + return vector + 2*limit; + else return vector; +#else + return (vector << (27 - f_code)) >> (27 - f_code); +#endif +} + +static inline int get_dmv (slice_t * slice) +{ +#define bit_buf (slice->bitstream_buf) +#define bits (slice->bitstream_bits) +#define bit_ptr (slice->bitstream_ptr) + + DMVtab * tab; + + tab = DMV_2 + UBITS (bit_buf, 2); + DUMPBITS (bit_buf, bits, tab->len); + return tab->dmv; +#undef bit_buf +#undef bits +#undef bit_ptr +} + +static inline int get_coded_block_pattern (slice_t * slice) +{ +#define bit_buf (slice->bitstream_buf) +#define bits (slice->bitstream_bits) +#define bit_ptr (slice->bitstream_ptr) + + CBPtab * tab; + + NEEDBITS (bit_buf, bits, bit_ptr); + + if (bit_buf >= 0x20000000) { + + tab = CBP_7 - 16 + UBITS (bit_buf, 7); + DUMPBITS (bit_buf, bits, tab->len); + return tab->cbp; + + } else { + + tab = CBP_9 + UBITS (bit_buf, 9); + DUMPBITS (bit_buf, bits, tab->len); + return tab->cbp; + } + +#undef bit_buf +#undef bits +#undef bit_ptr +} + +static inline int get_luma_dc_dct_diff (slice_t * slice) +{ +#define bit_buf (slice->bitstream_buf) +#define bits (slice->bitstream_bits) +#define bit_ptr (slice->bitstream_ptr) + DCtab * tab; + int size; + int dc_diff; + + if (bit_buf < 0xf8000000) { + tab = DC_lum_5 + UBITS (bit_buf, 5); + size = tab->size; + if (size) { + bits += tab->len + size; + bit_buf <<= tab->len; + dc_diff = + UBITS (bit_buf, size) - UBITS (SBITS (~bit_buf, 1), size); + bit_buf <<= size; + return dc_diff; + } else { + DUMPBITS (bit_buf, bits, 3); + return 0; + } + } else { + tab = DC_long - 0x1e0 + UBITS (bit_buf, 9); + size = tab->size; + DUMPBITS (bit_buf, bits, tab->len); + NEEDBITS (bit_buf, bits, bit_ptr); + dc_diff = UBITS (bit_buf, size) - UBITS (SBITS (~bit_buf, 1), size); + DUMPBITS (bit_buf, bits, size); + return dc_diff; + } +#undef bit_buf +#undef bits +#undef bit_ptr +} + +static inline int get_chroma_dc_dct_diff (slice_t * slice) +{ +#define bit_buf (slice->bitstream_buf) +#define bits (slice->bitstream_bits) +#define bit_ptr (slice->bitstream_ptr) + DCtab * tab; + int size; + int dc_diff; + + if (bit_buf < 0xf8000000) { + tab = DC_chrom_5 + UBITS (bit_buf, 5); + size = tab->size; + if (size) { + bits += tab->len + size; + bit_buf <<= tab->len; + dc_diff = + UBITS (bit_buf, size) - UBITS (SBITS (~bit_buf, 1), size); + bit_buf <<= size; + return dc_diff; + } else { + DUMPBITS (bit_buf, bits, 2); + return 0; + } + } else { + tab = DC_long - 0x3e0 + UBITS (bit_buf, 10); + size = tab->size; + DUMPBITS (bit_buf, bits, tab->len + 1); + NEEDBITS (bit_buf, bits, bit_ptr); + dc_diff = UBITS (bit_buf, size) - UBITS (SBITS (~bit_buf, 1), size); + DUMPBITS (bit_buf, bits, size); + return dc_diff; + } +#undef bit_buf +#undef bits +#undef bit_ptr +} + +#define SATURATE(val) \ +do { \ + if (val > 2047) \ + val = 2047; \ + else if (val < -2048) \ + val = -2048; \ +} while (0) + +static void get_intra_block_B14 (picture_t * picture, slice_t * slice, + int16_t * dest) +{ + int i; + int j; + int val; + uint8_t * scan = picture->scan; + uint8_t * quant_matrix = picture->intra_quantizer_matrix; + int quantizer_scale = slice->quantizer_scale; + int mismatch; + DCTtab * tab; + uint32_t bit_buf; + int bits; + uint8_t * bit_ptr; + + i = 0; + mismatch = ~dest[0]; + + bit_buf = slice->bitstream_buf; + bits = slice->bitstream_bits; + bit_ptr = slice->bitstream_ptr; + + NEEDBITS (bit_buf, bits, bit_ptr); + + while (1) { + if (bit_buf >= 0x28000000) { + + tab = DCT_B14AC_5 - 5 + UBITS (bit_buf, 5); + + i += tab->run; + if (i >= 64) + break; // end of block + + normal_code: + j = scan[i]; + bit_buf <<= tab->len; + bits += tab->len + 1; + val = (tab->level * quantizer_scale * quant_matrix[j]) >> 4; + + // if (bitstream_get (1)) val = -val; + val = (val ^ SBITS (bit_buf, 1)) - SBITS (bit_buf, 1); + + SATURATE (val); + dest[j] = val; + mismatch ^= val; + + bit_buf <<= 1; + NEEDBITS (bit_buf, bits, bit_ptr); + + continue; + + } else if (bit_buf >= 0x04000000) { + + tab = DCT_B14_8 - 4 + UBITS (bit_buf, 8); + + i += tab->run; + if (i < 64) + goto normal_code; + + // escape code + + i += UBITS (bit_buf << 6, 6) - 64; + if (i >= 64) + break; // illegal, but check needed to avoid buffer overflow + + j = scan[i]; + + DUMPBITS (bit_buf, bits, 12); + NEEDBITS (bit_buf, bits, bit_ptr); + val = (SBITS (bit_buf, 12) * + quantizer_scale * quant_matrix[j]) / 16; + + SATURATE (val); + dest[j] = val; + mismatch ^= val; + + DUMPBITS (bit_buf, bits, 12); + NEEDBITS (bit_buf, bits, bit_ptr); + + continue; + + } else if (bit_buf >= 0x02000000) { + tab = DCT_B14_10 - 8 + UBITS (bit_buf, 10); + i += tab->run; + if (i < 64) + goto normal_code; + } else if (bit_buf >= 0x00800000) { + tab = DCT_13 - 16 + UBITS (bit_buf, 13); + i += tab->run; + if (i < 64) + goto normal_code; + } else if (bit_buf >= 0x00200000) { + tab = DCT_15 - 16 + UBITS (bit_buf, 15); + i += tab->run; + if (i < 64) + goto normal_code; + } else { + tab = DCT_16 + UBITS (bit_buf, 16); + bit_buf <<= 16; + GETWORD (bit_buf, bits + 16, bit_ptr); + i += tab->run; + if (i < 64) + goto normal_code; + } + break; // illegal, but check needed to avoid buffer overflow + } + dest[63] ^= mismatch & 1; + DUMPBITS (bit_buf, bits, 2); // dump end of block code + slice->bitstream_buf = bit_buf; + slice->bitstream_bits = bits; + slice->bitstream_ptr = bit_ptr; +} + +static void get_intra_block_B15 (picture_t * picture, slice_t * slice, + int16_t * dest) +{ + int i; + int j; + int val; + uint8_t * scan = picture->scan; + uint8_t * quant_matrix = picture->intra_quantizer_matrix; + int quantizer_scale = slice->quantizer_scale; + int mismatch; + DCTtab * tab; + uint32_t bit_buf; + int bits; + uint8_t * bit_ptr; + + i = 0; + mismatch = ~dest[0]; + + bit_buf = slice->bitstream_buf; + bits = slice->bitstream_bits; + bit_ptr = slice->bitstream_ptr; + + NEEDBITS (bit_buf, bits, bit_ptr); + + while (1) { + if (bit_buf >= 0x04000000) { + + tab = DCT_B15_8 - 4 + UBITS (bit_buf, 8); + + i += tab->run; + if (i < 64) { + + normal_code: + j = scan[i]; + bit_buf <<= tab->len; + bits += tab->len + 1; + val = (tab->level * quantizer_scale * quant_matrix[j]) >> 4; + + // if (bitstream_get (1)) val = -val; + val = (val ^ SBITS (bit_buf, 1)) - SBITS (bit_buf, 1); + + SATURATE (val); + dest[j] = val; + mismatch ^= val; + + bit_buf <<= 1; + NEEDBITS (bit_buf, bits, bit_ptr); + + continue; + + } else { + + // end of block. I commented out this code because if we + // dont exit here we will still exit at the later test :) + + //if (i >= 128) break; // end of block + + // escape code + + i += UBITS (bit_buf << 6, 6) - 64; + if (i >= 64) + break; // illegal, but check against buffer overflow + + j = scan[i]; + + DUMPBITS (bit_buf, bits, 12); + NEEDBITS (bit_buf, bits, bit_ptr); + val = (SBITS (bit_buf, 12) * + quantizer_scale * quant_matrix[j]) / 16; + + SATURATE (val); + dest[j] = val; + mismatch ^= val; + + DUMPBITS (bit_buf, bits, 12); + NEEDBITS (bit_buf, bits, bit_ptr); + + continue; + + } + } else if (bit_buf >= 0x02000000) { + tab = DCT_B15_10 - 8 + UBITS (bit_buf, 10); + i += tab->run; + if (i < 64) + goto normal_code; + } else if (bit_buf >= 0x00800000) { + tab = DCT_13 - 16 + UBITS (bit_buf, 13); + i += tab->run; + if (i < 64) + goto normal_code; + } else if (bit_buf >= 0x00200000) { + tab = DCT_15 - 16 + UBITS (bit_buf, 15); + i += tab->run; + if (i < 64) + goto normal_code; + } else { + tab = DCT_16 + UBITS (bit_buf, 16); + bit_buf <<= 16; + GETWORD (bit_buf, bits + 16, bit_ptr); + i += tab->run; + if (i < 64) + goto normal_code; + } + break; // illegal, but check needed to avoid buffer overflow + } + dest[63] ^= mismatch & 1; + DUMPBITS (bit_buf, bits, 4); // dump end of block code + slice->bitstream_buf = bit_buf; + slice->bitstream_bits = bits; + slice->bitstream_ptr = bit_ptr; +} + +static void get_non_intra_block (picture_t * picture, slice_t * slice, + int16_t * dest) +{ + int i; + int j; + int val; + uint8_t * scan = picture->scan; + uint8_t * quant_matrix = picture->non_intra_quantizer_matrix; + int quantizer_scale = slice->quantizer_scale; + int mismatch; + DCTtab * tab; + uint32_t bit_buf; + int bits; + uint8_t * bit_ptr; + + i = -1; + mismatch = 1; + + bit_buf = slice->bitstream_buf; + bits = slice->bitstream_bits; + bit_ptr = slice->bitstream_ptr; + + NEEDBITS (bit_buf, bits, bit_ptr); + if (bit_buf >= 0x28000000) { + tab = DCT_B14DC_5 - 5 + UBITS (bit_buf, 5); + goto entry_1; + } else + goto entry_2; + + while (1) { + if (bit_buf >= 0x28000000) { + + tab = DCT_B14AC_5 - 5 + UBITS (bit_buf, 5); + + entry_1: + i += tab->run; + if (i >= 64) + break; // end of block + + normal_code: + j = scan[i]; + bit_buf <<= tab->len; + bits += tab->len + 1; + val = ((2*tab->level+1) * quantizer_scale * quant_matrix[j]) >> 5; + + // if (bitstream_get (1)) val = -val; + val = (val ^ SBITS (bit_buf, 1)) - SBITS (bit_buf, 1); + + SATURATE (val); + dest[j] = val; + mismatch ^= val; + + bit_buf <<= 1; + NEEDBITS (bit_buf, bits, bit_ptr); + + continue; + + } + + entry_2: + if (bit_buf >= 0x04000000) { + + tab = DCT_B14_8 - 4 + UBITS (bit_buf, 8); + + i += tab->run; + if (i < 64) + goto normal_code; + + // escape code + + i += UBITS (bit_buf << 6, 6) - 64; + if (i >= 64) + break; // illegal, but check needed to avoid buffer overflow + + j = scan[i]; + + DUMPBITS (bit_buf, bits, 12); + NEEDBITS (bit_buf, bits, bit_ptr); + val = 2 * (SBITS (bit_buf, 12) + SBITS (bit_buf, 1)) + 1; + val = (val * quantizer_scale * quant_matrix[j]) / 32; + + SATURATE (val); + dest[j] = val; + mismatch ^= val; + + DUMPBITS (bit_buf, bits, 12); + NEEDBITS (bit_buf, bits, bit_ptr); + + continue; + + } else if (bit_buf >= 0x02000000) { + tab = DCT_B14_10 - 8 + UBITS (bit_buf, 10); + i += tab->run; + if (i < 64) + goto normal_code; + } else if (bit_buf >= 0x00800000) { + tab = DCT_13 - 16 + UBITS (bit_buf, 13); + i += tab->run; + if (i < 64) + goto normal_code; + } else if (bit_buf >= 0x00200000) { + tab = DCT_15 - 16 + UBITS (bit_buf, 15); + i += tab->run; + if (i < 64) + goto normal_code; + } else { + tab = DCT_16 + UBITS (bit_buf, 16); + bit_buf <<= 16; + GETWORD (bit_buf, bits + 16, bit_ptr); + i += tab->run; + if (i < 64) + goto normal_code; + } + break; // illegal, but check needed to avoid buffer overflow + } + dest[63] ^= mismatch & 1; + DUMPBITS (bit_buf, bits, 2); // dump end of block code + slice->bitstream_buf = bit_buf; + slice->bitstream_bits = bits; + slice->bitstream_ptr = bit_ptr; +} + +static void get_mpeg1_intra_block (picture_t * picture, slice_t * slice, + int16_t * dest) +{ + int i; + int j; + int val; + uint8_t * scan = picture->scan; + uint8_t * quant_matrix = picture->intra_quantizer_matrix; + int quantizer_scale = slice->quantizer_scale; + DCTtab * tab; + uint32_t bit_buf; + int bits; + uint8_t * bit_ptr; + + i = 0; + + bit_buf = slice->bitstream_buf; + bits = slice->bitstream_bits; + bit_ptr = slice->bitstream_ptr; + + NEEDBITS (bit_buf, bits, bit_ptr); + + while (1) { + if (bit_buf >= 0x28000000) { + + tab = DCT_B14AC_5 - 5 + UBITS (bit_buf, 5); + + i += tab->run; + if (i >= 64) + break; // end of block + + normal_code: + j = scan[i]; + bit_buf <<= tab->len; + bits += tab->len + 1; + val = (tab->level * quantizer_scale * quant_matrix[j]) >> 4; + + // oddification + val = (val - 1) | 1; + + // if (bitstream_get (1)) val = -val; + val = (val ^ SBITS (bit_buf, 1)) - SBITS (bit_buf, 1); + + SATURATE (val); + dest[j] = val; + + bit_buf <<= 1; + NEEDBITS (bit_buf, bits, bit_ptr); + + continue; + + } else if (bit_buf >= 0x04000000) { + + tab = DCT_B14_8 - 4 + UBITS (bit_buf, 8); + + i += tab->run; + if (i < 64) + goto normal_code; + + // escape code + + i += UBITS (bit_buf << 6, 6) - 64; + if (i >= 64) + break; // illegal, but check needed to avoid buffer overflow + + j = scan[i]; + + DUMPBITS (bit_buf, bits, 12); + NEEDBITS (bit_buf, bits, bit_ptr); + val = SBITS (bit_buf, 8); + if (! (val & 0x7f)) { + DUMPBITS (bit_buf, bits, 8); + val = UBITS (bit_buf, 8) + 2 * val; + } + val = (val * quantizer_scale * quant_matrix[j]) / 16; + + // oddification + val = (val + ~SBITS (val, 1)) | 1; + + SATURATE (val); + dest[j] = val; + + DUMPBITS (bit_buf, bits, 8); + NEEDBITS (bit_buf, bits, bit_ptr); + + continue; + + } else if (bit_buf >= 0x02000000) { + tab = DCT_B14_10 - 8 + UBITS (bit_buf, 10); + i += tab->run; + if (i < 64) + goto normal_code; + } else if (bit_buf >= 0x00800000) { + tab = DCT_13 - 16 + UBITS (bit_buf, 13); + i += tab->run; + if (i < 64) + goto normal_code; + } else if (bit_buf >= 0x00200000) { + tab = DCT_15 - 16 + UBITS (bit_buf, 15); + i += tab->run; + if (i < 64) + goto normal_code; + } else { + tab = DCT_16 + UBITS (bit_buf, 16); + bit_buf <<= 16; + GETWORD (bit_buf, bits + 16, bit_ptr); + i += tab->run; + if (i < 64) + goto normal_code; + } + break; // illegal, but check needed to avoid buffer overflow + } + DUMPBITS (bit_buf, bits, 2); // dump end of block code + slice->bitstream_buf = bit_buf; + slice->bitstream_bits = bits; + slice->bitstream_ptr = bit_ptr; +} + +static void get_mpeg1_non_intra_block (picture_t * picture, slice_t * slice, + int16_t * dest) +{ + int i; + int j; + int val; + uint8_t * scan = picture->scan; + uint8_t * quant_matrix = picture->non_intra_quantizer_matrix; + int quantizer_scale = slice->quantizer_scale; + DCTtab * tab; + uint32_t bit_buf; + int bits; + uint8_t * bit_ptr; + + i = -1; + + bit_buf = slice->bitstream_buf; + bits = slice->bitstream_bits; + bit_ptr = slice->bitstream_ptr; + + NEEDBITS (bit_buf, bits, bit_ptr); + if (bit_buf >= 0x28000000) { + tab = DCT_B14DC_5 - 5 + UBITS (bit_buf, 5); + goto entry_1; + } else + goto entry_2; + + while (1) { + if (bit_buf >= 0x28000000) { + + tab = DCT_B14AC_5 - 5 + UBITS (bit_buf, 5); + + entry_1: + i += tab->run; + if (i >= 64) + break; // end of block + + normal_code: + j = scan[i]; + bit_buf <<= tab->len; + bits += tab->len + 1; + val = ((2*tab->level+1) * quantizer_scale * quant_matrix[j]) >> 5; + + // oddification + val = (val - 1) | 1; + + // if (bitstream_get (1)) val = -val; + val = (val ^ SBITS (bit_buf, 1)) - SBITS (bit_buf, 1); + + SATURATE (val); + dest[j] = val; + + bit_buf <<= 1; + NEEDBITS (bit_buf, bits, bit_ptr); + + continue; + + } + + entry_2: + if (bit_buf >= 0x04000000) { + + tab = DCT_B14_8 - 4 + UBITS (bit_buf, 8); + + i += tab->run; + if (i < 64) + goto normal_code; + + // escape code + + i += UBITS (bit_buf << 6, 6) - 64; + if (i >= 64) + break; // illegal, but check needed to avoid buffer overflow + + j = scan[i]; + + DUMPBITS (bit_buf, bits, 12); + NEEDBITS (bit_buf, bits, bit_ptr); + val = SBITS (bit_buf, 8); + if (! (val & 0x7f)) { + DUMPBITS (bit_buf, bits, 8); + val = UBITS (bit_buf, 8) + 2 * val; + } + val = 2 * (val + SBITS (val, 1)) + 1; + val = (val * quantizer_scale * quant_matrix[j]) / 32; + + // oddification + val = (val + ~SBITS (val, 1)) | 1; + + SATURATE (val); + dest[j] = val; + + DUMPBITS (bit_buf, bits, 8); + NEEDBITS (bit_buf, bits, bit_ptr); + + continue; + + } else if (bit_buf >= 0x02000000) { + tab = DCT_B14_10 - 8 + UBITS (bit_buf, 10); + i += tab->run; + if (i < 64) + goto normal_code; + } else if (bit_buf >= 0x00800000) { + tab = DCT_13 - 16 + UBITS (bit_buf, 13); + i += tab->run; + if (i < 64) + goto normal_code; + } else if (bit_buf >= 0x00200000) { + tab = DCT_15 - 16 + UBITS (bit_buf, 15); + i += tab->run; + if (i < 64) + goto normal_code; + } else { + tab = DCT_16 + UBITS (bit_buf, 16); + bit_buf <<= 16; + GETWORD (bit_buf, bits + 16, bit_ptr); + i += tab->run; + if (i < 64) + goto normal_code; + } + break; // illegal, but check needed to avoid buffer overflow + } + DUMPBITS (bit_buf, bits, 2); // dump end of block code + slice->bitstream_buf = bit_buf; + slice->bitstream_bits = bits; + slice->bitstream_ptr = bit_ptr; +} + +static inline int get_macroblock_address_increment (slice_t * slice) +{ +#define bit_buf (slice->bitstream_buf) +#define bits (slice->bitstream_bits) +#define bit_ptr (slice->bitstream_ptr) + + MBAtab * tab; + int mba; + + mba = 0; + + while (1) { + if (bit_buf >= 0x10000000) { + tab = MBA_5 - 2 + UBITS (bit_buf, 5); + DUMPBITS (bit_buf, bits, tab->len); + return mba + tab->mba; + } else if (bit_buf >= 0x03000000) { + tab = MBA_11 - 24 + UBITS (bit_buf, 11); + DUMPBITS (bit_buf, bits, tab->len); + return mba + tab->mba; + } else switch (UBITS (bit_buf, 11)) { + case 8: // macroblock_escape + mba += 33; + // no break here on purpose + case 15: // macroblock_stuffing (MPEG1 only) + DUMPBITS (bit_buf, bits, 11); + NEEDBITS (bit_buf, bits, bit_ptr); + break; + default: // end of slice, or error + return 0; + } + } + +#undef bit_buf +#undef bits +#undef bit_ptr +} + +static inline void slice_intra_DCT (picture_t * picture, slice_t * slice, + int cc, uint8_t * dest, int stride) +{ +#define bit_buf (slice->bitstream_buf) +#define bits (slice->bitstream_bits) +#define bit_ptr (slice->bitstream_ptr) + NEEDBITS (bit_buf, bits, bit_ptr); + //Get the intra DC coefficient and inverse quantize it + if (cc == 0) + slice->dc_dct_pred[0] += get_luma_dc_dct_diff (slice); + else + slice->dc_dct_pred[cc] += get_chroma_dc_dct_diff (slice); + DCTblock[0] = slice->dc_dct_pred[cc] << (3 - picture->intra_dc_precision); + + if (picture->mpeg1) { + if (picture->picture_coding_type != D_TYPE) + get_mpeg1_intra_block (picture, slice, DCTblock); + } else if (picture->intra_vlc_format) + get_intra_block_B15 (picture, slice, DCTblock); + else + get_intra_block_B14 (picture, slice, DCTblock); + idct_block_copy (DCTblock, dest, stride); + memset (DCTblock, 0, sizeof (DCTblock)); +#undef bit_buf +#undef bits +#undef bit_ptr +} + +static inline void slice_non_intra_DCT (picture_t * picture, slice_t * slice, + uint8_t * dest, int stride) +{ + if (picture->mpeg1) + get_mpeg1_non_intra_block (picture, slice, DCTblock); + else + get_non_intra_block (picture, slice, DCTblock); + idct_block_add (DCTblock, dest, stride); + memset (DCTblock, 0, sizeof (DCTblock)); +} + +static inline void motion_block (void (** table) (uint8_t *, uint8_t *, + int32_t, int32_t), + int x_pred, int y_pred, + uint8_t * dest[3], int dest_offset, + uint8_t * src[3], int src_offset, + int stride, int height, int second_half) +{ + int xy_half; + uint8_t * src1; + uint8_t * src2; + + xy_half = ((y_pred & 1) << 1) | (x_pred & 1); + + src1 = src[0] + src_offset + (x_pred >> 1) + (y_pred >> 1) * stride + + second_half * (stride << 3); + + table[xy_half] (dest[0] + dest_offset + second_half * (stride << 3), + src1, stride, height); + + x_pred /= 2; + y_pred /= 2; + + xy_half = ((y_pred & 1) << 1) | (x_pred & 1); + stride >>= 1; + height >>= 1; + src_offset >>= 1; src_offset += second_half * (stride << 2); + dest_offset >>= 1; dest_offset += second_half * (stride << 2); + + src1 = src[1] + src_offset + (x_pred >> 1) + (y_pred >> 1) * stride; + src2 = src[2] + src_offset + (x_pred >> 1) + (y_pred >> 1) * stride; + + table[4+xy_half] (dest[1] + dest_offset, src1, stride, height); + table[4+xy_half] (dest[2] + dest_offset, src2, stride, height); +} + + +static void motion_mp1 (slice_t * slice, motion_t * motion, + uint8_t * dest[3], int offset, int width, + void (** table) (uint8_t *, uint8_t *, int, int)) +{ +#define bit_buf (slice->bitstream_buf) +#define bits (slice->bitstream_bits) +#define bit_ptr (slice->bitstream_ptr) + int motion_x, motion_y; + + NEEDBITS (bit_buf, bits, bit_ptr); + motion_x = motion->pmv[0][0] + get_motion_delta (slice, motion->f_code[0]); + motion_x = bound_motion_vector (motion_x, motion->f_code[0]); + motion->pmv[0][0] = motion_x; + + NEEDBITS (bit_buf, bits, bit_ptr); + motion_y = motion->pmv[0][1] + get_motion_delta (slice, motion->f_code[0]); + motion_y = bound_motion_vector (motion_y, motion->f_code[0]); + motion->pmv[0][1] = motion_y; + + if (motion->f_code[1]) { + motion_x <<= 1; + motion_y <<= 1; + } + + motion_block (table, motion_x, motion_y, dest, offset, + motion->ref[0], offset, width, 16, 0); +#undef bit_buf +#undef bits +#undef bit_ptr +} + +static void motion_mp1_reuse (slice_t * slice, motion_t * motion, + uint8_t * dest[3], int offset, int width, + void (** table) (uint8_t *, uint8_t *, int, int)) +{ + int motion_x, motion_y; + + motion_x = motion->pmv[0][0]; + motion_y = motion->pmv[0][1]; + + if (motion->f_code[1]) { + motion_x <<= 1; + motion_y <<= 1; + } + + motion_block (table, motion_x, motion_y, dest, offset, + motion->ref[0], offset, width, 16, 0); +} + +static void motion_fr_frame (slice_t * slice, motion_t * motion, + uint8_t * dest[3], int offset, int width, + void (** table) (uint8_t *, uint8_t *, int, int)) +{ +#define bit_buf (slice->bitstream_buf) +#define bits (slice->bitstream_bits) +#define bit_ptr (slice->bitstream_ptr) + int motion_x, motion_y; + + NEEDBITS (bit_buf, bits, bit_ptr); + motion_x = motion->pmv[0][0] + get_motion_delta (slice, motion->f_code[0]); + motion_x = bound_motion_vector (motion_x, motion->f_code[0]); + motion->pmv[1][0] = motion->pmv[0][0] = motion_x; + + NEEDBITS (bit_buf, bits, bit_ptr); + motion_y = motion->pmv[0][1] + get_motion_delta (slice, motion->f_code[1]); + motion_y = bound_motion_vector (motion_y, motion->f_code[1]); + motion->pmv[1][1] = motion->pmv[0][1] = motion_y; + + motion_block (table, motion_x, motion_y, dest, offset, + motion->ref[0], offset, width, 16, 0); +#undef bit_buf +#undef bits +#undef bit_ptr +} + +static void motion_fr_field (slice_t * slice, motion_t * motion, + uint8_t * dest[3], int offset, int width, + void (** table) (uint8_t *, uint8_t *, int, int)) +{ +#define bit_buf (slice->bitstream_buf) +#define bits (slice->bitstream_bits) +#define bit_ptr (slice->bitstream_ptr) + int motion_x, motion_y; + int field_select; + + NEEDBITS (bit_buf, bits, bit_ptr); + field_select = SBITS (bit_buf, 1); + DUMPBITS (bit_buf, bits, 1); + + motion_x = motion->pmv[0][0] + get_motion_delta (slice, motion->f_code[0]); + motion_x = bound_motion_vector (motion_x, motion->f_code[0]); + motion->pmv[0][0] = motion_x; |