fix(imaging): record only what decoding proves and find GPR JPEGs across sequences
- Preview sizes come from probes and unscaled decodes: a size-limited decode picks the smallest preview that reaches its edge (CR3 PRVW 1620x1080), so renders and deep passes record its tier without a size and the output cap keeps the largest preview's. - A read error is not an undecodable file: ImageDecoder.Decode throws the IOException when a provider could not read the file and none produced pixels. Deep passes never flag frames they could not read or that vanished, and frames only an interrupted earlier run found undecodable are decoded again before they are flagged. - Renders resolve stand-ins only for frames the pipeline asks for; frames the read-ahead meets on the way (skipped by speed-ups) are left alone. - A GoPro GPR finds its JPEG in the index when an import or upload kept it in a sequence of its own (same original stem and capture time), for probes, deep analysis, renders, thumbnails, proxies and the editor; GPRs whose JPEG is indexed later are probed again. - Thumbnails judge an embedded preview by its own JPEG header. - PostgreSQL writes tier fields 1000 frames per statement. 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
+689
-111
No files matched your search
@@ -65,8 +65,10 @@ internal sealed class RenderJobHandler(TitanoDatabase db, ProcessingOptions opti
|
||||
if (targets.Count == 0) return;
|
||||
|
||||
var frames = await SequenceFrames.LoadAsync(db, archive, sequence, parameters.UseProxies, cancellation).ConfigureAwait(false);
|
||||
var companions = await GprCompanions.FindAsync(db, archive, frames.Frames, cancellation).ConfigureAwait(false);
|
||||
IReadOnlyList<TargetOutcome> outcomes;
|
||||
await using (var render = new SequenceRender(frames, project, SequenceAnalyser.ReadReport(folder, frames.Count), options, originals: !parameters.UseProxies))
|
||||
await using (var render = new SequenceRender(frames, project, SequenceAnalyser.ReadReport(folder, frames.Count), options, originals: !parameters.UseProxies,
|
||||
companions))
|
||||
{
|
||||
outcomes = await render.RenderAllAsync(
|
||||
targets,
|
||||
|
||||
@@ -30,6 +30,9 @@ internal sealed record AnalysisOutcome(Sequence Sequence, int Frames, int Warnin
|
||||
/// </summary>
|
||||
internal sealed class SequenceAnalyser(TitanoDatabase db, ProcessingOptions options, SequenceWriter writer, ArchiveIndexer indexer, ProjectStore projects)
|
||||
{
|
||||
/// <summary>Long edge of the decode that confirms a frame an interrupted earlier run found undecodable.</summary>
|
||||
private const int ConfirmLongEdge = 64;
|
||||
|
||||
private readonly FrameTiers _tiers = new(db, writer, indexer);
|
||||
|
||||
/// <summary>Analyses one sequence; null when it is no longer in the index.</summary>
|
||||
@@ -43,15 +46,15 @@ internal sealed class SequenceAnalyser(TitanoDatabase db, ProcessingOptions opti
|
||||
var folder = paths.Sequence(sequence.Folder);
|
||||
var frames = await SequenceFrames.LoadAsync(db, archive, sequence, useProxies: false, cancellationToken).ConfigureAwait(false);
|
||||
// Format tiers (BRIEF §10.1): frames indexed since the last analysis are probed with this host's decoders.
|
||||
var probed = _tiers.ProbeUnknown(folder, ManifestStore.ReadSequence(folder.ManifestPath), cancellationToken);
|
||||
var probed = await _tiers.ProbeUnknownAsync(archive, folder, ManifestStore.ReadSequence(folder.ManifestPath), cancellationToken).ConfigureAwait(false);
|
||||
frames = WithTiers(frames, await _tiers.RecordAsync(archive, sequence, probed, cancellationToken).ConfigureAwait(false));
|
||||
var metadata = frames.LightFrames();
|
||||
|
||||
DeepAnalysisReport? report;
|
||||
if (deep)
|
||||
{
|
||||
(report, var observed) = await AnalyseDeepAsync(frames, metadata, folder, progress, cancellationToken).ConfigureAwait(false);
|
||||
frames = WithTiers(frames, await _tiers.RecordAsync(archive, sequence, Decoded(frames, report, observed), cancellationToken).ConfigureAwait(false));
|
||||
(report, var decoded) = await AnalyseDeepAsync(archive, frames, metadata, folder, progress, cancellationToken).ConfigureAwait(false);
|
||||
frames = WithTiers(frames, await _tiers.RecordAsync(archive, sequence, decoded, cancellationToken).ConfigureAwait(false));
|
||||
metadata = [.. metadata.Select((m, i) => m with { Flags = frames.Frames[i].Flags })];
|
||||
}
|
||||
else
|
||||
@@ -128,35 +131,49 @@ internal sealed class SequenceAnalyser(TitanoDatabase db, ProcessingOptions opti
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// What the deep pass learnt about each frame's tier: the tier this run decoded it at, else (for frames analysed by
|
||||
/// an interrupted earlier run) unavailable when the report says it did not decode. Missing and corrupt frames are not
|
||||
/// decoded at all, so they say nothing.
|
||||
/// What the deep pass learnt about each frame's tier, by frame id: the tier this run decoded it at. A frame this run
|
||||
/// tried to read and recorded nothing for vanished or could not be read, which says nothing about its content. A frame
|
||||
/// only an interrupted earlier run analysed and found undecodable is decoded again (small) to confirm it: that run's
|
||||
/// failure may have been a read error too. Missing and corrupt frames are not decoded at all.
|
||||
/// </summary>
|
||||
private static Dictionary<Guid, FileTier> Decoded(SequenceFrames frames, DeepAnalysisReport report, IReadOnlyDictionary<int, FileTier> observed)
|
||||
private Dictionary<Guid, FileTier> Decoded(SequenceFrames frames, DeepAnalysisReport report, SequenceFrameSource source,
|
||||
IReadOnlyDictionary<Guid, string> companions, CancellationToken cancellationToken)
|
||||
{
|
||||
var tiers = new Dictionary<Guid, FileTier>();
|
||||
foreach (var (index, tier) in observed) tiers[frames.Frames[index].Id] = tier;
|
||||
foreach (var (index, tier) in source.Observed) tiers[frames.Frames[index].Id] = tier;
|
||||
var read = source.Read.ToHashSet();
|
||||
foreach (var index in report.Issues.Undecodable)
|
||||
{
|
||||
if (index >= frames.Count || observed.ContainsKey(index) || frames.Files[index] is null
|
||||
if (index >= frames.Count || read.Contains(index) || frames.Files[index] is not { } path
|
||||
|| (frames.Frames[index].Flags & SequenceFrames.NotAnalysed) != 0) continue;
|
||||
var frame = frames.Frames[index];
|
||||
tiers[frame.Id] = new FileTier(frame.Format, DecodeTier.Unavailable, null, "no decoder produced pixels", 0, 0);
|
||||
try
|
||||
{
|
||||
using var decoded = options.Archive.Decoder.Decode(
|
||||
new DecodeRequest(path, ConfirmLongEdge, DecodePurpose.Analysis, Companion: companions.GetValueOrDefault(frame.Id)), cancellationToken);
|
||||
if (decoded is not null) tiers[frame.Id] = new FileTier(frame.Format, decoded.Tier, decoded.Decoder, null, 0, 0);
|
||||
else if (File.Exists(path)) tiers[frame.Id] = new FileTier(frame.Format, DecodeTier.Unavailable, null, "no decoder produced pixels", 0, 0);
|
||||
}
|
||||
catch (Exception ex) when (ex is IOException or UnauthorizedAccessException)
|
||||
{
|
||||
Core.Diagnostics.Log.Warning("processing", $"Frame {path} could not be read", ex);
|
||||
}
|
||||
}
|
||||
return tiers;
|
||||
}
|
||||
|
||||
private async Task<(DeepAnalysisReport Report, IReadOnlyDictionary<int, FileTier> Observed)> AnalyseDeepAsync(SequenceFrames frames,
|
||||
private async Task<(DeepAnalysisReport Report, Dictionary<Guid, FileTier> Decoded)> AnalyseDeepAsync(Archive archive, SequenceFrames frames,
|
||||
IReadOnlyList<LightFrame> metadata, SequencePaths folder, IProgress<AnalysisProgress>? progress, CancellationToken cancellationToken)
|
||||
{
|
||||
var file = new DeepAnalysisCheckpointFile(Path.Combine(folder.MetaDirectory, DeepAnalysisCheckpointFile.FileName));
|
||||
var companions = await GprCompanions.FindAsync(db, archive, frames.Frames, cancellationToken).ConfigureAwait(false);
|
||||
DeepAnalysisReport report;
|
||||
IReadOnlyDictionary<int, FileTier> observed;
|
||||
await using (var source = new SequenceFrameSource(frames, metadata, options.Archive.Decoder) { Originals = true })
|
||||
Dictionary<Guid, FileTier> decoded;
|
||||
await using (var source = new SequenceFrameSource(frames, metadata, options.Archive.Decoder) { Originals = true, Companions = companions })
|
||||
{
|
||||
report = await DeepAnalyzer.AnalyzeAsync(source, options.DeepAnalysis, new FramesCheckpoint(file, ContentKey(frames)), progress, cancellationToken)
|
||||
.ConfigureAwait(false);
|
||||
observed = source.Observed.ToDictionary();
|
||||
decoded = Decoded(frames, report, source, companions, cancellationToken);
|
||||
}
|
||||
report.Save(folder.AnalysisPath);
|
||||
file.Delete();
|
||||
@@ -166,7 +183,7 @@ internal sealed class SequenceAnalyser(TitanoDatabase db, ProcessingOptions opti
|
||||
for (var i = 0; i < stats.Length; i++)
|
||||
stats[i] = new FrameLightStats(frames.Frames[i].Id, columns.LogLuminance[i], columns.ClipLow[i], columns.ClipHigh[i]);
|
||||
await db.Frames.UpdateLightStatsAsync(stats, cancellationToken).ConfigureAwait(false);
|
||||
return (report, observed);
|
||||
return (report, decoded);
|
||||
}
|
||||
|
||||
/// <summary>Identity of the analysed content: frames replaced or renumbered since a checkpoint was written (with the
|
||||
|
||||
@@ -15,7 +15,8 @@ namespace Titano.Data.Processing;
|
||||
/// still cannot be decoded stops the render with <see cref="FrameUnreadableKey"/>, unless the render gave
|
||||
/// <see cref="RenderExcluded"/>: then the nearest planned frame that decodes stands in for it (BRIEF §8.2 tier 3: one
|
||||
/// unreadable frame never stops a render) and it is listed in <see cref="StandIns"/>. Render reads go through a
|
||||
/// <see cref="FrameWindow"/> per decode size, which decodes ahead in index order and spills under memory pressure.
|
||||
/// <see cref="FrameWindow"/> per decode size, which decodes ahead in index order and spills under memory pressure; only
|
||||
/// frames the pipeline asks for get a stand-in, not those the read-ahead met on the way (a speed-up skips them).
|
||||
/// </summary>
|
||||
internal sealed class SequenceFrameSource : IFrameSource, IRenderFrameSource, IAsyncDisposable
|
||||
{
|
||||
@@ -33,6 +34,7 @@ internal sealed class SequenceFrameSource : IFrameSource, IRenderFrameSource, IA
|
||||
private readonly ConcurrentDictionary<int, FileTier> _observed = new();
|
||||
private readonly ConcurrentDictionary<int, byte> _standIns = new();
|
||||
private readonly ConcurrentDictionary<int, byte> _failed = new();
|
||||
private readonly ConcurrentDictionary<int, byte> _read = new();
|
||||
private FrameWindow? _window;
|
||||
private (int Edge, DecodePurpose Purpose) _windowKey;
|
||||
|
||||
@@ -56,11 +58,24 @@ internal sealed class SequenceFrameSource : IFrameSource, IRenderFrameSource, IA
|
||||
public IReadOnlySet<int>? RenderExcluded { get; init; }
|
||||
|
||||
/// <summary>
|
||||
/// Decode tier each frame decoded at, or <see cref="DecodeTier.Unavailable"/> when nothing decoded its file (sizes
|
||||
/// oriented, as <see cref="DecodedImage"/> reports them); recorded only for <see cref="Originals"/>.
|
||||
/// The JPEG a GoPro wrote with each GPR frame that is not beside it, by frame id (<see cref="DecodeRequest.Companion"/>).
|
||||
/// </summary>
|
||||
public IReadOnlyDictionary<Guid, string>? Companions { get; init; }
|
||||
|
||||
/// <summary>
|
||||
/// Decode tier each frame decoded at, or <see cref="DecodeTier.Unavailable"/> when the decoders read its file and none
|
||||
/// produced pixels; recorded only for <see cref="Originals"/>. Sizes are oriented and given only where they are the
|
||||
/// full size the tier delivers: a size-limited decode of a preview picks the smallest one that reaches the limit, so
|
||||
/// it reports none and the stored size of the largest preview stays.
|
||||
/// </summary>
|
||||
public IReadOnlyDictionary<int, FileTier> Observed => _observed;
|
||||
|
||||
/// <summary>
|
||||
/// Positions whose file this source tried to decode, whatever came of it (a read error or a vanished file records no
|
||||
/// tier in <see cref="Observed"/>).
|
||||
/// </summary>
|
||||
public IReadOnlyCollection<int> Read => [.. _read.Keys];
|
||||
|
||||
/// <summary>Positions a render showed a neighbour for because they did not decode, ascending.</summary>
|
||||
public IReadOnlyList<int> StandIns => [.. _standIns.Keys.Order()];
|
||||
|
||||
@@ -90,11 +105,11 @@ internal sealed class SequenceFrameSource : IFrameSource, IRenderFrameSource, IA
|
||||
{
|
||||
if (_window is not null) await _window.DisposeAsync().ConfigureAwait(false);
|
||||
_windowKey = (maxLongEdge, purpose);
|
||||
_window = new FrameWindow(Count, (i, ct) => ValueTask.FromResult(DecodeForRender(i, maxLongEdge, purpose, ct)), _windowOptions);
|
||||
_window = new FrameWindow(Count, (i, ct) => ValueTask.FromResult(Load(i, maxLongEdge, purpose, ct)), _windowOptions);
|
||||
}
|
||||
_window.MoveTo(index);
|
||||
using var lease = await _window.GetAsync(index, cancellationToken).ConfigureAwait(false);
|
||||
return lease.Image.Clone();
|
||||
return await ReadAsync(_window, index, cancellationToken).ConfigureAwait(false)
|
||||
?? await StandInAsync(_window, index, cancellationToken).ConfigureAwait(false);
|
||||
}
|
||||
finally
|
||||
{
|
||||
@@ -116,28 +131,50 @@ internal sealed class SequenceFrameSource : IFrameSource, IRenderFrameSource, IA
|
||||
}
|
||||
}
|
||||
|
||||
private ImageBuffer DecodeForRender(int index, int maxLongEdge, DecodePurpose purpose, CancellationToken cancellationToken)
|
||||
/// <summary>
|
||||
/// The window's loader, for the pipeline's reads and the read-ahead alike: the decoded frame, else
|
||||
/// <see cref="NotDecodedException"/>, remembered so the read-ahead does not decode the file again.
|
||||
/// </summary>
|
||||
private ImageBuffer Load(int index, int maxLongEdge, DecodePurpose purpose, CancellationToken cancellationToken)
|
||||
{
|
||||
if (Decode(index, maxLongEdge, purpose, cancellationToken) is { } decoded) return decoded.Pixels;
|
||||
// A frame the plan leaves out is reached only by the window's read-ahead, which never hands it to the pipeline.
|
||||
if (RenderExcluded is not { } excluded || excluded.Contains(index)) throw Unreadable(index);
|
||||
// Flagged frames never get here, so this one became unreadable after it was last analysed (or is beyond this host's
|
||||
// decoders): the nearest planned frame stands in, earlier first, and the render job records what decoding found.
|
||||
if (!_failed.ContainsKey(index) && Decode(index, maxLongEdge, purpose, cancellationToken) is { } decoded) return decoded.Pixels;
|
||||
_failed.TryAdd(index, 0);
|
||||
throw new NotDecodedException();
|
||||
}
|
||||
|
||||
/// <summary>A copy of the window's frame, or null when it does not decode.</summary>
|
||||
private static async ValueTask<ImageBuffer?> ReadAsync(FrameWindow window, int index, CancellationToken cancellationToken)
|
||||
{
|
||||
try
|
||||
{
|
||||
using var lease = await window.GetAsync(index, cancellationToken).ConfigureAwait(false);
|
||||
return lease.Image.Clone();
|
||||
}
|
||||
catch (NotDecodedException)
|
||||
{
|
||||
return null;
|
||||
}
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// The pixels shown for a frame the pipeline asked for that does not decode. Flagged frames are not planned, so this
|
||||
/// one became unreadable after it was last analysed (or is beyond this host's decoders): the nearest planned frame
|
||||
/// that decodes stands in, earlier first, and the render job records what decoding found. Without
|
||||
/// <see cref="RenderExcluded"/>, or when nothing decodes, the render stops.
|
||||
/// </summary>
|
||||
private async ValueTask<ImageBuffer> StandInAsync(FrameWindow window, int index, CancellationToken cancellationToken)
|
||||
{
|
||||
if (RenderExcluded is not { } excluded || excluded.Contains(index)) throw Unreadable(index);
|
||||
for (var distance = 1; distance < Count; distance++)
|
||||
{
|
||||
ReadOnlySpan<int> neighbours = [index - distance, index + distance];
|
||||
foreach (var neighbour in neighbours)
|
||||
for (var side = -1; side <= 1; side += 2)
|
||||
{
|
||||
var neighbour = index + side * distance;
|
||||
if (neighbour < 0 || neighbour >= Count || excluded.Contains(neighbour) || _failed.ContainsKey(neighbour)) continue;
|
||||
if (Decode(neighbour, maxLongEdge, purpose, cancellationToken) is not { } standIn)
|
||||
{
|
||||
_failed.TryAdd(neighbour, 0);
|
||||
continue;
|
||||
}
|
||||
if (await ReadAsync(window, neighbour, cancellationToken).ConfigureAwait(false) is not { } standIn) continue;
|
||||
_standIns.TryAdd(index, 0);
|
||||
Core.Diagnostics.Log.Warning("processing", $"Frame {index + 1} ({_frames.Frames[index].FileName}) does not decode; frame {neighbour + 1} stands in");
|
||||
return standIn.Pixels;
|
||||
return standIn;
|
||||
}
|
||||
}
|
||||
throw Unreadable(index);
|
||||
@@ -146,23 +183,29 @@ internal sealed class SequenceFrameSource : IFrameSource, IRenderFrameSource, IA
|
||||
private DecodedImage? Decode(int index, int maxLongEdge, DecodePurpose purpose, CancellationToken cancellationToken)
|
||||
{
|
||||
if (_frames.Files[index] is not { } file) return null;
|
||||
_read.TryAdd(index, 0);
|
||||
var frame = _frames.Frames[index];
|
||||
DecodedImage? decoded;
|
||||
try
|
||||
{
|
||||
decoded = _decoder.Decode(new DecodeRequest(file, maxLongEdge, purpose), cancellationToken);
|
||||
var companion = Companions?.GetValueOrDefault(frame.Id);
|
||||
decoded = _decoder.Decode(new DecodeRequest(file, maxLongEdge, purpose, Companion: companion), cancellationToken);
|
||||
}
|
||||
catch (Exception ex) when (ex is IOException or UnauthorizedAccessException)
|
||||
{
|
||||
// The file was not read (locked, vanished, a network read failed): that says nothing about its content.
|
||||
Core.Diagnostics.Log.Warning("processing", $"Frame {file} could not be read", ex);
|
||||
return null;
|
||||
}
|
||||
// A file that vanished is missing, not undecodable (a verify flags it); only what decoding found is recorded.
|
||||
if (Originals && (decoded is not null || File.Exists(file)))
|
||||
{
|
||||
var format = _frames.Frames[index].Format;
|
||||
_observed[index] = decoded is null
|
||||
? new FileTier(format, DecodeTier.Unavailable, null, "no decoder produced pixels", 0, 0)
|
||||
: new FileTier(format, decoded.Tier, decoded.Decoder, null, decoded.SourceWidth, decoded.SourceHeight);
|
||||
_observed[index] = decoded switch
|
||||
{
|
||||
null => new FileTier(frame.Format, DecodeTier.Unavailable, null, "no decoder produced pixels", 0, 0),
|
||||
{ Tier: DecodeTier.EmbeddedPreview } when maxLongEdge > 0 => new FileTier(frame.Format, decoded.Tier, decoded.Decoder, null, 0, 0),
|
||||
_ => new FileTier(frame.Format, decoded.Tier, decoded.Decoder, null, decoded.SourceWidth, decoded.SourceHeight),
|
||||
};
|
||||
}
|
||||
return decoded;
|
||||
}
|
||||
@@ -196,4 +239,7 @@ internal sealed class SequenceFrameSource : IFrameSource, IRenderFrameSource, IA
|
||||
return new RenderException(FrameUnreadableKey, $"Frame {index + 1} ({name}) could not be decoded.",
|
||||
[(index + 1).ToString(CultureInfo.InvariantCulture), name]);
|
||||
}
|
||||
|
||||
/// <summary>A frame of the window that does not decode; <see cref="GetFrameAsync"/> resolves it, nothing else sees it.</summary>
|
||||
private sealed class NotDecodedException : Exception;
|
||||
}
|
||||
@@ -32,7 +32,9 @@ internal sealed class SequenceRender : IAsyncDisposable
|
||||
|
||||
/// <param name="analysis">The sequence's deep report, when current: its luminance spares the preparation a decode pass.</param>
|
||||
/// <param name="originals">The frames' files are the archive's originals (no proxies), so <see cref="Decoded"/> describes them.</param>
|
||||
public SequenceRender(SequenceFrames frames, ProjectSettings project, DeepAnalysisReport? analysis, ProcessingOptions options, bool originals = false)
|
||||
/// <param name="companions">The JPEG of each GPR frame kept in another sequence, by frame id (<see cref="Archiving.GprCompanions"/>).</param>
|
||||
public SequenceRender(SequenceFrames frames, ProjectSettings project, DeepAnalysisReport? analysis, ProcessingOptions options, bool originals = false,
|
||||
IReadOnlyDictionary<Guid, string>? companions = null)
|
||||
{
|
||||
var metadata = frames.LightFrames();
|
||||
var planned = project with { ExcludedFrames = [.. project.ExcludedFrames.Union(frames.NotRenderable()).Order()] };
|
||||
@@ -41,6 +43,7 @@ internal sealed class SequenceRender : IAsyncDisposable
|
||||
{
|
||||
Originals = originals,
|
||||
RenderExcluded = planned.ExcludedFrames.ToHashSet(),
|
||||
Companions = companions,
|
||||
};
|
||||
_processing = options;
|
||||
_options = options.Render;
|
||||
|
||||
Reference in new issue
Block a user