From e6d1317aab32d1f855a98bfeaaedf4c02e7354c5 Mon Sep 17 00:00:00 2001 From: Claude Date: Fri, 2 Oct 2026 17:00:26 +0000 Subject: [PATCH] feat(app): editor session, project history, local source, preview renderer and export planner Co-Authored-By: Claude Opus 5.5 Claude-Session: https://claude.ai/code/session_012iLMBNwBE2GnphAsu4cjaC --- i18n/en/Editor.resx | 3 + i18n/it/Editor.it.resx | 3 + src/Titano.App/Editor/EditorSession.cs | 172 ++++++++++ src/Titano.App/Editor/EditorSettings.cs | 131 ++++++++ src/Titano.App/Editor/ExportPlanner.cs | 110 ++++++ src/Titano.App/Editor/ISequenceSource.cs | 69 ++++ src/Titano.App/Editor/LatestRunner.cs | 50 +++ src/Titano.App/Editor/LocalSequenceSource.cs | 151 +++++++++ src/Titano.App/Editor/PreviewRenderer.cs | 318 ++++++++++++++++++ src/Titano.App/Editor/ProjectHistory.cs | 76 +++++ src/Titano.App/Editor/ProjectMerge.cs | 42 +++ src/Titano.App/Editor/ProjectSaver.cs | 89 +++++ .../Titano.App.Tests/Editor/EditorFixture.cs | 132 ++++++++ .../Editor/EditorSessionTests.cs | 206 ++++++++++++ .../Editor/ExportPlannerTests.cs | 93 +++++ .../Editor/PreviewRendererTests.cs | 121 +++++++ .../Editor/ProjectHistoryTests.cs | 90 +++++ .../Editor/ProjectStoreTests.cs | 157 +++++++++ .../Editor/SchedulingTests.cs | 133 ++++++++ 19 files changed, 2146 insertions(+) create mode 100644 src/Titano.App/Editor/EditorSession.cs create mode 100644 src/Titano.App/Editor/EditorSettings.cs create mode 100644 src/Titano.App/Editor/ExportPlanner.cs create mode 100644 src/Titano.App/Editor/ISequenceSource.cs create mode 100644 src/Titano.App/Editor/LatestRunner.cs create mode 100644 src/Titano.App/Editor/LocalSequenceSource.cs create mode 100644 src/Titano.App/Editor/PreviewRenderer.cs create mode 100644 src/Titano.App/Editor/ProjectHistory.cs create mode 100644 src/Titano.App/Editor/ProjectMerge.cs create mode 100644 src/Titano.App/Editor/ProjectSaver.cs create mode 100644 tests/Titano.App.Tests/Editor/EditorFixture.cs create mode 100644 tests/Titano.App.Tests/Editor/EditorSessionTests.cs create mode 100644 tests/Titano.App.Tests/Editor/ExportPlannerTests.cs create mode 100644 tests/Titano.App.Tests/Editor/PreviewRendererTests.cs create mode 100644 tests/Titano.App.Tests/Editor/ProjectHistoryTests.cs create mode 100644 tests/Titano.App.Tests/Editor/ProjectStoreTests.cs create mode 100644 tests/Titano.App.Tests/Editor/SchedulingTests.cs diff --git a/i18n/en/Editor.resx b/i18n/en/Editor.resx index 2de00b56..c212191c 100644 --- a/i18n/en/Editor.resx +++ b/i18n/en/Editor.resx @@ -5,4 +5,7 @@ System.Resources.ResXResourceReader, System.Windows.Forms, Version=4.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089 System.Resources.ResXResourceWriter, System.Windows.Forms, Version=4.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089 Pick a sequence in the Library + This sequence is no longer in the archive.long + Jobs cannot run until the archive index is open.long + The settings of this sequence cannot be read.long diff --git a/i18n/it/Editor.it.resx b/i18n/it/Editor.it.resx index a0b523f2..f8da3819 100644 --- a/i18n/it/Editor.it.resx +++ b/i18n/it/Editor.it.resx @@ -5,4 +5,7 @@ System.Resources.ResXResourceReader, System.Windows.Forms, Version=4.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089 System.Resources.ResXResourceWriter, System.Windows.Forms, Version=4.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089 Scegli una sequenza in Libreria + Questa sequenza non è più nell’archivio. + I lavori non possono partire finché l’indice dell’archivio non è aperto. + Le impostazioni di questa sequenza non sono leggibili. diff --git a/src/Titano.App/Editor/EditorSession.cs b/src/Titano.App/Editor/EditorSession.cs new file mode 100644 index 00000000..4fc40fef --- /dev/null +++ b/src/Titano.App/Editor/EditorSession.cs @@ -0,0 +1,172 @@ +using Titano.Core.Advice; +using Titano.Core.Export; +using Titano.Core.Project; +using Titano.Core.Video; +using Titano.Data.Archiving.Jobs; +using Titano.Data.Processing; + +namespace Titano.App.Editor; + +/// +/// One sequence open in the editor (BRIEF §12.3): its content, the advisor's view of it (§10.3), the project with undo +/// and redo, and the actions of every section — edit, Optimise (§10.4), Reset, exclusions and hero, analysis and render +/// jobs. Every change is saved shortly after it is made (); a change stored meanwhile by +/// someone else (an analysis job, another client) is merged in. Call from one thread (the UI thread). +/// +public sealed class EditorSession : IAsyncDisposable +{ + public static readonly TimeSpan DefaultSaveDelay = TimeSpan.FromMilliseconds(400); + + /// Sections "Optimise all" walks through, in panel order. + public static IReadOnlyList OptimisedSections { get; } = + [AdvisorSection.Frames, AdvisorSection.Timing, AdvisorSection.Exposure, AdvisorSection.Motion, AdvisorSection.Camera, AdvisorSection.Export]; + + private readonly IReadOnlyList _codecs; + private readonly ProjectSaver _saver; + private ProjectSettings _basis; + + public EditorSession(ISequenceSource source, SequenceContent content, IReadOnlyList codecs, TimeSpan? saveDelay = null) + { + ArgumentNullException.ThrowIfNull(source); + ArgumentNullException.ThrowIfNull(content); + Source = source; + _codecs = codecs ?? []; + _basis = content.Project; + History = new ProjectHistory(content.Project); + History.Changed += OnProjectChanged; + _saver = new ProjectSaver(SaveAsync, saveDelay ?? DefaultSaveDelay); + _saver.Failed += ex => SaveFailed?.Invoke(ex); + (Content, Context, Advice, Presets) = Analyse(content, _codecs); + } + + /// The project changed (edit, undo, redo, Optimise, a merge). + public event Action? ProjectChanged; + + /// The sequence was read again (after an analysis): frames, reports, advice and presets. + public event Action? ContentChanged; + + /// Saving the project failed; the next change tries again. + public event Action? SaveFailed; + + public ISequenceSource Source { get; } + + public SequenceContent Content { get; private set; } + + public SequenceContext Context { get; private set; } + + /// The advisor's recommendations and findings for the current analyses. + public AdvisorResult Advice { get; private set; } + + /// Sequence presets that fit the content, best first (BRIEF §10.3). + public IReadOnlyList Presets { get; private set; } + + public ProjectHistory History { get; } + + public ProjectSettings Project => History.Current; + + /// Applies as an undoable step; false when it changes nothing. + /// Consecutive edits with the same key are one undo step (a slider drag). + public bool Edit(Func change, string? mergeKey = null) + { + ArgumentNullException.ThrowIfNull(change); + return History.Apply(change(Project), mergeKey); + } + + public bool Undo() => History.Undo(); + + public bool Redo() => History.Redo(); + + /// "Optimise section": the advisor's heavier decisions for , one undo step; returns what changed. + public IReadOnlyList Optimise(AdvisorSection section) => Optimise([section]); + + /// "Optimise all": every section, one undo step. + public IReadOnlyList OptimiseAll() => Optimise(OptimisedSections); + + /// "Reset": the section's Auto settings back to Auto. + public bool ResetSection(AdvisorSection section) => Edit(p => EditorSettings.ResetSection(p, section)); + + /// Adds frames to, or removes them from, the project's exclusions. + public bool SetExcluded(IEnumerable frames, bool excluded) + { + var chosen = frames.Where(i => (uint)i < (uint)Content.Count).ToHashSet(); + return Edit(p => p with + { + ExcludedFrames = excluded ? [.. p.ExcludedFrames.Union(chosen).Order()] : [.. p.ExcludedFrames.Where(i => !chosen.Contains(i))], + }); + } + + /// Makes the hero frame (sequence thumbnail). + public bool SetHero(int frame) => (uint)frame < (uint)Content.Count && Edit(p => p with { HeroFrame = frame }); + + /// + /// A render of with the project as it is now (the job embeds it, so a render queued before + /// the save lands still renders what the user sees). + /// + public ArchiveJobRequest RenderRequest(ExportSettings export, bool useProxies = false) => + ProcessingJobs.Render(Content.Sequence, export, Content.SourceWidth, Content.SourceHeight, Project with { Export = export }, useProxies); + + /// The export of the project with preset chosen (own values, no batch). + public ExportSettings ExportFor(string presetId) => + Project.Export with { Preset = Project.Export.Preset.WithManual(presetId), Overrides = new ExportOverrides(), Batch = [] }; + + public ArchiveJobRequest AnalysisRequest() => ProcessingJobs.DeepAnalysis([Content.Sequence.Id], Content.Sequence.DisplayTitle); + + public Task SubmitAsync(ArchiveJobRequest request, CancellationToken cancellationToken = default) => Source.SubmitAsync(request, cancellationToken); + + /// Reads the sequence again (an analysis finished): new reports and advice, the stored project merged into the open one. + public async Task ReloadAsync(CancellationToken cancellationToken = default) + { + await _saver.FlushAsync(); + var content = await Source.LoadAsync(cancellationToken); + var merged = ProjectMerge.Merge(_basis, Project, content.Project); + _basis = content.Project; + (Content, Context, Advice, Presets) = Analyse(content, _codecs); + History.Replace(merged); + ContentChanged?.Invoke(); + } + + /// Stores pending changes now. + public Task FlushAsync() => _saver.FlushAsync(); + + public async ValueTask DisposeAsync() + { + History.Changed -= OnProjectChanged; + await _saver.FlushAsync(); + await Source.DisposeAsync(); + } + + private static (SequenceContent, SequenceContext, AdvisorResult, IReadOnlyList) Analyse(SequenceContent content, + IReadOnlyList codecs) + { + var context = SequenceContext.FromFrames(content.Frames, codecs); + return (content, context, Advisor.Recommend(content.Light, content.Deep, context), SequencePresets.Generate(content.Light, content.Deep, context)); + } + + private IReadOnlyList Optimise(IReadOnlyList sections) + { + var settings = Project; + var changes = new List(); + foreach (var section in sections) + { + var result = Advisor.Optimise(section, settings, Advice); + settings = result.Settings; + changes.AddRange(result.Changes); + } + History.Apply(settings); + return changes; + } + + private void OnProjectChanged() + { + _saver.Schedule(Project); + ProjectChanged?.Invoke(); + } + + private async Task SaveAsync(ProjectSettings project) + { + var stored = await Source.SaveProjectAsync(project, _basis, CancellationToken.None); + _basis = stored; + // Someone else's change was merged in: show it without an undo step (edits made since the save are kept). + if (!ProjectMerge.SameValues(stored, project)) History.Replace(ProjectMerge.Merge(project, Project, stored)); + } +} diff --git a/src/Titano.App/Editor/EditorSettings.cs b/src/Titano.App/Editor/EditorSettings.cs new file mode 100644 index 00000000..bfa047bb --- /dev/null +++ b/src/Titano.App/Editor/EditorSettings.cs @@ -0,0 +1,131 @@ +using Titano.Core.Advice; +using Titano.Core.Analysis; +using Titano.Core.Analysis.Deep; +using Titano.Core.Export; +using Titano.Core.Motion; +using Titano.Core.Project; + +namespace Titano.App.Editor; + +/// An advisor-decided setting of the editor, whatever its value type. +public interface IAutoSetting +{ + /// id. + string Id { get; } + + AdvisorSection Section { get; } + + bool IsAuto(ProjectSettings project); + + /// Drops the manual value so the advice applies. + ProjectSettings Reset(ProjectSettings project); +} + +/// +/// One member of the project (BRIEF §10.4) and the value the engine uses when the advisor has +/// none (, from the deep analysis when it measured one). +/// +public sealed class AutoSetting( + string id, + AdvisorSection section, + Func> get, + Func, ProjectSettings> set, + Func fallback) : IAutoSetting + where T : notnull +{ + public string Id { get; } = id; + + public AdvisorSection Section { get; } = section; + + public Auto Get(ProjectSettings project) => get(project); + + public bool IsAuto(ProjectSettings project) => get(project).IsAuto; + + /// The advisor's value, else what the engine uses without advice. + public T AutoValue(ProjectSettings project, DeepAnalysisReport? deep) => get(project) is { HasAdvice: true, AutoValue: { } advised } ? advised : fallback(project, deep); + + /// The value in force. + public T Effective(ProjectSettings project, DeepAnalysisReport? deep) => get(project).Resolve(fallback(project, deep)); + + /// Touching the control: the value becomes manual. + public ProjectSettings WithManual(ProjectSettings project, T value) => set(project, get(project).WithManual(value)); + + public ProjectSettings Reset(ProjectSettings project) => set(project, get(project).Reset()); +} + +/// The Auto settings of the editor's sections, in panel order. +public static class EditorSettings +{ + public static readonly AutoSetting TimingMode = new(AdvisorParameters.TimingMode, AdvisorSection.Timing, + p => p.Timing.Mode, (p, a) => p with { Timing = p.Timing with { Mode = a } }, (_, _) => Core.Project.TimingMode.Constant); + + /// Playback rate; without a value it equals the export frame rate (DECISIONS: export fps vs playback rate). + public static readonly AutoSetting FrameRate = new(AdvisorParameters.FrameRate, AdvisorSection.Timing, + p => p.Timing.FrameRate, (p, a) => p with { Timing = p.Timing with { FrameRate = a } }, + (p, _) => FrameRateText.Format(ExportResolver.Resolve(p.Export).FrameRate)); + + public static readonly AutoSetting Deflicker = new(AdvisorParameters.Deflicker, AdvisorSection.Exposure, + p => p.Exposure.Deflicker, (p, a) => p with { Exposure = p.Exposure with { Deflicker = a } }, (_, _) => false); + + public static readonly AutoSetting DeflickerWindow = new(AdvisorParameters.DeflickerWindow, AdvisorSection.Exposure, + p => p.Exposure.DeflickerWindow, (p, a) => p with { Exposure = p.Exposure with { DeflickerWindow = a } }, + (_, deep) => deep?.Flicker.RecommendedWindow ?? DeflickerOptions.Default.Window); + + public static readonly AutoSetting DeflickerStrength = new(AdvisorParameters.DeflickerStrength, AdvisorSection.Exposure, + p => p.Exposure.DeflickerStrength, (p, a) => p with { Exposure = p.Exposure with { DeflickerStrength = a } }, (_, _) => DeflickerOptions.Default.Strength); + + public static readonly AutoSetting Regions = new(AdvisorParameters.Regions, AdvisorSection.Exposure, + p => p.Exposure.Regions, (p, a) => p with { Exposure = p.Exposure with { Regions = a } }, (_, _) => RegionMode.Off); + + public static readonly AutoSetting HolyGrail = new(AdvisorParameters.HolyGrail, AdvisorSection.Exposure, + p => p.Exposure.HolyGrail, (p, a) => p with { Exposure = p.Exposure with { HolyGrail = a } }, (_, _) => false); + + public static readonly AutoSetting TransitionLength = new(AdvisorParameters.TransitionLength, AdvisorSection.Exposure, + p => p.Exposure.TransitionLength, (p, a) => p with { Exposure = p.Exposure with { TransitionLength = a } }, + (_, deep) => deep?.Exposure.TransitionLength ?? new HolyGrailOptions().SingleStepTransitionFrames); + + public static readonly AutoSetting WhiteBalance = new(AdvisorParameters.WhiteBalanceSmoothing, AdvisorSection.Exposure, + p => p.Exposure.WhiteBalanceSmoothing, (p, a) => p with { Exposure = p.Exposure with { WhiteBalanceSmoothing = a } }, (_, _) => false); + + public static readonly AutoSetting MotionBlur = new(AdvisorParameters.MotionBlur, AdvisorSection.Motion, + p => p.Motion.MotionBlur, (p, a) => p with { Motion = p.Motion with { MotionBlur = a } }, (_, _) => false); + + public static readonly AutoSetting ShutterAngle = new(AdvisorParameters.TargetShutterAngle, AdvisorSection.Motion, + p => p.Motion.TargetShutterAngle, (p, a) => p with { Motion = p.Motion with { TargetShutterAngle = a } }, (_, _) => 180.0); + + public static readonly AutoSetting FlowQuality = new(AdvisorParameters.FlowQuality, AdvisorSection.Motion, + p => p.Motion.FlowQuality, (p, a) => p with { Motion = p.Motion with { FlowQuality = a } }, (_, _) => FlowQualitySetting.Balanced); + + public static readonly AutoSetting Stabilize = new(AdvisorParameters.Stabilize, AdvisorSection.Motion, + p => p.Motion.Stabilize, (p, a) => p with { Motion = p.Motion with { Stabilize = a } }, (_, _) => false); + + public static readonly AutoSetting StabilizeSmoothing = new(AdvisorParameters.StabilizeSmoothing, AdvisorSection.Motion, + p => p.Motion.StabilizeSmoothing, (p, a) => p with { Motion = p.Motion with { StabilizeSmoothing = a } }, (_, _) => new StabilizerOptions().SmoothingSigma); + + public static readonly AutoSetting Aspect = new(AdvisorParameters.Aspect, AdvisorSection.Camera, + p => p.Camera.Aspect, (p, a) => p with { Camera = p.Camera with { Aspect = a } }, (_, _) => "source"); + + public static readonly AutoSetting ExportPreset = new(AdvisorParameters.ExportPreset, AdvisorSection.Export, + p => p.Export.Preset, (p, a) => p with { Export = p.Export with { Preset = a } }, (_, _) => ExportResolver.DefaultPresetId); + + public static IReadOnlyList All { get; } = + [ + TimingMode, FrameRate, Deflicker, DeflickerWindow, DeflickerStrength, Regions, HolyGrail, TransitionLength, WhiteBalance, + MotionBlur, ShutterAngle, FlowQuality, Stabilize, StabilizeSmoothing, Aspect, ExportPreset, + ]; + + /// "Reset": every Auto setting of back to Auto; stacking (no advice) back to off. + public static ProjectSettings ResetSection(ProjectSettings project, AdvisorSection section) + { + ArgumentNullException.ThrowIfNull(project); + foreach (var setting in All) + { + if (setting.Section == section) project = setting.Reset(project); + } + return section == AdvisorSection.Stack ? project with { Stack = new StackSettings() } : project; + } + + /// True when every Auto setting of is in Auto. + public static bool IsSectionAuto(ProjectSettings project, AdvisorSection section) => + All.Where(s => s.Section == section).All(s => s.IsAuto(project)); +} diff --git a/src/Titano.App/Editor/ExportPlanner.cs b/src/Titano.App/Editor/ExportPlanner.cs new file mode 100644 index 00000000..96630024 --- /dev/null +++ b/src/Titano.App/Editor/ExportPlanner.cs @@ -0,0 +1,110 @@ +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) +{ + /// 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."); + } +} diff --git a/src/Titano.App/Editor/ISequenceSource.cs b/src/Titano.App/Editor/ISequenceSource.cs new file mode 100644 index 00000000..1423c73f --- /dev/null +++ b/src/Titano.App/Editor/ISequenceSource.cs @@ -0,0 +1,69 @@ +using Titano.Core.Analysis; +using Titano.Core.Analysis.Deep; +using Titano.Core.Imaging; +using Titano.Core.Model; +using Titano.Core.Project; +using Titano.Data.Archiving.Jobs; + +namespace Titano.App.Editor; + +/// +/// Where the sequence open in the editor lives: the local archive () or a server the +/// client is connected to (a source that reads proxies and posts jobs to the API). The editor reads frames, saves the +/// project and queues jobs only through this interface. +/// +public interface ISequenceSource : IAsyncDisposable +{ + /// Everything the editor shows of the sequence; read off the UI thread. + /// The project cannot be read (corrupt, or written by a newer Titano). + Task LoadAsync(CancellationToken cancellationToken); + + /// + /// Frame in linear light, oriented, long edge at most ; null + /// when the frame cannot be read. The caller owns the buffer. + /// + ValueTask DecodeAsync(int index, int maxLongEdge, DecodePurpose purpose, CancellationToken cancellationToken); + + /// + /// Stores , edited from (the version last loaded or saved). A + /// change made meanwhile by someone else is kept where this edit left the value alone (). + /// Returns the stored project. + /// + Task SaveProjectAsync(ProjectSettings project, ProjectSettings basis, CancellationToken cancellationToken); + + /// Queues an analysis or render job for the sequence; returns the job id. + Task SubmitAsync(ArchiveJobRequest request, CancellationToken cancellationToken); +} + +/// A sequence as the editor opens it. +/// Frame rows in index order. +/// Light analysis of the frame metadata (BRIEF §10.1). +/// The saved deep analysis when it describes the current frames, else null. +/// Dominant oriented full-resolution width (0 when unknown). +/// Default output folder of renders; null when the sequence is not on this PC. +/// Finished renders, newest first. +public sealed record SequenceContent( + Sequence Sequence, + IReadOnlyList Frames, + ProjectSettings Project, + LightAnalysisReport Light, + DeepAnalysisReport? Deep, + int SourceWidth, + int SourceHeight, + string? ExportsFolder, + IReadOnlyList Renders) +{ + /// Flags of frames that never reach a render (as the render job skips them). + public const FrameFlags NotRendered = FrameFlags.Missing | FrameFlags.Corrupt | FrameFlags.Excluded | FrameFlags.Undecodable; + + public int Count => Frames.Count; + + /// Frames the render skips besides the project's exclusions. + public IEnumerable NotRenderable() + { + for (var i = 0; i < Frames.Count; i++) + { + if ((Frames[i].Flags & NotRendered) != 0) yield return i; + } + } +} diff --git a/src/Titano.App/Editor/LatestRunner.cs b/src/Titano.App/Editor/LatestRunner.cs new file mode 100644 index 00000000..32376b43 --- /dev/null +++ b/src/Titano.App/Editor/LatestRunner.cs @@ -0,0 +1,50 @@ +namespace Titano.App.Editor; + +/// +/// Runs only the latest of a stream of requests (preview frames, estimates): scheduling cancels the request pending or +/// running before it, waits the debounce delay, then runs the work on the thread pool. A superseded run ends quietly; +/// other failures surface from the task returns. +/// +public sealed class LatestRunner(TimeSpan delay) : IDisposable +{ + private CancellationTokenSource? _current; + + /// Completes when the latest scheduled run has finished or been superseded. + public Task Idle { get; private set; } = Task.CompletedTask; + + public TimeSpan Delay { get; } = delay; + + /// Skip the debounce delay (playback, the first frame). + public Task Schedule(Func work, bool immediate = false) + { + ArgumentNullException.ThrowIfNull(work); + var cts = new CancellationTokenSource(); + Interlocked.Exchange(ref _current, cts)?.Cancel(); + var run = RunAsync(work, immediate ? TimeSpan.Zero : Delay, cts); + Idle = run; + return run; + } + + /// Cancels the pending or running request. + public void Cancel() => Interlocked.Exchange(ref _current, null)?.Cancel(); + + public void Dispose() => Cancel(); + + private async Task RunAsync(Func work, TimeSpan wait, CancellationTokenSource cts) + { + var token = cts.Token; + try + { + if (wait > TimeSpan.Zero) await Task.Delay(wait, token).ConfigureAwait(false); + await Task.Run(() => work(token), token).ConfigureAwait(false); + } + catch (OperationCanceledException) when (token.IsCancellationRequested) + { + } + finally + { + // Only the run that is still current releases the source; a superseded one was cancelled by its successor. + if (Interlocked.CompareExchange(ref _current, null, cts) == cts) cts.Dispose(); + } + } +} diff --git a/src/Titano.App/Editor/LocalSequenceSource.cs b/src/Titano.App/Editor/LocalSequenceSource.cs new file mode 100644 index 00000000..ddbedea3 --- /dev/null +++ b/src/Titano.App/Editor/LocalSequenceSource.cs @@ -0,0 +1,151 @@ +using System.IO; +using System.Text.Json; +using Titano.Core.Advice; +using Titano.Core.Analysis; +using Titano.Core.Analysis.Deep; +using Titano.Core.Archiving; +using Titano.Core.Diagnostics; +using Titano.Core.Imaging; +using Titano.Core.IO; +using Titano.Core.Localization; +using Titano.Core.Model; +using Titano.Core.Project; +using Titano.Data; +using Titano.Data.Archiving; +using Titano.Data.Archiving.Jobs; +using Titano.Data.Jobs; +using Titano.Data.Repositories; + +namespace Titano.App.Editor; + +/// +/// A sequence of an archive on this PC: rows from the index, originals decoded by the client's decoder chain, the +/// project in .titano/project.json mirrored in the index's settings column, jobs on this PC's engine. Project +/// writes follow the archive engine's protocol: under the sequence's lease file (so the server and the job engine wait +/// for it), file first (atomically), then the index with its settings version as an optimistic check. +/// +public sealed class LocalSequenceSource(TitanoDatabase db, Guid sequenceId, ImageDecoder decoder, JobEngine? engine, ArchiveServiceOptions? lease = null) + : ISequenceSource +{ + private const int PageSize = 1000; + private const int SaveAttempts = 3; + + private readonly ArchiveServiceOptions _lease = lease ?? new ArchiveServiceOptions(); + private string?[] _files = []; + private ArchivePaths? _paths; + private SequencePaths? _folder; + + public async Task LoadAsync(CancellationToken cancellationToken) + { + var sequence = await db.Sequences.GetAsync(sequenceId, cancellationToken) ?? throw Missing(); + var archive = await db.Archives.GetAsync(sequence.ArchiveId, cancellationToken) ?? throw Missing(); + var frames = new List(); + while (true) + { + var page = await db.Frames.ListBySequenceAsync(sequenceId, frames.Count > 0 ? frames[^1].Index : -1, PageSize, cancellationToken); + frames.AddRange(page); + if (page.Count < PageSize) break; + } + var renders = await db.Renders.ListBySequenceAsync(sequenceId, cancellationToken); + return await Task.Run(() => + { + var paths = new ArchivePaths(archive.RootPath); + var folder = paths.Sequence(sequence.Folder); + var files = new string?[frames.Count]; + for (var i = 0; i < files.Length; i++) + { + if ((frames[i].Flags & FrameFlags.Missing) == 0) files[i] = paths.FromRelative(frames[i].RelativePath); + } + (_paths, _folder, _files) = (paths, folder, files); + var context = SequenceContext.FromFrames(frames); + return new SequenceContent(sequence, frames, ReadProject(sequence, folder), LightAnalyzer.Analyze(frames), ReadReport(folder, frames.Count), + context.SourceWidth, context.SourceHeight, folder.Exports, [.. renders.OrderByDescending(r => r.Created)]); + }, cancellationToken); + } + + public async ValueTask DecodeAsync(int index, int maxLongEdge, DecodePurpose purpose, CancellationToken cancellationToken) + { + if ((uint)index >= (uint)_files.Length || _files[index] is not { } file) return null; + return await Task.Run(() => + { + try + { + return decoder.Decode(new DecodeRequest(file, maxLongEdge, purpose), cancellationToken)?.Pixels; + } + catch (Exception ex) when (ex is IOException or UnauthorizedAccessException) + { + Log.Warning("editor", $"Frame {file} could not be read", ex); + return null; + } + }, cancellationToken); + } + + public Task SaveProjectAsync(ProjectSettings project, ProjectSettings basis, CancellationToken cancellationToken) + { + ArgumentNullException.ThrowIfNull(project); + ArgumentNullException.ThrowIfNull(basis); + var (paths, folder) = (_paths, _folder); + if (paths is null || folder is null) throw new InvalidOperationException("LoadAsync first."); + return Task.Run(async () => + { + ArchiveVolume.EnsureWritable(paths); + using var held = await LeaseFile.AcquireAsync(paths.LocksDirectory, $"seq-{sequenceId:N}", _lease.Owner, _lease.LeaseWait, _lease.LeaseDuration, + cancellationToken); + for (var attempt = 1; ; attempt++) + { + var row = await db.Sequences.GetAsync(sequenceId, cancellationToken) ?? throw Missing(); + var stored = ReadProject(row, folder); + var merged = ProjectMerge.SameValues(stored, basis) ? project : ProjectMerge.Merge(basis, project, stored); + var json = merged.ToJson(); + if (json == stored.ToJson()) return merged; + AtomicFile.WriteAllText(folder.ProjectPath, json); + var result = await db.Sequences.EditAsync(sequenceId, new SequenceEdit { SettingsJson = json }, row.SettingsVersion, cancellationToken); + // A conflict means the settings moved in the index meanwhile (server API): merge again on top of them. + if (result.Outcome != SequenceEditOutcome.Conflict || attempt == SaveAttempts) return merged; + } + }, cancellationToken); + } + + public async Task SubmitAsync(ArchiveJobRequest request, CancellationToken cancellationToken) + { + if (engine is null) throw new InvalidOperationException(Loc.L("Editor.Error.NoEngine")); + var job = await engine.EnqueueAsync(request, cancellationToken: cancellationToken); + return job.Id; + } + + public ValueTask DisposeAsync() => ValueTask.CompletedTask; + + /// The settings in force: the index copy, else project.json, else defaults. + /// The settings are not a project or come from a newer Titano. + internal static ProjectSettings ReadProject(Sequence sequence, SequencePaths folder) + { + var json = sequence.SettingsJson ?? (File.Exists(folder.ProjectPath) ? File.ReadAllText(folder.ProjectPath) : null); + if (string.IsNullOrWhiteSpace(json)) return new ProjectSettings(); + try + { + return ProjectSettings.FromJson(json); + } + catch (JsonException ex) + { + throw new InvalidDataException(Loc.L("Editor.Error.Project"), ex); + } + } + + /// The saved deep report when it describes the current frames. + private static DeepAnalysisReport? ReadReport(SequencePaths folder, int frameCount) + { + if (!File.Exists(folder.AnalysisPath)) return null; + try + { + var report = DeepAnalysisReport.Load(folder.AnalysisPath); + return report.FrameCount == frameCount ? report : null; + } + catch (Exception ex) when (ex is IOException or InvalidDataException or JsonException) + { + Log.Warning("editor", $"{folder.AnalysisPath} cannot be read", ex); + return null; + } + } + + private static InvalidOperationException Missing() => new(Loc.L("Editor.Error.Missing")); +} diff --git a/src/Titano.App/Editor/PreviewRenderer.cs b/src/Titano.App/Editor/PreviewRenderer.cs new file mode 100644 index 00000000..cd887cfb --- /dev/null +++ b/src/Titano.App/Editor/PreviewRenderer.cs @@ -0,0 +1,318 @@ +using Titano.Core.Analysis; +using Titano.Core.Export; +using Titano.Core.Imaging; +using Titano.Core.Imaging.Geometry; +using Titano.Core.Motion; +using Titano.Core.Project; +using Titano.Core.Render; + +namespace Titano.App.Editor; + +/// What the preview shows of the frame: the output picture, or the whole source frame with the crop marked. +public enum PreviewFraming +{ + /// Through the render geometry (stabilisation, virtual camera, crop): what the video shows. + Output, + + /// The whole frame, exposure-corrected; marks what the output keeps. + Source, +} + +/// Long edge of the preview picture in pixels. +/// Also measure the motion field to the next frame. +public sealed record PreviewRequest(int Index, ProjectSettings Project, int LongEdge, PreviewFraming Framing, bool Vectors = false); + +/// A motion sample in preview pixels: origin and displacement to the next frame. +public readonly record struct VectorSample(float X, float Y, float Dx, float Dy); + +/// One rendered preview: the corrected and the original picture over the same area, and the overlays. +public sealed class PreviewFrame(int index, ImageBuffer corrected, ImageBuffer original) : IDisposable +{ + public int Index { get; } = index; + + public ImageBuffer Corrected { get; } = corrected; + + public ImageBuffer Original { get; } = original; + + /// The output's part of the picture, normalised (the whole picture in ). + public RectD Crop { get; init; } = new(0, 0, 1, 1); + + /// Sky/land boundary in picture pixels; empty when the analysis found no horizon. + public IReadOnlyList<(double X, double Y)> Horizon { get; init; } = []; + + public IReadOnlyList Vectors { get; init; } = []; + + public void Dispose() + { + Corrected.Dispose(); + Original.Dispose(); + } +} + +/// +/// The corrected-frame preview through the render pipeline at preview size (BRIEF §12.3). Corrections come from +/// exactly as a render prepares them (the deep analysis' luminance reused, the rest +/// measured on small decodes), cached per exposure/stabilisation settings; each frame then goes through the render's +/// exposure stage, its single geometry warp and the highlight knee. The render pipeline has no single-frame entry +/// point, so the per-frame steps of RenderRun are followed here; stacking and motion blur need neighbouring +/// frames and are left to the render. +/// +public sealed class PreviewRenderer(SequenceContent content, ISequenceSource source) +{ + /// Long edge of the frames the preparation measures (the render uses 512; the preview trades precision for speed). + public const int AnalysisLongEdge = 128; + + private const int VectorLongEdge = 320; + private const int VectorSpacing = 24; + + private readonly CachedFrames _frames = new(content, source); + private readonly Lock _gate = new(); + private (string Key, RenderCorrections Corrections)? _latest; + + /// Corrections for when already prepared, else null. + public RenderCorrections? Cached(ProjectSettings project) + { + var key = Key(project); + lock (_gate) return _latest is { } latest && latest.Key == key ? latest.Corrections : null; + } + + /// The most recently prepared corrections, whatever settings they were made for. + public RenderCorrections? Latest + { + get + { + lock (_gate) return _latest?.Corrections; + } + } + + /// + /// Prepares the corrections for (or returns them when prepared already). Requests come + /// one after the other (the editor cancels the previous one), so nothing is shared between calls but the cache. + /// + public async Task PrepareAsync(ProjectSettings project, CancellationToken cancellationToken) + { + if (Cached(project) is { } cached) return cached; + var key = Key(project); + var corrections = await RenderPreparation.PrepareAsync(Input(project), new RenderOptions { AnalysisLongEdge = AnalysisLongEdge }, null, cancellationToken) + .ConfigureAwait(false); + lock (_gate) _latest = (key, corrections); + return corrections; + } + + /// Renders one preview with (identity when null); null when the frame cannot be read. + public async Task RenderAsync(PreviewRequest request, RenderCorrections? corrections, CancellationToken cancellationToken) + { + ArgumentNullException.ThrowIfNull(request); + var setup = Geometry(request, corrections); + if (setup is null) return null; + var (geometry, edge) = setup.Value; + var frame = await PictureAsync(request.Index, request, corrections, geometry, edge, cancellationToken).ConfigureAwait(false); + if (frame is null) return null; + var (corrected, original, plan) = frame.Value; + try + { + var vectors = request.Vectors && request.Index + 1 < content.Count + ? await VectorsAsync(request, corrections, geometry, edge, corrected, cancellationToken).ConfigureAwait(false) + : []; + return new PreviewFrame(request.Index, corrected, original) + { + Crop = request.Framing == PreviewFraming.Source ? CropOf(plan, corrected.Width, corrected.Height) : new RectD(0, 0, 1, 1), + Horizon = request.Framing == PreviewFraming.Source ? HorizonOf(corrected.Width, corrected.Height) : [], + Vectors = vectors, + }; + } + catch + { + corrected.Dispose(); + original.Dispose(); + throw; + } + } + + /// Cache key: the settings reads. + internal static string Key(ProjectSettings project) => new ProjectSettings + { + Exposure = project.Exposure, + Motion = new MotionSettings { Stabilize = project.Motion.Stabilize, StabilizeSmoothing = project.Motion.StabilizeSmoothing }, + ExcludedFrames = project.ExcludedFrames, + }.ToJson(); + + private RenderInput Input(ProjectSettings project) => new() + { + Source = _frames, + Frames = SourceFrameInfo.From(content.Frames, content.Light.AdjustedTimes.Length == content.Count ? content.Light.AdjustedTimes : null), + Project = project with { ExcludedFrames = [.. project.ExcludedFrames.Union(content.NotRenderable()).Order()] }, + Analysis = content.Deep is { } deep && deep.FrameCount == content.Count + ? new PrecomputedAnalysis { LogLuminance = [.. deep.Frames.LogLuminance.Select(v => v ?? double.NaN)] } + : null, + }; + + private (RenderGeometry Geometry, int DecodeEdge)? Geometry(PreviewRequest request, RenderCorrections? corrections) + { + if ((uint)request.Index >= (uint)content.Count || content.SourceWidth <= 0 || content.SourceHeight <= 0) return null; + var export = ExportResolver.Resolve(request.Project.Export); + var full = RenderGeometry.Create(request.Project.Camera, export, content.SourceWidth, content.SourceHeight, corrections?.StabilizationZoom ?? 1, + request.LongEdge); + var (width, height) = Resampler.FitWithin(full.OutputWidth, full.OutputHeight, request.LongEdge); + var geometry = full.WithOutputSize(int.Max(1, width), int.Max(1, height)); + var edge = request.Framing == PreviewFraming.Source ? request.LongEdge : geometry.WorkingLongEdge; + return (geometry, int.Max(16, edge)); + } + + /// Decodes, corrects and frames one source frame: (corrected, original, output→picture plan). + private async Task<(ImageBuffer, ImageBuffer, GeometryPlan?)?> PictureAsync(int index, PreviewRequest request, RenderCorrections? corrections, + RenderGeometry geometry, int edge, CancellationToken cancellationToken) + { + var decoded = await source.DecodeAsync(index, edge, DecodePurpose.Proxy, cancellationToken).ConfigureAwait(false); + if (decoded is null) return null; + var raw = Rgb(decoded); + ImageBuffer? corrected = null; + try + { + corrected = raw.Clone(); + if (corrections is not null && corrections.FrameCount == content.Count) ApplyExposure(corrected, index, corrections); + var settings = geometry.For(raw.Width, raw.Height, index, corrections?.StabilizationAt(index, raw.Width, raw.Height) ?? Affine2D.Identity); + var plan = GeometryStage.Plan(raw.Width, raw.Height, settings); + if (request.Framing == PreviewFraming.Output) + { + var framed = GeometryStage.Apply(corrected, settings, cancellationToken); + corrected.Dispose(); + corrected = framed; + framed = GeometryStage.Apply(raw, settings, cancellationToken); + raw.Dispose(); + raw = framed; + } + if (request.Project.Exposure.HighlightKnee is > 0 and < 1 and var knee) ExposureProcessor.Apply(corrected, 0, null, knee); + return (corrected, raw, request.Framing == PreviewFraming.Output ? null : plan); + } + catch + { + corrected?.Dispose(); + raw.Dispose(); + throw; + } + } + + /// The render's exposure stage: region curves through the resampled sky mask, else the global correction, with white-balance gains. + private static void ApplyExposure(ImageBuffer frame, int index, RenderCorrections corrections) + { + var gains = corrections.WhiteBalance?[index]; + if (corrections is { Regions: { } mask, SkyEv: { } sky, LandEv: { } land }) + { + ExposureProcessor.ApplyRegion(frame, mask.Resample(frame.Width, frame.Height).Sky, sky[index], land[index], gains); + return; + } + var ev = corrections.ExposureEv[index]; + if (ev != 0 || (gains is { } g && g != ChannelGains.Identity)) ExposureProcessor.Apply(frame, ev, gains); + } + + /// Optical flow from this frame to the next, both prepared the same way, sampled on a grid in picture pixels. + private async Task> VectorsAsync(PreviewRequest request, RenderCorrections? corrections, RenderGeometry geometry, int edge, + ImageBuffer current, CancellationToken cancellationToken) + { + var next = await PictureAsync(request.Index + 1, request, corrections, geometry, edge, cancellationToken).ConfigureAwait(false); + if (next is null) return []; + var (nextCorrected, nextOriginal, _) = next.Value; + nextOriginal.Dispose(); + using (nextCorrected) + { + var scale = double.Min(1, (double)VectorLongEdge / int.Max(current.Width, current.Height)); + var (w, h) = ((int)double.Max(8, current.Width * scale), (int)double.Max(8, current.Height * scale)); + using var a = Resampler.Resize(current, w, h); + using var b = Resampler.Resize(nextCorrected, w, h); + using var engine = new OpticalFlowEngine(FlowQuality.Draft); + using var field = engine.Compute(a, b, cancellationToken); + var samples = new List(); + for (var y = VectorSpacing / 2.0; y < current.Height; y += VectorSpacing) + { + for (var x = VectorSpacing / 2.0; x < current.Width; x += VectorSpacing) + { + var (u, v) = field.Sample(x * w / current.Width, y * h / current.Height); + samples.Add(new VectorSample((float)x, (float)y, (float)(u * current.Width / w), (float)(v * current.Height / h))); + } + } + return samples; + } + } + + /// The output's footprint on the source picture (bounding box of the warped output corners), normalised. + private static RectD CropOf(GeometryPlan? plan, int width, int height) + { + if (plan is not { } p) return new RectD(0, 0, 1, 1); + double left = double.MaxValue, top = double.MaxValue, right = double.MinValue, bottom = double.MinValue; + foreach (var (u, v) in new[] { (-0.5, -0.5), (p.OutputWidth - 0.5, -0.5), (-0.5, p.OutputHeight - 0.5), (p.OutputWidth - 0.5, p.OutputHeight - 0.5) }) + { + var (x, y) = p.OutputToSource.Apply(u, v); + (left, top, right, bottom) = (double.Min(left, x + 0.5), double.Min(top, y + 0.5), double.Max(right, x + 0.5), double.Max(bottom, y + 0.5)); + } + left = double.Clamp(left / width, 0, 1); + top = double.Clamp(top / height, 0, 1); + return new RectD(left, top, double.Clamp(right / width, 0, 1) - left, double.Clamp(bottom / height, 0, 1) - top); + } + + /// The analysed horizon (normalised rows at evenly spaced columns) in pixels of the whole-frame picture. + private IReadOnlyList<(double X, double Y)> HorizonOf(int width, int height) + { + if (content.Deep?.Regions is not { Horizon: { Length: > 1 } rows }) return []; + var points = new (double X, double Y)[rows.Length]; + for (var k = 0; k < rows.Length; k++) points[k] = ((double)k / (rows.Length - 1) * width, rows[k] * height); + return points; + } + + /// Gray sources become RGB and alpha is dropped, as in the render. + private static ImageBuffer Rgb(ImageBuffer frame) + { + if (frame.Channels == 3) return frame; + using (frame) + { + if (frame.Channels == 1) return PixelConvert.GrayToRgb(frame); + var rgb = new ImageBuffer(frame.Width, frame.Height, 3, clear: false); + frame.Data[..(3 * frame.PlaneLength)].CopyTo(rgb.Data); + return rgb; + } + } + + /// + /// The sequence as the preparation reads it, with small decodes kept (within a memory budget) so changing a setting + /// does not decode the sequence again. + /// + private sealed class CachedFrames(SequenceContent content, ISequenceSource source) : IRenderFrameSource + { + private const long Budget = 64L << 20; + private readonly Lock _gate = new(); + private readonly Dictionary<(int Index, int Edge), ImageBuffer> _cache = []; + private readonly Queue<(int Index, int Edge)> _order = new(); + private long _bytes; + + public int FrameCount => content.Count; + + public int Width => content.SourceWidth; + + public int Height => content.SourceHeight; + + public async ValueTask GetFrameAsync(int index, int maxLongEdge, DecodePurpose purpose, CancellationToken cancellationToken) + { + lock (_gate) + { + if (_cache.TryGetValue((index, maxLongEdge), out var hit)) return hit.Clone(); + } + var frame = await source.DecodeAsync(index, maxLongEdge, purpose, cancellationToken).ConfigureAwait(false) + ?? throw new RenderException("Export.Render.FrameUnreadable", $"Frame {index + 1} could not be decoded.", + [(index + 1).ToString(System.Globalization.CultureInfo.InvariantCulture), content.Frames[index].FileName]); + lock (_gate) + { + if (frame.ByteSize <= Budget && _cache.TryAdd((index, maxLongEdge), frame.Clone())) + { + _order.Enqueue((index, maxLongEdge)); + _bytes += frame.ByteSize; + while (_bytes > Budget && _order.TryDequeue(out var oldest) && _cache.Remove(oldest, out var dropped)) + { + _bytes -= dropped.ByteSize; + dropped.Dispose(); + } + } + } + return frame; + } + } +} diff --git a/src/Titano.App/Editor/ProjectHistory.cs b/src/Titano.App/Editor/ProjectHistory.cs new file mode 100644 index 00000000..55c75df5 --- /dev/null +++ b/src/Titano.App/Editor/ProjectHistory.cs @@ -0,0 +1,76 @@ +using Titano.Core.Project; + +namespace Titano.App.Editor; + +/// +/// Undo and redo of the editor's project (Ctrl+Z / Ctrl+Y). Projects are immutable, so each step is a snapshot. +/// Consecutive edits with the same mergeKey (one slider drag, typing in one field) form a single step; any +/// other edit, undo or redo ends the run. The oldest steps are dropped beyond . +/// +public sealed class ProjectHistory(ProjectSettings initial, int capacity = 200) +{ + private readonly LinkedList _undo = new(); + private readonly Stack _redo = new(); + private string? _runKey; + + /// The project, after any edit, undo, redo or . + public event Action? Changed; + + public ProjectSettings Current { get; private set; } = initial ?? throw new ArgumentNullException(nameof(initial)); + + public int Capacity { get; } = capacity > 0 ? capacity : throw new ArgumentOutOfRangeException(nameof(capacity)); + + public bool CanUndo => _undo.Count > 0; + + public bool CanRedo => _redo.Count > 0; + + /// Makes current as an undoable step; false (and nothing recorded) when it stores the same values. + public bool Apply(ProjectSettings next, string? mergeKey = null) + { + ArgumentNullException.ThrowIfNull(next); + if (ProjectMerge.SameValues(next, Current)) return false; + if (mergeKey is null || mergeKey != _runKey || _undo.Count == 0) + { + _undo.AddLast(Current); + if (_undo.Count > Capacity) _undo.RemoveFirst(); + } + _runKey = mergeKey; + _redo.Clear(); + Current = next; + Changed?.Invoke(); + return true; + } + + public bool Undo() + { + if (_undo.Last is not { } previous) return false; + _undo.RemoveLast(); + _redo.Push(Current); + return Move(previous.Value); + } + + public bool Redo() + { + if (!_redo.TryPop(out var next)) return false; + _undo.AddLast(Current); + return Move(next); + } + + /// Adopts a project changed outside the editor (analysis advice, a merge on save) without an undo step. + public void Replace(ProjectSettings current) + { + ArgumentNullException.ThrowIfNull(current); + if (ProjectMerge.SameValues(current, Current)) return; + _runKey = null; + Current = current; + Changed?.Invoke(); + } + + private bool Move(ProjectSettings target) + { + _runKey = null; + Current = target; + Changed?.Invoke(); + return true; + } +} diff --git a/src/Titano.App/Editor/ProjectMerge.cs b/src/Titano.App/Editor/ProjectMerge.cs new file mode 100644 index 00000000..720726a9 --- /dev/null +++ b/src/Titano.App/Editor/ProjectMerge.cs @@ -0,0 +1,42 @@ +using System.Text.Json.Nodes; +using Titano.Core.Project; + +namespace Titano.App.Editor; + +/// +/// Three-way merge of projects: the editor's edit (ours) and a change stored meanwhile by someone else +/// (theirs: an analysis filling Auto values, another client through the server), both made from basis. +/// Values are compared member by member down the JSON tree; a member only one side changed takes that side's value, a +/// member both changed takes the editor's (the user is looking at it). +/// +public static class ProjectMerge +{ + public static ProjectSettings Merge(ProjectSettings basis, ProjectSettings ours, ProjectSettings theirs) + { + ArgumentNullException.ThrowIfNull(basis); + ArgumentNullException.ThrowIfNull(ours); + ArgumentNullException.ThrowIfNull(theirs); + var b = JsonNode.Parse(basis.ToJson()); + var o = JsonNode.Parse(ours.ToJson()); + var t = JsonNode.Parse(theirs.ToJson()); + return ProjectSettings.FromJson(Merge(b, o, t)?.ToJsonString() ?? "{}"); + } + + /// True when both projects store the same values. + public static bool SameValues(ProjectSettings a, ProjectSettings b) => a.ToJson() == b.ToJson(); + + private static JsonNode? Merge(JsonNode? basis, JsonNode? ours, JsonNode? theirs) + { + if (JsonNode.DeepEquals(ours, basis)) return theirs?.DeepClone(); + if (JsonNode.DeepEquals(theirs, basis) || JsonNode.DeepEquals(theirs, ours)) return ours?.DeepClone(); + if (ours is not JsonObject mine || theirs is not JsonObject other) return ours?.DeepClone(); + var common = basis as JsonObject; + var merged = new JsonObject(); + foreach (var name in mine.Select(p => p.Key).Union(other.Select(p => p.Key))) + { + var value = Merge(common?[name], mine[name], other[name]); + if (value is not null || mine.ContainsKey(name)) merged[name] = value; + } + return merged; + } +} diff --git a/src/Titano.App/Editor/ProjectSaver.cs b/src/Titano.App/Editor/ProjectSaver.cs new file mode 100644 index 00000000..c8b254a6 --- /dev/null +++ b/src/Titano.App/Editor/ProjectSaver.cs @@ -0,0 +1,89 @@ +using Titano.Core.Project; + +namespace Titano.App.Editor; + +/// +/// Writes the editor's project shortly after the last change: one write at a time, always the newest project, never +/// cancelled half way (a write is a file plus an index row). skips the wait and returns once +/// everything scheduled is stored. A failed write is reported and the next change tries again. +/// +public sealed class ProjectSaver(Func save, TimeSpan delay) +{ + private readonly Lock _gate = new(); + private ProjectSettings? _pending; + private bool _running; + private Task _loop = Task.CompletedTask; + private CancellationTokenSource _wait = new(); + + /// A write failed (raised on the context that scheduled the write). + public event Action? Failed; + + public bool HasPendingChanges + { + get + { + lock (_gate) return _running; + } + } + + public void Schedule(ProjectSettings project) + { + ArgumentNullException.ThrowIfNull(project); + TaskCompletionSource start; + lock (_gate) + { + _pending = project; + if (_running) return; + _running = true; + if (!_wait.TryReset()) _wait = new CancellationTokenSource(); + start = new TaskCompletionSource(); + _loop = LoopAsync(start.Task, _wait.Token); + } + // The loop starts outside the lock: with nothing to wait for it would otherwise save while holding it. + start.SetResult(); + } + + /// Stores what is pending now, without the delay. + public Task FlushAsync() + { + lock (_gate) + { + _wait.Cancel(); + return _loop; + } + } + + private async Task LoopAsync(Task started, CancellationToken wait) + { + await started; + try + { + await Task.Delay(delay, wait); + } + catch (OperationCanceledException) + { + } + while (true) + { + ProjectSettings next; + lock (_gate) + { + if (_pending is null) + { + _running = false; + return; + } + next = _pending; + _pending = null; + } + try + { + await save(next); + } + catch (Exception ex) + { + Failed?.Invoke(ex); + } + } + } +} diff --git a/tests/Titano.App.Tests/Editor/EditorFixture.cs b/tests/Titano.App.Tests/Editor/EditorFixture.cs new file mode 100644 index 00000000..1e06fdd8 --- /dev/null +++ b/tests/Titano.App.Tests/Editor/EditorFixture.cs @@ -0,0 +1,132 @@ +using System.Globalization; +using Titano.App.Editor; +using Titano.Core.Analysis; +using Titano.Core.Analysis.Deep; +using Titano.Core.Diagnostics.Synthetic; +using Titano.Core.Imaging; +using Titano.Core.Localization; +using Titano.Core.Model; +using Titano.Core.Project; +using Titano.Core.Render.Diagnostics; +using Titano.Data.Archiving.Jobs; + +namespace Titano.App.Tests.Editor; + +/// A synthetic sunset with flicker and two exposure steps, as the editor tests open it. +internal static class EditorFixture +{ + private static readonly Lazy Shared = new(() => Sequence()); + + static EditorFixture() => Loc.SetCulture(CultureInfo.GetCultureInfo("en")); + + public static SyntheticSequence Sunset => Shared.Value; + + public static SyntheticSequence Sequence(int frames = 72) => SyntheticSequence.Create(new SyntheticSequenceOptions + { + Width = 96, + Height = 64, + FrameCount = frames, + StartEv = 0, + EndEv = -2, + FlickerEv = 0.25, + ExposureSteps = [new SyntheticExposureStep(24, -1), new SyntheticExposureStep(48, -1)], + StartKelvin = 5200, + EndKelvin = 3400, + }); + + /// The sequence as returns it, with a deep analysis when asked. + public static SequenceContent Content(SyntheticSequence sequence, ProjectSettings? project = null, bool deep = true) + { + var id = Guid.CreateVersion7(); + var frames = sequence.Frames.Select(f => f with { SequenceId = id }).ToList(); + var row = new Sequence + { + Id = id, + ArchiveId = Guid.CreateVersion7(), + Title = "Harbour sunset", + Folder = "2026/2026-09-28 Synthetic", + CameraMake = sequence.Options.CameraMake, + CameraModel = sequence.Options.CameraModel, + FirstCapture = frames[0].CapturedAt, + LastCapture = frames[^1].CapturedAt, + FrameCount = frames.Count, + MedianInterval = sequence.Options.IntervalSeconds, + }; + var report = deep ? DeepAnalyzer.AnalyzeAsync(new Frames(sequence)).GetAwaiter().GetResult() : null; + return new SequenceContent(row, frames, project ?? new ProjectSettings(), LightAnalyzer.Analyze(frames), report, sequence.Options.Width, + sequence.Options.Height, null, []); + } + + /// Synthetic frames as the deep analysis reads them. + private sealed class Frames(SyntheticSequence sequence) : IFrameSource + { + public int Count => sequence.Count; + + public LightFrame Metadata(int index) => LightFrame.From(sequence.Metadata[index], index); + + public ValueTask LoadAsync(int index, int maxLongEdge, CancellationToken cancellationToken) => + ValueTask.FromResult(sequence.Render(index)); + } +} + +/// +/// An in-memory : synthetic pixels, a stored project with the same merge rule as the +/// archive, recorded jobs. simulates someone else changing the stored project before a save. +/// +internal sealed class FakeSequenceSource(SyntheticSequence sequence, SequenceContent content) : ISequenceSource +{ + private readonly SyntheticFrameSource _pixels = new(sequence); + + public ProjectSettings Stored { get; set; } = content.Project; + + public SequenceContent Content { get; set; } = content; + + public List Submitted { get; } = []; + + public int Saves { get; private set; } + + public int Decodes { get; private set; } + + /// Applied to the stored project before the next save reads it. + public Func? ExternalEdit { get; set; } + + /// Thrown by the next save. + public Exception? FailNextSave { get; set; } + + /// Awaited by every decode (tests of cancellation). + public Task DecodeGate { get; set; } = Task.CompletedTask; + + public Task LoadAsync(CancellationToken cancellationToken) => Task.FromResult(Content with { Project = Stored }); + + public async ValueTask DecodeAsync(int index, int maxLongEdge, DecodePurpose purpose, CancellationToken cancellationToken) + { + await DecodeGate.WaitAsync(cancellationToken); + Decodes++; + return await _pixels.GetFrameAsync(index, maxLongEdge, purpose, cancellationToken); + } + + public Task SaveProjectAsync(ProjectSettings project, ProjectSettings basis, CancellationToken cancellationToken) + { + if (FailNextSave is { } failure) + { + FailNextSave = null; + return Task.FromException(failure); + } + if (ExternalEdit is { } edit) + { + Stored = edit(Stored); + ExternalEdit = null; + } + Stored = ProjectMerge.SameValues(Stored, basis) ? project : ProjectMerge.Merge(basis, project, Stored); + Saves++; + return Task.FromResult(Stored); + } + + public Task SubmitAsync(ArchiveJobRequest request, CancellationToken cancellationToken) + { + Submitted.Add(request); + return Task.FromResult(Guid.CreateVersion7()); + } + + public ValueTask DisposeAsync() => ValueTask.CompletedTask; +} diff --git a/tests/Titano.App.Tests/Editor/EditorSessionTests.cs b/tests/Titano.App.Tests/Editor/EditorSessionTests.cs new file mode 100644 index 00000000..055251d4 --- /dev/null +++ b/tests/Titano.App.Tests/Editor/EditorSessionTests.cs @@ -0,0 +1,206 @@ +using System.IO; +using System.Text.Json; +using Titano.App.Editor; +using Titano.Core.Advice; +using Titano.Core.Model; +using Titano.Core.Project; +using Titano.Protocol; +using Titano.Testing; + +namespace Titano.App.Tests.Editor; + +public sealed class EditorSessionTests +{ + private static (EditorSession Session, FakeSequenceSource Source) Open(ProjectSettings? project = null, bool deep = true) + { + var content = EditorFixture.Content(EditorFixture.Sunset, project, deep); + var source = new FakeSequenceSource(EditorFixture.Sunset, content); + return (new EditorSession(source, content, [], TimeSpan.FromMilliseconds(20)), source); + } + + [Test] + public async Task EditsAreSavedAndUndoable() + { + var (session, source) = Open(); + await using (session) + { + Assert.True(session.Edit(p => EditorSettings.Deflicker.WithManual(p, true))); + await session.FlushAsync(); + Assert.True(source.Stored.Exposure.Deflicker is { IsAuto: false, Manual: true }); + + Assert.True(session.Undo()); + await session.FlushAsync(); + Assert.True(source.Stored.Exposure.Deflicker.IsAuto, "undo is saved too"); + Assert.True(session.Redo()); + await session.FlushAsync(); + Assert.False(source.Stored.Exposure.Deflicker.IsAuto); + } + } + + [Test] + public async Task RapidEditsSaveOnce() + { + var (session, source) = Open(); + await using (session) + { + for (var i = 1; i <= 20; i++) session.Edit(p => p with { Exposure = p.Exposure with { ExposureOffsetEv = i / 20.0 } }, "offset"); + await session.FlushAsync(); + Assert.Equal(1, source.Saves); + Assert.Equal(1.0, source.Stored.Exposure.ExposureOffsetEv); + } + } + + [Test] + public async Task AChangeStoredMeanwhileIsMergedIntoTheEditor() + { + var (session, source) = Open(); + await using (session) + { + source.ExternalEdit = p => p with { Camera = p.Camera with { OffsetY = -0.2 } }; + session.Edit(p => p with { HeroFrame = 5 }); + await session.FlushAsync(); + Assert.Equal(5, session.Project.HeroFrame); + Assert.Equal(-0.2, session.Project.Camera.OffsetY, "their change shows up in the editor"); + Assert.True(session.Undo()); + Assert.Null(session.Project.HeroFrame); + } + } + + [Test] + public async Task AFailedSaveIsReportedAndRetriedByTheNextChange() + { + var (session, source) = Open(); + await using (session) + { + Exception? reported = null; + session.SaveFailed += ex => reported = ex; + source.FailNextSave = new IOException("share offline"); + session.Edit(p => p with { HeroFrame = 1 }); + await session.FlushAsync(); + Assert.NotNull(reported); + session.Edit(p => p with { HeroFrame = 2 }); + await session.FlushAsync(); + Assert.Equal(2, source.Stored.HeroFrame); + } + } + + [Test] + public async Task OptimiseSectionTakesTheHeavierDecisionsAsOneStep() + { + // The user turned deflicker off by hand; the analysis measured flicker, so Optimise returns it to Auto (on). + var start = new ProjectSettings(); + start = start with { Exposure = start.Exposure with { Deflicker = start.Exposure.Deflicker.WithManual(false) } }; + var (session, _) = Open(start); + await using (session) + { + var advice = session.Advice.Find(AdvisorParameters.Deflicker); + Assert.NotNull(advice); + Assert.Equal(true, advice!.Value); + session.Edit(p => Advisor.Apply(p, session.Advice)); + Assert.False(EditorSettings.Deflicker.Effective(session.Project, null), "advice never overrides a manual value"); + + var changes = session.Optimise(AdvisorSection.Exposure); + Assert.True(EditorSettings.Deflicker.Effective(session.Project, null)); + Assert.True(session.Project.Exposure.Deflicker.IsAuto); + var change = Assert.Contains(changes, c => c.Parameter == AdvisorParameters.Deflicker); + Assert.Equal(false, change.Before); + Assert.Equal(true, change.After); + + Assert.True(session.Undo()); + Assert.False(session.Project.Exposure.Deflicker.IsAuto, "one undo restores the manual choice"); + } + } + + [Test] + public async Task WithoutAnalysisOptimiseChangesNothingItCannotMeasure() + { + var (session, _) = Open(deep: false); + await using (session) + { + Assert.True(session.Advice.Find(AdvisorParameters.Deflicker)!.IsMissingData); + var changes = session.Optimise(AdvisorSection.Exposure); + Assert.DoesNotContain(AdvisorParameters.Deflicker, changes.Select(c => c.Parameter)); + Assert.False(session.Project.Exposure.Deflicker.HasAdvice); + } + } + + [Test] + public async Task ResetReturnsTheSectionToAuto() + { + var (session, _) = Open(); + await using (session) + { + session.Edit(p => EditorSettings.Stabilize.WithManual(p, true)); + session.Edit(p => EditorSettings.ShutterAngle.WithManual(p, 90.0)); + session.Edit(p => EditorSettings.Deflicker.WithManual(p, true)); + Assert.False(EditorSettings.IsSectionAuto(session.Project, AdvisorSection.Motion)); + Assert.True(session.ResetSection(AdvisorSection.Motion)); + Assert.True(EditorSettings.IsSectionAuto(session.Project, AdvisorSection.Motion)); + Assert.False(session.Project.Exposure.Deflicker.IsAuto, "other sections keep their values"); + } + } + + [Test] + public async Task ExclusionsAndHeroAreFrameIndices() + { + var (session, _) = Open(); + await using (session) + { + Assert.True(session.SetExcluded([5, 3, 400], excluded: true)); + Assert.SequenceEqual([3, 5], session.Project.ExcludedFrames); + Assert.True(session.SetExcluded([3], excluded: false)); + Assert.SequenceEqual([5], session.Project.ExcludedFrames); + Assert.False(session.SetHero(999)); + Assert.True(session.SetHero(10)); + Assert.Equal(10, session.Project.HeroFrame); + } + } + + [Test] + public async Task RenderRequestsEmbedTheProjectAndTheExport() + { + var (session, source) = Open(); + await using (session) + { + session.Edit(p => p with { Exposure = p.Exposure with { ExposureOffsetEv = 0.7 } }); + var export = session.ExportFor("4k-hevc"); + Assert.Empty(export.Batch); + var request = session.RenderRequest(export with { Batch = ["1080p-h264"] }); + Assert.Equal(JobKind.Render, request.Kind); + Assert.SequenceEqual([session.Content.Sequence.Id], request.Targets); + Assert.Equal(JobPriority.User, request.Priority); + var parameters = JsonSerializer.Deserialize(request.ParametersJson!, ProtocolJson.Default.RenderJobParameters)!; + Assert.Equal("hevc", parameters.Codec); + Assert.Equal(3840, parameters.Width); + Assert.Contains("1080p-h264", parameters.PresetJson); + var embedded = ProjectSettings.FromJson(parameters.SettingsJson!); + Assert.Equal(0.7, embedded.Exposure.ExposureOffsetEv); + Assert.Equal("4k-hevc", embedded.Export.Preset.Manual); + + await session.SubmitAsync(request); + await session.SubmitAsync(session.AnalysisRequest()); + Assert.Equal(2, source.Submitted.Count); + Assert.Equal(JobKind.DeepAnalyse, source.Submitted[1].Kind); + } + } + + [Test] + public async Task ReloadBringsNewAnalysisAndKeepsUserValues() + { + var (session, source) = Open(deep: false); + await using (session) + { + Assert.Null(session.Content.Deep); + session.Edit(p => p with { HeroFrame = 2 }); + source.Content = EditorFixture.Content(EditorFixture.Sunset, deep: true) with { Sequence = session.Content.Sequence }; + source.Stored = Advisor.Apply(source.Stored, session.Advice); + var changed = 0; + session.ContentChanged += () => changed++; + await session.ReloadAsync(); + Assert.Equal(1, changed); + Assert.NotNull(session.Content.Deep); + Assert.False(session.Advice.Find(AdvisorParameters.Deflicker)!.IsMissingData, "advice from the new analysis"); + Assert.Equal(2, session.Project.HeroFrame); + } + } +} diff --git a/tests/Titano.App.Tests/Editor/ExportPlannerTests.cs b/tests/Titano.App.Tests/Editor/ExportPlannerTests.cs new file mode 100644 index 00000000..fa4fb3d4 --- /dev/null +++ b/tests/Titano.App.Tests/Editor/ExportPlannerTests.cs @@ -0,0 +1,93 @@ +using Titano.App.Editor; +using Titano.Core.Advice; +using Titano.Core.Export; +using Titano.Core.Project; +using Titano.Core.Video; +using Titano.Core.Video.Managed; +using Titano.Testing; + +namespace Titano.App.Tests.Editor; + +public sealed class ExportPlannerTests +{ + private static readonly SequenceContent Content = EditorFixture.Content(EditorFixture.Sunset); + + private static ExportPlanner Planner(EncoderHost host = EncoderHost.Client) => new(Content, [new ManagedEncoderProvider()], host); + + [Test] + public void TheListHasSequencePresetsFirstThenEveryBuiltInPreset() + { + var context = SequenceContext.FromFrames(Content.Frames); + var advice = Advisor.Recommend(Content.Light, Content.Deep, context); + var presets = SequencePresets.Generate(Content.Light, Content.Deep, context); + var choices = ExportPlanner.Choices(presets, advice); + + Assert.Equal(presets.Count + ExportPresets.BuiltIn.Count, choices.Count); + Assert.True(choices.Take(presets.Count).All(c => c.Sequence is not null)); + Assert.SequenceEqual(ExportPresets.BuiltIn.Select(p => p.Id), choices.Skip(presets.Count).Select(c => c.Id)); + var recommended = Assert.Contains(choices, c => c.IsRecommended); + Assert.Equal(advice.Find(AdvisorParameters.ExportPreset)!.Value, recommended.ExportPresetId); + Assert.NotNull(recommended.Reason); + Assert.True(choices.All(c => c.Name.Length > 0 && !c.Name.StartsWith("Export.", StringComparison.Ordinal)), "names are localised"); + } + + [Test] + public void AnImageSequenceIsEstimatedAndNamedAsAFolder() + { + var project = new ProjectSettings(); + project = project with { Export = project.Export with { Preset = project.Export.Preset.WithManual("png-sequence"), NameTemplate = "{title} {width}x{height} {codec}" } }; + var plan = Planner().Plan(project); + Assert.NotNull(plan); + Assert.True(plan!.Setup.Encoder.IsReady); + Assert.Equal(96, plan.Setup.Encoder.Settings.Width); + Assert.Equal("Harbour sunset 96x64 PNG", plan.FileName, "no extension: a folder of frames"); + Assert.Null(plan.NameError); + var workload = plan.Setup.Workload(); + Assert.Equal(ExportEstimator.EstimateBytes(plan.Export with { Codec = VideoCodec.PngSequence }, workload), plan.Estimate.Bytes); + Assert.Greater(plan.Estimate.Bytes, 0); + Assert.Greater(plan.Estimate.Time.TotalSeconds, 0); + } + + [Test] + public void ExcludedFramesShortenTheEstimate() + { + var project = new ProjectSettings(); + project = project with { Export = project.Export with { Preset = project.Export.Preset.WithManual("jpeg-sequence") } }; + var all = Planner().Plan(project)!; + var fewer = Planner().Plan(project with { ExcludedFrames = [.. Enumerable.Range(0, 36)] })!; + Assert.Equal(Content.Count, all.Setup.Plan.Frames.Count); + Assert.Less(fewer.Estimate.Bytes, all.Estimate.Bytes); + } + + [Test] + public void VideoWithoutAnEncoderExplainsWhy() + { + var plan = Planner().Plan(new ProjectSettings())!; + Assert.False(plan.Setup.Encoder.IsReady, "only managed encoders here; H.264 needs Media Foundation or a render node"); + Assert.Contains(plan.Setup.Encoder.Notices, n => n.Key == EncoderNotices.NoEncoder); + + var server = Planner(EncoderHost.Server).Plan(new ProjectSettings())!; + Assert.Equal(EncoderSelectionStatus.NeedsRenderNode, server.Setup.Encoder.Status); + } + + [Test] + public void ABrokenTemplateIsExplainedAndStillNamesTheFile() + { + var project = new ProjectSettings(); + project = project with { Export = project.Export with { Preset = project.Export.Preset.WithManual("gif-preview"), NameTemplate = "{title} {nope}" } }; + var plan = Planner().Plan(project)!; + Assert.Equal("Unknown token “nope” at position 9.", plan.NameError); + Assert.Equal("Harbour sunset.gif", plan.FileName); + } + + [Test] + public void EditingAValueMakesThePresetCustom() + { + var project = new ProjectSettings(); + project = project with { Export = project.Export with { Preset = project.Export.Preset.WithManual("png-sequence"), Overrides = new ExportOverrides { Width = 48 } } }; + var plan = Planner().Plan(project)!; + Assert.True(plan.Export.IsCustom); + Assert.Equal("Custom", plan.PresetName); + Assert.Equal(48, plan.Setup.Encoder.Settings.Width); + } +} diff --git a/tests/Titano.App.Tests/Editor/PreviewRendererTests.cs b/tests/Titano.App.Tests/Editor/PreviewRendererTests.cs new file mode 100644 index 00000000..cbf38189 --- /dev/null +++ b/tests/Titano.App.Tests/Editor/PreviewRendererTests.cs @@ -0,0 +1,121 @@ +using Titano.App.Editor; +using Titano.Core.Imaging; +using Titano.Core.Project; +using Titano.Testing; + +namespace Titano.App.Tests.Editor; + +/// The corrected-frame preview through the render pipeline at preview size. +public sealed class PreviewRendererTests +{ + private static (PreviewRenderer Renderer, FakeSequenceSource Source) Create() + { + var content = EditorFixture.Content(EditorFixture.Sunset); + var source = new FakeSequenceSource(EditorFixture.Sunset, content); + return (new PreviewRenderer(content, source), source); + } + + private static ProjectSettings Deflickered() + { + var p = new ProjectSettings(); + return p with { Exposure = p.Exposure with { Deflicker = p.Exposure.Deflicker.WithManual(true), HighlightKnee = null } }; + } + + [Test] + public async Task DeflickerFlattensThePreviewedLuminance() + { + var (renderer, _) = Create(); + var project = Deflickered(); + var corrections = await renderer.PrepareAsync(project, CancellationToken.None); + Assert.Same(corrections, renderer.Cached(project)); + Assert.Null(renderer.Cached(new ProjectSettings()), "other exposure settings need their own preparation"); + + var corrected = new List(); + var original = new List(); + for (var i = 4; i < 20; i++) + { + using var frame = await renderer.RenderAsync(new PreviewRequest(i, project, 64, PreviewFraming.Output), corrections, CancellationToken.None); + Assert.NotNull(frame); + corrected.Add(LogMean(frame!.Corrected)); + original.Add(LogMean(frame.Original)); + } + Assert.Less(Spread(corrected), Spread(original) * 0.6, "flicker removed between the steps"); + } + + [Test] + public async Task OutputFramingFollowsTheExportAspect() + { + var (renderer, _) = Create(); + var project = new ProjectSettings(); + project = project with { Export = project.Export with { Preset = project.Export.Preset.WithManual("square-1440") } }; + using var output = await renderer.RenderAsync(new PreviewRequest(0, project, 80, PreviewFraming.Output), null, CancellationToken.None); + Assert.Equal(80, output!.Corrected.Width, "the square output at the preview's long edge"); + Assert.Equal(80, output.Corrected.Height); + Assert.Equal(output.Corrected.Width, output.Original.Width); + + using var source = await renderer.RenderAsync(new PreviewRequest(0, project, 80, PreviewFraming.Source), null, CancellationToken.None); + Assert.Equal(80, source!.Corrected.Width); + Assert.Approximately(64.0 / 96, source.Crop.Width, 0.02, "the square crop keeps two thirds of the 3:2 frame"); + Assert.Approximately(1, source.Crop.Height, 0.02); + Assert.Approximately((1 - 64.0 / 96) / 2, source.Crop.X, 0.02, "centred"); + } + + [Test] + public async Task VectorsDescribeTheMotionToTheNextFrame() + { + var (renderer, _) = Create(); + using var frame = await renderer.RenderAsync(new PreviewRequest(10, new ProjectSettings(), 96, PreviewFraming.Source, Vectors: true), null, + CancellationToken.None); + Assert.NotEmpty(frame!.Vectors); + Assert.True(frame.Vectors.All(v => v.X >= 0 && v.X < 96 && v.Y >= 0 && v.Y < 64)); + using var last = await renderer.RenderAsync(new PreviewRequest(EditorFixture.Sunset.Count - 1, new ProjectSettings(), 96, PreviewFraming.Source, true), + null, CancellationToken.None); + Assert.Empty(last!.Vectors); + } + + [Test] + public async Task ACancelledPreviewStopsWhileDecoding() + { + var (renderer, source) = Create(); + source.DecodeGate = new TaskCompletionSource().Task; + using var cts = new CancellationTokenSource(TimeSpan.FromMilliseconds(100)); + await Assert.ThrowsAsync(() => renderer.RenderAsync(new PreviewRequest(0, new ProjectSettings(), 64, PreviewFraming.Output), null, cts.Token)); + } + + [Test] + public async Task AnUnknownFrameHasNoPreview() + { + var (renderer, _) = Create(); + Assert.Null(await renderer.RenderAsync(new PreviewRequest(-1, new ProjectSettings(), 64, PreviewFraming.Output), null, CancellationToken.None)); + Assert.Null(await renderer.RenderAsync(new PreviewRequest(10_000, new ProjectSettings(), 64, PreviewFraming.Output), null, CancellationToken.None)); + } + + [Test] + public async Task PreparedDecodesAreReused() + { + var (renderer, source) = Create(); + var project = new ProjectSettings(); + project = project with { Exposure = project.Exposure with { WhiteBalanceSmoothing = project.Exposure.WhiteBalanceSmoothing.WithManual(true) } }; + await renderer.PrepareAsync(project, CancellationToken.None); + var decodes = source.Decodes; + Assert.GreaterOrEqual(decodes, EditorFixture.Sunset.Count, "white balance is measured on every frame"); + await renderer.PrepareAsync(project with { Exposure = project.Exposure with { ExposureOffsetEv = 0.5 } }, CancellationToken.None); + Assert.Equal(decodes, source.Decodes); + } + + private static double LogMean(ImageBuffer image) + { + double sum = 0; + var n = image.PlaneLength; + for (var i = 0; i < n; i++) sum += 0.2126 * image.Plane(0)[i] + 0.7152 * image.Plane(1)[i] + 0.0722 * image.Plane(2)[i]; + return Math.Log2(sum / n); + } + + /// Standard deviation of the frame-to-frame differences (flicker, not the slow ramp). + private static double Spread(List values) + { + var diffs = values.Zip(values.Skip(1), (a, b) => b - a).ToList(); + var mean = diffs.Average(); + return Math.Sqrt(diffs.Sum(d => (d - mean) * (d - mean)) / diffs.Count); + } +} diff --git a/tests/Titano.App.Tests/Editor/ProjectHistoryTests.cs b/tests/Titano.App.Tests/Editor/ProjectHistoryTests.cs new file mode 100644 index 00000000..95cf5b20 --- /dev/null +++ b/tests/Titano.App.Tests/Editor/ProjectHistoryTests.cs @@ -0,0 +1,90 @@ +using Titano.App.Editor; +using Titano.Core.Project; +using Titano.Testing; + +namespace Titano.App.Tests.Editor; + +public sealed class ProjectHistoryTests +{ + private static ProjectSettings WithOffset(ProjectSettings p, double ev) => p with { Exposure = p.Exposure with { ExposureOffsetEv = ev } }; + + [Test] + public void UndoAndRedoWalkTheSnapshots() + { + var start = new ProjectSettings(); + var history = new ProjectHistory(start); + var changes = 0; + history.Changed += () => changes++; + + Assert.True(history.Apply(WithOffset(start, 0.5))); + Assert.True(history.Apply(WithOffset(history.Current, 1.0))); + Assert.Equal(1.0, history.Current.Exposure.ExposureOffsetEv); + + Assert.True(history.Undo()); + Assert.Equal(0.5, history.Current.Exposure.ExposureOffsetEv); + Assert.True(history.Undo()); + Assert.Equal(0.0, history.Current.Exposure.ExposureOffsetEv); + Assert.False(history.Undo(), "nothing left to undo"); + Assert.True(history.CanRedo); + + Assert.True(history.Redo()); + Assert.Equal(0.5, history.Current.Exposure.ExposureOffsetEv); + Assert.Equal(5, changes); + } + + [Test] + public void ANewEditClearsRedo() + { + var history = new ProjectHistory(new ProjectSettings()); + history.Apply(WithOffset(history.Current, 0.5)); + history.Undo(); + history.Apply(WithOffset(history.Current, -0.5)); + Assert.False(history.CanRedo); + Assert.False(history.Redo()); + } + + [Test] + public void EditsWithTheSameKeyAreOneStep() + { + var history = new ProjectHistory(new ProjectSettings()); + for (var i = 1; i <= 10; i++) history.Apply(WithOffset(history.Current, i / 10.0), "Exposure.Offset"); + history.Apply(history.Current with { HeroFrame = 3 }, "Frames.Hero"); + + Assert.True(history.Undo()); + Assert.Equal(1.0, history.Current.Exposure.ExposureOffsetEv, "the hero step undone"); + Assert.True(history.Undo()); + Assert.Equal(0.0, history.Current.Exposure.ExposureOffsetEv, "the whole drag undone in one step"); + Assert.False(history.CanUndo); + } + + [Test] + public void AnEditThatChangesNothingIsNotAStep() + { + var history = new ProjectHistory(new ProjectSettings()); + Assert.False(history.Apply(new ProjectSettings()), "equal values, even as a new instance"); + Assert.False(history.CanUndo); + } + + [Test] + public void ReplaceAdoptsWithoutAnUndoStep() + { + var history = new ProjectHistory(new ProjectSettings()); + history.Apply(WithOffset(history.Current, 0.5)); + history.Replace(history.Current with { HeroFrame = 7 }); + Assert.Equal(7, history.Current.HeroFrame); + Assert.True(history.Undo()); + Assert.Equal(0.0, history.Current.Exposure.ExposureOffsetEv); + Assert.False(history.CanUndo); + } + + [Test] + public void TheOldestStepsAreDroppedBeyondCapacity() + { + var history = new ProjectHistory(new ProjectSettings(), capacity: 3); + for (var i = 1; i <= 5; i++) history.Apply(history.Current with { HeroFrame = i }); + var undone = 0; + while (history.Undo()) undone++; + Assert.Equal(3, undone); + Assert.Equal(2, history.Current.HeroFrame); + } +} diff --git a/tests/Titano.App.Tests/Editor/ProjectStoreTests.cs b/tests/Titano.App.Tests/Editor/ProjectStoreTests.cs new file mode 100644 index 00000000..e29a2aaa --- /dev/null +++ b/tests/Titano.App.Tests/Editor/ProjectStoreTests.cs @@ -0,0 +1,157 @@ +using System.IO; +using Titano.App.Editor; +using Titano.Core.Archiving; +using Titano.Core.Codecs.Jpeg; +using Titano.Core.Imaging; +using Titano.Core.Imaging.Decoders; +using Titano.Core.Metadata; +using Titano.Core.Model; +using Titano.Core.Project; +using Titano.Data; +using Titano.Data.Archiving; +using Titano.Data.Jobs; +using Titano.Data.Processing; +using Titano.Data.Repositories; +using Titano.Testing; + +namespace Titano.App.Tests.Editor; + +/// Project merges, and the local source over a real SQLite index and archive folder. +public sealed class ProjectStoreTests(TestContext context) +{ + [Test] + public void AMemberOnlyTheyChangedIsKept() + { + var basis = new ProjectSettings(); + var ours = basis with { Exposure = basis.Exposure with { ExposureOffsetEv = 0.5 } }; + var theirs = basis with { HeroFrame = 12 }; + var merged = ProjectMerge.Merge(basis, ours, theirs); + Assert.Equal(0.5, merged.Exposure.ExposureOffsetEv); + Assert.Equal(12, merged.HeroFrame); + } + + [Test] + public void AdviceMergesIntoAManualValueOfTheSameSetting() + { + var basis = new ProjectSettings(); + var ours = basis with { Exposure = basis.Exposure with { Deflicker = basis.Exposure.Deflicker.WithManual(true) } }; + var theirs = basis with { Exposure = basis.Exposure with { Deflicker = basis.Exposure.Deflicker.WithAdvice(false, "Advisor.Reason.Deflicker.Off") } }; + var merged = ProjectMerge.Merge(basis, ours, theirs).Exposure.Deflicker; + Assert.False(merged.IsAuto, "the user's manual choice stays"); + Assert.True(merged.Manual); + Assert.True(merged.HasAdvice, "the new advice is stored next to it"); + Assert.Equal("Advisor.Reason.Deflicker.Off", merged.ReasonKey); + } + + [Test] + public void WhenBothChangedAValueTheEditorWins() + { + var basis = new ProjectSettings(); + var merged = ProjectMerge.Merge(basis, basis with { HeroFrame = 3 }, basis with { HeroFrame = 9 }); + Assert.Equal(3, merged.HeroFrame); + } + + [Test] + public async Task TheLocalSourceReadsDecodesSavesAndQueues() + { + var (db, sequenceId, root) = await ArchiveAsync(12); + await using (db) + { + var options = new ArchiveServiceOptions { Owner = "test", LeaseWait = TimeSpan.FromSeconds(20) }; + await using var source = new LocalSequenceSource(db, sequenceId, DecoderFactory.CreateClient(null), null, options); + var content = await source.LoadAsync(context.Cancellation); + Assert.Equal(12, content.Count); + Assert.Equal(96, content.SourceWidth); + Assert.Equal(64, content.SourceHeight); + Assert.Null(content.Deep, "not analysed yet"); + Assert.NotNull(content.ExportsFolder); + Assert.Contains("exports", content.ExportsFolder!); + + using (var frame = await source.DecodeAsync(3, 32, DecodePurpose.Thumbnail, context.Cancellation)) + { + Assert.NotNull(frame); + Assert.LessOrEqual(Math.Max(frame!.Width, frame.Height), 32); + } + Assert.Null(await source.DecodeAsync(99, 32, DecodePurpose.Thumbnail, context.Cancellation)); + + // A save writes project.json and the index row together. + var edited = content.Project with { HeroFrame = 4, Exposure = content.Project.Exposure with { ExposureOffsetEv = 0.3 } }; + var stored = await source.SaveProjectAsync(edited, content.Project, context.Cancellation); + Assert.True(ProjectMerge.SameValues(edited, stored)); + var projectPath = Path.Combine(root, "Synthetic", ".titano", "project.json"); + Assert.FileExists(projectPath); + Assert.Equal(edited.ToJson(), File.ReadAllText(projectPath)); + var row = await db.Sequences.GetAsync(sequenceId); + Assert.Equal(edited.ToJson(), row!.SettingsJson); + + // Someone else changes the settings meanwhile (the server API): both edits survive the next save. + var theirs = stored with { Camera = stored.Camera with { OffsetY = 0.25 } }; + await db.Sequences.EditAsync(sequenceId, new SequenceEdit { SettingsJson = theirs.ToJson() }, row.SettingsVersion); + var mine = stored with { HeroFrame = 7 }; + var merged = await source.SaveProjectAsync(mine, stored, context.Cancellation); + Assert.Equal(7, merged.HeroFrame); + Assert.Equal(0.25, merged.Camera.OffsetY); + Assert.Equal(merged.ToJson(), File.ReadAllText(projectPath)); + Assert.Equal(merged.ToJson(), (await db.Sequences.GetAsync(sequenceId))!.SettingsJson); + + var reloaded = await source.LoadAsync(context.Cancellation); + Assert.True(ProjectMerge.SameValues(merged, reloaded.Project), "the index copy is read back"); + + await Assert.ThrowsAsync(() => source.SubmitAsync(ProcessingJobs.DeepAnalysis([sequenceId]), context.Cancellation)); + } + } + + [Test] + public async Task JobsGoToTheEngine() + { + var (db, sequenceId, _) = await ArchiveAsync(4); + await using (db) + { + await using var engine = new JobEngine(db, [], new JobEngineOptions { WorkerId = "test" }); + await using var source = new LocalSequenceSource(db, sequenceId, DecoderFactory.CreateClient(null), engine); + var content = await source.LoadAsync(context.Cancellation); + var session = new EditorSession(source, content, []); + var id = await session.SubmitAsync(session.AnalysisRequest(), context.Cancellation); + var job = await db.Jobs.GetAsync(id); + Assert.Equal(JobKind.DeepAnalyse, job!.Kind); + Assert.Equal(JobState.Queued, job.State); + Assert.SequenceEqual([sequenceId], job.TargetIds); + } + } + + [Test] + public async Task AnUnreadableProjectIsReportedNotReplaced() + { + var (db, sequenceId, root) = await ArchiveAsync(4); + await using (db) + { + var path = Path.Combine(root, "Synthetic", ".titano", "project.json"); + File.WriteAllText(path, "{ not json"); + await using var source = new LocalSequenceSource(db, sequenceId, DecoderFactory.CreateClient(null), null); + await Assert.ThrowsAsync(() => source.LoadAsync(context.Cancellation)); + Assert.Equal("{ not json", File.ReadAllText(path)); + } + } + + /// A temporary archive with one adopted folder of synthetic JPEGs, indexed. + private async Task<(TitanoDatabase Db, Guid SequenceId, string Root)> ArchiveAsync(int frames) + { + var directory = context.CreateTempDirectory("editor"); + var root = Path.Combine(directory, "archive"); + var db = await TitanoDatabase.OpenAsync(DatabaseOptions.Sqlite(Path.Combine(directory, "index.db"))); + var service = new ArchiveService(db, new ArchiveServiceOptions { Owner = "test", LeaseWait = TimeSpan.FromSeconds(20) }); + var archive = await service.CreateAsync(root, "Test"); + var sequence = EditorFixture.Sequence(frames); + var folder = Path.Combine(root, "Synthetic"); + Directory.CreateDirectory(folder); + for (var i = 0; i < sequence.Count; i++) + { + using var image = sequence.Render(i); + var exif = ExifWriter.BuildJpegApp1(sequence.Metadata[i]); + File.WriteAllBytes(Path.Combine(folder, $"IMG_{i + 1:0000}.JPG"), JpegEncoder.Encode(image, new JpegEncodeOptions { Quality = 90, App1Segments = [exif[4..]] })); + } + await service.Indexer.ScanAsync(archive, ScanMode.Full, null, _ => ValueTask.CompletedTask, context.Cancellation); + var location = (await db.Sequences.ListLocationsAsync(archive.Id, context.Cancellation)).Single(); + return (db, location.Id, root); + } +} diff --git a/tests/Titano.App.Tests/Editor/SchedulingTests.cs b/tests/Titano.App.Tests/Editor/SchedulingTests.cs new file mode 100644 index 00000000..1df14a74 --- /dev/null +++ b/tests/Titano.App.Tests/Editor/SchedulingTests.cs @@ -0,0 +1,133 @@ +using System.Collections.Concurrent; +using Titano.App.Editor; +using Titano.Core.Project; +using Titano.Testing; + +namespace Titano.App.Tests.Editor; + +/// Debouncing and cancellation of preview work, and the coalescing project writer. +public sealed class SchedulingTests +{ + [Test] + public async Task OnlyTheLastOfRapidRequestsRuns() + { + using var runner = new LatestRunner(TimeSpan.FromMilliseconds(80)); + var ran = new ConcurrentQueue(); + var tasks = Enumerable.Range(0, 10).Select(i => runner.Schedule(_ => + { + ran.Enqueue(i); + return Task.CompletedTask; + })).ToList(); + await Task.WhenAll(tasks); + Assert.SequenceEqual([9], ran); + } + + [Test] + public async Task ANewRequestCancelsTheRunningOne() + { + using var runner = new LatestRunner(TimeSpan.Zero); + var started = new TaskCompletionSource(); + var cancelled = false; + var first = runner.Schedule(async token => + { + started.SetResult(); + try + { + await Task.Delay(Timeout.Infinite, token); + } + catch (OperationCanceledException) + { + cancelled = true; + throw; + } + }); + await started.Task; + var second = runner.Schedule(_ => Task.CompletedTask); + await Task.WhenAll(first, second); + Assert.True(cancelled, "the superseded run saw its token cancelled"); + Assert.True(first.IsCompletedSuccessfully, "a superseded run ends quietly"); + } + + [Test] + public async Task ImmediateRequestsSkipTheDelay() + { + using var runner = new LatestRunner(TimeSpan.FromSeconds(30)); + var ran = false; + await runner.Schedule(_ => + { + ran = true; + return Task.CompletedTask; + }, immediate: true).WaitAsync(TimeSpan.FromSeconds(10)); + Assert.True(ran); + } + + [Test] + public async Task FailuresSurfaceFromTheRun() + { + using var runner = new LatestRunner(TimeSpan.Zero); + await Assert.ThrowsAsync(() => runner.Schedule(_ => throw new InvalidOperationException("broken"))); + } + + [Test] + public async Task CancelStopsThePendingRequest() + { + using var runner = new LatestRunner(TimeSpan.FromMilliseconds(200)); + var ran = false; + var run = runner.Schedule(_ => + { + ran = true; + return Task.CompletedTask; + }); + runner.Cancel(); + await run; + Assert.False(ran); + } + + [Test] + public async Task TheSaverWritesTheNewestProjectOnce() + { + var saved = new List(); + var saver = new ProjectSaver(p => + { + saved.Add(p.HeroFrame); + return Task.CompletedTask; + }, TimeSpan.FromMilliseconds(50)); + for (var i = 0; i < 5; i++) saver.Schedule(new ProjectSettings { HeroFrame = i }); + await saver.FlushAsync(); + Assert.SequenceEqual([(int?)4], saved); + Assert.False(saver.HasPendingChanges); + } + + [Test] + public async Task FlushSkipsTheDelay() + { + var saved = 0; + var saver = new ProjectSaver(_ => + { + saved++; + return Task.CompletedTask; + }, TimeSpan.FromMinutes(5)); + saver.Schedule(new ProjectSettings()); + await saver.FlushAsync().WaitAsync(TimeSpan.FromSeconds(10)); + Assert.Equal(1, saved); + } + + [Test] + public async Task AChangeDuringAWriteIsWrittenAfterIt() + { + var gate = new TaskCompletionSource(); + var saved = new List(); + var saver = new ProjectSaver(async p => + { + saved.Add(p.HeroFrame); + if (saved.Count == 1) await gate.Task; + }, TimeSpan.Zero); + saver.Schedule(new ProjectSettings { HeroFrame = 1 }); + while (saved.Count == 0) await Task.Delay(5); + saver.Schedule(new ProjectSettings { HeroFrame = 2 }); + saver.Schedule(new ProjectSettings { HeroFrame = 3 }); + gate.SetResult(); + await saver.FlushAsync(); + Assert.SequenceEqual([(int?)1, 3], saved); + } +}