diff --git a/crates/ec-vp9/src/decode.rs b/crates/ec-vp9/src/decode.rs index e36a1cbf..5070a574 100644 --- a/crates/ec-vp9/src/decode.rs +++ b/crates/ec-vp9/src/decode.rs @@ -242,8 +242,7 @@ impl Decoder { let mut ti = 0usize; for tr in 0..tile_rows { let row_lo = ((sb64_rows * tr) >> hdr.tile_info.rows_log2) * SB_MI; - let row_hi = - (((sb64_rows * (tr + 1)) >> hdr.tile_info.rows_log2) * SB_MI).min(mi_rows); + let row_hi = (((sb64_rows * (tr + 1)) >> hdr.tile_info.rows_log2) * SB_MI).min(mi_rows); for tc in 0..tile_cols { let col_lo = ((sb64_cols * tc) >> hdr.tile_info.cols_log2) * SB_MI; let col_hi = @@ -709,19 +708,7 @@ impl Decoder { // 1. Intra prediction (reads reconstructed neighbours). { let (data, _) = planes.plane(plane); - build_intra_predictors( - data, - stride, - pw, - ph, - x0, - y0, - mode, - bs, - up, - lft, - rgt, - ); + build_intra_predictors(data, stride, pw, ph, x0, y0, mode, bs, up, lft, rgt); } if info.skip { @@ -764,6 +751,19 @@ impl Decoder { ctx_in ); } + if crate::trace_enabled() { + eprintln!( + "TXB p={} x={} y={} tx={} a={} l={} deq={},{}", + plane, + x0, + y0, + tx_size, + ectx[plane].above[ax], + ectx[plane].left[ay], + dequant[0], + dequant[1] + ); + } let coef = decode_coefs( r, ptype, diff --git a/crates/ec-vp9/src/intra.rs b/crates/ec-vp9/src/intra.rs index 1983c01f..715cb735 100644 --- a/crates/ec-vp9/src/intra.rs +++ b/crates/ec-vp9/src/intra.rs @@ -158,13 +158,26 @@ pub(crate) fn build_intra_predictors( "STAGE x0={x0} y0={y0} mode={mode} bs={bs} above={:?} cor={} left={:?}", &above_data[A..A + 8], above_data[A - 1], - &left_col[..4] + &left_col[..8] ); } let dst = &mut data[y0 * stride + x0..]; dispatch( mode, bs, dst, stride, &above_data, &left_col, up_available, left_available, ); + if crate::trace_enabled() + && std::env::var("EC_VP9_PROBE_TU") + .ok() + .and_then(|s| { + let mut it = s.split(','); + Some((it.next()?.parse::().ok()?, it.next()?.parse().ok()?)) + }) + .is_some_and(|(px, py)| x0 == px && y0 == py) + { + for rr in 0..bs { + eprintln!("PRED r={rr} {:?}", &dst[rr * stride..rr * stride + bs]); + } + } } fn dispatch( @@ -325,7 +338,7 @@ fn dispatch( } for r in 1..bs { for c in 0..bs - 2 { - dst[(r + 1) * stride + 2 + c] = dst[r * stride + c]; + dst[r * stride + 2 + c] = dst[(r - 1) * stride + c]; } } } diff --git a/crates/ec-vp9/src/loopfilter.rs b/crates/ec-vp9/src/loopfilter.rs index dd518e8f..f177a973 100644 --- a/crates/ec-vp9/src/loopfilter.rs +++ b/crates/ec-vp9/src/loopfilter.rs @@ -69,16 +69,14 @@ fn lpf_edge( blimit: i32, limit: i32, thresh: i32, + nlines: isize, ) { // `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 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 { + // luma: 4 lines per 4x4; chroma ss11: 8 lines matching vpx_lpf_*_c. + for line in 0..nlines { let edge = off as isize + line * line_step; macro_rules! rd { ($k:expr) => { @@ -144,17 +142,16 @@ fn lpf_edge( 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. + // flat_mask5(1, s[-8], s[-7], s[-6], s[-5], p0, q0, s[4], s[5], s[6], s[7]): + // |p7..p4 - p0| and |q4..q7 - q0| (the C names are shifted; q4..q6 + // are the q1..q3 args of the inner flat_mask4 and compare to q0). 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!(4) - rd!(0)).abs() <= 1 + && (rd!(5) - rd!(0)).abs() <= 1 + && (rd!(6) - rd!(0)).abs() <= 1 && (rd!(7) - rd!(0)).abs() <= 1; if !flat2 { let (p3, p2, p1, p0, q0, q1, q2, q3) = ( @@ -348,7 +345,7 @@ impl LfGrids { let (bl, li, th) = limits(level, sharpness); let off = r4 * 4 * stride + c4 * 4; if y_w >= c4 * 4 + 4 { - lpf_edge(y, off, stride, 1, taps, bl, li, th); + lpf_edge(y, off, stride, 1, taps, bl, li, th, 4); if std::env::var_os("LFTRACE").is_some() { let kind = match taps { 16 => "V16", @@ -406,7 +403,7 @@ impl LfGrids { let (bl, li, th) = limits(level, sharpness); let off = r4 * 4 * stride + c4 * 4; if y_h >= r4 * 4 + 4 { - lpf_edge(y, off, stride, 0, taps, bl, li, th); + lpf_edge(y, off, stride, 0, taps, bl, li, th, 4); if std::env::var_os("LFTRACE").is_some() { let kind = match taps { 16 => "H16", @@ -418,13 +415,15 @@ impl LfGrids { } } } - // Chroma (4:2:0): one 4x4 chroma TU per MI, so the only filtered - // edges are the MI boundaries (vp9_filter_block_plane_ss11): - // vertical on every MI column boundary, horizontal on every MI - // row boundary, taps from the plane's uv transform size. + // Chroma 4:2:0 (vp9_filter_block_plane_ss11): MI is 4 chroma + // pixels. Edges follow uv tx size, not every MI — TX_8 every 8 + // chroma px (2 MI), TX_16/32 every 16/32. TX_4 hits every MI + // (int_4x4_uv is those odd columns). for data in [&mut *u, &mut *v] { - // Vertical: left edge of each MI (except the leftmost column). for r in 0..self.mi_rows { + if r % 2 != 0 { + continue; + } for c in 1..self.mi_cols { let cell = r * self.mi_cols + c; let (bsize, _bor, _boc) = self.blk[cell]; @@ -433,22 +432,27 @@ impl LfGrids { continue; } let uv_tx = uv_txsize_lookup(bsize as usize, self.otx[cell] as usize); + let step = 1usize << uv_tx; + if c % step != 0 { + continue; + } let taps = match uv_tx { - TX_16X16 if c * 4 >= 8 && c * 4 + 8 <= uv_w => 16, - TX_16X16 => 8, - TX_8X8 => 8, + TX_16X16 | TX_32X32 if c * 4 >= 8 && c * 4 + 8 <= uv_w => 16, + TX_16X16 | TX_32X32 | TX_8X8 => 8, _ => 4, }; let (bl, li, th) = limits(level, sharpness); let off = r * 4 * uv_stride + c * 4; if uv_w >= c * 4 + 4 { - lpf_edge(data, off, uv_stride, 1, taps, bl, li, th); + lpf_edge(data, off, uv_stride, 1, taps, bl, li, th, 8); } } } - // Horizontal: top edge of each MI (except the top row). for r in 1..self.mi_rows { for c in 0..self.mi_cols { + if c % 2 != 0 { + continue; + } let cell = r * self.mi_cols + c; let (bsize, _bor, _boc) = self.blk[cell]; let level = self.level8[cell]; @@ -456,16 +460,19 @@ impl LfGrids { continue; } let uv_tx = uv_txsize_lookup(bsize as usize, self.otx[cell] as usize); + let step = 1usize << uv_tx; + if r % step != 0 { + continue; + } let taps = match uv_tx { - TX_16X16 if r * 4 >= 8 && r * 4 + 8 <= uv_h => 16, - TX_16X16 => 8, - TX_8X8 => 8, + TX_16X16 | TX_32X32 if r * 4 >= 8 && r * 4 + 8 <= uv_h => 16, + TX_16X16 | TX_32X32 | TX_8X8 => 8, _ => 4, }; let (bl, li, th) = limits(level, sharpness); let off = r * 4 * uv_stride + c * 4; if uv_h >= r * 4 + 4 { - lpf_edge(data, off, uv_stride, 0, taps, bl, li, th); + lpf_edge(data, off, uv_stride, 0, taps, bl, li, th, 8); } } } diff --git a/crates/ec-vp9/src/modes.rs b/crates/ec-vp9/src/modes.rs index 249dd315..7fa5db7e 100644 --- a/crates/ec-vp9/src/modes.rs +++ b/crates/ec-vp9/src/modes.rs @@ -262,5 +262,11 @@ pub(crate) fn read_intra_frame_mode_info( // uv mode: `vp9_kf_uv_mode_prob` indexed by y mode, decoded with the // SAME intra_mode_tree (decodemv.c:232 calls read_intra_mode). mi.uv_mode = r.read_tree(&INTRA_MODE_TREE, &kf_uv_probs(mi.mode)); + if crate::trace_enabled() { + eprintln!( + "MODE row={} col={} bsize={} skip={} tx={} y={} uv={}", + row, col, sb_type, mi.skip as u8, mi.tx_size, mi.mode, mi.uv_mode + ); + } Ok(mi) } diff --git a/crates/ec-vp9/src/tokens.rs b/crates/ec-vp9/src/tokens.rs index e6a2e74b..1859b72d 100644 --- a/crates/ec-vp9/src/tokens.rs +++ b/crates/ec-vp9/src/tokens.rs @@ -80,6 +80,9 @@ pub(crate) fn decode_coefs( } while c < max_eob { + if crate::trace_enabled() { + eprintln!("TOK c={c} band={band} ctx={ctx} p0={},{},{}", prob[0], prob[1], prob[2]); + } if !r.read_bool(prob[0]) { break; } @@ -146,7 +149,7 @@ pub(crate) fn decode_coefs( coef.eob = c; if crate::trace_enabled() { eprintln!( - "C p={} x={} y={} tx={} eob={} c0={}", + "EOB p={} x={} y={} tx={} eob={} c0={}", plane, pos.0, pos.1, tx_size, c, coef.coeffs[0] ); } diff --git a/crates/ec-vp9/src/transform.rs b/crates/ec-vp9/src/transform.rs index 6d4da4ac..9e0b8661 100644 --- a/crates/ec-vp9/src/transform.rs +++ b/crates/ec-vp9/src/transform.rs @@ -417,7 +417,24 @@ pub(crate) fn idct32(input: &[i32], output: &mut [i32]) { step1[13] = step2[2] - step2[13]; step1[14] = step2[1] - step2[14]; step1[15] = step2[0] - step2[15]; - step1[16..32].copy_from_slice(&step2[16..32]); + // stage 8 (inv_txfm.c:1120): cospi_16 rotation of 20..27 before the + // final combine — omitting it desyncs every 32x32 residual. + step1[16] = step2[16]; + step1[17] = step2[17]; + step1[18] = step2[18]; + step1[19] = step2[19]; + step1[20] = dcrs((-g(step2[20]) + g(step2[27])) * C16); + step1[27] = dcrs((g(step2[20]) + g(step2[27])) * C16); + step1[21] = dcrs((-g(step2[21]) + g(step2[26])) * C16); + step1[26] = dcrs((g(step2[21]) + g(step2[26])) * C16); + step1[22] = dcrs((-g(step2[22]) + g(step2[25])) * C16); + step1[25] = dcrs((g(step2[22]) + g(step2[25])) * C16); + step1[23] = dcrs((-g(step2[23]) + g(step2[24])) * C16); + step1[24] = dcrs((g(step2[23]) + g(step2[24])) * C16); + step1[28] = step2[28]; + step1[29] = step2[29]; + step1[30] = step2[30]; + step1[31] = step2[31]; for i in 0..16 { output[i] = step1[i] + step1[31 - i]; output[31 - i] = step1[i] - step1[31 - i]; diff --git a/crates/ec-vp9/tests/scratch_lfdump.rs b/crates/ec-vp9/tests/scratch_lfdump.rs index e7624e71..672bd9db 100644 --- a/crates/ec-vp9/tests/scratch_lfdump.rs +++ b/crates/ec-vp9/tests/scratch_lfdump.rs @@ -18,8 +18,8 @@ fn ffmpeg_raw_yuv(path: &std::path::Path) -> Vec { #[test] fn dump_lf_diff_map() { - let path = std::path::Path::new(env!("CARGO_MANIFEST_DIR")) - .join("../../fixtures/vp9/key-320.ivf"); + 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); @@ -44,12 +44,9 @@ fn dump_lf_diff_map() { 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(); + 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()); @@ -107,4 +104,30 @@ fn dump_lf_diff_map() { "diff rows>0 : {:?}", (0..h).filter(|&r| rows[r] > 0).take(24).collect::>() ); + let uv = (w / 2) * (h / 2); + let ff_u = &reference[ysz..ysz + uv]; + let ff_v = &reference[ysz + uv..ysz + 2 * uv]; + let uw = w / 2; + let u_d: Vec<_> = (0..uv).filter(|&i| pic_post.u[i] != ff_u[i]).collect(); + let v_d: Vec<_> = (0..uv).filter(|&i| pic_post.v[i] != ff_v[i]).collect(); + println!("U post diffs: {}", u_d.len()); + for &i in u_d.iter().take(12) { + println!( + " U ({}, {}) ours {} ff {}", + i % uw, + i / uw, + pic_post.u[i], + ff_u[i] + ); + } + println!("V post diffs: {}", v_d.len()); + for &i in v_d.iter().take(12) { + println!( + " V ({}, {}) ours {} ff {}", + i % uw, + i / uw, + pic_post.v[i], + ff_v[i] + ); + } } diff --git a/lanes/vp9kf.report.md b/lanes/vp9kf.report.md index 852d2c7e..ea995c1c 100644 --- a/lanes/vp9kf.report.md +++ b/lanes/vp9kf.report.md @@ -321,3 +321,21 @@ block ~207 (MI row 3, col 11 area). 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. + +## HANDOFF #6 (main session after flash 429, 2026-09-14) + +Pre-LF Y vs libvpx SKIPLF: **0 diffs** (the 684 figure was ffmpeg-with-LF vs our SKIP_LF). +Post-LF Y vs ffmpeg: **0 diffs**. + +Fixes this round: +1. generic d153 wrote `dst[(r+1)*stride + 2+c]` so row 1 leaked 128s. Now `dst[r*stride+2+c] = dst[(r-1)*stride+c]` (vpx `dst[r][2+c] = dst[r-1][c]`). +2. idct32 was missing vpx stage 8 (`inv_txfm.c:1120`) cospi_16 rotation of 20..27. +3. **flat2 was wrong.** `flat_mask5(1, s[-8]..s[-5], p0, q0, s[4]..s[7])` compares q4..q7 to **q0**, not p0. HANDOFF #5's "q4..q6 vs p0" misread the C parameter names. That was the 8 Y pixels at row 2 cols 76-86 (V16 at x=80 fell into filter8). +4. Chroma was filtering every MI (4 px) regardless of uv tx. Now step `1<