From 8ffebe64966c37a54a40c206e5738a2e12244db1 Mon Sep 17 00:00:00 2001 From: "Earle F. Philhower, III" Date: Sun, 21 Jan 2018 10:38:23 -0800 Subject: [PATCH] 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. --- src/AudioFileSourceBuffer.cpp | 17 +++- src/AudioFileSourceBuffer.h | 2 + src/AudioFileSourceHTTPStream.cpp | 8 +- src/AudioFileSourceHTTPStream.h | 2 +- src/AudioFileSourceICYStream.cpp | 8 +- src/AudioFileSourceSTDIO.cpp | 26 +++--- src/AudioGeneratorAAC.cpp | 69 ++++++++++++-- src/AudioGeneratorAAC.h | 9 +- src/AudioGeneratorMP3.cpp | 147 ++++++++++++++++++++---------- src/AudioGeneratorMP3.h | 13 ++- src/libhelix-aac/aaccommon.h | 2 + src/libhelix-aac/aacdec.c | 17 ++++ src/libhelix-aac/aacdec.h | 1 + src/libhelix-aac/buffers.c | 25 +++++ src/libhelix-aac/sbr.c | 23 +++++ src/libmad/frame.c | 16 +--- src/libmad/frame.h | 2 +- src/libmad/layer3.c | 39 +++----- src/libmad/mad.h | 4 +- src/libmad/stream.c | 5 - src/libmad/stream.h | 2 +- tests/host/aac.cpp | 3 +- tests/host/mp3.cpp | 4 +- 23 files changed, 306 insertions(+), 138 deletions(-) diff --git a/src/AudioFileSourceBuffer.cpp b/src/AudioFileSourceBuffer.cpp index 306dcbd..b85efff 100644 --- a/src/AudioFileSourceBuffer.cpp +++ b/src/AudioFileSourceBuffer.cpp @@ -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(); } diff --git a/src/AudioFileSourceBuffer.h b/src/AudioFileSourceBuffer.h index 940d64f..fe1ac29 100644 --- a/src/AudioFileSourceBuffer.h +++ b/src/AudioFileSourceBuffer.h @@ -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; diff --git a/src/AudioFileSourceHTTPStream.cpp b/src/AudioFileSourceHTTPStream.cpp index 7320301..3a41f53 100644 --- a/src/AudioFileSourceHTTPStream.cpp +++ b/src/AudioFileSourceHTTPStream.cpp @@ -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; } diff --git a/src/AudioFileSourceHTTPStream.h b/src/AudioFileSourceHTTPStream.h index 2887167..b483e95 100644 --- a/src/AudioFileSourceHTTPStream.h +++ b/src/AudioFileSourceHTTPStream.h @@ -57,7 +57,7 @@ class AudioFileSourceHTTPStream : public AudioFileSource int size; int reconnectTries; int reconnectDelayMs; - char *saveURL; + char saveURL[128]; }; diff --git a/src/AudioFileSourceICYStream.cpp b/src/AudioFileSourceICYStream.cpp index e752c76..29b63fb 100644 --- a/src/AudioFileSourceICYStream.cpp +++ b/src/AudioFileSourceICYStream.cpp @@ -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; } diff --git a/src/AudioFileSourceSTDIO.cpp b/src/AudioFileSourceSTDIO.cpp index 2f727d4..75d0cd1 100644 --- a/src/AudioFileSourceSTDIO.cpp +++ b/src/AudioFileSourceSTDIO.cpp @@ -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(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(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(data) + (rand() % ret)) = rand(); +// } return ret; } diff --git a/src/AudioGeneratorAAC.cpp b/src/AudioGeneratorAAC.cpp index 67ad883..fded623 100644 --- a/src/AudioGeneratorAAC.cpp +++ b/src/AudioGeneratorAAC.cpp @@ -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; diff --git a/src/AudioGeneratorAAC.h b/src/AudioGeneratorAAC.h index 2b0681a..2184e7e 100644 --- a/src/AudioGeneratorAAC.h +++ b/src/AudioGeneratorAAC.h @@ -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; diff --git a/src/AudioGeneratorMP3.cpp b/src/AudioGeneratorMP3.cpp index 66484d6..6954a30 100644 --- a/src/AudioGeneratorMP3.cpp +++ b/src/AudioGeneratorMP3.cpp @@ -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(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(preallocateSpace); + buff = reinterpret_cast(p); + p += (buffLen+7) & ~7; + stream = reinterpret_cast(p); + p += (sizeof(struct mad_stream)+7) & ~7; + frame = reinterpret_cast(p); + p += (sizeof(struct mad_frame)+7) & ~7; + synth = reinterpret_cast(p); + p += (sizeof(struct mad_synth)+7) & ~7; + int neededBytes = p - reinterpret_cast(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(malloc(buffLen)); + stream = reinterpret_cast(malloc(sizeof(struct mad_stream))); + frame = reinterpret_cast(malloc(sizeof(struct mad_frame))); + synth = reinterpret_cast(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; diff --git a/src/AudioGeneratorMP3.h b/src/AudioGeneratorMP3.h index 3858050..674baab 100644 --- a/src/AudioGeneratorMP3.h +++ b/src/AudioGeneratorMP3.h @@ -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; diff --git a/src/libhelix-aac/aaccommon.h b/src/libhelix-aac/aaccommon.h index e167c8f..804337c 100644 --- a/src/libhelix-aac/aaccommon.h +++ b/src/libhelix-aac/aaccommon.h @@ -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); diff --git a/src/libhelix-aac/aacdec.c b/src/libhelix-aac/aacdec.c index e2c1031..cb0e504 100644 --- a/src/libhelix-aac/aacdec.c +++ b/src/libhelix-aac/aacdec.c @@ -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 * diff --git a/src/libhelix-aac/aacdec.h b/src/libhelix-aac/aacdec.h index 3b1d579..0f7d91f 100644 --- a/src/libhelix-aac/aacdec.h +++ b/src/libhelix-aac/aacdec.h @@ -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); diff --git a/src/libhelix-aac/buffers.c b/src/libhelix-aac/buffers.c index debdb93..5acc61b 100644 --- a/src/libhelix-aac/buffers.c +++ b/src/libhelix-aac/buffers.c @@ -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 diff --git a/src/libhelix-aac/sbr.c b/src/libhelix-aac/sbr.c index 657f1b3..ec04672 100644 --- a/src/libhelix-aac/sbr.c +++ b/src/libhelix-aac/sbr.c @@ -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 * diff --git a/src/libmad/frame.c b/src/libmad/frame.c index 92e7e8e..1ad1e64 100644 --- a/src/libmad/frame.c +++ b/src/libmad/frame.c @@ -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; } } } diff --git a/src/libmad/frame.h b/src/libmad/frame.h index 87cc5ef..b3dece6 100644 --- a/src/libmad/frame.h +++ b/src/libmad/frame.h @@ -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]; diff --git a/src/libmad/layer3.c b/src/libmad/layer3.c index f70e0ea..694ce2d 100644 --- a/src/libmad/layer3.c +++ b/src/libmad/layer3.c @@ -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; } diff --git a/src/libmad/mad.h b/src/libmad/mad.h index f1ae858..0ed3a16 100644 --- a/src/libmad/mad.h +++ b/src/libmad/mad.h @@ -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]; diff --git a/src/libmad/stream.c b/src/libmad/stream.c index 573d347..e959206 100644 --- a/src/libmad/stream.c +++ b/src/libmad/stream.c @@ -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); diff --git a/src/libmad/stream.h b/src/libmad/stream.h index 8bff8b4..8dc8ec1 100644 --- a/src/libmad/stream.h +++ b/src/libmad/stream.h @@ -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 */ diff --git a/tests/host/aac.cpp b/tests/host/aac.cpp index 3bb6891..0857925 100644 --- a/tests/host/aac.cpp +++ b/tests/host/aac.cpp @@ -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*/ } diff --git a/tests/host/mp3.cpp b/tests/host/mp3.cpp index 983c7c2..9f6cfe4 100644 --- a/tests/host/mp3.cpp +++ b/tests/host/mp3.cpp @@ -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;