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
@@ -47,6 +47,7 @@ public sealed class ArchiveImages
|
||||
var manifest = ManifestStore.ReadSequence(folder.ManifestPath);
|
||||
int built = 0, reused = 0, undecodable = 0;
|
||||
var tiers = new Dictionary<Guid, FileTier>();
|
||||
var companions = await GprCompanions.FindAsync(_db, archive, manifest.Frames, cancellationToken).ConfigureAwait(false);
|
||||
// Thumbnail writers exclude each other only; ingest keeps committing meanwhile.
|
||||
using (await LeaseFile.AcquireAsync(paths.LocksDirectory, $"thumbs-{sequence.Id:N}", _writer.Options.Owner, _writer.Options.LeaseWait,
|
||||
_writer.Options.LeaseDuration, cancellationToken).ConfigureAwait(false))
|
||||
@@ -63,7 +64,8 @@ public sealed class ArchiveImages
|
||||
reused++;
|
||||
continue;
|
||||
}
|
||||
if (Render(folder, frame, tiers, () => _renderer.Thumbnail(folder, frame, cancellationToken)) is { } image)
|
||||
var companion = companions.GetValueOrDefault(frame.Id);
|
||||
if (Render(folder, frame, companion, tiers, () => _renderer.Thumbnail(folder, frame, cancellationToken, companion)) is { } image)
|
||||
{
|
||||
pack.Append(frame.I, image.Width, image.Height, fingerprint, image.Jpeg);
|
||||
built++;
|
||||
@@ -98,6 +100,7 @@ public sealed class ArchiveImages
|
||||
Directory.CreateDirectory(folder.Proxies);
|
||||
int built = 0, reused = 0, undecodable = 0;
|
||||
var tiers = new Dictionary<Guid, FileTier>();
|
||||
var companions = await GprCompanions.FindAsync(_db, archive, manifest.Frames, cancellationToken).ConfigureAwait(false);
|
||||
var cadence = new Cadence();
|
||||
for (var i = startFrame; i < manifest.Frames.Count; i++)
|
||||
{
|
||||
@@ -109,7 +112,8 @@ public sealed class ArchiveImages
|
||||
reused++;
|
||||
continue;
|
||||
}
|
||||
if (Render(folder, frame, tiers, () => _renderer.Proxy(folder, frame, manifest.Camera, policy, cancellationToken)) is { } image)
|
||||
var companion = companions.GetValueOrDefault(frame.Id);
|
||||
if (Render(folder, frame, companion, tiers, () => _renderer.Proxy(folder, frame, manifest.Camera, policy, cancellationToken, companion)) is { } image)
|
||||
{
|
||||
AtomicFile.WriteAllBytes(path, image.Jpeg);
|
||||
built++;
|
||||
@@ -144,7 +148,7 @@ public sealed class ArchiveImages
|
||||
/// <paramref name="tiers"/>: unavailable when nothing decoded a file that is there (a read error proves nothing), and
|
||||
/// the probed tier when a frame flagged undecodable decoded after all.
|
||||
/// </summary>
|
||||
private EncodedImage? Render(SequencePaths folder, SidecarFrame frame, Dictionary<Guid, FileTier> tiers, Func<EncodedImage?> render)
|
||||
private EncodedImage? Render(SequencePaths folder, SidecarFrame frame, string? companion, Dictionary<Guid, FileTier> tiers, Func<EncodedImage?> render)
|
||||
{
|
||||
var path = folder.File(frame.File);
|
||||
EncodedImage? image;
|
||||
@@ -152,7 +156,7 @@ public sealed class ArchiveImages
|
||||
{
|
||||
image = render();
|
||||
}
|
||||
catch (IOException ex)
|
||||
catch (Exception ex) when (ex is IOException or UnauthorizedAccessException)
|
||||
{
|
||||
Core.Diagnostics.Log.Warning("archive", "Frame image could not be rendered", ex);
|
||||
return null;
|
||||
@@ -168,17 +172,17 @@ public sealed class ArchiveImages
|
||||
}
|
||||
else if ((frame.Flags & FrameFlags.Undecodable) != 0)
|
||||
{
|
||||
tiers[frame.Id] = Reprobe(path, frame, image.Tier);
|
||||
tiers[frame.Id] = Reprobe(path, frame, companion, image.Tier);
|
||||
}
|
||||
return image;
|
||||
}
|
||||
|
||||
/// <summary>The tier of a frame that decoded after it was flagged undecodable (the image's own tier when probing finds none).</summary>
|
||||
private FileTier Reprobe(string path, SidecarFrame frame, DecodeTier decoded)
|
||||
private FileTier Reprobe(string path, SidecarFrame frame, string? companion, DecodeTier decoded)
|
||||
{
|
||||
try
|
||||
{
|
||||
if (_writer.Options.Decoder.Probe(path, frame.Format) is { Tier: not DecodeTier.Unavailable } tier) return tier;
|
||||
if (_writer.Options.Decoder.Probe(path, frame.Format, companion) is { Tier: not DecodeTier.Unavailable } tier) return tier;
|
||||
}
|
||||
catch (Exception ex) when (ex is IOException or UnauthorizedAccessException)
|
||||
{
|
||||
|
||||
@@ -17,20 +17,27 @@ namespace Titano.Data.Archiving;
|
||||
internal sealed class FrameTiers(TitanoDatabase db, SequenceWriter writer, ArchiveIndexer indexer)
|
||||
{
|
||||
/// <summary>
|
||||
/// The tier of every frame of <paramref name="manifest"/> whose tier is not known yet, probed with the host's decoders
|
||||
/// without decoding pixels; missing and unreadable files are left for later.
|
||||
/// The tier of every frame of <paramref name="manifest"/> whose tier is not known yet, that decodes only through a
|
||||
/// preview whose size is not known (the output cap needs it), or that is a GPR whose JPEG in another sequence
|
||||
/// (<see cref="GprCompanions"/>; an upload may index it after the GPR was probed) is larger than its stored preview,
|
||||
/// probed with the host's decoders without decoding pixels; missing and unreadable files are left for later.
|
||||
/// </summary>
|
||||
public Dictionary<Guid, FileTier> ProbeUnknown(SequencePaths folder, SequenceManifest manifest, CancellationToken cancellationToken)
|
||||
public async Task<Dictionary<Guid, FileTier>> ProbeUnknownAsync(Archive archive, SequencePaths folder, SequenceManifest manifest,
|
||||
CancellationToken cancellationToken)
|
||||
{
|
||||
var tiers = new Dictionary<Guid, FileTier>();
|
||||
foreach (var frame in manifest.Frames)
|
||||
var present = manifest.Frames.Where(f => (f.Flags & FrameFlags.Missing) == 0).ToList();
|
||||
var companions = await GprCompanions.FindWithSizesAsync(db, archive, present, cancellationToken).ConfigureAwait(false);
|
||||
var unknown = present.Where(f => f.Tier == 0 || (f.Tier == DecodeTier.EmbeddedPreview && f.PreviewW is null)
|
||||
|| (companions.TryGetValue(f.Id, out var jpeg)
|
||||
&& int.Max(f.PreviewW ?? 0, f.PreviewH ?? 0) < int.Max(jpeg.Width, jpeg.Height)));
|
||||
foreach (var frame in unknown)
|
||||
{
|
||||
if (frame.Tier != 0 || (frame.Flags & FrameFlags.Missing) != 0) continue;
|
||||
cancellationToken.ThrowIfCancellationRequested();
|
||||
var path = folder.File(frame.File);
|
||||
try
|
||||
{
|
||||
var tier = writer.Options.Decoder.Probe(path, frame.Format);
|
||||
var tier = writer.Options.Decoder.Probe(path, frame.Format, companions.TryGetValue(frame.Id, out var companion) ? companion.Path : null);
|
||||
Core.Diagnostics.Log.Debug("archive", $"{frame.File}: {tier.Tier} via {tier.Decoder ?? "none"}{(tier.Reason is { } why ? $" ({why})" : "")}");
|
||||
tiers[frame.Id] = tier;
|
||||
}
|
||||
|
||||
@@ -0,0 +1,63 @@
|
||||
using Titano.Core.Archiving;
|
||||
using Titano.Core.Imaging;
|
||||
using Titano.Core.Model;
|
||||
|
||||
namespace Titano.Data.Archiving;
|
||||
|
||||
/// <summary>A GPR's JPEG: its absolute path and its size as the index has it (before orientation).</summary>
|
||||
internal readonly record struct GprCompanion(string Path, int Width, int Height);
|
||||
|
||||
/// <summary>
|
||||
/// The JPEG a GoPro writes with each GPR, the server's pixel source for GPR (BRIEF §8.1; VC-5 is out of scope). In a
|
||||
/// folder adopted in place it sits beside the GPR, where the decoder finds it itself. A card import or an upload splits
|
||||
/// sessions per file format (DECISIONS 2026-10-02), so there it is a frame of another sequence: the archive's JPEG frame
|
||||
/// whose original name has the GPR's stem and whose capture time is the GPR's (within a second). Looked up in the index
|
||||
/// when frames are decoded, so renaming or moving either sequence never breaks the pair.
|
||||
/// </summary>
|
||||
public static class GprCompanions
|
||||
{
|
||||
private static readonly TimeSpan Tolerance = TimeSpan.FromSeconds(1);
|
||||
|
||||
/// <summary>
|
||||
/// The absolute path of the JPEG of each GPR frame of <paramref name="frames"/> (index rows), by frame id; frames
|
||||
/// without one are left out. One index query, none when no frame is a GPR.
|
||||
/// </summary>
|
||||
public static async Task<IReadOnlyDictionary<Guid, string>> FindAsync(TitanoDatabase db, Archive archive, IReadOnlyList<Frame> frames,
|
||||
CancellationToken cancellationToken) =>
|
||||
Paths(await FindAsync(db, archive, [.. frames.Where(f => f.Format == ImageFileFormat.Gpr).Select(f => (f.Id, f.OriginalFileName ?? f.FileName, f.CapturedAt))],
|
||||
cancellationToken).ConfigureAwait(false));
|
||||
|
||||
/// <inheritdoc cref="FindAsync(TitanoDatabase, Archive, IReadOnlyList{Frame}, CancellationToken)"/>
|
||||
public static async Task<IReadOnlyDictionary<Guid, string>> FindAsync(TitanoDatabase db, Archive archive, IReadOnlyList<SidecarFrame> frames,
|
||||
CancellationToken cancellationToken) =>
|
||||
Paths(await FindWithSizesAsync(db, archive, frames, cancellationToken).ConfigureAwait(false));
|
||||
|
||||
/// <summary>The JPEG of each GPR frame of <paramref name="frames"/> (sidecar frames) with its indexed size, by frame id.</summary>
|
||||
internal static Task<Dictionary<Guid, GprCompanion>> FindWithSizesAsync(TitanoDatabase db, Archive archive, IReadOnlyList<SidecarFrame> frames,
|
||||
CancellationToken cancellationToken) =>
|
||||
FindAsync(db, archive, [.. frames.Where(f => f.Format == ImageFileFormat.Gpr).Select(f => (f.Id, f.Original ?? Path.GetFileName(f.File), f.CapturedAt))],
|
||||
cancellationToken);
|
||||
|
||||
private static async Task<Dictionary<Guid, GprCompanion>> FindAsync(TitanoDatabase db, Archive archive,
|
||||
List<(Guid Id, string Name, DateTime? CapturedAt)> gprs, CancellationToken cancellationToken)
|
||||
{
|
||||
var found = new Dictionary<Guid, GprCompanion>();
|
||||
var timed = gprs.Where(g => g.CapturedAt is not null).Select(g => (g.Id, Stem: Stem(g.Name), At: g.CapturedAt!.Value)).ToList();
|
||||
if (timed.Count == 0) return found;
|
||||
var jpegs = await db.Frames.ListByCaptureRangeAsync(archive.Id, ImageFileFormat.Jpeg, timed.Min(g => g.At) - Tolerance,
|
||||
timed.Max(g => g.At) + Tolerance, cancellationToken).ConfigureAwait(false);
|
||||
var byStem = jpegs.Where(j => (j.Flags & FrameFlags.Missing) == 0 && j.CapturedAt is not null)
|
||||
.ToLookup(j => Stem(j.OriginalFileName ?? j.FileName), StringComparer.OrdinalIgnoreCase);
|
||||
var paths = new ArchivePaths(archive.RootPath);
|
||||
foreach (var (id, stem, at) in timed)
|
||||
{
|
||||
var match = byStem[stem].Where(j => (j.CapturedAt!.Value - at).Duration() <= Tolerance).MinBy(j => (j.CapturedAt!.Value - at).Duration());
|
||||
if (match is not null) found[id] = new GprCompanion(paths.FromRelative(match.RelativePath), match.Width, match.Height);
|
||||
}
|
||||
return found;
|
||||
}
|
||||
|
||||
private static Dictionary<Guid, string> Paths(Dictionary<Guid, GprCompanion> companions) => companions.ToDictionary(kv => kv.Key, kv => kv.Value.Path);
|
||||
|
||||
private static string Stem(string fileName) => Path.GetFileNameWithoutExtension(fileName);
|
||||
}
|
||||
Reference in new issue
Block a user