What a frame holds
A frame holds 1,152 samples for each channel, as 32 subbands of 36 samples. Within a band the 36 samples are split into three groups of 12, and each group has its own scalefactor. The bands are numbered from 0 (the lowest frequencies). The fields below are always read for a left and a right channel.
The fields appear in the same order in both versions: the resolution of every band, the mid/side flags, the scalefactor selectors, the scalefactors, and then the samples. They are coded in different ways, which the entropy coding page describes. A resolution of 0 means that a channel has no samples in a band; a band beyond the last used band has none in either channel.
An SV8 frame
Frames sit inside an AP packet with no separate length. The first frame of a packet is a key frame. The decoder reads:
- Used bands. The number of bands that follow, 0 to 32. In a key frame it is a logarithmic code with a maximum of the stream’s maximum band plus one. In other frames it is a Huffman-coded difference from the previous frame’s value (a result above 32 wraps by 33).
- Resolutions. Only when the used bands are not zero. First the resolution of the top used band of the left and then of the right channel: a Huffman code, where a value above 15 means that 17 is subtracted (so 16 and 17 read as −1 and 0, down to −17 where applicable). Then, from the second highest band down to band 0, each band’s difference from the band above it, left channel and then right channel, with a Huffman table chosen by whether the band above has a resolution above 2. A resolution outside −17 to 17 is a fault.
- Mid/side flags. Only when the stream allows mid/side. Let t be the number of bands where either channel has a non-zero resolution. A logarithmic code gives how many of them use mid/side. Unless that is none or all, an enumerative code follows that says which, for the smaller of the count and its complement; with more than half set, the bits are inverted. The flags are assigned from the top band down.
- Scalefactor selectors. For each used band, if either channel is coded, one Huffman code that holds the selector of each channel that is coded (two bits each: 0 three scalefactors, 1 the first two differ and the third repeats the second, 2 the first repeats into the second and the third differs, 3 all three alike. In SV8 a selector bit set means the next scalefactor is a copy).
- Scalefactors. For each used band and each channel with a resolution: the first scalefactor and then the other two. In a key frame the first is absolute: 7 bits minus 6. Otherwise it is a Huffman delta from the last scalefactor of that band in the previous frame (the sum is taken modulo 128), and a delta of 64 is followed by 6 more bits. The second and third are copies of the one before when the selector says so, and otherwise a Huffman delta from it (with 31 meaning 64 plus 6 more bits).
- Samples. For each band and then each channel, 36 quantised values, in the way the resolution says. See below.
SV8 sample codes by resolution
| Resolution | Coding of the 36 values |
|---|---|
| 0 | Nothing is stored; the values are zero |
| −1 | Nothing is stored: the decoder makes noise (see Requantisation) |
| 1 | Two halves of 18 values. Each half has a Huffman code of the number of non-zero values, an enumerative code of which ones they are (inverted when more than 9), and then a sign bit for each non-zero value (1 is +1, 0 is −1) |
| 2 | Twelve Huffman codes of three values each. The table of each code is chosen by a running measure of the size of the values so far |
| 3, 4 | Eighteen Huffman codes of two values each; the code gives the two values as two four-bit two’s complement numbers |
| 5 to 8 | One Huffman code for each value, with one of two tables chosen by a running measure of the size of the values so far |
| 9 to 17 | One Huffman code for each value, then (resolution − 9) extra low bits, and the value is reduced by an offset that depends on the resolution |
An SV7 frame
Each SV7 frame starts with a 20-bit length in bits, which counts the fields that follow it. The frame that ends the stream has an 11-bit number of samples after its fields (the gapless length), which is not counted in the 20-bit length. The fields:
- Resolutions. Band 0 has a 4-bit resolution for the left and then the right channel. Each following band, up to the stream’s maximum band, has a Huffman-coded difference from the band below for the left and then the right channel, where the code value 4 means that a plain 4-bit resolution follows instead.
- Mid/side flags. A one-bit flag after the resolutions of each band that has a non-zero resolution, when the stream allows mid/side. The number of used bands is the highest such band plus one.
- Scalefactor selectors. For each used band, the left and then right channel with a non-zero resolution has one Huffman code from 0 to 3.
- Scalefactors. For each used band and channel with a resolution, three scalefactors as the selector says. Each one is a Huffman-coded difference from the one before (the value 8 means that 6 plain bits follow). Selector 0 codes all three, 1 codes the first and the second and repeats the second, 2 codes the first and the third and repeats the first, and 3 codes only the first. A scalefactor above 1,024 is treated as the value 0x8080, which is a silent band.
- Samples. For each used band and each channel, 36 values: resolution −1 stores nothing and makes noise; 0 stores nothing; 1 has a table-select bit and 12 Huffman codes of three values; 2 has a table-select bit and 18 codes of two values; 3 to 7 have a table-select bit and 36 codes of one value; 8 to 17 have 36 plain values of the width in the table below, reduced by an offset.
| Resolution | 8 | 9 | 10 | 11 | 12 | 13 | 14 | 15 | 16 | 17 |
|---|---|---|---|---|---|---|---|---|---|---|
| Bits a value | 7 | 8 | 9 | 10 | 11 | 12 | 13 | 14 | 15 | 16 |
The width is one less than the resolution. The offsets are in the requantisation table.
Per-frame faults
The viewer reads every frame and lists the faults: a resolution outside −17 to 17, a number of used bands above 32, a scalefactor selector outside 0 to 3 (SV7), a read past the end of the data, and in SV7 a frame whose fields do not use exactly the bits its length field declares. After a fault in SV7 the next frame is found from the length field. SV8 frames have no length, so a fault is shown and reading goes on from where it stopped; the first frame of the next packet starts cleanly again.
Divergence from the specification
- The archived SV8 specification gives the frame fields as one line each and refers to the decoder for the code tables. The order above comes from the decoder source.
- The archived SV7 specification stops at the header; the frame order comes from the decoder source.
Sources
- Musepack decoder source: libmpcdec 1.2.6 (
mpc_decoder.cand the SV7 and SV8 table files) and revision 475 (SV8 reading and the enumerative and logarithmic codes). - Musepack SV8 specification (archived), the frame and packet fields.