The four 64x64 CDEF units of a 128x128 root block share one cdef_idx
Browse files at this commit74b705da53239226faa5cd218f925c8857d26fdfParent: 14a38e5- fix
- the four 64x64 CDEF units of a 128x128 root block share one cdef_idx
3 changed files +179 -2
- crates/ec-av1/src/encode.rs +102 -0
- crates/ec-av1/src/filter_search.rs +39 -2
- crates/ec-av1/src/tile.rs +38 -0
crates/ec-av1/src/encode.rsmodified+102 -0
| @@ -8564,6 +8564,9 @@ pub(crate) fn encode_key_frame_inner( | ||
| 8564 | 8564 | &mut tiles, |
| 8565 | 8565 | search.lambda, |
| 8566 | 8566 | fctx, |
| 8567 | + // A key frame codes no 128x128 root block (the writer has no intra | |
| 8568 | + // path at that size), so every unit carries its own literal. | |
| 8569 | + &[], | |
| 8567 | 8570 | // Every re-code starts from this frame's OWN starting tables, never |
| 8568 | 8571 | // from where the first write left them: the second tile is the same |
| 8569 | 8572 | // symbol sequence plus new ones, so it must adapt from the same |
| @@ -8656,6 +8659,10 @@ fn pick_and_apply_filters( | ||
| 8656 | 8659 | tiles: &mut Vec<Vec<u8>>, |
| 8657 | 8660 | lambda: f64, |
| 8658 | 8661 | fctx: &crate::decode::FrameCtx, |
| 8662 | + // lane-edge128: `crate::tile::cdef_unit_owner` for the blocks these tiles | |
| 8663 | + // carry -- empty (the identity map) for a frame whose every block covers | |
| 8664 | + // one 64x64 CDEF unit. | |
| 8665 | + cdef_owner: &[u32], | |
| 8659 | 8666 | recode: impl Fn( |
| 8660 | 8667 | u8, |
| 8661 | 8668 | usize, |
| @@ -8708,6 +8715,7 @@ fn pick_and_apply_filters( | ||
| 8708 | 8715 | damping, |
| 8709 | 8716 | (sb_cols, sb_rows), |
| 8710 | 8717 | lambda, |
| 8718 | + cdef_owner, | |
| 8711 | 8719 | ); |
| 8712 | 8720 | drop(ft); |
| 8713 | 8721 | let (lf, cdef, idx_grid, picture) = search_result?; |
| @@ -13228,6 +13236,7 @@ pub(crate) fn encode_inter_frame( | ||
| 13228 | 13236 | &mut tiles, |
| 13229 | 13237 | search.lambda, |
| 13230 | 13238 | fctx, |
| 13239 | + &crate::tile::cdef_unit_owner(&blocks, mi_cols, mi_rows), | |
| 13231 | 13240 | // As in `encode_key_frame_inner`, but this frame's end-of-tile tables |
| 13232 | 13241 | // are what the NEXT frame writes from, so the winning re-code |
| 13233 | 13242 | // publishes its own into `end_cdfs` -- the first write's are stale |
| @@ -16376,6 +16385,90 @@ mod tests { | ||
| 16376 | 16385 | } |
| 16377 | 16386 | } |
| 16378 | 16387 | |
| 16388 | + /// lane-edge128: a 128x128 root block WITH a residual on a clip whose | |
| 16389 | + /// CDEF search codes a per-64x64 `cdef_idx` list. `read_cdef` (spec | |
| 16390 | + /// 5.11.56) codes ONE literal for the block that first reaches a unit and | |
| 16391 | + /// copies it over every unit the block SPANS, so the four units of a 128 | |
| 16392 | + /// root share one index -- while the encoder's CDEF search prices a | |
| 16393 | + /// preset per unit and its filter replay applied all four. Only the first | |
| 16394 | + /// reached the stream, so every decoder filtered the other three with the | |
| 16395 | + /// origin's strengths and the encoder's reconstruction (the picture the | |
| 16396 | + /// next frame predicts from) was a filter apart from what a decoder | |
| 16397 | + /// produces. RED before this lane on the native BD gate's bars 2160p row | |
| 16398 | + /// (`EC_AV1_B128RES=1`, q=90, frame 11: 8 luma and 702 chroma samples, | |
| 16399 | + /// the rows above the 128 superblock's own edge); the stream itself was | |
| 16400 | + /// always right -- ffmpeg and our own decoder agreed with each other and | |
| 16401 | + /// disagreed with the encoder. | |
| 16402 | + /// | |
| 16403 | + /// `take_cdef_covered_units` is the gate on the fixture (class | |
| 16404 | + /// `gate-blind-to-feature`): a clip whose CDEF picks one strength pair | |
| 16405 | + /// for the whole frame codes no literal at all and witnesses nothing. | |
| 16406 | + /// | |
| 16407 | + /// cargo test -p ec-av1 --release --lib -- --ignored \ | |
| 16408 | + /// a_128_root_residual_block_under_a_per_unit_cdef --nocapture | |
| 16409 | + #[test] | |
| 16410 | + #[ignore = "sets the process-global superblock size: run it alone"] | |
| 16411 | + fn a_128_root_residual_block_under_a_per_unit_cdef_list_decodes_exact() { | |
| 16412 | + let _knobs = crate::speed::knob_write(); | |
| 16413 | + if !have_ffmpeg() { | |
| 16414 | + eprintln!("SKIP: no ffmpeg"); | |
| 16415 | + return; | |
| 16416 | + } | |
| 16417 | + force_sb128(true); | |
| 16418 | + let dir = std::env::temp_dir().join(format!("ec-av1-edge128-{}", std::process::id())); | |
| 16419 | + std::fs::create_dir_all(&dir).unwrap(); | |
| 16420 | + let clip = dir.join("testsrc2-1280x768.y4m"); | |
| 16421 | + // Detailed synthetic content: its CDEF search spends more than one | |
| 16422 | + // strength pair, which is what puts a `cdef_idx` literal in the tile. | |
| 16423 | + let ok = Command::new("ffmpeg") | |
| 16424 | + .args(["-v", "error", "-y", "-f", "lavfi"]) | |
| 16425 | + .args(["-i", "testsrc2=size=1280x768:rate=24"]) | |
| 16426 | + .args(["-frames:v", "12", "-pix_fmt", "yuv420p"]) | |
| 16427 | + .arg(&clip) | |
| 16428 | + .status() | |
| 16429 | + .expect("ffmpeg failed to run") | |
| 16430 | + .success(); | |
| 16431 | + assert!(ok, "ffmpeg could not write the synthetic clip"); | |
| 16432 | + let (width, height) = (1280usize, 768usize); | |
| 16433 | + let pictures = | |
| 16434 | + crate::probe::source(clip.to_str().unwrap(), "0", "null", width, height, 12); | |
| 16435 | + force_b128_root(true); | |
| 16436 | + force_b128_residual(true); | |
| 16437 | + let _ = crate::tile::take_sb128_residual_hits(); | |
| 16438 | + let _ = crate::filter_search::take_cdef_covered_units(); | |
| 16439 | + let encoded = encode_sequence(&pictures, 90, 0.5).unwrap(); | |
| 16440 | + let coded = crate::tile::take_sb128_residual_hits(); | |
| 16441 | + let covered = crate::filter_search::take_cdef_covered_units(); | |
| 16442 | + eprintln!( | |
| 16443 | + "128x128 blocks WITH a residual: writer coded {coded}; 64x64 CDEF units taking \ | |
| 16444 | + another unit's literal: {covered}" | |
| 16445 | + ); | |
| 16446 | + assert!(coded > 0, "no 128x128 block carried a residual on this clip"); | |
| 16447 | + assert!( | |
| 16448 | + covered > 0, | |
| 16449 | + "no 64x64 unit took a non-zero preset from another unit's literal, so this clip \ | |
| 16450 | + cannot witness the rule", | |
| 16451 | + ); | |
| 16452 | + let decoded = crate::stream::decode_stream(&encoded.stream).expect("our decoder"); | |
| 16453 | + assert_eq!(decoded.len(), encoded.frames.len(), "frame count"); | |
| 16454 | + for (i, (frame, dec)) in encoded.frames.iter().zip(&decoded).enumerate() { | |
| 16455 | + for (plane, got, want) in [ | |
| 16456 | + ("luma", &dec.y, &frame.reconstruction.y), | |
| 16457 | + ("U", &dec.u, &frame.reconstruction.u), | |
| 16458 | + ("V", &dec.v, &frame.reconstruction.v), | |
| 16459 | + ] { | |
| 16460 | + let differ = got.iter().zip(want.iter()).filter(|(a, b)| a != b).count(); | |
| 16461 | + assert_eq!(differ, 0, "frame {i}: {plane} -- {differ} samples differ"); | |
| 16462 | + } | |
| 16463 | + } | |
| 16464 | + let ff = ffmpeg_decode_sequence(&encoded.stream, width, height, pictures.len()); | |
| 16465 | + for (i, (frame, dec)) in encoded.frames.iter().zip(&ff).enumerate() { | |
| 16466 | + assert_eq!(dec.y, frame.reconstruction.y, "frame {i}: ffmpeg luma"); | |
| 16467 | + assert_eq!(dec.u, frame.reconstruction.u, "frame {i}: ffmpeg U"); | |
| 16468 | + assert_eq!(dec.v, frame.reconstruction.v, "frame {i}: ffmpeg V"); | |
| 16469 | + } | |
| 16470 | + } | |
| 16471 | + | |
| 16379 | 16472 | /// lane-b128r: the COMPOUND arm at the 128 root -- one 128x128 block |
| 16380 | 16473 | /// naming `LAST` plus `GOLDEN`/`ALTREF`, through the same |
| 16381 | 16474 | /// `write_compound_block` chain every smaller compound block takes, at a |
| @@ -17607,6 +17700,15 @@ mod tests { | ||
| 17607 | 17700 | for (i, (d, e)) in decoded.iter().zip(&encoded.frames).enumerate() { |
| 17608 | 17701 | if let Some((plane, s, got, want)) = first_plane_mismatch(d, &e.reconstruction) { |
| 17609 | 17702 | // TEMPORARY (lane-b128r bisect): the whole mismatch map. |
| 17703 | + if let Ok(dir) = std::env::var("EC_B128R_DUMP") { | |
| 17704 | + std::fs::write(format!("{dir}/gate-q{q}.obu"), &encoded.stream).unwrap(); | |
| 17705 | + let r = &e.reconstruction; | |
| 17706 | + let mut buf = Vec::new(); | |
| 17707 | + for pl in [&r.y, &r.u, &r.v] { | |
| 17708 | + buf.extend(pl.iter().flat_map(|s| s.to_le_bytes())); | |
| 17709 | + } | |
| 17710 | + std::fs::write(format!("{dir}/recon-q{q}.f{i}.yuv"), &buf).unwrap(); | |
| 17711 | + } | |
| 17610 | 17712 | let (l, r) = match plane { |
| 17611 | 17713 | "Y" => (&d.y, &e.reconstruction.y), |
| 17612 | 17714 | "U" => (&d.u, &e.reconstruction.u), |
crates/ec-av1/src/filter_search.rsmodified+39 -2
| @@ -48,6 +48,18 @@ thread_local! { | ||
| 48 | 48 | const { std::cell::RefCell::new(Vec::new()) }; |
| 49 | 49 | } |
| 50 | 50 | |
| 51 | +/// lane-edge128: 64x64 CDEF units that took a NON-ZERO preset index from | |
| 52 | +/// another unit's `cdef_idx` literal -- the units of a whole 128x128 root | |
| 53 | +/// block, the only shape whose literal covers more than one unit. Zero makes | |
| 54 | +/// any witness for that rule vacuous (class `gate-blind-to-feature`). | |
| 55 | +static CDEF_COVERED: std::sync::atomic::AtomicUsize = std::sync::atomic::AtomicUsize::new(0); | |
| 56 | + | |
| 57 | +/// Takes and clears [`CDEF_COVERED`]. | |
| 58 | +#[allow(dead_code)] // read from the `#[cfg(test)]` witness | |
| 59 | +pub(crate) fn take_cdef_covered_units() -> usize { | |
| 60 | + CDEF_COVERED.swap(0, std::sync::atomic::Ordering::Relaxed) | |
| 61 | +} | |
| 62 | + | |
| 51 | 63 | /// Takes and clears [`PRESET_COUNTS`]. |
| 52 | 64 | #[allow(dead_code)] // read from the `#[cfg(test)]` BD gate |
| 53 | 65 | pub(crate) fn take_cdef_presets() -> Vec<(u8, Vec<usize>)> { |
| @@ -203,6 +215,12 @@ pub(crate) fn pick_filters( | ||
| 203 | 215 | damping: u8, |
| 204 | 216 | (sb_cols, sb_rows): (usize, usize), |
| 205 | 217 | lambda: f64, |
| 218 | + // lane-edge128: which unit's `cdef_idx` literal covers each 64x64 unit | |
| 219 | + // (`crate::tile::cdef_unit_owner`). A unit covered by ANOTHER unit's | |
| 220 | + // literal -- the four units of a whole 128x128 root block -- cannot be | |
| 221 | + // given a preset of its own, so its error is scored into its owner's and | |
| 222 | + // it takes its owner's index. Empty means the identity map. | |
| 223 | + cdef_owner: &[u32], | |
| 206 | 224 | ) -> Result<(LoopFilterParams, CdefParams, Vec<u8>, Picture)> { |
| 207 | 225 | let (cw, ch) = (fw.div_ceil(2), fh.div_ceil(2)); |
| 208 | 226 | let nsb = sb_cols * sb_rows; |
| @@ -252,11 +270,19 @@ pub(crate) fn pick_filters( | ||
| 252 | 270 | st.chroma.push((ckey, (sse, units))); |
| 253 | 271 | } |
| 254 | 272 | drop(sst); |
| 255 | - let (sse_y, sse_uv, sse64) = { | |
| 273 | + let (sse_y, sse_uv, mut sse64): (u64, u64, Vec<u64>) = { | |
| 256 | 274 | let l = &st.luma.iter().find(|(k, _)| *k == lkey).expect("just inserted").1; |
| 257 | 275 | let c = &st.chroma.iter().find(|(k, _)| *k == ckey).expect("just inserted").1; |
| 258 | 276 | (l.0, c.0, l.1.iter().zip(&c.1).map(|(a, b)| a + b).collect()) |
| 259 | 277 | }; |
| 278 | + // A unit that codes no literal of its own carries its error into the | |
| 279 | + // one that does, so every choice below is made per GROUP. | |
| 280 | + for (u, &owner) in cdef_owner.iter().enumerate() { | |
| 281 | + if owner as usize != u { | |
| 282 | + sse64[owner as usize] += sse64[u]; | |
| 283 | + sse64[u] = 0; | |
| 284 | + } | |
| 285 | + } | |
| 260 | 286 | st.trials.push(Trial { cand, picture, sse_y, sse_uv, sse64 }); |
| 261 | 287 | Ok(st.trials.len() - 1) |
| 262 | 288 | }; |
| @@ -382,7 +408,7 @@ pub(crate) fn pick_filters( | ||
| 382 | 408 | bits = b; |
| 383 | 409 | } |
| 384 | 410 | } |
| 385 | - let grid: Vec<u8> = if bits == 0 { | |
| 411 | + let mut grid: Vec<u8> = if bits == 0 { | |
| 386 | 412 | Vec::new() |
| 387 | 413 | } else { |
| 388 | 414 | let n = 1usize << bits; |
| @@ -394,6 +420,17 @@ pub(crate) fn pick_filters( | ||
| 394 | 420 | }) |
| 395 | 421 | .collect() |
| 396 | 422 | }; |
| 423 | + // The covered units scored zero above, so they would all read preset 0 | |
| 424 | + // here: give them the index their owner's literal actually carries. | |
| 425 | + if !grid.is_empty() { | |
| 426 | + for u in 0..nsb { | |
| 427 | + let owner = cdef_owner.get(u).map_or(u, |&o| o as usize); | |
| 428 | + if owner != u && grid[owner] != 0 { | |
| 429 | + CDEF_COVERED.fetch_add(1, std::sync::atomic::Ordering::Relaxed); | |
| 430 | + } | |
| 431 | + grid[u] = grid[owner]; | |
| 432 | + } | |
| 433 | + } | |
| 397 | 434 | let mut params = cdef(best, damping); |
| 398 | 435 | params.bits = bits; |
| 399 | 436 | for (i, c) in presets.iter().take(1usize << bits).enumerate() { |
crates/ec-av1/src/tile.rsmodified+38 -0
| @@ -669,6 +669,44 @@ impl TileLayout { | ||
| 669 | 669 | } |
| 670 | 670 | } |
| 671 | 671 | |
| 672 | +/// lane-edge128: which 64x64 CDEF unit's `cdef_idx` literal covers each unit | |
| 673 | +/// of this frame, in the same raster order [`arm_cdef_idx`]'s grid is in. | |
| 674 | +/// | |
| 675 | +/// `read_cdef` (spec 5.11.56) codes ONE literal for the block that first | |
| 676 | +/// reaches a unit and copies it over every unit that block SPANS, so a whole | |
| 677 | +/// 128x128 root block puts one literal in the stream for all four of its | |
| 678 | +/// units -- the encoder's CDEF search, which prices a preset per unit, may | |
| 679 | +/// not hand those four units different presets: only the first would be | |
| 680 | +/// coded, and every decoder (ours and ffmpeg's alike) would apply it to all | |
| 681 | +/// four while the encoder's own filter replay applied four different ones. | |
| 682 | +/// That is the shape this map exists to collapse | |
| 683 | +/// ([`crate::filter_search::pick_filters`]). | |
| 684 | +/// | |
| 685 | +/// Identity everywhere else: every block this writer codes at 64x64 or below | |
| 686 | +/// covers exactly one unit. | |
| 687 | +pub(crate) fn cdef_unit_owner(blocks: &[Quadrant], mi_cols: u32, mi_rows: u32) -> Vec<u32> { | |
| 688 | + let (cols, rows) = block_grid(mi_cols, mi_rows); | |
| 689 | + let (sb_cols, sb_rows) = (cols.div_ceil(2) as usize, rows.div_ceil(2) as usize); | |
| 690 | + let mut owner: Vec<u32> = (0..(sb_cols * sb_rows) as u32).collect(); | |
| 691 | + for sb_r in (0..sb_rows).step_by(2) { | |
| 692 | + for sb_c in (0..sb_cols).step_by(2) { | |
| 693 | + let root = ((sb_r * 2) < rows as usize && (sb_c * 2) < cols as usize) | |
| 694 | + .then(|| &blocks[(sb_r * 2) * cols as usize + sb_c * 2]) | |
| 695 | + .is_some_and(|q| matches!(q, Quadrant::Whole128(_))); | |
| 696 | + if !root { | |
| 697 | + continue; | |
| 698 | + } | |
| 699 | + let origin = (sb_r * sb_cols + sb_c) as u32; | |
| 700 | + for r in sb_r..(sb_r + 2).min(sb_rows) { | |
| 701 | + for c in sb_c..(sb_c + 2).min(sb_cols) { | |
| 702 | + owner[r * sb_cols + c] = origin; | |
| 703 | + } | |
| 704 | + } | |
| 705 | + } | |
| 706 | + } | |
| 707 | + owner | |
| 708 | +} | |
| 709 | + | |
| 672 | 710 | /// One frame's per-superblock `cdef_idx` plan (see [`arm_cdef_idx`]). |
| 673 | 711 | struct CdefIdxPlan { |
| 674 | 712 | /// The header's `cdef_bits`; never 0 while armed. |