Jump to content
Setting up an AI agent? ReverseEngineering.wiki skill (works from the command line, no configuration): https://reverseengineering.wiki/skill/rewiki/SKILL.md · setup for every client, including MCP: https://reverseengineering.wiki/llms.txt · REWiki:Connect

Format:CHD (MAME Compressed Hunks of Data)

From ReverseEngineering.wiki
Format
Magic MComprHD (4D 43 6F 6D 70 72 48 44) at offset 0
Extensions .chd
Endianness big-endian
License libchdr: BSD-3-Clause
On this wiki Facts · Code · Notes · Topics

MAME's compressed container (.chd, magic MComprHD) for hard disks, CD/GD-ROM/DVD images and laserdiscs: a big-endian header (versions 1 to 5), a hunk map, compressed hunks and a linked list of metadata blobs.

Overview

A CHD (Compressed Hunks of Data) file stores a logical byte stream (a disk or disc image) cut into equal "hunks" (header field hunkbytes), each compressed on its own, plus a map that says where each hunk is and a chain of tagged metadata blobs. For optical discs the metadata holds the track list (tags CHCD, CHTR, CHT2 for CD, CHGT and CHGD for GD-ROM, DVD for DVD) and the hunks hold 2448-byte frames (2352 bytes of sector data plus 96 bytes of subchannel). All numbers in the file are big-endian. The format is defined in MAME (src/lib/util/chd.h); this page was read from the standalone reader libchdr (BSD-3-Clause), whose chd.h carries the format comment.

Header

Every version starts with the 8-byte tag MComprHD, a u32 header length and a u32 version. libchdr's header_read reads those 16 bytes first, rejects a wrong tag, rejects version 0 or above 5, and then requires the length field to equal the fixed size of that version: 76 (v1), 80 (v2), 120 (v3), 108 (v4), 124 (v5). Note that v3 is longer than v4. See the facts on this page for the field tables.

  • v1/v2 describe a hard disk by cylinders, heads and sectors (v2 adds a sector length; v1 assumes 512-byte sectors), with MD5s.
  • v3 and v4 hold flags (bit 0 has a parent, bit 1 writable), one compression id, hunk count, logical bytes, metadata offset and hunk bytes; v3 has MD5 and SHA-1, v4 only SHA-1 values. The map is a plain array right after the header (16-byte entries from v3, 8-byte before).
  • v5 (current) replaces the single compression id by four four-character codec codes, adds a map offset and a unit size, and has no flags field: a parent exists when parentsha1 is non-zero, and the file is uncompressed (including the map) when compressors[0] is 0.

Version 5 map

Uncompressed v5 map: one u32 per hunk, the file offset divided by hunk size. Compressed v5 map: a 16-byte header (compressed length, 48-bit offset of the first hunk, CRC-16 of the expanded map, and the bit widths lengthbits, selfbits, parentbits), then a bitstream. Pass one is a Huffman-coded (16 symbols, max 8 bits, tree stored run-length encoded) sequence of per-hunk types with run-length pseudo-types; pass two reads, per hunk, a length (lengthbits bits) and a 16-bit CRC for codec types, a CRC for stored hunks, or a self/parent reference. Each expanded entry is 12 bytes. See the fact "V5 compressed map decoding".

Codecs

v5 stores up to four codec codes in the header; map types 0 to 3 select among them. Codes defined in chd.h: zlib, lzma, huff, flac, zstd, avhu (A/V Huffman for laserdiscs) and the CD wrappers cdzl, cdlz, cdfl, cdzs. A CD wrapper splits each hunk into sector data and subchannel data, compresses them separately (cdlz and cdfl, whose names suggest a different codec for the sector stream and for CDDA, were not read in detail here; cdzl and cdzs use zlib or zstd for the sector stream) and stores a per-frame bit telling whether the sync header and ECC can be regenerated. See the fact "CD codec hunk layout".

Metadata

The header's metaoffset points to the first of a linked list of blobs, each with a 16-byte header (tag, flags and length, next-offset). The CD track metadata is ASCII text such as TRACK:1 TYPE:MODE1_RAW SUBTYPE:NONE FRAMES:1234 PREGAP:0 PGTYPE:MODE1 PGSUB:NONE POSTGAP:0 for CHT2 (the format string is in chd.h; example values are illustrative, Unverified).

Identification

Unverified: a CHD is quickly recognized by the first 8 bytes MComprHD; the version at offset 12 (big-endian) tells which header table applies.

Code & repositories

  • https://github.com/rtissera/libchdr @ 47d794151b2b64f6aba60304e6480f4f5a2bc24b · license: BSD-3-Clause
    Standalone CHD reader (BSD-3-Clause) with the format comment in chd.h and the CD codecs/ECC in libchdr_cdrom.c.
    Tested on: source read, not run: read only, no build · checked 2026-10-19; last activity 2026-10-11

See also

Format:CD-ROM raw sector layouts for what the hunks of a CD CHD contain.