Bit order
An SV7 file is read as 32-bit words. Each word is stored little-endian: swap the four bytes of the word, and then read its bits from the most significant down. The bits of consecutive fields run on from one word to the next. The file begins with the three bytes MP+ and a version byte, and these four bytes are the first word as stored.
The header
The header is 200 bits (25 bytes). The frames start at bit 200, in the middle of the seventh word.
| Bits | Field | Meaning |
|---|---|---|
| 24 | Marker | The bytes M, P and + (in file order, before any word swap) |
| 4 | Version | The low four bits of byte 3: 7 for this version |
| 4 | Noise substitution | The high four bits of byte 3; non-zero when the stream uses noise substitution |
| 32 | Frames | The number of frames (each 1,152 samples a channel) |
| 1 | Intensity stereo | Set when intensity stereo is used (not by any encoder) |
| 1 | Mid/side | Set when mid/side stereo may be used |
| 6 | Maximum band | The highest coded subband (31 means all 32) |
| 4 | Profile | The quality preset |
| 2 | Link | Unused |
| 2 | Sample rate | An index: 0 is 44,100 Hz, 1 is 48,000 Hz, 2 is 37,800 Hz, 3 is 32,000 Hz |
| 16 | Estimated peak | The peak of the stream, set by the encoder |
| 16 | Title gain | Replay gain of the track, in hundredths of a decibel (signed); 0 is not computed |
| 16 | Title peak | Peak of the track |
| 16 | Album gain | Replay gain of the album, in hundredths of a decibel (signed) |
| 16 | Album peak | Peak of the album |
| 1 | Gapless | Set when the last frame holds fewer than 1,152 samples |
| 11 | Last frame | The number of samples in the last frame when the gapless bit is set |
| 1 | Fast seeking | Set when the file may be seeked quickly (a hint) |
| 19 | Unused | Zero |
| 8 | Encoder | The encoder version. 0 is an early encoder (Buschmann 1.7.0 to 1.7.9 or Klemm 0.90 to 1.05). Up to 116 a multiple of 10 is a release, an even last digit a beta and an odd one an alpha. Above 116 the number is major, minor and build digits (for example 117 is 1.1.7); an odd minor digit is an unstable build |
The two replay gain fields use hundredths of a decibel, where SV8 uses 1/256 dB.
Profiles
The four-bit profile is a quality preset of the encoder. The decoder does not need it. The names, as the reference decoder lists them: 0 not set; 1 unstable or experimental; 2, 3 and 4 not used; 5 and 6 quality 0 and 1; 7 Telephone; 8 Thumb; 9 Radio; 10 Standard; 11 Xtreme; 12 Insane; 13 BrainDead; 14 and 15 quality 9 and 10.
Length and gapless playback
The decoder reads one more frame than the header states, because the filter bank delays the sound by 481 samples (see Synthesis). The number of samples a channel in the stream is:
- without the gapless bit: frames × 1,152 − 481;
- with the gapless bit: frames × 1,152 − (1,152 − last frame), where a last frame of 0 counts as 1,152.
The decoder does not stop at the frame count of the header. It decodes frames until the samples still to play, plus 481, are zero or less. The frame that reaches the end of the stream carries the 11-bit last-frame value again, at the end of its own length field (see Frames), and in gapless streams that value sets the final length. The first 481 samples the filter bank produces are dropped.
Divergence from the specification
- The specification for SV7 on the archived wiki lists the fields above in this order; the frame layout that follows is not in it and was taken from the decoder source.
- The decoder reads one frame more than the header’s frame count in non-gapless streams, as the viewer’s checks show.
Sources
- Musepack SV7 specification (archived copy of the old project wiki, saved in research/musepack/spec), for the field order.
- libmpcdec 1.2.6 and the revision 475 source, for the bit order, the length arithmetic and the unit of the gains.