Each sample is its residual plus a prediction formed from earlier samples of the same
channel. The encoder uses the polynomial predictors unless told to use linear prediction with
its -p option.
Fixed polynomial predictors
Commands DIFF0 to DIFF3 use these coefficients, applied to the most recent samples first:
| Command | Order | Coefficients | Prediction |
|---|---|---|---|
| DIFF0 | 0 | None | The running mean |
| DIFF1 | 1 | 1 | s[−1] |
| DIFF2 | 2 | 2, −1 | 2 s[−1] − s[−2] |
| DIFF3 | 3 | 3, −3, 1 | 3 s[−1] − 3 s[−2] + s[−3] |
Linear prediction
QLPC sends its coefficients in the command, with 5 fractional bits. So the prediction is:
sum = offset + Σ coefficient[i] × sample[−1−i]
sample = residual + (sum >> 5)
Here offset is 0 below version 2 and 32 from version 2 on. A QLPC block of order
0 has no shift and uses the running mean as its prediction.
Running means
Each channel keeps the means of its last few blocks, as set by the header’s mean field. In a QLPC block, the average of these is subtracted from the earlier samples before prediction and added back to the block after it. DIFF0, and a QLPC block of order 0, use the average as the prediction. DIFF1 to DIFF3 do not use it.
| Step | Below version 2 | Version 2 and later |
|---|---|---|
| Combine the stored means | sum ÷ n | (sum + n ÷ 2) ÷ n, then shifted right by the bit shift |
| Store the new block’s mean | sum ÷ block size | (sum + block size ÷ 2) ÷ block size, then shifted left by the bit shift |
With a mean field of 0 the stored value is used directly. Means begin at 0, or at 128 for U8 samples in FFmpeg, which decodes only U8 and the two signed 16-bit types. Version 0 has no mean field, and FFmpeg uses no running mean there.
Wrap and bit shift
Each channel carries its last samples from one block to the next, so that prediction can reach
back past the block’s start. The number kept, the wrap length, is the larger of 3 and the
header’s maximum LPC order. When a bit shift is in force, the rebuilt samples are multiplied
by 1 << shift after the wrap samples have been saved.