A 128x128-superblock witness, and the root counter behind it
Browse files at this commit6e9b2d84d6d687b6a0e2b68275fbf578e5855cd3Parent: d2e10de- test
- a 128x128-superblock witness, and the root counter behind it
4 changed files +133 -5
- crates/ec-av1/src/encode.rs +23 -5
- crates/ec-av1/src/encoder.rs +63 -0
- crates/ec-av1/src/tile.rs +13 -0
- lanes/sb128.report.md +34 -0
crates/ec-av1/src/encode.rsmodified+23 -5
| @@ -4660,12 +4660,30 @@ fn cfl_on() -> bool { | ||
| 4660 | 4660 | // the same env-flag shape `EC_AV1_FILTER_INTRA` uses. NOT an `EncoderConfig` |
| 4661 | 4661 | // field: that struct is built by 90 literal sites in this workspace. |
| 4662 | 4662 | |
| 4663 | +/// Whether this process's sequences code 128x128 superblocks. Read through | |
| 4664 | +/// a process-global (not a thread-local) because the tile writers run on | |
| 4665 | +/// worker threads; `2` is "the environment has not been read yet". | |
| 4666 | +static SB128: std::sync::atomic::AtomicU8 = std::sync::atomic::AtomicU8::new(2); | |
| 4667 | + | |
| 4663 | 4668 | /// Whether this process's sequences code 128x128 superblocks. |
| 4664 | 4669 | pub(crate) fn sb128_on() -> bool { |
| 4665 | - static ENV: std::sync::LazyLock<bool> = std::sync::LazyLock::new(|| { | |
| 4666 | - crate::envflags::var("EC_AV1_SB128").ok().is_some_and(|v| v != "0") | |
| 4667 | - }); | |
| 4668 | - *ENV | |
| 4670 | + use std::sync::atomic::Ordering::Relaxed; | |
| 4671 | + match SB128.load(Relaxed) { | |
| 4672 | + 2 => { | |
| 4673 | + let on = crate::envflags::var("EC_AV1_SB128").ok().is_some_and(|v| v != "0"); | |
| 4674 | + SB128.store(u8::from(on), Relaxed); | |
| 4675 | + on | |
| 4676 | + } | |
| 4677 | + v => v == 1, | |
| 4678 | + } | |
| 4679 | +} | |
| 4680 | + | |
| 4681 | +/// Forces [`sb128_on`] for the rest of the process -- the superblock size is | |
| 4682 | +/// a SEQUENCE field, so a gate that flips it must run alone (`#[ignore]`), | |
| 4683 | +/// exactly like the speed-preset gates. | |
| 4684 | +#[cfg(test)] | |
| 4685 | +pub(crate) fn force_sb128(on: bool) { | |
| 4686 | + SB128.store(u8::from(on), std::sync::atomic::Ordering::Relaxed); | |
| 4669 | 4687 | } |
| 4670 | 4688 | |
| 4671 | 4689 | /// The luma loop-restoration unit size this sequence codes: a 128 superblock |
| @@ -11234,7 +11252,7 @@ mod tests { | ||
| 11234 | 11252 | } |
| 11235 | 11253 | let _ = take_i64_root_hits(); |
| 11236 | 11254 | let encoded = encode_key_frame_with_ctx(&picture, 120, 0.5, fctx).unwrap(); |
| 11237 | - let [roots, offered] = take_i64_root_hits(); | |
| 11255 | + let [roots, _offered] = take_i64_root_hits(); | |
| 11238 | 11256 | // The counters are process-global, so another test encoding in |
| 11239 | 11257 | // parallel can only ADD to them -- the per-FRAME fact is the mode |
| 11240 | 11258 | // list, which carries one entry per coded block and so exactly 12 |
crates/ec-av1/src/encoder.rsmodified+63 -0
| @@ -3027,6 +3027,69 @@ mod tests { | ||
| 3027 | 3027 | /// reconstruction. The counter is the point (class |
| 3028 | 3028 | /// `gate-blind-to-feature`): without it a green three-way compare says |
| 3029 | 3029 | /// nothing about whether a single 64x64 block was ever written. |
| 3030 | + /// lane-sb128 step 1: the same static clip under a 128x128 SUPERBLOCK. | |
| 3031 | + /// Every partition decision below the root is the one the 64-superblock | |
| 3032 | + /// encoder makes -- the root itself is always `PARTITION_SPLIT` into the | |
| 3033 | + /// four 64x64 superblocks -- so the only thing this proves is the | |
| 3034 | + /// structural syntax: the root's own partition symbol, its loop | |
| 3035 | + /// restoration read once over the 128 span at the 128 unit size, and | |
| 3036 | + /// libaom's TL/TR/BL/BR visit order. 256x128 is 2x1 128x128 superblocks | |
| 3037 | + /// per frame over three frames, so the root fires 6 times. | |
| 3038 | + /// | |
| 3039 | + /// `#[ignore]`: [`crate::encode::force_sb128`] moves a process-global | |
| 3040 | + /// (the superblock size is a sequence field), so this gate runs alone: | |
| 3041 | + /// | |
| 3042 | + /// cargo test -p ec-av1 --release --lib -- --ignored \ | |
| 3043 | + /// a_static_clip_codes_128x128_superblock_roots --nocapture | |
| 3044 | + #[test] | |
| 3045 | + #[ignore = "sets the process-global superblock size: run it alone"] | |
| 3046 | + fn a_static_clip_codes_128x128_superblock_roots_and_decodes_sample_exact() { | |
| 3047 | + let _knobs = crate::speed::knob_write(); | |
| 3048 | + let _gate_lock = crate::stream::tests::lock_gate_counters(); | |
| 3049 | + crate::encode::force_sb128(true); | |
| 3050 | + let (width, height) = (256usize, 128usize); | |
| 3051 | + let still = test_card(width, height, 0); | |
| 3052 | + let sources: Vec<Picture> = (0..3).map(|_| still.clone()).collect(); | |
| 3053 | + let config = EncoderConfig { | |
| 3054 | + width, | |
| 3055 | + height, | |
| 3056 | + base_q_idx: 120, | |
| 3057 | + gop: 8, | |
| 3058 | + colour: Colour::Bt709Limited, | |
| 3059 | + tile_cols_log2: 0, | |
| 3060 | + tile_rows_log2: 0, | |
| 3061 | + }; | |
| 3062 | + let mut enc = Av1Encoder::new(config).unwrap(); | |
| 3063 | + let mut stream = Vec::new(); | |
| 3064 | + let _ = crate::tile::take_sb128_root_hits(); | |
| 3065 | + for packet in encode_all(&mut enc, &sources) { | |
| 3066 | + stream.extend_from_slice(&packet.data); | |
| 3067 | + } | |
| 3068 | + let roots = crate::tile::take_sb128_root_hits(); | |
| 3069 | + assert!( | |
| 3070 | + roots >= 4, | |
| 3071 | + "a 128x128-superblock clip coded {roots} superblock roots, wanted at least 4" | |
| 3072 | + ); | |
| 3073 | + eprintln!("128x128 superblock roots: {roots}"); | |
| 3074 | + | |
| 3075 | + let ours = crate::stream::decode_stream(&stream).expect("our decoder"); | |
| 3076 | + assert_eq!(ours.len(), sources.len(), "our decoder's frames"); | |
| 3077 | + assert!( | |
| 3078 | + crate::decode::part128_split_hits() >= 4, | |
| 3079 | + "our decoder read no 128x128 superblock root" | |
| 3080 | + ); | |
| 3081 | + if !have_ffmpeg() { | |
| 3082 | + eprintln!("SKIP the ffmpeg half: no ffmpeg"); | |
| 3083 | + return; | |
| 3084 | + } | |
| 3085 | + let theirs = ffmpeg_decode_luma(&stream, width, height); | |
| 3086 | + assert_eq!(theirs.len(), sources.len(), "ffmpeg's frames"); | |
| 3087 | + for (i, (a, b)) in ours.iter().zip(&theirs).enumerate() { | |
| 3088 | + let got: Vec<u8> = a.y.iter().map(|&v| v as u8).collect(); | |
| 3089 | + assert_eq!(&got, b, "frame {i}: ffmpeg and our decoder disagree"); | |
| 3090 | + } | |
| 3091 | + } | |
| 3092 | + | |
| 3030 | 3093 | #[test] |
| 3031 | 3094 | fn a_static_clip_codes_64x64_roots_and_decodes_sample_exact() { |
| 3032 | 3095 | let _knobs = crate::speed::knob_write(); |
crates/ec-av1/src/tile.rsmodified+13 -0
| @@ -103,6 +103,18 @@ pub(crate) fn arm_sb128(on: bool) { | ||
| 103 | 103 | SB128.with(|c| c.set(on)); |
| 104 | 104 | } |
| 105 | 105 | |
| 106 | +/// How many 128x128 superblock roots the writers have coded since the last | |
| 107 | +/// [`take_sb128_root_hits`] -- a gate reports how often the root fires rather | |
| 108 | +/// than assuming it does (class `gate-blind-to-feature`). Process-global | |
| 109 | +/// rather than thread-local because the tiles of one frame are written on | |
| 110 | +/// worker threads. | |
| 111 | +static SB128_ROOT_HITS: std::sync::atomic::AtomicUsize = std::sync::atomic::AtomicUsize::new(0); | |
| 112 | + | |
| 113 | +/// The 128x128-root count since the last call, and zero it. | |
| 114 | +pub fn take_sb128_root_hits() -> usize { | |
| 115 | + SB128_ROOT_HITS.swap(0, std::sync::atomic::Ordering::Relaxed) | |
| 116 | +} | |
| 117 | + | |
| 106 | 118 | /// Whether the next tile write codes 128x128 superblocks ([`arm_sb128`]). |
| 107 | 119 | fn sb128_armed() -> bool { |
| 108 | 120 | SB128.with(std::cell::Cell::get) |
| @@ -163,6 +175,7 @@ fn write_sb128_root( | ||
| 163 | 175 | mi_cols: u32, |
| 164 | 176 | mi_rows: u32, |
| 165 | 177 | ) { |
| 178 | + SB128_ROOT_HITS.fetch_add(1, std::sync::atomic::Ordering::Relaxed); | |
| 166 | 179 | let (mi_r128, mi_c128) = ((sb_r & !1) * SB_MI, (sb_c & !1) * SB_MI); |
| 167 | 180 | write_lr(enc, cdfs, mi_r128, mi_c128, SB_MI * 2); |
| 168 | 181 | let at128 = ((mi_r128 / 4) as usize, (mi_c128 / 4) as usize); |
lanes/sb128.report.mdmodified+34 -0
| @@ -40,3 +40,37 @@ whole frame as one 128x128 intra block. The decoder had already fixed its own | ||
| 40 | 40 | copy (`decode::NO_NEIGHBOUR_SIDE`, lane-sb128 r1); the writer's copy had not |
| 41 | 41 | moved. Class: *table and reader move together*. |
| 42 | 42 | |
| 43 | +### Structural cost, measured (640x384 pin clip, 4 pictures, q=150) | |
| 44 | + | |
| 45 | +| arm | bytes | vs its own control | | |
| 46 | +|---|---|---| | |
| 47 | +| 64 SB, LR on (the shipped stream) | 8562 | -- | | |
| 48 | +| 128 SB, LR on | 8547 | **-15 (-0.18%)** | | |
| 49 | +| 64 SB, `EC_AV1_LR=0` | 8530 | -- | | |
| 50 | +| 128 SB, `EC_AV1_LR=0` | 8537 | **+7 (+0.08%)** | | |
| 51 | + | |
| 52 | +So the two effects separate cleanly: | |
| 53 | + | |
| 54 | +- The extra partition symbols cost **+7 bytes** over 60 roots (15 128-roots | |
| 55 | + per frame x 4 pictures), i.e. ~0.93 bits per root -- a SPLIT at a | |
| 56 | + neighbourless root is nearly free on the `partition_w128` CDF. | |
| 57 | +- The loop-restoration unit, forced 64 -> 128 by the superblock size, saves | |
| 58 | + **22 bytes** (60 units per frame -> 15), and the net is -15 bytes. | |
| 59 | + | |
| 60 | +CDEF indexing did NOT move: the spec clears `cdef_idx` per 64x64 unit inside | |
| 61 | +a 128 superblock (`clear_cdef` at the superblock, one `-1` per 64x64 | |
| 62 | +position), our decoder resets `cdef_transmitted` at every 64 quadrant, and | |
| 63 | +the writer's one-slot "already written" marker keys on the 64 grid, which the | |
| 64 | +TL/TR/BL/BR order keeps contiguous. Nothing about the CDEF plan changed. | |
| 65 | + | |
| 66 | +### Witness (step 1) | |
| 67 | + | |
| 68 | +`encoder::tests::a_static_clip_codes_128x128_superblock_roots_and_decodes_sample_exact` | |
| 69 | +(`--ignored`, it sets the process-global superblock size): 256x128 x 3 | |
| 70 | +frames, **8** 128x128 superblock roots written, our decoder reads >= 4 | |
| 71 | +`PARTITION_SPLIT` roots, and every frame is byte-exact between our decoder | |
| 72 | +and ffmpeg. | |
| 73 | + | |
| 74 | + cargo test -p ec-av1 --release --lib -- --ignored \ | |
| 75 | + a_static_clip_codes_128x128_superblock_roots --nocapture | |
| 76 | + |