using Chronos.Core.Analysis.Deep; using Chronos.Core.Archiving; using Chronos.Core.Imaging; using Chronos.Core.Imaging.Decoders; using Chronos.Core.Model; using Chronos.Data.Archiving; using Chronos.Data.Processing; using Chronos.Testing; using SequenceFrames = Chronos.Data.Processing.SequenceFrames; namespace Chronos.Data.Tests.Processing; /// /// Photos decoded at once (owner review 1, R2): the whole parallelism, unless developing large originals whole would /// exceed the memory budget. Only the decodes that develop an original count: a JPEG at least twice the size asked for /// decodes scaled down, and a frame read through its proxy or an embedded preview of that size needs no development. /// public sealed class DecodeParallelismTests { /// 2 GiB: two 24 MP developments (some 768 MB each), one of 45 MP. private const long Budget = 2L << 30; private static readonly ImageDecoder Server = DecoderFactory.CreateServer(); [Test] public void WhatADecodeDevelopsWholeDependsOnTheFormatTheSizeAskedForAndThePreview() { const int w = 6000, h = 4000; Assert.Equal(0, DecodeBudget.Developed(ImageFileFormat.Jpeg, w, h, 0, 320, preferPreview: false)); Assert.Equal(0, DecodeBudget.Developed(ImageFileFormat.Jpeg, w, h, 0, 3000, preferPreview: false)); // A 4K render of a 24 MP JPEG decodes it nearly whole; full size (0) always does. Assert.Equal(24_000_000, DecodeBudget.Developed(ImageFileFormat.Jpeg, w, h, 0, 3840, preferPreview: false)); Assert.Equal(24_000_000, DecodeBudget.Developed(ImageFileFormat.Jpeg, w, h, 0, 0, preferPreview: true)); // A preview reaching the size serves only decodes that prefer previews; a render develops the RAW. Assert.Equal(0, DecodeBudget.Developed(ImageFileFormat.Cr2, w, h, 1620, 512, preferPreview: true)); Assert.Equal(24_000_000, DecodeBudget.Developed(ImageFileFormat.Cr2, w, h, 1620, 512, preferPreview: false)); Assert.Equal(24_000_000, DecodeBudget.Developed(ImageFileFormat.Cr2, w, h, 1620, 2048, preferPreview: true)); Assert.Equal(24_000_000, DecodeBudget.Developed(ImageFileFormat.Tiff, w, h, 0, 320, preferPreview: true)); // Only RAW decoders take a preview by choice: a TIFF's or a HEIF's is never a shortcut. Assert.Equal(24_000_000, DecodeBudget.Developed(ImageFileFormat.Tiff, w, h, w, 512, preferPreview: true)); Assert.Equal(24_000_000, DecodeBudget.Developed(ImageFileFormat.Heif, w, h, 512, 512, preferPreview: true)); Assert.Equal(0, DecodeBudget.Developed(ImageFileFormat.Dng, w, h, 1024, 512, preferPreview: true)); Assert.Equal(DecodeBudget.UnknownPixels, DecodeBudget.Developed(ImageFileFormat.Png, 0, 0, 0, 320, preferPreview: true)); } [Test] public void ThumbnailAndProxyPassesHoldBackOnlyForTheOriginalsTheyDevelop() { static SidecarFrame Frame(ImageFileFormat format, int width, int height, int? preview = null) => new() { Id = Guid.NewGuid(), File = "f", Format = format, W = width, H = height, PreviewW = preview, PreviewH = preview * 2 / 3 }; static int Thumbnails(params SidecarFrame[] frames) => ArchiveImages.PassParallelism(8, Budget, frames, 320, preferPreview: true, Server); static int Proxies(int edge, params SidecarFrame[] frames) => ArchiveImages.PassParallelism(8, Budget, frames, edge, preferPreview: false, Server); Assert.Equal(8, Thumbnails(Frame(ImageFileFormat.Jpeg, 8192, 5464), Frame(ImageFileFormat.Cr2, 8192, 5464, preview: 1620))); // A HEIF the server reads through its proxy or preview develops nothing either. Assert.Equal(8, Thumbnails(Frame(ImageFileFormat.Heif, 8192, 5464))); // A 45 MP TIFF is decoded whole: one at a time within 2 GiB, whatever the JPEGs beside it. Assert.Equal(1, Thumbnails(Frame(ImageFileFormat.Jpeg, 8192, 5464), Frame(ImageFileFormat.Tiff, 8192, 5464))); // A RAW whose preview is smaller than the thumbnail is developed; a size nobody knows counts as 24 MP. Assert.Equal(2, Thumbnails(Frame(ImageFileFormat.Cr2, 6000, 4000, preview: 160))); Assert.Equal(2, Thumbnails(Frame(ImageFileFormat.Png, 0, 0))); Assert.Equal(1, ArchiveImages.PassParallelism(0, Budget, [Frame(ImageFileFormat.Jpeg, 48, 32)], 320, preferPreview: true, Server)); // Proxies of 2048 px scale JPEGs down but develop every RAW, preview or not; full-size proxies decode JPEGs whole. Assert.Equal(8, Proxies(2048, Frame(ImageFileFormat.Jpeg, 8192, 5464))); Assert.Equal(2, Proxies(2048, Frame(ImageFileFormat.Cr2, 6000, 4000, preview: 6000))); Assert.Equal(2, Proxies(0, Frame(ImageFileFormat.Jpeg, 6000, 4000))); } [Test] public void ADeepAnalysisHoldsBackOnlyForTheOriginalsItDevelops() { var analysis = DeepAnalysisOptions.Default with { MaxDegreeOfParallelism = 8 }; Assert.Equal(8, SequenceAnalyser.AnalysisParallelism(analysis, Frames((ImageFileFormat.Jpeg, 8192, 5464, 0)), Budget)); // A DNG whose embedded preview reaches the analysis size (half the long edge, at most 2048 px) develops nothing. Assert.Equal(8, SequenceAnalyser.AnalysisParallelism(analysis, Frames((ImageFileFormat.Dng, 5568, 4872, 2784)), Budget)); Assert.Equal(1, SequenceAnalyser.AnalysisParallelism(analysis, Frames((ImageFileFormat.Dng, 5568, 4872, 1024), (ImageFileFormat.Tiff, 8192, 5464, 0)), Budget)); // Frames read through their proxies decode as JPEGs. Assert.Equal(8, SequenceAnalyser.AnalysisParallelism(analysis, Frames((ImageFileFormat.Tiff, 8192, 5464, 0)) with { Proxied = [true] }, Budget)); } [Test] public async Task AWindowHoldsBackWheneverItsDecodesDevelopOriginals() { var window = new FrameWindowOptions { MemoryBudget = Budget, MaxConcurrentLoads = 8, Ahead = 8 }; var tiffs = Frames((ImageFileFormat.Tiff, 8192, 5464, 0), (ImageFileFormat.Tiff, 8192, 5464, 0)); // A render node renders downloaded originals without counting what they decode (Originals unset): still held back. await using (var node = new SequenceFrameSource(tiffs, tiffs.LightFrames(), Server, window)) { Assert.Equal(1, node.WindowOptions(1920, DecodePurpose.Render).MaxConcurrentLoads); // A preparation pass decodes TIFFs whole too. Assert.Equal(1, node.WindowOptions(512, DecodePurpose.Analysis).MaxConcurrentLoads); Assert.Equal((Budget, 8), (node.WindowOptions(1920, DecodePurpose.Render).MemoryBudget, node.WindowOptions(1920, DecodePurpose.Render).Ahead)); } // RAWs whose previews reach the preparation's size: the preparation runs them all, the render develops them. var raws = Frames((ImageFileFormat.Cr2, 6000, 4000, 1620), (ImageFileFormat.Cr2, 6000, 4000, 1620)); await using (var raw = new SequenceFrameSource(raws, raws.LightFrames(), Server, window) { Originals = true }) { Assert.Equal(8, raw.WindowOptions(512, DecodePurpose.Analysis).MaxConcurrentLoads); Assert.Equal(2, raw.WindowOptions(1920, DecodePurpose.Render).MaxConcurrentLoads); } // Proxies decode as JPEGs, flagged or found in a proxy cache's .jpg files. await using (var proxied = new SequenceFrameSource(tiffs with { Proxied = [true, true] }, tiffs.LightFrames(), Server, window)) Assert.Equal(8, proxied.WindowOptions(1920, DecodePurpose.Render).MaxConcurrentLoads); var cached = raws with { Files = ["cache/0001.jpg", "cache/0002.JPG"] }; await using var cache = new SequenceFrameSource(cached, cached.LightFrames(), Server, window); Assert.Equal(8, cache.WindowOptions(1920, DecodePurpose.Render).MaxConcurrentLoads); } [Test] public async Task ADemandLoadWaitsForTheReadAheadOfADevelopingWindow() { // The window caps its read-ahead only: a render that skips frames (a speed-up) asks for a frame the read-ahead is // not decoding while it develops another, and both would run. One development at a time fits this budget. var decoder = new CountingDecoder(); var tiffs = Frames([.. Enumerable.Repeat((ImageFileFormat.Tiff, 8192, 5464, 0), 8)]); var frames = tiffs with { Files = [.. tiffs.Files.Select(f => f + ".tif")] }; var window = new FrameWindowOptions { MemoryBudget = Budget, MaxConcurrentLoads = 8, Ahead = 8 }; await using (var source = new SequenceFrameSource(frames, frames.LightFrames(), new ImageDecoder([decoder]), window)) { Assert.Equal(1, source.DevelopingLimit(1920, DecodePurpose.Render)); foreach (var index in new[] { 0, 2, 4, 6 }) using (await source.GetFrameAsync(index, 1920, DecodePurpose.Render, CancellationToken.None)) { } } Assert.Equal(1, decoder.Peak); Assert.Greater(decoder.Decodes, 4); } /// Decodes anything as a tiny frame, slowly, and counts how many decodes run at once. private sealed class CountingDecoder : IImageDecoderProvider { private int _running; private int _peak; private int _decodes; public int Peak => Volatile.Read(ref _peak); public int Decodes => Volatile.Read(ref _decodes); public string Name => "counting"; public DecodeTier? TierFor(ImageFileFormat format) => DecodeTier.Native; public DecodedImage? Decode(DecodeRequest request, CancellationToken cancellationToken = default) { var now = Interlocked.Increment(ref _running); int seen; while ((seen = Volatile.Read(ref _peak)) < now && Interlocked.CompareExchange(ref _peak, now, seen) != seen) { } Interlocked.Increment(ref _decodes); Thread.Sleep(40); Interlocked.Decrement(ref _running); return new DecodedImage(new ImageBuffer(8, 6), DecodeTier.Native, Name, 8192, 5464); } } private static SequenceFrames Frames(params (ImageFileFormat Format, int Width, int Height, int Preview)[] specs) { var sequence = new Sequence { Id = Guid.NewGuid(), ArchiveId = Guid.NewGuid(), Folder = "a" }; var frames = specs.Select((s, i) => new Frame { Id = Guid.NewGuid(), SequenceId = sequence.Id, Index = i, RelativePath = $"a/IMG_{i:0000}", FileName = $"IMG_{i:0000}", CapturedAt = new DateTime(2026, 10, 16, 6, 0, 0).AddSeconds(10 * i), Width = s.Width, Height = s.Height, Format = s.Format, PreviewWidth = s.Preview, PreviewHeight = s.Preview * 2 / 3, }).ToList(); return new SequenceFrames(sequence, frames, [.. frames.Select(f => (string?)f.RelativePath)]); } }