Introduction
Apple Lossless (ALAC) is Apple’s lossless audio codec, introduced in 2004 for iTunes and the iPod. Like FLAC it decodes to exactly the original samples, at roughly half the size of uncompressed audio. Apple published the encoder and decoder as open source in 2011, and the format is now royalty-free.
ALAC has no file format of its own. It is almost always stored in an MPEG-4 file with
the extension .m4a, the same extension used for AAC, so the extension
alone does not say whether a file is lossless. It can also be stored in a CAF
file.
Some people and programs name such a file .alac to tell it from AAC. That is
only a different name: inside is the same MPEG-4 file, and there is no separate raw
.alac format (see files named .alac).
To see which codec an .m4a file holds, look at the sample entry in its
stsd box: alac is Apple Lossless, mp4a is AAC.
Structure
An ALAC .m4a file is an MPEG-4 file made of boxes, each a 32-bit size and
a four-character type. It starts with an ftyp box, usually with the brand
M4A . The moov box describes the track: inside its sample
description (stsd) is an alac entry carrying the codec’s
settings, such as frame length, bit depth, channel count and sample rate. The sample
tables give the size and position of every frame. The encoded frames themselves sit
in an mdat box, and tags are stored as iTunes metadata in
moov.
0ftyp file type and brandsvaries…moov track, alac settings, sample tables, tagsvaries…mdat encoded ALAC framesvaries
The pages in the menu describe the magic cookie that configures the decoder, how MP4 and CAF hold it, the packets and elements of the audio data, and the prediction and entropy coding, and the tags, sample tables and gapless values around them.
Opening it in the explorer
Drop an .m4a, .alac, .caf or .mov file onto the
explorer. It reads the MPEG-4 boxes field by field (every byte belongs to a named field), shows every
field of the cookie, and under Sound lists three paged tables: every packet, every element of every
packet (SCE, CPE, LFE, DSE, FIL, END, with its instance tag, bit range, sample count, shift, escape and mix
fields) and every channel’s predictor block (mode, denShift, pbFactor, order and the coefficients). Each row
links to the bytes it covers (a bit range is shown in bits and selects the bytes that hold it).
It decodes the audio with the shared ALAC decoder, draws the waveform and plays it, and the waveform and
the packets’ bytes are linked both ways. Every problem in every packet (a bad element, a packet that is not
as long as the sample table says, a cookie out of range) is listed, counted without a cap and linked to the
mdat box, with the packet’s number and offset. The tags, cover art, chapters and gapless values are
shown too. A CAF file gets the same tables, byte link and problems; an ALAC track in a Matroska file
(A_ALAC) is decoded the same way, with its
CodecDelay and DiscardPadding applied (the tables are not drawn for Matroska). A file that is
not MPEG-4, such as a text file with the name song.alac, is reported as not that format.
Nothing is cut for length: every packet decodes on its own, so in MP4 (.m4a) and CAF the waveform, the exact Peak and RMS levels and Play cover the whole file. The waveform follows the MP4 edit list, or the iTunSMPB gapless values when there is none (the rule), and a CAF file’s priming and remainder frames. The byte link is drawn for the whole sound and for a trim or a single cut of it; an edit list that repeats or inserts silence has no byte link, because one place in the sound would be several places in the file.
Not described
These pages follow Apple’s open-source ALAC codec (downloaded and read: the magic cookie description,
ALACDecoder.cpp, ag_dec.c, dp_dec.c, matrix_dec.c), Apple’s
CAF and MP4 documentation, and what afconvert and FFmpeg write. The viewer’s decoder gives
the same samples as FFmpeg’s for every file built with afconvert (16, 20, 24 and 32 bits;
1 to 8 channels; 8,000 to 192,000 Hz; silence, noise, full-scale squares and sines; 1 millisecond to
20 seconds) and with FFmpeg’s own encoder.
The behaviour no encoder here writes was checked with streams built by a small ALAC encoder written for the
purpose (every case made from the decoder’s own rules, so the samples that go in are known) and decoded by two
independent decoders, Apple’s own (Core Audio, through afconvert) and FFmpeg’s: 117 streams, Apple’s
decoder gives exactly the input on all 102 it accepts and the viewer on all of them. That covers predictor
modeU and modeV 1, 2 and 15 at 16, 20 and 24 bits (an extra first-order pass; FFmpeg
decodes these wrongly, Apple’s decoder agrees with the viewer), orders 0 to 31, every bytesShifted,
uncompressed elements at every depth, partial packets, LFE, DSE and FIL elements, mix values, pbFactor and Golomb
parameters, frame lengths of 16 to 65,536, and a packet without an END element (Apple’s decoder stops once all channels are
decoded). A CCE or PCE element, bytesShifted 3, a sample count above the frame length and a CPE that does
not fit the channel count are errors in Apple’s decoder and in FFmpeg’s, and the viewer reports each and stops the
packet there; a stream whose unused header bits are not 0 is decoded by both of them and reported by the viewer.
Apple’s decoders drop shifted bytes at 16 bits (the reference source copies only 24- and 32-bit shifts back) and
refuse 32-bit packets with fewer than two shifted bytes; no encoder writes either, the viewer decodes both as the
source’s syntax says. A cookie whose frameLength is above 16,777,216 is not decoded.
Unknown
- Reference: the meaning of iTunSMPB fields 5 to 12 and of the numbers in iTunNORM is not published; the
viewer shows them as text. Tried: Apple’s ALAC source (no mention), FFmpeg’s
mov.c(reads only the first four words), every fileafconvertwrites here (fields 5 to 12 are zero), a web search for a primary Apple document (only forum reverse engineering, which disagree).
There is no bare .alac stream: a cookie followed by packets is described nowhere and no tool writes it, so
a file with that name is an MP4 file or not ALAC (the explorer reports it as such). That is the scope of the format, not a gap.
Also used for
- AAC audio in MPEG-4 – MP3’s successor: ADTS frames in .aac, or MPEG-4 boxes in .m4a.
Links
Software
- Apple Music – Apple’s music app, which rips CDs to ALAC and plays it (macOS, Windows, iOS)
- foobar2000 – free music player and converter (Windows, macOS, mobile)
- VLC media player – free player for almost any audio or video file (Windows, macOS, Linux, mobile)
- FFmpeg – command-line tool that encodes and decodes ALAC (Windows, macOS, Linux)
Developers
- Apple Lossless Audio Codec – Apple’s open-source encoder and decoder (C++)
- Mutagen – library that reads and writes MP4 tags (Python)
Reference
- ALAC magic cookie description – Apple’s description of the codec’s configuration and how containers store it