├── AudioFileSource.h
├── AudioFileSourcePROGMEM.cpp
├── AudioFileSourcePROGMEM.h
├── AudioFileSourceSPIFFS.cpp
├── AudioFileSourceSPIFFS.h
├── AudioGenerator.h
├── AudioGeneratorMP3.cpp
├── AudioGeneratorMP3.h
├── AudioOutput.h
├── AudioOutputI2SDAC.cpp
├── AudioOutputI2SDAC.h
├── AudioOutputI2SNoDAC.cpp
├── AudioOutputI2SNoDAC.h
├── AudioOutputSerialWAV.cpp
├── AudioOutputSerialWAV.h
├── CHANGES
├── COPYING
├── COPYRIGHT
├── CREDITS
├── D.dat.h
├── LICENSE
├── README
├── TODO
├── VERSION
├── bit.c
├── bit.h
├── config.h
├── decoder.c
├── decoder.h
├── fixed.c
├── fixed.h
├── frame.c
├── frame.h
├── global.h
├── huffman.c
├── huffman.h
├── imdct_s.dat.h
├── layer3.c
├── layer3.h
├── libmad-8266.ino
├── mad.h
├── mad.h.sed
├── qc_table.dat.h
├── rq_table.dat.h
├── sf_table.dat.h
├── stream.c
├── stream.h
├── synth.c
├── synth.h
├── timer.c
├── timer.h
├── version.c
└── version.h
/AudioFileSource.h:
--------------------------------------------------------------------------------
1 | /*
2 | AudioFileSource
3 | Base class of an input "file" to be used by AudioGenerator
4 |
5 | Copyright (C) 2017 Earle F. Philhower, III
6 |
7 | This program is free software: you can redistribute it and/or modify
8 | it under the terms of the GNU General Public License as published by
9 | the Free Software Foundation, either version 3 of the License, or
10 | (at your option) any later version.
11 |
12 | This program is distributed in the hope that it will be useful,
13 | but WITHOUT ANY WARRANTY; without even the implied warranty of
14 | MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
15 | GNU General Public License for more details.
16 |
17 | You should have received a copy of the GNU General Public License
18 | along with this program. If not, see .
19 | */
20 |
21 | #ifndef _AUDIOFILESOURCE_H
22 | #define _AUDIOFILESOURCE_H
23 |
24 | #include
25 |
26 | class AudioFileSource
27 | {
28 | public:
29 | AudioFileSource() {};
30 | virtual ~AudioFileSource() {};
31 | virtual bool open(const char *filename) { (void)filename; return false; };
32 | virtual uint32_t read(void *data, uint32_t len) { (void)data; (void)len; return 0; };
33 | virtual bool seek(int32_t pos, int dir) { (void)pos; (void)dir; return false; };
34 | virtual bool close() { return false; };
35 | virtual bool isOpen() { return false; };
36 | virtual uint32_t getSize() { return 0; };
37 | virtual uint32_t getPos() { return 0; };
38 | };
39 |
40 | #endif
41 |
42 |
--------------------------------------------------------------------------------
/AudioFileSourcePROGMEM.cpp:
--------------------------------------------------------------------------------
1 | /*
2 | AudioFileSourcePROGMEM
3 | Store a "file" as a PROGMEM array and use it as audio source data
4 |
5 | Copyright (C) 2017 Earle F. Philhower, III
6 |
7 | This program is free software: you can redistribute it and/or modify
8 | it under the terms of the GNU General Public License as published by
9 | the Free Software Foundation, either version 3 of the License, or
10 | (at your option) any later version.
11 |
12 | This program is distributed in the hope that it will be useful,
13 | but WITHOUT ANY WARRANTY; without even the implied warranty of
14 | MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
15 | GNU General Public License for more details.
16 |
17 | You should have received a copy of the GNU General Public License
18 | along with this program. If not, see .
19 | */
20 |
21 | #include "AudioFileSourcePROGMEM.h"
22 |
23 | AudioFileSourcePROGMEM::AudioFileSourcePROGMEM()
24 | {
25 | opened = false;
26 | progmemData = NULL;
27 | progmemLen = 0;
28 | filePointer = 0;
29 | }
30 |
31 | AudioFileSourcePROGMEM::AudioFileSourcePROGMEM(const void *data, uint32_t len)
32 | {
33 | open(data, len);
34 | }
35 |
36 | AudioFileSourcePROGMEM::~AudioFileSourcePROGMEM()
37 | {
38 | }
39 |
40 | bool AudioFileSourcePROGMEM::open(const void *data, uint32_t len)
41 | {
42 | if (!data || !len) return false;
43 |
44 | opened = true;
45 | progmemData = data;
46 | progmemLen = len;
47 | filePointer = 0;
48 | return true;
49 | }
50 |
51 | uint32_t AudioFileSourcePROGMEM::getSize()
52 | {
53 | if (!opened) return 0;
54 | return progmemLen;
55 | }
56 |
57 | bool AudioFileSourcePROGMEM::isOpen()
58 | {
59 | return opened;
60 | }
61 |
62 | bool AudioFileSourcePROGMEM::close()
63 | {
64 | opened = false;
65 | progmemData = NULL;
66 | progmemLen = 0;
67 | filePointer = 0;
68 | return true;
69 | }
70 |
71 | bool AudioFileSourcePROGMEM::seek(int32_t pos, int dir)
72 | {
73 | if (!opened) return false;
74 | uint32_t newPtr;
75 | switch (dir) {
76 | case SEEK_SET: newPtr = pos; break;
77 | case SEEK_CUR: newPtr = filePointer + pos; break;
78 | case SEEK_END: newPtr = progmemLen - pos; break;
79 | default: return false;
80 | }
81 | if (newPtr > progmemLen) return false;
82 | filePointer = newPtr;
83 | return true;
84 | }
85 |
86 | uint32_t AudioFileSourcePROGMEM::read(void *data, uint32_t len)
87 | {
88 | if (!opened) return 0;
89 | if (filePointer >= progmemLen) return 0;
90 |
91 | uint32_t toRead = progmemLen - filePointer;
92 | if (toRead > len) toRead = len;
93 |
94 | memcpy_P(data, reinterpret_cast(progmemData)+filePointer, toRead);
95 | filePointer += toRead;
96 | return toRead;
97 | }
98 |
99 |
100 |
--------------------------------------------------------------------------------
/AudioFileSourcePROGMEM.h:
--------------------------------------------------------------------------------
1 | /*
2 | AudioFileSourcePROGMEM
3 | Store a "file" as a PROGMEM array and use it as audio source data
4 |
5 | Copyright (C) 2017 Earle F. Philhower, III
6 |
7 | This program is free software: you can redistribute it and/or modify
8 | it under the terms of the GNU General Public License as published by
9 | the Free Software Foundation, either version 3 of the License, or
10 | (at your option) any later version.
11 |
12 | This program is distributed in the hope that it will be useful,
13 | but WITHOUT ANY WARRANTY; without even the implied warranty of
14 | MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
15 | GNU General Public License for more details.
16 |
17 | You should have received a copy of the GNU General Public License
18 | along with this program. If not, see .
19 | */
20 |
21 | #ifndef _AUDIOFILESOURCEPROGMEM_H
22 | #define _AUDIOFILESOURCEPROGMEM_H
23 |
24 | #include "AudioFileSource.h"
25 |
26 | class AudioFileSourcePROGMEM : public AudioFileSource
27 | {
28 | public:
29 | AudioFileSourcePROGMEM();
30 | AudioFileSourcePROGMEM(const void *data, uint32_t len);
31 | virtual ~AudioFileSourcePROGMEM() override;
32 | virtual uint32_t read(void *data, uint32_t len) override;
33 | virtual bool seek(int32_t pos, int dir) override;
34 | virtual bool close() override;
35 | virtual bool isOpen() override;
36 | virtual uint32_t getSize() override;
37 | virtual uint32_t getPos() override { if (!opened) return 0; else return filePointer; };
38 |
39 | bool open(const void *data, uint32_t len);
40 |
41 | private:
42 | bool opened;
43 | const void *progmemData;
44 | uint32_t progmemLen;
45 | uint32_t filePointer;
46 | };
47 |
48 | #endif
49 |
50 |
--------------------------------------------------------------------------------
/AudioFileSourceSPIFFS.cpp:
--------------------------------------------------------------------------------
1 | /*
2 | AudioFileSourceSPIFFS
3 | Input SPIFFS "file" to be used by AudioGenerator
4 |
5 | Copyright (C) 2017 Earle F. Philhower, III
6 |
7 | This program is free software: you can redistribute it and/or modify
8 | it under the terms of the GNU General Public License as published by
9 | the Free Software Foundation, either version 3 of the License, or
10 | (at your option) any later version.
11 |
12 | This program is distributed in the hope that it will be useful,
13 | but WITHOUT ANY WARRANTY; without even the implied warranty of
14 | MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
15 | GNU General Public License for more details.
16 |
17 | You should have received a copy of the GNU General Public License
18 | along with this program. If not, see .
19 | */
20 |
21 | #include "AudioFileSourceSPIFFS.h"
22 |
23 | AudioFileSourceSPIFFS::AudioFileSourceSPIFFS()
24 | {
25 | }
26 |
27 | AudioFileSourceSPIFFS::AudioFileSourceSPIFFS(const char *filename)
28 | {
29 | open(filename);
30 | }
31 |
32 | bool AudioFileSourceSPIFFS::open(const char *filename)
33 | {
34 | SPIFFS.begin();
35 | f = SPIFFS.open(filename, "r");
36 | return f;
37 | }
38 |
39 | AudioFileSourceSPIFFS::~AudioFileSourceSPIFFS()
40 | {
41 | if (f) f.close();
42 | }
43 |
44 | uint32_t AudioFileSourceSPIFFS::read(void *data, uint32_t len)
45 | {
46 | return f.read(reinterpret_cast(data), len);
47 | }
48 |
49 | bool AudioFileSourceSPIFFS::seek(int32_t pos, int dir)
50 | {
51 | return f.seek(pos, (dir==SEEK_SET)?SeekSet:(dir==SEEK_CUR)?SeekCur:SeekEnd);
52 | }
53 |
54 | bool AudioFileSourceSPIFFS::close()
55 | {
56 | f.close();
57 | return true;
58 | }
59 |
60 | bool AudioFileSourceSPIFFS::isOpen()
61 | {
62 | return f?true:false;
63 | }
64 |
65 | uint32_t AudioFileSourceSPIFFS::getSize()
66 | {
67 | if (!f) return 0;
68 | return f.size();
69 | }
70 |
71 |
72 |
--------------------------------------------------------------------------------
/AudioFileSourceSPIFFS.h:
--------------------------------------------------------------------------------
1 | /*
2 | AudioFileSourceSPIFFS
3 | Input SPIFFS "file" to be used by AudioGenerator
4 |
5 | Copyright (C) 2017 Earle F. Philhower, III
6 |
7 | This program is free software: you can redistribute it and/or modify
8 | it under the terms of the GNU General Public License as published by
9 | the Free Software Foundation, either version 3 of the License, or
10 | (at your option) any later version.
11 |
12 | This program is distributed in the hope that it will be useful,
13 | but WITHOUT ANY WARRANTY; without even the implied warranty of
14 | MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
15 | GNU General Public License for more details.
16 |
17 | You should have received a copy of the GNU General Public License
18 | along with this program. If not, see .
19 | */
20 |
21 | #ifndef _AUDIOFILESOURCESPIFFS_H
22 | #define _AUDIOFILESOURCESPIFFS_H
23 |
24 | #include
25 | #include
26 |
27 | #include "AudioFileSource.h"
28 |
29 | class AudioFileSourceSPIFFS : public AudioFileSource
30 | {
31 | public:
32 | AudioFileSourceSPIFFS();
33 | AudioFileSourceSPIFFS(const char *filename);
34 | virtual ~AudioFileSourceSPIFFS() override;
35 |
36 | virtual bool open(const char *filename) override;
37 | virtual uint32_t read(void *data, uint32_t len) override;
38 | virtual bool seek(int32_t pos, int dir) override;
39 | virtual bool close() override;
40 | virtual bool isOpen() override;
41 | virtual uint32_t getSize() override;
42 | virtual uint32_t getPos() override { if (!f) return 0; else return f.position(); };
43 |
44 | private:
45 | File f;
46 | };
47 |
48 |
49 | #endif
50 |
51 |
--------------------------------------------------------------------------------
/AudioGenerator.h:
--------------------------------------------------------------------------------
1 | /*
2 | AudioGenerator
3 | Base class of an audio output generator
4 |
5 | Copyright (C) 2017 Earle F. Philhower, III
6 |
7 | This program is free software: you can redistribute it and/or modify
8 | it under the terms of the GNU General Public License as published by
9 | the Free Software Foundation, either version 3 of the License, or
10 | (at your option) any later version.
11 |
12 | This program is distributed in the hope that it will be useful,
13 | but WITHOUT ANY WARRANTY; without even the implied warranty of
14 | MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
15 | GNU General Public License for more details.
16 |
17 | You should have received a copy of the GNU General Public License
18 | along with this program. If not, see .
19 | */
20 |
21 | #ifndef _AUDIOGENERATOR_H
22 | #define _AUDIOGENERATOR_H
23 |
24 | #include
25 | #include "AudioFileSource.h"
26 | #include "AudioOutput.h"
27 |
28 | class AudioGenerator
29 | {
30 | public:
31 | AudioGenerator() {};
32 | virtual ~AudioGenerator() {};
33 | virtual bool begin(AudioFileSource *source, AudioOutput *output) { (void)source; (void)output; return false; };
34 | virtual bool loop() { return false; };
35 | virtual bool stop() { return false; };
36 | virtual bool isRunning() { return false;};
37 |
38 | private:
39 | bool running;
40 | AudioFileSource *file;
41 | AudioOutput *output;
42 | };
43 |
44 |
45 | #endif
46 |
47 |
--------------------------------------------------------------------------------
/AudioGeneratorMP3.cpp:
--------------------------------------------------------------------------------
1 | /*
2 | AudioGeneratorMP3
3 | Wrap libmad MP3 library to play audio
4 |
5 | Copyright (C) 2017 Earle F. Philhower, III
6 |
7 | This program is free software: you can redistribute it and/or modify
8 | it under the terms of the GNU General Public License as published by
9 | the Free Software Foundation, either version 3 of the License, or
10 | (at your option) any later version.
11 |
12 | This program is distributed in the hope that it will be useful,
13 | but WITHOUT ANY WARRANTY; without even the implied warranty of
14 | MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
15 | GNU General Public License for more details.
16 |
17 | You should have received a copy of the GNU General Public License
18 | along with this program. If not, see .
19 | */
20 |
21 |
22 | #include "AudioGeneratorMP3.h"
23 |
24 | AudioGeneratorMP3::AudioGeneratorMP3()
25 | {
26 | running = false;
27 | file = NULL;
28 | output = NULL;
29 | buffLen = 2048;
30 | buff = NULL;
31 | }
32 |
33 | AudioGeneratorMP3::~AudioGeneratorMP3()
34 | {
35 | free(buff);
36 | buff = NULL;
37 | }
38 |
39 | bool AudioGeneratorMP3::SetBufferSize(int sz)
40 | {
41 | if (running) return false;
42 | buffLen = sz;
43 | return true;
44 | }
45 |
46 |
47 | bool AudioGeneratorMP3::stop()
48 | {
49 | if (!running) return true;
50 | running = false;
51 | free(buff);
52 | buff = NULL;
53 |
54 | mad_synth_finish(&synth);
55 | mad_frame_finish(&frame);
56 | mad_stream_finish(&stream);
57 |
58 | return file->close();
59 | }
60 |
61 | bool AudioGeneratorMP3::isRunning()
62 | {
63 | return running;
64 | }
65 |
66 | enum mad_flow AudioGeneratorMP3::ErrorToFlow()
67 | {
68 | char err[64];
69 | strcpy_P(err, mad_stream_errorstr(&stream));
70 | Serial.printf("Decoding error 0x%04x (%s) at byte offset %d\n", stream.error, err, (stream.this_frame - buff) + lastReadPos);
71 | Serial.flush();
72 | return MAD_FLOW_CONTINUE;
73 | }
74 |
75 | enum mad_flow AudioGeneratorMP3::Input()
76 | {
77 | int unused = 0;
78 | if (stream.next_frame) {
79 | unused = buffLen - (stream.next_frame - buff);
80 | memmove(buff, stream.next_frame, unused);
81 | }
82 | if (unused == buffLen)
83 | return MAD_FLOW_STOP;
84 |
85 | lastReadPos = file->getPos() - unused;
86 | int len = buffLen - unused;
87 | len = file->read(buff + unused, len);
88 | if (len == 0) return MAD_FLOW_STOP;
89 |
90 | mad_stream_buffer(&stream, buff, len + unused);
91 |
92 | return MAD_FLOW_CONTINUE;
93 | }
94 |
95 |
96 | bool AudioGeneratorMP3::DecodeNextFrame()
97 | {
98 | do {
99 | if (!inInnerDecode) {
100 | if (Input() == MAD_FLOW_STOP) return false;
101 | }
102 | while (1) {
103 | inInnerDecode = true;
104 | if (mad_frame_decode(&frame, &stream) == -1) {
105 | if (!MAD_RECOVERABLE(stream.error)) break;
106 | ErrorToFlow(); // Always returns CONTINUE
107 | continue;
108 | }
109 | return true;
110 | }
111 | inInnerDecode = false;
112 | } while (stream.error == MAD_ERROR_BUFLEN);
113 |
114 | return false;
115 | }
116 |
117 | bool AudioGeneratorMP3::GetOneSample(int16_t sample[2])
118 | {
119 | if ( (samplePtr >= synth.pcm.length) && (nsCount >= 1152/32) ) {
120 | if (!DecodeNextFrame()) return false;
121 | samplePtr = 9999;
122 | nsCount = 0;
123 | }
124 |
125 | if (synth.pcm.samplerate != lastRate) {
126 | output->SetRate(synth.pcm.samplerate);
127 | lastRate = synth.pcm.samplerate;
128 | }
129 | if (synth.pcm.channels != lastChannels) {
130 | output->SetChannels(synth.pcm.channels);
131 | lastChannels = synth.pcm.channels;
132 | }
133 |
134 | // If we're here, we have one decoded frame and sent 0 or more samples out
135 | if (samplePtr < synth.pcm.length) {
136 | sample[AudioOutput::LEFTCHANNEL ] = synth.pcm.samples[0][samplePtr];
137 | sample[AudioOutput::RIGHTCHANNEL] = synth.pcm.samples[1][samplePtr];
138 | samplePtr++;
139 | } else {
140 | samplePtr = 0;
141 |
142 | switch ( mad_synth_frame_onens(&synth, &frame, nsCount++) ) {
143 | case MAD_FLOW_STOP:
144 | case MAD_FLOW_BREAK:
145 | return false; // Either way we're done
146 | default:
147 | break; // Do nothing
148 | }
149 | // for IGNORE and CONTINUE, just play what we have now
150 | sample[AudioOutput::LEFTCHANNEL ] = synth.pcm.samples[0][samplePtr];
151 | sample[AudioOutput::RIGHTCHANNEL] = synth.pcm.samples[1][samplePtr];
152 | samplePtr++;
153 | }
154 | return true;
155 | }
156 |
157 |
158 | bool AudioGeneratorMP3::loop()
159 | {
160 | static int16_t lastSample[2]= {0,0};
161 |
162 | if (!running) return true; // Nothing to do here!
163 |
164 | // First, try and push in the stored sample. If we can't, then punt and try later
165 | if (!output->ConsumeSample(lastSample)) return true; // Can't send, but no error detected
166 | // Try and stuff the buffer one sample at a time
167 | do
168 | {
169 | if (!GetOneSample(lastSample)) {
170 | running = false;
171 | return false;
172 | }
173 | } while (running && output->ConsumeSample(lastSample));
174 |
175 | return running;
176 | }
177 |
178 |
179 |
180 | bool AudioGeneratorMP3::begin(AudioFileSource *source, AudioOutput *output)
181 | {
182 | if (!source) return false;
183 | file = source;
184 | if (!output) return false;
185 | this->output = output;
186 | if (!file->isOpen()) return false; // Error
187 | if (!output->begin()) return false;
188 |
189 | // Where we are in generating one frame's data, set to invalid so we will run loop on first getsample()
190 | samplePtr = 9999;
191 | nsCount = 9999;
192 | synth.pcm.length = 0;
193 | lastRate = 0;
194 | lastChannels = 0;
195 | lastReadPos = 0;
196 | inInnerDecode = false;
197 | buff = reinterpret_cast(malloc(buffLen));
198 | if (!buff) return false;
199 |
200 | mad_stream_init(&stream);
201 | mad_frame_init(&frame);
202 | mad_synth_init(&synth);
203 |
204 | mad_stream_options(&stream, 0); // TODO - add options suppoirt
205 |
206 | running = true;
207 | return true;
208 | }
209 |
210 |
--------------------------------------------------------------------------------
/AudioGeneratorMP3.h:
--------------------------------------------------------------------------------
1 | /*
2 | AudioGeneratorMP3
3 | Wrap libmad MP3 library to play audio
4 |
5 | Copyright (C) 2017 Earle F. Philhower, III
6 |
7 | This program is free software: you can redistribute it and/or modify
8 | it under the terms of the GNU General Public License as published by
9 | the Free Software Foundation, either version 3 of the License, or
10 | (at your option) any later version.
11 |
12 | This program is distributed in the hope that it will be useful,
13 | but WITHOUT ANY WARRANTY; without even the implied warranty of
14 | MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
15 | GNU General Public License for more details.
16 |
17 | You should have received a copy of the GNU General Public License
18 | along with this program. If not, see .
19 | */
20 |
21 | #ifndef _AUDIOGENERATORMP3_H
22 | #define _AUDIOGENERATORMP3_H
23 |
24 | #include "AudioGenerator.h"
25 | #include "config.h"
26 | #include "mad.h"
27 |
28 | class AudioGeneratorMP3 : AudioGenerator
29 | {
30 | public:
31 | AudioGeneratorMP3();
32 | virtual ~AudioGeneratorMP3() override;
33 | virtual bool begin(AudioFileSource *source, AudioOutput *output) override;
34 | virtual bool loop() override;
35 | virtual bool stop() override;
36 | virtual bool isRunning() override;
37 | bool SetBufferSize(int sz);
38 |
39 | private:
40 | bool running;
41 | AudioFileSource *file;
42 | AudioOutput *output;
43 |
44 | int buffLen;
45 | unsigned char *buff;
46 | int lastReadPos;
47 | unsigned int lastRate;
48 | int lastChannels;
49 |
50 | // Decoding bits
51 | struct mad_stream stream;
52 | struct mad_frame frame;
53 | struct mad_synth synth;
54 | int samplePtr;
55 | int nsCount;
56 | bool inInnerDecode;
57 |
58 | // The internal helpers
59 | enum mad_flow ErrorToFlow();
60 | enum mad_flow Input();
61 | bool DecodeNextFrame();
62 | bool GetOneSample(int16_t sample[2]);
63 |
64 | };
65 |
66 | #endif
67 |
68 |
--------------------------------------------------------------------------------
/AudioOutput.h:
--------------------------------------------------------------------------------
1 | /*
2 | AudioOutput
3 | Base class of an audio output player
4 |
5 | Copyright (C) 2017 Earle F. Philhower, III
6 |
7 | This program is free software: you can redistribute it and/or modify
8 | it under the terms of the GNU General Public License as published by
9 | the Free Software Foundation, either version 3 of the License, or
10 | (at your option) any later version.
11 |
12 | This program is distributed in the hope that it will be useful,
13 | but WITHOUT ANY WARRANTY; without even the implied warranty of
14 | MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
15 | GNU General Public License for more details.
16 |
17 | You should have received a copy of the GNU General Public License
18 | along with this program. If not, see .
19 | */
20 |
21 | #ifndef _AUDIOOUTPUT_H
22 | #define _AUDIOOUTPUT_H
23 |
24 | #include
25 |
26 | class AudioOutput
27 | {
28 | public:
29 | AudioOutput() {};
30 | virtual ~AudioOutput() {};
31 | virtual bool SetRate(int hz) { hertz = hz; return true; };
32 | virtual bool SetBitsPerSample(int bits) { bps = bits; return true; };
33 | virtual bool SetChannels(int chan) { channels = chan; return true; };
34 | virtual bool begin() { return false; };
35 | typedef enum { LEFTCHANNEL=0, RIGHTCHANNEL=1 } SampleIndex;
36 | virtual bool ConsumeSample(int16_t sample[2]) { (void)sample; return false; };
37 | virtual bool stop() { return false; };
38 |
39 | void MakeSampleStereo16(int16_t sample[2]) {
40 | // Mono to "stereo" conversion
41 | if (channels == 1)
42 | sample[RIGHTCHANNEL] = sample[LEFTCHANNEL];
43 | if (bps == 8) {
44 | // Upsample from unsigned 8 bits to signed 16 bits
45 | sample[LEFTCHANNEL] = (((int16_t)(sample[LEFTCHANNEL]&0xff)) - 128) << 8;
46 | sample[RIGHTCHANNEL] = (((int16_t)(sample[RIGHTCHANNEL]&0xff)) - 128) << 8;
47 | }
48 | };
49 |
50 | protected:
51 | int hertz;
52 | int bps;
53 | int channels;
54 | };
55 |
56 | #endif
57 |
58 |
--------------------------------------------------------------------------------
/AudioOutputI2SDAC.cpp:
--------------------------------------------------------------------------------
1 | /*
2 | AudioOutputI2SDAC
3 | Audio player for an I2S connected DAC, 16bps
4 |
5 | Copyright (C) 2017 Earle F. Philhower, III
6 |
7 | This program is free software: you can redistribute it and/or modify
8 | it under the terms of the GNU General Public License as published by
9 | the Free Software Foundation, either version 3 of the License, or
10 | (at your option) any later version.
11 |
12 | This program is distributed in the hope that it will be useful,
13 | but WITHOUT ANY WARRANTY; without even the implied warranty of
14 | MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
15 | GNU General Public License for more details.
16 |
17 | You should have received a copy of the GNU General Public License
18 | along with this program. If not, see .
19 | */
20 |
21 | #include
22 | #include
23 | #include "AudioOutputI2SDAC.h"
24 |
25 | bool AudioOutputI2SDAC::i2sOn = false;
26 |
27 | AudioOutputI2SDAC::AudioOutputI2SDAC()
28 | {
29 | if (!i2sOn) i2s_begin();
30 | i2sOn = true;
31 | }
32 |
33 | AudioOutputI2SDAC::~AudioOutputI2SDAC()
34 | {
35 | if (i2sOn) i2s_end();
36 | i2sOn = false;
37 | }
38 |
39 | bool AudioOutputI2SDAC::SetRate(int hz)
40 | {
41 | // TODO - have a list of allowable rates from constructor, check them
42 | this->hertz = hz;
43 | i2s_set_rate(hz);
44 | return true;
45 | }
46 |
47 | bool AudioOutputI2SDAC::SetBitsPerSample(int bits)
48 | {
49 | if ( (bits != 16) && (bits != 8) ) return false;
50 | this->bps = bits;
51 | return true;
52 | }
53 |
54 | bool AudioOutputI2SDAC::SetChannels(int channels)
55 | {
56 | if ( (channels < 1) || (channels > 2) ) return false;
57 | this->channels = channels;
58 | return true;
59 | }
60 |
61 | bool AudioOutputI2SDAC::SetOutputModeMono(bool mono)
62 | {
63 | this->mono = mono;
64 | return true;
65 | }
66 |
67 | bool AudioOutputI2SDAC::begin()
68 | {
69 | // Nothing to do here, i2s will start once data comes in
70 | return true;
71 | }
72 |
73 | bool AudioOutputI2SDAC::ConsumeSample(int16_t sample[2])
74 | {
75 | MakeSampleStereo16( sample );
76 |
77 | if (this->mono) {
78 | // Average the two samples and overwrite
79 | uint32_t ttl = sample[LEFTCHANNEL] + sample[RIGHTCHANNEL];
80 | sample[LEFTCHANNEL] = sample[RIGHTCHANNEL] = (ttl>>1) & 0xffff;
81 | }
82 | uint32_t s32 = ((sample[RIGHTCHANNEL])<<16) | (sample[LEFTCHANNEL] & 0xffff);
83 | return i2s_write_sample_nb(s32); // If we can't store it, return false. OTW true
84 | }
85 |
86 | bool AudioOutputI2SDAC::stop()
87 | {
88 | // Nothing to do here. Maybe mute control if DAC doesn't do it for you
89 | return true;
90 | }
91 |
92 |
93 |
--------------------------------------------------------------------------------
/AudioOutputI2SDAC.h:
--------------------------------------------------------------------------------
1 | /*
2 | AudioOutputI2SDAC
3 | Audio player for an I2S connected DAC, 16bps
4 |
5 | Copyright (C) 2017 Earle F. Philhower, III
6 |
7 | This program is free software: you can redistribute it and/or modify
8 | it under the terms of the GNU General Public License as published by
9 | the Free Software Foundation, either version 3 of the License, or
10 | (at your option) any later version.
11 |
12 | This program is distributed in the hope that it will be useful,
13 | but WITHOUT ANY WARRANTY; without even the implied warranty of
14 | MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
15 | GNU General Public License for more details.
16 |
17 | You should have received a copy of the GNU General Public License
18 | along with this program. If not, see .
19 | */
20 |
21 | #ifndef _AUDIOOUTPUTI2SDAC_H
22 | #define _AUDIOOUTPUTI2SDAC_H
23 |
24 | #include "AudioOutput.h"
25 |
26 | class AudioOutputI2SDAC : public AudioOutput
27 | {
28 | public:
29 | AudioOutputI2SDAC();
30 | virtual ~AudioOutputI2SDAC() override;
31 | virtual bool SetRate(int hz) override;
32 | virtual bool SetBitsPerSample(int bits) override;
33 | virtual bool SetChannels(int channels) override;
34 | virtual bool begin() override;
35 | virtual bool ConsumeSample(int16_t sample[2]) override;
36 | virtual bool stop() override;
37 |
38 | bool SetOutputModeMono(bool mono); // Force mono output no matter the input
39 |
40 | private:
41 | bool mono;
42 | static bool i2sOn; // One per machine, not per object...
43 | };
44 |
45 | #endif
46 |
47 |
--------------------------------------------------------------------------------
/AudioOutputI2SNoDAC.cpp:
--------------------------------------------------------------------------------
1 | /*
2 | AudioOutputI2SNoDAC
3 | Audio player using SW delta-sigma to generate "analog" on I2S data
4 |
5 | Copyright (C) 2017 Earle F. Philhower, III
6 |
7 | This program is free software: you can redistribute it and/or modify
8 | it under the terms of the GNU General Public License as published by
9 | the Free Software Foundation, either version 3 of the License, or
10 | (at your option) any later version.
11 |
12 | This program is distributed in the hope that it will be useful,
13 | but WITHOUT ANY WARRANTY; without even the implied warranty of
14 | MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
15 | GNU General Public License for more details.
16 |
17 | You should have received a copy of the GNU General Public License
18 | along with this program. If not, see .
19 | */
20 |
21 | #include
22 | #include
23 | #include "AudioOutputI2SNoDAC.h"
24 |
25 | bool AudioOutputI2SNoDAC::i2sOn = false;
26 |
27 | AudioOutputI2SNoDAC::AudioOutputI2SNoDAC()
28 | {
29 | if (!i2sOn) i2s_begin();
30 | i2sOn = true;
31 | hertz = 44100;
32 | oversample = 32;
33 | }
34 |
35 | AudioOutputI2SNoDAC::~AudioOutputI2SNoDAC()
36 | {
37 | if (i2sOn) i2s_end();
38 | i2sOn = false;
39 | }
40 |
41 | bool AudioOutputI2SNoDAC::SetRate(int hz)
42 | {
43 | // TODO - what is the max hz we can request on I2S?
44 | this->hertz = hz;
45 | i2s_set_rate(hz * (oversample / 32));
46 | return true;
47 | }
48 |
49 | bool AudioOutputI2SNoDAC::SetBitsPerSample(int bits)
50 | {
51 | if ( (bits != 16) && (bits != 8) ) return false;
52 | this->bps = bits;
53 | return true;
54 | }
55 |
56 | bool AudioOutputI2SNoDAC::SetChannels(int channels)
57 | {
58 | if ( (channels < 1) || (channels > 2) ) return false;
59 | this->channels = channels;
60 | return true;
61 | }
62 |
63 | bool AudioOutputI2SNoDAC::SetOversampling(int os) {
64 | if (os % 32) return false; // Only Nx32 oversampling supported
65 | if (os > 256) return false; // Don't be silly now!
66 | if (os < 32) return false; // Nothing under 32 allowed
67 |
68 | oversample = os;
69 | return SetRate(hertz);
70 | }
71 |
72 | bool AudioOutputI2SNoDAC::begin()
73 | {
74 | // Nothing to do here, i2s will start once data comes in
75 | return true;
76 | }
77 |
78 | typedef int32_t fixed24p8_t;
79 | #define fixedPosValue 0x007fff00 /* 24.8 of max-signed-int */
80 | void AudioOutputI2SNoDAC::DeltaSigma(int16_t sample[2], uint32_t dsBuff[4])
81 | {
82 | static fixed24p8_t lastSamp = 0; // Last sample value
83 | static fixed24p8_t cumErr = 0; // Running cumulative error since time began
84 |
85 | // Not shift 8 because addition takes care of one mult x 2
86 | fixed24p8_t newSamp = ( ((int32_t)sample[0]) + ((int32_t)sample[1]) ) << 7;
87 |
88 | int oversample32 = oversample / 32;
89 | // How much the comparison signal changes each oversample step
90 | fixed24p8_t diffPerStep = (newSamp - lastSamp) >> (4 + oversample32);
91 |
92 | // Don't need lastSamp anymore, store this one for next round
93 | lastSamp = newSamp;
94 |
95 | for (int j = 0; j < oversample32; j++) {
96 | uint32_t bits = 0; // The bits we convert the sample into, MSB to go on the wire first
97 |
98 | for (int i = 32; i > 0; i--) {
99 | bits = bits << 1;
100 | if (cumErr < 0) {
101 | bits |= 1;
102 | cumErr += fixedPosValue - newSamp;
103 | } else {
104 | // Bits[0] = 0 handled already by left shift
105 | cumErr -= fixedPosValue + newSamp;
106 | }
107 | newSamp += diffPerStep; // Move the reference signal towards destination
108 | }
109 | dsBuff[j] = bits;
110 | }
111 | }
112 |
113 | bool AudioOutputI2SNoDAC::ConsumeSample(int16_t sample[2])
114 | {
115 | MakeSampleStereo16( sample );
116 |
117 | // Make delta-sigma filled buffer
118 | uint32_t dsBuff[4];
119 | DeltaSigma(sample, dsBuff);
120 |
121 | // Either send complete pulse stream or nothing
122 | if (!i2s_write_sample_nb(dsBuff[0])) return false; // No room at the inn
123 | // At this point we've sent in first of possibly 4 32-bits, need to send
124 | // remaining ones even if they block.
125 | for (int i = 32; i < oversample; i+=32)
126 | i2s_write_sample( dsBuff[i / 32]);
127 | return true;
128 | }
129 |
130 | bool AudioOutputI2SNoDAC::stop()
131 | {
132 | // Nothing to do here. Maybe mute control if DAC doesn't do it for you
133 | return true;
134 | }
135 |
136 |
137 |
--------------------------------------------------------------------------------
/AudioOutputI2SNoDAC.h:
--------------------------------------------------------------------------------
1 | /*
2 | AudioOutputI2SNoDAC
3 | Audio player using SW delta-sigma to generate "analog" on I2S data
4 |
5 | Copyright (C) 2017 Earle F. Philhower, III
6 |
7 | This program is free software: you can redistribute it and/or modify
8 | it under the terms of the GNU General Public License as published by
9 | the Free Software Foundation, either version 3 of the License, or
10 | (at your option) any later version.
11 |
12 | This program is distributed in the hope that it will be useful,
13 | but WITHOUT ANY WARRANTY; without even the implied warranty of
14 | MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
15 | GNU General Public License for more details.
16 |
17 | You should have received a copy of the GNU General Public License
18 | along with this program. If not, see .
19 | */
20 |
21 | #ifndef _AUDIOOUTPUTI2SNODAC_H
22 | #define _AUDIOOUTPUTI2SNODAC_H
23 |
24 | #include "AudioOutput.h"
25 |
26 | class AudioOutputI2SNoDAC : public AudioOutput
27 | {
28 | public:
29 | AudioOutputI2SNoDAC();
30 | virtual ~AudioOutputI2SNoDAC() override;
31 | virtual bool SetRate(int hz) override;
32 | virtual bool SetBitsPerSample(int bits) override;
33 | virtual bool SetChannels(int channels) override;
34 | virtual bool begin() override;
35 | virtual bool ConsumeSample(int16_t sample[2]) override;
36 | virtual bool stop() override;
37 |
38 | bool SetOversampling(int os);
39 |
40 | private:
41 | static bool i2sOn; // One per machine, not per object...
42 | int oversample;
43 | void DeltaSigma(int16_t sample[2], uint32_t dsBuff[4]);
44 | };
45 |
46 | #endif
47 |
48 |
--------------------------------------------------------------------------------
/AudioOutputSerialWAV.cpp:
--------------------------------------------------------------------------------
1 | /*
2 | AudioOutputSerialWAV
3 | Writes a mostly correct WAV file to the serial port
4 |
5 | Copyright (C) 2017 Earle F. Philhower, III
6 |
7 | This program is free software: you can redistribute it and/or modify
8 | it under the terms of the GNU General Public License as published by
9 | the Free Software Foundation, either version 3 of the License, or
10 | (at your option) any later version.
11 |
12 | This program is distributed in the hope that it will be useful,
13 | but WITHOUT ANY WARRANTY; without even the implied warranty of
14 | MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
15 | GNU General Public License for more details.
16 |
17 | You should have received a copy of the GNU General Public License
18 | along with this program. If not, see .
19 | */
20 |
21 | #include "AudioOutputSerialWAV.h"
22 |
23 | bool AudioOutputSerialWAV::begin()
24 | {
25 | static uint8_t wavHeader[] = { // Hardcoded simple WAV header with 0xffffffff lengths all around
26 | 0x52, 0x49, 0x46, 0x46, 0xff, 0xff, 0xff, 0xff, 0x57, 0x41, 0x56, 0x45,
27 | 0x66, 0x6d, 0x74, 0x20, 0x10, 0x00, 0x00, 0x00, 0x01, 0x00, 0x02, 0x00, 0x22, 0x56, 0x00, 0x00, 0x88, 0x58, 0x01, 0x00, 0x04, 0x00, 0x10, 0x00,
28 | 0x64, 0x61, 0x74, 0x61, 0xff, 0xff, 0xff, 0xff };
29 | wavHeader[22] = channels & 0xff;
30 | wavHeader[23] = 0;
31 | wavHeader[24] = hertz & 0xff;
32 | wavHeader[25] = (hertz >> 8) & 0xff;
33 | wavHeader[26] = (hertz >> 16) & 0xff;
34 | wavHeader[27] = (hertz >> 24) & 0xff;
35 | int byteRate = hertz * bps * channels / 8;
36 | wavHeader[28] = byteRate & 0xff;
37 | wavHeader[29] = (byteRate >> 8) & 0xff;
38 | wavHeader[30] = (byteRate >> 16) & 0xff;
39 | wavHeader[31] = (byteRate >> 24) & 0xff;
40 | wavHeader[32] = channels * bps / 8;
41 | wavHeader[33] = 0;
42 | wavHeader[34] = bps;
43 | wavHeader[35] = 0;
44 | Serial.write(wavHeader, sizeof(wavHeader));
45 | return true;
46 | }
47 |
48 | bool AudioOutputSerialWAV::ConsumeSample(int16_t sample[2])
49 | {
50 | for (int i=0; i> 8) & 0xff;
57 | Serial.write(l);
58 | Serial.write(h);
59 | }
60 | }
61 | return true;
62 | }
63 |
64 |
65 | bool AudioOutputSerialWAV::stop()
66 | {
67 | Serial.print("\n\n\nEOF\n\n\n");
68 | return true;
69 | }
70 |
71 |
--------------------------------------------------------------------------------
/AudioOutputSerialWAV.h:
--------------------------------------------------------------------------------
1 | /*
2 | AudioOutputSerialWAV
3 | Writes a mostly correct WAV file to the serial port
4 |
5 | Copyright (C) 2017 Earle F. Philhower, III
6 |
7 | This program is free software: you can redistribute it and/or modify
8 | it under the terms of the GNU General Public License as published by
9 | the Free Software Foundation, either version 3 of the License, or
10 | (at your option) any later version.
11 |
12 | This program is distributed in the hope that it will be useful,
13 | but WITHOUT ANY WARRANTY; without even the implied warranty of
14 | MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
15 | GNU General Public License for more details.
16 |
17 | You should have received a copy of the GNU General Public License
18 | along with this program. If not, see .
19 | */
20 |
21 | #ifndef _AUDIOOUTPUTSERIALWAV_H
22 | #define _AUDIOOUTPUTSERIALWAV_H
23 |
24 | #include "AudioOutput.h"
25 |
26 | class AudioOutputSerialWAV : public AudioOutput
27 | {
28 | public:
29 | AudioOutputSerialWAV() {};
30 | ~AudioOutputSerialWAV() {};
31 | virtual bool begin() override;
32 | virtual bool ConsumeSample(int16_t sample[2]) override;
33 | virtual bool stop() override;
34 | };
35 |
36 | #endif
37 |
38 |
--------------------------------------------------------------------------------
/CHANGES:
--------------------------------------------------------------------------------
1 |
2 | libmad - MPEG audio decoder library
3 | Copyright (C) 2000-2004 Underbit Technologies, Inc.
4 |
5 | $Id: CHANGES,v 1.14 2004/02/17 02:02:03 rob Exp $
6 |
7 | ===============================================================================
8 |
9 | Version 0.15.1 (beta)
10 |
11 | * Updated to autoconf 2.59, automake 1.8.2, libtool 1.5.2.
12 |
13 | * Replaced Layer III IMDCT routine with one based on a faster algorithm,
14 | improving both speed and accuracy.
15 |
16 | * Improved portability of the Huffman table initialization.
17 |
18 | * Fixed a problem that could result in an assertion failure in layer3.c
19 | due to an invalid Layer III free format bitrate.
20 |
21 | * Improved the robustness of Layer II bitrate/mode combinations, and added
22 | a new MAD_ERROR_BADMODE error enum. The allowability of low-bitrate
23 | stereo streams is influenced by the --enable-strict-iso option to
24 | `configure'.
25 |
26 | Version 0.15.0 (beta)
27 |
28 | * Updated to autoconf 2.57, automake 1.7.5, libtool 1.4.3.
29 |
30 | * Added new mad_f_div() API routine.
31 |
32 | * Added a 64th entry to the Layer I/Layer II scalefactor table, for better
33 | compatibility with existing streams. The --enable-strict-iso option to
34 | `configure' can be used to disable use of this entry.
35 |
36 | * Modified the header decoding routine to allow the reserved emphasis
37 | value, for better compatibility with existing streams. The
38 | --enable-strict-iso option to `configure' can be used to restore the
39 | previous behavior of reporting this value as an error.
40 |
41 | * Added new MAD_EMPHASIS_RESERVED enumeration constant.
42 |
43 | * Fixed a bug in the ARM version of mad_f_scale64() discovered by Andre
44 | McCurdy.
45 |
46 | * Rewrote PowerPC assembly for minor gains.
47 |
48 | * Modified mad_timer_fraction() to avoid the possibility of division by
49 | zero when 0 is passed as the second argument.
50 |
51 | * Fixed a non-fatal problem caused by attempting to designate ancillary
52 | bits in Layer III after a decoding error.
53 |
54 | * Changed to build a shared library by default.
55 |
56 | * Changed to use native Cygwin build by default; give --host=mingw32 to
57 | `configure' to use MinGW (and avoid a dependency on the Cygwin DLL).
58 |
59 | Version 0.14.2 (beta)
60 |
61 | * Changed Cygwin builds to use MinGW; resulting Win32 executables no
62 | longer have a dependency on Cygwin DLLs.
63 |
64 | * Added a new mad_stream_errorstr() API function to libmad for retrieving
65 | a string description of the current error condition.
66 |
67 | Version 0.14.1 (beta)
68 |
69 | * Updated config.guess and config.sub to latest upstream versions.
70 |
71 | * Enabled libtool versioning rather than release numbering.
72 |
73 | * Improved the documentation in minimad.c.
74 |
75 | * Several other small fixes.
76 |
77 | Version 0.14.0 (beta)
78 |
79 | * Added a 64-bit FPM negation operation to improve performance of subband
80 | synthesis on some platforms.
81 |
82 | * Improved MSVC++ portability and added MSVC++ project files.
83 |
84 | * Added rounding to Layer III requantization for slightly better accuracy.
85 |
86 | Version 0.13.0 (beta)
87 |
88 | * Ancillary data is now properly extracted from Layer III streams.
89 |
90 | * Rewrote the Layer III joint stereo decoding routine to correct a major
91 | MPEG-2 problem and a minor MPEG-1 problem decoding intensity stereo.
92 |
93 | * Eliminated the dependency on sign-extending right shifts for Layer I and
94 | Layer II.
95 |
96 | * Renamed `private' field to `private_bits' for better C++ compatibility.
97 |
98 | * Gratuitously renamed `sfreq' field to `samplerate' and
99 | MAD_ERROR_BADSAMPLEFREQ constant to MAD_ERROR_BADSAMPLERATE.
100 |
101 | * Added `samplerate' and `channels' fields to synth.pcm struct to allow
102 | these to be different from the decoded frame, and for simpler access.
103 |
104 | * Added new mad_stream_options() and mad_decoder_options() API entries for
105 | special runtime decoding options.
106 |
107 | * Added new MAD_OPTION_IGNORECRC and MAD_OPTION_HALFSAMPLERATE options.
108 |
109 | * Added new MAD_FLAG_FREEFORMAT indicator flag.
110 |
111 | * Fixed some bugs in the async decoder.
112 |
113 | * Added a new mad_timer_multiply() API routine.
114 |
115 | * Eliminated `+' from asm constraints under Intel for better compatibility
116 | with some compilers.
117 |
118 | * Fixed a PIC-related problem in imdct_l_arm.S.
119 |
120 | * Eliminated a static variable to make libmad thread-safe.
121 |
122 | Version 0.12.5 (beta)
123 |
124 | * Modified Layer III requantization to occur during Huffman decoding for
125 | significant performance gains.
126 |
127 | * Optimized short block IMDCT by eliminating redundant calculations.
128 |
129 | * Made several other Layer III performance improvements; added
130 | ASO_INTERLEAVE1, ASO_INTERLEAVE2, and ASO_ZEROCHECK
131 | architecture-specific options for best performance on various
132 | architectures.
133 |
134 | * Optimized synthesis DCT to store result values as soon as they are
135 | calculated.
136 |
137 | Version 0.12.4 (beta)
138 |
139 | * New PowerPC fixed-point assembly courtesy of David Blythe.
140 |
141 | * Reorganized fixed-point assembly routines for easier maintenance and
142 | better performance.
143 |
144 | * Improved performance of subband synthesis through better indexing and
145 | fewer local variables.
146 |
147 | * Added alias reduction for the lower two subbands of mixed short blocks,
148 | per a report of ambiguity with ISO/IEC 11172-3 and for uniformity with
149 | most other implementations. Also improved alias reduction performance
150 | using multiply/accumulate.
151 |
152 | * Added --enable-strict-iso option to `configure' to override best
153 | accepted practices such as the alias reduction for mixed short blocks.
154 |
155 | * Improved performance of Layer III IMDCT by using longer
156 | multiply/accumulate runs where possible.
157 |
158 | Version 0.12.3 (beta)
159 |
160 | * Added MPEG 2.5 support.
161 |
162 | * Added preliminary support for parameterizing the binary point position
163 | in the fixed-point representation.
164 |
165 | * Added multiply/accumulate optimization to the Layer III IMDCT for long
166 | blocks.
167 |
168 | * Fixed a bug in the handling of Layer III mixed_block_flag.
169 |
170 | * Fixed a configure problem when multiple -O CFLAGS are present.
171 |
172 | Version 0.12.2 (beta)
173 |
174 | * Rearranged the synthesis polyphase filterbank memory vector for better
175 | locality of reference, and rewrote mad_synth_frame() to accommodate,
176 | resulting in improved performance.
177 |
178 | * Discovered a combination of compiler optimization flags that further
179 | improve performance.
180 |
181 | * Changed some array references in layer3.c to pointer derefs.
182 |
183 | Version 0.12.1 (beta)
184 |
185 | * Resolved the intensity + MS joint stereo issue (a simple bug).
186 | OPT_ISKLUGE is no longer considered to be a kluge.
187 |
188 | * Fixed another, hopefully last main_data memory bug.
189 |
190 | * Split part of struct mad_frame into struct mad_header for convenience
191 | and size.
192 |
193 | Version 0.12.0 (alpha)
194 |
195 | * Changed the build environment to use automake and libtool. A libmad
196 | shared library can now be built using the --enable-shared option to
197 | `configure'.
198 |
199 | * Added another callback to MAD's high-level decoder API after the frame
200 | header has been read but before the frame's audio data is decoded.
201 |
202 | * Streamlined header processing so that mad_frame_decode() can be called
203 | with or without having already called mad_frame_header().
204 |
205 | * Fixed some other header reading miscellany, including CRC handling and
206 | free bitrate detection, and frame length verification with free
207 | bitrates.
208 |
209 | * Fixed a problem with Layer III free bitrates > 320 kbps. The main_data
210 | buffer size should now be large enough to handle any size frame, by
211 | virtue of the maximum possible part2_3_length.
212 |
213 | * Further developed the async API; arbitrary messages can now be passed to
214 | the subsidiary decoding process.
215 |
216 | * Streamlined timer.c and extended its interface. It now has support for
217 | video frame/field lengths, including output support for drop-frame
218 | encoding.
219 |
220 | * Replaced many constant integer preprocessor defines with enums.
221 |
222 | Version 0.11.4 (beta)
223 |
224 | * Fixed free format bitrate discovery.
225 |
226 | * Changed the timer implementation and extended its interface.
227 |
228 | * Integrated Nicolas Pitre's patch for pre-shifting at compile-time and
229 | for better multiply/accumulate code output.
230 |
231 | * Applied Simon Burge's patch to imdct_l_arm.S for a.out compatibility.
232 |
233 | * Added -mtune=strongarm for all ARM targets.
234 |
235 | Version 0.11.3 (beta)
236 |
237 | * Added new --enable-speed and --enable-accuracy options for `configure'
238 | to automatically select appropriate SSO/ASO options, et al.
239 |
240 | * Modified subband synthesis to use multiply/accumulate optimization (if
241 | available) for better speed and/or accuracy.
242 |
243 | * Incorporated Andre McCurdy's changes for further rounding optimizations
244 | in the rest of his code.
245 |
246 | Version 0.11.2 (beta)
247 |
248 | * Incorporated Nicolas Pitre's ARM assembly and parameterized scaling
249 | changes.
250 |
251 | * Incorporated Andre McCurdy's ARM assembly optimization (used only if
252 | --enable-aso is given to `configure' to enable architecture-specific
253 | optimizations.)
254 |
255 | * Reduced FPM_INTEL assembly to two instructions.
256 |
257 | * Fixed accuracy problems with certain FPM modes in synth.c.
258 |
259 | * Improved the accuracy of FPM_APPROX.
260 |
261 | * Improved the accuracy of SSO.
262 |
263 | * Improved sync discovery by checking for a sync word in the following
264 | frame.
265 |
266 | * Minor code clean-up.
267 |
268 | * Added experimental rules for generating a libmad.so shared library.
269 |
270 | Version 0.11.1 (beta)
271 |
272 | * Moved libmad code into a separate directory.
273 |
274 | * Changed SSO to be disabled by default, as output accuracy is deemed to
275 | be more important than speed in the general case.
276 |
277 | * Fixed a bug in Layer III sanity checking that could cause a crash on
278 | certain random data input.
279 |
280 | * Extended the Layer III requantization table from 8191 to 8206 as some
281 | encoders are known to use these values, even though ISO/IEC 11172-3
282 | suggests the maximum should be 8191.
283 |
284 | Version 0.11.0 (beta)
285 |
286 | * Implemented MPEG-2 extension to Lower Sampling Frequencies.
287 |
288 | * Improved Layer III performance by avoiding IMDCT calculation when all
289 | input samples are zero.
290 |
291 | * Significantly reduced size of Layer II tables.
292 |
293 | Version 0.10.3 (beta)
294 |
295 | * Improved SSO output quality.
296 |
297 | * Made portable to cygwin.
298 |
299 | * Localized memory references in III_huffdecode() for better performance.
300 |
301 | Version 0.10.2 (beta)
302 |
303 | * Rewrote Layer III long block 36-point IMDCT routine for better
304 | performance.
305 |
306 | * Improved subband synthesis fixed-point games somewhat.
307 |
308 | Version 0.10.1 (beta)
309 |
310 | * Added a subband synthesis optimization (SSO) which involves modifying
311 | the fixed-point multiplication method during windowing. This produces
312 | subtle differences in the output but improves performance greatly.
313 |
314 | * Added I_STEREO and MS_STEREO flags to frame struct.
315 |
316 | * Eliminated privately-used CRCFAILED flag.
317 |
318 | * Fixed a bug where Layer III decoding could crash on some badly-formatted
319 | (e.g. non-MPEG) bitstreams.
320 |
321 | * Miscellaneous code clean-up.
322 |
323 | Version 0.10.0 (beta)
324 |
325 | * Added SPARC fixed-point math support.
326 |
327 | * Revamped libmad API for better high- and low-level support.
328 |
329 | * Documented more of the code.
330 |
331 | * Changed sync semantics such that new stream buffers are assumed to be
332 | sync-aligned.
333 |
334 | * Changed Layer III to dynamically allocate static memory so as not to
335 | waste it (about 6.4K) when only decoding Layer I or Layer II.
336 |
337 | ===============================================================================
338 |
339 |
--------------------------------------------------------------------------------
/COPYING:
--------------------------------------------------------------------------------
1 | GNU GENERAL PUBLIC LICENSE
2 | Version 2, June 1991
3 |
4 | Copyright (C) 1989, 1991 Free Software Foundation, Inc.
5 | 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
6 | Everyone is permitted to copy and distribute verbatim copies
7 | of this license document, but changing it is not allowed.
8 |
9 | Preamble
10 |
11 | The licenses for most software are designed to take away your
12 | freedom to share and change it. By contrast, the GNU General Public
13 | License is intended to guarantee your freedom to share and change free
14 | software--to make sure the software is free for all its users. This
15 | General Public License applies to most of the Free Software
16 | Foundation's software and to any other program whose authors commit to
17 | using it. (Some other Free Software Foundation software is covered by
18 | the GNU Library General Public License instead.) You can apply it to
19 | your programs, too.
20 |
21 | When we speak of free software, we are referring to freedom, not
22 | price. Our General Public Licenses are designed to make sure that you
23 | have the freedom to distribute copies of free software (and charge for
24 | this service if you wish), that you receive source code or can get it
25 | if you want it, that you can change the software or use pieces of it
26 | in new free programs; and that you know you can do these things.
27 |
28 | To protect your rights, we need to make restrictions that forbid
29 | anyone to deny you these rights or to ask you to surrender the rights.
30 | These restrictions translate to certain responsibilities for you if you
31 | distribute copies of the software, or if you modify it.
32 |
33 | For example, if you distribute copies of such a program, whether
34 | gratis or for a fee, you must give the recipients all the rights that
35 | you have. You must make sure that they, too, receive or can get the
36 | source code. And you must show them these terms so they know their
37 | rights.
38 |
39 | We protect your rights with two steps: (1) copyright the software, and
40 | (2) offer you this license which gives you legal permission to copy,
41 | distribute and/or modify the software.
42 |
43 | Also, for each author's protection and ours, we want to make certain
44 | that everyone understands that there is no warranty for this free
45 | software. If the software is modified by someone else and passed on, we
46 | want its recipients to know that what they have is not the original, so
47 | that any problems introduced by others will not reflect on the original
48 | authors' reputations.
49 |
50 | Finally, any free program is threatened constantly by software
51 | patents. We wish to avoid the danger that redistributors of a free
52 | program will individually obtain patent licenses, in effect making the
53 | program proprietary. To prevent this, we have made it clear that any
54 | patent must be licensed for everyone's free use or not licensed at all.
55 |
56 | The precise terms and conditions for copying, distribution and
57 | modification follow.
58 |
59 | GNU GENERAL PUBLIC LICENSE
60 | TERMS AND CONDITIONS FOR COPYING, DISTRIBUTION AND MODIFICATION
61 |
62 | 0. This License applies to any program or other work which contains
63 | a notice placed by the copyright holder saying it may be distributed
64 | under the terms of this General Public License. The "Program", below,
65 | refers to any such program or work, and a "work based on the Program"
66 | means either the Program or any derivative work under copyright law:
67 | that is to say, a work containing the Program or a portion of it,
68 | either verbatim or with modifications and/or translated into another
69 | language. (Hereinafter, translation is included without limitation in
70 | the term "modification".) Each licensee is addressed as "you".
71 |
72 | Activities other than copying, distribution and modification are not
73 | covered by this License; they are outside its scope. The act of
74 | running the Program is not restricted, and the output from the Program
75 | is covered only if its contents constitute a work based on the
76 | Program (independent of having been made by running the Program).
77 | Whether that is true depends on what the Program does.
78 |
79 | 1. You may copy and distribute verbatim copies of the Program's
80 | source code as you receive it, in any medium, provided that you
81 | conspicuously and appropriately publish on each copy an appropriate
82 | copyright notice and disclaimer of warranty; keep intact all the
83 | notices that refer to this License and to the absence of any warranty;
84 | and give any other recipients of the Program a copy of this License
85 | along with the Program.
86 |
87 | You may charge a fee for the physical act of transferring a copy, and
88 | you may at your option offer warranty protection in exchange for a fee.
89 |
90 | 2. You may modify your copy or copies of the Program or any portion
91 | of it, thus forming a work based on the Program, and copy and
92 | distribute such modifications or work under the terms of Section 1
93 | above, provided that you also meet all of these conditions:
94 |
95 | a) You must cause the modified files to carry prominent notices
96 | stating that you changed the files and the date of any change.
97 |
98 | b) You must cause any work that you distribute or publish, that in
99 | whole or in part contains or is derived from the Program or any
100 | part thereof, to be licensed as a whole at no charge to all third
101 | parties under the terms of this License.
102 |
103 | c) If the modified program normally reads commands interactively
104 | when run, you must cause it, when started running for such
105 | interactive use in the most ordinary way, to print or display an
106 | announcement including an appropriate copyright notice and a
107 | notice that there is no warranty (or else, saying that you provide
108 | a warranty) and that users may redistribute the program under
109 | these conditions, and telling the user how to view a copy of this
110 | License. (Exception: if the Program itself is interactive but
111 | does not normally print such an announcement, your work based on
112 | the Program is not required to print an announcement.)
113 |
114 | These requirements apply to the modified work as a whole. If
115 | identifiable sections of that work are not derived from the Program,
116 | and can be reasonably considered independent and separate works in
117 | themselves, then this License, and its terms, do not apply to those
118 | sections when you distribute them as separate works. But when you
119 | distribute the same sections as part of a whole which is a work based
120 | on the Program, the distribution of the whole must be on the terms of
121 | this License, whose permissions for other licensees extend to the
122 | entire whole, and thus to each and every part regardless of who wrote it.
123 |
124 | Thus, it is not the intent of this section to claim rights or contest
125 | your rights to work written entirely by you; rather, the intent is to
126 | exercise the right to control the distribution of derivative or
127 | collective works based on the Program.
128 |
129 | In addition, mere aggregation of another work not based on the Program
130 | with the Program (or with a work based on the Program) on a volume of
131 | a storage or distribution medium does not bring the other work under
132 | the scope of this License.
133 |
134 | 3. You may copy and distribute the Program (or a work based on it,
135 | under Section 2) in object code or executable form under the terms of
136 | Sections 1 and 2 above provided that you also do one of the following:
137 |
138 | a) Accompany it with the complete corresponding machine-readable
139 | source code, which must be distributed under the terms of Sections
140 | 1 and 2 above on a medium customarily used for software interchange; or,
141 |
142 | b) Accompany it with a written offer, valid for at least three
143 | years, to give any third party, for a charge no more than your
144 | cost of physically performing source distribution, a complete
145 | machine-readable copy of the corresponding source code, to be
146 | distributed under the terms of Sections 1 and 2 above on a medium
147 | customarily used for software interchange; or,
148 |
149 | c) Accompany it with the information you received as to the offer
150 | to distribute corresponding source code. (This alternative is
151 | allowed only for noncommercial distribution and only if you
152 | received the program in object code or executable form with such
153 | an offer, in accord with Subsection b above.)
154 |
155 | The source code for a work means the preferred form of the work for
156 | making modifications to it. For an executable work, complete source
157 | code means all the source code for all modules it contains, plus any
158 | associated interface definition files, plus the scripts used to
159 | control compilation and installation of the executable. However, as a
160 | special exception, the source code distributed need not include
161 | anything that is normally distributed (in either source or binary
162 | form) with the major components (compiler, kernel, and so on) of the
163 | operating system on which the executable runs, unless that component
164 | itself accompanies the executable.
165 |
166 | If distribution of executable or object code is made by offering
167 | access to copy from a designated place, then offering equivalent
168 | access to copy the source code from the same place counts as
169 | distribution of the source code, even though third parties are not
170 | compelled to copy the source along with the object code.
171 |
172 | 4. You may not copy, modify, sublicense, or distribute the Program
173 | except as expressly provided under this License. Any attempt
174 | otherwise to copy, modify, sublicense or distribute the Program is
175 | void, and will automatically terminate your rights under this License.
176 | However, parties who have received copies, or rights, from you under
177 | this License will not have their licenses terminated so long as such
178 | parties remain in full compliance.
179 |
180 | 5. You are not required to accept this License, since you have not
181 | signed it. However, nothing else grants you permission to modify or
182 | distribute the Program or its derivative works. These actions are
183 | prohibited by law if you do not accept this License. Therefore, by
184 | modifying or distributing the Program (or any work based on the
185 | Program), you indicate your acceptance of this License to do so, and
186 | all its terms and conditions for copying, distributing or modifying
187 | the Program or works based on it.
188 |
189 | 6. Each time you redistribute the Program (or any work based on the
190 | Program), the recipient automatically receives a license from the
191 | original licensor to copy, distribute or modify the Program subject to
192 | these terms and conditions. You may not impose any further
193 | restrictions on the recipients' exercise of the rights granted herein.
194 | You are not responsible for enforcing compliance by third parties to
195 | this License.
196 |
197 | 7. If, as a consequence of a court judgment or allegation of patent
198 | infringement or for any other reason (not limited to patent issues),
199 | conditions are imposed on you (whether by court order, agreement or
200 | otherwise) that contradict the conditions of this License, they do not
201 | excuse you from the conditions of this License. If you cannot
202 | distribute so as to satisfy simultaneously your obligations under this
203 | License and any other pertinent obligations, then as a consequence you
204 | may not distribute the Program at all. For example, if a patent
205 | license would not permit royalty-free redistribution of the Program by
206 | all those who receive copies directly or indirectly through you, then
207 | the only way you could satisfy both it and this License would be to
208 | refrain entirely from distribution of the Program.
209 |
210 | If any portion of this section is held invalid or unenforceable under
211 | any particular circumstance, the balance of the section is intended to
212 | apply and the section as a whole is intended to apply in other
213 | circumstances.
214 |
215 | It is not the purpose of this section to induce you to infringe any
216 | patents or other property right claims or to contest validity of any
217 | such claims; this section has the sole purpose of protecting the
218 | integrity of the free software distribution system, which is
219 | implemented by public license practices. Many people have made
220 | generous contributions to the wide range of software distributed
221 | through that system in reliance on consistent application of that
222 | system; it is up to the author/donor to decide if he or she is willing
223 | to distribute software through any other system and a licensee cannot
224 | impose that choice.
225 |
226 | This section is intended to make thoroughly clear what is believed to
227 | be a consequence of the rest of this License.
228 |
229 | 8. If the distribution and/or use of the Program is restricted in
230 | certain countries either by patents or by copyrighted interfaces, the
231 | original copyright holder who places the Program under this License
232 | may add an explicit geographical distribution limitation excluding
233 | those countries, so that distribution is permitted only in or among
234 | countries not thus excluded. In such case, this License incorporates
235 | the limitation as if written in the body of this License.
236 |
237 | 9. The Free Software Foundation may publish revised and/or new versions
238 | of the General Public License from time to time. Such new versions will
239 | be similar in spirit to the present version, but may differ in detail to
240 | address new problems or concerns.
241 |
242 | Each version is given a distinguishing version number. If the Program
243 | specifies a version number of this License which applies to it and "any
244 | later version", you have the option of following the terms and conditions
245 | either of that version or of any later version published by the Free
246 | Software Foundation. If the Program does not specify a version number of
247 | this License, you may choose any version ever published by the Free Software
248 | Foundation.
249 |
250 | 10. If you wish to incorporate parts of the Program into other free
251 | programs whose distribution conditions are different, write to the author
252 | to ask for permission. For software which is copyrighted by the Free
253 | Software Foundation, write to the Free Software Foundation; we sometimes
254 | make exceptions for this. Our decision will be guided by the two goals
255 | of preserving the free status of all derivatives of our free software and
256 | of promoting the sharing and reuse of software generally.
257 |
258 | NO WARRANTY
259 |
260 | 11. BECAUSE THE PROGRAM IS LICENSED FREE OF CHARGE, THERE IS NO WARRANTY
261 | FOR THE PROGRAM, TO THE EXTENT PERMITTED BY APPLICABLE LAW. EXCEPT WHEN
262 | OTHERWISE STATED IN WRITING THE COPYRIGHT HOLDERS AND/OR OTHER PARTIES
263 | PROVIDE THE PROGRAM "AS IS" WITHOUT WARRANTY OF ANY KIND, EITHER EXPRESSED
264 | OR IMPLIED, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF
265 | MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE. THE ENTIRE RISK AS
266 | TO THE QUALITY AND PERFORMANCE OF THE PROGRAM IS WITH YOU. SHOULD THE
267 | PROGRAM PROVE DEFECTIVE, YOU ASSUME THE COST OF ALL NECESSARY SERVICING,
268 | REPAIR OR CORRECTION.
269 |
270 | 12. IN NO EVENT UNLESS REQUIRED BY APPLICABLE LAW OR AGREED TO IN WRITING
271 | WILL ANY COPYRIGHT HOLDER, OR ANY OTHER PARTY WHO MAY MODIFY AND/OR
272 | REDISTRIBUTE THE PROGRAM AS PERMITTED ABOVE, BE LIABLE TO YOU FOR DAMAGES,
273 | INCLUDING ANY GENERAL, SPECIAL, INCIDENTAL OR CONSEQUENTIAL DAMAGES ARISING
274 | OUT OF THE USE OR INABILITY TO USE THE PROGRAM (INCLUDING BUT NOT LIMITED
275 | TO LOSS OF DATA OR DATA BEING RENDERED INACCURATE OR LOSSES SUSTAINED BY
276 | YOU OR THIRD PARTIES OR A FAILURE OF THE PROGRAM TO OPERATE WITH ANY OTHER
277 | PROGRAMS), EVEN IF SUCH HOLDER OR OTHER PARTY HAS BEEN ADVISED OF THE
278 | POSSIBILITY OF SUCH DAMAGES.
279 |
280 | END OF TERMS AND CONDITIONS
281 |
282 | How to Apply These Terms to Your New Programs
283 |
284 | If you develop a new program, and you want it to be of the greatest
285 | possible use to the public, the best way to achieve this is to make it
286 | free software which everyone can redistribute and change under these terms.
287 |
288 | To do so, attach the following notices to the program. It is safest
289 | to attach them to the start of each source file to most effectively
290 | convey the exclusion of warranty; and each file should have at least
291 | the "copyright" line and a pointer to where the full notice is found.
292 |
293 |
294 | Copyright (C)
295 |
296 | This program is free software; you can redistribute it and/or modify
297 | it under the terms of the GNU General Public License as published by
298 | the Free Software Foundation; either version 2 of the License, or
299 | (at your option) any later version.
300 |
301 | This program is distributed in the hope that it will be useful,
302 | but WITHOUT ANY WARRANTY; without even the implied warranty of
303 | MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
304 | GNU General Public License for more details.
305 |
306 | You should have received a copy of the GNU General Public License
307 | along with this program; if not, write to the Free Software
308 | Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
309 |
310 |
311 | Also add information on how to contact you by electronic and paper mail.
312 |
313 | If the program is interactive, make it output a short notice like this
314 | when it starts in an interactive mode:
315 |
316 | Gnomovision version 69, Copyright (C) year name of author
317 | Gnomovision comes with ABSOLUTELY NO WARRANTY; for details type `show w'.
318 | This is free software, and you are welcome to redistribute it
319 | under certain conditions; type `show c' for details.
320 |
321 | The hypothetical commands `show w' and `show c' should show the appropriate
322 | parts of the General Public License. Of course, the commands you use may
323 | be called something other than `show w' and `show c'; they could even be
324 | mouse-clicks or menu items--whatever suits your program.
325 |
326 | You should also get your employer (if you work as a programmer) or your
327 | school, if any, to sign a "copyright disclaimer" for the program, if
328 | necessary. Here is a sample; alter the names:
329 |
330 | Yoyodyne, Inc., hereby disclaims all copyright interest in the program
331 | `Gnomovision' (which makes passes at compilers) written by James Hacker.
332 |
333 | , 1 April 1989
334 | Ty Coon, President of Vice
335 |
336 | This General Public License does not permit incorporating your program into
337 | proprietary programs. If your program is a subroutine library, you may
338 | consider it more useful to permit linking proprietary applications with the
339 | library. If this is what you want to do, use the GNU Library General
340 | Public License instead of this License.
341 |
--------------------------------------------------------------------------------
/COPYRIGHT:
--------------------------------------------------------------------------------
1 |
2 | libmad - MPEG audio decoder library
3 | Copyright (C) 2000-2004 Underbit Technologies, Inc.
4 |
5 | This program is free software; you can redistribute it and/or modify
6 | it under the terms of the GNU General Public License as published by
7 | the Free Software Foundation; either version 2 of the License, or
8 | (at your option) any later version.
9 |
10 | This program is distributed in the hope that it will be useful,
11 | but WITHOUT ANY WARRANTY; without even the implied warranty of
12 | MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13 | GNU General Public License for more details.
14 |
15 | You should have received a copy of the GNU General Public License
16 | along with this program; if not, write to the Free Software
17 | Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
18 |
19 | If you would like to negotiate alternate licensing terms, you may do
20 | so by contacting: Underbit Technologies, Inc.
21 |
22 |
--------------------------------------------------------------------------------
/CREDITS:
--------------------------------------------------------------------------------
1 |
2 | libmad - MPEG audio decoder library
3 | Copyright (C) 2000-2004 Underbit Technologies, Inc.
4 |
5 | $Id: CREDITS,v 1.5 2004/02/17 02:02:03 rob Exp $
6 |
7 | ===============================================================================
8 |
9 | AUTHOR
10 |
11 | Except where otherwise noted, all code was authored by:
12 |
13 | Robert Leslie
14 |
15 | CONTRIBUTORS
16 |
17 | Significant contributions have been incorporated with thanks to:
18 |
19 | Anonymous
20 | 2002/03/15: frame.c
21 | - Reported problem with use of reserved emphasis value.
22 | 2003/08/31: layer12.c
23 | - Suggested support for certain disallowed bitrate/mode
24 | combinations.
25 |
26 | Niek Albers
27 | 2003/04/21: layer3.c
28 | - Reported runtime uninitialized use of `ptr' in designating
29 | ancillary bits after a decoding error.
30 |
31 | Christian Biere
32 | 2003/02/01: frame.c
33 | - Reported assertion failure in layer3.c due to an
34 | invalid/unsupported Layer III free format bitrate.
35 |
36 | David Blythe
37 | 2001/01/30: fixed.h
38 | - Provided initial PowerPC fixed-point assembly.
39 |
40 | Simon Burge
41 | 2000/09/20: imdct_l_arm.S
42 | - Suggested patch for a.out compatibility.
43 |
44 | Brian Cameron
45 | 2003/07/02: huffman.c
46 | - Suggested changes for improved portability.
47 |
48 | Joshua Haberman
49 | 2001/08/10: decoder.c, huffman.c
50 | - Suggested portability fixes.
51 |
52 | Timothy King
53 | 2002/05/04: sf_table.dat, layer12.c
54 | - Reported problem with use of (missing) scalefactor index 63.
55 |
56 | Felix von Leitner
57 | 2003/01/21: fixed.h
58 | - Suggested Intel scaling alternative for possible speedup.
59 |
60 | Andre McCurdy
61 | 2000/08/10: imdct_l_arm.S
62 | - ARM optimized assembly replacement for III_imdct_l().
63 | 2000/09/15: imdct_l_arm.S
64 | - Applied Nicolas Pitre's rounding optimisation in all remaining
65 | places.
66 | 2001/02/10: layer3.c
67 | - Inspiration for Huffman decoding and requantization rewrite, and
68 | other miscellany.
69 | 2001/03/24: imdct_l_arm.S
70 | - Corrected PIC unsafe code.
71 | 2002/02/16: fixed.h
72 | - Discovered bug in ARM version of mad_f_scale64().
73 |
74 | Haruhiko OGASAWARA
75 | 2001/01/28: layer3.c
76 | - Reported discrepancy in alias reduction for mixed short blocks.
77 |
78 | Brett Paterson
79 | 2001/10/28: global.h
80 | - Reported missing et al. under MS Embedded Visual C.
81 |
82 | Sean 'Shaleh' Perry
83 | 2000/04/04: fixed.h
84 | - Suggested use of size-dependent typedefs.
85 | 2001/10/22: config.guess, config.sub
86 | - Keep up to date for proper Debian packaging.
87 |
88 | Bertrand Petit
89 | 2001/11/05: synth.h
90 | - Suggested PCM channel enumeration constants.
91 | 2001/11/05: stream.h
92 | - Suggested MAD_ERROR_NONE enumeration constant.
93 | 2001/11/05: stream.c
94 | - Suggested mad_stream_errorstr() function.
95 |
96 | Nicolas Pitre
97 | 2000/09/09: fixed.h
98 | - Parameterized all scaling for correct use of all multiplication
99 | methods within mad_synth_frame().
100 | - Rewrote the FPM_ARM version of mad_f_mul() so we have 64-bit
101 | multiplication result, rounding and scaling with 3 instructions.
102 | 2000/09/09: imdct_l_arm.S
103 | - Optimized rounding + scaling operations.
104 | 2000/09/17: synth.c
105 | - Changed D[] run-time shifts to compile-time.
106 | - Modified synthesis for better multiply/accumulate code output.
107 | 2001/08/11: fixed.h, synth.c
108 | - Suggested 64-bit FPM negation and negative term factorization
109 | during synthesis.
110 | 2001/08/11: fixed.h
111 | - Suggested unrounded behavior for FPM_DEFAULT when OPT_SPEED.
112 | 2001/11/19: fixed.c
113 | - Suggested computation of any resampling ratio.
114 |
115 | ===============================================================================
116 |
117 |
--------------------------------------------------------------------------------
/README:
--------------------------------------------------------------------------------
1 |
2 | libmad - MPEG audio decoder library
3 | Copyright (C) 2000-2004 Underbit Technologies, Inc.
4 |
5 | $Id: README,v 1.4 2004/01/23 09:41:32 rob Exp $
6 |
7 | ===============================================================================
8 |
9 | INTRODUCTION
10 |
11 | MAD (libmad) is a high-quality MPEG audio decoder. It currently supports
12 | MPEG-1 and the MPEG-2 extension to Lower Sampling Frequencies, as well as
13 | the so-called MPEG 2.5 format. All three audio layers (Layer I, Layer II,
14 | and Layer III a.k.a. MP3) are fully implemented.
15 |
16 | MAD does not yet support MPEG-2 multichannel audio (although it should be
17 | backward compatible with such streams) nor does it currently support AAC.
18 |
19 | MAD has the following special features:
20 |
21 | - 24-bit PCM output
22 | - 100% fixed-point (integer) computation
23 | - completely new implementation based on the ISO/IEC standards
24 | - distributed under the terms of the GNU General Public License (GPL)
25 |
26 | Because MAD provides full 24-bit PCM output, applications using MAD are
27 | able to produce high quality audio. Even when the output device supports
28 | only 16-bit PCM, applications can use the extra resolution to increase the
29 | audible dynamic range through the use of dithering or noise shaping.
30 |
31 | Because MAD uses integer computation rather than floating point, it is
32 | well suited for architectures without a floating point unit. All
33 | calculations are performed with a 32-bit fixed-point integer
34 | representation.
35 |
36 | Because MAD is a new implementation of the ISO/IEC standards, it is
37 | unencumbered by the errors of other implementations. MAD is NOT a
38 | derivation of the ISO reference source or any other code. Considerable
39 | effort has been expended to ensure a correct implementation, even in cases
40 | where the standards are ambiguous or misleading.
41 |
42 | Because MAD is distributed under the terms of the GPL, its redistribution
43 | is not generally restricted, so long as the terms of the GPL are followed.
44 | This means MAD can be incorporated into other software as long as that
45 | software is also distributed under the GPL. (Should this be undesirable,
46 | alternate arrangements may be possible by contacting Underbit.)
47 |
48 | ===============================================================================
49 |
50 | ABOUT THE CODE
51 |
52 | The code is optimized and performs very well, although specific
53 | improvements can still be made. The output from the decoder library
54 | consists of 32-bit signed linear fixed-point values that can be easily
55 | scaled for any size PCM output, up to 24 bits per sample.
56 |
57 | The API for libmad can be found in the `mad.h' header file. Note that this
58 | file is automatically generated, and will not exist until after you have
59 | built the library.
60 |
61 | There are two APIs available, one high-level, and the other low-level.
62 | With the low-level API, each step of the decoding process must be handled
63 | explicitly, offering the greatest amount of control. With the high-level
64 | API, after callbacks are configured, a single routine will decode an
65 | entire bitstream.
66 |
67 | The high-level API may either be used synchronously or asynchronously. If
68 | used asynchronously, decoding will occur in a separate process.
69 | Communication is possible with the decoding process by passing control
70 | messages.
71 |
72 | The file `minimad.c' contains an example usage of the libmad API that
73 | shows only the bare minimum required to implement a useful decoder. It
74 | expects a regular file to be redirected to standard input, and it sends
75 | decoded 16-bit signed little-endian PCM samples to standard output. If a
76 | decoding error occurs, it is reported to standard error and decoding
77 | continues. Note that the scale() routine in this code is only provided as
78 | an example; it rounds MAD's high-resolution samples down to 16 bits, but
79 | does not perform any dithering or noise shaping. It is therefore not
80 | recommended to use this routine as-is in your own code if sound quality is
81 | important.
82 |
83 | Integer Performance
84 |
85 | To get the best possible performance, it is recommended that an assembly
86 | version of the fixed-point multiply and related routines be selected.
87 | Several such assembly routines have been written for various CPUs.
88 |
89 | If an assembly version is not available, a fast approximation version will
90 | be used. This will result in reduced accuracy of the decoder.
91 |
92 | Alternatively, if 64-bit integers are supported as a datatype by the
93 | compiler, another version can be used that is much more accurate.
94 | However, using an assembly version is generally much faster and just as
95 | accurate.
96 |
97 | More information can be gathered from the `fixed.h' header file.
98 |
99 | MAD's CPU-intensive subband synthesis routine can be further optimized at
100 | the expense of a slight loss in output accuracy due to a modified method
101 | for fixed-point multiplication with a small windowing constant. While this
102 | is helpful for performance and the output accuracy loss is generally
103 | undetectable, it is disabled by default and must be explicitly enabled.
104 |
105 | Under some architectures, other special optimizations may also be
106 | available.
107 |
108 | Audio Quality
109 |
110 | The output from MAD has been found to satisfy the ISO/IEC 11172-4
111 | computational accuracy requirements for compliance. In most
112 | configurations, MAD is a Full Layer III ISO/IEC 11172-3 audio decoder as
113 | defined by the standard.
114 |
115 | When the approximation version of the fixed-point multiply is used, MAD is
116 | a limited accuracy ISO/IEC 11172-3 audio decoder as defined by the
117 | standard.
118 |
119 | MAD can alternatively be configured to produce output with less or more
120 | accuracy than the default, as a tradeoff with performance.
121 |
122 | MAD produces output samples with a precision greater than 24 bits. Because
123 | most output formats use fewer bits, typically 16, it is recommended that a
124 | dithering algorithm be used (rather than rounding or truncating) to obtain
125 | the highest quality audio. However, dithering may unfavorably affect an
126 | analytic examination of the output (such as compliance testing); you may
127 | therefore wish to use rounding in this case instead.
128 |
129 | Portability Issues
130 |
131 | GCC is preferred to compile the code, but other compilers may also work.
132 | The assembly code in `fixed.h' depends on the inline assembly features of
133 | your compiler. If you're not using GCC or MSVC++, you can either write
134 | your own assembly macros or use the default (low quality output) version.
135 |
136 | The union initialization of `huffman.c' may not be portable to all
137 | platforms when GCC is not used.
138 |
139 | The code should not be sensitive to word sizes or byte ordering, however
140 | it does assume A % B has the same sign as A.
141 |
142 | ===============================================================================
143 |
144 | BUILDING AND INSTALLING
145 |
146 | Windows Platforms
147 |
148 | MAD can be built under Windows using either MSVC++ or Cygwin. A MSVC++
149 | project file can be found under the `msvc++' subdirectory.
150 |
151 | To build libmad using Cygwin, you will first need to install the Cygwin
152 | tools:
153 |
154 | http://www.cygwin.com/
155 |
156 | You may then proceed with the following POSIX instructions within the
157 | Cygwin shell.
158 |
159 | Note that by default Cygwin will build a library that depends on the
160 | Cygwin DLL. You can use MinGW to build a library that does not depend on
161 | the Cygwin DLL. To do so, give the option --host=mingw32 to `configure'.
162 |
163 | POSIX Platforms (including Cygwin)
164 |
165 | The code is distributed with a `configure' script that will generate for
166 | you a `Makefile' and a `config.h' for your platform. See the file
167 | `INSTALL' for generic instructions.
168 |
169 | The specific options you may want to give `configure' are:
170 |
171 | --enable-speed optimize for speed over accuracy
172 |
173 | --enable-accuracy optimize for accuracy over speed
174 |
175 | --disable-debugging do not compile with debugging support, and
176 | use more optimizations
177 |
178 | --disable-shared do not build a shared library
179 |
180 | Note that you need not specify one of --enable-speed or --enable-accuracy;
181 | in its default configuration, MAD is optimized for both. You should only
182 | use one of these options if you wish to compromise speed or accuracy for
183 | the other.
184 |
185 | By default the package will build a shared library if possible for your
186 | platform. If you want only a static library, use --disable-shared.
187 |
188 | It is not normally necessary to use the following options, but you may
189 | fine-tune the configuration with them if desired:
190 |
191 | --enable-fpm=ARCH use the ARCH-specific version of the
192 | fixed-point math assembly routines
193 | (current options are: intel, arm, mips,
194 | sparc, ppc; also allowed are: 64bit, approx)
195 |
196 | --enable-sso use the subband synthesis optimization,
197 | with reduced accuracy
198 |
199 | --disable-aso do not use certain architecture-specific
200 | optimizations
201 |
202 | By default an appropriate fixed-point assembly routine will be selected
203 | for the configured host type, if it can be determined. Thus if you are
204 | cross-compiling for another architecture, you should be sure either to
205 | give `configure' a host type argument (--host) or to use an explicit
206 | --enable-fpm option.
207 |
208 | If an appropriate assembly routine cannot be determined, the default
209 | approximation version will be used. In this case, use of an alternate
210 | --enable-fpm is highly recommended.
211 |
212 | Experimenting and Developing
213 |
214 | Further options for `configure' that may be useful to developers and
215 | experimenters are:
216 |
217 | --enable-debugging enable diagnostic debugging support and
218 | debugging symbols
219 |
220 | --enable-profiling generate `gprof' profiling code
221 |
222 | --enable-experimental enable code using the EXPERIMENTAL
223 | preprocessor define
224 |
225 | ===============================================================================
226 |
227 | COPYRIGHT
228 |
229 | Please read the `COPYRIGHT' file for copyright and warranty information.
230 | Also, the file `COPYING' contains the full text of the GNU GPL.
231 |
232 | Send inquiries, comments, bug reports, suggestions, patches, etc. to:
233 |
234 | Underbit Technologies, Inc.
235 |
236 | See also the MAD home page on the Web:
237 |
238 | http://www.underbit.com/products/mad/
239 |
240 | ===============================================================================
241 |
242 |
--------------------------------------------------------------------------------
/TODO:
--------------------------------------------------------------------------------
1 |
2 | libmad - MPEG audio decoder library
3 | Copyright (C) 2000-2004 Underbit Technologies, Inc.
4 |
5 | $Id: TODO,v 1.3 2004/02/05 09:02:39 rob Exp $
6 |
7 | ===============================================================================
8 |
9 | libmad:
10 | - more API layers (buffering, PCM samples, dithering, etc.)
11 | - x86 performance optimization compiler flags
12 | - function documentation, general docs
13 | - finish async API
14 | - parse system streams?
15 | - MPEG-2 MC, AAC?
16 | - logarithmic multiplication?
17 | - multiple frame decoding for better locality of reference?
18 | - frame serial numbers, Layer III frame continuity checks
19 |
20 | fixed.h:
21 | - experiment with FPM_INTEL:
22 |
23 | # if 1
24 | # define mad_f_scale64(hi, lo) \
25 | ({ mad_fixed_t __result; \
26 | asm ("shrl %3,%1\n\t" \
27 | "shll %4,%2\n\t" \
28 | "orl %2,%1" \
29 | : "=rm" (__result) \
30 | : "0" (lo), "r" (hi), \
31 | "I" (MAD_F_SCALEBITS), "I" (32 - MAD_F_SCALEBITS) \
32 | : "cc"); \
33 | __result; \
34 | })
35 | # else
36 | # define mad_f_scale64(hi, lo) \
37 | ({ mad_fixed64hi_t __hi_; \
38 | mad_fixed64lo_t __lo_; \
39 | mad_fixed_t __result; \
40 | asm ("sall %2,%1" \
41 | : "=r" (__hi_) \
42 | : "0" (hi), "I" (32 - MAD_F_SCALEBITS) \
43 | : "cc"); \
44 | asm ("shrl %2,%1" \
45 | : "=r" (__lo_) \
46 | : "0" (lo), "I" (MAD_F_SCALEBITS) \
47 | : "cc"); \
48 | asm ("orl %1,%2" \
49 | : "=rm" (__result) \
50 | : "r" (__hi_), "0" (__lo_) \
51 | : "cc"); \
52 | __result; \
53 | })
54 | # endif
55 |
56 | libmad Layer I:
57 | - check frame length sanity
58 |
59 | libmad Layer II:
60 | - check frame length sanity
61 |
62 | libmad Layer III:
63 | - circular buffer
64 | - optimize zero_part from Huffman decoding throughout
65 | - MPEG 2.5 8000 Hz sf bands? mixed blocks?
66 | - stereo->mono conversion optimization?
67 | - enable frame-at-a-time decoding
68 | - improve portability of huffman.c
69 |
70 |
--------------------------------------------------------------------------------
/VERSION:
--------------------------------------------------------------------------------
1 | 0.15.1b
2 | configure.ac:24
3 | version.h:25-28
4 | msvc++/config.h:99,105,120
5 | msvc++/mad.h:41-44
6 |
7 | Makefile.am:98-100
8 |
--------------------------------------------------------------------------------
/bit.c:
--------------------------------------------------------------------------------
1 | /*
2 | * libmad - MPEG audio decoder library
3 | * Copyright (C) 2000-2004 Underbit Technologies, Inc.
4 | *
5 | * This program is free software; you can redistribute it and/or modify
6 | * it under the terms of the GNU General Public License as published by
7 | * the Free Software Foundation; either version 2 of the License, or
8 | * (at your option) any later version.
9 | *
10 | * This program is distributed in the hope that it will be useful,
11 | * but WITHOUT ANY WARRANTY; without even the implied warranty of
12 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13 | * GNU General Public License for more details.
14 | *
15 | * You should have received a copy of the GNU General Public License
16 | * along with this program; if not, write to the Free Software
17 | * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
18 | *
19 | * $Id: bit.c,v 1.12 2004/01/23 09:41:32 rob Exp $
20 | */
21 |
22 | #pragma GCC optimize ("O3")
23 |
24 | #include
25 | # include "config.h"
26 |
27 | # include "global.h"
28 |
29 | # ifdef HAVE_LIMITS_H
30 | # include
31 | # else
32 | # define CHAR_BIT 8
33 | # endif
34 |
35 | # include "bit.h"
36 |
37 | /*
38 | * This is the lookup table for computing the CRC-check word.
39 | * As described in section 2.4.3.1 and depicted in Figure A.9
40 | * of ISO/IEC 11172-3, the generator polynomial is:
41 | *
42 | * G(X) = X^16 + X^15 + X^2 + 1
43 | */
44 | static const unsigned int crc_table[256] PROGMEM = {
45 | 0x0000, 0x8005, 0x800f, 0x000a, 0x801b, 0x001e, 0x0014, 0x8011,
46 | 0x8033, 0x0036, 0x003c, 0x8039, 0x0028, 0x802d, 0x8027, 0x0022,
47 | 0x8063, 0x0066, 0x006c, 0x8069, 0x0078, 0x807d, 0x8077, 0x0072,
48 | 0x0050, 0x8055, 0x805f, 0x005a, 0x804b, 0x004e, 0x0044, 0x8041,
49 | 0x80c3, 0x00c6, 0x00cc, 0x80c9, 0x00d8, 0x80dd, 0x80d7, 0x00d2,
50 | 0x00f0, 0x80f5, 0x80ff, 0x00fa, 0x80eb, 0x00ee, 0x00e4, 0x80e1,
51 | 0x00a0, 0x80a5, 0x80af, 0x00aa, 0x80bb, 0x00be, 0x00b4, 0x80b1,
52 | 0x8093, 0x0096, 0x009c, 0x8099, 0x0088, 0x808d, 0x8087, 0x0082,
53 |
54 | 0x8183, 0x0186, 0x018c, 0x8189, 0x0198, 0x819d, 0x8197, 0x0192,
55 | 0x01b0, 0x81b5, 0x81bf, 0x01ba, 0x81ab, 0x01ae, 0x01a4, 0x81a1,
56 | 0x01e0, 0x81e5, 0x81ef, 0x01ea, 0x81fb, 0x01fe, 0x01f4, 0x81f1,
57 | 0x81d3, 0x01d6, 0x01dc, 0x81d9, 0x01c8, 0x81cd, 0x81c7, 0x01c2,
58 | 0x0140, 0x8145, 0x814f, 0x014a, 0x815b, 0x015e, 0x0154, 0x8151,
59 | 0x8173, 0x0176, 0x017c, 0x8179, 0x0168, 0x816d, 0x8167, 0x0162,
60 | 0x8123, 0x0126, 0x012c, 0x8129, 0x0138, 0x813d, 0x8137, 0x0132,
61 | 0x0110, 0x8115, 0x811f, 0x011a, 0x810b, 0x010e, 0x0104, 0x8101,
62 |
63 | 0x8303, 0x0306, 0x030c, 0x8309, 0x0318, 0x831d, 0x8317, 0x0312,
64 | 0x0330, 0x8335, 0x833f, 0x033a, 0x832b, 0x032e, 0x0324, 0x8321,
65 | 0x0360, 0x8365, 0x836f, 0x036a, 0x837b, 0x037e, 0x0374, 0x8371,
66 | 0x8353, 0x0356, 0x035c, 0x8359, 0x0348, 0x834d, 0x8347, 0x0342,
67 | 0x03c0, 0x83c5, 0x83cf, 0x03ca, 0x83db, 0x03de, 0x03d4, 0x83d1,
68 | 0x83f3, 0x03f6, 0x03fc, 0x83f9, 0x03e8, 0x83ed, 0x83e7, 0x03e2,
69 | 0x83a3, 0x03a6, 0x03ac, 0x83a9, 0x03b8, 0x83bd, 0x83b7, 0x03b2,
70 | 0x0390, 0x8395, 0x839f, 0x039a, 0x838b, 0x038e, 0x0384, 0x8381,
71 |
72 | 0x0280, 0x8285, 0x828f, 0x028a, 0x829b, 0x029e, 0x0294, 0x8291,
73 | 0x82b3, 0x02b6, 0x02bc, 0x82b9, 0x02a8, 0x82ad, 0x82a7, 0x02a2,
74 | 0x82e3, 0x02e6, 0x02ec, 0x82e9, 0x02f8, 0x82fd, 0x82f7, 0x02f2,
75 | 0x02d0, 0x82d5, 0x82df, 0x02da, 0x82cb, 0x02ce, 0x02c4, 0x82c1,
76 | 0x8243, 0x0246, 0x024c, 0x8249, 0x0258, 0x825d, 0x8257, 0x0252,
77 | 0x0270, 0x8275, 0x827f, 0x027a, 0x826b, 0x026e, 0x0264, 0x8261,
78 | 0x0220, 0x8225, 0x822f, 0x022a, 0x823b, 0x023e, 0x0234, 0x8231,
79 | 0x8213, 0x0216, 0x021c, 0x8219, 0x0208, 0x820d, 0x8207, 0x0202
80 | };
81 |
82 | # define CRC_POLY 0x8005
83 |
84 | /*
85 | * NAME: bit->init()
86 | * DESCRIPTION: initialize bit pointer struct
87 | */
88 | void mad_bit_init(struct mad_bitptr *bitptr, unsigned char const *byte)
89 | {
90 | stack(__FUNCTION__, __FILE__, __LINE__);
91 | bitptr->byte = byte;
92 | bitptr->cache = 0;
93 | bitptr->left = CHAR_BIT;
94 | }
95 |
96 | /*
97 | * NAME: bit->length()
98 | * DESCRIPTION: return number of bits between start and end points
99 | */
100 | unsigned int mad_bit_length(struct mad_bitptr const *begin,
101 | struct mad_bitptr const *end)
102 | {
103 | stack(__FUNCTION__, __FILE__, __LINE__);
104 | return begin->left +
105 | CHAR_BIT * (end->byte - (begin->byte + 1)) + (CHAR_BIT - end->left);
106 | }
107 |
108 | /*
109 | * NAME: bit->nextbyte()
110 | * DESCRIPTION: return pointer to next unprocessed byte
111 | */
112 | unsigned char const *mad_bit_nextbyte(struct mad_bitptr const *bitptr)
113 | {
114 | stack(__FUNCTION__, __FILE__, __LINE__);
115 | return bitptr->left == CHAR_BIT ? bitptr->byte : bitptr->byte + 1;
116 | }
117 |
118 | /*
119 | * NAME: bit->skip()
120 | * DESCRIPTION: advance bit pointer
121 | */
122 | void mad_bit_skip(struct mad_bitptr *bitptr, unsigned int len)
123 | {
124 | stack(__FUNCTION__, __FILE__, __LINE__);
125 | bitptr->byte += len / CHAR_BIT;
126 | bitptr->left -= len % CHAR_BIT;
127 |
128 | if (bitptr->left > CHAR_BIT) {
129 | bitptr->byte++;
130 | bitptr->left += CHAR_BIT;
131 | }
132 |
133 | if (bitptr->left < CHAR_BIT)
134 | bitptr->cache = *bitptr->byte;
135 | }
136 |
137 | /*
138 | * NAME: bit->read()
139 | * DESCRIPTION: read an arbitrary number of bits and return their UIMSBF value
140 | */
141 | unsigned long mad_bit_read(struct mad_bitptr *bitptr, unsigned int len)
142 | {
143 | register unsigned long value;
144 |
145 | if (bitptr->left == CHAR_BIT)
146 | bitptr->cache = *bitptr->byte;
147 |
148 | if (len < bitptr->left) {
149 | value = (bitptr->cache & ((1 << bitptr->left) - 1)) >>
150 | (bitptr->left - len);
151 | bitptr->left -= len;
152 |
153 | return value;
154 | }
155 |
156 | /* remaining bits in current byte */
157 |
158 | value = bitptr->cache & ((1 << bitptr->left) - 1);
159 | len -= bitptr->left;
160 |
161 | bitptr->byte++;
162 | bitptr->left = CHAR_BIT;
163 |
164 | /* more bytes */
165 |
166 | while (len >= CHAR_BIT) {
167 | value = (value << CHAR_BIT) | *bitptr->byte++;
168 | len -= CHAR_BIT;
169 | }
170 |
171 | if (len > 0) {
172 | bitptr->cache = *bitptr->byte;
173 |
174 | value = (value << len) | (bitptr->cache >> (CHAR_BIT - len));
175 | bitptr->left -= len;
176 | }
177 |
178 | return value;
179 | }
180 |
181 | # if 0
182 | /*
183 | * NAME: bit->write()
184 | * DESCRIPTION: write an arbitrary number of bits
185 | */
186 | void mad_bit_write(struct mad_bitptr *bitptr, unsigned int len,
187 | unsigned long value)
188 | {
189 | unsigned char *ptr;
190 | stack(__FUNCTION__, __FILE__, __LINE__);
191 |
192 | ptr = (unsigned char *) bitptr->byte;
193 |
194 | /* ... */
195 | }
196 | # endif
197 |
198 | /*
199 | * NAME: bit->crc()
200 | * DESCRIPTION: compute CRC-check word
201 | */
202 | unsigned short mad_bit_crc(struct mad_bitptr bitptr, unsigned int len,
203 | unsigned short init)
204 | {
205 | register unsigned int crc;
206 | stack(__FUNCTION__, __FILE__, __LINE__);
207 |
208 | for (crc = init; len >= 32; len -= 32) {
209 | register unsigned long data;
210 |
211 | data = mad_bit_read(&bitptr, 32);
212 |
213 | crc = (crc << 8) ^ crc_table[((crc >> 8) ^ (data >> 24)) & 0xff];
214 | crc = (crc << 8) ^ crc_table[((crc >> 8) ^ (data >> 16)) & 0xff];
215 | crc = (crc << 8) ^ crc_table[((crc >> 8) ^ (data >> 8)) & 0xff];
216 | crc = (crc << 8) ^ crc_table[((crc >> 8) ^ (data >> 0)) & 0xff];
217 | }
218 |
219 | switch (len / 8) {
220 | case 3: crc = (crc << 8) ^
221 | crc_table[((crc >> 8) ^ mad_bit_read(&bitptr, 8)) & 0xff];
222 | case 2: crc = (crc << 8) ^
223 | crc_table[((crc >> 8) ^ mad_bit_read(&bitptr, 8)) & 0xff];
224 | case 1: crc = (crc << 8) ^
225 | crc_table[((crc >> 8) ^ mad_bit_read(&bitptr, 8)) & 0xff];
226 |
227 | len %= 8;
228 |
229 | case 0: break;
230 | }
231 |
232 | while (len--) {
233 | register unsigned int msb;
234 |
235 | msb = mad_bit_read(&bitptr, 1) ^ (crc >> 15);
236 |
237 | crc <<= 1;
238 | if (msb & 1)
239 | crc ^= CRC_POLY;
240 | }
241 |
242 | return crc & 0xffff;
243 | }
244 |
--------------------------------------------------------------------------------
/bit.h:
--------------------------------------------------------------------------------
1 | /*
2 | * libmad - MPEG audio decoder library
3 | * Copyright (C) 2000-2004 Underbit Technologies, Inc.
4 | *
5 | * This program is free software; you can redistribute it and/or modify
6 | * it under the terms of the GNU General Public License as published by
7 | * the Free Software Foundation; either version 2 of the License, or
8 | * (at your option) any later version.
9 | *
10 | * This program is distributed in the hope that it will be useful,
11 | * but WITHOUT ANY WARRANTY; without even the implied warranty of
12 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13 | * GNU General Public License for more details.
14 | *
15 | * You should have received a copy of the GNU General Public License
16 | * along with this program; if not, write to the Free Software
17 | * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
18 | *
19 | * $Id: bit.h,v 1.12 2004/01/23 09:41:32 rob Exp $
20 | */
21 |
22 | # ifndef LIBMAD_BIT_H
23 | # define LIBMAD_BIT_H
24 |
25 | struct mad_bitptr {
26 | unsigned char const *byte;
27 | unsigned short cache;
28 | unsigned short left;
29 | };
30 |
31 | void mad_bit_init(struct mad_bitptr *, unsigned char const *);
32 |
33 | # define mad_bit_finish(bitptr) /* nothing */
34 |
35 | unsigned int mad_bit_length(struct mad_bitptr const *,
36 | struct mad_bitptr const *);
37 |
38 | # define mad_bit_bitsleft(bitptr) ((bitptr)->left)
39 | unsigned char const *mad_bit_nextbyte(struct mad_bitptr const *);
40 |
41 | void mad_bit_skip(struct mad_bitptr *, unsigned int);
42 | unsigned long mad_bit_read(struct mad_bitptr *, unsigned int);
43 | void mad_bit_write(struct mad_bitptr *, unsigned int, unsigned long);
44 |
45 | unsigned short mad_bit_crc(struct mad_bitptr, unsigned int, unsigned short);
46 |
47 | # endif
48 |
--------------------------------------------------------------------------------
/config.h:
--------------------------------------------------------------------------------
1 | /* config.h. Generated by configure. */
2 | /* config.h.in. Generated from configure.ac by autoheader. */
3 |
4 | /* Define to enable diagnostic debugging support. */
5 | /* #undef DEBUG */
6 |
7 | // Uncomment to show heap and stack space on entry
8 | #define stack(a,b,c)
9 |
10 | /* Define to enable experimental code. */
11 | /* #undef EXPERIMENTAL */
12 |
13 | /* Define to 1 if you have the header file. */
14 | #define HAVE_ASSERT_H 1
15 |
16 | /* Define to 1 if you have the header file. */
17 | #undef HAVE_DLFCN_H
18 |
19 | /* Define to 1 if you have the header file. */
20 | #undef HAVE_ERRNO_H
21 |
22 | /* Define to 1 if you have the `fcntl' function. */
23 | #undef HAVE_FCNTL
24 |
25 | /* Define to 1 if you have the header file. */
26 | #undef HAVE_FCNTL_H
27 |
28 | /* Define to 1 if you have the `fork' function. */
29 | #undef HAVE_FORK
30 |
31 | /* Define to 1 if you have the header file. */
32 | #undef HAVE_INTTYPES_H
33 |
34 | /* Define to 1 if you have the header file. */
35 | #undef HAVE_LIMITS_H
36 |
37 | /* Define if your MIPS CPU supports a 2-operand MADD16 instruction. */
38 | /* #undef HAVE_MADD16_ASM */
39 |
40 | #define FPM_DEFAULT
41 |
42 | /* Define if your MIPS CPU supports a 2-operand MADD instruction. */
43 | #define HAVE_MADD_ASM 1
44 |
45 | /* Define to 1 if you have the header file. */
46 | #undef HAVE_MEMORY_H
47 |
48 | /* Define to 1 if you have the `pipe' function. */
49 | #undef HAVE_PIPE
50 |
51 | /* Define to 1 if you have the header file. */
52 | #define HAVE_STDINT_H 1
53 |
54 | /* Define to 1 if you have the header file. */
55 | #undef HAVE_STDLIB_H
56 |
57 | /* Define to 1 if you have the header file. */
58 | #undef HAVE_STRINGS_H
59 |
60 | /* Define to 1 if you have the header file. */
61 | #undef HAVE_STRING_H
62 |
63 | /* Define to 1 if you have the header file. */
64 | #undef HAVE_SYS_STAT_H
65 |
66 | /* Define to 1 if you have the header file. */
67 | #undef HAVE_SYS_TYPES_H
68 |
69 | /* Define to 1 if you have that is POSIX.1 compatible. */
70 | #undef HAVE_SYS_WAIT_H
71 |
72 | /* Define to 1 if you have the header file. */
73 | #undef HAVE_UNISTD_H
74 |
75 | /* Define to 1 if you have the `waitpid' function. */
76 | #undef HAVE_WAITPID
77 |
78 | /* Define to disable debugging assertions. */
79 | /* #undef NDEBUG */
80 |
81 | /* Define to optimize for accuracy over speed. */
82 | /* #undef OPT_ACCURACY */
83 |
84 | /* Define to optimize for speed over accuracy. */
85 | #define OPT_SPEED 1
86 |
87 | /* Define to enable a fast subband synthesis approximation optimization. */
88 | #define OPT_SSO 1
89 |
90 | /* Define to influence a strict interpretation of the ISO/IEC standards, even
91 | if this is in opposition with best accepted practices. */
92 | /* #undef OPT_STRICT */
93 |
94 | /* Name of package */
95 | #define PACKAGE "libmad"
96 |
97 | /* Define to the address where bug reports for this package should be sent. */
98 | #define PACKAGE_BUGREPORT "support@underbit.com"
99 |
100 | /* Define to the full name of this package. */
101 | #define PACKAGE_NAME "MPEG Audio Decoder"
102 |
103 | /* Define to the full name and version of this package. */
104 | #define PACKAGE_STRING "MPEG Audio Decoder 0.15.1b"
105 |
106 | /* Define to the one symbol short name of this package. */
107 | #define PACKAGE_TARNAME "libmad"
108 |
109 | /* Define to the version of this package. */
110 | #define PACKAGE_VERSION "0.15.1b"
111 |
112 | /* The size of a `int', as computed by sizeof. */
113 | #define SIZEOF_INT 4
114 |
115 | /* The size of a `long', as computed by sizeof. */
116 | #define SIZEOF_LONG 4
117 |
118 | /* The size of a `long long', as computed by sizeof. */
119 | #define SIZEOF_LONG_LONG 8
120 |
121 | /* Define to 1 if you have the ANSI C header files. */
122 | #undef STDC_HEADERS
123 |
124 | /* Version number of package */
125 | #define VERSION "0.15.1b-esp8266"
126 |
127 | /* Define to 1 if your processor stores words with the most significant byte
128 | first (like Motorola and SPARC, unlike Intel and VAX). */
129 | #undef WORDS_BIGENDIAN
130 |
131 | /* Define to empty if `const' does not conform to ANSI C. */
132 | /* #undef const */
133 |
134 | /* Define to `__inline__' or `__inline' if that's what the C compiler
135 | calls it, or to nothing if 'inline' is not supported under any name. */
136 | #ifndef __cplusplus
137 | /* #undef inline */
138 | #endif
139 |
140 | /* Define to `int' if does not define. */
141 | /* #undef pid_t */
142 |
--------------------------------------------------------------------------------
/decoder.c:
--------------------------------------------------------------------------------
1 | /*
2 | libmad - MPEG audio decoder library
3 | Copyright (C) 2000-2004 Underbit Technologies, Inc.
4 |
5 | This program is free software; you can redistribute it and/or modify
6 | it under the terms of the GNU General Public License as published by
7 | the Free Software Foundation; either version 2 of the License, or
8 | (at your option) any later version.
9 |
10 | This program is distributed in the hope that it will be useful,
11 | but WITHOUT ANY WARRANTY; without even the implied warranty of
12 | MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13 | GNU General Public License for more details.
14 |
15 | You should have received a copy of the GNU General Public License
16 | along with this program; if not, write to the Free Software
17 | Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
18 |
19 | $Id: decoder.c,v 1.22 2004/01/23 09:41:32 rob Exp $
20 | */
21 |
22 | #pragma GCC optimize ("O3")
23 |
24 | # include "config.h"
25 |
26 | # include "global.h"
27 |
28 | # ifdef HAVE_SYS_TYPES_H
29 | # include
30 | # endif
31 |
32 | # ifdef HAVE_SYS_WAIT_H
33 | # include
34 | # endif
35 |
36 | # ifdef HAVE_UNISTD_H
37 | # include
38 | # endif
39 |
40 | # ifdef HAVE_FCNTL_H
41 | # include
42 | # endif
43 |
44 | # include
45 |
46 | # ifdef HAVE_ERRNO_H
47 | # include
48 | # endif
49 |
50 | # include "stream.h"
51 | # include "frame.h"
52 | # include "synth.h"
53 | # include "decoder.h"
54 |
55 | /*
56 | NAME: decoder->init()
57 | DESCRIPTION: initialize a decoder object with callback routines
58 | */
59 | void mad_decoder_init(struct mad_decoder *decoder, void *data,
60 | enum mad_flow (*input_func)(void *,
61 | struct mad_stream *),
62 | enum mad_flow (*header_func)(void *,
63 | struct mad_header const *),
64 | enum mad_flow (*filter_func)(void *,
65 | struct mad_stream const *,
66 | struct mad_frame *),
67 | enum mad_flow (*output_func)(void *,
68 | struct mad_header const *,
69 | struct mad_pcm *),
70 | enum mad_flow (*error_func)(void *,
71 | struct mad_stream *,
72 | struct mad_frame *),
73 | enum mad_flow (*message_func)(void *,
74 | void *, unsigned int *))
75 | {
76 | decoder->mode = -1;
77 |
78 | decoder->options = 0;
79 |
80 | decoder->async.pid = 0;
81 | decoder->async.in = -1;
82 | decoder->async.out = -1;
83 |
84 | decoder->sync = 0;
85 |
86 | decoder->cb_data = data;
87 |
88 | decoder->input_func = input_func;
89 | decoder->header_func = header_func;
90 | decoder->filter_func = filter_func;
91 | decoder->output_func = output_func;
92 | decoder->error_func = error_func;
93 | decoder->message_func = message_func;
94 | }
95 |
96 | int mad_decoder_finish(struct mad_decoder *decoder)
97 | {
98 | # if defined(USE_ASYNC)
99 | if (decoder->mode == MAD_DECODER_MODE_ASYNC && decoder->async.pid) {
100 | pid_t pid;
101 | int status;
102 |
103 | close(decoder->async.in);
104 |
105 | do
106 | pid = waitpid(decoder->async.pid, &status, 0);
107 | while (pid == -1 && errno == EINTR);
108 |
109 | decoder->mode = -1;
110 |
111 | close(decoder->async.out);
112 |
113 | decoder->async.pid = 0;
114 | decoder->async.in = -1;
115 | decoder->async.out = -1;
116 |
117 | if (pid == -1)
118 | return -1;
119 |
120 | return (!WIFEXITED(status) || WEXITSTATUS(status)) ? -1 : 0;
121 | }
122 | # endif
123 |
124 | return 0;
125 | }
126 |
127 | # if defined(USE_ASYNC)
128 | static
129 | enum mad_flow send_io(int fd, void const *data, size_t len)
130 | {
131 | char const *ptr = data;
132 | ssize_t count;
133 |
134 | while (len) {
135 | do
136 | count = write(fd, ptr, len);
137 | while (count == -1 && errno == EINTR);
138 |
139 | if (count == -1)
140 | return MAD_FLOW_BREAK;
141 |
142 | len -= count;
143 | ptr += count;
144 | }
145 |
146 | return MAD_FLOW_CONTINUE;
147 | }
148 |
149 | static
150 | enum mad_flow receive_io(int fd, void *buffer, size_t len)
151 | {
152 | char *ptr = buffer;
153 | ssize_t count;
154 |
155 | while (len) {
156 | do
157 | count = read(fd, ptr, len);
158 | while (count == -1 && errno == EINTR);
159 |
160 | if (count == -1)
161 | return (errno == EAGAIN) ? MAD_FLOW_IGNORE : MAD_FLOW_BREAK;
162 | else if (count == 0)
163 | return MAD_FLOW_STOP;
164 |
165 | len -= count;
166 | ptr += count;
167 | }
168 |
169 | return MAD_FLOW_CONTINUE;
170 | }
171 |
172 | static
173 | enum mad_flow receive_io_blocking(int fd, void *buffer, size_t len)
174 | {
175 | int flags, blocking;
176 | enum mad_flow result;
177 |
178 | flags = fcntl(fd, F_GETFL);
179 | if (flags == -1)
180 | return MAD_FLOW_BREAK;
181 |
182 | blocking = flags & ~O_NONBLOCK;
183 |
184 | if (blocking != flags &&
185 | fcntl(fd, F_SETFL, blocking) == -1)
186 | return MAD_FLOW_BREAK;
187 |
188 | result = receive_io(fd, buffer, len);
189 |
190 | if (flags != blocking &&
191 | fcntl(fd, F_SETFL, flags) == -1)
192 | return MAD_FLOW_BREAK;
193 |
194 | return result;
195 | }
196 |
197 | static
198 | enum mad_flow send(int fd, void const *message, unsigned int size)
199 | {
200 | enum mad_flow result;
201 |
202 | /* send size */
203 |
204 | result = send_io(fd, &size, sizeof(size));
205 |
206 | /* send message */
207 |
208 | if (result == MAD_FLOW_CONTINUE)
209 | result = send_io(fd, message, size);
210 |
211 | return result;
212 | }
213 |
214 | static
215 | enum mad_flow receive(int fd, void **message, unsigned int *size)
216 | {
217 | enum mad_flow result;
218 | unsigned int actual;
219 |
220 | if (*message == 0)
221 | *size = 0;
222 |
223 | /* receive size */
224 |
225 | result = receive_io(fd, &actual, sizeof(actual));
226 |
227 | /* receive message */
228 |
229 | if (result == MAD_FLOW_CONTINUE) {
230 | if (actual > *size)
231 | actual -= *size;
232 | else {
233 | *size = actual;
234 | actual = 0;
235 | }
236 |
237 | if (*size > 0) {
238 | if (*message == 0) {
239 | *message = malloc(*size);
240 | if (*message == 0)
241 | return MAD_FLOW_BREAK;
242 | }
243 |
244 | result = receive_io_blocking(fd, *message, *size);
245 | }
246 |
247 | /* throw away remainder of message */
248 |
249 | while (actual && result == MAD_FLOW_CONTINUE) {
250 | char sink[256];
251 | unsigned int len;
252 |
253 | len = actual > sizeof(sink) ? sizeof(sink) : actual;
254 |
255 | result = receive_io_blocking(fd, sink, len);
256 |
257 | actual -= len;
258 | }
259 | }
260 |
261 | return result;
262 | }
263 |
264 | static
265 | enum mad_flow check_message(struct mad_decoder *decoder)
266 | {
267 | enum mad_flow result;
268 | void *message = 0;
269 | unsigned int size;
270 |
271 | result = receive(decoder->async.in, &message, &size);
272 |
273 | if (result == MAD_FLOW_CONTINUE) {
274 | if (decoder->message_func == 0)
275 | size = 0;
276 | else {
277 | result = decoder->message_func(decoder->cb_data, message, &size);
278 |
279 | if (result == MAD_FLOW_IGNORE ||
280 | result == MAD_FLOW_BREAK)
281 | size = 0;
282 | }
283 |
284 | if (send(decoder->async.out, message, size) != MAD_FLOW_CONTINUE)
285 | result = MAD_FLOW_BREAK;
286 | }
287 |
288 | if (message)
289 | free(message);
290 |
291 | return result;
292 | }
293 | # endif
294 |
295 | static
296 | enum mad_flow error_default(void *data, struct mad_stream *stream,
297 | struct mad_frame *frame)
298 | {
299 | int *bad_last_frame = data;
300 |
301 | switch (stream->error) {
302 | case MAD_ERROR_BADCRC:
303 | if (*bad_last_frame)
304 | mad_frame_mute(frame);
305 | else
306 | *bad_last_frame = 1;
307 |
308 | return MAD_FLOW_IGNORE;
309 |
310 | default:
311 | return MAD_FLOW_CONTINUE;
312 | }
313 | }
314 |
315 | static
316 | int run_sync(struct mad_decoder *decoder)
317 | {
318 | enum mad_flow (*error_func)(void *, struct mad_stream *, struct mad_frame *);
319 | void *error_data;
320 | int bad_last_frame = 0;
321 | struct mad_stream *stream;
322 | struct mad_frame *frame;
323 | struct mad_synth *synth;
324 | int result = 0;
325 | stack(__FUNCTION__, __FILE__, __LINE__);
326 |
327 | if (decoder->input_func == 0)
328 | return 0;
329 |
330 | if (decoder->error_func) {
331 | error_func = decoder->error_func;
332 | error_data = decoder->cb_data;
333 | }
334 | else {
335 | error_func = error_default;
336 | error_data = &bad_last_frame;
337 | }
338 |
339 | stream = &decoder->sync->stream;
340 | frame = &decoder->sync->frame;
341 | synth = &decoder->sync->synth;
342 |
343 | mad_stream_init(stream);
344 | mad_frame_init(frame);
345 | mad_synth_init(synth);
346 |
347 | mad_stream_options(stream, decoder->options);
348 |
349 | do {
350 | switch (decoder->input_func(decoder->cb_data, stream)) {
351 | case MAD_FLOW_STOP:
352 | goto done;
353 | case MAD_FLOW_BREAK:
354 | goto fail;
355 | case MAD_FLOW_IGNORE:
356 | continue;
357 | case MAD_FLOW_CONTINUE:
358 | break;
359 | }
360 |
361 | while (1) {
362 | # if defined(USE_ASYNC)
363 | if (decoder->mode == MAD_DECODER_MODE_ASYNC) {
364 | switch (check_message(decoder)) {
365 | case MAD_FLOW_IGNORE:
366 | case MAD_FLOW_CONTINUE:
367 | break;
368 | case MAD_FLOW_BREAK:
369 | goto fail;
370 | case MAD_FLOW_STOP:
371 | goto done;
372 | }
373 | }
374 | # endif
375 |
376 | if (decoder->header_func) {
377 | if (mad_header_decode(&frame->header, stream) == -1) {
378 | if (!MAD_RECOVERABLE(stream->error))
379 | break;
380 |
381 | switch (error_func(error_data, stream, frame)) {
382 | case MAD_FLOW_STOP:
383 | goto done;
384 | case MAD_FLOW_BREAK:
385 | goto fail;
386 | case MAD_FLOW_IGNORE:
387 | case MAD_FLOW_CONTINUE:
388 | default:
389 | continue;
390 | }
391 | }
392 |
393 | switch (decoder->header_func(decoder->cb_data, &frame->header)) {
394 | case MAD_FLOW_STOP:
395 | goto done;
396 | case MAD_FLOW_BREAK:
397 | goto fail;
398 | case MAD_FLOW_IGNORE:
399 | continue;
400 | case MAD_FLOW_CONTINUE:
401 | break;
402 | }
403 | }
404 | if (mad_frame_decode(frame, stream) == -1) {
405 | if (!MAD_RECOVERABLE(stream->error))
406 | break;
407 |
408 | switch (error_func(error_data, stream, frame)) {
409 | case MAD_FLOW_STOP:
410 | goto done;
411 | case MAD_FLOW_BREAK:
412 | goto fail;
413 | case MAD_FLOW_IGNORE:
414 | break;
415 | case MAD_FLOW_CONTINUE:
416 | default:
417 | continue;
418 | }
419 | }
420 | else
421 | bad_last_frame = 0;
422 |
423 | if (decoder->filter_func) {
424 | switch (decoder->filter_func(decoder->cb_data, stream, frame)) {
425 | case MAD_FLOW_STOP:
426 | goto done;
427 | case MAD_FLOW_BREAK:
428 | goto fail;
429 | case MAD_FLOW_IGNORE:
430 | continue;
431 | case MAD_FLOW_CONTINUE:
432 | break;
433 | }
434 | }
435 |
436 | // mad_synth_frame(synth, frame, decoder->output_func);
437 | if (decoder->output_func) {
438 | // switch (decoder->output_func(decoder->cb_data, &frame->header, &synth->pcm)) {
439 | switch (mad_synth_frame(synth, frame, decoder->output_func, decoder->cb_data)) { //decoder->output_func(decoder->cb_data, &frame->header, &synth->pcm)) {
440 | case MAD_FLOW_STOP:
441 | goto done;
442 | case MAD_FLOW_BREAK:
443 | goto fail;
444 | case MAD_FLOW_IGNORE:
445 | case MAD_FLOW_CONTINUE:
446 | break;
447 | }
448 | }
449 | }
450 | }
451 | while (stream->error == MAD_ERROR_BUFLEN);
452 |
453 | fail:
454 | result = -1;
455 |
456 | done:
457 | mad_synth_finish(synth);
458 | mad_frame_finish(frame);
459 | mad_stream_finish(stream);
460 |
461 | return result;
462 | }
463 |
464 | # if defined(USE_ASYNC)
465 | static
466 | int run_async(struct mad_decoder *decoder)
467 | {
468 | pid_t pid;
469 | int ptoc[2], ctop[2], flags;
470 |
471 | if (pipe(ptoc) == -1)
472 | return -1;
473 |
474 | if (pipe(ctop) == -1) {
475 | close(ptoc[0]);
476 | close(ptoc[1]);
477 | return -1;
478 | }
479 |
480 | flags = fcntl(ptoc[0], F_GETFL);
481 | if (flags == -1 ||
482 | fcntl(ptoc[0], F_SETFL, flags | O_NONBLOCK) == -1) {
483 | close(ctop[0]);
484 | close(ctop[1]);
485 | close(ptoc[0]);
486 | close(ptoc[1]);
487 | return -1;
488 | }
489 |
490 | pid = fork();
491 | if (pid == -1) {
492 | close(ctop[0]);
493 | close(ctop[1]);
494 | close(ptoc[0]);
495 | close(ptoc[1]);
496 | return -1;
497 | }
498 |
499 | decoder->async.pid = pid;
500 |
501 | if (pid) {
502 | /* parent */
503 |
504 | close(ptoc[0]);
505 | close(ctop[1]);
506 |
507 | decoder->async.in = ctop[0];
508 | decoder->async.out = ptoc[1];
509 |
510 | return 0;
511 | }
512 |
513 | /* child */
514 |
515 | close(ptoc[1]);
516 | close(ctop[0]);
517 |
518 | decoder->async.in = ptoc[0];
519 | decoder->async.out = ctop[1];
520 |
521 | _exit(run_sync(decoder));
522 |
523 | /* not reached */
524 | return -1;
525 | }
526 | # endif
527 |
528 | /*
529 | NAME: decoder->run()
530 | DESCRIPTION: run the decoder thread either synchronously or asynchronously
531 | */
532 | int mad_decoder_run(struct mad_decoder *decoder, enum mad_decoder_mode mode)
533 | {
534 | int result;
535 | int (*run)(struct mad_decoder *) = 0;
536 |
537 | switch (decoder->mode = mode) {
538 | case MAD_DECODER_MODE_SYNC:
539 | run = run_sync;
540 | break;
541 |
542 | case MAD_DECODER_MODE_ASYNC:
543 | # if defined(USE_ASYNC)
544 | run = run_async;
545 | # endif
546 | break;
547 | }
548 |
549 | if (run == 0)
550 | return -1;
551 |
552 | decoder->sync = malloc(sizeof(*decoder->sync));
553 | if (decoder->sync == 0)
554 | return -1;
555 |
556 | result = run(decoder);
557 |
558 | free(decoder->sync);
559 | decoder->sync = 0;
560 |
561 | return result;
562 | }
563 |
564 | /*
565 | NAME: decoder->message()
566 | DESCRIPTION: send a message to and receive a reply from the decoder process
567 | */
568 | int mad_decoder_message(struct mad_decoder *decoder,
569 | void *message, unsigned int *len)
570 | {
571 | # if defined(USE_ASYNC)
572 | if (decoder->mode != MAD_DECODER_MODE_ASYNC ||
573 | send(decoder->async.out, message, *len) != MAD_FLOW_CONTINUE ||
574 | receive(decoder->async.in, &message, len) != MAD_FLOW_CONTINUE)
575 | return -1;
576 |
577 | return 0;
578 | # else
579 | return -1;
580 | # endif
581 | }
582 |
--------------------------------------------------------------------------------
/decoder.h:
--------------------------------------------------------------------------------
1 | /*
2 | * libmad - MPEG audio decoder library
3 | * Copyright (C) 2000-2004 Underbit Technologies, Inc.
4 | *
5 | * This program is free software; you can redistribute it and/or modify
6 | * it under the terms of the GNU General Public License as published by
7 | * the Free Software Foundation; either version 2 of the License, or
8 | * (at your option) any later version.
9 | *
10 | * This program is distributed in the hope that it will be useful,
11 | * but WITHOUT ANY WARRANTY; without even the implied warranty of
12 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13 | * GNU General Public License for more details.
14 | *
15 | * You should have received a copy of the GNU General Public License
16 | * along with this program; if not, write to the Free Software
17 | * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
18 | *
19 | * $Id: decoder.h,v 1.17 2004/01/23 09:41:32 rob Exp $
20 | */
21 |
22 | # ifndef LIBMAD_DECODER_H
23 | # define LIBMAD_DECODER_H
24 |
25 | # include "stream.h"
26 | # include "frame.h"
27 | # include "synth.h"
28 |
29 | enum mad_decoder_mode {
30 | MAD_DECODER_MODE_SYNC = 0,
31 | MAD_DECODER_MODE_ASYNC
32 | };
33 |
34 | enum mad_flow {
35 | MAD_FLOW_CONTINUE = 0x0000, /* continue normally */
36 | MAD_FLOW_STOP = 0x0010, /* stop decoding normally */
37 | MAD_FLOW_BREAK = 0x0011, /* stop decoding and signal an error */
38 | MAD_FLOW_IGNORE = 0x0020 /* ignore the current frame */
39 | };
40 |
41 | struct mad_decoder {
42 | enum mad_decoder_mode mode;
43 |
44 | int options;
45 |
46 | struct {
47 | long pid;
48 | int in;
49 | int out;
50 | } async;
51 |
52 | struct {
53 | struct mad_stream stream;
54 | struct mad_frame frame;
55 | struct mad_synth synth;
56 | } *sync;
57 |
58 | void *cb_data;
59 |
60 | enum mad_flow (*input_func)(void *, struct mad_stream *);
61 | enum mad_flow (*header_func)(void *, struct mad_header const *);
62 | enum mad_flow (*filter_func)(void *,
63 | struct mad_stream const *, struct mad_frame *);
64 | enum mad_flow (*output_func)(void *,
65 | struct mad_header const *, struct mad_pcm *);
66 | enum mad_flow (*error_func)(void *, struct mad_stream *, struct mad_frame *);
67 | enum mad_flow (*message_func)(void *, void *, unsigned int *);
68 | };
69 |
70 | void mad_decoder_init(struct mad_decoder *, void *,
71 | enum mad_flow (*)(void *, struct mad_stream *),
72 | enum mad_flow (*)(void *, struct mad_header const *),
73 | enum mad_flow (*)(void *,
74 | struct mad_stream const *,
75 | struct mad_frame *),
76 | enum mad_flow (*)(void *,
77 | struct mad_header const *,
78 | struct mad_pcm *),
79 | enum mad_flow (*)(void *,
80 | struct mad_stream *,
81 | struct mad_frame *),
82 | enum mad_flow (*)(void *, void *, unsigned int *));
83 | int mad_decoder_finish(struct mad_decoder *);
84 |
85 | # define mad_decoder_options(decoder, opts) \
86 | ((void) ((decoder)->options = (opts)))
87 |
88 | int mad_decoder_run(struct mad_decoder *, enum mad_decoder_mode);
89 | int mad_decoder_message(struct mad_decoder *, void *, unsigned int *);
90 |
91 | # endif
92 |
--------------------------------------------------------------------------------
/fixed.c:
--------------------------------------------------------------------------------
1 | /*
2 | * libmad - MPEG audio decoder library
3 | * Copyright (C) 2000-2004 Underbit Technologies, Inc.
4 | *
5 | * This program is free software; you can redistribute it and/or modify
6 | * it under the terms of the GNU General Public License as published by
7 | * the Free Software Foundation; either version 2 of the License, or
8 | * (at your option) any later version.
9 | *
10 | * This program is distributed in the hope that it will be useful,
11 | * but WITHOUT ANY WARRANTY; without even the implied warranty of
12 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13 | * GNU General Public License for more details.
14 | *
15 | * You should have received a copy of the GNU General Public License
16 | * along with this program; if not, write to the Free Software
17 | * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
18 | *
19 | * $Id: fixed.c,v 1.13 2004/01/23 09:41:32 rob Exp $
20 | */
21 |
22 | #pragma GCC optimize ("O3")
23 |
24 | # include "config.h"
25 |
26 | # include "global.h"
27 |
28 | # include "fixed.h"
29 |
30 | /*
31 | * NAME: fixed->abs()
32 | * DESCRIPTION: return absolute value of a fixed-point number
33 | */
34 | mad_fixed_t mad_f_abs(mad_fixed_t x)
35 | {
36 | return x < 0 ? -x : x;
37 | }
38 |
39 | /*
40 | * NAME: fixed->div()
41 | * DESCRIPTION: perform division using fixed-point math
42 | */
43 | mad_fixed_t mad_f_div(mad_fixed_t x, mad_fixed_t y)
44 | {
45 | mad_fixed_t q, r;
46 | unsigned int bits;
47 |
48 | q = mad_f_abs(x / y);
49 |
50 | if (x < 0) {
51 | x = -x;
52 | y = -y;
53 | }
54 |
55 | r = x % y;
56 |
57 | if (y < 0) {
58 | x = -x;
59 | y = -y;
60 | }
61 |
62 | if (q > mad_f_intpart(MAD_F_MAX) &&
63 | !(q == -mad_f_intpart(MAD_F_MIN) && r == 0 && (x < 0) != (y < 0)))
64 | return 0;
65 |
66 | for (bits = MAD_F_FRACBITS; bits && r; --bits) {
67 | q <<= 1, r <<= 1;
68 | if (r >= y)
69 | r -= y, ++q;
70 | }
71 |
72 | /* round */
73 | if (2 * r >= y)
74 | ++q;
75 |
76 | /* fix sign */
77 | if ((x < 0) != (y < 0))
78 | q = -q;
79 |
80 | return q << bits;
81 | }
82 |
--------------------------------------------------------------------------------
/fixed.h:
--------------------------------------------------------------------------------
1 | /*
2 | * libmad - MPEG audio decoder library
3 | * Copyright (C) 2000-2004 Underbit Technologies, Inc.
4 | *
5 | * This program is free software; you can redistribute it and/or modify
6 | * it under the terms of the GNU General Public License as published by
7 | * the Free Software Foundation; either version 2 of the License, or
8 | * (at your option) any later version.
9 | *
10 | * This program is distributed in the hope that it will be useful,
11 | * but WITHOUT ANY WARRANTY; without even the implied warranty of
12 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13 | * GNU General Public License for more details.
14 | *
15 | * You should have received a copy of the GNU General Public License
16 | * along with this program; if not, write to the Free Software
17 | * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
18 | *
19 | * $Id: fixed.h,v 1.38 2004/02/17 02:02:03 rob Exp $
20 | */
21 |
22 | # ifndef LIBMAD_FIXED_H
23 | # define LIBMAD_FIXED_H
24 |
25 | # if SIZEOF_INT >= 4
26 | typedef signed int mad_fixed_t;
27 |
28 | typedef signed int mad_fixed64hi_t;
29 | typedef unsigned int mad_fixed64lo_t;
30 | # else
31 | typedef signed long mad_fixed_t;
32 |
33 | typedef signed long mad_fixed64hi_t;
34 | typedef unsigned long mad_fixed64lo_t;
35 | # endif
36 |
37 | # if defined(_MSC_VER)
38 | # define mad_fixed64_t signed __int64
39 | # elif 1 || defined(__GNUC__)
40 | # define mad_fixed64_t signed long long
41 | # endif
42 |
43 | # if defined(FPM_FLOAT)
44 | typedef double mad_sample_t;
45 | # else
46 | typedef mad_fixed_t mad_sample_t;
47 | # endif
48 |
49 | /*
50 | * Fixed-point format: 0xABBBBBBB
51 | * A == whole part (sign + 3 bits)
52 | * B == fractional part (28 bits)
53 | *
54 | * Values are signed two's complement, so the effective range is:
55 | * 0x80000000 to 0x7fffffff
56 | * -8.0 to +7.9999999962747097015380859375
57 | *
58 | * The smallest representable value is:
59 | * 0x00000001 == 0.0000000037252902984619140625 (i.e. about 3.725e-9)
60 | *
61 | * 28 bits of fractional accuracy represent about
62 | * 8.6 digits of decimal accuracy.
63 | *
64 | * Fixed-point numbers can be added or subtracted as normal
65 | * integers, but multiplication requires shifting the 64-bit result
66 | * from 56 fractional bits back to 28 (and rounding.)
67 | *
68 | * Changing the definition of MAD_F_FRACBITS is only partially
69 | * supported, and must be done with care.
70 | */
71 |
72 | # define MAD_F_FRACBITS 28
73 |
74 | # if MAD_F_FRACBITS == 28
75 | # define MAD_F(x) ((mad_fixed_t) (x##L))
76 | # else
77 | # if MAD_F_FRACBITS < 28
78 | # warning "MAD_F_FRACBITS < 28"
79 | # define MAD_F(x) ((mad_fixed_t) \
80 | (((x##L) + \
81 | (1L << (28 - MAD_F_FRACBITS - 1))) >> \
82 | (28 - MAD_F_FRACBITS)))
83 | # elif MAD_F_FRACBITS > 28
84 | # error "MAD_F_FRACBITS > 28 not currently supported"
85 | # define MAD_F(x) ((mad_fixed_t) \
86 | ((x##L) << (MAD_F_FRACBITS - 28)))
87 | # endif
88 | # endif
89 |
90 | # define MAD_F_MIN ((mad_fixed_t) -0x80000000L)
91 | # define MAD_F_MAX ((mad_fixed_t) +0x7fffffffL)
92 |
93 | # define MAD_F_ONE MAD_F(0x10000000)
94 |
95 | # define mad_f_tofixed(x) ((mad_fixed_t) \
96 | ((x) * (double) (1L << MAD_F_FRACBITS) + 0.5))
97 | # define mad_f_todouble(x) ((double) \
98 | ((x) / (double) (1L << MAD_F_FRACBITS)))
99 |
100 | # define mad_f_intpart(x) ((x) >> MAD_F_FRACBITS)
101 | # define mad_f_fracpart(x) ((x) & ((1L << MAD_F_FRACBITS) - 1))
102 | /* (x should be positive) */
103 |
104 | # define mad_f_fromint(x) ((x) << MAD_F_FRACBITS)
105 |
106 | # define mad_f_add(x, y) ((x) + (y))
107 | # define mad_f_sub(x, y) ((x) - (y))
108 |
109 | # if defined(FPM_FLOAT)
110 | # error "FPM_FLOAT not yet supported"
111 |
112 | # undef MAD_F
113 | # define MAD_F(x) mad_f_todouble(x)
114 |
115 | # define mad_f_mul(x, y) ((x) * (y))
116 | # define mad_f_scale64
117 |
118 | # undef ASO_ZEROCHECK
119 |
120 | # elif defined(FPM_64BIT)
121 |
122 | /*
123 | * This version should be the most accurate if 64-bit types are supported by
124 | * the compiler, although it may not be the most efficient.
125 | */
126 | # if defined(OPT_ACCURACY)
127 | # define mad_f_mul(x, y) \
128 | ((mad_fixed_t) \
129 | ((((mad_fixed64_t) (x) * (y)) + \
130 | (1L << (MAD_F_SCALEBITS - 1))) >> MAD_F_SCALEBITS))
131 | # else
132 | # define mad_f_mul(x, y) \
133 | ((mad_fixed_t) (((mad_fixed64_t) (x) * (y)) >> MAD_F_SCALEBITS))
134 | # endif
135 |
136 | # define MAD_F_SCALEBITS MAD_F_FRACBITS
137 |
138 | /* --- Intel --------------------------------------------------------------- */
139 |
140 | # elif defined(FPM_INTEL)
141 |
142 | # if defined(_MSC_VER)
143 | # pragma warning(push)
144 | # pragma warning(disable: 4035) /* no return value */
145 | static __forceinline
146 | mad_fixed_t mad_f_mul_inline(mad_fixed_t x, mad_fixed_t y)
147 | {
148 | enum {
149 | fracbits = MAD_F_FRACBITS
150 | };
151 |
152 | __asm {
153 | mov eax, x
154 | imul y
155 | shrd eax, edx, fracbits
156 | }
157 |
158 | /* implicit return of eax */
159 | }
160 | # pragma warning(pop)
161 |
162 | # define mad_f_mul mad_f_mul_inline
163 | # define mad_f_scale64
164 | # else
165 | /*
166 | * This Intel version is fast and accurate; the disposition of the least
167 | * significant bit depends on OPT_ACCURACY via mad_f_scale64().
168 | */
169 | # define MAD_F_MLX(hi, lo, x, y) \
170 | asm ("imull %3" \
171 | : "=a" (lo), "=d" (hi) \
172 | : "%a" (x), "rm" (y) \
173 | : "cc")
174 |
175 | # if defined(OPT_ACCURACY)
176 | /*
177 | * This gives best accuracy but is not very fast.
178 | */
179 | # define MAD_F_MLA(hi, lo, x, y) \
180 | ({ mad_fixed64hi_t __hi; \
181 | mad_fixed64lo_t __lo; \
182 | MAD_F_MLX(__hi, __lo, (x), (y)); \
183 | asm ("addl %2,%0\n\t" \
184 | "adcl %3,%1" \
185 | : "=rm" (lo), "=rm" (hi) \
186 | : "r" (__lo), "r" (__hi), "0" (lo), "1" (hi) \
187 | : "cc"); \
188 | })
189 | # endif /* OPT_ACCURACY */
190 |
191 | # if defined(OPT_ACCURACY)
192 | /*
193 | * Surprisingly, this is faster than SHRD followed by ADC.
194 | */
195 | # define mad_f_scale64(hi, lo) \
196 | ({ mad_fixed64hi_t __hi_; \
197 | mad_fixed64lo_t __lo_; \
198 | mad_fixed_t __result; \
199 | asm ("addl %4,%2\n\t" \
200 | "adcl %5,%3" \
201 | : "=rm" (__lo_), "=rm" (__hi_) \
202 | : "0" (lo), "1" (hi), \
203 | "ir" (1L << (MAD_F_SCALEBITS - 1)), "ir" (0) \
204 | : "cc"); \
205 | asm ("shrdl %3,%2,%1" \
206 | : "=rm" (__result) \
207 | : "0" (__lo_), "r" (__hi_), "I" (MAD_F_SCALEBITS) \
208 | : "cc"); \
209 | __result; \
210 | })
211 | # elif defined(OPT_INTEL)
212 | /*
213 | * Alternate Intel scaling that may or may not perform better.
214 | */
215 | # define mad_f_scale64(hi, lo) \
216 | ({ mad_fixed_t __result; \
217 | asm ("shrl %3,%1\n\t" \
218 | "shll %4,%2\n\t" \
219 | "orl %2,%1" \
220 | : "=rm" (__result) \
221 | : "0" (lo), "r" (hi), \
222 | "I" (MAD_F_SCALEBITS), "I" (32 - MAD_F_SCALEBITS) \
223 | : "cc"); \
224 | __result; \
225 | })
226 | # else
227 | # define mad_f_scale64(hi, lo) \
228 | ({ mad_fixed_t __result; \
229 | asm ("shrdl %3,%2,%1" \
230 | : "=rm" (__result) \
231 | : "0" (lo), "r" (hi), "I" (MAD_F_SCALEBITS) \
232 | : "cc"); \
233 | __result; \
234 | })
235 | # endif /* OPT_ACCURACY */
236 |
237 | # define MAD_F_SCALEBITS MAD_F_FRACBITS
238 | # endif
239 |
240 | /* --- ARM ----------------------------------------------------------------- */
241 |
242 | # elif defined(FPM_ARM)
243 |
244 | /*
245 | * This ARM V4 version is as accurate as FPM_64BIT but much faster. The
246 | * least significant bit is properly rounded at no CPU cycle cost!
247 | */
248 | # if 1
249 | /*
250 | * This is faster than the default implementation via MAD_F_MLX() and
251 | * mad_f_scale64().
252 | */
253 | # define mad_f_mul(x, y) \
254 | ({ mad_fixed64hi_t __hi; \
255 | mad_fixed64lo_t __lo; \
256 | mad_fixed_t __result; \
257 | asm ("smull %0, %1, %3, %4\n\t" \
258 | "movs %0, %0, lsr %5\n\t" \
259 | "adc %2, %0, %1, lsl %6" \
260 | : "=&r" (__lo), "=&r" (__hi), "=r" (__result) \
261 | : "%r" (x), "r" (y), \
262 | "M" (MAD_F_SCALEBITS), "M" (32 - MAD_F_SCALEBITS) \
263 | : "cc"); \
264 | __result; \
265 | })
266 | # endif
267 |
268 | # define MAD_F_MLX(hi, lo, x, y) \
269 | asm ("smull %0, %1, %2, %3" \
270 | : "=&r" (lo), "=&r" (hi) \
271 | : "%r" (x), "r" (y))
272 |
273 | # define MAD_F_MLA(hi, lo, x, y) \
274 | asm ("smlal %0, %1, %2, %3" \
275 | : "+r" (lo), "+r" (hi) \
276 | : "%r" (x), "r" (y))
277 |
278 | # define MAD_F_MLN(hi, lo) \
279 | asm ("rsbs %0, %2, #0\n\t" \
280 | "rsc %1, %3, #0" \
281 | : "=r" (lo), "=r" (hi) \
282 | : "0" (lo), "1" (hi) \
283 | : "cc")
284 |
285 | # define mad_f_scale64(hi, lo) \
286 | ({ mad_fixed_t __result; \
287 | asm ("movs %0, %1, lsr %3\n\t" \
288 | "adc %0, %0, %2, lsl %4" \
289 | : "=&r" (__result) \
290 | : "r" (lo), "r" (hi), \
291 | "M" (MAD_F_SCALEBITS), "M" (32 - MAD_F_SCALEBITS) \
292 | : "cc"); \
293 | __result; \
294 | })
295 |
296 | # define MAD_F_SCALEBITS MAD_F_FRACBITS
297 |
298 | /* --- MIPS ---------------------------------------------------------------- */
299 |
300 | # elif defined(FPM_MIPS)
301 |
302 | /*
303 | * This MIPS version is fast and accurate; the disposition of the least
304 | * significant bit depends on OPT_ACCURACY via mad_f_scale64().
305 | */
306 | # define MAD_F_MLX(hi, lo, x, y) \
307 | asm ("mult %2,%3" \
308 | : "=l" (lo), "=h" (hi) \
309 | : "%r" (x), "r" (y))
310 |
311 | # if defined(HAVE_MADD_ASM)
312 | # define MAD_F_MLA(hi, lo, x, y) \
313 | asm ("madd %2,%3" \
314 | : "+l" (lo), "+h" (hi) \
315 | : "%r" (x), "r" (y))
316 | # elif defined(HAVE_MADD16_ASM)
317 | /*
318 | * This loses significant accuracy due to the 16-bit integer limit in the
319 | * multiply/accumulate instruction.
320 | */
321 | # define MAD_F_ML0(hi, lo, x, y) \
322 | asm ("mult %2,%3" \
323 | : "=l" (lo), "=h" (hi) \
324 | : "%r" ((x) >> 12), "r" ((y) >> 16))
325 | # define MAD_F_MLA(hi, lo, x, y) \
326 | asm ("madd16 %2,%3" \
327 | : "+l" (lo), "+h" (hi) \
328 | : "%r" ((x) >> 12), "r" ((y) >> 16))
329 | # define MAD_F_MLZ(hi, lo) ((mad_fixed_t) (lo))
330 | # endif
331 |
332 | # if defined(OPT_SPEED)
333 | # define mad_f_scale64(hi, lo) \
334 | ((mad_fixed_t) ((hi) << (32 - MAD_F_SCALEBITS)))
335 | # define MAD_F_SCALEBITS MAD_F_FRACBITS
336 | # endif
337 |
338 | /* --- SPARC --------------------------------------------------------------- */
339 |
340 | # elif defined(FPM_SPARC)
341 |
342 | /*
343 | * This SPARC V8 version is fast and accurate; the disposition of the least
344 | * significant bit depends on OPT_ACCURACY via mad_f_scale64().
345 | */
346 | # define MAD_F_MLX(hi, lo, x, y) \
347 | asm ("smul %2, %3, %0\n\t" \
348 | "rd %%y, %1" \
349 | : "=r" (lo), "=r" (hi) \
350 | : "%r" (x), "rI" (y))
351 |
352 | /* --- PowerPC ------------------------------------------------------------- */
353 |
354 | # elif defined(FPM_PPC)
355 |
356 | /*
357 | * This PowerPC version is fast and accurate; the disposition of the least
358 | * significant bit depends on OPT_ACCURACY via mad_f_scale64().
359 | */
360 | # define MAD_F_MLX(hi, lo, x, y) \
361 | do { \
362 | asm ("mullw %0,%1,%2" \
363 | : "=r" (lo) \
364 | : "%r" (x), "r" (y)); \
365 | asm ("mulhw %0,%1,%2" \
366 | : "=r" (hi) \
367 | : "%r" (x), "r" (y)); \
368 | } \
369 | while (0)
370 |
371 | # if defined(OPT_ACCURACY)
372 | /*
373 | * This gives best accuracy but is not very fast.
374 | */
375 | # define MAD_F_MLA(hi, lo, x, y) \
376 | ({ mad_fixed64hi_t __hi; \
377 | mad_fixed64lo_t __lo; \
378 | MAD_F_MLX(__hi, __lo, (x), (y)); \
379 | asm ("addc %0,%2,%3\n\t" \
380 | "adde %1,%4,%5" \
381 | : "=r" (lo), "=r" (hi) \
382 | : "%r" (lo), "r" (__lo), \
383 | "%r" (hi), "r" (__hi) \
384 | : "xer"); \
385 | })
386 | # endif
387 |
388 | # if defined(OPT_ACCURACY)
389 | /*
390 | * This is slower than the truncating version below it.
391 | */
392 | # define mad_f_scale64(hi, lo) \
393 | ({ mad_fixed_t __result, __round; \
394 | asm ("rotrwi %0,%1,%2" \
395 | : "=r" (__result) \
396 | : "r" (lo), "i" (MAD_F_SCALEBITS)); \
397 | asm ("extrwi %0,%1,1,0" \
398 | : "=r" (__round) \
399 | : "r" (__result)); \
400 | asm ("insrwi %0,%1,%2,0" \
401 | : "+r" (__result) \
402 | : "r" (hi), "i" (MAD_F_SCALEBITS)); \
403 | asm ("add %0,%1,%2" \
404 | : "=r" (__result) \
405 | : "%r" (__result), "r" (__round)); \
406 | __result; \
407 | })
408 | # else
409 | # define mad_f_scale64(hi, lo) \
410 | ({ mad_fixed_t __result; \
411 | asm ("rotrwi %0,%1,%2" \
412 | : "=r" (__result) \
413 | : "r" (lo), "i" (MAD_F_SCALEBITS)); \
414 | asm ("insrwi %0,%1,%2,0" \
415 | : "+r" (__result) \
416 | : "r" (hi), "i" (MAD_F_SCALEBITS)); \
417 | __result; \
418 | })
419 | # endif
420 |
421 | # define MAD_F_SCALEBITS MAD_F_FRACBITS
422 |
423 | /* --- Default ------------------------------------------------------------- */
424 |
425 | # elif defined(FPM_DEFAULT)
426 |
427 | /*
428 | * This version is the most portable but it loses significant accuracy.
429 | * Furthermore, accuracy is biased against the second argument, so care
430 | * should be taken when ordering operands.
431 | *
432 | * The scale factors are constant as this is not used with SSO.
433 | *
434 | * Pre-rounding is required to stay within the limits of compliance.
435 | */
436 | # if defined(OPT_SPEED)
437 | # define mad_f_mul(x, y) (((x) >> 12) * ((y) >> 16))
438 | # else
439 | # define mad_f_mul(x, y) ((((x) + (1L << 11)) >> 12) * \
440 | (((y) + (1L << 15)) >> 16))
441 | # endif
442 |
443 | /* ------------------------------------------------------------------------- */
444 |
445 | # else
446 | # error "no FPM selected"
447 | # endif
448 |
449 | /* default implementations */
450 |
451 | # if !defined(mad_f_mul)
452 | # define mad_f_mul(x, y) \
453 | ({ register mad_fixed64hi_t __hi; \
454 | register mad_fixed64lo_t __lo; \
455 | MAD_F_MLX(__hi, __lo, (x), (y)); \
456 | mad_f_scale64(__hi, __lo); \
457 | })
458 | # endif
459 |
460 | # if !defined(MAD_F_MLA)
461 | # define MAD_F_ML0(hi, lo, x, y) ((lo) = mad_f_mul((x), (y)))
462 | # define MAD_F_MLA(hi, lo, x, y) ((lo) += mad_f_mul((x), (y)))
463 | # define MAD_F_MLN(hi, lo) ((lo) = -(lo))
464 | # define MAD_F_MLZ(hi, lo) ((void) (hi), (mad_fixed_t) (lo))
465 | # endif
466 |
467 | # if !defined(MAD_F_ML0)
468 | # define MAD_F_ML0(hi, lo, x, y) MAD_F_MLX((hi), (lo), (x), (y))
469 | # endif
470 |
471 | # if !defined(MAD_F_MLN)
472 | # define MAD_F_MLN(hi, lo) ((hi) = ((lo) = -(lo)) ? ~(hi) : -(hi))
473 | # endif
474 |
475 | # if !defined(MAD_F_MLZ)
476 | # define MAD_F_MLZ(hi, lo) mad_f_scale64((hi), (lo))
477 | # endif
478 |
479 | # if !defined(mad_f_scale64)
480 | # if defined(OPT_ACCURACY)
481 | # define mad_f_scale64(hi, lo) \
482 | ((((mad_fixed_t) \
483 | (((hi) << (32 - (MAD_F_SCALEBITS - 1))) | \
484 | ((lo) >> (MAD_F_SCALEBITS - 1)))) + 1) >> 1)
485 | # else
486 | # define mad_f_scale64(hi, lo) \
487 | ((mad_fixed_t) \
488 | (((hi) << (32 - MAD_F_SCALEBITS)) | \
489 | ((lo) >> MAD_F_SCALEBITS)))
490 | # endif
491 | # define MAD_F_SCALEBITS MAD_F_FRACBITS
492 | # endif
493 |
494 | /* C routines */
495 |
496 | mad_fixed_t mad_f_abs(mad_fixed_t);
497 | mad_fixed_t mad_f_div(mad_fixed_t, mad_fixed_t);
498 |
499 | # endif
500 |
--------------------------------------------------------------------------------
/frame.c:
--------------------------------------------------------------------------------
1 | /*
2 | * libmad - MPEG audio decoder library
3 | * Copyright (C) 2000-2004 Underbit Technologies, Inc.
4 | *
5 | * This program is free software; you can redistribute it and/or modify
6 | * it under the terms of the GNU General Public License as published by
7 | * the Free Software Foundation; either version 2 of the License, or
8 | * (at your option) any later version.
9 | *
10 | * This program is distributed in the hope that it will be useful,
11 | * but WITHOUT ANY WARRANTY; without even the implied warranty of
12 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13 | * GNU General Public License for more details.
14 | *
15 | * You should have received a copy of the GNU General Public License
16 | * along with this program; if not, write to the Free Software
17 | * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
18 | *
19 | * $Id: frame.c,v 1.29 2004/02/04 22:59:19 rob Exp $
20 | */
21 |
22 | #pragma GCC optimize ("O3")
23 |
24 | #include
25 | # include "config.h"
26 |
27 | # include "global.h"
28 |
29 | # include
30 |
31 | # include "bit.h"
32 | # include "stream.h"
33 | # include "frame.h"
34 | # include "timer.h"
35 | # include "layer3.h"
36 |
37 | static
38 | unsigned long const bitrate_table[5][15] PROGMEM = {
39 | /* MPEG-1 */
40 | { 0, 32000, 64000, 96000, 128000, 160000, 192000, 224000, /* Layer I */
41 | 256000, 288000, 320000, 352000, 384000, 416000, 448000 },
42 | { 0, 32000, 48000, 56000, 64000, 80000, 96000, 112000, /* Layer II */
43 | 128000, 160000, 192000, 224000, 256000, 320000, 384000 },
44 | { 0, 32000, 40000, 48000, 56000, 64000, 80000, 96000, /* Layer III */
45 | 112000, 128000, 160000, 192000, 224000, 256000, 320000 },
46 |
47 | /* MPEG-2 LSF */
48 | { 0, 32000, 48000, 56000, 64000, 80000, 96000, 112000, /* Layer I */
49 | 128000, 144000, 160000, 176000, 192000, 224000, 256000 },
50 | { 0, 8000, 16000, 24000, 32000, 40000, 48000, 56000, /* Layers */
51 | 64000, 80000, 96000, 112000, 128000, 144000, 160000 } /* II & III */
52 | };
53 |
54 | static
55 | unsigned int const samplerate_table[3] PROGMEM = { 44100, 48000, 32000 };
56 |
57 | static
58 | int (*const decoder_table[3])(struct mad_stream *, struct mad_frame *) = {
59 | NULL, //mad_layer_I,
60 | NULL, //mad_layer_II,
61 | mad_layer_III
62 | };
63 |
64 | /*
65 | * NAME: header->init()
66 | * DESCRIPTION: initialize header struct
67 | */
68 | void mad_header_init(struct mad_header *header)
69 | {
70 | header->layer = 0;
71 | header->mode = 0;
72 | header->mode_extension = 0;
73 | header->emphasis = 0;
74 |
75 | header->bitrate = 0;
76 | header->samplerate = 0;
77 |
78 | header->crc_check = 0;
79 | header->crc_target = 0;
80 |
81 | header->flags = 0;
82 | header->private_bits = 0;
83 |
84 | header->duration = mad_timer_zero;
85 | }
86 |
87 | /*
88 | * NAME: frame->init()
89 | * DESCRIPTION: initialize frame struct
90 | */
91 | void mad_frame_init(struct mad_frame *frame)
92 | {
93 | stack(__FUNCTION__,__FILE__,__LINE__);
94 | mad_header_init(&frame->header);
95 |
96 | frame->options = 0;
97 |
98 | frame->overlap = 0;
99 | mad_frame_mute(frame);
100 | }
101 |
102 | /*
103 | * NAME: frame->finish()
104 | * DESCRIPTION: deallocate any dynamic memory associated with frame
105 | */
106 | void mad_frame_finish(struct mad_frame *frame)
107 | {
108 | mad_header_finish(&frame->header);
109 |
110 | if (frame->overlap) {
111 | free(frame->overlap);
112 | frame->overlap = 0;
113 | }
114 | }
115 |
116 | /*
117 | * NAME: decode_header()
118 | * DESCRIPTION: read header data and following CRC word
119 | */
120 | static
121 | int decode_header(struct mad_header *header, struct mad_stream *stream)
122 | {
123 | unsigned int index;
124 |
125 | header->flags = 0;
126 | header->private_bits = 0;
127 |
128 | /* header() */
129 |
130 | /* syncword */
131 | mad_bit_skip(&stream->ptr, 11);
132 |
133 | /* MPEG 2.5 indicator (really part of syncword) */
134 | if (mad_bit_read(&stream->ptr, 1) == 0)
135 | header->flags |= MAD_FLAG_MPEG_2_5_EXT;
136 |
137 | /* ID */
138 | if (mad_bit_read(&stream->ptr, 1) == 0)
139 | header->flags |= MAD_FLAG_LSF_EXT;
140 | else if (header->flags & MAD_FLAG_MPEG_2_5_EXT) {
141 | stream->error = MAD_ERROR_LOSTSYNC;
142 | return -1;
143 | }
144 |
145 | /* layer */
146 | header->layer = 4 - mad_bit_read(&stream->ptr, 2);
147 |
148 | if (header->layer == 4) {
149 | stream->error = MAD_ERROR_BADLAYER;
150 | return -1;
151 | }
152 |
153 | /* protection_bit */
154 | if (mad_bit_read(&stream->ptr, 1) == 0) {
155 | header->flags |= MAD_FLAG_PROTECTION;
156 | header->crc_check = mad_bit_crc(stream->ptr, 16, 0xffff);
157 | }
158 |
159 | /* bitrate_index */
160 | index = mad_bit_read(&stream->ptr, 4);
161 |
162 | if (index == 15) {
163 | stream->error = MAD_ERROR_BADBITRATE;
164 | return -1;
165 | }
166 |
167 | if (header->flags & MAD_FLAG_LSF_EXT)
168 | header->bitrate = bitrate_table[3 + (header->layer >> 1)][index];
169 | else
170 | header->bitrate = bitrate_table[header->layer - 1][index];
171 |
172 | /* sampling_frequency */
173 | index = mad_bit_read(&stream->ptr, 2);
174 |
175 | if (index == 3) {
176 | stream->error = MAD_ERROR_BADSAMPLERATE;
177 | return -1;
178 | }
179 |
180 | header->samplerate = samplerate_table[index];
181 |
182 | if (header->flags & MAD_FLAG_LSF_EXT) {
183 | header->samplerate /= 2;
184 |
185 | if (header->flags & MAD_FLAG_MPEG_2_5_EXT)
186 | header->samplerate /= 2;
187 | }
188 |
189 | /* padding_bit */
190 | if (mad_bit_read(&stream->ptr, 1))
191 | header->flags |= MAD_FLAG_PADDING;
192 |
193 | /* private_bit */
194 | if (mad_bit_read(&stream->ptr, 1))
195 | header->private_bits |= MAD_PRIVATE_HEADER;
196 |
197 | /* mode */
198 | header->mode = 3 - mad_bit_read(&stream->ptr, 2);
199 |
200 | /* mode_extension */
201 | header->mode_extension = mad_bit_read(&stream->ptr, 2);
202 |
203 | /* copyright */
204 | if (mad_bit_read(&stream->ptr, 1))
205 | header->flags |= MAD_FLAG_COPYRIGHT;
206 |
207 | /* original/copy */
208 | if (mad_bit_read(&stream->ptr, 1))
209 | header->flags |= MAD_FLAG_ORIGINAL;
210 |
211 | /* emphasis */
212 | header->emphasis = mad_bit_read(&stream->ptr, 2);
213 |
214 | # if defined(OPT_STRICT)
215 | /*
216 | * ISO/IEC 11172-3 says this is a reserved emphasis value, but
217 | * streams exist which use it anyway. Since the value is not important
218 | * to the decoder proper, we allow it unless OPT_STRICT is defined.
219 | */
220 | if (header->emphasis == MAD_EMPHASIS_RESERVED) {
221 | stream->error = MAD_ERROR_BADEMPHASIS;
222 | return -1;
223 | }
224 | # endif
225 |
226 | /* error_check() */
227 |
228 | /* crc_check */
229 | if (header->flags & MAD_FLAG_PROTECTION)
230 | header->crc_target = mad_bit_read(&stream->ptr, 16);
231 |
232 | return 0;
233 | }
234 |
235 | /*
236 | * NAME: free_bitrate()
237 | * DESCRIPTION: attempt to discover the bitstream's free bitrate
238 | */
239 | static
240 | int free_bitrate(struct mad_stream *stream, struct mad_header const *header)
241 | {
242 | struct mad_bitptr keep_ptr;
243 | unsigned long rate = 0;
244 | unsigned int pad_slot, slots_per_frame;
245 | unsigned char const *ptr = 0;
246 |
247 | keep_ptr = stream->ptr;
248 |
249 | pad_slot = (header->flags & MAD_FLAG_PADDING) ? 1 : 0;
250 | slots_per_frame = (header->layer == MAD_LAYER_III &&
251 | (header->flags & MAD_FLAG_LSF_EXT)) ? 72 : 144;
252 |
253 | while (mad_stream_sync(stream) == 0) {
254 | struct mad_stream peek_stream;
255 | struct mad_header peek_header;
256 |
257 | peek_stream = *stream;
258 | peek_header = *header;
259 |
260 | if (decode_header(&peek_header, &peek_stream) == 0 &&
261 | peek_header.layer == header->layer &&
262 | peek_header.samplerate == header->samplerate) {
263 | unsigned int N;
264 |
265 | ptr = mad_bit_nextbyte(&stream->ptr);
266 |
267 | N = ptr - stream->this_frame;
268 |
269 | if (header->layer == MAD_LAYER_I) {
270 | rate = (unsigned long) header->samplerate *
271 | (N - 4 * pad_slot + 4) / 48 / 1000;
272 | }
273 | else {
274 | rate = (unsigned long) header->samplerate *
275 | (N - pad_slot + 1) / slots_per_frame / 1000;
276 | }
277 |
278 | if (rate >= 8)
279 | break;
280 | }
281 |
282 | mad_bit_skip(&stream->ptr, 8);
283 | }
284 |
285 | stream->ptr = keep_ptr;
286 |
287 | if (rate < 8 || (header->layer == MAD_LAYER_III && rate > 640)) {
288 | stream->error = MAD_ERROR_LOSTSYNC;
289 | return -1;
290 | }
291 |
292 | stream->freerate = rate * 1000;
293 |
294 | return 0;
295 | }
296 |
297 | /*
298 | * NAME: header->decode()
299 | * DESCRIPTION: read the next frame header from the stream
300 | */
301 | int mad_header_decode(struct mad_header *header, struct mad_stream *stream)
302 | {
303 | register unsigned char const *ptr, *end;
304 | unsigned int pad_slot, N;
305 |
306 | ptr = stream->next_frame;
307 | end = stream->bufend;
308 |
309 | if (ptr == 0) {
310 | stream->error = MAD_ERROR_BUFPTR;
311 | goto fail;
312 | }
313 |
314 | /* stream skip */
315 | if (stream->skiplen) {
316 | if (!stream->sync)
317 | ptr = stream->this_frame;
318 |
319 | if (end - ptr < stream->skiplen) {
320 | stream->skiplen -= end - ptr;
321 | stream->next_frame = end;
322 |
323 | stream->error = MAD_ERROR_BUFLEN;
324 | goto fail;
325 | }
326 |
327 | ptr += stream->skiplen;
328 | stream->skiplen = 0;
329 |
330 | stream->sync = 1;
331 | }
332 |
333 | sync:
334 | /* synchronize */
335 | if (stream->sync) {
336 | if (end - ptr < MAD_BUFFER_GUARD) {
337 | stream->next_frame = ptr;
338 |
339 | stream->error = MAD_ERROR_BUFLEN;
340 | goto fail;
341 | }
342 | else if (!(ptr[0] == 0xff && (ptr[1] & 0xe0) == 0xe0)) {
343 | /* mark point where frame sync word was expected */
344 | stream->this_frame = ptr;
345 | stream->next_frame = ptr + 1;
346 |
347 | stream->error = MAD_ERROR_LOSTSYNC;
348 | goto fail;
349 | }
350 | }
351 | else {
352 | mad_bit_init(&stream->ptr, ptr);
353 |
354 | if (mad_stream_sync(stream) == -1) {
355 | if (end - stream->next_frame >= MAD_BUFFER_GUARD)
356 | stream->next_frame = end - MAD_BUFFER_GUARD;
357 |
358 | stream->error = MAD_ERROR_BUFLEN;
359 | goto fail;
360 | }
361 |
362 | ptr = mad_bit_nextbyte(&stream->ptr);
363 | }
364 |
365 | /* begin processing */
366 | stream->this_frame = ptr;
367 | stream->next_frame = ptr + 1; /* possibly bogus sync word */
368 |
369 | mad_bit_init(&stream->ptr, stream->this_frame);
370 |
371 | if (decode_header(header, stream) == -1)
372 | goto fail;
373 |
374 | /* calculate frame duration */
375 | mad_timer_set(&header->duration, 0,
376 | 32 * MAD_NSBSAMPLES(header), header->samplerate);
377 |
378 | /* calculate free bit rate */
379 | if (header->bitrate == 0) {
380 | if ((stream->freerate == 0 || !stream->sync ||
381 | (header->layer == MAD_LAYER_III && stream->freerate > 640000)) &&
382 | free_bitrate(stream, header) == -1)
383 | goto fail;
384 |
385 | header->bitrate = stream->freerate;
386 | header->flags |= MAD_FLAG_FREEFORMAT;
387 | }
388 |
389 | /* calculate beginning of next frame */
390 | pad_slot = (header->flags & MAD_FLAG_PADDING) ? 1 : 0;
391 |
392 | if (header->layer == MAD_LAYER_I)
393 | N = ((12 * header->bitrate / header->samplerate) + pad_slot) * 4;
394 | else {
395 | unsigned int slots_per_frame;
396 |
397 | slots_per_frame = (header->layer == MAD_LAYER_III &&
398 | (header->flags & MAD_FLAG_LSF_EXT)) ? 72 : 144;
399 |
400 | N = (slots_per_frame * header->bitrate / header->samplerate) + pad_slot;
401 | }
402 |
403 | /* verify there is enough data left in buffer to decode this frame */
404 | if (N + MAD_BUFFER_GUARD > end - stream->this_frame) {
405 | stream->next_frame = stream->this_frame;
406 |
407 | stream->error = MAD_ERROR_BUFLEN;
408 | goto fail;
409 | }
410 |
411 | stream->next_frame = stream->this_frame + N;
412 |
413 | if (!stream->sync) {
414 | /* check that a valid frame header follows this frame */
415 |
416 | ptr = stream->next_frame;
417 | if (!(ptr[0] == 0xff && (ptr[1] & 0xe0) == 0xe0)) {
418 | ptr = stream->next_frame = stream->this_frame + 1;
419 | goto sync;
420 | }
421 |
422 | stream->sync = 1;
423 | }
424 |
425 | header->flags |= MAD_FLAG_INCOMPLETE;
426 |
427 | return 0;
428 |
429 | fail:
430 | stream->sync = 0;
431 |
432 | return -1;
433 | }
434 |
435 | /*
436 | * NAME: frame->decode()
437 | * DESCRIPTION: decode a single frame from a bitstream
438 | */
439 | int mad_frame_decode(struct mad_frame *frame, struct mad_stream *stream)
440 | {
441 | frame->options = stream->options;
442 |
443 | /* header() */
444 | /* error_check() */
445 |
446 | if (!(frame->header.flags & MAD_FLAG_INCOMPLETE) &&
447 | mad_header_decode(&frame->header, stream) == -1)
448 | goto fail;
449 |
450 | /* audio_data() */
451 |
452 | frame->header.flags &= ~MAD_FLAG_INCOMPLETE;
453 |
454 | if (decoder_table[frame->header.layer - 1](stream, frame) == -1) {
455 | if (!MAD_RECOVERABLE(stream->error))
456 | stream->next_frame = stream->this_frame;
457 |
458 | goto fail;
459 | }
460 |
461 | /* ancillary_data() */
462 |
463 | if (frame->header.layer != MAD_LAYER_III) {
464 | struct mad_bitptr next_frame;
465 |
466 | mad_bit_init(&next_frame, stream->next_frame);
467 |
468 | stream->anc_ptr = stream->ptr;
469 | stream->anc_bitlen = mad_bit_length(&stream->ptr, &next_frame);
470 |
471 | mad_bit_finish(&next_frame);
472 | }
473 |
474 | return 0;
475 |
476 | fail:
477 | stream->anc_bitlen = 0;
478 | return -1;
479 | }
480 |
481 | /*
482 | * NAME: frame->mute()
483 | * DESCRIPTION: zero all subband values so the frame becomes silent
484 | */
485 | void mad_frame_mute(struct mad_frame *frame)
486 | {
487 | unsigned int s, sb;
488 |
489 | for (s = 0; s < 36; ++s) {
490 | for (sb = 0; sb < 32; ++sb) {
491 | frame->sbsample[0][s][sb] =
492 | frame->sbsample[1][s][sb] = 0;
493 | }
494 | }
495 |
496 | if (frame->overlap) {
497 | for (s = 0; s < 18; ++s) {
498 | for (sb = 0; sb < 32; ++sb) {
499 | (*frame->overlap)[0][sb][s] =
500 | (*frame->overlap)[1][sb][s] = 0;
501 | }
502 | }
503 | }
504 | }
505 |
--------------------------------------------------------------------------------
/frame.h:
--------------------------------------------------------------------------------
1 | /*
2 | * libmad - MPEG audio decoder library
3 | * Copyright (C) 2000-2004 Underbit Technologies, Inc.
4 | *
5 | * This program is free software; you can redistribute it and/or modify
6 | * it under the terms of the GNU General Public License as published by
7 | * the Free Software Foundation; either version 2 of the License, or
8 | * (at your option) any later version.
9 | *
10 | * This program is distributed in the hope that it will be useful,
11 | * but WITHOUT ANY WARRANTY; without even the implied warranty of
12 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13 | * GNU General Public License for more details.
14 | *
15 | * You should have received a copy of the GNU General Public License
16 | * along with this program; if not, write to the Free Software
17 | * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
18 | *
19 | * $Id: frame.h,v 1.20 2004/01/23 09:41:32 rob Exp $
20 | */
21 |
22 | # ifndef LIBMAD_FRAME_H
23 | # define LIBMAD_FRAME_H
24 |
25 | # include "fixed.h"
26 | # include "timer.h"
27 | # include "stream.h"
28 |
29 | enum mad_layer {
30 | MAD_LAYER_I = 1, /* Layer I */
31 | MAD_LAYER_II = 2, /* Layer II */
32 | MAD_LAYER_III = 3 /* Layer III */
33 | };
34 |
35 | enum mad_mode {
36 | MAD_MODE_SINGLE_CHANNEL = 0, /* single channel */
37 | MAD_MODE_DUAL_CHANNEL = 1, /* dual channel */
38 | MAD_MODE_JOINT_STEREO = 2, /* joint (MS/intensity) stereo */
39 | MAD_MODE_STEREO = 3 /* normal LR stereo */
40 | };
41 |
42 | enum mad_emphasis {
43 | MAD_EMPHASIS_NONE = 0, /* no emphasis */
44 | MAD_EMPHASIS_50_15_US = 1, /* 50/15 microseconds emphasis */
45 | MAD_EMPHASIS_CCITT_J_17 = 3, /* CCITT J.17 emphasis */
46 | MAD_EMPHASIS_RESERVED = 2 /* unknown emphasis */
47 | };
48 |
49 | struct mad_header {
50 | enum mad_layer layer; /* audio layer (1, 2, or 3) */
51 | enum mad_mode mode; /* channel mode (see above) */
52 | int mode_extension; /* additional mode info */
53 | enum mad_emphasis emphasis; /* de-emphasis to use (see above) */
54 |
55 | unsigned long bitrate; /* stream bitrate (bps) */
56 | unsigned int samplerate; /* sampling frequency (Hz) */
57 |
58 | unsigned short crc_check; /* frame CRC accumulator */
59 | unsigned short crc_target; /* final target CRC checksum */
60 |
61 | int flags; /* flags (see below) */
62 | int private_bits; /* private bits (see below) */
63 |
64 | mad_timer_t duration; /* audio playing time of frame */
65 | };
66 |
67 | struct mad_frame {
68 | struct mad_header header; /* MPEG audio header */
69 |
70 | int options; /* decoding options (from stream) */
71 |
72 | mad_fixed_t sbsample[2][36][32]; /* synthesis subband filter samples */
73 | mad_fixed_t (*overlap)[2][32][18]; /* Layer III block overlap data */
74 | };
75 |
76 | # define MAD_NCHANNELS(header) ((header)->mode ? 2 : 1)
77 | # define MAD_NSBSAMPLES(header) \
78 | ((header)->layer == MAD_LAYER_I ? 12 : \
79 | (((header)->layer == MAD_LAYER_III && \
80 | ((header)->flags & MAD_FLAG_LSF_EXT)) ? 18 : 36))
81 |
82 | enum {
83 | MAD_FLAG_NPRIVATE_III = 0x0007, /* number of Layer III private bits */
84 | MAD_FLAG_INCOMPLETE = 0x0008, /* header but not data is decoded */
85 |
86 | MAD_FLAG_PROTECTION = 0x0010, /* frame has CRC protection */
87 | MAD_FLAG_COPYRIGHT = 0x0020, /* frame is copyright */
88 | MAD_FLAG_ORIGINAL = 0x0040, /* frame is original (else copy) */
89 | MAD_FLAG_PADDING = 0x0080, /* frame has additional slot */
90 |
91 | MAD_FLAG_I_STEREO = 0x0100, /* uses intensity joint stereo */
92 | MAD_FLAG_MS_STEREO = 0x0200, /* uses middle/side joint stereo */
93 | MAD_FLAG_FREEFORMAT = 0x0400, /* uses free format bitrate */
94 |
95 | MAD_FLAG_LSF_EXT = 0x1000, /* lower sampling freq. extension */
96 | MAD_FLAG_MC_EXT = 0x2000, /* multichannel audio extension */
97 | MAD_FLAG_MPEG_2_5_EXT = 0x4000 /* MPEG 2.5 (unofficial) extension */
98 | };
99 |
100 | enum {
101 | MAD_PRIVATE_HEADER = 0x0100, /* header private bit */
102 | MAD_PRIVATE_III = 0x001f /* Layer III private bits (up to 5) */
103 | };
104 |
105 | void mad_header_init(struct mad_header *);
106 |
107 | # define mad_header_finish(header) /* nothing */
108 |
109 | int mad_header_decode(struct mad_header *, struct mad_stream *);
110 |
111 | void mad_frame_init(struct mad_frame *);
112 | void mad_frame_finish(struct mad_frame *);
113 |
114 | int mad_frame_decode(struct mad_frame *, struct mad_stream *);
115 |
116 | void mad_frame_mute(struct mad_frame *);
117 |
118 | # endif
119 |
--------------------------------------------------------------------------------
/global.h:
--------------------------------------------------------------------------------
1 | /*
2 | * libmad - MPEG audio decoder library
3 | * Copyright (C) 2000-2004 Underbit Technologies, Inc.
4 | *
5 | * This program is free software; you can redistribute it and/or modify
6 | * it under the terms of the GNU General Public License as published by
7 | * the Free Software Foundation; either version 2 of the License, or
8 | * (at your option) any later version.
9 | *
10 | * This program is distributed in the hope that it will be useful,
11 | * but WITHOUT ANY WARRANTY; without even the implied warranty of
12 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13 | * GNU General Public License for more details.
14 | *
15 | * You should have received a copy of the GNU General Public License
16 | * along with this program; if not, write to the Free Software
17 | * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
18 | *
19 | * $Id: global.h,v 1.11 2004/01/23 09:41:32 rob Exp $
20 | */
21 |
22 | # ifndef LIBMAD_GLOBAL_H
23 | # define LIBMAD_GLOBAL_H
24 |
25 | /* conditional debugging */
26 |
27 | # if defined(DEBUG) && defined(NDEBUG)
28 | # error "cannot define both DEBUG and NDEBUG"
29 | # endif
30 |
31 | # if defined(DEBUG)
32 | # include
33 | # endif
34 |
35 | /* conditional features */
36 |
37 | # if defined(OPT_SPEED) && defined(OPT_ACCURACY)
38 | # error "cannot optimize for both speed and accuracy"
39 | # endif
40 |
41 | # if defined(OPT_SPEED) && !defined(OPT_SSO)
42 | # define OPT_SSO
43 | # endif
44 |
45 | # if defined(HAVE_UNISTD_H) && defined(HAVE_WAITPID) && \
46 | defined(HAVE_FCNTL) && defined(HAVE_PIPE) && defined(HAVE_FORK)
47 | # define USE_ASYNC
48 | # endif
49 |
50 | # if !defined(HAVE_ASSERT_H)
51 | # if defined(NDEBUG)
52 | # define assert(x) /* nothing */
53 | # else
54 | # define assert(x) do { if (!(x)) abort(); } while (0)
55 | # endif
56 | # endif
57 |
58 | # endif
59 |
--------------------------------------------------------------------------------
/huffman.h:
--------------------------------------------------------------------------------
1 | /*
2 | * libmad - MPEG audio decoder library
3 | * Copyright (C) 2000-2004 Underbit Technologies, Inc.
4 | *
5 | * This program is free software; you can redistribute it and/or modify
6 | * it under the terms of the GNU General Public License as published by
7 | * the Free Software Foundation; either version 2 of the License, or
8 | * (at your option) any later version.
9 | *
10 | * This program is distributed in the hope that it will be useful,
11 | * but WITHOUT ANY WARRANTY; without even the implied warranty of
12 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13 | * GNU General Public License for more details.
14 | *
15 | * You should have received a copy of the GNU General Public License
16 | * along with this program; if not, write to the Free Software
17 | * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
18 | *
19 | * $Id: huffman.h,v 1.11 2004/01/23 09:41:32 rob Exp $
20 | */
21 |
22 | # ifndef LIBMAD_HUFFMAN_H
23 | # define LIBMAD_HUFFMAN_H
24 |
25 | // Use ints instead of bitfields. This'll make the structure way larger, but allow
26 | // for easy direct access w/o any helper functions when placed in PROGMEM
27 |
28 | union huffquad {
29 | struct {
30 | unsigned int final;
31 | unsigned int bits;
32 | unsigned int offset;
33 | } ptr;
34 | struct {
35 | unsigned int final;
36 | unsigned int hlen;
37 | unsigned int v;
38 | unsigned int w;
39 | unsigned int x;
40 | unsigned int y;
41 | } value;
42 | unsigned int final ;
43 | };
44 |
45 | union huffpair {
46 | struct {
47 | unsigned int final;
48 | unsigned int bits;
49 | unsigned int offset;
50 | } ptr;
51 | struct {
52 | unsigned int final;
53 | unsigned int hlen;
54 | unsigned int x;
55 | unsigned int y;
56 | } value;
57 | unsigned int final;
58 | };
59 |
60 | struct hufftable {
61 | union huffpair const *table;
62 | unsigned int linbits;
63 | unsigned int startbits;
64 | };
65 |
66 | extern union huffquad const *const mad_huff_quad_table[2];
67 | extern struct hufftable const mad_huff_pair_table[32];
68 |
69 | # endif
70 |
--------------------------------------------------------------------------------
/imdct_s.dat.h:
--------------------------------------------------------------------------------
1 | /*
2 | * libmad - MPEG audio decoder library
3 | * Copyright (C) 2000-2004 Underbit Technologies, Inc.
4 | *
5 | * This program is free software; you can redistribute it and/or modify
6 | * it under the terms of the GNU General Public License as published by
7 | * the Free Software Foundation; either version 2 of the License, or
8 | * (at your option) any later version.
9 | *
10 | * This program is distributed in the hope that it will be useful,
11 | * but WITHOUT ANY WARRANTY; without even the implied warranty of
12 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13 | * GNU General Public License for more details.
14 | *
15 | * You should have received a copy of the GNU General Public License
16 | * along with this program; if not, write to the Free Software
17 | * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
18 | *
19 | * $Id: imdct_s.dat,v 1.8 2004/01/23 09:41:32 rob Exp $
20 | */
21 |
22 | /* 0 */ { MAD_F(0x09bd7ca0) /* 0.608761429 */,
23 | -MAD_F(0x0ec835e8) /* -0.923879533 */,
24 | -MAD_F(0x0216a2a2) /* -0.130526192 */,
25 | MAD_F(0x0fdcf549) /* 0.991444861 */,
26 | -MAD_F(0x061f78aa) /* -0.382683432 */,
27 | -MAD_F(0x0cb19346) /* -0.793353340 */ },
28 |
29 | /* 6 */ { -MAD_F(0x0cb19346) /* -0.793353340 */,
30 | MAD_F(0x061f78aa) /* 0.382683432 */,
31 | MAD_F(0x0fdcf549) /* 0.991444861 */,
32 | MAD_F(0x0216a2a2) /* 0.130526192 */,
33 | -MAD_F(0x0ec835e8) /* -0.923879533 */,
34 | -MAD_F(0x09bd7ca0) /* -0.608761429 */ },
35 |
36 | /* 1 */ { MAD_F(0x061f78aa) /* 0.382683432 */,
37 | -MAD_F(0x0ec835e8) /* -0.923879533 */,
38 | MAD_F(0x0ec835e8) /* 0.923879533 */,
39 | -MAD_F(0x061f78aa) /* -0.382683432 */,
40 | -MAD_F(0x061f78aa) /* -0.382683432 */,
41 | MAD_F(0x0ec835e8) /* 0.923879533 */ },
42 |
43 | /* 7 */ { -MAD_F(0x0ec835e8) /* -0.923879533 */,
44 | -MAD_F(0x061f78aa) /* -0.382683432 */,
45 | MAD_F(0x061f78aa) /* 0.382683432 */,
46 | MAD_F(0x0ec835e8) /* 0.923879533 */,
47 | MAD_F(0x0ec835e8) /* 0.923879533 */,
48 | MAD_F(0x061f78aa) /* 0.382683432 */ },
49 |
50 | /* 2 */ { MAD_F(0x0216a2a2) /* 0.130526192 */,
51 | -MAD_F(0x061f78aa) /* -0.382683432 */,
52 | MAD_F(0x09bd7ca0) /* 0.608761429 */,
53 | -MAD_F(0x0cb19346) /* -0.793353340 */,
54 | MAD_F(0x0ec835e8) /* 0.923879533 */,
55 | -MAD_F(0x0fdcf549) /* -0.991444861 */ },
56 |
57 | /* 8 */ { -MAD_F(0x0fdcf549) /* -0.991444861 */,
58 | -MAD_F(0x0ec835e8) /* -0.923879533 */,
59 | -MAD_F(0x0cb19346) /* -0.793353340 */,
60 | -MAD_F(0x09bd7ca0) /* -0.608761429 */,
61 | -MAD_F(0x061f78aa) /* -0.382683432 */,
62 | -MAD_F(0x0216a2a2) /* -0.130526192 */ }
63 |
--------------------------------------------------------------------------------
/layer3.h:
--------------------------------------------------------------------------------
1 | /*
2 | * libmad - MPEG audio decoder library
3 | * Copyright (C) 2000-2004 Underbit Technologies, Inc.
4 | *
5 | * This program is free software; you can redistribute it and/or modify
6 | * it under the terms of the GNU General Public License as published by
7 | * the Free Software Foundation; either version 2 of the License, or
8 | * (at your option) any later version.
9 | *
10 | * This program is distributed in the hope that it will be useful,
11 | * but WITHOUT ANY WARRANTY; without even the implied warranty of
12 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13 | * GNU General Public License for more details.
14 | *
15 | * You should have received a copy of the GNU General Public License
16 | * along with this program; if not, write to the Free Software
17 | * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
18 | *
19 | * $Id: layer3.h,v 1.10 2004/01/23 09:41:32 rob Exp $
20 | */
21 |
22 | # ifndef LIBMAD_LAYER3_H
23 | # define LIBMAD_LAYER3_H
24 |
25 | # include "stream.h"
26 | # include "frame.h"
27 |
28 | int mad_layer_III(struct mad_stream *, struct mad_frame *);
29 |
30 | # endif
31 |
--------------------------------------------------------------------------------
/libmad-8266.ino:
--------------------------------------------------------------------------------
1 | #include
2 | #include
3 | //#include "config.h"
4 | //#include "mad.h"
5 | #include "AudioFileSourceSPIFFS.h"
6 | #include "AudioGeneratorMP3.h"
7 | #include "AudioOutputI2SDAC.h"
8 | #include "AudioOutputI2SNoDAC.h"
9 |
10 | #undef stack
11 | extern "C" {
12 | #include
13 | extern cont_t g_cont;
14 |
15 | void stack(const char *s, const char *t, int i) {
16 | register uint32_t *sp asm("a1");
17 | int freestack = 4 * (sp - g_cont.stack);
18 | int freeheap = ESP.getFreeHeap();
19 | static int laststack, lastheap;
20 | if (laststack!=freestack|| lastheap !=freeheap)
21 | Serial.printf("%s: FREESTACK=%d, FREEHEAP=%d\n", s, /*t, i,*/ freestack, /*cont_get_free_stack(&g_cont),*/ freeheap); Serial.flush();
22 | if (freestack < 256) {Serial.printf("out of stack!\n"); while (1); }
23 | if (freeheap < 1024) {Serial.printf("out of heap!\n"); while (1); }
24 | laststack=freestack;lastheap=freeheap;
25 | }
26 | }
27 | #if 0
28 | /*
29 | * This is a private message structure. A generic pointer to this structure
30 | * is passed to each of the callback functions. Put here any data you need
31 | * to access from within the callbacks.
32 | */
33 |
34 | class Buffer
35 | {
36 | public:
37 | Buffer() { lastRate=0; lastChannels=0; lastReadPos = 0;}
38 | unsigned char space[2048];
39 | AudioFileSourceSPIFFS file;
40 | AudioOutputI2SNoDAC i2sdac;
41 | int lastRate;
42 | int lastChannels;
43 | int lastReadPos;
44 | };
45 |
46 | /*
47 | * This is the input callback. The purpose of this callback is to (re)fill
48 | * the stream buffer which is to be decoded. In this example, an entire file
49 | * has been mapped into memory, so we just call mad_stream_buffer() with the
50 | * address and length of the mapping. When this callback is called a second
51 | * time, we are finished decoding.
52 | */
53 | static enum mad_flow input(void *data,
54 | struct mad_stream *stream)
55 | {
56 | Buffer *buffer = reinterpret_cast(data);
57 |
58 | int unused = 0;
59 | if (stream->next_frame) {
60 | unused = sizeof(buffer->space) - (stream->next_frame - buffer->space);
61 | memmove(buffer->space, stream->next_frame, unused);
62 | }
63 | if (unused == sizeof(buffer->space))
64 | return MAD_FLOW_STOP;
65 |
66 | Serial.printf("unused=%d, fpos=%d\n", unused, buffer->file.getPos());
67 | buffer->lastReadPos = buffer->file.getPos() - unused;
68 | int len = sizeof(buffer->space) - unused;
69 | len = buffer->file.read(buffer->space + unused, len);
70 | if (len == 0) return MAD_FLOW_STOP;
71 |
72 | mad_stream_buffer(stream, buffer->space, len + unused);
73 |
74 | return MAD_FLOW_CONTINUE;
75 | }
76 |
77 | /*
78 | * The following utility routine performs simple rounding, clipping, and
79 | * scaling of MAD's high-resolution samples down to 16 bits. It does not
80 | * perform any dithering or noise shaping, which would be recommended to
81 | * obtain any exceptional audio quality. It is therefore not recommended to
82 | * use this routine if high-quality output is desired.
83 | */
84 |
85 |
86 | /*
87 | * This is the output callback function. It is called after each frame of
88 | * MPEG audio data has been completely decoded. The purpose of this callback
89 | * is to output (or play) the decoded PCM audio.
90 | */
91 |
92 | static enum mad_flow output(void *data,
93 | struct mad_header const *header,
94 | struct mad_pcm *pcm)
95 | {
96 | unsigned int nchannels, nsamples;
97 | int16_t const *left_ch, *right_ch;
98 | Buffer *buffer = reinterpret_cast(data);
99 |
100 | /* pcm->samplerate contains the sampling frequency */
101 | if (pcm->samplerate != buffer->lastRate) {
102 | stack(__FUNCTION__, __FILE__, __LINE__);
103 | buffer->i2sdac.SetRate(pcm->samplerate);
104 | buffer->lastRate = pcm->samplerate;
105 | Serial.printf("Setting sample rate: %d\n", pcm->samplerate);
106 | }
107 | if (pcm->channels != buffer->lastChannels) {
108 | buffer->i2sdac.SetChannels(pcm->channels);
109 | buffer->lastChannels = pcm->channels;
110 | Serial.printf("Setting channels: %d\n", pcm->channels);
111 | }
112 | nchannels = pcm->channels;
113 | nsamples = pcm->length;
114 | left_ch = pcm->samples[0];
115 | right_ch = pcm->samples[1];
116 |
117 | while (nsamples--) {
118 | int16_t sample[2];
119 | sample[AudioOutput::LEFTCHANNEL] = *(left_ch++);
120 | sample[AudioOutput::RIGHTCHANNEL] = *(right_ch++);
121 | while (!buffer->i2sdac.ConsumeSample(sample)) {yield();};
122 | }
123 |
124 | return MAD_FLOW_CONTINUE;
125 | }
126 |
127 | /*
128 | * This is the error callback function. It is called whenever a decoding
129 | * error occurs. The error is indicated by stream->error; the list of
130 | * possible MAD_ERROR_* errors can be found in the mad.h (or stream.h)
131 | * header file.
132 | */
133 |
134 | static enum mad_flow error(void *data,
135 | struct mad_stream *stream,
136 | struct mad_frame *frame)
137 | {
138 | Buffer *buffer = reinterpret_cast(data);
139 |
140 | char err[64];
141 | strcpy_P(err, mad_stream_errorstr(stream));
142 | Serial.printf("Decoding error 0x%04x (%s) at byte offset %p\n", stream->error, err, (stream->this_frame - buffer->space) + buffer->lastReadPos);
143 | Serial.flush();
144 | /* return MAD_FLOW_BREAK here to stop decoding (and propagate an error) */
145 |
146 | return MAD_FLOW_CONTINUE;
147 | }
148 |
149 | /*
150 | * This is the function called by main() above to perform all the decoding.
151 | * It instantiates a decoder object and configures it with the input,
152 | * output, and error callback functions above. A single call to
153 | * mad_decoder_run() continues until a callback function returns
154 | * MAD_FLOW_STOP (to stop decoding) or MAD_FLOW_BREAK (to stop decoding and
155 | * signal an error).
156 | */
157 |
158 | static int decode()
159 | {
160 | Buffer *buffer = new Buffer;
161 | struct mad_decoder decoder;
162 | int result;
163 |
164 | /* initialize our private message structure */
165 | buffer->file.open("/jamonit.mp3");
166 | buffer->i2sdac.begin();
167 | buffer->i2sdac.SetBitsPerSample(16);
168 | buffer->i2sdac.SetOversampling(32);
169 |
170 | /* configure input, output, and error functions */
171 |
172 | mad_decoder_init(&decoder, buffer,
173 | input, 0 /* header */, 0 /* filter */, output,
174 | error, 0 /* message */);
175 |
176 | /* start decoding */
177 |
178 | result = mad_decoder_run(&decoder, MAD_DECODER_MODE_SYNC);
179 |
180 | /* release the decoder */
181 |
182 | mad_decoder_finish(&decoder);
183 |
184 | return result;
185 | }
186 |
187 | void setup()
188 | {
189 | WiFi.forceSleepBegin();
190 | Serial.begin(115200);
191 | delay(1000);
192 | Serial.println("Begin recording...");
193 | delay(1000);
194 | stack(__FUNCTION__, __FILE__, __LINE__);
195 | decode();
196 | }
197 |
198 | void loop()
199 | {
200 | Serial.println("mp3 done");
201 | }
202 | #else
203 | AudioGeneratorMP3 *mp3;
204 | AudioFileSourceSPIFFS *file;
205 | AudioOutputI2SNoDAC *out;
206 | void setup()
207 | {
208 | WiFi.forceSleepBegin();
209 | Serial.begin(115200);
210 | delay(1000);
211 | Serial.println("Begin recording...");
212 | delay(1000);
213 | stack(__FUNCTION__, __FILE__, __LINE__);
214 | SPIFFS.begin();
215 | file = new AudioFileSourceSPIFFS("/jamonit.mp3");
216 | out = new AudioOutputI2SNoDAC();
217 | mp3 = new AudioGeneratorMP3();
218 | mp3->begin(file, out);
219 | }
220 |
221 | void loop()
222 | {
223 | if (mp3->isRunning()) mp3->loop();
224 | else {Serial.println("mp3 done\n"); delay(1000); }
225 | }
226 |
227 | #endif
228 |
--------------------------------------------------------------------------------
/mad.h.sed:
--------------------------------------------------------------------------------
1 | #
2 | # libmad - MPEG audio decoder library
3 | # Copyright (C) 2000-2004 Underbit Technologies, Inc.
4 | #
5 | # This program is free software; you can redistribute it and/or modify
6 | # it under the terms of the GNU General Public License as published by
7 | # the Free Software Foundation; either version 2 of the License, or
8 | # (at your option) any later version.
9 | #
10 | # This program is distributed in the hope that it will be useful,
11 | # but WITHOUT ANY WARRANTY; without even the implied warranty of
12 | # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13 | # GNU General Public License for more details.
14 | #
15 | # You should have received a copy of the GNU General Public License
16 | # along with this program; if not, write to the Free Software
17 | # Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
18 | #
19 | # $Id: mad.h.sed,v 1.9 2004/01/23 09:41:32 rob Exp $
20 | #
21 |
22 | /^\/\*$/{
23 | N
24 | s/ \* libmad - /&/
25 | t copy
26 | b next
27 | : copy
28 | g
29 | n
30 | s|^ \* \$\(Id: .*\) \$$|/* \1 */|p
31 | /^ \*\/$/d
32 | b copy
33 | }
34 | /^# *include "/d
35 | : next
36 | p
37 |
--------------------------------------------------------------------------------
/qc_table.dat.h:
--------------------------------------------------------------------------------
1 | /*
2 | * libmad - MPEG audio decoder library
3 | * Copyright (C) 2000-2004 Underbit Technologies, Inc.
4 | *
5 | * This program is free software; you can redistribute it and/or modify
6 | * it under the terms of the GNU General Public License as published by
7 | * the Free Software Foundation; either version 2 of the License, or
8 | * (at your option) any later version.
9 | *
10 | * This program is distributed in the hope that it will be useful,
11 | * but WITHOUT ANY WARRANTY; without even the implied warranty of
12 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13 | * GNU General Public License for more details.
14 | *
15 | * You should have received a copy of the GNU General Public License
16 | * along with this program; if not, write to the Free Software
17 | * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
18 | *
19 | * $Id: qc_table.dat,v 1.7 2004/01/23 09:41:32 rob Exp $
20 | */
21 |
22 | /*
23 | * These are the Layer II classes of quantization.
24 | * The table is derived from Table B.4 of ISO/IEC 11172-3.
25 | */
26 |
27 | { 3, 2, 5,
28 | MAD_F(0x15555555) /* 1.33333333333 => 1.33333333209, e 0.00000000124 */,
29 | MAD_F(0x08000000) /* 0.50000000000 => 0.50000000000, e 0.00000000000 */ },
30 | { 5, 3, 7,
31 | MAD_F(0x1999999a) /* 1.60000000000 => 1.60000000149, e -0.00000000149 */,
32 | MAD_F(0x08000000) /* 0.50000000000 => 0.50000000000, e 0.00000000000 */ },
33 | { 7, 0, 3,
34 | MAD_F(0x12492492) /* 1.14285714286 => 1.14285714179, e 0.00000000107 */,
35 | MAD_F(0x04000000) /* 0.25000000000 => 0.25000000000, e 0.00000000000 */ },
36 | { 9, 4, 10,
37 | MAD_F(0x1c71c71c) /* 1.77777777777 => 1.77777777612, e 0.00000000165 */,
38 | MAD_F(0x08000000) /* 0.50000000000 => 0.50000000000, e 0.00000000000 */ },
39 | { 15, 0, 4,
40 | MAD_F(0x11111111) /* 1.06666666666 => 1.06666666642, e 0.00000000024 */,
41 | MAD_F(0x02000000) /* 0.12500000000 => 0.12500000000, e 0.00000000000 */ },
42 | { 31, 0, 5,
43 | MAD_F(0x10842108) /* 1.03225806452 => 1.03225806355, e 0.00000000097 */,
44 | MAD_F(0x01000000) /* 0.06250000000 => 0.06250000000, e 0.00000000000 */ },
45 | { 63, 0, 6,
46 | MAD_F(0x10410410) /* 1.01587301587 => 1.01587301493, e 0.00000000094 */,
47 | MAD_F(0x00800000) /* 0.03125000000 => 0.03125000000, e 0.00000000000 */ },
48 | { 127, 0, 7,
49 | MAD_F(0x10204081) /* 1.00787401575 => 1.00787401572, e 0.00000000003 */,
50 | MAD_F(0x00400000) /* 0.01562500000 => 0.01562500000, e 0.00000000000 */ },
51 | { 255, 0, 8,
52 | MAD_F(0x10101010) /* 1.00392156863 => 1.00392156839, e 0.00000000024 */,
53 | MAD_F(0x00200000) /* 0.00781250000 => 0.00781250000, e 0.00000000000 */ },
54 | { 511, 0, 9,
55 | MAD_F(0x10080402) /* 1.00195694716 => 1.00195694715, e 0.00000000001 */,
56 | MAD_F(0x00100000) /* 0.00390625000 => 0.00390625000, e 0.00000000000 */ },
57 | { 1023, 0, 10,
58 | MAD_F(0x10040100) /* 1.00097751711 => 1.00097751617, e 0.00000000094 */,
59 | MAD_F(0x00080000) /* 0.00195312500 => 0.00195312500, e 0.00000000000 */ },
60 | { 2047, 0, 11,
61 | MAD_F(0x10020040) /* 1.00048851979 => 1.00048851967, e 0.00000000012 */,
62 | MAD_F(0x00040000) /* 0.00097656250 => 0.00097656250, e 0.00000000000 */ },
63 | { 4095, 0, 12,
64 | MAD_F(0x10010010) /* 1.00024420024 => 1.00024420023, e 0.00000000001 */,
65 | MAD_F(0x00020000) /* 0.00048828125 => 0.00048828125, e 0.00000000000 */ },
66 | { 8191, 0, 13,
67 | MAD_F(0x10008004) /* 1.00012208522 => 1.00012208521, e 0.00000000001 */,
68 | MAD_F(0x00010000) /* 0.00024414063 => 0.00024414062, e 0.00000000000 */ },
69 | { 16383, 0, 14,
70 | MAD_F(0x10004001) /* 1.00006103888 => 1.00006103888, e -0.00000000000 */,
71 | MAD_F(0x00008000) /* 0.00012207031 => 0.00012207031, e -0.00000000000 */ },
72 | { 32767, 0, 15,
73 | MAD_F(0x10002000) /* 1.00003051851 => 1.00003051758, e 0.00000000093 */,
74 | MAD_F(0x00004000) /* 0.00006103516 => 0.00006103516, e 0.00000000000 */ },
75 | { 65535, 0, 16,
76 | MAD_F(0x10001000) /* 1.00001525902 => 1.00001525879, e 0.00000000023 */,
77 | MAD_F(0x00002000) /* 0.00003051758 => 0.00003051758, e 0.00000000000 */ }
78 |
--------------------------------------------------------------------------------
/sf_table.dat.h:
--------------------------------------------------------------------------------
1 | /*
2 | * libmad - MPEG audio decoder library
3 | * Copyright (C) 2000-2004 Underbit Technologies, Inc.
4 | *
5 | * This program is free software; you can redistribute it and/or modify
6 | * it under the terms of the GNU General Public License as published by
7 | * the Free Software Foundation; either version 2 of the License, or
8 | * (at your option) any later version.
9 | *
10 | * This program is distributed in the hope that it will be useful,
11 | * but WITHOUT ANY WARRANTY; without even the implied warranty of
12 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13 | * GNU General Public License for more details.
14 | *
15 | * You should have received a copy of the GNU General Public License
16 | * along with this program; if not, write to the Free Software
17 | * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
18 | *
19 | * $Id: sf_table.dat,v 1.7 2004/01/23 09:41:33 rob Exp $
20 | */
21 |
22 | /*
23 | * These are the scalefactor values for Layer I and Layer II.
24 | * The values are from Table B.1 of ISO/IEC 11172-3.
25 | *
26 | * There is some error introduced by the 32-bit fixed-point representation;
27 | * the amount of error is shown. For 16-bit PCM output, this shouldn't be
28 | * too much of a problem.
29 | *
30 | * Strictly speaking, Table B.1 has only 63 entries (0-62), thus a strict
31 | * interpretation of ISO/IEC 11172-3 would suggest that a scalefactor index of
32 | * 63 is invalid. However, for better compatibility with current practices, we
33 | * add a 64th entry.
34 | */
35 |
36 | MAD_F(0x20000000), /* 2.000000000000 => 2.000000000000, e 0.000000000000 */
37 | MAD_F(0x1965fea5), /* 1.587401051968 => 1.587401051074, e 0.000000000894 */
38 | MAD_F(0x1428a2fa), /* 1.259921049895 => 1.259921051562, e -0.000000001667 */
39 | MAD_F(0x10000000), /* 1.000000000000 => 1.000000000000, e 0.000000000000 */
40 | MAD_F(0x0cb2ff53), /* 0.793700525984 => 0.793700527400, e -0.000000001416 */
41 | MAD_F(0x0a14517d), /* 0.629960524947 => 0.629960525781, e -0.000000000833 */
42 | MAD_F(0x08000000), /* 0.500000000000 => 0.500000000000, e 0.000000000000 */
43 | MAD_F(0x06597fa9), /* 0.396850262992 => 0.396850261837, e 0.000000001155 */
44 |
45 | MAD_F(0x050a28be), /* 0.314980262474 => 0.314980261028, e 0.000000001446 */
46 | MAD_F(0x04000000), /* 0.250000000000 => 0.250000000000, e 0.000000000000 */
47 | MAD_F(0x032cbfd5), /* 0.198425131496 => 0.198425132781, e -0.000000001285 */
48 | MAD_F(0x0285145f), /* 0.157490131237 => 0.157490130514, e 0.000000000723 */
49 | MAD_F(0x02000000), /* 0.125000000000 => 0.125000000000, e 0.000000000000 */
50 | MAD_F(0x01965fea), /* 0.099212565748 => 0.099212564528, e 0.000000001220 */
51 | MAD_F(0x01428a30), /* 0.078745065618 => 0.078745067120, e -0.000000001501 */
52 | MAD_F(0x01000000), /* 0.062500000000 => 0.062500000000, e 0.000000000000 */
53 |
54 | MAD_F(0x00cb2ff5), /* 0.049606282874 => 0.049606282264, e 0.000000000610 */
55 | MAD_F(0x00a14518), /* 0.039372532809 => 0.039372533560, e -0.000000000751 */
56 | MAD_F(0x00800000), /* 0.031250000000 => 0.031250000000, e 0.000000000000 */
57 | MAD_F(0x006597fb), /* 0.024803141437 => 0.024803142995, e -0.000000001558 */
58 | MAD_F(0x0050a28c), /* 0.019686266405 => 0.019686266780, e -0.000000000375 */
59 | MAD_F(0x00400000), /* 0.015625000000 => 0.015625000000, e 0.000000000000 */
60 | MAD_F(0x0032cbfd), /* 0.012401570719 => 0.012401569635, e 0.000000001084 */
61 | MAD_F(0x00285146), /* 0.009843133202 => 0.009843133390, e -0.000000000188 */
62 |
63 | MAD_F(0x00200000), /* 0.007812500000 => 0.007812500000, e 0.000000000000 */
64 | MAD_F(0x001965ff), /* 0.006200785359 => 0.006200786680, e -0.000000001321 */
65 | MAD_F(0x001428a3), /* 0.004921566601 => 0.004921566695, e -0.000000000094 */
66 | MAD_F(0x00100000), /* 0.003906250000 => 0.003906250000, e 0.000000000000 */
67 | MAD_F(0x000cb2ff), /* 0.003100392680 => 0.003100391477, e 0.000000001202 */
68 | MAD_F(0x000a1451), /* 0.002460783301 => 0.002460781485, e 0.000000001816 */
69 | MAD_F(0x00080000), /* 0.001953125000 => 0.001953125000, e 0.000000000000 */
70 | MAD_F(0x00065980), /* 0.001550196340 => 0.001550197601, e -0.000000001262 */
71 |
72 | MAD_F(0x00050a29), /* 0.001230391650 => 0.001230392605, e -0.000000000955 */
73 | MAD_F(0x00040000), /* 0.000976562500 => 0.000976562500, e 0.000000000000 */
74 | MAD_F(0x00032cc0), /* 0.000775098170 => 0.000775098801, e -0.000000000631 */
75 | MAD_F(0x00028514), /* 0.000615195825 => 0.000615194440, e 0.000000001385 */
76 | MAD_F(0x00020000), /* 0.000488281250 => 0.000488281250, e 0.000000000000 */
77 | MAD_F(0x00019660), /* 0.000387549085 => 0.000387549400, e -0.000000000315 */
78 | MAD_F(0x0001428a), /* 0.000307597913 => 0.000307597220, e 0.000000000693 */
79 | MAD_F(0x00010000), /* 0.000244140625 => 0.000244140625, e 0.000000000000 */
80 |
81 | MAD_F(0x0000cb30), /* 0.000193774542 => 0.000193774700, e -0.000000000158 */
82 | MAD_F(0x0000a145), /* 0.000153798956 => 0.000153798610, e 0.000000000346 */
83 | MAD_F(0x00008000), /* 0.000122070313 => 0.000122070313, e 0.000000000000 */
84 | MAD_F(0x00006598), /* 0.000096887271 => 0.000096887350, e -0.000000000079 */
85 | MAD_F(0x000050a3), /* 0.000076899478 => 0.000076901168, e -0.000000001689 */
86 | MAD_F(0x00004000), /* 0.000061035156 => 0.000061035156, e 0.000000000000 */
87 | MAD_F(0x000032cc), /* 0.000048443636 => 0.000048443675, e -0.000000000039 */
88 | MAD_F(0x00002851), /* 0.000038449739 => 0.000038448721, e 0.000000001018 */
89 |
90 | MAD_F(0x00002000), /* 0.000030517578 => 0.000030517578, e 0.000000000000 */
91 | MAD_F(0x00001966), /* 0.000024221818 => 0.000024221838, e -0.000000000020 */
92 | MAD_F(0x00001429), /* 0.000019224870 => 0.000019226223, e -0.000000001354 */
93 | MAD_F(0x00001000), /* 0.000015258789 => 0.000015258789, e -0.000000000000 */
94 | MAD_F(0x00000cb3), /* 0.000012110909 => 0.000012110919, e -0.000000000010 */
95 | MAD_F(0x00000a14), /* 0.000009612435 => 0.000009611249, e 0.000000001186 */
96 | MAD_F(0x00000800), /* 0.000007629395 => 0.000007629395, e -0.000000000000 */
97 | MAD_F(0x00000659), /* 0.000006055454 => 0.000006053597, e 0.000000001858 */
98 |
99 | MAD_F(0x0000050a), /* 0.000004806217 => 0.000004805624, e 0.000000000593 */
100 | MAD_F(0x00000400), /* 0.000003814697 => 0.000003814697, e 0.000000000000 */
101 | MAD_F(0x0000032d), /* 0.000003027727 => 0.000003028661, e -0.000000000934 */
102 | MAD_F(0x00000285), /* 0.000002403109 => 0.000002402812, e 0.000000000296 */
103 | MAD_F(0x00000200), /* 0.000001907349 => 0.000001907349, e -0.000000000000 */
104 | MAD_F(0x00000196), /* 0.000001513864 => 0.000001512468, e 0.000000001396 */
105 | MAD_F(0x00000143), /* 0.000001201554 => 0.000001203269, e -0.000000001714 */
106 | MAD_F(0x00000000) /* this compatibility entry is not part of Table B.1 */
107 |
--------------------------------------------------------------------------------
/stream.c:
--------------------------------------------------------------------------------
1 | /*
2 | * libmad - MPEG audio decoder library
3 | * Copyright (C) 2000-2004 Underbit Technologies, Inc.
4 | *
5 | * This program is free software; you can redistribute it and/or modify
6 | * it under the terms of the GNU General Public License as published by
7 | * the Free Software Foundation; either version 2 of the License, or
8 | * (at your option) any later version.
9 | *
10 | * This program is distributed in the hope that it will be useful,
11 | * but WITHOUT ANY WARRANTY; without even the implied warranty of
12 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13 | * GNU General Public License for more details.
14 | *
15 | * You should have received a copy of the GNU General Public License
16 | * along with this program; if not, write to the Free Software
17 | * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
18 | *
19 | * $Id: stream.c,v 1.12 2004/02/05 09:02:39 rob Exp $
20 | */
21 |
22 | #pragma GCC optimize ("O3")
23 |
24 | #include
25 | # include "config.h"
26 |
27 | # include "global.h"
28 |
29 | # include
30 |
31 | # include "bit.h"
32 | # include "stream.h"
33 |
34 | /*
35 | * NAME: stream->init()
36 | * DESCRIPTION: initialize stream struct
37 | */
38 | void mad_stream_init(struct mad_stream *stream)
39 | {
40 | stack(__FUNCTION__, __FILE__, __LINE__);
41 | stream->buffer = 0;
42 | stream->bufend = 0;
43 | stream->skiplen = 0;
44 |
45 | stream->sync = 0;
46 | stream->freerate = 0;
47 |
48 | stream->this_frame = 0;
49 | stream->next_frame = 0;
50 | mad_bit_init(&stream->ptr, 0);
51 |
52 | mad_bit_init(&stream->anc_ptr, 0);
53 | stream->anc_bitlen = 0;
54 |
55 | stream->main_data = 0;
56 | stream->md_len = 0;
57 |
58 | stream->options = 0;
59 | stream->error = MAD_ERROR_NONE;
60 | }
61 |
62 | /*
63 | * NAME: stream->finish()
64 | * DESCRIPTION: deallocate any dynamic memory associated with stream
65 | */
66 | void mad_stream_finish(struct mad_stream *stream)
67 | {
68 | stack(__FUNCTION__, __FILE__, __LINE__);
69 | if (stream->main_data) {
70 | free(stream->main_data);
71 | stream->main_data = 0;
72 | }
73 |
74 | mad_bit_finish(&stream->anc_ptr);
75 | mad_bit_finish(&stream->ptr);
76 | }
77 |
78 | /*
79 | * NAME: stream->buffer()
80 | * DESCRIPTION: set stream buffer pointers
81 | */
82 | void mad_stream_buffer(struct mad_stream *stream,
83 | unsigned char const *buffer, unsigned long length)
84 | {
85 | stack(__FUNCTION__, __FILE__, __LINE__);
86 | stream->buffer = buffer;
87 | stream->bufend = buffer + length;
88 |
89 | stream->this_frame = buffer;
90 | stream->next_frame = buffer;
91 |
92 | stream->sync = 1;
93 |
94 | mad_bit_init(&stream->ptr, buffer);
95 | }
96 |
97 | /*
98 | * NAME: stream->skip()
99 | * DESCRIPTION: arrange to skip bytes before the next frame
100 | */
101 | void mad_stream_skip(struct mad_stream *stream, unsigned long length)
102 | {
103 | stack(__FUNCTION__, __FILE__, __LINE__);
104 | stream->skiplen += length;
105 | }
106 |
107 | /*
108 | * NAME: stream->sync()
109 | * DESCRIPTION: locate the next stream sync word
110 | */
111 | int mad_stream_sync(struct mad_stream *stream)
112 | {
113 | register unsigned char const *ptr, *end;
114 | stack(__FUNCTION__, __FILE__, __LINE__);
115 |
116 | ptr = mad_bit_nextbyte(&stream->ptr);
117 | end = stream->bufend;
118 |
119 | while (ptr < end - 1 &&
120 | !(ptr[0] == 0xff && (ptr[1] & 0xe0) == 0xe0))
121 | ++ptr;
122 |
123 | if (end - ptr < MAD_BUFFER_GUARD)
124 | return -1;
125 |
126 | mad_bit_init(&stream->ptr, ptr);
127 |
128 | return 0;
129 | }
130 |
131 | /*
132 | * NAME: stream->errorstr()
133 | * DESCRIPTION: return a string description of the current error condition
134 | */
135 | char const *mad_stream_errorstr(struct mad_stream const *stream)
136 | {
137 | stack(__FUNCTION__, __FILE__, __LINE__);
138 | switch (stream->error) {
139 | case MAD_ERROR_NONE: return PSTR("no error");
140 |
141 | case MAD_ERROR_BUFLEN: return PSTR("input buffer too small (or EOF)");
142 | case MAD_ERROR_BUFPTR: return PSTR("invalid (null) buffer pointer");
143 |
144 | case MAD_ERROR_NOMEM: return PSTR("not enough memory");
145 |
146 | case MAD_ERROR_LOSTSYNC: return PSTR("lost synchronization");
147 | case MAD_ERROR_BADLAYER: return PSTR("reserved header layer value");
148 | case MAD_ERROR_BADBITRATE: return PSTR("forbidden bitrate value");
149 | case MAD_ERROR_BADSAMPLERATE: return PSTR("reserved sample frequency value");
150 | case MAD_ERROR_BADEMPHASIS: return PSTR("reserved emphasis value");
151 |
152 | case MAD_ERROR_BADCRC: return PSTR("CRC check failed");
153 | case MAD_ERROR_BADBITALLOC: return PSTR("forbidden bit allocation value");
154 | case MAD_ERROR_BADSCALEFACTOR: return PSTR("bad scalefactor index");
155 | case MAD_ERROR_BADMODE: return PSTR("bad bitrate/mode combination");
156 | case MAD_ERROR_BADFRAMELEN: return PSTR("bad frame length");
157 | case MAD_ERROR_BADBIGVALUES: return PSTR("bad big_values count");
158 | case MAD_ERROR_BADBLOCKTYPE: return PSTR("reserved block_type");
159 | case MAD_ERROR_BADSCFSI: return PSTR("bad scalefactor selection info");
160 | case MAD_ERROR_BADDATAPTR: return PSTR("bad main_data_begin pointer");
161 | case MAD_ERROR_BADPART3LEN: return PSTR("bad audio data length");
162 | case MAD_ERROR_BADHUFFTABLE: return PSTR("bad Huffman table select");
163 | case MAD_ERROR_BADHUFFDATA: return PSTR("Huffman data overrun");
164 | case MAD_ERROR_BADSTEREO: return PSTR("incompatible block_type for JS");
165 | }
166 |
167 | return 0;
168 | }
169 |
--------------------------------------------------------------------------------
/stream.h:
--------------------------------------------------------------------------------
1 | /*
2 | * libmad - MPEG audio decoder library
3 | * Copyright (C) 2000-2004 Underbit Technologies, Inc.
4 | *
5 | * This program is free software; you can redistribute it and/or modify
6 | * it under the terms of the GNU General Public License as published by
7 | * the Free Software Foundation; either version 2 of the License, or
8 | * (at your option) any later version.
9 | *
10 | * This program is distributed in the hope that it will be useful,
11 | * but WITHOUT ANY WARRANTY; without even the implied warranty of
12 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13 | * GNU General Public License for more details.
14 | *
15 | * You should have received a copy of the GNU General Public License
16 | * along with this program; if not, write to the Free Software
17 | * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
18 | *
19 | * $Id: stream.h,v 1.20 2004/02/05 09:02:39 rob Exp $
20 | */
21 |
22 | # ifndef LIBMAD_STREAM_H
23 | # define LIBMAD_STREAM_H
24 |
25 | # include "bit.h"
26 |
27 | # define MAD_BUFFER_GUARD 8
28 | # define MAD_BUFFER_MDLEN (511 + 2048 + MAD_BUFFER_GUARD)
29 |
30 | enum mad_error {
31 | MAD_ERROR_NONE = 0x0000, /* no error */
32 |
33 | MAD_ERROR_BUFLEN = 0x0001, /* input buffer too small (or EOF) */
34 | MAD_ERROR_BUFPTR = 0x0002, /* invalid (null) buffer pointer */
35 |
36 | MAD_ERROR_NOMEM = 0x0031, /* not enough memory */
37 |
38 | MAD_ERROR_LOSTSYNC = 0x0101, /* lost synchronization */
39 | MAD_ERROR_BADLAYER = 0x0102, /* reserved header layer value */
40 | MAD_ERROR_BADBITRATE = 0x0103, /* forbidden bitrate value */
41 | MAD_ERROR_BADSAMPLERATE = 0x0104, /* reserved sample frequency value */
42 | MAD_ERROR_BADEMPHASIS = 0x0105, /* reserved emphasis value */
43 |
44 | MAD_ERROR_BADCRC = 0x0201, /* CRC check failed */
45 | MAD_ERROR_BADBITALLOC = 0x0211, /* forbidden bit allocation value */
46 | MAD_ERROR_BADSCALEFACTOR = 0x0221, /* bad scalefactor index */
47 | MAD_ERROR_BADMODE = 0x0222, /* bad bitrate/mode combination */
48 | MAD_ERROR_BADFRAMELEN = 0x0231, /* bad frame length */
49 | MAD_ERROR_BADBIGVALUES = 0x0232, /* bad big_values count */
50 | MAD_ERROR_BADBLOCKTYPE = 0x0233, /* reserved block_type */
51 | MAD_ERROR_BADSCFSI = 0x0234, /* bad scalefactor selection info */
52 | MAD_ERROR_BADDATAPTR = 0x0235, /* bad main_data_begin pointer */
53 | MAD_ERROR_BADPART3LEN = 0x0236, /* bad audio data length */
54 | MAD_ERROR_BADHUFFTABLE = 0x0237, /* bad Huffman table select */
55 | MAD_ERROR_BADHUFFDATA = 0x0238, /* Huffman data overrun */
56 | MAD_ERROR_BADSTEREO = 0x0239 /* incompatible block_type for JS */
57 | };
58 |
59 | # define MAD_RECOVERABLE(error) ((error) & 0xff00)
60 |
61 | struct mad_stream {
62 | unsigned char const *buffer; /* input bitstream buffer */
63 | unsigned char const *bufend; /* end of buffer */
64 | unsigned long skiplen; /* bytes to skip before next frame */
65 |
66 | int sync; /* stream sync found */
67 | unsigned long freerate; /* free bitrate (fixed) */
68 |
69 | unsigned char const *this_frame; /* start of current frame */
70 | unsigned char const *next_frame; /* start of next frame */
71 | struct mad_bitptr ptr; /* current processing bit pointer */
72 |
73 | struct mad_bitptr anc_ptr; /* ancillary bits pointer */
74 | unsigned int anc_bitlen; /* number of ancillary bits */
75 |
76 | unsigned char (*main_data)[MAD_BUFFER_MDLEN];
77 | /* Layer III main_data() */
78 | unsigned int md_len; /* bytes in main_data */
79 |
80 | int options; /* decoding options (see below) */
81 | enum mad_error error; /* error code (see above) */
82 | };
83 |
84 | enum {
85 | MAD_OPTION_IGNORECRC = 0x0001, /* ignore CRC errors */
86 | MAD_OPTION_HALFSAMPLERATE = 0x0002 /* generate PCM at 1/2 sample rate */
87 | # if 0 /* not yet implemented */
88 | MAD_OPTION_LEFTCHANNEL = 0x0010, /* decode left channel only */
89 | MAD_OPTION_RIGHTCHANNEL = 0x0020, /* decode right channel only */
90 | MAD_OPTION_SINGLECHANNEL = 0x0030 /* combine channels */
91 | # endif
92 | };
93 |
94 | void mad_stream_init(struct mad_stream *);
95 | void mad_stream_finish(struct mad_stream *);
96 |
97 | # define mad_stream_options(stream, opts) \
98 | ((void) ((stream)->options = (opts)))
99 |
100 | void mad_stream_buffer(struct mad_stream *,
101 | unsigned char const *, unsigned long);
102 | void mad_stream_skip(struct mad_stream *, unsigned long);
103 |
104 | int mad_stream_sync(struct mad_stream *);
105 |
106 | char const *mad_stream_errorstr(struct mad_stream const *);
107 |
108 | # endif
109 |
--------------------------------------------------------------------------------
/synth.h:
--------------------------------------------------------------------------------
1 | /*
2 | * libmad - MPEG audio decoder library
3 | * Copyright (C) 2000-2004 Underbit Technologies, Inc.
4 | *
5 | * This program is free software; you can redistribute it and/or modify
6 | * it under the terms of the GNU General Public License as published by
7 | * the Free Software Foundation; either version 2 of the License, or
8 | * (at your option) any later version.
9 | *
10 | * This program is distributed in the hope that it will be useful,
11 | * but WITHOUT ANY WARRANTY; without even the implied warranty of
12 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13 | * GNU General Public License for more details.
14 | *
15 | * You should have received a copy of the GNU General Public License
16 | * along with this program; if not, write to the Free Software
17 | * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
18 | *
19 | * $Id: synth.h,v 1.15 2004/01/23 09:41:33 rob Exp $
20 | */
21 |
22 | # ifndef LIBMAD_SYNTH_H
23 | # define LIBMAD_SYNTH_H
24 |
25 | # include "fixed.h"
26 | # include "frame.h"
27 | #include
28 |
29 | struct mad_pcm {
30 | unsigned int samplerate; /* sampling frequency (Hz) */
31 | unsigned short channels; /* number of channels */
32 | unsigned short length; /* number of samples per channel */
33 | int16_t samples[2][32]; //1152]; /* PCM output samples [ch][sample] */
34 | };
35 |
36 | struct mad_synth {
37 | mad_fixed_t filter[2][2][2][16][8]; /* polyphase filterbank outputs */
38 | /* [ch][eo][peo][s][v] */
39 |
40 | unsigned int phase; /* current processing phase */
41 |
42 | struct mad_pcm pcm; /* PCM output */
43 | };
44 |
45 | /* single channel PCM selector */
46 | enum {
47 | MAD_PCM_CHANNEL_SINGLE = 0
48 | };
49 |
50 | /* dual channel PCM selector */
51 | enum {
52 | MAD_PCM_CHANNEL_DUAL_1 = 0,
53 | MAD_PCM_CHANNEL_DUAL_2 = 1
54 | };
55 |
56 | /* stereo PCM selector */
57 | enum {
58 | MAD_PCM_CHANNEL_STEREO_LEFT = 0,
59 | MAD_PCM_CHANNEL_STEREO_RIGHT = 1
60 | };
61 |
62 | void mad_synth_init(struct mad_synth *);
63 |
64 | # define mad_synth_finish(synth) /* nothing */
65 |
66 | void mad_synth_mute(struct mad_synth *);
67 |
68 | enum mad_flow mad_synth_frame(struct mad_synth *, struct mad_frame const *, enum mad_flow (*output_func)(void *s, struct mad_header const *, struct mad_pcm *), void *cbdata );
69 | enum mad_flow mad_synth_frame_onens(struct mad_synth *synth, struct mad_frame const *frame, unsigned int ns);
70 |
71 | # endif
72 |
--------------------------------------------------------------------------------
/timer.c:
--------------------------------------------------------------------------------
1 | /*
2 | * libmad - MPEG audio decoder library
3 | * Copyright (C) 2000-2004 Underbit Technologies, Inc.
4 | *
5 | * This program is free software; you can redistribute it and/or modify
6 | * it under the terms of the GNU General Public License as published by
7 | * the Free Software Foundation; either version 2 of the License, or
8 | * (at your option) any later version.
9 | *
10 | * This program is distributed in the hope that it will be useful,
11 | * but WITHOUT ANY WARRANTY; without even the implied warranty of
12 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13 | * GNU General Public License for more details.
14 | *
15 | * You should have received a copy of the GNU General Public License
16 | * along with this program; if not, write to the Free Software
17 | * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
18 | *
19 | * $Id: timer.c,v 1.18 2004/01/23 09:41:33 rob Exp $
20 | */
21 |
22 | #pragma GCC optimize ("O3")
23 |
24 | # include "config.h"
25 |
26 | # include "global.h"
27 |
28 | # include
29 |
30 | # ifdef HAVE_ASSERT_H
31 | # include
32 | # endif
33 |
34 | # include "timer.h"
35 |
36 | mad_timer_t const mad_timer_zero = { 0, 0 };
37 |
38 | /*
39 | * NAME: timer->compare()
40 | * DESCRIPTION: indicate relative order of two timers
41 | */
42 | int mad_timer_compare(mad_timer_t timer1, mad_timer_t timer2)
43 | {
44 | signed long diff;
45 |
46 | diff = timer1.seconds - timer2.seconds;
47 | if (diff < 0)
48 | return -1;
49 | else if (diff > 0)
50 | return +1;
51 |
52 | diff = timer1.fraction - timer2.fraction;
53 | if (diff < 0)
54 | return -1;
55 | else if (diff > 0)
56 | return +1;
57 |
58 | return 0;
59 | }
60 |
61 | /*
62 | * NAME: timer->negate()
63 | * DESCRIPTION: invert the sign of a timer
64 | */
65 | void mad_timer_negate(mad_timer_t *timer)
66 | {
67 | timer->seconds = -timer->seconds;
68 |
69 | if (timer->fraction) {
70 | timer->seconds -= 1;
71 | timer->fraction = MAD_TIMER_RESOLUTION - timer->fraction;
72 | }
73 | }
74 |
75 | /*
76 | * NAME: timer->abs()
77 | * DESCRIPTION: return the absolute value of a timer
78 | */
79 | mad_timer_t mad_timer_abs(mad_timer_t timer)
80 | {
81 | if (timer.seconds < 0)
82 | mad_timer_negate(&timer);
83 |
84 | return timer;
85 | }
86 |
87 | /*
88 | * NAME: reduce_timer()
89 | * DESCRIPTION: carry timer fraction into seconds
90 | */
91 | static
92 | void reduce_timer(mad_timer_t *timer)
93 | {
94 | timer->seconds += timer->fraction / MAD_TIMER_RESOLUTION;
95 | timer->fraction %= MAD_TIMER_RESOLUTION;
96 | }
97 |
98 | /*
99 | * NAME: gcd()
100 | * DESCRIPTION: compute greatest common denominator
101 | */
102 | static
103 | unsigned long gcd(unsigned long num1, unsigned long num2)
104 | {
105 | unsigned long tmp;
106 |
107 | while (num2) {
108 | tmp = num2;
109 | num2 = num1 % num2;
110 | num1 = tmp;
111 | }
112 |
113 | return num1;
114 | }
115 |
116 | /*
117 | * NAME: reduce_rational()
118 | * DESCRIPTION: convert rational expression to lowest terms
119 | */
120 | static
121 | void reduce_rational(unsigned long *numer, unsigned long *denom)
122 | {
123 | unsigned long factor;
124 |
125 | factor = gcd(*numer, *denom);
126 |
127 | assert(factor != 0);
128 |
129 | *numer /= factor;
130 | *denom /= factor;
131 | }
132 |
133 | /*
134 | * NAME: scale_rational()
135 | * DESCRIPTION: solve numer/denom == ?/scale avoiding overflowing
136 | */
137 | static
138 | unsigned long scale_rational(unsigned long numer, unsigned long denom,
139 | unsigned long scale)
140 | {
141 | reduce_rational(&numer, &denom);
142 | reduce_rational(&scale, &denom);
143 |
144 | assert(denom != 0);
145 |
146 | if (denom < scale)
147 | return numer * (scale / denom) + numer * (scale % denom) / denom;
148 | if (denom < numer)
149 | return scale * (numer / denom) + scale * (numer % denom) / denom;
150 |
151 | return numer * scale / denom;
152 | }
153 |
154 | /*
155 | * NAME: timer->set()
156 | * DESCRIPTION: set timer to specific (positive) value
157 | */
158 | void mad_timer_set(mad_timer_t *timer, unsigned long seconds,
159 | unsigned long numer, unsigned long denom)
160 | {
161 | timer->seconds = seconds;
162 | if (numer >= denom && denom > 0) {
163 | timer->seconds += numer / denom;
164 | numer %= denom;
165 | }
166 |
167 | switch (denom) {
168 | case 0:
169 | case 1:
170 | timer->fraction = 0;
171 | break;
172 |
173 | case MAD_TIMER_RESOLUTION:
174 | timer->fraction = numer;
175 | break;
176 |
177 | case 1000:
178 | timer->fraction = numer * (MAD_TIMER_RESOLUTION / 1000);
179 | break;
180 |
181 | case 8000:
182 | timer->fraction = numer * (MAD_TIMER_RESOLUTION / 8000);
183 | break;
184 |
185 | case 11025:
186 | timer->fraction = numer * (MAD_TIMER_RESOLUTION / 11025);
187 | break;
188 |
189 | case 12000:
190 | timer->fraction = numer * (MAD_TIMER_RESOLUTION / 12000);
191 | break;
192 |
193 | case 16000:
194 | timer->fraction = numer * (MAD_TIMER_RESOLUTION / 16000);
195 | break;
196 |
197 | case 22050:
198 | timer->fraction = numer * (MAD_TIMER_RESOLUTION / 22050);
199 | break;
200 |
201 | case 24000:
202 | timer->fraction = numer * (MAD_TIMER_RESOLUTION / 24000);
203 | break;
204 |
205 | case 32000:
206 | timer->fraction = numer * (MAD_TIMER_RESOLUTION / 32000);
207 | break;
208 |
209 | case 44100:
210 | timer->fraction = numer * (MAD_TIMER_RESOLUTION / 44100);
211 | break;
212 |
213 | case 48000:
214 | timer->fraction = numer * (MAD_TIMER_RESOLUTION / 48000);
215 | break;
216 |
217 | default:
218 | timer->fraction = scale_rational(numer, denom, MAD_TIMER_RESOLUTION);
219 | break;
220 | }
221 |
222 | if (timer->fraction >= MAD_TIMER_RESOLUTION)
223 | reduce_timer(timer);
224 | }
225 |
226 | /*
227 | * NAME: timer->add()
228 | * DESCRIPTION: add one timer to another
229 | */
230 | void mad_timer_add(mad_timer_t *timer, mad_timer_t incr)
231 | {
232 | timer->seconds += incr.seconds;
233 | timer->fraction += incr.fraction;
234 |
235 | if (timer->fraction >= MAD_TIMER_RESOLUTION)
236 | reduce_timer(timer);
237 | }
238 |
239 | /*
240 | * NAME: timer->multiply()
241 | * DESCRIPTION: multiply a timer by a scalar value
242 | */
243 | void mad_timer_multiply(mad_timer_t *timer, signed long scalar)
244 | {
245 | mad_timer_t addend;
246 | unsigned long factor;
247 |
248 | factor = scalar;
249 | if (scalar < 0) {
250 | factor = -scalar;
251 | mad_timer_negate(timer);
252 | }
253 |
254 | addend = *timer;
255 | *timer = mad_timer_zero;
256 |
257 | while (factor) {
258 | if (factor & 1)
259 | mad_timer_add(timer, addend);
260 |
261 | mad_timer_add(&addend, addend);
262 | factor >>= 1;
263 | }
264 | }
265 |
266 | /*
267 | * NAME: timer->count()
268 | * DESCRIPTION: return timer value in selected units
269 | */
270 | signed long mad_timer_count(mad_timer_t timer, enum mad_units units)
271 | {
272 | switch (units) {
273 | case MAD_UNITS_HOURS:
274 | return timer.seconds / 60 / 60;
275 |
276 | case MAD_UNITS_MINUTES:
277 | return timer.seconds / 60;
278 |
279 | case MAD_UNITS_SECONDS:
280 | return timer.seconds;
281 |
282 | case MAD_UNITS_DECISECONDS:
283 | case MAD_UNITS_CENTISECONDS:
284 | case MAD_UNITS_MILLISECONDS:
285 |
286 | case MAD_UNITS_8000_HZ:
287 | case MAD_UNITS_11025_HZ:
288 | case MAD_UNITS_12000_HZ:
289 | case MAD_UNITS_16000_HZ:
290 | case MAD_UNITS_22050_HZ:
291 | case MAD_UNITS_24000_HZ:
292 | case MAD_UNITS_32000_HZ:
293 | case MAD_UNITS_44100_HZ:
294 | case MAD_UNITS_48000_HZ:
295 |
296 | case MAD_UNITS_24_FPS:
297 | case MAD_UNITS_25_FPS:
298 | case MAD_UNITS_30_FPS:
299 | case MAD_UNITS_48_FPS:
300 | case MAD_UNITS_50_FPS:
301 | case MAD_UNITS_60_FPS:
302 | case MAD_UNITS_75_FPS:
303 | return timer.seconds * (signed long) units +
304 | (signed long) scale_rational(timer.fraction, MAD_TIMER_RESOLUTION,
305 | units);
306 |
307 | case MAD_UNITS_23_976_FPS:
308 | case MAD_UNITS_24_975_FPS:
309 | case MAD_UNITS_29_97_FPS:
310 | case MAD_UNITS_47_952_FPS:
311 | case MAD_UNITS_49_95_FPS:
312 | case MAD_UNITS_59_94_FPS:
313 | return (mad_timer_count(timer, -units) + 1) * 1000 / 1001;
314 | }
315 |
316 | /* unsupported units */
317 | return 0;
318 | }
319 |
320 | /*
321 | * NAME: timer->fraction()
322 | * DESCRIPTION: return fractional part of timer in arbitrary terms
323 | */
324 | unsigned long mad_timer_fraction(mad_timer_t timer, unsigned long denom)
325 | {
326 | timer = mad_timer_abs(timer);
327 |
328 | switch (denom) {
329 | case 0:
330 | return timer.fraction ?
331 | MAD_TIMER_RESOLUTION / timer.fraction : MAD_TIMER_RESOLUTION + 1;
332 |
333 | case MAD_TIMER_RESOLUTION:
334 | return timer.fraction;
335 |
336 | default:
337 | return scale_rational(timer.fraction, MAD_TIMER_RESOLUTION, denom);
338 | }
339 | }
340 |
341 | /*
342 | * NAME: timer->string()
343 | * DESCRIPTION: write a string representation of a timer using a template
344 | */
345 | void mad_timer_string(mad_timer_t timer,
346 | char *dest, char const *format, enum mad_units units,
347 | enum mad_units fracunits, unsigned long subparts)
348 | {
349 | unsigned long hours, minutes, seconds, sub;
350 | unsigned int frac;
351 |
352 | timer = mad_timer_abs(timer);
353 |
354 | seconds = timer.seconds;
355 | frac = sub = 0;
356 |
357 | switch (fracunits) {
358 | case MAD_UNITS_HOURS:
359 | case MAD_UNITS_MINUTES:
360 | case MAD_UNITS_SECONDS:
361 | break;
362 |
363 | case MAD_UNITS_DECISECONDS:
364 | case MAD_UNITS_CENTISECONDS:
365 | case MAD_UNITS_MILLISECONDS:
366 |
367 | case MAD_UNITS_8000_HZ:
368 | case MAD_UNITS_11025_HZ:
369 | case MAD_UNITS_12000_HZ:
370 | case MAD_UNITS_16000_HZ:
371 | case MAD_UNITS_22050_HZ:
372 | case MAD_UNITS_24000_HZ:
373 | case MAD_UNITS_32000_HZ:
374 | case MAD_UNITS_44100_HZ:
375 | case MAD_UNITS_48000_HZ:
376 |
377 | case MAD_UNITS_24_FPS:
378 | case MAD_UNITS_25_FPS:
379 | case MAD_UNITS_30_FPS:
380 | case MAD_UNITS_48_FPS:
381 | case MAD_UNITS_50_FPS:
382 | case MAD_UNITS_60_FPS:
383 | case MAD_UNITS_75_FPS:
384 | {
385 | unsigned long denom;
386 |
387 | denom = MAD_TIMER_RESOLUTION / fracunits;
388 |
389 | frac = timer.fraction / denom;
390 | sub = scale_rational(timer.fraction % denom, denom, subparts);
391 | }
392 | break;
393 |
394 | case MAD_UNITS_23_976_FPS:
395 | case MAD_UNITS_24_975_FPS:
396 | case MAD_UNITS_29_97_FPS:
397 | case MAD_UNITS_47_952_FPS:
398 | case MAD_UNITS_49_95_FPS:
399 | case MAD_UNITS_59_94_FPS:
400 | /* drop-frame encoding */
401 | /* N.B. this is only well-defined for MAD_UNITS_29_97_FPS */
402 | {
403 | unsigned long frame, cycle, d, m;
404 |
405 | frame = mad_timer_count(timer, fracunits);
406 |
407 | cycle = -fracunits * 60 * 10 - (10 - 1) * 2;
408 |
409 | d = frame / cycle;
410 | m = frame % cycle;
411 | frame += (10 - 1) * 2 * d;
412 | if (m > 2)
413 | frame += 2 * ((m - 2) / (cycle / 10));
414 |
415 | frac = frame % -fracunits;
416 | seconds = frame / -fracunits;
417 | }
418 | break;
419 | }
420 |
421 | switch (units) {
422 | case MAD_UNITS_HOURS:
423 | minutes = seconds / 60;
424 | hours = minutes / 60;
425 |
426 | sprintf(dest, format,
427 | hours,
428 | (unsigned int) (minutes % 60),
429 | (unsigned int) (seconds % 60),
430 | frac, sub);
431 | break;
432 |
433 | case MAD_UNITS_MINUTES:
434 | minutes = seconds / 60;
435 |
436 | sprintf(dest, format,
437 | minutes,
438 | (unsigned int) (seconds % 60),
439 | frac, sub);
440 | break;
441 |
442 | case MAD_UNITS_SECONDS:
443 | sprintf(dest, format,
444 | seconds,
445 | frac, sub);
446 | break;
447 |
448 | case MAD_UNITS_23_976_FPS:
449 | case MAD_UNITS_24_975_FPS:
450 | case MAD_UNITS_29_97_FPS:
451 | case MAD_UNITS_47_952_FPS:
452 | case MAD_UNITS_49_95_FPS:
453 | case MAD_UNITS_59_94_FPS:
454 | if (fracunits < 0) {
455 | /* not yet implemented */
456 | sub = 0;
457 | }
458 |
459 | /* fall through */
460 |
461 | case MAD_UNITS_DECISECONDS:
462 | case MAD_UNITS_CENTISECONDS:
463 | case MAD_UNITS_MILLISECONDS:
464 |
465 | case MAD_UNITS_8000_HZ:
466 | case MAD_UNITS_11025_HZ:
467 | case MAD_UNITS_12000_HZ:
468 | case MAD_UNITS_16000_HZ:
469 | case MAD_UNITS_22050_HZ:
470 | case MAD_UNITS_24000_HZ:
471 | case MAD_UNITS_32000_HZ:
472 | case MAD_UNITS_44100_HZ:
473 | case MAD_UNITS_48000_HZ:
474 |
475 | case MAD_UNITS_24_FPS:
476 | case MAD_UNITS_25_FPS:
477 | case MAD_UNITS_30_FPS:
478 | case MAD_UNITS_48_FPS:
479 | case MAD_UNITS_50_FPS:
480 | case MAD_UNITS_60_FPS:
481 | case MAD_UNITS_75_FPS:
482 | sprintf(dest, format, mad_timer_count(timer, units), sub);
483 | break;
484 | }
485 | }
486 |
--------------------------------------------------------------------------------
/timer.h:
--------------------------------------------------------------------------------
1 | /*
2 | * libmad - MPEG audio decoder library
3 | * Copyright (C) 2000-2004 Underbit Technologies, Inc.
4 | *
5 | * This program is free software; you can redistribute it and/or modify
6 | * it under the terms of the GNU General Public License as published by
7 | * the Free Software Foundation; either version 2 of the License, or
8 | * (at your option) any later version.
9 | *
10 | * This program is distributed in the hope that it will be useful,
11 | * but WITHOUT ANY WARRANTY; without even the implied warranty of
12 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13 | * GNU General Public License for more details.
14 | *
15 | * You should have received a copy of the GNU General Public License
16 | * along with this program; if not, write to the Free Software
17 | * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
18 | *
19 | * $Id: timer.h,v 1.16 2004/01/23 09:41:33 rob Exp $
20 | */
21 |
22 | # ifndef LIBMAD_TIMER_H
23 | # define LIBMAD_TIMER_H
24 |
25 | typedef struct {
26 | signed long seconds; /* whole seconds */
27 | unsigned long fraction; /* 1/MAD_TIMER_RESOLUTION seconds */
28 | } mad_timer_t;
29 |
30 | extern mad_timer_t const mad_timer_zero;
31 |
32 | # define MAD_TIMER_RESOLUTION 352800000UL
33 |
34 | enum mad_units {
35 | MAD_UNITS_HOURS = -2,
36 | MAD_UNITS_MINUTES = -1,
37 | MAD_UNITS_SECONDS = 0,
38 |
39 | /* metric units */
40 |
41 | MAD_UNITS_DECISECONDS = 10,
42 | MAD_UNITS_CENTISECONDS = 100,
43 | MAD_UNITS_MILLISECONDS = 1000,
44 |
45 | /* audio sample units */
46 |
47 | MAD_UNITS_8000_HZ = 8000,
48 | MAD_UNITS_11025_HZ = 11025,
49 | MAD_UNITS_12000_HZ = 12000,
50 |
51 | MAD_UNITS_16000_HZ = 16000,
52 | MAD_UNITS_22050_HZ = 22050,
53 | MAD_UNITS_24000_HZ = 24000,
54 |
55 | MAD_UNITS_32000_HZ = 32000,
56 | MAD_UNITS_44100_HZ = 44100,
57 | MAD_UNITS_48000_HZ = 48000,
58 |
59 | /* video frame/field units */
60 |
61 | MAD_UNITS_24_FPS = 24,
62 | MAD_UNITS_25_FPS = 25,
63 | MAD_UNITS_30_FPS = 30,
64 | MAD_UNITS_48_FPS = 48,
65 | MAD_UNITS_50_FPS = 50,
66 | MAD_UNITS_60_FPS = 60,
67 |
68 | /* CD audio frames */
69 |
70 | MAD_UNITS_75_FPS = 75,
71 |
72 | /* video drop-frame units */
73 |
74 | MAD_UNITS_23_976_FPS = -24,
75 | MAD_UNITS_24_975_FPS = -25,
76 | MAD_UNITS_29_97_FPS = -30,
77 | MAD_UNITS_47_952_FPS = -48,
78 | MAD_UNITS_49_95_FPS = -50,
79 | MAD_UNITS_59_94_FPS = -60
80 | };
81 |
82 | # define mad_timer_reset(timer) ((void) (*(timer) = mad_timer_zero))
83 |
84 | int mad_timer_compare(mad_timer_t, mad_timer_t);
85 |
86 | # define mad_timer_sign(timer) mad_timer_compare((timer), mad_timer_zero)
87 |
88 | void mad_timer_negate(mad_timer_t *);
89 | mad_timer_t mad_timer_abs(mad_timer_t);
90 |
91 | void mad_timer_set(mad_timer_t *, unsigned long, unsigned long, unsigned long);
92 | void mad_timer_add(mad_timer_t *, mad_timer_t);
93 | void mad_timer_multiply(mad_timer_t *, signed long);
94 |
95 | signed long mad_timer_count(mad_timer_t, enum mad_units);
96 | unsigned long mad_timer_fraction(mad_timer_t, unsigned long);
97 | void mad_timer_string(mad_timer_t, char *, char const *,
98 | enum mad_units, enum mad_units, unsigned long);
99 |
100 | # endif
101 |
--------------------------------------------------------------------------------
/version.c:
--------------------------------------------------------------------------------
1 | /*
2 | * libmad - MPEG audio decoder library
3 | * Copyright (C) 2000-2004 Underbit Technologies, Inc.
4 | *
5 | * This program is free software; you can redistribute it and/or modify
6 | * it under the terms of the GNU General Public License as published by
7 | * the Free Software Foundation; either version 2 of the License, or
8 | * (at your option) any later version.
9 | *
10 | * This program is distributed in the hope that it will be useful,
11 | * but WITHOUT ANY WARRANTY; without even the implied warranty of
12 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13 | * GNU General Public License for more details.
14 | *
15 | * You should have received a copy of the GNU General Public License
16 | * along with this program; if not, write to the Free Software
17 | * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
18 | *
19 | * $Id: version.c,v 1.15 2004/01/23 09:41:33 rob Exp $
20 | */
21 |
22 | #pragma GCC optimize ("O3")
23 |
24 | # include "config.h"
25 |
26 | # include "global.h"
27 |
28 | # include "version.h"
29 |
30 | char const mad_version[] = "MPEG Audio Decoder " MAD_VERSION;
31 | char const mad_copyright[] = "Copyright (C) " MAD_PUBLISHYEAR " " MAD_AUTHOR;
32 | char const mad_author[] = MAD_AUTHOR " <" MAD_EMAIL ">";
33 |
34 | char const mad_build[] = ""
35 | # if defined(DEBUG)
36 | "DEBUG "
37 | # elif defined(NDEBUG)
38 | "NDEBUG "
39 | # endif
40 |
41 | # if defined(EXPERIMENTAL)
42 | "EXPERIMENTAL "
43 | # endif
44 |
45 | # if defined(FPM_64BIT)
46 | "FPM_64BIT "
47 | # elif defined(FPM_INTEL)
48 | "FPM_INTEL "
49 | # elif defined(FPM_ARM)
50 | "FPM_ARM "
51 | # elif defined(FPM_MIPS)
52 | "FPM_MIPS "
53 | # elif defined(FPM_SPARC)
54 | "FPM_SPARC "
55 | # elif defined(FPM_PPC)
56 | "FPM_PPC "
57 | # elif defined(FPM_DEFAULT)
58 | "FPM_DEFAULT "
59 | # endif
60 |
61 | # if defined(ASO_IMDCT)
62 | "ASO_IMDCT "
63 | # endif
64 | # if defined(ASO_INTERLEAVE1)
65 | "ASO_INTERLEAVE1 "
66 | # endif
67 | # if defined(ASO_INTERLEAVE2)
68 | "ASO_INTERLEAVE2 "
69 | # endif
70 | # if defined(ASO_ZEROCHECK)
71 | "ASO_ZEROCHECK "
72 | # endif
73 |
74 | # if defined(OPT_SPEED)
75 | "OPT_SPEED "
76 | # elif defined(OPT_ACCURACY)
77 | "OPT_ACCURACY "
78 | # endif
79 |
80 | # if defined(OPT_SSO)
81 | "OPT_SSO "
82 | # endif
83 |
84 | # if defined(OPT_DCTO) /* never defined here */
85 | "OPT_DCTO "
86 | # endif
87 |
88 | # if defined(OPT_STRICT)
89 | "OPT_STRICT "
90 | # endif
91 | ;
92 |
--------------------------------------------------------------------------------
/version.h:
--------------------------------------------------------------------------------
1 | /*
2 | * libmad - MPEG audio decoder library
3 | * Copyright (C) 2000-2004 Underbit Technologies, Inc.
4 | *
5 | * This program is free software; you can redistribute it and/or modify
6 | * it under the terms of the GNU General Public License as published by
7 | * the Free Software Foundation; either version 2 of the License, or
8 | * (at your option) any later version.
9 | *
10 | * This program is distributed in the hope that it will be useful,
11 | * but WITHOUT ANY WARRANTY; without even the implied warranty of
12 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13 | * GNU General Public License for more details.
14 | *
15 | * You should have received a copy of the GNU General Public License
16 | * along with this program; if not, write to the Free Software
17 | * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
18 | *
19 | * $Id: version.h,v 1.26 2004/01/23 09:41:33 rob Exp $
20 | */
21 |
22 | # ifndef LIBMAD_VERSION_H
23 | # define LIBMAD_VERSION_H
24 |
25 | # define MAD_VERSION_MAJOR 0
26 | # define MAD_VERSION_MINOR 15
27 | # define MAD_VERSION_PATCH 1
28 | # define MAD_VERSION_EXTRA " (beta)"
29 |
30 | # define MAD_VERSION_STRINGIZE(str) #str
31 | # define MAD_VERSION_STRING(num) MAD_VERSION_STRINGIZE(num)
32 |
33 | # define MAD_VERSION MAD_VERSION_STRING(MAD_VERSION_MAJOR) "." \
34 | MAD_VERSION_STRING(MAD_VERSION_MINOR) "." \
35 | MAD_VERSION_STRING(MAD_VERSION_PATCH) \
36 | MAD_VERSION_EXTRA
37 |
38 | # define MAD_PUBLISHYEAR "2000-2004"
39 | # define MAD_AUTHOR "Underbit Technologies, Inc."
40 | # define MAD_EMAIL "info@underbit.com"
41 |
42 | extern char const mad_version[];
43 | extern char const mad_copyright[];
44 | extern char const mad_author[];
45 | extern char const mad_build[];
46 |
47 | # endif
48 |
--------------------------------------------------------------------------------