Files
Chronos/src/Titano.App/Editor/ExportPlanner.cs
T

114 lines
6.3 KiB
C#
Raw Blame History

This file contains ambiguous Unicode characters
This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.
using Titano.Core.Advice;
using Titano.Core.Export;
using Titano.Core.Imaging;
using Titano.Core.Localization;
using Titano.Core.Project;
using Titano.Core.Render;
using Titano.Core.Video;
using Titano.Data.Processing;
namespace Titano.App.Editor;
/// <summary>A choice of the Export tab: a built-in export preset, or a sequence preset (full settings plus an export preset).</summary>
/// <param name="ExportPresetId">The export preset the card renders.</param>
/// <param name="Sequence">The sequence preset it applies, null for a plain export preset.</param>
/// <param name="IsRecommended">The advisor's export choice for this sequence.</param>
/// <param name="Reason">Why it fits (advisor reason), localised; null for plain presets.</param>
public sealed record ExportChoice(string Id, string Name, string ExportPresetId, SequencePreset? Sequence, bool IsRecommended, string? Reason);
/// <summary>What one export will produce: resolved values, encoder, plan, size and time estimate, file name.</summary>
/// <param name="FileName">Output name with extension (a folder name for image sequences).</param>
/// <param name="NameError">The first problem of the name template, localised; null when it is valid.</param>
public sealed record ExportPlan(ResolvedExport Export, RenderSetup Setup, ExportEstimate Estimate, string FileName, string? NameError)
{
public string PresetName => ProcessingJobs.PresetName(Export);
}
/// <summary>
/// The Export tab's numbers (BRIEF §11.4–11.5): the preset list (sequence presets first, then the built-in presets, the
/// advisor's pick marked), and for any export the encoder this machine would use with its notices, the plan, the size
/// and time estimates (<see cref="ExportEstimator"/>) and the output name (<see cref="ExportNaming"/>). Nothing decodes:
/// the plan needs only the frame metadata and the source size.
/// </summary>
public sealed class ExportPlanner(SequenceContent content, IReadOnlyList<IVideoEncoderProvider> encoders, EncoderHost host, ThroughputModel? model = null)
{
/// <summary>Encoder providers of the machine that renders, in priority order.</summary>
public IReadOnlyList<IVideoEncoderProvider> Encoders { get; } = encoders;
/// <summary>Sequence presets that fit the content, then every built-in export preset.</summary>
public static IReadOnlyList<ExportChoice> Choices(IReadOnlyList<SequencePreset> presets, AdvisorResult advice)
{
ArgumentNullException.ThrowIfNull(presets);
ArgumentNullException.ThrowIfNull(advice);
var recommended = advice.Find(AdvisorParameters.ExportPreset)?.Value as string;
var choices = new List<ExportChoice>();
foreach (var preset in presets)
{
choices.Add(new ExportChoice("sequence:" + preset.Id, preset.DisplayName(), preset.ExportPresetId, preset, false,
preset.ReasonKey is { } key ? Advisor.Explain(key, preset.ReasonArgs) : null));
}
var reason = advice.Find(AdvisorParameters.ExportPreset) is { IsMissingData: false } r ? Advisor.Explain(r) : null;
foreach (var preset in ExportPresets.BuiltIn)
{
var isRecommended = string.Equals(preset.Id, recommended, StringComparison.OrdinalIgnoreCase);
choices.Add(new ExportChoice(preset.Id, Loc.L(preset.NameKey), preset.Id, null, isRecommended, isRecommended ? reason : null));
}
return choices;
}
/// <summary>The project's own export.</summary>
public ExportPlan? Plan(ProjectSettings project) => Plan(project, project.Export);
/// <summary>What <paramref name="export"/> would produce with <paramref name="project"/>; null when the source size is unknown.</summary>
public ExportPlan? Plan(ProjectSettings project, ExportSettings export)
{
ArgumentNullException.ThrowIfNull(project);
ArgumentNullException.ThrowIfNull(export);
if (content.SourceWidth <= 0 || content.SourceHeight <= 0 || content.Count == 0) return null;
var resolved = ExportResolver.Resolve(export);
var input = new RenderInput
{
Source = new SizeOnly(content),
Frames = SourceFrameInfo.From(content.Frames, content.Light.AdjustedTimes.Length == content.Count ? content.Light.AdjustedTimes : null),
Project = project with { Export = export, ExcludedFrames = [.. project.ExcludedFrames.Union(content.NotRenderable()).Order()] },
};
var setup = RenderSetup.Create(input, new RenderTarget(resolved, ExportNaming.FallbackName), Encoders, host);
var (name, error) = FileName(export.NameTemplate, resolved, setup);
return new ExportPlan(resolved, setup, setup.Estimate(model), name, error);
}
/// <summary>The first problem of a name template, localised; null when it is valid.</summary>
public static string? NameError(IReadOnlyList<Core.Naming.TemplateError> errors) =>
errors.Count == 0 ? null : Loc.L("Errors.Template." + errors[0].Code, errors[0].Token ?? string.Empty, errors[0].Position + 1);
private (string Name, string? Error) FileName(string template, ResolvedExport export, RenderSetup setup)
{
var settings = setup.Encoder.Settings;
var expanded = ExportNaming.Expand(template, new ExportNameContext
{
Date = content.Sequence.FirstCapture ?? DateTime.Now,
Title = content.Sequence.DisplayTitle,
PresetName = ProcessingJobs.PresetName(export),
Width = settings.Width,
Height = settings.Height,
FrameRate = settings.FrameRate,
Codec = settings.Codec,
});
var extension = VideoCodecInfo.DefaultContainer(settings.Codec) == ContainerFormat.ImageSequence ? string.Empty : VideoCodecInfo.FileExtension(settings.Codec);
return (expanded.Name + extension, NameError(expanded.Errors));
}
/// <summary>The render setup reads sizes and counts only.</summary>
private sealed class SizeOnly(SequenceContent content) : IRenderFrameSource
{
public int FrameCount => content.Count;
public int Width => content.SourceWidth;
public int Height => content.SourceHeight;
public ValueTask<ImageBuffer> GetFrameAsync(int index, int maxLongEdge, DecodePurpose purpose, CancellationToken cancellationToken) =>
throw new NotSupportedException("Export planning never decodes.");
}
}