A value from the Rice code passes through an adaptive filter and then a fixed predictor. Once every channel at a sample position is finished, the channels are rebuilt from their differences. This page follows FFmpeg’s decoder.
Hybrid filter
Each channel has its own filter of 8 taps, with weights qm, a delay line
dl and step values dx. The shift depends on the sample width, and
the rounding value is 1 << (shift − 1).
| Bytes per sample | Bits | Shift |
|---|---|---|
| 1 | 8 | 10 |
| 2 | 16 | 9 |
| 3 | 24 | 10 |
A fourth setting, for 4-byte samples (floating-point files, see the variants page), has
shift 12 and a delay line of plain outputs: step 6 below is replaced by dl[4] = dl[5], dl[5] = dl[6],
dl[6] = dl[7], dl[7] = y, and its fixed predictor is the whole last value. That is what ttaenc 3.2 and 3.3 and ttalib 1.1 do
(filter table {10,1} {9,1} {10,1} {12,0}); FFmpeg’s table has the shift 12 but always uses differences and refuses 4-byte files.
ttaenc 3.4.1 and libtta have only the first three (10, 9, 10). This is a sign-sign LMS filter: the weights move by a
small fixed step whose sign comes from the delay line and the previous error.
For each value v (the signed number from the Rice code) of a channel, in this order:
- Adapt the weights by the sign of the channel’s previous
v(err, 0 at the start of a frame). Iferris negative,qm[i] −= dx[i]for all 8; if positive,qm[i] += dx[i]; if 0, nothing. sum= rounding value + Σdl[i] × qm[i]over the 8 taps (32-bit, wrapping).- Shift
dx[0..3]anddl[0..3]down by one place (x[i] = x[i+1]), then setdx[4..7]from the olddl[4..7]:dx[4] = ±1,dx[5] = ±2,dx[6] = ±2,dx[7] = ±4, plus whendlis 0 or positive and minus when negative. The decoders write it with shifts on the 32-bit value:dx[4] = (dl[4] ≫ 30) | 1,dx[5] = ((dl[5] ≫ 30) | 2) & ~1,dx[6] = ((dl[6] ≫ 30) | 2) & ~1,dx[7] = ((dl[7] ≫ 30) | 4) & ~3(FFmpeg, libtta), or((dl ≫ 30) | 1) ≪ nwith n = 0, 1, 1, 2 (ttaenc). Sincedl ≫ 30is 0 or 1 for a value of 0 or more and −1 or −2 for a negative one, both forms always give exactly the plus or minus values above, for every 32-bitdl(the form is what the code does; the plain sign gives the same except that it must treat 0 as positive). err = v.- The output
y = v + (sum ≫ shift)(arithmetic shift). - Update the delay line:
dl[4] = −dl[5],dl[5] = −dl[6],dl[6] = y − dl[7],dl[7] = y, thendl[5] += dl[6]anddl[4] += dl[5]. Sodl[7]is the last output,dl[6]its first difference,dl[5]the second difference anddl[4]the third.
All of the state (weights, steps, delay line, err) starts at 0 at the start of a frame
for every channel, apart from the weights in encrypted files.
The numbers are 32-bit wrapping integers throughout. The same function, with the signs
reversed (v = x − (sum ≫ shift)), is the encoder’s.
Fixed predictor
After the filter, each value adds a first-order prediction from the channel’s previous result
p, the number this step made last time (kept before the channels are rebuilt; 0 at the
start of a frame): d = y + prediction(p), and d is the next p.
The shift is arithmetic, so negative values round towards minus infinity. In effect the
prediction is p less a sixteenth (8-bit) or a thirty-second (16 and 24-bit) of it:
| Bits | Prediction |
|---|---|
| 8 | ((p << 4) − p) >> 4 |
| 16 and 24 | ((p << 5) − p) >> 5 |
Rebuilding the channels
TTA stores differences between neighbouring channels, so once every channel of a sample position has been decoded, it rebuilds them in reverse. The last channel gains half the value of the one before it, and each earlier channel is then the next channel minus its own value:
last channel += value of the channel before ÷ 2 (the division truncates towards 0)
for each earlier channel, from the last:
channel = next channel − channel
With stored values d[0] … d[n−1] after the filter and predictor, the samples are:
s[n−1] = d[n−1] + trunc(d[n−2] ÷ 2)
s[i] = s[i+1] − d[i] for i = n−2 down to 0
and the encoder stores the inverse: d[i] = s[i+1] − s[i] for every channel but the last, and
d[n−1] = s[n−1] − trunc(d[n−2] ÷ 2). For two channels that is a side channel
(the right minus the left) and a middle channel (the right minus half of the side): the stored first channel
is the side, the second the middle, and the decoded second channel is the right. A mono file has no step.
With more than two the same chain runs through every channel, so each stored channel is the difference to
the next one. The values are 32-bit wrapping numbers, as in the encoders.
Example, two samples (left 4, right 50): the encoder stores d[0] = 50 − 4 = 46 and d[1] = 50 − trunc(46 ÷ 2) = 27. These are the first two values of the worked example on the frames page.
Floating-point files (audio format 3) skip this step: each float is already two integer channels, which the variants page joins back into a 32-bit float.
Checked: files of 1 to 8 channels written by FFmpeg’s encoder, and of 9, 10, 12, 16 and 20 written by the research encoder, decode to the original samples, and the FFmpeg ones match FFmpeg’s decoder.
Output
| Bits | Output |
|---|---|
| 8 | 0x80 added, to make unsigned samples |
| 16 | Signed |
| 24 | Signed |
Passwords
A file with audio format 2 is encrypted with a password. FFmpeg computes a CRC-64 of the password with the polynomial 0x42F0E1EBA9EA3693, and uses its 8 bytes, sign-extended, as the filter’s initial weights, for every channel at the start of every frame. The variants page has the exact calculation, a worked example and what was checked against the real encrypted file.