using Chronos.Core.Project;
using Chronos.Core.Video;
namespace Chronos.Core.Render;
/// One frame the encoder receives, in emission order ().
/// The frame of rendered for it.
/// How long it shows, on the 90 kHz clock: the planned frame's duration, a share of it (a held
/// frame split per slot inside a fade), or longer (the hold at the end).
/// The linear-light multiplier of the fade to and from black, applied after the look and the knee
/// (FrameFinish): 1 leaves the frame as it is, 0 is black. Already in linear light (the sRGB ramp converted), so a
/// fade looks even.
public readonly record struct ProgramEntry(int PlanIndex, long Duration90k, double Gain);
///
/// The output program of a render (owner review 1, R8): the plan's frames in the order and for the time they are emitted,
/// with the fade gain of each, from the export's . Reverse is the plan backwards; a boomerang
/// is forward then backward without repeating the turning frame; loops repeat the pass (a boomerang's later passes start at
/// its second frame, so the joint is not doubled); the hold extends the last emitted frame; fades longer than half of the
/// program are scaled to fit (warning ). Every pass is rendered again, so the render
/// time grows with .
///
///
/// Phase 5 groundwork: the API is final; emits
/// the plan forward once, without fades or hold, until the output program parcel completes it.
///
public sealed class OutputProgram
{
/// Warning key: the fades were longer than half of the program and were scaled to fit.
public const string FadesShortenedWarning = "Export.Render.FadesShortened";
/// Warning key: the playback names a direction this Chronos does not know ({0}); the video plays forward.
public const string DirectionUnknownWarning = "Export.Render.DirectionUnknown";
/// Warning key: a boomerang is not rendered with star trails; the video plays forward.
public const string BoomerangStackWarning = "Export.Render.BoomerangStack";
private OutputProgram(IReadOnlyList entries, IReadOnlyList warnings)
{
Entries = entries;
Warnings = warnings;
long total = 0;
foreach (var entry in entries) total += entry.Duration90k;
Duration90k = total;
}
/// The emitted frames, in order.
public IReadOnlyList Entries { get; }
/// Frames the encoder receives (and the render renders).
public int Count => Entries.Count;
/// Exact total duration on the 90 kHz clock, holds included.
public long Duration90k { get; }
/// The duration in seconds.
public double DurationSeconds => Duration90k / 90000.0;
/// What the program changed of the playback asked for (fades shortened, an unknown direction played forward).
public IReadOnlyList Warnings { get; }
/// The program of 's frames played as says (null: forward once).
public static OutputProgram Build(RenderPlan plan, PlaybackSettings? playback, Rational frameRate)
{
ArgumentNullException.ThrowIfNull(plan);
return Build(plan.Frames, playback, frameRate);
}
/// The program of the planned played as says (null: forward once).
/// The output frame rate: the grid a held frame is split on inside a fade.
public static OutputProgram Build(IReadOnlyList frames, PlaybackSettings? playback, Rational frameRate)
{
ArgumentNullException.ThrowIfNull(frames);
if (frameRate.Numerator <= 0 || frameRate.Denominator <= 0) throw new ArgumentOutOfRangeException(nameof(frameRate));
var warnings = new List();
if (playback?.Direction is { } direction && !PlaybackDirection.IsKnown(direction)) warnings.Add(new RenderWarning(DirectionUnknownWarning, direction));
var entries = new ProgramEntry[frames.Count];
for (var i = 0; i < frames.Count; i++) entries[i] = new ProgramEntry(i, frames[i].Duration90k, 1);
return new OutputProgram(entries, warnings);
}
}