Sessions write to a temporary file or folder that Complete renames into place and Dispose deletes; constant-rate outputs repeat held frames. Adds AVI/GIF/APNG readers used to verify rendered outputs. Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com> Claude-Session: https://claude.ai/code/session_012iLMBNwBE2GnphAsu4cjaC
191 lines
8.3 KiB
C#
191 lines
8.3 KiB
C#
using Titano.Core.Codecs.Jpeg;
|
|
using Titano.Core.Codecs.Png;
|
|
using Titano.Core.Codecs.Tiff;
|
|
using Titano.Core.Imaging;
|
|
using Titano.Core.Render.Diagnostics;
|
|
using Titano.Core.Video;
|
|
using Titano.Core.Video.Managed;
|
|
using Titano.Testing;
|
|
|
|
namespace Titano.Core.Tests.Export;
|
|
|
|
public sealed class ManagedEncoderTests
|
|
{
|
|
private const int Width = 48;
|
|
private const int Height = 32;
|
|
private static readonly Rational Fps = new(30, 1);
|
|
|
|
private readonly TestContext _context;
|
|
|
|
public ManagedEncoderTests(TestContext context) => _context = context;
|
|
|
|
private static ImageBuffer Frame(int seed)
|
|
{
|
|
var image = new ImageBuffer(Width, Height);
|
|
for (var y = 0; y < Height; y++)
|
|
{
|
|
for (var x = 0; x < Width; x++)
|
|
{
|
|
image[0, x, y] = (x + seed * 7) % Width / (float)Width;
|
|
image[1, x, y] = y / (float)Height * 0.8f;
|
|
image[2, x, y] = (x * y + seed * 13) % 23 / 22f * 0.5f;
|
|
}
|
|
}
|
|
return image;
|
|
}
|
|
|
|
private static EncodeSettings Settings(string path, VideoCodec codec, int bitDepth = 8) =>
|
|
new() { OutputPath = path, Codec = codec, Width = Width, Height = Height, FrameRate = Fps, BitDepth = bitDepth, Quality = 95 };
|
|
|
|
/// <summary>Three distinct frames lasting one, two and one periods.</summary>
|
|
private static void Encode(IVideoEncoderSession session)
|
|
{
|
|
long[] durations = [3000, 6000, 3000];
|
|
for (var i = 0; i < 3; i++)
|
|
{
|
|
using var frame = Frame(i);
|
|
session.WriteFrame(frame, durations[i]);
|
|
}
|
|
session.Complete();
|
|
}
|
|
|
|
private static double Psnr(ImageBuffer expected, ImageBuffer actual)
|
|
{
|
|
var a = PixelConvert.ToInterleaved8(expected, 3);
|
|
var b = PixelConvert.ToInterleaved8(actual, 3);
|
|
double sum = 0;
|
|
for (var i = 0; i < a.Length; i++) sum += (a[i] - b[i]) * (a[i] - b[i]);
|
|
return sum == 0 ? double.PositiveInfinity : 10 * Math.Log10(255.0 * 255 / (sum / a.Length));
|
|
}
|
|
|
|
[Test]
|
|
public void MjpegAviRepeatsHeldFramesOnTheConstantRateGrid()
|
|
{
|
|
var dir = _context.CreateTempDirectory();
|
|
var path = Path.Combine(dir, "out.avi");
|
|
using (var session = new ManagedEncoderProvider().Open(Settings(path, VideoCodec.Mjpeg))) Encode(session);
|
|
var avi = AviReader.Read(File.ReadAllBytes(path));
|
|
Assert.Equal(4, avi.Frames.Count);
|
|
Assert.Equal(4, avi.HeaderFrames);
|
|
Assert.Equal(4, avi.IndexEntries);
|
|
Assert.Equal(Fps, avi.FrameRate);
|
|
Assert.BytesEqual(avi.Frames[1], avi.Frames[2]);
|
|
using var decoded = JpegDecoder.DecodeToLinear(avi.Frames[3]);
|
|
using var expected = Frame(2);
|
|
Assert.Greater(Psnr(expected, decoded), 35);
|
|
Assert.SequenceEqual(new[] { "out.avi" }, Directory.GetFiles(dir).Select(Path.GetFileName));
|
|
}
|
|
|
|
[Theory]
|
|
[Case(VideoCodec.JpegSequence, 8)]
|
|
[Case(VideoCodec.PngSequence, 8)]
|
|
[Case(VideoCodec.PngSequence, 16)]
|
|
[Case(VideoCodec.Tiff16Sequence, 16)]
|
|
[Case(VideoCodec.LinearDngSequence, 16)]
|
|
public void SequencesWriteNumberedFilesIntoTheOutputFolder(VideoCodec codec, int bitDepth)
|
|
{
|
|
var dir = _context.CreateTempDirectory();
|
|
var folder = Path.Combine(dir, "Harbour - sequence");
|
|
using (var session = new ManagedEncoderProvider().Open(Settings(folder, codec, bitDepth))) Encode(session);
|
|
Assert.SequenceEqual(new[] { "Harbour - sequence" }, Directory.GetFileSystemEntries(dir).Select(Path.GetFileName));
|
|
var ext = VideoCodecInfo.FileExtension(codec);
|
|
var files = Directory.GetFiles(folder).Order(StringComparer.Ordinal).ToArray();
|
|
Assert.SequenceEqual(Enumerable.Range(1, 4).Select(i => $"Harbour - sequence_{i:D6}{ext}"), files.Select(Path.GetFileName));
|
|
Assert.BytesEqual(File.ReadAllBytes(files[1]), File.ReadAllBytes(files[2]));
|
|
using var expected = Frame(2);
|
|
var bytes = File.ReadAllBytes(files[3]);
|
|
switch (codec)
|
|
{
|
|
case VideoCodec.JpegSequence:
|
|
using (var jpeg = JpegDecoder.DecodeToLinear(bytes)) Assert.Greater(Psnr(expected, jpeg), 35);
|
|
break;
|
|
case VideoCodec.PngSequence when bitDepth == 8:
|
|
Assert.BytesEqual(PixelConvert.ToInterleaved8(expected, 3), PngDecoder.Decode(bytes).Pixels8);
|
|
break;
|
|
case VideoCodec.PngSequence:
|
|
Assert.SequenceEqual(PixelConvert.ToInterleaved16(expected, 3), PngDecoder.Decode(bytes).Pixels16!);
|
|
break;
|
|
case VideoCodec.Tiff16Sequence:
|
|
using (var tiff = TiffImageReader.Decode(bytes))
|
|
{
|
|
for (var i = 0; i < tiff.Data.Length; i += 7) Assert.Approximately(expected.Data[i], tiff.Data[i], 2e-4);
|
|
}
|
|
break;
|
|
default:
|
|
using (var dng = TiffImageReader.Decode(bytes))
|
|
{
|
|
for (var i = 0; i < dng.Data.Length; i += 7) Assert.Approximately(expected.Data[i], dng.Data[i], 1.0 / 65535);
|
|
}
|
|
break;
|
|
}
|
|
}
|
|
|
|
[Theory]
|
|
[Case(VideoCodec.Gif)]
|
|
[Case(VideoCodec.Apng)]
|
|
public void AnimationsKeepVariableDurations(VideoCodec codec)
|
|
{
|
|
var dir = _context.CreateTempDirectory();
|
|
var path = Path.Combine(dir, "out" + VideoCodecInfo.FileExtension(codec));
|
|
using (var session = new ManagedEncoderProvider().Open(Settings(path, codec))) Encode(session);
|
|
var bytes = File.ReadAllBytes(path);
|
|
var animation = codec == VideoCodec.Gif ? GifReader.Decode(bytes) : ApngReader.Decode(bytes);
|
|
Assert.Equal(3, animation.Frames.Count);
|
|
if (codec == VideoCodec.Apng)
|
|
{
|
|
Assert.SequenceEqual(new long[] { 3000, 6000, 3000 }, animation.Frames.Select(f => f.Duration90k));
|
|
using var decoded = animation.ToLinear(1);
|
|
using var expected = Frame(1);
|
|
Assert.True(Psnr(expected, decoded) > 60);
|
|
}
|
|
else
|
|
{
|
|
Assert.Approximately(12000, animation.Duration90k, 900);
|
|
using var decoded = animation.ToLinear(1);
|
|
using var expected = Frame(1);
|
|
Assert.Greater(Psnr(expected, decoded), 22);
|
|
}
|
|
}
|
|
|
|
[Theory]
|
|
[Case(VideoCodec.Mjpeg)]
|
|
[Case(VideoCodec.PngSequence)]
|
|
[Case(VideoCodec.Gif)]
|
|
[Case(VideoCodec.Apng)]
|
|
public void DisposingWithoutCompleteLeavesNothing(VideoCodec codec)
|
|
{
|
|
var dir = _context.CreateTempDirectory();
|
|
var path = Path.Combine(dir, "partial" + (VideoCodecInfo.DefaultContainer(codec) == ContainerFormat.ImageSequence ? string.Empty : VideoCodecInfo.FileExtension(codec)));
|
|
using (var session = new ManagedEncoderProvider().Open(Settings(path, codec)))
|
|
{
|
|
using var frame = Frame(0);
|
|
session.WriteFrame(frame, 3000);
|
|
session.WriteFrame(frame, 3000);
|
|
}
|
|
Assert.Empty(Directory.GetFileSystemEntries(dir));
|
|
}
|
|
|
|
[Test]
|
|
public void SessionsValidateTheirInput()
|
|
{
|
|
var dir = _context.CreateTempDirectory();
|
|
var provider = new ManagedEncoderProvider();
|
|
using (var session = provider.Open(Settings(Path.Combine(dir, "a.avi"), VideoCodec.Mjpeg)))
|
|
{
|
|
using var wrong = new ImageBuffer(Width + 2, Height);
|
|
Assert.Throws<ArgumentException>(() => session.WriteFrame(wrong, 3000));
|
|
Assert.Throws<InvalidOperationException>(session.Complete);
|
|
}
|
|
Assert.Empty(Directory.GetFileSystemEntries(dir));
|
|
|
|
var existing = Directory.CreateDirectory(Path.Combine(dir, "taken")).FullName;
|
|
Assert.Throws<IOException>(() => provider.Open(Settings(existing, VideoCodec.PngSequence)));
|
|
Assert.False(provider.CanEncode(Settings(Path.Combine(dir, "x.mp4"), VideoCodec.H264)));
|
|
Assert.False(provider.CanEncode(Settings(Path.Combine(dir, "x"), VideoCodec.PngSequence, bitDepth: 10)));
|
|
Assert.True(provider.CanEncode(Settings(Path.Combine(dir, "x"), VideoCodec.PngSequence, bitDepth: 16)));
|
|
Assert.Throws<NotSupportedException>(() => provider.Open(Settings(Path.Combine(dir, "x.mp4"), VideoCodec.Hevc)));
|
|
Assert.Equal(7, provider.Capabilities.Count);
|
|
Assert.True(provider.Capabilities.All(c => !c.Hardware && VideoCodecInfo.IsManaged(c.Codec)));
|
|
}
|
|
}
|