Around the packets an ALAC file in MP4 carries what any MP4 file carries: sample tables that
say where each packet is and how long it lasts, and a meta box of iTunes-style
tags. This page shows what is specific to ALAC files, and what Apple’s encoder
(afconvert, the same one iTunes and Apple Music use) and FFmpeg write for
them. The boxes themselves are described on the MP4 pages: see
sample tables and
metadata boxes. All numbers are big-endian.
Sample tables
Every ALAC packet is one MP4 sample, and every packet can be decoded by itself, so no
sync-sample box (stss) is written: with none, all samples are sync samples.
The tables of a 2-second, 44,100 Hz stereo file written by afconvert hold:
| Box | What it holds for ALAC | Example |
|---|---|---|
stts | Every packet lasts frameLength (normally 4096) units, in a timescale equal to the sample rate. The last packet is shorter when the audio does not fill it | 21 packets of 4096, then 1 of 2184: 88,200 samples |
stsz | The size in bytes of every packet. The sizes differ, because compression does: a loud passage costs more bytes than a quiet one. A fixed size appears only for audio that cannot be compressed, which is stored uncompressed | 1,674 bytes for the first packet; 22 entries |
stsc | How many packets sit together in a chunk. afconvert writes six to a chunk, with a shorter run in the last chunk | chunk 1: 6 packets; from chunk 4: 4 packets |
stco or co64 | Where each chunk starts in the file. co64 (64-bit offsets) appears when the data lies past 4 GiB | 4 chunk offsets, the first at 4096 |
stss, ctts | Absent: every packet is a sync sample, and decoding order is playing order |
The sample entry’s channelcount and samplesize repeat the cookie’s
numbers; the cookie’s are the ones to trust (see the sample entry).
The media timescale is the sample rate, so a packet’s duration in the table is a number of samples.
The explorer lists every packet in its Samples tab (offset, size, time, and which elements it holds) and, under Sound, a table of every packet’s elements with their fields; each row links to the packet’s bytes.
Gapless playback: iTunSMPB
A free-form item named iTunSMPB in the namespace com.apple.iTunes
tells a player how many samples of the stored audio are not music, so albums can play without
gaps. Its value is text: a space, then twelve fields of hexadecimal digits separated by
spaces.
| Field | Digits | Meaning |
|---|---|---|
| 1 | 8 | Always 00000000 |
| 2 | 8 | Encoder delay (priming): samples at the start to skip |
| 3 | 8 | End padding: samples at the end of the last packet to drop |
| 4 | 16 | Original length: the number of real samples |
| 5 to 12 | 8 each | Written as 00000000 by every encoder and every file seen here (afconvert and about 25 iTunes-style AAC files). Apple’s Music program reads the text only as far as field 6 (fields 1 to 6 are parsed, 7 to 12 are never read, and the text must be 115 or 116 characters long) and keeps fields 5 and 6; what it does with them is not traced, and no file with another value was found |
The tag written by afconvert for the file above reads
00000000 00000000 00000778 0000000000015888 00000000 …: no delay, 0x778 = 1,912
samples of padding and 0x15888 = 88,200 samples of sound. The packets hold 22 × 4096 =
90,112 = 1,912 + 88,200 samples, so the three numbers agree with the file. ALAC needs no
priming (the codec adds no delay), so the delay is 0, unlike AAC’s usual 2,112. Apple’s
encoder shortens the last packet’s duration in stts to 2,184 as well, so a player
that follows the tables alone also stops at the right sample. A file may instead
carry an edit list (edts); the explorer cuts the waveform to it. FFmpeg writes no
iTunSMPB for ALAC.
What the explorer does with it
The explorer trims the waveform, the levels and Play by the values when all of these hold: the item
iTunSMPB is in the file’s ilst; the track is the first sound track; no
edit list cuts the sound (an edit list is the container’s own statement and wins; an edit list that plays
the whole media from its start does not count); and the numbers fit the packets
(priming + length is no more than the frames the sample table holds, and the length is not 0). The sound is then the
length frames starting priming frames in. The padding word is not used on its own: it is what is
left after them, and when the last sample duration in stts is already cut to the end of the sound (afconvert
writes it so, and its tag says 0 priming and a length equal to the cut frame count) nothing changes. The Sound
tab’s Gapless trim (iTunSMPB) row says whether it was applied and, if not, why.
The rule was checked against FFmpeg (libavformat/mov.c, mov_read_custom). FFmpeg reads the same text (a
reserved word, priming, remainder, length: three numbers of at most 16 hexadecimal digits, each a number up to a limit) and
uses it in two ways. The priming becomes the stream’s initial padding, which only becomes samples to skip for AAC
(mov_read_header); for ALAC it is never applied. The remainder cuts the last frames of the stream
(first_discard_sample = duration − remainder, as discard padding on the packets) only when the media is longer
than both the remainder and the length word, so a file whose stts is already cut (afconvert’s)
plays whole. For priming 0 and a tag that agrees with the packets the explorer and FFmpeg therefore give the same frames,
sample for sample (checked on files built with afconvert and a patched tag); for a priming above 0 the explorer’s sound
is FFmpeg’s with that many frames removed from the start (the meaning iTunes gives the word; no Apple player was run here to confirm it for ALAC). ALAC
itself has no delay, so real files carry priming 0; AAC’s usual value is 2,112.
Another free-form item, iTunNORM, holds a run of hexadecimal numbers of volume
normalisation data (Sound Check); how iTunes computes them is not published, and the explorer shows
the text unchanged. The value is ten numbers for a stereo file (five for a mono file, see below), each a space and eight hexadecimal digits (signed 32 bits). The
beets project’s mediafile library (mediafile/utils/soundcheck.py, MIT licence, lines 10 to 50;
it writes iTunNORM and reads it back, so it is reverse-engineered, not Apple’s) takes numbers 1 and 2 as the left and
right loudness in units Apple does not name, with 1000 as the reference: the gain in decibels is −10 × log10(larger
÷ 1000). Numbers 7 and 8 are the left and right peak sample values (32768 is full scale at 16 bits). The beets code
also writes numbers 3 and 4 as 1 and 2 against a reference of 2500 and numbers 5, 6, 9 and 10 as 0; that is what
beets writes, and the real files below do not follow it. Treat all of this as inferred from one program, not as
Apple’s definition. A loudness check on four of those files (FFmpeg EBU R128 integrated loudness against the gain
implied by numbers 1 and 2) gave no constant offset, because the measure is not EBU R128 but the band-weighted
block energy described below.
Real files do not agree with all of that. Twenty-odd AAC .m4a files that Apple’s iMovie ships
(Trailers sound tracks and effects, written by iTunes-style tools, each with an iTunSMPB and an iTunNORM) were decoded with
FFmpeg and compared with their own numbers. Numbers 7 and 8 are the largest absolute sample of the left and right
channel of the decoded audio, or that value minus 1 (checked on 7 files, for example a left peak of 31,344 giving
0x7A6F = 31,343). Numbers 3 and 4 are not 2.5 times numbers 1 and 2 (a file with 1 and 2 of 0x15E and 0x4D8 has 3
and 4 of 0x2A4 and 0x936), and numbers 5, 6, 9 and 10 are not zero (0x103C, 0xEF7, 0x10C7, 0xF82 in one file; 0x0E8 and
0x0D0 in a short effect). They are not sample counts and not decibels: they are times in milliseconds (next paragraph).
The program that makes the numbers is Apple’s SoundCheckAnalyzer (and its
BufferedSoundCheckAnalyzer) in AudioToolboxCore; its disassembly (the symbols are in the
library: BufferedSoundCheckAnalyzer::ReceiveBufferedBlock, SoundCheckAnalyzer::PopulateSoundCheckDict)
gives this. The audio is cut into blocks of 1,024 frames with no overlap, windowed, and transformed by a 1,024-point real FFT. The samples are 32-bit floats (16-bit values divided by 32,768) and the window is a
table of 1,024 floats, sFFTWindowCoeffs, which is 32,768 × (0.5 − 0.5·cos(2π(n + 0.5) ÷ 1,024)), a Hann window shifted half a sample
(its sum is 224). The FFT is Apple’s packed real transform, whose output is twice the plain DFT, and bin 0 holds the DC value
in its real part and the Nyquist value in its imaginary part. In each of 25 critical (Bark) bands, a table of 16-bit pairs (centre, upper edge) in
the library gives centres 50, 150, 250, 350, 450, 570, 700, 840, 1,000, 1,175, 1,370, 1,600, 1,850, 2,150, 2,500, 2,900, 3,400, 4,000, 4,800, 5,800,
7,000, 8,500, 10,500, 13,500 and 19,500 Hz and upper edges 100, 200, 300, 400, 510, 630, 770, 920, 1,080, 1,270, 1,480, 1,720, 2,000,
2,320, 2,700, 3,150, 3,700, 4,400, 5,300, 6,400, 7,700, 9,500, 12,000, 15,500 and 22,050 Hz (a band whose edge is above half the sample rate is
dropped; the bins of a band run from the rounded position of the previous edge, edge × 1,024 ÷ rate + 0.5 truncated, up to but not including that of its own edge),
the power of the bins (real² + imaginary², times 0.25) is summed and multiplied by 10−T(f)/10, where
T is the threshold of hearing in quiet of Terhardt, T(f) = 3.64·f−0.8 − 6.5·e−0.6(f−3.3)² + 0.001·f4
with f in kHz taken at the band’s centre. The 25 products are added to give one
energy E per block and channel. Per channel the analyser then keeps: the mean of E over the blocks whose E is above
5×109 (numbers 1 and 2, each the mean divided by 109 and truncated); the largest E (numbers 3 and 4, also
divided by 109); the time stamp of the block with the largest E (numbers 5 and 6); the largest |sample| × 32768 truncated to an integer (numbers 7 and 8);
and the time stamp of the block where that peak was first reached (numbers 9 and 10). A block is replaced only when the new value is
strictly larger. The time stamp is in milliseconds: the analyser sets it before each block from the frame position of the block start, scaled by
1000 ÷ sample rate, and the fit on real files is floor(k × 1024 × 1000 ÷ rate) with k the index of the block in the untrimmed stream
(priming frames included; FFmpeg: -flags2 +skip_manual). Every number then matches exactly: the reference script
scripts/alac/itunnorm-ref.mjs reproduces all ten numbers of all 34 stereo 44.1 kHz AAC files in iMovie’s Trailers folder, and the test
parser-test/test-alac-itunnorm.mjs checks them. Apple’s own writer can also be run: afconvert -f m4af -d alac --soundcheck-generate
adds an iTunNORM tag to the file it writes. Eight files made that way (a 6-second sine plus pink noise; mono at 32,000, 44,100 and 48,000 Hz, stereo at 8,000, 11,025,
22,050, 48,000 and 96,000 Hz, in samples/alac/soundcheck/) show three things. A mono file’s tag holds five numbers, not ten: the mean, the
largest energy, its time stamp, the peak and its time stamp, all of channel 0. The largest energy, the peak and both time stamps match the reference script exactly on all eight files, at every rate.
On these files the time stamp is rounded to the nearest millisecond, floor(k × 1024 × 1000 ÷ rate + 0.5), where the older iMovie files fit the plain floor above.
The mean matches exactly on all eight. The analyser takes its input in chunks of 32,768 frames, zero-filling the last; the reference counts all whole chunks, and in the final chunk only a multiple of 2,048 frames
(the odd last block and the partial block are left out). The one exception fitted is a stereo file whose final chunk is under 2,048 frames: it is analysed whole, partial block included.
This was found by bisecting the input gain until the tag stepped (which gives Apple's unrounded mean) and checked on trimmed copies.
The Sound Check gain that players apply is then derived from numbers 1 and 2 (the −10 × log10(n ÷ 1000) of the beets note).
Apple’s Music program (read statically, not run) parses the value as ten fields of eight hexadecimal digits, each preceded by a space, in a text of 89 or 90 characters (the leading space is optional), keeps them as ten 32-bit numbers and writes them back in the same form with 8 uppercase hexadecimal digits each, so no number is derived in the text step.
Chapters
Two ways exist and the explorer reads both. FFmpeg and Nero’s tools write a chpl box
in udta: a full box of version 1, then four reserved bytes, a one-byte count, and for
each chapter a 64-bit start in units of 100 nanoseconds, a one-byte title length and the UTF-8 title.
Two chapters, One at 0 and Two at 1 second, look like this:
00 00 00 29 63 68 70 6C size 41, chpl
01 00 00 00 version 1, flags 0
00 00 00 00 reserved
02 2 chapters
00 00 00 00 00 00 00 00 start 0
03 4F 6E 65 length 3, "One"
00 00 00 00 00 98 96 80 start 10,000,000 units = 1 second
03 54 77 6F length 3, "Two"
Apple’s tools write a text track instead, linked from the audio track by a tref
box of type chap; each of its samples is a two-byte length and the title, with a UTF-16
byte order mark when the title is UTF-16.
Files named .alac
There is no format called .alac. The extension is given by people and tools to a
MPEG-4 file that holds Apple Lossless, to tell it from AAC in .m4a, and the file
inside is exactly the one described here: ftyp, moov and
mdat boxes. Neither Apple nor the open-source codec describes a bare stream of packets
with the cookie in front, so a file of that kind has no defined layout, and the explorer does not guess
one. The explorer opens a .alac file as MPEG-4 and reports a file that is not
MPEG-4 (plain text, for one) as not that format; an MPEG-4 file that holds AAC under the name
.alac is shown as AAC.
Source: the MP4 boxes stts, stsz, stsc, stco and co64 are those of ISO/IEC 14496-12 and the QuickTime File Format; the item list (ilst) names, the data-box type codes and the iTunSMPB layout are as iTunes and afconvert write them. Apple’s ALAC repository defines none of these, and Apple’s QuickTime metadata documentation could not be fetched as text (its pages are served as a script shell), so this page is not compared with a primary text.