Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

High memory usage when using bitmap subtitles #502

Open
umlx5h opened this issue Sep 16, 2024 · 5 comments
Open

High memory usage when using bitmap subtitles #502

umlx5h opened this issue Sep 16, 2024 · 5 comments
Labels
enhancement New feature or request

Comments

@umlx5h
Copy link
Contributor

umlx5h commented Sep 16, 2024

This commit supports bitmap subtitles, but I have observed that the memory usage is temporarily large after enabling bitmap subs. It may possibly be a memory leak.

9a47e74

The following settings seem to temporarily increase memory usage. It appears to be released after a long distance seek.

fmtCtx->probesize = Math.Max(fmtCtx->probesize, 5000 * (long)1024 * 1024); // Bytes
fmtCtx->max_analyze_duration = Math.Max(fmtCtx->max_analyze_duration, 1000 * (long)1000 * 1000); // Mcs

test video file

  • 3.55GB mkv HEVC 2h32min PGS Sub

Below are the results of the memory profiler in JetBrains Rider.

config_on

Starting playback of a video with bitmap subtitles enabled consumes a large amount of unmanaged memory usage. ( 2. 750 MB )
If playback is continued without seeking, memory usage does not decrease.

If you use the seek bar and seek large, memory usage is reduced significantly. ( 3. 350MB )

The following result shows the case where probesize and max_analyze_duration were not set.
config_off

No particular leakage occurred in this case. Seeking does not result in any particular change.

I checked the mpv and VLC sources and it seems that neither of them specifically set probesize and max_analyze_duration.
I would like to know in what situations these are necessary.

@SuRGeoNix
Copy link
Owner

So does VLC/mpv plays bitmap properly without specifying probesize/max_analyze_duration?
From my testing I've noticed that in case of bitmap subs in order for ffmpeg to read the stream's properties you need to increase those. Unfortunately, what FFmpeg does (just a guess) is that it caches even video frames/packets that's why you see an increased memory.

I didn't do any further testing but if you think is memory leak, it should be opened on FFmpeg's trac. If you think is this can be resolve in Flyleaf with another way I'm opened to fix it. (Possible add a config for whether to increase those values in this case or not)

@umlx5h
Copy link
Contributor Author

umlx5h commented Sep 27, 2024

Thanks for your reply.

I would like to investigate as well, Does the problem only reproduce itself with certain decoders or container videos?
I had no problems with the H264, mkv & mp4 videos I have on hand.

It may be acceptable to increase the setting value only for problematic formats.

@SuRGeoNix
Copy link
Owner

Didn't test it but it is possible to be affected by HEVC.
Generally speaking the FFmpeg's analyze is very problematic.

@umlx5h
Copy link
Contributor Author

umlx5h commented Sep 29, 2024

Just putting a seek after avformat_find_stream_info seems to free up memory.
Is this measure not sufficient?

$ git diff
diff --git a/FlyleafLib/MediaFramework/MediaDemuxer/Demuxer.cs b/FlyleafLib/MediaFramework/MediaDemuxer/Demuxer.cs
index c414f98..8e5732b 100644
--- a/FlyleafLib/MediaFramework/MediaDemuxer/Demuxer.cs
+++ b/FlyleafLib/MediaFramework/MediaDemuxer/Demuxer.cs
@@ -515,6 +515,11 @@ public unsafe class Demuxer : RunThreadBase
                 ret = avformat_find_stream_info(fmtCtx, null);
                 if (ret == AVERROR_EXIT || Status != Status.Opening || Interrupter.ForceInterrupt == 1) return error = "Cancelled";
                 if (ret < 0) return error = $"[avformat_find_stream_info] {FFmpegEngine.ErrorCodeToMsg(ret)} ({ret})";
+
+                if (requiresMoreAnalyse)
+                {
+                    avformat_seek_file(fmtCtx, -1, 0, 0, 0, 0);
+                }

@SuRGeoNix
Copy link
Owner

Seeking can cause issues so it should be avoided. Possible flushing the format/pb should work as well and it would be better. Maybe another ffmpeg option can avoid this memory usage too. I will try to look in to it in the future...

@SuRGeoNix SuRGeoNix added the enhancement New feature or request label Sep 29, 2024
@SuRGeoNix SuRGeoNix changed the title Memory leakage when using bitmap subtitles High memory usage when using bitmap subtitles Oct 4, 2024
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
enhancement New feature or request
Projects
None yet
Development

No branches or pull requests

2 participants