Files
Chronos/tests/Chronos.Core.Tests/Export/EncoderSelectorTests.cs
T
Alby96andClaude Opus 5.5 188b2c2e55
CI / Build and test (push) Successful in 8m19s
CI / Windows tests and installer (push) Skipped
CI / YAML check (push) Successful in 7s
Release / Build, test and publish (push) Successful in 12m9s
CI / PostgreSQL 16 integration (push) Successful in 7m49s
CI / PostgreSQL 17 integration (push) Successful in 7m19s
CI / Docker image (push) Successful in 46s
Release / Server image (push) Successful in 1m14s
feat(editor): visual crop, rotate/mirror/flip/straighten, colour looks (owner review 1 phase 4) (#26)
Owner review 1 R5 + R6. Camera rotation, mirror, flip, straighten and crop handles locked to the output; an Exposure Look group; Export output size ("proportions win"); settings format 2 with older-node gates. Also fixes project text and hashes differing between Windows and Linux.

Closes #19
Closes #20

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
Co-authored-by: Alberto Balbo <alberto.balbo.96@gmail.com>
2026-11-05 01:09:09 +01:00

163 lines
8.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 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<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 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));
}
}