diff --git a/src/Titano.Core/Metadata/Bmff/BmffReader.cs b/src/Titano.Core/Metadata/Bmff/BmffReader.cs new file mode 100644 index 00000000..8f57a992 --- /dev/null +++ b/src/Titano.Core/Metadata/Bmff/BmffReader.cs @@ -0,0 +1,127 @@ +using System.Buffers.Binary; + +namespace Titano.Core.Metadata.Bmff; + +/// 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 uuid user type). +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)"; +} + +/// Four-character codes as big-endian 32-bit integers. +public static class FourCC +{ + public static uint From(ReadOnlySpan 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 : '?'; + } + }); +} + +/// +/// Fuzz-safe ISO-BMFF box walker over a seekable stream: 32-bit, 64-bit (largesize) and to-end (size 0) boxes, +/// uuid user types, a cap on boxes per container and on descent depth. Only headers are read. +/// +public static class BmffReader +{ + public const int MaxBoxesPerContainer = 4096; + public const int MaxDepth = 16; + + private static readonly uint Uuid = FourCC.From("uuid"); + + /// Box headers inside [, ). Stops at the first malformed header; + /// a box running past is clamped so truncated files still expose their leading boxes. + public static IReadOnlyList ReadChildren(Stream stream, long start, long end, ICollection? warnings = null) + { + ArgumentNullException.ThrowIfNull(stream); + var boxes = new List(); + end = Math.Min(end, stream.Length); + Span 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; + } + + /// First box of in , or null. + public static BmffBox? Find(IReadOnlyList boxes, string type) + { + var code = FourCC.From(type); + foreach (var b in boxes) + { + if (b.Type == code) return b; + } + return null; + } + + /// Follows a path of plain (non-full) container boxes from 's payload, + /// taking the first match at each level. + public static BmffBox? FindPath(Stream stream, BmffBox parent, params ReadOnlySpan 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; + } +} diff --git a/src/Titano.Core/Metadata/Bmff/Cr3Parser.cs b/src/Titano.Core/Metadata/Bmff/Cr3Parser.cs new file mode 100644 index 00000000..a02d63ce --- /dev/null +++ b/src/Titano.Core/Metadata/Bmff/Cr3Parser.cs @@ -0,0 +1,158 @@ +using System.Buffers.Binary; +using Titano.Core.Metadata.Jpeg; +using Titano.Core.Metadata.MakerNotes; +using Titano.Core.Metadata.Tiff; +using Titano.Core.Metadata.Xmp; + +namespace Titano.Core.Metadata.Bmff; + +/// +/// Canon CR3 (ISO-BMFF, brand crx ): the Canon uuid box in moov holds CMT1 (IFD0), CMT2 (Exif IFD), +/// CMT3 (Canon MakerNote), CMT4 (GPS IFD) as little TIFF files plus the THMB thumbnail; a top-level uuid holds the +/// PRVW preview and another the XMP packet. Track 1's first sample is the full-size JPEG; the main size and bit depth come +/// from the largest raw track (CRAW sample entry with a CMP1 header), else from CMT1. +/// +internal static class Cr3Parser +{ + private static readonly Guid CanonUuid = new("85c0b687-820f-11e0-8111-f4ce462b6a48"); + private static readonly Guid PreviewUuid = new("eaf42b5e-1c98-4b88-b9fb-b7dc406e4d16"); + private static readonly Guid XmpUuid = new("be7acfcb-97a9-42e8-9c71-999491e3afac"); + + /// Offset of the child boxes inside a CRAW visual sample entry (8-byte header + 78 bytes of fields). + private const int CrawChildrenOffset = 86; + + /// The JPEG of THMB and PRVW starts within this many bytes of the box payload. + private const int JpegSearchBytes = 32; + + public static void Parse(MetadataBuilder m) + { + var s = m.Stream; + var warnings = new List(); + var top = BmffReader.ReadChildren(s, 0, m.Length, warnings); + var trackIndex = 0; + (int Width, int Height, int Bits) raw = default; + foreach (var box in top) + { + m.ThrowIfCancelled(); + if (box.Is("moov")) + { + foreach (var child in BmffReader.ReadChildren(s, box.PayloadStart, box.End, warnings)) + { + m.ThrowIfCancelled(); + if (child.Is("uuid") && child.UserType == CanonUuid) ReadCanonBox(m, child, warnings); + else if (child.Is("trak")) ReadTrack(m, child, trackIndex++ == 0, ref raw); + } + } + else if (box.Is("uuid") && box.UserType == PreviewUuid) + { + ReadPreviewBox(m, box); + } + else if (box.Is("uuid") && box.UserType == XmpUuid) + { + m.AddXmp(s.ReadAt(box.PayloadStart, box.PayloadLength, XmpParser.MaxPacketBytes)); + } + } + m.AddWarnings(warnings); + if (raw.Width > 0) m.SetContainerSize(raw.Width, raw.Height, raw.Bits > 0 ? raw.Bits : null); + } + + private static void ReadCanonBox(MetadataBuilder m, BmffBox canon, List warnings) + { + foreach (var box in BmffReader.ReadChildren(m.Stream, canon.PayloadStart, canon.End, warnings)) + { + if (box.Is("THMB")) + { + AddJpeg(m, box); + continue; + } + if (!(box.Is("CMT1") || box.Is("CMT2") || box.Is("CMT3") || box.Is("CMT4"))) continue; + var r = TiffReader.Open(m.Stream, box.PayloadStart); + if (r?.ReadIfd(r.FirstIfdOffset) is not { } ifd) + { + m.Warn($"CR3 {FourCC.Format(box.Type)} is not a TIFF block"); + continue; + } + if (box.Is("CMT1")) + { + ExifParser.ReadIfd0(r, ifd, m); + m.AddRaster(IfdValue.Int(r, ifd, TiffTags.ImageWidth) ?? 0, IfdValue.Int(r, ifd, TiffTags.ImageLength) ?? 0, null, true); + } + else if (box.Is("CMT2")) + { + ExifParser.ReadExif(r, ifd, m); + } + else if (box.Is("CMT3")) + { + CanonMakerNote.Parse(r, ifd, m); + } + else + { + ExifParser.ReadGps(r, ifd, m); + } + m.AddWarnings(r.Warnings); + } + } + + /// Track 1's first sample is the full-size JPEG; any track whose CRAW entry has a CMP1 header is raw data. + private static void ReadTrack(MetadataBuilder m, BmffBox trak, bool first, ref (int Width, int Height, int Bits) raw) + { + var s = m.Stream; + if (BmffReader.FindPath(s, trak, "mdia", "minf", "stbl") is not { } stbl) return; + var tables = BmffReader.ReadChildren(s, stbl.PayloadStart, stbl.End); + if (BmffReader.Find(tables, "stsd") is { } stsd && FirstSampleEntry(s, stsd) is { } entry && entry.Is("CRAW") + && BmffReader.Find(BmffReader.ReadChildren(s, entry.Start + CrawChildrenOffset, entry.End), "CMP1") is { } cmp1) + { + // CMP1 (big-endian): +8 width, +12 height, +24 bits per sample. + Span h = stackalloc byte[25]; + if (s.TryReadAt(cmp1.PayloadStart, h)) + { + var width = BinaryPrimitives.ReadInt32BigEndian(h[8..]); + var height = BinaryPrimitives.ReadInt32BigEndian(h[12..]); + if (width > 0 && height > 0 && (long)width * height > (long)raw.Width * raw.Height) raw = (width, height, h[24]); + } + } + if (first && FirstSample(s, tables) is { } sample) JpegParser.AddPreview(m, sample.Offset, sample.Length, PreviewKind.FullSize); + } + + private static BmffBox? FirstSampleEntry(Stream s, BmffBox stsd) => + BmffReader.ReadChildren(s, stsd.PayloadStart + 8, stsd.End) is [var entry, ..] ? entry : null; + + /// Offset (stco/co64) and size (stsz) of a track's first sample. + private static (long Offset, long Length)? FirstSample(Stream s, IReadOnlyList tables) + { + Span b = stackalloc byte[16]; + if (BmffReader.Find(tables, "stsz") is not { } stsz || !s.TryReadAt(stsz.PayloadStart, b)) return null; + long size = BinaryPrimitives.ReadUInt32BigEndian(b[4..]); + if (size == 0) size = BinaryPrimitives.ReadUInt32BigEndian(b[8..]) > 0 ? BinaryPrimitives.ReadUInt32BigEndian(b[12..]) : 0; + long offset; + if (BmffReader.Find(tables, "co64") is { } co64 && s.TryReadAt(co64.PayloadStart, b)) + offset = BinaryPrimitives.ReadUInt32BigEndian(b[4..]) > 0 ? (long)Math.Min(BinaryPrimitives.ReadUInt64BigEndian(b[8..]), long.MaxValue) : 0; + else if (BmffReader.Find(tables, "stco") is { } stco && s.TryReadAt(stco.PayloadStart, b[..12])) + offset = BinaryPrimitives.ReadUInt32BigEndian(b[4..]) > 0 ? BinaryPrimitives.ReadUInt32BigEndian(b[8..]) : 0; + else + return null; + return offset > 0 && size > 0 ? (offset, size) : null; + } + + /// The preview uuid holds 8 bytes of header fields, then a PRVW box. + private static void ReadPreviewBox(MetadataBuilder m, BmffBox uuid) + { + if (BmffReader.Find(BmffReader.ReadChildren(m.Stream, uuid.PayloadStart + 8, uuid.End), "PRVW") is { } prvw) AddJpeg(m, prvw); + else m.Warn("CR3 PRVW box not found"); + } + + /// THMB/PRVW: a few header fields (version, size, dimensions) followed by the JPEG to the end of the box. + private static void AddJpeg(MetadataBuilder m, BmffBox box) + { + Span head = stackalloc byte[JpegSearchBytes]; + var n = m.Stream.ReadUpTo(box.PayloadStart, head[..(int)Math.Min(head.Length, box.PayloadLength)]); + var at = head[..n].IndexOf([(byte)0xFF, (byte)0xD8, (byte)0xFF]); + if (at < 0) + { + m.Warn($"CR3 {FourCC.Format(box.Type)} holds no JPEG"); + return; + } + var start = box.PayloadStart + at; + JpegParser.AddPreview(m, start, box.End - start); + } +} diff --git a/src/Titano.Core/Metadata/Bmff/HeifParser.cs b/src/Titano.Core/Metadata/Bmff/HeifParser.cs new file mode 100644 index 00000000..b29c5c70 --- /dev/null +++ b/src/Titano.Core/Metadata/Bmff/HeifParser.cs @@ -0,0 +1,282 @@ +using System.Buffers.Binary; +using Titano.Core.Metadata.Tiff; +using Titano.Core.Metadata.Xmp; + +namespace Titano.Core.Metadata.Bmff; + +/// +/// HEIF / HEIC / AVIF: meta → hdlr, pitm, iinf/infe (v2/v3), iloc (v0–v2), +/// iprp/ipco/ipma. Main size from the primary item's ispe, bit depth from pixi, +/// Exif from the Exif item and XMP from the application/rdf+xml mime item. +/// +/// +/// Orientation rule: when the primary item carries irot and/or imir, their composition (in association +/// order) defines and the Exif orientation is ignored — HEIF decoders apply those +/// transforms themselves (ISO/IEC 23008-12), so honouring Exif as well would rotate twice. Without them, Exif applies. +/// imir axis 0 mirrors top↔bottom and axis 1 left↔right (23008-12 2nd edition, as libheif). +/// +internal static class HeifParser +{ + private const int MaxPropertyBytes = 64; + private const int MaxTableBytes = 1024 * 1024; + private const int MaxInfeBytes = 4096; + private const int MaxExtentsPerItem = 1024; + private static readonly uint Exif = FourCC.From("Exif"); + private static readonly uint Mime = FourCC.From("mime"); + + // Exif orientations 1–8 as integer matrices (a, b, c, d) mapping stored (x, y) to displayed (ax + by, cx + dy), y down. + private static readonly (int A, int B, int C, int D)[] Orientations = + [ + (1, 0, 0, 1), (-1, 0, 0, 1), (-1, 0, 0, -1), (1, 0, 0, -1), + (0, 1, 1, 0), (0, -1, 1, 0), (0, -1, -1, 0), (0, 1, -1, 0), + ]; + + public static void Parse(MetadataBuilder m) + { + var warnings = new List(); + var meta = BmffReader.Find(BmffReader.ReadChildren(m.Stream, 0, m.Length, warnings), "meta"); + m.AddWarnings(warnings); + if (meta is { } box) ParseMeta(m, box); + else m.Warn("HEIF meta box not found"); + } + + private static void ParseMeta(MetadataBuilder m, BmffBox meta) + { + var s = m.Stream; + var warnings = new List(); + var children = BmffReader.ReadChildren(s, meta.PayloadStart + 4, meta.End, warnings); + var items = new Dictionary(); + uint? primary = null; + BmffBox? iprp = null; + long idat = -1; + foreach (var child in children) + { + m.ThrowIfCancelled(); + if (child.Is("hdlr")) CheckHandler(m, child); + else if (child.Is("pitm")) primary = ReadPrimary(s, child); + else if (child.Is("iinf")) ReadItemInfo(m, child, items, warnings); + else if (child.Is("iloc")) ReadLocations(m, child, items); + else if (child.Is("iprp")) iprp = child; + else if (child.Is("idat")) idat = child.PayloadStart; + } + m.AddWarnings(warnings); + if (primary is { } id && iprp is { } properties) ReadPrimaryProperties(m, properties, id); + else m.Warn("HEIF primary item or properties missing"); + + foreach (var item in items.Values) + { + if (item.ConstructionMethod == 1 && idat < 0) continue; + var origin = item.ConstructionMethod == 1 ? idat : 0; + if (item.Type == Exif) ReadExifItem(m, item, origin); + else if (item.Type == Mime && item.ContentType == "application/rdf+xml") ReadXmpItem(m, item, origin); + } + } + + private static void CheckHandler(MetadataBuilder m, BmffBox hdlr) + { + Span b = stackalloc byte[12]; + if (!m.Stream.TryReadAt(hdlr.PayloadStart, b) || !b[8..].SequenceEqual("pict"u8)) m.Warn("HEIF handler is not 'pict'"); + } + + private static uint? ReadPrimary(Stream s, BmffBox pitm) + { + Span b = stackalloc byte[8]; + var n = s.ReadUpTo(pitm.PayloadStart, b[..(int)Math.Min(b.Length, pitm.PayloadLength)]); + if (n < 6) return null; + return b[0] == 0 ? BinaryPrimitives.ReadUInt16BigEndian(b[4..]) : n >= 8 ? BinaryPrimitives.ReadUInt32BigEndian(b[4..]) : null; + } + + /// iinf → infe v2/v3: item id, type and (for mime items) content type. Earlier infe versions carry no type. + private static void ReadItemInfo(MetadataBuilder m, BmffBox iinf, Dictionary items, List warnings) + { + Span head = stackalloc byte[1]; + if (!m.Stream.TryReadAt(iinf.PayloadStart, head)) return; + var first = iinf.PayloadStart + (head[0] == 0 ? 6 : 8); + foreach (var infe in BmffReader.ReadChildren(m.Stream, first, iinf.End, warnings)) + { + m.ThrowIfCancelled(); + if (!infe.Is("infe") || m.Stream.ReadAt(infe.PayloadStart, Math.Min(infe.PayloadLength, MaxInfeBytes), MaxInfeBytes) is not { } data) continue; + var r = new SpanReader(data); + var version = r.U8(); + r.Skip(3); + if (version < 2) continue; + var id = version == 2 ? r.U16() : r.U32(); + r.Skip(2); + var type = r.U32(); + if (!r.Ok) continue; + r.CString(); + var item = Get(items, id); + item.Type = type; + if (type == Mime && r.Remaining > 0) item.ContentType = r.CString(); + } + } + + /// iloc v0–v2: offset/length/base-offset/index field sizes, construction methods 0 (file) and 1 (idat), + /// any number of extents. + private static void ReadLocations(MetadataBuilder m, BmffBox iloc, Dictionary items) + { + if (m.Stream.ReadAt(iloc.PayloadStart, iloc.PayloadLength, MaxTableBytes) is not { } data) + { + m.Warn("HEIF iloc unreadable"); + return; + } + var r = new SpanReader(data); + var version = r.U8(); + r.Skip(3); + var sizes = r.U8(); + var sizes2 = r.U8(); + int offsetSize = sizes >> 4, lengthSize = sizes & 0xF, baseOffsetSize = sizes2 >> 4; + var indexSize = version is 1 or 2 ? sizes2 & 0xF : 0; + if (version > 2) + { + m.Warn($"HEIF iloc version {version} unsupported"); + return; + } + var count = version < 2 ? r.U16() : r.U32(); + for (uint i = 0; i < count && r.Ok; i++) + { + var id = version < 2 ? r.U16() : r.U32(); + var method = version is 1 or 2 ? r.U16() & 0xF : 0; + r.Skip(2); + var baseOffset = r.Sized(baseOffsetSize); + var extents = r.U16(); + var item = Get(items, id); + item.ConstructionMethod = method; + for (var e = 0; e < extents && r.Ok; e++) + { + r.Sized(indexSize); + var offset = baseOffset + r.Sized(offsetSize); + var length = r.Sized(lengthSize); + if (r.Ok && e < MaxExtentsPerItem && offset <= long.MaxValue && length <= long.MaxValue) + item.Extents.Add(((long)offset, (long)length)); + } + } + if (!r.Ok) m.Warn("HEIF iloc truncated or malformed"); + } + + /// ispe, pixi, irot and imir associated with the primary item through ipma (1-based ipco indices). + private static void ReadPrimaryProperties(MetadataBuilder m, BmffBox iprp, uint primary) + { + var s = m.Stream; + var children = BmffReader.ReadChildren(s, iprp.PayloadStart, iprp.End); + if (BmffReader.Find(children, "ipco") is not { } ipco || BmffReader.Find(children, "ipma") is not { } ipma) return; + var properties = BmffReader.ReadChildren(s, ipco.PayloadStart, ipco.End); + var transform = Orientations[0]; + var transformed = false; + foreach (var index in Associations(m, ipma, primary)) + { + if (index < 1 || index > properties.Count) continue; + var p = properties[index - 1]; + var data = s.ReadAt(p.PayloadStart, Math.Min(p.PayloadLength, MaxPropertyBytes), MaxPropertyBytes); + if (data is null) continue; + if (p.Is("ispe") && data.Length >= 12) + { + var width = BinaryPrimitives.ReadUInt32BigEndian(data.AsSpan(4)); + var height = BinaryPrimitives.ReadUInt32BigEndian(data.AsSpan(8)); + if (width <= int.MaxValue && height <= int.MaxValue) m.SetContainerSize((int)width, (int)height); + } + else if (p.Is("pixi") && data.Length >= 6 && data[4] > 0) + { + m.CodedBitsPerSample ??= data[5]; + } + else if (p.Is("irot") && data.Length >= 1) + { + // Anticlockwise quarter turns: 1 → orientation 8, 2 → 3, 3 → 6. + transform = Multiply(Orientations[(data[0] & 3) switch { 1 => 7, 2 => 2, 3 => 5, _ => 0 }], transform); + transformed = true; + } + else if (p.Is("imir") && data.Length >= 1) + { + transform = Multiply(Orientations[(data[0] & 1) == 0 ? 3 : 1], transform); + transformed = true; + } + } + if (transformed) m.ContainerOrientation = Array.IndexOf(Orientations, transform) + 1; + } + + private static List Associations(MetadataBuilder m, BmffBox ipma, uint primary) + { + var result = new List(); + if (m.Stream.ReadAt(ipma.PayloadStart, ipma.PayloadLength, MaxTableBytes) is not { } data) return result; + var r = new SpanReader(data); + var version = r.U8(); + r.Skip(2); + var wideIndex = (r.U8() & 1) != 0; + var count = r.U32(); + for (uint i = 0; i < count && r.Ok; i++) + { + var id = version < 1 ? r.U16() : r.U32(); + var associations = r.U8(); + for (var a = 0; a < associations && r.Ok; a++) + { + var index = wideIndex ? r.U16() & 0x7FFF : r.U8() & 0x7F; + if (id == primary && r.Ok) result.Add(index); + } + } + if (!r.Ok) m.Warn("HEIF ipma truncated"); + return result; + } + + private static void ReadExifItem(MetadataBuilder m, Item item, long origin) + { + if (item.Extents.Count == 0) return; + var (start, length) = item.Extents[0]; + for (var i = 1; i < item.Extents.Count; i++) + { + if (item.Extents[i].Offset != start + length) + { + m.Warn("Fragmented HEIF Exif item not supported"); + return; + } + length += item.Extents[i].Length; + } + start += origin; + if (length == 0) length = m.Length - start; + if (start < 0 || length <= 0 || start > m.Length - length) + { + m.Warn("HEIF Exif item outside the file"); + return; + } + ExifParser.ParseOffsetPrefixed(m, start, length); + } + + private static void ReadXmpItem(MetadataBuilder m, Item item, long origin) + { + long total = 0; + foreach (var (_, length) in item.Extents) total += length; + if (total <= 0 || total > XmpParser.MaxPacketBytes) + { + m.Warn("HEIF XMP item empty or too large"); + return; + } + var packet = new byte[total]; + var at = 0; + foreach (var (offset, length) in item.Extents) + { + if (!m.Stream.TryReadAt(origin + offset, packet.AsSpan(at, (int)length))) + { + m.Warn("HEIF XMP item outside the file"); + return; + } + at += (int)length; + } + m.AddXmp(packet); + } + + private static (int, int, int, int) Multiply((int A, int B, int C, int D) x, (int A, int B, int C, int D) y) => + (x.A * y.A + x.B * y.C, x.A * y.B + x.B * y.D, x.C * y.A + x.D * y.C, x.C * y.B + x.D * y.D); + + private static Item Get(Dictionary items, uint id) + { + if (!items.TryGetValue(id, out var item)) items[id] = item = new Item(); + return item; + } + + private sealed class Item + { + public uint Type { get; set; } + public string? ContentType { get; set; } + public int ConstructionMethod { get; set; } + public List<(long Offset, long Length)> Extents { get; } = []; + } +}