Skip to content

Commit

Permalink
Config: Introduces Decoder.AudioCodecOpt/VideoCodecOpt/SubtitlesCodec…
Browse files Browse the repository at this point in the history
…Opt to support all codec options per codec type

Config: Player.MaxLatency will auto-enable Decoder.LowDelay
  • Loading branch information
SuRGeoNix committed Dec 10, 2023
1 parent 910a31e commit 9d1a9e9
Show file tree
Hide file tree
Showing 3 changed files with 49 additions and 10 deletions.
24 changes: 21 additions & 3 deletions FlyleafLib/Engine/Config.cs
Original file line number Diff line number Diff line change
Expand Up @@ -192,12 +192,20 @@ public long MaxLatency {
if (player != null)
player.Speed = 1;

if (config != null)
config.Decoder.LowDelay = false;

return;
}

// Large max buffer so we ensure the actual latency distance
if (config != null && config.Demuxer.BufferDuration < value * 2)
config.Demuxer.BufferDuration = value * 2;
if (config != null)
{
if (config.Demuxer.BufferDuration < value * 2)
config.Demuxer.BufferDuration = value * 2;

config.Decoder.LowDelay = true;
}

// Small min buffer to avoid enabling latency speed directly
if (_MinBufferDuration > value / 10)
Expand Down Expand Up @@ -515,10 +523,20 @@ public bool AllowProfileMismatch
bool _ShowCorrupted;

/// <summary>
/// Forces low delay (Parses AV_CODEC_FLAG_LOW_DELAY to AVCodecContext)
/// Forces low delay (Parses AV_CODEC_FLAG_LOW_DELAY to AVCodecContext) (auto-enabled with MaxLatency)
/// </summary>
public bool LowDelay { get => _LowDelay; set => SetUI(ref _LowDelay, value); }
bool _LowDelay;

public SerializableDictionary<string, string>
AudioCodecOpt { get; set; } = new();
public SerializableDictionary<string, string>
VideoCodecOpt { get; set; } = new();
public SerializableDictionary<string, string>
SubtitlesCodecOpt { get; set; } = new();

public SerializableDictionary<string, string> GetCodecOptPtr(MediaType type)
=> type == MediaType.Video ? VideoCodecOpt : type == MediaType.Audio ? AudioCodecOpt : SubtitlesCodecOpt;
}
public class VideoConfig : NotifyPropertyChanged
{
Expand Down
21 changes: 20 additions & 1 deletion FlyleafLib/MediaFramework/MediaDecoder/DecoderBase.cs
Original file line number Diff line number Diff line change
Expand Up @@ -100,7 +100,26 @@ protected string Open2(StreamBase stream, StreamBase prevStream, bool openStream
if (ret < 0)
return error = $"[{Type} Setup] {ret}";

ret = avcodec_open2(codecCtx, codec, null);
var codecOpts = Config.Decoder.GetCodecOptPtr(stream.Type);
AVDictionary* avopt = null;
foreach(var optKV in codecOpts)
av_dict_set(&avopt, optKV.Key, optKV.Value, 0);

ret = avcodec_open2(codecCtx, codec, &avopt);

if (avopt != null)
{
if (ret >= 0)
{
AVDictionaryEntry *t = null;

while ((t = av_dict_get(avopt, "", t, AV_DICT_IGNORE_SUFFIX)) != null)
Log.Debug($"Ignoring codec option {Utils.BytePtrToStringUTF8(t->key)}");
}

av_dict_free(&avopt);
}

if (ret < 0)
return error = $"[{Type} avcodec_open2] {FFmpegEngine.ErrorCodeToMsg(ret)} ({ret})";

Expand Down
14 changes: 8 additions & 6 deletions FlyleafLib/MediaFramework/MediaDemuxer/Demuxer.cs
Original file line number Diff line number Diff line change
Expand Up @@ -523,16 +523,18 @@ private void OpenFormat(string url, AVInputFormat* inFmt, Dictionary<string, str
fixed(AVFormatContext** fmtCtxPtr = &fmtCtx)
ret = avformat_open_input(fmtCtxPtr, url, inFmt, &avopt);

if (avopt != null && ret >= 0)
if (avopt != null)
{
AVDictionaryEntry *t = null;
if (ret >= 0)
{
AVDictionaryEntry *t = null;

while ((t = av_dict_get(avopt, "", t, AV_DICT_IGNORE_SUFFIX)) != null)
Log.Debug($"Ignoring format option {Utils.BytePtrToStringUTF8(t->key)}");
}
while ((t = av_dict_get(avopt, "", t, AV_DICT_IGNORE_SUFFIX)) != null)
Log.Debug($"Ignoring format option {Utils.BytePtrToStringUTF8(t->key)}");
}

if (avopt != null)
av_dict_free(&avopt);
}
}
private bool FillInfo()
{
Expand Down

0 comments on commit 9d1a9e9

Please sign in to comment.