naudio / NLayer

MPEG 1 & 2 Decoder for Layers 1, 2, & 3
MIT License
123 stars 30 forks source link

Attempted to read past the end of the stream #1

Closed abadyl closed 6 years ago

abadyl commented 7 years ago

Hello,

I am trying to decode mp3 file. See simple console app.

var builder = new Mp3FileReader.FrameDecompressorBuilder(wf => new Mp3FrameDecompressor(wf)); using (var reader = new Mp3FileReader(@"D:\tEST\3.mp3", builder)) { WaveFileWriter.CreateWaveFile(@"D://tEST/output.wav", reader); }

As result the following exception was thrown.

at NLayer.NAudioSupport.Mp3FrameWrapper.ReadBits(Int32 bitCount) at NLayer.Decoder.BitReservoir.AddBits(IMpegFrame frame, Int32 overlap) at NLayer.Decoder.LayerIIIDecoder.DecodeFrame(IMpegFrame frame, Single[] ch0, Single[] ch1) at NLayer.MpegFrameDecoder.DecodeFrameImpl(IMpegFrame frame, Array dest, Int32 destOffset) at NLayer.MpegFrameDecoder.DecodeFrame(IMpegFrame frame, Byte[] dest, Int32 destOffset) at NLayer.NAudioSupport.Mp3FrameDecompressor.DecompressFrame(Mp3Frame frame, Byte[] dest, Int32 destOffset) at NAudio.Wave.Mp3FileReader.Read(Byte[] sampleBuffer, Int32 offset, Int32 numBytes) at NAudio.Wave.WaveFileWriter.CreateWaveFile(String filename, IWaveProvider sourceProvider) at NAaudioTest.Program.Main(String[] args) in D:\Data\startup\NAaudioTest\NAaudioTest\Program.cs:line 35 at System.AppDomain._nExecuteAssembly(RuntimeAssembly assembly, String[] args) at System.AppDomain.ExecuteAssembly(String assemblyFile, Evidence assemblySecurity, String[] args) at Microsoft.VisualStudio.HostingProcess.HostProc.RunUsersAssembly() at System.Threading.ThreadHelper.ThreadStart_Context(Object state) at System.Threading.ExecutionContext.RunInternal(ExecutionContext executionContext, ContextCallback callback, Object state, Boolean preserveSyncCtx) at System.Threading.ExecutionContext.Run(ExecutionContext executionContext, ContextCallback callback, Object state, Boolean preserveSyncCtx) at System.Threading.ExecutionContext.Run(ExecutionContext executionContext, ContextCallback callback, Object state) at System.Threading.ThreadHelper.ThreadStart()

/Best regards, /Alexey Badyl

markheath commented 7 years ago

probably some kind of corruption in the file, or maybe album art that looks like an MP3 frame. Ideally Mp3FileReader should cope with this better. Probably we should have had a TryDecompressFrame method instead.

smatthews1999 commented 7 years ago

@abadyl Did you ever find a solution to this? I am facing the same error. Same code... The mp3 plays fine and was created by AWS Polly text to speech service. I don't know how to detect any corruption.

var fileName = syspath + "aud.mpg";
var builder = new Mp3FileReader.FrameDecompressorBuilder(wf => new Mp3FrameDecompressor(wf));
using (var reader = new Mp3FileReader(fileName, builder))
{
     WaveFileWriter.CreateWaveFile(syspath+"pcm.wav", reader);
}
OutlawPro commented 6 years ago

Having the same issue here with an MP3 that plays fine on all systems when not using NLayer.NAudioSupport

Here is a sample MP3 that has the error. This same MP3 plays on other boxes using the raw NAudio player. When trying to play the file on a box that has no codecs and using NLayer, I get the error.

7336856-1016617.zip

pferraris commented 6 years ago

I had the same issue and I found the root cause and solution. When the side information is read, in cases where the MPEG version is 2 or higher, only 1 granule should be considered, and the method is reading 2 granules, so it ignores 8 bytes of frame information, causing a reading error. I will submit a pull request solving this.