- Renders, analyses, the server's manifests and proxy downloads take a proxy only when its Exif capture time is the frame's (ArchiveImages.IsProxyOf); one left at the index by a frame renumbered since counts as missing and is asked for again (V-24). - A frame whose original the host reads through its proxy (HEIF on the server) is preview-only only while it has none: the light analysis and the proxy pass keep the flag, the preview-only count, the light finding and the advisor's output cap in step with the proxies, and an arriving proxy queues a light analysis on the server (V-1, V-25). - The deep checkpoint's content key includes whether each frame is read from its original or its proxy, and analysis.json lists the frames measured from proxies; an unreadable verdict on a frame now read from its proxy makes the report stale (V-26). - Thumbnails drawn from proxies are compared with the archive's thumbnail size instead of 320 px (V-27). - A resumed thumbnail or proxy pass sets ThumbsBuilt/ProxiesBuilt only when the frames an earlier run did still have theirs, and the proxy pass logs frames waiting for a client's proxy apart from unreadable ones (V-28). Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com> Claude-Session: https://claude.ai/code/session_012iLMBNwBE2GnphAsu4cjaC
91 lines
4.5 KiB
C#
91 lines
4.5 KiB
C#
using System.Buffers.Binary;
|
||
using System.Text;
|
||
using Titano.Core.Codecs.Jpeg;
|
||
using Titano.Core.Imaging;
|
||
using Titano.Core.Metadata;
|
||
|
||
namespace Titano.Data.Tests.Archiving;
|
||
|
||
/// <summary>
|
||
/// HEIF files as phones and cameras write them, as far as a host without a HEVC decoder reads them (the server, BRIEF
|
||
/// §8.1): the primary image's size (<c>ispe</c>) and an Exif item with the capture time and, in IFD1, a small JPEG
|
||
/// thumbnail, the only pixels such a host finds in the file.
|
||
/// </summary>
|
||
internal static class HeifFile
|
||
{
|
||
/// <summary>A <paramref name="width"/> × <paramref name="height"/> HEIF captured at <paramref name="captured"/>, its Exif
|
||
/// thumbnail <paramref name="thumbnailWidth"/> × <paramref name="thumbnailHeight"/>.</summary>
|
||
public static byte[] Create(DateTime captured, int width, int height, int thumbnailWidth = 160, int thumbnailHeight = 120)
|
||
{
|
||
using var small = new ImageBuffer(thumbnailWidth, thumbnailHeight);
|
||
for (var c = 0; c < 3; c++) small.Plane(c).Fill(0.2f + c * 0.2f);
|
||
var thumbnail = JpegEncoder.Encode(small, new JpegEncodeOptions { Quality = 80 });
|
||
var tiff = WithThumbnail(ExifWriter.BuildTiff(new FrameMetadata { Make = "TestCam", Model = "H-1", CapturedAt = captured }), thumbnail);
|
||
byte[] exif = [.. U32(6), .. "Exif\0\0"u8, .. tiff];
|
||
// The HEVC-coded primary image, which no managed decoder reads.
|
||
var coded = new byte[64];
|
||
|
||
byte[] Build(int mdat) =>
|
||
[
|
||
.. Box("ftyp", "heic"u8.ToArray(), U32(0), "mif1heic"u8.ToArray()),
|
||
.. FullBox("meta", 0,
|
||
FullBox("hdlr", 0, U32(0), "pict"u8.ToArray(), new byte[13]),
|
||
FullBox("pitm", 0, U16(1)),
|
||
FullBox("iinf", 0, U16(2), FullBox("infe", 2, U16(1), U16(0), "hvc1\0"u8.ToArray()), FullBox("infe", 2, U16(2), U16(0), "Exif\0"u8.ToArray())),
|
||
FullBox("iloc", 1, [0x44, 0x00], U16(2),
|
||
U16(1), U16(0), U16(0), U16(1), U32(mdat), U32(coded.Length),
|
||
U16(2), U16(0), U16(0), U16(1), U32(mdat + coded.Length), U32(exif.Length)),
|
||
Box("iprp", Box("ipco", FullBox("ispe", 0, U32(width), U32(height))), FullBox("ipma", 0, U32(1), U16(1), [1, 0x81]))),
|
||
];
|
||
|
||
var head = Build(0);
|
||
head = Build(head.Length + 8);
|
||
return [.. head, .. Box("mdat", coded, exif)];
|
||
}
|
||
|
||
/// <summary><paramref name="tiff"/> (a single IFD0 chain, as <see cref="ExifWriter.BuildTiff"/> writes it) with an IFD1 holding <paramref name="thumbnail"/>.</summary>
|
||
private static byte[] WithThumbnail(byte[] tiff, byte[] thumbnail)
|
||
{
|
||
const int Entries = 3;
|
||
var ifd1 = (tiff.Length + 1) & ~1;
|
||
var jpeg = ifd1 + 2 + 12 * Entries + 4;
|
||
var result = new byte[jpeg + thumbnail.Length];
|
||
tiff.CopyTo(result, 0);
|
||
var ifd0Entries = BinaryPrimitives.ReadUInt16LittleEndian(result.AsSpan(8));
|
||
BinaryPrimitives.WriteUInt32LittleEndian(result.AsSpan(8 + 2 + 12 * ifd0Entries), (uint)ifd1);
|
||
BinaryPrimitives.WriteUInt16LittleEndian(result.AsSpan(ifd1), Entries);
|
||
Entry(result, ifd1 + 2, 0x0103, 3, 6); // Compression: JPEG
|
||
Entry(result, ifd1 + 14, 0x0201, 4, (uint)jpeg); // JPEGInterchangeFormat
|
||
Entry(result, ifd1 + 26, 0x0202, 4, (uint)thumbnail.Length); // JPEGInterchangeFormatLength
|
||
thumbnail.CopyTo(result, jpeg);
|
||
return result;
|
||
}
|
||
|
||
/// <summary>A little-endian IFD entry with one value.</summary>
|
||
private static void Entry(byte[] data, int at, ushort tag, ushort type, uint value)
|
||
{
|
||
BinaryPrimitives.WriteUInt16LittleEndian(data.AsSpan(at), tag);
|
||
BinaryPrimitives.WriteUInt16LittleEndian(data.AsSpan(at + 2), type);
|
||
BinaryPrimitives.WriteUInt32LittleEndian(data.AsSpan(at + 4), 1);
|
||
BinaryPrimitives.WriteUInt32LittleEndian(data.AsSpan(at + 8), value);
|
||
}
|
||
|
||
private static byte[] Box(string type, params byte[][] payload) => [.. U32(8 + payload.Sum(p => p.Length)), .. Encoding.ASCII.GetBytes(type), .. payload.SelectMany(p => p)];
|
||
|
||
private static byte[] FullBox(string type, byte version, params byte[][] payload) => Box(type, [[version, 0, 0, 0], .. payload]);
|
||
|
||
private static byte[] U16(int value)
|
||
{
|
||
var bytes = new byte[2];
|
||
BinaryPrimitives.WriteUInt16BigEndian(bytes, (ushort)value);
|
||
return bytes;
|
||
}
|
||
|
||
private static byte[] U32(int value)
|
||
{
|
||
var bytes = new byte[4];
|
||
BinaryPrimitives.WriteUInt32BigEndian(bytes, (uint)value);
|
||
return bytes;
|
||
}
|
||
}
|