The audio sub-blocks hold residuals, read through three adaptive medians per channel. This page follows the reference library. Bits are read from the lowest bit of each byte first.
Medians (ID_ENTROPY_VARS)
Three 16-bit little-endian values per channel, so 6 bytes for mono and 12 for stereo, and no
other length. Each is in the logarithmic form expanded by
wp_exp2s, read as unsigned. The medians are unsigned 32-bit numbers, and they
start again from these values in every block.
GET_MED(n) = (median[n] >> 4) + 1
inc(n): median[n] += ((median[n] + DIVn) ÷ DIVn) × 5
dec(n): median[n] −= ((median[n] + DIVn − 2) ÷ DIVn) × 2
DIV0 = 128, DIV1 = 64, DIV2 = 32
Reading a value
- Count the 1 bits up to a 0: this is the unary count.
- If the count reaches 16 (
LIMIT_ONES), an escape follows: an Elias-gamma number added to 16. A larger count than that is an error. - Split the result into a parity bit and a count, using the pending
holding_oneandholding_zerostate left by the previous value. The parity bit becomesholding_one, and half the value (plus 1, if the lastholding_onewas set) isones_count. An even value setsholding_zero, so that the next value has aones_countof 0 with no bits read. - Choose the range from the medians:
| ones_count | low | high | Medians changed |
|---|---|---|---|
| 0 | 0 | GET_MED(0) − 1 | dec 0 |
| 1 | GET_MED(0) | low + GET_MED(1) − 1 | inc 0, dec 1 |
| 2 | GET_MED(0) + GET_MED(1) | low + GET_MED(2) − 1 | inc 0, inc 1, dec 2 |
| 3 or more | GET_MED(0) + GET_MED(1) + (ones_count − 2) × GET_MED(2) | low + GET_MED(2) − 1 | inc 0, inc 1, inc 2 |
Then read the value within the range, and read one sign bit: 1 means negative, and the sample is
~value. Otherwise it is value. In a stereo block the values
alternate between the two channels, each with its own medians.
How the counts pair up
The parity rule means a single count can serve two values. After a count, exactly one of
holding_one and holding_zero is set. With
holding_zero, the next value is known to be in the lowest range and reads no
count. With holding_one, the next value is known to be above it, so its count is
read and 1 is added to half of it. An encoder chooses each count’s low bit by looking one
value ahead.
The truncated binary code
To read a number up to max: a max of 0 reads nothing, and 1 reads one
bit. Otherwise let bits be the bit length of max, and
extras = 2^bits − max − 1. Read bits − 1 bits. If the value is under
extras, that is the number. If not, read one more bit, and the number is
(value × 2) − extras + bit.
Runs of zeros
When both channels’ median[0] are 0 or 1 and neither
holding_one nor holding_zero is set, a run of zeros may be coded.
An Elias-gamma number gives the run length: count the 1 bits to a 0 (cbits),
which is the length itself if under 2; otherwise read cbits − 1 more bits,
lowest first, under an implicit leading 1. Thirty-three 1 bits are an error.
- A run of
nclears both channels’ medians, and the value being read and then − 1after it are 0, with no bits read. Values alternate between channels as usual, so a run can cross from one channel to the other. - The value after the run is read normally, without checking for another run.
- A run of length 0 means the value being read is coded normally.
- In hybrid mode, every zero of a run lowers its channel’s
slow_levelby(slow_level + 128) >> 8, and the error limit is not updated.
Hybrid mode (ID_HYBRID_PROFILE)
The payload holds these, one value per channel each, in the order:
| Item | Present | Stored as |
|---|---|---|
slow_level | Only with HYBRID_BITRATE | 16-bit logarithmic value, unsigned |
bitrate_acc | Always | 16 bits, shifted left by 16 |
bitrate_delta | If bytes remain | 16-bit logarithmic value, signed; otherwise 0 |
Any bytes after the deltas make the sub-block invalid.
The error limit
Each channel has an error limit: how far a decoded value may be from the true one. It is worked out again before every value of the first channel that is not part of a zero run (32-bit unsigned arithmetic):
bitrate_acc[c] += bitrate_delta[c]
br[c] = bitrate_acc[c] >> 16
slow[c] = (slow_level[c] + 128) >> 8
HYBRID_BITRATE clear: limit[c] = wp_exp2s(br[c])
HYBRID_BITRATE set: limit[c] = slow[c] − br[c] > −256 ? wp_exp2s(slow[c] − br[c] + 256) : 0
In a stereo block with both HYBRID_BITRATE and HYBRID_BALANCE, the two bit rates are first shared out by level:
balance = (slow[1] − slow[0] + br[1] + 1) >> 1
balance > br[0]: br[1] = br[0] × 2, br[0] = 0
−balance > br[0]: br[0] = br[0] × 2, br[1] = 0
otherwise: br[1] = br[0] + balance, br[0] = br[0] − balance
FFmpeg’s decoder applies this balance in every stereo bit-rate block, with or without HYBRID_BALANCE.
Reading a value in hybrid mode
The count and the range come from the medians as above, with low and
high kept to 31 bits and high raised to low if it is
below it. With a limit of 0 the value is read exactly, as in lossless mode. Otherwise the
range is halved bit by bit:
mid = (high + low + 1) >> 1
while high − low > limit:
bit 1: low = mid
bit 0: high = mid − 1
mid = (high + low + 1) >> 1
value = mid, then the sign bit
With HYBRID_BITRATE, each value then updates its channel’s level:
slow_level −= (slow_level + 128) >> 8, then
slow_level += wp_log2(mid).
The correction stream
With a .wvc file, a value read with a nonzero limit is followed, in the WVC
bit stream, by a truncated binary number up to
high − low. Added to low it gives the exact magnitude
v, and the correction is v − mid, or mid − v for a
negative value. Adding the correction to the lossy residual gives the exact residual.
- Mono, and stereo without CROSS_DECORR: the corrections are added after the decorrelation passes (and after joint stereo is undone on both versions).
- Stereo with CROSS_DECORR: the corrected residuals go through the passes’ predictions alongside the lossy ones, using the same weights and history, because the cross-channel terms need the corrected value of the other channel.
- The decorrelation history always holds the lossy values; the correction never changes what the next lossy sample decodes to.
- The header CRC to compare is the correction block’s, which covers the lossless samples.
Noise shaping (ID_SHAPING_WEIGHTS)
| Payload size | Layout |
|---|---|
| 2 bytes | Two signed bytes, restored as weights and shifted left by 16 |
| 4 bytes (mono) or 8 bytes (stereo), or more | Per channel, a 16-bit logarithmic error followed by 16-bit shaping_acc |
| 6 bytes (mono) or 12 bytes (stereo) | Then also a 16-bit shaping_delta per channel |
The 16-bit values are signed logarithms, expanded by wp_exp2s. Any other length
is invalid, and anything not stored starts at 0 in each block.
Noise shaping moves a hybrid file’s noise towards frequencies where it is heard less.
The lossy decoder does not need to know about it: the shaping is already in the lossy
samples. Only a decoder that adds the correction stream must undo it, for each channel and
each sample (signed 32-bit, apply as for weights):
shaping_acc += shaping_delta
w = shaping_acc >> 16
temp = −apply(w, error)
if NEW_SHAPING and w < 0 and temp ≠ 0:
if temp = error: move temp 1 towards 0
error = temp − correction
else:
error = −correction
sample = corrected − temp
Here corrected is the lossy sample plus its correction, and
correction is the difference between the two. In version 0x402 hybrid streams
the starting errors are stored at the front of
ID_DECORR_SAMPLES.
The logarithmic form
Medians, histories, levels and rates are stored as 16-bit logarithms: 8 bits of whole number and 8 bits of fraction, base 2. Two 256-entry tables of bytes do the work:
exp2_table[i] = round(256 × 2^(i ÷ 256)) − 256
log2_table[i] = round(256 × log2(1 + i ÷ 256))
wp_exp2s(L): negative L gives −wp_exp2s(−L)
v = exp2_table[L & 255] + 256, e = L >> 8
e ≤ 9: v >> (9 − e) otherwise: v << ((e − 9) & 31)
wp_log2(x): x += x >> 9 (unsigned)
d = the bit length of x
x < 256: (d << 8) + log2_table[(x << (9 − d)) & 255]
otherwise: (d << 8) + log2_table[(x >> (d − 9)) & 255]
These formulas give the reference library’s tables exactly. The encoder stores
wp_log2 of each value, signed for values that can be negative.
Source: libwavpack 5.x, src/read_words.c and src/unpack.c. The format document (April 2020) does not describe the entropy coder; this page follows the source (read in earlier rounds, outputs compared with libwavpack and ffmpeg), not a published specification.