diff --git a/src/Titano.Core/Export/FrameRateText.cs b/src/Titano.Core/Export/FrameRateText.cs new file mode 100644 index 00000000..c4a6013c --- /dev/null +++ b/src/Titano.Core/Export/FrameRateText.cs @@ -0,0 +1,36 @@ +using System.Globalization; +using Titano.Core.Video; + +namespace Titano.Core.Export; + +/// Frame rates as stored in project files: "num/den" ("24000/1001"), a decimal ("29.97") or an integer ("30"). +public static class FrameRateText +{ + /// Parses a frame rate; decimals near the NTSC rates map to their exact fractions. + public static bool TryParse(string? text, out Rational rate) + { + rate = default; + if (string.IsNullOrWhiteSpace(text)) return false; + var s = text.Trim(); + var slash = s.IndexOf('/', StringComparison.Ordinal); + if (slash >= 0) + { + if (!int.TryParse(s.AsSpan(0, slash), NumberStyles.None, CultureInfo.InvariantCulture, out var num) + || !int.TryParse(s.AsSpan(slash + 1), NumberStyles.None, CultureInfo.InvariantCulture, out var den) + || num <= 0 || den <= 0) return false; + rate = new Rational(num, den); + return true; + } + if (!double.TryParse(s, NumberStyles.Float, CultureInfo.InvariantCulture, out var fps) || !(fps > 0 && fps <= 1000)) return false; + rate = Rational.FromFps(fps); + return true; + } + + /// Canonical text: "num/den" for fractional rates, the integer otherwise. + public static string Format(Rational rate) => rate.Denominator == 1 + ? rate.Numerator.ToString(CultureInfo.InvariantCulture) + : string.Create(CultureInfo.InvariantCulture, $"{rate.Numerator}/{rate.Denominator}"); + + /// Exact rational equality (24000/1001 equals 48000/2002). + public static bool AreEqual(Rational a, Rational b) => (long)a.Numerator * b.Denominator == (long)b.Numerator * a.Denominator; +} diff --git a/src/Titano.Core/Render/RenderPlan.cs b/src/Titano.Core/Render/RenderPlan.cs new file mode 100644 index 00000000..b8e8cffb --- /dev/null +++ b/src/Titano.Core/Render/RenderPlan.cs @@ -0,0 +1,442 @@ +using Titano.Core.Export; +using Titano.Core.Mathematics; +using Titano.Core.Project; +using Titano.Core.Video; + +namespace Titano.Core.Render; + +[Flags] +public enum PlannedFrameFlags +{ + None = 0, + + /// Synthesised between two included source frames from the motion field. + Interpolated = 1, + + /// Shown for more than one output frame period. + Held = 2, +} + +/// One output frame of a . +/// Source frame index; fractional for synthesised frames (it may fall on an excluded frame). +/// Included source frame at or before the position. +/// Included source frame after the position; equals for whole frames. +/// Blend position between (0) and (1); 0 for whole frames. +/// Presentation time on the 90 kHz clock. +/// Display duration on the 90 kHz clock. +/// First output frame period at . +/// Output frame periods covered (more than one = held). +/// Source frames advanced per output frame period towards the next planned frame (scales motion blur). +public readonly record struct PlannedFrame( + double SourcePosition, + int From, + int To, + double Fraction, + long Start90k, + long Duration90k, + int StartSlot, + int Slots, + double Speed, + PlannedFrameFlags Flags) +{ + public bool IsInterpolated => (Flags & PlannedFrameFlags.Interpolated) != 0; + + public bool IsHeld => (Flags & PlannedFrameFlags.Held) != 0; +} + +/// Rules of the timing modes (documented defaults; BRIEF §11.1). +public sealed record RenderPlanOptions +{ + public static RenderPlanOptions Default { get; } = new(); + + /// An interval longer than this multiple of the median capture interval is a gap: Adaptive compresses + /// it and motion (blur, synthesis) is treated as discontinuous across it. + public double GapFactor { get; init; } = 3; + + /// Adaptive: longest display of a compressed gap, in playback frames (gaps are shown as + /// GapFactor + log2(ratio / GapFactor) frames up to this cap). + public double MaxGapFrames { get; init; } = 5; + + /// Adaptive: an interval of at least this multiple of the median and is a + /// deliberate pause and becomes a fixed visual cut. + public double PauseFactor { get; init; } = 20; + + public double MinimumPauseSeconds { get; init; } = 120; + + /// Adaptive: video seconds the frame before a deliberate pause stays on screen. + public double PauseCutSeconds { get; init; } = 0.2; + + /// Interpolated: most consecutive missing (or excluded) frames synthesised in one gap; longer gaps are cuts. + public int MaxSynthesizedFrames { get; init; } = 3; +} + +/// Figures for the Time tab's plan chart. +/// Frames in the sequence. +/// Frames excluded by the user. +/// Included frames shown whole at least once. +/// Included frames never shown nor used for synthesis (dropped by speed-ups). +/// Planned frames (held frames count once). +/// Output frame periods at the plan frame rate. +/// Planned frames covering several periods. +/// Planned frames synthesised between two source frames. +/// Motion discontinuities (gaps, pauses, long exclusion runs). +/// Median capture interval, when capture times are known. +public sealed record RenderPlanStatistics( + int SourceFrames, + int ExcludedFrames, + int ShownFrames, + int SkippedFrames, + int OutputFrames, + int OutputSlots, + int HeldFrames, + int InterpolatedFrames, + int Cuts, + double DurationSeconds, + double MinimumSpeed, + double MaximumSpeed, + double? MedianIntervalSeconds); + +/// +/// The list of output frames of a render (BRIEF §11.1): every timing mode produces planned frames with a (possibly +/// fractional) source position and a duration on the output frame grid. +/// +/// The playback rate (, default the output rate) is how many source frames +/// one second of video shows; the output grid is the export frame rate. +/// Constant: every included frame lasts one playback frame (whole frames only). +/// Adaptive: a frame lasts its capture interval in median intervals; intervals up to +/// are kept proportional (bursts play faster, small gaps are held), longer gaps are compressed logarithmically and +/// capped, deliberate pauses become a fixed cut of ; unknown or +/// non-positive intervals count as one. +/// Interpolated: like Constant, but gaps of up to missing or +/// excluded frames get their periods back with synthesised in-betweens, and slower-than-real-time playback is +/// synthesised rather than held. +/// Remapped: a Fritsch–Carlson monotone spline through maps output time to +/// source time (both 0–1); fractional positions are synthesised. +/// +/// rescales the output time axis. Each output period shows one picture +/// (synthesised stretches sampled at the period start, whole frames at its centre); consecutive periods showing the +/// same picture merge into one held frame. Durations are differences of the rounded 90 kHz times of period boundaries, +/// so they add up exactly to the total. +/// +public sealed class RenderPlan +{ + private const double SnapEpsilon = 1e-6; + private readonly bool[] _cutBefore; + private readonly int[] _includedOrder; + + private RenderPlan(TimingMode mode, Rational frameRate, double playbackRate, PlannedFrame[] frames, int slots, int[] included, + bool[] cutBefore, RenderPlanStatistics statistics, IReadOnlyList warnings) + { + Mode = mode; + FrameRate = frameRate; + PlaybackRate = playbackRate; + Frames = frames; + SlotCount = slots; + Duration90k = TicksAt(frameRate, slots); + IncludedFrames = included; + _cutBefore = cutBefore; + _includedOrder = new int[cutBefore.Length]; + Array.Fill(_includedOrder, -1); + for (var k = 0; k < included.Length; k++) _includedOrder[included[k]] = k; + Statistics = statistics; + Warnings = warnings; + } + + public TimingMode Mode { get; } + + /// Output frame rate (the grid of every duration). + public Rational FrameRate { get; } + + /// Source frames per second of video before fitting. + public double PlaybackRate { get; } + + public IReadOnlyList Frames { get; } + + /// Output frame periods covered by the plan. + public int SlotCount { get; } + + /// Exact total duration on the 90 kHz clock. + public long Duration90k { get; } + + public double DurationSeconds => Duration90k / 90000.0; + + /// Source indices that are not excluded, ascending. + public IReadOnlyList IncludedFrames { get; } + + public RenderPlanStatistics Statistics { get; } + + /// Localisation keys of planning remarks (e.g. Adaptive without capture times). + public IReadOnlyList Warnings { get; } + + /// True when motion is discontinuous between and the included frame before it. + public bool IsCutBefore(int sourceIndex) => _cutBefore[sourceIndex]; + + /// The included frame after with continuous motion, or −1. + public int NextContinuous(int sourceIndex) + { + var k = _includedOrder[sourceIndex]; + if (k < 0 || k + 1 >= IncludedFrames.Count) return -1; + var next = IncludedFrames[k + 1]; + return _cutBefore[next] ? -1 : next; + } + + /// The included frame before with continuous motion, or −1. + public int PreviousContinuous(int sourceIndex) + { + var k = _includedOrder[sourceIndex]; + return k <= 0 || _cutBefore[sourceIndex] ? -1 : IncludedFrames[k - 1]; + } + + /// Start of output period on the 90 kHz clock, rounded half up. + public static long TicksAt(Rational frameRate, long slot) => + (slot * 90000L * frameRate.Denominator * 2 + frameRate.Numerator) / (2L * frameRate.Numerator); + + /// Builds the plan for a sequence. + /// Capture time per source frame (clock-corrected when available, e.g. + /// LightAnalysisReport.AdjustedTimes); null for unknown. + /// Timing settings and excluded frames. + /// Output (encoded) frame rate. + public static RenderPlan Build(IReadOnlyList captureTimes, ProjectSettings project, Rational frameRate, RenderPlanOptions? options = null) + { + ArgumentNullException.ThrowIfNull(captureTimes); + ArgumentNullException.ThrowIfNull(project); + if (frameRate.Numerator <= 0 || frameRate.Denominator <= 0) throw new ArgumentOutOfRangeException(nameof(frameRate)); + options ??= RenderPlanOptions.Default; + var n = captureTimes.Count; + var timing = project.Timing; + var mode = timing.Mode.Resolve(TimingMode.Constant); + var playback = FrameRateText.TryParse(timing.FrameRate.Resolve(string.Empty), out var rate) ? rate.Value : frameRate.Value; + var warnings = new List(); + + var excluded = new bool[n]; + foreach (var i in project.ExcludedFrames) + { + if ((uint)i < (uint)n) excluded[i] = true; + } + var included = new List(n); + for (var i = 0; i < n; i++) + { + if (!excluded[i]) included.Add(i); + } + + var median = MedianInterval(captureTimes); + if (mode is TimingMode.Adaptive or TimingMode.Interpolated && median is null) warnings.Add("Render.Plan.NoCaptureTimes"); + var cutBefore = new bool[n]; + var entries = Timeline(captureTimes, included, mode, playback, median, options, cutBefore); + if (entries.Length == 0) + { + warnings.Add("Render.Plan.NoFrames"); + var empty = new RenderPlanStatistics(n, n - included.Count, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, median); + return new RenderPlan(mode, frameRate, playback, [], 0, [.. included], cutBefore, empty, warnings); + } + + var total = entries[^1].Start + entries[^1].Length; + var seconds = timing.FitToSeconds is { } fit && fit > 0 ? fit : total / playback; + var slots = (int)long.Clamp((long)double.Round(seconds * frameRate.Value), 1, int.MaxValue / 2); + var remap = mode == TimingMode.Remapped ? RemapSpline(timing.RemapCurve, warnings) : null; + + var frames = new List(); + Sample? open = null; + var openStart = 0; + for (var k = 0; k <= slots; k++) + { + Sample? sample = null; + if (k < slots) + { + sample = Locate(entries, TimelinePosition(k, slots, total, remap), TimelinePosition(k + 0.5, slots, total, remap), total); + if (open is { } current && current.SameAs(sample.Value)) continue; + } + if (open is { } done) frames.Add(Planned(done, openStart, k - openStart, frameRate)); + open = sample; + openStart = k; + } + + var planned = frames.ToArray(); + AssignSpeeds(planned); + var statistics = Summarise(n, n - included.Count, included, planned, slots, cutBefore, frameRate, median); + return new RenderPlan(mode, frameRate, playback, planned, slots, [.. included], cutBefore, statistics, warnings); + } + + /// A stretch of the playback timeline shown by one included frame. + /// Start in playback frames. + /// Length in playback frames (> 0). + /// Source index shown at the start. + /// Next included source index, −1 for the last. + /// The position moves linearly towards across the stretch (synthesis). + private readonly record struct Entry(double Start, double Length, int Index, int Next, bool Continuous); + + private readonly record struct Sample(int From, int To, double Fraction, double Position) + { + public bool SameAs(Sample other) => From == other.From && To == other.To && Fraction == other.Fraction; + } + + private static Entry[] Timeline(IReadOnlyList times, List included, TimingMode mode, double playback, double? median, + RenderPlanOptions options, bool[] cutBefore) + { + var entries = new Entry[included.Count]; + double start = 0; + for (var k = 0; k < included.Count; k++) + { + var index = included[k]; + var next = k + 1 < included.Count ? included[k + 1] : -1; + double length = 1; + var continuous = false; + if (next >= 0) + { + var seconds = times[index] is { } a && times[next] is { } b ? (b - a).TotalSeconds : double.NaN; + var ratio = median is { } m && seconds > 0 ? seconds / m : double.NaN; + var steps = next - index; + var gap = double.Max(double.IsNaN(ratio) ? steps : ratio, steps) > options.GapFactor; + switch (mode) + { + case TimingMode.Adaptive: + length = AdaptiveLength(ratio, seconds, playback, options, ref gap); + break; + case TimingMode.Interpolated: + var periods = int.Max(steps, double.IsNaN(ratio) ? 1 : (int)double.Min(double.Round(ratio), int.MaxValue / 2)); + gap = periods - 1 > options.MaxSynthesizedFrames; + if (!gap) + { + length = periods; + continuous = true; + } + break; + case TimingMode.Remapped: + continuous = !gap; + break; + } + cutBefore[next] = gap; + } + entries[k] = new Entry(start, length, index, next, continuous); + start += length; + } + return entries; + } + + private static double AdaptiveLength(double ratio, double seconds, double playback, RenderPlanOptions options, ref bool gap) + { + if (double.IsNaN(ratio)) return 1; + if (ratio <= options.GapFactor) return ratio; + gap = true; + if (ratio >= options.PauseFactor && seconds >= options.MinimumPauseSeconds) return double.Max(options.PauseCutSeconds * playback, 1e-3); + return double.Min(options.MaxGapFrames, options.GapFactor + double.Log2(ratio / options.GapFactor)); + } + + /// Playback-timeline position of output period (fractional slots address its interior). + private static double TimelinePosition(double slot, int slots, double total, MonotoneSpline? remap) + { + var x = slot / slots; + return remap is null ? x * total : double.Clamp(remap.Evaluate(x), 0, 1) * total; + } + + /// + /// The picture of an output period. Synthesised stretches are sampled at the period start (so equal rates give whole + /// frames); whole-frame stretches at the period centre, which rounds every frame boundary to the nearest period + /// (ties keep the earlier frame, so a 2× speed-up shows frames 0, 2, 4…). + /// + private static Sample Locate(Entry[] entries, double start, double centre, double total) + { + var e = entries[Find(entries, double.Clamp(start, 0, total), atBoundary: true)]; + if (e.Continuous && e.Next >= 0) + { + var fraction = double.Clamp((start - e.Start) / e.Length, 0, 1); + if (fraction < SnapEpsilon) return new Sample(e.Index, e.Index, 0, e.Index); + if (fraction > 1 - SnapEpsilon) return new Sample(e.Next, e.Next, 0, e.Next); + return new Sample(e.Index, e.Next, fraction, e.Index + fraction * (e.Next - e.Index)); + } + var index = entries[Find(entries, double.Clamp(centre, 0, total), atBoundary: false)].Index; + return new Sample(index, index, 0, index); + } + + /// Entry containing ; a position within 1e-9 of a boundary belongs to the entry starting + /// there when , else to the one ending there. + private static int Find(Entry[] entries, double u, bool atBoundary) + { + var limit = atBoundary ? u + 1e-9 : u - 1e-9; + int lo = 0, hi = entries.Length - 1; + while (lo < hi) + { + var mid = (lo + hi + 1) >> 1; + if (atBoundary ? entries[mid].Start <= limit : entries[mid].Start < limit) lo = mid; + else hi = mid - 1; + } + return lo; + } + private static PlannedFrame Planned(Sample s, int startSlot, int slots, Rational rate) + { + var start = TicksAt(rate, startSlot); + var flags = (s.Fraction > 0 ? PlannedFrameFlags.Interpolated : PlannedFrameFlags.None) | (slots > 1 ? PlannedFrameFlags.Held : PlannedFrameFlags.None); + return new PlannedFrame(s.Position, s.From, s.To, s.Fraction, start, TicksAt(rate, startSlot + slots) - start, startSlot, slots, 0, flags); + } + + private static void AssignSpeeds(PlannedFrame[] frames) + { + for (var k = 0; k + 1 < frames.Length; k++) + frames[k] = frames[k] with { Speed = (frames[k + 1].SourcePosition - frames[k].SourcePosition) / frames[k].Slots }; + if (frames.Length > 1) frames[^1] = frames[^1] with { Speed = frames[^2].Speed }; + else if (frames.Length == 1) frames[0] = frames[0] with { Speed = 1 }; + } + + /// Sorted, clamped and made non-decreasing so the spline (and the remap) is monotone. + private static MonotoneSpline? RemapSpline(IReadOnlyList curve, List warnings) + { + var points = curve.Where(p => double.IsFinite(p.X) && double.IsFinite(p.Y)) + .Select(p => (X: double.Clamp(p.X, 0, 1), Y: double.Clamp(p.Y, 0, 1))) + .OrderBy(p => p.X) + .ToList(); + if (points.Count == 0) return null; + var xs = new List(points.Count); + var ys = new List(points.Count); + var adjusted = false; + foreach (var (x, y) in points) + { + var value = y; + if (ys.Count > 0 && value < ys[^1]) + { + value = ys[^1]; + adjusted = true; + } + if (xs.Count > 0 && x == xs[^1]) + { + ys[^1] = double.Max(ys[^1], value); + continue; + } + xs.Add(x); + ys.Add(value); + } + if (adjusted) warnings.Add("Render.Plan.RemapNotMonotonic"); + return new MonotoneSpline(xs.ToArray(), ys.ToArray()); + } + + private static double? MedianInterval(IReadOnlyList times) + { + var intervals = new List(times.Count); + for (var i = 1; i < times.Count; i++) + { + if (times[i - 1] is { } a && times[i] is { } b && b > a) intervals.Add((b - a).TotalSeconds); + } + return intervals.Count == 0 ? null : Mathematics.Statistics.Median(intervals.ToArray()); + } + + private static RenderPlanStatistics Summarise(int sourceFrames, int excludedFrames, List included, PlannedFrame[] frames, int slots, + bool[] cutBefore, Rational rate, double? median) + { + var shown = new HashSet(); + var used = new HashSet(); + int held = 0, interpolated = 0; + double minSpeed = double.PositiveInfinity, maxSpeed = double.NegativeInfinity; + foreach (var f in frames) + { + used.Add(f.From); + used.Add(f.To); + if (f.Fraction == 0) shown.Add(f.From); + if (f.IsHeld) held++; + if (f.IsInterpolated) interpolated++; + minSpeed = double.Min(minSpeed, f.Speed); + maxSpeed = double.Max(maxSpeed, f.Speed); + } + var cuts = cutBefore.Count(c => c); + return new RenderPlanStatistics(sourceFrames, excludedFrames, shown.Count, included.Count - used.Count, frames.Length, slots, held, interpolated, + cuts, TicksAt(rate, slots) / 90000.0, frames.Length > 0 ? minSpeed : 0, frames.Length > 0 ? maxSpeed : 0, median); + } +} diff --git a/tests/Titano.Core.Tests/Render/RenderPlanTests.cs b/tests/Titano.Core.Tests/Render/RenderPlanTests.cs new file mode 100644 index 00000000..c269fa0f --- /dev/null +++ b/tests/Titano.Core.Tests/Render/RenderPlanTests.cs @@ -0,0 +1,193 @@ +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? curve = null, int[]? excluded = null) => new() + { + Timing = new TimingSettings + { + Mode = new Auto().WithManual(mode), + FrameRate = playback is null ? new Auto() : new Auto().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); + } +}