/*
Copyright (c) 2005, The Musepack Development Team
All rights reserved.
Redistribution and use in source and binary forms, with or without
modification, are permitted provided that the following conditions are
met:
* Redistributions of source code must retain the above copyright
notice, this list of conditions and the following disclaimer.
* Redistributions in binary form must reproduce the above
copyright notice, this list of conditions and the following
disclaimer in the documentation and/or other materials provided
with the distribution.
* Neither the name of the The Musepack Development Team nor the
names of its contributors may be used to endorse or promote
products derived from this software without specific prior
written permission.
THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
"AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
(INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
*/
/// \file mpc_decoder.c
/// Core decoding routines and logic.
#include <mpcdec/mpcdec.h>
#include <mpcdec/internal.h>
#include <mpcdec/requant.h>
#include <mpcdec/huffman.h>
//SV7 tables
extern const HuffmanTyp* mpc_table_HuffQ [2] [8];
extern const HuffmanTyp mpc_table_HuffHdr [10];
extern const HuffmanTyp mpc_table_HuffSCFI [ 4];
extern const HuffmanTyp mpc_table_HuffDSCF [16];
#ifdef MPC_SUPPORT_SV456
//SV4/5/6 tables
extern const HuffmanTyp* mpc_table_SampleHuff [18];
extern const HuffmanTyp mpc_table_SCFI_Bundle [ 8];
extern const HuffmanTyp mpc_table_DSCF_Entropie [13];
extern const HuffmanTyp mpc_table_Region_A [16];
extern const HuffmanTyp mpc_table_Region_B [ 8];
extern const HuffmanTyp mpc_table_Region_C [ 4];
#endif
#ifndef MPC_LITTLE_ENDIAN
#define SWAP(X) mpc_swap32(X)
#else
#define SWAP(X) (X)
#endif
//------------------------------------------------------------------------------
// types
//------------------------------------------------------------------------------
enum
{
EQ_TAP = 13, // length of FIR filter for EQ
DELAY = ((EQ_TAP + 1) / 2), // delay of FIR
FIR_BANDS = 4, // number of subbands to be FIR filtered
MEMSIZE = MPC_DECODER_MEMSIZE, // overall buffer size
MEMSIZE2 = (MEMSIZE/2), // size of one buffer
MEMMASK = (MEMSIZE-1)
};
//------------------------------------------------------------------------------
// forward declarations
//------------------------------------------------------------------------------
void mpc_decoder_read_bitstream_sv6(mpc_decoder *d, mpc_bool_t seeking);
void mpc_decoder_read_bitstream_sv7(mpc_decoder *d, mpc_bool_t seeking);
mpc_bool_t mpc_decoder_seek_sample(mpc_decoder *d, mpc_int64_t destsample);
void mpc_decoder_requantisierung(mpc_decoder *d, const mpc_int32_t Last_Band);
//------------------------------------------------------------------------------
// utility functions
//------------------------------------------------------------------------------
static mpc_int32_t f_read(mpc_decoder *d, void *ptr, mpc_int32_t size)
{
return d->r->read(d->r->data, ptr, size);
}
static mpc_bool_t f_seek(mpc_decoder *d, mpc_int32_t offset)
{
return d->r->seek(d->r->data, offset);
}
static mpc_int32_t f_read_dword(mpc_decoder *d, mpc_uint32_t * ptr, mpc_uint32_t count)
{
return f_read(d, ptr, count << 2) >> 2;
}
static void mpc_decoder_seek(mpc_decoder *d, mpc_uint32_t bitpos)
{
f_seek(d, (bitpos>>5) * 4 + d->MPCHeaderPos);
f_read_dword(d, d->Speicher, MEMSIZE);
d->dword = SWAP(d->Speicher[d->Zaehler = 0]);
d->pos = bitpos & 31;
d->WordsRead = bitpos >> 5;
}
// jump desired number of bits out of the bitstream
static void mpc_decoder_bitstream_jump(mpc_decoder *d, const mpc_uint32_t bits)
{
d->pos += bits;
if (d->pos >= 32) {
d->Zaehler = (d->Zaehler + (d->pos >> 5)) & MEMMASK;
d->dword = SWAP(d->Speicher[d->Zaehler]);
d->WordsRead += d->pos >> 5;
d->pos &= 31;
}
}
void mpc_decoder_update_buffer(mpc_decoder *d, mpc_uint32_t RING)
{
if ((RING ^ d->Zaehler) & MEMSIZE2 ) {
// update buffer
f_read_dword(d, d->Speicher + (RING & MEMSIZE2), MEMSIZE2);
}
}
//------------------------------------------------------------------------------
// huffman & bitstream functions
//------------------------------------------------------------------------------
/* F U N C T I O N S */
// resets bitstream decoding
static void
mpc_decoder_reset_bitstream_decode(mpc_decoder *d)
{
d->dword = 0;
d->pos = 0;
d->Zaehler = 0;
d->WordsRead = 0;
}
// reports the number of read bits
static mpc_uint32_t
mpc_decoder_bits_read(mpc_decoder *d)
{
return 32 * d->WordsRead + d->pos;
}
// read desired number of bits out of the bitstream (max 31)
static mpc_uint32_t
mpc_decoder_bitstream_read(mpc_decoder *d, const mpc_uint32_t bits)
{
mpc_uint32_t out = d->dword;
d->pos += bits;
if (d->pos < 32) {
out >>= (32 - d->pos);
} else {
d->dword = SWAP(d->Speicher[d->Zaehler = (d->Zaehler + 1) & MEMMASK]);
d->pos -= 32;
if (d->pos) {
out <<= d->pos;
out |= d->dword >> (32 - d->pos);
}
d->WordsRead++;
}
return out & ((1 << bits) - 1);
}
// basic huffman decoding routine
// works with maximum lengths up to max_length
static mpc_int32_t
mpc_decoder_huffman_decode(mpc_decoder *d, const HuffmanTyp *Table,
const mpc_uint32_t max_length)
{
// load preview and decode
mpc_uint32_t code = d->dword << d->pos;
if (32 - d->pos < max_length)
code |= SWAP(d->Speicher[(d->Zaehler + 1) & MEMMASK]) >> (32 - d->pos);
while (code < Table->Code) Table++;
// set the new position within bitstream without performing a dummy-read
if ((d->pos += Table->Length) >= 32) {
d->pos -= 32;
d->dword = SWAP(d->Speicher[d->Zaehler = (d->Zaehler + 1) & MEMMASK]);
d->WordsRead++;
}
return Table->Value;
}
// decode SCFI-bundle (sv4,5,6)
static void
mpc_decoder_scfi_bundle_read(mpc_decoder *d, const HuffmanTyp* Table,
mpc_int32_t* SCFI, mpc_bool_t* DSCF)
{
mpc_uint32_t value = mpc_decoder_huffman_decode(d, Table, 6);
*SCFI = value >> 1;
*DSCF = value & 1;
}
static void
mpc_decoder_reset_v(mpc_decoder *d)
{
memset(d->V_L, 0, sizeof d->V_L);
memset(d->V_R, 0, sizeof d->V_R);
}
static void
mpc_decoder_reset_synthesis(mpc_decoder *d)
{
mpc_decoder_reset_v(d);
}
static void
mpc_decoder_reset_y(mpc_decoder *d)
{
memset(d->Y_L, 0, sizeof d->Y_L);
memset(d->Y_R, 0, sizeof d->Y_R);
}
static void
mpc_decoder_reset_globals(mpc_decoder *d)
{
mpc_decoder_reset_bitstream_decode(d);
d->DecodedFrames = 0;
d->StreamVersion = 0;
d->MS_used = 0;
memset(d->Y_L , 0, sizeof d->Y_L );
memset(d->Y_R , 0, sizeof d->Y_R );
memset(d->SCF_Index_L , 0, sizeof d->SCF_Index_L );
memset(d->SCF_Index_R , 0, sizeof d->SCF_Index_R );
memset(d->Res_L , 0, sizeof d->Res_L );
memset(d->Res_R , 0, sizeof d->Res_R );
memset(d->SCFI_L , 0, sizeof d->SCFI_L
评论30
最新资源