/* MPEG AUDIO LAYER 3 DECODER */
/* Bjorn Wesen 1997 */
/*#define USE_DATA */
#undef ROCKFORD
#ifndef DSP
#define DECVERBOSE
#endif
#include "mp3dec.h"
#ifdef DSPSIMUL
#include <stdio.h>
#include <stdlib.h>
#include <math.h>
#else
#ifdef DSP
#include "c3x_start.h"
#include "c3x_control.h"
#endif
#endif
#ifdef INT_MATH
mpfloat
operator+(mpfloat a, mpfloat b) {
return mpfloat(a._val + b._val);
}
mpfloat
operator-(mpfloat a, mpfloat b) {
return mpfloat(a._val - b._val);
}
mpfloat
operator-(mpfloat a) {
return mpfloat(-a._val);
}
mpfloat
operator*(mpfloat a, mpfloat b) {
return mpfloat(a._val * b._val);
}
mpfloat operator/(mpfloat a, mpfloat b) {
return mpfloat(a._val / b._val);
}
#endif
static float Frame_sampfreqs[4] = { 44.1, 48, 32, 0 };
static int Frame_bitrates[15] = { 0, 32, 40, 48, 56, 64, 80, 96,
112, 128, 160, 192, 224, 256, 320 };
/* scalefac_compress indexes into this table to find the sizes of the
scalefactor bitfields */
static int Frame_slen[2][16] = { { 0, 0, 0, 0, 3, 1, 1, 1,
2, 2, 2, 3, 3, 3, 4, 4 },
{ 0, 1, 2, 3, 0, 1, 2, 3,
1, 2, 3, 1, 2, 3, 2, 3 } };
/* for each samplerate there is a special division of the 576 frequency
lines into bands */
struct Frame_band Frame_bands[3] = {
{ { 0,4,8,12,16,20,24,30,36,44,52,
62,74,90,110,134,162,196,238,288,342,418,576 },
{ 0,4,8,12,16,22,30,40,52,66,84,106,136,192 }},
{ { 0,4,8,12,16,20,24,30,36,42,50,
60,72,88,106,128,156,190,230,276,330,384,576 },
{ 0,4,8,12,16,22,28,38,50,64,80,100,126,192 }},
{ { 0,4,8,12,16,20,24,30,36,44,54,
66,82,102,126,156,194,240,296,364,448,550,576 },
{ 0,4,8,12,16,22,30,42,58,78,104,138,180,192 }}
};
static Bitstream Frame_bitbuf; /* used for keeping the main data */
#ifdef DECVERBOSE
void dump_floats(mpfloat *f, int num)
{
int i;
printf("\n");
for(i = 0; i < num; i++) {
if(i && !(i % 5))
printf("\n");
printf("%10f ", f[i]);
}
printf("\n");
}
void dump_ints(int *f, int num)
{
int i;
printf("\n");
for(i = 0; i < num; i++) {
if(i && !(i % 5))
printf("\n");
printf("%8d ", f[i]);
}
printf("\n");
}
#endif
int frameNum = 0;
#ifdef UNIX
int
main(int argc, char **argv)
{
FILE *fp;
int i = 0;
char nameb[256];
PCMSample samples[576*4];
int numframes = 1024;
Bitstream my_bs;
Frame my_frame;
my_bs.buffer32 = (unsigned long *)malloc(0x2000 * 4);
if(argc < 2)
exit(0);
if(argc == 3)
numframes = atoi(argv[2]);
Frame_init();
HuffmanTable_init();
Granule_init();
sprintf(nameb, "%s.mp3", argv[1]);
Bitstream_open(&my_bs, nameb);
sprintf(nameb, "%s.raw", argv[1]);
fp = fopen(nameb, "wb");
printf("Decoding...\n");
do {
fprintf(stderr, "\r[%6d]", i++);
fflush(stderr);
frameNum++;
Frame_load(&my_frame, &my_bs, samples);
fwrite(samples, 2, 576*4, fp);
/* Frame_dump(&my_frame); */
} while(!Bitstream32_fillbuffer(&my_bs) && i < numframes);
printf("\n");
Bitstream_close(&my_bs);
fclose(fp);
exit(0);
}
#endif
#ifdef DSP
#include "testmp.h"
//static unsigned char inbuffer[20000];
main()
{
int i, jayjay;
#ifdef DSP_LOFI
PCMSample samples[288*40];
#else
PCMSample samples[576*4];
#endif
int numframes = 40;
Bitstream my_bs;
Frame my_frame;
static unsigned char bit_reservoir[BITSTREAM_BUFSIZE + 4];
static unsigned char buffel[BITSTREAM_BUFSIZE + 4];
#ifdef DSPSIMUL
FILE *output;
#else
#ifndef ROCKFORD
init_da();
#endif
#endif
#ifdef ROCKFORD
for(jayjay = 0; jayjay < 10;) {
#endif
i = 0;
Frame_init();
Frame_bitbuf.buffer = bit_reservoir;
HuffmanTable_init();
Granule_init();
Bitstream_open(&my_bs, 0);
my_bs.buffer = buffel;
Bitstream_fillbuffer(&my_bs);
#ifdef DSPSIMUL
output = fopen("outfil.raw", "wb");
#endif
do {
frameNum++;
Frame_load(&my_frame, &my_bs, samples + 288*i);
#ifdef DSPSIMUL
fprintf(stderr, ".");
fflush(stderr);
fwrite(samples, 1, 576*4, output);
#else
/*play_buffer((void *)(samples + 288*i));*/
#endif
i++;
Bitstream_fillbuffer(&my_bs);
} while(i < numframes);
#ifdef ROCKFORD
}
#else
play_buffer((void *)samples);
#endif
#ifdef DSPSIMUL
fprintf(stderr, "\n");
fclose(output);
exit(0);
#endif
while(1);
}
#endif
static int Frame_bitbuf_framestart = 0;
void
Frame_init()
{
Bitstream_open(&Frame_bitbuf, 0);
}
void
Frame_load(Frame *f, Bitstream *bs, PCMSample *samps)
{
int ch, scfsi_band, gr, i, previous_end, alignbits,
trash, part2_start;
f->samples = samps;
/* seek the sync word */
if(!Bitstream_seek_sync(bs)) {
#ifndef DSP
printf("Couldn't find sync.\n");
#endif
return;
}
/* check that the ID is 1, describing ISO/IEC 11172-3 audio */
if(!Bitstream32_get1(bs)) {
#ifndef DSP
printf("Incorrect frame ID = 0!\n");
#endif
return;
}
/* check that its a layer 3 frame */
if(Bitstream32_get(bs, 2) != LAYER3) {
#ifndef DSP
printf("Not a layer-3 frame!\n");
#endif
return;
}
/* read the header */
f->CRC_enable = !Bitstream32_get1(bs); /* meaning of this bit is inverted */
f->bitrate_index = Bitstream32_get(bs, 4);
f->sampling_frequency = Bitstream32_get(bs, 2);
f->padding_bit = Bitstream32_get1(bs);
(void)Bitstream32_get1(bs); /* skip the private bit, we dont use it */
f->mode = Bitstream32_get(bs, 2);
f->channels = (f->mode == MODE_SINGLE_CHANNEL) ? 1 : 2;
f->mode_extension = Bitstream32_get(bs, 2);
f->copyright = Bitstream32_get1(bs);
f->originality = Bitstream32_get1(bs);
f->emphasis = Bitstream32_get(bs, 2);
/* if this frame has a CRC, read it */
if(f->CRC_enable)
f->CRC = Bitstream32_get(bs, 16);
/* now read the Layer-3 audio_data part of the frame */
/* the following data constitutes the side information stream -
- main_data_begin pointer
- side info for both granules (scfsi)
- side info granule 1
- side info granule 2
*/
/* read the starting offset of the main data */
f->main_data_begin = Bitstream32_get(bs, 9);
/* read private parts */
if(f->channels == 1)
(void)Bitstream32_get(bs, 5); /* 5 private bits for single_channel */
else
(void)Bitstream32_get(bs, 3); /* 3 private bits for other modes */
/* read scalefactor selection information, 4 bits per channel */
for(ch = 0; ch < f->channels; ch++)
for(scfsi_band = 0; scfsi_band < 4; scfsi_band++)
f->scfsi[ch][scfsi_band] = Bitstream32_get1(bs);
/* read the side info for the channels for each granule */
for(gr = 0; gr < 2; gr++)
for(ch = 0; ch < f->channels; ch++)
Granule_decode_info(&f->gr[gr][ch], bs);
/* calculate the size of this header in bytes */
f->header_size = 4 + 2 * f->CRC_enable + (f->channels == 1 ? 17 : 32);
/* calculate the size of the main_data block of this frame */
/* the distance between two consequitive syncwords is determined from
the formula: N = 144 * bitrate / sampling_frequency + padding */
/* then we find the size of the main data by taking that number and
subtracting the size of the header and padding */
f->main_data_size = (int)(((144 * Frame_bitrates[f->bitrate_index]) /
Frame_sampfreqs[f->sampling_frequency])) + f->padding_bit - f->header_size;
/* extract all main data of this frame and insert it into the bitbuffer */
/* ## TODO check if main data is bytealigned, if so, we can copy it much
faster */
for(i = f->main_data_size; i > 0; i--)
Bitstream_putbyte(&Frame_bitbuf, Bitstream32_get(bs, 8))