edith_codecs
Clone
https://git.tahinli.com/Tahinli/edith_codecs.git
0 watching0 forksRust370.3 MiBcfa93c0Tahinli3 months ago1659 commits
| 1 | //! Software decoder for this crate's own key-frame tile payloads (spec 5.11), |
| 2 | //! the reader half of [`crate::tile`]'s writer. |
| 3 | //! |
| 4 | //! `tile.rs` is another lane's territory this round (`lane-av1-rect` is |
| 5 | //! touching its partition search), so nothing here imports its private |
| 6 | //! helpers: the handful of `pub(crate)` items it already exposes |
| 7 | //! ([`crate::tile::block_grid`], [`crate::tile::has_half`], |
| 8 | //! [`crate::tile::INTRA_MODE_CTX`], [`crate::tile::write_golomb`]) are reused, |
| 9 | //! and everything else (the partition/coefficient context arithmetic, the |
| 10 | //! scan table) is a from-spec reimplementation kept in step with the writer by |
| 11 | //! the gate tests below rather than by sharing code with it. |
| 12 | //! |
| 13 | //! # Scope (round 1) |
| 14 | //! Key frames only, and only the shapes the encoder actually still needs to |
| 15 | //! prove itself against: every coded block is `DC_PRED` (this crate's mode |
| 16 | //! search names other intra modes, but a decoder for those needs the |
| 17 | //! directional-prediction edge-reach plumbing [`crate::intra::predict`] |
| 18 | //! already carries; wiring it up is round 2), never skipped, and every |
| 19 | //! superblock is a whole number of 64x64 units (a frame whose true edge falls |
| 20 | //! inside one, forcing the gathered-CDF partial-superblock path, is round 2 |
| 21 | //! too). Anything outside that refuses with [`Error::unsupported`] rather than |
| 22 | //! silently miscoding. |
| 23 | |
| 24 | use ec_av1_syntax::{ |
| 25 | CdefParams, DeltaParams, LoopFilterParams, LoopRestorationParams, |
| 26 | SEG_LVL_ALT_LF_Y_V, SEG_LVL_ALT_Q, SegmentationParams, TileInfo, |
| 27 | }; |
| 28 | use ec_core::{Error, Result}; |
| 29 | |
| 30 | use crate::cdf; |
| 31 | use crate::cdf_state::{Cdfs, MvComponentCdfs, TxbSet, TxbTables}; |
| 32 | use crate::encode::{Picture, Reach}; |
| 33 | use crate::intra::predict; |
| 34 | use crate::mc; |
| 35 | use crate::msac::SymbolDecoder; |
| 36 | use crate::mvstack::{ |
| 37 | ALTREF_FRAME, ALTREF2_FRAME, BWDREF_FRAME, GOLDEN_FRAME, LAST_FRAME, LAST2_FRAME, LAST3_FRAME, |
| 38 | MiGrid, MiInfo, NO_SIGN_BIAS, NeighbourRef, SignBiasTable, comp_reference_type_ctx, |
| 39 | find_mv_stack_with_sign_bias, reference_mode_ctx, |
| 40 | single_ref_p1_ctx, single_ref_p2_ctx, single_ref_p3_ctx, single_ref_p4_ctx, single_ref_p5_ctx, |
| 41 | single_ref_p6_ctx, uni_comp_ref_p1_ctx, |
| 42 | }; |
| 43 | use crate::tile::{INTRA_MODE_CTX, block_grid, has_half}; |
| 44 | use crate::transform::{TxType, dequant_and_inverse_typed, dequant_and_inverse_typed_wh}; |
| 45 | |
| 46 | const PARTITION_NONE: usize = 0; |
| 47 | const PARTITION_HORZ: usize = 1; |
| 48 | const PARTITION_VERT: usize = 2; |
| 49 | const PARTITION_SPLIT: usize = 3; |
| 50 | const PARTITION_HORZ_A: usize = 4; |
| 51 | const PARTITION_HORZ_B: usize = 5; |
| 52 | const PARTITION_VERT_A: usize = 6; |
| 53 | const PARTITION_VERT_B: usize = 7; |
| 54 | const PARTITION_HORZ_4: usize = 8; |
| 55 | const PARTITION_VERT_4: usize = 9; |
| 56 | const SB_MI: u32 = 16; |
| 57 | const BLOCK_MI: u32 = 8; |
| 58 | |
| 59 | // lane-sb128 r1: this sequence's `use_128x128_superblock` (spec 5.5.1). |
| 60 | // Every superblock-granular rule below (`delta_q`/`delta_lf`'s "is this the |
| 61 | // superblock's own top-left mi?", the loop-restoration read's unit span, the |
| 62 | // partition tree's own root level) keys off it; `SB_MI` stays the 64x64 |
| 63 | // *quadrant* size a 128 superblock is decoded in, which is also what the CDEF |
| 64 | // grid is indexed by (libaom `read_cdef` reads one `cdef_idx` per 64x64 |
| 65 | // inside a 128 superblock). |
| 66 | thread_local! { |
| 67 | static SB128: std::cell::Cell<bool> = const { std::cell::Cell::new(false) }; |
| 68 | } |
| 69 | |
| 70 | /// Sets [`SB128`] for this thread (called once per frame from `stream.rs`). |
| 71 | pub(crate) fn set_sb128(v: bool) { |
| 72 | SB128.with(|c| c.set(v)); |
| 73 | crate::encode::set_reach_superblock_px(if v { 128 } else { 64 }); |
| 74 | } |
| 75 | |
| 76 | fn sb128() -> bool { |
| 77 | SB128.with(|c| c.get()) |
| 78 | } |
| 79 | |
| 80 | /// This sequence's superblock size in mi units (16 = 64x64, 32 = 128x128). |
| 81 | pub(crate) fn sb_mi_cur() -> u32 { |
| 82 | if sb128() { SB_MI * 2 } else { SB_MI } |
| 83 | } |
| 84 | |
| 85 | /// How many real 128-level partition symbols this decoder has read, and how |
| 86 | /// many superblocks resolved to `PARTITION_SPLIT` (coded or forced) -- the |
| 87 | /// gate's proof a `--sb-size=128` stream exercised the 128 partition root. |
| 88 | thread_local! { |
| 89 | static PART128_SYMBOLS: std::cell::Cell<usize> = const { std::cell::Cell::new(0) }; |
| 90 | static PART128_SPLIT_HITS: std::cell::Cell<usize> = const { std::cell::Cell::new(0) }; |
| 91 | static PART128_GATHERED_SYMBOLS: std::cell::Cell<usize> = const { std::cell::Cell::new(0) }; |
| 92 | } |
| 93 | |
| 94 | /// Current value of `PART128_GATHERED_SYMBOLS`: 128 roots that read the |
| 95 | /// half-out-of-frame gathered split_or_horz/split_or_vert symbol (spec |
| 96 | /// 5.11.4), which this decoder used to read and throw away. |
| 97 | pub(crate) fn part128_gathered_symbols() -> usize { |
| 98 | PART128_GATHERED_SYMBOLS.with(|c| c.get()) |
| 99 | } |
| 100 | |
| 101 | /// Current value of [`PART128_SYMBOLS`]. |
| 102 | pub(crate) fn part128_symbols() -> usize { |
| 103 | PART128_SYMBOLS.with(|c| c.get()) |
| 104 | } |
| 105 | |
| 106 | /// Current value of [`PART128_SPLIT_HITS`]. |
| 107 | pub(crate) fn part128_split_hits() -> usize { |
| 108 | PART128_SPLIT_HITS.with(|c| c.get()) |
| 109 | } |
| 110 | |
| 111 | /// [`gather`] over an alphabet of any length -- the 128-level partition CDF |
| 112 | /// has 8 symbols (no `PARTITION_HORZ_4`/`_VERT_4` at `BLOCK_128X128`, spec |
| 113 | /// `partition_cdf_length`), so its forced-split gather sums 5 elements where |
| 114 | /// the 64/32 levels sum 6. |
| 115 | fn gather_of(cdf: &[u16], elements: &[usize]) -> [u16; 3] { |
| 116 | let split: u16 = elements.iter().map(|&e| element_prob(cdf, e)).sum(); |
| 117 | [32768 - split, 32768, 0] |
| 118 | } |
| 119 | |
| 120 | /// [`VERT_ALIKE`]/[`HORZ_ALIKE`] without the 1:4 arms, for the 128 root. |
| 121 | const VERT_ALIKE128: [usize; 5] = [2, PARTITION_SPLIT, 4, 6, 7]; |
| 122 | const HORZ_ALIKE128: [usize; 5] = [1, PARTITION_SPLIT, 4, 5, 6]; |
| 123 | |
| 124 | // How many `use_filter_intra` symbols this decoder has read as `1`, across |
| 125 | // every call on the current thread -- the cheap counter [`filter_intra_hits`] gate |
| 126 | // tests read (before/after, not the absolute value) to prove a stream |
| 127 | // actually exercised the filter-intra predictor rather than silently |
| 128 | // skipping it. |
| 129 | thread_local! { |
| 130 | static FILTER_INTRA_HITS: std::cell::Cell<usize> = |
| 131 | const { std::cell::Cell::new(0) }; |
| 132 | } |
| 133 | |
| 134 | /// Current value of [`FILTER_INTRA_HITS`]. |
| 135 | pub(crate) fn filter_intra_hits() -> usize { |
| 136 | FILTER_INTRA_HITS.with(|c| c.get()) |
| 137 | } |
| 138 | |
| 139 | // lane-fiinter r1: how many of those `use_filter_intra == 1` symbols belonged |
| 140 | // to an INTRA block inside an INTER frame (`read_intra_block_mode_info`'s own |
| 141 | // `read_filter_intra_mode_info` call), which no gate exercised before this |
| 142 | // round -- every inter gate recipe passed `--enable-filter-intra=0`. |
| 143 | thread_local! { |
| 144 | static INTRA_IN_INTER_FILTER_INTRA_HITS: std::cell::Cell<usize> = |
| 145 | const { std::cell::Cell::new(0) }; |
| 146 | } |
| 147 | |
| 148 | /// Current value of [`INTRA_IN_INTER_FILTER_INTRA_HITS`]. |
| 149 | pub(crate) fn intra_in_inter_filter_intra_hits() -> usize { |
| 150 | INTRA_IN_INTER_FILTER_INTRA_HITS.with(|c| c.get()) |
| 151 | } |
| 152 | |
| 153 | // lane-angleinter r1: how many INTRA blocks inside an INTER frame carried a |
| 154 | // nonzero `angle_delta_y` / `angle_delta_uv` (`read_intra_angle_info`). Both |
| 155 | // arms refused a nonzero luma delta before this round, and no inter gate |
| 156 | // enabled `--enable-angle-delta` (aomenc's own default is 1). |
| 157 | thread_local! { |
| 158 | static INTRA_IN_INTER_ANGLE_DELTA_Y_HITS: std::cell::Cell<usize> = |
| 159 | const { std::cell::Cell::new(0) }; |
| 160 | static INTRA_IN_INTER_ANGLE_DELTA_UV_HITS: std::cell::Cell<usize> = |
| 161 | const { std::cell::Cell::new(0) }; |
| 162 | } |
| 163 | |
| 164 | /// Current value of [`INTRA_IN_INTER_ANGLE_DELTA_Y_HITS`]. |
| 165 | pub(crate) fn intra_in_inter_angle_delta_y_hits() -> usize { |
| 166 | INTRA_IN_INTER_ANGLE_DELTA_Y_HITS.with(|c| c.get()) |
| 167 | } |
| 168 | |
| 169 | /// Current value of [`INTRA_IN_INTER_ANGLE_DELTA_UV_HITS`]. |
| 170 | pub(crate) fn intra_in_inter_angle_delta_uv_hits() -> usize { |
| 171 | INTRA_IN_INTER_ANGLE_DELTA_UV_HITS.with(|c| c.get()) |
| 172 | } |
| 173 | |
| 174 | // lane-realworld r1: this frame's `cdef.bits` (spec 5.9.19), threaded into |
| 175 | // [`maybe_read_cdef_idx`] the same way [`ENABLE_EDGE_FILTER`] threads a |
| 176 | // frame-level flag through the recursive block decode -- `0` (the default) |
| 177 | // makes every call a true no-op, matching every existing gate stream's |
| 178 | // `cdef_bits == 0` behaviour exactly. |
| 179 | thread_local! { |
| 180 | static CDEF_BITS: std::cell::Cell<u8> = const { std::cell::Cell::new(0) }; |
| 181 | } |
| 182 | // lane-realworld r8: the sequence header's `color_config.bit_depth`, set |
| 183 | // once per `decode_key_frame_tile_with_cdfs`/`decode_inter_frame_tile_with_cdfs` |
| 184 | // call the same way `CDEF_BITS` is above. `stream.rs`'s own refusal still |
| 185 | // blocks every non-8-bit stream from reaching a decode call, so this is `8` |
| 186 | // on every path that runs today -- threading it now (rather than the literal |
| 187 | // `8`) is stage 1 of widening past that refusal without re-deriving every |
| 188 | // clamp bound later. `PlaneBuf`'s sample storage stays `u16` regardless of |
| 189 | // depth (this decoder's own internal choice, matches dav1d/libaom). |
| 190 | thread_local! { |
| 191 | static BIT_DEPTH: std::cell::Cell<u8> = const { std::cell::Cell::new(8) }; |
| 192 | } |
| 193 | /// `(1 << bit_depth) - 1` — the reconstruction clamp bound for the current |
| 194 | /// stream's [`BIT_DEPTH`], replacing the crate-wide literal `255`. |
| 195 | pub(crate) fn sample_max() -> i32 { |
| 196 | (1i32 << BIT_DEPTH.with(std::cell::Cell::get)) - 1 |
| 197 | } |
| 198 | /// The current stream's bit depth, for callers (e.g. dequant) that need the |
| 199 | /// raw value rather than the derived clamp bound. |
| 200 | pub(crate) fn bit_depth() -> u8 { |
| 201 | BIT_DEPTH.with(std::cell::Cell::get) |
| 202 | } |
| 203 | /// Sets [`BIT_DEPTH`] for the current thread, called once per frame from |
| 204 | /// [`crate::stream`]'s decode loop. |
| 205 | pub(crate) fn set_bit_depth(bit_depth: u8) { |
| 206 | BIT_DEPTH.with(|c| c.set(bit_depth)); |
| 207 | } |
| 208 | // Whether this superblock's single CDEF unit (this decoder has no 128x128 |
| 209 | // SB, so the SB IS the CDEF unit) has already had its `cdef_idx` literal |
| 210 | // read -- reset to `false` at the top of each superblock in the two tile |
| 211 | // loops, spec `read_cdef`'s `cdef_transmitted[index]`. |
| 212 | thread_local! { |
| 213 | static CDEF_TRANSMITTED: std::cell::Cell<bool> = const { std::cell::Cell::new(false) }; |
| 214 | } |
| 215 | // Number of superblock columns in the current tile's grid, so |
| 216 | // [`maybe_read_cdef_idx`] can turn an `(mi_r, mi_c)` into a flat index into |
| 217 | // [`CDEF_IDX_GRID`]. |
| 218 | thread_local! { |
| 219 | static CDEF_SB_COLS: std::cell::Cell<usize> = const { std::cell::Cell::new(0) }; |
| 220 | } |
| 221 | // One resolved `cdef_idx` per superblock, read by [`apply_cdef`] to select |
| 222 | // which of the header's `1 << cdef.bits` strength pairs applies to that |
| 223 | // superblock's samples. |
| 224 | thread_local! { |
| 225 | static CDEF_IDX_GRID: std::cell::RefCell<Vec<u8>> = std::cell::RefCell::new(Vec::new()); |
| 226 | } |
| 227 | // How many real (`cdef.bits > 0`) `cdef_idx` literals [`maybe_read_cdef_idx`] |
| 228 | // has read, across every call on the current thread -- the gate's proof that a |
| 229 | // `cdef_bits > 0` stream actually reached the new reader rather than every |
| 230 | // superblock coincidentally being all-skip. |
| 231 | thread_local! { |
| 232 | static CDEF_IDX_HITS: std::cell::Cell<usize> = const { std::cell::Cell::new(0) }; |
| 233 | } |
| 234 | |
| 235 | /// Current value of [`CDEF_IDX_HITS`]. |
| 236 | pub(crate) fn cdef_idx_hits() -> usize { |
| 237 | CDEF_IDX_HITS.with(|c| c.get()) |
| 238 | } |
| 239 | |
| 240 | // lane-realworld r4: this frame's `delta.q_present` (spec 5.9.17) and its |
| 241 | // actual step (`1 << delta.q_res`, already the real multiplier, not the raw |
| 242 | // 2-bit field). `false`/`1` (the defaults) make [`maybe_read_delta_q`] a |
| 243 | // true no-op, matching every existing gate stream's `delta_q_present == 0` |
| 244 | // behaviour exactly, the same corner-cut [`CDEF_BITS`] takes above. |
| 245 | thread_local! { |
| 246 | static DELTA_Q_PRESENT: std::cell::Cell<bool> = const { std::cell::Cell::new(false) }; |
| 247 | static DELTA_Q_RES: std::cell::Cell<i32> = const { std::cell::Cell::new(1) }; |
| 248 | } |
| 249 | // The running quantizer index (spec `CurrentQIndex`, libaom |
| 250 | // `xd->current_base_qindex`) -- reset to the frame's own `base_q_idx` at the |
| 251 | // top of every tile (spec `decode_tile`), carried across superblocks within |
| 252 | // that tile, and mutated only by [`maybe_read_delta_q`]. Read back at the |
| 253 | // dequantization call sites instead of the frame-constant `base_q_idx`. |
| 254 | thread_local! { |
| 255 | static CURRENT_Q_IDX: std::cell::Cell<i32> = const { std::cell::Cell::new(0) }; |
| 256 | } |
| 257 | // This frame's own per-plane DC/AC quantizer-index deltas (lane-sbpart r11, |
| 258 | // spec 5.9.12/7.12.2, [`crate::quant::QuantDeltas`]) -- frame-constant (not |
| 259 | // per-superblock like [`CURRENT_Q_IDX`]), set once per frame decode and read |
| 260 | // back at every dequantization call site keyed on which plane it is doing. |
| 261 | thread_local! { |
| 262 | static QUANT_DELTAS: std::cell::Cell<crate::quant::QuantDeltas> = const { |
| 263 | std::cell::Cell::new(crate::quant::QuantDeltas { |
| 264 | y_dc: 0, |
| 265 | u_dc: 0, |
| 266 | u_ac: 0, |
| 267 | v_dc: 0, |
| 268 | v_ac: 0, |
| 269 | }) |
| 270 | }; |
| 271 | } |
| 272 | // How many real `delta_q_present` symbol groups [`maybe_read_delta_q`] has |
| 273 | // read, across every call on the current thread -- the gate's proof that a |
| 274 | // `delta_q_present` stream actually reached the new reader. |
| 275 | thread_local! { |
| 276 | static DELTA_Q_HITS: std::cell::Cell<usize> = const { std::cell::Cell::new(0) }; |
| 277 | } |
| 278 | |
| 279 | /// Current value of [`DELTA_Q_HITS`]. |
| 280 | pub(crate) fn delta_q_hits() -> usize { |
| 281 | DELTA_Q_HITS.with(|c| c.get()) |
| 282 | } |
| 283 | |
| 284 | /// This frame's [`QUANT_DELTAS`] `(dc, ac)` pair for plane `plane_idx` (0 = |
| 285 | /// Y, 1 = U, 2 = V) -- luma has no coded `ac` delta (spec 5.9.12 never reads |
| 286 | /// one), so it is always `0` here. |
| 287 | fn plane_q_delta(plane_idx: usize) -> (i32, i32) { |
| 288 | let d = QUANT_DELTAS.with(|c| c.get()); |
| 289 | match plane_idx { |
| 290 | 0 => (d.y_dc, 0), |
| 291 | 1 => (d.u_dc, d.u_ac), |
| 292 | _ => (d.v_dc, d.v_ac), |
| 293 | } |
| 294 | } |
| 295 | |
| 296 | /// `read_delta_qindex`/`read_delta_q_params` (spec 5.11.10, libaom |
| 297 | /// `decodemv.c:85-146,735-770`), called right after [`maybe_read_cdef_idx`] |
| 298 | /// at every block-decode call site (spec order `skip -> cdef -> delta_q`). |
| 299 | /// A no-op when `delta_q_present` is unset, or when this block is not at the |
| 300 | /// superblock's own top-left MI position (`b_row == 0 && b_col == 0` in |
| 301 | /// libaom, collapsed here to a position check since a partition tree's |
| 302 | /// top-left leaf is the only block that can ever land there), or when that |
| 303 | /// leaf both *is* the whole superblock and is `skip` (the one case libaom |
| 304 | /// itself skips the read, state carrying over unchanged). |
| 305 | fn maybe_read_delta_q(dec: &mut SymbolDecoder, cdfs: &mut Cdfs, mi_r: usize, mi_c: usize, is_whole_sb: bool, skip: bool) { |
| 306 | if !DELTA_Q_PRESENT.with(|c| c.get()) { |
| 307 | return; |
| 308 | } |
| 309 | let sb_mi = sb_mi_cur() as usize; |
| 310 | if mi_r % sb_mi != 0 || mi_c % sb_mi != 0 { |
| 311 | return; |
| 312 | } |
| 313 | // `is_whole_sb` is the caller's "this leaf IS the superblock" flag, |
| 314 | // computed against the sequence's own superblock size (lane-sb128b r1: |
| 315 | // it used to be hardcoded against 64 and disabled under `sb128`, which a |
| 316 | // `PARTITION_NONE` 128x128 root -- itself the whole superblock -- makes |
| 317 | // wrong). |
| 318 | if is_whole_sb && skip { |
| 319 | return; |
| 320 | } |
| 321 | let mut abs = dec.symbol(&mut cdfs.delta_q) as i32; |
| 322 | const DELTA_Q_SMALL: i32 = 3; |
| 323 | if abs >= DELTA_Q_SMALL { |
| 324 | let rem_bits = dec.literal(3) + 1; |
| 325 | let thr = (1i32 << rem_bits) + 1; |
| 326 | abs = dec.literal(rem_bits) as i32 + thr; |
| 327 | } |
| 328 | let sign_negative = if abs != 0 { dec.literal(1) != 0 } else { true }; |
| 329 | let reduced = if sign_negative { -abs } else { abs }; |
| 330 | DELTA_Q_HITS.with(|c| c.set(c.get() + 1)); |
| 331 | let res = DELTA_Q_RES.with(|c| c.get()); |
| 332 | CURRENT_Q_IDX.with(|c| c.set((c.get() + reduced * res).clamp(1, 255))); |
| 333 | } |
| 334 | |
| 335 | // lane-realworld r5: this frame's `delta.lf_present`/`lf_res`/`lf_multi` |
| 336 | // (spec 5.9.18) -- same no-op-by-default corner-cut as `DELTA_Q_PRESENT`. |
| 337 | thread_local! { |
| 338 | static DELTA_LF_PRESENT: std::cell::Cell<bool> = const { std::cell::Cell::new(false) }; |
| 339 | static DELTA_LF_RES: std::cell::Cell<i32> = const { std::cell::Cell::new(1) }; |
| 340 | static DELTA_LF_MULTI: std::cell::Cell<bool> = const { std::cell::Cell::new(false) }; |
| 341 | } |
| 342 | // The running per-plane/direction loop-filter delta (spec `DeltaLF[i]`, |
| 343 | // `i` in `0..FRAME_LF_COUNT`) -- reset to `[0; 4]` at the top of every tile, |
| 344 | // mutated only by [`maybe_read_delta_lf`]. `!delta_lf_multi` mode only ever |
| 345 | // updates index 0 and every reader treats that as the value for all 4 |
| 346 | // indices (spec `read_delta_lf_params`'s own single-cdf branch). |
| 347 | thread_local! { |
| 348 | static CURRENT_DELTA_LF: std::cell::Cell<[i32; 4]> = const { std::cell::Cell::new([0; 4]) }; |
| 349 | } |
| 350 | // How many real `delta_lf_present` symbol groups [`maybe_read_delta_lf`] has |
| 351 | // read -- the gate's proof the reader fired, mirroring [`DELTA_Q_HITS`]. |
| 352 | thread_local! { |
| 353 | static DELTA_LF_HITS: std::cell::Cell<usize> = const { std::cell::Cell::new(0) }; |
| 354 | } |
| 355 | |
| 356 | /// Current value of [`DELTA_LF_HITS`]. |
| 357 | pub(crate) fn delta_lf_hits() -> usize { |
| 358 | DELTA_LF_HITS.with(|c| c.get()) |
| 359 | } |
| 360 | |
| 361 | /// `read_delta_lflevel`/`read_delta_lf_params` (spec 5.11.11, libaom |
| 362 | /// `decodemv.c:99-146,735-770`) -- same shape and same call-site gating as |
| 363 | /// [`maybe_read_delta_q`] (spec order `delta_q -> delta_lf`), looped over |
| 364 | /// `FRAME_LF_COUNT = 4` planes/directions when `delta_lf_multi` is set, or |
| 365 | /// read once into index 0 otherwise. |
| 366 | fn maybe_read_delta_lf(dec: &mut SymbolDecoder, cdfs: &mut Cdfs, mi_r: usize, mi_c: usize, is_whole_sb: bool, skip: bool) { |
| 367 | if !DELTA_LF_PRESENT.with(|c| c.get()) { |
| 368 | return; |
| 369 | } |
| 370 | let sb_mi = sb_mi_cur() as usize; |
| 371 | if mi_r % sb_mi != 0 || mi_c % sb_mi != 0 { |
| 372 | return; |
| 373 | } |
| 374 | // `is_whole_sb` is the caller's "this leaf IS the superblock" flag, |
| 375 | // computed against the sequence's own superblock size (lane-sb128b r1: |
| 376 | // it used to be hardcoded against 64 and disabled under `sb128`, which a |
| 377 | // `PARTITION_NONE` 128x128 root -- itself the whole superblock -- makes |
| 378 | // wrong). |
| 379 | if is_whole_sb && skip { |
| 380 | return; |
| 381 | } |
| 382 | let multi = DELTA_LF_MULTI.with(|c| c.get()); |
| 383 | let res = DELTA_LF_RES.with(|c| c.get()); |
| 384 | let count = if multi { 4 } else { 1 }; |
| 385 | let mut cur = CURRENT_DELTA_LF.with(|c| c.get()); |
| 386 | for i in 0..count { |
| 387 | let mut abs = if multi { |
| 388 | dec.symbol(&mut cdfs.delta_lf_multi[i]) as i32 |
| 389 | } else { |
| 390 | dec.symbol(&mut cdfs.delta_lf) as i32 |
| 391 | }; |
| 392 | const DELTA_LF_SMALL: i32 = 3; |
| 393 | if abs >= DELTA_LF_SMALL { |
| 394 | let rem_bits = dec.literal(3) + 1; |
| 395 | let thr = (1i32 << rem_bits) + 1; |
| 396 | abs = dec.literal(rem_bits) as i32 + thr; |
| 397 | } |
| 398 | let sign_negative = if abs != 0 { dec.literal(1) != 0 } else { true }; |
| 399 | let reduced = if sign_negative { -abs } else { abs }; |
| 400 | cur[i] = (cur[i] + reduced * res).clamp(-63, 63); |
| 401 | DELTA_LF_HITS.with(|c| c.set(c.get() + 1)); |
| 402 | } |
| 403 | CURRENT_DELTA_LF.with(|c| c.set(cur)); |
| 404 | } |
| 405 | |
| 406 | /// `read_cdef` (spec 5.11.56, libaom `decodemv.c:read_cdef`), called right |
| 407 | /// after `skip` is known at every block-decode call site. A no-op at |
| 408 | /// `cdef.bits == 0` (0-bit literal, nothing to consume) -- and |
| 409 | /// `read_cdef_params` already forces `bits = 0` under `coded_lossless`/ |
| 410 | /// `allow_intrabc`, so neither needs a separate check here. Reads once per |
| 411 | /// superblock, at the first non-skip block. |
| 412 | fn maybe_read_cdef_idx(dec: &mut SymbolDecoder, mi_r: usize, mi_c: usize, skip: bool) { |
| 413 | let bits = CDEF_BITS.with(|c| c.get()); |
| 414 | if bits == 0 || skip || CDEF_TRANSMITTED.with(|c| c.get()) { |
| 415 | return; |
| 416 | } |
| 417 | let idx = dec.literal(u32::from(bits)) as u8; |
| 418 | CDEF_TRANSMITTED.with(|c| c.set(true)); |
| 419 | CDEF_IDX_HITS.with(|c| c.set(c.get() + 1)); |
| 420 | let (sb_r, sb_c) = (mi_r / SB_MI as usize, mi_c / SB_MI as usize); |
| 421 | let sb_cols = CDEF_SB_COLS.with(|c| c.get()); |
| 422 | if sb_cols > 0 { |
| 423 | CDEF_IDX_GRID.with(|g| { |
| 424 | let mut g = g.borrow_mut(); |
| 425 | let i = sb_r * sb_cols + sb_c; |
| 426 | if i < g.len() { |
| 427 | g[i] = idx; |
| 428 | } |
| 429 | }); |
| 430 | } |
| 431 | } |
| 432 | |
| 433 | // --------------------------------------------------------------------------- |
| 434 | // lane-seg: segmentation. Header (spec 5.9.14) is parsed by `ec-av1-syntax`; |
| 435 | // this block is the per-block `segment_id` reader (spec 5.11.7-5.11.9) plus |
| 436 | // the two consumers a real stream exercises: the per-segment quantizer index |
| 437 | // (spec 7.12.2 `get_qindex`) and the per-segment loop-filter level |
| 438 | // (spec 7.14.4). Shapes follow libaom `decodemv.c:read_segment_id`/ |
| 439 | // `read_intra_segment_id`/`read_inter_segment_id` and |
| 440 | // `pred_common.h:av1_get_spatial_seg_pred` -- note libaom returns the SPATIAL |
| 441 | // PREDICTION (not 0) from `read_segment_id` when the block is `skip`, which |
| 442 | // is what the oracle every gate here compares against actually does. |
| 443 | thread_local! { |
| 444 | static SEG: std::cell::RefCell<SegmentationParams> = |
| 445 | std::cell::RefCell::new(SegmentationParams::default()); |
| 446 | /// `CurrentSegmentIds`: this frame's own map, `mi_rows * mi_cols` entries |
| 447 | /// with stride `mi_cols` (NOT the padded `Neighbours` grid stride). |
| 448 | static SEG_IDS: std::cell::RefCell<Vec<u8>> = const { std::cell::RefCell::new(Vec::new()) }; |
| 449 | /// `PrevSegmentIds`: the primary reference frame's saved map, empty when |
| 450 | /// this frame has no primary reference (spec `load_previous_segment_ids`). |
| 451 | static PREV_SEG_IDS: std::cell::RefCell<Vec<u8>> = const { std::cell::RefCell::new(Vec::new()) }; |
| 452 | /// `(mi_rows, mi_cols)` for both maps above. |
| 453 | static SEG_MI_DIMS: std::cell::Cell<(usize, usize)> = const { std::cell::Cell::new((0, 0)) }; |
| 454 | /// `AboveSegPredContext` (per mi column) / `LeftSegPredContext` (per mi |
| 455 | /// row) -- spec 5.11.9's own `seg_id_predicted` neighbour state, which |
| 456 | /// libaom keeps on the neighbouring `mbmi->seg_id_predicted` instead. |
| 457 | static ABOVE_SEG_PRED: std::cell::RefCell<Vec<u8>> = const { std::cell::RefCell::new(Vec::new()) }; |
| 458 | static LEFT_SEG_PRED: std::cell::RefCell<Vec<u8>> = const { std::cell::RefCell::new(Vec::new()) }; |
| 459 | /// The block currently being decoded -- read back by [`block_q_idx`]. |
| 460 | static CUR_SEGMENT_ID: std::cell::Cell<u8> = const { std::cell::Cell::new(0) }; |
| 461 | /// The tile's own top-left mi position, for `AvailU`/`AvailL`. |
| 462 | static SEG_TILE_ORIGIN: std::cell::Cell<(usize, usize)> = const { std::cell::Cell::new((0, 0)) }; |
| 463 | /// How many real `segment_id` symbols were read, and the set of segment |
| 464 | /// ids any block ended up with -- the gate's proof the feature fired. |
| 465 | static SEG_ID_HITS: std::cell::Cell<usize> = const { std::cell::Cell::new(0) }; |
| 466 | static SEG_IDS_SEEN: std::cell::Cell<u8> = const { std::cell::Cell::new(0) }; |
| 467 | /// How many `seg_id_predicted` symbols were read (temporal update). |
| 468 | static SEG_PRED_HITS: std::cell::Cell<usize> = const { std::cell::Cell::new(0) }; |
| 469 | } |
| 470 | |
| 471 | /// Installs this frame's segmentation parameters and allocates its map, with |
| 472 | /// `prev` the primary reference frame's saved map (spec |
| 473 | /// `load_previous_segment_ids`; `None` for `PRIMARY_REF_NONE` or an empty |
| 474 | /// slot). Called once per frame, whether or not segmentation is enabled. |
| 475 | pub(crate) fn set_segmentation( |
| 476 | seg: SegmentationParams, |
| 477 | mi_rows: usize, |
| 478 | mi_cols: usize, |
| 479 | prev: Option<&[u8]>, |
| 480 | ) { |
| 481 | SEG.with(|s| *s.borrow_mut() = seg); |
| 482 | SEG_MI_DIMS.with(|c| c.set((mi_rows, mi_cols))); |
| 483 | SEG_IDS.with(|m| *m.borrow_mut() = vec![0u8; mi_rows * mi_cols]); |
| 484 | PREV_SEG_IDS.with(|m| { |
| 485 | let mut m = m.borrow_mut(); |
| 486 | m.clear(); |
| 487 | if let Some(p) = prev { |
| 488 | if p.len() == mi_rows * mi_cols { |
| 489 | m.extend_from_slice(p); |
| 490 | } |
| 491 | } |
| 492 | }); |
| 493 | ABOVE_SEG_PRED.with(|m| *m.borrow_mut() = vec![0u8; mi_cols]); |
| 494 | LEFT_SEG_PRED.with(|m| *m.borrow_mut() = vec![0u8; mi_rows]); |
| 495 | CUR_SEGMENT_ID.with(|c| c.set(0)); |
| 496 | } |
| 497 | |
| 498 | /// This frame's decoded segment map, to be stored into whichever reference |
| 499 | /// slots `refresh_frame_flags` names (spec 7.20's `SavedSegmentIds`). |
| 500 | pub(crate) fn take_segment_ids() -> Vec<u8> { |
| 501 | SEG_IDS.with(|m| m.borrow().clone()) |
| 502 | } |
| 503 | |
| 504 | /// How many `segment_id` symbols have been read (never reset per frame). |
| 505 | pub(crate) fn segment_id_hits() -> usize { |
| 506 | SEG_ID_HITS.with(|c| c.get()) |
| 507 | } |
| 508 | |
| 509 | /// How many distinct segment ids any decoded block has carried. |
| 510 | pub(crate) fn segment_ids_seen() -> usize { |
| 511 | SEG_IDS_SEEN.with(|c| c.get().count_ones() as usize) |
| 512 | } |
| 513 | |
| 514 | /// How many `seg_id_predicted` symbols have been read (temporal update). |
| 515 | pub(crate) fn segment_pred_hits() -> usize { |
| 516 | SEG_PRED_HITS.with(|c| c.get()) |
| 517 | } |
| 518 | |
| 519 | /// Resets the segmentation hit counters (gate setup). |
| 520 | pub(crate) fn reset_segment_hits() { |
| 521 | SEG_ID_HITS.with(|c| c.set(0)); |
| 522 | SEG_IDS_SEEN.with(|c| c.set(0)); |
| 523 | SEG_PRED_HITS.with(|c| c.set(0)); |
| 524 | } |
| 525 | |
| 526 | /// `seg_feature_active_idx(segment_id, feature)` for the frame currently |
| 527 | /// being decoded, with its `FeatureData` value. |
| 528 | fn seg_feature(segment_id: usize, feature: usize) -> Option<i32> { |
| 529 | SEG.with(|s| { |
| 530 | let s = s.borrow(); |
| 531 | s.feature_active(segment_id, feature) |
| 532 | .then(|| i32::from(s.feature_data[segment_id][feature])) |
| 533 | }) |
| 534 | } |
| 535 | |
| 536 | /// `get_qindex(ignoreDeltaQ = 0, segment_id)` (spec 7.12.2, libaom |
| 537 | /// `av1_get_qindex`) for the block currently being decoded: the running |
| 538 | /// `CurrentQIndex` shifted by this block's segment's `SEG_LVL_ALT_Q` data. |
| 539 | /// Every dequant call site reads the block's quantizer through here. |
| 540 | fn block_q_idx() -> i32 { |
| 541 | let base = CURRENT_Q_IDX.with(|c| c.get()); |
| 542 | let seg_id = CUR_SEGMENT_ID.with(|c| c.get()) as usize; |
| 543 | match seg_feature(seg_id, SEG_LVL_ALT_Q) { |
| 544 | Some(data) => (base + data).clamp(0, 255), |
| 545 | None => base, |
| 546 | } |
| 547 | } |
| 548 | |
| 549 | /// The segment id recorded for one mi cell of the current frame's map. |
| 550 | fn segment_id_at(mi_r: usize, mi_c: usize) -> u8 { |
| 551 | let (mi_rows, mi_cols) = SEG_MI_DIMS.with(|c| c.get()); |
| 552 | if mi_r >= mi_rows || mi_c >= mi_cols { |
| 553 | return 0; |
| 554 | } |
| 555 | SEG_IDS.with(|m| m.borrow()[mi_r * mi_cols + mi_c]) |
| 556 | } |
| 557 | |
| 558 | /// `set_segment_id`: stamps `id` over every mi cell this block covers. |
| 559 | fn write_segment_id(mi_r: usize, mi_c: usize, w_mi: usize, h_mi: usize, id: u8) { |
| 560 | let (mi_rows, mi_cols) = SEG_MI_DIMS.with(|c| c.get()); |
| 561 | let (x_mis, y_mis) = (w_mi.min(mi_cols.saturating_sub(mi_c)), h_mi.min(mi_rows.saturating_sub(mi_r))); |
| 562 | SEG_IDS.with(|m| { |
| 563 | let mut m = m.borrow_mut(); |
| 564 | for y in 0..y_mis { |
| 565 | for x in 0..x_mis { |
| 566 | m[(mi_r + y) * mi_cols + mi_c + x] = id; |
| 567 | } |
| 568 | } |
| 569 | }); |
| 570 | CUR_SEGMENT_ID.with(|c| c.set(id)); |
| 571 | SEG_IDS_SEEN.with(|c| c.set(c.get() | 1 << id)); |
| 572 | } |
| 573 | |
| 574 | /// `get_predicted_segment_id` (libaom `dec_get_segment_id` over |
| 575 | /// `last_frame_seg_map`): the minimum previous-frame segment id over the |
| 576 | /// block's own mi footprint. `0` when there is no previous map. |
| 577 | fn predicted_segment_id(mi_r: usize, mi_c: usize, w_mi: usize, h_mi: usize) -> u8 { |
| 578 | let (mi_rows, mi_cols) = SEG_MI_DIMS.with(|c| c.get()); |
| 579 | PREV_SEG_IDS.with(|m| { |
| 580 | let m = m.borrow(); |
| 581 | if m.is_empty() { |
| 582 | return 0; |
| 583 | } |
| 584 | let (x_mis, y_mis) = (w_mi.min(mi_cols.saturating_sub(mi_c)), h_mi.min(mi_rows.saturating_sub(mi_r))); |
| 585 | let mut id = u8::MAX; |
| 586 | for y in 0..y_mis { |
| 587 | for x in 0..x_mis { |
| 588 | id = id.min(m[(mi_r + y) * mi_cols + mi_c + x]); |
| 589 | } |
| 590 | } |
| 591 | if id == u8::MAX { 0 } else { id } |
| 592 | }) |
| 593 | } |
| 594 | |
| 595 | /// `av1_neg_deinterleave` (libaom `decodemv.c:258`). |
| 596 | fn neg_deinterleave(diff: i32, reference: i32, max: i32) -> i32 { |
| 597 | if reference == 0 { |
| 598 | return diff; |
| 599 | } |
| 600 | if reference >= max - 1 { |
| 601 | return max - diff - 1; |
| 602 | } |
| 603 | let half = if 2 * reference < max { |
| 604 | diff <= 2 * reference |
| 605 | } else { |
| 606 | diff <= 2 * (max - reference - 1) |
| 607 | }; |
| 608 | if half { |
| 609 | return if diff & 1 != 0 { |
| 610 | reference + ((diff + 1) >> 1) |
| 611 | } else { |
| 612 | reference - (diff >> 1) |
| 613 | }; |
| 614 | } |
| 615 | if 2 * reference < max { diff } else { max - (diff + 1) } |
| 616 | } |
| 617 | |
| 618 | /// `av1_get_spatial_seg_pred`: `(prediction, cdf_index)` from the current |
| 619 | /// frame's already-decoded above/left/above-left neighbours. |
| 620 | fn spatial_seg_pred(mi_r: usize, mi_c: usize) -> (i32, usize) { |
| 621 | let (row0, col0) = SEG_TILE_ORIGIN.with(|c| c.get()); |
| 622 | let (avail_u, avail_l) = (mi_r > row0, mi_c > col0); |
| 623 | let prev_ul = (avail_u && avail_l).then(|| i32::from(segment_id_at(mi_r - 1, mi_c - 1))); |
| 624 | let prev_u = avail_u.then(|| i32::from(segment_id_at(mi_r - 1, mi_c))); |
| 625 | let prev_l = avail_l.then(|| i32::from(segment_id_at(mi_r, mi_c - 1))); |
| 626 | let ctx = match prev_ul { |
| 627 | None => 0, |
| 628 | Some(ul) if Some(ul) == prev_u && Some(ul) == prev_l => 2, |
| 629 | Some(ul) if Some(ul) == prev_u || Some(ul) == prev_l || prev_u == prev_l => 1, |
| 630 | Some(_) => 0, |
| 631 | }; |
| 632 | let pred = match (prev_u, prev_l) { |
| 633 | (None, l) => l.unwrap_or(0), |
| 634 | (Some(u), None) => u, |
| 635 | (Some(u), Some(l)) => { |
| 636 | if prev_ul == Some(u) { |
| 637 | u |
| 638 | } else { |
| 639 | l |
| 640 | } |
| 641 | } |
| 642 | }; |
| 643 | (pred, ctx) |
| 644 | } |
| 645 | |
| 646 | /// `read_segment_id` (libaom `decodemv.c:280`): the spatially predicted id |
| 647 | /// when `skip`, otherwise one `segment_id` symbol de-interleaved against |
| 648 | /// that prediction. Does NOT write the map -- callers do. |
| 649 | fn read_segment_id(dec: &mut SymbolDecoder, cdfs: &mut Cdfs, mi_r: usize, mi_c: usize, skip: bool) -> u8 { |
| 650 | let (pred, ctx) = spatial_seg_pred(mi_r, mi_c); |
| 651 | if skip { |
| 652 | return pred as u8; |
| 653 | } |
| 654 | let coded = dec.symbol(&mut cdfs.segment_id[ctx]) as i32; |
| 655 | SEG_ID_HITS.with(|c| c.set(c.get() + 1)); |
| 656 | let last_active = SEG.with(|s| i32::from(s.borrow().last_active_seg_id)); |
| 657 | neg_deinterleave(coded, pred, last_active + 1).clamp(0, last_active) as u8 |
| 658 | } |
| 659 | |
| 660 | /// `read_intra_segment_id` (spec 5.11.7): called twice per intra-frame block |
| 661 | /// -- once before `skip` when `SegIdPreSkip`, once after it otherwise (the |
| 662 | /// caller decides which by passing `pre_skip`). |
| 663 | fn intra_segment_id( |
| 664 | dec: &mut SymbolDecoder, |
| 665 | cdfs: &mut Cdfs, |
| 666 | mi_r: usize, |
| 667 | mi_c: usize, |
| 668 | w_mi: usize, |
| 669 | h_mi: usize, |
| 670 | skip: bool, |
| 671 | ) { |
| 672 | if !SEG.with(|s| s.borrow().enabled) { |
| 673 | return; |
| 674 | } |
| 675 | let id = read_segment_id(dec, cdfs, mi_r, mi_c, skip); |
| 676 | write_segment_id(mi_r, mi_c, w_mi, h_mi, id); |
| 677 | } |
| 678 | |
| 679 | /// Whether this frame's intra blocks read their `segment_id` before `skip` |
| 680 | /// (`SegIdPreSkip`); `false` also when segmentation is off. |
| 681 | fn seg_id_pre_skip() -> bool { |
| 682 | SEG.with(|s| { |
| 683 | let s = s.borrow(); |
| 684 | s.enabled && s.seg_id_pre_skip |
| 685 | }) |
| 686 | } |
| 687 | |
| 688 | /// `read_inter_segment_id(preskip)` (spec 5.11.9, libaom `decodemv.c:352`). |
| 689 | /// Called at both spec positions of an inter frame's block: `pre_skip = true` |
| 690 | /// before `skip_mode`/`skip`, and `pre_skip = false` after them. |
| 691 | fn inter_segment_id( |
| 692 | dec: &mut SymbolDecoder, |
| 693 | cdfs: &mut Cdfs, |
| 694 | mi_r: usize, |
| 695 | mi_c: usize, |
| 696 | w_mi: usize, |
| 697 | h_mi: usize, |
| 698 | skip: bool, |
| 699 | pre_skip: bool, |
| 700 | ) { |
| 701 | let (enabled, update_map, temporal_update, pre_skip_frame) = SEG.with(|s| { |
| 702 | let s = s.borrow(); |
| 703 | (s.enabled, s.update_map, s.temporal_update, s.seg_id_pre_skip) |
| 704 | }); |
| 705 | if !enabled { |
| 706 | return; |
| 707 | } |
| 708 | if !update_map { |
| 709 | // The map is inherited wholesale from the previous frame. |
| 710 | let id = predicted_segment_id(mi_r, mi_c, w_mi, h_mi); |
| 711 | write_segment_id(mi_r, mi_c, w_mi, h_mi, id); |
| 712 | return; |
| 713 | } |
| 714 | if pre_skip && !pre_skip_frame { |
| 715 | return; |
| 716 | } |
| 717 | if !pre_skip && skip { |
| 718 | // libaom: a skipped block never codes `seg_id_predicted`, and its |
| 719 | // segment id is the spatial prediction. |
| 720 | stamp_seg_pred(mi_r, mi_c, w_mi, h_mi, 0); |
| 721 | let id = read_segment_id(dec, cdfs, mi_r, mi_c, true); |
| 722 | write_segment_id(mi_r, mi_c, w_mi, h_mi, id); |
| 723 | return; |
| 724 | } |
| 725 | let id = if temporal_update { |
| 726 | let ctx = seg_pred_ctx(mi_r, mi_c); |
| 727 | let predicted = dec.symbol(&mut cdfs.segment_pred[ctx]) != 0; |
| 728 | SEG_PRED_HITS.with(|c| c.set(c.get() + 1)); |
| 729 | stamp_seg_pred(mi_r, mi_c, w_mi, h_mi, u8::from(predicted)); |
| 730 | if predicted { |
| 731 | predicted_segment_id(mi_r, mi_c, w_mi, h_mi) |
| 732 | } else { |
| 733 | read_segment_id(dec, cdfs, mi_r, mi_c, false) |
| 734 | } |
| 735 | } else { |
| 736 | read_segment_id(dec, cdfs, mi_r, mi_c, false) |
| 737 | }; |
| 738 | write_segment_id(mi_r, mi_c, w_mi, h_mi, id); |
| 739 | } |
| 740 | |
| 741 | /// `av1_get_pred_context_seg_id`: above + left `seg_id_predicted` flags. |
| 742 | fn seg_pred_ctx(mi_r: usize, mi_c: usize) -> usize { |
| 743 | // libaom reads the flag off `above_mbmi`/`left_mbmi`, which are NULL (and |
| 744 | // so contribute 0) outside the tile -- the availability check here is |
| 745 | // what keeps a previous tile's stamps out of this tile's context. |
| 746 | let (row0, col0) = SEG_TILE_ORIGIN.with(|c| c.get()); |
| 747 | let above = if mi_r > row0 { |
| 748 | ABOVE_SEG_PRED.with(|m| m.borrow().get(mi_c).copied().unwrap_or(0)) |
| 749 | } else { |
| 750 | 0 |
| 751 | }; |
| 752 | let left = if mi_c > col0 { |
| 753 | LEFT_SEG_PRED.with(|m| m.borrow().get(mi_r).copied().unwrap_or(0)) |
| 754 | } else { |
| 755 | 0 |
| 756 | }; |
| 757 | usize::from(above + left) |
| 758 | } |
| 759 | |
| 760 | /// Spec 5.11.9's `AboveSegPredContext`/`LeftSegPredContext` update. |
| 761 | fn stamp_seg_pred(mi_r: usize, mi_c: usize, w_mi: usize, h_mi: usize, value: u8) { |
| 762 | ABOVE_SEG_PRED.with(|m| { |
| 763 | let mut m = m.borrow_mut(); |
| 764 | for i in 0..w_mi { |
| 765 | if let Some(cell) = m.get_mut(mi_c + i) { |
| 766 | *cell = value; |
| 767 | } |
| 768 | } |
| 769 | }); |
| 770 | LEFT_SEG_PRED.with(|m| { |
| 771 | let mut m = m.borrow_mut(); |
| 772 | for i in 0..h_mi { |
| 773 | if let Some(cell) = m.get_mut(mi_r + i) { |
| 774 | *cell = value; |
| 775 | } |
| 776 | } |
| 777 | }); |
| 778 | } |
| 779 | |
| 780 | // How many key-frame luma blocks resolved `smooth_neighbor` (spec |
| 781 | // `get_intra_edge_filter_type`) to `true` -- lane-chroma r2's own before/after |
| 782 | // counter, proving a stream actually put a directional block next to a |
| 783 | // smooth-predicted neighbour rather than every block reading the (previously |
| 784 | // hardcoded `false`) edge-filter strength bucket unchanged. |
| 785 | thread_local! { |
| 786 | static SMOOTH_LUMA_HITS: std::cell::Cell<usize> = |
| 787 | const { std::cell::Cell::new(0) }; |
| 788 | } |
| 789 | |
| 790 | /// Current value of [`SMOOTH_LUMA_HITS`]. |
| 791 | pub(crate) fn smooth_luma_hits() -> usize { |
| 792 | SMOOTH_LUMA_HITS.with(|c| c.get()) |
| 793 | } |
| 794 | |
| 795 | thread_local! { |
| 796 | /// lane-band46 r1: how many split-transform units answered their intra |
| 797 | /// reach DIFFERENTLY under libaom's per-unit rules ([`Reach::of_tu`], the |
| 798 | /// palette2 r12 fix) than the standalone-block lookup this decoder used to |
| 799 | /// apply to them (class reach-is-per-transform-unit). Every hit is a |
| 800 | /// transform unit whose above-right / below-left reference samples would |
| 801 | /// be wrong without that fix. |
| 802 | static SPLIT_TU_REACH_FIX_HITS: std::cell::Cell<usize> = |
| 803 | const { std::cell::Cell::new(0) }; |
| 804 | } |
| 805 | |
| 806 | /// Current value of [`SPLIT_TU_REACH_FIX_HITS`]. |
| 807 | pub(crate) fn split_tu_reach_fix_hits() -> usize { |
| 808 | SPLIT_TU_REACH_FIX_HITS.with(|c| c.get()) |
| 809 | } |
| 810 | |
| 811 | /// [`Reach::of_tu`] for one transform unit, counting how often that per-unit |
| 812 | /// answer differs from the standalone-block answer the call sites used before |
| 813 | /// lane-palette2 r12 -- the counter a gate asserts on to prove a stream really |
| 814 | /// exercised the fix. `col_off`/`row_off` are in SAMPLES from the parent |
| 815 | /// block's top-left `(px, py)`. |
| 816 | #[allow(clippy::too_many_arguments)] |
| 817 | fn tu_reach( |
| 818 | bw: usize, |
| 819 | bh: usize, |
| 820 | col_off: usize, |
| 821 | row_off: usize, |
| 822 | tx: usize, |
| 823 | block: Reach, |
| 824 | px: usize, |
| 825 | py: usize, |
| 826 | width: usize, |
| 827 | height: usize, |
| 828 | ) -> Reach { |
| 829 | let fixed = Reach::of_tu(bw, bh, col_off, row_off, tx, tx, block); |
| 830 | let standalone = Reach::of(tx, px + col_off, py + row_off, width, height); |
| 831 | if fixed != standalone { |
| 832 | SPLIT_TU_REACH_FIX_HITS.with(|c| c.set(c.get() + 1)); |
| 833 | } |
| 834 | fixed |
| 835 | } |
| 836 | |
| 837 | // How many `uv_mode` reads resolved to `SMOOTH_PRED..=PAETH_PRED` (9..=12), |
| 838 | // across every call on the current thread -- lane-chroma r3's own before/after |
| 839 | // counter, proving a stream actually exercised chroma's smooth/paeth |
| 840 | // predictor (the previously-refused round-2 gap) rather than every block |
| 841 | // landing on DC/CFL/directional. |
| 842 | thread_local! { |
| 843 | static SMOOTH_UV_HITS: std::cell::Cell<usize> = |
| 844 | const { std::cell::Cell::new(0) }; |
| 845 | } |
| 846 | |
| 847 | /// Current value of [`SMOOTH_UV_HITS`]. |
| 848 | pub(crate) fn smooth_uv_hits() -> usize { |
| 849 | SMOOTH_UV_HITS.with(|c| c.get()) |
| 850 | } |
| 851 | |
| 852 | // How many tiles [`decode_key_frame_tile_with_cdfs`] has actually decoded a |
| 853 | // full superblock walk over, across every call on the current thread -- |
| 854 | // lane-tiles r2's own gate-blind-to-feature guard: a gate over a |
| 855 | // multi-tile stream must prove more than one tile actually fired, not just |
| 856 | // that the stream carried tile_info.cols > 1. |
| 857 | thread_local! { |
| 858 | static TILE_HITS: std::cell::Cell<usize> = |
| 859 | const { std::cell::Cell::new(0) }; |
| 860 | } |
| 861 | /// Current value of [`TILE_HITS`]. |
| 862 | pub(crate) fn tile_hits() -> usize { |
| 863 | TILE_HITS.with(|c| c.get()) |
| 864 | } |
| 865 | |
| 866 | // How many 4-pixel deblocking edge groups [`edge_params`] has actually |
| 867 | // selected for filtering, across every call on the current thread -- the same |
| 868 | // before/after counter pattern as [`FILTER_INTRA_HITS`], proving a stream |
| 869 | // exercised `apply_deblock` rather than every edge landing on level 0. |
| 870 | thread_local! { |
| 871 | static DEBLOCK_HITS: std::cell::Cell<usize> = |
| 872 | const { std::cell::Cell::new(0) }; |
| 873 | } |
| 874 | /// lane-comppin r4: decode-order picture counter for `EC_AV1_PREFILT_DUMP`, |
| 875 | /// mirroring stream.rs's `pictures_decoded` so the two dumps index |
| 876 | /// identically against aomdec's own `ec_frame_idx` in decodeframe.c. |
| 877 | static PREFILT_PICTURE_IDX: std::sync::atomic::AtomicUsize = |
| 878 | std::sync::atomic::AtomicUsize::new(0); |
| 879 | |
| 880 | /// Current value of [`DEBLOCK_HITS`]. |
| 881 | pub(crate) fn deblock_hits() -> usize { |
| 882 | DEBLOCK_HITS.with(|c| c.get()) |
| 883 | } |
| 884 | |
| 885 | // lane-defon r1: how many COMPOUND_REFERENCE blocks read a |
| 886 | // `UNIDIR_COMP_REFERENCE` pair (both references on the same temporal side, |
| 887 | // spec 5.11.25 `comp_reference_type == 0`) -- the gate's proof that a |
| 888 | // stream built with `--enable-onesided-comp=1` really exercised the |
| 889 | // `uni_comp_ref` alphabet rather than only bidirectional pairs. |
| 890 | thread_local! { |
| 891 | static UNI_COMP_HITS: std::cell::Cell<usize> = |
| 892 | const { std::cell::Cell::new(0) }; |
| 893 | } |
| 894 | |
| 895 | /// Current value of [`UNI_COMP_HITS`]. |
| 896 | pub(crate) fn uni_comp_hits() -> usize { |
| 897 | UNI_COMP_HITS.with(|c| c.get()) |
| 898 | } |
| 899 | |
| 900 | // lane-defon r1: how many blocks blended through a `COMPOUND_DIFFWTD` mask |
| 901 | // (`mc::diffwtd_mask`) -- the narrower half of `MASKED_COMPOUND_HITS`, the |
| 902 | // gate's proof that `--enable-diff-wtd-comp=1` reached the difference-weighted |
| 903 | // blend and not only the wedge arm. |
| 904 | thread_local! { |
| 905 | static DIFFWTD_HITS: std::cell::Cell<usize> = |
| 906 | const { std::cell::Cell::new(0) }; |
| 907 | } |
| 908 | |
| 909 | /// Current value of [`DIFFWTD_HITS`]. |
| 910 | pub(crate) fn diffwtd_hits() -> usize { |
| 911 | DIFFWTD_HITS.with(|c| c.get()) |
| 912 | } |
| 913 | |
| 914 | // lane-mcomp64 r1: how many blocks took the INFERRED `COMPOUND_DIFFWTD` arm -- |
| 915 | // a block size where `av1_is_wedge_used` is false (64x64 is the only such size |
| 916 | // this decoder's square inter path reaches), so no `compound_type` symbol is |
| 917 | // coded at all. Strictly narrower than `DIFFWTD_HITS`, which also counts the |
| 918 | // coded-symbol DIFFWTD blocks at 8x8/16x16/32x32. |
| 919 | thread_local! { |
| 920 | static DIFFWTD_INFERRED_HITS: std::cell::Cell<usize> = |
| 921 | const { std::cell::Cell::new(0) }; |
| 922 | } |
| 923 | |
| 924 | /// Current value of [`DIFFWTD_INFERRED_HITS`]. |
| 925 | pub(crate) fn diffwtd_inferred_hits() -> usize { |
| 926 | DIFFWTD_INFERRED_HITS.with(|c| c.get()) |
| 927 | } |
| 928 | |
| 929 | // lane-defon r1: how many FORWARD KEYFRAMES were decoded -- a KEY frame coded |
| 930 | // with `show_frame == 0`, output later by a `show_existing_frame` header |
| 931 | // (aomenc `--enable-fwd-kf=1 --fwd-kf-dist=N`). Counted from |
| 932 | // `stream::decode_stream`, which is where the frame header is consumed; the |
| 933 | // gate's proof that the on-value really reached the bitstream rather than |
| 934 | // being dropped by the rate control. |
| 935 | thread_local! { |
| 936 | static FWD_KF_HITS: std::cell::Cell<usize> = |
| 937 | const { std::cell::Cell::new(0) }; |
| 938 | } |
| 939 | |
| 940 | /// Current value of [`FWD_KF_HITS`]. |
| 941 | pub(crate) fn fwd_kf_hits() -> usize { |
| 942 | FWD_KF_HITS.with(|c| c.get()) |
| 943 | } |
| 944 | |
| 945 | /// Records one decoded forward keyframe (see [`FWD_KF_HITS`]). |
| 946 | pub(crate) fn note_fwd_kf() { |
| 947 | FWD_KF_HITS.with(|c| c.set(c.get() + 1)); |
| 948 | } |
| 949 | |
| 950 | // How many [`read_tx_size`] reads resolved a `tx_depth` strictly less than |
| 951 | // the block's own side, across every call on the current thread -- the same |
| 952 | // before/after counter pattern as [`FILTER_INTRA_HITS`], proving a stream |
| 953 | // actually exercised a *split* transform under `TxMode::Select` rather than |
| 954 | // every block coincidentally resolving `depth=0` (indistinguishable from |
| 955 | // `TxMode::Largest` pixel-wise). |
| 956 | thread_local! { |
| 957 | static TX_DEPTH_HITS: std::cell::Cell<usize> = |
| 958 | const { std::cell::Cell::new(0) }; |
| 959 | } |
| 960 | |
| 961 | // How many plain SQUARE intra blocks (lane-sqchroma r1) resolved a luma |
| 962 | // transform smaller than their own side (`tx_depth != 0`) while coding a |
| 963 | // chroma prediction other than `DC_PRED` -- the pair the square path's chroma |
| 964 | // only exercises with aomenc's tx-size search left ON (its default), which |
| 965 | // every older recipe in this file pins off. Counted at the block, so a stream |
| 966 | // where RD happened to give every split-transform block a DC chroma mode |
| 967 | // reads as zero rather than silently passing for free. |
| 968 | thread_local! { |
| 969 | static SQ_CHROMA_TX_HITS: std::cell::Cell<usize> = |
| 970 | const { std::cell::Cell::new(0) }; |
| 971 | } |
| 972 | |
| 973 | /// Current value of [`SQ_CHROMA_TX_HITS`]. |
| 974 | pub(crate) fn sq_chroma_tx_hits() -> usize { |
| 975 | SQ_CHROMA_TX_HITS.with(|c| c.get()) |
| 976 | } |
| 977 | |
| 978 | /// Current value of [`TX_DEPTH_HITS`]. |
| 979 | pub(crate) fn tx_depth_hits() -> usize { |
| 980 | TX_DEPTH_HITS.with(|c| c.get()) |
| 981 | } |
| 982 | |
| 983 | // lane-txselect: how many `txfm_split` symbols an inter frame's var-tx tree |
| 984 | // read, and how many of those actually took the split, on the current thread |
| 985 | // -- the same before/after counter pattern as [`TX_DEPTH_HITS`], proving a |
| 986 | // stream exercised spec 5.11.17's `read_var_tx_size` rather than every block |
| 987 | // coincidentally coding one whole-block transform. |
| 988 | thread_local! { |
| 989 | static TXFM_SPLIT_READS: std::cell::Cell<usize> = const { std::cell::Cell::new(0) }; |
| 990 | static TXFM_SPLIT_HITS: std::cell::Cell<usize> = const { std::cell::Cell::new(0) }; |
| 991 | // lane-inter4 r2: rectangular INTER residual coding -- how many rect |
| 992 | // strips coded one whole-block rect transform unit, and how many split |
| 993 | // their rect max transform into square sub-transforms. |
| 994 | static RECT_INTER_TU_HITS: std::cell::Cell<usize> = const { std::cell::Cell::new(0) }; |
| 995 | static RECT_INTER_TXSPLIT_HITS: std::cell::Cell<usize> = const { std::cell::Cell::new(0) }; |
| 996 | // lane-r14 r2: luma transform units of an inter strip with a 64-px axis |
| 997 | // (64x32/32x64/64x16/16x64), whose coefficients are coded as the |
| 998 | // truncated low-32 corner. |
| 999 | static RECT64_INTER_TU_HITS: std::cell::Cell<usize> = const { std::cell::Cell::new(0) }; |
| 1000 | // lane-r14 r3: rectangular var-tx LEAVES, [32x16, 16x32]. |
| 1001 | static VARTX_RECT_LEAF_HITS: std::cell::Cell<[usize; 2]> = const { std::cell::Cell::new([0; 2]) }; |
| 1002 | // lane-inter4 r3: 16x16-level PARTITION_HORZ / PARTITION_VERT on an inter |
| 1003 | // frame -- 16x8 and 8x16 inter leaves. |
| 1004 | static INTER_LEAF16_HORZ_HITS: std::cell::Cell<usize> = const { std::cell::Cell::new(0) }; |
| 1005 | static INTER_LEAF16_VERT_HITS: std::cell::Cell<usize> = const { std::cell::Cell::new(0) }; |
| 1006 | // This frame's own `tx_mode == TxMode::Select` and `reduced_tx_set` bits, |
| 1007 | // set once per inter tile by [`decode_inter_frame_tile_with_cdfs`] -- |
| 1008 | // `decode_inter_block` already carries forty parameters and both are |
| 1009 | // frame-constant. |
| 1010 | static TX_SELECT_INTER: std::cell::Cell<bool> = const { std::cell::Cell::new(false) }; |
| 1011 | static REDUCED_TX_SET_INTER: std::cell::Cell<bool> = const { std::cell::Cell::new(true) }; |
| 1012 | // lane-fiinter r1: this sequence header's own `enable_filter_intra` bit, |
| 1013 | // frame-constant like the two above and stashed the same way -- an intra |
| 1014 | // block inside an inter frame reads `use_filter_intra` under it. |
| 1015 | static ENABLE_FILTER_INTRA_INTER: std::cell::Cell<bool> = |
| 1016 | const { std::cell::Cell::new(false) }; |
| 1017 | } |
| 1018 | |
| 1019 | /// Current value of [`TXFM_SPLIT_READS`]. |
| 1020 | pub(crate) fn txfm_split_reads() -> usize { |
| 1021 | TXFM_SPLIT_READS.with(|c| c.get()) |
| 1022 | } |
| 1023 | |
| 1024 | /// Current value of [`TXFM_SPLIT_HITS`]. |
| 1025 | pub(crate) fn txfm_split_hits() -> usize { |
| 1026 | TXFM_SPLIT_HITS.with(|c| c.get()) |
| 1027 | } |
| 1028 | |
| 1029 | /// Current value of [`INTER_LEAF16_HORZ_HITS`] (lane-inter4 r3). |
| 1030 | pub(crate) fn inter_leaf16_horz_hits() -> usize { |
| 1031 | INTER_LEAF16_HORZ_HITS.with(|c| c.get()) |
| 1032 | } |
| 1033 | |
| 1034 | /// Current value of [`INTER_LEAF16_VERT_HITS`] (lane-inter4 r3). |
| 1035 | pub(crate) fn inter_leaf16_vert_hits() -> usize { |
| 1036 | INTER_LEAF16_VERT_HITS.with(|c| c.get()) |
| 1037 | } |
| 1038 | |
| 1039 | /// Current value of [`RECT_INTER_TU_HITS`] (lane-inter4 r2). |
| 1040 | pub(crate) fn rect_inter_tu_hits() -> usize { |
| 1041 | RECT_INTER_TU_HITS.with(|c| c.get()) |
| 1042 | } |
| 1043 | |
| 1044 | /// Current value of [`RECT_INTER_TXSPLIT_HITS`] (lane-inter4 r2). |
| 1045 | pub(crate) fn rect_inter_txsplit_hits() -> usize { |
| 1046 | RECT_INTER_TXSPLIT_HITS.with(|c| c.get()) |
| 1047 | } |
| 1048 | |
| 1049 | /// Current value of [`RECT64_INTER_TU_HITS`] (lane-r14 r2). |
| 1050 | pub(crate) fn rect64_inter_tu_hits() -> usize { |
| 1051 | RECT64_INTER_TU_HITS.with(|c| c.get()) |
| 1052 | } |
| 1053 | |
| 1054 | /// Current value of [`VARTX_RECT_LEAF_HITS`] (lane-r14 r3): how many |
| 1055 | /// rectangular var-tx leaf transform units were coded, `[32x16, 16x32]`. |
| 1056 | pub(crate) fn vartx_rect_leaf_hits() -> [usize; 2] { |
| 1057 | VARTX_RECT_LEAF_HITS.with(|c| c.get()) |
| 1058 | } |
| 1059 | |
| 1060 | /// Records one rectangular var-tx leaf. |
| 1061 | fn vartx_rect_leaf_hit(tw: usize, th: usize) { |
| 1062 | VARTX_RECT_LEAF_HITS.with(|c| { |
| 1063 | let mut h = c.get(); |
| 1064 | h[usize::from(th > tw)] += 1; |
| 1065 | c.set(h); |
| 1066 | }); |
| 1067 | } |
| 1068 | |
| 1069 | /// Sets the current thread's [`TX_SELECT_INTER`]/[`REDUCED_TX_SET_INTER`]/ |
| 1070 | /// [`ENABLE_FILTER_INTRA_INTER`]. |
| 1071 | pub(crate) fn set_inter_tx_mode( |
| 1072 | tx_select: bool, |
| 1073 | reduced_tx_set: bool, |
| 1074 | enable_filter_intra: bool, |
| 1075 | ) { |
| 1076 | TX_SELECT_INTER.with(|c| c.set(tx_select)); |
| 1077 | REDUCED_TX_SET_INTER.with(|c| c.set(reduced_tx_set)); |
| 1078 | ENABLE_FILTER_INTRA_INTER.with(|c| c.set(enable_filter_intra)); |
| 1079 | } |
| 1080 | |
| 1081 | // How many `partition_w16` reads (a 16x16 block's own partition symbol, key |
| 1082 | // intra-frame path) resolved to `PARTITION_VERT_B` -- lane-rect16 r1: a |
| 1083 | // plain default-settings aomenc run over lavfi mandelbrot hits this arm on |
| 1084 | // frame 0's very first non-NONE/SPLIT `partition_w16` symbol (mi=(3,2)), not |
| 1085 | // the plain HORZ/VERT this lane's charter assumed. Left rect (8x16, real |
| 1086 | // `decode_block_rect`) + two 8x8 leaves stacked on the right (`decode_leaf8`, |
| 1087 | // chained via `prev_leaf` exactly like the existing 16x16-SPLIT-into-4x8x8 |
| 1088 | // path above). |
| 1089 | thread_local! { |
| 1090 | static VERT_B_INTRA_HITS: std::cell::Cell<usize> = |
| 1091 | const { std::cell::Cell::new(0) }; |
| 1092 | } |
| 1093 | |
| 1094 | /// Current value of [`VERT_B_INTRA_HITS`]. |
| 1095 | pub(crate) fn vert_b_intra_hits() -> usize { |
| 1096 | VERT_B_INTRA_HITS.with(|c| c.get()) |
| 1097 | } |
| 1098 | |
| 1099 | // lane-ab16 r1: how many 16x16-level AB partitions fired, per arm, in |
| 1100 | // `PARTITION_HORZ_A`, `_HORZ_B`, `_VERT_A`, `_VERT_B` order. Each arm is two |
| 1101 | // 8x8 squares (`decode_leaf8`) plus one 16x8/8x16 strip (`decode_leaf_rect`, |
| 1102 | // the non-skip coefficient path lane-rectx proved); the gate hard-asserts |
| 1103 | // every arm separately so a run where aomenc's RD only picked one cannot |
| 1104 | // pass as proof of four. |
| 1105 | thread_local! { |
| 1106 | static AB16_HITS: std::cell::Cell<[usize; 4]> = const { std::cell::Cell::new([0; 4]) }; |
| 1107 | } |
| 1108 | |
| 1109 | /// [`AB16_HITS`] per arm (HORZ_A, HORZ_B, VERT_A, VERT_B at 16x16). |
| 1110 | thread_local! { |
| 1111 | /// 4x4 transform units decoded inside an 8x8 square of a |
| 1112 | /// `PARTITION_VERT_A`/`_B` -- the exact call that panicked before |
| 1113 | /// [`crate::encode::Reach::of_tu`] (lane-ab16 r2), so the gate can prove |
| 1114 | /// a real `--enable-tx-size-search=1` stream reaches it. |
| 1115 | static VERT_AB_TX4_HITS: std::cell::Cell<usize> = const { std::cell::Cell::new(0) }; |
| 1116 | } |
| 1117 | |
| 1118 | pub(crate) fn vert_ab_tx4_hits() -> usize { |
| 1119 | VERT_AB_TX4_HITS.with(std::cell::Cell::get) |
| 1120 | } |
| 1121 | |
| 1122 | pub(crate) fn ab16_hits_by_arm() -> [usize; 4] { |
| 1123 | AB16_HITS.with(std::cell::Cell::get) |
| 1124 | } |
| 1125 | |
| 1126 | fn bump_ab16(arm: usize) { |
| 1127 | AB16_HITS.with(|c| { |
| 1128 | let mut v = c.get(); |
| 1129 | v[arm] += 1; |
| 1130 | c.set(v); |
| 1131 | }); |
| 1132 | } |
| 1133 | |
| 1134 | // How many `partition_w16` reads resolved to a plain `PARTITION_HORZ`/ |
| 1135 | // `PARTITION_VERT` (lane-rect16 r2): re-measuring after the VERT_B fix above |
| 1136 | // showed mandelbrot's real first-hit blocker is this plain arm at mi=(0,7), |
| 1137 | // not VERT_B (`refusal-names-a-correlate`; the VERT_B write-up's "frame 0's |
| 1138 | // very first non-NONE/SPLIT symbol" claim was about decode ORDER within one |
| 1139 | // superblock's z-order recursion, not raster mi position). |
| 1140 | thread_local! { |
| 1141 | static HORZ_VERT_INTRA_HITS: std::cell::Cell<usize> = |
| 1142 | const { std::cell::Cell::new(0) }; |
| 1143 | } |
| 1144 | |
| 1145 | /// Current value of [`HORZ_VERT_INTRA_HITS`]. |
| 1146 | pub(crate) fn horz_vert_intra_hits() -> usize { |
| 1147 | HORZ_VERT_INTRA_HITS.with(|c| c.get()) |
| 1148 | } |
| 1149 | |
| 1150 | // How many blocks were decoded as a frame-edge rect strip -- the single |
| 1151 | // coded half of a `PARTITION_HORZ`/`PARTITION_VERT` at the true frame edge |
| 1152 | // (lane-oddh r1), i.e. a block living inside the 8px band of a frame whose |
| 1153 | // height or width is 8 mod 16 (the user's 3840x1608 film). Intra and inter |
| 1154 | // paths share the counter. |
| 1155 | thread_local! { |
| 1156 | static EDGE_RECT_STRIP_HITS: std::cell::Cell<usize> = |
| 1157 | const { std::cell::Cell::new(0) }; |
| 1158 | } |
| 1159 | |
| 1160 | /// Current value of [`EDGE_RECT_STRIP_HITS`]. |
| 1161 | pub(crate) fn edge_rect_strip_hits() -> usize { |
| 1162 | EDGE_RECT_STRIP_HITS.with(|c| c.get()) |
| 1163 | } |
| 1164 | |
| 1165 | // lane-interedge r1: frame-edge half strips on the INTER path, per partition |
| 1166 | // level (64/32/16) x axis (HORZ at a bottom edge, VERT at a right edge). The |
| 1167 | // index is `level_idx * 2 + axis` with `level_idx` 0=64, 1=32, 2=16 and |
| 1168 | // `axis` 0=HORZ (`!has_rows`), 1=VERT (`!has_cols`). Both of the user's films |
| 1169 | // are height mod 16 == 8, so every inter frame crosses these nodes. |
| 1170 | thread_local! { |
| 1171 | static INTER_EDGE_STRIP_HITS: std::cell::Cell<[usize; 6]> = |
| 1172 | const { std::cell::Cell::new([0; 6]) }; |
| 1173 | } |
| 1174 | |
| 1175 | /// Bumps one bucket of [`INTER_EDGE_STRIP_HITS`]. |
| 1176 | pub(crate) fn bump_inter_edge_strip(idx: usize) { |
| 1177 | INTER_EDGE_STRIP_HITS.with(|c| { |
| 1178 | let mut v = c.get(); |
| 1179 | v[idx] += 1; |
| 1180 | c.set(v); |
| 1181 | }); |
| 1182 | } |
| 1183 | |
| 1184 | /// Current value of [`INTER_EDGE_STRIP_HITS`] (`[h64, v64, h32, v32, h16, v16]`). |
| 1185 | pub fn inter_edge_strip_hits() -> [usize; 6] { |
| 1186 | INTER_EDGE_STRIP_HITS.with(|c| c.get()) |
| 1187 | } |
| 1188 | |
| 1189 | /// Resets [`INTER_EDGE_STRIP_HITS`] -- a refused attempt must not carry hits |
| 1190 | /// into the next one (class `counter-from-refused-stream`). |
| 1191 | pub fn reset_inter_edge_strip_hits() { |
| 1192 | INTER_EDGE_STRIP_HITS.with(|c| c.set([0; 6])); |
| 1193 | } |
| 1194 | |
| 1195 | // How many HORZ/VERT intra strips resolved a SPLIT luma transform |
| 1196 | // (`tx_depth != 0`) and decoded it per transform unit (lane-rectsplit r1) -- |
| 1197 | // the case every rect path here refused by name before, and the first |
| 1198 | // refusal the user's ReelB extract (600s in) stops at. |
| 1199 | thread_local! { |
| 1200 | static RECT_SPLIT_TX_HITS: std::cell::Cell<usize> = |
| 1201 | const { std::cell::Cell::new(0) }; |
| 1202 | } |
| 1203 | |
| 1204 | /// Current value of [`RECT_SPLIT_TX_HITS`]. |
| 1205 | pub(crate) fn rect_split_tx_hits() -> usize { |
| 1206 | RECT_SPLIT_TX_HITS.with(|c| c.get()) |
| 1207 | } |
| 1208 | |
| 1209 | // The subset of [`RECT_SPLIT_TX_HITS`] that is BOTH superblock-level (one |
| 1210 | // side 64) AND split past the first depth, i.e. tiled more than one unit in |
| 1211 | // each axis -- the only shape with a transform unit that has neighbours on |
| 1212 | // two sides inside its own strip (`row_off > 0 && col_off > 0`), and the one |
| 1213 | // no gate fired before lane-rectsplit r2 (the shared counter above also |
| 1214 | // counts 32x32-level and depth-1 strips, so it cannot prove this case). |
| 1215 | thread_local! { |
| 1216 | static RECT_SPLIT_SB_INTERIOR_TU_HITS: std::cell::Cell<usize> = |
| 1217 | const { std::cell::Cell::new(0) }; |
| 1218 | } |
| 1219 | |
| 1220 | /// Current value of [`RECT_SPLIT_SB_INTERIOR_TU_HITS`]. |
| 1221 | pub(crate) fn rect_split_sb_interior_tu_hits() -> usize { |
| 1222 | RECT_SPLIT_SB_INTERIOR_TU_HITS.with(|c| c.get()) |
| 1223 | } |
| 1224 | |
| 1225 | // Per-shape hit counts for the rect chroma transforms lane-rectchroma r1 |
| 1226 | // wired into [`decode_rect_split`] -- the shapes a split-transform strip's |
| 1227 | // chroma half takes that had no coefficient tables before (index order: |
| 1228 | // 32x8, 8x32, 8x4, 4x8). One counter per shape because a gate that only |
| 1229 | // proves the pair it happens to hit cannot catch a transposed scan/context |
| 1230 | // table (class scan-weights-cross-axis). |
| 1231 | thread_local! { |
| 1232 | static RECT_SPLIT_CHROMA_SHAPE_HITS: std::cell::Cell<[usize; 4]> = |
| 1233 | const { std::cell::Cell::new([0; 4]) }; |
| 1234 | } |
| 1235 | |
| 1236 | /// Current value of [`RECT_SPLIT_CHROMA_SHAPE_HITS`] (32x8, 8x32, 8x4, 4x8). |
| 1237 | pub(crate) fn rect_split_chroma_shape_hits() -> [usize; 4] { |
| 1238 | RECT_SPLIT_CHROMA_SHAPE_HITS.with(|c| c.get()) |
| 1239 | } |
| 1240 | |
| 1241 | // How many HORZ/VERT intra strips actually predicted with filter intra |
| 1242 | // (lane-rectsplit r1) -- the case `decode_block_rect` refused by name |
| 1243 | // ("this decoder predicts square-only") until `predict_filter_intra` took |
| 1244 | // `bw`x`bh`. |
| 1245 | thread_local! { |
| 1246 | static FILTER_INTRA_RECT_HITS: std::cell::Cell<usize> = |
| 1247 | const { std::cell::Cell::new(0) }; |
| 1248 | } |
| 1249 | |
| 1250 | /// Current value of [`FILTER_INTRA_RECT_HITS`]. |
| 1251 | pub(crate) fn filter_intra_rect_hits() -> usize { |
| 1252 | FILTER_INTRA_RECT_HITS.with(|c| c.get()) |
| 1253 | } |
| 1254 | |
| 1255 | // As [`FILTER_INTRA_RECT_HITS`], counting only the 32x32-level 1:4 strips |
| 1256 | // (32x8/8x32, lane-fi32x8 r1) -- the shapes whose `use_filter_intra` symbol |
| 1257 | // was never read at all before that round, so a gate needs to prove the flag |
| 1258 | // fired at THAT shape and not at a 16x8 one somewhere else in the frame. |
| 1259 | // Split by ORIENTATION: rows 8 (32x8) and 9 (8x32) of `cdf::FILTER_INTRA` |
| 1260 | // carry different probabilities (18101 vs 20229), so a transposed pair is |
| 1261 | // only caught by a gate that sees the flag fire on BOTH shapes (class |
| 1262 | // scan-weights-cross-axis). |
| 1263 | thread_local! { |
| 1264 | static FILTER_INTRA_RECT4_HORZ_HITS: std::cell::Cell<usize> = |
| 1265 | const { std::cell::Cell::new(0) }; |
| 1266 | static FILTER_INTRA_RECT4_VERT_HITS: std::cell::Cell<usize> = |
| 1267 | const { std::cell::Cell::new(0) }; |
| 1268 | } |
| 1269 | |
| 1270 | /// Current value of [`FILTER_INTRA_RECT4_HORZ_HITS`] (32x8 strips). |
| 1271 | pub(crate) fn filter_intra_rect4_horz_hits() -> usize { |
| 1272 | FILTER_INTRA_RECT4_HORZ_HITS.with(|c| c.get()) |
| 1273 | } |
| 1274 | |
| 1275 | /// Current value of [`FILTER_INTRA_RECT4_VERT_HITS`] (8x32 strips). |
| 1276 | pub(crate) fn filter_intra_rect4_vert_hits() -> usize { |
| 1277 | FILTER_INTRA_RECT4_VERT_HITS.with(|c| c.get()) |
| 1278 | } |
| 1279 | |
| 1280 | // How many rectangular transform units actually coded coefficients through |
| 1281 | // [`read_coeffs_rect`] (`all_zero == 0`) -- lane-rect1d's measure that a gate |
| 1282 | // exercised the rect coefficient reader at all rather than passing on |
| 1283 | // all-skip blocks. |
| 1284 | thread_local! { |
| 1285 | static RECT_COEFF_TU_HITS: std::cell::Cell<usize> = const { std::cell::Cell::new(0) }; |
| 1286 | static RECT_CLASS1_HITS: std::cell::Cell<usize> = const { std::cell::Cell::new(0) }; |
| 1287 | } |
| 1288 | |
| 1289 | /// Current value of [`RECT_COEFF_TU_HITS`]. |
| 1290 | pub(crate) fn rect_coeff_tu_hits() -> usize { |
| 1291 | RECT_COEFF_TU_HITS.with(|c| c.get()) |
| 1292 | } |
| 1293 | |
| 1294 | /// Rectangular transform units whose resolved `tx_type` is a 1D class |
| 1295 | /// (`V_DCT`/`H_DCT`) -- [`TX_CLASS1_HITS`]'s rect-only half, so a gate can |
| 1296 | /// pin that the 1D-class-on-a-RECT-transform path fired rather than settling |
| 1297 | /// for the square reader's own 1D types. |
| 1298 | pub(crate) fn rect_class1_hits() -> usize { |
| 1299 | RECT_CLASS1_HITS.with(|c| c.get()) |
| 1300 | } |
| 1301 | |
| 1302 | // As [`FILTER_INTRA_RECT_HITS`], counting only the strips BELOW 16x16 |
| 1303 | // (8x16/16x8 leaves of a 16x16 HORZ/VERT partition, lane-fistrip r1) -- the |
| 1304 | // shapes whose `use_filter_intra` symbol had no CDF class before this round. |
| 1305 | // A gate asserting the wider counter would be satisfied by a 32x16 strip and |
| 1306 | // prove nothing about the new shapes. |
| 1307 | thread_local! { |
| 1308 | static FILTER_INTRA_RECT_SUB16_HITS: std::cell::Cell<usize> = |
| 1309 | const { std::cell::Cell::new(0) }; |
| 1310 | } |
| 1311 | |
| 1312 | /// Current value of [`FILTER_INTRA_RECT_SUB16_HITS`]. |
| 1313 | pub(crate) fn filter_intra_rect_sub16_hits() -> usize { |
| 1314 | FILTER_INTRA_RECT_SUB16_HITS.with(|c| c.get()) |
| 1315 | } |
| 1316 | |
| 1317 | // As [`FILTER_INTRA_RECT_SUB16_HITS`], one partition level further down: |
| 1318 | // 4x8/8x4 leaves of an 8x8 HORZ/VERT (lane-fi8 r1). A gate on the sub16 |
| 1319 | // counter would be satisfied by a 16x8 strip and prove nothing here. |
| 1320 | thread_local! { |
| 1321 | static FILTER_INTRA_RECT_SUB8_HITS: std::cell::Cell<usize> = |
| 1322 | const { std::cell::Cell::new(0) }; |
| 1323 | } |
| 1324 | |
| 1325 | /// Current value of [`FILTER_INTRA_RECT_SUB8_HITS`]. |
| 1326 | pub(crate) fn filter_intra_rect_sub8_hits() -> usize { |
| 1327 | FILTER_INTRA_RECT_SUB8_HITS.with(|c| c.get()) |
| 1328 | } |
| 1329 | |
| 1330 | // How many [`read_coeffs`] reads resolved a `tx_type` outside `TX_CLASS_2D` |
| 1331 | // (`V_DCT`/`H_DCT`, spec 5.11.39's `TxClass::Horiz`/`Vert`), across every |
| 1332 | // call on the current thread -- the same before/after counter pattern as |
| 1333 | // [`TX_DEPTH_HITS`], proving a stream actually exercised the class-split |
| 1334 | // `eob_pt`/1D-neighbour context path rather than every block coincidentally |
| 1335 | // landing on `DCT_DCT`. |
| 1336 | thread_local! { |
| 1337 | static TX_CLASS1_HITS: std::cell::Cell<usize> = |
| 1338 | const { std::cell::Cell::new(0) }; |
| 1339 | } |
| 1340 | |
| 1341 | // How many `uv_mode` reads resolved to a directional chroma mode (neither |
| 1342 | // `DC_PRED` nor `UV_CFL_PRED`), across every call on the current thread -- the same |
| 1343 | // before/after counter pattern as [`FILTER_INTRA_HITS`], proving a stream |
| 1344 | // actually exercised chroma's own directional predictor. |
| 1345 | thread_local! { |
| 1346 | static DIRECTIONAL_UV_HITS: std::cell::Cell<usize> = |
| 1347 | const { std::cell::Cell::new(0) }; |
| 1348 | } |
| 1349 | |
| 1350 | /// Current value of [`DIRECTIONAL_UV_HITS`]. |
| 1351 | pub(crate) fn directional_uv_hits() -> usize { |
| 1352 | DIRECTIONAL_UV_HITS.with(|c| c.get()) |
| 1353 | } |
| 1354 | |
| 1355 | // How many `angle_delta`/`angle_delta_uv` symbols this decoder has read as |
| 1356 | // something other than [`ANGLE_DELTA_ZERO`], across every call in the |
| 1357 | // process -- the same before/after counter pattern as [`FILTER_INTRA_HITS`], |
| 1358 | // proving a stream actually exercised a nonzero angle delta. |
| 1359 | thread_local! { |
| 1360 | static ANGLE_DELTA_HITS: std::cell::Cell<usize> = |
| 1361 | const { std::cell::Cell::new(0) }; |
| 1362 | } |
| 1363 | |
| 1364 | // How many [`read_single_ref`] reads resolved to a reference other than |
| 1365 | // `LAST_FRAME`, across every call on the current thread -- the same before/after |
| 1366 | // counter pattern as [`FILTER_INTRA_HITS`], proving a stream actually |
| 1367 | // exercised a non-`LAST_FRAME` reference (`GOLDEN_FRAME`, this decoder's |
| 1368 | // only other supported one so far) rather than every block coincidentally |
| 1369 | // resolving `LAST_FRAME`. |
| 1370 | thread_local! { |
| 1371 | static NON_LAST_REF_HITS: std::cell::Cell<usize> = |
| 1372 | const { std::cell::Cell::new(0) }; |
| 1373 | } |
| 1374 | |
| 1375 | /// Current value of [`NON_LAST_REF_HITS`]. |
| 1376 | pub(crate) fn non_last_ref_hits() -> usize { |
| 1377 | NON_LAST_REF_HITS.with(|c| c.get()) |
| 1378 | } |
| 1379 | |
| 1380 | // Per-reference [`NON_LAST_REF_HITS`] breakdown (lane-av1refs): one counter |
| 1381 | // per `MV_REFERENCE_FRAME` past `LAST_FRAME`, so a gate targeting one |
| 1382 | // specific reference (`LAST2`/`LAST3`/`GOLDEN`/`BWDREF`/`ALTREF2`/`ALTREF`) |
| 1383 | // can prove THAT reference fired rather than any non-`LAST_FRAME` one -- |
| 1384 | // `NON_LAST_REF_HITS` alone cannot tell a `LAST2_FRAME` gate from a |
| 1385 | // `GOLDEN_FRAME` draw it did not ask for. |
| 1386 | thread_local! { |
| 1387 | static LAST2_HITS: std::cell::Cell<usize> = |
| 1388 | const { std::cell::Cell::new(0) }; |
| 1389 | } |
| 1390 | thread_local! { |
| 1391 | static LAST3_HITS: std::cell::Cell<usize> = |
| 1392 | const { std::cell::Cell::new(0) }; |
| 1393 | } |
| 1394 | thread_local! { |
| 1395 | static GOLDEN_HITS: std::cell::Cell<usize> = |
| 1396 | const { std::cell::Cell::new(0) }; |
| 1397 | } |
| 1398 | thread_local! { |
| 1399 | static BWDREF_HITS: std::cell::Cell<usize> = |
| 1400 | const { std::cell::Cell::new(0) }; |
| 1401 | } |
| 1402 | thread_local! { |
| 1403 | static ALTREF2_HITS: std::cell::Cell<usize> = |
| 1404 | const { std::cell::Cell::new(0) }; |
| 1405 | } |
| 1406 | thread_local! { |
| 1407 | static ALTREF_HITS: std::cell::Cell<usize> = |
| 1408 | const { std::cell::Cell::new(0) }; |
| 1409 | } |
| 1410 | |
| 1411 | // How many [`read_comp_mode`] reads resolved `COMPOUND_REFERENCE` (lane-av1comp) |
| 1412 | // -- proves a `reference_select` stream actually reached a block that picked |
| 1413 | // compound, not just that the header bit was set. Every such block is |
| 1414 | // refused after this fires ([`decode_inter_block`]/[`decode_inter_block8`]), |
| 1415 | // so this only ever counts attempted reads, never a completed decode. |
| 1416 | thread_local! { |
| 1417 | static COMP_MODE_HITS: std::cell::Cell<usize> = |
| 1418 | const { std::cell::Cell::new(0) }; |
| 1419 | } |
| 1420 | |
| 1421 | /// Current value of [`COMP_MODE_HITS`]. |
| 1422 | pub(crate) fn comp_mode_hits() -> usize { |
| 1423 | COMP_MODE_HITS.with(|c| c.get()) |
| 1424 | } |
| 1425 | |
| 1426 | // How many blocks [`read_intra_mode`] decoded a real (nonzero-size) palette-Y |
| 1427 | // use for, across every call on the current thread -- the same before/after |
| 1428 | // counter pattern as [`FILTER_INTRA_HITS`], proving a stream actually |
| 1429 | // exercised palette reconstruction rather than only reading and refusing the |
| 1430 | // `palette_y_mode` symbol. |
| 1431 | thread_local! { |
| 1432 | static PALETTE_HITS: std::cell::Cell<usize> = const { std::cell::Cell::new(0) }; |
| 1433 | } |
| 1434 | |
| 1435 | /// Current value of [`PALETTE_HITS`]. |
| 1436 | pub(crate) fn palette_hits() -> usize { |
| 1437 | PALETTE_HITS.with(|c| c.get()) |
| 1438 | } |
| 1439 | |
| 1440 | // lane-tiles r12: of those, how many sat at their own TILE's left column -- |
| 1441 | // exactly the blocks whose palette cache/ctx must NOT see the previous tile's |
| 1442 | // colours (`av1_get_palette_cache`'s `xd->left_mbmi == NULL`). A multi-tile |
| 1443 | // palette gate that never hits one proves nothing about the tile guard. |
| 1444 | thread_local! { |
| 1445 | static PALETTE_TILE_LEFT_HITS: std::cell::Cell<usize> = const { std::cell::Cell::new(0) }; |
| 1446 | } |
| 1447 | |
| 1448 | /// Current value of [`PALETTE_TILE_LEFT_HITS`]. |
| 1449 | pub(crate) fn palette_tile_left_hits() -> usize { |
| 1450 | PALETTE_TILE_LEFT_HITS.with(|c| c.get()) |
| 1451 | } |
| 1452 | |
| 1453 | // lane-palette2 r1: as [`PALETTE_HITS`], for a real (nonzero-size) chroma |
| 1454 | // palette use -- proves a stream actually reconstructed UV palette pixels, |
| 1455 | // not just read and refused the `palette_uv_mode` symbol. |
| 1456 | thread_local! { |
| 1457 | static PALETTE_UV_HITS: std::cell::Cell<usize> = const { std::cell::Cell::new(0) }; |
| 1458 | } |
| 1459 | |
| 1460 | /// Current value of [`PALETTE_UV_HITS`]. |
| 1461 | pub(crate) fn palette_uv_hits() -> usize { |
| 1462 | PALETTE_UV_HITS.with(|c| c.get()) |
| 1463 | } |
| 1464 | |
| 1465 | // lane-palette2 r1: how many HORZ/VERT rect intra strips ([`read_intra_mode_rect`]) |
| 1466 | // decoded a real palette-Y use -- proves the rect strip's own palette syntax |
| 1467 | // (not just the square path's) actually fired. |
| 1468 | thread_local! { |
| 1469 | static PALETTE_RECT_HITS: std::cell::Cell<usize> = const { std::cell::Cell::new(0) }; |
| 1470 | } |
| 1471 | |
| 1472 | /// Current value of [`PALETTE_RECT_HITS`]. |
| 1473 | pub(crate) fn palette_rect_hits() -> usize { |
| 1474 | PALETTE_RECT_HITS.with(|c| c.get()) |
| 1475 | } |
| 1476 | |
| 1477 | // lane-palette2 r6: how many HORZ/VERT rect intra strips ran the |
| 1478 | // [`palette_bsize_ctx_wh`]-gated code path in [`read_intra_mode_rect`] -- |
| 1479 | // i.e. `allow_screen_content_tools` was set and the strip's `bw`x`bh` was in |
| 1480 | // range, so `palette_y_mode`/`palette_uv_mode` were actually read (and, per |
| 1481 | // RD, almost always refused-on-use because the block doesn't end up USING a |
| 1482 | // palette -- see r5's report). This is the site the removed refusal |
| 1483 | // ("a HORZ/VERT intra strip in a screen-content frame") used to guard: it |
| 1484 | // fired here whether or not the block picked a palette, so this is the |
| 1485 | // counter that proves the case r5's narrower [`PALETTE_RECT_HITS`] almost |
| 1486 | // never observes. |
| 1487 | thread_local! { |
| 1488 | static RECT_SCREEN_CONTENT_HITS: std::cell::Cell<usize> = const { std::cell::Cell::new(0) }; |
| 1489 | } |
| 1490 | |
| 1491 | /// Current value of [`RECT_SCREEN_CONTENT_HITS`]. |
| 1492 | pub(crate) fn rect_screen_content_hits() -> usize { |
| 1493 | RECT_SCREEN_CONTENT_HITS.with(|c| c.get()) |
| 1494 | } |
| 1495 | |
| 1496 | // lane-palette2 r1: how many palette-Y luma transform UNITS decoded under a |
| 1497 | // split luma transform (`logical_tx != side`) -- proves the per-TU index-map |
| 1498 | // windowing actually ran, not just that the whole-block path stayed exact. |
| 1499 | // Counts units, not blocks (a 64x64 palette block split to 16x16 TUs adds 16); |
| 1500 | // first actually incremented in r7, when the refusal it guarded was lifted. |
| 1501 | thread_local! { |
| 1502 | static PALETTE_SPLIT_TX_HITS: std::cell::Cell<usize> = const { std::cell::Cell::new(0) }; |
| 1503 | } |
| 1504 | |
| 1505 | thread_local! { |
| 1506 | /// lane-kf900 r4: rect intra strips that ACTUALLY use a palette -- the |
| 1507 | /// 1:4 shapes (32x8/8x32, 16x4/4x16) and the sub-16x16 2:1 leaves |
| 1508 | /// (16x8/8x16), the two shapes the lifted refusals named. |
| 1509 | static RECT4_PALETTE_HITS: std::cell::Cell<usize> = const { std::cell::Cell::new(0) }; |
| 1510 | } |
| 1511 | |
| 1512 | /// Current value of [`RECT4_PALETTE_HITS`]. |
| 1513 | pub(crate) fn rect4_palette_hits() -> usize { |
| 1514 | RECT4_PALETTE_HITS.with(|c| c.get()) |
| 1515 | } |
| 1516 | |
| 1517 | /// Current value of [`PALETTE_SPLIT_TX_HITS`]. |
| 1518 | pub(crate) fn palette_split_tx_hits() -> usize { |
| 1519 | PALETTE_SPLIT_TX_HITS.with(|c| c.get()) |
| 1520 | } |
| 1521 | |
| 1522 | // lane-hgkf r2: SKIPPED intra blocks whose `tx_depth` split the luma transform |
| 1523 | // and which are therefore predicted one transform unit at a time (a skipped |
| 1524 | // block codes no coefficients, so nothing else in the stream witnesses this |
| 1525 | // path -- only the pixels do). |
| 1526 | thread_local! { |
| 1527 | static SKIP_SPLIT_TX_HITS: std::cell::Cell<usize> = const { std::cell::Cell::new(0) }; |
| 1528 | } |
| 1529 | |
| 1530 | /// Current value of [`SKIP_SPLIT_TX_HITS`] (blocks, not units). |
| 1531 | pub fn skip_split_tx_hits() -> usize { |
| 1532 | SKIP_SPLIT_TX_HITS.with(|c| c.get()) |
| 1533 | } |
| 1534 | |
| 1535 | /// Zeroes [`SKIP_SPLIT_TX_HITS`] before a gate's own decode attempt. |
| 1536 | pub fn reset_skip_split_tx_hits() { |
| 1537 | SKIP_SPLIT_TX_HITS.with(|c| c.set(0)); |
| 1538 | } |
| 1539 | |
| 1540 | // lane-intrainter r1: intra blocks inside an INTER frame whose `tx_depth` |
| 1541 | // symbol split their luma transform, bucketed by block side (8, 16, 32, 64) -- |
| 1542 | // the gate's proof that the lifted path actually ran, per size. |
| 1543 | thread_local! { |
| 1544 | static INTRA_IN_INTER_SPLIT_TX_HITS: std::cell::Cell<[usize; 4]> = |
| 1545 | const { std::cell::Cell::new([0; 4]) }; |
| 1546 | } |
| 1547 | |
| 1548 | /// Current value of [`INTRA_IN_INTER_SPLIT_TX_HITS`], indexed 8/16/32/64. |
| 1549 | pub(crate) fn intra_in_inter_split_tx_hits() -> [usize; 4] { |
| 1550 | INTRA_IN_INTER_SPLIT_TX_HITS.with(std::cell::Cell::get) |
| 1551 | } |
| 1552 | |
| 1553 | thread_local! { |
| 1554 | /// A just-decoded palette-Y block's own predicted pixels |
| 1555 | /// (`side*side`, row-major, `colors[map[i]]`), consumed by |
| 1556 | /// [`PlaneBuf::reconstruct`]'s next call in place of `predict()` -- the |
| 1557 | /// same off-the-call-stack idiom as [`ENABLE_EDGE_FILTER`] (threading a |
| 1558 | /// per-block override through `read_plane`'s two dozen call sites is the |
| 1559 | /// alternative this avoids). Always empty again once that call consumes |
| 1560 | /// it (`.take()`), so a non-palette block's own `reconstruct` call never |
| 1561 | /// sees stale state. |
| 1562 | static PALETTE_PRED: std::cell::RefCell<Option<Vec<u16>>> = |
| 1563 | const { std::cell::RefCell::new(None) }; |
| 1564 | } |
| 1565 | |
| 1566 | |
| 1567 | /// Arms the pending [`PALETTE_PRED`] override for the next `reconstruct` |
| 1568 | /// (`set`) / consumes it (`take`). Set and take MUST pair: a buffer armed at a |
| 1569 | /// site whose reconstruct never runs would be picked up by the NEXT block's |
| 1570 | /// reconstruct (class [[refusal-hides-a-defect]] / stale thread-local), so |
| 1571 | /// `EC_DEBUG_PAL=1` traces every arm and every consumption with its call site. |
| 1572 | #[track_caller] |
| 1573 | fn set_palette_pred(buf: Vec<u16>) { |
| 1574 | if std::env::var_os("EC_DEBUG_PAL").is_some() { |
| 1575 | let stale = PALETTE_PRED.with(|c| c.borrow().is_some()); |
| 1576 | eprintln!( |
| 1577 | "PALSET len={} stale={stale} at {}", |
| 1578 | buf.len(), |
| 1579 | std::panic::Location::caller() |
| 1580 | ); |
| 1581 | } |
| 1582 | PALETTE_PRED.with(|c| *c.borrow_mut() = Some(buf)); |
| 1583 | } |
| 1584 | |
| 1585 | #[track_caller] |
| 1586 | fn take_palette_pred() -> Option<Vec<u16>> { |
| 1587 | let got = PALETTE_PRED.with(|c| c.borrow_mut().take()); |
| 1588 | if std::env::var_os("EC_DEBUG_PAL").is_some() { |
| 1589 | eprintln!( |
| 1590 | "PALTAKE len={:?} at {}", |
| 1591 | got.as_ref().map(Vec::len), |
| 1592 | std::panic::Location::caller() |
| 1593 | ); |
| 1594 | } |
| 1595 | got |
| 1596 | } |
| 1597 | |
| 1598 | thread_local! { |
| 1599 | /// The current frame's `mi` grid, kept only while a frame header set |
| 1600 | /// `allow_intrabc` -- an intrabc block's DV predictor is the ordinary |
| 1601 | /// MV stack ([`crate::mvstack::find_mv_stack`]) built against |
| 1602 | /// `INTRA_FRAME`, which needs the neighbour state a key frame otherwise |
| 1603 | /// never keeps. `(grid, mi_cols, mi_rows)`; `None` on every frame that |
| 1604 | /// does not allow intrabc (so nothing is paid for it). |
| 1605 | /// |
| 1606 | /// corner-cut: only [`decode_block`] (the square path) records into it, |
| 1607 | /// so a frame mixing intrabc with rect/sub-8x8 leaves would predict off |
| 1608 | /// an incomplete grid. Ceiling: the DV *value* (never the parse) would |
| 1609 | /// be wrong there; upgrade = record from the rect/leaf paths too. |
| 1610 | static INTRABC_MI_GRID: std::cell::RefCell<Option<(crate::mvstack::MiGrid, usize, usize)>> = |
| 1611 | const { std::cell::RefCell::new(None) }; |
| 1612 | /// The block vector [`read_intra_mode`] just decoded, handed to |
| 1613 | /// [`decode_block`] off the call stack (same idiom as [`PALETTE_PRED`], |
| 1614 | /// which the alternative -- a tenth tuple member through every caller -- |
| 1615 | /// already avoids). Always `.take()`n by the block that reads it. |
| 1616 | static INTRABC_DV: std::cell::Cell<Option<(i32, i32)>> = const { std::cell::Cell::new(None) }; |
| 1617 | /// How many blocks decoded `use_intrabc == 1` -- the gate's hit counter. |
| 1618 | static INTRABC_HITS: std::cell::Cell<usize> = const { std::cell::Cell::new(0) }; |
| 1619 | } |
| 1620 | |
| 1621 | /// Whether this frame's header set `allow_intrabc` -- [`INTRABC_MI_GRID`] is |
| 1622 | /// `Some` exactly then (set once per frame in `decode_frame`), so the flag |
| 1623 | /// needs no separate plumbing to reach the rect-strip mode reader. |
| 1624 | fn allow_intrabc_frame() -> bool { |
| 1625 | INTRABC_MI_GRID.with(|g| g.borrow().is_some()) |
| 1626 | } |
| 1627 | |
| 1628 | thread_local! { |
| 1629 | /// How many rect intra strips READ the `use_intrabc` symbol (i.e. how many |
| 1630 | /// times the fix below was exercised) -- the rect-strip gate's hit counter, |
| 1631 | /// distinct from [`INTRABC_HITS`] which counts blocks that USE intrabc. |
| 1632 | static RECT_INTRABC_READS: std::cell::Cell<usize> = const { std::cell::Cell::new(0) }; |
| 1633 | } |
| 1634 | |
| 1635 | /// Current value of [`RECT_INTRABC_READS`]. |
| 1636 | pub(crate) fn rect_intrabc_reads() -> usize { |
| 1637 | RECT_INTRABC_READS.with(|c| c.get()) |
| 1638 | } |
| 1639 | |
| 1640 | /// Zeroes [`RECT_INTRABC_READS`] (gate tests call this before a decode). |
| 1641 | pub(crate) fn reset_rect_intrabc_reads() { |
| 1642 | RECT_INTRABC_READS.with(|c| c.set(0)); |
| 1643 | } |
| 1644 | |
| 1645 | /// Current value of [`INTRABC_HITS`]. |
| 1646 | pub(crate) fn intrabc_hits() -> usize { |
| 1647 | INTRABC_HITS.with(|c| c.get()) |
| 1648 | } |
| 1649 | |
| 1650 | /// Zeroes [`INTRABC_HITS`] (gate tests call this before a decode). |
| 1651 | pub(crate) fn reset_intrabc_hits() { |
| 1652 | INTRABC_HITS.with(|c| c.set(0)); |
| 1653 | } |
| 1654 | |
| 1655 | /// Records one coded block into [`INTRABC_MI_GRID`]: every block contributes |
| 1656 | /// its size (libaom's `scan_row_mbmi` advances `processed_rows` off any |
| 1657 | /// candidate's `bsize`, ref-match or not), an intrabc one also its DV as an |
| 1658 | /// `INTRA_FRAME` candidate (libaom treats intrabc as `is_inter_block`). |
| 1659 | fn record_intrabc_mi(mi_r: usize, mi_c: usize, n4: usize, dv: Option<(i32, i32)>) { |
| 1660 | INTRABC_MI_GRID.with(|g| { |
| 1661 | let mut g = g.borrow_mut(); |
| 1662 | let Some((grid, _, _)) = g.as_mut() else { |
| 1663 | return; |
| 1664 | }; |
| 1665 | let info = crate::mvstack::MiInfo { |
| 1666 | is_inter: dv.is_some(), |
| 1667 | ref_frame: 0, |
| 1668 | ref_frame1: None, |
| 1669 | mv: dv.unwrap_or((0, 0)), |
| 1670 | mv1: None, |
| 1671 | is_new_mv: dv.is_some(), |
| 1672 | is_global_mv0: false, |
| 1673 | is_global_mv1: false, |
| 1674 | size: n4, |
| 1675 | size_h: n4, |
| 1676 | }; |
| 1677 | for r in mi_r..mi_r + n4 { |
| 1678 | for c in mi_c..mi_c + n4 { |
| 1679 | grid.set(r, c, info); |
| 1680 | } |
| 1681 | } |
| 1682 | }); |
| 1683 | } |
| 1684 | |
| 1685 | // How many blocks decoded `skip_mode == 1` (lane-av1comp round 14) -- |
| 1686 | // proves a real `skip_mode_present` stream actually reached a block that |
| 1687 | // picked it, not just that the frame header bit was set. |
| 1688 | thread_local! { |
| 1689 | static SKIP_MODE_HITS: std::cell::Cell<usize> = |
| 1690 | const { std::cell::Cell::new(0) }; |
| 1691 | } |
| 1692 | |
| 1693 | /// Current value of [`SKIP_MODE_HITS`]. |
| 1694 | pub(crate) fn skip_mode_hits() -> usize { |
| 1695 | SKIP_MODE_HITS.with(|c| c.get()) |
| 1696 | } |
| 1697 | |
| 1698 | // lane-motionmode: how many blocks actually decoded `obmc_selected == true` |
| 1699 | // (as opposed to just reaching an eligible `read_motion_mode` symbol read) |
| 1700 | // -- the gate's own proof that a real aomenc `--enable-obmc=1` stream |
| 1701 | // reached [`obmc_blend`], not just the header bit. |
| 1702 | thread_local! { |
| 1703 | static OBMC_HITS: std::cell::Cell<usize> = |
| 1704 | const { std::cell::Cell::new(0) }; |
| 1705 | } |
| 1706 | |
| 1707 | // lane-fimv r1 (class wrong-alphabet-same-value): how many blocks took the |
| 1708 | // 2-symbol `obmc_cdf` alphabet *because* `force_integer_mv` (or a scaled |
| 1709 | // reference) vetoed `WARPED_CAUSAL` in libaom `motion_mode_allowed`, while |
| 1710 | // `allow_warped_motion == 1` and `num_proj_ref >= 1` would otherwise have |
| 1711 | // picked the 3-symbol `motion_mode_cdf`. These are exactly the blocks this |
| 1712 | // decoder used to read with the wrong alphabet, so a gate that asserts this |
| 1713 | // counter advanced proves the fixed path fired (class |
| 1714 | // `gate-blind-to-feature`). |
| 1715 | thread_local! { |
| 1716 | static FIMV_ALPHABET_HITS: std::cell::Cell<usize> = const { std::cell::Cell::new(0) }; |
| 1717 | } |
| 1718 | |
| 1719 | /// Current value of [`FIMV_ALPHABET_HITS`]. |
| 1720 | pub fn fimv_alphabet_hits() -> usize { |
| 1721 | FIMV_ALPHABET_HITS.with(|c| c.get()) |
| 1722 | } |
| 1723 | |
| 1724 | // lane-fimv r1: how many frame headers this decoder has decoded with |
| 1725 | /// `force_integer_mv == 1` (bumped by `stream.rs` per frame). Lets a gate |
| 1726 | // prove the encoder really produced the header state under test rather than |
| 1727 | // assume it. |
| 1728 | thread_local! { |
| 1729 | static FIMV_FRAME_HITS: std::cell::Cell<usize> = const { std::cell::Cell::new(0) }; |
| 1730 | } |
| 1731 | |
| 1732 | /// Current value of [`FIMV_FRAME_HITS`]. |
| 1733 | pub fn fimv_frame_hits() -> usize { |
| 1734 | FIMV_FRAME_HITS.with(|c| c.get()) |
| 1735 | } |
| 1736 | |
| 1737 | /// Bump [`FIMV_FRAME_HITS`] -- called once per decoded frame header whose |
| 1738 | /// `force_integer_mv` bit is set. |
| 1739 | pub fn note_force_integer_mv_frame() { |
| 1740 | FIMV_FRAME_HITS.with(|c| c.set(c.get() + 1)); |
| 1741 | } |
| 1742 | |
| 1743 | /// Current value of [`OBMC_HITS`]. |
| 1744 | pub(crate) fn obmc_hits() -> usize { |
| 1745 | OBMC_HITS.with(|c| c.get()) |
| 1746 | } |
| 1747 | |
| 1748 | // lane-scaledref r1: per-case firing counts for the scaled-reference gate |
| 1749 | // (class `gate-blind-to-feature`) -- a pixel match under an UNSCALED |
| 1750 | // reference would pass just as well and prove nothing, so the gate |
| 1751 | // hard-asserts each combination this round lifts actually occurred: |
| 1752 | // a compound block with a scaled tap, a warp block whose warp libaom |
| 1753 | // suppresses because the reference is scaled, an OBMC block, an interintra |
| 1754 | // block, and an 8x8 leaf. |
| 1755 | thread_local! { |
| 1756 | static SCALED_COMPOUND_HITS: std::cell::Cell<usize> = const { std::cell::Cell::new(0) }; |
| 1757 | static SCALED_WARP_FALLBACK_HITS: std::cell::Cell<usize> = const { std::cell::Cell::new(0) }; |
| 1758 | static SCALED_OBMC_HITS: std::cell::Cell<usize> = const { std::cell::Cell::new(0) }; |
| 1759 | static SCALED_INTERINTRA_HITS: std::cell::Cell<usize> = const { std::cell::Cell::new(0) }; |
| 1760 | static SCALED_BLOCK8_HITS: std::cell::Cell<usize> = const { std::cell::Cell::new(0) }; |
| 1761 | static SCALED_WARP_SUPPRESSED_HITS: std::cell::Cell<usize> = const { std::cell::Cell::new(0) }; |
| 1762 | static MIXED_SCALE_COMPOUND_HITS: std::cell::Cell<usize> = const { std::cell::Cell::new(0) }; |
| 1763 | } |
| 1764 | |
| 1765 | /// Current value of [`MIXED_SCALE_COMPOUND_HITS`]: compound blocks with one |
| 1766 | /// scaled and one unscaled tap. |
| 1767 | pub(crate) fn mixed_scale_compound_hits() -> usize { |
| 1768 | MIXED_SCALE_COMPOUND_HITS.with(|c| c.get()) |
| 1769 | } |
| 1770 | |
| 1771 | /// Current value of [`SCALED_WARP_SUPPRESSED_HITS`]: blocks that would have |
| 1772 | /// read the 3-symbol `motion_mode_cdf` but read the 2-symbol `obmc_cdf` |
| 1773 | /// instead because their reference is scaled (libaom `motion_mode_allowed`, |
| 1774 | /// `blockd.h:1484`). |
| 1775 | pub(crate) fn scaled_warp_suppressed_hits() -> usize { |
| 1776 | SCALED_WARP_SUPPRESSED_HITS.with(|c| c.get()) |
| 1777 | } |
| 1778 | |
| 1779 | /// Current value of [`SCALED_COMPOUND_HITS`]. |
| 1780 | pub(crate) fn scaled_compound_hits() -> usize { |
| 1781 | SCALED_COMPOUND_HITS.with(|c| c.get()) |
| 1782 | } |
| 1783 | |
| 1784 | /// Current value of [`SCALED_WARP_FALLBACK_HITS`]. |
| 1785 | pub(crate) fn scaled_warp_fallback_hits() -> usize { |
| 1786 | SCALED_WARP_FALLBACK_HITS.with(|c| c.get()) |
| 1787 | } |
| 1788 | |
| 1789 | /// Current value of [`SCALED_OBMC_HITS`]. |
| 1790 | pub(crate) fn scaled_obmc_hits() -> usize { |
| 1791 | SCALED_OBMC_HITS.with(|c| c.get()) |
| 1792 | } |
| 1793 | |
| 1794 | /// Current value of [`SCALED_INTERINTRA_HITS`]. |
| 1795 | pub(crate) fn scaled_interintra_hits() -> usize { |
| 1796 | SCALED_INTERINTRA_HITS.with(|c| c.get()) |
| 1797 | } |
| 1798 | |
| 1799 | /// Current value of [`SCALED_BLOCK8_HITS`]. |
| 1800 | pub(crate) fn scaled_block8_hits() -> usize { |
| 1801 | SCALED_BLOCK8_HITS.with(|c| c.get()) |
| 1802 | } |
| 1803 | |
| 1804 | // lane-motionmode round 3: same proof as [`OBMC_HITS`], narrowed to |
| 1805 | // `decode_inter_block8`'s own 8x8-leaf `read_motion_mode` -- a subset of |
| 1806 | // [`OBMC_HITS`] (both fire together on an 8x8 hit), lets the gate tell an |
| 1807 | // 8x8-leaf OBMC block apart from a 16x16+ one. |
| 1808 | thread_local! { |
| 1809 | static OBMC_HITS_8: std::cell::Cell<usize> = const { std::cell::Cell::new(0) }; |
| 1810 | } |
| 1811 | |
| 1812 | // lane-warp round 1: how many blocks resolved `motion_mode_allowed` to |
| 1813 | // `WARPED_CAUSAL`-eligible (3-symbol read, `num_proj_ref >= 1` under |
| 1814 | // `allow_warped_motion`) AND the symbol actually decoded to `WARPED_CAUSAL` |
| 1815 | // -- the gate's proof that a fixture really exercises the alphabet this |
| 1816 | // round changed, even though the block itself still refuses (warp |
| 1817 | // estimation/filter not ported). |
| 1818 | thread_local! { |
| 1819 | static WARP_SELECTED_HITS: std::cell::Cell<usize> = |
| 1820 | const { std::cell::Cell::new(0) }; |
| 1821 | } |
| 1822 | |
| 1823 | /// Current value of [`WARP_SELECTED_HITS`]. |
| 1824 | pub(crate) fn warp_selected_hits() -> usize { |
| 1825 | WARP_SELECTED_HITS.with(|c| c.get()) |
| 1826 | } |
| 1827 | |
| 1828 | // lane-cwarp r1: how many COMPOUND_REFERENCE blocks had at least one |
| 1829 | // reference predicted through the per-ref GLOBAL warp (`av1_warp_plane` |
| 1830 | // into the compound intermediate) rather than translationally -- the |
| 1831 | // gate's proof that a `GLOBAL_GLOBALMV` block under a ROTZOOM/AFFINE model |
| 1832 | // really reached [`crate::warp::warp_affine_compound`]. |
| 1833 | thread_local! { |
| 1834 | static COMPOUND_WARP_HITS: std::cell::Cell<usize> = |
| 1835 | const { std::cell::Cell::new(0) }; |
| 1836 | } |
| 1837 | |
| 1838 | /// Current value of [`COMPOUND_WARP_HITS`]. |
| 1839 | pub(crate) fn compound_warp_hits() -> usize { |
| 1840 | COMPOUND_WARP_HITS.with(|c| c.get()) |
| 1841 | } |
| 1842 | |
| 1843 | // lane-gmaffine r1: how many blocks built their prediction from a |
| 1844 | // SIX-parameter (`AFFINE`) global-motion model through |
| 1845 | // `crate::warp::global_warp_params` -- the gate's proof that a stream really |
| 1846 | // carried an AFFINE `global_motion_params` AND that a block was predicted |
| 1847 | // with it, not merely that the header parsed one. |
| 1848 | thread_local! { |
| 1849 | static AFFINE_GM_HITS: std::cell::Cell<usize> = |
| 1850 | const { std::cell::Cell::new(0) }; |
| 1851 | } |
| 1852 | |
| 1853 | /// Current value of [`AFFINE_GM_HITS`]. |
| 1854 | pub(crate) fn affine_gm_hits() -> usize { |
| 1855 | AFFINE_GM_HITS.with(|c| c.get()) |
| 1856 | } |
| 1857 | |
| 1858 | // lane-gmaffine r1: 8x8-leaf-only counters -- how many `BLOCK_8X8` leaves |
| 1859 | // coded `GLOBALMV` (`GLOBALMV_HITS_8`) and how many built a real |
| 1860 | // `WARPED_CAUSAL` local-warp prediction there (`WARP_HITS_8`, incremented |
| 1861 | // only once `find_projection` returned a usable model, unlike |
| 1862 | // `WARP_SELECTED_HITS` which counts the symbol). Both are the 8x8 gates' |
| 1863 | // proof that the leaf path itself fired, not the 16x16+ one. |
| 1864 | thread_local! { |
| 1865 | static GLOBALMV_HITS_8: std::cell::Cell<usize> = const { std::cell::Cell::new(0) }; |
| 1866 | static WARP_HITS_8: std::cell::Cell<usize> = const { std::cell::Cell::new(0) }; |
| 1867 | } |
| 1868 | |
| 1869 | /// Current value of [`GLOBALMV_HITS_8`]. |
| 1870 | pub(crate) fn globalmv_hits_8() -> usize { |
| 1871 | GLOBALMV_HITS_8.with(|c| c.get()) |
| 1872 | } |
| 1873 | |
| 1874 | /// Current value of [`WARP_HITS_8`]. |
| 1875 | pub(crate) fn warp_hits_8() -> usize { |
| 1876 | WARP_HITS_8.with(|c| c.get()) |
| 1877 | } |
| 1878 | |
| 1879 | // How many blocks decoded `interintra == 1` with the non-wedge blended |
| 1880 | // prediction applied (lane-interintra r1) -- the gate's proof that a |
| 1881 | // stream actually exercised interintra. |
| 1882 | thread_local! { |
| 1883 | static INTERINTRA_HITS: std::cell::Cell<usize> = |
| 1884 | const { std::cell::Cell::new(0) }; |
| 1885 | } |
| 1886 | |
| 1887 | /// Current value of [`INTERINTRA_HITS`]. |
| 1888 | pub(crate) fn interintra_hits() -> usize { |
| 1889 | INTERINTRA_HITS.with(|c| c.get()) |
| 1890 | } |
| 1891 | |
| 1892 | // How many blocks decoded `comp_group_idx == 1` (masked COMPOUND_REFERENCE, |
| 1893 | // wedge or diffwtd) and had `compound_type`/`wedge_idx`/`wedge_sign`/ |
| 1894 | // `mask_type` consumed entropy-exact -- lane-maskcomp r1's proof that a |
| 1895 | // gate fixture actually exercises this alphabet, even though the block |
| 1896 | // still refuses (the mask blend itself is unported). |
| 1897 | thread_local! { |
| 1898 | static MASKED_COMPOUND_HITS: std::cell::Cell<usize> = |
| 1899 | const { std::cell::Cell::new(0) }; |
| 1900 | } |
| 1901 | |
| 1902 | /// Current value of [`MASKED_COMPOUND_HITS`]. |
| 1903 | pub(crate) fn masked_compound_hits() -> usize { |
| 1904 | MASKED_COMPOUND_HITS.with(|c| c.get()) |
| 1905 | } |
| 1906 | |
| 1907 | // How many blocks decoded a real `COMPOUND_WEDGE` block (`comp_group_idx == |
| 1908 | // 1`, `compound_type == 0`) -- lane-wedge r3's proof a gate fixture |
| 1909 | // actually exercised the wedge blend, not just the DIFFWTD half of |
| 1910 | // `MASKED_COMPOUND_HITS`. |
| 1911 | thread_local! { |
| 1912 | static WEDGE_HITS: std::cell::Cell<usize> = |
| 1913 | const { std::cell::Cell::new(0) }; |
| 1914 | } |
| 1915 | |
| 1916 | /// Current value of [`WEDGE_HITS`]. |
| 1917 | pub(crate) fn wedge_hits() -> usize { |
| 1918 | WEDGE_HITS.with(|c| c.get()) |
| 1919 | } |
| 1920 | |
| 1921 | // How many blocks decoded a real wedge-INTERINTRA block |
| 1922 | // (`interintra == 1 && use_wedge_interintra == 1`) -- lane-wii r2's proof |
| 1923 | // a gate fixture exercised the wedge blend (fixed sign 0), not just the |
| 1924 | // smooth arm counted by `INTERINTRA_HITS`. |
| 1925 | thread_local! { |
| 1926 | static WII_HITS: std::cell::Cell<usize> = |
| 1927 | const { std::cell::Cell::new(0) }; |
| 1928 | } |
| 1929 | |
| 1930 | /// Current value of [`WII_HITS`]. |
| 1931 | pub(crate) fn wii_hits() -> usize { |
| 1932 | WII_HITS.with(|c| c.get()) |
| 1933 | } |
| 1934 | |
| 1935 | // lane-sbrect10 r1: how many INTRA blocks read `uv_mode` off the 13-symbol |
| 1936 | // NO-CFL alphabet *because their size excludes CFL* (libaom `is_cfl_allowed`: |
| 1937 | // wide <= 32 && high <= 32). The witness that a gate stream really carries a |
| 1938 | // 64-axis intra block -- the shape whose 14-symbol read desynced every inter |
| 1939 | // frame carrying one. |
| 1940 | thread_local! { |
| 1941 | static NOCFL_UV_MODE_HITS: std::cell::Cell<usize> = const { std::cell::Cell::new(0) }; |
| 1942 | } |
| 1943 | |
| 1944 | /// Current value of [`NOCFL_UV_MODE_HITS`]. |
| 1945 | pub(crate) fn nocfl_uv_mode_hits() -> usize { |
| 1946 | NOCFL_UV_MODE_HITS.with(|c| c.get()) |
| 1947 | } |
| 1948 | |
| 1949 | // lane-inter4 r4: how many 16x8/8x16 INTER LEAVES actually ran the OBMC |
| 1950 | // blend -- the gate's proof that `--enable-obmc=1` reached this shape (the |
| 1951 | // r3 refusal's replacement: prefilt frames are bit-exact vs an instrumented |
| 1952 | // aomdec, neighbour list included). |
| 1953 | thread_local! { |
| 1954 | static OBMC_RECT_LEAF_HITS: std::cell::Cell<usize> = const { std::cell::Cell::new(0) }; |
| 1955 | } |
| 1956 | |
| 1957 | /// Current value of [`OBMC_RECT_LEAF_HITS`]. |
| 1958 | pub(crate) fn obmc_rect_leaf_hits() -> usize { |
| 1959 | OBMC_RECT_LEAF_HITS.with(|c| c.get()) |
| 1960 | } |
| 1961 | |
| 1962 | /// Current value of [`OBMC_HITS_8`]. |
| 1963 | pub(crate) fn obmc_hits_8() -> usize { |
| 1964 | OBMC_HITS_8.with(|c| c.get()) |
| 1965 | } |
| 1966 | |
| 1967 | // lane-rectgate r1: how many 32x32 quadrants actually decoded a |
| 1968 | // `PARTITION_HORZ_B` split (the one extended/ab partition this decoder's |
| 1969 | // inter-frame tile loop decodes -- `PARTITION_HORZ`/`VERT`/`HORZ_A`/`VERT_A`/ |
| 1970 | // `VERT_B` all still fall into the generic "a partition type this encoder |
| 1971 | // never writes" refusal below) -- the gate's proof that a free-partition |
| 1972 | // aomenc stream actually exercised a non-`NONE`/`SPLIT` partition rather than |
| 1973 | // coincidentally never selecting one. |
| 1974 | thread_local! { |
| 1975 | static EXTENDED_PARTITION_HITS: std::cell::Cell<usize> = |
| 1976 | const { std::cell::Cell::new(0) }; |
| 1977 | } |
| 1978 | |
| 1979 | /// Current value of [`EXTENDED_PARTITION_HITS`]. |
| 1980 | pub(crate) fn extended_partition_hits() -> usize { |
| 1981 | EXTENDED_PARTITION_HITS.with(|c| c.get()) |
| 1982 | } |
| 1983 | |
| 1984 | // lane-partitions r1: how many 32x32 quadrants decoded a real |
| 1985 | // `PARTITION_HORZ`/`PARTITION_VERT` (two true rectangular strips, unlike |
| 1986 | // `PARTITION_HORZ_B`'s square-context stand-in) -- the free-partition |
| 1987 | // gate's proof this round's arms actually fired. |
| 1988 | thread_local! { |
| 1989 | static RECT_PARTITION_HITS: std::cell::Cell<usize> = |
| 1990 | const { std::cell::Cell::new(0) }; |
| 1991 | } |
| 1992 | |
| 1993 | /// Current value of [`RECT_PARTITION_HITS`]. |
| 1994 | pub(crate) fn rect_partition_hits() -> usize { |
| 1995 | RECT_PARTITION_HITS.with(|c| c.get()) |
| 1996 | } |
| 1997 | |
| 1998 | // lane-inter8 r1: how many superblocks an inter tile coded as a single |
| 1999 | // whole-SB `PARTITION_NONE` 64x64 inter block (the case aomenc picks for |
| 2000 | // static content, previously a named refusal). |
| 2001 | thread_local! { |
| 2002 | static INTER_SB_NONE_HITS: std::cell::Cell<usize> = |
| 2003 | const { std::cell::Cell::new(0) }; |
| 2004 | } |
| 2005 | |
| 2006 | /// Current value of [`INTER_SB_NONE_HITS`]. |
| 2007 | pub(crate) fn inter_sb_none_hits() -> usize { |
| 2008 | INTER_SB_NONE_HITS.with(|c| c.get()) |
| 2009 | } |
| 2010 | |
| 2011 | // lane-inter8 r1: how many INTERIOR (not frame-edge-straddling) 16x16 inter |
| 2012 | // blocks coded a real `PARTITION_SPLIT` into four 8x8 leaves -- the straddle |
| 2013 | // path has always run that loop, so only this counter proves the newly-lifted |
| 2014 | // alphabet value fired. |
| 2015 | thread_local! { |
| 2016 | static INTER_SUB16_SPLIT_HITS: std::cell::Cell<usize> = |
| 2017 | const { std::cell::Cell::new(0) }; |
| 2018 | } |
| 2019 | |
| 2020 | /// Current value of [`INTER_SUB16_SPLIT_HITS`]. |
| 2021 | pub(crate) fn inter_sub16_split_hits() -> usize { |
| 2022 | INTER_SUB16_SPLIT_HITS.with(|c| c.get()) |
| 2023 | } |
| 2024 | |
| 2025 | // lane-rectwire r2: how many `PARTITION_HORZ`/`VERT` strips actually decoded |
| 2026 | // real (non-skip) coefficients through [`read_coeffs_rect`] -- proves the |
| 2027 | // rect coefficient reader itself fired, not just the strip-level partition |
| 2028 | // symbol [`RECT_PARTITION_HITS`] already counts. |
| 2029 | thread_local! { |
| 2030 | static RECT_COEFF_HITS: std::cell::Cell<usize> = |
| 2031 | const { std::cell::Cell::new(0) }; |
| 2032 | } |
| 2033 | |
| 2034 | /// Current value of [`RECT_COEFF_HITS`]. |
| 2035 | pub(crate) fn rect_coeff_hits() -> usize { |
| 2036 | RECT_COEFF_HITS.with(|c| c.get()) |
| 2037 | } |
| 2038 | |
| 2039 | // lane-rectx r1: how many below-16x16 HORZ/VERT leaves ([`decode_leaf_rect`]) |
| 2040 | // decoded real (non-skip) TX_16X8/TX_8X16 coefficients -- the counter the |
| 2041 | // charter's real-aomenc gate hard-asserts. |
| 2042 | thread_local! { |
| 2043 | static RECT_LEAF_COEFF_HITS: std::cell::Cell<usize> = |
| 2044 | const { std::cell::Cell::new(0) }; |
| 2045 | } |
| 2046 | |
| 2047 | /// Current value of [`RECT_LEAF_COEFF_HITS`]. |
| 2048 | pub(crate) fn rect_leaf_coeff_hits() -> usize { |
| 2049 | RECT_LEAF_COEFF_HITS.with(|c| c.get()) |
| 2050 | } |
| 2051 | |
| 2052 | // lane-intrarect r1: how many intra-coded HORZ/VERT strips decoded on the |
| 2053 | // INTER block path ([`decode_intra_rect_in_inter`]), per shape class -- |
| 2054 | // [0] the superblock-level 64x32/32x64, [1] 32x16/16x32, [2] 16x8/8x16. |
| 2055 | // Every one of them was the named refusal "an intra-coded HORZ/VERT strip |
| 2056 | // needs rectangular intra prediction" before this round. |
| 2057 | thread_local! { |
| 2058 | static INTRA_RECT_IN_INTER_HITS: std::cell::Cell<[usize; 3]> = |
| 2059 | const { std::cell::Cell::new([0; 3]) }; |
| 2060 | } |
| 2061 | |
| 2062 | /// Current value of [`INTRA_RECT_IN_INTER_HITS`] for one shape class |
| 2063 | /// (0 = 64x32/32x64, 1 = 32x16/16x32, 2 = 16x8/8x16). |
| 2064 | pub(crate) fn intra_rect_strip_in_inter_hits(class: usize) -> usize { |
| 2065 | INTRA_RECT_IN_INTER_HITS.with(|c| c.get()[class]) |
| 2066 | } |
| 2067 | |
| 2068 | // lane-rectx r5: how many times a NON-strip `kf_y_mode` reader |
| 2069 | // ([`decode_block`], [`decode_block_rect`], [`decode_block_rect64`]) took an |
| 2070 | // above/left mode from the mi-exact map that DIFFERS from its coarse 16x16 |
| 2071 | // slot -- i.e. how often the r5 defect would have picked the wrong CDF row. |
| 2072 | thread_local! { |
| 2073 | static MODE_MI_OVERRIDE_HITS: std::cell::Cell<usize> = |
| 2074 | const { std::cell::Cell::new(0) }; |
| 2075 | } |
| 2076 | |
| 2077 | /// Current value of [`MODE_MI_OVERRIDE_HITS`]. |
| 2078 | pub(crate) fn mode_mi_override_hits() -> usize { |
| 2079 | MODE_MI_OVERRIDE_HITS.with(|c| c.get()) |
| 2080 | } |
| 2081 | |
| 2082 | // lane-modectx r1: how many times a `kf_y_mode`/`y_mode` neighbour read at a |
| 2083 | // TILE edge found its coarse fallback band holding a NON-DC mode -- i.e. a |
| 2084 | // mode from the previous tile, where libaom's `up_available`/`left_available` |
| 2085 | // make the neighbour DC_PRED. Measured 0 over 20 two-tile-column streams: |
| 2086 | // `above_mode` is cleared per tile in `Neighbours::start_tile` and `left_mode` |
| 2087 | // per superblock row in `start_row`, so the luma fallback cannot leak the way |
| 2088 | // lane-mtfix's `above_uv_mode` one did. The gate asserts it stays 0 -- this is |
| 2089 | // the invariant that makes the coarse fallback safe. |
| 2090 | thread_local! { |
| 2091 | static MODE_TILE_EDGE_COARSE_LEAKS: std::cell::Cell<usize> = |
| 2092 | const { std::cell::Cell::new(0) }; |
| 2093 | } |
| 2094 | |
| 2095 | /// Current value of [`MODE_TILE_EDGE_COARSE_LEAKS`]. |
| 2096 | pub(crate) fn mode_tile_edge_coarse_leaks() -> usize { |
| 2097 | MODE_TILE_EDGE_COARSE_LEAKS.with(|c| c.get()) |
| 2098 | } |
| 2099 | |
| 2100 | // lane-cfl r1: how many times a chroma intra-edge filter-type read |
| 2101 | // (`get_filt_type`, libaom reconintra.c) took an above/left UV mode from the |
| 2102 | // mi-exact map that DIFFERS from its coarse 16x16 slot -- i.e. how often the |
| 2103 | // r1 defect would have filtered a chroma edge with the wrong strength. |
| 2104 | thread_local! { |
| 2105 | static UV_MODE_MI_OVERRIDE_HITS: std::cell::Cell<usize> = |
| 2106 | const { std::cell::Cell::new(0) }; |
| 2107 | } |
| 2108 | |
| 2109 | /// Current value of [`UV_MODE_MI_OVERRIDE_HITS`]. |
| 2110 | pub(crate) fn uv_mode_mi_override_hits() -> usize { |
| 2111 | UV_MODE_MI_OVERRIDE_HITS.with(|c| c.get()) |
| 2112 | } |
| 2113 | |
| 2114 | // lane-mtfix r1: how many times a chroma intra-edge filter-type read was |
| 2115 | // suppressed at a TILE edge that is not a frame edge -- the above/left |
| 2116 | // `uv_mode` neighbour of a block on a tile's first mi row/column lives in |
| 2117 | // another tile, so libaom's `above_mbmi`/`left_mbmi` are NULL there. The |
| 2118 | // coarse [`SUB`] maps are frame-sized and (`above_uv_mode`) not cleared per |
| 2119 | // tile, so without the guard in [`Neighbours::smooth_uv_neighbour`] the tile |
| 2120 | // above would decide this tile's chroma edge filter strength. |
| 2121 | thread_local! { |
| 2122 | static UV_TILE_EDGE_SUPPRESSED_HITS: std::cell::Cell<usize> = |
| 2123 | const { std::cell::Cell::new(0) }; |
| 2124 | } |
| 2125 | |
| 2126 | /// Current value of [`UV_TILE_EDGE_SUPPRESSED_HITS`]. |
| 2127 | pub(crate) fn uv_tile_edge_suppressed_hits() -> usize { |
| 2128 | UV_TILE_EDGE_SUPPRESSED_HITS.with(|c| c.get()) |
| 2129 | } |
| 2130 | |
| 2131 | // lane-cfl r1 gate counters: `UV_CFL_PRED` blocks (every `cfl_alpha_signs` |
| 2132 | // read) and chroma blocks with a NONZERO `angle_delta_uv` -- the two block |
| 2133 | // shapes the r1 chroma defect showed up on. |
| 2134 | thread_local! { |
| 2135 | static CFL_BLOCK_HITS: std::cell::Cell<usize> = const { std::cell::Cell::new(0) }; |
| 2136 | static UV_ANGLE_DELTA_HITS: std::cell::Cell<usize> = const { std::cell::Cell::new(0) }; |
| 2137 | } |
| 2138 | |
| 2139 | /// Current value of [`CFL_BLOCK_HITS`]. |
| 2140 | pub(crate) fn cfl_block_hits() -> usize { |
| 2141 | CFL_BLOCK_HITS.with(|c| c.get()) |
| 2142 | } |
| 2143 | |
| 2144 | /// Current value of [`UV_ANGLE_DELTA_HITS`]. |
| 2145 | pub(crate) fn uv_angle_delta_hits() -> usize { |
| 2146 | UV_ANGLE_DELTA_HITS.with(|c| c.get()) |
| 2147 | } |
| 2148 | |
| 2149 | // lane-sbpart r2: how many superblock-level `PARTITION_HORZ`/`PARTITION_VERT` |
| 2150 | // blocks (two true 64x32/32x64 strips, [`decode_block_rect64`]) fired -- the |
| 2151 | // gate's proof this round's arms actually reached a real block, not just |
| 2152 | // that `partition_w64` read one of those two symbol values. |
| 2153 | thread_local! { |
| 2154 | static SB_RECT_HITS: std::cell::Cell<usize> = |
| 2155 | const { std::cell::Cell::new(0) }; |
| 2156 | } |
| 2157 | |
| 2158 | /// Current value of [`SB_RECT_HITS`]. |
| 2159 | // lane-hgkf r1: the frame-edge partition bit, one level each. Slot 0 = a |
| 2160 | // 64-level gathered edge bit read as HORZ/VERT, 1 = the same bit read as |
| 2161 | // SPLIT, 2/3 = the 32-level pair. A gate asserting slot 0 or 2 proves the |
| 2162 | // non-SPLIT edge arm actually decoded (before this round the bit was read and |
| 2163 | // thrown away, so that arm was unreachable). |
| 2164 | thread_local! { |
| 2165 | static EDGE_PART_HITS: std::cell::Cell<[usize; 4]> = const { std::cell::Cell::new([0; 4]) }; |
| 2166 | } |
| 2167 | |
| 2168 | /// Current value of [`EDGE_PART_HITS`]. |
| 2169 | pub(crate) fn edge_part_hits() -> [usize; 4] { |
| 2170 | EDGE_PART_HITS.with(|c| c.get()) |
| 2171 | } |
| 2172 | |
| 2173 | /// Reset [`EDGE_PART_HITS`] (a gate counts one stream at a time). |
| 2174 | pub(crate) fn reset_edge_part_hits() { |
| 2175 | EDGE_PART_HITS.with(|c| c.set([0; 4])); |
| 2176 | } |
| 2177 | |
| 2178 | fn bump_edge_part(slot: usize) { |
| 2179 | EDGE_PART_HITS.with(|c| { |
| 2180 | let mut h = c.get(); |
| 2181 | h[slot] += 1; |
| 2182 | c.set(h); |
| 2183 | }); |
| 2184 | } |
| 2185 | |
| 2186 | pub(crate) fn sb_rect_hits() -> usize { |
| 2187 | SB_RECT_HITS.with(|c| c.get()) |
| 2188 | } |
| 2189 | |
| 2190 | // lane-golomb r2: the 32x32-level FRAME-EDGE partition. Slot 0/1 = the |
| 2191 | // gathered edge bit read as 0 (HORZ/VERT) / 1 (SPLIT); slot 2 = a 32-level |
| 2192 | // PARTITION_HORZ decoded with `has_rows32 == false` (only the top 32x16 half |
| 2193 | // is coded), slot 3 = a PARTITION_VERT with `has_cols32 == false`. Both tile |
| 2194 | // paths (intra and inter) feed the same counter. |
| 2195 | // |
| 2196 | // lane-golomb r3: slots 4..7 are the same four facts one level up, at the |
| 2197 | // SUPERBLOCK (64x64) edge -- 4/5 = the gathered 64-level bit read as |
| 2198 | // HORZ/VERT / SPLIT, 6 = a 64x32 bottom-edge strip decoded with |
| 2199 | // `has_rows == false`, 7 = a 32x64 right-edge strip. Real aomenc answers a |
| 2200 | // bottom-edge superblock with the 64-level HORZ (measured: 192x80 flat-band, |
| 2201 | // every SB of the last row), so the 32-level slots stay 0 on that content and |
| 2202 | // only these prove the arm fired. |
| 2203 | thread_local! { |
| 2204 | static EDGE32_HITS: std::cell::Cell<[usize; 8]> = |
| 2205 | const { std::cell::Cell::new([0; 8]) }; |
| 2206 | } |
| 2207 | |
| 2208 | /// Current value of [`EDGE32_HITS`]. |
| 2209 | pub(crate) fn edge32_hits() -> [usize; 8] { |
| 2210 | EDGE32_HITS.with(|c| c.get()) |
| 2211 | } |
| 2212 | |
| 2213 | /// [`bump_edge32`] of `base + bit`, returning the bit -- the gathered edge |
| 2214 | /// reads are `if`-conditions, so the counter has to ride the expression. |
| 2215 | fn bump_edge32_bit(base: usize, bit: usize) -> usize { |
| 2216 | bump_edge32(base + bit.min(1)); |
| 2217 | bit |
| 2218 | } |
| 2219 | |
| 2220 | fn bump_edge32(slot: usize) { |
| 2221 | EDGE32_HITS.with(|c| { |
| 2222 | let mut h = c.get(); |
| 2223 | h[slot] += 1; |
| 2224 | c.set(h) |
| 2225 | }); |
| 2226 | } |
| 2227 | |
| 2228 | // lane-part32 r4: how many superblock-level AB blocks (`PARTITION_HORZ_A`/ |
| 2229 | // `_B`/`VERT_A`/`_B` at 64x64 -- two 32x32 squares plus one 64x32/32x64 |
| 2230 | // strip) fired. Real aomenc picks these at 64 even with |
| 2231 | // `--enable-ab-partitions=0` (lane-sbpart r2's own measurement), so this is |
| 2232 | // the counter the gate hard-asserts. |
| 2233 | thread_local! { |
| 2234 | static SB_AB_HITS: std::cell::Cell<[usize; 4]> = |
| 2235 | const { std::cell::Cell::new([0; 4]) }; |
| 2236 | } |
| 2237 | |
| 2238 | /// Current value of [`SB_AB_HITS`], summed over the four arms. |
| 2239 | pub(crate) fn sb_ab_hits() -> usize { |
| 2240 | SB_AB_HITS.with(|c| c.get().iter().sum()) |
| 2241 | } |
| 2242 | |
| 2243 | /// [`SB_AB_HITS`] split per arm, in `PARTITION_HORZ_A`, `_HORZ_B`, |
| 2244 | /// `_VERT_A`, `_VERT_B` order -- what the gate hard-asserts arm by arm, so a |
| 2245 | /// run where aomenc's RD only ever picked (say) `HORZ_A` cannot pass off as |
| 2246 | /// proof of all four. |
| 2247 | pub(crate) fn sb_ab_hits_by_arm() -> [usize; 4] { |
| 2248 | SB_AB_HITS.with(std::cell::Cell::get) |
| 2249 | } |
| 2250 | |
| 2251 | // lane-tx64x16: the two 1:4 superblock arms, counted per orientation -- |
| 2252 | // `PARTITION_HORZ_4` (four 64x16 strips) and `PARTITION_VERT_4` (four 16x64 |
| 2253 | // strips). Two counters, not one, because a gate that only proved "some 1:4 |
| 2254 | // strip fired" would pass on a stream that never coded the other axis, and |
| 2255 | // every scan/context table this round added comes in a transposed pair. |
| 2256 | thread_local! { |
| 2257 | static SB_RECT4_HORZ_HITS: std::cell::Cell<usize> = const { std::cell::Cell::new(0) }; |
| 2258 | static SB_RECT4_VERT_HITS: std::cell::Cell<usize> = const { std::cell::Cell::new(0) }; |
| 2259 | } |
| 2260 | |
| 2261 | /// Current value of `SB_RECT4_HORZ_HITS` (64x16 strips decoded). |
| 2262 | pub(crate) fn sb_rect4_horz_hits() -> usize { |
| 2263 | SB_RECT4_HORZ_HITS.with(|c| c.get()) |
| 2264 | } |
| 2265 | |
| 2266 | /// Current value of `SB_RECT4_VERT_HITS` (16x64 strips decoded). |
| 2267 | pub(crate) fn sb_rect4_vert_hits() -> usize { |
| 2268 | SB_RECT4_VERT_HITS.with(|c| c.get()) |
| 2269 | } |
| 2270 | |
| 2271 | // lane-tx64x16 r3: the 32x32-level 1:4 arms, counted per orientation -- |
| 2272 | // `PARTITION_HORZ_4` (four 32x8 strips) and `PARTITION_VERT_4` (four 8x32 |
| 2273 | // strips), the arms the ReelB mid-film extract stops at. |
| 2274 | thread_local! { |
| 2275 | static RECT4_32_HORZ_HITS: std::cell::Cell<usize> = const { std::cell::Cell::new(0) }; |
| 2276 | static RECT4_32_VERT_HITS: std::cell::Cell<usize> = const { std::cell::Cell::new(0) }; |
| 2277 | /// lane-inter4 r1: the INTER-frame twins of the two counters above -- |
| 2278 | /// four 32x8 / 8x32 inter strips of a 32x32-level `PARTITION_HORZ_4` / |
| 2279 | /// `PARTITION_VERT_4`. |
| 2280 | static RECT4_32_HORZ_INTER_HITS: std::cell::Cell<usize> = const { std::cell::Cell::new(0) }; |
| 2281 | static RECT4_32_VERT_INTER_HITS: std::cell::Cell<usize> = const { std::cell::Cell::new(0) }; |
| 2282 | /// lane-inter4 r1: superblock-level inter `PARTITION_HORZ`/`PARTITION_VERT` |
| 2283 | /// strips (64x32 / 32x64) and their 1:4 pair (64x16 / 16x64). |
| 2284 | static INTER_SB_HORZ_HITS: std::cell::Cell<usize> = const { std::cell::Cell::new(0) }; |
| 2285 | static INTER_SB_VERT_HITS: std::cell::Cell<usize> = const { std::cell::Cell::new(0) }; |
| 2286 | static INTER_SB_HORZ4_HITS: std::cell::Cell<usize> = const { std::cell::Cell::new(0) }; |
| 2287 | static INTER_SB_VERT4_HITS: std::cell::Cell<usize> = const { std::cell::Cell::new(0) }; |
| 2288 | } |
| 2289 | |
| 2290 | /// How many 32x32-level 1:4 strips actually read coefficients (non-skip): |
| 2291 | /// a gate that only counted strips would pass on an all-skip stream, which |
| 2292 | /// exercises none of the new scan/context tables. |
| 2293 | thread_local! { |
| 2294 | static RECT4_COEFF_HITS: std::cell::Cell<usize> = const { std::cell::Cell::new(0) }; |
| 2295 | } |
| 2296 | |
| 2297 | // lane-rectsplitx r1: 1:4 strips (32x8/8x32/64x16/16x64) decoded through the |
| 2298 | // per-transform-unit split path, split by tx depth -- depth 1 is a 2:1 RECT |
| 2299 | // unit (TX_16X8/TX_32X16), depth 2 a square one, and the two INTERLEAVE in |
| 2300 | // every real stream, so a gate must prove both fired. `SUB16_SPLIT_HITS` |
| 2301 | // counts the same for a 16x8/8x16 strip below 16x16. |
| 2302 | thread_local! { |
| 2303 | static RECT4_SPLIT_DEPTH1_HITS: std::cell::Cell<usize> = const { std::cell::Cell::new(0) }; |
| 2304 | static RECT4_SPLIT_DEPTH2_HITS: std::cell::Cell<usize> = const { std::cell::Cell::new(0) }; |
| 2305 | static SUB16_SPLIT_HITS: std::cell::Cell<usize> = const { std::cell::Cell::new(0) }; |
| 2306 | } |
| 2307 | |
| 2308 | /// Current values of `RECT4_SPLIT_DEPTH1_HITS` / `RECT4_SPLIT_DEPTH2_HITS`. |
| 2309 | pub(crate) fn rect4_split_depth_hits() -> (usize, usize) { |
| 2310 | ( |
| 2311 | RECT4_SPLIT_DEPTH1_HITS.with(|c| c.get()), |
| 2312 | RECT4_SPLIT_DEPTH2_HITS.with(|c| c.get()), |
| 2313 | ) |
| 2314 | } |
| 2315 | |
| 2316 | /// Current value of `SUB16_SPLIT_HITS`. |
| 2317 | pub(crate) fn sub16_split_hits() -> usize { |
| 2318 | SUB16_SPLIT_HITS.with(|c| c.get()) |
| 2319 | } |
| 2320 | |
| 2321 | /// Current value of `RECT4_COEFF_HITS`. |
| 2322 | pub(crate) fn rect4_coeff_hits() -> usize { |
| 2323 | RECT4_COEFF_HITS.with(|c| c.get()) |
| 2324 | } |
| 2325 | |
| 2326 | // lane-inter16ab r1: how many 16x16-level AB partitions fired IN AN INTER |
| 2327 | // frame, per arm, in `PARTITION_HORZ_A`, `_HORZ_B`, `_VERT_A`, `_VERT_B` |
| 2328 | // order (the intra-frame counterpart is `AB16_HITS`). Each arm is two 8x8 |
| 2329 | // inter leaves (`decode_inter_block8`) plus one true 16x8/8x16 inter strip |
| 2330 | // (`decode_inter_block` with `write_w`/`write_h`); the gate asserts every |
| 2331 | // arm separately so a run whose RD only picked one cannot pass as proof of |
| 2332 | // four. |
| 2333 | thread_local! { |
| 2334 | static AB16_INTER_HITS: std::cell::Cell<[usize; 4]> = const { std::cell::Cell::new([0; 4]) }; |
| 2335 | } |
| 2336 | |
| 2337 | /// [`AB16_INTER_HITS`] per arm (HORZ_A, HORZ_B, VERT_A, VERT_B at 16x16 in an |
| 2338 | /// inter frame). |
| 2339 | pub fn ab16_inter_hits_by_arm() -> [usize; 4] { |
| 2340 | AB16_INTER_HITS.with(std::cell::Cell::get) |
| 2341 | } |
| 2342 | |
| 2343 | fn bump_ab16_inter(arm: usize) { |
| 2344 | AB16_INTER_HITS.with(|c| { |
| 2345 | let mut v = c.get(); |
| 2346 | v[arm] += 1; |
| 2347 | c.set(v); |
| 2348 | }); |
| 2349 | } |
| 2350 | |
| 2351 | // lane-r14: the 16x16-level 1:4 pair -- four 16x4 (`PARTITION_HORZ_4`) or |
| 2352 | // 4x16 (`PARTITION_VERT_4`) strips, `CHROMA` counting the odd strips that |
| 2353 | // close a 4:2:0 chroma pair and `COEFF` the strips with a coded residual. |
| 2354 | thread_local! { |
| 2355 | static RECT4_16_HORZ_HITS: std::cell::Cell<usize> = const { std::cell::Cell::new(0) }; |
| 2356 | static RECT4_16_VERT_HITS: std::cell::Cell<usize> = const { std::cell::Cell::new(0) }; |
| 2357 | static RECT4_16_CHROMA_HITS: std::cell::Cell<usize> = const { std::cell::Cell::new(0) }; |
| 2358 | static RECT4_16_COEFF_HITS: std::cell::Cell<usize> = const { std::cell::Cell::new(0) }; |
| 2359 | static RECT4_16_SPLIT_HITS: std::cell::Cell<usize> = const { std::cell::Cell::new(0) }; |
| 2360 | /// lane-kf1200: pairs whose chroma edge-filter type read from the PAIR's |
| 2361 | /// base mi differs from what the strip's own (odd) mi would have said -- |
| 2362 | /// the shape that made 4 chroma samples of the ReelB `-ss 1200` |
| 2363 | /// key frame miss by 1. Zero here means a gate never exercised the fix. |
| 2364 | static RECT4_16_UV_PAIR_FILT_HITS: std::cell::Cell<usize> = const { std::cell::Cell::new(0) }; |
| 2365 | /// lane-kf1200 r2: sub-8x8 chroma blocks that are `skip_txfm` AND |
| 2366 | /// `UV_CFL_PRED` -- the arm that used to drop the CfL alpha entirely. |
| 2367 | static SKIP_CFL_HITS: std::cell::Cell<usize> = const { std::cell::Cell::new(0) }; |
| 2368 | } |
| 2369 | |
| 2370 | /// How many skipped sub-8x8 chroma blocks carried a CfL alpha (the shape |
| 2371 | /// that reconstructed as flat DC before lane-kf1200 r2). |
| 2372 | pub fn skip_cfl_hits() -> usize { |
| 2373 | SKIP_CFL_HITS.with(|c| c.get()) |
| 2374 | } |
| 2375 | |
| 2376 | /// How many 16x4/4x16 chroma pairs took a different `intra_edge_filter_type` |
| 2377 | /// from the pair's base mi than the strip's own mi would have given. |
| 2378 | pub fn rect4_16_uv_pair_filt_hits() -> usize { |
| 2379 | RECT4_16_UV_PAIR_FILT_HITS.with(|c| c.get()) |
| 2380 | } |
| 2381 | |
| 2382 | // lane-reelckf r1: the two chroma defects ReelC's key frames exposed -- |
| 2383 | // CHROMA_SKIP_CFL counts skipped intra blocks whose chroma is `UV_CFL_PRED` |
| 2384 | // (prediction still runs, libaom `predict_and_reconstruct_intra_block`), and |
| 2385 | // RECT4_16_CHROMA_DIR counts 1:4 chroma pairs predicted directionally (whose |
| 2386 | // intra-edge filter type must come from the chroma-reference position). |
| 2387 | thread_local! { |
| 2388 | static CHROMA_SKIP_CFL_HITS: std::cell::Cell<usize> = const { std::cell::Cell::new(0) }; |
| 2389 | static RECT4_16_CHROMA_DIR_HITS: std::cell::Cell<usize> = const { std::cell::Cell::new(0) }; |
| 2390 | } |
| 2391 | |
| 2392 | /// `(skipped UV_CFL_PRED blocks, directional 1:4 chroma pairs)` -- lane-reelckf. |
| 2393 | pub fn reelc_chroma_counters() -> (usize, usize) { |
| 2394 | ( |
| 2395 | CHROMA_SKIP_CFL_HITS.with(|c| c.get()), |
| 2396 | RECT4_16_CHROMA_DIR_HITS.with(|c| c.get()), |
| 2397 | ) |
| 2398 | } |
| 2399 | |
| 2400 | /// Current values of the 16x16-level 1:4 counters: `(16x4 strips, 4x16 |
| 2401 | /// strips, chroma-reference strips, strips with a coded residual, strips |
| 2402 | /// whose luma transform is split)`. |
| 2403 | pub fn rect4_16_counters() -> (usize, usize, usize, usize, usize) { |
| 2404 | ( |
| 2405 | RECT4_16_HORZ_HITS.with(|c| c.get()), |
| 2406 | RECT4_16_VERT_HITS.with(|c| c.get()), |
| 2407 | RECT4_16_CHROMA_HITS.with(|c| c.get()), |
| 2408 | RECT4_16_COEFF_HITS.with(|c| c.get()), |
| 2409 | RECT4_16_SPLIT_HITS.with(|c| c.get()), |
| 2410 | ) |
| 2411 | } |
| 2412 | |
| 2413 | /// Current value of `RECT4_32_HORZ_HITS` (32x8 strips decoded). |
| 2414 | pub(crate) fn rect4_32_horz_hits() -> usize { |
| 2415 | RECT4_32_HORZ_HITS.with(|c| c.get()) |
| 2416 | } |
| 2417 | |
| 2418 | /// Current value of `RECT4_32_VERT_HITS` (8x32 strips decoded). |
| 2419 | pub(crate) fn rect4_32_vert_hits() -> usize { |
| 2420 | RECT4_32_VERT_HITS.with(|c| c.get()) |
| 2421 | } |
| 2422 | |
| 2423 | /// Current value of `RECT4_32_HORZ_INTER_HITS` (32x8 inter strips decoded). |
| 2424 | pub(crate) fn rect4_32_horz_inter_hits() -> usize { |
| 2425 | RECT4_32_HORZ_INTER_HITS.with(|c| c.get()) |
| 2426 | } |
| 2427 | |
| 2428 | /// Current value of `RECT4_32_VERT_INTER_HITS` (8x32 inter strips decoded). |
| 2429 | pub(crate) fn rect4_32_vert_inter_hits() -> usize { |
| 2430 | RECT4_32_VERT_INTER_HITS.with(|c| c.get()) |
| 2431 | } |
| 2432 | |
| 2433 | /// Current value of `INTER_SB_HORZ_HITS` (64x32 inter strips decoded). |
| 2434 | pub(crate) fn inter_sb_horz_hits() -> usize { |
| 2435 | INTER_SB_HORZ_HITS.with(|c| c.get()) |
| 2436 | } |
| 2437 | |
| 2438 | /// Current value of `INTER_SB_VERT_HITS` (32x64 inter strips decoded). |
| 2439 | pub(crate) fn inter_sb_vert_hits() -> usize { |
| 2440 | INTER_SB_VERT_HITS.with(|c| c.get()) |
| 2441 | } |
| 2442 | |
| 2443 | /// Current value of `INTER_SB_HORZ4_HITS` (64x16 inter strips decoded). |
| 2444 | pub(crate) fn inter_sb_horz4_hits() -> usize { |
| 2445 | INTER_SB_HORZ4_HITS.with(|c| c.get()) |
| 2446 | } |
| 2447 | |
| 2448 | /// Current value of `INTER_SB_VERT4_HITS` (16x64 inter strips decoded). |
| 2449 | pub(crate) fn inter_sb_vert4_hits() -> usize { |
| 2450 | INTER_SB_VERT4_HITS.with(|c| c.get()) |
| 2451 | } |
| 2452 | |
| 2453 | // lane-rect64q r1: how many of [`decode_block_rect64`]'s three per-plane |
| 2454 | // dequant calls actually observed `CURRENT_Q_IDX != base_q_idx` -- proof the |
| 2455 | // running-vs-stale-snapshot bug this round fixed is exercised by a gate, not |
| 2456 | // just inert-compiled. Zero forever on a passing gate means that gate's |
| 2457 | // stream never carries `delta_q_present=1` through a rect64 block. |
| 2458 | thread_local! { |
| 2459 | static RECT64_QIDX_DRIFT_HITS: std::cell::Cell<usize> = |
| 2460 | const { std::cell::Cell::new(0) }; |
| 2461 | } |
| 2462 | |
| 2463 | /// Current value of [`RECT64_QIDX_DRIFT_HITS`]. |
| 2464 | pub(crate) fn rect64_qidx_drift_hits() -> usize { |
| 2465 | RECT64_QIDX_DRIFT_HITS.with(|c| c.get()) |
| 2466 | } |
| 2467 | |
| 2468 | // lane-partab r1: how many 32x32 quadrants decoded an AB partition |
| 2469 | // (PARTITION_HORZ_A / VERT_A / VERT_B -- two 16x16 squares plus one |
| 2470 | // 16x32/32x16 strip). Like [`RECT_PARTITION_HITS`], this is the |
| 2471 | // free-partition gate's proof this round's arms actually fired. |
| 2472 | thread_local! { |
| 2473 | static PARTAB_HITS: std::cell::Cell<usize> = |
| 2474 | const { std::cell::Cell::new(0) }; |
| 2475 | } |
| 2476 | |
| 2477 | /// Current value of [`PARTAB_HITS`]. |
| 2478 | pub(crate) fn partab_hits() -> usize { |
| 2479 | PARTAB_HITS.with(|c| c.get()) |
| 2480 | } |
| 2481 | |
| 2482 | // lane-part32 r1: how many 32x32 quadrants decoded each INTRA AB/4-way arm, |
| 2483 | // one counter per arm rather than one shared total -- the gate hard-asserts |
| 2484 | // each of these individually nonzero so a recipe that only ever fires |
| 2485 | // HORZ_A can't silently stand in for the whole set. |
| 2486 | thread_local! { |
| 2487 | static INTRA_HORZ_A_HITS: std::cell::Cell<usize> = |
| 2488 | const { std::cell::Cell::new(0) }; |
| 2489 | static INTRA_HORZ_B_HITS: std::cell::Cell<usize> = |
| 2490 | const { std::cell::Cell::new(0) }; |
| 2491 | static INTRA_VERT_A_HITS: std::cell::Cell<usize> = |
| 2492 | const { std::cell::Cell::new(0) }; |
| 2493 | static INTRA_VERT_B_HITS: std::cell::Cell<usize> = |
| 2494 | const { std::cell::Cell::new(0) }; |
| 2495 | } |
| 2496 | |
| 2497 | /// Current value of [`INTRA_HORZ_A_HITS`]. |
| 2498 | pub(crate) fn intra_horz_a_hits() -> usize { |
| 2499 | INTRA_HORZ_A_HITS.with(|c| c.get()) |
| 2500 | } |
| 2501 | /// Current value of [`INTRA_HORZ_B_HITS`]. |
| 2502 | pub(crate) fn intra_horz_b_hits() -> usize { |
| 2503 | INTRA_HORZ_B_HITS.with(|c| c.get()) |
| 2504 | } |
| 2505 | /// Current value of [`INTRA_VERT_A_HITS`]. |
| 2506 | pub(crate) fn intra_vert_a_hits() -> usize { |
| 2507 | INTRA_VERT_A_HITS.with(|c| c.get()) |
| 2508 | } |
| 2509 | /// Current value of [`INTRA_VERT_B_HITS`]. |
| 2510 | pub(crate) fn intra_vert_b_hits() -> usize { |
| 2511 | INTRA_VERT_B_HITS.with(|c| c.get()) |
| 2512 | } |
| 2513 | |
| 2514 | // How many [`read_inter_compound_mode`] reads actually happened |
| 2515 | // (lane-av1comp) -- proves a `COMPOUND_REFERENCE` block reached its own |
| 2516 | // `compound_mode` symbol (past `comp_mode`/`comp_ref` and the compound |
| 2517 | // mvstack build), not just that `comp_mode` fired. Still refused right |
| 2518 | // after (no MV assignment or MC wired), so this only counts attempted |
| 2519 | // reads. |
| 2520 | thread_local! { |
| 2521 | static COMPOUND_MODE_HITS: std::cell::Cell<usize> = |
| 2522 | const { std::cell::Cell::new(0) }; |
| 2523 | } |
| 2524 | |
| 2525 | /// Current value of [`COMPOUND_MODE_HITS`]. |
| 2526 | pub(crate) fn compound_mode_hits() -> usize { |
| 2527 | COMPOUND_MODE_HITS.with(|c| c.get()) |
| 2528 | } |
| 2529 | |
| 2530 | /// How many query blocks folded in at least one temporal MV candidate |
| 2531 | /// (spec 7.10.2.8's `add_tpl_ref_mv`) so far in this process -- the |
| 2532 | /// temporal-MV gate's own firing counter (lane-av1tmvp). |
| 2533 | /// |
| 2534 | /// # HANDOFF (lane-av1tmvp): not yet wired to a live stream |
| 2535 | /// [`crate::mvstack::find_mv_stack_with_sign_bias`]'s only call site in |
| 2536 | /// this file (`decode_inter_block`) still passes `tpl: None` -- this |
| 2537 | /// counter compiles and the projection math it counts is unit-tested |
| 2538 | /// ([`crate::motion_field`]), but nothing in [`crate::stream::decode_stream`] |
| 2539 | /// builds a per-DPB-slot [`crate::motion_field::MotionField`] or calls |
| 2540 | /// [`crate::motion_field::setup_motion_field`] yet, so this will read `0` |
| 2541 | /// against any real stream today. See the module doc on |
| 2542 | /// `crate::motion_field` and this crate's ledger for the remaining wiring. |
| 2543 | pub(crate) fn tmv_hits() -> usize { |
| 2544 | crate::mvstack::tmv_hits() |
| 2545 | } |
| 2546 | |
| 2547 | /// Current value of [`crate::mc::inter_pred_hits`] -- inter predictions |
| 2548 | /// produced on this thread, the 10-bit inter gate's firing proof. |
| 2549 | pub(crate) fn inter_pred_hits() -> usize { |
| 2550 | crate::mc::inter_pred_hits() |
| 2551 | } |
| 2552 | |
| 2553 | /// Everything [`decode_inter_block`] needs to fold temporal MV candidates |
| 2554 | /// (spec 7.10.2.8), threaded down from [`decode_inter_frame_tile_with_cdfs`]: |
| 2555 | /// the current frame's own projected field ([`crate::motion_field::setup_motion_field`]'s |
| 2556 | /// output, built by the caller when `header.use_ref_frame_mvs` is set) plus |
| 2557 | /// the order-hint bookkeeping [`crate::motion_field::get_relative_dist`] |
| 2558 | /// needs to turn a query block's own resolved `ref_frame` into |
| 2559 | /// `TplArgs::cur_offset_0` (spec 7.9.3). |
| 2560 | pub(crate) struct TplFrameArgs<'a> { |
| 2561 | pub field: &'a crate::motion_field::TplField, |
| 2562 | pub order_hint_bits: u32, |
| 2563 | pub order_hint: u32, |
| 2564 | /// `ref_order_hints[ref_frame - LAST_FRAME]` for `ref_frame` in |
| 2565 | /// `LAST_FRAME..=ALTREF_FRAME` — the OrderHint of the picture this |
| 2566 | /// frame's own `ref_frame_idx` names for each single reference. |
| 2567 | pub ref_order_hints: [u32; 7], |
| 2568 | } |
| 2569 | |
| 2570 | /// Builds this frame's own saved motion field (libaom `av1_copy_frame_mvs`, |
| 2571 | /// spec 7.9's per-frame `MotionFieldMvs` storage step) from its fully |
| 2572 | /// decoded `grid`: every 4x4 unit an inter block covers reads the same |
| 2573 | /// `MiInfo` ([`decode_inter_block`]/`decode_inter_block8` write it |
| 2574 | /// block-uniform), so sampling any one 4x4 unit inside each 8x8 cell is |
| 2575 | /// exact — every block this crate ever codes is at least 8x8, so no 8x8 |
| 2576 | /// cell straddles two different blocks' values. |
| 2577 | pub(crate) fn build_motion_field( |
| 2578 | grid: &MiGrid, |
| 2579 | mi_cols: usize, |
| 2580 | mi_rows: usize, |
| 2581 | order_hint: u32, |
| 2582 | order_hint_bits: u32, |
| 2583 | ref_order_hints: [u32; 7], |
| 2584 | ) -> crate::motion_field::MotionField { |
| 2585 | let mut field = |
| 2586 | crate::motion_field::MotionField::new(mi_cols, mi_rows, order_hint, ref_order_hints, false); |
| 2587 | // lane-interbis r1: libaom `av1_calculate_ref_frame_side` -- a reference |
| 2588 | // whose OrderHint is AHEAD of (or equal to) this frame's is "on the other |
| 2589 | // side", and `av1_copy_frame_mvs` skips it entirely, so a compound block |
| 2590 | // referencing one stores only its OTHER side's mv. With |
| 2591 | // `enable_order_hint` off (`order_hint_bits == 0`) every side is 0 and |
| 2592 | // nothing is skipped. |
| 2593 | let other_side = |rf: i8| -> bool { |
| 2594 | if order_hint_bits == 0 { |
| 2595 | return false; |
| 2596 | } |
| 2597 | let oh = ref_order_hints[(rf - LAST_FRAME) as usize]; |
| 2598 | crate::motion_field::get_relative_dist(order_hint_bits, oh, order_hint) > 0 |
| 2599 | || oh == order_hint |
| 2600 | }; |
| 2601 | // libaom `REFMVS_LIMIT` (`mvref_common.h`): a stored motion field mv is |
| 2602 | // capped at 12 bits per component; a longer one is not stored at all. |
| 2603 | const REFMVS_LIMIT: i32 = (1 << 12) - 1; |
| 2604 | for row in 0..mi_rows { |
| 2605 | for col in 0..mi_cols { |
| 2606 | let Some(info) = grid.get(row, col) else { |
| 2607 | continue; |
| 2608 | }; |
| 2609 | // `av1_copy_frame_mvs` runs for EVERY block of an inter frame and |
| 2610 | // CLEARS the cells it covers first (`mv->ref_frame = NONE_FRAME`), |
| 2611 | // so an intra block -- or an inter block whose every slot is on the |
| 2612 | // other side / over the limit -- wipes what an earlier block wrote |
| 2613 | // into the same 8x8 cell (only reachable from sub-8x8 leaves, which |
| 2614 | // share a cell). |
| 2615 | if !info.is_inter { |
| 2616 | field.set(row, col, None); |
| 2617 | continue; |
| 2618 | } |
| 2619 | // `av1_copy_frame_mvs` walks BOTH reference slots and the LAST |
| 2620 | // qualifying one wins -- a compound block stores its second |
| 2621 | // reference's mv, never its first (unless the second is on the |
| 2622 | // other side or over the limit). Storing slot 0 unconditionally |
| 2623 | // put the wrong (mv, ref_frame_offset) pair in every compound |
| 2624 | // block's cell, which a later frame's temporal candidate then |
| 2625 | // projected with the wrong reference distance. |
| 2626 | let mut saved: Option<crate::motion_field::SavedMv> = None; |
| 2627 | for (rf, mv) in [ |
| 2628 | (info.ref_frame, Some(info.mv)), |
| 2629 | (info.ref_frame1.unwrap_or(0), info.mv1), |
| 2630 | ] { |
| 2631 | let (Some(mv), true) = (mv, rf > 0) else { |
| 2632 | continue; |
| 2633 | }; |
| 2634 | if other_side(rf) { |
| 2635 | continue; |
| 2636 | } |
| 2637 | if mv.0.abs() > REFMVS_LIMIT || mv.1.abs() > REFMVS_LIMIT { |
| 2638 | continue; |
| 2639 | } |
| 2640 | saved = Some(crate::motion_field::SavedMv { mv, ref_frame: rf }); |
| 2641 | } |
| 2642 | field.set(row, col, saved); |
| 2643 | } |
| 2644 | } |
| 2645 | field |
| 2646 | } |
| 2647 | |
| 2648 | /// Current value of the counter for `ref_frame` (`LAST2_FRAME`..=`ALTREF_FRAME`; |
| 2649 | /// panics on `LAST_FRAME`/`INTRA_FRAME`/`NONE`, which have no counter here). |
| 2650 | pub(crate) fn ref_hits(ref_frame: i8) -> usize { |
| 2651 | let c = match ref_frame { |
| 2652 | LAST2_FRAME => &LAST2_HITS, |
| 2653 | LAST3_FRAME => &LAST3_HITS, |
| 2654 | GOLDEN_FRAME => &GOLDEN_HITS, |
| 2655 | BWDREF_FRAME => &BWDREF_HITS, |
| 2656 | ALTREF2_FRAME => &ALTREF2_HITS, |
| 2657 | ALTREF_FRAME => &ALTREF_HITS, |
| 2658 | other => panic!("ref_hits: no per-reference counter for ref_frame {other}"), |
| 2659 | }; |
| 2660 | c.with(|v| v.get()) |
| 2661 | } |
| 2662 | |
| 2663 | thread_local! { |
| 2664 | /// The sequence header's `enable_intra_edge_filter`, set once per |
| 2665 | /// [`decode_key_frame_tile_with_cdfs`] call: [`PlaneBuf::reconstruct`]'s |
| 2666 | /// own read of it, kept off the call stack (see that function's own doc |
| 2667 | /// comment for why) rather than threaded through every `read_plane`/ |
| 2668 | /// `decode_block`/`decode_leaf8` call between here and there. |
| 2669 | static ENABLE_EDGE_FILTER: std::cell::Cell<bool> = const { std::cell::Cell::new(false) }; |
| 2670 | } |
| 2671 | |
| 2672 | /// Set [`ENABLE_EDGE_FILTER`] for this thread. |
| 2673 | /// |
| 2674 | /// `decode_key_frame_tile_with_cdfs` sets it, but `decode_inter_frame_tile` |
| 2675 | /// never did -- so an inter frame's intra blocks read whatever value the last |
| 2676 | /// key frame decoded ON THIS THREAD left behind. Inside one stream that is |
| 2677 | /// harmless (its own key frame sets it first), but a caller that decodes an |
| 2678 | /// inter frame directly, or a test harness running several streams on one |
| 2679 | /// thread, inherits a foreign sequence's bit: decode stops being a pure |
| 2680 | /// function of the bitstream. `decode_stream` now calls this once per stream |
| 2681 | /// from the sequence header, which covers every frame including inter ones. |
| 2682 | pub(crate) fn set_enable_edge_filter(value: bool) { |
| 2683 | ENABLE_EDGE_FILTER.with(|f| f.set(value)); |
| 2684 | } |
| 2685 | |
| 2686 | /// Current value of [`ANGLE_DELTA_HITS`]. |
| 2687 | pub(crate) fn angle_delta_hits() -> usize { |
| 2688 | ANGLE_DELTA_HITS.with(|c| c.get()) |
| 2689 | } |
| 2690 | |
| 2691 | /// Current value of [`TX_CLASS1_HITS`]. |
| 2692 | pub(crate) fn tx_class1_hits() -> usize { |
| 2693 | TX_CLASS1_HITS.with(|c| c.get()) |
| 2694 | } |
| 2695 | |
| 2696 | // How many `PARTITION_SPLIT` decisions below an 8x8 block (four independent |
| 2697 | // `BLOCK_4X4` leaves, spec `decode_partition`'s `bSize < BLOCK_8X8` recursion |
| 2698 | // bottom) this thread's decode actually reconstructed -- the same |
| 2699 | // before/after counter pattern as [`TX_CLASS1_HITS`], proving a stream |
| 2700 | // genuinely exercised the sub-8x8 leaf path rather than every attempt |
| 2701 | // coincidentally landing on `PARTITION_NONE`. |
| 2702 | thread_local! { |
| 2703 | static SUB8_SPLIT_HITS: std::cell::Cell<usize> = const { std::cell::Cell::new(0) }; |
| 2704 | } |
| 2705 | |
| 2706 | // lane-cdef r1: (a) how many 8x8 CDEF units `apply_cdef` EXCLUDED because all |
| 2707 | // four of their mi cells are `skip_txfm` (libaom `cdef.c:29-38` |
| 2708 | // `is_8x8_block_skip` / `av1_cdef_compute_sb_list`'s dlist test), and (b) how |
| 2709 | // many 8x8 inter leaves (`decode_inter_block8`) wrote that skip band at all. |
| 2710 | // (b) was 0 for every stream before this round -- the leaf path never touched |
| 2711 | // the band, so an all-skip 8x8 group kept the PREVIOUS frame's flags and got |
| 2712 | // filtered where libaom skips it. A gate asserts both are non-zero. |
| 2713 | thread_local! { |
| 2714 | static CDEF_SKIPPED_UNITS: std::cell::Cell<usize> = const { std::cell::Cell::new(0) }; |
| 2715 | static INTER8_SKIP_BAND_HITS: std::cell::Cell<usize> = const { std::cell::Cell::new(0) }; |
| 2716 | // lane-cdef r2: (c) 8x8 CDEF units that are FILTERED and whose extent |
| 2717 | // crosses the cropped frame edge -- the only units for which "clamp at |
| 2718 | // the crop" and "pad past the mi-rounded extent with CDEF_VERY_LARGE" |
| 2719 | // (libaom `cdef.c:249-256`, `cdef_prepare_fb`'s hsize/vsize are |
| 2720 | // `nhb << mi_wide_l2` off the MI grid, not the crop) differ. A frame |
| 2721 | // whose width or height is not a multiple of 8 is the only shape that |
| 2722 | // produces one; the straddling-band gate arms assert it grew. |
| 2723 | static CDEF_STRADDLE_UNITS: std::cell::Cell<usize> = const { std::cell::Cell::new(0) }; |
| 2724 | } |
| 2725 | |
| 2726 | /// Current value of `CDEF_SKIPPED_UNITS`. |
| 2727 | pub(crate) fn cdef_skipped_units() -> usize { |
| 2728 | CDEF_SKIPPED_UNITS.with(|c| c.get()) |
| 2729 | } |
| 2730 | |
| 2731 | /// Current value of `INTER8_SKIP_BAND_HITS`. |
| 2732 | pub(crate) fn inter8_skip_band_hits() -> usize { |
| 2733 | INTER8_SKIP_BAND_HITS.with(|c| c.get()) |
| 2734 | } |
| 2735 | |
| 2736 | /// Current value of `CDEF_STRADDLE_UNITS`. |
| 2737 | pub(crate) fn cdef_straddle_units() -> usize { |
| 2738 | CDEF_STRADDLE_UNITS.with(|c| c.get()) |
| 2739 | } |
| 2740 | |
| 2741 | /// Current value of [`SUB8_SPLIT_HITS`]. |
| 2742 | pub(crate) fn sub8_split_hits() -> usize { |
| 2743 | SUB8_SPLIT_HITS.with(|c| c.get()) |
| 2744 | } |
| 2745 | |
| 2746 | // How many 4x8 / 8x4 leaves (lane-tx4x8's `PARTITION_VERT`/`PARTITION_HORZ` |
| 2747 | // of an 8x8 block) this thread's decode reconstructed with REAL coefficients |
| 2748 | // -- a skipped leaf exercises the prediction and the mi bookkeeping but never |
| 2749 | // the rect transform, the 32-position `eob_pt` or the rect scan, so the gate |
| 2750 | // asserts on these two rather than a plain leaf count. |
| 2751 | thread_local! { |
| 2752 | static TX4X8_CODED_HITS: std::cell::Cell<usize> = const { std::cell::Cell::new(0) }; |
| 2753 | static TX8X4_CODED_HITS: std::cell::Cell<usize> = const { std::cell::Cell::new(0) }; |
| 2754 | /// Sub-8x8 rect leaves whose `tx_depth` symbol resolved to 1 -- the leaf's |
| 2755 | /// two 4x4 transform units, predicted one after the other. A separate |
| 2756 | /// counter from [`TX_DEPTH_HITS`] (which every square path also bumps) so |
| 2757 | /// the gate can prove THIS path fired. |
| 2758 | static RECT8_SPLIT_TX_HITS: std::cell::Cell<usize> = const { std::cell::Cell::new(0) }; |
| 2759 | /// 16x8/8x16 intra strips whose prediction actually read past the block's |
| 2760 | /// own width (above-right) or height (below-left) -- the samples |
| 2761 | /// `Reach::of_rect` decides on, and the only ones a wrong `has_tr_*`/ |
| 2762 | /// `has_bl_*` table row can corrupt (lane-tx4x8 r3). |
| 2763 | static RECT_STRIP_REACH_HITS: std::cell::Cell<usize> = const { std::cell::Cell::new(0) }; |
| 2764 | /// Every 16x8/8x16 intra strip predicted, reaching or not -- the |
| 2765 | /// denominator [`RECT_STRIP_REACH_HITS`] is a fraction of. |
| 2766 | static RECT_STRIP_PRED_HITS: std::cell::Cell<usize> = const { std::cell::Cell::new(0) }; |
| 2767 | } |
| 2768 | |
| 2769 | /// Current value of [`RECT8_SPLIT_TX_HITS`]. |
| 2770 | pub(crate) fn rect8_split_tx_hits() -> usize { |
| 2771 | RECT8_SPLIT_TX_HITS.with(|c| c.get()) |
| 2772 | } |
| 2773 | |
| 2774 | /// Current value of [`RECT_STRIP_PRED_HITS`]. |
| 2775 | pub(crate) fn rect_strip_pred_hits() -> usize { |
| 2776 | RECT_STRIP_PRED_HITS.with(|c| c.get()) |
| 2777 | } |
| 2778 | |
| 2779 | /// Current value of [`RECT_STRIP_REACH_HITS`]. |
| 2780 | pub(crate) fn rect_strip_reach_hits() -> usize { |
| 2781 | RECT_STRIP_REACH_HITS.with(|c| c.get()) |
| 2782 | } |
| 2783 | |
| 2784 | /// Current value of [`TX4X8_CODED_HITS`]. |
| 2785 | pub(crate) fn tx4x8_coded_hits() -> usize { |
| 2786 | TX4X8_CODED_HITS.with(|c| c.get()) |
| 2787 | } |
| 2788 | |
| 2789 | /// Current value of [`TX8X4_CODED_HITS`]. |
| 2790 | pub(crate) fn tx8x4_coded_hits() -> usize { |
| 2791 | TX8X4_CODED_HITS.with(|c| c.get()) |
| 2792 | } |
| 2793 | |
| 2794 | // How many CHROMA coefficient blocks read their `eob_pt` from the class-1 |
| 2795 | // (`TX_CLASS_HORIZ`/`VERT`) row, across every call on the current thread. |
| 2796 | // Chroma never codes a `tx_type` symbol -- it inherits the colocated luma |
| 2797 | // type verbatim (`av1_get_tx_type`, `blockd.h:1291`) -- so this fires only on |
| 2798 | // an inter leaf whose luma picked a 1D type, the path lane-eobc1 wired |
| 2799 | // (`eob_pt_{16,64,128,256}_chroma_class1`). |
| 2800 | thread_local! { |
| 2801 | static CHROMA_EOB_CLASS1_HITS: std::cell::Cell<usize> = |
| 2802 | const { std::cell::Cell::new(0) }; |
| 2803 | } |
| 2804 | |
| 2805 | /// Current value of [`CHROMA_EOB_CLASS1_HITS`]. |
| 2806 | pub(crate) fn chroma_eob_class1_hits() -> usize { |
| 2807 | CHROMA_EOB_CLASS1_HITS.with(|c| c.get()) |
| 2808 | } |
| 2809 | |
| 2810 | /// Counts one [`CHROMA_EOB_CLASS1_HITS`] when `plane_idx`'s inherited |
| 2811 | /// `tx_type` put this block's `eob_pt` on the class-1 row. |
| 2812 | fn note_chroma_class1(plane_idx: usize, tx_type: TxType) { |
| 2813 | if plane_idx > 0 && TxClass::of(tx_type) != TxClass::TwoD { |
| 2814 | CHROMA_EOB_CLASS1_HITS.with(|c| c.set(c.get() + 1)); |
| 2815 | } |
| 2816 | } |
| 2817 | |
| 2818 | const SUB_MI: u32 = 4; |
| 2819 | const MI: usize = 4; |
| 2820 | const SB: usize = 64; |
| 2821 | /// The `above_side`/`left_side` value a cell with no decoded neighbour holds |
| 2822 | /// (tile start, superblock-row start). |
| 2823 | /// |
| 2824 | /// [`Neighbours::partition_ctx_mi`] reads it as libaom's |
| 2825 | /// `av1_get_partition_plane_context` reads its own zero-initialised |
| 2826 | /// `above_partition_context` byte: bit `bsl` clear, i.e. "the neighbour is at |
| 2827 | /// least as wide as this block". lane-sb128 r1: the old sentinel was `SB` |
| 2828 | /// (64), which reads identically at every level up to 64x64 but sets the bit |
| 2829 | /// at the `BLOCK_128X128` root (64 * 2 <= 128) -- the 128-level partition |
| 2830 | /// symbol was read off CDF row 3 where libaom uses row 0 (aomdec |
| 2831 | /// `EC_PART mi_row=0 mi_col=0 bsize=15 ctx=16`, i.e. `bsl*4 + 0`). |
| 2832 | const NO_NEIGHBOUR_SIDE: usize = 128; |
| 2833 | const BLOCK: usize = 32; |
| 2834 | const SUB: usize = 16; |
| 2835 | const TX32: usize = 32; |
| 2836 | const TX16: usize = 16; |
| 2837 | const TX8: usize = 8; |
| 2838 | const TX4: usize = 4; |
| 2839 | const DC_PRED: usize = 0; |
| 2840 | /// `V_PRED` (spec 6.10.2): the first directional mode, and so the first that |
| 2841 | /// carries an angle delta — [`crate::tile`]'s own private copy of the same |
| 2842 | /// constant. |
| 2843 | const V_PRED: usize = 1; |
| 2844 | const H_PRED: usize = 2; |
| 2845 | /// `D157_PRED`: `fimode_to_intradir`'s target for `FILTER_D157_PRED` (spec |
| 2846 | /// `Fimode_To_Intradir`, libaom `blockd.h`). |
| 2847 | const D157_PRED: usize = 6; |
| 2848 | /// The last directional mode, `D67_PRED`. |
| 2849 | const D67_PRED: usize = 8; |
| 2850 | /// The symbol an angle delta of zero codes as (spec 9.3): the alphabet runs |
| 2851 | /// from -3 to +3, so this is its middle — [`crate::tile`]'s writer never |
| 2852 | /// codes anything else, so this decoder never reads anything else either. |
| 2853 | const ANGLE_DELTA_ZERO: usize = 3; |
| 2854 | const NUM_BASE_LEVELS: i32 = 2; |
| 2855 | const COEFF_BASE_RANGE: i32 = 12; |
| 2856 | const BR_STEP: i32 = 3; |
| 2857 | const MAX_BR_LEVEL: i32 = NUM_BASE_LEVELS + COEFF_BASE_RANGE; |
| 2858 | |
| 2859 | /// `partition_gather_vert_alike`/`_horz_alike` (spec 9.3): the partition types |
| 2860 | /// whose probability mass a superblock or block half outside the frame |
| 2861 | /// gathers into its one remaining flag — [`crate::tile`]'s own private copy, |
| 2862 | /// duplicated here (that module is another lane's territory this round) and |
| 2863 | /// kept in step with it by the byte-exact round-trip tests below rather than |
| 2864 | /// by sharing code. |
| 2865 | const VERT_ALIKE: [usize; 6] = [2, PARTITION_SPLIT, 4, 6, 7, 9]; |
| 2866 | const HORZ_ALIKE: [usize; 6] = [1, PARTITION_SPLIT, 4, 5, 6, 8]; |
| 2867 | |
| 2868 | fn trace_edge(mi_r: usize, mi_c: usize, bsize: u32, ctx: usize, rng: u32) { |
| 2869 | if std::env::var_os("EC_AV1_TRACE").is_some() { |
| 2870 | eprintln!("OUR_PART mi_row={mi_r} mi_col={mi_c} bsize={bsize} ctx={ctx} rng={rng}"); |
| 2871 | } |
| 2872 | } |
| 2873 | |
| 2874 | fn element_prob(cdf: &[u16], element: usize) -> u16 { |
| 2875 | cdf[element] - if element > 0 { cdf[element - 1] } else { 0 } |
| 2876 | } |
| 2877 | |
| 2878 | /// The two-symbol CDF a partial superblock/block's forced-split flag is read |
| 2879 | /// with: the mass of `elements` becomes the probability of the one partition |
| 2880 | /// the frame edge still allows, mirroring [`crate::tile`]'s own `gather` |
| 2881 | /// (the encoder's non-adapting counterpart to this read). |
| 2882 | fn gather(cdf: &[u16], elements: [usize; 6]) -> [u16; 3] { |
| 2883 | let split: u16 = elements.iter().map(|&e| element_prob(cdf, e)).sum(); |
| 2884 | [32768 - split, 32768, 0] |
| 2885 | } |
| 2886 | |
| 2887 | fn unsupported(what: impl Into<String>) -> Error { |
| 2888 | Error::unsupported("AV1 tile", what.into()) |
| 2889 | } |
| 2890 | |
| 2891 | /// The coefficient q-context a frame's `base_q_idx` picks its default CDFs |
| 2892 | /// from (spec `Get_Qctx`), mirroring [`crate::tile`]'s private copy of the |
| 2893 | /// same rule. |
| 2894 | pub(crate) fn q_ctx_of(base_q_idx: u8) -> usize { |
| 2895 | match base_q_idx { |
| 2896 | 0..=20 => 0, |
| 2897 | 21..=60 => 1, |
| 2898 | 61..=120 => 2, |
| 2899 | _ => 3, |
| 2900 | } |
| 2901 | } |
| 2902 | |
| 2903 | /// `Default_Scan_NxN` (spec 8.4.2), the same anti-diagonal walk |
| 2904 | /// [`crate::tile`]'s writer generates its scan tables with. |
| 2905 | fn default_scan(side: usize) -> Vec<u16> { |
| 2906 | let mut scan = Vec::with_capacity(side * side); |
| 2907 | for d in 0..(2 * side - 1) { |
| 2908 | let lo = d.saturating_sub(side - 1); |
| 2909 | let hi = d.min(side - 1); |
| 2910 | let diagonal = (lo..=hi).map(|row| (row * side + (d - row)) as u16); |
| 2911 | if d % 2 == 0 { |
| 2912 | scan.extend(diagonal.rev()); |
| 2913 | } else { |
| 2914 | scan.extend(diagonal); |
| 2915 | } |
| 2916 | } |
| 2917 | scan |
| 2918 | } |
| 2919 | |
| 2920 | /// `Mrow_Scan`/`Mcol_Scan` (spec 8.4.2, libaom `scan.c`): the two |
| 2921 | /// class-specific scans a `V_DCT`/`H_DCT` transform block reads coefficients |
| 2922 | /// in, instead of the default zigzag. libaom's own tables are keyed by a |
| 2923 | /// *column-major* raster (`get_txb_bhl`'s `col = pos / height`) over its |
| 2924 | /// physically column-major level buffer, but they decompose to the exact |
| 2925 | /// same (row, col) walk as a flat row-major traversal of *our* row-major |
| 2926 | /// `grid`/`levels` (`pos = row * side + col`) once you read them that way: |
| 2927 | /// `Mrow_Scan` visits row 0 across every column, then row 1, ... — precisely |
| 2928 | /// `0..side*side` in our own indexing — and `Mcol_Scan` is that walk |
| 2929 | /// transposed (column 0 down every row, then column 1, ...). |
| 2930 | fn class_scan_table(side: usize, class: TxClass) -> Vec<u16> { |
| 2931 | class_scan_table_wh(side, side, class) |
| 2932 | } |
| 2933 | |
| 2934 | /// [`class_scan_table`] widened to `(w, h)` (lane-tx4x8). Checked against |
| 2935 | /// libaom's own `mrow_scan_4x8`/`mcol_scan_4x8`/`mrow_scan_8x4`/ |
| 2936 | /// `mcol_scan_8x4` (`scan.c`) converted out of their column-major encoding |
| 2937 | /// by `(p % h) * w + p / h`, the same conversion [`SCAN_16X8`] was |
| 2938 | /// transcribed with -- `mrow` is the plain row-major raster in our indexing |
| 2939 | /// and `mcol` its transpose, at rect sizes exactly as at square ones. |
| 2940 | fn class_scan_table_wh(w: usize, h: usize, class: TxClass) -> Vec<u16> { |
| 2941 | match class { |
| 2942 | TxClass::Vert => (0..(w * h) as u16).collect(), |
| 2943 | TxClass::Horiz => (0..w) |
| 2944 | .flat_map(|col| (0..h).map(move |row| (row * w + col) as u16)) |
| 2945 | .collect(), |
| 2946 | TxClass::TwoD => unreachable!("class_scan is only for V_DCT/H_DCT"), |
| 2947 | } |
| 2948 | } |
| 2949 | |
| 2950 | fn eob_coeff_ctx(scan_idx: usize, area: usize) -> usize { |
| 2951 | match scan_idx { |
| 2952 | 0 => 0, |
| 2953 | i if i <= area / 8 => 1, |
| 2954 | i if i <= area / 4 => 2, |
| 2955 | _ => 3, |
| 2956 | } |
| 2957 | } |
| 2958 | |
| 2959 | fn neighbour(grid: &[i32], side: usize, row: usize, col: usize) -> i32 { |
| 2960 | if row >= side || col >= side { |
| 2961 | 0 |
| 2962 | } else { |
| 2963 | grid[row * side + col] |
| 2964 | } |
| 2965 | } |
| 2966 | |
| 2967 | /// spec 5.11.39's three `TxClass`es (`tx_type_to_class`, libaom |
| 2968 | /// `txb_common.h`): everything but the two lone-axis-identity types this |
| 2969 | /// decoder reads (`V_DCT`/`H_DCT`) is `TX_CLASS_2D`. |
| 2970 | #[derive(Clone, Copy, PartialEq, Eq)] |
| 2971 | enum TxClass { |
| 2972 | TwoD, |
| 2973 | Horiz, |
| 2974 | Vert, |
| 2975 | } |
| 2976 | |
| 2977 | impl TxClass { |
| 2978 | fn of(tx_type: TxType) -> Self { |
| 2979 | match tx_type { |
| 2980 | // `TX_CLASS_VERT`/`TX_CLASS_HORIZ` (`txb_common.h`'s `tx_type_to_class`) |
| 2981 | // key on axis alone -- FLIPADST is still ADST for this purpose. |
| 2982 | TxType::VDct | TxType::VAdst | TxType::VFlipAdst => Self::Vert, |
| 2983 | TxType::HDct | TxType::HAdst | TxType::HFlipAdst => Self::Horiz, |
| 2984 | _ => Self::TwoD, |
| 2985 | } |
| 2986 | } |
| 2987 | } |
| 2988 | |
| 2989 | /// `nz_map_ctx_offset_1d`: `TX_CLASS_HORIZ`/`TX_CLASS_VERT` share one 32-entry |
| 2990 | /// table (indexed by the position along the transform's *coded* axis -- |
| 2991 | /// `col` for horiz, `row` for vert) that places their contexts past the 2D |
| 2992 | /// table's own 26 rows (`SIG_COEF_CONTEXTS_2D`), 16 rows total |
| 2993 | /// (`SIG_COEF_CONTEXTS_1D`) split 26/31/36. |
| 2994 | fn nz_map_ctx_offset_1d(i: usize) -> usize { |
| 2995 | match i { |
| 2996 | 0 => 26, |
| 2997 | 1 => 31, |
| 2998 | _ => 36, |
| 2999 | } |
| 3000 | } |
| 3001 | |
| 3002 | fn base_ctx( |
| 3003 | grid: &[i32], |
| 3004 | side: usize, |
| 3005 | row: usize, |
| 3006 | col: usize, |
| 3007 | class: TxClass, |
| 3008 | rect_shape: Option<(usize, usize)>, |
| 3009 | ) -> usize { |
| 3010 | // libaom's `get_nz_map_ctx_from_stats` only special-cases the DC |
| 3011 | // coefficient to ctx 0 for `TX_CLASS_2D` (`(tx_class | coeff_idx) == 0`, |
| 3012 | // txb_common.h) -- `V_DCT`/`H_DCT` (class `Horiz`/`Vert`) still run the DC |
| 3013 | // position through the full 1D neighbour sum and offset table. Skipping |
| 3014 | // that for every class desynced the last (DC) coefficient of a |
| 3015 | // `TX_CLASS_HORIZ` block (lane-av1tx4 final round: real ctx 28 read as |
| 3016 | // 0, which then read one extra `BR` symbol at the max and forced a |
| 3017 | // spurious third Golomb call). |
| 3018 | if class == TxClass::TwoD && row == 0 && col == 0 { |
| 3019 | return 0; |
| 3020 | } |
| 3021 | let offsets: [(usize, usize); 5] = match class { |
| 3022 | TxClass::TwoD => [(1, 0), (0, 1), (1, 1), (2, 0), (0, 2)], |
| 3023 | TxClass::Horiz => [(1, 0), (0, 1), (0, 2), (0, 3), (0, 4)], |
| 3024 | TxClass::Vert => [(1, 0), (0, 1), (2, 0), (3, 0), (4, 0)], |
| 3025 | }; |
| 3026 | let mag: i32 = offsets |
| 3027 | .iter() |
| 3028 | .map(|&(dr, dc)| neighbour(grid, side, row + dr, col + dc).abs().min(3)) |
| 3029 | .sum(); |
| 3030 | let ctx = ((mag + 1) >> 1).min(4) as usize; |
| 3031 | match class { |
| 3032 | // libaom's CDF *set* for a truncated 64-wide/tall corner resolves |
| 3033 | // through `get_txsize_entropy_ctx` to the square `TX_64X64` (this |
| 3034 | // decoder's `TxbSet::Luma64`), but `get_nz_map_ctx_from_stats` |
| 3035 | // separately indexes `av1_nz_map_ctx_offset` by the raw, un-adjusted |
| 3036 | // `tx_size` -- `TX_32X64`/`TX_64X32` for a superblock-level HORZ/VERT |
| 3037 | // strip, which own a genuinely different table from the square one |
| 3038 | // (lane-sbpart r8 root cause: `(row=1, col=0)` reads ctx 2 under the |
| 3039 | // square table vs the real ctx 13 the rect table gives). |
| 3040 | TxClass::TwoD => { |
| 3041 | // lane-rectsplit r4: apply libaom's GENERATING RULE |
| 3042 | // (`txb_common.h:199-209`) directly instead of indexing the |
| 3043 | // transcribed 5x5 tables. `av1_nz_map_ctx_offset[tx_size]` is a |
| 3044 | // FLAT array read at `coeff_idx`, and `coeff_idx` is COLUMN-major |
| 3045 | // (libaom decomposes it as `col = coeff_idx >> bhl`, `bhl` the |
| 3046 | // ADJUSTED height's log2, 5 for both `TX_64X32` and `TX_32X64`), |
| 3047 | // so 32 consecutive entries are one COLUMN: the [`cdf`] |
| 3048 | // transcriptions are `[col][row]` and this read had them as |
| 3049 | // `[row][col]` -- the transpose, i.e. the other shape's rule with |
| 3050 | // 11 and 16 swapped. Same values as [`base_ctx_rect`] now, stated |
| 3051 | // once; `base_ctx_rect_offsets_match_the_transcribed_tables_over_the_whole_domain` |
| 3052 | // pins the rule against the tables over their whole 5x5 domain. |
| 3053 | if let Some((w, h)) = rect_shape { |
| 3054 | if std::env::var_os("EC_NZOFF_DUMP").is_some() { |
| 3055 | eprintln!("NZOFF side={side} shape={w}x{h} row={row} col={col}"); |
| 3056 | } |
| 3057 | if w < h && row < 2 { |
| 3058 | return ctx + 11; |
| 3059 | } |
| 3060 | if w > h && col < 2 { |
| 3061 | return ctx + 16; |
| 3062 | } |
| 3063 | } |
| 3064 | ctx + cdf::NZ_MAP_CTX_OFFSET_32[row.min(4)][col.min(4)] as usize |
| 3065 | } |
| 3066 | TxClass::Horiz => ctx + nz_map_ctx_offset_1d(col.min(31)), |
| 3067 | TxClass::Vert => ctx + nz_map_ctx_offset_1d(row.min(31)), |
| 3068 | } |
| 3069 | } |
| 3070 | |
| 3071 | fn br_ctx(grid: &[i32], side: usize, row: usize, col: usize, class: TxClass) -> usize { |
| 3072 | let extra = match class { |
| 3073 | TxClass::TwoD => neighbour(grid, side, row + 1, col + 1).abs(), |
| 3074 | TxClass::Horiz => neighbour(grid, side, row, col + 2).abs(), |
| 3075 | TxClass::Vert => neighbour(grid, side, row + 2, col).abs(), |
| 3076 | }; |
| 3077 | let mag = neighbour(grid, side, row + 1, col).abs() |
| 3078 | + neighbour(grid, side, row, col + 1).abs() |
| 3079 | + extra; |
| 3080 | let mag = (((mag + 1) >> 1).min(6)) as usize; |
| 3081 | if row == 0 && col == 0 { |
| 3082 | return mag; |
| 3083 | } |
| 3084 | let near_origin = match class { |
| 3085 | TxClass::TwoD => row < 2 && col < 2, |
| 3086 | TxClass::Horiz => col == 0, |
| 3087 | TxClass::Vert => row == 0, |
| 3088 | }; |
| 3089 | if near_origin { mag + 7 } else { mag + 14 } |
| 3090 | } |
| 3091 | |
| 3092 | /// `Default_Scan_32x16` (spec 8.4.2, libaom `scan.c`'s `default_scan_32x16`): |
| 3093 | /// transposed from libaom's column-major buffer into this decoder's row-major |
| 3094 | /// `pos = row * 32 + col` (lane-rectwire, verified as a bijection of `0..512` |
| 3095 | /// against the real table). Not the square zigzag formula generalized to |
| 3096 | /// unequal `w`/`h` -- checked and that does NOT reproduce this table. |
| 3097 | const SCAN_32X16: [u16; 512] = [ |
| 3098 | 0, 32, 1, 64, 33, 2, 96, 65, 34, 3, 128, 97, 66, 35, 4, 160, 129, 98, 67, 36, 5, 192, 161, |
| 3099 | 130, 99, 68, 37, 6, 224, 193, 162, 131, 100, 69, 38, 7, 256, 225, 194, 163, 132, 101, 70, 39, |
| 3100 | 8, 288, 257, 226, 195, 164, 133, 102, 71, 40, 9, 320, 289, 258, 227, 196, 165, 134, 103, 72, |
| 3101 | 41, 10, 352, 321, 290, 259, 228, 197, 166, 135, 104, 73, 42, 11, 384, 353, 322, 291, 260, 229, |
| 3102 | 198, 167, 136, 105, 74, 43, 12, 416, 385, 354, 323, 292, 261, 230, 199, 168, 137, 106, 75, 44, |
| 3103 | 13, 448, 417, 386, 355, 324, 293, 262, 231, 200, 169, 138, 107, 76, 45, 14, 480, 449, 418, |
| 3104 | 387, 356, 325, 294, 263, 232, 201, 170, 139, 108, 77, 46, 15, 481, 450, 419, 388, 357, 326, |
| 3105 | 295, 264, 233, 202, 171, 140, 109, 78, 47, 16, 482, 451, 420, 389, 358, 327, 296, 265, 234, |
| 3106 | 203, 172, 141, 110, 79, 48, 17, 483, 452, 421, 390, 359, 328, 297, 266, 235, 204, 173, 142, |
| 3107 | 111, 80, 49, 18, 484, 453, 422, 391, 360, 329, 298, 267, 236, 205, 174, 143, 112, 81, 50, 19, |
| 3108 | 485, 454, 423, 392, 361, 330, 299, 268, 237, 206, 175, 144, 113, 82, 51, 20, 486, 455, 424, |
| 3109 | 393, 362, 331, 300, 269, 238, 207, 176, 145, 114, 83, 52, 21, 487, 456, 425, 394, 363, 332, |
| 3110 | 301, 270, 239, 208, 177, 146, 115, 84, 53, 22, 488, 457, 426, 395, 364, 333, 302, 271, 240, |
| 3111 | 209, 178, 147, 116, 85, 54, 23, 489, 458, 427, 396, 365, 334, 303, 272, 241, 210, 179, 148, |
| 3112 | 117, 86, 55, 24, 490, 459, 428, 397, 366, 335, 304, 273, 242, 211, 180, 149, 118, 87, 56, 25, |
| 3113 | 491, 460, 429, 398, 367, 336, 305, 274, 243, 212, 181, 150, 119, 88, 57, 26, 492, 461, 430, |
| 3114 | 399, 368, 337, 306, 275, 244, 213, 182, 151, 120, 89, 58, 27, 493, 462, 431, 400, 369, 338, |
| 3115 | 307, 276, 245, 214, 183, 152, 121, 90, 59, 28, 494, 463, 432, 401, 370, 339, 308, 277, 246, |
| 3116 | 215, 184, 153, 122, 91, 60, 29, 495, 464, 433, 402, 371, 340, 309, 278, 247, 216, 185, 154, |
| 3117 | 123, 92, 61, 30, 496, 465, 434, 403, 372, 341, 310, 279, 248, 217, 186, 155, 124, 93, 62, 31, |
| 3118 | 497, 466, 435, 404, 373, 342, 311, 280, 249, 218, 187, 156, 125, 94, 63, 498, 467, 436, 405, |
| 3119 | 374, 343, 312, 281, 250, 219, 188, 157, 126, 95, 499, 468, 437, 406, 375, 344, 313, 282, 251, |
| 3120 | 220, 189, 158, 127, 500, 469, 438, 407, 376, 345, 314, 283, 252, 221, 190, 159, 501, 470, 439, |
| 3121 | 408, 377, 346, 315, 284, 253, 222, 191, 502, 471, 440, 409, 378, 347, 316, 285, 254, 223, 503, |
| 3122 | 472, 441, 410, 379, 348, 317, 286, 255, 504, 473, 442, 411, 380, 349, 318, 287, 505, 474, 443, |
| 3123 | 412, 381, 350, 319, 506, 475, 444, 413, 382, 351, 507, 476, 445, 414, 383, 508, 477, 446, 415, |
| 3124 | 509, 478, 447, 510, 479, 511, |
| 3125 | ]; |
| 3126 | |
| 3127 | /// `Default_Scan_16x32`, this decoder's own row-major transcription (see |
| 3128 | /// [`SCAN_32X16`]'s doc comment). |
| 3129 | const SCAN_16X32: [u16; 512] = [ |
| 3130 | 0, 1, 16, 2, 17, 32, 3, 18, 33, 48, 4, 19, 34, 49, 64, 5, 20, 35, 50, 65, 80, 6, 21, 36, 51, |
| 3131 | 66, 81, 96, 7, 22, 37, 52, 67, 82, 97, 112, 8, 23, 38, 53, 68, 83, 98, 113, 128, 9, 24, 39, 54, |
| 3132 | 69, 84, 99, 114, 129, 144, 10, 25, 40, 55, 70, 85, 100, 115, 130, 145, 160, 11, 26, 41, 56, 71, |
| 3133 | 86, 101, 116, 131, 146, 161, 176, 12, 27, 42, 57, 72, 87, 102, 117, 132, 147, 162, 177, 192, |
| 3134 | 13, 28, 43, 58, 73, 88, 103, 118, 133, 148, 163, 178, 193, 208, 14, 29, 44, 59, 74, 89, 104, |
| 3135 | 119, 134, 149, 164, 179, 194, 209, 224, 15, 30, 45, 60, 75, 90, 105, 120, 135, 150, 165, 180, |
| 3136 | 195, 210, 225, 240, 31, 46, 61, 76, 91, 106, 121, 136, 151, 166, 181, 196, 211, 226, 241, 256, |
| 3137 | 47, 62, 77, 92, 107, 122, 137, 152, 167, 182, 197, 212, 227, 242, 257, 272, 63, 78, 93, 108, |
| 3138 | 123, 138, 153, 168, 183, 198, 213, 228, 243, 258, 273, 288, 79, 94, 109, 124, 139, 154, 169, |
| 3139 | 184, 199, 214, 229, 244, 259, 274, 289, 304, 95, 110, 125, 140, 155, 170, 185, 200, 215, 230, |
| 3140 | 245, 260, 275, 290, 305, 320, 111, 126, 141, 156, 171, 186, 201, 216, 231, 246, 261, 276, 291, |
| 3141 | 306, 321, 336, 127, 142, 157, 172, 187, 202, 217, 232, 247, 262, 277, 292, 307, 322, 337, 352, |
| 3142 | 143, 158, 173, 188, 203, 218, 233, 248, 263, 278, 293, 308, 323, 338, 353, 368, 159, 174, 189, |
| 3143 | 204, 219, 234, 249, 264, 279, 294, 309, 324, 339, 354, 369, 384, 175, 190, 205, 220, 235, 250, |
| 3144 | 265, 280, 295, 310, 325, 340, 355, 370, 385, 400, 191, 206, 221, 236, 251, 266, 281, 296, 311, |
| 3145 | 326, 341, 356, 371, 386, 401, 416, 207, 222, 237, 252, 267, 282, 297, 312, 327, 342, 357, 372, |
| 3146 | 387, 402, 417, 432, 223, 238, 253, 268, 283, 298, 313, 328, 343, 358, 373, 388, 403, 418, 433, |
| 3147 | 448, 239, 254, 269, 284, 299, 314, 329, 344, 359, 374, 389, 404, 419, 434, 449, 464, 255, 270, |
| 3148 | 285, 300, 315, 330, 345, 360, 375, 390, 405, 420, 435, 450, 465, 480, 271, 286, 301, 316, 331, |
| 3149 | 346, 361, 376, 391, 406, 421, 436, 451, 466, 481, 496, 287, 302, 317, 332, 347, 362, 377, 392, |
| 3150 | 407, 422, 437, 452, 467, 482, 497, 303, 318, 333, 348, 363, 378, 393, 408, 423, 438, 453, 468, |
| 3151 | 483, 498, 319, 334, 349, 364, 379, 394, 409, 424, 439, 454, 469, 484, 499, 335, 350, 365, 380, |
| 3152 | 395, 410, 425, 440, 455, 470, 485, 500, 351, 366, 381, 396, 411, 426, 441, 456, 471, 486, 501, |
| 3153 | 367, 382, 397, 412, 427, 442, 457, 472, 487, 502, 383, 398, 413, 428, 443, 458, 473, 488, 503, |
| 3154 | 399, 414, 429, 444, 459, 474, 489, 504, 415, 430, 445, 460, 475, 490, 505, 431, 446, 461, 476, |
| 3155 | 491, 506, 447, 462, 477, 492, 507, 463, 478, 493, 508, 479, 494, 509, 495, 510, 511, |
| 3156 | ]; |
| 3157 | |
| 3158 | /// `Default_Scan_4x8` (libaom `scan.c`), this decoder's own row-major |
| 3159 | /// transcription (see [`SCAN_32X16`]'s doc comment for the conversion, which |
| 3160 | /// was re-verified this lane by regenerating [`SCAN_16X8`]/[`SCAN_8X16`] from |
| 3161 | /// the same source). |
| 3162 | const SCAN_4X8: [u16; 32] = [ |
| 3163 | 0, 1, 4, 2, 5, 8, 3, 6, 9, 12, 7, 10, 13, 16, 11, 14, 17, 20, 15, 18, 21, 24, 19, 22, 25, 28, |
| 3164 | 23, 26, 29, 27, 30, 31, |
| 3165 | ]; |
| 3166 | |
| 3167 | /// `Default_Scan_8x4`, same source and conversion as [`SCAN_4X8`]. |
| 3168 | const SCAN_8X4: [u16; 32] = [ |
| 3169 | 0, 8, 1, 16, 9, 2, 24, 17, 10, 3, 25, 18, 11, 4, 26, 19, 12, 5, 27, 20, 13, 6, 28, 21, 14, 7, |
| 3170 | 29, 22, 15, 30, 23, 31, |
| 3171 | ]; |
| 3172 | |
| 3173 | /// `Default_Scan_16x8`, this decoder's own row-major transcription (see |
| 3174 | /// [`SCAN_32X16`]'s doc comment). |
| 3175 | const SCAN_16X8: [u16; 128] = [ |
| 3176 | 0, 16, 1, 32, 17, 2, 48, 33, 18, 3, 64, 49, 34, 19, 4, 80, 65, 50, 35, 20, 5, 96, 81, 66, 51, |
| 3177 | 36, 21, 6, 112, 97, 82, 67, 52, 37, 22, 7, 113, 98, 83, 68, 53, 38, 23, 8, 114, 99, 84, 69, 54, |
| 3178 | 39, 24, 9, 115, 100, 85, 70, 55, 40, 25, 10, 116, 101, 86, 71, 56, 41, 26, 11, 117, 102, 87, |
| 3179 | 72, 57, 42, 27, 12, 118, 103, 88, 73, 58, 43, 28, 13, 119, 104, 89, 74, 59, 44, 29, 14, 120, |
| 3180 | 105, 90, 75, 60, 45, 30, 15, 121, 106, 91, 76, 61, 46, 31, 122, 107, 92, 77, 62, 47, 123, 108, |
| 3181 | 93, 78, 63, 124, 109, 94, 79, 125, 110, 95, 126, 111, 127, |
| 3182 | ]; |
| 3183 | |
| 3184 | /// `Default_Scan_8x16`, this decoder's own row-major transcription (see |
| 3185 | /// [`SCAN_32X16`]'s doc comment). |
| 3186 | const SCAN_8X16: [u16; 128] = [ |
| 3187 | 0, 1, 8, 2, 9, 16, 3, 10, 17, 24, 4, 11, 18, 25, 32, 5, 12, 19, 26, 33, 40, 6, 13, 20, 27, 34, |
| 3188 | 41, 48, 7, 14, 21, 28, 35, 42, 49, 56, 15, 22, 29, 36, 43, 50, 57, 64, 23, 30, 37, 44, 51, 58, |
| 3189 | 65, 72, 31, 38, 45, 52, 59, 66, 73, 80, 39, 46, 53, 60, 67, 74, 81, 88, 47, 54, 61, 68, 75, 82, |
| 3190 | 89, 96, 55, 62, 69, 76, 83, 90, 97, 104, 63, 70, 77, 84, 91, 98, 105, 112, 71, 78, 85, 92, 99, |
| 3191 | 106, 113, 120, 79, 86, 93, 100, 107, 114, 121, 87, 94, 101, 108, 115, 122, 95, 102, 109, 116, |
| 3192 | 123, 103, 110, 117, 124, 111, 118, 125, 119, 126, 127, |
| 3193 | ]; |
| 3194 | |
| 3195 | /// `Default_Scan_32x8`, this decoder's own row-major transcription (see |
| 3196 | /// [`SCAN_32X16`]'s doc comment) -- the chroma plane of a 64x16 |
| 3197 | /// `PARTITION_HORZ_4` superblock strip (lane-tx64x16). libaom stores its |
| 3198 | /// scans column-major (`p = col * height + row`), so a naive copy of |
| 3199 | /// `default_scan_32x8` would silently install the TRANSPOSED 8x32 order |
| 3200 | /// (class reference-layout-not-spec): `default_scan_16x8`'s literal bytes |
| 3201 | /// are byte-identical to this decoder's [`SCAN_8X16`]. |
| 3202 | const SCAN_32X8: [u16; 256] = [ |
| 3203 | 0, 32, 1, 64, 33, 2, 96, 65, 34, 3, 128, 97, 66, 35, 4, 160, 129, 98, 67, 36, 5, 192, 161, 130, |
| 3204 | 99, 68, 37, 6, 224, 193, 162, 131, 100, 69, 38, 7, 225, 194, 163, 132, 101, 70, 39, 8, 226, 195, |
| 3205 | 164, 133, 102, 71, 40, 9, 227, 196, 165, 134, 103, 72, 41, 10, 228, 197, 166, 135, 104, 73, 42, |
| 3206 | 11, 229, 198, 167, 136, 105, 74, 43, 12, 230, 199, 168, 137, 106, 75, 44, 13, 231, 200, 169, |
| 3207 | 138, 107, 76, 45, 14, 232, 201, 170, 139, 108, 77, 46, 15, 233, 202, 171, 140, 109, 78, 47, 16, |
| 3208 | 234, 203, 172, 141, 110, 79, 48, 17, 235, 204, 173, 142, 111, 80, 49, 18, 236, 205, 174, 143, |
| 3209 | 112, 81, 50, 19, 237, 206, 175, 144, 113, 82, 51, 20, 238, 207, 176, 145, 114, 83, 52, 21, 239, |
| 3210 | 208, 177, 146, 115, 84, 53, 22, 240, 209, 178, 147, 116, 85, 54, 23, 241, 210, 179, 148, 117, |
| 3211 | 86, 55, 24, 242, 211, 180, 149, 118, 87, 56, 25, 243, 212, 181, 150, 119, 88, 57, 26, 244, 213, |
| 3212 | 182, 151, 120, 89, 58, 27, 245, 214, 183, 152, 121, 90, 59, 28, 246, 215, 184, 153, 122, 91, |
| 3213 | 60, 29, 247, 216, 185, 154, 123, 92, 61, 30, 248, 217, 186, 155, 124, 93, 62, 31, 249, 218, 187, |
| 3214 | 156, 125, 94, 63, 250, 219, 188, 157, 126, 95, 251, 220, 189, 158, 127, 252, 221, 190, 159, 253, |
| 3215 | 222, 191, 254, 223, 255, |
| 3216 | ]; |
| 3217 | /// `Default_Scan_8x32`, [`SCAN_32X8`]'s transpose (the chroma plane of a |
| 3218 | /// 16x64 `PARTITION_VERT_4` strip). |
| 3219 | const SCAN_8X32: [u16; 256] = [ |
| 3220 | 0, 1, 8, 2, 9, 16, 3, 10, 17, 24, 4, 11, 18, 25, 32, 5, 12, 19, 26, 33, 40, 6, 13, 20, 27, 34, |
| 3221 | 41, 48, 7, 14, 21, 28, 35, 42, 49, 56, 15, 22, 29, 36, 43, 50, 57, 64, 23, 30, 37, 44, 51, 58, |
| 3222 | 65, 72, 31, 38, 45, 52, 59, 66, 73, 80, 39, 46, 53, 60, 67, 74, 81, 88, 47, 54, 61, 68, 75, 82, |
| 3223 | 89, 96, 55, 62, 69, 76, 83, 90, 97, 104, 63, 70, 77, 84, 91, 98, 105, 112, 71, 78, 85, 92, 99, |
| 3224 | 106, 113, 120, 79, 86, 93, 100, 107, 114, 121, 128, 87, 94, 101, 108, 115, 122, 129, 136, 95, |
| 3225 | 102, 109, 116, 123, 130, 137, 144, 103, 110, 117, 124, 131, 138, 145, 152, 111, 118, 125, 132, |
| 3226 | 139, 146, 153, 160, 119, 126, 133, 140, 147, 154, 161, 168, 127, 134, 141, 148, 155, 162, 169, |
| 3227 | 176, 135, 142, 149, 156, 163, 170, 177, 184, 143, 150, 157, 164, 171, 178, 185, 192, 151, 158, |
| 3228 | 165, 172, 179, 186, 193, 200, 159, 166, 173, 180, 187, 194, 201, 208, 167, 174, 181, 188, 195, |
| 3229 | 202, 209, 216, 175, 182, 189, 196, 203, 210, 217, 224, 183, 190, 197, 204, 211, 218, 225, 232, |
| 3230 | 191, 198, 205, 212, 219, 226, 233, 240, 199, 206, 213, 220, 227, 234, 241, 248, 207, 214, 221, |
| 3231 | 228, 235, 242, 249, 215, 222, 229, 236, 243, 250, 223, 230, 237, 244, 251, 231, 238, 245, 252, |
| 3232 | 239, 246, 253, 247, 254, 255, |
| 3233 | ]; |
| 3234 | /// `Default_Scan_16x4`, this decoder's own row-major transcription of |
| 3235 | /// libaom's column-major `default_scan_16x4` (`scan.c:66`) -- the CHROMA |
| 3236 | /// plane of a 32x8 `PARTITION_HORZ_4` strip at the 32x32 level |
| 3237 | /// (lane-tx64x16 r3). The transcription rule (`row * w + col` with |
| 3238 | /// `row = p % h`, `col = p / h`) was validated by REGENERATING the existing |
| 3239 | /// [`SCAN_16X8`] from `default_scan_16x8` byte for byte before these two |
| 3240 | /// were emitted -- a naive copy installs the transpose (class |
| 3241 | /// reference-layout-not-spec). |
| 3242 | const SCAN_16X4: [u16; 64] = [ |
| 3243 | 0, 16, 1, 32, 17, 2, 48, 33, 18, 3, 49, 34, 19, 4, 50, 35, 20, 5, 51, 36, 21, 6, 52, 37, 22, 7, |
| 3244 | 53, 38, 23, 8, 54, 39, 24, 9, 55, 40, 25, 10, 56, 41, 26, 11, 57, 42, 27, 12, 58, 43, 28, 13, |
| 3245 | 59, 44, 29, 14, 60, 45, 30, 15, 61, 46, 31, 62, 47, 63, |
| 3246 | ]; |
| 3247 | /// `Default_Scan_4x16`, [`SCAN_16X4`]'s transpose (the chroma plane of an |
| 3248 | /// 8x32 `PARTITION_VERT_4` strip). |
| 3249 | const SCAN_4X16: [u16; 64] = [ |
| 3250 | 0, 1, 4, 2, 5, 8, 3, 6, 9, 12, 7, 10, 13, 16, 11, 14, 17, 20, 15, 18, 21, 24, 19, 22, 25, 28, |
| 3251 | 23, 26, 29, 32, 27, 30, 33, 36, 31, 34, 37, 40, 35, 38, 41, 44, 39, 42, 45, 48, 43, 46, 49, 52, |
| 3252 | 47, 50, 53, 56, 51, 54, 57, 60, 55, 58, 61, 59, 62, 63, |
| 3253 | ]; |
| 3254 | |
| 3255 | /// [`neighbour`] with independent `w` (row stride)/`h` (row-bound) extents |
| 3256 | /// (lane-rectwire, mirrors [`record_rect`](Neighbours::record_rect)'s |
| 3257 | /// asymmetric-extent pattern). |
| 3258 | fn neighbour_rect(grid: &[i32], w: usize, h: usize, row: usize, col: usize) -> i32 { |
| 3259 | if row >= h || col >= w { |
| 3260 | 0 |
| 3261 | } else { |
| 3262 | grid[row * w + col] |
| 3263 | } |
| 3264 | } |
| 3265 | |
| 3266 | /// [`base_ctx`]'s `TxClass::TwoD` arm widened to `(w, h)` (lane-rectwire): |
| 3267 | /// the rect generalization is libaom's own comment in |
| 3268 | /// `get_nz_map_ctx_from_stats` (`txb_common.h:189-224`), reduces to the exact |
| 3269 | /// same [`cdf::NZ_MAP_CTX_OFFSET_32`] table when `w == h`. Never called for |
| 3270 | /// `V_DCT`/`H_DCT` -- geometrically impossible at the two size pairs |
| 3271 | /// [`decode_block_rect`] codes (see that function's own doc comment). |
| 3272 | fn base_ctx_rect( |
| 3273 | grid: &[i32], |
| 3274 | w: usize, |
| 3275 | h: usize, |
| 3276 | row: usize, |
| 3277 | col: usize, |
| 3278 | class: TxClass, |
| 3279 | ) -> usize { |
| 3280 | // Same `(tx_class | coeff_idx) == 0` guard [`base_ctx`] documents: only |
| 3281 | // the 2D class short-circuits the DC position. |
| 3282 | if class == TxClass::TwoD && row == 0 && col == 0 { |
| 3283 | return 0; |
| 3284 | } |
| 3285 | let offsets: [(usize, usize); 5] = match class { |
| 3286 | TxClass::TwoD => [(1, 0), (0, 1), (1, 1), (2, 0), (0, 2)], |
| 3287 | TxClass::Horiz => [(1, 0), (0, 1), (0, 2), (0, 3), (0, 4)], |
| 3288 | TxClass::Vert => [(1, 0), (0, 1), (2, 0), (3, 0), (4, 0)], |
| 3289 | }; |
| 3290 | let mag: i32 = offsets |
| 3291 | .iter() |
| 3292 | .map(|&(dr, dc)| neighbour_rect(grid, w, h, row + dr, col + dc).abs().min(3)) |
| 3293 | .sum(); |
| 3294 | let ctx = ((mag + 1) >> 1).min(4) as usize; |
| 3295 | match class { |
| 3296 | TxClass::TwoD => { |
| 3297 | if w < h && row < 2 { |
| 3298 | return ctx + 11; |
| 3299 | } |
| 3300 | if w > h && col < 2 { |
| 3301 | return ctx + 16; |
| 3302 | } |
| 3303 | ctx + cdf::NZ_MAP_CTX_OFFSET_32[row.min(4)][col.min(4)] as usize |
| 3304 | } |
| 3305 | // Shape-independent in libaom too (`get_nz_map_ctx_from_stats`'s |
| 3306 | // `TX_CLASS_HORIZ`/`VERT` arms index `nz_map_ctx_offset_1d` by the |
| 3307 | // position along one axis only). |
| 3308 | TxClass::Horiz => ctx + nz_map_ctx_offset_1d(col.min(31)), |
| 3309 | TxClass::Vert => ctx + nz_map_ctx_offset_1d(row.min(31)), |
| 3310 | } |
| 3311 | } |
| 3312 | |
| 3313 | /// [`br_ctx`]'s `TxClass::TwoD` arm widened to `(w, h)` (lane-rectwire): the |
| 3314 | /// neighbour-offset math itself is shape-independent, only the boundary |
| 3315 | /// clamp needs the real bound in each axis. |
| 3316 | fn br_ctx_rect( |
| 3317 | grid: &[i32], |
| 3318 | w: usize, |
| 3319 | h: usize, |
| 3320 | row: usize, |
| 3321 | col: usize, |
| 3322 | class: TxClass, |
| 3323 | ) -> usize { |
| 3324 | let extra = match class { |
| 3325 | TxClass::TwoD => neighbour_rect(grid, w, h, row + 1, col + 1).abs(), |
| 3326 | TxClass::Horiz => neighbour_rect(grid, w, h, row, col + 2).abs(), |
| 3327 | TxClass::Vert => neighbour_rect(grid, w, h, row + 2, col).abs(), |
| 3328 | }; |
| 3329 | let mag = neighbour_rect(grid, w, h, row + 1, col).abs() |
| 3330 | + neighbour_rect(grid, w, h, row, col + 1).abs() |
| 3331 | + extra; |
| 3332 | let mag = (((mag + 1) >> 1).min(6)) as usize; |
| 3333 | if row == 0 && col == 0 { |
| 3334 | return mag; |
| 3335 | } |
| 3336 | let near_origin = match class { |
| 3337 | TxClass::TwoD => row < 2 && col < 2, |
| 3338 | TxClass::Horiz => col == 0, |
| 3339 | TxClass::Vert => row == 0, |
| 3340 | }; |
| 3341 | if near_origin { mag + 7 } else { mag + 14 } |
| 3342 | } |
| 3343 | |
| 3344 | fn dc_vote(dc: Option<bool>) -> i32 { |
| 3345 | match dc { |
| 3346 | None => 0, |
| 3347 | Some(true) => -1, |
| 3348 | Some(false) => 1, |
| 3349 | } |
| 3350 | } |
| 3351 | |
| 3352 | fn dc_sign_ctx(vote: i32) -> usize { |
| 3353 | match vote.signum() { |
| 3354 | 0 => 0, |
| 3355 | -1 => 1, |
| 3356 | _ => 2, |
| 3357 | } |
| 3358 | } |
| 3359 | |
| 3360 | /// The remainder of a level the base and base-range syntax could not reach |
| 3361 | /// (spec 5.11.40): its bit length in unary, then that many of its own bits, |
| 3362 | /// most significant first — the exact inverse of [`crate::tile::write_golomb`]. |
| 3363 | fn read_golomb(dec: &mut SymbolDecoder) -> Result<u32> { |
| 3364 | // lane-scaledref r1: this cap MASKS A REAL DEFECT and must not be lifted |
| 3365 | // on its own. Reading up to 32 leading zeros (dav1d's `len < 32`; libaom |
| 3366 | // itself calls a 21st bit a corrupt frame, decodetxb.c:30) is bit-identical |
| 3367 | // for every tail any encoder writes -- our own writer tops out at 19 zeros |
| 3368 | // (`tile::MAX_LEVEL == MAX_BR_LEVEL + (1 << 19)`) -- and was implemented |
| 3369 | // and round-trip tested here (commit ee1f980, reverted in 314ee08). It |
| 3370 | // turned `a_real_aomenc_stream_with_a_superblock_level_horz_vert_partition_decodes_pixel_exact` |
| 3371 | // RED: seed 67 stops here today, and with the cap raised it decodes to a |
| 3372 | // frame-0 LUMA MISMATCH instead. A key frame cannot legitimately carry a |
| 3373 | // level above 1<<19, so the long tail is the SYMPTOM of an earlier desync |
| 3374 | // in that intra rect64 stream -- class `refusal-hides-a-defect`. Lift this |
| 3375 | // together with that defect's fix, not before. |
| 3376 | let mut length = 1u32; |
| 3377 | while dec.literal(1) == 0 { |
| 3378 | length += 1; |
| 3379 | if length > 20 { |
| 3380 | return Err(unsupported("a Golomb tail longer than this decoder reads")); |
| 3381 | } |
| 3382 | } |
| 3383 | let mut x = 1u32; |
| 3384 | for _ in 1..length { |
| 3385 | x = (x << 1) | dec.literal(1); |
| 3386 | } |
| 3387 | Ok(x - 1) |
| 3388 | } |
| 3389 | |
| 3390 | /// `decode_symbol`'s end-of-block position (spec 5.11.39): the inverse of |
| 3391 | /// [`crate::tile`]'s `write_eob`. |
| 3392 | fn read_eob(dec: &mut SymbolDecoder, coding: &mut TxbTables, class: TxClass) -> usize { |
| 3393 | const GROUP_START: [usize; 12] = [0, 1, 2, 3, 5, 9, 17, 33, 65, 129, 257, 513]; |
| 3394 | const OFFSET_BITS: [u32; 12] = [0, 0, 0, 1, 2, 3, 4, 5, 6, 7, 8, 9]; |
| 3395 | |
| 3396 | let trace = std::env::var_os("EC_AV1_TRACE").is_some(); |
| 3397 | // libaom's `eob_flag_cdf*` tables carry a second dimension the 2D scan |
| 3398 | // never touches: `TX_CLASS_HORIZ`/`TX_CLASS_VERT` (`V_DCT`/`H_DCT`) adapt |
| 3399 | // a wholly separate CDF from every other tx_type (`decodetxb.c`'s |
| 3400 | // `eob_multi_ctx`). Missing that split desynced the very first |
| 3401 | // `V_DCT`/`H_DCT` TU this decoder ever produced (lane-av1tx4 r5, caught |
| 3402 | // against a real aomdec trace). |
| 3403 | let eob_pt: &mut [u16] = match (class, coding.eob_pt_class1.as_deref_mut()) { |
| 3404 | (TxClass::TwoD, _) | (_, None) => coding.eob_pt, |
| 3405 | (_, Some(class1)) => class1, |
| 3406 | }; |
| 3407 | if std::env::var_os("EC_AV1_EOBPT_CDF").is_some() { |
| 3408 | let (range, value) = dec.debug_state(); |
| 3409 | eprintln!("EC_AV1_EOBPT_CDF {eob_pt:?} range={range} value={value}"); |
| 3410 | } |
| 3411 | let group = dec.symbol(eob_pt) + 1; |
| 3412 | if trace { |
| 3413 | eprintln!("TRACE eob_pt value={group} rng={}", dec.debug_state().0); |
| 3414 | } |
| 3415 | let bits = OFFSET_BITS[group]; |
| 3416 | let mut offset = 0u32; |
| 3417 | if bits > 0 { |
| 3418 | let top = dec.symbol(&mut coding.eob_extra[group - 3]) as u32; |
| 3419 | if trace { |
| 3420 | eprintln!("TRACE eob_extra ctx={} value={top}", group - 3); |
| 3421 | } |
| 3422 | offset = top << (bits - 1); |
| 3423 | if bits > 1 { |
| 3424 | offset |= dec.literal(bits - 1); |
| 3425 | } |
| 3426 | } |
| 3427 | GROUP_START[group] + offset as usize |
| 3428 | } |
| 3429 | |
| 3430 | /// One transform block's levels, the inverse of [`crate::tile`]'s |
| 3431 | /// `write_coeffs`, returned as a `side * side` grid in raster order, along |
| 3432 | /// with the transform type that grid was coded under (`DCT_DCT` when the |
| 3433 | /// block's `TxbSet` carries no `tx_type` symbol at all, e.g. 32-point and |
| 3434 | /// 64-point transforms, which never code anything else). |
| 3435 | /// |
| 3436 | /// # Errors |
| 3437 | /// Returns an error if the `tx_type` symbol decodes to a value outside its |
| 3438 | /// CDF's own set (a corrupt or unsupported bitstream) — every value the |
| 3439 | /// intra `Tx_Type_Intra_Inv_Set2` and inter `Tx_Type_Inter_Inv_Set3` CDFs can |
| 3440 | /// produce dispatches to a real inverse transform. |
| 3441 | /// `Intra_Mode_To_Tx_Type` (spec 9.3): chroma's transform type is never its |
| 3442 | /// own coded symbol -- `TxbSet::Chroma*`'s `tx_type` slot is always `None` |
| 3443 | /// (a chroma block shares no CDF row with `Luma16`'s mode-indexed one) -- so |
| 3444 | /// libaom's own `intra_mode_to_tx_type` (`blockd.h`) derives it purely from |
| 3445 | /// the plane's own predicted mode instead. Every entry this table can |
| 3446 | /// produce is a member of both `TX_SET_INTRA_1` and `_2` (spec 9.3's two |
| 3447 | /// intra sets differ only in `IDTX`/`V_DCT`/`H_DCT`, none of which this |
| 3448 | /// table ever names), so unlike the CDF-coded luma path there is no |
| 3449 | /// ext-tx-set membership check to fall back from here. |
| 3450 | fn default_intra_tx_type(mode: u8) -> TxType { |
| 3451 | use crate::intra::{ |
| 3452 | D45_PRED, D67_PRED, D113_PRED, D135_PRED, D157_PRED, D203_PRED, DC_PRED, H_PRED, |
| 3453 | PAETH_PRED, SMOOTH_H_PRED, SMOOTH_PRED, SMOOTH_V_PRED, V_PRED, |
| 3454 | }; |
| 3455 | match mode { |
| 3456 | DC_PRED | D45_PRED => TxType::DctDct, |
| 3457 | V_PRED | D113_PRED | D67_PRED | SMOOTH_V_PRED => TxType::AdstDct, |
| 3458 | H_PRED | D157_PRED | D203_PRED | SMOOTH_H_PRED => TxType::DctAdst, |
| 3459 | D135_PRED | SMOOTH_PRED | PAETH_PRED => TxType::AdstAdst, |
| 3460 | other => panic!("intra mode {other} has no Intra_Mode_To_Tx_Type entry"), |
| 3461 | } |
| 3462 | } |
| 3463 | |
| 3464 | /// `is_smooth` (libaom `reconintra.c`): true for `SMOOTH_PRED..=SMOOTH_H_PRED` |
| 3465 | /// (9..=11) — deliberately excludes `PAETH_PRED` (12), which the spec's own |
| 3466 | /// `get_intra_edge_filter_type`/libaom `get_filt_type` never treat as smooth. |
| 3467 | fn is_smooth_mode(mode: usize) -> bool { |
| 3468 | (crate::intra::SMOOTH_PRED as usize..=crate::intra::SMOOTH_H_PRED as usize).contains(&mode) |
| 3469 | } |
| 3470 | |
| 3471 | fn read_coeffs( |
| 3472 | dec: &mut SymbolDecoder, |
| 3473 | coding: &mut TxbTables, |
| 3474 | scan: &[u16], |
| 3475 | skip_ctx: usize, |
| 3476 | sign_ctx: usize, |
| 3477 | // The chroma default (spec `Intra_Mode_To_Tx_Type`, see |
| 3478 | // [`default_intra_tx_type`]) for the sizes where `coding.tx_type` codes |
| 3479 | // nothing at all; `DctDct` for every luma call and every chroma call at |
| 3480 | // a size the spec forces to `DCT_DCT` regardless of mode (32-point and |
| 3481 | // up, `EXT_TX_SET_DCTONLY`) -- the caller already folds that in. |
| 3482 | default_tx_type: TxType, |
| 3483 | // `Some((w, h))` with `w != h` when this read is a superblock-level |
| 3484 | // HORZ/VERT strip's truncated luma corner: the CDF *set* resolves square |
| 3485 | // (`side`/`coding` above), but `av1_nz_map_ctx_offset`'s position table |
| 3486 | // is indexed by the real, un-adjusted rectangular shape (lane-sbpart r8 |
| 3487 | // root cause, see [`base_ctx`]). `None` everywhere else -- every other |
| 3488 | // caller's block genuinely is `side` x `side`. |
| 3489 | rect_shape: Option<(usize, usize)>, |
| 3490 | ) -> Result<(Vec<i32>, TxType)> { |
| 3491 | let trace = std::env::var_os("EC_AV1_TRACE").is_some(); |
| 3492 | let side = coding.side; |
| 3493 | let mut grid = vec![0i32; side * side]; |
| 3494 | if std::env::var_os("EC_AV1_EOBPT_CDF").is_some() { |
| 3495 | let (range, value) = dec.debug_state(); |
| 3496 | eprintln!("EC_AV1_STATE_BEFORE_TXBSKIP range={range} value={value}"); |
| 3497 | } |
| 3498 | let all_zero = dec.symbol(&mut coding.txb_skip[skip_ctx]) == 1; |
| 3499 | if std::env::var_os("EC_TRACE_COEFF").is_some() { |
| 3500 | let (rng, _) = dec.debug_state(); |
| 3501 | eprintln!( |
| 3502 | "EC_COEFF_STEP tag=all_zero ctx={skip_ctx} all_zero={} rng={rng}", |
| 3503 | all_zero as i32 |
| 3504 | ); |
| 3505 | } |
| 3506 | if std::env::var_os("EC_AV1_TELL").is_some() { |
| 3507 | eprintln!( |
| 3508 | "TELL label=post_txb_skip ctx={skip_ctx} all_zero={} tell={} range={}", |
| 3509 | all_zero as u8, dec.debug_bitpos(), dec.debug_state().0 |
| 3510 | ); |
| 3511 | } |
| 3512 | if trace { |
| 3513 | eprintln!("TRACE all_zero ctx={skip_ctx} value={}", all_zero as i32); |
| 3514 | } |
| 3515 | if std::env::var_os("EC_AV1_EOBPT_CDF").is_some() { |
| 3516 | let (range, value) = dec.debug_state(); |
| 3517 | eprintln!("EC_AV1_STATE_AFTER_TXBSKIP range={range} value={value}"); |
| 3518 | } |
| 3519 | if all_zero { |
| 3520 | return Ok((grid, TxType::DctDct)); |
| 3521 | } |
| 3522 | let mut tx_type = default_tx_type; |
| 3523 | if let Some(tx_type_cdf) = coding.tx_type.as_deref_mut() { |
| 3524 | // The CDF row's own width names its set (lane-cdffwd2: extended |
| 3525 | // past the original two-way intra split once wider `reduced_tx_set |
| 3526 | // == 0` inter sets joined the table): 17 slots (16 symbols) is |
| 3527 | // `EXT_TX_SET_ALL16`, 13 (12 symbols) `EXT_TX_SET_DTT9_IDTX_1DDCT`, |
| 3528 | // 8 (7 symbols) `TX_SET_INTRA_1`, everything else (6 slots, 5 |
| 3529 | // symbols) the reduced `TX_SET_INTRA_2`/`TX_SET_INTER_3` two sets |
| 3530 | // share their symbol order with (`Tx_Type_Inter_Inv_Set3`'s two |
| 3531 | // members are a prefix of `Tx_Type_Intra_Inv_Set2`'s five). |
| 3532 | let len = tx_type_cdf.len(); |
| 3533 | if std::env::var_os("EC_AV1_EOBPT_CDF").is_some() && len == 3 { |
| 3534 | eprintln!("EC_AV1_TXTYPE32_CDF {tx_type_cdf:?}"); |
| 3535 | } |
| 3536 | let t = dec.symbol(tx_type_cdf); |
| 3537 | if std::env::var_os("EC_TRACE_COEFF").is_some() { |
| 3538 | let (rng, _) = dec.debug_state(); |
| 3539 | eprintln!("EC_COEFF_STEP tag=tx_type rng={rng}"); |
| 3540 | } |
| 3541 | if std::env::var_os("EC_AV1_EOBPT_CDF").is_some() { |
| 3542 | let (range, value) = dec.debug_state(); |
| 3543 | eprintln!("EC_AV1_STATE_AFTER_TXTYPE range={range} value={value}"); |
| 3544 | } |
| 3545 | if trace { |
| 3546 | eprintln!("TRACE tx_type value={t} len={len}"); |
| 3547 | } |
| 3548 | tx_type = match len { |
| 3549 | 17 => TxType::from_symbol_all16(t), |
| 3550 | 13 => TxType::from_symbol_set2_12(t), |
| 3551 | 8 => TxType::from_symbol_set1(t), |
| 3552 | _ => TxType::from_symbol(t), |
| 3553 | } |
| 3554 | .ok_or_else(|| unsupported(format!("a tx_type symbol outside its CDF's own set: {t}")))?; |
| 3555 | } |
| 3556 | |
| 3557 | let class = TxClass::of(tx_type); |
| 3558 | if class != TxClass::TwoD { |
| 3559 | TX_CLASS1_HITS.with(|c| c.set(c.get() + 1)); |
| 3560 | RECT_CLASS1_HITS.with(|c| c.set(c.get() + 1)); |
| 3561 | } |
| 3562 | let eob = read_eob(dec, coding, class); |
| 3563 | if trace { |
| 3564 | eprintln!("TRACE eob value={eob}"); |
| 3565 | } |
| 3566 | let ec_trace_coeff = std::env::var_os("EC_TRACE_COEFF").is_some(); |
| 3567 | if ec_trace_coeff { |
| 3568 | let (rng, _) = dec.debug_state(); |
| 3569 | let cls = match class { TxClass::TwoD => "2d", TxClass::Horiz => "horiz", TxClass::Vert => "vert" }; |
| 3570 | eprintln!("EC_COEFF_STEP tag=eob eob={eob} tx={tx_type:?} class={cls} rng={rng}"); |
| 3571 | } |
| 3572 | let class_scan; |
| 3573 | let scan: &[u16] = if class == TxClass::TwoD { |
| 3574 | scan |
| 3575 | } else { |
| 3576 | class_scan = class_scan_table(side, class); |
| 3577 | &class_scan |
| 3578 | }; |
| 3579 | let mut levels = vec![0i32; side * side]; |
| 3580 | for scan_idx in (0..eob).rev() { |
| 3581 | let pos = scan[scan_idx] as usize; |
| 3582 | let (row, col) = (pos / side, pos % side); |
| 3583 | let level = if scan_idx == eob - 1 { |
| 3584 | let ctx = eob_coeff_ctx(scan_idx, side * side); |
| 3585 | let v = dec.symbol(&mut coding.base_eob[ctx]) as i32 + 1; |
| 3586 | if trace { |
| 3587 | eprintln!( |
| 3588 | "TRACE base_eob scan_idx={scan_idx} pos={pos} row={row} col={col} ctx={ctx} value={v}" |
| 3589 | ); |
| 3590 | } |
| 3591 | v |
| 3592 | } else { |
| 3593 | let ctx = base_ctx(&levels, side, row, col, class, rect_shape); |
| 3594 | let v = dec.symbol(&mut coding.base[ctx]) as i32; |
| 3595 | if ec_trace_coeff { |
| 3596 | let (rng, _) = dec.debug_state(); |
| 3597 | // `pos` in libaom's column-major `coeff_idx` convention, so |
| 3598 | // the ladder lines up with instrumented `aomdec`. |
| 3599 | let apos = col * side + row; |
| 3600 | eprintln!( |
| 3601 | "EC_COEFF_STEP tag=base c={scan_idx} pos={apos} ctx={ctx} level={v} rng={rng}" |
| 3602 | ); |
| 3603 | } |
| 3604 | if trace { |
| 3605 | eprintln!( |
| 3606 | "TRACE base scan_idx={scan_idx} pos={pos} row={row} col={col} ctx={ctx} value={v}" |
| 3607 | ); |
| 3608 | } |
| 3609 | v |
| 3610 | }; |
| 3611 | let level = if level > NUM_BASE_LEVELS { |
| 3612 | let ctx = br_ctx(&levels, side, row, col, class); |
| 3613 | let mut level = level; |
| 3614 | let mut sent = 0; |
| 3615 | loop { |
| 3616 | let k = dec.symbol(&mut coding.br[ctx]) as i32; |
| 3617 | if trace { |
| 3618 | eprintln!( |
| 3619 | "TRACE br scan_idx={scan_idx} pos={pos} row={row} col={col} ctx={ctx} value={k}" |
| 3620 | ); |
| 3621 | } |
| 3622 | level += k; |
| 3623 | sent += BR_STEP; |
| 3624 | if k < BR_STEP || sent >= COEFF_BASE_RANGE { |
| 3625 | break; |
| 3626 | } |
| 3627 | } |
| 3628 | level |
| 3629 | } else { |
| 3630 | level |
| 3631 | }; |
| 3632 | if ec_trace_coeff && scan_idx == eob - 1 { |
| 3633 | let (rng, _) = dec.debug_state(); |
| 3634 | eprintln!("EC_COEFF_STEP tag=base_eob level={level} rng={rng}"); |
| 3635 | } |
| 3636 | levels[pos] = level; |
| 3637 | } |
| 3638 | if ec_trace_coeff { |
| 3639 | let (rng, _) = dec.debug_state(); |
| 3640 | eprintln!("EC_COEFF_STEP tag=after_bases rng={rng}"); |
| 3641 | } |
| 3642 | |
| 3643 | for (c, &pos) in scan[..eob].iter().enumerate() { |
| 3644 | let level = levels[pos as usize]; |
| 3645 | if level == 0 { |
| 3646 | continue; |
| 3647 | } |
| 3648 | let negative = if pos == 0 { |
| 3649 | let v = dec.symbol(&mut coding.dc_sign[sign_ctx]) == 1; |
| 3650 | if trace { |
| 3651 | eprintln!("TRACE dc_sign ctx={sign_ctx} value={}", v as i32); |
| 3652 | } |
| 3653 | v |
| 3654 | } else { |
| 3655 | dec.literal(1) == 1 |
| 3656 | }; |
| 3657 | if ec_trace_coeff { |
| 3658 | let (rng, _) = dec.debug_state(); |
| 3659 | eprintln!("EC_COEFF_STEP tag=sign c={c} sign={} rng={rng}", negative as i32); |
| 3660 | } |
| 3661 | let level = if level.abs_diff(0) as i32 > MAX_BR_LEVEL { |
| 3662 | let g = read_golomb(dec)?; |
| 3663 | if trace { |
| 3664 | eprintln!("TRACE golomb pos={pos} value={g}"); |
| 3665 | } |
| 3666 | level + g as i32 |
| 3667 | } else { |
| 3668 | level |
| 3669 | }; |
| 3670 | if ec_trace_coeff { |
| 3671 | let (rng, _) = dec.debug_state(); |
| 3672 | eprintln!("EC_COEFF_STEP tag=post_golomb c={c} level={level} rng={rng}"); |
| 3673 | } |
| 3674 | grid[pos as usize] = if negative { -level } else { level }; |
| 3675 | } |
| 3676 | Ok((grid, tx_type)) |
| 3677 | } |
| 3678 | |
| 3679 | /// [`read_coeffs`] widened to `(w, h)` (lane-rectwire; lane-rectx added the |
| 3680 | /// `tx_type` read [`TxbSet::LumaRect16x8`] needs), restricted to |
| 3681 | /// `TxClass::TwoD` -- the only class any rect size pair this decoder codes |
| 3682 | /// can ever produce, since every `tx_type` this decoder's rect sets can read |
| 3683 | /// (`EXT_TX_SET_DTT4_IDTX`'s five members) is itself 2D: refuses by name |
| 3684 | /// rather than guess-decode if a non-2D class ever shows up here, since |
| 3685 | /// neither `base_ctx_rect`/`br_ctx_rect` nor [`class_scan_table`] have a rect |
| 3686 | /// form for those. |
| 3687 | fn read_coeffs_rect( |
| 3688 | dec: &mut SymbolDecoder, |
| 3689 | coding: &mut TxbTables, |
| 3690 | scan: &[u16], |
| 3691 | w: usize, |
| 3692 | h: usize, |
| 3693 | skip_ctx: usize, |
| 3694 | sign_ctx: usize, |
| 3695 | default_tx_type: TxType, |
| 3696 | ) -> Result<(Vec<i32>, TxType)> { |
| 3697 | let rect_trace = std::env::var_os("EC_TRACE_COEFF").is_some(); |
| 3698 | let mut grid = vec![0i32; w * h]; |
| 3699 | let entry_rng = dec.debug_state().0; |
| 3700 | let all_zero = dec.symbol(&mut coding.txb_skip[skip_ctx]) == 1; |
| 3701 | if rect_trace { |
| 3702 | eprintln!( |
| 3703 | "EC_COEFF_STEP tag=all_zero plane=0 ctx={skip_ctx} entry={entry_rng} all_zero={} rng={}", |
| 3704 | all_zero as i32, |
| 3705 | dec.debug_state().0 |
| 3706 | ); |
| 3707 | } |
| 3708 | if all_zero { |
| 3709 | return Ok((grid, TxType::DctDct)); |
| 3710 | } |
| 3711 | RECT_COEFF_TU_HITS.with(|c| c.set(c.get() + 1)); |
| 3712 | // lane-tx4x8: a 4x8/8x4 luma TU DOES carry a `tx_type` symbol (its set is |
| 3713 | // `EXT_TX_SET_DTT4_IDTX(_1DDCT)`, `tx_size_sqr_up == TX_8X8`), including |
| 3714 | // the `V_DCT`/`H_DCT` members whose 1D tx class needs its own scan and |
| 3715 | // context offsets -- both of which now have rect forms |
| 3716 | // ([`class_scan_table_wh`], [`base_ctx_rect`]/[`br_ctx_rect`]'s `class`). |
| 3717 | // The rect sets that carry no symbol at all still pass `None` here. |
| 3718 | let mut tx_type = default_tx_type; |
| 3719 | if let Some(tx_type_cdf) = coding.tx_type.as_deref_mut() { |
| 3720 | let len = tx_type_cdf.len(); |
| 3721 | let t = dec.symbol(tx_type_cdf); |
| 3722 | tx_type = match len { |
| 3723 | 17 => TxType::from_symbol_all16(t), |
| 3724 | 13 => TxType::from_symbol_set2_12(t), |
| 3725 | 8 => TxType::from_symbol_set1(t), |
| 3726 | _ => TxType::from_symbol(t), |
| 3727 | } |
| 3728 | .ok_or_else(|| unsupported(format!("a tx_type symbol outside its CDF's own set: {t}")))?; |
| 3729 | if rect_trace { |
| 3730 | eprintln!("EC_COEFF_STEP tag=tx_type plane=0 rng={}", dec.debug_state().0); |
| 3731 | } |
| 3732 | } |
| 3733 | let class = TxClass::of(tx_type); |
| 3734 | if class != TxClass::TwoD { |
| 3735 | TX_CLASS1_HITS.with(|c| c.set(c.get() + 1)); |
| 3736 | } |
| 3737 | let eob = read_eob(dec, coding, class); |
| 3738 | if rect_trace { |
| 3739 | eprintln!("EC_COEFF_STEP tag=eob eob={eob} rng={}", dec.debug_state().0); |
| 3740 | } |
| 3741 | let class_scan; |
| 3742 | let scan: &[u16] = if class == TxClass::TwoD { |
| 3743 | scan |
| 3744 | } else { |
| 3745 | class_scan = class_scan_table_wh(w, h, class); |
| 3746 | &class_scan |
| 3747 | }; |
| 3748 | let mut levels = vec![0i32; w * h]; |
| 3749 | for scan_idx in (0..eob).rev() { |
| 3750 | let pos = scan[scan_idx] as usize; |
| 3751 | let (row, col) = (pos / w, pos % w); |
| 3752 | // libaom's column-major `coeff_idx`, for ladder comparison. |
| 3753 | let apos = col * h + row; |
| 3754 | let level = if scan_idx == eob - 1 { |
| 3755 | let ctx = eob_coeff_ctx(scan_idx, w * h); |
| 3756 | let v = dec.symbol(&mut coding.base_eob[ctx]) as i32 + 1; |
| 3757 | if rect_trace { |
| 3758 | eprintln!( |
| 3759 | "EC_COEFF_STEP tag=base_eob c={scan_idx} pos={pos} ctx={ctx} level={v} rng={}", |
| 3760 | dec.debug_state().0 |
| 3761 | ); |
| 3762 | } |
| 3763 | v |
| 3764 | } else { |
| 3765 | let ctx = base_ctx_rect(&levels, w, h, row, col, class); |
| 3766 | let v = dec.symbol(&mut coding.base[ctx]) as i32; |
| 3767 | if rect_trace { |
| 3768 | eprintln!( |
| 3769 | "EC_COEFF_STEP tag=base c={scan_idx} pos={pos} ctx={ctx} level={v} rng={}", |
| 3770 | dec.debug_state().0 |
| 3771 | ); |
| 3772 | } |
| 3773 | v |
| 3774 | }; |
| 3775 | let level = if level > NUM_BASE_LEVELS { |
| 3776 | let ctx = br_ctx_rect(&levels, w, h, row, col, class); |
| 3777 | let mut level = level; |
| 3778 | let mut sent = 0; |
| 3779 | loop { |
| 3780 | let k = dec.symbol(&mut coding.br[ctx]) as i32; |
| 3781 | if rect_trace { |
| 3782 | eprintln!( |
| 3783 | "EC_COEFF_STEP tag=br c={scan_idx} pos={pos} ctx={ctx} k={k} rng={}", |
| 3784 | dec.debug_state().0 |
| 3785 | ); |
| 3786 | } |
| 3787 | level += k; |
| 3788 | sent += BR_STEP; |
| 3789 | if k < BR_STEP || sent >= COEFF_BASE_RANGE { |
| 3790 | break; |
| 3791 | } |
| 3792 | } |
| 3793 | level |
| 3794 | } else { |
| 3795 | level |
| 3796 | }; |
| 3797 | levels[pos] = level; |
| 3798 | if rect_trace { |
| 3799 | let (rng, _) = dec.debug_state(); |
| 3800 | eprintln!("EC_COEFF_STEP tag=base c={scan_idx} pos={pos} level={level} rng={rng}"); |
| 3801 | } |
| 3802 | } |
| 3803 | if rect_trace { |
| 3804 | let (rng, _) = dec.debug_state(); |
| 3805 | eprintln!("EC_COEFF_STEP tag=after_bases rng={rng}"); |
| 3806 | } |
| 3807 | for &pos in &scan[..eob] { |
| 3808 | let level = levels[pos as usize]; |
| 3809 | if level == 0 { |
| 3810 | continue; |
| 3811 | } |
| 3812 | let negative = if pos == 0 { |
| 3813 | dec.symbol(&mut coding.dc_sign[sign_ctx]) == 1 |
| 3814 | } else { |
| 3815 | dec.literal(1) == 1 |
| 3816 | }; |
| 3817 | if rect_trace { |
| 3818 | let (rng, _) = dec.debug_state(); |
| 3819 | eprintln!("EC_COEFF_STEP tag=sign_rect pos={pos} sign={} rng={rng}", u8::from(negative)); |
| 3820 | } |
| 3821 | let level = if level.abs_diff(0) as i32 > MAX_BR_LEVEL { |
| 3822 | let g = read_golomb(dec)?; |
| 3823 | level + g as i32 |
| 3824 | } else { |
| 3825 | level |
| 3826 | }; |
| 3827 | grid[pos as usize] = if negative { -level } else { level }; |
| 3828 | } |
| 3829 | Ok((grid, tx_type)) |
| 3830 | } |
| 3831 | |
| 3832 | /// What one coded block leaves behind for the blocks that read it as a |
| 3833 | /// neighbour: whether it coded anything at all, and the sign of its DC — |
| 3834 | /// [`crate::tile`]'s own private `Neighbour`. |
| 3835 | #[derive(Clone, Copy, Default)] |
| 3836 | struct Neighbour { |
| 3837 | /// The transform unit's cumulative coefficient level (spec `cul_level`, |
| 3838 | /// `decodetxb.c`'s `read_coeffs_txb`): the sum of every coded level's |
| 3839 | /// magnitude, clamped to `COEFF_CONTEXT_MASK` (7). A plain "coded or not" |
| 3840 | /// boolean (`level != 0`) is enough for every context this decoder read |
| 3841 | /// until now, but the *luma* `txb_skip_ctx` of a transform unit smaller |
| 3842 | /// than its own block (`TxMode::Select`, spec `get_txb_ctx_general`) |
| 3843 | /// needs the actual magnitude tier of its above/left neighbours, not |
| 3844 | /// just whether they coded anything. |
| 3845 | level: u8, |
| 3846 | dc: Option<bool>, |
| 3847 | } |
| 3848 | |
| 3849 | fn neighbour_state(grid: &[i32]) -> Neighbour { |
| 3850 | let cul: u32 = grid.iter().map(|&l| l.unsigned_abs()).sum(); |
| 3851 | Neighbour { |
| 3852 | level: cul.min(7) as u8, |
| 3853 | dc: (grid[0] != 0).then_some(grid[0] < 0), |
| 3854 | } |
| 3855 | } |
| 3856 | |
| 3857 | /// spec `get_txb_ctx_general`'s `skip_contexts[top][left]` table (plane 0, |
| 3858 | /// transform unit smaller than the block it sits in): `top`/`left` are the |
| 3859 | /// above/left neighbours' magnitude tiers, each already clamped to 4. |
| 3860 | const SKIP_CONTEXTS: [[usize; 5]; 5] = [ |
| 3861 | [1, 2, 2, 2, 3], |
| 3862 | [2, 4, 4, 4, 5], |
| 3863 | [2, 4, 4, 4, 5], |
| 3864 | [2, 4, 4, 4, 5], |
| 3865 | [3, 5, 5, 5, 6], |
| 3866 | ]; |
| 3867 | |
| 3868 | /// The neighbour state a block's contexts are read from — a duplicate of |
| 3869 | /// [`crate::tile`]'s own private `Neighbours` (that module is another lane's |
| 3870 | /// territory this round), kept at the same two granularities the real writer |
| 3871 | /// tracks: `above_mode`/`left_mode`/`above_side`/`left_side` on the 16x16 |
| 3872 | /// (`SUB`) grid the partition/mode symbols read, and `above`/`left`'s |
| 3873 | /// per-plane coded/DC-sign state on the finer 4x4 (`MI`) grid the coefficient |
| 3874 | /// contexts read (spec `av1_set_entropy_contexts`) — which is what lets a |
| 3875 | /// chroma transform straddling the true, odd-sized frame edge see its |
| 3876 | /// neighbour's state clamped at its own subsampled edge rather than luma's. |
| 3877 | struct Neighbours { |
| 3878 | above: Vec<[Neighbour; 3]>, |
| 3879 | left: Vec<[Neighbour; 3]>, |
| 3880 | above_mode: Vec<usize>, |
| 3881 | left_mode: Vec<usize>, |
| 3882 | /// Chroma's own `uv_mode` companion to `above_mode`/`left_mode` (lane-chroma |
| 3883 | /// r3): a smooth/paeth `uv_mode`'s edge-filter strength (spec |
| 3884 | /// `get_intra_edge_filter_type`) reads the CHROMA neighbour's mode, not the |
| 3885 | /// luma one -- the same block can (and often does) code a different |
| 3886 | /// `uv_mode` than its own `mode`. Coarse [`SUB`]-grid like `above_mode`; |
| 3887 | /// [`Self::decode_leaf8`]'s per-leaf `prev_leaf` override (a real |
| 3888 | /// within-block neighbour finer than this grid) is luma-only -- a known, |
| 3889 | /// unexercised-by-gate corner-cut (every real-encoder gate in `stream.rs` |
| 3890 | /// uses `--min-partition-size=16`, so no leaf8 8x8 split ever reaches this). |
| 3891 | above_uv_mode: Vec<usize>, |
| 3892 | left_uv_mode: Vec<usize>, |
| 3893 | above_side: Vec<usize>, |
| 3894 | left_side: Vec<usize>, |
| 3895 | /// The same as `above_side`/`left_side`, but kept at the finer mi (4x4) |
| 3896 | /// granularity `above`/`left` are, rather than [`SUB`] -- [`crate::tile`]'s |
| 3897 | /// `above_side_mi`/`left_side_mi`: two 8x8 leaves of one straddling 16x16 |
| 3898 | /// block (lane-av1-rect) share a single [`SUB`]-grid cell, so a coarse |
| 3899 | /// array cannot tell the second leaf's partition symbol that the first |
| 3900 | /// one was coded at 8x8. |
| 3901 | above_side_mi: Vec<usize>, |
| 3902 | left_side_mi: Vec<usize>, |
| 3903 | /// The luma `mode` of the last `BLOCK_4X4` leaf decoded at each mi column |
| 3904 | /// / row, with the mi position it was written at |
| 3905 | /// (`(usize::MAX, _)` = never written). `above_mode`/`left_mode` are on |
| 3906 | /// the coarse 16x16 grid, which cannot distinguish the two 4x4 leaves an |
| 3907 | /// 8x8 `PARTITION_SPLIT` puts in one mi column; libaom's intra-mode |
| 3908 | /// context reads `mi(row, col-1)`/`mi(row-1, col)` exactly, so a sub-8x8 |
| 3909 | /// leaf whose neighbour is another split group needs that group's leaf 3 |
| 3910 | /// (bottom-right), not the leaf the coarse slot happens to hold. Written |
| 3911 | /// and read only by [`decode_leaf_split4`]; the position guard makes it |
| 3912 | /// fall back to the coarse slot whenever the real neighbour was a block |
| 3913 | /// of 8x8 or larger (whose one mode is correct for every row it spans). |
| 3914 | sub8_mode_col: Vec<(usize, usize)>, |
| 3915 | sub8_mode_row: Vec<(usize, usize)>, |
| 3916 | /// The `uv_mode` twin of `sub8_mode_col`/`sub8_mode_row` (lane-sub8 r5): |
| 3917 | /// chroma's edge-filter type reads the CHROMA neighbour's `uv_mode` |
| 3918 | /// (`get_intra_edge_filter_type`, reconintra.c:974), and the coarse |
| 3919 | /// [`SUB`] slots cannot name it once a 16x16 is split into 8x8 leaves -- |
| 3920 | /// the leaf above writes the same row slot the block to the left owns. |
| 3921 | uv_mode_col: Vec<(usize, usize)>, |
| 3922 | uv_mode_row: Vec<(usize, usize)>, |
| 3923 | /// Whether the block above/left of this column/row was coded `skip` -- |
| 3924 | /// an inter frame's own `skip` context (spec `SkipContext`), which the |
| 3925 | /// key-frame writer never tracks (its skip context is always zero); a |
| 3926 | /// duplicate of [`crate::tile`]'s private `Neighbours::above_skip`/ |
| 3927 | /// `left_skip`. |
| 3928 | above_skip: Vec<bool>, |
| 3929 | left_skip: Vec<bool>, |
| 3930 | /// libaom's `above_txfm_context`/`left_txfm_context` (`TXFM_CONTEXT`), at |
| 3931 | /// 4x4 mi granularity: the transform *width* (above) / *height* (left) in |
| 3932 | /// pixels last written over each unit by `set_txfm_ctxs`/ |
| 3933 | /// `txfm_partition_update`, which is what an inter block's `txfm_split` |
| 3934 | /// symbol reads its context from (lane-txselect, spec 5.11.17). |
| 3935 | /// [`TXFM_CTX_INIT`] until a block writes one; a fresh full-height `left` |
| 3936 | /// array is exactly libaom's per-superblock-row reset, since each mi row |
| 3937 | /// is only ever visited inside one superblock row. |
| 3938 | above_txfm: Vec<u8>, |
| 3939 | left_txfm: Vec<u8>, |
| 3940 | /// Whether the block above/left of this column/row coded `skip_mode` |
| 3941 | /// (spec 5.11.23) -- `av1_get_skip_mode_context`'s own neighbour lookup, |
| 3942 | /// lane-av1comp round 14. |
| 3943 | above_skip_mode: Vec<bool>, |
| 3944 | left_skip_mode: Vec<bool>, |
| 3945 | /// Whether the block above/left was coded inter -- the `is_inter` |
| 3946 | /// context (spec `av1_get_intra_inter_context`), a duplicate of |
| 3947 | /// [`crate::tile`]'s private `Neighbours::above_inter`/`left_inter`. |
| 3948 | above_inter: Vec<bool>, |
| 3949 | left_inter: Vec<bool>, |
| 3950 | /// The actual reference frame (`1..=7`, spec's `MV_REFERENCE_FRAME` |
| 3951 | /// alphabet) the block above/left of this column/row coded, or `-1` for |
| 3952 | /// an intra neighbour/frame border -- the `single_ref_p1`..`p6` context |
| 3953 | /// functions (`mvstack::ref_ctx` family) need the real reference, not |
| 3954 | /// just "was it inter" (lane-av1refs: every inter block used to be |
| 3955 | /// `LAST_FRAME` by construction, so `above_inter`/`left_inter` alone was |
| 3956 | /// enough). |
| 3957 | above_ref: Vec<i8>, |
| 3958 | left_ref: Vec<i8>, |
| 3959 | /// The block above/left's *second* reference frame when it was coded |
| 3960 | /// compound (spec `RefFrames[1]`), or `None` for single-ref/intra -- |
| 3961 | /// lane-av1comp: `mvstack::reference_mode_ctx`/`comp_reference_type_ctx` |
| 3962 | /// need a full [`crate::mvstack::NeighbourRef`], not just `above_ref`'s |
| 3963 | /// `i8`, once a compound `read_ref_frames` lands. Always `None` today |
| 3964 | /// (no decode path here ever produces a compound block yet). |
| 3965 | above_ref1: Vec<Option<i8>>, |
| 3966 | left_ref1: Vec<Option<i8>>, |
| 3967 | /// The precomputed `get_comp_group_idx_context`/`get_comp_index_context` |
| 3968 | /// neighbour contribution (spec 5.11.25, libaom `pred_common.h`): |
| 3969 | /// a compound neighbour's own `comp_group_idx`/`compound_idx` bit, or |
| 3970 | /// the "ref_frame[0] == ALTREF_FRAME" special case (`3`/`1`) for a |
| 3971 | /// single-ref one, or `0` for intra/unavailable -- lane-av1comp. |
| 3972 | above_comp_group_idx: Vec<u8>, |
| 3973 | left_comp_group_idx: Vec<u8>, |
| 3974 | above_compound_idx: Vec<u8>, |
| 3975 | left_compound_idx: Vec<u8>, |
| 3976 | /// `interp_filter[0]`/`[1]` (horizontal/vertical kernel index, 0..=2, or |
| 3977 | /// `3` = "no info": an intra neighbour or the frame's own border) of the |
| 3978 | /// block above/left of this column/row -- spec |
| 3979 | /// `av1_get_pred_context_switchable_interp`'s own neighbour lookup. |
| 3980 | above_filter: Vec<[u8; 2]>, |
| 3981 | left_filter: Vec<[u8; 2]>, |
| 3982 | mi_cols: usize, |
| 3983 | mi_rows: usize, |
| 3984 | /// Per-8x8 (2x2 mi cells) `skip_txfm` flag over the whole padded frame, |
| 3985 | /// spec `av1_cdef_compute_sb_list`'s `is_8x8_block_skip`: CDEF (spec |
| 3986 | /// 7.15) never filters an 8x8 luma block whose covering coded block read |
| 3987 | /// `skip` -- one flag per 4x4 mi cell (finer than needed, but matches the |
| 3988 | /// grain [`Self::record_mi`] already writes at), read back at 8x8 |
| 3989 | /// granularity by [`apply_cdef`] since `skip` is constant across every |
| 3990 | /// mi cell one coded block covers. |
| 3991 | skip_grid: Vec<bool>, |
| 3992 | skip_grid_cols_mi: usize, |
| 3993 | /// Per-4x4-mi luma transform width in pixels (8/16/32/64) -- this decoder |
| 3994 | /// never splits a coded block's transform below its own size (round 2: |
| 3995 | /// `TxMode::Select`/`tx_depth` refused), so a block's own side *is* its |
| 3996 | /// transform size, spec 7.14's `get_transform_size`/`get_filter_level` |
| 3997 | /// edge-length lookup (`apply_deblock`). |
| 3998 | tx_grid: Vec<u8>, |
| 3999 | /// The transform HEIGHT companion to [`Self::tx_grid`]'s width -- |
| 4000 | /// lane-rect r2: TX_32X16/TX_16X32 strips deblock their horizontal |
| 4001 | /// edges by tx height (spec 7.14.2 `set_lpf_parameters` dir==VERT). |
| 4002 | tx_h_grid: Vec<u8>, |
| 4003 | /// Per-4x4-mi CHROMA transform width/height in pixels -- lane-tiny r4. |
| 4004 | /// A block's chroma transform is `av1_get_max_uv_txsize(bsize)`: the |
| 4005 | /// largest transform covering the subsampled BLOCK, capped at 32x32. It |
| 4006 | /// does NOT follow luma's `tx_depth` split, so deriving it as |
| 4007 | /// `tx_grid / 2` invented chroma transform edges the deblocker then |
| 4008 | /// filtered (32x16 frames, U plane, seeds 42/47). Written from the |
| 4009 | /// block's own mi span in [`Self::fill_lf_grid_rect`], the one place |
| 4010 | /// every caller routes through. |
| 4011 | uv_tx_grid: Vec<u8>, |
| 4012 | uv_tx_h_grid: Vec<u8>, |
| 4013 | /// Per-4x4-mi reference frame (`0` = intra, magnitude `1..=7` = |
| 4014 | /// `MV_REFERENCE_FRAME`, negative = that reference coded as `GLOBALMV` -- |
| 4015 | /// `lf_level`'s `mode_lf_lut` row 0, lane-av1golden2) |
| 4016 | /// -- the loop filter's ref/mode delta lookup (spec 7.14.4 |
| 4017 | /// `get_filter_level`, `loop_filter_ref_deltas[ref_frame]`; lane-av1refs: |
| 4018 | /// widened from a bare `is_inter` bool, which collapsed every non-`LAST` |
| 4019 | /// reference onto `LAST_FRAME`'s own delta). |
| 4020 | ref_grid: Vec<i8>, |
| 4021 | /// Per-4x4-mi snapshot of [`CURRENT_DELTA_LF`] at the moment this cell's |
| 4022 | /// block was decoded (spec `DeltaLF[i]`, `i` in `0..FRAME_LF_COUNT`, |
| 4023 | /// Y-vertical/Y-horizontal/U/V) -- lane-realworld r5: `lf_level`'s |
| 4024 | /// per-block additive term, mirroring [`Self::ref_grid`]'s own snapshot |
| 4025 | /// pattern. `!delta_lf_multi` mode broadcasts index 0 into all 4 slots |
| 4026 | /// at write time ([`Self::fill_lf_grid_rect`]) so a reader never needs |
| 4027 | /// to know which mode produced the value. |
| 4028 | delta_lf_grid: Vec<[i8; 4]>, |
| 4029 | /// The block above/left's own palette-Y size (0 = not a palette block) |
| 4030 | /// and its base colours -- `av1_get_palette_mode_ctx`/ |
| 4031 | /// `av1_get_palette_cache` (pred_common.c)'s neighbour lookup, lane-palette |
| 4032 | /// r2. UV has no equivalent yet (stage 2, unreconstructed). |
| 4033 | above_palette_size: Vec<usize>, |
| 4034 | left_palette_size: Vec<usize>, |
| 4035 | above_palette_colors: Vec<[u16; 8]>, |
| 4036 | left_palette_colors: Vec<[u16; 8]>, |
| 4037 | /// As the four fields above, for the neighbour's own chroma (U-channel |
| 4038 | /// only -- `av1_get_palette_cache`'s plane-1 lookup, [`Self::palette_uv_cache`]) |
| 4039 | /// palette size/colours (lane-palette2 r1). |
| 4040 | above_palette_uv_size: Vec<usize>, |
| 4041 | left_palette_uv_size: Vec<usize>, |
| 4042 | above_palette_uv_colors: Vec<[u16; 8]>, |
| 4043 | left_palette_uv_colors: Vec<[u16; 8]>, |
| 4044 | /// This tile's own top-left corner, in 4x4 mode-info units (spec |
| 4045 | /// `MiRowStart`/`MiColStart`) -- lane-tiles: every "is there really a |
| 4046 | /// neighbour" check compares against this instead of literal `0`, since a |
| 4047 | /// block at the tile's own top/left edge has no usable neighbour even |
| 4048 | /// though a decoded block sits there in the picture (spec `decode_tile`'s |
| 4049 | /// per-tile `clear_above_context`/`left_available`/`up_available`). |
| 4050 | /// `0` for a single-tile frame, matching every existing caller exactly. |
| 4051 | tile_row0_mi: usize, |
| 4052 | tile_col0_mi: usize, |
| 4053 | } |
| 4054 | |
| 4055 | impl Neighbours { |
| 4056 | /// `cols`/`rows` are in [`SUB`] units; `mi_cols`/`mi_rows` are the frame's |
| 4057 | /// true (unpadded) size in 4x4 mode-info units. |
| 4058 | fn new(cols: usize, rows: usize, mi_cols: usize, mi_rows: usize) -> Self { |
| 4059 | Self { |
| 4060 | above: vec![[Neighbour::default(); 3]; cols * (SUB / MI)], |
| 4061 | left: vec![[Neighbour::default(); 3]; rows * (SUB / MI)], |
| 4062 | above_mode: vec![DC_PRED; cols], |
| 4063 | left_mode: vec![DC_PRED; rows], |
| 4064 | above_uv_mode: vec![DC_PRED; cols], |
| 4065 | left_uv_mode: vec![DC_PRED; rows], |
| 4066 | above_side: vec![NO_NEIGHBOUR_SIDE; cols], |
| 4067 | left_side: vec![NO_NEIGHBOUR_SIDE; rows], |
| 4068 | above_side_mi: vec![NO_NEIGHBOUR_SIDE; cols * (SUB / MI)], |
| 4069 | left_side_mi: vec![NO_NEIGHBOUR_SIDE; rows * (SUB / MI)], |
| 4070 | sub8_mode_col: vec![(usize::MAX, 0); cols * (SUB / MI)], |
| 4071 | sub8_mode_row: vec![(usize::MAX, 0); rows * (SUB / MI)], |
| 4072 | uv_mode_col: vec![(usize::MAX, 0); cols * (SUB / MI)], |
| 4073 | uv_mode_row: vec![(usize::MAX, 0); rows * (SUB / MI)], |
| 4074 | above_txfm: vec![TXFM_CTX_INIT; cols * (SUB / MI)], |
| 4075 | left_txfm: vec![TXFM_CTX_INIT; rows * (SUB / MI)], |
| 4076 | // lane-inter8 r2: mi(4px)-granular, not [`SUB`]-granular -- an |
| 4077 | // 8x8 inter leaf's left neighbour is the OTHER leaf row of the |
| 4078 | // 16x16 block beside it, which a SUB-grid slot cannot name |
| 4079 | // (class context-read-from-one-cell). |
| 4080 | above_skip: vec![false; cols * (SUB / MI)], |
| 4081 | left_skip: vec![false; rows * (SUB / MI)], |
| 4082 | above_skip_mode: vec![false; cols * (SUB / MI)], |
| 4083 | left_skip_mode: vec![false; rows * (SUB / MI)], |
| 4084 | above_inter: vec![false; cols * (SUB / MI)], |
| 4085 | left_inter: vec![false; rows * (SUB / MI)], |
| 4086 | above_ref: vec![-1; cols * (SUB / MI)], |
| 4087 | left_ref: vec![-1; rows * (SUB / MI)], |
| 4088 | above_ref1: vec![None; cols * (SUB / MI)], |
| 4089 | left_ref1: vec![None; rows * (SUB / MI)], |
| 4090 | above_comp_group_idx: vec![0; cols * (SUB / MI)], |
| 4091 | left_comp_group_idx: vec![0; rows * (SUB / MI)], |
| 4092 | above_compound_idx: vec![0; cols * (SUB / MI)], |
| 4093 | left_compound_idx: vec![0; rows * (SUB / MI)], |
| 4094 | above_filter: vec![[3u8; 2]; cols * (SUB / MI)], |
| 4095 | left_filter: vec![[3u8; 2]; rows * (SUB / MI)], |
| 4096 | mi_cols, |
| 4097 | mi_rows, |
| 4098 | skip_grid: vec![false; cols * (SUB / MI) * rows * (SUB / MI)], |
| 4099 | skip_grid_cols_mi: cols * (SUB / MI), |
| 4100 | tx_grid: vec![0u8; cols * (SUB / MI) * rows * (SUB / MI)], |
| 4101 | tx_h_grid: vec![0u8; cols * (SUB / MI) * rows * (SUB / MI)], |
| 4102 | uv_tx_grid: vec![0u8; cols * (SUB / MI) * rows * (SUB / MI)], |
| 4103 | uv_tx_h_grid: vec![0u8; cols * (SUB / MI) * rows * (SUB / MI)], |
| 4104 | ref_grid: vec![0i8; cols * (SUB / MI) * rows * (SUB / MI)], |
| 4105 | delta_lf_grid: vec![[0i8; 4]; cols * (SUB / MI) * rows * (SUB / MI)], |
| 4106 | above_palette_size: vec![0; cols], |
| 4107 | left_palette_size: vec![0; rows], |
| 4108 | above_palette_colors: vec![[0u16; 8]; cols], |
| 4109 | left_palette_colors: vec![[0u16; 8]; rows], |
| 4110 | above_palette_uv_size: vec![0; cols], |
| 4111 | left_palette_uv_size: vec![0; rows], |
| 4112 | above_palette_uv_colors: vec![[0u16; 8]; cols], |
| 4113 | left_palette_uv_colors: vec![[0u16; 8]; rows], |
| 4114 | tile_row0_mi: 0, |
| 4115 | tile_col0_mi: 0, |
| 4116 | } |
| 4117 | } |
| 4118 | |
| 4119 | /// spec `decode_tile`'s per-tile reset: `clear_above_context` over this |
| 4120 | /// tile's own column span (`col1_mi` exclusive), plus recording |
| 4121 | /// `(row0_mi, col0_mi)` as the tile's own top-left mi cell for every |
| 4122 | /// availability check below. Left context resets every superblock row |
| 4123 | /// regardless of tile ([`Self::start_row`], already called once per SB |
| 4124 | /// row by every caller) so it needs no tile-specific reset here. |
| 4125 | fn start_tile(&mut self, row0_mi: usize, col0_mi: usize, col1_mi: usize) { |
| 4126 | // lane-seg: `AvailU`/`AvailL` for the segment-id neighbour prediction |
| 4127 | // are tile-relative, same as every other neighbour lookup here. |
| 4128 | SEG_TILE_ORIGIN.with(|c| c.set((row0_mi, col0_mi))); |
| 4129 | self.tile_row0_mi = row0_mi; |
| 4130 | self.tile_col0_mi = col0_mi; |
| 4131 | let end = col1_mi.min(self.above.len()); |
| 4132 | for i in col0_mi.min(end)..end { |
| 4133 | self.above[i] = Default::default(); |
| 4134 | self.above_side_mi[i] = NO_NEIGHBOUR_SIDE; |
| 4135 | } |
| 4136 | let (sc0, sc1) = ( |
| 4137 | col0_mi / (SUB / MI), |
| 4138 | (col1_mi / (SUB / MI)).min(self.above_mode.len()), |
| 4139 | ); |
| 4140 | for i in sc0.min(sc1)..sc1 { |
| 4141 | self.above_mode[i] = DC_PRED; |
| 4142 | self.above_side[i] = NO_NEIGHBOUR_SIDE; |
| 4143 | } |
| 4144 | // lane-inter8 r2: the inter bands are mi-granular, so they reset over |
| 4145 | // the tile's own mi column span directly. |
| 4146 | let mend = col1_mi.min(self.above_skip.len()); |
| 4147 | for i in col0_mi.min(mend)..mend { |
| 4148 | self.above_skip[i] = false; |
| 4149 | self.above_skip_mode[i] = false; |
| 4150 | self.above_inter[i] = false; |
| 4151 | self.above_ref[i] = -1; |
| 4152 | self.above_ref1[i] = None; |
| 4153 | self.above_comp_group_idx[i] = 0; |
| 4154 | self.above_compound_idx[i] = 0; |
| 4155 | self.above_filter[i] = [3u8; 2]; |
| 4156 | } |
| 4157 | // lane-sub8 r6: the mi-granular column maps are per tile too -- a |
| 4158 | // stale `(row, mode)` from the tile to the LEFT names an mi row this |
| 4159 | // tile's first block would read as its own above neighbour. |
| 4160 | let mcol_end = col1_mi.min(self.sub8_mode_col.len()); |
| 4161 | for i in col0_mi.min(mcol_end)..mcol_end { |
| 4162 | self.sub8_mode_col[i] = (usize::MAX, 0); |
| 4163 | self.uv_mode_col[i] = (usize::MAX, 0); |
| 4164 | } |
| 4165 | } |
| 4166 | |
| 4167 | /// Writes a just-decoded block's own palette-Y size/colours (`size == 0` |
| 4168 | /// for a non-palette block, clearing any stale neighbour state) into every |
| 4169 | /// [`SUB`]-grid cell it covers on both the above and left edges -- |
| 4170 | /// mirrors [`Self::record_rect`]'s loop shape, square-only (this lane's |
| 4171 | /// call sites are all square blocks). |
| 4172 | fn record_palette_y(&mut self, at: (usize, usize), side: usize, size: usize, colors: [u16; 8]) { |
| 4173 | self.record_palette_y_rect(at, side, side, size, colors); |
| 4174 | } |
| 4175 | |
| 4176 | /// [`Self::record_palette_y`] with independent above (`w`)/left (`h`) |
| 4177 | /// extents (lane-palette2 r4, mirrors [`Self::record_rect`] vs |
| 4178 | /// [`Self::record`]): a true rect strip's above/left neighbour spans |
| 4179 | /// differ. |
| 4180 | fn record_palette_y_rect( |
| 4181 | &mut self, |
| 4182 | at: (usize, usize), |
| 4183 | w: usize, |
| 4184 | h: usize, |
| 4185 | size: usize, |
| 4186 | colors: [u16; 8], |
| 4187 | ) { |
| 4188 | let (r, c) = at; |
| 4189 | // `.max(1)`: a below-16px span covers no whole SUB cell, so the plain |
| 4190 | // `w / SUB` loop was a NO-OP for every 16x8/8x16/8x8/4x4 leaf and the |
| 4191 | // next block read a long-dead palette out of the above/left arrays |
| 4192 | // (class [[cdf-row-held-constant]], lane-palette2 r8). libaom keeps |
| 4193 | // `palette_size` per 4px mi cell; corner-cut: our grid is 16px, so a |
| 4194 | // sub-16 leaf stamps its whole containing cell -- safe today because |
| 4195 | // every sub-16 leaf path refuses palette outright (so it can only |
| 4196 | // ever stamp `size == 0`, and its cell holds no palette sibling). |
| 4197 | // Upgrade path: widen the palette arrays to mi granularity. |
| 4198 | for cell in 0..(w / SUB).max(1) { |
| 4199 | self.above_palette_size[c + cell] = size; |
| 4200 | self.above_palette_colors[c + cell] = colors; |
| 4201 | } |
| 4202 | for cell in 0..(h / SUB).max(1) { |
| 4203 | self.left_palette_size[r + cell] = size; |
| 4204 | self.left_palette_colors[r + cell] = colors; |
| 4205 | } |
| 4206 | } |
| 4207 | |
| 4208 | /// `av1_get_palette_mode_ctx` (pred_common.h:197): count of the above/left |
| 4209 | /// neighbours that are themselves a palette-Y block, plus |
| 4210 | /// `av1_get_palette_cache`'s own merged, ascending, deduplicated colour |
| 4211 | /// cache (pred_common.c:73). |
| 4212 | /// |
| 4213 | /// The SB-top-row exclusion of the above neighbour (`row % 64` in |
| 4214 | /// pred_common.c:76, `r % 4 == 0` here in [`SUB`]-grid units) belongs to |
| 4215 | /// the CACHE ALONE -- `av1_get_palette_mode_ctx` reads plain |
| 4216 | /// `xd->above_mbmi` with no such guard (lane-palette2 r7). Applying it to |
| 4217 | /// the ctx too made every superblock in the frame's second SB row read |
| 4218 | /// `palette_y_mode` off CDF row 0 while libaom used row 1, so a real |
| 4219 | /// palette block decoded as "no palette" and the tile desynced from |
| 4220 | /// there (class [[cdf-row-held-constant]]). |
| 4221 | fn palette_ctx_and_cache(&self, at: (usize, usize)) -> (usize, Vec<u16>) { |
| 4222 | self.palette_ctx_and_cache_mi(at, at.0 * (SUB / MI)) |
| 4223 | } |
| 4224 | |
| 4225 | /// [`Self::palette_ctx_and_cache`] with the block's REAL mi row, which a |
| 4226 | /// 1:4 strip does not share with its [`SUB`] cell: `av1_get_palette_cache` |
| 4227 | /// drops the above neighbour on a 64-px superblock boundary |
| 4228 | /// (pred_common.c:76, `row % (1 << MIN_SB_SIZE_LOG2)` with `row` the |
| 4229 | /// block's own PIXEL row), and strip 1 of a HORZ_4 sits 8 px into the |
| 4230 | /// cell -- reading `at.0 % 4` there excludes strip 0 from the cache of |
| 4231 | /// every strip in an SB's top cell row, which libaom does not do. |
| 4232 | fn palette_ctx_and_cache_mi(&self, at: (usize, usize), mi_r: usize) -> (usize, Vec<u16>) { |
| 4233 | let (r, c) = at; |
| 4234 | let above_ok = mi_r % 16 != 0; |
| 4235 | // `xd->above_mbmi` exists for every row but the tile's own first. |
| 4236 | let above_is_palette = mi_r > self.tile_row0_mi && self.above_palette_size[c] > 0; |
| 4237 | let (above_n, above_colors) = if above_ok && self.above_palette_size[c] > 0 { |
| 4238 | (self.above_palette_size[c], self.above_palette_colors[c]) |
| 4239 | } else { |
| 4240 | (0, [0u16; 8]) |
| 4241 | }; |
| 4242 | // lane-tiles r11: `av1_get_palette_cache` takes `xd->left_mbmi`, which |
| 4243 | // is NULL whenever the block sits at its TILE's own left column |
| 4244 | // (`left_available`), not the frame's -- without this guard a second |
| 4245 | // tile column reads the previous tile's palette colours into its cache |
| 4246 | // and its `palette_y_mode` ctx. |
| 4247 | let left_ok = c > self.tile_col0_mi / (SUB / MI); |
| 4248 | let (left_n, left_colors) = if left_ok && self.left_palette_size[r] > 0 { |
| 4249 | (self.left_palette_size[r], self.left_palette_colors[r]) |
| 4250 | } else { |
| 4251 | (0, [0u16; 8]) |
| 4252 | }; |
| 4253 | let ctx = usize::from(above_is_palette) + usize::from(left_n > 0); |
| 4254 | let mut cache = Vec::with_capacity(16); |
| 4255 | let push_dedup = |cache: &mut Vec<u16>, v: u16| { |
| 4256 | if cache.last() != Some(&v) { |
| 4257 | cache.push(v); |
| 4258 | } |
| 4259 | }; |
| 4260 | let (mut ai, mut li) = (0usize, 0usize); |
| 4261 | while ai < above_n && li < left_n { |
| 4262 | let (va, vl) = (above_colors[ai], left_colors[li]); |
| 4263 | if vl < va { |
| 4264 | push_dedup(&mut cache, vl); |
| 4265 | li += 1; |
| 4266 | } else { |
| 4267 | push_dedup(&mut cache, va); |
| 4268 | ai += 1; |
| 4269 | if vl == va { |
| 4270 | li += 1; |
| 4271 | } |
| 4272 | } |
| 4273 | } |
| 4274 | while ai < above_n { |
| 4275 | push_dedup(&mut cache, above_colors[ai]); |
| 4276 | ai += 1; |
| 4277 | } |
| 4278 | while li < left_n { |
| 4279 | push_dedup(&mut cache, left_colors[li]); |
| 4280 | li += 1; |
| 4281 | } |
| 4282 | (ctx, cache) |
| 4283 | } |
| 4284 | |
| 4285 | /// As [`Self::record_palette_y`], for a just-decoded block's own chroma |
| 4286 | /// (U-channel) palette size/colours -- `size == 0` clears stale state the |
| 4287 | /// same way (lane-palette2 r1). |
| 4288 | fn record_palette_uv(&mut self, at: (usize, usize), side: usize, size: usize, colors: [u16; 8]) { |
| 4289 | self.record_palette_uv_rect(at, side, side, size, colors); |
| 4290 | } |
| 4291 | |
| 4292 | /// [`Self::record_palette_uv`] with independent above (`w`)/left (`h`) |
| 4293 | /// extents, in LUMA pixels (lane-palette2 r4, same convention as |
| 4294 | /// [`Self::record_palette_y_rect`] -- the above/left grids are indexed at |
| 4295 | /// luma resolution regardless of which plane the recorded state is for). |
| 4296 | fn record_palette_uv_rect( |
| 4297 | &mut self, |
| 4298 | at: (usize, usize), |
| 4299 | w: usize, |
| 4300 | h: usize, |
| 4301 | size: usize, |
| 4302 | colors: [u16; 8], |
| 4303 | ) { |
| 4304 | let (r, c) = at; |
| 4305 | // See [`Self::record_palette_y_rect`] for the `.max(1)`. |
| 4306 | for cell in 0..(w / SUB).max(1) { |
| 4307 | self.above_palette_uv_size[c + cell] = size; |
| 4308 | self.above_palette_uv_colors[c + cell] = colors; |
| 4309 | } |
| 4310 | for cell in 0..(h / SUB).max(1) { |
| 4311 | self.left_palette_uv_size[r + cell] = size; |
| 4312 | self.left_palette_uv_colors[r + cell] = colors; |
| 4313 | } |
| 4314 | } |
| 4315 | |
| 4316 | /// [`Self::palette_ctx_and_cache`]'s cache half, for the U channel |
| 4317 | /// (`av1_get_palette_cache(xd, 1, cache)`) -- `palette_uv_mode_ctx` needs |
| 4318 | /// no neighbour lookup at all (it is this block's own just-decided Y |
| 4319 | /// palette use, `read_palette_mode_info`'s `pmi->palette_size[0] > 0`), |
| 4320 | /// so only the cache is wanted here. |
| 4321 | fn palette_uv_cache(&self, at: (usize, usize)) -> Vec<u16> { |
| 4322 | self.palette_uv_cache_mi(at, at.0 * (SUB / MI)) |
| 4323 | } |
| 4324 | |
| 4325 | /// [`Self::palette_uv_cache`] with the block's real mi row -- see |
| 4326 | /// [`Self::palette_ctx_and_cache_mi`]. |
| 4327 | fn palette_uv_cache_mi(&self, at: (usize, usize), mi_r: usize) -> Vec<u16> { |
| 4328 | let (r, c) = at; |
| 4329 | let above_ok = mi_r % 16 != 0; |
| 4330 | let (above_n, above_colors) = if above_ok && self.above_palette_uv_size[c] > 0 { |
| 4331 | (self.above_palette_uv_size[c], self.above_palette_uv_colors[c]) |
| 4332 | } else { |
| 4333 | (0, [0u16; 8]) |
| 4334 | }; |
| 4335 | // Same tile-relative left availability as the luma cache above. |
| 4336 | let left_ok = c > self.tile_col0_mi / (SUB / MI); |
| 4337 | let (left_n, left_colors) = if left_ok && self.left_palette_uv_size[r] > 0 { |
| 4338 | (self.left_palette_uv_size[r], self.left_palette_uv_colors[r]) |
| 4339 | } else { |
| 4340 | (0, [0u16; 8]) |
| 4341 | }; |
| 4342 | let mut cache = Vec::with_capacity(16); |
| 4343 | let push_dedup = |cache: &mut Vec<u16>, v: u16| { |
| 4344 | if cache.last() != Some(&v) { |
| 4345 | cache.push(v); |
| 4346 | } |
| 4347 | }; |
| 4348 | let (mut ai, mut li) = (0usize, 0usize); |
| 4349 | while ai < above_n && li < left_n { |
| 4350 | let (va, vl) = (above_colors[ai], left_colors[li]); |
| 4351 | if vl < va { |
| 4352 | push_dedup(&mut cache, vl); |
| 4353 | li += 1; |
| 4354 | } else { |
| 4355 | push_dedup(&mut cache, va); |
| 4356 | ai += 1; |
| 4357 | if vl == va { |
| 4358 | li += 1; |
| 4359 | } |
| 4360 | } |
| 4361 | } |
| 4362 | while ai < above_n { |
| 4363 | push_dedup(&mut cache, above_colors[ai]); |
| 4364 | ai += 1; |
| 4365 | } |
| 4366 | while li < left_n { |
| 4367 | push_dedup(&mut cache, left_colors[li]); |
| 4368 | li += 1; |
| 4369 | } |
| 4370 | cache |
| 4371 | } |
| 4372 | |
| 4373 | /// Marks every 4x4 mi cell a just-decoded coded block covers with its own |
| 4374 | /// transform width (in pixels) and `is_inter` flag -- the loop filter's |
| 4375 | /// per-edge lookup (`apply_deblock`), filled at the same call sites and |
| 4376 | /// units as [`Self::fill_skip_grid`]. |
| 4377 | fn fill_lf_grid(&mut self, at_mi: (usize, usize), side_mi: usize, tx_px: u8, ref_frame: i8) { |
| 4378 | self.fill_lf_grid_rect(at_mi, side_mi, side_mi, tx_px, tx_px, ref_frame); |
| 4379 | } |
| 4380 | |
| 4381 | /// [`Self::fill_lf_grid`] with independent row/col extents -- lane-partitions |
| 4382 | /// r1: a true rectangular strip's mi span isn't `side_mi` square (`w_mi` |
| 4383 | /// != `h_mi`); `tx_px` still names a single scalar (this decoder's |
| 4384 | /// existing corner-cut, `Neighbours::tx_grid`'s own doc comment -- a |
| 4385 | /// rectangular strip's transform is `TX_32X16`/`TX_16X32`, not tracked |
| 4386 | /// per-axis, matching lane-warp r5's `HORZ_B` top-strip precedent). |
| 4387 | fn fill_lf_grid_rect( |
| 4388 | &mut self, |
| 4389 | at_mi: (usize, usize), |
| 4390 | w_mi: usize, |
| 4391 | h_mi: usize, |
| 4392 | tx_px: u8, |
| 4393 | tx_h_px: u8, |
| 4394 | ref_frame: i8, |
| 4395 | ) { |
| 4396 | let (mi_r, mi_c) = at_mi; |
| 4397 | // `av1_get_max_uv_txsize` under 4:2:0: the block's chroma extent is |
| 4398 | // half its luma one (min 4 px, the smallest transform), capped at |
| 4399 | // TX_32X32 -- independent of this block's luma `tx_depth`. |
| 4400 | let uv_tx_w = ((w_mi * MI / 2).max(4).min(32)) as u8; |
| 4401 | let uv_tx_h = ((h_mi * MI / 2).max(4).min(32)) as u8; |
| 4402 | if std::env::var_os("EC_TXGRID_TRACE").is_some() { |
| 4403 | eprintln!("EC_LFGRID mi_row={mi_r} mi_col={mi_c} w_mi={w_mi} h_mi={h_mi} tx_px={tx_px} tx_h_px={tx_h_px}"); |
| 4404 | } |
| 4405 | let cur = CURRENT_DELTA_LF.with(|c| c.get()); |
| 4406 | let snapshot: [i8; 4] = if DELTA_LF_MULTI.with(|c| c.get()) { |
| 4407 | std::array::from_fn(|i| cur[i].clamp(-63, 63) as i8) |
| 4408 | } else { |
| 4409 | [cur[0].clamp(-63, 63) as i8; 4] |
| 4410 | }; |
| 4411 | for rr in 0..h_mi { |
| 4412 | for cc in 0..w_mi { |
| 4413 | let idx = (mi_r + rr) * self.skip_grid_cols_mi + (mi_c + cc); |
| 4414 | if let Some(cell) = self.tx_grid.get_mut(idx) { |
| 4415 | *cell = tx_px; |
| 4416 | } |
| 4417 | if let Some(cell) = self.tx_h_grid.get_mut(idx) { |
| 4418 | *cell = tx_h_px; |
| 4419 | } |
| 4420 | if let Some(cell) = self.uv_tx_grid.get_mut(idx) { |
| 4421 | *cell = uv_tx_w; |
| 4422 | } |
| 4423 | if let Some(cell) = self.uv_tx_h_grid.get_mut(idx) { |
| 4424 | *cell = uv_tx_h; |
| 4425 | } |
| 4426 | if let Some(cell) = self.ref_grid.get_mut(idx) { |
| 4427 | *cell = ref_frame; |
| 4428 | } |
| 4429 | if let Some(cell) = self.delta_lf_grid.get_mut(idx) { |
| 4430 | *cell = snapshot; |
| 4431 | } |
| 4432 | } |
| 4433 | } |
| 4434 | } |
| 4435 | |
| 4436 | /// One var-tx / split-tx LEAF's own LUMA transform dims over its mi span. |
| 4437 | /// libaom `get_transform_size` (av1_loopfilter.c:207) applies the split |
| 4438 | /// override only `if ((plane == AOM_PLANE_Y) && ...)` -- chroma keeps |
| 4439 | /// `av1_get_max_uv_txsize(mbmi->bsize)`, the whole BLOCK's uv transform. |
| 4440 | /// Routing a leaf through [`Self::fill_lf_grid_rect`] recomputed the uv |
| 4441 | /// dims from the LEAF span and so invented chroma transform edges the |
| 4442 | /// deblocker then filtered (lane-golomb r6). `ref_grid`/`delta_lf_grid` |
| 4443 | /// are already correct from the block's own fill over the same span. |
| 4444 | fn fill_lf_grid_leaf_luma( |
| 4445 | &mut self, |
| 4446 | at_mi: (usize, usize), |
| 4447 | w_mi: usize, |
| 4448 | h_mi: usize, |
| 4449 | tx_px: u8, |
| 4450 | tx_h_px: u8, |
| 4451 | ) { |
| 4452 | let (mi_r, mi_c) = at_mi; |
| 4453 | for rr in 0..h_mi { |
| 4454 | for cc in 0..w_mi { |
| 4455 | let idx = (mi_r + rr) * self.skip_grid_cols_mi + (mi_c + cc); |
| 4456 | if let Some(cell) = self.tx_grid.get_mut(idx) { |
| 4457 | *cell = tx_px; |
| 4458 | } |
| 4459 | if let Some(cell) = self.tx_h_grid.get_mut(idx) { |
| 4460 | *cell = tx_h_px; |
| 4461 | } |
| 4462 | } |
| 4463 | } |
| 4464 | } |
| 4465 | |
| 4466 | /// Marks every 4x4 mi cell a just-decoded coded block covers with its |
| 4467 | /// `skip` flag -- `at_mi`/`side_mi` are the block's own position and |
| 4468 | /// width/height in 4x4 mode-info units (mirroring [`Self::record_mi`]'s |
| 4469 | /// own units, not [`Self::record`]'s [`SUB`]-grid ones). |
| 4470 | fn fill_skip_grid(&mut self, at_mi: (usize, usize), side_mi: usize, skip: bool) { |
| 4471 | self.fill_skip_grid_rect(at_mi, side_mi, side_mi, skip); |
| 4472 | } |
| 4473 | |
| 4474 | /// [`Self::fill_skip_grid`] with independent row/col extents (lane-partitions r1). |
| 4475 | fn fill_skip_grid_rect(&mut self, at_mi: (usize, usize), w_mi: usize, h_mi: usize, skip: bool) { |
| 4476 | let (mi_r, mi_c) = at_mi; |
| 4477 | for rr in 0..h_mi { |
| 4478 | for cc in 0..w_mi { |
| 4479 | if let Some(cell) = self |
| 4480 | .skip_grid |
| 4481 | .get_mut((mi_r + rr) * self.skip_grid_cols_mi + (mi_c + cc)) |
| 4482 | { |
| 4483 | *cell = skip; |
| 4484 | } |
| 4485 | } |
| 4486 | } |
| 4487 | } |
| 4488 | |
| 4489 | /// Whether the 8x8 luma block at mi position `(mi_r, mi_c)` (2x2 mi |
| 4490 | /// cells) was `skip_txfm` -- [`apply_cdef`]'s dlist membership test. |
| 4491 | /// Spec/libaom `is_8x8_block_skip` requires *every* one of the four 4x4 |
| 4492 | /// mi cells the 8x8 covers to be skip (a smaller-than-8x8 partition can |
| 4493 | /// straddle the region with a mix of skip/non-skip blocks). |
| 4494 | /// A single 4x4 mi cell's own `skip` flag, unaggregated (unlike |
| 4495 | /// [`Self::is_skip_txfm`]'s 8x8 all-four-cells rule) -- what the loop |
| 4496 | /// filter's `curr_skipped`/`pv_skip_txfm` (spec 7.14.2) want, since a |
| 4497 | /// filtered edge's two sides are each exactly one coded block wide. |
| 4498 | fn skip_at(&self, mi_r: usize, mi_c: usize) -> bool { |
| 4499 | self.skip_grid |
| 4500 | .get(mi_r * self.skip_grid_cols_mi + mi_c) |
| 4501 | .copied() |
| 4502 | .unwrap_or(false) |
| 4503 | } |
| 4504 | |
| 4505 | /// The `skip_txfm` context of a block whose top-left mi cell is |
| 4506 | /// `(mi_r, mi_c)`: libaom `av1_get_skip_txfm_context` |
| 4507 | /// (`pred_common.h:175-181`) is `above_mi->skip_txfm + left_mi->skip_txfm`, |
| 4508 | /// with an absent neighbour contributing 0. The mi grid is per tile, so |
| 4509 | /// row/column 0 is a tile edge and has no neighbour on that axis. |
| 4510 | fn skip_txfm_ctx(&self, mi_r: usize, mi_c: usize) -> usize { |
| 4511 | usize::from(mi_r > self.tile_row0_mi && self.skip_at(mi_r - 1, mi_c)) |
| 4512 | + usize::from(mi_c > self.tile_col0_mi && self.skip_at(mi_r, mi_c - 1)) |
| 4513 | } |
| 4514 | |
| 4515 | fn is_skip_txfm(&self, mi_r: usize, mi_c: usize) -> bool { |
| 4516 | (0..2).all(|rr| { |
| 4517 | (0..2).all(|cc| { |
| 4518 | self.skip_grid |
| 4519 | .get((mi_r + rr) * self.skip_grid_cols_mi + (mi_c + cc)) |
| 4520 | .copied() |
| 4521 | .unwrap_or(false) |
| 4522 | }) |
| 4523 | }) |
| 4524 | } |
| 4525 | |
| 4526 | fn start_row(&mut self) { |
| 4527 | self.left.iter_mut().for_each(|l| *l = Default::default()); |
| 4528 | self.left_mode.iter_mut().for_each(|m| *m = DC_PRED); |
| 4529 | self.left_uv_mode.iter_mut().for_each(|m| *m = DC_PRED); |
| 4530 | self.left_side.iter_mut().for_each(|s| *s = NO_NEIGHBOUR_SIDE); |
| 4531 | self.left_side_mi.iter_mut().for_each(|s| *s = NO_NEIGHBOUR_SIDE); |
| 4532 | self.left_skip.iter_mut().for_each(|s| *s = false); |
| 4533 | self.left_skip_mode.iter_mut().for_each(|s| *s = false); |
| 4534 | self.left_inter.iter_mut().for_each(|i| *i = false); |
| 4535 | self.left_ref.iter_mut().for_each(|r| *r = -1); |
| 4536 | self.left_ref1.iter_mut().for_each(|r| *r = None); |
| 4537 | self.left_comp_group_idx.iter_mut().for_each(|r| *r = 0); |
| 4538 | self.left_compound_idx.iter_mut().for_each(|r| *r = 0); |
| 4539 | self.left_filter.iter_mut().for_each(|f| *f = [3u8; 2]); |
| 4540 | // lane-sub8 r6: mi-granular row maps reset with the rest of the left |
| 4541 | // context (a tile/SB-row boundary has no left neighbour). |
| 4542 | self.sub8_mode_row.iter_mut().for_each(|s| *s = (usize::MAX, 0)); |
| 4543 | self.uv_mode_row.iter_mut().for_each(|s| *s = (usize::MAX, 0)); |
| 4544 | // lane-tiles r11 (COMMON's NEIGHBOUR MAPS rule): the palette side bands |
| 4545 | // are per-mi maps like every other one here, so they reset with the |
| 4546 | // row too -- the left guard above is what actually stops a cross-tile |
| 4547 | // read, this keeps a stale colour out of the arrays at all. |
| 4548 | self.left_palette_size.iter_mut().for_each(|s| *s = 0); |
| 4549 | self.left_palette_colors.iter_mut().for_each(|c| *c = [0u16; 8]); |
| 4550 | self.left_palette_uv_size.iter_mut().for_each(|s| *s = 0); |
| 4551 | self.left_palette_uv_colors.iter_mut().for_each(|c| *c = [0u16; 8]); |
| 4552 | } |
| 4553 | |
| 4554 | /// Records a block's `skip`/`is_inter`/`interp_filter` state for the next |
| 4555 | /// block that reads it as a neighbour -- [`crate::tile`]'s |
| 4556 | /// `record_inter`. `filter` is `[3, 3]` for an intra block (spec |
| 4557 | /// `get_ref_filter_type`'s "not this ref frame" case reads the same as |
| 4558 | /// "no neighbour"). `ref_frame` is `-1` for an intra block, else the |
| 4559 | /// `1..=7` reference it coded (lane-av1refs's `single_ref_p*_ctx` needs |
| 4560 | /// the real value, not just `is_inter`). |
| 4561 | fn record_inter( |
| 4562 | &mut self, |
| 4563 | at: (usize, usize), |
| 4564 | side: usize, |
| 4565 | skip: bool, |
| 4566 | is_inter: bool, |
| 4567 | ref_frame: i8, |
| 4568 | filter: [u8; 2], |
| 4569 | skip_mode: bool, |
| 4570 | ) { |
| 4571 | self.record_inter_rect(at, side, side, skip, is_inter, ref_frame, filter, skip_mode); |
| 4572 | } |
| 4573 | |
| 4574 | /// [`Self::record_inter`] with independent above (`w_sub`, [`SUB`]-grid |
| 4575 | /// columns) / left (`h_sub`, [`SUB`]-grid rows) extents -- lane-partitions |
| 4576 | /// r1: a true rectangular strip's above-context span (its own width) and |
| 4577 | /// left-context span (its own height) differ, unlike every caller before |
| 4578 | /// this round (class neighbour-votes-all-its-fields). |
| 4579 | #[allow(clippy::too_many_arguments)] |
| 4580 | fn record_inter_rect( |
| 4581 | &mut self, |
| 4582 | at: (usize, usize), |
| 4583 | w_sub: usize, |
| 4584 | h_sub: usize, |
| 4585 | skip: bool, |
| 4586 | is_inter: bool, |
| 4587 | ref_frame: i8, |
| 4588 | filter: [u8; 2], |
| 4589 | skip_mode: bool, |
| 4590 | ) { |
| 4591 | let (r, c) = at; |
| 4592 | self.record_inter_rect_mi( |
| 4593 | (r * (SUB / MI), c * (SUB / MI)), |
| 4594 | w_sub / MI, |
| 4595 | h_sub / MI, |
| 4596 | skip, |
| 4597 | is_inter, |
| 4598 | ref_frame, |
| 4599 | filter, |
| 4600 | skip_mode, |
| 4601 | ); |
| 4602 | } |
| 4603 | |
| 4604 | /// [`Self::record_inter_rect`] in mi (4px) units -- lane-inter8 r2: an |
| 4605 | /// 8x8 leaf covers 2x2 mi, half a [`SUB`] cell in each direction, so the |
| 4606 | /// side bands are written (and read) per mi. Every [`SUB`]-unit caller |
| 4607 | /// routes through the wrapper above. |
| 4608 | #[allow(clippy::too_many_arguments)] |
| 4609 | fn record_inter_rect_mi( |
| 4610 | &mut self, |
| 4611 | at_mi: (usize, usize), |
| 4612 | w_mi: usize, |
| 4613 | h_mi: usize, |
| 4614 | skip: bool, |
| 4615 | is_inter: bool, |
| 4616 | ref_frame: i8, |
| 4617 | filter: [u8; 2], |
| 4618 | skip_mode: bool, |
| 4619 | ) { |
| 4620 | let (r, c) = at_mi; |
| 4621 | let altref = is_inter && ref_frame == crate::mvstack::ALTREF_FRAME; |
| 4622 | for cell in 0..w_mi { |
| 4623 | self.above_skip[c + cell] = skip; |
| 4624 | self.above_skip_mode[c + cell] = skip_mode; |
| 4625 | self.above_inter[c + cell] = is_inter; |
| 4626 | self.above_ref[c + cell] = ref_frame; |
| 4627 | // lane-av1comp: no caller of `record_inter` ever decodes a |
| 4628 | // compound block yet (see [`Self::above_ref1`]'s doc); the |
| 4629 | // parameter this becomes lands with `read_ref_frames`. |
| 4630 | self.above_ref1[c + cell] = None; |
| 4631 | // libaom `get_comp_group_idx_context`/`get_comp_index_context`'s |
| 4632 | // single-ref special case: `ref_frame[0] == ALTREF_FRAME` reads |
| 4633 | // as `3`/`1` even though this block has no second reference. |
| 4634 | // `record_compound_ctx` overwrites this with the real bit for an |
| 4635 | // actual compound block's cells, called right after this one. |
| 4636 | self.above_comp_group_idx[c + cell] = if altref { 3 } else { 0 }; |
| 4637 | self.above_compound_idx[c + cell] = u8::from(altref); |
| 4638 | self.above_filter[c + cell] = filter; |
| 4639 | } |
| 4640 | for cell in 0..h_mi { |
| 4641 | self.left_skip[r + cell] = skip; |
| 4642 | self.left_skip_mode[r + cell] = skip_mode; |
| 4643 | self.left_inter[r + cell] = is_inter; |
| 4644 | self.left_ref[r + cell] = ref_frame; |
| 4645 | self.left_ref1[r + cell] = None; |
| 4646 | self.left_comp_group_idx[r + cell] = if altref { 3 } else { 0 }; |
| 4647 | self.left_compound_idx[r + cell] = u8::from(altref); |
| 4648 | self.left_filter[r + cell] = filter; |
| 4649 | } |
| 4650 | } |
| 4651 | |
| 4652 | /// Overwrites [`Self::record_inter`]'s ALTREF-special-case default with |
| 4653 | /// a real compound block's own `comp_group_idx`/`compound_idx` bits |
| 4654 | /// (libaom `has_second_ref(mbmi)` branch of `get_comp_group_idx_context`/ |
| 4655 | /// `get_comp_index_context`) -- called right after `record_inter` for |
| 4656 | /// the same `at`/`side`, only on the `COMPOUND_REFERENCE` path. Also |
| 4657 | /// stamps `above_ref1`/`left_ref1` with the block's real second |
| 4658 | /// reference, which `record_inter` always clears to `None` (lane-av1comp: |
| 4659 | /// this is the one caller that actually knows it) -- so the next block's |
| 4660 | /// [`NeighbourRef`](crate::mvstack::NeighbourRef) sees a real compound |
| 4661 | /// neighbour instead of `record_inter`'s single-ref default. |
| 4662 | fn record_compound_ctx( |
| 4663 | &mut self, |
| 4664 | at: (usize, usize), |
| 4665 | side: usize, |
| 4666 | ref1: i8, |
| 4667 | comp_group_idx: u8, |
| 4668 | compound_idx: u8, |
| 4669 | ) { |
| 4670 | self.record_compound_ctx_rect(at, side, side, ref1, comp_group_idx, compound_idx); |
| 4671 | } |
| 4672 | |
| 4673 | /// [`Self::record_compound_ctx`] with independent above/left extents, |
| 4674 | /// mirroring [`Self::record_inter_rect`] (lane-partitions r1). |
| 4675 | #[allow(clippy::too_many_arguments)] |
| 4676 | fn record_compound_ctx_rect( |
| 4677 | &mut self, |
| 4678 | at: (usize, usize), |
| 4679 | w_sub: usize, |
| 4680 | h_sub: usize, |
| 4681 | ref1: i8, |
| 4682 | comp_group_idx: u8, |
| 4683 | compound_idx: u8, |
| 4684 | ) { |
| 4685 | let (r, c) = at; |
| 4686 | self.record_compound_ctx_rect_mi( |
| 4687 | (r * (SUB / MI), c * (SUB / MI)), |
| 4688 | w_sub / MI, |
| 4689 | h_sub / MI, |
| 4690 | ref1, |
| 4691 | comp_group_idx, |
| 4692 | compound_idx, |
| 4693 | ); |
| 4694 | } |
| 4695 | |
| 4696 | /// [`Self::record_compound_ctx_rect`] in mi units (lane-inter8 r2, see |
| 4697 | /// [`Self::record_inter_rect_mi`]). |
| 4698 | fn record_compound_ctx_rect_mi( |
| 4699 | &mut self, |
| 4700 | at_mi: (usize, usize), |
| 4701 | w_mi: usize, |
| 4702 | h_mi: usize, |
| 4703 | ref1: i8, |
| 4704 | comp_group_idx: u8, |
| 4705 | compound_idx: u8, |
| 4706 | ) { |
| 4707 | let (r, c) = at_mi; |
| 4708 | for cell in 0..w_mi { |
| 4709 | self.above_ref1[c + cell] = Some(ref1); |
| 4710 | self.above_comp_group_idx[c + cell] = comp_group_idx; |
| 4711 | self.above_compound_idx[c + cell] = compound_idx; |
| 4712 | } |
| 4713 | for cell in 0..h_mi { |
| 4714 | self.left_ref1[r + cell] = Some(ref1); |
| 4715 | self.left_comp_group_idx[r + cell] = comp_group_idx; |
| 4716 | self.left_compound_idx[r + cell] = compound_idx; |
| 4717 | } |
| 4718 | } |
| 4719 | |
| 4720 | /// The context of a block's partition symbol: delegates to the mi-precise |
| 4721 | /// reader (same pattern as `around`/`around_mi`) -- `above_side`/ |
| 4722 | /// `left_side` are only ever advanced in whole-[`SUB`] steps by |
| 4723 | /// [`Self::record`], so a leaf8's `record_mi` (lane-av1-rect) -- which |
| 4724 | /// only touches the finer mi arrays -- leaves them stale for the *next* |
| 4725 | /// sibling's own partition symbol. |
| 4726 | fn partition_ctx(&self, at: (usize, usize), side: usize) -> usize { |
| 4727 | let (r, c) = at; |
| 4728 | self.partition_ctx_mi((r * (SUB / MI), c * (SUB / MI)), side) |
| 4729 | } |
| 4730 | |
| 4731 | /// [`Self::partition_ctx`] at mi granularity, for an 8x8 leaf of a |
| 4732 | /// straddling 16x16 block: reads the finer `above_side_mi`/`left_side_mi` |
| 4733 | /// arrays so the second leaf sees the first leaf's own 8x8 side rather |
| 4734 | /// than the enclosing 16x16 slot's stale, shared state. |
| 4735 | fn partition_ctx_mi(&self, (mi_r, mi_c): (usize, usize), side: usize) -> usize { |
| 4736 | 2 * usize::from(self.left_side_mi[mi_r] * 2 <= side) |
| 4737 | + usize::from(self.above_side_mi[mi_c] * 2 <= side) |
| 4738 | } |
| 4739 | |
| 4740 | /// The gathered coded/DC-sign state of the blocks above and to the left |
| 4741 | /// of a block `side` samples across, per plane. |
| 4742 | fn around(&self, (r, c): (usize, usize), side: usize) -> [(bool, bool, i32); 3] { |
| 4743 | self.around_mi((r * (SUB / MI), c * (SUB / MI)), side) |
| 4744 | } |
| 4745 | |
| 4746 | /// [`Self::around`] taking the block's position directly in 4x4 mode-info |
| 4747 | /// units, for the same reason [`Self::record_mi`] does. |
| 4748 | fn around_mi(&self, (mi_r, mi_c): (usize, usize), side: usize) -> [(bool, bool, i32); 3] { |
| 4749 | let side_mi = side / MI; |
| 4750 | std::array::from_fn(|plane| { |
| 4751 | let mut above_coded = false; |
| 4752 | let mut left_coded = false; |
| 4753 | let mut vote = 0; |
| 4754 | for cell in 0..side_mi { |
| 4755 | let above = &self.above[mi_c + cell][plane]; |
| 4756 | let left = &self.left[mi_r + cell][plane]; |
| 4757 | above_coded |= above.level != 0; |
| 4758 | left_coded |= left.level != 0; |
| 4759 | vote += dc_vote(above.dc) + dc_vote(left.dc); |
| 4760 | } |
| 4761 | (above_coded, left_coded, vote) |
| 4762 | }) |
| 4763 | } |
| 4764 | |
| 4765 | /// [`Self::around`] with independent above (`w`)/left (`h`) extents, in |
| 4766 | /// pixels -- lane-rectwire, mirrors [`Self::record_rect`]'s asymmetric |
| 4767 | /// extent pattern for a true rectangular strip's own coefficient context. |
| 4768 | fn around_rect(&self, (r, c): (usize, usize), w: usize, h: usize) -> [(bool, bool, i32); 3] { |
| 4769 | self.around_mi_rect((r * (SUB / MI), c * (SUB / MI)), w, h) |
| 4770 | } |
| 4771 | |
| 4772 | /// [`Self::around_rect`] taking the block's position directly in 4x4 |
| 4773 | /// mode-info units, mirroring [`Self::around_mi`]. |
| 4774 | fn around_mi_rect(&self, (mi_r, mi_c): (usize, usize), w: usize, h: usize) -> [(bool, bool, i32); 3] { |
| 4775 | let (w_mi, h_mi) = (w / MI, h / MI); |
| 4776 | std::array::from_fn(|plane| { |
| 4777 | let mut above_coded = false; |
| 4778 | let mut left_coded = false; |
| 4779 | let mut vote = 0; |
| 4780 | for cell in 0..w_mi { |
| 4781 | let above = &self.above[mi_c + cell][plane]; |
| 4782 | above_coded |= above.level != 0; |
| 4783 | vote += dc_vote(above.dc); |
| 4784 | } |
| 4785 | for cell in 0..h_mi { |
| 4786 | let left = &self.left[mi_r + cell][plane]; |
| 4787 | left_coded |= left.level != 0; |
| 4788 | vote += dc_vote(left.dc); |
| 4789 | } |
| 4790 | (above_coded, left_coded, vote) |
| 4791 | }) |
| 4792 | } |
| 4793 | |
| 4794 | /// The luma `txb_skip_ctx` of a transform unit smaller than its own block |
| 4795 | /// (spec `get_txb_ctx_general`, plane 0, `plane_bsize != tx_size`): the |
| 4796 | /// above/left neighbours' magnitude tiers, OR-reduced over the unit's own |
| 4797 | /// span and each clamped to 4, indexed into [`SKIP_CONTEXTS`]. Unlike |
| 4798 | /// [`Self::around_mi`]'s plain coded/uncoded bit, this needs the real |
| 4799 | /// cumulative level -- a lone-TU block never calls this (its `txb_skip_ctx` |
| 4800 | /// is always 0, spec's `plane_bsize == tx_size` branch). |
| 4801 | fn luma_skip_ctx(&self, at_mi: (usize, usize), side_mi: usize) -> usize { |
| 4802 | self.luma_skip_ctx_rect(at_mi, side_mi, side_mi) |
| 4803 | } |
| 4804 | |
| 4805 | /// [`Self::luma_skip_ctx`] with the unit's own width and height in mi |
| 4806 | /// units (lane-rectsplitx r1) -- `get_txb_ctx_general` ORs the above |
| 4807 | /// context over `tx_size_wide` and the left one over `tx_size_high`. |
| 4808 | fn luma_skip_ctx_rect( |
| 4809 | &self, |
| 4810 | (mi_r, mi_c): (usize, usize), |
| 4811 | w_mi: usize, |
| 4812 | h_mi: usize, |
| 4813 | ) -> usize { |
| 4814 | let side_mi = w_mi; |
| 4815 | let mut top = 0u8; |
| 4816 | let mut left = 0u8; |
| 4817 | for cell in 0..w_mi { |
| 4818 | top |= self.above[mi_c + cell][0].level; |
| 4819 | } |
| 4820 | for cell in 0..h_mi { |
| 4821 | left |= self.left[mi_r + cell][0].level; |
| 4822 | } |
| 4823 | let ctx = SKIP_CONTEXTS[(top as usize).min(4)][(left as usize).min(4)]; |
| 4824 | if std::env::var_os("EC_AV1_TRACE").is_some() { |
| 4825 | eprintln!( |
| 4826 | "TRACE luma_skip_ctx mi=({mi_r},{mi_c}) side_mi={side_mi} top={top} left={left} ctx={ctx}" |
| 4827 | ); |
| 4828 | } |
| 4829 | ctx |
| 4830 | } |
| 4831 | |
| 4832 | /// Writes one coded block into every cell it covers, on both grids: the |
| 4833 | /// mode/side arrays up to `side`'s own width, and the coefficient-context |
| 4834 | /// arrays up to the true frame edge — the units past it are left at their |
| 4835 | /// default (uncoded), even mid-cell, exactly as [`crate::tile`]'s own |
| 4836 | /// `record` leaves them (spec `av1_set_entropy_contexts`, which clamps to |
| 4837 | /// `blocks_wide`/`blocks_high` derived from the true `mi_cols`/`mi_rows`, |
| 4838 | /// not from this block's own side). |
| 4839 | fn record( |
| 4840 | &mut self, |
| 4841 | at: (usize, usize), |
| 4842 | side: usize, |
| 4843 | mode: usize, |
| 4844 | uv_mode: usize, |
| 4845 | grids: &[Vec<i32>; 3], |
| 4846 | ) { |
| 4847 | self.record_rect(at, side, side, mode, uv_mode, grids); |
| 4848 | } |
| 4849 | |
| 4850 | /// [`Self::record`] with independent above (`w`)/left (`h`) extents, in |
| 4851 | /// pixels -- lane-partitions r1: `above_side`/`left_side` (read back by |
| 4852 | /// [`Self::partition_ctx`]) are the neighbour's own width/height per the |
| 4853 | /// spec's per-edge partition-context rule, so a true rectangular block |
| 4854 | /// naturally wants two different values here rather than one. |
| 4855 | /// Writes this block's luma `mode` into the mi-granular |
| 4856 | /// [`Self::sub8_mode_col`]/[`Self::sub8_mode_row`] maps, keyed by the mi |
| 4857 | /// position a *later* neighbour would read it from: the block's LAST mi |
| 4858 | /// row for every column it spans (what the block below reads via |
| 4859 | /// `mi(row-1, col)`) and its LAST mi column for every row (what the |
| 4860 | /// block to the right reads via `mi(row, col-1)`). Only sub-8x8 leaves |
| 4861 | /// read these back, and only when the recorded position is exactly their |
| 4862 | /// own neighbour -- see [`Self::sub8_mode_col`]'s doc. |
| 4863 | fn record_mode_mi( |
| 4864 | &mut self, |
| 4865 | mi_r: usize, |
| 4866 | mi_c: usize, |
| 4867 | mi_w: usize, |
| 4868 | mi_h: usize, |
| 4869 | mode: usize, |
| 4870 | ) { |
| 4871 | let (last_r, last_c) = (mi_r + mi_h - 1, mi_c + mi_w - 1); |
| 4872 | for cell in 0..mi_w { |
| 4873 | if let Some(slot) = self.sub8_mode_col.get_mut(mi_c + cell) { |
| 4874 | *slot = (last_r, mode); |
| 4875 | } |
| 4876 | } |
| 4877 | for cell in 0..mi_h { |
| 4878 | if let Some(slot) = self.sub8_mode_row.get_mut(mi_r + cell) { |
| 4879 | *slot = (last_c, mode); |
| 4880 | } |
| 4881 | } |
| 4882 | } |
| 4883 | |
| 4884 | /// The mi-exact above/left neighbour luma mode for a block whose top-left |
| 4885 | /// mi is `(mi_r, mi_c)`, or `None` when no block has recorded that exact |
| 4886 | /// mi position (then the coarse [`SUB`]-grid slot is right by |
| 4887 | /// construction). See [`Self::record_mode_mi`]. |
| 4888 | fn mode_above_mi(&self, mi_r: usize, mi_c: usize) -> Option<usize> { |
| 4889 | match self.sub8_mode_col.get(mi_c) { |
| 4890 | Some(&(row, m)) if mi_r > self.tile_row0_mi && row == mi_r - 1 => Some(m), |
| 4891 | _ => None, |
| 4892 | } |
| 4893 | } |
| 4894 | |
| 4895 | fn mode_left_mi(&self, mi_r: usize, mi_c: usize) -> Option<usize> { |
| 4896 | match self.sub8_mode_row.get(mi_r) { |
| 4897 | Some(&(col, m)) if mi_c > self.tile_col0_mi && col == mi_c - 1 => Some(m), |
| 4898 | _ => None, |
| 4899 | } |
| 4900 | } |
| 4901 | |
| 4902 | /// [`Self::record_mode_mi`] for the block's `uv_mode`, into |
| 4903 | /// [`Self::uv_mode_col`]/[`Self::uv_mode_row`]. |
| 4904 | fn record_uv_mode_mi( |
| 4905 | &mut self, |
| 4906 | mi_r: usize, |
| 4907 | mi_c: usize, |
| 4908 | mi_w: usize, |
| 4909 | mi_h: usize, |
| 4910 | uv_mode: usize, |
| 4911 | ) { |
| 4912 | let (last_r, last_c) = (mi_r + mi_h - 1, mi_c + mi_w - 1); |
| 4913 | for cell in 0..mi_w { |
| 4914 | if let Some(slot) = self.uv_mode_col.get_mut(mi_c + cell) { |
| 4915 | *slot = (last_r, uv_mode); |
| 4916 | } |
| 4917 | } |
| 4918 | for cell in 0..mi_h { |
| 4919 | if let Some(slot) = self.uv_mode_row.get_mut(mi_r + cell) { |
| 4920 | *slot = (last_c, uv_mode); |
| 4921 | } |
| 4922 | } |
| 4923 | } |
| 4924 | |
| 4925 | /// Whether either mi-exact chroma neighbour of the block at `(mi_r, mi_c)` |
| 4926 | /// coded a smooth `uv_mode` -- libaom's `intra_edge_filter_type` for |
| 4927 | /// planes 1/2. Falls back to the coarse [`SUB`] slot on whichever axis no |
| 4928 | /// block has recorded the exact neighbouring mi. |
| 4929 | fn smooth_uv_neighbour(&self, mi_r: usize, mi_c: usize, r: usize, c: usize) -> bool { |
| 4930 | // lane-kf900 r2: libaom reads the chroma neighbours off the CHROMA |
| 4931 | // REFERENCE block, not off the mi that carries the chroma syntax. |
| 4932 | // `set_mi_row_col` (`blockd.h`) takes |
| 4933 | // `base_mi = &xd->mi[-(mi_row & ss_y) * stride - (mi_col & ss_x)]` and |
| 4934 | // only then `chroma_above_mbmi = base_mi[-stride]` / |
| 4935 | // `chroma_left_mbmi = base_mi[-1]`. A sub-8 pair codes its chroma at |
| 4936 | // the ODD mi, so without the snap we looked one mi row/column too far |
| 4937 | // down/right -- the mi-exact slot never matched and the coarse |
| 4938 | // fallback answered instead. Measured on a 4K 10-bit key frame, chroma |
| 4939 | // 8x4 at (688, 100) = mi (51, 344): libaom's |
| 4940 | // `get_intra_edge_filter_type` is 1 (the chroma block above it is |
| 4941 | // `UV_SMOOTH_PRED`), ours was 0, which flips |
| 4942 | // `av1_use_intra_edge_upsample(8, 4, -6, type)` (`blk_wh == 12`: |
| 4943 | // false for type 1, TRUE for type 0) and upsampled an edge libaom |
| 4944 | // leaves alone -- +1 on rows 2/3 of that block and every chroma block |
| 4945 | // predicting from it (class `context-read-from-one-cell`). |
| 4946 | self.smooth_uv_neighbour_unsnapped(mi_r & !1, mi_c & !1, r, c) |
| 4947 | } |
| 4948 | |
| 4949 | /// [`Self::smooth_uv_neighbour`] without the chroma-reference snap, i.e. |
| 4950 | /// read off the mi passed in exactly. Only the 16x4/4x16 pair witness |
| 4951 | /// counter wants this: it measures how often the snap CHANGES the answer, |
| 4952 | /// which is what the lane-kf1200/kf900 chroma-reference fix buys. Every |
| 4953 | /// decode path uses the snapping form. |
| 4954 | fn smooth_uv_neighbour_unsnapped( |
| 4955 | &self, |
| 4956 | mi_r: usize, |
| 4957 | mi_c: usize, |
| 4958 | r: usize, |
| 4959 | c: usize, |
| 4960 | ) -> bool { |
| 4961 | // lane-mtfix r1: availability is TILE-relative on BOTH axes and on the |
| 4962 | // coarse fallback too. `above_uv_mode` is a frame-wide [`SUB`]-grid |
| 4963 | // array that `start_tile` does not clear, so a block on a tile's first |
| 4964 | // mi row used to read the uv_mode of the block above it in the tile |
| 4965 | // ABOVE and filter its chroma edge at that neighbour's strength |
| 4966 | // (2x2-tile intra gate, seed 46: chroma-only +-1..2 over the 8x8 block |
| 4967 | // at chroma (120, 32), the top-right tile's first block). |
| 4968 | let have_above = mi_r > self.tile_row0_mi; |
| 4969 | let have_left = mi_c > self.tile_col0_mi; |
| 4970 | if self.tile_row0_mi > 0 && !have_above && self.above_uv_mode[c] != DC_PRED |
| 4971 | || self.tile_col0_mi > 0 && !have_left && self.left_uv_mode[r] != DC_PRED |
| 4972 | { |
| 4973 | UV_TILE_EDGE_SUPPRESSED_HITS.with(|h| h.set(h.get() + 1)); |
| 4974 | } |
| 4975 | let above = match self.uv_mode_col.get(mi_c) { |
| 4976 | Some(&(row, m)) if have_above && row == mi_r - 1 => m, |
| 4977 | _ if have_above => self.above_uv_mode[c], |
| 4978 | _ => DC_PRED, |
| 4979 | }; |
| 4980 | let left = match self.uv_mode_row.get(mi_r) { |
| 4981 | Some(&(col, m)) if have_left && col == mi_c - 1 => m, |
| 4982 | _ if have_left => self.left_uv_mode[r], |
| 4983 | _ => DC_PRED, |
| 4984 | }; |
| 4985 | // lane-cfl r1 counter: how often the mi-exact chroma neighbour differs |
| 4986 | // from the coarse [`SUB`] slot -- i.e. how often a chroma edge would |
| 4987 | // have been filtered at the wrong strength by the coarse map alone. |
| 4988 | if above != self.above_uv_mode[c] || left != self.left_uv_mode[r] { |
| 4989 | UV_MODE_MI_OVERRIDE_HITS.with(|h| h.set(h.get() + 1)); |
| 4990 | } |
| 4991 | is_smooth_mode(above) || is_smooth_mode(left) |
| 4992 | } |
| 4993 | |
| 4994 | /// The above/left luma modes libaom's `above_mi`/`left_mi` really see for a |
| 4995 | /// block at coarse [`SUB`]-grid `(r, c)`. lane-rectx r5: the coarse |
| 4996 | /// `above_mode`/`left_mode` slots are written in 16x16 cells, so a |
| 4997 | /// sub-16x16 neighbour (a 16x8 rect leaf, an 8x8 split cell) leaves them |
| 4998 | /// holding an OLDER, larger block's mode -- it only ever recorded into the |
| 4999 | /// mi-exact map. Every `kf_y_mode` reader goes through here, not just the |
| 5000 | /// strip path ([`decode_leaf_rect`] took this override in r4; a 32x16 strip |
| 5001 | /// whose left neighbour was a 16x8 leaf still read the stale row and picked |
| 5002 | /// a different CDF for the same mode value). |
| 5003 | /// lane-rectsplitx r2: availability-correct above/left luma modes for a |
| 5004 | /// block whose top-left mi is `(mi_r, mi_c)`. libaom reads `above_mbmi` |
| 5005 | /// only when `up_available` and `left_mbmi` only when `left_available`; |
| 5006 | /// the coarse [`SUB`] bands cannot express that, and with 8-px strips they |
| 5007 | /// actively lie -- four 32x8 strips of one `HORZ_4` share ONE 16x16 |
| 5008 | /// `left_mode` cell, so the second strip at tile column 0 read the FIRST |
| 5009 | /// strip's mode as its "left neighbour" (measured on the band fixture at |
| 5010 | /// mi(10,0): mode 2 where libaom has DC, a different `kf_y_mode` row). |
| 5011 | fn modes_above_left_mi( |
| 5012 | &self, |
| 5013 | mi_r: usize, |
| 5014 | mi_c: usize, |
| 5015 | fallback: (usize, usize), |
| 5016 | ) -> (usize, usize) { |
| 5017 | let above = if mi_r > self.tile_row0_mi { |
| 5018 | self.mode_above_mi(mi_r, mi_c).unwrap_or(fallback.0) |
| 5019 | } else { |
| 5020 | DC_PRED |
| 5021 | }; |
| 5022 | let left = if mi_c > self.tile_col0_mi { |
| 5023 | self.mode_left_mi(mi_r, mi_c).unwrap_or(fallback.1) |
| 5024 | } else { |
| 5025 | DC_PRED |
| 5026 | }; |
| 5027 | (above, left) |
| 5028 | } |
| 5029 | |
| 5030 | /// lane-modectx r1: the coarse-[`SUB`]-grid entry point, now availability |
| 5031 | /// correct. The mi-exact lookups already refuse to cross a tile edge, but |
| 5032 | /// their FALLBACK (`above_mode`/`left_mode`) did not: `left_mode` is reset |
| 5033 | /// per superblock row and never per tile, so the first block column of |
| 5034 | /// tile 2..N read the previous tile's mode as its left neighbour, and the |
| 5035 | /// above band survives across a tile ROW boundary the same way lane-mtfix's |
| 5036 | /// `above_uv_mode` did. libaom reads `above_mbmi`/`left_mbmi` only when |
| 5037 | /// `up_available`/`left_available` and uses DC_PRED otherwise |
| 5038 | /// ([`Self::modes_above_left_mi`]). |
| 5039 | fn modes_above_left(&self, r: usize, c: usize) -> (usize, usize) { |
| 5040 | let (mi_r, mi_c) = (r * (SUB / MI), c * (SUB / MI)); |
| 5041 | let (above, left) = |
| 5042 | self.modes_above_left_mi(mi_r, mi_c, (self.above_mode[c], self.left_mode[r])); |
| 5043 | if above != self.above_mode[c] || left != self.left_mode[r] { |
| 5044 | MODE_MI_OVERRIDE_HITS.with(|h| h.set(h.get() + 1)); |
| 5045 | } |
| 5046 | // A neighbour the coarse band offered where the tile edge makes it |
| 5047 | // unavailable -- must never happen (see `MODE_TILE_EDGE_COARSE_LEAKS`). |
| 5048 | if (mi_r <= self.tile_row0_mi && self.above_mode[c] != DC_PRED) |
| 5049 | || (mi_c <= self.tile_col0_mi && self.left_mode[r] != DC_PRED) |
| 5050 | { |
| 5051 | MODE_TILE_EDGE_COARSE_LEAKS.with(|h| h.set(h.get() + 1)); |
| 5052 | } |
| 5053 | (above, left) |
| 5054 | } |
| 5055 | |
| 5056 | fn record_rect( |
| 5057 | &mut self, |
| 5058 | at: (usize, usize), |
| 5059 | w: usize, |
| 5060 | h: usize, |
| 5061 | mode: usize, |
| 5062 | uv_mode: usize, |
| 5063 | grids: &[Vec<i32>; 3], |
| 5064 | ) { |
| 5065 | self.record_rect_mi(sub16_to_mi(at), w, h, mode, uv_mode, grids); |
| 5066 | } |
| 5067 | |
| 5068 | /// [`Self::record_rect`] in mi (4 px) units -- lane-inter4 r1: a |
| 5069 | /// 32x8/8x32 strip of a 32-level 1:4 partition sits at an 8-px offset the |
| 5070 | /// [`SUB`]-unit `at` cannot name. The coarse 16-px bands are still keyed |
| 5071 | /// by the enclosing cell (a sub-16 extent writes none of them, exactly as |
| 5072 | /// the [`SUB`]-unit path does). |
| 5073 | fn record_rect_mi( |
| 5074 | &mut self, |
| 5075 | at_mi: (usize, usize), |
| 5076 | w: usize, |
| 5077 | h: usize, |
| 5078 | mode: usize, |
| 5079 | uv_mode: usize, |
| 5080 | grids: &[Vec<i32>; 3], |
| 5081 | ) { |
| 5082 | let (r, c) = (at_mi.0 / (SUB / MI), at_mi.1 / (SUB / MI)); |
| 5083 | // lane-inter4 r2: a strip whose own footprint runs past the frame's |
| 5084 | // last 16-px column/row stamps only the cells that exist (libaom's |
| 5085 | // arrays are padded past the frame and those cells are never read). |
| 5086 | for cell in 0..w / SUB { |
| 5087 | if c + cell >= self.above_mode.len() { |
| 5088 | break; |
| 5089 | } |
| 5090 | self.above_mode[c + cell] = mode; |
| 5091 | self.above_uv_mode[c + cell] = uv_mode; |
| 5092 | self.above_side[c + cell] = w; |
| 5093 | } |
| 5094 | for cell in 0..h / SUB { |
| 5095 | if r + cell >= self.left_mode.len() { |
| 5096 | break; |
| 5097 | } |
| 5098 | self.left_mode[r + cell] = mode; |
| 5099 | self.left_uv_mode[r + cell] = uv_mode; |
| 5100 | self.left_side[r + cell] = h; |
| 5101 | } |
| 5102 | self.record_mode_mi(at_mi.0, at_mi.1, w / MI, h / MI, mode); |
| 5103 | self.record_uv_mode_mi(at_mi.0, at_mi.1, w / MI, h / MI, uv_mode); |
| 5104 | self.record_mi_rect(at_mi, w, h, grids); |
| 5105 | } |
| 5106 | |
| 5107 | /// The coefficient-context half of [`Self::record`], taking the block's |
| 5108 | /// position directly in 4x4 mode-info units rather than [`SUB`]-grid |
| 5109 | /// `(r, c)`: an 8x8 leaf under a straddling 16x16 (lane-av1-rect) sits at |
| 5110 | /// a mi offset [`Self::record`]'s SUB-unit `at` cannot name. |
| 5111 | fn record_mi(&mut self, at_mi: (usize, usize), side: usize, grids: &[Vec<i32>; 3]) { |
| 5112 | self.record_mi_rect(at_mi, side, side, grids); |
| 5113 | } |
| 5114 | |
| 5115 | /// [`Self::record_mi`] with independent above (`w`)/left (`h`) extents, |
| 5116 | /// in pixels (lane-partitions r1, mirroring [`Self::record_rect`]). |
| 5117 | fn record_mi_rect( |
| 5118 | &mut self, |
| 5119 | at_mi: (usize, usize), |
| 5120 | w: usize, |
| 5121 | h: usize, |
| 5122 | grids: &[Vec<i32>; 3], |
| 5123 | ) { |
| 5124 | let (mi_r, mi_c) = at_mi; |
| 5125 | let states: [Neighbour; 3] = std::array::from_fn(|plane| neighbour_state(&grids[plane])); |
| 5126 | let (w_mi, h_mi) = (w / MI, h / MI); |
| 5127 | for cell in 0..h_mi { |
| 5128 | if let Some(slot) = self.left_side_mi.get_mut(mi_r + cell) { |
| 5129 | *slot = h; |
| 5130 | } |
| 5131 | } |
| 5132 | for cell in 0..w_mi { |
| 5133 | if let Some(slot) = self.above_side_mi.get_mut(mi_c + cell) { |
| 5134 | *slot = w; |
| 5135 | } |
| 5136 | } |
| 5137 | // A chroma 4x4 unit straddling the true luma edge is still whole in |
| 5138 | // chroma's own halved grid, so libaom rounds the luma bound up to the |
| 5139 | // plane's own 4x4 unit before clamping a subsampled plane |
| 5140 | // (`av1_write_intra_coeffs_mb`, encodetxb.c). |
| 5141 | let round_up_even = |n: usize| n.div_ceil(2) * 2; |
| 5142 | let bound_h = [ |
| 5143 | self.mi_rows, |
| 5144 | round_up_even(self.mi_rows), |
| 5145 | round_up_even(self.mi_rows), |
| 5146 | ]; |
| 5147 | let bound_w = [ |
| 5148 | self.mi_cols, |
| 5149 | round_up_even(self.mi_cols), |
| 5150 | round_up_even(self.mi_cols), |
| 5151 | ]; |
| 5152 | for cell in 0..h_mi { |
| 5153 | self.left[mi_r + cell] = std::array::from_fn(|plane| { |
| 5154 | if cell < h_mi.min(bound_h[plane].saturating_sub(mi_r)) { |
| 5155 | states[plane] |
| 5156 | } else { |
| 5157 | Default::default() |
| 5158 | } |
| 5159 | }); |
| 5160 | } |
| 5161 | for cell in 0..w_mi { |
| 5162 | self.above[mi_c + cell] = std::array::from_fn(|plane| { |
| 5163 | if cell < w_mi.min(bound_w[plane].saturating_sub(mi_c)) { |
| 5164 | states[plane] |
| 5165 | } else { |
| 5166 | Default::default() |
| 5167 | } |
| 5168 | }); |
| 5169 | } |
| 5170 | } |
| 5171 | |
| 5172 | /// [`Self::record_mi`]'s plane-0 half only, for one luma transform unit |
| 5173 | /// inside a coded block whose luma transform is smaller than its own |
| 5174 | /// side (`TxMode::Select`): the real per-transform-unit coefficient |
| 5175 | /// context (spec `AboveLevelContext`/`LeftLevelContext`), unlike the |
| 5176 | /// coarse per-block `tx_grid`/`fill_lf_grid` state |
| 5177 | /// [`Self::record_split_luma`] still writes once for the whole block. |
| 5178 | /// Leaves `above_side_mi`/`left_side_mi` and the chroma planes alone -- |
| 5179 | /// the caller's own [`Self::record_split_luma`] call sets those once, |
| 5180 | /// at the block's own true side, not this TU's. |
| 5181 | fn record_mi_luma(&mut self, at_mi: (usize, usize), tx_px: usize, grid: &[i32]) { |
| 5182 | self.record_mi_luma_rect(at_mi, tx_px, tx_px, grid); |
| 5183 | } |
| 5184 | |
| 5185 | /// [`Self::record_mi_luma`] for ONE CHROMA transform unit of a block whose |
| 5186 | /// uv plane block is several units (lane-sb128b r2: a 128x128 block's |
| 5187 | /// 64x64 uv plane block is four TX_32X32 units, `av1_get_max_uv_txsize`). |
| 5188 | /// `w_px`/`h_px` are the unit's extent in LUMA pixels -- the chroma |
| 5189 | /// coefficient-context arrays are indexed by luma mi, the convention |
| 5190 | /// [`Self::record_split_luma_rect_mi`] already writes them in, frame-edge |
| 5191 | /// clamp included. |
| 5192 | fn record_mi_chroma( |
| 5193 | &mut self, |
| 5194 | (mi_r, mi_c): (usize, usize), |
| 5195 | w_px: usize, |
| 5196 | h_px: usize, |
| 5197 | plane: usize, |
| 5198 | grid: &[i32], |
| 5199 | ) { |
| 5200 | let round_up_even = |n: usize| n.div_ceil(2) * 2; |
| 5201 | let (bound_h, bound_w) = (round_up_even(self.mi_rows), round_up_even(self.mi_cols)); |
| 5202 | let state = neighbour_state(grid); |
| 5203 | for cell in 0..(h_px / MI).min(bound_h.saturating_sub(mi_r)) { |
| 5204 | self.left[mi_r + cell][plane] = state; |
| 5205 | } |
| 5206 | for cell in 0..(w_px / MI).min(bound_w.saturating_sub(mi_c)) { |
| 5207 | self.above[mi_c + cell][plane] = state; |
| 5208 | } |
| 5209 | } |
| 5210 | |
| 5211 | /// [`Self::record_mi_luma`] for a RECTANGULAR transform unit (lane-rectsplitx |
| 5212 | /// r1): the above context spans the unit's width, the left context its |
| 5213 | /// height, which for a square unit is the same loop it always was. |
| 5214 | fn record_mi_luma_rect( |
| 5215 | &mut self, |
| 5216 | (mi_r, mi_c): (usize, usize), |
| 5217 | w_px: usize, |
| 5218 | h_px: usize, |
| 5219 | grid: &[i32], |
| 5220 | ) { |
| 5221 | let state = neighbour_state(grid); |
| 5222 | for cell in 0..h_px / MI { |
| 5223 | if mi_r + cell < self.mi_rows { |
| 5224 | self.left[mi_r + cell][0] = state; |
| 5225 | } |
| 5226 | } |
| 5227 | for cell in 0..w_px / MI { |
| 5228 | if mi_c + cell < self.mi_cols { |
| 5229 | self.above[mi_c + cell][0] = state; |
| 5230 | } |
| 5231 | } |
| 5232 | } |
| 5233 | |
| 5234 | /// [`Self::record`]'s mode/side and chroma-context bookkeeping, for a |
| 5235 | /// block whose luma was decoded as several transform units through |
| 5236 | /// [`Self::record_mi_luma`] rather than [`Self::record`]'s own single |
| 5237 | /// whole-block luma write -- everything [`Self::record`] does except |
| 5238 | /// plane 0, which is already correct per-TU by the time this runs. |
| 5239 | fn record_split_luma( |
| 5240 | &mut self, |
| 5241 | at: (usize, usize), |
| 5242 | side: usize, |
| 5243 | mode: usize, |
| 5244 | uv_mode: usize, |
| 5245 | chroma_grids: [&[i32]; 2], |
| 5246 | ) { |
| 5247 | self.record_split_luma_rect(at, side, side, mode, uv_mode, chroma_grids); |
| 5248 | } |
| 5249 | |
| 5250 | /// [`Self::record_split_luma`] with independent above (`w`)/left (`h`) |
| 5251 | /// extents in pixels (lane-rectsplit r1), the same relationship |
| 5252 | /// [`Self::record_rect`] has to [`Self::record`]: a HORZ/VERT strip whose |
| 5253 | /// luma transform split into several square units needs exactly this |
| 5254 | /// bookkeeping, at its own true rectangular extents. |
| 5255 | fn record_split_luma_rect( |
| 5256 | &mut self, |
| 5257 | at: (usize, usize), |
| 5258 | w: usize, |
| 5259 | h: usize, |
| 5260 | mode: usize, |
| 5261 | uv_mode: usize, |
| 5262 | chroma_grids: [&[i32]; 2], |
| 5263 | ) { |
| 5264 | let (r, c) = at; |
| 5265 | self.record_split_luma_rect_mi( |
| 5266 | (r * (SUB / MI), c * (SUB / MI)), |
| 5267 | w, |
| 5268 | h, |
| 5269 | mode, |
| 5270 | uv_mode, |
| 5271 | chroma_grids, |
| 5272 | ); |
| 5273 | } |
| 5274 | |
| 5275 | /// [`Self::record_split_luma_rect`] addressed in mi units (lane-rectsplitx |
| 5276 | /// r1) -- a strip 8 px tall starts mid-[`SUB`]-cell, which the coarse |
| 5277 | /// `(r, c)` form cannot name. The coarse mode/side arrays are stamped for |
| 5278 | /// every cell the strip TOUCHES (`div_ceil`), last writer wins, the |
| 5279 | /// convention [`decode_block_rect4`] already established for 1:4 strips. |
| 5280 | fn record_split_luma_rect_mi( |
| 5281 | &mut self, |
| 5282 | at_mi: (usize, usize), |
| 5283 | w: usize, |
| 5284 | h: usize, |
| 5285 | mode: usize, |
| 5286 | uv_mode: usize, |
| 5287 | chroma_grids: [&[i32]; 2], |
| 5288 | ) { |
| 5289 | let (mi_r, mi_c) = at_mi; |
| 5290 | let (r, c) = (mi_r / (SUB / MI), mi_c / (SUB / MI)); |
| 5291 | for cell in 0..w.div_ceil(SUB) { |
| 5292 | if c + cell >= self.above_mode.len() { |
| 5293 | break; |
| 5294 | } |
| 5295 | self.above_mode[c + cell] = mode; |
| 5296 | self.above_uv_mode[c + cell] = uv_mode; |
| 5297 | self.above_side[c + cell] = w; |
| 5298 | } |
| 5299 | for cell in 0..h.div_ceil(SUB) { |
| 5300 | if r + cell >= self.left_mode.len() { |
| 5301 | break; |
| 5302 | } |
| 5303 | self.left_mode[r + cell] = mode; |
| 5304 | self.left_uv_mode[r + cell] = uv_mode; |
| 5305 | self.left_side[r + cell] = h; |
| 5306 | } |
| 5307 | |
| 5308 | let (w_mi, h_mi) = (w / MI, h / MI); |
| 5309 | self.record_mode_mi(mi_r, mi_c, w_mi, h_mi, mode); |
| 5310 | self.record_uv_mode_mi(mi_r, mi_c, w_mi, h_mi, uv_mode); |
| 5311 | for cell in 0..h_mi { |
| 5312 | if let Some(slot) = self.left_side_mi.get_mut(mi_r + cell) { |
| 5313 | *slot = h; |
| 5314 | } |
| 5315 | } |
| 5316 | for cell in 0..w_mi { |
| 5317 | if let Some(slot) = self.above_side_mi.get_mut(mi_c + cell) { |
| 5318 | *slot = w; |
| 5319 | } |
| 5320 | } |
| 5321 | let round_up_even = |n: usize| n.div_ceil(2) * 2; |
| 5322 | let (bound_h, bound_w) = (round_up_even(self.mi_rows), round_up_even(self.mi_cols)); |
| 5323 | for (plane_idx, grid) in chroma_grids.into_iter().enumerate() { |
| 5324 | let plane = plane_idx + 1; |
| 5325 | let state = neighbour_state(grid); |
| 5326 | for cell in 0..h_mi { |
| 5327 | if cell < h_mi.min(bound_h.saturating_sub(mi_r)) { |
| 5328 | self.left[mi_r + cell][plane] = state; |
| 5329 | } |
| 5330 | } |
| 5331 | for cell in 0..w_mi { |
| 5332 | if cell < w_mi.min(bound_w.saturating_sub(mi_c)) { |
| 5333 | self.above[mi_c + cell][plane] = state; |
| 5334 | } |
| 5335 | } |
| 5336 | } |
| 5337 | } |
| 5338 | } |
| 5339 | |
| 5340 | /// Reads one coded block's skip flag, luma mode, angle delta (if the mode |
| 5341 | /// carries one) and chroma mode, mirroring [`crate::tile`]'s `write_intra_mode` |
| 5342 | /// exactly — including its skip context, which the real key-frame writer |
| 5343 | /// always writes at a fixed context of zero (it never tracks a neighbour-based |
| 5344 | /// one for intra blocks; that is the inter-frame writer's rule instead). |
| 5345 | /// Refuses a nonzero angle delta: this decoder reconstructs the thirteen |
| 5346 | /// luma modes at their base angle only. Chroma is DC-only or, where CFL is |
| 5347 | /// offered (`cfl`, `is_cfl_allowed`, spec 5.11.5), chroma-from-luma -- the |
| 5348 | /// third element carries that mode's `(alpha_q3_u, alpha_q3_v)`, `None` for |
| 5349 | /// plain DC chroma. |
| 5350 | /// spec 5.11.47 / libaom `av1_read_tx_type`'s `fimode_to_intradir`: which |
| 5351 | /// `intra_ext_tx_cdf` row a luma transform reads. A filter-intra block's |
| 5352 | /// `mode` is always `DC_PRED`, so the row comes from the FILTER-INTRA mode |
| 5353 | /// instead (the defect commit 1a89ed9 fixed at the square sites). |
| 5354 | fn fi_tx_row(mode: usize, filter_intra: Option<usize>) -> usize { |
| 5355 | const FIMODE_TO_INTRADIR: [usize; 5] = [DC_PRED, V_PRED, H_PRED, D157_PRED, DC_PRED]; |
| 5356 | filter_intra.map_or(mode, |fi| FIMODE_TO_INTRADIR[fi]) |
| 5357 | } |
| 5358 | |
| 5359 | /// A block-size class index into [`cdf::FILTER_INTRA`]/`Cdfs::filter_intra`, |
| 5360 | /// or `None` when `side` is past `av1_filter_intra_allowed_bsize`'s <=32 |
| 5361 | /// bound (spec `filter_intra_mode_info` never reads a symbol there). |
| 5362 | fn filter_intra_size_class(side: usize) -> Option<usize> { |
| 5363 | match side { |
| 5364 | 4 => Some(0), |
| 5365 | 8 => Some(1), |
| 5366 | 16 => Some(2), |
| 5367 | 32 => Some(3), |
| 5368 | _ => None, |
| 5369 | } |
| 5370 | } |
| 5371 | |
| 5372 | /// [`filter_intra_size_class`] for a true `bw`x`bh` rect strip |
| 5373 | /// (lane-intradisp r1): `av1_filter_intra_allowed_bsize` permits any bsize |
| 5374 | /// with both dims <= 32, which includes `BLOCK_32X16`/`BLOCK_16X32` -- their |
| 5375 | /// own distinct `cdf::FILTER_INTRA` rows, classes 4/5. |
| 5376 | fn filter_intra_size_class_rect(bw: usize, bh: usize) -> Option<usize> { |
| 5377 | match (bw, bh) { |
| 5378 | (32, 16) => Some(4), |
| 5379 | (16, 32) => Some(5), |
| 5380 | // lane-rectx r3: `av1_filter_intra_allowed_bsize` is "both sides <= |
| 5381 | // 32", so a 16x8/8x16 strip reads the flag too -- returning `None` |
| 5382 | // here dropped that symbol on every DC_PRED strip of that size and |
| 5383 | // desynced the tile (caught against an aomdec EC_TRACE_COEFF ladder: |
| 5384 | // identical mode/uv_mode values, range 34808 vs the oracle's 40668). |
| 5385 | (16, 8) => Some(6), |
| 5386 | (8, 16) => Some(7), |
| 5387 | // lane-fi8 r1: the sub-8 rect leaves are allowed too (both sides <= |
| 5388 | // 32), and `decode_leaf_rect8` codes them -- rows 1/2 of |
| 5389 | // `default_filter_intra_cdfs`, NOT BLOCK_4X4's row 0. |
| 5390 | (4, 8) => Some(8), |
| 5391 | (8, 4) => Some(9), |
| 5392 | // lane-fi32x8 r1: same bound, the 32x32-level 1:4 pair |
| 5393 | // (`decode_block_rect4`'s 32x8/8x32 strips). `None` here read no |
| 5394 | // `use_filter_intra` symbol on those strips although aomenc writes |
| 5395 | // one for every DC_PRED block of that shape with |
| 5396 | // `--enable-filter-intra=1` (its default), silently desyncing the |
| 5397 | // tile (class symbol-consumption-gap). |
| 5398 | (32, 8) => Some(10), |
| 5399 | (8, 32) => Some(11), |
| 5400 | // lane-r14: the 16x16-level 1:4 pair (`decode_rect4_16`'s 16x4/4x16 |
| 5401 | // strips), rows 17/16 of `default_filter_intra_cdfs`. |
| 5402 | (16, 4) => Some(12), |
| 5403 | (4, 16) => Some(13), |
| 5404 | _ if bw == bh => filter_intra_size_class(bw), |
| 5405 | _ => None, |
| 5406 | } |
| 5407 | } |
| 5408 | |
| 5409 | /// `av1_allow_palette`'s size bound (blockd.h: `block_size_wide/high <= |
| 5410 | /// MAX_PALETTE_BLOCK_WIDTH/HEIGHT` (64) and `sb_type >= BLOCK_8X8`) plus |
| 5411 | /// `av1_get_palette_bsize_ctx` (pred_common.h: `num_pels_log2_lookup[bsize] |
| 5412 | /// - num_pels_log2_lookup[BLOCK_8X8]`, i.e. `log2(bw*bh) - 6`) -- `None` past |
| 5413 | /// the bound (never gates a palette read), `Some(bsize_ctx)` otherwise. |
| 5414 | fn palette_bsize_ctx(side: usize) -> Option<usize> { |
| 5415 | palette_bsize_ctx_wh(side, side) |
| 5416 | } |
| 5417 | |
| 5418 | /// [`palette_bsize_ctx`], generalised to a true `bw`x`bh` rect strip |
| 5419 | /// (lane-palette2 r4) -- the same `log2(bw*bh) - 6` formula libaom's |
| 5420 | /// `num_pels_log2_lookup[bsize] - num_pels_log2_lookup[BLOCK_8X8]` computes |
| 5421 | /// for ANY bsize, not just the square ones a hand-picked table happened to |
| 5422 | /// enumerate before ([[cdf-row-held-constant]]: a table narrowed to a pinned |
| 5423 | /// row breaks the moment its indexing field moves off the diagonal, and a |
| 5424 | /// 64x32/32x64 superblock-level strip is exactly such a move). Every AV1 |
| 5425 | /// block dimension is a power of two, so `bw*bh` always is too and |
| 5426 | /// `trailing_zeros` is `log2` exactly. `None` past `av1_allow_palette`'s own |
| 5427 | /// bound (`block_size_wide/high <= 64`, `sb_type >= BLOCK_8X8`). |
| 5428 | fn palette_bsize_ctx_wh(bw: usize, bh: usize) -> Option<usize> { |
| 5429 | // lane-r14: `av1_allow_palette`'s bound is `sb_type >= BLOCK_8X8` by the |
| 5430 | // BLOCK_SIZES_ALL enum INDEX, which the 1:4 shapes BLOCK_4X16 (16) and |
| 5431 | // BLOCK_16X4 (17) clear -- a 64-pixel block, so `av1_get_palette_bsize_ctx` |
| 5432 | // (`num_pels_log2 - num_pels_log2[BLOCK_8X8]`) is 0 for them, exactly as |
| 5433 | // for an 8x8. Reading the dimension bound as "both sides >= 8" dropped |
| 5434 | // `palette_y_mode` on every 16x4/4x16 strip of a screen-content frame. |
| 5435 | if bw > 64 || bh > 64 || bw * bh < 64 { |
| 5436 | return None; |
| 5437 | } |
| 5438 | Some(((bw * bh) as u32).trailing_zeros() as usize - 6) |
| 5439 | } |
| 5440 | |
| 5441 | /// A just-decoded palette-Y block's own size, base colours (only the first |
| 5442 | /// `size` entries are real), and per-pixel colour-index map (`side*side`, |
| 5443 | /// row-major, each entry `< size`). |
| 5444 | struct PaletteY { |
| 5445 | size: usize, |
| 5446 | colors: [u16; 8], |
| 5447 | map: Vec<u8>, |
| 5448 | } |
| 5449 | |
| 5450 | /// As [`PaletteY`], for a just-decoded chroma palette block: one shared |
| 5451 | /// per-pixel colour-index map (spec `av1_visit_palette`'s plane-1 pass is |
| 5452 | /// shared between U and V, both co-located after 4:2:0 subsampling) plus |
| 5453 | /// each plane's own base colours. |
| 5454 | struct PaletteUv { |
| 5455 | size: usize, |
| 5456 | u_colors: [u16; 8], |
| 5457 | v_colors: [u16; 8], |
| 5458 | map: Vec<u8>, |
| 5459 | } |
| 5460 | |
| 5461 | /// `aom_ceil_log2` (aom_dsp/bitwriter_buffer.h-adjacent helper, used both by |
| 5462 | /// `read_palette_colors_y`'s shrinking `bits` and `av1_read_uniform`'s |
| 5463 | /// `get_unsigned_bits`, which is the same formula): smallest `n` with |
| 5464 | /// `2^n >= x`, `0` for `x <= 1`. |
| 5465 | fn ceil_log2(x: u32) -> u32 { |
| 5466 | if x < 2 { 0 } else { 32 - (x - 1).leading_zeros() } |
| 5467 | } |
| 5468 | |
| 5469 | /// `av1_read_uniform` (decoder.h:425): a uniform `0..n` value coded as |
| 5470 | /// `l-1` raw bits plus a conditional extra bit, `l = get_unsigned_bits(n)`. |
| 5471 | fn read_uniform(dec: &mut SymbolDecoder, n: usize) -> usize { |
| 5472 | let l = ceil_log2(n as u32); |
| 5473 | let m = (1usize << l) - n; |
| 5474 | let v = dec.literal(l - 1) as usize; |
| 5475 | if v < m { |
| 5476 | v |
| 5477 | } else { |
| 5478 | (v << 1) - m + dec.literal(1) as usize |
| 5479 | } |
| 5480 | } |
| 5481 | |
| 5482 | /// `merge_colors` (decodemv.c:462): interleaves the ascending `cached` |
| 5483 | /// colours (already-known, from the above/left neighbours) with the |
| 5484 | /// ascending `transmitted` ones (delta-coded off the wire) back into one |
| 5485 | /// ascending list of `cached.len() + transmitted.len()` colours -- a tied |
| 5486 | /// value picks the cached copy first, matching the C `<=`. |
| 5487 | fn merge_colors(transmitted: &[u16], cached: &[u16]) -> Vec<u16> { |
| 5488 | let mut out = Vec::with_capacity(transmitted.len() + cached.len()); |
| 5489 | let (mut ci, mut ti) = (0, 0); |
| 5490 | while ci < cached.len() && ti < transmitted.len() { |
| 5491 | if cached[ci] <= transmitted[ti] { |
| 5492 | out.push(cached[ci]); |
| 5493 | ci += 1; |
| 5494 | } else { |
| 5495 | out.push(transmitted[ti]); |
| 5496 | ti += 1; |
| 5497 | } |
| 5498 | } |
| 5499 | out.extend_from_slice(&cached[ci..]); |
| 5500 | out.extend_from_slice(&transmitted[ti..]); |
| 5501 | out |
| 5502 | } |
| 5503 | |
| 5504 | /// `read_palette_colors_y` (decodemv.c:478), at the sequence's real |
| 5505 | /// `bit_depth` -- lane-kf900 r3: every raw literal here (the first colour, |
| 5506 | /// each delta's width, the `range` bound and the clamp) is sized by |
| 5507 | /// `bit_depth`, and hardcoding 8 read 2 bits too few per colour on every |
| 5508 | /// 10-bit palette block. Raw literals are EQUIPROBABLE, and at the ranges |
| 5509 | /// this stream reaches (e.g. 62728) an equiprobable read is a FIXED POINT of |
| 5510 | /// the msac range, so a cross-decoder range ladder stays bit-identical |
| 5511 | /// through the whole miscount -- only the decoded VALUE differs (class |
| 5512 | /// `compare-range-not-tell`'s blind spot). (this |
| 5513 | /// decoder is 8-bit only): accepts up to `n` colours straight from `cache` |
| 5514 | /// (one flag bit each, in cache order), then delta-codes the remainder |
| 5515 | /// (first value raw, each following delta `>= 1` off a shrinking bit width), |
| 5516 | /// and merges the two ascending lists back together. |
| 5517 | fn read_palette_colors_y(dec: &mut SymbolDecoder, n: usize, cache: &[u16]) -> [u16; 8] { |
| 5518 | let mut cached = Vec::with_capacity(n); |
| 5519 | for &c in cache { |
| 5520 | if cached.len() >= n { |
| 5521 | break; |
| 5522 | } |
| 5523 | if dec.literal(1) == 1 { |
| 5524 | cached.push(c); |
| 5525 | } |
| 5526 | } |
| 5527 | let bd = u32::from(bit_depth()); |
| 5528 | let max_val = (1i32 << bd) - 1; |
| 5529 | let mut colors = [0u16; 8]; |
| 5530 | let n_cached = cached.len(); |
| 5531 | if n_cached < n { |
| 5532 | let mut transmitted = Vec::with_capacity(n - n_cached); |
| 5533 | let first = dec.literal(bd) as u16; |
| 5534 | transmitted.push(first); |
| 5535 | if n_cached + 1 < n { |
| 5536 | let min_bits = bd - 3; |
| 5537 | let mut bits = min_bits + dec.literal(2); |
| 5538 | let mut range = (1i32 << bd) - i32::from(first) - 1; |
| 5539 | for _ in (n_cached + 1)..n { |
| 5540 | let delta = dec.literal(bits) as i32 + 1; |
| 5541 | let prev = *transmitted.last().unwrap(); |
| 5542 | let val = (i32::from(prev) + delta).clamp(0, max_val) as u16; |
| 5543 | range -= i32::from(val) - i32::from(prev); |
| 5544 | bits = bits.min(ceil_log2(range.max(0) as u32)); |
| 5545 | transmitted.push(val); |
| 5546 | } |
| 5547 | } |
| 5548 | let merged = merge_colors(&transmitted, &cached); |
| 5549 | colors[..n].copy_from_slice(&merged); |
| 5550 | } else { |
| 5551 | colors[..n].copy_from_slice(&cached[..n]); |
| 5552 | } |
| 5553 | colors |
| 5554 | } |
| 5555 | |
| 5556 | /// `read_palette_colors_uv` (decodemv.c:509), at the sequence's real |
| 5557 | /// `bit_depth` (see [`read_palette_colors_y`]): U reads |
| 5558 | /// exactly [`read_palette_colors_y`]'s cache/delta scheme (against the U-only |
| 5559 | /// cache, `range` starting at `255 - prev` with no `-1`, and each raw `delta` |
| 5560 | /// unbiased by `+1` -- both differences from Y's own reader, matching the C |
| 5561 | /// side by side). V never uses a cache: a leading bit picks either `n` raw |
| 5562 | /// literals, or a first raw value plus `n-1` signed deltas that wrap mod 256 |
| 5563 | /// rather than clamp. |
| 5564 | fn read_palette_colors_uv(dec: &mut SymbolDecoder, n: usize, cache: &[u16]) -> ([u16; 8], [u16; 8]) { |
| 5565 | let mut cached = Vec::with_capacity(n); |
| 5566 | for &c in cache { |
| 5567 | if cached.len() >= n { |
| 5568 | break; |
| 5569 | } |
| 5570 | if dec.literal(1) == 1 { |
| 5571 | cached.push(c); |
| 5572 | } |
| 5573 | } |
| 5574 | let bd = u32::from(bit_depth()); |
| 5575 | let max_val = 1i32 << bd; |
| 5576 | let mut u_colors = [0u16; 8]; |
| 5577 | let n_cached = cached.len(); |
| 5578 | if n_cached < n { |
| 5579 | let mut transmitted = Vec::with_capacity(n - n_cached); |
| 5580 | let first = dec.literal(bd) as u16; |
| 5581 | transmitted.push(first); |
| 5582 | if n_cached + 1 < n { |
| 5583 | let min_bits = bd - 3; |
| 5584 | let mut bits = min_bits + dec.literal(2); |
| 5585 | let mut range = max_val - i32::from(first); |
| 5586 | for _ in (n_cached + 1)..n { |
| 5587 | let delta = dec.literal(bits) as i32; |
| 5588 | let prev = *transmitted.last().unwrap(); |
| 5589 | let val = (i32::from(prev) + delta).clamp(0, max_val - 1) as u16; |
| 5590 | range -= i32::from(val) - i32::from(prev); |
| 5591 | bits = bits.min(ceil_log2(range.max(0) as u32)); |
| 5592 | transmitted.push(val); |
| 5593 | } |
| 5594 | } |
| 5595 | let merged = merge_colors(&transmitted, &cached); |
| 5596 | u_colors[..n].copy_from_slice(&merged); |
| 5597 | } else { |
| 5598 | u_colors[..n].copy_from_slice(&cached[..n]); |
| 5599 | } |
| 5600 | let mut v_colors = [0u16; 8]; |
| 5601 | if dec.literal(1) == 1 { |
| 5602 | let min_bits_v = bd - 4; |
| 5603 | let bits = min_bits_v + dec.literal(2); |
| 5604 | v_colors[0] = dec.literal(bd) as u16; |
| 5605 | for i in 1..n { |
| 5606 | let mut delta = dec.literal(bits) as i32; |
| 5607 | if delta != 0 && dec.literal(1) == 1 { |
| 5608 | delta = -delta; |
| 5609 | } |
| 5610 | let mut val = i32::from(v_colors[i - 1]) + delta; |
| 5611 | if val < 0 { |
| 5612 | val += max_val; |
| 5613 | } |
| 5614 | if val >= max_val { |
| 5615 | val -= max_val; |
| 5616 | } |
| 5617 | v_colors[i] = val as u16; |
| 5618 | } |
| 5619 | } else { |
| 5620 | for i in 0..n { |
| 5621 | v_colors[i] = dec.literal(bd) as u16; |
| 5622 | } |
| 5623 | } |
| 5624 | (u_colors, v_colors) |
| 5625 | } |
| 5626 | |
| 5627 | /// `av1_palette_color_index_context_lookup` (entropymode.c:889): maps a |
| 5628 | /// `NUM_PALETTE_NEIGHBORS`-weighted score hash (`0..=8`) to the real |
| 5629 | /// `0..=4` context, `-1` for hashes no real neighbour configuration produces. |
| 5630 | const PALETTE_COLOR_INDEX_CONTEXT_LOOKUP: [i32; 9] = [-1, -1, 0, -1, -1, 4, 3, 2, 1]; |
| 5631 | |
| 5632 | /// `av1_get_palette_color_index_context` (entropymode.c:893): the colour-index |
| 5633 | /// map's own per-pixel context and colour-order permutation, from the |
| 5634 | /// left/up-left/up already-decoded neighbours (weights `[2, 1, 2]`, top-3 |
| 5635 | /// selection sort). `map`'s stride is `side` (this decoder never crops a |
| 5636 | /// palette block's map to a narrower plane edge, [`decode_color_index_map`]'s |
| 5637 | /// own doc). Returns `(ctx, color_order)`; `color_order[symbol]` is the |
| 5638 | /// actual colour index to store. |
| 5639 | fn palette_color_index_context( |
| 5640 | map: &[u8], |
| 5641 | side: usize, |
| 5642 | row: usize, |
| 5643 | col: usize, |
| 5644 | n: usize, |
| 5645 | ) -> (usize, [u8; 8]) { |
| 5646 | let left = if col >= 1 { Some(map[row * side + col - 1]) } else { None }; |
| 5647 | let up_left = if col >= 1 && row >= 1 { |
| 5648 | Some(map[(row - 1) * side + col - 1]) |
| 5649 | } else { |
| 5650 | None |
| 5651 | }; |
| 5652 | let up = if row >= 1 { Some(map[(row - 1) * side + col]) } else { None }; |
| 5653 | let mut scores = [0i32; 8]; |
| 5654 | for (neighbour, weight) in [(left, 2), (up_left, 1), (up, 2)] { |
| 5655 | if let Some(v) = neighbour { |
| 5656 | scores[v as usize] += weight; |
| 5657 | } |
| 5658 | } |
| 5659 | let mut color_order = [0u8; 8]; |
| 5660 | for i in 0..8 { |
| 5661 | color_order[i] = i as u8; |
| 5662 | } |
| 5663 | for i in 0..3 { |
| 5664 | let mut max = scores[i]; |
| 5665 | let mut max_idx = i; |
| 5666 | for j in (i + 1)..n { |
| 5667 | if scores[j] > max { |
| 5668 | max = scores[j]; |
| 5669 | max_idx = j; |
| 5670 | } |
| 5671 | } |
| 5672 | if max_idx != i { |
| 5673 | let max_score = scores[max_idx]; |
| 5674 | let max_color = color_order[max_idx]; |
| 5675 | for k in (i + 1..=max_idx).rev() { |
| 5676 | scores[k] = scores[k - 1]; |
| 5677 | color_order[k] = color_order[k - 1]; |
| 5678 | } |
| 5679 | scores[i] = max_score; |
| 5680 | color_order[i] = max_color; |
| 5681 | } |
| 5682 | } |
| 5683 | let hash = scores[0] + scores[1] * 2 + scores[2] * 2; |
| 5684 | let ctx = PALETTE_COLOR_INDEX_CONTEXT_LOOKUP[hash as usize]; |
| 5685 | (ctx as usize, color_order) |
| 5686 | } |
| 5687 | |
| 5688 | /// `decode_color_map_tokens` (detokenize.c:25): the palette-Y colour-index |
| 5689 | /// map's wavefront decode over a `side x side` grid -- this decoder's |
| 5690 | /// palette blocks are always square and never straddle the frame's true |
| 5691 | /// (possibly odd) edge without already allocating the full `side*side` |
| 5692 | /// region (every other skip/residual path here does the same), so no |
| 5693 | /// rows/cols-vs-plane_width/height cropping special case is needed, unlike |
| 5694 | /// libaom's own non-block-aligned split. |
| 5695 | fn decode_color_index_map( |
| 5696 | dec: &mut SymbolDecoder, |
| 5697 | cdfs: &mut Cdfs, |
| 5698 | n: usize, |
| 5699 | side: usize, |
| 5700 | uv: bool, |
| 5701 | ) -> Vec<u8> { |
| 5702 | decode_color_index_map_wh(dec, cdfs, n, side, side, uv) |
| 5703 | } |
| 5704 | |
| 5705 | /// [`decode_color_index_map`], generalised to a true `bw`x`bh` rect strip |
| 5706 | /// (lane-palette2 r4): `decode_color_map_tokens`'s own wavefront diagonal |
| 5707 | /// (`row = i - j`, `col = j`) walked over `cols = bw`, `rows = bh` |
| 5708 | /// independently rather than one shared `side` -- the square call above is |
| 5709 | /// just `bw == bh`. |
| 5710 | fn decode_color_index_map_wh( |
| 5711 | dec: &mut SymbolDecoder, |
| 5712 | cdfs: &mut Cdfs, |
| 5713 | n: usize, |
| 5714 | bw: usize, |
| 5715 | bh: usize, |
| 5716 | uv: bool, |
| 5717 | ) -> Vec<u8> { |
| 5718 | let trace = std::env::var_os("EC_AV1_TRACE").is_some(); |
| 5719 | let mut map = vec![0u8; bw * bh]; |
| 5720 | if trace { |
| 5721 | let (rng, _) = dec.debug_state(); |
| 5722 | eprintln!("EC_PAL row=0 col=0 ctx=-1 n={n} rng={rng}"); |
| 5723 | } |
| 5724 | map[0] = read_uniform(dec, n) as u8; |
| 5725 | if trace { |
| 5726 | let (rng, _) = dec.debug_state(); |
| 5727 | eprintln!("EC_PAL_VAL row=0 col=0 color_idx={} rng={rng}", map[0]); |
| 5728 | } |
| 5729 | for i in 1..(bw + bh - 1) { |
| 5730 | let j_hi = i.min(bw - 1); |
| 5731 | let j_lo = i.saturating_sub(bh - 1); |
| 5732 | for j in (j_lo..=j_hi).rev() { |
| 5733 | let row = i - j; |
| 5734 | let col = j; |
| 5735 | let (ctx, color_order) = palette_color_index_context(&map, bw, row, col, n); |
| 5736 | if trace { |
| 5737 | let (rng, _) = dec.debug_state(); |
| 5738 | eprintln!("EC_PAL row={row} col={col} ctx={ctx} n={n} rng={rng}"); |
| 5739 | } |
| 5740 | let symbol = if uv { |
| 5741 | dec.symbol(&mut cdfs.palette_uv_color_index[n - 2][ctx][..=n]) |
| 5742 | } else { |
| 5743 | dec.symbol(&mut cdfs.palette_y_color_index[n - 2][ctx][..=n]) |
| 5744 | }; |
| 5745 | map[row * bw + col] = color_order[symbol]; |
| 5746 | if trace { |
| 5747 | let (rng, _) = dec.debug_state(); |
| 5748 | eprintln!("EC_PAL_VAL row={row} col={col} color_idx={symbol} rng={rng}"); |
| 5749 | } |
| 5750 | } |
| 5751 | } |
| 5752 | map |
| 5753 | } |
| 5754 | |
| 5755 | /// [`read_intra_mode`] for a true `bw`x`bh` rect strip (lane-intradisp r1, |
| 5756 | /// palette wired lane-palette2 r4): identical except the `use_filter_intra` |
| 5757 | /// size class comes from [`filter_intra_size_class_rect`] instead of the |
| 5758 | /// square-only [`filter_intra_size_class`], and the palette read (when |
| 5759 | /// `allow_screen_content_tools`) uses [`palette_bsize_ctx_wh`]/ |
| 5760 | /// [`decode_color_index_map_wh`] instead of their square-only counterparts -- |
| 5761 | /// every other symbol this reads (`skip`, `y_mode`, `angle_delta`, `uv_mode`, |
| 5762 | /// `cfl_alphas`) is indexed by mode or neighbour state, never by block size. |
| 5763 | #[allow(clippy::too_many_arguments)] |
| 5764 | fn read_intra_mode_rect( |
| 5765 | dec: &mut SymbolDecoder, |
| 5766 | cdfs: &mut Cdfs, |
| 5767 | above_mode: usize, |
| 5768 | left_mode: usize, |
| 5769 | cfl: bool, |
| 5770 | // lane-r14: `is_chroma_reference` -- false only for a strip one mi tall |
| 5771 | // (or wide) that is NOT the odd one of its 4:2:0 pair, where libaom reads |
| 5772 | // no `uv_mode`/`cfl_alpha`/`angle_delta_uv`/`palette_uv` at all (the pair's |
| 5773 | // chroma is coded once, on the closing strip). Every other caller is a |
| 5774 | // block big enough to own its chroma and passes `true`. |
| 5775 | has_chroma: bool, |
| 5776 | bw: usize, |
| 5777 | bh: usize, |
| 5778 | enable_filter_intra: bool, |
| 5779 | skip_ctx: usize, |
| 5780 | allow_screen_content_tools: bool, |
| 5781 | // As [`read_intra_mode`]'s own `palette`/`palette_uv_cache` params: |
| 5782 | // `Some((palette_mode_ctx, color_cache))` reconstructs a real palette-Y |
| 5783 | // block here, `None` still reads (and refuses on use) the symbol at ctx 0. |
| 5784 | palette: Option<(usize, &[u16])>, |
| 5785 | palette_uv_cache: &[u16], |
| 5786 | mi_r: usize, |
| 5787 | mi_c: usize, |
| 5788 | ) -> Result<( |
| 5789 | bool, |
| 5790 | usize, |
| 5791 | i32, |
| 5792 | usize, |
| 5793 | i32, |
| 5794 | Option<(i32, i32)>, |
| 5795 | Option<usize>, |
| 5796 | Option<PaletteY>, |
| 5797 | Option<PaletteUv>, |
| 5798 | )> { |
| 5799 | let ec_istep = std::env::var_os("EC_TRACE_MODE_STEP").is_some(); |
| 5800 | if ec_istep { |
| 5801 | // Mirrors the oracle's own `EC_IMODE mi_row=.. mi_col=.. rng=..` line |
| 5802 | // (decodemv.c:818), printed at the same point: before the first |
| 5803 | // mode-info symbol of the block, so a cross-decoder range ladder can |
| 5804 | // be keyed on the BLOCK, not just on the symbol sequence. |
| 5805 | eprintln!("EC_IMODE mi_row={mi_r} mi_col={mi_c} fn=rect bw={bw} bh={bh} efi={} cls={:?} rng={}", enable_filter_intra as i32, filter_intra_size_class_rect(bw, bh), dec.debug_state().0); |
| 5806 | } |
| 5807 | macro_rules! istep { |
| 5808 | ($name:literal, $val:expr) => { |
| 5809 | if ec_istep { |
| 5810 | let (rng, _) = dec.debug_state(); |
| 5811 | eprintln!( |
| 5812 | "EC_ISTEP mi_row={mi_r} mi_col={mi_c} name={} val={} rng={rng}", |
| 5813 | $name, $val |
| 5814 | ); |
| 5815 | } |
| 5816 | }; |
| 5817 | } |
| 5818 | // lane-sbrect10 r1: libaom `is_cfl_allowed` (blockd.h) is |
| 5819 | // `block_size_wide <= 32 && block_size_high <= 32` -- enforce it HERE, at |
| 5820 | // the one point every rect caller routes through, so no call site can |
| 5821 | // offer the 14-symbol CFL alphabet on a 64-axis block (class |
| 5822 | // wrong-alphabet-same-value: same value, wrong interval, silent desync). |
| 5823 | let cfl = if cfl && bw.max(bh) > 32 { |
| 5824 | NOCFL_UV_MODE_HITS.with(|c| c.set(c.get() + 1)); |
| 5825 | false |
| 5826 | } else { |
| 5827 | cfl |
| 5828 | }; |
| 5829 | // Same `intra_segment_id` placement as the square reader above. |
| 5830 | let (seg_w_mi, seg_h_mi) = (bw / 4, bh / 4); |
| 5831 | if seg_id_pre_skip() { |
| 5832 | intra_segment_id(dec, cdfs, mi_r, mi_c, seg_w_mi, seg_h_mi, false); |
| 5833 | } |
| 5834 | let skip = dec.symbol(&mut cdfs.skip[skip_ctx]) != 0; |
| 5835 | istep!("skip", skip as i32); |
| 5836 | if !seg_id_pre_skip() { |
| 5837 | intra_segment_id(dec, cdfs, mi_r, mi_c, seg_w_mi, seg_h_mi, skip); |
| 5838 | } |
| 5839 | maybe_read_cdef_idx(dec, mi_r, mi_c, skip); |
| 5840 | istep!("cdef", 0); |
| 5841 | // A HORZ/VERT rect strip is never the whole superblock (`bw`/`bh` never |
| 5842 | // both 64 here -- see this fn's own doc), so `is_whole_sb` is always |
| 5843 | // `false`. |
| 5844 | maybe_read_delta_q(dec, cdfs, mi_r, mi_c, false, skip); |
| 5845 | maybe_read_delta_lf(dec, cdfs, mi_r, mi_c, false, skip); |
| 5846 | istep!("dq", 0); |
| 5847 | // `read_intrabc_info` (spec 5.11.13, libaom `decodemv.c:892`, right after |
| 5848 | // `skip`/`segment_id`/`cdef`/`delta_q` and before `mbmi->mode`): the |
| 5849 | // `use_intrabc` symbol is read for EVERY intra block of an `allow_intrabc` |
| 5850 | // frame -- `av1_allow_intrabc` is a frame-level test with no shape term, so |
| 5851 | // rect strips read it exactly like square leaves ([`read_intra_mode`], |
| 5852 | // [`read_intra_mode_sub8`]). Missing it here desynced the arithmetic |
| 5853 | // decoder at the FIRST symbol of the first rect block of every |
| 5854 | // `--tune-content=screen` key frame. Reconstruction of a real intrabc |
| 5855 | // block at this shape is out of scope, so the flag is consumed and |
| 5856 | // refused by name when it fires. |
| 5857 | if allow_intrabc_frame() { |
| 5858 | let use_intrabc = dec.symbol(&mut cdfs.intrabc) != 0; |
| 5859 | RECT_INTRABC_READS.with(|c| c.set(c.get() + 1)); |
| 5860 | istep!("intrabc", use_intrabc as i32); |
| 5861 | if use_intrabc { |
| 5862 | INTRABC_HITS.with(|c| c.set(c.get() + 1)); |
| 5863 | return Err(unsupported( |
| 5864 | "intra block copy on a HORZ/VERT/1:4 rect intra strip (reconstruction is not ported at this shape)", |
| 5865 | )); |
| 5866 | } |
| 5867 | } |
| 5868 | let above_ctx = INTRA_MODE_CTX[above_mode]; |
| 5869 | let left_ctx = INTRA_MODE_CTX[left_mode]; |
| 5870 | let mode = dec.symbol(&mut cdfs.kf_y_mode[above_ctx][left_ctx]); |
| 5871 | istep!("mode", mode as i32); |
| 5872 | let angle_delta_y = if (V_PRED..=D67_PRED).contains(&mode) { |
| 5873 | read_angle_delta(dec, &mut cdfs.angle_delta[mode - V_PRED]) |
| 5874 | } else { |
| 5875 | 0 |
| 5876 | }; |
| 5877 | istep!("angle_y", angle_delta_y); |
| 5878 | let uv_mode = if !has_chroma { |
| 5879 | DC_PRED |
| 5880 | } else if cfl { |
| 5881 | dec.symbol(&mut cdfs.uv_mode_cfl[mode]) |
| 5882 | } else { |
| 5883 | dec.symbol(&mut cdfs.uv_mode_no_cfl[mode]) |
| 5884 | }; |
| 5885 | istep!("uv_mode", uv_mode as i32); |
| 5886 | let alpha = if has_chroma && cfl && uv_mode == UV_CFL_PRED { |
| 5887 | Some(read_cfl_alphas(dec, cdfs)) |
| 5888 | } else { |
| 5889 | None |
| 5890 | }; |
| 5891 | if has_chroma && (9..=12).contains(&uv_mode) { |
| 5892 | SMOOTH_UV_HITS.with(|c| c.set(c.get() + 1)); |
| 5893 | } |
| 5894 | let angle_delta_uv = if has_chroma && (V_PRED..=D67_PRED).contains(&uv_mode) { |
| 5895 | DIRECTIONAL_UV_HITS.with(|c| c.set(c.get() + 1)); |
| 5896 | read_angle_delta(dec, &mut cdfs.angle_delta[uv_mode - V_PRED]) |
| 5897 | } else { |
| 5898 | 0 |
| 5899 | }; |
| 5900 | istep!("angle_uv", angle_delta_uv); |
| 5901 | if angle_delta_uv != 0 { |
| 5902 | UV_ANGLE_DELTA_HITS.with(|c| c.set(c.get() + 1)); |
| 5903 | } |
| 5904 | // `read_palette_mode_info` (spec 5.11.13), rect-generalised |
| 5905 | // (lane-palette2 r4): same bsize-ctx-gated read as [`read_intra_mode`], |
| 5906 | // just keyed by [`palette_bsize_ctx_wh`] instead of the square-only |
| 5907 | // [`palette_bsize_ctx`]. Order matches spec/libaom exactly: mode/size/ |
| 5908 | // colours only here, the colour-index maps deferred past `filter_intra` |
| 5909 | // (see [`read_intra_mode`]'s own comment on why -- `av1_visit_palette` |
| 5910 | // runs from the caller, after the whole mode-info read). |
| 5911 | let mut palette_y_pending: Option<(usize, [u16; 8])> = None; |
| 5912 | let mut palette_uv_pending: Option<(usize, [u16; 8], [u16; 8])> = None; |
| 5913 | if allow_screen_content_tools |
| 5914 | && let Some(bsize_ctx) = palette_bsize_ctx_wh(bw, bh) |
| 5915 | { |
| 5916 | // r6: this is the exact site the removed blanket refusal used to |
| 5917 | // guard -- reached for EVERY rect strip in a screen-content frame, |
| 5918 | // independent of whether the block ends up using a palette. |
| 5919 | RECT_SCREEN_CONTENT_HITS.with(|c| c.set(c.get() + 1)); |
| 5920 | let (mode_ctx, cache) = palette.unwrap_or((0, &[])); |
| 5921 | let use_palette_y = |
| 5922 | mode == DC_PRED && dec.symbol(&mut cdfs.palette_y_mode[bsize_ctx][mode_ctx]) != 0; |
| 5923 | if use_palette_y { |
| 5924 | if palette.is_none() { |
| 5925 | return Err(unsupported( |
| 5926 | "a block that actually uses a palette (Y) -- reconstruction is out of scope", |
| 5927 | )); |
| 5928 | } |
| 5929 | let n = 2 + dec.symbol(&mut cdfs.palette_y_size[bsize_ctx]); |
| 5930 | let colors = read_palette_colors_y(dec, n, cache); |
| 5931 | palette_y_pending = Some((n, colors)); |
| 5932 | } |
| 5933 | let palette_uv_mode_ctx = usize::from(use_palette_y); |
| 5934 | // `read_palette_mode_info`'s own `xd->is_chroma_ref` guard. |
| 5935 | if has_chroma |
| 5936 | && uv_mode == DC_PRED |
| 5937 | && dec.symbol(&mut cdfs.palette_uv_mode[palette_uv_mode_ctx]) != 0 |
| 5938 | { |
| 5939 | let n = 2 + dec.symbol(&mut cdfs.palette_uv_size[bsize_ctx]); |
| 5940 | let (u_colors, v_colors) = read_palette_colors_uv(dec, n, palette_uv_cache); |
| 5941 | palette_uv_pending = Some((n, u_colors, v_colors)); |
| 5942 | } |
| 5943 | } |
| 5944 | let mut filter_intra = None; |
| 5945 | if std::env::var_os("EC_AV1_TRACE").is_some() { |
| 5946 | let (rng, _) = dec.debug_state(); |
| 5947 | eprintln!( |
| 5948 | "TRACE_RECT_PREFI mode={mode} uv_mode={uv_mode} enable_filter_intra={} \ |
| 5949 | class={:?} rng={rng}", |
| 5950 | enable_filter_intra as i32, |
| 5951 | filter_intra_size_class_rect(bw, bh) |
| 5952 | ); |
| 5953 | } |
| 5954 | // As the square path: `av1_filter_intra_allowed` excludes a palette-Y |
| 5955 | // block (reconintra.h:77), so no `use_filter_intra` symbol exists there. |
| 5956 | if mode == DC_PRED |
| 5957 | && enable_filter_intra |
| 5958 | && palette_y_pending.is_none() |
| 5959 | && let Some(class) = filter_intra_size_class_rect(bw, bh) |
| 5960 | { |
| 5961 | let use_filter_intra = dec.symbol(&mut cdfs.filter_intra[class]) != 0; |
| 5962 | if std::env::var_os("EC_AV1_TRACE").is_some() { |
| 5963 | eprintln!("TRACE_RECT_USEFI value={}", use_filter_intra as i32); |
| 5964 | } |
| 5965 | istep!("use_filter_intra", use_filter_intra as i32); |
| 5966 | if use_filter_intra { |
| 5967 | FILTER_INTRA_HITS.with(|c| c.set(c.get() + 1)); |
| 5968 | let fi_mode = dec.symbol(&mut cdfs.filter_intra_mode); |
| 5969 | istep!("filter_intra_mode", fi_mode as i32); |
| 5970 | filter_intra = Some(fi_mode); |
| 5971 | } |
| 5972 | } |
| 5973 | // `av1_visit_palette`: plane 0 (Y) then plane 1 (chroma, shared U/V map), |
| 5974 | // same order as [`read_intra_mode`]'s own square path. |
| 5975 | let palette_y = palette_y_pending.map(|(n, colors)| { |
| 5976 | let map = decode_color_index_map_wh(dec, cdfs, n, bw, bh, false); |
| 5977 | PALETTE_HITS.with(|c| c.set(c.get() + 1)); |
| 5978 | PALETTE_RECT_HITS.with(|c| c.set(c.get() + 1)); |
| 5979 | PaletteY { size: n, colors, map } |
| 5980 | }); |
| 5981 | let palette_uv = palette_uv_pending.map(|(n, u_colors, v_colors)| { |
| 5982 | // `av1_get_plane_block_size(bsize, 1, 1)` (`ss_size_lookup`), NOT a |
| 5983 | // plain halving: BLOCK_16X4 subsamples to BLOCK_8X4 and BLOCK_4X16 to |
| 5984 | // BLOCK_4X8, both floored at the 4-px transform. Halving the short |
| 5985 | // axis to 2 read HALF the colour-index symbols of a 1:4 strip's |
| 5986 | // chroma palette and desynced the tile from there. |
| 5987 | let (cw, ch) = ((bw / 2).max(4), (bh / 2).max(4)); |
| 5988 | let map = decode_color_index_map_wh(dec, cdfs, n, cw, ch, true); |
| 5989 | PALETTE_UV_HITS.with(|c| c.set(c.get() + 1)); |
| 5990 | PaletteUv { size: n, u_colors, v_colors, map } |
| 5991 | }); |
| 5992 | Ok(( |
| 5993 | skip, |
| 5994 | mode, |
| 5995 | angle_delta_y, |
| 5996 | uv_mode, |
| 5997 | angle_delta_uv, |
| 5998 | alpha, |
| 5999 | filter_intra, |
| 6000 | palette_y, |
| 6001 | palette_uv, |
| 6002 | )) |
| 6003 | } |
| 6004 | |
| 6005 | /// Stamps a 1:4 strip's palette state into the coarse above/left arrays. |
| 6006 | /// `av1_get_palette_cache` reads `xd->above_mbmi`/`xd->left_mbmi`, and inside |
| 6007 | /// a HORZ_4 (VERT_4) the next strip's ABOVE (LEFT) neighbour is this strip |
| 6008 | /// while its LEFT (ABOVE) neighbour is still the parent block's. The coarse |
| 6009 | /// [`SUB`]-cell arrays alias all four strips onto one entry, so writing BOTH |
| 6010 | /// axes after every strip would hand strip i+1 this strip's colours on the |
| 6011 | /// wrong axis too -- a wrong cache costs the same bits, so the tile stays in |
| 6012 | /// sync and only the pixels are wrong (class [[cdf-row-held-constant]]). |
| 6013 | /// Only the split axis is written until the closing strip, which stamps both |
| 6014 | /// for whatever follows the parent block. |
| 6015 | #[allow(clippy::too_many_arguments)] |
| 6016 | fn record_strip_palette( |
| 6017 | n: &mut Neighbours, |
| 6018 | at: (usize, usize), |
| 6019 | bw: usize, |
| 6020 | bh: usize, |
| 6021 | y_size: usize, |
| 6022 | y_colors: [u16; 8], |
| 6023 | uv_size: usize, |
| 6024 | uv_colors: [u16; 8], |
| 6025 | last: bool, |
| 6026 | ) { |
| 6027 | let (r, c) = at; |
| 6028 | let horz = bw > bh; |
| 6029 | if horz || last { |
| 6030 | for cell in 0..(bw / SUB).max(1) { |
| 6031 | n.above_palette_size[c + cell] = y_size; |
| 6032 | n.above_palette_colors[c + cell] = y_colors; |
| 6033 | n.above_palette_uv_size[c + cell] = uv_size; |
| 6034 | n.above_palette_uv_colors[c + cell] = uv_colors; |
| 6035 | } |
| 6036 | } |
| 6037 | if !horz || last { |
| 6038 | for cell in 0..(bh / SUB).max(1) { |
| 6039 | n.left_palette_size[r + cell] = y_size; |
| 6040 | n.left_palette_colors[r + cell] = y_colors; |
| 6041 | n.left_palette_uv_size[r + cell] = uv_size; |
| 6042 | n.left_palette_uv_colors[r + cell] = uv_colors; |
| 6043 | } |
| 6044 | } |
| 6045 | } |
| 6046 | |
| 6047 | /// [`tx_size_context`] for a true `bw`x`bh` rect strip (lane-intradisp r1): |
| 6048 | /// `get_tx_size_context` (libaom `pred_common.h`) compares the neighbour |
| 6049 | /// above against this block's own *width* and the neighbour to the left |
| 6050 | /// against its own *height* -- for a square block those are the same value, |
| 6051 | /// which is why the square-only version can get away with one `own_side`. |
| 6052 | fn tx_size_context_rect( |
| 6053 | n: &Neighbours, |
| 6054 | (mi_r, mi_c): (usize, usize), |
| 6055 | own_w: usize, |
| 6056 | own_h: usize, |
| 6057 | ) -> usize { |
| 6058 | let above = mi_r > n.tile_row0_mi && tx_px_at(n, false, mi_r - 1, mi_c) as usize >= own_w; |
| 6059 | let left = mi_c > n.tile_col0_mi && tx_h_px_at(n, false, mi_r, mi_c - 1) as usize >= own_h; |
| 6060 | if std::env::var_os("EC_TRACE_MODE_STEP").is_some() { |
| 6061 | eprintln!( |
| 6062 | "EC_TXCTX mi_row={mi_r} mi_col={mi_c} w={own_w} h={own_h} above_px={} left_px={} ctx={}", |
| 6063 | if mi_r > n.tile_row0_mi { tx_px_at(n, false, mi_r - 1, mi_c) } else { 0 }, |
| 6064 | if mi_c > n.tile_col0_mi { tx_h_px_at(n, false, mi_r, mi_c - 1) } else { 0 }, |
| 6065 | usize::from(above) + usize::from(left) |
| 6066 | ); |
| 6067 | } |
| 6068 | usize::from(above) + usize::from(left) |
| 6069 | } |
| 6070 | |
| 6071 | /// [`cfl_ac_q3`] for a true `bw`x`bh` rect strip (lane-intradisp r1). |
| 6072 | fn cfl_ac_q3_rect(y: &PlaneBuf, px: usize, py: usize, bw: usize, bh: usize) -> Vec<i32> { |
| 6073 | let (cw, ch) = (bw / 2, bh / 2); |
| 6074 | let mut ac = vec![0i32; cw * ch]; |
| 6075 | let mut sum = 0i32; |
| 6076 | for cy in 0..ch { |
| 6077 | for cx in 0..cw { |
| 6078 | let (lx, ly) = (px + cx * 2, py + cy * 2); |
| 6079 | let q3 = (i32::from(y.data[ly * y.width + lx]) |
| 6080 | + i32::from(y.data[ly * y.width + lx + 1]) |
| 6081 | + i32::from(y.data[(ly + 1) * y.width + lx]) |
| 6082 | + i32::from(y.data[(ly + 1) * y.width + lx + 1])) |
| 6083 | << 1; |
| 6084 | ac[cy * cw + cx] = q3; |
| 6085 | sum += q3; |
| 6086 | } |
| 6087 | } |
| 6088 | let num_pel = (cw * ch) as i32; |
| 6089 | let avg = (sum + num_pel / 2) >> num_pel.trailing_zeros(); |
| 6090 | ac.iter_mut().for_each(|v| *v -= avg); |
| 6091 | ac |
| 6092 | } |
| 6093 | |
| 6094 | /// Everything a rect strip already read out of the stream before its luma |
| 6095 | /// transform units are decoded -- [`decode_rect_split`]'s own mode bundle |
| 6096 | /// (nine values that would otherwise be nine more parameters). |
| 6097 | struct RectStripModes { |
| 6098 | skip: bool, |
| 6099 | mode: usize, |
| 6100 | angle_delta_y: i32, |
| 6101 | uv_predict_mode: usize, |
| 6102 | angle_delta_uv: i32, |
| 6103 | alpha: Option<(i32, i32)>, |
| 6104 | filter_intra: Option<usize>, |
| 6105 | smooth_neighbor: bool, |
| 6106 | smooth_neighbor_uv: bool, |
| 6107 | /// lane-palette2 r9: the strip's palette-Y prediction (`bw`x`bh`, already |
| 6108 | /// mapped through the colour table), sliced per transform unit below -- |
| 6109 | /// palette prediction reads no edge pixels, so a unit's slice of the |
| 6110 | /// whole-block map IS its prediction. Without this a palette strip whose |
| 6111 | /// transform splits predicted DC and came out flat grey while the entropy |
| 6112 | /// stream stayed in sync (the coefficients are read either way). |
| 6113 | palette_y: Option<Vec<u16>>, |
| 6114 | /// As `palette_y`, for the un-split chroma transform (U, V). |
| 6115 | palette_uv: Option<(Vec<u16>, Vec<u16>)>, |
| 6116 | } |
| 6117 | |
| 6118 | /// The transform unit size (w, h) of a `bw`x`bh` intra block at tx `depth` |
| 6119 | /// -- libaom `depth_to_tx_size`, which walks `sub_tx_size_map` `depth` times |
| 6120 | /// from `max_txsize_rect_lookup[bsize]`. Every entry of that map halves the |
| 6121 | /// LONGER side of a rectangle and both sides of a square, so a 1:4 strip's |
| 6122 | /// first step is a 2:1 rect unit (`TX_32X8 -> TX_16X8`, `TX_64X16 -> |
| 6123 | /// TX_32X16`) and only the second reaches a square -- the reason |
| 6124 | /// `bw.min(bh) >> (depth - 1)` (correct for a 2:1 strip, where step one is |
| 6125 | /// already square) mis-tiled 1:4 strips. |
| 6126 | fn depth_to_tx_wh(bw: usize, bh: usize, depth: usize) -> (usize, usize) { |
| 6127 | let (mut w, mut h) = (bw.min(64), bh.min(64)); |
| 6128 | for _ in 0..depth { |
| 6129 | match w.cmp(&h) { |
| 6130 | std::cmp::Ordering::Greater => w /= 2, |
| 6131 | std::cmp::Ordering::Less => h /= 2, |
| 6132 | std::cmp::Ordering::Equal => { |
| 6133 | w /= 2; |
| 6134 | h /= 2; |
| 6135 | } |
| 6136 | } |
| 6137 | } |
| 6138 | (w, h) |
| 6139 | } |
| 6140 | |
| 6141 | /// Decodes the planes of one `bw`x`bh` HORZ/VERT intra strip whose luma |
| 6142 | /// transform is SPLIT (`tx_depth != 0`) -- lane-rectsplit r1, the case every |
| 6143 | /// rect path here refused by name until now ("per-unit rect prediction is not |
| 6144 | /// ported"). |
| 6145 | /// |
| 6146 | /// `depth_to_tx_size` (libaom `blockd.h`) walks `sub_tx_size_map` `depth` |
| 6147 | /// times from the block's own `max_txsize_rect_lookup` entry, and for a 2:1 |
| 6148 | /// strip the very first step already lands on a SQUARE transform |
| 6149 | /// (`sub_tx_size_map[TX_32X16] == TX_16X16`, `[TX_64X32] == TX_32X32`, and |
| 6150 | /// every further step halves a square) -- so each unit is square and reads |
| 6151 | /// through the ordinary square [`read_plane`]. What is genuinely rectangular |
| 6152 | /// here is the *tiling* (`bw/tx` by `bh/tx` units, not `n` by `n` as |
| 6153 | /// [`decode_block`]'s square split loop assumes) and the chroma plane, whose |
| 6154 | /// own transform is `av1_get_max_uv_txsize` of the whole strip and is never |
| 6155 | /// split by luma's depth. |
| 6156 | /// |
| 6157 | /// Each unit predicts off whatever the earlier units of this same block |
| 6158 | /// already reconstructed (spec 5.11.36 `predict_and_reconstruct_intra_block` |
| 6159 | /// inside `av1_foreach_transformed_block`, raster order), which is exactly |
| 6160 | /// what predicting the whole strip in one shot got wrong. |
| 6161 | #[allow(clippy::too_many_arguments)] |
| 6162 | fn decode_rect_split( |
| 6163 | dec: &mut SymbolDecoder, |
| 6164 | cdfs: &mut Cdfs, |
| 6165 | neighbours: &mut Neighbours, |
| 6166 | at_mi: (usize, usize), |
| 6167 | bw: usize, |
| 6168 | bh: usize, |
| 6169 | // The transform unit's own width and height in pixels. Square for every |
| 6170 | // 2:1 strip from the first split on, but a 1:4 strip's FIRST split is |
| 6171 | // still a 2:1 RECT unit (`sub_tx_size_map[TX_32X8] == TX_16X8`, |
| 6172 | // `[TX_64X16] == TX_32X16`) -- see [`depth_to_tx_wh`]. |
| 6173 | tx_w: usize, |
| 6174 | tx_h: usize, |
| 6175 | m: &RectStripModes, |
| 6176 | y: &mut PlaneBuf, |
| 6177 | u: &mut PlaneBuf, |
| 6178 | v: &mut PlaneBuf, |
| 6179 | base_q_idx: u8, |
| 6180 | reduced_tx_set: bool, |
| 6181 | ) -> Result<()> { |
| 6182 | let (mi_r, mi_c) = at_mi; |
| 6183 | let (px, py) = (mi_c * MI, mi_r * MI); |
| 6184 | let (cpx, cpy) = (px / 2, py / 2); |
| 6185 | let (chroma_w, chroma_h) = (bw / 2, bh / 2); |
| 6186 | // The chroma transform is picked before any symbol is read, so an |
| 6187 | // unsupported chroma shape refuses before this strip desyncs the tile. |
| 6188 | let chroma: Option<(TxbSet, &[u16])> = match (chroma_w, chroma_h) { |
| 6189 | (16, 8) => Some((TxbSet::ChromaRect16x8, &SCAN_16X8[..])), |
| 6190 | (8, 16) => Some((TxbSet::ChromaRect16x8, &SCAN_8X16[..])), |
| 6191 | (32, 16) => Some((TxbSet::ChromaRect32x16, &SCAN_32X16[..])), |
| 6192 | (16, 32) => Some((TxbSet::ChromaRect32x16, &SCAN_16X32[..])), |
| 6193 | // The 1:4 and sub-16 strips (lane-rectchroma r1 added the 32x8/8x32 |
| 6194 | // rows, lane-rectsplitx r1 the 16x4/4x16 ones). `get_txsize_entropy_ctx` |
| 6195 | // squares a rect transform down to a CDF set by |
| 6196 | // `(wide_log2 + high_log2 + 1) >> 1`: TX_32X8 -> the 16x16 set, |
| 6197 | // TX_16X4 -> the 8x8 set, TX_8X4 -> the 8x8 set -- exactly the sets |
| 6198 | // the UNSPLIT 1:4 (`decode_block_rect4`) and 16x8 (`decode_leaf_rect`) |
| 6199 | // paths already prove for these same shapes. |
| 6200 | (32, 8) => Some((TxbSet::Chroma16, &SCAN_32X8[..])), |
| 6201 | (8, 32) => Some((TxbSet::Chroma16, &SCAN_8X32[..])), |
| 6202 | (16, 4) => Some((TxbSet::Chroma8, &SCAN_16X4[..])), |
| 6203 | (4, 16) => Some((TxbSet::Chroma8, &SCAN_4X16[..])), |
| 6204 | (8, 4) => Some((TxbSet::ChromaRect8x4, &SCAN_8X4[..])), |
| 6205 | (4, 8) => Some((TxbSet::ChromaRect8x4, &SCAN_4X8[..])), |
| 6206 | _ => None, |
| 6207 | }; |
| 6208 | if !m.skip { |
| 6209 | if let Some(i) = match (chroma_w, chroma_h) { |
| 6210 | (32, 8) => Some(0), |
| 6211 | (8, 32) => Some(1), |
| 6212 | (8, 4) => Some(2), |
| 6213 | (4, 8) => Some(3), |
| 6214 | _ => None, |
| 6215 | } { |
| 6216 | RECT_SPLIT_CHROMA_SHAPE_HITS.with(|c| { |
| 6217 | let mut hits = c.get(); |
| 6218 | hits[i] += 1; |
| 6219 | c.set(hits); |
| 6220 | }); |
| 6221 | } |
| 6222 | if chroma.is_none() { |
| 6223 | // Diagnostic for the next caller that lands here (class |
| 6224 | // refusal-names-a-correlate: the message names the tool, not the |
| 6225 | // shape); the refusal below keeps its inventory-pinned wording. |
| 6226 | eprintln!("EC_RECTCHROMA_GAP luma={bw}x{bh} tx={tx_w}x{tx_h} chroma={chroma_w}x{chroma_h}"); |
| 6227 | } |
| 6228 | } |
| 6229 | // lane-rectsplit r2 (verifier finding): with both callers wired |
| 6230 | // (`decode_block_rect`'s 32x16/16x32 and `decode_block_rect64`'s |
| 6231 | // 64x32/32x64) every shape that reaches here has a table above, so this |
| 6232 | // arm is UNREACHABLE today. It stays as the shape guard for the next |
| 6233 | // caller -- a new strip size would otherwise hit the `expect` below and |
| 6234 | // panic instead of refusing by name -- and so stays in |
| 6235 | // `refusal_inventory::REFUSALS`. |
| 6236 | if !m.skip && chroma.is_none() { |
| 6237 | return Err(unsupported( |
| 6238 | "a coded HORZ/VERT strip whose chroma transform has no rect coefficient tables here", |
| 6239 | )); |
| 6240 | } |
| 6241 | RECT_SPLIT_TX_HITS.with(|c| c.set(c.get() + 1)); |
| 6242 | if bw.max(bh) == 64 && bw / tx_w > 1 && bh / tx_h > 1 { |
| 6243 | RECT_SPLIT_SB_INTERIOR_TU_HITS.with(|c| c.set(c.get() + 1)); |
| 6244 | } |
| 6245 | // LUMA: `bw/tx` x `bh/tx` square transform units, raster order. |
| 6246 | // A square unit reads through the ordinary square [`read_plane`]; a rect |
| 6247 | // one (a 1:4 strip's first split) reads through [`read_coeffs_rect`] with |
| 6248 | // the very tables the unsplit 16x8/32x16 paths use. |
| 6249 | let luma_rect: Option<(TxbSet, &[u16])> = match (tx_w, tx_h) { |
| 6250 | (w, h) if w == h => None, |
| 6251 | (16, 8) | (8, 16) => Some(( |
| 6252 | if reduced_tx_set { |
| 6253 | TxbSet::LumaRect16x8 |
| 6254 | } else { |
| 6255 | TxbSet::LumaRect16x8Set1 |
| 6256 | }, |
| 6257 | if tx_w == 16 { &SCAN_16X8[..] } else { &SCAN_8X16[..] }, |
| 6258 | )), |
| 6259 | (32, 16) => Some((TxbSet::LumaRect32x16, &SCAN_32X16[..])), |
| 6260 | (16, 32) => Some((TxbSet::LumaRect32x16, &SCAN_16X32[..])), |
| 6261 | _ => { |
| 6262 | return Err(unsupported(format!( |
| 6263 | "a split intra strip whose transform unit is {tx_w}x{tx_h} (no luma \ |
| 6264 | coefficient tables for that shape here)" |
| 6265 | ))) |
| 6266 | } |
| 6267 | }; |
| 6268 | // lane-rectsplitx r1, MEASURED: the SQUARE-unit walk below is pixel-exact |
| 6269 | // on a real stream (the pinned 16x8 filter-intra fixture decodes its whole |
| 6270 | // frame exactly, two sub-16 split strips in it), but the RECT-unit arm -- |
| 6271 | // a 1:4 strip's depth-1 TX_16X8/TX_32X16 -- mismatched ffmpeg on the |
| 6272 | // 32-level 1:4 band fixture (8-bit, cq 12, seed 42). The tables and the |
| 6273 | // per-unit rect walk stay wired above/below; the round that finds that |
| 6274 | // defect deletes this refusal and nothing else. |
| 6275 | let luma_set = txbset_for(tx_w.min(tx_h), reduced_tx_set); |
| 6276 | let luma_scan = default_scan(tx_w.min(tx_h)); |
| 6277 | let zero_tu = vec![0i32; tx_w * tx_h]; |
| 6278 | // The strip's OWN edge availability, the fallback for a transform unit |
| 6279 | // whose top-right/bottom-left pixels fall outside the strip. |
| 6280 | let block_reach = Reach::of_rect(bw, bh, px, py, y.width, y.height); |
| 6281 | for tu_row in 0..bh / tx_h { |
| 6282 | for tu_col in 0..bw / tx_w { |
| 6283 | let tu_mi = (mi_r + tu_row * (tx_h / MI), mi_c + tu_col * (tx_w / MI)); |
| 6284 | let (tu_px, tu_py) = (px + tu_col * tx_w, py + tu_row * tx_h); |
| 6285 | let (col_off, row_off) = (tu_col * tx_w, tu_row * tx_h); |
| 6286 | // lane-hgkf r1 (ported from lane-golomb 791d87a, unmerged): |
| 6287 | // libaom clips `av1_foreach_transformed_block_in_plane` to |
| 6288 | // `max_blocks_wide/high` (`mb_to_right_edge`/`mb_to_bottom_edge`, |
| 6289 | // i.e. `mi_cols * 4` / `mi_rows * 4`): a transform unit whose |
| 6290 | // TOP-LEFT sample falls outside the frame is never coded. We read |
| 6291 | // coefficients for those phantom units and desynced the stream at |
| 6292 | // the very next symbol. |
| 6293 | if tu_px >= y.true_width || tu_py >= y.true_height { |
| 6294 | continue; |
| 6295 | } |
| 6296 | // `has_top_right`/`has_bottom_left` (libaom `reconintra.c`) for a |
| 6297 | // transform unit INSIDE a block, which is a different rule from |
| 6298 | // the standalone-block table [`Reach::of`] applies -- and the |
| 6299 | // reason a depth-2 superblock strip came out one sample off |
| 6300 | // before this: `Reach::of(tx, ..)` answered as if the unit were |
| 6301 | // its own block at that superblock position. |
| 6302 | // * top-right: if the unit's right neighbour is still inside the |
| 6303 | // strip the pixels are already reconstructed (`col_off + |
| 6304 | // tx_wide < plane_bw`); otherwise only the strip's own top row |
| 6305 | // of units can reach past it, through the block-level answer. |
| 6306 | // * bottom-left: a unit that is not in the strip's left column |
| 6307 | // has none (`col_off > 0` returns 0); inside the strip |
| 6308 | // (`row_off + tx_high < plane_bh`) they are the left |
| 6309 | // neighbour's, already reconstructed; the last row falls back |
| 6310 | // to the block-level answer. |
| 6311 | let tu_reach = Reach::of_tu(bw, bh, col_off, row_off, tx_w, tx_h, block_reach); |
| 6312 | // The unit's own window on the block's palette prediction. |
| 6313 | if let Some(buf) = &m.palette_y { |
| 6314 | let tu_buf: Vec<u16> = (0..tx_h) |
| 6315 | .flat_map(|i| { |
| 6316 | let base = (row_off + i) * bw + col_off; |
| 6317 | buf[base..base + tx_w].iter().copied() |
| 6318 | }) |
| 6319 | .collect(); |
| 6320 | set_palette_pred(tu_buf); |
| 6321 | } |
| 6322 | if m.skip { |
| 6323 | y.reconstruct_rect( |
| 6324 | tu_px, |
| 6325 | tu_py, |
| 6326 | tx_w, |
| 6327 | tx_h, |
| 6328 | m.mode, |
| 6329 | m.angle_delta_y, |
| 6330 | tu_reach, |
| 6331 | &zero_tu, |
| 6332 | None, |
| 6333 | m.filter_intra, |
| 6334 | m.smooth_neighbor, |
| 6335 | ); |
| 6336 | neighbours.record_mi_luma_rect(tu_mi, tx_w, tx_h, &zero_tu); |
| 6337 | } else if let Some((rect_set, rect_scan)) = luma_rect { |
| 6338 | // The rect-unit twin of the square arm below: same contexts, |
| 6339 | // same order, through the rect coefficient reader. The |
| 6340 | // `tx_type` CDF row of a filter-intra block is |
| 6341 | // `fimode_to_intradir[filter_intra_mode]`, never the block's |
| 6342 | // own (always DC_PRED) mode -- [`read_plane`] applies that |
| 6343 | // for the square arm, so this one must too. |
| 6344 | let tu_around = neighbours.around_mi_rect(tu_mi, tx_w, tx_h)[0]; |
| 6345 | // libaom `get_txb_ctx`: a LUMA transform that covers its whole |
| 6346 | // block (`plane_bsize == txsize_to_bsize[tx_size]`, i.e. the |
| 6347 | // unsplit depth-0 strip) reads txb_skip ctx 0 flat -- the |
| 6348 | // neighbour-magnitude table is for a unit SMALLER than its |
| 6349 | // block. [`decode_block_rect`]'s own unsplit read passes a |
| 6350 | // literal 0 for the same reason; this arm applied the table |
| 6351 | // unconditionally and desynced every depth-0 rect strip that |
| 6352 | // reached it (lane-intrarect r2: ctx 5 where aomdec read 0). |
| 6353 | let tu_skip_ctx = if tx_w == bw && tx_h == bh { |
| 6354 | 0 |
| 6355 | } else { |
| 6356 | neighbours.luma_skip_ctx_rect(tu_mi, tx_w / MI, tx_h / MI) |
| 6357 | }; |
| 6358 | let mut coding = cdfs.txb(rect_set, fi_tx_row(m.mode, m.filter_intra)); |
| 6359 | let (tu_grid, tu_tx_type) = read_coeffs_rect( |
| 6360 | dec, |
| 6361 | &mut coding, |
| 6362 | rect_scan, |
| 6363 | tx_w, |
| 6364 | tx_h, |
| 6365 | tu_skip_ctx, |
| 6366 | dc_sign_ctx(tu_around.2), |
| 6367 | TxType::DctDct, |
| 6368 | )?; |
| 6369 | let residual = dequant_and_inverse_typed_wh( |
| 6370 | &tu_grid, |
| 6371 | tx_w, |
| 6372 | tx_h, |
| 6373 | crate::decode::bit_depth(), |
| 6374 | block_q_idx(), |
| 6375 | plane_q_delta(0).0, |
| 6376 | plane_q_delta(0).1, |
| 6377 | tu_tx_type, |
| 6378 | ); |
| 6379 | y.reconstruct_rect( |
| 6380 | tu_px, |
| 6381 | tu_py, |
| 6382 | tx_w, |
| 6383 | tx_h, |
| 6384 | m.mode, |
| 6385 | m.angle_delta_y, |
| 6386 | tu_reach, |
| 6387 | &residual, |
| 6388 | None, |
| 6389 | m.filter_intra, |
| 6390 | m.smooth_neighbor, |
| 6391 | ); |
| 6392 | neighbours.record_mi_luma_rect(tu_mi, tx_w, tx_h, &tu_grid); |
| 6393 | } else { |
| 6394 | let tu_around = neighbours.around_mi(tu_mi, tx_w)[0]; |
| 6395 | // This unit is smaller than the block it sits in, so |
| 6396 | // `txb_skip_ctx` is the neighbour-magnitude table, not the |
| 6397 | // lone-TU 0 (spec `get_txb_ctx_general`). |
| 6398 | let tu_skip_ctx = neighbours.luma_skip_ctx(tu_mi, tx_w / MI); |
| 6399 | let tu_grid = read_plane( |
| 6400 | dec, |
| 6401 | cdfs, |
| 6402 | luma_set, |
| 6403 | &luma_scan, |
| 6404 | 0, |
| 6405 | tu_around, |
| 6406 | m.mode, |
| 6407 | m.mode, |
| 6408 | m.angle_delta_y, |
| 6409 | tu_reach, |
| 6410 | y, |
| 6411 | tu_px, |
| 6412 | tu_py, |
| 6413 | tx_w, |
| 6414 | tx_w, |
| 6415 | base_q_idx, |
| 6416 | None, |
| 6417 | m.filter_intra, |
| 6418 | Some(tu_skip_ctx), |
| 6419 | m.smooth_neighbor, |
| 6420 | )?; |
| 6421 | neighbours.record_mi_luma(tu_mi, tx_w, &tu_grid); |
| 6422 | } |
| 6423 | } |
| 6424 | } |
| 6425 | // CHROMA: one un-split rect transform for the whole strip. |
| 6426 | let ac = m.alpha.map(|_| cfl_ac_q3_rect(y, px, py, bw, bh)); |
| 6427 | let reach = block_reach; |
| 6428 | let (u_grid, v_grid) = if m.skip { |
| 6429 | let zero = vec![0i32; chroma_w * chroma_h]; |
| 6430 | if let Some((ub, _)) = &m.palette_uv { |
| 6431 | set_palette_pred(ub.clone()); |
| 6432 | } |
| 6433 | u.reconstruct_rect( |
| 6434 | cpx, cpy, chroma_w, chroma_h, m.uv_predict_mode, m.angle_delta_uv, reach, &zero, |
| 6435 | m.alpha.zip(ac.as_deref()).map(|((au, _), ac)| (au, ac)), None, m.smooth_neighbor_uv, |
| 6436 | ); |
| 6437 | if let Some((_, vb)) = &m.palette_uv { |
| 6438 | set_palette_pred(vb.clone()); |
| 6439 | } |
| 6440 | v.reconstruct_rect( |
| 6441 | cpx, cpy, chroma_w, chroma_h, m.uv_predict_mode, m.angle_delta_uv, reach, &zero, |
| 6442 | m.alpha.zip(ac.as_deref()).map(|((_, av), ac)| (av, ac)), None, m.smooth_neighbor_uv, |
| 6443 | ); |
| 6444 | (zero.clone(), zero) |
| 6445 | } else { |
| 6446 | let (chroma_set, chroma_scan) = chroma.expect("refused above when None"); |
| 6447 | // `av1_get_ext_tx_set_type`: a chroma transform whose square-up size |
| 6448 | // is TX_32X32 or bigger resolves to `EXT_TX_SET_DCTONLY` for intra, |
| 6449 | // so only the 16x8/8x16 pair takes the mode-indexed default. |
| 6450 | let default_tx = if chroma_w.max(chroma_h) >= 32 { |
| 6451 | TxType::DctDct |
| 6452 | } else { |
| 6453 | default_intra_tx_type(m.uv_predict_mode as u8) |
| 6454 | }; |
| 6455 | let around = neighbours.around_mi_rect(at_mi, bw, bh); |
| 6456 | let mut grids: Vec<Vec<i32>> = Vec::with_capacity(2); |
| 6457 | for plane_idx in 1..=2 { |
| 6458 | let skip_ctx = |
| 6459 | usize::from(around[plane_idx].0) + usize::from(around[plane_idx].1); |
| 6460 | let mut coding = cdfs.txb(chroma_set, m.uv_predict_mode); |
| 6461 | let (levels, tx_type) = read_coeffs_rect( |
| 6462 | dec, |
| 6463 | &mut coding, |
| 6464 | chroma_scan, |
| 6465 | chroma_w, |
| 6466 | chroma_h, |
| 6467 | skip_ctx, |
| 6468 | dc_sign_ctx(around[plane_idx].2), |
| 6469 | default_tx, |
| 6470 | )?; |
| 6471 | // The same delta_q proof the unsplit rect64 path records |
| 6472 | // (lane-rectsplit r4: lifting the split-transform refusal moved |
| 6473 | // superblock strips onto this path, so only counting there would |
| 6474 | // leave the delta_q gate's counter silent for them). |
| 6475 | if bw.max(bh) == 64 && CURRENT_Q_IDX.with(|c| c.get()) != i32::from(base_q_idx) { |
| 6476 | RECT64_QIDX_DRIFT_HITS.with(|c| c.set(c.get() + 1)); |
| 6477 | } |
| 6478 | let residual = dequant_and_inverse_typed_wh( |
| 6479 | &levels, |
| 6480 | chroma_w, |
| 6481 | chroma_h, |
| 6482 | crate::decode::bit_depth(), |
| 6483 | CURRENT_Q_IDX.with(|c| c.get()), |
| 6484 | plane_q_delta(plane_idx).0, |
| 6485 | plane_q_delta(plane_idx).1, |
| 6486 | tx_type, |
| 6487 | ); |
| 6488 | let cfl = m |
| 6489 | .alpha |
| 6490 | .zip(ac.as_deref()) |
| 6491 | .map(|((au, av), ac)| (if plane_idx == 1 { au } else { av }, ac)); |
| 6492 | let plane = if plane_idx == 1 { &mut *u } else { &mut *v }; |
| 6493 | if let Some((ub, vb)) = &m.palette_uv { |
| 6494 | let buf = if plane_idx == 1 { ub } else { vb }; |
| 6495 | set_palette_pred(buf.clone()); |
| 6496 | } |
| 6497 | plane.reconstruct_rect( |
| 6498 | cpx, |
| 6499 | cpy, |
| 6500 | chroma_w, |
| 6501 | chroma_h, |
| 6502 | m.uv_predict_mode, |
| 6503 | m.angle_delta_uv, |
| 6504 | reach, |
| 6505 | &residual, |
| 6506 | cfl, |
| 6507 | None, |
| 6508 | m.smooth_neighbor_uv, |
| 6509 | ); |
| 6510 | grids.push(levels); |
| 6511 | } |
| 6512 | let mut it = grids.into_iter(); |
| 6513 | (it.next().unwrap(), it.next().unwrap()) |
| 6514 | }; |
| 6515 | neighbours.record_split_luma_rect_mi( |
| 6516 | at_mi, |
| 6517 | bw, |
| 6518 | bh, |
| 6519 | m.mode, |
| 6520 | m.uv_predict_mode, |
| 6521 | [&u_grid, &v_grid], |
| 6522 | ); |
| 6523 | neighbours.fill_skip_grid_rect((mi_r, mi_c), bw / MI, bh / MI, m.skip); |
| 6524 | neighbours.fill_lf_grid_rect((mi_r, mi_c), bw / MI, bh / MI, tx_w as u8, tx_h as u8, 0); |
| 6525 | if std::env::var_os("EC_AV1_TRACE").is_some() { |
| 6526 | let (rng, _) = dec.debug_state(); |
| 6527 | eprintln!( |
| 6528 | "TRACE_RECT_SPLIT mi_row={mi_r} mi_col={mi_c} bw={bw} bh={bh} \ |
| 6529 | tx={tx_w}x{tx_h} rng={rng}" |
| 6530 | ); |
| 6531 | } |
| 6532 | Ok(()) |
| 6533 | } |
| 6534 | |
| 6535 | /// One intra-coded HORZ/VERT strip on the INTER block path (lane-intrarect |
| 6536 | /// r1) -- the shape [`decode_inter_block`]'s intra arm refused by name |
| 6537 | /// because it predicts squares only. Every pixel here goes through the rect |
| 6538 | /// machinery the key-frame paths already prove: [`decode_rect_split`], which |
| 6539 | /// at `depth == 0` is the unsplit strip as a single transform unit and |
| 6540 | /// beyond it the per-unit walk of [`depth_to_tx_wh`]. |
| 6541 | /// |
| 6542 | /// What is genuinely different from [`decode_block_rect`] is the mode |
| 6543 | /// syntax: on an inter frame an intra block reads `y_mode[size_group]` and |
| 6544 | /// `uv_mode` (libaom `read_intra_block_mode_info`, decodemv.c:1065), never |
| 6545 | /// the key frame's `kf_y_mode` above/left pair. This mirrors the square |
| 6546 | /// intra-in-inter arm's own reads at the rect footprint, including its |
| 6547 | /// inherited gaps -- no `use_filter_intra` symbol (every inter gate recipe |
| 6548 | /// pins `--enable-filter-intra=0`; lane-fiinter owns that read for both the |
| 6549 | /// square and this arm) and a nonzero luma angle delta refused by name. |
| 6550 | /// |
| 6551 | /// The strip's own neighbour records (luma per transform unit, chroma, skip |
| 6552 | /// grid, deblock grid) are [`decode_rect_split`]'s; the caller adds the |
| 6553 | /// inter-side bands and the mi map. |
| 6554 | #[allow(clippy::too_many_arguments)] |
| 6555 | fn decode_intra_rect_in_inter( |
| 6556 | dec: &mut SymbolDecoder, |
| 6557 | cdfs: &mut Cdfs, |
| 6558 | neighbours: &mut Neighbours, |
| 6559 | at: (usize, usize), |
| 6560 | bw: usize, |
| 6561 | bh: usize, |
| 6562 | skip: bool, |
| 6563 | allow_screen_content_tools: bool, |
| 6564 | y: &mut PlaneBuf, |
| 6565 | u: &mut PlaneBuf, |
| 6566 | v: &mut PlaneBuf, |
| 6567 | base_q_idx: u8, |
| 6568 | ) -> Result<()> { |
| 6569 | // `at` is in MI units (the inter path's own unit since lane-inter4); |
| 6570 | // `(r, c)` is the enclosing SUB cell the SUB-granular neighbour bands |
| 6571 | // below are still keyed by. |
| 6572 | let (mi_r, mi_c) = at; |
| 6573 | let (r, c) = (mi_r / (SUB / MI), mi_c / (SUB / MI)); |
| 6574 | // `size_group_lookup` (libaom common_data.h) and `bsize_to_tx_size_cat` |
| 6575 | // (blockd.h) for the three 2:1 strip shapes: BLOCK_16X8 -> group 1 / |
| 6576 | // cat 1, BLOCK_32X16 -> 2 / 2, BLOCK_64X32 -> 3 / 3 (the rectsplitx |
| 6577 | // finding -- the category is NOT the same for every strip size). |
| 6578 | // A 1:4 strip breaks that diagonal (BLOCK_32X8 is group 1 but cat 2), so |
| 6579 | // it is refused by name rather than read from the wrong CDF row. |
| 6580 | let (size_group, tx_cat) = match (bw.min(bh), bw.max(bh)) { |
| 6581 | (8, 16) => (1usize, 1usize), |
| 6582 | (16, 32) => (2, 2), |
| 6583 | (32, 64) => (3, 3), |
| 6584 | _ => { |
| 6585 | return Err(unsupported( |
| 6586 | "an intra-coded 1:4 (or other non-2:1) rect strip on the inter block path", |
| 6587 | )) |
| 6588 | } |
| 6589 | }; |
| 6590 | // [`read_intra_mode_rect`]'s own refusal: palette/intrabc syntax is |
| 6591 | // consumed for square blocks only, so a strip in a screen-content frame |
| 6592 | // would skip symbols the encoder wrote. |
| 6593 | if allow_screen_content_tools { |
| 6594 | return Err(unsupported( |
| 6595 | "a HORZ/VERT intra strip in a screen-content frame (palette syntax \ |
| 6596 | is consumed for square blocks only)", |
| 6597 | )); |
| 6598 | } |
| 6599 | let mode = dec.symbol(&mut cdfs.y_mode[size_group]); |
| 6600 | if mode >= 13 { |
| 6601 | return Err(unsupported( |
| 6602 | "an intra mode this decoder does not code (round 2)", |
| 6603 | )); |
| 6604 | } |
| 6605 | // lane-angleinter (merged on main for the square intra-in-inter arm): |
| 6606 | // the delta is read off `angle_delta_cdf[mode - V_PRED]` and feeds the |
| 6607 | // directional predictor; this arm refused a nonzero one before the merge. |
| 6608 | let angle_delta_y = if (V_PRED..=D67_PRED).contains(&mode) { |
| 6609 | read_angle_delta(dec, &mut cdfs.angle_delta[mode - V_PRED]) |
| 6610 | } else { |
| 6611 | 0 |
| 6612 | }; |
| 6613 | if angle_delta_y != 0 { |
| 6614 | INTRA_IN_INTER_ANGLE_DELTA_Y_HITS.with(|c| c.set(c.get() + 1)); |
| 6615 | } |
| 6616 | // `is_cfl_allowed` (spec 5.11.5) caps CFL at 32x32, so the |
| 6617 | // superblock-level strip reads the no-CFL alphabet -- the same split |
| 6618 | // [`read_intra_mode_rect`] makes for [`decode_block_rect64`]. |
| 6619 | let cfl_allowed = bw.max(bh) <= 32; |
| 6620 | let uv_mode = if cfl_allowed { |
| 6621 | dec.symbol(&mut cdfs.uv_mode_cfl[mode]) |
| 6622 | } else { |
| 6623 | dec.symbol(&mut cdfs.uv_mode_no_cfl[mode]) |
| 6624 | }; |
| 6625 | if (9..=12).contains(&uv_mode) { |
| 6626 | SMOOTH_UV_HITS.with(|h| h.set(h.get() + 1)); |
| 6627 | } |
| 6628 | let alpha = if cfl_allowed && uv_mode == UV_CFL_PRED { |
| 6629 | Some(read_cfl_alphas(dec, cdfs)) |
| 6630 | } else { |
| 6631 | None |
| 6632 | }; |
| 6633 | let uv_predict_mode = if uv_mode == UV_CFL_PRED { |
| 6634 | DC_PRED |
| 6635 | } else { |
| 6636 | uv_mode |
| 6637 | }; |
| 6638 | let angle_delta_uv = if (V_PRED..=D67_PRED).contains(&uv_mode) { |
| 6639 | DIRECTIONAL_UV_HITS.with(|h| h.set(h.get() + 1)); |
| 6640 | read_angle_delta(dec, &mut cdfs.angle_delta[uv_mode - V_PRED]) |
| 6641 | } else { |
| 6642 | 0 |
| 6643 | }; |
| 6644 | if angle_delta_uv != 0 { |
| 6645 | UV_ANGLE_DELTA_HITS.with(|h| h.set(h.get() + 1)); |
| 6646 | INTRA_IN_INTER_ANGLE_DELTA_UV_HITS.with(|h| h.set(h.get() + 1)); |
| 6647 | } |
| 6648 | // lane-fiinter (merged on main for the square intra-in-inter arm): |
| 6649 | // `read_filter_intra_mode_info` is the tail of |
| 6650 | // `read_intra_block_mode_info`, after the palette syntax (refused whole |
| 6651 | // above for this shape), and `av1_filter_intra_allowed_bsize` admits the |
| 6652 | // 2:1 strips with both sides <= 32 -- their own CDF rows |
| 6653 | // ([`filter_intra_size_class_rect`]). |
| 6654 | let mut filter_intra = None; |
| 6655 | if mode == DC_PRED |
| 6656 | && ENABLE_FILTER_INTRA_INTER.with(std::cell::Cell::get) |
| 6657 | && let Some(class) = filter_intra_size_class_rect(bw, bh) |
| 6658 | && dec.symbol(&mut cdfs.filter_intra[class]) != 0 |
| 6659 | { |
| 6660 | FILTER_INTRA_HITS.with(|h| h.set(h.get() + 1)); |
| 6661 | INTRA_IN_INTER_FILTER_INTRA_HITS.with(|h| h.set(h.get() + 1)); |
| 6662 | filter_intra = Some(dec.symbol(&mut cdfs.filter_intra_mode)); |
| 6663 | } |
| 6664 | // The intra-edge filter type is the NEIGHBOUR's mode (libaom |
| 6665 | // `get_filt_type`), luma's off the luma neighbours and chroma's off the |
| 6666 | // chroma ones -- [`decode_block_rect`]'s own pair. |
| 6667 | let (nb_above_mode, nb_left_mode) = neighbours.modes_above_left(r, c); |
| 6668 | let smooth_neighbor = is_smooth_mode(nb_above_mode) || is_smooth_mode(nb_left_mode); |
| 6669 | if smooth_neighbor { |
| 6670 | SMOOTH_LUMA_HITS.with(|h| h.set(h.get() + 1)); |
| 6671 | } |
| 6672 | let smooth_neighbor_uv = neighbours.smooth_uv_neighbour(mi_r, mi_c, r, c); |
| 6673 | let depth = if TX_SELECT_INTER.with(std::cell::Cell::get) { |
| 6674 | let ctx = tx_size_context_rect(neighbours, (mi_r, mi_c), bw, bh); |
| 6675 | match tx_cat { |
| 6676 | 1 => dec.symbol(&mut cdfs.tx_size_cat1[ctx]), |
| 6677 | 2 => dec.symbol(&mut cdfs.tx_size_cat2[ctx]), |
| 6678 | _ => dec.symbol(&mut cdfs.tx_size_cat3[ctx]), |
| 6679 | } |
| 6680 | } else { |
| 6681 | 0 |
| 6682 | }; |
| 6683 | if depth != 0 { |
| 6684 | TX_DEPTH_HITS.with(|h| h.set(h.get() + 1)); |
| 6685 | // merge b51719f+lane-intrarect (class merge-cross-product-defect): |
| 6686 | // main's 10-bit `split_transform_intra_block` gate reaches this arm |
| 6687 | // with a nonzero tx depth, and the split rect-TU walk mis-decodes it |
| 6688 | // (2340 luma samples off at seed 51; refusing here restores that gate |
| 6689 | // while the depth-0 strip the lane proved keeps decoding). The lane's |
| 6690 | // own gate never exercised a split strip. |
| 6691 | return Err(unsupported( |
| 6692 | "a split (nonzero tx_depth) transform on an intra HORZ/VERT strip in an inter frame", |
| 6693 | )); |
| 6694 | } |
| 6695 | let (tx_w, tx_h) = depth_to_tx_wh(bw, bh, depth); |
| 6696 | let modes = RectStripModes { |
| 6697 | skip, |
| 6698 | mode, |
| 6699 | angle_delta_y, |
| 6700 | uv_predict_mode, |
| 6701 | angle_delta_uv, |
| 6702 | alpha, |
| 6703 | filter_intra, |
| 6704 | smooth_neighbor, |
| 6705 | smooth_neighbor_uv, |
| 6706 | // screen-content frames are refused whole above, so this strip can |
| 6707 | // carry no palette. |
| 6708 | palette_y: None, |
| 6709 | palette_uv: None, |
| 6710 | }; |
| 6711 | decode_rect_split( |
| 6712 | dec, |
| 6713 | cdfs, |
| 6714 | neighbours, |
| 6715 | (mi_r, mi_c), |
| 6716 | bw, |
| 6717 | bh, |
| 6718 | tx_w, |
| 6719 | tx_h, |
| 6720 | &modes, |
| 6721 | y, |
| 6722 | u, |
| 6723 | v, |
| 6724 | base_q_idx, |
| 6725 | REDUCED_TX_SET_INTER.with(std::cell::Cell::get), |
| 6726 | )?; |
| 6727 | let class = match bw.max(bh) { |
| 6728 | 64 => 0, |
| 6729 | 32 => 1, |
| 6730 | _ => 2, |
| 6731 | }; |
| 6732 | INTRA_RECT_IN_INTER_HITS.with(|h| { |
| 6733 | let mut v = h.get(); |
| 6734 | v[class] += 1; |
| 6735 | h.set(v); |
| 6736 | }); |
| 6737 | Ok(()) |
| 6738 | } |
| 6739 | |
| 6740 | /// Decodes one true `bw`x`bh` `PARTITION_HORZ`/`PARTITION_VERT` intra strip |
| 6741 | /// (lane-intradisp r1, spec `decode_block` restricted to a 32x32 quadrant's |
| 6742 | /// two rect children). Only the `skip` case is supported: `skip`/`y_mode`/ |
| 6743 | /// `uv_mode`/`filter_intra`/`tx_depth` are read exactly per spec either way |
| 6744 | /// (so a skip strip elsewhere in the tile stays in sync), but this decoder |
| 6745 | /// has no rectangular-transform coefficient reader, and a chroma plane here |
| 6746 | /// is genuinely `TX_16X8`/`TX_8X16` (`av1_get_max_uv_txsize`, spec 5.11.16) |
| 6747 | /// with no depth-based square-tiling escape the way luma's own resolved |
| 6748 | /// `tx_depth` has (`sub_tx_size_map[TX_32X16]` == `TX_16X16`, square) -- |
| 6749 | /// so a coded (non-skip) strip is refused by name instead of guess-decoded. |
| 6750 | #[allow(clippy::too_many_arguments)] |
| 6751 | fn decode_block_rect( |
| 6752 | dec: &mut SymbolDecoder, |
| 6753 | cdfs: &mut Cdfs, |
| 6754 | neighbours: &mut Neighbours, |
| 6755 | at: (usize, usize), |
| 6756 | bw: usize, |
| 6757 | bh: usize, |
| 6758 | y: &mut PlaneBuf, |
| 6759 | u: &mut PlaneBuf, |
| 6760 | v: &mut PlaneBuf, |
| 6761 | enable_filter_intra: bool, |
| 6762 | allow_screen_content_tools: bool, |
| 6763 | base_q_idx: u8, |
| 6764 | tx_select: bool, |
| 6765 | reduced_tx_set: bool, |
| 6766 | ) -> Result<()> { |
| 6767 | let (r, c) = at; |
| 6768 | let (px, py) = (c * SUB, r * SUB); |
| 6769 | let (nb_above_mode, nb_left_mode) = neighbours.modes_above_left(r, c); |
| 6770 | let (palette_ctx, palette_cache) = neighbours.palette_ctx_and_cache(at); |
| 6771 | let palette_uv_cache = neighbours.palette_uv_cache(at); |
| 6772 | let (skip, mode, angle_delta_y, uv_mode, angle_delta_uv, alpha, filter_intra, palette_y, palette_uv) = |
| 6773 | read_intra_mode_rect( |
| 6774 | dec, |
| 6775 | cdfs, |
| 6776 | nb_above_mode, |
| 6777 | nb_left_mode, |
| 6778 | true, |
| 6779 | true, |
| 6780 | bw, |
| 6781 | bh, |
| 6782 | enable_filter_intra, |
| 6783 | neighbours.skip_txfm_ctx(r * (SUB / MI), c * (SUB / MI)), |
| 6784 | allow_screen_content_tools, |
| 6785 | Some((palette_ctx, &palette_cache)), |
| 6786 | &palette_uv_cache, |
| 6787 | r * (SUB / MI), |
| 6788 | c * (SUB / MI), |
| 6789 | )?; |
| 6790 | let smooth_neighbor = is_smooth_mode(nb_above_mode) || is_smooth_mode(nb_left_mode); |
| 6791 | if smooth_neighbor { |
| 6792 | SMOOTH_LUMA_HITS.with(|c| c.set(c.get() + 1)); |
| 6793 | } |
| 6794 | // lane-chroma r3: chroma's own edge-filter-strength neighbour check |
| 6795 | // (spec `get_intra_edge_filter_type`) reads the CHROMA neighbour's |
| 6796 | // `uv_mode`, not the luma one. |
| 6797 | let smooth_neighbor_uv = |
| 6798 | neighbours.smooth_uv_neighbour(r * (SUB / MI), c * (SUB / MI), r, c); |
| 6799 | // lane-rectsplit r1: `predict_filter_intra` now takes the block's own |
| 6800 | // `bw`x`bh` (`av1_filter_intra_predictor_c` walks its 4x2 patches over a |
| 6801 | // rectangle just as happily), so a `use_filter_intra` strip at this level |
| 6802 | // predicts instead of refusing. The below-16x16 leaf arm |
| 6803 | // (`decode_leaf_rect`) keeps the refusal: it has no gate of its own. |
| 6804 | if filter_intra.is_some() { |
| 6805 | FILTER_INTRA_RECT_HITS.with(|c| c.set(c.get() + 1)); |
| 6806 | } |
| 6807 | let uv_predict_mode = if uv_mode == UV_CFL_PRED { |
| 6808 | DC_PRED |
| 6809 | } else { |
| 6810 | uv_mode |
| 6811 | }; |
| 6812 | let (mi_r, mi_c) = (r * (SUB / MI), c * (SUB / MI)); |
| 6813 | // The tx-depth symbol EXISTS only under TX_MODE_SELECT. Both square paths |
| 6814 | // gate their `read_tx_size` on `tx_select` for exactly this reason; this |
| 6815 | // one did not, so with `--enable-tx-size-search=0` -- which several gate |
| 6816 | // recipes use -- the decoder consumed a symbol the encoder never wrote and |
| 6817 | // desynced the tile from that point on. That is why both strips of the |
| 6818 | // pinned HORZ quadrant were wrong from their very first pixel, and why our |
| 6819 | // range after the read equalled the oracle's range for the whole block. |
| 6820 | let depth = if tx_select { |
| 6821 | let ctx = tx_size_context_rect(neighbours, (mi_r, mi_c), bw, bh); |
| 6822 | dec.symbol(&mut cdfs.tx_size_cat2[ctx]) |
| 6823 | } else { |
| 6824 | 0 |
| 6825 | }; |
| 6826 | if depth != 0 { |
| 6827 | TX_DEPTH_HITS.with(|c| c.set(c.get() + 1)); |
| 6828 | // lane-rectsplit r1: a split transform is predicted and reconstructed |
| 6829 | // per transform unit, each unit taking its edges from the previous |
| 6830 | // unit's reconstruction inside this same strip (spec 5.11.36) -- |
| 6831 | // [`decode_rect_split`], the unit named by [`depth_to_tx_wh`]. |
| 6832 | let (tx_w, tx_h) = depth_to_tx_wh(bw, bh, depth); |
| 6833 | if std::env::var_os("EC_SBPART_DUMP64").is_some() { |
| 6834 | eprintln!( |
| 6835 | "DUMP64SPLIT mi_r={mi_r} mi_c={mi_c} px={px} py={py} bw={bw} bh={bh} \ |
| 6836 | depth={depth} tx={tx_w}x{tx_h} mode={mode} uv={uv_predict_mode} skip={skip} \ |
| 6837 | angle_y={angle_delta_y}" |
| 6838 | ); |
| 6839 | } |
| 6840 | let modes = RectStripModes { |
| 6841 | skip, |
| 6842 | mode, |
| 6843 | angle_delta_y, |
| 6844 | uv_predict_mode, |
| 6845 | angle_delta_uv, |
| 6846 | alpha, |
| 6847 | filter_intra, |
| 6848 | smooth_neighbor, |
| 6849 | smooth_neighbor_uv, |
| 6850 | palette_y: palette_y |
| 6851 | .as_ref() |
| 6852 | .map(|p| p.map.iter().map(|&i| p.colors[i as usize]).collect()), |
| 6853 | palette_uv: palette_uv.as_ref().map(|p| { |
| 6854 | ( |
| 6855 | p.map.iter().map(|&i| p.u_colors[i as usize]).collect(), |
| 6856 | p.map.iter().map(|&i| p.v_colors[i as usize]).collect(), |
| 6857 | ) |
| 6858 | }), |
| 6859 | }; |
| 6860 | if palette_y.is_some() || palette_uv.is_some() { |
| 6861 | PALETTE_SPLIT_TX_HITS.with(|c| c.set(c.get() + 1)); |
| 6862 | } |
| 6863 | decode_rect_split( |
| 6864 | dec, |
| 6865 | cdfs, |
| 6866 | neighbours, |
| 6867 | (mi_r, mi_c), |
| 6868 | bw, |
| 6869 | bh, |
| 6870 | tx_w, |
| 6871 | tx_h, |
| 6872 | &modes, |
| 6873 | y, |
| 6874 | u, |
| 6875 | v, |
| 6876 | base_q_idx, |
| 6877 | reduced_tx_set, |
| 6878 | )?; |
| 6879 | // This early return skips this fn's own tail, so the palette |
| 6880 | // neighbour state has to be stamped HERE too: without it a split-tx |
| 6881 | // strip left the above/left palette arrays holding a DEAD block's |
| 6882 | // colours, and the next block's colour cache |
| 6883 | // ([`Neighbours::palette_uv_cache`]) then hit a cached entry with the |
| 6884 | // wrong VALUE -- same bit count, so the entropy stream stayed in sync |
| 6885 | // and only those pixels were wrong (lane-palette2 r10). |
| 6886 | let (py_size, py_colors) = palette_y.as_ref().map_or((0, [0u16; 8]), |p| (p.size, p.colors)); |
| 6887 | neighbours.record_palette_y_rect(at, bw, bh, py_size, py_colors); |
| 6888 | let (puv_size, puv_colors) = |
| 6889 | palette_uv.as_ref().map_or((0, [0u16; 8]), |p| (p.size, p.u_colors)); |
| 6890 | neighbours.record_palette_uv_rect(at, bw, bh, puv_size, puv_colors); |
| 6891 | RECT_PARTITION_HITS.with(|c| c.set(c.get() + 1)); |
| 6892 | return Ok(()); |
| 6893 | } |
| 6894 | let (tx_w, tx_h) = (bw, bh); |
| 6895 | let (cpx, cpy) = (px / 2, py / 2); |
| 6896 | let (chroma_w, chroma_h) = (bw / 2, bh / 2); |
| 6897 | let reach = Reach::of_rect(bw, bh, px, py, y.width, y.height); |
| 6898 | if std::env::var_os("EC_AV1_TRACE").is_some() { |
| 6899 | let (rng, _) = dec.debug_state(); |
| 6900 | eprintln!( |
| 6901 | "TRACE_RECT_IMODE mi_row={mi_r} mi_col={mi_c} mode={mode} uv_mode={uv_mode} \ |
| 6902 | skip={} tx={depth} rng={rng}", |
| 6903 | skip as i32 |
| 6904 | ); |
| 6905 | } |
| 6906 | // As [`decode_block`]'s own [`PALETTE_PRED`] wiring: a palette-Y/UV |
| 6907 | // block's prediction is the reconstructed colour-index map, set fresh |
| 6908 | // right before each `reconstruct_rect` call below (lane-palette2 r4). |
| 6909 | if let Some(ref pyv) = palette_y { |
| 6910 | let buf: Vec<u16> = pyv.map.iter().map(|&idx| pyv.colors[idx as usize]).collect(); |
| 6911 | set_palette_pred(buf); |
| 6912 | } |
| 6913 | let palette_uv_bufs: Option<(Vec<u16>, Vec<u16>)> = palette_uv.as_ref().map(|puv| { |
| 6914 | ( |
| 6915 | puv.map.iter().map(|&idx| puv.u_colors[idx as usize]).collect(), |
| 6916 | puv.map.iter().map(|&idx| puv.v_colors[idx as usize]).collect(), |
| 6917 | ) |
| 6918 | }); |
| 6919 | if skip { |
| 6920 | y.reconstruct_rect( |
| 6921 | px, |
| 6922 | py, |
| 6923 | bw, |
| 6924 | bh, |
| 6925 | mode, |
| 6926 | angle_delta_y, |
| 6927 | reach, |
| 6928 | &vec![0i32; bw * bh], |
| 6929 | None, |
| 6930 | filter_intra, |
| 6931 | smooth_neighbor, |
| 6932 | ); |
| 6933 | let ac = alpha.map(|_| cfl_ac_q3_rect(y, px, py, bw, bh)); |
| 6934 | if let Some((ub, _)) = &palette_uv_bufs { |
| 6935 | set_palette_pred(ub.clone()); |
| 6936 | } |
| 6937 | u.reconstruct_rect( |
| 6938 | cpx, |
| 6939 | cpy, |
| 6940 | chroma_w, |
| 6941 | chroma_h, |
| 6942 | uv_predict_mode, |
| 6943 | angle_delta_uv, |
| 6944 | reach, |
| 6945 | &vec![0i32; chroma_w * chroma_h], |
| 6946 | alpha.zip(ac.as_deref()).map(|((au, _), ac)| (au, ac)), |
| 6947 | None, |
| 6948 | smooth_neighbor_uv, |
| 6949 | ); |
| 6950 | if let Some((_, vb)) = &palette_uv_bufs { |
| 6951 | set_palette_pred(vb.clone()); |
| 6952 | } |
| 6953 | v.reconstruct_rect( |
| 6954 | cpx, |
| 6955 | cpy, |
| 6956 | chroma_w, |
| 6957 | chroma_h, |
| 6958 | uv_predict_mode, |
| 6959 | angle_delta_uv, |
| 6960 | reach, |
| 6961 | &vec![0i32; chroma_w * chroma_h], |
| 6962 | alpha.zip(ac.as_deref()).map(|((_, av), ac)| (av, ac)), |
| 6963 | None, |
| 6964 | smooth_neighbor_uv, |
| 6965 | ); |
| 6966 | neighbours.record_rect( |
| 6967 | at, |
| 6968 | bw, |
| 6969 | bh, |
| 6970 | mode, |
| 6971 | uv_predict_mode, |
| 6972 | &[ |
| 6973 | vec![0i32; bw * bh], |
| 6974 | vec![0i32; chroma_w * chroma_h], |
| 6975 | vec![0i32; chroma_w * chroma_h], |
| 6976 | ], |
| 6977 | ); |
| 6978 | } else { |
| 6979 | // lane-rectwire r2: real coefficients. `get_txsize_entropy_ctx` |
| 6980 | // reduces both size pairs to their square-up CDF sets |
| 6981 | // (`TxbSet::LumaRect32x16`/`ChromaRect16x8`, see those variants' own |
| 6982 | // doc comments); the scan order and 2D context tables are the real |
| 6983 | // rect ones (`SCAN_32X16`/etc, `base_ctx_rect`/`br_ctx_rect`). |
| 6984 | let (luma_scan, chroma_scan): (&[u16], &[u16]) = if bw == 32 { |
| 6985 | (&SCAN_32X16, &SCAN_16X8) |
| 6986 | } else { |
| 6987 | (&SCAN_16X32, &SCAN_8X16) |
| 6988 | }; |
| 6989 | // lane-rectwire r3: real coefficients desync a pixel-exact gate |
| 6990 | // (rectwire-flake-1.obu, seed 55) -- both 32x16 strips of frame 0's |
| 6991 | // mi(8,8) HORZ quadrant come out ~100% wrong from their very first |
| 6992 | // pixel (not a subtle context-table off-by-one), while the sibling |
| 6993 | // non-rect quadrants of the same frame match. Range-ladder bisection |
| 6994 | // against a real aomdec (EC_TRACE_COEFF/EC_TRACE_MODE) was started |
| 6995 | // but the intra key-frame path has no traced symbol between the |
| 6996 | // partition read and the first coefficient block (mode/skip/tx_depth |
| 6997 | // reads are all untraced in libaom's decodeframe.c intra path, unlike |
| 6998 | // the inter EC_TRACE_MODE this round added), so a same-length range |
| 6999 | // comparison wasn't reachable within budget -- see |
| 7000 | // lanes/rectwire-r3.report.md for what was ruled out (base_ctx_rect's |
| 7001 | // 5-neighbour offsets, u/v_skip_ctx's above+left formula, and |
| 7002 | // dc_sign_ctx's around-index all cross-checked byte-for-byte against |
| 7003 | // libaom and match). lane-rectwire r4: range-ladder against the |
| 7004 | // now-instrumented oracle (EC_TRACE_MODE's intra EC_IMODE/EC_IMODE_VAL) |
| 7005 | // narrowed the divergence past skip/y_mode/uv_mode (values AND range |
| 7006 | // both confirmed in sync -- rng=58692 on both sides right after |
| 7007 | // uv_mode, with `enable_filter_intra=0` on this frame so neither side |
| 7008 | // reads a filter_intra symbol) to the `tx_size_cat2` depth read |
| 7009 | // immediately after: ours moves rng 58692 -> 43570, which cannot be |
| 7010 | // reconciled with oracle's own EC_IMODE_VAL (rng=58692, taken *after* |
| 7011 | // its own tx_size read) unless oracle's tx symbol is a near-certainty |
| 7012 | // CDF that barely narrows the range -- i.e. this decoder's |
| 7013 | // `tx_size_context_rect` ctx or `cdfs.tx_size_cat2` CDF selection for |
| 7014 | // a 32x16/16x32 strip is the first diverging symbol, not the |
| 7015 | // coefficient-context math r3 already cleared. Not confirmed byte- |
| 7016 | // exact (would need a decodemv.c EC_TRACE_MODE patch specifically on |
| 7017 | // `read_tx_size`'s own rng, not attempted this round -- budget), so |
| 7018 | // r5 CONFIRMED the suspect and it was simpler than the CDF row: the |
| 7019 | // tx-depth symbol exists only under TX_MODE_SELECT, and this path read |
| 7020 | // it unconditionally where both square paths gate on `tx_select`. With |
| 7021 | // --enable-tx-size-search=0 the encoder writes no such symbol, so the |
| 7022 | // decoder consumed one that was never there. |
| 7023 | { |
| 7024 | let around = neighbours.around_rect(at, bw, bh); |
| 7025 | let mut luma_coding = cdfs.txb(TxbSet::LumaRect32x16, fi_tx_row(mode, filter_intra)); |
| 7026 | let (luma_levels, luma_tx_type) = read_coeffs_rect( |
| 7027 | dec, |
| 7028 | &mut luma_coding, |
| 7029 | luma_scan, |
| 7030 | bw, |
| 7031 | bh, |
| 7032 | 0, |
| 7033 | dc_sign_ctx(around[0].2), |
| 7034 | TxType::DctDct, |
| 7035 | )?; |
| 7036 | if std::env::var_os("EC_AV1_TRACE").is_some() { |
| 7037 | let (rng, _) = dec.debug_state(); |
| 7038 | eprintln!("TRACE_RECT_COEFF plane=0 mi_row={mi_r} mi_col={mi_c} rng={rng}"); |
| 7039 | } |
| 7040 | let luma_residual = dequant_and_inverse_typed_wh( |
| 7041 | &luma_levels, |
| 7042 | bw, |
| 7043 | bh, |
| 7044 | crate::decode::bit_depth(), |
| 7045 | block_q_idx(), |
| 7046 | plane_q_delta(0).0, |
| 7047 | plane_q_delta(0).1, |
| 7048 | luma_tx_type, |
| 7049 | ); |
| 7050 | y.reconstruct_rect( |
| 7051 | px, |
| 7052 | py, |
| 7053 | bw, |
| 7054 | bh, |
| 7055 | mode, |
| 7056 | angle_delta_y, |
| 7057 | reach, |
| 7058 | &luma_residual, |
| 7059 | None, |
| 7060 | filter_intra, |
| 7061 | smooth_neighbor, |
| 7062 | ); |
| 7063 | let ac = alpha.map(|_| cfl_ac_q3_rect(y, px, py, bw, bh)); |
| 7064 | if let Some((ub, _)) = &palette_uv_bufs { |
| 7065 | set_palette_pred(ub.clone()); |
| 7066 | } |
| 7067 | let u_default_tx = default_intra_tx_type(uv_predict_mode as u8); |
| 7068 | let u_skip_ctx = usize::from(around[1].0) + usize::from(around[1].1); |
| 7069 | let mut u_coding = cdfs.txb(TxbSet::ChromaRect16x8, uv_predict_mode); |
| 7070 | let (u_levels, u_tx_type) = read_coeffs_rect( |
| 7071 | dec, |
| 7072 | &mut u_coding, |
| 7073 | chroma_scan, |
| 7074 | chroma_w, |
| 7075 | chroma_h, |
| 7076 | u_skip_ctx, |
| 7077 | dc_sign_ctx(around[1].2), |
| 7078 | u_default_tx, |
| 7079 | )?; |
| 7080 | if std::env::var_os("EC_AV1_TRACE").is_some() { |
| 7081 | let (rng, _) = dec.debug_state(); |
| 7082 | eprintln!("TRACE_RECT_COEFF plane=1 mi_row={mi_r} mi_col={mi_c} rng={rng}"); |
| 7083 | } |
| 7084 | let u_residual = dequant_and_inverse_typed_wh( |
| 7085 | &u_levels, |
| 7086 | chroma_w, |
| 7087 | chroma_h, |
| 7088 | crate::decode::bit_depth(), |
| 7089 | block_q_idx(), |
| 7090 | plane_q_delta(1).0, |
| 7091 | plane_q_delta(1).1, |
| 7092 | u_tx_type, |
| 7093 | ); |
| 7094 | u.reconstruct_rect( |
| 7095 | cpx, |
| 7096 | cpy, |
| 7097 | chroma_w, |
| 7098 | chroma_h, |
| 7099 | uv_predict_mode, |
| 7100 | angle_delta_uv, |
| 7101 | reach, |
| 7102 | &u_residual, |
| 7103 | alpha.zip(ac.as_deref()).map(|((au, _), ac)| (au, ac)), |
| 7104 | None, |
| 7105 | smooth_neighbor_uv, |
| 7106 | ); |
| 7107 | if let Some((_, vb)) = &palette_uv_bufs { |
| 7108 | set_palette_pred(vb.clone()); |
| 7109 | } |
| 7110 | let v_default_tx = default_intra_tx_type(uv_predict_mode as u8); |
| 7111 | let v_skip_ctx = usize::from(around[2].0) + usize::from(around[2].1); |
| 7112 | let mut v_coding = cdfs.txb(TxbSet::ChromaRect16x8, uv_predict_mode); |
| 7113 | let (v_levels, v_tx_type) = read_coeffs_rect( |
| 7114 | dec, |
| 7115 | &mut v_coding, |
| 7116 | chroma_scan, |
| 7117 | chroma_w, |
| 7118 | chroma_h, |
| 7119 | v_skip_ctx, |
| 7120 | dc_sign_ctx(around[2].2), |
| 7121 | v_default_tx, |
| 7122 | )?; |
| 7123 | if std::env::var_os("EC_AV1_TRACE").is_some() { |
| 7124 | let (rng, _) = dec.debug_state(); |
| 7125 | eprintln!("TRACE_RECT_COEFF plane=2 mi_row={mi_r} mi_col={mi_c} rng={rng}"); |
| 7126 | } |
| 7127 | let v_residual = dequant_and_inverse_typed_wh( |
| 7128 | &v_levels, |
| 7129 | chroma_w, |
| 7130 | chroma_h, |
| 7131 | crate::decode::bit_depth(), |
| 7132 | block_q_idx(), |
| 7133 | plane_q_delta(2).0, |
| 7134 | plane_q_delta(2).1, |
| 7135 | v_tx_type, |
| 7136 | ); |
| 7137 | v.reconstruct_rect( |
| 7138 | cpx, |
| 7139 | cpy, |
| 7140 | chroma_w, |
| 7141 | chroma_h, |
| 7142 | uv_predict_mode, |
| 7143 | angle_delta_uv, |
| 7144 | reach, |
| 7145 | &v_residual, |
| 7146 | alpha.zip(ac.as_deref()).map(|((_, av), ac)| (av, ac)), |
| 7147 | None, |
| 7148 | smooth_neighbor_uv, |
| 7149 | ); |
| 7150 | neighbours.record_rect(at, bw, bh, mode, uv_predict_mode, &[luma_levels, u_levels, v_levels]); |
| 7151 | RECT_COEFF_HITS.with(|c| c.set(c.get() + 1)); |
| 7152 | } |
| 7153 | } |
| 7154 | let (py_size, py_colors) = palette_y.as_ref().map_or((0, [0u16; 8]), |p| (p.size, p.colors)); |
| 7155 | neighbours.record_palette_y_rect(at, bw, bh, py_size, py_colors); |
| 7156 | let (puv_size, puv_colors) = |
| 7157 | palette_uv.as_ref().map_or((0, [0u16; 8]), |p| (p.size, p.u_colors)); |
| 7158 | neighbours.record_palette_uv_rect(at, bw, bh, puv_size, puv_colors); |
| 7159 | neighbours.fill_skip_grid_rect((mi_r, mi_c), bw / MI, bh / MI, skip); |
| 7160 | neighbours.fill_lf_grid_rect((mi_r, mi_c), bw / MI, bh / MI, tx_w as u8, tx_h as u8, 0); |
| 7161 | RECT_PARTITION_HITS.with(|c| c.set(c.get() + 1)); |
| 7162 | if std::env::var_os("EC_AV1_TRACE").is_some() { |
| 7163 | eprintln!("TRACE_RECT32_END mi_row={mi_r} mi_col={mi_c} bw={bw} bh={bh}"); |
| 7164 | } |
| 7165 | Ok(()) |
| 7166 | } |
| 7167 | |
| 7168 | /// Decodes one `bw`x`bh` rect strip of a plain 16x16-level `PARTITION_HORZ`/ |
| 7169 | /// `PARTITION_VERT` (lane-rect16 r2), sibling to [`decode_block_rect`] but |
| 7170 | /// addressed by REAL mi coordinates rather than derived from an [`SUB`]-grid |
| 7171 | /// `outer_at`, the same relationship [`decode_leaf8`] has to `decode_block`: |
| 7172 | /// the second strip of a HORZ/VERT split sits offset by half the 16x16 |
| 7173 | /// parent, which `decode_block_rect`'s `at`-derived `px = c * SUB` can't |
| 7174 | /// name. `prev_leaf` mirrors `decode_leaf8`'s own convention -- when the |
| 7175 | /// previous strip is directly above (same column, `mi_h` rows up) or |
| 7176 | /// directly left (same row, `mi_w` cols over), its mode overrides the coarse |
| 7177 | /// per-16x16-cell `above_mode`/`left_mode` slot for THIS strip's own mode |
| 7178 | /// context read, and bookkeeping goes through the `_mi`/`_rect` neighbour |
| 7179 | /// writers (`record_mi_rect`, `fill_skip_grid_rect`, `fill_lf_grid_rect`) at |
| 7180 | /// this strip's real position, never the coarse `record_rect` (which the |
| 7181 | /// caller uses once, after both strips, exactly like the `VERT_B` arm next |
| 7182 | /// to it). The non-skip case (lane-rectx) reads real `TX_16X8`/`TX_8X16` |
| 7183 | /// luma coefficients (`TxbSet::LumaRect16x8`) and their `TX_8X4`/`TX_4X8` |
| 7184 | /// chroma half (`TxbSet::ChromaRect8x4`), the two size classes one level |
| 7185 | /// under [`decode_block_rect`]'s own 32x16/16x32 strip. |
| 7186 | #[allow(clippy::too_many_arguments)] |
| 7187 | fn decode_leaf_rect( |
| 7188 | dec: &mut SymbolDecoder, |
| 7189 | cdfs: &mut Cdfs, |
| 7190 | neighbours: &mut Neighbours, |
| 7191 | outer_at: (usize, usize), |
| 7192 | leaf_mi: (usize, usize), |
| 7193 | bw: usize, |
| 7194 | bh: usize, |
| 7195 | prev_leaf: Option<((usize, usize), usize)>, |
| 7196 | y: &mut PlaneBuf, |
| 7197 | u: &mut PlaneBuf, |
| 7198 | v: &mut PlaneBuf, |
| 7199 | enable_filter_intra: bool, |
| 7200 | allow_screen_content_tools: bool, |
| 7201 | base_q_idx: u8, |
| 7202 | tx_select: bool, |
| 7203 | reduced_tx_set: bool, |
| 7204 | ) -> Result<usize> { |
| 7205 | let _ = base_q_idx; |
| 7206 | if std::env::var_os("EC_AV1_RECTX_TRACE").is_some() { |
| 7207 | eprintln!( |
| 7208 | "decode_leaf_rect: leaf_mi={leaf_mi:?} bw={bw} bh={bh} outer_at={outer_at:?} rng={}", |
| 7209 | dec.debug_state().0 |
| 7210 | ); |
| 7211 | } |
| 7212 | let (r, c) = outer_at; |
| 7213 | let mut above_mode = neighbours.above_mode[c]; |
| 7214 | let mut left_mode = neighbours.left_mode[r]; |
| 7215 | let (mi_w, mi_h) = (bw / MI, bh / MI); |
| 7216 | if let Some(((pr, pc), pmode)) = prev_leaf { |
| 7217 | if pc == leaf_mi.1 && leaf_mi.0 == pr + mi_h { |
| 7218 | above_mode = pmode; |
| 7219 | } else if pr == leaf_mi.0 && leaf_mi.1 == pc + mi_w { |
| 7220 | left_mode = pmode; |
| 7221 | } |
| 7222 | } |
| 7223 | // Same mi-exact override [`decode_leaf8`] takes: one coarse 16x16 slot |
| 7224 | // cannot hold the modes a split (or a pair of strips) leaves behind, so |
| 7225 | // when the map holds the block at exactly this strip's above/left mi it |
| 7226 | // is the neighbour libaom's `above_mi`/`left_mi` reads. |
| 7227 | // lane-rectsplitx r2 (same class as the 1:4 fix): mi-exact AND |
| 7228 | // availability-correct -- four 16x8 strips share one coarse 16x16 |
| 7229 | // `left_mode` cell too, so the fallback must not stand in for an absent |
| 7230 | // tile-edge neighbour. |
| 7231 | let (above_mode, left_mode) = |
| 7232 | neighbours.modes_above_left_mi(leaf_mi.0, leaf_mi.1, (above_mode, left_mode)); |
| 7233 | let smooth_neighbor = |
| 7234 | is_smooth_mode(above_mode) || is_smooth_mode(left_mode); |
| 7235 | if smooth_neighbor { |
| 7236 | SMOOTH_LUMA_HITS.with(|c| c.set(c.get() + 1)); |
| 7237 | } |
| 7238 | // The palette-Y mode CDF row is picked by the neighbours' palette use |
| 7239 | // (`palette_ctx_and_cache`), so this sub-16x16 leaf must pass its own real |
| 7240 | // ctx/cache -- reading `palette_y_mode` off row 0 when a neighbour did use |
| 7241 | // a palette narrows the wrong interval and desyncs the tile even when the |
| 7242 | // decoded bit is the same. The cell is the 16px-SUB cell containing the |
| 7243 | // leaf (the palette neighbour arrays have no finer granularity). |
| 7244 | let pal_at = (leaf_mi.0 / (SUB / MI), leaf_mi.1 / (SUB / MI)); |
| 7245 | // lane-kf900 r4: the leaf's REAL mi row -- the second leaf of a 16x8 pair |
| 7246 | // sits 8 px into the cell, so the cache's superblock-top-row exclusion |
| 7247 | // must not be read off the cell (see [`Neighbours::palette_ctx_and_cache_mi`]). |
| 7248 | let (palette_ctx, palette_cache) = neighbours.palette_ctx_and_cache_mi(pal_at, leaf_mi.0); |
| 7249 | let palette_uv_cache = neighbours.palette_uv_cache_mi(pal_at, leaf_mi.0); |
| 7250 | let (skip, mode, angle_delta_y, uv_mode, angle_delta_uv, alpha, filter_intra, palette_y, palette_uv) = |
| 7251 | read_intra_mode_rect( |
| 7252 | dec, |
| 7253 | cdfs, |
| 7254 | above_mode, |
| 7255 | left_mode, |
| 7256 | true, |
| 7257 | true, |
| 7258 | bw, |
| 7259 | bh, |
| 7260 | enable_filter_intra, |
| 7261 | neighbours.skip_txfm_ctx(leaf_mi.0, leaf_mi.1), |
| 7262 | allow_screen_content_tools, |
| 7263 | Some((palette_ctx, &palette_cache)), |
| 7264 | &palette_uv_cache, |
| 7265 | leaf_mi.0, |
| 7266 | leaf_mi.1, |
| 7267 | )?; |
| 7268 | // lane-kf900 r4: palette prediction on this shape, the same colour-index |
| 7269 | // map -> base colours buffer [`decode_block_rect`] hands each |
| 7270 | // `reconstruct_rect` through [`set_palette_pred`]. |
| 7271 | if palette_y.is_some() || palette_uv.is_some() { |
| 7272 | RECT4_PALETTE_HITS.with(|c| c.set(c.get() + 1)); |
| 7273 | } |
| 7274 | let palette_y_buf: Option<Vec<u16>> = palette_y |
| 7275 | .as_ref() |
| 7276 | .map(|p| p.map.iter().map(|&i| p.colors[i as usize]).collect()); |
| 7277 | let palette_uv_bufs: Option<(Vec<u16>, Vec<u16>)> = palette_uv.as_ref().map(|p| { |
| 7278 | ( |
| 7279 | p.map.iter().map(|&i| p.u_colors[i as usize]).collect(), |
| 7280 | p.map.iter().map(|&i| p.v_colors[i as usize]).collect(), |
| 7281 | ) |
| 7282 | }); |
| 7283 | let (pal_y_size, pal_y_colors) = |
| 7284 | palette_y.as_ref().map_or((0, [0u16; 8]), |p| (p.size, p.colors)); |
| 7285 | let (pal_uv_size, pal_uv_colors) = |
| 7286 | palette_uv.as_ref().map_or((0, [0u16; 8]), |p| (p.size, p.u_colors)); |
| 7287 | // The closing leaf of the pair -- see [`record_strip_palette`]. |
| 7288 | let pal_last = if bw > bh { |
| 7289 | leaf_mi.0 % 4 == 2 |
| 7290 | } else { |
| 7291 | leaf_mi.1 % 4 == 2 |
| 7292 | }; |
| 7293 | if std::env::var_os("EC_AV1_RECTX_TRACE").is_some() { |
| 7294 | eprintln!(" skip={skip} mode={mode} angle_delta_y={angle_delta_y} uv_mode={uv_mode}"); |
| 7295 | } |
| 7296 | if filter_intra.is_some() { |
| 7297 | // lane-fistrip r1: `predict_filter_intra` already walks its 4x2 |
| 7298 | // patches over a true `bw`x`bh` block (lane-rectsplit r1), so an |
| 7299 | // 8x16/16x8 strip predicts through the same call `reconstruct_rect` |
| 7300 | // below already makes; this refusal was square-only prose. |
| 7301 | FILTER_INTRA_RECT_HITS.with(|c| c.set(c.get() + 1)); |
| 7302 | FILTER_INTRA_RECT_SUB16_HITS.with(|c| c.set(c.get() + 1)); |
| 7303 | } |
| 7304 | let uv_predict_mode = if uv_mode == UV_CFL_PRED { DC_PRED } else { uv_mode }; |
| 7305 | // lane-cfl r1, same defect as [`decode_leaf_8x8`]: chroma's intra-edge |
| 7306 | // filter type is the NEIGHBOUR's `uv_mode` (libaom `get_filt_type`), not |
| 7307 | // a constant `false`. |
| 7308 | let smooth_neighbor_uv = neighbours.smooth_uv_neighbour(leaf_mi.0, leaf_mi.1, r, c); |
| 7309 | let depth = if tx_select { |
| 7310 | let ctx = tx_size_context_rect(neighbours, leaf_mi, bw, bh); |
| 7311 | // lane-rectsplitx r1: `bsize_to_tx_size_cat` (libaom `blockd.h`, |
| 7312 | // `bsize_to_tx_size_depth_table`) is 2 for BLOCK_16X8/BLOCK_8X16, so |
| 7313 | // the category is 1 -- NOT the 2 the 32x16 strips read. Same symbol |
| 7314 | // count (3), different CDF row: a wrong-table read of a real symbol, |
| 7315 | // the class that cost lane-rectsplit r1 its 64x32 depth. |
| 7316 | // lane-rect1d r1: the CATEGORY of this leaf is 1, not 2. libaom's |
| 7317 | // `bsize_to_tx_size_cat` reads `bsize_to_tx_size_depth_table` and |
| 7318 | // subtracts one: BLOCK_16X8/8X16 (and the 1:4 BLOCK_16X4/4X16 this |
| 7319 | // function also decodes) sit at depth 2, so they read |
| 7320 | // `tx_size_cdf[1]` -- the same table a 16x16 square block reads -- |
| 7321 | // while `tx_size_cat2` belongs to the 32-level shapes |
| 7322 | // ([`decode_block_rect`]). Both tables carry three symbols, so the |
| 7323 | // wrong one decodes a plausible depth from the wrong probabilities: |
| 7324 | // the range diverges silently at the first such leaf and every later |
| 7325 | // symbol in the tile is read from the wrong place (class |
| 7326 | // `wrong-alphabet-same-value`, here same alphabet / wrong row). |
| 7327 | dec.symbol(&mut cdfs.tx_size_cat1[ctx]) |
| 7328 | } else { |
| 7329 | 0 |
| 7330 | }; |
| 7331 | if depth != 0 { |
| 7332 | // The same per-transform-unit walk the bigger strips take |
| 7333 | // (`depth_to_tx_wh`: TX_16X8 -> TX_8X8 at depth 1, -> TX_4X4 at |
| 7334 | // depth 2), addressed at this leaf's own mi position. |
| 7335 | TX_DEPTH_HITS.with(|c| c.set(c.get() + 1)); |
| 7336 | SUB16_SPLIT_HITS.with(|c| c.set(c.get() + 1)); |
| 7337 | let (tx_w, tx_h) = depth_to_tx_wh(bw, bh, depth); |
| 7338 | let modes = RectStripModes { |
| 7339 | skip, |
| 7340 | mode, |
| 7341 | angle_delta_y, |
| 7342 | uv_predict_mode, |
| 7343 | angle_delta_uv, |
| 7344 | alpha, |
| 7345 | filter_intra, |
| 7346 | smooth_neighbor, |
| 7347 | // lane-rectsplitx r3: the split arm hard-coded `false` here while |
| 7348 | // the unsplit arm below passes the real neighbour answer -- chroma |
| 7349 | // of a tx-split 16x8/8x16 leaf lost its intra-edge filter type. |
| 7350 | smooth_neighbor_uv, |
| 7351 | palette_y: palette_y_buf.clone(), |
| 7352 | palette_uv: palette_uv_bufs.clone(), |
| 7353 | }; |
| 7354 | if palette_y.is_some() || palette_uv.is_some() { |
| 7355 | PALETTE_SPLIT_TX_HITS.with(|c| c.set(c.get() + 1)); |
| 7356 | } |
| 7357 | decode_rect_split( |
| 7358 | dec, |
| 7359 | cdfs, |
| 7360 | neighbours, |
| 7361 | leaf_mi, |
| 7362 | bw, |
| 7363 | bh, |
| 7364 | tx_w, |
| 7365 | tx_h, |
| 7366 | &modes, |
| 7367 | y, |
| 7368 | u, |
| 7369 | v, |
| 7370 | base_q_idx, |
| 7371 | reduced_tx_set, |
| 7372 | )?; |
| 7373 | record_strip_palette( |
| 7374 | neighbours, pal_at, bw, bh, pal_y_size, pal_y_colors, pal_uv_size, pal_uv_colors, |
| 7375 | pal_last, |
| 7376 | ); |
| 7377 | return Ok(mode); |
| 7378 | } |
| 7379 | let (px, py) = (leaf_mi.1 * MI, leaf_mi.0 * MI); |
| 7380 | let reach = Reach::of_rect(bw, bh, px, py, y.width, y.height); |
| 7381 | let (cpx, cpy) = (px / 2, py / 2); |
| 7382 | let (chroma_w, chroma_h) = (bw / 2, bh / 2); |
| 7383 | let (luma_levels, u_levels, v_levels); |
| 7384 | if skip { |
| 7385 | luma_levels = vec![0i32; bw * bh]; |
| 7386 | u_levels = vec![0i32; chroma_w * chroma_h]; |
| 7387 | v_levels = vec![0i32; chroma_w * chroma_h]; |
| 7388 | if let Some(b) = &palette_y_buf { |
| 7389 | set_palette_pred(b.clone()); |
| 7390 | } |
| 7391 | y.reconstruct_rect( |
| 7392 | px, py, bw, bh, mode, angle_delta_y, reach, &luma_levels, None, filter_intra, |
| 7393 | smooth_neighbor, |
| 7394 | ); |
| 7395 | let ac = alpha.map(|_| cfl_ac_q3_rect(y, px, py, bw, bh)); |
| 7396 | if let Some((ub, _)) = &palette_uv_bufs { |
| 7397 | set_palette_pred(ub.clone()); |
| 7398 | } |
| 7399 | u.reconstruct_rect( |
| 7400 | cpx, cpy, chroma_w, chroma_h, uv_predict_mode, angle_delta_uv, reach, &u_levels, |
| 7401 | alpha.zip(ac.as_deref()).map(|((au, _), ac)| (au, ac)), None, smooth_neighbor_uv, |
| 7402 | ); |
| 7403 | if let Some((_, vb)) = &palette_uv_bufs { |
| 7404 | set_palette_pred(vb.clone()); |
| 7405 | } |
| 7406 | v.reconstruct_rect( |
| 7407 | cpx, cpy, chroma_w, chroma_h, uv_predict_mode, angle_delta_uv, reach, &v_levels, |
| 7408 | alpha.zip(ac.as_deref()).map(|((_, av), ac)| (av, ac)), None, smooth_neighbor_uv, |
| 7409 | ); |
| 7410 | } else { |
| 7411 | // lane-rectx: a genuine 16x8/8x16 luma transform (`TxbSet::LumaRect16x8`, |
| 7412 | // real `EOB_PT_128_LUMA` alphabet + the same mode-indexed `tx_type` |
| 7413 | // symbol the square `Luma16` set reads -- `get_ext_tx_set_type`'s |
| 7414 | // `use_reduced_set` branch returns `EXT_TX_SET_DTT4_IDTX` at |
| 7415 | // `tx_size_sqr_up == TX_16X16` regardless of the true, non-square |
| 7416 | // `tx_size`) and its chroma half (8x4/4x8, `TxbSet::ChromaRect8x4`). |
| 7417 | let (luma_scan, chroma_scan): (&[u16], &[u16]) = if bw == 16 { |
| 7418 | (&SCAN_16X8, &SCAN_8X4) |
| 7419 | } else { |
| 7420 | (&SCAN_8X16, &SCAN_4X8) |
| 7421 | }; |
| 7422 | let around = neighbours.around_mi_rect(leaf_mi, bw, bh); |
| 7423 | let luma_set = if reduced_tx_set { |
| 7424 | TxbSet::LumaRect16x8 |
| 7425 | } else { |
| 7426 | TxbSet::LumaRect16x8Set1 |
| 7427 | }; |
| 7428 | let mut luma_coding = cdfs.txb(luma_set, fi_tx_row(mode, filter_intra)); |
| 7429 | let (l_levels, luma_tx_type) = read_coeffs_rect( |
| 7430 | dec, &mut luma_coding, luma_scan, bw, bh, 0, dc_sign_ctx(around[0].2), TxType::DctDct, |
| 7431 | )?; |
| 7432 | if std::env::var_os("EC_AV1_RECTX_TRACE").is_some() { |
| 7433 | let nz: Vec<(usize, i32)> = l_levels.iter().copied().enumerate().filter(|(_, v)| *v != 0).collect(); |
| 7434 | eprintln!(" luma_tx_type={luma_tx_type:?} nz_levels={nz:?}"); |
| 7435 | } |
| 7436 | luma_levels = l_levels; |
| 7437 | let luma_residual = dequant_and_inverse_typed_wh( |
| 7438 | &luma_levels, bw, bh, crate::decode::bit_depth(), |
| 7439 | CURRENT_Q_IDX.with(|c| c.get()), plane_q_delta(0).0, plane_q_delta(0).1, luma_tx_type, |
| 7440 | ); |
| 7441 | if let Some(b) = &palette_y_buf { |
| 7442 | set_palette_pred(b.clone()); |
| 7443 | } |
| 7444 | y.reconstruct_rect( |
| 7445 | px, py, bw, bh, mode, angle_delta_y, reach, &luma_residual, None, filter_intra, |
| 7446 | smooth_neighbor, |
| 7447 | ); |
| 7448 | let ac = alpha.map(|_| cfl_ac_q3_rect(y, px, py, bw, bh)); |
| 7449 | let u_default_tx = default_intra_tx_type(uv_predict_mode as u8); |
| 7450 | let u_skip_ctx = usize::from(around[1].0) + usize::from(around[1].1); |
| 7451 | let mut u_coding = cdfs.txb(TxbSet::ChromaRect8x4, uv_predict_mode); |
| 7452 | let (u_l, u_tx_type) = read_coeffs_rect( |
| 7453 | dec, &mut u_coding, chroma_scan, chroma_w, chroma_h, u_skip_ctx, |
| 7454 | dc_sign_ctx(around[1].2), u_default_tx, |
| 7455 | )?; |
| 7456 | u_levels = u_l; |
| 7457 | let u_residual = dequant_and_inverse_typed_wh( |
| 7458 | &u_levels, chroma_w, chroma_h, crate::decode::bit_depth(), |
| 7459 | CURRENT_Q_IDX.with(|c| c.get()), plane_q_delta(1).0, plane_q_delta(1).1, u_tx_type, |
| 7460 | ); |
| 7461 | if let Some((ub, _)) = &palette_uv_bufs { |
| 7462 | set_palette_pred(ub.clone()); |
| 7463 | } |
| 7464 | u.reconstruct_rect( |
| 7465 | cpx, cpy, chroma_w, chroma_h, uv_predict_mode, angle_delta_uv, reach, &u_residual, |
| 7466 | alpha.zip(ac.as_deref()).map(|((au, _), ac)| (au, ac)), None, smooth_neighbor_uv, |
| 7467 | ); |
| 7468 | let v_default_tx = default_intra_tx_type(uv_predict_mode as u8); |
| 7469 | let v_skip_ctx = usize::from(around[2].0) + usize::from(around[2].1); |
| 7470 | let mut v_coding = cdfs.txb(TxbSet::ChromaRect8x4, uv_predict_mode); |
| 7471 | let (v_l, v_tx_type) = read_coeffs_rect( |
| 7472 | dec, &mut v_coding, chroma_scan, chroma_w, chroma_h, v_skip_ctx, |
| 7473 | dc_sign_ctx(around[2].2), v_default_tx, |
| 7474 | )?; |
| 7475 | v_levels = v_l; |
| 7476 | let v_residual = dequant_and_inverse_typed_wh( |
| 7477 | &v_levels, chroma_w, chroma_h, crate::decode::bit_depth(), |
| 7478 | CURRENT_Q_IDX.with(|c| c.get()), plane_q_delta(2).0, plane_q_delta(2).1, v_tx_type, |
| 7479 | ); |
| 7480 | if let Some((_, vb)) = &palette_uv_bufs { |
| 7481 | set_palette_pred(vb.clone()); |
| 7482 | } |
| 7483 | v.reconstruct_rect( |
| 7484 | cpx, cpy, chroma_w, chroma_h, uv_predict_mode, angle_delta_uv, reach, &v_residual, |
| 7485 | alpha.zip(ac.as_deref()).map(|((_, av), ac)| (av, ac)), None, smooth_neighbor_uv, |
| 7486 | ); |
| 7487 | RECT_LEAF_COEFF_HITS.with(|c| c.set(c.get() + 1)); |
| 7488 | } |
| 7489 | neighbours.record_mi_rect(leaf_mi, bw, bh, &[luma_levels, u_levels, v_levels]); |
| 7490 | neighbours.fill_skip_grid_rect(leaf_mi, mi_w, mi_h, skip); |
| 7491 | neighbours.fill_lf_grid_rect(leaf_mi, mi_w, mi_h, bw as u8, bh as u8, 0); |
| 7492 | // The leaf's own palette state (`0` clears, as this site always did |
| 7493 | // while a palette leaf refused -- lane-palette2 r8), written on the SPLIT |
| 7494 | // AXIS only until the pair's closing leaf: the coarse cell holds one |
| 7495 | // entry for both leaves, and leaf 0 zeroing the LEFT one threw away the |
| 7496 | // real left neighbour leaf 1 reads (lane-kf900 r4). |
| 7497 | record_strip_palette( |
| 7498 | neighbours, pal_at, bw, bh, pal_y_size, pal_y_colors, pal_uv_size, pal_uv_colors, |
| 7499 | pal_last, |
| 7500 | ); |
| 7501 | // A rect leaf spans mi_w x mi_h, so its mode reaches several columns and |
| 7502 | // rows -- record the whole span, not one cell. |
| 7503 | neighbours.record_mode_mi(leaf_mi.0, leaf_mi.1, mi_w, mi_h, mode); |
| 7504 | neighbours.record_uv_mode_mi(leaf_mi.0, leaf_mi.1, mi_w, mi_h, uv_predict_mode); |
| 7505 | Ok(mode) |
| 7506 | } |
| 7507 | |
| 7508 | /// Decodes one 32x8 / 8x32 strip of a 32x32-level `PARTITION_HORZ_4` / |
| 7509 | /// `PARTITION_VERT_4` (lane-tx64x16 r3): [`decode_block_rect`]'s 4:1 sibling, |
| 7510 | /// addressed by REAL mi coordinates for the same reason [`decode_leaf_rect`] |
| 7511 | /// is -- strips 1 and 3 start 8 px into a 16-px [`SUB`] cell, which an |
| 7512 | /// `at`-derived `px = c * SUB` cannot name. `prev_strip` carries the previous |
| 7513 | /// strip's mode so this one's mode context sees its true above/left |
| 7514 | /// neighbour inside the same 32x32 (`decode_leaf_rect`'s convention). |
| 7515 | /// |
| 7516 | /// Tables: LUMA `TX_32X8`/`TX_8X32` averages to the 16x16 entropy context |
| 7517 | /// ([`TxbSet::LumaRect32x8`], 256 positions) and scans with |
| 7518 | /// [`SCAN_32X8`]/[`SCAN_8X32`]; CHROMA `TX_16X4`/`TX_4X16` averages to the |
| 7519 | /// 8x8 one ([`TxbSet::Chroma8`], 64 positions) and scans with |
| 7520 | /// [`SCAN_16X4`]/[`SCAN_4X16`]. Neither shape takes the rect sqrt2 scale -- |
| 7521 | /// `get_rect_tx_log_ratio` fires at ratio 1 only, and this is ratio 2 |
| 7522 | /// (`inverse_transform_2d_typed_wh`'s own `rect_scale`). |
| 7523 | /// |
| 7524 | /// A split transform (`tx_depth != 0`) refuses by name: `depth_to_tx_size` of |
| 7525 | /// a 4:1 strip is not simply `bw.min(bh) >> (depth - 1)` the way a 2:1 |
| 7526 | /// strip's is, so [`decode_rect_split`]'s per-unit walk cannot be reused |
| 7527 | /// unexamined. |
| 7528 | #[allow(clippy::too_many_arguments)] |
| 7529 | fn decode_block_rect4( |
| 7530 | dec: &mut SymbolDecoder, |
| 7531 | cdfs: &mut Cdfs, |
| 7532 | neighbours: &mut Neighbours, |
| 7533 | strip_mi: (usize, usize), |
| 7534 | bw: usize, |
| 7535 | bh: usize, |
| 7536 | prev_strip: Option<((usize, usize), usize)>, |
| 7537 | y: &mut PlaneBuf, |
| 7538 | u: &mut PlaneBuf, |
| 7539 | v: &mut PlaneBuf, |
| 7540 | enable_filter_intra: bool, |
| 7541 | allow_screen_content_tools: bool, |
| 7542 | base_q_idx: u8, |
| 7543 | tx_select: bool, |
| 7544 | ) -> Result<(usize, usize)> { |
| 7545 | let _ = base_q_idx; |
| 7546 | let (mi_r, mi_c) = strip_mi; |
| 7547 | let (r, c) = (mi_r / (SUB / MI), mi_c / (SUB / MI)); |
| 7548 | let (mi_w, mi_h) = (bw / MI, bh / MI); |
| 7549 | let mut above_mode = neighbours.above_mode[c]; |
| 7550 | let mut left_mode = neighbours.left_mode[r]; |
| 7551 | if let Some(((pr, pc), pmode)) = prev_strip { |
| 7552 | if pc == mi_c && mi_r == pr + mi_h { |
| 7553 | above_mode = pmode; |
| 7554 | } else if pr == mi_r && mi_c == pc + mi_w { |
| 7555 | left_mode = pmode; |
| 7556 | } |
| 7557 | } |
| 7558 | // lane-rectsplitx r2, ROOT CAUSE of the 1:4 mismatch: mi-exact + |
| 7559 | // availability-correct neighbour modes (see |
| 7560 | // [`Neighbours::modes_above_left_mi`]). The coarse 16x16 bands alias every |
| 7561 | // 8-px strip of a `HORZ_4`/`VERT_4` onto one cell. |
| 7562 | let (above_mode, left_mode) = |
| 7563 | neighbours.modes_above_left_mi(mi_r, mi_c, (above_mode, left_mode)); |
| 7564 | // lane-kf900 r4: this strip's own palette-Y mode ctx and colour cache. |
| 7565 | // Passing `None`/`&[]` read `palette_y_mode` off CDF row 0 and every |
| 7566 | // `palette_colors_y` cache hit off an EMPTY cache -- right symbol count, |
| 7567 | // wrong interval, so a screen-content tile desynced at the first strip |
| 7568 | // whose neighbour used a palette (class [[cdf-row-held-constant]]). |
| 7569 | let pal_at = (mi_r / (SUB / MI), mi_c / (SUB / MI)); |
| 7570 | let (palette_ctx, palette_cache) = neighbours.palette_ctx_and_cache_mi(pal_at, mi_r); |
| 7571 | let palette_uv_cache = neighbours.palette_uv_cache_mi(pal_at, mi_r); |
| 7572 | let ( |
| 7573 | skip, |
| 7574 | mode, |
| 7575 | angle_delta_y, |
| 7576 | uv_mode, |
| 7577 | angle_delta_uv, |
| 7578 | alpha, |
| 7579 | filter_intra, |
| 7580 | palette_y, |
| 7581 | palette_uv, |
| 7582 | ) = read_intra_mode_rect( |
| 7583 | dec, |
| 7584 | cdfs, |
| 7585 | above_mode, |
| 7586 | left_mode, |
| 7587 | true, |
| 7588 | true, |
| 7589 | bw, |
| 7590 | bh, |
| 7591 | enable_filter_intra, |
| 7592 | neighbours.skip_txfm_ctx(mi_r, mi_c), |
| 7593 | allow_screen_content_tools, |
| 7594 | Some((palette_ctx, &palette_cache)), |
| 7595 | &palette_uv_cache, |
| 7596 | mi_r, |
| 7597 | mi_c, |
| 7598 | )?; |
| 7599 | // lane-kf900 r4: palette prediction on this shape, exactly as |
| 7600 | // [`decode_block_rect`] does one aspect ratio up -- the colour-index map |
| 7601 | // mapped through the base colours IS the prediction (it reads no edge |
| 7602 | // pixels), handed to each `reconstruct_rect` through [`set_palette_pred`]. |
| 7603 | if palette_y.is_some() || palette_uv.is_some() { |
| 7604 | RECT4_PALETTE_HITS.with(|c| c.set(c.get() + 1)); |
| 7605 | } |
| 7606 | let palette_y_buf: Option<Vec<u16>> = palette_y |
| 7607 | .as_ref() |
| 7608 | .map(|p| p.map.iter().map(|&i| p.colors[i as usize]).collect()); |
| 7609 | let palette_uv_bufs: Option<(Vec<u16>, Vec<u16>)> = palette_uv.as_ref().map(|p| { |
| 7610 | ( |
| 7611 | p.map.iter().map(|&i| p.u_colors[i as usize]).collect(), |
| 7612 | p.map.iter().map(|&i| p.v_colors[i as usize]).collect(), |
| 7613 | ) |
| 7614 | }); |
| 7615 | // `last`: strip 3 of the parent 32x32's HORZ_4 (VERT_4), the only one |
| 7616 | // whose colours a block outside the parent sees -- see |
| 7617 | // [`record_strip_palette`]. |
| 7618 | let pal_last = if bw > bh { mi_r % 8 == 6 } else { mi_c % 8 == 6 }; |
| 7619 | let (pal_y_size, pal_y_colors) = |
| 7620 | palette_y.as_ref().map_or((0, [0u16; 8]), |p| (p.size, p.colors)); |
| 7621 | let (pal_uv_size, pal_uv_colors) = |
| 7622 | palette_uv.as_ref().map_or((0, [0u16; 8]), |p| (p.size, p.u_colors)); |
| 7623 | let smooth_neighbor = is_smooth_mode(above_mode) || is_smooth_mode(left_mode); |
| 7624 | if smooth_neighbor { |
| 7625 | SMOOTH_LUMA_HITS.with(|c| c.set(c.get() + 1)); |
| 7626 | } |
| 7627 | // lane-rectsplitx r2: this strip's REAL mi, not the [`SUB`] cell's |
| 7628 | // top-left -- a 1:4 strip starts 8 px into a 16-px cell, so |
| 7629 | // `r * (SUB / MI)` names a different mi column and the chroma |
| 7630 | // edge-filter strength was read from the wrong neighbour (measured: |
| 7631 | // seed 43, the last 8x32 strip at luma (152,32), chroma-only mismatch |
| 7632 | // with luma bit-exact). |
| 7633 | let smooth_neighbor_uv = neighbours.smooth_uv_neighbour(mi_r, mi_c, r, c); |
| 7634 | if filter_intra.is_some() { |
| 7635 | FILTER_INTRA_RECT_HITS.with(|c| c.set(c.get() + 1)); |
| 7636 | if bw > bh { |
| 7637 | FILTER_INTRA_RECT4_HORZ_HITS.with(|c| c.set(c.get() + 1)); |
| 7638 | } else { |
| 7639 | FILTER_INTRA_RECT4_VERT_HITS.with(|c| c.set(c.get() + 1)); |
| 7640 | } |
| 7641 | } |
| 7642 | let uv_predict_mode = if uv_mode == UV_CFL_PRED { |
| 7643 | DC_PRED |
| 7644 | } else { |
| 7645 | uv_mode |
| 7646 | }; |
| 7647 | let depth = if tx_select { |
| 7648 | let ctx = tx_size_context_rect(neighbours, (mi_r, mi_c), bw, bh); |
| 7649 | dec.symbol(&mut cdfs.tx_size_cat2[ctx]) |
| 7650 | } else { |
| 7651 | 0 |
| 7652 | }; |
| 7653 | if depth != 0 { |
| 7654 | // lane-rectsplitx r1: the per-transform-unit walk, at this strip's |
| 7655 | // real mi position (strips 1 and 3 of a HORZ_4 start 8 px into a |
| 7656 | // 16-px [`SUB`] cell). depth 1 is the RECT unit TX_16X8/TX_8X16 |
| 7657 | // (`sub_tx_size_map[TX_32X8] == TX_16X8`), depth 2 the square TX_8X8; |
| 7658 | // both interleave in every real stream. |
| 7659 | TX_DEPTH_HITS.with(|c| c.set(c.get() + 1)); |
| 7660 | let (tx_w, tx_h) = depth_to_tx_wh(bw, bh, depth); |
| 7661 | // lane-rectsplitx r2: both depths ship together, as the r1 handoff |
| 7662 | // predicted -- depth 1 fires FIRST in every attempt that splits at |
| 7663 | // all, so a depth-2-only lift could never be gated. The r1 mismatch |
| 7664 | // was never the rect unit: it was the neighbour MODE the strip reads |
| 7665 | // (see `modes_above_left_mi`). |
| 7666 | let which = if depth == 1 { |
| 7667 | &RECT4_SPLIT_DEPTH1_HITS |
| 7668 | } else { |
| 7669 | &RECT4_SPLIT_DEPTH2_HITS |
| 7670 | }; |
| 7671 | which.with(|c| c.set(c.get() + 1)); |
| 7672 | let modes = RectStripModes { |
| 7673 | skip, |
| 7674 | mode, |
| 7675 | angle_delta_y, |
| 7676 | uv_predict_mode, |
| 7677 | angle_delta_uv, |
| 7678 | alpha, |
| 7679 | filter_intra, |
| 7680 | smooth_neighbor, |
| 7681 | smooth_neighbor_uv, |
| 7682 | palette_y: palette_y_buf.clone(), |
| 7683 | palette_uv: palette_uv_bufs.clone(), |
| 7684 | }; |
| 7685 | if palette_y.is_some() || palette_uv.is_some() { |
| 7686 | PALETTE_SPLIT_TX_HITS.with(|c| c.set(c.get() + 1)); |
| 7687 | } |
| 7688 | decode_rect_split( |
| 7689 | dec, |
| 7690 | cdfs, |
| 7691 | neighbours, |
| 7692 | (mi_r, mi_c), |
| 7693 | bw, |
| 7694 | bh, |
| 7695 | tx_w, |
| 7696 | tx_h, |
| 7697 | &modes, |
| 7698 | y, |
| 7699 | u, |
| 7700 | v, |
| 7701 | base_q_idx, |
| 7702 | false, |
| 7703 | )?; |
| 7704 | record_strip_palette( |
| 7705 | neighbours, pal_at, bw, bh, pal_y_size, pal_y_colors, pal_uv_size, |
| 7706 | pal_uv_colors, pal_last, |
| 7707 | ); |
| 7708 | if bw > bh { |
| 7709 | RECT4_32_HORZ_HITS.with(|c| c.set(c.get() + 1)); |
| 7710 | } else { |
| 7711 | RECT4_32_VERT_HITS.with(|c| c.set(c.get() + 1)); |
| 7712 | } |
| 7713 | if !skip { |
| 7714 | RECT4_COEFF_HITS.with(|c| c.set(c.get() + 1)); |
| 7715 | } |
| 7716 | return Ok((mode, uv_predict_mode)); |
| 7717 | } |
| 7718 | // The coarse (16-px [`SUB`] cell) mode/side arrays cannot name a strip |
| 7719 | // that is 8 px tall, so each strip stamps every cell it touches and the |
| 7720 | // LAST strip wins -- which is the right answer for the block below/right |
| 7721 | // of the 32x32, the only reader outside it. Written after this strip's |
| 7722 | // own context read above, never before. |
| 7723 | for cell in 0..bw.div_ceil(SUB) { |
| 7724 | neighbours.above_mode[c + cell] = mode; |
| 7725 | neighbours.above_uv_mode[c + cell] = uv_predict_mode; |
| 7726 | neighbours.above_side[c + cell] = bw; |
| 7727 | } |
| 7728 | for cell in 0..bh.div_ceil(SUB) { |
| 7729 | neighbours.left_mode[r + cell] = mode; |
| 7730 | neighbours.left_uv_mode[r + cell] = uv_predict_mode; |
| 7731 | neighbours.left_side[r + cell] = bh; |
| 7732 | } |
| 7733 | // ...and the mi-EXACT maps the coarse cells alias away, exactly as |
| 7734 | // [`decode_leaf_rect`] does (the split arm above records these through |
| 7735 | // `record_mi_luma_rect`). Without this an unsplit 32x8 strip left |
| 7736 | // `sub8_mode_row` holding the previous block, and the next 32x32's |
| 7737 | // strips read a stale `kf_y_mode` row (measured: mi(4,8) on the band |
| 7738 | // fixture). |
| 7739 | neighbours.record_mode_mi(mi_r, mi_c, mi_w, mi_h, mode); |
| 7740 | neighbours.record_uv_mode_mi(mi_r, mi_c, mi_w, mi_h, uv_predict_mode); |
| 7741 | let (px, py) = (mi_c * MI, mi_r * MI); |
| 7742 | let (cpx, cpy) = (px / 2, py / 2); |
| 7743 | let (chroma_w, chroma_h) = (bw / 2, bh / 2); |
| 7744 | let reach = Reach::of_rect(bw, bh, px, py, y.width, y.height); |
| 7745 | let (luma_scan, chroma_scan): (&[u16], &[u16]) = if bw > bh { |
| 7746 | (&SCAN_32X8, &SCAN_16X4) |
| 7747 | } else { |
| 7748 | (&SCAN_8X32, &SCAN_4X16) |
| 7749 | }; |
| 7750 | let (luma_levels, u_levels, v_levels) = if skip { |
| 7751 | ( |
| 7752 | vec![0i32; bw * bh], |
| 7753 | vec![0i32; chroma_w * chroma_h], |
| 7754 | vec![0i32; chroma_w * chroma_h], |
| 7755 | ) |
| 7756 | } else { |
| 7757 | let around = neighbours.around_mi_rect((mi_r, mi_c), bw, bh); |
| 7758 | let mut luma_coding = cdfs.txb(TxbSet::LumaRect32x8, mode); |
| 7759 | let (luma_levels, luma_tx_type) = read_coeffs_rect( |
| 7760 | dec, |
| 7761 | &mut luma_coding, |
| 7762 | luma_scan, |
| 7763 | bw, |
| 7764 | bh, |
| 7765 | 0, |
| 7766 | dc_sign_ctx(around[0].2), |
| 7767 | TxType::DctDct, |
| 7768 | )?; |
| 7769 | let mut planes = Vec::with_capacity(2); |
| 7770 | for plane in 1..3 { |
| 7771 | let default_tx = default_intra_tx_type(uv_predict_mode as u8); |
| 7772 | let skip_ctx = usize::from(around[plane].0) + usize::from(around[plane].1); |
| 7773 | let mut coding = cdfs.txb(TxbSet::Chroma8, uv_predict_mode); |
| 7774 | let (levels, tx_type) = read_coeffs_rect( |
| 7775 | dec, |
| 7776 | &mut coding, |
| 7777 | chroma_scan, |
| 7778 | chroma_w, |
| 7779 | chroma_h, |
| 7780 | skip_ctx, |
| 7781 | dc_sign_ctx(around[plane].2), |
| 7782 | default_tx, |
| 7783 | )?; |
| 7784 | planes.push((levels, tx_type)); |
| 7785 | } |
| 7786 | let (v_levels, v_tx_type) = planes.pop().expect("plane 2"); |
| 7787 | let (u_levels, u_tx_type) = planes.pop().expect("plane 1"); |
| 7788 | RECT4_COEFF_HITS.with(|c| c.set(c.get() + 1)); |
| 7789 | let luma_residual = dequant_and_inverse_typed_wh( |
| 7790 | &luma_levels, |
| 7791 | bw, |
| 7792 | bh, |
| 7793 | crate::decode::bit_depth(), |
| 7794 | block_q_idx(), |
| 7795 | plane_q_delta(0).0, |
| 7796 | plane_q_delta(0).1, |
| 7797 | luma_tx_type, |
| 7798 | ); |
| 7799 | if let Some(b) = &palette_y_buf { |
| 7800 | set_palette_pred(b.clone()); |
| 7801 | } |
| 7802 | y.reconstruct_rect( |
| 7803 | px, |
| 7804 | py, |
| 7805 | bw, |
| 7806 | bh, |
| 7807 | mode, |
| 7808 | angle_delta_y, |
| 7809 | reach, |
| 7810 | &luma_residual, |
| 7811 | None, |
| 7812 | filter_intra, |
| 7813 | smooth_neighbor, |
| 7814 | ); |
| 7815 | let ac = alpha.map(|_| cfl_ac_q3_rect(y, px, py, bw, bh)); |
| 7816 | for (plane, buf, levels, tx_type) in [ |
| 7817 | (1usize, &mut *u, &u_levels, u_tx_type), |
| 7818 | (2, &mut *v, &v_levels, v_tx_type), |
| 7819 | ] { |
| 7820 | let residual = dequant_and_inverse_typed_wh( |
| 7821 | levels, |
| 7822 | chroma_w, |
| 7823 | chroma_h, |
| 7824 | crate::decode::bit_depth(), |
| 7825 | block_q_idx(), |
| 7826 | plane_q_delta(plane).0, |
| 7827 | plane_q_delta(plane).1, |
| 7828 | tx_type, |
| 7829 | ); |
| 7830 | if let Some((ub, vb)) = &palette_uv_bufs { |
| 7831 | set_palette_pred(if plane == 1 { ub.clone() } else { vb.clone() }); |
| 7832 | } |
| 7833 | buf.reconstruct_rect( |
| 7834 | cpx, |
| 7835 | cpy, |
| 7836 | chroma_w, |
| 7837 | chroma_h, |
| 7838 | uv_predict_mode, |
| 7839 | angle_delta_uv, |
| 7840 | reach, |
| 7841 | &residual, |
| 7842 | alpha |
| 7843 | .zip(ac.as_deref()) |
| 7844 | .map(|((au, av), ac)| (if plane == 1 { au } else { av }, ac)), |
| 7845 | None, |
| 7846 | smooth_neighbor_uv, |
| 7847 | ); |
| 7848 | } |
| 7849 | neighbours.record_mi_rect( |
| 7850 | (mi_r, mi_c), |
| 7851 | bw, |
| 7852 | bh, |
| 7853 | &[luma_levels, u_levels, v_levels], |
| 7854 | ); |
| 7855 | neighbours.fill_skip_grid_rect((mi_r, mi_c), mi_w, mi_h, skip); |
| 7856 | neighbours.fill_lf_grid_rect((mi_r, mi_c), mi_w, mi_h, bw as u8, bh as u8, 0); |
| 7857 | record_strip_palette( |
| 7858 | neighbours, pal_at, bw, bh, pal_y_size, pal_y_colors, pal_uv_size, |
| 7859 | pal_uv_colors, pal_last, |
| 7860 | ); |
| 7861 | if bw > bh { |
| 7862 | RECT4_32_HORZ_HITS.with(|c| c.set(c.get() + 1)); |
| 7863 | } else { |
| 7864 | RECT4_32_VERT_HITS.with(|c| c.set(c.get() + 1)); |
| 7865 | } |
| 7866 | return Ok((mode, uv_predict_mode)); |
| 7867 | }; |
| 7868 | if let Some(b) = &palette_y_buf { |
| 7869 | set_palette_pred(b.clone()); |
| 7870 | } |
| 7871 | y.reconstruct_rect( |
| 7872 | px, |
| 7873 | py, |
| 7874 | bw, |
| 7875 | bh, |
| 7876 | mode, |
| 7877 | angle_delta_y, |
| 7878 | reach, |
| 7879 | &luma_levels, |
| 7880 | None, |
| 7881 | filter_intra, |
| 7882 | smooth_neighbor, |
| 7883 | ); |
| 7884 | let ac = alpha.map(|_| cfl_ac_q3_rect(y, px, py, bw, bh)); |
| 7885 | if let Some((ub, _)) = &palette_uv_bufs { |
| 7886 | set_palette_pred(ub.clone()); |
| 7887 | } |
| 7888 | u.reconstruct_rect( |
| 7889 | cpx, |
| 7890 | cpy, |
| 7891 | chroma_w, |
| 7892 | chroma_h, |
| 7893 | uv_predict_mode, |
| 7894 | angle_delta_uv, |
| 7895 | reach, |
| 7896 | &u_levels, |
| 7897 | alpha.zip(ac.as_deref()).map(|((au, _), ac)| (au, ac)), |
| 7898 | None, |
| 7899 | smooth_neighbor_uv, |
| 7900 | ); |
| 7901 | if let Some((_, vb)) = &palette_uv_bufs { |
| 7902 | set_palette_pred(vb.clone()); |
| 7903 | } |
| 7904 | v.reconstruct_rect( |
| 7905 | cpx, |
| 7906 | cpy, |
| 7907 | chroma_w, |
| 7908 | chroma_h, |
| 7909 | uv_predict_mode, |
| 7910 | angle_delta_uv, |
| 7911 | reach, |
| 7912 | &v_levels, |
| 7913 | alpha.zip(ac.as_deref()).map(|((_, av), ac)| (av, ac)), |
| 7914 | None, |
| 7915 | smooth_neighbor_uv, |
| 7916 | ); |
| 7917 | neighbours.record_mi_rect((mi_r, mi_c), bw, bh, &[luma_levels.to_vec(), u_levels, v_levels]); |
| 7918 | neighbours.fill_skip_grid_rect((mi_r, mi_c), mi_w, mi_h, skip); |
| 7919 | neighbours.fill_lf_grid_rect((mi_r, mi_c), mi_w, mi_h, bw as u8, bh as u8, 0); |
| 7920 | record_strip_palette( |
| 7921 | neighbours, pal_at, bw, bh, pal_y_size, pal_y_colors, pal_uv_size, pal_uv_colors, |
| 7922 | pal_last, |
| 7923 | ); |
| 7924 | if bw > bh { |
| 7925 | RECT4_32_HORZ_HITS.with(|c| c.set(c.get() + 1)); |
| 7926 | } else { |
| 7927 | RECT4_32_VERT_HITS.with(|c| c.set(c.get() + 1)); |
| 7928 | } |
| 7929 | Ok((mode, uv_predict_mode)) |
| 7930 | } |
| 7931 | |
| 7932 | /// Decodes the four 16x4 / 4x16 strips of a 16x16-level `PARTITION_HORZ_4` / |
| 7933 | /// `PARTITION_VERT_4` (lane-r14) -- [`decode_block_rect4`]'s pair one level |
| 7934 | /// down, and the first shape here whose luma block is a single mi row (or |
| 7935 | /// column). |
| 7936 | /// |
| 7937 | /// What is new at this size is CHROMA. `is_chroma_reference` |
| 7938 | /// (`av1_common_int.h`) is false for a 4:2:0 block one mi tall (wide) on an |
| 7939 | /// EVEN mi row (column), so strips 0 and 2 read no `uv_mode`/`cfl_alpha`/ |
| 7940 | /// `angle_delta_uv` at all and code no chroma residual; strips 1 and 3 close |
| 7941 | /// the pair and carry the chroma of the whole 16x8 (8x16) luma pair -- |
| 7942 | /// `setup_pred_plane`'s own `if (subsampling_y && (mi_row & 1) && |
| 7943 | /// mi_size_high[bsize] == 1) mi_row -= 1` offset, i.e. an 8x4 (4x8) chroma |
| 7944 | /// transform at the PAIR's origin, with CfL averaging over the pair's luma. |
| 7945 | /// |
| 7946 | /// Tables: LUMA `TX_16X4`/`TX_4X16` averages to the 8x8 entropy context |
| 7947 | /// ((0 + 2 + 1) >> 1 = 1) with 64 coded positions, so its CDF set is |
| 7948 | /// [`TxbSet::LumaRect16x4`] (`Luma8`'s tables, the `TX_4X4` `tx_type` row); |
| 7949 | /// scans are [`SCAN_16X4`]/[`SCAN_4X16`]. CHROMA `TX_8X4`/`TX_4X8` is |
| 7950 | /// [`TxbSet::ChromaRect8x4`] with [`SCAN_8X4`]/[`SCAN_4X8`]. Angle deltas ARE |
| 7951 | /// coded (`av1_use_angle_delta` compares the BLOCK_SIZES_ALL enum index, and |
| 7952 | /// BLOCK_16X4 is 17), and so is `use_filter_intra` (both sides <= 32) off this |
| 7953 | /// shape's own CDF row. |
| 7954 | /// |
| 7955 | /// A split transform (`tx_depth != 0`, `sub_tx_size_map[TX_16X4] == TX_8X4`) |
| 7956 | /// refuses by name rather than guess-decode. |
| 7957 | #[allow(clippy::too_many_arguments)] |
| 7958 | fn decode_rect4_16( |
| 7959 | dec: &mut SymbolDecoder, |
| 7960 | cdfs: &mut Cdfs, |
| 7961 | neighbours: &mut Neighbours, |
| 7962 | at16: (usize, usize), |
| 7963 | horz: bool, |
| 7964 | y: &mut PlaneBuf, |
| 7965 | u: &mut PlaneBuf, |
| 7966 | v: &mut PlaneBuf, |
| 7967 | enable_filter_intra: bool, |
| 7968 | allow_screen_content_tools: bool, |
| 7969 | base_q_idx: u8, |
| 7970 | tx_select: bool, |
| 7971 | reduced_tx_set: bool, |
| 7972 | ) -> Result<()> { |
| 7973 | let (r, c) = at16; |
| 7974 | let (mi_row0, mi_col0) = (r * (SUB / MI), c * (SUB / MI)); |
| 7975 | let (bw, bh) = if horz { (16, 4) } else { (4, 16) }; |
| 7976 | let (mi_w, mi_h) = (bw / MI, bh / MI); |
| 7977 | let scan: &[u16] = if horz { &SCAN_16X4 } else { &SCAN_4X16 }; |
| 7978 | let chroma_scan: &[u16] = if horz { &SCAN_8X4 } else { &SCAN_4X8 }; |
| 7979 | let mut prev: Option<((usize, usize), usize)> = None; |
| 7980 | let mut last_mode = DC_PRED; |
| 7981 | let mut last_uv_mode = DC_PRED; |
| 7982 | for i in 0..4usize { |
| 7983 | let lmi = if horz { |
| 7984 | (mi_row0 + i, mi_col0) |
| 7985 | } else { |
| 7986 | (mi_row0, mi_col0 + i) |
| 7987 | }; |
| 7988 | let has_chroma = i % 2 == 1; |
| 7989 | // `decode_block_rect4`'s own neighbour rule: the coarse 16-px cell |
| 7990 | // aliases all four strips onto one entry, so the mi-exact map wins and |
| 7991 | // the previous strip stands in on the split axis. |
| 7992 | let mut above_mode = neighbours.above_mode[c]; |
| 7993 | let mut left_mode = neighbours.left_mode[r]; |
| 7994 | if let Some(((pr, pc), pmode)) = prev { |
| 7995 | if pc == lmi.1 && lmi.0 == pr + mi_h { |
| 7996 | above_mode = pmode; |
| 7997 | } else if pr == lmi.0 && lmi.1 == pc + mi_w { |
| 7998 | left_mode = pmode; |
| 7999 | } |
| 8000 | } |
| 8001 | let (above_mode, left_mode) = |
| 8002 | neighbours.modes_above_left_mi(lmi.0, lmi.1, (above_mode, left_mode)); |
| 8003 | // lane-kf900 r4: the strip's real palette ctx + colour cache (see |
| 8004 | // [`decode_block_rect4`]); `None`/`&[]` here read `palette_y_mode` |
| 8005 | // off CDF row 0 and every cache hit off an empty cache. |
| 8006 | let (palette_ctx, palette_cache) = |
| 8007 | neighbours.palette_ctx_and_cache_mi(at16, lmi.0); |
| 8008 | let palette_uv_cache = neighbours.palette_uv_cache_mi(at16, lmi.0); |
| 8009 | let ( |
| 8010 | skip, |
| 8011 | mode, |
| 8012 | angle_delta_y, |
| 8013 | uv_mode, |
| 8014 | angle_delta_uv, |
| 8015 | alpha, |
| 8016 | filter_intra, |
| 8017 | palette_y, |
| 8018 | palette_uv, |
| 8019 | ) = read_intra_mode_rect( |
| 8020 | dec, |
| 8021 | cdfs, |
| 8022 | above_mode, |
| 8023 | left_mode, |
| 8024 | true, |
| 8025 | has_chroma, |
| 8026 | bw, |
| 8027 | bh, |
| 8028 | enable_filter_intra, |
| 8029 | neighbours.skip_txfm_ctx(lmi.0, lmi.1), |
| 8030 | allow_screen_content_tools, |
| 8031 | Some((palette_ctx, &palette_cache)), |
| 8032 | &palette_uv_cache, |
| 8033 | lmi.0, |
| 8034 | lmi.1, |
| 8035 | )?; |
| 8036 | if palette_y.is_some() || palette_uv.is_some() { |
| 8037 | RECT4_PALETTE_HITS.with(|h| h.set(h.get() + 1)); |
| 8038 | } |
| 8039 | // The colour-index map through the base colours IS the prediction. |
| 8040 | // Chroma's map is the PAIR's 8x4 (4x8) block, matching the one |
| 8041 | // chroma transform this closing strip codes. |
| 8042 | let palette_y_buf: Option<Vec<u16>> = palette_y |
| 8043 | .as_ref() |
| 8044 | .map(|p| p.map.iter().map(|&i| p.colors[i as usize]).collect()); |
| 8045 | let palette_uv_bufs: Option<(Vec<u16>, Vec<u16>)> = palette_uv.as_ref().map(|p| { |
| 8046 | ( |
| 8047 | p.map.iter().map(|&i| p.u_colors[i as usize]).collect(), |
| 8048 | p.map.iter().map(|&i| p.v_colors[i as usize]).collect(), |
| 8049 | ) |
| 8050 | }); |
| 8051 | let smooth_neighbor = is_smooth_mode(above_mode) || is_smooth_mode(left_mode); |
| 8052 | if smooth_neighbor { |
| 8053 | SMOOTH_LUMA_HITS.with(|h| h.set(h.get() + 1)); |
| 8054 | } |
| 8055 | if filter_intra.is_some() { |
| 8056 | FILTER_INTRA_RECT_HITS.with(|h| h.set(h.get() + 1)); |
| 8057 | } |
| 8058 | // `bsize_to_tx_size_cat(BLOCK_16X4)`: `max_txsize_rect_lookup` is |
| 8059 | // TX_16X4 and `sub_tx_size_map` reaches TX_4X4 in two steps, so the |
| 8060 | // depth symbol is the 3-symbol `tx_size_cat1` -- not the 2-symbol |
| 8061 | // `cat0` a 4x8 leaf reads nor the `cat2` a 32x8 strip does. |
| 8062 | let depth = if tx_select { |
| 8063 | let ctx = tx_size_context_rect(neighbours, lmi, bw, bh); |
| 8064 | dec.symbol(&mut cdfs.tx_size_cat1[ctx]) |
| 8065 | } else { |
| 8066 | 0 |
| 8067 | }; |
| 8068 | if depth != 0 { |
| 8069 | TX_DEPTH_HITS.with(|h| h.set(h.get() + 1)); |
| 8070 | RECT4_16_SPLIT_HITS.with(|h| h.set(h.get() + 1)); |
| 8071 | } |
| 8072 | let (tx_w, tx_h) = depth_to_tx_wh(bw, bh, depth); |
| 8073 | let (px, py) = (lmi.1 * MI, lmi.0 * MI); |
| 8074 | let reach = Reach::of_rect(bw, bh, px, py, y.width, y.height); |
| 8075 | neighbours.record_mode_mi(lmi.0, lmi.1, mi_w, mi_h, mode); |
| 8076 | if depth != 0 { |
| 8077 | // [`decode_rect_split`]'s per-unit walk, inlined because the |
| 8078 | // CHROMA of this shape belongs to the strip's 4:2:0 pair, not to |
| 8079 | // the strip (that function codes one chroma transform per block). |
| 8080 | // depth 1 is the 2:1 rect unit TX_8X4/TX_4X8 |
| 8081 | // (`sub_tx_size_map[TX_16X4]`), depth 2 the square TX_4X4; each |
| 8082 | // unit predicts off the units before it in raster order. |
| 8083 | let rect_unit = tx_w != tx_h; |
| 8084 | let zero_tu = vec![0i32; tx_w * tx_h]; |
| 8085 | for tu_row in 0..bh / tx_h { |
| 8086 | for tu_col in 0..bw / tx_w { |
| 8087 | let tu_mi = (lmi.0 + tu_row * (tx_h / MI), lmi.1 + tu_col * (tx_w / MI)); |
| 8088 | let (tu_px, tu_py) = (px + tu_col * tx_w, py + tu_row * tx_h); |
| 8089 | let tu_reach = Reach::of_tu( |
| 8090 | bw, |
| 8091 | bh, |
| 8092 | tu_col * tx_w, |
| 8093 | tu_row * tx_h, |
| 8094 | tx_w, |
| 8095 | tx_h, |
| 8096 | reach, |
| 8097 | ); |
| 8098 | if let Some(b) = &palette_y_buf { |
| 8099 | let tu_buf: Vec<u16> = (0..tx_h) |
| 8100 | .flat_map(|i| { |
| 8101 | let base = (tu_row * tx_h + i) * bw + tu_col * tx_w; |
| 8102 | b[base..base + tx_w].iter().copied() |
| 8103 | }) |
| 8104 | .collect(); |
| 8105 | set_palette_pred(tu_buf); |
| 8106 | } |
| 8107 | if skip { |
| 8108 | y.reconstruct_rect( |
| 8109 | tu_px, tu_py, tx_w, tx_h, mode, angle_delta_y, tu_reach, &zero_tu, |
| 8110 | None, filter_intra, smooth_neighbor, |
| 8111 | ); |
| 8112 | neighbours.record_mi_luma_rect(tu_mi, tx_w, tx_h, &zero_tu); |
| 8113 | } else if rect_unit { |
| 8114 | // `get_txsize_entropy_ctx(TX_8X4)` is the 8x8 class with |
| 8115 | // 32 coded positions and the `TX_4X4` `tx_type` row: |
| 8116 | // exactly [`TxbSet::LumaRect4x8`]. |
| 8117 | let tu_around = neighbours.around_mi_rect(tu_mi, tx_w, tx_h)[0]; |
| 8118 | let tu_skip_ctx = |
| 8119 | neighbours.luma_skip_ctx_rect(tu_mi, tx_w / MI, tx_h / MI); |
| 8120 | let set = if reduced_tx_set { |
| 8121 | TxbSet::LumaRect4x8 |
| 8122 | } else { |
| 8123 | TxbSet::LumaRect4x8Set1 |
| 8124 | }; |
| 8125 | let tu_scan: &[u16] = if tx_w > tx_h { &SCAN_8X4 } else { &SCAN_4X8 }; |
| 8126 | let mut coding = cdfs.txb(set, fi_tx_row(mode, filter_intra)); |
| 8127 | let (tu_grid, tu_tx_type) = read_coeffs_rect( |
| 8128 | dec, |
| 8129 | &mut coding, |
| 8130 | tu_scan, |
| 8131 | tx_w, |
| 8132 | tx_h, |
| 8133 | tu_skip_ctx, |
| 8134 | dc_sign_ctx(tu_around.2), |
| 8135 | TxType::DctDct, |
| 8136 | )?; |
| 8137 | let residual = dequant_and_inverse_typed_wh( |
| 8138 | &tu_grid, |
| 8139 | tx_w, |
| 8140 | tx_h, |
| 8141 | crate::decode::bit_depth(), |
| 8142 | block_q_idx(), |
| 8143 | plane_q_delta(0).0, |
| 8144 | plane_q_delta(0).1, |
| 8145 | tu_tx_type, |
| 8146 | ); |
| 8147 | y.reconstruct_rect( |
| 8148 | tu_px, tu_py, tx_w, tx_h, mode, angle_delta_y, tu_reach, &residual, |
| 8149 | None, filter_intra, smooth_neighbor, |
| 8150 | ); |
| 8151 | if tu_grid.iter().any(|&l| l != 0) { |
| 8152 | RECT4_16_COEFF_HITS.with(|h| h.set(h.get() + 1)); |
| 8153 | } |
| 8154 | neighbours.record_mi_luma_rect(tu_mi, tx_w, tx_h, &tu_grid); |
| 8155 | } else { |
| 8156 | let tu_around = neighbours.around_mi(tu_mi, tx_w)[0]; |
| 8157 | let tu_skip_ctx = neighbours.luma_skip_ctx(tu_mi, tx_w / MI); |
| 8158 | let tu_grid = read_plane( |
| 8159 | dec, |
| 8160 | cdfs, |
| 8161 | txbset_for(tx_w, reduced_tx_set), |
| 8162 | &default_scan(tx_w), |
| 8163 | 0, |
| 8164 | tu_around, |
| 8165 | mode, |
| 8166 | mode, |
| 8167 | angle_delta_y, |
| 8168 | tu_reach, |
| 8169 | y, |
| 8170 | tu_px, |
| 8171 | tu_py, |
| 8172 | tx_w, |
| 8173 | tx_w, |
| 8174 | base_q_idx, |
| 8175 | None, |
| 8176 | filter_intra, |
| 8177 | Some(tu_skip_ctx), |
| 8178 | smooth_neighbor, |
| 8179 | )?; |
| 8180 | if tu_grid.iter().any(|&l| l != 0) { |
| 8181 | RECT4_16_COEFF_HITS.with(|h| h.set(h.get() + 1)); |
| 8182 | } |
| 8183 | neighbours.record_mi_luma(tu_mi, tx_w, &tu_grid); |
| 8184 | } |
| 8185 | } |
| 8186 | } |
| 8187 | } else { |
| 8188 | let levels = if skip { |
| 8189 | let zeros = vec![0i32; bw * bh]; |
| 8190 | if let Some(b) = &palette_y_buf { |
| 8191 | set_palette_pred(b.clone()); |
| 8192 | } |
| 8193 | y.reconstruct_rect( |
| 8194 | px, py, bw, bh, mode, angle_delta_y, reach, &zeros, None, filter_intra, |
| 8195 | smooth_neighbor, |
| 8196 | ); |
| 8197 | zeros |
| 8198 | } else { |
| 8199 | // The strip's luma TU *is* the strip (`plane_bsize == |
| 8200 | // txsize_to_bsize[tx_size]`), so `get_txb_ctx` fixes |
| 8201 | // `txb_skip_ctx` at 0. |
| 8202 | let around = neighbours.around_mi_rect(lmi, bw, bh)[0]; |
| 8203 | let set = if reduced_tx_set { |
| 8204 | TxbSet::LumaRect16x4 |
| 8205 | } else { |
| 8206 | TxbSet::LumaRect16x4Set1 |
| 8207 | }; |
| 8208 | let mut coding = cdfs.txb(set, fi_tx_row(mode, filter_intra)); |
| 8209 | let (levels, tx_type) = read_coeffs_rect( |
| 8210 | dec, |
| 8211 | &mut coding, |
| 8212 | scan, |
| 8213 | bw, |
| 8214 | bh, |
| 8215 | 0, |
| 8216 | dc_sign_ctx(around.2), |
| 8217 | TxType::DctDct, |
| 8218 | )?; |
| 8219 | let residual = dequant_and_inverse_typed_wh( |
| 8220 | &levels, |
| 8221 | bw, |
| 8222 | bh, |
| 8223 | crate::decode::bit_depth(), |
| 8224 | block_q_idx(), |
| 8225 | plane_q_delta(0).0, |
| 8226 | plane_q_delta(0).1, |
| 8227 | tx_type, |
| 8228 | ); |
| 8229 | if let Some(b) = &palette_y_buf { |
| 8230 | set_palette_pred(b.clone()); |
| 8231 | } |
| 8232 | y.reconstruct_rect( |
| 8233 | px, py, bw, bh, mode, angle_delta_y, reach, &residual, None, filter_intra, |
| 8234 | smooth_neighbor, |
| 8235 | ); |
| 8236 | RECT4_16_COEFF_HITS.with(|h| h.set(h.get() + 1)); |
| 8237 | levels |
| 8238 | }; |
| 8239 | neighbours.record_mi_luma_rect(lmi, bw, bh, &levels); |
| 8240 | } |
| 8241 | neighbours.fill_skip_grid_rect(lmi, mi_w, mi_h, skip); |
| 8242 | neighbours.fill_lf_grid_rect(lmi, mi_w, mi_h, tx_w as u8, tx_h as u8, 0); |
| 8243 | // `update_partition_context` writes `partition_context_lookup[ |
| 8244 | // BLOCK_16X4]` over the whole 16x16: full width above, a 4-px side on |
| 8245 | // the left (mirrored under VERT_4). |
| 8246 | for cell in 0..mi_w { |
| 8247 | if let Some(slot) = neighbours.above_side_mi.get_mut(lmi.1 + cell) { |
| 8248 | *slot = bw; |
| 8249 | } |
| 8250 | } |
| 8251 | for cell in 0..mi_h { |
| 8252 | if let Some(slot) = neighbours.left_side_mi.get_mut(lmi.0 + cell) { |
| 8253 | *slot = bh; |
| 8254 | } |
| 8255 | } |
| 8256 | if has_chroma { |
| 8257 | RECT4_16_CHROMA_HITS.with(|h| h.set(h.get() + 1)); |
| 8258 | let pair_mi = if horz { |
| 8259 | (lmi.0 - 1, lmi.1) |
| 8260 | } else { |
| 8261 | (lmi.0, lmi.1 - 1) |
| 8262 | }; |
| 8263 | let (pw, ph) = if horz { (16, 8) } else { (8, 16) }; |
| 8264 | let (ppx, ppy) = (pair_mi.1 * MI, pair_mi.0 * MI); |
| 8265 | let (cpx, cpy) = (ppx / 2, ppy / 2); |
| 8266 | let (cw, ch) = (pw / 2, ph / 2); |
| 8267 | let uv_predict_mode = if uv_mode == UV_CFL_PRED { DC_PRED } else { uv_mode }; |
| 8268 | if (V_PRED..=D67_PRED).contains(&uv_predict_mode) { |
| 8269 | RECT4_16_CHROMA_DIR_HITS.with(|h| h.set(h.get() + 1)); |
| 8270 | } |
| 8271 | let pair_reach = Reach::of_rect(pw, ph, ppx, ppy, y.width, y.height); |
| 8272 | // lane-kf1200 r1: the chroma edge-filter type reads the neighbours |
| 8273 | // of the CHROMA block, which for a 16x4/4x16 pair is `pair_mi`'s |
| 8274 | // 16x8/8x16 region -- libaom builds `chroma_above_mbmi`/ |
| 8275 | // `chroma_left_mbmi` from `mi[-((mi_row & ss_y) * stride + (mi_col |
| 8276 | // & ss_x))]` (`set_mi_offsets`, av1/common/blockd.h ~1050), i.e. |
| 8277 | // the pair's top-left luma mi, never this leaf's odd row/column. |
| 8278 | // Reading `lmi` here named the leaf directly above (a non-chroma |
| 8279 | // -reference 4-row block, `uv_mode == UV_DC_PRED`) and so lost a |
| 8280 | // SMOOTH neighbour: filt_type 0 instead of 1 turned |
| 8281 | // `av1_use_intra_edge_upsample` on (blk_wh 12 <= 16) for a |
| 8282 | // directional chroma strip, moving 4 samples by 1. |
| 8283 | let smooth_neighbor_uv = |
| 8284 | neighbours.smooth_uv_neighbour(pair_mi.0, pair_mi.1, r, c); |
| 8285 | if smooth_neighbor_uv != neighbours.smooth_uv_neighbour_unsnapped(lmi.0, lmi.1, r, c) |
| 8286 | { |
| 8287 | RECT4_16_UV_PAIR_FILT_HITS.with(|h| h.set(h.get() + 1)); |
| 8288 | } |
| 8289 | let (u_levels, v_levels) = if skip { |
| 8290 | let zeros = vec![0i32; cw * ch]; |
| 8291 | // libaom `predict_and_reconstruct_intra_block` (decodeframe.c |
| 8292 | // ~1000) predicts EVERY plane and only guards the residual on |
| 8293 | // `mbmi->skip_txfm`, so a skipped `UV_CFL_PRED` block is still |
| 8294 | // DC + alpha * luma-AC. Dropping the CfL term here left a |
| 8295 | // chroma-only bounded box on ReelC's key frames. |
| 8296 | let ac = alpha.map(|_| cfl_ac_q3_rect(y, ppx, ppy, pw, ph)); |
| 8297 | if alpha.is_some() { |
| 8298 | CHROMA_SKIP_CFL_HITS.with(|h| h.set(h.get() + 1)); |
| 8299 | } |
| 8300 | for (buf, plane) in [(&mut *u, 1usize), (&mut *v, 2)] { |
| 8301 | if let Some((ub, vb)) = &palette_uv_bufs { |
| 8302 | set_palette_pred(if plane == 1 { ub.clone() } else { vb.clone() }); |
| 8303 | } |
| 8304 | buf.reconstruct_rect( |
| 8305 | cpx, |
| 8306 | cpy, |
| 8307 | cw, |
| 8308 | ch, |
| 8309 | uv_predict_mode, |
| 8310 | angle_delta_uv, |
| 8311 | pair_reach, |
| 8312 | &zeros, |
| 8313 | alpha |
| 8314 | .zip(ac.as_deref()) |
| 8315 | .map(|((au, av), ac)| (if plane == 1 { au } else { av }, ac)), |
| 8316 | None, |
| 8317 | smooth_neighbor_uv, |
| 8318 | ); |
| 8319 | } |
| 8320 | (zeros.clone(), zeros) |
| 8321 | } else { |
| 8322 | let around = neighbours.around_mi_rect(pair_mi, pw, ph); |
| 8323 | let mut planes = Vec::with_capacity(2); |
| 8324 | for plane in 1..3 { |
| 8325 | let default_tx = default_intra_tx_type(uv_predict_mode as u8); |
| 8326 | let skip_ctx = usize::from(around[plane].0) + usize::from(around[plane].1); |
| 8327 | let mut coding = cdfs.txb(TxbSet::ChromaRect8x4, uv_predict_mode); |
| 8328 | let (levels, tx_type) = read_coeffs_rect( |
| 8329 | dec, |
| 8330 | &mut coding, |
| 8331 | chroma_scan, |
| 8332 | cw, |
| 8333 | ch, |
| 8334 | skip_ctx, |
| 8335 | dc_sign_ctx(around[plane].2), |
| 8336 | default_tx, |
| 8337 | )?; |
| 8338 | planes.push((levels, tx_type)); |
| 8339 | } |
| 8340 | let (v_levels, v_tx_type) = planes.pop().expect("plane 2"); |
| 8341 | let (u_levels, u_tx_type) = planes.pop().expect("plane 1"); |
| 8342 | // CfL averages the luma of the WHOLE pair, the region this one |
| 8343 | // chroma transform covers. |
| 8344 | let ac = alpha.map(|_| cfl_ac_q3_rect(y, ppx, ppy, pw, ph)); |
| 8345 | for (plane, buf, levels, tx_type) in [ |
| 8346 | (1usize, &mut *u, &u_levels, u_tx_type), |
| 8347 | (2, &mut *v, &v_levels, v_tx_type), |
| 8348 | ] { |
| 8349 | let residual = dequant_and_inverse_typed_wh( |
| 8350 | levels, |
| 8351 | cw, |
| 8352 | ch, |
| 8353 | crate::decode::bit_depth(), |
| 8354 | block_q_idx(), |
| 8355 | plane_q_delta(plane).0, |
| 8356 | plane_q_delta(plane).1, |
| 8357 | tx_type, |
| 8358 | ); |
| 8359 | if let Some((ub, vb)) = &palette_uv_bufs { |
| 8360 | set_palette_pred(if plane == 1 { ub.clone() } else { vb.clone() }); |
| 8361 | } |
| 8362 | buf.reconstruct_rect( |
| 8363 | cpx, |
| 8364 | cpy, |
| 8365 | cw, |
| 8366 | ch, |
| 8367 | uv_predict_mode, |
| 8368 | angle_delta_uv, |
| 8369 | pair_reach, |
| 8370 | &residual, |
| 8371 | alpha |
| 8372 | .zip(ac.as_deref()) |
| 8373 | .map(|((au, av), ac)| (if plane == 1 { au } else { av }, ac)), |
| 8374 | None, |
| 8375 | smooth_neighbor_uv, |
| 8376 | ); |
| 8377 | } |
| 8378 | (u_levels, v_levels) |
| 8379 | }; |
| 8380 | neighbours.record_uv_mode_mi(pair_mi.0, pair_mi.1, pw / MI, ph / MI, uv_predict_mode); |
| 8381 | // The chroma planes' coefficient context, over the PAIR's mi span |
| 8382 | // (`decode_leaf_rect8`'s own rule: a subsampled plane's bound is |
| 8383 | // the luma one rounded up to its 4x4 unit). |
| 8384 | let u_state = neighbour_state(&u_levels); |
| 8385 | let v_state = neighbour_state(&v_levels); |
| 8386 | let round_up_even = |n: usize| n.div_ceil(2) * 2; |
| 8387 | let bound_h = round_up_even(neighbours.mi_rows); |
| 8388 | let bound_w = round_up_even(neighbours.mi_cols); |
| 8389 | for cell in 0..(ph / MI) { |
| 8390 | if pair_mi.0 + cell < bound_h |
| 8391 | && let Some(slot) = neighbours.left.get_mut(pair_mi.0 + cell) |
| 8392 | { |
| 8393 | slot[1] = u_state; |
| 8394 | slot[2] = v_state; |
| 8395 | } |
| 8396 | } |
| 8397 | for cell in 0..(pw / MI) { |
| 8398 | if pair_mi.1 + cell < bound_w |
| 8399 | && let Some(slot) = neighbours.above.get_mut(pair_mi.1 + cell) |
| 8400 | { |
| 8401 | slot[1] = u_state; |
| 8402 | slot[2] = v_state; |
| 8403 | } |
| 8404 | } |
| 8405 | last_uv_mode = uv_predict_mode; |
| 8406 | } |
| 8407 | if horz { |
| 8408 | RECT4_16_HORZ_HITS.with(|h| h.set(h.get() + 1)); |
| 8409 | } else { |
| 8410 | RECT4_16_VERT_HITS.with(|h| h.set(h.get() + 1)); |
| 8411 | } |
| 8412 | let (pal_y_size, pal_y_colors) = |
| 8413 | palette_y.as_ref().map_or((0, [0u16; 8]), |p| (p.size, p.colors)); |
| 8414 | let (pal_uv_size, pal_uv_colors) = |
| 8415 | palette_uv.as_ref().map_or((0, [0u16; 8]), |p| (p.size, p.u_colors)); |
| 8416 | record_strip_palette( |
| 8417 | neighbours, at16, bw, bh, pal_y_size, pal_y_colors, pal_uv_size, pal_uv_colors, |
| 8418 | i == 3, |
| 8419 | ); |
| 8420 | last_mode = mode; |
| 8421 | prev = Some((lmi, mode)); |
| 8422 | } |
| 8423 | // The coarse 16-px cell holds one entry for the whole 16x16: the last |
| 8424 | // strip is what a block below (right) sees through it, the mi-exact maps |
| 8425 | // above answering everything finer. |
| 8426 | neighbours.above_mode[c] = last_mode; |
| 8427 | neighbours.left_mode[r] = last_mode; |
| 8428 | neighbours.above_uv_mode[c] = last_uv_mode; |
| 8429 | neighbours.left_uv_mode[r] = last_uv_mode; |
| 8430 | neighbours.above_side[c] = bw; |
| 8431 | neighbours.left_side[r] = bh; |
| 8432 | Ok(()) |
| 8433 | } |
| 8434 | |
| 8435 | /// Decodes one true `bw`x`bh` superblock-level `PARTITION_HORZ`/ |
| 8436 | /// `PARTITION_VERT` strip (lane-sbpart r2, `bw, bh` one of `(64, 32)` / |
| 8437 | /// `(32, 64)` -- [`decode_block_rect`]'s own sibling one level up). Unlike |
| 8438 | /// that 32x16/16x32 strip, both dimensions here exceed the 32-coefficient |
| 8439 | /// cap, so LUMA needs the same corner truncation [`decode_block`] already |
| 8440 | /// does for a plain 64x64 square (spec 5.11.40): `get_txsize_entropy_ctx` |
| 8441 | /// resolves TX_64X32/TX_32X64 to TX_64X64 regardless of orientation (see |
| 8442 | /// this function's own charter/report), so both strips read a real 32x32 |
| 8443 | /// corner through [`read_coeffs`] with [`TxbSet::Luma64`]/[`SCAN_32`] and |
| 8444 | /// embed it top-left of a zeroed `bw`x`bh` grid, mirroring |
| 8445 | /// [`read_plane`]'s `tx_side != side` branch generalized to non-square. |
| 8446 | /// CHROMA (TX_32X16/TX_16X32) resolves to TX_32X32 -- a real, *untruncated* |
| 8447 | /// `bw/2`x`bh/2` transform via [`read_coeffs_rect`] with |
| 8448 | /// [`TxbSet::ChromaRect32x16`] and the matching [`SCAN_32X16`]/ |
| 8449 | /// [`SCAN_16X32`], same as [`decode_block_rect`]'s own chroma path. Like |
| 8450 | /// that function, only the single-transform-unit (`tx_depth == 0`) case is |
| 8451 | /// supported: a split transform refuses by name rather than guess-decode. |
| 8452 | #[allow(clippy::too_many_arguments)] |
| 8453 | fn decode_block_rect64( |
| 8454 | dec: &mut SymbolDecoder, |
| 8455 | cdfs: &mut Cdfs, |
| 8456 | neighbours: &mut Neighbours, |
| 8457 | at: (usize, usize), |
| 8458 | bw: usize, |
| 8459 | bh: usize, |
| 8460 | y: &mut PlaneBuf, |
| 8461 | u: &mut PlaneBuf, |
| 8462 | v: &mut PlaneBuf, |
| 8463 | enable_filter_intra: bool, |
| 8464 | allow_screen_content_tools: bool, |
| 8465 | base_q_idx: u8, |
| 8466 | tx_select: bool, |
| 8467 | reduced_tx_set: bool, |
| 8468 | ) -> Result<()> { |
| 8469 | let (r, c) = at; |
| 8470 | let (px, py) = (c * SUB, r * SUB); |
| 8471 | let (nb_above_mode, nb_left_mode) = neighbours.modes_above_left(r, c); |
| 8472 | let (palette_ctx, palette_cache) = neighbours.palette_ctx_and_cache(at); |
| 8473 | let palette_uv_cache = neighbours.palette_uv_cache(at); |
| 8474 | let (skip, mode, angle_delta_y, uv_mode, angle_delta_uv, alpha, filter_intra, palette_y, palette_uv) = |
| 8475 | read_intra_mode_rect( |
| 8476 | dec, |
| 8477 | cdfs, |
| 8478 | nb_above_mode, |
| 8479 | nb_left_mode, |
| 8480 | // `is_cfl_allowed` (spec 5.11.5) caps CFL at <=32x32; these |
| 8481 | // superblock-level HORZ/VERT strips are 64x32/32x64, so unlike |
| 8482 | // `decode_block_rect`'s own 32x16/16x32 strips (where `true` is |
| 8483 | // correct), CFL must never be offered here -- passing `true` |
| 8484 | // read `uv_mode` off the 14-symbol `uv_mode_cfl` CDF instead of |
| 8485 | // the real 13-symbol `uv_mode_no_cfl` one and desynced the tile |
| 8486 | // from this block's very first symbol (lane-sbpart r3 bisect). |
| 8487 | false, |
| 8488 | true, |
| 8489 | bw, |
| 8490 | bh, |
| 8491 | enable_filter_intra, |
| 8492 | neighbours.skip_txfm_ctx(r * (SUB / MI), c * (SUB / MI)), |
| 8493 | allow_screen_content_tools, |
| 8494 | Some((palette_ctx, &palette_cache)), |
| 8495 | &palette_uv_cache, |
| 8496 | r * (SUB / MI), |
| 8497 | c * (SUB / MI), |
| 8498 | )?; |
| 8499 | if (palette_y.is_some() || palette_uv.is_some()) && !skip { |
| 8500 | // The luma coefficient path here is the corner-cropped 32x32 real |
| 8501 | // transform embedded in a 64x32/32x64 grid ([`decode_block_rect64`]'s |
| 8502 | // own doc comment) -- a real (non-skip) palette block's residual |
| 8503 | // needs the plain, uncropped rect reader [`decode_block_rect`] already |
| 8504 | // has, not this one's truncation. `skip` (no residual at all) is |
| 8505 | // still supported below (lane-palette2 r4). |
| 8506 | return Err(unsupported( |
| 8507 | "a palette block with a real transform on a superblock-level HORZ/VERT \ |
| 8508 | strip (corner-cropped luma coefficients not ported for palette)", |
| 8509 | )); |
| 8510 | } |
| 8511 | let smooth_neighbor = is_smooth_mode(nb_above_mode) || is_smooth_mode(nb_left_mode); |
| 8512 | if smooth_neighbor { |
| 8513 | SMOOTH_LUMA_HITS.with(|c| c.set(c.get() + 1)); |
| 8514 | } |
| 8515 | let smooth_neighbor_uv = |
| 8516 | neighbours.smooth_uv_neighbour(r * (SUB / MI), c * (SUB / MI), r, c); |
| 8517 | if filter_intra.is_some() { |
| 8518 | // `filter_intra_size_class_rect` already returns `None` for both |
| 8519 | // `(64, 32)` and `(32, 64)` (`av1_filter_intra_allowed_bsize` caps at |
| 8520 | // 32 on both axes), so this symbol is never read at this level -- |
| 8521 | // kept only as the same defensive refusal [`decode_block_rect`] has. |
| 8522 | return Err(unsupported( |
| 8523 | "filter intra on a superblock-level HORZ/VERT strip (never expected -- \ |
| 8524 | av1_filter_intra_allowed_bsize caps at 32x32)", |
| 8525 | )); |
| 8526 | } |
| 8527 | let uv_predict_mode = if uv_mode == UV_CFL_PRED { |
| 8528 | DC_PRED |
| 8529 | } else { |
| 8530 | uv_mode |
| 8531 | }; |
| 8532 | let (mi_r, mi_c) = (r * (SUB / MI), c * (SUB / MI)); |
| 8533 | let depth = if tx_select { |
| 8534 | let ctx = tx_size_context_rect(neighbours, (mi_r, mi_c), bw, bh); |
| 8535 | // lane-rectsplit r1: `bsize_to_tx_size_cat` (libaom `decodemv.c`) |
| 8536 | // counts `sub_tx_size_map` steps from the block's own max rect |
| 8537 | // transform down to TX_4X4, minus one -- BLOCK_64X32's chain |
| 8538 | // (TX_64X32 -> TX_32X32 -> TX_16X16 -> TX_8X8 -> TX_4X4) is four |
| 8539 | // steps, so this level's category is 3 (the same one a 64x64 square |
| 8540 | // block uses), not the 2 a 32x16 strip uses. Reading the cat-2 CDF |
| 8541 | // here was a wrong-table read of a real symbol. |
| 8542 | dec.symbol(&mut cdfs.tx_size_cat3[ctx]) |
| 8543 | } else { |
| 8544 | 0 |
| 8545 | }; |
| 8546 | if depth != 0 { |
| 8547 | TX_DEPTH_HITS.with(|c| c.set(c.get() + 1)); |
| 8548 | // lane-rectsplit r1/r4: the superblock-level strip splits its |
| 8549 | // transform through the very same per-unit path as its 32x32-level |
| 8550 | // sibling; [`depth_to_tx_wh`] names the unit, which for a 64x16/16x64 |
| 8551 | // 1:4 strip is the 2:1 rect TX_32X16 at depth 1 (lane-rectchroma r1 |
| 8552 | // measured the old square formula mis-tiling exactly that strip). |
| 8553 | let (tx_w, tx_h) = depth_to_tx_wh(bw, bh, depth); |
| 8554 | if bw.max(bh) / bw.min(bh) == 4 { |
| 8555 | let which = if depth == 1 { |
| 8556 | &RECT4_SPLIT_DEPTH1_HITS |
| 8557 | } else { |
| 8558 | &RECT4_SPLIT_DEPTH2_HITS |
| 8559 | }; |
| 8560 | which.with(|c| c.set(c.get() + 1)); |
| 8561 | } |
| 8562 | if std::env::var_os("EC_SBPART_DUMP64").is_some() { |
| 8563 | eprintln!( |
| 8564 | "DUMP64SPLIT mi_r={mi_r} mi_c={mi_c} px={px} py={py} bw={bw} bh={bh} \ |
| 8565 | depth={depth} tx={tx_w}x{tx_h} mode={mode} uv={uv_predict_mode} skip={skip} \ |
| 8566 | angle_y={angle_delta_y}" |
| 8567 | ); |
| 8568 | } |
| 8569 | let modes = RectStripModes { |
| 8570 | skip, |
| 8571 | mode, |
| 8572 | angle_delta_y, |
| 8573 | uv_predict_mode, |
| 8574 | angle_delta_uv, |
| 8575 | alpha, |
| 8576 | filter_intra, |
| 8577 | smooth_neighbor, |
| 8578 | smooth_neighbor_uv, |
| 8579 | palette_y: palette_y |
| 8580 | .as_ref() |
| 8581 | .map(|p| p.map.iter().map(|&i| p.colors[i as usize]).collect()), |
| 8582 | palette_uv: palette_uv.as_ref().map(|p| { |
| 8583 | ( |
| 8584 | p.map.iter().map(|&i| p.u_colors[i as usize]).collect(), |
| 8585 | p.map.iter().map(|&i| p.v_colors[i as usize]).collect(), |
| 8586 | ) |
| 8587 | }), |
| 8588 | }; |
| 8589 | if palette_y.is_some() || palette_uv.is_some() { |
| 8590 | PALETTE_SPLIT_TX_HITS.with(|c| c.set(c.get() + 1)); |
| 8591 | } |
| 8592 | decode_rect_split( |
| 8593 | dec, |
| 8594 | cdfs, |
| 8595 | neighbours, |
| 8596 | (mi_r, mi_c), |
| 8597 | bw, |
| 8598 | bh, |
| 8599 | tx_w, |
| 8600 | tx_h, |
| 8601 | &modes, |
| 8602 | y, |
| 8603 | u, |
| 8604 | v, |
| 8605 | base_q_idx, |
| 8606 | reduced_tx_set, |
| 8607 | )?; |
| 8608 | // Same early-return palette stamp as [`decode_block_rect`]'s split arm |
| 8609 | // (lane-palette2 r10): this return skips the fn's own tail, so a |
| 8610 | // split-tx superblock strip would otherwise leave the above/left |
| 8611 | // palette arrays holding a dead block's colours. |
| 8612 | let (py_size, py_colors) = palette_y.as_ref().map_or((0, [0u16; 8]), |p| (p.size, p.colors)); |
| 8613 | neighbours.record_palette_y_rect(at, bw, bh, py_size, py_colors); |
| 8614 | let (puv_size, puv_colors) = |
| 8615 | palette_uv.as_ref().map_or((0, [0u16; 8]), |p| (p.size, p.u_colors)); |
| 8616 | neighbours.record_palette_uv_rect(at, bw, bh, puv_size, puv_colors); |
| 8617 | RECT_PARTITION_HITS.with(|c| c.set(c.get() + 1)); |
| 8618 | return Ok(()); |
| 8619 | } |
| 8620 | let (tx_w, tx_h) = (bw, bh); |
| 8621 | let (cpx, cpy) = (px / 2, py / 2); |
| 8622 | let (chroma_w, chroma_h) = (bw / 2, bh / 2); |
| 8623 | let reach = Reach::of_rect(bw, bh, px, py, y.width, y.height); |
| 8624 | if let Some(ref pyv) = palette_y { |
| 8625 | let buf: Vec<u16> = pyv.map.iter().map(|&idx| pyv.colors[idx as usize]).collect(); |
| 8626 | set_palette_pred(buf); |
| 8627 | } |
| 8628 | let palette_uv_bufs: Option<(Vec<u16>, Vec<u16>)> = palette_uv.as_ref().map(|puv| { |
| 8629 | ( |
| 8630 | puv.map.iter().map(|&idx| puv.u_colors[idx as usize]).collect(), |
| 8631 | puv.map.iter().map(|&idx| puv.v_colors[idx as usize]).collect(), |
| 8632 | ) |
| 8633 | }); |
| 8634 | if skip { |
| 8635 | y.reconstruct_rect( |
| 8636 | px, |
| 8637 | py, |
| 8638 | bw, |
| 8639 | bh, |
| 8640 | mode, |
| 8641 | angle_delta_y, |
| 8642 | reach, |
| 8643 | &vec![0i32; bw * bh], |
| 8644 | None, |
| 8645 | filter_intra, |
| 8646 | smooth_neighbor, |
| 8647 | ); |
| 8648 | let ac = alpha.map(|_| cfl_ac_q3_rect(y, px, py, bw, bh)); |
| 8649 | if let Some((ub, _)) = &palette_uv_bufs { |
| 8650 | set_palette_pred(ub.clone()); |
| 8651 | } |
| 8652 | u.reconstruct_rect( |
| 8653 | cpx, |
| 8654 | cpy, |
| 8655 | chroma_w, |
| 8656 | chroma_h, |
| 8657 | uv_predict_mode, |
| 8658 | angle_delta_uv, |
| 8659 | reach, |
| 8660 | &vec![0i32; chroma_w * chroma_h], |
| 8661 | alpha.zip(ac.as_deref()).map(|((au, _), ac)| (au, ac)), |
| 8662 | None, |
| 8663 | smooth_neighbor_uv, |
| 8664 | ); |
| 8665 | if let Some((_, vb)) = &palette_uv_bufs { |
| 8666 | set_palette_pred(vb.clone()); |
| 8667 | } |
| 8668 | v.reconstruct_rect( |
| 8669 | cpx, |
| 8670 | cpy, |
| 8671 | chroma_w, |
| 8672 | chroma_h, |
| 8673 | uv_predict_mode, |
| 8674 | angle_delta_uv, |
| 8675 | reach, |
| 8676 | &vec![0i32; chroma_w * chroma_h], |
| 8677 | alpha.zip(ac.as_deref()).map(|((_, av), ac)| (av, ac)), |
| 8678 | None, |
| 8679 | smooth_neighbor_uv, |
| 8680 | ); |
| 8681 | neighbours.record_rect( |
| 8682 | at, |
| 8683 | bw, |
| 8684 | bh, |
| 8685 | mode, |
| 8686 | uv_predict_mode, |
| 8687 | &[ |
| 8688 | vec![0i32; bw * bh], |
| 8689 | vec![0i32; chroma_w * chroma_h], |
| 8690 | vec![0i32; chroma_w * chroma_h], |
| 8691 | ], |
| 8692 | ); |
| 8693 | } else { |
| 8694 | let around = neighbours.around_rect(at, bw, bh); |
| 8695 | // LUMA: the coded corner, embedded top-left of the true bw x bh grid |
| 8696 | // (see this function's own doc comment). A 64-length axis codes only |
| 8697 | // its low 32 coefficients, so the corner is `min(bw, 32)` x |
| 8698 | // `min(bh, 32)`: 32x32 under a 64x32/32x64 HORZ/VERT strip, and |
| 8699 | // 32x16/16x32 under a 64x16/16x64 1:4 strip (lane-tx64x16). |
| 8700 | let (luma_cw, luma_ch) = (bw.min(32), bh.min(32)); |
| 8701 | let scan32 = default_scan(TX32); |
| 8702 | if std::env::var_os("EC_TRACE_COEFF").is_some() { |
| 8703 | let (rng, _) = dec.debug_state(); |
| 8704 | eprintln!("EC_COEFF plane=0 row={mi_r} col={mi_c} tx_size={luma_cw}x{luma_ch} rng={rng}"); |
| 8705 | } |
| 8706 | let (luma_corner, luma_tx_type) = if (luma_cw, luma_ch) == (32, 32) { |
| 8707 | let mut luma_coding = cdfs.txb(TxbSet::Luma64, mode); |
| 8708 | read_coeffs( |
| 8709 | dec, |
| 8710 | &mut luma_coding, |
| 8711 | &scan32, |
| 8712 | 0, |
| 8713 | dc_sign_ctx(around[0].2), |
| 8714 | TxType::DctDct, |
| 8715 | Some((bw, bh)), |
| 8716 | )? |
| 8717 | } else { |
| 8718 | // `av1_get_adjusted_tx_size` (`blockd.h:1361`) maps TX_64X16 -> |
| 8719 | // TX_32X16 and TX_16X64 -> TX_16X32, and `av1_scan_orders` |
| 8720 | // (`scan.c`) gives both the very same `default_scan_32x16` / |
| 8721 | // `default_scan_16x32` the 2:1 sizes use ("Half of the |
| 8722 | // coefficients of tx64 at higher frequencies are set to zeros. So |
| 8723 | // tx32's scan order is used"). `get_txsize_entropy_ctx` is |
| 8724 | // (txsize_sqr + txsize_sqr_up + 1) >> 1 = (TX_16X16 + TX_64X64 + |
| 8725 | // 1) >> 1 = TX_32X32 and `txsize_log2_minus4[TX_64X16]` is 5 (a |
| 8726 | // 512-position eob group) -- BOTH identical to TX_32X16, so the |
| 8727 | // 2:1 CDF set `LumaRect32x16` is bit-exact here and no new table |
| 8728 | // is owed. `av1_nz_map_ctx_offset[TX_64X16]` is literally |
| 8729 | // `av1_nz_map_ctx_offset_32x16` (`txb_common.c:340`), which is |
| 8730 | // what `base_ctx_rect` computes at (32, 16). |
| 8731 | let luma_scan: &[u16] = if bw == 64 { &SCAN_32X16 } else { &SCAN_16X32 }; |
| 8732 | let mut luma_coding = cdfs.txb(TxbSet::LumaRect32x16, mode); |
| 8733 | read_coeffs_rect( |
| 8734 | dec, |
| 8735 | &mut luma_coding, |
| 8736 | luma_scan, |
| 8737 | luma_cw, |
| 8738 | luma_ch, |
| 8739 | 0, |
| 8740 | dc_sign_ctx(around[0].2), |
| 8741 | TxType::DctDct, |
| 8742 | )? |
| 8743 | }; |
| 8744 | if std::env::var_os("EC_TRACE_COEFF").is_some() { |
| 8745 | let (rng, _) = dec.debug_state(); |
| 8746 | eprintln!("EC_COEFF_VAL plane=0 row={mi_r} col={mi_c} rng={rng}"); |
| 8747 | } |
| 8748 | let mut luma_levels = vec![0i32; bw * bh]; |
| 8749 | for row in 0..luma_ch { |
| 8750 | luma_levels[row * bw..][..luma_cw] |
| 8751 | .copy_from_slice(&luma_corner[row * luma_cw..][..luma_cw]); |
| 8752 | } |
| 8753 | { |
| 8754 | let cur = block_q_idx(); |
| 8755 | if cur != i32::from(base_q_idx) { |
| 8756 | RECT64_QIDX_DRIFT_HITS.with(|c| c.set(c.get() + 1)); |
| 8757 | } |
| 8758 | if std::env::var_os("EC_AV1_TRACE").is_some() { |
| 8759 | eprintln!( |
| 8760 | "TRACE rect64_dequant plane=0 base_q_idx={base_q_idx} current_q_idx={cur}" |
| 8761 | ); |
| 8762 | } |
| 8763 | } |
| 8764 | let luma_residual = dequant_and_inverse_typed_wh( |
| 8765 | &luma_levels, |
| 8766 | bw, |
| 8767 | bh, |
| 8768 | crate::decode::bit_depth(), |
| 8769 | block_q_idx(), |
| 8770 | plane_q_delta(0).0, |
| 8771 | plane_q_delta(0).1, |
| 8772 | luma_tx_type, |
| 8773 | ); |
| 8774 | y.reconstruct_rect( |
| 8775 | px, |
| 8776 | py, |
| 8777 | bw, |
| 8778 | bh, |
| 8779 | mode, |
| 8780 | angle_delta_y, |
| 8781 | reach, |
| 8782 | &luma_residual, |
| 8783 | None, |
| 8784 | filter_intra, |
| 8785 | smooth_neighbor, |
| 8786 | ); |
| 8787 | if std::env::var_os("EC_SBPART_DUMP64").is_some() { |
| 8788 | let leftcol: Vec<u16> = if px > 0 { |
| 8789 | (py..py + bh).map(|row| y.data[row * y.width + px - 1]).collect() |
| 8790 | } else { |
| 8791 | vec![] |
| 8792 | }; |
| 8793 | eprintln!( |
| 8794 | "DUMP64 mi_r={mi_r} mi_c={mi_c} px={px} py={py} bw={bw} bh={bh} mode={mode} \ |
| 8795 | skip={skip} eob_nonzero={} row0={:?} leftcol={:?}", |
| 8796 | luma_corner.iter().any(|&v| v != 0), |
| 8797 | &y.data[py * y.width + px..][..bw], |
| 8798 | leftcol, |
| 8799 | ); |
| 8800 | } |
| 8801 | // CHROMA: a real, untruncated chroma_w x chroma_h transform -- no |
| 8802 | // corner crop needed (see doc comment). |
| 8803 | let ac = alpha.map(|_| cfl_ac_q3_rect(y, px, py, bw, bh)); |
| 8804 | // The chroma plane is never truncated (both axes <= 32 after |
| 8805 | // subsampling). Under a 1:4 strip it is a true 32x8/8x32: |
| 8806 | // `get_txsize_entropy_ctx(TX_32X8)` = (TX_8X8 + TX_32X32 + 1) >> 1 = |
| 8807 | // TX_16X16 and `txsize_log2_minus4[TX_32X8]` = 4 (256 positions), |
| 8808 | // both identical to TX_16X16 -- so [`TxbSet::Chroma16`] is the exact |
| 8809 | // set, and only the scan is new (lane-tx64x16). |
| 8810 | let (chroma_set, chroma_scan): (TxbSet, &[u16]) = match (bw, bh) { |
| 8811 | (64, 16) => (TxbSet::Chroma16, &SCAN_32X8), |
| 8812 | (16, 64) => (TxbSet::Chroma16, &SCAN_8X32), |
| 8813 | (64, _) => (TxbSet::ChromaRect32x16, &SCAN_32X16), |
| 8814 | _ => (TxbSet::ChromaRect32x16, &SCAN_16X32), |
| 8815 | }; |
| 8816 | let u_skip_ctx = usize::from(around[1].0) + usize::from(around[1].1); |
| 8817 | let mut u_coding = cdfs.txb(chroma_set, uv_predict_mode); |
| 8818 | if std::env::var_os("EC_TRACE_COEFF").is_some() { |
| 8819 | let (rng, _) = dec.debug_state(); |
| 8820 | eprintln!("EC_COEFF plane=1 row={mi_r} col={mi_c} tx_size=rect32x16 rng={rng}"); |
| 8821 | } |
| 8822 | let (u_levels, u_tx_type) = read_coeffs_rect( |
| 8823 | dec, |
| 8824 | &mut u_coding, |
| 8825 | chroma_scan, |
| 8826 | chroma_w, |
| 8827 | chroma_h, |
| 8828 | u_skip_ctx, |
| 8829 | dc_sign_ctx(around[1].2), |
| 8830 | TxType::DctDct, |
| 8831 | )?; |
| 8832 | if std::env::var_os("EC_TRACE_COEFF").is_some() { |
| 8833 | let (rng, _) = dec.debug_state(); |
| 8834 | eprintln!("EC_COEFF_VAL plane=1 row={mi_r} col={mi_c} rng={rng}"); |
| 8835 | } |
| 8836 | { |
| 8837 | let cur = block_q_idx(); |
| 8838 | if cur != i32::from(base_q_idx) { |
| 8839 | RECT64_QIDX_DRIFT_HITS.with(|c| c.set(c.get() + 1)); |
| 8840 | } |
| 8841 | if std::env::var_os("EC_AV1_TRACE").is_some() { |
| 8842 | eprintln!( |
| 8843 | "TRACE rect64_dequant plane=1 base_q_idx={base_q_idx} current_q_idx={cur}" |
| 8844 | ); |
| 8845 | } |
| 8846 | } |
| 8847 | let u_residual = dequant_and_inverse_typed_wh( |
| 8848 | &u_levels, |
| 8849 | chroma_w, |
| 8850 | chroma_h, |
| 8851 | crate::decode::bit_depth(), |
| 8852 | block_q_idx(), |
| 8853 | plane_q_delta(1).0, |
| 8854 | plane_q_delta(1).1, |
| 8855 | u_tx_type, |
| 8856 | ); |
| 8857 | u.reconstruct_rect( |
| 8858 | cpx, |
| 8859 | cpy, |
| 8860 | chroma_w, |
| 8861 | chroma_h, |
| 8862 | uv_predict_mode, |
| 8863 | angle_delta_uv, |
| 8864 | reach, |
| 8865 | &u_residual, |
| 8866 | alpha.zip(ac.as_deref()).map(|((au, _), ac)| (au, ac)), |
| 8867 | None, |
| 8868 | smooth_neighbor_uv, |
| 8869 | ); |
| 8870 | let v_skip_ctx = usize::from(around[2].0) + usize::from(around[2].1); |
| 8871 | let mut v_coding = cdfs.txb(chroma_set, uv_predict_mode); |
| 8872 | if std::env::var_os("EC_TRACE_COEFF").is_some() { |
| 8873 | let (rng, _) = dec.debug_state(); |
| 8874 | eprintln!("EC_COEFF plane=2 row={mi_r} col={mi_c} tx_size=rect32x16 rng={rng}"); |
| 8875 | } |
| 8876 | let (v_levels, v_tx_type) = read_coeffs_rect( |
| 8877 | dec, |
| 8878 | &mut v_coding, |
| 8879 | chroma_scan, |
| 8880 | chroma_w, |
| 8881 | chroma_h, |
| 8882 | v_skip_ctx, |
| 8883 | dc_sign_ctx(around[2].2), |
| 8884 | TxType::DctDct, |
| 8885 | )?; |
| 8886 | if std::env::var_os("EC_TRACE_COEFF").is_some() { |
| 8887 | let (rng, _) = dec.debug_state(); |
| 8888 | eprintln!("EC_COEFF_VAL plane=2 row={mi_r} col={mi_c} rng={rng}"); |
| 8889 | } |
| 8890 | { |
| 8891 | let cur = block_q_idx(); |
| 8892 | if cur != i32::from(base_q_idx) { |
| 8893 | RECT64_QIDX_DRIFT_HITS.with(|c| c.set(c.get() + 1)); |
| 8894 | } |
| 8895 | if std::env::var_os("EC_AV1_TRACE").is_some() { |
| 8896 | eprintln!( |
| 8897 | "TRACE rect64_dequant plane=2 base_q_idx={base_q_idx} current_q_idx={cur}" |
| 8898 | ); |
| 8899 | } |
| 8900 | } |
| 8901 | let v_residual = dequant_and_inverse_typed_wh( |
| 8902 | &v_levels, |
| 8903 | chroma_w, |
| 8904 | chroma_h, |
| 8905 | crate::decode::bit_depth(), |
| 8906 | block_q_idx(), |
| 8907 | plane_q_delta(2).0, |
| 8908 | plane_q_delta(2).1, |
| 8909 | v_tx_type, |
| 8910 | ); |
| 8911 | v.reconstruct_rect( |
| 8912 | cpx, |
| 8913 | cpy, |
| 8914 | chroma_w, |
| 8915 | chroma_h, |
| 8916 | uv_predict_mode, |
| 8917 | angle_delta_uv, |
| 8918 | reach, |
| 8919 | &v_residual, |
| 8920 | alpha.zip(ac.as_deref()).map(|((_, av), ac)| (av, ac)), |
| 8921 | None, |
| 8922 | smooth_neighbor_uv, |
| 8923 | ); |
| 8924 | neighbours.record_rect(at, bw, bh, mode, uv_predict_mode, &[luma_levels, u_levels, v_levels]); |
| 8925 | RECT_COEFF_HITS.with(|c| c.set(c.get() + 1)); |
| 8926 | } |
| 8927 | let (py_size, py_colors) = palette_y.as_ref().map_or((0, [0u16; 8]), |p| (p.size, p.colors)); |
| 8928 | neighbours.record_palette_y_rect(at, bw, bh, py_size, py_colors); |
| 8929 | let (puv_size, puv_colors) = |
| 8930 | palette_uv.as_ref().map_or((0, [0u16; 8]), |p| (p.size, p.u_colors)); |
| 8931 | neighbours.record_palette_uv_rect(at, bw, bh, puv_size, puv_colors); |
| 8932 | neighbours.fill_skip_grid_rect((mi_r, mi_c), bw / MI, bh / MI, skip); |
| 8933 | neighbours.fill_lf_grid_rect((mi_r, mi_c), bw / MI, bh / MI, tx_w as u8, tx_h as u8, 0); |
| 8934 | SB_RECT_HITS.with(|c| c.set(c.get() + 1)); |
| 8935 | match (bw, bh) { |
| 8936 | (64, 16) => SB_RECT4_HORZ_HITS.with(|c| c.set(c.get() + 1)), |
| 8937 | (16, 64) => SB_RECT4_VERT_HITS.with(|c| c.set(c.get() + 1)), |
| 8938 | _ => {} |
| 8939 | } |
| 8940 | if std::env::var_os("EC_AV1_TRACE").is_some() { |
| 8941 | let (rng, _) = dec.debug_state(); |
| 8942 | eprintln!("TRACE_RECT64_END mi_row={mi_r} mi_col={mi_c} bw={bw} bh={bh} rng={rng}"); |
| 8943 | } |
| 8944 | Ok(()) |
| 8945 | } |
| 8946 | |
| 8947 | /// `read_angle_delta`/`Angle_Delta` (spec 5.11.42 + 9.3): the symbol minus |
| 8948 | /// [`ANGLE_DELTA_ZERO`] gives the signed `-MAX_ANGLE_DELTA..=MAX_ANGLE_DELTA` |
| 8949 | /// delta [`crate::intra::predict`] wants; bumps [`ANGLE_DELTA_HITS`] whenever |
| 8950 | /// it lands away from zero, for gate tests to prove a real stream fired one. |
| 8951 | fn read_angle_delta(dec: &mut SymbolDecoder, cdf: &mut [u16]) -> i32 { |
| 8952 | let symbol = dec.symbol(cdf); |
| 8953 | if symbol != ANGLE_DELTA_ZERO { |
| 8954 | ANGLE_DELTA_HITS.with(|c| c.set(c.get() + 1)); |
| 8955 | } |
| 8956 | symbol as i32 - ANGLE_DELTA_ZERO as i32 |
| 8957 | } |
| 8958 | |
| 8959 | #[allow(clippy::too_many_arguments)] |
| 8960 | fn read_intra_mode( |
| 8961 | dec: &mut SymbolDecoder, |
| 8962 | cdfs: &mut Cdfs, |
| 8963 | above_mode: usize, |
| 8964 | left_mode: usize, |
| 8965 | cfl: bool, |
| 8966 | side: usize, |
| 8967 | enable_filter_intra: bool, |
| 8968 | skip_ctx: usize, |
| 8969 | allow_screen_content_tools: bool, |
| 8970 | allow_intrabc: bool, |
| 8971 | // `Some((palette_mode_ctx, color_cache))` at the call sites this lane's |
| 8972 | // scope actually reconstructs a palette-Y block for (`av1_get_palette_mode_ctx` |
| 8973 | // and `av1_get_palette_cache`'s neighbour lookup, [`Neighbours::palette_ctx_and_cache`]); |
| 8974 | // `None` at the excluded call sites (the 8x8-leaf path), which still read |
| 8975 | // the `palette_y_mode` symbol at ctx 0 -- matching the bits a real |
| 8976 | // decoder reads either way -- but refuse by name the moment it fires, |
| 8977 | // exactly the old behaviour. |
| 8978 | palette: Option<(usize, &[u16])>, |
| 8979 | // The U-channel colour cache for a chroma palette read ([`Neighbours::palette_uv_cache`]), |
| 8980 | // `&[]` at the excluded call sites -- same corner-cut as `palette` above, |
| 8981 | // no `mode_ctx` needed (`palette_uv_mode_ctx` is this block's own just-read |
| 8982 | // `use_palette_y`, not a neighbour lookup). |
| 8983 | palette_uv_cache: &[u16], |
| 8984 | mi_r: usize, |
| 8985 | mi_c: usize, |
| 8986 | ) -> Result<( |
| 8987 | bool, |
| 8988 | usize, |
| 8989 | i32, |
| 8990 | usize, |
| 8991 | i32, |
| 8992 | Option<(i32, i32)>, |
| 8993 | Option<usize>, |
| 8994 | Option<PaletteY>, |
| 8995 | Option<PaletteUv>, |
| 8996 | )> { |
| 8997 | let trace = std::env::var_os("EC_AV1_TRACE").is_some(); |
| 8998 | // lane-tiny r2: EC_ISTEP-format trace (same "name=... val=... rng=..." |
| 8999 | // shape the oracle's own `ec_read_intra_frame_mode_info_impl` prints |
| 9000 | // under `EC_TRACE_MODE_STEP`) so a range ladder can be diffed line for |
| 9001 | // line against the instrumented aomdec, not just `tell()`. |
| 9002 | let ec_istep = std::env::var_os("EC_TRACE_MODE_STEP").is_some(); |
| 9003 | if ec_istep { |
| 9004 | // Mirrors the oracle's own `EC_IMODE mi_row=.. mi_col=.. rng=..` line |
| 9005 | // (decodemv.c:818), printed at the same point: before the first |
| 9006 | // mode-info symbol of the block, so a cross-decoder range ladder can |
| 9007 | // be keyed on the BLOCK, not just on the symbol sequence. |
| 9008 | eprintln!("EC_IMODE mi_row={mi_r} mi_col={mi_c} fn=sq side={side} rng={}", dec.debug_state().0); |
| 9009 | } |
| 9010 | macro_rules! istep { |
| 9011 | ($name:literal, $val:expr) => { |
| 9012 | if ec_istep { |
| 9013 | let (rng, _) = dec.debug_state(); |
| 9014 | eprintln!( |
| 9015 | "EC_ISTEP mi_row={mi_r} mi_col={mi_c} name={} val={} rng={rng}", |
| 9016 | $name, $val |
| 9017 | ); |
| 9018 | } |
| 9019 | }; |
| 9020 | } |
| 9021 | // lane-sbrect10 r1: `is_cfl_allowed`'s <=32 bound, enforced at the single |
| 9022 | // point every square caller routes through (see `read_intra_mode_rect`). |
| 9023 | let cfl = if cfl && side > 32 { |
| 9024 | NOCFL_UV_MODE_HITS.with(|c| c.set(c.get() + 1)); |
| 9025 | false |
| 9026 | } else { |
| 9027 | cfl |
| 9028 | }; |
| 9029 | // spec 5.11.6 `intra_frame_mode_info`: `intra_segment_id` is read BEFORE |
| 9030 | // `skip` when `SegIdPreSkip`, and after it otherwise. |
| 9031 | let (seg_w_mi, seg_h_mi) = (side / 4, side / 4); |
| 9032 | if seg_id_pre_skip() { |
| 9033 | intra_segment_id(dec, cdfs, mi_r, mi_c, seg_w_mi, seg_h_mi, false); |
| 9034 | } |
| 9035 | let skip = dec.symbol(&mut cdfs.skip[skip_ctx]) != 0; |
| 9036 | if trace { |
| 9037 | eprintln!("TRACE skip value={} rng={}", skip as i32, dec.debug_state().0); |
| 9038 | } |
| 9039 | istep!("skip", skip as i32); |
| 9040 | if !seg_id_pre_skip() { |
| 9041 | intra_segment_id(dec, cdfs, mi_r, mi_c, seg_w_mi, seg_h_mi, skip); |
| 9042 | } |
| 9043 | // spec order (see the comment below on `read_intrabc_info`): `skip`, |
| 9044 | // `segment_id`, `cdef`, `delta_q` -- `cdef` lands right here. |
| 9045 | maybe_read_cdef_idx(dec, mi_r, mi_c, skip); |
| 9046 | istep!("cdef", 0); |
| 9047 | // lane-sb128b r1: "this leaf IS the superblock" is `side == sb_size`, not |
| 9048 | // a hardcoded 64 -- a `PARTITION_NONE` 128x128 root is the whole |
| 9049 | // superblock and skips `delta_q` exactly like a 64 one under a 64 root. |
| 9050 | let whole_sb = side == sb_mi_cur() as usize * 4; |
| 9051 | maybe_read_delta_q(dec, cdfs, mi_r, mi_c, whole_sb, skip); |
| 9052 | maybe_read_delta_lf(dec, cdfs, mi_r, mi_c, whole_sb, skip); |
| 9053 | istep!("dq", 0); |
| 9054 | // `read_intrabc_info` (spec 5.11.13, libaom decodemv.c:693, called at |
| 9055 | // :811 right after `skip_txfm`/`segment_id`/`cdef`/`delta_q` and before |
| 9056 | // `mbmi->mode` is ever read): a `use_intrabc` symbol only present on an |
| 9057 | // intra frame whose header set `allow_intrabc`. Actual intra block copy |
| 9058 | // needs `assign_dv`'s own motion-vector-prediction machinery (`av1_find_mv_refs`, |
| 9059 | // `av1_find_ref_dv`) this decoder does not carry -- lane-screen scope |
| 9060 | // draws the line at consuming this one flag symbol and refusing by name |
| 9061 | // when it fires, so the arithmetic decoder still resyncs for a stream |
| 9062 | // that merely allows (but never uses) intrabc. |
| 9063 | if allow_intrabc { |
| 9064 | let use_intrabc = dec.symbol(&mut cdfs.intrabc) != 0; |
| 9065 | if trace { |
| 9066 | eprintln!("TRACE use_intrabc value={}", use_intrabc as i32); |
| 9067 | } |
| 9068 | if use_intrabc { |
| 9069 | let dv = read_intrabc_dv(dec, cdfs, mi_r, mi_c, side); |
| 9070 | if trace { |
| 9071 | eprintln!("TRACE intrabc dv={dv:?}"); |
| 9072 | } |
| 9073 | INTRABC_DV.with(|c| c.set(Some(dv))); |
| 9074 | INTRABC_HITS.with(|c| c.set(c.get() + 1)); |
| 9075 | // spec 5.11.13 / libaom `read_intra_frame_mode_info` returns the |
| 9076 | // moment `is_intrabc_block`: no y/uv mode, angle delta, palette, |
| 9077 | // CFL or filter-intra syntax follows. `YMode`/`UVMode` are forced |
| 9078 | // `DC_PRED` (which is also what the neighbour mode contexts and |
| 9079 | // the loop filter then see). |
| 9080 | return Ok((skip, DC_PRED, 0, DC_PRED, 0, None, None, None, None)); |
| 9081 | } |
| 9082 | } |
| 9083 | let above_ctx = INTRA_MODE_CTX[above_mode]; |
| 9084 | let left_ctx = INTRA_MODE_CTX[left_mode]; |
| 9085 | let mode = dec.symbol(&mut cdfs.kf_y_mode[above_ctx][left_ctx]); |
| 9086 | if trace { |
| 9087 | eprintln!("TRACE y_mode ctx=({above_ctx},{left_ctx}) value={mode} rng={}", dec.debug_state().0); |
| 9088 | } |
| 9089 | istep!("mode", mode as i32); |
| 9090 | let angle_delta_y = if (V_PRED..=D67_PRED).contains(&mode) { |
| 9091 | let delta = read_angle_delta(dec, &mut cdfs.angle_delta[mode - V_PRED]); |
| 9092 | if trace { |
| 9093 | eprintln!( |
| 9094 | "TRACE angle_delta value={}", |
| 9095 | delta + ANGLE_DELTA_ZERO as i32 |
| 9096 | ); |
| 9097 | } |
| 9098 | delta |
| 9099 | } else { |
| 9100 | 0 |
| 9101 | }; |
| 9102 | istep!("angle_y", angle_delta_y); |
| 9103 | let uv_mode = if cfl { |
| 9104 | dec.symbol(&mut cdfs.uv_mode_cfl[mode]) |
| 9105 | } else { |
| 9106 | dec.symbol(&mut cdfs.uv_mode_no_cfl[mode]) |
| 9107 | }; |
| 9108 | if trace { |
| 9109 | eprintln!("TRACE uv_mode cfl={cfl} y_mode={mode} value={uv_mode} rng={}", dec.debug_state().0); |
| 9110 | } |
| 9111 | istep!("uv_mode", uv_mode as i32); |
| 9112 | let alpha = if cfl && uv_mode == UV_CFL_PRED { |
| 9113 | Some(read_cfl_alphas(dec, cdfs)) |
| 9114 | } else { |
| 9115 | None |
| 9116 | }; |
| 9117 | // `SMOOTH_PRED..PAETH_PRED` (9..=12) chroma is a separate round-2 gap |
| 9118 | // from this lane's directional chroma: a corner block with neither |
| 9119 | // `above` nor `left` (both `Edges::build`'s no-neighbour fallback, |
| 9120 | // 127/129) fed `SMOOTH_PRED` produces the wrong pixel there (worst delta |
| 9121 | // 78 against ffmpeg, traced 2026-08-27) -- an existing, un-lane-touched |
| 9122 | // bug in that fallback this lane's own scope does not cover. Keep it |
| 9123 | // refused here rather than ship a silently-wrong decode. |
| 9124 | if (9..=12).contains(&uv_mode) { |
| 9125 | SMOOTH_UV_HITS.with(|c| c.set(c.get() + 1)); |
| 9126 | } |
| 9127 | // `get_uv_mode` (spec 9.3): `UV_CFL_PRED` predicts as `DC_PRED` for the |
| 9128 | // angle-delta question -- libaom's own `read_intra_frame_mode_info` reads |
| 9129 | // `angle_delta_uv` off `get_uv_mode(uv_mode)`, never off `uv_mode` raw, |
| 9130 | // so a CFL block (uv_mode==13, outside `V_PRED..=D67_PRED`) already takes |
| 9131 | // the `else` branch below and never reads one either way. |
| 9132 | let angle_delta_uv = if (V_PRED..=D67_PRED).contains(&uv_mode) { |
| 9133 | DIRECTIONAL_UV_HITS.with(|c| c.set(c.get() + 1)); |
| 9134 | read_angle_delta(dec, &mut cdfs.angle_delta[uv_mode - V_PRED]) |
| 9135 | } else { |
| 9136 | 0 |
| 9137 | }; |
| 9138 | istep!("angle_uv", angle_delta_uv); |
| 9139 | if angle_delta_uv != 0 { |
| 9140 | UV_ANGLE_DELTA_HITS.with(|c| c.set(c.get() + 1)); |
| 9141 | } |
| 9142 | // `read_palette_mode_info` (spec 5.11.13, libaom decodemv.c:567, called |
| 9143 | // at :840 right after `xd->cfl.store_y` and before `read_filter_intra_mode_info`): |
| 9144 | // gated by `av1_allow_palette` (blockd.h -- size bound only, |
| 9145 | // [`palette_bsize_ctx`]). `palette_mode_ctx` comes from the caller |
| 9146 | // (`palette` param, `None` refuses unconditionally at ctx 0 -- the old |
| 9147 | // behaviour, bit-identical since a hardcoded ctx-0 read and a real one |
| 9148 | // read the same CDF row whenever every neighbour really is non-palette). |
| 9149 | // `palette_uv_mode_ctx` is this block's own just-decided Y palette use, |
| 9150 | // per `read_palette_mode_info`'s own `pmi->palette_size[0] > 0` (not a |
| 9151 | // neighbour lookup at all), so it is exact even at the excluded call |
| 9152 | // sites once Y decodes real syntax there too. |
| 9153 | // |
| 9154 | // lane-palette r7: libaom's `decode_color_map_tokens` is NOT called |
| 9155 | // inline from `read_palette_mode_info` -- it runs later, from |
| 9156 | // `parse_decode_block` (`decodeframe.c:1135`, `av1_visit_palette`), |
| 9157 | // after the WHOLE mode-info read for the block, including UV palette |
| 9158 | // mode_info (`read_palette_mode_info`'s own second half, right below) |
| 9159 | // and `read_filter_intra_mode_info`. So this branch now only reads |
| 9160 | // mode/size/colours for both planes (deferring the two colour-index-map |
| 9161 | // decodes to after `filter_intra`, below) -- r6's range trace pinned |
| 9162 | // this exact ordering bug (`compare-range-not-tell`: ranges matched |
| 9163 | // bit-for-bit through the Y colours, diverged only at the old inline |
| 9164 | // `decode_color_index_map` call). |
| 9165 | let mut palette_y_pending: Option<(usize, [u16; 8])> = None; |
| 9166 | let mut palette_uv_pending: Option<(usize, [u16; 8], [u16; 8])> = None; |
| 9167 | if allow_screen_content_tools |
| 9168 | && let Some(bsize_ctx) = palette_bsize_ctx(side) |
| 9169 | { |
| 9170 | let (mode_ctx, cache) = palette.unwrap_or((0, &[])); |
| 9171 | if mode == DC_PRED { |
| 9172 | if trace { |
| 9173 | let (rng, _) = dec.debug_state(); |
| 9174 | eprintln!( |
| 9175 | "TRACE pre_palette_y_mode bsize_ctx={bsize_ctx} mode_ctx={mode_ctx} rng={rng}" |
| 9176 | ); |
| 9177 | } |
| 9178 | } |
| 9179 | let use_palette_y = |
| 9180 | mode == DC_PRED && dec.symbol(&mut cdfs.palette_y_mode[bsize_ctx][mode_ctx]) != 0; |
| 9181 | if mode == DC_PRED && trace { |
| 9182 | let (rng, _) = dec.debug_state(); |
| 9183 | eprintln!("TRACE palette_y_mode value={} rng={rng}", use_palette_y as i32); |
| 9184 | } |
| 9185 | if use_palette_y { |
| 9186 | if palette.is_none() { |
| 9187 | return Err(unsupported( |
| 9188 | "a block that actually uses a palette (Y) -- reconstruction is out of scope", |
| 9189 | )); |
| 9190 | } |
| 9191 | let n = 2 + dec.symbol(&mut cdfs.palette_y_size[bsize_ctx]); |
| 9192 | if trace { |
| 9193 | let (rng, _) = dec.debug_state(); |
| 9194 | eprintln!("TRACE palette_y_size n={n} rng={rng}"); |
| 9195 | } |
| 9196 | let colors = read_palette_colors_y(dec, n, cache); |
| 9197 | if trace { |
| 9198 | let (rng, _) = dec.debug_state(); |
| 9199 | eprintln!("TRACE palette_y_colors colors={:?} rng={rng}", &colors[..n]); |
| 9200 | } |
| 9201 | palette_y_pending = Some((n, colors)); |
| 9202 | } |
| 9203 | // `read_palette_mode_info`'s UV half (decodemv.c:588): read |
| 9204 | // unconditionally at this bit position whenever `uv_mode == |
| 9205 | // UV_DC_PRED`, regardless of whether Y just fired -- this decoder's |
| 9206 | // scope always reconstructs chroma alongside luma (`decode_block` |
| 9207 | // always calls `u.reconstruct`/`v.reconstruct`), so `is_chroma_ref` |
| 9208 | // is always true here and does not need threading in. |
| 9209 | let palette_uv_mode_ctx = usize::from(use_palette_y); |
| 9210 | if uv_mode == DC_PRED && dec.symbol(&mut cdfs.palette_uv_mode[palette_uv_mode_ctx]) != 0 { |
| 9211 | let n = 2 + dec.symbol(&mut cdfs.palette_uv_size[bsize_ctx]); |
| 9212 | let (u_colors, v_colors) = read_palette_colors_uv(dec, n, palette_uv_cache); |
| 9213 | palette_uv_pending = Some((n, u_colors, v_colors)); |
| 9214 | } |
| 9215 | } |
| 9216 | // `read_filter_intra_mode_info` (spec 5.11.14, libaom decodemv.c): a |
| 9217 | // `use_filter_intra` symbol this decoder never read at all until |
| 9218 | // lane-av1real r10 -- silently skipping it left every bit past a |
| 9219 | // DC_PRED block at <=32x32 read one symbol short of libaom for the rest |
| 9220 | // of the tile (the pinned CfL stream's actual bug: partition/skip/mode |
| 9221 | // matched bit-exact, then `eob_pt` read 5 instead of 6, because this |
| 9222 | // symbol's bits were never consumed). |
| 9223 | let mut filter_intra = None; |
| 9224 | // lane-tiles r12: `av1_filter_intra_allowed` (blockd.h) also requires |
| 9225 | // `mbmi->palette_mode_info.palette_size[0] == 0` -- libaom writes NO |
| 9226 | // `use_filter_intra` symbol for a Y-palette block, and reading one here |
| 9227 | // desynced every screen-content stream from the first palette block on |
| 9228 | // (it surfaced as a bogus "partition below 8x8" refusal further down the |
| 9229 | // tile). |
| 9230 | if mode == DC_PRED |
| 9231 | && enable_filter_intra |
| 9232 | && palette_y_pending.is_none() |
| 9233 | && let Some(class) = filter_intra_size_class(side) |
| 9234 | { |
| 9235 | let use_filter_intra = dec.symbol(&mut cdfs.filter_intra[class]) != 0; |
| 9236 | if trace { |
| 9237 | eprintln!( |
| 9238 | "TRACE use_filter_intra side={side} rng={} value={}", dec.debug_state().0, |
| 9239 | use_filter_intra as i32 |
| 9240 | ); |
| 9241 | } |
| 9242 | istep!("use_filter_intra", use_filter_intra as i32); |
| 9243 | if use_filter_intra { |
| 9244 | FILTER_INTRA_HITS.with(|c| c.set(c.get() + 1)); |
| 9245 | let fi_mode = dec.symbol(&mut cdfs.filter_intra_mode); |
| 9246 | if trace { |
| 9247 | eprintln!("TRACE filter_intra_mode value={fi_mode}"); |
| 9248 | } |
| 9249 | istep!("filter_intra_mode", fi_mode as i32); |
| 9250 | filter_intra = Some(fi_mode); |
| 9251 | } |
| 9252 | } |
| 9253 | // `av1_visit_palette` (decoder.c:234): plane 0 (Y) then plane 1 (chroma, |
| 9254 | // shared U/V map) -- the same order libaom's own loop visits them in. |
| 9255 | let palette_y = palette_y_pending.map(|(n, colors)| { |
| 9256 | let map = decode_color_index_map(dec, cdfs, n, side, false); |
| 9257 | PALETTE_HITS.with(|c| c.set(c.get() + 1)); |
| 9258 | if trace { |
| 9259 | eprintln!("TRACE palette_y size={n} colors={:?}", &colors[..n]); |
| 9260 | } |
| 9261 | PaletteY { size: n, colors, map } |
| 9262 | }); |
| 9263 | let palette_uv = palette_uv_pending.map(|(n, u_colors, v_colors)| { |
| 9264 | let map = decode_color_index_map(dec, cdfs, n, side / 2, true); |
| 9265 | PALETTE_UV_HITS.with(|c| c.set(c.get() + 1)); |
| 9266 | if trace { |
| 9267 | eprintln!( |
| 9268 | "TRACE palette_uv size={n} u_colors={:?} v_colors={:?}", |
| 9269 | &u_colors[..n], |
| 9270 | &v_colors[..n] |
| 9271 | ); |
| 9272 | } |
| 9273 | PaletteUv { size: n, u_colors, v_colors, map } |
| 9274 | }); |
| 9275 | Ok(( |
| 9276 | skip, |
| 9277 | mode, |
| 9278 | angle_delta_y, |
| 9279 | uv_mode, |
| 9280 | angle_delta_uv, |
| 9281 | alpha, |
| 9282 | filter_intra, |
| 9283 | palette_y, |
| 9284 | palette_uv, |
| 9285 | )) |
| 9286 | } |
| 9287 | |
| 9288 | /// `UV_CFL_PRED` (spec 6.10.19's chroma intra mode enum): the fourteenth |
| 9289 | /// entry of [`crate::cdf::UV_MODE_CFL`], one past the thirteen ordinary |
| 9290 | /// intra modes. |
| 9291 | const UV_CFL_PRED: usize = 13; |
| 9292 | |
| 9293 | /// `read_cfl_alphas` (libaom decodemv.c): the joint sign symbol (8-ary, spec |
| 9294 | /// 5.11.45's `cfl_alpha_signs`), then each plane's alpha magnitude symbol |
| 9295 | /// (16-ary, `cfl_alpha_u`/`cfl_alpha_v`) where its sign is nonzero -- context |
| 9296 | /// indices `CFL_CONTEXT_U`/`CFL_CONTEXT_V` (libaom cfl.h) derived from the |
| 9297 | /// joint sign the same way. Returns each plane's final signed `alpha_q3` |
| 9298 | /// (`cfl_idx_to_alpha`, cfl.c): magnitude `idx + 1`, sign-zero collapsing to 0. |
| 9299 | fn read_cfl_alphas(dec: &mut SymbolDecoder, cdfs: &mut Cdfs) -> (i32, i32) { |
| 9300 | CFL_BLOCK_HITS.with(|c| c.set(c.get() + 1)); |
| 9301 | let joint_sign = dec.symbol(&mut cdfs.cfl_sign) as i32; |
| 9302 | let sign_u = ((joint_sign + 1) * 11) >> 5; |
| 9303 | let sign_v = (joint_sign + 1) - 3 * sign_u; |
| 9304 | let alpha_u = if sign_u == 0 { |
| 9305 | 0 |
| 9306 | } else { |
| 9307 | let ctx_u = (joint_sign + 1 - 3) as usize; |
| 9308 | let mag = dec.symbol(&mut cdfs.cfl_alpha[ctx_u]) as i32 + 1; |
| 9309 | if sign_u == 2 { mag } else { -mag } |
| 9310 | }; |
| 9311 | let alpha_v = if sign_v == 0 { |
| 9312 | 0 |
| 9313 | } else { |
| 9314 | let ctx_v = (sign_v * 3 + sign_u - 3) as usize; |
| 9315 | let mag = dec.symbol(&mut cdfs.cfl_alpha[ctx_v]) as i32 + 1; |
| 9316 | if sign_v == 2 { mag } else { -mag } |
| 9317 | }; |
| 9318 | (alpha_u, alpha_v) |
| 9319 | } |
| 9320 | |
| 9321 | /// `cfl_luma_subsampling_420_lbd_c` + `subtract_average_c` (libaom cfl.c): |
| 9322 | /// the reconstructed luma samples under a `side`-square block, subsampled |
| 9323 | /// 4:2:0 to Q3 (`(a+b+c+d) << 1`, each 2x2 group), then block-average |
| 9324 | /// subtracted (`round_offset = num_pel/2`, right-shifted by `log2(num_pel)`) |
| 9325 | /// to give the AC values [`cfl_scaled`] scales by alpha. |
| 9326 | fn cfl_ac_q3(y: &PlaneBuf, px: usize, py: usize, side: usize) -> Vec<i32> { |
| 9327 | let cside = side / 2; |
| 9328 | let mut ac = vec![0i32; cside * cside]; |
| 9329 | let mut sum = 0i32; |
| 9330 | for cy in 0..cside { |
| 9331 | for cx in 0..cside { |
| 9332 | let (lx, ly) = (px + cx * 2, py + cy * 2); |
| 9333 | let q3 = (i32::from(y.data[ly * y.width + lx]) |
| 9334 | + i32::from(y.data[ly * y.width + lx + 1]) |
| 9335 | + i32::from(y.data[(ly + 1) * y.width + lx]) |
| 9336 | + i32::from(y.data[(ly + 1) * y.width + lx + 1])) |
| 9337 | << 1; |
| 9338 | ac[cy * cside + cx] = q3; |
| 9339 | sum += q3; |
| 9340 | } |
| 9341 | } |
| 9342 | let num_pel = (cside * cside) as i32; |
| 9343 | let avg = (sum + num_pel / 2) >> num_pel.trailing_zeros(); |
| 9344 | ac.iter_mut().for_each(|v| *v -= avg); |
| 9345 | ac |
| 9346 | } |
| 9347 | |
| 9348 | /// `get_scaled_luma_q0` (libaom cfl.h): `alpha_q3 * ac_q3`, rounded from Q6 |
| 9349 | /// back to whole samples with `ROUND_POWER_OF_TWO_SIGNED` (round-to-nearest, |
| 9350 | /// ties away from zero on the shifted-out sign). |
| 9351 | fn cfl_scaled(alpha_q3: i32, ac_q3: i32) -> i32 { |
| 9352 | let v = alpha_q3 * ac_q3; |
| 9353 | if v >= 0 { |
| 9354 | (v + 32) >> 6 |
| 9355 | } else { |
| 9356 | -((-v + 32) >> 6) |
| 9357 | } |
| 9358 | } |
| 9359 | |
| 9360 | /// One plane's reconstruction buffer, `width * height` samples, and the |
| 9361 | /// prediction/residual reads and writes into it — the decode-side twin of |
| 9362 | /// [`crate::encode`]'s private `Plane`, whose `edges` this mirrors exactly |
| 9363 | /// (own-edge and reach clamps both), passing what it gathers straight to |
| 9364 | /// [`crate::intra::predict`] instead of hand-rolling `DC_PRED` alone. |
| 9365 | /// lane-tiny r4: raw padded-plane dump for filter-stage bisection, written |
| 9366 | /// only when `var` names a path. Same byte shape as `EC_AV1_PREFILT_DUMP`. |
| 9367 | /// lane-hgkf r2: 16-bit cropped stage dump, the high-bitdepth twin of |
| 9368 | /// [`dump_stage`] (whose `as u8` truncation hides |delta| <= 2 defects) -- |
| 9369 | /// matches aomdec's own `ec_dump16` rung byte for byte (crop dims, u16 LE). |
| 9370 | fn dump_stage16(var: &str, y: &PlaneBuf, u: &PlaneBuf, v: &PlaneBuf, fw: usize, fh: usize) { |
| 9371 | use std::io::Write; |
| 9372 | // lane-sbrect10 r2 / lane-cdef r1 (one shared helper): index the dump by |
| 9373 | // decode-order picture like EC_AV1_PREFILT_DUMP already does -- a fixed |
| 9374 | // `.f0` name kept only the LAST frame, so a mid-sequence divergence could |
| 9375 | // not be read out at all. |
| 9376 | if let Ok(path) = std::env::var(var) |
| 9377 | && let Ok(mut f) = std::fs::File::create(format!("{path}.f{}", dump_stage_idx(var))) |
| 9378 | { |
| 9379 | for (p, w, h) in [(y, fw, fh), (u, fw.div_ceil(2), fh.div_ceil(2)), (v, fw.div_ceil(2), fh.div_ceil(2))] { |
| 9380 | for row in 0..h { |
| 9381 | let mut bytes = Vec::with_capacity(w * 2); |
| 9382 | for &s in &p.data[row * p.width..][..w] { |
| 9383 | bytes.extend_from_slice(&s.to_le_bytes()); |
| 9384 | } |
| 9385 | let _ = f.write_all(&bytes); |
| 9386 | } |
| 9387 | } |
| 9388 | } |
| 9389 | } |
| 9390 | |
| 9391 | /// Decode-order index of the next [`dump_stage`]/[`dump_stage16`] file for a |
| 9392 | /// given env var -- lane-cdef r1: both used to write `.f0` unconditionally, so |
| 9393 | /// every frame overwrote the previous one and only the LAST frame survived, |
| 9394 | /// which silently defeats any per-frame filter-stage bisection. |
| 9395 | fn dump_stage_idx(var: &str) -> usize { |
| 9396 | use std::collections::HashMap; |
| 9397 | use std::sync::{Mutex, OnceLock}; |
| 9398 | static IDX: OnceLock<Mutex<HashMap<String, usize>>> = OnceLock::new(); |
| 9399 | let mut m = IDX.get_or_init(|| Mutex::new(HashMap::new())).lock().unwrap(); |
| 9400 | let e = m.entry(var.to_string()).or_insert(0); |
| 9401 | let i = *e; |
| 9402 | *e += 1; |
| 9403 | i |
| 9404 | } |
| 9405 | |
| 9406 | fn dump_stage(var: &str, y: &PlaneBuf, u: &PlaneBuf, v: &PlaneBuf) { |
| 9407 | use std::io::Write; |
| 9408 | if let Ok(path) = std::env::var(var) |
| 9409 | && let Ok(mut f) = std::fs::File::create(format!("{path}.f{}", dump_stage_idx(var))) |
| 9410 | { |
| 9411 | for p in [y, u, v] { |
| 9412 | let narrow: Vec<u8> = p.data.iter().map(|&s| s as u8).collect(); |
| 9413 | let _ = f.write_all(&narrow); |
| 9414 | } |
| 9415 | } |
| 9416 | } |
| 9417 | |
| 9418 | #[derive(Clone)] |
| 9419 | struct PlaneBuf { |
| 9420 | data: Vec<u16>, |
| 9421 | width: usize, |
| 9422 | height: usize, |
| 9423 | /// The frame's true, decodable extent in this plane's own units — past |
| 9424 | /// this, samples are the padded coding surface's invented tail, never a |
| 9425 | /// real decoder's edge or reach reads. |
| 9426 | true_width: usize, |
| 9427 | true_height: usize, |
| 9428 | /// This plane's own tile's top-left pixel origin (spec: intra prediction |
| 9429 | /// never reaches across a tile boundary even though an earlier-decoded |
| 9430 | /// tile's real pixels sit right there in the shared buffer) — set via |
| 9431 | /// [`Self::set_tile_origin`] before each tile's own superblock walk, |
| 9432 | /// `0` for the single-tile case. lane-tiles r2: only the left/top bound |
| 9433 | /// is threaded (the two-tile column-split stage 1 fixture's tile always |
| 9434 | /// ends at the frame's own right/bottom edge); a non-last tile's own |
| 9435 | /// right/bottom reach bound is still open, tracked in the report. |
| 9436 | tile_x0: usize, |
| 9437 | tile_y0: usize, |
| 9438 | /// lane-tiles r5: this plane's own tile's right/bottom pixel bound |
| 9439 | /// (exclusive) -- a non-last tile column/row's own reach must stop at |
| 9440 | /// its own SB-aligned tile boundary. `width`/`height` (this plane's |
| 9441 | /// full padded extent) for the single-tile/last-tile case, matching the |
| 9442 | /// old unclipped behaviour exactly. |
| 9443 | tile_x1: usize, |
| 9444 | tile_y1: usize, |
| 9445 | } |
| 9446 | |
| 9447 | /// One entry per `MV_REFERENCE_FRAME` (index 0/1 unused -- `NONE`/ |
| 9448 | /// `INTRA_FRAME`/`LAST_FRAME` all resolve elsewhere), `Some` when this |
| 9449 | /// frame's own `ref_frame_idx` names a DPB slot that has a picture in it |
| 9450 | /// (lane-av1refs: the same "empty slot" case `GOLDEN_FRAME` already |
| 9451 | /// refused by name, now generic across `LAST2`/`LAST3`/`GOLDEN`/`BWDREF`/ |
| 9452 | /// `ALTREF2`/`ALTREF`). |
| 9453 | type RefSlots<'a> = [Option<(&'a PlaneBuf, &'a PlaneBuf, &'a PlaneBuf)>; 8]; |
| 9454 | |
| 9455 | impl PlaneBuf { |
| 9456 | /// Sets this plane's own tile pixel origin ([`Self::tile_x0`]/ |
| 9457 | /// [`Self::tile_y0`]) before that tile's own superblock walk. `x0`/`y0` |
| 9458 | /// are already in this plane's own units (luma pixels for `y`, chroma |
| 9459 | /// pixels — halved — for `u`/`v`). |
| 9460 | fn set_tile_origin(&mut self, x0: usize, y0: usize, x1: usize, y1: usize) { |
| 9461 | self.tile_x0 = x0; |
| 9462 | self.tile_y0 = y0; |
| 9463 | self.tile_x1 = x1; |
| 9464 | self.tile_y1 = y1; |
| 9465 | } |
| 9466 | |
| 9467 | fn edges( |
| 9468 | &self, |
| 9469 | x: usize, |
| 9470 | y: usize, |
| 9471 | side: usize, |
| 9472 | reach: Reach, |
| 9473 | ) -> (Option<Vec<u16>>, Option<Vec<u16>>, Option<u16>) { |
| 9474 | let own_across = x + side.min(self.true_width.saturating_sub(x)); |
| 9475 | let across = if reach.above_right { |
| 9476 | own_across + side.min(self.true_width.saturating_sub(own_across)) |
| 9477 | } else { |
| 9478 | own_across |
| 9479 | } |
| 9480 | .min(self.width) |
| 9481 | .min(self.tile_x1); |
| 9482 | let own_down = y + side.min(self.true_height.saturating_sub(y)); |
| 9483 | let down = if reach.below_left { |
| 9484 | own_down + side.min(self.true_height.saturating_sub(own_down)) |
| 9485 | } else { |
| 9486 | own_down |
| 9487 | } |
| 9488 | .min(self.height) |
| 9489 | .min(self.tile_y1); |
| 9490 | let above = (y > self.tile_y0 && across > x) |
| 9491 | .then(|| self.data[(y - 1) * self.width + x..][..across - x].to_vec()); |
| 9492 | let left = (x > self.tile_x0 && down > y).then(|| { |
| 9493 | (y..down) |
| 9494 | .map(|row| self.data[row * self.width + x - 1]) |
| 9495 | .collect::<Vec<_>>() |
| 9496 | }); |
| 9497 | let corner = |
| 9498 | (x > self.tile_x0 && y > self.tile_y0).then(|| self.data[(y - 1) * self.width + x - 1]); |
| 9499 | (above, left, corner) |
| 9500 | } |
| 9501 | |
| 9502 | /// [`Self::edges`] for a true `bw`x`bh` block (lane-intradisp r1): |
| 9503 | /// the above/left reach each extend by their OWN axis (`bw` across, |
| 9504 | /// `bh` down), not one shared `side`. |
| 9505 | fn edges_rect( |
| 9506 | &self, |
| 9507 | x: usize, |
| 9508 | y: usize, |
| 9509 | bw: usize, |
| 9510 | bh: usize, |
| 9511 | reach: Reach, |
| 9512 | ) -> (Option<Vec<u16>>, Option<Vec<u16>>, Option<u16>) { |
| 9513 | let own_across = x + bw.min(self.true_width.saturating_sub(x)); |
| 9514 | let across = if reach.above_right { |
| 9515 | own_across + bw.min(self.true_width.saturating_sub(own_across)) |
| 9516 | } else { |
| 9517 | own_across |
| 9518 | } |
| 9519 | .min(self.width) |
| 9520 | .min(self.tile_x1); |
| 9521 | let own_down = y + bh.min(self.true_height.saturating_sub(y)); |
| 9522 | let down = if reach.below_left { |
| 9523 | own_down + bh.min(self.true_height.saturating_sub(own_down)) |
| 9524 | } else { |
| 9525 | own_down |
| 9526 | } |
| 9527 | .min(self.height) |
| 9528 | .min(self.tile_y1); |
| 9529 | let above = (y > self.tile_y0 && across > x) |
| 9530 | .then(|| self.data[(y - 1) * self.width + x..][..across - x].to_vec()); |
| 9531 | let left = (x > self.tile_x0 && down > y).then(|| { |
| 9532 | (y..down) |
| 9533 | .map(|row| self.data[row * self.width + x - 1]) |
| 9534 | .collect::<Vec<_>>() |
| 9535 | }); |
| 9536 | let corner = |
| 9537 | (x > self.tile_x0 && y > self.tile_y0).then(|| self.data[(y - 1) * self.width + x - 1]); |
| 9538 | if matches!((bw, bh), (16, 8) | (8, 16)) { |
| 9539 | RECT_STRIP_PRED_HITS.with(|h| h.set(h.get() + 1)); |
| 9540 | if across > own_across || down > own_down { |
| 9541 | RECT_STRIP_REACH_HITS.with(|h| h.set(h.get() + 1)); |
| 9542 | } |
| 9543 | } |
| 9544 | (above, left, corner) |
| 9545 | } |
| 9546 | |
| 9547 | /// [`Self::reconstruct`] for a true `bw`x`bh` block (lane-intradisp r1), |
| 9548 | /// via [`Self::edges_rect`] and [`crate::intra::predict`]'s own |
| 9549 | /// already-rect-capable `(bw, bh)` pair. |
| 9550 | #[allow(clippy::too_many_arguments)] |
| 9551 | #[track_caller] |
| 9552 | fn reconstruct_rect( |
| 9553 | &mut self, |
| 9554 | x: usize, |
| 9555 | y: usize, |
| 9556 | bw: usize, |
| 9557 | bh: usize, |
| 9558 | mode: usize, |
| 9559 | angle_delta: i32, |
| 9560 | reach: Reach, |
| 9561 | residual: &[i32], |
| 9562 | cfl: Option<(i32, &[i32])>, |
| 9563 | filter_intra: Option<usize>, |
| 9564 | smooth_neighbor: bool, |
| 9565 | ) { |
| 9566 | // As [`Self::reconstruct`]'s own [`PALETTE_PRED`] override |
| 9567 | // (lane-palette2 r4): a palette block's prediction is the |
| 9568 | // reconstructed colour-index map, needing no edge pixels at all. |
| 9569 | let prediction = if let Some(buf) = take_palette_pred() { |
| 9570 | buf |
| 9571 | } else { |
| 9572 | let (above, left, corner) = self.edges_rect(x, y, bw, bh, reach); |
| 9573 | if std::env::var_os("EC_DEBUG_EDGES").is_some() { |
| 9574 | eprintln!( |
| 9575 | "EDGES x={x} y={y} bw={bw} bh={bh} mode={mode} above={:?} left_len={:?} left0={:?} corner={corner:?} tx0={} ty0={} truew={} trueh={}", |
| 9576 | above.as_ref().map(|a| (a.len(), a[0])), |
| 9577 | left.as_ref().map(|l| l.len()), |
| 9578 | left.as_ref().map(|l| l[0]), |
| 9579 | self.tile_x0, self.tile_y0, self.true_width, self.true_height |
| 9580 | ); |
| 9581 | } |
| 9582 | let mut prediction = vec![0u16; bw * bh]; |
| 9583 | if let Some(fi_mode) = filter_intra { |
| 9584 | crate::intra::predict_filter_intra( |
| 9585 | fi_mode, |
| 9586 | above.as_deref(), |
| 9587 | left.as_deref(), |
| 9588 | corner, |
| 9589 | bw, |
| 9590 | bh, |
| 9591 | &mut prediction, |
| 9592 | ); |
| 9593 | } else { |
| 9594 | let enable_edge_filter = ENABLE_EDGE_FILTER.with(std::cell::Cell::get); |
| 9595 | predict( |
| 9596 | mode as u8, |
| 9597 | angle_delta, |
| 9598 | above.as_deref(), |
| 9599 | left.as_deref(), |
| 9600 | corner, |
| 9601 | bw, |
| 9602 | bh, |
| 9603 | enable_edge_filter, |
| 9604 | smooth_neighbor, |
| 9605 | &mut prediction, |
| 9606 | ); |
| 9607 | } |
| 9608 | prediction |
| 9609 | }; |
| 9610 | // lane-hgkf r2: per-TU prediction trace, the twin of aomdec's |
| 9611 | // EC_PRED/EC_PREDND rungs (x/y are this plane's pixel origin). |
| 9612 | if std::env::var_os("EC_PRED").is_some() { |
| 9613 | let sum: i64 = prediction.iter().map(|&v| i64::from(v)).sum(); |
| 9614 | let row0: Vec<u16> = prediction[..bw.min(8)].to_vec(); |
| 9615 | let col0: Vec<u16> = (0..bh.min(8)).map(|r| prediction[r * bw]).collect(); |
| 9616 | eprintln!( |
| 9617 | "@{} OUR_PRED x={x} y={y} bw={bw} bh={bh} mode={mode} ad={angle_delta} sum={sum} row0={row0:?} col0={col0:?}", std::panic::Location::caller() |
| 9618 | ); |
| 9619 | } |
| 9620 | for row in 0..bh { |
| 9621 | for col in 0..bw { |
| 9622 | let idx = row * bw + col; |
| 9623 | let mut base = i32::from(prediction[idx]); |
| 9624 | if let Some((alpha_q3, ac_q3)) = cfl { |
| 9625 | base = (base + cfl_scaled(alpha_q3, ac_q3[idx])).clamp(0, sample_max()); |
| 9626 | } |
| 9627 | let sample = (base + residual[idx]).clamp(0, sample_max()) as u16; |
| 9628 | self.data[(y + row) * self.width + x + col] = sample; |
| 9629 | } |
| 9630 | } |
| 9631 | } |
| 9632 | |
| 9633 | /// Predicts under `mode` then adds `residual` (side*side, raster), |
| 9634 | /// writing the clamped reconstruction back into the plane at `(x, y)`. |
| 9635 | /// `cfl`, when `Some((alpha_q3, ac_q3))` (a `UV_CFL_PRED` chroma block), |
| 9636 | /// nudges the prediction by [`cfl_scaled`]'s per-sample amount before the |
| 9637 | /// residual is added — `av1_cfl_predict_block`'s own clip-then-add-residual |
| 9638 | /// order (cfl.c). |
| 9639 | #[track_caller] |
| 9640 | fn reconstruct( |
| 9641 | &mut self, |
| 9642 | x: usize, |
| 9643 | y: usize, |
| 9644 | side: usize, |
| 9645 | mode: usize, |
| 9646 | angle_delta: i32, |
| 9647 | reach: Reach, |
| 9648 | residual: &[i32], |
| 9649 | cfl: Option<(i32, &[i32])>, |
| 9650 | filter_intra: Option<usize>, |
| 9651 | // `smooth_neighbor` (spec `get_intra_edge_filter_type` / |
| 9652 | // libaom `get_filt_type`): true when the block's above OR left |
| 9653 | // neighbour's mode is `SMOOTH_PRED..=SMOOTH_H_PRED` (never `PAETH`). |
| 9654 | // Callers pass their own plane's neighbour-mode check |
| 9655 | // ([`is_smooth_mode`]) — chroma's is still hardcoded `false` by its |
| 9656 | // callers until a smooth/paeth `uv_mode` can reach this call at all |
| 9657 | // (still refused in `read_intra_mode`/`read_intra_mode_rect`), which |
| 9658 | // keeps `false` exact for chroma without this fn needing to know why. |
| 9659 | smooth_neighbor: bool, |
| 9660 | ) { |
| 9661 | // A palette block's own [`PALETTE_PRED`] override (lane-palette r2) |
| 9662 | // takes priority over `predict()`/`predict_filter_intra` entirely -- |
| 9663 | // palette prediction needs no edge pixels at all. |
| 9664 | let prediction = if let Some(buf) = take_palette_pred() { |
| 9665 | buf |
| 9666 | } else { |
| 9667 | let (above, left, corner) = self.edges(x, y, side, reach); |
| 9668 | let mut prediction = vec![0u16; side * side]; |
| 9669 | if let Some(fi_mode) = filter_intra { |
| 9670 | crate::intra::predict_filter_intra( |
| 9671 | fi_mode, |
| 9672 | above.as_deref(), |
| 9673 | left.as_deref(), |
| 9674 | corner, |
| 9675 | side, |
| 9676 | side, |
| 9677 | &mut prediction, |
| 9678 | ); |
| 9679 | } else { |
| 9680 | // `enable_intra_edge_filter` is the sequence header's own flag |
| 9681 | // (`ENABLE_EDGE_FILTER`, set once per `decode_key_frame_tile_with_cdfs` |
| 9682 | // call, never per-block) -- threading it through every |
| 9683 | // `read_plane`/`decode_block`/`decode_leaf8` call would touch two |
| 9684 | // dozen sites for a value that never changes mid-decode, so it |
| 9685 | // rides a thread-local instead (this decoder is single-threaded |
| 9686 | // per stream). |
| 9687 | let enable_edge_filter = ENABLE_EDGE_FILTER.with(std::cell::Cell::get); |
| 9688 | predict( |
| 9689 | mode as u8, |
| 9690 | angle_delta, |
| 9691 | above.as_deref(), |
| 9692 | left.as_deref(), |
| 9693 | corner, |
| 9694 | side, |
| 9695 | side, |
| 9696 | enable_edge_filter, |
| 9697 | smooth_neighbor, |
| 9698 | &mut prediction, |
| 9699 | ); |
| 9700 | } |
| 9701 | prediction |
| 9702 | }; |
| 9703 | // lane-hgkf r2: per-TU prediction trace, the twin of aomdec's |
| 9704 | // EC_PRED/EC_PREDND rungs (x/y are this plane's pixel origin). |
| 9705 | if std::env::var_os("EC_PRED").is_some() { |
| 9706 | let sum: i64 = prediction.iter().map(|&v| i64::from(v)).sum(); |
| 9707 | let row0: Vec<u16> = prediction[..side.min(8)].to_vec(); |
| 9708 | let col0: Vec<u16> = (0..side.min(8)).map(|r| prediction[r * side]).collect(); |
| 9709 | eprintln!( |
| 9710 | "@{} OUR_PRED x={x} y={y} side={side} side={side} mode={mode} ad={angle_delta} sum={sum} row0={row0:?} col0={col0:?}", std::panic::Location::caller() |
| 9711 | ); |
| 9712 | } |
| 9713 | for row in 0..side { |
| 9714 | for col in 0..side { |
| 9715 | let idx = row * side + col; |
| 9716 | let mut base = i32::from(prediction[idx]); |
| 9717 | if let Some((alpha_q3, ac_q3)) = cfl { |
| 9718 | base = (base + cfl_scaled(alpha_q3, ac_q3[idx])).clamp(0, sample_max()); |
| 9719 | } |
| 9720 | let sample = (base + residual[idx]).clamp(0, sample_max()) as u16; |
| 9721 | self.data[(y + row) * self.width + x + col] = sample; |
| 9722 | } |
| 9723 | } |
| 9724 | } |
| 9725 | } |
| 9726 | |
| 9727 | /// Reads one plane's transform block, reconstructs it into `plane` at |
| 9728 | /// `(x, y)`/`side`, and records what it leaves behind in `neighbours`. |
| 9729 | #[allow(clippy::too_many_arguments)] |
| 9730 | fn read_plane( |
| 9731 | dec: &mut SymbolDecoder, |
| 9732 | cdfs: &mut Cdfs, |
| 9733 | set: TxbSet, |
| 9734 | scan: &[u16], |
| 9735 | plane_idx: usize, |
| 9736 | around: (bool, bool, i32), |
| 9737 | // The block's own luma mode — [`crate::tile::write_block_planes`] passes |
| 9738 | // it for every plane's `txb` lookup (it is what an intra transform type's |
| 9739 | // CDF is indexed by, spec 9.3), never the plane's own predicted mode. |
| 9740 | tx_mode: usize, |
| 9741 | predict_mode: usize, |
| 9742 | angle_delta: i32, |
| 9743 | reach: Reach, |
| 9744 | plane: &mut PlaneBuf, |
| 9745 | x: usize, |
| 9746 | y: usize, |
| 9747 | side: usize, |
| 9748 | tx_side: usize, |
| 9749 | base_q_idx: u8, |
| 9750 | cfl: Option<(i32, &[i32])>, |
| 9751 | filter_intra: Option<usize>, |
| 9752 | // Only meaningful for `plane_idx == 0`: `Some` when this transform unit is |
| 9753 | // smaller than its own block (spec `get_txb_ctx_general`'s |
| 9754 | // `plane_bsize != tx_size` branch), carrying the already-looked-up |
| 9755 | // `SKIP_CONTEXTS` value; `None` everywhere else (a lone luma TU, or any |
| 9756 | // chroma plane), where `txb_skip_ctx` is 0 or the usual OR-of-neighbours |
| 9757 | // formula below. |
| 9758 | luma_skip_ctx: Option<usize>, |
| 9759 | // See [`PlaneBuf::reconstruct`]'s own doc: caller's neighbour-smooth check. |
| 9760 | smooth_neighbor: bool, |
| 9761 | ) -> Result<Vec<i32>> { |
| 9762 | let skip_ctx = if plane_idx == 0 { |
| 9763 | luma_skip_ctx.unwrap_or(0) |
| 9764 | } else { |
| 9765 | // libaom `get_txb_ctx`: a chroma unit's context is |
| 9766 | // `combine_entropy_contexts(above, left)` plus 7, or plus 10 when the |
| 9767 | // plane block is bigger than the transform. Our chroma tables hold the |
| 9768 | // offset-7 rows first, so the caller adds 3 for the bigger-block case |
| 9769 | // (only a 128x128 block's TX_32X32 chroma units, lane-sb128b r3). |
| 9770 | usize::from(around.0) + usize::from(around.1) + luma_skip_ctx.unwrap_or(0) |
| 9771 | }; |
| 9772 | // spec 5.11.47/libaom `av1_read_tx_type`: a filter-intra luma block's |
| 9773 | // `intra_ext_tx_cdf` row is indexed by `fimode_to_intradir[filter_intra_mode]`, |
| 9774 | // not by the block's ordinary `mode` (which is always DC_PRED whenever |
| 9775 | // filter-intra is on) -- caller passes the raw filter-intra mode 0..4 in |
| 9776 | // `filter_intra`, only meaningful for `plane_idx == 0` (chroma has no |
| 9777 | // filter-intra in AV1). |
| 9778 | let tx_mode = if plane_idx == 0 { fi_tx_row(tx_mode, filter_intra) } else { tx_mode }; |
| 9779 | let mut coding = cdfs.txb(set, tx_mode); |
| 9780 | // Luma's `default_tx_type` is only a fallback for the sizes whose |
| 9781 | // `TxbSet` carries no symbol at all (32-point and up), which the spec |
| 9782 | // fixes at `DCT_DCT` regardless of mode; a chroma plane's `tx_type` is |
| 9783 | // *never* its own coded symbol (`coding.tx_type` is always `None` for |
| 9784 | // every `TxbSet::Chroma*`), so it would take `Intra_Mode_To_Tx_Type` of |
| 9785 | // the plane's own predicted mode (`default_intra_tx_type`) -- except |
| 9786 | // `av1_get_ext_tx_set_type` (`blockd.h`) resolves chroma at `tx_size_sqr |
| 9787 | // >= TX_32X32` to `EXT_TX_SET_DCTONLY`, which forces the result back to |
| 9788 | // `DCT_DCT` no matter what the mode-indexed table said (`TxbSet::Chroma8` |
| 9789 | // and `Chroma16` never hit this — every value `default_intra_tx_type` can |
| 9790 | // produce is already a member of both `TX_SET_INTRA_1`/`_2`, so nothing |
| 9791 | // narrows there — only `Chroma32` and up do). |
| 9792 | let default_tx_type = if plane_idx == 0 || side >= 32 { |
| 9793 | TxType::DctDct |
| 9794 | } else { |
| 9795 | default_intra_tx_type(predict_mode as u8) |
| 9796 | }; |
| 9797 | let (grid, tx_type) = read_coeffs( |
| 9798 | dec, |
| 9799 | &mut coding, |
| 9800 | scan, |
| 9801 | skip_ctx, |
| 9802 | dc_sign_ctx(around.2), |
| 9803 | default_tx_type, |
| 9804 | None, |
| 9805 | )?; |
| 9806 | note_chroma_class1(plane_idx, tx_type); |
| 9807 | // A 64x64 luma block's transform covers the whole 64x64 area, but only its |
| 9808 | // top-left 32x32 of frequencies are coded (spec 5.11.40); the rest of the |
| 9809 | // dequantized grid stays zero, which `inverse_transform_2d`'s own `< 32` |
| 9810 | // guard also assumes. |
| 9811 | let levels = if tx_side == side { |
| 9812 | grid |
| 9813 | } else { |
| 9814 | // `grid` is `tx_side x tx_side` (the coded corner); `dequant_and_inverse` |
| 9815 | // wants the true `side x side` transform, with dqDenom (spec 7.12.3) |
| 9816 | // keyed by that true size, not the coded corner's -- so the corner |
| 9817 | // goes into the top-left of a `side`-sized grid, not the reverse. |
| 9818 | let mut full = vec![0i32; side * side]; |
| 9819 | for row in 0..tx_side { |
| 9820 | full[row * side..][..tx_side].copy_from_slice(&grid[row * tx_side..][..tx_side]); |
| 9821 | } |
| 9822 | full |
| 9823 | }; |
| 9824 | let (dc_delta, ac_delta) = plane_q_delta(plane_idx); |
| 9825 | let residual = dequant_and_inverse_typed(&levels, side, bit_depth(), block_q_idx(), dc_delta, ac_delta, tx_type); |
| 9826 | if std::env::var_os("EC_AV1_TRACE").is_some() { |
| 9827 | eprintln!( |
| 9828 | "TRACE dequant plane={plane_idx} base_q_idx={base_q_idx} tx_type={tx_type:?} side={side} levels={levels:?} residual={residual:?}", |
| 9829 | ); |
| 9830 | } |
| 9831 | plane.reconstruct( |
| 9832 | x, |
| 9833 | y, |
| 9834 | side, |
| 9835 | predict_mode, |
| 9836 | angle_delta, |
| 9837 | reach, |
| 9838 | &residual, |
| 9839 | cfl, |
| 9840 | filter_intra, |
| 9841 | smooth_neighbor, |
| 9842 | ); |
| 9843 | Ok(if tx_side == side { |
| 9844 | residual_grid_placeholder(&levels, side) |
| 9845 | } else { |
| 9846 | levels |
| 9847 | }) |
| 9848 | } |
| 9849 | |
| 9850 | /// `record`'s neighbour state only reads whether *the coded grid* (not the |
| 9851 | /// residual) has nonzero entries, so the levels this function already built |
| 9852 | /// are what it wants back; this helper exists only to name that clearly at |
| 9853 | /// the one call site where `tx_side == side`. |
| 9854 | fn residual_grid_placeholder(levels: &[i32], _tx_side: usize) -> Vec<i32> { |
| 9855 | levels.to_vec() |
| 9856 | } |
| 9857 | |
| 9858 | /// Decodes one whole-block coded superblock/quadrant/leaf: its mode, then its |
| 9859 | /// three planes, reconstructing them into `y`/`u`/`v` and updating |
| 9860 | /// `neighbours`. |
| 9861 | #[allow(clippy::too_many_arguments)] |
| 9862 | fn decode_block( |
| 9863 | dec: &mut SymbolDecoder, |
| 9864 | cdfs: &mut Cdfs, |
| 9865 | neighbours: &mut Neighbours, |
| 9866 | at: (usize, usize), |
| 9867 | side: usize, |
| 9868 | luma_set: TxbSet, |
| 9869 | chroma_set: TxbSet, |
| 9870 | luma_tx: usize, |
| 9871 | chroma_tx: usize, |
| 9872 | scans: (&[u16], &[u16]), |
| 9873 | cfl: bool, |
| 9874 | y: &mut PlaneBuf, |
| 9875 | u: &mut PlaneBuf, |
| 9876 | v: &mut PlaneBuf, |
| 9877 | base_q_idx: u8, |
| 9878 | enable_filter_intra: bool, |
| 9879 | allow_screen_content_tools: bool, |
| 9880 | allow_intrabc: bool, |
| 9881 | scan32: &[u16], |
| 9882 | scan16: &[u16], |
| 9883 | scan8: &[u16], |
| 9884 | scan4: &[u16], |
| 9885 | tx_select: bool, |
| 9886 | reduced_tx_set: bool, |
| 9887 | ) -> Result<()> { |
| 9888 | let (r, c) = at; |
| 9889 | let (px, py) = (c * SUB, r * SUB); |
| 9890 | let (palette_ctx, palette_cache) = neighbours.palette_ctx_and_cache(at); |
| 9891 | let palette_uv_cache = neighbours.palette_uv_cache(at); |
| 9892 | let (nb_above_mode, nb_left_mode) = neighbours.modes_above_left(r, c); |
| 9893 | let at_tile_left = c == neighbours.tile_col0_mi / (SUB / MI); |
| 9894 | let (skip, mode, angle_delta_y, uv_mode, angle_delta_uv, alpha, filter_intra, palette_y, palette_uv) = |
| 9895 | read_intra_mode( |
| 9896 | dec, |
| 9897 | cdfs, |
| 9898 | nb_above_mode, |
| 9899 | nb_left_mode, |
| 9900 | cfl, |
| 9901 | side, |
| 9902 | enable_filter_intra, |
| 9903 | neighbours.skip_txfm_ctx(r * (SUB / MI), c * (SUB / MI)), |
| 9904 | allow_screen_content_tools, |
| 9905 | allow_intrabc, |
| 9906 | Some((palette_ctx, &palette_cache)), |
| 9907 | &palette_uv_cache, |
| 9908 | r * (SUB / MI), |
| 9909 | c * (SUB / MI), |
| 9910 | )?; |
| 9911 | if at_tile_left && palette_y.is_some() { |
| 9912 | PALETTE_TILE_LEFT_HITS.with(|c| c.set(c.get() + 1)); |
| 9913 | } |
| 9914 | let intrabc_dv = INTRABC_DV.with(|c| c.take()); |
| 9915 | // libaom `parse_decode_block`: an intrabc block is `inter_block_tx`, so |
| 9916 | // under `TX_MODE_SELECT` its transform size comes from the inter var-tx |
| 9917 | // partition tree (`read_var_tx_size`), a syntax element this decoder has |
| 9918 | // nowhere -- reading the intra `tx_depth` symbol instead would desync. |
| 9919 | if intrabc_dv.is_some() && tx_select && !skip { |
| 9920 | return Err(unsupported( |
| 9921 | "an intrabc block under TxMode::Select (its transform size is coded by the inter var-tx partition tree, which this decoder never reads)", |
| 9922 | )); |
| 9923 | } |
| 9924 | // `predict` panics outside `DC_PRED..=PAETH_PRED` (0..=12); `UV_CFL_PRED` |
| 9925 | // (13) predicts as `DC_PRED` with the [`cfl_scaled`] nudge carrying the |
| 9926 | // actual chroma-from-luma correlation, same as this decoder always did. |
| 9927 | let uv_predict_mode = if uv_mode == UV_CFL_PRED { |
| 9928 | DC_PRED |
| 9929 | } else { |
| 9930 | uv_mode |
| 9931 | }; |
| 9932 | let smooth_neighbor = is_smooth_mode(nb_above_mode) || is_smooth_mode(nb_left_mode); |
| 9933 | if smooth_neighbor { |
| 9934 | SMOOTH_LUMA_HITS.with(|c| c.set(c.get() + 1)); |
| 9935 | } |
| 9936 | let smooth_neighbor_uv = |
| 9937 | neighbours.smooth_uv_neighbour(r * (SUB / MI), c * (SUB / MI), r, c); |
| 9938 | if std::env::var_os("EC_AV1_TRACE").is_some() { |
| 9939 | eprintln!( |
| 9940 | "TRACE block px={px} py={py} side={side} mode={mode} uv_mode={uv_mode} \ |
| 9941 | angle_delta_uv={angle_delta_uv}" |
| 9942 | ); |
| 9943 | } |
| 9944 | let (mi_r, mi_c) = (r * (SUB / MI), c * (SUB / MI)); |
| 9945 | // `TxMode::Select`'s `tx_depth` (spec 5.11.16): read for every intra |
| 9946 | // block, skipped or not. `logical_tx` is what the loop filter's edge |
| 9947 | // lookup and the next block's own `tx_size_context` want (the *nominal* |
| 9948 | // transform size -- 64 even for the 64x64 corner-scanned case, spec |
| 9949 | // 5.11.40); `coeff_tx_side` is the actual coded coefficient grid's side, |
| 9950 | // which only differs from it at that one corner case. |
| 9951 | let (logical_tx, coeff_tx_side) = if tx_select { |
| 9952 | let resolved = read_tx_size(dec, cdfs, neighbours, (mi_r, mi_c), side, None); |
| 9953 | (resolved, if resolved == 64 { 32 } else { resolved }) |
| 9954 | } else { |
| 9955 | // lane-sb128b r1: `max_txsize_rect_lookup[BLOCK_128X128]` is |
| 9956 | // TX_64X64, so a 128x128 block is always FOUR luma transform units |
| 9957 | // even with `tx_select` off -- the TU loop below, never the |
| 9958 | // single-unit path. |
| 9959 | (side.min(64), luma_tx) |
| 9960 | }; |
| 9961 | if tx_select && logical_tx < side && uv_mode != DC_PRED { |
| 9962 | SQ_CHROMA_TX_HITS.with(|c| c.set(c.get() + 1)); |
| 9963 | } |
| 9964 | let luma_set = if tx_select { |
| 9965 | txbset_for(logical_tx, reduced_tx_set) |
| 9966 | } else { |
| 9967 | luma_set |
| 9968 | }; |
| 9969 | let luma_set = if intrabc_dv.is_some() { |
| 9970 | txbset_for_inter(logical_tx, reduced_tx_set) |
| 9971 | } else { |
| 9972 | luma_set |
| 9973 | }; |
| 9974 | let luma_scan = scan_for(coeff_tx_side, scan32, scan16, scan8, scan4); |
| 9975 | let reach = Reach::of(side, px, py, y.width, y.height); |
| 9976 | let (cpx, cpy) = (px / 2, py / 2); |
| 9977 | let chroma_side = side / 2; |
| 9978 | // spec 7.11.3.4 with `use_intrabc`: the reference is the *current* |
| 9979 | // frame's own pre-loop-filter reconstruction (all loop filters are off |
| 9980 | // whenever `allow_intrabc`, frame.rs:194/210/252/272) at the full-pel |
| 9981 | // block vector, with the BILINEAR kernel libaom forces for intrabc -- |
| 9982 | // luma is a straight copy, chroma lands half-pel whenever the luma DV is |
| 9983 | // odd, which is exactly what the bilinear taps then interpolate. |
| 9984 | let intrabc_bufs = intrabc_dv.map(|(dv_row, dv_col)| { |
| 9985 | let mut yb = vec![0u16; side * side]; |
| 9986 | mc::predict_with_filter( |
| 9987 | &y.data, y.width, y.true_width, y.true_height, |
| 9988 | mv_to_q4(px, dv_col, true), mv_to_q4(py, dv_row, true), |
| 9989 | side, side, mc::InterpFilterKind::Bilinear, &mut yb, |
| 9990 | ); |
| 9991 | let mut ub = vec![0u16; chroma_side * chroma_side]; |
| 9992 | mc::predict_with_filter( |
| 9993 | &u.data, u.width, u.true_width, u.true_height, |
| 9994 | mv_to_q4(cpx, dv_col, false), mv_to_q4(cpy, dv_row, false), |
| 9995 | chroma_side, chroma_side, mc::InterpFilterKind::Bilinear, &mut ub, |
| 9996 | ); |
| 9997 | let mut vb = vec![0u16; chroma_side * chroma_side]; |
| 9998 | mc::predict_with_filter( |
| 9999 | &v.data, v.width, v.true_width, v.true_height, |
| 10000 | mv_to_q4(cpx, dv_col, false), mv_to_q4(cpy, dv_row, false), |
| 10001 | chroma_side, chroma_side, mc::InterpFilterKind::Bilinear, &mut vb, |
| 10002 | ); |
| 10003 | (yb, ub, vb) |
| 10004 | }); |
| 10005 | if let Some((yb, _, _)) = &intrabc_bufs { |
| 10006 | set_palette_pred(yb.clone()); |
| 10007 | } |
| 10008 | // A palette-Y block's own prediction is the reconstructed colour-index |
| 10009 | // map, not `predict()`'s edge-based one -- [`PALETTE_PRED`] carries it |
| 10010 | // to [`PlaneBuf::reconstruct`]'s next call rather than threading a |
| 10011 | // parameter through this function's dozen `y.reconstruct`/`read_plane` |
| 10012 | // sites. A split luma transform (`tx_select && logical_tx != side`) |
| 10013 | // would need the map sliced per-TU -- untested against a real |
| 10014 | // `--enable-palette=1` stream this round, so refuse by name rather than |
| 10015 | // guess at the slicing (charter's own named-acceptable fallback). |
| 10016 | let palette_y_buf: Option<Vec<u16>> = palette_y.as_ref().map(|py| { |
| 10017 | py.map |
| 10018 | .iter() |
| 10019 | .map(|&idx| py.colors[idx as usize]) |
| 10020 | .collect() |
| 10021 | }); |
| 10022 | // With one luma transform unit the whole `side`x`side` map is the |
| 10023 | // prediction; with several (`logical_tx != side`) each unit takes its own |
| 10024 | // `logical_tx`-sized window of that same map, set fresh inside the TU loop |
| 10025 | // below (lane-palette2 r7) -- setting the full-block buffer here would be |
| 10026 | // taken by the FIRST unit and indexed at the wrong stride. |
| 10027 | if let Some(buf) = &palette_y_buf |
| 10028 | && logical_tx == side |
| 10029 | { |
| 10030 | set_palette_pred(buf.clone()); |
| 10031 | } |
| 10032 | // A UV-palette block's own chroma prediction override (lane-palette2 r1), |
| 10033 | // same [`PALETTE_PRED`] slot as Y above -- set fresh right before each |
| 10034 | // plane's own `reconstruct`/`read_plane` call below (never both planes at |
| 10035 | // once), since Y's own set-then-take already emptied the slot by the time |
| 10036 | // either of these run. |
| 10037 | let palette_uv_bufs: Option<(Vec<u16>, Vec<u16>)> = palette_uv.as_ref().map(|puv| { |
| 10038 | ( |
| 10039 | puv.map.iter().map(|&idx| puv.u_colors[idx as usize]).collect(), |
| 10040 | puv.map.iter().map(|&idx| puv.v_colors[idx as usize]).collect(), |
| 10041 | ) |
| 10042 | }); |
| 10043 | // The chroma half of the intrabc prediction rides the same per-plane |
| 10044 | // override slot the UV-palette buffers do (set fresh before each plane's |
| 10045 | // own `reconstruct`/`read_plane` call below). |
| 10046 | let palette_uv_bufs = match &intrabc_bufs { |
| 10047 | Some((_, ub, vb)) => Some((ub.clone(), vb.clone())), |
| 10048 | None => palette_uv_bufs, |
| 10049 | }; |
| 10050 | if skip { |
| 10051 | // A skipped block codes no residual syntax at all (spec 5.11.34): |
| 10052 | // straight prediction, on every plane. |
| 10053 | // |
| 10054 | // lane-hgkf r2: an intra block's `tx_depth` is read whether or not it |
| 10055 | // is skipped (only an INTER skip forces the largest transform), and |
| 10056 | // spec 7.11.2 / libaom `predict_and_reconstruct_intra_block` predicts |
| 10057 | // ONE TRANSFORM UNIT AT A TIME -- each unit's edges are the units |
| 10058 | // already written left/above of it inside this same block. Predicting |
| 10059 | // the whole `side`x`side` at once gives a different (uniformly off by |
| 10060 | // ~1) answer, which is exactly the residue this lane was chasing. |
| 10061 | // The palette/intrabc override path keeps the single-call shape (its |
| 10062 | // per-TU windowing is the non-skip loop's own `palette_y_buf` slice, |
| 10063 | // untested for skip and unreachable in these streams). |
| 10064 | // lane-sb128b r2: a 128x128 block's transform is TX_64X64 even with |
| 10065 | // `tx_select` off (`max_txsize_rect_lookup[BLOCK_128X128]`), so the |
| 10066 | // per-unit prediction is keyed on the transform being smaller than |
| 10067 | // the block, not on the tx-depth syntax. The counter keeps lane-hgkf's |
| 10068 | // narrower meaning (a READ split tx depth). |
| 10069 | let split_tx_skip = |
| 10070 | logical_tx < side && palette_y_buf.is_none() && intrabc_bufs.is_none(); |
| 10071 | if split_tx_skip { |
| 10072 | if tx_select { |
| 10073 | SKIP_SPLIT_TX_HITS.with(|c| c.set(c.get() + 1)); |
| 10074 | } |
| 10075 | let n_axis = side / logical_tx; |
| 10076 | for tu_row in 0..n_axis { |
| 10077 | for tu_col in 0..n_axis { |
| 10078 | let tu_px = px + tu_col * logical_tx; |
| 10079 | let tu_py = py + tu_row * logical_tx; |
| 10080 | if tu_px >= y.true_width || tu_py >= y.true_height { |
| 10081 | continue; |
| 10082 | } |
| 10083 | let tu_reach = tu_reach( |
| 10084 | side, |
| 10085 | side, |
| 10086 | tu_col * logical_tx, |
| 10087 | tu_row * logical_tx, |
| 10088 | logical_tx, |
| 10089 | reach, |
| 10090 | px, |
| 10091 | py, |
| 10092 | y.width, |
| 10093 | y.height, |
| 10094 | ); |
| 10095 | y.reconstruct( |
| 10096 | tu_px, |
| 10097 | tu_py, |
| 10098 | logical_tx, |
| 10099 | mode, |
| 10100 | angle_delta_y, |
| 10101 | tu_reach, |
| 10102 | &vec![0i32; logical_tx * logical_tx], |
| 10103 | None, |
| 10104 | filter_intra, |
| 10105 | smooth_neighbor, |
| 10106 | ); |
| 10107 | } |
| 10108 | } |
| 10109 | } else { |
| 10110 | y.reconstruct( |
| 10111 | px, |
| 10112 | py, |
| 10113 | side, |
| 10114 | mode, |
| 10115 | angle_delta_y, |
| 10116 | reach, |
| 10117 | &vec![0i32; side * side], |
| 10118 | None, |
| 10119 | filter_intra, |
| 10120 | smooth_neighbor, |
| 10121 | ); |
| 10122 | } |
| 10123 | let ac = alpha.map(|_| cfl_ac_q3(y, px, py, side)); |
| 10124 | let cn = (chroma_side / chroma_tx).max(1); |
| 10125 | for cu_row in 0..cn { |
| 10126 | for cu_col in 0..cn { |
| 10127 | let luma_span = chroma_tx * 2; |
| 10128 | let cu_r = if cn == 1 { |
| 10129 | reach |
| 10130 | } else { |
| 10131 | tu_reach( |
| 10132 | side, side, |
| 10133 | cu_col * luma_span, cu_row * luma_span, luma_span, |
| 10134 | reach, px, py, y.width, y.height, |
| 10135 | ) |
| 10136 | }; |
| 10137 | let (cu_x, cu_y) = (cpx + cu_col * chroma_tx, cpy + cu_row * chroma_tx); |
| 10138 | if let Some((ub, _)) = &palette_uv_bufs { |
| 10139 | set_palette_pred(ub.clone()); |
| 10140 | } |
| 10141 | u.reconstruct( |
| 10142 | cu_x, |
| 10143 | cu_y, |
| 10144 | chroma_tx, |
| 10145 | uv_predict_mode, |
| 10146 | angle_delta_uv, |
| 10147 | cu_r, |
| 10148 | &vec![0i32; chroma_tx * chroma_tx], |
| 10149 | alpha.zip(ac.as_deref()).map(|((au, _), ac)| (au, ac)), |
| 10150 | None, |
| 10151 | smooth_neighbor_uv, |
| 10152 | ); |
| 10153 | if let Some((_, vb)) = &palette_uv_bufs { |
| 10154 | set_palette_pred(vb.clone()); |
| 10155 | } |
| 10156 | v.reconstruct( |
| 10157 | cu_x, |
| 10158 | cu_y, |
| 10159 | chroma_tx, |
| 10160 | uv_predict_mode, |
| 10161 | angle_delta_uv, |
| 10162 | cu_r, |
| 10163 | &vec![0i32; chroma_tx * chroma_tx], |
| 10164 | alpha.zip(ac.as_deref()).map(|((_, av), ac)| (av, ac)), |
| 10165 | None, |
| 10166 | smooth_neighbor_uv, |
| 10167 | ); |
| 10168 | } |
| 10169 | } |
| 10170 | let luma_grid = vec![0i32; side * side]; |
| 10171 | let u_grid = vec![0i32; chroma_side * chroma_side]; |
| 10172 | let v_grid = vec![0i32; chroma_side * chroma_side]; |
| 10173 | neighbours.record(at, side, mode, uv_predict_mode, &[luma_grid, u_grid, v_grid]); |
| 10174 | } else if logical_tx == side { |
| 10175 | // lane-sb128b r1: the test used to be `!tx_select || logical_tx == side`. |
| 10176 | // Without `tx_select` `logical_tx` IS `side` at every size <= 64, so |
| 10177 | // that arm was redundant -- and wrong at 128, where |
| 10178 | // `max_txsize_rect_lookup` caps the block's transform at 64 and the |
| 10179 | // block is four units whether or not `tx_select` is on. |
| 10180 | // A single luma transform unit -- the old path, unchanged (including |
| 10181 | // the 64x64 corner case, `logical_tx == side == 64` with |
| 10182 | // `coeff_tx_side == 32`). |
| 10183 | let around = neighbours.around(at, side); |
| 10184 | let luma_grid = read_plane( |
| 10185 | dec, |
| 10186 | cdfs, |
| 10187 | luma_set, |
| 10188 | luma_scan, |
| 10189 | 0, |
| 10190 | around[0], |
| 10191 | mode, |
| 10192 | mode, |
| 10193 | angle_delta_y, |
| 10194 | reach, |
| 10195 | y, |
| 10196 | px, |
| 10197 | py, |
| 10198 | side, |
| 10199 | coeff_tx_side, |
| 10200 | base_q_idx, |
| 10201 | None, |
| 10202 | filter_intra, |
| 10203 | None, |
| 10204 | smooth_neighbor, |
| 10205 | )?; |
| 10206 | let ac = alpha.map(|_| cfl_ac_q3(y, px, py, side)); |
| 10207 | if let Some((ub, _)) = &palette_uv_bufs { |
| 10208 | set_palette_pred(ub.clone()); |
| 10209 | } |
| 10210 | let u_grid = read_plane( |
| 10211 | dec, |
| 10212 | cdfs, |
| 10213 | chroma_set, |
| 10214 | scans.1, |
| 10215 | 1, |
| 10216 | around[1], |
| 10217 | mode, |
| 10218 | uv_predict_mode, |
| 10219 | angle_delta_uv, |
| 10220 | reach, |
| 10221 | u, |
| 10222 | cpx, |
| 10223 | cpy, |
| 10224 | chroma_side, |
| 10225 | chroma_tx, |
| 10226 | base_q_idx, |
| 10227 | alpha.zip(ac.as_deref()).map(|((au, _), ac)| (au, ac)), |
| 10228 | None, |
| 10229 | None, |
| 10230 | smooth_neighbor_uv, |
| 10231 | )?; |
| 10232 | if std::env::var_os("EC_AV1_TRACE").is_some() { |
| 10233 | eprintln!( |
| 10234 | "TRACE u-write cpx={cpx} cpy={cpy} chroma_side={chroma_side} row0={:?}", |
| 10235 | &u.data[cpy * u.width + cpx..][..chroma_side] |
| 10236 | ); |
| 10237 | } |
| 10238 | if let Some((_, vb)) = &palette_uv_bufs { |
| 10239 | set_palette_pred(vb.clone()); |
| 10240 | } |
| 10241 | let v_grid = read_plane( |
| 10242 | dec, |
| 10243 | cdfs, |
| 10244 | chroma_set, |
| 10245 | scans.1, |
| 10246 | 2, |
| 10247 | around[2], |
| 10248 | mode, |
| 10249 | uv_predict_mode, |
| 10250 | angle_delta_uv, |
| 10251 | reach, |
| 10252 | v, |
| 10253 | cpx, |
| 10254 | cpy, |
| 10255 | chroma_side, |
| 10256 | chroma_tx, |
| 10257 | base_q_idx, |
| 10258 | alpha.zip(ac.as_deref()).map(|((_, av), ac)| (av, ac)), |
| 10259 | None, |
| 10260 | None, |
| 10261 | smooth_neighbor_uv, |
| 10262 | )?; |
| 10263 | neighbours.record(at, side, mode, uv_predict_mode, &[luma_grid, u_grid, v_grid]); |
| 10264 | } else { |
| 10265 | // Several luma transform units, raster order (spec |
| 10266 | // `transform_block`'s loop): the block's resolved transform is |
| 10267 | // genuinely smaller than its own side, so each tile of it predicts |
| 10268 | // and codes independently, its own context read fresh off whatever |
| 10269 | // the earlier tiles in this same block already wrote -- the same |
| 10270 | // per-TU pattern [`decode_leaf8`] already runs for an 8x8 leaf. |
| 10271 | let n_axis = side / logical_tx; |
| 10272 | let cn = (chroma_side / chroma_tx).max(1); |
| 10273 | // libaom's transform-block loop is over 64x64 luma "mu" chunks |
| 10274 | // (`decode_token_recon_block`: `mu_blocks_wide/high` = |
| 10275 | // `mi_size_wide[BLOCK_64X64]`), and it codes EVERY PLANE of a chunk |
| 10276 | // before moving to the next one. Below 128 a block is a single chunk, |
| 10277 | // so this is byte-for-byte the all-luma-then-all-chroma order that has |
| 10278 | // always run; at 128 the four quadrants interleave Y,U,V per quadrant |
| 10279 | // and reading all four luma units first desyncs the tile at the second |
| 10280 | // one (lane-sb128b r3, measured against aomdec's `EC_TRACE_COEFF`). |
| 10281 | let nchunk = (side / 64).max(1); |
| 10282 | // `cn > 1` means the uv plane block is larger than the uv transform, |
| 10283 | // which moves libaom's chroma `txb_skip_ctx` offset from 7 to 10. |
| 10284 | let chroma_ctx_offset = if cn > 1 { Some(3) } else { None }; |
| 10285 | let tpc = (n_axis / nchunk).max(1); |
| 10286 | let cpc = (cn / nchunk).max(1); |
| 10287 | let mut last_grids = [Vec::new(), Vec::new()]; |
| 10288 | let mut units: Vec<((usize, usize), usize, Vec<i32>, Vec<i32>)> = Vec::new(); |
| 10289 | for ch_row in 0..nchunk { |
| 10290 | for ch_col in 0..nchunk { |
| 10291 | for tu_row in ch_row * tpc..(ch_row + 1) * tpc { |
| 10292 | for tu_col in ch_col * tpc..(ch_col + 1) * tpc { |
| 10293 | let tu_mi = ( |
| 10294 | mi_r + tu_row * (logical_tx / MI), |
| 10295 | mi_c + tu_col * (logical_tx / MI), |
| 10296 | ); |
| 10297 | let tu_px = px + tu_col * logical_tx; |
| 10298 | let tu_py = py + tu_row * logical_tx; |
| 10299 | // Same libaom clip as `decode_rect_split`'s loop above; a |
| 10300 | // square block only hangs off the frame when the frame's mi |
| 10301 | // dimensions are odd at the block's own level. |
| 10302 | if tu_px >= y.true_width || tu_py >= y.true_height { |
| 10303 | continue; |
| 10304 | } |
| 10305 | let tu_around = neighbours.around_mi(tu_mi, logical_tx)[0]; |
| 10306 | // A transform unit inside a block is NOT a block at that |
| 10307 | // position (lane-palette2 r12): `Reach::of` granted the |
| 10308 | // top-right 8x8 unit of a 16x16 D203 block bottom-left |
| 10309 | // pixels libaom refuses outright (`col_off > 0`), which is |
| 10310 | // the diagonal wedge seed 46 reconstructed wrong. |
| 10311 | let tu_reach = tu_reach( |
| 10312 | side, |
| 10313 | side, |
| 10314 | tu_col * logical_tx, |
| 10315 | tu_row * logical_tx, |
| 10316 | logical_tx, |
| 10317 | reach, |
| 10318 | px, |
| 10319 | py, |
| 10320 | y.width, |
| 10321 | y.height, |
| 10322 | ); |
| 10323 | // This transform unit's own bsize (`logical_tx`) is smaller |
| 10324 | // than the block it sits in (`side`), so `txb_skip_ctx` is |
| 10325 | // the neighbour-magnitude table, not the lone-TU 0 (spec |
| 10326 | // `get_txb_ctx_general`'s `plane_bsize != tx_size` branch). |
| 10327 | let tu_skip_ctx = neighbours.luma_skip_ctx(tu_mi, logical_tx / MI); |
| 10328 | if let Some(buf) = &palette_y_buf { |
| 10329 | PALETTE_SPLIT_TX_HITS.with(|c| c.set(c.get() + 1)); |
| 10330 | let mut window = vec![0u16; logical_tx * logical_tx]; |
| 10331 | for row in 0..logical_tx { |
| 10332 | let src = (tu_row * logical_tx + row) * side + tu_col * logical_tx; |
| 10333 | window[row * logical_tx..][..logical_tx] |
| 10334 | .copy_from_slice(&buf[src..][..logical_tx]); |
| 10335 | } |
| 10336 | set_palette_pred(window); |
| 10337 | } |
| 10338 | let tu_grid = read_plane( |
| 10339 | dec, |
| 10340 | cdfs, |
| 10341 | luma_set, |
| 10342 | luma_scan, |
| 10343 | 0, |
| 10344 | tu_around, |
| 10345 | mode, |
| 10346 | mode, |
| 10347 | angle_delta_y, |
| 10348 | tu_reach, |
| 10349 | y, |
| 10350 | tu_px, |
| 10351 | tu_py, |
| 10352 | logical_tx, |
| 10353 | // The unit's own COEFFICIENT grid, half its side at |
| 10354 | // TX_64X64 (a 64-point transform codes nothing past 32); |
| 10355 | // identical to `logical_tx` at every smaller size |
| 10356 | // (lane-sb128b r1). |
| 10357 | coeff_tx_side, |
| 10358 | base_q_idx, |
| 10359 | None, |
| 10360 | filter_intra, |
| 10361 | Some(tu_skip_ctx), |
| 10362 | smooth_neighbor, |
| 10363 | )?; |
| 10364 | if std::env::var_os("EC_AV1_TRACE").is_some() { |
| 10365 | eprintln!( |
| 10366 | "TRACE tu_bitpos mi=({},{}) tell={}", |
| 10367 | tu_mi.0, |
| 10368 | tu_mi.1, |
| 10369 | dec.debug_bitpos() |
| 10370 | ); |
| 10371 | } |
| 10372 | neighbours.record_mi_luma(tu_mi, logical_tx, &tu_grid); |
| 10373 | } |
| 10374 | } |
| 10375 | // CfL is only allowed up to 32x32, i.e. only at `nchunk == 1`, where |
| 10376 | // this still runs after every luma unit of the block. |
| 10377 | let ac = alpha.map(|_| cfl_ac_q3(y, px, py, side)); |
| 10378 | // The block's uv plane block is `chroma_side` across but its uv |
| 10379 | // transform is `chroma_tx` (`av1_get_max_uv_txsize` = |
| 10380 | // `av1_get_adjusted_tx_size(max_txsize_rect_lookup[uv_plane_bsize])`): |
| 10381 | // identical at every size up to 64x64, but a 128x128 block's 64x64 uv |
| 10382 | // plane block adjusts TX_64X64 down to TX_32X32, so each plane is |
| 10383 | // FOUR units (lane-sb128b r2) -- one per mu chunk. |
| 10384 | for cu_row in ch_row * cpc..(ch_row + 1) * cpc { |
| 10385 | for cu_col in ch_col * cpc..(ch_col + 1) * cpc { |
| 10386 | // The chroma context arrays are indexed in LUMA mi, so this |
| 10387 | // unit's anchor is its own luma quadrant: `chroma_tx` chroma |
| 10388 | // pixels span `2 * chroma_tx` luma ones. |
| 10389 | let luma_span = chroma_tx * 2; |
| 10390 | let cu_mi = ( |
| 10391 | mi_r + cu_row * (luma_span / MI), |
| 10392 | mi_c + cu_col * (luma_span / MI), |
| 10393 | ); |
| 10394 | let cu_around = neighbours.around_mi(cu_mi, luma_span); |
| 10395 | let cu_reach = if cn == 1 { |
| 10396 | reach |
| 10397 | } else { |
| 10398 | tu_reach( |
| 10399 | side, side, |
| 10400 | cu_col * luma_span, cu_row * luma_span, luma_span, |
| 10401 | reach, px, py, y.width, y.height, |
| 10402 | ) |
| 10403 | }; |
| 10404 | let (cu_x, cu_y) = (cpx + cu_col * chroma_tx, cpy + cu_row * chroma_tx); |
| 10405 | if let Some((ub, _)) = &palette_uv_bufs { |
| 10406 | set_palette_pred(ub.clone()); |
| 10407 | } |
| 10408 | let u_grid = read_plane( |
| 10409 | dec, cdfs, chroma_set, scans.1, 1, cu_around[1], mode, |
| 10410 | uv_predict_mode, angle_delta_uv, cu_reach, u, cu_x, cu_y, |
| 10411 | chroma_tx, chroma_tx, base_q_idx, |
| 10412 | alpha.zip(ac.as_deref()).map(|((au, _), ac)| (au, ac)), |
| 10413 | None, chroma_ctx_offset, smooth_neighbor_uv, |
| 10414 | )?; |
| 10415 | if let Some((_, vb)) = &palette_uv_bufs { |
| 10416 | set_palette_pred(vb.clone()); |
| 10417 | } |
| 10418 | let v_grid = read_plane( |
| 10419 | dec, cdfs, chroma_set, scans.1, 2, cu_around[2], mode, |
| 10420 | uv_predict_mode, angle_delta_uv, cu_reach, v, cu_x, cu_y, |
| 10421 | chroma_tx, chroma_tx, base_q_idx, |
| 10422 | alpha.zip(ac.as_deref()).map(|((_, av), ac)| (av, ac)), |
| 10423 | None, chroma_ctx_offset, smooth_neighbor_uv, |
| 10424 | )?; |
| 10425 | if cn > 1 { |
| 10426 | CHROMA_SPLIT_TX_HITS.with(|c| c.set(c.get() + 1)); |
| 10427 | // Immediately, so the NEXT unit of this same block reads |
| 10428 | // this one's coefficient context (libaom's chroma |
| 10429 | // `txb_skip_ctx` for the second quadrant is 11, i.e. base |
| 10430 | // 1 from this left neighbour -- measured against aomdec). |
| 10431 | // The same writes are replayed after `record_split_luma` |
| 10432 | // below, which would otherwise clobber them whole-block. |
| 10433 | neighbours.record_mi_chroma(cu_mi, luma_span, luma_span, 1, &u_grid); |
| 10434 | neighbours.record_mi_chroma(cu_mi, luma_span, luma_span, 2, &v_grid); |
| 10435 | units.push((cu_mi, luma_span, u_grid.clone(), v_grid.clone())); |
| 10436 | } |
| 10437 | last_grids = [u_grid, v_grid]; |
| 10438 | } |
| 10439 | } |
| 10440 | } |
| 10441 | } |
| 10442 | // Mode/side bookkeeping plus, at `cn == 1`, the whole-plane chroma |
| 10443 | // context write; at `cn > 1` the per-unit writes above are what the |
| 10444 | // next block reads, so they run after this (last writer wins). |
| 10445 | neighbours.record_split_luma(at, side, mode, uv_predict_mode, [&last_grids[0], &last_grids[1]]); |
| 10446 | for (cu_mi, luma_span, u_grid, v_grid) in &units { |
| 10447 | neighbours.record_mi_chroma(*cu_mi, *luma_span, *luma_span, 1, u_grid); |
| 10448 | neighbours.record_mi_chroma(*cu_mi, *luma_span, *luma_span, 2, v_grid); |
| 10449 | } |
| 10450 | } |
| 10451 | // Unconditional, `size == 0` for a non-palette block: clears any stale |
| 10452 | // neighbour palette state the same way [`Neighbours::record`] always |
| 10453 | // overwrites `above_mode`/`left_mode` regardless of this block's own mode |
| 10454 | // (the split-luma-TU branch above always refuses before reaching here |
| 10455 | // when `palette_y` is `Some`, so it is never live for that branch). |
| 10456 | let (py_size, py_colors) = palette_y |
| 10457 | .as_ref() |
| 10458 | .map_or((0, [0u16; 8]), |p| (p.size, p.colors)); |
| 10459 | neighbours.record_palette_y(at, side, py_size, py_colors); |
| 10460 | let (puv_size, puv_colors) = palette_uv |
| 10461 | .as_ref() |
| 10462 | .map_or((0, [0u16; 8]), |p| (p.size, p.u_colors)); |
| 10463 | neighbours.record_palette_uv(at, side, puv_size, puv_colors); |
| 10464 | neighbours.fill_skip_grid((r * (SUB / MI), c * (SUB / MI)), side / MI, skip); |
| 10465 | neighbours.fill_lf_grid( |
| 10466 | (r * (SUB / MI), c * (SUB / MI)), |
| 10467 | side / MI, |
| 10468 | logical_tx as u8, |
| 10469 | 0, |
| 10470 | ); |
| 10471 | record_intrabc_mi(mi_r, mi_c, side / MI, intrabc_dv); |
| 10472 | Ok(()) |
| 10473 | } |
| 10474 | |
| 10475 | /// Decodes one 8x8 leaf of a straddling 16x16 block (lane-av1-rect), mirroring |
| 10476 | /// [`crate::tile::write_leaf8`]: its intra-mode context comes from the |
| 10477 | /// *enclosing* 16x16 slot's `above_mode`/`left_mode` (`outer_at`, [`SUB`]-grid), |
| 10478 | /// overridden on whichever axis `prev_leaf` sits along -- the previous leaf's |
| 10479 | /// just-decoded mode, not the enclosing slot's stale one, is the true |
| 10480 | /// neighbour there. `leaf_mi` is this leaf's own position in 4x4 mode-info |
| 10481 | /// units, which is what its coefficient context (finer than [`SUB`]) is read |
| 10482 | /// at. Returns the mode this leaf decoded, for the caller's own |
| 10483 | /// `prev_leaf`/final mode-writeback bookkeeping. |
| 10484 | #[allow(clippy::too_many_arguments)] |
| 10485 | /// The coarse 16x16 `above_mode`/`left_mode` cells a four-way SPLIT of one |
| 10486 | /// 16x16 parent leaves behind (lane-rectx r3). The next block DOWN reads |
| 10487 | /// `above_mode[col]`, and its true above neighbour is the mi directly above |
| 10488 | /// its own top-left corner -- the parent's BOTTOM-LEFT leaf, not the last one |
| 10489 | /// decoded; the next block RIGHT reads `left_mode[row]`, whose true left |
| 10490 | /// neighbour is the parent's TOP-RIGHT leaf. Writing the z-order last leaf |
| 10491 | /// (bottom-right) into both, as this path did, is right only when all four |
| 10492 | /// leaves happen to share a mode. `done` may be short at the true frame edge |
| 10493 | /// (the straddle caller filters positions off the frame), so each side falls |
| 10494 | /// back to the last leaf actually decoded. |
| 10495 | fn write_back_split_modes( |
| 10496 | neighbours: &mut Neighbours, |
| 10497 | done: &[((usize, usize), usize)], |
| 10498 | parent_mi: (usize, usize), |
| 10499 | slot: (usize, usize), |
| 10500 | ) { |
| 10501 | let (sr, sc) = slot; |
| 10502 | let Some(&(_, last)) = done.last() else { |
| 10503 | return; |
| 10504 | }; |
| 10505 | let find = |mi: (usize, usize)| done.iter().find(|&&(at, _)| at == mi).map(|&(_, m)| m); |
| 10506 | neighbours.above_mode[sc] = find((parent_mi.0 + 2, parent_mi.1)).unwrap_or(last); |
| 10507 | neighbours.left_mode[sr] = find((parent_mi.0, parent_mi.1 + 2)).unwrap_or(last); |
| 10508 | } |
| 10509 | |
| 10510 | fn decode_leaf8( |
| 10511 | dec: &mut SymbolDecoder, |
| 10512 | cdfs: &mut Cdfs, |
| 10513 | neighbours: &mut Neighbours, |
| 10514 | outer_at: (usize, usize), |
| 10515 | leaf_mi: (usize, usize), |
| 10516 | scans: (&[u16], &[u16]), |
| 10517 | prev_leaf: Option<((usize, usize), usize)>, |
| 10518 | y: &mut PlaneBuf, |
| 10519 | u: &mut PlaneBuf, |
| 10520 | v: &mut PlaneBuf, |
| 10521 | base_q_idx: u8, |
| 10522 | enable_filter_intra: bool, |
| 10523 | allow_screen_content_tools: bool, |
| 10524 | allow_intrabc: bool, |
| 10525 | tx_select: bool, |
| 10526 | reduced_tx_set: bool, |
| 10527 | ) -> Result<usize> { |
| 10528 | let (r, c) = outer_at; |
| 10529 | // lane-modectx r1: the group's own availability-correct fallback pair, not |
| 10530 | // the raw coarse bands (which leak across a tile edge -- see |
| 10531 | // `modes_above_left`). |
| 10532 | let (mut above_mode, mut left_mode) = neighbours.modes_above_left(r, c); |
| 10533 | if let Some(((pr, pc), pmode)) = prev_leaf { |
| 10534 | if pc == leaf_mi.1 && leaf_mi.0 == pr + 2 { |
| 10535 | above_mode = pmode; |
| 10536 | } else if pr == leaf_mi.0 && leaf_mi.1 == pc + 2 { |
| 10537 | left_mode = pmode; |
| 10538 | } |
| 10539 | } |
| 10540 | // The mi-exact neighbour wins over both the coarse 16x16 slot and |
| 10541 | // `prev_leaf`: one 16x16 slot cannot hold the two different modes its |
| 10542 | // two 8x8 (or four 4x4) columns leave behind, so a leaf whose above/left |
| 10543 | // neighbour sits in a split cell read the wrong `kf_y_mode` row. |
| 10544 | // lane-rectsplitx r2 swept this arm into |
| 10545 | // [`Neighbours::modes_above_left_mi`]: the mi-exact map ALSO has to answer |
| 10546 | // "no neighbour" at a tile edge, where the coarse cell holds the block |
| 10547 | // above/left of it inside the same 16x16. |
| 10548 | let (above_mode, left_mode) = |
| 10549 | neighbours.modes_above_left_mi(leaf_mi.0, leaf_mi.1, (above_mode, left_mode)); |
| 10550 | let smooth_neighbor = is_smooth_mode(above_mode) || is_smooth_mode(left_mode); |
| 10551 | // Chroma's edge-filter type reads the CHROMA neighbour's `uv_mode` |
| 10552 | // (`get_intra_edge_filter_type`, reconintra.c:974), never the luma mode |
| 10553 | // this leaf's `smooth_neighbor` is built from -- passing `false` here |
| 10554 | // filtered a directional chroma block's edge at the wrong strength |
| 10555 | // (lane-sub8 r5: 4 pixels of one 8x8 leaf's chroma, deltas 1..3). |
| 10556 | // The `uv_mode` neighbours are mi-exact (`uv_mode_col`/`uv_mode_row`), |
| 10557 | // with the coarse [`SUB`] slot as the fallback when no block recorded |
| 10558 | // that exact mi cell. |
| 10559 | let smooth_neighbor_uv = neighbours.smooth_uv_neighbour(leaf_mi.0, leaf_mi.1, r, c); |
| 10560 | if smooth_neighbor { |
| 10561 | SMOOTH_LUMA_HITS.with(|c| c.set(c.get() + 1)); |
| 10562 | } |
| 10563 | // An 8x8 leaf is well within `is_cfl_allowed`'s <=32x32 bound (spec |
| 10564 | // 5.11.5), so it reads the CFL-allowed `uv_mode_cfl` CDF, like every other |
| 10565 | // `decode_block` caller at 16x16 and up. |
| 10566 | let (skip, mode, angle_delta_y, uv_mode, angle_delta_uv, alpha, filter_intra, _palette_y, _palette_uv) = |
| 10567 | read_intra_mode( |
| 10568 | dec, |
| 10569 | cdfs, |
| 10570 | above_mode, |
| 10571 | left_mode, |
| 10572 | true, |
| 10573 | 8, |
| 10574 | enable_filter_intra, |
| 10575 | neighbours.skip_txfm_ctx(leaf_mi.0, leaf_mi.1), |
| 10576 | allow_screen_content_tools, |
| 10577 | allow_intrabc, |
| 10578 | None, |
| 10579 | &[], |
| 10580 | leaf_mi.0, |
| 10581 | leaf_mi.1, |
| 10582 | )?; |
| 10583 | let uv_predict_mode = if uv_mode == UV_CFL_PRED { |
| 10584 | DC_PRED |
| 10585 | } else { |
| 10586 | uv_mode |
| 10587 | }; |
| 10588 | neighbours.above_uv_mode[c] = uv_predict_mode; |
| 10589 | neighbours.left_uv_mode[r] = uv_predict_mode; |
| 10590 | neighbours.record_uv_mode_mi(leaf_mi.0, leaf_mi.1, 2, 2, uv_predict_mode); |
| 10591 | // `TxMode::Select`'s `tx_depth` at an 8x8 leaf (spec 5.11.16): the only |
| 10592 | // depths an 8x8 block offers are `TX8` (depth 0) and `TX4` (depth 1, |
| 10593 | // which splits the leaf's own 8x8 prediction into a 2x2 grid of 4x4 |
| 10594 | // transform units, same raster-order per-TU pattern `decode_block`'s |
| 10595 | // multi-TU branch runs). |
| 10596 | let resolved = if tx_select { |
| 10597 | read_tx_size(dec, cdfs, neighbours, leaf_mi, 8, None) |
| 10598 | } else { |
| 10599 | 8 |
| 10600 | }; |
| 10601 | let (px, py) = (leaf_mi.1 * MI, leaf_mi.0 * MI); |
| 10602 | let reach = Reach::of(8, px, py, y.width, y.height); |
| 10603 | let (cpx, cpy) = (px / 2, py / 2); |
| 10604 | let (luma_grid, u_grid, v_grid); |
| 10605 | if skip { |
| 10606 | // lane-hgkf r2 (same shape as `decode_block`'s own skip branch): an |
| 10607 | // 8x8 leaf whose `tx_depth` resolved to TX_4X4 predicts FOUR 4x4 |
| 10608 | // transform units, each off the units already written left/above of |
| 10609 | // it inside this same leaf -- an intra block's transform size is read |
| 10610 | // whether or not it is skipped, and spec 7.11.2 predicts per unit. |
| 10611 | if resolved == 4 { |
| 10612 | SKIP_SPLIT_TX_HITS.with(|c| c.set(c.get() + 1)); |
| 10613 | for tu_row in 0..2 { |
| 10614 | for tu_col in 0..2 { |
| 10615 | let (tu_px, tu_py) = (px + tu_col * 4, py + tu_row * 4); |
| 10616 | if tu_px >= y.true_width || tu_py >= y.true_height { |
| 10617 | continue; |
| 10618 | } |
| 10619 | let tu_reach = tu_reach( |
| 10620 | 8, |
| 10621 | 8, |
| 10622 | tu_col * 4, |
| 10623 | tu_row * 4, |
| 10624 | 4, |
| 10625 | reach, |
| 10626 | px, |
| 10627 | py, |
| 10628 | y.width, |
| 10629 | y.height, |
| 10630 | ); |
| 10631 | y.reconstruct( |
| 10632 | tu_px, |
| 10633 | tu_py, |
| 10634 | 4, |
| 10635 | mode, |
| 10636 | angle_delta_y, |
| 10637 | tu_reach, |
| 10638 | &vec![0i32; 16], |
| 10639 | None, |
| 10640 | filter_intra, |
| 10641 | smooth_neighbor, |
| 10642 | ); |
| 10643 | } |
| 10644 | } |
| 10645 | } else { |
| 10646 | y.reconstruct( |
| 10647 | px, |
| 10648 | py, |
| 10649 | 8, |
| 10650 | mode, |
| 10651 | angle_delta_y, |
| 10652 | reach, |
| 10653 | &vec![0i32; 64], |
| 10654 | None, |
| 10655 | filter_intra, |
| 10656 | smooth_neighbor, |
| 10657 | ); |
| 10658 | } |
| 10659 | let ac = alpha.map(|_| cfl_ac_q3(y, px, py, 8)); |
| 10660 | u.reconstruct( |
| 10661 | cpx, |
| 10662 | cpy, |
| 10663 | 4, |
| 10664 | uv_predict_mode, |
| 10665 | angle_delta_uv, |
| 10666 | reach, |
| 10667 | &vec![0i32; 16], |
| 10668 | alpha.zip(ac.as_deref()).map(|((au, _), ac)| (au, ac)), |
| 10669 | None, |
| 10670 | smooth_neighbor_uv, |
| 10671 | ); |
| 10672 | v.reconstruct( |
| 10673 | cpx, |
| 10674 | cpy, |
| 10675 | 4, |
| 10676 | uv_predict_mode, |
| 10677 | angle_delta_uv, |
| 10678 | reach, |
| 10679 | &vec![0i32; 16], |
| 10680 | alpha.zip(ac.as_deref()).map(|((_, av), ac)| (av, ac)), |
| 10681 | None, |
| 10682 | smooth_neighbor_uv, |
| 10683 | ); |
| 10684 | luma_grid = vec![0i32; 64]; |
| 10685 | u_grid = vec![0i32; 16]; |
| 10686 | v_grid = vec![0i32; 16]; |
| 10687 | } else if resolved != 4 { |
| 10688 | let around = neighbours.around_mi(leaf_mi, 8); |
| 10689 | luma_grid = read_plane( |
| 10690 | dec, |
| 10691 | cdfs, |
| 10692 | if reduced_tx_set { |
| 10693 | TxbSet::Luma8 |
| 10694 | } else { |
| 10695 | TxbSet::Luma8Set1 |
| 10696 | }, |
| 10697 | scans.0, |
| 10698 | 0, |
| 10699 | around[0], |
| 10700 | mode, |
| 10701 | mode, |
| 10702 | angle_delta_y, |
| 10703 | reach, |
| 10704 | y, |
| 10705 | px, |
| 10706 | py, |
| 10707 | 8, |
| 10708 | TX8, |
| 10709 | base_q_idx, |
| 10710 | None, |
| 10711 | filter_intra, |
| 10712 | None, |
| 10713 | smooth_neighbor, |
| 10714 | )?; |
| 10715 | let ac = alpha.map(|_| cfl_ac_q3(y, px, py, 8)); |
| 10716 | u_grid = read_plane( |
| 10717 | dec, |
| 10718 | cdfs, |
| 10719 | TxbSet::Chroma4, |
| 10720 | scans.1, |
| 10721 | 1, |
| 10722 | around[1], |
| 10723 | mode, |
| 10724 | uv_predict_mode, |
| 10725 | angle_delta_uv, |
| 10726 | reach, |
| 10727 | u, |
| 10728 | cpx, |
| 10729 | cpy, |
| 10730 | 4, |
| 10731 | TX4, |
| 10732 | base_q_idx, |
| 10733 | alpha.zip(ac.as_deref()).map(|((au, _), ac)| (au, ac)), |
| 10734 | None, |
| 10735 | None, |
| 10736 | smooth_neighbor_uv, |
| 10737 | )?; |
| 10738 | v_grid = read_plane( |
| 10739 | dec, |
| 10740 | cdfs, |
| 10741 | TxbSet::Chroma4, |
| 10742 | scans.1, |
| 10743 | 2, |
| 10744 | around[2], |
| 10745 | mode, |
| 10746 | uv_predict_mode, |
| 10747 | angle_delta_uv, |
| 10748 | reach, |
| 10749 | v, |
| 10750 | cpx, |
| 10751 | cpy, |
| 10752 | 4, |
| 10753 | TX4, |
| 10754 | base_q_idx, |
| 10755 | alpha.zip(ac.as_deref()).map(|((_, av), ac)| (av, ac)), |
| 10756 | None, |
| 10757 | None, |
| 10758 | smooth_neighbor_uv, |
| 10759 | )?; |
| 10760 | } else { |
| 10761 | // `tx_depth` resolved this leaf's luma to `TX4`: a 2x2 raster-order |
| 10762 | // grid of 4x4 transform units, each predicted from the leaf's own |
| 10763 | // 8x8 `mode` and reading its own fresh context off the earlier |
| 10764 | // units in this same leaf (the same per-TU pattern `decode_block`'s |
| 10765 | // multi-TU branch runs at bigger blocks). |
| 10766 | luma_grid = vec![0i32; 64]; |
| 10767 | for tu_row in 0..2 { |
| 10768 | for tu_col in 0..2 { |
| 10769 | let tu_mi = (leaf_mi.0 + tu_row, leaf_mi.1 + tu_col); |
| 10770 | let tu_px = px + tu_col * 4; |
| 10771 | let tu_py = py + tu_row * 4; |
| 10772 | let tu_around = neighbours.around_mi(tu_mi, 4)[0]; |
| 10773 | if crate::encode::Reach::in_vert_ab() { |
| 10774 | VERT_AB_TX4_HITS.with(|c| c.set(c.get() + 1)); |
| 10775 | } |
| 10776 | let tu_reach = tu_reach( |
| 10777 | 8, |
| 10778 | 8, |
| 10779 | tu_col * 4, |
| 10780 | tu_row * 4, |
| 10781 | 4, |
| 10782 | reach, |
| 10783 | px, |
| 10784 | py, |
| 10785 | y.width, |
| 10786 | y.height, |
| 10787 | ); |
| 10788 | let tu_skip_ctx = neighbours.luma_skip_ctx(tu_mi, 1); |
| 10789 | let tu_grid = read_plane( |
| 10790 | dec, |
| 10791 | cdfs, |
| 10792 | if reduced_tx_set { |
| 10793 | TxbSet::Luma4 |
| 10794 | } else { |
| 10795 | TxbSet::Luma4Set1 |
| 10796 | }, |
| 10797 | scans.1, |
| 10798 | 0, |
| 10799 | tu_around, |
| 10800 | mode, |
| 10801 | mode, |
| 10802 | angle_delta_y, |
| 10803 | tu_reach, |
| 10804 | y, |
| 10805 | tu_px, |
| 10806 | tu_py, |
| 10807 | 4, |
| 10808 | 4, |
| 10809 | base_q_idx, |
| 10810 | None, |
| 10811 | filter_intra, |
| 10812 | Some(tu_skip_ctx), |
| 10813 | smooth_neighbor, |
| 10814 | )?; |
| 10815 | if std::env::var_os("EC_AV1_TRACE").is_some() { |
| 10816 | eprintln!( |
| 10817 | "TRACE tu_bitpos mi=({},{}) tell={}", |
| 10818 | tu_mi.0, |
| 10819 | tu_mi.1, |
| 10820 | dec.debug_bitpos() |
| 10821 | ); |
| 10822 | } |
| 10823 | neighbours.record_mi_luma(tu_mi, 4, &tu_grid); |
| 10824 | } |
| 10825 | } |
| 10826 | let ac = alpha.map(|_| cfl_ac_q3(y, px, py, 8)); |
| 10827 | let chroma_around = neighbours.around_mi(leaf_mi, 8); |
| 10828 | u_grid = read_plane( |
| 10829 | dec, |
| 10830 | cdfs, |
| 10831 | TxbSet::Chroma4, |
| 10832 | scans.1, |
| 10833 | 1, |
| 10834 | chroma_around[1], |
| 10835 | mode, |
| 10836 | uv_predict_mode, |
| 10837 | angle_delta_uv, |
| 10838 | reach, |
| 10839 | u, |
| 10840 | cpx, |
| 10841 | cpy, |
| 10842 | 4, |
| 10843 | TX4, |
| 10844 | base_q_idx, |
| 10845 | alpha.zip(ac.as_deref()).map(|((au, _), ac)| (au, ac)), |
| 10846 | None, |
| 10847 | None, |
| 10848 | smooth_neighbor_uv, |
| 10849 | )?; |
| 10850 | v_grid = read_plane( |
| 10851 | dec, |
| 10852 | cdfs, |
| 10853 | TxbSet::Chroma4, |
| 10854 | scans.1, |
| 10855 | 2, |
| 10856 | chroma_around[2], |
| 10857 | mode, |
| 10858 | uv_predict_mode, |
| 10859 | angle_delta_uv, |
| 10860 | reach, |
| 10861 | v, |
| 10862 | cpx, |
| 10863 | cpy, |
| 10864 | 4, |
| 10865 | TX4, |
| 10866 | base_q_idx, |
| 10867 | alpha.zip(ac.as_deref()).map(|((_, av), ac)| (av, ac)), |
| 10868 | None, |
| 10869 | None, |
| 10870 | smooth_neighbor_uv, |
| 10871 | )?; |
| 10872 | // `record_split_luma` expects a `record`-style `(r, c)` position in |
| 10873 | // `SUB`-grid units and rescales it by `SUB / MI`; `leaf_mi` is |
| 10874 | // already in mi units, so calling it here double-scaled the |
| 10875 | // position. Plane 0 is already correct per-TU from the |
| 10876 | // `record_mi_luma` calls above; this inlines `record_mi`'s tail |
| 10877 | // (spec `get_txb_ctx_general`'s neighbour-magnitude bookkeeping) for |
| 10878 | // the two chroma planes only, at `leaf_mi` directly, side_mi=2 (this |
| 10879 | // leaf's own 8x8 side over `MI`). |
| 10880 | let side_mi = 2; |
| 10881 | for cell in 0..side_mi { |
| 10882 | neighbours.left_side_mi[leaf_mi.0 + cell] = 8; |
| 10883 | neighbours.above_side_mi[leaf_mi.1 + cell] = 8; |
| 10884 | } |
| 10885 | let round_up_even = |n: usize| n.div_ceil(2) * 2; |
| 10886 | let bound_h = round_up_even(neighbours.mi_rows); |
| 10887 | let bound_w = round_up_even(neighbours.mi_cols); |
| 10888 | let u_state = neighbour_state(&u_grid); |
| 10889 | let v_state = neighbour_state(&v_grid); |
| 10890 | for cell in 0..side_mi { |
| 10891 | if cell < side_mi.min(bound_h.saturating_sub(leaf_mi.0)) { |
| 10892 | neighbours.left[leaf_mi.0 + cell][1] = u_state; |
| 10893 | neighbours.left[leaf_mi.0 + cell][2] = v_state; |
| 10894 | } |
| 10895 | if cell < side_mi.min(bound_w.saturating_sub(leaf_mi.1)) { |
| 10896 | neighbours.above[leaf_mi.1 + cell][1] = u_state; |
| 10897 | neighbours.above[leaf_mi.1 + cell][2] = v_state; |
| 10898 | } |
| 10899 | } |
| 10900 | // Non-palette 8x8 leaf (this path never reads a palette): clear the |
| 10901 | // above/left palette state over its own cell so the next block's ctx and |
| 10902 | // colour cache do not read a previous block's palette (lane-palette2 r8). |
| 10903 | { |
| 10904 | let pal_at = (leaf_mi.0 / (SUB / MI), leaf_mi.1 / (SUB / MI)); |
| 10905 | neighbours.record_palette_y_rect(pal_at, 8, 8, 0, [0u16; 8]); |
| 10906 | neighbours.record_palette_uv_rect(pal_at, 8, 8, 0, [0u16; 8]); |
| 10907 | } |
| 10908 | neighbours.fill_skip_grid(leaf_mi, 2, skip); |
| 10909 | neighbours.fill_lf_grid(leaf_mi, 2, 4, 0); |
| 10910 | neighbours.record_mode_mi(leaf_mi.0, leaf_mi.1, 2, 2, mode); |
| 10911 | return Ok(mode); |
| 10912 | } |
| 10913 | neighbours.record_mi(leaf_mi, 8, &[luma_grid, u_grid, v_grid]); |
| 10914 | // Non-palette 8x8 leaf (this path never reads a palette): clear the |
| 10915 | // above/left palette state over its own cell so the next block's ctx and |
| 10916 | // colour cache do not read a previous block's palette (lane-palette2 r8). |
| 10917 | { |
| 10918 | let pal_at = (leaf_mi.0 / (SUB / MI), leaf_mi.1 / (SUB / MI)); |
| 10919 | neighbours.record_palette_y_rect(pal_at, 8, 8, 0, [0u16; 8]); |
| 10920 | neighbours.record_palette_uv_rect(pal_at, 8, 8, 0, [0u16; 8]); |
| 10921 | } |
| 10922 | INTER8_SKIP_BAND_HITS.with(|c| c.set(c.get() + 1)); |
| 10923 | neighbours.fill_skip_grid(leaf_mi, 2, skip); |
| 10924 | // lane-kf900 r1: the leaf's own RESOLVED transform size, never the literal |
| 10925 | // block side. libaom calls `set_txfm_ctxs(mbmi->tx_size, ..., skip_txfm && |
| 10926 | // is_inter_block(mbmi), xd)` (decodemv.c) -- for an INTRA block that skip |
| 10927 | // term is always 0, so a skipped 8x8 leaf whose `tx_depth` resolved to |
| 10928 | // TX_4X4 still publishes 4 to the next block's `get_tx_size_context` |
| 10929 | // (pred_common.h:342). Writing 8 here made the 8x16 strip below the leaf |
| 10930 | // read `tx_size_cat1` row 2 where libaom reads row 1 -- same alphabet, |
| 10931 | // wrong CDF row, so the msac range diverged mid-frame with no refusal |
| 10932 | // (class `wrong-alphabet-same-value`; the ReelB key frame at |
| 10933 | // -ss 900, first bad luma sample row 186 col 1803). |
| 10934 | neighbours.fill_lf_grid(leaf_mi, 2, resolved as u8, 0); |
| 10935 | neighbours.record_mode_mi(leaf_mi.0, leaf_mi.1, 2, 2, mode); |
| 10936 | Ok(mode) |
| 10937 | } |
| 10938 | |
| 10939 | /// `read_intra_frame_mode_info`'s mode-info read for one `BLOCK_4X4` leaf |
| 10940 | /// below an 8x8 partition (spec `decode_partition`'s `bSize < BLOCK_8X8` |
| 10941 | /// bottom, where no partition symbol is read at all -- `decode_block` runs |
| 10942 | /// directly). No palette (`av1_allow_palette` needs both dims >= 8, spec |
| 10943 | /// blockd.h) and no `read_tx_size` (a sub-8x8 block's transform is always |
| 10944 | /// its own bsize, spec 5.11.16). `has_chroma` follows `is_chroma_reference` |
| 10945 | /// (spec/libaom `av1_common_int.h`): for 4:2:0, only the bottom-right leaf |
| 10946 | /// of the 2x2 4x4 group carries chroma syntax at all. |
| 10947 | #[allow(clippy::too_many_arguments)] |
| 10948 | fn read_intra_mode_sub8( |
| 10949 | dec: &mut SymbolDecoder, |
| 10950 | cdfs: &mut Cdfs, |
| 10951 | above_mode: usize, |
| 10952 | left_mode: usize, |
| 10953 | has_chroma: bool, |
| 10954 | enable_filter_intra: bool, |
| 10955 | // [`filter_intra_size_class_rect`] for the LEAF's own shape: 0 for a |
| 10956 | // `BLOCK_4X4` split leaf, 8/9 for a 4x8/8x4 rect leaf. Reading the flag |
| 10957 | // from the wrong row is the same VALUE with a different interval, so it |
| 10958 | // desyncs a block or two later, not here (class `wrong-alphabet-same-value`). |
| 10959 | fi_class: usize, |
| 10960 | skip_ctx: usize, |
| 10961 | allow_intrabc: bool, |
| 10962 | mi_r: usize, |
| 10963 | mi_c: usize, |
| 10964 | ) -> Result<(bool, usize, i32, Option<(usize, i32, Option<(i32, i32)>)>, Option<usize>)> { |
| 10965 | let trace = std::env::var_os("EC_AV1_TRACE").is_some(); |
| 10966 | if std::env::var_os("EC_TRACE_MODE_STEP").is_some() { |
| 10967 | eprintln!("EC_IMODE mi_row={mi_r} mi_col={mi_c} fn=sub8 rng={}", dec.debug_state().0); |
| 10968 | } |
| 10969 | let skip = dec.symbol(&mut cdfs.skip[skip_ctx]) != 0; |
| 10970 | if trace { |
| 10971 | eprintln!("TRACE sub8 skip mi=({mi_r},{mi_c}) ctx={skip_ctx} value={} rng={}", skip as i32, dec.debug_state().0); |
| 10972 | } |
| 10973 | maybe_read_cdef_idx(dec, mi_r, mi_c, skip); |
| 10974 | maybe_read_delta_q(dec, cdfs, mi_r, mi_c, false, skip); |
| 10975 | maybe_read_delta_lf(dec, cdfs, mi_r, mi_c, false, skip); |
| 10976 | if allow_intrabc { |
| 10977 | let use_intrabc = dec.symbol(&mut cdfs.intrabc) != 0; |
| 10978 | if use_intrabc { |
| 10979 | return Err(unsupported( |
| 10980 | "a sub-8x8 leaf that uses intrabc (this reader has no block-vector path; the 8x8-and-up reader reconstructs one)", |
| 10981 | )); |
| 10982 | } |
| 10983 | } |
| 10984 | let above_ctx = INTRA_MODE_CTX[above_mode]; |
| 10985 | let left_ctx = INTRA_MODE_CTX[left_mode]; |
| 10986 | let mode = dec.symbol(&mut cdfs.kf_y_mode[above_ctx][left_ctx]); |
| 10987 | if trace { |
| 10988 | eprintln!("TRACE sub8 y_mode mi=({mi_r},{mi_c}) ctx=({above_ctx},{left_ctx}) value={mode} rng={}", dec.debug_state().0); |
| 10989 | } |
| 10990 | // No `angle_delta` at all below 8x8: spec 5.11.6's `intra_angle_info_y`/ |
| 10991 | // `_uv` are gated on `MiSize >= BLOCK_8X8` (libaom `av1_use_angle_delta`, |
| 10992 | // blockd.h), so a directional mode on a `BLOCK_4X4` leaf carries no delta |
| 10993 | // symbol. Reading one consumed a symbol the encoder never wrote and |
| 10994 | // desynced the tile at the very next element. |
| 10995 | let angle_delta_y = 0; |
| 10996 | let chroma = if has_chroma { |
| 10997 | let uv_mode = dec.symbol(&mut cdfs.uv_mode_cfl[mode]); |
| 10998 | if trace { |
| 10999 | eprintln!("TRACE sub8 uv_mode value={uv_mode} rng={}", dec.debug_state().0); |
| 11000 | } |
| 11001 | if (9..=12).contains(&uv_mode) { |
| 11002 | SMOOTH_UV_HITS.with(|c| c.set(c.get() + 1)); |
| 11003 | } |
| 11004 | let alpha = if uv_mode == UV_CFL_PRED { |
| 11005 | Some(read_cfl_alphas(dec, cdfs)) |
| 11006 | } else { |
| 11007 | None |
| 11008 | }; |
| 11009 | if (V_PRED..=D67_PRED).contains(&uv_mode) { |
| 11010 | DIRECTIONAL_UV_HITS.with(|c| c.set(c.get() + 1)); |
| 11011 | } |
| 11012 | // See the luma comment above: no angle delta below 8x8. |
| 11013 | let angle_delta_uv = 0; |
| 11014 | Some((uv_mode, angle_delta_uv, alpha)) |
| 11015 | } else { |
| 11016 | None |
| 11017 | }; |
| 11018 | let mut filter_intra = None; |
| 11019 | if mode == DC_PRED && enable_filter_intra { |
| 11020 | let use_filter_intra = dec.symbol(&mut cdfs.filter_intra[fi_class]) != 0; |
| 11021 | if trace { |
| 11022 | eprintln!("TRACE sub8 use_filter_intra value={}", use_filter_intra as i32); |
| 11023 | } |
| 11024 | if use_filter_intra { |
| 11025 | FILTER_INTRA_HITS.with(|c| c.set(c.get() + 1)); |
| 11026 | let fi_mode = dec.symbol(&mut cdfs.filter_intra_mode); |
| 11027 | filter_intra = Some(fi_mode); |
| 11028 | } |
| 11029 | } |
| 11030 | Ok((skip, mode, angle_delta_y, chroma, filter_intra)) |
| 11031 | } |
| 11032 | |
| 11033 | /// Decodes a `PARTITION_SPLIT` of one 8x8 block into four `BLOCK_4X4` leaves |
| 11034 | /// (spec `decode_partition`'s recursion bottom), sharing [`decode_leaf8`]'s |
| 11035 | /// own signature so its caller's `prev_leaf`/`above_mode`/`left_mode` |
| 11036 | /// bookkeeping needs no change. Each leaf reads its own luma mode info, |
| 11037 | /// neighboured off the *previously decoded leaf in this same group* on |
| 11038 | /// whichever axis is adjacent (mirroring [`decode_leaf8`]'s own `prev_leaf` |
| 11039 | /// pattern, one level deeper); chroma is read and reconstructed exactly |
| 11040 | /// once, on the last (bottom-right) leaf, at the whole 8x8 group's own 4x4 |
| 11041 | /// chroma unit -- [`read_intra_mode_sub8`]'s doc has the `is_chroma_reference` |
| 11042 | /// derivation. |
| 11043 | #[allow(clippy::too_many_arguments)] |
| 11044 | fn decode_leaf_split4( |
| 11045 | dec: &mut SymbolDecoder, |
| 11046 | cdfs: &mut Cdfs, |
| 11047 | neighbours: &mut Neighbours, |
| 11048 | outer_at: (usize, usize), |
| 11049 | leaf_mi: (usize, usize), |
| 11050 | scan4: &[u16], |
| 11051 | prev_leaf: Option<((usize, usize), usize)>, |
| 11052 | y: &mut PlaneBuf, |
| 11053 | u: &mut PlaneBuf, |
| 11054 | v: &mut PlaneBuf, |
| 11055 | base_q_idx: u8, |
| 11056 | enable_filter_intra: bool, |
| 11057 | allow_intrabc: bool, |
| 11058 | reduced_tx_set: bool, |
| 11059 | // Returns `(below_mode, right_mode)`: the mode a neighbour BELOW this 8x8 |
| 11060 | // group sees (its bottom-left 4x4, libaom reads mi(row-1, col)) and the |
| 11061 | // one a neighbour to its RIGHT sees (its top-right 4x4, mi(row, col-1)). |
| 11062 | ) -> Result<(usize, usize)> { |
| 11063 | SUB8_SPLIT_HITS.with(|c| c.set(c.get() + 1)); |
| 11064 | let (r, c) = outer_at; |
| 11065 | let mut above_mode = neighbours.above_mode[c]; |
| 11066 | let mut left_mode = neighbours.left_mode[r]; |
| 11067 | if let Some(((pr, pc), pmode)) = prev_leaf { |
| 11068 | if pc == leaf_mi.1 && leaf_mi.0 == pr + 2 { |
| 11069 | above_mode = pmode; |
| 11070 | } else if pr == leaf_mi.0 && leaf_mi.1 == pc + 2 { |
| 11071 | left_mode = pmode; |
| 11072 | } |
| 11073 | } |
| 11074 | // See `decode_leaf8`'s own `smooth_neighbor_uv`: the chroma edge-filter |
| 11075 | // type is the chroma neighbours' `uv_mode`, mi-exact where recorded. |
| 11076 | let smooth_neighbor_uv = neighbours.smooth_uv_neighbour(leaf_mi.0, leaf_mi.1, r, c); |
| 11077 | let mut leaf_modes = [0usize; 4]; |
| 11078 | let mut leaf_skips = [false; 4]; |
| 11079 | let mut last_uv: Option<(usize, i32, Option<(i32, i32)>)> = None; |
| 11080 | for i in 0..4usize { |
| 11081 | let (dr, dc) = (i / 2, i % 2); |
| 11082 | let lmi = (leaf_mi.0 + dr, leaf_mi.1 + dc); |
| 11083 | // Neighbour modes at real mi granularity: inside the group the |
| 11084 | // sibling leaf, outside it the split-leaf map when the adjacent mi |
| 11085 | // was itself a 4x4 leaf, else the coarse 16x16 slot (see |
| 11086 | // `sub8_mode_col`'s doc). |
| 11087 | let leaf_above = if dr == 0 { |
| 11088 | neighbours.mode_above_mi(lmi.0, lmi.1).unwrap_or(above_mode) |
| 11089 | } else { |
| 11090 | leaf_modes[i - 2] |
| 11091 | }; |
| 11092 | let leaf_left = if dc == 0 { |
| 11093 | neighbours.mode_left_mi(lmi.0, lmi.1).unwrap_or(left_mode) |
| 11094 | } else { |
| 11095 | leaf_modes[i - 1] |
| 11096 | }; |
| 11097 | let has_chroma = i == 3; |
| 11098 | let skip_ctx = neighbours.skip_txfm_ctx(lmi.0, lmi.1); |
| 11099 | let (skip, mode, angle_delta_y, chroma, filter_intra) = read_intra_mode_sub8( |
| 11100 | dec, cdfs, leaf_above, leaf_left, has_chroma, enable_filter_intra, |
| 11101 | filter_intra_size_class(4).expect("BLOCK_4X4 has its own filter_intra row"), |
| 11102 | skip_ctx, allow_intrabc, lmi.0, lmi.1, |
| 11103 | )?; |
| 11104 | leaf_modes[i] = mode; |
| 11105 | leaf_skips[i] = skip; |
| 11106 | neighbours.record_mode_mi(lmi.0, lmi.1, 1, 1, mode); |
| 11107 | let smooth_neighbor = is_smooth_mode(leaf_above) || is_smooth_mode(leaf_left); |
| 11108 | if smooth_neighbor { |
| 11109 | SMOOTH_LUMA_HITS.with(|c| c.set(c.get() + 1)); |
| 11110 | } |
| 11111 | let (px, py) = (lmi.1 * MI, lmi.0 * MI); |
| 11112 | let reach = Reach::of(4, px, py, y.width, y.height); |
| 11113 | if skip { |
| 11114 | y.reconstruct(px, py, 4, mode, angle_delta_y, reach, &vec![0i32; 16], None, filter_intra, smooth_neighbor); |
| 11115 | neighbours.record_mi_luma(lmi, 4, &vec![0i32; 16]); |
| 11116 | } else { |
| 11117 | let tu_around = neighbours.around_mi(lmi, 4)[0]; |
| 11118 | // A `BLOCK_4X4` leaf's luma TU *is* the whole block, so libaom's |
| 11119 | // `get_txb_ctx` takes its `plane_bsize == txsize_to_bsize[tx_size]` |
| 11120 | // branch: `txb_skip_ctx = 0`, never the neighbour-magnitude table |
| 11121 | // (`txb_common.h:400`). Reading the table here decoded `all_zero` |
| 11122 | // off the wrong CDF on the very first leaf and desynced the tile. |
| 11123 | let tu_grid = read_plane( |
| 11124 | dec, |
| 11125 | cdfs, |
| 11126 | if reduced_tx_set { TxbSet::Luma4 } else { TxbSet::Luma4Set1 }, |
| 11127 | scan4, |
| 11128 | 0, |
| 11129 | tu_around, |
| 11130 | mode, |
| 11131 | mode, |
| 11132 | angle_delta_y, |
| 11133 | reach, |
| 11134 | y, |
| 11135 | px, |
| 11136 | py, |
| 11137 | 4, |
| 11138 | 4, |
| 11139 | base_q_idx, |
| 11140 | None, |
| 11141 | filter_intra, |
| 11142 | None, |
| 11143 | smooth_neighbor, |
| 11144 | )?; |
| 11145 | neighbours.record_mi_luma(lmi, 4, &tu_grid); |
| 11146 | } |
| 11147 | neighbours.fill_skip_grid(lmi, 1, skip); |
| 11148 | neighbours.fill_lf_grid(lmi, 1, 4, 0); |
| 11149 | if has_chroma { |
| 11150 | last_uv = chroma; |
| 11151 | } |
| 11152 | } |
| 11153 | let (gpx, gpy) = (leaf_mi.1 * MI, leaf_mi.0 * MI); |
| 11154 | let (cpx, cpy) = (gpx / 2, gpy / 2); |
| 11155 | let group_reach = Reach::of(8, gpx, gpy, y.width, y.height); |
| 11156 | let (uv_mode, angle_delta_uv, alpha) = |
| 11157 | last_uv.expect("i==3 always sets has_chroma, so chroma is always Some"); |
| 11158 | let uv_predict_mode = if uv_mode == UV_CFL_PRED { DC_PRED } else { uv_mode }; |
| 11159 | neighbours.above_uv_mode[c] = uv_predict_mode; |
| 11160 | neighbours.left_uv_mode[r] = uv_predict_mode; |
| 11161 | neighbours.record_uv_mode_mi(leaf_mi.0, leaf_mi.1, 2, 2, uv_predict_mode); |
| 11162 | let (u_grid, v_grid): (Vec<i32>, Vec<i32>) = if leaf_skips[3] { |
| 11163 | // A skipped chroma block still gets its CfL contribution: libaom's |
| 11164 | // `predict_and_reconstruct_intra_block` calls `cfl_predict_block` |
| 11165 | // from `av1_predict_intra_block_facade` (av1/common/reconintra.c:1719) |
| 11166 | // whenever `mbmi->uv_mode == UV_CFL_PRED`, before any residual is |
| 11167 | // added -- `skip_txfm` only drops the residual, never the alpha. |
| 11168 | // lane-reelckf and lane-kf1200 fixed THIS SAME arm independently; the |
| 11169 | // merge keeps one fix and feeds both lanes' counters so neither of |
| 11170 | // their gates goes vacuous. |
| 11171 | let ac = alpha.map(|_| cfl_ac_q3(y, gpx, gpy, 8)); |
| 11172 | if alpha.is_some() { |
| 11173 | CHROMA_SKIP_CFL_HITS.with(|h| h.set(h.get() + 1)); |
| 11174 | SKIP_CFL_HITS.with(|c| c.set(c.get() + 1)); |
| 11175 | } |
| 11176 | u.reconstruct(cpx, cpy, 4, uv_predict_mode, angle_delta_uv, group_reach, &vec![0i32; 16], alpha.zip(ac.as_deref()).map(|((au, _), ac)| (au, ac)), None, smooth_neighbor_uv); |
| 11177 | v.reconstruct(cpx, cpy, 4, uv_predict_mode, angle_delta_uv, group_reach, &vec![0i32; 16], alpha.zip(ac.as_deref()).map(|((_, av), ac)| (av, ac)), None, smooth_neighbor_uv); |
| 11178 | (vec![0i32; 16], vec![0i32; 16]) |
| 11179 | } else { |
| 11180 | let ac = alpha.map(|_| cfl_ac_q3(y, gpx, gpy, 8)); |
| 11181 | let chroma_around = neighbours.around_mi(leaf_mi, 8); |
| 11182 | let ug = read_plane( |
| 11183 | dec, cdfs, TxbSet::Chroma4, scan4, 1, chroma_around[1], uv_mode, uv_predict_mode, |
| 11184 | angle_delta_uv, group_reach, u, cpx, cpy, 4, TX4, base_q_idx, |
| 11185 | alpha.zip(ac.as_deref()).map(|((au, _), ac)| (au, ac)), None, None, smooth_neighbor_uv, |
| 11186 | )?; |
| 11187 | let vg = read_plane( |
| 11188 | dec, cdfs, TxbSet::Chroma4, scan4, 2, chroma_around[2], uv_mode, uv_predict_mode, |
| 11189 | angle_delta_uv, group_reach, v, cpx, cpy, 4, TX4, base_q_idx, |
| 11190 | alpha.zip(ac.as_deref()).map(|((_, av), ac)| (av, ac)), None, None, smooth_neighbor_uv, |
| 11191 | )?; |
| 11192 | (ug, vg) |
| 11193 | }; |
| 11194 | // Chroma neighbour bookkeeping for the whole 8x8 group -- mirrors |
| 11195 | // [`decode_leaf8`]'s own tx4-chroma section (`record_split_luma`'s |
| 11196 | // rescaling is wrong for an already-mi-unit position, so this inlines |
| 11197 | // its tail directly at `leaf_mi`, side_mi=2), EXCEPT for the partition |
| 11198 | // context value itself. Unlike `decode_leaf8`'s TX4 branch (still one |
| 11199 | // `BLOCK_8X8`, `PARTITION_NONE`, transform depth only), this group is |
| 11200 | // genuinely four `BLOCK_4X4` blocks (`PARTITION_SPLIT` at the 8x8 |
| 11201 | // level, libaom `decode_partition`'s `bsize2`/`subsize` recursion |
| 11202 | // bottom). libaom's `update_ext_partition_context` writes |
| 11203 | // `partition_context_lookup[subsize]` at the 8x8 slot -- `{30,30}` for |
| 11204 | // `BLOCK_8X8` (bit0=0, what `decode_leaf8` already reproduces via |
| 11205 | // side_mi=8) but `{31,31}` for `BLOCK_4X4` (bit0=1) here, and |
| 11206 | // `partition_ctx_mi`'s `left_side_mi[mi_r]*2 <= side` needs |
| 11207 | // `left_side_mi <= 4` to read that bit as 1. Leaving this at 8 (this |
| 11208 | // round's bug) makes the very next sibling 8x8's `partition_w8` read |
| 11209 | // with the wrong context and desync immediately. |
| 11210 | let side_mi = 2; |
| 11211 | for cell in 0..side_mi { |
| 11212 | neighbours.left_side_mi[leaf_mi.0 + cell] = 4; |
| 11213 | neighbours.above_side_mi[leaf_mi.1 + cell] = 4; |
| 11214 | } |
| 11215 | let round_up_even = |n: usize| n.div_ceil(2) * 2; |
| 11216 | let bound_h = round_up_even(neighbours.mi_rows); |
| 11217 | let bound_w = round_up_even(neighbours.mi_cols); |
| 11218 | let u_state = neighbour_state(&u_grid); |
| 11219 | let v_state = neighbour_state(&v_grid); |
| 11220 | for cell in 0..side_mi { |
| 11221 | if cell < side_mi.min(bound_h.saturating_sub(leaf_mi.0)) { |
| 11222 | neighbours.left[leaf_mi.0 + cell][1] = u_state; |
| 11223 | neighbours.left[leaf_mi.0 + cell][2] = v_state; |
| 11224 | } |
| 11225 | if cell < side_mi.min(bound_w.saturating_sub(leaf_mi.1)) { |
| 11226 | neighbours.above[leaf_mi.1 + cell][1] = u_state; |
| 11227 | neighbours.above[leaf_mi.1 + cell][2] = v_state; |
| 11228 | } |
| 11229 | } |
| 11230 | Ok((leaf_modes[2], leaf_modes[1])) |
| 11231 | } |
| 11232 | |
| 11233 | /// Decodes a `PARTITION_VERT`/`PARTITION_HORZ` of one 8x8 block into two |
| 11234 | /// `BLOCK_4X8`/`BLOCK_8X4` leaves (lane-tx4x8), the last partition shape |
| 11235 | /// below 8x8 this decoder refused. Mirrors [`decode_leaf_split4`] element for |
| 11236 | /// element -- same mode reads ([`read_intra_mode_sub8`]: no angle delta, no |
| 11237 | /// tx-depth symbol below 8x8), same "chroma once, on the last sub-block, at |
| 11238 | /// the whole 8x8 group's 4x4 unit" rule (`is_chroma_reference` is false for |
| 11239 | /// every sub-block one mi wide or high but the last) -- differing only where |
| 11240 | /// the shape does: |
| 11241 | /// |
| 11242 | /// * the transform is a true `TX_4X8`/`TX_8X4` |
| 11243 | /// ([`dequant_and_inverse_typed_wh`], whose `abs(rect_type) == 1` scale and |
| 11244 | /// `av1_inv_txfm_shift_ls` row already cover this size pair); |
| 11245 | /// * the CDF set is [`TxbSet::LumaRect4x8`] (`get_txsize_entropy_ctx` squares |
| 11246 | /// up to `TX_8X8`) with a 32-position `eob_pt` and the `TX_4X4` `tx_type` |
| 11247 | /// row -- see that variant's doc comment; |
| 11248 | /// * the scan is [`SCAN_4X8`]/[`SCAN_8X4`], or their `mrow`/`mcol` siblings |
| 11249 | /// ([`class_scan_table_wh`]) when the coded `tx_type` is `V_DCT`/`H_DCT`; |
| 11250 | /// * `Reach` comes from libaom's own `has_tr_4x8`/`has_bl_8x4` rows |
| 11251 | /// ([`Reach::of_rect`]); |
| 11252 | /// * `update_partition_context` writes `partition_context_lookup[BLOCK_4X8]` |
| 11253 | /// = `{31, 30}` (width 4 above, height 8 left), not the `{31, 31}` a |
| 11254 | /// `BLOCK_4X4` split writes. |
| 11255 | /// |
| 11256 | /// Filter intra on one of these leaves is decoded (lane-fi8 r1): the flag is |
| 11257 | /// read from `BLOCK_4X8`/`BLOCK_8X4`'s own `cdf::FILTER_INTRA` row (classes |
| 11258 | /// 8/9, NOT `BLOCK_4X4`'s), the predictor is [`crate::intra::predict_filter_intra`] |
| 11259 | /// over the leaf's true `bw`x`bh` -- per 4x4 transform unit, with that unit's |
| 11260 | /// own edge, when `tx_depth` splits it -- and the coded `tx_type` row is |
| 11261 | /// [`fi_tx_row`]'s `fimode_to_intradir`, not the block's raw `DC_PRED`. |
| 11262 | #[allow(clippy::too_many_arguments)] |
| 11263 | fn decode_leaf_rect8( |
| 11264 | dec: &mut SymbolDecoder, |
| 11265 | cdfs: &mut Cdfs, |
| 11266 | neighbours: &mut Neighbours, |
| 11267 | outer_at: (usize, usize), |
| 11268 | leaf_mi: (usize, usize), |
| 11269 | vert: bool, |
| 11270 | scan4: &[u16], |
| 11271 | y: &mut PlaneBuf, |
| 11272 | u: &mut PlaneBuf, |
| 11273 | v: &mut PlaneBuf, |
| 11274 | base_q_idx: u8, |
| 11275 | enable_filter_intra: bool, |
| 11276 | allow_intrabc: bool, |
| 11277 | tx_select: bool, |
| 11278 | reduced_tx_set: bool, |
| 11279 | ) -> Result<(usize, usize)> { |
| 11280 | let (r, c) = outer_at; |
| 11281 | // lane-modectx r1: availability-correct fallback pair (see |
| 11282 | // `modes_above_left`), not the raw coarse bands. |
| 11283 | let (above_mode, left_mode) = neighbours.modes_above_left(r, c); |
| 11284 | let smooth_neighbor_uv = neighbours.smooth_uv_neighbour(leaf_mi.0, leaf_mi.1, r, c); |
| 11285 | let (bw, bh) = if vert { (4, 8) } else { (8, 4) }; |
| 11286 | let (w_mi, h_mi) = (bw / MI, bh / MI); |
| 11287 | let scan: &[u16] = if vert { &SCAN_4X8 } else { &SCAN_8X4 }; |
| 11288 | let mut leaf_modes = [0usize; 2]; |
| 11289 | let mut leaf_skips = [false; 2]; |
| 11290 | let mut last_uv: Option<(usize, i32, Option<(i32, i32)>)> = None; |
| 11291 | for i in 0..2usize { |
| 11292 | let lmi = if vert { |
| 11293 | (leaf_mi.0, leaf_mi.1 + i) |
| 11294 | } else { |
| 11295 | (leaf_mi.0 + i, leaf_mi.1) |
| 11296 | }; |
| 11297 | // Inside the pair the sibling is the neighbour on the split axis; |
| 11298 | // outside it the mi-granular map, else the coarse 16x16 slot. |
| 11299 | let leaf_above = if !vert && i == 1 { |
| 11300 | leaf_modes[0] |
| 11301 | } else { |
| 11302 | neighbours.mode_above_mi(lmi.0, lmi.1).unwrap_or(above_mode) |
| 11303 | }; |
| 11304 | let leaf_left = if vert && i == 1 { |
| 11305 | leaf_modes[0] |
| 11306 | } else { |
| 11307 | neighbours.mode_left_mi(lmi.0, lmi.1).unwrap_or(left_mode) |
| 11308 | }; |
| 11309 | let has_chroma = i == 1; |
| 11310 | let skip_ctx = neighbours.skip_txfm_ctx(lmi.0, lmi.1); |
| 11311 | let (skip, mode, angle_delta_y, chroma, filter_intra) = read_intra_mode_sub8( |
| 11312 | dec, cdfs, leaf_above, leaf_left, has_chroma, enable_filter_intra, |
| 11313 | filter_intra_size_class_rect(bw, bh) |
| 11314 | .expect("BLOCK_4X8/BLOCK_8X4 are inside av1_filter_intra_allowed_bsize"), |
| 11315 | skip_ctx, allow_intrabc, lmi.0, lmi.1, |
| 11316 | )?; |
| 11317 | if filter_intra.is_some() { |
| 11318 | // lane-fi8 r1: `predict_filter_intra` walks its 4x2 patches over |
| 11319 | // a true `bw`x`bh` block, so a 4x8/8x4 leaf predicts through the |
| 11320 | // same `reconstruct_rect`/`reconstruct` calls below -- and under |
| 11321 | // a split transform, per 4x4 unit with that unit's own edge, |
| 11322 | // which is exactly what the depth-1 loop already does. |
| 11323 | FILTER_INTRA_RECT_HITS.with(|c| c.set(c.get() + 1)); |
| 11324 | FILTER_INTRA_RECT_SUB8_HITS.with(|c| c.set(c.get() + 1)); |
| 11325 | } |
| 11326 | // `TxMode::Select`'s `tx_depth` (spec 5.11.16) exists at a 4x8/8x4 |
| 11327 | // leaf too, and is read for every intra block, skipped or not. Its |
| 11328 | // category is libaom's `bsize_to_tx_size_cat(BLOCK_4X8) == 0` -- |
| 11329 | // `sub_tx_size_map[TX_4X8] == TX_4X4` is a single step, so it is the |
| 11330 | // same 2-symbol `tx_size_cat0` an 8x8 uses, not the 3-symbol |
| 11331 | // `tx_size_cat2` the 32x16 strips read -- with the rect context |
| 11332 | // (`get_tx_size_context`'s `max_tx_wide`/`max_tx_high` = bw/bh). |
| 11333 | // Missing this read consumed one symbol fewer than the encoder wrote |
| 11334 | // and desynced the tile at the very first rect leaf (class |
| 11335 | // `symbol-consumption-gap`). |
| 11336 | let depth = if tx_select { |
| 11337 | let ctx = tx_size_context_rect(neighbours, lmi, bw, bh); |
| 11338 | dec.symbol(&mut cdfs.tx_size_cat0[ctx]) |
| 11339 | } else { |
| 11340 | 0 |
| 11341 | }; |
| 11342 | if depth != 0 { |
| 11343 | TX_DEPTH_HITS.with(|c| c.set(c.get() + 1)); |
| 11344 | RECT8_SPLIT_TX_HITS.with(|c| c.set(c.get() + 1)); |
| 11345 | } |
| 11346 | leaf_modes[i] = mode; |
| 11347 | leaf_skips[i] = skip; |
| 11348 | neighbours.record_mode_mi(lmi.0, lmi.1, w_mi, h_mi, mode); |
| 11349 | let smooth_neighbor = is_smooth_mode(leaf_above) || is_smooth_mode(leaf_left); |
| 11350 | if smooth_neighbor { |
| 11351 | SMOOTH_LUMA_HITS.with(|c| c.set(c.get() + 1)); |
| 11352 | } |
| 11353 | let (px, py) = (lmi.1 * MI, lmi.0 * MI); |
| 11354 | let reach = Reach::of_rect(bw, bh, px, py, y.width, y.height); |
| 11355 | // `sub_tx_size_map[TX_4X8] == TX_4X4`: depth 1 splits the leaf into |
| 11356 | // two 4x4 transform units along its long axis, each predicted from |
| 11357 | // the previous unit's reconstruction and reading its own `txb_skip` |
| 11358 | // context -- `decode_leaf8`'s own depth-1 loop, two units instead of |
| 11359 | // four. The per-TU path writes its own neighbour state, so the |
| 11360 | // block-level write below is skipped for it. |
| 11361 | let split = depth != 0; |
| 11362 | if split { |
| 11363 | let mut done = 0usize; |
| 11364 | for tu in 0..2usize { |
| 11365 | let tu_mi = if vert { |
| 11366 | (lmi.0 + tu, lmi.1) |
| 11367 | } else { |
| 11368 | (lmi.0, lmi.1 + tu) |
| 11369 | }; |
| 11370 | let (tu_px, tu_py) = (tu_mi.1 * MI, tu_mi.0 * MI); |
| 11371 | let tu_around = neighbours.around_mi(tu_mi, 4)[0]; |
| 11372 | // Same per-TU rule as every other split-transform site |
| 11373 | // (lane-palette2 r12): a transform unit inside a block is not |
| 11374 | // a block at that position, so its reach comes from the |
| 11375 | // block's own row, offset by the unit's position. |
| 11376 | let tu_reach = tu_reach( |
| 11377 | bw, |
| 11378 | bh, |
| 11379 | tu_px - px, |
| 11380 | tu_py - py, |
| 11381 | 4, |
| 11382 | reach, |
| 11383 | px, |
| 11384 | py, |
| 11385 | y.width, |
| 11386 | y.height, |
| 11387 | ); |
| 11388 | let tu_skip_ctx = neighbours.luma_skip_ctx(tu_mi, 1); |
| 11389 | if skip { |
| 11390 | // A skipped leaf still predicts per transform unit: the |
| 11391 | // second 4x4 takes its above/left edge from the first |
| 11392 | // one's reconstruction, which is NOT what a single 4x8 |
| 11393 | // prediction of the same mode produces. |
| 11394 | let zeros = vec![0i32; 16]; |
| 11395 | y.reconstruct( |
| 11396 | tu_px, tu_py, 4, mode, angle_delta_y, tu_reach, &zeros, None, |
| 11397 | filter_intra, smooth_neighbor, |
| 11398 | ); |
| 11399 | neighbours.record_mi_luma(tu_mi, 4, &zeros); |
| 11400 | continue; |
| 11401 | } |
| 11402 | let tu_grid = read_plane( |
| 11403 | dec, |
| 11404 | cdfs, |
| 11405 | if reduced_tx_set { |
| 11406 | TxbSet::Luma4 |
| 11407 | } else { |
| 11408 | TxbSet::Luma4Set1 |
| 11409 | }, |
| 11410 | scan4, |
| 11411 | 0, |
| 11412 | tu_around, |
| 11413 | mode, |
| 11414 | mode, |
| 11415 | angle_delta_y, |
| 11416 | tu_reach, |
| 11417 | y, |
| 11418 | tu_px, |
| 11419 | tu_py, |
| 11420 | 4, |
| 11421 | TX4, |
| 11422 | base_q_idx, |
| 11423 | None, |
| 11424 | filter_intra, |
| 11425 | Some(tu_skip_ctx), |
| 11426 | smooth_neighbor, |
| 11427 | )?; |
| 11428 | if tu_grid.iter().any(|&l| l != 0) { |
| 11429 | done += 1; |
| 11430 | } |
| 11431 | neighbours.record_mi_luma(tu_mi, 4, &tu_grid); |
| 11432 | } |
| 11433 | if done > 0 { |
| 11434 | if vert { |
| 11435 | TX4X8_CODED_HITS.with(|h| h.set(h.get() + 1)); |
| 11436 | } else { |
| 11437 | TX8X4_CODED_HITS.with(|h| h.set(h.get() + 1)); |
| 11438 | } |
| 11439 | } |
| 11440 | neighbours.fill_skip_grid_rect(lmi, w_mi, h_mi, skip); |
| 11441 | neighbours.fill_lf_grid_rect(lmi, w_mi, h_mi, 4, 4, 0); |
| 11442 | if has_chroma { |
| 11443 | last_uv = chroma; |
| 11444 | } |
| 11445 | continue; |
| 11446 | } |
| 11447 | let grid = if skip { |
| 11448 | let zeros = vec![0i32; bw * bh]; |
| 11449 | y.reconstruct_rect( |
| 11450 | px, py, bw, bh, mode, angle_delta_y, reach, &zeros, None, filter_intra, |
| 11451 | smooth_neighbor, |
| 11452 | ); |
| 11453 | zeros |
| 11454 | } else { |
| 11455 | // `plane_bsize == txsize_to_bsize[tx_size]` here too (the leaf's |
| 11456 | // luma TU *is* the leaf), so `get_txb_ctx` fixes `txb_skip_ctx` |
| 11457 | // at 0 -- see `decode_leaf_split4`'s own note. |
| 11458 | let around = neighbours.around_mi_rect(lmi, bw, bh)[0]; |
| 11459 | let set = if reduced_tx_set { |
| 11460 | TxbSet::LumaRect4x8 |
| 11461 | } else { |
| 11462 | TxbSet::LumaRect4x8Set1 |
| 11463 | }; |
| 11464 | let mut coding = cdfs.txb(set, fi_tx_row(mode, filter_intra)); |
| 11465 | let (levels, tx_type) = read_coeffs_rect( |
| 11466 | dec, |
| 11467 | &mut coding, |
| 11468 | scan, |
| 11469 | bw, |
| 11470 | bh, |
| 11471 | 0, |
| 11472 | dc_sign_ctx(around.2), |
| 11473 | TxType::DctDct, |
| 11474 | )?; |
| 11475 | if levels.iter().any(|&l| l != 0) { |
| 11476 | if vert { |
| 11477 | TX4X8_CODED_HITS.with(|h| h.set(h.get() + 1)); |
| 11478 | } else { |
| 11479 | TX8X4_CODED_HITS.with(|h| h.set(h.get() + 1)); |
| 11480 | } |
| 11481 | } |
| 11482 | let residual = dequant_and_inverse_typed_wh( |
| 11483 | &levels, |
| 11484 | bw, |
| 11485 | bh, |
| 11486 | crate::decode::bit_depth(), |
| 11487 | CURRENT_Q_IDX.with(|q| q.get()), |
| 11488 | plane_q_delta(0).0, |
| 11489 | plane_q_delta(0).1, |
| 11490 | tx_type, |
| 11491 | ); |
| 11492 | y.reconstruct_rect( |
| 11493 | px, py, bw, bh, mode, angle_delta_y, reach, &residual, None, filter_intra, |
| 11494 | smooth_neighbor, |
| 11495 | ); |
| 11496 | levels |
| 11497 | }; |
| 11498 | // Plane 0's per-TU coefficient context over this leaf's own mi span |
| 11499 | // (`record_mi_luma`'s rect twin -- the chroma planes are written once |
| 11500 | // for the whole 8x8 group below). |
| 11501 | let state = neighbour_state(&grid); |
| 11502 | for cell in 0..h_mi { |
| 11503 | if lmi.0 + cell < neighbours.mi_rows { |
| 11504 | neighbours.left[lmi.0 + cell][0] = state; |
| 11505 | } |
| 11506 | } |
| 11507 | for cell in 0..w_mi { |
| 11508 | if lmi.1 + cell < neighbours.mi_cols { |
| 11509 | neighbours.above[lmi.1 + cell][0] = state; |
| 11510 | } |
| 11511 | } |
| 11512 | neighbours.fill_skip_grid_rect(lmi, w_mi, h_mi, skip); |
| 11513 | let (tx_w, tx_h) = if split { (4, 4) } else { (bw as u8, bh as u8) }; |
| 11514 | neighbours.fill_lf_grid_rect(lmi, w_mi, h_mi, tx_w, tx_h, 0); |
| 11515 | if has_chroma { |
| 11516 | last_uv = chroma; |
| 11517 | } |
| 11518 | } |
| 11519 | let (gpx, gpy) = (leaf_mi.1 * MI, leaf_mi.0 * MI); |
| 11520 | let (cpx, cpy) = (gpx / 2, gpy / 2); |
| 11521 | let group_reach = Reach::of(8, gpx, gpy, y.width, y.height); |
| 11522 | let (uv_mode, angle_delta_uv, alpha) = |
| 11523 | last_uv.expect("i==1 always sets has_chroma, so chroma is always Some"); |
| 11524 | let uv_predict_mode = if uv_mode == UV_CFL_PRED { DC_PRED } else { uv_mode }; |
| 11525 | neighbours.above_uv_mode[c] = uv_predict_mode; |
| 11526 | neighbours.left_uv_mode[r] = uv_predict_mode; |
| 11527 | neighbours.record_uv_mode_mi(leaf_mi.0, leaf_mi.1, 2, 2, uv_predict_mode); |
| 11528 | let (u_grid, v_grid): (Vec<i32>, Vec<i32>) = if leaf_skips[1] { |
| 11529 | // A skipped chroma block still gets its CfL contribution: libaom's |
| 11530 | // `predict_and_reconstruct_intra_block` calls `cfl_predict_block` |
| 11531 | // from `av1_predict_intra_block_facade` (av1/common/reconintra.c:1719) |
| 11532 | // whenever `mbmi->uv_mode == UV_CFL_PRED`, before any residual is |
| 11533 | // added -- `skip_txfm` only drops the residual, never the alpha. |
| 11534 | // lane-reelckf and lane-kf1200 fixed THIS SAME arm independently; the |
| 11535 | // merge keeps one fix and feeds both lanes' counters so neither of |
| 11536 | // their gates goes vacuous. |
| 11537 | let ac = alpha.map(|_| cfl_ac_q3(y, gpx, gpy, 8)); |
| 11538 | if alpha.is_some() { |
| 11539 | CHROMA_SKIP_CFL_HITS.with(|h| h.set(h.get() + 1)); |
| 11540 | SKIP_CFL_HITS.with(|c| c.set(c.get() + 1)); |
| 11541 | } |
| 11542 | u.reconstruct(cpx, cpy, 4, uv_predict_mode, angle_delta_uv, group_reach, &vec![0i32; 16], alpha.zip(ac.as_deref()).map(|((au, _), ac)| (au, ac)), None, smooth_neighbor_uv); |
| 11543 | v.reconstruct(cpx, cpy, 4, uv_predict_mode, angle_delta_uv, group_reach, &vec![0i32; 16], alpha.zip(ac.as_deref()).map(|((_, av), ac)| (av, ac)), None, smooth_neighbor_uv); |
| 11544 | (vec![0i32; 16], vec![0i32; 16]) |
| 11545 | } else { |
| 11546 | let ac = alpha.map(|_| cfl_ac_q3(y, gpx, gpy, 8)); |
| 11547 | let chroma_around = neighbours.around_mi(leaf_mi, 8); |
| 11548 | let ug = read_plane( |
| 11549 | dec, cdfs, TxbSet::Chroma4, scan4, 1, chroma_around[1], uv_mode, uv_predict_mode, |
| 11550 | angle_delta_uv, group_reach, u, cpx, cpy, 4, TX4, base_q_idx, |
| 11551 | alpha.zip(ac.as_deref()).map(|((au, _), ac)| (au, ac)), None, None, smooth_neighbor_uv, |
| 11552 | )?; |
| 11553 | let vg = read_plane( |
| 11554 | dec, cdfs, TxbSet::Chroma4, scan4, 2, chroma_around[2], uv_mode, uv_predict_mode, |
| 11555 | angle_delta_uv, group_reach, v, cpx, cpy, 4, TX4, base_q_idx, |
| 11556 | alpha.zip(ac.as_deref()).map(|((_, av), ac)| (av, ac)), None, None, smooth_neighbor_uv, |
| 11557 | )?; |
| 11558 | (ug, vg) |
| 11559 | }; |
| 11560 | // `update_partition_context` at the 8x8 level writes |
| 11561 | // `partition_context_lookup[subsize]` over the whole 8x8 span: BLOCK_4X8 |
| 11562 | // is `{31, 30}` (bit0 set on the above half only), BLOCK_8X4 `{30, 31}` |
| 11563 | // -- the mirror of `decode_leaf_split4`'s `{31, 31}`, which |
| 11564 | // `partition_ctx_mi` reads back off these side values. |
| 11565 | for cell in 0..2 { |
| 11566 | neighbours.above_side_mi[leaf_mi.1 + cell] = bw; |
| 11567 | neighbours.left_side_mi[leaf_mi.0 + cell] = bh; |
| 11568 | } |
| 11569 | let round_up_even = |n: usize| n.div_ceil(2) * 2; |
| 11570 | let bound_h = round_up_even(neighbours.mi_rows); |
| 11571 | let bound_w = round_up_even(neighbours.mi_cols); |
| 11572 | let u_state = neighbour_state(&u_grid); |
| 11573 | let v_state = neighbour_state(&v_grid); |
| 11574 | for cell in 0..2usize { |
| 11575 | if cell < 2usize.min(bound_h.saturating_sub(leaf_mi.0)) { |
| 11576 | neighbours.left[leaf_mi.0 + cell][1] = u_state; |
| 11577 | neighbours.left[leaf_mi.0 + cell][2] = v_state; |
| 11578 | } |
| 11579 | if cell < 2usize.min(bound_w.saturating_sub(leaf_mi.1)) { |
| 11580 | neighbours.above[leaf_mi.1 + cell][1] = u_state; |
| 11581 | neighbours.above[leaf_mi.1 + cell][2] = v_state; |
| 11582 | } |
| 11583 | } |
| 11584 | // `(below_mode, right_mode)`: the sub-block a neighbour below sees is the |
| 11585 | // bottom one (the second under HORZ, the left one under VERT), the one a |
| 11586 | // neighbour to the right sees is the top-right (the second under VERT). |
| 11587 | Ok(if vert { |
| 11588 | (leaf_modes[0], leaf_modes[1]) |
| 11589 | } else { |
| 11590 | (leaf_modes[1], leaf_modes[0]) |
| 11591 | }) |
| 11592 | } |
| 11593 | |
| 11594 | /// Spec 7.15.3's `Cdef_Directions`: `(row, col)` offsets for the two primary |
| 11595 | /// taps of each of the eight directions; the secondary taps at `dir+2`/`dir-2` |
| 11596 | /// (mod 8) reuse the same table. |
| 11597 | const CDEF_DIRECTIONS: [[(i32, i32); 2]; 8] = [ |
| 11598 | [(-1, 1), (-2, 2)], |
| 11599 | [(0, 1), (-1, 2)], |
| 11600 | [(0, 1), (0, 2)], |
| 11601 | [(0, 1), (1, 2)], |
| 11602 | [(1, 1), (2, 2)], |
| 11603 | [(1, 0), (2, 1)], |
| 11604 | [(1, 0), (2, 0)], |
| 11605 | [(1, 0), (2, -1)], |
| 11606 | ]; |
| 11607 | /// A neighbour tap outside the frame's true extent -- `constrain` reads this |
| 11608 | /// as "so far away it never contributes" (spec/libaom `CDEF_VERY_LARGE`). |
| 11609 | const CDEF_VERY_LARGE: i32 = 0x4000; |
| 11610 | const CDEF_PRI_TAPS: [[i32; 2]; 2] = [[4, 2], [3, 3]]; |
| 11611 | const CDEF_SEC_TAPS: [i32; 2] = [2, 1]; |
| 11612 | |
| 11613 | fn cdef_msb(x: i32) -> i32 { |
| 11614 | 31 - x.leading_zeros() as i32 |
| 11615 | } |
| 11616 | |
| 11617 | /// Spec 7.15.3's `constrain`: how much of a neighbour's difference from the |
| 11618 | /// centre sample a filter tap is allowed to contribute. |
| 11619 | fn cdef_constrain(diff: i32, threshold: i32, damping: i32) -> i32 { |
| 11620 | if threshold == 0 { |
| 11621 | return 0; |
| 11622 | } |
| 11623 | let shift = (damping - cdef_msb(threshold)).max(0); |
| 11624 | diff.signum() * (threshold - (diff.abs() >> shift)).clamp(0, diff.abs()) |
| 11625 | } |
| 11626 | |
| 11627 | /// Spec 7.15.3's direction search (libaom `cdef_find_dir_c`): the dominant |
| 11628 | /// direction of an 8x8 luma window and the variance gap between it and its |
| 11629 | /// orthogonal, `sample(row, col)` reading that window's own pixels. |
| 11630 | fn cdef_find_dir(coeff_shift: i32, sample: impl Fn(usize, usize) -> i32) -> (usize, i32) { |
| 11631 | const DIV_TABLE: [i64; 9] = [0, 840, 420, 280, 210, 168, 140, 120, 105]; |
| 11632 | let mut partial = [[0i64; 15]; 8]; |
| 11633 | for i in 0..8i64 { |
| 11634 | for j in 0..8i64 { |
| 11635 | // libaom `cdef_find_dir_c`: normalise to an 8-bit sample before |
| 11636 | // accumulating, same as `constrain`'s threshold shift below. |
| 11637 | let x = i64::from(sample(i as usize, j as usize) >> coeff_shift) - 128; |
| 11638 | partial[0][(i + j) as usize] += x; |
| 11639 | partial[1][(i + j / 2) as usize] += x; |
| 11640 | partial[2][i as usize] += x; |
| 11641 | partial[3][(3 + i - j / 2) as usize] += x; |
| 11642 | partial[4][(7 + i - j) as usize] += x; |
| 11643 | partial[5][(3 - i / 2 + j) as usize] += x; |
| 11644 | partial[6][j as usize] += x; |
| 11645 | partial[7][(i / 2 + j) as usize] += x; |
| 11646 | } |
| 11647 | } |
| 11648 | let mut cost = [0i64; 8]; |
| 11649 | for i in 0..8usize { |
| 11650 | cost[2] += partial[2][i] * partial[2][i]; |
| 11651 | cost[6] += partial[6][i] * partial[6][i]; |
| 11652 | } |
| 11653 | cost[2] *= DIV_TABLE[8]; |
| 11654 | cost[6] *= DIV_TABLE[8]; |
| 11655 | for i in 0..7usize { |
| 11656 | cost[0] += (partial[0][i] * partial[0][i] + partial[0][14 - i] * partial[0][14 - i]) |
| 11657 | * DIV_TABLE[i + 1]; |
| 11658 | cost[4] += (partial[4][i] * partial[4][i] + partial[4][14 - i] * partial[4][14 - i]) |
| 11659 | * DIV_TABLE[i + 1]; |
| 11660 | } |
| 11661 | cost[0] += partial[0][7] * partial[0][7] * DIV_TABLE[8]; |
| 11662 | cost[4] += partial[4][7] * partial[4][7] * DIV_TABLE[8]; |
| 11663 | for i in (1..8).step_by(2) { |
| 11664 | for j in 0..5usize { |
| 11665 | cost[i] += partial[i][3 + j] * partial[i][3 + j]; |
| 11666 | } |
| 11667 | cost[i] *= DIV_TABLE[8]; |
| 11668 | for j in 0..3usize { |
| 11669 | cost[i] += (partial[i][j] * partial[i][j] + partial[i][10 - j] * partial[i][10 - j]) |
| 11670 | * DIV_TABLE[2 * j + 2]; |
| 11671 | } |
| 11672 | } |
| 11673 | let mut best_cost = 0i64; |
| 11674 | let mut best_dir = 0usize; |
| 11675 | for (i, &c) in cost.iter().enumerate() { |
| 11676 | if c > best_cost { |
| 11677 | best_cost = c; |
| 11678 | best_dir = i; |
| 11679 | } |
| 11680 | } |
| 11681 | let var = (best_cost - cost[(best_dir + 4) & 7]) >> 10; |
| 11682 | (best_dir, var as i32) |
| 11683 | } |
| 11684 | |
| 11685 | /// Spec 7.15.2's per-8x8 strength adjustment: a strongly directional 8x8 |
| 11686 | /// (high variance gap) gets more deringing than a flat or non-directional |
| 11687 | /// one, only ever applied to the luma primary strength. |
| 11688 | fn cdef_adjust_strength(strength: i32, var: i32) -> i32 { |
| 11689 | if var == 0 { |
| 11690 | return 0; |
| 11691 | } |
| 11692 | let i = if var >> 6 != 0 { |
| 11693 | cdef_msb(var >> 6).min(12) |
| 11694 | } else { |
| 11695 | 0 |
| 11696 | }; |
| 11697 | (strength * (4 + i) + 8) >> 4 |
| 11698 | } |
| 11699 | |
| 11700 | /// Spec 7.15.3's `cdef_filter_block`: filters one block (8x8 luma, 4x4 4:2:0 |
| 11701 | /// chroma) in place, `sample(row, col)` reading the *unfiltered* frame (a |
| 11702 | /// previously-filtered neighbour must never feed this block's own sum). |
| 11703 | #[allow(clippy::too_many_arguments)] |
| 11704 | fn cdef_filter_block( |
| 11705 | sample: impl Fn(i32, i32) -> i32, |
| 11706 | dst: &mut PlaneBuf, |
| 11707 | ox: usize, |
| 11708 | oy: usize, |
| 11709 | bw: usize, |
| 11710 | bh: usize, |
| 11711 | pri_strength: i32, |
| 11712 | sec_strength: i32, |
| 11713 | dir: usize, |
| 11714 | pri_damping: i32, |
| 11715 | sec_damping: i32, |
| 11716 | enable_primary: bool, |
| 11717 | enable_secondary: bool, |
| 11718 | coeff_shift: i32, |
| 11719 | ) { |
| 11720 | let clipping_required = enable_primary && enable_secondary; |
| 11721 | // libaom `cdef_filter_block_internal`: `cdef_pri_taps[(pri_strength >> |
| 11722 | // coeff_shift) & 1]` -- `pri_strength` (and its `adjust_strength` |
| 11723 | // derivative) carries the bit-depth shift, so the tap-set parity bit |
| 11724 | // must be read back above that shift, not off the raw low bit. |
| 11725 | let pri_taps = CDEF_PRI_TAPS[((pri_strength >> coeff_shift) & 1) as usize]; |
| 11726 | for i in 0..bh { |
| 11727 | for j in 0..bw { |
| 11728 | let x = sample(i as i32, j as i32); |
| 11729 | let mut sum = 0i32; |
| 11730 | let mut max = x; |
| 11731 | let mut min = x; |
| 11732 | for k in 0..2usize { |
| 11733 | if enable_primary { |
| 11734 | let (tr, tc) = CDEF_DIRECTIONS[dir][k]; |
| 11735 | let p0 = sample(i as i32 + tr, j as i32 + tc); |
| 11736 | let p1 = sample(i as i32 - tr, j as i32 - tc); |
| 11737 | sum += pri_taps[k] * cdef_constrain(p0 - x, pri_strength, pri_damping); |
| 11738 | sum += pri_taps[k] * cdef_constrain(p1 - x, pri_strength, pri_damping); |
| 11739 | if clipping_required { |
| 11740 | if p0 != CDEF_VERY_LARGE { |
| 11741 | max = max.max(p0); |
| 11742 | } |
| 11743 | if p1 != CDEF_VERY_LARGE { |
| 11744 | max = max.max(p1); |
| 11745 | } |
| 11746 | min = min.min(p0).min(p1); |
| 11747 | } |
| 11748 | } |
| 11749 | if enable_secondary { |
| 11750 | let (tr0, tc0) = CDEF_DIRECTIONS[(dir + 2) & 7][k]; |
| 11751 | let (tr1, tc1) = CDEF_DIRECTIONS[(dir + 6) & 7][k]; |
| 11752 | let s0 = sample(i as i32 + tr0, j as i32 + tc0); |
| 11753 | let s1 = sample(i as i32 - tr0, j as i32 - tc0); |
| 11754 | let s2 = sample(i as i32 + tr1, j as i32 + tc1); |
| 11755 | let s3 = sample(i as i32 - tr1, j as i32 - tc1); |
| 11756 | if clipping_required { |
| 11757 | for s in [s0, s1, s2, s3] { |
| 11758 | if s != CDEF_VERY_LARGE { |
| 11759 | max = max.max(s); |
| 11760 | } |
| 11761 | } |
| 11762 | min = min.min(s0).min(s1).min(s2).min(s3); |
| 11763 | } |
| 11764 | sum += CDEF_SEC_TAPS[k] |
| 11765 | * (cdef_constrain(s0 - x, sec_strength, sec_damping) |
| 11766 | + cdef_constrain(s1 - x, sec_strength, sec_damping) |
| 11767 | + cdef_constrain(s2 - x, sec_strength, sec_damping) |
| 11768 | + cdef_constrain(s3 - x, sec_strength, sec_damping)); |
| 11769 | } |
| 11770 | } |
| 11771 | let mut y = x + ((8 + sum - i32::from(sum < 0)) >> 4); |
| 11772 | if clipping_required { |
| 11773 | y = y.clamp(min, max); |
| 11774 | } |
| 11775 | dst.data[(oy + i) * dst.width + (ox + j)] = y.clamp(0, sample_max()) as u16; |
| 11776 | } |
| 11777 | } |
| 11778 | } |
| 11779 | |
| 11780 | /// Spec 7.14: the in-loop deblocking filter, applied once per frame after |
| 11781 | /// tile reconstruction and before [`apply_cdef`]. Uniform-level path only -- |
| 11782 | /// `stream.rs` already refuses `delta_lf_present`/segmentation upstream, so |
| 11783 | /// every block's level comes from [`LoopFilterParams::level`] plus the |
| 11784 | /// `ref_deltas`/`mode_deltas` adjustment alone (spec 7.14.4's |
| 11785 | /// `get_filter_level`, precomputed per-block here rather than cached in a |
| 11786 | /// table the way libaom's `av1_loop_filter_frame_init` does). |
| 11787 | /// |
| 11788 | /// Ported from libaom's `av1/common/av1_loopfilter.c` (edge/level decision) |
| 11789 | /// and `aom_dsp/loopfilter.c` (the `aom_lpf_*` pixel kernels). |
| 11790 | fn apply_deblock( |
| 11791 | y: &mut PlaneBuf, |
| 11792 | u: &mut PlaneBuf, |
| 11793 | v: &mut PlaneBuf, |
| 11794 | lf: &LoopFilterParams, |
| 11795 | n: &Neighbours, |
| 11796 | frame_width: usize, |
| 11797 | frame_height: usize, |
| 11798 | ) { |
| 11799 | // lane-part32 r2 debug rung, env-gated: bisecting a widespread pixel |
| 11800 | // mismatch between the raw reconstruction and post-filter output. |
| 11801 | if std::env::var_os("EC_AV1_DEBUG_SKIP_DEBLOCK").is_some() { |
| 11802 | return; |
| 11803 | } |
| 11804 | if lf.level[0] != 0 || lf.level[1] != 0 { |
| 11805 | deblock_plane(y, 0, lf, n, frame_width, frame_height); |
| 11806 | } |
| 11807 | if lf.level[2] != 0 { |
| 11808 | deblock_plane(u, 1, lf, n, frame_width, frame_height); |
| 11809 | } |
| 11810 | if lf.level[3] != 0 { |
| 11811 | deblock_plane(v, 2, lf, n, frame_width, frame_height); |
| 11812 | } |
| 11813 | } |
| 11814 | |
| 11815 | /// Luma-mi-grid position (row or col) a plane-local pixel coordinate falls |
| 11816 | /// in -- `Neighbours::tx_grid`/`ref_grid` are always indexed at the luma |
| 11817 | /// 4x4-mi grain, even when `coord` is a chroma coordinate (2 chroma px per |
| 11818 | /// mi unit under this decoder's assumed 4:2:0 subsampling). |
| 11819 | fn plane_to_mi(chroma: bool, coord: usize) -> usize { |
| 11820 | if chroma { coord / 2 } else { coord / 4 } |
| 11821 | } |
| 11822 | |
| 11823 | /// This plane's own transform width in pixels at the covering block, spec |
| 11824 | /// `get_transform_size`'s plane-aware lookup: `tx_grid` stores the luma |
| 11825 | /// width directly; chroma halves it (this decoder's UV transform is always |
| 11826 | /// half its coded block's luma transform, spec's `av1_get_max_uv_txsize` |
| 11827 | /// under 4:2:0 and a block that never splits its own transform). |
| 11828 | fn tx_px_at(n: &Neighbours, chroma: bool, mi_r: usize, mi_c: usize) -> u32 { |
| 11829 | let grid = if chroma { &n.uv_tx_grid } else { &n.tx_grid }; |
| 11830 | grid.get(mi_r * n.skip_grid_cols_mi + mi_c).copied().unwrap_or(0) as u32 |
| 11831 | } |
| 11832 | |
| 11833 | /// [`tx_px_at`]'s HEIGHT twin (lane-rect r2): the deblocker's horizontal |
| 11834 | /// edges step and gate by transform height, which differs from width only |
| 11835 | /// on rect partition strips. |
| 11836 | fn tx_h_px_at(n: &Neighbours, chroma: bool, mi_r: usize, mi_c: usize) -> u32 { |
| 11837 | let grid = if chroma { &n.uv_tx_h_grid } else { &n.tx_h_grid }; |
| 11838 | grid.get(mi_r * n.skip_grid_cols_mi + mi_c).copied().unwrap_or(0) as u32 |
| 11839 | } |
| 11840 | |
| 11841 | /// `get_tx_size_context` (spec 9.3's `TxDepthCtx`, libaom |
| 11842 | /// `TXFM_CONTEXT`/`entropymode.c`): the sum of two boolean terms, each |
| 11843 | /// present only when its neighbour exists inside the true frame -- whether |
| 11844 | /// the block immediately above (at this block's own leftmost mi column) or |
| 11845 | /// to the left (at its own topmost mi row) coded a transform at least as wide |
| 11846 | /// as `own_side`. |
| 11847 | fn tx_size_context(n: &Neighbours, (mi_r, mi_c): (usize, usize), own_side: usize) -> usize { |
| 11848 | let above = mi_r > n.tile_row0_mi && tx_px_at(n, false, mi_r - 1, mi_c) as usize >= own_side; |
| 11849 | let left = mi_c > n.tile_col0_mi && tx_h_px_at(n, false, mi_r, mi_c - 1) as usize >= own_side; |
| 11850 | usize::from(above) + usize::from(left) |
| 11851 | } |
| 11852 | |
| 11853 | /// `get_tx_size_context` as libaom actually writes it (`pred_common.h:342`), |
| 11854 | /// for a block inside an INTER frame: the two `TXFM_CONTEXT` arrays |
| 11855 | /// ([`Neighbours::above_txfm`]/[`Neighbours::left_txfm`], which only the |
| 11856 | /// inter path maintains), except that an *inter* neighbour contributes its |
| 11857 | /// own BLOCK size rather than its transform size -- the two differ the moment |
| 11858 | /// a neighbour's var-tx tree splits. [`tx_size_context`]'s deblock-grid |
| 11859 | /// approximation coincides with this only while every neighbour codes one |
| 11860 | /// whole-block transform, which is exactly what `TxMode::Select` ends. |
| 11861 | fn tx_size_context_txfm(n: &Neighbours, (mi_r, mi_c): (usize, usize), side: usize) -> usize { |
| 11862 | let has_above = mi_r > n.tile_row0_mi; |
| 11863 | let has_left = mi_c > n.tile_col0_mi; |
| 11864 | let mut above = usize::from(n.above_txfm[mi_c]) >= side; |
| 11865 | let mut left = usize::from(n.left_txfm[mi_r]) >= side; |
| 11866 | if has_above && n.above_inter[mi_c / (SUB / MI)] { |
| 11867 | above = n.above_side_mi[mi_c] >= side; |
| 11868 | } |
| 11869 | if has_left && n.left_inter[mi_r / (SUB / MI)] { |
| 11870 | left = n.left_side_mi[mi_r] >= side; |
| 11871 | } |
| 11872 | match (has_above, has_left) { |
| 11873 | (true, true) => usize::from(above) + usize::from(left), |
| 11874 | (true, false) => usize::from(above), |
| 11875 | (false, true) => usize::from(left), |
| 11876 | (false, false) => 0, |
| 11877 | } |
| 11878 | } |
| 11879 | |
| 11880 | /// `read_tx_size`/`read_selected_tx_size` (spec 5.11.16): under |
| 11881 | /// `TxMode::Select`, an intra block's `tx_depth` symbol, resolved straight to |
| 11882 | /// the transform's own side in pixels (`side >> depth`) -- this decoder's |
| 11883 | /// four square categories (8/16/32/64) each halve at most twice, matching |
| 11884 | /// libaom's own per-size depth cap (`cdf::TX_SIZE_CAT0`..`TX_SIZE_CAT3`). |
| 11885 | fn read_tx_size( |
| 11886 | dec: &mut SymbolDecoder, |
| 11887 | cdfs: &mut Cdfs, |
| 11888 | n: &Neighbours, |
| 11889 | at_mi: (usize, usize), |
| 11890 | side: usize, |
| 11891 | // lane-txselect: `get_tx_size_context`'s real context, for the callers |
| 11892 | // that have one -- see [`tx_size_context_txfm`]. `None` keeps the |
| 11893 | // key-frame path's own deblock-grid approximation. |
| 11894 | ctx_override: Option<usize>, |
| 11895 | ) -> usize { |
| 11896 | // `max_txsize_rect_lookup[bsize]` (libaom `read_selected_tx_size` / |
| 11897 | // `get_tx_size_context`): a block wider than 64 still tops out at |
| 11898 | // TX_64X64, so BLOCK_128X128 reads BLOCK_64X64's own category (its depth |
| 11899 | // chain 64->32->16->8->4 is the same four steps) and resolves against 64, |
| 11900 | // never against its own 128 side (lane-sb128b r1). |
| 11901 | let max_tx = side.min(64); |
| 11902 | let ctx = ctx_override.unwrap_or_else(|| tx_size_context(n, at_mi, max_tx)); |
| 11903 | let depth = match max_tx { |
| 11904 | 8 => { |
| 11905 | dec.symbol(&mut cdfs.tx_size_cat0[ctx]) |
| 11906 | } |
| 11907 | 16 => { |
| 11908 | dec.symbol(&mut cdfs.tx_size_cat1[ctx]) |
| 11909 | } |
| 11910 | 32 => { |
| 11911 | dec.symbol(&mut cdfs.tx_size_cat2[ctx]) |
| 11912 | } |
| 11913 | 64 => { |
| 11914 | dec.symbol(&mut cdfs.tx_size_cat3[ctx]) |
| 11915 | } |
| 11916 | _ => unreachable!("decode_block/decode_leaf8 only call this at 8/16/32/64/128"), |
| 11917 | }; |
| 11918 | if std::env::var_os("EC_TRACE_MODE_STEP").is_some() { |
| 11919 | let (rng, _) = dec.debug_state(); |
| 11920 | let (mi_r, mi_c) = at_mi; |
| 11921 | eprintln!( |
| 11922 | "EC_ISTEP mi_row={mi_r} mi_col={mi_c} name=tx_depth val={depth} ctx={ctx} rng={rng}" |
| 11923 | ); |
| 11924 | } |
| 11925 | if depth != 0 { |
| 11926 | TX_DEPTH_HITS.with(|c| c.set(c.get() + 1)); |
| 11927 | } |
| 11928 | max_tx >> depth |
| 11929 | } |
| 11930 | |
| 11931 | /// `txfm_partition_context` (libaom `av1_common_int.h`): an inter block's |
| 11932 | /// `txfm_split` context -- a category from the block's own largest square |
| 11933 | /// transform and whether this unit already sits below it, times three, plus |
| 11934 | /// the two neighbour terms (whether the transform last written above/left of |
| 11935 | /// this unit is narrower/shorter than the unit itself). |
| 11936 | fn txfm_partition_ctx(above_px: u8, left_px: u8, blk_max_px: usize, tx_px: usize) -> usize { |
| 11937 | txfm_partition_ctx_rect(above_px, left_px, blk_max_px, tx_px, tx_px) |
| 11938 | } |
| 11939 | |
| 11940 | /// [`txfm_partition_ctx`] for a *rectangular* transform (lane-inter4 r2): |
| 11941 | /// libaom's `txfm_partition_context` compares the above context against |
| 11942 | /// `tx_size_wide` and the left one against `tx_size_high`, and takes its |
| 11943 | /// category from `txsize_sqr_up_map[tx_size]` -- all three collapse to one |
| 11944 | /// side for a square transform, which is why the square form above is a |
| 11945 | /// special case of this one. |
| 11946 | fn txfm_partition_ctx_rect( |
| 11947 | above_px: u8, |
| 11948 | left_px: u8, |
| 11949 | blk_max_px: usize, |
| 11950 | tx_w: usize, |
| 11951 | tx_h: usize, |
| 11952 | ) -> usize { |
| 11953 | let above = usize::from(usize::from(above_px) < tx_w); |
| 11954 | let left = usize::from(usize::from(left_px) < tx_h); |
| 11955 | let tx_px = tx_w.max(tx_h); |
| 11956 | // `get_sqr_tx_size(max(block_size_wide, block_size_high))`, capped at the |
| 11957 | // largest transform (`TX_64X64`); `TX_4X4`..`TX_64X64` index as |
| 11958 | // `log2(px) - 2`. |
| 11959 | let max_tx = blk_max_px.min(64); |
| 11960 | let max_idx = max_tx.trailing_zeros() as usize - 2; |
| 11961 | let category = usize::from(tx_px != max_tx && max_tx > 8) + (4 - max_idx) * 2; |
| 11962 | category * 3 + above + left |
| 11963 | } |
| 11964 | |
| 11965 | /// `txfm_partition_update` (libaom `av1_common_int.h`): once a var-tx unit |
| 11966 | /// resolves, every mi cell its *parent* unit (`txb_px`) spans records the |
| 11967 | /// resolved transform's own size. |
| 11968 | fn txfm_partition_update( |
| 11969 | n: &mut Neighbours, |
| 11970 | at_mi: (usize, usize), |
| 11971 | tx_px: usize, |
| 11972 | txb_px: usize, |
| 11973 | ) { |
| 11974 | txfm_partition_update_rect(n, at_mi, (tx_px, tx_px), (txb_px, txb_px)); |
| 11975 | } |
| 11976 | |
| 11977 | /// [`txfm_partition_update`] for a rectangular transform (lane-inter4 r2): |
| 11978 | /// the left column records `tx_size_high` over the parent's own height and |
| 11979 | /// the above row records `tx_size_wide` over its width. |
| 11980 | fn txfm_partition_update_rect( |
| 11981 | n: &mut Neighbours, |
| 11982 | (mi_r, mi_c): (usize, usize), |
| 11983 | (tx_w, tx_h): (usize, usize), |
| 11984 | (txb_w, txb_h): (usize, usize), |
| 11985 | ) { |
| 11986 | for i in 0..txb_h / MI { |
| 11987 | if let Some(cell) = n.left_txfm.get_mut(mi_r + i) { |
| 11988 | *cell = tx_h as u8; |
| 11989 | } |
| 11990 | } |
| 11991 | for i in 0..txb_w / MI { |
| 11992 | if let Some(cell) = n.above_txfm.get_mut(mi_c + i) { |
| 11993 | *cell = tx_w as u8; |
| 11994 | } |
| 11995 | } |
| 11996 | } |
| 11997 | |
| 11998 | /// `set_txfm_ctxs` (libaom `av1_common_int.h`): the non-var-tx write of the |
| 11999 | /// same contexts -- a skipped *inter* block records its own block size rather |
| 12000 | /// than its transform size. |
| 12001 | fn set_txfm_ctxs( |
| 12002 | n: &mut Neighbours, |
| 12003 | (mi_r, mi_c): (usize, usize), |
| 12004 | tx_px: usize, |
| 12005 | w_mi: usize, |
| 12006 | h_mi: usize, |
| 12007 | skip_inter: bool, |
| 12008 | ) { |
| 12009 | let bw = if skip_inter { w_mi * MI } else { tx_px }; |
| 12010 | let bh = if skip_inter { h_mi * MI } else { tx_px }; |
| 12011 | for i in 0..w_mi { |
| 12012 | if let Some(cell) = n.above_txfm.get_mut(mi_c + i) { |
| 12013 | *cell = bw as u8; |
| 12014 | } |
| 12015 | } |
| 12016 | for i in 0..h_mi { |
| 12017 | if let Some(cell) = n.left_txfm.get_mut(mi_r + i) { |
| 12018 | *cell = bh as u8; |
| 12019 | } |
| 12020 | } |
| 12021 | } |
| 12022 | |
| 12023 | /// The value libaom initialises both transform contexts to -- NOT zero: |
| 12024 | /// `av1_zero_above_context` memsets the txfm row to |
| 12025 | /// `tx_size_wide[TX_SIZES_LARGEST]` and `av1_zero_left_context` does the same |
| 12026 | /// with `tx_size_high[TX_SIZES_LARGEST]` (`av1_common_int.h`), i.e. 64 |
| 12027 | /// pixels, so an unwritten neighbour reads as the *widest* possible |
| 12028 | /// transform, not the narrowest. |
| 12029 | const TXFM_CTX_INIT: u8 = 64; |
| 12030 | |
| 12031 | /// libaom's `MAX_VARTX_DEPTH`: an inter block's transform tree halves at most |
| 12032 | /// twice below its own largest transform. |
| 12033 | const MAX_VARTX_DEPTH: usize = 2; |
| 12034 | |
| 12035 | /// libaom `sub_tx_size_map[TX_SIZES_ALL]` (`common_data.c`): the transform |
| 12036 | /// one `txfm_split` step below `tx_size`. RECTANGULAR for the 1:4 shapes |
| 12037 | /// (`[TX_64X16] == TX_32X16`, `[TX_16X64] == TX_16X32`) and for the 4:1 |
| 12038 | /// small ones, square everywhere else. |
| 12039 | fn sub_tx_size_map(tx_w: usize, tx_h: usize) -> (usize, usize) { |
| 12040 | match (tx_w, tx_h) { |
| 12041 | (4, 4) => (4, 4), |
| 12042 | (8, 8) => (4, 4), |
| 12043 | (16, 16) => (8, 8), |
| 12044 | (32, 32) => (16, 16), |
| 12045 | (64, 64) => (32, 32), |
| 12046 | (4, 8) | (8, 4) => (4, 4), |
| 12047 | (8, 16) | (16, 8) => (8, 8), |
| 12048 | (16, 32) | (32, 16) => (16, 16), |
| 12049 | (32, 64) | (64, 32) => (32, 32), |
| 12050 | (4, 16) => (4, 8), |
| 12051 | (16, 4) => (8, 4), |
| 12052 | (8, 32) => (8, 16), |
| 12053 | (32, 8) => (16, 8), |
| 12054 | (16, 64) => (16, 32), |
| 12055 | (64, 16) => (32, 16), |
| 12056 | _ => unreachable!("not a TX_SIZES_ALL shape: {tx_w}x{tx_h}"), |
| 12057 | } |
| 12058 | } |
| 12059 | |
| 12060 | #[test] |
| 12061 | fn sub_tx_size_map_matches_libaom() { |
| 12062 | // libaom `common_data.c` `sub_tx_size_map`, in TX_SIZES_ALL order. |
| 12063 | const TABLE: [((usize, usize), (usize, usize)); 19] = [ |
| 12064 | ((4, 4), (4, 4)), ((8, 8), (4, 4)), ((16, 16), (8, 8)), ((32, 32), (16, 16)), |
| 12065 | ((64, 64), (32, 32)), ((4, 8), (4, 4)), ((8, 4), (4, 4)), ((8, 16), (8, 8)), |
| 12066 | ((16, 8), (8, 8)), ((16, 32), (16, 16)), ((32, 16), (16, 16)), ((32, 64), (32, 32)), |
| 12067 | ((64, 32), (32, 32)), ((4, 16), (4, 8)), ((16, 4), (8, 4)), ((8, 32), (8, 16)), |
| 12068 | ((32, 8), (16, 8)), ((16, 64), (16, 32)), ((64, 16), (32, 16)), |
| 12069 | ]; |
| 12070 | for (tx, want) in TABLE { |
| 12071 | assert_eq!(sub_tx_size_map(tx.0, tx.1), want, "sub_tx_size_map({tx:?})"); |
| 12072 | } |
| 12073 | } |
| 12074 | |
| 12075 | /// `read_var_tx_size` (spec 5.11.17, libaom `decodeframe.c` |
| 12076 | /// `read_tx_size_vartx`): the recursive `txfm_split` tree of one |
| 12077 | /// max-transform-sized unit of an inter block, collecting the resolved leaf |
| 12078 | /// transform units into `out` as `(row offset, col offset, side)` in mi units |
| 12079 | /// -- exactly the order `decode_reconstruct_tx` later reads their |
| 12080 | /// coefficients in. `blk_row`/`blk_col` are mi offsets inside the block; |
| 12081 | /// `max_w_mi`/`max_h_mi` clip it to the true frame edge (`max_block_wide`/ |
| 12082 | /// `max_block_high`). |
| 12083 | #[allow(clippy::too_many_arguments)] |
| 12084 | fn read_var_tx_size( |
| 12085 | dec: &mut SymbolDecoder, |
| 12086 | cdfs: &mut Cdfs, |
| 12087 | n: &mut Neighbours, |
| 12088 | at_mi: (usize, usize), |
| 12089 | blk_max_px: usize, |
| 12090 | (max_w_mi, max_h_mi): (usize, usize), |
| 12091 | (tx_w, tx_h): (usize, usize), |
| 12092 | depth: usize, |
| 12093 | blk_row: usize, |
| 12094 | blk_col: usize, |
| 12095 | out: &mut Vec<(usize, usize, usize, usize)>, |
| 12096 | ) { |
| 12097 | if blk_row >= max_h_mi || blk_col >= max_w_mi { |
| 12098 | return; |
| 12099 | } |
| 12100 | let unit = (at_mi.0 + blk_row, at_mi.1 + blk_col); |
| 12101 | if depth == MAX_VARTX_DEPTH { |
| 12102 | out.push((blk_row, blk_col, tx_w, tx_h)); |
| 12103 | txfm_partition_update_rect(n, unit, (tx_w, tx_h), (tx_w, tx_h)); |
| 12104 | return; |
| 12105 | } |
| 12106 | let ctx = txfm_partition_ctx_rect( |
| 12107 | n.above_txfm[unit.1], |
| 12108 | n.left_txfm[unit.0], |
| 12109 | blk_max_px, |
| 12110 | tx_w, |
| 12111 | tx_h, |
| 12112 | ); |
| 12113 | let split = dec.symbol(&mut cdfs.txfm_partition[ctx]) == 1; |
| 12114 | TXFM_SPLIT_READS.with(|c| c.set(c.get() + 1)); |
| 12115 | if std::env::var_os("EC_TRACE_MODE_STEP").is_some() { |
| 12116 | let (rng, _) = dec.debug_state(); |
| 12117 | eprintln!( |
| 12118 | "EC_ISTEP mi_row={} mi_col={} name=txfm_split val={} ctx={ctx} rng={rng}", |
| 12119 | unit.0, |
| 12120 | unit.1, |
| 12121 | u8::from(split) |
| 12122 | ); |
| 12123 | } |
| 12124 | if !split { |
| 12125 | out.push((blk_row, blk_col, tx_w, tx_h)); |
| 12126 | txfm_partition_update_rect(n, unit, (tx_w, tx_h), (tx_w, tx_h)); |
| 12127 | return; |
| 12128 | } |
| 12129 | TXFM_SPLIT_HITS.with(|c| c.set(c.get() + 1)); |
| 12130 | let (sub_w, sub_h) = sub_tx_size_map(tx_w, tx_h); |
| 12131 | if (sub_w, sub_h) == (4, 4) { |
| 12132 | // libaom's `sub_txs == TX_4X4` early return: every mi cell of the |
| 12133 | // unit is marked 4x4 (raster order) without recursing. |
| 12134 | for row in 0..tx_h / MI { |
| 12135 | for col in 0..tx_w / MI { |
| 12136 | if blk_row + row < max_h_mi && blk_col + col < max_w_mi { |
| 12137 | out.push((blk_row + row, blk_col + col, 4, 4)); |
| 12138 | } |
| 12139 | } |
| 12140 | } |
| 12141 | txfm_partition_update_rect(n, unit, (4, 4), (tx_w, tx_h)); |
| 12142 | return; |
| 12143 | } |
| 12144 | for row in (0..tx_h / MI).step_by(sub_h / MI) { |
| 12145 | for col in (0..tx_w / MI).step_by(sub_w / MI) { |
| 12146 | read_var_tx_size( |
| 12147 | dec, |
| 12148 | cdfs, |
| 12149 | n, |
| 12150 | at_mi, |
| 12151 | blk_max_px, |
| 12152 | (max_w_mi, max_h_mi), |
| 12153 | (sub_w, sub_h), |
| 12154 | depth + 1, |
| 12155 | blk_row + row, |
| 12156 | blk_col + col, |
| 12157 | out, |
| 12158 | ); |
| 12159 | } |
| 12160 | } |
| 12161 | } |
| 12162 | |
| 12163 | /// `read_block_tx_size` (libaom `decodeframe.c` `parse_decode_block`'s tx |
| 12164 | /// branch): every block of a `TxMode::Select` inter frame codes its transform |
| 12165 | /// size right after its own mode info -- an inter non-skip block through the |
| 12166 | /// recursive var-tx tree ([`read_var_tx_size`], whose leaf list this returns), |
| 12167 | /// every other block through the intra `tx_depth` symbol (a skipped inter |
| 12168 | /// block codes no symbol at all) plus a `set_txfm_ctxs` write. Returns the |
| 12169 | /// block's own resolved transform side and the leaf list, the latter only |
| 12170 | /// when the tree resolved to more than one transform unit. |
| 12171 | #[allow(clippy::too_many_arguments)] |
| 12172 | fn read_block_tx_size( |
| 12173 | dec: &mut SymbolDecoder, |
| 12174 | cdfs: &mut Cdfs, |
| 12175 | n: &mut Neighbours, |
| 12176 | at_mi: (usize, usize), |
| 12177 | side: usize, |
| 12178 | (mi_cols, mi_rows): (usize, usize), |
| 12179 | is_inter: bool, |
| 12180 | skip: bool, |
| 12181 | ) -> Result<(usize, Option<Vec<(usize, usize, usize, usize)>>)> { |
| 12182 | if !TX_SELECT_INTER.with(std::cell::Cell::get) { |
| 12183 | return Ok((side, None)); |
| 12184 | } |
| 12185 | let side_mi = side / MI; |
| 12186 | if is_inter && !skip { |
| 12187 | let max_tx = side.min(64); |
| 12188 | let max_w_mi = side_mi.min(mi_cols.saturating_sub(at_mi.1)); |
| 12189 | let max_h_mi = side_mi.min(mi_rows.saturating_sub(at_mi.0)); |
| 12190 | let mut leaves = Vec::new(); |
| 12191 | let step = max_tx / MI; |
| 12192 | for row in (0..side_mi).step_by(step) { |
| 12193 | for col in (0..side_mi).step_by(step) { |
| 12194 | read_var_tx_size( |
| 12195 | dec, |
| 12196 | cdfs, |
| 12197 | n, |
| 12198 | at_mi, |
| 12199 | side, |
| 12200 | (max_w_mi, max_h_mi), |
| 12201 | (max_tx, max_tx), |
| 12202 | 0, |
| 12203 | row, |
| 12204 | col, |
| 12205 | &mut leaves, |
| 12206 | ); |
| 12207 | } |
| 12208 | } |
| 12209 | let single = leaves.len() == 1 && leaves[0].2 == side && leaves[0].3 == side; |
| 12210 | if !single && leaves.iter().any(|leaf| leaf.2.max(leaf.3) > 32) { |
| 12211 | // Only a block wider than a superblock's own 64x64 max transform |
| 12212 | // reaches here; this crate has no 64-point *inter* coefficient |
| 12213 | // table set ([`inter_txbset_for`] stops at 32). |
| 12214 | return Err(unsupported( |
| 12215 | "an inter var-tx tree with a leaf transform larger than 32x32", |
| 12216 | )); |
| 12217 | } |
| 12218 | let resolved = leaves.last().map_or(side, |l| l.2); |
| 12219 | return Ok((resolved, (!single).then_some(leaves))); |
| 12220 | } |
| 12221 | let tx = if is_inter { |
| 12222 | side.min(64) |
| 12223 | } else { |
| 12224 | read_tx_size( |
| 12225 | dec, |
| 12226 | cdfs, |
| 12227 | n, |
| 12228 | at_mi, |
| 12229 | side, |
| 12230 | Some(tx_size_context_txfm(n, at_mi, side)), |
| 12231 | ) |
| 12232 | }; |
| 12233 | set_txfm_ctxs(n, at_mi, tx, side_mi, side_mi, skip && is_inter); |
| 12234 | if !is_inter && tx != side { |
| 12235 | // lane-intrainter r1: an intra block inside an inter frame whose |
| 12236 | // `tx_depth` split its luma transform codes exactly the key-frame |
| 12237 | // split path's transform units (`decode_block`'s multi-TU loop), |
| 12238 | // raster order over the block. Handed back in the same leaf shape the |
| 12239 | // inter var-tx tree uses, so the caller's per-TU luma loop, the |
| 12240 | // `record_split_luma` neighbour write-back and the per-TU deblock |
| 12241 | // edges are the ones it already runs for a var-tx inter block. A |
| 12242 | // caller that has no per-TU intra loop (the 8x8 leaf) refuses on |
| 12243 | // seeing `Some` here. |
| 12244 | let step = tx / MI; |
| 12245 | let mut leaves = Vec::new(); |
| 12246 | for row in (0..side_mi).step_by(step) { |
| 12247 | for col in (0..side_mi).step_by(step) { |
| 12248 | leaves.push((row, col, tx, tx)); |
| 12249 | } |
| 12250 | } |
| 12251 | INTRA_IN_INTER_SPLIT_TX_HITS.with(|c| { |
| 12252 | let mut h = c.get(); |
| 12253 | h[(side.trailing_zeros() as usize).saturating_sub(3).min(3)] += 1; |
| 12254 | c.set(h); |
| 12255 | }); |
| 12256 | return Ok((tx, Some(leaves))); |
| 12257 | } |
| 12258 | Ok((tx, None)) |
| 12259 | } |
| 12260 | |
| 12261 | /// lane-inter4 r2: the shapes whose *rectangular inter* residual this |
| 12262 | /// decoder codes -- a 2:1 strip whose own max rect transform |
| 12263 | /// (`max_txsize_rect_lookup[BLOCK_32X16] == TX_32X16`) has both a |
| 12264 | /// coefficient table set ([`TxbSet::LumaRect32x16Inter`], 512-coefficient |
| 12265 | /// `eob_pt`) and a `tx_type` alphabet here. 64x32/64x16 strips would need a |
| 12266 | /// 64-point rect transform, and 16x8/8x16 needs |
| 12267 | /// `default_inter_ext_tx_cdf[2][TX_8X8]` (the 12-symbol |
| 12268 | /// `DTT9_IDTX_1DDCT` row at `tx_size_sqr == TX_8X8`, only reachable through a |
| 12269 | /// rect transform), neither of which exists yet -- both stay refused by name. |
| 12270 | fn rect_inter_residual_supported(w: usize, h: usize) -> bool { |
| 12271 | matches!( |
| 12272 | (w, h), |
| 12273 | // 2:1 strips whose transform is coded in full (lane-inter4). |
| 12274 | (32, 16) | (16, 32) | (16, 8) | (8, 16) |
| 12275 | // lane-r14 r2: strips with a 64-px axis. `av1_get_max_eob` codes |
| 12276 | // only the low 32 coefficients of a 64-length axis, so these read |
| 12277 | // exactly the truncated corner the INTRA superblock-strip path |
| 12278 | // already reads (`read_intra_mode_rect`), through |
| 12279 | // `av1_get_adjusted_tx_size`'s CDF set. Their `tx_size_sqr_up` is |
| 12280 | // `TX_64X64`, so `av1_get_ext_tx_set_type` is `DCT_ONLY` and no |
| 12281 | // `tx_type` symbol is coded at all. |
| 12282 | | (64, 32) | (32, 64) | (64, 16) | (16, 64) |
| 12283 | ) |
| 12284 | } |
| 12285 | |
| 12286 | /// The luma coefficient/`tx_type` table set of a whole-block rectangular |
| 12287 | /// INTER transform unit (lane-inter4 r3). `av1_get_ext_tx_set_type` splits |
| 12288 | /// the two shapes: a 32x16/16x32 unit has `tx_size_sqr_up == TX_32X32`, so |
| 12289 | /// it reads the 2-symbol `EXT_TX_SET_DCT_IDTX` set regardless of |
| 12290 | /// `reduced_tx_set`; a 16x8/8x16 unit has `tx_size_sqr_up == TX_16X16` and |
| 12291 | /// `tx_size_sqr == TX_8X8`, so it reads `EXT_TX_SET_DCT_IDTX` when reduced |
| 12292 | /// and the 16-symbol `EXT_TX_SET_ALL16` otherwise -- both at the *8x8* CDF |
| 12293 | /// row (`txsize_sqr_map`), never the 16x16 one. |
| 12294 | fn rect_inter_luma_set(w: usize, h: usize) -> TxbSet { |
| 12295 | match (w, h) { |
| 12296 | (32, 16) | (16, 32) => TxbSet::LumaRect32x16Inter, |
| 12297 | (16, 8) | (8, 16) => { |
| 12298 | if REDUCED_TX_SET_INTER.with(std::cell::Cell::get) { |
| 12299 | TxbSet::LumaRect16x8Inter |
| 12300 | } else { |
| 12301 | TxbSet::LumaRect16x8InterSet1 |
| 12302 | } |
| 12303 | } |
| 12304 | // lane-r14 r2: `av1_get_adjusted_tx_size` maps TX_64X32/TX_32X64 to |
| 12305 | // TX_32X32 and TX_64X16/TX_16X64 to TX_32X16/TX_16X32, and |
| 12306 | // `get_txsize_entropy_ctx`/`txsize_log2_minus4` agree with the |
| 12307 | // adjusted size, so the existing sets are bit-exact. Neither carries a |
| 12308 | // `tx_type` table (DCT_ONLY above), so the intra/inter split is moot. |
| 12309 | (64, 32) | (32, 64) => TxbSet::Luma64, |
| 12310 | (64, 16) | (16, 64) => TxbSet::LumaRect32x16, |
| 12311 | _ => unreachable!("rect_inter_residual_supported gates every shape that reaches here"), |
| 12312 | } |
| 12313 | } |
| 12314 | |
| 12315 | /// The chroma table set of the same block's single rectangular chroma unit |
| 12316 | /// (`av1_get_max_uv_txsize`): 16x8/8x16 under a 32x16/16x32 strip, 8x4/4x8 |
| 12317 | /// under a 16x8/8x16 one. Neither codes a `tx_type` symbol -- both inherit |
| 12318 | /// the luma unit's. |
| 12319 | fn rect_inter_chroma_set(w: usize, h: usize) -> TxbSet { |
| 12320 | match (w, h) { |
| 12321 | (16, 8) | (8, 16) => TxbSet::ChromaRect16x8, |
| 12322 | (8, 4) | (4, 8) => TxbSet::ChromaRect8x4, |
| 12323 | // lane-r14 r2: chroma of a 64-axis strip -- 32x16/16x32 under a |
| 12324 | // 64x32/32x64 one, 32x8/8x32 under a 64x16/16x64 one. Neither chroma |
| 12325 | // unit has a 64-px axis, so both are coded in full; `Chroma16` is the |
| 12326 | // exact set for TX_32X8/TX_8X32 (`get_txsize_entropy_ctx` = TX_16X16, |
| 12327 | // `txsize_log2_minus4` = 4), same as the intra strip path uses. |
| 12328 | (32, 16) | (16, 32) => TxbSet::ChromaRect32x16, |
| 12329 | (32, 8) | (8, 32) => TxbSet::Chroma16, |
| 12330 | _ => unreachable!("rect_inter_residual_supported gates every shape that reaches here"), |
| 12331 | } |
| 12332 | } |
| 12333 | |
| 12334 | /// libaom `size_group_lookup[bsize]` for the block shapes this decoder's |
| 12335 | /// inter path reaches, from the block's TRUE footprint rather than the |
| 12336 | /// square `side` corner-cut: 8x8/8x16/16x8 -> 1, 16x16/16x32/32x16 -> 2, |
| 12337 | /// everything 32 and above -> 3. |
| 12338 | fn size_group_wh(w: usize, h: usize) -> usize { |
| 12339 | // lane-inter4 r4: `size_group_lookup[BLOCK_SIZES_ALL]` (libaom |
| 12340 | // `common_data.h:60`) is 0 for every block with a 4-px side |
| 12341 | // (4x4/4x8/8x4/4x16/16x4), 1 for min side 8, 2 for 16, 3 above -- the old |
| 12342 | // `0..=8 => 1` row gave a sub-8 leaf group 1 where libaom says 0. |
| 12343 | match w.min(h) { |
| 12344 | 0..=4 => 0, |
| 12345 | 8 => 1, |
| 12346 | 16 => 2, |
| 12347 | _ => 3, |
| 12348 | } |
| 12349 | } |
| 12350 | |
| 12351 | #[test] |
| 12352 | fn size_group_wh_matches_libaom_size_group_lookup() { |
| 12353 | // `BLOCK_SIZES_ALL` in libaom's own order, with `size_group_lookup`'s |
| 12354 | // transcribed values (common_data.h:60-62). |
| 12355 | const TABLE: [((usize, usize), usize); 22] = [ |
| 12356 | ((4, 4), 0), ((4, 8), 0), ((8, 4), 0), ((8, 8), 1), ((8, 16), 1), ((16, 8), 1), |
| 12357 | ((16, 16), 2), ((16, 32), 2), ((32, 16), 2), ((32, 32), 3), ((32, 64), 3), |
| 12358 | ((64, 32), 3), ((64, 64), 3), ((64, 128), 3), ((128, 64), 3), ((128, 128), 3), |
| 12359 | ((4, 16), 0), ((16, 4), 0), ((8, 32), 1), ((32, 8), 1), ((16, 64), 2), ((64, 16), 2), |
| 12360 | ]; |
| 12361 | for ((w, h), want) in TABLE { |
| 12362 | assert_eq!(size_group_wh(w, h), want, "size_group_wh({w}, {h})"); |
| 12363 | } |
| 12364 | } |
| 12365 | |
| 12366 | /// The default (2D) coefficient scan of a rectangular transform. |
| 12367 | fn rect_scan(w: usize, h: usize) -> &'static [u16] { |
| 12368 | match (w, h) { |
| 12369 | (32, 16) => &SCAN_32X16, |
| 12370 | (16, 32) => &SCAN_16X32, |
| 12371 | (16, 8) => &SCAN_16X8, |
| 12372 | (8, 16) => &SCAN_8X16, |
| 12373 | (8, 4) => &SCAN_8X4, |
| 12374 | (4, 8) => &SCAN_4X8, |
| 12375 | (32, 8) => &SCAN_32X8, |
| 12376 | (8, 32) => &SCAN_8X32, |
| 12377 | _ => unreachable!("rect_inter_residual_supported gates every shape that reaches here"), |
| 12378 | } |
| 12379 | } |
| 12380 | |
| 12381 | /// [`read_block_tx_size`] for a rectangular inter block (lane-inter4 r2): |
| 12382 | /// spec 5.11.17 / libaom `read_tx_size_vartx` starting from the block's own |
| 12383 | /// `max_txsize_rect_lookup` entry, which for a 2:1 strip IS rectangular. The |
| 12384 | /// depth-0 `txfm_partition` symbol reads its context off the rect transform's |
| 12385 | /// own width/height; a `0` leaves one whole-block rect transform unit |
| 12386 | /// (`None`), and a `1` splits it into `sub_tx_size_map`'s two SQUARE |
| 12387 | /// sub-transforms (`sub_tx_size_map[TX_32X16] == TX_16X16`), from which the |
| 12388 | /// ordinary square [`read_var_tx_size`] recursion continues at depth 1 -- so |
| 12389 | /// every leaf of a split rect block is square and the existing square leaf |
| 12390 | /// loop reconstructs it unchanged. |
| 12391 | #[allow(clippy::too_many_arguments)] |
| 12392 | fn read_block_tx_size_rect( |
| 12393 | dec: &mut SymbolDecoder, |
| 12394 | cdfs: &mut Cdfs, |
| 12395 | n: &mut Neighbours, |
| 12396 | at_mi: (usize, usize), |
| 12397 | (bw, bh): (usize, usize), |
| 12398 | (mi_cols, mi_rows): (usize, usize), |
| 12399 | skip: bool, |
| 12400 | ) -> Result<Option<Vec<(usize, usize, usize, usize)>>> { |
| 12401 | if skip { |
| 12402 | // `set_txfm_ctxs(.., skip && is_inter)`: a skipped inter block records |
| 12403 | // its own BLOCK size -- width above, height left -- not a transform. |
| 12404 | txfm_partition_update_rect(n, at_mi, (bw, bh), (bw, bh)); |
| 12405 | return Ok(None); |
| 12406 | } |
| 12407 | // `max_txsize_rect_lookup` is the block's own size, capped at 64 -- and |
| 12408 | // `rect_inter_residual_supported` has already excluded everything above 32. |
| 12409 | let (tx_w, tx_h) = (bw, bh); |
| 12410 | if !TX_SELECT_INTER.with(std::cell::Cell::get) { |
| 12411 | // `TX_MODE_LARGEST`: no symbol at all, the whole block is one |
| 12412 | // transform, and `set_txfm_ctxs` records exactly what |
| 12413 | // `txfm_partition_update` would for that same unit. |
| 12414 | txfm_partition_update_rect(n, at_mi, (tx_w, tx_h), (bw, bh)); |
| 12415 | RECT_INTER_TU_HITS.with(|c| c.set(c.get() + 1)); |
| 12416 | return Ok(None); |
| 12417 | } |
| 12418 | let ctx = txfm_partition_ctx_rect( |
| 12419 | n.above_txfm[at_mi.1], |
| 12420 | n.left_txfm[at_mi.0], |
| 12421 | bw.max(bh), |
| 12422 | tx_w, |
| 12423 | tx_h, |
| 12424 | ); |
| 12425 | let split = dec.symbol(&mut cdfs.txfm_partition[ctx]) == 1; |
| 12426 | TXFM_SPLIT_READS.with(|c| c.set(c.get() + 1)); |
| 12427 | if std::env::var_os("EC_TRACE_MODE_STEP").is_some() { |
| 12428 | let (rng, _) = dec.debug_state(); |
| 12429 | eprintln!( |
| 12430 | "EC_ISTEP mi_row={} mi_col={} name=txfm_split_rect val={} ctx={ctx} rng={rng}", |
| 12431 | at_mi.0, |
| 12432 | at_mi.1, |
| 12433 | u8::from(split) |
| 12434 | ); |
| 12435 | } |
| 12436 | if !split { |
| 12437 | txfm_partition_update_rect(n, at_mi, (tx_w, tx_h), (bw, bh)); |
| 12438 | RECT_INTER_TU_HITS.with(|c| c.set(c.get() + 1)); |
| 12439 | return Ok(None); |
| 12440 | } |
| 12441 | TXFM_SPLIT_HITS.with(|c| c.set(c.get() + 1)); |
| 12442 | RECT_INTER_TXSPLIT_HITS.with(|c| c.set(c.get() + 1)); |
| 12443 | // lane-r14 r3: `sub_tx_size_map` (libaom `common_data.c`) stays |
| 12444 | // RECTANGULAR under a 1:4 strip -- `[TX_64X16] == TX_32X16`, |
| 12445 | // `[TX_16X64] == TX_16X32` -- so the recursion below is the general |
| 12446 | // rect one and its leaves carry their own width and height. |
| 12447 | let (sub_w, sub_h) = sub_tx_size_map(tx_w, tx_h); |
| 12448 | let max_w_mi = (bw / MI).min(mi_cols.saturating_sub(at_mi.1)); |
| 12449 | let max_h_mi = (bh / MI).min(mi_rows.saturating_sub(at_mi.0)); |
| 12450 | let mut leaves = Vec::new(); |
| 12451 | for row in (0..bh / MI).step_by(sub_h / MI) { |
| 12452 | for col in (0..bw / MI).step_by(sub_w / MI) { |
| 12453 | read_var_tx_size( |
| 12454 | dec, |
| 12455 | cdfs, |
| 12456 | n, |
| 12457 | at_mi, |
| 12458 | bw.max(bh), |
| 12459 | (max_w_mi, max_h_mi), |
| 12460 | (sub_w, sub_h), |
| 12461 | 1, |
| 12462 | row, |
| 12463 | col, |
| 12464 | &mut leaves, |
| 12465 | ); |
| 12466 | } |
| 12467 | } |
| 12468 | Ok(Some(leaves)) |
| 12469 | } |
| 12470 | |
| 12471 | /// [`read_inter_plane`] for a rectangular transform unit (lane-inter4 r2): |
| 12472 | /// the coefficients are read with the rect coefficient machinery |
| 12473 | /// ([`read_coeffs_rect`], whose `TxClass` handling covers the 1D `V_*`/`H_*` |
| 12474 | /// types), inverted with the rect inverse transform |
| 12475 | /// ([`dequant_and_inverse_typed_wh`], which carries the 2:1 `sqrt2` scale), |
| 12476 | /// and added onto the top-left `w x h` of the block's own square-strided |
| 12477 | /// motion-compensated prediction buffer. Returns the *rect* coefficient grid |
| 12478 | /// and the coded `tx_type`. |
| 12479 | /// |
| 12480 | /// `txb_skip_ctx` is 0 for luma: this unit covers the whole block, i.e. |
| 12481 | /// `get_txb_ctx`'s `plane_bsize == txsize_to_bsize[tx_size]` branch. |
| 12482 | #[allow(clippy::too_many_arguments)] |
| 12483 | fn read_inter_plane_rect( |
| 12484 | dec: &mut SymbolDecoder, |
| 12485 | cdfs: &mut Cdfs, |
| 12486 | set: TxbSet, |
| 12487 | (w, h): (usize, usize), |
| 12488 | stride: usize, |
| 12489 | plane_idx: usize, |
| 12490 | around: (bool, bool, i32), |
| 12491 | tx_mode: usize, |
| 12492 | plane: &mut PlaneBuf, |
| 12493 | x: usize, |
| 12494 | y: usize, |
| 12495 | prediction: &[u16], |
| 12496 | inherited_luma_tx_type: Option<TxType>, |
| 12497 | luma_skip_ctx: Option<usize>, |
| 12498 | ) -> Result<(Vec<i32>, TxType)> { |
| 12499 | let skip_ctx = if plane_idx == 0 { |
| 12500 | // `get_txb_ctx_general`: 0 only when this unit IS the whole plane |
| 12501 | // block; a var-tx leaf passes its own neighbour-table context. |
| 12502 | luma_skip_ctx.unwrap_or(0) |
| 12503 | } else { |
| 12504 | usize::from(around.0) + usize::from(around.1) |
| 12505 | }; |
| 12506 | let mut coding = cdfs.txb(set, tx_mode); |
| 12507 | // `av1_get_ext_tx_set_type`'s DCT-only clamp is on `tx_size_sqr_up`, i.e. |
| 12508 | // the *longer* side of this rect transform. |
| 12509 | let default_tx_type = match inherited_luma_tx_type { |
| 12510 | Some(t) => reduce_inherited_chroma_tx_type(t, w, h), |
| 12511 | None => TxType::DctDct, |
| 12512 | }; |
| 12513 | // lane-r14 r2: a 64-length axis codes only its low 32 coefficients |
| 12514 | // (`av1_get_max_eob`), so the coded unit is the `min(w,32) x min(h,32)` |
| 12515 | // corner, zero-extended into the true `w x h` grid before the inverse |
| 12516 | // transform -- exactly what the intra superblock-strip path does. A |
| 12517 | // corner that comes out square (32x32, under a 64x32/32x64 strip) reads |
| 12518 | // through the SQUARE coefficient reader with `rect_shape = Some((w, h))`, |
| 12519 | // because `av1_nz_map_ctx_offset` is indexed by the real, un-adjusted |
| 12520 | // rectangular shape (lane-sbpart r8). |
| 12521 | let (cw, ch) = (w.min(32), h.min(32)); |
| 12522 | if plane_idx == 0 && w.max(h) == 64 { |
| 12523 | RECT64_INTER_TU_HITS.with(|c| c.set(c.get() + 1)); |
| 12524 | } |
| 12525 | let (corner, tx_type) = if (cw, ch) == (32, 32) { |
| 12526 | let scan32 = default_scan(TX32); |
| 12527 | read_coeffs( |
| 12528 | dec, |
| 12529 | &mut coding, |
| 12530 | &scan32, |
| 12531 | skip_ctx, |
| 12532 | dc_sign_ctx(around.2), |
| 12533 | default_tx_type, |
| 12534 | Some((w, h)), |
| 12535 | )? |
| 12536 | } else { |
| 12537 | read_coeffs_rect( |
| 12538 | dec, |
| 12539 | &mut coding, |
| 12540 | rect_scan(cw, ch), |
| 12541 | cw, |
| 12542 | ch, |
| 12543 | skip_ctx, |
| 12544 | dc_sign_ctx(around.2), |
| 12545 | default_tx_type, |
| 12546 | )? |
| 12547 | }; |
| 12548 | let grid = if (cw, ch) == (w, h) { |
| 12549 | corner |
| 12550 | } else { |
| 12551 | let mut full = vec![0i32; w * h]; |
| 12552 | for row in 0..ch { |
| 12553 | full[row * w..][..cw].copy_from_slice(&corner[row * cw..][..cw]); |
| 12554 | } |
| 12555 | full |
| 12556 | }; |
| 12557 | let (dc_delta, ac_delta) = plane_q_delta(plane_idx); |
| 12558 | let residual = dequant_and_inverse_typed_wh( |
| 12559 | &grid, |
| 12560 | w, |
| 12561 | h, |
| 12562 | bit_depth(), |
| 12563 | block_q_idx(), |
| 12564 | dc_delta, |
| 12565 | ac_delta, |
| 12566 | tx_type, |
| 12567 | ); |
| 12568 | // `reconstruct_mc_rect` reads both buffers at the square prediction |
| 12569 | // stride, so the dense `w x h` residual is re-laid at that stride first. |
| 12570 | let mut strided = vec![0i32; stride * stride]; |
| 12571 | for row in 0..h { |
| 12572 | strided[row * stride..][..w].copy_from_slice(&residual[row * w..][..w]); |
| 12573 | } |
| 12574 | plane.reconstruct_mc_rect(x, y, stride, w, h, prediction, &strided); |
| 12575 | Ok((grid, tx_type)) |
| 12576 | } |
| 12577 | |
| 12578 | /// A rect coefficient grid re-laid into the square `stride x stride` grid the |
| 12579 | /// neighbour/deblock stamps downstream of the residual expect. |
| 12580 | fn embed_rect_grid(grid: &[i32], w: usize, h: usize, stride: usize) -> Vec<i32> { |
| 12581 | let mut full = vec![0i32; stride * stride]; |
| 12582 | for row in 0..h { |
| 12583 | full[row * stride..][..w].copy_from_slice(&grid[row * w..][..w]); |
| 12584 | } |
| 12585 | full |
| 12586 | } |
| 12587 | |
| 12588 | /// One 8x8 inter leaf's luma residual (lane-txselect): either the whole-8x8 |
| 12589 | /// transform, or -- when [`read_block_tx_size`]'s var-tx tree took the single |
| 12590 | /// split a `BLOCK_8X8` can take (`split`) -- the four 4x4 transform units in |
| 12591 | /// raster order, each reading its own coefficient context off the units |
| 12592 | /// already decoded before it. Returns the block's luma grid (all-zero in the |
| 12593 | /// split case, whose reconstruction has already happened unit by unit) and |
| 12594 | /// the `tx_type` chroma inherits: `av1_get_tx_type`'s colocated luma lookup, |
| 12595 | /// which for a split block lands on the top-left unit. |
| 12596 | #[allow(clippy::too_many_arguments)] |
| 12597 | fn read_inter_luma8( |
| 12598 | dec: &mut SymbolDecoder, |
| 12599 | cdfs: &mut Cdfs, |
| 12600 | neighbours: &mut Neighbours, |
| 12601 | leaf_mi: (usize, usize), |
| 12602 | y: &mut PlaneBuf, |
| 12603 | px: usize, |
| 12604 | py: usize, |
| 12605 | pred_y: &[u16], |
| 12606 | mode_for_tx: usize, |
| 12607 | base_q_idx: u8, |
| 12608 | scan8: &[u16], |
| 12609 | scan4: &[u16], |
| 12610 | reduced_tx_set: bool, |
| 12611 | split: bool, |
| 12612 | ) -> Result<(Vec<i32>, TxType)> { |
| 12613 | if !split { |
| 12614 | return read_inter_plane( |
| 12615 | dec, |
| 12616 | cdfs, |
| 12617 | if reduced_tx_set { |
| 12618 | TxbSet::Luma8Inter |
| 12619 | } else { |
| 12620 | TxbSet::Luma8InterSet1 |
| 12621 | }, |
| 12622 | scan8, |
| 12623 | 0, |
| 12624 | neighbours.around_mi(leaf_mi, 8)[0], |
| 12625 | mode_for_tx, |
| 12626 | y, |
| 12627 | px, |
| 12628 | py, |
| 12629 | 8, |
| 12630 | base_q_idx, |
| 12631 | pred_y, |
| 12632 | None, |
| 12633 | None, |
| 12634 | ); |
| 12635 | } |
| 12636 | let mut first = TxType::DctDct; |
| 12637 | for tu_row in 0..2 { |
| 12638 | for tu_col in 0..2 { |
| 12639 | let tu_mi = (leaf_mi.0 + tu_row, leaf_mi.1 + tu_col); |
| 12640 | let mut tu_pred = Vec::with_capacity(16); |
| 12641 | for rr in 0..4 { |
| 12642 | let start = (tu_row * 4 + rr) * 8 + tu_col * 4; |
| 12643 | tu_pred.extend_from_slice(&pred_y[start..start + 4]); |
| 12644 | } |
| 12645 | let tu_skip_ctx = neighbours.luma_skip_ctx(tu_mi, 1); |
| 12646 | let (tu_grid, tu_tx_type) = read_inter_plane( |
| 12647 | dec, |
| 12648 | cdfs, |
| 12649 | if reduced_tx_set { |
| 12650 | TxbSet::Luma4Inter |
| 12651 | } else { |
| 12652 | TxbSet::Luma4InterSet1 |
| 12653 | }, |
| 12654 | scan4, |
| 12655 | 0, |
| 12656 | neighbours.around_mi(tu_mi, 4)[0], |
| 12657 | mode_for_tx, |
| 12658 | y, |
| 12659 | px + tu_col * 4, |
| 12660 | py + tu_row * 4, |
| 12661 | 4, |
| 12662 | base_q_idx, |
| 12663 | &tu_pred, |
| 12664 | None, |
| 12665 | Some(tu_skip_ctx), |
| 12666 | )?; |
| 12667 | if tu_row == 0 && tu_col == 0 { |
| 12668 | first = tu_tx_type; |
| 12669 | } |
| 12670 | neighbours.record_mi_luma(tu_mi, 4, &tu_grid); |
| 12671 | } |
| 12672 | } |
| 12673 | Ok((vec![0i32; 64], first)) |
| 12674 | } |
| 12675 | |
| 12676 | /// The [`TxbSet`] one leaf of an inter block's var-tx tree reads its |
| 12677 | /// coefficient tables from -- the inter counterpart of [`txbset_for`]. |
| 12678 | /// `av1_get_tx_type`'s chroma tail (libaom `av1/common/blockd.h:1291-1305`): |
| 12679 | /// an inter block's chroma transform inherits the *colocated luma* unit's |
| 12680 | /// coded `tx_type`, then drops back to `DCT_DCT` unless that type is a member |
| 12681 | /// of the extended set the CHROMA transform size selects |
| 12682 | /// (`av1_ext_tx_used[av1_get_ext_tx_set_type(uv_tx, 1, reduced)][tx_type]`, |
| 12683 | /// `blockd.h:1036`/`1097`). lane-golomb r7: we used to force `DCT_DCT` at |
| 12684 | /// every chroma side >= 32, but `av1_get_ext_tx_set_type`'s DCT-only rung is |
| 12685 | /// `tx_size_sqr_up > TX_32X32`; at exactly TX_32X32 an inter block reads |
| 12686 | /// `EXT_TX_SET_DCT_IDTX`, so a 64x64 inter block whose first luma leaf coded |
| 12687 | /// `IDTX` inherits IDTX on its 32x32 chroma units. `IDTX` and `DCT_DCT` are |
| 12688 | /// both `TX_CLASS_2D`, so that mistake left the entropy stream perfectly in |
| 12689 | /// sync and only corrupted the inverse transform (one 64x64 superblock of |
| 12690 | /// high-frequency chroma noise, luma bit-exact). |
| 12691 | fn reduce_inherited_chroma_tx_type(t: TxType, w: usize, h: usize) -> TxType { |
| 12692 | use TxType::*; |
| 12693 | let sqr_up = w.max(h); |
| 12694 | let sqr = w.min(h); |
| 12695 | let reduced = REDUCED_TX_SET_INTER.with(std::cell::Cell::get); |
| 12696 | // `av1_get_ext_tx_set_type(tx_size, is_inter = 1, reduced)`. |
| 12697 | let allowed: &[TxType] = if sqr_up > 32 { |
| 12698 | // EXT_TX_SET_DCTONLY |
| 12699 | &[DctDct] |
| 12700 | } else if sqr_up == 32 || reduced { |
| 12701 | // EXT_TX_SET_DCT_IDTX |
| 12702 | &[DctDct, Idtx] |
| 12703 | } else if sqr == 16 { |
| 12704 | // EXT_TX_SET_DTT9_IDTX_1DDCT -- TX_TYPES 0..=11, i.e. everything but |
| 12705 | // the four 1D ADST/FLIPADST types. |
| 12706 | &[ |
| 12707 | DctDct, AdstDct, DctAdst, AdstAdst, FlipAdstDct, DctFlipAdst, FlipAdstFlipAdst, |
| 12708 | AdstFlipAdst, FlipAdstAdst, Idtx, VDct, HDct, |
| 12709 | ] |
| 12710 | } else { |
| 12711 | // EXT_TX_SET_ALL16 |
| 12712 | return t; |
| 12713 | }; |
| 12714 | if allowed.contains(&t) { t } else { DctDct } |
| 12715 | } |
| 12716 | |
| 12717 | fn inter_txbset_for(tx_px: usize, reduced_tx_set: bool) -> TxbSet { |
| 12718 | match tx_px { |
| 12719 | 32 => TxbSet::Luma32Inter, |
| 12720 | 16 if reduced_tx_set => TxbSet::Luma16Inter, |
| 12721 | 16 => TxbSet::Luma16InterSet1, |
| 12722 | 8 if reduced_tx_set => TxbSet::Luma8Inter, |
| 12723 | 8 => TxbSet::Luma8InterSet1, |
| 12724 | 4 if reduced_tx_set => TxbSet::Luma4Inter, |
| 12725 | 4 => TxbSet::Luma4InterSet1, |
| 12726 | _ => unreachable!("a var-tx leaf is never wider than a 64x64 block's 32x32 sub-transform"), |
| 12727 | } |
| 12728 | } |
| 12729 | |
| 12730 | /// The luma coefficient scan table for a resolved transform side -- 32/16/8/4, |
| 12731 | /// the only four sizes a `TxMode::Select` block's coded coefficient grid |
| 12732 | /// ([`read_tx_size`]'s resolved side, or 32 at the 64x64 corner case) can be. |
| 12733 | fn scan_for<'a>( |
| 12734 | tx_px: usize, |
| 12735 | scan32: &'a [u16], |
| 12736 | scan16: &'a [u16], |
| 12737 | scan8: &'a [u16], |
| 12738 | scan4: &'a [u16], |
| 12739 | ) -> &'a [u16] { |
| 12740 | match tx_px { |
| 12741 | 32 => scan32, |
| 12742 | 16 => scan16, |
| 12743 | 8 => scan8, |
| 12744 | 4 => scan4, |
| 12745 | _ => unreachable!("read_tx_size never resolves the coefficient grid to anything else"), |
| 12746 | } |
| 12747 | } |
| 12748 | |
| 12749 | /// The [`TxbSet`] a resolved luma transform side reads its coefficient tables |
| 12750 | /// from -- 64 is only ever the 64x64-block, depth-0, corner-scanned case |
| 12751 | /// ([`TxbSet::Luma64`]); every other resolved side maps to its own matching |
| 12752 | /// set. `get_tx_set` (spec 5.11.48) only splits on `reduced_tx_set` at |
| 12753 | /// `TX_8X8`/`TX_4X4`: an intra `TX_16X16` always reads `TX_SET_INTRA_2` |
| 12754 | /// regardless of the flag (`av1_get_ext_tx_set_type`'s |
| 12755 | /// `tx_size_sqr == TX_16X16` branch ignores `use_reduced_set` once it is past |
| 12756 | /// the `TX_32X32`/reduced-set checks), so only the 8x8 and 4x4 cases have a |
| 12757 | /// second table ([`TxbSet::Luma8Set1`]/[`TxbSet::Luma4Set1`]). |
| 12758 | /// [`txbset_for`], for a block libaom's `is_inter_block` calls inter -- which |
| 12759 | /// an intrabc block is (`blockd.h:373`), so its `tx_type` comes off the |
| 12760 | /// inter `ext_tx` sets, never the mode-indexed intra ones. |
| 12761 | fn txbset_for_inter(tx_px: usize, reduced_tx_set: bool) -> TxbSet { |
| 12762 | match tx_px { |
| 12763 | 64 => TxbSet::Luma64, |
| 12764 | 32 => TxbSet::Luma32Inter, |
| 12765 | 16 if reduced_tx_set => TxbSet::Luma16Inter, |
| 12766 | 16 => TxbSet::Luma16InterSet1, |
| 12767 | 8 if reduced_tx_set => TxbSet::Luma8Inter, |
| 12768 | 8 => TxbSet::Luma8InterSet1, |
| 12769 | _ => TxbSet::Luma64, |
| 12770 | } |
| 12771 | } |
| 12772 | |
| 12773 | fn txbset_for(tx_px: usize, reduced_tx_set: bool) -> TxbSet { |
| 12774 | match tx_px { |
| 12775 | 64 => TxbSet::Luma64, |
| 12776 | 32 => TxbSet::Luma32, |
| 12777 | 16 => TxbSet::Luma16, |
| 12778 | 8 if reduced_tx_set => TxbSet::Luma8, |
| 12779 | 8 => TxbSet::Luma8Set1, |
| 12780 | 4 if reduced_tx_set => TxbSet::Luma4, |
| 12781 | 4 => TxbSet::Luma4Set1, |
| 12782 | _ => unreachable!("read_tx_size never resolves a block's own side to anything else"), |
| 12783 | } |
| 12784 | } |
| 12785 | |
| 12786 | /// `tx_size_wide_unit_log2`/`tx_size_high_unit_log2`: `log2(px / 4)`, the |
| 12787 | /// index [`tx_dim_to_filter_length`] and the chroma 4-vs-6 choice key on. |
| 12788 | fn unit_log2(px: u32) -> i32 { |
| 12789 | if px == 0 { |
| 12790 | 0 |
| 12791 | } else { |
| 12792 | px.trailing_zeros() as i32 - 2 |
| 12793 | } |
| 12794 | } |
| 12795 | |
| 12796 | /// spec 7.14.4's `get_filter_level`: `ref_deltas` is indexed by the block's |
| 12797 | /// reference frame, `mode_deltas` by `mode_lf_lut[mode]` -- `0` for |
| 12798 | /// `GLOBALMV`, `1` for `NEARESTMV`/`NEARMV`/`NEWMV` (intra gets no mode |
| 12799 | /// delta). `ref_frame` here is [`Neighbours::ref_grid`]'s packed cell: |
| 12800 | /// magnitude = reference (`0` = intra), sign = `GLOBALMV` (lane-av1golden2: |
| 12801 | /// the flat `mode_deltas[1]` this used before was a `LAST_FRAME`-reachable |
| 12802 | /// wrong delta on every `GLOBALMV` block, masked while any `GOLDEN_FRAME` |
| 12803 | /// block anywhere aborted the whole stream's decode before comparison). |
| 12804 | fn lf_level( |
| 12805 | lf: &LoopFilterParams, |
| 12806 | plane_idx: usize, |
| 12807 | dir: usize, |
| 12808 | ref_frame: i8, |
| 12809 | delta_lf: i32, |
| 12810 | segment_id: u8, |
| 12811 | ) -> i32 { |
| 12812 | let base = i32::from(if plane_idx == 0 { |
| 12813 | lf.level[dir] |
| 12814 | } else if plane_idx == 1 { |
| 12815 | lf.level[2] |
| 12816 | } else { |
| 12817 | lf.level[3] |
| 12818 | }); |
| 12819 | // spec 7.14.4: the per-superblock `DeltaLF` term applies before the |
| 12820 | // ref/mode delta scaling below, and regardless of `delta_enabled` |
| 12821 | // (that flag only gates `ref_deltas`/`mode_deltas`). |
| 12822 | let base = (base + delta_lf).clamp(0, 63); |
| 12823 | // lane-seg, spec 7.14.4 / libaom `av1_loop_filter_frame_init`: this |
| 12824 | // block's segment's own `SEG_LVL_ALT_LF_*` delta applies after the |
| 12825 | // per-superblock `DeltaLF` term and before the ref/mode deltas below. |
| 12826 | // Feature index: `SEG_LVL_ALT_LF_Y_V + dir` for luma, `_U`/`_V` (3/4) |
| 12827 | // for chroma. |
| 12828 | let feature = if plane_idx == 0 { SEG_LVL_ALT_LF_Y_V + dir } else { plane_idx + 2 }; |
| 12829 | let base = match seg_feature(usize::from(segment_id), feature) { |
| 12830 | Some(data) => (base + data).clamp(0, 63), |
| 12831 | None => base, |
| 12832 | }; |
| 12833 | if !lf.delta_enabled { |
| 12834 | return base; |
| 12835 | } |
| 12836 | let scale = 1i32 << (base >> 5); |
| 12837 | let is_inter = ref_frame != 0; |
| 12838 | let is_globalmv = ref_frame < 0; |
| 12839 | let ref_idx = ref_frame.unsigned_abs() as usize; |
| 12840 | let mut lvl = base + i32::from(lf.ref_deltas[ref_idx]) * scale; |
| 12841 | if is_inter { |
| 12842 | lvl += i32::from(lf.mode_deltas[usize::from(!is_globalmv)]) * scale; |
| 12843 | } |
| 12844 | lvl.clamp(0, 63) |
| 12845 | } |
| 12846 | |
| 12847 | /// Spec 7.14.2's `set_lpf_parameters`, restricted to the uniform-level, |
| 12848 | /// no-segmentation, no-delta-LF path and to this decoder's own invariant |
| 12849 | /// that a coded block's transform is always its own full size -- so a TU |
| 12850 | /// edge is always also a PU edge, and the "both sides skipped, not a PU |
| 12851 | /// edge" suppression (which only ever fires at a non-PU-edge TU boundary) |
| 12852 | /// never applies here, letting this skip the `skip`/`skip_txfm` lookup |
| 12853 | /// entirely. Returns `None` when this 4-pixel edge group is not filtered. |
| 12854 | fn edge_params( |
| 12855 | lf: &LoopFilterParams, |
| 12856 | n: &Neighbours, |
| 12857 | plane_idx: usize, |
| 12858 | dir: usize, |
| 12859 | chroma: bool, |
| 12860 | x0: usize, |
| 12861 | y0: usize, |
| 12862 | ) -> Option<(u8, i32)> { |
| 12863 | let (mi_r, mi_c) = (plane_to_mi(chroma, y0), plane_to_mi(chroma, x0)); |
| 12864 | let cur_tx = if dir == 0 { |
| 12865 | tx_px_at(n, chroma, mi_r, mi_c) |
| 12866 | } else { |
| 12867 | tx_h_px_at(n, chroma, mi_r, mi_c) |
| 12868 | }; |
| 12869 | if cur_tx == 0 || (if dir == 0 { x0 } else { y0 }) % cur_tx as usize != 0 { |
| 12870 | return None; |
| 12871 | } |
| 12872 | let cur_ref = n.ref_grid[mi_r * n.skip_grid_cols_mi + mi_c]; |
| 12873 | let (pv_mi_r, pv_mi_c) = if dir == 0 { |
| 12874 | (mi_r, mi_c - 1) |
| 12875 | } else { |
| 12876 | (mi_r - 1, mi_c) |
| 12877 | }; |
| 12878 | let pv_tx = if dir == 0 { |
| 12879 | tx_px_at(n, chroma, pv_mi_r, pv_mi_c) |
| 12880 | } else { |
| 12881 | tx_h_px_at(n, chroma, pv_mi_r, pv_mi_c) |
| 12882 | }; |
| 12883 | let pv_ref = n.ref_grid[pv_mi_r * n.skip_grid_cols_mi + pv_mi_c]; |
| 12884 | let delta_idx = if plane_idx == 0 { dir } else { plane_idx + 1 }; |
| 12885 | let cur_delta_lf = i32::from(n.delta_lf_grid[mi_r * n.skip_grid_cols_mi + mi_c][delta_idx]); |
| 12886 | let pv_delta_lf = i32::from(n.delta_lf_grid[pv_mi_r * n.skip_grid_cols_mi + pv_mi_c][delta_idx]); |
| 12887 | |
| 12888 | let cur_level = lf_level(lf, plane_idx, dir, cur_ref, cur_delta_lf, segment_id_at(mi_r, mi_c)); |
| 12889 | let pv_level = lf_level( |
| 12890 | lf, |
| 12891 | plane_idx, |
| 12892 | dir, |
| 12893 | pv_ref, |
| 12894 | pv_delta_lf, |
| 12895 | segment_id_at(pv_mi_r, pv_mi_c), |
| 12896 | ); |
| 12897 | if cur_level == 0 && pv_level == 0 { |
| 12898 | return None; |
| 12899 | } |
| 12900 | let dim = unit_log2(cur_tx).min(unit_log2(pv_tx)).clamp(0, 4) as usize; |
| 12901 | let len: u8 = if chroma { |
| 12902 | if dim == 0 { 4 } else { 6 } |
| 12903 | } else { |
| 12904 | [4, 8, 14, 14, 14][dim] |
| 12905 | }; |
| 12906 | let level = if cur_level != 0 { cur_level } else { pv_level }; |
| 12907 | DEBLOCK_HITS.with(|c| c.set(c.get() + 1)); |
| 12908 | Some((len, level)) |
| 12909 | } |
| 12910 | |
| 12911 | /// `update_sharpness`'s per-level `(blimit, limit, hev_thr)` triple. |
| 12912 | fn deblock_thresholds(level: i32, sharpness: u8) -> (i32, i32, i32) { |
| 12913 | let sharpness = i32::from(sharpness); |
| 12914 | let shift = i32::from(sharpness > 0) + i32::from(sharpness > 4); |
| 12915 | let mut lim = level >> shift; |
| 12916 | if sharpness > 0 { |
| 12917 | lim = lim.min(9 - sharpness); |
| 12918 | } |
| 12919 | lim = lim.max(1); |
| 12920 | (2 * (level + 2) + lim, lim, level >> 4) |
| 12921 | } |
| 12922 | |
| 12923 | fn sclamp(v: i32) -> i32 { |
| 12924 | // libaom's `signed_char_clamp_high`: the clamp range scales with |
| 12925 | // `128 << (bit_depth - 8)` alongside the centering constant below. |
| 12926 | let scale = 1i32 << (bit_depth() - 8); |
| 12927 | v.clamp(-128 * scale, 128 * scale - 1) |
| 12928 | } |
| 12929 | |
| 12930 | fn round_pow2(v: i32, n: u32) -> i32 { |
| 12931 | (v + (1 << (n - 1))) >> n |
| 12932 | } |
| 12933 | |
| 12934 | /// `filter4`: the narrow 4-tap kernel, and every wider kernel's non-flat |
| 12935 | /// fallback. |
| 12936 | fn filter4(mask: bool, thresh: i32, p1: i32, p0: i32, q0: i32, q1: i32) -> [i32; 4] { |
| 12937 | if !mask { |
| 12938 | return [p1, p0, q0, q1]; |
| 12939 | } |
| 12940 | let hev = (p1 - p0).abs() > thresh || (q1 - q0).abs() > thresh; |
| 12941 | let centre = 128i32 << (bit_depth() - 8); |
| 12942 | let (ps1, ps0, qs0, qs1) = (p1 - centre, p0 - centre, q0 - centre, q1 - centre); |
| 12943 | let outer = if hev { sclamp(ps1 - qs1) } else { 0 }; |
| 12944 | let filter = sclamp(outer + 3 * (qs0 - ps0)); |
| 12945 | let filter1 = sclamp(filter + 4) >> 3; |
| 12946 | let filter2 = sclamp(filter + 3) >> 3; |
| 12947 | let oq0 = sclamp(qs0 - filter1) + centre; |
| 12948 | let op0 = sclamp(ps0 + filter2) + centre; |
| 12949 | let tap = if hev { 0 } else { round_pow2(filter1, 1) }; |
| 12950 | let oq1 = sclamp(qs1 - tap) + centre; |
| 12951 | let op1 = sclamp(ps1 + tap) + centre; |
| 12952 | [op1, op0, oq0, oq1] |
| 12953 | } |
| 12954 | |
| 12955 | /// `filter6`: chroma's flat 5-tap smoothing (spec `[1,2,2,2,1]`) over |
| 12956 | /// `filter4`'s narrow taps. |
| 12957 | #[allow(clippy::too_many_arguments)] |
| 12958 | fn filter6( |
| 12959 | mask: bool, |
| 12960 | thresh: i32, |
| 12961 | flat: bool, |
| 12962 | p2: i32, |
| 12963 | p1: i32, |
| 12964 | p0: i32, |
| 12965 | q0: i32, |
| 12966 | q1: i32, |
| 12967 | q2: i32, |
| 12968 | ) -> [i32; 4] { |
| 12969 | if flat && mask { |
| 12970 | [ |
| 12971 | round_pow2(p2 * 3 + p1 * 2 + p0 * 2 + q0, 3), |
| 12972 | round_pow2(p2 + p1 * 2 + p0 * 2 + q0 * 2 + q1, 3), |
| 12973 | round_pow2(p1 + p0 * 2 + q0 * 2 + q1 * 2 + q2, 3), |
| 12974 | round_pow2(p0 + q0 * 2 + q1 * 2 + q2 * 3, 3), |
| 12975 | ] |
| 12976 | } else { |
| 12977 | filter4(mask, thresh, p1, p0, q0, q1) |
| 12978 | } |
| 12979 | } |
| 12980 | |
| 12981 | /// `filter8`: luma's flat 7-tap smoothing (spec `[1,1,1,2,1,1,1]`). |
| 12982 | #[allow(clippy::too_many_arguments)] |
| 12983 | fn filter8( |
| 12984 | mask: bool, |
| 12985 | thresh: i32, |
| 12986 | flat: bool, |
| 12987 | p3: i32, |
| 12988 | p2: i32, |
| 12989 | p1: i32, |
| 12990 | p0: i32, |
| 12991 | q0: i32, |
| 12992 | q1: i32, |
| 12993 | q2: i32, |
| 12994 | q3: i32, |
| 12995 | ) -> [i32; 6] { |
| 12996 | if flat && mask { |
| 12997 | [ |
| 12998 | round_pow2(p3 + p3 + p3 + 2 * p2 + p1 + p0 + q0, 3), |
| 12999 | round_pow2(p3 + p3 + p2 + 2 * p1 + p0 + q0 + q1, 3), |
| 13000 | round_pow2(p3 + p2 + p1 + 2 * p0 + q0 + q1 + q2, 3), |
| 13001 | round_pow2(p2 + p1 + p0 + 2 * q0 + q1 + q2 + q3, 3), |
| 13002 | round_pow2(p1 + p0 + q0 + 2 * q1 + q2 + q3 + q3, 3), |
| 13003 | round_pow2(p0 + q0 + q1 + 2 * q2 + q3 + q3 + q3, 3), |
| 13004 | ] |
| 13005 | } else { |
| 13006 | let [op1, op0, oq0, oq1] = filter4(mask, thresh, p1, p0, q0, q1); |
| 13007 | [p2, op1, op0, oq0, oq1, q2] |
| 13008 | } |
| 13009 | } |
| 13010 | |
| 13011 | /// `filter14`: the wide 13-tap smoothing used at 16x16-and-larger transform |
| 13012 | /// edges; falls back to [`filter8`] (leaving `op6`/`oq6` untouched either |
| 13013 | /// way -- they are only ever taps here, never written). |
| 13014 | #[allow(clippy::too_many_arguments)] |
| 13015 | fn filter14( |
| 13016 | mask: bool, |
| 13017 | thresh: i32, |
| 13018 | flat: bool, |
| 13019 | flat2: bool, |
| 13020 | p6: i32, |
| 13021 | p5: i32, |
| 13022 | p4: i32, |
| 13023 | p3: i32, |
| 13024 | p2: i32, |
| 13025 | p1: i32, |
| 13026 | p0: i32, |
| 13027 | q0: i32, |
| 13028 | q1: i32, |
| 13029 | q2: i32, |
| 13030 | q3: i32, |
| 13031 | q4: i32, |
| 13032 | q5: i32, |
| 13033 | q6: i32, |
| 13034 | ) -> [i32; 12] { |
| 13035 | if flat2 && flat && mask { |
| 13036 | [ |
| 13037 | round_pow2(p6 * 7 + p5 * 2 + p4 * 2 + p3 + p2 + p1 + p0 + q0, 4), |
| 13038 | round_pow2( |
| 13039 | p6 * 5 + p5 * 2 + p4 * 2 + p3 * 2 + p2 + p1 + p0 + q0 + q1, |
| 13040 | 4, |
| 13041 | ), |
| 13042 | round_pow2( |
| 13043 | p6 * 4 + p5 + p4 * 2 + p3 * 2 + p2 * 2 + p1 + p0 + q0 + q1 + q2, |
| 13044 | 4, |
| 13045 | ), |
| 13046 | round_pow2( |
| 13047 | p6 * 3 + p5 + p4 + p3 * 2 + p2 * 2 + p1 * 2 + p0 + q0 + q1 + q2 + q3, |
| 13048 | 4, |
| 13049 | ), |
| 13050 | round_pow2( |
| 13051 | p6 * 2 + p5 + p4 + p3 + p2 * 2 + p1 * 2 + p0 * 2 + q0 + q1 + q2 + q3 + q4, |
| 13052 | 4, |
| 13053 | ), |
| 13054 | round_pow2( |
| 13055 | p6 + p5 + p4 + p3 + p2 + p1 * 2 + p0 * 2 + q0 * 2 + q1 + q2 + q3 + q4 + q5, |
| 13056 | 4, |
| 13057 | ), |
| 13058 | round_pow2( |
| 13059 | p5 + p4 + p3 + p2 + p1 + p0 * 2 + q0 * 2 + q1 * 2 + q2 + q3 + q4 + q5 + q6, |
| 13060 | 4, |
| 13061 | ), |
| 13062 | round_pow2( |
| 13063 | p4 + p3 + p2 + p1 + p0 + q0 * 2 + q1 * 2 + q2 * 2 + q3 + q4 + q5 + q6 * 2, |
| 13064 | 4, |
| 13065 | ), |
| 13066 | round_pow2( |
| 13067 | p3 + p2 + p1 + p0 + q0 + q1 * 2 + q2 * 2 + q3 * 2 + q4 + q5 + q6 * 3, |
| 13068 | 4, |
| 13069 | ), |
| 13070 | round_pow2( |
| 13071 | p2 + p1 + p0 + q0 + q1 + q2 * 2 + q3 * 2 + q4 * 2 + q5 + q6 * 4, |
| 13072 | 4, |
| 13073 | ), |
| 13074 | round_pow2( |
| 13075 | p1 + p0 + q0 + q1 + q2 + q3 * 2 + q4 * 2 + q5 * 2 + q6 * 5, |
| 13076 | 4, |
| 13077 | ), |
| 13078 | round_pow2(p0 + q0 + q1 + q2 + q3 + q4 * 2 + q5 * 2 + q6 * 7, 4), |
| 13079 | ] |
| 13080 | } else { |
| 13081 | let [op2, op1, op0, oq0, oq1, oq2] = |
| 13082 | filter8(mask, thresh, flat, p3, p2, p1, p0, q0, q1, q2, q3); |
| 13083 | [p5, p4, p3, op2, op1, op0, oq0, oq1, oq2, q3, q4, q5] |
| 13084 | } |
| 13085 | } |
| 13086 | |
| 13087 | /// Filters one 4-pixel edge group: `base` is the plane-local byte offset of |
| 13088 | /// the first (`q0`) sample past the edge, `outer_stride` advances to the |
| 13089 | /// next of the 4 perpendicular samples, `tap_stride` steps from one tap to |
| 13090 | /// the next along the edge's own filtering direction. |
| 13091 | #[allow(clippy::too_many_arguments)] |
| 13092 | fn filter_edge( |
| 13093 | data: &mut [u16], |
| 13094 | base: usize, |
| 13095 | outer_stride: isize, |
| 13096 | tap_stride: isize, |
| 13097 | len: u8, |
| 13098 | level: i32, |
| 13099 | sharpness: u8, |
| 13100 | ) { |
| 13101 | let (blimit, limit, hev_thr) = deblock_thresholds(level, sharpness); |
| 13102 | // libaom `highbd_filter_mask*`/`highbd_flat_mask4`/`highbd_hev_mask`: |
| 13103 | // every 8-bit threshold (including the flat masks' fixed `1`) is |
| 13104 | // compared against a raw pixel difference on the stream's own bit-depth |
| 13105 | // scale, so each is shifted left by `bit_depth - 8` before use. |
| 13106 | let shift = bit_depth() - 8; |
| 13107 | let (blimit, limit, hev_thr) = (blimit << shift, limit << shift, hev_thr << shift); |
| 13108 | let flat_thresh = 1i32 << shift; |
| 13109 | for i in 0..4isize { |
| 13110 | let center = (base as isize + i * outer_stride) as usize; |
| 13111 | let idx = |k: isize| -> usize { (center as isize + k * tap_stride) as usize }; |
| 13112 | let px = |k: isize| -> i32 { i32::from(data[idx(k)]) }; |
| 13113 | match len { |
| 13114 | 4 => { |
| 13115 | let (p1, p0, q0, q1) = (px(-2), px(-1), px(0), px(1)); |
| 13116 | let mask = (p1 - p0).abs() <= limit |
| 13117 | && (q1 - q0).abs() <= limit |
| 13118 | && (p0 - q0).abs() * 2 + (p1 - q1).abs() / 2 <= blimit; |
| 13119 | let [op1, op0, oq0, oq1] = filter4(mask, hev_thr, p1, p0, q0, q1); |
| 13120 | data[idx(-2)] = op1.clamp(0, sample_max()) as u16; |
| 13121 | data[idx(-1)] = op0.clamp(0, sample_max()) as u16; |
| 13122 | data[idx(0)] = oq0.clamp(0, sample_max()) as u16; |
| 13123 | data[idx(1)] = oq1.clamp(0, sample_max()) as u16; |
| 13124 | } |
| 13125 | 6 => { |
| 13126 | let (p2, p1, p0, q0, q1, q2) = (px(-3), px(-2), px(-1), px(0), px(1), px(2)); |
| 13127 | let mask = (p2 - p1).abs() <= limit |
| 13128 | && (p1 - p0).abs() <= limit |
| 13129 | && (q1 - q0).abs() <= limit |
| 13130 | && (q2 - q1).abs() <= limit |
| 13131 | && (p0 - q0).abs() * 2 + (p1 - q1).abs() / 2 <= blimit; |
| 13132 | let flat = (p1 - p0).abs() <= flat_thresh |
| 13133 | && (q1 - q0).abs() <= flat_thresh |
| 13134 | && (p2 - p0).abs() <= flat_thresh |
| 13135 | && (q2 - q0).abs() <= flat_thresh; |
| 13136 | let [op1, op0, oq0, oq1] = filter6(mask, hev_thr, flat, p2, p1, p0, q0, q1, q2); |
| 13137 | data[idx(-2)] = op1.clamp(0, sample_max()) as u16; |
| 13138 | data[idx(-1)] = op0.clamp(0, sample_max()) as u16; |
| 13139 | data[idx(0)] = oq0.clamp(0, sample_max()) as u16; |
| 13140 | data[idx(1)] = oq1.clamp(0, sample_max()) as u16; |
| 13141 | } |
| 13142 | 8 => { |
| 13143 | let (p3, p2, p1, p0, q0, q1, q2, q3) = |
| 13144 | (px(-4), px(-3), px(-2), px(-1), px(0), px(1), px(2), px(3)); |
| 13145 | let mask = (p3 - p2).abs() <= limit |
| 13146 | && (p2 - p1).abs() <= limit |
| 13147 | && (p1 - p0).abs() <= limit |
| 13148 | && (q1 - q0).abs() <= limit |
| 13149 | && (q2 - q1).abs() <= limit |
| 13150 | && (q3 - q2).abs() <= limit |
| 13151 | && (p0 - q0).abs() * 2 + (p1 - q1).abs() / 2 <= blimit; |
| 13152 | let flat = (p1 - p0).abs() <= flat_thresh |
| 13153 | && (q1 - q0).abs() <= flat_thresh |
| 13154 | && (p2 - p0).abs() <= flat_thresh |
| 13155 | && (q2 - q0).abs() <= flat_thresh |
| 13156 | && (p3 - p0).abs() <= flat_thresh |
| 13157 | && (q3 - q0).abs() <= flat_thresh; |
| 13158 | let [op2, op1, op0, oq0, oq1, oq2] = |
| 13159 | filter8(mask, hev_thr, flat, p3, p2, p1, p0, q0, q1, q2, q3); |
| 13160 | data[idx(-3)] = op2.clamp(0, sample_max()) as u16; |
| 13161 | data[idx(-2)] = op1.clamp(0, sample_max()) as u16; |
| 13162 | data[idx(-1)] = op0.clamp(0, sample_max()) as u16; |
| 13163 | data[idx(0)] = oq0.clamp(0, sample_max()) as u16; |
| 13164 | data[idx(1)] = oq1.clamp(0, sample_max()) as u16; |
| 13165 | data[idx(2)] = oq2.clamp(0, sample_max()) as u16; |
| 13166 | } |
| 13167 | _ => { |
| 13168 | let (p6, p5, p4, p3, p2, p1, p0) = |
| 13169 | (px(-7), px(-6), px(-5), px(-4), px(-3), px(-2), px(-1)); |
| 13170 | let (q0, q1, q2, q3, q4, q5, q6) = |
| 13171 | (px(0), px(1), px(2), px(3), px(4), px(5), px(6)); |
| 13172 | let mask = (p3 - p2).abs() <= limit |
| 13173 | && (p2 - p1).abs() <= limit |
| 13174 | && (p1 - p0).abs() <= limit |
| 13175 | && (q1 - q0).abs() <= limit |
| 13176 | && (q2 - q1).abs() <= limit |
| 13177 | && (q3 - q2).abs() <= limit |
| 13178 | && (p0 - q0).abs() * 2 + (p1 - q1).abs() / 2 <= blimit; |
| 13179 | let flat = (p1 - p0).abs() <= flat_thresh |
| 13180 | && (q1 - q0).abs() <= flat_thresh |
| 13181 | && (p2 - p0).abs() <= flat_thresh |
| 13182 | && (q2 - q0).abs() <= flat_thresh |
| 13183 | && (p3 - p0).abs() <= flat_thresh |
| 13184 | && (q3 - q0).abs() <= flat_thresh; |
| 13185 | let flat2 = (p4 - p0).abs() <= flat_thresh |
| 13186 | && (q4 - q0).abs() <= flat_thresh |
| 13187 | && (p5 - p0).abs() <= flat_thresh |
| 13188 | && (q5 - q0).abs() <= flat_thresh |
| 13189 | && (p6 - p0).abs() <= flat_thresh |
| 13190 | && (q6 - q0).abs() <= flat_thresh; |
| 13191 | let out = filter14( |
| 13192 | mask, hev_thr, flat, flat2, p6, p5, p4, p3, p2, p1, p0, q0, q1, q2, q3, q4, q5, |
| 13193 | q6, |
| 13194 | ); |
| 13195 | for (k, v) in (-6..=5isize).zip(out) { |
| 13196 | data[idx(k)] = v.clamp(0, sample_max()) as u16; |
| 13197 | } |
| 13198 | } |
| 13199 | } |
| 13200 | } |
| 13201 | } |
| 13202 | |
| 13203 | /// One plane's worth of spec 7.14: every vertical edge across the plane, |
| 13204 | /// then every horizontal edge (the order the spec and libaom both use). |
| 13205 | fn deblock_plane( |
| 13206 | plane: &mut PlaneBuf, |
| 13207 | plane_idx: usize, |
| 13208 | lf: &LoopFilterParams, |
| 13209 | n: &Neighbours, |
| 13210 | frame_width: usize, |
| 13211 | frame_height: usize, |
| 13212 | ) { |
| 13213 | let chroma = plane_idx != 0; |
| 13214 | // r7: libaom clips the deblock loop to the CODED frame's own |
| 13215 | // width/height, not the mi-aligned `true_width`/`true_height` margin |
| 13216 | // (the padding up to the next 8-pixel/superblock boundary that |
| 13217 | // reconstruction still fills in but the loop filter never visits). |
| 13218 | // Confirmed against the superres gate: with the margin included, the |
| 13219 | // last coded mi column's horizontal edges were filtered when libaom's |
| 13220 | // real output leaves them untouched (r6's post-deblock diff, 37 px, all |
| 13221 | // in columns 44-47); clipping here closes it (3/3 frames pixel-exact). |
| 13222 | // lane-golomb r9: the bound is the MI-ROUNDED frame, not the coded |
| 13223 | // frame. libaom's `av1_filter_block_plane_vert/horz` walk the superblock |
| 13224 | // and stop at `cm->mi_params.mi_cols`/`mi_rows`, which |
| 13225 | // `av1_set_mb_mi` derives from `ALIGN_POWER_OF_TWO(width, 3)` -- the |
| 13226 | // 8-pixel-aligned frame, i.e. a 68-wide frame is filtered out to column |
| 13227 | // 72 and the vertical edge AT x=68 is a real edge that rewrites the |
| 13228 | // in-crop columns 64..67. Clipping at `frame_width` skipped it, which is |
| 13229 | // the +-1 straddling-band defect (68x192 cq35, frame 1, 141 luma pixels |
| 13230 | // all in columns 64..67; frame 0 survived because that edge carried no |
| 13231 | // filter level there). r7's superres measurement is preserved: its frame |
| 13232 | // was already 8-aligned, so this bound equals `frame_width` there and |
| 13233 | // still excludes the superblock padding up to `true_width`. |
| 13234 | let (cw, ch) = if chroma { |
| 13235 | (frame_width.div_ceil(2), frame_height.div_ceil(2)) |
| 13236 | } else { |
| 13237 | (frame_width, frame_height) |
| 13238 | }; |
| 13239 | let (tw, th, stride) = ( |
| 13240 | plane.true_width.min(cw), |
| 13241 | plane.true_height.min(ch), |
| 13242 | plane.width, |
| 13243 | ); |
| 13244 | if std::env::var_os("EC_AV1_DEBLOCK_TRACE_V").is_some() { |
| 13245 | eprintln!("DEBLOCK plane={plane_idx} tw={tw} th={th} stride={stride} true={}x{} crop={cw}x{ch}", plane.true_width, plane.true_height); |
| 13246 | } |
| 13247 | let mut y0 = 0usize; |
| 13248 | while y0 < th { |
| 13249 | let mut x0 = 4usize; |
| 13250 | while x0 < tw { |
| 13251 | if let Some((len, level)) = edge_params(lf, n, plane_idx, 0, chroma, x0, y0) { |
| 13252 | if std::env::var_os("EC_AV1_DEBLOCK_TRACE_V").is_some() && plane_idx == 0 { |
| 13253 | eprintln!("VEDGE x0={x0} y0={y0} len={len} level={level}"); |
| 13254 | } |
| 13255 | filter_edge( |
| 13256 | &mut plane.data, |
| 13257 | y0 * stride + x0, |
| 13258 | stride as isize, |
| 13259 | 1, |
| 13260 | len, |
| 13261 | level, |
| 13262 | lf.sharpness, |
| 13263 | ); |
| 13264 | } |
| 13265 | x0 += 4; |
| 13266 | } |
| 13267 | y0 += 4; |
| 13268 | } |
| 13269 | let mut x0 = 0usize; |
| 13270 | while x0 < tw { |
| 13271 | let mut y0 = 4usize; |
| 13272 | while y0 < th { |
| 13273 | if let Some((len, level)) = edge_params(lf, n, plane_idx, 1, chroma, x0, y0) { |
| 13274 | if std::env::var_os("EC_AV1_DEBLOCK_TRACE").is_some() |
| 13275 | && plane_idx == 0 |
| 13276 | && x0 == 44 |
| 13277 | { |
| 13278 | eprintln!( |
| 13279 | "HEDGE x0={x0} y0={y0} len={len} level={level} sharpness={}", |
| 13280 | lf.sharpness |
| 13281 | ); |
| 13282 | } |
| 13283 | filter_edge( |
| 13284 | &mut plane.data, |
| 13285 | y0 * stride + x0, |
| 13286 | 1, |
| 13287 | stride as isize, |
| 13288 | len, |
| 13289 | level, |
| 13290 | lf.sharpness, |
| 13291 | ); |
| 13292 | } |
| 13293 | y0 += 4; |
| 13294 | } |
| 13295 | x0 += 4; |
| 13296 | } |
| 13297 | } |
| 13298 | |
| 13299 | fn apply_cdef( |
| 13300 | y: &mut PlaneBuf, |
| 13301 | u: &mut PlaneBuf, |
| 13302 | v: &mut PlaneBuf, |
| 13303 | cdef: &CdefParams, |
| 13304 | skip_grid: &Neighbours, |
| 13305 | // This frame header's own CROPPED luma size, for the straddling-unit |
| 13306 | // counter only -- CDEF itself walks the mi-rounded plane. |
| 13307 | (frame_w, frame_h): (usize, usize), |
| 13308 | ) { |
| 13309 | // lane-part32 r2 debug rung, env-gated: see `apply_deblock`'s sibling. |
| 13310 | if std::env::var_os("EC_AV1_DEBUG_SKIP_CDEF").is_some() { |
| 13311 | return; |
| 13312 | } |
| 13313 | // `cdef.bits == 0` still means "no filtering at all" whenever the single |
| 13314 | // (index 0) strength pair is all-zero -- the fast path every existing |
| 13315 | // gate stream takes. A `bits > 0` frame can still be all-zero-strength |
| 13316 | // at every index, but that is rare enough not to special-case; the |
| 13317 | // per-superblock lookup below degrades to the same index-0 read anyway |
| 13318 | // when [`CDEF_IDX_GRID`] was never populated (`bits == 0`). |
| 13319 | if cdef.bits == 0 |
| 13320 | && cdef.y_pri_strength[0] == 0 |
| 13321 | && cdef.y_sec_strength[0] == 0 |
| 13322 | && cdef.uv_pri_strength[0] == 0 |
| 13323 | && cdef.uv_sec_strength[0] == 0 |
| 13324 | { |
| 13325 | return; |
| 13326 | } |
| 13327 | let sb_cols = CDEF_SB_COLS.with(|c| c.get()); |
| 13328 | let idx_grid = CDEF_IDX_GRID.with(|g| g.borrow().clone()); |
| 13329 | let strength_idx = |mi_r: usize, mi_c: usize| -> usize { |
| 13330 | if sb_cols == 0 { |
| 13331 | return 0; |
| 13332 | } |
| 13333 | let (sb_r, sb_c) = (mi_r / SB_MI as usize, mi_c / SB_MI as usize); |
| 13334 | idx_grid |
| 13335 | .get(sb_r * sb_cols + sb_c) |
| 13336 | .copied() |
| 13337 | .unwrap_or(0) as usize |
| 13338 | }; |
| 13339 | let src_y = y.data.clone(); |
| 13340 | let src_u = u.data.clone(); |
| 13341 | let src_v = v.data.clone(); |
| 13342 | // libaom `av1_cdef_filter_fb`: `pri_strength = level << coeff_shift`, |
| 13343 | // `sec_strength <<= coeff_shift`, `damping += coeff_shift - (pli != 0)`. |
| 13344 | let coeff_shift = i32::from(bit_depth()) - 8; |
| 13345 | let damping = i32::from(cdef.damping) + coeff_shift; |
| 13346 | let damping_uv = (damping - 1).max(0); |
| 13347 | let mut mi_r = 0usize; |
| 13348 | while mi_r < skip_grid.mi_rows { |
| 13349 | let mut mi_c = 0usize; |
| 13350 | while mi_c < skip_grid.mi_cols { |
| 13351 | if skip_grid.is_skip_txfm(mi_r, mi_c) { |
| 13352 | CDEF_SKIPPED_UNITS.with(|c| c.set(c.get() + 1)); |
| 13353 | } |
| 13354 | if !skip_grid.is_skip_txfm(mi_r, mi_c) { |
| 13355 | let sidx = strength_idx(mi_r, mi_c); |
| 13356 | let (ox, oy) = (mi_c * 4, mi_r * 4); |
| 13357 | let (y_stride, y_true_w, y_true_h) = (y.width, y.true_width, y.true_height); |
| 13358 | let sample_y = |r: i32, c: i32| -> i32 { |
| 13359 | let (ny, nx) = (oy as i32 + r, ox as i32 + c); |
| 13360 | if ny < 0 || nx < 0 || ny as usize >= y_true_h || nx as usize >= y_true_w { |
| 13361 | CDEF_VERY_LARGE |
| 13362 | } else { |
| 13363 | i32::from(src_y[ny as usize * y_stride + nx as usize]) |
| 13364 | } |
| 13365 | }; |
| 13366 | // lane-cdef r1: the direction search reads the SAME padded |
| 13367 | // block the taps do -- libaom runs `cdef_find_dir` on the |
| 13368 | // prepared filter block (`cdef_block.c:296-320` over |
| 13369 | // `cdef_prepare_fb`'s buffer, `cdef.c:249-256`), whose columns |
| 13370 | // and rows past the mi-rounded frame extent are CDEF_VERY_LARGE. |
| 13371 | // Clamping to the last real sample instead made every straddling |
| 13372 | // edge 8x8 (odd `mi_cols`/`mi_rows`) pick a different var, hence |
| 13373 | // a different `adjust_strength`, hence a +-1 band at the crop edge. |
| 13374 | let (dir, var) = cdef_find_dir(coeff_shift, |r, c| sample_y(r as i32, c as i32)); |
| 13375 | |
| 13376 | // libaom `av1_cdef_filter_fb` passes `pri_strength ? dir : 0` |
| 13377 | // -- each plane zeroes the shared direction when *its own* |
| 13378 | // frame-level primary strength is 0, independent of the |
| 13379 | // per-block adjusted `t` and of the other plane's strength. |
| 13380 | let y_pri_strength = i32::from(cdef.y_pri_strength[sidx]) << coeff_shift; |
| 13381 | let y_sec_strength = i32::from(cdef.y_sec_strength[sidx]) << coeff_shift; |
| 13382 | let y_dir = if y_pri_strength != 0 { dir } else { 0 }; |
| 13383 | let t = cdef_adjust_strength(y_pri_strength, var); |
| 13384 | // lane-cdef r1 rung: one line per filtered 8x8 luma CDEF unit. |
| 13385 | if std::env::var_os("EC_AV1_CDEF_DBG").is_some() { |
| 13386 | eprintln!( |
| 13387 | "EC_CDEF mi_r={mi_r} mi_c={mi_c} x={ox} y={oy} sidx={sidx} dir={dir} var={var} t={t} pri={y_pri_strength} sec={y_sec_strength} damp={damping}" |
| 13388 | ); |
| 13389 | } |
| 13390 | let enable_primary = t != 0; |
| 13391 | let enable_secondary = y_sec_strength != 0; |
| 13392 | if (ox + 8 > frame_w || oy + 8 > frame_h) |
| 13393 | && (enable_primary |
| 13394 | || enable_secondary |
| 13395 | || cdef.uv_pri_strength[sidx] != 0 |
| 13396 | || cdef.uv_sec_strength[sidx] != 0) |
| 13397 | { |
| 13398 | CDEF_STRADDLE_UNITS.with(|c| c.set(c.get() + 1)); |
| 13399 | } |
| 13400 | if enable_primary || enable_secondary { |
| 13401 | cdef_filter_block( |
| 13402 | sample_y, |
| 13403 | y, |
| 13404 | ox, |
| 13405 | oy, |
| 13406 | 8, |
| 13407 | 8, |
| 13408 | t, |
| 13409 | y_sec_strength, |
| 13410 | y_dir, |
| 13411 | damping, |
| 13412 | damping, |
| 13413 | enable_primary, |
| 13414 | enable_secondary, |
| 13415 | coeff_shift, |
| 13416 | ); |
| 13417 | } |
| 13418 | |
| 13419 | let t_uv = i32::from(cdef.uv_pri_strength[sidx]) << coeff_shift; |
| 13420 | let uv_sec_strength = i32::from(cdef.uv_sec_strength[sidx]) << coeff_shift; |
| 13421 | let uv_dir = if t_uv != 0 { dir } else { 0 }; |
| 13422 | let enable_primary_uv = t_uv != 0; |
| 13423 | let enable_secondary_uv = uv_sec_strength != 0; |
| 13424 | if enable_primary_uv || enable_secondary_uv { |
| 13425 | let (cox, coy) = (ox / 2, oy / 2); |
| 13426 | for (plane, src_p) in [(&mut *u, &src_u), (&mut *v, &src_v)] { |
| 13427 | let (tw, th) = (plane.true_width, plane.true_height); |
| 13428 | let stride = plane.width; |
| 13429 | let sample_uv = |r: i32, c: i32| -> i32 { |
| 13430 | let (ny, nx) = (coy as i32 + r, cox as i32 + c); |
| 13431 | if ny < 0 || nx < 0 || ny as usize >= th || nx as usize >= tw { |
| 13432 | CDEF_VERY_LARGE |
| 13433 | } else { |
| 13434 | i32::from(src_p[ny as usize * stride + nx as usize]) |
| 13435 | } |
| 13436 | }; |
| 13437 | cdef_filter_block( |
| 13438 | sample_uv, |
| 13439 | plane, |
| 13440 | cox, |
| 13441 | coy, |
| 13442 | 4, |
| 13443 | 4, |
| 13444 | t_uv, |
| 13445 | uv_sec_strength, |
| 13446 | uv_dir, |
| 13447 | damping_uv, |
| 13448 | damping_uv, |
| 13449 | enable_primary_uv, |
| 13450 | enable_secondary_uv, |
| 13451 | coeff_shift, |
| 13452 | ); |
| 13453 | } |
| 13454 | } |
| 13455 | } |
| 13456 | mi_c += 2; |
| 13457 | } |
| 13458 | mi_r += 2; |
| 13459 | } |
| 13460 | } |
| 13461 | |
| 13462 | /// Spec 7.17: loop restoration, run after [`apply_cdef`] (a sibling lane's |
| 13463 | /// ordering note: libaom also runs `superres_post_decode` between the two, |
| 13464 | /// but this decoder never implements super-resolution, so there is nothing |
| 13465 | /// to interleave). `deblocked_*` is each plane's post-deblock, pre-CDEF |
| 13466 | /// buffer (the caller clones it before calling [`apply_cdef`]) -- loop |
| 13467 | /// restoration's stripe-boundary rows read from it instead of the |
| 13468 | /// CDEF-filtered plane, per `restoration.rs`'s own `lr_sample` doc comment. |
| 13469 | #[allow(clippy::too_many_arguments)] |
| 13470 | fn apply_loop_restoration( |
| 13471 | y: &mut PlaneBuf, |
| 13472 | u: &mut PlaneBuf, |
| 13473 | v: &mut PlaneBuf, |
| 13474 | deblocked_y: &PlaneBuf, |
| 13475 | deblocked_u: &PlaneBuf, |
| 13476 | deblocked_v: &PlaneBuf, |
| 13477 | lr: &LoopRestorationParams, |
| 13478 | grid: &crate::restoration::RestorationGrid, |
| 13479 | // lane-golomb r8: this frame header's own (cropped) luma size. Loop |
| 13480 | // restoration is the ONE post-decode filter that does NOT run over the |
| 13481 | // mi-rounded plane: libaom's `av1_loop_restoration_filter_frame` sizes |
| 13482 | // every plane with `av1_get_upsampled_plane_size` -- `plane_w = |
| 13483 | // ROUND_POWER_OF_TWO(cm->superres_upscaled_width, ss_x)`, `plane_h = |
| 13484 | // ROUND_POWER_OF_TWO(cm->height, ss_y)` (`av1/common/restoration.c:47`) |
| 13485 | // -- and asserts it equals the buffer's CROP width |
| 13486 | // (`restoration.c:1106`), then `av1_extend_frame(..., plane_w, plane_h, |
| 13487 | // ...)` (`restoration.c:1109`) replicates the right/bottom border from |
| 13488 | // that cropped edge before any unit is filtered. Deblock and CDEF DO |
| 13489 | // walk the mi grid, which is why passing `true_width`/`true_height` |
| 13490 | // here went unnoticed until a frame whose size is not a multiple of 8 |
| 13491 | // (68 -> mi-rounded 72): the LR taps at the last four real columns then |
| 13492 | // read this decoder's own decoded samples at columns 68..71 where |
| 13493 | // libaom reads column 67 replicated -- +-1 over the straddling band |
| 13494 | // (class av1-truesize-lane: true frame size vs mi-rounded). |
| 13495 | (frame_w, frame_h): (usize, usize), |
| 13496 | ) { |
| 13497 | y.data = crate::restoration::apply_loop_restoration_plane( |
| 13498 | &y.data, |
| 13499 | &deblocked_y.data, |
| 13500 | y.width, |
| 13501 | frame_w, |
| 13502 | frame_h, |
| 13503 | 0, |
| 13504 | lr.frame_restoration_type[0], |
| 13505 | lr.loop_restoration_size[0], |
| 13506 | grid, |
| 13507 | 0, |
| 13508 | ); |
| 13509 | u.data = crate::restoration::apply_loop_restoration_plane( |
| 13510 | &u.data, |
| 13511 | &deblocked_u.data, |
| 13512 | u.width, |
| 13513 | frame_w.div_ceil(2), |
| 13514 | frame_h.div_ceil(2), |
| 13515 | 1, |
| 13516 | lr.frame_restoration_type[1], |
| 13517 | lr.loop_restoration_size[1], |
| 13518 | grid, |
| 13519 | 1, |
| 13520 | ); |
| 13521 | v.data = crate::restoration::apply_loop_restoration_plane( |
| 13522 | &v.data, |
| 13523 | &deblocked_v.data, |
| 13524 | v.width, |
| 13525 | frame_w.div_ceil(2), |
| 13526 | frame_h.div_ceil(2), |
| 13527 | 1, |
| 13528 | lr.frame_restoration_type[2], |
| 13529 | lr.loop_restoration_size[2], |
| 13530 | grid, |
| 13531 | 2, |
| 13532 | ); |
| 13533 | } |
| 13534 | |
| 13535 | /// Decodes the payload [`crate::tile::sb_coeff_key_frame_tile`] writes, |
| 13536 | /// returning the picture it reconstructs to. |
| 13537 | /// |
| 13538 | /// `mi_cols`/`mi_rows` and `base_q_idx` are the frame header's, as parsed by |
| 13539 | /// [`ec_av1_syntax`]. `frame_width`/`frame_height` are the header's own true |
| 13540 | /// (render) size — a separate field from `mi_cols`/`mi_rows` (spec |
| 13541 | /// `compute_image_size` derives one from the other, but not losslessly: a |
| 13542 | /// width that is not a multiple of 8 samples leaves `mi_cols * 4` past it) — |
| 13543 | /// what the reconstruction is cropped to on the way out, mirroring |
| 13544 | /// [`crate::encode::crop_encoded`]. |
| 13545 | /// |
| 13546 | /// # Errors |
| 13547 | /// Returns an error when a block's partition, mode, skip flag or transform |
| 13548 | /// type is anything this decoder does not reconstruct (round 2: inter, tx |
| 13549 | /// types other than `DCT_DCT`, non-CDF-gathered rectangular splits below |
| 13550 | /// 16x16, a directional mode's angle delta other than zero, chroma-from-luma), |
| 13551 | /// or when the tile payload runs out of the symbols this decode expects (a |
| 13552 | /// genuinely foreign stream). |
| 13553 | pub fn decode_key_frame_tile( |
| 13554 | data: &[u8], |
| 13555 | mi_cols: u32, |
| 13556 | mi_rows: u32, |
| 13557 | base_q_idx: u8, |
| 13558 | frame_width: u32, |
| 13559 | frame_height: u32, |
| 13560 | enable_filter_intra: bool, |
| 13561 | cdef: &CdefParams, |
| 13562 | loop_filter: &LoopFilterParams, |
| 13563 | tx_select: bool, |
| 13564 | reduced_tx_set: bool, |
| 13565 | allow_screen_content_tools: bool, |
| 13566 | allow_intrabc: bool, |
| 13567 | ) -> Result<Picture> { |
| 13568 | let delta = DeltaParams::default(); |
| 13569 | let single_tile = TileInfo { |
| 13570 | mi_col_starts: vec![0, mi_cols], |
| 13571 | mi_row_starts: vec![0, mi_rows], |
| 13572 | ..TileInfo::default() |
| 13573 | }; |
| 13574 | decode_key_frame_tile_with_cdfs( |
| 13575 | &[data], |
| 13576 | &single_tile, |
| 13577 | mi_cols, |
| 13578 | mi_rows, |
| 13579 | base_q_idx, |
| 13580 | crate::quant::QuantDeltas::default(), |
| 13581 | frame_width, |
| 13582 | frame_height, |
| 13583 | enable_filter_intra, |
| 13584 | false, |
| 13585 | cdef, |
| 13586 | loop_filter, |
| 13587 | &LoopRestorationParams::default(), |
| 13588 | None, |
| 13589 | tx_select, |
| 13590 | reduced_tx_set, |
| 13591 | allow_screen_content_tools, |
| 13592 | allow_intrabc, |
| 13593 | delta, |
| 13594 | ) |
| 13595 | .map(|(picture, _)| picture) |
| 13596 | } |
| 13597 | |
| 13598 | thread_local! { |
| 13599 | /// lane-superres r3: the real decoded margin beyond `frame_width` up to |
| 13600 | /// the mi-aligned `true_width`, set by the most recent |
| 13601 | /// [`decode_key_frame_tile_with_cdfs`] OR (lane-superres r10) |
| 13602 | /// [`decode_inter_frame_tile_with_cdfs`] call -- both frame kinds crop |
| 13603 | /// their reconstructed buffer down from the same mi-aligned extent, so |
| 13604 | /// they share this one slot. See each function's own comment at its |
| 13605 | /// write site. `None` when there was no margin to save. |
| 13606 | static LAST_FRAME_WIDE_MARGIN: std::cell::RefCell<Option<Picture>> = |
| 13607 | const { std::cell::RefCell::new(None) }; |
| 13608 | } |
| 13609 | |
| 13610 | /// The margin the most recent key- or inter-frame tile decode stashed |
| 13611 | /// (see [`LAST_FRAME_WIDE_MARGIN`]) -- `stream.rs`'s superres path |
| 13612 | /// reads this immediately after the call, before any other frame |
| 13613 | /// decode can overwrite it. |
| 13614 | pub(crate) fn take_last_frame_wide_margin() -> Option<Picture> { |
| 13615 | LAST_FRAME_WIDE_MARGIN.with(|m| m.borrow_mut().take()) |
| 13616 | } |
| 13617 | |
| 13618 | /// [`decode_key_frame_tile`], threading a cross-frame CDF forward (spec |
| 13619 | /// 7.20's `load_cdfs`): `initial_cdfs` is the caller's forwarded state (from |
| 13620 | /// a prior frame's saved reference slot) when set, or the spec 8.4 defaults |
| 13621 | /// when `None` (the only case [`decode_key_frame_tile`] itself ever needs, |
| 13622 | /// since a key frame's own header always forces `primary_ref_frame == |
| 13623 | /// PRIMARY_REF_NONE`). Returns the tile's own end-of-tile adapted table |
| 13624 | /// alongside the picture, for the caller to save into whichever reference |
| 13625 | /// slots this frame's `refresh_frame_flags` names. |
| 13626 | /// The order the 64x64 superblock quadrants of a tile are decoded in. |
| 13627 | /// |
| 13628 | /// At `sb_size == 64` this is plain raster order over the tile's own |
| 13629 | /// superblock grid. At `sb_size == 128` libaom's `decode_partition` recurses |
| 13630 | /// each 128x128 superblock in raster order and visits its four 64x64 |
| 13631 | /// quadrants (TL, TR, BL, BR) before moving to the next superblock, so the |
| 13632 | /// 64-level bodies must run in that nested order. `row_start` marks the first |
| 13633 | /// item of each *superblock* row (where the left neighbour context resets), |
| 13634 | /// `sb_start` the first item of each superblock (where the 128 root's own |
| 13635 | /// loop-restoration units and partition symbol are read). |
| 13636 | fn sb_visit_order(r0: u32, r1: u32, c0: u32, c1: u32, sb128: bool) -> Vec<(u32, u32, bool, bool)> { |
| 13637 | let mut out = Vec::new(); |
| 13638 | if !sb128 { |
| 13639 | for r in r0..r1 { |
| 13640 | for c in c0..c1 { |
| 13641 | out.push((r, c, c == c0, true)); |
| 13642 | } |
| 13643 | } |
| 13644 | return out; |
| 13645 | } |
| 13646 | let mut r = r0; |
| 13647 | while r < r1 { |
| 13648 | let mut first_in_row = true; |
| 13649 | let mut c = c0; |
| 13650 | while c < c1 { |
| 13651 | let mut first_in_sb = true; |
| 13652 | for qr in 0..2 { |
| 13653 | for qc in 0..2 { |
| 13654 | let (rr, cc) = (r + qr, c + qc); |
| 13655 | if rr >= r1 || cc >= c1 { |
| 13656 | continue; |
| 13657 | } |
| 13658 | out.push((rr, cc, first_in_row, first_in_sb)); |
| 13659 | first_in_row = false; |
| 13660 | first_in_sb = false; |
| 13661 | } |
| 13662 | } |
| 13663 | c += 2; |
| 13664 | } |
| 13665 | r += 2; |
| 13666 | } |
| 13667 | out |
| 13668 | } |
| 13669 | |
| 13670 | /// lane-sb128 r2: the 128x128 superblock root, shared by the key-frame and |
| 13671 | /// the inter tile loop. Its loop-restoration units are read once for the whole |
| 13672 | /// 128x128 (spec 5.11.57 is called with the superblock's own size), then one |
| 13673 | /// partition symbol off the `BLOCK_128X128` CDF (8 symbols, no HORZ_4/VERT_4) |
| 13674 | /// decides the root -- only SPLIT recurses into the four 64x64 quadrants this |
| 13675 | /// decoder codes, the other seven arms refuse by name. |
| 13676 | #[allow(clippy::too_many_arguments)] |
| 13677 | thread_local! { |
| 13678 | /// How many 128x128 superblock roots resolved to `PARTITION_NONE` -- one |
| 13679 | /// whole 128x128 block (lane-sb128b r1's gate counter). |
| 13680 | pub(crate) static PART128_NONE_HITS: std::cell::Cell<usize> = const { std::cell::Cell::new(0) }; |
| 13681 | } |
| 13682 | |
| 13683 | /// [`PART128_NONE_HITS`], for a gate's own before/after delta. |
| 13684 | pub(crate) fn part128_none_hits() -> usize { |
| 13685 | PART128_NONE_HITS.with(std::cell::Cell::get) |
| 13686 | } |
| 13687 | |
| 13688 | thread_local! { |
| 13689 | /// How many CHROMA transform units were coded inside a block whose uv |
| 13690 | /// plane block is more than one unit (lane-sb128b r2 -- only a 128x128 |
| 13691 | /// block, four per plane). |
| 13692 | pub(crate) static CHROMA_SPLIT_TX_HITS: std::cell::Cell<usize> = const { std::cell::Cell::new(0) }; |
| 13693 | } |
| 13694 | |
| 13695 | /// [`CHROMA_SPLIT_TX_HITS`], for a gate's own before/after delta. |
| 13696 | pub(crate) fn chroma_split_tx_hits() -> usize { |
| 13697 | CHROMA_SPLIT_TX_HITS.with(std::cell::Cell::get) |
| 13698 | } |
| 13699 | |
| 13700 | fn read_sb128_root( |
| 13701 | dec: &mut SymbolDecoder, |
| 13702 | cdfs: &mut Cdfs, |
| 13703 | neighbours: &Neighbours, |
| 13704 | lr: &LoopRestorationParams, |
| 13705 | lr_grid: &mut crate::restoration::RestorationGrid, |
| 13706 | lr_reference: &mut [( |
| 13707 | crate::restoration::WienerInfo, |
| 13708 | crate::restoration::SgrprojInfo, |
| 13709 | ); 3], |
| 13710 | sb_r: u32, |
| 13711 | sb_c: u32, |
| 13712 | mi_cols: u32, |
| 13713 | mi_rows: u32, |
| 13714 | ) -> Result<usize> { |
| 13715 | let (mi_r128, mi_c128) = ((sb_r & !1) * SB_MI, (sb_c & !1) * SB_MI); |
| 13716 | crate::restoration::read_lr( |
| 13717 | dec, |
| 13718 | cdfs, |
| 13719 | lr, |
| 13720 | lr_grid, |
| 13721 | lr_reference, |
| 13722 | mi_r128, |
| 13723 | mi_c128, |
| 13724 | SB_MI * 2, |
| 13725 | ); |
| 13726 | let at128 = ((mi_r128 / SUB_MI) as usize, (mi_c128 / SUB_MI) as usize); |
| 13727 | let ctx128 = neighbours.partition_ctx(at128, 128); |
| 13728 | let (has_cols128, has_rows128) = (mi_c128 + SB_MI < mi_cols, mi_r128 + SB_MI < mi_rows); |
| 13729 | let part128 = if has_cols128 && has_rows128 { |
| 13730 | PART128_SYMBOLS.with(|c| c.set(c.get() + 1)); |
| 13731 | let p = dec.symbol(&mut cdfs.partition_w128[ctx128]); |
| 13732 | if std::env::var_os("EC_AV1_TRACE").is_some() { |
| 13733 | eprintln!("TRACE partition_w128 ctx={ctx128} value={p}"); |
| 13734 | } |
| 13735 | p |
| 13736 | } else { |
| 13737 | // lane-sb128 r4: the gathered half-out-of-frame reads DECIDE the |
| 13738 | // partition, they are not a forced split -- exactly the discarded-bit |
| 13739 | // defect the 64 root carries a comment about at `partition_w64`'s |
| 13740 | // gathered arm. Value 1 is SPLIT; 0 is HORZ (rows out of frame) or |
| 13741 | // VERT (cols out of frame), which the refusal below then names |
| 13742 | // instead of silently reconstructing a split. |
| 13743 | PART128_GATHERED_SYMBOLS.with(|c| c.set(c.get() + 1)); |
| 13744 | match (has_cols128, has_rows128) { |
| 13745 | (true, false) => { |
| 13746 | if dec.symbol_fixed(&gather_of(&cdfs.partition_w128[ctx128], &VERT_ALIKE128)) == 1 { |
| 13747 | PARTITION_SPLIT |
| 13748 | } else { |
| 13749 | PARTITION_HORZ |
| 13750 | } |
| 13751 | } |
| 13752 | (false, true) => { |
| 13753 | if dec.symbol_fixed(&gather_of(&cdfs.partition_w128[ctx128], &HORZ_ALIKE128)) == 1 { |
| 13754 | PARTITION_SPLIT |
| 13755 | } else { |
| 13756 | PARTITION_VERT |
| 13757 | } |
| 13758 | } |
| 13759 | _ => { |
| 13760 | PART128_GATHERED_SYMBOLS.with(|c| c.set(c.get() - 1)); |
| 13761 | PARTITION_SPLIT |
| 13762 | } |
| 13763 | } |
| 13764 | }; |
| 13765 | // lane-sb128b r1: `PARTITION_NONE` -- one 128x128 block -- is decoded by |
| 13766 | // the key-frame caller below. The six remaining arms (HORZ/VERT and the |
| 13767 | // four AB shapes) still need the rect/AB machinery at this size. |
| 13768 | if part128 != PARTITION_SPLIT && part128 != PARTITION_NONE { |
| 13769 | return Err(unsupported( |
| 13770 | "a 128x128 superblock HORZ/VERT or AB partition (only SPLIT and NONE are decoded at the 128 root)", |
| 13771 | )); |
| 13772 | } |
| 13773 | if part128 == PARTITION_SPLIT { |
| 13774 | PART128_SPLIT_HITS.with(|c| c.set(c.get() + 1)); |
| 13775 | } else { |
| 13776 | PART128_NONE_HITS.with(|c| c.set(c.get() + 1)); |
| 13777 | } |
| 13778 | Ok(part128) |
| 13779 | } |
| 13780 | |
| 13781 | pub(crate) fn decode_key_frame_tile_with_cdfs( |
| 13782 | tiles: &[&[u8]], |
| 13783 | tile_info: &TileInfo, |
| 13784 | mi_cols: u32, |
| 13785 | mi_rows: u32, |
| 13786 | base_q_idx: u8, |
| 13787 | // lane-sbpart r11: this frame header's own per-plane DC/AC quantizer |
| 13788 | // deltas (spec 5.9.12), threaded to [`QUANT_DELTAS`] at the top of every |
| 13789 | // tile alongside [`CURRENT_Q_IDX`]. |
| 13790 | deltas: crate::quant::QuantDeltas, |
| 13791 | frame_width: u32, |
| 13792 | frame_height: u32, |
| 13793 | enable_filter_intra: bool, |
| 13794 | enable_edge_filter: bool, |
| 13795 | cdef: &CdefParams, |
| 13796 | loop_filter: &LoopFilterParams, |
| 13797 | lr: &LoopRestorationParams, |
| 13798 | initial_cdfs: Option<Cdfs>, |
| 13799 | tx_select: bool, |
| 13800 | reduced_tx_set: bool, |
| 13801 | allow_screen_content_tools: bool, |
| 13802 | allow_intrabc: bool, |
| 13803 | // lane-realworld r4: this frame header's own `delta` (spec 5.9.17/5.9.18) |
| 13804 | // -- only `q_present`/`q_res` are actually read ([`maybe_read_delta_q`]); |
| 13805 | // `stream.rs` still refuses a frame with `lf_present` set before this |
| 13806 | // function is ever called. |
| 13807 | delta: DeltaParams, |
| 13808 | ) -> Result<(Picture, Cdfs)> { |
| 13809 | if mi_cols == 0 || mi_rows == 0 { |
| 13810 | return Err(unsupported("a frame with no mode-info grid")); |
| 13811 | } |
| 13812 | ENABLE_EDGE_FILTER.with(|f| f.set(enable_edge_filter)); |
| 13813 | let sb128 = sb128(); |
| 13814 | if sb128 && allow_intrabc { |
| 13815 | return Err(unsupported( |
| 13816 | "intrabc under a 128x128 superblock (libaom's av1_is_dv_valid derives the block-vector delay from sb_size, which this decoder hardcodes to 64)", |
| 13817 | )); |
| 13818 | } |
| 13819 | let (sb_cols, sb_rows) = (mi_cols.div_ceil(SB_MI), mi_rows.div_ceil(SB_MI)); |
| 13820 | INTRABC_MI_GRID.with(|g| { |
| 13821 | *g.borrow_mut() = allow_intrabc.then(|| { |
| 13822 | ( |
| 13823 | crate::mvstack::MiGrid::new(mi_cols as usize, mi_rows as usize), |
| 13824 | mi_cols as usize, |
| 13825 | mi_rows as usize, |
| 13826 | ) |
| 13827 | }); |
| 13828 | }); |
| 13829 | INTRABC_DV.with(|c| c.set(None)); |
| 13830 | CDEF_BITS.with(|c| c.set(cdef.bits)); |
| 13831 | CDEF_SB_COLS.with(|c| c.set(sb_cols as usize)); |
| 13832 | CDEF_IDX_GRID.with(|g| *g.borrow_mut() = vec![0u8; sb_cols as usize * sb_rows as usize]); |
| 13833 | DELTA_Q_PRESENT.with(|c| c.set(delta.q_present)); |
| 13834 | DELTA_Q_RES.with(|c| c.set(1i32 << delta.q_res)); |
| 13835 | DELTA_LF_PRESENT.with(|c| c.set(delta.lf_present)); |
| 13836 | DELTA_LF_RES.with(|c| c.set(1i32 << delta.lf_res)); |
| 13837 | DELTA_LF_MULTI.with(|c| c.set(delta.lf_multi)); |
| 13838 | let (cols32, rows32) = block_grid(mi_cols, mi_rows); |
| 13839 | let (true_width, true_height) = ((mi_cols * 4) as usize, (mi_rows * 4) as usize); |
| 13840 | let (width, height) = (cols32 as usize * BLOCK, rows32 as usize * BLOCK); |
| 13841 | |
| 13842 | let mut y = PlaneBuf { |
| 13843 | data: vec![0u16; width * height], |
| 13844 | width, |
| 13845 | height, |
| 13846 | true_width, |
| 13847 | true_height, |
| 13848 | tile_x0: 0, |
| 13849 | tile_y0: 0, |
| 13850 | tile_x1: width, |
| 13851 | tile_y1: height, |
| 13852 | }; |
| 13853 | let mut u = PlaneBuf { |
| 13854 | data: vec![0u16; width * height / 4], |
| 13855 | width: width / 2, |
| 13856 | height: height / 2, |
| 13857 | true_width: true_width / 2, |
| 13858 | true_height: true_height / 2, |
| 13859 | tile_x0: 0, |
| 13860 | tile_y0: 0, |
| 13861 | tile_x1: width / 2, |
| 13862 | tile_y1: height / 2, |
| 13863 | }; |
| 13864 | let mut v = PlaneBuf { |
| 13865 | data: vec![0u16; width * height / 4], |
| 13866 | width: width / 2, |
| 13867 | height: height / 2, |
| 13868 | true_width: true_width / 2, |
| 13869 | true_height: true_height / 2, |
| 13870 | tile_x0: 0, |
| 13871 | tile_y0: 0, |
| 13872 | tile_x1: width / 2, |
| 13873 | tile_y1: height / 2, |
| 13874 | }; |
| 13875 | |
| 13876 | let scan32 = default_scan(TX32); |
| 13877 | let scan16 = default_scan(TX16); |
| 13878 | let scan8 = default_scan(TX8); |
| 13879 | let scan4 = default_scan(TX4); |
| 13880 | |
| 13881 | let base_cdfs = initial_cdfs.unwrap_or_else(|| Cdfs::new(q_ctx_of(base_q_idx))); |
| 13882 | let mut result_cdfs = base_cdfs.clone(); |
| 13883 | let mut neighbours = Neighbours::new( |
| 13884 | cols32 as usize * 2, |
| 13885 | rows32 as usize * 2, |
| 13886 | mi_cols as usize, |
| 13887 | mi_rows as usize, |
| 13888 | ); |
| 13889 | let mut lr_grid = crate::restoration::RestorationGrid::new(lr, frame_width, frame_height); |
| 13890 | let mut lr_reference = [( |
| 13891 | crate::restoration::WienerInfo::default(), |
| 13892 | crate::restoration::SgrprojInfo::default(), |
| 13893 | ); 3]; |
| 13894 | |
| 13895 | // lane-tiles r2: each tile gets its own fresh `SymbolDecoder` over its |
| 13896 | // own byte range and its own fresh copy of the frame's initial CDFs |
| 13897 | // (spec 5.11.2 `decode_tile`'s `init_symbol`); only the tile named by |
| 13898 | // `context_update_tile_id` has its end-of-tile adapted table kept as the |
| 13899 | // frame's own output (spec `exit_symbol`) -- every other tile's |
| 13900 | // adaptation is discarded, matching the refusal this replaces. |
| 13901 | for (tile_idx, &tile_bytes) in tiles.iter().enumerate() { |
| 13902 | let tile_num = tile_idx as u32; |
| 13903 | let (trow, tcol) = (tile_num / tile_info.cols, tile_num % tile_info.cols); |
| 13904 | let mi_row0 = tile_info.mi_row_starts[trow as usize]; |
| 13905 | let mi_row1 = tile_info.mi_row_starts[trow as usize + 1]; |
| 13906 | let mi_col0 = tile_info.mi_col_starts[tcol as usize]; |
| 13907 | let mi_col1 = tile_info.mi_col_starts[tcol as usize + 1]; |
| 13908 | // Tile boundaries are always superblock-aligned in mi units (spec |
| 13909 | // 5.9.15's uniform/non-uniform derivation both only ever emit |
| 13910 | // multiples of the superblock's own mi size), so plain division is |
| 13911 | // exact here -- no rounding needed. |
| 13912 | // `mi_row0`/`mi_col0` (this tile's own start, `0` or an interior |
| 13913 | // tile boundary) are always superblock-aligned by spec 5.9.15; the |
| 13914 | // frame's own true final edge (`mi_row_starts`/`mi_col_starts`' |
| 13915 | // last entry) is not, so the upper bound needs `div_ceil` the same |
| 13916 | // way the whole-frame `sb_rows`/`sb_cols` above does, to keep the |
| 13917 | // trailing partial superblock row/column in scope. |
| 13918 | let (sb_r0, sb_r1) = ((mi_row0 / SB_MI).min(sb_rows), mi_row1.div_ceil(SB_MI).min(sb_rows)); |
| 13919 | let (sb_c0, sb_c1) = ((mi_col0 / SB_MI).min(sb_cols), mi_col1.div_ceil(SB_MI).min(sb_cols)); |
| 13920 | |
| 13921 | let mut cdfs = base_cdfs.clone(); |
| 13922 | // spec `decode_tile`: `CurrentQIndex` resets to the frame's own |
| 13923 | // `base_q_idx` at the top of every tile, not once per frame. |
| 13924 | CURRENT_Q_IDX.with(|c| c.set(i32::from(base_q_idx))); |
| 13925 | QUANT_DELTAS.with(|c| c.set(deltas)); |
| 13926 | CURRENT_DELTA_LF.with(|c| c.set([0; 4])); |
| 13927 | if std::env::var_os("EC_AV1_TRACE").is_some() { |
| 13928 | eprintln!( |
| 13929 | "TRACE key_tile_bytes tile={tile_num} len={} first8={:02x?} base_q_idx={base_q_idx} mi_cols={mi_cols} mi_rows={mi_rows}", |
| 13930 | tile_bytes.len(), |
| 13931 | &tile_bytes[..tile_bytes.len().min(8)] |
| 13932 | ); |
| 13933 | } |
| 13934 | let mut dec = SymbolDecoder::new(tile_bytes); |
| 13935 | neighbours.start_tile(mi_row0 as usize, mi_col0 as usize, mi_col1 as usize); |
| 13936 | y.set_tile_origin( |
| 13937 | mi_col0 as usize * 4, |
| 13938 | mi_row0 as usize * 4, |
| 13939 | (mi_col1 as usize * 4).min(y.width), |
| 13940 | (mi_row1 as usize * 4).min(y.height), |
| 13941 | ); |
| 13942 | u.set_tile_origin( |
| 13943 | mi_col0 as usize * 2, |
| 13944 | mi_row0 as usize * 2, |
| 13945 | (mi_col1 as usize * 2).min(u.width), |
| 13946 | (mi_row1 as usize * 2).min(u.height), |
| 13947 | ); |
| 13948 | v.set_tile_origin( |
| 13949 | mi_col0 as usize * 2, |
| 13950 | mi_row0 as usize * 2, |
| 13951 | (mi_col1 as usize * 2).min(v.width), |
| 13952 | (mi_row1 as usize * 2).min(v.height), |
| 13953 | ); |
| 13954 | TILE_HITS.with(|c| c.set(c.get() + 1)); |
| 13955 | |
| 13956 | // lane-sb128b r1: set when the 128 root resolved to `PARTITION_NONE` -- |
| 13957 | // its one block covers all four 64x64 quadrants, so the remaining three |
| 13958 | // visits of this superblock code nothing at all. |
| 13959 | let mut sb128_none = false; |
| 13960 | for (sb_r, sb_c, row_start, sb_start) in sb_visit_order(sb_r0, sb_r1, sb_c0, sb_c1, sb128) { |
| 13961 | if row_start { |
| 13962 | neighbours.start_row(); |
| 13963 | } |
| 13964 | if sb_start { |
| 13965 | sb128_none = false; |
| 13966 | } |
| 13967 | if sb128_none { |
| 13968 | continue; |
| 13969 | } |
| 13970 | { |
| 13971 | // lane-sb128 r1: the 128 root. Its loop-restoration units are read |
| 13972 | // once for the whole 128x128 (spec 5.11.57 is called with the |
| 13973 | // superblock's own size), then one partition symbol off the |
| 13974 | // `BLOCK_128X128` CDF (8 symbols) decides the root -- only SPLIT |
| 13975 | // recurses into the four 64x64 quadrants this decoder codes. |
| 13976 | if sb128 && sb_start { |
| 13977 | let part128 = read_sb128_root( |
| 13978 | &mut dec, |
| 13979 | &mut cdfs, |
| 13980 | &neighbours, |
| 13981 | lr, |
| 13982 | &mut lr_grid, |
| 13983 | &mut lr_reference, |
| 13984 | sb_r, |
| 13985 | sb_c, |
| 13986 | mi_cols, |
| 13987 | mi_rows, |
| 13988 | )?; |
| 13989 | if part128 == PARTITION_NONE { |
| 13990 | // One 128x128 intra block: four TX_64X64 luma units and a |
| 13991 | // single 64x64 chroma unit per plane (`av1_get_max_uv_txsize` |
| 13992 | // of a 128x128 block is TX_64X64 over its 64x64 uv plane |
| 13993 | // block). No CfL, no filter intra, no palette at this size -- |
| 13994 | // [`read_intra_mode`]'s own size gates already say so. |
| 13995 | sb128_none = true; |
| 13996 | CDEF_TRANSMITTED.with(|c| c.set(false)); |
| 13997 | let at128 = ((sb_r & !1) as usize * 4, (sb_c & !1) as usize * 4); |
| 13998 | decode_block( |
| 13999 | &mut dec, |
| 14000 | &mut cdfs, |
| 14001 | &mut neighbours, |
| 14002 | at128, |
| 14003 | SB * 2, |
| 14004 | TxbSet::Luma64, |
| 14005 | TxbSet::Chroma32, |
| 14006 | TX32, |
| 14007 | TX32, |
| 14008 | (&scan32, &scan32), |
| 14009 | false, |
| 14010 | &mut y, |
| 14011 | &mut u, |
| 14012 | &mut v, |
| 14013 | base_q_idx, |
| 14014 | enable_filter_intra, |
| 14015 | allow_screen_content_tools, |
| 14016 | allow_intrabc, |
| 14017 | &scan32, |
| 14018 | &scan16, |
| 14019 | &scan8, |
| 14020 | &scan4, |
| 14021 | tx_select, |
| 14022 | reduced_tx_set, |
| 14023 | )?; |
| 14024 | // libaom writes one `cdef_strength` into the block's own |
| 14025 | // `MB_MODE_INFO`, which every mi cell of the block -- |
| 14026 | // hence all four 64x64 CDEF units -- points at; our grid |
| 14027 | // is per-unit, so the read (which only ever lands in the |
| 14028 | // top-left unit, `read_cdef`'s index 0) is copied across. |
| 14029 | let (q_r, q_c) = ((sb_r & !1) as usize, (sb_c & !1) as usize); |
| 14030 | CDEF_IDX_GRID.with(|g| { |
| 14031 | let mut g = g.borrow_mut(); |
| 14032 | let cols = CDEF_SB_COLS.with(|c| c.get()); |
| 14033 | if cols > 0 && q_r * cols + q_c < g.len() { |
| 14034 | let idx = g[q_r * cols + q_c]; |
| 14035 | for (dr, dc) in [(0, 1), (1, 0), (1, 1)] { |
| 14036 | let i = (q_r + dr) * cols + q_c + dc; |
| 14037 | if q_c + dc < cols && i < g.len() { |
| 14038 | g[i] = idx; |
| 14039 | } |
| 14040 | } |
| 14041 | } |
| 14042 | }); |
| 14043 | continue; |
| 14044 | } |
| 14045 | } |
| 14046 | if !sb128 { |
| 14047 | crate::restoration::read_lr( |
| 14048 | &mut dec, |
| 14049 | &mut cdfs, |
| 14050 | lr, |
| 14051 | &mut lr_grid, |
| 14052 | &mut lr_reference, |
| 14053 | sb_r * SB_MI, |
| 14054 | sb_c * SB_MI, |
| 14055 | SB_MI, |
| 14056 | ); |
| 14057 | } |
| 14058 | CDEF_TRANSMITTED.with(|c| c.set(false)); |
| 14059 | let at = (sb_r as usize * 4, sb_c as usize * 4); |
| 14060 | let ctx = neighbours.partition_ctx(at, SB); |
| 14061 | let (has_cols, has_rows) = ( |
| 14062 | sb_c * SB_MI + SB_MI / 2 < mi_cols, |
| 14063 | sb_r * SB_MI + SB_MI / 2 < mi_rows, |
| 14064 | ); |
| 14065 | // Recomputed at this superblock's own half (spec |
| 14066 | // `decode_partition`): a full alphabet symbol when both halves are |
| 14067 | // inside the true frame, a single gathered bit when just one is |
| 14068 | // (the superblock cannot be left whole, so this is forced split), |
| 14069 | // and nothing at all (SPLIT inferred, no bits) when neither is — |
| 14070 | // mirroring [`crate::tile::sb_coeff_key_frame_tile`]'s own |
| 14071 | // three-way write exactly. |
| 14072 | let part = if has_cols && has_rows { |
| 14073 | let p = dec.symbol(&mut cdfs.partition_w64[ctx]); |
| 14074 | if std::env::var_os("EC_AV1_TRACE").is_some() { |
| 14075 | eprintln!("TRACE partition_w64 ctx={ctx} value={p}"); |
| 14076 | } |
| 14077 | p |
| 14078 | } else { |
| 14079 | // lane-hgkf r1 (class `parsed-then-discarded`, same finding as |
| 14080 | // the unmerged lane-golomb r1): the gathered bit IS the |
| 14081 | // partition -- libaom `ec_read_partition_impl` returns |
| 14082 | // PARTITION_HORZ (resp. VERT) for 0 and only SPLIT for 1. |
| 14083 | // Forcing SPLIT desynced the last superblock row of every |
| 14084 | // frame whose height is not a multiple of 64: the ReelB key frames (3840x1608) diverge at mi_row=400 mi_col=0, |
| 14085 | // the first block of that row. |
| 14086 | match (has_cols, has_rows) { |
| 14087 | (true, false) => { |
| 14088 | trace_edge((sb_r * SB_MI) as usize, (sb_c * SB_MI) as usize, 12, ctx, dec.debug_state().0 as u32); |
| 14089 | let b = dec.symbol_fixed(&gather(&cdfs.partition_w64[ctx], VERT_ALIKE)); |
| 14090 | bump_edge32(4 + b.min(1) as usize); |
| 14091 | bump_edge_part(b.min(1) as usize); |
| 14092 | if b == 1 { PARTITION_SPLIT } else { PARTITION_HORZ } |
| 14093 | } |
| 14094 | (false, true) => { |
| 14095 | trace_edge((sb_r * SB_MI) as usize, (sb_c * SB_MI) as usize, 12, ctx, dec.debug_state().0 as u32); |
| 14096 | let b = dec.symbol_fixed(&gather(&cdfs.partition_w64[ctx], HORZ_ALIKE)); |
| 14097 | bump_edge32(4 + b.min(1) as usize); |
| 14098 | bump_edge_part(b.min(1) as usize); |
| 14099 | if b == 1 { PARTITION_SPLIT } else { PARTITION_VERT } |
| 14100 | } |
| 14101 | _ => PARTITION_SPLIT, |
| 14102 | } |
| 14103 | }; |
| 14104 | match part { |
| 14105 | PARTITION_NONE => { |
| 14106 | decode_block( |
| 14107 | &mut dec, |
| 14108 | &mut cdfs, |
| 14109 | &mut neighbours, |
| 14110 | at, |
| 14111 | SB, |
| 14112 | TxbSet::Luma64, |
| 14113 | TxbSet::Chroma32, |
| 14114 | TX32, |
| 14115 | TX32, |
| 14116 | (&scan32, &scan32), |
| 14117 | false, |
| 14118 | &mut y, |
| 14119 | &mut u, |
| 14120 | &mut v, |
| 14121 | base_q_idx, |
| 14122 | enable_filter_intra, |
| 14123 | allow_screen_content_tools, |
| 14124 | allow_intrabc, |
| 14125 | &scan32, |
| 14126 | &scan16, |
| 14127 | &scan8, |
| 14128 | &scan4, |
| 14129 | tx_select, |
| 14130 | reduced_tx_set, |
| 14131 | )?; |
| 14132 | } |
| 14133 | PARTITION_SPLIT => { |
| 14134 | for q in 0..4 { |
| 14135 | let (r32, c32) = (sb_r * 2 + q / 2, sb_c * 2 + q % 2); |
| 14136 | if r32 >= rows32 || c32 >= cols32 { |
| 14137 | continue; |
| 14138 | } |
| 14139 | let at32 = (r32 as usize * 2, c32 as usize * 2); |
| 14140 | let ctx32 = neighbours.partition_ctx(at32, BLOCK); |
| 14141 | let (has_cols32, has_rows32) = ( |
| 14142 | has_half(c32 * BLOCK_MI, BLOCK_MI, mi_cols), |
| 14143 | has_half(r32 * BLOCK_MI, BLOCK_MI, mi_rows), |
| 14144 | ); |
| 14145 | let part32 = if has_cols32 && has_rows32 { |
| 14146 | let p = dec.symbol(&mut cdfs.partition_w32[ctx32]); |
| 14147 | if std::env::var_os("EC_AV1_TRACE").is_some() { |
| 14148 | let (rng, _) = dec.debug_state(); |
| 14149 | eprintln!( |
| 14150 | "TRACE partition_w32 mi=({},{}) ctx={ctx32} value={p} rng={rng}", |
| 14151 | r32 * BLOCK_MI, |
| 14152 | c32 * BLOCK_MI |
| 14153 | ); |
| 14154 | } |
| 14155 | p |
| 14156 | } else { |
| 14157 | // lane-hgkf r1: the 32-level frame-edge bit is the |
| 14158 | // partition too (same libaom reader as the |
| 14159 | // superblock level above). |
| 14160 | match (has_cols32, has_rows32) { |
| 14161 | (true, false) => { |
| 14162 | trace_edge((r32 * BLOCK_MI) as usize, (c32 * BLOCK_MI) as usize, 9, ctx32, dec.debug_state().0 as u32); |
| 14163 | let b = dec.symbol_fixed(&gather( |
| 14164 | &cdfs.partition_w32[ctx32], |
| 14165 | VERT_ALIKE, |
| 14166 | )); |
| 14167 | bump_edge32(b.min(1) as usize); |
| 14168 | bump_edge_part(2 + b.min(1) as usize); |
| 14169 | if b == 1 { PARTITION_SPLIT } else { PARTITION_HORZ } |
| 14170 | } |
| 14171 | (false, true) => { |
| 14172 | trace_edge((r32 * BLOCK_MI) as usize, (c32 * BLOCK_MI) as usize, 9, ctx32, dec.debug_state().0 as u32); |
| 14173 | let b = dec.symbol_fixed(&gather( |
| 14174 | &cdfs.partition_w32[ctx32], |
| 14175 | HORZ_ALIKE, |
| 14176 | )); |
| 14177 | bump_edge32(b.min(1) as usize); |
| 14178 | bump_edge_part(2 + b.min(1) as usize); |
| 14179 | if b == 1 { PARTITION_SPLIT } else { PARTITION_VERT } |
| 14180 | } |
| 14181 | _ => PARTITION_SPLIT, |
| 14182 | } |
| 14183 | }; |
| 14184 | match part32 { |
| 14185 | PARTITION_NONE => { |
| 14186 | decode_block( |
| 14187 | &mut dec, |
| 14188 | &mut cdfs, |
| 14189 | &mut neighbours, |
| 14190 | at32, |
| 14191 | BLOCK, |
| 14192 | TxbSet::Luma32, |
| 14193 | TxbSet::Chroma16, |
| 14194 | TX32, |
| 14195 | TX16, |
| 14196 | (&scan32, &scan16), |
| 14197 | true, |
| 14198 | &mut y, |
| 14199 | &mut u, |
| 14200 | &mut v, |
| 14201 | base_q_idx, |
| 14202 | enable_filter_intra, |
| 14203 | allow_screen_content_tools, |
| 14204 | allow_intrabc, |
| 14205 | &scan32, |
| 14206 | &scan16, |
| 14207 | &scan8, |
| 14208 | &scan4, |
| 14209 | tx_select, |
| 14210 | reduced_tx_set, |
| 14211 | )?; |
| 14212 | } |
| 14213 | PARTITION_SPLIT => { |
| 14214 | for sub in 0..4 { |
| 14215 | let (sr, sc) = |
| 14216 | (r32 as usize * 2 + sub / 2, c32 as usize * 2 + sub % 2); |
| 14217 | if (sr as u32) * SUB_MI >= mi_rows |
| 14218 | || (sc as u32) * SUB_MI >= mi_cols |
| 14219 | { |
| 14220 | continue; |
| 14221 | } |
| 14222 | let (has_cols16, has_rows16) = ( |
| 14223 | has_half(sc as u32 * SUB_MI, SUB_MI, mi_cols), |
| 14224 | has_half(sr as u32 * SUB_MI, SUB_MI, mi_rows), |
| 14225 | ); |
| 14226 | let at16 = (sr, sc); |
| 14227 | let ctx16 = neighbours.partition_ctx(at16, SUB); |
| 14228 | if has_cols16 && has_rows16 { |
| 14229 | let part16 = dec.symbol(&mut cdfs.partition_w16[ctx16]); |
| 14230 | if std::env::var_os("EC_AV1_TRACE").is_some() { |
| 14231 | eprintln!( |
| 14232 | "TRACE partition_w16 mi=({},{}) ctx={ctx16} value={part16}", |
| 14233 | at16.0, at16.1 |
| 14234 | ); |
| 14235 | } |
| 14236 | if part16 == PARTITION_NONE { |
| 14237 | decode_block( |
| 14238 | &mut dec, |
| 14239 | &mut cdfs, |
| 14240 | &mut neighbours, |
| 14241 | at16, |
| 14242 | SUB, |
| 14243 | TxbSet::Luma16, |
| 14244 | TxbSet::Chroma8, |
| 14245 | TX16, |
| 14246 | TX8, |
| 14247 | (&scan16, &scan8), |
| 14248 | true, |
| 14249 | &mut y, |
| 14250 | &mut u, |
| 14251 | &mut v, |
| 14252 | base_q_idx, |
| 14253 | enable_filter_intra, |
| 14254 | allow_screen_content_tools, |
| 14255 | allow_intrabc, |
| 14256 | &scan32, |
| 14257 | &scan16, |
| 14258 | &scan8, |
| 14259 | &scan4, |
| 14260 | tx_select, |
| 14261 | reduced_tx_set, |
| 14262 | )?; |
| 14263 | continue; |
| 14264 | } |
| 14265 | if (PARTITION_HORZ_A..=PARTITION_VERT_B).contains(&part16) { |
| 14266 | // lane-ab16 r1: the four AB (T-shaped) partitions at the 16x16 |
| 14267 | // level -- two 8x8 squares plus one 16x8/8x16 strip. libaom's |
| 14268 | // `decode_partition` visit order (decodeframe.c): |
| 14269 | // HORZ_A: TL 8x8, TR 8x8, bottom 16x8 |
| 14270 | // HORZ_B: top 16x8, BL 8x8, BR 8x8 |
| 14271 | // VERT_A: TL 8x8, BL 8x8, right 8x16 |
| 14272 | // VERT_B: left 8x16, TR 8x8, BR 8x8 |
| 14273 | // The squares go through `decode_leaf8`, the strip through |
| 14274 | // `decode_leaf_rect` (real, non-skip 16x8/8x16 coefficients -- |
| 14275 | // lane-rectx r2-r4; the older `decode_block_rect` this VERT_B arm |
| 14276 | // used refuses the non-skip case at this size). Both writers keep |
| 14277 | // mi-granular mode/skip/palette records, so a later block's |
| 14278 | // `above_mi`/`left_mi` sees the real sub-block, not the coarse |
| 14279 | // 16x16 cell. |
| 14280 | // |
| 14281 | // VERT_A/VERT_B visit TL,BL,TR,BR, so the bottom-left square's |
| 14282 | // top-right neighbour is the partition's own not-yet-decoded |
| 14283 | // rectangle: libaom switches those squares onto `has_tr_vert_*`/ |
| 14284 | // `has_bl_vert_*` (`reconintra.c` `get_has_tr_table`), which is |
| 14285 | // `Reach::vert_ab_partition()` (lane-part32 r5, cherry-picked here). |
| 14286 | let arm = part16 - PARTITION_HORZ_A; |
| 14287 | bump_ab16(arm); |
| 14288 | if part16 == PARTITION_VERT_B { |
| 14289 | VERT_B_INTRA_HITS.with(|c| c.set(c.get() + 1)); |
| 14290 | } |
| 14291 | let _vert_guard = if part16 == PARTITION_VERT_A || part16 == PARTITION_VERT_B { |
| 14292 | Some(crate::encode::Reach::vert_ab_partition()) |
| 14293 | } else { |
| 14294 | None |
| 14295 | }; |
| 14296 | let (mi_row0, mi_col0) = (sr * SUB_MI as usize, sc * SUB_MI as usize); |
| 14297 | macro_rules! ab_leaf8 { |
| 14298 | ($mi:expr, $prev:expr) => { |
| 14299 | decode_leaf8( |
| 14300 | &mut dec, |
| 14301 | &mut cdfs, |
| 14302 | &mut neighbours, |
| 14303 | at16, |
| 14304 | $mi, |
| 14305 | (&scan8, &scan4), |
| 14306 | $prev, |
| 14307 | &mut y, |
| 14308 | &mut u, |
| 14309 | &mut v, |
| 14310 | base_q_idx, |
| 14311 | enable_filter_intra, |
| 14312 | allow_screen_content_tools, |
| 14313 | allow_intrabc, |
| 14314 | tx_select, |
| 14315 | reduced_tx_set, |
| 14316 | )? |
| 14317 | }; |
| 14318 | } |
| 14319 | macro_rules! ab_strip { |
| 14320 | ($mi:expr, $bw:expr, $bh:expr, $prev:expr) => { |
| 14321 | decode_leaf_rect( |
| 14322 | &mut dec, |
| 14323 | &mut cdfs, |
| 14324 | &mut neighbours, |
| 14325 | at16, |
| 14326 | $mi, |
| 14327 | $bw, |
| 14328 | $bh, |
| 14329 | $prev, |
| 14330 | &mut y, |
| 14331 | &mut u, |
| 14332 | &mut v, |
| 14333 | enable_filter_intra, |
| 14334 | allow_screen_content_tools, |
| 14335 | base_q_idx, |
| 14336 | tx_select, |
| 14337 | reduced_tx_set, |
| 14338 | )? |
| 14339 | }; |
| 14340 | } |
| 14341 | let tl = (mi_row0, mi_col0); |
| 14342 | let tr = (mi_row0, mi_col0 + 2); |
| 14343 | let bl = (mi_row0 + 2, mi_col0); |
| 14344 | let br = (mi_row0 + 2, mi_col0 + 2); |
| 14345 | let mut done: Vec<((usize, usize), usize)> = Vec::new(); |
| 14346 | match part16 { |
| 14347 | PARTITION_HORZ_A => { |
| 14348 | let m_tl = ab_leaf8!(tl, done.last().copied()); |
| 14349 | done.push((tl, m_tl)); |
| 14350 | let m_tr = ab_leaf8!(tr, done.last().copied()); |
| 14351 | done.push((tr, m_tr)); |
| 14352 | let m_bot = ab_strip!(bl, 16, 8, Some((tl, m_tl))); |
| 14353 | done.push((bl, m_bot)); |
| 14354 | } |
| 14355 | PARTITION_HORZ_B => { |
| 14356 | let m_top = ab_strip!(tl, 16, 8, None); |
| 14357 | done.push((tl, m_top)); |
| 14358 | let m_bl = ab_leaf8!(bl, done.last().copied()); |
| 14359 | done.push((bl, m_bl)); |
| 14360 | let m_br = ab_leaf8!(br, done.last().copied()); |
| 14361 | done.push((br, m_br)); |
| 14362 | } |
| 14363 | PARTITION_VERT_A => { |
| 14364 | let m_tl = ab_leaf8!(tl, done.last().copied()); |
| 14365 | done.push((tl, m_tl)); |
| 14366 | let m_bl = ab_leaf8!(bl, done.last().copied()); |
| 14367 | done.push((bl, m_bl)); |
| 14368 | let m_right = ab_strip!(tr, 8, 16, Some((tl, m_tl))); |
| 14369 | done.push((tr, m_right)); |
| 14370 | } |
| 14371 | _ => { |
| 14372 | let m_left = ab_strip!(tl, 8, 16, None); |
| 14373 | done.push((tl, m_left)); |
| 14374 | let m_tr = ab_leaf8!(tr, done.last().copied()); |
| 14375 | done.push((tr, m_tr)); |
| 14376 | let m_br = ab_leaf8!(br, done.last().copied()); |
| 14377 | done.push((br, m_br)); |
| 14378 | } |
| 14379 | } |
| 14380 | // Same coarse-slot write-back the real SPLIT path takes: the |
| 14381 | // 16x16 cell's single `above_mode`/`left_mode` entry gets the |
| 14382 | // sub-block on the parent's bottom / right edge (the mi-exact map |
| 14383 | // above wins wherever it has an entry). |
| 14384 | write_back_split_modes(&mut neighbours, &done, (mi_row0, mi_col0), (sr, sc)); |
| 14385 | continue; |
| 14386 | } |
| 14387 | if part16 == PARTITION_HORZ { |
| 14388 | HORZ_VERT_INTRA_HITS.with(|c| c.set(c.get() + 1)); |
| 14389 | let (mi_row0, mi_col0) = ( |
| 14390 | sr as u32 * SUB_MI, |
| 14391 | sc as u32 * SUB_MI, |
| 14392 | ); |
| 14393 | let top = (mi_row0 as usize, mi_col0 as usize); |
| 14394 | let bot = (mi_row0 as usize + 2, mi_col0 as usize); |
| 14395 | let mode_top = decode_leaf_rect( |
| 14396 | &mut dec, |
| 14397 | &mut cdfs, |
| 14398 | &mut neighbours, |
| 14399 | at16, |
| 14400 | top, |
| 14401 | 16, |
| 14402 | 8, |
| 14403 | None, |
| 14404 | &mut y, |
| 14405 | &mut u, |
| 14406 | &mut v, |
| 14407 | enable_filter_intra, |
| 14408 | allow_screen_content_tools, |
| 14409 | base_q_idx, |
| 14410 | tx_select, |
| 14411 | reduced_tx_set, |
| 14412 | )?; |
| 14413 | let mode_bot = decode_leaf_rect( |
| 14414 | &mut dec, |
| 14415 | &mut cdfs, |
| 14416 | &mut neighbours, |
| 14417 | at16, |
| 14418 | bot, |
| 14419 | 16, |
| 14420 | 8, |
| 14421 | Some((top, mode_top)), |
| 14422 | &mut y, |
| 14423 | &mut u, |
| 14424 | &mut v, |
| 14425 | enable_filter_intra, |
| 14426 | allow_screen_content_tools, |
| 14427 | base_q_idx, |
| 14428 | tx_select, |
| 14429 | reduced_tx_set, |
| 14430 | )?; |
| 14431 | neighbours.above_mode[sc] = mode_bot; |
| 14432 | neighbours.left_mode[sr] = mode_bot; |
| 14433 | continue; |
| 14434 | } |
| 14435 | if part16 == PARTITION_VERT { |
| 14436 | HORZ_VERT_INTRA_HITS.with(|c| c.set(c.get() + 1)); |
| 14437 | let (mi_row0, mi_col0) = ( |
| 14438 | sr as u32 * SUB_MI, |
| 14439 | sc as u32 * SUB_MI, |
| 14440 | ); |
| 14441 | let left = (mi_row0 as usize, mi_col0 as usize); |
| 14442 | let right = (mi_row0 as usize, mi_col0 as usize + 2); |
| 14443 | let mode_left = decode_leaf_rect( |
| 14444 | &mut dec, |
| 14445 | &mut cdfs, |
| 14446 | &mut neighbours, |
| 14447 | at16, |
| 14448 | left, |
| 14449 | 8, |
| 14450 | 16, |
| 14451 | None, |
| 14452 | &mut y, |
| 14453 | &mut u, |
| 14454 | &mut v, |
| 14455 | enable_filter_intra, |
| 14456 | allow_screen_content_tools, |
| 14457 | base_q_idx, |
| 14458 | tx_select, |
| 14459 | reduced_tx_set, |
| 14460 | )?; |
| 14461 | let mode_right = decode_leaf_rect( |
| 14462 | &mut dec, |
| 14463 | &mut cdfs, |
| 14464 | &mut neighbours, |
| 14465 | at16, |
| 14466 | right, |
| 14467 | 8, |
| 14468 | 16, |
| 14469 | Some((left, mode_left)), |
| 14470 | &mut y, |
| 14471 | &mut u, |
| 14472 | &mut v, |
| 14473 | enable_filter_intra, |
| 14474 | allow_screen_content_tools, |
| 14475 | base_q_idx, |
| 14476 | tx_select, |
| 14477 | reduced_tx_set, |
| 14478 | )?; |
| 14479 | neighbours.above_mode[sc] = mode_right; |
| 14480 | neighbours.left_mode[sr] = mode_right; |
| 14481 | continue; |
| 14482 | } |
| 14483 | if part16 == PARTITION_HORZ_4 |
| 14484 | || part16 == PARTITION_VERT_4 |
| 14485 | { |
| 14486 | decode_rect4_16( |
| 14487 | &mut dec, |
| 14488 | &mut cdfs, |
| 14489 | &mut neighbours, |
| 14490 | at16, |
| 14491 | part16 == PARTITION_HORZ_4, |
| 14492 | &mut y, |
| 14493 | &mut u, |
| 14494 | &mut v, |
| 14495 | enable_filter_intra, |
| 14496 | allow_screen_content_tools, |
| 14497 | base_q_idx, |
| 14498 | tx_select, |
| 14499 | reduced_tx_set, |
| 14500 | )?; |
| 14501 | continue; |
| 14502 | } |
| 14503 | // lane-r14 r2 (verifier note): every |
| 14504 | // one of the 10 symbols this CDF can |
| 14505 | // produce is handled above, so SPLIT |
| 14506 | // is the only value that reaches |
| 14507 | // here -- the refusal that used to |
| 14508 | // stand here was unreachable and is |
| 14509 | // gone from `refusal_inventory`. |
| 14510 | debug_assert_eq!(part16, PARTITION_SPLIT); |
| 14511 | // A real (non-straddle) SPLIT of a |
| 14512 | // whole 16x16 into four 8x8 leaves -- |
| 14513 | // the same recursion the straddle |
| 14514 | // path below already runs when the |
| 14515 | // true frame edge forces it, just |
| 14516 | // over all four positions instead of |
| 14517 | // the one or two the edge leaves. |
| 14518 | let (mi_row0, mi_col0) = |
| 14519 | (sr as u32 * SUB_MI, sc as u32 * SUB_MI); |
| 14520 | // Each 8x8 leaf's intra-mode context |
| 14521 | // must see its own siblings: the |
| 14522 | // bottom-left leaf's ABOVE neighbour |
| 14523 | // is leaf 0, not the previously |
| 14524 | // decoded leaf 1 (and the bottom-right |
| 14525 | // leaf's above is leaf 1, while |
| 14526 | // `prev_leaf` only ever carried the |
| 14527 | // immediately preceding one). The |
| 14528 | // coarse `above_mode`/`left_mode` |
| 14529 | // arrays hold one slot per 16x16, so |
| 14530 | // the sibling modes are swapped in |
| 14531 | // around each leaf call and restored |
| 14532 | // after; the loop tail writes the last |
| 14533 | // leaf's mode as before. |
| 14534 | let mut sib_modes: [Option<(usize, usize)>; 4] = [None; 4]; |
| 14535 | for i in 0..4 { |
| 14536 | let (mr, mc) = |
| 14537 | (mi_row0 + (i / 2) * 2, mi_col0 + (i % 2) * 2); |
| 14538 | let leaf_mi = (mr as usize, mc as usize); |
| 14539 | let li = i as usize; |
| 14540 | let saved_modes = |
| 14541 | (neighbours.above_mode[sc], neighbours.left_mode[sr]); |
| 14542 | if li >= 2 { |
| 14543 | if let Some((below, _)) = sib_modes[li - 2] { |
| 14544 | neighbours.above_mode[sc] = below; |
| 14545 | } |
| 14546 | } |
| 14547 | if li % 2 == 1 { |
| 14548 | if let Some((_, right)) = sib_modes[li - 1] { |
| 14549 | neighbours.left_mode[sr] = right; |
| 14550 | } |
| 14551 | } |
| 14552 | let leaf_ctx = neighbours.partition_ctx_mi(leaf_mi, 8); |
| 14553 | let part8 = |
| 14554 | dec.symbol(&mut cdfs.partition_w8[leaf_ctx]); |
| 14555 | if std::env::var_os("EC_AV1_TRACE").is_some() { |
| 14556 | eprintln!( |
| 14557 | "TRACE partition_w8 mi=({mr},{mc}) ctx={leaf_ctx} value={part8}" |
| 14558 | ); |
| 14559 | } |
| 14560 | let leaf_mode = if part8 == PARTITION_NONE { |
| 14561 | decode_leaf8( |
| 14562 | &mut dec, |
| 14563 | &mut cdfs, |
| 14564 | &mut neighbours, |
| 14565 | at16, |
| 14566 | leaf_mi, |
| 14567 | (&scan8, &scan4), |
| 14568 | None, |
| 14569 | &mut y, |
| 14570 | &mut u, |
| 14571 | &mut v, |
| 14572 | base_q_idx, |
| 14573 | enable_filter_intra, |
| 14574 | allow_screen_content_tools, |
| 14575 | allow_intrabc, |
| 14576 | tx_select, |
| 14577 | reduced_tx_set, |
| 14578 | ).map(|m| (m, m))? |
| 14579 | } else if part8 == PARTITION_SPLIT { |
| 14580 | decode_leaf_split4( |
| 14581 | &mut dec, |
| 14582 | &mut cdfs, |
| 14583 | &mut neighbours, |
| 14584 | at16, |
| 14585 | leaf_mi, |
| 14586 | &scan4, |
| 14587 | None, |
| 14588 | &mut y, |
| 14589 | &mut u, |
| 14590 | &mut v, |
| 14591 | base_q_idx, |
| 14592 | enable_filter_intra, |
| 14593 | allow_intrabc, |
| 14594 | reduced_tx_set, |
| 14595 | )? |
| 14596 | } else if part8 == PARTITION_HORZ |
| 14597 | || part8 == PARTITION_VERT |
| 14598 | { |
| 14599 | decode_leaf_rect8( |
| 14600 | &mut dec, |
| 14601 | &mut cdfs, |
| 14602 | &mut neighbours, |
| 14603 | at16, |
| 14604 | leaf_mi, |
| 14605 | part8 == PARTITION_VERT, |
| 14606 | &scan4, |
| 14607 | &mut y, |
| 14608 | &mut u, |
| 14609 | &mut v, |
| 14610 | base_q_idx, |
| 14611 | enable_filter_intra, |
| 14612 | allow_intrabc, |
| 14613 | tx_select, |
| 14614 | reduced_tx_set, |
| 14615 | )? |
| 14616 | } else { |
| 14617 | unreachable!("partition_w8 is a 4-symbol CDF: NONE/HORZ/VERT/SPLIT only"); |
| 14618 | }; |
| 14619 | neighbours.above_mode[sc] = saved_modes.0; |
| 14620 | neighbours.left_mode[sr] = saved_modes.1; |
| 14621 | sib_modes[li] = Some(leaf_mode); |
| 14622 | } |
| 14623 | if let Some((below, _)) = sib_modes[2].or(sib_modes[3]).or(sib_modes[0]) { |
| 14624 | neighbours.above_mode[sc] = below; |
| 14625 | } |
| 14626 | if let Some((_, right)) = sib_modes[1].or(sib_modes[3]).or(sib_modes[0]) { |
| 14627 | neighbours.left_mode[sr] = right; |
| 14628 | } |
| 14629 | continue; |
| 14630 | } |
| 14631 | // The true edge falls inside this 16x16 |
| 14632 | // leaf itself (mod-32==8 target sizes, |
| 14633 | // lane-av1-rect): one axis only -- an 8x8 |
| 14634 | // leaf never itself straddles, so the |
| 14635 | // block splits cleanly along whichever |
| 14636 | // axis is short. |
| 14637 | if !has_cols16 && !has_rows16 { |
| 14638 | return Err(unsupported( |
| 14639 | "a 16x16 block whose true edge cuts through both \ |
| 14640 | axes needs a rectangular transform this decoder \ |
| 14641 | does not code yet", |
| 14642 | )); |
| 14643 | } |
| 14644 | // lane-golomb r1 (class |
| 14645 | // `parsed-then-discarded`): the gathered |
| 14646 | // bit IS the partition -- 0 names |
| 14647 | // PARTITION_HORZ (resp. VERT), only 1 is |
| 14648 | // SPLIT (libaom `ec_read_partition_impl`). |
| 14649 | // This path can only walk the four SPLIT |
| 14650 | // leaves, so a 0 refuses by name instead |
| 14651 | // of desyncing. |
| 14652 | trace_edge(sr * SUB_MI as usize, sc * SUB_MI as usize, 6, ctx16, dec.debug_state().0 as u32); |
| 14653 | let edge_split = if has_cols16 { |
| 14654 | dec.symbol_fixed(&gather( |
| 14655 | &cdfs.partition_w16[ctx16], |
| 14656 | VERT_ALIKE, |
| 14657 | )) |
| 14658 | } else { |
| 14659 | dec.symbol_fixed(&gather( |
| 14660 | &cdfs.partition_w16[ctx16], |
| 14661 | HORZ_ALIKE, |
| 14662 | )) |
| 14663 | }; |
| 14664 | // lane-hgkf r1: slots 2/3 of |
| 14665 | // [`EDGE_PART_HITS`], the gate's proof the |
| 14666 | // non-SPLIT edge arm actually decoded. |
| 14667 | bump_edge_part(if edge_split == 1 { 3 } else { 2 }); |
| 14668 | if edge_split == 0 { |
| 14669 | // lane-oddh r1: the strip IS decodable |
| 14670 | // -- libaom's `decode_partition` codes |
| 14671 | // only the FIRST half of an edge |
| 14672 | // HORZ/VERT (the second call is |
| 14673 | // guarded by the same has_rows/ |
| 14674 | // has_cols test that made the |
| 14675 | // partition non-square), so one |
| 14676 | // 16x8 (resp. 8x16) strip covers the |
| 14677 | // whole visible band of a frame whose |
| 14678 | // height (resp. width) is 8 mod 16 -- |
| 14679 | // the user's 3840x1608 film. |
| 14680 | HORZ_VERT_INTRA_HITS.with(|c| c.set(c.get() + 1)); |
| 14681 | EDGE_RECT_STRIP_HITS.with(|c| c.set(c.get() + 1)); |
| 14682 | let (mi_row0, mi_col0) = |
| 14683 | (sr as u32 * SUB_MI, sc as u32 * SUB_MI); |
| 14684 | let (bw, bh) = if has_cols16 { (16, 8) } else { (8, 16) }; |
| 14685 | let mode = decode_leaf_rect( |
| 14686 | &mut dec, |
| 14687 | &mut cdfs, |
| 14688 | &mut neighbours, |
| 14689 | at16, |
| 14690 | (mi_row0 as usize, mi_col0 as usize), |
| 14691 | bw, |
| 14692 | bh, |
| 14693 | None, |
| 14694 | &mut y, |
| 14695 | &mut u, |
| 14696 | &mut v, |
| 14697 | enable_filter_intra, |
| 14698 | allow_screen_content_tools, |
| 14699 | base_q_idx, |
| 14700 | tx_select, |
| 14701 | reduced_tx_set, |
| 14702 | )?; |
| 14703 | neighbours.above_mode[sc] = mode; |
| 14704 | neighbours.left_mode[sr] = mode; |
| 14705 | continue; |
| 14706 | } |
| 14707 | let (mi_row0, mi_col0) = |
| 14708 | (sr as u32 * SUB_MI, sc as u32 * SUB_MI); |
| 14709 | let leaf_positions: Vec<(u32, u32)> = (0..4) |
| 14710 | .map(|i| (mi_row0 + (i / 2) * 2, mi_col0 + (i % 2) * 2)) |
| 14711 | .filter(|&(mr, mc)| mr < mi_rows && mc < mi_cols) |
| 14712 | .collect(); |
| 14713 | // See the sibling-mode note in the clean |
| 14714 | // SPLIT path above. |
| 14715 | let mut sib_modes: [Option<(usize, usize)>; 4] = [None; 4]; |
| 14716 | for (mr, mc) in leaf_positions { |
| 14717 | let leaf_mi = (mr as usize, mc as usize); |
| 14718 | let li = (((mr - mi_row0) / 2) * 2 + (mc - mi_col0) / 2) |
| 14719 | as usize; |
| 14720 | let saved_modes = |
| 14721 | (neighbours.above_mode[sc], neighbours.left_mode[sr]); |
| 14722 | if li >= 2 { |
| 14723 | if let Some((below, _)) = sib_modes[li - 2] { |
| 14724 | neighbours.above_mode[sc] = below; |
| 14725 | } |
| 14726 | } |
| 14727 | if li % 2 == 1 { |
| 14728 | if let Some((_, right)) = sib_modes[li - 1] { |
| 14729 | neighbours.left_mode[sr] = right; |
| 14730 | } |
| 14731 | } |
| 14732 | let leaf_ctx = neighbours.partition_ctx_mi(leaf_mi, 8); |
| 14733 | let pre8 = dec.debug_state().0; |
| 14734 | let part8 = dec.symbol(&mut cdfs.partition_w8[leaf_ctx]); |
| 14735 | if std::env::var_os("EC_AV1_TRACE").is_some() { |
| 14736 | eprintln!( |
| 14737 | "TRACE partition_w8 mi=({mr},{mc}) ctx={leaf_ctx} value={part8} pre_rng={pre8}" |
| 14738 | ); |
| 14739 | } |
| 14740 | let leaf_mode = if part8 == PARTITION_NONE { |
| 14741 | decode_leaf8( |
| 14742 | &mut dec, |
| 14743 | &mut cdfs, |
| 14744 | &mut neighbours, |
| 14745 | at16, |
| 14746 | leaf_mi, |
| 14747 | (&scan8, &scan4), |
| 14748 | None, |
| 14749 | &mut y, |
| 14750 | &mut u, |
| 14751 | &mut v, |
| 14752 | base_q_idx, |
| 14753 | enable_filter_intra, |
| 14754 | allow_screen_content_tools, |
| 14755 | allow_intrabc, |
| 14756 | tx_select, |
| 14757 | reduced_tx_set, |
| 14758 | ).map(|m| (m, m))? |
| 14759 | } else if part8 == PARTITION_SPLIT { |
| 14760 | decode_leaf_split4( |
| 14761 | &mut dec, |
| 14762 | &mut cdfs, |
| 14763 | &mut neighbours, |
| 14764 | at16, |
| 14765 | leaf_mi, |
| 14766 | &scan4, |
| 14767 | None, |
| 14768 | &mut y, |
| 14769 | &mut u, |
| 14770 | &mut v, |
| 14771 | base_q_idx, |
| 14772 | enable_filter_intra, |
| 14773 | allow_intrabc, |
| 14774 | reduced_tx_set, |
| 14775 | )? |
| 14776 | } else if part8 == PARTITION_HORZ |
| 14777 | || part8 == PARTITION_VERT |
| 14778 | { |
| 14779 | decode_leaf_rect8( |
| 14780 | &mut dec, |
| 14781 | &mut cdfs, |
| 14782 | &mut neighbours, |
| 14783 | at16, |
| 14784 | leaf_mi, |
| 14785 | part8 == PARTITION_VERT, |
| 14786 | &scan4, |
| 14787 | &mut y, |
| 14788 | &mut u, |
| 14789 | &mut v, |
| 14790 | base_q_idx, |
| 14791 | enable_filter_intra, |
| 14792 | allow_intrabc, |
| 14793 | tx_select, |
| 14794 | reduced_tx_set, |
| 14795 | )? |
| 14796 | } else { |
| 14797 | unreachable!("partition_w8 is a 4-symbol CDF: NONE/HORZ/VERT/SPLIT only"); |
| 14798 | }; |
| 14799 | neighbours.above_mode[sc] = saved_modes.0; |
| 14800 | neighbours.left_mode[sr] = saved_modes.1; |
| 14801 | sib_modes[li] = Some(leaf_mode); |
| 14802 | } |
| 14803 | // `record()`'s `above_mode`/`left_mode` |
| 14804 | // write is a no-op at an 8x8 leaf's own |
| 14805 | // side, so force the write once the whole |
| 14806 | // 16x16 slot's leaves are done, from the |
| 14807 | // last leaf (mirrors the writer's r15 |
| 14808 | // fix). |
| 14809 | if let Some((below, _)) = sib_modes[2].or(sib_modes[3]).or(sib_modes[0]) { |
| 14810 | neighbours.above_mode[sc] = below; |
| 14811 | } |
| 14812 | if let Some((_, right)) = sib_modes[1].or(sib_modes[3]).or(sib_modes[0]) { |
| 14813 | neighbours.left_mode[sr] = right; |
| 14814 | } |
| 14815 | } |
| 14816 | } |
| 14817 | PARTITION_HORZ => { |
| 14818 | // lane-intradisp r1: two true 32x16 strips. |
| 14819 | decode_block_rect( |
| 14820 | &mut dec, |
| 14821 | &mut cdfs, |
| 14822 | &mut neighbours, |
| 14823 | at32, |
| 14824 | 32, |
| 14825 | 16, |
| 14826 | &mut y, |
| 14827 | &mut u, |
| 14828 | &mut v, |
| 14829 | enable_filter_intra, |
| 14830 | allow_screen_content_tools, |
| 14831 | base_q_idx, |
| 14832 | tx_select, |
| 14833 | reduced_tx_set, |
| 14834 | )?; |
| 14835 | if !has_rows32 { |
| 14836 | bump_edge32(2); |
| 14837 | } |
| 14838 | // lane-hgkf r1: at the frame edge only the first half of the strip is |
| 14839 | // coded (libaom `decode_partition` guards the second half with the same |
| 14840 | // test that made this partition non-square). |
| 14841 | if has_rows32 { |
| 14842 | decode_block_rect( |
| 14843 | &mut dec, |
| 14844 | &mut cdfs, |
| 14845 | &mut neighbours, |
| 14846 | (at32.0 + 1, at32.1), |
| 14847 | 32, |
| 14848 | 16, |
| 14849 | &mut y, |
| 14850 | &mut u, |
| 14851 | &mut v, |
| 14852 | enable_filter_intra, |
| 14853 | allow_screen_content_tools, |
| 14854 | base_q_idx, |
| 14855 | tx_select, |
| 14856 | reduced_tx_set, |
| 14857 | )?; |
| 14858 | } |
| 14859 | } |
| 14860 | PARTITION_VERT => { |
| 14861 | // lane-intradisp r1: mirror of PARTITION_HORZ |
| 14862 | // above with width/height swapped. |
| 14863 | decode_block_rect( |
| 14864 | &mut dec, |
| 14865 | &mut cdfs, |
| 14866 | &mut neighbours, |
| 14867 | at32, |
| 14868 | 16, |
| 14869 | 32, |
| 14870 | &mut y, |
| 14871 | &mut u, |
| 14872 | &mut v, |
| 14873 | enable_filter_intra, |
| 14874 | allow_screen_content_tools, |
| 14875 | base_q_idx, |
| 14876 | tx_select, |
| 14877 | reduced_tx_set, |
| 14878 | )?; |
| 14879 | if !has_cols32 { |
| 14880 | bump_edge32(3); |
| 14881 | } |
| 14882 | // lane-hgkf r1: at the frame edge only the first half of the strip is |
| 14883 | // coded (libaom `decode_partition` guards the second half with the same |
| 14884 | // test that made this partition non-square). |
| 14885 | if has_cols32 { |
| 14886 | decode_block_rect( |
| 14887 | &mut dec, |
| 14888 | &mut cdfs, |
| 14889 | &mut neighbours, |
| 14890 | (at32.0, at32.1 + 1), |
| 14891 | 16, |
| 14892 | 32, |
| 14893 | &mut y, |
| 14894 | &mut u, |
| 14895 | &mut v, |
| 14896 | enable_filter_intra, |
| 14897 | allow_screen_content_tools, |
| 14898 | base_q_idx, |
| 14899 | tx_select, |
| 14900 | reduced_tx_set, |
| 14901 | )?; |
| 14902 | } |
| 14903 | } |
| 14904 | PARTITION_HORZ_A => { |
| 14905 | // lane-part32 r1: two 16x16 squares on top + |
| 14906 | // a true 32x16 strip below (mirrors |
| 14907 | // decode_inter_block's cd6cb6d HORZ_A: TL, |
| 14908 | // TR, bottom strip). |
| 14909 | INTRA_HORZ_A_HITS.with(|c| c.set(c.get() + 1)); |
| 14910 | decode_block( |
| 14911 | &mut dec, |
| 14912 | &mut cdfs, |
| 14913 | &mut neighbours, |
| 14914 | at32, |
| 14915 | SUB, |
| 14916 | TxbSet::Luma16, |
| 14917 | TxbSet::Chroma8, |
| 14918 | TX16, |
| 14919 | TX8, |
| 14920 | (&scan16, &scan8), |
| 14921 | true, |
| 14922 | &mut y, |
| 14923 | &mut u, |
| 14924 | &mut v, |
| 14925 | base_q_idx, |
| 14926 | enable_filter_intra, |
| 14927 | allow_screen_content_tools, |
| 14928 | allow_intrabc, |
| 14929 | &scan32, |
| 14930 | &scan16, |
| 14931 | &scan8, |
| 14932 | &scan4, |
| 14933 | tx_select, |
| 14934 | reduced_tx_set, |
| 14935 | )?; |
| 14936 | decode_block( |
| 14937 | &mut dec, |
| 14938 | &mut cdfs, |
| 14939 | &mut neighbours, |
| 14940 | (at32.0, at32.1 + 1), |
| 14941 | SUB, |
| 14942 | TxbSet::Luma16, |
| 14943 | TxbSet::Chroma8, |
| 14944 | TX16, |
| 14945 | TX8, |
| 14946 | (&scan16, &scan8), |
| 14947 | true, |
| 14948 | &mut y, |
| 14949 | &mut u, |
| 14950 | &mut v, |
| 14951 | base_q_idx, |
| 14952 | enable_filter_intra, |
| 14953 | allow_screen_content_tools, |
| 14954 | allow_intrabc, |
| 14955 | &scan32, |
| 14956 | &scan16, |
| 14957 | &scan8, |
| 14958 | &scan4, |
| 14959 | tx_select, |
| 14960 | reduced_tx_set, |
| 14961 | )?; |
| 14962 | decode_block_rect( |
| 14963 | &mut dec, |
| 14964 | &mut cdfs, |
| 14965 | &mut neighbours, |
| 14966 | (at32.0 + 1, at32.1), |
| 14967 | 32, |
| 14968 | 16, |
| 14969 | &mut y, |
| 14970 | &mut u, |
| 14971 | &mut v, |
| 14972 | enable_filter_intra, |
| 14973 | allow_screen_content_tools, |
| 14974 | base_q_idx, |
| 14975 | tx_select, |
| 14976 | reduced_tx_set, |
| 14977 | )?; |
| 14978 | } |
| 14979 | PARTITION_HORZ_B => { |
| 14980 | // lane-part32 r1: a true 32x16 strip on top + |
| 14981 | // two 16x16 squares below (mirrors HORZ_A |
| 14982 | // with the strip/squares order flipped, same |
| 14983 | // shape as libaom decode_partition's HORZ_B). |
| 14984 | INTRA_HORZ_B_HITS.with(|c| c.set(c.get() + 1)); |
| 14985 | decode_block_rect( |
| 14986 | &mut dec, |
| 14987 | &mut cdfs, |
| 14988 | &mut neighbours, |
| 14989 | at32, |
| 14990 | 32, |
| 14991 | 16, |
| 14992 | &mut y, |
| 14993 | &mut u, |
| 14994 | &mut v, |
| 14995 | enable_filter_intra, |
| 14996 | allow_screen_content_tools, |
| 14997 | base_q_idx, |
| 14998 | tx_select, |
| 14999 | reduced_tx_set, |
| 15000 | )?; |
| 15001 | decode_block( |
| 15002 | &mut dec, |
| 15003 | &mut cdfs, |
| 15004 | &mut neighbours, |
| 15005 | (at32.0 + 1, at32.1), |
| 15006 | SUB, |
| 15007 | TxbSet::Luma16, |
| 15008 | TxbSet::Chroma8, |
| 15009 | TX16, |
| 15010 | TX8, |
| 15011 | (&scan16, &scan8), |
| 15012 | true, |
| 15013 | &mut y, |
| 15014 | &mut u, |
| 15015 | &mut v, |
| 15016 | base_q_idx, |
| 15017 | enable_filter_intra, |
| 15018 | allow_screen_content_tools, |
| 15019 | allow_intrabc, |
| 15020 | &scan32, |
| 15021 | &scan16, |
| 15022 | &scan8, |
| 15023 | &scan4, |
| 15024 | tx_select, |
| 15025 | reduced_tx_set, |
| 15026 | )?; |
| 15027 | decode_block( |
| 15028 | &mut dec, |
| 15029 | &mut cdfs, |
| 15030 | &mut neighbours, |
| 15031 | (at32.0 + 1, at32.1 + 1), |
| 15032 | SUB, |
| 15033 | TxbSet::Luma16, |
| 15034 | TxbSet::Chroma8, |
| 15035 | TX16, |
| 15036 | TX8, |
| 15037 | (&scan16, &scan8), |
| 15038 | true, |
| 15039 | &mut y, |
| 15040 | &mut u, |
| 15041 | &mut v, |
| 15042 | base_q_idx, |
| 15043 | enable_filter_intra, |
| 15044 | allow_screen_content_tools, |
| 15045 | allow_intrabc, |
| 15046 | &scan32, |
| 15047 | &scan16, |
| 15048 | &scan8, |
| 15049 | &scan4, |
| 15050 | tx_select, |
| 15051 | reduced_tx_set, |
| 15052 | )?; |
| 15053 | } |
| 15054 | PARTITION_VERT_A => { |
| 15055 | // lane-part32 r1: mirror of HORZ_A with |
| 15056 | // width/height swapped (TL, BL, right 16x32 |
| 15057 | // strip). |
| 15058 | INTRA_VERT_A_HITS.with(|c| c.set(c.get() + 1)); |
| 15059 | let _vert_ab = crate::encode::Reach::vert_ab_partition(); |
| 15060 | decode_block( |
| 15061 | &mut dec, |
| 15062 | &mut cdfs, |
| 15063 | &mut neighbours, |
| 15064 | at32, |
| 15065 | SUB, |
| 15066 | TxbSet::Luma16, |
| 15067 | TxbSet::Chroma8, |
| 15068 | TX16, |
| 15069 | TX8, |
| 15070 | (&scan16, &scan8), |
| 15071 | true, |
| 15072 | &mut y, |
| 15073 | &mut u, |
| 15074 | &mut v, |
| 15075 | base_q_idx, |
| 15076 | enable_filter_intra, |
| 15077 | allow_screen_content_tools, |
| 15078 | allow_intrabc, |
| 15079 | &scan32, |
| 15080 | &scan16, |
| 15081 | &scan8, |
| 15082 | &scan4, |
| 15083 | tx_select, |
| 15084 | reduced_tx_set, |
| 15085 | )?; |
| 15086 | decode_block( |
| 15087 | &mut dec, |
| 15088 | &mut cdfs, |
| 15089 | &mut neighbours, |
| 15090 | (at32.0 + 1, at32.1), |
| 15091 | SUB, |
| 15092 | TxbSet::Luma16, |
| 15093 | TxbSet::Chroma8, |
| 15094 | TX16, |
| 15095 | TX8, |
| 15096 | (&scan16, &scan8), |
| 15097 | true, |
| 15098 | &mut y, |
| 15099 | &mut u, |
| 15100 | &mut v, |
| 15101 | base_q_idx, |
| 15102 | enable_filter_intra, |
| 15103 | allow_screen_content_tools, |
| 15104 | allow_intrabc, |
| 15105 | &scan32, |
| 15106 | &scan16, |
| 15107 | &scan8, |
| 15108 | &scan4, |
| 15109 | tx_select, |
| 15110 | reduced_tx_set, |
| 15111 | )?; |
| 15112 | decode_block_rect( |
| 15113 | &mut dec, |
| 15114 | &mut cdfs, |
| 15115 | &mut neighbours, |
| 15116 | (at32.0, at32.1 + 1), |
| 15117 | 16, |
| 15118 | 32, |
| 15119 | &mut y, |
| 15120 | &mut u, |
| 15121 | &mut v, |
| 15122 | enable_filter_intra, |
| 15123 | allow_screen_content_tools, |
| 15124 | base_q_idx, |
| 15125 | tx_select, |
| 15126 | reduced_tx_set, |
| 15127 | )?; |
| 15128 | } |
| 15129 | PARTITION_VERT_B => { |
| 15130 | // lane-part32 r1: a true 16x32 strip on the |
| 15131 | // left + two 16x16 squares on the right |
| 15132 | // (libaom decode_partition VERT_B: left |
| 15133 | // strip, TR, BR). |
| 15134 | INTRA_VERT_B_HITS.with(|c| c.set(c.get() + 1)); |
| 15135 | let _vert_ab = crate::encode::Reach::vert_ab_partition(); |
| 15136 | decode_block_rect( |
| 15137 | &mut dec, |
| 15138 | &mut cdfs, |
| 15139 | &mut neighbours, |
| 15140 | at32, |
| 15141 | 16, |
| 15142 | 32, |
| 15143 | &mut y, |
| 15144 | &mut u, |
| 15145 | &mut v, |
| 15146 | enable_filter_intra, |
| 15147 | allow_screen_content_tools, |
| 15148 | base_q_idx, |
| 15149 | tx_select, |
| 15150 | reduced_tx_set, |
| 15151 | )?; |
| 15152 | decode_block( |
| 15153 | &mut dec, |
| 15154 | &mut cdfs, |
| 15155 | &mut neighbours, |
| 15156 | (at32.0, at32.1 + 1), |
| 15157 | SUB, |
| 15158 | TxbSet::Luma16, |
| 15159 | TxbSet::Chroma8, |
| 15160 | TX16, |
| 15161 | TX8, |
| 15162 | (&scan16, &scan8), |
| 15163 | true, |
| 15164 | &mut y, |
| 15165 | &mut u, |
| 15166 | &mut v, |
| 15167 | base_q_idx, |
| 15168 | enable_filter_intra, |
| 15169 | allow_screen_content_tools, |
| 15170 | allow_intrabc, |
| 15171 | &scan32, |
| 15172 | &scan16, |
| 15173 | &scan8, |
| 15174 | &scan4, |
| 15175 | tx_select, |
| 15176 | reduced_tx_set, |
| 15177 | )?; |
| 15178 | decode_block( |
| 15179 | &mut dec, |
| 15180 | &mut cdfs, |
| 15181 | &mut neighbours, |
| 15182 | (at32.0 + 1, at32.1 + 1), |
| 15183 | SUB, |
| 15184 | TxbSet::Luma16, |
| 15185 | TxbSet::Chroma8, |
| 15186 | TX16, |
| 15187 | TX8, |
| 15188 | (&scan16, &scan8), |
| 15189 | true, |
| 15190 | &mut y, |
| 15191 | &mut u, |
| 15192 | &mut v, |
| 15193 | base_q_idx, |
| 15194 | enable_filter_intra, |
| 15195 | allow_screen_content_tools, |
| 15196 | allow_intrabc, |
| 15197 | &scan32, |
| 15198 | &scan16, |
| 15199 | &scan8, |
| 15200 | &scan4, |
| 15201 | tx_select, |
| 15202 | reduced_tx_set, |
| 15203 | )?; |
| 15204 | } |
| 15205 | PARTITION_HORZ_4 | PARTITION_VERT_4 => { |
| 15206 | // lane-tx64x16 r3: the 1:4 pair at the 32x32 |
| 15207 | // level -- four 32x8 (or 8x32) strips in |
| 15208 | // raster order, `quarter_step` = 2 mi, with |
| 15209 | // libaom `decode_partition`'s own `i > 0` |
| 15210 | // frame-edge break. |
| 15211 | let horz = part32 == PARTITION_HORZ_4; |
| 15212 | let base_mi = (at32.0 * (SUB / MI), at32.1 * (SUB / MI)); |
| 15213 | let mut prev: Option<((usize, usize), usize)> = None; |
| 15214 | for i in 0..4 { |
| 15215 | let strip_mi = if horz { |
| 15216 | (base_mi.0 + i * 2, base_mi.1) |
| 15217 | } else { |
| 15218 | (base_mi.0, base_mi.1 + i * 2) |
| 15219 | }; |
| 15220 | if i > 0 |
| 15221 | && (strip_mi.0 >= mi_rows as usize |
| 15222 | || strip_mi.1 >= mi_cols as usize) |
| 15223 | { |
| 15224 | break; |
| 15225 | } |
| 15226 | let (bw, bh) = if horz { (32, 8) } else { (8, 32) }; |
| 15227 | let (mode, _uv) = decode_block_rect4( |
| 15228 | &mut dec, |
| 15229 | &mut cdfs, |
| 15230 | &mut neighbours, |
| 15231 | strip_mi, |
| 15232 | bw, |
| 15233 | bh, |
| 15234 | prev, |
| 15235 | &mut y, |
| 15236 | &mut u, |
| 15237 | &mut v, |
| 15238 | enable_filter_intra, |
| 15239 | allow_screen_content_tools, |
| 15240 | base_q_idx, |
| 15241 | tx_select, |
| 15242 | )?; |
| 15243 | prev = Some((strip_mi, mode)); |
| 15244 | } |
| 15245 | } |
| 15246 | _ => { |
| 15247 | return Err(unsupported(format!( |
| 15248 | "a 32x32 partition type this decoder does not code (value={part32})" |
| 15249 | ))); |
| 15250 | } |
| 15251 | } |
| 15252 | } |
| 15253 | } |
| 15254 | PARTITION_HORZ => { |
| 15255 | // lane-sbpart r2: two true 64x32 strips. |
| 15256 | decode_block_rect64( |
| 15257 | &mut dec, |
| 15258 | &mut cdfs, |
| 15259 | &mut neighbours, |
| 15260 | at, |
| 15261 | 64, |
| 15262 | 32, |
| 15263 | &mut y, |
| 15264 | &mut u, |
| 15265 | &mut v, |
| 15266 | enable_filter_intra, |
| 15267 | allow_screen_content_tools, |
| 15268 | base_q_idx, |
| 15269 | tx_select, |
| 15270 | reduced_tx_set, |
| 15271 | )?; |
| 15272 | if !has_rows { |
| 15273 | bump_edge32(6); |
| 15274 | } |
| 15275 | // lane-golomb r1: at the frame edge only the first half of the |
| 15276 | // strip is coded (libaom `decode_partition` guards the second |
| 15277 | // with the same has_rows test that made this partition non-square). |
| 15278 | if has_rows { |
| 15279 | decode_block_rect64( |
| 15280 | &mut dec, |
| 15281 | &mut cdfs, |
| 15282 | &mut neighbours, |
| 15283 | (at.0 + 2, at.1), |
| 15284 | 64, |
| 15285 | 32, |
| 15286 | &mut y, |
| 15287 | &mut u, |
| 15288 | &mut v, |
| 15289 | enable_filter_intra, |
| 15290 | allow_screen_content_tools, |
| 15291 | base_q_idx, |
| 15292 | tx_select, |
| 15293 | reduced_tx_set, |
| 15294 | )?; |
| 15295 | } |
| 15296 | } |
| 15297 | PARTITION_VERT => { |
| 15298 | // lane-sbpart r2: mirror of PARTITION_HORZ above with |
| 15299 | // width/height swapped. |
| 15300 | decode_block_rect64( |
| 15301 | &mut dec, |
| 15302 | &mut cdfs, |
| 15303 | &mut neighbours, |
| 15304 | at, |
| 15305 | 32, |
| 15306 | 64, |
| 15307 | &mut y, |
| 15308 | &mut u, |
| 15309 | &mut v, |
| 15310 | enable_filter_intra, |
| 15311 | allow_screen_content_tools, |
| 15312 | base_q_idx, |
| 15313 | tx_select, |
| 15314 | reduced_tx_set, |
| 15315 | )?; |
| 15316 | if !has_cols { |
| 15317 | bump_edge32(7); |
| 15318 | } |
| 15319 | // lane-golomb r1: at the frame edge only the first half of the |
| 15320 | // strip is coded (libaom `decode_partition` guards the second |
| 15321 | // with the same has_cols test that made this partition non-square). |
| 15322 | if has_cols { |
| 15323 | decode_block_rect64( |
| 15324 | &mut dec, |
| 15325 | &mut cdfs, |
| 15326 | &mut neighbours, |
| 15327 | (at.0, at.1 + 2), |
| 15328 | 32, |
| 15329 | 64, |
| 15330 | &mut y, |
| 15331 | &mut u, |
| 15332 | &mut v, |
| 15333 | enable_filter_intra, |
| 15334 | allow_screen_content_tools, |
| 15335 | base_q_idx, |
| 15336 | tx_select, |
| 15337 | reduced_tx_set, |
| 15338 | )?; |
| 15339 | } |
| 15340 | } |
| 15341 | PARTITION_HORZ_A | PARTITION_HORZ_B | PARTITION_VERT_A | PARTITION_VERT_B => { |
| 15342 | // lane-part32 r4: the four superblock-level AB arms, each |
| 15343 | // two 32x32 squares plus one 64x32/32x64 strip, in |
| 15344 | // libaom's own `decode_partition` order (decodeframe.c: |
| 15345 | // HORZ_A = TL, TR, bottom strip; HORZ_B = top strip, BL, |
| 15346 | // BR; VERT_A = TL, BL, right strip; VERT_B = left strip, |
| 15347 | // TR, BR). The pieces are exactly the ones already proven |
| 15348 | // by the `PARTITION_NONE`-under-SPLIT (32x32 square) and |
| 15349 | // `PARTITION_HORZ`/`VERT` (rect64 strip) arms above. |
| 15350 | SB_AB_HITS.with(|c| { |
| 15351 | let mut h = c.get(); |
| 15352 | h[part - PARTITION_HORZ_A] += 1; |
| 15353 | c.set(h); |
| 15354 | }); |
| 15355 | // lane-part32 r5: only the two VERTICAL arms reorder the |
| 15356 | // square sub-blocks (TL, BL, TR, BR), so only they switch |
| 15357 | // libaom's `has_tr`/`has_bl` tables. |
| 15358 | let _vert_ab = (part == PARTITION_VERT_A || part == PARTITION_VERT_B) |
| 15359 | .then(crate::encode::Reach::vert_ab_partition); |
| 15360 | macro_rules! square32 { |
| 15361 | ($at:expr) => { |
| 15362 | decode_block( |
| 15363 | &mut dec, |
| 15364 | &mut cdfs, |
| 15365 | &mut neighbours, |
| 15366 | $at, |
| 15367 | BLOCK, |
| 15368 | TxbSet::Luma32, |
| 15369 | TxbSet::Chroma16, |
| 15370 | TX32, |
| 15371 | TX16, |
| 15372 | (&scan32, &scan16), |
| 15373 | true, |
| 15374 | &mut y, |
| 15375 | &mut u, |
| 15376 | &mut v, |
| 15377 | base_q_idx, |
| 15378 | enable_filter_intra, |
| 15379 | allow_screen_content_tools, |
| 15380 | allow_intrabc, |
| 15381 | &scan32, |
| 15382 | &scan16, |
| 15383 | &scan8, |
| 15384 | &scan4, |
| 15385 | tx_select, |
| 15386 | reduced_tx_set, |
| 15387 | )? |
| 15388 | }; |
| 15389 | } |
| 15390 | macro_rules! strip64 { |
| 15391 | ($at:expr, $bw:expr, $bh:expr) => { |
| 15392 | decode_block_rect64( |
| 15393 | &mut dec, |
| 15394 | &mut cdfs, |
| 15395 | &mut neighbours, |
| 15396 | $at, |
| 15397 | $bw, |
| 15398 | $bh, |
| 15399 | &mut y, |
| 15400 | &mut u, |
| 15401 | &mut v, |
| 15402 | enable_filter_intra, |
| 15403 | allow_screen_content_tools, |
| 15404 | base_q_idx, |
| 15405 | tx_select, |
| 15406 | reduced_tx_set, |
| 15407 | )? |
| 15408 | }; |
| 15409 | } |
| 15410 | let (br, bc) = (at.0 + 2, at.1 + 2); |
| 15411 | match part { |
| 15412 | PARTITION_HORZ_A => { |
| 15413 | square32!(at); |
| 15414 | square32!((at.0, bc)); |
| 15415 | strip64!((br, at.1), 64, 32); |
| 15416 | } |
| 15417 | PARTITION_HORZ_B => { |
| 15418 | strip64!(at, 64, 32); |
| 15419 | square32!((br, at.1)); |
| 15420 | square32!((br, bc)); |
| 15421 | } |
| 15422 | PARTITION_VERT_A => { |
| 15423 | square32!(at); |
| 15424 | square32!((br, at.1)); |
| 15425 | strip64!((at.0, bc), 32, 64); |
| 15426 | } |
| 15427 | _ => { |
| 15428 | strip64!(at, 32, 64); |
| 15429 | square32!((at.0, bc)); |
| 15430 | square32!((br, bc)); |
| 15431 | } |
| 15432 | } |
| 15433 | } |
| 15434 | PARTITION_HORZ_4 | PARTITION_VERT_4 => { |
| 15435 | // lane-tx64x16: the 1:4 pair at 64x64 -- four 64x16 (or |
| 15436 | // 16x64) strips in raster order. `decode_partition` |
| 15437 | // (`decodeframe.c`) steps by `quarter_step = mi_size / 4` |
| 15438 | // (4 mi = 16 px = one SUB unit here) and BREAKS at the |
| 15439 | // frame edge for `i > 0`, so a partial superblock codes |
| 15440 | // only the strips whose origin is inside the frame. |
| 15441 | let horz = part == PARTITION_HORZ_4; |
| 15442 | for i in 0..4 { |
| 15443 | let step_mi = i * (SB_MI as usize / 4); |
| 15444 | let (this_r_mi, this_c_mi) = if horz { |
| 15445 | (sb_r as usize * SB_MI as usize + step_mi, sb_c as usize * SB_MI as usize) |
| 15446 | } else { |
| 15447 | (sb_r as usize * SB_MI as usize, sb_c as usize * SB_MI as usize + step_mi) |
| 15448 | }; |
| 15449 | if i > 0 |
| 15450 | && (this_r_mi >= mi_rows as usize || this_c_mi >= mi_cols as usize) |
| 15451 | { |
| 15452 | break; |
| 15453 | } |
| 15454 | let strip_at = if horz { (at.0 + i, at.1) } else { (at.0, at.1 + i) }; |
| 15455 | let (bw, bh) = if horz { (64, 16) } else { (16, 64) }; |
| 15456 | decode_block_rect64( |
| 15457 | &mut dec, |
| 15458 | &mut cdfs, |
| 15459 | &mut neighbours, |
| 15460 | strip_at, |
| 15461 | bw, |
| 15462 | bh, |
| 15463 | &mut y, |
| 15464 | &mut u, |
| 15465 | &mut v, |
| 15466 | enable_filter_intra, |
| 15467 | allow_screen_content_tools, |
| 15468 | base_q_idx, |
| 15469 | tx_select, |
| 15470 | reduced_tx_set, |
| 15471 | )?; |
| 15472 | } |
| 15473 | } |
| 15474 | _ => { |
| 15475 | return Err(unsupported( |
| 15476 | "a superblock-level partition value outside PARTITION_NONE..PARTITION_VERT_4", |
| 15477 | )); |
| 15478 | } |
| 15479 | } |
| 15480 | } |
| 15481 | } |
| 15482 | |
| 15483 | if tile_num == tile_info.context_update_tile_id { |
| 15484 | result_cdfs = cdfs; |
| 15485 | } |
| 15486 | } |
| 15487 | |
| 15488 | // lane-comppin r4: pre-loop-filter decode-order dump, matching aomdec's |
| 15489 | // own EC_AV1_PREFILT_DUMP shape (decodeframe.c ~5451) -- diffs against |
| 15490 | // that isolate whether a decode-order frame's mismatch already exists |
| 15491 | // in reconstruction (this dump) vs is introduced by the loop filter |
| 15492 | // (EC_AV1_DECODE_ORDER_DUMP's post-filter dump). |
| 15493 | dump_stage16("EC_AV1_PREFILT_DUMP16", &y, &u, &v, frame_width as usize, frame_height as usize); |
| 15494 | if let Ok(path) = std::env::var("EC_AV1_PREFILT_DUMP") { |
| 15495 | use std::io::Write; |
| 15496 | let idx = PREFILT_PICTURE_IDX.fetch_add(1, std::sync::atomic::Ordering::SeqCst); |
| 15497 | if let Ok(mut f) = std::fs::File::create(format!("{path}.f{idx}")) { |
| 15498 | let narrow = |p: &PlaneBuf| -> Vec<u8> { p.data.iter().map(|&s| s as u8).collect() }; |
| 15499 | let _ = f.write_all(&narrow(&y)); |
| 15500 | let _ = f.write_all(&narrow(&u)); |
| 15501 | let _ = f.write_all(&narrow(&v)); |
| 15502 | } |
| 15503 | } else { |
| 15504 | PREFILT_PICTURE_IDX.fetch_add(1, std::sync::atomic::Ordering::SeqCst); |
| 15505 | } |
| 15506 | if let Ok(path) = std::env::var("EC_AV1_PREFILT_WIDE_DUMP") { |
| 15507 | use std::io::Write; |
| 15508 | static IDX2: std::sync::atomic::AtomicUsize = std::sync::atomic::AtomicUsize::new(0); |
| 15509 | let idx = IDX2.fetch_add(1, std::sync::atomic::Ordering::SeqCst); |
| 15510 | let crop_wide = |plane: &PlaneBuf| -> Vec<u8> { |
| 15511 | let mut out = Vec::with_capacity(plane.true_width * plane.true_height); |
| 15512 | for row in 0..plane.true_height { |
| 15513 | out.extend(plane.data[row * plane.width..][..plane.true_width].iter().map(|&s| s as u8)); |
| 15514 | } |
| 15515 | out |
| 15516 | }; |
| 15517 | if let Ok(mut f) = std::fs::File::create(format!("{path}.f{idx}")) { |
| 15518 | let _ = f.write_all(&crop_wide(&y)); |
| 15519 | let _ = f.write_all(&crop_wide(&u)); |
| 15520 | let _ = f.write_all(&crop_wide(&v)); |
| 15521 | } |
| 15522 | } |
| 15523 | apply_deblock( |
| 15524 | &mut y, |
| 15525 | &mut u, |
| 15526 | &mut v, |
| 15527 | loop_filter, |
| 15528 | &neighbours, |
| 15529 | frame_width as usize, |
| 15530 | frame_height as usize, |
| 15531 | ); |
| 15532 | let (deblocked_y, deblocked_u, deblocked_v) = (y.clone(), u.clone(), v.clone()); |
| 15533 | // lane-tiny r4: post-deblock / post-CDEF dumps mirroring aomdec's own |
| 15534 | // EC_AV1_POSTDEBLOCK_DUMP (decodeframe.c ~5404) -- with the pre-filter |
| 15535 | // dump above these bisect WHICH filter stage introduced a mismatch. |
| 15536 | dump_stage("EC_AV1_POSTDEBLOCK_DUMP", &y, &u, &v); |
| 15537 | dump_stage16("EC_AV1_POSTDEBLOCK_DUMP16", &y, &u, &v, frame_width as usize, frame_height as usize); |
| 15538 | apply_cdef( |
| 15539 | &mut y, |
| 15540 | &mut u, |
| 15541 | &mut v, |
| 15542 | cdef, |
| 15543 | &neighbours, |
| 15544 | (frame_width as usize, frame_height as usize), |
| 15545 | ); |
| 15546 | dump_stage("EC_AV1_POSTCDEF_DUMP", &y, &u, &v); |
| 15547 | dump_stage16("EC_AV1_POSTCDEF_DUMP16", &y, &u, &v, frame_width as usize, frame_height as usize); |
| 15548 | apply_loop_restoration( |
| 15549 | &mut y, &mut u, &mut v, &deblocked_y, &deblocked_u, &deblocked_v, lr, &lr_grid, |
| 15550 | (frame_width as usize, frame_height as usize), |
| 15551 | ); |
| 15552 | |
| 15553 | let (fw, fh) = (frame_width as usize, frame_height as usize); |
| 15554 | if fw == width && fh == height { |
| 15555 | LAST_FRAME_WIDE_MARGIN.with(|m| *m.borrow_mut() = None); |
| 15556 | return Ok(( |
| 15557 | Picture { |
| 15558 | width, |
| 15559 | height, |
| 15560 | y: y.data, |
| 15561 | u: u.data, |
| 15562 | v: v.data, |
| 15563 | }, |
| 15564 | result_cdfs, |
| 15565 | )); |
| 15566 | } |
| 15567 | let crop = |plane: &PlaneBuf, w: usize, h: usize| -> Vec<u16> { |
| 15568 | let mut out = Vec::with_capacity(w * h); |
| 15569 | for row in 0..h { |
| 15570 | out.extend(plane.data[row * plane.width..][..w].iter().copied()); |
| 15571 | } |
| 15572 | out |
| 15573 | }; |
| 15574 | // lane-superres r3: `true_width`/`true_height` (`mi_cols`/`mi_rows` * 4) |
| 15575 | // is the real reconstructed extent -- columns `[fw, true_width)` hold |
| 15576 | // genuine decoded samples (the last coding block straddles the frame |
| 15577 | // edge whenever `frame_width` isn't 8-sample aligned), not padding. |
| 15578 | // libaom's superres upscale runs on that buffer's own border-extended |
| 15579 | // margin, so it reads those real columns, not a synthetic replicate of |
| 15580 | // column `fw - 1`. Stash them for `stream.rs`'s superres path (a real |
| 15581 | // aomenc 43->64 stream pinned column-by-column against libaom's own |
| 15582 | // `av1_convolve_horiz_rs_c` via `scripts/superres-pin-harness.c` -- |
| 15583 | // `row6-realedgeval` proved the replicate-of-`fw-1` padding this |
| 15584 | // decoder used before was off by 1 at the columns the tap window |
| 15585 | // reaches into that real margin; `None` when there is no margin (this |
| 15586 | // frame's `true_width`/`true_height` already equal `fw`/`fh`, e.g. a |
| 15587 | // non-superres frame whose width just isn't 32-block aligned). |
| 15588 | LAST_FRAME_WIDE_MARGIN.with(|m| { |
| 15589 | *m.borrow_mut() = if true_width > fw || true_height > fh { |
| 15590 | let yc = crop(&y, true_width, true_height); |
| 15591 | if let Ok(path) = std::env::var("EC_AV1_MARGIN_DUMP") { |
| 15592 | use std::io::Write; |
| 15593 | static IDX: std::sync::atomic::AtomicUsize = std::sync::atomic::AtomicUsize::new(0); |
| 15594 | let idx = IDX.fetch_add(1, std::sync::atomic::Ordering::SeqCst); |
| 15595 | if let Ok(mut f) = std::fs::File::create(format!("{path}.f{idx}")) { |
| 15596 | // corner-cut: this diagnostic dump stays 8-bit-narrowed |
| 15597 | // regardless of the stream's real bit depth (debug-only, |
| 15598 | // never read by a gate); widen if a >8-bit margin dump |
| 15599 | // is ever needed. |
| 15600 | let _ = f.write_all(&yc.iter().map(|&s| s as u8).collect::<Vec<u8>>()); |
| 15601 | } |
| 15602 | } |
| 15603 | Some(Picture { |
| 15604 | width: true_width, |
| 15605 | height: true_height, |
| 15606 | y: yc, |
| 15607 | u: crop(&u, true_width.div_ceil(2), true_height.div_ceil(2)), |
| 15608 | v: crop(&v, true_width.div_ceil(2), true_height.div_ceil(2)), |
| 15609 | }) |
| 15610 | } else { |
| 15611 | None |
| 15612 | } |
| 15613 | }); |
| 15614 | Ok(( |
| 15615 | Picture { |
| 15616 | width: fw, |
| 15617 | height: fh, |
| 15618 | y: crop(&y, fw, fh), |
| 15619 | u: crop(&u, fw.div_ceil(2), fh.div_ceil(2)), |
| 15620 | v: crop(&v, fw.div_ceil(2), fh.div_ceil(2)), |
| 15621 | }, |
| 15622 | result_cdfs, |
| 15623 | )) |
| 15624 | } |
| 15625 | |
| 15626 | /// `av1_get_pred_context_switchable_interp`'s context for `interp_filter[dir]` |
| 15627 | /// -- `above`/`left` are the neighbour's own resolved index for this |
| 15628 | /// direction (0..=2, or `3` = no info, spec `SWITCHABLE_FILTERS`). |
| 15629 | /// `is_compound` folds in libaom `av1_get_pred_context_switchable_interp`'s |
| 15630 | /// own `ctx_offset` term (`INTER_FILTER_COMP_OFFSET` = 4, added when *this* |
| 15631 | /// block itself has a second reference) -- lane-av1idx: a stale "this |
| 15632 | /// decoder never codes a compound reference" assumption baked this to |
| 15633 | /// always-0, which desynced the CDF context bucket (not the decoded value |
| 15634 | /// outright, hence small pixel deltas rather than a hard desync) the moment |
| 15635 | /// lane-av1blend unmasked plain-average compound blocks on a Switchable- |
| 15636 | /// filter frame. |
| 15637 | fn switchable_interp_ctx(above: u8, left: u8, dir: usize, is_compound: bool) -> usize { |
| 15638 | let base = dir * 8 + if is_compound { 4 } else { 0 }; |
| 15639 | let term = if left == above { |
| 15640 | left |
| 15641 | } else if left == 3 { |
| 15642 | above |
| 15643 | } else if above == 3 { |
| 15644 | left |
| 15645 | } else { |
| 15646 | 3 |
| 15647 | }; |
| 15648 | base + term as usize |
| 15649 | } |
| 15650 | |
| 15651 | /// Resolves the interpolation filter kernel(s) an inter block's motion |
| 15652 | /// compensation reads (spec 5.11.10/5.11.20): a fixed-filter frame |
| 15653 | /// (`interp_fixed = Some`) never reads a symbol; a `Switchable` frame |
| 15654 | /// (`interp_fixed = None`) reads `interp_filter[0]` and, when |
| 15655 | /// `enable_dual_filter`, a second `interp_filter[1]` -- unless |
| 15656 | /// `force_regular` (this block's own `needs_interp_filter() == 0` case: a |
| 15657 | /// `GLOBALMV` block, whose global motion is always `IDENTITY` in this |
| 15658 | /// decoder's scope, spec 5.11.10's `large && YMode == GLOBALMV` branch), |
| 15659 | /// which forces `EIGHTTAP` without reading anything. |
| 15660 | #[allow(clippy::too_many_arguments)] |
| 15661 | fn resolve_interp_filter( |
| 15662 | dec: &mut SymbolDecoder, |
| 15663 | cdfs: &mut Cdfs, |
| 15664 | interp_fixed: Option<mc::InterpFilterKind>, |
| 15665 | enable_dual_filter: bool, |
| 15666 | force_regular: bool, |
| 15667 | above: [u8; 2], |
| 15668 | left: [u8; 2], |
| 15669 | is_compound: bool, |
| 15670 | ) -> (mc::InterpFilterKind, mc::InterpFilterKind, [u8; 2]) { |
| 15671 | if let Some(kind) = interp_fixed { |
| 15672 | return (kind, kind, [3, 3]); |
| 15673 | } |
| 15674 | if force_regular { |
| 15675 | return ( |
| 15676 | mc::InterpFilterKind::Regular, |
| 15677 | mc::InterpFilterKind::Regular, |
| 15678 | [0, 0], |
| 15679 | ); |
| 15680 | } |
| 15681 | // libaom `av1_extract_interp_filter`/`read_mb_interp_filter`: the |
| 15682 | // symbol read at `dir=0` becomes the block's *vertical* (`y_filter`) |
| 15683 | // kernel, `dir=1` the *horizontal* (`x_filter`) one -- `dir` is not |
| 15684 | // "horizontal then vertical" despite `predict_with_filters`' own |
| 15685 | // `h_kind`/`v_kind` argument order, which is why this function returns |
| 15686 | // `(h, v, ..)` but reads `dir0` (`v`) before `dir1` (`h`). |
| 15687 | let ctx0 = switchable_interp_ctx(above[0], left[0], 0, is_compound); |
| 15688 | if std::env::var_os("EC_AV1_IFDBG").is_some() { |
| 15689 | eprintln!( |
| 15690 | "IFDBG dir=0 ctx={ctx0} above={above:?} left={left:?} cdf={:?}", |
| 15691 | &cdfs.switchable_interp[ctx0][..3] |
| 15692 | ); |
| 15693 | } |
| 15694 | let sym0 = dec.symbol(&mut cdfs.switchable_interp[ctx0]) as u8; |
| 15695 | let sym1 = if enable_dual_filter { |
| 15696 | let ctx1 = switchable_interp_ctx(above[1], left[1], 1, is_compound); |
| 15697 | if std::env::var_os("EC_AV1_IFDBG").is_some() { |
| 15698 | eprintln!( |
| 15699 | "IFDBG dir=1 ctx={ctx1} above={above:?} left={left:?} cdf={:?} sym0={sym0}", |
| 15700 | &cdfs.switchable_interp[ctx1][..3] |
| 15701 | ); |
| 15702 | } |
| 15703 | dec.symbol(&mut cdfs.switchable_interp[ctx1]) as u8 |
| 15704 | } else { |
| 15705 | sym0 |
| 15706 | }; |
| 15707 | ( |
| 15708 | mc::InterpFilterKind::from_switchable_symbol(sym1 as usize), |
| 15709 | mc::InterpFilterKind::from_switchable_symbol(sym0 as usize), |
| 15710 | [sym0, sym1], |
| 15711 | ) |
| 15712 | } |
| 15713 | |
| 15714 | /// The `is_inter` context (spec 5.11.16 via `av1_get_intra_inter_context`), |
| 15715 | /// duplicating [`crate::tile`]'s private copy of the same rule. |
| 15716 | pub(crate) fn intra_inter_ctx( |
| 15717 | has_above: bool, |
| 15718 | has_left: bool, |
| 15719 | above_inter: bool, |
| 15720 | left_inter: bool, |
| 15721 | ) -> usize { |
| 15722 | match (has_above, has_left) { |
| 15723 | (true, true) => { |
| 15724 | let (above_intra, left_intra) = (!above_inter, !left_inter); |
| 15725 | if above_intra && left_intra { |
| 15726 | 3 |
| 15727 | } else { |
| 15728 | usize::from(above_intra || left_intra) |
| 15729 | } |
| 15730 | } |
| 15731 | (true, false) => 2 * usize::from(!above_inter), |
| 15732 | (false, true) => 2 * usize::from(!left_inter), |
| 15733 | (false, false) => 0, |
| 15734 | } |
| 15735 | } |
| 15736 | |
| 15737 | /// `CLASS0_SIZE << (class + 2)`, duplicating `crate::tile`'s private |
| 15738 | /// `mv_class_base` (spec 3): the magnitude an `MV_CLASS_n` component's own |
| 15739 | /// bits start counting from. |
| 15740 | fn mv_class_base(class: usize) -> i32 { |
| 15741 | if class == 0 { 0 } else { 2i32 << (class + 2) } |
| 15742 | } |
| 15743 | |
| 15744 | /// One motion vector component's non-zero diff (spec 5.11.32 |
| 15745 | /// `read_mv_component`), the inverse of [`crate::tile`]'s private |
| 15746 | /// `write_mv_component`. |
| 15747 | fn read_mv_component( |
| 15748 | dec: &mut SymbolDecoder, |
| 15749 | c: &mut MvComponentCdfs, |
| 15750 | allow_high_precision_mv: bool, |
| 15751 | force_integer_mv: bool, |
| 15752 | ) -> i32 { |
| 15753 | let sign = dec.symbol(&mut c.sign); |
| 15754 | let class = dec.symbol(&mut c.class); |
| 15755 | let local = if class == 0 { |
| 15756 | let bit = dec.symbol(&mut c.class0_bit); |
| 15757 | // spec 5.11.32 `mv_class0_fr`: not coded at all when the frame |
| 15758 | // forces integer motion vectors -- implicitly `3` (the top |
| 15759 | // fractional value) rather than read, else every symbol after it |
| 15760 | // in the tile desyncs by one read. |
| 15761 | let fr = if force_integer_mv { |
| 15762 | 3 |
| 15763 | } else { |
| 15764 | dec.symbol(&mut c.class0_fr[bit]) |
| 15765 | }; |
| 15766 | // spec 5.11.32 `mv_class0_hp`: read only when the frame allows |
| 15767 | // high-precision motion vectors, else implicitly 1 (the low bit |
| 15768 | // stays at half-pel precision) -- this crate's own writer always |
| 15769 | // leaves the flag off, but a foreign stream can set it per frame. |
| 15770 | let hp = if allow_high_precision_mv { |
| 15771 | dec.symbol(&mut c.class0_hp) |
| 15772 | } else { |
| 15773 | 1 |
| 15774 | }; |
| 15775 | (bit << 3) | (fr << 1) | hp |
| 15776 | } else { |
| 15777 | let mut d = 0; |
| 15778 | for i in 0..class { |
| 15779 | d |= dec.symbol(&mut c.bit[i]) << i; |
| 15780 | } |
| 15781 | // spec 5.11.32 `mv_fr`: same force_integer_mv carve-out as |
| 15782 | // `mv_class0_fr` above. |
| 15783 | let fr = if force_integer_mv { 3 } else { dec.symbol(&mut c.fr) }; |
| 15784 | let hp = if allow_high_precision_mv { |
| 15785 | dec.symbol(&mut c.hp) |
| 15786 | } else { |
| 15787 | 1 |
| 15788 | }; |
| 15789 | (d << 3) | (fr << 1) | hp |
| 15790 | }; |
| 15791 | let mag = mv_class_base(class) + local as i32 + 1; |
| 15792 | if sign == 1 { -mag } else { mag } |
| 15793 | } |
| 15794 | |
| 15795 | /// A motion vector coded as a residual against `pred` (spec 5.11.32 |
| 15796 | /// `read_mv`), the inverse of [`crate::tile`]'s private `write_mv`. |
| 15797 | fn read_mv( |
| 15798 | dec: &mut SymbolDecoder, |
| 15799 | mv_comp: &mut [MvComponentCdfs; 2], |
| 15800 | mv_joint: &mut [u16; 5], |
| 15801 | pred: (i32, i32), |
| 15802 | allow_high_precision_mv: bool, |
| 15803 | force_integer_mv: bool, |
| 15804 | ) -> (i32, i32) { |
| 15805 | let joint = dec.symbol(mv_joint); |
| 15806 | let mut diff = (0, 0); |
| 15807 | if joint == 2 || joint == 3 { |
| 15808 | diff.0 = read_mv_component(dec, &mut mv_comp[0], allow_high_precision_mv, force_integer_mv); |
| 15809 | } |
| 15810 | if joint == 1 || joint == 3 { |
| 15811 | diff.1 = read_mv_component(dec, &mut mv_comp[1], allow_high_precision_mv, force_integer_mv); |
| 15812 | } |
| 15813 | (pred.0 + diff.0, pred.1 + diff.1) |
| 15814 | } |
| 15815 | |
| 15816 | /// One intrabc block's block vector (spec 5.11.13 `read_intrabc_info` -> |
| 15817 | /// `assign_mv` with `use_intrabc`, libaom `assign_dv`/`av1_find_ref_dv`). |
| 15818 | /// |
| 15819 | /// The predictor is the ordinary MV stack built against `INTRA_FRAME` |
| 15820 | /// (`av1_find_mv_refs(..., INTRA_FRAME, ...)`): `nearestmv`, or `nearmv` |
| 15821 | /// when that is zero, or -- when both are -- the fixed fallback DV one |
| 15822 | /// superblock up, or `INTRABC_DELAY_PIXELS` (256) plus one superblock to the |
| 15823 | /// left when this is the tile's first superblock row. The predictor is then |
| 15824 | /// rounded to full pel, and the difference reads off the *dv* nmv context |
| 15825 | /// (libaom `ndvc`) at full-pel precision (`MV_SUBPEL_NONE`: no `fr`/`hp` |
| 15826 | /// symbols, exactly this decoder's `force_integer_mv` carve-out). No DRL |
| 15827 | /// symbol is coded for a DV, so none of this can move the bitstream |
| 15828 | /// position -- only the reconstructed vector. |
| 15829 | fn read_intrabc_dv( |
| 15830 | dec: &mut SymbolDecoder, |
| 15831 | cdfs: &mut Cdfs, |
| 15832 | mi_r: usize, |
| 15833 | mi_c: usize, |
| 15834 | side: usize, |
| 15835 | ) -> (i32, i32) { |
| 15836 | const INTRABC_DELAY_PIXELS: i32 = 256; |
| 15837 | let n4 = side / MI; |
| 15838 | let stack_pred = INTRABC_MI_GRID.with(|g| { |
| 15839 | let g = g.borrow(); |
| 15840 | g.as_ref().map(|(grid, mi_cols, mi_rows)| { |
| 15841 | let stack = crate::mvstack::find_mv_stack( |
| 15842 | grid, mi_r, mi_c, n4, n4, 0, /* INTRA_FRAME */ |
| 15843 | *mi_cols, *mi_rows, |
| 15844 | ); |
| 15845 | if stack.nearest_mv == (0, 0) { |
| 15846 | stack.near_mv |
| 15847 | } else { |
| 15848 | stack.nearest_mv |
| 15849 | } |
| 15850 | }) |
| 15851 | }); |
| 15852 | let mut pred = stack_pred.unwrap_or((0, 0)); |
| 15853 | if pred == (0, 0) { |
| 15854 | // `av1_find_ref_dv` (mvref_common.c). Tile row start is 0 here (the |
| 15855 | // single-tile-row case every intrabc gate stream is). |
| 15856 | let sb_px = SB_MI as i32 * MI as i32; |
| 15857 | pred = if mi_r < SB_MI as usize { |
| 15858 | (0, -(sb_px + INTRABC_DELAY_PIXELS) * 8) |
| 15859 | } else { |
| 15860 | (-sb_px * 8, 0) |
| 15861 | }; |
| 15862 | } |
| 15863 | // "Ref DV should not have sub-pel" (`assign_dv`): floor to full pel. |
| 15864 | let pred = ((pred.0 >> 3) * 8, (pred.1 >> 3) * 8); |
| 15865 | read_mv(dec, &mut cdfs.dv_comp, &mut cdfs.dv_joint, pred, false, true) |
| 15866 | } |
| 15867 | |
| 15868 | /// A 1/8-pel motion vector component converted to the 1/16-pel offset |
| 15869 | /// [`mc::predict`] takes, duplicating [`crate::encode`]'s private `mv_to_q4` |
| 15870 | /// (its own doc comment explains the `*2`/`*1` luma/chroma split). |
| 15871 | fn mv_to_q4(pos: usize, mv_component: i32, luma: bool) -> i32 { |
| 15872 | (pos as i32) * 16 + mv_component * if luma { 2 } else { 1 } |
| 15873 | } |
| 15874 | |
| 15875 | impl PlaneBuf { |
| 15876 | /// Adds `residual` onto an already-computed `prediction` (motion |
| 15877 | /// compensation's output, rather than [`predict`]'s intra one), writing |
| 15878 | /// the clamped reconstruction into the plane at `(x, y)` -- the inter |
| 15879 | /// counterpart of [`PlaneBuf::reconstruct`]. |
| 15880 | fn reconstruct_mc( |
| 15881 | &mut self, |
| 15882 | x: usize, |
| 15883 | y: usize, |
| 15884 | side: usize, |
| 15885 | prediction: &[u16], |
| 15886 | residual: &[i32], |
| 15887 | ) { |
| 15888 | self.reconstruct_mc_rect(x, y, side, side, side, prediction, residual); |
| 15889 | } |
| 15890 | |
| 15891 | /// [`Self::reconstruct_mc`], writing only a `w`x`h` sub-rectangle of the |
| 15892 | /// `side`x`side` `prediction`/`residual` buffers (which stay `side`-square |
| 15893 | /// for stride/indexing) -- lane-partitions r1: a true rectangular HORZ/ |
| 15894 | /// VERT strip is predicted at its enclosing square `side` (matching |
| 15895 | /// `HORZ_B`'s already-accepted square-context corner-cut, lane-warp r5) |
| 15896 | /// but only its own true `w`x`h` footprint is committed to the plane, so |
| 15897 | /// two strips sharing a square's coordinate origin never clobber each |
| 15898 | /// other's real pixels. |
| 15899 | fn reconstruct_mc_rect( |
| 15900 | &mut self, |
| 15901 | x: usize, |
| 15902 | y: usize, |
| 15903 | side: usize, |
| 15904 | w: usize, |
| 15905 | h: usize, |
| 15906 | prediction: &[u16], |
| 15907 | residual: &[i32], |
| 15908 | ) { |
| 15909 | for row in 0..h { |
| 15910 | for col in 0..w { |
| 15911 | let sample = (i32::from(prediction[row * side + col]) + residual[row * side + col]) |
| 15912 | .clamp(0, crate::decode::sample_max()) as u16; |
| 15913 | self.data[(y + row) * self.width + x + col] = sample; |
| 15914 | } |
| 15915 | } |
| 15916 | } |
| 15917 | } |
| 15918 | |
| 15919 | /// Reads one inter-coded plane's transform block and reconstructs it by |
| 15920 | /// adding its residual onto a motion-compensated `prediction` -- the inter |
| 15921 | /// counterpart of [`read_plane`] (every inter transform this decoder reads is |
| 15922 | /// `side`-square with no oversized-64 case, so there is no padded-grid |
| 15923 | /// branch to mirror). |
| 15924 | #[allow(clippy::too_many_arguments)] |
| 15925 | fn read_inter_plane( |
| 15926 | dec: &mut SymbolDecoder, |
| 15927 | cdfs: &mut Cdfs, |
| 15928 | set: TxbSet, |
| 15929 | scan: &[u16], |
| 15930 | plane_idx: usize, |
| 15931 | around: (bool, bool, i32), |
| 15932 | tx_mode: usize, |
| 15933 | plane: &mut PlaneBuf, |
| 15934 | x: usize, |
| 15935 | y: usize, |
| 15936 | side: usize, |
| 15937 | base_q_idx: u8, |
| 15938 | prediction: &[u16], |
| 15939 | // lane-chromau: this block's own luma transform's *actual coded* |
| 15940 | // `tx_type` -- `av1_get_tx_type` (libaom `blockd.h`): an inter block's |
| 15941 | // chroma plane never codes its own `tx_type` symbol, but (unlike an |
| 15942 | // intra block, which falls back to `Intra_Mode_To_Tx_Type`) it inherits |
| 15943 | // the colocated luma transform's coded type, narrowed to the extended |
| 15944 | // set the CHROMA size selects ([`reduce_inherited_chroma_tx_type`]). |
| 15945 | // `None` for the luma call itself |
| 15946 | // (`plane_idx == 0`), where the `TxbSet`'s own symbol (or the true |
| 15947 | // `DCT_DCT` default at 32-point+) already resolves it without help. |
| 15948 | inherited_luma_tx_type: Option<TxType>, |
| 15949 | // lane-txselect: the luma `txb_skip_ctx` of a transform unit smaller than |
| 15950 | // its own block (spec `get_txb_ctx_general`'s `plane_bsize != tx_size` |
| 15951 | // branch, [`Neighbours::luma_skip_ctx`]) -- `None` for the whole-block |
| 15952 | // transform every non-var-tx caller reads, whose plane-0 context is the |
| 15953 | // lone-transform-unit 0. |
| 15954 | luma_skip_ctx: Option<usize>, |
| 15955 | ) -> Result<(Vec<i32>, TxType)> { |
| 15956 | let skip_ctx = if plane_idx == 0 { |
| 15957 | luma_skip_ctx.unwrap_or(0) |
| 15958 | } else { |
| 15959 | usize::from(around.0) + usize::from(around.1) |
| 15960 | }; |
| 15961 | let mut coding = cdfs.txb(set, tx_mode); |
| 15962 | let default_tx_type = match inherited_luma_tx_type { |
| 15963 | Some(t) => reduce_inherited_chroma_tx_type(t, side, side), |
| 15964 | None => TxType::DctDct, |
| 15965 | }; |
| 15966 | let (grid, tx_type) = read_coeffs( |
| 15967 | dec, |
| 15968 | &mut coding, |
| 15969 | scan, |
| 15970 | skip_ctx, |
| 15971 | dc_sign_ctx(around.2), |
| 15972 | default_tx_type, |
| 15973 | None, |
| 15974 | )?; |
| 15975 | note_chroma_class1(plane_idx, tx_type); |
| 15976 | // lane-inter8 r1: a 64-point transform covers the whole 64x64 area but |
| 15977 | // codes only its top-left 32x32 of frequencies (spec 5.11.40) -- the |
| 15978 | // caller hands the 32-point `scan` there, and the coded corner goes into |
| 15979 | // the top-left of the true `side`-sized grid before dequantization |
| 15980 | // (dqDenom is keyed by the true size, not the corner's), mirroring |
| 15981 | // [`read_plane`]'s own `tx_side != side` branch. Every other transform |
| 15982 | // this decoder codes has `tx_side == side`, so this is inert below 64. |
| 15983 | let tx_side = side.min(32); |
| 15984 | let grid = if tx_side == side { |
| 15985 | grid |
| 15986 | } else { |
| 15987 | let mut full = vec![0i32; side * side]; |
| 15988 | for row in 0..tx_side { |
| 15989 | full[row * side..][..tx_side].copy_from_slice(&grid[row * tx_side..][..tx_side]); |
| 15990 | } |
| 15991 | full |
| 15992 | }; |
| 15993 | let (dc_delta, ac_delta) = plane_q_delta(plane_idx); |
| 15994 | let residual = dequant_and_inverse_typed(&grid, side, bit_depth(), block_q_idx(), dc_delta, ac_delta, tx_type); |
| 15995 | plane.reconstruct_mc(x, y, side, prediction, &residual); |
| 15996 | Ok((grid, tx_type)) |
| 15997 | } |
| 15998 | |
| 15999 | /// Spec 5.11.25's `single_ref_p1`..`p6` tree (reachable only once `comp_mode` |
| 16000 | /// is known `SINGLE_REFERENCE` -- `decode_stream` refuses any frame with |
| 16001 | /// `reference_select` set before this is ever called, so that read is never |
| 16002 | /// needed here). `above_ref`/`left_ref` are `-1` for an intra/unavailable |
| 16003 | /// neighbour, else the `1..=7` reference that neighbour coded (lane-av1refs). |
| 16004 | fn read_single_ref( |
| 16005 | dec: &mut SymbolDecoder, |
| 16006 | cdfs: &mut Cdfs, |
| 16007 | above_ref: i8, |
| 16008 | above_ref1: Option<i8>, |
| 16009 | left_ref: i8, |
| 16010 | left_ref1: Option<i8>, |
| 16011 | ) -> i8 { |
| 16012 | let above = (above_ref > 0).then_some(above_ref); |
| 16013 | let left = (left_ref > 0).then_some(left_ref); |
| 16014 | let p1 = dec.symbol(&mut cdfs.single_ref[single_ref_p1_ctx(above, above_ref1, left, left_ref1)][0]); |
| 16015 | let ref_frame = if p1 == 1 { |
| 16016 | let p2 = dec.symbol(&mut cdfs.single_ref[single_ref_p2_ctx(above, above_ref1, left, left_ref1)][1]); |
| 16017 | if p2 == 1 { |
| 16018 | ALTREF_FRAME |
| 16019 | } else { |
| 16020 | let p6 = dec.symbol(&mut cdfs.single_ref[single_ref_p6_ctx(above, above_ref1, left, left_ref1)][5]); |
| 16021 | if p6 == 1 { ALTREF2_FRAME } else { BWDREF_FRAME } |
| 16022 | } |
| 16023 | } else { |
| 16024 | let p3 = dec.symbol(&mut cdfs.single_ref[single_ref_p3_ctx(above, above_ref1, left, left_ref1)][2]); |
| 16025 | if p3 == 1 { |
| 16026 | let p5 = dec.symbol(&mut cdfs.single_ref[single_ref_p5_ctx(above, above_ref1, left, left_ref1)][4]); |
| 16027 | if p5 == 1 { GOLDEN_FRAME } else { LAST3_FRAME } |
| 16028 | } else { |
| 16029 | let p4 = dec.symbol(&mut cdfs.single_ref[single_ref_p4_ctx(above, above_ref1, left, left_ref1)][3]); |
| 16030 | if p4 == 1 { LAST2_FRAME } else { LAST_FRAME } |
| 16031 | } |
| 16032 | }; |
| 16033 | if ref_frame != LAST_FRAME { |
| 16034 | NON_LAST_REF_HITS.with(|c| c.set(c.get() + 1)); |
| 16035 | let c = match ref_frame { |
| 16036 | LAST2_FRAME => &LAST2_HITS, |
| 16037 | LAST3_FRAME => &LAST3_HITS, |
| 16038 | GOLDEN_FRAME => &GOLDEN_HITS, |
| 16039 | BWDREF_FRAME => &BWDREF_HITS, |
| 16040 | ALTREF2_FRAME => &ALTREF2_HITS, |
| 16041 | ALTREF_FRAME => &ALTREF_HITS, |
| 16042 | _ => unreachable!("read_single_ref only ever returns LAST_FRAME..=ALTREF_FRAME"), |
| 16043 | }; |
| 16044 | c.with(|v| v.set(v.get() + 1)); |
| 16045 | } |
| 16046 | ref_frame |
| 16047 | } |
| 16048 | |
| 16049 | /// Spec 5.11.25's `comp_mode` (lane-av1comp): whether an inter block reads |
| 16050 | /// `SINGLE_REFERENCE` or `COMPOUND_REFERENCE`, only reached once a frame's |
| 16051 | /// `reference_select` header bit lets a block choose per-block instead of |
| 16052 | /// fixing the mode. `above`/`left` are `None` for an intra/unavailable |
| 16053 | /// neighbour, [`NeighbourRef`] for a coded one -- [`reference_mode_ctx`]'s |
| 16054 | /// own doc. Called from [`decode_inter_block`] once its own `reference_select` |
| 16055 | /// parameter is set; [`decode_inter_block8`]'s 8x8 leaf path gates the same |
| 16056 | /// way with a coarser (`LAST_FRAME`-only) neighbour shape. |
| 16057 | /// Resolves a compound reference's own planes -- the same `ref_frame == |
| 16058 | /// LAST_FRAME -> ref_y/u/v, else other_refs[ref_frame]` lookup |
| 16059 | /// [`decode_inter_block`]'s single-ref path already does, factored out so |
| 16060 | /// the compound path can call it twice (lane-av1comp). |
| 16061 | fn ref_planes<'a>( |
| 16062 | ref_frame: i8, |
| 16063 | ref_y: &'a PlaneBuf, |
| 16064 | ref_u: &'a PlaneBuf, |
| 16065 | ref_v: &'a PlaneBuf, |
| 16066 | other_refs: &'a RefSlots, |
| 16067 | ) -> Result<(&'a PlaneBuf, &'a PlaneBuf, &'a PlaneBuf)> { |
| 16068 | if ref_frame == LAST_FRAME { |
| 16069 | Ok((ref_y, ref_u, ref_v)) |
| 16070 | } else { |
| 16071 | match other_refs[ref_frame as usize] { |
| 16072 | Some((ry, ru, rv)) => Ok((ry, ru, rv)), |
| 16073 | None => Err(unsupported( |
| 16074 | "a reference frame selected with no picture at this frame's own \ |
| 16075 | ref_frame_idx slot for it", |
| 16076 | )), |
| 16077 | } |
| 16078 | } |
| 16079 | } |
| 16080 | |
| 16081 | /// libaom `foreach_overlappable_nb_above` (`obmc.h`): walks the mi row |
| 16082 | /// directly above this block's own column span, one bordering block at a |
| 16083 | /// time (stepping by that neighbour's own `MiInfo::size`, which is always |
| 16084 | /// `<= 16` since no block this decoder ever codes exceeds 64x64), collecting |
| 16085 | /// every *inter* neighbour found (an intra or unset cell is skipped, one mi |
| 16086 | /// unit at a time, and does not itself count as a step). `max_neighbors` |
| 16087 | /// caller-bounds the scan: `1` for the eligibility check (any hit at all), |
| 16088 | /// `av1_build_obmc_inter_prediction`'s own `max_neighbor_obmc` table for the |
| 16089 | /// real blend pass. Round 1 corner-cut: libaom's own "4-wide block, treat as |
| 16090 | /// half of a chroma pair" merge (`obmc.h`'s `mi_step == 1` special case) is |
| 16091 | /// not ported -- no block this decoder codes is narrower than 8px, so it |
| 16092 | /// never fires here regardless of what a neighbour's own decoder was. |
| 16093 | fn overlappable_above( |
| 16094 | grid: &MiGrid, |
| 16095 | mi_row: usize, |
| 16096 | mi_col: usize, |
| 16097 | bw4: usize, |
| 16098 | mi_cols: usize, |
| 16099 | max_neighbors: usize, |
| 16100 | ) -> Vec<(usize, usize, MiInfo)> { |
| 16101 | let mut out = Vec::new(); |
| 16102 | if mi_row == 0 { |
| 16103 | return out; |
| 16104 | } |
| 16105 | let end_col = (mi_col + bw4).min(mi_cols); |
| 16106 | let mut col = mi_col; |
| 16107 | while col < end_col && out.len() < max_neighbors { |
| 16108 | let cell = grid.get(mi_row - 1, col); |
| 16109 | let step = cell.map_or(1, |c| c.size).max(1); |
| 16110 | if let Some(info) = cell { |
| 16111 | if info.is_inter { |
| 16112 | out.push((col - mi_col, step.min(end_col - col), *info)); |
| 16113 | } |
| 16114 | } |
| 16115 | col += step; |
| 16116 | } |
| 16117 | out |
| 16118 | } |
| 16119 | |
| 16120 | /// `foreach_overlappable_nb_left`'s left-column mirror of |
| 16121 | /// [`overlappable_above`] -- see its doc for the shared corner-cut. |
| 16122 | fn overlappable_left( |
| 16123 | grid: &MiGrid, |
| 16124 | mi_row: usize, |
| 16125 | mi_col: usize, |
| 16126 | bh4: usize, |
| 16127 | mi_rows: usize, |
| 16128 | max_neighbors: usize, |
| 16129 | ) -> Vec<(usize, usize, MiInfo)> { |
| 16130 | let mut out = Vec::new(); |
| 16131 | if mi_col == 0 { |
| 16132 | return out; |
| 16133 | } |
| 16134 | let end_row = (mi_row + bh4).min(mi_rows); |
| 16135 | let mut row = mi_row; |
| 16136 | while row < end_row && out.len() < max_neighbors { |
| 16137 | let cell = grid.get(row, mi_col - 1); |
| 16138 | // Vertical walk steps by the neighbour's HEIGHT -- a 32x16 strip's |
| 16139 | // width (8) would swallow the strip below it. |
| 16140 | let step = cell.map_or(1, |c| c.size_h).max(1); |
| 16141 | if let Some(info) = cell { |
| 16142 | if info.is_inter { |
| 16143 | out.push((row - mi_row, step.min(end_row - row), *info)); |
| 16144 | } |
| 16145 | } |
| 16146 | row += step; |
| 16147 | } |
| 16148 | out |
| 16149 | } |
| 16150 | |
| 16151 | /// The warp-sample gather's own `has_top_right` probe (libaom `av1_findSamples` |
| 16152 | /// calls the same `mvref_common.c` function the MV stack does). lane-sb128 r2: |
| 16153 | /// this used to hardcode `SB_MI` (a 64x64 superblock) -- at `sb_size == 128` |
| 16154 | /// libaom's mask loop runs one level further and strips the top-right sample |
| 16155 | /// off blocks whose neighbour IS decoded, so a warped 128-superblock frame |
| 16156 | /// picked up wrong warp parameters (frame 6 of the 128 inter gate). Delegates |
| 16157 | /// to the single sb-aware port in [`crate::mvstack::has_top_right`]; `bs` is |
| 16158 | /// the block's own side in 4x4 (`mi`) units (square inter leaves only here). |
| 16159 | fn has_top_right(mi_row: usize, mi_col: usize, bs: usize) -> bool { |
| 16160 | crate::mvstack::has_top_right(mi_row, mi_col, bs, bs) |
| 16161 | } |
| 16162 | |
| 16163 | /// `av1_findSamples` (`mvref_common.c`), full port: the up to |
| 16164 | /// `LEAST_SQUARES_SAMPLES_MAX` (8) above/left/top-left/top-right neighbour |
| 16165 | /// samples (`record_samples`) that share this block's own single reference |
| 16166 | /// frame and are themselves single-ref -- `mbmi->num_proj_ref` is |
| 16167 | /// `.len()` of the returned vec. `bw4` is the block's own side in 4x4 units |
| 16168 | /// (square, so also `bh4`; a real block's `xd->width`/`xd->height` in |
| 16169 | /// libaom are also mi units of that same square side here). Each sample's |
| 16170 | /// own `bw`/`bh` (fed to [`crate::warp::record_sample`]'s offset formula) |
| 16171 | /// come from the *neighbour* cell's own coded size, not this block's. |
| 16172 | #[allow(clippy::too_many_arguments)] |
| 16173 | fn find_samples( |
| 16174 | grid: &MiGrid, |
| 16175 | mi_row: usize, |
| 16176 | mi_col: usize, |
| 16177 | bw4: usize, |
| 16178 | bh4: usize, |
| 16179 | mi_cols: usize, |
| 16180 | mi_rows: usize, |
| 16181 | ref_frame: i8, |
| 16182 | ) -> Vec<crate::warp::Sample> { |
| 16183 | const MAX: usize = crate::warp::LEAST_SQUARES_SAMPLES_MAX; |
| 16184 | let up_available = mi_row > 0; |
| 16185 | let left_available = mi_col > 0; |
| 16186 | let mut samples = Vec::with_capacity(MAX); |
| 16187 | let single_ref_match = |info: &MiInfo| info.ref_frame == ref_frame && info.ref_frame1.is_none(); |
| 16188 | let mut do_tl = true; |
| 16189 | let mut do_tr = true; |
| 16190 | let rec = |info: &MiInfo, row_offset: i32, sign_r: i32, col_offset: i32, sign_c: i32| { |
| 16191 | let nb_bw = (info.size * 4) as i32; |
| 16192 | // lane-rect r2: the neighbour's own true height -- a rect strip |
| 16193 | // neighbour donates its sample at ITS center (aom pts y=-72 for a |
| 16194 | // 32x16 above strip; the square assumption put it at -136). |
| 16195 | let nb_bh = (info.size_h * 4) as i32; |
| 16196 | crate::warp::record_sample(nb_bw, nb_bh, info.mv, row_offset, sign_r, col_offset, sign_c) |
| 16197 | }; |
| 16198 | |
| 16199 | 'outer: { |
| 16200 | if up_available { |
| 16201 | let above = grid.get(mi_row - 1, mi_col); |
| 16202 | let superblock_width = above.map_or(1, |c| c.size).max(1); |
| 16203 | if bw4 <= superblock_width { |
| 16204 | let col_offset = -((mi_col % superblock_width) as isize); |
| 16205 | if col_offset < 0 { |
| 16206 | do_tl = false; |
| 16207 | } |
| 16208 | if col_offset + superblock_width as isize > bw4 as isize { |
| 16209 | do_tr = false; |
| 16210 | } |
| 16211 | if let Some(info) = above { |
| 16212 | if single_ref_match(info) { |
| 16213 | samples.push(rec(info, 0, -1, col_offset as i32, 1)); |
| 16214 | if samples.len() >= MAX { |
| 16215 | break 'outer; |
| 16216 | } |
| 16217 | } |
| 16218 | } |
| 16219 | } else { |
| 16220 | let mut i = 0usize; |
| 16221 | let limit = bw4.min(mi_cols.saturating_sub(mi_col)); |
| 16222 | while i < limit { |
| 16223 | let cell = grid.get(mi_row - 1, mi_col + i); |
| 16224 | let sw = cell.map_or(1, |c| c.size).max(1); |
| 16225 | if let Some(info) = cell { |
| 16226 | if single_ref_match(info) { |
| 16227 | samples.push(rec(info, 0, -1, i as i32, 1)); |
| 16228 | if samples.len() >= MAX { |
| 16229 | break 'outer; |
| 16230 | } |
| 16231 | } |
| 16232 | } |
| 16233 | i += sw; |
| 16234 | } |
| 16235 | } |
| 16236 | } |
| 16237 | |
| 16238 | if left_available { |
| 16239 | let left = grid.get(mi_row, mi_col - 1); |
| 16240 | let superblock_height = left.map_or(1, |c| c.size_h).max(1); |
| 16241 | if bh4 <= superblock_height { |
| 16242 | let row_offset = -((mi_row % superblock_height) as isize); |
| 16243 | if row_offset < 0 { |
| 16244 | do_tl = false; |
| 16245 | } |
| 16246 | if let Some(info) = left { |
| 16247 | if single_ref_match(info) { |
| 16248 | samples.push(rec(info, row_offset as i32, 1, 0, -1)); |
| 16249 | if samples.len() >= MAX { |
| 16250 | break 'outer; |
| 16251 | } |
| 16252 | } |
| 16253 | } |
| 16254 | } else { |
| 16255 | let mut i = 0usize; |
| 16256 | let limit = bh4.min(mi_rows.saturating_sub(mi_row)); |
| 16257 | while i < limit { |
| 16258 | let cell = grid.get(mi_row + i, mi_col - 1); |
| 16259 | let sh = cell.map_or(1, |c| c.size_h).max(1); |
| 16260 | if let Some(info) = cell { |
| 16261 | if single_ref_match(info) { |
| 16262 | samples.push(rec(info, i as i32, 1, 0, -1)); |
| 16263 | if samples.len() >= MAX { |
| 16264 | break 'outer; |
| 16265 | } |
| 16266 | } |
| 16267 | } |
| 16268 | i += sh; |
| 16269 | } |
| 16270 | } |
| 16271 | } |
| 16272 | |
| 16273 | if do_tl && left_available && up_available { |
| 16274 | if let Some(info) = grid.get(mi_row - 1, mi_col - 1) { |
| 16275 | if single_ref_match(info) { |
| 16276 | samples.push(rec(info, 0, -1, 0, -1)); |
| 16277 | if samples.len() >= MAX { |
| 16278 | break 'outer; |
| 16279 | } |
| 16280 | } |
| 16281 | } |
| 16282 | } |
| 16283 | |
| 16284 | if do_tr && has_top_right(mi_row, mi_col, bw4) { |
| 16285 | let tr_col = mi_col + bw4; |
| 16286 | if mi_row > 0 && tr_col < mi_cols { |
| 16287 | if let Some(info) = grid.get(mi_row - 1, tr_col) { |
| 16288 | if single_ref_match(info) { |
| 16289 | samples.push(rec(info, 0, -1, bw4 as i32, 1)); |
| 16290 | } |
| 16291 | } |
| 16292 | } |
| 16293 | } |
| 16294 | } |
| 16295 | samples |
| 16296 | } |
| 16297 | |
| 16298 | /// `mbmi->num_proj_ref`: [`find_samples`]'s own count, all that |
| 16299 | /// `motion_mode_allowed` needs to pick the 3-symbol `motion_mode_cdf` over |
| 16300 | /// the 2-symbol `obmc_cdf` (spec 5.11.24). |
| 16301 | #[allow(clippy::too_many_arguments)] |
| 16302 | fn num_proj_ref( |
| 16303 | grid: &MiGrid, |
| 16304 | mi_row: usize, |
| 16305 | mi_col: usize, |
| 16306 | bw4: usize, |
| 16307 | bh4: usize, |
| 16308 | mi_cols: usize, |
| 16309 | mi_rows: usize, |
| 16310 | ref_frame: i8, |
| 16311 | ) -> u8 { |
| 16312 | find_samples(grid, mi_row, mi_col, bw4, bh4, mi_cols, mi_rows, ref_frame).len() as u8 |
| 16313 | } |
| 16314 | |
| 16315 | /// `av1_get_obmc_mask` (`reconinter.c`): the per-position blend weight |
| 16316 | /// (`AOM_BLEND_A64` numerator, out of 64) for the neighbour's own |
| 16317 | /// contribution's *complement* -- i.e. `dst = (mask*orig + (64-mask)*nbr + |
| 16318 | /// 32) >> 6`, low near the shared border (heavier neighbour weight) rising |
| 16319 | /// to 64 (no neighbour contribution) away from it. Only the lengths this |
| 16320 | /// decoder's own overlap sizes ever produce (luma 8/16/32, chroma 4/8/16) |
| 16321 | /// are transcribed. |
| 16322 | fn obmc_mask(len: usize) -> &'static [u8] { |
| 16323 | // lane-gmaffine r1: the two SHORT masks (libaom `reconinter.c` |
| 16324 | // `obmc_mask_1`/`obmc_mask_2`) -- an 8x8 leaf's chroma plane is 4x4, so |
| 16325 | // its overlap is 2 (and 1 at a half-block edge). Unreachable until this |
| 16326 | // round wired interior 16x16 splits down to real 8x8 leaves, where the |
| 16327 | // old `unreachable!()` below fired as a hard panic. |
| 16328 | const M1: [u8; 1] = [64]; |
| 16329 | // lane-inter8 r1: `obmc_mask_2` (`reconinter.c` 753) -- reached by the |
| 16330 | // LEFT pass of an 8x8 block's chroma plane (overlap 4 luma columns, 2 |
| 16331 | // chroma), which only became live once 8x8 inter leaves stopped being |
| 16332 | // refused. |
| 16333 | const M2: [u8; 2] = [45, 64]; |
| 16334 | const M4: [u8; 4] = [39, 50, 59, 64]; |
| 16335 | const M8: [u8; 8] = [36, 42, 48, 53, 57, 61, 64, 64]; |
| 16336 | const M16: [u8; 16] = [ |
| 16337 | 34, 37, 40, 43, 46, 49, 52, 54, 56, 58, 60, 61, 64, 64, 64, 64, |
| 16338 | ]; |
| 16339 | const M32: [u8; 32] = [ |
| 16340 | 33, 35, 36, 38, 40, 41, 43, 44, 45, 47, 48, 50, 51, 52, 53, 55, 56, 57, 58, 59, 60, 60, |
| 16341 | 61, 62, 64, 64, 64, 64, 64, 64, 64, 64, |
| 16342 | ]; |
| 16343 | match len { |
| 16344 | 1 => &M1, |
| 16345 | 2 => &M2, |
| 16346 | 4 => &M4, |
| 16347 | 8 => &M8, |
| 16348 | 16 => &M16, |
| 16349 | 32 => &M32, |
| 16350 | _ => unreachable!("obmc overlap length outside this decoder's own block-size range"), |
| 16351 | } |
| 16352 | } |
| 16353 | |
| 16354 | /// A bordering block's own resolved interp filter kernel, `(h, v)` -- |
| 16355 | /// mirrors [`resolve_interp_filter`]'s own return order from its stored |
| 16356 | /// `[u8; 2]` (`Neighbours::above_filter`/`left_filter`, `[sym0, sym1]` = |
| 16357 | /// `[v_sym, h_sym]`). The `[3, 3]` sentinel is only ever stored for an intra |
| 16358 | /// neighbour (excluded by [`overlappable_above`]/[`overlappable_left`]'s |
| 16359 | /// `is_inter` gate) or when `interp_fixed` is `Some` for the whole frame |
| 16360 | /// (handled here first) -- so a sentinel reaching the kernel lookup means the |
| 16361 | /// stream desynced upstream, and this refuses instead of panicking. |
| 16362 | fn neighbour_filter( |
| 16363 | interp_fixed: Option<mc::InterpFilterKind>, |
| 16364 | sym: [u8; 2], |
| 16365 | ) -> Result<(mc::InterpFilterKind, mc::InterpFilterKind)> { |
| 16366 | if let Some(kind) = interp_fixed { |
| 16367 | return Ok((kind, kind)); |
| 16368 | } |
| 16369 | // lane-golomb r9: this used to hand the sentinel straight to |
| 16370 | // `from_switchable_symbol`, which PANICS on any value above 2 -- a crash |
| 16371 | // on stream data (measured on a real 192x68 cq40 aomenc stream, where an |
| 16372 | // inter leaf below 8x8 desyncs upstream and leaves an unrecorded filter |
| 16373 | // behind). A decoder must refuse such a stream by name, never abort the |
| 16374 | // process; the desync that produces the sentinel belongs to lane-intersub8. |
| 16375 | if sym[0] > 2 || sym[1] > 2 { |
| 16376 | return Err(unsupported( |
| 16377 | "an OBMC neighbour whose switchable interp filter was never recorded", |
| 16378 | )); |
| 16379 | } |
| 16380 | Ok(( |
| 16381 | mc::InterpFilterKind::from_switchable_symbol(sym[1] as usize), |
| 16382 | mc::InterpFilterKind::from_switchable_symbol(sym[0] as usize), |
| 16383 | )) |
| 16384 | } |
| 16385 | |
| 16386 | /// One neighbour's own re-prediction over a `w`x`h` region, MC'd fresh |
| 16387 | /// against its own `mv`/reference/filter (libaom `av1_build_inter_predictor` |
| 16388 | /// under `av1_setup_build_prediction_by_{above,left}_pred`). |
| 16389 | #[allow(clippy::too_many_arguments)] |
| 16390 | fn obmc_neighbour_pred( |
| 16391 | refplane: &PlaneBuf, |
| 16392 | x: usize, |
| 16393 | y: usize, |
| 16394 | mv: (i32, i32), |
| 16395 | w: usize, |
| 16396 | h: usize, |
| 16397 | luma: bool, |
| 16398 | h_kind: mc::InterpFilterKind, |
| 16399 | v_kind: mc::InterpFilterKind, |
| 16400 | // lane-scaledref r1: spec 7.11.3.3's x_scale_fp for THIS neighbour's own |
| 16401 | // reference (derived from luma widths, applied unchanged to chroma whose |
| 16402 | // x_q4 is already in its own plane's pixel units). `REF_NO_SCALE` takes |
| 16403 | // the ordinary stride-1 path bit-exact. |
| 16404 | x_scale_fp: i64, |
| 16405 | ) -> Vec<u16> { |
| 16406 | let mut out = vec![0u16; w * h]; |
| 16407 | if x_scale_fp == mc::REF_NO_SCALE { |
| 16408 | mc::predict_with_filters( |
| 16409 | &refplane.data, |
| 16410 | refplane.width, |
| 16411 | refplane.true_width, |
| 16412 | refplane.true_height, |
| 16413 | mv_to_q4(x, mv.1, luma), |
| 16414 | mv_to_q4(y, mv.0, luma), |
| 16415 | w, |
| 16416 | h, |
| 16417 | h_kind, |
| 16418 | v_kind, |
| 16419 | &mut out, |
| 16420 | ); |
| 16421 | } else { |
| 16422 | mc::predict_scaled( |
| 16423 | &refplane.data, |
| 16424 | refplane.width, |
| 16425 | refplane.true_width, |
| 16426 | refplane.true_height, |
| 16427 | mv_to_q4(x, mv.1, luma), |
| 16428 | mv_to_q4(y, mv.0, luma), |
| 16429 | x_scale_fp, |
| 16430 | w, |
| 16431 | h, |
| 16432 | h_kind, |
| 16433 | v_kind, |
| 16434 | &mut out, |
| 16435 | ); |
| 16436 | } |
| 16437 | out |
| 16438 | } |
| 16439 | |
| 16440 | /// Blends `tmp` into `dst` (stride `stride`) at `(ox, oy)`, mask varying by |
| 16441 | /// row (the above-neighbour pass, `aom_blend_a64_vmask`). |
| 16442 | /// `ii_weights1d` (reconinter.c:524): the interintra smooth-mask falloff. |
| 16443 | const II_WEIGHTS_1D: [u8; 128] = [ |
| 16444 | 60, 58, 56, 54, 52, 50, 48, 47, 45, 44, 42, 41, 39, 38, 37, 35, 34, 33, 32, |
| 16445 | 31, 30, 29, 28, 27, 26, 25, 24, 23, 22, 22, 21, 20, 19, 19, 18, 18, 17, 16, |
| 16446 | 16, 15, 15, 14, 14, 13, 13, 12, 12, 12, 11, 11, 10, 10, 10, 9, 9, 9, 8, |
| 16447 | 8, 8, 8, 7, 7, 7, 7, 6, 6, 6, 6, 6, 5, 5, 5, 5, 5, 4, 4, |
| 16448 | 4, 4, 4, 4, 4, 4, 3, 3, 3, 3, 3, 3, 3, 3, 3, 2, 2, 2, 2, |
| 16449 | 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 1, 1, 1, 1, 1, 1, 1, 1, |
| 16450 | 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, |
| 16451 | ]; |
| 16452 | |
| 16453 | /// lane-interintra r1: non-wedge interintra prediction for one plane |
| 16454 | /// (reconinter.c `av1_build_interintra_predictor` + `combine_interintra` + |
| 16455 | /// `build_smooth_interintra_mask`). Builds the intra predictor for the |
| 16456 | /// block's II mode (DC/V/H/SMOOTH, angle 0, no filter-intra) from `plane`'s |
| 16457 | /// own decoded edges at `(x, y)`, then blends it over the inter prediction |
| 16458 | /// in `pred`: `comp = (mask*intra + (64-mask)*inter + 32) >> 6` |
| 16459 | /// (`aom_blend_a64_mask`, intra as src0). `ii_size_scales[plane_bsize]` |
| 16460 | /// reduces to `128 / side` for the square plane sizes this decoder reaches. |
| 16461 | /// lane-wii r2: `wedge == Some(..)` replaces the mode mask with the wedge |
| 16462 | /// codebook mask (fixed sign 0; luma stride, 2x2 box-averaged for chroma). |
| 16463 | fn interintra_blend( |
| 16464 | plane: &PlaneBuf, |
| 16465 | x: usize, |
| 16466 | y: usize, |
| 16467 | side: usize, |
| 16468 | ii_mode: u8, |
| 16469 | wedge: Option<(&'static [u8], usize)>, |
| 16470 | pred: &mut [u16], |
| 16471 | ) { |
| 16472 | // `interintra_to_intra_mode`: II_DC/II_V/II_H/II_SMOOTH. |
| 16473 | let intra_mode = match ii_mode { |
| 16474 | 0 => crate::intra::DC_PRED, |
| 16475 | 1 => crate::intra::V_PRED, |
| 16476 | 2 => crate::intra::H_PRED, |
| 16477 | _ => crate::intra::SMOOTH_PRED, |
| 16478 | }; |
| 16479 | let (above, left, corner) = plane.edges( |
| 16480 | x, |
| 16481 | y, |
| 16482 | side, |
| 16483 | crate::encode::Reach { |
| 16484 | above_right: false, |
| 16485 | below_left: false, |
| 16486 | }, |
| 16487 | ); |
| 16488 | let mut intra = vec![0u16; side * side]; |
| 16489 | // Edge filtering never applies here: V/H at delta 0 are plain edge |
| 16490 | // copies (angle 90/180 skip the directional walk), DC/SMOOTH carry no |
| 16491 | // angle at all. |
| 16492 | predict( |
| 16493 | intra_mode, |
| 16494 | 0, |
| 16495 | above.as_deref(), |
| 16496 | left.as_deref(), |
| 16497 | corner, |
| 16498 | side, |
| 16499 | side, |
| 16500 | false, |
| 16501 | false, |
| 16502 | &mut intra, |
| 16503 | ); |
| 16504 | let scale = 128 / side; |
| 16505 | for i in 0..side { |
| 16506 | for j in 0..side { |
| 16507 | let m = u32::from(match wedge { |
| 16508 | // lane-wii r2: wedge-interintra mask (aom_blend_a64_mask |
| 16509 | // over the wedge codebook, intra as src0, fixed sign 0). |
| 16510 | // Luma plane: mask rows are luma-resolution, ms == side. |
| 16511 | // 4:2:0 chroma: 2x2 box average (reconinter.c sub8x8 -- |
| 16512 | // subw == subh == 1). |
| 16513 | Some((mask, ms)) if ms == side => mask[i * side + j], |
| 16514 | Some((mask, ms)) => { |
| 16515 | let t = 2 * i * ms + 2 * j; |
| 16516 | ((u32::from(mask[t]) |
| 16517 | + u32::from(mask[t + 1]) |
| 16518 | + u32::from(mask[t + ms]) |
| 16519 | + u32::from(mask[t + ms + 1]) |
| 16520 | + 2) |
| 16521 | >> 2) as u8 |
| 16522 | } |
| 16523 | None => match ii_mode { |
| 16524 | 0 => 32, |
| 16525 | 1 => II_WEIGHTS_1D[i * scale], |
| 16526 | 2 => II_WEIGHTS_1D[j * scale], |
| 16527 | _ => II_WEIGHTS_1D[i.min(j) * scale], |
| 16528 | }, |
| 16529 | }); |
| 16530 | let idx = i * side + j; |
| 16531 | pred[idx] = |
| 16532 | ((m * u32::from(intra[idx]) + (64 - m) * u32::from(pred[idx]) + 32) >> 6) as u16; |
| 16533 | } |
| 16534 | } |
| 16535 | } |
| 16536 | |
| 16537 | fn obmc_blend_v(dst: &mut [u16], stride: usize, ox: usize, oy: usize, w: usize, h: usize, tmp: &[u16]) { |
| 16538 | let mask = obmc_mask(h); |
| 16539 | for row in 0..h { |
| 16540 | let m = u32::from(mask[row]); |
| 16541 | for col in 0..w { |
| 16542 | let d = &mut dst[(oy + row) * stride + ox + col]; |
| 16543 | let t = u32::from(tmp[row * w + col]); |
| 16544 | *d = ((m * u32::from(*d) + (64 - m) * t + 32) >> 6) as u16; |
| 16545 | } |
| 16546 | } |
| 16547 | } |
| 16548 | |
| 16549 | /// Blends `tmp` into `dst` at `(ox, oy)`, mask varying by column (the |
| 16550 | /// left-neighbour pass, `aom_blend_a64_hmask`). |
| 16551 | fn obmc_blend_h(dst: &mut [u16], stride: usize, ox: usize, oy: usize, w: usize, h: usize, tmp: &[u16]) { |
| 16552 | let mask = obmc_mask(w); |
| 16553 | for row in 0..h { |
| 16554 | for col in 0..w { |
| 16555 | let m = u32::from(mask[col]); |
| 16556 | let d = &mut dst[(oy + row) * stride + ox + col]; |
| 16557 | let t = u32::from(tmp[row * w + col]); |
| 16558 | *d = ((m * u32::from(*d) + (64 - m) * t + 32) >> 6) as u16; |
| 16559 | } |
| 16560 | } |
| 16561 | } |
| 16562 | |
| 16563 | /// `av1_build_obmc_inter_prediction` (libaom `reconinter.c`): re-predicts |
| 16564 | /// the overlap strip against each bordering above/left inter neighbour's own |
| 16565 | /// mv/ref/filter and blends it into this block's just-built single-reference |
| 16566 | /// prediction, luma then both 4:2:0 chroma planes -- above pass first, then |
| 16567 | /// left, each reading only the *original* prediction this function was |
| 16568 | /// handed (never the other pass's own output), matching libaom's own |
| 16569 | /// independence between the two. `max_neighbor_obmc`'s own cap table |
| 16570 | /// (`{0,1,2,3,4,4}` indexed by `mi_size_wide_log2`/`_high_log2`, this |
| 16571 | /// decoder's own square blocks always index the same value both ways) bounds |
| 16572 | /// how many bordering blocks actually get blended, separate from the |
| 16573 | /// eligibility scan's own unbounded ("any at all") walk. |
| 16574 | #[allow(clippy::too_many_arguments)] |
| 16575 | fn obmc_blend( |
| 16576 | grid: &MiGrid, |
| 16577 | neighbours: &Neighbours, |
| 16578 | mi_row: usize, |
| 16579 | mi_col: usize, |
| 16580 | bw4: usize, |
| 16581 | bh4: usize, |
| 16582 | mi_rows: usize, |
| 16583 | mi_cols: usize, |
| 16584 | side: usize, |
| 16585 | write_w: usize, |
| 16586 | write_h: usize, |
| 16587 | chroma_side: usize, |
| 16588 | px: usize, |
| 16589 | py: usize, |
| 16590 | cpx: usize, |
| 16591 | cpy: usize, |
| 16592 | ref_y: &PlaneBuf, |
| 16593 | ref_u: &PlaneBuf, |
| 16594 | ref_v: &PlaneBuf, |
| 16595 | other_refs: &RefSlots, |
| 16596 | interp_fixed: Option<mc::InterpFilterKind>, |
| 16597 | // lane-scaledref r1: this frame's own coded luma width -- each OBMC |
| 16598 | // neighbour is re-predicted against ITS OWN reference, which may be |
| 16599 | // scaled differently from this block's (spec 7.11.3.3 / libaom |
| 16600 | // `av1_setup_build_prediction_by_above_pred` passing that ref's own |
| 16601 | // `scale_factors`). |
| 16602 | frame_width: usize, |
| 16603 | pred_y: &mut [u16], |
| 16604 | pred_u: &mut [u16], |
| 16605 | pred_v: &mut [u16], |
| 16606 | ) -> Result<()> { |
| 16607 | // lane-rect r2 (libaom av1_build_obmc_inter_prediction): each pass has |
| 16608 | // its OWN neighbour cap (width-log2 for above, height-log2 for left) and |
| 16609 | // overlap (bh/2 rows above, bw/2 cols left) -- one square `side` was |
| 16610 | // right only for square blocks. |
| 16611 | // libaom `max_neighbor_obmc[6] = { 0, 1, 2, 3, 4, 4 }` indexed by |
| 16612 | // `mi_size_wide_log2[bsize]` (above pass) / `mi_size_high_log2` (left): |
| 16613 | // 1 mi -> 0, 2 -> 1, 4 -> 2, 8 -> 3, 16+ -> 4. lane-inter4 r3: the old |
| 16614 | // `1..=4 => 2` row was right only for blocks at least 16 px on that axis |
| 16615 | // -- a 16x8 leaf's LEFT pass (bh4 = 2) walked up to two neighbours where |
| 16616 | // libaom walks one, blending an extra 8-px-tall neighbour into every |
| 16617 | // rect leaf with two of them on its left edge (class table-narrowed-to- |
| 16618 | // the-reachable-sizes; the square gates could never expose it). |
| 16619 | let max_nb = |n4: usize| match n4 { |
| 16620 | 0..=1 => 0, |
| 16621 | 2 => 1, |
| 16622 | 3..=4 => 2, |
| 16623 | 5..=8 => 3, |
| 16624 | _ => 4, |
| 16625 | }; |
| 16626 | // lane-inter4 r4: r3 refused OBMC on a 16x8/8x16 leaf (5302 luma pixels |
| 16627 | // of one frame wrong). Re-measured on this tree with an `EC_OBMC` rung in |
| 16628 | // both decoders on that exact stream: the neighbour lists agree entry for |
| 16629 | // entry (position, span, mv, ref, size, filter) and all six pre-filter |
| 16630 | // frames are byte-identical to the instrumented aomdec's. The refusal is |
| 16631 | // gone; the counter below is what the gate's `--enable-obmc=1` arm reads. |
| 16632 | // The only decode-path change between r3's measurement and this one is the |
| 16633 | // lane-interbis merge (`build_motion_field`'s compound slot walk), which |
| 16634 | // moves temporal MV candidates -- not bisected further. |
| 16635 | if matches!((write_w, write_h), (16, 8) | (8, 16)) { |
| 16636 | OBMC_RECT_LEAF_HITS.with(|c| c.set(c.get() + 1)); |
| 16637 | } |
| 16638 | let overlap_above = write_h / 2; |
| 16639 | let overlap_left = write_w / 2; |
| 16640 | // lane-inter8 r1 / lane-scaledref r1: `av1_skip_u4x4_pred_in_obmc` |
| 16641 | // (`reconinter.c` 820, `DISABLE_CHROMA_U8X8_OBMC == 0` -- "one-sided |
| 16642 | // obmc") returns `dir == 0` when the chroma plane's own block is |
| 16643 | // BLOCK_4X4/8X4/4X8 (an 8x8, 16x8 or 8x16 luma block at 4:2:0): the |
| 16644 | // ABOVE pass skips chroma entirely, the LEFT pass still blends it. |
| 16645 | let skip_chroma_above = matches!((write_w, write_h), (8, 8) | (16, 8) | (8, 16)); |
| 16646 | |
| 16647 | for (off4, span4, nb) in overlappable_above(grid, mi_row, mi_col, bw4, mi_cols, max_nb(bw4)) |
| 16648 | { |
| 16649 | // `Neighbours::above_filter` is indexed in `SUB`(16px)-wide columns |
| 16650 | // (this decoder's own outer block-loop granularity), one step |
| 16651 | // coarser than the mi(4px) units `overlappable_above` walks in -- |
| 16652 | // divide back down to that column. |
| 16653 | // lane-gmaffine r2: the neighbour's OWN mi-granular filter |
| 16654 | // (libaom `av1_setup_build_prediction_by_above_pred` reads |
| 16655 | // `above_mbmi->interp_filters`); the 16px-granular `Neighbours` slot |
| 16656 | // stays as the fallback for paths that do not record one yet |
| 16657 | // (compound), which is what every earlier round used. |
| 16658 | let (h_kind, v_kind) = neighbour_filter( |
| 16659 | interp_fixed, |
| 16660 | neighbours.above_filter[mi_col + off4], |
| 16661 | )?; |
| 16662 | let (ny, nu, nv) = ref_planes(nb.ref_frame, ref_y, ref_u, ref_v, other_refs)?; |
| 16663 | let nb_scale = mc::scale_factor(ny.width, frame_width); |
| 16664 | let (bw, bh, ox) = (span4 * 4, overlap_above, off4 * 4); |
| 16665 | let tmp_y = obmc_neighbour_pred(ny, px + ox, py, nb.mv, bw, bh, true, h_kind, v_kind, nb_scale); |
| 16666 | obmc_blend_v(pred_y, side, ox, 0, bw, bh, &tmp_y); |
| 16667 | if !skip_chroma_above { |
| 16668 | let (cbw, cbh, cox) = (bw / 2, overlap_above / 2, ox / 2); |
| 16669 | let tmp_u = obmc_neighbour_pred(nu, cpx + cox, cpy, nb.mv, cbw, cbh, false, h_kind, v_kind, nb_scale); |
| 16670 | obmc_blend_v(pred_u, chroma_side, cox, 0, cbw, cbh, &tmp_u); |
| 16671 | let tmp_v = obmc_neighbour_pred(nv, cpx + cox, cpy, nb.mv, cbw, cbh, false, h_kind, v_kind, nb_scale); |
| 16672 | obmc_blend_v(pred_v, chroma_side, cox, 0, cbw, cbh, &tmp_v); |
| 16673 | } |
| 16674 | } |
| 16675 | |
| 16676 | for (off4, span4, nb) in overlappable_left(grid, mi_row, mi_col, bh4, mi_rows, max_nb(bh4)) { |
| 16677 | let (h_kind, v_kind) = neighbour_filter( |
| 16678 | interp_fixed, |
| 16679 | neighbours.left_filter[mi_row + off4], |
| 16680 | )?; |
| 16681 | let (ny, nu, nv) = ref_planes(nb.ref_frame, ref_y, ref_u, ref_v, other_refs)?; |
| 16682 | let nb_scale = mc::scale_factor(ny.width, frame_width); |
| 16683 | let (bw, bh, oy) = (overlap_left, span4 * 4, off4 * 4); |
| 16684 | let tmp_y = obmc_neighbour_pred(ny, px, py + oy, nb.mv, bw, bh, true, h_kind, v_kind, nb_scale); |
| 16685 | obmc_blend_h(pred_y, side, 0, oy, bw, bh, &tmp_y); |
| 16686 | let (cbw, cbh, coy) = (overlap_left / 2, bh / 2, oy / 2); |
| 16687 | let tmp_u = obmc_neighbour_pred(nu, cpx, cpy + coy, nb.mv, cbw, cbh, false, h_kind, v_kind, nb_scale); |
| 16688 | obmc_blend_h(pred_u, chroma_side, 0, coy, cbw, cbh, &tmp_u); |
| 16689 | let tmp_v = obmc_neighbour_pred(nv, cpx, cpy + coy, nb.mv, cbw, cbh, false, h_kind, v_kind, nb_scale); |
| 16690 | obmc_blend_h(pred_v, chroma_side, 0, coy, cbw, cbh, &tmp_v); |
| 16691 | } |
| 16692 | Ok(()) |
| 16693 | } |
| 16694 | |
| 16695 | /// `is_any_masked_compound_used` (libaom `reconinter.h`), specialised to |
| 16696 | /// the square block sizes this decoder ever decodes (32/16/8, all |
| 16697 | /// `min(w,h) >= 8`, `is_comp_ref_allowed` true): `COMPOUND_DIFFWTD` is then |
| 16698 | /// always `is_interinter_compound_used`, so the function is always `true` |
| 16699 | /// for `side` -- kept as a named call (rather than inlined `true`) so a |
| 16700 | /// future sub-8 block size does not silently misdecode a real stream. |
| 16701 | fn is_any_masked_compound_used_here(side: usize) -> bool { |
| 16702 | side.min(side) >= 8 |
| 16703 | } |
| 16704 | |
| 16705 | /// Whether a compound reference pair is unidirectional (both references on |
| 16706 | /// the same temporal side of the current frame) -- `has_uni_comp_refs` |
| 16707 | /// (libaom `av1_reference_frame_utils.h`/`pred_common.c`'s own definition: |
| 16708 | /// a pair is `UNIDIR_COMP_REFERENCE` exactly when neither reference is |
| 16709 | /// backward-while-the-other-is-forward, i.e. `is_backward(ref0) == |
| 16710 | /// is_backward(ref1)`). Used to build a real |
| 16711 | /// [`crate::mvstack::NeighbourRef::uni`] once `ref1` is known, matching |
| 16712 | /// [`crate::mvstack::comp_reference_type_ctx`]'s own `is_backward` calls. |
| 16713 | fn is_uni_comp_ref(ref0: i8, ref1: i8) -> bool { |
| 16714 | let backward = |r: i8| (BWDREF_FRAME..=ALTREF_FRAME).contains(&r); |
| 16715 | backward(ref0) == backward(ref1) |
| 16716 | } |
| 16717 | |
| 16718 | /// `get_comp_group_idx_context` (libaom `pred_common.h`): sums the |
| 16719 | /// above/left neighbour contributions [`Neighbours::record_inter`]/ |
| 16720 | /// [`Neighbours::record_compound_ctx`] already precomputed, clamped to 5. |
| 16721 | /// lane-inter8 r2: `at_mi` is the block's own top-left in 4x4 mi units -- |
| 16722 | /// an 8x8 leaf used to pass its enclosing 16x16's corner here, so all four |
| 16723 | /// leaves read the same neighbour cell (class context-read-from-one-cell). |
| 16724 | fn get_comp_group_idx_context( |
| 16725 | neighbours: &Neighbours, |
| 16726 | at_mi: (usize, usize), |
| 16727 | side: usize, |
| 16728 | ) -> usize { |
| 16729 | let _ = side; |
| 16730 | let (rmi, cmi) = at_mi; |
| 16731 | (neighbours.above_comp_group_idx[cmi] as usize + neighbours.left_comp_group_idx[rmi] as usize) |
| 16732 | .min(5) |
| 16733 | } |
| 16734 | |
| 16735 | /// `get_comp_index_context` (libaom `pred_common.h`): the above/left |
| 16736 | /// neighbour bits plus `3 * (fwd == bck)`, where `fwd`/`bck` are this |
| 16737 | /// block's own two references' distances from the current frame (spec |
| 16738 | /// 7.11.3.15's `get_relative_dist`, same convention |
| 16739 | /// [`crate::compound::dist_wtd_comp_weight_assign`] uses). |
| 16740 | #[allow(clippy::too_many_arguments)] |
| 16741 | fn get_comp_index_context( |
| 16742 | neighbours: &Neighbours, |
| 16743 | at_mi: (usize, usize), |
| 16744 | side: usize, |
| 16745 | order_hint_bits: u32, |
| 16746 | cur_order_hint: u32, |
| 16747 | ref0_order_hint: u32, |
| 16748 | ref1_order_hint: u32, |
| 16749 | ) -> usize { |
| 16750 | let _ = side; |
| 16751 | let (rmi, cmi) = at_mi; |
| 16752 | let fwd = |
| 16753 | crate::motion_field::get_relative_dist(order_hint_bits, ref1_order_hint, cur_order_hint) |
| 16754 | .abs(); |
| 16755 | let bck = |
| 16756 | crate::motion_field::get_relative_dist(order_hint_bits, cur_order_hint, ref0_order_hint) |
| 16757 | .abs(); |
| 16758 | let offset = usize::from(fwd == bck); |
| 16759 | neighbours.above_compound_idx[cmi] as usize |
| 16760 | + neighbours.left_compound_idx[rmi] as usize |
| 16761 | + 3 * offset |
| 16762 | } |
| 16763 | |
| 16764 | fn read_comp_mode( |
| 16765 | dec: &mut SymbolDecoder, |
| 16766 | cdfs: &mut Cdfs, |
| 16767 | above: Option<NeighbourRef>, |
| 16768 | left: Option<NeighbourRef>, |
| 16769 | ) -> bool { |
| 16770 | let ctx = reference_mode_ctx(above, left); |
| 16771 | let compound = dec.symbol(&mut cdfs.comp_mode[ctx]) == 1; |
| 16772 | if compound { |
| 16773 | COMP_MODE_HITS.with(|c| c.set(c.get() + 1)); |
| 16774 | } |
| 16775 | compound |
| 16776 | } |
| 16777 | |
| 16778 | /// Spec 5.11.25's `comp_reference_type` + the `uni_comp_ref`/`comp_ref`/ |
| 16779 | /// `comp_bwdref` trees (libaom `decodemv.c`'s `read_ref_frames` compound |
| 16780 | /// arm), reached once [`read_comp_mode`] has already returned |
| 16781 | /// `COMPOUND_REFERENCE`. Returns `(RefFrame[0], RefFrame[1])`. |
| 16782 | /// `above`/`left` are [`comp_reference_type_ctx`]'s own `NeighbourRef` |
| 16783 | /// convention; `above_ref`/`left_ref` are the scalar `1..=7`/`-1` |
| 16784 | /// convention [`read_single_ref`] already uses, for every downstream binary |
| 16785 | /// decision. libaom `pred_common.c`'s own compound context functions |
| 16786 | /// (`av1_get_pred_context_uni_comp_ref_p[1|2]`, `comp_ref_p[1|2]`, |
| 16787 | /// `comp_bwdref_p[1]`) turned out to be exactly [`single_ref_p1_ctx`].. |
| 16788 | /// [`single_ref_p6_ctx`]/[`uni_comp_ref_p1_ctx`] under a different name |
| 16789 | /// (same vote-the-neighbours-above-left shape, same forward/backward and |
| 16790 | /// LAST/LAST2/LAST3/GOLDEN groupings) -- lane-av1comp audited each one |
| 16791 | /// against the C source rather than porting six near-duplicate functions. |
| 16792 | /// Its caller today only needs the symbols consumed, correctly, to keep the |
| 16793 | /// arithmetic decoder's own state in sync before refusing the block by name |
| 16794 | /// -- the returned pair is not yet threaded into any motion-compensation |
| 16795 | /// path. |
| 16796 | fn read_compound_ref_frames( |
| 16797 | dec: &mut SymbolDecoder, |
| 16798 | cdfs: &mut Cdfs, |
| 16799 | above: Option<NeighbourRef>, |
| 16800 | left: Option<NeighbourRef>, |
| 16801 | above_ref: i8, |
| 16802 | left_ref: i8, |
| 16803 | ) -> (i8, i8) { |
| 16804 | let a = (above_ref > 0).then_some(above_ref); |
| 16805 | let l = (left_ref > 0).then_some(left_ref); |
| 16806 | // libaom `av1_collect_neighbors_ref_counts` counts BOTH of a compound |
| 16807 | // neighbour's references, not just its first -- `a1`/`l1` carry that |
| 16808 | // second reference through to every vote below (lane-av1blend r6: a |
| 16809 | // missing `a1`/`l1` here was decoding `compound_idx` off the wrong CDF |
| 16810 | // row for a block sitting under a skip_mode-forced compound neighbour). |
| 16811 | let a1 = above.and_then(|n| n.ref1); |
| 16812 | let l1 = left.and_then(|n| n.ref1); |
| 16813 | let type_ctx = comp_reference_type_ctx(above, left); |
| 16814 | let unidir = dec.symbol(&mut cdfs.comp_ref_type[type_ctx]) == 0; |
| 16815 | if unidir { |
| 16816 | UNI_COMP_HITS.with(|c| c.set(c.get() + 1)); |
| 16817 | // uni_comp_ref (p0): forward vs. backward -- av1_get_pred_context_- |
| 16818 | // uni_comp_ref_p is single_ref_p1's own forward/backward vote. |
| 16819 | let bit = dec.symbol(&mut cdfs.uni_comp_ref[single_ref_p1_ctx(a, a1, l, l1)][0]); |
| 16820 | if bit == 1 { |
| 16821 | (BWDREF_FRAME, ALTREF_FRAME) |
| 16822 | } else { |
| 16823 | let p1 = dec.symbol(&mut cdfs.uni_comp_ref[uni_comp_ref_p1_ctx(a, a1, l, l1)][1]); |
| 16824 | if p1 == 1 { |
| 16825 | // uni_comp_ref_p2: LAST3 vs. GOLDEN, same vote as single_ref_p5. |
| 16826 | let p2 = dec.symbol(&mut cdfs.uni_comp_ref[single_ref_p5_ctx(a, a1, l, l1)][2]); |
| 16827 | if p2 == 1 { |
| 16828 | (LAST_FRAME, GOLDEN_FRAME) |
| 16829 | } else { |
| 16830 | (LAST_FRAME, LAST3_FRAME) |
| 16831 | } |
| 16832 | } else { |
| 16833 | (LAST_FRAME, LAST2_FRAME) |
| 16834 | } |
| 16835 | } |
| 16836 | } else { |
| 16837 | // comp_ref (p0): LAST/LAST2 vs. LAST3/GOLDEN, same vote as single_ref_p3. |
| 16838 | let bit0 = dec.symbol(&mut cdfs.comp_ref[single_ref_p3_ctx(a, a1, l, l1)][0]); |
| 16839 | let ref0 = if bit0 == 0 { |
| 16840 | // comp_ref_p1: LAST vs. LAST2, same vote as single_ref_p4. |
| 16841 | let p1 = dec.symbol(&mut cdfs.comp_ref[single_ref_p4_ctx(a, a1, l, l1)][1]); |
| 16842 | if p1 == 1 { LAST2_FRAME } else { LAST_FRAME } |
| 16843 | } else { |
| 16844 | // comp_ref_p2: LAST3 vs. GOLDEN, same vote as single_ref_p5. |
| 16845 | let p2 = dec.symbol(&mut cdfs.comp_ref[single_ref_p5_ctx(a, a1, l, l1)][2]); |
| 16846 | if p2 == 1 { GOLDEN_FRAME } else { LAST3_FRAME } |
| 16847 | }; |
| 16848 | // comp_bwdref (p0): BWDREF/ALTREF2 vs. ALTREF, same vote as single_ref_p2. |
| 16849 | let bit1 = dec.symbol(&mut cdfs.comp_bwdref[single_ref_p2_ctx(a, a1, l, l1)][0]); |
| 16850 | let ref1 = if bit1 == 0 { |
| 16851 | // comp_bwdref_p1: BWDREF vs. ALTREF2, same vote as single_ref_p6. |
| 16852 | let p1 = dec.symbol(&mut cdfs.comp_bwdref[single_ref_p6_ctx(a, a1, l, l1)][1]); |
| 16853 | if p1 == 1 { ALTREF2_FRAME } else { BWDREF_FRAME } |
| 16854 | } else { |
| 16855 | ALTREF_FRAME |
| 16856 | }; |
| 16857 | (ref0, ref1) |
| 16858 | } |
| 16859 | } |
| 16860 | |
| 16861 | /// Spec 5.11.24's `compound_mode` (lane-av1comp): which of the eight |
| 16862 | /// `INTER_COMPOUND_MODES` (`NEAREST_NEARESTMV`..`NEW_NEWMV`, returned as |
| 16863 | /// `0..=7`) a `COMPOUND_REFERENCE` block takes, reached once |
| 16864 | /// [`read_comp_mode`] has already returned `COMPOUND_REFERENCE`. `ctx` is |
| 16865 | /// libaom's `av1_mode_context_analyzer`'s compound branch -- |
| 16866 | /// `compound_mode_ctx_map[ref_mv_ctx >> 1][min(new_mv_ctx, COMP_NEWMV_CTXS |
| 16867 | /// - 1)]` (`mvref_common.h`) -- folded from [`crate::mvstack:: |
| 16868 | /// find_mv_stack_compound`]'s own `new_mv_ctx`/`ref_mv_ctx`. The read symbol |
| 16869 | /// is not yet turned into an assigned MV pair or motion-compensated -- |
| 16870 | /// lane-av1comp. |
| 16871 | fn read_inter_compound_mode( |
| 16872 | dec: &mut SymbolDecoder, |
| 16873 | cdfs: &mut Cdfs, |
| 16874 | new_mv_ctx: usize, |
| 16875 | ref_mv_ctx: usize, |
| 16876 | ) -> u8 { |
| 16877 | let ctx = cdf::COMPOUND_MODE_CTX_MAP[ref_mv_ctx >> 1][new_mv_ctx.min(4)]; |
| 16878 | COMPOUND_MODE_HITS.with(|c| c.set(c.get() + 1)); |
| 16879 | dec.symbol(&mut cdfs.inter_compound_mode[ctx]) as u8 |
| 16880 | } |
| 16881 | |
| 16882 | /// spec 7.10.2.10 `assign_mv`'s compound branch, plus the compound half of |
| 16883 | /// `read_drl_idx` (spec 5.11.24) it depends on -- `mode` is |
| 16884 | /// [`read_inter_compound_mode`]'s own `0..=7` (`NEAREST_NEARESTMV`.. |
| 16885 | /// `NEW_NEWMV`, libaom `enums.h` order). Every `GLOBAL_GLOBALMV` side reads |
| 16886 | /// as `(0, 0)` since [`crate::stream::decode_stream`] already refuses any |
| 16887 | /// non-`IDENTITY` global motion model before a tile is ever decoded. |
| 16888 | /// |
| 16889 | /// Ported straight from libaom `decodemv.c`'s `read_drl_idx` (the |
| 16890 | /// `NEW_NEWMV`/`have_nearmv_in_inter_mode` branches) and `assign_mv`/its |
| 16891 | /// caller's `ref_mv[]`/`nearmv[]` setup: the `NEAR_NEWMV`/`NEW_NEARMV` |
| 16892 | /// `1 + ref_mv_idx` special case is folded into `new_idx` below, matching |
| 16893 | /// `read_drl_idx`'s own comment about "offsetting the NEARESTMV mode". |
| 16894 | fn assign_compound_mv( |
| 16895 | dec: &mut SymbolDecoder, |
| 16896 | cdfs: &mut Cdfs, |
| 16897 | comp_stack: &crate::mvstack::CompoundMvStack, |
| 16898 | mode: u8, |
| 16899 | allow_high_precision_mv: bool, |
| 16900 | force_integer_mv: bool, |
| 16901 | // lane-gm r2: `gm_get_motion_vector(ref0)`/`(ref1)` at this block's own |
| 16902 | // position, spec 7.10.2.10's `GLOBAL_GLOBALMV` assignment. |
| 16903 | global_mv: ((i32, i32), (i32, i32)), |
| 16904 | ) -> ((i32, i32), (i32, i32)) { |
| 16905 | const NEAREST_NEARESTMV: u8 = 0; |
| 16906 | const NEAR_NEARMV: u8 = 1; |
| 16907 | const NEAREST_NEWMV: u8 = 2; |
| 16908 | const NEW_NEARESTMV: u8 = 3; |
| 16909 | const NEAR_NEWMV: u8 = 4; |
| 16910 | const NEW_NEARMV: u8 = 5; |
| 16911 | const GLOBAL_GLOBALMV: u8 = 6; |
| 16912 | const NEW_NEWMV: u8 = 7; |
| 16913 | |
| 16914 | let mut ref_mv_idx = 0usize; |
| 16915 | if mode == NEW_NEWMV { |
| 16916 | let mut idx = 0usize; |
| 16917 | while idx < 2 && comp_stack.entries.len() > idx + 1 { |
| 16918 | if dec.symbol(&mut cdfs.drl_mode[comp_stack.drl_ctx[idx]]) == 0 { |
| 16919 | break; |
| 16920 | } |
| 16921 | idx += 1; |
| 16922 | } |
| 16923 | ref_mv_idx = idx; |
| 16924 | } else if matches!(mode, NEAR_NEARMV | NEAR_NEWMV | NEW_NEARMV) { |
| 16925 | let mut idx = 1usize; |
| 16926 | while idx < 3 && comp_stack.entries.len() > idx + 1 { |
| 16927 | if dec.symbol(&mut cdfs.drl_mode[comp_stack.drl_ctx[idx]]) == 0 { |
| 16928 | break; |
| 16929 | } |
| 16930 | idx += 1; |
| 16931 | } |
| 16932 | ref_mv_idx = idx - 1; |
| 16933 | } |
| 16934 | |
| 16935 | let near = comp_stack |
| 16936 | .entries |
| 16937 | .get(ref_mv_idx + 1) |
| 16938 | .map_or(comp_stack.near_mv, |e| (e.mv0, e.mv1)); |
| 16939 | let new_idx = if matches!(mode, NEAR_NEWMV | NEW_NEARMV) { |
| 16940 | ref_mv_idx + 1 |
| 16941 | } else { |
| 16942 | ref_mv_idx |
| 16943 | }; |
| 16944 | let new_pred = comp_stack |
| 16945 | .entries |
| 16946 | .get(new_idx) |
| 16947 | .map_or(comp_stack.nearest_mv, |e| (e.mv0, e.mv1)); |
| 16948 | |
| 16949 | let read_new = |dec: &mut SymbolDecoder, cdfs: &mut Cdfs, base: (i32, i32)| { |
| 16950 | read_mv( |
| 16951 | dec, |
| 16952 | &mut cdfs.mv_comp, |
| 16953 | &mut cdfs.mv_joint, |
| 16954 | base, |
| 16955 | allow_high_precision_mv, |
| 16956 | force_integer_mv, |
| 16957 | ) |
| 16958 | }; |
| 16959 | |
| 16960 | match mode { |
| 16961 | NEAREST_NEARESTMV => comp_stack.nearest_mv, |
| 16962 | NEAR_NEARMV => near, |
| 16963 | NEAREST_NEWMV => (comp_stack.nearest_mv.0, read_new(dec, cdfs, new_pred.1)), |
| 16964 | NEW_NEARESTMV => (read_new(dec, cdfs, new_pred.0), comp_stack.nearest_mv.1), |
| 16965 | NEAR_NEWMV => (near.0, read_new(dec, cdfs, new_pred.1)), |
| 16966 | NEW_NEARMV => (read_new(dec, cdfs, new_pred.0), near.1), |
| 16967 | GLOBAL_GLOBALMV => global_mv, |
| 16968 | _ => ( |
| 16969 | read_new(dec, cdfs, new_pred.0), |
| 16970 | read_new(dec, cdfs, new_pred.1), |
| 16971 | ), |
| 16972 | } |
| 16973 | } |
| 16974 | |
| 16975 | /// Derives this query block's own [`crate::mvstack::GmMvTable`] -- one |
| 16976 | /// `gm_get_motion_vector` (spec 7.10.2.1) result per reference frame, at |
| 16977 | /// THIS block's own `(mi_row, mi_col, bw4, bh4)` (mvstack.rs's own doc: the |
| 16978 | /// table is always computed at the *querying* block's position, never a |
| 16979 | /// donating neighbour's). |
| 16980 | fn build_gm_mv_table( |
| 16981 | global_motion: &[ec_av1_syntax::WarpParams; 7], |
| 16982 | mi_row: usize, |
| 16983 | mi_col: usize, |
| 16984 | bw4: usize, |
| 16985 | bh4: usize, |
| 16986 | allow_high_precision_mv: bool, |
| 16987 | force_integer_mv: bool, |
| 16988 | ) -> crate::mvstack::GmMvTable { |
| 16989 | std::array::from_fn(|i| { |
| 16990 | let gm = &global_motion[i]; |
| 16991 | crate::warp::gm_get_motion_vector( |
| 16992 | gm.model, |
| 16993 | &gm.params, |
| 16994 | mi_row, |
| 16995 | mi_col, |
| 16996 | bw4, |
| 16997 | bh4, |
| 16998 | allow_high_precision_mv, |
| 16999 | force_integer_mv, |
| 17000 | ) |
| 17001 | }) |
| 17002 | } |
| 17003 | |
| 17004 | /// Decodes one square inter-frame block (32x32 whole or 16x16 leaf): skip, |
| 17005 | /// `is_inter`, then either the single-reference/MV-stack/motion-vector chain |
| 17006 | /// and motion-compensated reconstruction, or the inter frame's own intra |
| 17007 | /// path (`Y_MODE` by size group, not `KF_Y_MODE` by neighbour), mirroring |
| 17008 | /// [`crate::tile`]'s `sb_coeff_inter_frame_tile`'s whole-block branch and its |
| 17009 | /// private `write_inter_frame_leaf` -- both share this one function here, |
| 17010 | /// parameterised by `side`/the transform sets/scan tables/size group they |
| 17011 | /// each use. |
| 17012 | /// |
| 17013 | /// # Errors |
| 17014 | /// Returns an error when a symbol names anything this decoder does not |
| 17015 | /// reconstruct: a reference other than `LAST_FRAME`, `GLOBALMV` (round 3; |
| 17016 | /// `NEARESTMV`/`NEARMV`/`NEWMV` are all reconstructed), or an intra |
| 17017 | /// mode/tx-type outside what [`read_intra_mode`]/[`read_coeffs`] already |
| 17018 | /// refuse. |
| 17019 | #[allow(clippy::too_many_arguments)] |
| 17020 | /// lane-inter4 r1: 16-px-cell coordinates -> MI (4-px) coordinates, the unit |
| 17021 | /// [`decode_inter_block`]'s `at` now takes. |
| 17022 | const fn sub16_to_mi(t: (usize, usize)) -> (usize, usize) { |
| 17023 | (t.0 * (SUB / MI), t.1 * (SUB / MI)) |
| 17024 | } |
| 17025 | |
| 17026 | fn decode_inter_block( |
| 17027 | dec: &mut SymbolDecoder, |
| 17028 | cdfs: &mut Cdfs, |
| 17029 | neighbours: &mut Neighbours, |
| 17030 | grid: &mut MiGrid, |
| 17031 | at: (usize, usize), |
| 17032 | side: usize, |
| 17033 | mi_cols: u32, |
| 17034 | mi_rows: u32, |
| 17035 | y: &mut PlaneBuf, |
| 17036 | u: &mut PlaneBuf, |
| 17037 | v: &mut PlaneBuf, |
| 17038 | ref_y: &PlaneBuf, |
| 17039 | ref_u: &PlaneBuf, |
| 17040 | ref_v: &PlaneBuf, |
| 17041 | // lane-av1refs: every non-`LAST_FRAME` reference this frame header's own |
| 17042 | // `ref_frame_idx` names a live DPB slot for, indexed `[ref_frame]` |
| 17043 | // (`LAST2_FRAME`=2 .. `ALTREF_FRAME`=7; index 0/1 unused, `LAST_FRAME` |
| 17044 | // stays on `ref_y`/`u`/`v` above) -- `None` at a live index means the |
| 17045 | // slot's still empty (an error-resilient stream, or too early in the |
| 17046 | // GOP), matching `GOLDEN_FRAME`'s existing empty-slot refusal below. |
| 17047 | other_refs: &RefSlots, |
| 17048 | sign_bias_table: &SignBiasTable, |
| 17049 | // lane-gm r2: this frame header's own `global_motion` table (spec |
| 17050 | // 5.9.24), indexed `[ref_frame - LAST_FRAME]` same as `sign_bias_table` |
| 17051 | // -- feeds `warp::gm_get_motion_vector` for `GLOBALMV`/`GLOBAL_GLOBALMV` |
| 17052 | // assignment and the mv-stack's neighbour substitution. |
| 17053 | global_motion: &[ec_av1_syntax::WarpParams; 7], |
| 17054 | base_q_idx: u8, |
| 17055 | luma_set_intra: TxbSet, |
| 17056 | luma_set_inter: TxbSet, |
| 17057 | chroma_set: TxbSet, |
| 17058 | luma_tx: usize, |
| 17059 | chroma_tx: usize, |
| 17060 | scan_luma: &[u16], |
| 17061 | scan_chroma: &[u16], |
| 17062 | // lane-inter4 r3: superseded by `size_group_wh(write_w, write_h)` at the |
| 17063 | // one site that read it (`y_mode`) -- a rect strip's group comes from its |
| 17064 | // true footprint, not the caller's square `side`. Kept in the signature |
| 17065 | // rather than editing twenty call sites. |
| 17066 | _size_group: usize, |
| 17067 | allow_high_precision_mv: bool, |
| 17068 | force_integer_mv: bool, |
| 17069 | interp_fixed: Option<mc::InterpFilterKind>, |
| 17070 | enable_dual_filter: bool, |
| 17071 | tpl_frame: Option<&TplFrameArgs>, |
| 17072 | // lane-av1comp: this frame header's own `reference_select` bit -- `false` |
| 17073 | // means every block is `SINGLE_REFERENCE` by construction and `comp_mode` |
| 17074 | // is never read (spec 5.11.25); `true` reads `comp_mode` per block and |
| 17075 | // refuses (rather than silently misdecoding) the ones that pick |
| 17076 | // `COMPOUND_REFERENCE`, since two-reference motion compensation is not |
| 17077 | // wired yet. |
| 17078 | reference_select: bool, |
| 17079 | // lane-av1comp: `seq_params`' own masked-compound/distance-weighted- |
| 17080 | // compound enable bits, gating `comp_group_idx`/`compound_idx` |
| 17081 | // (spec 5.11.25) -- always present (unlike `tpl_frame`), since the |
| 17082 | // compound blend weights need this frame's own order hint regardless |
| 17083 | // of `use_ref_frame_mvs`. |
| 17084 | enable_masked_compound: bool, |
| 17085 | // lane-sb128 r4: this sequence header's own `enable_interintra_compound` |
| 17086 | // bit (spec 5.11.24's `interintra` read, libaom `decodemv.c` |
| 17087 | // read_inter_block_mode_info ~1490-1510) -- read right after `assign_mv` |
| 17088 | // and before `read_motion_mode` on the single-ref path below, gated by |
| 17089 | // `is_interintra_allowed` (single-ref, `BLOCK_8X8..=BLOCK_32X32`, and |
| 17090 | // every mode this single-ref branch can produce is already in |
| 17091 | // `SINGLE_INTER_MODE_START..END`); `interintra == 1` is a named refusal |
| 17092 | // (inter+intra blended prediction unimplemented) rather than a |
| 17093 | // mis-decode. |
| 17094 | enable_interintra_compound: bool, |
| 17095 | enable_jnt_comp: bool, |
| 17096 | order_hint_bits: u32, |
| 17097 | order_hint: u32, |
| 17098 | ref_order_hints: [u32; 7], |
| 17099 | // lane-av1comp round 14: this frame header's own `skip_mode_present` bit |
| 17100 | // and `skip_mode_frame` reference pair (spec 5.9.22, `frame.rs`'s |
| 17101 | // `read_skip_mode_params`, already 1-based `MV_REFERENCE_FRAME`) -- |
| 17102 | // `skip_mode` (spec 5.11.23 `read_skip_mode`) is read *before* `skip` |
| 17103 | // when this is `true` (every block this decoder reaches is >= 8x8, so |
| 17104 | // `is_comp_ref_allowed` never gates it further); a `true` result forces |
| 17105 | // `skip_txfm`, `is_inter`, `NEAREST_NEARESTMV` of this pair with no mode/ |
| 17106 | // DRL symbol read, and a plain average blend with no `comp_group_idx`/ |
| 17107 | // `compound_idx` read (libaom `decodemv.c` 1604-1619, 1382-1384, 1509). |
| 17108 | skip_mode_present: bool, |
| 17109 | skip_mode_frame: [u8; 2], |
| 17110 | // lane-motionmode round 1: this frame header's own `is_motion_mode_switchable`/ |
| 17111 | // `allow_warped_motion` bits (spec 5.11.24's `read_motion_mode`). |
| 17112 | switchable_motion_mode: bool, |
| 17113 | allow_warped_motion: bool, |
| 17114 | // lane-warp r5: set by the HORZ_B arm -- the 32x16 top strip is decoded |
| 17115 | // as a square 32x32 block, which over-reads a residual unless coded |
| 17116 | // `skip`; a non-skip strip must refuse cleanly instead of desyncing. |
| 17117 | reject_residual: bool, |
| 17118 | // lane-partitions r1: the strip's own true width/height in pixels -- |
| 17119 | // `side` keeps governing every syntax/CDF/prediction-buffer decision |
| 17120 | // (matching `HORZ_B`'s already-accepted square-context corner-cut), but |
| 17121 | // the final pixel write and this block's own neighbour-context stamp |
| 17122 | // (`record*`/`fill_*_grid`) use `write_w`/`write_h` instead, so a |
| 17123 | // PARTITION_HORZ/VERT strip only claims its own true footprint. `side` |
| 17124 | // for every existing (square) caller. |
| 17125 | write_w: usize, |
| 17126 | write_h: usize, |
| 17127 | // lane-screen r2: this frame header's own `allow_screen_content_tools` |
| 17128 | // bit, threaded to the intra-sub-block branch below -- libaom's |
| 17129 | // `read_intra_block_mode_info` (decodemv.c:1065-1107, the inter-frame |
| 17130 | // counterpart of `read_intra_frame_mode_info`) reads `y_mode_cdf` |
| 17131 | // (never `kf_y_mode_cdf`) and has NO `use_intrabc` call at all (that |
| 17132 | // symbol is intra-frame-only, spec `av1_read_mode_info` dispatches on |
| 17133 | // `frame_is_intra_only`), but it still calls `read_palette_mode_info` |
| 17134 | // under the same `av1_allow_palette` gate as the key-frame path. |
| 17135 | allow_screen_content_tools: bool, |
| 17136 | // lane-superres r9: this frame's own coded luma width (spec 7.11.3.3) -- |
| 17137 | // compared against each reference's stored picture width to decide |
| 17138 | // whether a scaled-MC path (`mc::predict_scaled`) or the ordinary |
| 17139 | // stride-1 one (`mc::predict_with_filters`) applies per reference, and to |
| 17140 | // refuse (rather than mis-decode) the warp/OBMC/interintra/compound |
| 17141 | // combinations that don't implement scaled MC yet. |
| 17142 | frame_width: usize, |
| 17143 | ) -> Result<()> { |
| 17144 | // lane-inter4 r1: `at` is in MI units (4 px) so a 32-level 1:4 strip can |
| 17145 | // name an 8-px offset; `(r, c)` stays the enclosing 16-px cell for the |
| 17146 | // few 16-px-granular uses below. |
| 17147 | let (r, c) = (at.0 / (SUB / MI), at.1 / (SUB / MI)); |
| 17148 | // lane-inter8 r2: the inter side bands are mi-granular; this block's own |
| 17149 | // above/left neighbour is the mi cell at its top-left corner (libaom |
| 17150 | // `above_mbmi` = mi(mi_row - 1, mi_col)), so a SUB-aligned block reads |
| 17151 | // exactly what it read before. |
| 17152 | let (rmi, cmi) = at; |
| 17153 | let (px, py) = (at.1 * MI, at.0 * MI); |
| 17154 | let (cpx, cpy) = (px / 2, py / 2); |
| 17155 | let chroma_side = side / 2; |
| 17156 | let (write_chroma_w, write_chroma_h) = (write_w / 2, write_h / 2); |
| 17157 | |
| 17158 | if std::env::var_os("EC_AV1_TELL").is_some() { |
| 17159 | eprintln!( |
| 17160 | "TELL mi_row={} mi_col={} label=block_entry side={side} tell={} range={}", |
| 17161 | rmi, cmi, dec.debug_bitpos(), dec.debug_state().0 |
| 17162 | ); |
| 17163 | } |
| 17164 | let skip_mode_ctx = |
| 17165 | usize::from(neighbours.above_skip_mode[cmi]) + usize::from(neighbours.left_skip_mode[rmi]); |
| 17166 | // lane-comppin r3: skip_mode desync isolation -- dump ctx + the pre-read |
| 17167 | // MSAC tell so it can be diffed against an equivalent hook in libaom's |
| 17168 | // own `read_skip_mode`/`av1_get_skip_mode_context`. |
| 17169 | if std::env::var_os("EC_AV1_SKIPMODE_DUMP").is_some() { |
| 17170 | eprintln!( |
| 17171 | "EC_SKIPMODE r={r} c={c} ctx={skip_mode_ctx} skip_mode_present={skip_mode_present} tell_before={}", |
| 17172 | dec.debug_bitpos() |
| 17173 | ); |
| 17174 | } |
| 17175 | // spec 5.11.5 `inter_frame_mode_info`: `inter_segment_id(1)` before |
| 17176 | // `skip_mode`/`skip`, `inter_segment_id(0)` after them. |
| 17177 | let (seg_mi_r, seg_mi_c) = (rmi, cmi); |
| 17178 | let (seg_w_mi, seg_h_mi) = (write_w / 4, write_h / 4); |
| 17179 | inter_segment_id(dec, cdfs, seg_mi_r, seg_mi_c, seg_w_mi, seg_h_mi, false, true); |
| 17180 | let skip_mode = skip_mode_present && dec.symbol(&mut cdfs.skip_mode[skip_mode_ctx]) == 1; |
| 17181 | if std::env::var_os("EC_AV1_SKIPMODE_DUMP").is_some() { |
| 17182 | eprintln!( |
| 17183 | "EC_SKIPMODE r={r} c={c} result={skip_mode} tell_after={}", |
| 17184 | dec.debug_bitpos() |
| 17185 | ); |
| 17186 | } |
| 17187 | if skip_mode { |
| 17188 | SKIP_MODE_HITS.with(|c| c.set(c.get() + 1)); |
| 17189 | } |
| 17190 | |
| 17191 | let skip_ctx = usize::from(neighbours.above_skip[cmi]) + usize::from(neighbours.left_skip[rmi]); |
| 17192 | let skip = skip_mode || dec.symbol(&mut cdfs.skip[skip_ctx]) == 1; |
| 17193 | inter_segment_id(dec, cdfs, seg_mi_r, seg_mi_c, seg_w_mi, seg_h_mi, skip, false); |
| 17194 | maybe_read_cdef_idx(dec, rmi, cmi, skip); |
| 17195 | // lane-inter4 r1: spec 5.11.6's `bSize == sbSize` -- a 64x32/64x16 strip |
| 17196 | // is NOT the whole superblock, so its `skip` never suppresses the read. |
| 17197 | let whole_sb = side == 64 && write_w == 64 && write_h == 64; |
| 17198 | maybe_read_delta_q(dec, cdfs, rmi, cmi, whole_sb, skip); |
| 17199 | maybe_read_delta_lf(dec, cdfs, rmi, cmi, whole_sb, skip); |
| 17200 | let (has_above, has_left) = ( |
| 17201 | rmi > neighbours.tile_row0_mi, |
| 17202 | cmi > neighbours.tile_col0_mi, |
| 17203 | ); |
| 17204 | let (above_inter, left_inter) = (neighbours.above_inter[cmi], neighbours.left_inter[rmi]); |
| 17205 | let ii_ctx = intra_inter_ctx(has_above, has_left, above_inter, left_inter); |
| 17206 | let is_inter = skip_mode || dec.symbol(&mut cdfs.intra_inter[ii_ctx]) == 1; |
| 17207 | if std::env::var_os("EC_TRACE_MODE").is_some() { |
| 17208 | eprintln!( |
| 17209 | "EC_MODE mi_row={} mi_col={} rng={}", |
| 17210 | rmi, |
| 17211 | cmi, |
| 17212 | dec.debug_state().0 |
| 17213 | ); |
| 17214 | } |
| 17215 | // lane-warp r5: see `reject_residual` -- a square-block decode of a |
| 17216 | // HORZ_B strip is symbol-exact only for `skip` (no residual, no |
| 17217 | // motion_mode/warp symbol). lane-intrarect r1 MOVED this gate from just |
| 17218 | // after the `skip` read to here, past `is_inter` (class |
| 17219 | // [[refusal-short-circuits-its-own-code]]: at the old place it refused |
| 17220 | // 24/24 attempts of this lane's own gate before the intra arm below |
| 17221 | // could run). An INTRA strip with its true `write_w`x`write_h` footprint |
| 17222 | // now codes its residual rectangularly ([`decode_intra_rect_in_inter`]); |
| 17223 | // an INTER one, and a strip decoded with square dimensions anyway, still |
| 17224 | // cannot. |
| 17225 | // merge b51719f+lane-intrarect: main's `rect_inter_residual_supported` |
| 17226 | // term stays -- lane-inter4's 16x8/8x16 inter leaves DO code a |
| 17227 | // rectangular residual, and the lane's condition (written before that |
| 17228 | // landed) refused every one of them. |
| 17229 | if reject_residual |
| 17230 | && !skip |
| 17231 | && !rect_inter_residual_supported(write_w, write_h) |
| 17232 | && (is_inter || (write_w == side && write_h == side)) |
| 17233 | { |
| 17234 | return Err(unsupported( |
| 17235 | "a non-skip rectangular (HORZ/VERT/HORZ_B) strip needs rectangular residual coding", |
| 17236 | )); |
| 17237 | } |
| 17238 | if std::env::var_os("EC_AV1_TELL").is_some() { |
| 17239 | eprintln!( |
| 17240 | "TELL mi_row={} mi_col={} label=post_is_inter skip_mode={skip_mode} skip={skip} is_inter={is_inter} tell={} range={}", |
| 17241 | rmi, cmi, dec.debug_bitpos(), dec.debug_state().0 |
| 17242 | ); |
| 17243 | } |
| 17244 | |
| 17245 | let mode_for_tx; |
| 17246 | let uv_predict_mode; |
| 17247 | let (luma_grid, u_grid, v_grid); |
| 17248 | let block_filter; |
| 17249 | let ref_frame_for_lf; |
| 17250 | let globalmv_for_lf; |
| 17251 | // lane-av1comp: `Some((comp_group_idx, compound_idx))` only for a real |
| 17252 | // `COMPOUND_REFERENCE` block -- applied to `neighbours` *after* the |
| 17253 | // common `record_inter` call below (which would otherwise stamp the |
| 17254 | // single-ref ALTREF-special-case default over it). |
| 17255 | let mut compound_ctx: Option<(i8, u8, u8)> = None; |
| 17256 | // lane-txselect: `read_block_tx_size`'s leaf transform units (spec |
| 17257 | // 5.11.17), `Some` only when this block's var-tx tree resolved to more |
| 17258 | // than one luma transform -- read back by the residual loop, the |
| 17259 | // coefficient-context write-back and the loop-filter grid fill below. |
| 17260 | let mut vartx_leaves: Option<Vec<(usize, usize, usize, usize)>> = None; |
| 17261 | let at_mi = at; |
| 17262 | if is_inter { |
| 17263 | let above_nbr = has_above.then(|| NeighbourRef { |
| 17264 | is_inter: above_inter, |
| 17265 | ref0: if above_inter { |
| 17266 | neighbours.above_ref[cmi] |
| 17267 | } else { |
| 17268 | 0 |
| 17269 | }, |
| 17270 | ref1: neighbours.above_ref1[cmi], |
| 17271 | uni: neighbours.above_ref1[cmi] |
| 17272 | .is_some_and(|r1| is_uni_comp_ref(neighbours.above_ref[cmi], r1)), |
| 17273 | }); |
| 17274 | let left_nbr = has_left.then(|| NeighbourRef { |
| 17275 | is_inter: left_inter, |
| 17276 | ref0: if left_inter { |
| 17277 | neighbours.left_ref[rmi] |
| 17278 | } else { |
| 17279 | 0 |
| 17280 | }, |
| 17281 | ref1: neighbours.left_ref1[rmi], |
| 17282 | uni: neighbours.left_ref1[rmi] |
| 17283 | .is_some_and(|r1| is_uni_comp_ref(neighbours.left_ref[rmi], r1)), |
| 17284 | }); |
| 17285 | if std::env::var_os("EC_AV1_COMPIDX_DUMP").is_some() { |
| 17286 | eprintln!( |
| 17287 | "EC_PRECOMP r={r} c={c} skip_mode={skip_mode} skip={skip} reference_select={reference_select} tell={}", |
| 17288 | dec.debug_bitpos() |
| 17289 | ); |
| 17290 | } |
| 17291 | let is_compound = |
| 17292 | skip_mode || (reference_select && read_comp_mode(dec, cdfs, above_nbr, left_nbr)); |
| 17293 | if is_compound { |
| 17294 | let (ref0, ref1) = if skip_mode { |
| 17295 | (skip_mode_frame[0] as i8, skip_mode_frame[1] as i8) |
| 17296 | } else { |
| 17297 | read_compound_ref_frames( |
| 17298 | dec, |
| 17299 | cdfs, |
| 17300 | above_nbr, |
| 17301 | left_nbr, |
| 17302 | neighbours.above_ref[cmi], |
| 17303 | neighbours.left_ref[rmi], |
| 17304 | ) |
| 17305 | }; |
| 17306 | let (mi_row, mi_col) = (rmi, cmi); |
| 17307 | // lane-rect r2: true footprint, mirroring the single-ref path below. |
| 17308 | let bw4 = write_w / 4; |
| 17309 | let bh4 = write_h / 4; |
| 17310 | // spec 7.10.2.8, doubled per side (`CompoundTplArgs`): same |
| 17311 | // `use_ref_frame_mvs` gating as the single-ref `tpl` build |
| 17312 | // below, just one `get_relative_dist` per reference. |
| 17313 | let comp_tpl = tpl_frame.map(|t| crate::mvstack::CompoundTplArgs { |
| 17314 | field: t.field, |
| 17315 | cur_offset_0: crate::motion_field::get_relative_dist( |
| 17316 | t.order_hint_bits, |
| 17317 | t.order_hint, |
| 17318 | t.ref_order_hints[(ref0 - LAST_FRAME) as usize], |
| 17319 | ), |
| 17320 | cur_offset_1: crate::motion_field::get_relative_dist( |
| 17321 | t.order_hint_bits, |
| 17322 | t.order_hint, |
| 17323 | t.ref_order_hints[(ref1 - LAST_FRAME) as usize], |
| 17324 | ), |
| 17325 | allow_high_precision_mv, |
| 17326 | }); |
| 17327 | let gm_table = build_gm_mv_table( |
| 17328 | global_motion, |
| 17329 | mi_row, |
| 17330 | mi_col, |
| 17331 | bw4, |
| 17332 | bh4, |
| 17333 | allow_high_precision_mv, |
| 17334 | force_integer_mv, |
| 17335 | ); |
| 17336 | let comp_stack = crate::mvstack::find_mv_stack_compound( |
| 17337 | grid, |
| 17338 | mi_row, |
| 17339 | mi_col, |
| 17340 | bw4, |
| 17341 | bh4, |
| 17342 | (ref0, ref1), |
| 17343 | mi_cols as usize, |
| 17344 | mi_rows as usize, |
| 17345 | sign_bias_table, |
| 17346 | &gm_table, |
| 17347 | comp_tpl, |
| 17348 | ); |
| 17349 | let compound_mode = if skip_mode { |
| 17350 | 0 // NEAREST_NEARESTMV, forced -- no mode symbol read |
| 17351 | } else { |
| 17352 | read_inter_compound_mode(dec, cdfs, comp_stack.new_mv_ctx, comp_stack.ref_mv_ctx) |
| 17353 | }; |
| 17354 | let (mv0, mv1) = assign_compound_mv( |
| 17355 | dec, |
| 17356 | cdfs, |
| 17357 | &comp_stack, |
| 17358 | compound_mode, |
| 17359 | allow_high_precision_mv, |
| 17360 | force_integer_mv, |
| 17361 | (gm_table[(ref0 - LAST_FRAME) as usize], gm_table[(ref1 - LAST_FRAME) as usize]), |
| 17362 | ); |
| 17363 | if std::env::var_os("EC_TRACE_MODE").is_some() { |
| 17364 | eprintln!( |
| 17365 | "EC_MODE_VAL mi_row={mi_row} mi_col={mi_col} mode={} ref0={ref0} ref1={ref1} mv0=({},{}) stack={} rng={}", |
| 17366 | compound_mode + 17, |
| 17367 | mv0.0, |
| 17368 | mv0.1, |
| 17369 | comp_stack.entries.len(), |
| 17370 | dec.debug_state().0 |
| 17371 | ); |
| 17372 | for (i, e) in comp_stack.entries.iter().enumerate() { |
| 17373 | eprintln!( |
| 17374 | "EC_STACK mi_row={mi_row} mi_col={mi_col} i={i} this=({},{}) comp=({},{}) w={}", |
| 17375 | e.mv0.0, e.mv0.1, e.mv1.0, e.mv1.1, e.weight |
| 17376 | ); |
| 17377 | } |
| 17378 | } |
| 17379 | let is_globalmv = compound_mode == 6; // GLOBAL_GLOBALMV |
| 17380 | // spec `is_nontrans_global_motion`: ALL active refs' models must |
| 17381 | // be non-TRANSLATION (IDENTITY counts) for the compound block's |
| 17382 | // interp-filter read to be suppressed. |
| 17383 | let gm_nontrans = is_globalmv |
| 17384 | && global_motion[(ref0 - LAST_FRAME) as usize].model != ec_av1_syntax::WarpModel::Translation |
| 17385 | && global_motion[(ref1 - LAST_FRAME) as usize].model != ec_av1_syntax::WarpModel::Translation; |
| 17386 | let (py0, pu0, pv0) = ref_planes(ref0, ref_y, ref_u, ref_v, other_refs)?; |
| 17387 | let (py1, pu1, pv1) = ref_planes(ref1, ref_y, ref_u, ref_v, other_refs)?; |
| 17388 | // lane-cwarp r1: `is_global_mv_block` (blockd.h:421-429) is |
| 17389 | // PER REFERENCE SLOT -- mode GLOBAL_GLOBALMV, block >= 8x8, and |
| 17390 | // THAT slot's own gm model > TRANSLATION. `bw4`/`bh4` come from |
| 17391 | // the write extent below, so recompute the size bound from |
| 17392 | // `side` here (the block's own size, what libaom's `bsize` |
| 17393 | // is). |
| 17394 | let is_global_mv0 = is_globalmv |
| 17395 | && side >= 8 |
| 17396 | && global_motion[(ref0 - LAST_FRAME) as usize].model as u8 > 1; |
| 17397 | let is_global_mv1 = is_globalmv |
| 17398 | && side >= 8 |
| 17399 | && global_motion[(ref1 - LAST_FRAME) as usize].model as u8 > 1; |
| 17400 | // libaom `av1_init_warp_params` + `allow_warp`'s |
| 17401 | // `global_warp_allowed` branch, run once per reference for a |
| 17402 | // compound block exactly as for a single-ref one (reconinter.c |
| 17403 | // `build_inter_predictors_8x8_and_bigger` loops over `ref` and |
| 17404 | // calls `av1_init_warp_params` inside the loop) -- WARPED_CAUSAL |
| 17405 | // never reaches here (spec 5.11.27 / libaom `read_motion_mode`: |
| 17406 | // `has_second_ref` returns SIMPLE_TRANSLATION), so the local-warp |
| 17407 | // arm of `allow_warp` is dead on this path and only the global |
| 17408 | // one applies. `force_integer_mv` short-circuits |
| 17409 | // `av1_init_warp_params` before `allow_warp`. |
| 17410 | let compound_warp = |ref_frame: i8, eligible: bool| { |
| 17411 | if eligible |
| 17412 | && !force_integer_mv |
| 17413 | && !global_motion[(ref_frame - LAST_FRAME) as usize].invalid |
| 17414 | { |
| 17415 | crate::warp::global_warp_params( |
| 17416 | global_motion[(ref_frame - LAST_FRAME) as usize].params, |
| 17417 | ) |
| 17418 | } else { |
| 17419 | None |
| 17420 | } |
| 17421 | }; |
| 17422 | let warp0 = compound_warp(ref0, is_global_mv0); |
| 17423 | let warp1 = compound_warp(ref1, is_global_mv1); |
| 17424 | if warp0.is_some() || warp1.is_some() { |
| 17425 | COMPOUND_WARP_HITS.with(|c| c.set(c.get() + 1)); |
| 17426 | } |
| 17427 | // spec `get_ref_filter_type`: matches when EITHER of the |
| 17428 | // neighbour's two references equals this block's own ref0 -- |
| 17429 | // `above_ref1`/`left_ref1` is the neighbour's real second |
| 17430 | // reference (`record_compound_ctx`) when it was itself a |
| 17431 | // compound block; checking `above_ref`/`left_ref` alone drops a |
| 17432 | // real filter match down to the "no neighbour" sentinel |
| 17433 | // whenever the shared reference sits in the neighbour's SECOND |
| 17434 | // slot (lane-av1idx r2). |
| 17435 | let above_filter_ctx = if neighbours.above_ref[cmi] == ref0 |
| 17436 | || neighbours.above_ref1[cmi] == Some(ref0) |
| 17437 | { |
| 17438 | neighbours.above_filter[cmi] |
| 17439 | } else { |
| 17440 | [3, 3] |
| 17441 | }; |
| 17442 | let left_filter_ctx = if neighbours.left_ref[rmi] == ref0 |
| 17443 | || neighbours.left_ref1[rmi] == Some(ref0) |
| 17444 | { |
| 17445 | neighbours.left_filter[rmi] |
| 17446 | } else { |
| 17447 | [3, 3] |
| 17448 | }; |
| 17449 | globalmv_for_lf = is_globalmv; |
| 17450 | ref_frame_for_lf = ref0; |
| 17451 | mode_for_tx = 0; |
| 17452 | uv_predict_mode = DC_PRED; |
| 17453 | // lane-rect r2: `grid` (mvstack.rs's own MiGrid, distinct from |
| 17454 | // `neighbours`) must be stamped with the block's true footprint |
| 17455 | // too -- a square `bw4`x`bw4` stamp here claims a rect strip's |
| 17456 | // NEXT strip's own rows/cols before it decodes, corrupting |
| 17457 | // every later mvstack scan that reads this cell back. |
| 17458 | for dr in 0..bh4 { |
| 17459 | for dc in 0..bw4 { |
| 17460 | grid.set( |
| 17461 | mi_row + dr, |
| 17462 | mi_col + dc, |
| 17463 | MiInfo { |
| 17464 | is_inter: true, |
| 17465 | ref_frame: ref0, |
| 17466 | ref_frame1: Some(ref1), |
| 17467 | mv1: Some(mv1), |
| 17468 | mv: mv0, |
| 17469 | is_new_mv: matches!(compound_mode, 2 | 3 | 4 | 5 | 7), |
| 17470 | size: bw4, |
| 17471 | size_h: bh4, |
| 17472 | // libaom `is_global_mv_block`: per-ref-slot, mode |
| 17473 | // GLOBAL_GLOBALMV, block >= 8x8, and THAT slot's |
| 17474 | // own gm model > TRANSLATION (IDENTITY excluded |
| 17475 | // too -- the two-predicate trap, distinct from |
| 17476 | // `gm_nontrans` above which allows IDENTITY). |
| 17477 | is_global_mv0, |
| 17478 | is_global_mv1, |
| 17479 | }, |
| 17480 | ); |
| 17481 | } |
| 17482 | } |
| 17483 | // spec 5.11.25's `comp_group_idx`/`compound_idx`: only read when |
| 17484 | // masked compound / distance-weighted compound are actually |
| 17485 | // enabled for this stream -- `is_any_masked_compound_used_here` |
| 17486 | // is `true` for every block size this decoder ever reaches |
| 17487 | // (`min(side,side) >= 8`, `COMPOUND_DIFFWTD` always eligible |
| 17488 | // there), so the gate reduces to the sequence header bit; kept |
| 17489 | // as an explicit call so a future smaller block size is not |
| 17490 | // silently wrong (libaom `reconinter.h::is_any_masked_compound_used`). |
| 17491 | let group_ctx = get_comp_group_idx_context(neighbours, (rmi, cmi), side); |
| 17492 | let comp_group_idx = if !skip_mode |
| 17493 | && enable_masked_compound |
| 17494 | && is_any_masked_compound_used_here(side) |
| 17495 | { |
| 17496 | dec.symbol(&mut cdfs.comp_group_idx[group_ctx]) |
| 17497 | } else { |
| 17498 | 0 |
| 17499 | }; |
| 17500 | // lane-maskcomp r2 / lane-wedge r3: `Some(mask_type)` for a |
| 17501 | // `COMPOUND_DIFFWTD` block, `Some(mask)` (wedge codebook lookup) |
| 17502 | // for a `COMPOUND_WEDGE` block -- exactly one of the two is set |
| 17503 | // when `comp_group_idx == 1`. |
| 17504 | let mut diffwtd_mask_type: Option<u8> = None; |
| 17505 | let mut wedge_mask: Option<&'static [u8]> = None; |
| 17506 | if comp_group_idx == 1 { |
| 17507 | // lane-maskcomp r1: `read_compound_type` (spec 5.11.25, |
| 17508 | // libaom `decodemv.c` 1634-1656). `is_interinter_compound_used |
| 17509 | // (COMPOUND_WEDGE, bsize)` (`av1_is_wedge_used`, |
| 17510 | // `av1_wedge_params_lookup`) is true for every square bsize |
| 17511 | // this leaf reaches (8x8/16x16/32x32 all have |
| 17512 | // `wedge_types > 0`), so `compound_type` always reads a real |
| 17513 | // symbol here -- no alphabet collapse to worry about at this |
| 17514 | // block-size family. |
| 17515 | // lane-mcomp64 r1: `av1_is_wedge_used` is false at 64x64 |
| 17516 | // (`av1_wedge_params_lookup[BLOCK_64X64].wedge_types == 0`), |
| 17517 | // so libaom's `read_compound_type` (decodemv.c 1634-1656) |
| 17518 | // writes NO `compound_type` symbol there and INFERS |
| 17519 | // `COMPOUND_DIFFWTD` -- only the 1-bit `mask_type` literal |
| 17520 | // follows. `None` here is exactly that inferred arm. |
| 17521 | let wedge_bsize = match side { |
| 17522 | 8 => Some(3), |
| 17523 | 16 => Some(6), |
| 17524 | 32 => Some(9), |
| 17525 | _ => None, |
| 17526 | }; |
| 17527 | let compound_type = match wedge_bsize { |
| 17528 | Some(b) => dec.symbol(&mut cdfs.compound_type[b]), |
| 17529 | None => 1, |
| 17530 | }; |
| 17531 | if let Some(wedge_bsize) = wedge_bsize.filter(|_| compound_type == 0) { |
| 17532 | // COMPOUND_WEDGE: lane-wedge r3, codebook checksum- |
| 17533 | // verified vs independent C dump (wedge.rs). |
| 17534 | let wedge_index = dec.symbol(&mut cdfs.wedge_idx[wedge_bsize]); |
| 17535 | let wedge_sign = dec.literal(1); |
| 17536 | WEDGE_HITS.with(|c| c.set(c.get() + 1)); |
| 17537 | wedge_mask = Some( |
| 17538 | crate::wedge::wedge_masks() |
| 17539 | .codebook(side) |
| 17540 | .mask(wedge_sign as usize, wedge_index as usize), |
| 17541 | ); |
| 17542 | } else { |
| 17543 | // COMPOUND_DIFFWTD: `mask_type`, MAX_DIFFWTD_MASK_BITS==1. |
| 17544 | let mask_type = dec.literal(1); |
| 17545 | diffwtd_mask_type = Some(mask_type as u8); |
| 17546 | if wedge_bsize.is_none() { |
| 17547 | DIFFWTD_INFERRED_HITS.with(|c| c.set(c.get() + 1)); |
| 17548 | } |
| 17549 | } |
| 17550 | MASKED_COMPOUND_HITS.with(|c| c.set(c.get() + 1)); |
| 17551 | } |
| 17552 | // corner-cut (lane-av1comp r16/r17, lane-av1blend r1): r16/r17 |
| 17553 | // called this a `predict_compound_intermediate`/`combine_compound` |
| 17554 | // reconstruction defect. r1 FALSIFIED that hypothesis outright: |
| 17555 | // unmasking and running the real gates |
| 17556 | // (a_real_aomenc_stream_with_reference_select_reads_comp_mode_correctly, |
| 17557 | // a_real_aomenc_stream_with_compound_references_decodes_pixel_exact) |
| 17558 | // shows the plain-average blend is bit-exact *most of the time* |
| 17559 | // -- `combine_compound`'s two-shift-in-one round2 was proven |
| 17560 | // algebraically identical to libaom's separate |
| 17561 | // `>>DIST_PRECISION_BITS`-then-`ROUND_POWER_OF_TWO` sequence |
| 17562 | // (av1_dist_wtd_convolve_2d_c, convolve.c:402-415) for every |
| 17563 | // input, and a whole-pel zero-MV compound block (mv0=mv1=(0,0), |
| 17564 | // the common case a lag-in-frames GOP's hidden altref frames |
| 17565 | // hit) reproduces aomdec's own pre-deblock dump byte-for-byte |
| 17566 | // for several consecutive frames. |
| 17567 | // |
| 17568 | // The real defect a live gate run isolated (seed 49, |
| 17569 | // `fixtures/av1blend-r1-mismatch.obu`): decode-order frame 0 |
| 17570 | // (keyframe) and frame 1 (first hidden altref, `ref1 == |
| 17571 | // ALTREF_FRAME`) match aomdec's own pre-deblock dump byte for |
| 17572 | // byte; decode-order frame 2 -- same shape, same zero MV, only |
| 17573 | // difference `ref1 == BWDREF_FRAME` instead of `ALTREF_FRAME` -- |
| 17574 | // is the *first* frame to diverge (881/6144 luma+chroma bytes). |
| 17575 | // Every later frame up through decode-order 9 also mismatches, |
| 17576 | // *including* frame 8, whose four blocks are back to |
| 17577 | // `ref1 == ALTREF_FRAME` only -- ruling out "BWDREF_FRAME's MC |
| 17578 | // math is wrong" (that block's own math is proven exact) in |
| 17579 | // favour of "the picture sitting in some DPB slot is already |
| 17580 | // wrong by the time frame 2 reads it, and the corruption |
| 17581 | // propagates through refresh_frame_flags into every later |
| 17582 | // reference regardless of which named ref a later block picks". |
| 17583 | // Frames 10 through 18 (once the GOP's lineage stops tracing |
| 17584 | // back through the bad slot) are bit-exact again. This points at |
| 17585 | // reference-slot bookkeeping (`ref_slots`/`refresh_frame_flags` |
| 17586 | // in `stream.rs`'s `decode_stream`, or `ref_order_hints`/DPB |
| 17587 | // indexing feeding `dist_wtd_comp_weight_assign`) around |
| 17588 | // BWDREF_FRAME specifically, not at the MC/blend arithmetic in |
| 17589 | // mc.rs. Not isolated further within lane-av1blend's budget -- |
| 17590 | // refuse by name rather than ship a blend that is right only |
| 17591 | // sometimes. Ceiling: r13/r14's MC plumbing above this line |
| 17592 | // stays -- only the plain-average reconstruct is masked off |
| 17593 | // again. Upgrade: instrument `ref_slots`/`refresh_frame_flags` |
| 17594 | // per decode-order frame (which slot each `Frame` OBU refreshes, |
| 17595 | // and which slot each `ref_frame_idx[BWDREF_FRAME - LAST_FRAME]` |
| 17596 | // reads) against aomdec's own `RefCntBuffer` bookkeeping on |
| 17597 | // `fixtures/av1blend-r1-mismatch.obu`, starting at decode-order |
| 17598 | // frame 2. |
| 17599 | // |
| 17600 | // r2 (fresh fixture, seed 45, same recipe -- the original r1 |
| 17601 | // fixture is gitignored and could not be regenerated byte-exact): |
| 17602 | // static review of `stream.rs`'s `ref_slots`/`refresh_frame_flags`/ |
| 17603 | // `cdf_slots` bookkeeping and the `show_existing_frame` refresh |
| 17604 | // arm (spec 7.21: only refreshes on a shown KEY frame, which the |
| 17605 | // parser already enforces at `frame.rs:791/815` -- `refresh_frame_flags` |
| 17606 | // is forced 0 on every non-key `show_existing_frame`) turned up |
| 17607 | // nothing wrong; `other_refs`/`ref_planes` slot indexing for |
| 17608 | // `BWDREF_FRAME` is spec-correct too. A live unmasked run |
| 17609 | // isolated a *different, smaller* shape than r1's: decode-order |
| 17610 | // frame 3 (a `ref1 == BWDREF_FRAME`, zero-MV, simple-average |
| 17611 | // compound block) is the first to diverge from ffmpeg (worst |
| 17612 | // delta 1, luma only, clustered at 32-pixel block edges) even |
| 17613 | // though BOTH its compound inputs -- the key frame and |
| 17614 | // decode-order frame 2's own picture (`ref1 == ALTREF_FRAME`, |
| 17615 | // itself proven bit-exact: it is later re-shown via |
| 17616 | // `show_existing_frame` and matches ffmpeg exactly) -- are |
| 17617 | // independently pixel-exact, and the blend arithmetic is the |
| 17618 | // same proven-exact formula. That rules out "wrong slot"/"stale |
| 17619 | // picture" for this fixture: the inputs are right, so the defect |
| 17620 | // is in decode-order frame 3's *own* per-block decode (entropy |
| 17621 | // desync from compound-CDF adaptation carried across a slot, or |
| 17622 | // the compound mode/MV read itself) -- not in `ref_slots`/ |
| 17623 | // `refresh_frame_flags`. Not isolated further within budget. |
| 17624 | // Upgrade: bisect decode-order frame 3's tile decode against a |
| 17625 | // synced `EC_TOK`/`EC_PART` trace from the instrumented aomdec |
| 17626 | // build (`/tmp/libaom-src/build/decoder-debug/aomdec`) on |
| 17627 | // `fixtures/av1blend-r1-mismatch.obu` (seed 45 now, not seed 49) |
| 17628 | // -- the pre-deblock dump frame-index alignment between this |
| 17629 | // crate and that aomdec build is NOT 1:1 on this fixture (23 |
| 17630 | // dumps here vs 24 there), so calibrate that first. |
| 17631 | // r3 (this round): pre-deblock buffers were bisected byte-for-byte |
| 17632 | // against an instrumented aomdec build (frame-index alignment |
| 17633 | // recalibrated via content-hash matching rather than the raw |
| 17634 | // dump count -- the keyframe never calls a pre-deblock dump at |
| 17635 | // all, since only `decode_inter_frame_tile` does, which is the |
| 17636 | // whole "23 vs 24" gap, not a real bug). |
| 17637 | // Decode-order frame 3 (`fixtures/av1blend-r1-mismatch.obu`, |
| 17638 | // seed 45) is still the first divergence: PRE-deblock differs |
| 17639 | // from aomdec's own pre-deblock dump by up to 1, on ~20% of |
| 17640 | // luma pixels, clustered where its two compound inputs' pixel |
| 17641 | // values disagree by more than a couple of levels -- both |
| 17642 | // inputs (the keyframe and decode-order frame 2's own picture) |
| 17643 | // are independently proven bit-exact (pre- and post-deblock) |
| 17644 | // against aomdec, and `decodemv`'s own EC_TRACE (mode/mv/skip) |
| 17645 | // matches this decoder's block loop exactly for every block in |
| 17646 | // this frame. Solving `aomval = (key*w0 + bwd*w1 + 8) >> 4` for |
| 17647 | // every mismatching pixel against `key`/`bwd` (both known-exact) |
| 17648 | // picks out `w0=9, w1=7` for the majority (525/846) with zero |
| 17649 | // matches for the plain average this decoder actually emits or |
| 17650 | // the reversed `w0=7, w1=9` -- i.e. `compound_idx` is being |
| 17651 | // decoded as `1` (simple average, spec `COMPOUND_AVERAGE`) when |
| 17652 | // the real bitstream encoded `0` (`COMPOUND_DISTWTD`, |
| 17653 | // `dist_wtd_comp_weight_assign`'s own weights for this frame's |
| 17654 | // order-hint distances 2/3 land on exactly `(9, 7)` -- this |
| 17655 | // decoder's own `dist_wtd_comp_weight_assign` was independently |
| 17656 | // re-verified against `QUANT_DIST_WEIGHT`/`QUANT_DIST_LOOKUP_TABLE` |
| 17657 | // by hand for these distances and gives the same `(9, 7)`). |
| 17658 | // `get_comp_index_context` was checked line-for-line against |
| 17659 | // libaom's `pred_common.h` (the `fwd`/`bck` distance halves, the |
| 17660 | // `above_mi`/`left_mi` `has_second_ref`/`ALTREF_FRAME` fallback, |
| 17661 | // the `3 * offset` term) and matches; `Default_Compound_Idx_Cdf` |
| 17662 | // (`cdf.rs`'s `COMPOUND_IDX`) matches `entropymode.c`'s |
| 17663 | // `default_compound_idx_cdfs` value-for-value; the read is |
| 17664 | // gated on `!skip_mode && enable_jnt_comp`, matching libaom's |
| 17665 | // `has_second_ref(mbmi) && !mbmi->skip_mode` outer guard plus |
| 17666 | // `order_hint_info.enable_dist_wtd_comp` inner guard. The |
| 17667 | // remaining 321/846 mismatched pixels do NOT fit `(9, 7)` |
| 17668 | // either -- some blocks in this frame plausibly decode a |
| 17669 | // genuinely different (correct, context-dependent) compound_idx |
| 17670 | // than block (0,0)'s own -- so this is not simply "always wrong |
| 17671 | // the same way": table/context/gate are all individually |
| 17672 | // verified correct, yet the decoded VALUE at this specific |
| 17673 | // symbol is wrong at least once in this frame. Not isolated |
| 17674 | // further within budget. Upgrade: instrument `dec.symbol`'s own |
| 17675 | // range/bit state (`rng`/`tell`, matching aomdec's own EC_PART |
| 17676 | // trace fields) immediately before and after the `compound_idx` |
| 17677 | // read specifically, not just the surrounding mode/mv/skip |
| 17678 | // trace which does match -- the desync (if any) is local to |
| 17679 | // this one read, not a wider stream desync. |
| 17680 | // |
| 17681 | // r5 (this round): the `resolve_interp_filter` call used to sit |
| 17682 | // right here, ahead of `comp_group_idx`/`compound_idx` -- spec |
| 17683 | // 5.11.19/libaom `decodemv.c:1575` reads `interp_filter` AFTER |
| 17684 | // `read_compound_type`, not before, so that was a genuine |
| 17685 | // ordering bug for a Switchable-filter frame (moved below, past |
| 17686 | // the `compound_ctx = Some(...)` line). It does NOT explain this |
| 17687 | // fixture's own mismatch, though: `av1blend-r1-mismatch.obu`'s |
| 17688 | // header carries a *fixed* interpolation filter |
| 17689 | // (`interp_fixed = Some(Regular)`, confirmed live via a |
| 17690 | // one-off dump), so `resolve_interp_filter` never read a symbol |
| 17691 | // in either position -- zero bits moved, decoded values |
| 17692 | // unchanged before and after the reorder. Kept anyway: it is a |
| 17693 | // real spec-order fix for the next Switchable-filter compound |
| 17694 | // stream, independently correct regardless of this fixture. |
| 17695 | // |
| 17696 | // Also ruled out this round: comparing this crate's own |
| 17697 | // `SymbolDecoder::debug_bitpos()` against aomdec's |
| 17698 | // `aom_reader_tell()` at the block boundary is NOT a valid |
| 17699 | // desync probe -- they are different quantities (raw consumed |
| 17700 | // input bits vs. an entropy-cost estimate that advances without |
| 17701 | // a literal byte fetch), so equal/unequal bitpos proves nothing |
| 17702 | // either way. Do not repeat that comparison; instead trace |
| 17703 | // `range`/`value` (this crate) against libaom's own internal |
| 17704 | // `dif`/`rng` (needs new instrumentation in both) immediately |
| 17705 | // after each read *upstream* of this one in the same block: |
| 17706 | // `read_comp_mode`, `read_compound_ref_frames` (comp_ref_type/ |
| 17707 | // uni_comp_ref/comp_ref/comp_bwdref), `read_inter_compound_mode`, |
| 17708 | // and the DRL loop. All of those independently decoded the SAME |
| 17709 | // values this crate and aomdec both show (ref0=1, ref1=5, mode |
| 17710 | // NEAREST_NEARESTMV, mv=(0,0)) -- but an upstream *context* |
| 17711 | // miscomputation can still decode the identical symbol value off |
| 17712 | // a slightly different CDF, narrowing `range` by a different |
| 17713 | // amount than aomdec without changing the decoded value itself, |
| 17714 | // and only surface as a wrong decode a few reads later exactly |
| 17715 | // like this. `get_comp_index_context` and the `COMPOUND_IDX` |
| 17716 | // table were independently re-verified again this round and are |
| 17717 | // still correct; the four candidates above were not re-audited |
| 17718 | // this round. Refusing here (rather than shipping a value-exact- |
| 17719 | // most-of-the-time blend) until one of them is found wrong. |
| 17720 | // r6/r7: found and fixed one real bug (mvstack.rs ref_ctx family |
| 17721 | // undercounted compound neighbours in the single_ref_p*/comp_ref*/ |
| 17722 | // comp_bwdref* context reads -- kept, real fix). Unmasking on |
| 17723 | // that fix alone and running the wide gate sweep (many seeds, |
| 17724 | // `a_real_aomenc_stream_with_compound_references_decodes_pixel_exact`/ |
| 17725 | // `a_real_aomenc_stream_with_reference_select_reads_comp_mode_correctly`, |
| 17726 | // cq-level=45) still mismatches roughly half the time, at |
| 17727 | // different frames each run for the SAME seed -- aomenc's own |
| 17728 | // cpu-used=0 RD search is run-to-run nondeterministic (noted |
| 17729 | // above), so this is a second, still-unfound compound_idx defect, |
| 17730 | // not the one r6/r7 fixed. r6's own residual-mismatch note (321/ |
| 17731 | // 846 pixels not fitting the (9,7)-vs-plain-average hypothesis |
| 17732 | // either) already flagged more than one bug here. Re-masking: |
| 17733 | // do not ship a blend that is right only sometimes. |
| 17734 | // lane-av1idx r1: fixed one real bug this round |
| 17735 | // (`switchable_interp_ctx` never folded in libaom's |
| 17736 | // `INTER_FILTER_COMP_OFFSET` for a compound block's own |
| 17737 | // interp_filter context -- kept). Pinned |
| 17738 | // `fixtures/av1idx-refsel-pin.obu` (seed 61, reference_select |
| 17739 | // gate) still mismatches after that fix (decode-order frame 8 |
| 17740 | // first, small deltas, worst 1-2, propagating through later |
| 17741 | // frames via DPB reference) -- a second, still-unfound bug. |
| 17742 | // Re-masking: see that fixture + `scratch_isolate_pinned_mismatch` |
| 17743 | // (`EC_AV1_PIN=fixtures/av1idx-refsel-pin.obu EC_AV1_PIN_N=20`) |
| 17744 | // for the next round's starting point. |
| 17745 | // lane-maskcomp r2: `compound_idx` is only read when |
| 17746 | // `comp_group_idx == 0` (libaom `decodemv.c:1606`) -- a masked |
| 17747 | // (wedge/diffwtd) block never also carries a distance-weighted |
| 17748 | // split. This gate was missing before r2 (dead code: the masked |
| 17749 | // path always refused earlier, so it never executed with |
| 17750 | // `comp_group_idx == 1` in practice). |
| 17751 | let (fwd_offset, bck_offset, compound_idx) = if comp_group_idx == 0 |
| 17752 | && !skip_mode |
| 17753 | && enable_jnt_comp |
| 17754 | { |
| 17755 | let idx_ctx = get_comp_index_context( |
| 17756 | neighbours, |
| 17757 | (rmi, cmi), |
| 17758 | side, |
| 17759 | order_hint_bits, |
| 17760 | order_hint, |
| 17761 | ref_order_hints[(ref0 - LAST_FRAME) as usize], |
| 17762 | ref_order_hints[(ref1 - LAST_FRAME) as usize], |
| 17763 | ); |
| 17764 | let idx = dec.symbol(&mut cdfs.compound_idx[idx_ctx]); |
| 17765 | if idx == 1 { |
| 17766 | (8, 8, 1u8) |
| 17767 | } else { |
| 17768 | let (fwd, bck) = crate::compound::dist_wtd_comp_weight_assign( |
| 17769 | order_hint_bits, |
| 17770 | order_hint, |
| 17771 | ref_order_hints[(ref0 - LAST_FRAME) as usize], |
| 17772 | ref_order_hints[(ref1 - LAST_FRAME) as usize], |
| 17773 | ); |
| 17774 | (fwd, bck, 0u8) |
| 17775 | } |
| 17776 | } else { |
| 17777 | (8, 8, 1u8) |
| 17778 | }; |
| 17779 | compound_ctx = Some((ref1, comp_group_idx as u8, compound_idx)); |
| 17780 | if std::env::var_os("EC_AV1_COMPIDX_DUMP").is_some() { |
| 17781 | eprintln!( |
| 17782 | "EC_COMPIDX mi_row={mi_row} mi_col={mi_col} bsize={side} mode={compound_mode} mv0=({},{}) mv1=({},{}) ref0={ref0} ref1={ref1} comp_group_idx={comp_group_idx} compound_idx={compound_idx} tell={}", |
| 17783 | mv0.0, mv0.1, mv1.0, mv1.1, dec.debug_bitpos() |
| 17784 | ); |
| 17785 | } |
| 17786 | |
| 17787 | // spec 5.11.19/libaom `decodemv.c` 1575: `read_mb_interp_filter` |
| 17788 | // comes AFTER `read_compound_type` (the `comp_group_idx`/ |
| 17789 | // `compound_idx` reads just above), not before -- moved here |
| 17790 | // (lane-av1blend r5) from its old position ahead of those reads, |
| 17791 | // which stole their bits for a Switchable-filter compound block |
| 17792 | // and desynced every symbol read after it in the tile. |
| 17793 | // `av1_is_interp_needed` is also 0 for `skip_mode` blocks: the |
| 17794 | // filter is derived (Regular), never read -- same |
| 17795 | // conditional-read contract as the WARPED_CAUSAL suppression on |
| 17796 | // the single-ref path below. |
| 17797 | let (h_filter, v_filter, resolved_filter) = resolve_interp_filter( |
| 17798 | dec, |
| 17799 | cdfs, |
| 17800 | interp_fixed, |
| 17801 | enable_dual_filter, |
| 17802 | gm_nontrans || skip_mode, |
| 17803 | above_filter_ctx, |
| 17804 | left_filter_ctx, |
| 17805 | true, |
| 17806 | ); |
| 17807 | block_filter = resolved_filter; |
| 17808 | |
| 17809 | // lane-scaledref r1: each compound tap scales INDEPENDENTLY off |
| 17810 | // its own stored reference's luma width (spec 7.11.3.3 derives |
| 17811 | // x_scale_fp per reference; libaom `av1_setup_pre_planes` builds |
| 17812 | // one `scale_factors` per `ref_frame`), so the two taps can carry |
| 17813 | // different ratios in the same block. `REF_NO_SCALE` reduces |
| 17814 | // `predict_compound_intermediate`'s walk to the ordinary |
| 17815 | // stride-1 one, so the unscaled case is unchanged. |
| 17816 | let scale0 = mc::scale_factor(py0.width, frame_width); |
| 17817 | let scale1 = mc::scale_factor(py1.width, frame_width); |
| 17818 | if scale0 != mc::REF_NO_SCALE || scale1 != mc::REF_NO_SCALE { |
| 17819 | SCALED_COMPOUND_HITS.with(|c| c.set(c.get() + 1)); |
| 17820 | } |
| 17821 | // lane-scaledref r2: the MIXED case -- one tap scaled, the other |
| 17822 | // not -- is the one an all-frames-scaled recipe never produces, |
| 17823 | // and the one a single shared scale factor would get wrong. |
| 17824 | if (scale0 == mc::REF_NO_SCALE) != (scale1 == mc::REF_NO_SCALE) { |
| 17825 | MIXED_SCALE_COMPOUND_HITS.with(|c| c.set(c.get() + 1)); |
| 17826 | } |
| 17827 | |
| 17828 | let mut inter0_y = vec![0i32; side * side]; |
| 17829 | mc::predict_compound_intermediate( |
| 17830 | &py0.data, |
| 17831 | py0.width, |
| 17832 | py0.true_width, |
| 17833 | py0.true_height, |
| 17834 | mv_to_q4(px, mv0.1, true), |
| 17835 | mv_to_q4(py, mv0.0, true), |
| 17836 | scale0, |
| 17837 | side, |
| 17838 | side, |
| 17839 | h_filter, |
| 17840 | v_filter, |
| 17841 | &mut inter0_y, |
| 17842 | ); |
| 17843 | // lane-cwarp r1: this reference's own GLOBAL warp replaces the |
| 17844 | // translational tap (libaom `av1_warp_plane` with |
| 17845 | // `conv_params->is_compound`); the blend below is unchanged. |
| 17846 | if let Some(wp) = &warp0 { |
| 17847 | crate::warp::warp_affine_compound( |
| 17848 | wp, &py0.data, py0.true_width as i32, py0.true_height as i32, |
| 17849 | py0.width as i32, &mut inter0_y, px as i32, py as i32, side as i32, |
| 17850 | side as i32, side as i32, 0, 0, |
| 17851 | ); |
| 17852 | } |
| 17853 | let mut inter1_y = vec![0i32; side * side]; |
| 17854 | mc::predict_compound_intermediate( |
| 17855 | &py1.data, |
| 17856 | py1.width, |
| 17857 | py1.true_width, |
| 17858 | py1.true_height, |
| 17859 | mv_to_q4(px, mv1.1, true), |
| 17860 | mv_to_q4(py, mv1.0, true), |
| 17861 | scale1, |
| 17862 | side, |
| 17863 | side, |
| 17864 | h_filter, |
| 17865 | v_filter, |
| 17866 | &mut inter1_y, |
| 17867 | ); |
| 17868 | // lane-cwarp r1: this reference's own GLOBAL warp replaces the |
| 17869 | // translational tap (libaom `av1_warp_plane` with |
| 17870 | // `conv_params->is_compound`); the blend below is unchanged. |
| 17871 | if let Some(wp) = &warp1 { |
| 17872 | crate::warp::warp_affine_compound( |
| 17873 | wp, &py1.data, py1.true_width as i32, py1.true_height as i32, |
| 17874 | py1.width as i32, &mut inter1_y, px as i32, py as i32, side as i32, |
| 17875 | side as i32, side as i32, 0, 0, |
| 17876 | ); |
| 17877 | } |
| 17878 | let mut pred_y = vec![0u16; side * side]; |
| 17879 | let mut diffwtd_mask_y = Vec::new(); |
| 17880 | // lane-wedge r3: `mask_y` is the DIFFWTD buffer just computed OR |
| 17881 | // the wedge codebook lookup (comp_group_idx==1's two mutually |
| 17882 | // exclusive branches) -- same [`mc::blend_masked_compound`] call |
| 17883 | // either way, only the mask source differs. |
| 17884 | let mask_y: Option<&[u8]> = if let Some(mask_type) = diffwtd_mask_type { |
| 17885 | diffwtd_mask_y = vec![0u8; side * side]; |
| 17886 | DIFFWTD_HITS.with(|c| c.set(c.get() + 1)); |
| 17887 | mc::diffwtd_mask(&inter0_y, &inter1_y, mask_type == 1, &mut diffwtd_mask_y); |
| 17888 | Some(diffwtd_mask_y.as_slice()) |
| 17889 | } else { |
| 17890 | wedge_mask |
| 17891 | }; |
| 17892 | if let Some(mask_y) = mask_y { |
| 17893 | mc::blend_masked_compound( |
| 17894 | &inter0_y, &inter1_y, mask_y, side, side, side, false, &mut pred_y, |
| 17895 | ); |
| 17896 | } else { |
| 17897 | mc::combine_compound(&inter0_y, &inter1_y, fwd_offset, bck_offset, &mut pred_y); |
| 17898 | } |
| 17899 | |
| 17900 | let mut inter0_u = vec![0i32; chroma_side * chroma_side]; |
| 17901 | mc::predict_compound_intermediate( |
| 17902 | &pu0.data, |
| 17903 | pu0.width, |
| 17904 | pu0.true_width, |
| 17905 | pu0.true_height, |
| 17906 | mv_to_q4(cpx, mv0.1, false), |
| 17907 | mv_to_q4(cpy, mv0.0, false), |
| 17908 | scale0, |
| 17909 | chroma_side, |
| 17910 | chroma_side, |
| 17911 | h_filter, |
| 17912 | v_filter, |
| 17913 | &mut inter0_u, |
| 17914 | ); |
| 17915 | // `av1_init_warp_params` bails when the PLANE's block is |
| 17916 | // narrower/shorter than 8 (`block_width < 8`), so a 4x4 chroma |
| 17917 | // block of an 8x8 luma block stays translational. |
| 17918 | if let Some(wp) = &warp0 { |
| 17919 | if chroma_side >= 8 { |
| 17920 | crate::warp::warp_affine_compound( |
| 17921 | wp, &pu0.data, pu0.true_width as i32, pu0.true_height as i32, |
| 17922 | pu0.width as i32, &mut inter0_u, cpx as i32, cpy as i32, |
| 17923 | chroma_side as i32, chroma_side as i32, chroma_side as i32, 1, 1, |
| 17924 | ); |
| 17925 | } |
| 17926 | } |
| 17927 | let mut inter1_u = vec![0i32; chroma_side * chroma_side]; |
| 17928 | mc::predict_compound_intermediate( |
| 17929 | &pu1.data, |
| 17930 | pu1.width, |
| 17931 | pu1.true_width, |
| 17932 | pu1.true_height, |
| 17933 | mv_to_q4(cpx, mv1.1, false), |
| 17934 | mv_to_q4(cpy, mv1.0, false), |
| 17935 | scale1, |
| 17936 | chroma_side, |
| 17937 | chroma_side, |
| 17938 | h_filter, |
| 17939 | v_filter, |
| 17940 | &mut inter1_u, |
| 17941 | ); |
| 17942 | // `av1_init_warp_params` bails when the PLANE's block is |
| 17943 | // narrower/shorter than 8 (`block_width < 8`), so a 4x4 chroma |
| 17944 | // block of an 8x8 luma block stays translational. |
| 17945 | if let Some(wp) = &warp1 { |
| 17946 | if chroma_side >= 8 { |
| 17947 | crate::warp::warp_affine_compound( |
| 17948 | wp, &pu1.data, pu1.true_width as i32, pu1.true_height as i32, |
| 17949 | pu1.width as i32, &mut inter1_u, cpx as i32, cpy as i32, |
| 17950 | chroma_side as i32, chroma_side as i32, chroma_side as i32, 1, 1, |
| 17951 | ); |
| 17952 | } |
| 17953 | } |
| 17954 | let mut pred_u = vec![0u16; chroma_side * chroma_side]; |
| 17955 | if let Some(mask_y) = mask_y { |
| 17956 | mc::blend_masked_compound( |
| 17957 | &inter0_u, |
| 17958 | &inter1_u, |
| 17959 | mask_y, |
| 17960 | side, |
| 17961 | chroma_side, |
| 17962 | chroma_side, |
| 17963 | true, |
| 17964 | &mut pred_u, |
| 17965 | ); |
| 17966 | } else { |
| 17967 | mc::combine_compound(&inter0_u, &inter1_u, fwd_offset, bck_offset, &mut pred_u); |
| 17968 | } |
| 17969 | |
| 17970 | let mut inter0_v = vec![0i32; chroma_side * chroma_side]; |
| 17971 | mc::predict_compound_intermediate( |
| 17972 | &pv0.data, |
| 17973 | pv0.width, |
| 17974 | pv0.true_width, |
| 17975 | pv0.true_height, |
| 17976 | mv_to_q4(cpx, mv0.1, false), |
| 17977 | mv_to_q4(cpy, mv0.0, false), |
| 17978 | scale0, |
| 17979 | chroma_side, |
| 17980 | chroma_side, |
| 17981 | h_filter, |
| 17982 | v_filter, |
| 17983 | &mut inter0_v, |
| 17984 | ); |
| 17985 | // `av1_init_warp_params` bails when the PLANE's block is |
| 17986 | // narrower/shorter than 8 (`block_width < 8`), so a 4x4 chroma |
| 17987 | // block of an 8x8 luma block stays translational. |
| 17988 | if let Some(wp) = &warp0 { |
| 17989 | if chroma_side >= 8 { |
| 17990 | crate::warp::warp_affine_compound( |
| 17991 | wp, &pv0.data, pv0.true_width as i32, pv0.true_height as i32, |
| 17992 | pv0.width as i32, &mut inter0_v, cpx as i32, cpy as i32, |
| 17993 | chroma_side as i32, chroma_side as i32, chroma_side as i32, 1, 1, |
| 17994 | ); |
| 17995 | } |
| 17996 | } |
| 17997 | let mut inter1_v = vec![0i32; chroma_side * chroma_side]; |
| 17998 | mc::predict_compound_intermediate( |
| 17999 | &pv1.data, |
| 18000 | pv1.width, |
| 18001 | pv1.true_width, |
| 18002 | pv1.true_height, |
| 18003 | mv_to_q4(cpx, mv1.1, false), |
| 18004 | mv_to_q4(cpy, mv1.0, false), |
| 18005 | scale1, |
| 18006 | chroma_side, |
| 18007 | chroma_side, |
| 18008 | h_filter, |
| 18009 | v_filter, |
| 18010 | &mut inter1_v, |
| 18011 | ); |
| 18012 | // `av1_init_warp_params` bails when the PLANE's block is |
| 18013 | // narrower/shorter than 8 (`block_width < 8`), so a 4x4 chroma |
| 18014 | // block of an 8x8 luma block stays translational. |
| 18015 | if let Some(wp) = &warp1 { |
| 18016 | if chroma_side >= 8 { |
| 18017 | crate::warp::warp_affine_compound( |
| 18018 | wp, &pv1.data, pv1.true_width as i32, pv1.true_height as i32, |
| 18019 | pv1.width as i32, &mut inter1_v, cpx as i32, cpy as i32, |
| 18020 | chroma_side as i32, chroma_side as i32, chroma_side as i32, 1, 1, |
| 18021 | ); |
| 18022 | } |
| 18023 | } |
| 18024 | let mut pred_v = vec![0u16; chroma_side * chroma_side]; |
| 18025 | if let Some(mask_y) = mask_y { |
| 18026 | mc::blend_masked_compound( |
| 18027 | &inter0_v, |
| 18028 | &inter1_v, |
| 18029 | mask_y, |
| 18030 | side, |
| 18031 | chroma_side, |
| 18032 | chroma_side, |
| 18033 | true, |
| 18034 | &mut pred_v, |
| 18035 | ); |
| 18036 | } else { |
| 18037 | mc::combine_compound(&inter0_v, &inter1_v, fwd_offset, bck_offset, &mut pred_v); |
| 18038 | } |
| 18039 | |
| 18040 | // lane-inter4 r2: a rectangular strip's transform tree starts from |
| 18041 | // its own rect `max_txsize_rect_lookup` entry, not a square one. |
| 18042 | let rect_tu = (write_w, write_h) != (side, side); |
| 18043 | vartx_leaves = if rect_tu { |
| 18044 | read_block_tx_size_rect( |
| 18045 | dec, |
| 18046 | cdfs, |
| 18047 | neighbours, |
| 18048 | at_mi, |
| 18049 | (write_w, write_h), |
| 18050 | (mi_cols as usize, mi_rows as usize), |
| 18051 | skip, |
| 18052 | )? |
| 18053 | } else { |
| 18054 | read_block_tx_size( |
| 18055 | dec, |
| 18056 | cdfs, |
| 18057 | neighbours, |
| 18058 | at_mi, |
| 18059 | side, |
| 18060 | (mi_cols as usize, mi_rows as usize), |
| 18061 | true, |
| 18062 | skip, |
| 18063 | )? |
| 18064 | .1 |
| 18065 | }; |
| 18066 | if skip { |
| 18067 | y.reconstruct_mc_rect( |
| 18068 | px, |
| 18069 | py, |
| 18070 | side, |
| 18071 | write_w, |
| 18072 | write_h, |
| 18073 | &pred_y, |
| 18074 | &vec![0i32; side * side], |
| 18075 | ); |
| 18076 | u.reconstruct_mc_rect( |
| 18077 | cpx, |
| 18078 | cpy, |
| 18079 | chroma_side, |
| 18080 | write_chroma_w, |
| 18081 | write_chroma_h, |
| 18082 | &pred_u, |
| 18083 | &vec![0i32; chroma_side * chroma_side], |
| 18084 | ); |
| 18085 | v.reconstruct_mc_rect( |
| 18086 | cpx, |
| 18087 | cpy, |
| 18088 | chroma_side, |
| 18089 | write_chroma_w, |
| 18090 | write_chroma_h, |
| 18091 | &pred_v, |
| 18092 | &vec![0i32; chroma_side * chroma_side], |
| 18093 | ); |
| 18094 | luma_grid = vec![0i32; side * side]; |
| 18095 | u_grid = vec![0i32; chroma_side * chroma_side]; |
| 18096 | v_grid = vec![0i32; chroma_side * chroma_side]; |
| 18097 | } else { |
| 18098 | // lane-inter4 r2: a rect strip's coefficient context spans its |
| 18099 | // OWN width above and height left -- reading `side` cells both |
| 18100 | // ways indexes past the tile's last mi row for a bottom strip. |
| 18101 | let around = if rect_tu { |
| 18102 | neighbours.around_mi_rect(at_mi, write_w, write_h) |
| 18103 | } else { |
| 18104 | neighbours.around_mi(at, side) |
| 18105 | }; |
| 18106 | let luma_tx_type; |
| 18107 | if let Some(leaves) = vartx_leaves.clone() { |
| 18108 | // spec 5.11.17's transform tree, leaf by leaf in the order |
| 18109 | // `read_var_tx_size` coded it: each unit reads its own |
| 18110 | // coefficients against the neighbour magnitudes the |
| 18111 | // earlier units already wrote, and reconstructs over its |
| 18112 | // own slice of the block's motion-compensated predictor. |
| 18113 | let reduced = REDUCED_TX_SET_INTER.with(std::cell::Cell::get); |
| 18114 | let mut first_tx_type = TxType::DctDct; |
| 18115 | for (idx, &(row, col, tw, th)) in leaves.iter().enumerate() { |
| 18116 | let tu_mi = (at_mi.0 + row, at_mi.1 + col); |
| 18117 | let (tu_px, tu_py) = (px + col * MI, py + row * MI); |
| 18118 | let (tu_grid, tu_tx_type) = if tw == th { |
| 18119 | let tx_px = tw; |
| 18120 | let mut tu_pred = Vec::with_capacity(tx_px * tx_px); |
| 18121 | for rr in 0..tx_px { |
| 18122 | let start = (row * MI + rr) * side + col * MI; |
| 18123 | tu_pred.extend_from_slice(&pred_y[start..start + tx_px]); |
| 18124 | } |
| 18125 | let scan = default_scan(tx_px); |
| 18126 | let tu_skip_ctx = neighbours.luma_skip_ctx(tu_mi, tx_px / MI); |
| 18127 | read_inter_plane( |
| 18128 | dec, |
| 18129 | cdfs, |
| 18130 | inter_txbset_for(tx_px, reduced), |
| 18131 | &scan, |
| 18132 | 0, |
| 18133 | neighbours.around_mi(tu_mi, tx_px)[0], |
| 18134 | mode_for_tx, |
| 18135 | y, |
| 18136 | tu_px, |
| 18137 | tu_py, |
| 18138 | tx_px, |
| 18139 | base_q_idx, |
| 18140 | &tu_pred, |
| 18141 | None, |
| 18142 | Some(tu_skip_ctx), |
| 18143 | )? |
| 18144 | } else { |
| 18145 | // lane-r14 r3: a 1:4 strip's `sub_tx_size_map` |
| 18146 | // sub-transform is RECTANGULAR (TX_64X16 -> |
| 18147 | // TX_32X16, TX_16X64 -> TX_16X32), so this leaf is |
| 18148 | // read with the rect coefficient machinery, at its |
| 18149 | // own `txb_skip_ctx`/neighbour spans -- the unit is |
| 18150 | // smaller than its block, so `get_txb_ctx_general`'s |
| 18151 | // neighbour table applies, not the lone-TU 0. |
| 18152 | vartx_rect_leaf_hit(tw, th); |
| 18153 | let tu_skip_ctx = |
| 18154 | neighbours.luma_skip_ctx_rect(tu_mi, tw / MI, th / MI); |
| 18155 | let start = row * MI * side + col * MI; |
| 18156 | read_inter_plane_rect( |
| 18157 | dec, |
| 18158 | cdfs, |
| 18159 | rect_inter_luma_set(tw, th), |
| 18160 | (tw, th), |
| 18161 | side, |
| 18162 | 0, |
| 18163 | neighbours.around_mi_rect(tu_mi, tw, th)[0], |
| 18164 | mode_for_tx, |
| 18165 | y, |
| 18166 | tu_px, |
| 18167 | tu_py, |
| 18168 | &pred_y[start..], |
| 18169 | None, |
| 18170 | Some(tu_skip_ctx), |
| 18171 | )? |
| 18172 | }; |
| 18173 | if idx == 0 { |
| 18174 | first_tx_type = tu_tx_type; |
| 18175 | } |
| 18176 | neighbours.record_mi_luma_rect(tu_mi, tw, th, &tu_grid); |
| 18177 | } |
| 18178 | // `av1_get_tx_type`'s chroma lookup scales the chroma |
| 18179 | // position back to luma, so an inter block's chroma |
| 18180 | // inherits the *first* (top-left) luma unit's coded type. |
| 18181 | luma_tx_type = first_tx_type; |
| 18182 | luma_grid = vec![0i32; side * side]; |
| 18183 | } else if rect_tu { |
| 18184 | // The whole-block rectangular transform unit |
| 18185 | // (`txfm_partition` said 0 at depth 0). |
| 18186 | let (grid, tx_type) = read_inter_plane_rect( |
| 18187 | dec, |
| 18188 | cdfs, |
| 18189 | rect_inter_luma_set(write_w, write_h), |
| 18190 | (write_w, write_h), |
| 18191 | side, |
| 18192 | 0, |
| 18193 | around[0], |
| 18194 | mode_for_tx, |
| 18195 | y, |
| 18196 | px, |
| 18197 | py, |
| 18198 | &pred_y, |
| 18199 | None, |
| 18200 | None, |
| 18201 | )?; |
| 18202 | luma_tx_type = tx_type; |
| 18203 | luma_grid = embed_rect_grid(&grid, write_w, write_h, side); |
| 18204 | } else { |
| 18205 | (luma_grid, luma_tx_type) = read_inter_plane( |
| 18206 | dec, |
| 18207 | cdfs, |
| 18208 | luma_set_inter, |
| 18209 | scan_luma, |
| 18210 | 0, |
| 18211 | around[0], |
| 18212 | mode_for_tx, |
| 18213 | y, |
| 18214 | px, |
| 18215 | py, |
| 18216 | side, |
| 18217 | base_q_idx, |
| 18218 | &pred_y, |
| 18219 | None, |
| 18220 | None, |
| 18221 | )?; |
| 18222 | } |
| 18223 | if rect_tu { |
| 18224 | // `av1_get_max_uv_txsize(bsize)`: the chroma transform of a |
| 18225 | // 2:1 strip is the strip's own rect chroma size (16x8 / |
| 18226 | // 8x16), one unit for the whole block, and it codes no |
| 18227 | // `tx_type` symbol -- it inherits the luma unit's. |
| 18228 | let (uw, uh) = (write_chroma_w, write_chroma_h); |
| 18229 | u_grid = embed_rect_grid( |
| 18230 | &read_inter_plane_rect( |
| 18231 | dec, |
| 18232 | cdfs, |
| 18233 | rect_inter_chroma_set(write_chroma_w, write_chroma_h), |
| 18234 | (uw, uh), |
| 18235 | chroma_side, |
| 18236 | 1, |
| 18237 | around[1], |
| 18238 | mode_for_tx, |
| 18239 | u, |
| 18240 | cpx, |
| 18241 | cpy, |
| 18242 | &pred_u, |
| 18243 | Some(luma_tx_type), |
| 18244 | None, |
| 18245 | )? |
| 18246 | .0, |
| 18247 | uw, |
| 18248 | uh, |
| 18249 | chroma_side, |
| 18250 | ); |
| 18251 | v_grid = embed_rect_grid( |
| 18252 | &read_inter_plane_rect( |
| 18253 | dec, |
| 18254 | cdfs, |
| 18255 | rect_inter_chroma_set(write_chroma_w, write_chroma_h), |
| 18256 | (uw, uh), |
| 18257 | chroma_side, |
| 18258 | 2, |
| 18259 | around[2], |
| 18260 | mode_for_tx, |
| 18261 | v, |
| 18262 | cpx, |
| 18263 | cpy, |
| 18264 | &pred_v, |
| 18265 | Some(luma_tx_type), |
| 18266 | None, |
| 18267 | )? |
| 18268 | .0, |
| 18269 | uw, |
| 18270 | uh, |
| 18271 | chroma_side, |
| 18272 | ); |
| 18273 | } else { |
| 18274 | u_grid = read_inter_plane( |
| 18275 | dec, |
| 18276 | cdfs, |
| 18277 | chroma_set, |
| 18278 | scan_chroma, |
| 18279 | 1, |
| 18280 | around[1], |
| 18281 | mode_for_tx, |
| 18282 | u, |
| 18283 | cpx, |
| 18284 | cpy, |
| 18285 | chroma_side, |
| 18286 | base_q_idx, |
| 18287 | &pred_u, |
| 18288 | Some(luma_tx_type), |
| 18289 | None, |
| 18290 | )? |
| 18291 | .0; |
| 18292 | v_grid = read_inter_plane( |
| 18293 | dec, |
| 18294 | cdfs, |
| 18295 | chroma_set, |
| 18296 | scan_chroma, |
| 18297 | 2, |
| 18298 | around[2], |
| 18299 | mode_for_tx, |
| 18300 | v, |
| 18301 | cpx, |
| 18302 | cpy, |
| 18303 | chroma_side, |
| 18304 | base_q_idx, |
| 18305 | &pred_v, |
| 18306 | Some(luma_tx_type), |
| 18307 | None, |
| 18308 | )? |
| 18309 | .0; |
| 18310 | } |
| 18311 | } |
| 18312 | } else { |
| 18313 | let ref_frame = |
| 18314 | read_single_ref( |
| 18315 | dec, |
| 18316 | cdfs, |
| 18317 | neighbours.above_ref[cmi], |
| 18318 | neighbours.above_ref1[cmi], |
| 18319 | neighbours.left_ref[rmi], |
| 18320 | neighbours.left_ref1[rmi], |
| 18321 | ); |
| 18322 | ref_frame_for_lf = ref_frame; |
| 18323 | if std::env::var_os("EC_AV1_TELL").is_some() { |
| 18324 | let (mi_row, mi_col) = (rmi, cmi); |
| 18325 | eprintln!( |
| 18326 | "TELL mi_row={mi_row} mi_col={mi_col} label=post_ref_frame ref={ref_frame} tell={} range={}", |
| 18327 | dec.debug_bitpos(), dec.debug_state().0 |
| 18328 | ); |
| 18329 | } |
| 18330 | // round 4-9 (lane-av1golden..lane-av1golden7): GOLDEN_FRAME's own |
| 18331 | // stacked defects (lf_level ref/mode-delta forwarding, the |
| 18332 | // switchable_interp ref-frame ctx gate, and a film-grain synthesis |
| 18333 | // gap that looked like a GOLDEN MC bug by survivor bias -- see the |
| 18334 | // ledger) are all closed; GOLDEN_FRAME decodes live via `other_refs` |
| 18335 | // below same as every other non-`LAST_FRAME` reference. lane-av1refs |
| 18336 | // widens this from GOLDEN alone to every reference `read_single_ref` |
| 18337 | // can name. |
| 18338 | let (py_ref, pu_ref, pv_ref) = if ref_frame == LAST_FRAME { |
| 18339 | (ref_y, ref_u, ref_v) |
| 18340 | } else { |
| 18341 | match other_refs[ref_frame as usize] { |
| 18342 | Some((ry, ru, rv)) => (ry, ru, rv), |
| 18343 | None => { |
| 18344 | return Err(unsupported( |
| 18345 | "a reference frame selected with no picture at this frame's own \ |
| 18346 | ref_frame_idx slot for it", |
| 18347 | )); |
| 18348 | } |
| 18349 | } |
| 18350 | }; |
| 18351 | |
| 18352 | let (mi_row, mi_col) = (rmi, cmi); |
| 18353 | // lane-rect r2: the mv stack must be queried with the block's |
| 18354 | // REAL extent (libaom `av1_get_mv_refs` gets bw4/bh4 from |
| 18355 | // xd->width/xd->height, not a square guess) -- `write_w`/ |
| 18356 | // `write_h` already name the true HORZ/VERT/HORZ_B strip |
| 18357 | // footprint (r1's plumbing), so derive bw4/bh4 from those |
| 18358 | // instead of the old `side`-square (HORZ_B-only) heuristic. |
| 18359 | let bw4 = write_w / 4; |
| 18360 | let bh4 = write_h / 4; |
| 18361 | // spec 7.10.2.8: only reached when the frame header set |
| 18362 | // `use_ref_frame_mvs` (`tpl_frame` is `Some` then, `None` |
| 18363 | // otherwise) -- `cur_offset_0` is this query block's own resolved |
| 18364 | // `ref_frame`'s distance from the current frame (spec 7.9.3), |
| 18365 | // recomputed per block since different blocks pick different refs. |
| 18366 | let tpl = tpl_frame.map(|t| crate::mvstack::TplArgs { |
| 18367 | field: t.field, |
| 18368 | cur_offset_0: crate::motion_field::get_relative_dist( |
| 18369 | t.order_hint_bits, |
| 18370 | t.order_hint, |
| 18371 | t.ref_order_hints[(ref_frame - LAST_FRAME) as usize], |
| 18372 | ), |
| 18373 | allow_high_precision_mv, |
| 18374 | }); |
| 18375 | let gm_table = build_gm_mv_table( |
| 18376 | global_motion, |
| 18377 | mi_row, |
| 18378 | mi_col, |
| 18379 | bw4, |
| 18380 | bh4, |
| 18381 | allow_high_precision_mv, |
| 18382 | force_integer_mv, |
| 18383 | ); |
| 18384 | let stack = find_mv_stack_with_sign_bias( |
| 18385 | grid, |
| 18386 | mi_row, |
| 18387 | mi_col, |
| 18388 | bw4, |
| 18389 | bh4, |
| 18390 | ref_frame, |
| 18391 | mi_cols as usize, |
| 18392 | mi_rows as usize, |
| 18393 | sign_bias_table, |
| 18394 | &gm_table, |
| 18395 | tpl, |
| 18396 | ); |
| 18397 | let not_new = dec.symbol(&mut cdfs.new_mv[stack.new_mv_ctx]) == 1; |
| 18398 | let mut is_globalmv = false; |
| 18399 | // lane-interbis r1: which single-ref mode this block resolved to, |
| 18400 | // in the oracle's own numbering (NEARESTMV=13 .. NEWMV=16), for |
| 18401 | // the `EC_MODE_VAL` value ladder below. |
| 18402 | let mut dbg_mode = 16u32; |
| 18403 | let (mv, is_new_mv) = if !not_new { |
| 18404 | // NEWMV (spec 5.11.24's `read_drl_idx`, `RefMvIdx` starting at 0): |
| 18405 | // read at most two `drl_mode` bits, one per stack entry past the |
| 18406 | // first, stopping at the first `0`. The chosen index selects |
| 18407 | // which stack entry `read_mv`'s base predictor comes from |
| 18408 | // (spec 7.10.2.10 `assign_mv`'s `PredMv = RefStackMv[RefMvIdx]`) |
| 18409 | // -- entry 0 (`stack.pred_mv`) only when the loop never advances. |
| 18410 | let mut idx = 0usize; |
| 18411 | while idx < 2 && stack.entries.len() > idx + 1 { |
| 18412 | if dec.symbol(&mut cdfs.drl_mode[stack.drl_ctx[idx]]) == 0 { |
| 18413 | break; |
| 18414 | } |
| 18415 | idx += 1; |
| 18416 | } |
| 18417 | let base_mv = stack.entries.get(idx).map_or(stack.pred_mv, |e| e.mv); |
| 18418 | ( |
| 18419 | read_mv( |
| 18420 | dec, |
| 18421 | &mut cdfs.mv_comp, |
| 18422 | &mut cdfs.mv_joint, |
| 18423 | base_mv, |
| 18424 | allow_high_precision_mv, |
| 18425 | force_integer_mv, |
| 18426 | ), |
| 18427 | true, |
| 18428 | ) |
| 18429 | } else { |
| 18430 | let not_zero = dec.symbol(&mut cdfs.zero_mv[stack.zero_mv_ctx]) == 1; |
| 18431 | is_globalmv = !not_zero; |
| 18432 | let mv = if !not_zero { |
| 18433 | // GLOBALMV: `gm_get_motion_vector` (spec 7.10.2.1), already |
| 18434 | // computed at this block's own position in `gm_table`. |
| 18435 | dbg_mode = 15; |
| 18436 | gm_table[(ref_frame - LAST_FRAME) as usize] |
| 18437 | } else { |
| 18438 | let nearest = dec.symbol(&mut cdfs.ref_mv[stack.ref_mv_ctx]) == 0; |
| 18439 | dbg_mode = if nearest { 13 } else { 14 }; |
| 18440 | if nearest { |
| 18441 | stack.nearest_mv |
| 18442 | } else { |
| 18443 | // NEARMV (spec 5.11.24's `read_drl_idx`, `RefMvIdx` starting |
| 18444 | // at 1): read at most two more `drl_mode` bits, one per stack |
| 18445 | // entry past the second, stopping at the first `0`. |
| 18446 | // `drl_ctx[idx]` is the context between `entries[idx]` and |
| 18447 | // `entries[idx + 1]` (`MvStack::drl_ctx`'s own doc), which is |
| 18448 | // exactly the pair this index is choosing between. |
| 18449 | let mut idx = 1usize; |
| 18450 | while idx < 3 && stack.entries.len() > idx + 1 { |
| 18451 | if dec.symbol(&mut cdfs.drl_mode[stack.drl_ctx[idx]]) == 0 { |
| 18452 | break; |
| 18453 | } |
| 18454 | idx += 1; |
| 18455 | } |
| 18456 | stack.entries.get(idx).map_or(stack.near_mv, |e| e.mv) |
| 18457 | } |
| 18458 | }; |
| 18459 | (mv, false) |
| 18460 | }; |
| 18461 | if std::env::var_os("EC_TRACE_MODE").is_some() { |
| 18462 | for (i, e) in stack.entries.iter().enumerate() { |
| 18463 | eprintln!( |
| 18464 | "EC_STACK mi_row={mi_row} mi_col={mi_col} ref={ref_frame} i={i} this=({},{}) comp=(0,0) w={}", |
| 18465 | e.mv.0, e.mv.1, e.weight |
| 18466 | ); |
| 18467 | } |
| 18468 | eprintln!( |
| 18469 | "EC_MODE_VAL mi_row={mi_row} mi_col={mi_col} mode={dbg_mode} ref0={ref_frame} ref1=-1 mv0=({},{}) stack={} rng={}", |
| 18470 | mv.0, |
| 18471 | mv.1, |
| 18472 | stack.entries.len(), |
| 18473 | dec.debug_state().0 |
| 18474 | ); |
| 18475 | } |
| 18476 | // lane-gm r2: the two libaom predicates single-ref GLOBALMV |
| 18477 | // blocks need -- `is_global_mv_block` (blockd.h:421-429, mode |
| 18478 | // GLOBAL* AND model > TRANSLATION AND min(bw,bh) >= 8px, gating |
| 18479 | // `motion_mode_eligible`/`MiInfo::is_global_mv0` below) and |
| 18480 | // `is_nontrans_global_motion` (reconinter.h:420-425, model != |
| 18481 | // TRANSLATION -- IDENTITY counts here -- gating the interp |
| 18482 | // filter suppress below). Do NOT unify these. |
| 18483 | let gm_model = global_motion[(ref_frame - LAST_FRAME) as usize].model; |
| 18484 | let min_bw_bh4 = bw4.min(bh4); |
| 18485 | let is_global_mv_block = is_globalmv && gm_model as u8 > 1 && min_bw_bh4 >= 2; |
| 18486 | let gm_nontrans = |
| 18487 | is_globalmv && gm_model != ec_av1_syntax::WarpModel::Translation && min_bw_bh4 >= 2; |
| 18488 | if std::env::var_os("EC_AV1_TELL").is_some() { |
| 18489 | eprintln!( |
| 18490 | "TELL mi_row={mi_row} mi_col={mi_col} label=post_assign_mv tell={} range={}", |
| 18491 | dec.debug_bitpos(), dec.debug_state().0 |
| 18492 | ); |
| 18493 | } |
| 18494 | // lane-sb128 r4: `interintra` (spec 5.11.24; libaom |
| 18495 | // `decodemv.c` read_inter_block_mode_info ~1490-1510), placed |
| 18496 | // right after `assign_mv` and before `read_motion_mode` below -- |
| 18497 | // libaom's own sequential order. `is_interintra_allowed_bsize` |
| 18498 | // (`BLOCK_8X8..=BLOCK_32X32`) is the only real gate on this |
| 18499 | // single-ref path: `is_interintra_allowed_ref` always holds here |
| 18500 | // (single ref, `ref_frame[1]` not yet `INTRA_FRAME`) and |
| 18501 | // `is_interintra_allowed_mode` always holds (every mode this |
| 18502 | // branch produces -- NEARESTMV/NEARMV/GLOBALMV/NEWMV -- is |
| 18503 | // `SINGLE_INTER_MODE_START..END` by construction). |
| 18504 | // `size_group_lookup`: BLOCK_16X16 -> 2, BLOCK_32X32 -> 3. |
| 18505 | let mut interintra_mode: Option<u8> = None; |
| 18506 | let mut wedge_mask: Option<(&'static [u8], usize)> = None; |
| 18507 | // lane-inter4 r1: `is_interintra_allowed_bsize` is |
| 18508 | // `BLOCK_8X8..=BLOCK_32X32` in libaom's enum ORDER, which excludes |
| 18509 | // every 1:4 shape (BLOCK_8X32 = 18, BLOCK_32X8 = 19) even though |
| 18510 | // both dimensions are in range -- reading this symbol on a 1:4 |
| 18511 | // strip is a symbol the reference never wrote. |
| 18512 | let interintra_shape = |
| 18513 | write_w.max(write_h) <= 32 && write_w.max(write_h) / write_w.min(write_h) <= 2; |
| 18514 | if enable_interintra_compound |
| 18515 | && !skip_mode |
| 18516 | && (side == 16 || side == 32) |
| 18517 | && interintra_shape |
| 18518 | { |
| 18519 | // lane-inter4 r3: `size_group_lookup` reads the block's |
| 18520 | // TRUE footprint -- a 16x8 strip is group 1, a 32x16 strip |
| 18521 | // group 2, not their square `side`'s 2/3. |
| 18522 | let bsize_group = size_group_wh(write_w, write_h); |
| 18523 | let interintra = dec.symbol(&mut cdfs.interintra[bsize_group]) == 1; |
| 18524 | if interintra { |
| 18525 | // lane-interintra r1 (decodemv.c 1540-1555): interintra_mode, |
| 18526 | // then -- gated on block size ALONE, never the |
| 18527 | // `enable_interintra_wedge` seq bit -- the wedge flag |
| 18528 | // (`av1_is_wedge_used(bsize)` holds for 16x16/32x32). |
| 18529 | let ii = dec.symbol(&mut cdfs.interintra_mode[bsize_group]) as u8; |
| 18530 | let wedge_bsize = if side == 16 { 6 } else { 9 }; |
| 18531 | let wedge = dec.symbol(&mut cdfs.wedge_interintra[wedge_bsize]) == 1; |
| 18532 | if wedge { |
| 18533 | // lane-wii r2 (spec 5.11.25): `wedge_index` is an |
| 18534 | // ADAPTING CDF symbol over the same `wedge_bsize` row; |
| 18535 | // NO sign symbol follows -- libaom fixes |
| 18536 | // INTERINTRA_WEDGE_SIGN 0 (blockd.h). |
| 18537 | let wedge_index = dec.symbol(&mut cdfs.wedge_idx[wedge_bsize]); |
| 18538 | WII_HITS.with(|c| c.set(c.get() + 1)); |
| 18539 | wedge_mask = Some(( |
| 18540 | crate::wedge::wedge_masks() |
| 18541 | .codebook(side) |
| 18542 | .mask(0, wedge_index as usize), |
| 18543 | side, |
| 18544 | )); |
| 18545 | } else { |
| 18546 | INTERINTRA_HITS.with(|c| c.set(c.get() + 1)); |
| 18547 | } |
| 18548 | interintra_mode = Some(ii); |
| 18549 | } |
| 18550 | } |
| 18551 | if std::env::var_os("EC_AV1_TELL").is_some() { |
| 18552 | eprintln!( |
| 18553 | "TELL mi_row={mi_row} mi_col={mi_col} label=post_interintra tell={} range={}", |
| 18554 | dec.debug_bitpos(), dec.debug_state().0 |
| 18555 | ); |
| 18556 | } |
| 18557 | // lane-motionmode round 1: `read_motion_mode` (spec 5.11.24; |
| 18558 | // libaom `decodemv.c` read_inter_block_mode_info, ~1520-1528), |
| 18559 | // placed right after MV assignment and before the switchable |
| 18560 | // interp filter read below (`read_mb_interp_filter`, decodemv.c |
| 18561 | // ~1600) -- libaom's own sequential order. `motion_mode_allowed` |
| 18562 | // (`reconinter.h`) reduces, for every block this single-ref path |
| 18563 | // reaches, to "does an inter neighbour border the above row or |
| 18564 | // left column": `is_motion_variation_allowed_bsize` always holds |
| 18565 | // (`side` is never below 16px on this path), `ref_frame[1] != |
| 18566 | // INTRA_FRAME` always holds (no interintra reader here), |
| 18567 | // `is_motion_variation_allowed_compound` always holds (this is |
| 18568 | // the single-ref branch), and `is_global_mv_block` never |
| 18569 | // triggers (this decoder's global motion is always `IDENTITY`, |
| 18570 | // so its warp `type` is never `> TRANSLATION`). A stream with |
| 18571 | // `allow_warped_motion=1` is refused right at an eligible block |
| 18572 | // rather than guessed at: `av1_findSamples`/`num_proj_ref` is |
| 18573 | // not ported, so this decoder cannot tell a real |
| 18574 | // `WARPED_CAUSAL`-eligible block (3-symbol `motion_mode_cdf`) |
| 18575 | // from an `OBMC_CAUSAL`-only one (2-symbol `obmc_cdf`) whenever |
| 18576 | // the header allows warp -- reading the wrong alphabet desyncs |
| 18577 | // the tile. |
| 18578 | // An interintra block has `ref_frame[1] == INTRA_FRAME`, which |
| 18579 | // fails libaom's `is_motion_variation_allowed_compound`: the |
| 18580 | // motion_mode symbol is NOT read (SIMPLE_TRANSLATION implied). |
| 18581 | let motion_mode_eligible = switchable_motion_mode |
| 18582 | && !skip_mode |
| 18583 | && interintra_mode.is_none() |
| 18584 | && (!overlappable_above(grid, mi_row, mi_col, bw4, mi_cols as usize, 1).is_empty() |
| 18585 | || !overlappable_left(grid, mi_row, mi_col, bh4, mi_rows as usize, 1) |
| 18586 | .is_empty()) |
| 18587 | // libaom `motion_mode_allowed`: a GLOBALMV block whose model |
| 18588 | // is non-IDENTITY-non-TRANSLATION under free (non-integer) |
| 18589 | // mv reads no motion_mode/obmc symbol at all -- implicit |
| 18590 | // SIMPLE_TRANSLATION. |
| 18591 | && !(is_global_mv_block && !force_integer_mv); |
| 18592 | let mut obmc_selected = false; |
| 18593 | // lane-warp round 2: `Some` when motion_mode == WARPED_CAUSAL |
| 18594 | // *and* the local warp estimate is valid (`!wm_params.invalid`, |
| 18595 | // `av1_find_projection`) -- an invalid estimate falls back to |
| 18596 | // this block's own translational mv, same as any other block |
| 18597 | // (spec `allow_warp`/`reconinter.c` -- global warp is never the |
| 18598 | // fallback here since this decoder's global motion is always |
| 18599 | // IDENTITY). |
| 18600 | let mut warp_params: Option<crate::warp::WarpParams> = None; |
| 18601 | // Tracks the SYMBOL value, not projection validity: libaom's |
| 18602 | // `av1_is_interp_needed` suppresses the interp-filter read for |
| 18603 | // `motion_mode == WARPED_CAUSAL` even when the projection later |
| 18604 | // falls back to translation. Its third suppressor, |
| 18605 | // `is_nontrans_global_motion`, matches our unconditional |
| 18606 | // `is_globalmv` only because this decoder codes global motion as |
| 18607 | // IDENTITY (non-TRANSLATION) and never reads switchable filters |
| 18608 | // below 8x8 -- porting TRANSLATION global motion must add the |
| 18609 | // wmtype check here. |
| 18610 | let mut warped_selected = false; |
| 18611 | if motion_mode_eligible { |
| 18612 | // `default_obmc_cdf`'s own index: square bsize 8/16/32/64 -> |
| 18613 | // 0/1/2/3; lane-rect r2: a rect strip reads its OWN bsize row |
| 18614 | // (libaom `motion_mode_cdf[mbmi->bsize]`: BLOCK_16X32 -> 4, |
| 18615 | // BLOCK_32X16 -> 5 in our packed table) -- rect-flake-1's |
| 18616 | // strip motion_mode read diverged on the square row. |
| 18617 | // lane-inter4 r1: every rect shape this decoder can now |
| 18618 | // reach reads its OWN libaom bsize row (the packed order is |
| 18619 | // documented on `cdf::MOTION_MODE`); a wrong row is a wrong |
| 18620 | // interval narrowing, not a wrong value (class |
| 18621 | // wrong-alphabet-same-value). |
| 18622 | let bsize_idx = match (write_w, write_h) { |
| 18623 | (w, h) if w == h => (w.trailing_zeros() - 3) as usize, |
| 18624 | (16, 32) => 4, |
| 18625 | (32, 16) => 5, |
| 18626 | (32, 64) => 6, |
| 18627 | (64, 32) => 7, |
| 18628 | (8, 32) => 8, |
| 18629 | (32, 8) => 9, |
| 18630 | (16, 64) => 10, |
| 18631 | (64, 16) => 11, |
| 18632 | // lane-inter4 r3: the 16x16-level rect inter leaves. |
| 18633 | (8, 16) => 12, |
| 18634 | (16, 8) => 13, |
| 18635 | _ => { |
| 18636 | return Err(unsupported( |
| 18637 | "a motion_mode symbol for a block shape with no CDF row here", |
| 18638 | )); |
| 18639 | } |
| 18640 | }; |
| 18641 | // `motion_mode_allowed` reads the 3-symbol `motion_mode_cdf` |
| 18642 | // instead of the 2-symbol `obmc_cdf` exactly when |
| 18643 | // `num_proj_ref >= 1` under `allow_warped_motion`. |
| 18644 | // lane-scaledref r2 + lane-fimv r1: libaom |
| 18645 | // `motion_mode_allowed` (`blockd.h:1484`) requires BOTH |
| 18646 | // `!av1_is_scaled(block_ref_scale_factors[0])` and |
| 18647 | // `!xd->cur_frame_force_integer_mv` for WARPED_CAUSAL -- |
| 18648 | // under a scaled reference (superres) OR on a frame whose |
| 18649 | // header set `force_integer_mv` (screen content) the block |
| 18650 | // reads the 2-symbol `obmc_cdf`, never the 3-symbol |
| 18651 | // `motion_mode_cdf`. Reading the wrong alphabet here narrows |
| 18652 | // the arithmetic coder by the wrong amount and predicts the |
| 18653 | // rest of the tile off a diverged state: silently wrong |
| 18654 | // pixels, not a desync error. `force_integer_mv` is the |
| 18655 | // per-frame value (spec 5.9.2: coded per frame only when |
| 18656 | // `seq_force_integer_mv == SELECT_INTEGER_MV`, threaded from |
| 18657 | // `FrameHeader::force_integer_mv`). |
| 18658 | // libaom `av1_is_scaled` is `x_scale_fp != REF_NO_SCALE || |
| 18659 | // y_scale_fp != REF_NO_SCALE`; the y half is unreachable here |
| 18660 | // because a reference whose height differs from this frame's |
| 18661 | // true size is refused before any tile decodes ("a reference |
| 18662 | // picture whose height does not match this frame's own true |
| 18663 | // size", ~14681). Whoever lifts that refusal adds the y term. |
| 18664 | let ref_is_scaled = |
| 18665 | mc::scale_factor(py_ref.width, frame_width) != mc::REF_NO_SCALE; |
| 18666 | let proj_ok = num_proj_ref( |
| 18667 | grid, |
| 18668 | mi_row, |
| 18669 | mi_col, |
| 18670 | bw4, |
| 18671 | bh4, |
| 18672 | mi_cols as usize, |
| 18673 | mi_rows as usize, |
| 18674 | ref_frame, |
| 18675 | ) >= 1; |
| 18676 | let warp_eligible = |
| 18677 | allow_warped_motion && !ref_is_scaled && !force_integer_mv && proj_ok; |
| 18678 | if allow_warped_motion && proj_ok && ref_is_scaled { |
| 18679 | SCALED_WARP_SUPPRESSED_HITS.with(|c| c.set(c.get() + 1)); |
| 18680 | } |
| 18681 | if allow_warped_motion && proj_ok && !ref_is_scaled && force_integer_mv { |
| 18682 | FIMV_ALPHABET_HITS.with(|c| c.set(c.get() + 1)); |
| 18683 | } |
| 18684 | if warp_eligible { |
| 18685 | let mode = dec.symbol(&mut cdfs.motion_mode[bsize_idx]); |
| 18686 | match mode { |
| 18687 | 0 => {} |
| 18688 | 1 => { |
| 18689 | obmc_selected = true; |
| 18690 | OBMC_HITS.with(|c| c.set(c.get() + 1)); |
| 18691 | } |
| 18692 | _ => { |
| 18693 | warped_selected = true; |
| 18694 | WARP_SELECTED_HITS.with(|c| c.set(c.get() + 1)); |
| 18695 | let mut samples = find_samples( |
| 18696 | grid, |
| 18697 | mi_row, |
| 18698 | mi_col, |
| 18699 | bw4, |
| 18700 | bh4, |
| 18701 | mi_cols as usize, |
| 18702 | mi_rows as usize, |
| 18703 | ref_frame, |
| 18704 | ); |
| 18705 | if std::env::var_os("EC_WARP_DEBUG").is_some() { |
| 18706 | eprintln!( |
| 18707 | "EC_WARP_DEBUG findSamples mi_row={mi_row} mi_col={mi_col} bsize={side} num_proj_ref={}", |
| 18708 | samples.len() |
| 18709 | ); |
| 18710 | for (i, s) in samples.iter().enumerate() { |
| 18711 | eprintln!("EC_WARP_DEBUG sample[{i}] pts={:?} pts_inref={:?}", s.pts1, s.pts2); |
| 18712 | } |
| 18713 | } |
| 18714 | if samples.len() > 1 { |
| 18715 | crate::warp::select_samples(mv, &mut samples, side as i32, side as i32); |
| 18716 | } |
| 18717 | warp_params = crate::warp::find_projection( |
| 18718 | &samples, |
| 18719 | // lane-rect r2: the block's TRUE dims -- a |
| 18720 | // 32x16 strip's model center is not a |
| 18721 | // 32x32's (aom av1_find_projection bw/bh). |
| 18722 | write_w as i32, |
| 18723 | write_h as i32, |
| 18724 | mv.1, |
| 18725 | mv.0, |
| 18726 | mi_row as i32, |
| 18727 | mi_col as i32, |
| 18728 | ); |
| 18729 | if std::env::var_os("EC_WARP_DEBUG").is_some() { |
| 18730 | eprintln!( |
| 18731 | "EC_WARP_DEBUG projection mi_row={mi_row} mi_col={mi_col} num_proj_ref(final)={} mv=({},{}) params={:?}", |
| 18732 | samples.len(), mv.0, mv.1, warp_params |
| 18733 | ); |
| 18734 | for (i, s) in samples.iter().enumerate() { |
| 18735 | eprintln!("EC_WARP_DEBUG sample_used[{i}] pts={:?} pts_inref={:?}", s.pts1, s.pts2); |
| 18736 | } |
| 18737 | } |
| 18738 | } |
| 18739 | } |
| 18740 | } else { |
| 18741 | obmc_selected = dec.symbol(&mut cdfs.obmc[bsize_idx]) == 1; |
| 18742 | if obmc_selected { |
| 18743 | OBMC_HITS.with(|c| c.set(c.get() + 1)); |
| 18744 | } |
| 18745 | } |
| 18746 | } |
| 18747 | // lane-gm r4: `allow_warp`'s `global_warp_allowed` branch |
| 18748 | // (`reconinter.c:33-55`), gated INDEPENDENTLY of `motion_mode` -- |
| 18749 | // libaom's `av1_init_warp_params` runs for every inter |
| 18750 | // predictor build, not just a `WARPED_CAUSAL`-selected one. Only |
| 18751 | // reachable when `warp_params` is still `None` here: a block |
| 18752 | // that read (and got) local `WARPED_CAUSAL` above already has |
| 18753 | // `local_warp_allowed && !wm_params.invalid`, which `allow_warp` |
| 18754 | // checks FIRST and short-circuits on -- the global branch is an |
| 18755 | // `else if`, never layered on top. `is_global_mv_block` already |
| 18756 | // encodes `allow_warp`'s size bound (`min(bw4,bh4) >= 2`, i.e. |
| 18757 | // >=8px both dims, matching `av1_init_warp_params`'s |
| 18758 | // `block_height < 8 || block_width < 8` early return) and, via |
| 18759 | // `motion_mode_eligible`'s own suppression above, the |
| 18760 | // `force_integer_mv` case never reads a motion_mode symbol at |
| 18761 | // all -- checked again here directly since this branch fires |
| 18762 | // independent of `motion_mode_eligible`. |
| 18763 | let gm_ref = &global_motion[(ref_frame - LAST_FRAME) as usize]; |
| 18764 | if warp_params.is_none() && is_global_mv_block && !force_integer_mv && !gm_ref.invalid { |
| 18765 | warp_params = crate::warp::global_warp_params(gm_ref.params); |
| 18766 | if warp_params.is_some() && gm_ref.model == ec_av1_syntax::WarpModel::Affine { |
| 18767 | AFFINE_GM_HITS.with(|c| c.set(c.get() + 1)); |
| 18768 | } |
| 18769 | } |
| 18770 | if std::env::var_os("EC_AV1_TELL").is_some() { |
| 18771 | eprintln!( |
| 18772 | "TELL mi_row={mi_row} mi_col={mi_col} label=post_motion_mode eligible={} tell={} range={}", |
| 18773 | motion_mode_eligible as u8, dec.debug_bitpos(), dec.debug_state().0 |
| 18774 | ); |
| 18775 | } |
| 18776 | // spec `get_ref_filter_type`: a neighbour's own filter only feeds |
| 18777 | // this block's switchable_interp context when that neighbour coded |
| 18778 | // the SAME reference frame this block is about to read -- a |
| 18779 | // different-ref neighbour reads as "no neighbour" ([3, 3], the |
| 18780 | // sentinel `Neighbours::new` already seeds unset slots with), |
| 18781 | // exactly like an intra neighbour. Without this gate, a GOLDEN_FRAME |
| 18782 | // block next to a LAST_FRAME one inherits that neighbour's filter |
| 18783 | // choice into its own context, corrupting the symbol it reads. |
| 18784 | // lane-av1idx r2: also match on the neighbour's SECOND reference |
| 18785 | // (`above_ref1`/`left_ref1`, real for a compound neighbour) -- |
| 18786 | // see the 16x16 leaf's own comment above for the spec citation. |
| 18787 | let above_filter_ctx = if neighbours.above_ref[cmi] == ref_frame |
| 18788 | || neighbours.above_ref1[cmi] == Some(ref_frame) |
| 18789 | { |
| 18790 | neighbours.above_filter[cmi] |
| 18791 | } else { |
| 18792 | [3, 3] |
| 18793 | }; |
| 18794 | let left_filter_ctx = if neighbours.left_ref[rmi] == ref_frame |
| 18795 | || neighbours.left_ref1[rmi] == Some(ref_frame) |
| 18796 | { |
| 18797 | neighbours.left_filter[rmi] |
| 18798 | } else { |
| 18799 | [3, 3] |
| 18800 | }; |
| 18801 | let (h_filter, v_filter, resolved_filter) = resolve_interp_filter( |
| 18802 | dec, |
| 18803 | cdfs, |
| 18804 | interp_fixed, |
| 18805 | enable_dual_filter, |
| 18806 | gm_nontrans || warped_selected, |
| 18807 | above_filter_ctx, |
| 18808 | left_filter_ctx, |
| 18809 | false, |
| 18810 | ); |
| 18811 | block_filter = resolved_filter; |
| 18812 | globalmv_for_lf = is_globalmv; |
| 18813 | if std::env::var_os("EC_AV1_TELL").is_some() { |
| 18814 | eprintln!( |
| 18815 | "TELL mi_row={mi_row} mi_col={mi_col} label=post_interp_filter tell={} range={}", |
| 18816 | dec.debug_bitpos(), dec.debug_state().0 |
| 18817 | ); |
| 18818 | } |
| 18819 | if std::env::var_os("EC_AV1_TRACE").is_some() { |
| 18820 | eprintln!( |
| 18821 | "EC_TRACE mi_row={mi_row} mi_col={mi_col} skip={} is_inter=1 mv=({},{}) is_new_mv={is_new_mv} bsize={side} ref={ref_frame} filter={:?} motion_mode_eligible={} obmc_selected={} warped={} tell={}", |
| 18822 | skip as u8, mv.0, mv.1, block_filter, motion_mode_eligible as u8, obmc_selected as u8, warp_params.is_some() as u8, dec.debug_bitpos() |
| 18823 | ); |
| 18824 | } |
| 18825 | // lane-rect r2: see the compound path's matching comment above -- |
| 18826 | // `grid` must be stamped with the block's true bh4/bw4 span, not |
| 18827 | // a square `bw4`x`bw4` guess. |
| 18828 | for dr in 0..bh4 { |
| 18829 | for dc in 0..bw4 { |
| 18830 | grid.set( |
| 18831 | mi_row + dr, |
| 18832 | mi_col + dc, |
| 18833 | MiInfo { |
| 18834 | is_inter: true, |
| 18835 | ref_frame, |
| 18836 | // An interintra block records ref_frame[1] == |
| 18837 | // INTRA_FRAME (0): warp-sample gathering |
| 18838 | // (libaom av1_findSamples, mvref_common.c:1155) |
| 18839 | // requires ref_frame[1] == NONE_FRAME, so such a |
| 18840 | // neighbour must not donate samples or count in |
| 18841 | // num_proj_ref. |
| 18842 | ref_frame1: interintra_mode.map(|_| 0), |
| 18843 | mv1: None, |
| 18844 | mv, |
| 18845 | is_new_mv, |
| 18846 | size: bw4, |
| 18847 | size_h: bh4, |
| 18848 | is_global_mv0: is_global_mv_block, |
| 18849 | is_global_mv1: false, |
| 18850 | }, |
| 18851 | ); |
| 18852 | } |
| 18853 | } |
| 18854 | mode_for_tx = 0; |
| 18855 | uv_predict_mode = DC_PRED; |
| 18856 | |
| 18857 | // lane-superres r9: spec 7.11.3.3's x_scale_fp comes from LUMA |
| 18858 | // widths only (r8's derivation) and applies unchanged to the |
| 18859 | // chroma calls below (their x_q4 is already in the chroma |
| 18860 | // plane's own pixel units). `mc::predict_scaled` has no warp/OBMC |
| 18861 | // /interintra counterpart -- those three combinations are refused |
| 18862 | // by name instead of silently sampling a scaled reference wrong |
| 18863 | // (warp_params/obmc_selected/interintra_mode are all resolved by |
| 18864 | // this point, every symbol for this block already read). |
| 18865 | let luma_scale = mc::scale_factor(py_ref.width, frame_width); |
| 18866 | if luma_scale != mc::REF_NO_SCALE { |
| 18867 | // lane-scaledref r1: libaom `allow_warp` |
| 18868 | // (av1/common/reconinter.c:41) suppresses BOTH local and |
| 18869 | // global warp under a scaled reference and predicts |
| 18870 | // translationally instead -- that fallback is implemented |
| 18871 | // below, but the one real aomenc stream this round found |
| 18872 | // that reaches it (`--enable-warped-motion=1`, |
| 18873 | // `--superres-denominator=16`, seed 47) still mismatches |
| 18874 | // ffmpeg at frame 2 luma, so the case stays refused by name |
| 18875 | // until it has a green gate rather than shipping wrong |
| 18876 | // pixels behind a lifted refusal. |
| 18877 | if warp_params.is_some() { |
| 18878 | SCALED_WARP_FALLBACK_HITS.with(|c| c.set(c.get() + 1)); |
| 18879 | return Err(unsupported( |
| 18880 | "warp prediction with a scaled reference (superres, unimplemented)", |
| 18881 | )); |
| 18882 | } |
| 18883 | if obmc_selected { |
| 18884 | SCALED_OBMC_HITS.with(|c| c.set(c.get() + 1)); |
| 18885 | } |
| 18886 | if interintra_mode.is_some() { |
| 18887 | SCALED_INTERINTRA_HITS.with(|c| c.set(c.get() + 1)); |
| 18888 | } |
| 18889 | } |
| 18890 | |
| 18891 | let mut pred_y = vec![0u16; side * side]; |
| 18892 | let mut pred_u = vec![0u16; chroma_side * chroma_side]; |
| 18893 | let mut pred_v = vec![0u16; chroma_side * chroma_side]; |
| 18894 | if luma_scale == mc::REF_NO_SCALE { |
| 18895 | mc::predict_with_filters( |
| 18896 | &py_ref.data, |
| 18897 | py_ref.width, |
| 18898 | py_ref.true_width, |
| 18899 | py_ref.true_height, |
| 18900 | mv_to_q4(px, mv.1, true), |
| 18901 | mv_to_q4(py, mv.0, true), |
| 18902 | side, |
| 18903 | side, |
| 18904 | h_filter, |
| 18905 | v_filter, |
| 18906 | &mut pred_y, |
| 18907 | ); |
| 18908 | mc::predict_with_filters( |
| 18909 | &pu_ref.data, |
| 18910 | pu_ref.width, |
| 18911 | pu_ref.true_width, |
| 18912 | pu_ref.true_height, |
| 18913 | mv_to_q4(cpx, mv.1, false), |
| 18914 | mv_to_q4(cpy, mv.0, false), |
| 18915 | chroma_side, |
| 18916 | chroma_side, |
| 18917 | h_filter, |
| 18918 | v_filter, |
| 18919 | &mut pred_u, |
| 18920 | ); |
| 18921 | mc::predict_with_filters( |
| 18922 | &pv_ref.data, |
| 18923 | pv_ref.width, |
| 18924 | pv_ref.true_width, |
| 18925 | pv_ref.true_height, |
| 18926 | mv_to_q4(cpx, mv.1, false), |
| 18927 | mv_to_q4(cpy, mv.0, false), |
| 18928 | chroma_side, |
| 18929 | chroma_side, |
| 18930 | h_filter, |
| 18931 | v_filter, |
| 18932 | &mut pred_v, |
| 18933 | ); |
| 18934 | } else { |
| 18935 | mc::predict_scaled( |
| 18936 | &py_ref.data, |
| 18937 | py_ref.width, |
| 18938 | py_ref.true_width, |
| 18939 | py_ref.true_height, |
| 18940 | mv_to_q4(px, mv.1, true), |
| 18941 | mv_to_q4(py, mv.0, true), |
| 18942 | luma_scale, |
| 18943 | side, |
| 18944 | side, |
| 18945 | h_filter, |
| 18946 | v_filter, |
| 18947 | &mut pred_y, |
| 18948 | ); |
| 18949 | mc::predict_scaled( |
| 18950 | &pu_ref.data, |
| 18951 | pu_ref.width, |
| 18952 | pu_ref.true_width, |
| 18953 | pu_ref.true_height, |
| 18954 | mv_to_q4(cpx, mv.1, false), |
| 18955 | mv_to_q4(cpy, mv.0, false), |
| 18956 | luma_scale, |
| 18957 | chroma_side, |
| 18958 | chroma_side, |
| 18959 | h_filter, |
| 18960 | v_filter, |
| 18961 | &mut pred_u, |
| 18962 | ); |
| 18963 | mc::predict_scaled( |
| 18964 | &pv_ref.data, |
| 18965 | pv_ref.width, |
| 18966 | pv_ref.true_width, |
| 18967 | pv_ref.true_height, |
| 18968 | mv_to_q4(cpx, mv.1, false), |
| 18969 | mv_to_q4(cpy, mv.0, false), |
| 18970 | luma_scale, |
| 18971 | chroma_side, |
| 18972 | chroma_side, |
| 18973 | h_filter, |
| 18974 | v_filter, |
| 18975 | &mut pred_v, |
| 18976 | ); |
| 18977 | } |
| 18978 | |
| 18979 | // lane-scaledref r1: libaom `allow_warp` (av1/common/reconinter.c:41) |
| 18980 | // opens with `if (av1_is_scaled(sf)) return 0;` -- BOTH local |
| 18981 | // (`wm_params`) and global warp fall back to the translational |
| 18982 | // prediction above when the reference is scaled, even though the |
| 18983 | // motion_mode symbol was still read and warp_params still |
| 18984 | // resolved. Suppress the warp, keep every symbol read. |
| 18985 | if let Some(params) = warp_params.as_ref().filter(|_| luma_scale == mc::REF_NO_SCALE) { |
| 18986 | crate::warp::warp_affine( |
| 18987 | params, &py_ref.data, py_ref.true_width as i32, py_ref.true_height as i32, |
| 18988 | py_ref.width as i32, &mut pred_y, px as i32, py as i32, side as i32, |
| 18989 | side as i32, side as i32, 0, 0, |
| 18990 | ); |
| 18991 | // libaom `av1_init_warp_params` (reconinter.c): warp is |
| 18992 | // per PLANE and bails out at `block_width < 8 || |
| 18993 | // block_height < 8`, so a plane whose own block is under |
| 18994 | // 8x8 keeps the translational prediction built above -- |
| 18995 | // in 420 that is every chroma plane of a luma block below |
| 18996 | // 16x16 (lane-gmaffine r4: chroma-only mismatch of a few |
| 18997 | // levels on both 8x8-leaf motion gates). |
| 18998 | if chroma_side >= 8 { |
| 18999 | crate::warp::warp_affine( |
| 19000 | params, &pu_ref.data, pu_ref.true_width as i32, pu_ref.true_height as i32, |
| 19001 | pu_ref.width as i32, &mut pred_u, cpx as i32, cpy as i32, chroma_side as i32, |
| 19002 | chroma_side as i32, chroma_side as i32, 1, 1, |
| 19003 | ); |
| 19004 | crate::warp::warp_affine( |
| 19005 | params, &pv_ref.data, pv_ref.true_width as i32, pv_ref.true_height as i32, |
| 19006 | pv_ref.width as i32, &mut pred_v, cpx as i32, cpy as i32, chroma_side as i32, |
| 19007 | chroma_side as i32, chroma_side as i32, 1, 1, |
| 19008 | ); |
| 19009 | } |
| 19010 | } |
| 19011 | |
| 19012 | if obmc_selected { |
| 19013 | obmc_blend( |
| 19014 | grid, |
| 19015 | neighbours, |
| 19016 | mi_row, |
| 19017 | mi_col, |
| 19018 | bw4, |
| 19019 | bh4, |
| 19020 | mi_rows as usize, |
| 19021 | mi_cols as usize, |
| 19022 | side, |
| 19023 | write_w, |
| 19024 | write_h, |
| 19025 | chroma_side, |
| 19026 | px, |
| 19027 | py, |
| 19028 | cpx, |
| 19029 | cpy, |
| 19030 | ref_y, |
| 19031 | ref_u, |
| 19032 | ref_v, |
| 19033 | other_refs, |
| 19034 | interp_fixed, |
| 19035 | frame_width, |
| 19036 | &mut pred_y, |
| 19037 | &mut pred_u, |
| 19038 | &mut pred_v, |
| 19039 | )?; |
| 19040 | } |
| 19041 | |
| 19042 | // Mutually exclusive with warp/OBMC (motion_mode is not read for |
| 19043 | // an interintra block): blend the intra predictor over the MC |
| 19044 | // result, all planes (reconinter.c av1_build_interintra_predictor |
| 19045 | // plane loop). |
| 19046 | if let Some(ii) = interintra_mode { |
| 19047 | interintra_blend(y, px, py, side, ii, wedge_mask, &mut pred_y); |
| 19048 | interintra_blend(u, cpx, cpy, chroma_side, ii, wedge_mask, &mut pred_u); |
| 19049 | interintra_blend(v, cpx, cpy, chroma_side, ii, wedge_mask, &mut pred_v); |
| 19050 | } |
| 19051 | |
| 19052 | // lane-inter4 r2: a rectangular strip's transform tree starts from |
| 19053 | // its own rect `max_txsize_rect_lookup` entry, not a square one. |
| 19054 | let rect_tu = (write_w, write_h) != (side, side); |
| 19055 | vartx_leaves = if rect_tu { |
| 19056 | read_block_tx_size_rect( |
| 19057 | dec, |
| 19058 | cdfs, |
| 19059 | neighbours, |
| 19060 | at_mi, |
| 19061 | (write_w, write_h), |
| 19062 | (mi_cols as usize, mi_rows as usize), |
| 19063 | skip, |
| 19064 | )? |
| 19065 | } else { |
| 19066 | read_block_tx_size( |
| 19067 | dec, |
| 19068 | cdfs, |
| 19069 | neighbours, |
| 19070 | at_mi, |
| 19071 | side, |
| 19072 | (mi_cols as usize, mi_rows as usize), |
| 19073 | true, |
| 19074 | skip, |
| 19075 | )? |
| 19076 | .1 |
| 19077 | }; |
| 19078 | if skip { |
| 19079 | y.reconstruct_mc_rect( |
| 19080 | px, |
| 19081 | py, |
| 19082 | side, |
| 19083 | write_w, |
| 19084 | write_h, |
| 19085 | &pred_y, |
| 19086 | &vec![0i32; side * side], |
| 19087 | ); |
| 19088 | u.reconstruct_mc_rect( |
| 19089 | cpx, |
| 19090 | cpy, |
| 19091 | chroma_side, |
| 19092 | write_chroma_w, |
| 19093 | write_chroma_h, |
| 19094 | &pred_u, |
| 19095 | &vec![0i32; chroma_side * chroma_side], |
| 19096 | ); |
| 19097 | v.reconstruct_mc_rect( |
| 19098 | cpx, |
| 19099 | cpy, |
| 19100 | chroma_side, |
| 19101 | write_chroma_w, |
| 19102 | write_chroma_h, |
| 19103 | &pred_v, |
| 19104 | &vec![0i32; chroma_side * chroma_side], |
| 19105 | ); |
| 19106 | luma_grid = vec![0i32; side * side]; |
| 19107 | u_grid = vec![0i32; chroma_side * chroma_side]; |
| 19108 | v_grid = vec![0i32; chroma_side * chroma_side]; |
| 19109 | } else { |
| 19110 | // lane-inter4 r2: a rect strip's coefficient context spans its |
| 19111 | // OWN width above and height left -- reading `side` cells both |
| 19112 | // ways indexes past the tile's last mi row for a bottom strip. |
| 19113 | let around = if rect_tu { |
| 19114 | neighbours.around_mi_rect(at_mi, write_w, write_h) |
| 19115 | } else { |
| 19116 | neighbours.around_mi(at, side) |
| 19117 | }; |
| 19118 | let luma_tx_type; |
| 19119 | if let Some(leaves) = vartx_leaves.clone() { |
| 19120 | // spec 5.11.17's transform tree, leaf by leaf in the order |
| 19121 | // `read_var_tx_size` coded it: each unit reads its own |
| 19122 | // coefficients against the neighbour magnitudes the |
| 19123 | // earlier units already wrote, and reconstructs over its |
| 19124 | // own slice of the block's motion-compensated predictor. |
| 19125 | let reduced = REDUCED_TX_SET_INTER.with(std::cell::Cell::get); |
| 19126 | let mut first_tx_type = TxType::DctDct; |
| 19127 | for (idx, &(row, col, tw, th)) in leaves.iter().enumerate() { |
| 19128 | let tu_mi = (at_mi.0 + row, at_mi.1 + col); |
| 19129 | let (tu_px, tu_py) = (px + col * MI, py + row * MI); |
| 19130 | let (tu_grid, tu_tx_type) = if tw == th { |
| 19131 | let tx_px = tw; |
| 19132 | let mut tu_pred = Vec::with_capacity(tx_px * tx_px); |
| 19133 | for rr in 0..tx_px { |
| 19134 | let start = (row * MI + rr) * side + col * MI; |
| 19135 | tu_pred.extend_from_slice(&pred_y[start..start + tx_px]); |
| 19136 | } |
| 19137 | let scan = default_scan(tx_px); |
| 19138 | let tu_skip_ctx = neighbours.luma_skip_ctx(tu_mi, tx_px / MI); |
| 19139 | read_inter_plane( |
| 19140 | dec, |
| 19141 | cdfs, |
| 19142 | inter_txbset_for(tx_px, reduced), |
| 19143 | &scan, |
| 19144 | 0, |
| 19145 | neighbours.around_mi(tu_mi, tx_px)[0], |
| 19146 | mode_for_tx, |
| 19147 | y, |
| 19148 | tu_px, |
| 19149 | tu_py, |
| 19150 | tx_px, |
| 19151 | base_q_idx, |
| 19152 | &tu_pred, |
| 19153 | None, |
| 19154 | Some(tu_skip_ctx), |
| 19155 | )? |
| 19156 | } else { |
| 19157 | // lane-r14 r3: a 1:4 strip's `sub_tx_size_map` |
| 19158 | // sub-transform is RECTANGULAR (TX_64X16 -> |
| 19159 | // TX_32X16, TX_16X64 -> TX_16X32), so this leaf is |
| 19160 | // read with the rect coefficient machinery, at its |
| 19161 | // own `txb_skip_ctx`/neighbour spans -- the unit is |
| 19162 | // smaller than its block, so `get_txb_ctx_general`'s |
| 19163 | // neighbour table applies, not the lone-TU 0. |
| 19164 | vartx_rect_leaf_hit(tw, th); |
| 19165 | let tu_skip_ctx = |
| 19166 | neighbours.luma_skip_ctx_rect(tu_mi, tw / MI, th / MI); |
| 19167 | let start = row * MI * side + col * MI; |
| 19168 | read_inter_plane_rect( |
| 19169 | dec, |
| 19170 | cdfs, |
| 19171 | rect_inter_luma_set(tw, th), |
| 19172 | (tw, th), |
| 19173 | side, |
| 19174 | 0, |
| 19175 | neighbours.around_mi_rect(tu_mi, tw, th)[0], |
| 19176 | mode_for_tx, |
| 19177 | y, |
| 19178 | tu_px, |
| 19179 | tu_py, |
| 19180 | &pred_y[start..], |
| 19181 | None, |
| 19182 | Some(tu_skip_ctx), |
| 19183 | )? |
| 19184 | }; |
| 19185 | if idx == 0 { |
| 19186 | first_tx_type = tu_tx_type; |
| 19187 | } |
| 19188 | neighbours.record_mi_luma_rect(tu_mi, tw, th, &tu_grid); |
| 19189 | } |
| 19190 | // `av1_get_tx_type`'s chroma lookup scales the chroma |
| 19191 | // position back to luma, so an inter block's chroma |
| 19192 | // inherits the *first* (top-left) luma unit's coded type. |
| 19193 | luma_tx_type = first_tx_type; |
| 19194 | luma_grid = vec![0i32; side * side]; |
| 19195 | } else if rect_tu { |
| 19196 | // The whole-block rectangular transform unit |
| 19197 | // (`txfm_partition` said 0 at depth 0). |
| 19198 | let (grid, tx_type) = read_inter_plane_rect( |
| 19199 | dec, |
| 19200 | cdfs, |
| 19201 | rect_inter_luma_set(write_w, write_h), |
| 19202 | (write_w, write_h), |
| 19203 | side, |
| 19204 | 0, |
| 19205 | around[0], |
| 19206 | mode_for_tx, |
| 19207 | y, |
| 19208 | px, |
| 19209 | py, |
| 19210 | &pred_y, |
| 19211 | None, |
| 19212 | None, |
| 19213 | )?; |
| 19214 | luma_tx_type = tx_type; |
| 19215 | luma_grid = embed_rect_grid(&grid, write_w, write_h, side); |
| 19216 | } else { |
| 19217 | (luma_grid, luma_tx_type) = read_inter_plane( |
| 19218 | dec, |
| 19219 | cdfs, |
| 19220 | luma_set_inter, |
| 19221 | scan_luma, |
| 19222 | 0, |
| 19223 | around[0], |
| 19224 | mode_for_tx, |
| 19225 | y, |
| 19226 | px, |
| 19227 | py, |
| 19228 | side, |
| 19229 | base_q_idx, |
| 19230 | &pred_y, |
| 19231 | None, |
| 19232 | None, |
| 19233 | )?; |
| 19234 | } |
| 19235 | if rect_tu { |
| 19236 | // `av1_get_max_uv_txsize(bsize)`: the chroma transform of a |
| 19237 | // 2:1 strip is the strip's own rect chroma size (16x8 / |
| 19238 | // 8x16), one unit for the whole block, and it codes no |
| 19239 | // `tx_type` symbol -- it inherits the luma unit's. |
| 19240 | let (uw, uh) = (write_chroma_w, write_chroma_h); |
| 19241 | u_grid = embed_rect_grid( |
| 19242 | &read_inter_plane_rect( |
| 19243 | dec, |
| 19244 | cdfs, |
| 19245 | rect_inter_chroma_set(write_chroma_w, write_chroma_h), |
| 19246 | (uw, uh), |
| 19247 | chroma_side, |
| 19248 | 1, |
| 19249 | around[1], |
| 19250 | mode_for_tx, |
| 19251 | u, |
| 19252 | cpx, |
| 19253 | cpy, |
| 19254 | &pred_u, |
| 19255 | Some(luma_tx_type), |
| 19256 | None, |
| 19257 | )? |
| 19258 | .0, |
| 19259 | uw, |
| 19260 | uh, |
| 19261 | chroma_side, |
| 19262 | ); |
| 19263 | v_grid = embed_rect_grid( |
| 19264 | &read_inter_plane_rect( |
| 19265 | dec, |
| 19266 | cdfs, |
| 19267 | rect_inter_chroma_set(write_chroma_w, write_chroma_h), |
| 19268 | (uw, uh), |
| 19269 | chroma_side, |
| 19270 | 2, |
| 19271 | around[2], |
| 19272 | mode_for_tx, |
| 19273 | v, |
| 19274 | cpx, |
| 19275 | cpy, |
| 19276 | &pred_v, |
| 19277 | Some(luma_tx_type), |
| 19278 | None, |
| 19279 | )? |
| 19280 | .0, |
| 19281 | uw, |
| 19282 | uh, |
| 19283 | chroma_side, |
| 19284 | ); |
| 19285 | } else { |
| 19286 | u_grid = read_inter_plane( |
| 19287 | dec, |
| 19288 | cdfs, |
| 19289 | chroma_set, |
| 19290 | scan_chroma, |
| 19291 | 1, |
| 19292 | around[1], |
| 19293 | mode_for_tx, |
| 19294 | u, |
| 19295 | cpx, |
| 19296 | cpy, |
| 19297 | chroma_side, |
| 19298 | base_q_idx, |
| 19299 | &pred_u, |
| 19300 | Some(luma_tx_type), |
| 19301 | None, |
| 19302 | )? |
| 19303 | .0; |
| 19304 | v_grid = read_inter_plane( |
| 19305 | dec, |
| 19306 | cdfs, |
| 19307 | chroma_set, |
| 19308 | scan_chroma, |
| 19309 | 2, |
| 19310 | around[2], |
| 19311 | mode_for_tx, |
| 19312 | v, |
| 19313 | cpx, |
| 19314 | cpy, |
| 19315 | chroma_side, |
| 19316 | base_q_idx, |
| 19317 | &pred_v, |
| 19318 | Some(luma_tx_type), |
| 19319 | None, |
| 19320 | )? |
| 19321 | .0; |
| 19322 | } |
| 19323 | } |
| 19324 | } |
| 19325 | } else { |
| 19326 | // lane-partitions r1: intra prediction (`PlaneBuf::reconstruct`) has |
| 19327 | // no rectangular counterpart -- an intra-coded HORZ/VERT strip needs |
| 19328 | // its own true-width/height edge/predict math, unlike the inter |
| 19329 | // skip path above (which only needed a clipped write of an |
| 19330 | // already-square-predicted buffer). Named refusal instead of a |
| 19331 | // silently wrong square-shaped intra prediction. |
| 19332 | if write_w != side || write_h != side { |
| 19333 | // lane-intrarect r1: the strip decodes through its own rect |
| 19334 | // machinery ([`decode_intra_rect_in_inter`] -> the key-frame |
| 19335 | // paths' [`decode_rect_split`]) instead of refusing. That call |
| 19336 | // already writes everything the shared tail below would for a |
| 19337 | // square block EXCEPT the inter-side bands and the mi map, so |
| 19338 | // this arm returns rather than falling through (a second |
| 19339 | // `record_rect`/`fill_lf_grid_rect` at block granularity would |
| 19340 | // overwrite the per-transform-unit records it just made). |
| 19341 | decode_intra_rect_in_inter( |
| 19342 | dec, |
| 19343 | cdfs, |
| 19344 | neighbours, |
| 19345 | at, |
| 19346 | write_w, |
| 19347 | write_h, |
| 19348 | skip, |
| 19349 | allow_screen_content_tools, |
| 19350 | y, |
| 19351 | u, |
| 19352 | v, |
| 19353 | base_q_idx, |
| 19354 | )?; |
| 19355 | // merge b51719f+lane-intrarect: `at` is in MI units on this path |
| 19356 | // (lane-inter4), so the strip's mi origin is `at` itself -- the |
| 19357 | // lane's `r * SUB_MI` re-derivation would snap a sub-SUB strip to |
| 19358 | // its enclosing 16-px cell. |
| 19359 | let (mi_r0, mi_c0) = (rmi, cmi); |
| 19360 | for dr in 0..write_h / MI { |
| 19361 | for dc in 0..write_w / MI { |
| 19362 | grid.set( |
| 19363 | mi_r0 + dr, |
| 19364 | mi_c0 + dc, |
| 19365 | MiInfo { |
| 19366 | is_inter: false, |
| 19367 | ref_frame: -1, |
| 19368 | ref_frame1: None, |
| 19369 | mv1: None, |
| 19370 | mv: (0, 0), |
| 19371 | is_new_mv: false, |
| 19372 | size: write_w / MI, |
| 19373 | size_h: write_h / MI, |
| 19374 | is_global_mv0: false, |
| 19375 | is_global_mv1: false, |
| 19376 | }, |
| 19377 | ); |
| 19378 | } |
| 19379 | } |
| 19380 | neighbours.record_inter_rect_mi( |
| 19381 | at, |
| 19382 | write_w / MI, |
| 19383 | write_h / MI, |
| 19384 | skip, |
| 19385 | false, |
| 19386 | 0, |
| 19387 | [3, 3], |
| 19388 | skip_mode, |
| 19389 | ); |
| 19390 | return Ok(()); |
| 19391 | } |
| 19392 | let mode = dec.symbol(&mut cdfs.y_mode[size_group_wh(write_w, write_h)]); |
| 19393 | if std::env::var_os("EC_IIS").is_some() { |
| 19394 | eprintln!( |
| 19395 | "TRACE iis px={px} py={py} side={side} w={write_w} h={write_h} sg={} mode={mode} skip={skip}", |
| 19396 | size_group_wh(write_w, write_h) |
| 19397 | ); |
| 19398 | } |
| 19399 | if mode >= 13 { |
| 19400 | return Err(unsupported( |
| 19401 | "an intra mode this decoder does not code (round 2)", |
| 19402 | )); |
| 19403 | } |
| 19404 | // lane-angleinter r1: `read_intra_angle_info` (libaom `decodemv.c`, |
| 19405 | // inside `read_intra_block_mode_info`) -- an intra block in an inter |
| 19406 | // frame reads `angle_delta_y` off the same |
| 19407 | // `angle_delta_cdf[mode - V_PRED]` row a key-frame block does, and |
| 19408 | // the delta feeds the directional predictor (and the edge |
| 19409 | // filter/upsample decision derived from the delta'd angle) unchanged. |
| 19410 | // The symbol was read and then refused before this round even though |
| 19411 | // aomenc enables `--enable-angle-delta` by default. |
| 19412 | let angle_delta_y = if (V_PRED..=D67_PRED).contains(&mode) { |
| 19413 | read_angle_delta(dec, &mut cdfs.angle_delta[mode - V_PRED]) |
| 19414 | } else { |
| 19415 | 0 |
| 19416 | }; |
| 19417 | if angle_delta_y != 0 { |
| 19418 | INTRA_IN_INTER_ANGLE_DELTA_Y_HITS.with(|c| c.set(c.get() + 1)); |
| 19419 | } |
| 19420 | // The delta'd angle decides whether the intra edge filter runs at |
| 19421 | // all, so this block needs the same luma edge-filter-type neighbour |
| 19422 | // check a key-frame block does (`get_intra_edge_filter_type`, spec |
| 19423 | // 7.11.2) -- this arm passed a hardcoded `false` while every |
| 19424 | // directional intra-in-inter block was refused above. |
| 19425 | let (nb_above_mode, nb_left_mode) = neighbours.modes_above_left(r, c); |
| 19426 | let smooth_neighbor = is_smooth_mode(nb_above_mode) || is_smooth_mode(nb_left_mode); |
| 19427 | // lane-sbrect10 r1 (class wrong-alphabet-same-value, second instance |
| 19428 | // after lane-sbpart r3's rect strips): `is_cfl_allowed` (libaom |
| 19429 | // `blockd.h` -- `block_size_wide[bsize] <= 32 && block_size_high[bsize] |
| 19430 | // <= 32`, spec 5.11.5) caps CFL at 32x32, so a 64x64 intra block inside |
| 19431 | // an INTER frame reads the 13-symbol `uv_mode_no_cfl` alphabet. Reading |
| 19432 | // the 14-symbol CFL one gave the same DC_PRED VALUE but narrowed the |
| 19433 | // arithmetic coder by the wrong interval, desyncing the tile from this |
| 19434 | // block on. |
| 19435 | let cfl_allowed = write_w.max(write_h) <= 32; |
| 19436 | if !cfl_allowed { |
| 19437 | NOCFL_UV_MODE_HITS.with(|c| c.set(c.get() + 1)); |
| 19438 | } |
| 19439 | let uv_mode = if cfl_allowed { |
| 19440 | dec.symbol(&mut cdfs.uv_mode_cfl[mode]) |
| 19441 | } else { |
| 19442 | dec.symbol(&mut cdfs.uv_mode_no_cfl[mode]) |
| 19443 | }; |
| 19444 | if (9..=12).contains(&uv_mode) { |
| 19445 | SMOOTH_UV_HITS.with(|c| c.set(c.get() + 1)); |
| 19446 | } |
| 19447 | let alpha = if cfl_allowed && uv_mode == UV_CFL_PRED { |
| 19448 | Some(read_cfl_alphas(dec, cdfs)) |
| 19449 | } else { |
| 19450 | None |
| 19451 | }; |
| 19452 | // `get_uv_mode` (spec 9.3): `UV_CFL_PRED` predicts as `DC_PRED` for |
| 19453 | // the angle-delta question, same as [`read_intra_mode`]'s own copy |
| 19454 | // -- `uv_mode` (13) already falls outside `V_PRED..=D67_PRED` so the |
| 19455 | // `else` branch below is exact for it either way. |
| 19456 | let angle_delta_uv = if (V_PRED..=D67_PRED).contains(&uv_mode) { |
| 19457 | DIRECTIONAL_UV_HITS.with(|c| c.set(c.get() + 1)); |
| 19458 | read_angle_delta(dec, &mut cdfs.angle_delta[uv_mode - V_PRED]) |
| 19459 | } else { |
| 19460 | 0 |
| 19461 | }; |
| 19462 | if angle_delta_uv != 0 { |
| 19463 | UV_ANGLE_DELTA_HITS.with(|c| c.set(c.get() + 1)); |
| 19464 | INTRA_IN_INTER_ANGLE_DELTA_UV_HITS.with(|c| c.set(c.get() + 1)); |
| 19465 | } |
| 19466 | uv_predict_mode = if uv_mode == UV_CFL_PRED { |
| 19467 | DC_PRED |
| 19468 | } else { |
| 19469 | uv_mode |
| 19470 | }; |
| 19471 | // lane-chroma r4: same chroma edge-filter-strength neighbour check |
| 19472 | // as [`read_intra_mode`]/[`read_intra_mode_rect`] -- the CHROMA |
| 19473 | // neighbour's own `uv_mode`, not the luma one. |
| 19474 | let smooth_neighbor_uv = |
| 19475 | neighbours.smooth_uv_neighbour(r * (SUB / MI), c * (SUB / MI), r, c); |
| 19476 | // `read_palette_mode_info` (decodemv.c:567, called from |
| 19477 | // `read_intra_block_mode_info` right after `xd->cfl.store_y`, same |
| 19478 | // gating and same corner-cut as `read_intra_mode`'s own copy above |
| 19479 | // -- `palette_mode_ctx`/`palette_uv_mode_ctx` hardcoded 0, provably |
| 19480 | // safe since a nonzero neighbour `palette_size` would already have |
| 19481 | // refused the decode that produced it. |
| 19482 | if allow_screen_content_tools |
| 19483 | && let Some(bsize_ctx) = palette_bsize_ctx(side) |
| 19484 | { |
| 19485 | if mode == DC_PRED && dec.symbol(&mut cdfs.palette_y_mode[bsize_ctx][0]) != 0 { |
| 19486 | return Err(unsupported( |
| 19487 | "a block that actually uses a palette (Y) -- reconstruction is out of scope", |
| 19488 | )); |
| 19489 | } |
| 19490 | if uv_mode == DC_PRED && dec.symbol(&mut cdfs.palette_uv_mode[0]) != 0 { |
| 19491 | return Err(unsupported( |
| 19492 | "a block that actually uses a palette (UV) -- reconstruction is out of scope", |
| 19493 | )); |
| 19494 | } |
| 19495 | } |
| 19496 | // lane-fiinter r1: `read_filter_intra_mode_info` (libaom `decodemv.c`, |
| 19497 | // the tail of `read_intra_block_mode_info`) -- an intra block inside |
| 19498 | // an INTER frame reads `use_filter_intra` in exactly the same place a |
| 19499 | // key-frame block does, right after the palette syntax. Nothing read |
| 19500 | // it here before this round, and aomenc enables filter intra by |
| 19501 | // default, so every inter frame carrying one desynced at this symbol. |
| 19502 | // `av1_filter_intra_allowed`'s `palette_size[0] == 0` term is implied: |
| 19503 | // a real palette-Y block already returned above. |
| 19504 | let mut filter_intra = None; |
| 19505 | if mode == DC_PRED |
| 19506 | && ENABLE_FILTER_INTRA_INTER.with(std::cell::Cell::get) |
| 19507 | && let Some(class) = filter_intra_size_class(side) |
| 19508 | && dec.symbol(&mut cdfs.filter_intra[class]) != 0 |
| 19509 | { |
| 19510 | FILTER_INTRA_HITS.with(|c| c.set(c.get() + 1)); |
| 19511 | INTRA_IN_INTER_FILTER_INTRA_HITS.with(|c| c.set(c.get() + 1)); |
| 19512 | filter_intra = Some(dec.symbol(&mut cdfs.filter_intra_mode)); |
| 19513 | } |
| 19514 | mode_for_tx = mode; |
| 19515 | block_filter = [3, 3]; |
| 19516 | ref_frame_for_lf = 0; |
| 19517 | globalmv_for_lf = false; |
| 19518 | let (mi_row, mi_col) = (rmi, cmi); |
| 19519 | for dr in 0..side / 4 { |
| 19520 | for dc in 0..side / 4 { |
| 19521 | grid.set( |
| 19522 | mi_row + dr, |
| 19523 | mi_col + dc, |
| 19524 | MiInfo { |
| 19525 | is_inter: false, |
| 19526 | ref_frame: -1, |
| 19527 | ref_frame1: None, |
| 19528 | mv1: None, |
| 19529 | mv: (0, 0), |
| 19530 | is_new_mv: false, |
| 19531 | size: side / 4, |
| 19532 | size_h: side / 4, |
| 19533 | is_global_mv0: false, |
| 19534 | is_global_mv1: false, |
| 19535 | }, |
| 19536 | ); |
| 19537 | } |
| 19538 | } |
| 19539 | |
| 19540 | // lane-intrainter r1: an intra block in an inter frame reads the same |
| 19541 | // `tx_depth` symbol a key-frame block does, and when it splits, the |
| 19542 | // block codes one intra prediction + one coefficient set PER |
| 19543 | // transform unit (`decode_block`'s multi-TU branch), not one for the |
| 19544 | // whole block. The leaf list rides the existing `vartx_leaves` slot, |
| 19545 | // whose tail already does the split-block neighbour write-back |
| 19546 | // (`record_split_luma`) and the per-TU deblock edges. |
| 19547 | vartx_leaves = read_block_tx_size( |
| 19548 | dec, |
| 19549 | cdfs, |
| 19550 | neighbours, |
| 19551 | at_mi, |
| 19552 | side, |
| 19553 | (mi_cols as usize, mi_rows as usize), |
| 19554 | false, |
| 19555 | skip, |
| 19556 | )? |
| 19557 | .1; |
| 19558 | let reach = Reach::of(side, px, py, y.width, y.height); |
| 19559 | let split_luma = vartx_leaves.is_some(); |
| 19560 | if let Some(leaves) = vartx_leaves.clone() { |
| 19561 | let reduced = REDUCED_TX_SET_INTER.with(std::cell::Cell::get); |
| 19562 | for &(row, col, tx_px, _) in &leaves { |
| 19563 | let tu_mi = (at_mi.0 + row, at_mi.1 + col); |
| 19564 | let (tu_px, tu_py) = (px + col * MI, py + row * MI); |
| 19565 | // A transform unit inside a block is not a block at that |
| 19566 | // position (class from lane-palette2 r12): top-right / |
| 19567 | // bottom-left availability is the block's, narrowed by the |
| 19568 | // unit's own offset inside it. |
| 19569 | let tu_reach = Reach::of_tu(side, side, col * MI, row * MI, tx_px, tx_px, reach); |
| 19570 | let tu_grid = if skip { |
| 19571 | y.reconstruct( |
| 19572 | tu_px, |
| 19573 | tu_py, |
| 19574 | tx_px, |
| 19575 | mode, |
| 19576 | angle_delta_y, |
| 19577 | tu_reach, |
| 19578 | &vec![0i32; tx_px * tx_px], |
| 19579 | None, |
| 19580 | filter_intra, |
| 19581 | smooth_neighbor, |
| 19582 | ); |
| 19583 | vec![0i32; tx_px * tx_px] |
| 19584 | } else { |
| 19585 | // The unit's bsize is smaller than the block it sits in, |
| 19586 | // so `txb_skip_ctx` is the neighbour-magnitude table, not |
| 19587 | // the lone-TU 0 (`get_txb_ctx_general`). |
| 19588 | let tu_skip_ctx = neighbours.luma_skip_ctx(tu_mi, tx_px / MI); |
| 19589 | read_plane( |
| 19590 | dec, |
| 19591 | cdfs, |
| 19592 | txbset_for(tx_px, reduced), |
| 19593 | &default_scan(tx_px), |
| 19594 | 0, |
| 19595 | neighbours.around_mi(tu_mi, tx_px)[0], |
| 19596 | mode, |
| 19597 | mode, |
| 19598 | angle_delta_y, |
| 19599 | tu_reach, |
| 19600 | y, |
| 19601 | tu_px, |
| 19602 | tu_py, |
| 19603 | tx_px, |
| 19604 | tx_px, |
| 19605 | base_q_idx, |
| 19606 | None, |
| 19607 | filter_intra, |
| 19608 | Some(tu_skip_ctx), |
| 19609 | smooth_neighbor, |
| 19610 | )? |
| 19611 | }; |
| 19612 | neighbours.record_mi_luma(tu_mi, tx_px, &tu_grid); |
| 19613 | } |
| 19614 | } |
| 19615 | if skip { |
| 19616 | if !split_luma { |
| 19617 | y.reconstruct( |
| 19618 | px, |
| 19619 | py, |
| 19620 | side, |
| 19621 | mode, |
| 19622 | angle_delta_y, |
| 19623 | reach, |
| 19624 | &vec![0i32; side * side], |
| 19625 | None, |
| 19626 | filter_intra, |
| 19627 | smooth_neighbor, |
| 19628 | ); |
| 19629 | } |
| 19630 | let ac = alpha.map(|_| cfl_ac_q3(y, px, py, side)); |
| 19631 | u.reconstruct( |
| 19632 | cpx, |
| 19633 | cpy, |
| 19634 | chroma_side, |
| 19635 | uv_predict_mode, |
| 19636 | angle_delta_uv, |
| 19637 | reach, |
| 19638 | &vec![0i32; chroma_side * chroma_side], |
| 19639 | alpha.zip(ac.as_deref()).map(|((au, _), ac)| (au, ac)), |
| 19640 | None, |
| 19641 | smooth_neighbor_uv, |
| 19642 | ); |
| 19643 | v.reconstruct( |
| 19644 | cpx, |
| 19645 | cpy, |
| 19646 | chroma_side, |
| 19647 | uv_predict_mode, |
| 19648 | angle_delta_uv, |
| 19649 | reach, |
| 19650 | &vec![0i32; chroma_side * chroma_side], |
| 19651 | alpha.zip(ac.as_deref()).map(|((_, av), ac)| (av, ac)), |
| 19652 | None, |
| 19653 | smooth_neighbor_uv, |
| 19654 | ); |
| 19655 | luma_grid = vec![0i32; side * side]; |
| 19656 | u_grid = vec![0i32; chroma_side * chroma_side]; |
| 19657 | v_grid = vec![0i32; chroma_side * chroma_side]; |
| 19658 | } else { |
| 19659 | let around = neighbours.around_mi(at, side); |
| 19660 | luma_grid = if split_luma { |
| 19661 | // Already coded and reconstructed unit by unit above; the |
| 19662 | // block-level grid is only the neighbour write-back's input, |
| 19663 | // which `record_split_luma` ignores for plane 0. |
| 19664 | vec![0i32; side * side] |
| 19665 | } else { |
| 19666 | read_plane( |
| 19667 | dec, |
| 19668 | cdfs, |
| 19669 | luma_set_intra, |
| 19670 | scan_luma, |
| 19671 | 0, |
| 19672 | around[0], |
| 19673 | mode, |
| 19674 | mode, |
| 19675 | angle_delta_y, |
| 19676 | reach, |
| 19677 | y, |
| 19678 | px, |
| 19679 | py, |
| 19680 | side, |
| 19681 | luma_tx, |
| 19682 | base_q_idx, |
| 19683 | None, |
| 19684 | filter_intra, |
| 19685 | None, |
| 19686 | smooth_neighbor, |
| 19687 | )? |
| 19688 | }; |
| 19689 | let ac = alpha.map(|_| cfl_ac_q3(y, px, py, side)); |
| 19690 | u_grid = read_plane( |
| 19691 | dec, |
| 19692 | cdfs, |
| 19693 | chroma_set, |
| 19694 | scan_chroma, |
| 19695 | 1, |
| 19696 | around[1], |
| 19697 | mode, |
| 19698 | uv_predict_mode, |
| 19699 | angle_delta_uv, |
| 19700 | reach, |
| 19701 | u, |
| 19702 | cpx, |
| 19703 | cpy, |
| 19704 | chroma_side, |
| 19705 | chroma_tx, |
| 19706 | base_q_idx, |
| 19707 | alpha.zip(ac.as_deref()).map(|((au, _), ac)| (au, ac)), |
| 19708 | None, |
| 19709 | None, |
| 19710 | smooth_neighbor_uv, |
| 19711 | )?; |
| 19712 | v_grid = read_plane( |
| 19713 | dec, |
| 19714 | cdfs, |
| 19715 | chroma_set, |
| 19716 | scan_chroma, |
| 19717 | 2, |
| 19718 | around[2], |
| 19719 | mode, |
| 19720 | uv_predict_mode, |
| 19721 | angle_delta_uv, |
| 19722 | reach, |
| 19723 | v, |
| 19724 | cpx, |
| 19725 | cpy, |
| 19726 | chroma_side, |
| 19727 | chroma_tx, |
| 19728 | base_q_idx, |
| 19729 | alpha.zip(ac.as_deref()).map(|((_, av), ac)| (av, ac)), |
| 19730 | None, |
| 19731 | None, |
| 19732 | smooth_neighbor_uv, |
| 19733 | )?; |
| 19734 | } |
| 19735 | } |
| 19736 | // lane-comppin r9: end-of-block checkpoint -- everything between |
| 19737 | // post_assign_mv and here (interintra, motion_mode, interp_filter, then |
| 19738 | // every plane's coeffs) is unproven; a range mismatch here vs aomdec's |
| 19739 | // equivalent post-content point (added at the matching call site) |
| 19740 | // narrows the ladder to this block's own tail instead of the next |
| 19741 | // block's partition read. |
| 19742 | if std::env::var_os("EC_AV1_TELL").is_some() { |
| 19743 | eprintln!( |
| 19744 | "TELL mi_row={} mi_col={} label=block_end tell={} range={}", |
| 19745 | rmi, cmi, dec.debug_bitpos(), dec.debug_state().0 |
| 19746 | ); |
| 19747 | } |
| 19748 | if vartx_leaves.is_some() { |
| 19749 | // Plane 0 is already correct per transform unit |
| 19750 | // ([`Neighbours::record_mi_luma`] above); this writes everything else |
| 19751 | // [`Neighbours::record_rect`] would (a var-tx block is always square, |
| 19752 | // so `write_w == write_h == side`). |
| 19753 | neighbours.record_split_luma_rect_mi(at, side, side, mode_for_tx, uv_predict_mode, [&u_grid, &v_grid]); |
| 19754 | } else { |
| 19755 | neighbours.record_rect_mi( |
| 19756 | at, |
| 19757 | write_w, |
| 19758 | write_h, |
| 19759 | mode_for_tx, |
| 19760 | uv_predict_mode, |
| 19761 | &[luma_grid, u_grid, v_grid], |
| 19762 | ); |
| 19763 | } |
| 19764 | neighbours.record_inter_rect_mi( |
| 19765 | at, |
| 19766 | write_w / MI, |
| 19767 | write_h / MI, |
| 19768 | skip, |
| 19769 | is_inter, |
| 19770 | ref_frame_for_lf, |
| 19771 | block_filter, |
| 19772 | skip_mode, |
| 19773 | ); |
| 19774 | if let Some((ref1, group_idx, idx)) = compound_ctx { |
| 19775 | neighbours.record_compound_ctx_rect_mi(at, write_w / MI, write_h / MI, ref1, group_idx, idx); |
| 19776 | } |
| 19777 | neighbours.fill_skip_grid_rect( |
| 19778 | (rmi, cmi), |
| 19779 | write_w / MI, |
| 19780 | write_h / MI, |
| 19781 | skip, |
| 19782 | ); |
| 19783 | neighbours.fill_lf_grid_rect( |
| 19784 | (rmi, cmi), |
| 19785 | write_w / MI, |
| 19786 | write_h / MI, |
| 19787 | // lane-rect r2: the strip's true TX_32X16/TX_16X32 dims -- the |
| 19788 | // single-scalar corner-cut deblocked the strip seam with the wrong |
| 19789 | // edge length (1-px chroma drift on rect-flake-1 f17+). |
| 19790 | write_w as u8, |
| 19791 | write_h as u8, |
| 19792 | // ref_grid's packed convention (lf_level): sign carries GLOBALMV, |
| 19793 | // whose mode_lf_lut row is 0, not the 1 every other inter mode gets. |
| 19794 | if globalmv_for_lf { |
| 19795 | -ref_frame_for_lf |
| 19796 | } else { |
| 19797 | ref_frame_for_lf |
| 19798 | }, |
| 19799 | ); |
| 19800 | // lane-txselect: a var-tx block's deblock edges follow its own transform |
| 19801 | // units, not the block -- for LUMA only (lane-golomb r6): the chroma |
| 19802 | // grids keep the block's own `av1_get_max_uv_txsize`, which is what |
| 19803 | // libaom's `get_transform_size` reads for `plane != AOM_PLANE_Y`. |
| 19804 | if let Some(leaves) = &vartx_leaves { |
| 19805 | for &(row, col, tw, th) in leaves { |
| 19806 | // lane-r14's RECT leaf dims through lane-golomb r6's luma-only |
| 19807 | // writer: fill_lf_grid_rect would recompute CHROMA tx dims from |
| 19808 | // the leaf span, but libaom's `get_transform_size` splits only for |
| 19809 | // AOM_PLANE_Y (merge J, ledger constraint). |
| 19810 | neighbours.fill_lf_grid_leaf_luma( |
| 19811 | (at_mi.0 + row, at_mi.1 + col), |
| 19812 | tw / MI, |
| 19813 | th / MI, |
| 19814 | tw as u8, |
| 19815 | th as u8, |
| 19816 | ); |
| 19817 | } |
| 19818 | } |
| 19819 | Ok(()) |
| 19820 | } |
| 19821 | |
| 19822 | /// Decodes one 8x8 leaf of a straddling 16x16 inter-frame block |
| 19823 | /// (lane-av1inter8), mirroring [`crate::tile::write_inter_frame_leaf8`]'s |
| 19824 | /// exact symbol order: its own skip flag, intra/inter choice and (when |
| 19825 | /// inter) `NEARESTMV`/`NEWMV` chain against this leaf's own 2x2-mi mv-stack |
| 19826 | /// window, or (when intra) `Y_MODE`, then its own luma and two chroma |
| 19827 | /// transform blocks through [`TxbSet::Luma8Inter`]/[`TxbSet::Chroma4`], |
| 19828 | /// same as [`decode_leaf8`]'s coefficient layer but with |
| 19829 | /// motion-compensated prediction instead of intra prediction when inter. |
| 19830 | /// `outer_at` (in [`SUB`]-grid units) is the enclosing 16x16 slot's |
| 19831 | /// skip/intra-inter context, overridden by `prev_leaf` exactly as |
| 19832 | /// [`decode_leaf8`] overrides mode context: `Neighbours`' above/left arrays |
| 19833 | /// only resolve to [`SUB`] granularity. Hands back this leaf's own skip flag |
| 19834 | /// and intra/inter choice, for the next leaf or the caller's final |
| 19835 | /// write-back. |
| 19836 | #[allow(clippy::too_many_arguments)] |
| 19837 | fn decode_inter_block8( |
| 19838 | dec: &mut SymbolDecoder, |
| 19839 | cdfs: &mut Cdfs, |
| 19840 | neighbours: &mut Neighbours, |
| 19841 | grid: &mut MiGrid, |
| 19842 | mi_cols: u32, |
| 19843 | mi_rows: u32, |
| 19844 | outer_at: (usize, usize), |
| 19845 | leaf_mi: (usize, usize), |
| 19846 | y: &mut PlaneBuf, |
| 19847 | u: &mut PlaneBuf, |
| 19848 | v: &mut PlaneBuf, |
| 19849 | ref_y: &PlaneBuf, |
| 19850 | ref_u: &PlaneBuf, |
| 19851 | ref_v: &PlaneBuf, |
| 19852 | other_refs: &RefSlots, |
| 19853 | base_q_idx: u8, |
| 19854 | scan8: &[u16], |
| 19855 | scan4: &[u16], |
| 19856 | allow_high_precision_mv: bool, |
| 19857 | force_integer_mv: bool, |
| 19858 | // lane-gmaffine r1: this frame header's own `global_motion` table (spec |
| 19859 | // 5.9.24), indexed `[ref_frame - LAST_FRAME]` -- same table |
| 19860 | // [`decode_inter_block`] takes, threaded here so the 8x8 leaf can code |
| 19861 | // `GLOBALMV` (`gm_get_motion_vector`), fill its mv stack's missing |
| 19862 | // candidates with the gm mv (gm r6's root cause) and build a global |
| 19863 | // warp prediction. |
| 19864 | global_motion: &[ec_av1_syntax::WarpParams; 7], |
| 19865 | // lane-gmaffine r1: this sequence header's own `enable_dual_filter` bit, |
| 19866 | // for the leaf's own `read_mb_interp_filter` below. |
| 19867 | enable_dual_filter: bool, |
| 19868 | // lane-av1comp: see [`decode_inter_block`]'s own doc -- this leaf path |
| 19869 | // only ever tracks a coarse `LAST_FRAME`-or-intra neighbour shape (no |
| 19870 | // per-leaf `above_ref`/`left_ref` array exists here), so `comp_mode`'s |
| 19871 | // context uses that same approximation. |
| 19872 | reference_select: bool, |
| 19873 | enable_masked_compound: bool, |
| 19874 | // lane-sb128 r4: see [`decode_inter_block`]'s own doc -- `BLOCK_8X8` is |
| 19875 | // always `is_interintra_allowed_bsize`-eligible, so this leaf's own gate |
| 19876 | // drops the `side` check entirely. |
| 19877 | enable_interintra_compound: bool, |
| 19878 | enable_jnt_comp: bool, |
| 19879 | order_hint_bits: u32, |
| 19880 | order_hint: u32, |
| 19881 | ref_order_hints: [u32; 7], |
| 19882 | // lane-av1comp round 14: see [`decode_inter_block`]'s own doc -- this |
| 19883 | // leaf has no per-leaf `above_skip_mode`/`left_skip_mode` array either, |
| 19884 | // so its `skip_mode` context reuses the same [`SUB`]-grid |
| 19885 | // `above_skip_mode[cmi]`/`left_skip_mode[rmi]` the enclosing 16x16 slot |
| 19886 | // tracks (mirroring `above_skip`/`left_skip`'s own leaf approximation |
| 19887 | // just above). |
| 19888 | skip_mode_present: bool, |
| 19889 | skip_mode_frame: [u8; 2], |
| 19890 | // lane-cdffwd2: this frame header's own `reduced_tx_set` bit -- see |
| 19891 | // [`decode_inter_frame_tile_with_cdfs`]'s own doc. |
| 19892 | reduced_tx_set: bool, |
| 19893 | // lane-motionmode round 3: this frame header's own `is_motion_mode_switchable`/ |
| 19894 | // `allow_warped_motion` bits (spec 5.11.24's `read_motion_mode`), plus |
| 19895 | // the fixed/switchable interp filter kind `obmc_blend`'s own neighbour |
| 19896 | // reads need (see [`decode_inter_block`]'s own doc) -- this leaf's own |
| 19897 | // prediction is always `Regular` (documented corner-cut, `mc::predict` |
| 19898 | // has no filter param), but an OBMC neighbour can be a real 16x16+ |
| 19899 | // block that DID resolve a switchable filter. |
| 19900 | interp_fixed: Option<mc::InterpFilterKind>, |
| 19901 | switchable_motion_mode: bool, |
| 19902 | allow_warped_motion: bool, |
| 19903 | // lane-av1comp: `Some((ref1, comp_group_idx, compound_idx))` only for a |
| 19904 | // real `COMPOUND_REFERENCE` leaf, for the caller's own end-of-16x16 |
| 19905 | // `record_compound_ctx` call -- mirrors [`decode_inter_block`]'s own |
| 19906 | // `compound_ctx` local, just handed back instead of applied here (this |
| 19907 | // fn's `neighbours` recording happens once for the whole straddling |
| 19908 | // 16x16 block, after every leaf). |
| 19909 | // lane-screen r2: see [`decode_inter_block`]'s own copy of this param -- |
| 19910 | // `BLOCK_8X8` (this leaf's fixed size) is always `av1_allow_palette`- |
| 19911 | // eligible (`bsize >= BLOCK_8X8`). |
| 19912 | allow_screen_content_tools: bool, |
| 19913 | // lane-inter8 r1: the leaf's mv stacks used to be built without the |
| 19914 | // frame's sign-bias table (`NO_SIGN_BIAS`), while [`decode_inter_block`] |
| 19915 | // one level up passed it -- it changes the candidate list and with it |
| 19916 | // `new_mv_ctx`/`ref_mv_ctx`, `drl_ctx` and the number of symbols read. |
| 19917 | // Measured: an 8x8 compound NEAR_NEWMV leaf desynced against aomdec's |
| 19918 | // EC_MODE ladder at exactly this block (mi 4,14 of the mandelbrot |
| 19919 | // 8x8-split gate). |
| 19920 | sign_bias_table: &SignBiasTable, |
| 19921 | // lane-inter8 r2: the frame's temporal mv field (spec 7.10.2.8), which |
| 19922 | // [`decode_inter_block`] one level up has always passed and this leaf |
| 19923 | // passed `None` for -- a `use_ref_frame_mvs` frame's stack then holds |
| 19924 | // fewer candidates here than in the reference decoder, which changes the |
| 19925 | // mode contexts AND the number of `drl_mode` bits read (class parsed |
| 19926 | // then discarded). |
| 19927 | tpl_frame: Option<&TplFrameArgs>, |
| 19928 | // lane-scaledref r1: this frame's own coded luma width (spec 7.11.3.3), |
| 19929 | // for the scaled-reference MC this leaf's own single-ref/compound/OBMC |
| 19930 | // predictions take when a stored reference was coded at another width |
| 19931 | // (`use_superres`) -- mirrors [`decode_inter_block`]'s own param. |
| 19932 | frame_width: usize, |
| 19933 | ) -> Result<(bool, bool, bool, Option<(i8, i8, u8, u8)>, [u8; 2], (i8, Option<i8>))> { |
| 19934 | const LAST_FRAME: i8 = 1; |
| 19935 | // lane-gmaffine r2: the leaf's OWN switchable-filter symbols, handed back |
| 19936 | // so the caller stamps them into `Neighbours` instead of the `[3, 3]` |
| 19937 | // placeholder. `[3, 3]` is the "intra neighbour, no filter" sentinel: an |
| 19938 | // OBMC blend that picks such a neighbour up feeds it to |
| 19939 | // `neighbour_filter`, which PANICS on it (`from_switchable_symbol`) -- |
| 19940 | // that was the warp gate's crash, not an entropy desync. Stays `[3, 3]` |
| 19941 | // on the intra and compound paths. |
| 19942 | let mut leaf_filter_syms = [3u8; 2]; |
| 19943 | // lane-gmaffine r1: this leaf's GLOBALMV vector AND the mv stack's |
| 19944 | // missing-candidate fallback both come from the frame's global motion |
| 19945 | // evaluated at THIS block's centre (gm r6's root cause). |
| 19946 | let gm_table = build_gm_mv_table( |
| 19947 | global_motion, |
| 19948 | leaf_mi.0, |
| 19949 | leaf_mi.1, |
| 19950 | 2, |
| 19951 | 2, |
| 19952 | allow_high_precision_mv, |
| 19953 | force_integer_mv, |
| 19954 | ); |
| 19955 | /// `Y_MODE`'s size group (`common_data.h`'s `size_group_lookup[BLOCK_8X8]`). |
| 19956 | const SIZE_GROUP_8: usize = 1; |
| 19957 | |
| 19958 | let (px, py) = (leaf_mi.1 * MI, leaf_mi.0 * MI); |
| 19959 | let (cpx, cpy) = (px / 2, py / 2); |
| 19960 | // lane-txselect: whether this leaf's `TxMode::Select` var-tx tree took |
| 19961 | // the one split a `BLOCK_8X8` can take (`read_block_tx_size` below) -- |
| 19962 | // read back by the luma residual read and the tail's neighbour/loop-filter |
| 19963 | // bookkeeping. Always `false` outside a `TxMode::Select` inter frame. |
| 19964 | let mut split8 = false; |
| 19965 | const SIDE: usize = 8; |
| 19966 | const CHROMA_SIDE: usize = 4; |
| 19967 | |
| 19968 | let (r, c) = outer_at; |
| 19969 | let _ = (r, c); |
| 19970 | // lane-inter8 r2: this leaf's OWN mi cell, not the enclosing 16x16's -- |
| 19971 | // leaf (mi_r, mi_c + 2)'s left neighbour is leaf (mi_r, mi_c), and the |
| 19972 | // left column of a 16x16's second leaf ROW is the previous 16x16 block's |
| 19973 | // bottom leaf, neither of which a SUB-grid slot can name. |
| 19974 | let (rmi, cmi) = leaf_mi; |
| 19975 | // lane-inter8 r2: no sibling-leaf override any more -- each leaf stamps |
| 19976 | // its own 2x2-mi span into the bands right after it decodes (the caller's |
| 19977 | // `record_inter_rect_mi`), so a plain mi read already names the correct |
| 19978 | // neighbour, including one belonging to the previous 16x16 block. |
| 19979 | let above_skip = neighbours.above_skip[cmi]; |
| 19980 | let left_skip = neighbours.left_skip[rmi]; |
| 19981 | let above_inter = neighbours.above_inter[cmi]; |
| 19982 | let left_inter = neighbours.left_inter[rmi]; |
| 19983 | let above_ref0 = neighbours.above_ref[cmi]; |
| 19984 | let above_ref1 = neighbours.above_ref1[cmi]; |
| 19985 | let left_ref0 = neighbours.left_ref[rmi]; |
| 19986 | let left_ref1 = neighbours.left_ref1[rmi]; |
| 19987 | let skip_mode_ctx = |
| 19988 | usize::from(neighbours.above_skip_mode[cmi]) + usize::from(neighbours.left_skip_mode[rmi]); |
| 19989 | // Same two `inter_segment_id` positions as [`decode_inter_block`]; this |
| 19990 | // leaf is always `BLOCK_8X8`, i.e. 2x2 mi. |
| 19991 | inter_segment_id(dec, cdfs, leaf_mi.0, leaf_mi.1, 2, 2, false, true); |
| 19992 | let skip_mode = skip_mode_present && dec.symbol(&mut cdfs.skip_mode[skip_mode_ctx]) == 1; |
| 19993 | if skip_mode { |
| 19994 | SKIP_MODE_HITS.with(|c| c.set(c.get() + 1)); |
| 19995 | } |
| 19996 | |
| 19997 | let skip_ctx = usize::from(above_skip) + usize::from(left_skip); |
| 19998 | let skip = skip_mode || dec.symbol(&mut cdfs.skip[skip_ctx]) == 1; |
| 19999 | inter_segment_id(dec, cdfs, leaf_mi.0, leaf_mi.1, 2, 2, skip, false); |
| 20000 | maybe_read_cdef_idx(dec, leaf_mi.0, leaf_mi.1, skip); |
| 20001 | // Always a `BLOCK_8X8` leaf here, never the whole superblock. |
| 20002 | maybe_read_delta_q(dec, cdfs, leaf_mi.0, leaf_mi.1, false, skip); |
| 20003 | maybe_read_delta_lf(dec, cdfs, leaf_mi.0, leaf_mi.1, false, skip); |
| 20004 | |
| 20005 | let (has_above, has_left) = ( |
| 20006 | leaf_mi.0 > neighbours.tile_row0_mi, |
| 20007 | leaf_mi.1 > neighbours.tile_col0_mi, |
| 20008 | ); |
| 20009 | let ii_ctx = intra_inter_ctx(has_above, has_left, above_inter, left_inter); |
| 20010 | let is_inter = skip_mode || dec.symbol(&mut cdfs.intra_inter[ii_ctx]) == 1; |
| 20011 | // lane-inter8 r1: mirrors the oracle's `EC_MODE` (instrument rung 4, |
| 20012 | // `read_inter_block_mode_info`) so an 8x8 leaf's mode-info range ladder |
| 20013 | // diffs line for line against a real aomdec. |
| 20014 | if std::env::var_os("EC_TRACE_MODE").is_some() { |
| 20015 | eprintln!( |
| 20016 | "EC_MODE mi_row={} mi_col={} rng={}", |
| 20017 | leaf_mi.0, |
| 20018 | leaf_mi.1, |
| 20019 | dec.debug_state().0 |
| 20020 | ); |
| 20021 | } |
| 20022 | |
| 20023 | let mode_for_tx; |
| 20024 | let (luma_grid, u_grid, v_grid); |
| 20025 | let mut compound_ctx8: Option<(i8, i8, u8, u8)> = None; |
| 20026 | // lane-inter8 r1: this leaf's own resolved references, handed back so the |
| 20027 | // NEXT leaf of the same 16x16 can use them as its real above/left |
| 20028 | // neighbour (`Neighbours`' arrays only move once per 16x16 block). |
| 20029 | // `(0, None)` is intra. |
| 20030 | let mut leaf_refs: (i8, Option<i8>) = (0, None); |
| 20031 | if is_inter { |
| 20032 | if reference_select || skip_mode { |
| 20033 | // lane-inter8 r1: the REAL neighbour references, not a |
| 20034 | // `LAST_FRAME`-or-intra stand-in. `av1_get_reference_mode_context` |
| 20035 | // and `read_compound_ref_frames`' own contexts both ask whether a |
| 20036 | // neighbour is COMPOUND, so pretending every inter neighbour is |
| 20037 | // single-ref LAST picked the wrong `comp_mode` CDF row -- measured |
| 20038 | // as an 8x8 leaf reading single-ref where aomdec read compound |
| 20039 | // NEAR_NEWMV (mi 4,14). |
| 20040 | let above_nbr = has_above.then(|| NeighbourRef { |
| 20041 | is_inter: above_inter, |
| 20042 | ref0: if above_inter { above_ref0 } else { 0 }, |
| 20043 | ref1: above_ref1, |
| 20044 | uni: above_ref1.is_some_and(|r1| is_uni_comp_ref(above_ref0, r1)), |
| 20045 | }); |
| 20046 | let left_nbr = has_left.then(|| NeighbourRef { |
| 20047 | is_inter: left_inter, |
| 20048 | ref0: if left_inter { left_ref0 } else { 0 }, |
| 20049 | ref1: left_ref1, |
| 20050 | uni: left_ref1.is_some_and(|r1| is_uni_comp_ref(left_ref0, r1)), |
| 20051 | }); |
| 20052 | if skip_mode || read_comp_mode(dec, cdfs, above_nbr, left_nbr) { |
| 20053 | let (ref0, ref1) = if skip_mode { |
| 20054 | (skip_mode_frame[0] as i8, skip_mode_frame[1] as i8) |
| 20055 | } else { |
| 20056 | read_compound_ref_frames( |
| 20057 | dec, |
| 20058 | cdfs, |
| 20059 | above_nbr, |
| 20060 | left_nbr, |
| 20061 | if has_above && above_inter { |
| 20062 | LAST_FRAME |
| 20063 | } else { |
| 20064 | -1 |
| 20065 | }, |
| 20066 | if has_left && left_inter { |
| 20067 | LAST_FRAME |
| 20068 | } else { |
| 20069 | -1 |
| 20070 | }, |
| 20071 | ) |
| 20072 | }; |
| 20073 | leaf_refs = (ref0, Some(ref1)); |
| 20074 | let (mi_row, mi_col) = leaf_mi; |
| 20075 | // spec 7.10.2.8, mirroring [`decode_inter_block`]'s own |
| 20076 | // `comp_tpl` build (`use_ref_frame_mvs` frames only). |
| 20077 | let comp_tpl = tpl_frame.map(|t| crate::mvstack::CompoundTplArgs { |
| 20078 | field: t.field, |
| 20079 | cur_offset_0: crate::motion_field::get_relative_dist( |
| 20080 | t.order_hint_bits, |
| 20081 | t.order_hint, |
| 20082 | t.ref_order_hints[(ref0 - LAST_FRAME) as usize], |
| 20083 | ), |
| 20084 | cur_offset_1: crate::motion_field::get_relative_dist( |
| 20085 | t.order_hint_bits, |
| 20086 | t.order_hint, |
| 20087 | t.ref_order_hints[(ref1 - LAST_FRAME) as usize], |
| 20088 | ), |
| 20089 | allow_high_precision_mv, |
| 20090 | }); |
| 20091 | // lane-av1comp: this leaf path has neither a real sign-bias |
| 20092 | // table nor `tpl_frame` (see this function's own doc on the |
| 20093 | // coarse `LAST_FRAME`-or-intra neighbour approximation |
| 20094 | // already in force above), matching the plain `find_mv_stack` |
| 20095 | // (no bias, no tpl) the leaf's single-ref arm below already |
| 20096 | // uses. |
| 20097 | let comp_stack = crate::mvstack::find_mv_stack_compound( |
| 20098 | grid, |
| 20099 | mi_row, |
| 20100 | mi_col, |
| 20101 | 2, |
| 20102 | 2, |
| 20103 | (ref0, ref1), |
| 20104 | mi_cols as usize, |
| 20105 | mi_rows as usize, |
| 20106 | sign_bias_table, |
| 20107 | &gm_table, |
| 20108 | comp_tpl, |
| 20109 | ); |
| 20110 | let compound_mode = if skip_mode { |
| 20111 | 0 // NEAREST_NEARESTMV, forced -- no mode symbol read |
| 20112 | } else { |
| 20113 | read_inter_compound_mode( |
| 20114 | dec, |
| 20115 | cdfs, |
| 20116 | comp_stack.new_mv_ctx, |
| 20117 | comp_stack.ref_mv_ctx, |
| 20118 | ) |
| 20119 | }; |
| 20120 | if std::env::var_os("EC_TRACE_MODE").is_some() { |
| 20121 | eprintln!( |
| 20122 | "EC_LEAFMODE mi_row={} mi_col={} cmode={} stack={} newmv_ctx={} refmv_ctx={} rng={}", |
| 20123 | leaf_mi.0, |
| 20124 | leaf_mi.1, |
| 20125 | compound_mode + 17, |
| 20126 | comp_stack.entries.len(), |
| 20127 | comp_stack.new_mv_ctx, |
| 20128 | comp_stack.ref_mv_ctx, |
| 20129 | dec.debug_state().0 |
| 20130 | ); |
| 20131 | } |
| 20132 | let (mv0, mv1) = assign_compound_mv( |
| 20133 | dec, |
| 20134 | cdfs, |
| 20135 | &comp_stack, |
| 20136 | compound_mode, |
| 20137 | allow_high_precision_mv, |
| 20138 | force_integer_mv, |
| 20139 | // lane-inter8 r1: the real per-ref gm mvs, same as |
| 20140 | // [`decode_inter_block`]'s own compound arm. |
| 20141 | ( |
| 20142 | gm_table[(ref0 - LAST_FRAME) as usize], |
| 20143 | gm_table[(ref1 - LAST_FRAME) as usize], |
| 20144 | ), |
| 20145 | ); |
| 20146 | // lane-inter8 r3: byte-format-identical to the oracle's |
| 20147 | // rung-4 `EC_MODE_VAL`, so the leaf's mode+mv VALUE ladder |
| 20148 | // diffs line for line against a real aomdec. |
| 20149 | if std::env::var_os("EC_TRACE_MODE").is_some() { |
| 20150 | eprintln!( |
| 20151 | "EC_MODE_VAL mi_row={} mi_col={} mode={} ref0={} ref1={} mv0=({},{}) rng={}", |
| 20152 | leaf_mi.0, |
| 20153 | leaf_mi.1, |
| 20154 | compound_mode + 17, |
| 20155 | ref0, |
| 20156 | ref1, |
| 20157 | mv0.0, |
| 20158 | mv0.1, |
| 20159 | dec.debug_state().0 |
| 20160 | ); |
| 20161 | } |
| 20162 | |
| 20163 | // spec 5.11.25: same gating [`decode_inter_block`]'s own |
| 20164 | // compound arm uses, keyed off `outer_at`'s [`SUB`]-grid |
| 20165 | // slot -- the only granularity `Neighbours`' comp_group_idx/ |
| 20166 | // compound_idx arrays track. |
| 20167 | let group_ctx = get_comp_group_idx_context(neighbours, leaf_mi, SIDE); |
| 20168 | let comp_group_idx = if !skip_mode |
| 20169 | && enable_masked_compound |
| 20170 | && is_any_masked_compound_used_here(SIDE) |
| 20171 | { |
| 20172 | dec.symbol(&mut cdfs.comp_group_idx[group_ctx]) |
| 20173 | } else { |
| 20174 | 0 |
| 20175 | }; |
| 20176 | // lane-maskcomp r2 / lane-wedge r3: see the 16x16/32x32 |
| 20177 | // leaf's own comment. |
| 20178 | let mut diffwtd_mask_type: Option<u8> = None; |
| 20179 | let mut wedge_mask: Option<&'static [u8]> = None; |
| 20180 | if comp_group_idx == 1 { |
| 20181 | // lane-maskcomp r1: see the 16x16 leaf's own comment |
| 20182 | // above -- SIDE==8 here, `wedge_bsize` index 3 |
| 20183 | // (BLOCK_8X8, `wedge_types > 0`). |
| 20184 | let compound_type = dec.symbol(&mut cdfs.compound_type[3]); |
| 20185 | if compound_type == 0 { |
| 20186 | // COMPOUND_WEDGE: lane-wedge r3, see the 16x16/32x32 |
| 20187 | // leaf's own comment. |
| 20188 | let wedge_index = dec.symbol(&mut cdfs.wedge_idx[3]); |
| 20189 | let wedge_sign = dec.literal(1); |
| 20190 | WEDGE_HITS.with(|c| c.set(c.get() + 1)); |
| 20191 | wedge_mask = Some( |
| 20192 | crate::wedge::wedge_masks() |
| 20193 | .codebook(SIDE) |
| 20194 | .mask(wedge_sign as usize, wedge_index as usize), |
| 20195 | ); |
| 20196 | } else { |
| 20197 | let mask_type = dec.literal(1); |
| 20198 | diffwtd_mask_type = Some(mask_type as u8); |
| 20199 | } |
| 20200 | MASKED_COMPOUND_HITS.with(|c| c.set(c.get() + 1)); |
| 20201 | } |
| 20202 | // corner-cut (lane-av1comp r16/r17, lane-av1blend r1): same |
| 20203 | // reference-slot defect as the 16x16 leaf's own mask above |
| 20204 | // (r1: falsified as an MC/blend math bug, isolated to a DPB |
| 20205 | // slot/refresh_frame_flags issue around BWDREF_FRAME) -- see |
| 20206 | // that comment. |
| 20207 | // r3: same defect as the 16x16 leaf's own mask above -- see |
| 20208 | // that comment (compound_idx's decoded value, not the blend |
| 20209 | // math or reference-slot bookkeeping). r6/r7: same re-mask |
| 20210 | // as the 16x16 leaf above -- one real bug found and fixed |
| 20211 | // (mvstack.rs ref_ctx family), a second, still-unfound |
| 20212 | // compound_idx defect surfaces on the wide gate sweep. |
| 20213 | // lane-av1idx r1: same re-mask as the 16x16 leaf above -- |
| 20214 | // see that comment. |
| 20215 | let (fwd_offset, bck_offset, compound_idx) = if comp_group_idx == 0 |
| 20216 | && !skip_mode |
| 20217 | && enable_jnt_comp |
| 20218 | { |
| 20219 | let idx_ctx = get_comp_index_context( |
| 20220 | neighbours, |
| 20221 | leaf_mi, |
| 20222 | SIDE, |
| 20223 | order_hint_bits, |
| 20224 | order_hint, |
| 20225 | ref_order_hints[(ref0 - LAST_FRAME) as usize], |
| 20226 | ref_order_hints[(ref1 - LAST_FRAME) as usize], |
| 20227 | ); |
| 20228 | let idx = dec.symbol(&mut cdfs.compound_idx[idx_ctx]); |
| 20229 | if idx == 1 { |
| 20230 | (8, 8, 1u8) |
| 20231 | } else { |
| 20232 | let (fwd, bck) = crate::compound::dist_wtd_comp_weight_assign( |
| 20233 | order_hint_bits, |
| 20234 | order_hint, |
| 20235 | ref_order_hints[(ref0 - LAST_FRAME) as usize], |
| 20236 | ref_order_hints[(ref1 - LAST_FRAME) as usize], |
| 20237 | ); |
| 20238 | (fwd, bck, 0u8) |
| 20239 | } |
| 20240 | } else { |
| 20241 | (8, 8, 1u8) |
| 20242 | }; |
| 20243 | compound_ctx8 = Some((ref0, ref1, comp_group_idx as u8, compound_idx)); |
| 20244 | if std::env::var_os("EC_AV1_COMPIDX_DUMP").is_some() { |
| 20245 | eprintln!( |
| 20246 | "EC_COMPIDX mi_row={mi_row} mi_col={mi_col} bsize=8 mode={compound_mode} mv0=({},{}) mv1=({},{}) ref0={ref0} ref1={ref1} comp_group_idx={comp_group_idx} compound_idx={compound_idx} tell={}", |
| 20247 | mv0.0, mv0.1, mv1.0, mv1.1, dec.debug_bitpos() |
| 20248 | ); |
| 20249 | } |
| 20250 | |
| 20251 | let (py0, pu0, pv0) = ref_planes(ref0, ref_y, ref_u, ref_v, other_refs)?; |
| 20252 | let (py1, pu1, pv1) = ref_planes(ref1, ref_y, ref_u, ref_v, other_refs)?; |
| 20253 | let scale0 = mc::scale_factor(py0.width, frame_width); |
| 20254 | let scale1 = mc::scale_factor(py1.width, frame_width); |
| 20255 | if scale0 != mc::REF_NO_SCALE || scale1 != mc::REF_NO_SCALE { |
| 20256 | SCALED_COMPOUND_HITS.with(|c| c.set(c.get() + 1)); |
| 20257 | SCALED_BLOCK8_HITS.with(|c| c.set(c.get() + 1)); |
| 20258 | } |
| 20259 | |
| 20260 | for dr in 0..2 { |
| 20261 | for dc in 0..2 { |
| 20262 | grid.set( |
| 20263 | mi_row + dr, |
| 20264 | mi_col + dc, |
| 20265 | MiInfo { |
| 20266 | is_inter: true, |
| 20267 | ref_frame: ref0, |
| 20268 | ref_frame1: Some(ref1), |
| 20269 | mv1: Some(mv1), |
| 20270 | mv: mv0, |
| 20271 | is_new_mv: matches!(compound_mode, 2 | 3 | 4 | 5 | 7), |
| 20272 | size: 2, |
| 20273 | size_h: 2, |
| 20274 | is_global_mv0: false, |
| 20275 | is_global_mv1: false, |
| 20276 | }, |
| 20277 | ); |
| 20278 | } |
| 20279 | } |
| 20280 | mode_for_tx = 0; |
| 20281 | |
| 20282 | // lane-av1comp corner-cut, matching this leaf's single-ref |
| 20283 | // arm below (`mc::predict`'s own fixed-Regular default): |
| 20284 | // `decode_inter_block8` never resolves a switchable filter, |
| 20285 | // so both compound taps use `Regular` too. |
| 20286 | use mc::InterpFilterKind::Regular; |
| 20287 | let mut inter0_y = vec![0i32; SIDE * SIDE]; |
| 20288 | mc::predict_compound_intermediate( |
| 20289 | &py0.data, |
| 20290 | py0.width, |
| 20291 | py0.true_width, |
| 20292 | py0.true_height, |
| 20293 | mv_to_q4(px, mv0.1, true), |
| 20294 | mv_to_q4(py, mv0.0, true), |
| 20295 | scale0, |
| 20296 | SIDE, |
| 20297 | SIDE, |
| 20298 | Regular, |
| 20299 | Regular, |
| 20300 | &mut inter0_y, |
| 20301 | ); |
| 20302 | let mut inter1_y = vec![0i32; SIDE * SIDE]; |
| 20303 | mc::predict_compound_intermediate( |
| 20304 | &py1.data, |
| 20305 | py1.width, |
| 20306 | py1.true_width, |
| 20307 | py1.true_height, |
| 20308 | mv_to_q4(px, mv1.1, true), |
| 20309 | mv_to_q4(py, mv1.0, true), |
| 20310 | scale1, |
| 20311 | SIDE, |
| 20312 | SIDE, |
| 20313 | Regular, |
| 20314 | Regular, |
| 20315 | &mut inter1_y, |
| 20316 | ); |
| 20317 | let mut pred_y = vec![0u16; SIDE * SIDE]; |
| 20318 | let mut diffwtd_mask_y = Vec::new(); |
| 20319 | let mask_y: Option<&[u8]> = if let Some(mask_type) = diffwtd_mask_type { |
| 20320 | diffwtd_mask_y = vec![0u8; SIDE * SIDE]; |
| 20321 | DIFFWTD_HITS.with(|c| c.set(c.get() + 1)); |
| 20322 | mc::diffwtd_mask(&inter0_y, &inter1_y, mask_type == 1, &mut diffwtd_mask_y); |
| 20323 | Some(diffwtd_mask_y.as_slice()) |
| 20324 | } else { |
| 20325 | wedge_mask |
| 20326 | }; |
| 20327 | if let Some(mask_y) = mask_y { |
| 20328 | mc::blend_masked_compound( |
| 20329 | &inter0_y, &inter1_y, mask_y, SIDE, SIDE, SIDE, false, &mut pred_y, |
| 20330 | ); |
| 20331 | } else { |
| 20332 | mc::combine_compound(&inter0_y, &inter1_y, fwd_offset, bck_offset, &mut pred_y); |
| 20333 | } |
| 20334 | |
| 20335 | let mut inter0_u = vec![0i32; CHROMA_SIDE * CHROMA_SIDE]; |
| 20336 | mc::predict_compound_intermediate( |
| 20337 | &pu0.data, |
| 20338 | pu0.width, |
| 20339 | pu0.true_width, |
| 20340 | pu0.true_height, |
| 20341 | mv_to_q4(cpx, mv0.1, false), |
| 20342 | mv_to_q4(cpy, mv0.0, false), |
| 20343 | scale0, |
| 20344 | CHROMA_SIDE, |
| 20345 | CHROMA_SIDE, |
| 20346 | Regular, |
| 20347 | Regular, |
| 20348 | &mut inter0_u, |
| 20349 | ); |
| 20350 | let mut inter1_u = vec![0i32; CHROMA_SIDE * CHROMA_SIDE]; |
| 20351 | mc::predict_compound_intermediate( |
| 20352 | &pu1.data, |
| 20353 | pu1.width, |
| 20354 | pu1.true_width, |
| 20355 | pu1.true_height, |
| 20356 | mv_to_q4(cpx, mv1.1, false), |
| 20357 | mv_to_q4(cpy, mv1.0, false), |
| 20358 | scale1, |
| 20359 | CHROMA_SIDE, |
| 20360 | CHROMA_SIDE, |
| 20361 | Regular, |
| 20362 | Regular, |
| 20363 | &mut inter1_u, |
| 20364 | ); |
| 20365 | let mut pred_u = vec![0u16; CHROMA_SIDE * CHROMA_SIDE]; |
| 20366 | if let Some(mask_y) = mask_y { |
| 20367 | mc::blend_masked_compound( |
| 20368 | &inter0_u, |
| 20369 | &inter1_u, |
| 20370 | mask_y, |
| 20371 | SIDE, |
| 20372 | CHROMA_SIDE, |
| 20373 | CHROMA_SIDE, |
| 20374 | true, |
| 20375 | &mut pred_u, |
| 20376 | ); |
| 20377 | } else { |
| 20378 | mc::combine_compound(&inter0_u, &inter1_u, fwd_offset, bck_offset, &mut pred_u); |
| 20379 | } |
| 20380 | |
| 20381 | let mut inter0_v = vec![0i32; CHROMA_SIDE * CHROMA_SIDE]; |
| 20382 | mc::predict_compound_intermediate( |
| 20383 | &pv0.data, |
| 20384 | pv0.width, |
| 20385 | pv0.true_width, |
| 20386 | pv0.true_height, |
| 20387 | mv_to_q4(cpx, mv0.1, false), |
| 20388 | mv_to_q4(cpy, mv0.0, false), |
| 20389 | scale0, |
| 20390 | CHROMA_SIDE, |
| 20391 | CHROMA_SIDE, |
| 20392 | Regular, |
| 20393 | Regular, |
| 20394 | &mut inter0_v, |
| 20395 | ); |
| 20396 | let mut inter1_v = vec![0i32; CHROMA_SIDE * CHROMA_SIDE]; |
| 20397 | mc::predict_compound_intermediate( |
| 20398 | &pv1.data, |
| 20399 | pv1.width, |
| 20400 | pv1.true_width, |
| 20401 | pv1.true_height, |
| 20402 | mv_to_q4(cpx, mv1.1, false), |
| 20403 | mv_to_q4(cpy, mv1.0, false), |
| 20404 | scale1, |
| 20405 | CHROMA_SIDE, |
| 20406 | CHROMA_SIDE, |
| 20407 | Regular, |
| 20408 | Regular, |
| 20409 | &mut inter1_v, |
| 20410 | ); |
| 20411 | let mut pred_v = vec![0u16; CHROMA_SIDE * CHROMA_SIDE]; |
| 20412 | if let Some(mask_y) = mask_y { |
| 20413 | mc::blend_masked_compound( |
| 20414 | &inter0_v, |
| 20415 | &inter1_v, |
| 20416 | mask_y, |
| 20417 | SIDE, |
| 20418 | CHROMA_SIDE, |
| 20419 | CHROMA_SIDE, |
| 20420 | true, |
| 20421 | &mut pred_v, |
| 20422 | ); |
| 20423 | } else { |
| 20424 | mc::combine_compound(&inter0_v, &inter1_v, fwd_offset, bck_offset, &mut pred_v); |
| 20425 | } |
| 20426 | |
| 20427 | split8 = read_block_tx_size( |
| 20428 | dec, |
| 20429 | cdfs, |
| 20430 | neighbours, |
| 20431 | leaf_mi, |
| 20432 | SIDE, |
| 20433 | (mi_cols as usize, mi_rows as usize), |
| 20434 | true, |
| 20435 | skip, |
| 20436 | )? |
| 20437 | .1 |
| 20438 | .is_some(); |
| 20439 | if skip { |
| 20440 | y.reconstruct_mc(px, py, SIDE, &pred_y, &vec![0i32; SIDE * SIDE]); |
| 20441 | u.reconstruct_mc( |
| 20442 | cpx, |
| 20443 | cpy, |
| 20444 | CHROMA_SIDE, |
| 20445 | &pred_u, |
| 20446 | &vec![0i32; CHROMA_SIDE * CHROMA_SIDE], |
| 20447 | ); |
| 20448 | v.reconstruct_mc( |
| 20449 | cpx, |
| 20450 | cpy, |
| 20451 | CHROMA_SIDE, |
| 20452 | &pred_v, |
| 20453 | &vec![0i32; CHROMA_SIDE * CHROMA_SIDE], |
| 20454 | ); |
| 20455 | luma_grid = vec![0i32; SIDE * SIDE]; |
| 20456 | u_grid = vec![0i32; CHROMA_SIDE * CHROMA_SIDE]; |
| 20457 | v_grid = vec![0i32; CHROMA_SIDE * CHROMA_SIDE]; |
| 20458 | } else { |
| 20459 | let around = neighbours.around_mi(leaf_mi, 8); |
| 20460 | let (grid8, luma_tx_type) = read_inter_luma8( |
| 20461 | dec, |
| 20462 | cdfs, |
| 20463 | neighbours, |
| 20464 | leaf_mi, |
| 20465 | y, |
| 20466 | px, |
| 20467 | py, |
| 20468 | &pred_y, |
| 20469 | mode_for_tx, |
| 20470 | base_q_idx, |
| 20471 | scan8, |
| 20472 | scan4, |
| 20473 | reduced_tx_set, |
| 20474 | split8, |
| 20475 | )?; |
| 20476 | luma_grid = grid8; |
| 20477 | u_grid = read_inter_plane( |
| 20478 | dec, |
| 20479 | cdfs, |
| 20480 | TxbSet::Chroma4, |
| 20481 | scan4, |
| 20482 | 1, |
| 20483 | around[1], |
| 20484 | mode_for_tx, |
| 20485 | u, |
| 20486 | cpx, |
| 20487 | cpy, |
| 20488 | CHROMA_SIDE, |
| 20489 | base_q_idx, |
| 20490 | &pred_u, |
| 20491 | Some(luma_tx_type), |
| 20492 | None, |
| 20493 | )? |
| 20494 | .0; |
| 20495 | v_grid = read_inter_plane( |
| 20496 | dec, |
| 20497 | cdfs, |
| 20498 | TxbSet::Chroma4, |
| 20499 | scan4, |
| 20500 | 2, |
| 20501 | around[2], |
| 20502 | mode_for_tx, |
| 20503 | v, |
| 20504 | cpx, |
| 20505 | cpy, |
| 20506 | CHROMA_SIDE, |
| 20507 | base_q_idx, |
| 20508 | &pred_v, |
| 20509 | Some(luma_tx_type), |
| 20510 | None, |
| 20511 | )? |
| 20512 | .0; |
| 20513 | } |
| 20514 | // lane-inter8 r2: the COMPOUND leaf used to return here |
| 20515 | // WITHOUT the end-of-function `record_mi`/`fill_lf_grid` the |
| 20516 | // single-ref path falls through to -- so a compound 8x8 leaf |
| 20517 | // left `above_side_mi`/`left_side_mi` (the next block's |
| 20518 | // partition context), the per-plane coefficient level state |
| 20519 | // and the loop-filter grid describing whatever block was |
| 20520 | // there before. Every leaf of the gate's stream is compound, |
| 20521 | // so the 16x16 below a split block read partition ctx 0 |
| 20522 | // instead of 1 and mis-read PARTITION_SPLIT as NONE. |
| 20523 | neighbours.record_mi(leaf_mi, 8, &[luma_grid, u_grid, v_grid]); |
| 20524 | // lane-cdef r1: ... and the CDEF skip band too. `apply_cdef`'s |
| 20525 | // `is_8x8_block_skip` (libaom `cdef.c:29-38`) reads all four mi |
| 20526 | // cells of the 8x8; this leaf wrote none of them, so a 64x64 |
| 20527 | // whose bottom-right 16x16 is an all-skip 8x8-leaf group kept |
| 20528 | // the PREVIOUS frame's flags and got filtered where libaom's |
| 20529 | // dlist excludes it (3 luma samples on a 192x128 inter frame). |
| 20530 | INTER8_SKIP_BAND_HITS.with(|c| c.set(c.get() + 1)); |
| 20531 | neighbours.fill_skip_grid(leaf_mi, 2, skip); |
| 20532 | neighbours.fill_lf_grid( |
| 20533 | leaf_mi, |
| 20534 | 2, |
| 20535 | 8, |
| 20536 | if is_inter { leaf_refs.0.max(LAST_FRAME) } else { 0 }, |
| 20537 | ); |
| 20538 | return Ok(( |
| 20539 | skip, |
| 20540 | is_inter, |
| 20541 | skip_mode, |
| 20542 | compound_ctx8, |
| 20543 | leaf_filter_syms, |
| 20544 | leaf_refs, |
| 20545 | )); |
| 20546 | } |
| 20547 | } |
| 20548 | // lane-gmaffine r1: the leaf reads the FULL `single_ref` tree |
| 20549 | // ([`read_single_ref`], the same one the 16x16+ leaf uses, with the |
| 20550 | // real per-neighbour p1..p6 contexts) instead of the old three-symbol |
| 20551 | // LAST-only probe plus a refusal -- aomenc picks GOLDEN/ALTREF for |
| 20552 | // 8x8 leaves constantly, so that refusal was what every 8x8 gate |
| 20553 | // actually stopped at. |
| 20554 | let ref_frame = read_single_ref( |
| 20555 | dec, |
| 20556 | cdfs, |
| 20557 | neighbours.above_ref[cmi], |
| 20558 | neighbours.above_ref1[cmi], |
| 20559 | neighbours.left_ref[rmi], |
| 20560 | neighbours.left_ref1[rmi], |
| 20561 | ); |
| 20562 | leaf_refs = (ref_frame, None); |
| 20563 | let (sref_y, sref_u, sref_v) = ref_planes(ref_frame, ref_y, ref_u, ref_v, other_refs)?; |
| 20564 | |
| 20565 | let (mi_row, mi_col) = leaf_mi; |
| 20566 | // lane-inter8 r2: the frame's temporal mv candidates, keyed on THIS |
| 20567 | // leaf's own reference (lane-gmaffine r1 taught it non-LAST refs). |
| 20568 | let tpl = tpl_frame.map(|t| crate::mvstack::TplArgs { |
| 20569 | field: t.field, |
| 20570 | cur_offset_0: crate::motion_field::get_relative_dist( |
| 20571 | t.order_hint_bits, |
| 20572 | t.order_hint, |
| 20573 | t.ref_order_hints[(ref_frame - LAST_FRAME) as usize], |
| 20574 | ), |
| 20575 | allow_high_precision_mv, |
| 20576 | }); |
| 20577 | let stack = crate::mvstack::find_mv_stack_with_sign_bias( |
| 20578 | grid, |
| 20579 | mi_row, |
| 20580 | mi_col, |
| 20581 | 2, |
| 20582 | 2, |
| 20583 | ref_frame, |
| 20584 | mi_cols as usize, |
| 20585 | mi_rows as usize, |
| 20586 | sign_bias_table, |
| 20587 | &gm_table, |
| 20588 | tpl, |
| 20589 | ); |
| 20590 | |
| 20591 | let not_new = dec.symbol(&mut cdfs.new_mv[stack.new_mv_ctx]) == 1; |
| 20592 | let mut is_globalmv = false; |
| 20593 | let (mv, is_new_mv) = if !not_new { |
| 20594 | // NEWMV (spec 5.11.24's `read_drl_idx`, `RefMvIdx` starting at 0): |
| 20595 | // read at most two `drl_mode` bits, one per stack entry past the |
| 20596 | // first, stopping at the first `0`. The chosen index selects |
| 20597 | // which stack entry `read_mv`'s base predictor comes from |
| 20598 | // (spec 7.10.2.10 `assign_mv`'s `PredMv = RefStackMv[RefMvIdx]`) |
| 20599 | // -- entry 0 (`stack.pred_mv`) only when the loop never advances. |
| 20600 | let mut idx = 0usize; |
| 20601 | while idx < 2 && stack.entries.len() > idx + 1 { |
| 20602 | if dec.symbol(&mut cdfs.drl_mode[stack.drl_ctx[idx]]) == 0 { |
| 20603 | break; |
| 20604 | } |
| 20605 | idx += 1; |
| 20606 | } |
| 20607 | let base_mv = stack.entries.get(idx).map_or(stack.pred_mv, |e| e.mv); |
| 20608 | ( |
| 20609 | read_mv( |
| 20610 | dec, |
| 20611 | &mut cdfs.mv_comp, |
| 20612 | &mut cdfs.mv_joint, |
| 20613 | base_mv, |
| 20614 | allow_high_precision_mv, |
| 20615 | force_integer_mv, |
| 20616 | ), |
| 20617 | true, |
| 20618 | ) |
| 20619 | } else { |
| 20620 | let not_zero = dec.symbol(&mut cdfs.zero_mv[stack.zero_mv_ctx]) == 1; |
| 20621 | is_globalmv = !not_zero; |
| 20622 | let mv = if !not_zero { |
| 20623 | // lane-gmaffine r1: GLOBALMV (spec 7.10.2.1) -- the same |
| 20624 | // `gm_get_motion_vector` value the 16x16+ leaf reads out of |
| 20625 | // its own `gm_table`, computed at this block's centre. |
| 20626 | GLOBALMV_HITS_8.with(|c| c.set(c.get() + 1)); |
| 20627 | gm_table[(ref_frame - LAST_FRAME) as usize] |
| 20628 | } else { |
| 20629 | let nearest = dec.symbol(&mut cdfs.ref_mv[stack.ref_mv_ctx]) == 0; |
| 20630 | if nearest { |
| 20631 | stack.nearest_mv |
| 20632 | } else { |
| 20633 | let mut idx = 1usize; |
| 20634 | while idx < 3 && stack.entries.len() > idx + 1 { |
| 20635 | if dec.symbol(&mut cdfs.drl_mode[stack.drl_ctx[idx]]) == 0 { |
| 20636 | break; |
| 20637 | } |
| 20638 | idx += 1; |
| 20639 | } |
| 20640 | stack.entries.get(idx).map_or(stack.near_mv, |e| e.mv) |
| 20641 | } |
| 20642 | }; |
| 20643 | (mv, false) |
| 20644 | }; |
| 20645 | // lane-sb128 r4: `interintra` (spec 5.11.24) at the 8x8 leaf too -- |
| 20646 | // see [`decode_inter_block`]'s own doc; `BLOCK_8X8` is always |
| 20647 | // `is_interintra_allowed_bsize`-eligible, single-ref-only branch |
| 20648 | // (compound already returned above), GLOBALMV already refused |
| 20649 | // (round 3), so the only real gate left is the header enable bit. |
| 20650 | let mut interintra_mode: Option<u8> = None; |
| 20651 | let mut wedge_mask: Option<(&'static [u8], usize)> = None; |
| 20652 | if enable_interintra_compound && !skip_mode { |
| 20653 | let interintra = dec.symbol(&mut cdfs.interintra[SIZE_GROUP_8]) == 1; |
| 20654 | if interintra { |
| 20655 | // lane-interintra r1: same read pair as the 16/32 site; |
| 20656 | // wedge cdf row = BLOCK_8X8 (index 3), read on block size |
| 20657 | // alone, never the `enable_interintra_wedge` seq bit. |
| 20658 | let ii = dec.symbol(&mut cdfs.interintra_mode[SIZE_GROUP_8]) as u8; |
| 20659 | let wedge = dec.symbol(&mut cdfs.wedge_interintra[3]) == 1; |
| 20660 | if wedge { |
| 20661 | // lane-wii r2: same adapting `wedge_index` symbol as the |
| 20662 | // 16/32 leaf, fixed sign 0 -- see that site's comment. |
| 20663 | let wedge_index = dec.symbol(&mut cdfs.wedge_idx[3]); |
| 20664 | WII_HITS.with(|c| c.set(c.get() + 1)); |
| 20665 | wedge_mask = Some(( |
| 20666 | crate::wedge::wedge_masks() |
| 20667 | .codebook(SIDE) |
| 20668 | .mask(0, wedge_index as usize), |
| 20669 | SIDE, |
| 20670 | )); |
| 20671 | } else { |
| 20672 | INTERINTRA_HITS.with(|c| c.set(c.get() + 1)); |
| 20673 | } |
| 20674 | interintra_mode = Some(ii); |
| 20675 | } |
| 20676 | } |
| 20677 | // lane-motionmode round 3: `read_motion_mode` (spec 5.11.24) at the |
| 20678 | // 8x8 leaf too -- `motion_mode_allowed` still holds here (BLOCK_8X8 |
| 20679 | // is the spec's own minimum eligible size, single-ref-only branch, |
| 20680 | // no interintra reader, `IDENTITY`-only global motion). Mirrors the |
| 20681 | // 16x16 leaf's own `decode_inter_block` read (see its doc above) -- |
| 20682 | // same 2-symbol `obmc_cdf` alphabet, same `allow_warped_motion=1` |
| 20683 | // refusal (warp needs `av1_findSamples`, not ported at either leaf). |
| 20684 | // lane-gmaffine r1: the two libaom predicates, exactly as the |
| 20685 | // 16x16+ leaf resolves them (see [`decode_inter_block`]'s own doc): |
| 20686 | // `is_global_mv_block` (mode GLOBAL*, model > TRANSLATION, block |
| 20687 | // >= 8x8 -- BLOCK_8X8 IS the 8px minimum, so the size predicate is |
| 20688 | // always true here) suppresses the motion_mode read entirely and |
| 20689 | // turns on the global warp below. |
| 20690 | let gm_ref = &global_motion[(ref_frame - LAST_FRAME) as usize]; |
| 20691 | let is_global_mv_block = is_globalmv && gm_ref.model as u8 > 1; |
| 20692 | let motion_mode_eligible = switchable_motion_mode |
| 20693 | && !skip_mode |
| 20694 | && interintra_mode.is_none() |
| 20695 | && !(is_global_mv_block && !force_integer_mv) |
| 20696 | && (!overlappable_above(grid, mi_row, mi_col, 2, mi_cols as usize, 1).is_empty() |
| 20697 | || !overlappable_left(grid, mi_row, mi_col, 2, mi_rows as usize, 1).is_empty()); |
| 20698 | let mut warp_params: Option<crate::warp::WarpParams> = None; |
| 20699 | let mut warped_selected = false; |
| 20700 | let mut obmc_selected = false; |
| 20701 | if motion_mode_eligible { |
| 20702 | // lane-warp round 1: same 3-vs-2-symbol split as the 16x16+ leaf |
| 20703 | // (see its own doc) -- this leaf is always `LAST_FRAME`-only |
| 20704 | // (grid.set below hardcodes it). |
| 20705 | // lane-scaledref r2 sibling of the 16x16+ leaf's fix: libaom |
| 20706 | // drops WARPED_CAUSAL eligibility (so this reads `obmc_cdf`) |
| 20707 | // when the reference is scaled. Not gated here because an 8x8 |
| 20708 | // leaf under a scaled reference is refused before this function |
| 20709 | // runs (see the block8 refusal below, ~12500) -- whoever lifts |
| 20710 | // that refusal must add `&& !ref_is_scaled` here too. |
| 20711 | // lane-fimv r1: libaom `motion_mode_allowed` also requires |
| 20712 | // `!xd->cur_frame_force_integer_mv`, so a screen-content frame |
| 20713 | // that set `force_integer_mv` reads the 2-symbol `obmc_cdf` here |
| 20714 | // (class wrong-alphabet-same-value). |
| 20715 | let proj_ok = |
| 20716 | num_proj_ref(grid, mi_row, mi_col, 2, 2, mi_cols as usize, mi_rows as usize, ref_frame) >= 1; |
| 20717 | let warp_eligible = allow_warped_motion && !force_integer_mv && proj_ok; |
| 20718 | if allow_warped_motion && proj_ok && force_integer_mv { |
| 20719 | FIMV_ALPHABET_HITS.with(|c| c.set(c.get() + 1)); |
| 20720 | } |
| 20721 | if warp_eligible { |
| 20722 | let mode = dec.symbol(&mut cdfs.motion_mode[0]); |
| 20723 | match mode { |
| 20724 | 0 => {} |
| 20725 | 1 => { |
| 20726 | obmc_selected = true; |
| 20727 | OBMC_HITS.with(|c| c.set(c.get() + 1)); |
| 20728 | OBMC_HITS_8.with(|c| c.set(c.get() + 1)); |
| 20729 | } |
| 20730 | _ => { |
| 20731 | // lane-gmaffine r1: WARPED_CAUSAL at the 8x8 leaf -- |
| 20732 | // the same findSamples -> select_samples -> |
| 20733 | // av1_find_projection chain the 16x16+ leaf runs, |
| 20734 | // with BLOCK_8X8's own 8x8 model centre; an |
| 20735 | // unusable projection falls back to the block's |
| 20736 | // translational mv (libaom `av1_find_projection` |
| 20737 | // returning 1 leaves `wm_params` invalid and |
| 20738 | // `build_inter_predictors` skips the warp). |
| 20739 | WARP_SELECTED_HITS.with(|c| c.set(c.get() + 1)); |
| 20740 | let mut samples = find_samples( |
| 20741 | grid, |
| 20742 | mi_row, |
| 20743 | mi_col, |
| 20744 | 2, |
| 20745 | 2, |
| 20746 | mi_cols as usize, |
| 20747 | mi_rows as usize, |
| 20748 | ref_frame, |
| 20749 | ); |
| 20750 | if samples.len() > 1 { |
| 20751 | crate::warp::select_samples(mv, &mut samples, SIDE as i32, SIDE as i32); |
| 20752 | } |
| 20753 | warp_params = crate::warp::find_projection( |
| 20754 | &samples, |
| 20755 | SIDE as i32, |
| 20756 | SIDE as i32, |
| 20757 | mv.1, |
| 20758 | mv.0, |
| 20759 | mi_row as i32, |
| 20760 | mi_col as i32, |
| 20761 | ); |
| 20762 | warped_selected = true; |
| 20763 | if warp_params.is_some() { |
| 20764 | WARP_HITS_8.with(|c| c.set(c.get() + 1)); |
| 20765 | } |
| 20766 | } |
| 20767 | } |
| 20768 | } else { |
| 20769 | // `default_obmc_cdf`'s own index: square bsize 8/16/32/64 -> 0/1/2/3. |
| 20770 | obmc_selected = dec.symbol(&mut cdfs.obmc[0]) == 1; |
| 20771 | if obmc_selected { |
| 20772 | OBMC_HITS.with(|c| c.set(c.get() + 1)); |
| 20773 | OBMC_HITS_8.with(|c| c.set(c.get() + 1)); |
| 20774 | } |
| 20775 | } |
| 20776 | } |
| 20777 | // lane-gmaffine r1: `read_mb_interp_filter` (spec 5.11.26) at the 8x8 |
| 20778 | // leaf, in libaom's own order (interintra, motion_mode, THEN the |
| 20779 | // filter). Until this round the leaf read no filter symbol at all and |
| 20780 | // predicted Regular unconditionally -- correct only for a frame with a |
| 20781 | // fixed `interp_filter`, and a silent entropy DESYNC for every |
| 20782 | // SWITCHABLE-filter stream that reaches an 8x8 leaf (which is what |
| 20783 | // made the interior-split wiring above unusable at first). |
| 20784 | // corner-cut: the neighbour filter context is the enclosing 16x16 |
| 20785 | // block's own coarse `above_filter`/`left_filter` entry (this leaf |
| 20786 | // path keeps no per-leaf neighbour array), so all four siblings read |
| 20787 | // the same context row. Ceiling: a stream whose 8x8 siblings pick |
| 20788 | // DIFFERENT filters decodes the second..fourth leaf's symbol from the |
| 20789 | // wrong CDF row. Upgrade: give `Neighbours` a real 8x8-granular |
| 20790 | // filter array, same shape as the `[3, 3]` approximation the caller |
| 20791 | // records after the leaf loop. |
| 20792 | let above_filter_ctx = if neighbours.above_ref[cmi] == ref_frame |
| 20793 | || neighbours.above_ref1[cmi] == Some(ref_frame) |
| 20794 | { |
| 20795 | neighbours.above_filter[cmi] |
| 20796 | } else { |
| 20797 | [3, 3] |
| 20798 | }; |
| 20799 | let left_filter_ctx = if neighbours.left_ref[rmi] == ref_frame |
| 20800 | || neighbours.left_ref1[rmi] == Some(ref_frame) |
| 20801 | { |
| 20802 | neighbours.left_filter[rmi] |
| 20803 | } else { |
| 20804 | [3, 3] |
| 20805 | }; |
| 20806 | let gm_nontrans = |
| 20807 | is_globalmv && gm_ref.model != ec_av1_syntax::WarpModel::Translation; |
| 20808 | let (h_filter, v_filter, resolved_filter) = resolve_interp_filter( |
| 20809 | dec, |
| 20810 | cdfs, |
| 20811 | interp_fixed, |
| 20812 | enable_dual_filter, |
| 20813 | gm_nontrans || warped_selected || skip_mode, |
| 20814 | above_filter_ctx, |
| 20815 | left_filter_ctx, |
| 20816 | false, |
| 20817 | ); |
| 20818 | leaf_filter_syms = resolved_filter; |
| 20819 | if std::env::var_os("EC_TRACE_MODE").is_some() { |
| 20820 | eprintln!( |
| 20821 | "EC_MODE_VAL8 mi_row={} mi_col={} newmv={is_new_mv} globalmv={is_globalmv} ref0={ref_frame} mv0=({},{}) stack={} rng={}", |
| 20822 | leaf_mi.0, |
| 20823 | leaf_mi.1, |
| 20824 | mv.0, |
| 20825 | mv.1, |
| 20826 | stack.entries.len(), |
| 20827 | dec.debug_state().0 |
| 20828 | ); |
| 20829 | } |
| 20830 | // lane-gmaffine r1: `allow_warp`'s `global_warp_allowed` branch |
| 20831 | // (`reconinter.c:33-55`), gated INDEPENDENTLY of `motion_mode` -- |
| 20832 | // see [`decode_inter_block`]'s own copy. A local WARPED_CAUSAL model |
| 20833 | // wins (`is_none()` guard); BLOCK_8X8 already satisfies the >=8px |
| 20834 | // size predicate baked into `is_global_mv_block`. |
| 20835 | if warp_params.is_none() && is_global_mv_block && !force_integer_mv && !gm_ref.invalid { |
| 20836 | warp_params = crate::warp::global_warp_params(gm_ref.params); |
| 20837 | if warp_params.is_some() && gm_ref.model == ec_av1_syntax::WarpModel::Affine { |
| 20838 | AFFINE_GM_HITS.with(|c| c.set(c.get() + 1)); |
| 20839 | } |
| 20840 | } |
| 20841 | if std::env::var_os("EC_TRACE_MODE").is_some() { |
| 20842 | eprintln!( |
| 20843 | "EC_WARP8 mi_row={} mi_col={} warp={} globalblk={is_global_mv_block}", |
| 20844 | leaf_mi.0, |
| 20845 | leaf_mi.1, |
| 20846 | warp_params.is_some(), |
| 20847 | ); |
| 20848 | } |
| 20849 | for dr in 0..2 { |
| 20850 | for dc in 0..2 { |
| 20851 | grid.set( |
| 20852 | mi_row + dr, |
| 20853 | mi_col + dc, |
| 20854 | MiInfo { |
| 20855 | is_inter: true, |
| 20856 | ref_frame, |
| 20857 | // INTRA_FRAME marker for interintra blocks -- keeps |
| 20858 | // them out of warp-sample gathering (see 16/32 site). |
| 20859 | ref_frame1: interintra_mode.map(|_| 0), |
| 20860 | mv1: None, |
| 20861 | mv, |
| 20862 | is_new_mv, |
| 20863 | size: 2, |
| 20864 | size_h: 2, |
| 20865 | is_global_mv0: is_global_mv_block, |
| 20866 | is_global_mv1: false, |
| 20867 | }, |
| 20868 | ); |
| 20869 | } |
| 20870 | } |
| 20871 | mode_for_tx = 0; |
| 20872 | |
| 20873 | // lane-scaledref r1: this leaf's own prediction is always `Regular` |
| 20874 | // (documented corner-cut above); under a scaled reference the same |
| 20875 | // fixed kernel runs through spec 7.11.3.3's scaled walk instead. |
| 20876 | let luma_scale = mc::scale_factor(sref_y.width, frame_width); |
| 20877 | if luma_scale != mc::REF_NO_SCALE { |
| 20878 | SCALED_BLOCK8_HITS.with(|c| c.set(c.get() + 1)); |
| 20879 | } |
| 20880 | let mut pred_y = vec![0u16; SIDE * SIDE]; |
| 20881 | let mut pred_u = vec![0u16; CHROMA_SIDE * CHROMA_SIDE]; |
| 20882 | let mut pred_v = vec![0u16; CHROMA_SIDE * CHROMA_SIDE]; |
| 20883 | // Merge of lane-gmaffine (this leaf's own switchable filters) with |
| 20884 | // lane-scaledref (spec 7.11.3.3's scaled walk): same kernels either |
| 20885 | // way, only the sample walk differs. |
| 20886 | for (plane, x, y, dim, dst) in [ |
| 20887 | (sref_y, px, py, SIDE, &mut pred_y), |
| 20888 | (sref_u, cpx, cpy, CHROMA_SIDE, &mut pred_u), |
| 20889 | (sref_v, cpx, cpy, CHROMA_SIDE, &mut pred_v), |
| 20890 | ] { |
| 20891 | let luma = std::ptr::eq(plane, sref_y); |
| 20892 | if luma_scale == mc::REF_NO_SCALE { |
| 20893 | mc::predict_with_filters( |
| 20894 | &plane.data, |
| 20895 | plane.width, |
| 20896 | plane.true_width, |
| 20897 | plane.true_height, |
| 20898 | mv_to_q4(x, mv.1, luma), |
| 20899 | mv_to_q4(y, mv.0, luma), |
| 20900 | dim, |
| 20901 | dim, |
| 20902 | h_filter, |
| 20903 | v_filter, |
| 20904 | dst, |
| 20905 | ); |
| 20906 | } else { |
| 20907 | mc::predict_scaled( |
| 20908 | &plane.data, |
| 20909 | plane.width, |
| 20910 | plane.true_width, |
| 20911 | plane.true_height, |
| 20912 | mv_to_q4(x, mv.1, luma), |
| 20913 | mv_to_q4(y, mv.0, luma), |
| 20914 | luma_scale, |
| 20915 | dim, |
| 20916 | dim, |
| 20917 | h_filter, |
| 20918 | v_filter, |
| 20919 | dst, |
| 20920 | ); |
| 20921 | } |
| 20922 | } |
| 20923 | |
| 20924 | // lane-gmaffine r1: the warp filter REPLACES the translational |
| 20925 | // prediction just built (libaom builds one or the other), same as |
| 20926 | // the 16x16+ leaf's own call trio. |
| 20927 | if let Some(params) = &warp_params { |
| 20928 | crate::warp::warp_affine( |
| 20929 | params, &sref_y.data, sref_y.true_width as i32, sref_y.true_height as i32, |
| 20930 | sref_y.width as i32, &mut pred_y, px as i32, py as i32, SIDE as i32, |
| 20931 | SIDE as i32, SIDE as i32, 0, 0, |
| 20932 | ); |
| 20933 | // `av1_init_warp_params`'s per-plane `block_width/height < 8` |
| 20934 | // bail-out: an 8x8 leaf's chroma plane is 4x4 in 420, so it is |
| 20935 | // predicted translationally even when the luma warps. |
| 20936 | #[allow(clippy::absurd_extreme_comparisons)] |
| 20937 | if CHROMA_SIDE >= 8 { |
| 20938 | crate::warp::warp_affine( |
| 20939 | params, &sref_u.data, sref_u.true_width as i32, sref_u.true_height as i32, |
| 20940 | sref_u.width as i32, &mut pred_u, cpx as i32, cpy as i32, CHROMA_SIDE as i32, |
| 20941 | CHROMA_SIDE as i32, CHROMA_SIDE as i32, 1, 1, |
| 20942 | ); |
| 20943 | crate::warp::warp_affine( |
| 20944 | params, &sref_v.data, sref_v.true_width as i32, sref_v.true_height as i32, |
| 20945 | sref_v.width as i32, &mut pred_v, cpx as i32, cpy as i32, CHROMA_SIDE as i32, |
| 20946 | CHROMA_SIDE as i32, CHROMA_SIDE as i32, 1, 1, |
| 20947 | ); |
| 20948 | } |
| 20949 | } |
| 20950 | |
| 20951 | if obmc_selected { |
| 20952 | obmc_blend( |
| 20953 | grid, |
| 20954 | neighbours, |
| 20955 | mi_row, |
| 20956 | mi_col, |
| 20957 | 2, |
| 20958 | 2, |
| 20959 | mi_rows as usize, |
| 20960 | mi_cols as usize, |
| 20961 | SIDE, |
| 20962 | SIDE, |
| 20963 | SIDE, |
| 20964 | CHROMA_SIDE, |
| 20965 | px, |
| 20966 | py, |
| 20967 | cpx, |
| 20968 | cpy, |
| 20969 | ref_y, |
| 20970 | ref_u, |
| 20971 | ref_v, |
| 20972 | other_refs, |
| 20973 | interp_fixed, |
| 20974 | frame_width, |
| 20975 | &mut pred_y, |
| 20976 | &mut pred_u, |
| 20977 | &mut pred_v, |
| 20978 | )?; |
| 20979 | } |
| 20980 | |
| 20981 | // Non-wedge interintra blend, mutually exclusive with OBMC (the |
| 20982 | // motion_mode symbol is not read for an interintra block). |
| 20983 | if let Some(ii) = interintra_mode { |
| 20984 | interintra_blend(y, px, py, SIDE, ii, wedge_mask, &mut pred_y); |
| 20985 | interintra_blend(u, cpx, cpy, CHROMA_SIDE, ii, wedge_mask, &mut pred_u); |
| 20986 | interintra_blend(v, cpx, cpy, CHROMA_SIDE, ii, wedge_mask, &mut pred_v); |
| 20987 | } |
| 20988 | |
| 20989 | split8 = read_block_tx_size( |
| 20990 | dec, |
| 20991 | cdfs, |
| 20992 | neighbours, |
| 20993 | leaf_mi, |
| 20994 | SIDE, |
| 20995 | (mi_cols as usize, mi_rows as usize), |
| 20996 | true, |
| 20997 | skip, |
| 20998 | )? |
| 20999 | .1 |
| 21000 | .is_some(); |
| 21001 | if skip { |
| 21002 | y.reconstruct_mc(px, py, SIDE, &pred_y, &vec![0i32; SIDE * SIDE]); |
| 21003 | u.reconstruct_mc( |
| 21004 | cpx, |
| 21005 | cpy, |
| 21006 | CHROMA_SIDE, |
| 21007 | &pred_u, |
| 21008 | &vec![0i32; CHROMA_SIDE * CHROMA_SIDE], |
| 21009 | ); |
| 21010 | v.reconstruct_mc( |
| 21011 | cpx, |
| 21012 | cpy, |
| 21013 | CHROMA_SIDE, |
| 21014 | &pred_v, |
| 21015 | &vec![0i32; CHROMA_SIDE * CHROMA_SIDE], |
| 21016 | ); |
| 21017 | luma_grid = vec![0i32; SIDE * SIDE]; |
| 21018 | u_grid = vec![0i32; CHROMA_SIDE * CHROMA_SIDE]; |
| 21019 | v_grid = vec![0i32; CHROMA_SIDE * CHROMA_SIDE]; |
| 21020 | } else { |
| 21021 | let around = neighbours.around_mi(leaf_mi, 8); |
| 21022 | let (grid8, luma_tx_type) = read_inter_luma8( |
| 21023 | dec, |
| 21024 | cdfs, |
| 21025 | neighbours, |
| 21026 | leaf_mi, |
| 21027 | y, |
| 21028 | px, |
| 21029 | py, |
| 21030 | &pred_y, |
| 21031 | mode_for_tx, |
| 21032 | base_q_idx, |
| 21033 | scan8, |
| 21034 | scan4, |
| 21035 | reduced_tx_set, |
| 21036 | split8, |
| 21037 | )?; |
| 21038 | luma_grid = grid8; |
| 21039 | u_grid = read_inter_plane( |
| 21040 | dec, |
| 21041 | cdfs, |
| 21042 | TxbSet::Chroma4, |
| 21043 | scan4, |
| 21044 | 1, |
| 21045 | around[1], |
| 21046 | mode_for_tx, |
| 21047 | u, |
| 21048 | cpx, |
| 21049 | cpy, |
| 21050 | CHROMA_SIDE, |
| 21051 | base_q_idx, |
| 21052 | &pred_u, |
| 21053 | Some(luma_tx_type), |
| 21054 | None, |
| 21055 | )? |
| 21056 | .0; |
| 21057 | v_grid = read_inter_plane( |
| 21058 | dec, |
| 21059 | cdfs, |
| 21060 | TxbSet::Chroma4, |
| 21061 | scan4, |
| 21062 | 2, |
| 21063 | around[2], |
| 21064 | mode_for_tx, |
| 21065 | v, |
| 21066 | cpx, |
| 21067 | cpy, |
| 21068 | CHROMA_SIDE, |
| 21069 | base_q_idx, |
| 21070 | &pred_v, |
| 21071 | Some(luma_tx_type), |
| 21072 | None, |
| 21073 | )? |
| 21074 | .0; |
| 21075 | } |
| 21076 | } else { |
| 21077 | let mode = dec.symbol(&mut cdfs.y_mode[SIZE_GROUP_8]); |
| 21078 | if mode >= 13 { |
| 21079 | return Err(unsupported( |
| 21080 | "an intra mode this decoder does not code (round 2)", |
| 21081 | )); |
| 21082 | } |
| 21083 | // lane-angleinter r1: `read_intra_angle_info` (libaom `decodemv.c`, |
| 21084 | // inside `read_intra_block_mode_info`) -- an intra block in an inter |
| 21085 | // frame reads `angle_delta_y` off the same |
| 21086 | // `angle_delta_cdf[mode - V_PRED]` row a key-frame block does, and |
| 21087 | // the delta feeds the directional predictor (and the edge |
| 21088 | // filter/upsample decision derived from the delta'd angle) unchanged. |
| 21089 | // The symbol was read and then refused before this round even though |
| 21090 | // aomenc enables `--enable-angle-delta` by default. |
| 21091 | let angle_delta_y = if (V_PRED..=D67_PRED).contains(&mode) { |
| 21092 | read_angle_delta(dec, &mut cdfs.angle_delta[mode - V_PRED]) |
| 21093 | } else { |
| 21094 | 0 |
| 21095 | }; |
| 21096 | if angle_delta_y != 0 { |
| 21097 | INTRA_IN_INTER_ANGLE_DELTA_Y_HITS.with(|c| c.set(c.get() + 1)); |
| 21098 | // lane-angleinter r1: the >=16x16 arm's lift is gated by a real |
| 21099 | // aomenc stream; this leaf's is NOT -- 80 attempts across two |
| 21100 | // sizes and five quantisers never produced an 8x8 intra leaf with |
| 21101 | // a nonzero delta (the leaf's own sibling refusals -- non-DC |
| 21102 | // chroma, the TX_4X4 split, sub-16 rect inter leaves -- fire |
| 21103 | // first). The prediction below would handle it; refusing keeps |
| 21104 | // the rule that no lift ships ungated. Note the claim is about |
| 21105 | // THIS DECODER's gate coverage, not about what an encoder writes. |
| 21106 | return Err(unsupported( |
| 21107 | "a nonzero angle delta on an 8x8 intra leaf in an inter frame (no gate reaches \ |
| 21108 | this leaf with one; the >=16x16 arm decodes deltas)", |
| 21109 | )); |
| 21110 | } |
| 21111 | let uv_mode = dec.symbol(&mut cdfs.uv_mode_cfl[mode]); |
| 21112 | if uv_mode != DC_PRED { |
| 21113 | return Err(unsupported( |
| 21114 | "a non-DC chroma mode on an 8x8 inter-frame leaf (this encoder never writes one)", |
| 21115 | )); |
| 21116 | } |
| 21117 | // lane-screen r2: see [`decode_inter_block`]'s own copy -- `mode`/ |
| 21118 | // `uv_mode` are both effectively `DC_PRED`-gated already on this |
| 21119 | // leaf (a nonzero `mode` still passes, but `uv_mode` is forced |
| 21120 | // `DC_PRED` by the refusal just above), same order as libaom's |
| 21121 | // `read_palette_mode_info` call right after `xd->cfl.store_y`. |
| 21122 | if allow_screen_content_tools { |
| 21123 | if mode == DC_PRED && dec.symbol(&mut cdfs.palette_y_mode[0][0]) != 0 { |
| 21124 | return Err(unsupported( |
| 21125 | "a block that actually uses a palette (Y) -- reconstruction is out of scope", |
| 21126 | )); |
| 21127 | } |
| 21128 | if dec.symbol(&mut cdfs.palette_uv_mode[0]) != 0 { |
| 21129 | return Err(unsupported( |
| 21130 | "a block that actually uses a palette (UV) -- reconstruction is out of scope", |
| 21131 | )); |
| 21132 | } |
| 21133 | } |
| 21134 | // lane-fiinter r1: `read_filter_intra_mode_info` (libaom `decodemv.c`, |
| 21135 | // the tail of `read_intra_block_mode_info`) -- an intra block inside |
| 21136 | // an INTER frame reads `use_filter_intra` in exactly the same place a |
| 21137 | // key-frame block does, right after the palette syntax. Nothing read |
| 21138 | // it here before this round, and aomenc enables filter intra by |
| 21139 | // default, so every inter frame carrying one desynced at this symbol. |
| 21140 | // `av1_filter_intra_allowed`'s `palette_size[0] == 0` term is implied: |
| 21141 | // a real palette-Y block already returned above. |
| 21142 | let mut filter_intra = None; |
| 21143 | if mode == DC_PRED |
| 21144 | && ENABLE_FILTER_INTRA_INTER.with(std::cell::Cell::get) |
| 21145 | && let Some(class) = filter_intra_size_class(SIDE) |
| 21146 | && dec.symbol(&mut cdfs.filter_intra[class]) != 0 |
| 21147 | { |
| 21148 | FILTER_INTRA_HITS.with(|c| c.set(c.get() + 1)); |
| 21149 | INTRA_IN_INTER_FILTER_INTRA_HITS.with(|c| c.set(c.get() + 1)); |
| 21150 | filter_intra = Some(dec.symbol(&mut cdfs.filter_intra_mode)); |
| 21151 | } |
| 21152 | mode_for_tx = mode; |
| 21153 | let (mi_row, mi_col) = leaf_mi; |
| 21154 | for dr in 0..2 { |
| 21155 | for dc in 0..2 { |
| 21156 | grid.set( |
| 21157 | mi_row + dr, |
| 21158 | mi_col + dc, |
| 21159 | MiInfo { |
| 21160 | is_inter: false, |
| 21161 | ref_frame: -1, |
| 21162 | ref_frame1: None, |
| 21163 | mv1: None, |
| 21164 | mv: (0, 0), |
| 21165 | is_new_mv: false, |
| 21166 | size: 2, |
| 21167 | size_h: 2, |
| 21168 | is_global_mv0: false, |
| 21169 | is_global_mv1: false, |
| 21170 | }, |
| 21171 | ); |
| 21172 | } |
| 21173 | } |
| 21174 | |
| 21175 | // lane-intrainter r1: the 8x8 leaf's own intra `tx_depth` can only |
| 21176 | // resolve to TX_4X4, whose 2x2 unit grid this leaf path has no |
| 21177 | // prediction loop for (the >=16x16 square path above does). Refuse by |
| 21178 | // name rather than mis-reconstruct. |
| 21179 | if read_block_tx_size( |
| 21180 | dec, |
| 21181 | cdfs, |
| 21182 | neighbours, |
| 21183 | leaf_mi, |
| 21184 | SIDE, |
| 21185 | (mi_cols as usize, mi_rows as usize), |
| 21186 | false, |
| 21187 | skip, |
| 21188 | )? |
| 21189 | .1 |
| 21190 | .is_some() |
| 21191 | { |
| 21192 | return Err(unsupported( |
| 21193 | "an 8x8 intra leaf in an inter frame whose tx_depth splits it into 4x4 \ |
| 21194 | transform units", |
| 21195 | )); |
| 21196 | } |
| 21197 | let reach = Reach::of(SIDE, px, py, y.width, y.height); |
| 21198 | if skip { |
| 21199 | y.reconstruct( |
| 21200 | px, |
| 21201 | py, |
| 21202 | SIDE, |
| 21203 | mode, |
| 21204 | angle_delta_y, |
| 21205 | reach, |
| 21206 | &vec![0i32; SIDE * SIDE], |
| 21207 | None, |
| 21208 | filter_intra, |
| 21209 | false, |
| 21210 | ); |
| 21211 | u.reconstruct( |
| 21212 | cpx, |
| 21213 | cpy, |
| 21214 | CHROMA_SIDE, |
| 21215 | DC_PRED, |
| 21216 | 0, |
| 21217 | reach, |
| 21218 | &vec![0i32; CHROMA_SIDE * CHROMA_SIDE], |
| 21219 | None, |
| 21220 | None, |
| 21221 | false, |
| 21222 | ); |
| 21223 | v.reconstruct( |
| 21224 | cpx, |
| 21225 | cpy, |
| 21226 | CHROMA_SIDE, |
| 21227 | DC_PRED, |
| 21228 | 0, |
| 21229 | reach, |
| 21230 | &vec![0i32; CHROMA_SIDE * CHROMA_SIDE], |
| 21231 | None, |
| 21232 | None, |
| 21233 | false, |
| 21234 | ); |
| 21235 | luma_grid = vec![0i32; SIDE * SIDE]; |
| 21236 | u_grid = vec![0i32; CHROMA_SIDE * CHROMA_SIDE]; |
| 21237 | v_grid = vec![0i32; CHROMA_SIDE * CHROMA_SIDE]; |
| 21238 | } else { |
| 21239 | let around = neighbours.around_mi(leaf_mi, 8); |
| 21240 | luma_grid = read_plane( |
| 21241 | dec, |
| 21242 | cdfs, |
| 21243 | TxbSet::Luma8, |
| 21244 | scan8, |
| 21245 | 0, |
| 21246 | around[0], |
| 21247 | mode, |
| 21248 | mode, |
| 21249 | angle_delta_y, |
| 21250 | reach, |
| 21251 | y, |
| 21252 | px, |
| 21253 | py, |
| 21254 | SIDE, |
| 21255 | TX8, |
| 21256 | base_q_idx, |
| 21257 | None, |
| 21258 | filter_intra, |
| 21259 | None, |
| 21260 | false, |
| 21261 | )?; |
| 21262 | u_grid = read_plane( |
| 21263 | dec, |
| 21264 | cdfs, |
| 21265 | TxbSet::Chroma4, |
| 21266 | scan4, |
| 21267 | 1, |
| 21268 | around[1], |
| 21269 | mode, |
| 21270 | DC_PRED, |
| 21271 | 0, |
| 21272 | reach, |
| 21273 | u, |
| 21274 | cpx, |
| 21275 | cpy, |
| 21276 | CHROMA_SIDE, |
| 21277 | TX4, |
| 21278 | base_q_idx, |
| 21279 | None, |
| 21280 | None, |
| 21281 | None, |
| 21282 | false, |
| 21283 | )?; |
| 21284 | v_grid = read_plane( |
| 21285 | dec, |
| 21286 | cdfs, |
| 21287 | TxbSet::Chroma4, |
| 21288 | scan4, |
| 21289 | 2, |
| 21290 | around[2], |
| 21291 | mode, |
| 21292 | DC_PRED, |
| 21293 | 0, |
| 21294 | reach, |
| 21295 | v, |
| 21296 | cpx, |
| 21297 | cpy, |
| 21298 | CHROMA_SIDE, |
| 21299 | TX4, |
| 21300 | base_q_idx, |
| 21301 | None, |
| 21302 | None, |
| 21303 | None, |
| 21304 | false, |
| 21305 | )?; |
| 21306 | } |
| 21307 | } |
| 21308 | // lane-txselect: a split leaf's plane-0 neighbour context is per 4x4 |
| 21309 | // transform unit (written by `record_mi_luma` inside `read_inter_luma8`), |
| 21310 | // but `record_mi` rewrites all three planes of these cells at once -- so |
| 21311 | // save plane 0 across it, exactly as `record_split_luma` leaves plane 0 |
| 21312 | // alone at the bigger block sizes. |
| 21313 | let saved_luma_ctx = split8.then(|| { |
| 21314 | ( |
| 21315 | [ |
| 21316 | neighbours.left[leaf_mi.0][0], |
| 21317 | neighbours.left[leaf_mi.0 + 1][0], |
| 21318 | ], |
| 21319 | [ |
| 21320 | neighbours.above[leaf_mi.1][0], |
| 21321 | neighbours.above[leaf_mi.1 + 1][0], |
| 21322 | ], |
| 21323 | ) |
| 21324 | }); |
| 21325 | neighbours.record_mi(leaf_mi, 8, &[luma_grid, u_grid, v_grid]); |
| 21326 | if let Some((left, above)) = saved_luma_ctx { |
| 21327 | for (cell, state) in left.into_iter().enumerate() { |
| 21328 | neighbours.left[leaf_mi.0 + cell][0] = state; |
| 21329 | } |
| 21330 | for (cell, state) in above.into_iter().enumerate() { |
| 21331 | neighbours.above[leaf_mi.1 + cell][0] = state; |
| 21332 | } |
| 21333 | } |
| 21334 | // lane-gmaffine r2: the deblock grid's ref id is the leaf's OWN reference |
| 21335 | // (r1 taught this leaf non-LAST refs; the hardcoded LAST_FRAME made every |
| 21336 | // GOLDEN/ALTREF leaf read as LAST at the loop-filter's ref/mv edge test). |
| 21337 | // lane-cdef r1: the fall-through twin of the compound arm's skip-band |
| 21338 | // write above -- `skip` is one flag for the whole 8x8 leaf (read at |
| 21339 | // `decode.rs`'s `skip_ctx` site), `split8` only splits its transform. |
| 21340 | neighbours.fill_skip_grid(leaf_mi, 2, skip); |
| 21341 | neighbours.fill_lf_grid( |
| 21342 | leaf_mi, |
| 21343 | 2, |
| 21344 | if split8 { 4 } else { 8 }, |
| 21345 | if is_inter { leaf_refs.0.max(LAST_FRAME) } else { 0 }, |
| 21346 | ); |
| 21347 | Ok(( |
| 21348 | skip, |
| 21349 | is_inter, |
| 21350 | skip_mode, |
| 21351 | compound_ctx8, |
| 21352 | leaf_filter_syms, |
| 21353 | leaf_refs, |
| 21354 | )) |
| 21355 | } |
| 21356 | |
| 21357 | /// Decodes the payload [`crate::tile::sb_coeff_inter_frame_tile`] writes, |
| 21358 | /// against `reference` (the previous frame's own decoded picture, at this |
| 21359 | /// frame's true size -- a decoder's single-slot DPB, matching what this |
| 21360 | /// crate's encoder always predicts from), returning the picture it |
| 21361 | /// reconstructs to. Mirrors [`decode_key_frame_tile`]'s own contract |
| 21362 | /// (`mi_cols`/`mi_rows`/`base_q_idx` from the frame header, |
| 21363 | /// `frame_width`/`frame_height` its true render size). |
| 21364 | /// |
| 21365 | /// # Errors |
| 21366 | /// Returns an error under the same conditions [`decode_inter_block`] does, |
| 21367 | /// or when `reference` is not this frame's own true size, or when the tile |
| 21368 | /// codes a 16x16 leaf whose own true edge cuts through it (round 2, same |
| 21369 | /// refusal [`crate::tile`]'s writer gives its encoder). |
| 21370 | pub fn decode_inter_frame_tile( |
| 21371 | data: &[u8], |
| 21372 | mi_cols: u32, |
| 21373 | mi_rows: u32, |
| 21374 | base_q_idx: u8, |
| 21375 | frame_width: u32, |
| 21376 | frame_height: u32, |
| 21377 | reference: &Picture, |
| 21378 | other_refs: [Option<&Picture>; 8], |
| 21379 | cdef: &CdefParams, |
| 21380 | loop_filter: &LoopFilterParams, |
| 21381 | allow_high_precision_mv: bool, |
| 21382 | force_integer_mv: bool, |
| 21383 | interp_fixed: Option<mc::InterpFilterKind>, |
| 21384 | enable_dual_filter: bool, |
| 21385 | reference_select: bool, |
| 21386 | ) -> Result<Picture> { |
| 21387 | let single_tile = TileInfo { |
| 21388 | mi_col_starts: vec![0, mi_cols], |
| 21389 | mi_row_starts: vec![0, mi_rows], |
| 21390 | ..TileInfo::default() |
| 21391 | }; |
| 21392 | decode_inter_frame_tile_with_cdfs( |
| 21393 | &[data], |
| 21394 | &single_tile, |
| 21395 | mi_cols, |
| 21396 | mi_rows, |
| 21397 | base_q_idx, |
| 21398 | crate::quant::QuantDeltas::default(), |
| 21399 | frame_width, |
| 21400 | frame_height, |
| 21401 | reference, |
| 21402 | other_refs, |
| 21403 | cdef, |
| 21404 | loop_filter, |
| 21405 | &LoopRestorationParams::default(), |
| 21406 | None, |
| 21407 | allow_high_precision_mv, |
| 21408 | force_integer_mv, |
| 21409 | NO_SIGN_BIAS, |
| 21410 | [ec_av1_syntax::WarpParams::default(); 7], |
| 21411 | interp_fixed, |
| 21412 | enable_dual_filter, |
| 21413 | 0, |
| 21414 | 0, |
| 21415 | [0; 7], |
| 21416 | None, |
| 21417 | reference_select, |
| 21418 | false, |
| 21419 | false, |
| 21420 | false, |
| 21421 | false, |
| 21422 | [0; 2], |
| 21423 | true, |
| 21424 | // `tx_select`: this raw entry point decodes this crate's own encoder's |
| 21425 | // streams, which always write `TxMode::Largest`. |
| 21426 | false, |
| 21427 | false, |
| 21428 | false, |
| 21429 | false, |
| 21430 | DeltaParams::default(), |
| 21431 | // `enable_filter_intra`: this crate's own encoder never writes the |
| 21432 | // sequence bit (`encode.rs`), so no intra-in-inter block of a stream |
| 21433 | // reaching this entry point reads a `use_filter_intra` symbol. |
| 21434 | false, |
| 21435 | ) |
| 21436 | .map(|(picture, _, _)| picture) |
| 21437 | } |
| 21438 | |
| 21439 | /// [`decode_inter_frame_tile`], threading a cross-frame CDF forward -- see |
| 21440 | /// [`decode_key_frame_tile_with_cdfs`]'s doc for `initial_cdfs`/the returned |
| 21441 | /// end-of-tile table. An inter frame's own header can name |
| 21442 | /// `primary_ref_frame == PRIMARY_REF_NONE` too (an error-resilient stream), |
| 21443 | /// so `None` is a real case here, not only a convenience default. |
| 21444 | pub(crate) fn decode_inter_frame_tile_with_cdfs( |
| 21445 | tiles: &[&[u8]], |
| 21446 | tile_info: &TileInfo, |
| 21447 | mi_cols: u32, |
| 21448 | mi_rows: u32, |
| 21449 | base_q_idx: u8, |
| 21450 | // lane-sbpart r11: see [`decode_key_frame_tile_with_cdfs`]'s own `deltas`. |
| 21451 | deltas: crate::quant::QuantDeltas, |
| 21452 | frame_width: u32, |
| 21453 | frame_height: u32, |
| 21454 | reference: &Picture, |
| 21455 | other_refs: [Option<&Picture>; 8], |
| 21456 | cdef: &CdefParams, |
| 21457 | loop_filter: &LoopFilterParams, |
| 21458 | lr: &LoopRestorationParams, |
| 21459 | initial_cdfs: Option<Cdfs>, |
| 21460 | allow_high_precision_mv: bool, |
| 21461 | force_integer_mv: bool, |
| 21462 | sign_bias_table: SignBiasTable, |
| 21463 | // lane-gm r2: this frame header's own `global_motion` table (spec |
| 21464 | // 5.9.24), threaded to every `decode_inter_block` call below. |
| 21465 | global_motion: [ec_av1_syntax::WarpParams; 7], |
| 21466 | interp_fixed: Option<mc::InterpFilterKind>, |
| 21467 | enable_dual_filter: bool, |
| 21468 | // lane-av1tmvp: `order_hint`/`order_hint_bits`/`ref_order_hints` are |
| 21469 | // always needed (this frame's own saved [`crate::motion_field::MotionField`] |
| 21470 | // carries them for a *later* frame's projection, spec 7.9); `tpl_field` |
| 21471 | // is `Some` only when this frame's own header set `use_ref_frame_mvs` |
| 21472 | // and the caller ([`crate::stream::decode_stream`]) projected one. |
| 21473 | order_hint_bits: u32, |
| 21474 | order_hint: u32, |
| 21475 | ref_order_hints: [u32; 7], |
| 21476 | tpl_field: Option<&crate::motion_field::TplField>, |
| 21477 | // lane-av1comp: this frame header's own `reference_select` bit, threaded |
| 21478 | // to every `decode_inter_block`/`decode_inter_block8` call below. |
| 21479 | reference_select: bool, |
| 21480 | // lane-av1comp: `seq_params`' own compound-blend enable bits. |
| 21481 | enable_masked_compound: bool, |
| 21482 | enable_jnt_comp: bool, |
| 21483 | // lane-sb128 r3: this sequence header's own `enable_interintra_compound` |
| 21484 | // bit, threaded to every `decode_inter_block`/`decode_inter_block8` call |
| 21485 | // below (spec 5.11.24's `interintra` read). |
| 21486 | enable_interintra_compound: bool, |
| 21487 | // lane-av1comp round 14: this frame header's own `skip_mode_present`/ |
| 21488 | // `skip_mode_frame` (spec 5.9.22), threaded to every `decode_inter_block`/ |
| 21489 | // `decode_inter_block8` call below. |
| 21490 | skip_mode_present: bool, |
| 21491 | skip_mode_frame: [u8; 2], |
| 21492 | // lane-cdffwd2: this frame header's own `reduced_tx_set` bit, threaded to |
| 21493 | // every inter `tx_type` read below (`false` needs the 12-/16-symbol |
| 21494 | // `Set1` coefficient tables at 16x16/8x8 rather than the reduced |
| 21495 | // 2-symbol ones -- see [`TxbSet::Luma16InterSet1`]/[`TxbSet::Luma8InterSet1`]). |
| 21496 | reduced_tx_set: bool, |
| 21497 | // lane-txselect: this frame header's own `tx_mode == TxMode::Select` |
| 21498 | // bit (spec 5.9.14). Together with `reduced_tx_set` it is stashed in a |
| 21499 | // thread-local by [`set_inter_tx_mode`] rather than threaded through |
| 21500 | // `decode_inter_block`'s forty parameters -- both are frame-constant, and |
| 21501 | // only [`read_block_tx_size`]/the var-tx residual loop read them. |
| 21502 | tx_select: bool, |
| 21503 | // lane-motionmode round 1/3: this frame header's own `is_motion_mode_switchable`/ |
| 21504 | // `allow_warped_motion` bits, threaded to every `decode_inter_block`/ |
| 21505 | // `decode_inter_block8` call below (spec 5.11.24's `read_motion_mode`). |
| 21506 | switchable_motion_mode: bool, |
| 21507 | allow_warped_motion: bool, |
| 21508 | // lane-screen r2: this frame header's own `allow_screen_content_tools` |
| 21509 | // bit, threaded to every `decode_inter_block`/`decode_inter_block8` |
| 21510 | // call below -- consumes (never reconstructs) the intra-sub-block |
| 21511 | // palette syntax libaom's `read_intra_block_mode_info` reads under the |
| 21512 | // same `av1_allow_palette` gate as the key-frame path. |
| 21513 | allow_screen_content_tools: bool, |
| 21514 | // lane-realworld r5: this frame header's own `delta` (spec 5.9.17/5.9.18), |
| 21515 | // same contract as [`decode_key_frame_tile_with_cdfs`]'s own `delta` |
| 21516 | // param -- only `q_present`/`q_res` are read. |
| 21517 | delta: DeltaParams, |
| 21518 | // lane-fiinter r1: this sequence header's own `enable_filter_intra` bit |
| 21519 | // (spec 5.5.2), stashed in a thread-local with `tx_select` rather than |
| 21520 | // threaded through `decode_inter_block`'s fifty parameters -- read by the |
| 21521 | // intra-in-inter arms' `read_filter_intra_mode_info`. |
| 21522 | enable_filter_intra: bool, |
| 21523 | ) -> Result<(Picture, Cdfs, crate::motion_field::MotionField)> { |
| 21524 | set_inter_tx_mode(tx_select, reduced_tx_set, enable_filter_intra); |
| 21525 | let tpl_frame = tpl_field.map(|field| TplFrameArgs { |
| 21526 | field, |
| 21527 | order_hint_bits, |
| 21528 | order_hint, |
| 21529 | ref_order_hints, |
| 21530 | }); |
| 21531 | if mi_cols == 0 || mi_rows == 0 { |
| 21532 | return Err(unsupported("a frame with no mode-info grid")); |
| 21533 | } |
| 21534 | let (true_width, true_height) = ((mi_cols * 4) as usize, (mi_rows * 4) as usize); |
| 21535 | // lane-superres r10: a width mismatch alone is no longer refused here -- |
| 21536 | // `mc::predict_scaled` (spec 7.11.3.3, threaded through |
| 21537 | // `decode_inter_block`'s single-ref branch) exists precisely for a |
| 21538 | // `use_superres` reference at a different width than this frame's own |
| 21539 | // true size. Height must still match: AV1 superres never scales height |
| 21540 | // (r8's derivation, `mc.rs`'s own doc), and nothing in this decode path |
| 21541 | // has a vertical scaling pass. |
| 21542 | // lane-golomb r8: the comparison is against the header's own frame |
| 21543 | // height, NOT the mi-rounded `true_height`. libaom compares references |
| 21544 | // on the CROPPED dimensions -- `av1_setup_scale_factors_for_frame(&sf, |
| 21545 | // ref->y_crop_width, ref->y_crop_height, cm->width, cm->height)` |
| 21546 | // (`av1/common/reconinter.c`), and a reference is "scaled" only when |
| 21547 | // those differ (`av1_is_scaled`). A decoded `Picture` is cropped to |
| 21548 | // `fw`/`fh`, so for any height that is not a multiple of 8 (68 -> |
| 21549 | // mi_rows 18 -> `true_height` 72) this test compared 68 against 72 and |
| 21550 | // refused every same-size reference (class av1-truesize-lane: true |
| 21551 | // frame size vs mi-rounded). Height must still MATCH: AV1 superres |
| 21552 | // never scales height, and no path here has a vertical scaling pass. |
| 21553 | if reference.height != frame_height as usize { |
| 21554 | return Err(unsupported( |
| 21555 | "a reference picture whose height does not match this frame's own true size", |
| 21556 | )); |
| 21557 | } |
| 21558 | let (cols, rows) = block_grid(mi_cols, mi_rows); |
| 21559 | let sb128 = sb128(); |
| 21560 | let (sb_cols, sb_rows) = (cols.div_ceil(2), rows.div_ceil(2)); |
| 21561 | CDEF_BITS.with(|c| c.set(cdef.bits)); |
| 21562 | CDEF_SB_COLS.with(|c| c.set(sb_cols as usize)); |
| 21563 | CDEF_IDX_GRID.with(|g| *g.borrow_mut() = vec![0u8; sb_cols as usize * sb_rows as usize]); |
| 21564 | let (width, height) = (cols as usize * BLOCK, rows as usize * BLOCK); |
| 21565 | |
| 21566 | let ref_y = PlaneBuf { |
| 21567 | data: reference.y.iter().map(|&s| s as u16).collect(), |
| 21568 | width: reference.width, |
| 21569 | height: reference.height, |
| 21570 | true_width: reference.width, |
| 21571 | true_height: reference.height, |
| 21572 | tile_x0: 0, |
| 21573 | tile_y0: 0, |
| 21574 | tile_x1: reference.width, |
| 21575 | tile_y1: reference.height, |
| 21576 | }; |
| 21577 | let ref_u = PlaneBuf { |
| 21578 | data: reference.u.iter().map(|&s| s as u16).collect(), |
| 21579 | width: reference.width / 2, |
| 21580 | height: reference.height / 2, |
| 21581 | true_width: reference.width / 2, |
| 21582 | true_height: reference.height / 2, |
| 21583 | tile_x0: 0, |
| 21584 | tile_y0: 0, |
| 21585 | tile_x1: reference.width / 2, |
| 21586 | tile_y1: reference.height / 2, |
| 21587 | }; |
| 21588 | let ref_v = PlaneBuf { |
| 21589 | data: reference.v.iter().map(|&s| s as u16).collect(), |
| 21590 | width: reference.width / 2, |
| 21591 | height: reference.height / 2, |
| 21592 | true_width: reference.width / 2, |
| 21593 | true_height: reference.height / 2, |
| 21594 | tile_x0: 0, |
| 21595 | tile_y0: 0, |
| 21596 | tile_x1: reference.width / 2, |
| 21597 | tile_y1: reference.height / 2, |
| 21598 | }; |
| 21599 | // Every non-`LAST_FRAME` reference this frame header's own `ref_frame_idx` |
| 21600 | // names a live DPB slot for (lane-av1refs: generalised from the old |
| 21601 | // GOLDEN_FRAME-only block -- same empty-slot/size-mismatch refusal, |
| 21602 | // now per-reference). `decode_inter_block` refuses by name when the |
| 21603 | // block it is decoding selects an index that is still `None` here |
| 21604 | // (spec 7.9 requires every *active* reference be a compatible size; |
| 21605 | // an inactive one this frame never selects is simply left `None`). |
| 21606 | let owned_refs: [Option<(PlaneBuf, PlaneBuf, PlaneBuf)>; 8] = std::array::from_fn(|i| { |
| 21607 | other_refs[i] |
| 21608 | .filter(|g| g.width == reference.width && g.height == reference.height) |
| 21609 | .map(|g| { |
| 21610 | ( |
| 21611 | PlaneBuf { |
| 21612 | data: g.y.iter().map(|&s| s as u16).collect(), |
| 21613 | width: g.width, |
| 21614 | height: g.height, |
| 21615 | true_width: g.width, |
| 21616 | true_height: g.height, |
| 21617 | tile_x0: 0, |
| 21618 | tile_y0: 0, |
| 21619 | tile_x1: g.width, |
| 21620 | tile_y1: g.height, |
| 21621 | }, |
| 21622 | PlaneBuf { |
| 21623 | data: g.u.iter().map(|&s| s as u16).collect(), |
| 21624 | width: g.width / 2, |
| 21625 | height: g.height / 2, |
| 21626 | true_width: g.width / 2, |
| 21627 | true_height: g.height / 2, |
| 21628 | tile_x0: 0, |
| 21629 | tile_y0: 0, |
| 21630 | tile_x1: g.width / 2, |
| 21631 | tile_y1: g.height / 2, |
| 21632 | }, |
| 21633 | PlaneBuf { |
| 21634 | data: g.v.iter().map(|&s| s as u16).collect(), |
| 21635 | width: g.width / 2, |
| 21636 | height: g.height / 2, |
| 21637 | true_width: g.width / 2, |
| 21638 | true_height: g.height / 2, |
| 21639 | tile_x0: 0, |
| 21640 | tile_y0: 0, |
| 21641 | tile_x1: g.width / 2, |
| 21642 | tile_y1: g.height / 2, |
| 21643 | }, |
| 21644 | ) |
| 21645 | }) |
| 21646 | }); |
| 21647 | let ref_slots: RefSlots = |
| 21648 | std::array::from_fn(|i| owned_refs[i].as_ref().map(|(a, b, c)| (a, b, c))); |
| 21649 | |
| 21650 | let mut y = PlaneBuf { |
| 21651 | data: vec![0u16; width * height], |
| 21652 | width, |
| 21653 | height, |
| 21654 | true_width, |
| 21655 | true_height, |
| 21656 | tile_x0: 0, |
| 21657 | tile_y0: 0, |
| 21658 | tile_x1: width, |
| 21659 | tile_y1: height, |
| 21660 | }; |
| 21661 | let mut u = PlaneBuf { |
| 21662 | data: vec![0u16; width * height / 4], |
| 21663 | width: width / 2, |
| 21664 | height: height / 2, |
| 21665 | true_width: true_width / 2, |
| 21666 | true_height: true_height / 2, |
| 21667 | tile_x0: 0, |
| 21668 | tile_y0: 0, |
| 21669 | tile_x1: width / 2, |
| 21670 | tile_y1: height / 2, |
| 21671 | }; |
| 21672 | let mut v = PlaneBuf { |
| 21673 | data: vec![0u16; width * height / 4], |
| 21674 | width: width / 2, |
| 21675 | height: height / 2, |
| 21676 | true_width: true_width / 2, |
| 21677 | true_height: true_height / 2, |
| 21678 | tile_x0: 0, |
| 21679 | tile_y0: 0, |
| 21680 | tile_x1: width / 2, |
| 21681 | tile_y1: height / 2, |
| 21682 | }; |
| 21683 | |
| 21684 | let scan32 = default_scan(TX32); |
| 21685 | let scan16 = default_scan(TX16); |
| 21686 | let scan8 = default_scan(TX8); |
| 21687 | let scan4 = default_scan(TX4); |
| 21688 | |
| 21689 | let base_cdfs = initial_cdfs.unwrap_or_else(|| Cdfs::new(q_ctx_of(base_q_idx))); |
| 21690 | let mut result_cdfs = base_cdfs.clone(); |
| 21691 | // spec `decode_tile`: `CurrentQIndex`/`DeltaLF` reset to the frame's own |
| 21692 | // `base_q_idx`/zero at the top of every tile, not once per frame (mirrors |
| 21693 | // `decode_key_frame_tile_with_cdfs`'s per-tile reset below); the |
| 21694 | // delta_q/delta_lf presence/resolution flags themselves are frame-level |
| 21695 | // and set once here. |
| 21696 | DELTA_Q_PRESENT.with(|c| c.set(delta.q_present)); |
| 21697 | DELTA_Q_RES.with(|c| c.set(1i32 << delta.q_res)); |
| 21698 | DELTA_LF_PRESENT.with(|c| c.set(delta.lf_present)); |
| 21699 | DELTA_LF_RES.with(|c| c.set(1i32 << delta.lf_res)); |
| 21700 | DELTA_LF_MULTI.with(|c| c.set(delta.lf_multi)); |
| 21701 | let mut neighbours = Neighbours::new( |
| 21702 | cols as usize * 2, |
| 21703 | rows as usize * 2, |
| 21704 | mi_cols as usize, |
| 21705 | mi_rows as usize, |
| 21706 | ); |
| 21707 | let mut grid = MiGrid::new(mi_cols as usize, mi_rows as usize); |
| 21708 | let mut lr_grid = crate::restoration::RestorationGrid::new(lr, frame_width, frame_height); |
| 21709 | let mut lr_reference = [( |
| 21710 | crate::restoration::WienerInfo::default(), |
| 21711 | crate::restoration::SgrprojInfo::default(), |
| 21712 | ); 3]; |
| 21713 | |
| 21714 | // lane-tiles r6: mirrors decode_key_frame_tile_with_cdfs's own per-tile |
| 21715 | // loop (spec 5.11.2 decode_tile / 7.20 exit_symbol) -- each tile gets |
| 21716 | // its own fresh SymbolDecoder over its own byte range and its own fresh |
| 21717 | // copy of the frame's initial CDFs; only context_update_tile_id's own |
| 21718 | // end-of-tile adapted table becomes the frame's own output. |
| 21719 | for (tile_idx, &data) in tiles.iter().enumerate() { |
| 21720 | let tile_num = tile_idx as u32; |
| 21721 | let (trow, tcol) = (tile_num / tile_info.cols, tile_num % tile_info.cols); |
| 21722 | let mi_row0 = tile_info.mi_row_starts[trow as usize]; |
| 21723 | let mi_row1 = tile_info.mi_row_starts[trow as usize + 1]; |
| 21724 | let mi_col0 = tile_info.mi_col_starts[tcol as usize]; |
| 21725 | let mi_col1 = tile_info.mi_col_starts[tcol as usize + 1]; |
| 21726 | let (sb_r0, sb_r1) = ( |
| 21727 | (mi_row0 / SB_MI).min(sb_rows), |
| 21728 | mi_row1.div_ceil(SB_MI).min(sb_rows), |
| 21729 | ); |
| 21730 | let (sb_c0, sb_c1) = ( |
| 21731 | (mi_col0 / SB_MI).min(sb_cols), |
| 21732 | mi_col1.div_ceil(SB_MI).min(sb_cols), |
| 21733 | ); |
| 21734 | let mut cdfs = base_cdfs.clone(); |
| 21735 | CURRENT_Q_IDX.with(|c| c.set(i32::from(base_q_idx))); |
| 21736 | QUANT_DELTAS.with(|c| c.set(deltas)); |
| 21737 | CURRENT_DELTA_LF.with(|c| c.set([0; 4])); |
| 21738 | let mut dec = SymbolDecoder::new(data); |
| 21739 | // lane-comppin r9: tile-entry range, the earliest point comparable |
| 21740 | // against aomdec's own `r->ec.rng` right after `aom_reader_init` -- the |
| 21741 | // first ladder rung before any symbol (partition or otherwise) is read. |
| 21742 | if std::env::var_os("EC_AV1_TELL").is_some() { |
| 21743 | eprintln!( |
| 21744 | "TELL label=tile_init tell={} range={}", |
| 21745 | dec.debug_bitpos(), |
| 21746 | dec.debug_state().0 |
| 21747 | ); |
| 21748 | } |
| 21749 | neighbours.start_tile(mi_row0 as usize, mi_col0 as usize, mi_col1 as usize); |
| 21750 | y.set_tile_origin( |
| 21751 | mi_col0 as usize * 4, |
| 21752 | mi_row0 as usize * 4, |
| 21753 | (mi_col1 as usize * 4).min(y.width), |
| 21754 | (mi_row1 as usize * 4).min(y.height), |
| 21755 | ); |
| 21756 | u.set_tile_origin( |
| 21757 | mi_col0 as usize * 2, |
| 21758 | mi_row0 as usize * 2, |
| 21759 | (mi_col1 as usize * 2).min(u.width), |
| 21760 | (mi_row1 as usize * 2).min(u.height), |
| 21761 | ); |
| 21762 | v.set_tile_origin( |
| 21763 | mi_col0 as usize * 2, |
| 21764 | mi_row0 as usize * 2, |
| 21765 | (mi_col1 as usize * 2).min(v.width), |
| 21766 | (mi_row1 as usize * 2).min(v.height), |
| 21767 | ); |
| 21768 | // lane-tiles r6: an MV candidate scan (mvstack.rs's grid.get) must |
| 21769 | // not reach across a tile boundary any more than intra prediction's |
| 21770 | // PlaneBuf reach does -- bounding the shared grid's own read window |
| 21771 | // per tile is equivalent to threading tile bounds through every |
| 21772 | // find_mv_stack* call site (all four read through this one grid). |
| 21773 | grid.set_tile_bounds( |
| 21774 | mi_row0 as usize, |
| 21775 | mi_col0 as usize, |
| 21776 | mi_row1 as usize, |
| 21777 | mi_col1 as usize, |
| 21778 | ); |
| 21779 | TILE_HITS.with(|c| c.set(c.get() + 1)); |
| 21780 | |
| 21781 | // lane-sb128 r2: at `sb_size == 128` the tile walks each 128x128 |
| 21782 | // superblock's four 64x64 quadrants in libaom `decode_partition` order |
| 21783 | // (TL, TR, BL, BR) and reads the 128 root's own loop-restoration units and |
| 21784 | // partition symbol once per superblock -- exactly what the key-frame tile |
| 21785 | // loop does; the 64-level body below is unchanged, `read_cdef`'s |
| 21786 | // per-64x64-quadrant reset (`CDEF_TRANSMITTED`) already matches libaom's |
| 21787 | // `cdef_idx[(mi_row & 16) >> 4 * 2 + (mi_col & 16) >> 4]` slot per quadrant. |
| 21788 | for (sb_r, sb_c, row_start, sb_start) in sb_visit_order(sb_r0, sb_r1, sb_c0, sb_c1, sb128) { |
| 21789 | if row_start { |
| 21790 | neighbours.start_row(); |
| 21791 | } |
| 21792 | { |
| 21793 | if sb128 && sb_start { |
| 21794 | if read_sb128_root( |
| 21795 | &mut dec, |
| 21796 | &mut cdfs, |
| 21797 | &neighbours, |
| 21798 | lr, |
| 21799 | &mut lr_grid, |
| 21800 | &mut lr_reference, |
| 21801 | sb_r, |
| 21802 | sb_c, |
| 21803 | mi_cols, |
| 21804 | mi_rows, |
| 21805 | )? != PARTITION_SPLIT |
| 21806 | { |
| 21807 | // lane-sb128b r1 decodes a `PARTITION_NONE` 128 root on |
| 21808 | // KEY frames only; an inter 128x128 leaf needs whole-block |
| 21809 | // MC, OBMC and compound at a size this path has never run. |
| 21810 | return Err(unsupported( |
| 21811 | "a 128x128 superblock PARTITION_NONE root on an inter frame (only the key-frame path codes a whole 128x128 block)", |
| 21812 | )); |
| 21813 | } |
| 21814 | } |
| 21815 | if !sb128 { |
| 21816 | crate::restoration::read_lr( |
| 21817 | &mut dec, |
| 21818 | &mut cdfs, |
| 21819 | lr, |
| 21820 | &mut lr_grid, |
| 21821 | &mut lr_reference, |
| 21822 | sb_r * SB_MI, |
| 21823 | sb_c * SB_MI, |
| 21824 | SB_MI, |
| 21825 | ); |
| 21826 | } |
| 21827 | CDEF_TRANSMITTED.with(|c| c.set(false)); |
| 21828 | let sb_at = (sb_r as usize * 4, sb_c as usize * 4); |
| 21829 | // lane-inter4 r1: every rect inter piece below is the same |
| 21830 | // `decode_inter_block` call with only position / true footprint / |
| 21831 | // transform-set differing; the macro keeps the new arms readable |
| 21832 | // instead of six more 60-line copies of the argument list. |
| 21833 | macro_rules! inter_piece { |
| 21834 | ($at_mi:expr, $side:expr, $li:expr, $lt:expr, $ch:expr, $ltx:expr, |
| 21835 | $ctx:expr, $sl:expr, $sc:expr, $sg:expr, $ww:expr, $wh:expr) => { |
| 21836 | decode_inter_block( |
| 21837 | &mut dec, |
| 21838 | &mut cdfs, |
| 21839 | &mut neighbours, |
| 21840 | &mut grid, |
| 21841 | $at_mi, |
| 21842 | $side, |
| 21843 | mi_cols, |
| 21844 | mi_rows, |
| 21845 | &mut y, |
| 21846 | &mut u, |
| 21847 | &mut v, |
| 21848 | &ref_y, |
| 21849 | &ref_u, |
| 21850 | &ref_v, |
| 21851 | &ref_slots, |
| 21852 | &sign_bias_table, |
| 21853 | &global_motion, |
| 21854 | base_q_idx, |
| 21855 | $li, |
| 21856 | $lt, |
| 21857 | $ch, |
| 21858 | $ltx, |
| 21859 | $ctx, |
| 21860 | $sl, |
| 21861 | $sc, |
| 21862 | $sg, |
| 21863 | allow_high_precision_mv, |
| 21864 | force_integer_mv, |
| 21865 | interp_fixed, |
| 21866 | enable_dual_filter, |
| 21867 | tpl_frame.as_ref(), |
| 21868 | reference_select, |
| 21869 | enable_masked_compound, |
| 21870 | enable_interintra_compound, |
| 21871 | enable_jnt_comp, |
| 21872 | order_hint_bits, |
| 21873 | order_hint, |
| 21874 | ref_order_hints, |
| 21875 | skip_mode_present, |
| 21876 | skip_mode_frame, |
| 21877 | switchable_motion_mode, |
| 21878 | allow_warped_motion, |
| 21879 | true, |
| 21880 | $ww, |
| 21881 | $wh, |
| 21882 | allow_screen_content_tools, |
| 21883 | frame_width as usize, |
| 21884 | )? |
| 21885 | }; |
| 21886 | } |
| 21887 | // lane-inter16ab r1: one 8x8 inter leaf of a 16x16-level AB |
| 21888 | // partition (libaom `decode_partition` PARTITION_HORZ_A/_B/ |
| 21889 | // VERT_A/_B at BLOCK_16X16 code `bsize2` = BLOCK_8X8 for two of |
| 21890 | // their three pieces, decodeframe.c ~1900). Same body the |
| 21891 | // PARTITION_SPLIT arm runs per leaf, minus its own partition |
| 21892 | // symbol (an AB piece carries none) and its `prev_leaves` |
| 21893 | // bookkeeping. |
| 21894 | macro_rules! inter_leaf8 { |
| 21895 | ($outer:expr, $leaf:expr) => {{ |
| 21896 | let leaf_mi = $leaf; |
| 21897 | let ( |
| 21898 | skip, |
| 21899 | is_inter, |
| 21900 | skip_mode_leaf, |
| 21901 | compound_ctx8, |
| 21902 | leaf_filter, |
| 21903 | leaf_refs, |
| 21904 | ) = decode_inter_block8( |
| 21905 | &mut dec, |
| 21906 | &mut cdfs, |
| 21907 | &mut neighbours, |
| 21908 | &mut grid, |
| 21909 | mi_cols, |
| 21910 | mi_rows, |
| 21911 | $outer, |
| 21912 | leaf_mi, |
| 21913 | &mut y, |
| 21914 | &mut u, |
| 21915 | &mut v, |
| 21916 | &ref_y, |
| 21917 | &ref_u, |
| 21918 | &ref_v, |
| 21919 | &ref_slots, |
| 21920 | base_q_idx, |
| 21921 | &scan8, |
| 21922 | &scan4, |
| 21923 | allow_high_precision_mv, |
| 21924 | force_integer_mv, |
| 21925 | &global_motion, |
| 21926 | enable_dual_filter, |
| 21927 | reference_select, |
| 21928 | enable_masked_compound, |
| 21929 | enable_interintra_compound, |
| 21930 | enable_jnt_comp, |
| 21931 | order_hint_bits, |
| 21932 | order_hint, |
| 21933 | ref_order_hints, |
| 21934 | skip_mode_present, |
| 21935 | skip_mode_frame, |
| 21936 | reduced_tx_set, |
| 21937 | interp_fixed, |
| 21938 | switchable_motion_mode, |
| 21939 | allow_warped_motion, |
| 21940 | allow_screen_content_tools, |
| 21941 | &sign_bias_table, |
| 21942 | tpl_frame.as_ref(), |
| 21943 | frame_width as usize, |
| 21944 | )?; |
| 21945 | neighbours.record_inter_rect_mi( |
| 21946 | leaf_mi, |
| 21947 | 2, |
| 21948 | 2, |
| 21949 | skip, |
| 21950 | is_inter, |
| 21951 | leaf_refs.0, |
| 21952 | leaf_filter, |
| 21953 | skip_mode_leaf, |
| 21954 | ); |
| 21955 | if let Some(ref1) = leaf_refs.1 |
| 21956 | && let Some((_, _, group_idx, idx)) = compound_ctx8 |
| 21957 | { |
| 21958 | neighbours.record_compound_ctx_rect_mi( |
| 21959 | leaf_mi, 2, 2, ref1, group_idx, idx, |
| 21960 | ); |
| 21961 | } |
| 21962 | }}; |
| 21963 | } |
| 21964 | let sb_ctx = neighbours.partition_ctx(sb_at, SB); |
| 21965 | let (has_cols, has_rows) = ( |
| 21966 | sb_c * SB_MI + SB_MI / 2 < mi_cols, |
| 21967 | sb_r * SB_MI + SB_MI / 2 < mi_rows, |
| 21968 | ); |
| 21969 | // The straddle cases (one or both halves outside the true |
| 21970 | // frame) never carry a real alphabet symbol -- spec forces |
| 21971 | // SPLIT there (mirrors the intra key-frame tile's own |
| 21972 | // three-way write above). Only the (true, true) case can name |
| 21973 | // a non-SPLIT value, and this loop below only ever recurses as |
| 21974 | // SPLIT -- so a real non-SPLIT part64 here must refuse by name |
| 21975 | // instead of silently decoding as SPLIT and desyncing. |
| 21976 | let part64 = match (has_cols, has_rows) { |
| 21977 | (true, true) => { |
| 21978 | let pre_rng = dec.debug_state().0; |
| 21979 | let p = dec.symbol(&mut cdfs.partition_w64[sb_ctx]); |
| 21980 | if std::env::var_os("EC_AV1_TRACE").is_some() { |
| 21981 | eprintln!( |
| 21982 | "TRACE partition_w64 mi=({},{}) ctx={sb_ctx} value={p} pre_rng={pre_rng}", |
| 21983 | sb_r * SB_MI, |
| 21984 | sb_c * SB_MI |
| 21985 | ); |
| 21986 | } |
| 21987 | p |
| 21988 | } |
| 21989 | (true, false) => { |
| 21990 | // lane-golomb r1 (class `parsed-then-discarded`): the gathered |
| 21991 | // bit IS the partition -- 0 means PARTITION_HORZ, only 1 means SPLIT |
| 21992 | // (libaom `ec_read_partition_impl`). |
| 21993 | if bump_edge32_bit( |
| 21994 | 4, |
| 21995 | dec.symbol_fixed(&gather(&cdfs.partition_w64[sb_ctx], VERT_ALIKE)), |
| 21996 | ) == 1 { |
| 21997 | PARTITION_SPLIT |
| 21998 | } else { |
| 21999 | PARTITION_HORZ |
| 22000 | } |
| 22001 | } |
| 22002 | (false, true) => { |
| 22003 | // lane-golomb r1 (class `parsed-then-discarded`): the gathered |
| 22004 | // bit IS the partition -- 0 means PARTITION_VERT, only 1 means SPLIT |
| 22005 | // (libaom `ec_read_partition_impl`). |
| 22006 | if bump_edge32_bit( |
| 22007 | 4, |
| 22008 | dec.symbol_fixed(&gather(&cdfs.partition_w64[sb_ctx], HORZ_ALIKE)), |
| 22009 | ) == 1 { |
| 22010 | PARTITION_SPLIT |
| 22011 | } else { |
| 22012 | PARTITION_VERT |
| 22013 | } |
| 22014 | } |
| 22015 | (false, false) => PARTITION_SPLIT, |
| 22016 | }; |
| 22017 | if part64 == PARTITION_NONE { |
| 22018 | // lane-inter8 r1: the whole superblock as one 64x64 inter |
| 22019 | // block. `SB`-sized syntax throughout (size_group 3, same as |
| 22020 | // 32x32 -- `size_group_lookup[BLOCK_64X64] == 3`), luma |
| 22021 | // coefficients through `TxbSet::Luma64` with the 32-point |
| 22022 | // scan (TX_64X64 codes only its top-left corner, spec |
| 22023 | // 5.11.40 -- `read_inter_plane` widens the corner back to |
| 22024 | // 64x64 before the inverse transform), chroma as a plain |
| 22025 | // 32x32 `TxbSet::Chroma32`. TX_64X64 carries no `tx_type` |
| 22026 | // symbol at all, so the intra/inter luma sets coincide. |
| 22027 | INTER_SB_NONE_HITS.with(|c| c.set(c.get() + 1)); |
| 22028 | decode_inter_block( |
| 22029 | &mut dec, |
| 22030 | &mut cdfs, |
| 22031 | &mut neighbours, |
| 22032 | &mut grid, |
| 22033 | sub16_to_mi(sb_at), |
| 22034 | SB, |
| 22035 | mi_cols, |
| 22036 | mi_rows, |
| 22037 | &mut y, |
| 22038 | &mut u, |
| 22039 | &mut v, |
| 22040 | &ref_y, |
| 22041 | &ref_u, |
| 22042 | &ref_v, |
| 22043 | &ref_slots, |
| 22044 | &sign_bias_table, |
| 22045 | &global_motion, |
| 22046 | base_q_idx, |
| 22047 | TxbSet::Luma64, |
| 22048 | TxbSet::Luma64, |
| 22049 | TxbSet::Chroma32, |
| 22050 | TX32, |
| 22051 | TX32, |
| 22052 | &scan32, |
| 22053 | &scan32, |
| 22054 | 3, |
| 22055 | allow_high_precision_mv, |
| 22056 | force_integer_mv, |
| 22057 | interp_fixed, |
| 22058 | enable_dual_filter, |
| 22059 | tpl_frame.as_ref(), |
| 22060 | reference_select, |
| 22061 | enable_masked_compound, |
| 22062 | enable_interintra_compound, |
| 22063 | enable_jnt_comp, |
| 22064 | order_hint_bits, |
| 22065 | order_hint, |
| 22066 | ref_order_hints, |
| 22067 | skip_mode_present, |
| 22068 | skip_mode_frame, |
| 22069 | switchable_motion_mode, |
| 22070 | allow_warped_motion, |
| 22071 | false, |
| 22072 | SB, |
| 22073 | SB, |
| 22074 | allow_screen_content_tools, |
| 22075 | frame_width as usize, |
| 22076 | )?; |
| 22077 | continue; |
| 22078 | } |
| 22079 | if matches!( |
| 22080 | part64, |
| 22081 | PARTITION_HORZ | PARTITION_VERT | PARTITION_HORZ_4 | PARTITION_VERT_4 |
| 22082 | ) { |
| 22083 | // lane-inter4 r1: the superblock-level rect + 1:4 pairs as |
| 22084 | // inter blocks -- two 64x32 / 32x64 strips, or four 64x16 / |
| 22085 | // 16x64 ones. Each piece keeps `side = SB` for every |
| 22086 | // syntax/CDF/prediction-buffer decision (the square-context |
| 22087 | // corner-cut PARTITION_HORZ already ships at the 32 level) |
| 22088 | // but names its own true footprint through `write_w`/ |
| 22089 | // `write_h`, which is what `motion_mode`'s bsize row, the |
| 22090 | // mv-stack's bw4/bh4, the delta-q `bSize == sbSize` test and |
| 22091 | // every neighbour stamp read. `reject_residual = true`: a |
| 22092 | // non-skip strip refuses by name rather than over-read a |
| 22093 | // square residual (rectangular inter residual coding is not |
| 22094 | // ported -- see lanes/inter4-r1.report.md). |
| 22095 | let horz = matches!(part64, PARTITION_HORZ | PARTITION_HORZ_4); |
| 22096 | let four = matches!(part64, PARTITION_HORZ_4 | PARTITION_VERT_4); |
| 22097 | let base_mi = ( |
| 22098 | sb_r as usize * SB_MI as usize, |
| 22099 | sb_c as usize * SB_MI as usize, |
| 22100 | ); |
| 22101 | let (n_pieces, step_mi) = if four { (4usize, 4usize) } else { (2usize, 8usize) }; |
| 22102 | let (bw, bh) = match (horz, four) { |
| 22103 | (true, false) => (64usize, 32usize), |
| 22104 | (false, false) => (32, 64), |
| 22105 | (true, true) => (64, 16), |
| 22106 | (false, true) => (16, 64), |
| 22107 | }; |
| 22108 | for i in 0..n_pieces { |
| 22109 | let at_mi = if horz { |
| 22110 | (base_mi.0 + i * step_mi, base_mi.1) |
| 22111 | } else { |
| 22112 | (base_mi.0, base_mi.1 + i * step_mi) |
| 22113 | }; |
| 22114 | // libaom `decode_partition`'s own `i > 0` frame-edge break. |
| 22115 | if i > 0 && (at_mi.0 >= mi_rows as usize || at_mi.1 >= mi_cols as usize) { |
| 22116 | break; |
| 22117 | } |
| 22118 | inter_piece!( |
| 22119 | at_mi, |
| 22120 | SB, |
| 22121 | TxbSet::Luma64, |
| 22122 | TxbSet::Luma64, |
| 22123 | TxbSet::Chroma32, |
| 22124 | TX32, |
| 22125 | TX32, |
| 22126 | &scan32, |
| 22127 | &scan32, |
| 22128 | 3, |
| 22129 | bw, |
| 22130 | bh |
| 22131 | ); |
| 22132 | if !four && i == 0 && ((horz && !has_rows) || (!horz && !has_cols)) { |
| 22133 | bump_inter_edge_strip(if horz { 0 } else { 1 }); |
| 22134 | } |
| 22135 | match (horz, four) { |
| 22136 | (true, false) => INTER_SB_HORZ_HITS.with(|c| c.set(c.get() + 1)), |
| 22137 | (false, false) => INTER_SB_VERT_HITS.with(|c| c.set(c.get() + 1)), |
| 22138 | (true, true) => INTER_SB_HORZ4_HITS.with(|c| c.set(c.get() + 1)), |
| 22139 | (false, true) => INTER_SB_VERT4_HITS.with(|c| c.set(c.get() + 1)), |
| 22140 | } |
| 22141 | } |
| 22142 | continue; |
| 22143 | } |
| 22144 | if part64 != PARTITION_SPLIT { |
| 22145 | return Err(unsupported( |
| 22146 | "an inter SB-level AB partition (HORZ_A/HORZ_B/VERT_A/VERT_B; this \ |
| 22147 | decoder's inter tile path codes a superblock as NONE, SPLIT, HORZ, \ |
| 22148 | VERT, HORZ_4 or VERT_4)", |
| 22149 | )); |
| 22150 | } |
| 22151 | |
| 22152 | for quadrant in 0..4 { |
| 22153 | let (r32, c32) = (sb_r * 2 + quadrant / 2, sb_c * 2 + quadrant % 2); |
| 22154 | if r32 >= rows || c32 >= cols { |
| 22155 | continue; |
| 22156 | } |
| 22157 | let at = (r32 as usize * 2, c32 as usize * 2); |
| 22158 | let ctx32 = neighbours.partition_ctx(at, BLOCK); |
| 22159 | let (has_cols32, has_rows32) = ( |
| 22160 | has_half(c32 * BLOCK_MI, BLOCK_MI, mi_cols), |
| 22161 | has_half(r32 * BLOCK_MI, BLOCK_MI, mi_rows), |
| 22162 | ); |
| 22163 | let part32 = if has_cols32 && has_rows32 { |
| 22164 | if std::env::var_os("EC_AV1_TRACE").is_some() { |
| 22165 | eprintln!( |
| 22166 | "TRACE part32_pre mi=({},{}) rng={}", |
| 22167 | r32 * BLOCK_MI, |
| 22168 | c32 * BLOCK_MI, |
| 22169 | dec.debug_state().0 |
| 22170 | ); |
| 22171 | } |
| 22172 | let p = dec.symbol(&mut cdfs.partition_w32[ctx32]); |
| 22173 | if std::env::var_os("EC_AV1_TRACE").is_some() { |
| 22174 | eprintln!( |
| 22175 | "TRACE partition_w32 mi=({},{}) ctx={ctx32} value={p}", |
| 22176 | r32 * BLOCK_MI, |
| 22177 | c32 * BLOCK_MI |
| 22178 | ); |
| 22179 | } |
| 22180 | p |
| 22181 | } else { |
| 22182 | match (has_cols32, has_rows32) { |
| 22183 | (true, false) => { |
| 22184 | // lane-golomb r1 (class `parsed-then-discarded`): the gathered |
| 22185 | // bit IS the partition -- 0 means PARTITION_HORZ, only 1 means SPLIT |
| 22186 | // (libaom `ec_read_partition_impl`). |
| 22187 | if { |
| 22188 | let b = dec.symbol_fixed(&gather(&cdfs.partition_w32[ctx32], VERT_ALIKE)); |
| 22189 | bump_edge32(b.min(1) as usize); |
| 22190 | b |
| 22191 | } == 1 { |
| 22192 | PARTITION_SPLIT |
| 22193 | } else { |
| 22194 | PARTITION_HORZ |
| 22195 | } |
| 22196 | } |
| 22197 | (false, true) => { |
| 22198 | // lane-golomb r1 (class `parsed-then-discarded`): the gathered |
| 22199 | // bit IS the partition -- 0 means PARTITION_VERT, only 1 means SPLIT |
| 22200 | // (libaom `ec_read_partition_impl`). |
| 22201 | if { |
| 22202 | let b = dec.symbol_fixed(&gather(&cdfs.partition_w32[ctx32], HORZ_ALIKE)); |
| 22203 | bump_edge32(b.min(1) as usize); |
| 22204 | b |
| 22205 | } == 1 { |
| 22206 | PARTITION_SPLIT |
| 22207 | } else { |
| 22208 | PARTITION_VERT |
| 22209 | } |
| 22210 | } |
| 22211 | _ => PARTITION_SPLIT, |
| 22212 | } |
| 22213 | }; |
| 22214 | match part32 { |
| 22215 | PARTITION_NONE => { |
| 22216 | decode_inter_block( |
| 22217 | &mut dec, |
| 22218 | &mut cdfs, |
| 22219 | &mut neighbours, |
| 22220 | &mut grid, |
| 22221 | sub16_to_mi(at), |
| 22222 | BLOCK, |
| 22223 | mi_cols, |
| 22224 | mi_rows, |
| 22225 | &mut y, |
| 22226 | &mut u, |
| 22227 | &mut v, |
| 22228 | &ref_y, |
| 22229 | &ref_u, |
| 22230 | &ref_v, |
| 22231 | &ref_slots, |
| 22232 | &sign_bias_table, |
| 22233 | &global_motion, |
| 22234 | base_q_idx, |
| 22235 | TxbSet::Luma32, |
| 22236 | TxbSet::Luma32Inter, |
| 22237 | TxbSet::Chroma16, |
| 22238 | TX32, |
| 22239 | TX16, |
| 22240 | &scan32, |
| 22241 | &scan16, |
| 22242 | 3, |
| 22243 | allow_high_precision_mv, |
| 22244 | force_integer_mv, |
| 22245 | interp_fixed, |
| 22246 | enable_dual_filter, |
| 22247 | tpl_frame.as_ref(), |
| 22248 | reference_select, |
| 22249 | enable_masked_compound, |
| 22250 | enable_interintra_compound, |
| 22251 | enable_jnt_comp, |
| 22252 | order_hint_bits, |
| 22253 | order_hint, |
| 22254 | ref_order_hints, |
| 22255 | skip_mode_present, |
| 22256 | skip_mode_frame, |
| 22257 | switchable_motion_mode, |
| 22258 | allow_warped_motion, |
| 22259 | false, |
| 22260 | BLOCK, |
| 22261 | BLOCK, |
| 22262 | allow_screen_content_tools, |
| 22263 | frame_width as usize, |
| 22264 | )?; |
| 22265 | } |
| 22266 | PARTITION_SPLIT => { |
| 22267 | for sub in 0..4 { |
| 22268 | let (sr, sc) = (r32 as usize * 2 + sub / 2, c32 as usize * 2 + sub % 2); |
| 22269 | if (sr as u32) * SUB_MI >= mi_rows || (sc as u32) * SUB_MI >= mi_cols { |
| 22270 | continue; |
| 22271 | } |
| 22272 | let (has_cols16, has_rows16) = ( |
| 22273 | has_half(sc as u32 * SUB_MI, SUB_MI, mi_cols), |
| 22274 | has_half(sr as u32 * SUB_MI, SUB_MI, mi_rows), |
| 22275 | ); |
| 22276 | if !has_cols16 && !has_rows16 { |
| 22277 | return Err(unsupported( |
| 22278 | "a 16x16 inter block whose true edge cuts through both \ |
| 22279 | axes needs a rectangular transform this decoder does \ |
| 22280 | not code yet", |
| 22281 | )); |
| 22282 | } |
| 22283 | let at16 = (sr, sc); |
| 22284 | let ctx16 = neighbours.partition_ctx(at16, SUB); |
| 22285 | // lane-inter8 r1: a straddling 16x16 cannot name a |
| 22286 | // value (spec forces SPLIT, one gathered bit); an |
| 22287 | // interior one reads the real alphabet, and |
| 22288 | // `PARTITION_SPLIT` now recurses into four 8x8 |
| 22289 | // inter leaves through the very same loop the |
| 22290 | // straddling case has always used. |
| 22291 | let part16 = if has_cols16 && has_rows16 { |
| 22292 | let p = dec.symbol(&mut cdfs.partition_w16[ctx16]); |
| 22293 | if std::env::var_os("EC_AV1_TRACE").is_some() { |
| 22294 | let (rng, _) = dec.debug_state(); |
| 22295 | eprintln!( |
| 22296 | "TRACE partition_w16 mi=({},{}) ctx={ctx16} value={p} rng={rng}", |
| 22297 | sr as u32 * SUB_MI, |
| 22298 | sc as u32 * SUB_MI |
| 22299 | ); |
| 22300 | } |
| 22301 | p |
| 22302 | } else { |
| 22303 | if has_cols16 { |
| 22304 | // lane-golomb r1 (class `parsed-then-discarded`): the gathered |
| 22305 | // bit IS the partition -- 0 means PARTITION_HORZ, only 1 means SPLIT |
| 22306 | // (libaom `ec_read_partition_impl`). |
| 22307 | if dec.symbol_fixed(&gather(&cdfs.partition_w16[ctx16], VERT_ALIKE)) == 1 { |
| 22308 | PARTITION_SPLIT |
| 22309 | } else { |
| 22310 | PARTITION_HORZ |
| 22311 | } |
| 22312 | } else { |
| 22313 | // lane-golomb r1 (class `parsed-then-discarded`): the gathered |
| 22314 | // bit IS the partition -- 0 means PARTITION_VERT, only 1 means SPLIT |
| 22315 | // (libaom `ec_read_partition_impl`). |
| 22316 | if dec.symbol_fixed(&gather(&cdfs.partition_w16[ctx16], HORZ_ALIKE)) == 1 { |
| 22317 | PARTITION_SPLIT |
| 22318 | } else { |
| 22319 | PARTITION_VERT |
| 22320 | } |
| 22321 | } |
| 22322 | }; |
| 22323 | if part16 == PARTITION_NONE { |
| 22324 | decode_inter_block( |
| 22325 | &mut dec, |
| 22326 | &mut cdfs, |
| 22327 | &mut neighbours, |
| 22328 | &mut grid, |
| 22329 | sub16_to_mi(at16), |
| 22330 | SUB, |
| 22331 | mi_cols, |
| 22332 | mi_rows, |
| 22333 | &mut y, |
| 22334 | &mut u, |
| 22335 | &mut v, |
| 22336 | &ref_y, |
| 22337 | &ref_u, |
| 22338 | &ref_v, |
| 22339 | &ref_slots, |
| 22340 | &sign_bias_table, |
| 22341 | &global_motion, |
| 22342 | base_q_idx, |
| 22343 | TxbSet::Luma16, |
| 22344 | if reduced_tx_set { |
| 22345 | TxbSet::Luma16Inter |
| 22346 | } else { |
| 22347 | TxbSet::Luma16InterSet1 |
| 22348 | }, |
| 22349 | TxbSet::Chroma8, |
| 22350 | TX16, |
| 22351 | TX8, |
| 22352 | &scan16, |
| 22353 | &scan8, |
| 22354 | 2, |
| 22355 | allow_high_precision_mv, |
| 22356 | force_integer_mv, |
| 22357 | interp_fixed, |
| 22358 | enable_dual_filter, |
| 22359 | tpl_frame.as_ref(), |
| 22360 | reference_select, |
| 22361 | enable_masked_compound, |
| 22362 | enable_interintra_compound, |
| 22363 | enable_jnt_comp, |
| 22364 | order_hint_bits, |
| 22365 | order_hint, |
| 22366 | ref_order_hints, |
| 22367 | skip_mode_present, |
| 22368 | skip_mode_frame, |
| 22369 | switchable_motion_mode, |
| 22370 | allow_warped_motion, |
| 22371 | false, |
| 22372 | SUB, |
| 22373 | SUB, |
| 22374 | allow_screen_content_tools, |
| 22375 | frame_width as usize, |
| 22376 | )?; |
| 22377 | } else if part16 == PARTITION_HORZ || part16 == PARTITION_VERT { |
| 22378 | // lane-inter4 r3: the 16x16-level rect inter |
| 22379 | // leaves -- two true 16x8 (HORZ) or 8x16 |
| 22380 | // (VERT) inter blocks. Same shape as the |
| 22381 | // 32-level HORZ/VERT arm above: `side = SUB` |
| 22382 | // keeps every CDF/prediction-buffer decision |
| 22383 | // square, `write_w`/`write_h` carry the true |
| 22384 | // footprint into the mv stack, the motion-mode |
| 22385 | // row, the neighbour stamps and the rectangular |
| 22386 | // residual path (`rect_inter_residual_supported` |
| 22387 | // covers 16x8/8x16 since this round). |
| 22388 | let horz = part16 == PARTITION_HORZ; |
| 22389 | if horz { |
| 22390 | INTER_LEAF16_HORZ_HITS.with(|c| c.set(c.get() + 1)); |
| 22391 | } else { |
| 22392 | INTER_LEAF16_VERT_HITS.with(|c| c.set(c.get() + 1)); |
| 22393 | } |
| 22394 | let base_mi = sub16_to_mi(at16); |
| 22395 | let (ww, wh) = if horz { (16, 8) } else { (8, 16) }; |
| 22396 | for piece in 0..2 { |
| 22397 | let piece_mi = if horz { |
| 22398 | (base_mi.0 + piece * 2, base_mi.1) |
| 22399 | } else { |
| 22400 | (base_mi.0, base_mi.1 + piece * 2) |
| 22401 | }; |
| 22402 | // libaom `decode_partition`'s own second- |
| 22403 | // half frame-edge break. |
| 22404 | if piece > 0 |
| 22405 | && (piece_mi.0 >= mi_rows as usize |
| 22406 | || piece_mi.1 >= mi_cols as usize) |
| 22407 | { |
| 22408 | bump_inter_edge_strip(if horz { 4 } else { 5 }); |
| 22409 | break; |
| 22410 | } |
| 22411 | decode_inter_block( |
| 22412 | &mut dec, |
| 22413 | &mut cdfs, |
| 22414 | &mut neighbours, |
| 22415 | &mut grid, |
| 22416 | piece_mi, |
| 22417 | SUB, |
| 22418 | mi_cols, |
| 22419 | mi_rows, |
| 22420 | &mut y, |
| 22421 | &mut u, |
| 22422 | &mut v, |
| 22423 | &ref_y, |
| 22424 | &ref_u, |
| 22425 | &ref_v, |
| 22426 | &ref_slots, |
| 22427 | &sign_bias_table, |
| 22428 | &global_motion, |
| 22429 | base_q_idx, |
| 22430 | TxbSet::Luma16, |
| 22431 | if reduced_tx_set { |
| 22432 | TxbSet::Luma16Inter |
| 22433 | } else { |
| 22434 | TxbSet::Luma16InterSet1 |
| 22435 | }, |
| 22436 | TxbSet::Chroma8, |
| 22437 | TX16, |
| 22438 | TX8, |
| 22439 | &scan16, |
| 22440 | &scan8, |
| 22441 | 1, |
| 22442 | allow_high_precision_mv, |
| 22443 | force_integer_mv, |
| 22444 | interp_fixed, |
| 22445 | enable_dual_filter, |
| 22446 | tpl_frame.as_ref(), |
| 22447 | reference_select, |
| 22448 | enable_masked_compound, |
| 22449 | enable_interintra_compound, |
| 22450 | enable_jnt_comp, |
| 22451 | order_hint_bits, |
| 22452 | order_hint, |
| 22453 | ref_order_hints, |
| 22454 | skip_mode_present, |
| 22455 | skip_mode_frame, |
| 22456 | switchable_motion_mode, |
| 22457 | allow_warped_motion, |
| 22458 | true, |
| 22459 | ww, |
| 22460 | wh, |
| 22461 | allow_screen_content_tools, |
| 22462 | frame_width as usize, |
| 22463 | )?; |
| 22464 | } |
| 22465 | } else if (PARTITION_HORZ_A..=PARTITION_VERT_B).contains(&part16) { |
| 22466 | // lane-inter16ab r1: the four AB (T-shaped) |
| 22467 | // partitions at the 16x16 level of an INTER |
| 22468 | // frame -- two 8x8 inter leaves plus one true |
| 22469 | // 16x8/8x16 inter strip. libaom |
| 22470 | // `decode_partition` (decodeframe.c ~5040, |
| 22471 | // `bsize2 = get_partition_subsize(bsize, |
| 22472 | // PARTITION_SPLIT)` = BLOCK_8X8 here) visits: |
| 22473 | // HORZ_A: TL 8x8, TR 8x8, bottom 16x8 |
| 22474 | // HORZ_B: top 16x8, BL 8x8, BR 8x8 |
| 22475 | // VERT_A: TL 8x8, BL 8x8, right 8x16 |
| 22476 | // VERT_B: left 8x16, TR 8x8, BR 8x8 |
| 22477 | // Every piece is inside the frame by |
| 22478 | // construction: an AB value can only be read |
| 22479 | // from the (has_cols16 && has_rows16) branch |
| 22480 | // above, i.e. mi_row+2 < mi_rows and |
| 22481 | // mi_col+2 < mi_cols, which is exactly |
| 22482 | // libaom's own `has_rows`/`has_cols`. |
| 22483 | // |
| 22484 | // VERT_A/VERT_B visit TL,BL,TR,BR, so a |
| 22485 | // bottom-left square's top-right neighbour is |
| 22486 | // the partition's own not-yet-decoded |
| 22487 | // rectangle: libaom switches those squares |
| 22488 | // onto `has_tr_vert_*`/`has_bl_vert_*` |
| 22489 | // (`reconintra.c get_has_tr_table`) -- |
| 22490 | // `Reach::vert_ab_partition()`, the same |
| 22491 | // guard the intra 16-level arm and the |
| 22492 | // 32-level inter AB arms take (class |
| 22493 | // `visit-order-changes-availability`). |
| 22494 | // |
| 22495 | // OBMC/warp and compound stay legal here: |
| 22496 | // every piece is >= 8x8, so libaom's |
| 22497 | // `is_motion_variation_allowed_bsize` |
| 22498 | // (min(bw,bh) >= 8, blockd.h) and |
| 22499 | // `is_comp_ref_allowed` (block_size_wide + |
| 22500 | // block_size_high >= 16, blockd.h) both hold |
| 22501 | // and the leaf/strip decoders' own gates |
| 22502 | // apply unchanged. |
| 22503 | bump_ab16_inter(part16 - PARTITION_HORZ_A); |
| 22504 | let _vert_guard = if part16 == PARTITION_VERT_A |
| 22505 | || part16 == PARTITION_VERT_B |
| 22506 | { |
| 22507 | Some(crate::encode::Reach::vert_ab_partition()) |
| 22508 | } else { |
| 22509 | None |
| 22510 | }; |
| 22511 | let (mi_row0, mi_col0) = sub16_to_mi(at16); |
| 22512 | let tl = (mi_row0, mi_col0); |
| 22513 | let tr = (mi_row0, mi_col0 + 2); |
| 22514 | let bl = (mi_row0 + 2, mi_col0); |
| 22515 | let br = (mi_row0 + 2, mi_col0 + 2); |
| 22516 | macro_rules! ab_strip16 { |
| 22517 | ($mi:expr, $ww:expr, $wh:expr) => { |
| 22518 | inter_piece!( |
| 22519 | $mi, |
| 22520 | SUB, |
| 22521 | TxbSet::Luma16, |
| 22522 | if reduced_tx_set { |
| 22523 | TxbSet::Luma16Inter |
| 22524 | } else { |
| 22525 | TxbSet::Luma16InterSet1 |
| 22526 | }, |
| 22527 | TxbSet::Chroma8, |
| 22528 | TX16, |
| 22529 | TX8, |
| 22530 | &scan16, |
| 22531 | &scan8, |
| 22532 | 1, |
| 22533 | $ww, |
| 22534 | $wh |
| 22535 | ) |
| 22536 | }; |
| 22537 | } |
| 22538 | match part16 { |
| 22539 | PARTITION_HORZ_A => { |
| 22540 | inter_leaf8!(at16, tl); |
| 22541 | inter_leaf8!(at16, tr); |
| 22542 | ab_strip16!(bl, 16, 8); |
| 22543 | } |
| 22544 | PARTITION_HORZ_B => { |
| 22545 | ab_strip16!(tl, 16, 8); |
| 22546 | inter_leaf8!(at16, bl); |
| 22547 | inter_leaf8!(at16, br); |
| 22548 | } |
| 22549 | PARTITION_VERT_A => { |
| 22550 | inter_leaf8!(at16, tl); |
| 22551 | inter_leaf8!(at16, bl); |
| 22552 | ab_strip16!(tr, 8, 16); |
| 22553 | } |
| 22554 | _ => { |
| 22555 | ab_strip16!(tl, 8, 16); |
| 22556 | inter_leaf8!(at16, tr); |
| 22557 | inter_leaf8!(at16, br); |
| 22558 | } |
| 22559 | } |
| 22560 | } else if part16 != PARTITION_SPLIT { |
| 22561 | return Err(unsupported( |
| 22562 | "an inter 16x16-level 1:4 partition (HORZ_4/VERT_4 -- four 16x4 or 4x16 inter strips; this decoder's inter path codes a 16x16 as NONE, HORZ, VERT, SPLIT or AB)", |
| 22563 | )); |
| 22564 | } else { |
| 22565 | if has_cols16 && has_rows16 { |
| 22566 | INTER_SUB16_SPLIT_HITS.with(|c| c.set(c.get() + 1)); |
| 22567 | } |
| 22568 | let (mi_row0, mi_col0) = (sr as u32 * SUB_MI, sc as u32 * SUB_MI); |
| 22569 | let leaf_positions: Vec<(u32, u32)> = (0..4) |
| 22570 | .map(|i| (mi_row0 + (i / 2) * 2, mi_col0 + (i % 2) * 2)) |
| 22571 | .filter(|&(mr, mc)| mr < mi_rows && mc < mi_cols) |
| 22572 | .collect(); |
| 22573 | // lane-scaledref r1: `decode_inter_block8` IS |
| 22574 | // threaded for a scaled reference now (its |
| 22575 | // single-ref, compound and OBMC MC all take |
| 22576 | // spec 7.11.3.3's scaled walk), but the one |
| 22577 | // recipe that reaches this path at all -- a |
| 22578 | // 64x72 superres fixture whose bottom 16-row |
| 22579 | // band straddles the true edge, so the |
| 22580 | // gathered split bit lands on 8x8 leaves -- |
| 22581 | // DESYNCS inside the leaf itself |
| 22582 | // (`from_switchable_symbol` handed a 4th |
| 22583 | // symbol, mc.rs:200), the same |
| 22584 | // below-8x8 desync lane-sub8 r2 left open and |
| 22585 | // unrelated to scaling. So the scaled MC here |
| 22586 | // is unproven: refuse by name rather than |
| 22587 | // ship a capability claim no gate exercises. |
| 22588 | // Deleting these six lines is the whole lift |
| 22589 | // once the leaf8 desync is fixed. |
| 22590 | if ref_y.width != frame_width as usize |
| 22591 | || ref_slots.iter().flatten().any(|(py, _, _)| { |
| 22592 | py.width != frame_width as usize |
| 22593 | }) |
| 22594 | { |
| 22595 | return Err(unsupported( |
| 22596 | "an 8x8 partition leaf under a scaled reference (superres, unimplemented)", |
| 22597 | )); |
| 22598 | } |
| 22599 | let mut prev_leaves: Vec<((usize, usize), bool, bool, i8, Option<i8>)> = Vec::new(); |
| 22600 | for (mr, mc) in leaf_positions { |
| 22601 | let leaf_mi = (mr as usize, mc as usize); |
| 22602 | let leaf_ctx = neighbours.partition_ctx_mi(leaf_mi, 8); |
| 22603 | let part8 = dec.symbol(&mut cdfs.partition_w8[leaf_ctx]); |
| 22604 | if part8 != PARTITION_NONE { |
| 22605 | return Err(unsupported( |
| 22606 | "an inter partition below 8x8 (this decoder codes no inter leaf smaller than 8x8; lane-sub8 scoped to intra)", |
| 22607 | )); |
| 22608 | } |
| 22609 | let ( |
| 22610 | skip, |
| 22611 | is_inter, |
| 22612 | skip_mode_leaf, |
| 22613 | compound_ctx8, |
| 22614 | leaf_filter, |
| 22615 | leaf_refs, |
| 22616 | ) = decode_inter_block8( |
| 22617 | &mut dec, |
| 22618 | &mut cdfs, |
| 22619 | &mut neighbours, |
| 22620 | &mut grid, |
| 22621 | mi_cols, |
| 22622 | mi_rows, |
| 22623 | at16, |
| 22624 | leaf_mi, |
| 22625 | &mut y, |
| 22626 | &mut u, |
| 22627 | &mut v, |
| 22628 | &ref_y, |
| 22629 | &ref_u, |
| 22630 | &ref_v, |
| 22631 | &ref_slots, |
| 22632 | base_q_idx, |
| 22633 | &scan8, |
| 22634 | &scan4, |
| 22635 | allow_high_precision_mv, |
| 22636 | force_integer_mv, |
| 22637 | &global_motion, |
| 22638 | enable_dual_filter, |
| 22639 | reference_select, |
| 22640 | enable_masked_compound, |
| 22641 | enable_interintra_compound, |
| 22642 | enable_jnt_comp, |
| 22643 | order_hint_bits, |
| 22644 | order_hint, |
| 22645 | ref_order_hints, |
| 22646 | skip_mode_present, |
| 22647 | skip_mode_frame, |
| 22648 | reduced_tx_set, |
| 22649 | interp_fixed, |
| 22650 | switchable_motion_mode, |
| 22651 | allow_warped_motion, |
| 22652 | allow_screen_content_tools, |
| 22653 | &sign_bias_table, |
| 22654 | tpl_frame.as_ref(), |
| 22655 | frame_width as usize, |
| 22656 | )?; |
| 22657 | prev_leaves.push((leaf_mi, skip, is_inter, leaf_refs.0, leaf_refs.1)); |
| 22658 | // lane-inter8 r2: stamp THIS leaf's own |
| 22659 | // 2x2-mi span before the next leaf reads |
| 22660 | // it as a neighbour -- the whole-16x16 |
| 22661 | // stamp below only ever described the |
| 22662 | // last leaf, so three quarters of the |
| 22663 | // block carried the wrong skip/ref/inter |
| 22664 | // state into the next block's contexts |
| 22665 | // (class context-read-from-one-cell). |
| 22666 | // lane-gmaffine r3: with the mi-granular |
| 22667 | // band, the leaf's OWN switchable-filter |
| 22668 | // symbols go in here -- the `[3, 3]` |
| 22669 | // ("intra, no filter") sentinel this used |
| 22670 | // to stamp made `obmc_blend`'s |
| 22671 | // `neighbour_filter` PANIC as soon as a |
| 22672 | // sibling leaf blended one of these. |
| 22673 | neighbours.record_inter_rect_mi( |
| 22674 | leaf_mi, |
| 22675 | 2, |
| 22676 | 2, |
| 22677 | skip, |
| 22678 | is_inter, |
| 22679 | leaf_refs.0, |
| 22680 | leaf_filter, |
| 22681 | skip_mode_leaf, |
| 22682 | ); |
| 22683 | if let Some(ref1) = leaf_refs.1 |
| 22684 | && let Some((_, _, group_idx, idx)) = compound_ctx8 |
| 22685 | { |
| 22686 | neighbours.record_compound_ctx_rect_mi( |
| 22687 | leaf_mi, 2, 2, ref1, group_idx, idx, |
| 22688 | ); |
| 22689 | } |
| 22690 | } |
| 22691 | // lane-inter8 r2: the four per-leaf |
| 22692 | // `record_inter_rect_mi` stamps above replace |
| 22693 | // the single whole-16x16 one that used to run |
| 22694 | // here with the LAST leaf's state. |
| 22695 | } |
| 22696 | } |
| 22697 | } |
| 22698 | PARTITION_HORZ_B => { |
| 22699 | // lane-warp r5: PARTITION_HORZ_B = 32x32 top strip + |
| 22700 | // 16x16 bottom-left + 16x16 bottom-right. aomenc |
| 22701 | // carves static regions into it; the strip is |
| 22702 | // decoded as a square 32x32 block: for a `skip` |
| 22703 | // block the symbol stream is identical to the true |
| 22704 | // 32x16 coding (no residual, no motion_mode/warp |
| 22705 | // symbol), and the square stamps it leaves over the |
| 22706 | // bottom half (neighbour arrays, skip/lf grids) are |
| 22707 | // re-stamped by the C/D leaves right after. The |
| 22708 | // mi-granular `left_side_mi` rows of a last-block-in- |
| 22709 | // tile strip leak 32-vs-16 only until the per-tile |
| 22710 | // `Neighbours::new` reset. |
| 22711 | EXTENDED_PARTITION_HITS.with(|c| c.set(c.get() + 1)); |
| 22712 | let at32 = at; |
| 22713 | decode_inter_block( |
| 22714 | &mut dec, |
| 22715 | &mut cdfs, |
| 22716 | &mut neighbours, |
| 22717 | &mut grid, |
| 22718 | sub16_to_mi(at32), |
| 22719 | BLOCK, |
| 22720 | mi_cols, |
| 22721 | mi_rows, |
| 22722 | &mut y, |
| 22723 | &mut u, |
| 22724 | &mut v, |
| 22725 | &ref_y, |
| 22726 | &ref_u, |
| 22727 | &ref_v, |
| 22728 | &ref_slots, |
| 22729 | &sign_bias_table, |
| 22730 | &global_motion, |
| 22731 | base_q_idx, |
| 22732 | TxbSet::Luma32, |
| 22733 | TxbSet::Luma32Inter, |
| 22734 | TxbSet::Chroma16, |
| 22735 | TX32, |
| 22736 | TX16, |
| 22737 | &scan32, |
| 22738 | &scan16, |
| 22739 | 3, |
| 22740 | allow_high_precision_mv, |
| 22741 | force_integer_mv, |
| 22742 | interp_fixed, |
| 22743 | enable_dual_filter, |
| 22744 | tpl_frame.as_ref(), |
| 22745 | reference_select, |
| 22746 | enable_masked_compound, |
| 22747 | enable_interintra_compound, |
| 22748 | enable_jnt_comp, |
| 22749 | order_hint_bits, |
| 22750 | order_hint, |
| 22751 | ref_order_hints, |
| 22752 | skip_mode_present, |
| 22753 | skip_mode_frame, |
| 22754 | switchable_motion_mode, |
| 22755 | allow_warped_motion, |
| 22756 | true, |
| 22757 | // lane-rect r2: the top strip's TRUE 32x16 |
| 22758 | // footprint (was BLOCK,BLOCK -- the same square |
| 22759 | // corner-cut rect-flake-1 exposed on HORZ). |
| 22760 | BLOCK, |
| 22761 | SUB, |
| 22762 | allow_screen_content_tools, |
| 22763 | frame_width as usize, |
| 22764 | )?; |
| 22765 | if (r32 * 2 + 1) as u32 * SUB_MI < mi_rows { |
| 22766 | decode_inter_block( |
| 22767 | &mut dec, |
| 22768 | &mut cdfs, |
| 22769 | &mut neighbours, |
| 22770 | &mut grid, |
| 22771 | sub16_to_mi((r32 as usize * 2 + 1, c32 as usize * 2)), |
| 22772 | SUB, |
| 22773 | mi_cols, |
| 22774 | mi_rows, |
| 22775 | &mut y, |
| 22776 | &mut u, |
| 22777 | &mut v, |
| 22778 | &ref_y, |
| 22779 | &ref_u, |
| 22780 | &ref_v, |
| 22781 | &ref_slots, |
| 22782 | &sign_bias_table, |
| 22783 | &global_motion, |
| 22784 | base_q_idx, |
| 22785 | TxbSet::Luma16, |
| 22786 | if reduced_tx_set { |
| 22787 | TxbSet::Luma16Inter |
| 22788 | } else { |
| 22789 | TxbSet::Luma16InterSet1 |
| 22790 | }, |
| 22791 | TxbSet::Chroma8, |
| 22792 | TX16, |
| 22793 | TX8, |
| 22794 | &scan16, |
| 22795 | &scan8, |
| 22796 | 2, |
| 22797 | allow_high_precision_mv, |
| 22798 | force_integer_mv, |
| 22799 | interp_fixed, |
| 22800 | enable_dual_filter, |
| 22801 | tpl_frame.as_ref(), |
| 22802 | reference_select, |
| 22803 | enable_masked_compound, |
| 22804 | enable_interintra_compound, |
| 22805 | enable_jnt_comp, |
| 22806 | order_hint_bits, |
| 22807 | order_hint, |
| 22808 | ref_order_hints, |
| 22809 | skip_mode_present, |
| 22810 | skip_mode_frame, |
| 22811 | switchable_motion_mode, |
| 22812 | allow_warped_motion, |
| 22813 | false, |
| 22814 | SUB, |
| 22815 | SUB, |
| 22816 | allow_screen_content_tools, |
| 22817 | frame_width as usize, |
| 22818 | )?; |
| 22819 | if (c32 * 2 + 1) as u32 * SUB_MI < mi_cols { |
| 22820 | decode_inter_block( |
| 22821 | &mut dec, |
| 22822 | &mut cdfs, |
| 22823 | &mut neighbours, |
| 22824 | &mut grid, |
| 22825 | sub16_to_mi((r32 as usize * 2 + 1, c32 as usize * 2 + 1)), |
| 22826 | SUB, |
| 22827 | mi_cols, |
| 22828 | mi_rows, |
| 22829 | &mut y, |
| 22830 | &mut u, |
| 22831 | &mut v, |
| 22832 | &ref_y, |
| 22833 | &ref_u, |
| 22834 | &ref_v, |
| 22835 | &ref_slots, |
| 22836 | &sign_bias_table, |
| 22837 | &global_motion, |
| 22838 | base_q_idx, |
| 22839 | TxbSet::Luma16, |
| 22840 | if reduced_tx_set { |
| 22841 | TxbSet::Luma16Inter |
| 22842 | } else { |
| 22843 | TxbSet::Luma16InterSet1 |
| 22844 | }, |
| 22845 | TxbSet::Chroma8, |
| 22846 | TX16, |
| 22847 | TX8, |
| 22848 | &scan16, |
| 22849 | &scan8, |
| 22850 | 2, |
| 22851 | allow_high_precision_mv, |
| 22852 | force_integer_mv, |
| 22853 | interp_fixed, |
| 22854 | enable_dual_filter, |
| 22855 | tpl_frame.as_ref(), |
| 22856 | reference_select, |
| 22857 | enable_masked_compound, |
| 22858 | enable_interintra_compound, |
| 22859 | enable_jnt_comp, |
| 22860 | order_hint_bits, |
| 22861 | order_hint, |
| 22862 | ref_order_hints, |
| 22863 | skip_mode_present, |
| 22864 | skip_mode_frame, |
| 22865 | switchable_motion_mode, |
| 22866 | allow_warped_motion, |
| 22867 | false, |
| 22868 | SUB, |
| 22869 | SUB, |
| 22870 | allow_screen_content_tools, |
| 22871 | frame_width as usize, |
| 22872 | )?; |
| 22873 | } |
| 22874 | } |
| 22875 | } |
| 22876 | PARTITION_HORZ => { |
| 22877 | // lane-rect r2: two true 32x16 strips. Both read at |
| 22878 | // `side=BLOCK` (CDF/size-class selection stays square, |
| 22879 | // matching HORZ_B's accepted corner-cut) but bw4/bh4 |
| 22880 | // (mvstack.rs, `motion_mode_eligible`) now derive from |
| 22881 | // `write_w`/`write_h`'s true 32x16 footprint -- the |
| 22882 | // pin's fix (r1's finding). |
| 22883 | RECT_PARTITION_HITS.with(|c| c.set(c.get() + 1)); |
| 22884 | decode_inter_block( |
| 22885 | &mut dec, |
| 22886 | &mut cdfs, |
| 22887 | &mut neighbours, |
| 22888 | &mut grid, |
| 22889 | sub16_to_mi(at), |
| 22890 | BLOCK, |
| 22891 | mi_cols, |
| 22892 | mi_rows, |
| 22893 | &mut y, |
| 22894 | &mut u, |
| 22895 | &mut v, |
| 22896 | &ref_y, |
| 22897 | &ref_u, |
| 22898 | &ref_v, |
| 22899 | &ref_slots, |
| 22900 | &sign_bias_table, |
| 22901 | &global_motion, |
| 22902 | base_q_idx, |
| 22903 | TxbSet::Luma32, |
| 22904 | TxbSet::Luma32Inter, |
| 22905 | TxbSet::Chroma16, |
| 22906 | TX32, |
| 22907 | TX16, |
| 22908 | &scan32, |
| 22909 | &scan16, |
| 22910 | 3, |
| 22911 | allow_high_precision_mv, |
| 22912 | force_integer_mv, |
| 22913 | interp_fixed, |
| 22914 | enable_dual_filter, |
| 22915 | tpl_frame.as_ref(), |
| 22916 | reference_select, |
| 22917 | enable_masked_compound, |
| 22918 | enable_interintra_compound, |
| 22919 | enable_jnt_comp, |
| 22920 | order_hint_bits, |
| 22921 | order_hint, |
| 22922 | ref_order_hints, |
| 22923 | skip_mode_present, |
| 22924 | skip_mode_frame, |
| 22925 | switchable_motion_mode, |
| 22926 | allow_warped_motion, |
| 22927 | true, |
| 22928 | BLOCK, |
| 22929 | SUB, |
| 22930 | allow_screen_content_tools, |
| 22931 | frame_width as usize, |
| 22932 | )?; |
| 22933 | if !has_rows32 { |
| 22934 | bump_edge32(2); |
| 22935 | } |
| 22936 | // lane-golomb r2: at the frame edge only the first half of the |
| 22937 | // strip is coded (libaom `decode_partition` guards the second |
| 22938 | // with the same has_rows32 test that made this partition non-square). |
| 22939 | if has_rows32 { |
| 22940 | decode_inter_block( |
| 22941 | &mut dec, |
| 22942 | &mut cdfs, |
| 22943 | &mut neighbours, |
| 22944 | &mut grid, |
| 22945 | sub16_to_mi((r32 as usize * 2 + 1, c32 as usize * 2)), |
| 22946 | BLOCK, |
| 22947 | mi_cols, |
| 22948 | mi_rows, |
| 22949 | &mut y, |
| 22950 | &mut u, |
| 22951 | &mut v, |
| 22952 | &ref_y, |
| 22953 | &ref_u, |
| 22954 | &ref_v, |
| 22955 | &ref_slots, |
| 22956 | &sign_bias_table, |
| 22957 | &global_motion, |
| 22958 | base_q_idx, |
| 22959 | TxbSet::Luma32, |
| 22960 | TxbSet::Luma32Inter, |
| 22961 | TxbSet::Chroma16, |
| 22962 | TX32, |
| 22963 | TX16, |
| 22964 | &scan32, |
| 22965 | &scan16, |
| 22966 | 3, |
| 22967 | allow_high_precision_mv, |
| 22968 | force_integer_mv, |
| 22969 | interp_fixed, |
| 22970 | enable_dual_filter, |
| 22971 | tpl_frame.as_ref(), |
| 22972 | reference_select, |
| 22973 | enable_masked_compound, |
| 22974 | enable_interintra_compound, |
| 22975 | enable_jnt_comp, |
| 22976 | order_hint_bits, |
| 22977 | order_hint, |
| 22978 | ref_order_hints, |
| 22979 | skip_mode_present, |
| 22980 | skip_mode_frame, |
| 22981 | switchable_motion_mode, |
| 22982 | allow_warped_motion, |
| 22983 | true, |
| 22984 | BLOCK, |
| 22985 | SUB, |
| 22986 | allow_screen_content_tools, |
| 22987 | frame_width as usize, |
| 22988 | )?; |
| 22989 | } else { |
| 22990 | bump_inter_edge_strip(2); |
| 22991 | } |
| 22992 | } |
| 22993 | PARTITION_VERT => { |
| 22994 | // lane-rect r2: mirror of PARTITION_HORZ above with |
| 22995 | // width/height swapped. |
| 22996 | RECT_PARTITION_HITS.with(|c| c.set(c.get() + 1)); |
| 22997 | decode_inter_block( |
| 22998 | &mut dec, |
| 22999 | &mut cdfs, |
| 23000 | &mut neighbours, |
| 23001 | &mut grid, |
| 23002 | sub16_to_mi(at), |
| 23003 | BLOCK, |
| 23004 | mi_cols, |
| 23005 | mi_rows, |
| 23006 | &mut y, |
| 23007 | &mut u, |
| 23008 | &mut v, |
| 23009 | &ref_y, |
| 23010 | &ref_u, |
| 23011 | &ref_v, |
| 23012 | &ref_slots, |
| 23013 | &sign_bias_table, |
| 23014 | &global_motion, |
| 23015 | base_q_idx, |
| 23016 | TxbSet::Luma32, |
| 23017 | TxbSet::Luma32Inter, |
| 23018 | TxbSet::Chroma16, |
| 23019 | TX32, |
| 23020 | TX16, |
| 23021 | &scan32, |
| 23022 | &scan16, |
| 23023 | 3, |
| 23024 | allow_high_precision_mv, |
| 23025 | force_integer_mv, |
| 23026 | interp_fixed, |
| 23027 | enable_dual_filter, |
| 23028 | tpl_frame.as_ref(), |
| 23029 | reference_select, |
| 23030 | enable_masked_compound, |
| 23031 | enable_interintra_compound, |
| 23032 | enable_jnt_comp, |
| 23033 | order_hint_bits, |
| 23034 | order_hint, |
| 23035 | ref_order_hints, |
| 23036 | skip_mode_present, |
| 23037 | skip_mode_frame, |
| 23038 | switchable_motion_mode, |
| 23039 | allow_warped_motion, |
| 23040 | true, |
| 23041 | SUB, |
| 23042 | BLOCK, |
| 23043 | allow_screen_content_tools, |
| 23044 | frame_width as usize, |
| 23045 | )?; |
| 23046 | if !has_cols32 { |
| 23047 | bump_edge32(3); |
| 23048 | } |
| 23049 | // lane-golomb r2: at the frame edge only the first half of the |
| 23050 | // strip is coded (libaom `decode_partition` guards the second |
| 23051 | // with the same has_cols32 test that made this partition non-square). |
| 23052 | if has_cols32 { |
| 23053 | decode_inter_block( |
| 23054 | &mut dec, |
| 23055 | &mut cdfs, |
| 23056 | &mut neighbours, |
| 23057 | &mut grid, |
| 23058 | sub16_to_mi((r32 as usize * 2, c32 as usize * 2 + 1)), |
| 23059 | BLOCK, |
| 23060 | mi_cols, |
| 23061 | mi_rows, |
| 23062 | &mut y, |
| 23063 | &mut u, |
| 23064 | &mut v, |
| 23065 | &ref_y, |
| 23066 | &ref_u, |
| 23067 | &ref_v, |
| 23068 | &ref_slots, |
| 23069 | &sign_bias_table, |
| 23070 | &global_motion, |
| 23071 | base_q_idx, |
| 23072 | TxbSet::Luma32, |
| 23073 | TxbSet::Luma32Inter, |
| 23074 | TxbSet::Chroma16, |
| 23075 | TX32, |
| 23076 | TX16, |
| 23077 | &scan32, |
| 23078 | &scan16, |
| 23079 | 3, |
| 23080 | allow_high_precision_mv, |
| 23081 | force_integer_mv, |
| 23082 | interp_fixed, |
| 23083 | enable_dual_filter, |
| 23084 | tpl_frame.as_ref(), |
| 23085 | reference_select, |
| 23086 | enable_masked_compound, |
| 23087 | enable_interintra_compound, |
| 23088 | enable_jnt_comp, |
| 23089 | order_hint_bits, |
| 23090 | order_hint, |
| 23091 | ref_order_hints, |
| 23092 | skip_mode_present, |
| 23093 | skip_mode_frame, |
| 23094 | switchable_motion_mode, |
| 23095 | allow_warped_motion, |
| 23096 | true, |
| 23097 | SUB, |
| 23098 | BLOCK, |
| 23099 | allow_screen_content_tools, |
| 23100 | frame_width as usize, |
| 23101 | )?; |
| 23102 | } else { |
| 23103 | bump_inter_edge_strip(3); |
| 23104 | } |
| 23105 | } |
| 23106 | PARTITION_HORZ_A => { |
| 23107 | // lane-partab r1: two 16x16 squares on top + a true |
| 23108 | // 32x16 strip below (libaom decode_partition |
| 23109 | // PARTITION_HORZ_A: TL, TR, bottom strip). |
| 23110 | PARTAB_HITS.with(|c| c.set(c.get() + 1)); |
| 23111 | decode_inter_block( |
| 23112 | &mut dec, |
| 23113 | &mut cdfs, |
| 23114 | &mut neighbours, |
| 23115 | &mut grid, |
| 23116 | sub16_to_mi(at), |
| 23117 | SUB, |
| 23118 | mi_cols, |
| 23119 | mi_rows, |
| 23120 | &mut y, |
| 23121 | &mut u, |
| 23122 | &mut v, |
| 23123 | &ref_y, |
| 23124 | &ref_u, |
| 23125 | &ref_v, |
| 23126 | &ref_slots, |
| 23127 | &sign_bias_table, |
| 23128 | &global_motion, |
| 23129 | base_q_idx, |
| 23130 | TxbSet::Luma16, |
| 23131 | if reduced_tx_set { |
| 23132 | TxbSet::Luma16Inter |
| 23133 | } else { |
| 23134 | TxbSet::Luma16InterSet1 |
| 23135 | }, |
| 23136 | TxbSet::Chroma8, |
| 23137 | TX16, |
| 23138 | TX8, |
| 23139 | &scan16, |
| 23140 | &scan8, |
| 23141 | 2, |
| 23142 | allow_high_precision_mv, |
| 23143 | force_integer_mv, |
| 23144 | interp_fixed, |
| 23145 | enable_dual_filter, |
| 23146 | tpl_frame.as_ref(), |
| 23147 | reference_select, |
| 23148 | enable_masked_compound, |
| 23149 | enable_interintra_compound, |
| 23150 | enable_jnt_comp, |
| 23151 | order_hint_bits, |
| 23152 | order_hint, |
| 23153 | ref_order_hints, |
| 23154 | skip_mode_present, |
| 23155 | skip_mode_frame, |
| 23156 | switchable_motion_mode, |
| 23157 | allow_warped_motion, |
| 23158 | false, |
| 23159 | SUB, |
| 23160 | SUB, |
| 23161 | allow_screen_content_tools, |
| 23162 | frame_width as usize, |
| 23163 | )?; |
| 23164 | decode_inter_block( |
| 23165 | &mut dec, |
| 23166 | &mut cdfs, |
| 23167 | &mut neighbours, |
| 23168 | &mut grid, |
| 23169 | sub16_to_mi((r32 as usize * 2, c32 as usize * 2 + 1)), |
| 23170 | SUB, |
| 23171 | mi_cols, |
| 23172 | mi_rows, |
| 23173 | &mut y, |
| 23174 | &mut u, |
| 23175 | &mut v, |
| 23176 | &ref_y, |
| 23177 | &ref_u, |
| 23178 | &ref_v, |
| 23179 | &ref_slots, |
| 23180 | &sign_bias_table, |
| 23181 | &global_motion, |
| 23182 | base_q_idx, |
| 23183 | TxbSet::Luma16, |
| 23184 | if reduced_tx_set { |
| 23185 | TxbSet::Luma16Inter |
| 23186 | } else { |
| 23187 | TxbSet::Luma16InterSet1 |
| 23188 | }, |
| 23189 | TxbSet::Chroma8, |
| 23190 | TX16, |
| 23191 | TX8, |
| 23192 | &scan16, |
| 23193 | &scan8, |
| 23194 | 2, |
| 23195 | allow_high_precision_mv, |
| 23196 | force_integer_mv, |
| 23197 | interp_fixed, |
| 23198 | enable_dual_filter, |
| 23199 | tpl_frame.as_ref(), |
| 23200 | reference_select, |
| 23201 | enable_masked_compound, |
| 23202 | enable_interintra_compound, |
| 23203 | enable_jnt_comp, |
| 23204 | order_hint_bits, |
| 23205 | order_hint, |
| 23206 | ref_order_hints, |
| 23207 | skip_mode_present, |
| 23208 | skip_mode_frame, |
| 23209 | switchable_motion_mode, |
| 23210 | allow_warped_motion, |
| 23211 | false, |
| 23212 | SUB, |
| 23213 | SUB, |
| 23214 | allow_screen_content_tools, |
| 23215 | frame_width as usize, |
| 23216 | )?; |
| 23217 | decode_inter_block( |
| 23218 | &mut dec, |
| 23219 | &mut cdfs, |
| 23220 | &mut neighbours, |
| 23221 | &mut grid, |
| 23222 | sub16_to_mi((r32 as usize * 2 + 1, c32 as usize * 2)), |
| 23223 | BLOCK, |
| 23224 | mi_cols, |
| 23225 | mi_rows, |
| 23226 | &mut y, |
| 23227 | &mut u, |
| 23228 | &mut v, |
| 23229 | &ref_y, |
| 23230 | &ref_u, |
| 23231 | &ref_v, |
| 23232 | &ref_slots, |
| 23233 | &sign_bias_table, |
| 23234 | &global_motion, |
| 23235 | base_q_idx, |
| 23236 | TxbSet::Luma32, |
| 23237 | TxbSet::Luma32Inter, |
| 23238 | TxbSet::Chroma16, |
| 23239 | TX32, |
| 23240 | TX16, |
| 23241 | &scan32, |
| 23242 | &scan16, |
| 23243 | 3, |
| 23244 | allow_high_precision_mv, |
| 23245 | force_integer_mv, |
| 23246 | interp_fixed, |
| 23247 | enable_dual_filter, |
| 23248 | tpl_frame.as_ref(), |
| 23249 | reference_select, |
| 23250 | enable_masked_compound, |
| 23251 | enable_interintra_compound, |
| 23252 | enable_jnt_comp, |
| 23253 | order_hint_bits, |
| 23254 | order_hint, |
| 23255 | ref_order_hints, |
| 23256 | skip_mode_present, |
| 23257 | skip_mode_frame, |
| 23258 | switchable_motion_mode, |
| 23259 | allow_warped_motion, |
| 23260 | true, |
| 23261 | BLOCK, |
| 23262 | SUB, |
| 23263 | allow_screen_content_tools, |
| 23264 | frame_width as usize, |
| 23265 | )?; |
| 23266 | } |
| 23267 | PARTITION_VERT_A => { |
| 23268 | // lane-partab r1: mirror of PARTITION_HORZ_A with |
| 23269 | // width/height swapped (TL, BL, right 16x32 strip). |
| 23270 | PARTAB_HITS.with(|c| c.set(c.get() + 1)); |
| 23271 | let _vert_ab = crate::encode::Reach::vert_ab_partition(); |
| 23272 | decode_inter_block( |
| 23273 | &mut dec, |
| 23274 | &mut cdfs, |
| 23275 | &mut neighbours, |
| 23276 | &mut grid, |
| 23277 | sub16_to_mi(at), |
| 23278 | SUB, |
| 23279 | mi_cols, |
| 23280 | mi_rows, |
| 23281 | &mut y, |
| 23282 | &mut u, |
| 23283 | &mut v, |
| 23284 | &ref_y, |
| 23285 | &ref_u, |
| 23286 | &ref_v, |
| 23287 | &ref_slots, |
| 23288 | &sign_bias_table, |
| 23289 | &global_motion, |
| 23290 | base_q_idx, |
| 23291 | TxbSet::Luma16, |
| 23292 | if reduced_tx_set { |
| 23293 | TxbSet::Luma16Inter |
| 23294 | } else { |
| 23295 | TxbSet::Luma16InterSet1 |
| 23296 | }, |
| 23297 | TxbSet::Chroma8, |
| 23298 | TX16, |
| 23299 | TX8, |
| 23300 | &scan16, |
| 23301 | &scan8, |
| 23302 | 2, |
| 23303 | allow_high_precision_mv, |
| 23304 | force_integer_mv, |
| 23305 | interp_fixed, |
| 23306 | enable_dual_filter, |
| 23307 | tpl_frame.as_ref(), |
| 23308 | reference_select, |
| 23309 | enable_masked_compound, |
| 23310 | enable_interintra_compound, |
| 23311 | enable_jnt_comp, |
| 23312 | order_hint_bits, |
| 23313 | order_hint, |
| 23314 | ref_order_hints, |
| 23315 | skip_mode_present, |
| 23316 | skip_mode_frame, |
| 23317 | switchable_motion_mode, |
| 23318 | allow_warped_motion, |
| 23319 | false, |
| 23320 | SUB, |
| 23321 | SUB, |
| 23322 | allow_screen_content_tools, |
| 23323 | frame_width as usize, |
| 23324 | )?; |
| 23325 | decode_inter_block( |
| 23326 | &mut dec, |
| 23327 | &mut cdfs, |
| 23328 | &mut neighbours, |
| 23329 | &mut grid, |
| 23330 | sub16_to_mi((r32 as usize * 2 + 1, c32 as usize * 2)), |
| 23331 | SUB, |
| 23332 | mi_cols, |
| 23333 | mi_rows, |
| 23334 | &mut y, |
| 23335 | &mut u, |
| 23336 | &mut v, |
| 23337 | &ref_y, |
| 23338 | &ref_u, |
| 23339 | &ref_v, |
| 23340 | &ref_slots, |
| 23341 | &sign_bias_table, |
| 23342 | &global_motion, |
| 23343 | base_q_idx, |
| 23344 | TxbSet::Luma16, |
| 23345 | if reduced_tx_set { |
| 23346 | TxbSet::Luma16Inter |
| 23347 | } else { |
| 23348 | TxbSet::Luma16InterSet1 |
| 23349 | }, |
| 23350 | TxbSet::Chroma8, |
| 23351 | TX16, |
| 23352 | TX8, |
| 23353 | &scan16, |
| 23354 | &scan8, |
| 23355 | 2, |
| 23356 | allow_high_precision_mv, |
| 23357 | force_integer_mv, |
| 23358 | interp_fixed, |
| 23359 | enable_dual_filter, |
| 23360 | tpl_frame.as_ref(), |
| 23361 | reference_select, |
| 23362 | enable_masked_compound, |
| 23363 | enable_interintra_compound, |
| 23364 | enable_jnt_comp, |
| 23365 | order_hint_bits, |
| 23366 | order_hint, |
| 23367 | ref_order_hints, |
| 23368 | skip_mode_present, |
| 23369 | skip_mode_fr |
Showing the first 1 MiB of this file. View raw.