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
This commit is contained in:
3 files changed
+671
No files matched your search
@@ -0,0 +1,36 @@
|
||||
using System.Globalization;
|
||||
using Titano.Core.Video;
|
||||
|
||||
namespace Titano.Core.Export;
|
||||
|
||||
/// <summary>Frame rates as stored in project files: "num/den" ("24000/1001"), a decimal ("29.97") or an integer ("30").</summary>
|
||||
public static class FrameRateText
|
||||
{
|
||||
/// <summary>Parses a frame rate; decimals near the NTSC rates map to their exact fractions.</summary>
|
||||
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;
|
||||
}
|
||||
|
||||
/// <summary>Canonical text: "num/den" for fractional rates, the integer otherwise.</summary>
|
||||
public static string Format(Rational rate) => rate.Denominator == 1
|
||||
? rate.Numerator.ToString(CultureInfo.InvariantCulture)
|
||||
: string.Create(CultureInfo.InvariantCulture, $"{rate.Numerator}/{rate.Denominator}");
|
||||
|
||||
/// <summary>Exact rational equality (24000/1001 equals 48000/2002).</summary>
|
||||
public static bool AreEqual(Rational a, Rational b) => (long)a.Numerator * b.Denominator == (long)b.Numerator * a.Denominator;
|
||||
}
|
||||
@@ -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,
|
||||
|
||||
/// <summary>Synthesised between two included source frames from the motion field.</summary>
|
||||
Interpolated = 1,
|
||||
|
||||
/// <summary>Shown for more than one output frame period.</summary>
|
||||
Held = 2,
|
||||
}
|
||||
|
||||
/// <summary>One output frame of a <see cref="RenderPlan"/>.</summary>
|
||||
/// <param name="SourcePosition">Source frame index; fractional for synthesised frames (it may fall on an excluded frame).</param>
|
||||
/// <param name="From">Included source frame at or before the position.</param>
|
||||
/// <param name="To">Included source frame after the position; equals <paramref name="From"/> for whole frames.</param>
|
||||
/// <param name="Fraction">Blend position between <paramref name="From"/> (0) and <paramref name="To"/> (1); 0 for whole frames.</param>
|
||||
/// <param name="Start90k">Presentation time on the 90 kHz clock.</param>
|
||||
/// <param name="Duration90k">Display duration on the 90 kHz clock.</param>
|
||||
/// <param name="StartSlot">First output frame period at <see cref="RenderPlan.FrameRate"/>.</param>
|
||||
/// <param name="Slots">Output frame periods covered (more than one = held).</param>
|
||||
/// <param name="Speed">Source frames advanced per output frame period towards the next planned frame (scales motion blur).</param>
|
||||
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;
|
||||
}
|
||||
|
||||
/// <summary>Rules of the timing modes (documented defaults; BRIEF §11.1).</summary>
|
||||
public sealed record RenderPlanOptions
|
||||
{
|
||||
public static RenderPlanOptions Default { get; } = new();
|
||||
|
||||
/// <summary>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.</summary>
|
||||
public double GapFactor { get; init; } = 3;
|
||||
|
||||
/// <summary>Adaptive: longest display of a compressed gap, in playback frames (gaps are shown as
|
||||
/// GapFactor + log2(ratio / GapFactor) frames up to this cap).</summary>
|
||||
public double MaxGapFrames { get; init; } = 5;
|
||||
|
||||
/// <summary>Adaptive: an interval of at least this multiple of the median and <see cref="MinimumPauseSeconds"/> is a
|
||||
/// deliberate pause and becomes a fixed visual cut.</summary>
|
||||
public double PauseFactor { get; init; } = 20;
|
||||
|
||||
public double MinimumPauseSeconds { get; init; } = 120;
|
||||
|
||||
/// <summary>Adaptive: video seconds the frame before a deliberate pause stays on screen.</summary>
|
||||
public double PauseCutSeconds { get; init; } = 0.2;
|
||||
|
||||
/// <summary>Interpolated: most consecutive missing (or excluded) frames synthesised in one gap; longer gaps are cuts.</summary>
|
||||
public int MaxSynthesizedFrames { get; init; } = 3;
|
||||
}
|
||||
|
||||
/// <summary>Figures for the Time tab's plan chart.</summary>
|
||||
/// <param name="SourceFrames">Frames in the sequence.</param>
|
||||
/// <param name="ExcludedFrames">Frames excluded by the user.</param>
|
||||
/// <param name="ShownFrames">Included frames shown whole at least once.</param>
|
||||
/// <param name="SkippedFrames">Included frames never shown nor used for synthesis (dropped by speed-ups).</param>
|
||||
/// <param name="OutputFrames">Planned frames (held frames count once).</param>
|
||||
/// <param name="OutputSlots">Output frame periods at the plan frame rate.</param>
|
||||
/// <param name="HeldFrames">Planned frames covering several periods.</param>
|
||||
/// <param name="InterpolatedFrames">Planned frames synthesised between two source frames.</param>
|
||||
/// <param name="Cuts">Motion discontinuities (gaps, pauses, long exclusion runs).</param>
|
||||
/// <param name="MedianIntervalSeconds">Median capture interval, when capture times are known.</param>
|
||||
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);
|
||||
|
||||
/// <summary>
|
||||
/// 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.
|
||||
/// <list type="bullet">
|
||||
/// <item>The playback rate (<see cref="TimingSettings.FrameRate"/>, default the output rate) is how many source frames
|
||||
/// one second of video shows; the output grid is the export frame rate.</item>
|
||||
/// <item>Constant: every included frame lasts one playback frame (whole frames only).</item>
|
||||
/// <item>Adaptive: a frame lasts its capture interval in median intervals; intervals up to <see cref="RenderPlanOptions.GapFactor"/>
|
||||
/// are kept proportional (bursts play faster, small gaps are held), longer gaps are compressed logarithmically and
|
||||
/// capped, deliberate pauses become a fixed cut of <see cref="RenderPlanOptions.PauseCutSeconds"/>; unknown or
|
||||
/// non-positive intervals count as one.</item>
|
||||
/// <item>Interpolated: like Constant, but gaps of up to <see cref="RenderPlanOptions.MaxSynthesizedFrames"/> missing or
|
||||
/// excluded frames get their periods back with synthesised in-betweens, and slower-than-real-time playback is
|
||||
/// synthesised rather than held.</item>
|
||||
/// <item>Remapped: a Fritsch–Carlson monotone spline through <see cref="TimingSettings.RemapCurve"/> maps output time to
|
||||
/// source time (both 0–1); fractional positions are synthesised.</item>
|
||||
/// </list>
|
||||
/// <see cref="TimingSettings.FitToSeconds"/> 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.
|
||||
/// </summary>
|
||||
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<string> 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; }
|
||||
|
||||
/// <summary>Output frame rate (the grid of every duration).</summary>
|
||||
public Rational FrameRate { get; }
|
||||
|
||||
/// <summary>Source frames per second of video before fitting.</summary>
|
||||
public double PlaybackRate { get; }
|
||||
|
||||
public IReadOnlyList<PlannedFrame> Frames { get; }
|
||||
|
||||
/// <summary>Output frame periods covered by the plan.</summary>
|
||||
public int SlotCount { get; }
|
||||
|
||||
/// <summary>Exact total duration on the 90 kHz clock.</summary>
|
||||
public long Duration90k { get; }
|
||||
|
||||
public double DurationSeconds => Duration90k / 90000.0;
|
||||
|
||||
/// <summary>Source indices that are not excluded, ascending.</summary>
|
||||
public IReadOnlyList<int> IncludedFrames { get; }
|
||||
|
||||
public RenderPlanStatistics Statistics { get; }
|
||||
|
||||
/// <summary>Localisation keys of planning remarks (e.g. Adaptive without capture times).</summary>
|
||||
public IReadOnlyList<string> Warnings { get; }
|
||||
|
||||
/// <summary>True when motion is discontinuous between <paramref name="sourceIndex"/> and the included frame before it.</summary>
|
||||
public bool IsCutBefore(int sourceIndex) => _cutBefore[sourceIndex];
|
||||
|
||||
/// <summary>The included frame after <paramref name="sourceIndex"/> with continuous motion, or −1.</summary>
|
||||
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;
|
||||
}
|
||||
|
||||
/// <summary>The included frame before <paramref name="sourceIndex"/> with continuous motion, or −1.</summary>
|
||||
public int PreviousContinuous(int sourceIndex)
|
||||
{
|
||||
var k = _includedOrder[sourceIndex];
|
||||
return k <= 0 || _cutBefore[sourceIndex] ? -1 : IncludedFrames[k - 1];
|
||||
}
|
||||
|
||||
/// <summary>Start of output period <paramref name="slot"/> on the 90 kHz clock, rounded half up.</summary>
|
||||
public static long TicksAt(Rational frameRate, long slot) =>
|
||||
(slot * 90000L * frameRate.Denominator * 2 + frameRate.Numerator) / (2L * frameRate.Numerator);
|
||||
|
||||
/// <summary>Builds the plan for a sequence.</summary>
|
||||
/// <param name="captureTimes">Capture time per source frame (clock-corrected when available, e.g.
|
||||
/// <c>LightAnalysisReport.AdjustedTimes</c>); null for unknown.</param>
|
||||
/// <param name="project">Timing settings and excluded frames.</param>
|
||||
/// <param name="frameRate">Output (encoded) frame rate.</param>
|
||||
public static RenderPlan Build(IReadOnlyList<DateTime?> 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<string>();
|
||||
|
||||
var excluded = new bool[n];
|
||||
foreach (var i in project.ExcludedFrames)
|
||||
{
|
||||
if ((uint)i < (uint)n) excluded[i] = true;
|
||||
}
|
||||
var included = new List<int>(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<PlannedFrame>();
|
||||
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);
|
||||
}
|
||||
|
||||
/// <summary>A stretch of the playback timeline shown by one included frame.</summary>
|
||||
/// <param name="Start">Start in playback frames.</param>
|
||||
/// <param name="Length">Length in playback frames (> 0).</param>
|
||||
/// <param name="Index">Source index shown at the start.</param>
|
||||
/// <param name="Next">Next included source index, −1 for the last.</param>
|
||||
/// <param name="Continuous">The position moves linearly towards <paramref name="Next"/> across the stretch (synthesis).</param>
|
||||
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<DateTime?> times, List<int> 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));
|
||||
}
|
||||
|
||||
/// <summary>Playback-timeline position of output period <paramref name="slot"/> (fractional slots address its interior).</summary>
|
||||
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;
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// 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…).
|
||||
/// </summary>
|
||||
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);
|
||||
}
|
||||
|
||||
/// <summary>Entry containing <paramref name="u"/>; a position within 1e-9 of a boundary belongs to the entry starting
|
||||
/// there when <paramref name="atBoundary"/>, else to the one ending there.</summary>
|
||||
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 };
|
||||
}
|
||||
|
||||
/// <summary>Sorted, clamped and made non-decreasing so the spline (and the remap) is monotone.</summary>
|
||||
private static MonotoneSpline? RemapSpline(IReadOnlyList<CurvePoint> curve, List<string> 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<double>(points.Count);
|
||||
var ys = new List<double>(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<DateTime?> times)
|
||||
{
|
||||
var intervals = new List<double>(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<int> included, PlannedFrame[] frames, int slots,
|
||||
bool[] cutBefore, Rational rate, double? median)
|
||||
{
|
||||
var shown = new HashSet<int>();
|
||||
var used = new HashSet<int>();
|
||||
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);
|
||||
}
|
||||
}
|
||||
@@ -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<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);
|
||||
}
|
||||
}
|
||||
Reference in new issue
Block a user