Files
Chronos/tests/Titano.Data.Tests/Processing/ProcessingFixture.cs
T
Claude 4d321a84b0 fix(imaging): record decode tiers per frame and keep undecodable frames out of renders
BRIEF §8.2 tiers 2 and 3 had no writer: PreviewOnly and Undecodable were never set,
so the advisor could not cap the output at the preview size and one unreadable frame
failed a whole render.

- ImageDecoder.Probe names a file's tier without decoding it (JPEG, PNG and TIFF
  without opening the file); the light analysis probes frames whose tier is unknown,
  and the deep analysis, renders from originals and thumbnail/proxy passes record
  what decoding actually found (FrameTiers). Sidecar frames gain "tier", "previewW"
  and "previewH"; the index gains frames.preview_width/height and
  sequences.preview_only_count (migration 0005, SQLite and PostgreSQL).
- SequenceContext takes the preview size from preview-only frames, so the advisor's
  output cap applies; the light analysis reports format tiers (preview-only frames as
  information, undecodable ones as a warning under the deep analysis' key, listed
  once); a sequence that gains undecodable frames warns at once.
- Only a file in a format the host decodes natively is flagged undecodable; files
  beyond the host's decoders keep their tier without the flag, so render nodes and
  proxies can still supply them.
- A frame that stops decoding during a render is replaced by its nearest planned
  neighbour and logged instead of failing the job; the render fails only when no
  planned frame decodes.
- The Library card shows a preview-only chip with the count (local and server
  sequences: SequenceDto.PreviewOnlyCount, FrameDto preview size).
- GoPro GPR decodes through the JPEG the camera writes beside it when that is larger
  than the embedded preview (BRIEF §8.1).
- Thumbnails use a stored embedded preview only when it reaches 320 px; a smaller one
  sends the frame through the host's decoders instead (BRIEF §8.5).

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
Claude-Session: https://claude.ai/code/session_012iLMBNwBE2GnphAsu4cjaC
2026-10-12 09:58:36 +00:00

148 lines
7.0 KiB
C#

using System.Collections.Concurrent;
using Titano.Core.Analysis.Deep;
using Titano.Core.Archiving;
using Titano.Core.Codecs.Jpeg;
using Titano.Core.Diagnostics.Synthetic;
using Titano.Core.Imaging;
using Titano.Core.Imaging.Decoders;
using Titano.Core.Metadata;
using Titano.Core.Model;
using Titano.Data.Archiving;
using Titano.Data.Jobs;
using Titano.Data.Processing;
using Titano.Data.Tests.Archiving;
using Titano.Testing;
namespace Titano.Data.Tests.Processing;
/// <summary>A temporary archive holding synthetic sequences (JPEGs with Exif), a processing engine and its job engine.</summary>
internal sealed class ProcessingFixture : IAsyncDisposable
{
/// <summary>Small sources are analysed at native size; batches of two frames between checkpoints.</summary>
public static readonly DeepAnalysisOptions DeepOptions = new() { MinAnalysisLongEdge = 64, FlowEvery = 5, MaxDegreeOfParallelism = 2, RegionSampleFrames = 9 };
private JobEngine? _engine;
private ProcessingFixture(ArchiveFixture archive, GateDecoder decoder, Func<ProcessingOptions, ProcessingOptions>? configure)
{
Archive = archive;
Decoder = decoder;
var options = new ProcessingOptions
{
Archive = new ArchiveServiceOptions { Decoder = new ImageDecoder([decoder]), Owner = "test", LeaseWait = TimeSpan.FromSeconds(20) },
DeepAnalysis = DeepOptions,
ProducedBy = "test-pc",
};
Processing = new ProcessingService(archive.Db, configure?.Invoke(options) ?? options);
}
public ArchiveFixture Archive { get; }
public GateDecoder Decoder { get; }
public ProcessingService Processing { get; }
public TitanoDatabase Db => Archive.Db;
public CancellationToken Ct => Archive.Ct;
public JobEngine Engine => _engine ?? throw new InvalidOperationException("Start the engine first.");
/// <param name="configure">Changes to the processing options (a server's host and render-node check).</param>
public static async Task<ProcessingFixture> CreateAsync(TestContext context, Func<ProcessingOptions, ProcessingOptions>? configure = null) =>
new(await ArchiveFixture.CreateAsync(context), new GateDecoder(), configure);
/// <summary>A started engine with the processing handlers, and the archive's when asked (one worker unless asked, fast checkpoints).</summary>
public async Task<JobEngine> StartEngineAsync(bool withArchive = false, int workers = 1)
{
_engine = new JobEngine(Db, [.. withArchive ? Archive.Service.CreateJobHandlers() : [], .. Processing.CreateJobHandlers()],
new JobEngineOptions { Workers = workers, WorkerId = "test", PollInterval = TimeSpan.FromMilliseconds(50), CheckpointInterval = TimeSpan.FromMilliseconds(50) });
await _engine.StartAsync(Ct);
return _engine;
}
public Task<Job> EnqueueAsync(Titano.Data.Archiving.Jobs.ArchiveJobRequest request) => Engine.EnqueueAsync(request, cancellationToken: Ct);
public Task<Job> WaitForAsync(Guid jobId, JobState state) => Archive.WaitForAsync(jobId, state, 60);
/// <summary>Writes frames <paramref name="first"/>… of <paramref name="sequence"/> as JPEGs with their Exif into
/// <paramref name="folder"/> (adopted in place) and indexes the archive.</summary>
public async Task<Sequence> AddAsync(SyntheticSequence sequence, string folder = "Synthetic", int first = 0)
{
Write(sequence, folder, first);
await Archive.ScanAsync(ScanMode.Full);
var location = (await Db.Sequences.ListLocationsAsync(Archive.Archive.Id, Ct)).Single(l => l.Folder == folder);
return (await Db.Sequences.GetAsync(location.Id, Ct))!;
}
/// <summary>Writes frames <paramref name="first"/>… of <paramref name="sequence"/> as JPEGs with their Exif into <paramref name="folder"/>.</summary>
public void Write(SyntheticSequence sequence, string folder = "Synthetic", int first = 0)
{
var directory = Path.Combine(Archive.Paths.Root, folder);
Directory.CreateDirectory(directory);
for (var i = first; i < sequence.Count; i++)
{
using var image = sequence.Render(i);
var exif = ExifWriter.BuildJpegApp1(sequence.Metadata[i]);
File.WriteAllBytes(Path.Combine(directory, FileName(i)), JpegEncoder.Encode(image, new JpegEncodeOptions { Quality = 95, App1Segments = [exif[4..]] }));
}
}
public static string FileName(int index) => $"IMG_{index + 1:0000}.JPG";
public SequencePaths Folder(Sequence sequence) => Archive.Paths.Sequence(sequence.Folder);
public async ValueTask DisposeAsync()
{
if (_engine is not null) await _engine.DisposeAsync();
await Archive.DisposeAsync();
}
}
/// <summary>Managed decoding that records every request and can hold one file's decode until the job is cancelled.</summary>
internal sealed class GateDecoder : IImageDecoderProvider
{
private readonly ManagedDecoderProvider _managed = new();
public string Name => "gate";
public ConcurrentQueue<DecodeRequest> Requests { get; } = new();
/// <summary>File name whose decode waits for cancellation; null decodes everything.</summary>
public string? Hold { get; set; }
/// <summary>Holds only decodes of this purpose (null: any).</summary>
public DecodePurpose? HoldPurpose { get; set; }
/// <summary>Holds only decodes with this long edge (null: any).</summary>
public int? HoldEdge { get; set; }
/// <summary>Completed when a decode of <see cref="Hold"/> started waiting.</summary>
public TaskCompletionSource Holding { get; private set; } = new(TaskCreationOptions.RunContinuationsAsynchronously);
public void HoldFrame(int index, DecodePurpose? purpose = null, int? edge = null)
{
Holding = new TaskCompletionSource(TaskCreationOptions.RunContinuationsAsynchronously);
(HoldPurpose, HoldEdge) = (purpose, edge);
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>
public ConcurrentDictionary<string, FileTier> Tiers { 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 DecodedImage? Decode(DecodeRequest request, CancellationToken cancellationToken = default)
{
Requests.Enqueue(request);
if (Hold is { } hold && Path.GetFileName(request.Path) == 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);
}
}