ALAC has no file format of its own. Its packets live in a container, which has to carry the magic cookie and the size of every packet. This page shows only what is specific to ALAC; see MP4 and CAF for the containers themselves. All fields here are big-endian.
MP4 and M4A
The track’s stsd box holds one sample entry of type alac,
which ends with an alac box holding the cookie.
| Offset | Size | Type | Field | Meaning |
|---|---|---|---|---|
| 0 | 4 | uint32 | Sample entry size | Size of this whole entry |
| 4 | 4 | fourcc | Sample entry type | alac |
| 8 | 6 | bytes | Reserved | 0 |
| 14 | 2 | uint16 | Data reference index | 1 |
| 16 | 8 | bytes | Reserved | 0 |
| 24 | 2 | uint16 | Channel count | Number of channels |
| 26 | 2 | uint16 | Sample size | Bit depth of the source PCM |
| 28 | 2 | uint16 | Pre-defined | 0 |
| 30 | 2 | uint16 | Reserved | 0 |
| 32 | 4 | uint32 | Sample rate | The stream’s rate. MP4 writes this field as 16.16 fixed point; use the rate in the cookie |
| 36 | 4 | uint32 | ALAC box size | 36 for a 24-byte cookie |
| 40 | 4 | fourcc | ALAC box type | alac |
| 44 | 4 | uint32 | Version and flags | 0 |
| 48 | 24 or 48 | bytes | Magic cookie | As the encoder produced it |
Apple’s document gives the ALAC box size as 36 even though the cookie may be 48 bytes. A parser should take the cookie’s length from the enclosing sizes rather than assuming 36.
The older QuickTime wrapper
Older encoders, and QuickTime movies, pass the cookie inside extra atoms, and a decoder must accept this form too. These atoms arrive in order, 56 bytes with a 24-byte cookie:
| Atom | Size | Layout |
|---|---|---|
frma | 12 | Size 12, the type frma, then the format alac |
alac | 36 | Size 36, the type alac, version and flags 0, then the 24-byte cookie |
| Terminator | 8 | Size 8, then a type of 0 |
Apple’s decoder recognises this by looking for frma at offset 4 and skipping
12 bytes, then looking for alac at offset 4 and skipping 12 more.
CAF
Apple’s reference tool writes the chunks in this order: the CAF file header, then
desc, kuki, chan (only for more than two channels),
pakt and data.
| Chunk | ALAC use |
|---|---|
desc | Format ID alac, with the format flags below |
kuki | The magic cookie, unchanged |
chan | The channel layout, using a tag from the list of layout tags |
pakt | The size of every packet |
data | A 4-byte edit count, then the packets |
| Format flags | Source bit depth |
|---|---|
| 1 | 16 |
| 2 | 20 |
| 3 | 24 |
| 4 | 32 |
The packet table
| Offset | Size | Type | Field | Meaning |
|---|---|---|---|---|
| 0 | 8 | int64 | Number of packets | Packets in the stream |
| 8 | 8 | int64 | Valid frames | Audio frames that are real samples |
| 16 | 4 | int32 | Priming frames | Frames at the start to discard |
| 20 | 4 | int32 | Remainder frames | Frames at the end of the last packet to discard |
| 24 | varies | bytes | Packet sizes | One variable-length integer per packet |
Each size uses seven bits per byte, most significant group first, with the top bit set on every byte except the last. The reference code accepts up to five bytes.