Overview
All codes are read most significant bit first. In SV7 the file’s 32-bit words are first byte-swapped (see the SV7 header).
| Code | Used for | Version |
|---|---|---|
| Varint | Packet sizes, sample counts, chapter positions, the first two seek positions | 8 |
| SV7 Huffman | Resolution differences, scalefactor selectors, scalefactor differences, sample values | 7 |
| Canonical Huffman | Band counts, resolutions, selectors, scalefactor differences, sample values | 8 |
| Logarithmic | The number of used bands in a key frame, the number of mid/side bands | 8 |
| Enumerative | Which bands use mid/side; which values of a resolution 1 band are non-zero | 8 |
| Golomb | The seek table | 8 |
Varint
Groups of seven bits, most significant group first, each in a byte whose top bit is set when another byte follows. At most 9 bytes are read.
SV7 Huffman tables
Each SV7 table is a list of entries (code, length, value), tried from the longest code value downwards: the decoder looks at the next 16 bits and takes the first entry whose left-aligned code is not above them. The three small tables are given in full. Codes are written in binary.
Resolution differences (hdr)
The difference from the resolution of the band below. Value 4 is an escape: a plain 4-bit resolution follows.
| Code | 1 | 00 | 0100 | 01010 | 010110 | 011 | 01011100 | 0101111 | 010111011 | 010111010 |
|---|---|---|---|---|---|---|---|---|---|---|
| Value | 0 | −1 | −2 | −3 | 2 | 1 | −5 | −4 | 3 | 4 (escape) |
Scalefactor selectors (scfi)
| Code | 1 | 011 | 010 | 00 |
|---|---|---|---|---|
| Value | 1 | 2 | 0 | 3 |
Scalefactor differences (dscf)
Value 8 is an escape: a plain 6-bit scalefactor follows.
| Code | Value | Code | Value |
|---|---|---|---|
| 000 | −2 | 1001 | 0 |
| 00100 | −6 | 101 | 1 |
| 00101 | 6 | 1100 | 8 (escape) |
| 0011 | 4 | 1101 | −3 |
| 010 | 2 | 1110 | 3 |
| 011 | −1 | 11110 | −4 |
| 100000 | −7 | 11111 | 5 |
| 100001 | 7 | 10001 | −5 |
Sample tables
There are 7 groups of two tables for the sample values, selected by the resolution (1 to 7) and by the table-select bit of the band: 27 entries for resolution 1 (three values per code, each digit of the symbol in base 3 minus 1), 25 for resolution 2 (two values per code, base 5 minus 2) and one entry per value for 3 to 7 (7, 9, 15, 31 and 63 entries). Every code is listed on the Tables page.
SV8 canonical Huffman tables
An SV8 table is a list of code lengths and the first code of each length. The decoder compares the next 16 bits with the left-aligned first code of each length, longest codes first, takes the first length for which the bits are not smaller, and finds the symbol by subtracting the code from the entry’s offset and looking it up in a list of symbols, which is sorted by code. The tables are:
- bands, 12 lengths (1 to 13 bits), 33 symbols: the change in the number of used bands.
- res, two tables: the top resolution, and the difference between resolutions (one table for a band whose upper neighbour has a resolution above 2, one for the rest).
- scfi, two tables: the selector of one channel (4 symbols) or of both (16 symbols).
- dscf, two tables: the scalefactor difference of the first scalefactor of a frame, and of the second and third.
- q1, 10 lengths, 19 symbols: the number of non-zero values in half of a resolution 1 band.
- q, six groups of two tables, one per resolution group: resolution 2 (125 symbols, three values each, as base 5 digits minus 2), resolution 3 (49 symbols) and 4 (91 symbols) as pairs of four-bit values, and resolutions 5, 6, 7 and 8 (15, 31, 63 and 127 symbols, one value each). For 2 and 5 to 8 the two tables of the group are chosen by the running measure below; for 3 and 4 the two tables of the group are the table of resolution 3 and the table of resolution 4, with no choice between them.
- q9up, 6 lengths, 256 symbols: the high bits of values of resolution 9 and above.
The running measure used to pick between the two tables of a resolution is: start at twice the threshold of the resolution (3 for resolution 2, 1 for 5, 3 for 6, 4 for 7 and 8 for 8), use the second table when the measure is above the threshold, and after each code replace the measure by half of it (rounded down) plus the size of the values just read (the sum of the absolute values for resolution 2, and the absolute value for 5 to 8). Every code of these tables is listed on the Tables page, and the logarithmic and enumerative code tables (log2, Cnk) in its codes section.
Logarithmic code
A value from 0 to max (inclusive) coded as a truncated binary number. Let w be the number of bits of max (at least 1) and lost the number of values that need one bit fewer (2w − (max + 1)). Read w − 1 bits as v; if v is at least lost, read one more bit and replace v by 2v + that bit − lost. With max 0 nothing is read.
Enumerative code
Gives a choice of k set bits out of n (n up to 32). The index of the choice among all C(n, k) is written as a truncated binary number in the same way as a logarithmic code, with the width and lost values taken from tables of the binomial coefficients. The index is then turned into the bit pattern by scanning positions from n − 1 down to 0: where the index is at least the binomial coefficient C(position, k), the bit is set, that coefficient is subtracted and k goes down by one.
Golomb code
Used in the seek table: the quotient in unary (a number of 0 bits ended by a 1 bit), then a 12-bit remainder. The value is quotient × 4,096 + remainder. Its lowest bit is the sign: the number is shifted right by one, and negated when the bit is set.
Divergence from the specification
- The SV8 specification names these codes and gives their sizes, and leaves the tables to the source code. The tables in the viewer are taken from the decoder source; the table shapes above are checked by the decoding of real files, not by the specification.
Sources
- libmpcdec 1.2.6 (SV7 tables) and revision 475 of the Musepack source (SV8 tables, the logarithmic and enumerative codes).
- Musepack SV8 specification (archived): the Golomb code of the seek table and the packet size code.