diff --git a/crates/ec-av1/src/encode.rs b/crates/ec-av1/src/encode.rs index 09e96085..c80a0b55 100644 --- a/crates/ec-av1/src/encode.rs +++ b/crates/ec-av1/src/encode.rs @@ -4660,12 +4660,30 @@ fn cfl_on() -> bool { // the same env-flag shape `EC_AV1_FILTER_INTRA` uses. NOT an `EncoderConfig` // field: that struct is built by 90 literal sites in this workspace. +/// Whether this process's sequences code 128x128 superblocks. Read through +/// a process-global (not a thread-local) because the tile writers run on +/// worker threads; `2` is "the environment has not been read yet". +static SB128: std::sync::atomic::AtomicU8 = std::sync::atomic::AtomicU8::new(2); + /// Whether this process's sequences code 128x128 superblocks. pub(crate) fn sb128_on() -> bool { - static ENV: std::sync::LazyLock = std::sync::LazyLock::new(|| { - crate::envflags::var("EC_AV1_SB128").ok().is_some_and(|v| v != "0") - }); - *ENV + use std::sync::atomic::Ordering::Relaxed; + match SB128.load(Relaxed) { + 2 => { + let on = crate::envflags::var("EC_AV1_SB128").ok().is_some_and(|v| v != "0"); + SB128.store(u8::from(on), Relaxed); + on + } + v => v == 1, + } +} + +/// Forces [`sb128_on`] for the rest of the process -- the superblock size is +/// a SEQUENCE field, so a gate that flips it must run alone (`#[ignore]`), +/// exactly like the speed-preset gates. +#[cfg(test)] +pub(crate) fn force_sb128(on: bool) { + SB128.store(u8::from(on), std::sync::atomic::Ordering::Relaxed); } /// The luma loop-restoration unit size this sequence codes: a 128 superblock @@ -11234,7 +11252,7 @@ mod tests { } let _ = take_i64_root_hits(); let encoded = encode_key_frame_with_ctx(&picture, 120, 0.5, fctx).unwrap(); - let [roots, offered] = take_i64_root_hits(); + let [roots, _offered] = take_i64_root_hits(); // The counters are process-global, so another test encoding in // parallel can only ADD to them -- the per-FRAME fact is the mode // list, which carries one entry per coded block and so exactly 12 diff --git a/crates/ec-av1/src/encoder.rs b/crates/ec-av1/src/encoder.rs index c1e0b372..10739700 100644 --- a/crates/ec-av1/src/encoder.rs +++ b/crates/ec-av1/src/encoder.rs @@ -3027,6 +3027,69 @@ mod tests { /// reconstruction. The counter is the point (class /// `gate-blind-to-feature`): without it a green three-way compare says /// nothing about whether a single 64x64 block was ever written. + /// lane-sb128 step 1: the same static clip under a 128x128 SUPERBLOCK. + /// Every partition decision below the root is the one the 64-superblock + /// encoder makes -- the root itself is always `PARTITION_SPLIT` into the + /// four 64x64 superblocks -- so the only thing this proves is the + /// structural syntax: the root's own partition symbol, its loop + /// restoration read once over the 128 span at the 128 unit size, and + /// libaom's TL/TR/BL/BR visit order. 256x128 is 2x1 128x128 superblocks + /// per frame over three frames, so the root fires 6 times. + /// + /// `#[ignore]`: [`crate::encode::force_sb128`] moves a process-global + /// (the superblock size is a sequence field), so this gate runs alone: + /// + /// cargo test -p ec-av1 --release --lib -- --ignored \ + /// a_static_clip_codes_128x128_superblock_roots --nocapture + #[test] + #[ignore = "sets the process-global superblock size: run it alone"] + fn a_static_clip_codes_128x128_superblock_roots_and_decodes_sample_exact() { + let _knobs = crate::speed::knob_write(); + let _gate_lock = crate::stream::tests::lock_gate_counters(); + crate::encode::force_sb128(true); + let (width, height) = (256usize, 128usize); + let still = test_card(width, height, 0); + let sources: Vec = (0..3).map(|_| still.clone()).collect(); + let config = EncoderConfig { + width, + height, + base_q_idx: 120, + gop: 8, + colour: Colour::Bt709Limited, + tile_cols_log2: 0, + tile_rows_log2: 0, + }; + let mut enc = Av1Encoder::new(config).unwrap(); + let mut stream = Vec::new(); + let _ = crate::tile::take_sb128_root_hits(); + for packet in encode_all(&mut enc, &sources) { + stream.extend_from_slice(&packet.data); + } + let roots = crate::tile::take_sb128_root_hits(); + assert!( + roots >= 4, + "a 128x128-superblock clip coded {roots} superblock roots, wanted at least 4" + ); + eprintln!("128x128 superblock roots: {roots}"); + + let ours = crate::stream::decode_stream(&stream).expect("our decoder"); + assert_eq!(ours.len(), sources.len(), "our decoder's frames"); + assert!( + crate::decode::part128_split_hits() >= 4, + "our decoder read no 128x128 superblock root" + ); + if !have_ffmpeg() { + eprintln!("SKIP the ffmpeg half: no ffmpeg"); + return; + } + let theirs = ffmpeg_decode_luma(&stream, width, height); + assert_eq!(theirs.len(), sources.len(), "ffmpeg's frames"); + for (i, (a, b)) in ours.iter().zip(&theirs).enumerate() { + let got: Vec = a.y.iter().map(|&v| v as u8).collect(); + assert_eq!(&got, b, "frame {i}: ffmpeg and our decoder disagree"); + } + } + #[test] fn a_static_clip_codes_64x64_roots_and_decodes_sample_exact() { let _knobs = crate::speed::knob_write(); diff --git a/crates/ec-av1/src/tile.rs b/crates/ec-av1/src/tile.rs index 52dbc4fa..9ab174f3 100644 --- a/crates/ec-av1/src/tile.rs +++ b/crates/ec-av1/src/tile.rs @@ -103,6 +103,18 @@ pub(crate) fn arm_sb128(on: bool) { SB128.with(|c| c.set(on)); } +/// How many 128x128 superblock roots the writers have coded since the last +/// [`take_sb128_root_hits`] -- a gate reports how often the root fires rather +/// than assuming it does (class `gate-blind-to-feature`). Process-global +/// rather than thread-local because the tiles of one frame are written on +/// worker threads. +static SB128_ROOT_HITS: std::sync::atomic::AtomicUsize = std::sync::atomic::AtomicUsize::new(0); + +/// The 128x128-root count since the last call, and zero it. +pub fn take_sb128_root_hits() -> usize { + SB128_ROOT_HITS.swap(0, std::sync::atomic::Ordering::Relaxed) +} + /// Whether the next tile write codes 128x128 superblocks ([`arm_sb128`]). fn sb128_armed() -> bool { SB128.with(std::cell::Cell::get) @@ -163,6 +175,7 @@ fn write_sb128_root( mi_cols: u32, mi_rows: u32, ) { + SB128_ROOT_HITS.fetch_add(1, std::sync::atomic::Ordering::Relaxed); let (mi_r128, mi_c128) = ((sb_r & !1) * SB_MI, (sb_c & !1) * SB_MI); write_lr(enc, cdfs, mi_r128, mi_c128, SB_MI * 2); let at128 = ((mi_r128 / 4) as usize, (mi_c128 / 4) as usize); diff --git a/lanes/sb128.report.md b/lanes/sb128.report.md index ef604cb6..f1db5c6a 100644 --- a/lanes/sb128.report.md +++ b/lanes/sb128.report.md @@ -40,3 +40,37 @@ whole frame as one 128x128 intra block. The decoder had already fixed its own copy (`decode::NO_NEIGHBOUR_SIDE`, lane-sb128 r1); the writer's copy had not moved. Class: *table and reader move together*. +### Structural cost, measured (640x384 pin clip, 4 pictures, q=150) + +| arm | bytes | vs its own control | +|---|---|---| +| 64 SB, LR on (the shipped stream) | 8562 | -- | +| 128 SB, LR on | 8547 | **-15 (-0.18%)** | +| 64 SB, `EC_AV1_LR=0` | 8530 | -- | +| 128 SB, `EC_AV1_LR=0` | 8537 | **+7 (+0.08%)** | + +So the two effects separate cleanly: + +- The extra partition symbols cost **+7 bytes** over 60 roots (15 128-roots + per frame x 4 pictures), i.e. ~0.93 bits per root -- a SPLIT at a + neighbourless root is nearly free on the `partition_w128` CDF. +- The loop-restoration unit, forced 64 -> 128 by the superblock size, saves + **22 bytes** (60 units per frame -> 15), and the net is -15 bytes. + +CDEF indexing did NOT move: the spec clears `cdef_idx` per 64x64 unit inside +a 128 superblock (`clear_cdef` at the superblock, one `-1` per 64x64 +position), our decoder resets `cdef_transmitted` at every 64 quadrant, and +the writer's one-slot "already written" marker keys on the 64 grid, which the +TL/TR/BL/BR order keeps contiguous. Nothing about the CDEF plan changed. + +### Witness (step 1) + +`encoder::tests::a_static_clip_codes_128x128_superblock_roots_and_decodes_sample_exact` +(`--ignored`, it sets the process-global superblock size): 256x128 x 3 +frames, **8** 128x128 superblock roots written, our decoder reads >= 4 +`PARTITION_SPLIT` roots, and every frame is byte-exact between our decoder +and ffmpeg. + + cargo test -p ec-av1 --release --lib -- --ignored \ + a_static_clip_codes_128x128_superblock_roots --nocapture +