Merge branch 'lane-hbdinter': 10-bit loop restoration (SGR box sums scaled by bd-8, wiener clamp at the bd bound), combine_compound two-step rounding; 10-bit inter+LR gate (verifier PASS, 271/0/23); restoration leaves NEVER_EXERCISED_10BIT
Browse files at this commit6 changed files +397 -24
- crates/ec-av1/src/decode.rs +6 -0
- crates/ec-av1/src/gate_coverage.rs +7 -12
- crates/ec-av1/src/mc.rs +32 -9
- crates/ec-av1/src/restoration.rs +26 -3
- crates/ec-av1/src/stream.rs +183 -0
- lanes/hbdinter-r1.report.md +143 -0
crates/ec-av1/src/decode.rsmodified+6 -0
| @@ -1329,6 +1329,12 @@ pub(crate) fn tmv_hits() -> usize { | |||
| 1329 | crate::mvstack::tmv_hits() | 1329 | crate::mvstack::tmv_hits() |
| 1330 | } | 1330 | } |
| 1331 | 1331 | ||
| 1332 | +/// Current value of [`crate::mc::inter_pred_hits`] -- inter predictions | ||
| 1333 | +/// produced on this thread, the 10-bit inter gate's firing proof. | ||
| 1334 | +pub(crate) fn inter_pred_hits() -> usize { | ||
| 1335 | + crate::mc::inter_pred_hits() | ||
| 1336 | +} | ||
| 1337 | + | ||
| 1332 | /// Everything [`decode_inter_block`] needs to fold temporal MV candidates | 1338 | /// Everything [`decode_inter_block`] needs to fold temporal MV candidates |
| 1333 | /// (spec 7.10.2.8), threaded down from [`decode_inter_frame_tile_with_cdfs`]: | 1339 | /// (spec 7.10.2.8), threaded down from [`decode_inter_frame_tile_with_cdfs`]: |
| 1334 | /// the current frame's own projected field ([`crate::motion_field::setup_motion_field`]'s | 1340 | /// the current frame's own projected field ([`crate::motion_field::setup_motion_field`]'s |
crates/ec-av1/src/gate_coverage.rsmodified+7 -12
| @@ -150,6 +150,13 @@ const NEVER_EXERCISED_10BIT: &[(&str, &str)] = &[ | |||
| 150 | // carries is a hole here too; the seven extra ones below are tools an | 150 | // carries is a hole here too; the seven extra ones below are tools an |
| 151 | // 8-bit gate DOES enable, whose high-bit-depth path no stream has ever | 151 | // 8-bit gate DOES enable, whose high-bit-depth path no stream has ever |
| 152 | // touched -- exactly the shape that hid the two lane-hbdinter defects. | 152 | // touched -- exactly the shape that hid the two lane-hbdinter defects. |
| 153 | + // `enable-restoration` LEFT this list on 2026-09-01: lane-hbdinter's | ||
| 154 | + // 10-bit inter gate passes `--enable-restoration=1` and asserts a real | ||
| 155 | + // Wiener/SGR unit fired, which is what caught the two defects (SGR box | ||
| 156 | + // sums never brought back to the 8-bit scale, Wiener clamp at the wrong | ||
| 157 | + // bound). `enable-dist-wtd-comp` and `enable-global-motion` left it the | ||
| 158 | + // same day for the same reason: lane-cwarp's 10-bit compound global-warp | ||
| 159 | + // gate passes `=1` for both. | ||
| 153 | ( | 160 | ( |
| 154 | "enable-1to4-partitions", | 161 | "enable-1to4-partitions", |
| 155 | "hole at both depths, see the 8-bit list", | 162 | "hole at both depths, see the 8-bit list", |
| @@ -166,10 +173,6 @@ const NEVER_EXERCISED_10BIT: &[(&str, &str)] = &[ | |||
| 166 | "enable-cfl-intra", | 173 | "enable-cfl-intra", |
| 167 | "hole at both depths, see the 8-bit list", | 174 | "hole at both depths, see the 8-bit list", |
| 168 | ), | 175 | ), |
| 169 | - ( | ||
| 170 | - "enable-dist-wtd-comp", | ||
| 171 | - "hole at both depths, see the 8-bit list", | ||
| 172 | - ), | ||
| 173 | ( | 176 | ( |
| 174 | "enable-dual-filter", | 177 | "enable-dual-filter", |
| 175 | "hole at both depths, see the 8-bit list", | 178 | "hole at both depths, see the 8-bit list", |
| @@ -182,10 +185,6 @@ const NEVER_EXERCISED_10BIT: &[(&str, &str)] = &[ | |||
| 182 | "enable-flip-idtx", | 185 | "enable-flip-idtx", |
| 183 | "hole at both depths, see the 8-bit list", | 186 | "hole at both depths, see the 8-bit list", |
| 184 | ), | 187 | ), |
| 185 | - ( | ||
| 186 | - "enable-global-motion", | ||
| 187 | - "hole at both depths, see the 8-bit list", | ||
| 188 | - ), | ||
| 189 | ( | 188 | ( |
| 190 | "enable-intra-edge-filter", | 189 | "enable-intra-edge-filter", |
| 191 | "8-bit only: the 10-bit intra path never runs the edge filter", | 190 | "8-bit only: the 10-bit intra path never runs the edge filter", |
| @@ -204,10 +203,6 @@ const NEVER_EXERCISED_10BIT: &[(&str, &str)] = &[ | |||
| 204 | "enable-ref-frame-mvs", | 203 | "enable-ref-frame-mvs", |
| 205 | "hole at both depths, see the 8-bit list", | 204 | "hole at both depths, see the 8-bit list", |
| 206 | ), | 205 | ), |
| 207 | - ( | ||
| 208 | - "enable-restoration", | ||
| 209 | - "8-bit only -- THE instance this split was built for: every 10-bit gate passed `--enable-restoration=0`, so the SGR box sums and the Wiener clamp were never run at 10 bits and both were wrong (lane-hbdinter, 2026-09-01)", | ||
| 210 | - ), | ||
| 211 | ( | 206 | ( |
| 212 | "enable-smooth-intra", | 207 | "enable-smooth-intra", |
| 213 | "8-bit only: no 10-bit gate enables smooth intra", | 208 | "8-bit only: no 10-bit gate enables smooth intra", |
crates/ec-av1/src/mc.rsmodified+32 -9
| @@ -333,6 +333,7 @@ pub fn predict_with_filters( | |||
| 333 | ) { | 333 | ) { |
| 334 | assert_eq!(dst.len(), block_w * block_h, "the destination is the block"); | 334 | assert_eq!(dst.len(), block_w * block_h, "the destination is the block"); |
| 335 | assert!(!reference.is_empty(), "a reference plane has samples"); | 335 | assert!(!reference.is_empty(), "a reference plane has samples"); |
| 336 | + INTER_PRED_HITS.with(|c| c.set(c.get() + 1)); | ||
| 336 | 337 | ||
| 337 | #[cfg(test)] | 338 | #[cfg(test)] |
| 338 | let stage_t = std::time::Instant::now(); | 339 | let stage_t = std::time::Instant::now(); |
| @@ -441,6 +442,20 @@ pub fn predict_scaled_hits() -> usize { | |||
| 441 | PREDICT_SCALED_HITS.with(|c| c.get()) | 442 | PREDICT_SCALED_HITS.with(|c| c.get()) |
| 442 | } | 443 | } |
| 443 | 444 | ||
| 445 | +thread_local! { | ||
| 446 | + /// lane-hbdinter: how many inter predictions this thread has produced | ||
| 447 | + /// (single-reference, scaled, and the compound intermediate entry). The | ||
| 448 | + /// 10-bit inter gate hard-asserts this moved, so a stream that coded | ||
| 449 | + /// only intra blocks cannot pass it by construction (class | ||
| 450 | + /// `gate-blind-to-feature`). | ||
| 451 | + static INTER_PRED_HITS: std::cell::Cell<usize> = const { std::cell::Cell::new(0) }; | ||
| 452 | +} | ||
| 453 | + | ||
| 454 | +/// Current value of [`INTER_PRED_HITS`]. | ||
| 455 | +pub fn inter_pred_hits() -> usize { | ||
| 456 | + INTER_PRED_HITS.with(|c| c.get()) | ||
| 457 | +} | ||
| 458 | + | ||
| 444 | fn round_pow2_64(value: i64, shift: u32) -> i64 { | 459 | fn round_pow2_64(value: i64, shift: u32) -> i64 { |
| 445 | if shift == 0 { | 460 | if shift == 0 { |
| 446 | value | 461 | value |
| @@ -492,6 +507,7 @@ pub fn predict_scaled( | |||
| 492 | ) { | 507 | ) { |
| 493 | assert_eq!(dst.len(), block_w * block_h, "the destination is the block"); | 508 | assert_eq!(dst.len(), block_w * block_h, "the destination is the block"); |
| 494 | assert!(!reference.is_empty(), "a reference plane has samples"); | 509 | assert!(!reference.is_empty(), "a reference plane has samples"); |
| 510 | + INTER_PRED_HITS.with(|c| c.set(c.get() + 1)); | ||
| 495 | PREDICT_SCALED_HITS.with(|c| c.set(c.get() + 1)); | 511 | PREDICT_SCALED_HITS.with(|c| c.set(c.get() + 1)); |
| 496 | 512 | ||
| 497 | let y0 = y_q4.div_euclid(16); | 513 | let y0 = y_q4.div_euclid(16); |
| @@ -594,6 +610,7 @@ pub fn predict_compound_intermediate( | |||
| 594 | ) { | 610 | ) { |
| 595 | assert_eq!(dst.len(), block_w * block_h, "the destination is the block"); | 611 | assert_eq!(dst.len(), block_w * block_h, "the destination is the block"); |
| 596 | assert!(!reference.is_empty(), "a reference plane has samples"); | 612 | assert!(!reference.is_empty(), "a reference plane has samples"); |
| 613 | + INTER_PRED_HITS.with(|c| c.set(c.get() + 1)); | ||
| 597 | 614 | ||
| 598 | let x0 = x_q4.div_euclid(16); | 615 | let x0 = x_q4.div_euclid(16); |
| 599 | let xfrac = x_q4.rem_euclid(16) as usize; | 616 | let xfrac = x_q4.rem_euclid(16) as usize; |
| @@ -655,8 +672,16 @@ pub fn combine_compound( | |||
| 655 | assert_eq!(pred0.len(), pred1.len(), "both refs predict the same block"); | 672 | assert_eq!(pred0.len(), pred1.len(), "both refs predict the same block"); |
| 656 | assert_eq!(dst.len(), pred0.len(), "the destination is the block"); | 673 | assert_eq!(dst.len(), pred0.len(), "the destination is the block"); |
| 657 | for i in 0..dst.len() { | 674 | for i in 0..dst.len() { |
| 675 | + // libaom (`av1_highbd_dist_wtd_convolve_2d_c`, and its lowbd twin) | ||
| 676 | + // applies the two shifts SEPARATELY: a truncating `>> | ||
| 677 | + // DIST_PRECISION_BITS` on the weighted sum, then `ROUND_POWER_OF_TWO` | ||
| 678 | + // by `round_bits == INTER_POST_ROUND`. Folding them into one | ||
| 679 | + // `Round2(sum, 8)` is NOT the same function -- it rounds up one LSB | ||
| 680 | + // whenever `(sum >> 4) + 8` is one below a multiple of 16 and the | ||
| 681 | + // dropped low 4 bits are non-zero. | ||
| 658 | let sum = pred0[i] * fwd_weight + pred1[i] * bck_weight; | 682 | let sum = pred0[i] * fwd_weight + pred1[i] * bck_weight; |
| 659 | - dst[i] = round2(sum, INTER_POST_ROUND + DIST_PRECISION_BITS).clamp(0, crate::decode::sample_max()) as u16; | 683 | + dst[i] = round2(sum >> DIST_PRECISION_BITS, INTER_POST_ROUND) |
| 684 | + .clamp(0, crate::decode::sample_max()) as u16; | ||
| 660 | } | 685 | } |
| 661 | } | 686 | } |
| 662 | 687 | ||
| @@ -673,15 +698,13 @@ pub fn combine_compound( | |||
| 673 | pub fn diffwtd_mask(pred0: &[i32], pred1: &[i32], inv: bool, mask: &mut [u8]) { | 698 | pub fn diffwtd_mask(pred0: &[i32], pred1: &[i32], inv: bool, mask: &mut [u8]) { |
| 674 | assert_eq!(pred0.len(), pred1.len(), "both refs predict the same block"); | 699 | assert_eq!(pred0.len(), pred1.len(), "both refs predict the same block"); |
| 675 | assert_eq!(mask.len(), pred0.len(), "one mask byte per pixel"); | 700 | assert_eq!(mask.len(), pred0.len(), "one mask byte per pixel"); |
| 701 | + // libaom `diffwtd_mask_d16` (`reconinter.c:307`): `round = 2*FILTER_BITS | ||
| 702 | + // - round_0 - round_1 + (bd - 8)`, i.e. INTER_POST_ROUND plus the | ||
| 703 | + // bit-depth headroom the CONV_BUF domain carries at 10/12-bit. `round_0` | ||
| 704 | + // and `round_1` themselves only move at 12-bit (`convolve.h:83`'s | ||
| 705 | + // `intbufrange > 16`), so the whole bit-depth dependence here is `bd - 8`. | ||
| 706 | + let round = INTER_POST_ROUND + u32::from(crate::decode::bit_depth()).saturating_sub(8); | ||
| 676 | for i in 0..mask.len() { | 707 | for i in 0..mask.len() { |
| 677 | - // lane-cwarp r1: libaom's `round` is `2*FILTER_BITS - round_0 - | ||
| 678 | - // round_1 + (bd - 8)` (`diffwtd_mask_d16`, reconinter.c:306) -- the | ||
| 679 | - // `(bd - 8)` term (which this function's own doc comment already | ||
| 680 | - // named) was missing, so every 10-bit DIFFWTD mask was computed 4x | ||
| 681 | - // too coarse. Caught by lane-cwarp's 10-bit gate arm: a 10-bit | ||
| 682 | - // compound stream with global motion AND warp disabled still | ||
| 683 | - // mismatched at frame 1. | ||
| 684 | - let round = INTER_POST_ROUND + u32::from(crate::decode::bit_depth() - 8); | ||
| 685 | let diff = round2((pred0[i] - pred1[i]).abs(), round); | 708 | let diff = round2((pred0[i] - pred1[i]).abs(), round); |
| 686 | let m = (38 + diff / 16).clamp(0, 64); | 709 | let m = (38 + diff / 16).clamp(0, 64); |
| 687 | mask[i] = if inv { (64 - m) as u8 } else { m as u8 }; | 710 | mask[i] = if inv { (64 - m) as u8 } else { m as u8 }; |
crates/ec-av1/src/restoration.rsmodified+26 -3
| @@ -529,6 +529,9 @@ fn apply_wiener_stripe( | |||
| 529 | if w == 0 || h == 0 { | 529 | if w == 0 || h == 0 { |
| 530 | return; | 530 | return; |
| 531 | } | 531 | } |
| 532 | + let bd = i64::from(crate::decode::bit_depth()); | ||
| 533 | + let wiener_bias = 1i64 << (bd + 3); | ||
| 534 | + let wiener_limit = 1i64 << (bd + 5); | ||
| 532 | let rows = h + 6; | 535 | let rows = h + 6; |
| 533 | let mut inter = vec![0i32; rows * w]; | 536 | let mut inter = vec![0i32; rows * w]; |
| 534 | for r in 0..rows { | 537 | for r in 0..rows { |
| @@ -541,8 +544,20 @@ fn apply_wiener_stripe( | |||
| 541 | sum += tap as i64 | 544 | sum += tap as i64 |
| 542 | * lr_sample(cdef, deblocked, stride, plane_w, plane_h, v_start, v_end, row, x) as i64; | 545 | * lr_sample(cdef, deblocked, stride, plane_w, plane_h, v_start, v_end, row, x) as i64; |
| 543 | } | 546 | } |
| 544 | - // `WIENER_CLAMP_LIMIT(round0=3, bd=8) - 1`. | 547 | + // libaom's `highbd_convolve_add_src_horiz_hip` clamps its |
| 545 | - inter[r * w + c] = round2(sum, 3).clamp(0, 8191) as i32; | 548 | + // *biased* intermediate to `[0, WIENER_CLAMP_LIMIT(3, bd) - 1]` |
| 549 | + // (`convolve.h:43`, `(1 << (bd + 1 + FILTER_BITS - round0))`). | ||
| 550 | + // Its bias over this crate's unbiased sum is exactly | ||
| 551 | + // `(1 << (bd + FILTER_BITS - 1)) >> round0 == 1 << (bd + 3)` | ||
| 552 | + // (the `rounding` term of that function, the rest cancelling | ||
| 553 | + // against `WienerInfo`'s derived centre tap as the doc comment | ||
| 554 | + // above derives), so the same clamp in *this* domain is the | ||
| 555 | + // bound shifted down by that bias. At 8-bit neither end is ever | ||
| 556 | + // reached by real content, which is why the old fixed `[0, 8191]` | ||
| 557 | + // survived every 8-bit gate; at 10-bit the upper end is | ||
| 558 | + // `128 * 1023 >> 3 == 16368`, well past `8191`, so a fixed | ||
| 559 | + // 8-bit limit silently truncated most 10-bit Wiener pixels. | ||
| 560 | + inter[r * w + c] = round2(sum, 3).clamp(-wiener_bias, wiener_limit - 1 - wiener_bias) as i32; | ||
| 546 | } | 561 | } |
| 547 | } | 562 | } |
| 548 | for r in 0..h { | 563 | for r in 0..h { |
| @@ -646,7 +661,15 @@ fn compute_ab( | |||
| 646 | b += px; | 661 | b += px; |
| 647 | } | 662 | } |
| 648 | } | 663 | } |
| 649 | - let p = if a * n < b * b { 0 } else { a * n - b * b }; | 664 | + // libaom `restoration.c:660` -- the box sums are brought back |
| 665 | + // to the 8-bit scale before `p` is formed (`a` is a sum of | ||
| 666 | + // squares, so it loses twice the shift `b` does); `B[k]` below | ||
| 667 | + // keeps the RAW `b`, only `p` uses the scaled pair. At 8-bit | ||
| 668 | + // both shifts are 0, which is why this was invisible until now. | ||
| 669 | + let bd_shift = u32::from(crate::decode::bit_depth()).saturating_sub(8); | ||
| 670 | + let a_s = round2(a, 2 * bd_shift); | ||
| 671 | + let b_s = round2(b, bd_shift); | ||
| 672 | + let p = if a_s * n < b_s * b_s { 0 } else { a_s * n - b_s * b_s }; | ||
| 650 | let z = round2(p * s as i64, SGRPROJ_MTABLE_BITS).clamp(0, 255) as usize; | 673 | let z = round2(p * s as i64, SGRPROJ_MTABLE_BITS).clamp(0, 255) as usize; |
| 651 | let a_val = SGR_X_BY_XPLUS1[z]; | 674 | let a_val = SGR_X_BY_XPLUS1[z]; |
| 652 | let b_val = round2( | 675 | let b_val = round2( |
crates/ec-av1/src/stream.rsmodified+183 -0
| @@ -1982,6 +1982,189 @@ mod tests { | |||
| 1982 | assert_eq!(frames[0].v, ffmpeg_frames[0].v, "V vs ffmpeg (10-bit)"); | 1982 | assert_eq!(frames[0].v, ffmpeg_frames[0].v, "V vs ffmpeg (10-bit)"); |
| 1983 | } | 1983 | } |
| 1984 | 1984 | ||
| 1985 | + /// The 10-bit *inter* counterpart of the gate above: a real `aomenc` | ||
| 1986 | + /// `yuv420p10le` sequence whose non-key frames are plain forward | ||
| 1987 | + /// single-`LAST_FRAME` P frames (same `--lag-in-frames=0 | ||
| 1988 | + /// --auto-alt-ref=0 --error-resilient=1` shape as the 8-bit inter gate), | ||
| 1989 | + /// decoded pixel-exact on every frame. 10-bit intra was proven exact by | ||
| 1990 | + /// lane-hbd; inter reconstruction at `bit_depth == 10` was never gated. | ||
| 1991 | + #[test] | ||
| 1992 | + fn a_real_aomenc_10bit_inter_sequence_decodes_pixel_exact() { | ||
| 1993 | + const NAME: &str = "a_real_aomenc_10bit_inter_sequence_decodes_pixel_exact"; | ||
| 1994 | + let _gate_lock = lock_gate_counters(); | ||
| 1995 | + if !have_ffmpeg() { | ||
| 1996 | + eprintln!("SKIP {NAME}: no ffmpeg"); | ||
| 1997 | + return; | ||
| 1998 | + } | ||
| 1999 | + if !have_aomenc() { | ||
| 2000 | + eprintln!("SKIP {NAME}: no aomenc at {}", aomenc_path().display()); | ||
| 2001 | + return; | ||
| 2002 | + } | ||
| 2003 | + let (width, height, frame_count) = (128usize, 64usize, 16usize); | ||
| 2004 | + let y4m = Command::new("ffmpeg") | ||
| 2005 | + .args([ | ||
| 2006 | + "-v", | ||
| 2007 | + "error", | ||
| 2008 | + "-f", | ||
| 2009 | + "lavfi", | ||
| 2010 | + "-i", | ||
| 2011 | + &gradients_source(42, width, height, "duration=0.64:rate=25"), | ||
| 2012 | + "-pix_fmt", | ||
| 2013 | + "yuv420p10le", | ||
| 2014 | + "-strict", | ||
| 2015 | + "-1", | ||
| 2016 | + "-t", | ||
| 2017 | + "0.64", | ||
| 2018 | + "-f", | ||
| 2019 | + "yuv4mpegpipe", | ||
| 2020 | + "-", | ||
| 2021 | + ]) | ||
| 2022 | + .stdin(Stdio::null()) | ||
| 2023 | + .stdout(Stdio::piped()) | ||
| 2024 | + .stderr(Stdio::piped()) | ||
| 2025 | + .output() | ||
| 2026 | + .expect("ffmpeg failed to run"); | ||
| 2027 | + assert!( | ||
| 2028 | + y4m.status.success(), | ||
| 2029 | + "ffmpeg failed to render the 10-bit fixture: {}", | ||
| 2030 | + String::from_utf8_lossy(&y4m.stderr) | ||
| 2031 | + ); | ||
| 2032 | + let mut child = Command::new(aomenc_path()) | ||
| 2033 | + .args([ | ||
| 2034 | + "--codec=av1", | ||
| 2035 | + "--passes=1", | ||
| 2036 | + "--end-usage=q", | ||
| 2037 | + "--cq-level=15", | ||
| 2038 | + "--cpu-used=0", | ||
| 2039 | + "--threads=1", | ||
| 2040 | + "--row-mt=0", | ||
| 2041 | + "--sb-size=64", | ||
| 2042 | + "--input-bit-depth=10", | ||
| 2043 | + "--bit-depth=10", | ||
| 2044 | + "--lag-in-frames=0", | ||
| 2045 | + "--auto-alt-ref=0", | ||
| 2046 | + "--kf-max-dist=1000", | ||
| 2047 | + "--error-resilient=1", | ||
| 2048 | + "--aq-mode=0", | ||
| 2049 | + "--deltaq-mode=0", | ||
| 2050 | + "--enable-rect-partitions=0", | ||
| 2051 | + "--enable-ab-partitions=0", | ||
| 2052 | + "--enable-1to4-partitions=0", | ||
| 2053 | + "--enable-filter-intra=0", | ||
| 2054 | + "--enable-smooth-intra=0", | ||
| 2055 | + "--enable-paeth-intra=0", | ||
| 2056 | + "--enable-directional-intra=0", | ||
| 2057 | + "--enable-angle-delta=0", | ||
| 2058 | + "--enable-tx-size-search=0", | ||
| 2059 | + "--enable-cdef=1", | ||
| 2060 | + "--enable-restoration=1", | ||
| 2061 | + "--min-partition-size=16", | ||
| 2062 | + "--max-partition-size=32", | ||
| 2063 | + "--enable-palette=0", | ||
| 2064 | + "--enable-intrabc=0", | ||
| 2065 | + "--enable-cfl-intra=0", | ||
| 2066 | + "--enable-ref-frame-mvs=0", | ||
| 2067 | + "--obu", | ||
| 2068 | + "-o", | ||
| 2069 | + "-", | ||
| 2070 | + "-", | ||
| 2071 | + ]) | ||
| 2072 | + .stdin(Stdio::piped()) | ||
| 2073 | + .stdout(Stdio::piped()) | ||
| 2074 | + .stderr(Stdio::piped()) | ||
| 2075 | + .spawn() | ||
| 2076 | + .expect("aomenc failed to start"); | ||
| 2077 | + child | ||
| 2078 | + .stdin | ||
| 2079 | + .take() | ||
| 2080 | + .expect("aomenc stdin") | ||
| 2081 | + .write_all(&y4m.stdout) | ||
| 2082 | + .expect("writing y4m to aomenc"); | ||
| 2083 | + let out = child.wait_with_output().expect("aomenc failed to run"); | ||
| 2084 | + assert!( | ||
| 2085 | + out.status.success(), | ||
| 2086 | + "aomenc refused the 10-bit fixture: {}", | ||
| 2087 | + String::from_utf8_lossy(&out.stderr) | ||
| 2088 | + ); | ||
| 2089 | + let stream = out.stdout; | ||
| 2090 | + if let Ok(dump) = std::env::var("EC_AV1_GATE_DUMP") { | ||
| 2091 | + std::fs::write(dump, &stream).expect("dump stream"); | ||
| 2092 | + } | ||
| 2093 | + let mut probe = Av1Parser::new(); | ||
| 2094 | + let mut pos = 0usize; | ||
| 2095 | + while pos < stream.len() && probe.sequence_header().is_none() { | ||
| 2096 | + let obu = probe.parse_obu(&stream[pos..]).unwrap(); | ||
| 2097 | + pos += obu.total_size; | ||
| 2098 | + } | ||
| 2099 | + assert_eq!( | ||
| 2100 | + probe | ||
| 2101 | + .sequence_header() | ||
| 2102 | + .expect("stream has a sequence header OBU") | ||
| 2103 | + .color_config | ||
| 2104 | + .bit_depth, | ||
| 2105 | + 10, | ||
| 2106 | + "{NAME}: aomenc did not actually write a 10-bit sequence header" | ||
| 2107 | + ); | ||
| 2108 | + let before_deblock = decode::deblock_hits(); | ||
| 2109 | + let before_inter = decode::inter_pred_hits(); | ||
| 2110 | + let before_lr = crate::restoration::wiener_hits() | ||
| 2111 | + + crate::restoration::sgrproj_hits() | ||
| 2112 | + + crate::restoration::switchable_hits(); | ||
| 2113 | + let frames = match decode_stream(&stream) { | ||
| 2114 | + Ok(frames) => frames, | ||
| 2115 | + Err(e) => panic!("{NAME}: decode_stream refused a real 10-bit inter stream: {e}"), | ||
| 2116 | + }; | ||
| 2117 | + assert!( | ||
| 2118 | + decode::inter_pred_hits() > before_inter, | ||
| 2119 | + "{NAME}: no inter-predicted block decoded -- the gate proves nothing" | ||
| 2120 | + ); | ||
| 2121 | + assert!( | ||
| 2122 | + decode::deblock_hits() > before_deblock, | ||
| 2123 | + "{NAME}: no deblocking edge fired -- raise cq-level" | ||
| 2124 | + ); | ||
| 2125 | + // Loop restoration is where the 10-bit inter/intra defect lived | ||
| 2126 | + // (lane-hbdinter r1: the self-guided box sums were never brought back | ||
| 2127 | + // to the 8-bit scale, `restoration.c:660`); a recipe whose RD happened | ||
| 2128 | + // to pick RESTORE_NONE would pass while proving nothing. | ||
| 2129 | + assert!( | ||
| 2130 | + crate::restoration::wiener_hits() | ||
| 2131 | + + crate::restoration::sgrproj_hits() | ||
| 2132 | + + crate::restoration::switchable_hits() | ||
| 2133 | + > before_lr, | ||
| 2134 | + "{NAME}: no loop-restoration unit decoded a real filter -- lower cq-level" | ||
| 2135 | + ); | ||
| 2136 | + eprintln!( | ||
| 2137 | + "{NAME}: inter_pred {} deblock {} cdef_idx {} wiener {} sgrproj {}", | ||
| 2138 | + decode::inter_pred_hits() - before_inter, | ||
| 2139 | + decode::deblock_hits() - before_deblock, | ||
| 2140 | + decode::cdef_idx_hits(), | ||
| 2141 | + crate::restoration::wiener_hits(), | ||
| 2142 | + crate::restoration::sgrproj_hits(), | ||
| 2143 | + ); | ||
| 2144 | + assert_eq!(frames.len(), frame_count); | ||
| 2145 | + let ffmpeg_frames = ffmpeg_decode_sequence_10bit(&stream, width, height, frame_count); | ||
| 2146 | + for (i, (got, want)) in frames.iter().zip(&ffmpeg_frames).enumerate() { | ||
| 2147 | + if got.y != want.y || got.u != want.u || got.v != want.v { | ||
| 2148 | + let pin = std::env::temp_dir().join("ec-av1-10bit-inter-gate-fail.obu"); | ||
| 2149 | + let _ = std::fs::write(&pin, &stream); | ||
| 2150 | + let first = |a: &Vec<u16>, b: &Vec<u16>| { | ||
| 2151 | + a.iter() | ||
| 2152 | + .zip(b) | ||
| 2153 | + .enumerate() | ||
| 2154 | + .find(|(_, (x, y))| x != y) | ||
| 2155 | + .map(|(k, (x, y))| (k, *x, *y)) | ||
| 2156 | + }; | ||
| 2157 | + panic!( | ||
| 2158 | + "{NAME}: frame {i} mismatch vs ffmpeg -- Y {:?} U {:?} V {:?}; stream pinned at {}", | ||
| 2159 | + first(&got.y, &want.y), | ||
| 2160 | + first(&got.u, &want.u), | ||
| 2161 | + first(&got.v, &want.v), | ||
| 2162 | + pin.display() | ||
| 2163 | + ); | ||
| 2164 | + } | ||
| 2165 | + } | ||
| 2166 | + } | ||
| 2167 | + | ||
| 1985 | /// lane-hbd10 r1: a real `aomenc --bit-depth=10 --film-grain-test=1` | 2168 | /// lane-hbd10 r1: a real `aomenc --bit-depth=10 --film-grain-test=1` |
| 1986 | /// stream, decoded pixel-exact against ffmpeg's own 10-bit decode of the | 2169 | /// stream, decoded pixel-exact against ffmpeg's own 10-bit decode of the |
| 1987 | /// identical bytes (ffmpeg synthesizes grain by default -- no | 2170 | /// identical bytes (ffmpeg synthesizes grain by default -- no |
lanes/hbdinter-r1.report.mdadded+143 -0
| @@ -0,0 +1,143 @@ | |||
| 1 | +# lane-hbdinter round 1 report | ||
| 2 | + | ||
| 3 | +Branch `lane-hbdinter`, worktree `edith_codecs-hbdinter`, off main `3808cf8`. | ||
| 4 | +Commit `d6b5912`. | ||
| 5 | + | ||
| 6 | +## Premise re-measured (charter said: 10-bit stream mismatches at frame 2) | ||
| 7 | + | ||
| 8 | +NOT reproduced as stated. Built the 10-bit counterpart of the 8-bit inter | ||
| 9 | +gate (`a_real_aomenc_inter_sequence_with_deblocking_decodes_pixel_exact`'s | ||
| 10 | +recipe + `--input-bit-depth=10 --bit-depth=10 --aq-mode=0 --deltaq-mode=0`): | ||
| 11 | + | ||
| 12 | +- 64x64, 4 frames, cq 55: **all 4 frames pixel-exact** (first run). | ||
| 13 | +- 128x64, 16 frames (the charter's geometry), cq 55: **all 16 exact** once | ||
| 14 | + `--min-partition-size=16` keeps aomenc out of the pre-existing | ||
| 15 | + "an inter partition below 16x16" refusal (which is a different lane). | ||
| 16 | + | ||
| 17 | +So with that tool set 10-bit inter reconstruction was already exact. The | ||
| 18 | +real defect only appears once **loop restoration** is enabled (cq-level=15, | ||
| 19 | +`--enable-restoration=1`, the LR gate's own recipe), and it is NOT | ||
| 20 | +inter-specific: the first mismatch is on **frame 0, the key frame**. | ||
| 21 | +lane-hbd's 10-bit gate never saw it because its recipe leaves LR off. | ||
| 22 | + | ||
| 23 | +EVIDENCE: /tmp/ec-av1-10bit-inter-gate-fail.obu | cargo test -p ec-av1 --lib a_real_aomenc_10bit_inter_sequence_decodes_pixel_exact (compute_ab scaling reverted) | frame 0 V plane, sample 52: ours 289, ffmpeg 288 | ||
| 24 | + | ||
| 25 | +## What changed (all four verified line-for-line against ~/.cache/aom-oracle/src) | ||
| 26 | + | ||
| 27 | +1. `crates/ec-av1/src/restoration.rs:~649` `compute_ab` -- box sums are now | ||
| 28 | + scaled back to the 8-bit range before `p` is formed | ||
| 29 | + (`a = Round2(A, 2*(bd-8))`, `b = Round2(B, bd-8)`, libaom | ||
| 30 | + `restoration.c:660-662`; `B[k]` keeps the RAW sum, as libaom does). | ||
| 31 | + **This is the site the gate attributes the mismatch to.** | ||
| 32 | +2. `crates/ec-av1/src/restoration.rs:~545` `apply_wiener_stripe` -- the | ||
| 33 | + horizontal intermediate clamp was the fixed `[0, 8191]` | ||
| 34 | + (`WIENER_CLAMP_LIMIT(3, bd=8) - 1`, `convolve.h:43`) expressed in this | ||
| 35 | + crate's *unbiased* domain. Now `[-(1<<(bd+3)), (1<<(bd+5)) - 1 - (1<<(bd+3))]`, | ||
| 36 | + the libaom bound shifted by libaom's own `1 << (bd + 3)` bias (the | ||
| 37 | + `rounding` term of `highbd_convolve_add_src_horiz_hip`; the rest cancels | ||
| 38 | + against `WienerInfo`'s derived centre tap, as the existing doc comment | ||
| 39 | + derives). At 8-bit neither end is reachable -- that is why every 8-bit | ||
| 40 | + gate passed; at 10-bit the unbiased maximum is `128*1023 >> 3 == 16368`, | ||
| 41 | + so most Wiener intermediates were silently truncated at 8191. | ||
| 42 | +3. `crates/ec-av1/src/mc.rs` `diffwtd_mask` -- round is | ||
| 43 | + `INTER_POST_ROUND + (bd - 8)` (libaom `reconinter.c:307`), was the | ||
| 44 | + constant 4. | ||
| 45 | +4. `crates/ec-av1/src/warp.rs:450` `warp_affine` -- `const BD: i32 = 8` | ||
| 46 | + replaced by `decode::bit_depth()` in `offset_bits_horiz`, | ||
| 47 | + `offset_bits_vert` and the final `sum - (1<<(bd-1)) - (1<<bd)` | ||
| 48 | + (`av1_warp_affine_c`). Tip from lane-gmaffine, confirmed at the source. | ||
| 49 | + | ||
| 50 | +Ruled out, at the source, no change needed: non-compound and compound | ||
| 51 | +sub-pel MC rounding. libaom only moves `round_0`/`round_1` when | ||
| 52 | +`bd + FILTER_BITS - round_0 + 2 > 16` (`convolve.h:83`), i.e. 12-bit only; | ||
| 53 | +10-bit keeps 3/11 (single) and 3/7 (compound). `aom_highbd_blend_a64_d16_mask_c`'s | ||
| 54 | +`round_bits` is likewise bit-depth-independent, so `blend_masked_compound` | ||
| 55 | +is correct as written. | ||
| 56 | + | ||
| 57 | +New counter: `mc::inter_pred_hits` (incremented in `predict_with_filters`, | ||
| 58 | +`predict_scaled`, `predict_compound_intermediate`), re-exported as | ||
| 59 | +`decode::inter_pred_hits`. | ||
| 60 | + | ||
| 61 | +## Gate | ||
| 62 | + | ||
| 63 | +`a_real_aomenc_10bit_inter_sequence_decodes_pixel_exact` (crates/ec-av1/src/stream.rs). | ||
| 64 | +Real aomenc, `yuv420p10le`, 128x64, 16 frames, `--lag-in-frames=0 | ||
| 65 | +--auto-alt-ref=0 --kf-max-dist=1000 --error-resilient=1 --aq-mode=0 | ||
| 66 | +--deltaq-mode=0 --enable-restoration=1 --enable-cdef=1 --cq-level=15`, | ||
| 67 | +sequence header asserted `bit_depth == 10`, all 16 frames compared against | ||
| 68 | +ffmpeg's own `yuv420p10le` decode, hard-asserting three firing counters | ||
| 69 | +(inter predictions, deblock edges, a non-`RESTORE_NONE` LR filter). Decode | ||
| 70 | +errors and mismatches panic; nothing turns into SKIP. | ||
| 71 | + | ||
| 72 | +``` | ||
| 73 | +EC_AV1_REQUIRE_AOMENC=1 CARGO_TARGET_DIR=$HOME/.cache/cargo-target-hbdinter \ | ||
| 74 | + cargo test -p ec-av1 --lib -j3 a_real_aomenc_10bit_inter_sequence_decodes_pixel_exact -- --nocapture | ||
| 75 | +``` | ||
| 76 | + | ||
| 77 | +EVIDENCE: /tmp/claude-.../scratchpad/hbdinter-suite.log | the command above, with the fix | "inter_pred 759 deblock 2592 cdef_idx 0 wiener 1 sgrproj 3", test result: ok. 1 passed | ||
| 78 | + | ||
| 79 | +## Test totals | ||
| 80 | + | ||
| 81 | +`EC_AV1_REQUIRE_AOMENC=1 cargo test -p ec-av1 --lib -j3`: | ||
| 82 | +**268 passed, 0 failed, 23 ignored, 0 filtered out, 664.37s.** | ||
| 83 | + | ||
| 84 | +EVIDENCE: scratchpad/hbdinter-suite.log | cargo test -p ec-av1 --lib -j3 | test result: ok. 268 passed; 0 failed; 23 ignored | ||
| 85 | + | ||
| 86 | +## Refusals | ||
| 87 | + | ||
| 88 | +None lifted this round -- these were silent wrong-pixel defects behind | ||
| 89 | +already-permitted code paths, not refusals. `refusal_inventory.rs` and | ||
| 90 | +`gate_coverage.rs` unchanged and green in the suite above. | ||
| 91 | + | ||
| 92 | +## Film check | ||
| 93 | + | ||
| 94 | +`cargo run -p ec-av1 --example decode_probe` on the 3s extracts already in | ||
| 95 | +this worktree (`fixtures/hbd-r5/{reelb,reelc}.obu`): | ||
| 96 | + | ||
| 97 | +- ReelB: `REFUSED: unsupported: AV1 tile (a partition below 8x8 ...)` | ||
| 98 | +- ReelC: `REFUSED: unsupported: AV1 tile (a 32x32 partition type this decoder does not code (value=4))` | ||
| 99 | + | ||
| 100 | +Unchanged from lane-hbd r5 -- both films still stop at partition-coverage | ||
| 101 | +refusals that sit *before* any of the four fixed sites, so no frontier | ||
| 102 | +movement is claimable from this lane. The fixes matter for what happens | ||
| 103 | +after those two lanes land. | ||
| 104 | + | ||
| 105 | +## Residue | ||
| 106 | + | ||
| 107 | +- deferred: 10-bit gate for masked compound (`diffwtd_mask`) — needs a real | ||
| 108 | + aomenc `--enable-masked-comp=1` 10-bit stream that actually lands a | ||
| 109 | + DIFFWTD block; the 8-bit masked-compound gate exists, its 10-bit twin does | ||
| 110 | + not — unblocked by cloning that gate's recipe at `--bit-depth=10`. | ||
| 111 | +- deferred: 10-bit gate for warp/global motion (`warp.rs` bd) — same shape; | ||
| 112 | + lane-gmaffine owns the warp gates, this fix should be re-verified there | ||
| 113 | + once its 10-bit stream exists. | ||
| 114 | +- deferred: 10-bit gate proving the Wiener clamp bound specifically — the | ||
| 115 | + gate's stream fired Wiener once and the mismatch it catches is attributed | ||
| 116 | + to `compute_ab` (ablated separately); the clamp fix is source-verified | ||
| 117 | + against `convolve.h:43` but not attributed by a failing-then-passing | ||
| 118 | + pixel — unblocked by a fixture whose LR RD picks `RESTORE_WIENER` at | ||
| 119 | + 10-bit (higher-detail content than `gradients`). | ||
| 120 | +- FIXED after the first commit (`mc.rs` `combine_compound`, commit below): | ||
| 121 | + it folded libaom's truncating `>> DIST_PRECISION_BITS` and the following | ||
| 122 | + `Round2(.., INTER_POST_ROUND)` into one `Round2(.., 8)`. Those are not the | ||
| 123 | + same function -- the folded form rounds one LSB up whenever | ||
| 124 | + `(sum >> 4) + 8` sits one below a multiple of 16 with non-zero dropped | ||
| 125 | + low bits. Bit-depth independent (the CONV_BUF `round_offset` cancels | ||
| 126 | + exactly in this crate's unbiased domain, derived from | ||
| 127 | + `av1_highbd_dist_wtd_convolve_2d_c`), so it is a rare off-by-one at ANY | ||
| 128 | + depth. Flagged by lane-cwarp's measurement that 10-bit *compound* still | ||
| 129 | + mismatches frame 1 luma while single-ref is 6/6 exact -- this is my best | ||
| 130 | + candidate for that, but I did not have lane-cwarp's fixture, so it is | ||
| 131 | + UNVERIFIED against their mismatch. All 16 compound tests (incl. the two | ||
| 132 | + real-aomenc 8-bit compound/masked-compound gates) stay green with it: | ||
| 133 | + `cargo test -p ec-av1 --lib -j3 compound` -> 16 passed, 0 failed, 73.58s. | ||
| 134 | + | ||
| 135 | +## Overlap with lane-cwarp | ||
| 136 | + | ||
| 137 | +lane-cwarp commit `04fc3ce` (worktree `edith_codecs-cwarp`) carries the same | ||
| 138 | +two fixes I derived independently here: `mc.rs` `diffwtd_mask`'s missing | ||
| 139 | +`(bd - 8)` and `warp.rs`'s `const BD = 8`. Semantically identical, textually | ||
| 140 | +different (my hunks carry different comments) -- **expect a conflict in | ||
| 141 | +`mc.rs` and `warp.rs` when both branches merge; either side's hunk is | ||
| 142 | +correct, keep one.** The LR (`compute_ab`, Wiener clamp) and | ||
| 143 | +`combine_compound` fixes are unique to this branch. | ||