After the header, a Shorten stream is a list of
commands, each starting with a function code, a uvar(2). Six commands carry a block of samples for one channel:
the four fixed predictors DIFF0 to DIFF3, QLPC and ZERO. The blocks come one channel at a time, going round the channels in order
(channel 0, channel 1, then channel 0 again), so a block of every channel makes one round and one stretch of the sound. The other four
commands change a setting, carry bytes of the original file, or end the stream.
The commands
| Value | Name | Arguments | Meaning |
|---|---|---|---|
| 0 | DIFF0 | Energy, residuals | A block predicted by the running mean (polynomial order 0) |
| 1 | DIFF1 | Energy, residuals | A block predicted by the previous sample (order 1) |
| 2 | DIFF2 | Energy, residuals | A block predicted by a straight line through the last two samples (order 2) |
| 3 | DIFF3 | Energy, residuals | A block predicted by a parabola through the last three samples (order 3) |
| 4 | QUIT | None | The end of the stream |
| 5 | BLOCKSIZE | New size | Change the number of samples in the blocks that follow |
| 6 | BITSHIFT | Shift | Change how far the samples are shifted left |
| 7 | QLPC | Energy, order, coefficients, residuals | A block predicted by a linear filter whose coefficients are in the command |
| 8 | ZERO | None | A block of zero samples |
| 9 | VERBATIM | Length, bytes | Bytes copied to the output as they are: the header and trailer of the original file |
Any higher value cannot be decoded: the program stops with “sanity check fails trying to decode function”. The prediction page describes how the samples are rebuilt from a block’s residuals.
Audio commands
DIFF0 to DIFF3, and QLPC, share this layout. ZERO has no arguments at all (its function code is the whole command).
| Field | Code | Meaning |
|---|---|---|
| Energy | uvar(3) | The Rice parameter k of the residuals. In version 0 the field is one more than k, so the decoder subtracts 1 (the energy 0 then gives svar(−1): no low bits at all). FFmpeg rejects a parameter above 30 |
| Order | uvar(2) | QLPC only: the number of coefficients, up to the wrap length (the larger of 3 and the header’s maximum order). It may be 0 |
| Coefficients | svar(5) each | QLPC only: the filter’s coefficients, with 5 fractional bits, the one for the previous sample first |
| Residuals | svar(k) each | One for each sample in the block: the difference between the sample and its prediction |
A block has as many residuals as the block size in force. ZERO makes the samples 0 without reading anything. A zero block still takes part in the mean estimator (a zero mean is stored) and counts as a block for the seek tables.
Setting commands
| Command | Field | Code | Meaning |
|---|---|---|---|
| BLOCKSIZE | New size | From version 1 a ulong. In version 0 a uvar whose k is the integer part of log2 of the block size now in force (computed with floating-point logarithms by the program) | The encoder sends one for the last, shorter block of a file whose length is not a multiple of the block size. Any size is allowed by the format; one above the header’s block size overruns the program’s buffers. FFmpeg rejects an increase, and sizes of 0 or above 65,535 |
| BITSHIFT | Shift | uvar(2) | Decoded samples are multiplied by 2 to this power (for the µ-law types AU1 and AU2 it chooses a table instead). FFmpeg rejects a shift above 32 and makes a shift of 32 give zeros (in the C program a shift of 32 is undefined; the real encoder sends it only for all-zero blocks of versions 0 and 1, where every reading gives zeros). It stays in force until the next BITSHIFT, across channels |
| QUIT | The end of the stream; see how it is closed |
Verbatim command
| Field | Code | Meaning |
|---|---|---|
| Length | uvar(5) | The number of bytes that follow. The encoder splits a header into runs of at most 256, so the length takes up to 8 zero bits ahead of its stop bit |
| Bytes | uvar(8) each | One value per byte of the original file. A byte of 0 to 255 has no zero bits ahead of the stop bit, so each is exactly 9 bits: a 1 bit and the byte |
The bytes are sent to the output where the command stands: a run before the first block is the original’s header, and a run after the last block its trailer (see the original file).
A real stream, command by command
The mono test stream tests/mono/test.shn (version 2, 16,298 samples of 16-bit sound, block size 256) holds 67 commands.
The explorer’s Commands table lists them with their bit positions; the first five are:
| # | Bits | Command | Fields |
|---|---|---|---|
| 0 | 82–489 (408 bits) | VERBATIM | function 001 01; length 01 01100 = 44; 44 bytes of 9 bits (the WAV header, RIFF…) |
| 1 | 490–1261 (772 bits) | DIFF1 | function 1 01; energy 1 000 = 0; 256 residuals of svar(0), 2 bits for a 0 or −1 and 3 for a 1 or −2 (765 bits in all) |
| 2 | 1262–2015 | DIFF1 | energy 0 |
| 3 | 2016–2789 | DIFF1 | energy 0 |
| 4 | 2790–3729 | DIFF2 | energy 1 |
The stream has 8 DIFF0, 17 DIFF1, 23 DIFF2, 16 DIFF3, one VERBATIM, one BLOCKSIZE and one QUIT. After 63 blocks of 256 samples (16,128), the BLOCKSIZE command at bit 93,665 sets the size 170 for the last block (16,298 = 63 × 256 + 170), then comes the last DIFF1 block and QUIT. Because the stream has one channel, its blocks are all channel 0 and every hundredth one gets a seek entry: block 0 only, the one at bit 490. Decoded, the stream gives back the 32,640-byte WAV file next to it in the sources, byte for byte: its 44-byte header, the samples and nothing after them (no trailer).
Links
Specification and source
- shorten 3.6.1 – the reference program (
shorten.cholds the command loop,vario.cthe number codes)
Source: shorten 3.6.1 (Tony Robinson, SoftSound), include/shorten.h (FN_* values, FNSIZE, ENERGYSIZE, LPCQSIZE, LPCQUANT, BITSHIFTSIZE, VERBATIM_*, NWRAP, MAX_LPC_ORDER) and src/shorten.c. Robinson’s technical report CUED/F-INFENG/TR.156 (December 1994) describes the method but gives no bit-stream tables, so the numbers here come from the source; they are compared by scripts/shn/spec-check.mjs. The worked example at the end is from a file made for this site.