std/vp8: parse the residuals

This commit, part of a series, is inspired by pull request PR #168 by
Damian Kaczmarek (with AI assistance), with some modifications.

Updates PR #168
This commit is contained in:
Nigel Tao committed 2026-06-12 22:21:24 +10:00
1 parent 3f612e7214
commit f8a443b85b
6 files changed
+890 -62

No files matched your search

+55
View File
@@ -107,6 +107,10 @@ pri const RENORM_RANGE_M1 : roarray[256] base.u8[127 ..= 254] = [
254,
]
pri const CLIP_9 : roarray[16] base.u8[..= 9] = [
0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 9, 9, 9, 9, 9, 9,
]
// RFC 6386 Section 11.5. Subblock Mode Probability Table.
//
// Note that the specification (the RFC) differs from the implementation
@@ -243,6 +247,31 @@ pri const SUBBLOCK_MODE_PROBS : roarray[10] roarray[10] roarray[9] base.u8[1 ..=
[112, 19, 12, 61, 195, 128, 48, 4, 24],
]]
// RFC 6386 Section 13.2. Coding of Individual Coefficient Values.
//
// Categories 1 and 2 are inlined.
pri const CATEGORY_PROBS : roarray[4] roarray[16] base.u8 = [[
// Category 3.
173, 148, 140, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
],[
// Category 4.
176, 155, 140, 135, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
],[
// Category 5.
180, 157, 141, 134, 130, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
],[
// Category 6.
254, 254, 243, 230, 196, 177, 153, 140, 133, 130, 129, 0, 0, 0, 0, 0,
]]
// RFC 6386 Section 13.3. Token Probabilities.
//
// These are the RFC's coeff_bands array values multplied by 33, with an extra
// element at the end so we can index by (i+1).
pri const COEFF_BANDS_33 : roarray[17] base.u32[..= 231] = [
0, 33, 66, 99, 198, 132, 165, 198, 198, 198, 198, 198, 198, 198, 198, 231, 0,
]
// RFC 6386 Section 13.4. Token Probability Updates.
//
// 1056 is (4 planes, 8 bands, 3 contexts, 11 tokens) flattened.
@@ -532,3 +561,29 @@ pri const DEFAULT_COEFF_PROBS : roarray[1056] base.u8[1 ..=] = [
244, 1, 255, 128, 128, 128, 128, 128, 128, 128, 128,
238, 1, 255, 128, 128, 128, 128, 128, 128, 128, 128,
]
// RFC 6386 Section 14.1. Dequantization.
pri const DEQUANTS : roarray[2] roarray[128] base.u16[..= 284] = [[
4, 5, 6, 7, 8, 9, 10, 10, 11, 12, 13, 14, 15, 16, 17, 17,
18, 19, 20, 20, 21, 21, 22, 22, 23, 23, 24, 25, 25, 26, 27, 28,
29, 30, 31, 32, 33, 34, 35, 36, 37, 37, 38, 39, 40, 41, 42, 43,
44, 45, 46, 46, 47, 48, 49, 50, 51, 52, 53, 54, 55, 56, 57, 58,
59, 60, 61, 62, 63, 64, 65, 66, 67, 68, 69, 70, 71, 72, 73, 74,
75, 76, 76, 77, 78, 79, 80, 81, 82, 83, 84, 85, 86, 87, 88, 89,
91, 93, 95, 96, 98, 100, 101, 102, 104, 106, 108, 110, 112, 114, 116, 118,
122, 124, 126, 128, 130, 132, 134, 136, 138, 140, 143, 145, 148, 151, 154, 157,
],[
4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15, 16, 17, 18, 19,
20, 21, 22, 23, 24, 25, 26, 27, 28, 29, 30, 31, 32, 33, 34, 35,
36, 37, 38, 39, 40, 41, 42, 43, 44, 45, 46, 47, 48, 49, 50, 51,
52, 53, 54, 55, 56, 57, 58, 60, 62, 64, 66, 68, 70, 72, 74, 76,
78, 80, 82, 84, 86, 88, 90, 92, 94, 96, 98, 100, 102, 104, 106, 108,
110, 112, 114, 116, 119, 122, 125, 128, 131, 134, 137, 140, 143, 146, 149, 152,
155, 158, 161, 164, 167, 170, 173, 177, 181, 185, 189, 193, 197, 201, 205, 209,
213, 217, 221, 225, 229, 234, 239, 245, 249, 254, 259, 264, 269, 274, 279, 284,
]]
// RFC 6386 Section 20.16. tokens.c.
pri const ZIGZAG : roarray[16] base.u8[..= 15] = [
0, 1, 4, 8, 5, 2, 3, 6, 9, 12, 13, 10, 7, 11, 14, 15,
]
+42
View File
@@ -74,6 +74,7 @@ pri func decoder.decode_first_partition!(workbuf: roslice base.u8) base.status {
this.quant_y2ac_delta = this.read_signed!(workbuf: args.workbuf, part: 0, n: 4)
this.quant_uvdc_delta = this.read_signed!(workbuf: args.workbuf, part: 0, n: 4)
this.quant_uvac_delta = this.read_signed!(workbuf: args.workbuf, part: 0, n: 4)
this.calculate_dequants!()
// RFC 6386 Section 9.7. Refresh Golden Frame and Altref Frame.
if not this.is_key_frame {
@@ -203,6 +204,47 @@ pri func decoder.decode_coeff_probs!(workbuf: roslice base.u8) {
}
}
pri func decoder.calculate_dequants!() {
var seg : base.u32
var q : base.u32
seg = 0
while seg < 4 {
q = this.quant_yac_qi
if not this.seg_enabled {
// No-op.
} else if this.seg_absolute {
q = this.util.sign_extend_convert_u8_u32(a: this.seg_quants[seg])
} else {
q ~mod+= this.util.sign_extend_convert_u8_u32(a: this.seg_quants[seg])
}
this.dequants[seg][0][0] = DEQUANTS[0][this.clip_127(a: q ~mod+ this.quant_ydc_delta)]
this.dequants[seg][0][1] = DEQUANTS[1][this.clip_127(a: q)]
this.dequants[seg][1][0] = DEQUANTS[0][this.clip_127(a: q ~mod+ this.quant_y2dc_delta)] * 2
this.dequants[seg][1][1] = (DEQUANTS[1][this.clip_127(a: q ~mod+ this.quant_y2ac_delta)] * 155) / 100
if this.dequants[seg][1][1] < 8 {
this.dequants[seg][1][1] = 8
}
this.dequants[seg][2][0] = DEQUANTS[0][this.clip_127(a: q ~mod+ this.quant_uvdc_delta)]
if this.dequants[seg][2][0] > 132 {
this.dequants[seg][2][0] = 132
}
this.dequants[seg][2][1] = DEQUANTS[1][this.clip_127(a: q ~mod+ this.quant_uvac_delta)]
seg += 1
}
}
pri func decoder.clip_127(a: base.u32) base.u32[..= 127] {
if args.a >= 0x8000_0000 {
return 0
} else if args.a >= 127 {
return 127
}
return args.a
}
pri func decoder.decode_other_partition_lengths!(workbuf: roslice base.u8) base.status {
var v32 : base.u32
var remainder : base.u32
+35 -32
View File
@@ -16,10 +16,7 @@ pri func decoder.decode_macroblocks!(workbuf: slice base.u8) base.status {
while mbx < this.mbw {
assert mbx < 0x400 via "a < b: a < c; c <= b"(c: this.mbw)
this.mb_top_modes[mbx][0] = 0
this.mb_top_modes[mbx][1] = 0
this.mb_top_modes[mbx][2] = 0
this.mb_top_modes[mbx][3] = 0
this.mb_states_top[mbx] = 0
mbx += 1
}
@@ -28,10 +25,7 @@ pri func decoder.decode_macroblocks!(workbuf: slice base.u8) base.status {
while mby < this.mbh {
assert mby < 0x400 via "a < b: a < c; c <= b"(c: this.mbh)
this.mb_left_modes[0] = 0
this.mb_left_modes[1] = 0
this.mb_left_modes[2] = 0
this.mb_left_modes[3] = 0
this.mb_states_left = 0
mbx = 0
while mbx < this.mbw,
@@ -81,8 +75,14 @@ pri func decoder.decode_one_macroblock!(workbuf: slice base.u8, mbx: base.u32[..
chroma_mode = this.decode_chroma_mode!(workbuf: args.workbuf)
if skip {
// TODO.
// Clear the HNZC bits.
this.mb_states_left &= 0x0000_FFFF
this.mb_states_top[args.mbx] &= 0x0000_FFFF
} else {
this.decode_residuals!(
workbuf: args.workbuf, mbx: args.mbx, mby: args.mby,
seg: seg, luma_mode: luma_mode, chroma_mode: chroma_mode)
}
}
@@ -94,6 +94,11 @@ pri func decoder.decode_one_macroblock!(workbuf: slice base.u8, mbx: base.u32[..
// 3 = B_HE_PRED = predict columns using column to the left.
// 4 = B_PRED = each Y subblock is independently predicted.
//
// Returning (as luma_mode) at least 4 (equivalently, returning B_PRED) means
// that we use "intra4x4 prediction" (as opposed to "intra16x16 prediction").
// (luma_mode >= 4) here is equivalent to libwebp's "is_i4x4 == true" and
// golang.org/x/image/vp8's "usePredY16 == false".
//
// The RFC gives an enum in (DC_PRED, V_PRED, H_PRED, TM_PRED, B_PRED) order
// but the libwebp implementation uses a different one, (B_DC_PRED, B_TM_PRED,
// B_VE_PRED, B_HE_PRED, B_PRED). Modulo the B_ prefixes, it's DC,V,H,TM versus
@@ -106,9 +111,11 @@ pri func decoder.decode_luma_mode!(workbuf: slice base.u8) base.u32[..= 4] {
v1 = this.read_bit!(workbuf: args.workbuf, part: 0, prob: 145)
if v1 == 0 {
return 4 // 4=B_PRED.
return 4 // 4=B_PRED. is_i4x4 == true. usePredY16 == false.
}
// Otherwise, is_i4x4 == false. usePredY16 == true.
v1 = this.read_bit!(workbuf: args.workbuf, part: 0, prob: 156)
if v1 == 0 {
v1 = this.read_bit!(workbuf: args.workbuf, part: 0, prob: 163)
@@ -144,33 +151,27 @@ pri func decoder.decode_subblock_modes!(
workbuf: slice base.u8,
mbx: base.u32[..= 0x3FF],
luma_mode: base.u32[..= 4]) {
var i : base.u32
var top_mode : base.u8[..= 9]
var left_mode : base.u8[..= 9]
var mode : base.u8[..= 9]
var i : base.u32
var top_mode : base.u8[..= 9]
var left_mode : base.u8[..= 9]
var mode : base.u8[..= 9]
if args.luma_mode < 4 {
this.mb_top_modes[args.mbx][0] = mode
this.mb_top_modes[args.mbx][1] = mode
this.mb_top_modes[args.mbx][2] = mode
this.mb_top_modes[args.mbx][3] = mode
this.mb_left_modes[0] = mode
this.mb_left_modes[1] = mode
this.mb_left_modes[2] = mode
this.mb_left_modes[3] = mode
this.mb_states_top[args.mbx] = (mode as base.u32) * 0x1111
this.mb_states_left = (mode as base.u32) * 0x1111
return nothing
}
i = 0
while i < 16 {
if i < 4 {
top_mode = this.mb_top_modes[args.mbx][i]
top_mode = CLIP_9[15 & (this.mb_states_top[args.mbx] >> (i * 4))]
} else {
top_mode = this.mb_subblock_modes[i - 4]
}
if (i & 3) == 0 {
left_mode = this.mb_left_modes[i >> 2]
left_mode = CLIP_9[15 & (this.mb_states_left >> (i & 12))]
} else {
left_mode = this.mb_subblock_modes[(i ~mod+ 15) & 15]
}
@@ -181,14 +182,16 @@ pri func decoder.decode_subblock_modes!(
i += 1
}
this.mb_top_modes[args.mbx][0] = this.mb_subblock_modes[0xC]
this.mb_top_modes[args.mbx][1] = this.mb_subblock_modes[0xD]
this.mb_top_modes[args.mbx][2] = this.mb_subblock_modes[0xE]
this.mb_top_modes[args.mbx][3] = this.mb_subblock_modes[0xF]
this.mb_left_modes[0] = this.mb_subblock_modes[0x3]
this.mb_left_modes[1] = this.mb_subblock_modes[0x7]
this.mb_left_modes[2] = this.mb_subblock_modes[0xB]
this.mb_left_modes[3] = this.mb_subblock_modes[0xF]
this.mb_states_top[args.mbx] = (this.mb_states_top[args.mbx] & 0xFFFF_0000) |
((this.mb_subblock_modes[0xC] as base.u32) << 0x0) |
((this.mb_subblock_modes[0xD] as base.u32) << 0x4) |
((this.mb_subblock_modes[0xE] as base.u32) << 0x8) |
((this.mb_subblock_modes[0xF] as base.u32) << 0xC)
this.mb_states_left = (this.mb_states_left & 0xFFFF_0000) |
((this.mb_subblock_modes[0x3] as base.u32) << 0x0) |
((this.mb_subblock_modes[0x7] as base.u32) << 0x4) |
((this.mb_subblock_modes[0xB] as base.u32) << 0x8) |
((this.mb_subblock_modes[0xF] as base.u32) << 0xC)
}
// RFC 6386 Section 11.3. Subblock Mode Contexts.
+292
View File
@@ -0,0 +1,292 @@
// Copyright 2026 The Wuffs Authors.
//
// Licensed under the Apache License, Version 2.0 <LICENSE-APACHE or
// https://www.apache.org/licenses/LICENSE-2.0> or the MIT license
// <LICENSE-MIT or https://opensource.org/licenses/MIT>, at your
// option. This file may not be copied, modified, or distributed
// except according to those terms.
//
// SPDX-License-Identifier: Apache-2.0 OR MIT
pri func decoder.decode_residuals!(
workbuf: slice base.u8,
mbx: base.u32[..= 0x3FF],
mby: base.u32[..= 0x3FF],
seg: base.u32[..= 3],
luma_mode: base.u32[..= 4],
chroma_mode: base.u32[..= 3]) {
var bc : base.u32
var by : base.u32
var bx : base.u32
var plane : base.u32[..= 3]
var lnz : base.u32[..= 1]
var tnz : base.u32[..= 1]
this.initialize_mb_coeffs!()
// Handle "intra16x16 prediction".
plane = 3 // Plane 3: Y beginning at coefficient 0.
if args.luma_mode < 4 {
lnz = 1 & (this.mb_states_left >> 23)
this.mb_states_left &= 0xFF7F_FFFF
tnz = 1 & (this.mb_states_top[args.mbx] >> 23)
this.mb_states_top[args.mbx] &= 0xFF7F_FFFF
lnz = this.decode_block_coefficients!(
workbuf: args.workbuf,
mby: args.mby,
seg: args.seg,
b: 24,
plane: 1, // Plane 1: Y2.
context: lnz + tnz)
this.mb_states_top[args.mbx] |= lnz << 23
this.mb_states_left |= lnz << 23
plane = 0 // Plane 0: Y beginning at coefficient 1.
}
// Parse Luma coefficients.
by = 0
while by < 4 {
lnz = 1 & (this.mb_states_left >> (24 + by))
this.mb_states_left &= 0xFFFF_FFFF ^ ((1 as base.u32) << (24 + by))
bx = 0
while bx < 4,
inv by < 4,
{
tnz = 1 & (this.mb_states_top[args.mbx] >> (24 + bx))
this.mb_states_top[args.mbx] &= 0xFFFF_FFFF ^ ((1 as base.u32) << (24 + bx))
lnz = this.decode_block_coefficients!(
workbuf: args.workbuf,
mby: args.mby,
seg: args.seg,
b: (4 * by) + bx,
plane: plane,
context: lnz + tnz)
this.mb_states_top[args.mbx] |= lnz << (24 + bx)
bx += 1
}
this.mb_states_left |= lnz << (24 + by)
by += 1
}
// Parse Chroma coefficients.
bc = 0
while bc <= 2 {
by = 0
while by < 2,
inv bc <= 2,
{
lnz = 1 & (this.mb_states_left >> (28 + bc + by))
this.mb_states_left &= 0xFFFF_FFFF ^ ((1 as base.u32) << (28 + bc + by))
bx = 0
while bx < 2,
inv bc <= 2,
inv by < 2,
{
tnz = 1 & (this.mb_states_top[args.mbx] >> (28 + bc + bx))
this.mb_states_top[args.mbx] &= 0xFFFF_FFFF ^ ((1 as base.u32) << (28 + bc + bx))
lnz = this.decode_block_coefficients!(
workbuf: args.workbuf,
mby: args.mby,
seg: args.seg,
b: (2 * bc) + (2 * by) + bx + 16,
plane: 2, // Plane 2: U or V.
context: lnz + tnz)
this.mb_states_top[args.mbx] |= lnz << (28 + bc + bx)
bx += 1
}
this.mb_states_left |= lnz << (28 + bc + by)
by += 1
}
bc += 2
}
}
pri func decoder.initialize_mb_coeffs!() {
var i : base.u32
var j : base.u32
i = 0
while i < 25 {
j = 0
while j < 16,
inv i < 25,
{
this.mb_coeffs[i][j] = 0
j += 1
}
i += 1
}
}
pri func decoder.decode_block_coefficients!(
workbuf: slice base.u8,
mby: base.u32[..= 0x3FF],
seg: base.u32[..= 3],
b: base.u32[..= 24],
plane: base.u32[..= 3],
context: base.u32[..= 2]) base.u32[..= 1] {
var q1 : base.u32[..= 2]
var q2 : base.u32[..= 1]
var quant : base.u32
var i : base.u32
var part : base.u32[..= 8]
var prob_base : base.u32[..= 1045]
var v1 : base.u32[..= 1]
var value : base.u32
q1 = 0
if args.b == 24 {
q1 = 1
} else if args.b >= 16 {
q1 = 2
}
i = 0
assert i <= 1
if args.plane == 0 {
i = 1
assert i <= 1
}
part = 1 + (args.mby & this.num_other_partitions_m1)
prob_base = (args.plane * (8 * 3 * 11)) + COEFF_BANDS_33[i] + (args.context * 11)
v1 = this.read_bit!(workbuf: args.workbuf, part: part,
prob: this.coeff_probs[prob_base + 0] as base.u32)
if v1 == 0 {
return 0
}
while true,
pre i < 16,
{
v1 = this.read_bit!(workbuf: args.workbuf, part: part,
prob: this.coeff_probs[prob_base + 1] as base.u32)
if v1 == 0 {
i += 1
if i >= 16 {
break
}
prob_base = (args.plane * (8 * 3 * 11)) + COEFF_BANDS_33[i + 0] + (0 * 11)
continue
}
v1 = this.read_bit!(workbuf: args.workbuf, part: part,
prob: this.coeff_probs[prob_base + 2] as base.u32)
if v1 == 0 {
value = 1
prob_base = (args.plane * (8 * 3 * 11)) + COEFF_BANDS_33[i + 1] + (1 * 11)
} else {
value = this.decode_large_value!(
workbuf: args.workbuf,
part: part,
prob_base: prob_base)
prob_base = (args.plane * (8 * 3 * 11)) + COEFF_BANDS_33[i + 1] + (2 * 11)
}
v1 = this.read_bit!(workbuf: args.workbuf, part: part, prob: 128)
if v1 <> 0 {
value = 0 ~mod- value
}
q2 = 0
if i <> 0 {
q2 = 1
}
quant = this.dequants[args.seg][q1][q2] as base.u32
this.mb_coeffs[args.b][ZIGZAG[i]] = ((value ~mod* quant) & 0xFFFF) as base.u16
if i >= 15 {
break
}
v1 = this.read_bit!(workbuf: args.workbuf, part: part,
prob: this.coeff_probs[prob_base + 0] as base.u32)
if v1 == 0 {
break
}
i += 1
}
return 1
}
pri func decoder.decode_large_value!(
workbuf: slice base.u8,
part: base.u32[..= 8],
prob_base: base.u32[..= 1045]) base.u32[2 ..= 2114] {
var v1 : base.u32[..= 1]
var w1 : base.u32[..= 1]
var category : base.u32[..= 3]
var value : base.u32
var i : base.u32[..= 15]
v1 = this.read_bit!(workbuf: args.workbuf, part: args.part,
prob: this.coeff_probs[args.prob_base + 3] as base.u32)
if v1 == 0 {
v1 = this.read_bit!(workbuf: args.workbuf, part: args.part,
prob: this.coeff_probs[args.prob_base + 4] as base.u32)
if v1 == 0 {
return 2
}
v1 = this.read_bit!(workbuf: args.workbuf, part: args.part,
prob: this.coeff_probs[args.prob_base + 5] as base.u32)
return 3 + v1
}
v1 = this.read_bit!(workbuf: args.workbuf, part: args.part,
prob: this.coeff_probs[args.prob_base + 6] as base.u32)
if v1 == 0 {
v1 = this.read_bit!(workbuf: args.workbuf, part: args.part,
prob: this.coeff_probs[args.prob_base + 7] as base.u32)
if v1 == 0 {
// Category 1.
v1 = this.read_bit!(workbuf: args.workbuf, part: args.part,
prob: 159)
return 5 + v1
}
// Category 2.
v1 = this.read_bit!(workbuf: args.workbuf, part: args.part,
prob: 165)
w1 = this.read_bit!(workbuf: args.workbuf, part: args.part,
prob: 145)
return 7 + (2 * v1) + w1
}
// Categories 3, 4, 5, 6.
v1 = this.read_bit!(workbuf: args.workbuf, part: args.part,
prob: this.coeff_probs[args.prob_base + 8] as base.u32)
w1 = this.read_bit!(workbuf: args.workbuf, part: args.part,
prob: this.coeff_probs[args.prob_base + 9 + v1] as base.u32)
category = (2 * v1) + w1
// Read 3, 4, 5 or 11 extra bits, depending on the category.
value = 0
i = 0
while true {
v1 = this.read_bit!(workbuf: args.workbuf, part: args.part,
prob: CATEGORY_PROBS[category][i] as base.u32)
value ~mod<<= 1
value ~mod+= v1
i = (i + 1) & 15
if CATEGORY_PROBS[category][i] == 0 {
break
}
}
return (value & 0x7FF) + ((8 as base.u32) << category) + 3
}
+25 -2
View File
@@ -88,13 +88,17 @@ pub struct decoder? implements base.image_decoder(
quant_uvdc_delta : base.u32,
quant_uvac_delta : base.u32,
// Look-up tables derived from the dequantization parameters.
//
// The first index is the segment. The second index is Y/Y2/UV. The
// third index is DC/AC.
dequants : array[4] array[3] array[2] base.u16,
// These 9 bits combine what RFC 6386 calls mb_no_skip_coeff (which we
// hold in the 0x100 bit) and prob_skip_false (in the low 8 bits).
prob_skip : base.u32[..= 0x1FF],
mb_subblock_modes : array[16] base.u8[..= 9],
mb_left_modes : array[4] base.u8[..= 9],
mb_top_modes : array[0x400] array[4] base.u8[..= 9],
dst_x : base.u32,
dst_y : base.u32,
@@ -104,6 +108,25 @@ pub struct decoder? implements base.image_decoder(
) + (
// 1056 is (4 planes, 8 bands, 3 contexts, 11 tokens) flattened.
coeff_probs : array[1056] base.u8,
mb_coeffs : array[25] array[16] base.u16,
// Macroblock states are a u32 whose bits are packed like this:
//
// - 28 ..= 31 are Chroma (UV) HNZC bits.
// - 24 ..= 27 are Luma4 (Y4) HNZC bits.
// - 23 is Luma16 (Y16) HNZC bit.
// - 16 ..= 22 are unused.
// - 12 ..= 15 are the 3rd predictor mode.
// - 8 ..= 12 are the 2nd predictor mode.
// - 4 ..= 7 are the 1st predictor mode.
// - 0 ..= 3 are the 0th predictor mode.
//
// HNZC stands for has-non-zero-coefficients.
//
// The mb_states_top array is indexed by mbx.
mb_states_left : base.u32,
mb_states_top : array[0x400] base.u32,
)
pub func decoder.get_quirk(key: base.u32) base.u64 {