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 Rice coders, before version 3900
The frame is read bit by bit, most significant bit first, from the swapped stream (see
words). A value is a unary part (zero bits up to a 1 bit)
and then k low bits; the result goes through the same sign mapping as the later coders. In
these versions each channel’s values for the whole frame come one channel after the other (first channel
“Y”, then “X”), not interleaved.
Versions 3800 to 3859 (3.80 to 3.85)
- The first 5 values use
k= 10; their sum startsksum. - Then
kis the number of bits inksum ÷ 10(0 for 0); each of the values up to the 64th is read with the currentk, added toksum, andkis recomputed as the bit length ofksum ÷ (2 × values read). - From the 65th value on,
kstarts as the bit length ofksum ÷ 128, andksumis a window of the last 64 values (the new value is added, the one 64 back taken off).kgoes down whileksumis under2^(k+6)(0 for k = 0) and up while it is at least2^(k+7), with the limits moved with it. - A
kof 24 or more ends the frame as damaged.
Versions 3860 to 3899
kstarts at 10 andksumat 16 × 210.- Read the unary part (
overflow). Above version 3880, while it is 16 or more, take 16 off and add 4 tok. - If
kis 0 the value isoverflow; otherwise(overflow << k) + k bits(kabove 24 is an error). ksum += x − ((ksum + 8) >> 4);kgoes down whenksumis below2^(k+4)(0 for k = 0) and up when it is at least2^(k+5)andkis under 24.
These were checked on the 3.80 and 3.88 sample files (frame CRCs right and every sample equal to FFmpeg’s). 3.81 to 3.87 follow the same two paths by the version thresholds.
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.
Source: Monkey’s Audio SDK 13.46, Source/MACLib/UnBitArrayBase.h (the range-coder constants and the tables RANGE_TOTAL_1, RANGE_WIDTH_1, RANGE_TOTAL_2, RANGE_WIDTH_2, which the page names counts_3970 and counts_3980 after FFmpeg’s tables) and BitArray.cpp (SHIFT_BITS). Every constant and table row above is compared with the SDK by scripts/ape/spec-check.mjs; the version limits of the coders come from the SDK’s decoder classes and the version history and are not compared by script.