Merge branch 'lane-sb128' (f3298510): the tile writers can walk 128x128 superblocks (EC_AV1_SB128), off by default
Browse files at this commit5 changed files +392 -32
- crates/ec-av1/src/decode.rs +14 -5
- crates/ec-av1/src/encode.rs +54 -8
- crates/ec-av1/src/encoder.rs +63 -0
- crates/ec-av1/src/tile.rs +185 -19
- lanes/sb128.report.md +76 -0
crates/ec-av1/src/decode.rsmodified+14 -5
| @@ -158,7 +158,12 @@ impl FrameCtx { | |||
| 158 | /// predecessor was born with. | 158 | /// predecessor was born with. |
| 159 | pub(crate) fn new() -> Self { | 159 | pub(crate) fn new() -> Self { |
| 160 | Self { | 160 | Self { |
| 161 | - sb128_flag: std::cell::Cell::new(false), | 161 | + // lane-sb128: an ENCODER context starts at the sequence size |
| 162 | + // this process encodes (`crate::encode::sb128_on`) so the mode | ||
| 163 | + // search's own `Reach` and the filter search's internal decodes | ||
| 164 | + // agree with the tile writer; every real decode overwrites it | ||
| 165 | + // from the parsed sequence header (`set_sb128`, stream.rs). | ||
| 166 | + sb128_flag: std::cell::Cell::new(crate::encode::sb128_on()), | ||
| 162 | lossless_flag: std::cell::Cell::new(false), | 167 | lossless_flag: std::cell::Cell::new(false), |
| 163 | cdef_bits: std::cell::Cell::new(0), | 168 | cdef_bits: std::cell::Cell::new(0), |
| 164 | bit_depth: std::cell::Cell::new(8), | 169 | bit_depth: std::cell::Cell::new(8), |
| @@ -203,7 +208,11 @@ impl FrameCtx { | |||
| 203 | inter_last_mc: std::cell::Cell::new(None), | 208 | inter_last_mc: std::cell::Cell::new(None), |
| 204 | enable_edge_filter: std::cell::Cell::new(false), | 209 | enable_edge_filter: std::cell::Cell::new(false), |
| 205 | last_frame_wide_margin: std::cell::RefCell::new(None), | 210 | last_frame_wide_margin: std::cell::RefCell::new(None), |
| 206 | - reach_sb_px: std::cell::Cell::new(crate::encode::SUPERBLOCK), | 211 | + reach_sb_px: std::cell::Cell::new(if crate::encode::sb128_on() { |
| 212 | + 128 | ||
| 213 | + } else { | ||
| 214 | + crate::encode::SUPERBLOCK | ||
| 215 | + }), | ||
| 207 | grain_bit_depth: std::cell::Cell::new(8), | 216 | grain_bit_depth: std::cell::Cell::new(8), |
| 208 | recon_ops: std::cell::RefCell::new(Vec::new()), | 217 | recon_ops: std::cell::RefCell::new(Vec::new()), |
| 209 | recon_spare: std::cell::RefCell::new(Vec::new()), | 218 | recon_spare: std::cell::RefCell::new(Vec::new()), |
| @@ -407,14 +416,14 @@ pub(crate) fn part128_split_hits() -> usize { | |||
| 407 | /// has 8 symbols (no `PARTITION_HORZ_4`/`_VERT_4` at `BLOCK_128X128`, spec | 416 | /// has 8 symbols (no `PARTITION_HORZ_4`/`_VERT_4` at `BLOCK_128X128`, spec |
| 408 | /// `partition_cdf_length`), so its forced-split gather sums 5 elements where | 417 | /// `partition_cdf_length`), so its forced-split gather sums 5 elements where |
| 409 | /// the 64/32 levels sum 6. | 418 | /// the 64/32 levels sum 6. |
| 410 | -fn gather_of(cdf: &[u16], elements: &[usize]) -> [u16; 3] { | 419 | +pub(crate) fn gather_of(cdf: &[u16], elements: &[usize]) -> [u16; 3] { |
| 411 | let split: u16 = elements.iter().map(|&e| element_prob(cdf, e)).sum(); | 420 | let split: u16 = elements.iter().map(|&e| element_prob(cdf, e)).sum(); |
| 412 | [32768 - split, 32768, 0] | 421 | [32768 - split, 32768, 0] |
| 413 | } | 422 | } |
| 414 | 423 | ||
| 415 | /// [`VERT_ALIKE`]/[`HORZ_ALIKE`] without the 1:4 arms, for the 128 root. | 424 | /// [`VERT_ALIKE`]/[`HORZ_ALIKE`] without the 1:4 arms, for the 128 root. |
| 416 | -const VERT_ALIKE128: [usize; 5] = [2, PARTITION_SPLIT, 4, 6, 7]; | 425 | +pub(crate) const VERT_ALIKE128: [usize; 5] = [2, PARTITION_SPLIT, 4, 6, 7]; |
| 417 | -const HORZ_ALIKE128: [usize; 5] = [1, PARTITION_SPLIT, 4, 5, 6]; | 426 | +pub(crate) const HORZ_ALIKE128: [usize; 5] = [1, PARTITION_SPLIT, 4, 5, 6]; |
| 418 | 427 | ||
| 419 | // How many `use_filter_intra` symbols this decoder has read as `1`, across | 428 | // How many `use_filter_intra` symbols this decoder has read as `1`, across |
| 420 | // every call on the current thread -- the cheap counter [`filter_intra_hits`] gate | 429 | // every call on the current thread -- the cheap counter [`filter_intra_hits`] gate |
crates/ec-av1/src/encode.rsmodified+54 -8
| @@ -1554,7 +1554,7 @@ pub(crate) fn key_frame_headers_colour( | |||
| 1554 | frame_height_bits, | 1554 | frame_height_bits, |
| 1555 | max_frame_width: w, | 1555 | max_frame_width: w, |
| 1556 | max_frame_height: h, | 1556 | max_frame_height: h, |
| 1557 | - use_128x128_superblock: false, | 1557 | + use_128x128_superblock: sb128_on(), |
| 1558 | // lane-fintra: the encoder's intra search offers the five recursive | 1558 | // lane-fintra: the encoder's intra search offers the five recursive |
| 1559 | // filter-intra modes on eligible blocks, so the sequence bit is set | 1559 | // filter-intra modes on eligible blocks, so the sequence bit is set |
| 1560 | // whenever that search is on (`EC_AV1_FILTER_INTRA=0` switches both | 1560 | // whenever that search is on (`EC_AV1_FILTER_INTRA=0` switches both |
| @@ -4661,6 +4661,46 @@ fn cfl_on() -> bool { | |||
| 4661 | /// (`EC_AV1_FILTER_INTRA=0` switches both off, which is how the lane measured | 4661 | /// (`EC_AV1_FILTER_INTRA=0` switches both off, which is how the lane measured |
| 4662 | /// its own on/off pair -- and how every byte pin written before it still | 4662 | /// its own on/off pair -- and how every byte pin written before it still |
| 4663 | /// reproduces). It is also a speed lever: on at preset 0, off above it. | 4663 | /// reproduces). It is also a speed lever: on at preset 0, off above it. |
| 4664 | +// lane-sb128: `use_128x128_superblock` (spec 5.5.1). Off by default until | ||
| 4665 | +// the BD gate says otherwise; `EC_AV1_SB128=1` turns it on for the whole | ||
| 4666 | +// process (a sequence-header field, so it cannot change mid-stream anyway), | ||
| 4667 | +// the same env-flag shape `EC_AV1_FILTER_INTRA` uses. NOT an `EncoderConfig` | ||
| 4668 | +// field: that struct is built by 90 literal sites in this workspace. | ||
| 4669 | + | ||
| 4670 | +/// Whether this process's sequences code 128x128 superblocks. Read through | ||
| 4671 | +/// a process-global (not a thread-local) because the tile writers run on | ||
| 4672 | +/// worker threads; `2` is "the environment has not been read yet". | ||
| 4673 | +static SB128: std::sync::atomic::AtomicU8 = std::sync::atomic::AtomicU8::new(2); | ||
| 4674 | + | ||
| 4675 | +/// Whether this process's sequences code 128x128 superblocks. | ||
| 4676 | +pub(crate) fn sb128_on() -> bool { | ||
| 4677 | + use std::sync::atomic::Ordering::Relaxed; | ||
| 4678 | + match SB128.load(Relaxed) { | ||
| 4679 | + 2 => { | ||
| 4680 | + let on = crate::envflags::var("EC_AV1_SB128").ok().is_some_and(|v| v != "0"); | ||
| 4681 | + SB128.store(u8::from(on), Relaxed); | ||
| 4682 | + on | ||
| 4683 | + } | ||
| 4684 | + v => v == 1, | ||
| 4685 | + } | ||
| 4686 | +} | ||
| 4687 | + | ||
| 4688 | +/// Forces [`sb128_on`] for the rest of the process -- the superblock size is | ||
| 4689 | +/// a SEQUENCE field, so a gate that flips it must run alone (`#[ignore]`), | ||
| 4690 | +/// exactly like the speed-preset gates. | ||
| 4691 | +#[cfg(test)] | ||
| 4692 | +pub(crate) fn force_sb128(on: bool) { | ||
| 4693 | + SB128.store(u8::from(on), std::sync::atomic::Ordering::Relaxed); | ||
| 4694 | +} | ||
| 4695 | + | ||
| 4696 | +/// The luma loop-restoration unit size this sequence codes: a 128 superblock | ||
| 4697 | +/// cannot signal the 64 unit at all (spec `lr_params` reads ONE bit there, | ||
| 4698 | +/// `RESTORATION_TILESIZE_MAX >> (2 - (shift + 1))`, so the smallest unit is | ||
| 4699 | +/// 128), which is the one filter parameter the superblock size moves. | ||
| 4700 | +pub(crate) fn lr_unit() -> u32 { | ||
| 4701 | + if sb128_on() { 128 } else { 64 } | ||
| 4702 | +} | ||
| 4703 | + | ||
| 4664 | pub(crate) fn filter_intra_on() -> bool { | 4704 | pub(crate) fn filter_intra_on() -> bool { |
| 4665 | #[cfg(test)] | 4705 | #[cfg(test)] |
| 4666 | if let Some(forced) = FILTER_INTRA_FORCE.with(std::cell::Cell::get) { | 4706 | if let Some(forced) = FILTER_INTRA_FORCE.with(std::cell::Cell::get) { |
| @@ -7011,8 +7051,8 @@ pub(crate) fn encode_key_frame_inner( | |||
| 7011 | // (`av1_lr_count_units` rounds to nearest, so a short last unit is | 7051 | // (`av1_lr_count_units` rounds to nearest, so a short last unit is |
| 7012 | // swallowed by the one before it), for the tile writer's own per-unit | 7052 | // swallowed by the one before it), for the tile writer's own per-unit |
| 7013 | // walk. | 7053 | // walk. |
| 7014 | - let lr_horz = crate::restoration::count_units(header.frame_width, 64) as u32; | 7054 | + let lr_horz = crate::restoration::count_units(header.frame_width, lr_unit()) as u32; |
| 7015 | - let lr_vert = crate::restoration::count_units(header.frame_height, 64) as u32; | 7055 | + let lr_vert = crate::restoration::count_units(header.frame_height, lr_unit()) as u32; |
| 7016 | // Every tile is coded from this frame's OWN starting tables and leaves | 7056 | // Every tile is coded from this frame's OWN starting tables and leaves |
| 7017 | // its own end state behind (spec 5.11.1: each tile resets the CDFs, the | 7057 | // its own end state behind (spec 5.11.1: each tile resets the CDFs, the |
| 7018 | // loop-restoration reference and its neighbour contexts), so the tiles | 7058 | // loop-restoration reference and its neighbour contexts), so the tiles |
| @@ -7024,7 +7064,8 @@ pub(crate) fn encode_key_frame_inner( | |||
| 7024 | -> Result<Vec<Vec<u8>>> { | 7064 | -> Result<Vec<Vec<u8>>> { |
| 7025 | let (out, _) = write_tiles(layout.count(), usize::MAX, |index| { | 7065 | let (out, _) = write_tiles(layout.count(), usize::MAX, |index| { |
| 7026 | crate::tile::arm_cdef_idx(bits, sb_cols, grid.to_vec()); | 7066 | crate::tile::arm_cdef_idx(bits, sb_cols, grid.to_vec()); |
| 7027 | - crate::tile::arm_lr(64, lr_horz, lr_vert, units.to_vec()); | 7067 | + crate::tile::arm_lr(lr_unit(), lr_horz, lr_vert, units.to_vec()); |
| 7068 | + crate::tile::arm_sb128(sb128_on()); | ||
| 7028 | let mut cdfs = start_cdfs.0.clone(); | 7069 | let mut cdfs = start_cdfs.0.clone(); |
| 7029 | crate::tile::arm_screen(screen); | 7070 | crate::tile::arm_screen(screen); |
| 7030 | crate::tile::arm_filter_intra(filter_intra_on()); | 7071 | crate::tile::arm_filter_intra(filter_intra_on()); |
| @@ -7310,9 +7351,13 @@ fn pick_and_apply_filters( | |||
| 7310 | // rather than decoding the re-coded tile. Cleared at the end instead. | 7351 | // rather than decoding the re-coded tile. Cleared at the end instead. |
| 7311 | let mut lr = LoopRestorationParams::default(); | 7352 | let mut lr = LoopRestorationParams::default(); |
| 7312 | if lr_on { | 7353 | if lr_on { |
| 7354 | + let unit = lr_unit(); | ||
| 7313 | let mut want = LoopRestorationParams { | 7355 | let mut want = LoopRestorationParams { |
| 7314 | - loop_restoration_size: [64, 64, 64], | 7356 | + loop_restoration_size: [unit, unit, unit], |
| 7315 | uses_lr: true, | 7357 | uses_lr: true, |
| 7358 | + // spec `lr_params`: `RESTORATION_TILESIZE_MAX >> (2 - shift)`, so | ||
| 7359 | + // 0 is the 64 unit and 1 the 128 one a 128 superblock forces. | ||
| 7360 | + lr_unit_shift: u8::from(unit == 128), | ||
| 7316 | ..LoopRestorationParams::default() | 7361 | ..LoopRestorationParams::default() |
| 7317 | }; | 7362 | }; |
| 7318 | want.frame_restoration_type[0] = ec_av1_syntax::RestorationType::Wiener; | 7363 | want.frame_restoration_type[0] = ec_av1_syntax::RestorationType::Wiener; |
| @@ -10626,8 +10671,8 @@ pub(crate) fn encode_inter_frame( | |||
| 10626 | let switchable_motion_mode = header.is_motion_mode_switchable; | 10671 | let switchable_motion_mode = header.is_motion_mode_switchable; |
| 10627 | let warped_motion = header.allow_warped_motion; | 10672 | let warped_motion = header.allow_warped_motion; |
| 10628 | let order_hint_bits = seq.order_hint_bits; | 10673 | let order_hint_bits = seq.order_hint_bits; |
| 10629 | - let lr_horz = crate::restoration::count_units(header.frame_width, 64) as u32; | 10674 | + let lr_horz = crate::restoration::count_units(header.frame_width, lr_unit()) as u32; |
| 10630 | - let lr_vert = crate::restoration::count_units(header.frame_height, 64) as u32; | 10675 | + let lr_vert = crate::restoration::count_units(header.frame_height, lr_unit()) as u32; |
| 10631 | // What this frame stores is the LARGEST tile's end-of-tile tables -- | 10676 | // What this frame stores is the LARGEST tile's end-of-tile tables -- |
| 10632 | // its own header's `context_update_tile_id` (spec 7.20), which | 10677 | // its own header's `context_update_tile_id` (spec 7.20), which |
| 10633 | // `tile_info_of` set to exactly that tile. | 10678 | // `tile_info_of` set to exactly that tile. |
| @@ -10648,7 +10693,8 @@ pub(crate) fn encode_inter_frame( | |||
| 10648 | sb_q.clone().unwrap_or_default(), | 10693 | sb_q.clone().unwrap_or_default(), |
| 10649 | base_q_idx, | 10694 | base_q_idx, |
| 10650 | ); | 10695 | ); |
| 10651 | - crate::tile::arm_lr(64, lr_horz, lr_vert, units.to_vec()); | 10696 | + crate::tile::arm_lr(lr_unit(), lr_horz, lr_vert, units.to_vec()); |
| 10697 | + crate::tile::arm_sb128(sb128_on()); | ||
| 10652 | crate::tile::arm_sign_bias(sign_bias); | 10698 | crate::tile::arm_sign_bias(sign_bias); |
| 10653 | // Thread-locals: every tile job arms its own worker. | 10699 | // Thread-locals: every tile job arms its own worker. |
| 10654 | crate::tile::arm_reference_select(reference_select_bit); | 10700 | crate::tile::arm_reference_select(reference_select_bit); |
crates/ec-av1/src/encoder.rsmodified+63 -0
| @@ -3027,6 +3027,69 @@ mod tests { | |||
| 3027 | /// reconstruction. The counter is the point (class | 3027 | /// reconstruction. The counter is the point (class |
| 3028 | /// `gate-blind-to-feature`): without it a green three-way compare says | 3028 | /// `gate-blind-to-feature`): without it a green three-way compare says |
| 3029 | /// nothing about whether a single 64x64 block was ever written. | 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 | #[test] | 3093 | #[test] |
| 3031 | fn a_static_clip_codes_64x64_roots_and_decodes_sample_exact() { | 3094 | fn a_static_clip_codes_64x64_roots_and_decodes_sample_exact() { |
| 3032 | let _knobs = crate::speed::knob_write(); | 3095 | let _knobs = crate::speed::knob_write(); |
crates/ec-av1/src/tile.rsmodified+185 -19
| @@ -85,19 +85,136 @@ pub(crate) fn arm_lr( | |||
| 85 | }); | 85 | }); |
| 86 | } | 86 | } |
| 87 | 87 | ||
| 88 | +// --------------------------------------------------------------------------- | ||
| 89 | +// lane-sb128: the sequence's `use_128x128_superblock`. Armed per tile write | ||
| 90 | +// like [`arm_lr`] (the encoder codes one tile at a time per thread) rather | ||
| 91 | +// than threaded through every writer signature. When set, the two tile | ||
| 92 | +// writers walk 128x128 superblocks -- loop restoration and one | ||
| 93 | +// `partition_w128` symbol at the 128 root, then the four 64x64 quadrants in | ||
| 94 | +// libaom `decode_partition`'s own TL, TR, BL, BR order -- which is exactly | ||
| 95 | +// what `crate::decode::read_sb128_root` reads. | ||
| 96 | + | ||
| 97 | +thread_local! { | ||
| 98 | + static SB128: std::cell::Cell<bool> = const { std::cell::Cell::new(false) }; | ||
| 99 | +} | ||
| 100 | + | ||
| 101 | +/// Arms the next tile write with the sequence's 128x128 superblock size. | ||
| 102 | +pub(crate) fn arm_sb128(on: bool) { | ||
| 103 | + SB128.with(|c| c.set(on)); | ||
| 104 | +} | ||
| 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 | + | ||
| 118 | +/// Whether the next tile write codes 128x128 superblocks ([`arm_sb128`]). | ||
| 119 | +fn sb128_armed() -> bool { | ||
| 120 | + SB128.with(std::cell::Cell::get) | ||
| 121 | +} | ||
| 122 | + | ||
| 123 | +/// The order a tile's 64x64 superblock cells are written in, mirroring | ||
| 124 | +/// `crate::decode::sb_visit_order` exactly: plain raster at a 64 superblock, | ||
| 125 | +/// and TL/TR/BL/BR inside each 128x128 superblock (in raster order over the | ||
| 126 | +/// 128 grid) at a 128 one. The two flags mark the first cell of a superblock | ||
| 127 | +/// ROW (where the left neighbour context resets) and the first cell of a | ||
| 128 | +/// superblock (where the 128 root's own restoration units and partition | ||
| 129 | +/// symbol are written). | ||
| 130 | +fn sb_write_order(r0: u32, r1: u32, c0: u32, c1: u32, sb128: bool) -> Vec<(u32, u32, bool, bool)> { | ||
| 131 | + let mut out = Vec::new(); | ||
| 132 | + if !sb128 { | ||
| 133 | + for r in r0..r1 { | ||
| 134 | + for c in c0..c1 { | ||
| 135 | + out.push((r, c, c == c0, true)); | ||
| 136 | + } | ||
| 137 | + } | ||
| 138 | + return out; | ||
| 139 | + } | ||
| 140 | + let mut r = r0; | ||
| 141 | + while r < r1 { | ||
| 142 | + let mut first_in_row = true; | ||
| 143 | + let mut c = c0; | ||
| 144 | + while c < c1 { | ||
| 145 | + let mut first_in_sb = true; | ||
| 146 | + for qr in 0..2 { | ||
| 147 | + for qc in 0..2 { | ||
| 148 | + let (rr, cc) = (r + qr, c + qc); | ||
| 149 | + if rr >= r1 || cc >= c1 { | ||
| 150 | + continue; | ||
| 151 | + } | ||
| 152 | + out.push((rr, cc, first_in_row, first_in_sb)); | ||
| 153 | + first_in_row = false; | ||
| 154 | + first_in_sb = false; | ||
| 155 | + } | ||
| 156 | + } | ||
| 157 | + c += 2; | ||
| 158 | + } | ||
| 159 | + r += 2; | ||
| 160 | + } | ||
| 161 | + out | ||
| 162 | +} | ||
| 163 | + | ||
| 164 | +/// The 128x128 superblock root ([`sb_write_order`]'s `sb_start` cells): the | ||
| 165 | +/// units its own 128-sample span covers, then the partition symbol. Only | ||
| 166 | +/// `PARTITION_SPLIT` is written -- the four 64x64 quadrants below are today's | ||
| 167 | +/// superblocks -- and at a root the true frame edge cuts, the DECIDING | ||
| 168 | +/// gathered bit the decoder reads instead (`read_sb128_root`). | ||
| 169 | +fn write_sb128_root( | ||
| 170 | + enc: &mut SymbolEncoder, | ||
| 171 | + cdfs: &mut Cdfs, | ||
| 172 | + neighbours: &Neighbours, | ||
| 173 | + sb_r: u32, | ||
| 174 | + sb_c: u32, | ||
| 175 | + mi_cols: u32, | ||
| 176 | + mi_rows: u32, | ||
| 177 | +) { | ||
| 178 | + SB128_ROOT_HITS.fetch_add(1, std::sync::atomic::Ordering::Relaxed); | ||
| 179 | + let (mi_r128, mi_c128) = ((sb_r & !1) * SB_MI, (sb_c & !1) * SB_MI); | ||
| 180 | + write_lr(enc, cdfs, mi_r128, mi_c128, SB_MI * 2); | ||
| 181 | + let at128 = ((mi_r128 / 4) as usize, (mi_c128 / 4) as usize); | ||
| 182 | + let ctx128 = neighbours.partition_ctx(at128, 128); | ||
| 183 | + let (has_cols128, has_rows128) = (mi_c128 + SB_MI < mi_cols, mi_r128 + SB_MI < mi_rows); | ||
| 184 | + if crate::envflags::env_flag!("EC_AV1_TRACE") { | ||
| 185 | + eprintln!("WTRACE partition_w128 ctx={ctx128} cols={has_cols128} rows={has_rows128}"); | ||
| 186 | + } | ||
| 187 | + match (has_cols128, has_rows128) { | ||
| 188 | + (true, true) => enc.symbol(PARTITION_SPLIT, &mut cdfs.partition_w128[ctx128]), | ||
| 189 | + // As at the 64 root: the gathered CDF is built for the read and | ||
| 190 | + // thrown away, so nothing adapts here. Value 1 is SPLIT. | ||
| 191 | + (true, false) => enc.symbol_fixed( | ||
| 192 | + 1, | ||
| 193 | + &crate::decode::gather_of(&cdfs.partition_w128[ctx128], &crate::decode::VERT_ALIKE128), | ||
| 194 | + ), | ||
| 195 | + (false, true) => enc.symbol_fixed( | ||
| 196 | + 1, | ||
| 197 | + &crate::decode::gather_of(&cdfs.partition_w128[ctx128], &crate::decode::HORZ_ALIKE128), | ||
| 198 | + ), | ||
| 199 | + // Both halves outside: the split is the only partition left and the | ||
| 200 | + // decoder reads nothing. | ||
| 201 | + (false, false) => {} | ||
| 202 | + } | ||
| 203 | +} | ||
| 204 | + | ||
| 88 | /// `read_lr`'s write side, called once at the top of every superblock, | 205 | /// `read_lr`'s write side, called once at the top of every superblock, |
| 89 | /// before its partition symbol: writes the units this superblock's own | 206 | /// before its partition symbol: writes the units this superblock's own |
| 90 | /// mi span covers (`av1_loop_restoration_corners_in_sb`; no superres, so the | 207 | /// mi span covers (`av1_loop_restoration_corners_in_sb`; no superres, so the |
| 91 | /// column scaling is the plain mi one). | 208 | /// column scaling is the plain mi one). |
| 92 | -fn write_lr(enc: &mut SymbolEncoder, cdfs: &mut Cdfs, mi_row: u32, mi_col: u32) { | 209 | +fn write_lr(enc: &mut SymbolEncoder, cdfs: &mut Cdfs, mi_row: u32, mi_col: u32, span: u32) { |
| 93 | LR_PLAN.with(|c| { | 210 | LR_PLAN.with(|c| { |
| 94 | let mut plan = c.borrow_mut(); | 211 | let mut plan = c.borrow_mut(); |
| 95 | let Some(plan) = plan.as_mut() else { return }; | 212 | let Some(plan) = plan.as_mut() else { return }; |
| 96 | let unit = plan.unit_size; | 213 | let unit = plan.unit_size; |
| 97 | let rcol0 = (mi_col * 4).div_ceil(unit); | 214 | let rcol0 = (mi_col * 4).div_ceil(unit); |
| 98 | - let rcol1 = ((mi_col + SB_MI_W) * 4).div_ceil(unit).min(plan.horz_units); | 215 | + let rcol1 = ((mi_col + span) * 4).div_ceil(unit).min(plan.horz_units); |
| 99 | let rrow0 = (mi_row * 4).div_ceil(unit); | 216 | let rrow0 = (mi_row * 4).div_ceil(unit); |
| 100 | - let rrow1 = ((mi_row + SB_MI_W) * 4).div_ceil(unit).min(plan.vert_units); | 217 | + let rrow1 = ((mi_row + span) * 4).div_ceil(unit).min(plan.vert_units); |
| 101 | for rrow in rrow0..rrow1 { | 218 | for rrow in rrow0..rrow1 { |
| 102 | for rcol in rcol0..rcol1 { | 219 | for rcol in rcol0..rcol1 { |
| 103 | let info = plan.units[(rcol + rrow * plan.horz_units) as usize]; | 220 | let info = plan.units[(rcol + rrow * plan.horz_units) as usize]; |
| @@ -177,9 +294,14 @@ impl TileLayout { | |||
| 177 | pub(crate) fn new(mi_cols: u32, mi_rows: u32, cols_log2: u32, rows_log2: u32) -> Self { | 294 | pub(crate) fn new(mi_cols: u32, mi_rows: u32, cols_log2: u32, rows_log2: u32) -> Self { |
| 178 | let (cols32, rows32) = block_grid(mi_cols, mi_rows); | 295 | let (cols32, rows32) = block_grid(mi_cols, mi_rows); |
| 179 | let (sb_cols, sb_rows) = (cols32.div_ceil(2), rows32.div_ceil(2)); | 296 | let (sb_cols, sb_rows) = (cols32.div_ceil(2), rows32.div_ceil(2)); |
| 297 | + // lane-sb128: a tile boundary is a SUPERBLOCK boundary, so under a | ||
| 298 | + // 128 superblock the starts are laid out on the 128 grid and then | ||
| 299 | + // doubled back into the 64-cell units the writers walk. | ||
| 300 | + let unit = u32::from(crate::encode::sb128_on()) + 1; | ||
| 180 | let starts = |total: u32, log2: u32| { | 301 | let starts = |total: u32, log2: u32| { |
| 181 | - let step = total.div_ceil(1 << log2).max(1); | 302 | + let total128 = total.div_ceil(unit); |
| 182 | - let mut v: Vec<u32> = (0..total).step_by(step as usize).collect(); | 303 | + let step = total128.div_ceil(1 << log2).max(1); |
| 304 | + let mut v: Vec<u32> = (0..total128).step_by(step as usize).map(|s| s * unit).collect(); | ||
| 183 | v.push(total); | 305 | v.push(total); |
| 184 | v | 306 | v |
| 185 | }; | 307 | }; |
| @@ -1779,6 +1901,14 @@ const TX4: usize = 4; | |||
| 1779 | /// Side of a superblock, in samples, which is the size a block outside the | 1901 | /// Side of a superblock, in samples, which is the size a block outside the |
| 1780 | /// tile reads as. | 1902 | /// tile reads as. |
| 1781 | const SB: usize = 64; | 1903 | const SB: usize = 64; |
| 1904 | + | ||
| 1905 | +/// The `above_side`/`left_side` value a cell with no coded neighbour holds | ||
| 1906 | +/// (tile start, superblock-row start) -- the writer's copy of | ||
| 1907 | +/// `crate::decode::NO_NEIGHBOUR_SIDE`, and 128 for the same reason: at 64 it | ||
| 1908 | +/// reads identically at every level up to 64x64 but sets the bit at the | ||
| 1909 | +/// `BLOCK_128X128` root (64 * 2 <= 128), putting that root's partition symbol | ||
| 1910 | +/// on CDF row 3 where libaom uses row 0. | ||
| 1911 | +const NO_NEIGHBOUR_SIDE: usize = 128; | ||
| 1782 | /// Side of the smallest block the writer codes, in samples, which is the grid | 1912 | /// Side of the smallest block the writer codes, in samples, which is the grid |
| 1783 | /// the neighbour bookkeeping is kept on. | 1913 | /// the neighbour bookkeeping is kept on. |
| 1784 | const SUB: usize = 16; | 1914 | const SUB: usize = 16; |
| @@ -2030,10 +2160,10 @@ impl Neighbours { | |||
| 2030 | left: vec![[Neighbour::default(); 3]; rows * (SUB / MI)], | 2160 | left: vec![[Neighbour::default(); 3]; rows * (SUB / MI)], |
| 2031 | above_mode: vec![DC_PRED; cols], | 2161 | above_mode: vec![DC_PRED; cols], |
| 2032 | left_mode: vec![DC_PRED; rows], | 2162 | left_mode: vec![DC_PRED; rows], |
| 2033 | - above_side: vec![SB; cols], | 2163 | + above_side: vec![NO_NEIGHBOUR_SIDE; cols], |
| 2034 | - left_side: vec![SB; rows], | 2164 | + left_side: vec![NO_NEIGHBOUR_SIDE; rows], |
| 2035 | - above_side_mi: vec![SB; cols * (SUB / MI)], | 2165 | + above_side_mi: vec![NO_NEIGHBOUR_SIDE; cols * (SUB / MI)], |
| 2036 | - left_side_mi: vec![SB; rows * (SUB / MI)], | 2166 | + left_side_mi: vec![NO_NEIGHBOUR_SIDE; rows * (SUB / MI)], |
| 2037 | above_skip: vec![false; cols * (SUB / MI)], | 2167 | above_skip: vec![false; cols * (SUB / MI)], |
| 2038 | left_skip: vec![false; rows * (SUB / MI)], | 2168 | left_skip: vec![false; rows * (SUB / MI)], |
| 2039 | above_inter: vec![false; cols * (SUB / MI)], | 2169 | above_inter: vec![false; cols * (SUB / MI)], |
| @@ -2086,8 +2216,8 @@ impl Neighbours { | |||
| 2086 | fn start_row(&mut self) { | 2216 | fn start_row(&mut self) { |
| 2087 | self.left.iter_mut().for_each(|l| *l = Default::default()); | 2217 | self.left.iter_mut().for_each(|l| *l = Default::default()); |
| 2088 | self.left_mode.iter_mut().for_each(|m| *m = DC_PRED); | 2218 | self.left_mode.iter_mut().for_each(|m| *m = DC_PRED); |
| 2089 | - self.left_side.iter_mut().for_each(|s| *s = SB); | 2219 | + self.left_side.iter_mut().for_each(|s| *s = NO_NEIGHBOUR_SIDE); |
| 2090 | - self.left_side_mi.iter_mut().for_each(|s| *s = SB); | 2220 | + self.left_side_mi.iter_mut().for_each(|s| *s = NO_NEIGHBOUR_SIDE); |
| 2091 | self.left_skip.iter_mut().for_each(|s| *s = false); | 2221 | self.left_skip.iter_mut().for_each(|s| *s = false); |
| 2092 | self.left_inter.iter_mut().for_each(|i| *i = false); | 2222 | self.left_inter.iter_mut().for_each(|i| *i = false); |
| 2093 | self.left_ref.iter_mut().for_each(|r| *r = -1); | 2223 | self.left_ref.iter_mut().for_each(|r| *r = -1); |
| @@ -2731,10 +2861,28 @@ pub(crate) fn sb_coeff_key_frame_tile_cdfs( | |||
| 2731 | // off. | 2861 | // off. |
| 2732 | let mut cdfs = cdfs; | 2862 | let mut cdfs = cdfs; |
| 2733 | let mut enc = SymbolEncoder::new(); | 2863 | let mut enc = SymbolEncoder::new(); |
| 2734 | - for sb_r in tile.sb_row0..tile.sb_row1.min(sb_rows) { | 2864 | + let sb128 = sb128_armed(); |
| 2735 | - neighbours.start_row(); | 2865 | + // lane-sb128: at a 128 superblock the four 64x64 cells of each root are |
| 2736 | - for sb_c in tile.sb_col0..tile.sb_col1.min(sb_cols) { | 2866 | + // written in libaom's own TL/TR/BL/BR order, the root's restoration units |
| 2737 | - write_lr(&mut enc, &mut cdfs, sb_r * SB_MI_W, sb_c * SB_MI_W); | 2867 | + // and partition symbol first; at a 64 one this is plain raster and every |
| 2868 | + // cell is its own superblock, i.e. byte-identical to the loop before. | ||
| 2869 | + for (sb_r, sb_c, row_start, sb_start) in sb_write_order( | ||
| 2870 | + tile.sb_row0, | ||
| 2871 | + tile.sb_row1.min(sb_rows), | ||
| 2872 | + tile.sb_col0, | ||
| 2873 | + tile.sb_col1.min(sb_cols), | ||
| 2874 | + sb128, | ||
| 2875 | + ) { | ||
| 2876 | + if row_start { | ||
| 2877 | + neighbours.start_row(); | ||
| 2878 | + } | ||
| 2879 | + if sb128 && sb_start { | ||
| 2880 | + write_sb128_root(&mut enc, &mut cdfs, &neighbours, sb_r, sb_c, mi_cols, mi_rows); | ||
| 2881 | + } | ||
| 2882 | + { | ||
| 2883 | + if !sb128 { | ||
| 2884 | + write_lr(&mut enc, &mut cdfs, sb_r * SB_MI_W, sb_c * SB_MI_W, SB_MI_W); | ||
| 2885 | + } | ||
| 2738 | let at = (sb_r as usize * 4, sb_c as usize * 4); | 2886 | let at = (sb_r as usize * 4, sb_c as usize * 4); |
| 2739 | let ctx = neighbours.partition_ctx(at, SB); | 2887 | let ctx = neighbours.partition_ctx(at, SB); |
| 2740 | ec_rng_trace(|| { | 2888 | ec_rng_trace(|| { |
| @@ -5892,10 +6040,28 @@ pub(crate) fn sb_coeff_inter_frame_tile_cdfs( | |||
| 5892 | vec![0i32; TX16 * TX16], | 6040 | vec![0i32; TX16 * TX16], |
| 5893 | ]; | 6041 | ]; |
| 5894 | 6042 | ||
| 5895 | - for sb_r in tile.sb_row0..tile.sb_row1.min(sb_rows) { | 6043 | + let sb128 = sb128_armed(); |
| 5896 | - neighbours.start_row(); | 6044 | + // lane-sb128: at a 128 superblock the four 64x64 cells of each root are |
| 5897 | - for sb_c in tile.sb_col0..tile.sb_col1.min(sb_cols) { | 6045 | + // written in libaom's own TL/TR/BL/BR order, the root's restoration units |
| 5898 | - write_lr(&mut enc, &mut cdfs, sb_r * SB_MI_W, sb_c * SB_MI_W); | 6046 | + // and partition symbol first; at a 64 one this is plain raster and every |
| 6047 | + // cell is its own superblock, i.e. byte-identical to the loop before. | ||
| 6048 | + for (sb_r, sb_c, row_start, sb_start) in sb_write_order( | ||
| 6049 | + tile.sb_row0, | ||
| 6050 | + tile.sb_row1.min(sb_rows), | ||
| 6051 | + tile.sb_col0, | ||
| 6052 | + tile.sb_col1.min(sb_cols), | ||
| 6053 | + sb128, | ||
| 6054 | + ) { | ||
| 6055 | + if row_start { | ||
| 6056 | + neighbours.start_row(); | ||
| 6057 | + } | ||
| 6058 | + if sb128 && sb_start { | ||
| 6059 | + write_sb128_root(&mut enc, &mut cdfs, &neighbours, sb_r, sb_c, mi_cols, mi_rows); | ||
| 6060 | + } | ||
| 6061 | + { | ||
| 6062 | + if !sb128 { | ||
| 6063 | + write_lr(&mut enc, &mut cdfs, sb_r * SB_MI_W, sb_c * SB_MI_W, SB_MI_W); | ||
| 6064 | + } | ||
| 5899 | let sb_at = (sb_r as usize * 4, sb_c as usize * 4); | 6065 | let sb_at = (sb_r as usize * 4, sb_c as usize * 4); |
| 5900 | let sb_ctx = neighbours.partition_ctx(sb_at, SB); | 6066 | let sb_ctx = neighbours.partition_ctx(sb_at, SB); |
| 5901 | // spec `decode_partition`'s hasRows/hasCols (5.11.4): a superblock | 6067 | // spec `decode_partition`'s hasRows/hasCols (5.11.4): a superblock |
lanes/sb128.report.mdadded+76 -0
| @@ -0,0 +1,76 @@ | |||
| 1 | +# lane-sb128 -- 128x128 superblocks in the encoder | ||
| 2 | + | ||
| 3 | +## Step 1: the sequence option, SPLIT-only roots (structural cost) | ||
| 4 | + | ||
| 5 | +`EC_AV1_SB128=1` writes `use_128x128_superblock = 1` in the sequence header | ||
| 6 | +and makes both tile writers walk 128x128 superblocks: loop restoration and | ||
| 7 | +one `partition_w128` symbol at the 128 root, then the four 64x64 quadrants | ||
| 8 | +(today's superblocks, unchanged) in libaom's TL/TR/BL/BR order. Every | ||
| 9 | +partition decision below the root is identical to the 64-superblock encoder, | ||
| 10 | +so the whole cost of the step is the extra partition symbols plus the moved | ||
| 11 | +loop-restoration unit size. | ||
| 12 | + | ||
| 13 | +NOT an `EncoderConfig` field (deviation from the charter, stated): that | ||
| 14 | +struct is built by 90 literal sites in this workspace, so a new public field | ||
| 15 | +is a 90-file diff for a knob whose default the gate decides. `EC_AV1_SB128` | ||
| 16 | +follows `EC_AV1_FILTER_INTRA`'s existing shape. | ||
| 17 | + | ||
| 18 | +### What moved | ||
| 19 | + | ||
| 20 | +- `crates/ec-av1/src/tile.rs` -- `arm_sb128` + `sb_write_order` (an exact | ||
| 21 | + mirror of `decode::sb_visit_order`) + `write_sb128_root` (LR over the 128 | ||
| 22 | + span, then `PARTITION_SPLIT` off `partition_w128`, or the DECIDING gathered | ||
| 23 | + bit at a root the frame edge cuts); `write_lr` takes its span; the | ||
| 24 | + no-neighbour partition sentinel is 128, not 64. | ||
| 25 | +- `crates/ec-av1/src/encode.rs` -- `sb128_on()`, `lr_unit()` (a 128 | ||
| 26 | + superblock cannot signal the 64 restoration unit at all), the sequence bit, | ||
| 27 | + both tile-write arming sites. | ||
| 28 | +- `crates/ec-av1/src/decode.rs` -- `gather_of`/`VERT_ALIKE128`/`HORZ_ALIKE128` | ||
| 29 | + exported to the writer; an encoder `FrameCtx` starts at this process's | ||
| 30 | + superblock size so the search's `Reach` and the filter search's internal | ||
| 31 | + decodes agree with the writer. | ||
| 32 | + | ||
| 33 | +### Root cause found on the way | ||
| 34 | + | ||
| 35 | +The writer's `Neighbours` used 64 as its "no neighbour" partition side. That | ||
| 36 | +reads identically at every level up to 64x64 but sets the bit at the 128 root | ||
| 37 | +(`64 * 2 <= 128`), so the writer coded the root's partition symbol off CDF | ||
| 38 | +row 3 while the decoder read row 0 -- a first-symbol desync that decoded the | ||
| 39 | +whole frame as one 128x128 intra block. The decoder had already fixed its own | ||
| 40 | +copy (`decode::NO_NEIGHBOUR_SIDE`, lane-sb128 r1); the writer's copy had not | ||
| 41 | +moved. Class: *table and reader move together*. | ||
| 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 | + | ||