forge
Explore
Log in

Don't you dare go Hollow

vp9-kf: loopfilter per-MI masks, 4-line kernel walk, flat_mask5 semantics

Browse files at this commit

9769de27e932399a41316f1971d823dd236261a6Ahmet Kaan GümüşParent: 7ed859c
- luma edge masks per-MI (internal MI boundaries of wide blocks filter;
  left/above_64x64_txform_mask rules), luma edge set now matches the
  instrumented-libvpx trace 3044/3044 anchors
- lpf_edge walks the 4 pixel-lines of each chunk (was filtering only
  every 4th row/col)
- flat2 follows flat_mask5's shifted params: q7 vs q0, no |q0-p0| clause
- probes: EC_VP9_SKIP_LF, LFTRACE (ours + patched oracle), scratch_lfdump
  Y-plane dumps; HANDOFF #5 in lanes/vp9kf.report.md

keyframes_match_ffmpeg still FAIL: first diff (76,2) 47 vs 48; pre-LF
decode divergence from (88,24) tracked as the next dominant bug.
crates/ec-vp9/src/decode.rsmodified+1 -1
@@ -292,7 +292,7 @@ impl Decoder {
292 }292 }
293 293
294 // Loop filter post-pass (level 0 disables it for the frame).294 // Loop filter post-pass (level 0 disables it for the frame).
295- if hdr.loop_filter.level > 0 {295+ if hdr.loop_filter.level > 0 && std::env::var_os("EC_VP9_SKIP_LF").is_none() {
296 let p = self.planes.as_mut().expect("frame scratch");296 let p = self.planes.as_mut().expect("frame scratch");
297 grids.filter_frame(297 grids.filter_frame(
298 &mut p.y,298 &mut p.y,
crates/ec-vp9/src/loopfilter.rsmodified+179 -36
@@ -73,9 +73,23 @@ fn lpf_edge(
73 // `dir` 0 = horizontal edges (rows step by stride), 1 = vertical73 // `dir` 0 = horizontal edges (rows step by stride), 1 = vertical
74 // (columns step by 1). `taps` in {4, 8, 16}.74 // (columns step by 1). `taps` in {4, 8, 16}.
75 let step: isize = if dir == 0 { stride as isize } else { 1 };75 let step: isize = if dir == 0 { stride as isize } else { 1 };
76- let edge = off as isize;76+ let line_step: isize = if dir == 0 { 1 } else { stride as isize };
77- macro_rules! rd { ($k:expr) => { data[(edge + $k * step) as usize] as i32 } }77+ // The kernel filters a single pixel-line; one call covers a 4-pixel
78- macro_rules! put { ($k:expr, $v:expr) => { data[(edge + $k * step) as usize] = $v; } }78+ // chunk, so run it on each of the four lines crossing the edge
79+ // (every row for a vertical edge, every column for a horizontal one)
80+ // — this is the per-row `s += pitch` walk inside vpx_lpf_*_c.
81+ for line in 0..4isize {
82+ let edge = off as isize + line * line_step;
83+ macro_rules! rd {
84+ ($k:expr) => {
85+ data[(edge + $k * step) as usize] as i32
86+ };
87+ }
88+ macro_rules! put {
89+ ($k:expr, $v:expr) => {
90+ data[(edge + $k * step) as usize] = $v;
91+ };
92+ }
79 // filter_mask (vpx_dsp/loopfilter.c:34): p3..q3 gradient limits +93 // filter_mask (vpx_dsp/loopfilter.c:34): p3..q3 gradient limits +
80 // blimit; shared by the 4-, 8- and 16-tap entry points.94 // blimit; shared by the 4-, 8- and 16-tap entry points.
81 let mask = (rd!(-4) - rd!(-3)).abs() <= limit95 let mask = (rd!(-4) - rd!(-3)).abs() <= limit
@@ -93,10 +107,10 @@ fn lpf_edge(
93 ];107 ];
94 if taps == 4 {108 if taps == 4 {
95 filter4(data, idx4, thresh, mask);109 filter4(data, idx4, thresh, mask);
96- return;110+ continue;
97 }111 }
98 if !mask {112 if !mask {
99- return;113+ continue;
100 }114 }
101 // flat_mask4(1, p3..q3)115 // flat_mask4(1, p3..q3)
102 let flat = (rd!(-2) - rd!(-1)).abs() <= 1116 let flat = (rd!(-2) - rd!(-1)).abs() <= 1
@@ -109,60 +123,126 @@ fn lpf_edge(
109 if !flat {123 if !flat {
110 // plain filter4 with the real mask and hev threshold124 // plain filter4 with the real mask and hev threshold
111 filter4(data, idx4, thresh, mask);125 filter4(data, idx4, thresh, mask);
112- return;126+ continue;
113 }127 }
114 // 7-tap [1,1,1,2,1,1,1] outputs (filter8).128 // 7-tap [1,1,1,2,1,1,1] outputs (filter8).
115- let (p3, p2, p1, p0, q0, q1, q2, q3) =129+ let (p3, p2, p1, p0, q0, q1, q2, q3) = (
116- (rd!(-4), rd!(-3), rd!(-2), rd!(-1), rd!(0), rd!(1), rd!(2), rd!(3));130+ rd!(-4),
131+ rd!(-3),
132+ rd!(-2),
133+ rd!(-1),
134+ rd!(0),
135+ rd!(1),
136+ rd!(2),
137+ rd!(3),
138+ );
117 put!(-3, ((p3 * 3 + p2 * 2 + p1 + p0 + q0 + 4) >> 3) as u8);139 put!(-3, ((p3 * 3 + p2 * 2 + p1 + p0 + q0 + 4) >> 3) as u8);
118 put!(-2, ((p3 * 2 + p2 + p1 * 2 + p0 + q0 + q1 + 4) >> 3) as u8);140 put!(-2, ((p3 * 2 + p2 + p1 * 2 + p0 + q0 + q1 + 4) >> 3) as u8);
119 put!(-1, ((p3 + p2 + p1 + p0 * 2 + q0 + q1 + q2 + 4) >> 3) as u8);141 put!(-1, ((p3 + p2 + p1 + p0 * 2 + q0 + q1 + q2 + 4) >> 3) as u8);
120 put!(0, ((p2 + p1 + p0 + q0 * 2 + q1 + q2 + q3 + 4) >> 3) as u8);142 put!(0, ((p2 + p1 + p0 + q0 * 2 + q1 + q2 + q3 + 4) >> 3) as u8);
121 put!(1, ((p1 + p0 + q0 + q1 * 2 + q2 + q3 + q3 + 4) >> 3) as u8);143 put!(1, ((p1 + p0 + q0 + q1 * 2 + q2 + q3 + q3 + 4) >> 3) as u8);
122 put!(2, ((p0 + q0 + q1 + q2 * 2 + q3 + q3 + q3 + 4) >> 3) as u8);144 put!(2, ((p0 + q0 + q1 + q2 * 2 + q3 + q3 + q3 + 4) >> 3) as u8);
123- return;145+ continue;
124 }146 }
125- // 16-tap: flat2 = flat_mask5(1, p7..p4, p0, q0, q4..q7) — every147+ // flat2 = flat_mask5(1, s[-8..-5], p0, q0, s[4..7]) with the C
126- // sample compared against p0 (rd!(-1)).148+ // call's shifted parameter names: p7..p4 and q4..q6 compare
149+ // against p0, while the outermost q7 compares against q0; q0..q3
150+ // and p1..p3 are not tested at all.
127 let flat2 = (rd!(-8) - rd!(-1)).abs() <= 1151 let flat2 = (rd!(-8) - rd!(-1)).abs() <= 1
128 && (rd!(-7) - rd!(-1)).abs() <= 1152 && (rd!(-7) - rd!(-1)).abs() <= 1
129 && (rd!(-6) - rd!(-1)).abs() <= 1153 && (rd!(-6) - rd!(-1)).abs() <= 1
130 && (rd!(-5) - rd!(-1)).abs() <= 1154 && (rd!(-5) - rd!(-1)).abs() <= 1
131- && (rd!(0) - rd!(-1)).abs() <= 1
132 && (rd!(4) - rd!(-1)).abs() <= 1155 && (rd!(4) - rd!(-1)).abs() <= 1
133 && (rd!(5) - rd!(-1)).abs() <= 1156 && (rd!(5) - rd!(-1)).abs() <= 1
134 && (rd!(6) - rd!(-1)).abs() <= 1157 && (rd!(6) - rd!(-1)).abs() <= 1
135- && (rd!(7) - rd!(-1)).abs() <= 1;158+ && (rd!(7) - rd!(0)).abs() <= 1;
136 if !flat2 {159 if !flat2 {
137- let (p3, p2, p1, p0, q0, q1, q2, q3) =160+ let (p3, p2, p1, p0, q0, q1, q2, q3) = (
138- (rd!(-4), rd!(-3), rd!(-2), rd!(-1), rd!(0), rd!(1), rd!(2), rd!(3));161+ rd!(-4),
162+ rd!(-3),
163+ rd!(-2),
164+ rd!(-1),
165+ rd!(0),
166+ rd!(1),
167+ rd!(2),
168+ rd!(3),
169+ );
139 put!(-3, ((p3 * 3 + p2 * 2 + p1 + p0 + q0 + 4) >> 3) as u8);170 put!(-3, ((p3 * 3 + p2 * 2 + p1 + p0 + q0 + 4) >> 3) as u8);
140 put!(-2, ((p3 * 2 + p2 + p1 * 2 + p0 + q0 + q1 + 4) >> 3) as u8);171 put!(-2, ((p3 * 2 + p2 + p1 * 2 + p0 + q0 + q1 + 4) >> 3) as u8);
141 put!(-1, ((p3 + p2 + p1 + p0 * 2 + q0 + q1 + q2 + 4) >> 3) as u8);172 put!(-1, ((p3 + p2 + p1 + p0 * 2 + q0 + q1 + q2 + 4) >> 3) as u8);
142 put!(0, ((p2 + p1 + p0 + q0 * 2 + q1 + q2 + q3 + 4) >> 3) as u8);173 put!(0, ((p2 + p1 + p0 + q0 * 2 + q1 + q2 + q3 + 4) >> 3) as u8);
143 put!(1, ((p1 + p0 + q0 + q1 * 2 + q2 + q3 + q3 + 4) >> 3) as u8);174 put!(1, ((p1 + p0 + q0 + q1 * 2 + q2 + q3 + q3 + 4) >> 3) as u8);
144 put!(2, ((p0 + q0 + q1 + q2 * 2 + q3 + q3 + q3 + 4) >> 3) as u8);175 put!(2, ((p0 + q0 + q1 + q2 * 2 + q3 + q3 + q3 + 4) >> 3) as u8);
145- return;176+ continue;
146 }177 }
147 // 15-tap filter [1 x 7, 2, 1 x 7] (filter16, weights verbatim).178 // 15-tap filter [1 x 7, 2, 1 x 7] (filter16, weights verbatim).
148 let (p7, p6, p5, p4) = (rd!(-8), rd!(-7), rd!(-6), rd!(-5));179 let (p7, p6, p5, p4) = (rd!(-8), rd!(-7), rd!(-6), rd!(-5));
149 let (p3, p2, p1, p0) = (rd!(-4), rd!(-3), rd!(-2), rd!(-1));180 let (p3, p2, p1, p0) = (rd!(-4), rd!(-3), rd!(-2), rd!(-1));
150 let (q0, q1, q2, q3) = (rd!(0), rd!(1), rd!(2), rd!(3));181 let (q0, q1, q2, q3) = (rd!(0), rd!(1), rd!(2), rd!(3));
151 let (q4, q5, q6, q7) = (rd!(4), rd!(5), rd!(6), rd!(7));182 let (q4, q5, q6, q7) = (rd!(4), rd!(5), rd!(6), rd!(7));
152- put!(-7, ((p7 * 7 + p6 * 2 + p5 + p4 + p3 + p2 + p1 + p0 + q0 + 8) >> 4) as u8);183+ put!(
153- put!(-6, ((p7 * 6 + p6 + p5 * 2 + p4 + p3 + p2 + p1 + p0 + q0 + q1 + 8) >> 4) as u8);184+ -7,
154- put!(-5, ((p7 * 5 + p6 + p5 + p4 * 2 + p3 + p2 + p1 + p0 + q0 + q1 + q2 + 8) >> 4) as u8);185+ ((p7 * 7 + p6 * 2 + p5 + p4 + p3 + p2 + p1 + p0 + q0 + 8) >> 4) as u8
155- put!(-4, ((p7 * 4 + p6 + p5 + p4 + p3 * 2 + p2 + p1 + p0 + q0 + q1 + q2 + q3 + 8) >> 4) as u8);186+ );
156- put!(-3, ((p7 * 3 + p6 + p5 + p4 + p3 + p2 * 2 + p1 + p0 + q0 + q1 + q2 + q3 + q4 + 8) >> 4) as u8);187+ put!(
157- put!(-2, ((p7 * 2 + p6 + p5 + p4 + p3 + p2 + p1 * 2 + p0 + q0 + q1 + q2 + q3 + q4 + q5 + 8) >> 4) as u8);188+ -6,
158- put!(-1, ((p7 + p6 + p5 + p4 + p3 + p2 + p1 + p0 * 2 + q0 + q1 + q2 + q3 + q4 + q5 + q6 + 8) >> 4) as u8);189+ ((p7 * 6 + p6 + p5 * 2 + p4 + p3 + p2 + p1 + p0 + q0 + q1 + 8) >> 4) as u8
159- put!(0, ((p6 + p5 + p4 + p3 + p2 + p1 + p0 + q0 * 2 + q1 + q2 + q3 + q4 + q5 + q6 + q7 + 8) >> 4) as u8);190+ );
160- put!(1, ((p5 + p4 + p3 + p2 + p1 + p0 + q0 + q1 * 2 + q2 + q3 + q4 + q5 + q6 + q7 * 2 + 8) >> 4) as u8);191+ put!(
161- put!(2, ((p4 + p3 + p2 + p1 + p0 + q0 + q1 + q2 * 2 + q3 + q4 + q5 + q6 + q7 * 3 + 8) >> 4) as u8);192+ -5,
162- put!(3, ((p3 + p2 + p1 + p0 + q0 + q1 + q2 + q3 * 2 + q4 + q5 + q6 + q7 * 4 + 8) >> 4) as u8);193+ ((p7 * 5 + p6 + p5 + p4 * 2 + p3 + p2 + p1 + p0 + q0 + q1 + q2 + 8) >> 4) as u8
163- put!(4, ((p2 + p1 + p0 + q0 + q1 + q2 + q3 + q4 * 2 + q5 + q6 + q7 * 5 + 8) >> 4) as u8);194+ );
164- put!(5, ((p1 + p0 + q0 + q1 + q2 + q3 + q4 + q5 * 2 + q6 + q7 * 6 + 8) >> 4) as u8);195+ put!(
165- put!(6, ((p0 + q0 + q1 + q2 + q3 + q4 + q5 + q6 * 2 + q7 * 7 + 8) >> 4) as u8);196+ -4,
197+ ((p7 * 4 + p6 + p5 + p4 + p3 * 2 + p2 + p1 + p0 + q0 + q1 + q2 + q3 + 8) >> 4) as u8
198+ );
199+ put!(
200+ -3,
201+ ((p7 * 3 + p6 + p5 + p4 + p3 + p2 * 2 + p1 + p0 + q0 + q1 + q2 + q3 + q4 + 8) >> 4)
202+ as u8
203+ );
204+ put!(
205+ -2,
206+ ((p7 * 2 + p6 + p5 + p4 + p3 + p2 + p1 * 2 + p0 + q0 + q1 + q2 + q3 + q4 + q5 + 8) >> 4)
207+ as u8
208+ );
209+ put!(
210+ -1,
211+ ((p7 + p6 + p5 + p4 + p3 + p2 + p1 + p0 * 2 + q0 + q1 + q2 + q3 + q4 + q5 + q6 + 8)
212+ >> 4) as u8
213+ );
214+ put!(
215+ 0,
216+ ((p6 + p5 + p4 + p3 + p2 + p1 + p0 + q0 * 2 + q1 + q2 + q3 + q4 + q5 + q6 + q7 + 8)
217+ >> 4) as u8
218+ );
219+ put!(
220+ 1,
221+ ((p5 + p4 + p3 + p2 + p1 + p0 + q0 + q1 * 2 + q2 + q3 + q4 + q5 + q6 + q7 * 2 + 8) >> 4)
222+ as u8
223+ );
224+ put!(
225+ 2,
226+ ((p4 + p3 + p2 + p1 + p0 + q0 + q1 + q2 * 2 + q3 + q4 + q5 + q6 + q7 * 3 + 8) >> 4)
227+ as u8
228+ );
229+ put!(
230+ 3,
231+ ((p3 + p2 + p1 + p0 + q0 + q1 + q2 + q3 * 2 + q4 + q5 + q6 + q7 * 4 + 8) >> 4) as u8
232+ );
233+ put!(
234+ 4,
235+ ((p2 + p1 + p0 + q0 + q1 + q2 + q3 + q4 * 2 + q5 + q6 + q7 * 5 + 8) >> 4) as u8
236+ );
237+ put!(
238+ 5,
239+ ((p1 + p0 + q0 + q1 + q2 + q3 + q4 + q5 * 2 + q6 + q7 * 6 + 8) >> 4) as u8
240+ );
241+ put!(
242+ 6,
243+ ((p0 + q0 + q1 + q2 + q3 + q4 + q5 + q6 * 2 + q7 * 7 + 8) >> 4) as u8
244+ );
245+ }
166 }246 }
167 247
168 /// Everything the edge pass needs, recorded during decode.248 /// Everything the edge pass needs, recorded during decode.
@@ -210,24 +290,53 @@ impl LfGrids {
210 ) {290 ) {
211 let mc2 = self.mi_cols * 2;291 let mc2 = self.mi_cols * 2;
212 let mr2 = self.mi_rows * 2;292 let mr2 = self.mi_rows * 2;
293+ if std::env::var_os("LFGRID").is_some() {
294+ for r in 0..self.mi_rows {
295+ for c in 0..self.mi_cols {
296+ let cell = r * self.mi_cols + c;
297+ println!(
298+ "G {} {} {} {} {}",
299+ r, c, self.blk[cell].0, self.level8[cell], self.otx[cell]
300+ );
301+ }
302+ }
303+ }
213 // Luma, vertical edges (4x4 column boundaries).304 // Luma, vertical edges (4x4 column boundaries).
214 for r4 in 0..mr2 {305 for r4 in 0..mr2 {
215 for c4 in 1..mc2 {306 for c4 in 1..mc2 {
216 let cell = (r4 / 2) * self.mi_cols + c4 / 2;307 let cell = (r4 / 2) * self.mi_cols + c4 / 2;
217- let (_bsize, bor, boc) = self.blk[cell];
218 let level = self.level8[cell];308 let level = self.level8[cell];
219 if level == 0 {309 if level == 0 {
220 continue;310 continue;
221 }311 }
312+ // The masks are per-MI: every MI of a block contributes
313+ // its own left edge (`left_64x64_txform_mask` marks every
314+ // MI column for TX_8X8, every other for TX_16X16, every
315+ // fourth for TX_32X32), so internal MI boundaries of wide
316+ // blocks are filtered too — not just the block origin.
317+ let mut taps = if c4 % 2 == 0 {
222 let tx = self.otx[cell] as usize;318 let tx = self.otx[cell] as usize;
223- let mut taps = if c4 == boc as usize * 2 {319+ if c4 % 8 == 0 {
224 match tx {320 match tx {
225 TX_16X16 | TX_32X32 => 16,321 TX_16X16 | TX_32X32 => 16,
322+ _ => 8,
323+ }
324+ } else if c4 % 4 == 0 {
325+ match tx {
326+ TX_32X32 => continue,
327+ TX_16X16 => 16,
226 TX_8X8 => 8,328 TX_8X8 => 8,
227- _ if c4 % 8 == 0 => 8,
228 _ => 4,329 _ => 4,
229 }330 }
331+ } else {
332+ match tx {
333+ TX_8X8 => 8,
334+ TX_4X4 => 4,
335+ _ => continue,
336+ }
337+ }
230 } else if self.tx4[r4 * mc2 + c4] as usize == TX_4X4 {338 } else if self.tx4[r4 * mc2 + c4] as usize == TX_4X4 {
339+ // mask_4x4_int: internal 4x4 edge at the half position.
231 4340 4
232 } else {341 } else {
233 continue;342 continue;
@@ -240,6 +349,14 @@ impl LfGrids {
240 let off = r4 * 4 * stride + c4 * 4;349 let off = r4 * 4 * stride + c4 * 4;
241 if y_w >= c4 * 4 + 4 {350 if y_w >= c4 * 4 + 4 {
242 lpf_edge(y, off, stride, 1, taps, bl, li, th);351 lpf_edge(y, off, stride, 1, taps, bl, li, th);
352+ if std::env::var_os("LFTRACE").is_some() {
353+ let kind = match taps {
354+ 16 => "V16",
355+ 8 => "V8",
356+ _ => "V4",
357+ };
358+ println!("{kind} {} {}", c4 * 4, r4 * 4);
359+ }
243 }360 }
244 }361 }
245 }362 }
@@ -247,20 +364,38 @@ impl LfGrids {
247 for r4 in 1..mr2 {364 for r4 in 1..mr2 {
248 for c4 in 0..mc2 {365 for c4 in 0..mc2 {
249 let cell = (r4 / 2) * self.mi_cols + c4 / 2;366 let cell = (r4 / 2) * self.mi_cols + c4 / 2;
250- let (_bsize, bor, _boc) = self.blk[cell];
251 let level = self.level8[cell];367 let level = self.level8[cell];
252 if level == 0 {368 if level == 0 {
253 continue;369 continue;
254 }370 }
371+ // Horizontal mirror of the vertical rule: the above-masks
372+ // mark every MI row for TX_8X8 (`above_64x64_txform_mask`),
373+ // every other row for TX_16X16, every fourth for TX_32X32.
374+ let mut taps = if r4 % 2 == 0 {
255 let tx = self.otx[cell] as usize;375 let tx = self.otx[cell] as usize;
256- let mut taps = if r4 == bor as usize * 2 {376+ if r4 % 8 == 0 {
257 match tx {377 match tx {
258 TX_16X16 | TX_32X32 => 16,378 TX_16X16 | TX_32X32 => 16,
379+ _ => 8,
380+ }
381+ } else if r4 % 4 == 0 {
382+ match tx {
383+ TX_32X32 => continue,
384+ TX_16X16 => 16,
259 TX_8X8 => 8,385 TX_8X8 => 8,
260- _ if r4 % 8 == 0 => 8,
261 _ => 4,386 _ => 4,
262 }387 }
388+ } else {
389+ match tx {
390+ TX_8X8 => 8,
391+ TX_4X4 => 4,
392+ _ => continue,
393+ }
394+ }
263 } else if self.tx4[r4 * mc2 + c4] as usize == TX_4X4 {395 } else if self.tx4[r4 * mc2 + c4] as usize == TX_4X4 {
396+ // mask_4x4_int: internal 4x4 edge at the half position
397+ // (filter_selectively_horiz fires it alongside 8/4-tap
398+ // top edges and alone when no top edge exists).
264 4399 4
265 } else {400 } else {
266 continue;401 continue;
@@ -272,6 +407,14 @@ impl LfGrids {
272 let off = r4 * 4 * stride + c4 * 4;407 let off = r4 * 4 * stride + c4 * 4;
273 if y_h >= r4 * 4 + 4 {408 if y_h >= r4 * 4 + 4 {
274 lpf_edge(y, off, stride, 0, taps, bl, li, th);409 lpf_edge(y, off, stride, 0, taps, bl, li, th);
410+ if std::env::var_os("LFTRACE").is_some() {
411+ let kind = match taps {
412+ 16 => "H16",
413+ 8 => "H8",
414+ _ => "H4",
415+ };
416+ println!("{kind} {} {}", c4 * 4, r4 * 4);
417+ }
275 }418 }
276 }419 }
277 }420 }
crates/ec-vp9/tests/scratch_lfdump.rsadded+110 -0
@@ -0,0 +1,110 @@
1+//! [scratch] Loop-filter diff map: decode key-320 frame 0 with and without
2+//! the loop filter (EC_VP9_SKIP_LF=1), print where we disagree with ffmpeg.
3+
4+mod ivf;
5+
6+use ec_vp9::decode::Decoder;
7+
8+fn ffmpeg_raw_yuv(path: &std::path::Path) -> Vec<u8> {
9+ let out = std::process::Command::new("ffmpeg")
10+ .args(["-v", "error", "-i"])
11+ .arg(path)
12+ .args(["-f", "rawvideo", "-pix_fmt", "yuv420p", "-"])
13+ .output()
14+ .expect("ffmpeg on PATH");
15+ assert!(out.status.success());
16+ out.stdout
17+}
18+
19+#[test]
20+fn dump_lf_diff_map() {
21+ let path = std::path::Path::new(env!("CARGO_MANIFEST_DIR"))
22+ .join("../../fixtures/vp9/key-320.ivf");
23+ let bytes = std::fs::read(&path).unwrap();
24+ let (_f, _w, _h, frames) = ivf::parse_ivf(&bytes);
25+ let reference = ffmpeg_raw_yuv(&path);
26+
27+ let (w, h) = (320usize, 240usize);
28+ let ysz = w * h;
29+
30+ // Pre-LF run.
31+ // SAFETY: single-threaded test process; no other env readers run here.
32+ unsafe { std::env::set_var("EC_VP9_SKIP_LF", "1") };
33+ let mut dec = Decoder::new();
34+ let pic_pre = dec.decode(&frames[0].data).expect("decode").expect("shown");
35+ std::fs::write("/tmp/y_pre_ours.raw", &pic_pre.y[..ysz]).unwrap();
36+ unsafe { std::env::remove_var("EC_VP9_SKIP_LF") };
37+
38+ // Post-LF run (fresh decoder).
39+ let mut dec = Decoder::new();
40+ let pic_post = dec.decode(&frames[0].data).expect("decode").expect("shown");
41+ std::fs::write("/tmp/y_post_ours.raw", &pic_post.y[..ysz]).unwrap();
42+
43+ let ff = &reference[..ysz];
44+ let pre = &pic_pre.y[..ysz];
45+ let post = &pic_post.y[..ysz];
46+
47+ let pre_diffs: Vec<usize> =
48+ (0..ysz).filter(|&i| pre[i] != ff[i]).collect();
49+ let post_diffs: Vec<usize> =
50+ (0..ysz).filter(|&i| post[i] != ff[i]).collect();
51+ let lf_changed_ours: Vec<usize> =
52+ (0..ysz).filter(|&i| pre[i] != post[i]).collect();
53+ println!("pre-LF diffs vs ffmpeg : {}", pre_diffs.len());
54+ println!("post-LF diffs vs ffmpeg: {}", post_diffs.len());
55+ println!("pixels our LF changed : {}", lf_changed_ours.len());
56+ for &i in post_diffs.iter().take(30) {
57+ println!(
58+ " POST diff ({:3},{:3}) ours {:3} ff {:3} (pre-ours {:3})",
59+ i % w,
60+ i / w,
61+ post[i],
62+ ff[i],
63+ pre[i]
64+ );
65+ }
66+ for &i in pre_diffs.iter().take(10) {
67+ println!(
68+ " PRE diff ({:3},{:3}) ours {:3} ff {:3}",
69+ i % w,
70+ i / w,
71+ pre[i],
72+ ff[i]
73+ );
74+ }
75+
76+ // ASCII map of the top-left 48x32: mark post-LF diffs (#), pre-LF diffs
77+ // (?), pixels only our LF touched (o).
78+ println!("map rows 0..32, cols 0..48 (#=post diff, ?=pre-only, o=our-LF-changed)");
79+ for y in 0..32 {
80+ let mut line = String::new();
81+ for x in 0..48 {
82+ let i = y * w + x;
83+ if post_diffs.binary_search(&i).is_ok() {
84+ line.push('#');
85+ } else if pre_diffs.binary_search(&i).is_ok() {
86+ line.push('?');
87+ } else if lf_changed_ours.binary_search(&i).is_ok() {
88+ line.push('o');
89+ } else {
90+ line.push('.');
91+ }
92+ }
93+ println!("{line}");
94+ }
95+ // Column/row histograms of post-LF diffs (first 24 of each).
96+ let mut cols = vec![0usize; w];
97+ let mut rows = vec![0usize; h];
98+ for &i in &post_diffs {
99+ cols[i % w] += 1;
100+ rows[i / w] += 1;
101+ }
102+ println!(
103+ "diff columns>0: {:?}",
104+ (0..w).filter(|&c| cols[c] > 0).take(24).collect::<Vec<_>>()
105+ );
106+ println!(
107+ "diff rows>0 : {:?}",
108+ (0..h).filter(|&r| rows[r] > 0).take(24).collect::<Vec<_>>()
109+ );
110+}
lanes/vp9kf.report.mdmodified+83 -0
@@ -238,3 +238,86 @@ EC_VP9_FORCE_UH/HSZ, scratch_* tests, and the EC_VP9_PROBE_TU/STAGE/CTX
238 debug prints in decode.rs/intra.rs.238 debug prints in decode.rs/intra.rs.
239 Oracle: /tmp/vp9loss/{libvpx-src,drv}; /tmp/vp9loss/d45probe2.c proves239 Oracle: /tmp/vp9loss/{libvpx-src,drv}; /tmp/vp9loss/d45probe2.c proves
240 SIMD==C for the specialized 4x4 kernels.240 SIMD==C for the specialized 4x4 kernels.
241+
242+## 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)
243+
244+### Fixed this round (loopfilter.rs, all proven against the instrumented oracle)
245+
246+1. **Per-MI edge masks (was: block-origin only).** libvpx builds masks per-MI —
247+ every MI of a wide block carries its own left/top edge
248+ (`left/above_64x64_txform_mask`: every MI col for TX_8X8, every 2nd for
249+ TX_16X16, every 4th for TX_32X32). We skipped internal MI boundaries of
250+ wide blocks via the tx4 grid. Vertical+horizontal loops now derive taps
251+ per-MI: c4%8==0 → 16-tap for TX_16X16/32 else 8; c4%4==0 → 16/8/4 by tx
252+ (TX_32X32 none); else 8-px → 8/4; odd → 4 iff tx4==TX_4X4 (mask_4x4_int).
253+ **Luma edge set now byte-identical to the oracle's: 3044/3044 anchors**
254+ (kind+position, LFTRACE diff — 0 missing, 0 extra).
255+2. **lpf_edge line coverage (the (7,1) root cause).** The kernel filters ONE
256+ pixel-line but was called once per 4x4 chunk — only every 4th row/col was
257+ ever filtered (row 0 changed, rows 1-3 never). lpf_edge now loops the 4
258+ lines (the `s += pitch` walk inside vpx_lpf_*_c). Post diffs 1219 → 696.
259+3. **flat2 = flat_mask5 shifted-parameter semantics.** With the C call
260+ `flat_mask5(1, s[-8], s[-7], s[-6], s[-5], p0, q0, s[4], s[5], s[6], s[7])`
261+ the tests are: p7..p4 and q4..q6 vs **p0**, but the outermost q7 vs **q0**;
262+ q0..q3 and p1..p3 are never tested. We had a bogus |q0-p0| clause and
263+ compared q7 against p0.
264+
265+### Remaining loopfilter diff (8 pixels)
266+
267+Row 2 only, cols 76-86: ours 47,47,47,49,49,50,50,48 vs vpx/ffmpeg
268+48,48,48,48,48,49,49,49. Zone = V16 anchor (80,0) + H4 anchors at y=4
269+(H4@y=4 writes rows 2..5; p1 row = 2). V-only python sim of the trace
270+matches vpx on rows 1,3 — the residual is in H-pass behavior on post-V
271+bytes around these anchors. Next: instrument H4@(72..84,4) mask/hev
272+verdicts on post-V bytes; re-check the filter_selectively_horiz 8-branch
273+int-alongside handling and the H4 mask's p3..q3 column bytes.
274+
275+### NEW dominant bug — decode diverges BEFORE the loop filter
276+
277+Pre-LF planes (EC_VP9_SKIP_LF=1) vs oracle SKIPLF dump: **684 differing
278+bytes, first at pixel (88,24): ours 78, vpx 48** (region top-left of that is
279+byte-exact — (7,1) pre matched 75=75). This is ~block 207+ — exactly where
280+HANDOFF #4's per-block verification stopped. The loop filter is exonerated
281+for the remaining mass: edge grid/levels/kinds/positions match the oracle
282+exactly, so 688 of the 696 post diffs are these pre-LF bytes flowing
283+through. Chase with the HANDOFF #3 protocol: oracle TOK/TXB diff from
284+block ~207 (MI row 3, col 11 area).
285+
286+### Reusable verification assets built this round
287+
288+- `EC_VP9_SKIP_LF=1` env (decode.rs): decode without loop filter.
289+- `LFTRACE=1` on OUR side: prints `V4|V8|V16|H4|H8|H16 x y` per luma
290+ lpf_edge call (loopfilter.rs, env-gated; removable after green).
291+- Oracle `/tmp/vp9loss/libvpx-src` is PATCHED: `LFTRACE=1` prints the same
292+ anchor format per vpx_lpf_* call site (filter_selectively_vert_row2 +
293+ filter_selectively_horiz, luma-gated via ss00/ss11 entry hooks); LFINIT
294+ prints stride/border; `drv` gained `SKIPLF=1` (VP9_SET_SKIP_LOOP_FILTER)
295+ and `YDUMP=<file>` (raw Y-plane dump). `/tmp` is volatile — if lost,
296+ rebuild libvpx then re-apply: the patch is mechanical (globals + one
297+ lf_tr() helper + getenv hook in loop_filter_rows + brace-wrapped call
298+ sites), ~40 lines; git checkout the file first.
299+- `tests/scratch_lfdump.rs`: dumps `/tmp/y_{pre,post}_ours.raw`, prints
300+ diff maps vs ffmpeg. Compare against `drv SKIPLF=1 YDUMP=...` (pre) and
301+ plain `YDUMP=...` (post). ffmpeg Y frame 0 == libvpx Y frame 0 (verified
302+ byte-equal), so either is a valid oracle target.
303+
304+### Trace-reading pitfalls learned (do not re-learn)
305+
306+- The oracle LF runs via `vp9_loop_filter_frame_mt` → loop_filter_rows fires
307+ once per worker CHUNK (3x for this frame, same buffer) — LFINIT repeats
308+ are chunking, not multi-pass; anchors union to the single-pass set.
309+- Luma-gate the oracle trace (chroma u/v buffers sit ~300 "rows" past the Y
310+ base and poison the x/y decomposition).
311+- libvpx vertical anchors sit on 8-px columns EXCEPT mask_4x4_int anchors at
312+ +4 (x%8==4) — don't mistake them for chroma.
313+- One vpx_lpf_* call covers 8 lines; our lpf_edge covers 4 — expand vpx
314+ anchors by +4 (rows for V, cols for H) before set-diffing.
315+- cargo test swallows eprintln unless `-- --nocapture`; byte dumps are the
316+ robust channel.
317+- VP8's filter_mask and VP9's are IDENTICAL in vpx_dsp (abs(p0-q0)*2 +
318+ abs(p1-q1)/2) — don't "fix" that; the flat_mask5 asymmetry IS real.
319+
320+**State at yield:** lib 8/8 PASS; lossless_64, inter_is_named_unsupported,
321+profile1_444 all PASS; keyframes_match_ffmpeg FAIL, first diff (76,2)
322+ours 47 ref 48. NOT pushed (gate not green). Branch lane-vp9-kf has the
323+three loopfilter fixes + probe hooks + scratch_lfdump.