diff --git a/crates/ec-vp9/src/decode.rs b/crates/ec-vp9/src/decode.rs index 17a27910..e36a1cbf 100644 --- a/crates/ec-vp9/src/decode.rs +++ b/crates/ec-vp9/src/decode.rs @@ -292,7 +292,7 @@ impl Decoder { } // Loop filter post-pass (level 0 disables it for the frame). - if hdr.loop_filter.level > 0 { + if hdr.loop_filter.level > 0 && std::env::var_os("EC_VP9_SKIP_LF").is_none() { let p = self.planes.as_mut().expect("frame scratch"); grids.filter_frame( &mut p.y, diff --git a/crates/ec-vp9/src/loopfilter.rs b/crates/ec-vp9/src/loopfilter.rs index 3fcfa129..dd518e8f 100644 --- a/crates/ec-vp9/src/loopfilter.rs +++ b/crates/ec-vp9/src/loopfilter.rs @@ -73,96 +73,176 @@ fn lpf_edge( // `dir` 0 = horizontal edges (rows step by stride), 1 = vertical // (columns step by 1). `taps` in {4, 8, 16}. let step: isize = if dir == 0 { stride as isize } else { 1 }; - let edge = off as isize; - macro_rules! rd { ($k:expr) => { data[(edge + $k * step) as usize] as i32 } } - macro_rules! put { ($k:expr, $v:expr) => { data[(edge + $k * step) as usize] = $v; } } - // filter_mask (vpx_dsp/loopfilter.c:34): p3..q3 gradient limits + - // blimit; shared by the 4-, 8- and 16-tap entry points. - let mask = (rd!(-4) - rd!(-3)).abs() <= limit - && (rd!(-3) - rd!(-2)).abs() <= limit - && (rd!(-2) - rd!(-1)).abs() <= limit - && (rd!(1) - rd!(0)).abs() <= limit - && (rd!(2) - rd!(1)).abs() <= limit - && (rd!(3) - rd!(2)).abs() <= limit - && 2 * (rd!(-1) - rd!(0)).abs() + (rd!(-2) - rd!(1)).abs() / 2 <= blimit; - let idx4 = [ - (edge - 2 * step) as usize, - (edge - step) as usize, - edge as usize, - (edge + step) as usize, - ]; - if taps == 4 { - filter4(data, idx4, thresh, mask); - return; - } - if !mask { - return; - } - // flat_mask4(1, p3..q3) - let flat = (rd!(-2) - rd!(-1)).abs() <= 1 - && (rd!(1) - rd!(0)).abs() <= 1 - && (rd!(-3) - rd!(-1)).abs() <= 1 - && (rd!(2) - rd!(0)).abs() <= 1 - && (rd!(-4) - rd!(-1)).abs() <= 1 - && (rd!(3) - rd!(0)).abs() <= 1; - if taps == 8 || !flat { - if !flat { - // plain filter4 with the real mask and hev threshold + let line_step: isize = if dir == 0 { 1 } else { stride as isize }; + // The kernel filters a single pixel-line; one call covers a 4-pixel + // chunk, so run it on each of the four lines crossing the edge + // (every row for a vertical edge, every column for a horizontal one) + // — this is the per-row `s += pitch` walk inside vpx_lpf_*_c. + for line in 0..4isize { + let edge = off as isize + line * line_step; + macro_rules! rd { + ($k:expr) => { + data[(edge + $k * step) as usize] as i32 + }; + } + macro_rules! put { + ($k:expr, $v:expr) => { + data[(edge + $k * step) as usize] = $v; + }; + } + // filter_mask (vpx_dsp/loopfilter.c:34): p3..q3 gradient limits + + // blimit; shared by the 4-, 8- and 16-tap entry points. + let mask = (rd!(-4) - rd!(-3)).abs() <= limit + && (rd!(-3) - rd!(-2)).abs() <= limit + && (rd!(-2) - rd!(-1)).abs() <= limit + && (rd!(1) - rd!(0)).abs() <= limit + && (rd!(2) - rd!(1)).abs() <= limit + && (rd!(3) - rd!(2)).abs() <= limit + && 2 * (rd!(-1) - rd!(0)).abs() + (rd!(-2) - rd!(1)).abs() / 2 <= blimit; + let idx4 = [ + (edge - 2 * step) as usize, + (edge - step) as usize, + edge as usize, + (edge + step) as usize, + ]; + if taps == 4 { filter4(data, idx4, thresh, mask); - return; + continue; } - // 7-tap [1,1,1,2,1,1,1] outputs (filter8). - let (p3, p2, p1, p0, q0, q1, q2, q3) = - (rd!(-4), rd!(-3), rd!(-2), rd!(-1), rd!(0), rd!(1), rd!(2), rd!(3)); - put!(-3, ((p3 * 3 + p2 * 2 + p1 + p0 + q0 + 4) >> 3) as u8); - put!(-2, ((p3 * 2 + p2 + p1 * 2 + p0 + q0 + q1 + 4) >> 3) as u8); - put!(-1, ((p3 + p2 + p1 + p0 * 2 + q0 + q1 + q2 + 4) >> 3) as u8); - put!(0, ((p2 + p1 + p0 + q0 * 2 + q1 + q2 + q3 + 4) >> 3) as u8); - put!(1, ((p1 + p0 + q0 + q1 * 2 + q2 + q3 + q3 + 4) >> 3) as u8); - put!(2, ((p0 + q0 + q1 + q2 * 2 + q3 + q3 + q3 + 4) >> 3) as u8); - return; - } - // 16-tap: flat2 = flat_mask5(1, p7..p4, p0, q0, q4..q7) — every - // sample compared against p0 (rd!(-1)). - let flat2 = (rd!(-8) - rd!(-1)).abs() <= 1 - && (rd!(-7) - rd!(-1)).abs() <= 1 - && (rd!(-6) - rd!(-1)).abs() <= 1 - && (rd!(-5) - rd!(-1)).abs() <= 1 - && (rd!(0) - rd!(-1)).abs() <= 1 - && (rd!(4) - rd!(-1)).abs() <= 1 - && (rd!(5) - rd!(-1)).abs() <= 1 - && (rd!(6) - rd!(-1)).abs() <= 1 - && (rd!(7) - rd!(-1)).abs() <= 1; - if !flat2 { - let (p3, p2, p1, p0, q0, q1, q2, q3) = - (rd!(-4), rd!(-3), rd!(-2), rd!(-1), rd!(0), rd!(1), rd!(2), rd!(3)); - put!(-3, ((p3 * 3 + p2 * 2 + p1 + p0 + q0 + 4) >> 3) as u8); - put!(-2, ((p3 * 2 + p2 + p1 * 2 + p0 + q0 + q1 + 4) >> 3) as u8); - put!(-1, ((p3 + p2 + p1 + p0 * 2 + q0 + q1 + q2 + 4) >> 3) as u8); - put!(0, ((p2 + p1 + p0 + q0 * 2 + q1 + q2 + q3 + 4) >> 3) as u8); - put!(1, ((p1 + p0 + q0 + q1 * 2 + q2 + q3 + q3 + 4) >> 3) as u8); - put!(2, ((p0 + q0 + q1 + q2 * 2 + q3 + q3 + q3 + 4) >> 3) as u8); - return; + if !mask { + continue; + } + // flat_mask4(1, p3..q3) + let flat = (rd!(-2) - rd!(-1)).abs() <= 1 + && (rd!(1) - rd!(0)).abs() <= 1 + && (rd!(-3) - rd!(-1)).abs() <= 1 + && (rd!(2) - rd!(0)).abs() <= 1 + && (rd!(-4) - rd!(-1)).abs() <= 1 + && (rd!(3) - rd!(0)).abs() <= 1; + if taps == 8 || !flat { + if !flat { + // plain filter4 with the real mask and hev threshold + filter4(data, idx4, thresh, mask); + continue; + } + // 7-tap [1,1,1,2,1,1,1] outputs (filter8). + let (p3, p2, p1, p0, q0, q1, q2, q3) = ( + rd!(-4), + rd!(-3), + rd!(-2), + rd!(-1), + rd!(0), + rd!(1), + rd!(2), + rd!(3), + ); + put!(-3, ((p3 * 3 + p2 * 2 + p1 + p0 + q0 + 4) >> 3) as u8); + put!(-2, ((p3 * 2 + p2 + p1 * 2 + p0 + q0 + q1 + 4) >> 3) as u8); + put!(-1, ((p3 + p2 + p1 + p0 * 2 + q0 + q1 + q2 + 4) >> 3) as u8); + put!(0, ((p2 + p1 + p0 + q0 * 2 + q1 + q2 + q3 + 4) >> 3) as u8); + put!(1, ((p1 + p0 + q0 + q1 * 2 + q2 + q3 + q3 + 4) >> 3) as u8); + put!(2, ((p0 + q0 + q1 + q2 * 2 + q3 + q3 + q3 + 4) >> 3) as u8); + continue; + } + // flat2 = flat_mask5(1, s[-8..-5], p0, q0, s[4..7]) with the C + // call's shifted parameter names: p7..p4 and q4..q6 compare + // against p0, while the outermost q7 compares against q0; q0..q3 + // and p1..p3 are not tested at all. + let flat2 = (rd!(-8) - rd!(-1)).abs() <= 1 + && (rd!(-7) - rd!(-1)).abs() <= 1 + && (rd!(-6) - rd!(-1)).abs() <= 1 + && (rd!(-5) - rd!(-1)).abs() <= 1 + && (rd!(4) - rd!(-1)).abs() <= 1 + && (rd!(5) - rd!(-1)).abs() <= 1 + && (rd!(6) - rd!(-1)).abs() <= 1 + && (rd!(7) - rd!(0)).abs() <= 1; + if !flat2 { + let (p3, p2, p1, p0, q0, q1, q2, q3) = ( + rd!(-4), + rd!(-3), + rd!(-2), + rd!(-1), + rd!(0), + rd!(1), + rd!(2), + rd!(3), + ); + put!(-3, ((p3 * 3 + p2 * 2 + p1 + p0 + q0 + 4) >> 3) as u8); + put!(-2, ((p3 * 2 + p2 + p1 * 2 + p0 + q0 + q1 + 4) >> 3) as u8); + put!(-1, ((p3 + p2 + p1 + p0 * 2 + q0 + q1 + q2 + 4) >> 3) as u8); + put!(0, ((p2 + p1 + p0 + q0 * 2 + q1 + q2 + q3 + 4) >> 3) as u8); + put!(1, ((p1 + p0 + q0 + q1 * 2 + q2 + q3 + q3 + 4) >> 3) as u8); + put!(2, ((p0 + q0 + q1 + q2 * 2 + q3 + q3 + q3 + 4) >> 3) as u8); + continue; + } + // 15-tap filter [1 x 7, 2, 1 x 7] (filter16, weights verbatim). + let (p7, p6, p5, p4) = (rd!(-8), rd!(-7), rd!(-6), rd!(-5)); + let (p3, p2, p1, p0) = (rd!(-4), rd!(-3), rd!(-2), rd!(-1)); + let (q0, q1, q2, q3) = (rd!(0), rd!(1), rd!(2), rd!(3)); + let (q4, q5, q6, q7) = (rd!(4), rd!(5), rd!(6), rd!(7)); + put!( + -7, + ((p7 * 7 + p6 * 2 + p5 + p4 + p3 + p2 + p1 + p0 + q0 + 8) >> 4) as u8 + ); + put!( + -6, + ((p7 * 6 + p6 + p5 * 2 + p4 + p3 + p2 + p1 + p0 + q0 + q1 + 8) >> 4) as u8 + ); + put!( + -5, + ((p7 * 5 + p6 + p5 + p4 * 2 + p3 + p2 + p1 + p0 + q0 + q1 + q2 + 8) >> 4) as u8 + ); + put!( + -4, + ((p7 * 4 + p6 + p5 + p4 + p3 * 2 + p2 + p1 + p0 + q0 + q1 + q2 + q3 + 8) >> 4) as u8 + ); + put!( + -3, + ((p7 * 3 + p6 + p5 + p4 + p3 + p2 * 2 + p1 + p0 + q0 + q1 + q2 + q3 + q4 + 8) >> 4) + as u8 + ); + put!( + -2, + ((p7 * 2 + p6 + p5 + p4 + p3 + p2 + p1 * 2 + p0 + q0 + q1 + q2 + q3 + q4 + q5 + 8) >> 4) + as u8 + ); + put!( + -1, + ((p7 + p6 + p5 + p4 + p3 + p2 + p1 + p0 * 2 + q0 + q1 + q2 + q3 + q4 + q5 + q6 + 8) + >> 4) as u8 + ); + put!( + 0, + ((p6 + p5 + p4 + p3 + p2 + p1 + p0 + q0 * 2 + q1 + q2 + q3 + q4 + q5 + q6 + q7 + 8) + >> 4) as u8 + ); + put!( + 1, + ((p5 + p4 + p3 + p2 + p1 + p0 + q0 + q1 * 2 + q2 + q3 + q4 + q5 + q6 + q7 * 2 + 8) >> 4) + as u8 + ); + put!( + 2, + ((p4 + p3 + p2 + p1 + p0 + q0 + q1 + q2 * 2 + q3 + q4 + q5 + q6 + q7 * 3 + 8) >> 4) + as u8 + ); + put!( + 3, + ((p3 + p2 + p1 + p0 + q0 + q1 + q2 + q3 * 2 + q4 + q5 + q6 + q7 * 4 + 8) >> 4) as u8 + ); + put!( + 4, + ((p2 + p1 + p0 + q0 + q1 + q2 + q3 + q4 * 2 + q5 + q6 + q7 * 5 + 8) >> 4) as u8 + ); + put!( + 5, + ((p1 + p0 + q0 + q1 + q2 + q3 + q4 + q5 * 2 + q6 + q7 * 6 + 8) >> 4) as u8 + ); + put!( + 6, + ((p0 + q0 + q1 + q2 + q3 + q4 + q5 + q6 * 2 + q7 * 7 + 8) >> 4) as u8 + ); } - // 15-tap filter [1 x 7, 2, 1 x 7] (filter16, weights verbatim). - let (p7, p6, p5, p4) = (rd!(-8), rd!(-7), rd!(-6), rd!(-5)); - let (p3, p2, p1, p0) = (rd!(-4), rd!(-3), rd!(-2), rd!(-1)); - let (q0, q1, q2, q3) = (rd!(0), rd!(1), rd!(2), rd!(3)); - let (q4, q5, q6, q7) = (rd!(4), rd!(5), rd!(6), rd!(7)); - put!(-7, ((p7 * 7 + p6 * 2 + p5 + p4 + p3 + p2 + p1 + p0 + q0 + 8) >> 4) as u8); - put!(-6, ((p7 * 6 + p6 + p5 * 2 + p4 + p3 + p2 + p1 + p0 + q0 + q1 + 8) >> 4) as u8); - put!(-5, ((p7 * 5 + p6 + p5 + p4 * 2 + p3 + p2 + p1 + p0 + q0 + q1 + q2 + 8) >> 4) as u8); - put!(-4, ((p7 * 4 + p6 + p5 + p4 + p3 * 2 + p2 + p1 + p0 + q0 + q1 + q2 + q3 + 8) >> 4) as u8); - put!(-3, ((p7 * 3 + p6 + p5 + p4 + p3 + p2 * 2 + p1 + p0 + q0 + q1 + q2 + q3 + q4 + 8) >> 4) as u8); - put!(-2, ((p7 * 2 + p6 + p5 + p4 + p3 + p2 + p1 * 2 + p0 + q0 + q1 + q2 + q3 + q4 + q5 + 8) >> 4) as u8); - put!(-1, ((p7 + p6 + p5 + p4 + p3 + p2 + p1 + p0 * 2 + q0 + q1 + q2 + q3 + q4 + q5 + q6 + 8) >> 4) as u8); - put!(0, ((p6 + p5 + p4 + p3 + p2 + p1 + p0 + q0 * 2 + q1 + q2 + q3 + q4 + q5 + q6 + q7 + 8) >> 4) as u8); - put!(1, ((p5 + p4 + p3 + p2 + p1 + p0 + q0 + q1 * 2 + q2 + q3 + q4 + q5 + q6 + q7 * 2 + 8) >> 4) as u8); - put!(2, ((p4 + p3 + p2 + p1 + p0 + q0 + q1 + q2 * 2 + q3 + q4 + q5 + q6 + q7 * 3 + 8) >> 4) as u8); - put!(3, ((p3 + p2 + p1 + p0 + q0 + q1 + q2 + q3 * 2 + q4 + q5 + q6 + q7 * 4 + 8) >> 4) as u8); - put!(4, ((p2 + p1 + p0 + q0 + q1 + q2 + q3 + q4 * 2 + q5 + q6 + q7 * 5 + 8) >> 4) as u8); - put!(5, ((p1 + p0 + q0 + q1 + q2 + q3 + q4 + q5 * 2 + q6 + q7 * 6 + 8) >> 4) as u8); - put!(6, ((p0 + q0 + q1 + q2 + q3 + q4 + q5 + q6 * 2 + q7 * 7 + 8) >> 4) as u8); } /// Everything the edge pass needs, recorded during decode. @@ -210,24 +290,53 @@ impl LfGrids { ) { let mc2 = self.mi_cols * 2; let mr2 = self.mi_rows * 2; + if std::env::var_os("LFGRID").is_some() { + for r in 0..self.mi_rows { + for c in 0..self.mi_cols { + let cell = r * self.mi_cols + c; + println!( + "G {} {} {} {} {}", + r, c, self.blk[cell].0, self.level8[cell], self.otx[cell] + ); + } + } + } // Luma, vertical edges (4x4 column boundaries). for r4 in 0..mr2 { for c4 in 1..mc2 { let cell = (r4 / 2) * self.mi_cols + c4 / 2; - let (_bsize, bor, boc) = self.blk[cell]; let level = self.level8[cell]; if level == 0 { continue; } - let tx = self.otx[cell] as usize; - let mut taps = if c4 == boc as usize * 2 { - match tx { - TX_16X16 | TX_32X32 => 16, - TX_8X8 => 8, - _ if c4 % 8 == 0 => 8, - _ => 4, + // The masks are per-MI: every MI of a block contributes + // its own left edge (`left_64x64_txform_mask` marks every + // MI column for TX_8X8, every other for TX_16X16, every + // fourth for TX_32X32), so internal MI boundaries of wide + // blocks are filtered too — not just the block origin. + let mut taps = if c4 % 2 == 0 { + let tx = self.otx[cell] as usize; + if c4 % 8 == 0 { + match tx { + TX_16X16 | TX_32X32 => 16, + _ => 8, + } + } else if c4 % 4 == 0 { + match tx { + TX_32X32 => continue, + TX_16X16 => 16, + TX_8X8 => 8, + _ => 4, + } + } else { + match tx { + TX_8X8 => 8, + TX_4X4 => 4, + _ => continue, + } } } else if self.tx4[r4 * mc2 + c4] as usize == TX_4X4 { + // mask_4x4_int: internal 4x4 edge at the half position. 4 } else { continue; @@ -240,6 +349,14 @@ impl LfGrids { let off = r4 * 4 * stride + c4 * 4; if y_w >= c4 * 4 + 4 { lpf_edge(y, off, stride, 1, taps, bl, li, th); + if std::env::var_os("LFTRACE").is_some() { + let kind = match taps { + 16 => "V16", + 8 => "V8", + _ => "V4", + }; + println!("{kind} {} {}", c4 * 4, r4 * 4); + } } } } @@ -247,20 +364,38 @@ impl LfGrids { for r4 in 1..mr2 { for c4 in 0..mc2 { let cell = (r4 / 2) * self.mi_cols + c4 / 2; - let (_bsize, bor, _boc) = self.blk[cell]; let level = self.level8[cell]; if level == 0 { continue; } - let tx = self.otx[cell] as usize; - let mut taps = if r4 == bor as usize * 2 { - match tx { - TX_16X16 | TX_32X32 => 16, - TX_8X8 => 8, - _ if r4 % 8 == 0 => 8, - _ => 4, + // Horizontal mirror of the vertical rule: the above-masks + // mark every MI row for TX_8X8 (`above_64x64_txform_mask`), + // every other row for TX_16X16, every fourth for TX_32X32. + let mut taps = if r4 % 2 == 0 { + let tx = self.otx[cell] as usize; + if r4 % 8 == 0 { + match tx { + TX_16X16 | TX_32X32 => 16, + _ => 8, + } + } else if r4 % 4 == 0 { + match tx { + TX_32X32 => continue, + TX_16X16 => 16, + TX_8X8 => 8, + _ => 4, + } + } else { + match tx { + TX_8X8 => 8, + TX_4X4 => 4, + _ => continue, + } } } else if self.tx4[r4 * mc2 + c4] as usize == TX_4X4 { + // mask_4x4_int: internal 4x4 edge at the half position + // (filter_selectively_horiz fires it alongside 8/4-tap + // top edges and alone when no top edge exists). 4 } else { continue; @@ -272,6 +407,14 @@ impl LfGrids { let off = r4 * 4 * stride + c4 * 4; if y_h >= r4 * 4 + 4 { lpf_edge(y, off, stride, 0, taps, bl, li, th); + if std::env::var_os("LFTRACE").is_some() { + let kind = match taps { + 16 => "H16", + 8 => "H8", + _ => "H4", + }; + println!("{kind} {} {}", c4 * 4, r4 * 4); + } } } } diff --git a/crates/ec-vp9/tests/scratch_lfdump.rs b/crates/ec-vp9/tests/scratch_lfdump.rs new file mode 100644 index 00000000..e7624e71 --- /dev/null +++ b/crates/ec-vp9/tests/scratch_lfdump.rs @@ -0,0 +1,110 @@ +//! [scratch] Loop-filter diff map: decode key-320 frame 0 with and without +//! the loop filter (EC_VP9_SKIP_LF=1), print where we disagree with ffmpeg. + +mod ivf; + +use ec_vp9::decode::Decoder; + +fn ffmpeg_raw_yuv(path: &std::path::Path) -> Vec { + let out = std::process::Command::new("ffmpeg") + .args(["-v", "error", "-i"]) + .arg(path) + .args(["-f", "rawvideo", "-pix_fmt", "yuv420p", "-"]) + .output() + .expect("ffmpeg on PATH"); + assert!(out.status.success()); + out.stdout +} + +#[test] +fn dump_lf_diff_map() { + let path = std::path::Path::new(env!("CARGO_MANIFEST_DIR")) + .join("../../fixtures/vp9/key-320.ivf"); + let bytes = std::fs::read(&path).unwrap(); + let (_f, _w, _h, frames) = ivf::parse_ivf(&bytes); + let reference = ffmpeg_raw_yuv(&path); + + let (w, h) = (320usize, 240usize); + let ysz = w * h; + + // Pre-LF run. + // SAFETY: single-threaded test process; no other env readers run here. + unsafe { std::env::set_var("EC_VP9_SKIP_LF", "1") }; + let mut dec = Decoder::new(); + let pic_pre = dec.decode(&frames[0].data).expect("decode").expect("shown"); + std::fs::write("/tmp/y_pre_ours.raw", &pic_pre.y[..ysz]).unwrap(); + unsafe { std::env::remove_var("EC_VP9_SKIP_LF") }; + + // Post-LF run (fresh decoder). + let mut dec = Decoder::new(); + let pic_post = dec.decode(&frames[0].data).expect("decode").expect("shown"); + std::fs::write("/tmp/y_post_ours.raw", &pic_post.y[..ysz]).unwrap(); + + let ff = &reference[..ysz]; + let pre = &pic_pre.y[..ysz]; + let post = &pic_post.y[..ysz]; + + let pre_diffs: Vec = + (0..ysz).filter(|&i| pre[i] != ff[i]).collect(); + let post_diffs: Vec = + (0..ysz).filter(|&i| post[i] != ff[i]).collect(); + let lf_changed_ours: Vec = + (0..ysz).filter(|&i| pre[i] != post[i]).collect(); + println!("pre-LF diffs vs ffmpeg : {}", pre_diffs.len()); + println!("post-LF diffs vs ffmpeg: {}", post_diffs.len()); + println!("pixels our LF changed : {}", lf_changed_ours.len()); + for &i in post_diffs.iter().take(30) { + println!( + " POST diff ({:3},{:3}) ours {:3} ff {:3} (pre-ours {:3})", + i % w, + i / w, + post[i], + ff[i], + pre[i] + ); + } + for &i in pre_diffs.iter().take(10) { + println!( + " PRE diff ({:3},{:3}) ours {:3} ff {:3}", + i % w, + i / w, + pre[i], + ff[i] + ); + } + + // ASCII map of the top-left 48x32: mark post-LF diffs (#), pre-LF diffs + // (?), pixels only our LF touched (o). + println!("map rows 0..32, cols 0..48 (#=post diff, ?=pre-only, o=our-LF-changed)"); + for y in 0..32 { + let mut line = String::new(); + for x in 0..48 { + let i = y * w + x; + if post_diffs.binary_search(&i).is_ok() { + line.push('#'); + } else if pre_diffs.binary_search(&i).is_ok() { + line.push('?'); + } else if lf_changed_ours.binary_search(&i).is_ok() { + line.push('o'); + } else { + line.push('.'); + } + } + println!("{line}"); + } + // Column/row histograms of post-LF diffs (first 24 of each). + let mut cols = vec![0usize; w]; + let mut rows = vec![0usize; h]; + for &i in &post_diffs { + cols[i % w] += 1; + rows[i / w] += 1; + } + println!( + "diff columns>0: {:?}", + (0..w).filter(|&c| cols[c] > 0).take(24).collect::>() + ); + println!( + "diff rows>0 : {:?}", + (0..h).filter(|&r| rows[r] > 0).take(24).collect::>() + ); +} diff --git a/lanes/vp9kf.report.md b/lanes/vp9kf.report.md index 7e095f60..852d2c7e 100644 --- a/lanes/vp9kf.report.md +++ b/lanes/vp9kf.report.md @@ -238,3 +238,86 @@ EC_VP9_FORCE_UH/HSZ, scratch_* tests, and the EC_VP9_PROBE_TU/STAGE/CTX debug prints in decode.rs/intra.rs. Oracle: /tmp/vp9loss/{libvpx-src,drv}; /tmp/vp9loss/d45probe2.c proves SIMD==C for the specialized 4x4 kernels. + +## HANDOFF #5 (Vp9Lf, 2026-09-14) — loopfilter semantics fixed (3 root causes); first diff now (76,2); NEW dominant bug is decode-side at (88,24) + +### Fixed this round (loopfilter.rs, all proven against the instrumented oracle) + +1. **Per-MI edge masks (was: block-origin only).** libvpx builds masks per-MI — + every MI of a wide block carries its own left/top edge + (`left/above_64x64_txform_mask`: every MI col for TX_8X8, every 2nd for + TX_16X16, every 4th for TX_32X32). We skipped internal MI boundaries of + wide blocks via the tx4 grid. Vertical+horizontal loops now derive taps + per-MI: c4%8==0 → 16-tap for TX_16X16/32 else 8; c4%4==0 → 16/8/4 by tx + (TX_32X32 none); else 8-px → 8/4; odd → 4 iff tx4==TX_4X4 (mask_4x4_int). + **Luma edge set now byte-identical to the oracle's: 3044/3044 anchors** + (kind+position, LFTRACE diff — 0 missing, 0 extra). +2. **lpf_edge line coverage (the (7,1) root cause).** The kernel filters ONE + pixel-line but was called once per 4x4 chunk — only every 4th row/col was + ever filtered (row 0 changed, rows 1-3 never). lpf_edge now loops the 4 + lines (the `s += pitch` walk inside vpx_lpf_*_c). Post diffs 1219 → 696. +3. **flat2 = flat_mask5 shifted-parameter semantics.** With the C call + `flat_mask5(1, s[-8], s[-7], s[-6], s[-5], p0, q0, s[4], s[5], s[6], s[7])` + the tests are: p7..p4 and q4..q6 vs **p0**, but the outermost q7 vs **q0**; + q0..q3 and p1..p3 are never tested. We had a bogus |q0-p0| clause and + compared q7 against p0. + +### Remaining loopfilter diff (8 pixels) + +Row 2 only, cols 76-86: ours 47,47,47,49,49,50,50,48 vs vpx/ffmpeg +48,48,48,48,48,49,49,49. Zone = V16 anchor (80,0) + H4 anchors at y=4 +(H4@y=4 writes rows 2..5; p1 row = 2). V-only python sim of the trace +matches vpx on rows 1,3 — the residual is in H-pass behavior on post-V +bytes around these anchors. Next: instrument H4@(72..84,4) mask/hev +verdicts on post-V bytes; re-check the filter_selectively_horiz 8-branch +int-alongside handling and the H4 mask's p3..q3 column bytes. + +### NEW dominant bug — decode diverges BEFORE the loop filter + +Pre-LF planes (EC_VP9_SKIP_LF=1) vs oracle SKIPLF dump: **684 differing +bytes, first at pixel (88,24): ours 78, vpx 48** (region top-left of that is +byte-exact — (7,1) pre matched 75=75). This is ~block 207+ — exactly where +HANDOFF #4's per-block verification stopped. The loop filter is exonerated +for the remaining mass: edge grid/levels/kinds/positions match the oracle +exactly, so 688 of the 696 post diffs are these pre-LF bytes flowing +through. Chase with the HANDOFF #3 protocol: oracle TOK/TXB diff from +block ~207 (MI row 3, col 11 area). + +### Reusable verification assets built this round + +- `EC_VP9_SKIP_LF=1` env (decode.rs): decode without loop filter. +- `LFTRACE=1` on OUR side: prints `V4|V8|V16|H4|H8|H16 x y` per luma + lpf_edge call (loopfilter.rs, env-gated; removable after green). +- Oracle `/tmp/vp9loss/libvpx-src` is PATCHED: `LFTRACE=1` prints the same + anchor format per vpx_lpf_* call site (filter_selectively_vert_row2 + + filter_selectively_horiz, luma-gated via ss00/ss11 entry hooks); LFINIT + prints stride/border; `drv` gained `SKIPLF=1` (VP9_SET_SKIP_LOOP_FILTER) + and `YDUMP=` (raw Y-plane dump). `/tmp` is volatile — if lost, + rebuild libvpx then re-apply: the patch is mechanical (globals + one + lf_tr() helper + getenv hook in loop_filter_rows + brace-wrapped call + sites), ~40 lines; git checkout the file first. +- `tests/scratch_lfdump.rs`: dumps `/tmp/y_{pre,post}_ours.raw`, prints + diff maps vs ffmpeg. Compare against `drv SKIPLF=1 YDUMP=...` (pre) and + plain `YDUMP=...` (post). ffmpeg Y frame 0 == libvpx Y frame 0 (verified + byte-equal), so either is a valid oracle target. + +### Trace-reading pitfalls learned (do not re-learn) + +- The oracle LF runs via `vp9_loop_filter_frame_mt` → loop_filter_rows fires + once per worker CHUNK (3x for this frame, same buffer) — LFINIT repeats + are chunking, not multi-pass; anchors union to the single-pass set. +- Luma-gate the oracle trace (chroma u/v buffers sit ~300 "rows" past the Y + base and poison the x/y decomposition). +- libvpx vertical anchors sit on 8-px columns EXCEPT mask_4x4_int anchors at + +4 (x%8==4) — don't mistake them for chroma. +- One vpx_lpf_* call covers 8 lines; our lpf_edge covers 4 — expand vpx + anchors by +4 (rows for V, cols for H) before set-diffing. +- cargo test swallows eprintln unless `-- --nocapture`; byte dumps are the + robust channel. +- VP8's filter_mask and VP9's are IDENTICAL in vpx_dsp (abs(p0-q0)*2 + + abs(p1-q1)/2) — don't "fix" that; the flat_mask5 asymmetry IS real. + +**State at yield:** lib 8/8 PASS; lossless_64, inter_is_named_unsupported, +profile1_444 all PASS; keyframes_match_ffmpeg FAIL, first diff (76,2) +ours 47 ref 48. NOT pushed (gate not green). Branch lane-vp9-kf has the +three loopfilter fixes + probe hooks + scratch_lfdump.