radek-k / FFMediaToolkit

FFMediaToolkit is a cross-platform video decoder/encoder library for .NET that uses FFmpeg native libraries. It supports video frames extraction, reading stream metadata and creating videos from bitmaps in any format supported by FFmpeg.
MIT License
352 stars 56 forks source link

Cannot send a packet to the decoder. Error code: -1094995529 : Invalid data found when processing input #132

Open Khhs167 opened 11 months ago

Khhs167 commented 11 months ago

Hello!

I am currently working on a system to allow me to integrate video support in my lil' game engine, and I decided to use FFMediaToolkit for decoding, since it seemed to fit my purposes.

Now, decoding works fine and dandy(some exceptions, which are most likely due to my own jank), for a couple seconds. Sometimes, it's 1 second, sometimes it's 6 seconds, and I don't know why.

The error log I've been getting(if you ignore the ALERROR, I've messed up some deinitialization stuff for OpenAL)

[vp9 @ 0x5567eb9d2a80] Not all references are available
[Error/AudioThread(11)] (ConsoleErr): AL ERROR: IllegalCommand:  at Close
[Fatal/Main(1)] (Engine): FFMediaToolkit.FFmpegException: Cannot send a packet to the decoder. Error code: -1094995529 : Invalid data found when processing input
   at FFMediaToolkit.Decoding.Internal.Decoder.DecodePacket()
   at FFMediaToolkit.Decoding.Internal.Decoder.ReadNextFrame()
   at FFMediaToolkit.Decoding.Internal.Decoder.SkipFrames(Int64 targetTs)
   at FFMediaToolkit.Decoding.MediaStream.GetFrameByTimestamp(Int64 ts)
   at FFMediaToolkit.Decoding.VideoStream.TryGetFrame(TimeSpan time, IntPtr buffer, Int32 bufferStride)
   at Soul.Engine.Video.Step(Single delta) in /home/khhs/RiderProjects/Soul/SoulEngine/Soul.Engine/Resources/Videos/Video.cs:line 73
   at Soul.UIScreens.DanceScreen.Update(Single delta) in /home/khhs/RiderProjects/Soul/Soul/UIScreens/DanceScreen.cs:line 24
   at Soul.Engine.UI.ComponentUI.Update(Single delta) in /home/khhs/RiderProjects/Soul/SoulEngine/Soul.Engine/UI/ComponentUI.cs:line 39
   at Soul.Engine.UI.UIScreen.UpdateCurrent(Single delta) in /home/khhs/RiderProjects/Soul/SoulEngine/Soul.Engine/UI/UIScreen.cs:line 28
   at Soul.Engine.Engine.OnUpdateFrame(Entity rootEntity) in /home/khhs/RiderProjects/Soul/SoulEngine/Soul.Engine/Core/Engine.cs:line 209
   at Soul.Engine.Engine.Loop() in /home/khhs/RiderProjects/Soul/SoulEngine/Soul.Engine/Core/Engine.cs:line 151

I can also attach Video.cs, and I hope it is understandable! Video.cs.txt

If any further info is needed, feel free to contact me.

I'm using the latest FFMPEG binaries available in that repo you've got linked in the README(the version given by FFmpegLoader.FFmpegVersion is N-111626-g0ba719f726-20230726 so idk)

Khhs167 commented 11 months ago

I'd love to attach the video I've been using, but I am currently on limited and slow data.

Khhs167 commented 11 months ago

After further investigation, it seems like TryGetFrame(or possibly GetFrame) is the root of the problem.

When using TryGetNextFrame it seems like it suddenly works.

I'll do some more testing!

Khhs167 commented 11 months ago

Even more investigation later, and these kinds of errors seem to be more random that I thought. All of a sudden TryGetNextFrame stopped working...

I wonder if it has something to do with threading?

The decoding thread shouldn't be ongoing though? Shouldn't that be invoked and then waited for?

radek-k commented 11 months ago

It might be a threading issue, but I'm not sure. Did you test other video files? What decoding thread do you mean? Is the Video.Step method called from another thread? FFMediaToolkit isn't thread-safe, so make sure that the TryGetFrame isn't called on different threads at the same time. You can try wrapping it with lock statement to ensure that.

Khhs167 commented 11 months ago

I am calling TryGetFrame from the same thread I do all FFmpeg stuff. I managed to get ogv to work smoothly, mp4 and mov had their own issues(mp4 was "reiniting context", mov I don't remember, but it was 2.4 GB which I ain't dealin' with).

I didn't try other webm's but I tried re-encoding etc.