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:
Claude committed 2026-10-12 11:17:49 +00:00
1 parent 366dd9f303
commit 5599e4de89
19 files changed
+689 -111

No files matched your search

+8 -1
View File
@@ -37,6 +37,9 @@ public sealed class LocalSequenceSource : ISequenceSource
private readonly JobEngine? _engine;
private readonly ArchiveServiceOptions _lease;
private string?[] _files = [];
/// <summary>The JPEG of each GPR frame that an import keeps in another sequence (<see cref="GprCompanions"/>), by position.</summary>
private string?[] _companions = [];
private Guid[] _frameIds = [];
private ArchivePaths? _paths;
private SequencePaths? _folder;
@@ -81,6 +84,7 @@ public sealed class LocalSequenceSource : ISequenceSource
if (page.Count < PageSize) break;
}
var renders = await _db.Renders.ListBySequenceAsync(_sequenceId, cancellationToken);
var companions = await GprCompanions.FindAsync(_db, archive, frames, cancellationToken);
return await Task.Run(() =>
{
var paths = new ArchivePaths(archive.RootPath);
@@ -90,6 +94,7 @@ public sealed class LocalSequenceSource : ISequenceSource
{
if ((frames[i].Flags & FrameFlags.Missing) == 0) files[i] = paths.FromRelative(frames[i].RelativePath);
}
_companions = [.. frames.Select(f => companions.GetValueOrDefault(f.Id))];
(_paths, _folder, _files, _frameIds) = (paths, folder, files, [.. frames.Select(f => f.Id)]);
var context = SequenceContext.FromFrames(frames);
return new SequenceContent(sequence, frames, ReadProject(sequence, folder), LightAnalyzer.Analyze(frames), ReadReport(folder, frames.Count),
@@ -100,12 +105,14 @@ public sealed class LocalSequenceSource : ISequenceSource
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;
var companions = _companions;
var companion = (uint)index < (uint)companions.Length ? companions[index] : null;
return await Task.Run(() =>
{
try
{
// Thumbnails take an embedded preview when it is large enough (no RAW development for a filmstrip cell).
var request = new DecodeRequest(file, maxLongEdge, purpose, ForcePreview: purpose == DecodePurpose.Thumbnail);
var request = new DecodeRequest(file, maxLongEdge, purpose, ForcePreview: purpose == DecodePurpose.Thumbnail, Companion: companion);
return _decoder.Decode(request, cancellationToken)?.Pixels;
}
catch (Exception ex) when (ex is IOException or UnauthorizedAccessException)
+19 -11
View File
@@ -18,19 +18,22 @@ public sealed class FrameImageRenderer(ImageDecoder decoder)
public const int ThumbnailQuality = 80;
/// <summary>A 320 px q80 thumbnail, or null when no decoder can read the frame.</summary>
public EncodedImage? Thumbnail(SequencePaths sequence, SidecarFrame frame, CancellationToken cancellationToken = default)
/// <param name="companion">Where a GPR's JPEG is kept when it is not beside it (<see cref="DecodeRequest.Companion"/>).</param>
public EncodedImage? Thumbnail(SequencePaths sequence, SidecarFrame frame, CancellationToken cancellationToken = default, string? companion = null)
{
using var decoded = Decode(sequence.File(frame.File), frame, ThumbnailLongEdge, DecodePurpose.Thumbnail, preferPreview: true, cancellationToken);
using var decoded = Decode(sequence.File(frame.File), frame, ThumbnailLongEdge, DecodePurpose.Thumbnail, preferPreview: true, companion, cancellationToken);
if (decoded is null) return null;
using var small = Resampler.Thumbnail(decoded.Pixels, ThumbnailLongEdge);
return Encode(small, new JpegEncodeOptions { Quality = ThumbnailQuality }, decoded.Tier);
}
/// <summary>A proxy at the policy's long edge and quality with capture time and camera in its Exif; null when undecodable.</summary>
public EncodedImage? Proxy(SequencePaths sequence, SidecarFrame frame, SidecarCamera? camera, ProxyPolicy policy, CancellationToken cancellationToken = default)
/// <param name="companion">Where a GPR's JPEG is kept when it is not beside it (<see cref="DecodeRequest.Companion"/>).</param>
public EncodedImage? Proxy(SequencePaths sequence, SidecarFrame frame, SidecarCamera? camera, ProxyPolicy policy, CancellationToken cancellationToken = default,
string? companion = null)
{
if (policy.LongEdge == ProxyPolicy.Disabled) throw new InvalidOperationException("Proxies are disabled for this archive.");
using var decoded = Decode(sequence.File(frame.File), frame, policy.LongEdge, DecodePurpose.Proxy, preferPreview: false, cancellationToken);
using var decoded = Decode(sequence.File(frame.File), frame, policy.LongEdge, DecodePurpose.Proxy, preferPreview: false, companion, cancellationToken);
if (decoded is null) return null;
var source = decoded.Pixels;
var (w, h) = policy.LongEdge == ProxyPolicy.FullSize ? (source.Width, source.Height) : Resampler.FitWithin(source.Width, source.Height, policy.LongEdge);
@@ -59,25 +62,30 @@ public sealed class FrameImageRenderer(ImageDecoder decoder)
/// <summary>
/// The frame's pixels: a JPEG through the managed decoder, the stored embedded preview when <paramref name="preferPreview"/>
/// and it reaches <paramref name="maxLongEdge"/> (a smaller one would be upscaled or leave the thumbnail short), then
/// the host's decoders, and the stored preview as the last resort. Null when nothing decodes.
/// the host's decoders (with the GPR's <paramref name="companion"/>), and the stored preview as the last resort. Null
/// when nothing decodes.
/// </summary>
private DecodedFrame? Decode(string path, SidecarFrame frame, int maxLongEdge, DecodePurpose purpose, bool preferPreview, CancellationToken cancellationToken)
/// <exception cref="IOException">The file could not be read.</exception>
private DecodedFrame? Decode(string path, SidecarFrame frame, int maxLongEdge, DecodePurpose purpose, bool preferPreview, string? companion,
CancellationToken cancellationToken)
{
if (frame.Format == ImageFileFormat.Jpeg && DecodeJpeg(File.ReadAllBytes(path), maxLongEdge, frame.Orientation, DecodeTier.Native) is { } jpeg) return jpeg;
byte[]? preview = null;
if (preferPreview && (preview = ReadPreview(path, frame)) is not null && Reaches(preview, frame, maxLongEdge)
if (preferPreview && (preview = ReadPreview(path, frame)) is not null && Reaches(preview, maxLongEdge)
&& DecodeJpeg(preview, maxLongEdge, frame.Orientation, DecodeTier.EmbeddedPreview) is { } fast)
return fast;
var request = new DecodeRequest(path, System.Math.Max(0, maxLongEdge), purpose, ApplyOrientation: true, AllowPreview: true);
var request = new DecodeRequest(path, System.Math.Max(0, maxLongEdge), purpose, ApplyOrientation: true, AllowPreview: true, Companion: companion);
if (decoder.Decode(request, cancellationToken) is { } decoded) return new DecodedFrame(decoded.Pixels, decoded.Tier);
return (preview ?? ReadPreview(path, frame)) is { } fallback ? DecodeJpeg(fallback, maxLongEdge, frame.Orientation, DecodeTier.EmbeddedPreview) : null;
}
/// <summary>Whether the stored preview's long edge reaches <paramref name="maxLongEdge"/> (the sidecar's size, else the JPEG's own).</summary>
private static bool Reaches(byte[] preview, SidecarFrame frame, int maxLongEdge)
/// <summary>
/// Whether the stored preview's long edge reaches <paramref name="maxLongEdge"/>, from the JPEG's own header: the
/// sidecar's preview size may be a GPR companion's, larger than the preview embedded in the file.
/// </summary>
private static bool Reaches(byte[] preview, int maxLongEdge)
{
if (maxLongEdge <= 0) return true;
if (frame is { PreviewW: { } w, PreviewH: { } h }) return int.Max(w, h) >= maxLongEdge;
try
{
var info = JpegDecoder.ReadInfo(preview);
@@ -11,7 +11,8 @@ namespace Titano.Core.Imaging.Decoders;
/// The managed decoder (BRIEF §8.2), available on every platform and the only one on the server: JPEG, PNG and TIFF
/// natively; DNG and the supported camera RAWs (CR2, NEF, ARW, PEF, RAF) through <see cref="RawDecoder"/>; every other
/// RAW, CR3 and HEIF through the largest suitable embedded JPEG preview, and GoPro GPR through the JPEG the camera writes
/// beside it when that is larger (BRIEF §8.1; VC-5 is out of scope); nothing for formats without either.
/// with it (beside it, or where <see cref="DecodeRequest.Companion"/> says an import put it) when that is larger
/// (BRIEF §8.1; VC-5 is out of scope); nothing for formats without either.
/// <see cref="DecodeRequest"/> fast paths: JPEG DCT scaling and RAW CFA binning when <see cref="DecodeRequest.MaxLongEdge"/>
/// allows, bilinear demosaicing for thumbnails and analysis, and <see cref="DecodeRequest.ForcePreview"/> to skip native
/// RAW development. Results fit within MaxLongEdge (never upscaled) and are always RGB.
@@ -59,16 +60,21 @@ public sealed class ManagedDecoderProvider : IImageDecoderProvider
/// The tier for a file whose signature format is already known (the index's): JPEG, PNG and TIFF decode natively
/// whatever they hold (damage shows only when decoding), so only RAW and HEIF files are opened.
/// </summary>
public FileTier Probe(string path, ImageFileFormat format)
public FileTier Probe(string path, ImageFileFormat format) => Probe(path, format, null);
/// <inheritdoc cref="Probe(string, ImageFileFormat)"/>
/// <param name="companion">Where a GPR's JPEG is kept when it is not beside it (<see cref="DecodeRequest.Companion"/>).</param>
public FileTier Probe(string path, ImageFileFormat format, string? companion)
{
var family = ImageFormatInfo.Of(format).Family;
if (family is not (ImageFamily.Jpeg or ImageFamily.Png or ImageFamily.Tiff)) return Probe(path);
if (family is not (ImageFamily.Jpeg or ImageFamily.Png or ImageFamily.Tiff)) return Probe(path, companion);
var decoder = family switch { ImageFamily.Jpeg => "managed-jpeg", ImageFamily.Png => "managed-png", _ => "managed-tiff" };
return new FileTier(format, DecodeTier.Native, decoder, null, 0, 0);
}
/// <summary>The tier this provider would use for the file at <paramref name="path"/>, without decoding pixels.</summary>
public FileTier Probe(string path)
/// <param name="companion">Where a GPR's JPEG is kept when it is not beside it (<see cref="DecodeRequest.Companion"/>).</param>
public FileTier Probe(string path, string? companion = null)
{
using var stream = Open(path);
var format = ImageSignature.Detect(stream, path);
@@ -94,23 +100,20 @@ public sealed class ManagedDecoderProvider : IImageDecoderProvider
}
var metadata = MetadataReader.Read(stream, path);
var (pw, ph) = metadata.LargestPreview is { } preview ? PixelConvert.OrientedSize(preview.Width, preview.Height, metadata.Orientation) : (0, 0);
if (format == ImageFileFormat.Gpr && CompanionJpeg(path) is { } companion)
{
var jpeg = MetadataReader.Read(companion);
var (cw, ch) = jpeg.OrientedSize;
if (jpeg.Format == ImageFileFormat.Jpeg && int.Max(cw, ch) > int.Max(pw, ph))
return new FileTier(format, DecodeTier.EmbeddedPreview, "managed-sidecar", reason, cw, ch);
}
if (format == ImageFileFormat.Gpr && CompanionSize(path, companion) is var (cw, ch) && int.Max(cw, ch) > int.Max(pw, ph))
return new FileTier(format, DecodeTier.EmbeddedPreview, "managed-sidecar", reason, cw, ch);
if (metadata.LargestPreview is null) return new FileTier(format, DecodeTier.Unavailable, null, reason + "; no embedded preview", 0, 0);
return new FileTier(format, DecodeTier.EmbeddedPreview, "managed-preview", reason, pw, ph);
}
/// <summary>
/// The JPEG a GoPro writes beside a GPR (same name, <c>.JPG</c>), or null. Only the usual spellings are tried, so a
/// folder of thousands of frames is never listed once per frame.
/// The JPEG a GoPro writes with a GPR: <paramref name="companion"/> when the caller found it elsewhere (an import
/// keeps it in a sequence of its own) and it is there, else the one beside the GPR (same name, <c>.JPG</c>), or
/// null. Only the usual spellings are tried, so a folder of thousands of frames is never listed once per frame.
/// </summary>
internal static string? CompanionJpeg(string path)
internal static string? CompanionJpeg(string path, string? companion = null)
{
if (companion is not null && File.Exists(companion)) return companion;
var stem = Path.Combine(Path.GetDirectoryName(path) ?? string.Empty, Path.GetFileNameWithoutExtension(path));
foreach (var extension in CompanionExtensions)
{
@@ -119,6 +122,15 @@ public sealed class ManagedDecoderProvider : IImageDecoderProvider
return null;
}
/// <summary>Oriented size of the GPR's JPEG (see <see cref="CompanionJpeg"/>), or null when there is none.</summary>
/// <exception cref="IOException">The JPEG cannot be read.</exception>
private static (int Width, int Height)? CompanionSize(string path, string? companion)
{
if (CompanionJpeg(path, companion) is not { } file) return null;
var jpeg = MetadataReader.Read(file);
return jpeg.Format == ImageFileFormat.Jpeg ? jpeg.OrientedSize : null;
}
private static FileStream Open(string path) =>
new(path, FileMode.Open, FileAccess.Read, FileShare.ReadWrite | FileShare.Delete, FileBufferSize, FileOptions.RandomAccess);
@@ -179,12 +191,13 @@ public sealed class ManagedDecoderProvider : IImageDecoderProvider
}
/// <summary>
/// GPR's pixel source: the GoPro's own JPEG beside it when it is larger than every embedded preview (the camera writes
/// it at full size), decoded with DCT scaling and oriented by its own Exif; null when there is none.
/// GPR's pixel source: the GoPro's own JPEG (<see cref="DecodeRequest.Companion"/>, else beside it) when it is larger
/// than every embedded preview (the camera writes it at full size), decoded with DCT scaling and oriented by its own
/// Exif; null when there is none.
/// </summary>
private static DecodedImage? Companion(Stream raw, DecodeRequest request, CancellationToken cancellationToken)
{
if (CompanionJpeg(request.Path) is not { } path) return null;
if (CompanionJpeg(request.Path, request.Companion) is not { } path) return null;
var previews = MetadataReader.Read(raw, request.Path, cancellationToken: cancellationToken).Previews;
var largestPreview = previews.Count > 0 ? int.Max(previews[0].Width, previews[0].Height) : 0;
using var stream = Open(path);
+25 -4
View File
@@ -1,3 +1,5 @@
using System.Runtime.ExceptionServices;
namespace Titano.Core.Imaging;
/// <summary>How a frame's pixels were obtained (BRIEF §8.2). Logged per frame.</summary>
@@ -28,13 +30,16 @@ public enum DecodePurpose
/// <param name="ApplyOrientation">Rotate/flip according to the Exif orientation.</param>
/// <param name="AllowPreview">Accept the embedded-preview tier when native decode is impossible.</param>
/// <param name="ForcePreview">Use the embedded preview even when native decode is possible (fast paths).</param>
/// <param name="Companion">The JPEG a GoPro wrote with this GPR when it is kept elsewhere than beside it (an import puts
/// it in a sequence of its own); null looks beside the file. Other formats ignore it.</param>
public sealed record DecodeRequest(
string Path,
int MaxLongEdge = 0,
DecodePurpose Purpose = DecodePurpose.Render,
bool ApplyOrientation = true,
bool AllowPreview = true,
bool ForcePreview = false);
bool ForcePreview = false,
string? Companion = null);
/// <summary>Decoded pixels plus how they were produced. Dispose to return the buffer to the pool.</summary>
public sealed class DecodedImage(ImageBuffer pixels, DecodeTier tier, string decoder, int sourceWidth, int sourceHeight) : IDisposable
@@ -84,6 +89,13 @@ public interface IImageDecoderProvider
? new FileTier(format, tier, Name, null, 0, 0)
: new FileTier(format, DecodeTier.Unavailable, null, $"{Name}: no decoder for {format}", 0, 0);
/// <summary>
/// <see cref="Probe(string, ImageFileFormat)"/> for a file whose camera companion (see
/// <see cref="DecodeRequest.Companion"/>) is kept at <paramref name="companion"/>; a provider that uses no
/// companion ignores it.
/// </summary>
FileTier Probe(string path, ImageFileFormat format, string? companion) => Probe(path, format);
/// <summary>Decodes or returns null when this provider cannot handle the specific file.</summary>
DecodedImage? Decode(DecodeRequest request, CancellationToken cancellationToken = default);
}
@@ -110,8 +122,9 @@ public sealed class ImageDecoder(IReadOnlyList<IImageDecoderProvider> providers)
/// embedded preview a provider offers, else unavailable with the first reason given. A provider that finds the file
/// malformed is skipped like in <see cref="Decode"/>.
/// </summary>
/// <param name="companion">Where a GoPro GPR's JPEG is kept when it is not beside the file (<see cref="DecodeRequest.Companion"/>).</param>
/// <exception cref="IOException">The file cannot be read.</exception>
public FileTier Probe(string path, ImageFileFormat format)
public FileTier Probe(string path, ImageFileFormat format, string? companion = null)
{
FileTier? preview = null;
string? reason = null;
@@ -121,7 +134,7 @@ public sealed class ImageDecoder(IReadOnlyList<IImageDecoderProvider> providers)
FileTier tier;
try
{
tier = provider.Probe(path, format);
tier = provider.Probe(path, format, companion);
}
catch (Exception ex) when (ex is InvalidDataException or NotSupportedException or FormatException or InvalidOperationException
or System.Runtime.InteropServices.COMException)
@@ -137,11 +150,17 @@ public sealed class ImageDecoder(IReadOnlyList<IImageDecoderProvider> providers)
return preview ?? new FileTier(format, DecodeTier.Unavailable, null, reason ?? $"no decoder for {format}", 0, 0);
}
/// <summary>Decodes with the first provider that succeeds; null means <see cref="DecodeTier.Unavailable"/>.</summary>
/// <summary>
/// Decodes with the first provider that succeeds; null means <see cref="DecodeTier.Unavailable"/>: the providers read
/// the file and none produced pixels. When one could not read it (locked by another program, vanished, a network
/// read failed) and none produced pixels, nothing is known about the content, so that read error is thrown instead.
/// </summary>
/// <exception cref="IOException">The file could not be read and no provider produced pixels.</exception>
public DecodedImage? Decode(DecodeRequest request, CancellationToken cancellationToken = default)
{
var format = ImageFormatInfo.FromPath(request.Path);
List<Exception>? errors = null;
IOException? unread = null;
foreach (var provider in Providers.OrderBy(p => p.TierFor(format.Format) ?? DecodeTier.Unavailable))
{
if (provider.TierFor(format.Format) is null) continue;
@@ -158,8 +177,10 @@ public sealed class ImageDecoder(IReadOnlyList<IImageDecoderProvider> providers)
catch (Exception ex) when (ex is IOException or InvalidDataException or NotSupportedException or FormatException or InvalidOperationException or System.Runtime.InteropServices.COMException)
{
(errors ??= []).Add(ex);
if (ex is IOException io) unread ??= io;
}
}
if (unread is not null) ExceptionDispatchInfo.Throw(unread);
return null;
}
}
+11 -7
View File
@@ -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)
{
+13 -6
View File
@@ -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);
}
@@ -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,
+32 -15
View File
@@ -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;
}
+4 -1
View File
@@ -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;
@@ -138,10 +138,35 @@ public sealed class FrameRepository
_db.InTransactionAsync(async session =>
{
var updated = 0;
foreach (var f in frames)
if (_db.Sql.IsPostgres)
{
updated += await session.ExecuteAsync("UPDATE frames SET flags = $2, preview_width = $3, preview_height = $4 WHERE id = $1",
[f.FrameId, (int)f.Flags, f.PreviewWidth, f.PreviewHeight], cancellationToken).ConfigureAwait(false);
// One UPDATE … FROM (VALUES …) per batch instead of a round trip per frame (a first analysis of a RAW sequence).
for (var start = 0; start < frames.Count; start += Batch)
{
var count = Math.Min(Batch, frames.Count - start);
var sql = new StringBuilder("UPDATE frames AS f SET flags = v.fl, preview_width = v.w, preview_height = v.h FROM (VALUES ");
var args = new DbParam[count * 4];
for (var i = 0; i < count; i++)
{
var f = frames[start + i];
sql.Append(i > 0 ? ", (" : "(").Append('$').Append(4 * i + 1).Append(", $").Append(4 * i + 2)
.Append(", $").Append(4 * i + 3).Append(", $").Append(4 * i + 4).Append(')');
args[4 * i] = f.FrameId;
args[4 * i + 1] = (int)f.Flags;
args[4 * i + 2] = f.PreviewWidth;
args[4 * i + 3] = f.PreviewHeight;
}
sql.Append(") AS v(id, fl, w, h) WHERE f.id = v.id");
updated += await session.ExecuteAsync(sql.ToString(), args, cancellationToken).ConfigureAwait(false);
}
}
else
{
foreach (var f in frames)
{
updated += await session.ExecuteAsync("UPDATE frames SET flags = $2, preview_width = $3, preview_height = $4 WHERE id = $1",
[f.FrameId, (int)f.Flags, f.PreviewWidth, f.PreviewHeight], cancellationToken).ConfigureAwait(false);
}
}
return updated;
}, cancellationToken);
@@ -189,6 +214,15 @@ public sealed class FrameRepository
"WHERE s.archive_id = $1 AND f.size = $2 AND f.captured_at = $3",
[archiveId, size, SequenceRepository.Wall(capturedAt)], Map, cancellationToken);
/// <summary>Frames of an archive in this format captured from <paramref name="from"/> to <paramref name="to"/> (inclusive),
/// by capture time (a GoPro GPR's JPEG in another sequence).</summary>
public Task<IReadOnlyList<Frame>> ListByCaptureRangeAsync(Guid archiveId, ImageFileFormat format, DateTime from, DateTime to,
CancellationToken cancellationToken = default) =>
_db.ListAsync(
$"SELECT {string.Join(", ", ColumnNames.Select(c => "f." + c))} FROM frames f JOIN sequences s ON s.id = f.sequence_id " +
"WHERE s.archive_id = $1 AND f.format = $2 AND f.captured_at >= $3 AND f.captured_at <= $4 ORDER BY f.captured_at, f.id",
[archiveId, (int)format, SequenceRepository.Wall(from), SequenceRepository.Wall(to)], Map, cancellationToken);
/// <summary>Every frame of an archive whose SHA-256 occurs more than once, grouped by hash then sequence and index.</summary>
public Task<IReadOnlyList<DuplicateFrame>> ListDuplicatesAsync(Guid archiveId, CancellationToken cancellationToken = default) =>
_db.ListAsync(