A Shorten file is five byte-aligned bytes (the marker and the version) and then one continuous bit stream, read most significant bit first. In the file the stream is written as 32-bit words, high byte first, which comes to the same bit order as reading the bytes one after another. This page follows the shorten 3.6.1 source (the reference program, by Tony Robinson and later maintainers) and was checked against its own test files; FFmpeg’s decoder was used as a second decoder. The program’s manual page says that the earlier technical report (CUED/F-INFENG/TR.156) has errors in the bit layout and is superseded by the manual page, so these pages follow the program.
Marker and version
| Offset | Size | Field | Meaning |
|---|---|---|---|
| 0 | 4 | Marker | ajkg (bytes 61 6A 6B 67) |
| 4 | 1 | Version | The format version: 0 to 3 are decoded; the program looks for a version byte of 7 at most |
The program does not insist that the marker is at the start of the file. It reads on until it finds the four marker bytes followed by a byte of at most 7, and passes everything before them to its output (see the original file). That is how a file with an ID3v2 tag in front, or one made with the encoder’s alignment option, is read. A version byte above 3 is refused (“can’t decode version”).
| Version | What changes |
|---|---|
| 0 | The first form. The header holds only the file type and channel count, with the number codes shown below (no ulong). The block size is 256 until a BLOCKSIZE command changes it, there is no linear prediction (maximum order 0) and no mean estimator (0 blocks). A block’s energy field is one more than the Rice parameter of its residuals |
| 1 | Every header number is a ulong, and the header gains the block size, the maximum LPC order, the number of mean blocks and the skipped bytes. Residuals are coded with the energy as given |
| 2 | The mean estimator rounds, the mean of a block is kept at the full scale of the bit shift, and a linear-prediction sum starts at 32 (one whole unit of the 5-bit shift, V2LPCQOFFSET). The default is 4 mean blocks |
| 3 | The stream is exactly that of version 2. The program does not write it: its encoder clamps the version to 2 (shorten.c, around lines 762 to 765, with the limit in shorten.h line 28) |
| 4 to 7 | Not defined. A program that finds one of these after ajkg reads it as a version, and then refuses it |
The program writes version 2 unless asked for an older one with its -v option; a larger number is clamped to 2. Seek tables are written to version 2 files as well. The
prediction page lists what each version does to the arithmetic.
Number codes
Everything after the version byte is made of Rice-like numbers. A number is a run of 0 bits ended by a 1 bit (the length of the run is the high part), then a fixed count of low bits. The parameter k is the count of low bits.
| Name | Reads as |
|---|---|
uvar(k) | Count the 0 bits before the first 1 bit: q. Read k bits: b. The value is (q << k) + b. So with k = 2, the bits 0 0 1 1 0 are q = 2 and b = 2, the value 10 |
svar(k) | Read uvar(k + 1) as u. If u is odd the value is −1 − (u >> 1), otherwise u >> 1: 0, −1, 1, −2, 2, and so on. The lowest bit is the sign, and a negative number is stored as its complement so that there is no “negative zero”. A value costs about (|v| >> k) + k + 2 bits |
ulong | Read uvar(2) as n, the number of bits in the value, then uvar(n). The value needs at most n bits, so the run of zeros is always empty and the number is a 1 bit followed by n bits: 0 is 1 00 1 (n = 0, then a bare stop bit), 5 is 1 11 1 101 |
The man page’s examples for a 2-bit mantissa (the last bit is the sign): 0 is 1 00 0, 1 is 1 01 0, −1 is 1 00 1, 4 is 0 1 00 0
and 8 is 00 1 00 0. (That is the 3.6.1 man page; its entry for 2 repeats the code of 1, a slip,
and it says TR.156 “contains bugs in this format description”. Table 1 of TR.156 does give different codes, for
example 0001 for 0 and 0010001 for 13, which no decoder reads.)
What TR.156 adds about these numbers concerns the encoder, not the stream: it models the residual as a Laplacian
distribution and picks the low-bit count k so that half the samples fall within ±2k, which gives
k = log₂(log 2 × mean |x|) (its equation 21); a typical code is 7 bits long for 16 kHz speech. The report’s other
format-level statements are covered elsewhere: the four polynomial predictors (equations 3 to 6, on the
prediction page), 256 samples as the default block, a rectangular window longer than the
block by the prediction order, linear-prediction coefficients quantised with the same code at about 7 bits each, and the
lossy mode, which zeroes low bits (the -q, -r and -n options of the man page in its appendix).
The program’s numbers are 32 bits wide: the long number of the header may have n up to 32, and an energy above 30 cannot be right. A damaged stream can ask for more, and a decoder should stop there (the explorer reports “a long number of 33 bits”).
Header fields
Straight after the version byte, in this order. From version 1 each is a ulong; in version 0 the first two have the
codes shown and the rest do not exist.
| Field | Code in version 0 | Code from version 1 | Meaning |
|---|---|---|---|
| File type | uvar(4) | ulong | What the samples of the original were: the table below |
| Channels | uvar(0) | ulong | The number of channels, 1 or more. Blocks come one channel at a time, in channel order (no stereo decorrelation: the channels are coded independently) |
| Block size | absent: 256 | ulong | The number of samples in a block, until a BLOCKSIZE command changes it. The encoder’s default is 256 |
| Maximum LPC order | absent: 0 | ulong | The highest order of linear prediction any block uses. 0 means the four fixed polynomial predictors only. The samples remembered from block to block are the larger of 3 and this |
| Mean blocks | absent: 0 | ulong | The number of past blocks whose means are averaged; 0 turns the mean estimator off. The encoder’s default is 4 from version 2 and 0 before |
| Skipped bytes | absent: 0 | ulong | How many bytes follow that are stored as they are, each as a uvar(7). They are written to the output before anything else that the stream stores |
A real header
The mono stream tests/mono/test.shn of the shorten 3.6.1 sources starts
61 6A 6B 67 02 FB 72 E0 13 F2 4A B2 …. After ajkg and the version 2 the stream begins at bit 40, and its first 82 bits are:
1 11 1 101 file type: n = 3, value 101 = 5 (S16LH) bits 40–46
1 01 1 1 channels: n = 1, value 1 bits 47–51
001 01 1 100000000 block size: n = 9, value 1 0000 0000 = 256 bits 52–66
1 00 1 maximum LPC order: n = 0, value 0 bits 67–70
1 11 1 100 mean blocks: n = 3, value 100 = 4 bits 71–77
1 00 1 skipped bytes: n = 0, value 0 bits 78–81
The first command starts at bit 82: 001 01, the function code uvar(2) with q = 2 and b = 1, which is 9, VERBATIM;
then its length 01 01100, uvar(5) = (1 << 5) + 12 = 44, the bytes of a WAV header. See
the commands page.
File types
The type says how the samples of the original file were stored and how the decoder must write them out. A stream never holds types 11 to 13 or 128 and 129, which the program uses internally for “a WAV file”, “an AIFF file”, “end” and the generic µ-law and A-law selections; any other value is refused by the decoder.
| Value | Name | Samples | Value held in the stream |
|---|---|---|---|
| 0 | AU1 | 8-bit µ-law bytes, lossless | A number from −128 to 127 that the µ-law byte maps to, in order of its linear value, and squeezed by the block’s bit shift (see the tables) |
| 1 | S8 | Signed 8-bit | The sample |
| 2 | U8 | Unsigned 8-bit | The sample, 0 to 255 (the mean starts at 128) |
| 3 | S16HL | Signed 16-bit, high byte first (big-endian) | The sample |
| 4 | U16HL | Unsigned 16-bit, high byte first | The sample, 0 to 65,535 (the mean starts at 32,768) |
| 5 | S16LH | Signed 16-bit, low byte first (little-endian, the WAV form) | The sample |
| 6 | U16LH | Unsigned 16-bit, low byte first | The sample, 0 to 65,535 (the mean starts at 32,768) |
| 7 | ULAW | 8-bit µ-law bytes, lossy | The linear value of the byte divided by 8; the decoder multiplies by 8 and converts back to a µ-law byte, which is usually the byte it was |
| 8 | AU2 | 8-bit µ-law bytes, lossless, second mapping | As type 0, except that the byte 0x7F, the negative zero, is the number −1 |
| 9 | AU3 | 8-bit A-law bytes, lossless | For a byte below 128, 127 − (byte XOR 0xD5); otherwise (byte XOR 0x55) − 128: a number from −128 to 127 |
| 10 | ALAW | 8-bit A-law bytes, lossy | The linear value of the byte divided by 8, as for type 7 |
The file type also fixes the start value of the means (128 and 32,768 for the unsigned types, 0 otherwise) and the width of the samples the decoder writes. There is no way to give channels different types.
After the header
The header is followed at once by the first command. When the file came from a WAV or AIFF file the first commands are VERBATIM commands holding that file’s header (see the original file); FFmpeg insists on this and takes the sample rate from it.
The end of the stream
The last command is QUIT. After it the writer sends a stop bit, 1, and zeros up to the end of the 32-bit word (the program
writes uvar(0, 31), a 1 bit and 31 zeros, and drops the incomplete word). When QUIT ends exactly at the end of a word, the
flush is therefore a whole extra word, 80 00 00 00. A stream is always a whole number of 32-bit words after the version byte:
the program reads words, so a file cut inside a word loses that word’s bytes. In the test stream test.shn QUIT (0100) ends at bit 94,243 and
the stream closes at bit 94,248, the last byte being 90 = 1001 0000: the last three bits of QUIT, the stop bit, and four zeros.
Anything after the last word is not read: a seek table, an APEv2 or ID3v1 tag, or other bytes. See seek tables and the tags on the overview page.
Damaged and cut files
- A stream that ends inside a number or before QUIT is “premature EOF on compressed stream” to the program, which stops with an error; what it has written until then is output. FFmpeg decodes the blocks it can.
- A command number above 9, a file type above 10, a prediction order above the wrap length, an energy above 30, a bit shift above 32 or a long number of more than 32 bits cannot appear in a good stream: FFmpeg stops there, and the explorer shows the stream up to that point and labels the rest.
- A BLOCKSIZE command to a size larger than the header’s overruns the program’s own buffer, which was sized from the header. The encoder
only ever makes the block size smaller (for the last block). In a version 0 stream the starting size is 256, so a block size larger than 256
(the encoder’s
-boption writes a BLOCKSIZE command at the start) overruns it too.
Links
Specification and source
- shorten 3.6.1 – the reference program: source, manual page and test files (free for non-commercial use)
- FFmpeg shorten.c – the second decoder (LGPL)
Source: shorten 3.6.1, include/shorten.h (MAGIC, the version limits, the defaults, TYPE_*, TYPESIZE, ULONGSIZE, NSKIPSIZE, XBYTESIZE) and src/shorten.c (header reading and writing). TR.156 does not define the header: all 16 pages of the report were viewed as images (equations 1 to 22, Figures 1 to 3, Tables 1 and 2, the man page of version 1.22 in the appendix), and none has a bit-stream layout, function numbers or a seek table. The man page of shorten 3.6.1 (man/shorten.1) supplies the code examples above. Compared by scripts/shn/spec-check.mjs.