Files
Chronos/src/Chronos.Core.Windows/MediaFoundation/MediaFoundationEncoders.cs
T
Alby96andClaude Opus 5.5 f2b58522b9
CI / Build and test (push) Successful in 8m14s
CI / Windows tests and installer (push) Skipped
CI / YAML check (push) Successful in 7s
Release / Build, test and publish (push) Successful in 13m19s
CI / PostgreSQL 16 integration (push) Successful in 4m49s
CI / PostgreSQL 17 integration (push) Successful in 5m28s
CI / Docker image (push) Successful in 53s
Release / Server image (push) Failing after 10s
refactor!: rename Titano to Chronos and move CI and releases to Gitea (#6)
The owner renamed the product and moved the repository to the self-hosted
Gitea (https://gitea.encke-hake.ts.net/BalboSoft/Chronos) for more control,
and asked for Actions and a first automatic release with the server package
and the Windows exe (docs/requests/2026-10-23-chronos-gitea.md).

- Chronos everywhere: a byte-exact rename of contents and paths, then a
  per-language review of the forms the old name's ending had dictated.
- Gitea instead of GitHub: the update check, licence, issue and support
  links read ChronosReleasesUrl; the update check takes the highest stable
  version, offers a pre-release build its final release and reports a
  repository it cannot see as a failed check. EULA, security policy and
  Gitea issue forms live at the repository root.
- Gitea Actions for the Linux act_runner (.gitea/workflows): CI, release,
  nightly image and performance gates in the .NET SDK container with git
  checkout and the system SQLite. The release cross-compiles the Windows
  programs, checks their icon, version and DPI manifest, packs installers
  and zips, publishes the Gitea release and pushes the server image
  (REGISTRY_TOKEN). Windows-only tests run in an opt-in job on a Windows
  runner (WINDOWS_RUNNER).
- New Chronos.Tools commands: zip, gitea-release, postgres-ci (replaces the
  docker-exec script, which Gitea's service containers do not allow; the
  throw-away server runs without durability) and pe-check.
- Timed tests take their budget from TestTiming (CHRONOS_TEST_TIME_FACTOR,
  3 on the shared runner; the weekly performance gates keep theirs).

BREAKING CHANGE: programs, settings, data folders, file types, environment
variables and the Docker image use the new name; nothing is migrated from
Titano, which never had a release.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
Co-authored-by: Alberto Balbo <alberto.balbo.96@gmail.com>
2026-10-23 12:45:18 +02:00

96 lines
4.5 KiB
C#

using System.Runtime.InteropServices;
using Chronos.Core.Diagnostics;
using Chronos.Core.Video;
using Chronos.Core.Windows.Interop;
namespace Chronos.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;
}
}