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. Each is in
the logarithmic form expanded by wp_exp2s.
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.
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 nothing is pending, 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 under an
implicit leading 1. A nonzero run clears both channels’ medians. Each sample in the run is then
decoded as 0 with no bits read. A run of length 0 falls through to normal decoding.
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 |
bitrate_acc | Always | 16 bits, shifted left by 16 |
bitrate_delta | If bytes remain | 16-bit logarithmic value, signed; otherwise 0 |
Each block’s error limit is recalculated from these, using (slow_level + 128) >> 8.
When the limit is not 0, the range is narrowed bit by bit until it is no wider than the limit,
instead of a truncated binary read. In a hybrid file with a correction stream, a truncated binary
read of the WVC stream then gives the exact value.
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 |