Files
Chronos/src/Titano.Core.Windows/MediaFoundation/MediaFoundationEncoderProvider.cs
T

125 lines
6.1 KiB
C#
Raw Blame History

This file contains ambiguous Unicode characters
This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.
using System.Runtime.InteropServices;
using Titano.Core.Diagnostics;
using Titano.Core.Video;
using Titano.Core.Windows.Interop;
namespace Titano.Core.Windows.MediaFoundation;
/// <summary>
/// H.264, HEVC and AV1 encoding through Media Foundation MFTs (BRIEF §11.3): hardware encoders (NVENC, Quick Sync,
/// AMF…) first, the Microsoft software encoder as the H.264 fallback; HEVC and AV1 are offered only with hardware.
/// Codecs without a usable encoder are absent from <see cref="Capabilities"/>.
/// </summary>
public sealed class MediaFoundationEncoderProvider : IVideoEncoderProvider
{
private const string LogCategory = MediaFoundationEncoders.LogCategory;
private const int SoftwareH264MaxWidth = 4096, SoftwareH264MaxHeight = 2304; // Microsoft H.264 encoder, level 5.2
private readonly Lazy<IReadOnlyList<EncoderCapability>> _capabilities = new(BuildCapabilities);
public string Name => "Media Foundation";
public IReadOnlyList<EncoderCapability> Capabilities => _capabilities.Value;
public bool CanEncode(EncodeSettings settings)
{
if (!SettingsSupported(settings)) return false;
foreach (var c in Capabilities)
{
if (Fits(c.Codec, c.Name, c.Hardware, c.SupportsTenBit, settings) && settings.Width <= c.MaxWidth && settings.Height <= c.MaxHeight) return true;
}
return false;
}
public IVideoEncoderSession Open(EncodeSettings settings)
{
if (!SettingsSupported(settings)) throw new NotSupportedException($"Media Foundation cannot encode {settings.Codec} {settings.Width}×{settings.Height} {settings.BitDepth}-bit");
var runtime = MediaFoundationRuntime.Acquire();
var candidates = new List<(MediaFoundationEncoderInfo Info, IMFActivate Activate)>();
try
{
foreach (var (info, activate) in MediaFoundationEncoders.Find(settings.Codec))
{
if (Fits(info.Codec, info.Name, info.IsHardware, info.SupportsTenBit, settings)) candidates.Add((info, activate));
else ComObjects.Release(activate);
}
// Named encoder first, then the preferred kind; MFTEnumEx merit order otherwise.
var ordered = candidates
.OrderByDescending(c => settings.EncoderName is not null && c.Info.Name == settings.EncoderName)
.ThenByDescending(c => c.Info.IsHardware == settings.PreferHardware)
.ToList();
Exception? failure = null;
for (var i = 0; i < ordered.Count; i++)
{
var (info, activate) = ordered[i];
try
{
var session = MediaFoundationEncoderSession.Open(settings, info, activate, runtime);
for (var j = i + 1; j < ordered.Count; j++) ComObjects.Release(ordered[j].Activate);
candidates.Clear();
return session;
}
catch (Exception ex) when (ex is COMException or NotSupportedException or InvalidCastException)
{
Log.Warning(LogCategory, $"\"{info.Name}\" could not be configured for {settings.Codec} {settings.Width}×{settings.Height}: {ex.Message}");
candidates.Remove(ordered[i]);
activate.ShutdownObject();
ComObjects.Release(activate);
failure = ex;
}
}
throw new NotSupportedException($"No Media Foundation encoder accepted {settings.Codec} {settings.Width}×{settings.Height} {settings.BitDepth}-bit", failure);
}
catch
{
foreach (var (_, activate) in candidates) ComObjects.Release(activate);
runtime.Dispose();
throw;
}
}
private static bool SettingsSupported(EncodeSettings s) =>
s.Codec is VideoCodec.H264 or VideoCodec.Hevc or VideoCodec.Av1
&& s.Width > 0 && s.Height > 0 && s.Width % 2 == 0 && s.Height % 2 == 0
&& s.FrameRate.Numerator > 0 && s.FrameRate.Denominator > 0
&& (s.BitDepth == 8 || (s.BitDepth == 10 && s.Codec != VideoCodec.H264));
/// <summary>§11.3: HEVC and AV1 only in hardware; 10-bit needs P010 input; a named encoder must match.</summary>
private static bool Fits(VideoCodec codec, string name, bool hardware, bool tenBit, EncodeSettings s) =>
codec == s.Codec
&& (hardware || codec == VideoCodec.H264)
&& (s.BitDepth == 8 || tenBit)
&& (s.EncoderName is null || s.EncoderName == name);
private static IReadOnlyList<EncoderCapability> BuildCapabilities()
{
var capabilities = new List<EncoderCapability>();
try
{
foreach (var info in MediaFoundationEncoders.Enumerate())
{
if (!info.IsHardware && info.Codec != VideoCodec.H264) continue;
var (width, height) = info.IsHardware ? (info.Codec == VideoCodec.H264 ? (4096, 4096) : (8192, 8192)) : (SoftwareH264MaxWidth, SoftwareH264MaxHeight);
capabilities.Add(new EncoderCapability(info.Codec, info.Name, info.IsHardware, width, height,
info.SupportsTenBit && info.Codec != VideoCodec.H264, VendorName(info.VendorId)));
Log.Info(LogCategory, $"{info.Codec} encoder \"{info.Name}\" ({(info.IsHardware ? "hardware" : "software")}{(info.SupportsTenBit ? ", 10-bit" : string.Empty)})");
}
}
catch (Exception ex) when (ex is COMException or DllNotFoundException or EntryPointNotFoundException or PlatformNotSupportedException)
{
Log.Warning(LogCategory, "Media Foundation encoders could not be enumerated", ex);
}
return capabilities;
}
/// <summary>Readable vendor for PCI vendor ids reported as "VEN_xxxx".</summary>
internal static string? VendorName(string? vendorId) => vendorId?.ToUpperInvariant() switch
{
null => null,
"VEN_10DE" => "NVIDIA",
"VEN_8086" => "Intel",
"VEN_1002" or "VEN_1022" => "AMD",
"VEN_5143" or "VEN_4D4F4351" => "Qualcomm",
var other => other,
};
}