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);
|
||||
}
|
||||
}
|
||||
Reference in new issue
Block a user