📄 mp3dec.c
字号:
faddp st(1), st
add eax, 32
and eax, 0x3ff
add ecx, 256
inc edx
cmp edx, 7
jle filt_lp1
// store accumulated sums as integers
// scaling is not necessary, because we put the 32768 factor in the D coefficients
fistp isum2
fistp isum1
// clipcheck and store sample 1
mov edx, isum1
#ifdef OVERFLOW_CHECKING
cmp edx, 32767
jle no_isum1_of
mov word ptr [edi], 0x7fff
jmp done_isum1
no_isum1_of:
cmp edx, -32768
jge no_isum1_uf
mov word ptr [edi], 0x8000
jmp done_isum1
no_isum1_uf:
#endif
mov [edi], dx
done_isum1:
// clipcheck and store sample 2
mov edx, isum2
#ifdef OVERFLOW_CHECKING
cmp edx, 32767
jle no_isum2_of
mov word ptr [edi+2], 0x7fff
jmp done_isum2
no_isum2_of:
cmp edx, -32768
jge no_isum2_uf
mov word ptr [edi+2], 0x8000
jmp done_isum2
no_isum2_uf:
#endif
mov [edi+2], dx
done_isum2:
add edi, 4
// increase loop counter and loop
inc ebx
cmp ebx, 31
jle window_loop
// store the new sample counter
mov S, edi
pop edi
}
#else
for (j = 0; j < 32; j++) {
a = &Granule_sbsynth_D[j];
k = j + Granule_sbsynth_Vptr[0];
b = &Granule_sbsynth_V[0][0];
sum = (mpfloat)0.0f;
sum2 = (mpfloat)0.0f;
for(i = 0; i < 8; i++) {
sum += a[0] * b[k];
sum2 += a[0] * b[k+1024];
k = (k + 96) & 0x3ff;
sum += a[32] * b[k];
sum2 += a[32] * b[k+1024];
k = (k + 32) & 0x3ff;
a = &a[64];
}
#ifdef INT_MATH
i = sum.rint();
#else
/* convert to integer, and clip the output */
i = (int)sum;
#endif
#ifdef OVERFLOW_CHECKING
if(i >= 32768) {
*S = 32767;
} else if(i < -32768) {
*S = -32768;
} else
#endif
*S = i;
S++;
#ifdef INT_MATH
i = sum2.rint();
#else
/* convert to integer, and clip the output */
i = (int)sum2;
#endif
#ifdef OVERFLOW_CHECKING
if(i >= 32768) {
*S = 32767;
} else if(i < -32768) {
*S = -32768;
} else
#endif
*S = i;
S++;
}
#endif
}
}
#ifdef DECVERBOSE
void
Frame_dump(Frame *f)
{
printf("CRC %d, ", f->CRC_enable);
printf("bitrate %d, ", f->bitrate_index);
printf("sampfreq %d, ", f->sampling_frequency);
printf("mode %d, ", f->mode);
printf("mode_ext %d\n", f->mode_extension);
printf("copyright %d, ", f->copyright);
printf("originality %d, ", f->originality);
printf("emphasis %d\n", f->emphasis);
if(f->CRC_enable)
printf("CRC 0x%4x\n", f->CRC);
printf("main_data_begin %d, header_size %d, datasize %d\n",
f->main_data_begin,
f->header_size,
f->main_data_size);
}
#endif
/* Bitstream code */
/* ###TODO Its NOT optimal to have a hardcoded bufsize */
int
Bitstream_open(Bitstream *bs, char *fname)
{
#ifndef DSP
if(fname) {
/* open the file */
if(!(bs->f = fopen(fname, "rb"))) {
printf("Couldn't open file %s for reading.\n", fname);
bs->eof_found = 1;
return -1;
}
} else
bs->f = 0;
#endif
/* reset indexes to first bit in the file */
bs->bit_index = 0;
#if 0
bs->byte_index = 0;
#endif
bs->offset = 0;
bs->write_index = 0;
bs->pos = 0;
bs->eof_found = 0;
/* fill it up */
#ifndef DSP
if(bs->f)
Bitstream_fillbuffer(bs);
#endif
return 0;
}
/* close the bitstream */
void
Bitstream_close(Bitstream *bs)
{
#ifndef DSP
if(bs->f) {
fclose(bs->f);
bs->f = 0;
}
#endif
}
/* fill the buffer with as many new bytes as possible */
/* returns 0 if successful, -1 if it couldn't load any new bytes */
int
Bitstream_fillbuffer(Bitstream *bs)
{
int to_load, loaded, i;
/* Figure out how many bytes we can read. we have a circular buffer
with a read (byte_index) and write (write_index) pointer.
We can load new bytes from the write pointer up to but not including
the read pointer. */
to_load = ((bs->pos >> 3) - bs->write_index) % BITSTREAM_BUFSIZE;
/* if the read and write pointers are equal, we assume we should read in
the whole buffer */
if(!to_load)
to_load = BITSTREAM_BUFSIZE;
#ifndef DSP
/* don't bother to fill if the buffer is more than half full */
if(to_load < BITSTREAM_BUFSIZE/2)
return 0;
#endif
#ifdef DSP
loaded = MIN(20000 - bs->offset, MIN(to_load,
BITSTREAM_BUFSIZE - bs->write_index));
for(i = 0; i < loaded; i++)
bs->buffer[bs->write_index++] = inbuffer[bs->offset++];
bs->write_index = bs->write_index % BITSTREAM_BUFSIZE;
to_load -= loaded;
if(to_load) {
loaded = MIN(20000 - bs->offset, to_load);
for(i = 0; i < loaded; i++)
bs->buffer[bs->write_index++] = inbuffer[bs->offset++];
bs->write_index = bs->write_index % BITSTREAM_BUFSIZE;
}
#else
loaded = fread(&bs->buffer[bs->write_index], 1,
MIN(to_load, BITSTREAM_BUFSIZE - bs->write_index),
bs->f);
if(!loaded) {
printf("End of file in Bitstream_fillbuffer.\n");
return -1;
}
to_load -= loaded;
bs->offset += loaded;
bs->write_index = (bs->write_index + loaded) % BITSTREAM_BUFSIZE;
if(to_load) {
/* read the second chunk if any */
loaded = fread(bs->buffer, 1, to_load, bs->f);
if(loaded != to_load) {
printf("End of file in Bitstream_fillbuffer.\n");
return 0;
}
bs->offset += loaded;
bs->write_index = (bs->write_index + loaded) % BITSTREAM_BUFSIZE;
}
#endif
/* duplicate the end to reduce the need for modulo calcs in viewbits */
bs->buffer[BITSTREAM_BUFSIZE] = bs->buffer[0];
bs->buffer[BITSTREAM_BUFSIZE + 1] = bs->buffer[1];
bs->buffer[BITSTREAM_BUFSIZE + 2] = bs->buffer[2];
bs->buffer[BITSTREAM_BUFSIZE + 3] = bs->buffer[3];
return 0;
}
ibool
Bitstream_eof(Bitstream *bs)
{
return bs->eof_found;
}
/*
static int Bitstream_getmask[9] = { 0, 0x1, 0x3, 0x7, 0xf,
0x1f, 0x3f, 0x7f, 0xff };
static int Bitstream_getmask2[9] = { 0, 0x1, 0x2, 0x4, 0x8,
0x10, 0x20, 0x40, 0x80 };
*/
unsigned int Bitstream_msk[8] = {
0xffffffff,0x7fffffff,0x3fffffff,0x1fffffff,
0x0fffffff,0x07ffffff,0x03ffffff,0x01ffffff,
};
/* get numbits from the bitstream bs */
/* it is assumed that the buffer has enough fresh bits to satisfy the
request
*/
unsigned int
Bitstream_viewbits(Bitstream *bs, int n)
{
int pos;
unsigned int ret_value=0;
if(!n)
return 0;
pos = (bs->pos >> 3) & (BITSTREAM_BUFSIZE - 1);
ret_value = bs->buffer[pos] << 24 |
bs->buffer[pos + 1] << 16 |
bs->buffer[pos + 2] << 8 |
bs->buffer[pos + 3];
ret_value &= Bitstream_msk[bs->pos & 7];
ret_value >>= 32 - n - (bs->pos & 7);
return ret_value;
}
void
Bitstream_flushbits(Bitstream *bs, int n)
{
bs->pos += n;
/* bs->pos &= 8 * BITSTREAM_BUFSIZE - 1; */
}
unsigned int
Bitstream_get(Bitstream *bs, int n)
{
unsigned int ret_value;
if(!n)
return 0;
ret_value = Bitstream_viewbits(bs, n);
Bitstream_flushbits(bs, n);
return ret_value;
}
unsigned int
Bitstream_get1(Bitstream *bs)
{
return Bitstream_get(bs, 1);
}
void
Bitstream_putbyte(Bitstream *bs, unsigned int val)
{
bs->buffer[bs->write_index] = val;
bs->write_index = (bs->write_index + 1) % BITSTREAM_BUFSIZE;
}
void
Bitstream_rewindbits(Bitstream *bs, int n)
{
bs->pos -= n;
/*
bs->bit_index += n;
bs->byte_index -= bs->bit_index / 8;
bs->bit_index = bs->bit_index % 8;
*/
}
void
Bitstream_rewindbytes(Bitstream *bs, int n)
{
bs->pos -= n*8;
/*
bs->byte_index -= n;
*/
}
int
Bitstream_tell(Bitstream *bs)
{
return bs->pos;
}
/* seek forward until we find a bytealigned bit sequence of 1111 1111 1111 */
/* this is the syncword in mpeg audio layer 3 */
ibool
Bitstream32_seek_sync(Bitstream *bs)
{
int aligning;
unsigned int val;
aligning = bs->pos % 8; /* sync words are byte aligned */
if (aligning)
Bitstream32_get(bs, 8 - aligning);
/* preload val with 8 bits, then keep loading 4 bits at a time until we
find the sync word */
val = Bitstream32_get(bs, 8);
while (((val & 0xfff) != 0xfff) && !Bitstream_eof(bs)) {
val <<= 4;
val |= Bitstream32_get(bs, 4);
}
/* printf("sync found, now at %d\n", bs->pos);
*/
if (Bitstream_eof(bs))
return(0);
else
return(1);
}
int
Bitstream32_open(Bitstream *bs, char *fname)
{
#ifndef DSP
if(fname) {
/* open the file */
if(!(bs->f = fopen(fname, "rb"))) {
printf("Couldn't open file %s for reading.\n", fname);
bs->eof_found = 1;
return -1;
}
} else
bs->f = 0;
#endif
/* reset indexes to first bit in the file */
bs->bit_index = 0;
#if 0
bs->byte_index = 0;
#endif
bs->offset = 0;
bs->write_index = 0;
bs->pos = 0;
bs->eof_found = 0;
/* fill it up */
#ifndef DSP
if(bs->f)
Bitstream32_fillbuffer(bs);
#endif
return 0;
}
/* fill the buffer with as many new bytes as possible */
/* returns 0 if successful, -1 if it couldn't load any new bytes */
int
Bitstream32_fillbuffer(Bitstream *bs)
{
int to_load, loaded, i;
/* Figure out how many bytes we can read. we have a circular buffer
with a read (byte_index) and write (write_index) pointer.
We can load new bytes from the write pointer up to but not including
the read pointer. */
to_load = ((bs->pos >> 5) - bs->write_index) & 0x1fff;
/* if the read and write pointers are equal, we assume we should read in
the whole buffer */
if(!to_load)
to_load = 0x2000;
#ifndef DSP
/* don't bother to fill if the buffer is more than half full */
if(to_load < 0x1000)
return 0;
#endif
#ifdef DSP
loaded = MIN(20000 - bs->offset, MIN(to_load,
BITSTREAM_BUFSIZE - bs->write_index));
for(i = 0; i < loaded; i++)
bs->buffer[bs->write_index++] = inbuffer[bs->offset++];
bs->write_index = bs->write_index % BITSTREAM_BUFSIZE;
to_load -= loaded;
if(to_load) {
loaded = MIN(20000 - bs->offset, to_load);
for(i = 0; i < loaded; i++)
bs->buffer[bs->write_index++] = inbuffer[bs->offset++];
bs->write_index = bs->write_index % BITSTREAM_BUFSIZE;
}
#else
for(i = 0; i < MIN(to_load, 0x2000 - bs->write_index); i++) {
loaded = fread(&bs->buffer32[bs->write_index], 4, 1, bs->f);
if(!loaded) {
printf("End of file in Bitstream_fillbuffer.\n");
return -1;
}
bs->buffer32[bs->write_index] = htonl(bs->buffer32[bs->write_index]);
bs->write_index = (bs->write_index + loaded) & 0x1fff;
to_load -= loaded;
bs->offset += loaded;
}
/* read the second chunk if any */
for(i = 0; i < to_load; i++) {
loaded = fread(&bs->buffer32[bs->write_index], 4, 1, bs->f);
if(!loaded) {
printf("End of file in Bitstream_fillbuffer.\n");
return 0;
}
bs->buffer32[bs->write_index] = htonl(bs->buffer32[bs->write_index]);
bs->offset += loaded;
bs->write_index = (bs->write_index + loaded) & 0x1fff;
}
#endif
return 0;
}
unsigned int Bitstream32_msk[32] = {
0xffffffff,0x7fffffff,0x3fffffff,0x1fffffff,
0x0fffffff,0x07ffffff,0x03ffffff,0x01ffffff,
0x00ffffff,0x007fffff,0x003fffff,0x001fffff,
0x000fffff,0x0007ffff,0x0003ffff,0x0001ffff,
0x0000ffff,0x00007fff,0x00003fff,0x00001fff,
0x00000fff,0x000007ff,0x000003ff,0x000001ff,
0x000000ff,0x0000007f,0x0000003f,0x0000001f,
0x0000000f,0x00000007,0x00000003,0x00000001
};
/* get numbits from the bitstream bs */
/* it is assumed that the buffer has enough fresh bits to satisfy the
request
*/
unsigned int
Bitstream32_viewbits(Bitstream *bs, int n)
{
int pos;
unsigned int ret_value=0;
if(!n)
return 0;
pos = (bs->pos >> 5) & 0x1fff; /* find index in buffer */
ret_value = bs->buffer[pos] << 24 |
bs->buffer[pos + 1] << 16 |
bs->buffer[pos + 2] << 8 |
bs->buffer[pos + 3];
ret_value &= Bitstream_msk[bs->pos & 7];
ret_value >>= 32 - n - (bs->pos & 7);
return ret_value;
}
unsigned int
Bitstream32_get(Bitstream *bs, int n)
{
unsigned int ret_value;
if(!n)
return 0;
ret_value = Bitstream_viewbits(bs, n);
Bitstream_flushbits(bs, n);
return ret_value;
}
unsigned int
Bitstream32_get1(Bitstream *bs)
{
return Bitstream_get(bs, 1);
}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -