Each channel’s values are the outputs that the filters consume. The way they are coded has changed several times, so the decoder chooses by file version. This page follows FFmpeg’s decoder; Monkey’s Audio has no written specification of it.
Coders by version
| Version | Coder | Tables |
|---|---|---|
| Below 3860 | Adaptive Rice coding, read through a bit reader | None |
| 3860 to 3899 | Adaptive Rice coding with overflow handling; above 3880, a loop that raises k | None |
| 3900 to 3929 | Range coder; stereo channels coded one after the other | counts_3970 |
| 3930 to 3989 | Range coder; stereo Y and X values interleaved. Mono is as for 3900 | counts_3970 |
| 3990 and later | Range coder with an adaptive pivot | counts_3980 |
The history records: 3.86 rewrote the bitstream, 3.89 made it more robust, 3.90 introduced
the new format, 3.96 corrected it for rare failures and 3.99 improved the entropy coder. Both
Rice states start at k = 10 at the start of every frame.
The range coder
From version 3900 a frame’s data goes through a byte-oriented range coder.
| Constant | Value |
|---|---|
| CODE_BITS | 32 |
| TOP_VALUE | 1 << 31 |
| SHIFT_BITS | 23 |
| EXTRA_BITS | 7 |
| BOTTOM_VALUE | TOP_VALUE >> 8 |
| MODEL_ELEMENTS | 64; the symbol value 63 is an escape |
- Start: read one byte, then set
lowto that byte shifted right by 1, andrangeto 128. - Normalise: while
rangeis at most 2²³, read another byte and scale. - Decode: for a total frequency
tot,help = range ÷ totand the cumulative frequency islow ÷ help. A power-of-two total uses a shift instead. - Update:
low −= help × ltandrange = help × sy, with the symbol’s cumulative start and width.
A symbol is found by locating the cumulative frequency in the tables below. In the 3900 stereo path the decoder backs up one byte between channels and restarts the range coder, to match the reference implementation.
Symbol tables
counts holds each symbol’s cumulative start, and counts_diff its
width. A 16-bit cumulative frequency above 65492 is outside the tables: it is its own symbol,
frequency − 65535 + 63, with a width of 1. Symbol 63 is the escape.
| Symbol | counts_3970 | counts_diff_3970 | counts_3980 | counts_diff_3980 |
|---|---|---|---|---|
| 0 | 0 | 14824 | 0 | 19578 |
| 1 | 14824 | 13400 | 19578 | 16582 |
| 2 | 28224 | 11124 | 36160 | 12257 |
| 3 | 39348 | 8507 | 48417 | 7906 |
| 4 | 47855 | 6139 | 56323 | 4576 |
| 5 | 53994 | 4177 | 60899 | 2366 |
| 6 | 58171 | 2755 | 63265 | 1170 |
| 7 | 60926 | 1756 | 64435 | 536 |
| 8 | 62682 | 1104 | 64971 | 261 |
| 9 | 63786 | 677 | 65232 | 119 |
| 10 | 64463 | 415 | 65351 | 65 |
| 11 | 64878 | 248 | 65416 | 31 |
| 12 | 65126 | 150 | 65447 | 19 |
| 13 | 65276 | 89 | 65466 | 10 |
| 14 | 65365 | 54 | 65476 | 6 |
| 15 | 65419 | 31 | 65482 | 3 |
| 16 | 65450 | 19 | 65485 | 3 |
| 17 | 65469 | 11 | 65488 | 2 |
| 18 | 65480 | 7 | 65490 | 1 |
| 19 | 65487 | 4 | 65491 | 1 |
| 20 | 65491 | 2 | 65492 | 1 |
| 21 | 65493 | 65493 |
Decoding a value, 3900 to 3989
Read a symbol using counts_3970; this is the overflow. If it is the escape, read 5
more bits as tmpk and set the overflow to 0; otherwise
tmpk is k − 1, or 0 if that is negative. Then read
tmpk bits as x, in two parts above 16 bits, and add
overflow << tmpk. The update of k and the sign mapping are as
for 3990, below.
Decoding a value, 3990
pivotisksum >> 5, at least 1.- Read the overflow with
counts_3980. On the escape, it is rebuilt from two 16-bit reads. - Read
basewith a total ofpivot. A pivot of 65536 or more is reduced to 16 bits and read in two steps. x = base + overflow × pivot.- Update:
limis1 << (k + 4), or 0 whenkis 0;ksum += (x + 1) ÷ 2 − ((ksum + 16) >> 5). Ifksumis belowlim, lowerk; otherwise, if it is at least1 << (k + 5)andkis under 24, raisek. - The sign:
((x >> 1) ^ ((x & 1) − 1)) + 1. An even value gives a negative result and an odd value a positive one.