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; /// A choice of the Export tab: a built-in export preset, or a sequence preset (full settings plus an export preset). /// The export preset the card renders. /// The sequence preset it applies, null for a plain export preset. /// The advisor's export choice for this sequence. /// Why it fits (advisor reason), localised; null for plain presets. public sealed record ExportChoice(string Id, string Name, string ExportPresetId, SequencePreset? Sequence, bool IsRecommended, string? Reason); /// What one export will produce: resolved values, encoder, plan, size and time estimate, file name. /// Output name with extension (a folder name for image sequences). /// The first problem of the name template, localised; null when it is valid. public sealed record ExportPlan(ResolvedExport Export, RenderSetup Setup, ExportEstimate Estimate, string FileName, string? NameError) { public string PresetName => ProcessingJobs.PresetName(Export); } /// /// 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 () and the output name (). Nothing decodes: /// the plan needs only the frame metadata and the source size. /// public sealed class ExportPlanner(SequenceContent content, IReadOnlyList encoders, EncoderHost host, ThroughputModel? model = null) { /// Encoder providers of the machine that renders, in priority order. public IReadOnlyList Encoders { get; } = encoders; /// Sequence presets that fit the content, then every built-in export preset. public static IReadOnlyList Choices(IReadOnlyList presets, AdvisorResult advice) { ArgumentNullException.ThrowIfNull(presets); ArgumentNullException.ThrowIfNull(advice); var recommended = advice.Find(AdvisorParameters.ExportPreset)?.Value as string; var choices = new List(); 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; } /// The project's own export. public ExportPlan? Plan(ProjectSettings project) => Plan(project, project.Export); /// What would produce with ; null when the source size is unknown. 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); } /// The first problem of a name template, localised; null when it is valid. public static string? NameError(IReadOnlyList 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)); } /// The render setup reads sizes and counts only. private sealed class SizeOnly(SequenceContent content) : IRenderFrameSource { public int FrameCount => content.Count; public int Width => content.SourceWidth; public int Height => content.SourceHeight; public ValueTask GetFrameAsync(int index, int maxLongEdge, DecodePurpose purpose, CancellationToken cancellationToken) => throw new NotSupportedException("Export planning never decodes."); } }