Merge branch 'fix/imaging-metadata': decode tiers, preview-only and undecodable frames, GPR companions, MakerNote fields
Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com> Claude-Session: https://claude.ai/code/session_012iLMBNwBE2GnphAsu4cjaC
This commit is contained in:
70 files changed
+2201
-124
No files matched your search
@@ -472,7 +472,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
|
||||
@@ -491,6 +491,7 @@ public sealed class SampleData
|
||||
Status = status,
|
||||
RenderedCount = renders,
|
||||
WarningCount = warnings,
|
||||
PreviewOnlyCount = previewOnly,
|
||||
Origin = origin,
|
||||
SyncState = sync,
|
||||
UpdatedAt = Now,
|
||||
@@ -506,7 +507,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,
|
||||
|
||||
@@ -37,6 +37,9 @@ public sealed class LocalSequenceSource : ISequenceSource
|
||||
private readonly JobEngine? _engine;
|
||||
private readonly ArchiveServiceOptions _lease;
|
||||
private string?[] _files = [];
|
||||
|
||||
/// <summary>The JPEG of each GPR frame that an import keeps in another sequence (<see cref="GprCompanions"/>), by position.</summary>
|
||||
private string?[] _companions = [];
|
||||
private Guid[] _frameIds = [];
|
||||
private ArchivePaths? _paths;
|
||||
private SequencePaths? _folder;
|
||||
@@ -81,6 +84,7 @@ public sealed class LocalSequenceSource : ISequenceSource
|
||||
if (page.Count < PageSize) break;
|
||||
}
|
||||
var renders = await _db.Renders.ListBySequenceAsync(_sequenceId, cancellationToken);
|
||||
var companions = await GprCompanions.FindAsync(_db, archive, frames, cancellationToken);
|
||||
return await Task.Run(() =>
|
||||
{
|
||||
var paths = new ArchivePaths(archive.RootPath);
|
||||
@@ -90,6 +94,7 @@ public sealed class LocalSequenceSource : ISequenceSource
|
||||
{
|
||||
if ((frames[i].Flags & FrameFlags.Missing) == 0) files[i] = paths.FromRelative(frames[i].RelativePath);
|
||||
}
|
||||
_companions = [.. frames.Select(f => companions.GetValueOrDefault(f.Id))];
|
||||
(_paths, _folder, _files, _frameIds) = (paths, folder, files, [.. frames.Select(f => f.Id)]);
|
||||
var context = SequenceContext.FromFrames(frames);
|
||||
return new SequenceContent(sequence, frames, ReadProject(sequence, folder), LightAnalyzer.Analyze(frames), ReadReport(folder, frames.Count),
|
||||
@@ -100,12 +105,14 @@ public sealed class LocalSequenceSource : ISequenceSource
|
||||
public async ValueTask<ImageBuffer?> DecodeAsync(int index, int maxLongEdge, DecodePurpose purpose, CancellationToken cancellationToken)
|
||||
{
|
||||
if ((uint)index >= (uint)_files.Length || _files[index] is not { } file) return null;
|
||||
var companions = _companions;
|
||||
var companion = (uint)index < (uint)companions.Length ? companions[index] : null;
|
||||
return await Task.Run(() =>
|
||||
{
|
||||
try
|
||||
{
|
||||
// Thumbnails take an embedded preview when it is large enough (no RAW development for a filmstrip cell).
|
||||
var request = new DecodeRequest(file, maxLongEdge, purpose, ForcePreview: purpose == DecodePurpose.Thumbnail);
|
||||
var request = new DecodeRequest(file, maxLongEdge, purpose, ForcePreview: purpose == DecodePurpose.Thumbnail, Companion: companion);
|
||||
return _decoder.Decode(request, cancellationToken)?.Pixels;
|
||||
}
|
||||
catch (Exception ex) when (ex is IOException or UnauthorizedAccessException)
|
||||
|
||||
@@ -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,
|
||||
|
||||
@@ -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;
|
||||
}
|
||||
|
||||
/// <summary>Graphics adapter, video encoders, WIC decoders per format and missing Store extensions.</summary>
|
||||
private static string Codecs(IEncoderInventory encoders)
|
||||
|
||||
@@ -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,
|
||||
|
||||
@@ -237,10 +237,13 @@ public sealed class SequenceItem : ObservableObject
|
||||
_ => null,
|
||||
};
|
||||
|
||||
/// <summary>Card status row: analysed, proxies, renders, errors or warnings, sync (Main.html); the sync glyph is last so a narrow card cuts it first.</summary>
|
||||
/// <summary>
|
||||
/// 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.
|
||||
/// </summary>
|
||||
public static IReadOnlyList<CardStatus> BuildStatuses(Sequence s, bool analysing)
|
||||
{
|
||||
var list = new List<CardStatus>(5);
|
||||
var list = new List<CardStatus>(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)
|
||||
|
||||
@@ -16,14 +16,32 @@ public static class Advisor
|
||||
{
|
||||
/// <summary>
|
||||
/// Recommendations for every parameter the rules cover (see <see cref="AdvisorRules"/>), 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. 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.
|
||||
/// </summary>
|
||||
public static AdvisorResult Recommend(LightAnalysisReport light, DeepAnalysisReport? deep, SequenceContext context)
|
||||
{
|
||||
ArgumentNullException.ThrowIfNull(light);
|
||||
ArgumentNullException.ThrowIfNull(context);
|
||||
var findings = new List<Finding>(light.Findings);
|
||||
if (deep is not null) findings.AddRange(deep.Findings.Select(f => f.ToFinding()));
|
||||
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);
|
||||
}
|
||||
|
||||
|
||||
@@ -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");
|
||||
|
||||
@@ -185,11 +185,13 @@ internal sealed class AdvisorRules
|
||||
|
||||
/// <summary>
|
||||
/// 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.
|
||||
/// </summary>
|
||||
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);
|
||||
|
||||
@@ -144,27 +144,38 @@ public sealed record SequenceContext
|
||||
/// <summary>Wanted video length ("fit to N seconds"), when the user set one.</summary>
|
||||
public double? TargetDurationSeconds { get; init; }
|
||||
|
||||
/// <summary>Context from the archive's frame rows: count, dominant oriented size, preview-only count, median shutter time.</summary>
|
||||
/// <summary>
|
||||
/// 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.
|
||||
/// </summary>
|
||||
public static SequenceContext FromFrames(IReadOnlyList<Frame> frames, IReadOnlyList<EncoderCapability>? codecs = null)
|
||||
{
|
||||
ArgumentNullException.ThrowIfNull(frames);
|
||||
var sizes = new Dictionary<(int, int), int>();
|
||||
var previews = new Dictionary<(int, int), int>();
|
||||
var exposures = new List<double>();
|
||||
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 ?? [],
|
||||
};
|
||||
|
||||
@@ -114,6 +114,7 @@ public sealed record DeepFinding(string Key, FindingSeverity Severity, double[]
|
||||
/// <summary>Resource keys of the findings produced by the deep analysis; arguments are numbers.</summary>
|
||||
public static class DeepFindingKeys
|
||||
{
|
||||
/// <summary>Frames nothing decodes; the light analysis reports the frames flagged so under it too (format tier 3).</summary>
|
||||
public const string UnreadableFrames = "Advisor.Finding.UnreadableFrames";
|
||||
public const string BlackFrames = "Advisor.Finding.BlackFrames";
|
||||
public const string WhiteFrames = "Advisor.Finding.WhiteFrames";
|
||||
|
||||
@@ -36,6 +36,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";
|
||||
|
||||
/// <summary>Frames that decode only through their embedded preview (format tier 2); {0} frame count.</summary>
|
||||
public const string PreviewOnly = "Advisor.Finding.PreviewOnly";
|
||||
public const string TimeZoneEstimated = "Advisor.Finding.TimeZoneEstimated";
|
||||
public const string DayNightTransition = "Advisor.Finding.DayNightTransition";
|
||||
}
|
||||
@@ -131,5 +131,14 @@ public sealed record LightAnalysisReport
|
||||
/// <summary>Light condition per frame from GPS position and capture time; all null without GPS.</summary>
|
||||
public DayPhase?[] DayPhases { get; init; } = [];
|
||||
|
||||
/// <summary>Frames that decode only through their embedded preview (format tier 2, BRIEF §8.2).</summary>
|
||||
public IReadOnlyList<int> PreviewOnlyFrames { get; init; } = [];
|
||||
|
||||
/// <summary>Frames that nothing decodes (format tier 3): renders skip them.</summary>
|
||||
public IReadOnlyList<int> UndecodableFrames { get; init; } = [];
|
||||
|
||||
/// <summary>Frames the camera's own interval-shooting mode took (MakerNote hint, <see cref="Model.FrameFlags.Intervalometer"/>).</summary>
|
||||
public int IntervalometerFrames { get; init; }
|
||||
|
||||
public IReadOnlyList<Finding> Findings { get; init; } = [];
|
||||
}
|
||||
@@ -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;
|
||||
|
||||
/// <summary>
|
||||
/// 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.
|
||||
/// </summary>
|
||||
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,9 @@ public static class LightAnalyzer
|
||||
DominantOrientation = orientation,
|
||||
OrientationOutliers = orientationOutliers,
|
||||
DayPhases = phases,
|
||||
PreviewOnlyFrames = previewOnly,
|
||||
UndecodableFrames = undecodable,
|
||||
IntervalometerFrames = frames.Count(f => (f.Flags & FrameFlags.Intervalometer) != 0),
|
||||
Findings = findings,
|
||||
};
|
||||
}
|
||||
@@ -334,6 +340,25 @@ public static class LightAnalyzer
|
||||
return (dominant, sizeOutliers, orientation, orientationOutliers);
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// 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).
|
||||
/// </summary>
|
||||
private static (List<int> PreviewOnly, List<int> Undecodable) AnalyzeTiers(IReadOnlyList<LightFrame> frames, List<Finding> findings)
|
||||
{
|
||||
var previewOnly = new List<int>();
|
||||
var undecodable = new List<int>();
|
||||
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<LightFrame> frames, DateTime?[] adjusted, List<Finding> findings)
|
||||
{
|
||||
var n = frames.Count;
|
||||
|
||||
@@ -31,6 +31,9 @@ public sealed record LightFrame
|
||||
/// <summary>GPS (satellite) UTC time stamp, used to infer the camera clock's UTC offset.</summary>
|
||||
public DateTime? GpsUtc { get; init; }
|
||||
|
||||
/// <summary>The archived frame's flags; the decode-tier bits feed the format-tier findings.</summary>
|
||||
public FrameFlags Flags { get; init; }
|
||||
|
||||
/// <summary>Exposure value at ISO 100: log2(N²/t) − log2(ISO/100).</summary>
|
||||
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,
|
||||
};
|
||||
}
|
||||
|
||||
|
||||
@@ -8,8 +8,9 @@ namespace Titano.Core.Archiving;
|
||||
public sealed record EncodedImage(byte[] Jpeg, int Width, int Height, DecodeTier Tier);
|
||||
|
||||
/// <summary>
|
||||
/// 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. <see cref="EncodedImage.Tier"/> says how the pixels were obtained.
|
||||
/// </summary>
|
||||
public sealed class FrameImageRenderer(ImageDecoder decoder)
|
||||
{
|
||||
@@ -21,19 +22,23 @@ public sealed class FrameImageRenderer(ImageDecoder decoder)
|
||||
Thumbnail(sequence, frame, new ThumbnailPolicy(), cancellationToken);
|
||||
|
||||
/// <summary>A thumbnail at the policy's long edge and quality (the archive's, BRIEF §8.5), or null when no decoder can read the frame.</summary>
|
||||
public EncodedImage? Thumbnail(SequencePaths sequence, SidecarFrame frame, ThumbnailPolicy policy, CancellationToken cancellationToken = default)
|
||||
/// <param name="companion">Where a GPR's JPEG is kept when it is not beside it (<see cref="DecodeRequest.Companion"/>).</param>
|
||||
public EncodedImage? Thumbnail(SequencePaths sequence, SidecarFrame frame, ThumbnailPolicy policy, CancellationToken cancellationToken = default,
|
||||
string? companion = null)
|
||||
{
|
||||
using var decoded = Decode(sequence.File(frame.File), frame, policy.LongEdge, DecodePurpose.Thumbnail, preferPreview: true, cancellationToken);
|
||||
using var decoded = Decode(sequence.File(frame.File), frame, policy.LongEdge, DecodePurpose.Thumbnail, preferPreview: true, companion, cancellationToken);
|
||||
if (decoded is null) return null;
|
||||
using var small = Resampler.Thumbnail(decoded.Pixels, policy.LongEdge);
|
||||
return Encode(small, new JpegEncodeOptions { Quality = policy.Quality }, decoded.Tier);
|
||||
}
|
||||
|
||||
/// <summary>A proxy at the policy's long edge and quality with capture time and camera in its Exif; null when undecodable.</summary>
|
||||
public EncodedImage? Proxy(SequencePaths sequence, SidecarFrame frame, SidecarCamera? camera, ProxyPolicy policy, CancellationToken cancellationToken = default)
|
||||
/// <param name="companion">Where a GPR's JPEG is kept when it is not beside it (<see cref="DecodeRequest.Companion"/>).</param>
|
||||
public EncodedImage? Proxy(SequencePaths sequence, SidecarFrame frame, SidecarCamera? camera, ProxyPolicy policy, CancellationToken cancellationToken = default,
|
||||
string? companion = null)
|
||||
{
|
||||
if (policy.LongEdge == ProxyPolicy.Disabled) throw new InvalidOperationException("Proxies are disabled for this archive.");
|
||||
using var decoded = Decode(sequence.File(frame.File), frame, policy.LongEdge, DecodePurpose.Proxy, preferPreview: false, cancellationToken);
|
||||
using var decoded = Decode(sequence.File(frame.File), frame, policy.LongEdge, DecodePurpose.Proxy, preferPreview: false, companion, cancellationToken);
|
||||
if (decoded is null) return null;
|
||||
var source = decoded.Pixels;
|
||||
var (w, h) = policy.LongEdge == ProxyPolicy.FullSize ? (source.Width, source.Height) : Resampler.FitWithin(source.Width, source.Height, policy.LongEdge);
|
||||
@@ -59,13 +64,42 @@ 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);
|
||||
|
||||
private DecodedFrame? Decode(string path, SidecarFrame frame, int maxLongEdge, DecodePurpose purpose, bool preferPreview, CancellationToken cancellationToken)
|
||||
/// <summary>
|
||||
/// The frame's pixels: a JPEG through the managed decoder, the stored embedded preview when <paramref name="preferPreview"/>
|
||||
/// and it reaches <paramref name="maxLongEdge"/> (a smaller one would be upscaled or leave the thumbnail short), then
|
||||
/// the host's decoders (with the GPR's <paramref name="companion"/>), and the stored preview as the last resort. Null
|
||||
/// when nothing decodes.
|
||||
/// </summary>
|
||||
/// <exception cref="IOException">The file could not be read.</exception>
|
||||
private DecodedFrame? Decode(string path, SidecarFrame frame, int maxLongEdge, DecodePurpose purpose, bool preferPreview, string? companion,
|
||||
CancellationToken cancellationToken)
|
||||
{
|
||||
if (frame.Format == ImageFileFormat.Jpeg) 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);
|
||||
var request = new DecodeRequest(path, System.Math.Max(0, maxLongEdge), purpose, ApplyOrientation: true, AllowPreview: true);
|
||||
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, 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, Companion: companion);
|
||||
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;
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Whether the stored preview's long edge reaches <paramref name="maxLongEdge"/>, from the JPEG's own header: the
|
||||
/// sidecar's preview size may be a GPR companion's, larger than the preview embedded in the file.
|
||||
/// </summary>
|
||||
private static bool Reaches(byte[] preview, int maxLongEdge)
|
||||
{
|
||||
if (maxLongEdge <= 0) return true;
|
||||
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)
|
||||
|
||||
@@ -1,3 +1,4 @@
|
||||
using Titano.Core.Imaging;
|
||||
using Titano.Core.Metadata;
|
||||
using Titano.Core.Model;
|
||||
|
||||
@@ -13,7 +14,7 @@ public static class SequenceFrames
|
||||
return byTime != 0 ? byTime : string.CompareOrdinal(a.File, b.File);
|
||||
});
|
||||
|
||||
/// <summary>A frame record from light metadata.</summary>
|
||||
/// <summary>A frame record from light metadata (with <see cref="FrameFlags.Intervalometer"/> when the MakerNote says so).</summary>
|
||||
/// <param name="file">Path relative to the sequence folder.</param>
|
||||
/// <param name="fallbackCapture">Used when the file records no capture time of its own (only a file-system date).</param>
|
||||
public static SidecarFrame FromMetadata(Guid id, string file, string? original, long size, string? sha256, FrameMetadata metadata,
|
||||
@@ -42,9 +43,33 @@ 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),
|
||||
Flags = metadata.IntervalShooting == true ? FrameFlags.Intervalometer : FrameFlags.None,
|
||||
};
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// The frame with its decode tier (BRIEF §8.2) and the flags that follow it: <see cref="FrameFlags.PreviewOnly"/> for
|
||||
/// the embedded-preview tier, <see cref="FrameFlags.Undecodable"/> for none. <paramref name="tier"/>'s size, oriented
|
||||
/// like every <see cref="FileTier"/>, replaces the stored preview size when it is a preview's.
|
||||
/// </summary>
|
||||
/// <param name="damaged">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 <see cref="FrameFlags.Undecodable"/> is left as it was.</param>
|
||||
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;
|
||||
|
||||
/// <summary>
|
||||
/// Merges <paramref name="added"/> into the frame list by capture time (existing frames keep their relative order),
|
||||
/// renumbers indexes and bumps the frames version.
|
||||
|
||||
@@ -109,6 +109,20 @@ public sealed record SidecarFrame
|
||||
public long? PreviewOffset { get; init; }
|
||||
public long? PreviewLength { get; init; }
|
||||
|
||||
/// <summary>Size of the largest embedded preview (or GPR companion JPEG), stored like <see cref="W"/> and <see cref="H"/>
|
||||
/// before orientation; null when the file has none or it is not known yet.</summary>
|
||||
public int? PreviewW { get; init; }
|
||||
|
||||
public int? PreviewH { get; init; }
|
||||
|
||||
/// <summary>
|
||||
/// Decode tier of the file (BRIEF §8.2) as the host that indexed or decoded it last found it; omitted until it is
|
||||
/// known. <see cref="FrameFlags.PreviewOnly"/> and <see cref="FrameFlags.Undecodable"/> follow it.
|
||||
/// </summary>
|
||||
[JsonConverter(typeof(TolerantEnumConverter<DecodeTier>))]
|
||||
[JsonIgnore(Condition = JsonIgnoreCondition.WhenWritingDefault)]
|
||||
public DecodeTier Tier { get; init; }
|
||||
|
||||
/// <summary><see cref="FrameFlags"/> bits, stored as a number so bits of newer builds survive.</summary>
|
||||
public FrameFlags Flags { get; init; }
|
||||
|
||||
|
||||
@@ -7,19 +7,12 @@ using Titano.Core.Metadata;
|
||||
|
||||
namespace Titano.Core.Imaging.Decoders;
|
||||
|
||||
/// <summary>Tier a provider would use for one specific file, with the reason when it is not native.</summary>
|
||||
/// <param name="Format">Format detected from the file's signature (extension only breaks ties).</param>
|
||||
/// <param name="Tier">Best tier available for this file.</param>
|
||||
/// <param name="Decoder">Decoder that would produce it ("managed-cr2", "managed-preview"…), null when unavailable.</param>
|
||||
/// <param name="Reason">Why the native tier is not used (unsupported body or encoding), else null.</param>
|
||||
/// <param name="Width">Full-size width that tier delivers (developed RAW, preview or image size), oriented; 0 when unknown.</param>
|
||||
/// <param name="Height">Full-size height, oriented; 0 when unknown.</param>
|
||||
public sealed record FileTier(ImageFileFormat Format, DecodeTier Tier, string? Decoder, string? Reason, int Width, int Height);
|
||||
|
||||
/// <summary>
|
||||
/// The managed decoder (BRIEF §8.2), available on every platform and the only one on the server: JPEG, PNG and TIFF
|
||||
/// natively; DNG and the supported camera RAWs (CR2, NEF, ARW, PEF, RAF) through <see cref="RawDecoder"/>; every other
|
||||
/// RAW, CR3 and HEIF through the largest suitable embedded JPEG preview; 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
|
||||
/// with it (beside it, or where <see cref="DecodeRequest.Companion"/> says an import put it) when that is larger
|
||||
/// (BRIEF §8.1; VC-5 is out of scope); nothing for formats without either.
|
||||
/// <see cref="DecodeRequest"/> fast paths: JPEG DCT scaling and RAW CFA binning when <see cref="DecodeRequest.MaxLongEdge"/>
|
||||
/// allows, bilinear demosaicing for thumbnails and analysis, and <see cref="DecodeRequest.ForcePreview"/> to skip native
|
||||
/// RAW development. Results fit within MaxLongEdge (never upscaled) and are always RGB.
|
||||
@@ -29,6 +22,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,8 +56,25 @@ public sealed class ManagedDecoderProvider : IImageDecoderProvider
|
||||
return result;
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// The tier for a file whose signature format is already known (the index's): JPEG, PNG and TIFF decode natively
|
||||
/// whatever they hold (damage shows only when decoding), so only RAW and HEIF files are opened.
|
||||
/// </summary>
|
||||
public FileTier Probe(string path, ImageFileFormat format) => Probe(path, format, null);
|
||||
|
||||
/// <inheritdoc cref="Probe(string, ImageFileFormat)"/>
|
||||
/// <param name="companion">Where a GPR's JPEG is kept when it is not beside it (<see cref="DecodeRequest.Companion"/>).</param>
|
||||
public FileTier Probe(string path, ImageFileFormat format, string? companion)
|
||||
{
|
||||
var family = ImageFormatInfo.Of(format).Family;
|
||||
if (family is not (ImageFamily.Jpeg or ImageFamily.Png or ImageFamily.Tiff)) return Probe(path, companion);
|
||||
var decoder = family switch { ImageFamily.Jpeg => "managed-jpeg", ImageFamily.Png => "managed-png", _ => "managed-tiff" };
|
||||
return new FileTier(format, DecodeTier.Native, decoder, null, 0, 0);
|
||||
}
|
||||
|
||||
/// <summary>The tier this provider would use for the file at <paramref name="path"/>, without decoding pixels.</summary>
|
||||
public FileTier Probe(string path)
|
||||
/// <param name="companion">Where a GPR's JPEG is kept when it is not beside it (<see cref="DecodeRequest.Companion"/>).</param>
|
||||
public FileTier Probe(string path, string? companion = null)
|
||||
{
|
||||
using var stream = Open(path);
|
||||
var format = ImageSignature.Detect(stream, path);
|
||||
@@ -88,11 +99,38 @@ 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 && CompanionSize(path, companion) is var (cw, ch) && int.Max(cw, ch) > int.Max(pw, ph))
|
||||
return new FileTier(format, DecodeTier.EmbeddedPreview, "managed-sidecar", reason, cw, ch);
|
||||
if (metadata.LargestPreview is null) return new FileTier(format, DecodeTier.Unavailable, null, reason + "; no embedded preview", 0, 0);
|
||||
return new FileTier(format, DecodeTier.EmbeddedPreview, "managed-preview", reason, pw, ph);
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// The JPEG a GoPro writes with a GPR: <paramref name="companion"/> when the caller found it elsewhere (an import
|
||||
/// keeps it in a sequence of its own) and it is there, else the one beside the GPR (same name, <c>.JPG</c>), or
|
||||
/// null. Only the usual spellings are tried, so a folder of thousands of frames is never listed once per frame.
|
||||
/// </summary>
|
||||
internal static string? CompanionJpeg(string path, 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)
|
||||
{
|
||||
if (File.Exists(stem + extension)) return stem + extension;
|
||||
}
|
||||
return null;
|
||||
}
|
||||
|
||||
/// <summary>Oriented size of the GPR's JPEG (see <see cref="CompanionJpeg"/>), or null when there is none.</summary>
|
||||
/// <exception cref="IOException">The JPEG cannot be read.</exception>
|
||||
private static (int Width, int Height)? CompanionSize(string path, string? companion)
|
||||
{
|
||||
if (CompanionJpeg(path, companion) is not { } file) return null;
|
||||
var jpeg = MetadataReader.Read(file);
|
||||
return jpeg.Format == ImageFileFormat.Jpeg ? jpeg.OrientedSize : null;
|
||||
}
|
||||
|
||||
private static FileStream Open(string path) =>
|
||||
new(path, FileMode.Open, FileAccess.Read, FileShare.ReadWrite | FileShare.Delete, FileBufferSize, FileOptions.RandomAccess);
|
||||
|
||||
@@ -147,7 +185,38 @@ 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);
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// GPR's pixel source: the GoPro's own JPEG (<see cref="DecodeRequest.Companion"/>, else beside it) when it is larger
|
||||
/// than every embedded preview (the camera writes it at full size), decoded with DCT scaling and oriented by its own
|
||||
/// Exif; null when there is none.
|
||||
/// </summary>
|
||||
private static DecodedImage? Companion(Stream raw, DecodeRequest request, CancellationToken cancellationToken)
|
||||
{
|
||||
if (CompanionJpeg(request.Path, 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);
|
||||
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");
|
||||
}
|
||||
|
||||
/// <summary>Embedded-preview tier: the smallest preview whose long edge still reaches MaxLongEdge (the largest when none
|
||||
|
||||
@@ -1,3 +1,5 @@
|
||||
using System.Runtime.ExceptionServices;
|
||||
|
||||
namespace Titano.Core.Imaging;
|
||||
|
||||
/// <summary>How a frame's pixels were obtained (BRIEF §8.2). Logged per frame.</summary>
|
||||
@@ -28,13 +30,16 @@ public enum DecodePurpose
|
||||
/// <param name="ApplyOrientation">Rotate/flip according to the Exif orientation.</param>
|
||||
/// <param name="AllowPreview">Accept the embedded-preview tier when native decode is impossible.</param>
|
||||
/// <param name="ForcePreview">Use the embedded preview even when native decode is possible (fast paths).</param>
|
||||
/// <param name="Companion">The JPEG a GoPro wrote with this GPR when it is kept elsewhere than beside it (an import puts
|
||||
/// it in a sequence of its own); null looks beside the file. Other formats ignore it.</param>
|
||||
public sealed record DecodeRequest(
|
||||
string Path,
|
||||
int MaxLongEdge = 0,
|
||||
DecodePurpose Purpose = DecodePurpose.Render,
|
||||
bool ApplyOrientation = true,
|
||||
bool AllowPreview = true,
|
||||
bool ForcePreview = false);
|
||||
bool ForcePreview = false,
|
||||
string? Companion = null);
|
||||
|
||||
/// <summary>Decoded pixels plus how they were produced. Dispose to return the buffer to the pool.</summary>
|
||||
public sealed class DecodedImage(ImageBuffer pixels, DecodeTier tier, string decoder, int sourceWidth, int sourceHeight) : IDisposable
|
||||
@@ -53,6 +58,15 @@ public sealed class DecodedImage(ImageBuffer pixels, DecodeTier tier, string dec
|
||||
public void Dispose() => Pixels.Dispose();
|
||||
}
|
||||
|
||||
/// <summary>Tier a provider would use for one specific file, with the reason when it is not native.</summary>
|
||||
/// <param name="Format">Format of the file (detected from its signature; the extension only breaks ties).</param>
|
||||
/// <param name="Tier">Best tier available for this file.</param>
|
||||
/// <param name="Decoder">Decoder that would produce it ("managed-cr2", "managed-preview"…), null when unavailable.</param>
|
||||
/// <param name="Reason">Why the native tier is not used (unsupported body or encoding), else null.</param>
|
||||
/// <param name="Width">Full-size width that tier delivers (developed RAW, preview or image size), oriented; 0 when unknown.</param>
|
||||
/// <param name="Height">Full-size height, oriented; 0 when unknown.</param>
|
||||
public sealed record FileTier(ImageFileFormat Format, DecodeTier Tier, string? Decoder, string? Reason, int Width, int Height);
|
||||
|
||||
/// <summary>A platform image decoder (WIC on Windows, managed codecs everywhere).</summary>
|
||||
public interface IImageDecoderProvider
|
||||
{
|
||||
@@ -64,6 +78,24 @@ public interface IImageDecoderProvider
|
||||
|
||||
bool CanDecode(ImageFormatInfo format) => TierFor(format.Format) is not null;
|
||||
|
||||
/// <summary>
|
||||
/// The tier this provider would use for the file at <paramref name="path"/>, whose signature says
|
||||
/// <paramref name="format"/>, without decoding pixels. The default trusts <see cref="TierFor"/>; a provider whose
|
||||
/// support depends on the file itself (camera body, encoding, embedded previews) looks inside it.
|
||||
/// </summary>
|
||||
/// <exception cref="IOException">The file cannot be read.</exception>
|
||||
/// <exception cref="InvalidDataException">The file is malformed where the provider looks.</exception>
|
||||
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);
|
||||
|
||||
/// <summary>
|
||||
/// <see cref="Probe(string, ImageFileFormat)"/> for a file whose camera companion (see
|
||||
/// <see cref="DecodeRequest.Companion"/>) is kept at <paramref name="companion"/>; a provider that uses no
|
||||
/// companion ignores it.
|
||||
/// </summary>
|
||||
FileTier Probe(string path, ImageFileFormat format, string? companion) => Probe(path, format);
|
||||
|
||||
/// <summary>Decodes or returns null when this provider cannot handle the specific file.</summary>
|
||||
DecodedImage? Decode(DecodeRequest request, CancellationToken cancellationToken = default);
|
||||
}
|
||||
@@ -84,11 +116,51 @@ public sealed class ImageDecoder(IReadOnlyList<IImageDecoderProvider> providers)
|
||||
return best;
|
||||
}
|
||||
|
||||
/// <summary>Decodes with the first provider that succeeds; null means <see cref="DecodeTier.Unavailable"/>.</summary>
|
||||
/// <summary>
|
||||
/// The tier this chain would use for the file at <paramref name="path"/> (signature format <paramref name="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 <see cref="Decode"/>.
|
||||
/// </summary>
|
||||
/// <param name="companion">Where a GoPro GPR's JPEG is kept when it is not beside the file (<see cref="DecodeRequest.Companion"/>).</param>
|
||||
/// <exception cref="IOException">The file cannot be read.</exception>
|
||||
public FileTier Probe(string path, ImageFileFormat format, string? companion = null)
|
||||
{
|
||||
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, companion);
|
||||
}
|
||||
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);
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Decodes with the first provider that succeeds; null means <see cref="DecodeTier.Unavailable"/>: the providers read
|
||||
/// the file and none produced pixels. When one could not read it (locked by another program, vanished, a network
|
||||
/// read failed) and none produced pixels, nothing is known about the content, so that read error is thrown instead.
|
||||
/// </summary>
|
||||
/// <exception cref="IOException">The file could not be read and no provider produced pixels.</exception>
|
||||
public DecodedImage? Decode(DecodeRequest request, CancellationToken cancellationToken = default)
|
||||
{
|
||||
var format = ImageFormatInfo.FromPath(request.Path);
|
||||
List<Exception>? errors = null;
|
||||
IOException? unread = null;
|
||||
foreach (var provider in Providers.OrderBy(p => p.TierFor(format.Format) ?? DecodeTier.Unavailable))
|
||||
{
|
||||
if (provider.TierFor(format.Format) is null) continue;
|
||||
@@ -105,8 +177,10 @@ public sealed class ImageDecoder(IReadOnlyList<IImageDecoderProvider> providers)
|
||||
catch (Exception ex) when (ex is IOException or InvalidDataException or NotSupportedException or FormatException or InvalidOperationException or System.Runtime.InteropServices.COMException)
|
||||
{
|
||||
(errors ??= []).Add(ex);
|
||||
if (ex is IOException io) unread ??= io;
|
||||
}
|
||||
}
|
||||
if (unread is not null) ExceptionDispatchInfo.Throw(unread);
|
||||
return null;
|
||||
}
|
||||
}
|
||||
@@ -10,6 +10,19 @@ public enum PreviewKind
|
||||
FullSize,
|
||||
}
|
||||
|
||||
/// <summary>Resolution a camera recorded its RAW at (BRIEF §9 sRaw flags).</summary>
|
||||
public enum RawSize
|
||||
{
|
||||
/// <summary>Full sensor resolution.</summary>
|
||||
Full,
|
||||
|
||||
/// <summary>Canon mRAW (sRAW1), Nikon RAW M.</summary>
|
||||
Medium,
|
||||
|
||||
/// <summary>Canon sRAW (sRAW2), Nikon RAW S and sRAW.</summary>
|
||||
Small,
|
||||
}
|
||||
|
||||
/// <summary>An embedded JPEG inside a RAW/TIFF/HEIF/JPEG(MPF) container.</summary>
|
||||
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; }
|
||||
|
||||
/// <summary>
|
||||
/// 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).
|
||||
/// </summary>
|
||||
public bool? IntervalShooting { get; init; }
|
||||
|
||||
/// <summary>Resolution of a camera RAW from the MakerNote (sRAW/mRAW, RAW M/S); null when not recorded.</summary>
|
||||
public RawSize? RawSize { get; init; }
|
||||
|
||||
/// <summary>Embedded JPEGs, largest first.</summary>
|
||||
public IReadOnlyList<EmbeddedPreview> Previews { get; init; } = [];
|
||||
|
||||
|
||||
@@ -1,9 +1,13 @@
|
||||
using System.Globalization;
|
||||
using Titano.Core.Imaging;
|
||||
using Titano.Core.Metadata.Tiff;
|
||||
|
||||
namespace Titano.Core.Metadata.MakerNotes;
|
||||
|
||||
/// <summary>Canon MakerNote: a bare IFD whose offsets are relative to the enclosing TIFF header (also CR3 CMT3).</summary>
|
||||
/// <summary>
|
||||
/// 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.
|
||||
/// </summary>
|
||||
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,
|
||||
};
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -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<byte> 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,
|
||||
};
|
||||
}
|
||||
}
|
||||
@@ -9,4 +9,14 @@ internal sealed class MakerNoteValues
|
||||
public bool? ImageStabilization { get; set; }
|
||||
public int? Iso { get; set; }
|
||||
public int? ColorTemperature { get; set; }
|
||||
|
||||
/// <summary>The vendor's exposure mode as an Exif <c>ExposureProgram</c> 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.</summary>
|
||||
public int? ExposureProgram { get; set; }
|
||||
|
||||
/// <summary>True when the camera's own interval-shooting or time-lapse mode took the photo, false for another known mode.</summary>
|
||||
public bool? IntervalShooting { get; set; }
|
||||
|
||||
/// <summary>Resolution of a camera RAW (Canon sRAW/mRAW, Nikon RAW M/S).</summary>
|
||||
public RawSize? RawSize { get; set; }
|
||||
}
|
||||
@@ -3,14 +3,19 @@ using Titano.Core.Metadata.Tiff;
|
||||
namespace Titano.Core.Metadata.MakerNotes;
|
||||
|
||||
/// <summary>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.</summary>
|
||||
/// ("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.</summary>
|
||||
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<byte> 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,
|
||||
};
|
||||
}
|
||||
}
|
||||
@@ -6,7 +6,8 @@ namespace Titano.Core.Metadata.MakerNotes;
|
||||
/// <summary>
|
||||
/// 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.
|
||||
/// </summary>
|
||||
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<byte> 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)
|
||||
|
||||
@@ -2,13 +2,17 @@ using Titano.Core.Metadata.Tiff;
|
||||
|
||||
namespace Titano.Core.Metadata.MakerNotes;
|
||||
|
||||
/// <summary>Panasonic MakerNote: "Panasonic\0\0\0" then an IFD whose offsets are relative to the Exif TIFF.</summary>
|
||||
/// <summary>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.</summary>
|
||||
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,
|
||||
};
|
||||
}
|
||||
}
|
||||
@@ -3,12 +3,14 @@ using Titano.Core.Metadata.Tiff;
|
||||
namespace Titano.Core.Metadata.MakerNotes;
|
||||
|
||||
/// <summary>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.</summary>
|
||||
/// Lens: the unencrypted LensSpec (BCD focal/aperture range); numeric LensType tables are not shipped. Exposure mode
|
||||
/// from ExposureMode (0xB041).</summary>
|
||||
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,
|
||||
};
|
||||
}
|
||||
|
||||
/// <summary>Bytes 1–2 short focal, 3–4 long focal (BCD mm), 5–6 apertures (BCD ×10); flags 0 and 7 are not decoded.</summary>
|
||||
|
||||
@@ -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(),
|
||||
};
|
||||
|
||||
@@ -59,6 +59,10 @@ public sealed record Sequence
|
||||
public int FramesVersion { get; init; }
|
||||
public Guid? OwnerUserId { get; init; }
|
||||
public int WarningCount { get; init; }
|
||||
|
||||
/// <summary>Frames that decode only through their embedded preview (<see cref="FrameFlags.PreviewOnly"/>, BRIEF §8.2).</summary>
|
||||
public int PreviewOnlyCount { get; init; }
|
||||
|
||||
public DateTimeOffset UpdatedAt { get; init; }
|
||||
|
||||
/// <summary>The title, else capture date and camera, else the folder name (never empty).</summary>
|
||||
@@ -112,6 +116,12 @@ public sealed record Frame
|
||||
public long? PreviewOffset { get; init; }
|
||||
public long? PreviewLength { get; init; }
|
||||
|
||||
/// <summary>Size of the largest embedded preview like <see cref="Width"/> and <see cref="Height"/> (before orientation);
|
||||
/// 0 when there is none or it is not known.</summary>
|
||||
public int PreviewWidth { get; init; }
|
||||
|
||||
public int PreviewHeight { get; init; }
|
||||
|
||||
/// <summary>Mean log2 luminance once analysed.</summary>
|
||||
public double? MeanLogLuminance { get; init; }
|
||||
|
||||
|
||||
@@ -24,6 +24,12 @@ public enum FrameFlags
|
||||
Undecodable = 32,
|
||||
Black = 64,
|
||||
White = 128,
|
||||
|
||||
/// <summary>
|
||||
/// 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).
|
||||
/// </summary>
|
||||
Intervalometer = 256,
|
||||
}
|
||||
|
||||
[Flags]
|
||||
|
||||
@@ -35,7 +35,8 @@ public enum ProxyStoreResult
|
||||
/// 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. A frame whose original this host cannot
|
||||
/// decode (<see cref="NeedsProxy(ImageDecoder, ImageFileFormat)"/>) gets its proxy from a client or render node that can
|
||||
/// (<see cref="StoreProxy"/>, BRIEF §2.3) and its thumbnail from that proxy.
|
||||
/// (<see cref="StoreProxy"/>, BRIEF §2.3) and its thumbnail from that proxy. A frame nothing decodes is flagged
|
||||
/// undecodable (BRIEF §8.2 tier 3); one flagged so that decodes after all gets its tier probed again.
|
||||
/// </summary>
|
||||
public sealed class ArchiveImages
|
||||
{
|
||||
@@ -51,6 +52,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)
|
||||
{
|
||||
@@ -58,6 +60,7 @@ public sealed class ArchiveImages
|
||||
_writer = writer;
|
||||
_indexer = indexer;
|
||||
_renderer = new FrameImageRenderer(writer.Options.Decoder);
|
||||
_tiers = new FrameTiers(db, writer, indexer);
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
@@ -156,6 +159,8 @@ 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<Guid, FileTier>();
|
||||
var companions = await GprCompanions.FindAsync(_db, archive, manifest.Frames, cancellationToken).ConfigureAwait(false);
|
||||
// Thumbnail writers exclude each other only; ingest keeps committing meanwhile.
|
||||
using (await LeaseFile.AcquireAsync(paths.LocksDirectory, $"thumbs-{sequence.Id:N}", _writer.Options.Owner, _writer.Options.LeaseWait,
|
||||
_writer.Options.LeaseDuration, cancellationToken).ConfigureAwait(false))
|
||||
@@ -181,8 +186,12 @@ public sealed class ArchiveImages
|
||||
reused++;
|
||||
continue;
|
||||
}
|
||||
var source = fromProxy ? ProxySource(frame) : frame;
|
||||
if (Render(() => _renderer.Thumbnail(folder, source, policy, cancellationToken)) is { } image)
|
||||
var companion = companions.GetValueOrDefault(frame.Id);
|
||||
// A thumbnail drawn from the proxy says nothing about how this host decodes the original.
|
||||
var image = fromProxy
|
||||
? Render(folder, frame, null, null, () => _renderer.Thumbnail(folder, ProxySource(frame), policy, cancellationToken))
|
||||
: Render(folder, frame, companion, tiers, () => _renderer.Thumbnail(folder, frame, policy, cancellationToken, companion));
|
||||
if (image is not null)
|
||||
{
|
||||
pack.Append(frame.I, image.Width, image.Height, fingerprint, image.Jpeg);
|
||||
built++;
|
||||
@@ -199,6 +208,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);
|
||||
}
|
||||
@@ -215,6 +225,8 @@ 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<Guid, FileTier>();
|
||||
var companions = await GprCompanions.FindAsync(_db, archive, manifest.Frames, cancellationToken).ConfigureAwait(false);
|
||||
var cadence = new Cadence();
|
||||
for (var i = startFrame; i < manifest.Frames.Count; i++)
|
||||
{
|
||||
@@ -243,7 +255,8 @@ public sealed class ArchiveImages
|
||||
reused++;
|
||||
continue;
|
||||
}
|
||||
else if (Render(() => _renderer.Proxy(folder, frame, manifest.Camera, policy, cancellationToken)) is { } image)
|
||||
else if (Render(folder, frame, companions.GetValueOrDefault(frame.Id), tiers,
|
||||
() => _renderer.Proxy(folder, frame, manifest.Camera, policy, cancellationToken, companions.GetValueOrDefault(frame.Id))) is { } image)
|
||||
{
|
||||
AtomicFile.WriteAllBytes(path, image.Jpeg);
|
||||
built++;
|
||||
@@ -254,6 +267,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);
|
||||
}
|
||||
@@ -330,18 +344,53 @@ public sealed class ArchiveImages
|
||||
}
|
||||
}
|
||||
|
||||
/// <summary>A frame no decoder can read is skipped and counted; it never stops the pass.</summary>
|
||||
private static EncodedImage? Render(Func<EncodedImage?> render)
|
||||
/// <summary>
|
||||
/// A frame no decoder can read is skipped and counted; it never stops the pass. What decoding found goes into
|
||||
/// <paramref name="tiers"/> (when given): unavailable when nothing decoded a file that is there (a read error proves
|
||||
/// nothing), and the probed tier when a frame flagged undecodable decoded after all.
|
||||
/// </summary>
|
||||
private EncodedImage? Render(SequencePaths folder, SidecarFrame frame, string? companion, Dictionary<Guid, FileTier>? tiers, Func<EncodedImage?> 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 (Exception ex) when (ex is IOException or UnauthorizedAccessException)
|
||||
{
|
||||
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 (tiers is null) return image;
|
||||
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, companion, image.Tier);
|
||||
}
|
||||
return image;
|
||||
}
|
||||
|
||||
/// <summary>The tier of a frame that decoded after it was flagged undecodable (the image's own tier when probing finds none).</summary>
|
||||
private FileTier Reprobe(string path, SidecarFrame frame, string? companion, DecodeTier decoded)
|
||||
{
|
||||
try
|
||||
{
|
||||
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)
|
||||
{
|
||||
Core.Diagnostics.Log.Warning("archive", $"{path} could not be probed", ex);
|
||||
}
|
||||
return new FileTier(frame.Format, decoded, null, null, 0, 0);
|
||||
}
|
||||
|
||||
/// <summary>Commit/checkpoint rhythm: every <see cref="CommitFrames"/> rendered frames or <see cref="CommitInterval"/>.</summary>
|
||||
|
||||
@@ -0,0 +1,105 @@
|
||||
using Titano.Core.Archiving;
|
||||
using Titano.Core.Imaging;
|
||||
using Titano.Core.Model;
|
||||
using Titano.Data.Repositories;
|
||||
|
||||
namespace Titano.Data.Archiving;
|
||||
|
||||
/// <summary>
|
||||
/// Decode tiers of archived frames (BRIEF §8.2) written to the sidecar and the index together: the tier, the
|
||||
/// <see cref="FrameFlags.PreviewOnly"/> and <see cref="FrameFlags.Undecodable"/> 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.
|
||||
/// </summary>
|
||||
internal sealed class FrameTiers(TitanoDatabase db, SequenceWriter writer, ArchiveIndexer indexer)
|
||||
{
|
||||
/// <summary>
|
||||
/// The tier of every frame of <paramref name="manifest"/> whose tier is not known yet, that decodes only through a
|
||||
/// preview whose size is not known (the output cap needs it), or that is a GPR whose JPEG in another sequence
|
||||
/// (<see cref="GprCompanions"/>; an upload may index it after the GPR was probed) is larger than its stored preview,
|
||||
/// probed with the host's decoders without decoding pixels; missing and unreadable files are left for later.
|
||||
/// </summary>
|
||||
public async Task<Dictionary<Guid, FileTier>> ProbeUnknownAsync(Archive archive, SequencePaths folder, SequenceManifest manifest,
|
||||
CancellationToken cancellationToken)
|
||||
{
|
||||
var tiers = new Dictionary<Guid, FileTier>();
|
||||
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)
|
||||
{
|
||||
cancellationToken.ThrowIfCancellationRequested();
|
||||
var path = folder.File(frame.File);
|
||||
try
|
||||
{
|
||||
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;
|
||||
}
|
||||
catch (Exception ex) when (ex is IOException or UnauthorizedAccessException)
|
||||
{
|
||||
Core.Diagnostics.Log.Warning("archive", $"{path} could not be probed", ex);
|
||||
}
|
||||
}
|
||||
return tiers;
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Records <paramref name="tiers"/> (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.
|
||||
/// </summary>
|
||||
/// <exception cref="ArchiveOfflineException">The archive is offline or read-only.</exception>
|
||||
public async Task<IReadOnlyList<SidecarFrame>> RecordAsync(Archive archive, Sequence sequence, IReadOnlyDictionary<Guid, FileTier> tiers,
|
||||
CancellationToken cancellationToken)
|
||||
{
|
||||
if (tiers.Count == 0) return [];
|
||||
var paths = new ArchivePaths(archive.RootPath);
|
||||
ArchiveVolume.EnsureWritable(paths);
|
||||
var changed = new List<SidecarFrame>();
|
||||
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;
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Whether this host decodes <paramref name="format"/> 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).
|
||||
/// </summary>
|
||||
private bool DecodesNatively(ImageFileFormat format) =>
|
||||
format == ImageFileFormat.Jpeg || writer.Options.Decoder.TierFor(format) == DecodeTier.Native;
|
||||
}
|
||||
@@ -0,0 +1,63 @@
|
||||
using Titano.Core.Archiving;
|
||||
using Titano.Core.Imaging;
|
||||
using Titano.Core.Model;
|
||||
|
||||
namespace Titano.Data.Archiving;
|
||||
|
||||
/// <summary>A GPR's JPEG: its absolute path and its size as the index has it (before orientation).</summary>
|
||||
internal readonly record struct GprCompanion(string Path, int Width, int Height);
|
||||
|
||||
/// <summary>
|
||||
/// The JPEG a GoPro writes with each GPR, the server's pixel source for GPR (BRIEF §8.1; VC-5 is out of scope). In a
|
||||
/// folder adopted in place it sits beside the GPR, where the decoder finds it itself. A card import or an upload splits
|
||||
/// sessions per file format (DECISIONS 2026-10-02), so there it is a frame of another sequence: the archive's JPEG frame
|
||||
/// whose original name has the GPR's stem and whose capture time is the GPR's (within a second). Looked up in the index
|
||||
/// when frames are decoded, so renaming or moving either sequence never breaks the pair.
|
||||
/// </summary>
|
||||
public static class GprCompanions
|
||||
{
|
||||
private static readonly TimeSpan Tolerance = TimeSpan.FromSeconds(1);
|
||||
|
||||
/// <summary>
|
||||
/// The absolute path of the JPEG of each GPR frame of <paramref name="frames"/> (index rows), by frame id; frames
|
||||
/// without one are left out. One index query, none when no frame is a GPR.
|
||||
/// </summary>
|
||||
public static async Task<IReadOnlyDictionary<Guid, string>> FindAsync(TitanoDatabase db, Archive archive, IReadOnlyList<Frame> frames,
|
||||
CancellationToken cancellationToken) =>
|
||||
Paths(await FindAsync(db, archive, [.. frames.Where(f => f.Format == ImageFileFormat.Gpr).Select(f => (f.Id, f.OriginalFileName ?? f.FileName, f.CapturedAt))],
|
||||
cancellationToken).ConfigureAwait(false));
|
||||
|
||||
/// <inheritdoc cref="FindAsync(TitanoDatabase, Archive, IReadOnlyList{Frame}, CancellationToken)"/>
|
||||
public static async Task<IReadOnlyDictionary<Guid, string>> FindAsync(TitanoDatabase db, Archive archive, IReadOnlyList<SidecarFrame> frames,
|
||||
CancellationToken cancellationToken) =>
|
||||
Paths(await FindWithSizesAsync(db, archive, frames, cancellationToken).ConfigureAwait(false));
|
||||
|
||||
/// <summary>The JPEG of each GPR frame of <paramref name="frames"/> (sidecar frames) with its indexed size, by frame id.</summary>
|
||||
internal static Task<Dictionary<Guid, GprCompanion>> FindWithSizesAsync(TitanoDatabase db, Archive archive, IReadOnlyList<SidecarFrame> frames,
|
||||
CancellationToken cancellationToken) =>
|
||||
FindAsync(db, archive, [.. frames.Where(f => f.Format == ImageFileFormat.Gpr).Select(f => (f.Id, f.Original ?? Path.GetFileName(f.File), f.CapturedAt))],
|
||||
cancellationToken);
|
||||
|
||||
private static async Task<Dictionary<Guid, GprCompanion>> FindAsync(TitanoDatabase db, Archive archive,
|
||||
List<(Guid Id, string Name, DateTime? CapturedAt)> gprs, CancellationToken cancellationToken)
|
||||
{
|
||||
var found = new Dictionary<Guid, GprCompanion>();
|
||||
var timed = gprs.Where(g => g.CapturedAt is not null).Select(g => (g.Id, Stem: Stem(g.Name), At: g.CapturedAt!.Value)).ToList();
|
||||
if (timed.Count == 0) return found;
|
||||
var jpegs = await db.Frames.ListByCaptureRangeAsync(archive.Id, ImageFileFormat.Jpeg, timed.Min(g => g.At) - Tolerance,
|
||||
timed.Max(g => g.At) + Tolerance, cancellationToken).ConfigureAwait(false);
|
||||
var byStem = jpegs.Where(j => (j.Flags & FrameFlags.Missing) == 0 && j.CapturedAt is not null)
|
||||
.ToLookup(j => Stem(j.OriginalFileName ?? j.FileName), StringComparer.OrdinalIgnoreCase);
|
||||
var paths = new ArchivePaths(archive.RootPath);
|
||||
foreach (var (id, stem, at) in timed)
|
||||
{
|
||||
var match = byStem[stem].Where(j => (j.CapturedAt!.Value - at).Duration() <= Tolerance).MinBy(j => (j.CapturedAt!.Value - at).Duration());
|
||||
if (match is not null) found[id] = new GprCompanion(paths.FromRelative(match.RelativePath), match.Width, match.Height);
|
||||
}
|
||||
return found;
|
||||
}
|
||||
|
||||
private static Dictionary<Guid, string> Paths(Dictionary<Guid, GprCompanion> companions) => companions.ToDictionary(kv => kv.Key, kv => kv.Value.Path);
|
||||
|
||||
private static string Stem(string fileName) => Path.GetFileNameWithoutExtension(fileName);
|
||||
}
|
||||
@@ -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),
|
||||
};
|
||||
}
|
||||
|
||||
|
||||
@@ -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;
|
||||
|
||||
@@ -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;
|
||||
@@ -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;
|
||||
|
||||
@@ -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;
|
||||
@@ -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;
|
||||
/// <see cref="JobKind.Render"/> on this machine (BRIEF §11.5): target = the sequence; parameters = Protocol
|
||||
/// <c>RenderJobParameters</c> 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
|
||||
/// <c>exports/</c> (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 <c>exports/</c> (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.
|
||||
/// </summary>
|
||||
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;
|
||||
|
||||
@@ -63,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, options.Archive.Decoder, cancellation).ConfigureAwait(false);
|
||||
var companions = await GprCompanions.FindAsync(db, archive, frames.Frames, cancellation).ConfigureAwait(false);
|
||||
IReadOnlyList<TargetOutcome> outcomes;
|
||||
await using (var render = new SequenceRender(frames, project, SequenceAnalyser.ReadReport(folder, frames.Count), options))
|
||||
await using (var render = new SequenceRender(frames, project, SequenceAnalyser.ReadReport(folder, frames.Count), options, originals: !parameters.UseProxies,
|
||||
companions))
|
||||
{
|
||||
outcomes = await render.RenderAllAsync(
|
||||
targets,
|
||||
@@ -87,6 +91,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));
|
||||
|
||||
@@ -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
|
||||
/// <summary>Handlers for <see cref="JobKind.LightAnalyse"/>, <see cref="JobKind.DeepAnalyse"/> and <see cref="JobKind.Render"/>.</summary>
|
||||
public IReadOnlyList<IJobHandler> CreateJobHandlers()
|
||||
{
|
||||
var render = new RenderJobHandler(_db, Options);
|
||||
var render = new RenderJobHandler(_db, Options, _tiers);
|
||||
return
|
||||
[
|
||||
new AnalyseJobHandler(_db, _analyser, JobKind.LightAnalyse),
|
||||
|
||||
@@ -565,6 +565,8 @@ public sealed class RenderNodeClient : IAsyncDisposable
|
||||
Height = f.Height,
|
||||
Format = Enum.TryParse<ImageFileFormat>(f.Format, ignoreCase: true, out var format) ? format : default,
|
||||
Flags = (FrameFlags)f.Flags,
|
||||
PreviewWidth = f.PreviewWidth,
|
||||
PreviewHeight = f.PreviewHeight,
|
||||
};
|
||||
|
||||
/// <summary>
|
||||
|
||||
@@ -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,20 @@ internal sealed record AnalysisOutcome(Sequence Sequence, int Frames, int Warnin
|
||||
|
||||
/// <summary>
|
||||
/// 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 <c>.titano/analysis.partial</c> so a cancelled, paused or crashed run resumes, and
|
||||
/// saves <c>analysis.json</c> atomically. Results land in the sidecar status, the index (status, warning count, compact
|
||||
/// summary, per-frame light statistics) and <c>project.json</c>.
|
||||
/// 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 <c>.titano/analysis.partial</c> so a cancelled, paused or crashed run resumes, records the tier
|
||||
/// each frame decoded at (or that it did not decode) and saves <c>analysis.json</c> atomically. Results land in the
|
||||
/// sidecar (status, tiers), the index (status, warning count, compact summary, tier flags, per-frame light statistics)
|
||||
/// and <c>project.json</c>.
|
||||
/// </summary>
|
||||
internal sealed class SequenceAnalyser(TitanoDatabase db, ProcessingOptions options, SequenceWriter writer, ArchiveIndexer indexer, ProjectStore projects)
|
||||
{
|
||||
/// <summary>Long edge of the decode that confirms a frame an interrupted earlier run found undecodable.</summary>
|
||||
private const int ConfirmLongEdge = 64;
|
||||
|
||||
private readonly FrameTiers _tiers = new(db, writer, indexer);
|
||||
|
||||
/// <summary>Analyses one sequence; null when it is no longer in the index.</summary>
|
||||
/// <exception cref="ArchiveOfflineException">The archive is offline or read-only.</exception>
|
||||
public async Task<AnalysisOutcome?> AnalyseAsync(Guid sequenceId, bool deep, IProgress<AnalysisProgress>? progress, CancellationToken cancellationToken)
|
||||
@@ -37,12 +45,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, options.Archive.Decoder, cancellationToken).ConfigureAwait(false);
|
||||
// Format tiers (BRIEF §10.1): frames indexed since the last analysis are probed with this host's decoders.
|
||||
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();
|
||||
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 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
|
||||
{
|
||||
report = ReadReport(folder, frames.Count);
|
||||
}
|
||||
var light = LightAnalyzer.Analyze(metadata);
|
||||
var context = SequenceContext.FromFrames(frames.Frames, options.AdvisorCodecs);
|
||||
|
||||
bool changed, unreadable = false;
|
||||
@@ -100,18 +119,64 @@ internal sealed class SequenceAnalyser(TitanoDatabase db, ProcessingOptions opti
|
||||
}
|
||||
}
|
||||
|
||||
private async Task<DeepAnalysisReport> AnalyseDeepAsync(SequenceFrames frames, IReadOnlyList<LightFrame> metadata, SequencePaths folder,
|
||||
IProgress<AnalysisProgress>? progress, CancellationToken cancellationToken)
|
||||
/// <summary>The frame rows with the tier fields of <paramref name="changed"/> sidecar frames.</summary>
|
||||
private static SequenceFrames WithTiers(SequenceFrames frames, IReadOnlyList<SidecarFrame> 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 };
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// What the deep pass learnt about each frame's tier, by frame id: the tier this run decoded it at. A frame this run
|
||||
/// tried to read and recorded nothing for vanished or could not be read, which says nothing about its content. A frame
|
||||
/// only an interrupted earlier run analysed and found undecodable is decoded again (small) to confirm it: that run's
|
||||
/// failure may have been a read error too. Missing and corrupt frames are not decoded at all.
|
||||
/// </summary>
|
||||
private Dictionary<Guid, FileTier> Decoded(SequenceFrames frames, DeepAnalysisReport report, SequenceFrameSource source,
|
||||
IReadOnlyDictionary<Guid, string> companions, CancellationToken cancellationToken)
|
||||
{
|
||||
var tiers = new Dictionary<Guid, FileTier>();
|
||||
foreach (var (index, tier) in source.Observed) tiers[frames.Frames[index].Id] = tier;
|
||||
var read = source.Read.ToHashSet();
|
||||
foreach (var index in report.Issues.Undecodable)
|
||||
{
|
||||
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];
|
||||
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, Dictionary<Guid, FileTier> Decoded)> AnalyseDeepAsync(Archive archive, SequenceFrames frames,
|
||||
IReadOnlyList<LightFrame> metadata, SequencePaths folder, IProgress<AnalysisProgress>? progress, CancellationToken cancellationToken)
|
||||
{
|
||||
var file = new DeepAnalysisCheckpointFile(Path.Combine(folder.MetaDirectory, DeepAnalysisCheckpointFile.FileName));
|
||||
// The host's analysis parameters as they are now (Settings › Analysis on the desktop). Those that change per-frame
|
||||
// results are part of the checkpoint's fingerprint, so a partial run made with other values starts again.
|
||||
var analysis = options.Current.DeepAnalysis;
|
||||
var companions = await GprCompanions.FindAsync(db, archive, frames.Frames, cancellationToken).ConfigureAwait(false);
|
||||
DeepAnalysisReport report;
|
||||
await using (var source = new SequenceFrameSource(frames, metadata, options.Archive.Decoder))
|
||||
Dictionary<Guid, FileTier> decoded;
|
||||
await using (var source = new SequenceFrameSource(frames, metadata, options.Archive.Decoder) { Originals = true, Companions = companions })
|
||||
{
|
||||
report = await DeepAnalyzer.AnalyzeAsync(source, analysis, new FramesCheckpoint(file, ContentKey(frames)), progress, cancellationToken)
|
||||
.ConfigureAwait(false);
|
||||
decoded = Decoded(frames, report, source, companions, cancellationToken);
|
||||
}
|
||||
report.Save(folder.AnalysisPath);
|
||||
file.Delete();
|
||||
@@ -121,7 +186,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, decoded);
|
||||
}
|
||||
|
||||
/// <summary>Identity of the analysed content: frames replaced or renumbered since a checkpoint was written (with the
|
||||
|
||||
@@ -1,3 +1,4 @@
|
||||
using System.Collections.Concurrent;
|
||||
using System.Globalization;
|
||||
using Titano.Core.Advice;
|
||||
using Titano.Core.Analysis;
|
||||
@@ -10,9 +11,12 @@ namespace Titano.Data.Processing;
|
||||
/// <summary>
|
||||
/// The frames of a <see cref="SequenceFrames"/> decoded by the host's <see cref="ImageDecoder"/> (oriented, linear
|
||||
/// light, embedded previews when nothing else decodes). For the deep analysis a frame that is missing, corrupt or
|
||||
/// unreadable is null, as <see cref="IFrameSource"/> expects. Renders exclude flagged frames from the plan, so a
|
||||
/// frame that still cannot be decoded stops the render with <see cref="FrameUnreadableKey"/>; render reads go through
|
||||
/// a <see cref="FrameWindow"/> per decode size, which decodes ahead in index order and spills under memory pressure.
|
||||
/// unreadable is null, as <see cref="IFrameSource"/> expects. Renders exclude flagged frames from the plan; a frame that
|
||||
/// still cannot be decoded stops the render with <see cref="FrameUnreadableKey"/>, unless the render gave
|
||||
/// <see cref="RenderExcluded"/>: then the nearest planned frame that decodes stands in for it (BRIEF §8.2 tier 3: one
|
||||
/// unreadable frame never stops a render) and it is listed in <see cref="StandIns"/>. Render reads go through a
|
||||
/// <see cref="FrameWindow"/> per decode size, which decodes ahead in index order and spills under memory pressure; only
|
||||
/// frames the pipeline asks for get a stand-in, not those the read-ahead met on the way (a speed-up skips them).
|
||||
/// </summary>
|
||||
internal sealed class SequenceFrameSource : IFrameSource, IRenderFrameSource, IAsyncDisposable
|
||||
{
|
||||
@@ -27,6 +31,10 @@ 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<int, FileTier> _observed = new();
|
||||
private readonly ConcurrentDictionary<int, byte> _standIns = new();
|
||||
private readonly ConcurrentDictionary<int, byte> _failed = new();
|
||||
private readonly ConcurrentDictionary<int, byte> _read = new();
|
||||
private FrameWindow? _window;
|
||||
private (int Edge, DecodePurpose Purpose) _windowKey;
|
||||
|
||||
@@ -40,6 +48,37 @@ internal sealed class SequenceFrameSource : IFrameSource, IRenderFrameSource, IA
|
||||
_size = new Lazy<(int, int)>(MeasureSize);
|
||||
}
|
||||
|
||||
/// <summary>The files are the archive's originals, so what decoding finds describes the frames (<see cref="Observed"/>).</summary>
|
||||
public bool Originals { get; init; }
|
||||
|
||||
/// <summary>
|
||||
/// 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.
|
||||
/// </summary>
|
||||
public IReadOnlySet<int>? RenderExcluded { get; init; }
|
||||
|
||||
/// <summary>
|
||||
/// The JPEG a GoPro wrote with each GPR frame that is not beside it, by frame id (<see cref="DecodeRequest.Companion"/>).
|
||||
/// </summary>
|
||||
public IReadOnlyDictionary<Guid, string>? Companions { get; init; }
|
||||
|
||||
/// <summary>
|
||||
/// Decode tier each frame decoded at, or <see cref="DecodeTier.Unavailable"/> when the decoders read its file and none
|
||||
/// produced pixels; recorded only for <see cref="Originals"/>. Sizes are oriented and given only where they are the
|
||||
/// full size the tier delivers: a size-limited decode of a preview picks the smallest one that reaches the limit, so
|
||||
/// it reports none and the stored size of the largest preview stays.
|
||||
/// </summary>
|
||||
public IReadOnlyDictionary<int, FileTier> Observed => _observed;
|
||||
|
||||
/// <summary>
|
||||
/// Positions whose file this source tried to decode, whatever came of it (a read error or a vanished file records no
|
||||
/// tier in <see cref="Observed"/>).
|
||||
/// </summary>
|
||||
public IReadOnlyCollection<int> Read => [.. _read.Keys];
|
||||
|
||||
/// <summary>Positions a render showed a neighbour for because they did not decode, ascending.</summary>
|
||||
public IReadOnlyList<int> StandIns => [.. _standIns.Keys.Order()];
|
||||
|
||||
public int Count => _frames.Count;
|
||||
|
||||
public int FrameCount => _frames.Count;
|
||||
@@ -66,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
|
||||
{
|
||||
@@ -92,35 +131,107 @@ 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);
|
||||
/// <summary>
|
||||
/// The window's loader, for the pipeline's reads and the read-ahead alike: the decoded frame, else
|
||||
/// <see cref="NotDecodedException"/>, remembered so the read-ahead does not decode the file again.
|
||||
/// </summary>
|
||||
private ImageBuffer Load(int index, int maxLongEdge, DecodePurpose purpose, CancellationToken cancellationToken)
|
||||
{
|
||||
if (!_failed.ContainsKey(index) && Decode(index, maxLongEdge, purpose, cancellationToken) is { } decoded) return decoded.Pixels;
|
||||
_failed.TryAdd(index, 0);
|
||||
throw new NotDecodedException();
|
||||
}
|
||||
|
||||
private DecodedImage? Decode(int index, int maxLongEdge, DecodePurpose purpose, CancellationToken cancellationToken)
|
||||
/// <summary>A copy of the window's frame, or null when it does not decode.</summary>
|
||||
private static async ValueTask<ImageBuffer?> ReadAsync(FrameWindow window, int index, CancellationToken cancellationToken)
|
||||
{
|
||||
if (_frames.Files[index] is not { } file) return null;
|
||||
try
|
||||
{
|
||||
return _decoder.Decode(new DecodeRequest(file, maxLongEdge, purpose), cancellationToken);
|
||||
using var lease = await window.GetAsync(index, cancellationToken).ConfigureAwait(false);
|
||||
return lease.Image.Clone();
|
||||
}
|
||||
catch (Exception ex) when (ex is IOException or UnauthorizedAccessException)
|
||||
catch (NotDecodedException)
|
||||
{
|
||||
Core.Diagnostics.Log.Warning("processing", $"Frame {file} could not be read", ex);
|
||||
return null;
|
||||
}
|
||||
}
|
||||
|
||||
/// <summary>The dominant oriented size from the metadata, else the size the first renderable frame decodes to.</summary>
|
||||
/// <summary>
|
||||
/// The pixels shown for a frame the pipeline asked for that does not decode. Flagged frames are not planned, so this
|
||||
/// one became unreadable after it was last analysed (or is beyond this host's decoders): the nearest planned frame
|
||||
/// that decodes stands in, earlier first, and the render job records what decoding found. Without
|
||||
/// <see cref="RenderExcluded"/>, or when nothing decodes, the render stops.
|
||||
/// </summary>
|
||||
private async ValueTask<ImageBuffer> StandInAsync(FrameWindow window, int index, CancellationToken cancellationToken)
|
||||
{
|
||||
if (RenderExcluded is not { } excluded || excluded.Contains(index)) throw Unreadable(index);
|
||||
for (var distance = 1; distance < Count; distance++)
|
||||
{
|
||||
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 (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;
|
||||
}
|
||||
}
|
||||
throw Unreadable(index);
|
||||
}
|
||||
|
||||
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];
|
||||
// A frame read from its proxy (an original this host cannot decode) says nothing about its original.
|
||||
var proxied = _frames.Proxied is { } proxies && proxies[index];
|
||||
DecodedImage? decoded;
|
||||
try
|
||||
{
|
||||
var companion = proxied ? null : 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 && !proxied && (decoded is not null || File.Exists(file)))
|
||||
{
|
||||
_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;
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// 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).
|
||||
/// </summary>
|
||||
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.");
|
||||
}
|
||||
|
||||
@@ -130,4 +241,7 @@ internal sealed class SequenceFrameSource : IFrameSource, IRenderFrameSource, IA
|
||||
return new RenderException(FrameUnreadableKey, $"Frame {index + 1} ({name}) could not be decoded.",
|
||||
[(index + 1).ToString(CultureInfo.InvariantCulture), name]);
|
||||
}
|
||||
|
||||
/// <summary>A frame of the window that does not decode; <see cref="GetFrameAsync"/> resolves it, nothing else sees it.</summary>
|
||||
private sealed class NotDecodedException : Exception;
|
||||
}
|
||||
@@ -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,25 +31,41 @@ internal sealed class SequenceRender : IAsyncDisposable
|
||||
private readonly RenderOptions _options;
|
||||
|
||||
/// <param name="analysis">The sequence's deep report, when current: its luminance spares the preparation a decode pass.</param>
|
||||
public SequenceRender(SequenceFrames frames, ProjectSettings project, DeepAnalysisReport? analysis, ProcessingOptions options)
|
||||
/// <param name="originals">The frames' files are the archive's originals (no proxies), so <see cref="Decoded"/> describes them.</param>
|
||||
/// <param name="companions">The JPEG of each GPR frame kept in another sequence, by frame id (<see cref="Archiving.GprCompanions"/>).</param>
|
||||
public SequenceRender(SequenceFrames frames, ProjectSettings project, DeepAnalysisReport? analysis, ProcessingOptions options, bool originals = false,
|
||||
IReadOnlyDictionary<Guid, string>? companions = null)
|
||||
{
|
||||
// The host's tuning as it is now (memory budget, quality profile): a change applies from the next render.
|
||||
options = options.Current;
|
||||
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(),
|
||||
Companions = companions,
|
||||
};
|
||||
_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; }
|
||||
|
||||
/// <summary>Positions that did not decode during the render; a neighbour stood in for each (ascending).</summary>
|
||||
public IReadOnlyList<int> StandIns => _source.StandIns;
|
||||
|
||||
/// <summary>The decode tier each frame was read at so far (unavailable when it did not decode); originals only.</summary>
|
||||
public IReadOnlyDictionary<int, FileTier> Decoded => _source.Observed;
|
||||
|
||||
/// <summary>Encoder, geometry and plan of an export before its output is named.</summary>
|
||||
public RenderSetup Setup(ResolvedExport export) => Pipeline(export).Setup(Input, new RenderTarget(export, ExportNaming.FallbackName), _options);
|
||||
|
||||
|
||||
@@ -19,6 +19,9 @@ public sealed record DuplicateFrame(string Sha256, Guid FrameId, Guid SequenceId
|
||||
/// <summary>Light statistics of one frame, written by the analysis job.</summary>
|
||||
public readonly record struct FrameLightStats(Guid FrameId, double? MeanLogLuminance, double? ClipLow, double? ClipHigh);
|
||||
|
||||
/// <summary>Decode-tier fields of one frame (BRIEF §8.2): its flags and the stored size of its largest embedded preview.</summary>
|
||||
public readonly record struct FrameTierFields(Guid FrameId, FrameFlags Flags, int PreviewWidth, int PreviewHeight);
|
||||
|
||||
/// <summary>Per-format totals and flagged-frame counts of one sequence.</summary>
|
||||
public sealed record SequenceFrameStats(IReadOnlyList<FormatBucket> ByFormat, long Missing, long Corrupt, long Duplicate, long Excluded);
|
||||
|
||||
@@ -30,7 +33,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);
|
||||
@@ -40,7 +43,7 @@ public sealed class FrameRepository
|
||||
/// <summary>Rows per statement of the batched PostgreSQL updates (4 or fewer parameters per row).</summary>
|
||||
private const int Batch = 1000;
|
||||
|
||||
/// <summary>Rows per multi-row upsert: 30 x 27 parameters stays below SQLite's historical 999-variable limit.</summary>
|
||||
/// <summary>Rows per multi-row upsert: 30 x 29 parameters stays below SQLite's historical 999-variable limit.</summary>
|
||||
private const int UpsertRows = 30;
|
||||
|
||||
/// <summary>Light statistics belong to the analysis job; a sync from the sidecar never clears them.</summary>
|
||||
@@ -138,6 +141,44 @@ public sealed class FrameRepository
|
||||
return updated;
|
||||
}, cancellationToken);
|
||||
|
||||
/// <summary>Writes the decode-tier fields of many frames in one transaction; returns the number of frames updated.</summary>
|
||||
public Task<int> UpdateTiersAsync(IReadOnlyList<FrameTierFields> frames, CancellationToken cancellationToken = default) =>
|
||||
_db.InTransactionAsync(async session =>
|
||||
{
|
||||
var updated = 0;
|
||||
if (_db.Sql.IsPostgres)
|
||||
{
|
||||
// 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);
|
||||
|
||||
/// <summary>
|
||||
/// Makes the frames of a sequence exactly <paramref name="frames"/> (from its sidecar): rows are upserted by id,
|
||||
/// rows not listed are deleted, light statistics of kept rows survive. Indexes may be renumbered freely.
|
||||
@@ -181,6 +222,15 @@ public sealed class FrameRepository
|
||||
"WHERE s.archive_id = $1 AND f.size = $2 AND f.captured_at = $3",
|
||||
[archiveId, size, SequenceRepository.Wall(capturedAt)], Map, cancellationToken);
|
||||
|
||||
/// <summary>Frames of an archive in this format captured from <paramref name="from"/> to <paramref name="to"/> (inclusive),
|
||||
/// by capture time (a GoPro GPR's JPEG in another sequence).</summary>
|
||||
public Task<IReadOnlyList<Frame>> ListByCaptureRangeAsync(Guid archiveId, ImageFileFormat format, DateTime from, DateTime to,
|
||||
CancellationToken cancellationToken = default) =>
|
||||
_db.ListAsync(
|
||||
$"SELECT {string.Join(", ", ColumnNames.Select(c => "f." + c))} FROM frames f JOIN sequences s ON s.id = f.sequence_id " +
|
||||
"WHERE s.archive_id = $1 AND f.format = $2 AND f.captured_at >= $3 AND f.captured_at <= $4 ORDER BY f.captured_at, f.id",
|
||||
[archiveId, (int)format, SequenceRepository.Wall(from), SequenceRepository.Wall(to)], Map, cancellationToken);
|
||||
|
||||
/// <summary>Every frame of an archive whose SHA-256 occurs more than once, grouped by hash then sequence and index.</summary>
|
||||
public Task<IReadOnlyList<DuplicateFrame>> ListDuplicatesAsync(Guid archiveId, CancellationToken cancellationToken = default) =>
|
||||
_db.ListAsync(
|
||||
@@ -247,7 +297,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()
|
||||
@@ -279,5 +329,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),
|
||||
};
|
||||
}
|
||||
@@ -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";
|
||||
|
||||
/// <summary>rendered_count is owned by <see cref="RenderRepository"/> once the row exists.</summary>
|
||||
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"]);
|
||||
|
||||
/// <summary>Ordinal of the tag list after <see cref="Columns"/>, and of the sort key a listing appends after it.</summary>
|
||||
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,
|
||||
};
|
||||
}
|
||||
|
||||
@@ -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<IReadOnlyList<string>> ProbedCompileOptions = new(() => ProbeCompileOptions(ReadCompileOptions));
|
||||
|
||||
private readonly string _filename;
|
||||
private readonly bool _memory;
|
||||
@@ -84,9 +85,24 @@ public sealed class SqliteDatabase : IDatabase
|
||||
}
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// The library's <c>PRAGMA compile_options</c> (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.
|
||||
/// </summary>
|
||||
public static IReadOnlyList<string> 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,48 @@ 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.");
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// The options <paramref name="read"/> 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).
|
||||
/// </summary>
|
||||
internal static IReadOnlyList<string> ProbeCompileOptions(Func<IReadOnlyList<string>> read)
|
||||
{
|
||||
try
|
||||
{
|
||||
var options = read();
|
||||
Log.Info(LogCategory, $"SQLite {LibraryVersion}; compile options: {string.Join(' ', options)}");
|
||||
return options;
|
||||
}
|
||||
catch (Exception ex) when (ex is not OutOfMemoryException)
|
||||
{
|
||||
Log.Warning(LogCategory, "PRAGMA compile_options could not be read", ex);
|
||||
return [];
|
||||
}
|
||||
}
|
||||
|
||||
private static List<string> ReadCompileOptions()
|
||||
{
|
||||
var options = new List<string>();
|
||||
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;
|
||||
}
|
||||
|
||||
private static void Validate(string backupPath)
|
||||
{
|
||||
try
|
||||
|
||||
@@ -1,6 +1,7 @@
|
||||
namespace Titano.Protocol;
|
||||
|
||||
/// <summary>Sequence summary for lists (BRIEF §12.2 card data).</summary>
|
||||
/// <param name="PreviewOnlyCount">Frames that decode only through their embedded preview (BRIEF §8.2 tier 2).</param>
|
||||
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);
|
||||
|
||||
/// <summary>A page of results with an opaque keyset cursor; null cursor = last page.</summary>
|
||||
public sealed record Page<T>(IReadOnlyList<T> Items, string? NextCursor, long? Total);
|
||||
@@ -45,6 +47,8 @@ public sealed record UpdateSequenceRequest(
|
||||
int? ExpectedSettingsVersion,
|
||||
Guid? HeroFrameId = null);
|
||||
|
||||
/// <param name="PreviewWidth">Width of the largest embedded preview before orientation, like <paramref name="Width"/>; 0 when unknown.</param>
|
||||
/// <param name="PreviewHeight">Height of that preview before orientation; 0 when unknown.</param>
|
||||
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);
|
||||
|
||||
/// <summary><c>GET /api/v1/sequences/{id}/manifest</c>: what a client needs to diff a sequence (BRIEF §14.5).</summary>
|
||||
/// <param name="Proxies">How to make the proxies the server asks for (<see cref="ManifestFrame.WantsProxy"/>); null when it makes none.</param>
|
||||
|
||||
@@ -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);
|
||||
|
||||
|
||||
@@ -23,6 +23,9 @@ public enum StatusKind
|
||||
Paused,
|
||||
Online,
|
||||
Offline,
|
||||
|
||||
/// <summary>Frames that decode only through their embedded preview (BRIEF §8.2 tier 2).</summary>
|
||||
PreviewOnly,
|
||||
}
|
||||
|
||||
/// <summary>Glyph + text in the colour of the status kind.</summary>
|
||||
|
||||
@@ -1363,6 +1363,9 @@
|
||||
<Trigger Property="Kind" Value="Rendered">
|
||||
<Setter TargetName="Glyph" Property="Glyph" Value="{StaticResource Icon.TriangleRight}" />
|
||||
</Trigger>
|
||||
<Trigger Property="Kind" Value="PreviewOnly">
|
||||
<Setter TargetName="Glyph" Property="Glyph" Value="{StaticResource Icon.Image}" />
|
||||
</Trigger>
|
||||
<Trigger Property="Kind" Value="Synced"><Setter Property="Foreground" Value="{DynamicResource Brush.Accent}" /></Trigger>
|
||||
<Trigger Property="Kind" Value="Remote"><Setter Property="Foreground" Value="{DynamicResource Brush.Accent}" /></Trigger>
|
||||
<Trigger Property="Kind" Value="Running"><Setter Property="Foreground" Value="{DynamicResource Brush.Accent}" /></Trigger>
|
||||
|
||||
Reference in new issue
Block a user