Each sample is its residual plus a prediction formed from earlier samples of the same channel, and the stream
holds only the residuals. The encoder tries the four fixed predictors (or, when it was told to use linear prediction with its
-p option, a filter it computes for each block) and writes the best. The arithmetic below is exact: all
sums are signed 32-bit numbers that wrap on overflow, right shifts of negative numbers keep the sign, and a division by a count
truncates towards zero, as in the C program. The arithmetic follows the program’s source; the sample files section lists
what it was checked against.
Fixed polynomial predictors
Commands DIFF0 to DIFF3 predict from the most recent samples s[−1], s[−2], s[−3] (the ones just before the sample, across the previous block’s end where needed):
| Command | Order | Prediction |
|---|---|---|
| DIFF0 | 0 | The block’s mean |
| DIFF1 | 1 | s[−1] |
| DIFF2 | 2 | 2 s[−1] − s[−2] |
| DIFF3 | 3 | 3 (s[−1] − s[−2]) + s[−3] |
The sample is the residual plus the prediction. Before anything has been decoded the remembered samples are 0 (not the mean).
Linear prediction (QLPC)
QLPC sends its coefficients c0 … corder−1 in the command, as integers with 5 fractional bits. With m the block’s mean (the mean the DIFF0 command would use), a sample is:
sum = offset + Σ c[j] × (s[−1−j] − m) for j = 0 … order−1
s = residual + (sum >> 5) + m
Here offset is 0 up to version 1 and 32 from version 2 (V2LPCQOFFSET = 1 << LPCQUANT, shorten.c line 49; with 5 fractional bits that is one whole unit, not the 16 that would round, so every prediction is one higher than the plain shift gives). The
mean is taken off the remembered samples only for the filter: it is added back to the block afterwards. A block of order 0 has
no filter: its prediction is the mean, plus 1 from version 2 because 32 >> 5 is 1.
Two quirks of the program matter to an exact decoder:
- The mean is subtracted from the remembered order samples in the history itself and not put back. For a block of at least as many samples as the wrap length that does not matter, because the history is overwritten by the block’s last samples (which have the mean added back). For a block shorter than the wrap length, the history that carries over still holds the samples with the mean taken off. FFmpeg does the same. The explorer’s decoder and the real program agree on streams with last blocks of 1, 2, 3 and 5 samples (linear prediction of order 8 and 32, signed and unsigned input), which the real program also round-trips exactly.
- FFmpeg predicts a QLPC block of order 0 as
(mean >> 5) + mean, not as the program does, so it decodes such a block differently. The explorer follows the program, and the real encoder does write blocks of order 0 (it searches from order 0 upwards): the explorer decodes those streams exactly.
Running means
Each channel keeps the means of its last n blocks (the header’s mean blocks), starting with the type’s middle value (0, or 128 or 32,768 for unsigned samples). The estimate used for a block is the average of them. DIFF0 uses it as its prediction, QLPC takes it off the history and puts it back, DIFF1 to DIFF3 ignore it. A stream with 0 mean blocks never updates it: the estimate stays the starting value.
| Step | Below version 2 | Version 2 and later |
|---|---|---|
| Estimate for a block | sum ÷ n | (sum + n ÷ 2) ÷ n, then shifted right by the bit shift in force (an arithmetic shift, by 0 or more places; with 32 the result is 0 or −1) |
| Mean stored after a block | sum ÷ block size (the block’s samples, before any bit-shift multiplication) | (sum + block size ÷ 2) ÷ block size, then shifted left by the bit shift |
Divisions truncate towards zero (so a negative sum rounds up). The block’s mean is that of the decoded samples before they are multiplied by the bit shift, which is why the stored version is shifted up again to the output scale and the estimate shifted down to the block’s scale. The oldest of the n stored means is dropped when a new one is added.
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. They are the block’s samples before the bit shift is applied, which is why a change of bit shift between blocks leaves the first predictions of the new block at the old scale, and the encoder takes that into account.
The bit shift multiplies a decoded block by 2shift (a shift of 32 is undefined in the C program, where on common processors it changes nothing; FFmpeg and the explorer give zeros, which agrees whenever the block is all zeros, the only case the real encoder writes it for) after the wrap samples and the block’s mean have been saved. The encoder finds
the shift as the number of low bits that are zero in every sample of the block, or, for a lossy stream, a larger one. When a version 2 or later block is
entirely zero the encoder sends ZERO, and keeps the last shift. Version 0 and 1 streams send a shift of 32 and then a block of zero residuals instead.
From numbers to the original’s samples
After the shift, a block is written in the type’s form: interleaved, a frame at a time, with each value cut to the type’s range (values above the maximum are held at it; the lower end wraps as a C cast does). The table gives the bytes of a sample.
| Type | Bytes written for a number v |
|---|---|
| S8 | v as one signed byte |
| U8 | v as one byte |
| S16HL, U16HL | two bytes, high byte first |
| S16LH, U16LH | two bytes, low byte first |
| ULAW | the µ-law byte of the linear value v × 8 (held to 32,767) |
| ALAW | the A-law byte of v × 8 (held to 32,767) |
| AU3 | (127 − v) XOR 0xD5 for a negative v, (v + 128) XOR 0x55 otherwise |
| AU1, AU2 | through the tables below |
The µ-law tables (AU1 and AU2)
A lossless µ-law stream has 8-bit codes that mean linear values 8 apart at the quietest end and far apart at the loud end, so a block that uses only the
codes that a shifted-down linear signal can reach is coded with a shift. Both directions use tables of 13 rows (shifts 0 to 12), built from the µ-law encoder
Slinear2ulaw (G.711 with a bias of 0x84 and a clip at 32,635) like this:
- For each shift s from 0 to 12, for each linear value from −32,768 in steps of
2s+3, the µ-law byte of that value is given the largest shift s it was reached with (maxshift[byte]; a byte nothing reaches has 0). - For a shift s, the numbering of the bytes (
inward[s][byte]): bytes 255 down to 128 whosemaxshiftis at least s get 0, 1, 2 … in that order, then the rest of 255 down to 128 continue the count; bytes 126 down to 0 withmaxshiftat least s get −1, −2, … then byte 127 (the negative zero) takes the next number, then the rest of 126 down to 0 continue downwards. outward[s]is the inverse: the byte for the number i isoutward[s][i + 128].
The encoder takes the block’s shift as the smallest maxshift of its bytes and sends inward[shift][byte] as the sample (for AU2: bytes above 0x7F as that, 0x7F as −1 and the others as inward[shift][byte] − 1).
The decoder turns a decoded number back with outward[shift] (AU2: a number of −1 is byte 0x7F, a negative number v is outward[shift][v + 129]). The shift is not applied as a multiplication for these two types.
The lossy types ULAW and ALAW instead turn each byte into its linear value divided by 8 on the way in (so the coded values are 13-bit), and into a byte by the standard encoders on the way out; the result is the original byte in most cases and a neighbouring one in the rest.
What each version does
| 0 | 1 | 2 (3 is decoded alike) | |
|---|---|---|---|
| Header numbers | fixed uvar for type and channels only | ulong | ulong |
| Block size, order, mean blocks, skipped bytes in the header | no (256, 0, 0, 0) | yes | yes |
| Energy field | Rice parameter + 1 | Rice parameter | Rice parameter |
| BLOCKSIZE argument | uvar(log₂ of the size in force) | ulong | ulong |
| Mean estimate | no mean blocks: the starting value | truncated average | rounded, scaled by the bit shift |
| Stored mean | unused | truncated, unscaled | rounded, scaled up by the bit shift |
| QLPC offset | (no QLPC) | 0 | 32 |
| All-zero block | a shift of 32 and zero residuals | as 0 | ZERO, the shift kept |
| Default mean blocks | 0 | 0 | 4 |
Links
Specification and source
- shorten 3.6.1 – the reference program (
shorten.c,fixio.c,utils/mkbshift.c) - FFmpeg shorten.c – the second decoder
Source: the fixed polynomial predictors are the polynomial predictors of TR.156 and the FN_DIFF0 to FN_DIFF3 cases of shorten 3.6.1’s src/shorten.c; the mean estimator, the bit-shift rules and the sample types are from shorten.c and include/shorten.h (the macro ROUNDEDSHIFTDOWN and the TYPE_* codes). The decoder lines of the predictors are compared by scripts/shn/spec-check.mjs.