Enable static allocated buffers, no dynamic malloc
Because we are allocating objects totaling over 80% of the ESP8266 memory space, we are very sensitive to memory fragmentation. While we can control our own allocs, the IP and SDK can allocate any size, at any time, which can cause us to not be able to find the large contiguous memory areas we need for the codes. Work around this by preallocating, before anything else, buffer memory and never freeing it. Pass this memory into the codec and it will allocate from this block and be guaranteed to always succeed. This may fix the last of the hangs on the webradio/http streaming, which only seemed to happen after a stop/start cycle.
This commit is contained in:
1 parent
d04beb1105
commit
8ffebe6496
23 files changed
+306
-138
No files matched your search
@@ -28,6 +28,19 @@ AudioFileSourceBuffer::AudioFileSourceBuffer(AudioFileSource *source, int buffSi
|
||||
buffSize = buffSizeBytes;
|
||||
buffer = (uint8_t*)malloc(sizeof(uint8_t) * buffSize);
|
||||
if (!buffer) Serial.printf_P(PSTR("Unable to allocate AudioFileSourceBuffer::buffer[]\n"));
|
||||
deallocateBuffer = true;
|
||||
writePtr = 0;
|
||||
readPtr = 0;
|
||||
src = source;
|
||||
length = 0;
|
||||
filled = false;
|
||||
}
|
||||
|
||||
AudioFileSourceBuffer::AudioFileSourceBuffer(AudioFileSource *source, void *inBuff, int buffSizeBytes)
|
||||
{
|
||||
buffSize = buffSizeBytes;
|
||||
buffer = (uint8_t*)inBuff;
|
||||
deallocateBuffer = false;
|
||||
writePtr = 0;
|
||||
readPtr = 0;
|
||||
src = source;
|
||||
@@ -37,7 +50,7 @@ AudioFileSourceBuffer::AudioFileSourceBuffer(AudioFileSource *source, int buffSi
|
||||
|
||||
AudioFileSourceBuffer::~AudioFileSourceBuffer()
|
||||
{
|
||||
free(buffer);
|
||||
if (deallocateBuffer) free(buffer);
|
||||
buffer = NULL;
|
||||
}
|
||||
|
||||
@@ -52,7 +65,7 @@ bool AudioFileSourceBuffer::seek(int32_t pos, int dir)
|
||||
|
||||
bool AudioFileSourceBuffer::close()
|
||||
{
|
||||
free(buffer);
|
||||
if (deallocateBuffer) free(buffer);
|
||||
buffer = NULL;
|
||||
return src->close();
|
||||
}
|
||||
|
||||
@@ -28,6 +28,7 @@ class AudioFileSourceBuffer : public AudioFileSource
|
||||
{
|
||||
public:
|
||||
AudioFileSourceBuffer(AudioFileSource *in, int bufferBytes);
|
||||
AudioFileSourceBuffer(AudioFileSource *in, void *buffer, int bufferBytes); // Pre-allocated buffer by app
|
||||
virtual ~AudioFileSourceBuffer() override;
|
||||
|
||||
virtual uint32_t read(void *data, uint32_t len) override;
|
||||
@@ -47,6 +48,7 @@ class AudioFileSourceBuffer : public AudioFileSource
|
||||
AudioFileSource *src;
|
||||
uint16_t buffSize;
|
||||
uint8_t *buffer;
|
||||
bool deallocateBuffer;
|
||||
uint16_t writePtr;
|
||||
uint16_t readPtr;
|
||||
uint16_t length;
|
||||
|
||||
@@ -24,12 +24,12 @@ AudioFileSourceHTTPStream::AudioFileSourceHTTPStream()
|
||||
{
|
||||
pos = 0;
|
||||
reconnectTries = 0;
|
||||
saveURL = NULL;
|
||||
saveURL[0] = 0;
|
||||
}
|
||||
|
||||
AudioFileSourceHTTPStream::AudioFileSourceHTTPStream(const char *url)
|
||||
{
|
||||
saveURL = NULL;
|
||||
saveURL[0] = 0;
|
||||
reconnectTries = 0;
|
||||
open(url);
|
||||
}
|
||||
@@ -46,8 +46,8 @@ bool AudioFileSourceHTTPStream::open(const char *url)
|
||||
return false;
|
||||
}
|
||||
size = http.getSize();
|
||||
free(saveURL); saveURL = NULL;
|
||||
saveURL = strdup(url);
|
||||
strncpy(saveURL, url, sizeof(saveURL));
|
||||
saveURL[sizeof(saveURL)-1] = 0;
|
||||
return true;
|
||||
}
|
||||
|
||||
|
||||
@@ -57,7 +57,7 @@ class AudioFileSourceHTTPStream : public AudioFileSource
|
||||
int size;
|
||||
int reconnectTries;
|
||||
int reconnectDelayMs;
|
||||
char *saveURL;
|
||||
char saveURL[128];
|
||||
};
|
||||
|
||||
|
||||
|
||||
@@ -24,12 +24,12 @@ AudioFileSourceICYStream::AudioFileSourceICYStream()
|
||||
{
|
||||
pos = 0;
|
||||
reconnectTries = 0;
|
||||
saveURL = NULL;
|
||||
saveURL[0] = 0;
|
||||
}
|
||||
|
||||
AudioFileSourceICYStream::AudioFileSourceICYStream(const char *url)
|
||||
{
|
||||
saveURL = NULL;
|
||||
saveURL[0] = 0;
|
||||
reconnectTries = 0;
|
||||
open(url);
|
||||
}
|
||||
@@ -56,8 +56,8 @@ bool AudioFileSourceICYStream::open(const char *url)
|
||||
}
|
||||
icyByteCount = 0;
|
||||
size = http.getSize();
|
||||
free(saveURL); saveURL = NULL;
|
||||
saveURL = strdup(url);
|
||||
strncpy(saveURL, url, sizeof(saveURL));
|
||||
saveURL[sizeof(saveURL)-1] = 0;
|
||||
return true;
|
||||
}
|
||||
|
||||
|
||||
@@ -50,20 +50,20 @@ AudioFileSourceSTDIO::~AudioFileSourceSTDIO()
|
||||
|
||||
uint32_t AudioFileSourceSTDIO::read(void *data, uint32_t len)
|
||||
{
|
||||
if (rand() % 100 == 69) { // Give 0 data 1%
|
||||
printf("0 read\n");
|
||||
len = 0;
|
||||
} else if (rand() % 100 == 1) { // Give short reads 1%
|
||||
printf("0 read\n");
|
||||
len = 0;
|
||||
}
|
||||
// if (rand() % 100 == 69) { // Give 0 data 1%
|
||||
// printf("0 read\n");
|
||||
// len = 0;
|
||||
// } else if (rand() % 100 == 1) { // Give short reads 1%
|
||||
// printf("0 read\n");
|
||||
// len = 0;
|
||||
// }
|
||||
int ret = fread(reinterpret_cast<uint8_t*>(data), 1, len, f);
|
||||
if (ret && rand() % 100 < 5 ) {
|
||||
// We're really mean...throw bad data in the mix
|
||||
printf("bad data\n");
|
||||
for (int i=0; i<100; i++)
|
||||
*(reinterpret_cast<uint8_t*>(data) + (rand() % ret)) = rand();
|
||||
}
|
||||
// if (ret && rand() % 100 < 5 ) {
|
||||
// // We're really mean...throw bad data in the mix
|
||||
// printf("bad data\n");
|
||||
// for (int i=0; i<100; i++)
|
||||
// *(reinterpret_cast<uint8_t*>(data) + (rand() % ret)) = rand();
|
||||
// }
|
||||
return ret;
|
||||
}
|
||||
|
||||
|
||||
@@ -24,17 +24,26 @@
|
||||
|
||||
AudioGeneratorAAC::AudioGeneratorAAC()
|
||||
{
|
||||
preallocateSpace = NULL;
|
||||
preallocateSize = 0;
|
||||
|
||||
running = false;
|
||||
file = NULL;
|
||||
output = NULL;
|
||||
|
||||
buff = (uint8_t*)malloc(buffLen);
|
||||
outSample = (int16_t*)malloc(1024 * 2 * sizeof(uint16_t));
|
||||
if (!buff || !outSample) {
|
||||
Serial.printf_P(PSTR("ERROR: Out of memory in AAC\n"));
|
||||
Serial.flush();
|
||||
}
|
||||
|
||||
hAACDecoder = AACInitDecoder();
|
||||
if (!hAACDecoder) {
|
||||
Serial.printf_P(PSTR("Out of memory error! hAACDecoder==NULL\n"));
|
||||
Serial.flush();
|
||||
}
|
||||
// For sanity's sake...
|
||||
memset(buff, 0, sizeof(buff));
|
||||
memset(outSample, 0, sizeof(outSample));
|
||||
|
||||
buffValid = 0;
|
||||
lastFrameEnd = 0;
|
||||
validSamples = 0;
|
||||
@@ -43,9 +52,48 @@ AudioGeneratorAAC::AudioGeneratorAAC()
|
||||
lastChannels = 0;
|
||||
}
|
||||
|
||||
AudioGeneratorAAC::AudioGeneratorAAC(void *preallocateData, int preallocateSz)
|
||||
{
|
||||
preallocateSpace = preallocateData;
|
||||
preallocateSize = preallocateSz;
|
||||
|
||||
running = false;
|
||||
file = NULL;
|
||||
output = NULL;
|
||||
|
||||
uint8_t *p = (uint8_t*)preallocateSpace;
|
||||
buff = (uint8_t*) p;
|
||||
p += (buffLen + 7) & ~7;
|
||||
outSample = (int16_t*) p;
|
||||
p += (1024 * 2 * sizeof(int16_t) + 7) & ~7;
|
||||
int used = p - (uint8_t*)preallocateSpace;
|
||||
int availSpace = preallocateSize - used;
|
||||
if (availSpace < 0 ) {
|
||||
Serial.printf_P(PSTR("ERROR: Out of memory in AAC\n"));
|
||||
}
|
||||
|
||||
hAACDecoder = AACInitDecoderPre(p, availSpace);
|
||||
if (!hAACDecoder) {
|
||||
Serial.printf_P(PSTR("Out of memory error! hAACDecoder==NULL\n"));
|
||||
Serial.flush();
|
||||
}
|
||||
buffValid = 0;
|
||||
lastFrameEnd = 0;
|
||||
validSamples = 0;
|
||||
curSample = 0;
|
||||
lastRate = 0;
|
||||
lastChannels = 0;
|
||||
}
|
||||
|
||||
|
||||
|
||||
AudioGeneratorAAC::~AudioGeneratorAAC()
|
||||
{
|
||||
AACFreeDecoder(hAACDecoder);
|
||||
if (!preallocateSpace) {
|
||||
AACFreeDecoder(hAACDecoder);
|
||||
free(buff);
|
||||
free(outSample);
|
||||
}
|
||||
}
|
||||
|
||||
bool AudioGeneratorAAC::stop()
|
||||
@@ -71,10 +119,10 @@ bool AudioGeneratorAAC::FillBufferWithValidFrame()
|
||||
if (nextSync == -1) {
|
||||
if (buff[buffValid-1]==0xff) { // Could be 1st half of syncword, preserve it...
|
||||
buff[0] = 0xff;
|
||||
buffValid = file->read(buff+1, sizeof(buff)-1);
|
||||
buffValid = file->read(buff+1, buffLen-1);
|
||||
if (buffValid==0) return false; // No data available, EOF
|
||||
} else { // Try a whole new buffer
|
||||
buffValid = file->read(buff, sizeof(buff));
|
||||
buffValid = file->read(buff, buffLen-1);
|
||||
if (buffValid==0) return false; // No data available, EOF
|
||||
}
|
||||
}
|
||||
@@ -85,7 +133,7 @@ bool AudioGeneratorAAC::FillBufferWithValidFrame()
|
||||
memmove(buff, buff+nextSync, buffValid);
|
||||
|
||||
// We have a sync word at 0 now, try and fill remainder of buffer
|
||||
buffValid += file->read(buff + buffValid, sizeof(buff) - buffValid);
|
||||
buffValid += file->read(buff + buffValid, buffLen - buffValid);
|
||||
|
||||
return true;
|
||||
}
|
||||
@@ -152,7 +200,12 @@ bool AudioGeneratorAAC::begin(AudioFileSource *source, AudioOutput *output)
|
||||
|
||||
// AAC always comes out at 16 bits
|
||||
output->SetBitsPerSample(16);
|
||||
|
||||
|
||||
|
||||
memset(buff, 0, buffLen);
|
||||
memset(outSample, 0, 1024*2*sizeof(int16_t));
|
||||
|
||||
|
||||
running = true;
|
||||
|
||||
return true;
|
||||
|
||||
@@ -28,6 +28,7 @@ class AudioGeneratorAAC : public AudioGenerator
|
||||
{
|
||||
public:
|
||||
AudioGeneratorAAC();
|
||||
AudioGeneratorAAC(void *preallocateData, int preallocateSize);
|
||||
virtual ~AudioGeneratorAAC() override;
|
||||
virtual bool begin(AudioFileSource *source, AudioOutput *output) override;
|
||||
virtual bool loop() override;
|
||||
@@ -35,17 +36,21 @@ class AudioGeneratorAAC : public AudioGenerator
|
||||
virtual bool isRunning() override;
|
||||
|
||||
protected:
|
||||
void *preallocateSpace;
|
||||
int preallocateSize;
|
||||
|
||||
// Helix AAC decoder
|
||||
HAACDecoder hAACDecoder;
|
||||
|
||||
// Input buffering
|
||||
uint8_t buff[1600]; // File buffer required to store at least a whole compressed frame
|
||||
const int buffLen = 1600;
|
||||
uint8_t *buff; //[1600]; // File buffer required to store at least a whole compressed frame
|
||||
int16_t buffValid;
|
||||
int16_t lastFrameEnd;
|
||||
bool FillBufferWithValidFrame(); // Read until we get a valid syncword and min(feof, 2048) butes in the buffer
|
||||
|
||||
// Output buffering
|
||||
int16_t outSample[1024 * 2]; // Interleaved L/R
|
||||
int16_t *outSample; //[1024 * 2]; // Interleaved L/R
|
||||
int16_t validSamples;
|
||||
int16_t curSample;
|
||||
|
||||
|
||||
+99
-48
@@ -26,38 +26,57 @@ AudioGeneratorMP3::AudioGeneratorMP3()
|
||||
running = false;
|
||||
file = NULL;
|
||||
output = NULL;
|
||||
buffLen = 1500; // Max theoretical frame of 1441 + 8 guard bytes, with some fluff
|
||||
buff = NULL;
|
||||
nsCountMax = 1152/32;
|
||||
madInitted = false;
|
||||
preallocateSpace = NULL;
|
||||
preallocateSize = 0;
|
||||
}
|
||||
|
||||
AudioGeneratorMP3::AudioGeneratorMP3(void *space, int size)
|
||||
{
|
||||
running = false;
|
||||
file = NULL;
|
||||
output = NULL;
|
||||
buff = NULL;
|
||||
nsCountMax = 1152/32;
|
||||
madInitted = false;
|
||||
preallocateSpace = space;
|
||||
preallocateSize = size;
|
||||
}
|
||||
|
||||
AudioGeneratorMP3::~AudioGeneratorMP3()
|
||||
{
|
||||
free(buff);
|
||||
buff = NULL;
|
||||
}
|
||||
|
||||
bool AudioGeneratorMP3::SetBufferSize(int sz)
|
||||
{
|
||||
if (running) return false;
|
||||
buffLen = sz;
|
||||
return true;
|
||||
if (!preallocateSpace) {
|
||||
free(buff);
|
||||
free(synth);
|
||||
free(frame);
|
||||
free(stream);
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
bool AudioGeneratorMP3::stop()
|
||||
{
|
||||
free(buff);
|
||||
buff = NULL;
|
||||
|
||||
if (madInitted) {
|
||||
mad_synth_finish(&synth);
|
||||
mad_frame_finish(&frame);
|
||||
mad_stream_finish(&stream);
|
||||
mad_synth_finish(synth);
|
||||
mad_frame_finish(frame);
|
||||
mad_stream_finish(stream);
|
||||
madInitted = false;
|
||||
}
|
||||
|
||||
if (!preallocateSpace) {
|
||||
free(buff);
|
||||
free(synth);
|
||||
free(frame);
|
||||
free(stream);
|
||||
}
|
||||
|
||||
buff = NULL;
|
||||
synth = NULL;
|
||||
frame = NULL;
|
||||
stream = NULL;
|
||||
|
||||
running = false;
|
||||
output->stop();
|
||||
return file->close();
|
||||
@@ -74,13 +93,13 @@ enum mad_flow AudioGeneratorMP3::ErrorToFlow()
|
||||
char errLine[128];
|
||||
|
||||
// Special case - eat "lost sync @ byte 0" as it always occurs and is not really correct....it never had sync!
|
||||
if ((lastReadPos==0) && (stream.error==MAD_ERROR_LOSTSYNC)) return MAD_FLOW_CONTINUE;
|
||||
if ((lastReadPos==0) && (stream->error==MAD_ERROR_LOSTSYNC)) return MAD_FLOW_CONTINUE;
|
||||
|
||||
strcpy_P(err, mad_stream_errorstr(&stream));
|
||||
strcpy_P(err, mad_stream_errorstr(stream));
|
||||
snprintf_P(errLine, sizeof(errLine), PSTR("Decoding error '%s' at byte offset %d"),
|
||||
err, (stream.this_frame - buff) + lastReadPos);
|
||||
err, (stream->this_frame - buff) + lastReadPos);
|
||||
yield(); // Something bad happened anyway, ensure WiFi gets some time, too
|
||||
cb.st(stream.error, errLine);
|
||||
cb.st(stream->error, errLine);
|
||||
return MAD_FLOW_CONTINUE;
|
||||
}
|
||||
|
||||
@@ -88,10 +107,10 @@ enum mad_flow AudioGeneratorMP3::Input()
|
||||
{
|
||||
int unused = 0;
|
||||
|
||||
if (stream.next_frame) {
|
||||
unused = buffLen - (stream.next_frame - buff);
|
||||
memmove(buff, stream.next_frame, unused);
|
||||
stream.next_frame = NULL;
|
||||
if (stream->next_frame) {
|
||||
unused = buffLen - (stream->next_frame - buff);
|
||||
memmove(buff, stream->next_frame, unused);
|
||||
stream->next_frame = NULL;
|
||||
}
|
||||
if (unused == buffLen) {
|
||||
// Something wicked this way came, throw it all out and try again
|
||||
@@ -106,7 +125,7 @@ enum mad_flow AudioGeneratorMP3::Input()
|
||||
return MAD_FLOW_STOP;
|
||||
}
|
||||
|
||||
mad_stream_buffer(&stream, buff, len + unused);
|
||||
mad_stream_buffer(stream, buff, len + unused);
|
||||
|
||||
return MAD_FLOW_CONTINUE;
|
||||
}
|
||||
@@ -114,34 +133,34 @@ enum mad_flow AudioGeneratorMP3::Input()
|
||||
|
||||
bool AudioGeneratorMP3::DecodeNextFrame()
|
||||
{
|
||||
if (mad_frame_decode(&frame, &stream) == -1) {
|
||||
if (mad_frame_decode(frame, stream) == -1) {
|
||||
ErrorToFlow(); // Always returns CONTINUE
|
||||
return false;
|
||||
}
|
||||
nsCountMax = MAD_NSBSAMPLES(&frame.header);
|
||||
nsCountMax = MAD_NSBSAMPLES(&frame->header);
|
||||
return true;
|
||||
}
|
||||
|
||||
bool AudioGeneratorMP3::GetOneSample(int16_t sample[2])
|
||||
{
|
||||
if (synth.pcm.samplerate != lastRate) {
|
||||
output->SetRate(synth.pcm.samplerate);
|
||||
lastRate = synth.pcm.samplerate;
|
||||
if (synth->pcm.samplerate != lastRate) {
|
||||
output->SetRate(synth->pcm.samplerate);
|
||||
lastRate = synth->pcm.samplerate;
|
||||
}
|
||||
if (synth.pcm.channels != lastChannels) {
|
||||
output->SetChannels(synth.pcm.channels);
|
||||
lastChannels = synth.pcm.channels;
|
||||
if (synth->pcm.channels != lastChannels) {
|
||||
output->SetChannels(synth->pcm.channels);
|
||||
lastChannels = synth->pcm.channels;
|
||||
}
|
||||
|
||||
// If we're here, we have one decoded frame and sent 0 or more samples out
|
||||
if (samplePtr < synth.pcm.length) {
|
||||
sample[AudioOutput::LEFTCHANNEL ] = synth.pcm.samples[0][samplePtr];
|
||||
sample[AudioOutput::RIGHTCHANNEL] = synth.pcm.samples[1][samplePtr];
|
||||
if (samplePtr < synth->pcm.length) {
|
||||
sample[AudioOutput::LEFTCHANNEL ] = synth->pcm.samples[0][samplePtr];
|
||||
sample[AudioOutput::RIGHTCHANNEL] = synth->pcm.samples[1][samplePtr];
|
||||
samplePtr++;
|
||||
} else {
|
||||
samplePtr = 0;
|
||||
|
||||
switch ( mad_synth_frame_onens(&synth, &frame, nsCount++) ) {
|
||||
switch ( mad_synth_frame_onens(synth, frame, nsCount++) ) {
|
||||
case MAD_FLOW_STOP:
|
||||
case MAD_FLOW_BREAK: Serial.printf_P(PSTR("msf1ns failed\n"));
|
||||
return false; // Either way we're done
|
||||
@@ -149,8 +168,8 @@ bool AudioGeneratorMP3::GetOneSample(int16_t sample[2])
|
||||
break; // Do nothing
|
||||
}
|
||||
// for IGNORE and CONTINUE, just play what we have now
|
||||
sample[AudioOutput::LEFTCHANNEL ] = synth.pcm.samples[0][samplePtr];
|
||||
sample[AudioOutput::RIGHTCHANNEL] = synth.pcm.samples[1][samplePtr];
|
||||
sample[AudioOutput::LEFTCHANNEL ] = synth->pcm.samples[0][samplePtr];
|
||||
sample[AudioOutput::RIGHTCHANNEL] = synth->pcm.samples[1][samplePtr];
|
||||
samplePtr++;
|
||||
}
|
||||
return true;
|
||||
@@ -168,7 +187,7 @@ bool AudioGeneratorMP3::loop()
|
||||
do
|
||||
{
|
||||
// Decode next frame if we're beyond the existing generated data
|
||||
if ( (samplePtr >= synth.pcm.length) && (nsCount >= nsCountMax) ) {
|
||||
if ( (samplePtr >= synth->pcm.length) && (nsCount >= nsCountMax) ) {
|
||||
retry:
|
||||
if (Input() == MAD_FLOW_STOP) {
|
||||
return false;
|
||||
@@ -216,17 +235,49 @@ bool AudioGeneratorMP3::begin(AudioFileSource *source, AudioOutput *output)
|
||||
// Where we are in generating one frame's data, set to invalid so we will run loop on first getsample()
|
||||
samplePtr = 9999;
|
||||
nsCount = 9999;
|
||||
synth.pcm.length = 0;
|
||||
lastRate = 0;
|
||||
lastChannels = 0;
|
||||
lastReadPos = 0;
|
||||
buff = reinterpret_cast<unsigned char *>(malloc(buffLen));
|
||||
if (!buff) return false;
|
||||
|
||||
mad_stream_init(&stream);
|
||||
mad_frame_init(&frame);
|
||||
mad_synth_init(&synth);
|
||||
mad_stream_options(&stream, 0); // TODO - add options support
|
||||
|
||||
// Allocate all large memory chunks
|
||||
if (preallocateSpace) {
|
||||
uint8_t *p = reinterpret_cast<uint8_t *>(preallocateSpace);
|
||||
buff = reinterpret_cast<unsigned char *>(p);
|
||||
p += (buffLen+7) & ~7;
|
||||
stream = reinterpret_cast<struct mad_stream *>(p);
|
||||
p += (sizeof(struct mad_stream)+7) & ~7;
|
||||
frame = reinterpret_cast<struct mad_frame *>(p);
|
||||
p += (sizeof(struct mad_frame)+7) & ~7;
|
||||
synth = reinterpret_cast<struct mad_synth *>(p);
|
||||
p += (sizeof(struct mad_synth)+7) & ~7;
|
||||
int neededBytes = p - reinterpret_cast<uint8_t *>(preallocateSpace);
|
||||
if (neededBytes > preallocateSize) {
|
||||
Serial.printf_P("OOM error in MP3: Want %d bytes, have %d bytes preallocated.\n", neededBytes, preallocateSize);
|
||||
return false;
|
||||
}
|
||||
} else {
|
||||
buff = reinterpret_cast<unsigned char *>(malloc(buffLen));
|
||||
stream = reinterpret_cast<struct mad_stream *>(malloc(sizeof(struct mad_stream)));
|
||||
frame = reinterpret_cast<struct mad_frame *>(malloc(sizeof(struct mad_frame)));
|
||||
synth = reinterpret_cast<struct mad_synth *>(malloc(sizeof(struct mad_synth)));
|
||||
if (!buff || !stream || !frame || !synth) {
|
||||
free(buff);
|
||||
free(stream);
|
||||
free(frame);
|
||||
free(synth);
|
||||
buff = NULL;
|
||||
stream = NULL;
|
||||
frame = NULL;
|
||||
synth = NULL;
|
||||
return false;
|
||||
}
|
||||
}
|
||||
|
||||
mad_stream_init(stream);
|
||||
mad_frame_init(frame);
|
||||
mad_synth_init(synth);
|
||||
synth->pcm.length = 0;
|
||||
mad_stream_options(stream, 0); // TODO - add options support
|
||||
madInitted = true;
|
||||
|
||||
running = true;
|
||||
|
||||
@@ -29,15 +29,18 @@ class AudioGeneratorMP3 : public AudioGenerator
|
||||
{
|
||||
public:
|
||||
AudioGeneratorMP3();
|
||||
AudioGeneratorMP3(void *preallocateSpace, int preallocateSize);
|
||||
virtual ~AudioGeneratorMP3() override;
|
||||
virtual bool begin(AudioFileSource *source, AudioOutput *output) override;
|
||||
virtual bool loop() override;
|
||||
virtual bool stop() override;
|
||||
virtual bool isRunning() override;
|
||||
bool SetBufferSize(int sz);
|
||||
|
||||
protected:
|
||||
int buffLen;
|
||||
void *preallocateSpace;
|
||||
int preallocateSize;
|
||||
|
||||
const int buffLen = 0x600; // Slightly larger than largest MP3 frame
|
||||
unsigned char *buff;
|
||||
int lastReadPos;
|
||||
unsigned int lastRate;
|
||||
@@ -45,9 +48,9 @@ class AudioGeneratorMP3 : public AudioGenerator
|
||||
|
||||
// Decoding bits
|
||||
bool madInitted;
|
||||
struct mad_stream stream;
|
||||
struct mad_frame frame;
|
||||
struct mad_synth synth;
|
||||
struct mad_stream *stream;
|
||||
struct mad_frame *frame;
|
||||
struct mad_synth *synth;
|
||||
int samplePtr;
|
||||
int nsCount;
|
||||
int nsCountMax;
|
||||
|
||||
@@ -157,6 +157,7 @@ typedef struct _AACDecInfo {
|
||||
|
||||
/* decoder functions which must be implemented for each platform */
|
||||
AACDecInfo *AllocateBuffers(void);
|
||||
AACDecInfo *AllocateBuffersPre(void **space, int *len);
|
||||
void FreeBuffers(AACDecInfo *aacDecInfo);
|
||||
void ClearBuffer(void *buf, int nBytes);
|
||||
|
||||
@@ -179,6 +180,7 @@ int IMDCT(AACDecInfo *aacDecInfo, int ch, int chBase, short *outbuf);
|
||||
|
||||
/* SBR specific functions */
|
||||
int InitSBR(AACDecInfo *aacDecInfo);
|
||||
int InitSBRPre(AACDecInfo *aacDecInfo, void **ptr, int *sz);
|
||||
void FreeSBR(AACDecInfo *aacDecInfo);
|
||||
int DecodeSBRBitstream(AACDecInfo *aacDecInfo, int chBase);
|
||||
int DecodeSBRData(AACDecInfo *aacDecInfo, int chBase, short *outbuf);
|
||||
|
||||
@@ -81,6 +81,23 @@ HAACDecoder AACInitDecoder(void)
|
||||
return (HAACDecoder)aacDecInfo;
|
||||
}
|
||||
|
||||
HAACDecoder AACInitDecoderPre(void *ptr, int sz)
|
||||
{
|
||||
AACDecInfo *aacDecInfo;
|
||||
|
||||
aacDecInfo = AllocateBuffersPre(&ptr, &sz);
|
||||
if (!aacDecInfo)
|
||||
return 0;
|
||||
|
||||
#ifdef AAC_ENABLE_SBR
|
||||
if (InitSBRPre(aacDecInfo, &ptr, &sz)) {
|
||||
return 0;
|
||||
}
|
||||
#endif
|
||||
|
||||
return (HAACDecoder)aacDecInfo;
|
||||
}
|
||||
|
||||
/**************************************************************************************
|
||||
* Function: AACFreeDecoder
|
||||
*
|
||||
|
||||
@@ -152,6 +152,7 @@ typedef void *HAACDecoder;
|
||||
|
||||
/* public C API */
|
||||
HAACDecoder AACInitDecoder(void);
|
||||
HAACDecoder AACInitDecoderPre(void *ptr, int sz);
|
||||
void AACFreeDecoder(HAACDecoder hAACDecoder);
|
||||
int AACDecode(HAACDecoder hAACDecoder, unsigned char **inbuf, int *bytesLeft, short *outbuf);
|
||||
|
||||
|
||||
@@ -113,6 +113,31 @@ AACDecInfo *AllocateBuffers(void)
|
||||
return aacDecInfo;
|
||||
}
|
||||
|
||||
AACDecInfo *AllocateBuffersPre(void **ptr, int *sz)
|
||||
{
|
||||
AACDecInfo *aacDecInfo;
|
||||
|
||||
char *p = (char*)*ptr;
|
||||
aacDecInfo = (AACDecInfo *)p;
|
||||
p += (sizeof(AACDecInfo) +7 ) & ~7;
|
||||
*sz -= (sizeof(AACDecInfo) +7 ) & ~7;
|
||||
if (*sz < 0)
|
||||
return 0;
|
||||
ClearBuffer(aacDecInfo, sizeof(AACDecInfo));
|
||||
|
||||
aacDecInfo->psInfoBase = (PSInfoBase*)p;
|
||||
p += (sizeof(PSInfoBase) + 7) & ~7;
|
||||
*sz -= (sizeof(PSInfoBase) + 7) & ~7;
|
||||
if (*sz <0) {
|
||||
return 0;
|
||||
}
|
||||
ClearBuffer(aacDecInfo->psInfoBase, sizeof(PSInfoBase));
|
||||
|
||||
*ptr = p;
|
||||
|
||||
return aacDecInfo;
|
||||
}
|
||||
|
||||
#ifndef SAFE_FREE
|
||||
#define SAFE_FREE(x) {if (x) free(x); (x) = 0;} /* helper macro */
|
||||
#endif
|
||||
|
||||
@@ -114,6 +114,29 @@ int InitSBR(AACDecInfo *aacDecInfo)
|
||||
return ERR_AAC_NONE;
|
||||
}
|
||||
|
||||
int InitSBRPre(AACDecInfo *aacDecInfo, void **ptr, int *sz)
|
||||
{
|
||||
PSInfoSBR *psi;
|
||||
|
||||
if (!aacDecInfo)
|
||||
return ERR_AAC_NULL_POINTER;
|
||||
|
||||
/* allocate SBR state structure */
|
||||
psi = (PSInfoSBR *)*ptr;
|
||||
*sz -= sizeof(PSInfoSBR);
|
||||
if (*sz < 0) {
|
||||
printf("OOM in SBR, can't allocate %d bytes\n", sizeof(PSInfoSBR));
|
||||
return ERR_AAC_SBR_INIT;
|
||||
}
|
||||
InitSBRState(psi);
|
||||
|
||||
*ptr = (void*)((char*)(*ptr) + sizeof(PSInfoSBR));
|
||||
aacDecInfo->psInfoSBR = psi;
|
||||
return ERR_AAC_NONE;
|
||||
}
|
||||
|
||||
|
||||
|
||||
/**************************************************************************************
|
||||
* Function: FreeSBR
|
||||
*
|
||||
|
||||
+4
-12
@@ -95,7 +95,6 @@ void mad_frame_init(struct mad_frame *frame)
|
||||
|
||||
frame->options = 0;
|
||||
|
||||
frame->overlap = 0;
|
||||
mad_frame_mute(frame);
|
||||
}
|
||||
|
||||
@@ -106,11 +105,6 @@ void mad_frame_init(struct mad_frame *frame)
|
||||
void mad_frame_finish(struct mad_frame *frame)
|
||||
{
|
||||
mad_header_finish(&frame->header);
|
||||
|
||||
if (frame->overlap) {
|
||||
free(frame->overlap);
|
||||
frame->overlap = 0;
|
||||
}
|
||||
}
|
||||
|
||||
/*
|
||||
@@ -498,12 +492,10 @@ void mad_frame_mute(struct mad_frame *frame)
|
||||
}
|
||||
}
|
||||
|
||||
if (frame->overlap) {
|
||||
for (s = 0; s < 18; ++s) {
|
||||
for (sb = 0; sb < 32; ++sb) {
|
||||
(*frame->overlap)[0][sb][s] =
|
||||
(*frame->overlap)[1][sb][s] = 0;
|
||||
}
|
||||
for (s = 0; s < 18; ++s) {
|
||||
for (sb = 0; sb < 32; ++sb) {
|
||||
frame->overlap[0][sb][s] =
|
||||
frame->overlap[1][sb][s] = 0;
|
||||
}
|
||||
}
|
||||
}
|
||||
+1
-1
@@ -70,7 +70,7 @@ struct mad_frame {
|
||||
int options; /* decoding options (from stream) */
|
||||
|
||||
mad_fixed_t sbsample[2][36][32]; /* synthesis subband filter samples */
|
||||
mad_fixed_t (*overlap)[2][32][18]; /* Layer III block overlap data */
|
||||
mad_fixed_t overlap[2][32][18]; /* Layer III block overlap data */
|
||||
|
||||
mad_fixed_t xr_raw[576*2];
|
||||
mad_fixed_t tmp[576];
|
||||
|
||||
+11
-28
@@ -2537,14 +2537,14 @@ enum mad_error III_decode(struct mad_bitptr *ptr, struct mad_frame *frame,
|
||||
/* long blocks */
|
||||
for (sb = 0; sb < 2; ++sb, l += 18) {
|
||||
III_imdct_l(&xr[ch][l], output, block_type);
|
||||
III_overlap(output, (*frame->overlap)[ch][sb], sample, sb);
|
||||
III_overlap(output, frame->overlap[ch][sb], sample, sb);
|
||||
}
|
||||
}
|
||||
else {
|
||||
/* short blocks */
|
||||
for (sb = 0; sb < 2; ++sb, l += 18) {
|
||||
III_imdct_s(&xr[ch][l], output);
|
||||
III_overlap(output, (*frame->overlap)[ch][sb], sample, sb);
|
||||
III_overlap(output, frame->overlap[ch][sb], sample, sb);
|
||||
}
|
||||
}
|
||||
|
||||
@@ -2562,7 +2562,7 @@ enum mad_error III_decode(struct mad_bitptr *ptr, struct mad_frame *frame,
|
||||
/* long blocks */
|
||||
for (sb = 2; sb < sblimit; ++sb, l += 18) {
|
||||
III_imdct_l(&xr[ch][l], output, channel->block_type);
|
||||
III_overlap(output, (*frame->overlap)[ch][sb], sample, sb);
|
||||
III_overlap(output, frame->overlap[ch][sb], sample, sb);
|
||||
|
||||
if (sb & 1)
|
||||
III_freqinver(sample, sb);
|
||||
@@ -2572,7 +2572,7 @@ enum mad_error III_decode(struct mad_bitptr *ptr, struct mad_frame *frame,
|
||||
/* short blocks */
|
||||
for (sb = 2; sb < sblimit; ++sb, l += 18) {
|
||||
III_imdct_s(&xr[ch][l], output);
|
||||
III_overlap(output, (*frame->overlap)[ch][sb], sample, sb);
|
||||
III_overlap(output, frame->overlap[ch][sb], sample, sb);
|
||||
|
||||
if (sb & 1)
|
||||
III_freqinver(sample, sb);
|
||||
@@ -2582,7 +2582,7 @@ enum mad_error III_decode(struct mad_bitptr *ptr, struct mad_frame *frame,
|
||||
/* remaining (zero) subbands */
|
||||
|
||||
for (sb = sblimit; sb < 32; ++sb) {
|
||||
III_overlap_z((*frame->overlap)[ch][sb], sample, sb);
|
||||
III_overlap_z(frame->overlap[ch][sb], sample, sb);
|
||||
|
||||
if (sb & 1)
|
||||
III_freqinver(sample, sb);
|
||||
@@ -2609,23 +2609,6 @@ int mad_layer_III(struct mad_stream *stream, struct mad_frame *frame)
|
||||
enum mad_error error;
|
||||
int result = 0;
|
||||
stack(__FUNCTION__, __FILE__, __LINE__);
|
||||
/* allocate Layer III dynamic structures */
|
||||
|
||||
if (stream->main_data == 0) {
|
||||
stream->main_data = malloc(MAD_BUFFER_MDLEN);
|
||||
if (stream->main_data == 0) {
|
||||
stream->error = MAD_ERROR_NOMEM;
|
||||
return -1;
|
||||
}
|
||||
}
|
||||
|
||||
if (frame->overlap == 0) {
|
||||
frame->overlap = calloc(2 * 32 * 18, sizeof(mad_fixed_t));
|
||||
if (frame->overlap == 0) {
|
||||
stream->error = MAD_ERROR_NOMEM;
|
||||
return -1;
|
||||
}
|
||||
}
|
||||
|
||||
nch = MAD_NCHANNELS(header);
|
||||
si_len = (header->flags & MAD_FLAG_LSF_EXT) ?
|
||||
@@ -2711,13 +2694,13 @@ int mad_layer_III(struct mad_stream *stream, struct mad_frame *frame)
|
||||
}
|
||||
else {
|
||||
mad_bit_init(&ptr,
|
||||
*stream->main_data + stream->md_len - si.main_data_begin);
|
||||
stream->main_data + stream->md_len - si.main_data_begin);
|
||||
|
||||
if (md_len > si.main_data_begin) {
|
||||
assert(stream->md_len + md_len -
|
||||
si.main_data_begin <= MAD_BUFFER_MDLEN);
|
||||
|
||||
memcpy(*stream->main_data + stream->md_len,
|
||||
memcpy(stream->main_data + stream->md_len,
|
||||
mad_bit_nextbyte(&stream->ptr),
|
||||
frame_used = md_len - si.main_data_begin);
|
||||
stream->md_len += frame_used;
|
||||
@@ -2753,7 +2736,7 @@ int mad_layer_III(struct mad_stream *stream, struct mad_frame *frame)
|
||||
/* preload main_data buffer with up to 511 bytes for next frame(s) */
|
||||
|
||||
if (frame_free >= next_md_begin) {
|
||||
memcpy(*stream->main_data,
|
||||
memcpy(stream->main_data,
|
||||
stream->next_frame - next_md_begin, next_md_begin);
|
||||
stream->md_len = next_md_begin;
|
||||
}
|
||||
@@ -2766,15 +2749,15 @@ int mad_layer_III(struct mad_stream *stream, struct mad_frame *frame)
|
||||
extra = next_md_begin - frame_free;
|
||||
|
||||
if (extra < stream->md_len) {
|
||||
memmove(*stream->main_data,
|
||||
*stream->main_data + stream->md_len - extra, extra);
|
||||
memmove(stream->main_data,
|
||||
stream->main_data + stream->md_len - extra, extra);
|
||||
stream->md_len = extra;
|
||||
}
|
||||
}
|
||||
else
|
||||
stream->md_len = 0;
|
||||
|
||||
memcpy(*stream->main_data + stream->md_len,
|
||||
memcpy(stream->main_data + stream->md_len,
|
||||
stream->next_frame - frame_free, frame_free);
|
||||
stream->md_len += frame_free;
|
||||
}
|
||||
|
||||
+2
-2
@@ -706,7 +706,7 @@ struct mad_stream {
|
||||
struct mad_bitptr anc_ptr; /* ancillary bits pointer */
|
||||
unsigned int anc_bitlen; /* number of ancillary bits */
|
||||
|
||||
unsigned char (*main_data)[MAD_BUFFER_MDLEN];
|
||||
unsigned char main_data[MAD_BUFFER_MDLEN];
|
||||
/* Layer III main_data() */
|
||||
unsigned int md_len; /* bytes in main_data */
|
||||
|
||||
@@ -790,7 +790,7 @@ struct mad_frame {
|
||||
int options; /* decoding options (from stream) */
|
||||
|
||||
mad_fixed_t sbsample[2][36][32]; /* synthesis subband filter samples */
|
||||
mad_fixed_t (*overlap)[2][32][18]; /* Layer III block overlap data */
|
||||
mad_fixed_t overlap[2][32][18]; /* Layer III block overlap data */
|
||||
|
||||
mad_fixed_t xr_raw[576*2];
|
||||
mad_fixed_t tmp[576];
|
||||
|
||||
@@ -52,7 +52,6 @@ stack(__FUNCTION__, __FILE__, __LINE__);
|
||||
mad_bit_init(&stream->anc_ptr, 0);
|
||||
stream->anc_bitlen = 0;
|
||||
|
||||
stream->main_data = 0;
|
||||
stream->md_len = 0;
|
||||
|
||||
stream->options = 0;
|
||||
@@ -66,10 +65,6 @@ stack(__FUNCTION__, __FILE__, __LINE__);
|
||||
void mad_stream_finish(struct mad_stream *stream)
|
||||
{
|
||||
stack(__FUNCTION__, __FILE__, __LINE__);
|
||||
if (stream->main_data) {
|
||||
free(stream->main_data);
|
||||
stream->main_data = 0;
|
||||
}
|
||||
|
||||
mad_bit_finish(&stream->anc_ptr);
|
||||
mad_bit_finish(&stream->ptr);
|
||||
|
||||
+1
-1
@@ -73,7 +73,7 @@ struct mad_stream {
|
||||
struct mad_bitptr anc_ptr; /* ancillary bits pointer */
|
||||
unsigned int anc_bitlen; /* number of ancillary bits */
|
||||
|
||||
unsigned char (*main_data)[MAD_BUFFER_MDLEN];
|
||||
unsigned char main_data[MAD_BUFFER_MDLEN];
|
||||
/* Layer III main_data() */
|
||||
unsigned int md_len; /* bytes in main_data */
|
||||
|
||||
|
||||
+2
-1
@@ -10,7 +10,8 @@ int main(int argc, char **argv)
|
||||
AudioFileSourceSTDIO *in = new AudioFileSourceSTDIO("jamonit.aac");
|
||||
AudioOutputSTDIO *out = new AudioOutputSTDIO();
|
||||
out->SetFilename("jamonit.aac.wav");
|
||||
AudioGeneratorAAC *aac = new AudioGeneratorAAC();
|
||||
void *space = malloc(28000+60000);
|
||||
AudioGeneratorAAC *aac = new AudioGeneratorAAC(space, 28000+60000);
|
||||
|
||||
aac->begin(in, out);
|
||||
while (aac->loop()) { /*noop*/ }
|
||||
|
||||
+3
-1
@@ -51,12 +51,14 @@ int main(int argc, char **argv)
|
||||
id3->RegisterMetadataCB(MDCallback, (void*)"ID3TAG");
|
||||
AudioOutputSTDIO *out = new AudioOutputSTDIO();
|
||||
out->SetFilename("jamonit.wav");
|
||||
AudioGeneratorMP3 *mp3 = new AudioGeneratorMP3();
|
||||
void *space = malloc(29192);
|
||||
AudioGeneratorMP3 *mp3 = new AudioGeneratorMP3(space, 29192);
|
||||
|
||||
mp3->begin(id3, out);
|
||||
while (mp3->loop()) { /*noop*/ }
|
||||
mp3->stop();
|
||||
|
||||
free(space);
|
||||
delete mp3;
|
||||
delete out;
|
||||
delete id3;
|
||||
|
||||
Reference in new issue
Block a user