📄 mpc_decoder.c
字号:
/*
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 "musepack/musepack.h"
#include "musepack/internal.h"
#include "musepack/requant.h"
#include "musepack/huffman.h"
//------------------------------------------------------------------------------
// 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_init_huffman_sv6(mpc_decoder *d);
void mpc_decoder_init_huffman_sv7(mpc_decoder *d);
void mpc_decoder_read_bitstream_sv6(mpc_decoder *d);
void mpc_decoder_read_bitstream_sv7(mpc_decoder *d);
void mpc_decoder_update_buffer(mpc_decoder *d, mpc_uint32_t RING);
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, size_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)
{
count = f_read(d, ptr, count << 2) >> 2;
#ifndef MPC_LITTLE_ENDIAN
mpc_uint32_t n;
for(n = 0; n< count; n++) {
ptr[n] = swap32(ptr[n]);
}
#endif
return count;
}
//------------------------------------------------------------------------------
// huffman & bitstream functions
//------------------------------------------------------------------------------
static const mpc_uint32_t mask [33] = {
0x00000000, 0x00000001, 0x00000003, 0x00000007,
0x0000000F, 0x0000001F, 0x0000003F, 0x0000007F,
0x000000FF, 0x000001FF, 0x000003FF, 0x000007FF,
0x00000FFF, 0x00001FFF, 0x00003FFF, 0x00007FFF,
0x0000FFFF, 0x0001FFFF, 0x0003FFFF, 0x0007FFFF,
0x000FFFFF, 0x001FFFFF, 0x003FFFFF, 0x007FFFFF,
0x00FFFFFF, 0x01FFFFFF, 0x03FFFFFF, 0x07FFFFFF,
0x0FFFFFFF, 0x1FFFFFFF, 0x3FFFFFFF, 0x7FFFFFFF,
0xFFFFFFFF
};
/* 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
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 = 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 & mask[bits];
}
// decode SCFI-bundle (sv4,5,6)
static void
mpc_decoder_scfi_bundle_read(
mpc_decoder *d,
HuffmanTyp* Table, mpc_int32_t* SCFI, mpc_int32_t* DSCF)
{
// load preview and decode
mpc_uint32_t code = d->dword << d->pos;
if (d->pos > 26) {
code |= 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 = d->Speicher[d->Zaehler = (d->Zaehler+1) & MEMMASK];
++(d->WordsRead);
}
*SCFI = Table->Value >> 1;
*DSCF = Table->Value & 1;
}
static int __cdecl
mpc_decoder_huffman_typ_cmpfn(const void* p1, const void* p2)
{
if (((HuffmanTyp*) p1)->Code < ((HuffmanTyp*) p2)->Code ) return +1;
if (((HuffmanTyp*) p1)->Code > ((HuffmanTyp*) p2)->Code ) return -1;
return 0;
}
// sort huffman-tables by codeword
// offset resulting value
void
mpc_decoder_resort_huff_tables(
const mpc_uint32_t elements, HuffmanTyp* Table, const mpc_int32_t offset )
{
mpc_uint32_t i;
for ( i = 0; i < elements; i++ ) {
Table[i].Code <<= 32 - Table[i].Length;
Table[i].Value = i - offset;
}
qsort(Table, elements, sizeof(*Table), mpc_decoder_huffman_typ_cmpfn);
}
// basic huffman decoding routine
// works with maximum lengths up to 14
static mpc_int32_t
mpc_decoder_huffman_decode(mpc_decoder *d, const HuffmanTyp *Table)
{
// load preview and decode
mpc_uint32_t code = d->dword << d->pos;
if (d->pos > 18) {
code |= 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 = d->Speicher[d->Zaehler = (d->Zaehler + 1) & MEMMASK];
++(d->WordsRead);
}
return Table->Value;
}
// faster huffman through previewing less bits
// works with maximum lengths up to 10
static mpc_int32_t
mpc_decoder_huffman_decode_fast(mpc_decoder *d, const HuffmanTyp* Table)
{
// load preview and decode
mpc_uint32_t code = d->dword << d->pos;
if (d->pos > 22) {
code |= 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 = d->Speicher[d->Zaehler = (d->Zaehler + 1) & MEMMASK];
++(d->WordsRead);
}
return Table->Value;
}
// even faster huffman through previewing even less bits
// works with maximum lengths up to 5
static mpc_int32_t
mpc_decoder_huffman_decode_faster(mpc_decoder *d, const HuffmanTyp* Table)
{
// load preview and decode
mpc_uint32_t code = d->dword << d->pos;
if (d->pos > 27) {
code |= 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 = d->Speicher[d->Zaehler = (d->Zaehler + 1) & MEMMASK];
++(d->WordsRead);
}
return Table->Value;
}
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 );
memset(d->SCFI_R , 0, sizeof d->SCFI_R );
memset(d->DSCF_Flag_L , 0, sizeof d->DSCF_Flag_L );
memset(d->DSCF_Flag_R , 0, sizeof d->DSCF_Flag_R );
memset(d->DSCF_Reference_L, 0, sizeof d->DSCF_Reference_L );
memset(d->DSCF_Reference_R, 0, sizeof d->DSCF_Reference_R );
memset(d->Q , 0, sizeof d->Q );
memset(d->MS_Flag , 0, sizeof d->MS_Flag );
}
static mpc_uint32_t
mpc_decoder_decode_internal(mpc_decoder *d, MPC_SAMPLE_FORMAT *buffer)
{
mpc_uint32_t output_frame_length = MPC_FRAME_LENGTH;
mpc_uint32_t FrameBitCnt = 0;
if (d->DecodedFrames >= d->OverallFrames) {
return (mpc_uint32_t)(-1); // end of file -> abort decoding
}
// read jump-info for validity check of frame
d->FwdJumpInfo = mpc_decoder_bitstream_read(d, 20);
if (d->SeekTable)
d->SeekTable[d->DecodedFrames] = 20 + d->FwdJumpInfo; // Picard 2005.04.29
d->ActDecodePos = (d->Zaehler << 5) + d->pos;
// decode data and check for validity of frame
FrameBitCnt = mpc_decoder_bits_read(d);
switch (d->StreamVersion) {
case 0x04:
case 0x05:
case 0x06:
mpc_decoder_read_bitstream_sv6(d);
break;
case 0x07:
case 0x17:
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -