Packet syntax
After the four bytes MPCK, an SV8 file is a sequence of packets. Each packet has:
| Field | Size | Meaning |
|---|---|---|
| Key | 2 bytes | Two capital letters: SH, RG, EI, SO, ST, AP, CT or SE |
| Size | 1 to 9 bytes | A variable-length number: the size of the whole packet, counting the key, the size field itself and the payload |
| Payload | size − key − size field | The fields of the packet |
A variable-length number (a varint) is written in groups of seven bits, most significant group first. The top bit of each byte is set when another byte follows and clear on the last one. So 0 to 127 take one byte, and 128 to 16,383 take two. Positions of the seek table and sample offsets use the same encoding.
Readers skip packets whose key they do not know, using the size. The reference decoder stops at the stream end packet (SE), which has a size of 3 and no payload; anything after it is a tag.
The usual order is SH, RG, EI, SO, then the AP packets, then ST, CT and SE. The stream header must come first. The seek table offset (SO) may be absent, and the encoder writes the seek table (ST) after the audio.
SH: stream header
The first packet. The CRC, the version byte and the two varints are whole bytes; the last five fields share one 16-bit big-endian word, read from the most significant bit.
| Field | Size | Meaning |
|---|---|---|
| CRC | 32 bits | CRC-32 (the polynomial of PNG and zlib) of every byte of the payload after this field, up to the end of the packet. Zero means not set |
| Version | 8 bits | The stream version: 8 |
| Sample count | varint | Samples a channel in the stream; 0 means unknown |
| Beginning silence | varint | Samples a channel at the start that the player should skip |
| Sample rate | 3 bits | An index: 0 is 44,100 Hz, 1 is 48,000 Hz, 2 is 37,800 Hz, 3 is 32,000 Hz |
| Maximum band | 5 bits | One less than the number of coded subbands (so 31 means all 32) |
| Channels | 4 bits | One less than the channel count |
| Mid/side | 1 bit | Set when mid/side stereo may be used |
| Block power | 3 bits | Audio packets hold 4 to the power of this value frames (so 6 gives 4,096) |
The viewer checks the CRC (and reports a wrong one), and accepts version 8 only.
RG: replay gain
| Field | Size | Meaning |
|---|---|---|
| Version | 1 byte | 1 |
| Title gain | signed 16 bits | The gain of the track |
| Title peak | unsigned 16 bits | The peak of the track |
| Album gain | signed 16 bits | The gain of the album |
| Album peak | unsigned 16 bits | The peak of the album |
All values are big-endian. A gain is in units of 1/256 dB, measured from a reference level of 64.82 dB: the gain in decibels is 64.82 minus the value divided by 256. A value of zero means that it was not computed. A peak is 256 times 20 times the base-10 logarithm of the sample peak (full scale 32,768); zero again means not computed.
EI: encoder information
| Field | Size | Meaning |
|---|---|---|
| Profile | 7 bits | The quality setting in 1/8 steps (so 40 is quality 5.0) |
| Noise substitution | 1 bit | Set when the stream uses noise substitution |
| Major version | 8 bits | Encoder version |
| Minor version | 8 bits | Even for a stable release, odd for a development build |
| Build | 8 bits | The build number |
SO: seek table offset
One varint: the distance in bytes from the start of this packet to the start of the ST packet. The reference encoder writes it before the audio and pads the payload with zero bytes, so that it can come back and fill it in once the seek table is placed.
AP: audio packet
The payload is a bit stream of frames, one after another, and padded at the end to a whole byte. An audio packet holds the number of frames given by the block power of the stream header (the last packet may hold fewer). The first frame of every packet is a key frame: it carries everything a decoder needs to start there (the used bands, all scalefactors, and the resolutions are coded without reference to the previous frame). Later frames in the packet are coded relative to the frame before. See Frames.
ST: seek table
A list of the positions of audio packets, so that a player can jump close to a given sample. All fields are bit fields:
| Field | Size | Meaning |
|---|---|---|
| Count | varint | The number of entries |
| Spacing | 4 bits | Entries mark every 2 to the power of this value audio packets |
| First value | varint (when the count is 1 or more) | The byte position of the first entry, counted from the M of MPCK |
| Second value | varint (when the count is 2 or more) | The second entry, coded the same way. These two varints start straight after the 4-bit spacing, so they are not byte-aligned |
| Remaining entries | Golomb codes | For each further entry, a code of the second difference |
The second difference of entry n is position(n) − 2 × position(n−1) + position(n−2). It is folded to an unsigned number (the sign in the lowest bit) and written as a Golomb code: the quotient in unary (that many 1 bits and a 0 bit), then the remainder in 12 bits. The decoder recovers each position from the two before it.
CT: chapter
| Field | Size | Meaning |
|---|---|---|
| Start sample | varint | The sample where the chapter begins |
| Gain | signed 16 bits | Replay gain of the chapter (as in RG) |
| Peak | unsigned 16 bits | Peak of the chapter (as in RG) |
| Tag | APEv2 items | An APEv2 tag without the APETAGEX preamble: a 24-byte header, the items, and optionally a 24-byte footer. See Tags |
SE: stream end
The two bytes SE and a size of 3. It has no payload, and nothing after it belongs to the stream. Tags follow it.
Divergence from the specification
- The specification gives the SO offset as a varint of 1 to 9 bytes. The reference encoder writes the packet with a fixed, zero-padded payload and fills it in later, so bytes after the varint may be padding. The viewer shows them as padding.
Sources
- Musepack SV8 specification, the packet syntax, SH, RG, EI, SO, ST, AP, CT and SE (archived copy of the old project wiki, saved in research/musepack/spec).
- The Musepack decoder source (libmpcdec 1.2.6 and revision 475), for the packet reading, the Golomb code of the seek table and the replay gain units.