feat(app): editor session, project history, local source, preview renderer and export planner
Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com> Claude-Session: https://claude.ai/code/session_012iLMBNwBE2GnphAsu4cjaC
This commit is contained in:
19 files changed
+2146
No files matched your search
@@ -5,4 +5,7 @@
|
||||
<resheader name="reader"><value>System.Resources.ResXResourceReader, System.Windows.Forms, Version=4.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089</value></resheader>
|
||||
<resheader name="writer"><value>System.Resources.ResXResourceWriter, System.Windows.Forms, Version=4.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089</value></resheader>
|
||||
<data name="Editor.Empty" xml:space="preserve"><value>Pick a sequence in the Library</value></data>
|
||||
<data name="Editor.Error.Missing" xml:space="preserve"><value>This sequence is no longer in the archive.</value><comment>long</comment></data>
|
||||
<data name="Editor.Error.NoEngine" xml:space="preserve"><value>Jobs cannot run until the archive index is open.</value><comment>long</comment></data>
|
||||
<data name="Editor.Error.Project" xml:space="preserve"><value>The settings of this sequence cannot be read.</value><comment>long</comment></data>
|
||||
</root>
|
||||
@@ -5,4 +5,7 @@
|
||||
<resheader name="reader"><value>System.Resources.ResXResourceReader, System.Windows.Forms, Version=4.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089</value></resheader>
|
||||
<resheader name="writer"><value>System.Resources.ResXResourceWriter, System.Windows.Forms, Version=4.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089</value></resheader>
|
||||
<data name="Editor.Empty" xml:space="preserve"><value>Scegli una sequenza in Libreria</value></data>
|
||||
<data name="Editor.Error.Missing" xml:space="preserve"><value>Questa sequenza non è più nell’archivio.</value></data>
|
||||
<data name="Editor.Error.NoEngine" xml:space="preserve"><value>I lavori non possono partire finché l’indice dell’archivio non è aperto.</value></data>
|
||||
<data name="Editor.Error.Project" xml:space="preserve"><value>Le impostazioni di questa sequenza non sono leggibili.</value></data>
|
||||
</root>
|
||||
@@ -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;
|
||||
|
||||
/// <summary>
|
||||
/// 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 (<see cref="ProjectSaver"/>); a change stored meanwhile by
|
||||
/// someone else (an analysis job, another client) is merged in. Call from one thread (the UI thread).
|
||||
/// </summary>
|
||||
public sealed class EditorSession : IAsyncDisposable
|
||||
{
|
||||
public static readonly TimeSpan DefaultSaveDelay = TimeSpan.FromMilliseconds(400);
|
||||
|
||||
/// <summary>Sections "Optimise all" walks through, in panel order.</summary>
|
||||
public static IReadOnlyList<AdvisorSection> OptimisedSections { get; } =
|
||||
[AdvisorSection.Frames, AdvisorSection.Timing, AdvisorSection.Exposure, AdvisorSection.Motion, AdvisorSection.Camera, AdvisorSection.Export];
|
||||
|
||||
private readonly IReadOnlyList<EncoderCapability> _codecs;
|
||||
private readonly ProjectSaver _saver;
|
||||
private ProjectSettings _basis;
|
||||
|
||||
public EditorSession(ISequenceSource source, SequenceContent content, IReadOnlyList<EncoderCapability> 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);
|
||||
}
|
||||
|
||||
/// <summary>The project changed (edit, undo, redo, Optimise, a merge).</summary>
|
||||
public event Action? ProjectChanged;
|
||||
|
||||
/// <summary>The sequence was read again (after an analysis): frames, reports, advice and presets.</summary>
|
||||
public event Action? ContentChanged;
|
||||
|
||||
/// <summary>Saving the project failed; the next change tries again.</summary>
|
||||
public event Action<Exception>? SaveFailed;
|
||||
|
||||
public ISequenceSource Source { get; }
|
||||
|
||||
public SequenceContent Content { get; private set; }
|
||||
|
||||
public SequenceContext Context { get; private set; }
|
||||
|
||||
/// <summary>The advisor's recommendations and findings for the current analyses.</summary>
|
||||
public AdvisorResult Advice { get; private set; }
|
||||
|
||||
/// <summary>Sequence presets that fit the content, best first (BRIEF §10.3).</summary>
|
||||
public IReadOnlyList<SequencePreset> Presets { get; private set; }
|
||||
|
||||
public ProjectHistory History { get; }
|
||||
|
||||
public ProjectSettings Project => History.Current;
|
||||
|
||||
/// <summary>Applies <paramref name="change"/> as an undoable step; false when it changes nothing.</summary>
|
||||
/// <param name="mergeKey">Consecutive edits with the same key are one undo step (a slider drag).</param>
|
||||
public bool Edit(Func<ProjectSettings, ProjectSettings> change, string? mergeKey = null)
|
||||
{
|
||||
ArgumentNullException.ThrowIfNull(change);
|
||||
return History.Apply(change(Project), mergeKey);
|
||||
}
|
||||
|
||||
public bool Undo() => History.Undo();
|
||||
|
||||
public bool Redo() => History.Redo();
|
||||
|
||||
/// <summary>"Optimise section": the advisor's heavier decisions for <paramref name="section"/>, one undo step; returns what changed.</summary>
|
||||
public IReadOnlyList<SettingChange> Optimise(AdvisorSection section) => Optimise([section]);
|
||||
|
||||
/// <summary>"Optimise all": every section, one undo step.</summary>
|
||||
public IReadOnlyList<SettingChange> OptimiseAll() => Optimise(OptimisedSections);
|
||||
|
||||
/// <summary>"Reset": the section's Auto settings back to Auto.</summary>
|
||||
public bool ResetSection(AdvisorSection section) => Edit(p => EditorSettings.ResetSection(p, section));
|
||||
|
||||
/// <summary>Adds frames to, or removes them from, the project's exclusions.</summary>
|
||||
public bool SetExcluded(IEnumerable<int> 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))],
|
||||
});
|
||||
}
|
||||
|
||||
/// <summary>Makes <paramref name="frame"/> the hero frame (sequence thumbnail).</summary>
|
||||
public bool SetHero(int frame) => (uint)frame < (uint)Content.Count && Edit(p => p with { HeroFrame = frame });
|
||||
|
||||
/// <summary>
|
||||
/// A render of <paramref name="export"/> 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).
|
||||
/// </summary>
|
||||
public ArchiveJobRequest RenderRequest(ExportSettings export, bool useProxies = false) =>
|
||||
ProcessingJobs.Render(Content.Sequence, export, Content.SourceWidth, Content.SourceHeight, Project with { Export = export }, useProxies);
|
||||
|
||||
/// <summary>The export of the project with preset <paramref name="presetId"/> chosen (own values, no batch).</summary>
|
||||
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<Guid> SubmitAsync(ArchiveJobRequest request, CancellationToken cancellationToken = default) => Source.SubmitAsync(request, cancellationToken);
|
||||
|
||||
/// <summary>Reads the sequence again (an analysis finished): new reports and advice, the stored project merged into the open one.</summary>
|
||||
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();
|
||||
}
|
||||
|
||||
/// <summary>Stores pending changes now.</summary>
|
||||
public Task FlushAsync() => _saver.FlushAsync();
|
||||
|
||||
public async ValueTask DisposeAsync()
|
||||
{
|
||||
History.Changed -= OnProjectChanged;
|
||||
await _saver.FlushAsync();
|
||||
await Source.DisposeAsync();
|
||||
}
|
||||
|
||||
private static (SequenceContent, SequenceContext, AdvisorResult, IReadOnlyList<SequencePreset>) Analyse(SequenceContent content,
|
||||
IReadOnlyList<EncoderCapability> 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<SettingChange> Optimise(IReadOnlyList<AdvisorSection> sections)
|
||||
{
|
||||
var settings = Project;
|
||||
var changes = new List<SettingChange>();
|
||||
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));
|
||||
}
|
||||
}
|
||||
@@ -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;
|
||||
|
||||
/// <summary>An advisor-decided setting of the editor, whatever its value type.</summary>
|
||||
public interface IAutoSetting
|
||||
{
|
||||
/// <summary><see cref="AdvisorParameters"/> id.</summary>
|
||||
string Id { get; }
|
||||
|
||||
AdvisorSection Section { get; }
|
||||
|
||||
bool IsAuto(ProjectSettings project);
|
||||
|
||||
/// <summary>Drops the manual value so the advice applies.</summary>
|
||||
ProjectSettings Reset(ProjectSettings project);
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// One <see cref="Auto{T}"/> member of the project (BRIEF §10.4) and the value the engine uses when the advisor has
|
||||
/// none (<see cref="Fallback"/>, from the deep analysis when it measured one).
|
||||
/// </summary>
|
||||
public sealed class AutoSetting<T>(
|
||||
string id,
|
||||
AdvisorSection section,
|
||||
Func<ProjectSettings, Auto<T>> get,
|
||||
Func<ProjectSettings, Auto<T>, ProjectSettings> set,
|
||||
Func<ProjectSettings, DeepAnalysisReport?, T> fallback) : IAutoSetting
|
||||
where T : notnull
|
||||
{
|
||||
public string Id { get; } = id;
|
||||
|
||||
public AdvisorSection Section { get; } = section;
|
||||
|
||||
public Auto<T> Get(ProjectSettings project) => get(project);
|
||||
|
||||
public bool IsAuto(ProjectSettings project) => get(project).IsAuto;
|
||||
|
||||
/// <summary>The advisor's value, else what the engine uses without advice.</summary>
|
||||
public T AutoValue(ProjectSettings project, DeepAnalysisReport? deep) => get(project) is { HasAdvice: true, AutoValue: { } advised } ? advised : fallback(project, deep);
|
||||
|
||||
/// <summary>The value in force.</summary>
|
||||
public T Effective(ProjectSettings project, DeepAnalysisReport? deep) => get(project).Resolve(fallback(project, deep));
|
||||
|
||||
/// <summary>Touching the control: the value becomes manual.</summary>
|
||||
public ProjectSettings WithManual(ProjectSettings project, T value) => set(project, get(project).WithManual(value));
|
||||
|
||||
public ProjectSettings Reset(ProjectSettings project) => set(project, get(project).Reset());
|
||||
}
|
||||
|
||||
/// <summary>The Auto settings of the editor's sections, in panel order.</summary>
|
||||
public static class EditorSettings
|
||||
{
|
||||
public static readonly AutoSetting<TimingMode> TimingMode = new(AdvisorParameters.TimingMode, AdvisorSection.Timing,
|
||||
p => p.Timing.Mode, (p, a) => p with { Timing = p.Timing with { Mode = a } }, (_, _) => Core.Project.TimingMode.Constant);
|
||||
|
||||
/// <summary>Playback rate; without a value it equals the export frame rate (DECISIONS: export fps vs playback rate).</summary>
|
||||
public static readonly AutoSetting<string> 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<bool> 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<int> 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<double> 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<RegionMode> 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<bool> 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<int> 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<bool> 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<bool> 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<double> 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<FlowQualitySetting> 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<bool> 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<double> 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<string> 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<string> 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<IAutoSetting> All { get; } =
|
||||
[
|
||||
TimingMode, FrameRate, Deflicker, DeflickerWindow, DeflickerStrength, Regions, HolyGrail, TransitionLength, WhiteBalance,
|
||||
MotionBlur, ShutterAngle, FlowQuality, Stabilize, StabilizeSmoothing, Aspect, ExportPreset,
|
||||
];
|
||||
|
||||
/// <summary>"Reset": every Auto setting of <paramref name="section"/> back to Auto; stacking (no advice) back to off.</summary>
|
||||
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;
|
||||
}
|
||||
|
||||
/// <summary>True when every Auto setting of <paramref name="section"/> is in Auto.</summary>
|
||||
public static bool IsSectionAuto(ProjectSettings project, AdvisorSection section) =>
|
||||
All.Where(s => s.Section == section).All(s => s.IsAuto(project));
|
||||
}
|
||||
@@ -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;
|
||||
|
||||
/// <summary>A choice of the Export tab: a built-in export preset, or a sequence preset (full settings plus an export preset).</summary>
|
||||
/// <param name="ExportPresetId">The export preset the card renders.</param>
|
||||
/// <param name="Sequence">The sequence preset it applies, null for a plain export preset.</param>
|
||||
/// <param name="IsRecommended">The advisor's export choice for this sequence.</param>
|
||||
/// <param name="Reason">Why it fits (advisor reason), localised; null for plain presets.</param>
|
||||
public sealed record ExportChoice(string Id, string Name, string ExportPresetId, SequencePreset? Sequence, bool IsRecommended, string? Reason);
|
||||
|
||||
/// <summary>What one export will produce: resolved values, encoder, plan, size and time estimate, file name.</summary>
|
||||
/// <param name="FileName">Output name with extension (a folder name for image sequences).</param>
|
||||
/// <param name="NameError">The first problem of the name template, localised; null when it is valid.</param>
|
||||
public sealed record ExportPlan(ResolvedExport Export, RenderSetup Setup, ExportEstimate Estimate, string FileName, string? NameError)
|
||||
{
|
||||
public string PresetName => ProcessingJobs.PresetName(Export);
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// The Export tab's numbers (BRIEF §11.4–11.5): the preset list (sequence presets first, then the built-in presets, the
|
||||
/// advisor's pick marked), and for any export the encoder this machine would use with its notices, the plan, the size
|
||||
/// and time estimates (<see cref="ExportEstimator"/>) and the output name (<see cref="ExportNaming"/>). Nothing decodes:
|
||||
/// the plan needs only the frame metadata and the source size.
|
||||
/// </summary>
|
||||
public sealed class ExportPlanner(SequenceContent content, IReadOnlyList<IVideoEncoderProvider> encoders, EncoderHost host, ThroughputModel? model = null)
|
||||
{
|
||||
/// <summary>Sequence presets that fit the content, then every built-in export preset.</summary>
|
||||
public static IReadOnlyList<ExportChoice> Choices(IReadOnlyList<SequencePreset> presets, AdvisorResult advice)
|
||||
{
|
||||
ArgumentNullException.ThrowIfNull(presets);
|
||||
ArgumentNullException.ThrowIfNull(advice);
|
||||
var recommended = advice.Find(AdvisorParameters.ExportPreset)?.Value as string;
|
||||
var choices = new List<ExportChoice>();
|
||||
foreach (var preset in presets)
|
||||
{
|
||||
choices.Add(new ExportChoice("sequence:" + preset.Id, preset.DisplayName(), preset.ExportPresetId, preset, false,
|
||||
preset.ReasonKey is { } key ? Advisor.Explain(key, preset.ReasonArgs) : null));
|
||||
}
|
||||
var reason = advice.Find(AdvisorParameters.ExportPreset) is { IsMissingData: false } r ? Advisor.Explain(r) : null;
|
||||
foreach (var preset in ExportPresets.BuiltIn)
|
||||
{
|
||||
var isRecommended = string.Equals(preset.Id, recommended, StringComparison.OrdinalIgnoreCase);
|
||||
choices.Add(new ExportChoice(preset.Id, Loc.L(preset.NameKey), preset.Id, null, isRecommended, isRecommended ? reason : null));
|
||||
}
|
||||
return choices;
|
||||
}
|
||||
|
||||
/// <summary>The project's own export.</summary>
|
||||
public ExportPlan? Plan(ProjectSettings project) => Plan(project, project.Export);
|
||||
|
||||
/// <summary>What <paramref name="export"/> would produce with <paramref name="project"/>; null when the source size is unknown.</summary>
|
||||
public ExportPlan? Plan(ProjectSettings project, ExportSettings export)
|
||||
{
|
||||
ArgumentNullException.ThrowIfNull(project);
|
||||
ArgumentNullException.ThrowIfNull(export);
|
||||
if (content.SourceWidth <= 0 || content.SourceHeight <= 0 || content.Count == 0) return null;
|
||||
var resolved = ExportResolver.Resolve(export);
|
||||
var input = new RenderInput
|
||||
{
|
||||
Source = new SizeOnly(content),
|
||||
Frames = SourceFrameInfo.From(content.Frames, content.Light.AdjustedTimes.Length == content.Count ? content.Light.AdjustedTimes : null),
|
||||
Project = project with { Export = export, ExcludedFrames = [.. project.ExcludedFrames.Union(content.NotRenderable()).Order()] },
|
||||
};
|
||||
var setup = RenderSetup.Create(input, new RenderTarget(resolved, ExportNaming.FallbackName), encoders, host);
|
||||
var (name, error) = FileName(export.NameTemplate, resolved, setup);
|
||||
return new ExportPlan(resolved, setup, setup.Estimate(model), name, error);
|
||||
}
|
||||
|
||||
/// <summary>The first problem of a name template, localised; null when it is valid.</summary>
|
||||
public static string? NameError(IReadOnlyList<Core.Naming.TemplateError> errors) =>
|
||||
errors.Count == 0 ? null : Loc.L("Errors.Template." + errors[0].Code, errors[0].Token ?? string.Empty, errors[0].Position + 1);
|
||||
|
||||
private (string Name, string? Error) FileName(string template, ResolvedExport export, RenderSetup setup)
|
||||
{
|
||||
var settings = setup.Encoder.Settings;
|
||||
var expanded = ExportNaming.Expand(template, new ExportNameContext
|
||||
{
|
||||
Date = content.Sequence.FirstCapture ?? DateTime.Now,
|
||||
Title = content.Sequence.DisplayTitle,
|
||||
PresetName = ProcessingJobs.PresetName(export),
|
||||
Width = settings.Width,
|
||||
Height = settings.Height,
|
||||
FrameRate = settings.FrameRate,
|
||||
Codec = settings.Codec,
|
||||
});
|
||||
var extension = VideoCodecInfo.DefaultContainer(settings.Codec) == ContainerFormat.ImageSequence ? string.Empty : VideoCodecInfo.FileExtension(settings.Codec);
|
||||
return (expanded.Name + extension, NameError(expanded.Errors));
|
||||
}
|
||||
|
||||
/// <summary>The render setup reads sizes and counts only.</summary>
|
||||
private sealed class SizeOnly(SequenceContent content) : IRenderFrameSource
|
||||
{
|
||||
public int FrameCount => content.Count;
|
||||
|
||||
public int Width => content.SourceWidth;
|
||||
|
||||
public int Height => content.SourceHeight;
|
||||
|
||||
public ValueTask<ImageBuffer> GetFrameAsync(int index, int maxLongEdge, DecodePurpose purpose, CancellationToken cancellationToken) =>
|
||||
throw new NotSupportedException("Export planning never decodes.");
|
||||
}
|
||||
}
|
||||
@@ -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;
|
||||
|
||||
/// <summary>
|
||||
/// Where the sequence open in the editor lives: the local archive (<see cref="LocalSequenceSource"/>) 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.
|
||||
/// </summary>
|
||||
public interface ISequenceSource : IAsyncDisposable
|
||||
{
|
||||
/// <summary>Everything the editor shows of the sequence; read off the UI thread.</summary>
|
||||
/// <exception cref="InvalidDataException">The project cannot be read (corrupt, or written by a newer Titano).</exception>
|
||||
Task<SequenceContent> LoadAsync(CancellationToken cancellationToken);
|
||||
|
||||
/// <summary>
|
||||
/// Frame <paramref name="index"/> in linear light, oriented, long edge at most <paramref name="maxLongEdge"/>; null
|
||||
/// when the frame cannot be read. The caller owns the buffer.
|
||||
/// </summary>
|
||||
ValueTask<ImageBuffer?> DecodeAsync(int index, int maxLongEdge, DecodePurpose purpose, CancellationToken cancellationToken);
|
||||
|
||||
/// <summary>
|
||||
/// Stores <paramref name="project"/>, edited from <paramref name="basis"/> (the version last loaded or saved). A
|
||||
/// change made meanwhile by someone else is kept where this edit left the value alone (<see cref="ProjectMerge"/>).
|
||||
/// Returns the stored project.
|
||||
/// </summary>
|
||||
Task<ProjectSettings> SaveProjectAsync(ProjectSettings project, ProjectSettings basis, CancellationToken cancellationToken);
|
||||
|
||||
/// <summary>Queues an analysis or render job for the sequence; returns the job id.</summary>
|
||||
Task<Guid> SubmitAsync(ArchiveJobRequest request, CancellationToken cancellationToken);
|
||||
}
|
||||
|
||||
/// <summary>A sequence as the editor opens it.</summary>
|
||||
/// <param name="Frames">Frame rows in index order.</param>
|
||||
/// <param name="Light">Light analysis of the frame metadata (BRIEF §10.1).</param>
|
||||
/// <param name="Deep">The saved deep analysis when it describes the current frames, else null.</param>
|
||||
/// <param name="SourceWidth">Dominant oriented full-resolution width (0 when unknown).</param>
|
||||
/// <param name="ExportsFolder">Default output folder of renders; null when the sequence is not on this PC.</param>
|
||||
/// <param name="Renders">Finished renders, newest first.</param>
|
||||
public sealed record SequenceContent(
|
||||
Sequence Sequence,
|
||||
IReadOnlyList<Frame> Frames,
|
||||
ProjectSettings Project,
|
||||
LightAnalysisReport Light,
|
||||
DeepAnalysisReport? Deep,
|
||||
int SourceWidth,
|
||||
int SourceHeight,
|
||||
string? ExportsFolder,
|
||||
IReadOnlyList<Render> Renders)
|
||||
{
|
||||
/// <summary>Flags of frames that never reach a render (as the render job skips them).</summary>
|
||||
public const FrameFlags NotRendered = FrameFlags.Missing | FrameFlags.Corrupt | FrameFlags.Excluded | FrameFlags.Undecodable;
|
||||
|
||||
public int Count => Frames.Count;
|
||||
|
||||
/// <summary>Frames the render skips besides the project's exclusions.</summary>
|
||||
public IEnumerable<int> NotRenderable()
|
||||
{
|
||||
for (var i = 0; i < Frames.Count; i++)
|
||||
{
|
||||
if ((Frames[i].Flags & NotRendered) != 0) yield return i;
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,50 @@
|
||||
namespace Titano.App.Editor;
|
||||
|
||||
/// <summary>
|
||||
/// 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 <see cref="Schedule"/> returns.
|
||||
/// </summary>
|
||||
public sealed class LatestRunner(TimeSpan delay) : IDisposable
|
||||
{
|
||||
private CancellationTokenSource? _current;
|
||||
|
||||
/// <summary>Completes when the latest scheduled run has finished or been superseded.</summary>
|
||||
public Task Idle { get; private set; } = Task.CompletedTask;
|
||||
|
||||
public TimeSpan Delay { get; } = delay;
|
||||
|
||||
/// <param name="immediate">Skip the debounce delay (playback, the first frame).</param>
|
||||
public Task Schedule(Func<CancellationToken, Task> 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;
|
||||
}
|
||||
|
||||
/// <summary>Cancels the pending or running request.</summary>
|
||||
public void Cancel() => Interlocked.Exchange(ref _current, null)?.Cancel();
|
||||
|
||||
public void Dispose() => Cancel();
|
||||
|
||||
private async Task RunAsync(Func<CancellationToken, Task> 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();
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -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;
|
||||
|
||||
/// <summary>
|
||||
/// A sequence of an archive on this PC: rows from the index, originals decoded by the client's decoder chain, the
|
||||
/// project in <c>.titano/project.json</c> 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.
|
||||
/// </summary>
|
||||
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<SequenceContent> 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<Frame>();
|
||||
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<ImageBuffer?> 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<ProjectSettings> 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<Guid> 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;
|
||||
|
||||
/// <summary>The settings in force: the index copy, else <c>project.json</c>, else defaults.</summary>
|
||||
/// <exception cref="InvalidDataException">The settings are not a project or come from a newer Titano.</exception>
|
||||
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);
|
||||
}
|
||||
}
|
||||
|
||||
/// <summary>The saved deep report when it describes the current frames.</summary>
|
||||
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"));
|
||||
}
|
||||
@@ -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;
|
||||
|
||||
/// <summary>What the preview shows of the frame: the output picture, or the whole source frame with the crop marked.</summary>
|
||||
public enum PreviewFraming
|
||||
{
|
||||
/// <summary>Through the render geometry (stabilisation, virtual camera, crop): what the video shows.</summary>
|
||||
Output,
|
||||
|
||||
/// <summary>The whole frame, exposure-corrected; <see cref="PreviewFrame.Crop"/> marks what the output keeps.</summary>
|
||||
Source,
|
||||
}
|
||||
|
||||
/// <param name="LongEdge">Long edge of the preview picture in pixels.</param>
|
||||
/// <param name="Vectors">Also measure the motion field to the next frame.</param>
|
||||
public sealed record PreviewRequest(int Index, ProjectSettings Project, int LongEdge, PreviewFraming Framing, bool Vectors = false);
|
||||
|
||||
/// <summary>A motion sample in preview pixels: origin and displacement to the next frame.</summary>
|
||||
public readonly record struct VectorSample(float X, float Y, float Dx, float Dy);
|
||||
|
||||
/// <summary>One rendered preview: the corrected and the original picture over the same area, and the overlays.</summary>
|
||||
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;
|
||||
|
||||
/// <summary>The output's part of the picture, normalised (the whole picture in <see cref="PreviewFraming.Output"/>).</summary>
|
||||
public RectD Crop { get; init; } = new(0, 0, 1, 1);
|
||||
|
||||
/// <summary>Sky/land boundary in picture pixels; empty when the analysis found no horizon.</summary>
|
||||
public IReadOnlyList<(double X, double Y)> Horizon { get; init; } = [];
|
||||
|
||||
public IReadOnlyList<VectorSample> Vectors { get; init; } = [];
|
||||
|
||||
public void Dispose()
|
||||
{
|
||||
Corrected.Dispose();
|
||||
Original.Dispose();
|
||||
}
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// The corrected-frame preview through the render pipeline at preview size (BRIEF §12.3). Corrections come from
|
||||
/// <see cref="RenderPreparation"/> 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 <c>RenderRun</c> are followed here; stacking and motion blur need neighbouring
|
||||
/// frames and are left to the render.
|
||||
/// </summary>
|
||||
public sealed class PreviewRenderer(SequenceContent content, ISequenceSource source)
|
||||
{
|
||||
/// <summary>Long edge of the frames the preparation measures (the render uses 512; the preview trades precision for speed).</summary>
|
||||
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;
|
||||
|
||||
/// <summary>Corrections for <paramref name="project"/> when already prepared, else null.</summary>
|
||||
public RenderCorrections? Cached(ProjectSettings project)
|
||||
{
|
||||
var key = Key(project);
|
||||
lock (_gate) return _latest is { } latest && latest.Key == key ? latest.Corrections : null;
|
||||
}
|
||||
|
||||
/// <summary>The most recently prepared corrections, whatever settings they were made for.</summary>
|
||||
public RenderCorrections? Latest
|
||||
{
|
||||
get
|
||||
{
|
||||
lock (_gate) return _latest?.Corrections;
|
||||
}
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Prepares the corrections for <paramref name="project"/> (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.
|
||||
/// </summary>
|
||||
public async Task<RenderCorrections> 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;
|
||||
}
|
||||
|
||||
/// <summary>Renders one preview with <paramref name="corrections"/> (identity when null); null when the frame cannot be read.</summary>
|
||||
public async Task<PreviewFrame?> 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;
|
||||
}
|
||||
}
|
||||
|
||||
/// <summary>Cache key: the settings <see cref="RenderPreparation"/> reads.</summary>
|
||||
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));
|
||||
}
|
||||
|
||||
/// <summary>Decodes, corrects and frames one source frame: (corrected, original, output→picture plan).</summary>
|
||||
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;
|
||||
}
|
||||
}
|
||||
|
||||
/// <summary>The render's exposure stage: region curves through the resampled sky mask, else the global correction, with white-balance gains.</summary>
|
||||
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);
|
||||
}
|
||||
|
||||
/// <summary>Optical flow from this frame to the next, both prepared the same way, sampled on a grid in picture pixels.</summary>
|
||||
private async Task<IReadOnlyList<VectorSample>> 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<VectorSample>();
|
||||
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;
|
||||
}
|
||||
}
|
||||
|
||||
/// <summary>The output's footprint on the source picture (bounding box of the warped output corners), normalised.</summary>
|
||||
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);
|
||||
}
|
||||
|
||||
/// <summary>The analysed horizon (normalised rows at evenly spaced columns) in pixels of the whole-frame picture.</summary>
|
||||
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;
|
||||
}
|
||||
|
||||
/// <summary>Gray sources become RGB and alpha is dropped, as in the render.</summary>
|
||||
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;
|
||||
}
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// 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.
|
||||
/// </summary>
|
||||
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<ImageBuffer> 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;
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,76 @@
|
||||
using Titano.Core.Project;
|
||||
|
||||
namespace Titano.App.Editor;
|
||||
|
||||
/// <summary>
|
||||
/// 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 <c>mergeKey</c> (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 <see cref="Capacity"/>.
|
||||
/// </summary>
|
||||
public sealed class ProjectHistory(ProjectSettings initial, int capacity = 200)
|
||||
{
|
||||
private readonly LinkedList<ProjectSettings> _undo = new();
|
||||
private readonly Stack<ProjectSettings> _redo = new();
|
||||
private string? _runKey;
|
||||
|
||||
/// <summary>The project, after any edit, undo, redo or <see cref="Replace"/>.</summary>
|
||||
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;
|
||||
|
||||
/// <summary>Makes <paramref name="next"/> current as an undoable step; false (and nothing recorded) when it stores the same values.</summary>
|
||||
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);
|
||||
}
|
||||
|
||||
/// <summary>Adopts a project changed outside the editor (analysis advice, a merge on save) without an undo step.</summary>
|
||||
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;
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,42 @@
|
||||
using System.Text.Json.Nodes;
|
||||
using Titano.Core.Project;
|
||||
|
||||
namespace Titano.App.Editor;
|
||||
|
||||
/// <summary>
|
||||
/// Three-way merge of projects: the editor's edit (<c>ours</c>) and a change stored meanwhile by someone else
|
||||
/// (<c>theirs</c>: an analysis filling Auto values, another client through the server), both made from <c>basis</c>.
|
||||
/// 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).
|
||||
/// </summary>
|
||||
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() ?? "{}");
|
||||
}
|
||||
|
||||
/// <summary>True when both projects store the same values.</summary>
|
||||
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;
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,89 @@
|
||||
using Titano.Core.Project;
|
||||
|
||||
namespace Titano.App.Editor;
|
||||
|
||||
/// <summary>
|
||||
/// 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). <see cref="FlushAsync"/> skips the wait and returns once
|
||||
/// everything scheduled is stored. A failed write is reported and the next change tries again.
|
||||
/// </summary>
|
||||
public sealed class ProjectSaver(Func<ProjectSettings, Task> save, TimeSpan delay)
|
||||
{
|
||||
private readonly Lock _gate = new();
|
||||
private ProjectSettings? _pending;
|
||||
private bool _running;
|
||||
private Task _loop = Task.CompletedTask;
|
||||
private CancellationTokenSource _wait = new();
|
||||
|
||||
/// <summary>A write failed (raised on the context that scheduled the write).</summary>
|
||||
public event Action<Exception>? 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();
|
||||
}
|
||||
|
||||
/// <summary>Stores what is pending now, without the delay.</summary>
|
||||
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);
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -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;
|
||||
|
||||
/// <summary>A synthetic sunset with flicker and two exposure steps, as the editor tests open it.</summary>
|
||||
internal static class EditorFixture
|
||||
{
|
||||
private static readonly Lazy<SyntheticSequence> 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,
|
||||
});
|
||||
|
||||
/// <summary>The sequence as <see cref="ISequenceSource.LoadAsync"/> returns it, with a deep analysis when asked.</summary>
|
||||
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, []);
|
||||
}
|
||||
|
||||
/// <summary>Synthetic frames as the deep analysis reads them.</summary>
|
||||
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<ImageBuffer?> LoadAsync(int index, int maxLongEdge, CancellationToken cancellationToken) =>
|
||||
ValueTask.FromResult<ImageBuffer?>(sequence.Render(index));
|
||||
}
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// An in-memory <see cref="ISequenceSource"/>: synthetic pixels, a stored project with the same merge rule as the
|
||||
/// archive, recorded jobs. <see cref="ExternalEdit"/> simulates someone else changing the stored project before a save.
|
||||
/// </summary>
|
||||
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<ArchiveJobRequest> Submitted { get; } = [];
|
||||
|
||||
public int Saves { get; private set; }
|
||||
|
||||
public int Decodes { get; private set; }
|
||||
|
||||
/// <summary>Applied to the stored project before the next save reads it.</summary>
|
||||
public Func<ProjectSettings, ProjectSettings>? ExternalEdit { get; set; }
|
||||
|
||||
/// <summary>Thrown by the next save.</summary>
|
||||
public Exception? FailNextSave { get; set; }
|
||||
|
||||
/// <summary>Awaited by every decode (tests of cancellation).</summary>
|
||||
public Task DecodeGate { get; set; } = Task.CompletedTask;
|
||||
|
||||
public Task<SequenceContent> LoadAsync(CancellationToken cancellationToken) => Task.FromResult(Content with { Project = Stored });
|
||||
|
||||
public async ValueTask<ImageBuffer?> DecodeAsync(int index, int maxLongEdge, DecodePurpose purpose, CancellationToken cancellationToken)
|
||||
{
|
||||
await DecodeGate.WaitAsync(cancellationToken);
|
||||
Decodes++;
|
||||
return await _pixels.GetFrameAsync(index, maxLongEdge, purpose, cancellationToken);
|
||||
}
|
||||
|
||||
public Task<ProjectSettings> SaveProjectAsync(ProjectSettings project, ProjectSettings basis, CancellationToken cancellationToken)
|
||||
{
|
||||
if (FailNextSave is { } failure)
|
||||
{
|
||||
FailNextSave = null;
|
||||
return Task.FromException<ProjectSettings>(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<Guid> SubmitAsync(ArchiveJobRequest request, CancellationToken cancellationToken)
|
||||
{
|
||||
Submitted.Add(request);
|
||||
return Task.FromResult(Guid.CreateVersion7());
|
||||
}
|
||||
|
||||
public ValueTask DisposeAsync() => ValueTask.CompletedTask;
|
||||
}
|
||||
@@ -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);
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -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);
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,121 @@
|
||||
using Titano.App.Editor;
|
||||
using Titano.Core.Imaging;
|
||||
using Titano.Core.Project;
|
||||
using Titano.Testing;
|
||||
|
||||
namespace Titano.App.Tests.Editor;
|
||||
|
||||
/// <summary>The corrected-frame preview through the render pipeline at preview size.</summary>
|
||||
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<double>();
|
||||
var original = new List<double>();
|
||||
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<OperationCanceledException>(() => 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);
|
||||
}
|
||||
|
||||
/// <summary>Standard deviation of the frame-to-frame differences (flicker, not the slow ramp).</summary>
|
||||
private static double Spread(List<double> 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);
|
||||
}
|
||||
}
|
||||
@@ -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);
|
||||
}
|
||||
}
|
||||
@@ -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;
|
||||
|
||||
/// <summary>Project merges, and the local source over a real SQLite index and archive folder.</summary>
|
||||
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<InvalidOperationException>(() => 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<InvalidDataException>(() => source.LoadAsync(context.Cancellation));
|
||||
Assert.Equal("{ not json", File.ReadAllText(path));
|
||||
}
|
||||
}
|
||||
|
||||
/// <summary>A temporary archive with one adopted folder of synthetic JPEGs, indexed.</summary>
|
||||
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);
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,133 @@
|
||||
using System.Collections.Concurrent;
|
||||
using Titano.App.Editor;
|
||||
using Titano.Core.Project;
|
||||
using Titano.Testing;
|
||||
|
||||
namespace Titano.App.Tests.Editor;
|
||||
|
||||
/// <summary>Debouncing and cancellation of preview work, and the coalescing project writer.</summary>
|
||||
public sealed class SchedulingTests
|
||||
{
|
||||
[Test]
|
||||
public async Task OnlyTheLastOfRapidRequestsRuns()
|
||||
{
|
||||
using var runner = new LatestRunner(TimeSpan.FromMilliseconds(80));
|
||||
var ran = new ConcurrentQueue<int>();
|
||||
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<InvalidOperationException>(() => 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<int?>();
|
||||
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<int?>();
|
||||
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);
|
||||
}
|
||||
}
|
||||
Reference in new issue
Block a user