HEIF (High Efficiency Image File Format) is based on the ISO Base Media File Format (ISOBMFF), a flexible and extensible container format. This means HEIF files are essentially a collection of "boxes" (also known as "atoms") organized hierarchically. Each box has a size and a type, allowing parsers to navigate and understand the file's structure.
HEIF File Box Structure Explained
The fundamental building block of an ISOBMFF file (and thus HEIF) is the box.
Basic Box Structure:
Every box starts with a standard header:
size (4 bytes, unsigned 32-bit integer): Specifies the total size of the box in bytes, including its own header. If size is 1, a largesize field follows.
type (4 bytes, 4-character code / FourCC): Identifies the type of box (e.g., ftyp, mdat, meta).
If size is 1, then:
largesize (8 bytes, unsigned 64-bit integer): Specifies the total size for boxes larger than 4GB.
Hierarchical Nature:
Boxes can contain other boxes, forming a tree-like structure. This allows for complex and organized data representation.
Key Boxes in a HEIF File:
-
ftyp (File Type Box):
- Purpose: Always the first box. Identifies the file type and specifies compatible brands.
- Contents:
major_brand: Primary brand of the file (e.g., heic for HEIF, mif1 for HEIF multiple image file).
minor_version: Version of the major brand.
compatible_brands: A list of other brands the file is compatible with. This is crucial for decoders to know if they can parse the file even if they don't support the major_brand directly.
-
mdat (Media Data Box):
- Purpose: Contains the actual raw media data (e.g., compressed HEVC image bitstreams, XMP metadata, Exif data).
- Contents: A contiguous block of bytes. The data within
mdat is generally referenced by other metadata boxes (like iloc). It's often the largest box.
-
meta (Meta Box):
- Purpose: The central metadata container for HEIF files. It holds information about individual image items, their properties, and relationships. It usually appears as a child of a
moov (movie) box or directly at the file root.
- Contents (Key child boxes for HEIF):
hdlr (Handler Reference Box): Specifies the type of handler that processes the data in the meta box (for images, this is usually pict).
pitm (Primary Item Box): Identifies the default or primary image item within the file (by its item ID).
iloc (Item Location Box): Crucial for XMP. Maps item IDs to their actual data locations (offsets and lengths) within the mdat box or other data sources. It contains a list of item_info_entry structures, each detailing an item's ID, data offset(s), and size(s).
iinf (Item Info Box): Crucial for XMP. Provides descriptive information about each item, such as its type (XML , Exif , hvc1 for HEVC), name, content type (image/heic, application/rdf+xml), and whether it's hidden. It contains a list of item_info_entry structures.
iprp (Item Properties Box): Crucial for XMP (indirectly). Defines various properties and associates them with items. This includes properties like:
ispe (Image Spatial Extent Box): Image dimensions (width, height).
pixi (Pixel Information Box): Pixel format (channels, bits per channel).
hvcC (HEVC Configuration Box): HEVC decoder configuration record.
dref (Data Reference Box): References external data sources (if data is not in mdat).
iref (Item Reference Box): Defines relationships between items (e.g., a thumbnail item referencing a full-resolution image item, or a derived image referencing its base images).
idat (Item Data Box): Can contain small metadata items directly within the meta box, instead of referencing mdat. Less common for larger items like XMP.
-
moov (Movie Box):
- Purpose: In traditional video/audio files, this box contains all the metadata for tracks, samples, etc. In HEIF, it might still exist, especially if the HEIF file is embedded within a video stream, but the
meta box is where the image-specific metadata resides. A meta box can be a child of moov.
Simplified HEIF Structure Example:
[ftyp]
[mdat]
[moov]
[mvhd] (Movie Header Box)
[meta]
[hdlr] (Handler Reference)
[pitm] (Primary Item)
[iloc] (Item Location - Maps item ID to data in mdat)
[iinf] (Item Info - Describes items, e.g., type="XML ")
[iprp] (Item Properties - Dimensions, pixel info, etc.)
[iref] (Item Reference - Relationships between items)
(Note: A meta box can also be a top-level box, immediately after mdat or ftyp, especially in single-image HEIF files.)
Rewriting Syntax When Adding Additional XMP Metadata
When you add new XMP metadata to a HEIF file, you're essentially adding a new "item" to the file. This involves placing the XMP data itself and then updating the metadata boxes that describe and locate this new item. Because the ISOBMFF structure relies heavily on precise byte offsets and sizes, adding data almost always requires a cascading series of updates to parent box sizes.
Here's an outline of what parts of the syntax would need to be rewritten:
-
Append XMP Data to mdat Box:
- Action: The XMP packet (the XML string) needs to be appended to the existing
mdat box. This means writing the new data to the end of the file or inserting it into the mdat's current data block.
- Syntax Changes:
mdat box size: This field must be updated to reflect the original size plus the size of the new XMP data.
- File Size: The overall file size will increase. Any top-level box
size fields (e.g., for moov if mdat is its child, or the file size if mdat is root) will need to be updated.
-
Update iloc (Item Location Box):
- Purpose: To inform the parser where the new XMP data resides within the file.
- Action: A new item entry must be added to the
iloc box's list of items.
- Syntax Changes:
iloc box size: Will increase by the size of the new item entry.
item_count: Increment this field (usually iloc has an item_count at its start).
- New
item_ID: Assign a unique ID to the XMP item. This ID must be different from all existing item IDs in the file.
base_offset: This specifies the starting byte offset of the XMP data. This will typically be the offset of the mdat box, or 0 if mdat is considered the base.
extent_count: Usually 1 for a single contiguous block of XMP data.
extent_offset: The offset of the XMP data relative to the base_offset (i.e., its position within the mdat data block).
extent_length: The exact byte length of the XMP metadata.
-
Update iinf (Item Info Box):
- Purpose: To describe the new XMP item's type and content.
- A must be added to the box's list.
Summary of Rewritten Syntax:
- Content: The raw XMP byte stream appended to
mdat.
mdat box: size field.
iloc box: size field, item_count, and a new item_info_entry with its item_ID, base_offset, extent_offset, and extent_length.
iinf box: size field, item_count, and a new item_info_entry with its item_ID, item_type ('XML '), and content_type (application/rdf+xml).
meta box: size field.
- Any parent container boxes:
size fields (e.g., moov).
- Overall file size: The total byte length of the file will increase.
Important Considerations:
- Byte Order: All multi-byte integers in ISOBMFF are big-endian.
- Unique IDs: Ensure the new
item_ID for your XMP data is genuinely unique within the HEIF file. You'd typically find the highest existing ID and increment it.
- Tooling: Manually modifying HEIF files at the byte level is extremely complex and error-prone. It's highly recommended to use robust libraries or tools designed for ISOBMFF manipulation (e.g.,
mp4box for ISOBMFF structures, or more abstract image libraries that handle HEIF/XMP). ExifTool is also a powerful command-line utility that can read and write XMP to HEIF, abstracting away the box-level complexities.