Files
Chronos/tests/Titano.Core.Tests/Render/RenderProcessingTests.cs
T
Claude 3780105f36 feat(render): preparation pass, render geometry and pipeline
RenderPreparation measures (or reuses cached) luminance, colour, region
and camera-path series and turns them into per-frame corrections.
RenderPipeline renders planned frames through exposure, one geometry
resample, flow synthesis, motion blur, stacking and the knee, overlapping
encoding with processing; cancellation and failures leave no output and
no unobserved exceptions. Batch renders share one preparation.

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

352 lines
19 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.Analysis;
using Titano.Core.Diagnostics.Synthetic;
using Titano.Core.Imaging;
using Titano.Core.Imaging.Geometry;
using Titano.Core.Motion;
using Titano.Core.Project;
using Titano.Core.Render;
using Titano.Core.Render.Diagnostics;
using Titano.Testing;
using static Titano.Core.Tests.Render.RenderFixture;
namespace Titano.Core.Tests.Render;
public sealed class RenderProcessingTests
{
private readonly TestContext _context;
public RenderProcessingTests(TestContext context) => _context = context;
private static ProjectSettings With(ExposureSettings? exposure = null, MotionSettings? motion = null, CameraSettings? camera = null, StackSettings? stack = null,
TimingSettings? timing = null) => Plain with
{
Exposure = exposure ?? Plain.Exposure,
Motion = motion ?? new MotionSettings(),
Camera = camera ?? new CameraSettings(),
Stack = stack ?? new StackSettings(),
Timing = timing ?? new TimingSettings(),
};
private static Auto<T> On<T>(T value) => new Auto<T>().WithManual(value);
private static double[] LogLuminance(IEnumerable<ImageBuffer> frames) => [.. frames.Select(f => FrameStatistics.MeasureLogLuminance(f))];
private static double Rms(IEnumerable<double> values)
{
var v = values.ToArray();
var mean = v.Average();
return Math.Sqrt(v.Sum(x => (x - mean) * (x - mean)) / v.Length);
}
/// <summary>The §20.4 deflicker gate measured on rendered frames (same recipe as the analysis gate).</summary>
[Test]
public async Task DeflickerRemovesFlickerAndKeepsTheRamp()
{
var source = Source(200, configure: o => o with { Seed = 3, StartEv = -2, EndEv = 1, FlickerEv = 0.1 });
var project = With(new ExposureSettings { Deflicker = On(true), DeflickerWindow = On(21), HighlightKnee = null });
var (encode, _) = await CaptureAsync(Input(source, project), cancellationToken: _context.Cancellation);
var truth = source.Sequence.SceneEv;
var measured = Analysis.AnalysisFixtures.MeasureSeries(source.Sequence);
var output = LogLuminance(encode.Frames);
var before = Analysis.AnalysisFixtures.FrameToFrameResidualRms(measured, truth);
var after = Analysis.AnalysisFixtures.FrameToFrameResidualRms(output, truth);
var rampError = Analysis.AnalysisFixtures.MaxDeviation(output, truth);
_context.Log($"flicker {before:F4} → {after:F4} EV ({1 - after / before:P0}); max ramp deviation {rampError:F4} EV");
Assert.GreaterOrEqual(1 - after / before, 0.6);
Assert.LessOrEqual(rampError, 0.05);
}
[Test]
public async Task HolyGrailTurnsExposureStepsIntoRamps()
{
var source = Source(80, configure: o => o with { ExposureSteps = [new SyntheticExposureStep(40, 1.0)], StartEv = 0, EndEv = 0 });
var project = With(new ExposureSettings { HolyGrail = On(true), Deflicker = On(true), HighlightKnee = null });
var (encode, _) = await CaptureAsync(Input(source, project), cancellationToken: _context.Cancellation);
var output = LogLuminance(encode.Frames);
var steps = output.Zip(output.Skip(1), (a, b) => Math.Abs(b - a)).ToArray();
_context.Log($"largest output step {steps.Max():F3} EV");
Assert.LessOrEqual(steps.Max(), 0.3 + 0.02);
Assert.Approximately(-1.0, output[^1] - output[0], 0.1, "the step is kept, only spread out");
}
[Test]
public async Task WhiteBalanceSmoothingRemovesColourJitter()
{
var source = Source(48, configure: o => o with { WhiteBalanceJitter = 0.1 });
var (encode, _) = await CaptureAsync(Input(source, With(new ExposureSettings { WhiteBalanceSmoothing = On(true), HighlightKnee = null })), cancellationToken: _context.Cancellation);
double Ratio(ImageBuffer f)
{
var stats = FrameStatistics.Measure(f);
return Math.Log2(stats.MeanRed / stats.MeanGreen);
}
var before = Enumerable.Range(0, 48).Select(i =>
{
using var f = source.Sequence.Render(i);
return Ratio(f);
}).ToArray();
var after = encode.Frames.Select(Ratio).ToArray();
double Jitter(double[] r) => Rms(r.Zip(r.Skip(1), (a, b) => b - a));
_context.Log($"R/G jitter {Jitter(before):F4} → {Jitter(after):F4}");
Assert.Less(Jitter(after), 0.3 * Jitter(before));
}
[Test]
public async Task RegionDeflickerCorrectsSkyAndLandSeparately()
{
var source = Source(48, configure: o => o with { SkyFlickerEv = 0.3, LandFlickerEv = 0.3, StartEv = 0, EndEv = 0 });
var landMask = new float[96 * 64];
for (var y = 0; y < 64; y++)
{
for (var x = 0; x < 96; x++) landMask[y * 96 + x] = source.Sequence.IsSky(x, y) ? 0 : 1;
}
double LandResidual(List<ImageBuffer> frames) => Rms(frames.Select(f => FrameStatistics.MeasureLogLuminance(f, landMask)));
var global = await CaptureAsync(Input(source, With(new ExposureSettings { Deflicker = On(true), HighlightKnee = null })), cancellationToken: _context.Cancellation);
var regions = await CaptureAsync(Input(source, With(new ExposureSettings { Deflicker = On(true), Regions = On(RegionMode.Horizon), HighlightKnee = null })),
cancellationToken: _context.Cancellation);
var g = LandResidual(global.Encode.Frames);
var r = LandResidual(regions.Encode.Frames);
_context.Log($"land residual global {g:F3} EV, regions {r:F3} EV");
Assert.Less(r, 0.5 * g);
var prepared = await RenderPreparation.PrepareAsync(Input(source, With(new ExposureSettings { Deflicker = On(true), Regions = On(RegionMode.Horizon) })),
cancellationToken: _context.Cancellation);
Assert.Equal(RegionMethod.Horizon, prepared.Report.RegionMethod);
Assert.NotNull(prepared.SkyEv);
}
[Test]
public async Task ExportAspectCropsTheCentreAndCameraCropMovesIt()
{
var source = Source(4);
using var reference = source.Sequence.Render(2);
var (square, _) = await CaptureAsync(Input(source), Preset("png-sequence", 64, 64), cancellationToken: _context.Cancellation);
using (var expected = GeometryStage.Apply(reference, new GeometrySettings { Crop = new RectD(16, 0, 64, 64), OutputWidth = 64, OutputHeight = 64 }))
Assert.Greater(Psnr(expected, square.Frames[2]), 60);
var quarter = With(camera: new CameraSettings { Crop = [0.5, 0.5, 0.5, 0.5] });
var (corner, result) = await CaptureAsync(Input(source, quarter), Preset("png-sequence", 0, 0), cancellationToken: _context.Cancellation);
Assert.Equal((48, 32), (result.Width, result.Height));
using (var expected = GeometryStage.Apply(reference, new GeometrySettings { Crop = new RectD(48, 32, 48, 32) }))
Assert.Greater(Psnr(expected, corner.Frames[2]), 60);
}
[Test]
public async Task VirtualCameraZoomsAndTheDecodeSizeFollowsTheNeed()
{
var plain = Source(4, width: 480, height: 320);
await CaptureAsync(Input(plain), Preset("png-sequence", 96, 64), cancellationToken: _context.Cancellation);
Assert.Equal(96, plain.LargestRenderRequest);
var zoomed = Source(4, width: 480, height: 320);
var camera = With(camera: new CameraSettings { Keyframes = [new CameraKey(0, 0.5, 0.5, 2, 0), new CameraKey(3, 0.5, 0.5, 2, 0)] });
var (encode, _) = await CaptureAsync(Input(zoomed, camera), Preset("png-sequence", 96, 64), cancellationToken: _context.Cancellation);
Assert.Equal(192, zoomed.LargestRenderRequest);
using var full = zoomed.Sequence.Render(1);
using var expected = GeometryStage.Apply(full, new GeometrySettings { Crop = new RectD(120, 80, 240, 160), OutputWidth = 96, OutputHeight = 64 });
var psnr = Psnr(expected, encode.Frames[1]);
_context.Log($"zoom ×2 vs direct crop: {psnr:F1} dB");
Assert.Greater(psnr, 28);
}
[Test]
public async Task ExposureOffsetAndKneeAreApplied()
{
var source = Source(3, configure: o => o with { StartEv = 2, EndEv = 2 });
var (raw, _) = await CaptureAsync(Input(source), cancellationToken: _context.Cancellation);
var (brighter, _) = await CaptureAsync(Input(source, With(new ExposureSettings { ExposureOffsetEv = 1, HighlightKnee = null })), cancellationToken: _context.Cancellation);
for (var i = 0; i < raw.Frames[0].Data.Length; i += 13) Assert.Approximately(2 * raw.Frames[0].Data[i], brighter.Frames[0].Data[i], 1e-5);
Assert.Greater(raw.Frames[0].Data.ToArray().Max(), 1f);
var (kneed, _) = await CaptureAsync(Input(source, With(new ExposureSettings { HighlightKnee = 0.8 })), cancellationToken: _context.Cancellation);
Assert.Less(kneed.Frames[0].Data.ToArray().Max(), 1f);
for (var i = 0; i < raw.Frames[0].Data.Length; i += 13)
{
if (raw.Frames[0].Data[i] < 0.5f) Assert.Approximately(raw.Frames[0].Data[i], kneed.Frames[0].Data[i], 1e-6);
}
}
[Test]
public async Task InterpolatedFramesFollowTheMotion()
{
Func<int, (double, double)> pan = i => (2.0 * i, 0);
var source = Source(6, motion: pan, configure: o => o with { StartEv = 0, EndEv = 0 });
var timing = new TimingSettings { Mode = On(TimingMode.Interpolated), FrameRate = new Auto<string>().WithManual("15") };
var export = Preset("png-sequence") with { FrameRate = new Titano.Core.Video.Rational(30, 1) };
var (encode, result) = await CaptureAsync(Input(source, With(timing: timing)), export, cancellationToken: _context.Cancellation);
Assert.Equal(11, result.FrameCount);
Assert.Equal(12, encode.Durations.Sum() / 3000);
var truth = Source(6, motion: i => (2.0 * i + 1, 0), configure: o => o with { StartEv = 0, EndEv = 0 });
using var shifted = await truth.GetFrameAsync(2, 0, DecodePurpose.Render, _context.Cancellation);
double Interior(ImageBuffer a, ImageBuffer b)
{
using var ca = GeometryStage.Apply(a, new GeometrySettings { Crop = new RectD(12, 4, 72, 56) });
using var cb = GeometryStage.Apply(b, new GeometrySettings { Crop = new RectD(12, 4, 72, 56) });
return Psnr(ca, cb);
}
var synthesised = Interior(shifted, encode.Frames[5]);
var nearest = Interior(shifted, encode.Frames[4]);
_context.Log($"synthesised frame {synthesised:F1} dB vs nearest whole frame {nearest:F1} dB");
Assert.Greater(synthesised, 30);
Assert.Greater(synthesised, nearest + 5);
}
[Test]
public async Task MotionBlurSmearsAlongTheMotionOnly()
{
var blur = new MotionSettings { MotionBlur = On(true), TargetShutterAngle = On(360.0) };
var moving = Source(6, motion: i => (3.0 * i, 0), configure: o => o with { StartEv = 0, EndEv = 0 });
var sharp = await CaptureAsync(Input(moving), cancellationToken: _context.Cancellation);
var blurred = await CaptureAsync(Input(moving, With(motion: blur)), cancellationToken: _context.Cancellation);
double Gradient(ImageBuffer f, int dx, int dy)
{
double sum = 0;
var g = f.Plane(1);
for (var y = 8; y < f.Height - 8; y++)
{
for (var x = 8; x < f.Width - 8; x++) sum += Math.Abs(g[y * f.Width + x] - g[(y + dy) * f.Width + x + dx]);
}
return sum;
}
var horizontal = Gradient(blurred.Encode.Frames[3], 1, 0) / Gradient(sharp.Encode.Frames[3], 1, 0);
var vertical = Gradient(blurred.Encode.Frames[3], 0, 1) / Gradient(sharp.Encode.Frames[3], 0, 1);
_context.Log($"gradient ratios: horizontal {horizontal:F3}, vertical {vertical:F3}");
Assert.Less(horizontal, 0.75);
Assert.Greater(vertical, horizontal + 0.1);
var still = Source(6, configure: o => o with { StartEv = 0, EndEv = 0 });
var a = await CaptureAsync(Input(still), cancellationToken: _context.Cancellation);
var b = await CaptureAsync(Input(still, With(motion: blur)), cancellationToken: _context.Cancellation);
Assert.Greater(Psnr(a.Encode.Frames[3], b.Encode.Frames[3]), 40);
}
[Test]
public async Task StabilisationRemovesShake()
{
var random = new Random(11);
var shake = Enumerable.Range(0, 24).Select(_ => (X: random.NextDouble() * 6 - 3, Y: random.NextDouble() * 6 - 3)).ToArray();
var source = new PatternFrameSource(320, 192, 24, i => shake[i]);
var project = With(motion: new MotionSettings { Stabilize = On(true), StabilizeSmoothing = On(12.0) });
var input = new RenderInput { Source = source, Frames = source.Metadata(), Project = project };
static double Steps(IReadOnlyList<(double X, double Y)> p) =>
Math.Sqrt(Enumerable.Range(1, p.Count - 1).Average(k => Math.Pow(p[k].X - p[k - 1].X, 2) + Math.Pow(p[k].Y - p[k - 1].Y, 2)));
var prepared = await RenderPreparation.PrepareAsync(input, cancellationToken: _context.Cancellation);
var residual = Enumerable.Range(0, 24).Select(k =>
{
var (x, y) = prepared.Stabilization![k].Apply(159.5, 95.5);
return (shake[k].X + x - 159.5, shake[k].Y + y - 95.5);
}).ToArray();
_context.Log($"camera path steps {Steps(shake):F2} px → {Steps(residual):F3} px after correction; zoom {prepared.StabilizationZoom:F3}");
Assert.Less(Steps(residual), 0.1 * Steps(shake));
Assert.InRange(prepared.StabilizationZoom, 1.01, 1.2);
var (encode, result) = await CaptureAsync(input, Preset("png-sequence", 0, 0), cancellationToken: _context.Cancellation);
Assert.Equal((320, 192), (result.Width, result.Height));
using var stabilizer = new Stabilizer(320, 192);
var output = encode.Frames.Select(f => stabilizer.AddFrame(f).Transform).Skip(1).ToArray();
var after = Math.Sqrt(output.Average(m => m.Tx * m.Tx + m.Ty * m.Ty));
_context.Log($"rendered frame-to-frame shift {after:F3} px");
Assert.Less(after, 0.1 * Steps(shake) * prepared.StabilizationZoom);
}
[Test]
public async Task MedianStackRemovesTransientsAndStarTrailsNeverDarken()
{
var source = Source(9, configure: o => o with { StartEv = 0, EndEv = 0 });
var input = Input(source, With(stack: new StackSettings { Mode = StackSetting.Median, Length = 5 })) with { Source = new TransientSource(source, 4) };
var (median, _) = await CaptureAsync(input, cancellationToken: _context.Cancellation);
Assert.Equal(9, median.Frames.Count);
using var clean = source.Sequence.Render(4);
Assert.Greater(Psnr(clean, median.Frames[4]), 45);
var stars = Source(12, motion: i => (1.0 * i, 0), configure: o => o with { StarField = true, StartEv = 0, EndEv = 0, StarCount = 60 });
var (trails, _) = await CaptureAsync(Input(stars, With(stack: new StackSettings { Mode = StackSetting.StarTrails, Length = 0 })), cancellationToken: _context.Cancellation);
var luminance = LogLuminance(trails.Frames);
Assert.Monotonic(luminance);
for (var k = 1; k < trails.Frames.Count; k++)
{
var previous = trails.Frames[k - 1].Data;
var current = trails.Frames[k].Data;
for (var i = 0; i < current.Length; i++) Assert.True(current[i] >= previous[i] - 1e-6f, $"pixel {i} darkened at frame {k}");
}
}
[Test]
public async Task PrecomputedAnalysisSkipsTheMeasuringPass()
{
var source = Source(30, flickerEv: 0.2);
var project = With(new ExposureSettings { Deflicker = On(true), HighlightKnee = null });
var measured = await RenderPreparation.PrepareAsync(Input(source, project), cancellationToken: _context.Cancellation);
Assert.Equal(30, measured.Report.MeasuredFrames);
var cached = new PrecomputedAnalysis { LogLuminance = measured.MeasuredLogLuminance };
var decodes = source.Decodes;
var reused = await RenderPreparation.PrepareAsync(Input(source, project) with { Analysis = cached }, cancellationToken: _context.Cancellation);
Assert.Equal(0, reused.Report.MeasuredFrames);
Assert.Equal(decodes, source.Decodes);
Assert.SequenceEqual(measured.ExposureEv, reused.ExposureEv);
var wrongLength = await RenderPreparation.PrepareAsync(Input(source, project) with { Analysis = new PrecomputedAnalysis { LogLuminance = new double[3] } },
cancellationToken: _context.Cancellation);
Assert.Equal(30, wrongLength.Report.MeasuredFrames);
Assert.SequenceEqual(measured.ExposureEv, wrongLength.ExposureEv);
var nothing = await RenderPreparation.PrepareAsync(Input(source), cancellationToken: _context.Cancellation);
Assert.Equal(0, nothing.Report.MeasuredFrames);
Assert.True(nothing.ExposureEv.All(e => e == 0));
}
[Test]
public async Task HeldFramesKeepTheirDurationOnConstantRateOutputs()
{
var source = Source(10, configure: o => o with { TimeShifts = [new SyntheticTimeShift(5, 20)] });
var project = With(timing: new TimingSettings { Mode = On(TimingMode.Adaptive) });
var export = Preset("mjpeg-avi");
var dir = _context.CreateTempDirectory();
var result = await Pipeline().RenderAsync(Input(source, project), Target(dir, export), cancellationToken: _context.Cancellation);
var avi = AviReader.Read(File.ReadAllBytes(result.OutputPath));
Assert.Equal(10, result.FrameCount);
Assert.Equal(12, avi.Frames.Count); // frame 4 lasts three periods (30 s ÷ 10 s)
Assert.Approximately(12 / 25.0, result.DurationSeconds, 1e-9);
}
[Test]
public void SetupReportsGeometryEncoderPlanAndEstimates()
{
var source = Source(48, width: 480, height: 320);
var input = Input(source, With(new ExposureSettings { Deflicker = On(true) }, motion: new MotionSettings { MotionBlur = On(true) }));
var setup = Pipeline().Setup(input, Target("/exports", Preset("gif-preview", 480, 270)));
Assert.True(setup.Encoder.IsReady);
Assert.Equal((480, 270), (setup.Geometry.OutputWidth, setup.Geometry.OutputHeight));
Assert.Equal(48, setup.Plan.Frames.Count);
Assert.True(setup.NeedsPreparation);
var workload = setup.Workload();
Assert.Equal(48, workload.FlowFrames);
Assert.Equal(48, workload.AnalysisFrames);
Assert.Equal(480L * 320, workload.WorkingPixels);
var estimate = setup.Estimate();
Assert.Greater(estimate.Bytes, 0);
Assert.Greater(estimate.Time.TotalSeconds, 0);
}
/// <summary>Adds a bright square to one frame (a passing aircraft light).</summary>
private sealed class TransientSource(IRenderFrameSource inner, int frame) : IRenderFrameSource
{
public int FrameCount => inner.FrameCount;
public int Width => inner.Width;
public int Height => inner.Height;
public async ValueTask<ImageBuffer> GetFrameAsync(int index, int maxLongEdge, DecodePurpose purpose, CancellationToken cancellationToken)
{
var image = await inner.GetFrameAsync(index, maxLongEdge, purpose, cancellationToken);
if (index != frame) return image;
for (var c = 0; c < 3; c++)
{
for (var y = 20; y < 30; y++)
{
for (var x = 40; x < 50; x++) image[c, x, y] = 1;
}
}
return image;
}
}
}