videoduplicatefinder icon indicating copy to clipboard operation
videoduplicatefinder copied to clipboard

Crash on thumbnail generation using ffmpeg native binding

Open King555a opened this issue 2 years ago • 11 comments

Environment

  • OS: Windows 10 22H2 x64

Describe the bug When I started using VDF 3.x in September 2022, I did not have this problem. I did every update since then and since several updates, maybe it was one in the last 1 or 2 months, VDF crashes most of the times after a completed scan. There is absolutely no error message, not on the screen and not in the logs. I alread deleted everything including settings and database and did a new download and "install" of VDF. It still crashes. This is the log after the crash:

11:24:18 => Found previously scanned files, importing...
11:24:18 => Previously scanned files imported. 36.104 files in 00:00:00.4412633
11:24:38 => Files in database: 36.143 (39 files added)
11:24:38 => Finished building file list in 00:00:20.8628825
11:24:38 => Gathering media info and buildings hashes...
[...]
11:25:11 => Finished gathering and hashing in 00:00:32.3106845
11:25:11 => Save scanned files to disk (36.143 files).
11:25:11 => Scan for duplicates...
11:25:11 => Prepare list of items to compare...
11:25:11 => Scanning for duplicates in 36.060 files
11:28:00 => Finished scanning for duplicates in 00:02:49.5157806
11:28:00 => Highlighting best results...
11:28:01 => Scan done.
11:28:01 => Save scanned files to disk (36.143 files).

[...] are some ffmpeg error messages, because some files cannot be read correctly. But these error messages are there really every time, so I don't think they are related to the crash.

These are my settings: settings

Expected behavior No crash or at least an error message.

King555a avatar Mar 11 '23 10:03 King555a

There should be crash entries in the event viewer of Windows. Please take a look and post them here. Only the .NET crash entries are important.

Also try uncheck native ffmpeg binding and/or generate preview images to see if those matters.

0x90d avatar Mar 11 '23 10:03 0x90d

The error from the event viewer:

Application: VDF.GUI.exe
CoreCLR Version: 7.0.323.6910
.NET Version: 7.0.3
Description: The process was terminated due to an unhandled exception.
Exception Info: System.AccessViolationException: Attempted to read or write protected memory. This is often an indication that other memory is corrupt.
Stack:
   at VDF.Core.FFTools.FFmpegNative.VideoFrameConverter.Convert(FFmpeg.AutoGen.AVFrame)
   at VDF.Core.FFTools.FfmpegEngine.GetThumbnail(VDF.Core.FFTools.FfmpegSettings, Boolean)
   at VDF.Core.ScanEngine.<RetrieveThumbnails>b__70_1(VDF.Core.ViewModels.DuplicateItem, System.Threading.CancellationToken)
   at System.Threading.Tasks.Parallel+<>c__50`1+<<ForEachAsync>b__50_0>d[[System.__Canon, System.Private.CoreLib, Version=7.0.0.0, Culture=neutral, PublicKeyToken=7cec85d7bea7798e]].MoveNext()
   at System.Runtime.CompilerServices.AsyncMethodBuilderCore.Start[[System.Threading.Tasks.Parallel+<>c__50`1+<<ForEachAsync>b__50_0>d[[System.__Canon, System.Private.CoreLib, Version=7.0.0.0, Culture=neutral, PublicKeyToken=7cec85d7bea7798e]], System.Threading.Tasks.Parallel, Version=7.0.0.0, Culture=neutral, PublicKeyToken=b03f5f7f11d50a3a]](<<ForEachAsync>b__50_0>d<System.__Canon> ByRef)
   at System.Runtime.CompilerServices.AsyncTaskMethodBuilder.Start[[System.Threading.Tasks.Parallel+<>c__50`1+<<ForEachAsync>b__50_0>d[[System.__Canon, System.Private.CoreLib, Version=7.0.0.0, Culture=neutral, PublicKeyToken=7cec85d7bea7798e]], System.Threading.Tasks.Parallel, Version=7.0.0.0, Culture=neutral, PublicKeyToken=b03f5f7f11d50a3a]](<<ForEachAsync>b__50_0>d<System.__Canon> ByRef)
   at System.Threading.Tasks.Parallel+<>c__50`1[[System.__Canon, System.Private.CoreLib, Version=7.0.0.0, Culture=neutral, PublicKeyToken=7cec85d7bea7798e]].<ForEachAsync>b__50_0(System.Object)
   at System.Threading.Tasks.Parallel+ForEachAsyncState`1+<>c[[System.__Canon, System.Private.CoreLib, Version=7.0.0.0, Culture=neutral, PublicKeyToken=7cec85d7bea7798e]].<System.Threading.IThreadPoolWorkItem.Execute>b__15_0(System.Object)
   at System.Threading.ExecutionContext.RunInternal(System.Threading.ExecutionContext, System.Threading.ContextCallback, System.Object)
   at System.Threading.ThreadPoolWorkQueue.Dispatch()
   at System.Threading.PortableThreadPool+WorkerThread.WorkerThreadStart()

I try a new scan with the two things disabled.

King555a avatar Mar 11 '23 11:03 King555a

It IS the native binding + thumbnail generation. Which exact ffmpeg version you have?

0x90d avatar Mar 11 '23 16:03 0x90d

I downloaded "ffmpeg-5.1.2-full_build-shared" from https://www.gyan.dev/ffmpeg/builds/

I could not find 5.0(.x), even when I started using VDF some time ago. But I did not have those crashes in the beginning.

King555a avatar Mar 11 '23 17:03 King555a

I cannot reproduce it with the given ffmpeg build. For some reason the exception is also not caught by my try-catch. It probably occurs within ffmpeg itself. Maybe caused by a corrupt video / frame, as you say it has worked before. Maybe @Maltragor has an idea how to deal with that?

0x90d avatar Mar 12 '23 04:03 0x90d

With disabled native binding it does indeed work (but thumbnail generation is incredible slow). I did not know before that thumbnails are generated after the scan is done.

So there must be one quite new video on my harddisk, which does not want its thumbnails to be created.

King555a avatar Mar 12 '23 13:03 King555a

Then a few thoughts out loud: It would certainly help to further narrow down the components involved in the error.

  • Are there certain videos that trigger the error (of course, a video in that sense can't be the cause of the error, but it can trigger the error). If such a video would be identified, it would be easier to reproduce the error and search for the cause.

So there must be one quite new video on my harddisk, which does not want its thumbnails to be created.

  • This is not necessarily the case. If a new video is recognized as a duplicate of an old one, this also leads to the creation of thumbnails of the old video. And if no thumbnails were created from the old one before, ... By the way, thumbnails are also created once during the scan of new videos. But then in a different resolution and only in grayscale. You write that you have deleted the database before. So if there is a particular video that triggers the error, then it only happens with the larger thumbnails displayed. A smaller grayscale thumbnail would have already been successfully created in that case. To find out without adjustments to VDF if a certain video causes problems and if so which one, you could use "Process Monitor" (https://learn.microsoft.com/en-us/sysinternals/downloads/procmon). The program records among other things all file accesses. The last video file accessed before VDF crashes would be the one you are looking for, especially if it is always the same. A few more notes about this: VDF can process multiple files at the same time. (Settings: Max Degree Of Parallelism > 1). In this case the last one in the log is not necessarily the one you are looking for. Therefore set "Max Degree Of Parallelism" to 1. On the other hand you have to get familiar with the functionality of Process Monitor. (Set filter of the processes to be monitored, disable registry monitoring,... otherwise the log will be very full)

(@0x90d In principle, you could also think about whether VDF (configurable) also logs the access before each thumbnail creation and outputs it together with the normal log via System.Diagnostics.Trace.WriteLine(), so that you can still read everything even in case of a crash).

  • If "Hardware Acceleration" is active in the VDF settings, I would also disable it for a test.

  • On https://ffmpeg.org/download.html there are two links for Windows. The second one (https://github.com/BtbN/FFmpeg-Builds/releases) still has the older ffmpeg versions, so you could try different versions.

  • @0x90d VDF uses another library (FFmpeg.AutoGen) for native ffmpeg access. On 2023-02-06 there was a commit in VDF to update this from 5.0.0 to 5.1.1. Since this is in the 1-2 month timeframe mentioned, this would also be a possible candidate.

Exception Info: System.AccessViolationException: Attempted to read or write protected memory. This is often an indication that other memory is corrupt.

For some reason the exception is also not caught by my try-catch.

@0x90d Well, the error message sounds a bit special. I have briefly researched:
https://learn.microsoft.com/en-us/dotnet/api/system.accessviolationexception?view=net-7.0

AccessViolationException exceptions thrown by the common language runtime are not handled by the catch statement in a structured exception handler if the exception occurs outside of the memory reserved by the common language runtime. To handle such an AccessViolationException exception, apply the HandleProcessCorruptedStateExceptionsAttribute attribute to the method

Maltragor avatar Mar 12 '23 18:03 Maltragor

I ran Process Monitor and according to it, the last file which was accessed has this format (only the most relevant information about it):

General
Format                      : AVI

Video
Format                      : JPEG
Codec ID                    : MJPG

Audio
Format                      : PCM

The file plays fine in Media Player Classic. Is there an ffmpeg command which I can use to test thumbnail generation?

EDIT: And the crash is reproducible. Moving the file into another directory, which is not scanned, prevents the crash. Moving it back: crash. And the file is not new, but several years old.

King555a avatar Mar 12 '23 21:03 King555a

Well, the error message sounds a bit special. I have briefly researched:

I cannot apply this attribute as a red box on MDSN says that for .NET 7 this attribute is ignored. Extended logging is something I definitely consider, possibly by switching to SeriLog or similar.

@King555a: Running a ffmpeg command wouldn't necessarily help as ffmpeg.exe seems to handle it fine (you say that disabling native binding does not crash, I expect you have let it complete thumbnail generation at least once?). As a workaround you could possibly convert this video file using a different codec. I'm currently seeing myself unable to fix it. I cannot catch/handle this exception and it seems to occur either in Ffmpeg.AutoGen or in ffmpeg. In first case I could only up-/ or downgrade. Have you tried a different ffmpeg version yourself?

0x90d avatar Mar 13 '23 10:03 0x90d

And the file is not new, but several years old.

Is this old video recognized as a duplicate of a new video? So does the error occur since the mentioned one to two months because in that time a corresponding new similar video was added to the collection, or is it really due to a software update?

Maltragor avatar Mar 13 '23 20:03 Maltragor

I expect you have let it complete thumbnail generation at least once?

Yes, of course.

Have you tried a different ffmpeg version yourself?

I just downgraded from 5.1.2 to 5.0.1. Same crash ...

Is this old video recognized as a duplicate of a new video?

I just checked. No, both videos are from 2019 (and the duplicate group only contains these two videos).

King555a avatar Mar 14 '23 17:03 King555a

I'm closing this as I don't think it's something I could fix on my end. And perhaps it's not even relevant anymore.

0x90d avatar Jun 15 '24 05:06 0x90d