Skip to content
Draft
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
15 changes: 13 additions & 2 deletions src/ui/Logic/VideoPlayers/Ffmpeg/FfmpegPlayer.cs
Original file line number Diff line number Diff line change
Expand Up @@ -1162,7 +1162,7 @@ private static bool SupportsHardwareDevice(AVCodec* decoder, AVHWDeviceType devi
/// is attached; the caller can tell by <c>hw_device_ctx</c> being set. Any hardware setup
/// failure silently means software.
/// </summary>
private AVCodecContext* OpenDecoder(AVStream* stream, bool hardware = false)
private AVCodecContext* OpenDecoder(AVStream* stream, bool hardware = false, int hardwareStartIndex = 0)
{
var decoder = ffmpeg.avcodec_find_decoder(stream->codecpar->codec_id);
if (decoder == null)
Expand All @@ -1186,10 +1186,12 @@ private static bool SupportsHardwareDevice(AVCodec* decoder, AVHWDeviceType devi
codec->pkt_timebase = stream->time_base;
codec->thread_count = 0; // auto

var selectedHardwareIndex = -1;
if (hardware && stream->codecpar->codec_type == AVMediaType.AVMEDIA_TYPE_VIDEO)
{
foreach (var deviceType in HardwareDeviceTypes)
for (var i = hardwareStartIndex; i < HardwareDeviceTypes.Length; i++)
{
var deviceType = HardwareDeviceTypes[i];
if (!SupportsHardwareDevice(decoder, deviceType))
{
continue;
Expand All @@ -1212,13 +1214,22 @@ private static bool SupportsHardwareDevice(AVCodec* decoder, AVHWDeviceType devi

codec->hw_device_ctx = device; // freed with the codec context
codec->get_format = GetHardwareFormatDelegate;
selectedHardwareIndex = i;
break;
}
}

result = ffmpeg.avcodec_open2(codec, decoder, null);
if (result < 0)
{
if (hardware && selectedHardwareIndex >= 0)
{
var deviceName = HardwareDeviceName(codec);
Se.LogError($"ffmpeg player: {deviceName} decoder open failed for {ffmpeg.avcodec_get_name(stream->codecpar->codec_id)} ({FfmpegLibraries.ErrorText(result)}), trying the next hardware decoder or software");
ffmpeg.avcodec_free_context(&codec);
return OpenDecoder(stream, hardware: true, hardwareStartIndex: selectedHardwareIndex + 1);
}

ffmpeg.avcodec_free_context(&codec);
throw new InvalidOperationException($"avcodec_open2: {FfmpegLibraries.ErrorText(result)}");
}
Expand Down
Loading