Files
Chronos/tests/Titano.Core.Tests/Export/EncoderSelectorTests.cs
T
Claude f9c83af609 feat(export): encoder selection with hardware preference, fallbacks and level caps
Client renders prefer hardware Media Foundation encoders, fall back from
HEVC/AV1 to H.264, drop to 8-bit and cap size/bit rate with reason keys;
the server reports that MP4 codecs need a render node.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
Claude-Session: https://claude.ai/code/session_012iLMBNwBE2GnphAsu4cjaC
2026-10-02 10:48:34 +00:00

147 lines
7.5 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 Titano.Core.Export;
using Titano.Core.Tests.Render;
using Titano.Core.Video;
using Titano.Core.Video.Managed;
using Titano.Testing;
namespace Titano.Core.Tests.Export;
public sealed class EncoderSelectorTests
{
private static readonly EncoderCapability SoftwareH264 = new(VideoCodec.H264, "Microsoft H.264", false, 4096, 2304, false);
private static readonly EncoderCapability HardwareH264 = new(VideoCodec.H264, "NVENC H.264", true, 4096, 4096, false, "NVIDIA");
private static readonly EncoderCapability HardwareHevc = new(VideoCodec.Hevc, "NVENC HEVC", true, 8192, 8192, true, "NVIDIA");
private static readonly EncoderCapability HardwareHevc8 = new(VideoCodec.Hevc, "QSV HEVC", true, 8192, 8192, false, "Intel");
private static EncodeSettings Request(VideoCodec codec, int width = 3840, int height = 2160, int bitDepth = 8, long bitRate = 20_000_000, Rational? fps = null) => new()
{
OutputPath = "/exports/out" + VideoCodecInfo.FileExtension(codec),
Codec = codec,
Width = width,
Height = height,
FrameRate = fps ?? new Rational(30, 1),
BitDepth = bitDepth,
BitRate = bitRate,
Profile = bitDepth == 10 ? CodecProfile.HevcMain10 : CodecProfile.Auto,
};
private static IVideoEncoderProvider[] Client(params EncoderCapability[] capabilities) =>
[new CapturingEncoderProvider("Media Foundation", capabilities), new ManagedEncoderProvider()];
[Test]
public void ManagedCodecsUseTheManagedProviderEverywhere()
{
foreach (var host in new[] { EncoderHost.Client, EncoderHost.Server })
{
var selection = EncoderSelector.Select(Request(VideoCodec.Mjpeg, bitDepth: 8), Client(HardwareH264), host);
Assert.True(selection.IsReady);
Assert.Equal(ManagedEncoderProvider.ProviderName, selection.Provider!.Name);
Assert.Equal("Managed MJPEG", selection.Capability!.Name);
Assert.Empty(selection.Notices);
}
}
[Test]
public void ManagedBitDepthsAreAdjustedWithANotice()
{
var png = EncoderSelector.Select(Request(VideoCodec.PngSequence, bitDepth: 10), [new ManagedEncoderProvider()], EncoderHost.Server);
Assert.Equal(16, png.Settings.BitDepth);
Assert.Equal(EncoderNotices.BitDepthAdjusted, png.Notices.Single().Key);
Assert.SequenceEqual(new[] { "10", "16" }, png.Notices.Single().Args);
var jpeg = EncoderSelector.Select(Request(VideoCodec.JpegSequence, bitDepth: 16), [new ManagedEncoderProvider()], EncoderHost.Server);
Assert.Equal(8, jpeg.Settings.BitDepth);
}
[Test]
public void ServerNeedsARenderNodeForMp4()
{
var selection = EncoderSelector.Select(Request(VideoCodec.H264), [new ManagedEncoderProvider()], EncoderHost.Server);
Assert.Equal(EncoderSelectionStatus.NeedsRenderNode, selection.Status);
Assert.Null(selection.Provider);
Assert.Equal(EncoderNotices.NeedsRenderNode, selection.Notices.Single().Key);
Assert.Throws<InvalidOperationException>(() => selection.Open());
}
[Test]
public void HardwareIsPreferredUnlessSoftwareIsAsked()
{
var providers = Client(SoftwareH264, HardwareH264);
var hardware = EncoderSelector.Select(Request(VideoCodec.H264), providers, EncoderHost.Client);
Assert.True(hardware.IsReady);
Assert.Equal("NVENC H.264", hardware.Capability!.Name);
Assert.Empty(hardware.Notices);
Assert.Equal(new CodecLevel(VideoCodec.H264, 5, 1), hardware.Level);
var software = EncoderSelector.Select(Request(VideoCodec.H264) with { PreferHardware = false }, providers, EncoderHost.Client);
Assert.Equal("Microsoft H.264", software.Capability!.Name);
var named = EncoderSelector.Select(Request(VideoCodec.H264) with { EncoderName = "Microsoft H.264" }, providers, EncoderHost.Client);
Assert.Equal("Microsoft H.264", named.Capability!.Name);
Assert.Equal(EncoderNotices.SoftwareOnly, named.Notices.Single().Key);
}
[Test]
public void HevcFallsBackToH264WhenNoEncoderExists()
{
var selection = EncoderSelector.Select(Request(VideoCodec.Hevc, bitDepth: 10, bitRate: 14_000_000), Client(SoftwareH264), EncoderHost.Client);
Assert.True(selection.IsReady);
Assert.Equal(VideoCodec.H264, selection.Settings.Codec);
Assert.Equal(8, selection.Settings.BitDepth);
Assert.Equal(CodecProfile.H264High, selection.Settings.Profile);
Assert.Equal(20_000_000L, selection.Settings.BitRate);
Assert.Equal(EncoderNotices.FallbackToH264, selection.Notices[0].Key);
Assert.SequenceEqual(new[] { "Hevc" }, selection.Notices[0].Args);
Assert.Contains(selection.Notices, n => n.Key == EncoderNotices.SoftwareOnly);
Assert.Equal(".mp4", Path.GetExtension(selection.Settings.OutputPath));
}
[Test]
public void TenBitNeedsATenBitEncoder()
{
var eightOnly = EncoderSelector.Select(Request(VideoCodec.Hevc, bitDepth: 10), Client(HardwareHevc8), EncoderHost.Client);
Assert.Equal(8, eightOnly.Settings.BitDepth);
Assert.Equal(CodecProfile.HevcMain, eightOnly.Settings.Profile);
Assert.Equal(EncoderNotices.TenBitUnavailable, eightOnly.Notices.Single().Key);
var tenBit = EncoderSelector.Select(Request(VideoCodec.Hevc, bitDepth: 10), Client(HardwareHevc8, HardwareHevc), EncoderHost.Client);
Assert.Equal("NVENC HEVC", tenBit.Capability!.Name);
Assert.Equal(10, tenBit.Settings.BitDepth);
Assert.Empty(tenBit.Notices);
}
[Test]
public void SizeIsCappedByEncoderAndLevel()
{
var encoder = EncoderSelector.Select(Request(VideoCodec.H264, 7680, 4320), Client(HardwareH264), EncoderHost.Client);
Assert.Equal((4096, 2304), (encoder.Settings.Width, encoder.Settings.Height));
Assert.Equal(EncoderNotices.ResolutionCappedByEncoder, encoder.Notices.Single().Key);
Assert.SequenceEqual(new[] { "NVENC H.264", "4096×2304" }, encoder.Notices.Single().Args);
var level = EncoderSelector.Select(Request(VideoCodec.Hevc, 7680, 4320, fps: new Rational(240, 1)), Client(HardwareHevc), EncoderHost.Client);
Assert.Equal(EncoderNotices.ResolutionCappedByLevel, level.Notices[0].Key);
Assert.Less(level.Settings.Width, 7680);
Assert.Equal(0, level.Settings.Width % 2);
Assert.Approximately(16.0 / 9, level.Settings.Width / (double)level.Settings.Height, 0.01);
Assert.True(CodecLimits.Check(VideoCodec.Hevc, level.Settings.Width, level.Settings.Height, 240, level.Settings.BitRate).Fits);
var odd = EncoderSelector.Select(Request(VideoCodec.H264, 1081, 607), Client(HardwareH264), EncoderHost.Client);
Assert.Equal((1080, 606), (odd.Settings.Width, odd.Settings.Height));
}
[Test]
public void BitRateIsCappedByTheHighestLevel()
{
var selection = EncoderSelector.Select(Request(VideoCodec.H264, 1920, 1080, bitRate: 2_000_000_000), Client(HardwareH264), EncoderHost.Client);
Assert.Equal(1_000_000_000L, selection.Settings.BitRate);
Assert.Equal(EncoderNotices.BitRateCapped, selection.Notices.Single().Key);
}
[Test]
public void NoVideoEncoderMeansUnavailable()
{
var selection = EncoderSelector.Select(Request(VideoCodec.Av1), [new ManagedEncoderProvider()], EncoderHost.Client);
Assert.Equal(EncoderSelectionStatus.Unavailable, selection.Status);
Assert.SequenceEqual(new[] { EncoderNotices.FallbackToH264, EncoderNotices.NoEncoder }, selection.Notices.Select(n => n.Key));
}
}