fix(imaging): record only what decoding proves and find GPR JPEGs across sequences
- Preview sizes come from probes and unscaled decodes: a size-limited decode picks the smallest preview that reaches its edge (CR3 PRVW 1620x1080), so renders and deep passes record its tier without a size and the output cap keeps the largest preview's. - A read error is not an undecodable file: ImageDecoder.Decode throws the IOException when a provider could not read the file and none produced pixels. Deep passes never flag frames they could not read or that vanished, and frames only an interrupted earlier run found undecodable are decoded again before they are flagged. - Renders resolve stand-ins only for frames the pipeline asks for; frames the read-ahead meets on the way (skipped by speed-ups) are left alone. - A GoPro GPR finds its JPEG in the index when an import or upload kept it in a sequence of its own (same original stem and capture time), for probes, deep analysis, renders, thumbnails, proxies and the editor; GPRs whose JPEG is indexed later are probed again. - Thumbnails judge an embedded preview by its own JPEG header. - PostgreSQL writes tier fields 1000 frames per statement. Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com> Claude-Session: https://claude.ai/code/session_012iLMBNwBE2GnphAsu4cjaC
This commit is contained in:
19 files changed
+689
-111
No files matched your search
@@ -18,19 +18,22 @@ public sealed class FrameImageRenderer(ImageDecoder decoder)
|
||||
public const int ThumbnailQuality = 80;
|
||||
|
||||
/// <summary>A 320 px q80 thumbnail, or null when no decoder can read the frame.</summary>
|
||||
public EncodedImage? Thumbnail(SequencePaths sequence, SidecarFrame frame, CancellationToken cancellationToken = default)
|
||||
/// <param name="companion">Where a GPR's JPEG is kept when it is not beside it (<see cref="DecodeRequest.Companion"/>).</param>
|
||||
public EncodedImage? Thumbnail(SequencePaths sequence, SidecarFrame frame, CancellationToken cancellationToken = default, string? companion = null)
|
||||
{
|
||||
using var decoded = Decode(sequence.File(frame.File), frame, ThumbnailLongEdge, DecodePurpose.Thumbnail, preferPreview: true, cancellationToken);
|
||||
using var decoded = Decode(sequence.File(frame.File), frame, ThumbnailLongEdge, DecodePurpose.Thumbnail, preferPreview: true, companion, cancellationToken);
|
||||
if (decoded is null) return null;
|
||||
using var small = Resampler.Thumbnail(decoded.Pixels, ThumbnailLongEdge);
|
||||
return Encode(small, new JpegEncodeOptions { Quality = ThumbnailQuality }, decoded.Tier);
|
||||
}
|
||||
|
||||
/// <summary>A proxy at the policy's long edge and quality with capture time and camera in its Exif; null when undecodable.</summary>
|
||||
public EncodedImage? Proxy(SequencePaths sequence, SidecarFrame frame, SidecarCamera? camera, ProxyPolicy policy, CancellationToken cancellationToken = default)
|
||||
/// <param name="companion">Where a GPR's JPEG is kept when it is not beside it (<see cref="DecodeRequest.Companion"/>).</param>
|
||||
public EncodedImage? Proxy(SequencePaths sequence, SidecarFrame frame, SidecarCamera? camera, ProxyPolicy policy, CancellationToken cancellationToken = default,
|
||||
string? companion = null)
|
||||
{
|
||||
if (policy.LongEdge == ProxyPolicy.Disabled) throw new InvalidOperationException("Proxies are disabled for this archive.");
|
||||
using var decoded = Decode(sequence.File(frame.File), frame, policy.LongEdge, DecodePurpose.Proxy, preferPreview: false, cancellationToken);
|
||||
using var decoded = Decode(sequence.File(frame.File), frame, policy.LongEdge, DecodePurpose.Proxy, preferPreview: false, companion, cancellationToken);
|
||||
if (decoded is null) return null;
|
||||
var source = decoded.Pixels;
|
||||
var (w, h) = policy.LongEdge == ProxyPolicy.FullSize ? (source.Width, source.Height) : Resampler.FitWithin(source.Width, source.Height, policy.LongEdge);
|
||||
@@ -59,25 +62,30 @@ public sealed class FrameImageRenderer(ImageDecoder decoder)
|
||||
/// <summary>
|
||||
/// The frame's pixels: a JPEG through the managed decoder, the stored embedded preview when <paramref name="preferPreview"/>
|
||||
/// and it reaches <paramref name="maxLongEdge"/> (a smaller one would be upscaled or leave the thumbnail short), then
|
||||
/// the host's decoders, and the stored preview as the last resort. Null when nothing decodes.
|
||||
/// the host's decoders (with the GPR's <paramref name="companion"/>), and the stored preview as the last resort. Null
|
||||
/// when nothing decodes.
|
||||
/// </summary>
|
||||
private DecodedFrame? Decode(string path, SidecarFrame frame, int maxLongEdge, DecodePurpose purpose, bool preferPreview, CancellationToken cancellationToken)
|
||||
/// <exception cref="IOException">The file could not be read.</exception>
|
||||
private DecodedFrame? Decode(string path, SidecarFrame frame, int maxLongEdge, DecodePurpose purpose, bool preferPreview, string? companion,
|
||||
CancellationToken cancellationToken)
|
||||
{
|
||||
if (frame.Format == ImageFileFormat.Jpeg && DecodeJpeg(File.ReadAllBytes(path), maxLongEdge, frame.Orientation, DecodeTier.Native) is { } jpeg) return jpeg;
|
||||
byte[]? preview = null;
|
||||
if (preferPreview && (preview = ReadPreview(path, frame)) is not null && Reaches(preview, frame, maxLongEdge)
|
||||
if (preferPreview && (preview = ReadPreview(path, frame)) is not null && Reaches(preview, maxLongEdge)
|
||||
&& DecodeJpeg(preview, maxLongEdge, frame.Orientation, DecodeTier.EmbeddedPreview) is { } fast)
|
||||
return fast;
|
||||
var request = new DecodeRequest(path, System.Math.Max(0, maxLongEdge), purpose, ApplyOrientation: true, AllowPreview: true);
|
||||
var request = new DecodeRequest(path, System.Math.Max(0, maxLongEdge), purpose, ApplyOrientation: true, AllowPreview: true, Companion: companion);
|
||||
if (decoder.Decode(request, cancellationToken) is { } decoded) return new DecodedFrame(decoded.Pixels, decoded.Tier);
|
||||
return (preview ?? ReadPreview(path, frame)) is { } fallback ? DecodeJpeg(fallback, maxLongEdge, frame.Orientation, DecodeTier.EmbeddedPreview) : null;
|
||||
}
|
||||
|
||||
/// <summary>Whether the stored preview's long edge reaches <paramref name="maxLongEdge"/> (the sidecar's size, else the JPEG's own).</summary>
|
||||
private static bool Reaches(byte[] preview, SidecarFrame frame, int maxLongEdge)
|
||||
/// <summary>
|
||||
/// Whether the stored preview's long edge reaches <paramref name="maxLongEdge"/>, from the JPEG's own header: the
|
||||
/// sidecar's preview size may be a GPR companion's, larger than the preview embedded in the file.
|
||||
/// </summary>
|
||||
private static bool Reaches(byte[] preview, int maxLongEdge)
|
||||
{
|
||||
if (maxLongEdge <= 0) return true;
|
||||
if (frame is { PreviewW: { } w, PreviewH: { } h }) return int.Max(w, h) >= maxLongEdge;
|
||||
try
|
||||
{
|
||||
var info = JpegDecoder.ReadInfo(preview);
|
||||
|
||||
@@ -11,7 +11,8 @@ namespace Titano.Core.Imaging.Decoders;
|
||||
/// The managed decoder (BRIEF §8.2), available on every platform and the only one on the server: JPEG, PNG and TIFF
|
||||
/// natively; DNG and the supported camera RAWs (CR2, NEF, ARW, PEF, RAF) through <see cref="RawDecoder"/>; every other
|
||||
/// RAW, CR3 and HEIF through the largest suitable embedded JPEG preview, and GoPro GPR through the JPEG the camera writes
|
||||
/// beside it when that is larger (BRIEF §8.1; VC-5 is out of scope); nothing for formats without either.
|
||||
/// with it (beside it, or where <see cref="DecodeRequest.Companion"/> says an import put it) when that is larger
|
||||
/// (BRIEF §8.1; VC-5 is out of scope); nothing for formats without either.
|
||||
/// <see cref="DecodeRequest"/> fast paths: JPEG DCT scaling and RAW CFA binning when <see cref="DecodeRequest.MaxLongEdge"/>
|
||||
/// allows, bilinear demosaicing for thumbnails and analysis, and <see cref="DecodeRequest.ForcePreview"/> to skip native
|
||||
/// RAW development. Results fit within MaxLongEdge (never upscaled) and are always RGB.
|
||||
@@ -59,16 +60,21 @@ public sealed class ManagedDecoderProvider : IImageDecoderProvider
|
||||
/// The tier for a file whose signature format is already known (the index's): JPEG, PNG and TIFF decode natively
|
||||
/// whatever they hold (damage shows only when decoding), so only RAW and HEIF files are opened.
|
||||
/// </summary>
|
||||
public FileTier Probe(string path, ImageFileFormat format)
|
||||
public FileTier Probe(string path, ImageFileFormat format) => Probe(path, format, null);
|
||||
|
||||
/// <inheritdoc cref="Probe(string, ImageFileFormat)"/>
|
||||
/// <param name="companion">Where a GPR's JPEG is kept when it is not beside it (<see cref="DecodeRequest.Companion"/>).</param>
|
||||
public FileTier Probe(string path, ImageFileFormat format, string? companion)
|
||||
{
|
||||
var family = ImageFormatInfo.Of(format).Family;
|
||||
if (family is not (ImageFamily.Jpeg or ImageFamily.Png or ImageFamily.Tiff)) return Probe(path);
|
||||
if (family is not (ImageFamily.Jpeg or ImageFamily.Png or ImageFamily.Tiff)) return Probe(path, companion);
|
||||
var decoder = family switch { ImageFamily.Jpeg => "managed-jpeg", ImageFamily.Png => "managed-png", _ => "managed-tiff" };
|
||||
return new FileTier(format, DecodeTier.Native, decoder, null, 0, 0);
|
||||
}
|
||||
|
||||
/// <summary>The tier this provider would use for the file at <paramref name="path"/>, without decoding pixels.</summary>
|
||||
public FileTier Probe(string path)
|
||||
/// <param name="companion">Where a GPR's JPEG is kept when it is not beside it (<see cref="DecodeRequest.Companion"/>).</param>
|
||||
public FileTier Probe(string path, string? companion = null)
|
||||
{
|
||||
using var stream = Open(path);
|
||||
var format = ImageSignature.Detect(stream, path);
|
||||
@@ -94,23 +100,20 @@ public sealed class ManagedDecoderProvider : IImageDecoderProvider
|
||||
}
|
||||
var metadata = MetadataReader.Read(stream, path);
|
||||
var (pw, ph) = metadata.LargestPreview is { } preview ? PixelConvert.OrientedSize(preview.Width, preview.Height, metadata.Orientation) : (0, 0);
|
||||
if (format == ImageFileFormat.Gpr && CompanionJpeg(path) is { } companion)
|
||||
{
|
||||
var jpeg = MetadataReader.Read(companion);
|
||||
var (cw, ch) = jpeg.OrientedSize;
|
||||
if (jpeg.Format == ImageFileFormat.Jpeg && int.Max(cw, ch) > int.Max(pw, ph))
|
||||
return new FileTier(format, DecodeTier.EmbeddedPreview, "managed-sidecar", reason, cw, ch);
|
||||
}
|
||||
if (format == ImageFileFormat.Gpr && CompanionSize(path, companion) is var (cw, ch) && int.Max(cw, ch) > int.Max(pw, ph))
|
||||
return new FileTier(format, DecodeTier.EmbeddedPreview, "managed-sidecar", reason, cw, ch);
|
||||
if (metadata.LargestPreview is null) return new FileTier(format, DecodeTier.Unavailable, null, reason + "; no embedded preview", 0, 0);
|
||||
return new FileTier(format, DecodeTier.EmbeddedPreview, "managed-preview", reason, pw, ph);
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// The JPEG a GoPro writes beside a GPR (same name, <c>.JPG</c>), or null. Only the usual spellings are tried, so a
|
||||
/// folder of thousands of frames is never listed once per frame.
|
||||
/// The JPEG a GoPro writes with a GPR: <paramref name="companion"/> when the caller found it elsewhere (an import
|
||||
/// keeps it in a sequence of its own) and it is there, else the one beside the GPR (same name, <c>.JPG</c>), or
|
||||
/// null. Only the usual spellings are tried, so a folder of thousands of frames is never listed once per frame.
|
||||
/// </summary>
|
||||
internal static string? CompanionJpeg(string path)
|
||||
internal static string? CompanionJpeg(string path, string? companion = null)
|
||||
{
|
||||
if (companion is not null && File.Exists(companion)) return companion;
|
||||
var stem = Path.Combine(Path.GetDirectoryName(path) ?? string.Empty, Path.GetFileNameWithoutExtension(path));
|
||||
foreach (var extension in CompanionExtensions)
|
||||
{
|
||||
@@ -119,6 +122,15 @@ public sealed class ManagedDecoderProvider : IImageDecoderProvider
|
||||
return null;
|
||||
}
|
||||
|
||||
/// <summary>Oriented size of the GPR's JPEG (see <see cref="CompanionJpeg"/>), or null when there is none.</summary>
|
||||
/// <exception cref="IOException">The JPEG cannot be read.</exception>
|
||||
private static (int Width, int Height)? CompanionSize(string path, string? companion)
|
||||
{
|
||||
if (CompanionJpeg(path, companion) is not { } file) return null;
|
||||
var jpeg = MetadataReader.Read(file);
|
||||
return jpeg.Format == ImageFileFormat.Jpeg ? jpeg.OrientedSize : null;
|
||||
}
|
||||
|
||||
private static FileStream Open(string path) =>
|
||||
new(path, FileMode.Open, FileAccess.Read, FileShare.ReadWrite | FileShare.Delete, FileBufferSize, FileOptions.RandomAccess);
|
||||
|
||||
@@ -179,12 +191,13 @@ public sealed class ManagedDecoderProvider : IImageDecoderProvider
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// GPR's pixel source: the GoPro's own JPEG beside it when it is larger than every embedded preview (the camera writes
|
||||
/// it at full size), decoded with DCT scaling and oriented by its own Exif; null when there is none.
|
||||
/// GPR's pixel source: the GoPro's own JPEG (<see cref="DecodeRequest.Companion"/>, else beside it) when it is larger
|
||||
/// than every embedded preview (the camera writes it at full size), decoded with DCT scaling and oriented by its own
|
||||
/// Exif; null when there is none.
|
||||
/// </summary>
|
||||
private static DecodedImage? Companion(Stream raw, DecodeRequest request, CancellationToken cancellationToken)
|
||||
{
|
||||
if (CompanionJpeg(request.Path) is not { } path) return null;
|
||||
if (CompanionJpeg(request.Path, request.Companion) is not { } path) return null;
|
||||
var previews = MetadataReader.Read(raw, request.Path, cancellationToken: cancellationToken).Previews;
|
||||
var largestPreview = previews.Count > 0 ? int.Max(previews[0].Width, previews[0].Height) : 0;
|
||||
using var stream = Open(path);
|
||||
|
||||
@@ -1,3 +1,5 @@
|
||||
using System.Runtime.ExceptionServices;
|
||||
|
||||
namespace Titano.Core.Imaging;
|
||||
|
||||
/// <summary>How a frame's pixels were obtained (BRIEF §8.2). Logged per frame.</summary>
|
||||
@@ -28,13 +30,16 @@ public enum DecodePurpose
|
||||
/// <param name="ApplyOrientation">Rotate/flip according to the Exif orientation.</param>
|
||||
/// <param name="AllowPreview">Accept the embedded-preview tier when native decode is impossible.</param>
|
||||
/// <param name="ForcePreview">Use the embedded preview even when native decode is possible (fast paths).</param>
|
||||
/// <param name="Companion">The JPEG a GoPro wrote with this GPR when it is kept elsewhere than beside it (an import puts
|
||||
/// it in a sequence of its own); null looks beside the file. Other formats ignore it.</param>
|
||||
public sealed record DecodeRequest(
|
||||
string Path,
|
||||
int MaxLongEdge = 0,
|
||||
DecodePurpose Purpose = DecodePurpose.Render,
|
||||
bool ApplyOrientation = true,
|
||||
bool AllowPreview = true,
|
||||
bool ForcePreview = false);
|
||||
bool ForcePreview = false,
|
||||
string? Companion = null);
|
||||
|
||||
/// <summary>Decoded pixels plus how they were produced. Dispose to return the buffer to the pool.</summary>
|
||||
public sealed class DecodedImage(ImageBuffer pixels, DecodeTier tier, string decoder, int sourceWidth, int sourceHeight) : IDisposable
|
||||
@@ -84,6 +89,13 @@ public interface IImageDecoderProvider
|
||||
? new FileTier(format, tier, Name, null, 0, 0)
|
||||
: new FileTier(format, DecodeTier.Unavailable, null, $"{Name}: no decoder for {format}", 0, 0);
|
||||
|
||||
/// <summary>
|
||||
/// <see cref="Probe(string, ImageFileFormat)"/> for a file whose camera companion (see
|
||||
/// <see cref="DecodeRequest.Companion"/>) is kept at <paramref name="companion"/>; a provider that uses no
|
||||
/// companion ignores it.
|
||||
/// </summary>
|
||||
FileTier Probe(string path, ImageFileFormat format, string? companion) => Probe(path, format);
|
||||
|
||||
/// <summary>Decodes or returns null when this provider cannot handle the specific file.</summary>
|
||||
DecodedImage? Decode(DecodeRequest request, CancellationToken cancellationToken = default);
|
||||
}
|
||||
@@ -110,8 +122,9 @@ public sealed class ImageDecoder(IReadOnlyList<IImageDecoderProvider> providers)
|
||||
/// embedded preview a provider offers, else unavailable with the first reason given. A provider that finds the file
|
||||
/// malformed is skipped like in <see cref="Decode"/>.
|
||||
/// </summary>
|
||||
/// <param name="companion">Where a GoPro GPR's JPEG is kept when it is not beside the file (<see cref="DecodeRequest.Companion"/>).</param>
|
||||
/// <exception cref="IOException">The file cannot be read.</exception>
|
||||
public FileTier Probe(string path, ImageFileFormat format)
|
||||
public FileTier Probe(string path, ImageFileFormat format, string? companion = null)
|
||||
{
|
||||
FileTier? preview = null;
|
||||
string? reason = null;
|
||||
@@ -121,7 +134,7 @@ public sealed class ImageDecoder(IReadOnlyList<IImageDecoderProvider> providers)
|
||||
FileTier tier;
|
||||
try
|
||||
{
|
||||
tier = provider.Probe(path, format);
|
||||
tier = provider.Probe(path, format, companion);
|
||||
}
|
||||
catch (Exception ex) when (ex is InvalidDataException or NotSupportedException or FormatException or InvalidOperationException
|
||||
or System.Runtime.InteropServices.COMException)
|
||||
@@ -137,11 +150,17 @@ public sealed class ImageDecoder(IReadOnlyList<IImageDecoderProvider> providers)
|
||||
return preview ?? new FileTier(format, DecodeTier.Unavailable, null, reason ?? $"no decoder for {format}", 0, 0);
|
||||
}
|
||||
|
||||
/// <summary>Decodes with the first provider that succeeds; null means <see cref="DecodeTier.Unavailable"/>.</summary>
|
||||
/// <summary>
|
||||
/// Decodes with the first provider that succeeds; null means <see cref="DecodeTier.Unavailable"/>: the providers read
|
||||
/// the file and none produced pixels. When one could not read it (locked by another program, vanished, a network
|
||||
/// read failed) and none produced pixels, nothing is known about the content, so that read error is thrown instead.
|
||||
/// </summary>
|
||||
/// <exception cref="IOException">The file could not be read and no provider produced pixels.</exception>
|
||||
public DecodedImage? Decode(DecodeRequest request, CancellationToken cancellationToken = default)
|
||||
{
|
||||
var format = ImageFormatInfo.FromPath(request.Path);
|
||||
List<Exception>? errors = null;
|
||||
IOException? unread = null;
|
||||
foreach (var provider in Providers.OrderBy(p => p.TierFor(format.Format) ?? DecodeTier.Unavailable))
|
||||
{
|
||||
if (provider.TierFor(format.Format) is null) continue;
|
||||
@@ -158,8 +177,10 @@ public sealed class ImageDecoder(IReadOnlyList<IImageDecoderProvider> providers)
|
||||
catch (Exception ex) when (ex is IOException or InvalidDataException or NotSupportedException or FormatException or InvalidOperationException or System.Runtime.InteropServices.COMException)
|
||||
{
|
||||
(errors ??= []).Add(ex);
|
||||
if (ex is IOException io) unread ??= io;
|
||||
}
|
||||
}
|
||||
if (unread is not null) ExceptionDispatchInfo.Throw(unread);
|
||||
return null;
|
||||
}
|
||||
}
|
||||
Reference in new issue
Block a user