diff options
author | arpi <arpi@b3059339-0415-0410-9bf9-f77b7e298cf2> | 2003-04-06 16:36:02 +0000 |
---|---|---|
committer | arpi <arpi@b3059339-0415-0410-9bf9-f77b7e298cf2> | 2003-04-06 16:36:02 +0000 |
commit | 0b6eb24b9a8034287f67f800fc61d07b7f018891 (patch) | |
tree | ee1e7fe1ba37e57d24b8604e1c53f38e7b67484c /libmpeg2/slice.c | |
parent | 27ff6fa04a22e2a56848b3ff3e1d681a18bc98e4 (diff) | |
download | mpv-0b6eb24b9a8034287f67f800fc61d07b7f018891.tar.bz2 mpv-0b6eb24b9a8034287f67f800fc61d07b7f018891.tar.xz |
Importing libmpeg2 from mpeg2dec-0.3.1
git-svn-id: svn://svn.mplayerhq.hu/mplayer/trunk@9853 b3059339-0415-0410-9bf9-f77b7e298cf2
Diffstat (limited to 'libmpeg2/slice.c')
-rw-r--r-- | libmpeg2/slice.c | 1368 |
1 files changed, 673 insertions, 695 deletions
diff --git a/libmpeg2/slice.c b/libmpeg2/slice.c index 4e289f0d06..7f6a2ed052 100644 --- a/libmpeg2/slice.c +++ b/libmpeg2/slice.c @@ -1,8 +1,10 @@ /* * slice.c - * Copyright (C) 1999-2001 Aaron Holtzman <aholtzma@ess.engr.uvic.ca> + * Copyright (C) 2000-2002 Michel Lespinasse <walken@zoy.org> + * Copyright (C) 1999-2000 Aaron Holtzman <aholtzma@ess.engr.uvic.ca> * * This file is part of mpeg2dec, a free MPEG-2 video stream decoder. + * See http://libmpeg2.sourceforge.net/ for updates. * * mpeg2dec is free software; you can redistribute it and/or modify * it under the terms of the GNU General Public License as published by @@ -21,20 +23,18 @@ #include "config.h" -#include <string.h> #include <inttypes.h> -#include "video_out.h" +#include "mpeg2.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); - -//#ifdef MPEG12_POSTPROC -//extern int quant_store[MPEG2_MBR+1][MPEG2_MBC+1]; // [Review] -//#endif +extern mpeg2_mc_t mpeg2_mc; +extern void (* mpeg2_idct_copy) (int16_t * block, uint8_t * dest, int stride); +extern void (* mpeg2_idct_add) (int last, int16_t * block, + uint8_t * dest, int stride); +extern void (* mpeg2_cpu_state_save) (cpu_state_t * state); +extern void (* mpeg2_cpu_state_restore) (cpu_state_t * state); #include "vlc.h" @@ -45,23 +45,23 @@ static int non_linear_quantizer_scale [] = { 56, 64, 72, 80, 88, 96, 104, 112 }; -static inline int get_macroblock_modes (picture_t * picture) +static inline int get_macroblock_modes (decoder_t * const decoder) { -#define bit_buf (picture->bitstream_buf) -#define bits (picture->bitstream_bits) -#define bit_ptr (picture->bitstream_ptr) +#define bit_buf (decoder->bitstream_buf) +#define bits (decoder->bitstream_bits) +#define bit_ptr (decoder->bitstream_ptr) int macroblock_modes; - MBtab * tab; + const MBtab * tab; - switch (picture->picture_coding_type) { + switch (decoder->coding_type) { case I_TYPE: tab = MB_I + UBITS (bit_buf, 1); DUMPBITS (bit_buf, bits, tab->len); macroblock_modes = tab->modes; - if ((! (picture->frame_pred_frame_dct)) && - (picture->picture_structure == FRAME_PICTURE)) { + if ((! (decoder->frame_pred_frame_dct)) && + (decoder->picture_structure == FRAME_PICTURE)) { macroblock_modes |= UBITS (bit_buf, 1) * DCT_TYPE_INTERLACED; DUMPBITS (bit_buf, bits, 1); } @@ -74,13 +74,13 @@ static inline int get_macroblock_modes (picture_t * picture) DUMPBITS (bit_buf, bits, tab->len); macroblock_modes = tab->modes; - if (picture->picture_structure != FRAME_PICTURE) { + if (decoder->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 (picture->frame_pred_frame_dct) { + } else if (decoder->frame_pred_frame_dct) { if (macroblock_modes & MACROBLOCK_MOTION_FORWARD) macroblock_modes |= MC_FRAME; return macroblock_modes; @@ -102,13 +102,13 @@ static inline int get_macroblock_modes (picture_t * picture) DUMPBITS (bit_buf, bits, tab->len); macroblock_modes = tab->modes; - if (picture->picture_structure != FRAME_PICTURE) { + if (decoder->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 (picture->frame_pred_frame_dct) { + } else if (decoder->frame_pred_frame_dct) { /* if (! (macroblock_modes & MACROBLOCK_INTRA)) */ macroblock_modes |= MC_FRAME; return macroblock_modes; @@ -138,18 +138,18 @@ static inline int get_macroblock_modes (picture_t * picture) #undef bit_ptr } -static inline int get_quantizer_scale (picture_t * picture) +static inline int get_quantizer_scale (decoder_t * const decoder) { -#define bit_buf (picture->bitstream_buf) -#define bits (picture->bitstream_bits) -#define bit_ptr (picture->bitstream_ptr) +#define bit_buf (decoder->bitstream_buf) +#define bits (decoder->bitstream_bits) +#define bit_ptr (decoder->bitstream_ptr) int quantizer_scale_code; quantizer_scale_code = UBITS (bit_buf, 5); DUMPBITS (bit_buf, bits, 5); - if (picture->q_scale_type) + if (decoder->q_scale_type) return non_linear_quantizer_scale [quantizer_scale_code]; else return quantizer_scale_code << 1; @@ -158,15 +158,16 @@ static inline int get_quantizer_scale (picture_t * picture) #undef bit_ptr } -static inline int get_motion_delta (picture_t * picture, int f_code) +static inline int get_motion_delta (decoder_t * const decoder, + const int f_code) { -#define bit_buf (picture->bitstream_buf) -#define bits (picture->bitstream_bits) -#define bit_ptr (picture->bitstream_ptr) +#define bit_buf (decoder->bitstream_buf) +#define bits (decoder->bitstream_bits) +#define bit_ptr (decoder->bitstream_ptr) int delta; int sign; - MVtab * tab; + const MVtab * tab; if (bit_buf & 0x80000000) { DUMPBITS (bit_buf, bits, 1); @@ -211,30 +212,32 @@ static inline int get_motion_delta (picture_t * picture, int f_code) #undef bit_ptr } -static inline int bound_motion_vector (int vector, int f_code) +static inline int bound_motion_vector (const int vector, const int f_code) { -#if 1 - int limit; +#if 0 + unsigned int limit; + int sign; limit = 16 << f_code; - if (vector >= limit) - return vector - 2*limit; - else if (vector < -limit) - return vector + 2*limit; - else return vector; + if ((unsigned int)(vector + limit) < 2 * limit) + return vector; + else { + sign = ((int32_t)vector) >> 31; + return vector - ((2 * limit) ^ sign) + sign; + } #else - return (vector << (27 - f_code)) >> (27 - f_code); + return ((int32_t)vector << (27 - f_code)) >> (27 - f_code); #endif } -static inline int get_dmv (picture_t * picture) +static inline int get_dmv (decoder_t * const decoder) { -#define bit_buf (picture->bitstream_buf) -#define bits (picture->bitstream_bits) -#define bit_ptr (picture->bitstream_ptr) +#define bit_buf (decoder->bitstream_buf) +#define bits (decoder->bitstream_bits) +#define bit_ptr (decoder->bitstream_ptr) - DMVtab * tab; + const DMVtab * tab; tab = DMV_2 + UBITS (bit_buf, 2); DUMPBITS (bit_buf, bits, tab->len); @@ -244,19 +247,19 @@ static inline int get_dmv (picture_t * picture) #undef bit_ptr } -static inline int get_coded_block_pattern (picture_t * picture) +static inline int get_coded_block_pattern (decoder_t * const decoder) { -#define bit_buf (picture->bitstream_buf) -#define bits (picture->bitstream_bits) -#define bit_ptr (picture->bitstream_ptr) +#define bit_buf (decoder->bitstream_buf) +#define bits (decoder->bitstream_bits) +#define bit_ptr (decoder->bitstream_ptr) - CBPtab * tab; + const CBPtab * tab; NEEDBITS (bit_buf, bits, bit_ptr); if (bit_buf >= 0x20000000) { - tab = CBP_7 - 16 + UBITS (bit_buf, 7); + tab = CBP_7 + (UBITS (bit_buf, 7) - 16); DUMPBITS (bit_buf, bits, tab->len); return tab->cbp; @@ -272,12 +275,12 @@ static inline int get_coded_block_pattern (picture_t * picture) #undef bit_ptr } -static inline int get_luma_dc_dct_diff (picture_t * picture) +static inline int get_luma_dc_dct_diff (decoder_t * const decoder) { -#define bit_buf (picture->bitstream_buf) -#define bits (picture->bitstream_bits) -#define bit_ptr (picture->bitstream_ptr) - DCtab * tab; +#define bit_buf (decoder->bitstream_buf) +#define bits (decoder->bitstream_bits) +#define bit_ptr (decoder->bitstream_ptr) + const DCtab * tab; int size; int dc_diff; @@ -296,7 +299,7 @@ static inline int get_luma_dc_dct_diff (picture_t * picture) return 0; } } else { - tab = DC_long - 0x1e0 + UBITS (bit_buf, 9); + tab = DC_long + (UBITS (bit_buf, 9) - 0x1e0); size = tab->size; DUMPBITS (bit_buf, bits, tab->len); NEEDBITS (bit_buf, bits, bit_ptr); @@ -309,12 +312,12 @@ static inline int get_luma_dc_dct_diff (picture_t * picture) #undef bit_ptr } -static inline int get_chroma_dc_dct_diff (picture_t * picture) +static inline int get_chroma_dc_dct_diff (decoder_t * const decoder) { -#define bit_buf (picture->bitstream_buf) -#define bits (picture->bitstream_bits) -#define bit_ptr (picture->bitstream_ptr) - DCtab * tab; +#define bit_buf (decoder->bitstream_buf) +#define bits (decoder->bitstream_bits) +#define bit_ptr (decoder->bitstream_ptr) + const DCtab * tab; int size; int dc_diff; @@ -333,7 +336,7 @@ static inline int get_chroma_dc_dct_diff (picture_t * picture) return 0; } } else { - tab = DC_long - 0x3e0 + UBITS (bit_buf, 10); + tab = DC_long + (UBITS (bit_buf, 10) - 0x3e0); size = tab->size; DUMPBITS (bit_buf, bits, tab->len + 1); NEEDBITS (bit_buf, bits, bit_ptr); @@ -346,41 +349,41 @@ static inline int get_chroma_dc_dct_diff (picture_t * picture) #undef bit_ptr } -#define SATURATE(val) \ -do { \ - if ((uint32_t)(val + 2048) > 4095) \ - val = (val > 0) ? 2047 : -2048; \ +#define SATURATE(val) \ +do { \ + if (unlikely ((uint32_t)(val + 2048) > 4095)) \ + val = SBITS (val, 1) ^ 2047; \ } while (0) -static void get_intra_block_B14 (picture_t * picture) +static void get_intra_block_B14 (decoder_t * const decoder) { int i; int j; int val; - uint8_t * scan = picture->scan; - uint8_t * quant_matrix = picture->intra_quantizer_matrix; - int quantizer_scale = picture->quantizer_scale; + const uint8_t * scan = decoder->scan; + const uint8_t * quant_matrix = decoder->intra_quantizer_matrix; + int quantizer_scale = decoder->quantizer_scale; int mismatch; - DCTtab * tab; + const DCTtab * tab; uint32_t bit_buf; int bits; - uint8_t * bit_ptr; + const uint8_t * bit_ptr; int16_t * dest; - dest = picture->DCTblock; + dest = decoder->DCTblock; i = 0; mismatch = ~dest[0]; - bit_buf = picture->bitstream_buf; - bits = picture->bitstream_bits; - bit_ptr = picture->bitstream_ptr; + bit_buf = decoder->bitstream_buf; + bits = decoder->bitstream_bits; + bit_ptr = decoder->bitstream_ptr; NEEDBITS (bit_buf, bits, bit_ptr); while (1) { if (bit_buf >= 0x28000000) { - tab = DCT_B14AC_5 - 5 + UBITS (bit_buf, 5); + tab = DCT_B14AC_5 + (UBITS (bit_buf, 5) - 5); i += tab->run; if (i >= 64) @@ -406,7 +409,7 @@ static void get_intra_block_B14 (picture_t * picture) } else if (bit_buf >= 0x04000000) { - tab = DCT_B14_8 - 4 + UBITS (bit_buf, 8); + tab = DCT_B14_8 + (UBITS (bit_buf, 8) - 4); i += tab->run; if (i < 64) @@ -435,17 +438,17 @@ static void get_intra_block_B14 (picture_t * picture) continue; } else if (bit_buf >= 0x02000000) { - tab = DCT_B14_10 - 8 + UBITS (bit_buf, 10); + tab = DCT_B14_10 + (UBITS (bit_buf, 10) - 8); i += tab->run; if (i < 64) goto normal_code; } else if (bit_buf >= 0x00800000) { - tab = DCT_13 - 16 + UBITS (bit_buf, 13); + tab = DCT_13 + (UBITS (bit_buf, 13) - 16); i += tab->run; if (i < 64) goto normal_code; } else if (bit_buf >= 0x00200000) { - tab = DCT_15 - 16 + UBITS (bit_buf, 15); + tab = DCT_15 + (UBITS (bit_buf, 15) - 16); i += tab->run; if (i < 64) goto normal_code; @@ -461,40 +464,40 @@ static void get_intra_block_B14 (picture_t * picture) } dest[63] ^= mismatch & 1; DUMPBITS (bit_buf, bits, 2); /* dump end of block code */ - picture->bitstream_buf = bit_buf; - picture->bitstream_bits = bits; - picture->bitstream_ptr = bit_ptr; + decoder->bitstream_buf = bit_buf; + decoder->bitstream_bits = bits; + decoder->bitstream_ptr = bit_ptr; } -static void get_intra_block_B15 (picture_t * picture) +static void get_intra_block_B15 (decoder_t * const decoder) { int i; int j; int val; - uint8_t * scan = picture->scan; - uint8_t * quant_matrix = picture->intra_quantizer_matrix; - int quantizer_scale = picture->quantizer_scale; + const uint8_t * scan = decoder->scan; + const uint8_t * quant_matrix = decoder->intra_quantizer_matrix; + int quantizer_scale = decoder->quantizer_scale; int mismatch; - DCTtab * tab; + const DCTtab * tab; uint32_t bit_buf; int bits; - uint8_t * bit_ptr; + const uint8_t * bit_ptr; int16_t * dest; - dest = picture->DCTblock; + dest = decoder->DCTblock; i = 0; mismatch = ~dest[0]; - bit_buf = picture->bitstream_buf; - bits = picture->bitstream_bits; - bit_ptr = picture->bitstream_ptr; + bit_buf = decoder->bitstream_buf; + bits = decoder->bitstream_bits; + bit_ptr = decoder->bitstream_ptr; NEEDBITS (bit_buf, bits, bit_ptr); while (1) { if (bit_buf >= 0x04000000) { - tab = DCT_B15_8 - 4 + UBITS (bit_buf, 8); + tab = DCT_B15_8 + (UBITS (bit_buf, 8) - 4); i += tab->run; if (i < 64) { @@ -548,17 +551,17 @@ static void get_intra_block_B15 (picture_t * picture) } } else if (bit_buf >= 0x02000000) { - tab = DCT_B15_10 - 8 + UBITS (bit_buf, 10); + tab = DCT_B15_10 + (UBITS (bit_buf, 10) - 8); i += tab->run; if (i < 64) goto normal_code; } else if (bit_buf >= 0x00800000) { - tab = DCT_13 - 16 + UBITS (bit_buf, 13); + tab = DCT_13 + (UBITS (bit_buf, 13) - 16); i += tab->run; if (i < 64) goto normal_code; } else if (bit_buf >= 0x00200000) { - tab = DCT_15 - 16 + UBITS (bit_buf, 15); + tab = DCT_15 + (UBITS (bit_buf, 15) - 16); i += tab->run; if (i < 64) goto normal_code; @@ -574,37 +577,37 @@ static void get_intra_block_B15 (picture_t * picture) } dest[63] ^= mismatch & 1; DUMPBITS (bit_buf, bits, 4); /* dump end of block code */ - picture->bitstream_buf = bit_buf; - picture->bitstream_bits = bits; - picture->bitstream_ptr = bit_ptr; + decoder->bitstream_buf = bit_buf; + decoder->bitstream_bits = bits; + decoder->bitstream_ptr = bit_ptr; } -static void get_non_intra_block (picture_t * picture) +static int get_non_intra_block (decoder_t * const decoder) { int i; int j; int val; - uint8_t * scan = picture->scan; - uint8_t * quant_matrix = picture->non_intra_quantizer_matrix; - int quantizer_scale = picture->quantizer_scale; + const uint8_t * scan = decoder->scan; + const uint8_t * quant_matrix = decoder->non_intra_quantizer_matrix; + int quantizer_scale = decoder->quantizer_scale; int mismatch; - DCTtab * tab; + const DCTtab * tab; uint32_t bit_buf; int bits; - uint8_t * bit_ptr; + const uint8_t * bit_ptr; int16_t * dest; i = -1; mismatch = 1; - dest = picture->DCTblock; + dest = decoder->DCTblock; - bit_buf = picture->bitstream_buf; - bits = picture->bitstream_bits; - bit_ptr = picture->bitstream_ptr; + bit_buf = decoder->bitstream_buf; + bits = decoder->bitstream_bits; + bit_ptr = decoder->bitstream_ptr; NEEDBITS (bit_buf, bits, bit_ptr); if (bit_buf >= 0x28000000) { - tab = DCT_B14DC_5 - 5 + UBITS (bit_buf, 5); + tab = DCT_B14DC_5 + (UBITS (bit_buf, 5) - 5); goto entry_1; } else goto entry_2; @@ -612,7 +615,7 @@ static void get_non_intra_block (picture_t * picture) while (1) { if (bit_buf >= 0x28000000) { - tab = DCT_B14AC_5 - 5 + UBITS (bit_buf, 5); + tab = DCT_B14AC_5 + (UBITS (bit_buf, 5) - 5); entry_1: i += tab->run; @@ -642,7 +645,7 @@ static void get_non_intra_block (picture_t * picture) entry_2: if (bit_buf >= 0x04000000) { - tab = DCT_B14_8 - 4 + UBITS (bit_buf, 8); + tab = DCT_B14_8 + (UBITS (bit_buf, 8) - 4); i += tab->run; if (i < 64) @@ -671,17 +674,17 @@ static void get_non_intra_block (picture_t * picture) continue; } else if (bit_buf >= 0x02000000) { - tab = DCT_B14_10 - 8 + UBITS (bit_buf, 10); + tab = DCT_B14_10 + (UBITS (bit_buf, 10) - 8); i += tab->run; if (i < 64) goto normal_code; } else if (bit_buf >= 0x00800000) { - tab = DCT_13 - 16 + UBITS (bit_buf, 13); + tab = DCT_13 + (UBITS (bit_buf, 13) - 16); i += tab->run; if (i < 64) goto normal_code; } else if (bit_buf >= 0x00200000) { - tab = DCT_15 - 16 + UBITS (bit_buf, 15); + tab = DCT_15 + (UBITS (bit_buf, 15) - 16); i += tab->run; if (i < 64) goto normal_code; @@ -697,38 +700,39 @@ static void get_non_intra_block (picture_t * picture) } dest[63] ^= mismatch & 1; DUMPBITS (bit_buf, bits, 2); /* dump end of block code */ - picture->bitstream_buf = bit_buf; - picture->bitstream_bits = bits; - picture->bitstream_ptr = bit_ptr; + decoder->bitstream_buf = bit_buf; + decoder->bitstream_bits = bits; + decoder->bitstream_ptr = bit_ptr; + return i; } -static void get_mpeg1_intra_block (picture_t * picture) +static void get_mpeg1_intra_block (decoder_t * const decoder) { int i; int j; int val; - uint8_t * scan = picture->scan; - uint8_t * quant_matrix = picture->intra_quantizer_matrix; - int quantizer_scale = picture->quantizer_scale; - DCTtab * tab; + const uint8_t * scan = decoder->scan; + const uint8_t * quant_matrix = decoder->intra_quantizer_matrix; + int quantizer_scale = decoder->quantizer_scale; + const DCTtab * tab; uint32_t bit_buf; int bits; - uint8_t * bit_ptr; + const uint8_t * bit_ptr; int16_t * dest; i = 0; - dest = picture->DCTblock; + dest = decoder->DCTblock; - bit_buf = picture->bitstream_buf; - bits = picture->bitstream_bits; - bit_ptr = picture->bitstream_ptr; + bit_buf = decoder->bitstream_buf; + bits = decoder->bitstream_bits; + bit_ptr = decoder->bitstream_ptr; NEEDBITS (bit_buf, bits, bit_ptr); while (1) { if (bit_buf >= 0x28000000) { - tab = DCT_B14AC_5 - 5 + UBITS (bit_buf, 5); + tab = DCT_B14AC_5 + (UBITS (bit_buf, 5) - 5); i += tab->run; if (i >= 64) @@ -756,7 +760,7 @@ static void get_mpeg1_intra_block (picture_t * picture) } else if (bit_buf >= 0x04000000) { - tab = DCT_B14_8 - 4 + UBITS (bit_buf, 8); + tab = DCT_B14_8 + (UBITS (bit_buf, 8) - 4); i += tab->run; if (i < 64) @@ -791,17 +795,17 @@ static void get_mpeg1_intra_block (picture_t * picture) continue; } else if (bit_buf >= 0x02000000) { - tab = DCT_B14_10 - 8 + UBITS (bit_buf, 10); + tab = DCT_B14_10 + (UBITS (bit_buf, 10) - 8); i += tab->run; if (i < 64) goto normal_code; } else if (bit_buf >= 0x00800000) { - tab = DCT_13 - 16 + UBITS (bit_buf, 13); + tab = DCT_13 + (UBITS (bit_buf, 13) - 16); i += tab->run; if (i < 64) goto normal_code; } else if (bit_buf >= 0x00200000) { - tab = DCT_15 - 16 + UBITS (bit_buf, 15); + tab = DCT_15 + (UBITS (bit_buf, 15) - 16); i += tab->run; if (i < 64) goto normal_code; @@ -816,35 +820,35 @@ static void get_mpeg1_intra_block (picture_t * picture) break; /* illegal, check needed to avoid buffer overflow */ } DUMPBITS (bit_buf, bits, 2); /* dump end of block code */ - picture->bitstream_buf = bit_buf; - picture->bitstream_bits = bits; - picture->bitstream_ptr = bit_ptr; + decoder->bitstream_buf = bit_buf; + decoder->bitstream_bits = bits; + decoder->bitstream_ptr = bit_ptr; } -static void get_mpeg1_non_intra_block (picture_t * picture) +static int get_mpeg1_non_intra_block (decoder_t * const decoder) { int i; int j; int val; - uint8_t * scan = picture->scan; - uint8_t * quant_matrix = picture->non_intra_quantizer_matrix; - int quantizer_scale = picture->quantizer_scale; - DCTtab * tab; + const uint8_t * scan = decoder->scan; + const uint8_t * quant_matrix = decoder->non_intra_quantizer_matrix; + int quantizer_scale = decoder->quantizer_scale; + const DCTtab * tab; uint32_t bit_buf; int bits; - uint8_t * bit_ptr; + const uint8_t * bit_ptr; int16_t * dest; i = -1; - dest = picture->DCTblock; + dest = decoder->DCTblock; - bit_buf = picture->bitstream_buf; - bits = picture->bitstream_bits; - bit_ptr = picture->bitstream_ptr; + bit_buf = decoder->bitstream_buf; + bits = decoder->bitstream_bits; + bit_ptr = decoder->bitstream_ptr; NEEDBITS (bit_buf, bits, bit_ptr); if (bit_buf >= 0x28000000) { - tab = DCT_B14DC_5 - 5 + UBITS (bit_buf, 5); + tab = DCT_B14DC_5 + (UBITS (bit_buf, 5) - 5); goto entry_1; } else goto entry_2; @@ -852,7 +856,7 @@ static void get_mpeg1_non_intra_block (picture_t * picture) while (1) { if (bit_buf >= 0x28000000) { - tab = DCT_B14AC_5 - 5 + UBITS (bit_buf, 5); + tab = DCT_B14AC_5 + (UBITS (bit_buf, 5) - 5); entry_1: i += tab->run; @@ -884,7 +888,7 @@ static void get_mpeg1_non_intra_block (picture_t * picture) entry_2: if (bit_buf >= 0x04000000) { - tab = DCT_B14_8 - 4 + UBITS (bit_buf, 8); + tab = DCT_B14_8 + (UBITS (bit_buf, 8) - 4); i += tab->run; if (i < 64) @@ -920,17 +924,17 @@ static void get_mpeg1_non_intra_block (picture_t * picture) continue; } else if (bit_buf >= 0x02000000) { - tab = DCT_B14_10 - 8 + UBITS (bit_buf, 10); + tab = DCT_B14_10 + (UBITS (bit_buf, 10) - 8); i += tab->run; if (i < 64) goto normal_code; } else if (bit_buf >= 0x00800000) { - tab = DCT_13 - 16 + UBITS (bit_buf, 13); + tab = DCT_13 + (UBITS (bit_buf, 13) - 16); i += tab->run; if (i < 64) goto normal_code; } else if (bit_buf >= 0x00200000) { - tab = DCT_15 - 16 + UBITS (bit_buf, 15); + tab = DCT_15 + (UBITS (bit_buf, 15) - 16); i += tab->run; if (i < 64) goto normal_code; @@ -945,362 +949,320 @@ static void get_mpeg1_non_intra_block (picture_t * picture) break; /* illegal, check needed to avoid buffer overflow */ } DUMPBITS (bit_buf, bits, 2); /* dump end of block code */ - picture->bitstream_buf = bit_buf; - picture->bitstream_bits = bits; - picture->bitstream_ptr = bit_ptr; + decoder->bitstream_buf = bit_buf; + decoder->bitstream_bits = bits; + decoder->bitstream_ptr = bit_ptr; + return i; } -static inline int get_macroblock_address_increment (picture_t * picture) +static inline void slice_intra_DCT (decoder_t * const decoder, const int cc, + uint8_t * const dest, const int stride) { -#define bit_buf (picture->bitstream_buf) -#define bits (picture->bitstream_bits) -#define bit_ptr (picture->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 */ -// printf("MB error: %d \n",(UBITS (bit_buf, 11))); // FIXME! -// return 0; - return -1; - } - } - -#undef bit_buf -#undef bits -#undef bit_ptr -} - -static inline void slice_intra_DCT (picture_t * picture, int cc, - uint8_t * dest, int stride) -{ -#define bit_buf (picture->bitstream_buf) -#define bits (picture->bitstream_bits) -#define bit_ptr (picture->bitstream_ptr) +#define bit_buf (decoder->bitstream_buf) +#define bits (decoder->bitstream_bits) +#define bit_ptr (decoder->bitstream_ptr) NEEDBITS (bit_buf, bits, bit_ptr); /* Get the intra DC coefficient and inverse quantize it */ if (cc == 0) - picture->dc_dct_pred[0] += get_luma_dc_dct_diff (picture); + decoder->dc_dct_pred[0] += get_luma_dc_dct_diff (decoder); else - picture->dc_dct_pred[cc] += get_chroma_dc_dct_diff (picture); - picture->DCTblock[0] = - picture->dc_dct_pred[cc] << (3 - picture->intra_dc_precision); - memset (picture->DCTblock + 1, 0, 63 * sizeof (int16_t)); - - if (picture->mpeg1) { - if (picture->picture_coding_type != D_TYPE) - get_mpeg1_intra_block (picture); - } else if (picture->intra_vlc_format) - get_intra_block_B15 (picture); + decoder->dc_dct_pred[cc] += get_chroma_dc_dct_diff (decoder); + decoder->DCTblock[0] = + decoder->dc_dct_pred[cc] << (3 - decoder->intra_dc_precision); + + if (decoder->mpeg1) { + if (decoder->coding_type != D_TYPE) + get_mpeg1_intra_block (decoder); + } else if (decoder->intra_vlc_format) + get_intra_block_B15 (decoder); else - get_intra_block_B14 (picture); - idct_block_copy (picture->DCTblock, dest, stride); + get_intra_block_B14 (decoder); + mpeg2_idct_copy (decoder->DCTblock, dest, stride); #undef bit_buf #undef bits #undef bit_ptr } -static inline void slice_non_intra_DCT (picture_t * picture, uint8_t * dest, - int stride) -{ - memset (picture->DCTblock, 0, 64 * sizeof (int16_t)); - if (picture->mpeg1) - get_mpeg1_non_intra_block (picture); - else - get_non_intra_block (picture); - idct_block_add (picture->DCTblock, dest, stride); -} - -#define MOTION_Y(table,offset_x,offset_y,motion_x,motion_y, \ - dest,src,offset_dest,offset_src,stride,height) \ -do { \ - int xy_half; \ - int total_offset; \ - \ - xy_half = ((motion_y & 1) << 1) | (motion_x & 1); \ - total_offset = ((offset_y + (motion_y >> 1)) * stride + \ - offset_x + (motion_x >> 1) + (offset_src)); \ - table[xy_half] (dest[0] + offset_x + (offset_dest), \ - src[0] + total_offset, stride, height); \ -} while (0) - -#define MOTION_UV(table,offset_x,offset_y,motion_x,motion_y, \ - dest,src,offset_dest,offset_src,stride,height) \ -do { \ - int xy_half; \ - int total_offset; \ - \ - xy_half = ((motion_y & 1) << 1) | (motion_x & 1); \ - total_offset = (((offset_y + motion_y) >> 1) * (stride) + \ - ((offset_x + motion_x) >> 1) + (offset_src)); \ - table[4+xy_half] (dest[1] + (offset_x >> 1) + (offset_dest), \ - src[1] + total_offset, stride, height); \ - table[4+xy_half] (dest[2] + (offset_x >> 1) + (offset_dest), \ - src[2] + total_offset, stride, height); \ -} while (0) - -static inline void motion_block (void (** table) (uint8_t *, uint8_t *, - int32_t, int32_t), - int x_offset, int y_offset, int mb_y_8_offset, - int src_field, int dest_field, - int x_pred, int y_pred, - uint8_t * dest[3], uint8_t * src[3], - int stride, int height) +static inline void slice_non_intra_DCT (decoder_t * const decoder, + uint8_t * const dest, const int stride) { - MOTION_Y (table, x_offset, y_offset, x_pred, y_pred, dest, src, - dest_field + mb_y_8_offset*8*stride, src_field, stride, height); + int last; - x_pred /= 2; - y_pred /= 2; - stride >>= 1; - height >>= 1; - - MOTION_UV (table, x_offset, y_offset, x_pred, y_pred, dest, src, - (dest_field >> 1) + mb_y_8_offset*4*stride, src_field >> 1, - stride, height); + if (decoder->mpeg1) + last = get_mpeg1_non_intra_block (decoder); + else + last = get_non_intra_block (decoder); + mpeg2_idct_add (last, decoder->DCTblock, dest, stride); } -static void motion_mp1 (picture_t * picture, motion_t * motion, - uint8_t * dest[3], int offset, int stride, - void (** table) (uint8_t *, uint8_t *, int, int)) +#define MOTION(table,ref,motion_x,motion_y,size,y) \ + pos_x = 2 * decoder->offset + motion_x; \ + pos_y = 2 * decoder->v_offset + motion_y + 2 * y; \ + if ((pos_x > decoder->limit_x) || (pos_y > decoder->limit_y_ ## size)) \ + return; \ + xy_half = ((pos_y & 1) << 1) | (pos_x & 1); \ + table[xy_half] (decoder->dest[0] + y * decoder->stride + decoder->offset, \ + ref[0] + (pos_x >> 1) + (pos_y >> 1) * decoder->stride, \ + decoder->stride, size); \ + motion_x /= 2; motion_y /= 2; \ + xy_half = ((motion_y & 1) << 1) | (motion_x & 1); \ + offset = (((decoder->offset + motion_x) >> 1) + \ + ((((decoder->v_offset + motion_y) >> 1) + y/2) * \ + decoder->uv_stride)); \ + table[4+xy_half] (decoder->dest[1] + y/2 * decoder->uv_stride + \ + (decoder->offset >> 1), ref[1] + offset, \ + decoder->uv_stride, size/2); \ + table[4+xy_half] (decoder->dest[2] + y/2 * decoder->uv_stride + \ + (decoder->offset >> 1), ref[2] + offset, \ + decoder->uv_stride, size/2) + +#define MOTION_FIELD(table,ref,motion_x,motion_y,dest_field,op,src_field) \ + pos_x = 2 * decoder->offset + motion_x; \ + pos_y = decoder->v_offset + motion_y; \ + if ((pos_x > decoder->limit_x) || (pos_y > decoder->limit_y)) \ + return; \ + xy_half = ((pos_y & 1) << 1) | (pos_x & 1); \ + table[xy_half] (decoder->dest[0] + dest_field * decoder->stride + \ + decoder->offset, \ + (ref[0] + (pos_x >> 1) + \ + ((pos_y op) + src_field) * decoder->stride), \ + 2 * decoder->stride, 8); \ + motion_x /= 2; motion_y /= 2; \ + xy_half = ((motion_y & 1) << 1) | (motion_x & 1); \ + offset = (((decoder->offset + motion_x) >> 1) + \ + (((decoder->v_offset >> 1) + (motion_y op) + src_field) * \ + decoder->uv_stride)); \ + table[4+xy_half] (decoder->dest[1] + dest_field * decoder->uv_stride + \ + (decoder->offset >> 1), ref[1] + offset, \ + 2 * decoder->uv_stride, 4); \ + table[4+xy_half] (decoder->dest[2] + dest_field * decoder->uv_stride + \ + (decoder->offset >> 1), ref[2] + offset, \ + 2 * decoder->uv_stride, 4) + +static void motion_mp1 (decoder_t * const decoder, motion_t * const motion, + mpeg2_mc_fct * const * const table) { -#define bit_buf (picture->bitstream_buf) -#define bits (picture->bitstream_bits) -#define bit_ptr (picture->bitstream_ptr) +#define bit_buf (decoder->bitstream_buf) +#define bits (decoder->bitstream_bits) +#define bit_ptr (decoder->bitstream_ptr) int motion_x, motion_y; + unsigned int pos_x, pos_y, xy_half, offset; NEEDBITS (bit_buf, bits, bit_ptr); - motion_x = motion->pmv[0][0] + get_motion_delta (picture, - motion->f_code[0]); - motion_x = bound_motion_vector (motion_x, motion->f_code[0]); + motion_x = (motion->pmv[0][0] + + (get_motion_delta (decoder, + motion->f_code[0]) << motion->f_code[1])); + motion_x = bound_motion_vector (motion_x, + motion->f_code[0] + motion->f_code[1]); motion->pmv[0][0] = motion_x; NEEDBITS (bit_buf, bits, bit_ptr); - motion_y = motion->pmv[0][1] + get_motion_delta (picture, - motion->f_code[0]); - motion_y = bound_motion_vector (motion_y, motion->f_code[0]); + motion_y = (motion->pmv[0][1] + + (get_motion_delta (decoder, + motion->f_code[0]) << motion->f_code[1])); + motion_y = bound_motion_vector (motion_y, + motion->f_code[0] + motion->f_code[1]); motion->pmv[0][1] = motion_y; - if (motion->f_code[1]) { - motion_x <<= 1; - motion_y <<= 1; - } - - motion_block (table, offset, picture->v_offset, 0, 0, 0, - motion_x, motion_y, dest, motion->ref[0], stride, 16); + MOTION (table, motion->ref[0], motion_x, motion_y, 16, 0); #undef bit_buf #undef bits #undef bit_ptr } -static void motion_mp1_reuse (picture_t * picture, motion_t * motion, - uint8_t * dest[3], int offset, int stride, - void (** table) (uint8_t *, uint8_t *, int, int)) +static void motion_fr_frame (decoder_t * const decoder, + motion_t * const motion, + mpeg2_mc_fct * const * const table) { +#define bit_buf (decoder->bitstream_buf) +#define bits (decoder->bitstream_bits) +#define bit_ptr (decoder->bitstream_ptr) 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, offset, picture->v_offset, 0, 0, 0, - motion_x, motion_y, dest, motion->ref[0], stride, 16); -} - -static void motion_fr_frame (picture_t * picture, motion_t * motion, - uint8_t * dest[3], int offset, int stride, - void (** table) (uint8_t *, uint8_t *, int, int)) -{ -#define bit_buf (picture->bitstream_buf) |