The tile writers walk 128x128 superblocks under EC_AV1_SB128
Browse files at this commitd2e10de1e982958ec3a31bf8e30564f877c50719Parent: f82ceec- feat
- the tile writers walk 128x128 superblocks under EC_AV1_SB128
4 changed files +262 -30
- crates/ec-av1/src/decode.rs +14 -5
- crates/ec-av1/src/encode.rs +36 -8
- crates/ec-av1/src/tile.rs +170 -17
- lanes/sb128.report.md +42 -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+36 -8
| @@ -1547,7 +1547,7 @@ pub(crate) fn key_frame_headers_colour( | |||
| 1547 | frame_height_bits, | 1547 | frame_height_bits, |
| 1548 | max_frame_width: w, | 1548 | max_frame_width: w, |
| 1549 | max_frame_height: h, | 1549 | max_frame_height: h, |
| 1550 | - use_128x128_superblock: false, | 1550 | + use_128x128_superblock: sb128_on(), |
| 1551 | // lane-fintra: the encoder's intra search offers the five recursive | 1551 | // lane-fintra: the encoder's intra search offers the five recursive |
| 1552 | // filter-intra modes on eligible blocks, so the sequence bit is set | 1552 | // filter-intra modes on eligible blocks, so the sequence bit is set |
| 1553 | // whenever that search is on (`EC_AV1_FILTER_INTRA=0` switches both | 1553 | // whenever that search is on (`EC_AV1_FILTER_INTRA=0` switches both |
| @@ -4654,6 +4654,28 @@ fn cfl_on() -> bool { | |||
| 4654 | /// (`EC_AV1_FILTER_INTRA=0` switches both off, which is how the lane measured | 4654 | /// (`EC_AV1_FILTER_INTRA=0` switches both off, which is how the lane measured |
| 4655 | /// its own on/off pair -- and how every byte pin written before it still | 4655 | /// its own on/off pair -- and how every byte pin written before it still |
| 4656 | /// reproduces). It is also a speed lever: on at preset 0, off above it. | 4656 | /// reproduces). It is also a speed lever: on at preset 0, off above it. |
| 4657 | +// lane-sb128: `use_128x128_superblock` (spec 5.5.1). Off by default until | ||
| 4658 | +// the BD gate says otherwise; `EC_AV1_SB128=1` turns it on for the whole | ||
| 4659 | +// process (a sequence-header field, so it cannot change mid-stream anyway), | ||
| 4660 | +// the same env-flag shape `EC_AV1_FILTER_INTRA` uses. NOT an `EncoderConfig` | ||
| 4661 | +// field: that struct is built by 90 literal sites in this workspace. | ||
| 4662 | + | ||
| 4663 | +/// Whether this process's sequences code 128x128 superblocks. | ||
| 4664 | +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 | ||
| 4669 | +} | ||
| 4670 | + | ||
| 4671 | +/// The luma loop-restoration unit size this sequence codes: a 128 superblock | ||
| 4672 | +/// cannot signal the 64 unit at all (spec `lr_params` reads ONE bit there, | ||
| 4673 | +/// `RESTORATION_TILESIZE_MAX >> (2 - (shift + 1))`, so the smallest unit is | ||
| 4674 | +/// 128), which is the one filter parameter the superblock size moves. | ||
| 4675 | +pub(crate) fn lr_unit() -> u32 { | ||
| 4676 | + if sb128_on() { 128 } else { 64 } | ||
| 4677 | +} | ||
| 4678 | + | ||
| 4657 | pub(crate) fn filter_intra_on() -> bool { | 4679 | pub(crate) fn filter_intra_on() -> bool { |
| 4658 | #[cfg(test)] | 4680 | #[cfg(test)] |
| 4659 | if let Some(forced) = FILTER_INTRA_FORCE.with(std::cell::Cell::get) { | 4681 | if let Some(forced) = FILTER_INTRA_FORCE.with(std::cell::Cell::get) { |
| @@ -7004,8 +7026,8 @@ pub(crate) fn encode_key_frame_inner( | |||
| 7004 | // (`av1_lr_count_units` rounds to nearest, so a short last unit is | 7026 | // (`av1_lr_count_units` rounds to nearest, so a short last unit is |
| 7005 | // swallowed by the one before it), for the tile writer's own per-unit | 7027 | // swallowed by the one before it), for the tile writer's own per-unit |
| 7006 | // walk. | 7028 | // walk. |
| 7007 | - let lr_horz = crate::restoration::count_units(header.frame_width, 64) as u32; | 7029 | + let lr_horz = crate::restoration::count_units(header.frame_width, lr_unit()) as u32; |
| 7008 | - let lr_vert = crate::restoration::count_units(header.frame_height, 64) as u32; | 7030 | + let lr_vert = crate::restoration::count_units(header.frame_height, lr_unit()) as u32; |
| 7009 | // Every tile is coded from this frame's OWN starting tables and leaves | 7031 | // Every tile is coded from this frame's OWN starting tables and leaves |
| 7010 | // its own end state behind (spec 5.11.1: each tile resets the CDFs, the | 7032 | // its own end state behind (spec 5.11.1: each tile resets the CDFs, the |
| 7011 | // loop-restoration reference and its neighbour contexts), so the tiles | 7033 | // loop-restoration reference and its neighbour contexts), so the tiles |
| @@ -7017,7 +7039,8 @@ pub(crate) fn encode_key_frame_inner( | |||
| 7017 | -> Result<Vec<Vec<u8>>> { | 7039 | -> Result<Vec<Vec<u8>>> { |
| 7018 | let (out, _) = write_tiles(layout.count(), usize::MAX, |index| { | 7040 | let (out, _) = write_tiles(layout.count(), usize::MAX, |index| { |
| 7019 | crate::tile::arm_cdef_idx(bits, sb_cols, grid.to_vec()); | 7041 | crate::tile::arm_cdef_idx(bits, sb_cols, grid.to_vec()); |
| 7020 | - crate::tile::arm_lr(64, lr_horz, lr_vert, units.to_vec()); | 7042 | + crate::tile::arm_lr(lr_unit(), lr_horz, lr_vert, units.to_vec()); |
| 7043 | + crate::tile::arm_sb128(sb128_on()); | ||
| 7021 | let mut cdfs = start_cdfs.0.clone(); | 7044 | let mut cdfs = start_cdfs.0.clone(); |
| 7022 | crate::tile::arm_screen(screen); | 7045 | crate::tile::arm_screen(screen); |
| 7023 | crate::tile::arm_filter_intra(filter_intra_on()); | 7046 | crate::tile::arm_filter_intra(filter_intra_on()); |
| @@ -7302,9 +7325,13 @@ fn pick_and_apply_filters( | |||
| 7302 | // rather than decoding the re-coded tile. Cleared at the end instead. | 7325 | // rather than decoding the re-coded tile. Cleared at the end instead. |
| 7303 | let mut lr = LoopRestorationParams::default(); | 7326 | let mut lr = LoopRestorationParams::default(); |
| 7304 | if lr_on { | 7327 | if lr_on { |
| 7328 | + let unit = lr_unit(); | ||
| 7305 | let mut want = LoopRestorationParams { | 7329 | let mut want = LoopRestorationParams { |
| 7306 | - loop_restoration_size: [64, 64, 64], | 7330 | + loop_restoration_size: [unit, unit, unit], |
| 7307 | uses_lr: true, | 7331 | uses_lr: true, |
| 7332 | + // spec `lr_params`: `RESTORATION_TILESIZE_MAX >> (2 - shift)`, so | ||
| 7333 | + // 0 is the 64 unit and 1 the 128 one a 128 superblock forces. | ||
| 7334 | + lr_unit_shift: u8::from(unit == 128), | ||
| 7308 | ..LoopRestorationParams::default() | 7335 | ..LoopRestorationParams::default() |
| 7309 | }; | 7336 | }; |
| 7310 | want.frame_restoration_type[0] = ec_av1_syntax::RestorationType::Wiener; | 7337 | want.frame_restoration_type[0] = ec_av1_syntax::RestorationType::Wiener; |
| @@ -10446,8 +10473,8 @@ pub(crate) fn encode_inter_frame( | |||
| 10446 | let switchable_motion_mode = header.is_motion_mode_switchable; | 10473 | let switchable_motion_mode = header.is_motion_mode_switchable; |
| 10447 | let warped_motion = header.allow_warped_motion; | 10474 | let warped_motion = header.allow_warped_motion; |
| 10448 | let order_hint_bits = seq.order_hint_bits; | 10475 | let order_hint_bits = seq.order_hint_bits; |
| 10449 | - let lr_horz = crate::restoration::count_units(header.frame_width, 64) as u32; | 10476 | + let lr_horz = crate::restoration::count_units(header.frame_width, lr_unit()) as u32; |
| 10450 | - let lr_vert = crate::restoration::count_units(header.frame_height, 64) as u32; | 10477 | + let lr_vert = crate::restoration::count_units(header.frame_height, lr_unit()) as u32; |
| 10451 | // What this frame stores is the LARGEST tile's end-of-tile tables -- | 10478 | // What this frame stores is the LARGEST tile's end-of-tile tables -- |
| 10452 | // its own header's `context_update_tile_id` (spec 7.20), which | 10479 | // its own header's `context_update_tile_id` (spec 7.20), which |
| 10453 | // `tile_info_of` set to exactly that tile. | 10480 | // `tile_info_of` set to exactly that tile. |
| @@ -10462,7 +10489,8 @@ pub(crate) fn encode_inter_frame( | |||
| 10462 | -> Result<Vec<Vec<u8>>> { | 10489 | -> Result<Vec<Vec<u8>>> { |
| 10463 | let (out, stored) = write_tiles(layout.count(), store_tile, |index| { | 10490 | let (out, stored) = write_tiles(layout.count(), store_tile, |index| { |
| 10464 | crate::tile::arm_cdef_idx(bits, sb_cols, cdef_grid.to_vec()); | 10491 | crate::tile::arm_cdef_idx(bits, sb_cols, cdef_grid.to_vec()); |
| 10465 | - crate::tile::arm_lr(64, lr_horz, lr_vert, units.to_vec()); | 10492 | + crate::tile::arm_lr(lr_unit(), lr_horz, lr_vert, units.to_vec()); |
| 10493 | + crate::tile::arm_sb128(sb128_on()); | ||
| 10466 | crate::tile::arm_sign_bias(sign_bias); | 10494 | crate::tile::arm_sign_bias(sign_bias); |
| 10467 | // Thread-locals: every tile job arms its own worker. | 10495 | // Thread-locals: every tile job arms its own worker. |
| 10468 | crate::tile::arm_reference_select(reference_select_bit); | 10496 | crate::tile::arm_reference_select(reference_select_bit); |
crates/ec-av1/src/tile.rsmodified+170 -17
| @@ -85,19 +85,123 @@ 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 | +/// Whether the next tile write codes 128x128 superblocks ([`arm_sb128`]). | ||
| 107 | +fn sb128_armed() -> bool { | ||
| 108 | + SB128.with(std::cell::Cell::get) | ||
| 109 | +} | ||
| 110 | + | ||
| 111 | +/// The order a tile's 64x64 superblock cells are written in, mirroring | ||
| 112 | +/// `crate::decode::sb_visit_order` exactly: plain raster at a 64 superblock, | ||
| 113 | +/// and TL/TR/BL/BR inside each 128x128 superblock (in raster order over the | ||
| 114 | +/// 128 grid) at a 128 one. The two flags mark the first cell of a superblock | ||
| 115 | +/// ROW (where the left neighbour context resets) and the first cell of a | ||
| 116 | +/// superblock (where the 128 root's own restoration units and partition | ||
| 117 | +/// symbol are written). | ||
| 118 | +fn sb_write_order(r0: u32, r1: u32, c0: u32, c1: u32, sb128: bool) -> Vec<(u32, u32, bool, bool)> { | ||
| 119 | + let mut out = Vec::new(); | ||
| 120 | + if !sb128 { | ||
| 121 | + for r in r0..r1 { | ||
| 122 | + for c in c0..c1 { | ||
| 123 | + out.push((r, c, c == c0, true)); | ||
| 124 | + } | ||
| 125 | + } | ||
| 126 | + return out; | ||
| 127 | + } | ||
| 128 | + let mut r = r0; | ||
| 129 | + while r < r1 { | ||
| 130 | + let mut first_in_row = true; | ||
| 131 | + let mut c = c0; | ||
| 132 | + while c < c1 { | ||
| 133 | + let mut first_in_sb = true; | ||
| 134 | + for qr in 0..2 { | ||
| 135 | + for qc in 0..2 { | ||
| 136 | + let (rr, cc) = (r + qr, c + qc); | ||
| 137 | + if rr >= r1 || cc >= c1 { | ||
| 138 | + continue; | ||
| 139 | + } | ||
| 140 | + out.push((rr, cc, first_in_row, first_in_sb)); | ||
| 141 | + first_in_row = false; | ||
| 142 | + first_in_sb = false; | ||
| 143 | + } | ||
| 144 | + } | ||
| 145 | + c += 2; | ||
| 146 | + } | ||
| 147 | + r += 2; | ||
| 148 | + } | ||
| 149 | + out | ||
| 150 | +} | ||
| 151 | + | ||
| 152 | +/// The 128x128 superblock root ([`sb_write_order`]'s `sb_start` cells): the | ||
| 153 | +/// units its own 128-sample span covers, then the partition symbol. Only | ||
| 154 | +/// `PARTITION_SPLIT` is written -- the four 64x64 quadrants below are today's | ||
| 155 | +/// superblocks -- and at a root the true frame edge cuts, the DECIDING | ||
| 156 | +/// gathered bit the decoder reads instead (`read_sb128_root`). | ||
| 157 | +fn write_sb128_root( | ||
| 158 | + enc: &mut SymbolEncoder, | ||
| 159 | + cdfs: &mut Cdfs, | ||
| 160 | + neighbours: &Neighbours, | ||
| 161 | + sb_r: u32, | ||
| 162 | + sb_c: u32, | ||
| 163 | + mi_cols: u32, | ||
| 164 | + mi_rows: u32, | ||
| 165 | +) { | ||
| 166 | + let (mi_r128, mi_c128) = ((sb_r & !1) * SB_MI, (sb_c & !1) * SB_MI); | ||
| 167 | + write_lr(enc, cdfs, mi_r128, mi_c128, SB_MI * 2); | ||
| 168 | + let at128 = ((mi_r128 / 4) as usize, (mi_c128 / 4) as usize); | ||
| 169 | + let ctx128 = neighbours.partition_ctx(at128, 128); | ||
| 170 | + let (has_cols128, has_rows128) = (mi_c128 + SB_MI < mi_cols, mi_r128 + SB_MI < mi_rows); | ||
| 171 | + if crate::envflags::env_flag!("EC_AV1_TRACE") { | ||
| 172 | + eprintln!("WTRACE partition_w128 ctx={ctx128} cols={has_cols128} rows={has_rows128}"); | ||
| 173 | + } | ||
| 174 | + match (has_cols128, has_rows128) { | ||
| 175 | + (true, true) => enc.symbol(PARTITION_SPLIT, &mut cdfs.partition_w128[ctx128]), | ||
| 176 | + // As at the 64 root: the gathered CDF is built for the read and | ||
| 177 | + // thrown away, so nothing adapts here. Value 1 is SPLIT. | ||
| 178 | + (true, false) => enc.symbol_fixed( | ||
| 179 | + 1, | ||
| 180 | + &crate::decode::gather_of(&cdfs.partition_w128[ctx128], &crate::decode::VERT_ALIKE128), | ||
| 181 | + ), | ||
| 182 | + (false, true) => enc.symbol_fixed( | ||
| 183 | + 1, | ||
| 184 | + &crate::decode::gather_of(&cdfs.partition_w128[ctx128], &crate::decode::HORZ_ALIKE128), | ||
| 185 | + ), | ||
| 186 | + // Both halves outside: the split is the only partition left and the | ||
| 187 | + // decoder reads nothing. | ||
| 188 | + (false, false) => {} | ||
| 189 | + } | ||
| 190 | +} | ||
| 191 | + | ||
| 88 | /// `read_lr`'s write side, called once at the top of every superblock, | 192 | /// `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 | 193 | /// 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 | 194 | /// mi span covers (`av1_loop_restoration_corners_in_sb`; no superres, so the |
| 91 | /// column scaling is the plain mi one). | 195 | /// column scaling is the plain mi one). |
| 92 | -fn write_lr(enc: &mut SymbolEncoder, cdfs: &mut Cdfs, mi_row: u32, mi_col: u32) { | 196 | +fn write_lr(enc: &mut SymbolEncoder, cdfs: &mut Cdfs, mi_row: u32, mi_col: u32, span: u32) { |
| 93 | LR_PLAN.with(|c| { | 197 | LR_PLAN.with(|c| { |
| 94 | let mut plan = c.borrow_mut(); | 198 | let mut plan = c.borrow_mut(); |
| 95 | let Some(plan) = plan.as_mut() else { return }; | 199 | let Some(plan) = plan.as_mut() else { return }; |
| 96 | let unit = plan.unit_size; | 200 | let unit = plan.unit_size; |
| 97 | let rcol0 = (mi_col * 4).div_ceil(unit); | 201 | let rcol0 = (mi_col * 4).div_ceil(unit); |
| 98 | - let rcol1 = ((mi_col + SB_MI_W) * 4).div_ceil(unit).min(plan.horz_units); | 202 | + let rcol1 = ((mi_col + span) * 4).div_ceil(unit).min(plan.horz_units); |
| 99 | let rrow0 = (mi_row * 4).div_ceil(unit); | 203 | let rrow0 = (mi_row * 4).div_ceil(unit); |
| 100 | - let rrow1 = ((mi_row + SB_MI_W) * 4).div_ceil(unit).min(plan.vert_units); | 204 | + let rrow1 = ((mi_row + span) * 4).div_ceil(unit).min(plan.vert_units); |
| 101 | for rrow in rrow0..rrow1 { | 205 | for rrow in rrow0..rrow1 { |
| 102 | for rcol in rcol0..rcol1 { | 206 | for rcol in rcol0..rcol1 { |
| 103 | let info = plan.units[(rcol + rrow * plan.horz_units) as usize]; | 207 | let info = plan.units[(rcol + rrow * plan.horz_units) as usize]; |
| @@ -177,9 +281,14 @@ impl TileLayout { | |||
| 177 | pub(crate) fn new(mi_cols: u32, mi_rows: u32, cols_log2: u32, rows_log2: u32) -> Self { | 281 | 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); | 282 | let (cols32, rows32) = block_grid(mi_cols, mi_rows); |
| 179 | let (sb_cols, sb_rows) = (cols32.div_ceil(2), rows32.div_ceil(2)); | 283 | let (sb_cols, sb_rows) = (cols32.div_ceil(2), rows32.div_ceil(2)); |
| 284 | + // lane-sb128: a tile boundary is a SUPERBLOCK boundary, so under a | ||
| 285 | + // 128 superblock the starts are laid out on the 128 grid and then | ||
| 286 | + // doubled back into the 64-cell units the writers walk. | ||
| 287 | + let unit = u32::from(crate::encode::sb128_on()) + 1; | ||
| 180 | let starts = |total: u32, log2: u32| { | 288 | let starts = |total: u32, log2: u32| { |
| 181 | - let step = total.div_ceil(1 << log2).max(1); | 289 | + let total128 = total.div_ceil(unit); |
| 182 | - let mut v: Vec<u32> = (0..total).step_by(step as usize).collect(); | 290 | + let step = total128.div_ceil(1 << log2).max(1); |
| 291 | + let mut v: Vec<u32> = (0..total128).step_by(step as usize).map(|s| s * unit).collect(); | ||
| 183 | v.push(total); | 292 | v.push(total); |
| 184 | v | 293 | v |
| 185 | }; | 294 | }; |
| @@ -1696,6 +1805,14 @@ const TX4: usize = 4; | |||
| 1696 | /// Side of a superblock, in samples, which is the size a block outside the | 1805 | /// Side of a superblock, in samples, which is the size a block outside the |
| 1697 | /// tile reads as. | 1806 | /// tile reads as. |
| 1698 | const SB: usize = 64; | 1807 | const SB: usize = 64; |
| 1808 | + | ||
| 1809 | +/// The `above_side`/`left_side` value a cell with no coded neighbour holds | ||
| 1810 | +/// (tile start, superblock-row start) -- the writer's copy of | ||
| 1811 | +/// `crate::decode::NO_NEIGHBOUR_SIDE`, and 128 for the same reason: at 64 it | ||
| 1812 | +/// reads identically at every level up to 64x64 but sets the bit at the | ||
| 1813 | +/// `BLOCK_128X128` root (64 * 2 <= 128), putting that root's partition symbol | ||
| 1814 | +/// on CDF row 3 where libaom uses row 0. | ||
| 1815 | +const NO_NEIGHBOUR_SIDE: usize = 128; | ||
| 1699 | /// Side of the smallest block the writer codes, in samples, which is the grid | 1816 | /// Side of the smallest block the writer codes, in samples, which is the grid |
| 1700 | /// the neighbour bookkeeping is kept on. | 1817 | /// the neighbour bookkeeping is kept on. |
| 1701 | const SUB: usize = 16; | 1818 | const SUB: usize = 16; |
| @@ -1947,10 +2064,10 @@ impl Neighbours { | |||
| 1947 | left: vec![[Neighbour::default(); 3]; rows * (SUB / MI)], | 2064 | left: vec![[Neighbour::default(); 3]; rows * (SUB / MI)], |
| 1948 | above_mode: vec![DC_PRED; cols], | 2065 | above_mode: vec![DC_PRED; cols], |
| 1949 | left_mode: vec![DC_PRED; rows], | 2066 | left_mode: vec![DC_PRED; rows], |
| 1950 | - above_side: vec![SB; cols], | 2067 | + above_side: vec![NO_NEIGHBOUR_SIDE; cols], |
| 1951 | - left_side: vec![SB; rows], | 2068 | + left_side: vec![NO_NEIGHBOUR_SIDE; rows], |
| 1952 | - above_side_mi: vec![SB; cols * (SUB / MI)], | 2069 | + above_side_mi: vec![NO_NEIGHBOUR_SIDE; cols * (SUB / MI)], |
| 1953 | - left_side_mi: vec![SB; rows * (SUB / MI)], | 2070 | + left_side_mi: vec![NO_NEIGHBOUR_SIDE; rows * (SUB / MI)], |
| 1954 | above_skip: vec![false; cols * (SUB / MI)], | 2071 | above_skip: vec![false; cols * (SUB / MI)], |
| 1955 | left_skip: vec![false; rows * (SUB / MI)], | 2072 | left_skip: vec![false; rows * (SUB / MI)], |
| 1956 | above_inter: vec![false; cols * (SUB / MI)], | 2073 | above_inter: vec![false; cols * (SUB / MI)], |
| @@ -2003,8 +2120,8 @@ impl Neighbours { | |||
| 2003 | fn start_row(&mut self) { | 2120 | fn start_row(&mut self) { |
| 2004 | self.left.iter_mut().for_each(|l| *l = Default::default()); | 2121 | self.left.iter_mut().for_each(|l| *l = Default::default()); |
| 2005 | self.left_mode.iter_mut().for_each(|m| *m = DC_PRED); | 2122 | self.left_mode.iter_mut().for_each(|m| *m = DC_PRED); |
| 2006 | - self.left_side.iter_mut().for_each(|s| *s = SB); | 2123 | + self.left_side.iter_mut().for_each(|s| *s = NO_NEIGHBOUR_SIDE); |
| 2007 | - self.left_side_mi.iter_mut().for_each(|s| *s = SB); | 2124 | + self.left_side_mi.iter_mut().for_each(|s| *s = NO_NEIGHBOUR_SIDE); |
| 2008 | self.left_skip.iter_mut().for_each(|s| *s = false); | 2125 | self.left_skip.iter_mut().for_each(|s| *s = false); |
| 2009 | self.left_inter.iter_mut().for_each(|i| *i = false); | 2126 | self.left_inter.iter_mut().for_each(|i| *i = false); |
| 2010 | self.left_ref.iter_mut().for_each(|r| *r = -1); | 2127 | self.left_ref.iter_mut().for_each(|r| *r = -1); |
| @@ -2648,10 +2765,28 @@ pub(crate) fn sb_coeff_key_frame_tile_cdfs( | |||
| 2648 | // off. | 2765 | // off. |
| 2649 | let mut cdfs = cdfs; | 2766 | let mut cdfs = cdfs; |
| 2650 | let mut enc = SymbolEncoder::new(); | 2767 | let mut enc = SymbolEncoder::new(); |
| 2651 | - for sb_r in tile.sb_row0..tile.sb_row1.min(sb_rows) { | 2768 | + let sb128 = sb128_armed(); |
| 2769 | + // lane-sb128: at a 128 superblock the four 64x64 cells of each root are | ||
| 2770 | + // written in libaom's own TL/TR/BL/BR order, the root's restoration units | ||
| 2771 | + // and partition symbol first; at a 64 one this is plain raster and every | ||
| 2772 | + // cell is its own superblock, i.e. byte-identical to the loop before. | ||
| 2773 | + for (sb_r, sb_c, row_start, sb_start) in sb_write_order( | ||
| 2774 | + tile.sb_row0, | ||
| 2775 | + tile.sb_row1.min(sb_rows), | ||
| 2776 | + tile.sb_col0, | ||
| 2777 | + tile.sb_col1.min(sb_cols), | ||
| 2778 | + sb128, | ||
| 2779 | + ) { | ||
| 2780 | + if row_start { | ||
| 2652 | neighbours.start_row(); | 2781 | neighbours.start_row(); |
| 2653 | - for sb_c in tile.sb_col0..tile.sb_col1.min(sb_cols) { | 2782 | + } |
| 2654 | - write_lr(&mut enc, &mut cdfs, sb_r * SB_MI_W, sb_c * SB_MI_W); | 2783 | + if sb128 && sb_start { |
| 2784 | + write_sb128_root(&mut enc, &mut cdfs, &neighbours, sb_r, sb_c, mi_cols, mi_rows); | ||
| 2785 | + } | ||
| 2786 | + { | ||
| 2787 | + if !sb128 { | ||
| 2788 | + write_lr(&mut enc, &mut cdfs, sb_r * SB_MI_W, sb_c * SB_MI_W, SB_MI_W); | ||
| 2789 | + } | ||
| 2655 | let at = (sb_r as usize * 4, sb_c as usize * 4); | 2790 | let at = (sb_r as usize * 4, sb_c as usize * 4); |
| 2656 | let ctx = neighbours.partition_ctx(at, SB); | 2791 | let ctx = neighbours.partition_ctx(at, SB); |
| 2657 | ec_rng_trace(|| { | 2792 | ec_rng_trace(|| { |
| @@ -5809,10 +5944,28 @@ pub(crate) fn sb_coeff_inter_frame_tile_cdfs( | |||
| 5809 | vec![0i32; TX16 * TX16], | 5944 | vec![0i32; TX16 * TX16], |
| 5810 | ]; | 5945 | ]; |
| 5811 | 5946 | ||
| 5812 | - for sb_r in tile.sb_row0..tile.sb_row1.min(sb_rows) { | 5947 | + let sb128 = sb128_armed(); |
| 5948 | + // lane-sb128: at a 128 superblock the four 64x64 cells of each root are | ||
| 5949 | + // written in libaom's own TL/TR/BL/BR order, the root's restoration units | ||
| 5950 | + // and partition symbol first; at a 64 one this is plain raster and every | ||
| 5951 | + // cell is its own superblock, i.e. byte-identical to the loop before. | ||
| 5952 | + for (sb_r, sb_c, row_start, sb_start) in sb_write_order( | ||
| 5953 | + tile.sb_row0, | ||
| 5954 | + tile.sb_row1.min(sb_rows), | ||
| 5955 | + tile.sb_col0, | ||
| 5956 | + tile.sb_col1.min(sb_cols), | ||
| 5957 | + sb128, | ||
| 5958 | + ) { | ||
| 5959 | + if row_start { | ||
| 5813 | neighbours.start_row(); | 5960 | neighbours.start_row(); |
| 5814 | - for sb_c in tile.sb_col0..tile.sb_col1.min(sb_cols) { | 5961 | + } |
| 5815 | - write_lr(&mut enc, &mut cdfs, sb_r * SB_MI_W, sb_c * SB_MI_W); | 5962 | + if sb128 && sb_start { |
| 5963 | + write_sb128_root(&mut enc, &mut cdfs, &neighbours, sb_r, sb_c, mi_cols, mi_rows); | ||
| 5964 | + } | ||
| 5965 | + { | ||
| 5966 | + if !sb128 { | ||
| 5967 | + write_lr(&mut enc, &mut cdfs, sb_r * SB_MI_W, sb_c * SB_MI_W, SB_MI_W); | ||
| 5968 | + } | ||
| 5816 | let sb_at = (sb_r as usize * 4, sb_c as usize * 4); | 5969 | let sb_at = (sb_r as usize * 4, sb_c as usize * 4); |
| 5817 | let sb_ctx = neighbours.partition_ctx(sb_at, SB); | 5970 | let sb_ctx = neighbours.partition_ctx(sb_at, SB); |
| 5818 | // spec `decode_partition`'s hasRows/hasCols (5.11.4): a superblock | 5971 | // spec `decode_partition`'s hasRows/hasCols (5.11.4): a superblock |
lanes/sb128.report.mdadded+42 -0
| @@ -0,0 +1,42 @@ | |||
| 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 | + | ||