Deblocker derived the chroma transform size from luma's tx_depth-split tx, inventing chroma edges
Browse files at this commit9920fec0d27291a5a368b7a4a58c3b5e21517daaParent: d21774e- fix
- deblocker derived the chroma transform size from luma's tx_depth-split tx, inventing chroma edges
3 changed files +117 -24
- crates/ec-av1/examples/decode_probe.rs +16 -1
- crates/ec-av1/src/decode.rs +51 -12
- crates/ec-av1/src/stream.rs +50 -11
crates/ec-av1/examples/decode_probe.rsmodified+16 -1
| @@ -32,7 +32,22 @@ fn main() { | |||
| 32 | Ok(frames) if frames.is_empty() => { | 32 | Ok(frames) if frames.is_empty() => { |
| 33 | println!("OK but EMPTY: no frames -- is {path} an IVF rather than a raw OBU stream?"); | 33 | println!("OK but EMPTY: no frames -- is {path} an IVF rather than a raw OBU stream?"); |
| 34 | } | 34 | } |
| 35 | - Ok(frames) => println!("OK: {} frames decoded", frames.len()), | 35 | + Ok(frames) => { |
| 36 | + println!("OK: {} frames decoded", frames.len()); | ||
| 37 | + // Optional second arg: dump the decoded planes as raw yuv420p so a | ||
| 38 | + // pixel diff against `ffmpeg -i s.obu -f rawvideo` needs no test harness. | ||
| 39 | + if let Some(out) = std::env::args().nth(2) { | ||
| 40 | + // 8-bit only: planes are u16, take the low byte. | ||
| 41 | + let mut buf: Vec<u8> = Vec::new(); | ||
| 42 | + for f in &frames { | ||
| 43 | + for p in [&f.y, &f.u, &f.v] { | ||
| 44 | + buf.extend(p.iter().map(|&s| s as u8)); | ||
| 45 | + } | ||
| 46 | + } | ||
| 47 | + std::fs::write(&out, &buf).expect("writing raw planes"); | ||
| 48 | + println!("wrote {} bytes of yuv420p to {out}", buf.len()); | ||
| 49 | + } | ||
| 50 | + } | ||
| 36 | Err(e) => println!("REFUSED: {e}"), | 51 | Err(e) => println!("REFUSED: {e}"), |
| 37 | } | 52 | } |
| 38 | } | 53 | } |
crates/ec-av1/src/decode.rsmodified+51 -12
| @@ -1823,6 +1823,16 @@ struct Neighbours { | |||
| 1823 | /// lane-rect r2: TX_32X16/TX_16X32 strips deblock their horizontal | 1823 | /// lane-rect r2: TX_32X16/TX_16X32 strips deblock their horizontal |
| 1824 | /// edges by tx height (spec 7.14.2 `set_lpf_parameters` dir==VERT). | 1824 | /// edges by tx height (spec 7.14.2 `set_lpf_parameters` dir==VERT). |
| 1825 | tx_h_grid: Vec<u8>, | 1825 | tx_h_grid: Vec<u8>, |
| 1826 | + /// Per-4x4-mi CHROMA transform width/height in pixels -- lane-tiny r4. | ||
| 1827 | + /// A block's chroma transform is `av1_get_max_uv_txsize(bsize)`: the | ||
| 1828 | + /// largest transform covering the subsampled BLOCK, capped at 32x32. It | ||
| 1829 | + /// does NOT follow luma's `tx_depth` split, so deriving it as | ||
| 1830 | + /// `tx_grid / 2` invented chroma transform edges the deblocker then | ||
| 1831 | + /// filtered (32x16 frames, U plane, seeds 42/47). Written from the | ||
| 1832 | + /// block's own mi span in [`Self::fill_lf_grid_rect`], the one place | ||
| 1833 | + /// every caller routes through. | ||
| 1834 | + uv_tx_grid: Vec<u8>, | ||
| 1835 | + uv_tx_h_grid: Vec<u8>, | ||
| 1826 | /// Per-4x4-mi reference frame (`0` = intra, magnitude `1..=7` = | 1836 | /// Per-4x4-mi reference frame (`0` = intra, magnitude `1..=7` = |
| 1827 | /// `MV_REFERENCE_FRAME`, negative = that reference coded as `GLOBALMV` -- | 1837 | /// `MV_REFERENCE_FRAME`, negative = that reference coded as `GLOBALMV` -- |
| 1828 | /// `lf_level`'s `mode_lf_lut` row 0, lane-av1golden2) | 1838 | /// `lf_level`'s `mode_lf_lut` row 0, lane-av1golden2) |
| @@ -1902,6 +1912,8 @@ impl Neighbours { | |||
| 1902 | skip_grid_cols_mi: cols * (SUB / MI), | 1912 | skip_grid_cols_mi: cols * (SUB / MI), |
| 1903 | tx_grid: vec![0u8; cols * (SUB / MI) * rows * (SUB / MI)], | 1913 | tx_grid: vec![0u8; cols * (SUB / MI) * rows * (SUB / MI)], |
| 1904 | tx_h_grid: vec![0u8; cols * (SUB / MI) * rows * (SUB / MI)], | 1914 | tx_h_grid: vec![0u8; cols * (SUB / MI) * rows * (SUB / MI)], |
| 1915 | + uv_tx_grid: vec![0u8; cols * (SUB / MI) * rows * (SUB / MI)], | ||
| 1916 | + uv_tx_h_grid: vec![0u8; cols * (SUB / MI) * rows * (SUB / MI)], | ||
| 1905 | ref_grid: vec![0i8; cols * (SUB / MI) * rows * (SUB / MI)], | 1917 | ref_grid: vec![0i8; cols * (SUB / MI) * rows * (SUB / MI)], |
| 1906 | delta_lf_grid: vec![[0i8; 4]; cols * (SUB / MI) * rows * (SUB / MI)], | 1918 | delta_lf_grid: vec![[0i8; 4]; cols * (SUB / MI) * rows * (SUB / MI)], |
| 1907 | above_palette_size: vec![0; cols], | 1919 | above_palette_size: vec![0; cols], |
| @@ -2105,6 +2117,11 @@ impl Neighbours { | |||
| 2105 | ref_frame: i8, | 2117 | ref_frame: i8, |
| 2106 | ) { | 2118 | ) { |
| 2107 | let (mi_r, mi_c) = at_mi; | 2119 | let (mi_r, mi_c) = at_mi; |
| 2120 | + // `av1_get_max_uv_txsize` under 4:2:0: the block's chroma extent is | ||
| 2121 | + // half its luma one (min 4 px, the smallest transform), capped at | ||
| 2122 | + // TX_32X32 -- independent of this block's luma `tx_depth`. | ||
| 2123 | + let uv_tx_w = ((w_mi * MI / 2).max(4).min(32)) as u8; | ||
| 2124 | + let uv_tx_h = ((h_mi * MI / 2).max(4).min(32)) as u8; | ||
| 2108 | let cur = CURRENT_DELTA_LF.with(|c| c.get()); | 2125 | let cur = CURRENT_DELTA_LF.with(|c| c.get()); |
| 2109 | let snapshot: [i8; 4] = if DELTA_LF_MULTI.with(|c| c.get()) { | 2126 | let snapshot: [i8; 4] = if DELTA_LF_MULTI.with(|c| c.get()) { |
| 2110 | std::array::from_fn(|i| cur[i].clamp(-63, 63) as i8) | 2127 | std::array::from_fn(|i| cur[i].clamp(-63, 63) as i8) |
| @@ -2120,6 +2137,12 @@ impl Neighbours { | |||
| 2120 | if let Some(cell) = self.tx_h_grid.get_mut(idx) { | 2137 | if let Some(cell) = self.tx_h_grid.get_mut(idx) { |
| 2121 | *cell = tx_h_px; | 2138 | *cell = tx_h_px; |
| 2122 | } | 2139 | } |
| 2140 | + if let Some(cell) = self.uv_tx_grid.get_mut(idx) { | ||
| 2141 | + *cell = uv_tx_w; | ||
| 2142 | + } | ||
| 2143 | + if let Some(cell) = self.uv_tx_h_grid.get_mut(idx) { | ||
| 2144 | + *cell = uv_tx_h; | ||
| 2145 | + } | ||
| 2123 | if let Some(cell) = self.ref_grid.get_mut(idx) { | 2146 | if let Some(cell) = self.ref_grid.get_mut(idx) { |
| 2124 | *cell = ref_frame; | 2147 | *cell = ref_frame; |
| 2125 | } | 2148 | } |
| @@ -4325,6 +4348,20 @@ fn cfl_scaled(alpha_q3: i32, ac_q3: i32) -> i32 { | |||
| 4325 | /// [`crate::encode`]'s private `Plane`, whose `edges` this mirrors exactly | 4348 | /// [`crate::encode`]'s private `Plane`, whose `edges` this mirrors exactly |
| 4326 | /// (own-edge and reach clamps both), passing what it gathers straight to | 4349 | /// (own-edge and reach clamps both), passing what it gathers straight to |
| 4327 | /// [`crate::intra::predict`] instead of hand-rolling `DC_PRED` alone. | 4350 | /// [`crate::intra::predict`] instead of hand-rolling `DC_PRED` alone. |
| 4351 | +/// lane-tiny r4: raw padded-plane dump for filter-stage bisection, written | ||
| 4352 | +/// only when `var` names a path. Same byte shape as `EC_AV1_PREFILT_DUMP`. | ||
| 4353 | +fn dump_stage(var: &str, y: &PlaneBuf, u: &PlaneBuf, v: &PlaneBuf) { | ||
| 4354 | + use std::io::Write; | ||
| 4355 | + if let Ok(path) = std::env::var(var) | ||
| 4356 | + && let Ok(mut f) = std::fs::File::create(format!("{path}.f0")) | ||
| 4357 | + { | ||
| 4358 | + for p in [y, u, v] { | ||
| 4359 | + let narrow: Vec<u8> = p.data.iter().map(|&s| s as u8).collect(); | ||
| 4360 | + let _ = f.write_all(&narrow); | ||
| 4361 | + } | ||
| 4362 | + } | ||
| 4363 | +} | ||
| 4364 | + | ||
| 4328 | #[derive(Clone)] | 4365 | #[derive(Clone)] |
| 4329 | struct PlaneBuf { | 4366 | struct PlaneBuf { |
| 4330 | data: Vec<u16>, | 4367 | data: Vec<u16>, |
| @@ -5674,24 +5711,16 @@ fn plane_to_mi(chroma: bool, coord: usize) -> usize { | |||
| 5674 | /// half its coded block's luma transform, spec's `av1_get_max_uv_txsize` | 5711 | /// half its coded block's luma transform, spec's `av1_get_max_uv_txsize` |
| 5675 | /// under 4:2:0 and a block that never splits its own transform). | 5712 | /// under 4:2:0 and a block that never splits its own transform). |
| 5676 | fn tx_px_at(n: &Neighbours, chroma: bool, mi_r: usize, mi_c: usize) -> u32 { | 5713 | fn tx_px_at(n: &Neighbours, chroma: bool, mi_r: usize, mi_c: usize) -> u32 { |
| 5677 | - let luma_tx = n | 5714 | + let grid = if chroma { &n.uv_tx_grid } else { &n.tx_grid }; |
| 5678 | - .tx_grid | 5715 | + grid.get(mi_r * n.skip_grid_cols_mi + mi_c).copied().unwrap_or(0) as u32 |
| 5679 | - .get(mi_r * n.skip_grid_cols_mi + mi_c) | ||
| 5680 | - .copied() | ||
| 5681 | - .unwrap_or(0) as u32; | ||
| 5682 | - if chroma { luma_tx / 2 } else { luma_tx } | ||
| 5683 | } | 5716 | } |
| 5684 | 5717 | ||
| 5685 | /// [`tx_px_at`]'s HEIGHT twin (lane-rect r2): the deblocker's horizontal | 5718 | /// [`tx_px_at`]'s HEIGHT twin (lane-rect r2): the deblocker's horizontal |
| 5686 | /// edges step and gate by transform height, which differs from width only | 5719 | /// edges step and gate by transform height, which differs from width only |
| 5687 | /// on rect partition strips. | 5720 | /// on rect partition strips. |
| 5688 | fn tx_h_px_at(n: &Neighbours, chroma: bool, mi_r: usize, mi_c: usize) -> u32 { | 5721 | fn tx_h_px_at(n: &Neighbours, chroma: bool, mi_r: usize, mi_c: usize) -> u32 { |
| 5689 | - let luma_tx = n | 5722 | + let grid = if chroma { &n.uv_tx_h_grid } else { &n.tx_h_grid }; |
| 5690 | - .tx_h_grid | 5723 | + grid.get(mi_r * n.skip_grid_cols_mi + mi_c).copied().unwrap_or(0) as u32 |
| 5691 | - .get(mi_r * n.skip_grid_cols_mi + mi_c) | ||
| 5692 | - .copied() | ||
| 5693 | - .unwrap_or(0) as u32; | ||
| 5694 | - if chroma { luma_tx / 2 } else { luma_tx } | ||
| 5695 | } | 5724 | } |
| 5696 | 5725 | ||
| 5697 | /// `get_tx_size_context` (spec 9.3's `TxDepthCtx`, libaom | 5726 | /// `get_tx_size_context` (spec 9.3's `TxDepthCtx`, libaom |
| @@ -7411,7 +7440,12 @@ pub(crate) fn decode_key_frame_tile_with_cdfs( | |||
| 7411 | frame_height as usize, | 7440 | frame_height as usize, |
| 7412 | ); | 7441 | ); |
| 7413 | let (deblocked_y, deblocked_u, deblocked_v) = (y.clone(), u.clone(), v.clone()); | 7442 | let (deblocked_y, deblocked_u, deblocked_v) = (y.clone(), u.clone(), v.clone()); |
| 7443 | + // lane-tiny r4: post-deblock / post-CDEF dumps mirroring aomdec's own | ||
| 7444 | + // EC_AV1_POSTDEBLOCK_DUMP (decodeframe.c ~5404) -- with the pre-filter | ||
| 7445 | + // dump above these bisect WHICH filter stage introduced a mismatch. | ||
| 7446 | + dump_stage("EC_AV1_POSTDEBLOCK_DUMP", &y, &u, &v); | ||
| 7414 | apply_cdef(&mut y, &mut u, &mut v, cdef, &neighbours); | 7447 | apply_cdef(&mut y, &mut u, &mut v, cdef, &neighbours); |
| 7448 | + dump_stage("EC_AV1_POSTCDEF_DUMP", &y, &u, &v); | ||
| 7415 | apply_loop_restoration(&mut y, &mut u, &mut v, &deblocked_y, &deblocked_u, &deblocked_v, lr, &lr_grid); | 7449 | apply_loop_restoration(&mut y, &mut u, &mut v, &deblocked_y, &deblocked_u, &deblocked_v, lr, &lr_grid); |
| 7416 | 7450 | ||
| 7417 | let (fw, fh) = (frame_width as usize, frame_height as usize); | 7451 | let (fw, fh) = (frame_width as usize, frame_height as usize); |
| @@ -13306,7 +13340,12 @@ pub(crate) fn decode_inter_frame_tile_with_cdfs( | |||
| 13306 | frame_height as usize, | 13340 | frame_height as usize, |
| 13307 | ); | 13341 | ); |
| 13308 | let (deblocked_y, deblocked_u, deblocked_v) = (y.clone(), u.clone(), v.clone()); | 13342 | let (deblocked_y, deblocked_u, deblocked_v) = (y.clone(), u.clone(), v.clone()); |
| 13343 | + // lane-tiny r4: post-deblock / post-CDEF dumps mirroring aomdec's own | ||
| 13344 | + // EC_AV1_POSTDEBLOCK_DUMP (decodeframe.c ~5404) -- with the pre-filter | ||
| 13345 | + // dump above these bisect WHICH filter stage introduced a mismatch. | ||
| 13346 | + dump_stage("EC_AV1_POSTDEBLOCK_DUMP", &y, &u, &v); | ||
| 13309 | apply_cdef(&mut y, &mut u, &mut v, cdef, &neighbours); | 13347 | apply_cdef(&mut y, &mut u, &mut v, cdef, &neighbours); |
| 13348 | + dump_stage("EC_AV1_POSTCDEF_DUMP", &y, &u, &v); | ||
| 13310 | apply_loop_restoration(&mut y, &mut u, &mut v, &deblocked_y, &deblocked_u, &deblocked_v, lr, &lr_grid); | 13349 | apply_loop_restoration(&mut y, &mut u, &mut v, &deblocked_y, &deblocked_u, &deblocked_v, lr, &lr_grid); |
| 13311 | 13350 | ||
| 13312 | let motion_field = build_motion_field( | 13351 | let motion_field = build_motion_field( |
crates/ec-av1/src/stream.rsmodified+50 -11
| @@ -6361,20 +6361,23 @@ mod tests { | |||
| 6361 | crate::decode::cdef_idx_hits() | 6361 | crate::decode::cdef_idx_hits() |
| 6362 | ); | 6362 | ); |
| 6363 | } | 6363 | } |
| 6364 | - /// lane-tiny r1 diagnostic (not a gate): sweeps tiny key-frame sizes | 6364 | + /// lane-tiny r4 GATE (was r1's `#[ignore]`d probe): every tiny key-frame |
| 6365 | - /// against real aomenc + ffmpeg to find the exact size boundary of the | 6365 | + /// size real aomenc will encode, decoded pixel-exact against ffmpeg. |
| 6366 | - /// silent-wrong-pixels defect the sub8 lane's r2 dead-end flagged (a | 6366 | + /// Tiny frames are where every edge/partial-superblock corner case is |
| 6367 | - /// plain 16x16 stream, no exotic tools). `#[ignore]`d -- prints a | 6367 | + /// forced, and this sweep is what found both r3's filter-intra tx_type |
| 6368 | - /// pass/fail table, asserts nothing; run manually with `--nocapture`. | 6368 | + /// CDF row and r4's chroma-transform-size deblock defect -- so it |
| 6369 | + /// hard-asserts now: a decode error, a refusal, or one differing sample | ||
| 6370 | + /// at any size/seed fails. Only a genuinely absent oracle skips, and | ||
| 6371 | + /// `EC_AV1_REQUIRE_AOMENC=1` turns that into a failure too. | ||
| 6369 | #[test] | 6372 | #[test] |
| 6370 | - #[ignore = "diagnostic sweep, run manually with --nocapture"] | 6373 | + fn a_real_aomenc_tiny_frame_size_sweep_decodes_pixel_exact() { |
| 6371 | - fn probe_tiny_frame_size_boundary() { | ||
| 6372 | if !have_ffmpeg() || !have_aomenc() { | 6374 | if !have_ffmpeg() || !have_aomenc() { |
| 6373 | eprintln!("SKIP: no ffmpeg/aomenc"); | 6375 | eprintln!("SKIP: no ffmpeg/aomenc"); |
| 6374 | return; | 6376 | return; |
| 6375 | } | 6377 | } |
| 6376 | let sizes: &[(usize, usize)] = | 6378 | let sizes: &[(usize, usize)] = |
| 6377 | &[(8, 8), (16, 16), (32, 32), (16, 32), (32, 16), (64, 64), (48, 48), (24, 24)]; | 6379 | &[(8, 8), (16, 16), (32, 32), (16, 32), (32, 16), (64, 64), (48, 48), (24, 24)]; |
| 6380 | + let mut total_exact = 0u32; | ||
| 6378 | for &(width, height) in sizes { | 6381 | for &(width, height) in sizes { |
| 6379 | let mut ok = 0u32; | 6382 | let mut ok = 0u32; |
| 6380 | let mut bad = 0u32; | 6383 | let mut bad = 0u32; |
| @@ -6423,7 +6426,18 @@ mod tests { | |||
| 6423 | let stream = out.stdout; | 6426 | let stream = out.stdout; |
| 6424 | let frames = match decode_stream(&stream) { | 6427 | let frames = match decode_stream(&stream) { |
| 6425 | Err(e) => { | 6428 | Err(e) => { |
| 6426 | - eprintln!(" {width}x{height} seed {seed}: REFUSED {e}"); | 6429 | + // A NAMED refusal is the only non-exact outcome this |
| 6430 | + // gate tolerates (8x8 still needs a rectangular | ||
| 6431 | + // transform); anything else is a hard failure, and | ||
| 6432 | + // the floor assert below keeps that from going | ||
| 6433 | + // vacuous. | ||
| 6434 | + let msg = e.to_string(); | ||
| 6435 | + assert!( | ||
| 6436 | + msg.contains("unsupported"), | ||
| 6437 | + "{width}x{height} seed {seed}: failed outright, not a named \ | ||
| 6438 | + refusal: {msg}" | ||
| 6439 | + ); | ||
| 6440 | + eprintln!(" {width}x{height} seed {seed}: REFUSED {msg}"); | ||
| 6427 | refused += 1; | 6441 | refused += 1; |
| 6428 | continue; | 6442 | continue; |
| 6429 | } | 6443 | } |
| @@ -6438,6 +6452,12 @@ mod tests { | |||
| 6438 | ok += 1; | 6452 | ok += 1; |
| 6439 | } else { | 6453 | } else { |
| 6440 | bad += 1; | 6454 | bad += 1; |
| 6455 | + let nd = |a: &[u16], b: &[u16]| a.iter().zip(b).filter(|(x, y)| x != y).count(); | ||
| 6456 | + let (ndy, ndu, ndv) = ( | ||
| 6457 | + nd(&frames[0].y, &ffmpeg_frames[0].y), | ||
| 6458 | + nd(&frames[0].u, &ffmpeg_frames[0].u), | ||
| 6459 | + nd(&frames[0].v, &ffmpeg_frames[0].v), | ||
| 6460 | + ); | ||
| 6441 | let mut first = None; | 6461 | let mut first = None; |
| 6442 | for row in 0..height { | 6462 | for row in 0..height { |
| 6443 | for col in 0..width { | 6463 | for col in 0..width { |
| @@ -6458,13 +6478,32 @@ mod tests { | |||
| 6458 | .filter(|(a, b)| a != b) | 6478 | .filter(|(a, b)| a != b) |
| 6459 | .count(); | 6479 | .count(); |
| 6460 | eprintln!( | 6480 | eprintln!( |
| 6461 | - " {width}x{height} seed {seed}: MISMATCH luma first_diff={first:?} ndiff_luma={ndiff}/{}", | 6481 | + " {width}x{height} seed {seed}: MISMATCH first_diff={first:?} \ |
| 6482 | + ndiff_luma={ndiff}/{} ndiff y/u/v={ndy}/{ndu}/{ndv}", | ||
| 6462 | width * height | 6483 | width * height |
| 6463 | ); | 6484 | ); |
| 6464 | } | 6485 | } |
| 6465 | } | 6486 | } |
| 6466 | - eprintln!("{width}x{height}: {ok} ok / {bad} mismatch / {refused} refused (of {n})"); | 6487 | + assert_eq!( |
| 6467 | - } | 6488 | + bad, 0, |
| 6489 | + "{width}x{height}: {bad} of {n} seeds decoded to WRONG PIXELS ({ok} exact, \ | ||
| 6490 | + {refused} named refusals)" | ||
| 6491 | + ); | ||
| 6492 | + eprintln!("{width}x{height}: {ok}/{n} pixel-exact, {refused} named refusals"); | ||
| 6493 | + total_exact += ok; | ||
| 6494 | + } | ||
| 6495 | + // Hit counter: the sweep proved nothing unless every size x seed | ||
| 6496 | + // attempt really ran end to end (aomenc -> us -> ffmpeg compare). | ||
| 6497 | + // Floor (hit counter): 7 of the 8 sizes must decode all 10 seeds | ||
| 6498 | + // pixel-exact -- only 8x8 is allowed to refuse outright today. If a | ||
| 6499 | + // future change makes sizes refuse instead of decode, this fails | ||
| 6500 | + // rather than passing on refusals alone. | ||
| 6501 | + assert!( | ||
| 6502 | + total_exact >= 70, | ||
| 6503 | + "tiny sweep only reached {total_exact} pixel-exact attempts (floor 70) -- \ | ||
| 6504 | + refusals cannot substitute for decodes" | ||
| 6505 | + ); | ||
| 6506 | + eprintln!("tiny frame sweep: {total_exact} pixel-exact attempts"); | ||
| 6468 | } | 6507 | } |
| 6469 | 6508 | ||
| 6470 | /// lane-tiny r1: re-decodes the known-failing 32x32 seed 45 fixture with | 6509 | /// lane-tiny r1: re-decodes the known-failing 32x32 seed 45 fixture with |