diff --git a/src/Titano.Core.Windows/Dxgi/GpuAdapters.cs b/src/Titano.Core.Windows/Dxgi/GpuAdapters.cs new file mode 100644 index 00000000..ae6aaa28 --- /dev/null +++ b/src/Titano.Core.Windows/Dxgi/GpuAdapters.cs @@ -0,0 +1,131 @@ +using System.Runtime.InteropServices; +using System.Runtime.InteropServices.Marshalling; +using Titano.Core.Diagnostics; +using Titano.Core.Windows.Interop; + +namespace Titano.Core.Windows.Dxgi; + +/// A graphics adapter as reported by DXGI (shown next to the encoders in Settings › Rendering). +/// PCI vendor id (0x10DE NVIDIA, 0x8086 Intel, 0x1002 AMD…). +/// The Microsoft Basic Render Driver or another software adapter. +public sealed record GpuAdapterInfo(string Description, uint VendorId, uint DeviceId, ulong DedicatedVideoMemory, ulong SharedSystemMemory, bool IsSoftware); + +/// Enumerates graphics adapters with CreateDXGIFactory1 → EnumAdapters1 → GetDesc1. +public static partial class GpuAdapters +{ + private const int NotFound = unchecked((int)0x887A0002); // DXGI_ERROR_NOT_FOUND + private const uint SoftwareFlag = 2; // DXGI_ADAPTER_FLAG_SOFTWARE + + /// All adapters in DXGI order (the primary adapter first); empty when DXGI is unavailable. + public static IReadOnlyList Enumerate() + { + var adapters = new List(); + if (!OperatingSystem.IsWindows()) return adapters; + IDXGIFactory1? factory = null; + try + { + ComObjects.Check(CreateDXGIFactory1(typeof(IDXGIFactory1).GUID, out var pointer), "CreateDXGIFactory1"); + factory = ComObjects.Attach(pointer); + for (uint index = 0; ; index++) + { + var hr = factory.EnumAdapters1(index, out var adapterPointer); + if (hr == NotFound) break; + ComObjects.Check(hr, "IDXGIFactory1.EnumAdapters1"); + var adapter = ComObjects.Attach(adapterPointer); + try + { + ComObjects.Check(adapter.GetDesc1(out var desc), "IDXGIAdapter1.GetDesc1"); + adapters.Add(desc.ToInfo()); + } + finally + { + ComObjects.Release(adapter); + } + } + } + catch (Exception ex) when (ex is COMException or DllNotFoundException or EntryPointNotFoundException) + { + Log.Warning("Dxgi", "Graphics adapters could not be enumerated", ex); + } + finally + { + ComObjects.Release(factory); + } + return adapters; + } + + [LibraryImport("dxgi.dll")] + private static partial int CreateDXGIFactory1(in Guid iid, out nint factory); + + [StructLayout(LayoutKind.Sequential)] + internal unsafe struct AdapterDesc1 + { + public fixed char Description[128]; + public uint VendorId; + public uint DeviceId; + public uint SubSysId; + public uint Revision; + public nuint DedicatedVideoMemory; + public nuint DedicatedSystemMemory; + public nuint SharedSystemMemory; + public uint AdapterLuidLow; + public int AdapterLuidHigh; + public uint Flags; + + public readonly GpuAdapterInfo ToInfo() + { + fixed (char* text = Description) + { + var description = new string(text, 0, new ReadOnlySpan(text, 128).IndexOf('\0') is var n and >= 0 ? n : 128); + return new GpuAdapterInfo(description, VendorId, DeviceId, DedicatedVideoMemory, SharedSystemMemory, (Flags & SoftwareFlag) != 0); + } + } + } +} + +// DXGI interfaces in vtable order (dxgi.h); unused slots keep their real signatures with raw pointers. + +[GeneratedComInterface] +[Guid("aec22fb8-76f3-4639-9be0-28eb43a67a2e")] +internal partial interface IDXGIObject +{ + [PreserveSig] int SetPrivateData(in Guid name, uint size, nint data); + [PreserveSig] int SetPrivateDataInterface(in Guid name, nint unknown); + [PreserveSig] int GetPrivateData(in Guid name, ref uint size, nint data); + [PreserveSig] int GetParent(in Guid iid, out nint parent); +} + +[GeneratedComInterface] +[Guid("7b7166ec-21c7-44ae-b21a-c9ae321ae369")] +internal partial interface IDXGIFactory : IDXGIObject +{ + [PreserveSig] int EnumAdapters(uint index, out nint adapter); + [PreserveSig] int MakeWindowAssociation(nint window, uint flags); + [PreserveSig] int GetWindowAssociation(out nint window); + [PreserveSig] int CreateSwapChain(nint device, nint description, out nint swapChain); + [PreserveSig] int CreateSoftwareAdapter(nint module, out nint adapter); +} + +[GeneratedComInterface] +[Guid("770aae78-f26f-4dba-a829-253c83d1b387")] +internal partial interface IDXGIFactory1 : IDXGIFactory +{ + [PreserveSig] int EnumAdapters1(uint index, out nint adapter); + [PreserveSig] int IsCurrent(); +} + +[GeneratedComInterface] +[Guid("2411e7e1-12ac-4ccf-bd14-9798e8534dc0")] +internal partial interface IDXGIAdapter : IDXGIObject +{ + [PreserveSig] int EnumOutputs(uint index, out nint output); + [PreserveSig] int GetDesc(nint description); + [PreserveSig] int CheckInterfaceSupport(in Guid iid, out long umdVersion); +} + +[GeneratedComInterface] +[Guid("29038f61-3839-4626-91fd-086879011a05")] +internal partial interface IDXGIAdapter1 : IDXGIAdapter +{ + [PreserveSig] int GetDesc1(out GpuAdapters.AdapterDesc1 description); +} diff --git a/src/Titano.Core.Windows/Interop/ComObjects.cs b/src/Titano.Core.Windows/Interop/ComObjects.cs new file mode 100644 index 00000000..db6a7bc6 --- /dev/null +++ b/src/Titano.Core.Windows/Interop/ComObjects.cs @@ -0,0 +1,60 @@ +using System.Runtime.InteropServices; +using System.Runtime.InteropServices.Marshalling; + +namespace Titano.Core.Windows.Interop; + +/// +/// Lifetime helpers for source-generated COM interop. Interface pointers received as out nint are wrapped in +/// unique runtime-callable wrappers so they can be released deterministically (Media Foundation encoders recycle a +/// small pool of samples and stall when references linger until finalization). +/// +internal static class ComObjects +{ + private static readonly StrategyBasedComWrappers Wrappers = new(); + + /// Wraps an owned interface pointer; the caller's reference is released (the wrapper holds its own). + public static T Attach(nint pointer) where T : class + { + if (pointer == 0) throw new ArgumentNullException(nameof(pointer)); + try + { + return (T)Wrappers.GetOrCreateObjectForComInstance(pointer, CreateObjectFlags.UniqueInstance); + } + finally + { + Marshal.Release(pointer); + } + } + + /// Like but returns null for a null pointer. + public static T? AttachOrNull(nint pointer) where T : class => pointer == 0 ? null : Attach(pointer); + + /// An AddRef'ed pointer of a wrapper, for structures; release it with . + public static unsafe nint GetPointer(T wrapper) where T : class => (nint)ComInterfaceMarshaller.ConvertToUnmanaged(wrapper); + + /// Releases the COM object behind a wrapper created by now rather than at finalization. + public static void Release(object? wrapper) + { + if (wrapper is ComObject com) com.FinalRelease(); + } + + /// Throws a naming the operation when is a failure code. + public static void Check(int hr, string operation) + { + if (hr < 0) throw new COMException($"{operation} failed with HRESULT 0x{hr:X8}", hr); + } + + /// Reads and frees a CoTaskMem-allocated UTF-16 string. + public static string? TakeString(nint pointer) + { + if (pointer == 0) return null; + try + { + return Marshal.PtrToStringUni(pointer); + } + finally + { + Marshal.FreeCoTaskMem(pointer); + } + } +} diff --git a/src/Titano.Core.Windows/MediaFoundation/MediaFoundationEncoderProvider.cs b/src/Titano.Core.Windows/MediaFoundation/MediaFoundationEncoderProvider.cs new file mode 100644 index 00000000..d7bc3752 --- /dev/null +++ b/src/Titano.Core.Windows/MediaFoundation/MediaFoundationEncoderProvider.cs @@ -0,0 +1,124 @@ +using System.Runtime.InteropServices; +using Titano.Core.Diagnostics; +using Titano.Core.Video; +using Titano.Core.Windows.Interop; + +namespace Titano.Core.Windows.MediaFoundation; + +/// +/// 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 . +/// +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> _capabilities = new(BuildCapabilities); + + public string Name => "Media Foundation"; + + public IReadOnlyList 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)); + + /// §11.3: HEVC and AV1 only in hardware; 10-bit needs P010 input; a named encoder must match. + 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 BuildCapabilities() + { + var capabilities = new List(); + 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; + } + + /// Readable vendor for PCI vendor ids reported as "VEN_xxxx". + 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, + }; +} diff --git a/src/Titano.Core.Windows/MediaFoundation/MediaFoundationEncoderSession.cs b/src/Titano.Core.Windows/MediaFoundation/MediaFoundationEncoderSession.cs new file mode 100644 index 00000000..f82354aa --- /dev/null +++ b/src/Titano.Core.Windows/MediaFoundation/MediaFoundationEncoderSession.cs @@ -0,0 +1,640 @@ +using System.Runtime.InteropServices; +using System.Runtime.InteropServices.Marshalling; +using Titano.Core.Diagnostics; +using Titano.Core.Imaging; +using Titano.Core.IO; +using Titano.Core.Video; +using Titano.Core.Video.Bitstream; +using Titano.Core.Video.Mp4; +using Titano.Core.Video.Yuv; +using Titano.Core.Windows.Interop; + +namespace Titano.Core.Windows.MediaFoundation; + +/// +/// One encode through a Media Foundation encoder MFT driven directly (no SinkWriter): output type first, then NV12/P010 +/// input, ICodecAPI rate control/GOP/B-frames, synchronous ProcessInput/ProcessOutput or the asynchronous event loop of +/// hardware MFTs, and Annex-B/OBU output muxed by into a temporary file that +/// commits. Disposing without completing deletes the partial file. +/// +public sealed class MediaFoundationEncoderSession : IVideoEncoderSession +{ + private const string LogCategory = MediaFoundationEncoders.LogCategory; + + private readonly EncodeSettings _settings; + private readonly IDisposable _runtime; + private readonly IMFActivate _activate; + private readonly IMFTransform _transform; + private readonly IMFMediaEventGenerator? _events; + private readonly bool _tenBit; + private readonly string _tempPath; + private readonly FileStream _file; + private readonly Mp4Muxer _muxer; + private readonly Dictionary _ticksByHns = []; + private readonly Dictionary _durationByTicks = []; + private readonly Queue _inputStarts = new(); + private bool _providesSamples; + private uint _outputBufferSize; + private int _needInput; + private bool _drained; + private long _time; + private Encoded? _pending; + private int _framesIn; + private int _framesOut; + private bool _completed; + private bool _released; + private bool _disposed; + + private MediaFoundationEncoderSession(EncodeSettings settings, MediaFoundationEncoderInfo info, IMFActivate activate, IMFTransform transform, IDisposable runtime) + { + _settings = settings; + _activate = activate; + _transform = transform; + _runtime = runtime; + _tenBit = settings.BitDepth == 10; + EncoderName = info.Name; + IsHardware = info.IsHardware; + + if (UnlockAsync(transform)) + _events = transform as IMFMediaEventGenerator ?? throw new NotSupportedException("Asynchronous MFT without an event generator"); + if (transform as ICodecAPI is { } codecApi) ApplyCodecSettings(codecApi); + SetOutputType(); + SetInputType(); + ReadOutputStreamInfo(); + Message(MfNative.MessageNotifyBeginStreaming); + Message(MfNative.MessageNotifyStartOfStream); + + _tempPath = AtomicFile.TempPathFor(settings.OutputPath); + _file = new FileStream(_tempPath, FileMode.CreateNew, FileAccess.ReadWrite, FileShare.None, 1 << 20); + try + { + _muxer = new Mp4Muxer(_file, new VideoTrackConfig { Codec = settings.Codec, Width = settings.Width, Height = settings.Height, FastStart = true }); + ReadSequenceHeader(); + } + catch + { + _file.Dispose(); + File.Delete(_tempPath); + throw; + } + Log.Info(LogCategory, $"Encoding {settings.Codec} {settings.Width}×{settings.Height} {settings.BitDepth}-bit with \"{info.Name}\" ({(info.IsHardware ? "hardware" : "software")}, {(_events is null ? "sync" : "async")})"); + } + + public string EncoderName { get; } + + public bool IsHardware { get; } + + /// Activates the MFT behind and configures it; the session then owns the activate and the runtime lease. + internal static MediaFoundationEncoderSession Open(EncodeSettings settings, MediaFoundationEncoderInfo info, IMFActivate activate, IDisposable runtime) + { + ComObjects.Check(activate.ActivateObject(MfGuids.IidTransform, out var pointer), "IMFActivate.ActivateObject"); + var transform = ComObjects.Attach(pointer); + try + { + return new MediaFoundationEncoderSession(settings, info, activate, transform, runtime); + } + catch + { + ComObjects.Release(transform); + throw; + } + } + + public void WriteFrame(ImageBuffer frame, long duration90k) + { + ObjectDisposedException.ThrowIf(_disposed, this); + if (_completed) throw new InvalidOperationException("The session is complete"); + if (frame.Width != _settings.Width || frame.Height != _settings.Height) throw new ArgumentException("Frame size differs from the encode size", nameof(frame)); + ArgumentOutOfRangeException.ThrowIfNegativeOrZero(duration90k); + + var start = _time; + _time += duration90k; + var hns = ToHns(start); + _ticksByHns[hns] = start; + _durationByTicks[start] = duration90k; + _inputStarts.Enqueue(start); + var sample = CreateInputSample(frame, hns, ToHns(_time) - hns); + try + { + if (_events is not null) + { + Pump(() => _needInput > 0); + _needInput--; + ComObjects.Check(_transform.ProcessInput(0, sample, 0), "ProcessInput"); + } + else + { + SubmitSync(sample); + } + } + finally + { + ComObjects.Release(sample); + } + _framesIn++; + } + + public void Complete() + { + ObjectDisposedException.ThrowIf(_disposed, this); + if (_completed) throw new InvalidOperationException("The session is already complete"); + if (_framesIn == 0) throw new InvalidOperationException("No frames were written"); + Message(MfNative.MessageNotifyEndOfStream); + Message(MfNative.MessageCommandDrain); + if (_events is not null) Pump(() => _drained); + else while (ProcessOutput()) { } + if (_pending is { } last) + { + // The last decoded picture keeps the duration of its own presentation slot. + WritePending(last, _durationByTicks.GetValueOrDefault(last.Pts, 1)); + _pending = null; + } + Message(MfNative.MessageNotifyEndStreaming); + ReleaseEncoder(); + + _muxer.Complete(); + _file.Flush(flushToDisk: true); + _file.Dispose(); + AtomicFile.Commit(_tempPath, _settings.OutputPath); + _completed = true; + Log.Info(LogCategory, $"\"{EncoderName}\" encoded {_framesIn} frames into {_framesOut} samples: {_settings.OutputPath}"); + } + + public void Dispose() + { + if (_disposed) return; + _disposed = true; + if (!_completed) + { + _file.Dispose(); + try + { + File.Delete(_tempPath); + } + catch (IOException ex) + { + Log.Warning(LogCategory, "Could not delete the partial output " + _tempPath, ex); + } + } + ReleaseEncoder(); + _runtime.Dispose(); + } + + private sealed record Encoded(byte[] Data, long Pts, long Dts, bool Key); + + private static bool UnlockAsync(IMFTransform transform) + { + if (transform.GetAttributes(out var pointer) < 0 || pointer == 0) return false; + var attributes = ComObjects.Attach(pointer); + try + { + if (attributes.GetUINT32(MfGuids.TransformAsync, out var isAsync) < 0 || isAsync == 0) return false; + ComObjects.Check(attributes.SetUINT32(MfGuids.TransformAsyncUnlock, 1), "MF_TRANSFORM_ASYNC_UNLOCK"); + return true; + } + finally + { + ComObjects.Release(attributes); + } + } + + private void ApplyCodecSettings(ICodecAPI api) + { + var mode = _settings.RateControl switch + { + RateControl.Cbr => MfNative.RateControlCbr, + RateControl.Quality => MfNative.RateControlQuality, + _ => MfNative.RateControlUnconstrainedVbr, + }; + SetCodecValue(api, MfGuids.CodecRateControlMode, ComVariant.Create(mode), "RateControlMode"); + if (_settings.RateControl == RateControl.Quality) + SetCodecValue(api, MfGuids.CodecQuality, ComVariant.Create((uint)Math.Clamp(_settings.Quality, 1, 100)), "Quality"); + else + SetCodecValue(api, MfGuids.CodecMeanBitRate, ComVariant.Create(BitRate), "MeanBitRate"); + var gop = _settings.GopLength > 0 ? _settings.GopLength : (int)Math.Max(1, Math.Round(_settings.FrameRate.Value)); + SetCodecValue(api, MfGuids.CodecGopSize, ComVariant.Create((uint)gop), "GOPSize"); + var bFrames = _settings.Profile == CodecProfile.H264Baseline ? 0 : Math.Max(0, _settings.BFrames); + SetCodecValue(api, MfGuids.CodecBPictureCount, ComVariant.Create((uint)bFrames), "BPictureCount"); + SetCodecValue(api, MfGuids.CodecLowLatencyMode, ComVariant.Create(false), "LowLatencyMode"); + } + + private static unsafe void SetCodecValue(ICodecAPI api, in Guid property, ComVariant value, string name) + { + if (api.IsSupported(property) != 0) + { + Log.Debug(LogCategory, $"ICodecAPI {name} not supported by the encoder"); + return; + } + var hr = api.SetValue(property, (nint)(&value)); + if (hr < 0) Log.Debug(LogCategory, $"ICodecAPI {name} rejected (0x{hr:X8})"); + } + + /// Requested bit rate, or the preset density (§11.4: 0.10/0.07/0.05 bits per pixel per frame) when unset. + private uint BitRate + { + get + { + if (_settings.BitRate > 0) return (uint)Math.Min(uint.MaxValue, _settings.BitRate); + var bpp = _settings.Codec switch { VideoCodec.H264 => 0.10, VideoCodec.Hevc => 0.07, _ => 0.05 }; + return (uint)Math.Min(uint.MaxValue, _settings.Width * (double)_settings.Height * _settings.FrameRate.Value * bpp); + } + } + + private void SetOutputType() + { + var level = CodecLimits.Check(_settings.Codec, _settings.Width, _settings.Height, _settings.FrameRate.Value, BitRate, _settings.Profile).Level; + var hr = TrySetOutputType(level); + if (hr < 0 && level is not null) + { + Log.Debug(LogCategory, $"Output type with level {level} rejected (0x{hr:X8}); letting the encoder choose"); + hr = TrySetOutputType(null); + } + ComObjects.Check(hr, "SetOutputType"); + } + + private int TrySetOutputType(CodecLevel? level) + { + var type = CreateMediaType(); + try + { + ComObjects.Check(type.SetGUID(MfGuids.Subtype, MediaFoundationEncoders.Subtype(_settings.Codec)), "MF_MT_SUBTYPE"); + ComObjects.Check(type.SetUINT32(MfGuids.AverageBitrate, BitRate), "MF_MT_AVG_BITRATE"); + SetVideoFormat(type); + var profile = Profile(); + if (profile > 0) ComObjects.Check(type.SetUINT32(MfGuids.Mpeg2Profile, profile), "MF_MT_MPEG2_PROFILE"); + if (level is { } l && _settings.Codec != VideoCodec.Av1) + { + var idc = _settings.Codec == VideoCodec.H264 ? l.H264LevelIdc : l.HevcLevelIdc; + ComObjects.Check(type.SetUINT32(MfGuids.Mpeg2Level, (uint)idc), "MF_MT_MPEG2_LEVEL"); + } + return _transform.SetOutputType(0, type, 0); + } + finally + { + ComObjects.Release(type); + } + } + + /// eAVEncH264VProfile / eAVEncH265VProfile value; AV1 encoders derive the profile from the input format. + private uint Profile() => _settings.Codec switch + { + VideoCodec.H264 => _settings.Profile switch + { + CodecProfile.H264Baseline => 66u, + CodecProfile.H264Main => 77u, + _ => 100u, + }, + VideoCodec.Hevc => _tenBit ? 2u : 1u, + _ => 0u, + }; + + private void SetInputType() + { + var subtype = _tenBit ? MfGuids.FormatP010 : MfGuids.FormatNv12; + var type = CreateMediaType(); + int hr; + try + { + ComObjects.Check(type.SetGUID(MfGuids.Subtype, subtype), "MF_MT_SUBTYPE"); + ComObjects.Check(type.SetUINT32(MfGuids.DefaultStride, (uint)(_settings.Width * (_tenBit ? 2 : 1))), "MF_MT_DEFAULT_STRIDE"); + SetVideoFormat(type); + hr = _transform.SetInputType(0, type, 0); + } + finally + { + ComObjects.Release(type); + } + + // Some encoders only accept their own enumerated input types: complete one with our format. + for (uint index = 0; hr < 0; index++) + { + var available = _transform.GetInputAvailableType(0, index, out var pointer); + if (available < 0) break; + var candidate = ComObjects.Attach(pointer); + try + { + if (candidate.GetGUID(MfGuids.Subtype, out var s) < 0 || s != subtype) continue; + SetVideoFormat(candidate); + hr = _transform.SetInputType(0, candidate, 0); + } + finally + { + ComObjects.Release(candidate); + } + } + ComObjects.Check(hr, $"SetInputType ({(_tenBit ? "P010" : "NV12")})"); + } + + private static IMFMediaType CreateMediaType() + { + ComObjects.Check(MfNative.MFCreateMediaType(out var pointer), "MFCreateMediaType"); + var type = ComObjects.Attach(pointer); + ComObjects.Check(type.SetGUID(MfGuids.MajorType, MfGuids.MediaTypeVideo), "MF_MT_MAJOR_TYPE"); + return type; + } + + /// Frame size and rate, square pixels, progressive, BT.709 limited range. + private void SetVideoFormat(IMFMediaType type) + { + ComObjects.Check(type.SetUINT64(MfGuids.FrameSize, Pack(_settings.Width, _settings.Height)), "MF_MT_FRAME_SIZE"); + ComObjects.Check(type.SetUINT64(MfGuids.FrameRate, Pack(_settings.FrameRate.Numerator, _settings.FrameRate.Denominator)), "MF_MT_FRAME_RATE"); + ComObjects.Check(type.SetUINT64(MfGuids.PixelAspectRatio, Pack(1, 1)), "MF_MT_PIXEL_ASPECT_RATIO"); + ComObjects.Check(type.SetUINT32(MfGuids.InterlaceMode, MfNative.InterlaceProgressive), "MF_MT_INTERLACE_MODE"); + type.SetUINT32(MfGuids.NominalRange, MfNative.NominalRange16To235); + type.SetUINT32(MfGuids.VideoPrimaries, MfNative.PrimariesBt709); + type.SetUINT32(MfGuids.TransferFunction, MfNative.TransferBt709); + type.SetUINT32(MfGuids.YuvMatrix, MfNative.MatrixBt709); + } + + private static ulong Pack(int high, int low) => (ulong)(uint)high << 32 | (uint)low; + + private void ReadOutputStreamInfo() + { + ComObjects.Check(_transform.GetOutputStreamInfo(0, out var info), "GetOutputStreamInfo"); + _providesSamples = (info.Flags & (MfNative.OutputStreamProvidesSamples | MfNative.OutputStreamCanProvideSamples)) != 0; + _outputBufferSize = Math.Max(info.Size, (uint)(_settings.Width * _settings.Height * 3 / 2)); + } + + /// Hands MF_MT_MPEG_SEQUENCE_HEADER (parameter sets / sequence header OBU) of the current output type to the muxer. + private void ReadSequenceHeader() + { + if (_transform.GetOutputCurrentType(0, out var pointer) < 0) return; + var type = ComObjects.Attach(pointer); + try + { + if (type.GetAllocatedBlob(MfGuids.MpegSequenceHeader, out var blob, out var size) < 0) return; + try + { + var bytes = new byte[size]; + Marshal.Copy(blob, bytes, 0, (int)size); + _muxer.AddParameterSets(bytes); + } + finally + { + Marshal.FreeCoTaskMem(blob); + } + } + finally + { + ComObjects.Release(type); + } + } + + private unsafe IMFSample CreateInputSample(ImageBuffer frame, long time, long duration) + { + var fourCc = _tenBit ? 0x30313050u : 0x3231564Eu; // 'P010' / 'NV12' + ComObjects.Check(MfNative.MFCreate2DMediaBuffer((uint)_settings.Width, (uint)_settings.Height, fourCc, false, out var bufferPointer), "MFCreate2DMediaBuffer"); + var buffer = ComObjects.Attach(bufferPointer); + try + { + var buffer2D = buffer as IMF2DBuffer ?? throw new NotSupportedException("Media buffer without IMF2DBuffer"); + ComObjects.Check(buffer2D.Lock2D(out var scanline0, out var pitch), "IMF2DBuffer.Lock2D"); + try + { + if (pitch <= 0) throw new NotSupportedException("Bottom-up input buffers are not supported"); + var rows = _settings.Height * 3 / 2; + if (_tenBit) Nv12Converter.ToP010(frame, new Span((void*)scanline0, pitch / 2 * rows), ChromaSiting.Left, pitch / 2); + else Nv12Converter.ToNv12(frame, new Span((void*)scanline0, pitch * rows), ChromaSiting.Left, pitch); + } + finally + { + buffer2D.Unlock2D(); + } + // 2D buffers report their contiguous size; setting it explicitly helps encoders that read the plain length. + if (buffer2D.GetContiguousLength(out var length) >= 0 && buffer.SetCurrentLength(length) < 0) + Log.Debug(LogCategory, "SetCurrentLength on the 2D input buffer was rejected"); + + ComObjects.Check(MfNative.MFCreateSample(out var samplePointer), "MFCreateSample"); + var sample = ComObjects.Attach(samplePointer); + try + { + ComObjects.Check(sample.AddBuffer(buffer), "AddBuffer"); + ComObjects.Check(sample.SetSampleTime(time), "SetSampleTime"); + ComObjects.Check(sample.SetSampleDuration(duration), "SetSampleDuration"); + return sample; + } + catch + { + ComObjects.Release(sample); + throw; + } + } + finally + { + ComObjects.Release(buffer); + } + } + + private void SubmitSync(IMFSample sample) + { + while (true) + { + var hr = _transform.ProcessInput(0, sample, 0); + if (hr != MfNative.NotAccepting) + { + ComObjects.Check(hr, "ProcessInput"); + break; + } + // Full: collect output first; an encoder that neither accepts input nor yields output is stuck. + if (!ProcessOutput()) throw new COMException("Encoder refuses input without producing output", hr); + while (ProcessOutput()) { } + } + while (ProcessOutput()) { } + } + + /// Runs the asynchronous MFT event loop until holds. + private void Pump(Func done) + { + while (!done()) + { + ComObjects.Check(_events!.GetEvent(0, out var pointer), "IMFMediaEventGenerator.GetEvent"); + var mediaEvent = ComObjects.Attach(pointer); + uint type; + int status; + try + { + ComObjects.Check(mediaEvent.GetEventType(out type), "IMFMediaEvent.GetType"); + ComObjects.Check(mediaEvent.GetStatus(out status), "IMFMediaEvent.GetStatus"); + } + finally + { + ComObjects.Release(mediaEvent); + } + if (status < 0 || type == MfNative.EventError) throw new COMException($"Encoder reported event {type} with failure", status); + switch (type) + { + case MfNative.EventNeedInput: + _needInput++; + break; + case MfNative.EventHaveOutput: + ProcessOutput(); + break; + case MfNative.EventDrainComplete: + _drained = true; + break; + } + } + } + + /// Pulls one output sample; false when the encoder needs more input. + private bool ProcessOutput() + { + IMFSample? own = null; + nint ownPointer = 0; + var data = new MftOutputDataBuffer(); + if (!_providesSamples) + { + own = CreateOutputSample(); + data.Sample = ownPointer = ComObjects.GetPointer(own); + } + int hr; + try + { + hr = _transform.ProcessOutput(0, 1, ref data, out _); + } + finally + { + if (data.Events != 0) Marshal.Release(data.Events); + if (ownPointer != 0) Marshal.Release(ownPointer); + } + + if (hr < 0) + { + ComObjects.Release(own); + if (hr == MfNative.TransformNeedMoreInput) return false; + if (hr == MfNative.TransformStreamChange) + { + Renegotiate(); + return true; + } + ComObjects.Check(hr, "ProcessOutput"); + } + + var output = own ?? ComObjects.AttachOrNull(data.Sample); + if (output is null) return true; + try + { + Deliver(output); + } + finally + { + ComObjects.Release(output); + } + return true; + } + + private IMFSample CreateOutputSample() + { + ComObjects.Check(MfNative.MFCreateMemoryBuffer(_outputBufferSize, out var bufferPointer), "MFCreateMemoryBuffer"); + var buffer = ComObjects.Attach(bufferPointer); + try + { + ComObjects.Check(MfNative.MFCreateSample(out var samplePointer), "MFCreateSample"); + var sample = ComObjects.Attach(samplePointer); + ComObjects.Check(sample.AddBuffer(buffer), "AddBuffer"); + return sample; + } + finally + { + ComObjects.Release(buffer); + } + } + + /// MF_E_TRANSFORM_STREAM_CHANGE: adopt the encoder's new output type and its sequence header. + private void Renegotiate() + { + ComObjects.Check(_transform.GetOutputAvailableType(0, 0, out var pointer), "GetOutputAvailableType"); + var type = ComObjects.Attach(pointer); + try + { + ComObjects.Check(_transform.SetOutputType(0, type, 0), "SetOutputType (stream change)"); + } + finally + { + ComObjects.Release(type); + } + ReadOutputStreamInfo(); + ReadSequenceHeader(); + Log.Debug(LogCategory, $"\"{EncoderName}\" renegotiated its output type"); + } + + private void Deliver(IMFSample sample) + { + ComObjects.Check(sample.GetSampleTime(out var pts), "GetSampleTime"); + bool? cleanPoint = sample.GetUINT32(MfGuids.SampleCleanPoint, out var clean) >= 0 ? clean != 0 : null; + long? dts = sample.GetUINT64(MfGuids.SampleDecodeTimestamp, out var decode) >= 0 ? (long)decode : null; + ComObjects.Check(sample.ConvertToContiguousBuffer(out var bufferPointer), "ConvertToContiguousBuffer"); + var buffer = ComObjects.Attach(bufferPointer); + byte[] bytes; + try + { + ComObjects.Check(buffer.Lock(out var data, out _, out var length), "IMFMediaBuffer.Lock"); + try + { + bytes = new byte[length]; + Marshal.Copy(data, bytes, 0, (int)length); + } + finally + { + buffer.Unlock(); + } + } + finally + { + ComObjects.Release(buffer); + } + if (bytes.Length == 0) return; + var key = cleanPoint ?? IsRandomAccess(bytes); + + var ptsTicks = _ticksByHns.Remove(pts, out var exact) ? exact : To90k(pts); + // Without MFSampleExtension_DecodeTimestamp the n-th output decodes at the n-th input's time. + _inputStarts.TryDequeue(out var inputStart); + var dtsTicks = dts is { } d ? To90k(d) : inputStart; + var encoded = new Encoded(bytes, ptsTicks, dtsTicks, key); + if (_pending is { } previous) WritePending(previous, Math.Max(1, encoded.Dts - previous.Dts)); + _pending = encoded; + _framesOut++; + } + + /// Sync detection for encoders that do not set MFSampleExtension_CleanPoint: IDR/IRAP NAL units or an AV1 sequence header. + private bool IsRandomAccess(byte[] data) + { + if (_settings.Codec == VideoCodec.Av1) + { + foreach (var obu in Av1ObuParser.Split(data)) + { + if (obu.Type == Av1ObuType.SequenceHeader) return true; + } + return false; + } + foreach (var unit in AnnexBParser.SplitAccessUnit(data, out _)) + { + var header = data[unit.Offset]; + if (_settings.Codec == VideoCodec.H264 ? AnnexBParser.H264Type(header) == H264NalUnitType.IdrSlice + : AnnexBParser.HevcType(header) is >= HevcNalUnitType.BlaWLp and <= HevcNalUnitType.Cra) + return true; + } + return false; + } + + private void WritePending(Encoded sample, long duration) + { + _muxer.WriteSample(sample.Data, duration, sample.Key, sample.Pts - sample.Dts); + _durationByTicks.Remove(sample.Pts); + } + + private void Message(uint message) => ComObjects.Check(_transform.ProcessMessage(message, 0), $"ProcessMessage(0x{message:X})"); + + private void ReleaseEncoder() + { + if (_released) return; + _released = true; + ComObjects.Release(_transform); + _activate.ShutdownObject(); + ComObjects.Release(_activate); + } + + private static long ToHns(long ticks90k) => (ticks90k * 1000 + 4) / 9; + + private static long To90k(long hns) => (long)Math.Round(hns * 9 / 1000.0); +} diff --git a/src/Titano.Core.Windows/MediaFoundation/MediaFoundationEncoders.cs b/src/Titano.Core.Windows/MediaFoundation/MediaFoundationEncoders.cs new file mode 100644 index 00000000..b8a05dc8 --- /dev/null +++ b/src/Titano.Core.Windows/MediaFoundation/MediaFoundationEncoders.cs @@ -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; + +/// A Media Foundation video encoder MFT available on this machine. +/// MFT_FRIENDLY_NAME_Attribute, e.g. "NVIDIA HEVC Encoder MFT". +/// MFT_ENUM_HARDWARE_VENDOR_ID_Attribute of hardware MFTs, e.g. "VEN_10DE". +/// The MFT accepts P010 input (HEVC Main10 / AV1 10-bit). +public sealed record MediaFoundationEncoderInfo(VideoCodec Codec, string Name, bool IsHardware, bool IsAsync, string? VendorId, bool SupportsTenBit); + +/// Discovers H.264, HEVC and AV1 encoder MFTs with MFTEnumEx in the system's merit order (hardware first). +public static class MediaFoundationEncoders +{ + internal const string LogCategory = "MediaFoundation"; + private static readonly VideoCodec[] Codecs = [VideoCodec.H264, VideoCodec.Hevc, VideoCodec.Av1]; + + /// Every encoder MFT for H.264, HEVC and AV1; empty when Media Foundation is unavailable. + public static IReadOnlyList Enumerate() + { + if (!MediaFoundationRuntime.IsAvailable) return []; + using var lease = MediaFoundationRuntime.Acquire(); + var result = new List(); + foreach (var codec in Codecs) + { + foreach (var (info, activate) in Find(codec)) + { + result.Add(info); + ComObjects.Release(activate); + } + } + return result; + } + + /// Encoder activation objects for ; Media Foundation must be started and the caller releases them. + internal static List<(MediaFoundationEncoderInfo Info, IMFActivate Activate)> Find(VideoCodec codec) + { + var tenBit = new HashSet(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 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((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(Marshal.ReadIntPtr(array, i * IntPtr.Size))); + } + finally + { + Marshal.FreeCoTaskMem(array); + } + return list; + } +} diff --git a/src/Titano.Core.Windows/MediaFoundation/MediaFoundationRuntime.cs b/src/Titano.Core.Windows/MediaFoundation/MediaFoundationRuntime.cs new file mode 100644 index 00000000..a8cfb3a1 --- /dev/null +++ b/src/Titano.Core.Windows/MediaFoundation/MediaFoundationRuntime.cs @@ -0,0 +1,44 @@ +using System.Runtime.InteropServices; +using Titano.Core.Windows.Interop; + +namespace Titano.Core.Windows.MediaFoundation; + +/// Reference-counted MFStartup/MFShutdown shared by every Media Foundation user in the process. +internal static class MediaFoundationRuntime +{ + private static readonly Lock Gate = new(); + private static int _references; + + /// True when mfplat.dll can be loaded (absent on Windows "N" editions without the Media Feature Pack). + public static bool IsAvailable { get; } = OperatingSystem.IsWindows() && NativeLibrary.TryLoad("mfplat.dll", out _); + + /// Starts Media Foundation for the lifetime of the returned lease. + public static IDisposable Acquire() + { + if (!IsAvailable) throw new PlatformNotSupportedException("Media Foundation is not available on this system"); + lock (Gate) + { + if (_references == 0) ComObjects.Check(MfNative.MFStartup(MfNative.Version, MfNative.StartupLite), "MFStartup"); + _references++; + } + return new Lease(); + } + + private static void Release() + { + lock (Gate) + { + if (--_references == 0) MfNative.MFShutdown(); + } + } + + private sealed class Lease : IDisposable + { + private int _released; + + public void Dispose() + { + if (Interlocked.Exchange(ref _released, 1) == 0) Release(); + } + } +} diff --git a/src/Titano.Core.Windows/MediaFoundation/MfInterop.cs b/src/Titano.Core.Windows/MediaFoundation/MfInterop.cs new file mode 100644 index 00000000..a6ff08f1 --- /dev/null +++ b/src/Titano.Core.Windows/MediaFoundation/MfInterop.cs @@ -0,0 +1,314 @@ +using System.Runtime.InteropServices; +using System.Runtime.InteropServices.Marshalling; + +namespace Titano.Core.Windows.MediaFoundation; + +// Media Foundation COM interfaces in vtable order (mfobjects.h, mftransform.h, strmif.h). Every method returns the raw +// HRESULT because MF uses failure codes for flow control (MF_E_TRANSFORM_NEED_MORE_INPUT, MF_E_NOTACCEPTING…). +// Interface pointers come back as nint and are wrapped with ComObjects.Attach for deterministic release; VARIANT and +// PROPVARIANT arguments are raw pointers so the assembly keeps runtime marshalling enabled. + +[GeneratedComInterface] +[Guid("2cd2d921-c447-44a7-a13c-4adabfc247e3")] +internal partial interface IMFAttributes +{ + [PreserveSig] int GetItem(in Guid key, nint value); + [PreserveSig] int GetItemType(in Guid key, out int type); + [PreserveSig] int CompareItem(in Guid key, nint value, out int result); + [PreserveSig] int Compare(nint theirs, int matchType, out int result); + [PreserveSig] int GetUINT32(in Guid key, out uint value); + [PreserveSig] int GetUINT64(in Guid key, out ulong value); + [PreserveSig] int GetDouble(in Guid key, out double value); + [PreserveSig] int GetGUID(in Guid key, out Guid value); + [PreserveSig] int GetStringLength(in Guid key, out uint length); + [PreserveSig] int GetString(in Guid key, nint buffer, uint bufferSize, out uint length); + [PreserveSig] int GetAllocatedString(in Guid key, out nint value, out uint length); + [PreserveSig] int GetBlobSize(in Guid key, out uint size); + [PreserveSig] int GetBlob(in Guid key, nint buffer, uint bufferSize, out uint size); + [PreserveSig] int GetAllocatedBlob(in Guid key, out nint buffer, out uint size); + [PreserveSig] int GetUnknown(in Guid key, in Guid iid, out nint value); + [PreserveSig] int SetItem(in Guid key, nint value); + [PreserveSig] int DeleteItem(in Guid key); + [PreserveSig] int DeleteAllItems(); + [PreserveSig] int SetUINT32(in Guid key, uint value); + [PreserveSig] int SetUINT64(in Guid key, ulong value); + [PreserveSig] int SetDouble(in Guid key, double value); + [PreserveSig] int SetGUID(in Guid key, in Guid value); + [PreserveSig] int SetString(in Guid key, nint value); + [PreserveSig] int SetBlob(in Guid key, nint buffer, uint size); + [PreserveSig] int SetUnknown(in Guid key, nint unknown); + [PreserveSig] int LockStore(); + [PreserveSig] int UnlockStore(); + [PreserveSig] int GetCount(out uint count); + [PreserveSig] int GetItemByIndex(uint index, out Guid key, nint value); + [PreserveSig] int CopyAllItems(nint destination); +} + +[GeneratedComInterface] +[Guid("44ae0fa8-ea31-4109-8d2e-4cae4997c555")] +internal partial interface IMFMediaType : IMFAttributes +{ + [PreserveSig] int GetMajorType(out Guid type); + [PreserveSig] int IsCompressedFormat(out int compressed); + [PreserveSig] int IsEqual(nint other, out uint flags); + [PreserveSig] int GetRepresentation(Guid representation, out nint value); + [PreserveSig] int FreeRepresentation(Guid representation, nint value); +} + +[GeneratedComInterface] +[Guid("045fa593-8799-42b8-bc8d-8968c6453507")] +internal partial interface IMFMediaBuffer +{ + [PreserveSig] int Lock(out nint buffer, out uint maxLength, out uint currentLength); + [PreserveSig] int Unlock(); + [PreserveSig] int GetCurrentLength(out uint length); + [PreserveSig] int SetCurrentLength(uint length); + [PreserveSig] int GetMaxLength(out uint length); +} + +[GeneratedComInterface] +[Guid("7dc9d5f9-9ed9-44ec-9bbf-0600bb589fbb")] +internal partial interface IMF2DBuffer +{ + [PreserveSig] int Lock2D(out nint scanline0, out int pitch); + [PreserveSig] int Unlock2D(); + [PreserveSig] int GetScanline0AndPitch(out nint scanline0, out int pitch); + [PreserveSig] int IsContiguousFormat(out int contiguous); + [PreserveSig] int GetContiguousLength(out uint length); + [PreserveSig] int ContiguousCopyTo(nint destination, uint size); + [PreserveSig] int ContiguousCopyFrom(nint source, uint size); +} + +[GeneratedComInterface] +[Guid("c40a00f2-b93a-4d80-ae8c-5a1c634f58e4")] +internal partial interface IMFSample : IMFAttributes +{ + [PreserveSig] int GetSampleFlags(out uint flags); + [PreserveSig] int SetSampleFlags(uint flags); + [PreserveSig] int GetSampleTime(out long time); + [PreserveSig] int SetSampleTime(long time); + [PreserveSig] int GetSampleDuration(out long duration); + [PreserveSig] int SetSampleDuration(long duration); + [PreserveSig] int GetBufferCount(out uint count); + [PreserveSig] int GetBufferByIndex(uint index, out nint buffer); + [PreserveSig] int ConvertToContiguousBuffer(out nint buffer); + [PreserveSig] int AddBuffer(IMFMediaBuffer buffer); + [PreserveSig] int RemoveBufferByIndex(uint index); + [PreserveSig] int RemoveAllBuffers(); + [PreserveSig] int GetTotalLength(out uint length); + [PreserveSig] int CopyToBuffer(IMFMediaBuffer buffer); +} + +[GeneratedComInterface] +[Guid("7fee9e9a-4a89-47a6-899c-b6a53a70fb67")] +internal partial interface IMFActivate : IMFAttributes +{ + [PreserveSig] int ActivateObject(in Guid iid, out nint instance); + [PreserveSig] int ShutdownObject(); + [PreserveSig] int DetachObject(); +} + +[GeneratedComInterface] +[Guid("df598932-f10c-4e39-bba2-c308f101daa3")] +internal partial interface IMFMediaEvent : IMFAttributes +{ + [PreserveSig] int GetEventType(out uint type); + [PreserveSig] int GetExtendedType(out Guid type); + [PreserveSig] int GetStatus(out int status); + [PreserveSig] int GetValue(nint propVariant); +} + +[GeneratedComInterface] +[Guid("2cd0bd52-bcd5-4b89-b62c-eadc0c031e7d")] +internal partial interface IMFMediaEventGenerator +{ + [PreserveSig] int GetEvent(uint flags, out nint mediaEvent); + [PreserveSig] int BeginGetEvent(nint callback, nint state); + [PreserveSig] int EndGetEvent(nint result, out nint mediaEvent); + [PreserveSig] int QueueEvent(uint type, in Guid extendedType, int status, nint value); +} + +[GeneratedComInterface] +[Guid("bf94c121-5b05-4e6f-8000-ba598961414d")] +internal partial interface IMFTransform +{ + [PreserveSig] int GetStreamLimits(out uint inputMinimum, out uint inputMaximum, out uint outputMinimum, out uint outputMaximum); + [PreserveSig] int GetStreamCount(out uint inputStreams, out uint outputStreams); + [PreserveSig] int GetStreamIDs(uint inputSize, nint inputIds, uint outputSize, nint outputIds); + [PreserveSig] int GetInputStreamInfo(uint stream, out MftInputStreamInfo info); + [PreserveSig] int GetOutputStreamInfo(uint stream, out MftOutputStreamInfo info); + [PreserveSig] int GetAttributes(out nint attributes); + [PreserveSig] int GetInputStreamAttributes(uint stream, out nint attributes); + [PreserveSig] int GetOutputStreamAttributes(uint stream, out nint attributes); + [PreserveSig] int DeleteInputStream(uint stream); + [PreserveSig] int AddInputStreams(uint count, nint ids); + [PreserveSig] int GetInputAvailableType(uint stream, uint index, out nint type); + [PreserveSig] int GetOutputAvailableType(uint stream, uint index, out nint type); + [PreserveSig] int SetInputType(uint stream, IMFMediaType? type, uint flags); + [PreserveSig] int SetOutputType(uint stream, IMFMediaType? type, uint flags); + [PreserveSig] int GetInputCurrentType(uint stream, out nint type); + [PreserveSig] int GetOutputCurrentType(uint stream, out nint type); + [PreserveSig] int GetInputStatus(uint stream, out uint flags); + [PreserveSig] int GetOutputStatus(out uint flags); + [PreserveSig] int SetOutputBounds(long lower, long upper); + [PreserveSig] int ProcessEvent(uint stream, nint mediaEvent); + [PreserveSig] int ProcessMessage(uint message, nint parameter); + [PreserveSig] int ProcessInput(uint stream, IMFSample sample, uint flags); + [PreserveSig] int ProcessOutput(uint flags, uint count, ref MftOutputDataBuffer buffers, out uint status); +} + +[GeneratedComInterface] +[Guid("901db4c7-31ce-41a2-85dc-8fa0bf41b8da")] +internal partial interface ICodecAPI +{ + [PreserveSig] int IsSupported(in Guid api); + [PreserveSig] int IsModifiable(in Guid api); + [PreserveSig] int GetParameterRange(in Guid api, nint minimum, nint maximum, nint step); + [PreserveSig] int GetParameterValues(in Guid api, out nint values, out uint count); + [PreserveSig] int GetDefaultValue(in Guid api, nint value); + [PreserveSig] int GetValue(in Guid api, nint value); + + /// Sets a property; points to a VARIANT (a ComVariant). + [PreserveSig] int SetValue(in Guid api, nint value); + [PreserveSig] int RegisterForEvent(in Guid api, nint userData); + [PreserveSig] int UnregisterForEvent(in Guid api); + [PreserveSig] int SetAllDefaults(); + [PreserveSig] int SetValueWithNotify(in Guid api, nint value, out nint changed, out uint count); + [PreserveSig] int SetAllDefaultsWithNotify(out nint changed, out uint count); + [PreserveSig] int GetAllSettings(nint stream); + [PreserveSig] int SetAllSettings(nint stream); + [PreserveSig] int SetAllSettingsWithNotify(nint stream, out nint changed, out uint count); +} + +[StructLayout(LayoutKind.Sequential)] +internal struct MftInputStreamInfo +{ + public long MaxLatency; + public uint Flags; + public uint Size; + public uint MaxLookahead; + public uint Alignment; +} + +[StructLayout(LayoutKind.Sequential)] +internal struct MftOutputStreamInfo +{ + public uint Flags; + public uint Size; + public uint Alignment; +} + +[StructLayout(LayoutKind.Sequential)] +internal struct MftOutputDataBuffer +{ + public uint StreamId; + public nint Sample; + public uint Status; + public nint Events; +} + +[StructLayout(LayoutKind.Sequential)] +internal struct MftRegisterTypeInfo +{ + public Guid MajorType; + public Guid Subtype; +} + +/// mfplat.dll entry points and the constants this assembly uses. +internal static unsafe partial class MfNative +{ + public const uint Version = 0x0002_0070; // MF_SDK_VERSION << 16 | MF_API_VERSION + public const uint StartupLite = 1; // MFSTARTUP_NOSOCKET + + public const uint EnumSync = 0x1, EnumAsync = 0x2, EnumHardware = 0x4, EnumSortAndFilter = 0x40; + + public const uint MessageCommandDrain = 0x1; + public const uint MessageNotifyBeginStreaming = 0x1000_0000; + public const uint MessageNotifyEndStreaming = 0x1000_0001; + public const uint MessageNotifyEndOfStream = 0x1000_0002; + public const uint MessageNotifyStartOfStream = 0x1000_0003; + + public const uint OutputStreamProvidesSamples = 0x100, OutputStreamCanProvideSamples = 0x200; + + public const uint EventError = 1, EventNeedInput = 601, EventHaveOutput = 602, EventDrainComplete = 603; + + public const int TransformNeedMoreInput = unchecked((int)0xC00D6D72); + public const int TransformStreamChange = unchecked((int)0xC00D6D61); + public const int NotAccepting = unchecked((int)0xC00D36B5); + public const int NoMoreTypes = unchecked((int)0xC00D36B9); + + public const uint InterlaceProgressive = 2; + public const uint NominalRange16To235 = 2; + public const uint PrimariesBt709 = 2; + public const uint TransferBt709 = 5; + public const uint MatrixBt709 = 1; + + public const uint RateControlCbr = 0, RateControlUnconstrainedVbr = 2, RateControlQuality = 3; + + [LibraryImport("mfplat.dll")] + public static partial int MFStartup(uint version, uint flags); + + [LibraryImport("mfplat.dll")] + public static partial int MFShutdown(); + + [LibraryImport("mfplat.dll")] + public static partial int MFTEnumEx(Guid category, uint flags, MftRegisterTypeInfo* inputType, MftRegisterTypeInfo* outputType, out nint activates, out uint count); + + [LibraryImport("mfplat.dll")] + public static partial int MFCreateMediaType(out nint mediaType); + + [LibraryImport("mfplat.dll")] + public static partial int MFCreateSample(out nint sample); + + [LibraryImport("mfplat.dll")] + public static partial int MFCreateMemoryBuffer(uint maxLength, out nint buffer); + + [LibraryImport("mfplat.dll")] + public static partial int MFCreate2DMediaBuffer(uint width, uint height, uint fourCc, [MarshalAs(UnmanagedType.Bool)] bool bottomUp, out nint buffer); +} + +/// Media Foundation GUIDs (mfapi.h, mftransform.h, codecapi.h). +internal static class MfGuids +{ + public static readonly Guid VideoEncoderCategory = new("f79eac7d-e545-4387-bdee-d647d7bde42a"); + public static readonly Guid MediaTypeVideo = new("73646976-0000-0010-8000-00aa00389b71"); + public static readonly Guid FormatH264 = new("34363248-0000-0010-8000-00aa00389b71"); + public static readonly Guid FormatHevc = new("43564548-0000-0010-8000-00aa00389b71"); + public static readonly Guid FormatAv1 = new("31305641-0000-0010-8000-00aa00389b71"); + public static readonly Guid FormatNv12 = new("3231564e-0000-0010-8000-00aa00389b71"); + public static readonly Guid FormatP010 = new("30313050-0000-0010-8000-00aa00389b71"); + + public static readonly Guid MajorType = new("48eba18e-f8c9-4687-bf11-0a74c9f96a8f"); + public static readonly Guid Subtype = new("f7e34c9a-42e8-4714-b74b-cb29d72c35e5"); + public static readonly Guid FrameSize = new("1652c33d-d6b2-4012-b834-72030849a37d"); + public static readonly Guid FrameRate = new("c459a2e8-3d2c-4e44-b132-fee5156c7bb0"); + public static readonly Guid PixelAspectRatio = new("c6376a1e-8d0a-4027-be45-6d9a0ad39bb6"); + public static readonly Guid InterlaceMode = new("e2724bb8-e676-4806-b4b2-a8d6efb44ccd"); + public static readonly Guid AverageBitrate = new("20332624-fb0d-4d9e-bd0d-cbf6786c102e"); + public static readonly Guid Mpeg2Profile = new("ad76a80b-2d5c-4e0b-b375-64e520137036"); + public static readonly Guid Mpeg2Level = new("96f66574-11c5-4015-8666-bff516436da7"); + public static readonly Guid MpegSequenceHeader = new("3c036de7-3ad0-4c9e-9216-ee6d6ac21cb3"); + public static readonly Guid DefaultStride = new("644b4e48-1e02-4516-b0eb-c01ca9d49ac6"); + public static readonly Guid NominalRange = new("c21b8ee5-b956-4071-8daf-325edf5cab11"); + public static readonly Guid VideoPrimaries = new("dbfbe4d7-0740-4ee0-8192-850ab0e21935"); + public static readonly Guid TransferFunction = new("5fb0fce9-be5c-4935-a811-ec838f8eed93"); + public static readonly Guid YuvMatrix = new("3e23d450-2c75-4d25-a00e-b91670d12327"); + + public static readonly Guid TransformAsync = new("f81a699a-649a-497d-8c73-29f8fed6ad7a"); + public static readonly Guid TransformAsyncUnlock = new("e5666d6b-3422-4eb6-a421-da7db1f8e207"); + public static readonly Guid TransformFlags = new("9359bb7e-6275-46c4-a025-1c01e45f1a86"); + public static readonly Guid FriendlyName = new("314ffbae-5b41-4c95-9c19-4e7d586face3"); + public static readonly Guid HardwareVendorId = new("3aecb0cc-035b-4bcc-8185-2b8d551ef3af"); + + public static readonly Guid SampleCleanPoint = new("9cdf01d8-a0f0-43ba-b077-eaa06cbd728a"); + public static readonly Guid SampleDecodeTimestamp = new("73a954d4-09e2-4861-befc-94bd97c08e6e"); + + public static readonly Guid CodecRateControlMode = new("1c0608e9-370c-4710-8a58-cb6181c42423"); + public static readonly Guid CodecMeanBitRate = new("f7222374-2144-4815-b550-a37f8e12ee52"); + public static readonly Guid CodecQuality = new("fcbf57a3-7ea5-4b0c-9644-69b40c39c391"); + public static readonly Guid CodecGopSize = new("95f31b26-95a4-41aa-9303-246a7fc6eef1"); + public static readonly Guid CodecBPictureCount = new("8d390aac-dc5c-4200-b57f-814d04babab2"); + public static readonly Guid CodecLowLatencyMode = new("9c27891a-ed7a-40e1-88e8-b22727a024ee"); + + public static readonly Guid IidTransform = typeof(IMFTransform).GUID; +} diff --git a/tests/Titano.Core.Windows.Tests/MediaFoundation/GpuAdaptersTests.cs b/tests/Titano.Core.Windows.Tests/MediaFoundation/GpuAdaptersTests.cs new file mode 100644 index 00000000..a84fee47 --- /dev/null +++ b/tests/Titano.Core.Windows.Tests/MediaFoundation/GpuAdaptersTests.cs @@ -0,0 +1,18 @@ +using Titano.Core.Windows.Dxgi; +using Titano.Testing; + +namespace Titano.Core.Windows.Tests.MediaFoundation; + +public sealed class GpuAdaptersTests(TestContext context) +{ + [Test] + [WindowsOnly] + public void EnumeratesAtLeastTheBasicRenderDriver() + { + var adapters = GpuAdapters.Enumerate(); + foreach (var a in adapters) + context.Log($"{a.Description} vendor=0x{a.VendorId:X4} device=0x{a.DeviceId:X4} vram={a.DedicatedVideoMemory >> 20} MiB software={a.IsSoftware}"); + Assert.NotEmpty(adapters); + Assert.True(adapters.All(a => a.Description.Length > 0 && !a.Description.Contains('\0'))); + } +} diff --git a/tests/Titano.Core.Windows.Tests/MediaFoundation/MediaFoundationEncoderTests.cs b/tests/Titano.Core.Windows.Tests/MediaFoundation/MediaFoundationEncoderTests.cs new file mode 100644 index 00000000..7ec92670 --- /dev/null +++ b/tests/Titano.Core.Windows.Tests/MediaFoundation/MediaFoundationEncoderTests.cs @@ -0,0 +1,153 @@ +using Titano.Core.Imaging; +using Titano.Core.Video; +using Titano.Core.Windows.MediaFoundation; +using Titano.Testing; + +namespace Titano.Core.Windows.Tests.MediaFoundation; + +[Serial] // encoders share GPU sessions and the MF runtime +public sealed class MediaFoundationEncoderTests(TestContext context) +{ + private const long Frame30 = 3000; // 1/30 s at 90 kHz + + [Test] + public void VendorIdsMapToNames() + { + Assert.Equal("NVIDIA", MediaFoundationEncoderProvider.VendorName("VEN_10DE")); + Assert.Equal("Intel", MediaFoundationEncoderProvider.VendorName("ven_8086")); + Assert.Equal("AMD", MediaFoundationEncoderProvider.VendorName("VEN_1002")); + Assert.Equal("VEN_ABCD", MediaFoundationEncoderProvider.VendorName("VEN_ABCD")); + Assert.Null(MediaFoundationEncoderProvider.VendorName(null)); + } + + [Test] + [WindowsOnly] + public void EnumeratesEncodersAndHidesSoftwareHevcAndAv1() + { + RequireMediaFoundation(); + var encoders = MediaFoundationEncoders.Enumerate(); + foreach (var e in encoders) context.Log($"{e.Codec} \"{e.Name}\" hardware={e.IsHardware} async={e.IsAsync} vendor={e.VendorId} 10-bit={e.SupportsTenBit}"); + Assert.True(encoders.All(e => !string.IsNullOrWhiteSpace(e.Name))); + + var capabilities = new MediaFoundationEncoderProvider().Capabilities; + Assert.True(capabilities.All(c => c.Hardware || c.Codec == VideoCodec.H264), "HEVC/AV1 are hardware-only"); + Assert.True(capabilities.All(c => !c.SupportsTenBit || c.Codec != VideoCodec.H264)); + Assert.Equal(encoders.Count(e => e.IsHardware || e.Codec == VideoCodec.H264), capabilities.Count); + } + + [Test] + [WindowsOnly] + [Timeout(60_000)] + public void SoftwareH264EncodeProducesAValidMp4() + { + RequireEncoder(VideoCodec.H264, hardware: false, tenBit: false); + var dir = context.CreateTempDirectory(); + var settings = Settings(Path.Combine(dir, "software.mp4"), VideoCodec.H264, 8) with { PreferHardware = false }; + var provider = new MediaFoundationEncoderProvider(); + Assert.True(provider.CanEncode(settings)); + using (var session = provider.Open(settings)) + { + Assert.False(session.IsHardware); + context.Log("encoder: " + session.EncoderName); + WriteFrames(session, 60, 8); + session.Complete(); + } + + var report = Mp4Inspector.Inspect(settings.OutputPath); + context.Log(report.ToString()); + Assert.True(report.IsValid, string.Join("; ", report.Errors)); + Assert.Equal(VideoCodec.H264, report.Codec); + Assert.Equal(1280, report.Width); + Assert.Equal(720, report.Height); + Assert.Equal(60, report.FrameCount); + Assert.Approximately(2.0, report.Duration.TotalSeconds, 0.034); + Assert.True(report.FastStart); + Assert.Count(1, Directory.GetFiles(dir)); // no temporary file left behind + } + + [Test] + [WindowsOnly] + [Timeout(60_000)] + public void HardwareHevcMain10EncodeWhenAvailable() + { + RequireEncoder(VideoCodec.Hevc, hardware: true, tenBit: true); + var dir = context.CreateTempDirectory(); + var settings = Settings(Path.Combine(dir, "main10.mp4"), VideoCodec.Hevc, 10); + using (var session = new MediaFoundationEncoderProvider().Open(settings)) + { + Assert.True(session.IsHardware); + context.Log("encoder: " + session.EncoderName); + WriteFrames(session, 60, 10); + session.Complete(); + } + var report = Mp4Inspector.Inspect(settings.OutputPath); + context.Log(report.ToString()); + Assert.True(report.IsValid, string.Join("; ", report.Errors)); + Assert.Equal(VideoCodec.Hevc, report.Codec); + Assert.Equal(10, report.BitDepth); + Assert.Equal(60, report.FrameCount); + } + + [Test] + [WindowsOnly] + [Timeout(60_000)] + public void DisposeWithoutCompleteLeavesNoFile() + { + RequireEncoder(VideoCodec.H264, hardware: false, tenBit: false); + var dir = context.CreateTempDirectory(); + var settings = Settings(Path.Combine(dir, "cancelled.mp4"), VideoCodec.H264, 8) with { PreferHardware = false }; + using (var session = new MediaFoundationEncoderProvider().Open(settings)) + { + WriteFrames(session, 5, 8); + } + Assert.Empty(Directory.GetFiles(dir)); + } + + private static EncodeSettings Settings(string path, VideoCodec codec, int bitDepth) => new() + { + OutputPath = path, + Codec = codec, + Width = 1280, + Height = 720, + FrameRate = new Rational(30, 1), + BitDepth = bitDepth, + Profile = codec == VideoCodec.Hevc ? (bitDepth == 10 ? CodecProfile.HevcMain10 : CodecProfile.HevcMain) : CodecProfile.H264High, + BitRate = 4_000_000, + GopLength = 30, + }; + + /// A moving colour ramp with a bright square, distinct per frame. + private static void WriteFrames(IVideoEncoderSession session, int count, int bitDepth) + { + using var frame = new ImageBuffer(1280, 720); + for (var f = 0; f < count; f++) + { + for (var y = 0; y < 720; y++) + { + var r = frame.Row(0, y); + var g = frame.Row(1, y); + var b = frame.Row(2, y); + for (var x = 0; x < 1280; x++) + { + var square = Math.Abs(x - 100 - f * 15) < 60 && Math.Abs(y - 360) < 60; + r[x] = square ? 1f : (float)ColorSpace.SrgbToLinear((x + f * 7) % 1280 / 1280.0); + g[x] = square ? 1f : (float)ColorSpace.SrgbToLinear(y / 720.0); + b[x] = (float)ColorSpace.SrgbToLinear(bitDepth == 10 ? 0.5 + 0.4 * Math.Sin((x + y) * 0.01) : 0.3); + } + } + session.WriteFrame(frame, Frame30); + } + } + + private static void RequireMediaFoundation() + { + if (!MediaFoundationRuntime.IsAvailable) TestContext.Skip("Media Foundation is not installed (Windows N or Server without Media Foundation)"); + } + + private static void RequireEncoder(VideoCodec codec, bool hardware, bool tenBit) + { + RequireMediaFoundation(); + if (!MediaFoundationEncoders.Enumerate().Any(e => e.Codec == codec && e.IsHardware == hardware && (!tenBit || e.SupportsTenBit))) + TestContext.Skip($"No {(hardware ? "hardware" : "software")} {codec}{(tenBit ? " 10-bit" : string.Empty)} encoder on this machine"); + } +}