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, and Apple’s own encoder (afconvert, here
for 5.1 audio) writes it so: the alac box holds only the 24-byte
ALACSpecificConfig, and the channel layout follows as a separate 24-byte chan
box, the next box in the same sample entry (whose size, 96, covers both). The 48-byte form of
the cookie, config and layout together, is what CAF’s kuki chunk holds. A parser
should still 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.
Matroska
A Matroska (.mka, .mkv) track with the codec ID A_ALAC holds one ALAC
packet in each block (no lacing). Its CodecPrivate is the magic cookie bare: the 24-byte
ALACSpecificConfig, and, when the writer keeps it for more than two channels, the 24-byte channel layout
information after it. It has none of the wrappers an MP4 file puts around it: no frma and
alac atom headers (FFmpeg’s Matroska writer cuts the 12-byte header off its ALAC extradata). A file written with
ffmpeg -i song.m4a -c copy song.mkv has a 24-byte CodecPrivate in the stereo and the 5.1 case, with no layout
information, so the channels are taken in the order the cookie’s channel count gives (the same order an MP4 file
without a chan box has). The track’s sample rate and channel count in the Audio element repeat the
cookie’s; the cookie wins. Nothing in Matroska carries the iTunSMPB gapless values; the container’s
CodecDelay and DiscardPadding exist but the encoders seen here do not write them for ALAC. The explorer
applies them as the Matroska specification says: the delay is dropped from the start, the last block’s padding from its end
(a negative padding from the start). FFmpeg applies them only inside one packet (a delay of more than one packet, or a padding longer than
the last block, is dropped or ignored), so for those values the two differ; a padding on a block that is not the last is shown, not applied.
The Matroska codec mapping lists A_ALAC with one rule: the CodecPrivate holds ALAC’s magic cookie,
meaning the codec-specific configuration and the optional channel layout information. It says nothing about atom headers, lacing,
CodecDelay or DiscardPadding for ALAC, so those points above rest on the files and on FFmpeg’s writer, not on the mapping.
Source: the “Storing Apple Lossless Magic Cookie in Audio Files” section of Apple’s ALACMagicCookieDescription.txt (the MP4 sample entry sizes and the CAF kuki chunk) and Apple’s Core Audio Format Specification 1.0 (the desc, kuki, chan, pakt and data chunks). The CAF desc format flags 1 to 4 for the bit depths, the frma and terminator atoms of the QuickTime wrapper are read from the ALAC decoder source and the cookie description only as far as they say; the flags table is not defined in either text, it is what Apple’s encoder writes (checked on files made with afconvert).