The magic cookie is ALAC’s configuration: everything the decoder must know before the
first packet. It is either 24 bytes (ALACSpecificConfig alone) or 48 bytes
(with channel layout information after it). Every field is big-endian, whatever the
container. A container stores the cookie unchanged and hands it to the decoder unchanged;
MP4 and CAF wrap it differently.
ALACSpecificConfig
| Offset | Size | Type | Field | Meaning |
|---|---|---|---|---|
| 0 | 4 | uint32 | frameLength | Samples per channel in a packet. 4096 is the default and the safe choice for compatibility |
| 4 | 1 | uint8 | compatibleVersion | Must be 0. The reference decoder refuses a higher value |
| 5 | 1 | uint8 | bitDepth | Bit depth of the source PCM, at most 32. The reference decoder handles 16, 20, 24 and 32 |
| 6 | 1 | uint8 | pb | Entropy coder tuning. Set to 40. Decoders use the value stored here |
| 7 | 1 | uint8 | mb | Entropy coder tuning. Set to 10 |
| 8 | 1 | uint8 | kb | Entropy coder tuning. Set to 14 |
| 9 | 1 | uint8 | numChannels | Channels, 1 to 8 |
| 10 | 2 | uint16 | maxRun | Set to 255. Currently unused |
| 12 | 4 | uint32 | maxFrameBytes | Largest packet in bytes; 0 means unknown |
| 16 | 4 | uint32 | avgBitRate | Average bit rate in bits per second; 0 means unknown |
| 20 | 4 | uint32 | sampleRate | Sample rate in hertz |
Apple’s document calls pb, mb and kb unused
tuning parameters that should be set to the values above, but the decoder does read them:
see the entropy coder.
Worked example
A 16-bit stereo stream at 44,100 Hz, with 4096 samples to a packet and the two sizes left unknown:
00 00 10 00 frameLength 4096
00 compatibleVersion 0
10 bitDepth 16
28 0A 0E pb 40, mb 10, kb 14
02 numChannels 2
00 FF maxRun 255
00 00 00 00 maxFrameBytes unknown
00 00 00 00 avgBitRate unknown
00 00 AC 44 sampleRate 44100
Channel layout information
This optional 24-byte block follows the config. Without it, one channel means mono, two mean left and right, and more than two carry no particular roles.
| Offset | Size | Type | Field | Meaning |
|---|---|---|---|---|
| 0 | 4 | uint32 | channelLayoutInfoSize | 24 |
| 4 | 4 | fourcc | channelLayoutInfoID | chan |
| 8 | 4 | uint32 | versionFlags | 0 |
| 12 | 4 | uint32 | channelLayoutTag | One of the tags below |
| 16 | 4 | uint32 | reserved1 | 0 |
| 20 | 4 | uint32 | reserved2 | 0 |
A tag is a layout code in its high 16 bits and the channel count in its low 16. ALAC uses exactly one tag for each channel count:
| Channels | Tag | Name | Channel order |
|---|---|---|---|
| 1 | 0x00640001 | Mono | C |
| 2 | 0x00650002 | Stereo | L R |
| 3 | 0x00710003 | MPEG_3_0_B | C L R |
| 4 | 0x00740004 | MPEG_4_0_B | C L R Cs |
| 5 | 0x00780005 | MPEG_5_0_D | C L R Ls Rs |
| 6 | 0x007C0006 | MPEG_5_1_D | C L R Ls Rs LFE |
| 7 | 0x008E0007 | AAC_6_1 | C L R Ls Rs Cs LFE |
| 8 | 0x007F0008 | MPEG_7_1_B | C Lc Rc L R Ls Rs LFE |
Source: Apple, Apple Lossless Format “Magic Cookie” Description (ALACMagicCookieDescription.txt in the open-source ALAC repository, Apache License 2.0), sections ALAC Specific Info, Channel Layout Info and Channel Layout Tags, and ALACAudioTypes.h and ALACDecoder.cpp from the same repository (the decoder’s handling of the version, the bit depths and the frma and alac atom headers). Every offset, width and tag is compared with them by scripts/alac/spec-check.mjs.