From 7f7c0ee187f068ca3cc0594a922c70c5b3e60a3d Mon Sep 17 00:00:00 2001 From: emeric Date: Sat, 4 Apr 2026 17:56:51 +0200 Subject: [PATCH] When hitting bad data, abort gracefully only if this is the last frame read --- src/libs/audio/impl/ffmpeg/PcmDecoder.cpp | 14 +++++++++++++- 1 file changed, 13 insertions(+), 1 deletion(-) diff --git a/src/libs/audio/impl/ffmpeg/PcmDecoder.cpp b/src/libs/audio/impl/ffmpeg/PcmDecoder.cpp index df61e894..6937c3ce 100644 --- a/src/libs/audio/impl/ffmpeg/PcmDecoder.cpp +++ b/src/libs/audio/impl/ffmpeg/PcmDecoder.cpp @@ -318,8 +318,20 @@ namespace lms::audio::ffmpeg { if (_inputPacket->stream_index == _inputStreamIndex) { - const int sendError{ ::avcodec_send_packet(_decoderContext.get(), _inputPacket.get()) }; + int sendError{ ::avcodec_send_packet(_decoderContext.get(), _inputPacket.get()) }; ::av_packet_unref(_inputPacket.get()); + if (sendError == AVERROR_INVALIDDATA) + { + // we may be close to the end of the file, abort gracefully *only* if next packet is EOF + const int peekError{ ::av_read_frame(_context.get(), _inputPacket.get()) }; + ::av_packet_unref(_inputPacket.get()); + if (peekError == AVERROR_EOF) + { + LMS_LOG(AUDIO, DEBUG, "Invalid data in packet at end of file"); + _eof = true; + sendError = 0; + } + } if (sendError < 0) throw FFmpegException{ "avcodec_send_packet failed", sendError }; }