The 128 root offers the compound pair candidates
Browse files at this commit53ce20dc4b47e3b000298deeb14a2dda112e1a8dParent: f677e4c- feat
- the 128 root offers the compound pair candidates
2 changed files +182 -5
- crates/ec-av1/src/encode.rs +168 -5
- crates/ec-av1/src/tile.rs +14 -0
crates/ec-av1/src/encode.rsmodified+168 -5
| @@ -762,6 +762,30 @@ fn b128_residual() -> bool { | |||
| 762 | && b128_root() | 762 | && b128_root() |
| 763 | } | 763 | } |
| 764 | 764 | ||
| 765 | +/// lane-b128r: whether the 128 root offers the COMPOUND candidates the 64 | ||
| 766 | +/// root already does (`NEAREST_NEARESTMV`, `GLOBAL_GLOBALMV`, | ||
| 767 | +/// `NEW_NEARESTMV` over the LAST+GOLDEN / LAST+ALTREF pairs), at the block's | ||
| 768 | +/// own 32x32-mi window. `EC_AV1_B128COMP=0` turns it off, which is this | ||
| 769 | +/// arm's attribution control; the writer has always had the path | ||
| 770 | +/// (`write_inter_block_128` calls `write_compound_block` for a block that | ||
| 771 | +/// names a second reference). | ||
| 772 | +fn b128_compound() -> bool { | ||
| 773 | + static ENV: std::sync::LazyLock<Option<bool>> = std::sync::LazyLock::new(|| { | ||
| 774 | + crate::envflags::var("EC_AV1_B128COMP").ok().map(|v| v != "0") | ||
| 775 | + }); | ||
| 776 | + ENV.unwrap_or(true) && b128_root() | ||
| 777 | +} | ||
| 778 | + | ||
| 779 | +/// How many 128 roots came out COMPOUND since the last | ||
| 780 | +/// [`take_b128_compound_hits`] (class `gate-blind-to-feature`). | ||
| 781 | +static B128_COMPOUND_HITS: std::sync::atomic::AtomicUsize = | ||
| 782 | + std::sync::atomic::AtomicUsize::new(0); | ||
| 783 | + | ||
| 784 | +/// The compound-128 count since the last call, and zero it. | ||
| 785 | +pub fn take_b128_compound_hits() -> usize { | ||
| 786 | + B128_COMPOUND_HITS.swap(0, std::sync::atomic::Ordering::Relaxed) | ||
| 787 | +} | ||
| 788 | + | ||
| 765 | /// Test-only: take the 128 root's residual arm whatever it costs, so a | 789 | /// Test-only: take the 128 root's residual arm whatever it costs, so a |
| 766 | /// witness clip codes the shape rather than waiting for content on which the | 790 | /// witness clip codes the shape rather than waiting for content on which the |
| 767 | /// RD picks it (the same role [`force_sb128`] plays for the superblock size). | 791 | /// RD picks it (the same role [`force_sb128`] plays for the superblock size). |
| @@ -769,10 +793,25 @@ fn b128_residual() -> bool { | |||
| 769 | static FORCE_B128_RESIDUAL: std::sync::atomic::AtomicBool = | 793 | static FORCE_B128_RESIDUAL: std::sync::atomic::AtomicBool = |
| 770 | std::sync::atomic::AtomicBool::new(false); | 794 | std::sync::atomic::AtomicBool::new(false); |
| 771 | 795 | ||
| 772 | -/// Arms [`FORCE_B128_RESIDUAL`] for the rest of the process. | 796 | +/// Test-only twin of [`FORCE_B128_RESIDUAL`] for the PARTITION decision: take |
| 797 | +/// the 128 root whenever it produced a candidate at all, whatever its four | ||
| 798 | +/// cells cost, so a witness codes the block it is about rather than waiting | ||
| 799 | +/// for content on which the root also wins its own comparison. | ||
| 800 | +static FORCE_B128_ROOT: std::sync::atomic::AtomicBool = | ||
| 801 | + std::sync::atomic::AtomicBool::new(false); | ||
| 802 | + | ||
| 803 | +/// Arms [`FORCE_B128_RESIDUAL`] -- and with it [`FORCE_B128_ROOT`], since a | ||
| 804 | +/// forced residual block only reaches the writer if its root is taken. | ||
| 773 | #[cfg(test)] | 805 | #[cfg(test)] |
| 774 | pub(crate) fn force_b128_residual(on: bool) { | 806 | pub(crate) fn force_b128_residual(on: bool) { |
| 775 | FORCE_B128_RESIDUAL.store(on, std::sync::atomic::Ordering::Relaxed); | 807 | FORCE_B128_RESIDUAL.store(on, std::sync::atomic::Ordering::Relaxed); |
| 808 | + force_b128_root(on); | ||
| 809 | +} | ||
| 810 | + | ||
| 811 | +/// Arms [`FORCE_B128_ROOT`] for the rest of the process. | ||
| 812 | +#[cfg(test)] | ||
| 813 | +pub(crate) fn force_b128_root(on: bool) { | ||
| 814 | + FORCE_B128_ROOT.store(on, std::sync::atomic::Ordering::Relaxed); | ||
| 776 | } | 815 | } |
| 777 | 816 | ||
| 778 | /// How many 128 roots came out cheaper WITH a residual than skipped, since | 817 | /// How many 128 roots came out cheaper WITH a residual than skipped, since |
| @@ -9445,6 +9484,11 @@ fn search_root_128( | |||
| 9445 | tpl_cells_x: usize, | 9484 | tpl_cells_x: usize, |
| 9446 | base_q_idx: u8, | 9485 | base_q_idx: u8, |
| 9447 | reference: &Picture, | 9486 | reference: &Picture, |
| 9487 | + // The `LAST` + extra-reference pairs at THIS ROOT's own 32x32-mi window, | ||
| 9488 | + // built by the caller off the same grid the tile writer rebuilds its | ||
| 9489 | + // stack from (empty unless the frame carries `reference_select` and | ||
| 9490 | + // [`b128_compound`] is on). | ||
| 9491 | + compound: &[(i8, &Picture, crate::mvstack::CompoundMvStack)], | ||
| 9448 | fctx: &crate::decode::FrameCtx, | 9492 | fctx: &crate::decode::FrameCtx, |
| 9449 | ) -> Option<(f64, f64, BlockCoeffs, [(Vec<u8>, Vec<CoefCtx>); 3])> { | 9493 | ) -> Option<(f64, f64, BlockCoeffs, [(Vec<u8>, Vec<CoefCtx>); 3])> { |
| 9450 | const SIDE: usize = SUPERBLOCK * 2; | 9494 | const SIDE: usize = SUPERBLOCK * 2; |
| @@ -9510,10 +9554,13 @@ fn search_root_128( | |||
| 9510 | false, | 9554 | false, |
| 9511 | reference, | 9555 | reference, |
| 9512 | &stack, | 9556 | &stack, |
| 9513 | - &[], | 9557 | + compound, |
| 9514 | fctx, | 9558 | fctx, |
| 9515 | ); | 9559 | ); |
| 9516 | let (mut cost, mut block) = found?; | 9560 | let (mut cost, mut block) = found?; |
| 9561 | + if block.inter.is_some_and(|i| i.ref1.is_some()) { | ||
| 9562 | + B128_COMPOUND_HITS.fetch_add(1, std::sync::atomic::Ordering::Relaxed); | ||
| 9563 | + } | ||
| 9517 | // lane-b128r: the same prediction with a REAL residual, priced against | 9564 | // lane-b128r: the same prediction with a REAL residual, priced against |
| 9518 | // the skip arm the search just committed to the planes (which IS the | 9565 | // the skip arm the search just committed to the planes (which IS the |
| 9519 | // winner's prediction, sample for sample). The block's residual is four | 9566 | // winner's prediction, sample for sample). The block's residual is four |
| @@ -12263,6 +12310,40 @@ pub(crate) fn encode_inter_frame( | |||
| 12263 | // 128x128 candidate is weighed. | 12310 | // 128x128 candidate is weighed. |
| 12264 | let mark128 = blocks.len(); | 12311 | let mark128 = blocks.len(); |
| 12265 | let mut root_cost = 0.0f64; | 12312 | let mut root_cost = 0.0f64; |
| 12313 | + // The 128 root's own compound pairs, at its 32x32-mi window (the 64 | ||
| 12314 | + // root builds the same list one size down, per superblock). | ||
| 12315 | + let root_compound: Vec<(i8, &Picture, crate::mvstack::CompoundMvStack)> = | ||
| 12316 | + if header.reference_select && b128_compound() && !screen && sb128_search { | ||
| 12317 | + [ | ||
| 12318 | + (crate::mvstack::GOLDEN_FRAME, golden), | ||
| 12319 | + (crate::mvstack::ALTREF_FRAME, altref), | ||
| 12320 | + ] | ||
| 12321 | + .into_iter() | ||
| 12322 | + .filter_map(|(r, pic)| { | ||
| 12323 | + pic.map(|p| { | ||
| 12324 | + ( | ||
| 12325 | + r, | ||
| 12326 | + p, | ||
| 12327 | + crate::mvstack::find_mv_stack_compound( | ||
| 12328 | + &grid, | ||
| 12329 | + root_r * 16, | ||
| 12330 | + root_c * 16, | ||
| 12331 | + 32, | ||
| 12332 | + 32, | ||
| 12333 | + (crate::mvstack::LAST_FRAME, r), | ||
| 12334 | + mi_cols, | ||
| 12335 | + mi_rows, | ||
| 12336 | + grid.sign_bias_table(), | ||
| 12337 | + &[(0, 0); 7], | ||
| 12338 | + None, | ||
| 12339 | + ), | ||
| 12340 | + ) | ||
| 12341 | + }) | ||
| 12342 | + }) | ||
| 12343 | + .collect() | ||
| 12344 | + } else { | ||
| 12345 | + Vec::new() | ||
| 12346 | + }; | ||
| 12266 | let root128 = (sb128_search && b128_root() && cells.len() == 4) | 12347 | let root128 = (sb128_search && b128_root() && cells.len() == 4) |
| 12267 | .then(|| search_root_128( | 12348 | .then(|| search_root_128( |
| 12268 | &mut luma, | 12349 | &mut luma, |
| @@ -12278,6 +12359,7 @@ pub(crate) fn encode_inter_frame( | |||
| 12278 | tpl_cells_x, | 12359 | tpl_cells_x, |
| 12279 | base_q_idx, | 12360 | base_q_idx, |
| 12280 | reference, | 12361 | reference, |
| 12362 | + &root_compound, | ||
| 12281 | fctx, | 12363 | fctx, |
| 12282 | )) | 12364 | )) |
| 12283 | .flatten(); | 12365 | .flatten(); |
| @@ -12863,7 +12945,7 @@ pub(crate) fn encode_inter_frame( | |||
| 12863 | && (cost128 < root_cost + lambda128 * crate::tile::partition_bits(128, true) | 12945 | && (cost128 < root_cost + lambda128 * crate::tile::partition_bits(128, true) |
| 12864 | // The witness knob takes the root whatever it costs, so the | 12946 | // The witness knob takes the root whatever it costs, so the |
| 12865 | // forced residual block really reaches the writer. | 12947 | // forced residual block really reaches the writer. |
| 12866 | - || FORCE_B128_RESIDUAL.load(std::sync::atomic::Ordering::Relaxed)) | 12948 | + || FORCE_B128_ROOT.load(std::sync::atomic::Ordering::Relaxed)) |
| 12867 | { | 12949 | { |
| 12868 | B128_NONE_HITS.fetch_add(1, std::sync::atomic::Ordering::Relaxed); | 12950 | B128_NONE_HITS.fetch_add(1, std::sync::atomic::Ordering::Relaxed); |
| 12869 | let (x128, y128) = (root_c * SUPERBLOCK, root_r * SUPERBLOCK); | 12951 | let (x128, y128) = (root_c * SUPERBLOCK, root_r * SUPERBLOCK); |
| @@ -12920,8 +13002,17 @@ pub(crate) fn encode_inter_frame( | |||
| 12920 | .iter() | 13002 | .iter() |
| 12921 | .filter(|q| matches!(q, Quadrant::Whole128(b) if !b.skip)) | 13003 | .filter(|q| matches!(q, Quadrant::Whole128(b) if !b.skip)) |
| 12922 | .count(); | 13004 | .count(); |
| 13005 | + // lane-b128r: and how many named a second reference (the compound | ||
| 13006 | + // arm at the 128 root). | ||
| 13007 | + let compound = blocks | ||
| 13008 | + .iter() | ||
| 13009 | + .filter(|q| { | ||
| 13010 | + matches!(q, Quadrant::Whole128(b) if b.inter.is_some_and(|i| i.ref1.is_some())) | ||
| 13011 | + }) | ||
| 13012 | + .count(); | ||
| 12923 | eprintln!( | 13013 | eprintln!( |
| 12924 | - "B128 census: inter frame, {whole} whole 128x128 roots ({residual} with a residual), {roots} coded blocks ({:.1}% of the frame's area at 128)", | 13014 | + "B128 census: inter frame, {whole} whole 128x128 roots ({residual} with a residual, \ |
| 13015 | + {compound} compound), {roots} coded blocks ({:.1}% of the frame's area at 128)", | ||
| 12925 | 100.0 * (whole * 16) as f64 / blocks.len().max(1) as f64, | 13016 | 100.0 * (whole * 16) as f64 / blocks.len().max(1) as f64, |
| 12926 | ); | 13017 | ); |
| 12927 | } | 13018 | } |
| @@ -16285,6 +16376,73 @@ mod tests { | |||
| 16285 | } | 16376 | } |
| 16286 | } | 16377 | } |
| 16287 | 16378 | ||
| 16379 | + /// lane-b128r: the COMPOUND arm at the 128 root -- one 128x128 block | ||
| 16380 | + /// naming `LAST` plus `GOLDEN`/`ALTREF`, through the same | ||
| 16381 | + /// `write_compound_block` chain every smaller compound block takes, at a | ||
| 16382 | + /// window it had never been exercised at. RED before this lane: the | ||
| 16383 | + /// search passed an empty compound list at the 128 root, so the count was | ||
| 16384 | + /// 0 and no such block existed to decode. | ||
| 16385 | + /// | ||
| 16386 | + /// cargo test -p ec-av1 --release --lib -- --ignored \ | ||
| 16387 | + /// a_128_root_compound_block --nocapture | ||
| 16388 | + #[test] | ||
| 16389 | + #[ignore = "sets the process-global superblock size: run it alone"] | ||
| 16390 | + fn a_128_root_compound_block_decodes_exact_through_both_decoders() { | ||
| 16391 | + let _knobs = crate::speed::knob_write(); | ||
| 16392 | + if !have_ffmpeg() { | ||
| 16393 | + eprintln!("SKIP: no ffmpeg"); | ||
| 16394 | + return; | ||
| 16395 | + } | ||
| 16396 | + force_sb128(true); | ||
| 16397 | + let dir = std::env::temp_dir().join(format!("ec-av1-b128c-{}", std::process::id())); | ||
| 16398 | + std::fs::create_dir_all(&dir).unwrap(); | ||
| 16399 | + let clip = dir.join("testsrc2-1280x768.y4m"); | ||
| 16400 | + // Moving objects that occlude each other: a pan is coded by one | ||
| 16401 | + // reference outright, and a compound pair only pays where the two | ||
| 16402 | + // references disagree. | ||
| 16403 | + let ok = Command::new("ffmpeg") | ||
| 16404 | + .args(["-v", "error", "-y", "-f", "lavfi"]) | ||
| 16405 | + .args(["-i", "testsrc2=size=1280x768:rate=24"]) | ||
| 16406 | + .args(["-frames:v", "12", "-pix_fmt", "yuv420p"]) | ||
| 16407 | + .arg(&clip) | ||
| 16408 | + .status() | ||
| 16409 | + .expect("ffmpeg failed to run") | ||
| 16410 | + .success(); | ||
| 16411 | + assert!(ok, "ffmpeg could not write the synthetic clip"); | ||
| 16412 | + let (width, height) = (1280usize, 768usize); | ||
| 16413 | + let pictures = | ||
| 16414 | + crate::probe::source(clip.to_str().unwrap(), "0", "null", width, height, 12); | ||
| 16415 | + // The compound candidate wins the 128 block's own search on this pan | ||
| 16416 | + // but the root then loses to its four cells, so the root is forced | ||
| 16417 | + // (the residual arm is left at its default, off). | ||
| 16418 | + force_b128_root(true); | ||
| 16419 | + let _ = take_b128_compound_hits(); | ||
| 16420 | + let _ = crate::tile::take_sb128_compound_hits(); | ||
| 16421 | + let encoded = encode_sequence(&pictures, 150, 0.5).unwrap(); | ||
| 16422 | + let (won, coded) = | ||
| 16423 | + (take_b128_compound_hits(), crate::tile::take_sb128_compound_hits()); | ||
| 16424 | + eprintln!("COMPOUND 128x128 blocks: search won {won}, writer coded {coded}"); | ||
| 16425 | + assert!(coded > 0, "no compound 128x128 block was coded on this clip"); | ||
| 16426 | + let decoded = crate::stream::decode_stream(&encoded.stream).expect("our decoder"); | ||
| 16427 | + assert_eq!(decoded.len(), encoded.frames.len(), "frame count"); | ||
| 16428 | + for (i, (frame, dec)) in encoded.frames.iter().zip(&decoded).enumerate() { | ||
| 16429 | + for (plane, got, want) in [ | ||
| 16430 | + ("luma", &dec.y, &frame.reconstruction.y), | ||
| 16431 | + ("U", &dec.u, &frame.reconstruction.u), | ||
| 16432 | + ("V", &dec.v, &frame.reconstruction.v), | ||
| 16433 | + ] { | ||
| 16434 | + let differ = got.iter().zip(want.iter()).filter(|(a, b)| a != b).count(); | ||
| 16435 | + assert_eq!(differ, 0, "frame {i}: {plane} -- {differ} samples differ"); | ||
| 16436 | + } | ||
| 16437 | + } | ||
| 16438 | + let ff = ffmpeg_decode_sequence(&encoded.stream, width, height, pictures.len()); | ||
| 16439 | + for (i, (frame, dec)) in encoded.frames.iter().zip(&ff).enumerate() { | ||
| 16440 | + assert_eq!(dec.y, frame.reconstruction.y, "frame {i}: ffmpeg luma"); | ||
| 16441 | + assert_eq!(dec.u, frame.reconstruction.u, "frame {i}: ffmpeg U"); | ||
| 16442 | + assert_eq!(dec.v, frame.reconstruction.v, "frame {i}: ffmpeg V"); | ||
| 16443 | + } | ||
| 16444 | + } | ||
| 16445 | + | ||
| 16288 | #[test] | 16446 | #[test] |
| 16289 | #[ignore = "sets the process-global superblock size: run it alone"] | 16447 | #[ignore = "sets the process-global superblock size: run it alone"] |
| 16290 | fn a_128_superblock_clip_whose_drl_index_the_write_time_stack_cannot_carry_decodes_exact() { | 16448 | fn a_128_superblock_clip_whose_drl_index_the_write_time_stack_cannot_carry_decodes_exact() { |
| @@ -17751,7 +17909,12 @@ mod tests { | |||
| 17751 | // q=150 and 33194 at q=60. `EC_AV1_SB128=0` restores 8252 / 33053. | 17909 | // q=150 and 33194 at q=60. `EC_AV1_SB128=0` restores 8252 / 33053. |
| 17752 | // Re-taken at the merge of lane-b128m onto main (128 superblocks on | 17910 | // Re-taken at the merge of lane-b128m onto main (128 superblocks on |
| 17753 | // by default, on top of the ARF temporal filter). | 17911 | // by default, on top of the ARF temporal filter). |
| 17754 | - let pins: [(u8, usize, u64); 2] = [(150, 8354, 0x1290b8628438c6d4), (60, 33222, 0xc7f92971ab1264c3)]; | 17912 | + // Re-taken on lane-b128r: the 128 root now offers the COMPOUND |
| 17913 | + // candidates the 64 root already did ([`b128_compound`]), so every | ||
| 17914 | + // inter frame that carries `reference_select` codes different blocks | ||
| 17915 | + // -- 8354 -> 8364 bytes at q=150 and 33222 -> 33257 at q=60. | ||
| 17916 | + // `EC_AV1_B128COMP=0` restores 8354 / 33222 exactly. | ||
| 17917 | + let pins: [(u8, usize, u64); 2] = [(150, 8364, 0xcb2ad7936d5127c5), (60, 33257, 0x87db250ba556e5a6)]; | ||
| 17755 | let coded: Vec<(u8, usize, u64)> = pins | 17918 | let coded: Vec<(u8, usize, u64)> = pins |
| 17756 | .iter() | 17919 | .iter() |
| 17757 | .map(|&(q, _, _)| { | 17920 | .map(|&(q, _, _)| { |
crates/ec-av1/src/tile.rsmodified+14 -0
| @@ -458,6 +458,9 @@ fn write_inter_block_128( | |||
| 458 | if !block.skip { | 458 | if !block.skip { |
| 459 | SB128_RESIDUAL_HITS.fetch_add(1, std::sync::atomic::Ordering::Relaxed); | 459 | SB128_RESIDUAL_HITS.fetch_add(1, std::sync::atomic::Ordering::Relaxed); |
| 460 | } | 460 | } |
| 461 | + if info.ref1.is_some() { | ||
| 462 | + SB128_COMPOUND_HITS.fetch_add(1, std::sync::atomic::Ordering::Relaxed); | ||
| 463 | + } | ||
| 461 | Ok(()) | 464 | Ok(()) |
| 462 | } | 465 | } |
| 463 | 466 | ||
| @@ -472,6 +475,17 @@ pub fn take_sb128_residual_hits() -> usize { | |||
| 472 | SB128_RESIDUAL_HITS.swap(0, std::sync::atomic::Ordering::Relaxed) | 475 | SB128_RESIDUAL_HITS.swap(0, std::sync::atomic::Ordering::Relaxed) |
| 473 | } | 476 | } |
| 474 | 477 | ||
| 478 | +/// lane-b128r: how many of those blocks named a SECOND reference (the | ||
| 479 | +/// compound arm at the 128 root), since the last | ||
| 480 | +/// [`take_sb128_compound_hits`]. | ||
| 481 | +static SB128_COMPOUND_HITS: std::sync::atomic::AtomicUsize = | ||
| 482 | + std::sync::atomic::AtomicUsize::new(0); | ||
| 483 | + | ||
| 484 | +/// The compound-128 count since the last call, and zero it. | ||
| 485 | +pub fn take_sb128_compound_hits() -> usize { | ||
| 486 | + SB128_COMPOUND_HITS.swap(0, std::sync::atomic::Ordering::Relaxed) | ||
| 487 | +} | ||
| 488 | + | ||
| 475 | /// How many 128x128 `PARTITION_NONE` blocks the inter writer has coded since | 489 | /// How many 128x128 `PARTITION_NONE` blocks the inter writer has coded since |
| 476 | /// the last [`take_sb128_none_hits`] (class `gate-blind-to-feature`). | 490 | /// the last [`take_sb128_none_hits`] (class `gate-blind-to-feature`). |
| 477 | static SB128_NONE_HITS: std::sync::atomic::AtomicUsize = std::sync::atomic::AtomicUsize::new(0); | 491 | static SB128_NONE_HITS: std::sync::atomic::AtomicUsize = std::sync::atomic::AtomicUsize::new(0); |