From 2fb8d887b18401c863f5b56fbc6525a7dbc01d64 Mon Sep 17 00:00:00 2001 From: david gauchard Date: Sun, 6 Sep 2020 20:41:45 +0200 Subject: [PATCH] Fixes when seeking in an MP3 file (#303) * separate malloc structures * add desync() for user when randomly seeking in the input file fix corner cases leading to exceptions when seeking * additional test when trying to crash-shake everything with a rotary encoder seeking into the currently played file * use private instead of static * remove temporary debug code --- src/AudioGenerator.h | 1 + src/AudioGeneratorMP3.cpp | 78 +++++++++++++++++++++++++++++++++------ src/AudioGeneratorMP3.h | 26 ++++++++++--- 3 files changed, 89 insertions(+), 16 deletions(-) diff --git a/src/AudioGenerator.h b/src/AudioGenerator.h index 889b228..6d97487 100644 --- a/src/AudioGenerator.h +++ b/src/AudioGenerator.h @@ -35,6 +35,7 @@ class AudioGenerator virtual bool loop() { return false; }; virtual bool stop() { return false; }; virtual bool isRunning() { return false;}; + virtual void desync () { }; public: virtual bool RegisterMetadataCB(AudioStatus::metadataCBFn fn, void *data) { return cb.RegisterMetadataCB(fn, data); } diff --git a/src/AudioGeneratorMP3.cpp b/src/AudioGeneratorMP3.cpp index 44e3f6f..9cc90b7 100644 --- a/src/AudioGeneratorMP3.cpp +++ b/src/AudioGeneratorMP3.cpp @@ -29,11 +29,23 @@ AudioGeneratorMP3::AudioGeneratorMP3() buff = NULL; nsCountMax = 1152/32; madInitted = false; - preallocateSpace = NULL; - preallocateSize = 0; } -AudioGeneratorMP3::AudioGeneratorMP3(void *space, int size) +AudioGeneratorMP3::AudioGeneratorMP3(void *space, int size): preallocateSpace(space), preallocateSize(size) +{ + running = false; + file = NULL; + output = NULL; + buff = NULL; + nsCountMax = 1152/32; + madInitted = false; +} + +AudioGeneratorMP3::AudioGeneratorMP3(void *buff, int buffSize, void *stream, int streamSize, void *frame, int frameSize, void *synth, int synthSize): + preallocateSpace(buff), preallocateSize(buffSize), + preallocateStreamSpace(stream), preallocateStreamSize(streamSize), + preallocateFrameSpace(frame), preallocateFrameSize(frameSize), + preallocateSynthSpace(synth), preallocateSynthSize(synthSize) { running = false; file = NULL; @@ -41,8 +53,6 @@ AudioGeneratorMP3::AudioGeneratorMP3(void *space, int size) buff = NULL; nsCountMax = 1152/32; madInitted = false; - preallocateSpace = space; - preallocateSize = size; } AudioGeneratorMP3::~AudioGeneratorMP3() @@ -109,7 +119,12 @@ enum mad_flow AudioGeneratorMP3::Input() if (stream->next_frame) { unused = lastBuffLen - (stream->next_frame - buff); - memmove(buff, stream->next_frame, unused); + if (unused < 0) { + desync(); + unused = 0; + } else { + memmove(buff, stream->next_frame, unused); + } stream->next_frame = NULL; } @@ -125,6 +140,10 @@ enum mad_flow AudioGeneratorMP3::Input() // Can't read any from the file, and we don't have anything left. It's done.... return MAD_FLOW_STOP; } + if (len < 0) { + desync(); + unused = 0; + } lastBuffLen = len + unused; mad_stream_buffer(stream, buff, lastBuffLen); @@ -132,6 +151,16 @@ enum mad_flow AudioGeneratorMP3::Input() return MAD_FLOW_CONTINUE; } +void AudioGeneratorMP3::desync () +{ + audioLogger->printf_P(PSTR("MP3:desync\n")); + if (stream) { + stream->next_frame = nullptr; + stream->this_frame = nullptr; + stream->sync = 0; + } + lastBuffLen = 0; +} bool AudioGeneratorMP3::DecodeNextFrame() { @@ -196,6 +225,17 @@ retry: } if (!DecodeNextFrame()) { + if (stream->error == MAD_ERROR_BUFLEN) { + // randomly seeking can lead to endless + // and unrecoverable "MAD_ERROR_BUFLEN" loop + audioLogger->printf_P(PSTR("MP3:ERROR_BUFLEN %d\n"), unrecoverable); + if (++unrecoverable >= 3) { + unrecoverable = 0; + return (running = false); + } + } else { + unrecoverable = 0; + } goto retry; } samplePtr = 9999; @@ -243,16 +283,32 @@ bool AudioGeneratorMP3::begin(AudioFileSource *source, AudioOutput *output) lastBuffLen = 0; // Allocate all large memory chunks - if (preallocateSpace) { + if (preallocateStreamSize + preallocateFrameSize + preallocateSynthSize) { + if (preallocateSize >= preAllocBuffSize() && + preallocateStreamSize >= preAllocStreamSize() && + preallocateFrameSize >= preAllocFrameSize() && + preallocateSynthSize >= preAllocSynthSize()) { + buff = reinterpret_cast(preallocateSpace); + stream = reinterpret_cast(preallocateStreamSpace); + frame = reinterpret_cast(preallocateFrameSpace); + synth = reinterpret_cast(preallocateSynthSpace); + } + else { + audioLogger->printf_P("OOM error in MP3: Want %d/%d/%d/%d bytes, have %d/%d/%d/%d bytes preallocated.\n", + preAllocBuffSize(), preAllocStreamSize(), preAllocFrameSize(), preAllocSynthSize(), + preallocateSize, preallocateStreamSize, preallocateFrameSize, preallocateSynthSize); + return false; + } + } else if (preallocateSpace) { uint8_t *p = reinterpret_cast(preallocateSpace); buff = reinterpret_cast(p); - p += (buffLen+7) & ~7; + p += preAllocBuffSize(); stream = reinterpret_cast(p); - p += (sizeof(struct mad_stream)+7) & ~7; + p += preAllocStreamSize(); frame = reinterpret_cast(p); - p += (sizeof(struct mad_frame)+7) & ~7; + p += preAllocFrameSize(); synth = reinterpret_cast(p); - p += (sizeof(struct mad_synth)+7) & ~7; + p += preAllocSynthSize(); int neededBytes = p - reinterpret_cast(preallocateSpace); if (neededBytes > preallocateSize) { audioLogger->printf_P("OOM error in MP3: Want %d bytes, have %d bytes preallocated.\n", neededBytes, preallocateSize); diff --git a/src/AudioGeneratorMP3.h b/src/AudioGeneratorMP3.h index 750ceca..0f4e439 100644 --- a/src/AudioGeneratorMP3.h +++ b/src/AudioGeneratorMP3.h @@ -30,17 +30,31 @@ class AudioGeneratorMP3 : public AudioGenerator public: AudioGeneratorMP3(); AudioGeneratorMP3(void *preallocateSpace, int preallocateSize); + AudioGeneratorMP3(void *buff, int buffSize, void *stream, int streamSize, void *frame, int frameSize, void *synth, int synthSize); virtual ~AudioGeneratorMP3() override; virtual bool begin(AudioFileSource *source, AudioOutput *output) override; virtual bool loop() override; virtual bool stop() override; virtual bool isRunning() override; - - protected: - void *preallocateSpace; - int preallocateSize; + virtual void desync () override; - const int buffLen = 0x600; // Slightly larger than largest MP3 frame + static constexpr int preAllocSize () { return preAllocBuffSize() + preAllocStreamSize() + preAllocFrameSize() + preAllocSynthSize(); } + static constexpr int preAllocBuffSize () { return ((buffLen + 7) & ~7); } + static constexpr int preAllocStreamSize () { return ((sizeof(struct mad_stream) + 7) & ~7); } + static constexpr int preAllocFrameSize () { return (sizeof(struct mad_frame) + 7) & ~7; } + static constexpr int preAllocSynthSize () { return (sizeof(struct mad_synth) + 7) & ~7; } + + protected: + void *preallocateSpace = nullptr; + int preallocateSize = 0; + void *preallocateStreamSpace = nullptr; + int preallocateStreamSize = 0; + void *preallocateFrameSpace = nullptr; + int preallocateFrameSize = 0; + void *preallocateSynthSpace = nullptr; + int preallocateSynthSize = 0; + + static constexpr int buffLen = 0x600; // Slightly larger than largest MP3 frame unsigned char *buff; int lastReadPos; int lastBuffLen; @@ -62,6 +76,8 @@ class AudioGeneratorMP3 : public AudioGenerator bool DecodeNextFrame(); bool GetOneSample(int16_t sample[2]); + private: + int unrecoverable = 0; }; #endif