using System.IO;
using Chronos.App.Editor;
using Chronos.Core.Export;
using Chronos.Core.Imaging;
using Chronos.Core.Project;
using Chronos.Core.Render;
using Chronos.Core.Render.Diagnostics;
using Chronos.Core.Video;
using Chronos.Core.Video.Managed;
using Chronos.Testing;
namespace Chronos.App.Tests.Editor;
/// The corrected-frame preview through the render pipeline at preview size.
public sealed class PreviewRendererTests
{
private static (PreviewRenderer Renderer, FakeSequenceSource Source) Create()
{
var content = EditorFixture.Content(EditorFixture.Sunset);
var source = new FakeSequenceSource(EditorFixture.Sunset, content);
return (new PreviewRenderer(content, source), source);
}
private static ProjectSettings Deflickered()
{
var p = new ProjectSettings();
return p with { Exposure = p.Exposure with { Deflicker = p.Exposure.Deflicker.WithManual(true), HighlightKnee = null } };
}
[Test]
public async Task DeflickerFlattensThePreviewedLuminance()
{
var (renderer, _) = Create();
var project = Deflickered();
var corrections = await renderer.PrepareAsync(project, CancellationToken.None);
Assert.Same(corrections, renderer.Cached(project));
Assert.Null(renderer.Cached(new ProjectSettings()), "other exposure settings need their own preparation");
var corrected = new List();
var original = new List();
for (var i = 4; i < 20; i++)
{
using var frame = await renderer.RenderAsync(new PreviewRequest(i, project, 64, PreviewFraming.Output), corrections, CancellationToken.None);
Assert.NotNull(frame);
corrected.Add(LogMean(frame!.Corrected));
original.Add(LogMean(frame.Original));
}
Assert.Less(Spread(corrected), Spread(original) * 0.6, "flicker removed between the steps");
}
[Test]
public async Task OutputFramingFollowsTheExportAspect()
{
var (renderer, _) = Create();
var project = new ProjectSettings();
project = project with { Export = project.Export with { Preset = project.Export.Preset.WithManual("square-1440") } };
using var output = await renderer.RenderAsync(new PreviewRequest(0, project, 80, PreviewFraming.Output), null, CancellationToken.None);
Assert.Equal(80, output!.Corrected.Width, "the square output at the preview's long edge");
Assert.Equal(80, output.Corrected.Height);
Assert.Equal(output.Corrected.Width, output.Original.Width);
using var source = await renderer.RenderAsync(new PreviewRequest(0, project, 80, PreviewFraming.Source), null, CancellationToken.None);
Assert.Equal(80, source!.Corrected.Width);
Assert.Approximately(64.0 / 96, source.Crop.Width, 0.02, "the square crop keeps two thirds of the 3:2 frame");
Assert.Approximately(1, source.Crop.Height, 0.02);
Assert.Approximately((1 - 64.0 / 96) / 2, source.Crop.X, 0.02, "centred");
}
[Test]
public async Task VectorsDescribeTheMotionToTheNextFrame()
{
var (renderer, _) = Create();
using var frame = await renderer.RenderAsync(new PreviewRequest(10, new ProjectSettings(), 96, PreviewFraming.Source, Vectors: true), null,
CancellationToken.None);
Assert.NotEmpty(frame!.Vectors);
Assert.True(frame.Vectors.All(v => v.X >= 0 && v.X < 96 && v.Y >= 0 && v.Y < 64));
using var last = await renderer.RenderAsync(new PreviewRequest(EditorFixture.Sunset.Count - 1, new ProjectSettings(), 96, PreviewFraming.Source, true),
null, CancellationToken.None);
Assert.Empty(last!.Vectors);
// The flow sees the frames as the render's does, before the look (owner review 1, phase 4): the same vectors with Vivid.
var vivid = new ProjectSettings { Look = new LookSettings { Filter = "vivid" } };
using var looked = await renderer.RenderAsync(new PreviewRequest(10, vivid, 96, PreviewFraming.Source, Vectors: true), null, CancellationToken.None);
Assert.SequenceEqual(frame.Vectors, looked!.Vectors);
}
[Test]
public async Task ACancelledPreviewStopsWhileDecoding()
{
var (renderer, source) = Create();
source.DecodeGate = new TaskCompletionSource().Task;
using var cts = new CancellationTokenSource(TimeSpan.FromMilliseconds(100));
await Assert.ThrowsAsync(() => renderer.RenderAsync(new PreviewRequest(0, new ProjectSettings(), 64, PreviewFraming.Output), null, cts.Token));
}
[Test]
public async Task AnUnknownFrameHasNoPreview()
{
var (renderer, _) = Create();
Assert.Null(await renderer.RenderAsync(new PreviewRequest(-1, new ProjectSettings(), 64, PreviewFraming.Output), null, CancellationToken.None));
Assert.Null(await renderer.RenderAsync(new PreviewRequest(10_000, new ProjectSettings(), 64, PreviewFraming.Output), null, CancellationToken.None));
}
[Test]
public async Task PreparedDecodesAreReused()
{
var (renderer, source) = Create();
var project = new ProjectSettings();
project = project with { Exposure = project.Exposure with { WhiteBalanceSmoothing = project.Exposure.WhiteBalanceSmoothing.WithManual(true) } };
await renderer.PrepareAsync(project, CancellationToken.None);
var decodes = source.Decodes;
Assert.GreaterOrEqual(decodes, EditorFixture.Sunset.Count, "white balance is measured on every frame");
await renderer.PrepareAsync(project with { Exposure = project.Exposure with { ExposureOffsetEv = 0.5 } }, CancellationToken.None);
Assert.Equal(decodes, source.Decodes);
}
[Test]
public async Task SolveSettingsReuseTheMeasurementsWithoutDecoding()
{
var (renderer, source) = Create();
var project = Sunset();
await renderer.PrepareAsync(project, CancellationToken.None);
var decodes = source.Decodes;
Assert.Greater(decodes, 0, "stabilisation and colour are measured once");
foreach (var strength in new[] { 0.3, 0.6, 0.9 })
{
var changed = project with { Exposure = project.Exposure with { DeflickerStrength = project.Exposure.DeflickerStrength.WithManual(strength), TransitionLength = project.Exposure.TransitionLength.WithManual(6) } };
await renderer.PrepareAsync(changed, CancellationToken.None);
}
Assert.Equal(decodes, source.Decodes, "a strength or transition change re-solves from the measured series");
}
[Test]
public async Task WithoutAnalysisNothingIsMeasuredUntilAModuleNeedsIt()
{
var content = EditorFixture.Content(EditorFixture.Sunset, deep: false);
var source = new FakeSequenceSource(EditorFixture.Sunset, content);
var renderer = new PreviewRenderer(content, source);
await renderer.PrepareAsync(new ProjectSettings(), CancellationToken.None);
Assert.Equal(0, source.Decodes, "no module on, nothing to measure");
var project = Deflickered();
await renderer.PrepareAsync(project, CancellationToken.None);
var decodes = source.Decodes;
Assert.GreaterOrEqual(decodes, EditorFixture.Sunset.Count, "deflicker measures the luminance once");
await renderer.PrepareAsync(project with { Exposure = project.Exposure with { DeflickerStrength = project.Exposure.DeflickerStrength.WithManual(0.4) } },
CancellationToken.None);
Assert.Equal(decodes, source.Decodes);
}
[Test]
public async Task AReSolveMatchesAFreshPreparation()
{
var (warm, _) = Create();
var project = Sunset();
await warm.PrepareAsync(project, CancellationToken.None);
var changed = project with { Exposure = project.Exposure with { DeflickerStrength = project.Exposure.DeflickerStrength.WithManual(0.5), ExposureOffsetEv = 0.3 } };
var resolved = await warm.PrepareAsync(changed, CancellationToken.None);
var (cold, _) = Create();
var fresh = await cold.PrepareAsync(changed, CancellationToken.None);
Assert.Equal(fresh.ExposureEv.Length, resolved.ExposureEv.Length);
for (var i = 0; i < fresh.ExposureEv.Length; i++) Assert.Approximately(fresh.ExposureEv[i], resolved.ExposureEv[i], 1e-9, $"frame {i}");
Assert.NotNull(resolved.WhiteBalance);
for (var i = 0; i < fresh.WhiteBalance!.Length; i++) Assert.Equal(fresh.WhiteBalance[i], resolved.WhiteBalance![i]);
Assert.Equal(fresh.StabilizationZoom, resolved.StabilizationZoom);
Assert.NotNull(resolved.Stabilization);
Assert.SequenceEqual(fresh.Stabilization!, resolved.Stabilization!);
}
[Test]
public async Task ActualSizeRendersTheSourcePixels()
{
var (renderer, _) = Create();
var edge = int.Max(EditorFixture.Sunset.Options.Width, EditorFixture.Sunset.Options.Height);
using var frame = await renderer.RenderAsync(new PreviewRequest(3, new ProjectSettings(), edge, PreviewFraming.Source, Vectors: true), null,
CancellationToken.None);
Assert.Equal(EditorFixture.Sunset.Options.Width, frame!.Corrected.Width, "one picture pixel per source pixel");
Assert.Equal(EditorFixture.Sunset.Options.Height, frame.Corrected.Height);
Assert.NotEmpty(frame.Vectors);
Assert.True(frame.Vectors.All(v => v.X < frame.Corrected.Width && v.Y < frame.Corrected.Height), "vectors in picture pixels");
}
[Test]
public async Task TheWaveformIsMeasuredOnRequest()
{
var (renderer, _) = Create();
using var plain = await renderer.RenderAsync(new PreviewRequest(3, new ProjectSettings(), 64, PreviewFraming.Output), null, CancellationToken.None);
Assert.Null(plain!.Waveform);
using var frame = await renderer.RenderAsync(new PreviewRequest(3, new ProjectSettings(), 64, PreviewFraming.Output, Waveform: true), null,
CancellationToken.None);
var waveform = Assert.NotNull(frame!.Waveform);
Assert.Equal(frame.Corrected.Width, waveform.Columns);
Assert.True(waveform.Density.Any(d => d > 0));
}
[Test]
public async Task OutputFramingFollowsChosenPortraitProportions()
{
// The chosen proportions win (owner review 1, R5): the square preset's box takes a 9:16 picture.
var (renderer, _) = Create();
var project = new ProjectSettings();
project = project with
{
Export = project.Export with { Preset = project.Export.Preset.WithManual("square-1440") },
Camera = project.Camera with { Aspect = project.Camera.Aspect.WithManual("9:16") },
};
using var output = await renderer.RenderAsync(new PreviewRequest(0, project, 80, PreviewFraming.Output), null, CancellationToken.None);
Assert.Equal(45, output!.Corrected.Width, "810 × 1440 at the preview's long edge");
Assert.Equal(80, output.Corrected.Height);
using var source = await renderer.RenderAsync(new PreviewRequest(0, project, 96, PreviewFraming.Source), null, CancellationToken.None);
Assert.Equal(96, source!.Corrected.Width, "the whole frame");
Assert.Approximately(9.0 / 16 * 64 / 96, source.LockRatio, 1e-12, "the box's ratio in normalised units");
Assert.Approximately(1, source.EditCrop.Height, 1e-9, "the largest 9:16 box of the 3:2 frame");
Assert.Approximately(source.LockRatio, source.EditCrop.Width, 1e-9);
Assert.Approximately(source.EditCrop.Width, source.Crop.Width, 0.02, "the output's footprint is the box");
Assert.Approximately(source.EditCrop.X, source.Crop.X, 0.02);
}
[Test]
public async Task SourceFramingShowsTheTurnedViewFrame()
{
var (renderer, _) = Create();
var plain = new ProjectSettings();
var turned = plain with { Camera = plain.Camera with { Rotate = 90 } };
using var frame = await renderer.RenderAsync(new PreviewRequest(5, plain, 96, PreviewFraming.Source), null, CancellationToken.None);
using var view = await renderer.RenderAsync(new PreviewRequest(5, turned, 96, PreviewFraming.Source), null, CancellationToken.None);
Assert.Equal((96, 64), (frame!.Corrected.Width, frame.Corrected.Height));
Assert.Equal((64, 96), (view!.Corrected.Width, view.Corrected.Height), "a quarter turn swaps the size");
Assert.Equal((64, 96), (view.Original.Width, view.Original.Height));
Assert.Equal((64, 96), view.ViewSize);
Assert.Equal((96, 64), frame.ViewSize);
// Turned a quarter clockwise, exactly: the view's (x, y) is the frame's (y, H − 1 − x).
for (var c = 0; c < 3; c++)
{
var a = frame.Original.Plane(c);
var b = view.Original.Plane(c);
for (var y = 0; y < 96; y += 7)
{
for (var x = 0; x < 64; x += 5) Assert.Approximately(a[(63 - x) * 96 + y], b[y * 64 + x], 1e-6, $"channel {c} at ({x}, {y})");
}
}
}
[Test]
public async Task TheMaskIsTheEditableCropOfTheViewFrame()
{
var (renderer, _) = Create();
var project = new ProjectSettings();
project = project with
{
Camera = project.Camera with
{
Aspect = project.Camera.Aspect.WithManual("1:1"),
Crop = [0.1, 0.05, 0.6, 0.8],
OffsetX = 0.5,
Rotate = 270,
Straighten = 4,
},
};
using var frame = await renderer.RenderAsync(new PreviewRequest(2, project, 96, PreviewFraming.Source), null, CancellationToken.None);
var geometry = RenderGeometry.Create(project.Camera, ExportResolver.Resolve(project.Export), EditorFixture.Sunset.Options.Width, EditorFixture.Sunset.Options.Height);
Assert.Equal(geometry.EditableCrop, frame!.EditCrop, "the box the render fits, the same on every frame");
Assert.Equal(geometry.LockRatio, frame.LockRatio);
Assert.Approximately(frame.EditCrop.Width / frame.EditCrop.Height * frame.Corrected.Width / frame.Corrected.Height, 1, 0.03, "square on the picture");
Assert.Approximately(frame.EditCrop.X, frame.Crop.X, 0.02, "without stabilisation or pose, the output's footprint is the box");
Assert.Approximately(frame.EditCrop.Width, frame.Crop.Width, 0.02);
Assert.Approximately(frame.EditCrop.Height, frame.Crop.Height, 0.02);
Assert.Equal((64, 96), (frame.Corrected.Width, frame.Corrected.Height));
}
[Test]
public async Task ALookChangesOnlyTheCorrectedPicture()
{
var (renderer, _) = Create();
var plain = Deflickered() with { Exposure = new ProjectSettings().Exposure with { HighlightKnee = null } };
var look = new LookSettings { Filter = "sepia", Strength = 0.6 };
foreach (var framing in new[] { PreviewFraming.Output, PreviewFraming.Source })
{
using var before = await renderer.RenderAsync(new PreviewRequest(4, plain, 64, framing), null, CancellationToken.None);
using var after = await renderer.RenderAsync(new PreviewRequest(4, plain with { Look = look }, 64, framing), null, CancellationToken.None);
Assert.True(before!.Original.Data.SequenceEqual(after!.Original.Data), $"{framing}: the original side has no look");
Assert.False(before.Corrected.Data.SequenceEqual(after.Corrected.Data), $"{framing}: the corrected side has it");
FrameFinish.Apply(before.Corrected, look, null);
Assert.True(before.Corrected.Data.SequenceEqual(after.Corrected.Data), $"{framing}: the render's finish");
}
}
[Test]
public async Task OutputFramingMatchesTheRenderTurnedStraightenedAndWithALook()
{
var project = new ProjectSettings();
project = project with
{
Exposure = project.Exposure with { HighlightKnee = null },
Export = project.Export with { Preset = project.Export.Preset.WithManual("png-sequence") },
Camera = project.Camera with { Rotate = 90, Straighten = 3 },
Look = new LookSettings { Filter = "sepia", Strength = 0.6 },
};
var (renderer, _) = Create();
using var preview = await renderer.RenderAsync(new PreviewRequest(0, project, 96, PreviewFraming.Output), null, CancellationToken.None);
Assert.Equal((64, 96), (preview!.Corrected.Width, preview.Corrected.Height), "the turned frame at the source's size");
var capture = new CapturingEncoder();
var pixels = new SyntheticFrameSource(EditorFixture.Sunset);
var folder = Path.Combine(Path.GetTempPath(), "chronos-preview-" + Guid.NewGuid().ToString("N"));
try
{
var input = new RenderInput { Source = pixels, Frames = pixels.Metadata(), Project = project };
await new RenderPipeline([capture, new ManagedEncoderProvider()]).RenderAsync(input, new RenderTarget(ExportResolver.Resolve(project.Export), folder));
}
finally
{
if (Directory.Exists(folder)) Directory.Delete(folder, recursive: true);
}
var rendered = capture.Frames[0];
Assert.Equal((preview.Corrected.Width, preview.Corrected.Height), (rendered.Width, rendered.Height));
Assert.Greater(Psnr(rendered, preview.Corrected), 40, "the preview is the render's frame");
foreach (var frame in capture.Frames) frame.Dispose();
}
/// A sunset project as the advisor leaves it: deflicker, holy grail, white balance and stabilisation on.
private static ProjectSettings Sunset()
{
var p = new ProjectSettings();
return p with
{
Exposure = p.Exposure with
{
Deflicker = p.Exposure.Deflicker.WithManual(true),
HolyGrail = p.Exposure.HolyGrail.WithManual(true),
WhiteBalanceSmoothing = p.Exposure.WhiteBalanceSmoothing.WithManual(true),
},
Motion = p.Motion with { Stabilize = p.Motion.Stabilize.WithManual(true) },
};
}
private static double LogMean(ImageBuffer image)
{
double sum = 0;
var n = image.PlaneLength;
for (var i = 0; i < n; i++) sum += 0.2126 * image.Plane(0)[i] + 0.7152 * image.Plane(1)[i] + 0.0722 * image.Plane(2)[i];
return Math.Log2(sum / n);
}
/// Peak signal-to-noise ratio of the 8-bit sRGB encodings.
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));
}
/// A PNG-sequence encoder that keeps a copy of every frame instead of writing it.
private sealed class CapturingEncoder : IVideoEncoderProvider
{
private static readonly EncoderCapability Capability = new(VideoCodec.PngSequence, "Capture", false, 8192, 8192, true);
public string Name => "capture";
public IReadOnlyList Capabilities { get; } = [Capability];
public List Frames { get; } = [];
public bool CanEncode(EncodeSettings settings) => settings.Codec == VideoCodec.PngSequence;
public IVideoEncoderSession Open(EncodeSettings settings) => new Session(this);
private sealed class Session(CapturingEncoder owner) : IVideoEncoderSession
{
public string EncoderName => Capability.Name;
public bool IsHardware => false;
public void WriteFrame(ImageBuffer frame, long duration90k) => owner.Frames.Add(frame.Clone());
public void Complete()
{
}
public void Dispose()
{
}
}
}
/// Standard deviation of the frame-to-frame differences (flicker, not the slow ramp).
private static double Spread(List values)
{
var diffs = values.Zip(values.Skip(1), (a, b) => b - a).ToList();
var mean = diffs.Average();
return Math.Sqrt(diffs.Sum(d => (d - mean) * (d - mean)) / diffs.Count);
}
}