Every frame holds the same number of samples, except the last. Frames are independent: the codec’s state starts afresh, so a decoder can begin at any of them. The compressed samples of each channel are interleaved in order, one value per channel for each sample position.
Frame layout
| Size | Field | Meaning |
|---|---|---|
| varies | Bit stream | The Rice-coded values, ending on a byte boundary |
| 4 | CRC-32 | Of the frame’s bytes before this field, little-endian |
Bits are read from the least significant end of each byte first.
Two-parameter Rice code
Each channel keeps two parameters, k0 and k1, each with a running
sum. Both start at k = 10, with the sum set to shift_16[k].
To read one value:
- Count the 1 bits up to a 0 bit: this is the unary count.
- A count of 0 uses
k0. Otherwise subtract 1 from the count and usek1. - The value is
(count << k) + the next k bits. - When
k1was used, updatesum1andk1, then addshift_1[k0]to the value. - Update
sum0andk0with the value (in every case). - Map to a signed value:
1 + ((value >> 1) ^ ((value & 1) − 1)).
Updating a parameter
sum += value − (sum >> 4)
if k > 0 and sum < shift_16[k]: k −= 1
else if sum > shift_16[k + 1]: k += 1
The shift tables
shift_1[n] is 1 << n for n up to 31, then
0x80000000 up to entry 39, then 0xFFFFFFFF at entry 40. shift_16[n] is
shift_1[n + 4].
The bit stream
The values of a frame are written one after another with no gaps: for each sample position
in turn, one value for each channel in order, as many positions as the frame has samples. Bits
are packed starting at the least significant bit of the first byte. A value is its unary part
(that many 1 bits and a 0 bit) and then k bits, the low bit of the number first. After the last value the
last byte is filled with 0 bits. Those padding bits, and any bytes between them and the CRC, are
zero in every file checked (the explorer’s Checks say when they are not). The decoder never needs the padding: the frame ends at
its last value, and the 4 bytes after that are the CRC.
The two parameters of a channel start at k0 = k1 = 10, with both sums at
shift_16[10] = 214 = 16,384, at the start of every frame. A value
that needs a long run of 1 bits is legal: the run is limited only by the frame’s data (the
decoders stop at the end of the frame’s bytes; FFmpeg also refuses a value of 231 or more).
The shift tables let k climb to 35; a read of more than 32 bits is refused.
Worked example: the first values of a real frame
Frame 0 of luckynight.tta (stereo, 16-bit) starts at byte 258 with
B6 50 03 2A 50 08 42 02 00 9A …:
bits of B6 (lowest first) 0 1 1 0 1 1 0 1 bit 0 is 0: unary count 0, so k0 = 10 is used
next 10 bits (lowest first) 1 1 0 1 1 0 1 | 0 0 0 = 1 + 2 + 8 + 16 + 64 = 91
sum0 = 16384 + 91 − (16384 ≫ 4) = 15451, below 16384 = shift_16[10]: k0 becomes 9
91 is odd: signed value (91 ≫ 1) + 1 = 46 (channel 0, sample 0)
next 11 bits: 53 → 27, k0 of channel 1 becomes 9 (channel 1, sample 0)
With every filter and predictor still at zero, 46 and 27 pass through unchanged, and the channels are rebuilt: the last channel is 27 + 46 ÷ 2 = 50 and channel 0 is 50 − 46 = 4. The first sample of the file is left 4, right 50 (FFmpeg decodes the same). Values 2 and 3 are −42 and −20; the filters have by then something to work with, and the left channel goes on 4, 4, −22, 20, 3, −11, 19, −5 … and the right 50, 7, 13, 51, 45, 45, −15, −14 … (checked against FFmpeg).
A value that uses k1 is 54 values in, again channel 0: its bits start with
1 0: unary count 1, so k1 = 10 is used on the count minus one (0), then 10 bits
giving 9. sum1 is updated with 9 (k1 becomes 9), then shift_1[k0] = 27 =
128 is added (k0 was 7): 137, odd, so the signed value is 69. Then sum0 is updated with 137.
The frame CRC
The 4 bytes after the padding are the CRC-32 of the frame’s bytes before them (the same
CRC-32 as the header’s), little-endian. Frame 0 above ends with
36 7D D7 05, the number 0x05D77D36. The seek table’s entry for a frame
is its size including these 4 bytes. The CRC covers the coded bytes, not the sound: with an encrypted
file it is right for every password.
The last frame
The last frame holds samples mod frame length samples (a whole frame if that is 0), and
is coded and padded the same way. A decoder that does not know the sample count from the header
(FFmpeg’s, which decodes a frame at a time) tells that a frame is the last when the values it has read leave exactly
4 bytes. The explorer works the sample count out from the header’s length.
Damaged frames
Because every frame starts afresh, damage stays inside the frame where it happens (the filter and Rice states are not carried on). A wrong CRC says the frame is damaged but not where; the values after the first bad bit of a frame are usually wrong (a changed bit changes a unary count and everything after it shifts), and nothing resynchronises within the frame. A frame that runs out of data before its samples are done is cut. The explorer decodes such a frame as far as it goes, labels it, and carries on with the next (ttaenc replaces a bad frame with silence; FFmpeg with its CRC check off decodes it as it is).
After the Rice code
Each signed value goes through the hybrid filter and predictor. When every channel of one sample position is done, the channels are rebuilt from their differences.