After entropy decoding, the values pass through up to three adaptive filters, then a predictor, and finally stereo reconstruction. This page follows FFmpeg’s decoder, which handles file versions 3800 to 3990.
Filters by compression level
The level picks a row: row = level ÷ 1000 − 1. An order of 0 means the stage
is absent, so Fast has no filter stage at all.
| Level | Name | Filter orders | Fractional bits |
|---|---|---|---|
| 1000 | Fast | none | |
| 2000 | Normal | 16 | 11 |
| 3000 | High | 64 | 11 |
| 4000 | Extra high | 32, 256 | 10, 13 |
| 5000 | Insane | 16, 256, 1280 | 11, 13, 15 |
How a filter adapts
Each filter keeps a history and a set of coefficients, predicts by their dot product, and changes the coefficients according to the sign of the result. The history values are clipped to 16 bits. The size of the adaptation step depends on the version:
| Version | Adaptation value | Older values halved |
|---|---|---|
| Below 3980 | ±4 from the sign of the result; 0 when the result is 0 | At offsets −4 and −8 |
| 3980 and later | The sign of the result times 8, doubled for each of two thresholds passed. The thresholds are 3 × the running average and 4/3 × the average, with the average updated by (|result| − average) ÷ 16 | At offsets −1, −2 and −8 |
Predictors by version
| Version | Predictor |
|---|---|
| Below 3930 | Older filters: a fast first-order form at level 1000, and longer filters at 3000 and 4000 |
| 3930 to 3949 | A single-stage predictor of four coefficients |
| 3950 and later | The stereo predictor described below, with 64-bit sums where needed |
The 3950 predictor
Stereo decoding uses two channels called Y and X. Each sample is predicted from its own channel’s last four values with coefficient set A and, using the other channel, five values with set B. The output is the input plus the combined prediction shifted right by 10. Each coefficient then moves by 1, or not at all, according to the signs of the input and of its history value.
| Item | Value |
|---|---|
| History buffer | HISTORY_SIZE 512, plus PREDICTOR_SIZE 50 |
| Predictor order | PREDICTOR_ORDER 8 |
| Delay offsets | YDELAYA 50, YDELAYB 42, XDELAYA 34, XDELAYB 26 |
| Adaptation offsets | YADAPTCOEFFSA 18, XADAPTCOEFFSA 14, YADAPTCOEFFSB 10, XADAPTCOEFFSB 5 |
| Initial A coefficients | 360, 317, −109, 98 |
| Initial B coefficients | All 0 |
A mono stream uses only the Y side and set A. For 24-bit stereo, FFmpeg runs a second pass on a copy and watches for overflow past 2²³; it calls this interim mode. There is no clipping of the output in this predictor.
An NN filter, exactly
The filter keeps order coefficients (16-bit, all 0 at the start of a frame), the last
order outputs clipped to 16 bits (also 0), and for each of them an adaptation value (16-bit,
0). For each value v of the frame, in order:
dot= the sum of coefficient × output over theorderpositions, the newest output with the last coefficient, in 32-bit wrap-around arithmetic.out = v + ((dot + 2fraction−1) >> fraction)(the shift is arithmetic, and the addition is done wide, so it cannot wrap).- If
vis positive, subtract each adaptation value from its coefficient; if negative, add it (16-bit wrap-around); if 0, change nothing. The sign is that of the inputv, not of the output. - Record
clip16(out)as the newest output, and work out its adaptation value fromout(below).
The adaptation value of the new output: 0 if out is 0, otherwise its sign (+1 for
positive) times 8, 16 or 32: 32 if |out| is more than 3 × the running average, 16 if more
than the average plus a third of it (integer), else 8 (version 3980 and later); the running average is
then moved by (|out| − average) ÷ 16, rounding toward zero. Then the values of the adaptation
vector 1, 2 and 8 places back are halved (arithmetic shift). Before 3980: the value is −4 or +4 from bit
28 of out (so −4 for non-negative, +4 for negative; 0 for 0) and the values 4 and 8 places back
are halved. The SDK writes the same rules in terms of the unrounded input; both give the same numbers.
The filters apply in the order of the table above (first listed first) to a whole frame’s values for one channel, before the second stage. The encoder runs them in the opposite order after prediction.
The first-order filter
Both the encoder and the decoder keep a one-value history and use a fixed first-order filter:
out = in + ((last × 31) >> 5) when decoding (with last the previous
output), and the inverse when encoding. The 3950 predictor uses it twice: on its own channel’s result
(the last step of decoding) and on the other channel’s values.
The 3950 second stage, step by step
For a channel, with a the value from the NN filters, b the other channel’s latest
output (0 for a mono channel), and the coefficient sets A (4 coefficients, starting 360, 317, −109, 98) and
B (5, starting 0), per sample:
- Take
last, the previous step’s result before the first-order filter. Form A0 = last, A1 = last − (the previous A0), A2 = the previous A1, A3 = the previous A2. - Pass
bthrough the first-order filter in its encoding direction (b − ((previous b × 31) >> 5)) to get B0; B1 = B0 − the previous B0; B2, B3, B4 are the previous B1, B2, B3. result = a + ((Σ Ak × MAk + ((Σ Bk × MBk) >> 1)) >> 10), all in 32-bit wrap-around arithmetic.- Move each coefficient:
MAk += adapt(Ak) × sandMBk += adapt(Bk) × s, wheresis −1 whenais positive, +1 when negative, 0 when 0, andadapt(x)is +1 for negativex, −1 for positive, 0 for 0. last = result; the sample isresult + ((previous output × 31) >> 5), which becomes the previous output.
In a stereo file the first channel decoded uses the second channel’s previous output as b
and the second uses the first’s current output. All of these start from 0 at every frame.
Files at 24 bits whose frames fail the CRC in this form are decoded again with the same steps in 64-bit arithmetic (see the interim form); the sums are no longer wrapped to 32 bits, and the signs of the adaptation are those of the full values.
Before version 3950
3930 and 3940
One channel at a time (no cross-channel input): the history is the channel’s last result and its
differences, d0 = last, d1 = d0 − h1, d2 = h1 − h2, d3 = h2 − h3, with
four coefficients starting 360, 317, −109, 98. result = a + ((Σ dk × ck) >> 9); each coefficient
moves by +1 if its d is negative and −1 if not, times the sign of a (flipped as above);
the output is result + ((previous output × 31) >> 5). In stereo files of these versions
the two decoded value streams are crossed: the first output channel is made from the second stream and the
second from the first.
3800 to 3920
Level 1000 uses one coefficient (375) on twice the last value minus the one before ((2·h1 − h2) × c >> 9), the first three samples
passing straight through. Other levels use two cascaded predictors of three and two taps
(initial coefficients 64, 115, 64 and 740, 0) with shifts of 10 or 11, and a long filter run over the whole frame
first: an order-16 filter (shift 9) at level 3000, an order-128 filter (shift 11) at 4000 and, from 3830, an
order-256 one (shift 12) preceded by an 8-tap filter. The first start samples (4; 16 at level 3000;
128 or 256 at 4000) skip the main predictor. The long filters are the same sign-adaptive FIR as the NN filter, without
the 16-bit clipping and with a fixed ±1 adaptation. The exact sequence is in the source of
predictor_decode_*_3800 in FFmpeg’s apedec.c; the explorer implements it and agrees with FFmpeg on
the 3.80, 3.88, 3.89 beta 1, 3.91 beta 1 and 3.92 beta 2 sample files at levels 2000 and 4000. Level 1000 (the one-coefficient
predictor) and level 3000 (the order-16 long filter) of these versions follow the same source.
More than two channels, 8 and 24 bits
The SDK’s decoder (version 13.46) handles files of up to 32 channels at 8, 16, 24 (and 32) bits. Each
channel is coded as a mono stream: one range coder shared by all channels, with the channels’ values taken one
block at a time in channel order, each with its own Rice state and its own predictor (the other-channel input
b is 0). After decoding, some channels are pairs of the same (mid, side) form as stereo (the first
of the pair is the mid, the second the side: first = mid − side ÷ 2; second = first + side):
| Channels | Pairs (first, second) | Coded as they are |
|---|---|---|
| 1, 2 | stereo: the two-channel form of the previous section | mono: the one channel |
| 3, 5 | none | every channel |
| 4 | (0, 1), (2, 3) | none |
| 6 | (0, 1), (4, 5) | 2 and 3 (centre and low frequency) |
| 7 | (0, 1), (4, 5) | 2, 3 and 6 |
| 8 | (0, 1), (4, 5), (6, 7) | 2 and 3 |
| 9 and more | (0, 1), (4, 5), (6, 7) | 2, 3 and 8 onward |
The pairs apply to 16-bit and 24-bit files. In an 8-bit file of more than two channels, and in a 32-bit file of more than two, every channel is coded as it is. Only version 3990 files can have more than two channels. This layout is from the SDK’s source; FFmpeg does not decode these files, so the explorer’s decoding of them was checked only by round trip against the research encoder, which follows the same reading. That is a check that the two agree, not an independent one.
8-bit files hold unsigned samples in the WAV (offset by 128); the decoder works with the signed value (−128 to 127). 24-bit samples are signed and 3 bytes in the WAV. Format flag 2048 says the original 8-bit values were signed; flag 512 big-endian; neither changes the coding.
Stereo reconstruction
The encoder converts left and right to a mid and a side signal. The decoder reverses that:
left = decoded[1] − (decoded[0] ÷ 2)
right = left + decoded[0]
Here decoded[0] is the difference and decoded[1] the mid signal.
The conversion is a whole-number one that loses nothing. For files of more than two channels
(added in version 4.86), or for 32-bit and floating-point audio, the layout could not be
confirmed.
Source: Monkey’s Audio SDK 13.46, Source/MACLib/NewPredictor.cpp (the filter orders and fractional bits of each level); the predictor constants (HISTORY_SIZE, YDELAYA and the rest, the initial coefficients 360, 317, −109, 98) are from FFmpeg’s apedec.c. All are compared by scripts/ape/spec-check.mjs. The adaptation rules and channel pairing tables are read from the SDK and FFmpeg sources and verified by decoding real files, not by script comparison.