fix: full verification against the brief — 137 confirmed findings fixed over six passes #4

Merged
Alby96 merged 169 commits from claude/keen-knuth-uhxv8w into main 2026-10-13 12:10:04 +02:00
19 changed files with 689 additions and 111 deletions
Showing only changes of commit 5599e4de89 - Show all commits

No files matched your search

+1 -1
View File
@@ -56,7 +56,7 @@ Dependencies point one way: `Core` ← `Core.Windows`, `Data`, `Protocol` ← `S
| `Migrations/{sqlite,postgres}/NNNN_name.sql` | Embedded, applied in one transaction at open (`MigrationRunner`) |
| `Repositories` | `TitanoDatabase` facade: archives, sequences, frames, tags, renders, jobs, cards, users, tokens, uploads, render nodes, settings, audit |
| `Jobs` | `JobEngine`, `IJobHandler`, `IJobHandlerProvider`, `JobContext` (progress, checkpoints, follow-up jobs) |
| `Archiving` | `ArchiveService` (create/adopt/open), `ArchiveIndexer` (scan, rebuild), `ArchiveIngest` (uploads and imports), `ImportService`, `ArchiveVerifier`, `ArchiveImages`, `FrameTiers` (decode tier, `PreviewOnly`/`Undecodable` flags and preview size of frames, written to sidecar and index by the light analysis, deep analysis, renders and image passes), `ArchiveMaintenance`, `MassRenamer`, `SequenceTrash` (delete into the archive's hidden trash with a dry run, restore; the folder moves back when the index step fails), job handlers |
| `Archiving` | `ArchiveService` (create/adopt/open), `ArchiveIndexer` (scan, rebuild), `ArchiveIngest` (uploads and imports), `ImportService`, `ArchiveVerifier`, `ArchiveImages`, `FrameTiers` (decode tier, `PreviewOnly`/`Undecodable` flags and preview size of frames, written to sidecar and index by the light analysis, deep analysis, renders and image passes), `GprCompanions` (the JPEG of each GoPro GPR, found in the index when an import kept it in another sequence), `ArchiveMaintenance`, `MassRenamer`, `SequenceTrash` (delete into the archive's hidden trash with a dry run, restore; the folder moves back when the index step fails), job handlers |
| `Processing` | `ProcessingService`, `SequenceAnalyser`, `SequenceRender`, `ProjectStore`, `RenderNodeClient`, job handlers |
| `Sync` | `SyncService` (sync table of one server: compare, link by content, settle conflicts, enqueue transfers), `SyncRules` (status, settings merge and differences, no I/O), `SyncLinkStore` (`sync_links`, `sync_state`), `SyncTransfers` and `SyncJobHandler` (resumable, hash-verified uploads and downloads as jobs; downloads commit through the archive ingest), `SyncAgent` (background comparison, server events, backoff, offline proxies), `ProxyCache` and `RemoteSequence` (server sequences opened with proxies), `ISyncRemote` (the server calls; `TitanoSyncRemote` over `TitanoClient`) |
+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(
@@ -9,6 +9,7 @@ using Titano.Core.Imaging;
using Titano.Core.Imaging.Decoders;
using Titano.Core.Metadata;
using Titano.Core.Model;
using Titano.Core.Project;
using Titano.Core.Tests.Analysis.Deep;
using Titano.Core.Tests.Raw;
using Titano.Testing;
@@ -101,11 +102,12 @@ public sealed class DecodeTierTests(TestContext context)
var dng = SyntheticDng.Write(scene, new DngSpec());
var renderer = new FrameImageRenderer(DecoderFactory.CreateServer());
EncodedImage Thumbnail(string name, int previewWidth, bool sizeKnown)
EncodedImage Thumbnail(string name, int previewWidth, bool sizeKnown, int? storedWidth = null)
{
// The preview is appended after the DNG, which the TIFF offsets never reach.
var preview = TestPhotos.Jpeg(Start, 3, width: previewWidth, height: previewWidth * 3 / 4);
File.WriteAllBytes(Path.Combine(sequence.Originals, name), [.. dng, .. preview]);
var stored = storedWidth ?? previewWidth;
var frame = new SidecarFrame
{
Id = Guid.CreateVersion7(),
@@ -115,8 +117,8 @@ public sealed class DecodeTierTests(TestContext context)
H = 480,
PreviewOffset = dng.Length,
PreviewLength = preview.Length,
PreviewW = sizeKnown ? previewWidth : null,
PreviewH = sizeKnown ? previewWidth * 3 / 4 : null,
PreviewW = sizeKnown ? stored : null,
PreviewH = sizeKnown ? stored * 3 / 4 : null,
};
return renderer.Thumbnail(sequence, frame)!;
}
@@ -126,6 +128,9 @@ public sealed class DecodeTierTests(TestContext context)
Assert.Equal((320, 240, DecodeTier.Native), (small.Width, small.Height, small.Tier));
var unknown = Thumbnail("unknown.dng", 200, sizeKnown: false);
Assert.Equal((320, DecodeTier.Native), (unknown.Width, unknown.Tier));
// The stored size can be a GPR companion's, larger than the embedded preview: the JPEG's own header decides.
var companionSized = Thumbnail("companion.dng", 200, sizeKnown: true, storedWidth: 4000);
Assert.Equal((320, DecodeTier.Native), (companionSized.Width, companionSized.Tier));
var large = Thumbnail("large.dng", 400, sizeKnown: true);
Assert.Equal((320, 240, DecodeTier.EmbeddedPreview), (large.Width, large.Height, large.Tier));
Assert.Equal(320, JpegDecoder.ReadInfo(large.Jpeg).Width);
@@ -177,22 +182,60 @@ public sealed class DecodeTierTests(TestContext context)
[Test]
public async Task FramesBothAnalysesFoundUndecodableAreListedOnce()
{
// Frame 4 does not decode; frame 7 is not read either (missing, or beyond this host), which never flags it.
var source = new SyntheticFrameSource(SyntheticSequence.Create(new SyntheticSequenceOptions { Width = 96, Height = 64, FrameCount = 12 }))
{
Undecodable = [4],
Undecodable = [4, 7],
};
var deep = await DeepAnalyzer.AnalyzeAsync(source, DeepScenes.Options);
var metadata = Enumerable.Range(0, source.Count).Select(source.Metadata).ToList();
var context = new SequenceContext { FrameCount = source.Count, SourceWidth = 96, SourceHeight = 64 };
// Before the deep pass flagged the frame, only its own finding names it.
// Before the deep pass flagged the frame, only its own finding names them.
var unflagged = Advisor.Recommend(LightAnalyzer.Analyze(metadata), deep, context).Findings.Where(f => f.Key == DeepFindingKeys.UnreadableFrames).ToList();
Assert.SequenceEqual([4], unflagged.Single().Frames);
Assert.SequenceEqual([4, 7], unflagged.Single().Frames);
// Once flagged, the light analysis lists it and the deep finding is not repeated.
// Once frame 4 is flagged both analyses report it: one finding over both lists, counted once.
var flagged = metadata.Select(m => m.Index == 4 ? m with { Flags = FrameFlags.Undecodable } : m).ToList();
var light = LightAnalyzer.Analyze(flagged);
var findings = Advisor.Recommend(light, deep, context).Findings.Where(f => f.Key == DeepFindingKeys.UnreadableFrames).ToList();
Assert.Same(light.Findings.Single(f => f.Key == DeepFindingKeys.UnreadableFrames), findings.Single());
Assert.SequenceEqual([4], light.Findings.Single(f => f.Key == DeepFindingKeys.UnreadableFrames).Frames);
var merged = Advisor.Recommend(light, deep, context).Findings.Single(f => f.Key == DeepFindingKeys.UnreadableFrames);
Assert.SequenceEqual([4, 7], merged.Frames);
Assert.Equal((FindingSeverity.Warning, 2), (merged.Severity, Convert.ToInt32(merged.Arguments.Single())));
// A frame flagged since the deep pass (a render found it broken) joins the list too.
var later = LightAnalyzer.Analyze(flagged.Select(m => m.Index == 9 ? m with { Flags = FrameFlags.Undecodable } : m).ToList());
var all = Advisor.Recommend(later, deep, context).Findings.Single(f => f.Key == DeepFindingKeys.UnreadableFrames);
Assert.Equal((3, 3), (all.Frames.Count, Convert.ToInt32(all.Arguments.Single())));
}
[Test]
public void FramesTheCamerasIntervalometerShotTimeTheirGapsAdaptively()
{
var metadata = new FrameMetadata { Format = ImageFileFormat.Rw2, IntervalShooting = true };
Assert.Equal(FrameFlags.Intervalometer, SequenceFrames.FromMetadata(Guid.CreateVersion7(), "a.rw2", "a.rw2", 10, null, metadata).Flags);
Assert.Equal(FrameFlags.None, SequenceFrames.FromMetadata(Guid.CreateVersion7(), "b.rw2", "b.rw2", 10, null, metadata with { IntervalShooting = false }).Flags);
// 60 frames 10 s apart with one 40 s gap: a sparse gap alone keeps the timing constant (motion unmeasured).
LightFrame Frame(int i, FrameFlags flags) => new()
{
Index = i,
CapturedAt = Start.AddSeconds(10 * i + (i >= 30 ? 30 : 0)),
Width = 6000,
Height = 4000,
Flags = flags,
};
var context = new SequenceContext { FrameCount = 60, SourceWidth = 6000, SourceHeight = 4000 };
var plain = LightAnalyzer.Analyze([.. Enumerable.Range(0, 60).Select(i => Frame(i, FrameFlags.None))]);
Assert.Equal(0, plain.IntervalometerFrames);
var free = Advisor.Recommend(plain, null, context).Find(AdvisorParameters.TimingMode)!;
Assert.Equal((AdvisorKeys.TimingSparseGaps, (object?)TimingMode.Constant), (free.ReasonKey, free.Value));
// The camera's own intervalometer shot them: its pause was deliberate, so adaptive timing keeps it short.
var shot = LightAnalyzer.Analyze([.. Enumerable.Range(0, 60).Select(i => Frame(i, FrameFlags.Intervalometer))]);
Assert.Equal(60, shot.IntervalometerFrames);
var advice = Advisor.Recommend(shot, null, context).Find(AdvisorParameters.TimingMode)!;
Assert.Equal((AdvisorKeys.TimingIntervalometer, (object?)TimingMode.Adaptive), (advice.ReasonKey, advice.Value));
Assert.Equal(1, Convert.ToInt32(advice.ReasonArgs.Single()));
}
}
@@ -260,6 +260,52 @@ public sealed class ManagedDecoderProviderTests(TestContext context)
Assert.Equal("managed-preview", new ManagedDecoderProvider().Probe(big).Decoder);
}
[Test]
public void GoProRawDecodesThroughItsJpegWhereTheImportKeptIt()
{
// An import splits sessions per format: the JPEG lives in another sequence folder, under its imported name.
var gpr = Write("GOPR0003.GPR", Gpr(previewWidth: 64, previewHeight: 48));
var elsewhere = Path.Combine(context.CreateTempDirectory("jpeg-sequence"), "2026-09-28_0003.JPG");
File.WriteAllBytes(elsewhere, JpegFile(160, 120, 1));
var server = DecoderFactory.CreateServer();
using (var decoded = server.Decode(new DecodeRequest(gpr, Companion: elsewhere)))
Assert.Equal(("managed-sidecar", 160, 120), (decoded!.Decoder, decoded.SourceWidth, decoded.SourceHeight));
var probe = server.Probe(gpr, ImageFileFormat.Gpr, elsewhere);
Assert.Equal((DecodeTier.EmbeddedPreview, "managed-sidecar", 160, 120), (probe.Tier, probe.Decoder, probe.Width, probe.Height));
// A JPEG that is gone (deleted since) leaves the embedded preview; other formats ignore the companion.
var gone = Path.Combine(_dir, "gone.JPG");
using (var preview = server.Decode(new DecodeRequest(gpr, Companion: gone)))
Assert.Equal(("managed-preview", 64), (preview!.Decoder, preview.Pixels.Width));
Assert.Equal("managed-preview", server.Probe(gpr, ImageFileFormat.Gpr, gone).Decoder);
var jpeg = Write("plain.jpg", JpegFile(64, 48, 1));
using (var plain = server.Decode(new DecodeRequest(jpeg, Companion: elsewhere)))
Assert.Equal(("managed-jpeg", 64), (plain!.Decoder, plain.SourceWidth));
}
[Test]
public void AFileNoProviderCouldReadIsAReadErrorNotAnUndecodableFile()
{
var path = Write("locked.jpg", JpegFile(64, 48, 1));
// Every provider failed to read the file (locked by another program, a network read failed): nothing is known
// about its content, so the caller gets the read error instead of "nothing decodes it".
var unreadable = new ImageDecoder([new ScriptedProvider("a", _ => throw new IOException("sharing violation"))]);
Assert.Throws<IOException>(() => unreadable.Decode(new DecodeRequest(path)));
// One provider read it and found nothing, another could not read it: still unproven.
var mixed = new ImageDecoder([new ScriptedProvider("a", _ => null), new ScriptedProvider("b", _ => throw new IOException("short read"))]);
Assert.Throws<IOException>(() => mixed.Decode(new DecodeRequest(path)));
// A provider that produces pixels wins over one that could not read the file.
var recovered = new ImageDecoder([new ScriptedProvider("a", _ => throw new IOException("busy")), new ManagedDecoderProvider()]);
using (var image = recovered.Decode(new DecodeRequest(path)))
Assert.Equal("managed-jpeg", image!.Decoder);
// Providers that read the file and found it malformed prove it undecodable.
var damaged = new ImageDecoder([new ScriptedProvider("a", _ => throw new InvalidDataException("bad marker"))]);
Assert.Null(damaged.Decode(new DecodeRequest(path)));
Assert.Null(DecoderFactory.CreateServer().Decode(new DecodeRequest(Write("garbage.jpg", new byte[3000]))));
Assert.Throws<FileNotFoundException>(() => DecoderFactory.CreateServer().Decode(new DecodeRequest(Path.Combine(_dir, "vanished.jpg"))));
}
/// <summary>A GoPro GPR as far as Titano reads it: a DNG made by "GoPro" whose only decodable image is a YCbCr JPEG preview.</summary>
private static byte[] Gpr(int previewWidth, int previewHeight)
{
@@ -286,4 +332,12 @@ public sealed class ManagedDecoderProviderTests(TestContext context)
return null;
}
}
/// <summary>A JPEG provider whose decode does what the test says (return nothing, or throw).</summary>
private sealed class ScriptedProvider(string name, Func<DecodeRequest, DecodedImage?> decode) : IImageDecoderProvider
{
public string Name => name;
public DecodeTier? TierFor(ImageFileFormat format) => format == ImageFileFormat.Jpeg ? DecodeTier.Native : null;
public DecodedImage? Decode(DecodeRequest request, CancellationToken cancellationToken = default) => decode(request);
}
}
@@ -2,11 +2,16 @@ using Titano.Core.Advice;
using Titano.Core.Analysis;
using Titano.Core.Analysis.Deep;
using Titano.Core.Archiving;
using Titano.Core.Codecs.Dng;
using Titano.Core.Codecs.Jpeg;
using Titano.Core.Diagnostics.Synthetic;
using Titano.Core.Imaging;
using Titano.Core.Import;
using Titano.Core.Localization;
using Titano.Core.Metadata;
using Titano.Core.Model;
using Titano.Core.Project;
using Titano.Data.Archiving.Jobs;
using Titano.Data.Processing;
using Titano.Data.Processing.Jobs;
using Titano.Data.Repositories;
@@ -134,6 +139,138 @@ public sealed class AnalysisJobTests(TestContext context)
Assert.Equal(JobState.Done, done.State);
}
[Test]
public async Task AnImportedGoProRawDecodesThroughTheJpegItsImportKeptInAnotherSequence()
{
await using var fx = await ProcessingFixture.CreateAsync(context);
// A GoPro card: each GPR (VC-5, without a usable embedded image here) has the camera's full-size JPEG beside it.
// An upload may send the GPRs first: they arrive as two cards here.
var raws = Path.Combine(context.CreateTempDirectory("card"), "DCIM", "100GOPRO");
var jpegs = Path.Combine(context.CreateTempDirectory("card"), "DCIM", "100GOPRO");
Directory.CreateDirectory(raws);
Directory.CreateDirectory(jpegs);
var start = new DateTime(2026, 9, 28, 18, 0, 0);
for (var i = 0; i < 4; i++)
{
var metadata = new FrameMetadata { Make = "GoPro", Model = "HERO12 Black", BodySerial = "C3501", CapturedAt = start.AddSeconds(5 * i) };
using var sensor = new ImageBuffer(64, 48);
File.WriteAllBytes(Path.Combine(raws, $"GOPR{i + 1:0000}.GPR"),
LinearDngWriter.Write(sensor, new LinearDngOptions { Exif = ExifWriter.BuildTiff(metadata), TileSize = 64 }));
using var picture = new ImageBuffer(160, 120);
picture.Plane(1).Fill(0.25f * (i + 1));
File.WriteAllBytes(Path.Combine(jpegs, $"GOPR{i + 1:0000}.JPG"),
JpegEncoder.Encode(picture, new JpegEncodeOptions { App1Segments = [ExifWriter.BuildJpegApp1(metadata)[4..]] }));
}
var service = fx.Archive.Service;
async Task<ImportPlan> ImportAsync(string card)
{
var plan = await service.PlanImportAsync(fx.Archive.Archive.Id, Path.GetDirectoryName(Path.GetDirectoryName(card))!, new ImportOptions(),
cancellationToken: fx.Ct);
await service.Imports.ExecuteAsync(new ImportJobParameters(fx.Archive.Archive.Id, plan, null, true, null), Guid.NewGuid(), 0,
_ => ValueTask.CompletedTask, null, fx.Ct);
return plan;
}
var gprSession = (await ImportAsync(raws)).Sessions.Single();
Assert.Equal(ImageFileFormat.Gpr, gprSession.Format);
var id = await fx.Db.Sequences.FindBySessionKeyAsync(fx.Archive.Archive.Id, gprSession.SessionKey, fx.Ct);
var sequence = (await fx.Db.Sequences.GetAsync(id!.Value, fx.Ct))!;
await fx.StartEngineAsync();
// Alone, a GPR without an embedded preview gives no pixels here, which proves nothing about the file.
var first = await fx.EnqueueAsync(ProcessingJobs.LightAnalysis([sequence.Id]));
await fx.WaitForAsync(first.Id, JobState.Done);
var alone = ManifestStore.ReadSequence(fx.Folder(sequence).ManifestPath).Frames;
Assert.True(alone.All(f => f is { Tier: DecodeTier.Unavailable, Flags: FrameFlags.None, PreviewW: null }), "unavailable, not flagged");
// The JPEGs arrive as a sequence of their own (the import splits per file format, DECISIONS 2026-10-02); the next
// probe finds each GPR's JPEG there, so the output cap is the JPEG's size.
Assert.Equal(ImageFileFormat.Jpeg, (await ImportAsync(jpegs)).Sessions.Single().Format);
var jpegFrame = (await fx.Db.Frames.ListByCaptureRangeAsync(fx.Archive.Archive.Id, ImageFileFormat.Jpeg, start, start.AddMinutes(1), fx.Ct))[0];
Assert.NotEqual(Path.GetDirectoryName(fx.Folder(sequence).File(alone[0].File)), Path.GetDirectoryName(fx.Archive.Paths.FromRelative(jpegFrame.RelativePath)),
"not beside the GPR");
var light = await fx.EnqueueAsync(ProcessingJobs.LightAnalysis([sequence.Id]));
await fx.WaitForAsync(light.Id, JobState.Done);
var sidecar = ManifestStore.ReadSequence(fx.Folder(sequence).ManifestPath).Frames;
Assert.True(sidecar.All(f => f is { Tier: DecodeTier.EmbeddedPreview, Flags: FrameFlags.PreviewOnly, PreviewW: 160, PreviewH: 120 }),
"preview-only at the JPEG's size");
// The deep pass decodes the GPRs through those JPEGs (decoder "managed-sidecar"), never failing a frame.
var deep = await fx.EnqueueAsync(ProcessingJobs.DeepAnalysis([sequence.Id]));
await fx.WaitForAsync(deep.Id, JobState.Done);
var gprs = fx.Decoder.Decoded.Where(d => d.File.EndsWith(".GPR", StringComparison.OrdinalIgnoreCase)).ToList();
Assert.NotEmpty(gprs);
Assert.True(gprs.All(d => d.Decoder == "managed-sidecar"), string.Join(", ", gprs.Select(d => d.Decoder).Distinct()));
Assert.Empty(SequenceAnalyser.ReadReport(fx.Folder(sequence), sequence.FrameCount)!.Issues.Undecodable);
}
[Test]
public async Task DeepAnalysisNeverFlagsFramesThatVanishedOrCouldNotBeRead()
{
await using var fx = await ProcessingFixture.CreateAsync(context);
var sequence = await fx.AddAsync(Flickering(12));
// Deleted after indexing (a verify flags it missing), and held by another program during the pass.
File.Delete(Path.Combine(fx.Folder(sequence).Folder, ProcessingFixture.FileName(3)));
fx.Decoder.Unreadable[ProcessingFixture.FileName(6)] = 0;
await fx.StartEngineAsync();
var job = await fx.EnqueueAsync(ProcessingJobs.DeepAnalysis([sequence.Id]));
await fx.WaitForAsync(job.Id, JobState.Done);
// The pass could not read them, so it lists them as unreadable, but nothing proves the files undecodable.
var report = SequenceAnalyser.ReadReport(fx.Folder(sequence), sequence.FrameCount)!;
Assert.SequenceEqual([3, 6], report.Issues.Undecodable);
var sidecar = ManifestStore.ReadSequence(fx.Folder(sequence).ManifestPath).Frames;
foreach (var index in new[] { 3, 6 })
{
Assert.Equal(FrameFlags.None, sidecar[index].Flags & FrameFlags.Undecodable);
Assert.NotEqual(DecodeTier.Unavailable, sidecar[index].Tier);
}
var rows = await fx.Db.Frames.ListBySequenceAsync(sequence.Id, cancellationToken: fx.Ct);
Assert.True(rows.All(f => (f.Flags & FrameFlags.Undecodable) == 0), "no frame flagged in the index");
var unreadable = Advisor.Recommend(LightAnalyzer.Analyze(rows), report, SequenceContext.FromFrames(rows)).Findings
.Single(f => f.Key == DeepFindingKeys.UnreadableFrames);
Assert.SequenceEqual([3, 6], unreadable.Frames);
// Back in place and readable, the next pass reads both frames.
fx.Decoder.Unreadable.Clear();
await File.WriteAllBytesAsync(Path.Combine(fx.Folder(sequence).Folder, ProcessingFixture.FileName(3)),
await File.ReadAllBytesAsync(Path.Combine(fx.Folder(sequence).Folder, ProcessingFixture.FileName(2)), fx.Ct), fx.Ct);
var again = await fx.EnqueueAsync(ProcessingJobs.DeepAnalysis([sequence.Id]));
await fx.WaitForAsync(again.Id, JobState.Done);
Assert.Empty(SequenceAnalyser.ReadReport(fx.Folder(sequence), sequence.FrameCount)!.Issues.Undecodable);
}
[Test]
public async Task AResumedDeepAnalysisDecodesAgainWhatTheEarlierRunCouldNotRead()
{
await using var fx = await ProcessingFixture.CreateAsync(context);
var sequence = await fx.AddAsync(Flickering());
// The first run cannot read frame 12 (held by another program) and is cancelled later, at frame 26.
fx.Decoder.Unreadable[ProcessingFixture.FileName(11)] = 0;
fx.Decoder.HoldFrame(25);
await fx.StartEngineAsync();
var job = await fx.EnqueueAsync(ProcessingJobs.DeepAnalysis([sequence.Id]));
await fx.Decoder.Holding.Task.WaitAsync(TimeSpan.FromSeconds(60), fx.Ct);
await fx.Engine.CancelAsync(job.Id, fx.Ct);
await fx.WaitForAsync(job.Id, JobState.Cancelled);
// Readable again; the resumed run takes frame 12 from its checkpoint (it is not in the region sample).
fx.Decoder.Unreadable.Clear();
fx.Decoder.Hold = null;
while (fx.Decoder.Requests.TryDequeue(out _))
{
}
await fx.Engine.ResumeAsync(job.Id, fx.Ct);
await fx.WaitForAsync(job.Id, JobState.Done);
Assert.Contains(11, SequenceAnalyser.ReadReport(fx.Folder(sequence), sequence.FrameCount)!.Issues.Undecodable);
// That verdict was a read error: decoding the frame again shows it is fine, so nothing flags it.
Assert.Count(1, fx.Decoder.Requests.Where(r => Path.GetFileName(r.Path) == ProcessingFixture.FileName(11)));
var frame = ManifestStore.ReadSequence(fx.Folder(sequence).ManifestPath).Frames[11];
Assert.Equal((DecodeTier.Native, FrameFlags.None), (frame.Tier, frame.Flags & FrameFlags.Undecodable));
Assert.Equal(FrameFlags.None, (await fx.Db.Frames.ListBySequenceAsync(sequence.Id, cancellationToken: fx.Ct))[11].Flags);
}
[Test]
public async Task DeepAnalysisResumesAfterCancellationWithTheSameReport()
{
@@ -123,25 +123,43 @@ internal sealed class GateDecoder : IImageDecoderProvider
Hold = ProcessingFixture.FileName(index);
}
/// <summary>Tiers <see cref="Probe"/> reports per file name instead of the managed provider's (a host that reads them
/// only as previews, or not at all).</summary>
/// <summary>Tiers <see cref="Probe(string, ImageFileFormat)"/> reports per file name instead of the managed provider's (a
/// host that reads them only as previews, or not at all).</summary>
public ConcurrentDictionary<string, FileTier> Tiers { get; } = new();
/// <summary>Files (by name) whose decodes report the embedded-preview tier from a preview of this oriented size, as
/// a size-limited decode that picked a smaller preview would.</summary>
public ConcurrentDictionary<string, (int Width, int Height)> PreviewDecodes { get; } = new();
/// <summary>Files (by name) that cannot be read: their decodes throw <see cref="IOException"/> (a sharing violation).</summary>
public ConcurrentDictionary<string, byte> Unreadable { get; } = new();
/// <summary>File name and decoder of every decode that produced pixels.</summary>
public ConcurrentQueue<(string File, string Decoder)> Decoded { get; } = new();
public DecodeTier? TierFor(ImageFileFormat format) => _managed.TierFor(format);
public FileTier Probe(string path, ImageFileFormat format) =>
Tiers.TryGetValue(Path.GetFileName(path), out var tier) ? tier : _managed.Probe(path, format);
public FileTier Probe(string path, ImageFileFormat format) => Probe(path, format, null);
public FileTier Probe(string path, ImageFileFormat format, string? companion) =>
Tiers.TryGetValue(Path.GetFileName(path), out var tier) ? tier : _managed.Probe(path, format, companion);
public DecodedImage? Decode(DecodeRequest request, CancellationToken cancellationToken = default)
{
Requests.Enqueue(request);
if (Hold is { } hold && Path.GetFileName(request.Path) == hold && (HoldPurpose ?? request.Purpose) == request.Purpose
var name = Path.GetFileName(request.Path);
if (Hold is { } hold && name == hold && (HoldPurpose ?? request.Purpose) == request.Purpose
&& (HoldEdge ?? request.MaxLongEdge) == request.MaxLongEdge)
{
Holding.TrySetResult();
cancellationToken.WaitHandle.WaitOne(TimeSpan.FromSeconds(30));
cancellationToken.ThrowIfCancellationRequested();
}
return _managed.Decode(request, cancellationToken);
if (Unreadable.ContainsKey(name)) throw new IOException($"The process cannot access the file '{request.Path}' because it is being used by another process.");
var decoded = _managed.Decode(request, cancellationToken);
if (decoded is null) return null;
Decoded.Enqueue((name, decoded.Decoder));
if (!PreviewDecodes.TryGetValue(name, out var preview)) return decoded;
return new DecodedImage(decoded.Pixels, DecodeTier.EmbeddedPreview, "test-preview", preview.Width, preview.Height);
}
}
@@ -128,6 +128,105 @@ public sealed class RenderJobTests(TestContext context)
Assert.Count(2, await fx.Db.Renders.ListBySequenceAsync(sequence.Id, fx.Ct));
}
[Test]
public async Task AFrameThatCannotBeReadIsShownAsItsNeighbourButNeverFlagged()
{
await using var fx = await ProcessingFixture.CreateAsync(context);
var sequence = await fx.AddAsync(Scene());
// Another program holds the file during the render (antivirus, backup): a read error says nothing about the file.
fx.Decoder.Unreadable[ProcessingFixture.FileName(10)] = 0;
await fx.StartEngineAsync();
var job = await fx.EnqueueAsync(ProcessingJobs.Render(sequence, Mjpeg, 96, 64));
var done = await fx.WaitForAsync(job.Id, JobState.Done);
Assert.Contains(Loc.L("Export.Log.StandIns", "11"), done.LogTail);
var sidecar = Titano.Core.Archiving.ManifestStore.ReadSequence(fx.Folder(sequence).ManifestPath);
Assert.Equal(FrameFlags.None, sidecar.Frames[10].Flags & FrameFlags.Undecodable);
Assert.NotEqual(DecodeTier.Unavailable, sidecar.Frames[10].Tier);
Assert.False(sidecar.Status.HasFlag(SequenceStatus.HasWarnings), "no warning");
Assert.Equal(FrameFlags.None, (await fx.Db.Frames.ListBySequenceAsync(sequence.Id, cancellationToken: fx.Ct))[10].Flags);
}
[Test]
public async Task ASmallRenderKeepsTheStoredSizeOfTheLargestPreview()
{
await using var fx = await ProcessingFixture.CreateAsync(context);
var sequence = await fx.AddAsync(Scene());
// A CR3 on the server: the probe finds its full-size 6000×4000 preview, which caps the output.
for (var i = 0; i < Frames; i++)
fx.Decoder.Tiers[ProcessingFixture.FileName(i)] = new FileTier(ImageFileFormat.Jpeg, DecodeTier.EmbeddedPreview, "test-preview", "test", 6000, 4000);
await fx.StartEngineAsync();
var light = await fx.EnqueueAsync(ProcessingJobs.LightAnalysis([sequence.Id]));
await fx.WaitForAsync(light.Id, JobState.Done);
var probed = Titano.Core.Archiving.ManifestStore.ReadSequence(fx.Folder(sequence).ManifestPath).Frames[3];
Assert.Equal((FrameFlags.PreviewOnly, (int?)6000, (int?)4000), (probed.Flags, probed.PreviewW, probed.PreviewH));
// A small render decodes the smallest preview that reaches its size (1620×1080): that is not the largest.
for (var i = 0; i < Frames; i++) fx.Decoder.PreviewDecodes[ProcessingFixture.FileName(i)] = (1620, 1080);
var job = await fx.EnqueueAsync(ProcessingJobs.Render(sequence, Mjpeg, 96, 64));
await fx.WaitForAsync(job.Id, JobState.Done);
var frames = Titano.Core.Archiving.ManifestStore.ReadSequence(fx.Folder(sequence).ManifestPath).Frames;
Assert.True(frames.All(f => f is { Tier: DecodeTier.EmbeddedPreview, PreviewW: 6000, PreviewH: 4000 }), "sidecar preview sizes kept");
var rows = await fx.Db.Frames.ListBySequenceAsync(sequence.Id, cancellationToken: fx.Ct);
Assert.True(rows.All(f => (f.PreviewWidth, f.PreviewHeight) == (6000, 4000)), "index preview sizes kept");
Assert.Equal((6000, 4000), (Titano.Core.Advice.SequenceContext.FromFrames(rows).PreviewWidth, Titano.Core.Advice.SequenceContext.FromFrames(rows).PreviewHeight));
}
[Test]
public void FramesARenderNodeReceivesKeepTheirTierFields()
{
var dto = new Titano.Protocol.FrameDto(Guid.CreateVersion7(), Guid.CreateVersion7(), 3, "IMG_0004.CR3", 25_000_000, new string('a', 64),
new DateTime(2026, 9, 28, 18, 0, 20), 120, 1.0 / 250, 8, 100, 24, 6, 6000, 4000, "cr3",
(long)(FrameFlags.PreviewOnly | FrameFlags.Intervalometer), 1620, 1080);
var frame = RenderNodeClient.ToFrame(dto);
Assert.Equal((ImageFileFormat.Cr3, FrameFlags.PreviewOnly | FrameFlags.Intervalometer, 1620, 1080),
(frame.Format, frame.Flags, frame.PreviewWidth, frame.PreviewHeight));
// The node's advisor caps a portrait sequence at its previews, oriented like the main image.
var tiers = Titano.Core.Advice.SequenceContext.FromFrames([frame]);
Assert.Equal((1, 1080, 1620), (tiers.PreviewOnlyCount, tiers.PreviewWidth, tiers.PreviewHeight));
}
[Test]
public async Task OnlyFramesTheRenderShowsGetAStandIn()
{
await using var fx = await ProcessingFixture.CreateAsync(context);
var sequence = await fx.AddAsync(Scene());
foreach (var i in new[] { 1, 3 })
await File.WriteAllBytesAsync(Path.Combine(fx.Archive.Paths.Root, "Synthetic", ProcessingFixture.FileName(i)), new byte[5000], fx.Ct);
var frames = await SequenceFrames.LoadAsync(fx.Db, fx.Archive.Archive, sequence, useProxies: false, fx.Ct);
// A plan that skips the odd frames (a speed-up): the read-ahead meets frames 1 and 3, the pipeline never asks.
var source = new SequenceFrameSource(frames, frames.LightFrames(), new ImageDecoder([fx.Decoder]), new FrameWindowOptions { Ahead = 4 })
{
Originals = true,
RenderExcluded = new HashSet<int>(),
};
await using (source)
{
foreach (var index in new[] { 0, 2, 4, 6 })
{
using var frame = await source.GetFrameAsync(index, 64, DecodePurpose.Render, fx.Ct);
Assert.Equal(64, frame.Width);
}
// The read-ahead decodes in the background: wait until it has met frame 4 (index 3).
for (var waited = 0; !source.Observed.ContainsKey(3) && waited < 200; waited++) await Task.Delay(50, fx.Ct);
}
Assert.Equal(DecodeTier.Unavailable, source.Observed[3].Tier);
Assert.Empty(source.StandIns);
// Asked for, a damaged frame shows its neighbour and is listed.
await using var asked = new SequenceFrameSource(frames, frames.LightFrames(), new ImageDecoder([fx.Decoder]))
{
RenderExcluded = new HashSet<int>(),
};
using (var standIn = await asked.GetFrameAsync(3, 64, DecodePurpose.Render, fx.Ct))
Assert.Equal(64, standIn.Width);
Assert.SequenceEqual([3], asked.StandIns);
}
[Test]
public async Task ARenderInterruptedByACrashStartsItsOutputAgainCleanly()
{
@@ -318,17 +318,19 @@ public abstract class CatalogRepositoryTests(TestContext context) : RepositoryTe
await db.Archives.UpsertAsync(archive, Ct);
var sequence = TestData.Sequence(archive.Id);
await db.Sequences.UpsertAsync(sequence, Ct);
var frames = Enumerable.Range(0, 40).Select(i => TestData.Frame(sequence.Id, i) with { Flags = FrameFlags.Duplicate }).ToList();
// More frames than one batched PostgreSQL statement holds.
const int count = 1205;
var frames = Enumerable.Range(0, count).Select(i => TestData.Frame(sequence.Id, i) with { Flags = FrameFlags.Duplicate }).ToList();
await db.Frames.BulkInsertAsync(frames, Ct);
var tiers = frames.Select(f => new FrameTierFields(f.Id, f.Index % 2 == 0 ? FrameFlags.PreviewOnly : FrameFlags.Undecodable, 1620 + f.Index, 1080)).ToList();
Assert.Equal(40, await db.Frames.UpdateTiersAsync(tiers, Ct));
var read = await db.Frames.ListBySequenceAsync(sequence.Id, cancellationToken: Ct);
Assert.Equal(count, await db.Frames.UpdateTiersAsync(tiers, Ct));
var read = await Titano.Data.Processing.SequenceFrames.ListAsync(db, sequence.Id, Ct);
Assert.SequenceEqual(tiers.Select(t => (t.Flags, t.PreviewWidth, t.PreviewHeight)), read.Select(f => (f.Flags, f.PreviewWidth, f.PreviewHeight)));
// A sync from the sidecar writes the tier fields like every other one.
await db.Frames.SyncSequenceAsync(sequence.Id, [.. read.Select(f => f with { PreviewWidth = 4, PreviewHeight = 3, Flags = FrameFlags.None })], Ct);
Assert.True((await db.Frames.ListBySequenceAsync(sequence.Id, cancellationToken: Ct)).All(f => (f.Flags, f.PreviewWidth, f.PreviewHeight) == (FrameFlags.None, 4, 3)));
Assert.True((await Titano.Data.Processing.SequenceFrames.ListAsync(db, sequence.Id, Ct)).All(f => (f.Flags, f.PreviewWidth, f.PreviewHeight) == (FrameFlags.None, 4, 3)));
}
[Test]