forge
Explore
Log in

Don't you dare go Hollow

edith_codecs

Clone
★ 0

0 watching0 forksRust370.3 MiB00c1b3fTahinli2 months ago1999 commits

wedge.rs389 lines, 15.6 KiBHistoryRawDownload
1//! lane-wedge r3: `COMPOUND_WEDGE` mask codebook -- libaom
2//! `av1/common/reconinter.c`'s `init_wedge_master_masks`/`init_wedge_masks`/
3//! `get_wedge_mask_inplace`, ported for EVERY bsize libaom gives a wedge
4//! codebook (`av1_wedge_params_lookup`, reconinter.c:236-267): the three
5//! square sizes 8x8/16x16/32x32 (`wedge_codebook_16_heqw`) and the six
6//! rectangular ones 8x16/16x32/8x32 (`wedge_codebook_16_hgtw`, h > w) and
7//! 16x8/32x16/32x8 (`wedge_codebook_16_hltw`, h < w), each with its own
8//! `wedge_signflip_lookup` row (reconinter.c:160-183).
9//!
10//! lane-inter16ab r5: masks are stored PADDED to a `side x side` plane
11//! (`side = max(bw, bh)`, row stride `side`), because `decode.rs` predicts
12//! a rectangular strip into a square `side x side` buffer and writes only
13//! its true `write_w x write_h` footprint -- so a padded mask drops
14//! straight into the existing square blend call with no stride plumbing.
15//! Rows/columns outside `bw x bh` are zero and never reach the frame.
16//!
17//! VERIFICATION (class shared-oracle-blindness): `lanes/wedge_libaom.expected.txt`
18//! is generated by a C harness LINKED AGAINST libaom itself
19//! (`~/.cache/inter16ab-tmp/wedge_h.c`, `av1_init_wedge_masks()` then
20//! `av1_get_contiguous_soft_mask(idx, sign, bsize)`) -- a genuinely foreign
21//! implementation, not a transcription. Its 96 square-size lines are
22//! byte-identical to the older independent transcription
23//! `lanes/wedge_dump.expected.txt` (`lanes/wedge_dump.c`), so the two
24//! oracles agree where they overlap. `checksum()` below reproduces the
25//! harness's per-block checksum formula (true `bw x bh` window, padded
26//! stride skipped); `wedge_codebook_matches_libaom_dump` asserts all
27//! 9 x 2 x 16 = 288 entries.
28
29const MASK_MASTER_SIZE: usize = 64;
30const WEDGE_WEIGHT_BITS: u32 = 6;
31pub const MAX_WEDGE_TYPES: usize = 16;
32
33#[derive(Clone, Copy)]
34enum Dir {
35 Horizontal = 0,
36 Vertical = 1,
37 Oblique27 = 2,
38 Oblique63 = 3,
39 Oblique117 = 4,
40 Oblique153 = 5,
41}
42const N_DIR: usize = 6;
43
44const WEDGE_MASTER_OBLIQUE_ODD: [u8; MASK_MASTER_SIZE] = [
45 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 1, 2, 6,
46 18, 37, 53, 60, 63, 64, 64, 64, 64, 64, 64, 64, 64, 64, 64, 64, 64, 64, 64, 64, 64, 64, 64,
47 64, 64, 64, 64, 64, 64, 64, 64, 64, 64,
48];
49const WEDGE_MASTER_OBLIQUE_EVEN: [u8; MASK_MASTER_SIZE] = [
50 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 1, 4, 11,
51 27, 46, 58, 62, 63, 64, 64, 64, 64, 64, 64, 64, 64, 64, 64, 64, 64, 64, 64, 64, 64, 64, 64,
52 64, 64, 64, 64, 64, 64, 64, 64, 64, 64,
53];
54const WEDGE_MASTER_VERTICAL: [u8; MASK_MASTER_SIZE] = [
55 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 2, 7,
56 21, 43, 57, 62, 64, 64, 64, 64, 64, 64, 64, 64, 64, 64, 64, 64, 64, 64, 64, 64, 64, 64, 64,
57 64, 64, 64, 64, 64, 64, 64, 64, 64, 64,
58];
59
60/// `wedge_codebook_16_heqw` (reconinter.c:225) -- (direction, x_offset,
61/// y_offset); the codebook for the square sizes (h == w).
62const HEQW: [(Dir, i32, i32); MAX_WEDGE_TYPES] = [
63 (Dir::Oblique27, 4, 4),
64 (Dir::Oblique63, 4, 4),
65 (Dir::Oblique117, 4, 4),
66 (Dir::Oblique153, 4, 4),
67 (Dir::Horizontal, 4, 2),
68 (Dir::Horizontal, 4, 6),
69 (Dir::Vertical, 2, 4),
70 (Dir::Vertical, 6, 4),
71 (Dir::Oblique27, 4, 2),
72 (Dir::Oblique27, 4, 6),
73 (Dir::Oblique153, 4, 2),
74 (Dir::Oblique153, 4, 6),
75 (Dir::Oblique63, 2, 4),
76 (Dir::Oblique63, 6, 4),
77 (Dir::Oblique117, 2, 4),
78 (Dir::Oblique117, 6, 4),
79];
80/// `wedge_codebook_16_hgtw` (reconinter.c:203) -- h > w (8x16/16x32/8x32).
81/// Differs from `heqw` only in entries 4..7: three HORIZONTAL splits plus a
82/// single centred VERTICAL one.
83const HGTW: [(Dir, i32, i32); MAX_WEDGE_TYPES] = [
84 (Dir::Oblique27, 4, 4),
85 (Dir::Oblique63, 4, 4),
86 (Dir::Oblique117, 4, 4),
87 (Dir::Oblique153, 4, 4),
88 (Dir::Horizontal, 4, 2),
89 (Dir::Horizontal, 4, 4),
90 (Dir::Horizontal, 4, 6),
91 (Dir::Vertical, 4, 4),
92 (Dir::Oblique27, 4, 2),
93 (Dir::Oblique27, 4, 6),
94 (Dir::Oblique153, 4, 2),
95 (Dir::Oblique153, 4, 6),
96 (Dir::Oblique63, 2, 4),
97 (Dir::Oblique63, 6, 4),
98 (Dir::Oblique117, 2, 4),
99 (Dir::Oblique117, 6, 4),
100];
101/// `wedge_codebook_16_hltw` (reconinter.c:214) -- h < w (16x8/32x16/32x8):
102/// the transpose of `hgtw`'s 4..7 (three VERTICAL splits, one centred
103/// HORIZONTAL).
104const HLTW: [(Dir, i32, i32); MAX_WEDGE_TYPES] = [
105 (Dir::Oblique27, 4, 4),
106 (Dir::Oblique63, 4, 4),
107 (Dir::Oblique117, 4, 4),
108 (Dir::Oblique153, 4, 4),
109 (Dir::Vertical, 2, 4),
110 (Dir::Vertical, 4, 4),
111 (Dir::Vertical, 6, 4),
112 (Dir::Horizontal, 4, 4),
113 (Dir::Oblique27, 4, 2),
114 (Dir::Oblique27, 4, 6),
115 (Dir::Oblique153, 4, 2),
116 (Dir::Oblique153, 4, 6),
117 (Dir::Oblique63, 2, 4),
118 (Dir::Oblique63, 6, 4),
119 (Dir::Oblique117, 2, 4),
120 (Dir::Oblique117, 6, 4),
121];
122
123/// `wedge_signflip_lookup` (reconinter.c:160-183), one row per bsize.
124/// `BLOCK_8X8`/`16X16`/`32X32` share this row.
125const SF_SQUARE: [u8; MAX_WEDGE_TYPES] = [1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 0, 1, 1, 1, 0, 1];
126/// `BLOCK_8X16`/`16X8`/`16X32`/`32X16` share this row (index 4 flips to 0).
127const SF_RECT: [u8; MAX_WEDGE_TYPES] = [1, 1, 1, 1, 0, 1, 1, 1, 1, 1, 0, 1, 1, 1, 0, 1];
128/// `BLOCK_8X32` (reconinter.c:173) -- index 8 also 0.
129const SF_8X32: [u8; MAX_WEDGE_TYPES] = [1, 1, 1, 1, 0, 1, 1, 1, 0, 1, 0, 1, 1, 1, 0, 1];
130/// `BLOCK_32X8` (reconinter.c:174) -- index 12 also 0.
131const SF_32X8: [u8; MAX_WEDGE_TYPES] = [1, 1, 1, 1, 0, 1, 1, 1, 1, 1, 0, 1, 0, 1, 0, 1];
132
133/// `av1_wedge_params_lookup[bsize]`'s `(codebook, signflip)` pair for a
134/// block footprint, `None` where `wedge_types == 0`.
135fn wedge_params(bw: usize, bh: usize) -> Option<(&'static [(Dir, i32, i32); MAX_WEDGE_TYPES], &'static [u8; MAX_WEDGE_TYPES])> {
136 Some(match (bw, bh) {
137 (8, 8) | (16, 16) | (32, 32) => (&HEQW, &SF_SQUARE),
138 (8, 16) | (16, 32) => (&HGTW, &SF_RECT),
139 (16, 8) | (32, 16) => (&HLTW, &SF_RECT),
140 (8, 32) => (&HGTW, &SF_8X32),
141 (32, 8) => (&HLTW, &SF_32X8),
142 _ => return None,
143 })
144}
145
146/// Every `(bw, bh)` with a wedge codebook, in `av1_wedge_params_lookup` order.
147pub const WEDGE_BSIZES: [(usize, usize); 9] = [
148 (8, 8),
149 (8, 16),
150 (16, 8),
151 (16, 16),
152 (16, 32),
153 (32, 16),
154 (32, 32),
155 (8, 32),
156 (32, 8),
157];
158
159/// `shift_copy` (reconinter.c) -- shift a `MASK_MASTER_SIZE`-wide row right
160/// (`shift >= 0`) or left, replicating the edge sample into the gap.
161fn shift_copy(src: &[u8; MASK_MASTER_SIZE], dst: &mut [u8], shift: i32) {
162 let w = MASK_MASTER_SIZE;
163 if shift >= 0 {
164 let shift = shift as usize;
165 dst[shift..w].copy_from_slice(&src[..w - shift]);
166 dst[..shift].fill(src[0]);
167 } else {
168 let shift = (-shift) as usize;
169 dst[..w - shift].copy_from_slice(&src[shift..]);
170 dst[w - shift..].fill(src[w - 1]);
171 }
172}
173
174/// `wedge_mask_obl[neg][direction]`, a flat `MASK_MASTER_SIZE`-square plane
175/// per (neg, direction). Built once by [`init_wedge_master_masks`].
176struct MasterMasks {
177 /// `[neg][direction] -> MASK_MASTER_SIZE * MASK_MASTER_SIZE` bytes.
178 obl: [[Vec<u8>; N_DIR]; 2],
179}
180
181/// `init_wedge_master_masks` (reconinter.c) ported verbatim.
182fn init_wedge_master_masks() -> MasterMasks {
183 let w = MASK_MASTER_SIZE;
184 let stride = MASK_MASTER_SIZE;
185 let mut obl: [[Vec<u8>; N_DIR]; 2] = Default::default();
186 for neg in 0..2 {
187 for d in 0..N_DIR {
188 obl[neg][d] = vec![0u8; w * w];
189 }
190 }
191 let mut shift = (w / 4) as i32;
192 for i in (0..w).step_by(2) {
193 {
194 let row_start = i * stride;
195 shift_copy(
196 &WEDGE_MASTER_OBLIQUE_EVEN,
197 &mut obl[0][Dir::Oblique63 as usize][row_start..row_start + w],
198 shift,
199 );
200 }
201 shift -= 1;
202 {
203 let row_start = (i + 1) * stride;
204 shift_copy(
205 &WEDGE_MASTER_OBLIQUE_ODD,
206 &mut obl[0][Dir::Oblique63 as usize][row_start..row_start + w],
207 shift,
208 );
209 }
210 obl[0][Dir::Vertical as usize][i * stride..i * stride + w]
211 .copy_from_slice(&WEDGE_MASTER_VERTICAL);
212 obl[0][Dir::Vertical as usize][(i + 1) * stride..(i + 1) * stride + w]
213 .copy_from_slice(&WEDGE_MASTER_VERTICAL);
214 }
215 for i in 0..w {
216 for j in 0..w {
217 let msk = obl[0][Dir::Oblique63 as usize][i * stride + j] as i32;
218 obl[0][Dir::Oblique27 as usize][j * stride + i] = msk as u8;
219 let flip = ((1i32 << WEDGE_WEIGHT_BITS) - msk) as u8;
220 obl[0][Dir::Oblique117 as usize][i * stride + w - 1 - j] = flip;
221 obl[0][Dir::Oblique153 as usize][(w - 1 - j) * stride + i] = flip;
222 obl[1][Dir::Oblique63 as usize][i * stride + j] = flip;
223 obl[1][Dir::Oblique27 as usize][j * stride + i] = flip;
224 obl[1][Dir::Oblique117 as usize][i * stride + w - 1 - j] = msk as u8;
225 obl[1][Dir::Oblique153 as usize][(w - 1 - j) * stride + i] = msk as u8;
226 let mskx = obl[0][Dir::Vertical as usize][i * stride + j] as i32;
227 obl[0][Dir::Horizontal as usize][j * stride + i] = mskx as u8;
228 let flipx = ((1i32 << WEDGE_WEIGHT_BITS) - mskx) as u8;
229 obl[1][Dir::Vertical as usize][i * stride + j] = flipx;
230 obl[1][Dir::Horizontal as usize][j * stride + i] = flipx;
231 }
232 }
233 MasterMasks { obl }
234}
235
236/// `get_wedge_mask_inplace` (reconinter.c:270-289): the `bw x bh` window of
237/// the master plane for this `(wedge_index, neg, bw, bh)`, still in
238/// `MASK_MASTER_SIZE`-stride coordinates (caller extracts the window).
239fn wedge_window(wedge_index: usize, neg: usize, bw: usize, bh: usize) -> (usize, usize, usize) {
240 let (codebook, signflip) = wedge_params(bw, bh).expect("wedge_window on a wedge-capable bsize");
241 let (dir, xo, yo) = codebook[wedge_index];
242 let wsignflip = signflip[wedge_index] as usize;
243 let woff = (xo as usize * bw) >> 3;
244 let hoff = (yo as usize * bh) >> 3;
245 let row0 = MASK_MASTER_SIZE / 2 - hoff;
246 let col0 = MASK_MASTER_SIZE / 2 - woff;
247 (neg ^ wsignflip, dir as usize, row0 * MASK_MASTER_SIZE + col0)
248}
249
250/// Per-`(sign, wedge_index)` mask for one bsize, stored PADDED into a
251/// `stride x stride` plane (`stride = max(bw, bh)`) so a rectangular mask
252/// indexes exactly like the square prediction buffer `decode.rs` blends
253/// into. Only rows `0..bh` / columns `0..bw` are meaningful.
254pub struct WedgeCodebook {
255 /// `[sign][wedge_index] -> stride*stride` bytes, row stride `stride`.
256 masks: [[Vec<u8>; MAX_WEDGE_TYPES]; 2],
257 pub bw: usize,
258 pub bh: usize,
259 /// `max(bw, bh)` -- the row stride of every mask returned by [`Self::mask`].
260 pub stride: usize,
261}
262
263impl WedgeCodebook {
264 fn build(master: &MasterMasks, bw: usize, bh: usize) -> Self {
265 let stride = bw.max(bh);
266 let mut masks: [[Vec<u8>; MAX_WEDGE_TYPES]; 2] = Default::default();
267 for sign in 0..2 {
268 for idx in 0..MAX_WEDGE_TYPES {
269 let (neg_idx, dir, off) = wedge_window(idx, sign, bw, bh);
270 let plane = &master.obl[neg_idx][dir];
271 let mut m = vec![0u8; stride * stride];
272 for i in 0..bh {
273 let src = off + i * MASK_MASTER_SIZE;
274 m[i * stride..i * stride + bw].copy_from_slice(&plane[src..src + bw]);
275 }
276 masks[sign][idx] = m;
277 }
278 }
279 WedgeCodebook { masks, bw, bh, stride }
280 }
281
282 /// `wedge_sign` in `{0,1}` (spec `wedge_sign`), `wedge_index` in
283 /// `0..MAX_WEDGE_TYPES`. Mask is luma-resolution, row stride
284 /// [`Self::stride`] (`max(bw, bh)`), length `stride * stride`.
285 pub fn mask(&self, wedge_sign: usize, wedge_index: usize) -> &[u8] {
286 &self.masks[wedge_sign][wedge_index]
287 }
288}
289
290/// One codebook per wedge-capable bsize, built once.
291pub struct WedgeMasks {
292 /// Parallel to [`WEDGE_BSIZES`].
293 books: Vec<WedgeCodebook>,
294}
295
296impl WedgeMasks {
297 fn new() -> Self {
298 let master = init_wedge_master_masks();
299 WedgeMasks {
300 books: WEDGE_BSIZES
301 .iter()
302 .map(|&(bw, bh)| WedgeCodebook::build(&master, bw, bh))
303 .collect(),
304 }
305 }
306
307 /// `(bw, bh)` is the block's TRUE footprint; panics on a size libaom
308 /// gives no codebook (the same invariant `decode.rs`'s `wedge_used_bsize`
309 /// already enforces before any wedge symbol is read).
310 pub fn codebook(&self, bw: usize, bh: usize) -> &WedgeCodebook {
311 let i = WEDGE_BSIZES
312 .iter()
313 .position(|&s| s == (bw, bh))
314 .unwrap_or_else(|| unreachable!("no wedge codebook for {bw}x{bh}"));
315 &self.books[i]
316 }
317}
318
319static WEDGE_MASKS: std::sync::OnceLock<WedgeMasks> = std::sync::OnceLock::new();
320
321pub fn wedge_masks() -> &'static WedgeMasks {
322 WEDGE_MASKS.get_or_init(WedgeMasks::new)
323}
324
325/// Reproduces the libaom-linked harness's per-block checksum formula
326/// (`~/.cache/inter16ab-tmp/wedge_h.c`): the TRUE `bw x bh` window read out
327/// of the padded plane, weighted by its contiguous position + 1.
328#[cfg(test)]
329fn checksum(mask: &[u8], bw: usize, bh: usize, stride: usize) -> u64 {
330 let mut sum: u64 = 0;
331 for i in 0..bh {
332 for j in 0..bw {
333 sum += mask[i * stride + j] as u64 * (i * bw + j + 1) as u64;
334 }
335 }
336 sum
337}
338
339#[cfg(test)]
340mod tests {
341 use super::*;
342
343 /// lane-inter16ab r5 (class enumerate-table-domain): checksum-verify
344 /// EVERY (bsize, sign, wedge_index) mask this decoder can produce --
345 /// all 9 wedge-capable bsizes, both signs, all 16 indices = 288 entries
346 /// -- against `av1_get_contiguous_soft_mask()` read straight out of
347 /// libaom by the linked C harness (`lanes/wedge_libaom.expected.txt`,
348 /// regenerate with `gcc -O2 -o wedge_h wedge_h.c -I <aom-src> -I <aom-build>
349 /// <aom-build>/libaom.a -lm -lpthread && ./wedge_h`).
350 #[test]
351 fn wedge_codebook_matches_libaom_dump() {
352 let expected_txt = std::fs::read_to_string(concat!(
353 env!("CARGO_MANIFEST_DIR"),
354 "/../../lanes/wedge_libaom.expected.txt"
355 ))
356 .expect("lanes/wedge_libaom.expected.txt (libaom-linked harness dump)");
357 let mut expected = std::collections::HashMap::new();
358 for line in expected_txt.lines() {
359 // "16x8 sign=0 idx=0 checksum=39141"
360 let parts: Vec<&str> = line.split_whitespace().collect();
361 let name = parts[0].to_string();
362 let sign: usize = parts[1]["sign=".len()..].parse().unwrap();
363 let idx: usize = parts[2]["idx=".len()..].parse().unwrap();
364 let cs: u64 = parts[3]["checksum=".len()..].parse().unwrap();
365 expected.insert((name, sign, idx), cs);
366 }
367 assert_eq!(expected.len(), 9 * 2 * MAX_WEDGE_TYPES, "the dump covers every entry");
368 let masks = wedge_masks();
369 let mut checked = 0;
370 for (bw, bh) in WEDGE_BSIZES {
371 let name = format!("{bw}x{bh}");
372 let cb = masks.codebook(bw, bh);
373 for sign in 0..2 {
374 for idx in 0..MAX_WEDGE_TYPES {
375 let got = checksum(cb.mask(sign, idx), bw, bh, cb.stride);
376 let want = *expected.get(&(name.clone(), sign, idx)).unwrap_or_else(|| {
377 panic!("missing expected entry for {name} sign={sign} idx={idx}")
378 });
379 assert_eq!(
380 got, want,
381 "{name} sign={sign} idx={idx}: rust checksum {got} != libaom {want}"
382 );
383 checked += 1;
384 }
385 }
386 }
387 assert_eq!(checked, 9 * 2 * MAX_WEDGE_TYPES, "checked every (bsize,sign,index) triple");
388 }
389}