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.
- A prefix of 9 or more is an escape: 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. Ifnexceeds 0xFFFF, the mean becomes 0xFFFF.
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. That many zero residuals follow. If the length
is 65535 or more, zmode returns to 0. The mean then becomes 0.
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.
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 |