Files
Chronos/tests/Titano.Core.Tests/Render/RenderPlanTests.cs
T
Claude 33d7284c25 feat(render): render plan for constant, adaptive, interpolated and remapped timing
Output frames on the export frame grid with fractional source positions,
exact 90 kHz durations, held/interpolated flags, motion cuts and plan
statistics; FitToSeconds rescales the output axis.

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

194 lines
8.9 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.Project;
using Titano.Core.Render;
using Titano.Core.Video;
using Titano.Testing;
namespace Titano.Core.Tests.Render;
public sealed class RenderPlanTests
{
private static readonly DateTime Start = new(2024, 6, 21, 19, 0, 0);
private static DateTime?[] Regular(int count, double interval = 10) =>
Enumerable.Range(0, count).Select(i => (DateTime?)Start.AddSeconds(i * interval)).ToArray();
private static ProjectSettings Timing(TimingMode mode, string? playback = null, double? fit = null, IReadOnlyList<CurvePoint>? curve = null, int[]? excluded = null) => new()
{
Timing = new TimingSettings
{
Mode = new Auto<TimingMode>().WithManual(mode),
FrameRate = playback is null ? new Auto<string>() : new Auto<string>().WithManual(playback),
FitToSeconds = fit,
RemapCurve = curve ?? [new(0, 0), new(1, 1)],
},
ExcludedFrames = excluded ?? [],
};
private static void AssertExactDurations(RenderPlan plan)
{
Assert.Equal(plan.Duration90k, plan.Frames.Sum(f => f.Duration90k));
Assert.Equal(RenderPlan.TicksAt(plan.FrameRate, plan.SlotCount), plan.Duration90k);
long t = 0;
var slot = 0;
foreach (var f in plan.Frames)
{
Assert.Equal(t, f.Start90k);
Assert.Equal(slot, f.StartSlot);
Assert.Greater(f.Duration90k, 0);
t += f.Duration90k;
slot += f.Slots;
}
Assert.Equal(plan.SlotCount, slot);
Assert.Monotonic(plan.Frames.Select(f => f.SourcePosition));
}
[Test]
public void ConstantShowsEveryFrameOnceAtTheOutputRate()
{
var plan = RenderPlan.Build(Regular(100), Timing(TimingMode.Constant), new Rational(24, 1));
Assert.Equal(100, plan.Frames.Count);
Assert.SequenceEqual(Enumerable.Range(0, 100).Select(i => (double)i), plan.Frames.Select(f => f.SourcePosition));
Assert.True(plan.Frames.All(f => f.Duration90k == 3750 && f.Flags == PlannedFrameFlags.None && f.Speed == 1));
Assert.Equal(375_000L, plan.Duration90k);
AssertExactDurations(plan);
}
[Test]
public void ExcludedFramesAreSkipped()
{
var plan = RenderPlan.Build(Regular(20), Timing(TimingMode.Constant, excluded: [3, 7, 99]), new Rational(25, 1));
Assert.Equal(18, plan.Frames.Count);
Assert.DoesNotContain(3, plan.Frames.Select(f => f.From));
Assert.DoesNotContain(7, plan.Frames.Select(f => f.To));
Assert.Equal(2, plan.Statistics.ExcludedFrames);
Assert.Equal(2.0, plan.Frames.Single(f => f.From == 2).Speed);
AssertExactDurations(plan);
}
[Test]
public void NtscDurationsAccumulateWithoutDrift()
{
var plan = RenderPlan.Build(Regular(1001), Timing(TimingMode.Constant), Rational.Fps2997);
Assert.Equal(1001 * 3003L, plan.Duration90k);
Assert.True(plan.Frames.All(f => f.Duration90k == 3003));
var fps23 = RenderPlan.Build(Regular(7), Timing(TimingMode.Constant), Rational.Fps23976);
Assert.SequenceEqual(new long[] { 3754, 3754, 3753, 3754, 3754, 3754, 3753 }, fps23.Frames.Select(f => f.Duration90k));
Assert.Equal(26_276L, fps23.Duration90k);
AssertExactDurations(fps23);
}
[Test]
public void SlowerPlaybackHoldsFramesInConstantAndSynthesisesInInterpolated()
{
var held = RenderPlan.Build(Regular(10), Timing(TimingMode.Constant, playback: "12"), new Rational(24, 1));
Assert.Equal(10, held.Frames.Count);
Assert.True(held.Frames.All(f => f.Slots == 2 && f.IsHeld && f.Duration90k == 7500));
AssertExactDurations(held);
var smooth = RenderPlan.Build(Regular(10), Timing(TimingMode.Interpolated, playback: "12"), new Rational(24, 1));
Assert.Equal(19, smooth.Frames.Count);
Assert.Equal(9, smooth.Statistics.InterpolatedFrames);
Assert.Equal(0.5, smooth.Frames[1].SourcePosition);
Assert.Equal(0.5, smooth.Frames[1].Fraction);
Assert.Equal(9.0, smooth.Frames[^1].SourcePosition);
Assert.True(smooth.Frames[^1].IsHeld, "the last frame has no successor to blend towards");
AssertExactDurations(smooth);
}
[Test]
public void FitToSecondsScalesThePlan()
{
var shorter = RenderPlan.Build(Regular(100), Timing(TimingMode.Constant, fit: 2), new Rational(25, 1));
Assert.Equal(50, shorter.SlotCount);
Assert.Equal(180_000L, shorter.Duration90k);
Assert.True(shorter.Frames.All(f => f.Fraction == 0 && f.SourcePosition % 2 == 0));
Assert.Equal(50, shorter.Statistics.SkippedFrames);
Assert.Approximately(2.0, shorter.Statistics.MaximumSpeed, 1e-9);
var longer = RenderPlan.Build(Regular(100), Timing(TimingMode.Interpolated, fit: 7.96), new Rational(25, 1));
Assert.Equal(199, longer.SlotCount);
Assert.Equal(716_400L, longer.Duration90k);
Assert.Greater(longer.Statistics.InterpolatedFrames, 90);
AssertExactDurations(longer);
}
[Test]
public void AdaptiveFollowsCaptureTimeAndShortensPauses()
{
var times = Regular(40);
for (var i = 11; i < 40; i++) times[i] = times[i]!.Value.AddSeconds(10); // 2× interval into frame 11
for (var i = 21; i < 40; i++) times[i] = times[i]!.Value.AddSeconds(60); // 7× gap into frame 21
for (var i = 31; i < 40; i++) times[i] = times[i]!.Value.AddHours(1); // deliberate pause into frame 31
var plan = RenderPlan.Build(times, Timing(TimingMode.Adaptive), new Rational(24, 1));
Assert.Equal(40, plan.Frames.Count);
Assert.Equal(2, plan.Frames[10].Slots);
Assert.Equal(4, plan.Frames[20].Slots); // 3 + log2(7/3) = 4.22 periods, rounded on the grid
Assert.Equal(5, plan.Frames[30].Slots); // 0.2 s × 24 fps = 4.8 periods
Assert.True(plan.Frames.Where((_, i) => i is not (10 or 20 or 30)).All(f => f.Slots == 1));
Assert.False(plan.IsCutBefore(11));
Assert.True(plan.IsCutBefore(21));
Assert.True(plan.IsCutBefore(31));
Assert.Equal(-1, plan.NextContinuous(30));
Assert.Equal(29, plan.PreviousContinuous(30));
Assert.Equal(2, plan.Statistics.Cuts);
Assert.Approximately(10.0, plan.Statistics.MedianIntervalSeconds!.Value, 1e-9);
AssertExactDurations(plan);
}
[Test]
public void InterpolatedFillsMissingAndExcludedFrames()
{
var times = Regular(40);
for (var i = 20; i < 40; i++) times[i] = times[i]!.Value.AddSeconds(10); // one capture missing before frame 20
for (var i = 35; i < 40; i++) times[i] = times[i]!.Value.AddSeconds(600); // too long to synthesise
var plan = RenderPlan.Build(times, Timing(TimingMode.Interpolated, excluded: [30]), new Rational(30, 1));
var gap = plan.Frames.Single(f => f.From == 19 && f.IsInterpolated);
Assert.Equal(19.5, gap.SourcePosition);
Assert.Equal(20, gap.To);
var excluded = plan.Frames.Single(f => f.From == 29 && f.IsInterpolated);
Assert.Equal(30.0, excluded.SourcePosition);
Assert.Equal(31, excluded.To);
Assert.Equal(0.5, excluded.Fraction);
Assert.True(plan.IsCutBefore(35));
Assert.False(plan.Frames.Any(f => f.From == 34 && f.IsInterpolated));
Assert.Equal(41, plan.Frames.Count);
AssertExactDurations(plan);
}
[Test]
public void RemapIsMonotoneEvenForBadCurves()
{
var ease = RenderPlan.Build(Regular(120), Timing(TimingMode.Remapped, curve: [new(0, 0), new(0.5, 0.2), new(1, 1)]), new Rational(24, 1));
AssertExactDurations(ease);
Assert.Equal(120, ease.SlotCount);
Assert.Equal(0.0, ease.Frames[0].SourcePosition);
Assert.Greater(ease.Frames[^1].SourcePosition, 115);
Assert.Less(ease.Frames[^1].Speed * ease.Frames[^1].Slots, 3.5);
Assert.Less(ease.Frames[10].Speed, 0.5);
Assert.Empty(ease.Warnings);
var bad = RenderPlan.Build(Regular(60), Timing(TimingMode.Remapped, curve: [new(1, 1), new(0, 0.1), new(0.4, 0.8), new(0.6, 0.3)]), new Rational(24, 1));
Assert.Contains("Render.Plan.RemapNotMonotonic", bad.Warnings);
AssertExactDurations(bad);
Assert.Approximately(0.1 * 60, bad.Frames[0].SourcePosition, 1e-9);
}
[Test]
public void EverythingExcludedGivesAnEmptyPlan()
{
var plan = RenderPlan.Build(Regular(3), Timing(TimingMode.Constant, excluded: [0, 1, 2]), new Rational(30, 1));
Assert.Empty(plan.Frames);
Assert.Equal(0L, plan.Duration90k);
Assert.Contains("Render.Plan.NoFrames", plan.Warnings);
}
[Test]
public void AdaptiveWithoutCaptureTimesFallsBackToConstantPacing()
{
var plan = RenderPlan.Build(new DateTime?[12], Timing(TimingMode.Adaptive), new Rational(30, 1));
Assert.Equal(12, plan.Frames.Count);
Assert.Contains("Render.Plan.NoCaptureTimes", plan.Warnings);
AssertExactDurations(plan);
}
}