using Chronos.Core.Export; using Chronos.Core.Tests.Render; using Chronos.Core.Video; using Chronos.Core.Video.Managed; using Chronos.Testing; namespace Chronos.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(() => 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 APortraitFrameReadsTheLimitTurned() { // An encoder announced as 4096×2304 takes 4K at 9:16: its limit is long edge × short edge. var portrait = EncoderSelector.Select(Request(VideoCodec.H264, 2160, 3840), Client(SoftwareH264), EncoderHost.Client); Assert.Equal((2160, 3840), (portrait.Settings.Width, portrait.Settings.Height)); Assert.False(portrait.Notices.Any(n => n.Key is EncoderNotices.ResolutionCappedByEncoder or EncoderNotices.ResolutionCappedByLevel), "not capped"); var capped = EncoderSelector.Select(Request(VideoCodec.H264, 4320, 7680), Client(SoftwareH264), EncoderHost.Client); Assert.Equal((2304, 4096), (capped.Settings.Width, capped.Settings.Height)); Assert.True(capped.Notices.Any(n => n.Key == EncoderNotices.ResolutionCappedByEncoder)); Assert.True(EncoderSelector.Takes(SoftwareH264, 2304, 4096)); Assert.False(EncoderSelector.Takes(SoftwareH264, 2400, 3840), "the short edge is over the limit"); } [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)); } }