WavPack’s main path codes integers of up to 24 bits. Floating-point audio, wider integers and DSD each have an extra layer. Float and wide-integer files split each sample into an integer part in the main stream and extra bits in the WVX stream.
Floating point (ID_FLOAT_INFO)
Exactly 4 bytes:
| Offset | Field | Meaning |
|---|---|---|
| 0 | float_flags | See below |
| 1 | float_shift | Left shift of the integer values, up to 31 |
| 2 | float_max_exp | The starting exponent: that of the block’s largest value |
| 3 | float_norm_exp | The exponent of full scale in the original data: 127 when full scale is ±1.0. A decoder asked for normalised output scales by 2^(127 − float_norm_exp) |
| Mask | Name | Meaning |
|---|---|---|
0x01 | FLOAT_SHIFT_ONES | Bits shifted out are filled with 1s |
0x02 | FLOAT_SHIFT_SAME | A WVX bit says whether the low bits are all 1s |
0x04 | FLOAT_SHIFT_SENT | Low bits are read from the WVX stream |
0x08 | FLOAT_ZEROS_SENT | Zero samples carry extra data in the WVX stream |
0x10 | FLOAT_NEG_ZEROS | A sign bit is stored for zero samples |
0x20 | FLOAT_EXCEPTIONS | Marks a block with exceptions; the reference decoder does not test this bit |
Rebuilding a float
The main stream holds each float as a signed integer of up to 24 bits: the mantissa with its
hidden 1, shifted right by however far the value’s exponent is below
float_max_exp. The WVX stream holds what that loses. For each sample, in the
order the samples are interleaved:
- Zero. With FLOAT_ZEROS_SENT, the WVX stream has a presence bit. If set:
a 23-bit mantissa, an 8-bit exponent (only when
float_max_expis 25 or more; otherwise the exponent is 0) and a sign bit. If clear and FLOAT_NEG_ZEROS is set, only a sign bit. Without FLOAT_ZEROS_SENT a zero is +0. - Otherwise shift the integer left by
float_shift. A negative value is negated and gets the sign bit. - Infinity and NaN. A magnitude of exactly 0x1000000 means an exponent of 255. One WVX bit says whether a 23-bit mantissa follows; without it the mantissa is 0.
- Normalise. Starting from
float_max_exp, shift left and lower the exponent while bit 23 is clear, stopping if the exponent reaches 0 (a denormal). The number of shifts, at most 31, issc; the mantissa is the low 23 bits. - Fill the shifted bits. If
scis not 0: FLOAT_SHIFT_ONES fills them with 1s; else FLOAT_SHIFT_SAME reads one bit, and a 1 fills them with 1s; else FLOAT_SHIFT_SENT reads them from the WVX stream, lowest first; else they stay 0.
With ID_WVX_NEW_BITSTREAM, FLOAT_SHIFT_SENT does not always send all sc bits.
Let z be 0, or sc − max_shifted_ones when
max_shifted_ones is not 0 and sc is larger; if
min_shifted_zeros is larger than z, z becomes the
smaller of min_shifted_zeros and sc. Then sc − z bits
are read and placed above z zero bits.
The WVX stream’s CRC is updated for every sample, zeros included, as
crc = crc × 27 + mantissa × 9 + exponent × 3 + sign (32 bits, starting at
0xFFFFFFFF), and compared with the CRC at the start of the WVX sub-block. The header’s CRC
covers the integers.
Without a WVX stream (a hybrid file without its correction file, or one written with CONFIG_SKIP_WVX) the floats are rebuilt from the integers alone: magnitudes of 0x1000000 or more are shifted down while bits 24 to 27 are set, raising the exponent; smaller ones are normalised as above, and only FLOAT_SHIFT_ONES fills the shifted bits. Zeros are +0.
A float block always has BYTES_STORED 3, and the header’s SHIFT does not apply to it.
Wide integers (ID_INT32_INFO)
Exactly 4 bytes:
| Offset | Field | After decoding |
|---|---|---|
| 0 | sent_bits | Low-order bits sent in the WVX stream: x = (x << sent_bits) | bits (FFmpeg allows up to 30) |
| 1 | zeros | Low bits that are all 0: x <<= zeros |
| 2 | ones | Low bits that are all 1: x = ((x + 1) << ones) − 1 |
| 3 | dups | Low bits that copy the lowest kept bit: x = ((x + (x & 1)) << dups) − (x & 1) |
The block’s flags include INT32_DATA, and its integers come from the main stream as usual. After decoding, each value is rebuilt in this order:
- With a WVX stream: the
sent_bitslow bits are read from it, lowest first, and appended. With ID_WVX_NEW_BITSTREAM and a nonzero largest widthW, a value whose own bit lengthb(ofx, or of~xwhen negative) would makeb + sent_bitswider thanWgets onlymax(0, W − b)bits, and the rest of its low bits are 0. - Then one of zeros, ones or dups, the first that is not 0.
- The WVX CRC:
crc = crc × 9 + (x & 0xFFFF) × 3 + ((x >> 16) & 0xFFFF)over every value, starting at 0xFFFFFFFF, compared with the CRC at the start of the WVX sub-block. - The header’s SHIFT, as for any block.
Without a WVX stream, if sent_bits is not 0 the missing bits cannot be known:
all four fields are added to the shift instead, so the value is the integer moved left, and
the file is lossy. If only zeros, ones or dups is set, it is applied as above. In a lossy
32-bit block the reference decoder moves up to 8 of those bits into the shift instead, so
that the clipping to the sample width works.
FFmpeg’s decoder applies only the first nonzero field of the four, and does not read ID_WVX_NEW_BITSTREAM.
DSD (ID_DSD_BLOCK)
A block with DSD_FLAG holds 1-bit audio, a byte of eight one-bit samples at a time, the first
sample in the top bit. A “sample” in the header’s counts is one such byte a channel. The
header’s sample rate times 1 << shift gives bytes a second, and eight
times that is the DSD rate: 44,100 with a shift of 3 is DSD64, 2,822,400 one-bit samples a
second. The channels’ bytes alternate in a stereo block, and the header’s CRC is
crc = crc × 3 + byte over them in that order.
| Offset | Field | Meaning |
|---|---|---|
| 0 | Rate shift | 0 to 31. Used from the first stream only |
| 1 | Mode | 0 raw, 1 fast, 3 high. Any other value is an error |
Mode 0: raw
One byte per sample per channel. The data length must be exactly block_samples × channels.
Mode 1: fast
| Field | Size | Meaning |
|---|---|---|
| History bits | 1 | At most 5. There are 1 << bits bins |
| Max probability | 1 | Below 0xFF the table is run-length coded; 0xFF means bins × 256 raw bytes follow |
| Probability table | varies | In the run-length form, codes above the maximum are runs of zeros, others are literal values, and a zero byte ends it |
| Range coder start | 4 | Then the data |
The table has 256 one-byte probabilities for each bin. In the run-length form, a code
above the maximum stands for code − maximum zeros; after the table is full a
0 byte must follow. The probabilities of all bins together may add up to at most
bins × 1280.
Each byte is one symbol of a range coder (32-bit low, high and
value, starting at 0, 0xFFFFFFFF and the first four data bytes, big-endian).
The bin is chosen by the low history bits of the same channel’s previous byte, starting at
bin 0:
T = the bin's total
mult = (high − low) ÷ T
if mult = 0: value = the next 4 bytes, low = 0, high = 0xFFFFFFFF, mult = high ÷ T
index = (value − low) ÷ mult (must be below T)
byte = the symbol whose share of the bin covers index
low += (sum of the probabilities below byte) × mult
high = low + probability(byte) × mult − 1
while the top bytes of low and high are equal:
value = value << 8 | next byte, high = high << 8 | 0xFF, low = low << 8
Mode 3: high
| Field | Size | Meaning |
|---|---|---|
| rate_i | 1 | Used to build a 256-entry probability table |
| rate_s | 1 | Must be 20 |
| Per channel | 7 each | Five filter bytes, shifted left by 12, then a 16-bit signed factor, low byte first |
| Range coder start | 4 | Then the data |
After the rate shift and mode bytes, this mode needs at least 13 bytes for mono (2 + 7 + 4) and 20 for stereo (2 + 14 + 4). A sixth filter value starts at 0.
Each one-bit sample is coded with an adaptive probability chosen by a predictor. The
probability table has 256 entries in 8.16 fixed point; it starts from rate_i
and rate_s, and each entry moves 1/256 of the way towards 0x10000 (a 0 bit) or
0x10000FE (a 1 bit) whenever it is used:
value = 0x808000, rate = rate_i × 256
repeat (rate + 128) >> 8 times: value += (0x10000 − value) >> 8
for i = 0 to 127:
table[i] = value, table[255 − i] = 0x100FFFF − value
if value > 0x10000:
rate += (rate × rate_s + 128) >> 8
repeat (rate + 64) >> 7 times: value += (0x10000 − value) >> 8
For each byte, every channel’s prediction is
p = f1 − f5 + ((f6 × factor) >> 2) (values in 12.20 fixed point). Then
for each of the 8 bits, top first, and in a stereo block for each channel in turn on the one
range coder:
t = table[(p >> 8) & 255]
split = low + ((high − low) >> 8) × (t >> 16)
value ≤ split: bit 1, high = split, t += (0x10000FE − t) >> 8
otherwise: bit 0, low = split + 1, t += (0x10000 − t) >> 8
(renormalise as in mode 1)
p += f6 × 8
factor ± 1: by the sign rule below, only when p and p − 16 × f6 differ in sign
f1 += (B − f1) >> 6, f2 += (B − f2) >> 4 B = 0x100000 for a 1 bit, else 0
f3 += (f2 − f3) >> 4, f4 += (f3 − f4) >> 4
d = (f4 − f5) >> 4, f5 += d, f6 += (d − f6) >> 3
p = f1 − f5 + ((f6 × factor) >> 2)
The factor rises by 1 when the bit is 1 and p is negative, or the bit is 0 and
p is not; otherwise it falls by 1. After each byte, every channel’s factor
decays: factor −= (factor + 512) >> 10.
The reference library decodes DSD to bytes. Players that need PCM low-pass and decimate them; that step is not part of the format.
Source: libwavpack 5.x, src/unpack_floats.c, src/unpack_dsd.c, src/wavpack_local.h (the FLOAT_ bits) and src/unpack.c. The format document (April 2020) says only that ID_FLOAT_INFO, ID_INT32_INFO and ID_DSD_BLOCK exist; their layouts are not in it. These layouts were read from the source in earlier rounds and checked against libwavpack’s own output (wvunpack) and ffmpeg; they are not compared by spec-check.mjs beyond the FLOAT_ bit values.