2001-02-24 20:28:24 +00:00
|
|
|
/*
|
|
|
|
* slice.c
|
2003-04-06 16:36:02 +00:00
|
|
|
* Copyright (C) 2000-2002 Michel Lespinasse <walken@zoy.org>
|
|
|
|
* Copyright (C) 1999-2000 Aaron Holtzman <aholtzma@ess.engr.uvic.ca>
|
2001-02-24 20:28:24 +00:00
|
|
|
*
|
|
|
|
* This file is part of mpeg2dec, a free MPEG-2 video stream decoder.
|
2003-04-06 16:36:02 +00:00
|
|
|
* See http://libmpeg2.sourceforge.net/ for updates.
|
2001-02-24 20:28:24 +00:00
|
|
|
*
|
|
|
|
* 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 <inttypes.h>
|
|
|
|
|
2003-04-06 16:36:02 +00:00
|
|
|
#include "mpeg2.h"
|
2001-02-24 20:28:24 +00:00
|
|
|
#include "mpeg2_internal.h"
|
|
|
|
#include "attributes.h"
|
|
|
|
|
2003-04-06 16:36:02 +00:00
|
|
|
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);
|
2001-11-07 01:59:28 +00:00
|
|
|
|
2001-02-24 20:28:24 +00:00
|
|
|
#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
|
|
|
|
};
|
|
|
|
|
2003-04-06 16:36:02 +00:00
|
|
|
static inline int get_macroblock_modes (decoder_t * const decoder)
|
2001-02-24 20:28:24 +00:00
|
|
|
{
|
2003-04-06 16:36:02 +00:00
|
|
|
#define bit_buf (decoder->bitstream_buf)
|
|
|
|
#define bits (decoder->bitstream_bits)
|
|
|
|
#define bit_ptr (decoder->bitstream_ptr)
|
2001-02-24 20:28:24 +00:00
|
|
|
int macroblock_modes;
|
2003-04-06 16:36:02 +00:00
|
|
|
const MBtab * tab;
|
2001-02-24 20:28:24 +00:00
|
|
|
|
2003-04-06 16:36:02 +00:00
|
|
|
switch (decoder->coding_type) {
|
2001-02-24 20:28:24 +00:00
|
|
|
case I_TYPE:
|
|
|
|
|
|
|
|
tab = MB_I + UBITS (bit_buf, 1);
|
|
|
|
DUMPBITS (bit_buf, bits, tab->len);
|
|
|
|
macroblock_modes = tab->modes;
|
|
|
|
|
2003-04-06 16:36:02 +00:00
|
|
|
if ((! (decoder->frame_pred_frame_dct)) &&
|
|
|
|
(decoder->picture_structure == FRAME_PICTURE)) {
|
2001-02-24 20:28:24 +00:00
|
|
|
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;
|
|
|
|
|
2003-04-06 16:36:02 +00:00
|
|
|
if (decoder->picture_structure != FRAME_PICTURE) {
|
2001-02-24 20:28:24 +00:00
|
|
|
if (macroblock_modes & MACROBLOCK_MOTION_FORWARD) {
|
|
|
|
macroblock_modes |= UBITS (bit_buf, 2) * MOTION_TYPE_BASE;
|
|
|
|
DUMPBITS (bit_buf, bits, 2);
|
|
|
|
}
|
|
|
|
return macroblock_modes;
|
2003-04-06 16:36:02 +00:00
|
|
|
} else if (decoder->frame_pred_frame_dct) {
|
2001-02-24 20:28:24 +00:00
|
|
|
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;
|
|
|
|
|
2003-04-06 16:36:02 +00:00
|
|
|
if (decoder->picture_structure != FRAME_PICTURE) {
|
2001-02-24 20:28:24 +00:00
|
|
|
if (! (macroblock_modes & MACROBLOCK_INTRA)) {
|
|
|
|
macroblock_modes |= UBITS (bit_buf, 2) * MOTION_TYPE_BASE;
|
|
|
|
DUMPBITS (bit_buf, bits, 2);
|
|
|
|
}
|
|
|
|
return macroblock_modes;
|
2003-04-06 16:36:02 +00:00
|
|
|
} else if (decoder->frame_pred_frame_dct) {
|
2001-03-04 21:01:54 +00:00
|
|
|
/* if (! (macroblock_modes & MACROBLOCK_INTRA)) */
|
2001-02-24 20:28:24 +00:00
|
|
|
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
|
|
|
|
}
|
|
|
|
|
2003-04-06 16:36:02 +00:00
|
|
|
static inline int get_quantizer_scale (decoder_t * const decoder)
|
2001-02-24 20:28:24 +00:00
|
|
|
{
|
2003-04-06 16:36:02 +00:00
|
|
|
#define bit_buf (decoder->bitstream_buf)
|
|
|
|
#define bits (decoder->bitstream_bits)
|
|
|
|
#define bit_ptr (decoder->bitstream_ptr)
|
2001-02-24 20:28:24 +00:00
|
|
|
|
|
|
|
int quantizer_scale_code;
|
|
|
|
|
|
|
|
quantizer_scale_code = UBITS (bit_buf, 5);
|
|
|
|
DUMPBITS (bit_buf, bits, 5);
|
|
|
|
|
2003-04-06 16:36:02 +00:00
|
|
|
if (decoder->q_scale_type)
|
2001-02-24 20:28:24 +00:00
|
|
|
return non_linear_quantizer_scale [quantizer_scale_code];
|
|
|
|
else
|
|
|
|
return quantizer_scale_code << 1;
|
|
|
|
#undef bit_buf
|
|
|
|
#undef bits
|
|
|
|
#undef bit_ptr
|
|
|
|
}
|
|
|
|
|
2003-04-06 16:36:02 +00:00
|
|
|
static inline int get_motion_delta (decoder_t * const decoder,
|
|
|
|
const int f_code)
|
2001-02-24 20:28:24 +00:00
|
|
|
{
|
2003-04-06 16:36:02 +00:00
|
|
|
#define bit_buf (decoder->bitstream_buf)
|
|
|
|
#define bits (decoder->bitstream_bits)
|
|
|
|
#define bit_ptr (decoder->bitstream_ptr)
|
2001-02-24 20:28:24 +00:00
|
|
|
|
|
|
|
int delta;
|
|
|
|
int sign;
|
2003-04-06 16:36:02 +00:00
|
|
|
const MVtab * tab;
|
2001-02-24 20:28:24 +00:00
|
|
|
|
|
|
|
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
|
|
|
|
}
|
|
|
|
|
2003-04-06 16:36:02 +00:00
|
|
|
static inline int bound_motion_vector (const int vector, const int f_code)
|
2001-02-24 20:28:24 +00:00
|
|
|
{
|
2003-04-06 16:36:02 +00:00
|
|
|
#if 0
|
|
|
|
unsigned int limit;
|
|
|
|
int sign;
|
2001-02-24 20:28:24 +00:00
|
|
|
|
|
|
|
limit = 16 << f_code;
|
|
|
|
|
2003-04-06 16:36:02 +00:00
|
|
|
if ((unsigned int)(vector + limit) < 2 * limit)
|
|
|
|
return vector;
|
|
|
|
else {
|
|
|
|
sign = ((int32_t)vector) >> 31;
|
|
|
|
return vector - ((2 * limit) ^ sign) + sign;
|
|
|
|
}
|
2001-02-24 20:28:24 +00:00
|
|
|
#else
|
2003-04-06 16:36:02 +00:00
|
|
|
return ((int32_t)vector << (27 - f_code)) >> (27 - f_code);
|
2001-02-24 20:28:24 +00:00
|
|
|
#endif
|
|
|
|
}
|
|
|
|
|
2003-04-06 16:36:02 +00:00
|
|
|
static inline int get_dmv (decoder_t * const decoder)
|
2001-02-24 20:28:24 +00:00
|
|
|
{
|
2003-04-06 16:36:02 +00:00
|
|
|
#define bit_buf (decoder->bitstream_buf)
|
|
|
|
#define bits (decoder->bitstream_bits)
|
|
|
|
#define bit_ptr (decoder->bitstream_ptr)
|
2001-02-24 20:28:24 +00:00
|
|
|
|
2003-04-06 16:36:02 +00:00
|
|
|
const DMVtab * tab;
|
2001-02-24 20:28:24 +00:00
|
|
|
|
|
|
|
tab = DMV_2 + UBITS (bit_buf, 2);
|
|
|
|
DUMPBITS (bit_buf, bits, tab->len);
|
|
|
|
return tab->dmv;
|
|
|
|
#undef bit_buf
|
|
|
|
#undef bits
|
|
|
|
#undef bit_ptr
|
|
|
|
}
|
|
|
|
|
2003-04-06 16:36:02 +00:00
|
|
|
static inline int get_coded_block_pattern (decoder_t * const decoder)
|
2001-02-24 20:28:24 +00:00
|
|
|
{
|
2003-04-06 16:36:02 +00:00
|
|
|
#define bit_buf (decoder->bitstream_buf)
|
|
|
|
#define bits (decoder->bitstream_bits)
|
|
|
|
#define bit_ptr (decoder->bitstream_ptr)
|
2001-02-24 20:28:24 +00:00
|
|
|
|
2003-04-06 16:36:02 +00:00
|
|
|
const CBPtab * tab;
|
2001-02-24 20:28:24 +00:00
|
|
|
|
|
|
|
NEEDBITS (bit_buf, bits, bit_ptr);
|
|
|
|
|
|
|
|
if (bit_buf >= 0x20000000) {
|
|
|
|
|
2003-04-06 16:36:02 +00:00
|
|
|
tab = CBP_7 + (UBITS (bit_buf, 7) - 16);
|
2001-02-24 20:28:24 +00:00
|
|
|
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
|
|
|
|
}
|
|
|
|
|
2003-04-06 16:36:02 +00:00
|
|
|
static inline int get_luma_dc_dct_diff (decoder_t * const decoder)
|
2001-02-24 20:28:24 +00:00
|
|
|
{
|
2003-04-06 16:36:02 +00:00
|
|
|
#define bit_buf (decoder->bitstream_buf)
|
|
|
|
#define bits (decoder->bitstream_bits)
|
|
|
|
#define bit_ptr (decoder->bitstream_ptr)
|
|
|
|
const DCtab * tab;
|
2001-02-24 20:28:24 +00:00
|
|
|
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 {
|
2003-04-06 16:36:02 +00:00
|
|
|
tab = DC_long + (UBITS (bit_buf, 9) - 0x1e0);
|
2001-02-24 20:28:24 +00:00
|
|
|
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
|
|
|
|
}
|
|
|
|
|
2003-04-06 16:36:02 +00:00
|
|
|
static inline int get_chroma_dc_dct_diff (decoder_t * const decoder)
|
2001-02-24 20:28:24 +00:00
|
|
|
{
|
2003-04-06 16:36:02 +00:00
|
|
|
#define bit_buf (decoder->bitstream_buf)
|
|
|
|
#define bits (decoder->bitstream_bits)
|
|
|
|
#define bit_ptr (decoder->bitstream_ptr)
|
|
|
|
const DCtab * tab;
|
2001-02-24 20:28:24 +00:00
|
|
|
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 {
|
2003-04-06 16:36:02 +00:00
|
|
|
tab = DC_long + (UBITS (bit_buf, 10) - 0x3e0);
|
2001-02-24 20:28:24 +00:00
|
|
|
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
|
|
|
|
}
|
|
|
|
|
2003-04-06 16:36:02 +00:00
|
|
|
#define SATURATE(val) \
|
|
|
|
do { \
|
|
|
|
if (unlikely ((uint32_t)(val + 2048) > 4095)) \
|
|
|
|
val = SBITS (val, 1) ^ 2047; \
|
2001-02-24 20:28:24 +00:00
|
|
|
} while (0)
|
|
|
|
|
2003-04-06 16:36:02 +00:00
|
|
|
static void get_intra_block_B14 (decoder_t * const decoder)
|
2001-02-24 20:28:24 +00:00
|
|
|
{
|
|
|
|
int i;
|
|
|
|
int j;
|
|
|
|
int val;
|
2003-04-06 16:36:02 +00:00
|
|
|
const uint8_t * scan = decoder->scan;
|
|
|
|
const uint8_t * quant_matrix = decoder->intra_quantizer_matrix;
|
|
|
|
int quantizer_scale = decoder->quantizer_scale;
|
2001-02-24 20:28:24 +00:00
|
|
|
int mismatch;
|
2003-04-06 16:36:02 +00:00
|
|
|
const DCTtab * tab;
|
2001-02-24 20:28:24 +00:00
|
|
|
uint32_t bit_buf;
|
|
|
|
int bits;
|
2003-04-06 16:36:02 +00:00
|
|
|
const uint8_t * bit_ptr;
|
2001-03-04 21:01:54 +00:00
|
|
|
int16_t * dest;
|
2001-02-24 20:28:24 +00:00
|
|
|
|
2003-04-06 16:36:02 +00:00
|
|
|
dest = decoder->DCTblock;
|
2001-02-24 20:28:24 +00:00
|
|
|
i = 0;
|
|
|
|
mismatch = ~dest[0];
|
|
|
|
|
2003-04-06 16:36:02 +00:00
|
|
|
bit_buf = decoder->bitstream_buf;
|
|
|
|
bits = decoder->bitstream_bits;
|
|
|
|
bit_ptr = decoder->bitstream_ptr;
|
2001-02-24 20:28:24 +00:00
|
|
|
|
|
|
|
NEEDBITS (bit_buf, bits, bit_ptr);
|
|
|
|
|
|
|
|
while (1) {
|
|
|
|
if (bit_buf >= 0x28000000) {
|
|
|
|
|
2003-04-06 16:36:02 +00:00
|
|
|
tab = DCT_B14AC_5 + (UBITS (bit_buf, 5) - 5);
|
2001-02-24 20:28:24 +00:00
|
|
|
|
|
|
|
i += tab->run;
|
|
|
|
if (i >= 64)
|
2001-03-04 21:01:54 +00:00
|
|
|
break; /* end of block */
|
2001-02-24 20:28:24 +00:00
|
|
|
|
|
|
|
normal_code:
|
|
|
|
j = scan[i];
|
|
|
|
bit_buf <<= tab->len;
|
|
|
|
bits += tab->len + 1;
|
|
|
|
val = (tab->level * quantizer_scale * quant_matrix[j]) >> 4;
|
|
|
|
|
2001-03-04 21:01:54 +00:00
|
|
|
/* if (bitstream_get (1)) val = -val; */
|
2001-02-24 20:28:24 +00:00
|
|
|
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) {
|
|
|
|
|
2003-04-06 16:36:02 +00:00
|
|
|
tab = DCT_B14_8 + (UBITS (bit_buf, 8) - 4);
|
2001-02-24 20:28:24 +00:00
|
|
|
|
|
|
|
i += tab->run;
|
|
|
|
if (i < 64)
|
|
|
|
goto normal_code;
|
|
|
|
|
2001-03-04 21:01:54 +00:00
|
|
|
/* escape code */
|
2001-02-24 20:28:24 +00:00
|
|
|
|
|
|
|
i += UBITS (bit_buf << 6, 6) - 64;
|
|
|
|
if (i >= 64)
|
2001-03-04 21:01:54 +00:00
|
|
|
break; /* illegal, check needed to avoid buffer overflow */
|
2001-02-24 20:28:24 +00:00
|
|
|
|
|
|
|
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) {
|
2003-04-06 16:36:02 +00:00
|
|
|
tab = DCT_B14_10 + (UBITS (bit_buf, 10) - 8);
|
2001-02-24 20:28:24 +00:00
|
|
|
i += tab->run;
|
|
|
|
if (i < 64)
|
|
|
|
goto normal_code;
|
|
|
|
} else if (bit_buf >= 0x00800000) {
|
2003-04-06 16:36:02 +00:00
|
|
|
tab = DCT_13 + (UBITS (bit_buf, 13) - 16);
|
2001-02-24 20:28:24 +00:00
|
|
|
i += tab->run;
|
|
|
|
if (i < 64)
|
|
|
|
goto normal_code;
|
|
|
|
} else if (bit_buf >= 0x00200000) {
|
2003-04-06 16:36:02 +00:00
|
|
|
tab = DCT_15 + (UBITS (bit_buf, 15) - 16);
|
2001-02-24 20:28:24 +00:00
|
|
|
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;
|
|
|
|
}
|
2001-03-04 21:01:54 +00:00
|
|
|
break; /* illegal, check needed to avoid buffer overflow */
|
2001-02-24 20:28:24 +00:00
|
|
|
}
|
|
|
|
dest[63] ^= mismatch & 1;
|
2001-03-04 21:01:54 +00:00
|
|
|
DUMPBITS (bit_buf, bits, 2); /* dump end of block code */
|
2003-04-06 16:36:02 +00:00
|
|
|
decoder->bitstream_buf = bit_buf;
|
|
|
|
decoder->bitstream_bits = bits;
|
|
|
|
decoder->bitstream_ptr = bit_ptr;
|
2001-02-24 20:28:24 +00:00
|
|
|
}
|
|
|
|
|
2003-04-06 16:36:02 +00:00
|
|
|
static void get_intra_block_B15 (decoder_t * const decoder)
|
2001-02-24 20:28:24 +00:00
|
|
|
{
|
|
|
|
int i;
|
|
|
|
int j;
|
|
|
|
int val;
|
2003-04-06 16:36:02 +00:00
|
|
|
const uint8_t * scan = decoder->scan;
|
|
|
|
const uint8_t * quant_matrix = decoder->intra_quantizer_matrix;
|
|
|
|
int quantizer_scale = decoder->quantizer_scale;
|
2001-02-24 20:28:24 +00:00
|
|
|
int mismatch;
|
2003-04-06 16:36:02 +00:00
|
|
|
const DCTtab * tab;
|
2001-02-24 20:28:24 +00:00
|
|
|
uint32_t bit_buf;
|
|
|
|
int bits;
|
2003-04-06 16:36:02 +00:00
|
|
|
const uint8_t * bit_ptr;
|
2001-03-04 21:01:54 +00:00
|
|
|
int16_t * dest;
|
2001-02-24 20:28:24 +00:00
|
|
|
|
2003-04-06 16:36:02 +00:00
|
|
|
dest = decoder->DCTblock;
|
2001-02-24 20:28:24 +00:00
|
|
|
i = 0;
|
|
|
|
mismatch = ~dest[0];
|
|
|
|
|
2003-04-06 16:36:02 +00:00
|
|
|
bit_buf = decoder->bitstream_buf;
|
|
|
|
bits = decoder->bitstream_bits;
|
|
|
|
bit_ptr = decoder->bitstream_ptr;
|
2001-02-24 20:28:24 +00:00
|
|
|
|
|
|
|
NEEDBITS (bit_buf, bits, bit_ptr);
|
|
|
|
|
|
|
|
while (1) {
|
|
|
|
if (bit_buf >= 0x04000000) {
|
|
|
|
|
2003-04-06 16:36:02 +00:00
|
|
|
tab = DCT_B15_8 + (UBITS (bit_buf, 8) - 4);
|
2001-02-24 20:28:24 +00:00
|
|
|
|
|
|
|
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;
|
|
|
|
|
2001-03-04 21:01:54 +00:00
|
|
|
/* if (bitstream_get (1)) val = -val; */
|
2001-02-24 20:28:24 +00:00
|
|
|
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 {
|
|
|
|
|
2001-03-04 21:01:54 +00:00
|
|
|
/* end of block. I commented out this code because if we */
|
|
|
|
/* dont exit here we will still exit at the later test :) */
|
2001-02-24 20:28:24 +00:00
|
|
|
|
2001-03-04 21:01:54 +00:00
|
|
|
/* if (i >= 128) break; */ /* end of block */
|
2001-02-24 20:28:24 +00:00
|
|
|
|
2001-03-04 21:01:54 +00:00
|
|
|
/* escape code */
|
2001-02-24 20:28:24 +00:00
|
|
|
|
|
|
|
i += UBITS (bit_buf << 6, 6) - 64;
|
|
|
|
if (i >= 64)
|
2001-03-04 21:01:54 +00:00
|
|
|
break; /* illegal, check against buffer overflow */
|
2001-02-24 20:28:24 +00:00
|
|
|
|
|
|
|
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) {
|
2003-04-06 16:36:02 +00:00
|
|
|
tab = DCT_B15_10 + (UBITS (bit_buf, 10) - 8);
|
2001-02-24 20:28:24 +00:00
|
|
|
i += tab->run;
|
|
|
|
if (i < 64)
|
|
|
|
goto normal_code;
|
|
|
|
} else if (bit_buf >= 0x00800000) {
|
2003-04-06 16:36:02 +00:00
|
|
|
tab = DCT_13 + (UBITS (bit_buf, 13) - 16);
|
2001-02-24 20:28:24 +00:00
|
|
|
i += tab->run;
|
|
|
|
if (i < 64)
|
|
|
|
goto normal_code;
|
|
|
|
} else if (bit_buf >= 0x00200000) {
|
2003-04-06 16:36:02 +00:00
|
|
|
tab = DCT_15 + (UBITS (bit_buf, 15) - 16);
|
2001-02-24 20:28:24 +00:00
|
|
|
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;
|
|
|
|
}
|
2001-03-04 21:01:54 +00:00
|
|
|
break; /* illegal, check needed to avoid buffer overflow */
|
2001-02-24 20:28:24 +00:00
|
|
|
}
|
|
|
|
dest[63] ^= mismatch & 1;
|
2001-03-04 21:01:54 +00:00
|
|
|
DUMPBITS (bit_buf, bits, 4); /* dump end of block code */
|
2003-04-06 16:36:02 +00:00
|
|
|
decoder->bitstream_buf = bit_buf;
|
|
|
|
decoder->bitstream_bits = bits;
|
|
|
|
decoder->bitstream_ptr = bit_ptr;
|
2001-02-24 20:28:24 +00:00
|
|
|
}
|
|
|
|
|
2003-04-06 16:36:02 +00:00
|
|
|
static int get_non_intra_block (decoder_t * const decoder)
|
2001-02-24 20:28:24 +00:00
|
|
|
{
|
|
|
|
int i;
|
|
|
|
int j;
|
|
|
|
int val;
|
2003-04-06 16:36:02 +00:00
|
|
|
const uint8_t * scan = decoder->scan;
|
|
|
|
const uint8_t * quant_matrix = decoder->non_intra_quantizer_matrix;
|
|
|
|
int quantizer_scale = decoder->quantizer_scale;
|
2001-02-24 20:28:24 +00:00
|
|
|
int mismatch;
|
2003-04-06 16:36:02 +00:00
|
|
|
const DCTtab * tab;
|
2001-02-24 20:28:24 +00:00
|
|
|
uint32_t bit_buf;
|
|
|
|
int bits;
|
2003-04-06 16:36:02 +00:00
|
|
|
const uint8_t * bit_ptr;
|
2001-03-04 21:01:54 +00:00
|
|
|
int16_t * dest;
|
2001-02-24 20:28:24 +00:00
|
|
|
|
|
|
|
i = -1;
|
|
|
|
mismatch = 1;
|
2003-04-06 16:36:02 +00:00
|
|
|
dest = decoder->DCTblock;
|
2001-02-24 20:28:24 +00:00
|
|
|
|
2003-04-06 16:36:02 +00:00
|
|
|
bit_buf = decoder->bitstream_buf;
|
|
|
|
bits = decoder->bitstream_bits;
|
|
|
|
bit_ptr = decoder->bitstream_ptr;
|
2001-02-24 20:28:24 +00:00
|
|
|
|
|
|
|
NEEDBITS (bit_buf, bits, bit_ptr);
|
|
|
|
if (bit_buf >= 0x28000000) {
|
2003-04-06 16:36:02 +00:00
|
|
|
tab = DCT_B14DC_5 + (UBITS (bit_buf, 5) - 5);
|
2001-02-24 20:28:24 +00:00
|
|
|
goto entry_1;
|
|
|
|
} else
|
|
|
|
goto entry_2;
|
|
|
|
|
|
|
|
while (1) {
|
|
|
|
if (bit_buf >= 0x28000000) {
|
|
|
|
|
2003-04-06 16:36:02 +00:00
|
|
|
tab = DCT_B14AC_5 + (UBITS (bit_buf, 5) - 5);
|
2001-02-24 20:28:24 +00:00
|
|
|
|
|
|
|
entry_1:
|
|
|
|
i += tab->run;
|
|
|
|
if (i >= 64)
|
2001-03-04 21:01:54 +00:00
|
|
|
break; /* end of block */
|
2001-02-24 20:28:24 +00:00
|
|
|
|
|
|
|
normal_code:
|
|
|
|
j = scan[i];
|
|
|
|
bit_buf <<= tab->len;
|
|
|
|
bits += tab->len + 1;
|
|
|
|
val = ((2*tab->level+1) * quantizer_scale * quant_matrix[j]) >> 5;
|
|
|
|
|
2001-03-04 21:01:54 +00:00
|
|
|
/* if (bitstream_get (1)) val = -val; */
|
2001-02-24 20:28:24 +00:00
|
|
|
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) {
|
|
|
|
|
2003-04-06 16:36:02 +00:00
|
|
|
tab = DCT_B14_8 + (UBITS (bit_buf, 8) - 4);
|
2001-02-24 20:28:24 +00:00
|
|
|
|
|
|
|
i += tab->run;
|
|
|
|
if (i < 64)
|
|
|
|
goto normal_code;
|
|
|
|
|
2001-03-04 21:01:54 +00:00
|
|
|
/* escape code */
|
2001-02-24 20:28:24 +00:00
|
|
|
|
|
|
|
i += UBITS (bit_buf << 6, 6) - 64;
|
|
|
|
if (i >= 64)
|
2001-03-04 21:01:54 +00:00
|
|
|
break; /* illegal, check needed to avoid buffer overflow */
|
2001-02-24 20:28:24 +00:00
|
|
|
|
|
|
|
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) {
|
2003-04-06 16:36:02 +00:00
|
|
|
tab = DCT_B14_10 + (UBITS (bit_buf, 10) - 8);
|
2001-02-24 20:28:24 +00:00
|
|
|
i += tab->run;
|
|
|
|
if (i < 64)
|
|
|
|
goto normal_code;
|
|
|
|
} else if (bit_buf >= 0x00800000) {
|
2003-04-06 16:36:02 +00:00
|
|
|
tab = DCT_13 + (UBITS (bit_buf, 13) - 16);
|
2001-02-24 20:28:24 +00:00
|
|
|
i += tab->run;
|
|
|
|
if (i < 64)
|
|
|
|
goto normal_code;
|
|
|
|
} else if (bit_buf >= 0x00200000) {
|
2003-04-06 16:36:02 +00:00
|
|
|
tab = DCT_15 + (UBITS (bit_buf, 15) - 16);
|
2001-02-24 20:28:24 +00:00
|
|
|
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;
|
|
|
|
}
|
2001-03-04 21:01:54 +00:00
|
|
|
break; /* illegal, check needed to avoid buffer overflow */
|
2001-02-24 20:28:24 +00:00
|
|
|
}
|
|
|
|
dest[63] ^= mismatch & 1;
|
2001-03-04 21:01:54 +00:00
|
|
|
DUMPBITS (bit_buf, bits, 2); /* dump end of block code */
|
2003-04-06 16:36:02 +00:00
|
|
|
decoder->bitstream_buf = bit_buf;
|
|
|
|
decoder->bitstream_bits = bits;
|
|
|
|
decoder->bitstream_ptr = bit_ptr;
|
|
|
|
return i;
|
2001-02-24 20:28:24 +00:00
|
|
|
}
|
|
|
|
|
2003-04-06 16:36:02 +00:00
|
|
|
static void get_mpeg1_intra_block (decoder_t * const decoder)
|
2001-02-24 20:28:24 +00:00
|
|
|
{
|
|
|
|
int i;
|
|
|
|
int j;
|
|
|
|
int val;
|
2003-04-06 16:36:02 +00:00
|
|
|
const uint8_t * scan = decoder->scan;
|
|
|
|
const uint8_t * quant_matrix = decoder->intra_quantizer_matrix;
|
|
|
|
int quantizer_scale = decoder->quantizer_scale;
|
|
|
|
const DCTtab * tab;
|
2001-02-24 20:28:24 +00:00
|
|
|
uint32_t bit_buf;
|
|
|
|
int bits;
|
2003-04-06 16:36:02 +00:00
|
|
|
const uint8_t * bit_ptr;
|
2001-03-04 21:01:54 +00:00
|
|
|
int16_t * dest;
|
2001-02-24 20:28:24 +00:00
|
|
|
|
|
|
|
i = 0;
|
2003-04-06 16:36:02 +00:00
|
|
|
dest = decoder->DCTblock;
|
2001-02-24 20:28:24 +00:00
|
|
|
|
2003-04-06 16:36:02 +00:00
|
|
|
bit_buf = decoder->bitstream_buf;
|
|
|
|
bits = decoder->bitstream_bits;
|
|
|
|
bit_ptr = decoder->bitstream_ptr;
|
2001-02-24 20:28:24 +00:00
|
|
|
|
|
|
|
NEEDBITS (bit_buf, bits, bit_ptr);
|
|
|
|
|
|
|
|
while (1) {
|
|
|
|
if (bit_buf >= 0x28000000) {
|
|
|
|
|
2003-04-06 16:36:02 +00:00
|
|
|
tab = DCT_B14AC_5 + (UBITS (bit_buf, 5) - 5);
|
2001-02-24 20:28:24 +00:00
|
|
|
|
|
|
|
i += tab->run;
|
|
|
|
if (i >= 64)
|
2001-03-04 21:01:54 +00:00
|
|
|
break; /* end of block */
|
2001-02-24 20:28:24 +00:00
|
|
|
|
|
|
|
normal_code:
|
|
|
|
j = scan[i];
|
|
|
|
bit_buf <<= tab->len;
|
|
|
|
bits += tab->len + 1;
|
|
|
|
val = (tab->level * quantizer_scale * quant_matrix[j]) >> 4;
|
|
|
|
|
2001-03-04 21:01:54 +00:00
|
|
|
/* oddification */
|
2001-02-24 20:28:24 +00:00
|
|
|
val = (val - 1) | 1;
|
|
|
|
|
2001-03-04 21:01:54 +00:00
|
|
|
/* if (bitstream_get (1)) val = -val; */
|
2001-02-24 20:28:24 +00:00
|
|
|
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) {
|
|
|
|
|
2003-04-06 16:36:02 +00:00
|
|
|
tab = DCT_B14_8 + (UBITS (bit_buf, 8) - 4);
|
2001-02-24 20:28:24 +00:00
|
|
|
|
|
|
|
i += tab->run;
|
|
|
|
if (i < 64)
|
|
|
|
goto normal_code;
|
|
|
|
|
2001-03-04 21:01:54 +00:00
|
|
|
/* escape code */
|
2001-02-24 20:28:24 +00:00
|
|
|
|
|
|
|
i += UBITS (bit_buf << 6, 6) - 64;
|
|
|
|
if (i >= 64)
|
2001-03-04 21:01:54 +00:00
|
|
|
break; /* illegal, check needed to avoid buffer overflow */
|
2001-02-24 20:28:24 +00:00
|
|
|
|
|
|
|
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;
|
|
|
|
|
2001-03-04 21:01:54 +00:00
|
|
|
/* oddification */
|
2001-02-24 20:28:24 +00:00
|
|
|
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) {
|
2003-04-06 16:36:02 +00:00
|
|
|
tab = DCT_B14_10 + (UBITS (bit_buf, 10) - 8);
|
2001-02-24 20:28:24 +00:00
|
|
|
i += tab->run;
|
|
|
|
if (i < 64)
|
|
|
|
goto normal_code;
|
|
|
|
} else if (bit_buf >= 0x00800000) {
|
2003-04-06 16:36:02 +00:00
|
|
|
tab = DCT_13 + (UBITS (bit_buf, 13) - 16);
|
2001-02-24 20:28:24 +00:00
|
|
|
i += tab->run;
|
|
|
|
if (i < 64)
|
|
|
|
goto normal_code;
|
|
|
|
} else if (bit_buf >= 0x00200000) {
|
2003-04-06 16:36:02 +00:00
|
|
|
tab = DCT_15 + (UBITS (bit_buf, 15) - 16);
|
2001-02-24 20:28:24 +00:00
|
|
|
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;
|
|
|
|
}
|
2001-03-04 21:01:54 +00:00
|
|
|
break; /* illegal, check needed to avoid buffer overflow */
|
2001-02-24 20:28:24 +00:00
|
|
|
}
|
2001-03-04 21:01:54 +00:00
|
|
|
DUMPBITS (bit_buf, bits, 2); /* dump end of block code */
|
2003-04-06 16:36:02 +00:00
|
|
|
decoder->bitstream_buf = bit_buf;
|
|
|
|
decoder->bitstream_bits = bits;
|
|
|
|
decoder->bitstream_ptr = bit_ptr;
|
2001-02-24 20:28:24 +00:00
|
|
|
}
|
|
|
|
|
2003-04-06 16:36:02 +00:00
|
|
|
static int get_mpeg1_non_intra_block (decoder_t * const decoder)
|
2001-02-24 20:28:24 +00:00
|
|
|
{
|
|
|
|
int i;
|
|
|
|
int j;
|
|
|
|
int val;
|
2003-04-06 16:36:02 +00:00
|
|
|
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;
|
2001-02-24 20:28:24 +00:00
|
|
|
uint32_t bit_buf;
|
|
|
|
int bits;
|
2003-04-06 16:36:02 +00:00
|
|
|
const uint8_t * bit_ptr;
|
2001-03-04 21:01:54 +00:00
|
|
|
int16_t * dest;
|
2001-02-24 20:28:24 +00:00
|
|
|
|
|
|
|
i = -1;
|
2003-04-06 16:36:02 +00:00
|
|
|
dest = decoder->DCTblock;
|
2001-02-24 20:28:24 +00:00
|
|
|
|
2003-04-06 16:36:02 +00:00
|
|
|
bit_buf = decoder->bitstream_buf;
|
|
|
|
bits = decoder->bitstream_bits;
|
|
|
|
bit_ptr = decoder->bitstream_ptr;
|
2001-02-24 20:28:24 +00:00
|
|
|
|
|
|
|
NEEDBITS (bit_buf, bits, bit_ptr);
|
|
|
|
if (bit_buf >= 0x28000000) {
|
2003-04-06 16:36:02 +00:00
|
|
|
tab = DCT_B14DC_5 + (UBITS (bit_buf, 5) - 5);
|
2001-02-24 20:28:24 +00:00
|
|
|
goto entry_1;
|
|
|
|
} else
|
|
|
|
goto entry_2;
|
|
|
|
|
|
|
|
while (1) {
|
|
|
|
if (bit_buf >= 0x28000000) {
|
|
|
|
|
2003-04-06 16:36:02 +00:00
|
|
|
tab = DCT_B14AC_5 + (UBITS (bit_buf, 5) - 5);
|
2001-02-24 20:28:24 +00:00
|
|
|
|
|
|
|
entry_1:
|
|
|
|
i += tab->run;
|
|
|
|
if (i >= 64)
|
2001-03-04 21:01:54 +00:00
|
|
|
break; /* end of block */
|
2001-02-24 20:28:24 +00:00
|
|
|
|
|
|
|
normal_code:
|
|
|
|
j = scan[i];
|
|
|
|
bit_buf <<= tab->len;
|
|
|
|
bits += tab->len + 1;
|
|
|
|
val = ((2*tab->level+1) * quantizer_scale * quant_matrix[j]) >> 5;
|
|
|
|
|
2001-03-04 21:01:54 +00:00
|
|
|
/* oddification */
|
2001-02-24 20:28:24 +00:00
|
|
|
val = (val - 1) | 1;
|
|
|
|
|
2001-03-04 21:01:54 +00:00
|
|
|
/* if (bitstream_get (1)) val = -val; */
|
2001-02-24 20:28:24 +00:00
|
|
|
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) {
|
|
|
|
|
2003-04-06 16:36:02 +00:00
|
|
|
tab = DCT_B14_8 + (UBITS (bit_buf, 8) - 4);
|
2001-02-24 20:28:24 +00:00
|
|
|
|
|
|
|
i += tab->run;
|
|
|
|
if (i < 64)
|
|
|
|
goto normal_code;
|
|
|
|
|
2001-03-04 21:01:54 +00:00
|
|
|
/* escape code */
|
2001-02-24 20:28:24 +00:00
|
|
|
|
|
|
|
i += UBITS (bit_buf << 6, 6) - 64;
|
|
|
|
if (i >= 64)
|
2001-03-04 21:01:54 +00:00
|
|
|
break; /* illegal, check needed to avoid buffer overflow */
|
2001-02-24 20:28:24 +00:00
|
|
|
|
|
|
|
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;
|
|
|
|
|
2001-03-04 21:01:54 +00:00
|
|
|
/* oddification */
|
2001-02-24 20:28:24 +00:00
|
|
|
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) {
|
2003-04-06 16:36:02 +00:00
|
|
|
tab = DCT_B14_10 + (UBITS (bit_buf, 10) - 8);
|
2001-02-24 20:28:24 +00:00
|
|
|
i += tab->run;
|
|
|
|
if (i < 64)
|
|
|
|
goto normal_code;
|
|
|
|
} else if (bit_buf >= 0x00800000) {
|
2003-04-06 16:36:02 +00:00
|
|
|
tab = DCT_13 + (UBITS (bit_buf, 13) - 16);
|
2001-02-24 20:28:24 +00:00
|
|
|
i += tab->run;
|
|
|
|
if (i < 64)
|
|
|
|
goto normal_code;
|
|
|
|
} else if (bit_buf >= 0x00200000) {
|
2003-04-06 16:36:02 +00:00
|
|
|
tab = DCT_15 + (UBITS (bit_buf, 15) - 16);
|
2001-02-24 20:28:24 +00:00
|
|
|
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;
|
|
|
|
}
|
2001-03-04 21:01:54 +00:00
|
|
|
break; /* illegal, check needed to avoid buffer overflow */
|
2001-02-24 20:28:24 +00:00
|
|
|
}
|
2001-03-04 21:01:54 +00:00
|
|
|
DUMPBITS (bit_buf, bits, 2); /* dump end of block code */
|
2003-04-06 16:36:02 +00:00
|
|
|
decoder->bitstream_buf = bit_buf;
|
|
|
|
decoder->bitstream_bits = bits;
|
|
|
|
decoder->bitstream_ptr = bit_ptr;
|
|
|
|
return i;
|
2001-02-24 20:28:24 +00:00
|
|
|
}
|
|
|
|
|
2003-04-06 16:36:02 +00:00
|
|
|
static inline void slice_intra_DCT (decoder_t * const decoder, const int cc,
|
|
|
|
uint8_t * const dest, const int stride)
|
2001-02-24 20:28:24 +00:00
|
|
|
{
|
2003-04-06 16:36:02 +00:00
|
|
|
#define bit_buf (decoder->bitstream_buf)
|
|
|
|
#define bits (decoder->bitstream_bits)
|
|
|
|
#define bit_ptr (decoder->bitstream_ptr)
|
2001-02-24 20:28:24 +00:00
|
|
|
NEEDBITS (bit_buf, bits, bit_ptr);
|
2001-03-04 21:01:54 +00:00
|
|
|
/* Get the intra DC coefficient and inverse quantize it */
|
2001-02-24 20:28:24 +00:00
|
|
|
if (cc == 0)
|
2003-04-06 16:36:02 +00:00
|
|
|
decoder->dc_dct_pred[0] += get_luma_dc_dct_diff (decoder);
|
2001-02-24 20:28:24 +00:00
|
|
|
else
|
2003-04-06 16:36:02 +00:00
|
|
|
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);
|
2001-02-24 20:28:24 +00:00
|
|
|
else
|
2003-04-06 16:36:02 +00:00
|
|
|
get_intra_block_B14 (decoder);
|
|
|
|
mpeg2_idct_copy (decoder->DCTblock, dest, stride);
|
2001-02-24 20:28:24 +00:00
|
|
|
#undef bit_buf
|
|
|
|
#undef bits
|
|
|
|
#undef bit_ptr
|
|
|
|
}
|
|
|
|
|
2003-04-06 16:36:02 +00:00
|
|
|
static inline void slice_non_intra_DCT (decoder_t * const decoder,
|
|
|
|
uint8_t * const dest, const int stride)
|
2001-02-24 20:28:24 +00:00
|
|
|
{
|
2003-04-06 16:36:02 +00:00
|
|
|
int last;
|
2001-02-24 20:28:24 +00:00
|
|
|
|
2003-04-06 16:36:02 +00:00
|
|
|
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);
|
2001-02-24 20:28:24 +00:00
|
|
|
}
|
|
|
|
|
2003-04-06 16:36:02 +00:00
|
|
|
#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)
|
2001-02-24 20:28:24 +00:00
|
|
|
{
|
2003-04-06 16:36:02 +00:00
|
|
|
#define bit_buf (decoder->bitstream_buf)
|
|
|
|
#define bits (decoder->bitstream_bits)
|
|
|
|
#define bit_ptr (decoder->bitstream_ptr)
|
2001-02-24 20:28:24 +00:00
|
|
|
int motion_x, motion_y;
|
2003-04-06 16:36:02 +00:00
|
|
|
unsigned int pos_x, pos_y, xy_half, offset;
|
2001-02-24 20:28:24 +00:00
|
|
|
|
|
|
|
NEEDBITS (bit_buf, bits, bit_ptr);
|
2003-04-06 16:36:02 +00:00
|
|
|
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]);
|
2001-02-24 20:28:24 +00:00
|
|
|
motion->pmv[0][0] = motion_x;
|
|
|
|
|
|
|
|
NEEDBITS (bit_buf, bits, bit_ptr);
|
2003-04-06 16:36:02 +00:00
|
|
|
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]);
|
2001-02-24 20:28:24 +00:00
|
|
|
motion->pmv[0][1] = motion_y;
|
|
|
|
|
2003-04-06 16:36:02 +00:00
|
|
|
MOTION (table, motion->ref[0], motion_x, motion_y, 16, 0);
|
2001-02-24 20:28:24 +00:00
|
|
|
#undef bit_buf
|
|
|
|
#undef bits
|
|
|
|
#undef bit_ptr
|
|
|
|
}
|
|
|
|
|
2003-04-06 16:36:02 +00:00
|
|
|
static void motion_fr_frame (decoder_t * const decoder,
|
|
|
|
motion_t * const motion,
|
|
|
|
mpeg2_mc_fct * const * const table)
|
2001-02-24 20:28:24 +00:00
|
|
|
{
|
2003-04-06 16:36:02 +00:00
|
|
|
#define bit_buf (decoder->bitstream_buf)
|
|
|
|
#define bits (decoder->bitstream_bits)
|
|
|
|
#define bit_ptr (decoder->bitstream_ptr)
|
2001-02-24 20:28:24 +00:00
|
|
|
int motion_x, motion_y;
|
2003-04-06 16:36:02 +00:00
|
|
|
unsigned int pos_x, pos_y, xy_half, offset;
|
2001-02-24 20:28:24 +00:00
|
|
|
|
|
|
|
NEEDBITS (bit_buf, bits, bit_ptr);
|
2003-04-06 16:36:02 +00:00
|
|
|
motion_x = motion->pmv[0][0] + get_motion_delta (decoder,
|
2001-03-04 21:01:54 +00:00
|
|
|
motion->f_code[0]);
|
2001-02-24 20:28:24 +00:00
|
|
|
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);
|
2003-04-06 16:36:02 +00:00
|
|
|
motion_y = motion->pmv[0][1] + get_motion_delta (decoder,
|
2001-03-04 21:01:54 +00:00
|
|
|
motion->f_code[1]);
|
2001-02-24 20:28:24 +00:00
|
|
|
motion_y = bound_motion_vector (motion_y, motion->f_code[1]);
|
|
|
|
motion->pmv[1][1] = motion->pmv[0][1] = motion_y;
|
|
|
|
|
2003-04-06 16:36:02 +00:00
|
|
|
MOTION (table, motion->ref[0], motion_x, motion_y, 16, 0);
|
2001-02-24 20:28:24 +00:00
|
|
|
#undef bit_buf
|
|
|
|
#undef bits
|
|
|
|
#undef bit_ptr
|
|
|
|
}
|
|
|
|
|
2003-04-06 16:36:02 +00:00
|
|
|
static void motion_fr_field (decoder_t * const decoder,
|
|
|
|
motion_t * const motion,
|
|
|
|
mpeg2_mc_fct * const * const table)
|
2001-02-24 20:28:24 +00:00
|
|
|
{
|
2003-04-06 16:36:02 +00:00
|
|
|
#define bit_buf (decoder->bitstream_buf)
|
|
|
|
#define bits (decoder->bitstream_bits)
|
|
|
|
#define bit_ptr (decoder->bitstream_ptr)
|
|
|
|
int motion_x, motion_y, field;
|
|
|
|
unsigned int pos_x, pos_y, xy_half, offset;
|
2001-02-24 20:28:24 +00:00
|
|
|
|
|
|
|
NEEDBITS (bit_buf, bits, bit_ptr);
|
2003-04-06 16:36:02 +00:00
|
|
|
field = UBITS (bit_buf, 1);
|
2001-02-24 20:28:24 +00:00
|
|
|
DUMPBITS (bit_buf, bits, 1);
|
|
|
|
|
2003-04-06 16:36:02 +00:00
|
|
|
motion_x = motion->pmv[0][0] + get_motion_delta (decoder,
|
2001-03-04 21:01:54 +00:00
|
|
|
motion->f_code[0]);
|
2001-02-24 20:28:24 +00:00
|
|
|
motion_x = bound_motion_vector (motion_x, motion->f_code[0]);
|
|
|
|
motion->pmv[0][0] = motion_x;
|
|
|
|
|
|
|
|
NEEDBITS (bit_buf, bits, bit_ptr);
|
2003-04-06 16:36:02 +00:00
|
|
|
motion_y = (motion->pmv[0][1] >> 1) + get_motion_delta (decoder,
|
2001-02-24 20:28:24 +00:00
|
|
|
motion->f_code[1]);
|
2001-03-04 21:01:54 +00:00
|
|
|
/* motion_y = bound_motion_vector (motion_y, motion->f_code[1]); */
|
2001-02-24 20:28:24 +00:00
|
|
|
motion->pmv[0][1] = motion_y << 1;
|
|
|
|
|
2003-04-06 16:36:02 +00:00
|
|
|
MOTION_FIELD (table, motion->ref[0], motion_x, motion_y, 0, & ~1, field);
|
2001-02-24 20:28:24 +00:00
|
|
|
|
|
|
|
NEEDBITS (bit_buf, bits, bit_ptr);
|
2003-04-06 16:36:02 +00:00
|
|
|
field = UBITS (bit_buf, 1);
|
2001-02-24 20:28:24 +00:00
|
|
|
DUMPBITS (bit_buf, bits, 1);
|
|
|
|
|
2003-04-06 16:36:02 +00:00
|
|
|
motion_x = motion->pmv[1][0] + get_motion_delta (decoder,
|
2001-03-04 21:01:54 +00:00
|
|
|
motion->f_code[0]);
|
2001-02-24 20:28:24 +00:00
|
|
|
motion_x = bound_motion_vector (motion_x, motion->f_code[0]);
|
|
|
|
motion->pmv[1][0] = motion_x;
|
|
|
|
|
|
|
|
NEEDBITS (bit_buf, bits, bit_ptr);
|
2003-04-06 16:36:02 +00:00
|
|
|
motion_y = (motion->pmv[1][1] >> 1) + get_motion_delta (decoder,
|
2001-02-24 20:28:24 +00:00
|
|
|
motion->f_code[1]);
|
2001-03-04 21:01:54 +00:00
|
|
|
/* motion_y = bound_motion_vector (motion_y, motion->f_code[1]); */
|
2001-02-24 20:28:24 +00:00
|
|
|
motion->pmv[1][1] = motion_y << 1;
|
|
|
|
|
2003-04-06 16:36:02 +00:00
|
|
|
MOTION_FIELD (table, motion->ref[0], motion_x, motion_y, 1, & ~1, field);
|
2001-02-24 20:28:24 +00:00
|
|
|
#undef bit_buf
|
|
|
|
#undef bits
|
|
|
|
#undef bit_ptr
|
|
|
|
}
|
|
|
|
|
2003-04-06 16:36:02 +00:00
|
|
|
static void motion_fr_dmv (decoder_t * const decoder, motion_t * const motion,
|
|
|
|
mpeg2_mc_fct * const * const table)
|
2001-02-24 20:28:24 +00:00
|
|
|
{
|
2003-04-06 16:36:02 +00:00
|
|
|
#define bit_buf (decoder->bitstream_buf)
|
|
|
|
#define bits (decoder->bitstream_bits)
|
|
|
|
#define bit_ptr (decoder->bitstream_ptr)
|
|
|
|
int motion_x, motion_y, dmv_x, dmv_y, m, other_x, other_y;
|
|
|
|
unsigned int pos_x, pos_y, xy_half, offset;
|
2001-02-24 20:28:24 +00:00
|
|
|
|
|
|
|
NEEDBITS (bit_buf, bits, bit_ptr);
|
2003-04-06 16:36:02 +00:00
|
|
|
motion_x = motion->pmv[0][0] + get_motion_delta (decoder,
|
2001-03-04 21:01:54 +00:00
|
|
|
motion->f_code[0]);
|
2001-02-24 20:28:24 +00:00
|
|
|
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);
|
2003-04-06 16:36:02 +00:00
|
|
|
dmv_x = get_dmv (decoder);
|
2001-02-24 20:28:24 +00:00
|
|
|
|
2003-04-06 16:36:02 +00:00
|
|
|
motion_y = (motion->pmv[0][1] >> 1) + get_motion_delta (decoder,
|
2001-02-24 20:28:24 +00:00
|
|
|
motion->f_code[1]);
|
2001-03-04 21:01:54 +00:00
|
|
|
/* motion_y = bound_motion_vector (motion_y, motion->f_code[1]); */
|
2001-02-24 20:28:24 +00:00
|
|
|
motion->pmv[1][1] = motion->pmv[0][1] = motion_y << 1;
|
2003-04-06 16:36:02 +00:00
|
|
|
dmv_y = get_dmv (decoder);
|
2001-02-24 20:28:24 +00:00
|
|
|
|
2003-04-06 16:36:02 +00:00
|
|
|
m = decoder->top_field_first ? 1 : 3;
|
2001-02-24 20:28:24 +00:00
|
|
|
other_x = ((motion_x * m + (motion_x > 0)) >> 1) + dmv_x;
|
|
|
|
other_y = ((motion_y * m + (motion_y > 0)) >> 1) + dmv_y - 1;
|
2003-04-06 16:36:02 +00:00
|
|
|
MOTION_FIELD (mpeg2_mc.put, motion->ref[0], other_x, other_y, 0, | 1, 0);
|
2001-02-24 20:28:24 +00:00
|
|
|
|
2003-04-06 16:36:02 +00:00
|
|
|
m = decoder->top_field_first ? 3 : 1;
|
2001-02-24 20:28:24 +00:00
|
|
|
other_x = ((motion_x * m + (motion_x > 0)) >> 1) + dmv_x;
|
|
|
|
other_y = ((motion_y * m + (motion_y > 0)) >> 1) + dmv_y + 1;
|
2003-04-06 16:36:02 +00:00
|
|
|
MOTION_FIELD (mpeg2_mc.put, motion->ref[0], other_x, other_y, 1, & ~1, 0);
|
|
|
|
|
|
|
|
xy_half = ((motion_y & 1) << 1) | (motion_x & 1);
|
|
|
|
offset = (decoder->offset + (motion_x >> 1) +
|
|
|
|
(decoder->v_offset + (motion_y & ~1)) * decoder->stride);
|
|
|
|
mpeg2_mc.avg[xy_half]
|
|
|
|
(decoder->dest[0] + decoder->offset,
|
|
|
|
motion->ref[0][0] + offset, 2 * decoder->stride, 8);
|
|
|
|
mpeg2_mc.avg[xy_half]
|
|
|
|
(decoder->dest[0] + decoder->stride + decoder->offset,
|
|
|
|
motion->ref[0][0] + decoder->stride + offset, 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 & ~1)) *
|
|
|
|
decoder->uv_stride));
|
|
|
|
mpeg2_mc.avg[4+xy_half]
|
|
|
|
(decoder->dest[1] + (decoder->offset >> 1),
|
|
|
|
motion->ref[0][1] + offset, 2 * decoder->uv_stride, 4);
|
|
|
|
mpeg2_mc.avg[4+xy_half]
|
|
|
|
(decoder->dest[1] + decoder->uv_stride + (decoder->offset >> 1),
|
|
|
|
motion->ref[0][1] + decoder->uv_stride + offset,
|
|
|
|
2 * decoder->uv_stride, 4);
|
|
|
|
mpeg2_mc.avg[4+xy_half]
|
|
|
|
(decoder->dest[2] + (decoder->offset >> 1),
|
|
|
|
motion->ref[0][2] + offset, 2 * decoder->uv_stride, 4);
|
|
|
|
mpeg2_mc.avg[4+xy_half]
|
|
|
|
(decoder->dest[2] + decoder->uv_stride + (decoder->offset >> 1),
|
|
|
|
motion->ref[0][2] + decoder->uv_stride + offset,
|
|
|
|
2 * decoder->uv_stride, 4);
|
2001-02-24 20:28:24 +00:00
|
|
|
#undef bit_buf
|
|
|
|
#undef bits
|
|
|
|
#undef bit_ptr
|
|
|
|
}
|
|
|
|
|
2003-04-06 16:36:02 +00:00
|
|
|
static inline void motion_reuse (const decoder_t * const decoder,
|
|
|
|
const motion_t * const motion,
|
|
|
|
mpeg2_mc_fct * const * const table)
|
2001-02-24 20:28:24 +00:00
|
|
|
{
|
2003-04-06 16:36:02 +00:00
|
|
|
int motion_x, motion_y;
|
|
|
|
unsigned int pos_x, pos_y, xy_half, offset;
|
|
|
|
|
|
|
|
motion_x = motion->pmv[0][0];
|
|
|
|
motion_y = motion->pmv[0][1];
|
|
|
|
|
|
|
|
MOTION (table, motion->ref[0], motion_x, motion_y, 16, 0);
|
2001-02-24 20:28:24 +00:00
|
|
|
}
|
|
|
|
|
2003-04-06 16:36:02 +00:00
|
|
|
static inline void motion_zero (const decoder_t * const decoder,
|
|
|
|
const motion_t * const motion,
|
|
|
|
mpeg2_mc_fct * const * const table)
|
2001-02-24 20:28:24 +00:00
|
|
|
{
|
2003-04-06 16:36:02 +00:00
|
|
|
unsigned int offset;
|
|
|
|
|
|
|
|
table[0] (decoder->dest[0] + decoder->offset,
|
|
|
|
(motion->ref[0][0] + decoder->offset +
|
|
|
|
decoder->v_offset * decoder->stride),
|
|
|
|
decoder->stride, 16);
|
|
|
|
|
|
|
|
offset = ((decoder->offset >> 1) +
|
|
|
|
(decoder->v_offset >> 1) * decoder->uv_stride);
|
|
|
|
table[4] (decoder->dest[1] + (decoder->offset >> 1),
|
|
|
|
motion->ref[0][1] + offset, decoder->uv_stride, 8);
|
|
|
|
table[4] (decoder->dest[2] + (decoder->offset >> 1),
|
|
|
|
motion->ref[0][2] + offset, decoder->uv_stride, 8);
|
2001-02-24 20:28:24 +00:00
|
|
|
}
|
|
|
|
|
2001-03-04 21:01:54 +00:00
|
|
|
/* like motion_frame, but parsing without actual motion compensation */
|
2003-04-06 16:36:02 +00:00
|
|
|
static void motion_fr_conceal (decoder_t * const decoder)
|
2001-02-24 20:28:24 +00:00
|
|
|
{
|
2003-04-06 16:36:02 +00:00
|
|
|
#define bit_buf (decoder->bitstream_buf)
|
|
|
|
#define bits (decoder->bitstream_bits)
|
|
|
|
#define bit_ptr (decoder->bitstream_ptr)
|
2001-02-24 20:28:24 +00:00
|
|
|
int tmp;
|
|
|
|
|
|
|
|
NEEDBITS (bit_buf, bits, bit_ptr);
|
2003-04-06 16:36:02 +00:00
|
|
|
tmp = (decoder->f_motion.pmv[0][0] +
|
|
|
|
get_motion_delta (decoder, decoder->f_motion.f_code[0]));
|
|
|
|
tmp = bound_motion_vector (tmp, decoder->f_motion.f_code[0]);
|
|
|
|
decoder->f_motion.pmv[1][0] = decoder->f_motion.pmv[0][0] = tmp;
|
2001-02-24 20:28:24 +00:00
|
|
|
|
|
|
|
NEEDBITS (bit_buf, bits, bit_ptr);
|
2003-04-06 16:36:02 +00:00
|
|
|
tmp = (decoder->f_motion.pmv[0][1] +
|
|
|
|
get_motion_delta (decoder, decoder->f_motion.f_code[1]));
|
|
|
|
tmp = bound_motion_vector (tmp, decoder->f_motion.f_code[1]);
|
|
|
|
decoder->f_motion.pmv[1][1] = decoder->f_motion.pmv[0][1] = tmp;
|
2001-02-24 20:28:24 +00:00
|
|
|
|
2001-03-04 21:01:54 +00:00
|
|
|
DUMPBITS (bit_buf, bits, 1); /* remove marker_bit */
|
2001-02-24 20:28:24 +00:00
|
|
|
#undef bit_buf
|
|
|
|
#undef bits
|
|
|
|
#undef bit_ptr
|
|
|
|
}
|
|
|
|
|
2003-04-06 16:36:02 +00:00
|
|
|
static void motion_fi_field (decoder_t * const decoder,
|
|
|
|
motion_t * const motion,
|
|
|
|
mpeg2_mc_fct * const * const table)
|
2001-02-24 20:28:24 +00:00
|
|
|
{
|
2003-04-06 16:36:02 +00:00
|
|
|
#define bit_buf (decoder->bitstream_buf)
|
|
|
|
#define bits (decoder->bitstream_bits)
|
|
|
|
#define bit_ptr (decoder->bitstream_ptr)
|
2001-02-24 20:28:24 +00:00
|
|
|
int motion_x, motion_y;
|
2003-04-06 16:36:02 +00:00
|
|
|
uint8_t ** ref_field;
|
|
|
|
unsigned int pos_x, pos_y, xy_half, offset;
|
2001-02-24 20:28:24 +00:00
|
|
|
|
|
|
|
NEEDBITS (bit_buf, bits, bit_ptr);
|
2003-04-06 16:36:02 +00:00
|
|
|
ref_field = motion->ref2[UBITS (bit_buf, 1)];
|
2001-02-24 20:28:24 +00:00
|
|
|
DUMPBITS (bit_buf, bits, 1);
|
|
|
|
|
2003-04-06 16:36:02 +00:00
|
|
|
motion_x = motion->pmv[0][0] + get_motion_delta (decoder,
|
2001-03-04 21:01:54 +00:00
|
|
|
motion->f_code[0]);
|
2001-02-24 20:28:24 +00:00
|
|
|
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);
|
2003-04-06 16:36:02 +00:00
|
|
|
motion_y = motion->pmv[0][1] + get_motion_delta (decoder,
|
2001-03-04 21:01:54 +00:00
|
|
|
motion->f_code[1]);
|
2001-02-24 20:28:24 +00:00
|
|
|
motion_y = bound_motion_vector (motion_y, motion->f_code[1]);
|
|
|
|
motion->pmv[1][1] = motion->pmv[0][1] = motion_y;
|
|
|
|
|
2003-04-06 16:36:02 +00:00
|
|
|
MOTION (table, ref_field, motion_x, motion_y, 16, 0);
|
2001-02-24 20:28:24 +00:00
|
|
|
#undef bit_buf
|
|
|
|
#undef bits
|
|
|
|
#undef bit_ptr
|
|
|
|
}
|
|
|
|
|
2003-04-06 16:36:02 +00:00
|
|
|
static void motion_fi_16x8 (decoder_t * const decoder, motion_t * const motion,
|
|
|
|
mpeg2_mc_fct * const * const table)
|
2001-02-24 20:28:24 +00:00
|
|
|
{
|
2003-04-06 16:36:02 +00:00
|
|
|
#define bit_buf (decoder->bitstream_buf)
|
|
|
|
#define bits (decoder->bitstream_bits)
|
|
|
|
#define bit_ptr (decoder->bitstream_ptr)
|
2001-02-24 20:28:24 +00:00
|
|
|
int motion_x, motion_y;
|
2003-04-06 16:36:02 +00:00
|
|
|
uint8_t ** ref_field;
|
|
|
|
unsigned int pos_x, pos_y, xy_half, offset;
|
2001-02-24 20:28:24 +00:00
|
|
|
|
|
|
|
NEEDBITS (bit_buf, bits, bit_ptr);
|
2003-04-06 16:36:02 +00:00
|
|
|
ref_field = motion->ref2[UBITS (bit_buf, 1)];
|
2001-02-24 20:28:24 +00:00
|
|
|
DUMPBITS (bit_buf, bits, 1);
|
|
|
|
|
2003-04-06 16:36:02 +00:00
|
|
|
motion_x = motion->pmv[0][0] + get_motion_delta (decoder,
|
2001-03-04 21:01:54 +00:00
|
|
|
motion->f_code[0]);
|
2001-02-24 20:28:24 +00:00
|
|
|
motion_x = bound_motion_vector (motion_x, motion->f_code[0]);
|
|
|
|
motion->pmv[0][0] = motion_x;
|
|
|
|
|
|
|
|
NEEDBITS (bit_buf, bits, bit_ptr);
|
2003-04-06 16:36:02 +00:00
|
|
|
motion_y = motion->pmv[0][1] + get_motion_delta (decoder,
|
2001-03-04 21:01:54 +00:00
|
|
|
motion->f_code[1]);
|
2001-02-24 20:28:24 +00:00
|
|
|
motion_y = bound_motion_vector (motion_y, motion->f_code[1]);
|
|
|
|
motion->pmv[0][1] = motion_y;
|
|
|
|
|
2003-04-06 16:36:02 +00:00
|
|
|
MOTION (table, ref_field, motion_x, motion_y, 8, 0);
|
2001-02-24 20:28:24 +00:00
|
|
|
|
|
|
|
NEEDBITS (bit_buf, bits, bit_ptr);
|
2003-04-06 16:36:02 +00:00
|
|
|
ref_field = motion->ref2[UBITS (bit_buf, 1)];
|
2001-02-24 20:28:24 +00:00
|
|
|
DUMPBITS (bit_buf, bits, 1);
|
|
|
|
|
2003-04-06 16:36:02 +00:00
|
|
|
motion_x = motion->pmv[1][0] + get_motion_delta (decoder,
|
2001-03-04 21:01:54 +00:00
|
|
|
motion->f_code[0]);
|
2001-02-24 20:28:24 +00:00
|
|
|
motion_x = bound_motion_vector (motion_x, motion->f_code[0]);
|
|
|
|
motion->pmv[1][0] = motion_x;
|
|
|
|
|
|
|
|
NEEDBITS (bit_buf, bits, bit_ptr);
|
2003-04-06 16:36:02 +00:00
|
|
|
motion_y = motion->pmv[1][1] + get_motion_delta (decoder,
|
2001-03-04 21:01:54 +00:00
|
|
|
motion->f_code[1]);
|
2001-02-24 20:28:24 +00:00
|
|
|
motion_y = bound_motion_vector (motion_y, motion->f_code[1]);
|
|
|
|
motion->pmv[1][1] = motion_y;
|
|
|
|
|
2003-04-06 16:36:02 +00:00
|
|
|
MOTION (table, ref_field, motion_x, motion_y, 8, 8);
|
2001-02-24 20:28:24 +00:00
|
|
|
#undef bit_buf
|
|
|
|
#undef bits
|
|
|
|
#undef bit_ptr
|
|
|
|
}
|
|
|
|
|
2003-04-06 16:36:02 +00:00
|
|
|
static void motion_fi_dmv (decoder_t * const decoder, motion_t * const motion,
|
|
|
|
mpeg2_mc_fct * const * const table)
|
2001-02-24 20:28:24 +00:00
|
|
|
{
|
2003-04-06 16:36:02 +00:00
|
|
|
#define bit_buf (decoder->bitstream_buf)
|
|
|
|
#define bits (decoder->bitstream_bits)
|
|
|
|
#define bit_ptr (decoder->bitstream_ptr)
|
|
|
|
int motion_x, motion_y, other_x, other_y;
|
|
|
|
unsigned int pos_x, pos_y, xy_half, offset;
|
2001-02-24 20:28:24 +00:00
|
|
|
|
|
|
|
NEEDBITS (bit_buf, bits, bit_ptr);
|
2003-04-06 16:36:02 +00:00
|
|
|
motion_x = motion->pmv[0][0] + get_motion_delta (decoder,
|
2001-03-04 21:01:54 +00:00
|
|
|
motion->f_code[0]);
|
2001-02-24 20:28:24 +00:00
|
|
|
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);
|
2003-04-06 16:36:02 +00:00
|
|
|
other_x = ((motion_x + (motion_x > 0)) >> 1) + get_dmv (decoder);
|
2001-02-24 20:28:24 +00:00
|
|
|
|
2003-04-06 16:36:02 +00:00
|
|
|
motion_y = motion->pmv[0][1] + get_motion_delta (decoder,
|
2001-03-04 21:01:54 +00:00
|
|
|
motion->f_code[1]);
|
2001-02-24 20:28:24 +00:00
|
|
|
motion_y = bound_motion_vector (motion_y, motion->f_code[1]);
|
|
|
|
motion->pmv[1][1] = motion->pmv[0][1] = motion_y;
|
2003-04-06 16:36:02 +00:00
|
|
|
other_y = (((motion_y + (motion_y > 0)) >> 1) + get_dmv (decoder) +
|
|
|
|
decoder->dmv_offset);
|
2001-02-24 20:28:24 +00:00
|
|
|
|
2003-04-06 16:36:02 +00:00
|
|
|
MOTION (mpeg2_mc.put, motion->ref[0], motion_x, motion_y, 16, 0);
|
|
|
|
MOTION (mpeg2_mc.avg, motion->ref[1], other_x, other_y, 16, 0);
|
2001-02-24 20:28:24 +00:00
|
|
|
#undef bit_buf
|
|
|
|
#undef bits
|
|
|
|
#undef bit_ptr
|
|
|
|
}
|
|
|
|
|
2003-04-06 16:36:02 +00:00
|
|
|
static void motion_fi_conceal (decoder_t * const decoder)
|
2001-02-24 20:28:24 +00:00
|
|
|
{
|
2003-04-06 16:36:02 +00:00
|
|
|
#define bit_buf (decoder->bitstream_buf)
|
|
|
|
#define bits (decoder->bitstream_bits)
|
|
|
|
#define bit_ptr (decoder->bitstream_ptr)
|
2001-02-24 20:28:24 +00:00
|
|
|
int tmp;
|
|
|
|
|
|
|
|
NEEDBITS (bit_buf, bits, bit_ptr);
|
2001-03-04 21:01:54 +00:00
|
|
|
DUMPBITS (bit_buf, bits, 1); /* remove field_select */
|
2001-02-24 20:28:24 +00:00
|
|
|
|
2003-04-06 16:36:02 +00:00
|
|
|
tmp = (decoder->f_motion.pmv[0][0] +
|
|
|
|
get_motion_delta (decoder, decoder->f_motion.f_code[0]));
|
|
|
|
tmp = bound_motion_vector (tmp, decoder->f_motion.f_code[0]);
|
|
|
|
decoder->f_motion.pmv[1][0] = decoder->f_motion.pmv[0][0] = tmp;
|
2001-02-24 20:28:24 +00:00
|
|
|
|
|
|
|
NEEDBITS (bit_buf, bits, bit_ptr);
|
2003-04-06 16:36:02 +00:00
|
|
|
tmp = (decoder->f_motion.pmv[0][1] +
|
|
|
|
get_motion_delta (decoder, decoder->f_motion.f_code[1]));
|
|
|
|
tmp = bound_motion_vector (tmp, decoder->f_motion.f_code[1]);
|
|
|
|
decoder->f_motion.pmv[1][1] = decoder->f_motion.pmv[0][1] = tmp;
|
2001-02-24 20:28:24 +00:00
|
|
|
|
2001-03-04 21:01:54 +00:00
|
|
|
DUMPBITS (bit_buf, bits, 1); /* remove marker_bit */
|
2001-02-24 20:28:24 +00:00
|
|
|
#undef bit_buf
|
|
|
|
#undef bits
|
|
|
|
#undef bit_ptr
|
|
|
|
}
|
|
|
|
|
2003-04-06 16:36:02 +00:00
|
|
|
#define MOTION_CALL(routine,direction) \
|
|
|
|
do { \
|
|
|
|
if ((direction) & MACROBLOCK_MOTION_FORWARD) \
|
|
|
|
routine (decoder, &(decoder->f_motion), mpeg2_mc.put); \
|
|
|
|
if ((direction) & MACROBLOCK_MOTION_BACKWARD) \
|
|
|
|
routine (decoder, &(decoder->b_motion), \
|
|
|
|
((direction) & MACROBLOCK_MOTION_FORWARD ? \
|
|
|
|
mpeg2_mc.avg : mpeg2_mc.put)); \
|
2001-02-24 20:28:24 +00:00
|
|
|
} while (0)
|
|
|
|
|
2003-04-06 16:36:02 +00:00
|
|
|
#define NEXT_MACROBLOCK \
|
2001-03-04 21:01:54 +00:00
|
|
|
do { \
|
2003-04-06 16:37:37 +00:00
|
|
|
if(decoder->quant_store) \
|
|
|
|
decoder->quant_store[decoder->quant_stride*(decoder->v_offset>>4) \
|
|
|
|
+(decoder->offset>>4)] = decoder->quantizer_scale; \
|
2003-04-06 16:36:02 +00:00
|
|
|
decoder->offset += 16; \
|
|
|
|
if (decoder->offset == decoder->width) { \
|
2001-03-04 21:01:54 +00:00
|
|
|
do { /* just so we can use the break statement */ \
|
2003-04-06 16:36:02 +00:00
|
|
|
if (decoder->convert) { \
|
|
|
|
decoder->convert (decoder->fbuf_id, decoder->dest, \
|
|
|
|
decoder->v_offset); \
|
|
|
|
if (decoder->coding_type == B_TYPE) \
|
2001-03-04 21:01:54 +00:00
|
|
|
break; \
|
|
|
|
} \
|
2003-04-06 16:36:02 +00:00
|
|
|
decoder->dest[0] += 16 * decoder->stride; \
|
|
|
|
decoder->dest[1] += 4 * decoder->stride; \
|
|
|
|
decoder->dest[2] += 4 * decoder->stride; \
|
2001-03-04 21:01:54 +00:00
|
|
|
} while (0); \
|
2003-04-06 16:36:02 +00:00
|
|
|
decoder->v_offset += 16; \
|
|
|
|
if (decoder->v_offset > decoder->limit_y) { \
|
|
|
|
if (mpeg2_cpu_state_restore) \
|
|
|
|
mpeg2_cpu_state_restore (&cpu_state); \
|
|
|
|
return; \
|
|
|
|
} \
|
|
|
|
decoder->offset = 0; \
|
2001-03-04 21:01:54 +00:00
|
|
|
} \
|
2001-02-24 20:28:24 +00:00
|
|
|
} while (0)
|
|
|
|
|
2003-04-06 16:36:02 +00:00
|
|
|
void mpeg2_init_fbuf (decoder_t * decoder, uint8_t * current_fbuf[3],
|
|
|
|
uint8_t * forward_fbuf[3], uint8_t * backward_fbuf[3])
|
2001-02-24 20:28:24 +00:00
|
|
|
{
|
2003-04-06 16:36:02 +00:00
|
|
|
int offset, stride, height, bottom_field;
|
2001-02-24 20:28:24 +00:00
|
|
|
|
2003-04-06 16:36:02 +00:00
|
|
|
stride = decoder->width;
|
|
|
|
bottom_field = (decoder->picture_structure == BOTTOM_FIELD);
|
|
|
|
offset = bottom_field ? stride : 0;
|
|
|
|
height = decoder->height;
|
2001-03-07 01:15:02 +00:00
|
|
|
|
2003-04-06 16:36:02 +00:00
|
|
|
decoder->picture_dest[0] = current_fbuf[0] + offset;
|
|
|
|
decoder->picture_dest[1] = current_fbuf[1] + (offset >> 1);
|
|
|
|
decoder->picture_dest[2] = current_fbuf[2] + (offset >> 1);
|
2001-03-07 01:15:02 +00:00
|
|
|
|
2003-04-06 16:36:02 +00:00
|
|
|
decoder->f_motion.ref[0][0] = forward_fbuf[0] + offset;
|
|
|
|
decoder->f_motion.ref[0][1] = forward_fbuf[1] + (offset >> 1);
|
|
|
|
decoder->f_motion.ref[0][2] = forward_fbuf[2] + (offset >> 1);
|
2001-03-07 01:15:02 +00:00
|
|
|
|
2003-04-06 16:36:02 +00:00
|
|
|
decoder->b_motion.ref[0][0] = backward_fbuf[0] + offset;
|
|
|
|
decoder->b_motion.ref[0][1] = backward_fbuf[1] + (offset >> 1);
|
|
|
|
decoder->b_motion.ref[0][2] = backward_fbuf[2] + (offset >> 1);
|
2001-03-04 21:01:54 +00:00
|
|
|
|
2003-04-06 16:36:02 +00:00
|
|
|
if (decoder->picture_structure != FRAME_PICTURE) {
|
|
|
|
decoder->dmv_offset = bottom_field ? 1 : -1;
|
|
|
|
decoder->f_motion.ref2[0] = decoder->f_motion.ref[bottom_field];
|
|
|
|
decoder->f_motion.ref2[1] = decoder->f_motion.ref[!bottom_field];
|
|
|
|
decoder->b_motion.ref2[0] = decoder->b_motion.ref[bottom_field];
|
|
|
|
decoder->b_motion.ref2[1] = decoder->b_motion.ref[!bottom_field];
|
|
|
|
offset = stride - offset;
|
2001-02-24 20:28:24 +00:00
|
|
|
|
2003-04-06 16:36:02 +00:00
|
|
|
if (decoder->second_field && (decoder->coding_type != B_TYPE))
|
|
|
|
forward_fbuf = current_fbuf;
|
|
|
|
|
|
|
|
decoder->f_motion.ref[1][0] = forward_fbuf[0] + offset;
|
|
|
|
decoder->f_motion.ref[1][1] = forward_fbuf[1] + (offset >> 1);
|
|
|
|
decoder->f_motion.ref[1][2] = forward_fbuf[2] + (offset >> 1);
|
|
|
|
|
|
|
|
decoder->b_motion.ref[1][0] = backward_fbuf[0] + offset;
|
|
|
|
decoder->b_motion.ref[1][1] = backward_fbuf[1] + (offset >> 1);
|
|
|
|
decoder->b_motion.ref[1][2] = backward_fbuf[2] + (offset >> 1);
|
2001-02-24 20:28:24 +00:00
|
|
|
|
2001-03-04 21:01:54 +00:00
|
|
|
stride <<= 1;
|
2003-04-06 16:36:02 +00:00
|
|
|
height >>= 1;
|
2001-02-24 20:28:24 +00:00
|
|
|
}
|
|
|
|
|
2003-04-06 16:36:02 +00:00
|
|
|
decoder->stride = stride;
|
|
|
|
decoder->uv_stride = stride >> 1;
|
|
|
|
decoder->limit_x = 2 * decoder->width - 32;
|
|
|
|
decoder->limit_y_16 = 2 * height - 32;
|
|
|
|
decoder->limit_y_8 = 2 * height - 16;
|
|
|
|
decoder->limit_y = height - 16;
|
|
|
|
}
|
2001-02-24 20:28:24 +00:00
|
|
|
|
2003-04-06 16:36:02 +00:00
|
|
|
static inline int slice_init (decoder_t * const decoder, int code)
|
|
|
|
{
|
|
|
|
#define bit_buf (decoder->bitstream_buf)
|
|
|
|
#define bits (decoder->bitstream_bits)
|
|
|
|
#define bit_ptr (decoder->bitstream_ptr)
|
|
|
|
int offset;
|
|
|
|
const MBAtab * mba;
|
2001-02-24 20:28:24 +00:00
|
|
|
|
2003-04-06 16:36:02 +00:00
|
|
|
decoder->dc_dct_pred[0] = decoder->dc_dct_pred[1] =
|
|
|
|
decoder->dc_dct_pred[2] = 128 << decoder->intra_dc_precision;
|
|
|
|
|
|
|
|
decoder->f_motion.pmv[0][0] = decoder->f_motion.pmv[0][1] = 0;
|
|
|
|
decoder->f_motion.pmv[1][0] = decoder->f_motion.pmv[1][1] = 0;
|
|
|
|
decoder->b_motion.pmv[0][0] = decoder->b_motion.pmv[0][1] = 0;
|
|
|
|
decoder->b_motion.pmv[1][0] = decoder->b_motion.pmv[1][1] = 0;
|
|
|
|
|
|
|
|
if (decoder->vertical_position_extension) {
|
|
|
|
code += UBITS (bit_buf, 3) << 7;
|
|
|
|
DUMPBITS (bit_buf, bits, 3);
|
|
|
|
}
|
|
|
|
decoder->v_offset = (code - 1) * 16;
|
|
|
|
offset = 0;
|
|
|
|
if (!(decoder->convert) || decoder->coding_type != B_TYPE)
|
|
|
|
offset = (code - 1) * decoder->stride * 4;
|
|
|
|
|
|
|
|
decoder->dest[0] = decoder->picture_dest[0] + offset * 4;
|
|
|
|
decoder->dest[1] = decoder->picture_dest[1] + offset;
|
|
|
|
decoder->dest[2] = decoder->picture_dest[2] + offset;
|
|
|
|
|
|
|
|
decoder->quantizer_scale = get_quantizer_scale (decoder);
|
2001-02-24 20:28:24 +00:00
|
|
|
|
2001-03-04 21:01:54 +00:00
|
|
|
/* ignore intra_slice and all the extra data */
|
2001-02-24 20:28:24 +00:00
|
|
|
while (bit_buf & 0x80000000) {
|
|
|
|
DUMPBITS (bit_buf, bits, 9);
|
|
|
|
NEEDBITS (bit_buf, bits, bit_ptr);
|
|
|
|
}
|
|
|
|
|
2003-04-06 16:36:02 +00:00
|
|
|
/* decode initial macroblock address increment */
|
|
|
|
offset = 0;
|
|
|
|
while (1) {
|
|
|
|
if (bit_buf >= 0x08000000) {
|
|
|
|
mba = MBA_5 + (UBITS (bit_buf, 6) - 2);
|
|
|
|
break;
|
|
|
|
} else if (bit_buf >= 0x01800000) {
|
|
|
|
mba = MBA_11 + (UBITS (bit_buf, 12) - 24);
|
|
|
|
break;
|
|
|
|
} else switch (UBITS (bit_buf, 12)) {
|
|
|
|
case 8: /* macroblock_escape */
|
|
|
|
offset += 33;
|
|
|
|
DUMPBITS (bit_buf, bits, 11);
|
|
|
|
NEEDBITS (bit_buf, bits, bit_ptr);
|
|
|
|
continue;
|
|
|
|
case 15: /* macroblock_stuffing (MPEG1 only) */
|
|
|
|
bit_buf &= 0xfffff;
|
|
|
|
DUMPBITS (bit_buf, bits, 11);
|
|
|
|
NEEDBITS (bit_buf, bits, bit_ptr);
|
|
|
|
continue;
|
|
|
|
default: /* error */
|
|
|
|
return 1;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
DUMPBITS (bit_buf, bits, mba->len + 1);
|
|
|
|
decoder->offset = (offset + mba->mba) << 4;
|
|
|
|
|
|
|
|
while (decoder->offset - decoder->width >= 0) {
|
|
|
|
decoder->offset -= decoder->width;
|
|
|
|
if (!(decoder->convert) || decoder->coding_type != B_TYPE) {
|
|
|
|
decoder->dest[0] += 16 * decoder->stride;
|
|
|
|
decoder->dest[1] += 4 * decoder->stride;
|
|
|
|
decoder->dest[2] += 4 * decoder->stride;
|
|
|
|
}
|
|
|
|
decoder->v_offset += 16;
|
|
|
|
}
|
|
|
|
if (decoder->v_offset > decoder->limit_y)
|
|
|
|
return 1;
|
|
|
|
|
|
|
|
return 0;
|
|
|
|
#undef bit_buf
|
|
|
|
#undef bits
|
|
|
|
#undef bit_ptr
|
|
|
|
}
|
|
|
|
|
|
|
|
void mpeg2_slice (decoder_t * const decoder, const int code,
|
|
|
|
const uint8_t * const buffer)
|
|
|
|
{
|
|
|
|
#define bit_buf (decoder->bitstream_buf)
|
|
|
|
#define bits (decoder->bitstream_bits)
|
|
|
|
#define bit_ptr (decoder->bitstream_ptr)
|
|
|
|
cpu_state_t cpu_state;
|
|
|
|
|
|
|
|
bitstream_init (decoder, buffer);
|
|
|
|
|
|
|
|
if (slice_init (decoder, code))
|
|
|
|
return;
|
|
|
|
|
|
|
|
if (mpeg2_cpu_state_save)
|
|
|
|
mpeg2_cpu_state_save (&cpu_state);
|
2001-02-24 20:28:24 +00:00
|
|
|
|
|
|
|
while (1) {
|
2003-04-06 16:36:02 +00:00
|
|
|
int macroblock_modes;
|
|
|
|
int mba_inc;
|
|
|
|
const MBAtab * mba;
|
|
|
|
|
2001-02-24 20:28:24 +00:00
|
|
|
NEEDBITS (bit_buf, bits, bit_ptr);
|
|
|
|
|
2003-04-06 16:36:02 +00:00
|
|
|
macroblock_modes = get_macroblock_modes (decoder);
|
2001-02-24 20:28:24 +00:00
|
|
|
|
2001-03-04 21:01:54 +00:00
|
|
|
/* maybe integrate MACROBLOCK_QUANT test into get_macroblock_modes ? */
|
2001-02-24 20:28:24 +00:00
|
|
|
if (macroblock_modes & MACROBLOCK_QUANT)
|
2003-04-06 16:36:02 +00:00
|
|
|
decoder->quantizer_scale = get_quantizer_scale (decoder);
|
2001-02-24 20:28:24 +00:00
|
|
|
|
|
|
|
if (macroblock_modes & MACROBLOCK_INTRA) {
|
|
|
|
|
|
|
|
int DCT_offset, DCT_stride;
|
2003-04-06 16:36:02 +00:00
|
|
|
int offset;
|
|
|
|
uint8_t * dest_y;
|
2001-02-24 20:28:24 +00:00
|
|
|
|
2003-04-06 16:36:02 +00:00
|
|
|
if (decoder->concealment_motion_vectors) {
|
|
|
|
if (decoder->picture_structure == FRAME_PICTURE)
|
|
|
|
motion_fr_conceal (decoder);
|
2001-02-24 20:28:24 +00:00
|
|
|
else
|
2003-04-06 16:36:02 +00:00
|
|
|
motion_fi_conceal (decoder);
|
2001-02-24 20:28:24 +00:00
|
|
|
} else {
|
2003-04-06 16:36:02 +00:00
|
|
|
decoder->f_motion.pmv[0][0] = decoder->f_motion.pmv[0][1] = 0;
|
|
|
|
decoder->f_motion.pmv[1][0] = decoder->f_motion.pmv[1][1] = 0;
|
|
|
|
decoder->b_motion.pmv[0][0] = decoder->b_motion.pmv[0][1] = 0;
|
|
|
|
decoder->b_motion.pmv[1][0] = decoder->b_motion.pmv[1][1] = 0;
|
2001-02-24 20:28:24 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
if (macroblock_modes & DCT_TYPE_INTERLACED) {
|
2003-04-06 16:36:02 +00:00
|
|
|
DCT_offset = decoder->stride;
|
|
|
|
DCT_stride = decoder->stride * 2;
|
2001-02-24 20:28:24 +00:00
|
|
|
} else {
|
2003-04-06 16:36:02 +00:00
|
|
|
DCT_offset = decoder->stride * 8;
|
|
|
|
DCT_stride = decoder->stride;
|
2001-02-24 20:28:24 +00:00
|
|
|
}
|
|
|
|
|
2003-04-06 16:36:02 +00:00
|
|
|
offset = decoder->offset;
|
|
|
|
dest_y = decoder->dest[0] + offset;
|
|
|
|
slice_intra_DCT (decoder, 0, dest_y, DCT_stride);
|
|
|
|
slice_intra_DCT (decoder, 0, dest_y + 8, DCT_stride);
|
|
|
|
slice_intra_DCT (decoder, 0, dest_y + DCT_offset, DCT_stride);
|
|
|
|
slice_intra_DCT (decoder, 0, dest_y + DCT_offset + 8, DCT_stride);
|
|
|
|
slice_intra_DCT (decoder, 1, decoder->dest[1] + (offset >> 1),
|
|
|
|
decoder->uv_stride);
|
|
|
|
slice_intra_DCT (decoder, 2, decoder->dest[2] + (offset >> 1),
|
|
|
|
decoder->uv_stride);
|
|
|
|
|
|
|
|
if (decoder->coding_type == D_TYPE) {
|
2001-02-24 20:28:24 +00:00
|
|
|
NEEDBITS (bit_buf, bits, bit_ptr);
|
|
|
|
DUMPBITS (bit_buf, bits, 1);
|
|
|
|
}
|
|
|
|
} else {
|
|
|
|
|
2003-04-06 16:36:02 +00:00
|
|
|
if (decoder->picture_structure == FRAME_PICTURE)
|
2001-02-24 20:28:24 +00:00
|
|
|
switch (macroblock_modes & MOTION_TYPE_MASK) {
|
|
|
|
case MC_FRAME:
|
2003-04-06 16:36:02 +00:00
|
|
|
if (decoder->mpeg1)
|
|
|
|
MOTION_CALL (motion_mp1, macroblock_modes);
|
|
|
|
else
|
|
|
|
MOTION_CALL (motion_fr_frame, macroblock_modes);
|
2001-02-24 20:28:24 +00:00
|
|
|
break;
|
|
|
|
|
|
|
|
case MC_FIELD:
|
2003-04-06 16:36:02 +00:00
|
|
|
MOTION_CALL (motion_fr_field, macroblock_modes);
|
2001-02-24 20:28:24 +00:00
|
|
|
break;
|
|
|
|
|
|
|
|
case MC_DMV:
|
2003-04-06 16:36:02 +00:00
|
|
|
MOTION_CALL (motion_fr_dmv, MACROBLOCK_MOTION_FORWARD);
|
2001-02-24 20:28:24 +00:00
|
|
|
break;
|
|
|
|
|
|
|
|
case 0:
|
2001-03-04 21:01:54 +00:00
|
|
|
/* non-intra mb without forward mv in a P picture */
|
2003-04-06 16:36:02 +00:00
|
|
|
decoder->f_motion.pmv[0][0] = 0;
|
|
|
|
decoder->f_motion.pmv[0][1] = 0;
|
|
|
|
decoder->f_motion.pmv[1][0] = 0;
|
|
|
|
decoder->f_motion.pmv[1][1] = 0;
|
|
|
|
MOTION_CALL (motion_zero, MACROBLOCK_MOTION_FORWARD);
|
2001-02-24 20:28:24 +00:00
|
|
|
break;
|
|
|
|
}
|
|
|
|
else
|
|
|
|
switch (macroblock_modes & MOTION_TYPE_MASK) {
|
|
|
|
case MC_FIELD:
|
2003-04-06 16:36:02 +00:00
|
|
|
MOTION_CALL (motion_fi_field, macroblock_modes);
|
2001-02-24 20:28:24 +00:00
|
|
|
break;
|
|
|
|
|
|
|
|
case MC_16X8:
|
2003-04-06 16:36:02 +00:00
|
|
|
MOTION_CALL (motion_fi_16x8, macroblock_modes);
|
2001-02-24 20:28:24 +00:00
|
|
|
break;
|
|
|
|
|
|
|
|
case MC_DMV:
|
2003-04-06 16:36:02 +00:00
|
|
|
MOTION_CALL (motion_fi_dmv, MACROBLOCK_MOTION_FORWARD);
|
2001-02-24 20:28:24 +00:00
|
|
|
break;
|
|
|
|
|
|
|
|
case 0:
|
2001-03-04 21:01:54 +00:00
|
|
|
/* non-intra mb without forward mv in a P picture */
|
2003-04-06 16:36:02 +00:00
|
|
|
decoder->f_motion.pmv[0][0] = 0;
|
|
|
|
decoder->f_motion.pmv[0][1] = 0;
|
|
|
|
decoder->f_motion.pmv[1][0] = 0;
|
|
|
|
decoder->f_motion.pmv[1][1] = 0;
|
|
|
|
MOTION_CALL (motion_zero, MACROBLOCK_MOTION_FORWARD);
|
2001-02-24 20:28:24 +00:00
|
|
|
break;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (macroblock_modes & MACROBLOCK_PATTERN) {
|
|
|
|
int coded_block_pattern;
|
|
|
|
int DCT_offset, DCT_stride;
|
2003-04-06 16:36:02 +00:00
|
|
|
int offset;
|
|
|
|
uint8_t * dest_y;
|
2001-02-24 20:28:24 +00:00
|
|
|
|
|
|
|
if (macroblock_modes & DCT_TYPE_INTERLACED) {
|
2003-04-06 16:36:02 +00:00
|
|
|
DCT_offset = decoder->stride;
|
|
|
|
DCT_stride = decoder->stride * 2;
|
2001-02-24 20:28:24 +00:00
|
|
|
} else {
|
2003-04-06 16:36:02 +00:00
|
|
|
DCT_offset = decoder->stride * 8;
|
|
|
|
DCT_stride = decoder->stride;
|
2001-02-24 20:28:24 +00:00
|
|
|
}
|
|
|
|
|
2003-04-06 16:36:02 +00:00
|
|
|
coded_block_pattern = get_coded_block_pattern (decoder);
|
2001-02-24 20:28:24 +00:00
|
|
|
|
2003-04-06 16:36:02 +00:00
|
|
|
offset = decoder->offset;
|
|
|
|
dest_y = decoder->dest[0] + offset;
|
2001-02-24 20:28:24 +00:00
|
|
|
if (coded_block_pattern & 0x20)
|
2003-04-06 16:36:02 +00:00
|
|
|
slice_non_intra_DCT (decoder, dest_y, DCT_stride);
|
2001-02-24 20:28:24 +00:00
|
|
|
if (coded_block_pattern & 0x10)
|
2003-04-06 16:36:02 +00:00
|
|
|
slice_non_intra_DCT (decoder, dest_y + 8, DCT_stride);
|
2001-02-24 20:28:24 +00:00
|
|
|
if (coded_block_pattern & 0x08)
|
2003-04-06 16:36:02 +00:00
|
|
|
slice_non_intra_DCT (decoder, dest_y + DCT_offset,
|
2001-02-24 20:28:24 +00:00
|
|
|
DCT_stride);
|
|
|
|
if (coded_block_pattern & 0x04)
|
2003-04-06 16:36:02 +00:00
|
|
|
slice_non_intra_DCT (decoder, dest_y + DCT_offset + 8,
|
2001-02-24 20:28:24 +00:00
|
|
|
DCT_stride);
|
|
|
|
if (coded_block_pattern & 0x2)
|
2003-04-06 16:36:02 +00:00
|
|
|
slice_non_intra_DCT (decoder,
|
|
|
|
decoder->dest[1] + (offset >> 1),
|
|
|
|
decoder->uv_stride);
|
2001-02-24 20:28:24 +00:00
|
|
|
if (coded_block_pattern & 0x1)
|
2003-04-06 16:36:02 +00:00
|
|
|
slice_non_intra_DCT (decoder,
|
|
|
|
decoder->dest[2] + (offset >> 1),
|
|
|
|
decoder->uv_stride);
|
2001-02-24 20:28:24 +00:00
|
|
|
}
|
|
|
|
|
2003-04-06 16:36:02 +00:00
|
|
|
decoder->dc_dct_pred[0] = decoder->dc_dct_pred[1] =
|
|
|
|
decoder->dc_dct_pred[2] = 128 << decoder->intra_dc_precision;
|
2001-02-24 20:28:24 +00:00
|
|
|
}
|
|
|
|
|
2003-04-06 16:36:02 +00:00
|
|
|
NEXT_MACROBLOCK;
|
2001-02-24 20:28:24 +00:00
|
|
|
|
|
|
|
NEEDBITS (bit_buf, bits, bit_ptr);
|
2003-04-06 16:36:02 +00:00
|
|
|
mba_inc = 0;
|
|
|
|
while (1) {
|
|
|
|
if (bit_buf >= 0x10000000) {
|
|
|
|
mba = MBA_5 + (UBITS (bit_buf, 5) - 2);
|
2001-02-24 20:28:24 +00:00
|
|
|
break;
|
2003-04-06 16:36:02 +00:00
|
|
|
} else if (bit_buf >= 0x03000000) {
|
|
|
|
mba = MBA_11 + (UBITS (bit_buf, 11) - 24);
|
|
|
|
break;
|
|
|
|
} else switch (UBITS (bit_buf, 11)) {
|
|
|
|
case 8: /* macroblock_escape */
|
|
|
|
mba_inc += 33;
|
|
|
|
/* pass through */
|
|
|
|
case 15: /* macroblock_stuffing (MPEG1 only) */
|
|
|
|
DUMPBITS (bit_buf, bits, 11);
|
|
|
|
NEEDBITS (bit_buf, bits, bit_ptr);
|
|
|
|
continue;
|
|
|
|
default: /* end of slice, or error */
|
|
|
|
if (mpeg2_cpu_state_restore)
|
|
|
|
mpeg2_cpu_state_restore (&cpu_state);
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
DUMPBITS (bit_buf, bits, mba->len);
|
|
|
|
mba_inc += mba->mba;
|
2001-02-24 20:28:24 +00:00
|
|
|
|
2003-04-06 16:36:02 +00:00
|
|
|
if (mba_inc) {
|
|
|
|
decoder->dc_dct_pred[0] = decoder->dc_dct_pred[1] =
|
|
|
|
decoder->dc_dct_pred[2] = 128 << decoder->intra_dc_precision;
|
2001-02-24 20:28:24 +00:00
|
|
|
|
2003-04-06 16:36:02 +00:00
|
|
|
if (decoder->coding_type == P_TYPE) {
|
|
|
|
decoder->f_motion.pmv[0][0] = decoder->f_motion.pmv[0][1] = 0;
|
|
|
|
decoder->f_motion.pmv[1][0] = decoder->f_motion.pmv[1][1] = 0;
|
2001-02-24 20:28:24 +00:00
|
|
|
|
|
|
|
do {
|
2003-04-06 16:36:02 +00:00
|
|
|
MOTION_CALL (motion_zero, MACROBLOCK_MOTION_FORWARD);
|
|
|
|
NEXT_MACROBLOCK;
|
2001-02-24 20:28:24 +00:00
|
|
|
} while (--mba_inc);
|
|
|
|
} else {
|
|
|
|
do {
|
2003-04-06 16:36:02 +00:00
|
|
|
MOTION_CALL (motion_reuse, macroblock_modes);
|
|
|
|
NEXT_MACROBLOCK;
|
2001-02-24 20:28:24 +00:00
|
|
|
} while (--mba_inc);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
#undef bit_buf
|
|
|
|
#undef bits
|
|
|
|
#undef bit_ptr
|
|
|
|
}
|