std/vp8: add comments about reconstruction etc

This commit is contained in:
Nigel Tao committed 2026-07-09 17:08:58 +10:00
1 parent 46ac36bd7b
commit 3fce6225c9
8 files changed
+139 -12

No files matched your search

+6 -6
View File
@@ -81873,7 +81873,7 @@ wuffs_vp8__decoder__initialize_mb_coeffs(
WUFFS_BASE__GENERATED_C_CODE
static uint32_t
wuffs_vp8__decoder__decode_block_coefficients(
wuffs_vp8__decoder__decode_subblock_coefficients(
wuffs_vp8__decoder* self,
wuffs_base__slice_u8 a_workbuf,
uint32_t a_mby,
@@ -82376,7 +82376,7 @@ wuffs_vp8__decoder__decode_coefficients(
self->private_data.f_mb_states_left &= 4286578687u;
v_tnz = (1u & (self->private_data.f_mb_states_top[a_mbx] >> 23u));
self->private_data.f_mb_states_top[a_mbx] &= 4286578687u;
v_lnz = wuffs_vp8__decoder__decode_block_coefficients(self,
v_lnz = wuffs_vp8__decoder__decode_subblock_coefficients(self,
a_workbuf,
a_mby,
a_seg,
@@ -82397,7 +82397,7 @@ wuffs_vp8__decoder__decode_coefficients(
v_tnz = (1u & (self->private_data.f_mb_states_top[a_mbx] >> (24u + v_bx)));
self->private_data.f_mb_states_top[a_mbx] &= (4294967295u ^ (((uint32_t)(1u)) << (24u + v_bx)));
v_b = ((4u * v_by) + v_bx);
v_lnz = wuffs_vp8__decoder__decode_block_coefficients(self,
v_lnz = wuffs_vp8__decoder__decode_subblock_coefficients(self,
a_workbuf,
a_mby,
a_seg,
@@ -82430,7 +82430,7 @@ wuffs_vp8__decoder__decode_coefficients(
(2u * v_by) +
v_bx +
16u);
v_lnz = wuffs_vp8__decoder__decode_block_coefficients(self,
v_lnz = wuffs_vp8__decoder__decode_subblock_coefficients(self,
a_workbuf,
a_mby,
a_seg,
@@ -82478,11 +82478,11 @@ wuffs_vp8__decoder__initialize_mb_coeffs(
return wuffs_base__make_empty_struct();
}
// -------- func vp8.decoder.decode_block_coefficients
// -------- func vp8.decoder.decode_subblock_coefficients
WUFFS_BASE__GENERATED_C_CODE
static uint32_t
wuffs_vp8__decoder__decode_block_coefficients(
wuffs_vp8__decoder__decode_subblock_coefficients(
wuffs_vp8__decoder* self,
wuffs_base__slice_u8 a_workbuf,
uint32_t a_mby,
+4
View File
@@ -8,6 +8,10 @@
//
// SPDX-License-Identifier: Apache-2.0 OR MIT
// --------
// RFC 6386 Section 7. Boolean Entropy Decoder.
pri func decoder.read_bit!(workbuf: roslice base.u8, part: base.u32[..= 8], prob: base.u32[..= 0xFF]) base.u32[..= 1] {
var p : base.u32[..= 8]
var c8 : base.u8
+12 -4
View File
@@ -8,6 +8,14 @@
//
// SPDX-License-Identifier: Apache-2.0 OR MIT
// --------
// RFC 6386 Section 13. DCT Coefficient Decoding.
//
// and
//
// RFC 6386 Section 13.2. Coding of Individual Coefficient Values.
pri func decoder.decode_coefficients!(
workbuf: slice base.u8,
mbx: base.u32[..= 0x3FF],
@@ -36,7 +44,7 @@ pri func decoder.decode_coefficients!(
tnz = 1 & (this.mb_states_top[args.mbx] >> 23)
this.mb_states_top[args.mbx] &= 0xFF7F_FFFF
lnz = this.decode_block_coefficients!(
lnz = this.decode_subblock_coefficients!(
workbuf: args.workbuf,
mby: args.mby,
seg: args.seg,
@@ -67,7 +75,7 @@ pri func decoder.decode_coefficients!(
b = (4 * by) + bx
lnz = this.decode_block_coefficients!(
lnz = this.decode_subblock_coefficients!(
workbuf: args.workbuf,
mby: args.mby,
seg: args.seg,
@@ -110,7 +118,7 @@ pri func decoder.decode_coefficients!(
this.mb_states_top[args.mbx] &= 0xFFFF_FFFF ^ ((1 as base.u32) << (28 + bc + bx))
b = (2 * bc) + (2 * by) + bx + 16
lnz = this.decode_block_coefficients!(
lnz = this.decode_subblock_coefficients!(
workbuf: args.workbuf,
mby: args.mby,
seg: args.seg,
@@ -158,7 +166,7 @@ pri func decoder.initialize_mb_coeffs!() {
}
}
pri func decoder.decode_block_coefficients!(
pri func decoder.decode_subblock_coefficients!(
workbuf: slice base.u8,
mby: base.u32[..= 0x3FF],
seg: base.u32[..= 3],
+13 -1
View File
@@ -8,6 +8,10 @@
//
// SPDX-License-Identifier: Apache-2.0 OR MIT
// --------
// A simplified and optimized version of decoder.inverse_dct_full when the AC
// coefficients are all zero.
pri func decoder.inverse_dct_dc_only!(
cachex: base.u32[..= 28],
cachey: base.u32[..= 22],
@@ -25,6 +29,9 @@ pri func decoder.inverse_dct_dc_only!(
}
}
// RFC 6386 Section 14.4. Implementation of the DCT Inversion.
//
// It reads from mb_coeffs and yuv_cache and writes to yuv_cache.
pri func decoder.inverse_dct_full!(
cachex: base.u32[..= 28],
cachey: base.u32[..= 22],
@@ -119,8 +126,10 @@ pri func decoder.inverse_dct_full!(
var q32 : base.u32
var q33 : base.u32
// 85627 is approximately 65536 * cos(pi/8) * sqrt(2).
// 35468 is approximately 65536 * sin(pi/8) * sqrt(2).
//
// 85627 is approximately 65536 * cos(pi/8) * sqrt(2). Compared to the RFC
// implementation 85627 is also (20091 + 65536).
// --------
@@ -299,6 +308,9 @@ pri func decoder.inverse_dct_full!(
this.yuv_cache[args.cachey + 3][args.cachex + 3] as base.u32) ~mod+ q33)]
}
// RFC 6386 Section 14.3. Implementation of the WHT Inversion.
//
// It reads from and writes to mb_coeffs.
pri func decoder.inverse_wht!() {
var a00 : base.u32
var a01 : base.u32
+5
View File
@@ -28,6 +28,8 @@ pri func decoder.decode_macroblocks!(dst: ptr base.pixel_buffer, workbuf: slice
this.mb_states_left = 0
// Decode this row, using this.mb_coeffs and this.yuv_cache before
// ultimately writing to args.workbuf.
mbx = 0
while mbx < this.mbw,
inv mby < 0x400,
@@ -39,6 +41,7 @@ pri func decoder.decode_macroblocks!(dst: ptr base.pixel_buffer, workbuf: slice
mbx += 1
}
// Filter the previous row, already in args.workbuf.
if mby > 0 {
if this.filt_level <= 0 {
// No-op.
@@ -52,6 +55,7 @@ pri func decoder.decode_macroblocks!(dst: ptr base.pixel_buffer, workbuf: slice
mby += 1
}
// Filter the final row, already in args.workbuf, and swizzle to args.dst.
if this.mbh > 0 {
if this.filt_level <= 0 {
// No-op.
@@ -79,6 +83,7 @@ pri func decoder.decode_one_macroblock!(workbuf: slice base.u8, mbx: base.u32[..
var luma_mode : base.u32[..= 4]
var chroma_mode : base.u32[..= 3]
// RFC 6386 Section 10. Segment-Based Feature Adjustments.
seg = 0
if this.seg_update_map {
v1 = this.read_bit!(workbuf: args.workbuf, part: 0, prob: this.seg_probs[0] as base.u32)
+7 -1
View File
@@ -10,7 +10,13 @@
// --------
// Predicts a 4×4 luma block, the Xs below, based on samples top and left.
// RFC 6386 Section 12. Intraframe Prediction.
//
// These methods read from and write to yuv_cache.
// --------
// Predicts a 4×4 luma subblock, the Xs below, based on samples top and left.
//
// a b c d e f g h i
// p X X X X
+68
View File
@@ -8,6 +8,66 @@
//
// SPDX-License-Identifier: Apache-2.0 OR MIT
// --------
// Reconstructing YUV samples involves adding predictions (based on previous
// YUV samples, in macroblocks one column left or one column above) to
// residuals (decoded from the bitstream, dequantized and IDCT'ed).
//
// There are 16×16 = 256 luma and 2×8×8 = 128 chroma samples per macroblock.
// The decoder.yuv_cache field is a bit larger than that, being an array[26]
// array[32] base.u8. Here's the layout.
//
// 0 1 2 3 4 5 6 7 8 9 A B C D E F 0 1 2 3 4 5 6 7 8 9 A B C D E F
// . . . . . . . a b b b b b b b b b b b b b b b b c c c c . . . . 00
// . . . . . . . d Y Y Y Y Y Y Y Y Y Y Y Y Y Y Y Y . . . . . . . . 01
// . . . . . . . d Y Y Y Y Y Y Y Y Y Y Y Y Y Y Y Y . . . . . . . . 02
// . . . . . . . d Y Y Y Y Y Y Y Y Y Y Y Y Y Y Y Y . . . . . . . . 03
// . . . . . . . d Y Y Y Y Y Y Y Y Y Y Y Y Y Y Y Y c c c c . . . . 04
// . . . . . . . d Y Y Y Y Y Y Y Y Y Y Y Y Y Y Y Y . . . . . . . . 05
// . . . . . . . d Y Y Y Y Y Y Y Y Y Y Y Y Y Y Y Y . . . . . . . . 06
// . . . . . . . d Y Y Y Y Y Y Y Y Y Y Y Y Y Y Y Y . . . . . . . . 07
// . . . . . . . d Y Y Y Y Y Y Y Y Y Y Y Y Y Y Y Y c c c c . . . . 08
// . . . . . . . d Y Y Y Y Y Y Y Y Y Y Y Y Y Y Y Y . . . . . . . . 09
// . . . . . . . d Y Y Y Y Y Y Y Y Y Y Y Y Y Y Y Y . . . . . . . . 0A
// . . . . . . . d Y Y Y Y Y Y Y Y Y Y Y Y Y Y Y Y . . . . . . . . 0B
// . . . . . . . d Y Y Y Y Y Y Y Y Y Y Y Y Y Y Y Y c c c c . . . . 0C
// . . . . . . . d Y Y Y Y Y Y Y Y Y Y Y Y Y Y Y Y . . . . . . . . 0D
// . . . . . . . d Y Y Y Y Y Y Y Y Y Y Y Y Y Y Y Y . . . . . . . . 0E
// . . . . . . . d Y Y Y Y Y Y Y Y Y Y Y Y Y Y Y Y . . . . . . . . 0F
// . . . . . . . d Y Y Y Y Y Y Y Y Y Y Y Y Y Y Y Y . . . . . . . . 10
// . . . . . . . e f f f f f f f f . . . . . . . g h h h h h h h h 11
// . . . . . . . i U U U U U U U U . . . . . . . j V V V V V V V V 12
// . . . . . . . i U U U U U U U U . . . . . . . j V V V V V V V V 13
// . . . . . . . i U U U U U U U U . . . . . . . j V V V V V V V V 14
// . . . . . . . i U U U U U U U U . . . . . . . j V V V V V V V V 15
// . . . . . . . i U U U U U U U U . . . . . . . j V V V V V V V V 16
// . . . . . . . i U U U U U U U U . . . . . . . j V V V V V V V V 17
// . . . . . . . i U U U U U U U U . . . . . . . j V V V V V V V V 18
// . . . . . . . i U U U U U U U U . . . . . . . j V V V V V V V V 19
//
// Y, U and V are this macroblock's luma and chroma values.
//
// The Y predictions (per 4×4 subblock) depend on the row above's Y values
// (either abc or dYc) and the column left's Y values (ad or bY).
//
// The U predictions (per 8×8 block) depend on the row above's U values (ef)
// and the column left's U values (i).
//
// The V predictions (per 8×8 block) depend on the row above's V values (gh)
// and the column left's V values (j).
//
// For top-row (mby == 0) macroblocks, abcefgh is initialized to 0x7F. For
// other macroblocks, these are copied from the bottom-most row of the
// macroblock above (before filtering, for I-Frames), from the workbuf.
//
// The c values are then duplicated from row 0x00 to rows 0x04, 0x08 and 0x0C.
//
// For left-column (mbx == 0) macroblocks, adeigj are initialized to 0x81 (but
// aeg is 0x7F if mby is also 0). For other macroblocks, these are copied from
// the right-most column of the macroblock to the left (before filtering, for
// I-Frames), from the yuv_cache to itself, shifted by 8 or 16 columns.
pri func decoder.reconstruct!(
workbuf: slice base.u8,
mbx: base.u32[..= 0x3FF],
@@ -120,7 +180,10 @@ pri func decoder.prepare_yuv_cache!(
var offset : base.u64[..= 0x17FE_FFF8]
var o : base.u64
// For abcdefghij comments, see the layout at the top of this file.
if args.mbx <= 0 {
// Initialize adeigj to 0x81.
j = 0x00
while j < 0x11 {
this.yuv_cache[j][0x07] = 0x81
@@ -133,6 +196,7 @@ pri func decoder.prepare_yuv_cache!(
}
} else {
// Copy from the previous (left) macroblock, in yuv_cache, to adeigj.
j = 0x00
while j < 0x11 {
this.yuv_cache[j][0x07] = this.yuv_cache[j][0x17]
@@ -146,6 +210,7 @@ pri func decoder.prepare_yuv_cache!(
}
if args.mby <= 0 {
// Initialize abcefgh to 0x7F.
i = 0x00
while i < 0x20 {
this.yuv_cache[0x00][i] = 0x7F
@@ -158,6 +223,8 @@ pri func decoder.prepare_yuv_cache!(
}
} else {
// Copy from the previous (above) macroblock, in args.workbuf, to bcfh.
offset = 0 +
(((((args.mby * 16) - 1) * this.workbuf_yuv_y_stride) + (args.mbx * 16)) as base.u64)
i = 0x00
@@ -220,6 +287,7 @@ pri func decoder.prepare_yuv_cache!(
}
}
// Duplicate the c values.
j = 0x04
while j < 0x10 {
this.yuv_cache[j][0x18] = this.yuv_cache[0][0x18]
+24
View File
@@ -33,6 +33,8 @@ pub struct decoder? implements base.image_decoder(
frame_config_io_position : base.u64,
// This uses the same representation as an wuffs_base__optional_u63:
// ((value_as_u63 << 1) | has_value_as_u1).
quirk_source_length : base.u64,
// Just under 144 MiB, for 9 partitions each up to 0xFF_FFFF bytes.
@@ -119,6 +121,7 @@ pub struct decoder? implements base.image_decoder(
// hold in the 0x100 bit) and prob_skip_false (in the low 8 bits).
prob_skip : base.u32[..= 0x1FF],
// 4×4 luma subblock prediction modes.
mb_subblock_modes : array[16] base.u8[..= 9],
// Two u32 values whose bits are packed like this:
@@ -132,6 +135,13 @@ pub struct decoder? implements base.image_decoder(
mb_dc_nz : base.u32,
mb_ac_nz : base.u32,
// The workspace for the reconstruction-in-progress macroblock,
// containing the predicted and then fully decoded (prediction plus
// post-IDCT residuals) YUV samples to copy out to the workbuf. Its
// edges also hold samples from previous (left and top) macroblocks
// that are used in calculating this macroblock's predictions.
//
// See the comments near decoder.reconstruct for the layout.
yuv_cache : array[26] array[32] base.u8,
swizzler : base.pixel_swizzler,
@@ -142,6 +152,20 @@ pub struct decoder? implements base.image_decoder(
// 1056 is (4 planes, 8 bands, 3 contexts, 11 tokens) flattened.
coeff_probs : array[1056] base.u8,
// Macroblock coefficients. Per this.decode_one_macroblock:
//
// this.decode_coefficients decodes these macroblock coefficients from
// the bitstream and dequantizes them, writing them to this.mb_coeffs.
// At this point, these are pre-IDCT residuals.
//
// this.reconstruct calculates the predictions (writing them to
// this.yuv_cache, not this.mb_coeffs) and then applies the inverse WHT
// (if applicable) and the inverse DCT to the pre-IDCT residuals, by
// calling this.inverse_dct_etc.
//
// this.inverse_dct_etc reads the pre-IDCT residuals from
// this.mb_coeffs and writes fully decoded samples (predictions plus
// post-IDCT residuals) to this.yuv_cache.
mb_coeffs : array[25] array[16] base.u16,
// Macroblock states are a u32 whose bits are packed like this: