Decoding runs in three stages for each channel. The entropy decoder turns the bit stream into residuals, the predictor turns the residuals into samples, and in a channel pair the stereo matrix rebuilds left and right. This page follows Apple’s reference decoder.
Adaptive Golomb decoding
The coder keeps a running mean of the values it has seen, mb, and derives
each code’s parameter from it. It starts at the cookie’s mb value.
The adaptation rate is pb × pbFactor ÷ 4, from the cookie’s
pb and the element’s pbFactor.
| Constant | Value | Purpose |
|---|---|---|
| QBSHIFT | 9 | Fractional bits of the mean |
| QB | 512 | 1 << QBSHIFT |
| PB0 | 40 | Default pb |
| MB0 | 10 | Default mb |
| KB0 | 14 | Default kb, the largest k |
| MAX_RUN_DEFAULT | 255 | Default maxRun |
| MMULSHIFT | 2 | Used for the zero-run test |
| MDENSHIFT | 6 | QBSHIFT − MMULSHIFT − 1 |
| MOFF | 16 | 1 << (MDENSHIFT − 2) |
| BITOFF | 24 | Used for the zero-run k |
| MAX_PREFIX_16 | 9 | Longest prefix for a zero-run length |
| MAX_PREFIX_32 | 9 | Longest prefix for a sample value |
| N_MAX_MEAN_CLAMP | 0xFFFF | Larger values reset the mean |
| N_MEAN_CLAMP_VAL | 0xFFFF | The value the mean resets to |
For each value:
m = mb >> QBSHIFT, andkis ⌊log₂(m + 3)⌋, capped at the cookie’skb. Thenm = (1 << k) − 1.- Count the 1 bits up to a 0: this is the prefix. The 0 is consumed with it.
- A prefix of 9 or more is an escape: only 9 bits are consumed (no 0 after them), and the value follows as a raw field as wide as the channel’s predicted bits.
- Otherwise, if
kis 1, the value is the prefix. If not, peek atkbits asv; the value isprefix × m. Ifvis under 2, onlyk − 1bits are consumed. If not, allkare, andv − 1is added. - Add
zmode(below). A set low bit means negative: the residual is−(n + 1) ÷ 2when odd andn ÷ 2when even. - Update the mean:
mb = pb × n + mb − ((pb × mb) >> QBSHIFT), withnincludingzmode, in unsigned 32-bit arithmetic. If the value read (beforezmodeis added) exceeds 0xFFFF, the mean becomes 0xFFFF.
The mean starts again from the cookie’s mb for each channel of each
element.
Zero runs
After a value, zmode resets to 0. If mb × 4 is below 512 and
samples remain, a run follows: zmode becomes 1, then the run length is read
with the same coding, using
k = lead(mb) − 24 + ((mb + 16) >> 6), where lead is the
number of leading zero bits in 32 bits, and m = ((1 << k) − 1) limited
to the bits of (1 << kb) − 1. Two things differ from a value: there is no
special case for k of 1, and the escape after a prefix of 9 is a 16-bit field.
That many zero residuals follow; a run that would pass the end of the element is an error.
After every run, whatever its length, the mean mb becomes 0 (ag_dec.c line 352). If the length is 65535 or more, zmode also returns to 0.
Because zmode stays 1 after a run shorter than 65535, the value after it
decodes one higher than its code and can never be 0: a run takes in every zero up to the next
nonzero value.
The adaptive predictor
The predictor runs over the residuals with the element’s num coefficients and
denShift.
- The first sample is the first residual, unchanged.
- With no coefficients the remaining samples are the residuals, unchanged.
- With 31 coefficients the code is a plain first-order integrator: each output is the
previous output plus the residual, wrapped to the channel’s bit width. The first pass
when
modeis not 0 runs this way. - Otherwise the first
numsamples after the first use that same first-order rule. Full prediction starts at the next sample.
For sample j, with top the sample num + 1 places
back:
prediction = top + ((Σ coef[k] × (out[j−1−k] − top) + half) >> denShift)
out[j] = wrap(residual[j] + prediction)
Here half is 1 << (denShift − 1), and wrap
discards the bits above the channel’s width and sign-extends.
Coefficient adaptation
Each coefficient steps by 1 according to the sign of the residual and the sign of its
difference from top. Steps run from the oldest coefficient towards the newest,
and each updated coefficient subtracts its weighted contribution from a running budget
that starts as the residual. When the budget crosses zero, the remaining coefficients
stay as they are. A zero residual changes nothing.
Exactly, for the sample at j, with r the residual as read (before the prediction is added),
dk = top − out[j−1−k], sk the sign of dk (−1, 0 or 1) and
budget = r: for k from num − 1 (the oldest sample) down to 0, if
r is positive, coef[k] −= sk; if r is negative,
coef[k] += sk. Then budget −= (num − k) × (|dk| >> denShift) for a positive
r and budget += (num − k) × (|dk| >> denShift) for a negative one; the loop ends after the
coefficient whose budget reaches 0 or less (positive r) or 0 or more (negative r). A coefficient is a signed 16-bit
number and wraps. The adapted coefficients carry from one sample to the next within the element; each element starts from the coefficients it stores, and the adapted values are discarded at its end.
The 4 and 8 coefficient code in the reference is the same rule unrolled. (dp_dec.c, unpc_block, the general case.)
The stereo matrix
A CPE decodes two channels, U and V. With mixRes not zero:
L = U + V − ((mixRes × V) >> mixBits)
R = L − V
With mixRes zero, L is U and R is V. Any shifted-off bits are put back after
this step.
Output by bit depth
| bitDepth | Sample width | Notes |
|---|---|---|
| 16 | 16 bits | Direct |
| 20 | 24 bits | Shifted left by 4, so the bottom 4 bits are 0 |
| 24 | 24 bits | Direct; low bytes restored when bytesShifted is not 0 |
| 32 | 32 bits | Direct; low bytes restored when bytesShifted is not 0 |
Source: Apple’s ALAC repository, aglib.h and ag_dec.c (the constants of the table above: QBSHIFT, PB0, MB0, KB0, MAX_RUN_DEFAULT, MMULSHIFT, MDENSHIFT, MOFF, BITOFF, MAX_PREFIX_16, MAX_PREFIX_32, N_MAX_MEAN_CLAMP, N_MEAN_CLAMP_VAL), matrix_dec.c (the 20-bit shift by 4) and ALACDecoder.cpp; compared by scripts/alac/spec-check.mjs.