feat(metadata): add ISO-BMFF walker with HEIF and CR3 parsers
BmffReader reads box headers only (32/64-bit and to-end sizes, uuid user types, per-container count and path depth caps). HeifParser handles hdlr/pitm/iinf (infe v2/v3)/iloc (v0-v2, multi-extent, idat)/ iprp: size from the primary item's ispe, bit depth from pixi, Exif and XMP items; irot/imir define the orientation and override Exif because HEIF decoders apply them. Cr3Parser reads CMT1-4 and THMB from the Canon uuid box, PRVW, the XMP uuid, track 1's full-size JPEG through the sample tables and the raw size from the CRAW/CMP1 header. Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com> Claude-Session: https://claude.ai/code/session_012iLMBNwBE2GnphAsu4cjaC
This commit is contained in:
3 files changed
+567
No files matched your search
@@ -0,0 +1,127 @@
|
||||
using System.Buffers.Binary;
|
||||
|
||||
namespace Titano.Core.Metadata.Bmff;
|
||||
|
||||
/// <summary>One ISO-BMFF box header: four-character type, absolute start, total size and header size
|
||||
/// (8, 16 with a 64-bit size, plus 16 for a <c>uuid</c> user type).</summary>
|
||||
public readonly record struct BmffBox(uint Type, long Start, long Size, int HeaderSize, Guid UserType)
|
||||
{
|
||||
public long End => Start + Size;
|
||||
public long PayloadStart => Start + HeaderSize;
|
||||
public long PayloadLength => Size - HeaderSize;
|
||||
|
||||
public bool Is(string fourCC) => Type == FourCC.From(fourCC);
|
||||
|
||||
public override string ToString() => $"{FourCC.Format(Type)} @{Start} ({Size} bytes)";
|
||||
}
|
||||
|
||||
/// <summary>Four-character codes as big-endian 32-bit integers.</summary>
|
||||
public static class FourCC
|
||||
{
|
||||
public static uint From(ReadOnlySpan<char> code)
|
||||
{
|
||||
if (code.Length != 4) throw new ArgumentException("A four-character code has exactly four characters.", nameof(code));
|
||||
return (uint)(byte)code[0] << 24 | (uint)(byte)code[1] << 16 | (uint)(byte)code[2] << 8 | (byte)code[3];
|
||||
}
|
||||
|
||||
public static string Format(uint code) =>
|
||||
string.Create(4, code, static (span, c) =>
|
||||
{
|
||||
for (var i = 0; i < 4; i++)
|
||||
{
|
||||
var b = (char)((c >> (24 - 8 * i)) & 0xFF);
|
||||
span[i] = b is >= ' ' and <= '~' ? b : '?';
|
||||
}
|
||||
});
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Fuzz-safe ISO-BMFF box walker over a seekable stream: 32-bit, 64-bit (<c>largesize</c>) and to-end (size 0) boxes,
|
||||
/// <c>uuid</c> user types, a cap on boxes per container and on descent depth. Only headers are read.
|
||||
/// </summary>
|
||||
public static class BmffReader
|
||||
{
|
||||
public const int MaxBoxesPerContainer = 4096;
|
||||
public const int MaxDepth = 16;
|
||||
|
||||
private static readonly uint Uuid = FourCC.From("uuid");
|
||||
|
||||
/// <summary>Box headers inside [<paramref name="start"/>, <paramref name="end"/>). Stops at the first malformed header;
|
||||
/// a box running past <paramref name="end"/> is clamped so truncated files still expose their leading boxes.</summary>
|
||||
public static IReadOnlyList<BmffBox> ReadChildren(Stream stream, long start, long end, ICollection<string>? warnings = null)
|
||||
{
|
||||
ArgumentNullException.ThrowIfNull(stream);
|
||||
var boxes = new List<BmffBox>();
|
||||
end = Math.Min(end, stream.Length);
|
||||
Span<byte> h = stackalloc byte[32];
|
||||
var pos = Math.Max(0, start);
|
||||
while (pos <= end - 8)
|
||||
{
|
||||
if (boxes.Count == MaxBoxesPerContainer)
|
||||
{
|
||||
warnings?.Add($"More than {MaxBoxesPerContainer} boxes at {start}; rest ignored");
|
||||
break;
|
||||
}
|
||||
var n = stream.ReadUpTo(pos, h[..(int)Math.Min(h.Length, end - pos)]);
|
||||
if (n < 8) break;
|
||||
long size = BinaryPrimitives.ReadUInt32BigEndian(h);
|
||||
var type = BinaryPrimitives.ReadUInt32BigEndian(h[4..]);
|
||||
var header = 8;
|
||||
if (size == 1)
|
||||
{
|
||||
if (n < 16) break;
|
||||
size = (long)Math.Min(BinaryPrimitives.ReadUInt64BigEndian(h[8..]), long.MaxValue);
|
||||
header = 16;
|
||||
}
|
||||
else if (size == 0)
|
||||
{
|
||||
size = end - pos;
|
||||
}
|
||||
var userType = Guid.Empty;
|
||||
if (type == Uuid)
|
||||
{
|
||||
if (n < header + 16) break;
|
||||
userType = new Guid(h.Slice(header, 16), bigEndian: true);
|
||||
header += 16;
|
||||
}
|
||||
if (size < header)
|
||||
{
|
||||
warnings?.Add($"Malformed box header at {pos}");
|
||||
break;
|
||||
}
|
||||
if (size > end - pos)
|
||||
{
|
||||
warnings?.Add($"Box '{FourCC.Format(type)}' at {pos} runs past its container");
|
||||
size = end - pos;
|
||||
}
|
||||
boxes.Add(new BmffBox(type, pos, size, header, userType));
|
||||
pos += size;
|
||||
}
|
||||
return boxes;
|
||||
}
|
||||
|
||||
/// <summary>First box of <paramref name="type"/> in <paramref name="boxes"/>, or null.</summary>
|
||||
public static BmffBox? Find(IReadOnlyList<BmffBox> boxes, string type)
|
||||
{
|
||||
var code = FourCC.From(type);
|
||||
foreach (var b in boxes)
|
||||
{
|
||||
if (b.Type == code) return b;
|
||||
}
|
||||
return null;
|
||||
}
|
||||
|
||||
/// <summary>Follows a path of plain (non-full) container boxes from <paramref name="parent"/>'s payload,
|
||||
/// taking the first match at each level.</summary>
|
||||
public static BmffBox? FindPath(Stream stream, BmffBox parent, params ReadOnlySpan<string> path)
|
||||
{
|
||||
if (path.Length > MaxDepth) throw new ArgumentException($"Paths are limited to {MaxDepth} levels.", nameof(path));
|
||||
BmffBox? current = parent;
|
||||
foreach (var type in path)
|
||||
{
|
||||
if (current is not { } box) return null;
|
||||
current = Find(ReadChildren(stream, box.PayloadStart, box.End), type);
|
||||
}
|
||||
return current;
|
||||
}
|
||||
}
|
||||
Reference in new issue
Block a user