From d09b5a5cfb1b8e2f0474e2a9b748e0fa7fc20377 Mon Sep 17 00:00:00 2001 From: Claude Date: Mon, 12 Oct 2026 09:58:14 +0000 Subject: [PATCH 1/7] fix(data): name the right mirror file in the 0003 migration headers Both 0003_server_uploads.sql files still pointed at the 0005 names they had before the migrations were renumbered without gaps. Comment-only: migrations are keyed by version, not by content. Co-Authored-By: Claude Opus 5.5 Claude-Session: https://claude.ai/code/session_012iLMBNwBE2GnphAsu4cjaC --- src/Titano.Data/Migrations/postgres/0003_server_uploads.sql | 2 +- src/Titano.Data/Migrations/sqlite/0003_server_uploads.sql | 2 +- 2 files changed, 2 insertions(+), 2 deletions(-) diff --git a/src/Titano.Data/Migrations/postgres/0003_server_uploads.sql b/src/Titano.Data/Migrations/postgres/0003_server_uploads.sql index 271f679a..3396cdf8 100644 --- a/src/Titano.Data/Migrations/postgres/0003_server_uploads.sql +++ b/src/Titano.Data/Migrations/postgres/0003_server_uploads.sql @@ -1,5 +1,5 @@ -- Titano Server additions (PostgreSQL): automatic grouping key of frame uploads, render-result uploads of --- render-node jobs, and the sign-in log lookup. Mirrors sqlite/0005_server_uploads.sql. +-- render-node jobs, and the sign-in log lookup. Mirrors sqlite/0003_server_uploads.sql. ALTER TABLE uploads ADD COLUMN session_key text; ALTER TABLE uploads ADD COLUMN job_id uuid; diff --git a/src/Titano.Data/Migrations/sqlite/0003_server_uploads.sql b/src/Titano.Data/Migrations/sqlite/0003_server_uploads.sql index ce6e5d64..51409056 100644 --- a/src/Titano.Data/Migrations/sqlite/0003_server_uploads.sql +++ b/src/Titano.Data/Migrations/sqlite/0003_server_uploads.sql @@ -1,5 +1,5 @@ -- Titano Server additions (SQLite): automatic grouping key of frame uploads, render-result uploads of render-node --- jobs, and the sign-in log lookup. Mirrors postgres/0005_server_uploads.sql. +-- jobs, and the sign-in log lookup. Mirrors postgres/0003_server_uploads.sql. ALTER TABLE uploads ADD COLUMN session_key TEXT; ALTER TABLE uploads ADD COLUMN job_id TEXT; From 61e61702c601c142a9cfb319e338250e34511b2b Mon Sep 17 00:00:00 2001 From: Claude Date: Mon, 12 Oct 2026 09:58:21 +0000 Subject: [PATCH 2/7] fix(metadata): read exposure mode, interval shooting and RAW size from MakerNotes MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit BRIEF §9 lists exposure mode, intervalometer hints and sRaw flags among the MakerNote fields; no vendor parser read them. The vendor exposure mode (Canon, Olympus, Fujifilm, Panasonic, Sony) now fills ExposureProgram when Exif has none, Panasonic's TimerRecording becomes FrameMetadata.IntervalShooting, and Canon's SRAWQuality and Nikon's ImageSizeRAW / small NEF compression become FrameMetadata.RawSize. Nikon keeps its interval-shooting state in the encrypted ShotInfo, which stays undecoded. Co-Authored-By: Claude Opus 5.5 Claude-Session: https://claude.ai/code/session_012iLMBNwBE2GnphAsu4cjaC --- src/Titano.Core/Metadata/FrameMetadata.cs | 22 +++ .../Metadata/MakerNotes/CanonMakerNote.cs | 35 ++++- .../Metadata/MakerNotes/FujifilmMakerNote.cs | 13 ++ .../Metadata/MakerNotes/MakerNoteValues.cs | 10 ++ .../Metadata/MakerNotes/NikonMakerNote.cs | 15 ++- .../Metadata/MakerNotes/OlympusMakerNote.cs | 13 +- .../Metadata/MakerNotes/PanasonicMakerNote.cs | 25 +++- .../Metadata/MakerNotes/SonyMakerNote.cs | 16 ++- src/Titano.Core/Metadata/MetadataBuilder.cs | 4 +- .../Metadata/MakerNoteTests.cs | 125 ++++++++++++++++++ 10 files changed, 271 insertions(+), 7 deletions(-) create mode 100644 tests/Titano.Core.Tests/Metadata/MakerNoteTests.cs diff --git a/src/Titano.Core/Metadata/FrameMetadata.cs b/src/Titano.Core/Metadata/FrameMetadata.cs index e38d2714..2fca42c5 100644 --- a/src/Titano.Core/Metadata/FrameMetadata.cs +++ b/src/Titano.Core/Metadata/FrameMetadata.cs @@ -10,6 +10,19 @@ public enum PreviewKind FullSize, } +/// Resolution a camera recorded its RAW at (BRIEF §9 sRaw flags). +public enum RawSize +{ + /// Full sensor resolution. + Full, + + /// Canon mRAW (sRAW1), Nikon RAW M. + Medium, + + /// Canon sRAW (sRAW2), Nikon RAW S and sRAW. + Small, +} + /// An embedded JPEG inside a RAW/TIFF/HEIF/JPEG(MPF) container. public sealed record EmbeddedPreview(long Offset, long Length, int Width, int Height, PreviewKind Kind); @@ -59,6 +72,15 @@ public sealed record FrameMetadata public long? ShutterCount { get; init; } public bool? ImageStabilization { get; init; } + /// + /// Intervalometer hint from the MakerNote: true when the camera's own interval-shooting or time-lapse mode took the + /// photo, false when the MakerNote names another drive mode, null when it says nothing (BRIEF §9). + /// + public bool? IntervalShooting { get; init; } + + /// Resolution of a camera RAW from the MakerNote (sRAW/mRAW, RAW M/S); null when not recorded. + public RawSize? RawSize { get; init; } + /// Embedded JPEGs, largest first. public IReadOnlyList Previews { get; init; } = []; diff --git a/src/Titano.Core/Metadata/MakerNotes/CanonMakerNote.cs b/src/Titano.Core/Metadata/MakerNotes/CanonMakerNote.cs index 6c60030e..59eda5e0 100644 --- a/src/Titano.Core/Metadata/MakerNotes/CanonMakerNote.cs +++ b/src/Titano.Core/Metadata/MakerNotes/CanonMakerNote.cs @@ -1,9 +1,13 @@ using System.Globalization; +using Titano.Core.Imaging; using Titano.Core.Metadata.Tiff; namespace Titano.Core.Metadata.MakerNotes; -/// Canon MakerNote: a bare IFD whose offsets are relative to the enclosing TIFF header (also CR3 CMT3). +/// +/// Canon MakerNote: a bare IFD whose offsets are relative to the enclosing TIFF header (also CR3 CMT3). CameraSettings +/// gives the stabiliser, the exposure mode and the sRAW size; Canon records no interval-shooting hint there. +/// internal static class CanonMakerNote { private const ushort CameraSettings = 0x0001; @@ -11,7 +15,9 @@ internal static class CanonMakerNote private const ushort LensModel = 0x0095; private const ushort InternalSerialNumber = 0x0096; private const ushort ColorTemperature = 0x00AE; + private const int ExposureModeIndex = 20; private const int ImageStabilizationIndex = 34; + private const int SRawQualityIndex = 46; public static void Parse(TiffReader r, Ifd ifd, MetadataBuilder m) { @@ -25,7 +31,21 @@ internal static class CanonMakerNote v.BodySerial ??= IfdValue.String(r, ifd, InternalSerialNumber); v.LensModel ??= IfdValue.String(r, ifd, LensModel); v.ColorTemperature ??= IfdValue.Int(r, ifd, ColorTemperature) is { } kelvin and > 0 and < 65535 ? kelvin : null; - if (IfdValue.UInts(r, ifd, CameraSettings) is { Length: > ImageStabilizationIndex } settings) + if (IfdValue.UInts(r, ifd, CameraSettings) is not { } settings) return; + if (settings.Length > ExposureModeIndex) + { + // CanonExposureMode: 1 Program, 2 Tv, 3 Av, 4 Manual, 5 Depth-of-field AE, 6 M-Dep, 7 Bulb, 8 Fv; 0 Easy (scene). + v.ExposureProgram ??= settings[ExposureModeIndex] switch + { + 1 or 8 => 2, + 2 => 4, + 3 => 3, + 4 or 7 => 1, + 5 or 6 => 5, + _ => null, + }; + } + if (settings.Length > ImageStabilizationIndex) { // 0 Off, 1 On, 2 Shoot only, 3 Panning, 4 Dynamic; +256 for the "(2)" variants. v.ImageStabilization ??= (settings[ImageStabilizationIndex] & 0xFF) switch @@ -35,5 +55,16 @@ internal static class CanonMakerNote _ => null, }; } + if (settings.Length > SRawQualityIndex && m.Format is ImageFileFormat.Cr2 or ImageFileFormat.Cr3) + { + // SRAWQuality: 0 full-size RAW, 1 sRAW1 (mRAW), 2 sRAW2 (sRAW). + v.RawSize ??= settings[SRawQualityIndex] switch + { + 0 => RawSize.Full, + 1 => RawSize.Medium, + 2 => RawSize.Small, + _ => null, + }; + } } } diff --git a/src/Titano.Core/Metadata/MakerNotes/FujifilmMakerNote.cs b/src/Titano.Core/Metadata/MakerNotes/FujifilmMakerNote.cs index 1b5f93e6..911ba340 100644 --- a/src/Titano.Core/Metadata/MakerNotes/FujifilmMakerNote.cs +++ b/src/Titano.Core/Metadata/MakerNotes/FujifilmMakerNote.cs @@ -9,6 +9,7 @@ internal static class FujifilmMakerNote { private const ushort SerialNumber = 0x0010; private const ushort ColorTemperature = 0x1005; + private const ushort PictureMode = 0x1031; private const ushort ImageStabilization = 0x1422; public static void Parse(MetadataBuilder m, long start, ReadOnlySpan header) @@ -27,5 +28,17 @@ internal static class FujifilmMakerNote // [0] type (0 none, 1 optical, 2 sensor-shift, …), [1] mode (0 off, 1/2 on). if (IfdValue.UInts(r, ifd, ImageStabilization) is [var type, var mode, ..]) v.ImageStabilization ??= type != 0 && mode != 0; + // PictureMode: 0x300 Manual, 6 Program AE, 0x100 Aperture priority, 0x200 Shutter priority, 1 Portrait, 2 Landscape, 4 Sports. + v.ExposureProgram ??= IfdValue.UInt(r, ifd, PictureMode) switch + { + 0x300 => 1, + 6 => 2, + 0x100 => 3, + 0x200 => 4, + 4 => 6, + 1 => 7, + 2 => 8, + _ => null, + }; } } diff --git a/src/Titano.Core/Metadata/MakerNotes/MakerNoteValues.cs b/src/Titano.Core/Metadata/MakerNotes/MakerNoteValues.cs index 9d2625b9..97da31b3 100644 --- a/src/Titano.Core/Metadata/MakerNotes/MakerNoteValues.cs +++ b/src/Titano.Core/Metadata/MakerNotes/MakerNoteValues.cs @@ -9,4 +9,14 @@ internal sealed class MakerNoteValues public bool? ImageStabilization { get; set; } public int? Iso { get; set; } public int? ColorTemperature { get; set; } + + /// The vendor's exposure mode as an Exif ExposureProgram value (1 manual, 2 program, 3 aperture + /// priority, 4 shutter priority, 5 creative, 6 action, 7 portrait, 8 landscape); scene modes without one stay null. + public int? ExposureProgram { get; set; } + + /// True when the camera's own interval-shooting or time-lapse mode took the photo, false for another known mode. + public bool? IntervalShooting { get; set; } + + /// Resolution of a camera RAW (Canon sRAW/mRAW, Nikon RAW M/S). + public RawSize? RawSize { get; set; } } diff --git a/src/Titano.Core/Metadata/MakerNotes/NikonMakerNote.cs b/src/Titano.Core/Metadata/MakerNotes/NikonMakerNote.cs index a7e50bb7..c53a86fe 100644 --- a/src/Titano.Core/Metadata/MakerNotes/NikonMakerNote.cs +++ b/src/Titano.Core/Metadata/MakerNotes/NikonMakerNote.cs @@ -3,14 +3,19 @@ using Titano.Core.Metadata.Tiff; namespace Titano.Core.Metadata.MakerNotes; /// Nikon MakerNote. Type 3 ("Nikon\0" + version 2.x) embeds its own TIFF header 10 bytes in; type 1 -/// ("Nikon\0" + version 1) has an IFD at +8 and headerless type 2 an IFD at +0, both relative to the Exif TIFF. +/// ("Nikon\0" + version 1) has an IFD at +8 and headerless type 2 an IFD at +0, both relative to the Exif TIFF. +/// The RAW size comes from ImageSizeRAW or the "small" NEF compression; the interval-shooting state lives only in the +/// encrypted ShotInfo, which is not decoded. internal static class NikonMakerNote { private const ushort Iso = 0x0002; private const ushort SerialNumber = 0x001D; private const ushort VrInfo = 0x001F; + private const ushort ImageSizeRaw = 0x003E; private const ushort Lens = 0x0084; + private const ushort NefCompression = 0x0093; private const ushort ShutterCount = 0x00A7; + private const uint SmallNef = 8; public static void Parse(TiffReader exif, long start, ReadOnlySpan header, MetadataBuilder m) { @@ -50,5 +55,13 @@ internal static class NikonMakerNote _ => null, }; } + // ImageSizeRAW: 1 Large, 2 Medium, 3 Small; NEFCompression 8 is the older sRAW ("Small"). + v.RawSize ??= IfdValue.UInt(r, ifd, ImageSizeRaw) switch + { + 1 => RawSize.Full, + 2 => RawSize.Medium, + 3 => RawSize.Small, + _ => IfdValue.UInt(r, ifd, NefCompression) is SmallNef ? RawSize.Small : null, + }; } } diff --git a/src/Titano.Core/Metadata/MakerNotes/OlympusMakerNote.cs b/src/Titano.Core/Metadata/MakerNotes/OlympusMakerNote.cs index e00e90b5..66455c9d 100644 --- a/src/Titano.Core/Metadata/MakerNotes/OlympusMakerNote.cs +++ b/src/Titano.Core/Metadata/MakerNotes/OlympusMakerNote.cs @@ -6,7 +6,8 @@ namespace Titano.Core.Metadata.MakerNotes; /// /// Olympus / OM System MakerNote. "OLYMPUS\0II\3\0" (IFD at +12) and "OM SYSTEM\0\0\0II\4\0" (IFD at +16) use offsets /// relative to the MakerNote start and their own byte order; legacy "OLYMP\0" has its IFD at +8 relative to the -/// Exif TIFF. Equipment (0x2010) gives serial and lens; CameraSettings (0x2020) gives the ORF preview and stabiliser. +/// Exif TIFF. Equipment (0x2010) gives serial and lens; CameraSettings (0x2020) gives the ORF preview, the stabiliser +/// and the exposure mode. /// internal static class OlympusMakerNote { @@ -17,6 +18,7 @@ internal static class OlympusMakerNote private const ushort PreviewImageValid = 0x0100; private const ushort PreviewImageStart = 0x0101; private const ushort PreviewImageLength = 0x0102; + private const ushort ExposureMode = 0x0200; private const ushort ImageStabilization = 0x0604; public static void Parse(TiffReader exif, long start, ReadOnlySpan header, MetadataBuilder m) @@ -52,6 +54,15 @@ internal static class OlympusMakerNote >= 1 and <= 4 => true, _ => null, }; + // 1 Manual, 2 Program, 3 Aperture priority, 4 Shutter priority, 5 Program shift. + v.ExposureProgram ??= IfdValue.UInt(r, settings, ExposureMode) switch + { + 1 => 1, + 2 or 5 => 2, + 3 => 3, + 4 => 4, + _ => null, + }; if (IfdValue.UInt(r, settings, PreviewImageValid) != 0 && IfdValue.UInt(r, settings, PreviewImageStart) is { } offset and > 0 && IfdValue.UInt(r, settings, PreviewImageLength) is { } length and > 0) diff --git a/src/Titano.Core/Metadata/MakerNotes/PanasonicMakerNote.cs b/src/Titano.Core/Metadata/MakerNotes/PanasonicMakerNote.cs index 9709903c..6217e5e7 100644 --- a/src/Titano.Core/Metadata/MakerNotes/PanasonicMakerNote.cs +++ b/src/Titano.Core/Metadata/MakerNotes/PanasonicMakerNote.cs @@ -2,13 +2,17 @@ using Titano.Core.Metadata.Tiff; namespace Titano.Core.Metadata.MakerNotes; -/// Panasonic MakerNote: "Panasonic\0\0\0" then an IFD whose offsets are relative to the Exif TIFF. +/// Panasonic MakerNote: "Panasonic\0\0\0" then an IFD whose offsets are relative to the Exif TIFF. Besides +/// identity and stabiliser it gives the shooting mode and TimerRecording, which says whether the camera's time-lapse +/// mode took the photo. internal static class PanasonicMakerNote { private const ushort ImageStabilization = 0x001A; + private const ushort ShootingMode = 0x001F; private const ushort InternalSerialNumber = 0x0025; private const ushort ColorTempKelvin = 0x0044; private const ushort LensType = 0x0051; + private const ushort TimerRecording = 0x0096; public static void Parse(TiffReader r, Ifd ifd, MetadataBuilder m) { @@ -23,5 +27,24 @@ internal static class PanasonicMakerNote 2 or (>= 4 and <= 7) or (>= 9 and <= 12) => true, _ => null, }; + // ShootingMode: 2 Portrait, 3 Scenery, 4 Sports, 6 Program, 7 Aperture priority, 8 Shutter priority, 11 Manual. + v.ExposureProgram ??= IfdValue.UInt(r, ifd, ShootingMode) switch + { + 11 => 1, + 6 => 2, + 7 => 3, + 8 => 4, + 4 => 6, + 2 => 7, + 3 => 8, + _ => null, + }; + // TimerRecording: 0 Off, 1 Time Lapse, 2 Stop-motion Animation, 3 Focus Bracketing. + v.IntervalShooting ??= IfdValue.UInt(r, ifd, TimerRecording) switch + { + 1 => true, + 0 or 2 or 3 => false, + _ => null, + }; } } diff --git a/src/Titano.Core/Metadata/MakerNotes/SonyMakerNote.cs b/src/Titano.Core/Metadata/MakerNotes/SonyMakerNote.cs index 692a129b..4280f7a2 100644 --- a/src/Titano.Core/Metadata/MakerNotes/SonyMakerNote.cs +++ b/src/Titano.Core/Metadata/MakerNotes/SonyMakerNote.cs @@ -3,12 +3,14 @@ using Titano.Core.Metadata.Tiff; namespace Titano.Core.Metadata.MakerNotes; /// Sony MakerNote ("SONY DSC/CAM" 12-byte header or headerless; offsets relative to the Exif TIFF). -/// Lens: the unencrypted LensSpec (BCD focal/aperture range); numeric LensType tables are not shipped. +/// Lens: the unencrypted LensSpec (BCD focal/aperture range); numeric LensType tables are not shipped. Exposure mode +/// from ExposureMode (0xB041). internal static class SonyMakerNote { private const ushort ColorTemperature = 0xB021; private const ushort ImageStabilization = 0xB026; private const ushort LensSpec = 0xB02A; + private const ushort ExposureMode = 0xB041; public static void Parse(TiffReader r, Ifd ifd, MetadataBuilder m) { @@ -22,6 +24,18 @@ internal static class SonyMakerNote 1 => true, _ => null, }; + // 0 Program AE, 1 Portrait, 3 Sports, 5 Landscape, 7 Aperture priority, 8 Shutter priority, 15 Manual. + v.ExposureProgram ??= IfdValue.UInt(r, ifd, ExposureMode) switch + { + 15 => 1, + 0 => 2, + 7 => 3, + 8 => 4, + 3 => 6, + 1 => 7, + 5 => 8, + _ => null, + }; } /// Bytes 1–2 short focal, 3–4 long focal (BCD mm), 5–6 apertures (BCD ×10); flags 0 and 7 are not decoded. diff --git a/src/Titano.Core/Metadata/MetadataBuilder.cs b/src/Titano.Core/Metadata/MetadataBuilder.cs index f55fe60e..e1673585 100644 --- a/src/Titano.Core/Metadata/MetadataBuilder.cs +++ b/src/Titano.Core/Metadata/MetadataBuilder.cs @@ -153,7 +153,7 @@ internal sealed class MetadataBuilder FocalLength = FocalLength, FocalLength35 = FocalLength35, ExposureBias = ExposureBias, - ExposureProgram = ExposureProgram, + ExposureProgram = ExposureProgram ?? MakerNote.ExposureProgram, MeteringMode = MeteringMode, WhiteBalanceMode = WhiteBalanceMode, ColorTemperature = MakerNote.ColorTemperature, @@ -167,6 +167,8 @@ internal sealed class MetadataBuilder Label = Xmp.Label, ShutterCount = MakerNote.ShutterCount, ImageStabilization = MakerNote.ImageStabilization, + IntervalShooting = MakerNote.IntervalShooting, + RawSize = MakerNote.RawSize, Previews = ResolvePreviews(main is { } m ? Math.Max(m.Width, m.Height) : 0), Warnings = _warnings.ToArray(), }; diff --git a/tests/Titano.Core.Tests/Metadata/MakerNoteTests.cs b/tests/Titano.Core.Tests/Metadata/MakerNoteTests.cs new file mode 100644 index 00000000..cceede71 --- /dev/null +++ b/tests/Titano.Core.Tests/Metadata/MakerNoteTests.cs @@ -0,0 +1,125 @@ +using Titano.Core.Imaging; +using Titano.Core.Metadata; +using Titano.Core.Metadata.Tiff; +using Titano.Core.Tests.Metadata.Builders; +using Titano.Testing; +using static Titano.Core.Tests.Metadata.MetadataTestHelpers; + +namespace Titano.Core.Tests.Metadata; + +/// +/// MakerNote fields of BRIEF §9 beyond identity: the vendor exposure mode (when Exif has no ExposureProgram), the +/// intervalometer hint and the reduced-size RAW flag, from synthetic vendor IFDs. +/// +public sealed class MakerNoteTests +{ + /// A JPEG whose Exif lacks ExposureProgram and carries after . + private static FrameMetadata Jpeg(string make, string model, byte[] prefix, IfdSpec makerNote, bool relativeToTiff = true, bool keepProgram = false) + { + var exif = Fixtures.ExifIfd(identity: false).Embedded(TiffTags.MakerNote, prefix, makerNote, relativeToTiff, littleEndian: true); + if (!keepProgram) exif.Without(TiffTags.ExposureProgram); + var m = Read(new JpegBuilder().Exif(TiffFile.Build(Fixtures.Ifd0(make, model, 1).Child(TiffTags.ExifIfd, exif))).Build(64, 48)); + AssertNoFault(m); + return m; + } + + /// A CR2 ("CR" header block) with only IFD0, the Exif IFD and a Canon MakerNote. + private static FrameMetadata Cr2(ushort[] cameraSettings) + { + var makerNote = new IfdSpec().Short(0x0001, cameraSettings); + var exif = Fixtures.ExifIfd(identity: false).Without(TiffTags.ExposureProgram) + .Embedded(TiffTags.MakerNote, [], makerNote, relativeToTiff: true, littleEndian: true); + var ifd0 = Fixtures.Ifd0("Canon", "Canon EOS 5D Mark IV", 1).Child(TiffTags.ExifIfd, exif); + var m = Read(TiffFile.Build(ifd0, true, headerExtra: [(byte)'C', (byte)'R', 2, 0, 0, 0, 0, 0]), "IMG_0001.CR2"); + AssertNoFault(m); + return m; + } + + private static ushort[] CanonSettings(ushort exposureMode, ushort sRawQuality) => + [.. Enumerable.Range(0, 48).Select(i => (ushort)(i switch { 20 => exposureMode, 46 => sRawQuality, _ => 0 }))]; + + [Theory] + [Case(1, 2)] + [Case(2, 4)] + [Case(3, 3)] + [Case(4, 1)] + [Case(5, 5)] + [Case(7, 1)] + [Case(8, 2)] + public void CanonExposureModeFillsTheExposureProgram(int canonMode, int exposureProgram) + { + var m = Jpeg("Canon", "Canon EOS R6", [], new IfdSpec().Short(0x0001, CanonSettings((ushort)canonMode, 0))); + Assert.Equal(exposureProgram, m.ExposureProgram); + // Not a RAW: the sRAW field says nothing about a JPEG. + Assert.Null(m.RawSize); + } + + [Test] + public void CanonEasyModeLeavesTheProgramUnknownAndExifWins() + { + Assert.Null(Jpeg("Canon", "Canon EOS R6", [], new IfdSpec().Short(0x0001, CanonSettings(0, 0))).ExposureProgram); + // The fixture's Exif says aperture priority (3); the MakerNote's manual mode does not override it. + Assert.Equal(3, Jpeg("Canon", "Canon EOS R6", [], new IfdSpec().Short(0x0001, CanonSettings(4, 0)), keepProgram: true).ExposureProgram); + } + + [Theory] + [Case(0, RawSize.Full)] + [Case(1, RawSize.Medium)] + [Case(2, RawSize.Small)] + public void CanonSRawQualityGivesTheRawSize(int quality, RawSize expected) + { + var m = Cr2(CanonSettings(3, (ushort)quality)); + Assert.Equal(ImageFileFormat.Cr2, m.Format); + Assert.Equal(expected, m.RawSize); + Assert.Equal(3, m.ExposureProgram); + // Bodies whose settings stop before the sRAW field record no size. + Assert.Null(Cr2([.. CanonSettings(3, (ushort)quality).Take(40)]).RawSize); + } + + [Test] + public void NikonRawSizeComesFromImageSizeRawOrTheSmallNefCompression() + { + static FrameMetadata Nikon(IfdSpec ifd) => Jpeg("NIKON CORPORATION", "NIKON Z 8", [], ifd); + Assert.Equal(RawSize.Medium, Nikon(new IfdSpec().Short(0x003E, 2)).RawSize); + Assert.Equal(RawSize.Small, Nikon(new IfdSpec().Short(0x003E, 3)).RawSize); + Assert.Equal(RawSize.Full, Nikon(new IfdSpec().Short(0x003E, 1).Short(0x0093, 3)).RawSize); + Assert.Equal(RawSize.Small, Nikon(new IfdSpec().Short(0x0093, 8)).RawSize); + Assert.Null(Nikon(new IfdSpec().Short(0x0093, 3)).RawSize); + // Nikon keeps interval shooting in its encrypted ShotInfo only. + Assert.Null(Nikon(new IfdSpec().Short(0x003E, 1)).IntervalShooting); + } + + [Test] + public void OlympusFujifilmAndSonyExposureModes() + { + var olympus = Jpeg("OLYMPUS", "E-M1MarkIII", [.. "OLYMP\0"u8, 1, 0], new IfdSpec().Child(0x2020, new IfdSpec().Short(0x0200, 4))); + Assert.Equal(4, olympus.ExposureProgram); + var fuji = Jpeg("FUJIFILM", "X-T5", [.. "FUJIFILM"u8, 12, 0, 0, 0], new IfdSpec().Short(0x1031, 0x300), relativeToTiff: false); + Assert.Equal(1, fuji.ExposureProgram); + var fujiAperture = Jpeg("FUJIFILM", "X-T5", [.. "FUJIFILM"u8, 12, 0, 0, 0], new IfdSpec().Short(0x1031, 0x100), relativeToTiff: false); + Assert.Equal(3, fujiAperture.ExposureProgram); + var sony = Jpeg("SONY", "ILCE-7M4", [.. "SONY DSC \0\0\0"u8], new IfdSpec().Short(0xB041, 8)); + Assert.Equal(4, sony.ExposureProgram); + var sonyAuto = Jpeg("SONY", "ILCE-7M4", [.. "SONY DSC \0\0\0"u8], new IfdSpec().Short(0xB041, 6)); + Assert.Null(sonyAuto.ExposureProgram); + } + + [Theory] + [Case(1, true)] + [Case(0, false)] + [Case(2, false)] + [Case(3, false)] + public void PanasonicTimerRecordingIsTheIntervalometerHint(int timerRecording, bool expected) + { + var m = Jpeg("Panasonic", "DC-GH6", [.. "Panasonic\0\0\0"u8], new IfdSpec().Short(0x001F, 11).Short(0x0096, (ushort)timerRecording)); + Assert.Equal(expected, m.IntervalShooting); + Assert.Equal(1, m.ExposureProgram); + } + + [Test] + public void WithoutTheTagsNothingIsGuessed() + { + var m = Jpeg("Panasonic", "DC-GH6", [.. "Panasonic\0\0\0"u8], new IfdSpec().Short(0x001A, 2)); + Assert.Equal(((bool?)null, (RawSize?)null, (int?)null), (m.IntervalShooting, m.RawSize, m.ExposureProgram)); + } +} From 61d913baa9fd95a739ef2a0e39e452bf11a9772c Mon Sep 17 00:00:00 2001 From: Claude Date: Mon, 12 Oct 2026 09:58:26 +0000 Subject: [PATCH 3/7] fix(data): probe PRAGMA compile_options next to the SQLite version check MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit BRIEF §6.2 asks for the probe at startup. The options are read once per process from an in-memory connection, logged with the library version and added to the desktop diagnostics bundle; nothing depends on them (no FTS or JSON use). Co-Authored-By: Claude Opus 5.5 Claude-Session: https://claude.ai/code/session_012iLMBNwBE2GnphAsu4cjaC --- .../Infrastructure/DiagnosticsBundle.cs | 12 ++++- src/Titano.Data/Sqlite/SqliteDatabase.cs | 47 +++++++++++++++++++ .../Sqlite/SqliteBindingTests.cs | 13 +++++ 3 files changed, 70 insertions(+), 2 deletions(-) diff --git a/src/Titano.App/Infrastructure/DiagnosticsBundle.cs b/src/Titano.App/Infrastructure/DiagnosticsBundle.cs index bb00d67c..084b1819 100644 --- a/src/Titano.App/Infrastructure/DiagnosticsBundle.cs +++ b/src/Titano.App/Infrastructure/DiagnosticsBundle.cs @@ -8,6 +8,7 @@ using Titano.Core.Export; using Titano.Core.Model; using Titano.Core.Windows.Codecs; using Titano.Data; +using Titano.Data.Sqlite; namespace Titano.App.Infrastructure; @@ -73,8 +74,15 @@ public static class DiagnosticsBundle return sb.ToString(); } - private static string DatabaseLine(TitanoDatabase? database) => - database is null ? "Index not open\n" : string.Create(CultureInfo.InvariantCulture, $"Index schema {database.SchemaVersion}\n"); + private static string DatabaseLine(TitanoDatabase? database) + { + if (database is null) return "Index not open\n"; + var line = string.Create(CultureInfo.InvariantCulture, $"Index schema {database.SchemaVersion}\n"); + // The SQLite build's options (BRIEF §6.2) explain behaviour differences between Windows versions. + return database.Database is SqliteDatabase + ? line + $"SQLite {SqliteDatabase.LibraryVersion}; compile options: {string.Join(' ', SqliteDatabase.CompileOptions)}\n" + : line; + } /// Graphics adapter, video encoders, WIC decoders per format and missing Store extensions. private static string Codecs(IEncoderInventory encoders) diff --git a/src/Titano.Data/Sqlite/SqliteDatabase.cs b/src/Titano.Data/Sqlite/SqliteDatabase.cs index f7dc78e9..f9b56736 100644 --- a/src/Titano.Data/Sqlite/SqliteDatabase.cs +++ b/src/Titano.Data/Sqlite/SqliteDatabase.cs @@ -37,6 +37,7 @@ public sealed class SqliteDatabase : IDatabase internal const int BusyTimeoutMs = 5000; private const string LogCategory = "Data.Sqlite"; private static readonly TimeSpan WriteLockTimeout = TimeSpan.FromSeconds(60); + private static readonly Lazy> ProbedCompileOptions = new(ProbeCompileOptions); private readonly string _filename; private readonly bool _memory; @@ -84,9 +85,24 @@ public sealed class SqliteDatabase : IDatabase } } + /// + /// The library's PRAGMA compile_options (BRIEF §6.2), probed and logged once per process; empty when the + /// probe fails. Informational only: Titano uses no optional feature (FTS, JSON), so nothing depends on it; it goes to + /// the log and the diagnostics bundle. + /// + public static IReadOnlyList CompileOptions + { + get + { + EnsureLibrary(); + return ProbedCompileOptions.Value; + } + } + internal static SqliteDatabase Open(SqliteSettings settings) { EnsureLibrary(); + _ = ProbedCompileOptions.Value; var database = new SqliteDatabase(settings); // The first connection validates the path, switches the file to WAL and keeps a memory database alive. database._idle.Push(database.CreateConnection()); @@ -259,6 +275,37 @@ public sealed class SqliteDatabase : IDatabase throw new NotSupportedException($"SQLite {version / 1_000_000}.{version / 1000 % 1000}.{version % 1000} is too old; 3.31 or newer is required."); } + private static IReadOnlyList ProbeCompileOptions() + { + var options = new List(); + try + { + using var connection = SqliteConnection.Open(":memory:", isMemory: true, BusyTimeoutMs); + var statement = connection.Rent("PRAGMA compile_options"); + try + { + var columns = statement.ReadColumns(); + int rc; + while ((rc = statement.Step()) == SQLITE_ROW) + { + if (statement.ReadRow(columns).GetStringOrNull(0) is { Length: > 0 } option) options.Add(option); + } + if (rc != SQLITE_DONE) throw connection.Error(rc); + } + finally + { + connection.Return(statement); + } + } + catch (SqliteException ex) + { + Log.Warning(LogCategory, "PRAGMA compile_options could not be read", ex); + return []; + } + Log.Info(LogCategory, $"SQLite {LibraryVersion}; compile options: {string.Join(' ', options)}"); + return options; + } + private static void Validate(string backupPath) { try diff --git a/tests/Titano.Data.Tests/Sqlite/SqliteBindingTests.cs b/tests/Titano.Data.Tests/Sqlite/SqliteBindingTests.cs index f2d2b576..4fe19f6e 100644 --- a/tests/Titano.Data.Tests/Sqlite/SqliteBindingTests.cs +++ b/tests/Titano.Data.Tests/Sqlite/SqliteBindingTests.cs @@ -15,6 +15,19 @@ public sealed class SqliteBindingTests(TestContext context) : DatabaseTest(conte Assert.True(parts[0] > 3 || (parts[0] == 3 && parts[1] >= 31), $"SQLite {SqliteDatabase.LibraryVersion}"); } + [Test] + public async Task CompileOptionsAreProbedOnceAndMatchTheLibrary() + { + await using var database = await OpenDatabaseAsync(); + var options = SqliteDatabase.CompileOptions; + // Informational and probed once: the same list on every call, and what any connection reports. + Assert.Same(options, SqliteDatabase.CompileOptions); + await using var session = await database.OpenAsync(Ct); + Assert.SequenceEqual(await session.QueryListAsync("PRAGMA compile_options", [], r => r.GetString(0), Ct), options); + // Linux libraries keep the diagnostics (a Windows build may omit them, which leaves the list empty). + if (!OperatingSystem.IsWindows()) Assert.Contains(options, o => o.StartsWith("THREADSAFE=", StringComparison.Ordinal)); + } + [Test] public async Task ConnectionsUseTheBriefPragmas() { From 4d321a84b0930128e89e211ab2dde859cedbb990 Mon Sep 17 00:00:00 2001 From: Claude Date: Mon, 12 Oct 2026 09:58:36 +0000 Subject: [PATCH 4/7] fix(imaging): record decode tiers per frame and keep undecodable frames out of renders MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit 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 Claude-Session: https://claude.ai/code/session_012iLMBNwBE2GnphAsu4cjaC --- docs/ARCHITECTURE.md | 4 +- i18n/en/Advisor.resx | 1 + i18n/en/Export.resx | 1 + i18n/en/Tooltips.resx | 2 + i18n/it/Advisor.it.resx | 1 + i18n/it/Export.it.resx | 1 + i18n/it/Tooltips.it.resx | 2 + src/Titano.App/DesignTime/SampleData.cs | 5 +- src/Titano.App/Editor/RemoteSequenceSource.cs | 1 + src/Titano.App/Library/RemoteCards.cs | 1 + src/Titano.App/Library/SequenceItem.cs | 9 +- src/Titano.Core/Advice/Advisor.cs | 7 +- src/Titano.Core/Advice/AdvisorTypes.cs | 17 +- .../Analysis/Deep/DeepAnalysisReport.cs | 1 + src/Titano.Core/Analysis/Finding.cs | 3 + .../Analysis/LightAnalysisReport.cs | 6 + src/Titano.Core/Analysis/LightAnalyzer.cs | 28 ++- src/Titano.Core/Analysis/LightFrame.cs | 4 + .../Archiving/FrameImageRenderer.cs | 35 +++- src/Titano.Core/Archiving/SequenceFrames.cs | 24 +++ src/Titano.Core/Archiving/SequenceManifest.cs | 14 ++ .../Decoders/ManagedDecoderProvider.cs | 82 ++++++-- src/Titano.Core/Imaging/Decoding.cs | 53 +++++ src/Titano.Core/Model/Entities.cs | 10 + src/Titano.Data/Archiving/ArchiveImages.cs | 56 ++++- src/Titano.Data/Archiving/FrameTiers.cs | 98 +++++++++ src/Titano.Data/Archiving/SequenceMapping.cs | 3 + .../Migrations/postgres/0005_decode_tiers.sql | 7 + .../Migrations/sqlite/0005_decode_tiers.sql | 7 + .../Processing/Jobs/RenderJobHandler.cs | 32 ++- .../Processing/ProcessingService.cs | 7 +- .../Processing/RenderNodeClient.cs | 2 + .../Processing/SequenceAnalyser.cs | 72 +++++-- .../Processing/SequenceFrameSource.cs | 84 +++++++- src/Titano.Data/Processing/SequenceRender.cs | 20 +- .../Repositories/FrameRepository.cs | 24 ++- .../Repositories/SequenceRepository.cs | 12 +- src/Titano.Protocol/Sequences.cs | 10 +- src/Titano.Server/Api/Dto.cs | 4 +- src/Titano.Ui/Controls/StatusGlyph.cs | 3 + src/Titano.Ui/Themes/Controls.xaml | 3 + .../Archiving/DecodeTierTests.cs | 198 ++++++++++++++++++ .../Decoding/ManagedDecoderProviderTests.cs | 72 +++++++ .../Archiving/ArchiveJobTests.cs | 54 +++++ .../Processing/AnalysisJobTests.cs | 72 +++++++ .../Processing/ProcessingFixture.cs | 7 + .../Processing/RenderJobTests.cs | 36 +++- .../Repositories/CatalogRepositoryTests.cs | 24 +++ 48 files changed, 1132 insertions(+), 87 deletions(-) create mode 100644 src/Titano.Data/Archiving/FrameTiers.cs create mode 100644 src/Titano.Data/Migrations/postgres/0005_decode_tiers.sql create mode 100644 src/Titano.Data/Migrations/sqlite/0005_decode_tiers.sql create mode 100644 tests/Titano.Core.Tests/Archiving/DecodeTierTests.cs diff --git a/docs/ARCHITECTURE.md b/docs/ARCHITECTURE.md index d216a96d..b22dacd6 100644 --- a/docs/ARCHITECTURE.md +++ b/docs/ARCHITECTURE.md @@ -29,7 +29,7 @@ Dependencies point one way: `Core` ← `Core.Windows`, `Data`, `Protocol` ← `S | Folder / namespace | Content | |---|---| -| `Imaging` (+ `Decoders`, `Geometry`, `Thumbnails`) | `ImageBuffer` (planar float, linear light), `IImageDecoderProvider`, `DecoderFactory` (decode tiers: native → embedded preview), `ManagedDecoderProvider`, `FrameWindow` (read-ahead, memory budget, float32 spill), resampler, thumbnail cache | +| `Imaging` (+ `Decoders`, `Geometry`, `Thumbnails`) | `ImageBuffer` (planar float, linear light), `IImageDecoderProvider`, `DecoderFactory` (decode tiers: native → embedded preview; `ImageDecoder.Probe` names a file's tier without decoding it), `ManagedDecoderProvider`, `FrameWindow` (read-ahead, memory budget, float32 spill), resampler, thumbnail cache | | `Metadata` (+ `Tiff`, `Jpeg`, `Bmff`, `Raf`, `Riff`, `Png`, `Xmp`, `MakerNotes`) | Signature sniffing, Exif/GPS/XMP/MakerNotes, ISO-BMFF (HEIF, CR3), `MetadataReader`, `MetadataBuilder` (all fallback chains), `ExifWriter` | | `Codecs` (`Jpeg`, `Png`, `Tiff`, `Dng`, `Raw`, `Gif`, `Apng`, `Ico`) | Pure managed codecs; native RAW development (DNG, CR2, NEF, ARW, PEF, RAF) | | `Analysis` (+ `Deep`) | Luminance statistics, deflicker, regions, holy grail, white balance, colour temperature, light analysis, streaming deep analysis with checkpoints | @@ -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`, `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), `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`) | diff --git a/i18n/en/Advisor.resx b/i18n/en/Advisor.resx index 7c3ab191..c76505d4 100644 --- a/i18n/en/Advisor.resx +++ b/i18n/en/Advisor.resx @@ -22,6 +22,7 @@ Only {0} of {1} frames have a capture time, so cadence and gaps cannot be measured.{0} frames with a capture time; {1} frames in total {0} frames are out of time order.{0} frame count The view drifts by {0:0.##} px per frame (pan or slider).{0} drift in source pixels per frame + Frames decoding only from their embedded preview: {0}.{0} frame count The view jumps at {0} frames (camera moved or bumped).{0} frame count Shot in {0} separate sessions.{0} session count The camera shakes by {0:0.##} px RMS; hiding it costs {1:0.#}% of the frame.{0} shake RMS in source pixels; {1} crop cost in percent of the area diff --git a/i18n/en/Export.resx b/i18n/en/Export.resx index bd544a1a..28116072 100644 --- a/i18n/en/Export.resx +++ b/i18n/en/Export.resx @@ -34,6 +34,7 @@ {0} failed: {1}long: job log line; {0} preset name; {1} reason No render node can encode {1}; {0} is rendered as {2} instead.long: job log line; {0} preset name; {1} codec asked for; {2} managed codec used {0} saved as {1}long: job log line; {0} preset name; {1} output file name + Undecodable frames shown as a neighbour: {0}.long: job log line; {0} frame numbers from 1, comma-separated Playback rate not understood; the export frame rate is used.long No capture times: frames are spaced evenly.long Every frame is excluded.long diff --git a/i18n/en/Tooltips.resx b/i18n/en/Tooltips.resx index 915ab73b..09afe065 100644 --- a/i18n/en/Tooltips.resx +++ b/i18n/en/Tooltips.resx @@ -399,6 +399,8 @@ This PC has originals or settings the server does not have yet No proxies yet; editing reads the originals Not analysed yet + {0:N0} frame decodes only from its embedded preview; renders are capped at its size + {0:N0} frames decode only from their embedded previews; renders are capped at their size JPEG proxies built for fast editing Lives on the server; this PC has proxies at most Rendered once diff --git a/i18n/it/Advisor.it.resx b/i18n/it/Advisor.it.resx index c50b0774..c199c431 100644 --- a/i18n/it/Advisor.it.resx +++ b/i18n/it/Advisor.it.resx @@ -22,6 +22,7 @@ Solo {0} fotogrammi su {1} hanno un orario di scatto: cadenza e pause non sono misurabili. {0} fotogrammi sono fuori ordine temporale. L'inquadratura scorre di {0:0.##} px per fotogramma (panoramica o slider). + Fotogrammi decodificati solo dall'anteprima incorporata: {0}. L'inquadratura salta in {0} fotogrammi (fotocamera spostata o urtata). Ripreso in {0} sessioni separate. La fotocamera vibra di {0:0.##} px RMS; compensarla costa il {1:0.#}% dell'inquadratura. diff --git a/i18n/it/Export.it.resx b/i18n/it/Export.it.resx index 03ca3056..6f9dea5f 100644 --- a/i18n/it/Export.it.resx +++ b/i18n/it/Export.it.resx @@ -20,6 +20,7 @@ {0} non riuscito: {1} Nessun nodo di rendering può codificare {1}; {0} viene esportato in {2}. {0} salvato come {1} + Fotogrammi non decodificabili sostituiti da un vicino: {0}. Velocità di riproduzione non valida; viene usata la frequenza di esportazione. Nessun orario di scatto: fotogrammi distribuiti in modo uniforme. Tutti i fotogrammi sono esclusi. diff --git a/i18n/it/Tooltips.it.resx b/i18n/it/Tooltips.it.resx index 634fa3ca..6af78d01 100644 --- a/i18n/it/Tooltips.it.resx +++ b/i18n/it/Tooltips.it.resx @@ -399,6 +399,8 @@ Questo PC ha originali o impostazioni che il server non ha ancora Ancora nessun proxy; la modifica legge gli originali Non ancora analizzata + {0:N0} fotogramma si decodifica solo dall'anteprima incorporata; i render sono limitati alla sua dimensione + {0:N0} fotogrammi si decodificano solo dalle anteprime incorporate; i render sono limitati alla loro dimensione Proxy JPEG pronti per una modifica veloce Si trova sul server; questo PC ne ha al massimo i proxy Esportata una volta diff --git a/src/Titano.App/DesignTime/SampleData.cs b/src/Titano.App/DesignTime/SampleData.cs index 828dcccd..eaea7d75 100644 --- a/src/Titano.App/DesignTime/SampleData.cs +++ b/src/Titano.App/DesignTime/SampleData.cs @@ -456,7 +456,7 @@ public sealed class SampleData var now = new DateTime(2026, 9, 28, 18, 2, 11); (Sequence, string[], bool) Make(string? title, string folder, string make, string model, int frames, TimeSpan span, double interval, int daysAgo, SequenceStatus status, string[] colours, int renders = 0, int warnings = 0, SequenceOrigin origin = SequenceOrigin.Local, - SyncState sync = SyncState.LocalOnly, bool raw = false) + SyncState sync = SyncState.LocalOnly, bool raw = false, int previewOnly = 0) { var start = now.AddDays(-daysAgo); return (new Sequence @@ -475,6 +475,7 @@ public sealed class SampleData Status = status, RenderedCount = renders, WarningCount = warnings, + PreviewOnlyCount = previewOnly, Origin = origin, SyncState = sync, UpdatedAt = DateTimeOffset.UtcNow, @@ -490,7 +491,7 @@ public sealed class SampleData yield return Make(null, "2026/2026-09-27 Sony ILCE-7M4", "SONY", "ILCE-7M4", 2_140, new TimeSpan(1, 11, 0), 2, 1, Analysed, ["#4A7FB5", "#A9C9E6", "#6F7D6A"]); yield return Make("Aurora, Tromsø", "2026/2026-02-20 Nikon Z 8", "NIKON CORPORATION", "NIKON Z 8", 1_860, new TimeSpan(4, 8, 0), 8, 220, Full, - ["#0C2A3A", "#1AA08C", "#0B1A26"], sync: SyncState.InSync); + ["#0C2A3A", "#1AA08C", "#0B1A26"], sync: SyncState.InSync, previewOnly: 1_860); yield return Make("Construction cam · week 38", "2026/2026-09-14 Brinno TLC2020", "Brinno", "TLC2020", 8_920, TimeSpan.FromDays(7), 60, 14, SequenceStatus.None, ["#7C8AA0", "#C9CFD6", "#4C5058"], origin: SequenceOrigin.Server, sync: SyncState.RemoteOnly); yield return Make("Milan skyline dusk", "2026/2026-06-03 Apple iPhone 16 Pro", "Apple", "iPhone 16 Pro", 2_400, new TimeSpan(1, 20, 0), 2, 117, Full, diff --git a/src/Titano.App/Editor/RemoteSequenceSource.cs b/src/Titano.App/Editor/RemoteSequenceSource.cs index c245b805..43385418 100644 --- a/src/Titano.App/Editor/RemoteSequenceSource.cs +++ b/src/Titano.App/Editor/RemoteSequenceSource.cs @@ -103,6 +103,7 @@ public sealed class RemoteSequenceSource : ISequenceSource RenderedCount = dto.RenderedCount, HeroFrameId = dto.HeroFrameId, WarningCount = dto.WarningCount, + PreviewOnlyCount = dto.PreviewOnlyCount, SettingsJson = manifest.SettingsJson, SettingsVersion = manifest.SettingsVersion, UpdatedAt = dto.UpdatedAt, diff --git a/src/Titano.App/Library/RemoteCards.cs b/src/Titano.App/Library/RemoteCards.cs index f99e4aa8..3ecc5ca7 100644 --- a/src/Titano.App/Library/RemoteCards.cs +++ b/src/Titano.App/Library/RemoteCards.cs @@ -43,6 +43,7 @@ public static class RemoteCards SettingsVersion = dto.SettingsVersion, FramesVersion = dto.FramesVersion, WarningCount = dto.WarningCount, + PreviewOnlyCount = dto.PreviewOnlyCount, OwnerUserId = dto.OwnerUserId, UpdatedAt = dto.UpdatedAt, Origin = SequenceOrigin.Server, diff --git a/src/Titano.App/Library/SequenceItem.cs b/src/Titano.App/Library/SequenceItem.cs index 3256faff..40d2d6a7 100644 --- a/src/Titano.App/Library/SequenceItem.cs +++ b/src/Titano.App/Library/SequenceItem.cs @@ -237,10 +237,13 @@ public sealed class SequenceItem : ObservableObject _ => null, }; - /// Card status row: analysed, proxies, renders, errors or warnings, sync (Main.html); the sync glyph is last so a narrow card cuts it first. + /// + /// Card status row: analysed, proxies, renders, preview-only frames (BRIEF §8.2), errors or warnings, sync (Main.html); + /// the sync glyph is last so a narrow card cuts it first. + /// public static IReadOnlyList BuildStatuses(Sequence s, bool analysing) { - var list = new List(5); + var list = new List(6); if (analysing) list.Add(new CardStatus(StatusKind.Analysing, Loc.L("Library.Status.Analysing"), Loc.L("Tooltips.Library.Status.Analysing"))); else if ((s.Status & SequenceStatus.AnalyzedLight) != 0) list.Add(new CardStatus(StatusKind.Analysed, Loc.L("Library.Status.Analysed"), Loc.L("Tooltips.Library.Status.Analysed"))); else list.Add(new CardStatus(StatusKind.NotAnalysed, Loc.L("Library.Status.Analysed"), Loc.L("Tooltips.Library.Status.NotAnalysed"))); @@ -258,6 +261,8 @@ public sealed class SequenceItem : ObservableObject if (s.RenderedCount > 0) list.Add(new CardStatus(StatusKind.Rendered, Formatting.Count(s.RenderedCount), Loc.Plural("Tooltips.Library.Status.Rendered", s.RenderedCount))); + if (s.PreviewOnlyCount > 0) + list.Add(new CardStatus(StatusKind.PreviewOnly, Formatting.Count(s.PreviewOnlyCount), Loc.Plural("Tooltips.Library.Status.PreviewOnly", s.PreviewOnlyCount))); if ((s.Status & SequenceStatus.HasErrors) != 0) list.Add(new CardStatus(StatusKind.Error, Formatting.Count(Math.Max(1, s.WarningCount)), Loc.L("Tooltips.Library.Status.Errors"))); else if (s.WarningCount > 0 || (s.Status & SequenceStatus.HasWarnings) != 0) diff --git a/src/Titano.Core/Advice/Advisor.cs b/src/Titano.Core/Advice/Advisor.cs index e1e5e38d..32217eb1 100644 --- a/src/Titano.Core/Advice/Advisor.cs +++ b/src/Titano.Core/Advice/Advisor.cs @@ -16,14 +16,17 @@ public static class Advisor { /// /// Recommendations for every parameter the rules cover (see ), in section order, plus - /// every finding of both analyses. Without a deep report, pixel-based parameters get missing-data recommendations. + /// every finding of both analyses; a finding both report (frames that cannot be decoded: the light analysis reads + /// the flags the deep pass recorded) is listed once, as the light analysis has it. Without a deep report, + /// pixel-based parameters get missing-data recommendations. /// public static AdvisorResult Recommend(LightAnalysisReport light, DeepAnalysisReport? deep, SequenceContext context) { ArgumentNullException.ThrowIfNull(light); ArgumentNullException.ThrowIfNull(context); var findings = new List(light.Findings); - if (deep is not null) findings.AddRange(deep.Findings.Select(f => f.ToFinding())); + var lightKeys = light.Findings.Select(f => f.Key).ToHashSet(StringComparer.Ordinal); + if (deep is not null) findings.AddRange(deep.Findings.Select(f => f.ToFinding()).Where(f => !lightKeys.Contains(f.Key))); return new AdvisorResult(new AdvisorRules(light, deep, context).Run(), findings); } diff --git a/src/Titano.Core/Advice/AdvisorTypes.cs b/src/Titano.Core/Advice/AdvisorTypes.cs index 59c07f63..e8b3a6d0 100644 --- a/src/Titano.Core/Advice/AdvisorTypes.cs +++ b/src/Titano.Core/Advice/AdvisorTypes.cs @@ -143,27 +143,38 @@ public sealed record SequenceContext /// Wanted video length ("fit to N seconds"), when the user set one. public double? TargetDurationSeconds { get; init; } - /// Context from the archive's frame rows: count, dominant oriented size, preview-only count, median shutter time. + /// + /// Context from the archive's frame rows: count, dominant oriented size, preview-only count with the dominant oriented + /// size of those frames' previews, median shutter time. + /// public static SequenceContext FromFrames(IReadOnlyList frames, IReadOnlyList? codecs = null) { ArgumentNullException.ThrowIfNull(frames); var sizes = new Dictionary<(int, int), int>(); + var previews = new Dictionary<(int, int), int>(); var exposures = new List(); var previewOnly = 0; foreach (var f in frames) { - var size = f.Orientation is >= 5 and <= 8 ? (f.Height, f.Width) : (f.Width, f.Height); + var rotated = f.Orientation is >= 5 and <= 8; + var size = rotated ? (f.Height, f.Width) : (f.Width, f.Height); if (size.Item1 > 0 && size.Item2 > 0) sizes[size] = sizes.GetValueOrDefault(size) + 1; if (f.ExposureTime is > 0 and var t) exposures.Add(t); - if ((f.Flags & FrameFlags.PreviewOnly) != 0) previewOnly++; + if ((f.Flags & FrameFlags.PreviewOnly) == 0) continue; + previewOnly++; + var preview = rotated ? (f.PreviewHeight, f.PreviewWidth) : (f.PreviewWidth, f.PreviewHeight); + if (preview.Item1 > 0 && preview.Item2 > 0) previews[preview] = previews.GetValueOrDefault(preview) + 1; } var (width, height) = sizes.Count > 0 ? sizes.MaxBy(kv => kv.Value).Key : (0, 0); + var (previewWidth, previewHeight) = previews.Count > 0 ? previews.MaxBy(kv => kv.Value).Key : (0, 0); return new SequenceContext { FrameCount = frames.Count, SourceWidth = width, SourceHeight = height, PreviewOnlyCount = previewOnly, + PreviewWidth = previewWidth, + PreviewHeight = previewHeight, MedianExposureSeconds = exposures.Count > 0 ? Statistics.Median(exposures.ToArray()) : null, Codecs = codecs ?? [], }; diff --git a/src/Titano.Core/Analysis/Deep/DeepAnalysisReport.cs b/src/Titano.Core/Analysis/Deep/DeepAnalysisReport.cs index 34268978..89866d9e 100644 --- a/src/Titano.Core/Analysis/Deep/DeepAnalysisReport.cs +++ b/src/Titano.Core/Analysis/Deep/DeepAnalysisReport.cs @@ -114,6 +114,7 @@ public sealed record DeepFinding(string Key, FindingSeverity Severity, double[] /// Resource keys of the findings produced by the deep analysis; arguments are numbers. public static class DeepFindingKeys { + /// Frames nothing decodes; the light analysis reports the frames flagged so under it too (format tier 3). public const string UnreadableFrames = "Advisor.Finding.UnreadableFrames"; public const string BlackFrames = "Advisor.Finding.BlackFrames"; public const string WhiteFrames = "Advisor.Finding.WhiteFrames"; diff --git a/src/Titano.Core/Analysis/Finding.cs b/src/Titano.Core/Analysis/Finding.cs index 217208d6..00b4d8e2 100644 --- a/src/Titano.Core/Analysis/Finding.cs +++ b/src/Titano.Core/Analysis/Finding.cs @@ -33,6 +33,9 @@ public static class FindingKeys public const string LensChanged = "Advisor.Finding.LensChanged"; public const string MixedResolution = "Advisor.Finding.MixedResolution"; public const string MixedOrientation = "Advisor.Finding.MixedOrientation"; + + /// Frames that decode only through their embedded preview (format tier 2); {0} frame count. + public const string PreviewOnly = "Advisor.Finding.PreviewOnly"; public const string TimeZoneEstimated = "Advisor.Finding.TimeZoneEstimated"; public const string DayNightTransition = "Advisor.Finding.DayNightTransition"; } diff --git a/src/Titano.Core/Analysis/LightAnalysisReport.cs b/src/Titano.Core/Analysis/LightAnalysisReport.cs index 501ca433..9e7fe647 100644 --- a/src/Titano.Core/Analysis/LightAnalysisReport.cs +++ b/src/Titano.Core/Analysis/LightAnalysisReport.cs @@ -131,5 +131,11 @@ public sealed record LightAnalysisReport /// Light condition per frame from GPS position and capture time; all null without GPS. public DayPhase?[] DayPhases { get; init; } = []; + /// Frames that decode only through their embedded preview (format tier 2, BRIEF §8.2). + public IReadOnlyList PreviewOnlyFrames { get; init; } = []; + + /// Frames that nothing decodes (format tier 3): renders skip them. + public IReadOnlyList UndecodableFrames { get; init; } = []; + public IReadOnlyList Findings { get; init; } = []; } diff --git a/src/Titano.Core/Analysis/LightAnalyzer.cs b/src/Titano.Core/Analysis/LightAnalyzer.cs index 0fc5dc9b..aa1f0c1a 100644 --- a/src/Titano.Core/Analysis/LightAnalyzer.cs +++ b/src/Titano.Core/Analysis/LightAnalyzer.cs @@ -1,3 +1,4 @@ +using Titano.Core.Analysis.Deep; using Titano.Core.Mathematics; using Titano.Core.Model; @@ -5,8 +6,9 @@ namespace Titano.Core.Analysis; /// /// Metadata-only sequence analysis run at import/scan (BRIEF §10.1): cadence, gaps and sessions, timestamp -/// sanity, exposure series and steps, camera/lens identity, size and orientation consistency, and day phases -/// from GPS. Linear in the number of frames apart from two medians; deterministic. +/// sanity, exposure series and steps, camera/lens identity, size and orientation consistency, format tiers (from the +/// frames' decode-tier flags) and day phases from GPS. Linear in the number of frames apart from two medians; +/// deterministic. /// public static class LightAnalyzer { @@ -31,6 +33,7 @@ public static class LightAnalyzer var exposure = AnalyzeExposure(frames, options, findings); var identity = AnalyzeIdentity(frames, findings); var (dominantSize, sizeOutliers, orientation, orientationOutliers) = AnalyzeGeometry(frames, findings); + var (previewOnly, undecodable) = AnalyzeTiers(frames, findings); var phases = AnalyzeDayPhases(frames, timing.Adjusted, findings); return new LightAnalysisReport @@ -56,6 +59,8 @@ public static class LightAnalyzer DominantOrientation = orientation, OrientationOutliers = orientationOutliers, DayPhases = phases, + PreviewOnlyFrames = previewOnly, + UndecodableFrames = undecodable, Findings = findings, }; } @@ -331,6 +336,25 @@ public static class LightAnalyzer return (dominant, sizeOutliers, orientation, orientationOutliers); } + /// + /// Format tiers (BRIEF §8.2): frames that decode only through their embedded preview, which caps the output at the + /// preview size, and frames that nothing decodes, which renders skip (a warning, under the key the deep analysis uses + /// for the frames it could not decode, so the advisor lists them once). + /// + private static (List PreviewOnly, List Undecodable) AnalyzeTiers(IReadOnlyList frames, List findings) + { + var previewOnly = new List(); + var undecodable = new List(); + for (var i = 0; i < frames.Count; i++) + { + if ((frames[i].Flags & FrameFlags.Undecodable) != 0) undecodable.Add(i); + else if ((frames[i].Flags & FrameFlags.PreviewOnly) != 0) previewOnly.Add(i); + } + if (previewOnly.Count > 0) findings.Add(new Finding(FindingKeys.PreviewOnly, FindingSeverity.Info, [previewOnly.Count], Cap(previewOnly))); + if (undecodable.Count > 0) findings.Add(new Finding(DeepFindingKeys.UnreadableFrames, FindingSeverity.Warning, [undecodable.Count], Cap(undecodable))); + return (previewOnly, undecodable); + } + private static DayPhase?[] AnalyzeDayPhases(IReadOnlyList frames, DateTime?[] adjusted, List findings) { var n = frames.Count; diff --git a/src/Titano.Core/Analysis/LightFrame.cs b/src/Titano.Core/Analysis/LightFrame.cs index 40529c0f..7a860e09 100644 --- a/src/Titano.Core/Analysis/LightFrame.cs +++ b/src/Titano.Core/Analysis/LightFrame.cs @@ -31,6 +31,9 @@ public sealed record LightFrame /// GPS (satellite) UTC time stamp, used to infer the camera clock's UTC offset. public DateTime? GpsUtc { get; init; } + /// The archived frame's flags; the decode-tier bits feed the format-tier findings. + public FrameFlags Flags { get; init; } + /// Exposure value at ISO 100: log2(N²/t) − log2(ISO/100). public double? ExposureValue => ExposureTime is > 0 && FNumber is > 0 ? Math.Log2(FNumber.Value * FNumber.Value / ExposureTime.Value) - Math.Log2((Iso is > 0 ? Iso.Value : 100) / 100.0) @@ -54,6 +57,7 @@ public sealed record LightFrame Width = frame.Width, Height = frame.Height, Orientation = frame.Orientation, + Flags = frame.Flags, }; } diff --git a/src/Titano.Core/Archiving/FrameImageRenderer.cs b/src/Titano.Core/Archiving/FrameImageRenderer.cs index 0cdbfff3..5e37bc61 100644 --- a/src/Titano.Core/Archiving/FrameImageRenderer.cs +++ b/src/Titano.Core/Archiving/FrameImageRenderer.cs @@ -8,8 +8,9 @@ namespace Titano.Core.Archiving; public sealed record EncodedImage(byte[] Jpeg, int Width, int Height, DecodeTier Tier); /// -/// Makes thumbnails and proxies of frames (BRIEF §8.5): the embedded preview or the managed JPEG decoder first, then -/// the host's decode providers (WIC on Windows); orientation applied, resampled in linear light, encoded in-house. +/// Makes thumbnails and proxies of frames (BRIEF §8.5): the managed JPEG decoder, or for thumbnails an embedded preview +/// at least 320 px long, first, then the host's decode providers (WIC on Windows); orientation applied, resampled in +/// linear light, encoded in-house. says how the pixels were obtained. /// public sealed class FrameImageRenderer(ImageDecoder decoder) { @@ -55,13 +56,37 @@ public sealed class FrameImageRenderer(ImageDecoder decoder) private static EncodedImage Encode(ImageBuffer pixels, JpegEncodeOptions options, DecodeTier tier) => new(JpegEncoder.Encode(pixels, options), pixels.Width, pixels.Height, tier); + /// + /// The frame's pixels: a JPEG through the managed decoder, the stored embedded preview when + /// and it reaches (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. + /// private DecodedFrame? Decode(string path, SidecarFrame frame, int maxLongEdge, DecodePurpose purpose, bool preferPreview, CancellationToken cancellationToken) { - if (frame.Format == ImageFileFormat.Jpeg) return DecodeJpeg(File.ReadAllBytes(path), maxLongEdge, frame.Orientation, DecodeTier.Native); - if (preferPreview && ReadPreview(path, frame) is { } preview) return DecodeJpeg(preview, maxLongEdge, frame.Orientation, DecodeTier.EmbeddedPreview); + 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) + && 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); if (decoder.Decode(request, cancellationToken) is { } decoded) return new DecodedFrame(decoded.Pixels, decoded.Tier); - return ReadPreview(path, frame) is { } fallback ? DecodeJpeg(fallback, maxLongEdge, frame.Orientation, DecodeTier.EmbeddedPreview) : null; + return (preview ?? ReadPreview(path, frame)) is { } fallback ? DecodeJpeg(fallback, maxLongEdge, frame.Orientation, DecodeTier.EmbeddedPreview) : null; + } + + /// Whether the stored preview's long edge reaches (the sidecar's size, else the JPEG's own). + private static bool Reaches(byte[] preview, SidecarFrame frame, 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); + return int.Max(info.Width, info.Height) >= maxLongEdge; + } + catch (InvalidDataException) + { + return false; + } } private static DecodedFrame? DecodeJpeg(byte[] data, int maxLongEdge, int orientation, DecodeTier tier) diff --git a/src/Titano.Core/Archiving/SequenceFrames.cs b/src/Titano.Core/Archiving/SequenceFrames.cs index 4735e3ae..8f79a8de 100644 --- a/src/Titano.Core/Archiving/SequenceFrames.cs +++ b/src/Titano.Core/Archiving/SequenceFrames.cs @@ -1,3 +1,4 @@ +using Titano.Core.Imaging; using Titano.Core.Metadata; using Titano.Core.Model; @@ -42,9 +43,32 @@ public static class SequenceFrames Format = metadata.Format, PreviewOffset = metadata.LargestPreview?.Offset, PreviewLength = metadata.LargestPreview?.Length, + PreviewW = Positive(metadata.LargestPreview?.Width), + PreviewH = Positive(metadata.LargestPreview?.Height), }; } + /// + /// The frame with its decode tier (BRIEF §8.2) and the flags that follow it: for + /// the embedded-preview tier, for none. 's size, oriented + /// like every , replaces the stored preview size when it is a preview's. + /// + /// For the unavailable tier: whether that proves the file unreadable. False when the host has no + /// native decoder for the format, so a client or render node with one (or a proxy) may still produce its pixels: the + /// tier is kept and is left as it was. + public static SidecarFrame WithTier(SidecarFrame frame, FileTier tier, bool damaged = true) + { + var flags = frame.Flags & ~(FrameFlags.PreviewOnly | FrameFlags.Undecodable); + if (tier.Tier == DecodeTier.EmbeddedPreview) flags |= FrameFlags.PreviewOnly; + else if (tier.Tier == DecodeTier.Unavailable) flags |= damaged ? FrameFlags.Undecodable : frame.Flags & FrameFlags.Undecodable; + var (w, h) = tier.Tier == DecodeTier.EmbeddedPreview && tier.Width > 0 && tier.Height > 0 + ? PixelConvert.OrientedSize(tier.Width, tier.Height, frame.Orientation) + : (frame.PreviewW ?? 0, frame.PreviewH ?? 0); + return frame with { Tier = tier.Tier, Flags = flags, PreviewW = Positive(w), PreviewH = Positive(h) }; + } + + private static int? Positive(int? value) => value > 0 ? value : null; + /// /// Merges into the frame list by capture time (existing frames keep their relative order), /// renumbers indexes and bumps the frames version. diff --git a/src/Titano.Core/Archiving/SequenceManifest.cs b/src/Titano.Core/Archiving/SequenceManifest.cs index dbab9390..7d7c7b6f 100644 --- a/src/Titano.Core/Archiving/SequenceManifest.cs +++ b/src/Titano.Core/Archiving/SequenceManifest.cs @@ -109,6 +109,20 @@ public sealed record SidecarFrame public long? PreviewOffset { get; init; } public long? PreviewLength { get; init; } + /// Size of the largest embedded preview (or GPR companion JPEG), stored like and + /// before orientation; null when the file has none or it is not known yet. + public int? PreviewW { get; init; } + + public int? PreviewH { get; init; } + + /// + /// Decode tier of the file (BRIEF §8.2) as the host that indexed or decoded it last found it; omitted until it is + /// known. and follow it. + /// + [JsonConverter(typeof(TolerantEnumConverter))] + [JsonIgnore(Condition = JsonIgnoreCondition.WhenWritingDefault)] + public DecodeTier Tier { get; init; } + /// bits, stored as a number so bits of newer builds survive. public FrameFlags Flags { get; init; } diff --git a/src/Titano.Core/Imaging/Decoders/ManagedDecoderProvider.cs b/src/Titano.Core/Imaging/Decoders/ManagedDecoderProvider.cs index 9b8c7e2f..4fa99fea 100644 --- a/src/Titano.Core/Imaging/Decoders/ManagedDecoderProvider.cs +++ b/src/Titano.Core/Imaging/Decoders/ManagedDecoderProvider.cs @@ -7,19 +7,11 @@ using Titano.Core.Metadata; namespace Titano.Core.Imaging.Decoders; -/// Tier a provider would use for one specific file, with the reason when it is not native. -/// Format detected from the file's signature (extension only breaks ties). -/// Best tier available for this file. -/// Decoder that would produce it ("managed-cr2", "managed-preview"…), null when unavailable. -/// Why the native tier is not used (unsupported body or encoding), else null. -/// Full-size width that tier delivers (developed RAW, preview or image size), oriented; 0 when unknown. -/// Full-size height, oriented; 0 when unknown. -public sealed record FileTier(ImageFileFormat Format, DecodeTier Tier, string? Decoder, string? Reason, int Width, int Height); - /// /// 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 ; every other -/// RAW, CR3 and HEIF through the largest suitable embedded JPEG preview; nothing for formats without either. +/// 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. /// fast paths: JPEG DCT scaling and RAW CFA binning when /// allows, bilinear demosaicing for thumbnails and analysis, and to skip native /// RAW development. Results fit within MaxLongEdge (never upscaled) and are always RGB. @@ -29,6 +21,7 @@ public sealed class ManagedDecoderProvider : IImageDecoderProvider private const string LogCategory = "Decode"; private const int FileBufferSize = 1 << 16; private const long MaxPreviewBytes = 256L << 20; + private static readonly string[] CompanionExtensions = [".JPG", ".jpg", ".JPEG", ".jpeg"]; public string Name => "managed"; @@ -62,6 +55,18 @@ public sealed class ManagedDecoderProvider : IImageDecoderProvider return result; } + /// + /// 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. + /// + public FileTier Probe(string path, ImageFileFormat format) + { + var family = ImageFormatInfo.Of(format).Family; + if (family is not (ImageFamily.Jpeg or ImageFamily.Png or ImageFamily.Tiff)) return Probe(path); + var decoder = family switch { ImageFamily.Jpeg => "managed-jpeg", ImageFamily.Png => "managed-png", _ => "managed-tiff" }; + return new FileTier(format, DecodeTier.Native, decoder, null, 0, 0); + } + /// The tier this provider would use for the file at , without decoding pixels. public FileTier Probe(string path) { @@ -88,11 +93,32 @@ public sealed class ManagedDecoderProvider : IImageDecoderProvider reason = $"no managed decoder for {format}"; } var metadata = MetadataReader.Read(stream, path); - if (metadata.LargestPreview is not { } preview) return new FileTier(format, DecodeTier.Unavailable, null, reason + "; no embedded preview", 0, 0); - var (pw, ph) = PixelConvert.OrientedSize(preview.Width, preview.Height, metadata.Orientation); + 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 (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); } + /// + /// The JPEG a GoPro writes beside a GPR (same name, .JPG), or null. Only the usual spellings are tried, so a + /// folder of thousands of frames is never listed once per frame. + /// + internal static string? CompanionJpeg(string path) + { + var stem = Path.Combine(Path.GetDirectoryName(path) ?? string.Empty, Path.GetFileNameWithoutExtension(path)); + foreach (var extension in CompanionExtensions) + { + if (File.Exists(stem + extension)) return stem + extension; + } + return null; + } + private static FileStream Open(string path) => new(path, FileMode.Open, FileAccess.Read, FileShare.ReadWrite | FileShare.Delete, FileBufferSize, FileOptions.RandomAccess); @@ -147,7 +173,37 @@ public sealed class ManagedDecoderProvider : IImageDecoderProvider Log.Debug(LogCategory, $"{Path.GetFileName(request.Path)}: native decode unavailable ({ex.Message}); trying the embedded preview"); } } - return request.AllowPreview || request.ForcePreview ? Preview(stream, request, adequateOnly: false, cancellationToken) : null; + if (!request.AllowPreview && !request.ForcePreview) return null; + if (format == ImageFileFormat.Gpr && Companion(stream, request, cancellationToken) is { } companion) return companion; + return Preview(stream, request, adequateOnly: false, cancellationToken); + } + + /// + /// 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. + /// + private static DecodedImage? Companion(Stream raw, DecodeRequest request, CancellationToken cancellationToken) + { + if (CompanionJpeg(request.Path) 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); + if (stream.Length > MaxPreviewBytes) return null; + var data = ReadAll(stream); + JpegInfo info; + try + { + info = JpegDecoder.ReadInfo(data); + } + catch (InvalidDataException ex) + { + Log.Debug(LogCategory, $"{Path.GetFileName(path)}: not a usable JPEG ({ex.Message}); using the embedded preview"); + return null; + } + if (int.Max(info.Width, info.Height) <= largestPreview) return null; + var image = JpegDecoder.DecodeToLinear(data, new JpegDecodeOptions { MaxLongEdge = request.MaxLongEdge }, expandGray: true); + var orientation = request.ApplyOrientation ? MetadataReader.Read(stream, path, cancellationToken: cancellationToken).Orientation : 1; + return Finish(image, orientation, request, info.Width, info.Height, DecodeTier.EmbeddedPreview, "managed-sidecar"); } /// Embedded-preview tier: the smallest preview whose long edge still reaches MaxLongEdge (the largest when none diff --git a/src/Titano.Core/Imaging/Decoding.cs b/src/Titano.Core/Imaging/Decoding.cs index 98f5f519..06a4c2d6 100644 --- a/src/Titano.Core/Imaging/Decoding.cs +++ b/src/Titano.Core/Imaging/Decoding.cs @@ -53,6 +53,15 @@ public sealed class DecodedImage(ImageBuffer pixels, DecodeTier tier, string dec public void Dispose() => Pixels.Dispose(); } +/// Tier a provider would use for one specific file, with the reason when it is not native. +/// Format of the file (detected from its signature; the extension only breaks ties). +/// Best tier available for this file. +/// Decoder that would produce it ("managed-cr2", "managed-preview"…), null when unavailable. +/// Why the native tier is not used (unsupported body or encoding), else null. +/// Full-size width that tier delivers (developed RAW, preview or image size), oriented; 0 when unknown. +/// Full-size height, oriented; 0 when unknown. +public sealed record FileTier(ImageFileFormat Format, DecodeTier Tier, string? Decoder, string? Reason, int Width, int Height); + /// A platform image decoder (WIC on Windows, managed codecs everywhere). public interface IImageDecoderProvider { @@ -64,6 +73,17 @@ public interface IImageDecoderProvider bool CanDecode(ImageFormatInfo format) => TierFor(format.Format) is not null; + /// + /// The tier this provider would use for the file at , whose signature says + /// , without decoding pixels. The default trusts ; a provider whose + /// support depends on the file itself (camera body, encoding, embedded previews) looks inside it. + /// + /// The file cannot be read. + /// The file is malformed where the provider looks. + FileTier Probe(string path, ImageFileFormat format) => TierFor(format) is { } tier + ? new FileTier(format, tier, Name, null, 0, 0) + : new FileTier(format, DecodeTier.Unavailable, null, $"{Name}: no decoder for {format}", 0, 0); + /// Decodes or returns null when this provider cannot handle the specific file. DecodedImage? Decode(DecodeRequest request, CancellationToken cancellationToken = default); } @@ -84,6 +104,39 @@ public sealed class ImageDecoder(IReadOnlyList providers) return best; } + /// + /// The tier this chain would use for the file at (signature format ) + /// without decoding pixels (BRIEF §8.2): the first provider whose probe reaches the native tier, else the largest + /// embedded preview a provider offers, else unavailable with the first reason given. A provider that finds the file + /// malformed is skipped like in . + /// + /// The file cannot be read. + public FileTier Probe(string path, ImageFileFormat format) + { + FileTier? preview = null; + string? reason = null; + foreach (var provider in Providers.OrderBy(p => p.TierFor(format) ?? DecodeTier.Unavailable)) + { + if (provider.TierFor(format) is null) continue; + FileTier tier; + try + { + tier = provider.Probe(path, format); + } + catch (Exception ex) when (ex is InvalidDataException or NotSupportedException or FormatException or InvalidOperationException + or System.Runtime.InteropServices.COMException) + { + reason ??= $"{provider.Name}: {ex.Message}"; + continue; + } + if (tier.Tier == DecodeTier.Native) return tier; + if (tier.Tier == DecodeTier.EmbeddedPreview && (preview is null || long.Max(tier.Width, tier.Height) > long.Max(preview.Width, preview.Height))) + preview = tier; + reason ??= tier.Reason; + } + return preview ?? new FileTier(format, DecodeTier.Unavailable, null, reason ?? $"no decoder for {format}", 0, 0); + } + /// Decodes with the first provider that succeeds; null means . public DecodedImage? Decode(DecodeRequest request, CancellationToken cancellationToken = default) { diff --git a/src/Titano.Core/Model/Entities.cs b/src/Titano.Core/Model/Entities.cs index 6697f044..0cb9e600 100644 --- a/src/Titano.Core/Model/Entities.cs +++ b/src/Titano.Core/Model/Entities.cs @@ -59,6 +59,10 @@ public sealed record Sequence public int FramesVersion { get; init; } public Guid? OwnerUserId { get; init; } public int WarningCount { get; init; } + + /// Frames that decode only through their embedded preview (, BRIEF §8.2). + public int PreviewOnlyCount { get; init; } + public DateTimeOffset UpdatedAt { get; init; } /// The title, else capture date and camera, else the folder name (never empty). @@ -112,6 +116,12 @@ public sealed record Frame public long? PreviewOffset { get; init; } public long? PreviewLength { get; init; } + /// Size of the largest embedded preview like and (before orientation); + /// 0 when there is none or it is not known. + public int PreviewWidth { get; init; } + + public int PreviewHeight { get; init; } + /// Mean log2 luminance once analysed. public double? MeanLogLuminance { get; init; } diff --git a/src/Titano.Data/Archiving/ArchiveImages.cs b/src/Titano.Data/Archiving/ArchiveImages.cs index e8ae63be..c2c6e8b5 100644 --- a/src/Titano.Data/Archiving/ArchiveImages.cs +++ b/src/Titano.Data/Archiving/ArchiveImages.cs @@ -1,5 +1,6 @@ using System.Diagnostics; using Titano.Core.Archiving; +using Titano.Core.Imaging; using Titano.Core.IO; using Titano.Core.Metadata; using Titano.Core.Model; @@ -12,7 +13,8 @@ public sealed record ImagePassResult(int Built, int Reused, int Undecodable); /// /// Thumbnails (thumbs.pack) and proxies (proxies/0001.jpg) of sequences (BRIEF §8.5). Both are /// regenerable and self-validating: a thumbnail carries its source fingerprint, a proxy its capture time, so after -/// frames were renumbered the stale ones are rebuilt and the rest reused. +/// frames were renumbered the stale ones are rebuilt and the rest reused. A frame nothing decodes is flagged undecodable +/// (BRIEF §8.2 tier 3); one flagged so that decodes after all gets its tier probed again. /// public sealed class ArchiveImages { @@ -23,6 +25,7 @@ public sealed class ArchiveImages private readonly SequenceWriter _writer; private readonly ArchiveIndexer _indexer; private readonly FrameImageRenderer _renderer; + private readonly FrameTiers _tiers; internal ArchiveImages(TitanoDatabase db, SequenceWriter writer, ArchiveIndexer indexer) { @@ -30,6 +33,7 @@ public sealed class ArchiveImages _writer = writer; _indexer = indexer; _renderer = new FrameImageRenderer(writer.Options.Decoder); + _tiers = new FrameTiers(db, writer, indexer); } /// Resume point inside the sequence (frames before it were done by an earlier run). @@ -42,6 +46,7 @@ public sealed class ArchiveImages var folder = paths.Sequence(sequence.Folder); var manifest = ManifestStore.ReadSequence(folder.ManifestPath); int built = 0, reused = 0, undecodable = 0; + var tiers = new Dictionary(); // 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)) @@ -58,7 +63,7 @@ public sealed class ArchiveImages reused++; continue; } - if (Render(() => _renderer.Thumbnail(folder, frame, cancellationToken)) is { } image) + if (Render(folder, frame, tiers, () => _renderer.Thumbnail(folder, frame, cancellationToken)) is { } image) { pack.Append(frame.I, image.Width, image.Height, fingerprint, image.Jpeg); built++; @@ -75,6 +80,7 @@ public sealed class ArchiveImages } pack.Commit(); } + await _tiers.RecordAsync(archive, sequence, tiers, cancellationToken).ConfigureAwait(false); await MarkAsync(archive, paths, sequence, SequenceStatus.ThumbsBuilt, undecodable == 0, cancellationToken).ConfigureAwait(false); return new ImagePassResult(built, reused, undecodable); } @@ -91,6 +97,7 @@ public sealed class ArchiveImages var manifest = ManifestStore.ReadSequence(folder.ManifestPath); Directory.CreateDirectory(folder.Proxies); int built = 0, reused = 0, undecodable = 0; + var tiers = new Dictionary(); var cadence = new Cadence(); for (var i = startFrame; i < manifest.Frames.Count; i++) { @@ -102,7 +109,7 @@ public sealed class ArchiveImages reused++; continue; } - if (Render(() => _renderer.Proxy(folder, frame, manifest.Camera, policy, cancellationToken)) is { } image) + if (Render(folder, frame, tiers, () => _renderer.Proxy(folder, frame, manifest.Camera, policy, cancellationToken)) is { } image) { AtomicFile.WriteAllBytes(path, image.Jpeg); built++; @@ -113,6 +120,7 @@ public sealed class ArchiveImages } if (cadence.Due()) await checkpoint(i + 1, manifest.Frames.Count).ConfigureAwait(false); } + await _tiers.RecordAsync(archive, sequence, tiers, cancellationToken).ConfigureAwait(false); await MarkAsync(archive, paths, sequence, SequenceStatus.ProxiesBuilt, undecodable == 0, cancellationToken).ConfigureAwait(false); return new ImagePassResult(built, reused, undecodable); } @@ -131,18 +139,52 @@ public sealed class ArchiveImages } } - /// A frame no decoder can read is skipped and counted; it never stops the pass. - private static EncodedImage? Render(Func render) + /// + /// A frame no decoder can read is skipped and counted; it never stops the pass. What decoding found goes into + /// : 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. + /// + private EncodedImage? Render(SequencePaths folder, SidecarFrame frame, Dictionary tiers, Func render) { + var path = folder.File(frame.File); + EncodedImage? image; try { - return render(); + image = render(); } - catch (Exception ex) when (ex is IOException or InvalidDataException or NotSupportedException) + catch (IOException ex) { Core.Diagnostics.Log.Warning("archive", "Frame image could not be rendered", ex); return null; } + catch (Exception ex) when (ex is InvalidDataException or NotSupportedException) + { + Core.Diagnostics.Log.Warning("archive", "Frame image could not be rendered", ex); + image = null; + } + if (image is null) + { + if (File.Exists(path)) tiers[frame.Id] = new FileTier(frame.Format, DecodeTier.Unavailable, null, "no decoder produced pixels", 0, 0); + } + else if ((frame.Flags & FrameFlags.Undecodable) != 0) + { + tiers[frame.Id] = Reprobe(path, frame, image.Tier); + } + return image; + } + + /// The tier of a frame that decoded after it was flagged undecodable (the image's own tier when probing finds none). + private FileTier Reprobe(string path, SidecarFrame frame, DecodeTier decoded) + { + try + { + if (_writer.Options.Decoder.Probe(path, frame.Format) is { Tier: not DecodeTier.Unavailable } tier) return tier; + } + catch (Exception ex) when (ex is IOException or UnauthorizedAccessException) + { + Core.Diagnostics.Log.Warning("archive", $"{path} could not be probed", ex); + } + return new FileTier(frame.Format, decoded, null, null, 0, 0); } /// Commit/checkpoint rhythm: every rendered frames or . diff --git a/src/Titano.Data/Archiving/FrameTiers.cs b/src/Titano.Data/Archiving/FrameTiers.cs new file mode 100644 index 00000000..245c89e5 --- /dev/null +++ b/src/Titano.Data/Archiving/FrameTiers.cs @@ -0,0 +1,98 @@ +using Titano.Core.Archiving; +using Titano.Core.Imaging; +using Titano.Core.Model; +using Titano.Data.Repositories; + +namespace Titano.Data.Archiving; + +/// +/// Decode tiers of archived frames (BRIEF §8.2) written to the sidecar and the index together: the tier, the +/// and flags that follow it and the preview +/// size, then the sequence row (its preview-only count for the Library chip). A sequence that gains undecodable frames +/// shows a warning at once; its next light analysis lists them. Tiers describe the host that probed or decoded the files +/// last. Only a file in a format this host decodes natively is flagged undecodable (it is damaged); one beyond this +/// host's decoders (HEIF or an unsupported RAW without a preview on the server) keeps its tier but no flag, so render +/// nodes and proxies can still provide its pixels, and a host that decodes a flagged frame clears the flag. +/// +internal sealed class FrameTiers(TitanoDatabase db, SequenceWriter writer, ArchiveIndexer indexer) +{ + /// + /// The tier of every frame of whose tier is not known yet, probed with the host's decoders + /// without decoding pixels; missing and unreadable files are left for later. + /// + public Dictionary ProbeUnknown(SequencePaths folder, SequenceManifest manifest, CancellationToken cancellationToken) + { + var tiers = new Dictionary(); + foreach (var frame in manifest.Frames) + { + 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); + Core.Diagnostics.Log.Debug("archive", $"{frame.File}: {tier.Tier} via {tier.Decoder ?? "none"}{(tier.Reason is { } why ? $" ({why})" : "")}"); + tiers[frame.Id] = tier; + } + catch (Exception ex) when (ex is IOException or UnauthorizedAccessException) + { + Core.Diagnostics.Log.Warning("archive", $"{path} could not be probed", ex); + } + } + return tiers; + } + + /// + /// Records (by frame id) where they change what the sidecar says, and returns the frames that + /// changed as they are now. Frames no longer in the sequence are ignored. + /// + /// The archive is offline or read-only. + public async Task> RecordAsync(Archive archive, Sequence sequence, IReadOnlyDictionary tiers, + CancellationToken cancellationToken) + { + if (tiers.Count == 0) return []; + var paths = new ArchivePaths(archive.RootPath); + ArchiveVolume.EnsureWritable(paths); + var changed = new List(); + var manifest = await writer.UpdateAsync(paths, sequence.Id, sequence.Folder, m => + { + changed.Clear(); + var gained = false; + var frames = new SidecarFrame[m.Frames.Count]; + for (var i = 0; i < frames.Length; i++) + { + var frame = m.Frames[i]; + frames[i] = frame; + if (!tiers.TryGetValue(frame.Id, out var tier)) continue; + var updated = SequenceFrames.WithTier(frame, tier, DecodesNatively(frame.Format)); + if (updated.Tier == frame.Tier && updated.Flags == frame.Flags && updated.PreviewW == frame.PreviewW && updated.PreviewH == frame.PreviewH) + continue; + gained |= (updated.Flags & ~frame.Flags & FrameFlags.Undecodable) != 0; + frames[i] = updated; + changed.Add(updated); + } + if (changed.Count == 0) return null; + return m with + { + Frames = frames, + Status = gained ? m.Status | SequenceStatus.HasWarnings : m.Status, + UpdatedAt = DateTimeOffset.UtcNow, + }; + }, cancellationToken).ConfigureAwait(false); + if (changed.Count == 0) return []; + await db.Frames.UpdateTiersAsync(changed.ConvertAll(f => new FrameTierFields(f.Id, f.Flags, f.PreviewW ?? 0, f.PreviewH ?? 0)), cancellationToken) + .ConfigureAwait(false); + var folder = paths.Sequence(sequence.Folder); + if (await db.Sequences.GetAsync(sequence.Id, cancellationToken).ConfigureAwait(false) is { } existing) + await db.Sequences.UpsertAsync(SequenceMapping.ToSequence(manifest, archive.Id, folder, existing), cancellationToken).ConfigureAwait(false); + await indexer.RecordStampsAsync(archive, folder, sequence.Id, cancellationToken).ConfigureAwait(false); + return changed; + } + + /// + /// Whether this host decodes natively, so a file of it that yields no pixels is damaged + /// rather than beyond this host (JPEG always: the managed JPEG decoder is part of every host). + /// + private bool DecodesNatively(ImageFileFormat format) => + format == ImageFileFormat.Jpeg || writer.Options.Decoder.TierFor(format) == DecodeTier.Native; +} diff --git a/src/Titano.Data/Archiving/SequenceMapping.cs b/src/Titano.Data/Archiving/SequenceMapping.cs index 7439a955..a01bdb54 100644 --- a/src/Titano.Data/Archiving/SequenceMapping.cs +++ b/src/Titano.Data/Archiving/SequenceMapping.cs @@ -32,6 +32,8 @@ internal static class SequenceMapping Format = f.Format, PreviewOffset = f.PreviewOffset, PreviewLength = f.PreviewLength, + PreviewWidth = f.PreviewW ?? 0, + PreviewHeight = f.PreviewH ?? 0, Flags = f.Flags, }; @@ -76,6 +78,7 @@ internal static class SequenceMapping SyncState = existing?.SyncState ?? SyncState.LocalOnly, OwnerUserId = existing?.OwnerUserId ?? (Guid.TryParse(m.CreatedBy, out var owner) ? owner : null), WarningCount = existing?.WarningCount ?? 0, + PreviewOnlyCount = m.Frames.Count(f => (f.Flags & FrameFlags.PreviewOnly) != 0), }; } diff --git a/src/Titano.Data/Migrations/postgres/0005_decode_tiers.sql b/src/Titano.Data/Migrations/postgres/0005_decode_tiers.sql new file mode 100644 index 00000000..797cef89 --- /dev/null +++ b/src/Titano.Data/Migrations/postgres/0005_decode_tiers.sql @@ -0,0 +1,7 @@ +-- Decode tiers (BRIEF §8.2): the size of each frame's largest embedded preview (before orientation, like width and +-- height), which caps the output of sequences whose frames decode only through it, and per sequence the number of +-- those preview-only frames for the Library chip. Mirrors sqlite/0005_decode_tiers.sql. + +ALTER TABLE frames ADD COLUMN preview_width integer NOT NULL DEFAULT 0; +ALTER TABLE frames ADD COLUMN preview_height integer NOT NULL DEFAULT 0; +ALTER TABLE sequences ADD COLUMN preview_only_count integer NOT NULL DEFAULT 0; diff --git a/src/Titano.Data/Migrations/sqlite/0005_decode_tiers.sql b/src/Titano.Data/Migrations/sqlite/0005_decode_tiers.sql new file mode 100644 index 00000000..06913b19 --- /dev/null +++ b/src/Titano.Data/Migrations/sqlite/0005_decode_tiers.sql @@ -0,0 +1,7 @@ +-- Decode tiers (BRIEF §8.2): the size of each frame's largest embedded preview (before orientation, like width and +-- height), which caps the output of sequences whose frames decode only through it, and per sequence the number of +-- those preview-only frames for the Library chip. Mirrors postgres/0005_decode_tiers.sql. + +ALTER TABLE frames ADD COLUMN preview_width INTEGER NOT NULL DEFAULT 0; +ALTER TABLE frames ADD COLUMN preview_height INTEGER NOT NULL DEFAULT 0; +ALTER TABLE sequences ADD COLUMN preview_only_count INTEGER NOT NULL DEFAULT 0; diff --git a/src/Titano.Data/Processing/Jobs/RenderJobHandler.cs b/src/Titano.Data/Processing/Jobs/RenderJobHandler.cs index 359361d5..66446574 100644 --- a/src/Titano.Data/Processing/Jobs/RenderJobHandler.cs +++ b/src/Titano.Data/Processing/Jobs/RenderJobHandler.cs @@ -6,6 +6,7 @@ using Titano.Core.Localization; using Titano.Core.Model; using Titano.Core.Project; using Titano.Core.Render; +using Titano.Data.Archiving; using Titano.Data.Jobs; namespace Titano.Data.Processing.Jobs; @@ -14,14 +15,15 @@ namespace Titano.Data.Processing.Jobs; /// on this machine (BRIEF §11.5): target = the sequence; parameters = Protocol /// RenderJobParameters whose preset JSON is the export section of a project (preset, overrides, name template, /// folder, batch). The project is the embedded one or the sequence's own; frames flagged missing, corrupt, excluded or -/// undecodable are left out. The preset and every batch preset render after one preparation pass into the sequence's -/// exports/ (or the export folder, never on a server), named by the template without taking a name another render -/// of this process is writing, and each finished output is recorded at once (its row id derives from the job and the -/// entry, so every attempt knows what is recorded). Checkpoint: the entries recorded and the output being written, so a -/// crashed attempt's leftovers are removed and only unfinished entries render again. A failed entry fails the job after -/// the others ran; retrying renders it alone. +/// undecodable are left out, and a frame found undecodable while rendering is shown as its nearest neighbour, logged +/// and (when the originals were read) flagged so later renders leave it out (BRIEF §8.2). The preset and every batch +/// preset render after one preparation pass into the sequence's exports/ (or the export folder, never on a +/// server), named by the template without taking a name another render of this process is writing, and each finished +/// output is recorded at once (its row id derives from the job and the entry, so every attempt knows what is recorded). +/// Checkpoint: the entries recorded and the output being written, so a crashed attempt's leftovers are removed and only +/// unfinished entries render again. A failed entry fails the job after the others ran; retrying renders it alone. /// -internal sealed class RenderJobHandler(TitanoDatabase db, ProcessingOptions options) : IJobHandler +internal sealed class RenderJobHandler(TitanoDatabase db, ProcessingOptions options, FrameTiers tiers) : IJobHandler { public JobKind Kind => JobKind.Render; @@ -64,7 +66,7 @@ internal sealed class RenderJobHandler(TitanoDatabase db, ProcessingOptions opti var frames = await SequenceFrames.LoadAsync(db, archive, sequence, parameters.UseProxies, cancellation).ConfigureAwait(false); IReadOnlyList outcomes; - await using (var render = new SequenceRender(frames, project, SequenceAnalyser.ReadReport(folder, frames.Count), options)) + await using (var render = new SequenceRender(frames, project, SequenceAnalyser.ReadReport(folder, frames.Count), options, originals: !parameters.UseProxies)) { outcomes = await render.RenderAllAsync( targets, @@ -87,6 +89,20 @@ internal sealed class RenderJobHandler(TitanoDatabase db, ProcessingOptions opti }, context.ReportAsync, cancellation).ConfigureAwait(false); + if (render.StandIns.Count > 0) + context.Log(Loc.L("Export.Log.StandIns", string.Join(", ", render.StandIns.Select(i => (i + 1).ToString(Loc.Culture))))); + // Recorded without the job's token: what decoding found holds even if the job is cancelled meanwhile, and a + // sidecar that cannot be written now only leaves it to the next analysis or render. + try + { + var decoded = render.Decoded.ToDictionary(kv => frames.Frames[kv.Key].Id, kv => kv.Value); + await tiers.RecordAsync(archive, sequence, decoded, CancellationToken.None).ConfigureAwait(false); + } + catch (Exception ex) when (ex is IOException or UnauthorizedAccessException or InvalidDataException or LeaseUnavailableException + or ArchiveOfflineException) + { + Core.Diagnostics.Log.Warning("processing", $"The decode tiers of {sequence.DisplayTitle} could not be recorded", ex); + } } var failed = outcomes.Where(o => o.Error is not null).ToList(); foreach (var outcome in failed) context.Log(Loc.L("Export.Log.Failed", outcome.Target.PresetName, outcome.Error)); diff --git a/src/Titano.Data/Processing/ProcessingService.cs b/src/Titano.Data/Processing/ProcessingService.cs index 581a87b7..02019ab8 100644 --- a/src/Titano.Data/Processing/ProcessingService.cs +++ b/src/Titano.Data/Processing/ProcessingService.cs @@ -17,6 +17,7 @@ public sealed class ProcessingService : IJobHandlerProvider { private readonly TitanoDatabase _db; private readonly SequenceAnalyser _analyser; + private readonly FrameTiers _tiers; public ProcessingService(TitanoDatabase db, ProcessingOptions? options = null) { @@ -24,7 +25,9 @@ public sealed class ProcessingService : IJobHandlerProvider Options = options ?? new ProcessingOptions(); // Sidecar and project writes coordinate with the archive engine through the archive's lease files. var writer = new SequenceWriter(Options.Archive); - _analyser = new SequenceAnalyser(db, Options, writer, new ArchiveIndexer(db, writer), new ProjectStore(db, writer)); + var indexer = new ArchiveIndexer(db, writer); + _tiers = new FrameTiers(db, writer, indexer); + _analyser = new SequenceAnalyser(db, Options, writer, indexer, new ProjectStore(db, writer)); } public ProcessingOptions Options { get; } @@ -32,7 +35,7 @@ public sealed class ProcessingService : IJobHandlerProvider /// Handlers for , and . public IReadOnlyList CreateJobHandlers() { - var render = new RenderJobHandler(_db, Options); + var render = new RenderJobHandler(_db, Options, _tiers); return [ new AnalyseJobHandler(_db, _analyser, JobKind.LightAnalyse), diff --git a/src/Titano.Data/Processing/RenderNodeClient.cs b/src/Titano.Data/Processing/RenderNodeClient.cs index c8938ad7..9a23038e 100644 --- a/src/Titano.Data/Processing/RenderNodeClient.cs +++ b/src/Titano.Data/Processing/RenderNodeClient.cs @@ -464,6 +464,8 @@ public sealed class RenderNodeClient : IAsyncDisposable Height = f.Height, Format = Enum.TryParse(f.Format, ignoreCase: true, out var format) ? format : default, Flags = (FrameFlags)f.Flags, + PreviewWidth = f.PreviewWidth, + PreviewHeight = f.PreviewHeight, }; /// diff --git a/src/Titano.Data/Processing/SequenceAnalyser.cs b/src/Titano.Data/Processing/SequenceAnalyser.cs index c99cd8c7..0122627f 100644 --- a/src/Titano.Data/Processing/SequenceAnalyser.cs +++ b/src/Titano.Data/Processing/SequenceAnalyser.cs @@ -4,6 +4,7 @@ using Titano.Core.Advice; using Titano.Core.Analysis; using Titano.Core.Analysis.Deep; using Titano.Core.Archiving; +using Titano.Core.Imaging; using Titano.Core.Model; using Titano.Data.Archiving; using Titano.Data.Repositories; @@ -20,13 +21,17 @@ internal sealed record AnalysisOutcome(Sequence Sequence, int Frames, int Warnin /// /// Light (BRIEF §10.1) and deep (§10.2) analysis of archived sequences, followed by the advisor (§10.3–10.4) filling the -/// Auto values of the project. The light pass reads the index only (plus one file for the GPS fix); the deep pass decodes -/// every frame, checkpointing into .titano/analysis.partial so a cancelled, paused or crashed run resumes, and -/// saves analysis.json atomically. Results land in the sidecar status, the index (status, warning count, compact -/// summary, per-frame light statistics) and project.json. +/// Auto values of the project. The light pass reads the index, probes the decode tier (§8.2) of frames whose tier is not +/// known yet (headers only, once per frame) and reads one file for the GPS fix; the deep pass decodes every frame, +/// checkpointing into .titano/analysis.partial so a cancelled, paused or crashed run resumes, records the tier +/// each frame decoded at (or that it did not decode) and saves analysis.json atomically. Results land in the +/// sidecar (status, tiers), the index (status, warning count, compact summary, tier flags, per-frame light statistics) +/// and project.json. /// internal sealed class SequenceAnalyser(TitanoDatabase db, ProcessingOptions options, SequenceWriter writer, ArchiveIndexer indexer, ProjectStore projects) { + private readonly FrameTiers _tiers = new(db, writer, indexer); + /// Analyses one sequence; null when it is no longer in the index. /// The archive is offline or read-only. public async Task AnalyseAsync(Guid sequenceId, bool deep, IProgress? progress, CancellationToken cancellationToken) @@ -37,12 +42,23 @@ internal sealed class SequenceAnalyser(TitanoDatabase db, ProcessingOptions opti ArchiveVolume.EnsureWritable(paths); 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); + frames = WithTiers(frames, await _tiers.RecordAsync(archive, sequence, probed, cancellationToken).ConfigureAwait(false)); var metadata = frames.LightFrames(); - var light = LightAnalyzer.Analyze(metadata); - var report = deep - ? await AnalyseDeepAsync(frames, metadata, folder, progress, cancellationToken).ConfigureAwait(false) - : ReadReport(folder, frames.Count); + 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)); + metadata = [.. metadata.Select((m, i) => m with { Flags = frames.Frames[i].Flags })]; + } + else + { + report = ReadReport(folder, frames.Count); + } + var light = LightAnalyzer.Analyze(metadata); var context = SequenceContext.FromFrames(frames.Frames, options.AdvisorCodecs); bool changed, unreadable = false; @@ -100,15 +116,47 @@ internal sealed class SequenceAnalyser(TitanoDatabase db, ProcessingOptions opti } } - private async Task AnalyseDeepAsync(SequenceFrames frames, IReadOnlyList metadata, SequencePaths folder, - IProgress? progress, CancellationToken cancellationToken) + /// The frame rows with the tier fields of sidecar frames. + private static SequenceFrames WithTiers(SequenceFrames frames, IReadOnlyList changed) + { + if (changed.Count == 0) return frames; + var byId = changed.ToDictionary(f => f.Id); + var rows = frames.Frames.Select(f => byId.TryGetValue(f.Id, out var s) + ? f with { Flags = s.Flags, PreviewWidth = s.PreviewW ?? 0, PreviewHeight = s.PreviewH ?? 0 } + : f).ToList(); + return frames with { Frames = rows }; + } + + /// + /// 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. + /// + private static Dictionary Decoded(SequenceFrames frames, DeepAnalysisReport report, IReadOnlyDictionary observed) + { + var tiers = new Dictionary(); + foreach (var (index, tier) in observed) tiers[frames.Frames[index].Id] = tier; + foreach (var index in report.Issues.Undecodable) + { + if (index >= frames.Count || observed.ContainsKey(index) || frames.Files[index] is null + || (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); + } + return tiers; + } + + private async Task<(DeepAnalysisReport Report, IReadOnlyDictionary Observed)> AnalyseDeepAsync(SequenceFrames frames, + IReadOnlyList metadata, SequencePaths folder, IProgress? progress, CancellationToken cancellationToken) { var file = new DeepAnalysisCheckpointFile(Path.Combine(folder.MetaDirectory, DeepAnalysisCheckpointFile.FileName)); DeepAnalysisReport report; - await using (var source = new SequenceFrameSource(frames, metadata, options.Archive.Decoder)) + IReadOnlyDictionary observed; + await using (var source = new SequenceFrameSource(frames, metadata, options.Archive.Decoder) { Originals = true }) { report = await DeepAnalyzer.AnalyzeAsync(source, options.DeepAnalysis, new FramesCheckpoint(file, ContentKey(frames)), progress, cancellationToken) .ConfigureAwait(false); + observed = source.Observed.ToDictionary(); } report.Save(folder.AnalysisPath); file.Delete(); @@ -118,7 +166,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; + return (report, observed); } /// Identity of the analysed content: frames replaced or renumbered since a checkpoint was written (with the diff --git a/src/Titano.Data/Processing/SequenceFrameSource.cs b/src/Titano.Data/Processing/SequenceFrameSource.cs index 34f6a268..8fb0aaf9 100644 --- a/src/Titano.Data/Processing/SequenceFrameSource.cs +++ b/src/Titano.Data/Processing/SequenceFrameSource.cs @@ -1,3 +1,4 @@ +using System.Collections.Concurrent; using System.Globalization; using Titano.Core.Advice; using Titano.Core.Analysis; @@ -10,9 +11,11 @@ namespace Titano.Data.Processing; /// /// The frames of a decoded by the host's (oriented, linear /// light, embedded previews when nothing else decodes). For the deep analysis a frame that is missing, corrupt or -/// unreadable is null, as expects. Renders exclude flagged frames from the plan, so a -/// frame that still cannot be decoded stops the render with ; render reads go through -/// a per decode size, which decodes ahead in index order and spills under memory pressure. +/// unreadable is null, as expects. Renders exclude flagged frames from the plan; a frame that +/// still cannot be decoded stops the render with , unless the render gave +/// : 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 . Render reads go through a +/// per decode size, which decodes ahead in index order and spills under memory pressure. /// internal sealed class SequenceFrameSource : IFrameSource, IRenderFrameSource, IAsyncDisposable { @@ -27,6 +30,9 @@ internal sealed class SequenceFrameSource : IFrameSource, IRenderFrameSource, IA private readonly FrameWindowOptions _windowOptions; private readonly Lazy<(int Width, int Height)> _size; private readonly SemaphoreSlim _gate = new(1, 1); + private readonly ConcurrentDictionary _observed = new(); + private readonly ConcurrentDictionary _standIns = new(); + private readonly ConcurrentDictionary _failed = new(); private FrameWindow? _window; private (int Edge, DecodePurpose Purpose) _windowKey; @@ -40,6 +46,24 @@ internal sealed class SequenceFrameSource : IFrameSource, IRenderFrameSource, IA _size = new Lazy<(int, int)>(MeasureSize); } + /// The files are the archive's originals, so what decoding finds describes the frames (). + public bool Originals { get; init; } + + /// + /// Positions the render plan leaves out. When set, a render frame that does not decode is replaced by the nearest + /// planned frame that does, instead of failing the render. + /// + public IReadOnlySet? RenderExcluded { get; init; } + + /// + /// Decode tier each frame decoded at, or when nothing decoded its file (sizes + /// oriented, as reports them); recorded only for . + /// + public IReadOnlyDictionary Observed => _observed; + + /// Positions a render showed a neighbour for because they did not decode, ascending. + public IReadOnlyList StandIns => [.. _standIns.Keys.Order()]; + public int Count => _frames.Count; public int FrameCount => _frames.Count; @@ -92,35 +116,77 @@ internal sealed class SequenceFrameSource : IFrameSource, IRenderFrameSource, IA } } - private ImageBuffer DecodeForRender(int index, int maxLongEdge, DecodePurpose purpose, CancellationToken cancellationToken) => - Decode(index, maxLongEdge, purpose, cancellationToken)?.Pixels ?? throw Unreadable(index); + private ImageBuffer DecodeForRender(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. + _failed.TryAdd(index, 0); + for (var distance = 1; distance < Count; distance++) + { + ReadOnlySpan neighbours = [index - distance, index + distance]; + foreach (var neighbour in neighbours) + { + 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; + } + _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; + } + } + throw Unreadable(index); + } private DecodedImage? Decode(int index, int maxLongEdge, DecodePurpose purpose, CancellationToken cancellationToken) { if (_frames.Files[index] is not { } file) return null; + DecodedImage? decoded; try { - return _decoder.Decode(new DecodeRequest(file, maxLongEdge, purpose), cancellationToken); + decoded = _decoder.Decode(new DecodeRequest(file, maxLongEdge, purpose), cancellationToken); } catch (Exception ex) when (ex is IOException or UnauthorizedAccessException) { 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); + } + return decoded; } - /// The dominant oriented size from the metadata, else the size the first renderable frame decodes to. + /// + /// The dominant oriented size from the metadata, else the size the first renderable frame decodes to (frames that do + /// not decode are passed over while a render may stand in for them). + /// private (int, int) MeasureSize() { var context = SequenceContext.FromFrames(_frames.Frames); if (context.SourceWidth > 0 && context.SourceHeight > 0) return (context.SourceWidth, context.SourceHeight); var skipped = _frames.NotRenderable().ToHashSet(); + if (RenderExcluded is { } excluded) skipped.UnionWith(excluded); + int? first = null; for (var i = 0; i < Count; i++) { if (skipped.Contains(i)) continue; - using var probe = Decode(i, ProbeLongEdge, DecodePurpose.Thumbnail, CancellationToken.None) ?? throw Unreadable(i); - return (probe.SourceWidth, probe.SourceHeight); + using var probe = Decode(i, ProbeLongEdge, DecodePurpose.Thumbnail, CancellationToken.None); + if (probe is not null) return (probe.SourceWidth, probe.SourceHeight); + first ??= i; + if (RenderExcluded is null) break; } + if (first is { } unreadable) throw Unreadable(unreadable); throw new RenderException("Export.Render.NoFrames", "Every frame is excluded."); } diff --git a/src/Titano.Data/Processing/SequenceRender.cs b/src/Titano.Data/Processing/SequenceRender.cs index 2c5e8ca4..edf29c10 100644 --- a/src/Titano.Data/Processing/SequenceRender.cs +++ b/src/Titano.Data/Processing/SequenceRender.cs @@ -1,6 +1,7 @@ using Titano.Core.Analysis; using Titano.Core.Analysis.Deep; using Titano.Core.Export; +using Titano.Core.Imaging; using Titano.Core.Localization; using Titano.Core.Model; using Titano.Core.Project; @@ -30,23 +31,36 @@ internal sealed class SequenceRender : IAsyncDisposable private readonly RenderOptions _options; /// The sequence's deep report, when current: its luminance spares the preparation a decode pass. - public SequenceRender(SequenceFrames frames, ProjectSettings project, DeepAnalysisReport? analysis, ProcessingOptions options) + /// The frames' files are the archive's originals (no proxies), so describes them. + public SequenceRender(SequenceFrames frames, ProjectSettings project, DeepAnalysisReport? analysis, ProcessingOptions options, bool originals = false) { var metadata = frames.LightFrames(); - _source = new SequenceFrameSource(frames, metadata, options.Archive.Decoder, options.FrameWindow); + var planned = project with { ExcludedFrames = [.. project.ExcludedFrames.Union(frames.NotRenderable()).Order()] }; + // A frame that turns out not to decode is shown as its nearest planned neighbour instead of failing the render. + _source = new SequenceFrameSource(frames, metadata, options.Archive.Decoder, options.FrameWindow) + { + Originals = originals, + RenderExcluded = planned.ExcludedFrames.ToHashSet(), + }; _processing = options; _options = options.Render; Input = new RenderInput { Source = _source, Frames = SourceFrameInfo.From(frames.Frames, LightAnalyzer.Analyze(metadata).AdjustedTimes), - Project = project with { ExcludedFrames = [.. project.ExcludedFrames.Union(frames.NotRenderable()).Order()] }, + Project = planned, Analysis = analysis?.FrameCount == frames.Count ? new PrecomputedAnalysis { LogLuminance = Luminance(analysis.Frames) } : null, }; } public RenderInput Input { get; } + /// Positions that did not decode during the render; a neighbour stood in for each (ascending). + public IReadOnlyList StandIns => _source.StandIns; + + /// The decode tier each frame was read at so far (unavailable when it did not decode); originals only. + public IReadOnlyDictionary Decoded => _source.Observed; + /// Encoder, geometry and plan of an export before its output is named. public RenderSetup Setup(ResolvedExport export) => Pipeline(export).Setup(Input, new RenderTarget(export, ExportNaming.FallbackName), _options); diff --git a/src/Titano.Data/Repositories/FrameRepository.cs b/src/Titano.Data/Repositories/FrameRepository.cs index cb10e5e7..bac5ed69 100644 --- a/src/Titano.Data/Repositories/FrameRepository.cs +++ b/src/Titano.Data/Repositories/FrameRepository.cs @@ -16,6 +16,9 @@ public sealed record DuplicateFrame(string Sha256, Guid FrameId, Guid SequenceId /// Light statistics of one frame, written by the analysis job. public readonly record struct FrameLightStats(Guid FrameId, double? MeanLogLuminance, double? ClipLow, double? ClipHigh); +/// Decode-tier fields of one frame (BRIEF §8.2): its flags and the stored size of its largest embedded preview. +public readonly record struct FrameTierFields(Guid FrameId, FrameFlags Flags, int PreviewWidth, int PreviewHeight); + /// Per-format totals and flagged-frame counts of one sequence. public sealed record SequenceFrameStats(IReadOnlyList ByFormat, long Missing, long Corrupt, long Duplicate, long Excluded); @@ -27,7 +30,7 @@ public sealed class FrameRepository "id", "sequence_id", "idx", "relative_path", "file_name", "original_file_name", "size", "sha256", "captured_at", "utc_offset_minutes", "timestamp_source", "exposure_time", "f_number", "iso", "focal_length", "exposure_bias", "white_balance", "orientation", "width", "height", "format", "preview_offset", "preview_length", - "mean_log_luminance", "clip_low", "clip_high", "flags", + "mean_log_luminance", "clip_low", "clip_high", "flags", "preview_width", "preview_height", ]; private static readonly string Columns = string.Join(", ", ColumnNames); @@ -37,7 +40,7 @@ public sealed class FrameRepository /// Rows per statement of the batched PostgreSQL updates (4 or fewer parameters per row). private const int Batch = 1000; - /// Rows per multi-row upsert: 30 x 27 parameters stays below SQLite's historical 999-variable limit. + /// Rows per multi-row upsert: 30 x 29 parameters stays below SQLite's historical 999-variable limit. private const int UpsertRows = 30; /// Light statistics belong to the analysis job; a sync from the sidecar never clears them. @@ -130,6 +133,19 @@ public sealed class FrameRepository return updated; }, cancellationToken); + /// Writes the decode-tier fields of many frames in one transaction; returns the number of frames updated. + public Task UpdateTiersAsync(IReadOnlyList frames, CancellationToken cancellationToken = default) => + _db.InTransactionAsync(async session => + { + var updated = 0; + 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); + /// /// Makes the frames of a sequence exactly (from its sidecar): rows are upserted by id, /// rows not listed are deleted, light statistics of kept rows survive. Indexes may be renumbered freely. @@ -239,7 +255,7 @@ public sealed class FrameRepository f.Id, f.SequenceId, f.Index, f.RelativePath, f.FileName, f.OriginalFileName, f.Size, f.Sha256?.ToLowerInvariant(), SequenceRepository.Wall(f.CapturedAt), f.UtcOffsetMinutes, (int)f.TimestampSource, f.ExposureTime, f.FNumber, f.Iso, f.FocalLength, f.ExposureBias, f.WhiteBalance, f.Orientation, f.Width, f.Height, (int)f.Format, f.PreviewOffset, - f.PreviewLength, f.MeanLogLuminance, f.ClipLow, f.ClipHigh, (int)f.Flags, + f.PreviewLength, f.MeanLogLuminance, f.ClipLow, f.ClipHigh, (int)f.Flags, f.PreviewWidth, f.PreviewHeight, ]; private static Frame Map(DbRow r) => new() @@ -271,5 +287,7 @@ public sealed class FrameRepository ClipLow = r.GetDoubleOrNull(24), ClipHigh = r.GetDoubleOrNull(25), Flags = (FrameFlags)r.GetInt32(26), + PreviewWidth = r.GetInt32(27), + PreviewHeight = r.GetInt32(28), }; } diff --git a/src/Titano.Data/Repositories/SequenceRepository.cs b/src/Titano.Data/Repositories/SequenceRepository.cs index a4735d37..a7e78308 100644 --- a/src/Titano.Data/Repositories/SequenceRepository.cs +++ b/src/Titano.Data/Repositories/SequenceRepository.cs @@ -122,7 +122,8 @@ public sealed class SequenceRepository private const string Columns = "s.id, s.archive_id, s.title, s.folder, s.camera_make, s.camera_model, s.camera_serial, s.lens, s.first_capture, s.last_capture, " + "s.frame_count, s.median_interval, s.total_bytes, s.status, s.rendered_count, s.verified_at, s.hero_frame_id, s.rating, s.notes, " + - "s.settings_json, s.analysis_json, s.origin, s.sync_state, s.settings_version, s.frames_version, s.owner_user_id, s.warning_count, s.updated_at"; + "s.settings_json, s.analysis_json, s.origin, s.sync_state, s.settings_version, s.frames_version, s.owner_user_id, s.warning_count, s.updated_at, " + + "s.preview_only_count"; /// rendered_count is owned by once the row exists. private static readonly string UpsertSql = SqlDialect.Upsert("sequences", @@ -130,12 +131,12 @@ public sealed class SequenceRepository "id", "archive_id", "title", "title_key", "search_text", "folder", "camera_make", "camera_model", "camera_serial", "lens", "first_capture", "last_capture", "frame_count", "median_interval", "total_bytes", "status", "rendered_count", "verified_at", "hero_frame_id", "rating", "notes", "settings_json", "analysis_json", "origin", "sync_state", "settings_version", - "frames_version", "owner_user_id", "warning_count", "updated_at", + "frames_version", "owner_user_id", "warning_count", "updated_at", "preview_only_count", ], 1, ["rendered_count"]); /// Ordinal of the tag list after , and of the sort key a listing appends after it. - private const int TagsOrdinal = 28; - private const int SortKeyOrdinal = 29; + private const int TagsOrdinal = 29; + private const int SortKeyOrdinal = 30; private readonly DbAccess _db; private readonly string _select; @@ -445,7 +446,7 @@ public sealed class SequenceRepository s.Id, s.ArchiveId, s.Title, s.DisplayTitle.ToLowerInvariant(), search, s.Folder, s.CameraMake, s.CameraModel, s.CameraSerial, s.Lens, Wall(s.FirstCapture), Wall(s.LastCapture), s.FrameCount, s.MedianInterval, s.TotalBytes, (int)s.Status, s.RenderedCount, s.VerifiedAt, s.HeroFrameId, s.Rating, s.Notes, DbParam.Json(s.SettingsJson), DbParam.Json(s.AnalysisJson), (int)s.Origin, (int)s.SyncState, - s.SettingsVersion, s.FramesVersion, s.OwnerUserId, s.WarningCount, s.UpdatedAt, + s.SettingsVersion, s.FramesVersion, s.OwnerUserId, s.WarningCount, s.UpdatedAt, s.PreviewOnlyCount, ], cancellationToken); } @@ -523,6 +524,7 @@ public sealed class SequenceRepository OwnerUserId = r.GetGuidOrNull(25), WarningCount = r.GetInt32(26), UpdatedAt = r.GetDateTimeOffset(27), + PreviewOnlyCount = r.GetInt32(28), Tags = tags, }; } diff --git a/src/Titano.Protocol/Sequences.cs b/src/Titano.Protocol/Sequences.cs index 8ad09422..ecc90de6 100644 --- a/src/Titano.Protocol/Sequences.cs +++ b/src/Titano.Protocol/Sequences.cs @@ -1,6 +1,7 @@ namespace Titano.Protocol; /// Sequence summary for lists (BRIEF §12.2 card data). +/// Frames that decode only through their embedded preview (BRIEF §8.2 tier 2). public sealed record SequenceDto( Guid Id, string? Title, @@ -24,7 +25,8 @@ public sealed record SequenceDto( int FramesVersion, int WarningCount, Guid? OwnerUserId, - DateTimeOffset UpdatedAt); + DateTimeOffset UpdatedAt, + int PreviewOnlyCount = 0); /// A page of results with an opaque keyset cursor; null cursor = last page. public sealed record Page(IReadOnlyList Items, string? NextCursor, long? Total); @@ -45,6 +47,8 @@ public sealed record UpdateSequenceRequest( int? ExpectedSettingsVersion, Guid? HeroFrameId = null); +/// Width of the largest embedded preview before orientation, like ; 0 when unknown. +/// Height of that preview before orientation; 0 when unknown. public sealed record FrameDto( Guid Id, Guid SequenceId, @@ -62,7 +66,9 @@ public sealed record FrameDto( int Width, int Height, string Format, - long Flags); + long Flags, + int PreviewWidth = 0, + int PreviewHeight = 0); /// GET /api/v1/sequences/{id}/manifest: what a client needs to diff a sequence (BRIEF §14.5). public sealed record SequenceManifest( diff --git a/src/Titano.Server/Api/Dto.cs b/src/Titano.Server/Api/Dto.cs index a80cf647..d3ca9e76 100644 --- a/src/Titano.Server/Api/Dto.cs +++ b/src/Titano.Server/Api/Dto.cs @@ -17,11 +17,11 @@ public static class Dto public static SequenceDto Sequence(Sequence s) => new( s.Id, s.Title, s.DisplayTitle, s.Folder, s.CameraMake, s.CameraModel, s.Lens, s.FirstCapture, s.LastCapture, s.FrameCount, s.MedianInterval, s.TotalBytes, (long)s.Status, s.RenderedCount, s.HeroFrameId, s.Tags, s.Rating, s.Notes, s.SettingsVersion, - s.FramesVersion, s.WarningCount, s.OwnerUserId, s.UpdatedAt); + s.FramesVersion, s.WarningCount, s.OwnerUserId, s.UpdatedAt, s.PreviewOnlyCount); public static FrameDto Frame(Frame f) => new( f.Id, f.SequenceId, f.Index, f.FileName, f.Size, f.Sha256, f.CapturedAt, f.UtcOffsetMinutes, f.ExposureTime, f.FNumber, f.Iso, - f.FocalLength, f.Orientation, f.Width, f.Height, f.Format.ToString().ToLowerInvariant(), (long)f.Flags); + f.FocalLength, f.Orientation, f.Width, f.Height, f.Format.ToString().ToLowerInvariant(), (long)f.Flags, f.PreviewWidth, f.PreviewHeight); public static RenderDto Render(Render r) => new(r.Id, r.PresetName, r.Codec, r.Width, r.Height, r.Fps, r.DurationSeconds, r.Bytes, r.Created, r.ProducedBy); diff --git a/src/Titano.Ui/Controls/StatusGlyph.cs b/src/Titano.Ui/Controls/StatusGlyph.cs index d5d13bc1..76f093c8 100644 --- a/src/Titano.Ui/Controls/StatusGlyph.cs +++ b/src/Titano.Ui/Controls/StatusGlyph.cs @@ -23,6 +23,9 @@ public enum StatusKind Paused, Online, Offline, + + /// Frames that decode only through their embedded preview (BRIEF §8.2 tier 2). + PreviewOnly, } /// Glyph + text in the colour of the status kind. diff --git a/src/Titano.Ui/Themes/Controls.xaml b/src/Titano.Ui/Themes/Controls.xaml index 158b26f7..044df000 100644 --- a/src/Titano.Ui/Themes/Controls.xaml +++ b/src/Titano.Ui/Themes/Controls.xaml @@ -1356,6 +1356,9 @@ + + + diff --git a/tests/Titano.Core.Tests/Archiving/DecodeTierTests.cs b/tests/Titano.Core.Tests/Archiving/DecodeTierTests.cs new file mode 100644 index 00000000..72e5c19a --- /dev/null +++ b/tests/Titano.Core.Tests/Archiving/DecodeTierTests.cs @@ -0,0 +1,198 @@ +using System.Text; +using Titano.Core.Advice; +using Titano.Core.Analysis; +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.Core.Tests.Analysis.Deep; +using Titano.Core.Tests.Raw; +using Titano.Testing; + +namespace Titano.Core.Tests.Archiving; + +/// Decode tiers of frames (BRIEF §8.2): sidecar fields, flags, thumbnails from previews, and what the advisor reads. +[Category("archive")] +public sealed class DecodeTierTests(TestContext context) +{ + private static readonly DateTime Start = new(2026, 9, 28, 18, 0, 0); + + private static SidecarFrame Sidecar(int orientation = 1, int? previewW = null, int? previewH = null) => new() + { + Id = Guid.CreateVersion7(), + File = "originals/IMG_0001.CR2", + Format = ImageFileFormat.Cr2, + W = 6000, + H = 4000, + Orientation = orientation, + PreviewW = previewW, + PreviewH = previewH, + }; + + [Test] + public void TheTierSetsItsFlagsAndThePreviewSize() + { + var frame = Sidecar(orientation: 6) with { Flags = FrameFlags.Duplicate }; + // FileTier sizes are oriented; the sidecar keeps them like W and H, before orientation. + var preview = SequenceFrames.WithTier(frame, new FileTier(ImageFileFormat.Cr2, DecodeTier.EmbeddedPreview, "managed-preview", "body", 1080, 1620)); + Assert.Equal((DecodeTier.EmbeddedPreview, FrameFlags.Duplicate | FrameFlags.PreviewOnly, (int?)1620, (int?)1080), + (preview.Tier, preview.Flags, preview.PreviewW, preview.PreviewH)); + + var none = SequenceFrames.WithTier(preview, new FileTier(ImageFileFormat.Cr2, DecodeTier.Unavailable, null, "nothing", 0, 0)); + Assert.Equal((FrameFlags.Duplicate | FrameFlags.Undecodable, (int?)1620), (none.Flags, none.PreviewW)); + + var native = SequenceFrames.WithTier(none, new FileTier(ImageFileFormat.Cr2, DecodeTier.Native, "wic", null, 4000, 6000)); + Assert.Equal((DecodeTier.Native, FrameFlags.Duplicate, (int?)1620), (native.Tier, native.Flags, native.PreviewW)); + + // A host without a native decoder for the format proves nothing about the file: tier kept, flag left as it was. + var beyond = SequenceFrames.WithTier(native, new FileTier(ImageFileFormat.Cr2, DecodeTier.Unavailable, null, "nothing here", 0, 0), damaged: false); + Assert.Equal((DecodeTier.Unavailable, FrameFlags.Duplicate), (beyond.Tier, beyond.Flags)); + Assert.Equal(FrameFlags.Duplicate | FrameFlags.Undecodable, + SequenceFrames.WithTier(none, new FileTier(ImageFileFormat.Cr2, DecodeTier.Unavailable, null, "nothing here", 0, 0), damaged: false).Flags); + } + + [Test] + public void SidecarsKeepTheTierAndThePreviewSizeAndOlderOnesReadAsUnknown() + { + var path = Path.Combine(context.CreateTempDirectory(), "sequence.json"); + var frame = SequenceFrames.WithTier(Sidecar(previewW: 1620, previewH: 1080), + new FileTier(ImageFileFormat.Cr2, DecodeTier.EmbeddedPreview, "managed-preview", null, 1620, 1080)); + ManifestStore.WriteSequence(path, new SequenceManifest { Id = Guid.CreateVersion7(), Frames = [frame] }); + var json = File.ReadAllText(path); + Assert.Contains("\"tier\":\"EmbeddedPreview\"", json); + Assert.Contains("\"previewW\":1620", json); + var read = ManifestStore.ReadSequence(path).Frames.Single(); + Assert.Equal((DecodeTier.EmbeddedPreview, FrameFlags.PreviewOnly, (int?)1620, (int?)1080), (read.Tier, read.Flags, read.PreviewW, read.PreviewH)); + + // A frame never probed writes no tier, and a sidecar from before tiers reads as unknown. + ManifestStore.WriteSequence(path, new SequenceManifest { Id = Guid.CreateVersion7(), Frames = [Sidecar()] }); + Assert.DoesNotContain("tier", File.ReadAllText(path).Split('"')); + var older = ManifestStore.ParseSequence(Encoding.UTF8.GetBytes( + """{"id":"0192f7a0-0000-7000-8000-000000000001","frames":[{"id":"0192f7a0-0000-7000-8000-000000000002","file":"a.jpg","tier":"SomethingNew"}]}"""), "test"); + Assert.Equal((DecodeTier)0, older.Frames.Single().Tier); + } + + [Test] + public void IndexingKeepsTheSizeOfTheLargestPreview() + { + var metadata = new FrameMetadata + { + Format = ImageFileFormat.Cr2, + Width = 6000, + Height = 4000, + Previews = [new EmbeddedPreview(4096, 90_000, 1620, 1080, PreviewKind.Preview), new EmbeddedPreview(100, 4000, 160, 120, PreviewKind.Thumbnail)], + }; + var frame = SequenceFrames.FromMetadata(Guid.CreateVersion7(), "a.cr2", "a.cr2", 10, null, metadata); + Assert.Equal((4096L, (int?)1620, (int?)1080, (DecodeTier)0), (frame.PreviewOffset!.Value, frame.PreviewW, frame.PreviewH, frame.Tier)); + Assert.Null(SequenceFrames.FromMetadata(Guid.CreateVersion7(), "b.jpg", "b.jpg", 10, null, new FrameMetadata { Format = ImageFileFormat.Jpeg }).PreviewW); + } + + [Test] + public void ThumbnailsUseAStoredPreviewOnlyWhenItReachesTheThumbnailSize() + { + var archive = new ArchivePaths(context.CreateTempDirectory("archive")); + var sequence = archive.Sequence("s"); + Directory.CreateDirectory(sequence.Originals); + using var scene = SyntheticRaw.Smooth(640, 480); + var dng = SyntheticDng.Write(scene, new DngSpec()); + var renderer = new FrameImageRenderer(DecoderFactory.CreateServer()); + + EncodedImage Thumbnail(string name, int previewWidth, bool sizeKnown) + { + // 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 frame = new SidecarFrame + { + Id = Guid.CreateVersion7(), + File = "originals/" + name, + Format = ImageFileFormat.Dng, + W = 640, + H = 480, + PreviewOffset = dng.Length, + PreviewLength = preview.Length, + PreviewW = sizeKnown ? previewWidth : null, + PreviewH = sizeKnown ? previewWidth * 3 / 4 : null, + }; + return renderer.Thumbnail(sequence, frame)!; + } + + // A 200 px preview would leave the thumbnail short of 320 px (ENG-10): the RAW is developed instead. + var small = Thumbnail("small.dng", 200, sizeKnown: true); + 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)); + 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); + } + + [Test] + public void TheAdvisorCapsTheOutputAtThePreviewsOfPreviewOnlyFrames() + { + var sequence = Guid.CreateVersion7(); + Frame Row(int index, FrameFlags flags, int previewW, int previewH) => new() + { + Id = Guid.CreateVersion7(), + SequenceId = sequence, + Index = index, + RelativePath = $"s/{index}.cr3", + FileName = $"{index}.cr3", + CapturedAt = Start.AddSeconds(5 * index), + ExposureTime = 1.0 / 250, + FNumber = 8, + Iso = 100, + Width = 6000, + Height = 4000, + Orientation = 6, + Flags = flags, + PreviewWidth = previewW, + PreviewHeight = previewH, + }; + var rows = Enumerable.Range(0, 20) + .Select(i => Row(i, i == 19 ? FrameFlags.Undecodable : i < 15 ? FrameFlags.PreviewOnly : FrameFlags.None, i == 3 ? 1280 : 1620, i == 3 ? 960 : 1080)) + .ToList(); + + var context = SequenceContext.FromFrames(rows); + // Portrait frames: the previews are oriented like the main image; the most common preview size wins. + Assert.Equal((4000, 6000, 15, 1080, 1620), (context.SourceWidth, context.SourceHeight, context.PreviewOnlyCount, context.PreviewWidth, context.PreviewHeight)); + + var light = LightAnalyzer.Analyze(rows); + Assert.SequenceEqual(Enumerable.Range(0, 15), light.PreviewOnlyFrames); + Assert.SequenceEqual([19], light.UndecodableFrames); + var preview = light.Findings.Single(f => f.Key == FindingKeys.PreviewOnly); + Assert.Equal((FindingSeverity.Info, (object)15), (preview.Severity, preview.Arguments[0])); + Assert.Equal(FindingSeverity.Warning, light.Findings.Single(f => f.Key == DeepFindingKeys.UnreadableFrames).Severity); + + var result = Advisor.Recommend(light, null, context); + var resolution = result.Find(AdvisorParameters.Resolution)!; + Assert.Equal(AdvisorKeys.ResolutionPreview, resolution.ReasonKey); + Assert.SequenceEqual([15, 1080, 1620], resolution.ReasonArgs.Select(Convert.ToInt32)); + } + + [Test] + public async Task FramesBothAnalysesFoundUndecodableAreListedOnce() + { + var source = new SyntheticFrameSource(SyntheticSequence.Create(new SyntheticSequenceOptions { Width = 96, Height = 64, FrameCount = 12 })) + { + Undecodable = [4], + }; + 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. + var unflagged = Advisor.Recommend(LightAnalyzer.Analyze(metadata), deep, context).Findings.Where(f => f.Key == DeepFindingKeys.UnreadableFrames).ToList(); + Assert.SequenceEqual([4], unflagged.Single().Frames); + + // Once flagged, the light analysis lists it and the deep finding is not repeated. + 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()); + } +} diff --git a/tests/Titano.Core.Tests/Decoding/ManagedDecoderProviderTests.cs b/tests/Titano.Core.Tests/Decoding/ManagedDecoderProviderTests.cs index bcbc861e..a3edf6a7 100644 --- a/tests/Titano.Core.Tests/Decoding/ManagedDecoderProviderTests.cs +++ b/tests/Titano.Core.Tests/Decoding/ManagedDecoderProviderTests.cs @@ -5,6 +5,8 @@ using Titano.Core.Codecs.Tiff; using Titano.Core.Imaging; using Titano.Core.Imaging.Decoders; using Titano.Core.Metadata; +using Titano.Core.Metadata.Tiff; +using Titano.Core.Tests.Metadata.Builders; using Titano.Core.Tests.Raw; using Titano.Testing; using static Titano.Core.Tests.Raw.NativeRawWriters; @@ -201,6 +203,76 @@ public sealed class ManagedDecoderProviderTests(TestContext context) } } + [Test] + public void TheChainProbesEachFileForItsTier() + { + var server = DecoderFactory.CreateServer(); + // JPEG, PNG and TIFF decode natively whatever they hold: the probe does not even open them. + Assert.Equal(DecodeTier.Native, server.Probe(Path.Combine(_dir, "absent.jpg"), ImageFileFormat.Jpeg).Tier); + + var supported = server.Probe(Write("IMG_5.CR2", Cr2File("Canon EOS 5D Mark III")), ImageFileFormat.Cr2); + Assert.Equal((DecodeTier.Native, "managed-cr2"), (supported.Tier, supported.Decoder)); + + // An unsupported body decodes only through its 64×48 preview, rotated like the main image (orientation 6). + var preview = server.Probe(Write("IMG_6.CR2", Cr2File("Canon EOS R99", orientation: 6)), ImageFileFormat.Cr2); + Assert.Equal((DecodeTier.EmbeddedPreview, 48, 64), (preview.Tier, preview.Width, preview.Height)); + Assert.Contains("no colour matrix", preview.Reason); + + using var scene = SyntheticRaw.Smooth(W, H); + var nef = Nef(Sensor(scene, "Nikon D850", CfaPattern.Bayer("RGGB"), W, H, 0, 0, 600, 16383), 14, NefEncoding.Lossless); + var unknownBody = System.Text.Encoding.Latin1.GetBytes(System.Text.Encoding.Latin1.GetString(nef).Replace("NIKON D850", "NIKON Z99X")); + var none = server.Probe(Write("DSC_7.NEF", unknownBody), ImageFileFormat.Nef); + Assert.Equal(DecodeTier.Unavailable, none.Tier); + Assert.Contains("no embedded preview", none.Reason); + + // A Windows-like provider native for CR3 answers from its codec list, before the managed preview tier. + var client = DecoderFactory.CreateClient(new FakeProvider()); + var cr3 = client.Probe(Path.Combine(_dir, "absent.cr3"), ImageFileFormat.Cr3); + Assert.Equal((DecodeTier.Native, "fake"), (cr3.Tier, cr3.Decoder)); + Assert.Equal(DecodeTier.Unavailable, client.Probe(Write("y.webp", Fixtures.WebP()), ImageFileFormat.WebP).Tier); + } + + [Test] + public void GoProRawDecodesThroughTheJpegBesideIt() + { + var gpr = Write("GOPR0001.GPR", Gpr(previewWidth: 64, previewHeight: 48)); + var server = DecoderFactory.CreateServer(); + // Alone, a GPR (VC-5, out of scope) shows its embedded preview. + using (var alone = server.Decode(new DecodeRequest(gpr))) + Assert.Equal(("managed-preview", 64), (alone!.Decoder, alone.Pixels.Width)); + + Write("GOPR0001.JPG", JpegFile(160, 120, 1)); + using (var withJpeg = server.Decode(new DecodeRequest(gpr))) + { + Assert.Equal((DecodeTier.EmbeddedPreview, "managed-sidecar"), (withJpeg!.Tier, withJpeg.Decoder)); + Assert.Equal((160, 120, 160, 120), (withJpeg.Pixels.Width, withJpeg.Pixels.Height, withJpeg.SourceWidth, withJpeg.SourceHeight)); + } + using (var scaled = server.Decode(new DecodeRequest(gpr, MaxLongEdge: 40, Purpose: DecodePurpose.Thumbnail))) + Assert.Equal(40, scaled!.Pixels.Width); + var probe = new ManagedDecoderProvider().Probe(gpr); + Assert.Equal((DecodeTier.EmbeddedPreview, "managed-sidecar", 160, 120), (probe.Tier, probe.Decoder, probe.Width, probe.Height)); + + // A companion smaller than the embedded preview is not the better source. + var big = Write("GOPR0002.GPR", Gpr(previewWidth: 320, previewHeight: 240)); + Write("GOPR0002.JPG", JpegFile(160, 120, 1)); + using (var preview = server.Decode(new DecodeRequest(big))) + Assert.Equal(("managed-preview", 320), (preview!.Decoder, preview.Pixels.Width)); + Assert.Equal("managed-preview", new ManagedDecoderProvider().Probe(big).Decoder); + } + + /// A GoPro GPR as far as Titano reads it: a DNG made by "GoPro" whose only decodable image is a YCbCr JPEG preview. + private static byte[] Gpr(int previewWidth, int previewHeight) + { + var preview = new IfdSpec().Long(TiffTags.NewSubfileType, 1) + .Long(TiffTags.ImageWidth, (uint)previewWidth).Long(TiffTags.ImageLength, (uint)previewHeight) + .Short(TiffTags.BitsPerSample, 8, 8, 8).Short(TiffTags.Compression, 7) + .Short(TiffTags.PhotometricInterpretation, 6).Strip(JpegFile(previewWidth, previewHeight, 1)); + var ifd0 = Fixtures.Ifd0("GoPro", "HERO8 Black", 1) + .Bytes(TiffTags.DngVersion, 1, 4, 0, 0) + .Child(TiffTags.SubIfds, preview); + return TiffFile.Build(ifd0); + } + /// A Windows-like provider claiming native support for everything but declining every file. private sealed class FakeProvider : IImageDecoderProvider { diff --git a/tests/Titano.Data.Tests/Archiving/ArchiveJobTests.cs b/tests/Titano.Data.Tests/Archiving/ArchiveJobTests.cs index 91b954e3..e9131f19 100644 --- a/tests/Titano.Data.Tests/Archiving/ArchiveJobTests.cs +++ b/tests/Titano.Data.Tests/Archiving/ArchiveJobTests.cs @@ -88,6 +88,60 @@ public sealed class ArchiveJobTests(TestContext context) Assert.Contains("3 made, 3 reused", done.LogTail); } + [Test] + public async Task AThumbnailPassFlagsFramesNothingDecodesAndClearsThemOnceTheyDecode() + { + await using var fx = await CreateAsync(context); + var files = Photos.WriteSeries(Path.Combine(fx.Paths.Root, "a"), Start, 6); + await fx.ScanAsync(); + var a = (await LocationsAsync(fx))[0]; + var sequence = (await fx.Db.Sequences.GetAsync(a.Id))!; + var paths = fx.Paths.Sequence(a.Folder); + var original = await File.ReadAllBytesAsync(files[2], fx.Ct); + await File.WriteAllBytesAsync(files[2], new byte[original.Length], fx.Ct); + + var result = await fx.Service.Images.BuildThumbnailsAsync(fx.Archive, sequence, false, 0, (_, _) => ValueTask.CompletedTask, fx.Ct); + + Assert.Equal((5, 1), (result.Built, result.Undecodable)); + var frame = ManifestStore.ReadSequence(paths.ManifestPath).Frames[2]; + Assert.Equal((Titano.Core.Imaging.DecodeTier.Unavailable, FrameFlags.Undecodable), (frame.Tier, frame.Flags)); + Assert.Equal(FrameFlags.Undecodable, (await fx.Db.Frames.ListBySequenceAsync(a.Id))[2].Flags); + Assert.True((await fx.Db.Sequences.GetAsync(a.Id))!.Status.HasFlag(SequenceStatus.HasWarnings), "the sequence warns at once"); + + // Restored from a copy: the next pass decodes the frame and clears the flag; the others are reused. + await File.WriteAllBytesAsync(files[2], original, fx.Ct); + result = await fx.Service.Images.BuildThumbnailsAsync(fx.Archive, sequence, false, 0, (_, _) => ValueTask.CompletedTask, fx.Ct); + Assert.Equal((1, 5, 0), (result.Built, result.Reused, result.Undecodable)); + frame = ManifestStore.ReadSequence(paths.ManifestPath).Frames[2]; + Assert.Equal((Titano.Core.Imaging.DecodeTier.Native, FrameFlags.None), (frame.Tier, frame.Flags)); + Assert.Equal(FrameFlags.None, (await fx.Db.Frames.ListBySequenceAsync(a.Id))[2].Flags); + } + + [Test] + public async Task AHostWithoutADecoderForTheFormatRecordsTheTierWithoutFlaggingTheFrames() + { + // The fixture's decoder chain is empty: only JPEG decodes here (its decoder is built in), PNG does not. + await using var fx = await CreateAsync(context); + var folder = Path.Combine(fx.Paths.Root, "p"); + Directory.CreateDirectory(folder); + using (var image = new Titano.Core.Imaging.ImageBuffer(32, 24)) + { + for (var i = 0; i < 3; i++) File.WriteAllBytes(Path.Combine(folder, $"P{i}.png"), Titano.Core.Codecs.Png.PngEncoder.Encode(image)); + } + await fx.ScanAsync(); + var p = (await LocationsAsync(fx)).Single(); + + var result = await fx.Service.Images.BuildThumbnailsAsync(fx.Archive, (await fx.Db.Sequences.GetAsync(p.Id))!, false, 0, + (_, _) => ValueTask.CompletedTask, fx.Ct); + + // Another host (or a proxy) may still provide the pixels: the tier is recorded, the frames stay renderable. + Assert.Equal(3, result.Undecodable); + var frames = ManifestStore.ReadSequence(fx.Paths.Sequence(p.Folder).ManifestPath).Frames; + Assert.True(frames.All(f => f.Tier == Titano.Core.Imaging.DecodeTier.Unavailable && f.Flags == FrameFlags.None), "tier without flag"); + Assert.True((await fx.Db.Frames.ListBySequenceAsync(p.Id)).All(f => f.Flags == FrameFlags.None), "index flags"); + Assert.False((await fx.Db.Sequences.GetAsync(p.Id))!.Status.HasFlag(SequenceStatus.HasWarnings), "no warning"); + } + [Test] public async Task AMaintenanceJobResumesAfterItsDoneSteps() { diff --git a/tests/Titano.Data.Tests/Processing/AnalysisJobTests.cs b/tests/Titano.Data.Tests/Processing/AnalysisJobTests.cs index 8fb31ae9..c49fce15 100644 --- a/tests/Titano.Data.Tests/Processing/AnalysisJobTests.cs +++ b/tests/Titano.Data.Tests/Processing/AnalysisJobTests.cs @@ -1,6 +1,9 @@ +using Titano.Core.Advice; +using Titano.Core.Analysis; using Titano.Core.Analysis.Deep; using Titano.Core.Archiving; using Titano.Core.Diagnostics.Synthetic; +using Titano.Core.Imaging; using Titano.Core.Localization; using Titano.Core.Model; using Titano.Core.Project; @@ -62,6 +65,75 @@ public sealed class AnalysisJobTests(TestContext context) Assert.Equal(version, (await fx.Db.Sequences.GetAsync(sequence.Id, fx.Ct))!.SettingsVersion); } + [Test] + public async Task LightAnalysisRecordsFormatTiersAndTheAdvisorCapsTheOutputAtThePreviews() + { + await using var fx = await ProcessingFixture.CreateAsync(context); + var sequence = await fx.AddAsync(Flickering(12)); + // This host reads the first ten files only through 48×32 embedded previews and cannot decode the last one. + for (var i = 0; i < 10; i++) + fx.Decoder.Tiers[ProcessingFixture.FileName(i)] = new FileTier(ImageFileFormat.Jpeg, DecodeTier.EmbeddedPreview, "test-preview", "test", 48, 32); + fx.Decoder.Tiers[ProcessingFixture.FileName(11)] = new FileTier(ImageFileFormat.Jpeg, DecodeTier.Unavailable, null, "test", 0, 0); + await fx.StartEngineAsync(); + + var job = await fx.EnqueueAsync(ProcessingJobs.LightAnalysis([sequence.Id])); + await fx.WaitForAsync(job.Id, JobState.Done); + + var folder = fx.Folder(sequence); + var sidecar = ManifestStore.ReadSequence(folder.ManifestPath).Frames; + Assert.Equal((DecodeTier.EmbeddedPreview, FrameFlags.PreviewOnly, (int?)48, (int?)32), (sidecar[0].Tier, sidecar[0].Flags, sidecar[0].PreviewW, sidecar[0].PreviewH)); + Assert.Equal((DecodeTier.Native, FrameFlags.None), (sidecar[10].Tier, sidecar[10].Flags)); + Assert.Equal((DecodeTier.Unavailable, FrameFlags.Undecodable), (sidecar[11].Tier, sidecar[11].Flags)); + var rows = await fx.Db.Frames.ListBySequenceAsync(sequence.Id, cancellationToken: fx.Ct); + Assert.Equal((FrameFlags.PreviewOnly, 48, 32), (rows[3].Flags, rows[3].PreviewWidth, rows[3].PreviewHeight)); + Assert.Equal(FrameFlags.Undecodable, rows[11].Flags); + var row = (await fx.Db.Sequences.GetAsync(sequence.Id, fx.Ct))!; + Assert.Equal(10, row.PreviewOnlyCount); + Assert.GreaterOrEqual(row.WarningCount, 1); + Assert.True(row.Status.HasFlag(SequenceStatus.HasWarnings), "the undecodable frame is a sequence warning"); + + // The advisor reads the preview size from the index and caps the output there (BRIEF §8.2 tier 2). + var tiers = SequenceContext.FromFrames(rows); + Assert.Equal((10, 48, 32), (tiers.PreviewOnlyCount, tiers.PreviewWidth, tiers.PreviewHeight)); + var light = LightAnalyzer.Analyze(rows); + var advice = Advisor.Recommend(light, null, tiers); + Assert.Equal(AdvisorKeys.ResolutionPreview, advice.Find(AdvisorParameters.Resolution)!.ReasonKey); + Assert.Contains(advice.Findings, f => f.Key == FindingKeys.PreviewOnly && Equals(f.Arguments[0], 10)); + Assert.Contains(advice.Findings, f => f.Key == DeepFindingKeys.UnreadableFrames && f.Frames.SequenceEqual([11])); + + // Probed once: a second pass opens no file for its tier and leaves the sidecar alone. + fx.Decoder.Tiers.Clear(); + var written = File.GetLastWriteTimeUtc(folder.ManifestPath); + var again = await fx.EnqueueAsync(ProcessingJobs.LightAnalysis([sequence.Id])); + await fx.WaitForAsync(again.Id, JobState.Done); + Assert.Equal(FrameFlags.PreviewOnly, ManifestStore.ReadSequence(folder.ManifestPath).Frames[0].Flags); + Assert.Equal(written, File.GetLastWriteTimeUtc(folder.ManifestPath)); + } + + [Test] + public async Task DeepAnalysisFlagsFramesItCannotDecodeAndListsThemOnce() + { + await using var fx = await ProcessingFixture.CreateAsync(context); + var sequence = await fx.AddAsync(Flickering(12)); + // Damaged after indexing: the header probe still says JPEG, only decoding finds out. + await File.WriteAllBytesAsync(Path.Combine(fx.Folder(sequence).Folder, ProcessingFixture.FileName(4)), new byte[3000], fx.Ct); + await fx.StartEngineAsync(); + + var job = await fx.EnqueueAsync(ProcessingJobs.DeepAnalysis([sequence.Id])); + var done = await fx.WaitForAsync(job.Id, JobState.Done); + + var sidecar = ManifestStore.ReadSequence(fx.Folder(sequence).ManifestPath).Frames; + Assert.Equal((DecodeTier.Unavailable, FrameFlags.Undecodable), (sidecar[4].Tier, sidecar[4].Flags)); + Assert.Equal(DecodeTier.Native, sidecar[5].Tier); + Assert.Equal(FrameFlags.Undecodable, (await fx.Db.Frames.ListBySequenceAsync(sequence.Id, cancellationToken: fx.Ct))[4].Flags); + var report = SequenceAnalyser.ReadReport(fx.Folder(sequence), sequence.FrameCount)!; + var rows = await fx.Db.Frames.ListBySequenceAsync(sequence.Id, cancellationToken: fx.Ct); + var findings = Advisor.Recommend(LightAnalyzer.Analyze(rows), report, SequenceContext.FromFrames(rows)).Findings; + Assert.Count(1, findings.Where(f => f.Key == DeepFindingKeys.UnreadableFrames)); + Assert.True((await fx.Db.Sequences.GetAsync(sequence.Id, fx.Ct))!.WarningCount >= 1, "listed in the sequence warnings"); + Assert.Equal(JobState.Done, done.State); + } + [Test] public async Task DeepAnalysisResumesAfterCancellationWithTheSameReport() { diff --git a/tests/Titano.Data.Tests/Processing/ProcessingFixture.cs b/tests/Titano.Data.Tests/Processing/ProcessingFixture.cs index d5b9ad90..8853d362 100644 --- a/tests/Titano.Data.Tests/Processing/ProcessingFixture.cs +++ b/tests/Titano.Data.Tests/Processing/ProcessingFixture.cs @@ -123,8 +123,15 @@ internal sealed class GateDecoder : IImageDecoderProvider Hold = ProcessingFixture.FileName(index); } + /// Tiers reports per file name instead of the managed provider's (a host that reads them + /// only as previews, or not at all). + public ConcurrentDictionary 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); diff --git a/tests/Titano.Data.Tests/Processing/RenderJobTests.cs b/tests/Titano.Data.Tests/Processing/RenderJobTests.cs index 656032ef..c22757f4 100644 --- a/tests/Titano.Data.Tests/Processing/RenderJobTests.cs +++ b/tests/Titano.Data.Tests/Processing/RenderJobTests.cs @@ -86,18 +86,48 @@ public sealed class RenderJobTests(TestContext context) { await using var fx = await ProcessingFixture.CreateAsync(context); var sequence = await fx.AddAsync(Scene()); - // Damaged after indexing: nothing flags the frame, so only decoding finds out. - await File.WriteAllBytesAsync(Path.Combine(fx.Archive.Paths.Root, "Synthetic", ProcessingFixture.FileName(10)), new byte[5000], fx.Ct); + // Every file damaged after indexing: nothing flags them, and no frame can stand in for another. + for (var i = 0; i < Frames; i++) + await File.WriteAllBytesAsync(Path.Combine(fx.Archive.Paths.Root, "Synthetic", ProcessingFixture.FileName(i)), new byte[5000], fx.Ct); await fx.StartEngineAsync(); var job = await fx.EnqueueAsync(ProcessingJobs.Render(sequence, Mjpeg, 96, 64)); var failed = await fx.WaitForAsync(job.Id, JobState.Failed); - Assert.Equal(Loc.L(SequenceFrameSource.FrameUnreadableKey, "11", ProcessingFixture.FileName(10)), failed.Error); + Assert.Equal(Loc.L(SequenceFrameSource.FrameUnreadableKey, "1", ProcessingFixture.FileName(0)), failed.Error); Assert.Empty(Entries(fx.Folder(sequence).Exports)); Assert.Empty(await fx.Db.Renders.ListBySequenceAsync(sequence.Id, fx.Ct)); } + [Test] + public async Task AFrameThatNoLongerDecodesIsReplacedByItsNeighbourAndLeftOutNextTime() + { + await using var fx = await ProcessingFixture.CreateAsync(context); + var sequence = await fx.AddAsync(Scene()); + // Damaged after indexing: nothing flags the frame, so only decoding finds out (BRIEF §8.2 tier 3). + await File.WriteAllBytesAsync(Path.Combine(fx.Archive.Paths.Root, "Synthetic", ProcessingFixture.FileName(10)), new byte[5000], fx.Ct); + 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); + Assert.SequenceEqual(["clip-MJPEG.avi"], Entries(fx.Folder(sequence).Exports)); + var row = (await fx.Db.Frames.ListBySequenceAsync(sequence.Id, cancellationToken: fx.Ct))[10]; + Assert.Equal(FrameFlags.Undecodable, row.Flags); + var sidecar = Titano.Core.Archiving.ManifestStore.ReadSequence(fx.Folder(sequence).ManifestPath); + Assert.Equal((DecodeTier.Unavailable, FrameFlags.Undecodable), (sidecar.Frames[10].Tier, sidecar.Frames[10].Flags)); + Assert.Equal(DecodeTier.Native, sidecar.Frames[9].Tier); + Assert.True(sidecar.Status.HasFlag(SequenceStatus.HasWarnings), "the sequence warns at once"); + Assert.True((await fx.Db.Sequences.GetAsync(sequence.Id, fx.Ct))!.Status.HasFlag(SequenceStatus.HasWarnings), "index status"); + + // The next render plans without the frame, so nothing has to stand in for it. + var again = await fx.EnqueueAsync(ProcessingJobs.Render(sequence, Mjpeg, 96, 64)); + var second = await fx.WaitForAsync(again.Id, JobState.Done); + Assert.False(second.LogTail?.Contains(Loc.L("Export.Log.StandIns", "11"), StringComparison.Ordinal) == true, "no stand-in"); + Assert.Count(2, await fx.Db.Renders.ListBySequenceAsync(sequence.Id, fx.Ct)); + } + [Test] public async Task ARenderInterruptedByACrashStartsItsOutputAgainCleanly() { diff --git a/tests/Titano.Data.Tests/Repositories/CatalogRepositoryTests.cs b/tests/Titano.Data.Tests/Repositories/CatalogRepositoryTests.cs index 968fbcd7..0c09f672 100644 --- a/tests/Titano.Data.Tests/Repositories/CatalogRepositoryTests.cs +++ b/tests/Titano.Data.Tests/Repositories/CatalogRepositoryTests.cs @@ -51,6 +51,7 @@ public abstract class CatalogRepositoryTests(TestContext context) : RepositoryTe FramesVersion = 7, OwnerUserId = Guid.CreateVersion7(), WarningCount = 2, + PreviewOnlyCount = 1150, Tags = ["Sunset", "harbour", "SUNSET"], }; await db.Sequences.UpsertAsync(sequence, Ct); @@ -248,6 +249,8 @@ public abstract class CatalogRepositoryTests(TestContext context) : RepositoryTe Orientation = 6, PreviewOffset = 1024, PreviewLength = 65536, + PreviewWidth = 1620, + PreviewHeight = 1080, MeanLogLuminance = -2.5, ClipLow = 0.01, ClipHigh = 0.02, @@ -307,6 +310,27 @@ public abstract class CatalogRepositoryTests(TestContext context) : RepositoryTe Assert.Equal(1200, await db.Frames.DeleteBySequenceAsync(sequence.Id, Ct)); } + [Test] + public async Task FrameTiersUpdateInBulkAndSurviveASyncFromTheSidecar() + { + var db = await OpenTitanoAsync(); + var archive = TestData.Archive(); + 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(); + 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.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))); + } + [Test] public async Task RendersKeepTheSequenceCountInStep() { From 366dd9f303c4c7cc658fe3d3ebce74d4ca8ca074 Mon Sep 17 00:00:00 2001 From: Claude Date: Mon, 12 Oct 2026 11:17:38 +0000 Subject: [PATCH 5/7] feat(advice): time intervalometer sequences adaptively and list unreadable frames once MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit The MakerNote interval-shooting hint (Panasonic TimerRecording) is kept as FrameFlags.Intervalometer from indexing on, so it reaches the sidecar, the index, sync and the protocol. When most frames carry it, gaps in the cadence are the intervalometer's deliberate pauses and the advisor picks adaptive timing (Advisor.Reason.Timing.Intervalometer, BRIEF §10.3). Frames both analyses find undecodable are now one finding over the union of both lists at the higher severity: the deep pass also lists missing, corrupt and beyond-this-host frames, which the light analysis never flags. Co-Authored-By: Claude Opus 5.5 Claude-Session: https://claude.ai/code/session_012iLMBNwBE2GnphAsu4cjaC --- i18n/en/Advisor.resx | 1 + i18n/it/Advisor.it.resx | 1 + src/Titano.Core/Advice/Advisor.cs | 25 +++++++++++++++---- src/Titano.Core/Advice/AdvisorKeys.cs | 1 + src/Titano.Core/Advice/AdvisorRules.cs | 17 +++++++++---- .../Analysis/LightAnalysisReport.cs | 3 +++ src/Titano.Core/Analysis/LightAnalyzer.cs | 1 + src/Titano.Core/Archiving/SequenceFrames.cs | 3 ++- src/Titano.Core/Model/Enums.cs | 6 +++++ 9 files changed, 47 insertions(+), 11 deletions(-) diff --git a/i18n/en/Advisor.resx b/i18n/en/Advisor.resx index c76505d4..27db3d3f 100644 --- a/i18n/en/Advisor.resx +++ b/i18n/en/Advisor.resx @@ -97,6 +97,7 @@ Smoothing the camera path over {0} frames removes shake and keeps deliberate pans.{0} smoothing sigma in frames Strong flicker ({0} EV): every frame lands on the smooth curve.{0} RMS flicker in EV Mild flicker ({0} EV): reliable frames keep 20% of their own variation.{0} RMS flicker in EV + The camera's intervalometer shot this sequence, so its pauses were deliberate (gaps: {0}): adaptive timing keeps them short and the pace even.{0} gap count Intervals vary by {0} s around {1} s: adaptive timing follows the capture clock so motion stays even.{0} interval jitter in seconds; {1} median interval in seconds {0} gaps in the cadence: adaptive timing keeps the output pace true to capture time.{0} gap count Steady {0} s interval (jitter {1} s): each photo becomes one video frame.{0} median interval in seconds; {1} jitter in seconds diff --git a/i18n/it/Advisor.it.resx b/i18n/it/Advisor.it.resx index c199c431..88a10f18 100644 --- a/i18n/it/Advisor.it.resx +++ b/i18n/it/Advisor.it.resx @@ -97,6 +97,7 @@ Smussare il percorso della fotocamera su {0} fotogrammi elimina la vibrazione e conserva le panoramiche volute. Sfarfallio forte ({0} EV): ogni fotogramma viene portato sulla curva regolare. Sfarfallio lieve ({0} EV): i fotogrammi affidabili conservano il 20% della propria variazione. + La sequenza è stata scattata dall'intervallometro della fotocamera, quindi le pause sono volute (pause: {0}): la temporizzazione adattiva le accorcia e mantiene il ritmo. Gli intervalli variano di {0} s attorno a {1} s: la temporizzazione adattiva segue l'orario di scatto e rende il movimento uniforme. {0} pause nella cadenza: la temporizzazione adattiva mantiene il ritmo fedele all'orario di scatto. Intervallo costante di {0} s (variazione {1} s): ogni foto diventa un fotogramma del video. diff --git a/src/Titano.Core/Advice/Advisor.cs b/src/Titano.Core/Advice/Advisor.cs index 32217eb1..2f447b58 100644 --- a/src/Titano.Core/Advice/Advisor.cs +++ b/src/Titano.Core/Advice/Advisor.cs @@ -16,17 +16,32 @@ public static class Advisor { /// /// Recommendations for every parameter the rules cover (see ), in section order, plus - /// every finding of both analyses; a finding both report (frames that cannot be decoded: the light analysis reads - /// the flags the deep pass recorded) is listed once, as the light analysis has it. Without a deep report, - /// pixel-based parameters get missing-data recommendations. + /// every finding of both analyses. Frames that cannot be decoded are reported by both (the light analysis reads the + /// undecodable flags, the deep pass also counts missing and corrupt files and those beyond this host's decoders): + /// they are listed once, over the union of both lists, at the higher severity. Without a deep report, pixel-based + /// parameters get missing-data recommendations. /// public static AdvisorResult Recommend(LightAnalysisReport light, DeepAnalysisReport? deep, SequenceContext context) { ArgumentNullException.ThrowIfNull(light); ArgumentNullException.ThrowIfNull(context); var findings = new List(light.Findings); - var lightKeys = light.Findings.Select(f => f.Key).ToHashSet(StringComparer.Ordinal); - if (deep is not null) findings.AddRange(deep.Findings.Select(f => f.ToFinding()).Where(f => !lightKeys.Contains(f.Key))); + foreach (var finding in deep?.Findings.Select(f => f.ToFinding()) ?? []) + { + var shared = finding.Key == DeepFindingKeys.UnreadableFrames ? findings.FindIndex(f => f.Key == finding.Key) : -1; + if (shared < 0) + { + findings.Add(finding); + continue; + } + var unreadable = light.UndecodableFrames.Union(deep!.Issues.Undecodable).Order().ToList(); + findings[shared] = findings[shared] with + { + Severity = (FindingSeverity)int.Max((int)findings[shared].Severity, (int)finding.Severity), + Arguments = [unreadable.Count], + Frames = [.. unreadable.Take(Finding.MaxFrames)], + }; + } return new AdvisorResult(new AdvisorRules(light, deep, context).Run(), findings); } diff --git a/src/Titano.Core/Advice/AdvisorKeys.cs b/src/Titano.Core/Advice/AdvisorKeys.cs index 305a8bd5..8c3c89d8 100644 --- a/src/Titano.Core/Advice/AdvisorKeys.cs +++ b/src/Titano.Core/Advice/AdvisorKeys.cs @@ -19,6 +19,7 @@ public static class AdvisorKeys public static string TimingSessions { get; } = LocKey("Advisor.Reason.Timing.Sessions"); public static string TimingIrregular { get; } = LocKey("Advisor.Reason.Timing.Irregular"); + public static string TimingIntervalometer { get; } = LocKey("Advisor.Reason.Timing.Intervalometer"); public static string TimingSparseGapsSmooth { get; } = LocKey("Advisor.Reason.Timing.SparseGapsSmooth"); public static string TimingSparseGaps { get; } = LocKey("Advisor.Reason.Timing.SparseGaps"); public static string TimingManyGaps { get; } = LocKey("Advisor.Reason.Timing.ManyGaps"); diff --git a/src/Titano.Core/Advice/AdvisorRules.cs b/src/Titano.Core/Advice/AdvisorRules.cs index 00cef188..4a34dcbe 100644 --- a/src/Titano.Core/Advice/AdvisorRules.cs +++ b/src/Titano.Core/Advice/AdvisorRules.cs @@ -185,11 +185,13 @@ internal sealed class AdvisorRules /// /// Timing mode, in order: several capture sessions (pauses ≥ the session gap) → Adaptive; jitter above 10 % of the - /// median interval → Adaptive; gaps that are sparse (≤ 2 % of intervals, each ≤ 10 × median) with smooth measured - /// motion (95th percentile ≤ 1 % of the long edge per frame) → Interpolated, or Constant when motion is unknown or - /// fast; denser gaps → Adaptive; otherwise Constant. Frame rate, in order: "fit to N s" target → the standard rate - /// nearest frames/N; under 240 frames → 24 (clip ≥ 10 s); over 10 min at 30 fps → 60; several sessions → 30; fast - /// motion (≥ 1 % of the long edge per frame) → 24; measured moderate motion (≥ 0.2 %) → 24; otherwise 30. + /// median interval → Adaptive; gaps in a sequence the camera's own intervalometer shot (most frames carry its + /// MakerNote hint), so its pauses were deliberate → Adaptive; gaps that are sparse (≤ 2 % of intervals, each ≤ 10 × + /// median) with smooth measured motion (95th percentile ≤ 1 % of the long edge per frame) → Interpolated, or Constant + /// when motion is unknown or fast; denser gaps → Adaptive; otherwise Constant. Frame rate, in order: "fit to N s" + /// target → the standard rate nearest frames/N; under 240 frames → 24 (clip ≥ 10 s); over 10 min at 30 fps → 60; + /// several sessions → 30; fast motion (≥ 1 % of the long edge per frame) → 24; measured moderate motion (≥ 0.2 %) → + /// 24; otherwise 30. /// private void Timing() { @@ -222,6 +224,11 @@ internal sealed class AdvisorRules _timing = TimingMode.Adaptive; Add(section, parameter, _timing, 0.7, AdvisorKeys.TimingIrregular, R(jitter, 2), R(median, 2)); } + else if (gaps.Count > 0 && 2 * _light.IntervalometerFrames > _light.FrameCount) + { + _timing = TimingMode.Adaptive; + Add(section, parameter, _timing, 0.8, AdvisorKeys.TimingIntervalometer, gaps.Count); + } else if (gaps.Count > 0) { var sparse = gaps.Count <= double.Max(1, 0.02 * _light.Intervals.Count) && gaps.All(g => g.Seconds <= 10 * median); diff --git a/src/Titano.Core/Analysis/LightAnalysisReport.cs b/src/Titano.Core/Analysis/LightAnalysisReport.cs index 9e7fe647..fedd5360 100644 --- a/src/Titano.Core/Analysis/LightAnalysisReport.cs +++ b/src/Titano.Core/Analysis/LightAnalysisReport.cs @@ -137,5 +137,8 @@ public sealed record LightAnalysisReport /// Frames that nothing decodes (format tier 3): renders skip them. public IReadOnlyList UndecodableFrames { get; init; } = []; + /// Frames the camera's own interval-shooting mode took (MakerNote hint, ). + public int IntervalometerFrames { get; init; } + public IReadOnlyList Findings { get; init; } = []; } diff --git a/src/Titano.Core/Analysis/LightAnalyzer.cs b/src/Titano.Core/Analysis/LightAnalyzer.cs index aa1f0c1a..be8685d8 100644 --- a/src/Titano.Core/Analysis/LightAnalyzer.cs +++ b/src/Titano.Core/Analysis/LightAnalyzer.cs @@ -61,6 +61,7 @@ public static class LightAnalyzer DayPhases = phases, PreviewOnlyFrames = previewOnly, UndecodableFrames = undecodable, + IntervalometerFrames = frames.Count(f => (f.Flags & FrameFlags.Intervalometer) != 0), Findings = findings, }; } diff --git a/src/Titano.Core/Archiving/SequenceFrames.cs b/src/Titano.Core/Archiving/SequenceFrames.cs index 8f79a8de..5f72d16b 100644 --- a/src/Titano.Core/Archiving/SequenceFrames.cs +++ b/src/Titano.Core/Archiving/SequenceFrames.cs @@ -14,7 +14,7 @@ public static class SequenceFrames return byTime != 0 ? byTime : string.CompareOrdinal(a.File, b.File); }); - /// A frame record from light metadata. + /// A frame record from light metadata (with when the MakerNote says so). /// Path relative to the sequence folder. /// Used when the file records no capture time of its own (only a file-system date). public static SidecarFrame FromMetadata(Guid id, string file, string? original, long size, string? sha256, FrameMetadata metadata, @@ -45,6 +45,7 @@ public static class SequenceFrames PreviewLength = metadata.LargestPreview?.Length, PreviewW = Positive(metadata.LargestPreview?.Width), PreviewH = Positive(metadata.LargestPreview?.Height), + Flags = metadata.IntervalShooting == true ? FrameFlags.Intervalometer : FrameFlags.None, }; } diff --git a/src/Titano.Core/Model/Enums.cs b/src/Titano.Core/Model/Enums.cs index 2bb39f50..d6dcfd3c 100644 --- a/src/Titano.Core/Model/Enums.cs +++ b/src/Titano.Core/Model/Enums.cs @@ -24,6 +24,12 @@ public enum FrameFlags Undecodable = 32, Black = 64, White = 128, + + /// + /// The camera's own interval-shooting or time-lapse mode took the frame (MakerNote hint, BRIEF §9): a capture fact, + /// not a problem; its pauses were deliberate (§10.3 adaptive timing). + /// + Intervalometer = 256, } [Flags] From 5599e4de894cd3bad2d86768d6225b0ca60cc408 Mon Sep 17 00:00:00 2001 From: Claude Date: Mon, 12 Oct 2026 11:17:49 +0000 Subject: [PATCH 6/7] fix(imaging): record only what decoding proves and find GPR JPEGs across sequences - Preview sizes come from probes and unscaled decodes: a size-limited decode picks the smallest preview that reaches its edge (CR3 PRVW 1620x1080), so renders and deep passes record its tier without a size and the output cap keeps the largest preview's. - A read error is not an undecodable file: ImageDecoder.Decode throws the IOException when a provider could not read the file and none produced pixels. Deep passes never flag frames they could not read or that vanished, and frames only an interrupted earlier run found undecodable are decoded again before they are flagged. - Renders resolve stand-ins only for frames the pipeline asks for; frames the read-ahead meets on the way (skipped by speed-ups) are left alone. - A GoPro GPR finds its JPEG in the index when an import or upload kept it in a sequence of its own (same original stem and capture time), for probes, deep analysis, renders, thumbnails, proxies and the editor; GPRs whose JPEG is indexed later are probed again. - Thumbnails judge an embedded preview by its own JPEG header. - PostgreSQL writes tier fields 1000 frames per statement. Co-Authored-By: Claude Opus 5.5 Claude-Session: https://claude.ai/code/session_012iLMBNwBE2GnphAsu4cjaC --- docs/ARCHITECTURE.md | 2 +- src/Titano.App/Editor/LocalSequenceSource.cs | 9 +- .../Archiving/FrameImageRenderer.cs | 30 ++-- .../Decoders/ManagedDecoderProvider.cs | 47 +++--- src/Titano.Core/Imaging/Decoding.cs | 29 +++- src/Titano.Data/Archiving/ArchiveImages.cs | 18 ++- src/Titano.Data/Archiving/FrameTiers.cs | 19 ++- src/Titano.Data/Archiving/GprCompanions.cs | 63 ++++++++ .../Processing/Jobs/RenderJobHandler.cs | 4 +- .../Processing/SequenceAnalyser.cs | 47 ++++-- .../Processing/SequenceFrameSource.cs | 96 ++++++++---- src/Titano.Data/Processing/SequenceRender.cs | 5 +- .../Repositories/FrameRepository.cs | 40 ++++- .../Archiving/DecodeTierTests.cs | 61 ++++++-- .../Decoding/ManagedDecoderProviderTests.cs | 54 +++++++ .../Processing/AnalysisJobTests.cs | 137 ++++++++++++++++++ .../Processing/ProcessingFixture.cs | 30 +++- .../Processing/RenderJobTests.cs | 99 +++++++++++++ .../Repositories/CatalogRepositoryTests.cs | 10 +- 19 files changed, 689 insertions(+), 111 deletions(-) create mode 100644 src/Titano.Data/Archiving/GprCompanions.cs diff --git a/docs/ARCHITECTURE.md b/docs/ARCHITECTURE.md index b22dacd6..08085490 100644 --- a/docs/ARCHITECTURE.md +++ b/docs/ARCHITECTURE.md @@ -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`) | diff --git a/src/Titano.App/Editor/LocalSequenceSource.cs b/src/Titano.App/Editor/LocalSequenceSource.cs index 6395d6be..cc6e98dc 100644 --- a/src/Titano.App/Editor/LocalSequenceSource.cs +++ b/src/Titano.App/Editor/LocalSequenceSource.cs @@ -37,6 +37,9 @@ public sealed class LocalSequenceSource : ISequenceSource private readonly JobEngine? _engine; private readonly ArchiveServiceOptions _lease; private string?[] _files = []; + + /// The JPEG of each GPR frame that an import keeps in another sequence (), by position. + 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 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) diff --git a/src/Titano.Core/Archiving/FrameImageRenderer.cs b/src/Titano.Core/Archiving/FrameImageRenderer.cs index 5e37bc61..fd91120d 100644 --- a/src/Titano.Core/Archiving/FrameImageRenderer.cs +++ b/src/Titano.Core/Archiving/FrameImageRenderer.cs @@ -18,19 +18,22 @@ public sealed class FrameImageRenderer(ImageDecoder decoder) public const int ThumbnailQuality = 80; /// A 320 px q80 thumbnail, or null when no decoder can read the frame. - public EncodedImage? Thumbnail(SequencePaths sequence, SidecarFrame frame, CancellationToken cancellationToken = default) + /// Where a GPR's JPEG is kept when it is not beside it (). + 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); } /// A proxy at the policy's long edge and quality with capture time and camera in its Exif; null when undecodable. - public EncodedImage? Proxy(SequencePaths sequence, SidecarFrame frame, SidecarCamera? camera, ProxyPolicy policy, CancellationToken cancellationToken = default) + /// Where a GPR's JPEG is kept when it is not beside it (). + 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) /// /// The frame's pixels: a JPEG through the managed decoder, the stored embedded preview when /// and it reaches (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 ), and the stored preview as the last resort. Null + /// when nothing decodes. /// - private DecodedFrame? Decode(string path, SidecarFrame frame, int maxLongEdge, DecodePurpose purpose, bool preferPreview, CancellationToken cancellationToken) + /// The file could not be read. + 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; } - /// Whether the stored preview's long edge reaches (the sidecar's size, else the JPEG's own). - private static bool Reaches(byte[] preview, SidecarFrame frame, int maxLongEdge) + /// + /// Whether the stored preview's long edge reaches , 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. + /// + 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); diff --git a/src/Titano.Core/Imaging/Decoders/ManagedDecoderProvider.cs b/src/Titano.Core/Imaging/Decoders/ManagedDecoderProvider.cs index 4fa99fea..568ccf8f 100644 --- a/src/Titano.Core/Imaging/Decoders/ManagedDecoderProvider.cs +++ b/src/Titano.Core/Imaging/Decoders/ManagedDecoderProvider.cs @@ -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 ; 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 says an import put it) when that is larger +/// (BRIEF §8.1; VC-5 is out of scope); nothing for formats without either. /// fast paths: JPEG DCT scaling and RAW CFA binning when /// allows, bilinear demosaicing for thumbnails and analysis, and 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. /// - public FileTier Probe(string path, ImageFileFormat format) + public FileTier Probe(string path, ImageFileFormat format) => Probe(path, format, null); + + /// + /// Where a GPR's JPEG is kept when it is not beside it (). + 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); } /// The tier this provider would use for the file at , without decoding pixels. - public FileTier Probe(string path) + /// Where a GPR's JPEG is kept when it is not beside it (). + 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); } /// - /// The JPEG a GoPro writes beside a GPR (same name, .JPG), 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: 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, .JPG), or + /// null. Only the usual spellings are tried, so a folder of thousands of frames is never listed once per frame. /// - 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; } + /// Oriented size of the GPR's JPEG (see ), or null when there is none. + /// The JPEG cannot be read. + 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 } /// - /// 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 (, 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. /// 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); diff --git a/src/Titano.Core/Imaging/Decoding.cs b/src/Titano.Core/Imaging/Decoding.cs index 06a4c2d6..108831ae 100644 --- a/src/Titano.Core/Imaging/Decoding.cs +++ b/src/Titano.Core/Imaging/Decoding.cs @@ -1,3 +1,5 @@ +using System.Runtime.ExceptionServices; + namespace Titano.Core.Imaging; /// How a frame's pixels were obtained (BRIEF §8.2). Logged per frame. @@ -28,13 +30,16 @@ public enum DecodePurpose /// Rotate/flip according to the Exif orientation. /// Accept the embedded-preview tier when native decode is impossible. /// Use the embedded preview even when native decode is possible (fast paths). +/// 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. 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); /// Decoded pixels plus how they were produced. Dispose to return the buffer to the pool. 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); + /// + /// for a file whose camera companion (see + /// ) is kept at ; a provider that uses no + /// companion ignores it. + /// + FileTier Probe(string path, ImageFileFormat format, string? companion) => Probe(path, format); + /// Decodes or returns null when this provider cannot handle the specific file. DecodedImage? Decode(DecodeRequest request, CancellationToken cancellationToken = default); } @@ -110,8 +122,9 @@ public sealed class ImageDecoder(IReadOnlyList providers) /// embedded preview a provider offers, else unavailable with the first reason given. A provider that finds the file /// malformed is skipped like in . /// + /// Where a GoPro GPR's JPEG is kept when it is not beside the file (). /// The file cannot be read. - 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 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 providers) return preview ?? new FileTier(format, DecodeTier.Unavailable, null, reason ?? $"no decoder for {format}", 0, 0); } - /// Decodes with the first provider that succeeds; null means . + /// + /// Decodes with the first provider that succeeds; null means : 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. + /// + /// The file could not be read and no provider produced pixels. public DecodedImage? Decode(DecodeRequest request, CancellationToken cancellationToken = default) { var format = ImageFormatInfo.FromPath(request.Path); List? 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 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; } } diff --git a/src/Titano.Data/Archiving/ArchiveImages.cs b/src/Titano.Data/Archiving/ArchiveImages.cs index c2c6e8b5..6b11aa7a 100644 --- a/src/Titano.Data/Archiving/ArchiveImages.cs +++ b/src/Titano.Data/Archiving/ArchiveImages.cs @@ -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(); + 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(); + 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 /// : 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. /// - private EncodedImage? Render(SequencePaths folder, SidecarFrame frame, Dictionary tiers, Func render) + private EncodedImage? Render(SequencePaths folder, SidecarFrame frame, string? companion, Dictionary tiers, Func 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; } /// The tier of a frame that decoded after it was flagged undecodable (the image's own tier when probing finds none). - 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) { diff --git a/src/Titano.Data/Archiving/FrameTiers.cs b/src/Titano.Data/Archiving/FrameTiers.cs index 245c89e5..8b7ae94d 100644 --- a/src/Titano.Data/Archiving/FrameTiers.cs +++ b/src/Titano.Data/Archiving/FrameTiers.cs @@ -17,20 +17,27 @@ namespace Titano.Data.Archiving; internal sealed class FrameTiers(TitanoDatabase db, SequenceWriter writer, ArchiveIndexer indexer) { /// - /// The tier of every frame of 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 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 + /// (; 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. /// - public Dictionary ProbeUnknown(SequencePaths folder, SequenceManifest manifest, CancellationToken cancellationToken) + public async Task> ProbeUnknownAsync(Archive archive, SequencePaths folder, SequenceManifest manifest, + CancellationToken cancellationToken) { var tiers = new Dictionary(); - 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; } diff --git a/src/Titano.Data/Archiving/GprCompanions.cs b/src/Titano.Data/Archiving/GprCompanions.cs new file mode 100644 index 00000000..1f167de8 --- /dev/null +++ b/src/Titano.Data/Archiving/GprCompanions.cs @@ -0,0 +1,63 @@ +using Titano.Core.Archiving; +using Titano.Core.Imaging; +using Titano.Core.Model; + +namespace Titano.Data.Archiving; + +/// A GPR's JPEG: its absolute path and its size as the index has it (before orientation). +internal readonly record struct GprCompanion(string Path, int Width, int Height); + +/// +/// 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. +/// +public static class GprCompanions +{ + private static readonly TimeSpan Tolerance = TimeSpan.FromSeconds(1); + + /// + /// The absolute path of the JPEG of each GPR frame of (index rows), by frame id; frames + /// without one are left out. One index query, none when no frame is a GPR. + /// + public static async Task> FindAsync(TitanoDatabase db, Archive archive, IReadOnlyList 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)); + + /// + public static async Task> FindAsync(TitanoDatabase db, Archive archive, IReadOnlyList frames, + CancellationToken cancellationToken) => + Paths(await FindWithSizesAsync(db, archive, frames, cancellationToken).ConfigureAwait(false)); + + /// The JPEG of each GPR frame of (sidecar frames) with its indexed size, by frame id. + internal static Task> FindWithSizesAsync(TitanoDatabase db, Archive archive, IReadOnlyList 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> FindAsync(TitanoDatabase db, Archive archive, + List<(Guid Id, string Name, DateTime? CapturedAt)> gprs, CancellationToken cancellationToken) + { + var found = new Dictionary(); + 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 Paths(Dictionary companions) => companions.ToDictionary(kv => kv.Key, kv => kv.Value.Path); + + private static string Stem(string fileName) => Path.GetFileNameWithoutExtension(fileName); +} diff --git a/src/Titano.Data/Processing/Jobs/RenderJobHandler.cs b/src/Titano.Data/Processing/Jobs/RenderJobHandler.cs index 66446574..4dfb7efa 100644 --- a/src/Titano.Data/Processing/Jobs/RenderJobHandler.cs +++ b/src/Titano.Data/Processing/Jobs/RenderJobHandler.cs @@ -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 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, diff --git a/src/Titano.Data/Processing/SequenceAnalyser.cs b/src/Titano.Data/Processing/SequenceAnalyser.cs index 0122627f..4354c5d7 100644 --- a/src/Titano.Data/Processing/SequenceAnalyser.cs +++ b/src/Titano.Data/Processing/SequenceAnalyser.cs @@ -30,6 +30,9 @@ internal sealed record AnalysisOutcome(Sequence Sequence, int Frames, int Warnin /// internal sealed class SequenceAnalyser(TitanoDatabase db, ProcessingOptions options, SequenceWriter writer, ArchiveIndexer indexer, ProjectStore projects) { + /// Long edge of the decode that confirms a frame an interrupted earlier run found undecodable. + private const int ConfirmLongEdge = 64; + private readonly FrameTiers _tiers = new(db, writer, indexer); /// Analyses one sequence; null when it is no longer in the index. @@ -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 } /// - /// 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. /// - private static Dictionary Decoded(SequenceFrames frames, DeepAnalysisReport report, IReadOnlyDictionary observed) + private Dictionary Decoded(SequenceFrames frames, DeepAnalysisReport report, SequenceFrameSource source, + IReadOnlyDictionary companions, CancellationToken cancellationToken) { var tiers = new Dictionary(); - 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 Observed)> AnalyseDeepAsync(SequenceFrames frames, + private async Task<(DeepAnalysisReport Report, Dictionary Decoded)> AnalyseDeepAsync(Archive archive, SequenceFrames frames, IReadOnlyList metadata, SequencePaths folder, IProgress? 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 observed; - await using (var source = new SequenceFrameSource(frames, metadata, options.Archive.Decoder) { Originals = true }) + Dictionary 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); } /// Identity of the analysed content: frames replaced or renumbered since a checkpoint was written (with the diff --git a/src/Titano.Data/Processing/SequenceFrameSource.cs b/src/Titano.Data/Processing/SequenceFrameSource.cs index 8fb0aaf9..17d4f5b4 100644 --- a/src/Titano.Data/Processing/SequenceFrameSource.cs +++ b/src/Titano.Data/Processing/SequenceFrameSource.cs @@ -15,7 +15,8 @@ namespace Titano.Data.Processing; /// still cannot be decoded stops the render with , unless the render gave /// : 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 . Render reads go through a -/// per decode size, which decodes ahead in index order and spills under memory pressure. +/// 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). /// internal sealed class SequenceFrameSource : IFrameSource, IRenderFrameSource, IAsyncDisposable { @@ -33,6 +34,7 @@ internal sealed class SequenceFrameSource : IFrameSource, IRenderFrameSource, IA private readonly ConcurrentDictionary _observed = new(); private readonly ConcurrentDictionary _standIns = new(); private readonly ConcurrentDictionary _failed = new(); + private readonly ConcurrentDictionary _read = new(); private FrameWindow? _window; private (int Edge, DecodePurpose Purpose) _windowKey; @@ -56,11 +58,24 @@ internal sealed class SequenceFrameSource : IFrameSource, IRenderFrameSource, IA public IReadOnlySet? RenderExcluded { get; init; } /// - /// Decode tier each frame decoded at, or when nothing decoded its file (sizes - /// oriented, as reports them); recorded only for . + /// The JPEG a GoPro wrote with each GPR frame that is not beside it, by frame id (). + /// + public IReadOnlyDictionary? Companions { get; init; } + + /// + /// Decode tier each frame decoded at, or when the decoders read its file and none + /// produced pixels; recorded only for . 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. /// public IReadOnlyDictionary Observed => _observed; + /// + /// Positions whose file this source tried to decode, whatever came of it (a read error or a vanished file records no + /// tier in ). + /// + public IReadOnlyCollection Read => [.. _read.Keys]; + /// Positions a render showed a neighbour for because they did not decode, ascending. public IReadOnlyList 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) + /// + /// The window's loader, for the pipeline's reads and the read-ahead alike: the decoded frame, else + /// , remembered so the read-ahead does not decode the file again. + /// + 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(); + } + + /// A copy of the window's frame, or null when it does not decode. + private static async ValueTask 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; + } + } + + /// + /// 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 + /// , or when nothing decodes, the render stops. + /// + private async ValueTask 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 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]); } + + /// A frame of the window that does not decode; resolves it, nothing else sees it. + private sealed class NotDecodedException : Exception; } diff --git a/src/Titano.Data/Processing/SequenceRender.cs b/src/Titano.Data/Processing/SequenceRender.cs index edf29c10..adcac592 100644 --- a/src/Titano.Data/Processing/SequenceRender.cs +++ b/src/Titano.Data/Processing/SequenceRender.cs @@ -32,7 +32,9 @@ internal sealed class SequenceRender : IAsyncDisposable /// The sequence's deep report, when current: its luminance spares the preparation a decode pass. /// The frames' files are the archive's originals (no proxies), so describes them. - public SequenceRender(SequenceFrames frames, ProjectSettings project, DeepAnalysisReport? analysis, ProcessingOptions options, bool originals = false) + /// The JPEG of each GPR frame kept in another sequence, by frame id (). + public SequenceRender(SequenceFrames frames, ProjectSettings project, DeepAnalysisReport? analysis, ProcessingOptions options, bool originals = false, + IReadOnlyDictionary? 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; diff --git a/src/Titano.Data/Repositories/FrameRepository.cs b/src/Titano.Data/Repositories/FrameRepository.cs index bac5ed69..c8eac1e3 100644 --- a/src/Titano.Data/Repositories/FrameRepository.cs +++ b/src/Titano.Data/Repositories/FrameRepository.cs @@ -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); + /// Frames of an archive in this format captured from to (inclusive), + /// by capture time (a GoPro GPR's JPEG in another sequence). + public Task> 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); + /// Every frame of an archive whose SHA-256 occurs more than once, grouped by hash then sequence and index. public Task> ListDuplicatesAsync(Guid archiveId, CancellationToken cancellationToken = default) => _db.ListAsync( diff --git a/tests/Titano.Core.Tests/Archiving/DecodeTierTests.cs b/tests/Titano.Core.Tests/Archiving/DecodeTierTests.cs index 72e5c19a..49f548b2 100644 --- a/tests/Titano.Core.Tests/Archiving/DecodeTierTests.cs +++ b/tests/Titano.Core.Tests/Archiving/DecodeTierTests.cs @@ -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())); } } diff --git a/tests/Titano.Core.Tests/Decoding/ManagedDecoderProviderTests.cs b/tests/Titano.Core.Tests/Decoding/ManagedDecoderProviderTests.cs index a3edf6a7..aa161f84 100644 --- a/tests/Titano.Core.Tests/Decoding/ManagedDecoderProviderTests.cs +++ b/tests/Titano.Core.Tests/Decoding/ManagedDecoderProviderTests.cs @@ -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(() => 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(() => 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(() => DecoderFactory.CreateServer().Decode(new DecodeRequest(Path.Combine(_dir, "vanished.jpg")))); + } + /// A GoPro GPR as far as Titano reads it: a DNG made by "GoPro" whose only decodable image is a YCbCr JPEG preview. private static byte[] Gpr(int previewWidth, int previewHeight) { @@ -286,4 +332,12 @@ public sealed class ManagedDecoderProviderTests(TestContext context) return null; } } + + /// A JPEG provider whose decode does what the test says (return nothing, or throw). + private sealed class ScriptedProvider(string name, Func 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); + } } diff --git a/tests/Titano.Data.Tests/Processing/AnalysisJobTests.cs b/tests/Titano.Data.Tests/Processing/AnalysisJobTests.cs index c49fce15..eb531572 100644 --- a/tests/Titano.Data.Tests/Processing/AnalysisJobTests.cs +++ b/tests/Titano.Data.Tests/Processing/AnalysisJobTests.cs @@ -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 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() { diff --git a/tests/Titano.Data.Tests/Processing/ProcessingFixture.cs b/tests/Titano.Data.Tests/Processing/ProcessingFixture.cs index 8853d362..690ff8fd 100644 --- a/tests/Titano.Data.Tests/Processing/ProcessingFixture.cs +++ b/tests/Titano.Data.Tests/Processing/ProcessingFixture.cs @@ -123,25 +123,43 @@ internal sealed class GateDecoder : IImageDecoderProvider Hold = ProcessingFixture.FileName(index); } - /// Tiers reports per file name instead of the managed provider's (a host that reads them - /// only as previews, or not at all). + /// Tiers reports per file name instead of the managed provider's (a + /// host that reads them only as previews, or not at all). public ConcurrentDictionary Tiers { get; } = new(); + /// 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. + public ConcurrentDictionary PreviewDecodes { get; } = new(); + + /// Files (by name) that cannot be read: their decodes throw (a sharing violation). + public ConcurrentDictionary Unreadable { get; } = new(); + + /// File name and decoder of every decode that produced pixels. + 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); } } diff --git a/tests/Titano.Data.Tests/Processing/RenderJobTests.cs b/tests/Titano.Data.Tests/Processing/RenderJobTests.cs index c22757f4..9bfb19b0 100644 --- a/tests/Titano.Data.Tests/Processing/RenderJobTests.cs +++ b/tests/Titano.Data.Tests/Processing/RenderJobTests.cs @@ -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(), + }; + 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(), + }; + 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() { diff --git a/tests/Titano.Data.Tests/Repositories/CatalogRepositoryTests.cs b/tests/Titano.Data.Tests/Repositories/CatalogRepositoryTests.cs index 0c09f672..b9bd4b22 100644 --- a/tests/Titano.Data.Tests/Repositories/CatalogRepositoryTests.cs +++ b/tests/Titano.Data.Tests/Repositories/CatalogRepositoryTests.cs @@ -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] From bbc7f244fa8e59f6299ba3f8c86d21b3a0772c1a Mon Sep 17 00:00:00 2001 From: Claude Date: Mon, 12 Oct 2026 11:17:55 +0000 Subject: [PATCH 7/7] fix(data): never let the compile_options probe block opening an index Any failure of the informational PRAGMA compile_options probe is logged and gives an empty list; before, anything but a SqliteException was cached by the lazy value and rethrown by every later open. Tests now cover the decode-tier fields end to end: the Library's preview-only chip and its plural tooltip, server cards and frames carrying previewOnlyCount and the preview size over the API. Co-Authored-By: Claude Opus 5.5 Claude-Session: https://claude.ai/code/session_012iLMBNwBE2GnphAsu4cjaC --- src/Titano.Data/Sqlite/SqliteDatabase.cs | 53 +++++++++++-------- .../Library/RemoteCardsTests.cs | 22 ++++++++ .../Sqlite/SqliteBindingTests.cs | 9 ++++ .../Titano.Server.Tests/Api/SequenceTests.cs | 37 +++++++++++++ 4 files changed, 100 insertions(+), 21 deletions(-) diff --git a/src/Titano.Data/Sqlite/SqliteDatabase.cs b/src/Titano.Data/Sqlite/SqliteDatabase.cs index f9b56736..c7286df0 100644 --- a/src/Titano.Data/Sqlite/SqliteDatabase.cs +++ b/src/Titano.Data/Sqlite/SqliteDatabase.cs @@ -37,7 +37,7 @@ public sealed class SqliteDatabase : IDatabase internal const int BusyTimeoutMs = 5000; private const string LogCategory = "Data.Sqlite"; private static readonly TimeSpan WriteLockTimeout = TimeSpan.FromSeconds(60); - private static readonly Lazy> ProbedCompileOptions = new(ProbeCompileOptions); + private static readonly Lazy> ProbedCompileOptions = new(() => ProbeCompileOptions(ReadCompileOptions)); private readonly string _filename; private readonly bool _memory; @@ -275,34 +275,45 @@ public sealed class SqliteDatabase : IDatabase throw new NotSupportedException($"SQLite {version / 1_000_000}.{version / 1000 % 1000}.{version % 1000} is too old; 3.31 or newer is required."); } - private static IReadOnlyList ProbeCompileOptions() + /// + /// The options returns, logged; any failure is logged and gives none, since the probe is + /// informational and must never stop an index from opening (a failure escaping the lazy value would be rethrown by + /// every later open). + /// + internal static IReadOnlyList ProbeCompileOptions(Func> read) { - var options = new List(); try { - using var connection = SqliteConnection.Open(":memory:", isMemory: true, BusyTimeoutMs); - var statement = connection.Rent("PRAGMA compile_options"); - try - { - var columns = statement.ReadColumns(); - int rc; - while ((rc = statement.Step()) == SQLITE_ROW) - { - if (statement.ReadRow(columns).GetStringOrNull(0) is { Length: > 0 } option) options.Add(option); - } - if (rc != SQLITE_DONE) throw connection.Error(rc); - } - finally - { - connection.Return(statement); - } + var options = read(); + Log.Info(LogCategory, $"SQLite {LibraryVersion}; compile options: {string.Join(' ', options)}"); + return options; } - catch (SqliteException ex) + catch (Exception ex) when (ex is not OutOfMemoryException) { Log.Warning(LogCategory, "PRAGMA compile_options could not be read", ex); return []; } - Log.Info(LogCategory, $"SQLite {LibraryVersion}; compile options: {string.Join(' ', options)}"); + } + + private static List ReadCompileOptions() + { + var options = new List(); + using var connection = SqliteConnection.Open(":memory:", isMemory: true, BusyTimeoutMs); + var statement = connection.Rent("PRAGMA compile_options"); + try + { + var columns = statement.ReadColumns(); + int rc; + while ((rc = statement.Step()) == SQLITE_ROW) + { + if (statement.ReadRow(columns).GetStringOrNull(0) is { Length: > 0 } option) options.Add(option); + } + if (rc != SQLITE_DONE) throw connection.Error(rc); + } + finally + { + connection.Return(statement); + } return options; } diff --git a/tests/Titano.App.Tests/Library/RemoteCardsTests.cs b/tests/Titano.App.Tests/Library/RemoteCardsTests.cs index 64202d20..9fe96252 100644 --- a/tests/Titano.App.Tests/Library/RemoteCardsTests.cs +++ b/tests/Titano.App.Tests/Library/RemoteCardsTests.cs @@ -1,9 +1,12 @@ +using Titano.App.Infrastructure; using Titano.App.Library; +using Titano.Core.Localization; using Titano.Core.Model; using Titano.Data.Repositories; using Titano.Data.Sync; using Titano.Protocol; using Titano.Testing; +using Titano.Ui.Controls; namespace Titano.App.Tests.Library; @@ -70,6 +73,25 @@ public sealed class RemoteCardsTests Assert.Equal(SyncState.RemoteOnly, card.SyncState); } + [Test] + public void PreviewOnlyFramesShowAChipWithTheirCount() + { + // A server sequence of CR3s it reads only through their previews (BRIEF §8.2 tier 2). + var link = Link("Aurora"); + var dto = new SequenceDto(link.RemoteId, "Aurora", "Aurora", "2026/2026-02-20 Nikon Z 8", "NIKON CORPORATION", "NIKON Z 8", null, + new DateTime(2026, 2, 20, 21, 0, 0), new DateTime(2026, 2, 21, 1, 8, 0), 1_860, 8.0, 50_000_000_000, (int)SequenceStatus.AnalyzedLight, 0, null, + [], 0, null, 1, 1, 0, null, DateTimeOffset.UtcNow, PreviewOnlyCount: 1_860); + var card = RemoteCards.Build([new SyncEntry(null, link, SyncStatus.RemoteOnly)], new Dictionary { [dto.Id] = dto }).Single(); + Assert.Equal(1_860, card.PreviewOnlyCount); + Assert.Equal(1_860, RemoteCards.ToSequence(dto).PreviewOnlyCount); + + var chip = SequenceItem.BuildStatuses(card, analysing: false).Single(s => s.Kind == StatusKind.PreviewOnly); + Assert.Equal(Formatting.Count(1_860), chip.Text); + Assert.Equal(Loc.Plural("Tooltips.Library.Status.PreviewOnly", 1_860), chip.ToolTip); + Assert.NotEqual(Loc.Plural("Tooltips.Library.Status.PreviewOnly", 1), chip.ToolTip, "plural forms"); + Assert.False(SequenceItem.BuildStatuses(card with { PreviewOnlyCount = 0 }, analysing: false).Any(s => s.Kind == StatusKind.PreviewOnly), "no chip without them"); + } + [Test] public void CardsSortAmongThePagesAndFollowTheFilter() { diff --git a/tests/Titano.Data.Tests/Sqlite/SqliteBindingTests.cs b/tests/Titano.Data.Tests/Sqlite/SqliteBindingTests.cs index 4fe19f6e..ef68a17a 100644 --- a/tests/Titano.Data.Tests/Sqlite/SqliteBindingTests.cs +++ b/tests/Titano.Data.Tests/Sqlite/SqliteBindingTests.cs @@ -28,6 +28,15 @@ public sealed class SqliteBindingTests(TestContext context) : DatabaseTest(conte if (!OperatingSystem.IsWindows()) Assert.Contains(options, o => o.StartsWith("THREADSAFE=", StringComparison.Ordinal)); } + [Test] + public void AProbeThatFailsInAnyWayGivesNoOptionsInsteadOfBlockingTheIndex() + { + // A build that returns no statement for the pragma, a column it cannot read, a SQLite error: none escapes. + Assert.Empty(SqliteDatabase.ProbeCompileOptions(() => throw new ArgumentException("No statement in 'PRAGMA compile_options'."))); + Assert.Empty(SqliteDatabase.ProbeCompileOptions(() => throw new InvalidOperationException("Column type 5 is not text."))); + Assert.SequenceEqual(["THREADSAFE=1"], SqliteDatabase.ProbeCompileOptions(() => ["THREADSAFE=1"])); + } + [Test] public async Task ConnectionsUseTheBriefPragmas() { diff --git a/tests/Titano.Server.Tests/Api/SequenceTests.cs b/tests/Titano.Server.Tests/Api/SequenceTests.cs index b160c278..ca46a573 100644 --- a/tests/Titano.Server.Tests/Api/SequenceTests.cs +++ b/tests/Titano.Server.Tests/Api/SequenceTests.cs @@ -228,4 +228,41 @@ public sealed class SequenceTests(TestContext context) using var analysis = await editor.GetAsync($"/api/v1/sequences/{id}/analysis"); Assert.Equal("analysis", (await analysis.ExpectErrorAsync(HttpStatusCode.NotFound, "not_found")).Details); } + + [Test] + public async Task CardsAndFramesCarryTheDecodeTierFields() + { + await using var server = await TestServer.StartAsync(context); + var (editor, _, _) = await server.UserAsync("alberto", "editor"); + var first = await Uploads.UploadAsync(editor, Uploads.Photo(1000, 50), "t0.CR3", sessionKey: "t"); + await Uploads.UploadAsync(editor, Uploads.Photo(1001, 51), "t1.CR3", sessionKey: "t"); + var db = server.Database; + // The follow-up jobs of the upload (thumbnails, proxies, light analysis) rewrite the rows: let them finish first. + for (var waited = 0; waited < 600; waited++) + { + if ((await db.Jobs.ListAsync(Titano.Core.Model.JobState.Queued)).Items.Count == 0 + && (await db.Jobs.ListAsync(Titano.Core.Model.JobState.Running)).Items.Count == 0) break; + await Task.Delay(50, context.Cancellation); + } + // What a light analysis on the server records for CR3s it reads only through their previews. + var frames = await db.Frames.ListBySequenceAsync(first.SequenceId); + await db.Frames.UpdateTiersAsync([.. frames.Select(f => new Titano.Data.Repositories.FrameTierFields(f.Id, Titano.Core.Model.FrameFlags.PreviewOnly, 1620, 1080))]); + var sequence = (await db.Sequences.GetAsync(first.SequenceId))!; + await db.Sequences.UpsertAsync(sequence with { PreviewOnlyCount = 2 }); + + // The Library cards read the count (the chip), the remote editor the preview size (the output cap). + using var list = await editor.GetAsync("/api/v1/sequences"); + await list.ExpectAsync(HttpStatusCode.OK); + Assert.Contains("\"previewOnlyCount\":2", await list.Content.ReadAsStringAsync()); + using var listed = await editor.GetAsync("/api/v1/sequences"); + Assert.Equal(2, (await listed.ReadAsync(ProtocolJson.Default.PageSequenceDto)).Items.Single(s => s.Id == first.SequenceId).PreviewOnlyCount); + using var page = await editor.GetAsync($"/api/v1/sequences/{first.SequenceId}/frames"); + await page.ExpectAsync(HttpStatusCode.OK); + var json = await page.Content.ReadAsStringAsync(); + Assert.Contains("\"previewWidth\":1620", json); + Assert.Contains("\"previewHeight\":1080", json); + using var typed = await editor.GetAsync($"/api/v1/sequences/{first.SequenceId}/frames"); + var dtos = (await typed.ReadAsync(ProtocolJson.Default.PageFrameDto)).Items; + Assert.True(dtos.All(f => (f.PreviewWidth, f.PreviewHeight, f.Flags) == (1620, 1080, (long)Titano.Core.Model.FrameFlags.PreviewOnly)), "frame DTOs"); + } }