Skip to content

Commit 91755ab

Browse files
committed
fix(subtitle): fix an issue where the first bitmap sub could not be displayed correctly
There was a problem that the first bitmap subtitle could not be displayed in the correct position and size because it could not get the width and height of the video, due to the fact that analyseduration and probesize are no longer increased. This problem has been solved by caching the video size and using it. SuRGeoNix/Flyleaf#502
1 parent 8efe548 commit 91755ab

File tree

2 files changed

+32
-6
lines changed

2 files changed

+32
-6
lines changed

FlyleafLib/MediaPlayer/Subtitles.cs

Lines changed: 11 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -103,8 +103,8 @@ public void Calculate()
103103
if (_player.Subtitles == null ||
104104
_player.Subtitles[_subIndex].Data.Bitmap == null ||
105105
_player.SubtitlesDecoders[_subIndex].SubtitlesStream == null ||
106-
_player.SubtitlesDecoders[_subIndex].Width == 0 ||
107-
_player.SubtitlesDecoders[_subIndex].Height == 0)
106+
(_player.SubtitlesDecoders[_subIndex].Width == 0 && _player.SubtitlesManager[_subIndex].Width == 0) ||
107+
(_player.SubtitlesDecoders[_subIndex].Height == 0 && _player.SubtitlesManager[_subIndex].Height == 0))
108108
{
109109
return;
110110
}
@@ -113,13 +113,20 @@ public void Calculate()
113113

114114
// Calculate the ratio of the current width of the window to the width of the video
115115
double renderWidth = _player.VideoDecoder.Renderer.GetViewport.Width;
116-
// double windowWidth = ActualWidth;
117116
double videoWidth = _player.SubtitlesDecoders[_subIndex].Width;
117+
if (videoWidth == 0)
118+
{
119+
// Restore from cache because Width/Height may not be taken if the subtitle is not decoded enough.
120+
videoWidth = _player.SubtitlesManager[_subIndex].Width;
121+
}
118122

119123
// double videoHeight_ = (int)(videoWidth / Player.VideoDemuxer.VideoStream.AspectRatio.Value);
120124
double renderHeight = _player.VideoDecoder.Renderer.GetViewport.Height;
121-
// double windowHeight = ActualHeight;
122125
double videoHeight = _player.SubtitlesDecoders[_subIndex].Height;
126+
if (videoHeight == 0)
127+
{
128+
videoHeight = _player.SubtitlesManager[_subIndex].Height;
129+
}
123130

124131
// In aspect ratio like a movie, a black background may be added to the top and bottom.
125132
// In this case, the subtitles should be placed based on the video display area, so the offset from the image rendering area excluding the black background should be taken into consideration.

FlyleafLib/MediaPlayer/SubtitlesManager.cs

Lines changed: 21 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -123,6 +123,10 @@ public Language? LanguageSource
123123
}
124124
}
125125

126+
// For displaying bitmap subtitles, manage video width and height
127+
public int Width { get; internal set; }
128+
public int Height { get; internal set; }
129+
126130
private readonly object _subsLocker = new();
127131
private readonly Config _config;
128132
private readonly int _subIndex;
@@ -426,7 +430,7 @@ public void Open(string url, int streamIndex, MediaType type, bool useBitmap, La
426430
try
427431
{
428432
_cts = new CancellationTokenSource();
429-
using SubtitleReader reader = new(_config, _subIndex);
433+
using SubtitleReader reader = new(this, _config, _subIndex);
430434
reader.Open(url, streamIndex, type, _cts.Token);
431435

432436
_cts.Token.ThrowIfCancellationRequested();
@@ -511,6 +515,8 @@ public void Clear()
511515
State = PositionState.First;
512516
LanguageSource = null;
513517
IsLoading = false;
518+
Width = 0;
519+
Height = 0;
514520
}
515521
}
516522

@@ -541,6 +547,7 @@ protected bool Set<T>(ref T field, T value, [CallerMemberName] string? propertyN
541547

542548
public unsafe class SubtitleReader : IDisposable
543549
{
550+
private readonly SubManager _manager;
544551
private readonly Config _config;
545552
private readonly LogHandler Log;
546553
private readonly int _subIndex;
@@ -551,8 +558,9 @@ public unsafe class SubtitleReader : IDisposable
551558

552559
private AVPacket* _packet = null;
553560

554-
public SubtitleReader(Config config, int subIndex)
561+
public SubtitleReader(SubManager manager, Config config, int subIndex)
555562
{
563+
_manager = manager;
556564
_config = config;
557565
Log = new LogHandler(("[#1]").PadRight(8, ' ') + $" [SubReader{subIndex + 1} ] ");
558566

@@ -705,6 +713,17 @@ public void ReadAll(bool useBitmap, Action<SubtitleData> addSub, CancellationTok
705713
continue;
706714
}
707715

716+
if (_stream.IsBitmap)
717+
{
718+
// Cache the width and height of the video for use in displaying bitmap subtitles
719+
// width and height may be 0 unless after decoding the subtitles
720+
// In this case, bitmap subtitles cannot be displayed correctly, so the size should be cached here
721+
if (_manager.Width != _decoder.CodecCtx->width)
722+
_manager.Width = _decoder.CodecCtx->width;
723+
if (_manager.Height != _decoder.CodecCtx->height)
724+
_manager.Height = _decoder.CodecCtx->height;
725+
}
726+
708727
// Bitmap PGS has a special format.
709728
if (_stream.IsBitmap && prevSub != null
710729
/*&& _stream->codecpar->codec_id == AVCodecID.AV_CODEC_ID_HDMV_PGS_SUBTITLE*/)

0 commit comments

Comments
 (0)