feat(windows): add Media Foundation encoder provider/session and DXGI adapter enumeration
Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com> Claude-Session: https://claude.ai/code/session_012iLMBNwBE2GnphAsu4cjaC
This commit is contained in:
9 files changed
+1579
No files matched your search
@@ -0,0 +1,95 @@
|
||||
using System.Runtime.InteropServices;
|
||||
using Titano.Core.Diagnostics;
|
||||
using Titano.Core.Video;
|
||||
using Titano.Core.Windows.Interop;
|
||||
|
||||
namespace Titano.Core.Windows.MediaFoundation;
|
||||
|
||||
/// <summary>A Media Foundation video encoder MFT available on this machine.</summary>
|
||||
/// <param name="Name">MFT_FRIENDLY_NAME_Attribute, e.g. "NVIDIA HEVC Encoder MFT".</param>
|
||||
/// <param name="VendorId">MFT_ENUM_HARDWARE_VENDOR_ID_Attribute of hardware MFTs, e.g. "VEN_10DE".</param>
|
||||
/// <param name="SupportsTenBit">The MFT accepts P010 input (HEVC Main10 / AV1 10-bit).</param>
|
||||
public sealed record MediaFoundationEncoderInfo(VideoCodec Codec, string Name, bool IsHardware, bool IsAsync, string? VendorId, bool SupportsTenBit);
|
||||
|
||||
/// <summary>Discovers H.264, HEVC and AV1 encoder MFTs with MFTEnumEx in the system's merit order (hardware first).</summary>
|
||||
public static class MediaFoundationEncoders
|
||||
{
|
||||
internal const string LogCategory = "MediaFoundation";
|
||||
private static readonly VideoCodec[] Codecs = [VideoCodec.H264, VideoCodec.Hevc, VideoCodec.Av1];
|
||||
|
||||
/// <summary>Every encoder MFT for H.264, HEVC and AV1; empty when Media Foundation is unavailable.</summary>
|
||||
public static IReadOnlyList<MediaFoundationEncoderInfo> Enumerate()
|
||||
{
|
||||
if (!MediaFoundationRuntime.IsAvailable) return [];
|
||||
using var lease = MediaFoundationRuntime.Acquire();
|
||||
var result = new List<MediaFoundationEncoderInfo>();
|
||||
foreach (var codec in Codecs)
|
||||
{
|
||||
foreach (var (info, activate) in Find(codec))
|
||||
{
|
||||
result.Add(info);
|
||||
ComObjects.Release(activate);
|
||||
}
|
||||
}
|
||||
return result;
|
||||
}
|
||||
|
||||
/// <summary>Encoder activation objects for <paramref name="codec"/>; Media Foundation must be started and the caller releases them.</summary>
|
||||
internal static List<(MediaFoundationEncoderInfo Info, IMFActivate Activate)> Find(VideoCodec codec)
|
||||
{
|
||||
var tenBit = new HashSet<string>(StringComparer.Ordinal);
|
||||
if (codec != VideoCodec.H264)
|
||||
{
|
||||
foreach (var activate in Activates(codec, MfGuids.FormatP010))
|
||||
{
|
||||
if (FriendlyName(activate) is { } name) tenBit.Add(name);
|
||||
ComObjects.Release(activate);
|
||||
}
|
||||
}
|
||||
var found = new List<(MediaFoundationEncoderInfo, IMFActivate)>();
|
||||
foreach (var activate in Activates(codec, null))
|
||||
{
|
||||
var name = FriendlyName(activate) ?? "Unnamed encoder";
|
||||
var flags = activate.GetUINT32(MfGuids.TransformFlags, out var f) >= 0 ? f : 0;
|
||||
string? vendor = null;
|
||||
if (activate.GetAllocatedString(MfGuids.HardwareVendorId, out var vendorPointer, out _) >= 0) vendor = ComObjects.TakeString(vendorPointer);
|
||||
var info = new MediaFoundationEncoderInfo(codec, name, (flags & MfNative.EnumHardware) != 0, (flags & MfNative.EnumAsync) != 0, vendor, tenBit.Contains(name));
|
||||
found.Add((info, activate));
|
||||
}
|
||||
return found;
|
||||
}
|
||||
|
||||
internal static Guid Subtype(VideoCodec codec) => codec switch
|
||||
{
|
||||
VideoCodec.H264 => MfGuids.FormatH264,
|
||||
VideoCodec.Hevc => MfGuids.FormatHevc,
|
||||
VideoCodec.Av1 => MfGuids.FormatAv1,
|
||||
_ => throw new ArgumentException($"{codec} has no Media Foundation encoder", nameof(codec)),
|
||||
};
|
||||
|
||||
private static string? FriendlyName(IMFActivate activate) =>
|
||||
activate.GetAllocatedString(MfGuids.FriendlyName, out var pointer, out _) >= 0 ? ComObjects.TakeString(pointer) : null;
|
||||
|
||||
private static unsafe List<IMFActivate> Activates(VideoCodec codec, Guid? inputSubtype)
|
||||
{
|
||||
var output = new MftRegisterTypeInfo { MajorType = MfGuids.MediaTypeVideo, Subtype = Subtype(codec) };
|
||||
var input = new MftRegisterTypeInfo { MajorType = MfGuids.MediaTypeVideo, Subtype = inputSubtype ?? Guid.Empty };
|
||||
const uint flags = MfNative.EnumHardware | MfNative.EnumAsync | MfNative.EnumSync | MfNative.EnumSortAndFilter;
|
||||
var hr = MfNative.MFTEnumEx(MfGuids.VideoEncoderCategory, flags, inputSubtype.HasValue ? &input : null, &output, out var array, out var count);
|
||||
var list = new List<IMFActivate>((int)count);
|
||||
if (hr < 0)
|
||||
{
|
||||
Log.Warning(LogCategory, $"MFTEnumEx for {codec} failed with HRESULT 0x{hr:X8}");
|
||||
return list;
|
||||
}
|
||||
try
|
||||
{
|
||||
for (var i = 0; i < count; i++) list.Add(ComObjects.Attach<IMFActivate>(Marshal.ReadIntPtr(array, i * IntPtr.Size)));
|
||||
}
|
||||
finally
|
||||
{
|
||||
Marshal.FreeCoTaskMem(array);
|
||||
}
|
||||
return list;
|
||||
}
|
||||
}
|
||||
Reference in new issue
Block a user