Files
Chronos/src/Chronos.Data/Processing/SequenceRender.cs
T
Alby96andClaude Opus 5.5 1be273d0e4 fix: address the phase 5 review
Video trash moves across volumes commit once the verified copy has its
name and leave an undeletable source whole behind a .leftover marker that
the next start settles; deletes rehash under the lease. Restores honour
the names renders are writing, and encoders never overwrite an output.
Routing floors at the stored settings' format, kindless stills read as
stills, star trails restart per loop, and settings the encoder ignored
become render warnings. The Videos section fits beside the player, plays
downloaded server copies, names blocked videos and says why actions are
off; the admin panel offers no download for sequences or missing files
and names a filtered sequence from its record.

Refs #21

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
2026-11-05 04:11:02 +01:00

247 lines
14 KiB
C#

using Chronos.Core.Analysis;
using Chronos.Core.Analysis.Deep;
using Chronos.Core.Export;
using Chronos.Core.Imaging;
using Chronos.Core.Localization;
using Chronos.Core.Model;
using Chronos.Core.Project;
using Chronos.Core.Render;
namespace Chronos.Data.Processing;
/// <summary>One output of a render job: the resolved export values and the preset name recorded with the render.</summary>
internal sealed record ExportTarget(ResolvedExport Export, string PresetName);
/// <summary>How one target of <see cref="SequenceRender.RenderAllAsync"/> ended: a result, or the localised reason it failed.</summary>
internal sealed record TargetOutcome(ExportTarget Target, RenderResult? Result, string? Error);
/// <summary>
/// The render of one sequence (BRIEF §11): the frame source, the clock-corrected capture metadata, the project with the
/// frames that cannot be rendered excluded, and the pipeline with the host's encoders. Targets share one preparation
/// pass; each is named once its encoder (and so its codec and size) is known, and failures of one target do not stop
/// the others. Outputs are written by the encoder sessions to temporary names and renamed on completion, so a
/// cancelled or failed target leaves nothing behind. A target gets the hardware encoders only when the user allows them
/// for its preset (<see cref="ProcessingOptions.AllowsHardware"/>).
/// </summary>
internal sealed class SequenceRender : IAsyncDisposable
{
private readonly SequenceFrameSource _source;
private readonly ProcessingOptions _processing;
private readonly Dictionary<bool, RenderPipeline> _pipelines = [];
private readonly RenderOptions _options;
/// <param name="analysis">The sequence's deep report, when current: its luminance spares the preparation a decode pass.</param>
/// <param name="originals">The frames' files are the archive's originals (no proxies), so <see cref="Decoded"/> describes them.</param>
/// <param name="companions">The JPEG of each GPR frame kept in another sequence, by frame id (<see cref="Archiving.GprCompanions"/>).</param>
public SequenceRender(SequenceFrames frames, ProjectSettings project, DeepAnalysisReport? analysis, ProcessingOptions options, bool originals = false,
IReadOnlyDictionary<Guid, string>? companions = null)
{
// The host's tuning as it is now (memory budget, quality profile): a change applies from the next render.
options = options.Current;
var metadata = frames.LightFrames();
var planned = project with { ExcludedFrames = [.. project.ExcludedFrames.Union(frames.NotRenderable()).Order()] };
// A frame that turns out not to decode is shown as its nearest planned neighbour instead of failing the render.
_source = new SequenceFrameSource(frames, metadata, options.Archive.Decoder, options.FrameWindow)
{
Originals = originals,
RenderExcluded = planned.ExcludedFrames.ToHashSet(),
Companions = companions,
};
_processing = options;
_options = options.Render;
Input = new RenderInput
{
Source = _source,
Frames = SourceFrameInfo.From(frames.Frames, LightAnalyzer.Analyze(metadata).AdjustedTimes),
Project = planned,
Analysis = analysis?.FrameCount == frames.Count ? new PrecomputedAnalysis { LogLuminance = Luminance(analysis.Frames) } : null,
};
}
public RenderInput Input { get; }
/// <summary>Positions that did not decode during the render; a neighbour stood in for each (ascending).</summary>
public IReadOnlyList<int> StandIns => _source.StandIns;
/// <summary>The decode tier each frame was read at so far (unavailable when it did not decode); originals only.</summary>
public IReadOnlyDictionary<int, FileTier> Decoded => _source.Observed;
/// <summary>Encoder, geometry and plan of an export before its output is named.</summary>
public RenderSetup Setup(ResolvedExport export) => Pipeline(export).Setup(Input, new RenderTarget(export, ExportNaming.FallbackName), _options);
/// <summary>
/// Prepares the sequence once and renders every target. <paramref name="name"/> gives the output path of a target
/// from its setup; <paramref name="starting"/> runs before a target writes anything (checkpoints),
/// <paramref name="finished"/> after its output is complete (record it, upload it). Progress counts measured and
/// rendered frames over the whole call.
/// </summary>
public async Task<IReadOnlyList<TargetOutcome>> RenderAllAsync(
IReadOnlyList<(int Index, ExportTarget Target)> targets,
Func<ExportTarget, RenderSetup, string> name,
Func<int, string, ValueTask> starting,
Func<int, ExportTarget, RenderResult, ValueTask> finished,
Func<long, long, string?, ValueTask> progress,
CancellationToken cancellationToken)
{
var outcomes = new List<TargetOutcome>(targets.Count);
var ready = new List<(int Index, ExportTarget Target, RenderSetup Setup)>();
foreach (var (index, target) in targets)
{
RenderSetup setup;
try
{
setup = Setup(target.Export);
}
catch (RenderException ex)
{
// The source size could not be measured (no renderable frame decodes).
outcomes.Add(new TargetOutcome(target, null, Reason(ex)));
continue;
}
if (setup.Encoder.IsReady && setup.Program.Count > 0) ready.Add((index, target, setup));
else outcomes.Add(new TargetOutcome(target, null, Reason(setup)));
}
if (ready.Count == 0) return outcomes;
var preparing = ready[0].Setup.NeedsPreparation ? ready[0].Setup.Plan.IncludedFrames.Count : 0;
// Rendered pictures are counted in the output program's entries (loops, a boomerang and fades add to the plan's).
var total = preparing + ready.Sum(r => (long)r.Setup.Program.Count);
var stage = Loc.L("Export.Job.Preparing");
var chain = new ProgressChain(progress);
var corrections = await RenderPreparation.PrepareAsync(Input, _options, Stage(chain, 0, preparing, total, stage), cancellationToken).ConfigureAwait(false);
await chain.FlushAsync().ConfigureAwait(false);
long done = preparing;
foreach (var (index, target, setup) in ready)
{
cancellationToken.ThrowIfCancellationRequested();
var output = name(target, setup);
await starting(index, output).ConfigureAwait(false);
try
{
var result = await Pipeline(target.Export).RenderAsync(Input, corrections, new RenderTarget(target.Export, output), _options,
Stage(chain, done, setup.Program.Count, total, target.PresetName), cancellationToken).ConfigureAwait(false);
await chain.FlushAsync().ConfigureAwait(false);
await finished(index, target, result).ConfigureAwait(false);
outcomes.Add(new TargetOutcome(target, result, null));
}
catch (Exception ex) when (!(ex is OperationCanceledException && cancellationToken.IsCancellationRequested))
{
Core.Diagnostics.Log.Warning("processing", $"Render of {target.PresetName} to {output} failed", ex);
outcomes.Add(new TargetOutcome(target, null, Reason(ex)));
}
done += setup.Program.Count;
}
return outcomes;
}
/// <summary>
/// Saves frame <paramref name="frameIndex"/> as one image ("Save frame", owner review 1, R8): the sequence prepared whole
/// as for a video (so its exposure, colour and stabilisation corrections are the ones the video gives that frame), then
/// that frame rendered by the pipeline's still path (<see cref="RenderTarget.StillFrame"/>) at the export's output size
/// with every correction and the look, without stacking, interpolation, motion blur or playback, through the image
/// encoder of <paramref name="target"/>'s codec (PNG, JPEG or TIFF 16-bit). The encoder writes a temporary name and
/// renames it once complete, so a cancelled or failed still leaves nothing behind.
/// </summary>
/// <param name="name">The output path of the still from its setup.</param>
/// <param name="starting">Runs with that path before anything is written (checkpoints).</param>
/// <exception cref="RenderException">The frame is not one of the sequence's, or no encoder writes the codec.</exception>
public async Task<RenderResult> RenderStillAsync(ExportTarget target, int frameIndex, Func<RenderSetup, string> name, Func<string, ValueTask> starting,
Func<long, long, string?, ValueTask> progress, CancellationToken cancellationToken)
{
ArgumentNullException.ThrowIfNull(target);
ArgumentNullException.ThrowIfNull(name);
var pipeline = Pipeline(target.Export);
var setup = pipeline.Setup(Input, new RenderTarget(target.Export, ExportNaming.FallbackName, frameIndex), _options);
if (!setup.Encoder.IsReady)
{
var notice = setup.Encoder.Notices.LastOrDefault() ?? new EncoderNotice(EncoderNotices.NoEncoder, setup.Encoder.Settings.Codec.ToString());
throw new RenderException(notice.Key, $"No encoder can save {target.Export.Codec} here ({setup.Encoder.Status})", notice.Args);
}
var output = name(setup);
await starting(output).ConfigureAwait(false);
var chain = new ProgressChain(progress);
var preparing = setup.NeedsPreparation ? setup.Plan.IncludedFrames.Count : 0;
var total = preparing + 1L;
// The corrections of the whole sequence, indexed by source frame: the frame gets what the video gives it.
var corrections = await RenderPreparation.PrepareAsync(Input, _options, Stage(chain, 0, preparing, total, Loc.L("Export.Job.Preparing")), cancellationToken)
.ConfigureAwait(false);
await chain.FlushAsync().ConfigureAwait(false);
var result = await pipeline.RenderAsync(Input, corrections, new RenderTarget(target.Export, output, frameIndex), _options,
Stage(chain, preparing, 1, total, target.PresetName), cancellationToken).ConfigureAwait(false);
await chain.FlushAsync().ConfigureAwait(false);
return result;
}
/// <summary>
/// The path of a still of frame <paramref name="frameIndex"/> (0-based) in <paramref name="folder"/> (or the export's own
/// folder): <see cref="ExportNaming.StillPath(ExportSettings, string, ExportNameContext, int, Func{string, bool}?)"/>,
/// "2026-09-28 Harbour sunset - PNG - 0124.png"; " (2)" and so on when taken.
/// </summary>
public static string StillPath(ExportSettings export, string folder, ExportNameContext context, int frameIndex, Func<string, bool>? taken = null) =>
ExportNaming.StillPath(export, folder, context, frameIndex, taken);
/// <summary>
/// The frame a still job saves (<see cref="Protocol.RenderJobParameters.StillFrame"/>, <see cref="Protocol.RenderJobParameters.StillPhoto"/>):
/// the frame holding the photo when its SHA-256 is given (the frames may have moved since), null when that photo is no
/// longer in the sequence (the index would name another photo now); without a hash (a photo not hashed yet) the index.
/// Null when neither names a frame of the sequence.
/// </summary>
public static int? StillIndex(SequenceFrames frames, int? frameIndex, string? photoSha256)
{
ArgumentNullException.ThrowIfNull(frames);
if (photoSha256 is { Length: > 0 } sha)
{
for (var i = 0; i < frames.Frames.Count; i++)
{
if (string.Equals(frames.Frames[i].Sha256, sha, StringComparison.OrdinalIgnoreCase)) return i;
}
return null;
}
return frameIndex is { } index && (uint)index < (uint)frames.Frames.Count ? index : null;
}
/// <summary>Values of the output name tokens: the shoot date and title of the sequence, the codec and size the encoder will use.</summary>
public static ExportNameContext NameContext(Sequence sequence, ExportTarget target, RenderSetup setup) => new()
{
Date = sequence.FirstCapture ?? DateTime.Now,
Title = sequence.DisplayTitle,
PresetName = target.PresetName,
Width = setup.Encoder.Settings.Width,
Height = setup.Encoder.Settings.Height,
FrameRate = setup.Encoder.Settings.FrameRate,
Codec = setup.Encoder.Settings.Codec,
};
/// <summary>Localised text of a render failure.</summary>
public static string Reason(Exception exception) =>
exception is RenderException render ? Loc.L(render.ReasonKey, [.. render.Args]) : exception.Message;
/// <summary>Localised remarks of a finished render (encoder fallbacks, ignored settings…), without repeats.</summary>
public static IEnumerable<string> Remarks(RenderResult result) => result.Warnings.Select(w => Loc.L(w.Key, [.. w.Args])).Distinct();
public ValueTask DisposeAsync() => _source.DisposeAsync();
/// <summary>The pipeline with the encoders <paramref name="export"/> may use.</summary>
private RenderPipeline Pipeline(ResolvedExport export)
{
var hardware = _processing.AllowsHardware(export.BasePresetId);
if (!_pipelines.TryGetValue(hardware, out var pipeline))
_pipelines[hardware] = pipeline = new RenderPipeline(hardware ? _processing.Encoders : _processing.SoftwareEncoders, _processing.Host);
return pipeline;
}
private static string Reason(RenderSetup setup)
{
if (setup.Program.Count == 0) return Loc.L("Export.Render.NoFrames");
var notice = setup.Encoder.Notices.LastOrDefault() ?? new EncoderNotice(EncoderNotices.NoEncoder, setup.Encoder.Settings.Codec.ToString());
return Loc.L(notice.Key, [.. notice.Args]);
}
private static double[] Luminance(DeepFrameColumns columns) => [.. columns.LogLuminance.Select(v => v ?? double.NaN)];
/// <summary>One stage's progress (frames measured, or rendered for one target) as frames of the whole call.</summary>
private static IProgress<RenderProgress> Stage(ProgressChain chain, long offset, long length, long total, string message) =>
chain.For<RenderProgress>(p => (offset + long.Min(length, p.Done), total, message));
}