edith_codecs
Clone
https://git.tahinli.com/Tahinli/edith_codecs.git
0 watching0 forksRust370.3 MiB50503afTahinli3 months ago1344 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, find_mv_stack_with_sign_bias, reference_mode_ctx, single_ref_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 SB_MI: u32 = 16; |
| 55 | const BLOCK_MI: u32 = 8; |
| 56 | |
| 57 | // How many `use_filter_intra` symbols this decoder has read as `1`, across |
| 58 | // every call on the current thread -- the cheap counter [`filter_intra_hits`] gate |
| 59 | // tests read (before/after, not the absolute value) to prove a stream |
| 60 | // actually exercised the filter-intra predictor rather than silently |
| 61 | // skipping it. |
| 62 | thread_local! { |
| 63 | static FILTER_INTRA_HITS: std::cell::Cell<usize> = |
| 64 | const { std::cell::Cell::new(0) }; |
| 65 | } |
| 66 | |
| 67 | /// Current value of [`FILTER_INTRA_HITS`]. |
| 68 | pub(crate) fn filter_intra_hits() -> usize { |
| 69 | FILTER_INTRA_HITS.with(|c| c.get()) |
| 70 | } |
| 71 | |
| 72 | // lane-realworld r1: this frame's `cdef.bits` (spec 5.9.19), threaded into |
| 73 | // [`maybe_read_cdef_idx`] the same way [`ENABLE_EDGE_FILTER`] threads a |
| 74 | // frame-level flag through the recursive block decode -- `0` (the default) |
| 75 | // makes every call a true no-op, matching every existing gate stream's |
| 76 | // `cdef_bits == 0` behaviour exactly. |
| 77 | thread_local! { |
| 78 | static CDEF_BITS: std::cell::Cell<u8> = const { std::cell::Cell::new(0) }; |
| 79 | } |
| 80 | // lane-realworld r8: the sequence header's `color_config.bit_depth`, set |
| 81 | // once per `decode_key_frame_tile_with_cdfs`/`decode_inter_frame_tile_with_cdfs` |
| 82 | // call the same way `CDEF_BITS` is above. `stream.rs`'s own refusal still |
| 83 | // blocks every non-8-bit stream from reaching a decode call, so this is `8` |
| 84 | // on every path that runs today -- threading it now (rather than the literal |
| 85 | // `8`) is stage 1 of widening past that refusal without re-deriving every |
| 86 | // clamp bound later. `PlaneBuf`'s sample storage stays `u16` regardless of |
| 87 | // depth (this decoder's own internal choice, matches dav1d/libaom). |
| 88 | thread_local! { |
| 89 | static BIT_DEPTH: std::cell::Cell<u8> = const { std::cell::Cell::new(8) }; |
| 90 | } |
| 91 | /// `(1 << bit_depth) - 1` — the reconstruction clamp bound for the current |
| 92 | /// stream's [`BIT_DEPTH`], replacing the crate-wide literal `255`. |
| 93 | pub(crate) fn sample_max() -> i32 { |
| 94 | (1i32 << BIT_DEPTH.with(std::cell::Cell::get)) - 1 |
| 95 | } |
| 96 | /// The current stream's bit depth, for callers (e.g. dequant) that need the |
| 97 | /// raw value rather than the derived clamp bound. |
| 98 | pub(crate) fn bit_depth() -> u8 { |
| 99 | BIT_DEPTH.with(std::cell::Cell::get) |
| 100 | } |
| 101 | /// Sets [`BIT_DEPTH`] for the current thread, called once per frame from |
| 102 | /// [`crate::stream`]'s decode loop. |
| 103 | pub(crate) fn set_bit_depth(bit_depth: u8) { |
| 104 | BIT_DEPTH.with(|c| c.set(bit_depth)); |
| 105 | } |
| 106 | // Whether this superblock's single CDEF unit (this decoder has no 128x128 |
| 107 | // SB, so the SB IS the CDEF unit) has already had its `cdef_idx` literal |
| 108 | // read -- reset to `false` at the top of each superblock in the two tile |
| 109 | // loops, spec `read_cdef`'s `cdef_transmitted[index]`. |
| 110 | thread_local! { |
| 111 | static CDEF_TRANSMITTED: std::cell::Cell<bool> = const { std::cell::Cell::new(false) }; |
| 112 | } |
| 113 | // Number of superblock columns in the current tile's grid, so |
| 114 | // [`maybe_read_cdef_idx`] can turn an `(mi_r, mi_c)` into a flat index into |
| 115 | // [`CDEF_IDX_GRID`]. |
| 116 | thread_local! { |
| 117 | static CDEF_SB_COLS: std::cell::Cell<usize> = const { std::cell::Cell::new(0) }; |
| 118 | } |
| 119 | // One resolved `cdef_idx` per superblock, read by [`apply_cdef`] to select |
| 120 | // which of the header's `1 << cdef.bits` strength pairs applies to that |
| 121 | // superblock's samples. |
| 122 | thread_local! { |
| 123 | static CDEF_IDX_GRID: std::cell::RefCell<Vec<u8>> = std::cell::RefCell::new(Vec::new()); |
| 124 | } |
| 125 | // How many real (`cdef.bits > 0`) `cdef_idx` literals [`maybe_read_cdef_idx`] |
| 126 | // has read, across every call on the current thread -- the gate's proof that a |
| 127 | // `cdef_bits > 0` stream actually reached the new reader rather than every |
| 128 | // superblock coincidentally being all-skip. |
| 129 | thread_local! { |
| 130 | static CDEF_IDX_HITS: std::cell::Cell<usize> = const { std::cell::Cell::new(0) }; |
| 131 | } |
| 132 | |
| 133 | /// Current value of [`CDEF_IDX_HITS`]. |
| 134 | pub(crate) fn cdef_idx_hits() -> usize { |
| 135 | CDEF_IDX_HITS.with(|c| c.get()) |
| 136 | } |
| 137 | |
| 138 | // lane-realworld r4: this frame's `delta.q_present` (spec 5.9.17) and its |
| 139 | // actual step (`1 << delta.q_res`, already the real multiplier, not the raw |
| 140 | // 2-bit field). `false`/`1` (the defaults) make [`maybe_read_delta_q`] a |
| 141 | // true no-op, matching every existing gate stream's `delta_q_present == 0` |
| 142 | // behaviour exactly, the same corner-cut [`CDEF_BITS`] takes above. |
| 143 | thread_local! { |
| 144 | static DELTA_Q_PRESENT: std::cell::Cell<bool> = const { std::cell::Cell::new(false) }; |
| 145 | static DELTA_Q_RES: std::cell::Cell<i32> = const { std::cell::Cell::new(1) }; |
| 146 | } |
| 147 | // The running quantizer index (spec `CurrentQIndex`, libaom |
| 148 | // `xd->current_base_qindex`) -- reset to the frame's own `base_q_idx` at the |
| 149 | // top of every tile (spec `decode_tile`), carried across superblocks within |
| 150 | // that tile, and mutated only by [`maybe_read_delta_q`]. Read back at the |
| 151 | // dequantization call sites instead of the frame-constant `base_q_idx`. |
| 152 | thread_local! { |
| 153 | static CURRENT_Q_IDX: std::cell::Cell<i32> = const { std::cell::Cell::new(0) }; |
| 154 | } |
| 155 | // This frame's own per-plane DC/AC quantizer-index deltas (lane-sbpart r11, |
| 156 | // spec 5.9.12/7.12.2, [`crate::quant::QuantDeltas`]) -- frame-constant (not |
| 157 | // per-superblock like [`CURRENT_Q_IDX`]), set once per frame decode and read |
| 158 | // back at every dequantization call site keyed on which plane it is doing. |
| 159 | thread_local! { |
| 160 | static QUANT_DELTAS: std::cell::Cell<crate::quant::QuantDeltas> = const { |
| 161 | std::cell::Cell::new(crate::quant::QuantDeltas { |
| 162 | y_dc: 0, |
| 163 | u_dc: 0, |
| 164 | u_ac: 0, |
| 165 | v_dc: 0, |
| 166 | v_ac: 0, |
| 167 | }) |
| 168 | }; |
| 169 | } |
| 170 | // How many real `delta_q_present` symbol groups [`maybe_read_delta_q`] has |
| 171 | // read, across every call on the current thread -- the gate's proof that a |
| 172 | // `delta_q_present` stream actually reached the new reader. |
| 173 | thread_local! { |
| 174 | static DELTA_Q_HITS: std::cell::Cell<usize> = const { std::cell::Cell::new(0) }; |
| 175 | } |
| 176 | |
| 177 | /// Current value of [`DELTA_Q_HITS`]. |
| 178 | pub(crate) fn delta_q_hits() -> usize { |
| 179 | DELTA_Q_HITS.with(|c| c.get()) |
| 180 | } |
| 181 | |
| 182 | /// This frame's [`QUANT_DELTAS`] `(dc, ac)` pair for plane `plane_idx` (0 = |
| 183 | /// Y, 1 = U, 2 = V) -- luma has no coded `ac` delta (spec 5.9.12 never reads |
| 184 | /// one), so it is always `0` here. |
| 185 | fn plane_q_delta(plane_idx: usize) -> (i32, i32) { |
| 186 | let d = QUANT_DELTAS.with(|c| c.get()); |
| 187 | match plane_idx { |
| 188 | 0 => (d.y_dc, 0), |
| 189 | 1 => (d.u_dc, d.u_ac), |
| 190 | _ => (d.v_dc, d.v_ac), |
| 191 | } |
| 192 | } |
| 193 | |
| 194 | /// `read_delta_qindex`/`read_delta_q_params` (spec 5.11.10, libaom |
| 195 | /// `decodemv.c:85-146,735-770`), called right after [`maybe_read_cdef_idx`] |
| 196 | /// at every block-decode call site (spec order `skip -> cdef -> delta_q`). |
| 197 | /// A no-op when `delta_q_present` is unset, or when this block is not at the |
| 198 | /// superblock's own top-left MI position (`b_row == 0 && b_col == 0` in |
| 199 | /// libaom, collapsed here to a position check since a partition tree's |
| 200 | /// top-left leaf is the only block that can ever land there), or when that |
| 201 | /// leaf both *is* the whole superblock and is `skip` (the one case libaom |
| 202 | /// itself skips the read, state carrying over unchanged). |
| 203 | fn maybe_read_delta_q(dec: &mut SymbolDecoder, cdfs: &mut Cdfs, mi_r: usize, mi_c: usize, is_whole_sb: bool, skip: bool) { |
| 204 | if !DELTA_Q_PRESENT.with(|c| c.get()) { |
| 205 | return; |
| 206 | } |
| 207 | if mi_r % SB_MI as usize != 0 || mi_c % SB_MI as usize != 0 { |
| 208 | return; |
| 209 | } |
| 210 | if is_whole_sb && skip { |
| 211 | return; |
| 212 | } |
| 213 | let mut abs = dec.symbol(&mut cdfs.delta_q) as i32; |
| 214 | const DELTA_Q_SMALL: i32 = 3; |
| 215 | if abs >= DELTA_Q_SMALL { |
| 216 | let rem_bits = dec.literal(3) + 1; |
| 217 | let thr = (1i32 << rem_bits) + 1; |
| 218 | abs = dec.literal(rem_bits) as i32 + thr; |
| 219 | } |
| 220 | let sign_negative = if abs != 0 { dec.literal(1) != 0 } else { true }; |
| 221 | let reduced = if sign_negative { -abs } else { abs }; |
| 222 | DELTA_Q_HITS.with(|c| c.set(c.get() + 1)); |
| 223 | let res = DELTA_Q_RES.with(|c| c.get()); |
| 224 | CURRENT_Q_IDX.with(|c| c.set((c.get() + reduced * res).clamp(1, 255))); |
| 225 | } |
| 226 | |
| 227 | // lane-realworld r5: this frame's `delta.lf_present`/`lf_res`/`lf_multi` |
| 228 | // (spec 5.9.18) -- same no-op-by-default corner-cut as `DELTA_Q_PRESENT`. |
| 229 | thread_local! { |
| 230 | static DELTA_LF_PRESENT: std::cell::Cell<bool> = const { std::cell::Cell::new(false) }; |
| 231 | static DELTA_LF_RES: std::cell::Cell<i32> = const { std::cell::Cell::new(1) }; |
| 232 | static DELTA_LF_MULTI: std::cell::Cell<bool> = const { std::cell::Cell::new(false) }; |
| 233 | } |
| 234 | // The running per-plane/direction loop-filter delta (spec `DeltaLF[i]`, |
| 235 | // `i` in `0..FRAME_LF_COUNT`) -- reset to `[0; 4]` at the top of every tile, |
| 236 | // mutated only by [`maybe_read_delta_lf`]. `!delta_lf_multi` mode only ever |
| 237 | // updates index 0 and every reader treats that as the value for all 4 |
| 238 | // indices (spec `read_delta_lf_params`'s own single-cdf branch). |
| 239 | thread_local! { |
| 240 | static CURRENT_DELTA_LF: std::cell::Cell<[i32; 4]> = const { std::cell::Cell::new([0; 4]) }; |
| 241 | } |
| 242 | // How many real `delta_lf_present` symbol groups [`maybe_read_delta_lf`] has |
| 243 | // read -- the gate's proof the reader fired, mirroring [`DELTA_Q_HITS`]. |
| 244 | thread_local! { |
| 245 | static DELTA_LF_HITS: std::cell::Cell<usize> = const { std::cell::Cell::new(0) }; |
| 246 | } |
| 247 | |
| 248 | /// Current value of [`DELTA_LF_HITS`]. |
| 249 | pub(crate) fn delta_lf_hits() -> usize { |
| 250 | DELTA_LF_HITS.with(|c| c.get()) |
| 251 | } |
| 252 | |
| 253 | /// `read_delta_lflevel`/`read_delta_lf_params` (spec 5.11.11, libaom |
| 254 | /// `decodemv.c:99-146,735-770`) -- same shape and same call-site gating as |
| 255 | /// [`maybe_read_delta_q`] (spec order `delta_q -> delta_lf`), looped over |
| 256 | /// `FRAME_LF_COUNT = 4` planes/directions when `delta_lf_multi` is set, or |
| 257 | /// read once into index 0 otherwise. |
| 258 | fn maybe_read_delta_lf(dec: &mut SymbolDecoder, cdfs: &mut Cdfs, mi_r: usize, mi_c: usize, is_whole_sb: bool, skip: bool) { |
| 259 | if !DELTA_LF_PRESENT.with(|c| c.get()) { |
| 260 | return; |
| 261 | } |
| 262 | if mi_r % SB_MI as usize != 0 || mi_c % SB_MI as usize != 0 { |
| 263 | return; |
| 264 | } |
| 265 | if is_whole_sb && skip { |
| 266 | return; |
| 267 | } |
| 268 | let multi = DELTA_LF_MULTI.with(|c| c.get()); |
| 269 | let res = DELTA_LF_RES.with(|c| c.get()); |
| 270 | let count = if multi { 4 } else { 1 }; |
| 271 | let mut cur = CURRENT_DELTA_LF.with(|c| c.get()); |
| 272 | for i in 0..count { |
| 273 | let mut abs = if multi { |
| 274 | dec.symbol(&mut cdfs.delta_lf_multi[i]) as i32 |
| 275 | } else { |
| 276 | dec.symbol(&mut cdfs.delta_lf) as i32 |
| 277 | }; |
| 278 | const DELTA_LF_SMALL: i32 = 3; |
| 279 | if abs >= DELTA_LF_SMALL { |
| 280 | let rem_bits = dec.literal(3) + 1; |
| 281 | let thr = (1i32 << rem_bits) + 1; |
| 282 | abs = dec.literal(rem_bits) as i32 + thr; |
| 283 | } |
| 284 | let sign_negative = if abs != 0 { dec.literal(1) != 0 } else { true }; |
| 285 | let reduced = if sign_negative { -abs } else { abs }; |
| 286 | cur[i] = (cur[i] + reduced * res).clamp(-63, 63); |
| 287 | DELTA_LF_HITS.with(|c| c.set(c.get() + 1)); |
| 288 | } |
| 289 | CURRENT_DELTA_LF.with(|c| c.set(cur)); |
| 290 | } |
| 291 | |
| 292 | /// `read_cdef` (spec 5.11.56, libaom `decodemv.c:read_cdef`), called right |
| 293 | /// after `skip` is known at every block-decode call site. A no-op at |
| 294 | /// `cdef.bits == 0` (0-bit literal, nothing to consume) -- and |
| 295 | /// `read_cdef_params` already forces `bits = 0` under `coded_lossless`/ |
| 296 | /// `allow_intrabc`, so neither needs a separate check here. Reads once per |
| 297 | /// superblock, at the first non-skip block. |
| 298 | fn maybe_read_cdef_idx(dec: &mut SymbolDecoder, mi_r: usize, mi_c: usize, skip: bool) { |
| 299 | let bits = CDEF_BITS.with(|c| c.get()); |
| 300 | if bits == 0 || skip || CDEF_TRANSMITTED.with(|c| c.get()) { |
| 301 | return; |
| 302 | } |
| 303 | let idx = dec.literal(u32::from(bits)) as u8; |
| 304 | CDEF_TRANSMITTED.with(|c| c.set(true)); |
| 305 | CDEF_IDX_HITS.with(|c| c.set(c.get() + 1)); |
| 306 | let (sb_r, sb_c) = (mi_r / SB_MI as usize, mi_c / SB_MI as usize); |
| 307 | let sb_cols = CDEF_SB_COLS.with(|c| c.get()); |
| 308 | if sb_cols > 0 { |
| 309 | CDEF_IDX_GRID.with(|g| { |
| 310 | let mut g = g.borrow_mut(); |
| 311 | let i = sb_r * sb_cols + sb_c; |
| 312 | if i < g.len() { |
| 313 | g[i] = idx; |
| 314 | } |
| 315 | }); |
| 316 | } |
| 317 | } |
| 318 | |
| 319 | // --------------------------------------------------------------------------- |
| 320 | // lane-seg: segmentation. Header (spec 5.9.14) is parsed by `ec-av1-syntax`; |
| 321 | // this block is the per-block `segment_id` reader (spec 5.11.7-5.11.9) plus |
| 322 | // the two consumers a real stream exercises: the per-segment quantizer index |
| 323 | // (spec 7.12.2 `get_qindex`) and the per-segment loop-filter level |
| 324 | // (spec 7.14.4). Shapes follow libaom `decodemv.c:read_segment_id`/ |
| 325 | // `read_intra_segment_id`/`read_inter_segment_id` and |
| 326 | // `pred_common.h:av1_get_spatial_seg_pred` -- note libaom returns the SPATIAL |
| 327 | // PREDICTION (not 0) from `read_segment_id` when the block is `skip`, which |
| 328 | // is what the oracle every gate here compares against actually does. |
| 329 | thread_local! { |
| 330 | static SEG: std::cell::RefCell<SegmentationParams> = |
| 331 | std::cell::RefCell::new(SegmentationParams::default()); |
| 332 | /// `CurrentSegmentIds`: this frame's own map, `mi_rows * mi_cols` entries |
| 333 | /// with stride `mi_cols` (NOT the padded `Neighbours` grid stride). |
| 334 | static SEG_IDS: std::cell::RefCell<Vec<u8>> = const { std::cell::RefCell::new(Vec::new()) }; |
| 335 | /// `PrevSegmentIds`: the primary reference frame's saved map, empty when |
| 336 | /// this frame has no primary reference (spec `load_previous_segment_ids`). |
| 337 | static PREV_SEG_IDS: std::cell::RefCell<Vec<u8>> = const { std::cell::RefCell::new(Vec::new()) }; |
| 338 | /// `(mi_rows, mi_cols)` for both maps above. |
| 339 | static SEG_MI_DIMS: std::cell::Cell<(usize, usize)> = const { std::cell::Cell::new((0, 0)) }; |
| 340 | /// `AboveSegPredContext` (per mi column) / `LeftSegPredContext` (per mi |
| 341 | /// row) -- spec 5.11.9's own `seg_id_predicted` neighbour state, which |
| 342 | /// libaom keeps on the neighbouring `mbmi->seg_id_predicted` instead. |
| 343 | static ABOVE_SEG_PRED: std::cell::RefCell<Vec<u8>> = const { std::cell::RefCell::new(Vec::new()) }; |
| 344 | static LEFT_SEG_PRED: std::cell::RefCell<Vec<u8>> = const { std::cell::RefCell::new(Vec::new()) }; |
| 345 | /// The block currently being decoded -- read back by [`block_q_idx`]. |
| 346 | static CUR_SEGMENT_ID: std::cell::Cell<u8> = const { std::cell::Cell::new(0) }; |
| 347 | /// The tile's own top-left mi position, for `AvailU`/`AvailL`. |
| 348 | static SEG_TILE_ORIGIN: std::cell::Cell<(usize, usize)> = const { std::cell::Cell::new((0, 0)) }; |
| 349 | /// How many real `segment_id` symbols were read, and the set of segment |
| 350 | /// ids any block ended up with -- the gate's proof the feature fired. |
| 351 | static SEG_ID_HITS: std::cell::Cell<usize> = const { std::cell::Cell::new(0) }; |
| 352 | static SEG_IDS_SEEN: std::cell::Cell<u8> = const { std::cell::Cell::new(0) }; |
| 353 | /// How many `seg_id_predicted` symbols were read (temporal update). |
| 354 | static SEG_PRED_HITS: std::cell::Cell<usize> = const { std::cell::Cell::new(0) }; |
| 355 | } |
| 356 | |
| 357 | /// Installs this frame's segmentation parameters and allocates its map, with |
| 358 | /// `prev` the primary reference frame's saved map (spec |
| 359 | /// `load_previous_segment_ids`; `None` for `PRIMARY_REF_NONE` or an empty |
| 360 | /// slot). Called once per frame, whether or not segmentation is enabled. |
| 361 | pub(crate) fn set_segmentation( |
| 362 | seg: SegmentationParams, |
| 363 | mi_rows: usize, |
| 364 | mi_cols: usize, |
| 365 | prev: Option<&[u8]>, |
| 366 | ) { |
| 367 | SEG.with(|s| *s.borrow_mut() = seg); |
| 368 | SEG_MI_DIMS.with(|c| c.set((mi_rows, mi_cols))); |
| 369 | SEG_IDS.with(|m| *m.borrow_mut() = vec![0u8; mi_rows * mi_cols]); |
| 370 | PREV_SEG_IDS.with(|m| { |
| 371 | let mut m = m.borrow_mut(); |
| 372 | m.clear(); |
| 373 | if let Some(p) = prev { |
| 374 | if p.len() == mi_rows * mi_cols { |
| 375 | m.extend_from_slice(p); |
| 376 | } |
| 377 | } |
| 378 | }); |
| 379 | ABOVE_SEG_PRED.with(|m| *m.borrow_mut() = vec![0u8; mi_cols]); |
| 380 | LEFT_SEG_PRED.with(|m| *m.borrow_mut() = vec![0u8; mi_rows]); |
| 381 | CUR_SEGMENT_ID.with(|c| c.set(0)); |
| 382 | } |
| 383 | |
| 384 | /// This frame's decoded segment map, to be stored into whichever reference |
| 385 | /// slots `refresh_frame_flags` names (spec 7.20's `SavedSegmentIds`). |
| 386 | pub(crate) fn take_segment_ids() -> Vec<u8> { |
| 387 | SEG_IDS.with(|m| m.borrow().clone()) |
| 388 | } |
| 389 | |
| 390 | /// How many `segment_id` symbols have been read (never reset per frame). |
| 391 | pub(crate) fn segment_id_hits() -> usize { |
| 392 | SEG_ID_HITS.with(|c| c.get()) |
| 393 | } |
| 394 | |
| 395 | /// How many distinct segment ids any decoded block has carried. |
| 396 | pub(crate) fn segment_ids_seen() -> usize { |
| 397 | SEG_IDS_SEEN.with(|c| c.get().count_ones() as usize) |
| 398 | } |
| 399 | |
| 400 | /// How many `seg_id_predicted` symbols have been read (temporal update). |
| 401 | pub(crate) fn segment_pred_hits() -> usize { |
| 402 | SEG_PRED_HITS.with(|c| c.get()) |
| 403 | } |
| 404 | |
| 405 | /// Resets the segmentation hit counters (gate setup). |
| 406 | pub(crate) fn reset_segment_hits() { |
| 407 | SEG_ID_HITS.with(|c| c.set(0)); |
| 408 | SEG_IDS_SEEN.with(|c| c.set(0)); |
| 409 | SEG_PRED_HITS.with(|c| c.set(0)); |
| 410 | } |
| 411 | |
| 412 | /// `seg_feature_active_idx(segment_id, feature)` for the frame currently |
| 413 | /// being decoded, with its `FeatureData` value. |
| 414 | fn seg_feature(segment_id: usize, feature: usize) -> Option<i32> { |
| 415 | SEG.with(|s| { |
| 416 | let s = s.borrow(); |
| 417 | s.feature_active(segment_id, feature) |
| 418 | .then(|| i32::from(s.feature_data[segment_id][feature])) |
| 419 | }) |
| 420 | } |
| 421 | |
| 422 | /// `get_qindex(ignoreDeltaQ = 0, segment_id)` (spec 7.12.2, libaom |
| 423 | /// `av1_get_qindex`) for the block currently being decoded: the running |
| 424 | /// `CurrentQIndex` shifted by this block's segment's `SEG_LVL_ALT_Q` data. |
| 425 | /// Every dequant call site reads the block's quantizer through here. |
| 426 | fn block_q_idx() -> i32 { |
| 427 | let base = CURRENT_Q_IDX.with(|c| c.get()); |
| 428 | let seg_id = CUR_SEGMENT_ID.with(|c| c.get()) as usize; |
| 429 | match seg_feature(seg_id, SEG_LVL_ALT_Q) { |
| 430 | Some(data) => (base + data).clamp(0, 255), |
| 431 | None => base, |
| 432 | } |
| 433 | } |
| 434 | |
| 435 | /// The segment id recorded for one mi cell of the current frame's map. |
| 436 | fn segment_id_at(mi_r: usize, mi_c: usize) -> u8 { |
| 437 | let (mi_rows, mi_cols) = SEG_MI_DIMS.with(|c| c.get()); |
| 438 | if mi_r >= mi_rows || mi_c >= mi_cols { |
| 439 | return 0; |
| 440 | } |
| 441 | SEG_IDS.with(|m| m.borrow()[mi_r * mi_cols + mi_c]) |
| 442 | } |
| 443 | |
| 444 | /// `set_segment_id`: stamps `id` over every mi cell this block covers. |
| 445 | fn write_segment_id(mi_r: usize, mi_c: usize, w_mi: usize, h_mi: usize, id: u8) { |
| 446 | let (mi_rows, mi_cols) = SEG_MI_DIMS.with(|c| c.get()); |
| 447 | let (x_mis, y_mis) = (w_mi.min(mi_cols.saturating_sub(mi_c)), h_mi.min(mi_rows.saturating_sub(mi_r))); |
| 448 | SEG_IDS.with(|m| { |
| 449 | let mut m = m.borrow_mut(); |
| 450 | for y in 0..y_mis { |
| 451 | for x in 0..x_mis { |
| 452 | m[(mi_r + y) * mi_cols + mi_c + x] = id; |
| 453 | } |
| 454 | } |
| 455 | }); |
| 456 | CUR_SEGMENT_ID.with(|c| c.set(id)); |
| 457 | SEG_IDS_SEEN.with(|c| c.set(c.get() | 1 << id)); |
| 458 | } |
| 459 | |
| 460 | /// `get_predicted_segment_id` (libaom `dec_get_segment_id` over |
| 461 | /// `last_frame_seg_map`): the minimum previous-frame segment id over the |
| 462 | /// block's own mi footprint. `0` when there is no previous map. |
| 463 | fn predicted_segment_id(mi_r: usize, mi_c: usize, w_mi: usize, h_mi: usize) -> u8 { |
| 464 | let (mi_rows, mi_cols) = SEG_MI_DIMS.with(|c| c.get()); |
| 465 | PREV_SEG_IDS.with(|m| { |
| 466 | let m = m.borrow(); |
| 467 | if m.is_empty() { |
| 468 | return 0; |
| 469 | } |
| 470 | let (x_mis, y_mis) = (w_mi.min(mi_cols.saturating_sub(mi_c)), h_mi.min(mi_rows.saturating_sub(mi_r))); |
| 471 | let mut id = u8::MAX; |
| 472 | for y in 0..y_mis { |
| 473 | for x in 0..x_mis { |
| 474 | id = id.min(m[(mi_r + y) * mi_cols + mi_c + x]); |
| 475 | } |
| 476 | } |
| 477 | if id == u8::MAX { 0 } else { id } |
| 478 | }) |
| 479 | } |
| 480 | |
| 481 | /// `av1_neg_deinterleave` (libaom `decodemv.c:258`). |
| 482 | fn neg_deinterleave(diff: i32, reference: i32, max: i32) -> i32 { |
| 483 | if reference == 0 { |
| 484 | return diff; |
| 485 | } |
| 486 | if reference >= max - 1 { |
| 487 | return max - diff - 1; |
| 488 | } |
| 489 | let half = if 2 * reference < max { |
| 490 | diff <= 2 * reference |
| 491 | } else { |
| 492 | diff <= 2 * (max - reference - 1) |
| 493 | }; |
| 494 | if half { |
| 495 | return if diff & 1 != 0 { |
| 496 | reference + ((diff + 1) >> 1) |
| 497 | } else { |
| 498 | reference - (diff >> 1) |
| 499 | }; |
| 500 | } |
| 501 | if 2 * reference < max { diff } else { max - (diff + 1) } |
| 502 | } |
| 503 | |
| 504 | /// `av1_get_spatial_seg_pred`: `(prediction, cdf_index)` from the current |
| 505 | /// frame's already-decoded above/left/above-left neighbours. |
| 506 | fn spatial_seg_pred(mi_r: usize, mi_c: usize) -> (i32, usize) { |
| 507 | let (row0, col0) = SEG_TILE_ORIGIN.with(|c| c.get()); |
| 508 | let (avail_u, avail_l) = (mi_r > row0, mi_c > col0); |
| 509 | let prev_ul = (avail_u && avail_l).then(|| i32::from(segment_id_at(mi_r - 1, mi_c - 1))); |
| 510 | let prev_u = avail_u.then(|| i32::from(segment_id_at(mi_r - 1, mi_c))); |
| 511 | let prev_l = avail_l.then(|| i32::from(segment_id_at(mi_r, mi_c - 1))); |
| 512 | let ctx = match prev_ul { |
| 513 | None => 0, |
| 514 | Some(ul) if Some(ul) == prev_u && Some(ul) == prev_l => 2, |
| 515 | Some(ul) if Some(ul) == prev_u || Some(ul) == prev_l || prev_u == prev_l => 1, |
| 516 | Some(_) => 0, |
| 517 | }; |
| 518 | let pred = match (prev_u, prev_l) { |
| 519 | (None, l) => l.unwrap_or(0), |
| 520 | (Some(u), None) => u, |
| 521 | (Some(u), Some(l)) => { |
| 522 | if prev_ul == Some(u) { |
| 523 | u |
| 524 | } else { |
| 525 | l |
| 526 | } |
| 527 | } |
| 528 | }; |
| 529 | (pred, ctx) |
| 530 | } |
| 531 | |
| 532 | /// `read_segment_id` (libaom `decodemv.c:280`): the spatially predicted id |
| 533 | /// when `skip`, otherwise one `segment_id` symbol de-interleaved against |
| 534 | /// that prediction. Does NOT write the map -- callers do. |
| 535 | fn read_segment_id(dec: &mut SymbolDecoder, cdfs: &mut Cdfs, mi_r: usize, mi_c: usize, skip: bool) -> u8 { |
| 536 | let (pred, ctx) = spatial_seg_pred(mi_r, mi_c); |
| 537 | if skip { |
| 538 | return pred as u8; |
| 539 | } |
| 540 | let coded = dec.symbol(&mut cdfs.segment_id[ctx]) as i32; |
| 541 | SEG_ID_HITS.with(|c| c.set(c.get() + 1)); |
| 542 | let last_active = SEG.with(|s| i32::from(s.borrow().last_active_seg_id)); |
| 543 | neg_deinterleave(coded, pred, last_active + 1).clamp(0, last_active) as u8 |
| 544 | } |
| 545 | |
| 546 | /// `read_intra_segment_id` (spec 5.11.7): called twice per intra-frame block |
| 547 | /// -- once before `skip` when `SegIdPreSkip`, once after it otherwise (the |
| 548 | /// caller decides which by passing `pre_skip`). |
| 549 | fn intra_segment_id( |
| 550 | dec: &mut SymbolDecoder, |
| 551 | cdfs: &mut Cdfs, |
| 552 | mi_r: usize, |
| 553 | mi_c: usize, |
| 554 | w_mi: usize, |
| 555 | h_mi: usize, |
| 556 | skip: bool, |
| 557 | ) { |
| 558 | if !SEG.with(|s| s.borrow().enabled) { |
| 559 | return; |
| 560 | } |
| 561 | let id = read_segment_id(dec, cdfs, mi_r, mi_c, skip); |
| 562 | write_segment_id(mi_r, mi_c, w_mi, h_mi, id); |
| 563 | } |
| 564 | |
| 565 | /// Whether this frame's intra blocks read their `segment_id` before `skip` |
| 566 | /// (`SegIdPreSkip`); `false` also when segmentation is off. |
| 567 | fn seg_id_pre_skip() -> bool { |
| 568 | SEG.with(|s| { |
| 569 | let s = s.borrow(); |
| 570 | s.enabled && s.seg_id_pre_skip |
| 571 | }) |
| 572 | } |
| 573 | |
| 574 | /// `read_inter_segment_id(preskip)` (spec 5.11.9, libaom `decodemv.c:352`). |
| 575 | /// Called at both spec positions of an inter frame's block: `pre_skip = true` |
| 576 | /// before `skip_mode`/`skip`, and `pre_skip = false` after them. |
| 577 | fn inter_segment_id( |
| 578 | dec: &mut SymbolDecoder, |
| 579 | cdfs: &mut Cdfs, |
| 580 | mi_r: usize, |
| 581 | mi_c: usize, |
| 582 | w_mi: usize, |
| 583 | h_mi: usize, |
| 584 | skip: bool, |
| 585 | pre_skip: bool, |
| 586 | ) { |
| 587 | let (enabled, update_map, temporal_update, pre_skip_frame) = SEG.with(|s| { |
| 588 | let s = s.borrow(); |
| 589 | (s.enabled, s.update_map, s.temporal_update, s.seg_id_pre_skip) |
| 590 | }); |
| 591 | if !enabled { |
| 592 | return; |
| 593 | } |
| 594 | if !update_map { |
| 595 | // The map is inherited wholesale from the previous frame. |
| 596 | let id = predicted_segment_id(mi_r, mi_c, w_mi, h_mi); |
| 597 | write_segment_id(mi_r, mi_c, w_mi, h_mi, id); |
| 598 | return; |
| 599 | } |
| 600 | if pre_skip && !pre_skip_frame { |
| 601 | return; |
| 602 | } |
| 603 | if !pre_skip && skip { |
| 604 | // libaom: a skipped block never codes `seg_id_predicted`, and its |
| 605 | // segment id is the spatial prediction. |
| 606 | stamp_seg_pred(mi_r, mi_c, w_mi, h_mi, 0); |
| 607 | let id = read_segment_id(dec, cdfs, mi_r, mi_c, true); |
| 608 | write_segment_id(mi_r, mi_c, w_mi, h_mi, id); |
| 609 | return; |
| 610 | } |
| 611 | let id = if temporal_update { |
| 612 | let ctx = seg_pred_ctx(mi_r, mi_c); |
| 613 | let predicted = dec.symbol(&mut cdfs.segment_pred[ctx]) != 0; |
| 614 | SEG_PRED_HITS.with(|c| c.set(c.get() + 1)); |
| 615 | stamp_seg_pred(mi_r, mi_c, w_mi, h_mi, u8::from(predicted)); |
| 616 | if predicted { |
| 617 | predicted_segment_id(mi_r, mi_c, w_mi, h_mi) |
| 618 | } else { |
| 619 | read_segment_id(dec, cdfs, mi_r, mi_c, false) |
| 620 | } |
| 621 | } else { |
| 622 | read_segment_id(dec, cdfs, mi_r, mi_c, false) |
| 623 | }; |
| 624 | write_segment_id(mi_r, mi_c, w_mi, h_mi, id); |
| 625 | } |
| 626 | |
| 627 | /// `av1_get_pred_context_seg_id`: above + left `seg_id_predicted` flags. |
| 628 | fn seg_pred_ctx(mi_r: usize, mi_c: usize) -> usize { |
| 629 | // libaom reads the flag off `above_mbmi`/`left_mbmi`, which are NULL (and |
| 630 | // so contribute 0) outside the tile -- the availability check here is |
| 631 | // what keeps a previous tile's stamps out of this tile's context. |
| 632 | let (row0, col0) = SEG_TILE_ORIGIN.with(|c| c.get()); |
| 633 | let above = if mi_r > row0 { |
| 634 | ABOVE_SEG_PRED.with(|m| m.borrow().get(mi_c).copied().unwrap_or(0)) |
| 635 | } else { |
| 636 | 0 |
| 637 | }; |
| 638 | let left = if mi_c > col0 { |
| 639 | LEFT_SEG_PRED.with(|m| m.borrow().get(mi_r).copied().unwrap_or(0)) |
| 640 | } else { |
| 641 | 0 |
| 642 | }; |
| 643 | usize::from(above + left) |
| 644 | } |
| 645 | |
| 646 | /// Spec 5.11.9's `AboveSegPredContext`/`LeftSegPredContext` update. |
| 647 | fn stamp_seg_pred(mi_r: usize, mi_c: usize, w_mi: usize, h_mi: usize, value: u8) { |
| 648 | ABOVE_SEG_PRED.with(|m| { |
| 649 | let mut m = m.borrow_mut(); |
| 650 | for i in 0..w_mi { |
| 651 | if let Some(cell) = m.get_mut(mi_c + i) { |
| 652 | *cell = value; |
| 653 | } |
| 654 | } |
| 655 | }); |
| 656 | LEFT_SEG_PRED.with(|m| { |
| 657 | let mut m = m.borrow_mut(); |
| 658 | for i in 0..h_mi { |
| 659 | if let Some(cell) = m.get_mut(mi_r + i) { |
| 660 | *cell = value; |
| 661 | } |
| 662 | } |
| 663 | }); |
| 664 | } |
| 665 | |
| 666 | // How many key-frame luma blocks resolved `smooth_neighbor` (spec |
| 667 | // `get_intra_edge_filter_type`) to `true` -- lane-chroma r2's own before/after |
| 668 | // counter, proving a stream actually put a directional block next to a |
| 669 | // smooth-predicted neighbour rather than every block reading the (previously |
| 670 | // hardcoded `false`) edge-filter strength bucket unchanged. |
| 671 | thread_local! { |
| 672 | static SMOOTH_LUMA_HITS: std::cell::Cell<usize> = |
| 673 | const { std::cell::Cell::new(0) }; |
| 674 | } |
| 675 | |
| 676 | /// Current value of [`SMOOTH_LUMA_HITS`]. |
| 677 | pub(crate) fn smooth_luma_hits() -> usize { |
| 678 | SMOOTH_LUMA_HITS.with(|c| c.get()) |
| 679 | } |
| 680 | |
| 681 | // How many `uv_mode` reads resolved to `SMOOTH_PRED..=PAETH_PRED` (9..=12), |
| 682 | // across every call on the current thread -- lane-chroma r3's own before/after |
| 683 | // counter, proving a stream actually exercised chroma's smooth/paeth |
| 684 | // predictor (the previously-refused round-2 gap) rather than every block |
| 685 | // landing on DC/CFL/directional. |
| 686 | thread_local! { |
| 687 | static SMOOTH_UV_HITS: std::cell::Cell<usize> = |
| 688 | const { std::cell::Cell::new(0) }; |
| 689 | } |
| 690 | |
| 691 | /// Current value of [`SMOOTH_UV_HITS`]. |
| 692 | pub(crate) fn smooth_uv_hits() -> usize { |
| 693 | SMOOTH_UV_HITS.with(|c| c.get()) |
| 694 | } |
| 695 | |
| 696 | // How many tiles [`decode_key_frame_tile_with_cdfs`] has actually decoded a |
| 697 | // full superblock walk over, across every call on the current thread -- |
| 698 | // lane-tiles r2's own gate-blind-to-feature guard: a gate over a |
| 699 | // multi-tile stream must prove more than one tile actually fired, not just |
| 700 | // that the stream carried tile_info.cols > 1. |
| 701 | thread_local! { |
| 702 | static TILE_HITS: std::cell::Cell<usize> = |
| 703 | const { std::cell::Cell::new(0) }; |
| 704 | } |
| 705 | /// Current value of [`TILE_HITS`]. |
| 706 | pub(crate) fn tile_hits() -> usize { |
| 707 | TILE_HITS.with(|c| c.get()) |
| 708 | } |
| 709 | |
| 710 | // How many 4-pixel deblocking edge groups [`edge_params`] has actually |
| 711 | // selected for filtering, across every call on the current thread -- the same |
| 712 | // before/after counter pattern as [`FILTER_INTRA_HITS`], proving a stream |
| 713 | // exercised `apply_deblock` rather than every edge landing on level 0. |
| 714 | thread_local! { |
| 715 | static DEBLOCK_HITS: std::cell::Cell<usize> = |
| 716 | const { std::cell::Cell::new(0) }; |
| 717 | } |
| 718 | /// lane-comppin r4: decode-order picture counter for `EC_AV1_PREFILT_DUMP`, |
| 719 | /// mirroring stream.rs's `pictures_decoded` so the two dumps index |
| 720 | /// identically against aomdec's own `ec_frame_idx` in decodeframe.c. |
| 721 | static PREFILT_PICTURE_IDX: std::sync::atomic::AtomicUsize = |
| 722 | std::sync::atomic::AtomicUsize::new(0); |
| 723 | |
| 724 | /// Current value of [`DEBLOCK_HITS`]. |
| 725 | pub(crate) fn deblock_hits() -> usize { |
| 726 | DEBLOCK_HITS.with(|c| c.get()) |
| 727 | } |
| 728 | |
| 729 | // How many [`read_tx_size`] reads resolved a `tx_depth` strictly less than |
| 730 | // the block's own side, across every call on the current thread -- the same |
| 731 | // before/after counter pattern as [`FILTER_INTRA_HITS`], proving a stream |
| 732 | // actually exercised a *split* transform under `TxMode::Select` rather than |
| 733 | // every block coincidentally resolving `depth=0` (indistinguishable from |
| 734 | // `TxMode::Largest` pixel-wise). |
| 735 | thread_local! { |
| 736 | static TX_DEPTH_HITS: std::cell::Cell<usize> = |
| 737 | const { std::cell::Cell::new(0) }; |
| 738 | } |
| 739 | |
| 740 | // How many plain SQUARE intra blocks (lane-sqchroma r1) resolved a luma |
| 741 | // transform smaller than their own side (`tx_depth != 0`) while coding a |
| 742 | // chroma prediction other than `DC_PRED` -- the pair the square path's chroma |
| 743 | // only exercises with aomenc's tx-size search left ON (its default), which |
| 744 | // every older recipe in this file pins off. Counted at the block, so a stream |
| 745 | // where RD happened to give every split-transform block a DC chroma mode |
| 746 | // reads as zero rather than silently passing for free. |
| 747 | thread_local! { |
| 748 | static SQ_CHROMA_TX_HITS: std::cell::Cell<usize> = |
| 749 | const { std::cell::Cell::new(0) }; |
| 750 | } |
| 751 | |
| 752 | /// Current value of [`SQ_CHROMA_TX_HITS`]. |
| 753 | pub(crate) fn sq_chroma_tx_hits() -> usize { |
| 754 | SQ_CHROMA_TX_HITS.with(|c| c.get()) |
| 755 | } |
| 756 | |
| 757 | /// Current value of [`TX_DEPTH_HITS`]. |
| 758 | pub(crate) fn tx_depth_hits() -> usize { |
| 759 | TX_DEPTH_HITS.with(|c| c.get()) |
| 760 | } |
| 761 | |
| 762 | // lane-txselect: how many `txfm_split` symbols an inter frame's var-tx tree |
| 763 | // read, and how many of those actually took the split, on the current thread |
| 764 | // -- the same before/after counter pattern as [`TX_DEPTH_HITS`], proving a |
| 765 | // stream exercised spec 5.11.17's `read_var_tx_size` rather than every block |
| 766 | // coincidentally coding one whole-block transform. |
| 767 | thread_local! { |
| 768 | static TXFM_SPLIT_READS: std::cell::Cell<usize> = const { std::cell::Cell::new(0) }; |
| 769 | static TXFM_SPLIT_HITS: std::cell::Cell<usize> = const { std::cell::Cell::new(0) }; |
| 770 | // This frame's own `tx_mode == TxMode::Select` and `reduced_tx_set` bits, |
| 771 | // set once per inter tile by [`decode_inter_frame_tile_with_cdfs`] -- |
| 772 | // `decode_inter_block` already carries forty parameters and both are |
| 773 | // frame-constant. |
| 774 | static TX_SELECT_INTER: std::cell::Cell<bool> = const { std::cell::Cell::new(false) }; |
| 775 | static REDUCED_TX_SET_INTER: std::cell::Cell<bool> = const { std::cell::Cell::new(true) }; |
| 776 | } |
| 777 | |
| 778 | /// Current value of [`TXFM_SPLIT_READS`]. |
| 779 | pub(crate) fn txfm_split_reads() -> usize { |
| 780 | TXFM_SPLIT_READS.with(|c| c.get()) |
| 781 | } |
| 782 | |
| 783 | /// Current value of [`TXFM_SPLIT_HITS`]. |
| 784 | pub(crate) fn txfm_split_hits() -> usize { |
| 785 | TXFM_SPLIT_HITS.with(|c| c.get()) |
| 786 | } |
| 787 | |
| 788 | /// Sets the current thread's [`TX_SELECT_INTER`]/[`REDUCED_TX_SET_INTER`]. |
| 789 | pub(crate) fn set_inter_tx_mode(tx_select: bool, reduced_tx_set: bool) { |
| 790 | TX_SELECT_INTER.with(|c| c.set(tx_select)); |
| 791 | REDUCED_TX_SET_INTER.with(|c| c.set(reduced_tx_set)); |
| 792 | } |
| 793 | |
| 794 | // How many `partition_w16` reads (a 16x16 block's own partition symbol, key |
| 795 | // intra-frame path) resolved to `PARTITION_VERT_B` -- lane-rect16 r1: a |
| 796 | // plain default-settings aomenc run over lavfi mandelbrot hits this arm on |
| 797 | // frame 0's very first non-NONE/SPLIT `partition_w16` symbol (mi=(3,2)), not |
| 798 | // the plain HORZ/VERT this lane's charter assumed. Left rect (8x16, real |
| 799 | // `decode_block_rect`) + two 8x8 leaves stacked on the right (`decode_leaf8`, |
| 800 | // chained via `prev_leaf` exactly like the existing 16x16-SPLIT-into-4x8x8 |
| 801 | // path above). |
| 802 | thread_local! { |
| 803 | static VERT_B_INTRA_HITS: std::cell::Cell<usize> = |
| 804 | const { std::cell::Cell::new(0) }; |
| 805 | } |
| 806 | |
| 807 | /// Current value of [`VERT_B_INTRA_HITS`]. |
| 808 | pub(crate) fn vert_b_intra_hits() -> usize { |
| 809 | VERT_B_INTRA_HITS.with(|c| c.get()) |
| 810 | } |
| 811 | |
| 812 | // How many `partition_w16` reads resolved to a plain `PARTITION_HORZ`/ |
| 813 | // `PARTITION_VERT` (lane-rect16 r2): re-measuring after the VERT_B fix above |
| 814 | // showed mandelbrot's real first-hit blocker is this plain arm at mi=(0,7), |
| 815 | // not VERT_B (`refusal-names-a-correlate`; the VERT_B write-up's "frame 0's |
| 816 | // very first non-NONE/SPLIT symbol" claim was about decode ORDER within one |
| 817 | // superblock's z-order recursion, not raster mi position). |
| 818 | thread_local! { |
| 819 | static HORZ_VERT_INTRA_HITS: std::cell::Cell<usize> = |
| 820 | const { std::cell::Cell::new(0) }; |
| 821 | } |
| 822 | |
| 823 | /// Current value of [`HORZ_VERT_INTRA_HITS`]. |
| 824 | pub(crate) fn horz_vert_intra_hits() -> usize { |
| 825 | HORZ_VERT_INTRA_HITS.with(|c| c.get()) |
| 826 | } |
| 827 | |
| 828 | // How many HORZ/VERT intra strips resolved a SPLIT luma transform |
| 829 | // (`tx_depth != 0`) and decoded it per transform unit (lane-rectsplit r1) -- |
| 830 | // the case every rect path here refused by name before, and the first |
| 831 | // refusal the user's ReelB extract (600s in) stops at. |
| 832 | thread_local! { |
| 833 | static RECT_SPLIT_TX_HITS: std::cell::Cell<usize> = |
| 834 | const { std::cell::Cell::new(0) }; |
| 835 | } |
| 836 | |
| 837 | /// Current value of [`RECT_SPLIT_TX_HITS`]. |
| 838 | pub(crate) fn rect_split_tx_hits() -> usize { |
| 839 | RECT_SPLIT_TX_HITS.with(|c| c.get()) |
| 840 | } |
| 841 | |
| 842 | // The subset of [`RECT_SPLIT_TX_HITS`] that is BOTH superblock-level (one |
| 843 | // side 64) AND split past the first depth, i.e. tiled more than one unit in |
| 844 | // each axis -- the only shape with a transform unit that has neighbours on |
| 845 | // two sides inside its own strip (`row_off > 0 && col_off > 0`), and the one |
| 846 | // no gate fired before lane-rectsplit r2 (the shared counter above also |
| 847 | // counts 32x32-level and depth-1 strips, so it cannot prove this case). |
| 848 | thread_local! { |
| 849 | static RECT_SPLIT_SB_INTERIOR_TU_HITS: std::cell::Cell<usize> = |
| 850 | const { std::cell::Cell::new(0) }; |
| 851 | } |
| 852 | |
| 853 | /// Current value of [`RECT_SPLIT_SB_INTERIOR_TU_HITS`]. |
| 854 | pub(crate) fn rect_split_sb_interior_tu_hits() -> usize { |
| 855 | RECT_SPLIT_SB_INTERIOR_TU_HITS.with(|c| c.get()) |
| 856 | } |
| 857 | |
| 858 | // How many HORZ/VERT intra strips actually predicted with filter intra |
| 859 | // (lane-rectsplit r1) -- the case `decode_block_rect` refused by name |
| 860 | // ("this decoder predicts square-only") until `predict_filter_intra` took |
| 861 | // `bw`x`bh`. |
| 862 | thread_local! { |
| 863 | static FILTER_INTRA_RECT_HITS: std::cell::Cell<usize> = |
| 864 | const { std::cell::Cell::new(0) }; |
| 865 | } |
| 866 | |
| 867 | /// Current value of [`FILTER_INTRA_RECT_HITS`]. |
| 868 | pub(crate) fn filter_intra_rect_hits() -> usize { |
| 869 | FILTER_INTRA_RECT_HITS.with(|c| c.get()) |
| 870 | } |
| 871 | |
| 872 | // How many [`read_coeffs`] reads resolved a `tx_type` outside `TX_CLASS_2D` |
| 873 | // (`V_DCT`/`H_DCT`, spec 5.11.39's `TxClass::Horiz`/`Vert`), across every |
| 874 | // call on the current thread -- the same before/after counter pattern as |
| 875 | // [`TX_DEPTH_HITS`], proving a stream actually exercised the class-split |
| 876 | // `eob_pt`/1D-neighbour context path rather than every block coincidentally |
| 877 | // landing on `DCT_DCT`. |
| 878 | thread_local! { |
| 879 | static TX_CLASS1_HITS: std::cell::Cell<usize> = |
| 880 | const { std::cell::Cell::new(0) }; |
| 881 | } |
| 882 | |
| 883 | // How many `uv_mode` reads resolved to a directional chroma mode (neither |
| 884 | // `DC_PRED` nor `UV_CFL_PRED`), across every call on the current thread -- the same |
| 885 | // before/after counter pattern as [`FILTER_INTRA_HITS`], proving a stream |
| 886 | // actually exercised chroma's own directional predictor. |
| 887 | thread_local! { |
| 888 | static DIRECTIONAL_UV_HITS: std::cell::Cell<usize> = |
| 889 | const { std::cell::Cell::new(0) }; |
| 890 | } |
| 891 | |
| 892 | /// Current value of [`DIRECTIONAL_UV_HITS`]. |
| 893 | pub(crate) fn directional_uv_hits() -> usize { |
| 894 | DIRECTIONAL_UV_HITS.with(|c| c.get()) |
| 895 | } |
| 896 | |
| 897 | // How many `angle_delta`/`angle_delta_uv` symbols this decoder has read as |
| 898 | // something other than [`ANGLE_DELTA_ZERO`], across every call in the |
| 899 | // process -- the same before/after counter pattern as [`FILTER_INTRA_HITS`], |
| 900 | // proving a stream actually exercised a nonzero angle delta. |
| 901 | thread_local! { |
| 902 | static ANGLE_DELTA_HITS: std::cell::Cell<usize> = |
| 903 | const { std::cell::Cell::new(0) }; |
| 904 | } |
| 905 | |
| 906 | // How many [`read_single_ref`] reads resolved to a reference other than |
| 907 | // `LAST_FRAME`, across every call on the current thread -- the same before/after |
| 908 | // counter pattern as [`FILTER_INTRA_HITS`], proving a stream actually |
| 909 | // exercised a non-`LAST_FRAME` reference (`GOLDEN_FRAME`, this decoder's |
| 910 | // only other supported one so far) rather than every block coincidentally |
| 911 | // resolving `LAST_FRAME`. |
| 912 | thread_local! { |
| 913 | static NON_LAST_REF_HITS: std::cell::Cell<usize> = |
| 914 | const { std::cell::Cell::new(0) }; |
| 915 | } |
| 916 | |
| 917 | /// Current value of [`NON_LAST_REF_HITS`]. |
| 918 | pub(crate) fn non_last_ref_hits() -> usize { |
| 919 | NON_LAST_REF_HITS.with(|c| c.get()) |
| 920 | } |
| 921 | |
| 922 | // Per-reference [`NON_LAST_REF_HITS`] breakdown (lane-av1refs): one counter |
| 923 | // per `MV_REFERENCE_FRAME` past `LAST_FRAME`, so a gate targeting one |
| 924 | // specific reference (`LAST2`/`LAST3`/`GOLDEN`/`BWDREF`/`ALTREF2`/`ALTREF`) |
| 925 | // can prove THAT reference fired rather than any non-`LAST_FRAME` one -- |
| 926 | // `NON_LAST_REF_HITS` alone cannot tell a `LAST2_FRAME` gate from a |
| 927 | // `GOLDEN_FRAME` draw it did not ask for. |
| 928 | thread_local! { |
| 929 | static LAST2_HITS: std::cell::Cell<usize> = |
| 930 | const { std::cell::Cell::new(0) }; |
| 931 | } |
| 932 | thread_local! { |
| 933 | static LAST3_HITS: std::cell::Cell<usize> = |
| 934 | const { std::cell::Cell::new(0) }; |
| 935 | } |
| 936 | thread_local! { |
| 937 | static GOLDEN_HITS: std::cell::Cell<usize> = |
| 938 | const { std::cell::Cell::new(0) }; |
| 939 | } |
| 940 | thread_local! { |
| 941 | static BWDREF_HITS: std::cell::Cell<usize> = |
| 942 | const { std::cell::Cell::new(0) }; |
| 943 | } |
| 944 | thread_local! { |
| 945 | static ALTREF2_HITS: std::cell::Cell<usize> = |
| 946 | const { std::cell::Cell::new(0) }; |
| 947 | } |
| 948 | thread_local! { |
| 949 | static ALTREF_HITS: std::cell::Cell<usize> = |
| 950 | const { std::cell::Cell::new(0) }; |
| 951 | } |
| 952 | |
| 953 | // How many [`read_comp_mode`] reads resolved `COMPOUND_REFERENCE` (lane-av1comp) |
| 954 | // -- proves a `reference_select` stream actually reached a block that picked |
| 955 | // compound, not just that the header bit was set. Every such block is |
| 956 | // refused after this fires ([`decode_inter_block`]/[`decode_inter_block8`]), |
| 957 | // so this only ever counts attempted reads, never a completed decode. |
| 958 | thread_local! { |
| 959 | static COMP_MODE_HITS: std::cell::Cell<usize> = |
| 960 | const { std::cell::Cell::new(0) }; |
| 961 | } |
| 962 | |
| 963 | /// Current value of [`COMP_MODE_HITS`]. |
| 964 | pub(crate) fn comp_mode_hits() -> usize { |
| 965 | COMP_MODE_HITS.with(|c| c.get()) |
| 966 | } |
| 967 | |
| 968 | // How many blocks [`read_intra_mode`] decoded a real (nonzero-size) palette-Y |
| 969 | // use for, across every call on the current thread -- the same before/after |
| 970 | // counter pattern as [`FILTER_INTRA_HITS`], proving a stream actually |
| 971 | // exercised palette reconstruction rather than only reading and refusing the |
| 972 | // `palette_y_mode` symbol. |
| 973 | thread_local! { |
| 974 | static PALETTE_HITS: std::cell::Cell<usize> = const { std::cell::Cell::new(0) }; |
| 975 | } |
| 976 | |
| 977 | /// Current value of [`PALETTE_HITS`]. |
| 978 | pub(crate) fn palette_hits() -> usize { |
| 979 | PALETTE_HITS.with(|c| c.get()) |
| 980 | } |
| 981 | |
| 982 | // lane-palette2 r1: as [`PALETTE_HITS`], for a real (nonzero-size) chroma |
| 983 | // palette use -- proves a stream actually reconstructed UV palette pixels, |
| 984 | // not just read and refused the `palette_uv_mode` symbol. |
| 985 | thread_local! { |
| 986 | static PALETTE_UV_HITS: std::cell::Cell<usize> = const { std::cell::Cell::new(0) }; |
| 987 | } |
| 988 | |
| 989 | /// Current value of [`PALETTE_UV_HITS`]. |
| 990 | pub(crate) fn palette_uv_hits() -> usize { |
| 991 | PALETTE_UV_HITS.with(|c| c.get()) |
| 992 | } |
| 993 | |
| 994 | // lane-palette2 r1: how many HORZ/VERT rect intra strips ([`read_intra_mode_rect`]) |
| 995 | // decoded a real palette-Y use -- proves the rect strip's own palette syntax |
| 996 | // (not just the square path's) actually fired. |
| 997 | thread_local! { |
| 998 | static PALETTE_RECT_HITS: std::cell::Cell<usize> = const { std::cell::Cell::new(0) }; |
| 999 | } |
| 1000 | |
| 1001 | /// Current value of [`PALETTE_RECT_HITS`]. |
| 1002 | pub(crate) fn palette_rect_hits() -> usize { |
| 1003 | PALETTE_RECT_HITS.with(|c| c.get()) |
| 1004 | } |
| 1005 | |
| 1006 | // lane-palette2 r1: how many palette-Y blocks decoded with a split luma |
| 1007 | // transform (`tx_select && logical_tx != side`) -- proves the per-TU index-map |
| 1008 | // slicing actually ran, not just that the whole-block path stayed exact. |
| 1009 | thread_local! { |
| 1010 | static PALETTE_SPLIT_TX_HITS: std::cell::Cell<usize> = const { std::cell::Cell::new(0) }; |
| 1011 | } |
| 1012 | |
| 1013 | /// Current value of [`PALETTE_SPLIT_TX_HITS`]. |
| 1014 | pub(crate) fn palette_split_tx_hits() -> usize { |
| 1015 | PALETTE_SPLIT_TX_HITS.with(|c| c.get()) |
| 1016 | } |
| 1017 | |
| 1018 | thread_local! { |
| 1019 | /// A just-decoded palette-Y block's own predicted pixels |
| 1020 | /// (`side*side`, row-major, `colors[map[i]]`), consumed by |
| 1021 | /// [`PlaneBuf::reconstruct`]'s next call in place of `predict()` -- the |
| 1022 | /// same off-the-call-stack idiom as [`ENABLE_EDGE_FILTER`] (threading a |
| 1023 | /// per-block override through `read_plane`'s two dozen call sites is the |
| 1024 | /// alternative this avoids). Always empty again once that call consumes |
| 1025 | /// it (`.take()`), so a non-palette block's own `reconstruct` call never |
| 1026 | /// sees stale state. |
| 1027 | static PALETTE_PRED: std::cell::RefCell<Option<Vec<u16>>> = |
| 1028 | const { std::cell::RefCell::new(None) }; |
| 1029 | } |
| 1030 | |
| 1031 | thread_local! { |
| 1032 | /// The current frame's `mi` grid, kept only while a frame header set |
| 1033 | /// `allow_intrabc` -- an intrabc block's DV predictor is the ordinary |
| 1034 | /// MV stack ([`crate::mvstack::find_mv_stack`]) built against |
| 1035 | /// `INTRA_FRAME`, which needs the neighbour state a key frame otherwise |
| 1036 | /// never keeps. `(grid, mi_cols, mi_rows)`; `None` on every frame that |
| 1037 | /// does not allow intrabc (so nothing is paid for it). |
| 1038 | /// |
| 1039 | /// corner-cut: only [`decode_block`] (the square path) records into it, |
| 1040 | /// so a frame mixing intrabc with rect/sub-8x8 leaves would predict off |
| 1041 | /// an incomplete grid. Ceiling: the DV *value* (never the parse) would |
| 1042 | /// be wrong there; upgrade = record from the rect/leaf paths too. |
| 1043 | static INTRABC_MI_GRID: std::cell::RefCell<Option<(crate::mvstack::MiGrid, usize, usize)>> = |
| 1044 | const { std::cell::RefCell::new(None) }; |
| 1045 | /// The block vector [`read_intra_mode`] just decoded, handed to |
| 1046 | /// [`decode_block`] off the call stack (same idiom as [`PALETTE_PRED`], |
| 1047 | /// which the alternative -- a tenth tuple member through every caller -- |
| 1048 | /// already avoids). Always `.take()`n by the block that reads it. |
| 1049 | static INTRABC_DV: std::cell::Cell<Option<(i32, i32)>> = const { std::cell::Cell::new(None) }; |
| 1050 | /// How many blocks decoded `use_intrabc == 1` -- the gate's hit counter. |
| 1051 | static INTRABC_HITS: std::cell::Cell<usize> = const { std::cell::Cell::new(0) }; |
| 1052 | } |
| 1053 | |
| 1054 | /// Current value of [`INTRABC_HITS`]. |
| 1055 | pub(crate) fn intrabc_hits() -> usize { |
| 1056 | INTRABC_HITS.with(|c| c.get()) |
| 1057 | } |
| 1058 | |
| 1059 | /// Zeroes [`INTRABC_HITS`] (gate tests call this before a decode). |
| 1060 | pub(crate) fn reset_intrabc_hits() { |
| 1061 | INTRABC_HITS.with(|c| c.set(0)); |
| 1062 | } |
| 1063 | |
| 1064 | /// Records one coded block into [`INTRABC_MI_GRID`]: every block contributes |
| 1065 | /// its size (libaom's `scan_row_mbmi` advances `processed_rows` off any |
| 1066 | /// candidate's `bsize`, ref-match or not), an intrabc one also its DV as an |
| 1067 | /// `INTRA_FRAME` candidate (libaom treats intrabc as `is_inter_block`). |
| 1068 | fn record_intrabc_mi(mi_r: usize, mi_c: usize, n4: usize, dv: Option<(i32, i32)>) { |
| 1069 | INTRABC_MI_GRID.with(|g| { |
| 1070 | let mut g = g.borrow_mut(); |
| 1071 | let Some((grid, _, _)) = g.as_mut() else { |
| 1072 | return; |
| 1073 | }; |
| 1074 | let info = crate::mvstack::MiInfo { |
| 1075 | is_inter: dv.is_some(), |
| 1076 | ref_frame: 0, |
| 1077 | ref_frame1: None, |
| 1078 | mv: dv.unwrap_or((0, 0)), |
| 1079 | mv1: None, |
| 1080 | is_new_mv: dv.is_some(), |
| 1081 | is_global_mv0: false, |
| 1082 | is_global_mv1: false, |
| 1083 | size: n4, |
| 1084 | size_h: n4, |
| 1085 | }; |
| 1086 | for r in mi_r..mi_r + n4 { |
| 1087 | for c in mi_c..mi_c + n4 { |
| 1088 | grid.set(r, c, info); |
| 1089 | } |
| 1090 | } |
| 1091 | }); |
| 1092 | } |
| 1093 | |
| 1094 | // How many blocks decoded `skip_mode == 1` (lane-av1comp round 14) -- |
| 1095 | // proves a real `skip_mode_present` stream actually reached a block that |
| 1096 | // picked it, not just that the frame header bit was set. |
| 1097 | thread_local! { |
| 1098 | static SKIP_MODE_HITS: std::cell::Cell<usize> = |
| 1099 | const { std::cell::Cell::new(0) }; |
| 1100 | } |
| 1101 | |
| 1102 | /// Current value of [`SKIP_MODE_HITS`]. |
| 1103 | pub(crate) fn skip_mode_hits() -> usize { |
| 1104 | SKIP_MODE_HITS.with(|c| c.get()) |
| 1105 | } |
| 1106 | |
| 1107 | // lane-motionmode: how many blocks actually decoded `obmc_selected == true` |
| 1108 | // (as opposed to just reaching an eligible `read_motion_mode` symbol read) |
| 1109 | // -- the gate's own proof that a real aomenc `--enable-obmc=1` stream |
| 1110 | // reached [`obmc_blend`], not just the header bit. |
| 1111 | thread_local! { |
| 1112 | static OBMC_HITS: std::cell::Cell<usize> = |
| 1113 | const { std::cell::Cell::new(0) }; |
| 1114 | } |
| 1115 | |
| 1116 | // lane-fimv r1 (class wrong-alphabet-same-value): how many blocks took the |
| 1117 | // 2-symbol `obmc_cdf` alphabet *because* `force_integer_mv` (or a scaled |
| 1118 | // reference) vetoed `WARPED_CAUSAL` in libaom `motion_mode_allowed`, while |
| 1119 | // `allow_warped_motion == 1` and `num_proj_ref >= 1` would otherwise have |
| 1120 | // picked the 3-symbol `motion_mode_cdf`. These are exactly the blocks this |
| 1121 | // decoder used to read with the wrong alphabet, so a gate that asserts this |
| 1122 | // counter advanced proves the fixed path fired (class |
| 1123 | // `gate-blind-to-feature`). |
| 1124 | thread_local! { |
| 1125 | static FIMV_ALPHABET_HITS: std::cell::Cell<usize> = const { std::cell::Cell::new(0) }; |
| 1126 | } |
| 1127 | |
| 1128 | /// Current value of [`FIMV_ALPHABET_HITS`]. |
| 1129 | pub fn fimv_alphabet_hits() -> usize { |
| 1130 | FIMV_ALPHABET_HITS.with(|c| c.get()) |
| 1131 | } |
| 1132 | |
| 1133 | // lane-fimv r1: how many frame headers this decoder has decoded with |
| 1134 | /// `force_integer_mv == 1` (bumped by `stream.rs` per frame). Lets a gate |
| 1135 | // prove the encoder really produced the header state under test rather than |
| 1136 | // assume it. |
| 1137 | thread_local! { |
| 1138 | static FIMV_FRAME_HITS: std::cell::Cell<usize> = const { std::cell::Cell::new(0) }; |
| 1139 | } |
| 1140 | |
| 1141 | /// Current value of [`FIMV_FRAME_HITS`]. |
| 1142 | pub fn fimv_frame_hits() -> usize { |
| 1143 | FIMV_FRAME_HITS.with(|c| c.get()) |
| 1144 | } |
| 1145 | |
| 1146 | /// Bump [`FIMV_FRAME_HITS`] -- called once per decoded frame header whose |
| 1147 | /// `force_integer_mv` bit is set. |
| 1148 | pub fn note_force_integer_mv_frame() { |
| 1149 | FIMV_FRAME_HITS.with(|c| c.set(c.get() + 1)); |
| 1150 | } |
| 1151 | |
| 1152 | /// Current value of [`OBMC_HITS`]. |
| 1153 | pub(crate) fn obmc_hits() -> usize { |
| 1154 | OBMC_HITS.with(|c| c.get()) |
| 1155 | } |
| 1156 | |
| 1157 | // lane-scaledref r1: per-case firing counts for the scaled-reference gate |
| 1158 | // (class `gate-blind-to-feature`) -- a pixel match under an UNSCALED |
| 1159 | // reference would pass just as well and prove nothing, so the gate |
| 1160 | // hard-asserts each combination this round lifts actually occurred: |
| 1161 | // a compound block with a scaled tap, a warp block whose warp libaom |
| 1162 | // suppresses because the reference is scaled, an OBMC block, an interintra |
| 1163 | // block, and an 8x8 leaf. |
| 1164 | thread_local! { |
| 1165 | static SCALED_COMPOUND_HITS: std::cell::Cell<usize> = const { std::cell::Cell::new(0) }; |
| 1166 | static SCALED_WARP_FALLBACK_HITS: std::cell::Cell<usize> = const { std::cell::Cell::new(0) }; |
| 1167 | static SCALED_OBMC_HITS: std::cell::Cell<usize> = const { std::cell::Cell::new(0) }; |
| 1168 | static SCALED_INTERINTRA_HITS: std::cell::Cell<usize> = const { std::cell::Cell::new(0) }; |
| 1169 | static SCALED_BLOCK8_HITS: std::cell::Cell<usize> = const { std::cell::Cell::new(0) }; |
| 1170 | static SCALED_WARP_SUPPRESSED_HITS: std::cell::Cell<usize> = const { std::cell::Cell::new(0) }; |
| 1171 | static MIXED_SCALE_COMPOUND_HITS: std::cell::Cell<usize> = const { std::cell::Cell::new(0) }; |
| 1172 | } |
| 1173 | |
| 1174 | /// Current value of [`MIXED_SCALE_COMPOUND_HITS`]: compound blocks with one |
| 1175 | /// scaled and one unscaled tap. |
| 1176 | pub(crate) fn mixed_scale_compound_hits() -> usize { |
| 1177 | MIXED_SCALE_COMPOUND_HITS.with(|c| c.get()) |
| 1178 | } |
| 1179 | |
| 1180 | /// Current value of [`SCALED_WARP_SUPPRESSED_HITS`]: blocks that would have |
| 1181 | /// read the 3-symbol `motion_mode_cdf` but read the 2-symbol `obmc_cdf` |
| 1182 | /// instead because their reference is scaled (libaom `motion_mode_allowed`, |
| 1183 | /// `blockd.h:1484`). |
| 1184 | pub(crate) fn scaled_warp_suppressed_hits() -> usize { |
| 1185 | SCALED_WARP_SUPPRESSED_HITS.with(|c| c.get()) |
| 1186 | } |
| 1187 | |
| 1188 | /// Current value of [`SCALED_COMPOUND_HITS`]. |
| 1189 | pub(crate) fn scaled_compound_hits() -> usize { |
| 1190 | SCALED_COMPOUND_HITS.with(|c| c.get()) |
| 1191 | } |
| 1192 | |
| 1193 | /// Current value of [`SCALED_WARP_FALLBACK_HITS`]. |
| 1194 | pub(crate) fn scaled_warp_fallback_hits() -> usize { |
| 1195 | SCALED_WARP_FALLBACK_HITS.with(|c| c.get()) |
| 1196 | } |
| 1197 | |
| 1198 | /// Current value of [`SCALED_OBMC_HITS`]. |
| 1199 | pub(crate) fn scaled_obmc_hits() -> usize { |
| 1200 | SCALED_OBMC_HITS.with(|c| c.get()) |
| 1201 | } |
| 1202 | |
| 1203 | /// Current value of [`SCALED_INTERINTRA_HITS`]. |
| 1204 | pub(crate) fn scaled_interintra_hits() -> usize { |
| 1205 | SCALED_INTERINTRA_HITS.with(|c| c.get()) |
| 1206 | } |
| 1207 | |
| 1208 | /// Current value of [`SCALED_BLOCK8_HITS`]. |
| 1209 | pub(crate) fn scaled_block8_hits() -> usize { |
| 1210 | SCALED_BLOCK8_HITS.with(|c| c.get()) |
| 1211 | } |
| 1212 | |
| 1213 | // lane-motionmode round 3: same proof as [`OBMC_HITS`], narrowed to |
| 1214 | // `decode_inter_block8`'s own 8x8-leaf `read_motion_mode` -- a subset of |
| 1215 | // [`OBMC_HITS`] (both fire together on an 8x8 hit), lets the gate tell an |
| 1216 | // 8x8-leaf OBMC block apart from a 16x16+ one. |
| 1217 | thread_local! { |
| 1218 | static OBMC_HITS_8: std::cell::Cell<usize> = const { std::cell::Cell::new(0) }; |
| 1219 | } |
| 1220 | |
| 1221 | // lane-warp round 1: how many blocks resolved `motion_mode_allowed` to |
| 1222 | // `WARPED_CAUSAL`-eligible (3-symbol read, `num_proj_ref >= 1` under |
| 1223 | // `allow_warped_motion`) AND the symbol actually decoded to `WARPED_CAUSAL` |
| 1224 | // -- the gate's proof that a fixture really exercises the alphabet this |
| 1225 | // round changed, even though the block itself still refuses (warp |
| 1226 | // estimation/filter not ported). |
| 1227 | thread_local! { |
| 1228 | static WARP_SELECTED_HITS: std::cell::Cell<usize> = |
| 1229 | const { std::cell::Cell::new(0) }; |
| 1230 | } |
| 1231 | |
| 1232 | /// Current value of [`WARP_SELECTED_HITS`]. |
| 1233 | pub(crate) fn warp_selected_hits() -> usize { |
| 1234 | WARP_SELECTED_HITS.with(|c| c.get()) |
| 1235 | } |
| 1236 | |
| 1237 | // lane-cwarp r1: how many COMPOUND_REFERENCE blocks had at least one |
| 1238 | // reference predicted through the per-ref GLOBAL warp (`av1_warp_plane` |
| 1239 | // into the compound intermediate) rather than translationally -- the |
| 1240 | // gate's proof that a `GLOBAL_GLOBALMV` block under a ROTZOOM/AFFINE model |
| 1241 | // really reached [`crate::warp::warp_affine_compound`]. |
| 1242 | thread_local! { |
| 1243 | static COMPOUND_WARP_HITS: std::cell::Cell<usize> = |
| 1244 | const { std::cell::Cell::new(0) }; |
| 1245 | } |
| 1246 | |
| 1247 | /// Current value of [`COMPOUND_WARP_HITS`]. |
| 1248 | pub(crate) fn compound_warp_hits() -> usize { |
| 1249 | COMPOUND_WARP_HITS.with(|c| c.get()) |
| 1250 | } |
| 1251 | |
| 1252 | // How many blocks decoded `interintra == 1` with the non-wedge blended |
| 1253 | // prediction applied (lane-interintra r1) -- the gate's proof that a |
| 1254 | // stream actually exercised interintra. |
| 1255 | thread_local! { |
| 1256 | static INTERINTRA_HITS: std::cell::Cell<usize> = |
| 1257 | const { std::cell::Cell::new(0) }; |
| 1258 | } |
| 1259 | |
| 1260 | /// Current value of [`INTERINTRA_HITS`]. |
| 1261 | pub(crate) fn interintra_hits() -> usize { |
| 1262 | INTERINTRA_HITS.with(|c| c.get()) |
| 1263 | } |
| 1264 | |
| 1265 | // How many blocks decoded `comp_group_idx == 1` (masked COMPOUND_REFERENCE, |
| 1266 | // wedge or diffwtd) and had `compound_type`/`wedge_idx`/`wedge_sign`/ |
| 1267 | // `mask_type` consumed entropy-exact -- lane-maskcomp r1's proof that a |
| 1268 | // gate fixture actually exercises this alphabet, even though the block |
| 1269 | // still refuses (the mask blend itself is unported). |
| 1270 | thread_local! { |
| 1271 | static MASKED_COMPOUND_HITS: std::cell::Cell<usize> = |
| 1272 | const { std::cell::Cell::new(0) }; |
| 1273 | } |
| 1274 | |
| 1275 | /// Current value of [`MASKED_COMPOUND_HITS`]. |
| 1276 | pub(crate) fn masked_compound_hits() -> usize { |
| 1277 | MASKED_COMPOUND_HITS.with(|c| c.get()) |
| 1278 | } |
| 1279 | |
| 1280 | // How many blocks decoded a real `COMPOUND_WEDGE` block (`comp_group_idx == |
| 1281 | // 1`, `compound_type == 0`) -- lane-wedge r3's proof a gate fixture |
| 1282 | // actually exercised the wedge blend, not just the DIFFWTD half of |
| 1283 | // `MASKED_COMPOUND_HITS`. |
| 1284 | thread_local! { |
| 1285 | static WEDGE_HITS: std::cell::Cell<usize> = |
| 1286 | const { std::cell::Cell::new(0) }; |
| 1287 | } |
| 1288 | |
| 1289 | /// Current value of [`WEDGE_HITS`]. |
| 1290 | pub(crate) fn wedge_hits() -> usize { |
| 1291 | WEDGE_HITS.with(|c| c.get()) |
| 1292 | } |
| 1293 | |
| 1294 | // How many blocks decoded a real wedge-INTERINTRA block |
| 1295 | // (`interintra == 1 && use_wedge_interintra == 1`) -- lane-wii r2's proof |
| 1296 | // a gate fixture exercised the wedge blend (fixed sign 0), not just the |
| 1297 | // smooth arm counted by `INTERINTRA_HITS`. |
| 1298 | thread_local! { |
| 1299 | static WII_HITS: std::cell::Cell<usize> = |
| 1300 | const { std::cell::Cell::new(0) }; |
| 1301 | } |
| 1302 | |
| 1303 | /// Current value of [`WII_HITS`]. |
| 1304 | pub(crate) fn wii_hits() -> usize { |
| 1305 | WII_HITS.with(|c| c.get()) |
| 1306 | } |
| 1307 | |
| 1308 | /// Current value of [`OBMC_HITS_8`]. |
| 1309 | pub(crate) fn obmc_hits_8() -> usize { |
| 1310 | OBMC_HITS_8.with(|c| c.get()) |
| 1311 | } |
| 1312 | |
| 1313 | // lane-rectgate r1: how many 32x32 quadrants actually decoded a |
| 1314 | // `PARTITION_HORZ_B` split (the one extended/ab partition this decoder's |
| 1315 | // inter-frame tile loop decodes -- `PARTITION_HORZ`/`VERT`/`HORZ_A`/`VERT_A`/ |
| 1316 | // `VERT_B` all still fall into the generic "a partition type this encoder |
| 1317 | // never writes" refusal below) -- the gate's proof that a free-partition |
| 1318 | // aomenc stream actually exercised a non-`NONE`/`SPLIT` partition rather than |
| 1319 | // coincidentally never selecting one. |
| 1320 | thread_local! { |
| 1321 | static EXTENDED_PARTITION_HITS: std::cell::Cell<usize> = |
| 1322 | const { std::cell::Cell::new(0) }; |
| 1323 | } |
| 1324 | |
| 1325 | /// Current value of [`EXTENDED_PARTITION_HITS`]. |
| 1326 | pub(crate) fn extended_partition_hits() -> usize { |
| 1327 | EXTENDED_PARTITION_HITS.with(|c| c.get()) |
| 1328 | } |
| 1329 | |
| 1330 | // lane-partitions r1: how many 32x32 quadrants decoded a real |
| 1331 | // `PARTITION_HORZ`/`PARTITION_VERT` (two true rectangular strips, unlike |
| 1332 | // `PARTITION_HORZ_B`'s square-context stand-in) -- the free-partition |
| 1333 | // gate's proof this round's arms actually fired. |
| 1334 | thread_local! { |
| 1335 | static RECT_PARTITION_HITS: std::cell::Cell<usize> = |
| 1336 | const { std::cell::Cell::new(0) }; |
| 1337 | } |
| 1338 | |
| 1339 | /// Current value of [`RECT_PARTITION_HITS`]. |
| 1340 | pub(crate) fn rect_partition_hits() -> usize { |
| 1341 | RECT_PARTITION_HITS.with(|c| c.get()) |
| 1342 | } |
| 1343 | |
| 1344 | // lane-inter8 r1: how many superblocks an inter tile coded as a single |
| 1345 | // whole-SB `PARTITION_NONE` 64x64 inter block (the case aomenc picks for |
| 1346 | // static content, previously a named refusal). |
| 1347 | thread_local! { |
| 1348 | static INTER_SB_NONE_HITS: std::cell::Cell<usize> = |
| 1349 | const { std::cell::Cell::new(0) }; |
| 1350 | } |
| 1351 | |
| 1352 | /// Current value of [`INTER_SB_NONE_HITS`]. |
| 1353 | pub(crate) fn inter_sb_none_hits() -> usize { |
| 1354 | INTER_SB_NONE_HITS.with(|c| c.get()) |
| 1355 | } |
| 1356 | |
| 1357 | // lane-inter8 r1: how many INTERIOR (not frame-edge-straddling) 16x16 inter |
| 1358 | // blocks coded a real `PARTITION_SPLIT` into four 8x8 leaves -- the straddle |
| 1359 | // path has always run that loop, so only this counter proves the newly-lifted |
| 1360 | // alphabet value fired. |
| 1361 | thread_local! { |
| 1362 | static INTER_SUB16_SPLIT_HITS: std::cell::Cell<usize> = |
| 1363 | const { std::cell::Cell::new(0) }; |
| 1364 | } |
| 1365 | |
| 1366 | /// Current value of [`INTER_SUB16_SPLIT_HITS`]. |
| 1367 | pub(crate) fn inter_sub16_split_hits() -> usize { |
| 1368 | INTER_SUB16_SPLIT_HITS.with(|c| c.get()) |
| 1369 | } |
| 1370 | |
| 1371 | // lane-rectwire r2: how many `PARTITION_HORZ`/`VERT` strips actually decoded |
| 1372 | // real (non-skip) coefficients through [`read_coeffs_rect`] -- proves the |
| 1373 | // rect coefficient reader itself fired, not just the strip-level partition |
| 1374 | // symbol [`RECT_PARTITION_HITS`] already counts. |
| 1375 | thread_local! { |
| 1376 | static RECT_COEFF_HITS: std::cell::Cell<usize> = |
| 1377 | const { std::cell::Cell::new(0) }; |
| 1378 | } |
| 1379 | |
| 1380 | /// Current value of [`RECT_COEFF_HITS`]. |
| 1381 | pub(crate) fn rect_coeff_hits() -> usize { |
| 1382 | RECT_COEFF_HITS.with(|c| c.get()) |
| 1383 | } |
| 1384 | |
| 1385 | // lane-sbpart r2: how many superblock-level `PARTITION_HORZ`/`PARTITION_VERT` |
| 1386 | // blocks (two true 64x32/32x64 strips, [`decode_block_rect64`]) fired -- the |
| 1387 | // gate's proof this round's arms actually reached a real block, not just |
| 1388 | // that `partition_w64` read one of those two symbol values. |
| 1389 | thread_local! { |
| 1390 | static SB_RECT_HITS: std::cell::Cell<usize> = |
| 1391 | const { std::cell::Cell::new(0) }; |
| 1392 | } |
| 1393 | |
| 1394 | /// Current value of [`SB_RECT_HITS`]. |
| 1395 | pub(crate) fn sb_rect_hits() -> usize { |
| 1396 | SB_RECT_HITS.with(|c| c.get()) |
| 1397 | } |
| 1398 | |
| 1399 | // lane-part32 r4: how many superblock-level AB blocks (`PARTITION_HORZ_A`/ |
| 1400 | // `_B`/`VERT_A`/`_B` at 64x64 -- two 32x32 squares plus one 64x32/32x64 |
| 1401 | // strip) fired. Real aomenc picks these at 64 even with |
| 1402 | // `--enable-ab-partitions=0` (lane-sbpart r2's own measurement), so this is |
| 1403 | // the counter the gate hard-asserts. |
| 1404 | thread_local! { |
| 1405 | static SB_AB_HITS: std::cell::Cell<[usize; 4]> = |
| 1406 | const { std::cell::Cell::new([0; 4]) }; |
| 1407 | } |
| 1408 | |
| 1409 | /// Current value of [`SB_AB_HITS`], summed over the four arms. |
| 1410 | pub(crate) fn sb_ab_hits() -> usize { |
| 1411 | SB_AB_HITS.with(|c| c.get().iter().sum()) |
| 1412 | } |
| 1413 | |
| 1414 | /// [`SB_AB_HITS`] split per arm, in `PARTITION_HORZ_A`, `_HORZ_B`, |
| 1415 | /// `_VERT_A`, `_VERT_B` order -- what the gate hard-asserts arm by arm, so a |
| 1416 | /// run where aomenc's RD only ever picked (say) `HORZ_A` cannot pass off as |
| 1417 | /// proof of all four. |
| 1418 | pub(crate) fn sb_ab_hits_by_arm() -> [usize; 4] { |
| 1419 | SB_AB_HITS.with(std::cell::Cell::get) |
| 1420 | } |
| 1421 | |
| 1422 | // lane-rect64q r1: how many of [`decode_block_rect64`]'s three per-plane |
| 1423 | // dequant calls actually observed `CURRENT_Q_IDX != base_q_idx` -- proof the |
| 1424 | // running-vs-stale-snapshot bug this round fixed is exercised by a gate, not |
| 1425 | // just inert-compiled. Zero forever on a passing gate means that gate's |
| 1426 | // stream never carries `delta_q_present=1` through a rect64 block. |
| 1427 | thread_local! { |
| 1428 | static RECT64_QIDX_DRIFT_HITS: std::cell::Cell<usize> = |
| 1429 | const { std::cell::Cell::new(0) }; |
| 1430 | } |
| 1431 | |
| 1432 | /// Current value of [`RECT64_QIDX_DRIFT_HITS`]. |
| 1433 | pub(crate) fn rect64_qidx_drift_hits() -> usize { |
| 1434 | RECT64_QIDX_DRIFT_HITS.with(|c| c.get()) |
| 1435 | } |
| 1436 | |
| 1437 | // lane-partab r1: how many 32x32 quadrants decoded an AB partition |
| 1438 | // (PARTITION_HORZ_A / VERT_A / VERT_B -- two 16x16 squares plus one |
| 1439 | // 16x32/32x16 strip). Like [`RECT_PARTITION_HITS`], this is the |
| 1440 | // free-partition gate's proof this round's arms actually fired. |
| 1441 | thread_local! { |
| 1442 | static PARTAB_HITS: std::cell::Cell<usize> = |
| 1443 | const { std::cell::Cell::new(0) }; |
| 1444 | } |
| 1445 | |
| 1446 | /// Current value of [`PARTAB_HITS`]. |
| 1447 | pub(crate) fn partab_hits() -> usize { |
| 1448 | PARTAB_HITS.with(|c| c.get()) |
| 1449 | } |
| 1450 | |
| 1451 | // lane-part32 r1: how many 32x32 quadrants decoded each INTRA AB/4-way arm, |
| 1452 | // one counter per arm rather than one shared total -- the gate hard-asserts |
| 1453 | // each of these individually nonzero so a recipe that only ever fires |
| 1454 | // HORZ_A can't silently stand in for the whole set. |
| 1455 | thread_local! { |
| 1456 | static INTRA_HORZ_A_HITS: std::cell::Cell<usize> = |
| 1457 | const { std::cell::Cell::new(0) }; |
| 1458 | static INTRA_HORZ_B_HITS: std::cell::Cell<usize> = |
| 1459 | const { std::cell::Cell::new(0) }; |
| 1460 | static INTRA_VERT_A_HITS: std::cell::Cell<usize> = |
| 1461 | const { std::cell::Cell::new(0) }; |
| 1462 | static INTRA_VERT_B_HITS: std::cell::Cell<usize> = |
| 1463 | const { std::cell::Cell::new(0) }; |
| 1464 | } |
| 1465 | |
| 1466 | /// Current value of [`INTRA_HORZ_A_HITS`]. |
| 1467 | pub(crate) fn intra_horz_a_hits() -> usize { |
| 1468 | INTRA_HORZ_A_HITS.with(|c| c.get()) |
| 1469 | } |
| 1470 | /// Current value of [`INTRA_HORZ_B_HITS`]. |
| 1471 | pub(crate) fn intra_horz_b_hits() -> usize { |
| 1472 | INTRA_HORZ_B_HITS.with(|c| c.get()) |
| 1473 | } |
| 1474 | /// Current value of [`INTRA_VERT_A_HITS`]. |
| 1475 | pub(crate) fn intra_vert_a_hits() -> usize { |
| 1476 | INTRA_VERT_A_HITS.with(|c| c.get()) |
| 1477 | } |
| 1478 | /// Current value of [`INTRA_VERT_B_HITS`]. |
| 1479 | pub(crate) fn intra_vert_b_hits() -> usize { |
| 1480 | INTRA_VERT_B_HITS.with(|c| c.get()) |
| 1481 | } |
| 1482 | |
| 1483 | // How many [`read_inter_compound_mode`] reads actually happened |
| 1484 | // (lane-av1comp) -- proves a `COMPOUND_REFERENCE` block reached its own |
| 1485 | // `compound_mode` symbol (past `comp_mode`/`comp_ref` and the compound |
| 1486 | // mvstack build), not just that `comp_mode` fired. Still refused right |
| 1487 | // after (no MV assignment or MC wired), so this only counts attempted |
| 1488 | // reads. |
| 1489 | thread_local! { |
| 1490 | static COMPOUND_MODE_HITS: std::cell::Cell<usize> = |
| 1491 | const { std::cell::Cell::new(0) }; |
| 1492 | } |
| 1493 | |
| 1494 | /// Current value of [`COMPOUND_MODE_HITS`]. |
| 1495 | pub(crate) fn compound_mode_hits() -> usize { |
| 1496 | COMPOUND_MODE_HITS.with(|c| c.get()) |
| 1497 | } |
| 1498 | |
| 1499 | /// How many query blocks folded in at least one temporal MV candidate |
| 1500 | /// (spec 7.10.2.8's `add_tpl_ref_mv`) so far in this process -- the |
| 1501 | /// temporal-MV gate's own firing counter (lane-av1tmvp). |
| 1502 | /// |
| 1503 | /// # HANDOFF (lane-av1tmvp): not yet wired to a live stream |
| 1504 | /// [`crate::mvstack::find_mv_stack_with_sign_bias`]'s only call site in |
| 1505 | /// this file (`decode_inter_block`) still passes `tpl: None` -- this |
| 1506 | /// counter compiles and the projection math it counts is unit-tested |
| 1507 | /// ([`crate::motion_field`]), but nothing in [`crate::stream::decode_stream`] |
| 1508 | /// builds a per-DPB-slot [`crate::motion_field::MotionField`] or calls |
| 1509 | /// [`crate::motion_field::setup_motion_field`] yet, so this will read `0` |
| 1510 | /// against any real stream today. See the module doc on |
| 1511 | /// `crate::motion_field` and this crate's ledger for the remaining wiring. |
| 1512 | pub(crate) fn tmv_hits() -> usize { |
| 1513 | crate::mvstack::tmv_hits() |
| 1514 | } |
| 1515 | |
| 1516 | /// Current value of [`crate::mc::inter_pred_hits`] -- inter predictions |
| 1517 | /// produced on this thread, the 10-bit inter gate's firing proof. |
| 1518 | pub(crate) fn inter_pred_hits() -> usize { |
| 1519 | crate::mc::inter_pred_hits() |
| 1520 | } |
| 1521 | |
| 1522 | /// Everything [`decode_inter_block`] needs to fold temporal MV candidates |
| 1523 | /// (spec 7.10.2.8), threaded down from [`decode_inter_frame_tile_with_cdfs`]: |
| 1524 | /// the current frame's own projected field ([`crate::motion_field::setup_motion_field`]'s |
| 1525 | /// output, built by the caller when `header.use_ref_frame_mvs` is set) plus |
| 1526 | /// the order-hint bookkeeping [`crate::motion_field::get_relative_dist`] |
| 1527 | /// needs to turn a query block's own resolved `ref_frame` into |
| 1528 | /// `TplArgs::cur_offset_0` (spec 7.9.3). |
| 1529 | pub(crate) struct TplFrameArgs<'a> { |
| 1530 | pub field: &'a crate::motion_field::TplField, |
| 1531 | pub order_hint_bits: u32, |
| 1532 | pub order_hint: u32, |
| 1533 | /// `ref_order_hints[ref_frame - LAST_FRAME]` for `ref_frame` in |
| 1534 | /// `LAST_FRAME..=ALTREF_FRAME` — the OrderHint of the picture this |
| 1535 | /// frame's own `ref_frame_idx` names for each single reference. |
| 1536 | pub ref_order_hints: [u32; 7], |
| 1537 | } |
| 1538 | |
| 1539 | /// Builds this frame's own saved motion field (libaom `av1_copy_frame_mvs`, |
| 1540 | /// spec 7.9's per-frame `MotionFieldMvs` storage step) from its fully |
| 1541 | /// decoded `grid`: every 4x4 unit an inter block covers reads the same |
| 1542 | /// `MiInfo` ([`decode_inter_block`]/`decode_inter_block8` write it |
| 1543 | /// block-uniform), so sampling any one 4x4 unit inside each 8x8 cell is |
| 1544 | /// exact — every block this crate ever codes is at least 8x8, so no 8x8 |
| 1545 | /// cell straddles two different blocks' values. |
| 1546 | pub(crate) fn build_motion_field( |
| 1547 | grid: &MiGrid, |
| 1548 | mi_cols: usize, |
| 1549 | mi_rows: usize, |
| 1550 | order_hint: u32, |
| 1551 | ref_order_hints: [u32; 7], |
| 1552 | ) -> crate::motion_field::MotionField { |
| 1553 | let mut field = |
| 1554 | crate::motion_field::MotionField::new(mi_cols, mi_rows, order_hint, ref_order_hints); |
| 1555 | for row in 0..mi_rows { |
| 1556 | for col in 0..mi_cols { |
| 1557 | if let Some(info) = grid.get(row, col) |
| 1558 | && info.is_inter |
| 1559 | && info.ref_frame > 0 |
| 1560 | { |
| 1561 | field.set( |
| 1562 | row, |
| 1563 | col, |
| 1564 | crate::motion_field::SavedMv { |
| 1565 | mv: info.mv, |
| 1566 | ref_frame: info.ref_frame, |
| 1567 | }, |
| 1568 | ); |
| 1569 | } |
| 1570 | } |
| 1571 | } |
| 1572 | field |
| 1573 | } |
| 1574 | |
| 1575 | /// Current value of the counter for `ref_frame` (`LAST2_FRAME`..=`ALTREF_FRAME`; |
| 1576 | /// panics on `LAST_FRAME`/`INTRA_FRAME`/`NONE`, which have no counter here). |
| 1577 | pub(crate) fn ref_hits(ref_frame: i8) -> usize { |
| 1578 | let c = match ref_frame { |
| 1579 | LAST2_FRAME => &LAST2_HITS, |
| 1580 | LAST3_FRAME => &LAST3_HITS, |
| 1581 | GOLDEN_FRAME => &GOLDEN_HITS, |
| 1582 | BWDREF_FRAME => &BWDREF_HITS, |
| 1583 | ALTREF2_FRAME => &ALTREF2_HITS, |
| 1584 | ALTREF_FRAME => &ALTREF_HITS, |
| 1585 | other => panic!("ref_hits: no per-reference counter for ref_frame {other}"), |
| 1586 | }; |
| 1587 | c.with(|v| v.get()) |
| 1588 | } |
| 1589 | |
| 1590 | thread_local! { |
| 1591 | /// The sequence header's `enable_intra_edge_filter`, set once per |
| 1592 | /// [`decode_key_frame_tile_with_cdfs`] call: [`PlaneBuf::reconstruct`]'s |
| 1593 | /// own read of it, kept off the call stack (see that function's own doc |
| 1594 | /// comment for why) rather than threaded through every `read_plane`/ |
| 1595 | /// `decode_block`/`decode_leaf8` call between here and there. |
| 1596 | static ENABLE_EDGE_FILTER: std::cell::Cell<bool> = const { std::cell::Cell::new(false) }; |
| 1597 | } |
| 1598 | |
| 1599 | /// Set [`ENABLE_EDGE_FILTER`] for this thread. |
| 1600 | /// |
| 1601 | /// `decode_key_frame_tile_with_cdfs` sets it, but `decode_inter_frame_tile` |
| 1602 | /// never did -- so an inter frame's intra blocks read whatever value the last |
| 1603 | /// key frame decoded ON THIS THREAD left behind. Inside one stream that is |
| 1604 | /// harmless (its own key frame sets it first), but a caller that decodes an |
| 1605 | /// inter frame directly, or a test harness running several streams on one |
| 1606 | /// thread, inherits a foreign sequence's bit: decode stops being a pure |
| 1607 | /// function of the bitstream. `decode_stream` now calls this once per stream |
| 1608 | /// from the sequence header, which covers every frame including inter ones. |
| 1609 | pub(crate) fn set_enable_edge_filter(value: bool) { |
| 1610 | ENABLE_EDGE_FILTER.with(|f| f.set(value)); |
| 1611 | } |
| 1612 | |
| 1613 | /// Current value of [`ANGLE_DELTA_HITS`]. |
| 1614 | pub(crate) fn angle_delta_hits() -> usize { |
| 1615 | ANGLE_DELTA_HITS.with(|c| c.get()) |
| 1616 | } |
| 1617 | |
| 1618 | /// Current value of [`TX_CLASS1_HITS`]. |
| 1619 | pub(crate) fn tx_class1_hits() -> usize { |
| 1620 | TX_CLASS1_HITS.with(|c| c.get()) |
| 1621 | } |
| 1622 | const SUB_MI: u32 = 4; |
| 1623 | const MI: usize = 4; |
| 1624 | const SB: usize = 64; |
| 1625 | const BLOCK: usize = 32; |
| 1626 | const SUB: usize = 16; |
| 1627 | const TX32: usize = 32; |
| 1628 | const TX16: usize = 16; |
| 1629 | const TX8: usize = 8; |
| 1630 | const TX4: usize = 4; |
| 1631 | const DC_PRED: usize = 0; |
| 1632 | /// `V_PRED` (spec 6.10.2): the first directional mode, and so the first that |
| 1633 | /// carries an angle delta — [`crate::tile`]'s own private copy of the same |
| 1634 | /// constant. |
| 1635 | const V_PRED: usize = 1; |
| 1636 | const H_PRED: usize = 2; |
| 1637 | /// `D157_PRED`: `fimode_to_intradir`'s target for `FILTER_D157_PRED` (spec |
| 1638 | /// `Fimode_To_Intradir`, libaom `blockd.h`). |
| 1639 | const D157_PRED: usize = 6; |
| 1640 | /// The last directional mode, `D67_PRED`. |
| 1641 | const D67_PRED: usize = 8; |
| 1642 | /// The symbol an angle delta of zero codes as (spec 9.3): the alphabet runs |
| 1643 | /// from -3 to +3, so this is its middle — [`crate::tile`]'s writer never |
| 1644 | /// codes anything else, so this decoder never reads anything else either. |
| 1645 | const ANGLE_DELTA_ZERO: usize = 3; |
| 1646 | const NUM_BASE_LEVELS: i32 = 2; |
| 1647 | const COEFF_BASE_RANGE: i32 = 12; |
| 1648 | const BR_STEP: i32 = 3; |
| 1649 | const MAX_BR_LEVEL: i32 = NUM_BASE_LEVELS + COEFF_BASE_RANGE; |
| 1650 | |
| 1651 | /// `partition_gather_vert_alike`/`_horz_alike` (spec 9.3): the partition types |
| 1652 | /// whose probability mass a superblock or block half outside the frame |
| 1653 | /// gathers into its one remaining flag — [`crate::tile`]'s own private copy, |
| 1654 | /// duplicated here (that module is another lane's territory this round) and |
| 1655 | /// kept in step with it by the byte-exact round-trip tests below rather than |
| 1656 | /// by sharing code. |
| 1657 | const VERT_ALIKE: [usize; 6] = [2, PARTITION_SPLIT, 4, 6, 7, 9]; |
| 1658 | const HORZ_ALIKE: [usize; 6] = [1, PARTITION_SPLIT, 4, 5, 6, 8]; |
| 1659 | |
| 1660 | fn element_prob(cdf: &[u16], element: usize) -> u16 { |
| 1661 | cdf[element] - if element > 0 { cdf[element - 1] } else { 0 } |
| 1662 | } |
| 1663 | |
| 1664 | /// The two-symbol CDF a partial superblock/block's forced-split flag is read |
| 1665 | /// with: the mass of `elements` becomes the probability of the one partition |
| 1666 | /// the frame edge still allows, mirroring [`crate::tile`]'s own `gather` |
| 1667 | /// (the encoder's non-adapting counterpart to this read). |
| 1668 | fn gather(cdf: &[u16], elements: [usize; 6]) -> [u16; 3] { |
| 1669 | let split: u16 = elements.iter().map(|&e| element_prob(cdf, e)).sum(); |
| 1670 | [32768 - split, 32768, 0] |
| 1671 | } |
| 1672 | |
| 1673 | fn unsupported(what: impl Into<String>) -> Error { |
| 1674 | Error::unsupported("AV1 tile", what.into()) |
| 1675 | } |
| 1676 | |
| 1677 | /// The coefficient q-context a frame's `base_q_idx` picks its default CDFs |
| 1678 | /// from (spec `Get_Qctx`), mirroring [`crate::tile`]'s private copy of the |
| 1679 | /// same rule. |
| 1680 | pub(crate) fn q_ctx_of(base_q_idx: u8) -> usize { |
| 1681 | match base_q_idx { |
| 1682 | 0..=20 => 0, |
| 1683 | 21..=60 => 1, |
| 1684 | 61..=120 => 2, |
| 1685 | _ => 3, |
| 1686 | } |
| 1687 | } |
| 1688 | |
| 1689 | /// `Default_Scan_NxN` (spec 8.4.2), the same anti-diagonal walk |
| 1690 | /// [`crate::tile`]'s writer generates its scan tables with. |
| 1691 | fn default_scan(side: usize) -> Vec<u16> { |
| 1692 | let mut scan = Vec::with_capacity(side * side); |
| 1693 | for d in 0..(2 * side - 1) { |
| 1694 | let lo = d.saturating_sub(side - 1); |
| 1695 | let hi = d.min(side - 1); |
| 1696 | let diagonal = (lo..=hi).map(|row| (row * side + (d - row)) as u16); |
| 1697 | if d % 2 == 0 { |
| 1698 | scan.extend(diagonal.rev()); |
| 1699 | } else { |
| 1700 | scan.extend(diagonal); |
| 1701 | } |
| 1702 | } |
| 1703 | scan |
| 1704 | } |
| 1705 | |
| 1706 | /// `Mrow_Scan`/`Mcol_Scan` (spec 8.4.2, libaom `scan.c`): the two |
| 1707 | /// class-specific scans a `V_DCT`/`H_DCT` transform block reads coefficients |
| 1708 | /// in, instead of the default zigzag. libaom's own tables are keyed by a |
| 1709 | /// *column-major* raster (`get_txb_bhl`'s `col = pos / height`) over its |
| 1710 | /// physically column-major level buffer, but they decompose to the exact |
| 1711 | /// same (row, col) walk as a flat row-major traversal of *our* row-major |
| 1712 | /// `grid`/`levels` (`pos = row * side + col`) once you read them that way: |
| 1713 | /// `Mrow_Scan` visits row 0 across every column, then row 1, ... — precisely |
| 1714 | /// `0..side*side` in our own indexing — and `Mcol_Scan` is that walk |
| 1715 | /// transposed (column 0 down every row, then column 1, ...). |
| 1716 | fn class_scan_table(side: usize, class: TxClass) -> Vec<u16> { |
| 1717 | match class { |
| 1718 | TxClass::Vert => (0..(side * side) as u16).collect(), |
| 1719 | TxClass::Horiz => (0..side) |
| 1720 | .flat_map(|col| (0..side).map(move |row| (row * side + col) as u16)) |
| 1721 | .collect(), |
| 1722 | TxClass::TwoD => unreachable!("class_scan is only for V_DCT/H_DCT"), |
| 1723 | } |
| 1724 | } |
| 1725 | |
| 1726 | fn eob_coeff_ctx(scan_idx: usize, area: usize) -> usize { |
| 1727 | match scan_idx { |
| 1728 | 0 => 0, |
| 1729 | i if i <= area / 8 => 1, |
| 1730 | i if i <= area / 4 => 2, |
| 1731 | _ => 3, |
| 1732 | } |
| 1733 | } |
| 1734 | |
| 1735 | fn neighbour(grid: &[i32], side: usize, row: usize, col: usize) -> i32 { |
| 1736 | if row >= side || col >= side { |
| 1737 | 0 |
| 1738 | } else { |
| 1739 | grid[row * side + col] |
| 1740 | } |
| 1741 | } |
| 1742 | |
| 1743 | /// spec 5.11.39's three `TxClass`es (`tx_type_to_class`, libaom |
| 1744 | /// `txb_common.h`): everything but the two lone-axis-identity types this |
| 1745 | /// decoder reads (`V_DCT`/`H_DCT`) is `TX_CLASS_2D`. |
| 1746 | #[derive(Clone, Copy, PartialEq, Eq)] |
| 1747 | enum TxClass { |
| 1748 | TwoD, |
| 1749 | Horiz, |
| 1750 | Vert, |
| 1751 | } |
| 1752 | |
| 1753 | impl TxClass { |
| 1754 | fn of(tx_type: TxType) -> Self { |
| 1755 | match tx_type { |
| 1756 | // `TX_CLASS_VERT`/`TX_CLASS_HORIZ` (`txb_common.h`'s `tx_type_to_class`) |
| 1757 | // key on axis alone -- FLIPADST is still ADST for this purpose. |
| 1758 | TxType::VDct | TxType::VAdst | TxType::VFlipAdst => Self::Vert, |
| 1759 | TxType::HDct | TxType::HAdst | TxType::HFlipAdst => Self::Horiz, |
| 1760 | _ => Self::TwoD, |
| 1761 | } |
| 1762 | } |
| 1763 | } |
| 1764 | |
| 1765 | /// `nz_map_ctx_offset_1d`: `TX_CLASS_HORIZ`/`TX_CLASS_VERT` share one 32-entry |
| 1766 | /// table (indexed by the position along the transform's *coded* axis -- |
| 1767 | /// `col` for horiz, `row` for vert) that places their contexts past the 2D |
| 1768 | /// table's own 26 rows (`SIG_COEF_CONTEXTS_2D`), 16 rows total |
| 1769 | /// (`SIG_COEF_CONTEXTS_1D`) split 26/31/36. |
| 1770 | fn nz_map_ctx_offset_1d(i: usize) -> usize { |
| 1771 | match i { |
| 1772 | 0 => 26, |
| 1773 | 1 => 31, |
| 1774 | _ => 36, |
| 1775 | } |
| 1776 | } |
| 1777 | |
| 1778 | fn base_ctx( |
| 1779 | grid: &[i32], |
| 1780 | side: usize, |
| 1781 | row: usize, |
| 1782 | col: usize, |
| 1783 | class: TxClass, |
| 1784 | rect_shape: Option<(usize, usize)>, |
| 1785 | ) -> usize { |
| 1786 | // libaom's `get_nz_map_ctx_from_stats` only special-cases the DC |
| 1787 | // coefficient to ctx 0 for `TX_CLASS_2D` (`(tx_class | coeff_idx) == 0`, |
| 1788 | // txb_common.h) -- `V_DCT`/`H_DCT` (class `Horiz`/`Vert`) still run the DC |
| 1789 | // position through the full 1D neighbour sum and offset table. Skipping |
| 1790 | // that for every class desynced the last (DC) coefficient of a |
| 1791 | // `TX_CLASS_HORIZ` block (lane-av1tx4 final round: real ctx 28 read as |
| 1792 | // 0, which then read one extra `BR` symbol at the max and forced a |
| 1793 | // spurious third Golomb call). |
| 1794 | if class == TxClass::TwoD && row == 0 && col == 0 { |
| 1795 | return 0; |
| 1796 | } |
| 1797 | let offsets: [(usize, usize); 5] = match class { |
| 1798 | TxClass::TwoD => [(1, 0), (0, 1), (1, 1), (2, 0), (0, 2)], |
| 1799 | TxClass::Horiz => [(1, 0), (0, 1), (0, 2), (0, 3), (0, 4)], |
| 1800 | TxClass::Vert => [(1, 0), (0, 1), (2, 0), (3, 0), (4, 0)], |
| 1801 | }; |
| 1802 | let mag: i32 = offsets |
| 1803 | .iter() |
| 1804 | .map(|&(dr, dc)| neighbour(grid, side, row + dr, col + dc).abs().min(3)) |
| 1805 | .sum(); |
| 1806 | let ctx = ((mag + 1) >> 1).min(4) as usize; |
| 1807 | match class { |
| 1808 | // libaom's CDF *set* for a truncated 64-wide/tall corner resolves |
| 1809 | // through `get_txsize_entropy_ctx` to the square `TX_64X64` (this |
| 1810 | // decoder's `TxbSet::Luma64`), but `get_nz_map_ctx_from_stats` |
| 1811 | // separately indexes `av1_nz_map_ctx_offset` by the raw, un-adjusted |
| 1812 | // `tx_size` -- `TX_32X64`/`TX_64X32` for a superblock-level HORZ/VERT |
| 1813 | // strip, which own a genuinely different table from the square one |
| 1814 | // (lane-sbpart r8 root cause: `(row=1, col=0)` reads ctx 2 under the |
| 1815 | // square table vs the real ctx 13 the rect table gives). |
| 1816 | TxClass::TwoD => { |
| 1817 | // OPEN (lane-rectsplit r3, measured): the two rect |
| 1818 | // transcriptions in [`cdf`] are packed `[col][row]` -- libaom's |
| 1819 | // `av1_nz_map_ctx_offset_32x64` is a FLAT 1024-entry array whose |
| 1820 | // `coeff_idx` is COLUMN-major with stride 32 (`TX_CLASS_HORIZ` |
| 1821 | // reads its column back as `coeff_idx >> bhl`), and entries |
| 1822 | // 0..5 / 32..35 match the generating rule (txb_common.h:199-209) |
| 1823 | // only under `flat[col * 32 + row]`; |
| 1824 | // `nz_map_ctx_offset_tables_match_the_rect_rule` pins that. |
| 1825 | // Yet reading `[col][row]` here BREAKS |
| 1826 | // `a_real_aomenc_stream_with_a_superblock_level_horz_vert_partition_decodes_pixel_exact` |
| 1827 | // (seed 43) while `[row][col]` breaks the split-transform SB gate |
| 1828 | // (seed 50); swapping the shape test as well fails BOTH ways. So |
| 1829 | // this read is not the defect -- our (row, col) here come from a |
| 1830 | // square 32x32-corner scan whose orientation relative to libaom's |
| 1831 | // column-major `coeff_idx` is what is really unproven, and each |
| 1832 | // "fix" only moves which rare corner position desyncs. Left at |
| 1833 | // the r1 reading, the one every merged gate is green under. |
| 1834 | // `EC_NZOFF_DUMP=1` prints every rect-shaped read. |
| 1835 | let table = match rect_shape { |
| 1836 | Some((w, h)) if w < h => &cdf::NZ_MAP_CTX_OFFSET_32X64, |
| 1837 | Some((w, h)) if w > h => &cdf::NZ_MAP_CTX_OFFSET_64X32, |
| 1838 | _ => &cdf::NZ_MAP_CTX_OFFSET_32, |
| 1839 | }; |
| 1840 | if std::env::var_os("EC_NZOFF_DUMP").is_some() { |
| 1841 | eprintln!("NZOFF side={side} shape={rect_shape:?} row={row} col={col}"); |
| 1842 | } |
| 1843 | ctx + table[row.min(4)][col.min(4)] as usize |
| 1844 | } |
| 1845 | TxClass::Horiz => ctx + nz_map_ctx_offset_1d(col.min(31)), |
| 1846 | TxClass::Vert => ctx + nz_map_ctx_offset_1d(row.min(31)), |
| 1847 | } |
| 1848 | } |
| 1849 | |
| 1850 | fn br_ctx(grid: &[i32], side: usize, row: usize, col: usize, class: TxClass) -> usize { |
| 1851 | let extra = match class { |
| 1852 | TxClass::TwoD => neighbour(grid, side, row + 1, col + 1).abs(), |
| 1853 | TxClass::Horiz => neighbour(grid, side, row, col + 2).abs(), |
| 1854 | TxClass::Vert => neighbour(grid, side, row + 2, col).abs(), |
| 1855 | }; |
| 1856 | let mag = neighbour(grid, side, row + 1, col).abs() |
| 1857 | + neighbour(grid, side, row, col + 1).abs() |
| 1858 | + extra; |
| 1859 | let mag = (((mag + 1) >> 1).min(6)) as usize; |
| 1860 | if row == 0 && col == 0 { |
| 1861 | return mag; |
| 1862 | } |
| 1863 | let near_origin = match class { |
| 1864 | TxClass::TwoD => row < 2 && col < 2, |
| 1865 | TxClass::Horiz => col == 0, |
| 1866 | TxClass::Vert => row == 0, |
| 1867 | }; |
| 1868 | if near_origin { mag + 7 } else { mag + 14 } |
| 1869 | } |
| 1870 | |
| 1871 | /// `Default_Scan_32x16` (spec 8.4.2, libaom `scan.c`'s `default_scan_32x16`): |
| 1872 | /// transposed from libaom's column-major buffer into this decoder's row-major |
| 1873 | /// `pos = row * 32 + col` (lane-rectwire, verified as a bijection of `0..512` |
| 1874 | /// against the real table). Not the square zigzag formula generalized to |
| 1875 | /// unequal `w`/`h` -- checked and that does NOT reproduce this table. |
| 1876 | const SCAN_32X16: [u16; 512] = [ |
| 1877 | 0, 32, 1, 64, 33, 2, 96, 65, 34, 3, 128, 97, 66, 35, 4, 160, 129, 98, 67, 36, 5, 192, 161, |
| 1878 | 130, 99, 68, 37, 6, 224, 193, 162, 131, 100, 69, 38, 7, 256, 225, 194, 163, 132, 101, 70, 39, |
| 1879 | 8, 288, 257, 226, 195, 164, 133, 102, 71, 40, 9, 320, 289, 258, 227, 196, 165, 134, 103, 72, |
| 1880 | 41, 10, 352, 321, 290, 259, 228, 197, 166, 135, 104, 73, 42, 11, 384, 353, 322, 291, 260, 229, |
| 1881 | 198, 167, 136, 105, 74, 43, 12, 416, 385, 354, 323, 292, 261, 230, 199, 168, 137, 106, 75, 44, |
| 1882 | 13, 448, 417, 386, 355, 324, 293, 262, 231, 200, 169, 138, 107, 76, 45, 14, 480, 449, 418, |
| 1883 | 387, 356, 325, 294, 263, 232, 201, 170, 139, 108, 77, 46, 15, 481, 450, 419, 388, 357, 326, |
| 1884 | 295, 264, 233, 202, 171, 140, 109, 78, 47, 16, 482, 451, 420, 389, 358, 327, 296, 265, 234, |
| 1885 | 203, 172, 141, 110, 79, 48, 17, 483, 452, 421, 390, 359, 328, 297, 266, 235, 204, 173, 142, |
| 1886 | 111, 80, 49, 18, 484, 453, 422, 391, 360, 329, 298, 267, 236, 205, 174, 143, 112, 81, 50, 19, |
| 1887 | 485, 454, 423, 392, 361, 330, 299, 268, 237, 206, 175, 144, 113, 82, 51, 20, 486, 455, 424, |
| 1888 | 393, 362, 331, 300, 269, 238, 207, 176, 145, 114, 83, 52, 21, 487, 456, 425, 394, 363, 332, |
| 1889 | 301, 270, 239, 208, 177, 146, 115, 84, 53, 22, 488, 457, 426, 395, 364, 333, 302, 271, 240, |
| 1890 | 209, 178, 147, 116, 85, 54, 23, 489, 458, 427, 396, 365, 334, 303, 272, 241, 210, 179, 148, |
| 1891 | 117, 86, 55, 24, 490, 459, 428, 397, 366, 335, 304, 273, 242, 211, 180, 149, 118, 87, 56, 25, |
| 1892 | 491, 460, 429, 398, 367, 336, 305, 274, 243, 212, 181, 150, 119, 88, 57, 26, 492, 461, 430, |
| 1893 | 399, 368, 337, 306, 275, 244, 213, 182, 151, 120, 89, 58, 27, 493, 462, 431, 400, 369, 338, |
| 1894 | 307, 276, 245, 214, 183, 152, 121, 90, 59, 28, 494, 463, 432, 401, 370, 339, 308, 277, 246, |
| 1895 | 215, 184, 153, 122, 91, 60, 29, 495, 464, 433, 402, 371, 340, 309, 278, 247, 216, 185, 154, |
| 1896 | 123, 92, 61, 30, 496, 465, 434, 403, 372, 341, 310, 279, 248, 217, 186, 155, 124, 93, 62, 31, |
| 1897 | 497, 466, 435, 404, 373, 342, 311, 280, 249, 218, 187, 156, 125, 94, 63, 498, 467, 436, 405, |
| 1898 | 374, 343, 312, 281, 250, 219, 188, 157, 126, 95, 499, 468, 437, 406, 375, 344, 313, 282, 251, |
| 1899 | 220, 189, 158, 127, 500, 469, 438, 407, 376, 345, 314, 283, 252, 221, 190, 159, 501, 470, 439, |
| 1900 | 408, 377, 346, 315, 284, 253, 222, 191, 502, 471, 440, 409, 378, 347, 316, 285, 254, 223, 503, |
| 1901 | 472, 441, 410, 379, 348, 317, 286, 255, 504, 473, 442, 411, 380, 349, 318, 287, 505, 474, 443, |
| 1902 | 412, 381, 350, 319, 506, 475, 444, 413, 382, 351, 507, 476, 445, 414, 383, 508, 477, 446, 415, |
| 1903 | 509, 478, 447, 510, 479, 511, |
| 1904 | ]; |
| 1905 | |
| 1906 | /// `Default_Scan_16x32`, this decoder's own row-major transcription (see |
| 1907 | /// [`SCAN_32X16`]'s doc comment). |
| 1908 | const SCAN_16X32: [u16; 512] = [ |
| 1909 | 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, |
| 1910 | 66, 81, 96, 7, 22, 37, 52, 67, 82, 97, 112, 8, 23, 38, 53, 68, 83, 98, 113, 128, 9, 24, 39, 54, |
| 1911 | 69, 84, 99, 114, 129, 144, 10, 25, 40, 55, 70, 85, 100, 115, 130, 145, 160, 11, 26, 41, 56, 71, |
| 1912 | 86, 101, 116, 131, 146, 161, 176, 12, 27, 42, 57, 72, 87, 102, 117, 132, 147, 162, 177, 192, |
| 1913 | 13, 28, 43, 58, 73, 88, 103, 118, 133, 148, 163, 178, 193, 208, 14, 29, 44, 59, 74, 89, 104, |
| 1914 | 119, 134, 149, 164, 179, 194, 209, 224, 15, 30, 45, 60, 75, 90, 105, 120, 135, 150, 165, 180, |
| 1915 | 195, 210, 225, 240, 31, 46, 61, 76, 91, 106, 121, 136, 151, 166, 181, 196, 211, 226, 241, 256, |
| 1916 | 47, 62, 77, 92, 107, 122, 137, 152, 167, 182, 197, 212, 227, 242, 257, 272, 63, 78, 93, 108, |
| 1917 | 123, 138, 153, 168, 183, 198, 213, 228, 243, 258, 273, 288, 79, 94, 109, 124, 139, 154, 169, |
| 1918 | 184, 199, 214, 229, 244, 259, 274, 289, 304, 95, 110, 125, 140, 155, 170, 185, 200, 215, 230, |
| 1919 | 245, 260, 275, 290, 305, 320, 111, 126, 141, 156, 171, 186, 201, 216, 231, 246, 261, 276, 291, |
| 1920 | 306, 321, 336, 127, 142, 157, 172, 187, 202, 217, 232, 247, 262, 277, 292, 307, 322, 337, 352, |
| 1921 | 143, 158, 173, 188, 203, 218, 233, 248, 263, 278, 293, 308, 323, 338, 353, 368, 159, 174, 189, |
| 1922 | 204, 219, 234, 249, 264, 279, 294, 309, 324, 339, 354, 369, 384, 175, 190, 205, 220, 235, 250, |
| 1923 | 265, 280, 295, 310, 325, 340, 355, 370, 385, 400, 191, 206, 221, 236, 251, 266, 281, 296, 311, |
| 1924 | 326, 341, 356, 371, 386, 401, 416, 207, 222, 237, 252, 267, 282, 297, 312, 327, 342, 357, 372, |
| 1925 | 387, 402, 417, 432, 223, 238, 253, 268, 283, 298, 313, 328, 343, 358, 373, 388, 403, 418, 433, |
| 1926 | 448, 239, 254, 269, 284, 299, 314, 329, 344, 359, 374, 389, 404, 419, 434, 449, 464, 255, 270, |
| 1927 | 285, 300, 315, 330, 345, 360, 375, 390, 405, 420, 435, 450, 465, 480, 271, 286, 301, 316, 331, |
| 1928 | 346, 361, 376, 391, 406, 421, 436, 451, 466, 481, 496, 287, 302, 317, 332, 347, 362, 377, 392, |
| 1929 | 407, 422, 437, 452, 467, 482, 497, 303, 318, 333, 348, 363, 378, 393, 408, 423, 438, 453, 468, |
| 1930 | 483, 498, 319, 334, 349, 364, 379, 394, 409, 424, 439, 454, 469, 484, 499, 335, 350, 365, 380, |
| 1931 | 395, 410, 425, 440, 455, 470, 485, 500, 351, 366, 381, 396, 411, 426, 441, 456, 471, 486, 501, |
| 1932 | 367, 382, 397, 412, 427, 442, 457, 472, 487, 502, 383, 398, 413, 428, 443, 458, 473, 488, 503, |
| 1933 | 399, 414, 429, 444, 459, 474, 489, 504, 415, 430, 445, 460, 475, 490, 505, 431, 446, 461, 476, |
| 1934 | 491, 506, 447, 462, 477, 492, 507, 463, 478, 493, 508, 479, 494, 509, 495, 510, 511, |
| 1935 | ]; |
| 1936 | |
| 1937 | /// `Default_Scan_16x8`, this decoder's own row-major transcription (see |
| 1938 | /// [`SCAN_32X16`]'s doc comment). |
| 1939 | const SCAN_16X8: [u16; 128] = [ |
| 1940 | 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, |
| 1941 | 36, 21, 6, 112, 97, 82, 67, 52, 37, 22, 7, 113, 98, 83, 68, 53, 38, 23, 8, 114, 99, 84, 69, 54, |
| 1942 | 39, 24, 9, 115, 100, 85, 70, 55, 40, 25, 10, 116, 101, 86, 71, 56, 41, 26, 11, 117, 102, 87, |
| 1943 | 72, 57, 42, 27, 12, 118, 103, 88, 73, 58, 43, 28, 13, 119, 104, 89, 74, 59, 44, 29, 14, 120, |
| 1944 | 105, 90, 75, 60, 45, 30, 15, 121, 106, 91, 76, 61, 46, 31, 122, 107, 92, 77, 62, 47, 123, 108, |
| 1945 | 93, 78, 63, 124, 109, 94, 79, 125, 110, 95, 126, 111, 127, |
| 1946 | ]; |
| 1947 | |
| 1948 | /// `Default_Scan_8x16`, this decoder's own row-major transcription (see |
| 1949 | /// [`SCAN_32X16`]'s doc comment). |
| 1950 | const SCAN_8X16: [u16; 128] = [ |
| 1951 | 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, |
| 1952 | 41, 48, 7, 14, 21, 28, 35, 42, 49, 56, 15, 22, 29, 36, 43, 50, 57, 64, 23, 30, 37, 44, 51, 58, |
| 1953 | 65, 72, 31, 38, 45, 52, 59, 66, 73, 80, 39, 46, 53, 60, 67, 74, 81, 88, 47, 54, 61, 68, 75, 82, |
| 1954 | 89, 96, 55, 62, 69, 76, 83, 90, 97, 104, 63, 70, 77, 84, 91, 98, 105, 112, 71, 78, 85, 92, 99, |
| 1955 | 106, 113, 120, 79, 86, 93, 100, 107, 114, 121, 87, 94, 101, 108, 115, 122, 95, 102, 109, 116, |
| 1956 | 123, 103, 110, 117, 124, 111, 118, 125, 119, 126, 127, |
| 1957 | ]; |
| 1958 | |
| 1959 | /// [`neighbour`] with independent `w` (row stride)/`h` (row-bound) extents |
| 1960 | /// (lane-rectwire, mirrors [`record_rect`](Neighbours::record_rect)'s |
| 1961 | /// asymmetric-extent pattern). |
| 1962 | fn neighbour_rect(grid: &[i32], w: usize, h: usize, row: usize, col: usize) -> i32 { |
| 1963 | if row >= h || col >= w { |
| 1964 | 0 |
| 1965 | } else { |
| 1966 | grid[row * w + col] |
| 1967 | } |
| 1968 | } |
| 1969 | |
| 1970 | /// [`base_ctx`]'s `TxClass::TwoD` arm widened to `(w, h)` (lane-rectwire): |
| 1971 | /// the rect generalization is libaom's own comment in |
| 1972 | /// `get_nz_map_ctx_from_stats` (`txb_common.h:189-224`), reduces to the exact |
| 1973 | /// same [`cdf::NZ_MAP_CTX_OFFSET_32`] table when `w == h`. Never called for |
| 1974 | /// `V_DCT`/`H_DCT` -- geometrically impossible at the two size pairs |
| 1975 | /// [`decode_block_rect`] codes (see that function's own doc comment). |
| 1976 | fn base_ctx_rect(grid: &[i32], w: usize, h: usize, row: usize, col: usize) -> usize { |
| 1977 | if row == 0 && col == 0 { |
| 1978 | return 0; |
| 1979 | } |
| 1980 | const OFFSETS: [(usize, usize); 5] = [(1, 0), (0, 1), (1, 1), (2, 0), (0, 2)]; |
| 1981 | let mag: i32 = OFFSETS |
| 1982 | .iter() |
| 1983 | .map(|&(dr, dc)| neighbour_rect(grid, w, h, row + dr, col + dc).abs().min(3)) |
| 1984 | .sum(); |
| 1985 | let ctx = ((mag + 1) >> 1).min(4) as usize; |
| 1986 | if w < h && row < 2 { |
| 1987 | return ctx + 11; |
| 1988 | } |
| 1989 | if w > h && col < 2 { |
| 1990 | return ctx + 16; |
| 1991 | } |
| 1992 | ctx + cdf::NZ_MAP_CTX_OFFSET_32[row.min(4)][col.min(4)] as usize |
| 1993 | } |
| 1994 | |
| 1995 | /// [`br_ctx`]'s `TxClass::TwoD` arm widened to `(w, h)` (lane-rectwire): the |
| 1996 | /// neighbour-offset math itself is shape-independent, only the boundary |
| 1997 | /// clamp needs the real bound in each axis. |
| 1998 | fn br_ctx_rect(grid: &[i32], w: usize, h: usize, row: usize, col: usize) -> usize { |
| 1999 | let extra = neighbour_rect(grid, w, h, row + 1, col + 1).abs(); |
| 2000 | let mag = neighbour_rect(grid, w, h, row + 1, col).abs() |
| 2001 | + neighbour_rect(grid, w, h, row, col + 1).abs() |
| 2002 | + extra; |
| 2003 | let mag = (((mag + 1) >> 1).min(6)) as usize; |
| 2004 | if row == 0 && col == 0 { |
| 2005 | return mag; |
| 2006 | } |
| 2007 | if row < 2 && col < 2 { mag + 7 } else { mag + 14 } |
| 2008 | } |
| 2009 | |
| 2010 | fn dc_vote(dc: Option<bool>) -> i32 { |
| 2011 | match dc { |
| 2012 | None => 0, |
| 2013 | Some(true) => -1, |
| 2014 | Some(false) => 1, |
| 2015 | } |
| 2016 | } |
| 2017 | |
| 2018 | fn dc_sign_ctx(vote: i32) -> usize { |
| 2019 | match vote.signum() { |
| 2020 | 0 => 0, |
| 2021 | -1 => 1, |
| 2022 | _ => 2, |
| 2023 | } |
| 2024 | } |
| 2025 | |
| 2026 | /// The remainder of a level the base and base-range syntax could not reach |
| 2027 | /// (spec 5.11.40): its bit length in unary, then that many of its own bits, |
| 2028 | /// most significant first — the exact inverse of [`crate::tile::write_golomb`]. |
| 2029 | fn read_golomb(dec: &mut SymbolDecoder) -> Result<u32> { |
| 2030 | // lane-scaledref r1: this cap MASKS A REAL DEFECT and must not be lifted |
| 2031 | // on its own. Reading up to 32 leading zeros (dav1d's `len < 32`; libaom |
| 2032 | // itself calls a 21st bit a corrupt frame, decodetxb.c:30) is bit-identical |
| 2033 | // for every tail any encoder writes -- our own writer tops out at 19 zeros |
| 2034 | // (`tile::MAX_LEVEL == MAX_BR_LEVEL + (1 << 19)`) -- and was implemented |
| 2035 | // and round-trip tested here (commit ee1f980, reverted in 314ee08). It |
| 2036 | // turned `a_real_aomenc_stream_with_a_superblock_level_horz_vert_partition_decodes_pixel_exact` |
| 2037 | // RED: seed 67 stops here today, and with the cap raised it decodes to a |
| 2038 | // frame-0 LUMA MISMATCH instead. A key frame cannot legitimately carry a |
| 2039 | // level above 1<<19, so the long tail is the SYMPTOM of an earlier desync |
| 2040 | // in that intra rect64 stream -- class `refusal-hides-a-defect`. Lift this |
| 2041 | // together with that defect's fix, not before. |
| 2042 | let mut length = 1u32; |
| 2043 | while dec.literal(1) == 0 { |
| 2044 | length += 1; |
| 2045 | if length > 20 { |
| 2046 | return Err(unsupported("a Golomb tail longer than this decoder reads")); |
| 2047 | } |
| 2048 | } |
| 2049 | let mut x = 1u32; |
| 2050 | for _ in 1..length { |
| 2051 | x = (x << 1) | dec.literal(1); |
| 2052 | } |
| 2053 | Ok(x - 1) |
| 2054 | } |
| 2055 | |
| 2056 | /// `decode_symbol`'s end-of-block position (spec 5.11.39): the inverse of |
| 2057 | /// [`crate::tile`]'s `write_eob`. |
| 2058 | fn read_eob(dec: &mut SymbolDecoder, coding: &mut TxbTables, class: TxClass) -> usize { |
| 2059 | const GROUP_START: [usize; 12] = [0, 1, 2, 3, 5, 9, 17, 33, 65, 129, 257, 513]; |
| 2060 | const OFFSET_BITS: [u32; 12] = [0, 0, 0, 1, 2, 3, 4, 5, 6, 7, 8, 9]; |
| 2061 | |
| 2062 | let trace = std::env::var_os("EC_AV1_TRACE").is_some(); |
| 2063 | // libaom's `eob_flag_cdf*` tables carry a second dimension the 2D scan |
| 2064 | // never touches: `TX_CLASS_HORIZ`/`TX_CLASS_VERT` (`V_DCT`/`H_DCT`) adapt |
| 2065 | // a wholly separate CDF from every other tx_type (`decodetxb.c`'s |
| 2066 | // `eob_multi_ctx`). Missing that split desynced the very first |
| 2067 | // `V_DCT`/`H_DCT` TU this decoder ever produced (lane-av1tx4 r5, caught |
| 2068 | // against a real aomdec trace). |
| 2069 | let eob_pt: &mut [u16] = match (class, coding.eob_pt_class1.as_deref_mut()) { |
| 2070 | (TxClass::TwoD, _) | (_, None) => coding.eob_pt, |
| 2071 | (_, Some(class1)) => class1, |
| 2072 | }; |
| 2073 | if std::env::var_os("EC_AV1_EOBPT_CDF").is_some() { |
| 2074 | let (range, value) = dec.debug_state(); |
| 2075 | eprintln!("EC_AV1_EOBPT_CDF {eob_pt:?} range={range} value={value}"); |
| 2076 | } |
| 2077 | let group = dec.symbol(eob_pt) + 1; |
| 2078 | if trace { |
| 2079 | eprintln!("TRACE eob_pt value={group} rng={}", dec.debug_state().0); |
| 2080 | } |
| 2081 | let bits = OFFSET_BITS[group]; |
| 2082 | let mut offset = 0u32; |
| 2083 | if bits > 0 { |
| 2084 | let top = dec.symbol(&mut coding.eob_extra[group - 3]) as u32; |
| 2085 | if trace { |
| 2086 | eprintln!("TRACE eob_extra ctx={} value={top}", group - 3); |
| 2087 | } |
| 2088 | offset = top << (bits - 1); |
| 2089 | if bits > 1 { |
| 2090 | offset |= dec.literal(bits - 1); |
| 2091 | } |
| 2092 | } |
| 2093 | GROUP_START[group] + offset as usize |
| 2094 | } |
| 2095 | |
| 2096 | /// One transform block's levels, the inverse of [`crate::tile`]'s |
| 2097 | /// `write_coeffs`, returned as a `side * side` grid in raster order, along |
| 2098 | /// with the transform type that grid was coded under (`DCT_DCT` when the |
| 2099 | /// block's `TxbSet` carries no `tx_type` symbol at all, e.g. 32-point and |
| 2100 | /// 64-point transforms, which never code anything else). |
| 2101 | /// |
| 2102 | /// # Errors |
| 2103 | /// Returns an error if the `tx_type` symbol decodes to a value outside its |
| 2104 | /// CDF's own set (a corrupt or unsupported bitstream) — every value the |
| 2105 | /// intra `Tx_Type_Intra_Inv_Set2` and inter `Tx_Type_Inter_Inv_Set3` CDFs can |
| 2106 | /// produce dispatches to a real inverse transform. |
| 2107 | /// `Intra_Mode_To_Tx_Type` (spec 9.3): chroma's transform type is never its |
| 2108 | /// own coded symbol -- `TxbSet::Chroma*`'s `tx_type` slot is always `None` |
| 2109 | /// (a chroma block shares no CDF row with `Luma16`'s mode-indexed one) -- so |
| 2110 | /// libaom's own `intra_mode_to_tx_type` (`blockd.h`) derives it purely from |
| 2111 | /// the plane's own predicted mode instead. Every entry this table can |
| 2112 | /// produce is a member of both `TX_SET_INTRA_1` and `_2` (spec 9.3's two |
| 2113 | /// intra sets differ only in `IDTX`/`V_DCT`/`H_DCT`, none of which this |
| 2114 | /// table ever names), so unlike the CDF-coded luma path there is no |
| 2115 | /// ext-tx-set membership check to fall back from here. |
| 2116 | fn default_intra_tx_type(mode: u8) -> TxType { |
| 2117 | use crate::intra::{ |
| 2118 | D45_PRED, D67_PRED, D113_PRED, D135_PRED, D157_PRED, D203_PRED, DC_PRED, H_PRED, |
| 2119 | PAETH_PRED, SMOOTH_H_PRED, SMOOTH_PRED, SMOOTH_V_PRED, V_PRED, |
| 2120 | }; |
| 2121 | match mode { |
| 2122 | DC_PRED | D45_PRED => TxType::DctDct, |
| 2123 | V_PRED | D113_PRED | D67_PRED | SMOOTH_V_PRED => TxType::AdstDct, |
| 2124 | H_PRED | D157_PRED | D203_PRED | SMOOTH_H_PRED => TxType::DctAdst, |
| 2125 | D135_PRED | SMOOTH_PRED | PAETH_PRED => TxType::AdstAdst, |
| 2126 | other => panic!("intra mode {other} has no Intra_Mode_To_Tx_Type entry"), |
| 2127 | } |
| 2128 | } |
| 2129 | |
| 2130 | /// `is_smooth` (libaom `reconintra.c`): true for `SMOOTH_PRED..=SMOOTH_H_PRED` |
| 2131 | /// (9..=11) — deliberately excludes `PAETH_PRED` (12), which the spec's own |
| 2132 | /// `get_intra_edge_filter_type`/libaom `get_filt_type` never treat as smooth. |
| 2133 | fn is_smooth_mode(mode: usize) -> bool { |
| 2134 | (crate::intra::SMOOTH_PRED as usize..=crate::intra::SMOOTH_H_PRED as usize).contains(&mode) |
| 2135 | } |
| 2136 | |
| 2137 | fn read_coeffs( |
| 2138 | dec: &mut SymbolDecoder, |
| 2139 | coding: &mut TxbTables, |
| 2140 | scan: &[u16], |
| 2141 | skip_ctx: usize, |
| 2142 | sign_ctx: usize, |
| 2143 | // The chroma default (spec `Intra_Mode_To_Tx_Type`, see |
| 2144 | // [`default_intra_tx_type`]) for the sizes where `coding.tx_type` codes |
| 2145 | // nothing at all; `DctDct` for every luma call and every chroma call at |
| 2146 | // a size the spec forces to `DCT_DCT` regardless of mode (32-point and |
| 2147 | // up, `EXT_TX_SET_DCTONLY`) -- the caller already folds that in. |
| 2148 | default_tx_type: TxType, |
| 2149 | // `Some((w, h))` with `w != h` when this read is a superblock-level |
| 2150 | // HORZ/VERT strip's truncated luma corner: the CDF *set* resolves square |
| 2151 | // (`side`/`coding` above), but `av1_nz_map_ctx_offset`'s position table |
| 2152 | // is indexed by the real, un-adjusted rectangular shape (lane-sbpart r8 |
| 2153 | // root cause, see [`base_ctx`]). `None` everywhere else -- every other |
| 2154 | // caller's block genuinely is `side` x `side`. |
| 2155 | rect_shape: Option<(usize, usize)>, |
| 2156 | ) -> Result<(Vec<i32>, TxType)> { |
| 2157 | let trace = std::env::var_os("EC_AV1_TRACE").is_some(); |
| 2158 | let side = coding.side; |
| 2159 | let mut grid = vec![0i32; side * side]; |
| 2160 | if std::env::var_os("EC_AV1_EOBPT_CDF").is_some() { |
| 2161 | let (range, value) = dec.debug_state(); |
| 2162 | eprintln!("EC_AV1_STATE_BEFORE_TXBSKIP range={range} value={value}"); |
| 2163 | } |
| 2164 | let all_zero = dec.symbol(&mut coding.txb_skip[skip_ctx]) == 1; |
| 2165 | if std::env::var_os("EC_TRACE_COEFF").is_some() { |
| 2166 | let (rng, _) = dec.debug_state(); |
| 2167 | eprintln!( |
| 2168 | "EC_COEFF_STEP tag=all_zero ctx={skip_ctx} all_zero={} rng={rng}", |
| 2169 | all_zero as i32 |
| 2170 | ); |
| 2171 | } |
| 2172 | if std::env::var_os("EC_AV1_TELL").is_some() { |
| 2173 | eprintln!( |
| 2174 | "TELL label=post_txb_skip ctx={skip_ctx} all_zero={} tell={} range={}", |
| 2175 | all_zero as u8, dec.debug_bitpos(), dec.debug_state().0 |
| 2176 | ); |
| 2177 | } |
| 2178 | if trace { |
| 2179 | eprintln!("TRACE all_zero ctx={skip_ctx} value={}", all_zero as i32); |
| 2180 | } |
| 2181 | if std::env::var_os("EC_AV1_EOBPT_CDF").is_some() { |
| 2182 | let (range, value) = dec.debug_state(); |
| 2183 | eprintln!("EC_AV1_STATE_AFTER_TXBSKIP range={range} value={value}"); |
| 2184 | } |
| 2185 | if all_zero { |
| 2186 | return Ok((grid, TxType::DctDct)); |
| 2187 | } |
| 2188 | let mut tx_type = default_tx_type; |
| 2189 | if let Some(tx_type_cdf) = coding.tx_type.as_deref_mut() { |
| 2190 | // The CDF row's own width names its set (lane-cdffwd2: extended |
| 2191 | // past the original two-way intra split once wider `reduced_tx_set |
| 2192 | // == 0` inter sets joined the table): 17 slots (16 symbols) is |
| 2193 | // `EXT_TX_SET_ALL16`, 13 (12 symbols) `EXT_TX_SET_DTT9_IDTX_1DDCT`, |
| 2194 | // 8 (7 symbols) `TX_SET_INTRA_1`, everything else (6 slots, 5 |
| 2195 | // symbols) the reduced `TX_SET_INTRA_2`/`TX_SET_INTER_3` two sets |
| 2196 | // share their symbol order with (`Tx_Type_Inter_Inv_Set3`'s two |
| 2197 | // members are a prefix of `Tx_Type_Intra_Inv_Set2`'s five). |
| 2198 | let len = tx_type_cdf.len(); |
| 2199 | if std::env::var_os("EC_AV1_EOBPT_CDF").is_some() && len == 3 { |
| 2200 | eprintln!("EC_AV1_TXTYPE32_CDF {tx_type_cdf:?}"); |
| 2201 | } |
| 2202 | let t = dec.symbol(tx_type_cdf); |
| 2203 | if std::env::var_os("EC_TRACE_COEFF").is_some() { |
| 2204 | let (rng, _) = dec.debug_state(); |
| 2205 | eprintln!("EC_COEFF_STEP tag=tx_type rng={rng}"); |
| 2206 | } |
| 2207 | if std::env::var_os("EC_AV1_EOBPT_CDF").is_some() { |
| 2208 | let (range, value) = dec.debug_state(); |
| 2209 | eprintln!("EC_AV1_STATE_AFTER_TXTYPE range={range} value={value}"); |
| 2210 | } |
| 2211 | if trace { |
| 2212 | eprintln!("TRACE tx_type value={t} len={len}"); |
| 2213 | } |
| 2214 | tx_type = match len { |
| 2215 | 17 => TxType::from_symbol_all16(t), |
| 2216 | 13 => TxType::from_symbol_set2_12(t), |
| 2217 | 8 => TxType::from_symbol_set1(t), |
| 2218 | _ => TxType::from_symbol(t), |
| 2219 | } |
| 2220 | .ok_or_else(|| unsupported(format!("a tx_type symbol outside its CDF's own set: {t}")))?; |
| 2221 | } |
| 2222 | |
| 2223 | let class = TxClass::of(tx_type); |
| 2224 | if class != TxClass::TwoD { |
| 2225 | TX_CLASS1_HITS.with(|c| c.set(c.get() + 1)); |
| 2226 | } |
| 2227 | let eob = read_eob(dec, coding, class); |
| 2228 | if trace { |
| 2229 | eprintln!("TRACE eob value={eob}"); |
| 2230 | } |
| 2231 | let ec_trace_coeff = std::env::var_os("EC_TRACE_COEFF").is_some(); |
| 2232 | if ec_trace_coeff { |
| 2233 | let (rng, _) = dec.debug_state(); |
| 2234 | eprintln!("EC_COEFF_STEP tag=eob eob={eob} rng={rng}"); |
| 2235 | } |
| 2236 | let class_scan; |
| 2237 | let scan: &[u16] = if class == TxClass::TwoD { |
| 2238 | scan |
| 2239 | } else { |
| 2240 | class_scan = class_scan_table(side, class); |
| 2241 | &class_scan |
| 2242 | }; |
| 2243 | let mut levels = vec![0i32; side * side]; |
| 2244 | for scan_idx in (0..eob).rev() { |
| 2245 | let pos = scan[scan_idx] as usize; |
| 2246 | let (row, col) = (pos / side, pos % side); |
| 2247 | let level = if scan_idx == eob - 1 { |
| 2248 | let ctx = eob_coeff_ctx(scan_idx, side * side); |
| 2249 | let v = dec.symbol(&mut coding.base_eob[ctx]) as i32 + 1; |
| 2250 | if trace { |
| 2251 | eprintln!( |
| 2252 | "TRACE base_eob scan_idx={scan_idx} pos={pos} row={row} col={col} ctx={ctx} value={v}" |
| 2253 | ); |
| 2254 | } |
| 2255 | v |
| 2256 | } else { |
| 2257 | let ctx = base_ctx(&levels, side, row, col, class, rect_shape); |
| 2258 | let v = dec.symbol(&mut coding.base[ctx]) as i32; |
| 2259 | if trace { |
| 2260 | eprintln!( |
| 2261 | "TRACE base scan_idx={scan_idx} pos={pos} row={row} col={col} ctx={ctx} value={v}" |
| 2262 | ); |
| 2263 | } |
| 2264 | v |
| 2265 | }; |
| 2266 | let level = if level > NUM_BASE_LEVELS { |
| 2267 | let ctx = br_ctx(&levels, side, row, col, class); |
| 2268 | let mut level = level; |
| 2269 | let mut sent = 0; |
| 2270 | loop { |
| 2271 | let k = dec.symbol(&mut coding.br[ctx]) as i32; |
| 2272 | if trace { |
| 2273 | eprintln!( |
| 2274 | "TRACE br scan_idx={scan_idx} pos={pos} row={row} col={col} ctx={ctx} value={k}" |
| 2275 | ); |
| 2276 | } |
| 2277 | level += k; |
| 2278 | sent += BR_STEP; |
| 2279 | if k < BR_STEP || sent >= COEFF_BASE_RANGE { |
| 2280 | break; |
| 2281 | } |
| 2282 | } |
| 2283 | level |
| 2284 | } else { |
| 2285 | level |
| 2286 | }; |
| 2287 | if ec_trace_coeff && scan_idx == eob - 1 { |
| 2288 | let (rng, _) = dec.debug_state(); |
| 2289 | eprintln!("EC_COEFF_STEP tag=base_eob level={level} rng={rng}"); |
| 2290 | } |
| 2291 | levels[pos] = level; |
| 2292 | } |
| 2293 | if ec_trace_coeff { |
| 2294 | let (rng, _) = dec.debug_state(); |
| 2295 | eprintln!("EC_COEFF_STEP tag=after_bases rng={rng}"); |
| 2296 | } |
| 2297 | |
| 2298 | for (c, &pos) in scan[..eob].iter().enumerate() { |
| 2299 | let level = levels[pos as usize]; |
| 2300 | if level == 0 { |
| 2301 | continue; |
| 2302 | } |
| 2303 | let negative = if pos == 0 { |
| 2304 | let v = dec.symbol(&mut coding.dc_sign[sign_ctx]) == 1; |
| 2305 | if trace { |
| 2306 | eprintln!("TRACE dc_sign ctx={sign_ctx} value={}", v as i32); |
| 2307 | } |
| 2308 | v |
| 2309 | } else { |
| 2310 | dec.literal(1) == 1 |
| 2311 | }; |
| 2312 | if ec_trace_coeff { |
| 2313 | let (rng, _) = dec.debug_state(); |
| 2314 | eprintln!("EC_COEFF_STEP tag=sign c={c} sign={} rng={rng}", negative as i32); |
| 2315 | } |
| 2316 | let level = if level.abs_diff(0) as i32 > MAX_BR_LEVEL { |
| 2317 | let g = read_golomb(dec)?; |
| 2318 | if trace { |
| 2319 | eprintln!("TRACE golomb pos={pos} value={g}"); |
| 2320 | } |
| 2321 | level + g as i32 |
| 2322 | } else { |
| 2323 | level |
| 2324 | }; |
| 2325 | if ec_trace_coeff { |
| 2326 | let (rng, _) = dec.debug_state(); |
| 2327 | eprintln!("EC_COEFF_STEP tag=post_golomb c={c} level={level} rng={rng}"); |
| 2328 | } |
| 2329 | grid[pos as usize] = if negative { -level } else { level }; |
| 2330 | } |
| 2331 | Ok((grid, tx_type)) |
| 2332 | } |
| 2333 | |
| 2334 | /// [`read_coeffs`] widened to `(w, h)` (lane-rectwire), restricted to |
| 2335 | /// `TxClass::TwoD` -- the only class either rect size pair this decoder codes |
| 2336 | /// can ever produce (see [`decode_block_rect`]'s doc comment): refuses by |
| 2337 | /// name rather than guess-decode if a `tx_type` symbol or non-2D class ever |
| 2338 | /// shows up here, since neither `base_ctx_rect`/`br_ctx_rect` nor |
| 2339 | /// [`class_scan_table`] have a rect form for those. |
| 2340 | fn read_coeffs_rect( |
| 2341 | dec: &mut SymbolDecoder, |
| 2342 | coding: &mut TxbTables, |
| 2343 | scan: &[u16], |
| 2344 | w: usize, |
| 2345 | h: usize, |
| 2346 | skip_ctx: usize, |
| 2347 | sign_ctx: usize, |
| 2348 | default_tx_type: TxType, |
| 2349 | ) -> Result<(Vec<i32>, TxType)> { |
| 2350 | let ec_trace_coeff = std::env::var_os("EC_TRACE_COEFF").is_some(); |
| 2351 | let mut grid = vec![0i32; w * h]; |
| 2352 | let all_zero = dec.symbol(&mut coding.txb_skip[skip_ctx]) == 1; |
| 2353 | if ec_trace_coeff { |
| 2354 | let (rng, _) = dec.debug_state(); |
| 2355 | eprintln!( |
| 2356 | "EC_COEFF_STEP tag=all_zero ctx={skip_ctx} all_zero={} rng={rng}", |
| 2357 | all_zero as i32 |
| 2358 | ); |
| 2359 | } |
| 2360 | if all_zero { |
| 2361 | return Ok((grid, TxType::DctDct)); |
| 2362 | } |
| 2363 | if coding.tx_type.is_some() { |
| 2364 | return Err(unsupported( |
| 2365 | "a tx_type symbol on a rectangular transform (never expected at this size)", |
| 2366 | )); |
| 2367 | } |
| 2368 | let tx_type = default_tx_type; |
| 2369 | if TxClass::of(tx_type) != TxClass::TwoD { |
| 2370 | return Err(unsupported( |
| 2371 | "a non-2D tx class on a rectangular transform (never expected at this size)", |
| 2372 | )); |
| 2373 | } |
| 2374 | let eob = read_eob(dec, coding, TxClass::TwoD); |
| 2375 | if ec_trace_coeff { |
| 2376 | let (rng, _) = dec.debug_state(); |
| 2377 | eprintln!("EC_COEFF_STEP tag=eob eob={eob} rng={rng}"); |
| 2378 | } |
| 2379 | let mut levels = vec![0i32; w * h]; |
| 2380 | for scan_idx in (0..eob).rev() { |
| 2381 | let pos = scan[scan_idx] as usize; |
| 2382 | let (row, col) = (pos / w, pos % w); |
| 2383 | let level = if scan_idx == eob - 1 { |
| 2384 | let ctx = eob_coeff_ctx(scan_idx, w * h); |
| 2385 | let l = dec.symbol(&mut coding.base_eob[ctx]) as i32 + 1; |
| 2386 | if ec_trace_coeff { |
| 2387 | let (rng, _) = dec.debug_state(); |
| 2388 | eprintln!( |
| 2389 | "EC_COEFF_STEP tag=base_eob c={scan_idx} pos={pos} ctx={ctx} level={} rng={rng}", |
| 2390 | l - 1 |
| 2391 | ); |
| 2392 | } |
| 2393 | l |
| 2394 | } else { |
| 2395 | let ctx = base_ctx_rect(&levels, w, h, row, col); |
| 2396 | let l = dec.symbol(&mut coding.base[ctx]) as i32; |
| 2397 | if ec_trace_coeff { |
| 2398 | let (rng, _) = dec.debug_state(); |
| 2399 | eprintln!( |
| 2400 | "EC_COEFF_STEP tag=base c={scan_idx} pos={pos} ctx={ctx} level={l} rng={rng}" |
| 2401 | ); |
| 2402 | } |
| 2403 | l |
| 2404 | }; |
| 2405 | let level = if level > NUM_BASE_LEVELS { |
| 2406 | let ctx = br_ctx_rect(&levels, w, h, row, col); |
| 2407 | let mut level = level; |
| 2408 | let mut sent = 0; |
| 2409 | loop { |
| 2410 | let k = dec.symbol(&mut coding.br[ctx]) as i32; |
| 2411 | if ec_trace_coeff { |
| 2412 | let (rng, _) = dec.debug_state(); |
| 2413 | eprintln!( |
| 2414 | "EC_COEFF_STEP tag=br c={scan_idx} pos={pos} ctx={ctx} k={k} rng={rng}" |
| 2415 | ); |
| 2416 | } |
| 2417 | level += k; |
| 2418 | sent += BR_STEP; |
| 2419 | if k < BR_STEP || sent >= COEFF_BASE_RANGE { |
| 2420 | break; |
| 2421 | } |
| 2422 | } |
| 2423 | level |
| 2424 | } else { |
| 2425 | level |
| 2426 | }; |
| 2427 | levels[pos] = level; |
| 2428 | } |
| 2429 | if ec_trace_coeff { |
| 2430 | let (rng, _) = dec.debug_state(); |
| 2431 | eprintln!("EC_COEFF_STEP tag=after_bases rng={rng}"); |
| 2432 | } |
| 2433 | for &pos in &scan[..eob] { |
| 2434 | let level = levels[pos as usize]; |
| 2435 | if level == 0 { |
| 2436 | continue; |
| 2437 | } |
| 2438 | let negative = if pos == 0 { |
| 2439 | dec.symbol(&mut coding.dc_sign[sign_ctx]) == 1 |
| 2440 | } else { |
| 2441 | dec.literal(1) == 1 |
| 2442 | }; |
| 2443 | let level = if level.abs_diff(0) as i32 > MAX_BR_LEVEL { |
| 2444 | let g = read_golomb(dec)?; |
| 2445 | level + g as i32 |
| 2446 | } else { |
| 2447 | level |
| 2448 | }; |
| 2449 | grid[pos as usize] = if negative { -level } else { level }; |
| 2450 | } |
| 2451 | Ok((grid, tx_type)) |
| 2452 | } |
| 2453 | |
| 2454 | /// What one coded block leaves behind for the blocks that read it as a |
| 2455 | /// neighbour: whether it coded anything at all, and the sign of its DC — |
| 2456 | /// [`crate::tile`]'s own private `Neighbour`. |
| 2457 | #[derive(Clone, Copy, Default)] |
| 2458 | struct Neighbour { |
| 2459 | /// The transform unit's cumulative coefficient level (spec `cul_level`, |
| 2460 | /// `decodetxb.c`'s `read_coeffs_txb`): the sum of every coded level's |
| 2461 | /// magnitude, clamped to `COEFF_CONTEXT_MASK` (7). A plain "coded or not" |
| 2462 | /// boolean (`level != 0`) is enough for every context this decoder read |
| 2463 | /// until now, but the *luma* `txb_skip_ctx` of a transform unit smaller |
| 2464 | /// than its own block (`TxMode::Select`, spec `get_txb_ctx_general`) |
| 2465 | /// needs the actual magnitude tier of its above/left neighbours, not |
| 2466 | /// just whether they coded anything. |
| 2467 | level: u8, |
| 2468 | dc: Option<bool>, |
| 2469 | } |
| 2470 | |
| 2471 | fn neighbour_state(grid: &[i32]) -> Neighbour { |
| 2472 | let cul: u32 = grid.iter().map(|&l| l.unsigned_abs()).sum(); |
| 2473 | Neighbour { |
| 2474 | level: cul.min(7) as u8, |
| 2475 | dc: (grid[0] != 0).then_some(grid[0] < 0), |
| 2476 | } |
| 2477 | } |
| 2478 | |
| 2479 | /// spec `get_txb_ctx_general`'s `skip_contexts[top][left]` table (plane 0, |
| 2480 | /// transform unit smaller than the block it sits in): `top`/`left` are the |
| 2481 | /// above/left neighbours' magnitude tiers, each already clamped to 4. |
| 2482 | const SKIP_CONTEXTS: [[usize; 5]; 5] = [ |
| 2483 | [1, 2, 2, 2, 3], |
| 2484 | [2, 4, 4, 4, 5], |
| 2485 | [2, 4, 4, 4, 5], |
| 2486 | [2, 4, 4, 4, 5], |
| 2487 | [3, 5, 5, 5, 6], |
| 2488 | ]; |
| 2489 | |
| 2490 | /// The neighbour state a block's contexts are read from — a duplicate of |
| 2491 | /// [`crate::tile`]'s own private `Neighbours` (that module is another lane's |
| 2492 | /// territory this round), kept at the same two granularities the real writer |
| 2493 | /// tracks: `above_mode`/`left_mode`/`above_side`/`left_side` on the 16x16 |
| 2494 | /// (`SUB`) grid the partition/mode symbols read, and `above`/`left`'s |
| 2495 | /// per-plane coded/DC-sign state on the finer 4x4 (`MI`) grid the coefficient |
| 2496 | /// contexts read (spec `av1_set_entropy_contexts`) — which is what lets a |
| 2497 | /// chroma transform straddling the true, odd-sized frame edge see its |
| 2498 | /// neighbour's state clamped at its own subsampled edge rather than luma's. |
| 2499 | struct Neighbours { |
| 2500 | above: Vec<[Neighbour; 3]>, |
| 2501 | left: Vec<[Neighbour; 3]>, |
| 2502 | above_mode: Vec<usize>, |
| 2503 | left_mode: Vec<usize>, |
| 2504 | /// Chroma's own `uv_mode` companion to `above_mode`/`left_mode` (lane-chroma |
| 2505 | /// r3): a smooth/paeth `uv_mode`'s edge-filter strength (spec |
| 2506 | /// `get_intra_edge_filter_type`) reads the CHROMA neighbour's mode, not the |
| 2507 | /// luma one -- the same block can (and often does) code a different |
| 2508 | /// `uv_mode` than its own `mode`. Coarse [`SUB`]-grid like `above_mode`; |
| 2509 | /// [`Self::decode_leaf8`]'s per-leaf `prev_leaf` override (a real |
| 2510 | /// within-block neighbour finer than this grid) is luma-only -- a known, |
| 2511 | /// unexercised-by-gate corner-cut (every real-encoder gate in `stream.rs` |
| 2512 | /// uses `--min-partition-size=16`, so no leaf8 8x8 split ever reaches this). |
| 2513 | above_uv_mode: Vec<usize>, |
| 2514 | left_uv_mode: Vec<usize>, |
| 2515 | above_side: Vec<usize>, |
| 2516 | left_side: Vec<usize>, |
| 2517 | /// The same as `above_side`/`left_side`, but kept at the finer mi (4x4) |
| 2518 | /// granularity `above`/`left` are, rather than [`SUB`] -- [`crate::tile`]'s |
| 2519 | /// `above_side_mi`/`left_side_mi`: two 8x8 leaves of one straddling 16x16 |
| 2520 | /// block (lane-av1-rect) share a single [`SUB`]-grid cell, so a coarse |
| 2521 | /// array cannot tell the second leaf's partition symbol that the first |
| 2522 | /// one was coded at 8x8. |
| 2523 | above_side_mi: Vec<usize>, |
| 2524 | left_side_mi: Vec<usize>, |
| 2525 | /// Whether the block above/left of this column/row was coded `skip` -- |
| 2526 | /// an inter frame's own `skip` context (spec `SkipContext`), which the |
| 2527 | /// key-frame writer never tracks (its skip context is always zero); a |
| 2528 | /// duplicate of [`crate::tile`]'s private `Neighbours::above_skip`/ |
| 2529 | /// `left_skip`. |
| 2530 | above_skip: Vec<bool>, |
| 2531 | left_skip: Vec<bool>, |
| 2532 | /// libaom's `above_txfm_context`/`left_txfm_context` (`TXFM_CONTEXT`), at |
| 2533 | /// 4x4 mi granularity: the transform *width* (above) / *height* (left) in |
| 2534 | /// pixels last written over each unit by `set_txfm_ctxs`/ |
| 2535 | /// `txfm_partition_update`, which is what an inter block's `txfm_split` |
| 2536 | /// symbol reads its context from (lane-txselect, spec 5.11.17). |
| 2537 | /// [`TXFM_CTX_INIT`] until a block writes one; a fresh full-height `left` |
| 2538 | /// array is exactly libaom's per-superblock-row reset, since each mi row |
| 2539 | /// is only ever visited inside one superblock row. |
| 2540 | above_txfm: Vec<u8>, |
| 2541 | left_txfm: Vec<u8>, |
| 2542 | /// Whether the block above/left of this column/row coded `skip_mode` |
| 2543 | /// (spec 5.11.23) -- `av1_get_skip_mode_context`'s own neighbour lookup, |
| 2544 | /// lane-av1comp round 14. |
| 2545 | above_skip_mode: Vec<bool>, |
| 2546 | left_skip_mode: Vec<bool>, |
| 2547 | /// Whether the block above/left was coded inter -- the `is_inter` |
| 2548 | /// context (spec `av1_get_intra_inter_context`), a duplicate of |
| 2549 | /// [`crate::tile`]'s private `Neighbours::above_inter`/`left_inter`. |
| 2550 | above_inter: Vec<bool>, |
| 2551 | left_inter: Vec<bool>, |
| 2552 | /// The actual reference frame (`1..=7`, spec's `MV_REFERENCE_FRAME` |
| 2553 | /// alphabet) the block above/left of this column/row coded, or `-1` for |
| 2554 | /// an intra neighbour/frame border -- the `single_ref_p1`..`p6` context |
| 2555 | /// functions (`mvstack::ref_ctx` family) need the real reference, not |
| 2556 | /// just "was it inter" (lane-av1refs: every inter block used to be |
| 2557 | /// `LAST_FRAME` by construction, so `above_inter`/`left_inter` alone was |
| 2558 | /// enough). |
| 2559 | above_ref: Vec<i8>, |
| 2560 | left_ref: Vec<i8>, |
| 2561 | /// The block above/left's *second* reference frame when it was coded |
| 2562 | /// compound (spec `RefFrames[1]`), or `None` for single-ref/intra -- |
| 2563 | /// lane-av1comp: `mvstack::reference_mode_ctx`/`comp_reference_type_ctx` |
| 2564 | /// need a full [`crate::mvstack::NeighbourRef`], not just `above_ref`'s |
| 2565 | /// `i8`, once a compound `read_ref_frames` lands. Always `None` today |
| 2566 | /// (no decode path here ever produces a compound block yet). |
| 2567 | above_ref1: Vec<Option<i8>>, |
| 2568 | left_ref1: Vec<Option<i8>>, |
| 2569 | /// The precomputed `get_comp_group_idx_context`/`get_comp_index_context` |
| 2570 | /// neighbour contribution (spec 5.11.25, libaom `pred_common.h`): |
| 2571 | /// a compound neighbour's own `comp_group_idx`/`compound_idx` bit, or |
| 2572 | /// the "ref_frame[0] == ALTREF_FRAME" special case (`3`/`1`) for a |
| 2573 | /// single-ref one, or `0` for intra/unavailable -- lane-av1comp. |
| 2574 | above_comp_group_idx: Vec<u8>, |
| 2575 | left_comp_group_idx: Vec<u8>, |
| 2576 | above_compound_idx: Vec<u8>, |
| 2577 | left_compound_idx: Vec<u8>, |
| 2578 | /// `interp_filter[0]`/`[1]` (horizontal/vertical kernel index, 0..=2, or |
| 2579 | /// `3` = "no info": an intra neighbour or the frame's own border) of the |
| 2580 | /// block above/left of this column/row -- spec |
| 2581 | /// `av1_get_pred_context_switchable_interp`'s own neighbour lookup. |
| 2582 | above_filter: Vec<[u8; 2]>, |
| 2583 | left_filter: Vec<[u8; 2]>, |
| 2584 | mi_cols: usize, |
| 2585 | mi_rows: usize, |
| 2586 | /// Per-8x8 (2x2 mi cells) `skip_txfm` flag over the whole padded frame, |
| 2587 | /// spec `av1_cdef_compute_sb_list`'s `is_8x8_block_skip`: CDEF (spec |
| 2588 | /// 7.15) never filters an 8x8 luma block whose covering coded block read |
| 2589 | /// `skip` -- one flag per 4x4 mi cell (finer than needed, but matches the |
| 2590 | /// grain [`Self::record_mi`] already writes at), read back at 8x8 |
| 2591 | /// granularity by [`apply_cdef`] since `skip` is constant across every |
| 2592 | /// mi cell one coded block covers. |
| 2593 | skip_grid: Vec<bool>, |
| 2594 | skip_grid_cols_mi: usize, |
| 2595 | /// Per-4x4-mi luma transform width in pixels (8/16/32/64) -- this decoder |
| 2596 | /// never splits a coded block's transform below its own size (round 2: |
| 2597 | /// `TxMode::Select`/`tx_depth` refused), so a block's own side *is* its |
| 2598 | /// transform size, spec 7.14's `get_transform_size`/`get_filter_level` |
| 2599 | /// edge-length lookup (`apply_deblock`). |
| 2600 | tx_grid: Vec<u8>, |
| 2601 | /// The transform HEIGHT companion to [`Self::tx_grid`]'s width -- |
| 2602 | /// lane-rect r2: TX_32X16/TX_16X32 strips deblock their horizontal |
| 2603 | /// edges by tx height (spec 7.14.2 `set_lpf_parameters` dir==VERT). |
| 2604 | tx_h_grid: Vec<u8>, |
| 2605 | /// Per-4x4-mi CHROMA transform width/height in pixels -- lane-tiny r4. |
| 2606 | /// A block's chroma transform is `av1_get_max_uv_txsize(bsize)`: the |
| 2607 | /// largest transform covering the subsampled BLOCK, capped at 32x32. It |
| 2608 | /// does NOT follow luma's `tx_depth` split, so deriving it as |
| 2609 | /// `tx_grid / 2` invented chroma transform edges the deblocker then |
| 2610 | /// filtered (32x16 frames, U plane, seeds 42/47). Written from the |
| 2611 | /// block's own mi span in [`Self::fill_lf_grid_rect`], the one place |
| 2612 | /// every caller routes through. |
| 2613 | uv_tx_grid: Vec<u8>, |
| 2614 | uv_tx_h_grid: Vec<u8>, |
| 2615 | /// Per-4x4-mi reference frame (`0` = intra, magnitude `1..=7` = |
| 2616 | /// `MV_REFERENCE_FRAME`, negative = that reference coded as `GLOBALMV` -- |
| 2617 | /// `lf_level`'s `mode_lf_lut` row 0, lane-av1golden2) |
| 2618 | /// -- the loop filter's ref/mode delta lookup (spec 7.14.4 |
| 2619 | /// `get_filter_level`, `loop_filter_ref_deltas[ref_frame]`; lane-av1refs: |
| 2620 | /// widened from a bare `is_inter` bool, which collapsed every non-`LAST` |
| 2621 | /// reference onto `LAST_FRAME`'s own delta). |
| 2622 | ref_grid: Vec<i8>, |
| 2623 | /// Per-4x4-mi snapshot of [`CURRENT_DELTA_LF`] at the moment this cell's |
| 2624 | /// block was decoded (spec `DeltaLF[i]`, `i` in `0..FRAME_LF_COUNT`, |
| 2625 | /// Y-vertical/Y-horizontal/U/V) -- lane-realworld r5: `lf_level`'s |
| 2626 | /// per-block additive term, mirroring [`Self::ref_grid`]'s own snapshot |
| 2627 | /// pattern. `!delta_lf_multi` mode broadcasts index 0 into all 4 slots |
| 2628 | /// at write time ([`Self::fill_lf_grid_rect`]) so a reader never needs |
| 2629 | /// to know which mode produced the value. |
| 2630 | delta_lf_grid: Vec<[i8; 4]>, |
| 2631 | /// The block above/left's own palette-Y size (0 = not a palette block) |
| 2632 | /// and its base colours -- `av1_get_palette_mode_ctx`/ |
| 2633 | /// `av1_get_palette_cache` (pred_common.c)'s neighbour lookup, lane-palette |
| 2634 | /// r2. UV has no equivalent yet (stage 2, unreconstructed). |
| 2635 | above_palette_size: Vec<usize>, |
| 2636 | left_palette_size: Vec<usize>, |
| 2637 | above_palette_colors: Vec<[u16; 8]>, |
| 2638 | left_palette_colors: Vec<[u16; 8]>, |
| 2639 | /// As the four fields above, for the neighbour's own chroma (U-channel |
| 2640 | /// only -- `av1_get_palette_cache`'s plane-1 lookup, [`Self::palette_uv_cache`]) |
| 2641 | /// palette size/colours (lane-palette2 r1). |
| 2642 | above_palette_uv_size: Vec<usize>, |
| 2643 | left_palette_uv_size: Vec<usize>, |
| 2644 | above_palette_uv_colors: Vec<[u16; 8]>, |
| 2645 | left_palette_uv_colors: Vec<[u16; 8]>, |
| 2646 | /// This tile's own top-left corner, in 4x4 mode-info units (spec |
| 2647 | /// `MiRowStart`/`MiColStart`) -- lane-tiles: every "is there really a |
| 2648 | /// neighbour" check compares against this instead of literal `0`, since a |
| 2649 | /// block at the tile's own top/left edge has no usable neighbour even |
| 2650 | /// though a decoded block sits there in the picture (spec `decode_tile`'s |
| 2651 | /// per-tile `clear_above_context`/`left_available`/`up_available`). |
| 2652 | /// `0` for a single-tile frame, matching every existing caller exactly. |
| 2653 | tile_row0_mi: usize, |
| 2654 | tile_col0_mi: usize, |
| 2655 | } |
| 2656 | |
| 2657 | impl Neighbours { |
| 2658 | /// `cols`/`rows` are in [`SUB`] units; `mi_cols`/`mi_rows` are the frame's |
| 2659 | /// true (unpadded) size in 4x4 mode-info units. |
| 2660 | fn new(cols: usize, rows: usize, mi_cols: usize, mi_rows: usize) -> Self { |
| 2661 | Self { |
| 2662 | above: vec![[Neighbour::default(); 3]; cols * (SUB / MI)], |
| 2663 | left: vec![[Neighbour::default(); 3]; rows * (SUB / MI)], |
| 2664 | above_mode: vec![DC_PRED; cols], |
| 2665 | left_mode: vec![DC_PRED; rows], |
| 2666 | above_uv_mode: vec![DC_PRED; cols], |
| 2667 | left_uv_mode: vec![DC_PRED; rows], |
| 2668 | above_side: vec![SB; cols], |
| 2669 | left_side: vec![SB; rows], |
| 2670 | above_side_mi: vec![SB; cols * (SUB / MI)], |
| 2671 | left_side_mi: vec![SB; rows * (SUB / MI)], |
| 2672 | above_txfm: vec![TXFM_CTX_INIT; cols * (SUB / MI)], |
| 2673 | left_txfm: vec![TXFM_CTX_INIT; rows * (SUB / MI)], |
| 2674 | // lane-inter8 r2: mi(4px)-granular, not [`SUB`]-granular -- an |
| 2675 | // 8x8 inter leaf's left neighbour is the OTHER leaf row of the |
| 2676 | // 16x16 block beside it, which a SUB-grid slot cannot name |
| 2677 | // (class context-read-from-one-cell). |
| 2678 | above_skip: vec![false; cols * (SUB / MI)], |
| 2679 | left_skip: vec![false; rows * (SUB / MI)], |
| 2680 | above_skip_mode: vec![false; cols * (SUB / MI)], |
| 2681 | left_skip_mode: vec![false; rows * (SUB / MI)], |
| 2682 | above_inter: vec![false; cols * (SUB / MI)], |
| 2683 | left_inter: vec![false; rows * (SUB / MI)], |
| 2684 | above_ref: vec![-1; cols * (SUB / MI)], |
| 2685 | left_ref: vec![-1; rows * (SUB / MI)], |
| 2686 | above_ref1: vec![None; cols * (SUB / MI)], |
| 2687 | left_ref1: vec![None; rows * (SUB / MI)], |
| 2688 | above_comp_group_idx: vec![0; cols * (SUB / MI)], |
| 2689 | left_comp_group_idx: vec![0; rows * (SUB / MI)], |
| 2690 | above_compound_idx: vec![0; cols * (SUB / MI)], |
| 2691 | left_compound_idx: vec![0; rows * (SUB / MI)], |
| 2692 | above_filter: vec![[3u8; 2]; cols * (SUB / MI)], |
| 2693 | left_filter: vec![[3u8; 2]; rows * (SUB / MI)], |
| 2694 | mi_cols, |
| 2695 | mi_rows, |
| 2696 | skip_grid: vec![false; cols * (SUB / MI) * rows * (SUB / MI)], |
| 2697 | skip_grid_cols_mi: cols * (SUB / MI), |
| 2698 | tx_grid: vec![0u8; cols * (SUB / MI) * rows * (SUB / MI)], |
| 2699 | tx_h_grid: vec![0u8; cols * (SUB / MI) * rows * (SUB / MI)], |
| 2700 | uv_tx_grid: vec![0u8; cols * (SUB / MI) * rows * (SUB / MI)], |
| 2701 | uv_tx_h_grid: vec![0u8; cols * (SUB / MI) * rows * (SUB / MI)], |
| 2702 | ref_grid: vec![0i8; cols * (SUB / MI) * rows * (SUB / MI)], |
| 2703 | delta_lf_grid: vec![[0i8; 4]; cols * (SUB / MI) * rows * (SUB / MI)], |
| 2704 | above_palette_size: vec![0; cols], |
| 2705 | left_palette_size: vec![0; rows], |
| 2706 | above_palette_colors: vec![[0u16; 8]; cols], |
| 2707 | left_palette_colors: vec![[0u16; 8]; rows], |
| 2708 | above_palette_uv_size: vec![0; cols], |
| 2709 | left_palette_uv_size: vec![0; rows], |
| 2710 | above_palette_uv_colors: vec![[0u16; 8]; cols], |
| 2711 | left_palette_uv_colors: vec![[0u16; 8]; rows], |
| 2712 | tile_row0_mi: 0, |
| 2713 | tile_col0_mi: 0, |
| 2714 | } |
| 2715 | } |
| 2716 | |
| 2717 | /// spec `decode_tile`'s per-tile reset: `clear_above_context` over this |
| 2718 | /// tile's own column span (`col1_mi` exclusive), plus recording |
| 2719 | /// `(row0_mi, col0_mi)` as the tile's own top-left mi cell for every |
| 2720 | /// availability check below. Left context resets every superblock row |
| 2721 | /// regardless of tile ([`Self::start_row`], already called once per SB |
| 2722 | /// row by every caller) so it needs no tile-specific reset here. |
| 2723 | fn start_tile(&mut self, row0_mi: usize, col0_mi: usize, col1_mi: usize) { |
| 2724 | // lane-seg: `AvailU`/`AvailL` for the segment-id neighbour prediction |
| 2725 | // are tile-relative, same as every other neighbour lookup here. |
| 2726 | SEG_TILE_ORIGIN.with(|c| c.set((row0_mi, col0_mi))); |
| 2727 | self.tile_row0_mi = row0_mi; |
| 2728 | self.tile_col0_mi = col0_mi; |
| 2729 | let end = col1_mi.min(self.above.len()); |
| 2730 | for i in col0_mi.min(end)..end { |
| 2731 | self.above[i] = Default::default(); |
| 2732 | self.above_side_mi[i] = SB; |
| 2733 | } |
| 2734 | let (sc0, sc1) = ( |
| 2735 | col0_mi / (SUB / MI), |
| 2736 | (col1_mi / (SUB / MI)).min(self.above_mode.len()), |
| 2737 | ); |
| 2738 | for i in sc0.min(sc1)..sc1 { |
| 2739 | self.above_mode[i] = DC_PRED; |
| 2740 | self.above_side[i] = SB; |
| 2741 | } |
| 2742 | // lane-inter8 r2: the inter bands are mi-granular, so they reset over |
| 2743 | // the tile's own mi column span directly. |
| 2744 | let mend = col1_mi.min(self.above_skip.len()); |
| 2745 | for i in col0_mi.min(mend)..mend { |
| 2746 | self.above_skip[i] = false; |
| 2747 | self.above_skip_mode[i] = false; |
| 2748 | self.above_inter[i] = false; |
| 2749 | self.above_ref[i] = -1; |
| 2750 | self.above_ref1[i] = None; |
| 2751 | self.above_comp_group_idx[i] = 0; |
| 2752 | self.above_compound_idx[i] = 0; |
| 2753 | self.above_filter[i] = [3u8; 2]; |
| 2754 | } |
| 2755 | } |
| 2756 | |
| 2757 | /// Writes a just-decoded block's own palette-Y size/colours (`size == 0` |
| 2758 | /// for a non-palette block, clearing any stale neighbour state) into every |
| 2759 | /// [`SUB`]-grid cell it covers on both the above and left edges -- |
| 2760 | /// mirrors [`Self::record_rect`]'s loop shape, square-only (this lane's |
| 2761 | /// call sites are all square blocks). |
| 2762 | fn record_palette_y(&mut self, at: (usize, usize), side: usize, size: usize, colors: [u16; 8]) { |
| 2763 | let (r, c) = at; |
| 2764 | for cell in 0..side / SUB { |
| 2765 | self.above_palette_size[c + cell] = size; |
| 2766 | self.above_palette_colors[c + cell] = colors; |
| 2767 | } |
| 2768 | for cell in 0..side / SUB { |
| 2769 | self.left_palette_size[r + cell] = size; |
| 2770 | self.left_palette_colors[r + cell] = colors; |
| 2771 | } |
| 2772 | } |
| 2773 | |
| 2774 | /// `av1_get_palette_mode_ctx` (pred_common.h:197): count of the above/left |
| 2775 | /// neighbours that are themselves a palette-Y block, plus |
| 2776 | /// `av1_get_palette_cache`'s own merged, ascending, deduplicated colour |
| 2777 | /// cache (pred_common.c:73) -- the above neighbour is excluded at a |
| 2778 | /// superblock's own top row (`r % 4 == 0` in [`SUB`]-grid units, 4 cells |
| 2779 | /// per 64px SB, `MIN_SB_SIZE_LOG2`), the left neighbour never is. |
| 2780 | fn palette_ctx_and_cache(&self, at: (usize, usize)) -> (usize, Vec<u16>) { |
| 2781 | let (r, c) = at; |
| 2782 | let above_ok = r % 4 != 0; |
| 2783 | let (above_n, above_colors) = if above_ok && self.above_palette_size[c] > 0 { |
| 2784 | (self.above_palette_size[c], self.above_palette_colors[c]) |
| 2785 | } else { |
| 2786 | (0, [0u16; 8]) |
| 2787 | }; |
| 2788 | let (left_n, left_colors) = if self.left_palette_size[r] > 0 { |
| 2789 | (self.left_palette_size[r], self.left_palette_colors[r]) |
| 2790 | } else { |
| 2791 | (0, [0u16; 8]) |
| 2792 | }; |
| 2793 | let ctx = usize::from(above_n > 0) + usize::from(left_n > 0); |
| 2794 | let mut cache = Vec::with_capacity(16); |
| 2795 | let push_dedup = |cache: &mut Vec<u16>, v: u16| { |
| 2796 | if cache.last() != Some(&v) { |
| 2797 | cache.push(v); |
| 2798 | } |
| 2799 | }; |
| 2800 | let (mut ai, mut li) = (0usize, 0usize); |
| 2801 | while ai < above_n && li < left_n { |
| 2802 | let (va, vl) = (above_colors[ai], left_colors[li]); |
| 2803 | if vl < va { |
| 2804 | push_dedup(&mut cache, vl); |
| 2805 | li += 1; |
| 2806 | } else { |
| 2807 | push_dedup(&mut cache, va); |
| 2808 | ai += 1; |
| 2809 | if vl == va { |
| 2810 | li += 1; |
| 2811 | } |
| 2812 | } |
| 2813 | } |
| 2814 | while ai < above_n { |
| 2815 | push_dedup(&mut cache, above_colors[ai]); |
| 2816 | ai += 1; |
| 2817 | } |
| 2818 | while li < left_n { |
| 2819 | push_dedup(&mut cache, left_colors[li]); |
| 2820 | li += 1; |
| 2821 | } |
| 2822 | (ctx, cache) |
| 2823 | } |
| 2824 | |
| 2825 | /// As [`Self::record_palette_y`], for a just-decoded block's own chroma |
| 2826 | /// (U-channel) palette size/colours -- `size == 0` clears stale state the |
| 2827 | /// same way (lane-palette2 r1). |
| 2828 | fn record_palette_uv(&mut self, at: (usize, usize), side: usize, size: usize, colors: [u16; 8]) { |
| 2829 | let (r, c) = at; |
| 2830 | for cell in 0..side / SUB { |
| 2831 | self.above_palette_uv_size[c + cell] = size; |
| 2832 | self.above_palette_uv_colors[c + cell] = colors; |
| 2833 | } |
| 2834 | for cell in 0..side / SUB { |
| 2835 | self.left_palette_uv_size[r + cell] = size; |
| 2836 | self.left_palette_uv_colors[r + cell] = colors; |
| 2837 | } |
| 2838 | } |
| 2839 | |
| 2840 | /// [`Self::palette_ctx_and_cache`]'s cache half, for the U channel |
| 2841 | /// (`av1_get_palette_cache(xd, 1, cache)`) -- `palette_uv_mode_ctx` needs |
| 2842 | /// no neighbour lookup at all (it is this block's own just-decided Y |
| 2843 | /// palette use, `read_palette_mode_info`'s `pmi->palette_size[0] > 0`), |
| 2844 | /// so only the cache is wanted here. |
| 2845 | fn palette_uv_cache(&self, at: (usize, usize)) -> Vec<u16> { |
| 2846 | let (r, c) = at; |
| 2847 | let above_ok = r % 4 != 0; |
| 2848 | let (above_n, above_colors) = if above_ok && self.above_palette_uv_size[c] > 0 { |
| 2849 | (self.above_palette_uv_size[c], self.above_palette_uv_colors[c]) |
| 2850 | } else { |
| 2851 | (0, [0u16; 8]) |
| 2852 | }; |
| 2853 | let (left_n, left_colors) = if self.left_palette_uv_size[r] > 0 { |
| 2854 | (self.left_palette_uv_size[r], self.left_palette_uv_colors[r]) |
| 2855 | } else { |
| 2856 | (0, [0u16; 8]) |
| 2857 | }; |
| 2858 | let mut cache = Vec::with_capacity(16); |
| 2859 | let push_dedup = |cache: &mut Vec<u16>, v: u16| { |
| 2860 | if cache.last() != Some(&v) { |
| 2861 | cache.push(v); |
| 2862 | } |
| 2863 | }; |
| 2864 | let (mut ai, mut li) = (0usize, 0usize); |
| 2865 | while ai < above_n && li < left_n { |
| 2866 | let (va, vl) = (above_colors[ai], left_colors[li]); |
| 2867 | if vl < va { |
| 2868 | push_dedup(&mut cache, vl); |
| 2869 | li += 1; |
| 2870 | } else { |
| 2871 | push_dedup(&mut cache, va); |
| 2872 | ai += 1; |
| 2873 | if vl == va { |
| 2874 | li += 1; |
| 2875 | } |
| 2876 | } |
| 2877 | } |
| 2878 | while ai < above_n { |
| 2879 | push_dedup(&mut cache, above_colors[ai]); |
| 2880 | ai += 1; |
| 2881 | } |
| 2882 | while li < left_n { |
| 2883 | push_dedup(&mut cache, left_colors[li]); |
| 2884 | li += 1; |
| 2885 | } |
| 2886 | cache |
| 2887 | } |
| 2888 | |
| 2889 | /// Marks every 4x4 mi cell a just-decoded coded block covers with its own |
| 2890 | /// transform width (in pixels) and `is_inter` flag -- the loop filter's |
| 2891 | /// per-edge lookup (`apply_deblock`), filled at the same call sites and |
| 2892 | /// units as [`Self::fill_skip_grid`]. |
| 2893 | fn fill_lf_grid(&mut self, at_mi: (usize, usize), side_mi: usize, tx_px: u8, ref_frame: i8) { |
| 2894 | self.fill_lf_grid_rect(at_mi, side_mi, side_mi, tx_px, tx_px, ref_frame); |
| 2895 | } |
| 2896 | |
| 2897 | /// [`Self::fill_lf_grid`] with independent row/col extents -- lane-partitions |
| 2898 | /// r1: a true rectangular strip's mi span isn't `side_mi` square (`w_mi` |
| 2899 | /// != `h_mi`); `tx_px` still names a single scalar (this decoder's |
| 2900 | /// existing corner-cut, `Neighbours::tx_grid`'s own doc comment -- a |
| 2901 | /// rectangular strip's transform is `TX_32X16`/`TX_16X32`, not tracked |
| 2902 | /// per-axis, matching lane-warp r5's `HORZ_B` top-strip precedent). |
| 2903 | fn fill_lf_grid_rect( |
| 2904 | &mut self, |
| 2905 | at_mi: (usize, usize), |
| 2906 | w_mi: usize, |
| 2907 | h_mi: usize, |
| 2908 | tx_px: u8, |
| 2909 | tx_h_px: u8, |
| 2910 | ref_frame: i8, |
| 2911 | ) { |
| 2912 | let (mi_r, mi_c) = at_mi; |
| 2913 | // `av1_get_max_uv_txsize` under 4:2:0: the block's chroma extent is |
| 2914 | // half its luma one (min 4 px, the smallest transform), capped at |
| 2915 | // TX_32X32 -- independent of this block's luma `tx_depth`. |
| 2916 | let uv_tx_w = ((w_mi * MI / 2).max(4).min(32)) as u8; |
| 2917 | let uv_tx_h = ((h_mi * MI / 2).max(4).min(32)) as u8; |
| 2918 | let cur = CURRENT_DELTA_LF.with(|c| c.get()); |
| 2919 | let snapshot: [i8; 4] = if DELTA_LF_MULTI.with(|c| c.get()) { |
| 2920 | std::array::from_fn(|i| cur[i].clamp(-63, 63) as i8) |
| 2921 | } else { |
| 2922 | [cur[0].clamp(-63, 63) as i8; 4] |
| 2923 | }; |
| 2924 | for rr in 0..h_mi { |
| 2925 | for cc in 0..w_mi { |
| 2926 | let idx = (mi_r + rr) * self.skip_grid_cols_mi + (mi_c + cc); |
| 2927 | if let Some(cell) = self.tx_grid.get_mut(idx) { |
| 2928 | *cell = tx_px; |
| 2929 | } |
| 2930 | if let Some(cell) = self.tx_h_grid.get_mut(idx) { |
| 2931 | *cell = tx_h_px; |
| 2932 | } |
| 2933 | if let Some(cell) = self.uv_tx_grid.get_mut(idx) { |
| 2934 | *cell = uv_tx_w; |
| 2935 | } |
| 2936 | if let Some(cell) = self.uv_tx_h_grid.get_mut(idx) { |
| 2937 | *cell = uv_tx_h; |
| 2938 | } |
| 2939 | if let Some(cell) = self.ref_grid.get_mut(idx) { |
| 2940 | *cell = ref_frame; |
| 2941 | } |
| 2942 | if let Some(cell) = self.delta_lf_grid.get_mut(idx) { |
| 2943 | *cell = snapshot; |
| 2944 | } |
| 2945 | } |
| 2946 | } |
| 2947 | } |
| 2948 | |
| 2949 | /// Marks every 4x4 mi cell a just-decoded coded block covers with its |
| 2950 | /// `skip` flag -- `at_mi`/`side_mi` are the block's own position and |
| 2951 | /// width/height in 4x4 mode-info units (mirroring [`Self::record_mi`]'s |
| 2952 | /// own units, not [`Self::record`]'s [`SUB`]-grid ones). |
| 2953 | fn fill_skip_grid(&mut self, at_mi: (usize, usize), side_mi: usize, skip: bool) { |
| 2954 | self.fill_skip_grid_rect(at_mi, side_mi, side_mi, skip); |
| 2955 | } |
| 2956 | |
| 2957 | /// [`Self::fill_skip_grid`] with independent row/col extents (lane-partitions r1). |
| 2958 | fn fill_skip_grid_rect(&mut self, at_mi: (usize, usize), w_mi: usize, h_mi: usize, skip: bool) { |
| 2959 | let (mi_r, mi_c) = at_mi; |
| 2960 | for rr in 0..h_mi { |
| 2961 | for cc in 0..w_mi { |
| 2962 | if let Some(cell) = self |
| 2963 | .skip_grid |
| 2964 | .get_mut((mi_r + rr) * self.skip_grid_cols_mi + (mi_c + cc)) |
| 2965 | { |
| 2966 | *cell = skip; |
| 2967 | } |
| 2968 | } |
| 2969 | } |
| 2970 | } |
| 2971 | |
| 2972 | /// Whether the 8x8 luma block at mi position `(mi_r, mi_c)` (2x2 mi |
| 2973 | /// cells) was `skip_txfm` -- [`apply_cdef`]'s dlist membership test. |
| 2974 | /// Spec/libaom `is_8x8_block_skip` requires *every* one of the four 4x4 |
| 2975 | /// mi cells the 8x8 covers to be skip (a smaller-than-8x8 partition can |
| 2976 | /// straddle the region with a mix of skip/non-skip blocks). |
| 2977 | /// A single 4x4 mi cell's own `skip` flag, unaggregated (unlike |
| 2978 | /// [`Self::is_skip_txfm`]'s 8x8 all-four-cells rule) -- what the loop |
| 2979 | /// filter's `curr_skipped`/`pv_skip_txfm` (spec 7.14.2) want, since a |
| 2980 | /// filtered edge's two sides are each exactly one coded block wide. |
| 2981 | fn skip_at(&self, mi_r: usize, mi_c: usize) -> bool { |
| 2982 | self.skip_grid |
| 2983 | .get(mi_r * self.skip_grid_cols_mi + mi_c) |
| 2984 | .copied() |
| 2985 | .unwrap_or(false) |
| 2986 | } |
| 2987 | |
| 2988 | /// The `skip_txfm` context of a block whose top-left mi cell is |
| 2989 | /// `(mi_r, mi_c)`: libaom `av1_get_skip_txfm_context` |
| 2990 | /// (`pred_common.h:175-181`) is `above_mi->skip_txfm + left_mi->skip_txfm`, |
| 2991 | /// with an absent neighbour contributing 0. The mi grid is per tile, so |
| 2992 | /// row/column 0 is a tile edge and has no neighbour on that axis. |
| 2993 | fn skip_txfm_ctx(&self, mi_r: usize, mi_c: usize) -> usize { |
| 2994 | usize::from(mi_r > self.tile_row0_mi && self.skip_at(mi_r - 1, mi_c)) |
| 2995 | + usize::from(mi_c > self.tile_col0_mi && self.skip_at(mi_r, mi_c - 1)) |
| 2996 | } |
| 2997 | |
| 2998 | fn is_skip_txfm(&self, mi_r: usize, mi_c: usize) -> bool { |
| 2999 | (0..2).all(|rr| { |
| 3000 | (0..2).all(|cc| { |
| 3001 | self.skip_grid |
| 3002 | .get((mi_r + rr) * self.skip_grid_cols_mi + (mi_c + cc)) |
| 3003 | .copied() |
| 3004 | .unwrap_or(false) |
| 3005 | }) |
| 3006 | }) |
| 3007 | } |
| 3008 | |
| 3009 | fn start_row(&mut self) { |
| 3010 | self.left.iter_mut().for_each(|l| *l = Default::default()); |
| 3011 | self.left_mode.iter_mut().for_each(|m| *m = DC_PRED); |
| 3012 | self.left_uv_mode.iter_mut().for_each(|m| *m = DC_PRED); |
| 3013 | self.left_side.iter_mut().for_each(|s| *s = SB); |
| 3014 | self.left_side_mi.iter_mut().for_each(|s| *s = SB); |
| 3015 | self.left_skip.iter_mut().for_each(|s| *s = false); |
| 3016 | self.left_skip_mode.iter_mut().for_each(|s| *s = false); |
| 3017 | self.left_inter.iter_mut().for_each(|i| *i = false); |
| 3018 | self.left_ref.iter_mut().for_each(|r| *r = -1); |
| 3019 | self.left_ref1.iter_mut().for_each(|r| *r = None); |
| 3020 | self.left_comp_group_idx.iter_mut().for_each(|r| *r = 0); |
| 3021 | self.left_compound_idx.iter_mut().for_each(|r| *r = 0); |
| 3022 | self.left_filter.iter_mut().for_each(|f| *f = [3u8; 2]); |
| 3023 | } |
| 3024 | |
| 3025 | /// Records a block's `skip`/`is_inter`/`interp_filter` state for the next |
| 3026 | /// block that reads it as a neighbour -- [`crate::tile`]'s |
| 3027 | /// `record_inter`. `filter` is `[3, 3]` for an intra block (spec |
| 3028 | /// `get_ref_filter_type`'s "not this ref frame" case reads the same as |
| 3029 | /// "no neighbour"). `ref_frame` is `-1` for an intra block, else the |
| 3030 | /// `1..=7` reference it coded (lane-av1refs's `single_ref_p*_ctx` needs |
| 3031 | /// the real value, not just `is_inter`). |
| 3032 | fn record_inter( |
| 3033 | &mut self, |
| 3034 | at: (usize, usize), |
| 3035 | side: usize, |
| 3036 | skip: bool, |
| 3037 | is_inter: bool, |
| 3038 | ref_frame: i8, |
| 3039 | filter: [u8; 2], |
| 3040 | skip_mode: bool, |
| 3041 | ) { |
| 3042 | self.record_inter_rect(at, side, side, skip, is_inter, ref_frame, filter, skip_mode); |
| 3043 | } |
| 3044 | |
| 3045 | /// [`Self::record_inter`] with independent above (`w_sub`, [`SUB`]-grid |
| 3046 | /// columns) / left (`h_sub`, [`SUB`]-grid rows) extents -- lane-partitions |
| 3047 | /// r1: a true rectangular strip's above-context span (its own width) and |
| 3048 | /// left-context span (its own height) differ, unlike every caller before |
| 3049 | /// this round (class neighbour-votes-all-its-fields). |
| 3050 | #[allow(clippy::too_many_arguments)] |
| 3051 | fn record_inter_rect( |
| 3052 | &mut self, |
| 3053 | at: (usize, usize), |
| 3054 | w_sub: usize, |
| 3055 | h_sub: usize, |
| 3056 | skip: bool, |
| 3057 | is_inter: bool, |
| 3058 | ref_frame: i8, |
| 3059 | filter: [u8; 2], |
| 3060 | skip_mode: bool, |
| 3061 | ) { |
| 3062 | let (r, c) = at; |
| 3063 | self.record_inter_rect_mi( |
| 3064 | (r * (SUB / MI), c * (SUB / MI)), |
| 3065 | w_sub / MI, |
| 3066 | h_sub / MI, |
| 3067 | skip, |
| 3068 | is_inter, |
| 3069 | ref_frame, |
| 3070 | filter, |
| 3071 | skip_mode, |
| 3072 | ); |
| 3073 | } |
| 3074 | |
| 3075 | /// [`Self::record_inter_rect`] in mi (4px) units -- lane-inter8 r2: an |
| 3076 | /// 8x8 leaf covers 2x2 mi, half a [`SUB`] cell in each direction, so the |
| 3077 | /// side bands are written (and read) per mi. Every [`SUB`]-unit caller |
| 3078 | /// routes through the wrapper above. |
| 3079 | #[allow(clippy::too_many_arguments)] |
| 3080 | fn record_inter_rect_mi( |
| 3081 | &mut self, |
| 3082 | at_mi: (usize, usize), |
| 3083 | w_mi: usize, |
| 3084 | h_mi: usize, |
| 3085 | skip: bool, |
| 3086 | is_inter: bool, |
| 3087 | ref_frame: i8, |
| 3088 | filter: [u8; 2], |
| 3089 | skip_mode: bool, |
| 3090 | ) { |
| 3091 | let (r, c) = at_mi; |
| 3092 | let altref = is_inter && ref_frame == crate::mvstack::ALTREF_FRAME; |
| 3093 | for cell in 0..w_mi { |
| 3094 | self.above_skip[c + cell] = skip; |
| 3095 | self.above_skip_mode[c + cell] = skip_mode; |
| 3096 | self.above_inter[c + cell] = is_inter; |
| 3097 | self.above_ref[c + cell] = ref_frame; |
| 3098 | // lane-av1comp: no caller of `record_inter` ever decodes a |
| 3099 | // compound block yet (see [`Self::above_ref1`]'s doc); the |
| 3100 | // parameter this becomes lands with `read_ref_frames`. |
| 3101 | self.above_ref1[c + cell] = None; |
| 3102 | // libaom `get_comp_group_idx_context`/`get_comp_index_context`'s |
| 3103 | // single-ref special case: `ref_frame[0] == ALTREF_FRAME` reads |
| 3104 | // as `3`/`1` even though this block has no second reference. |
| 3105 | // `record_compound_ctx` overwrites this with the real bit for an |
| 3106 | // actual compound block's cells, called right after this one. |
| 3107 | self.above_comp_group_idx[c + cell] = if altref { 3 } else { 0 }; |
| 3108 | self.above_compound_idx[c + cell] = u8::from(altref); |
| 3109 | self.above_filter[c + cell] = filter; |
| 3110 | } |
| 3111 | for cell in 0..h_mi { |
| 3112 | self.left_skip[r + cell] = skip; |
| 3113 | self.left_skip_mode[r + cell] = skip_mode; |
| 3114 | self.left_inter[r + cell] = is_inter; |
| 3115 | self.left_ref[r + cell] = ref_frame; |
| 3116 | self.left_ref1[r + cell] = None; |
| 3117 | self.left_comp_group_idx[r + cell] = if altref { 3 } else { 0 }; |
| 3118 | self.left_compound_idx[r + cell] = u8::from(altref); |
| 3119 | self.left_filter[r + cell] = filter; |
| 3120 | } |
| 3121 | } |
| 3122 | |
| 3123 | /// Overwrites [`Self::record_inter`]'s ALTREF-special-case default with |
| 3124 | /// a real compound block's own `comp_group_idx`/`compound_idx` bits |
| 3125 | /// (libaom `has_second_ref(mbmi)` branch of `get_comp_group_idx_context`/ |
| 3126 | /// `get_comp_index_context`) -- called right after `record_inter` for |
| 3127 | /// the same `at`/`side`, only on the `COMPOUND_REFERENCE` path. Also |
| 3128 | /// stamps `above_ref1`/`left_ref1` with the block's real second |
| 3129 | /// reference, which `record_inter` always clears to `None` (lane-av1comp: |
| 3130 | /// this is the one caller that actually knows it) -- so the next block's |
| 3131 | /// [`NeighbourRef`](crate::mvstack::NeighbourRef) sees a real compound |
| 3132 | /// neighbour instead of `record_inter`'s single-ref default. |
| 3133 | fn record_compound_ctx( |
| 3134 | &mut self, |
| 3135 | at: (usize, usize), |
| 3136 | side: usize, |
| 3137 | ref1: i8, |
| 3138 | comp_group_idx: u8, |
| 3139 | compound_idx: u8, |
| 3140 | ) { |
| 3141 | self.record_compound_ctx_rect(at, side, side, ref1, comp_group_idx, compound_idx); |
| 3142 | } |
| 3143 | |
| 3144 | /// [`Self::record_compound_ctx`] with independent above/left extents, |
| 3145 | /// mirroring [`Self::record_inter_rect`] (lane-partitions r1). |
| 3146 | #[allow(clippy::too_many_arguments)] |
| 3147 | fn record_compound_ctx_rect( |
| 3148 | &mut self, |
| 3149 | at: (usize, usize), |
| 3150 | w_sub: usize, |
| 3151 | h_sub: usize, |
| 3152 | ref1: i8, |
| 3153 | comp_group_idx: u8, |
| 3154 | compound_idx: u8, |
| 3155 | ) { |
| 3156 | let (r, c) = at; |
| 3157 | self.record_compound_ctx_rect_mi( |
| 3158 | (r * (SUB / MI), c * (SUB / MI)), |
| 3159 | w_sub / MI, |
| 3160 | h_sub / MI, |
| 3161 | ref1, |
| 3162 | comp_group_idx, |
| 3163 | compound_idx, |
| 3164 | ); |
| 3165 | } |
| 3166 | |
| 3167 | /// [`Self::record_compound_ctx_rect`] in mi units (lane-inter8 r2, see |
| 3168 | /// [`Self::record_inter_rect_mi`]). |
| 3169 | fn record_compound_ctx_rect_mi( |
| 3170 | &mut self, |
| 3171 | at_mi: (usize, usize), |
| 3172 | w_mi: usize, |
| 3173 | h_mi: usize, |
| 3174 | ref1: i8, |
| 3175 | comp_group_idx: u8, |
| 3176 | compound_idx: u8, |
| 3177 | ) { |
| 3178 | let (r, c) = at_mi; |
| 3179 | for cell in 0..w_mi { |
| 3180 | self.above_ref1[c + cell] = Some(ref1); |
| 3181 | self.above_comp_group_idx[c + cell] = comp_group_idx; |
| 3182 | self.above_compound_idx[c + cell] = compound_idx; |
| 3183 | } |
| 3184 | for cell in 0..h_mi { |
| 3185 | self.left_ref1[r + cell] = Some(ref1); |
| 3186 | self.left_comp_group_idx[r + cell] = comp_group_idx; |
| 3187 | self.left_compound_idx[r + cell] = compound_idx; |
| 3188 | } |
| 3189 | } |
| 3190 | |
| 3191 | /// The context of a block's partition symbol: delegates to the mi-precise |
| 3192 | /// reader (same pattern as `around`/`around_mi`) -- `above_side`/ |
| 3193 | /// `left_side` are only ever advanced in whole-[`SUB`] steps by |
| 3194 | /// [`Self::record`], so a leaf8's `record_mi` (lane-av1-rect) -- which |
| 3195 | /// only touches the finer mi arrays -- leaves them stale for the *next* |
| 3196 | /// sibling's own partition symbol. |
| 3197 | fn partition_ctx(&self, at: (usize, usize), side: usize) -> usize { |
| 3198 | let (r, c) = at; |
| 3199 | self.partition_ctx_mi((r * (SUB / MI), c * (SUB / MI)), side) |
| 3200 | } |
| 3201 | |
| 3202 | /// [`Self::partition_ctx`] at mi granularity, for an 8x8 leaf of a |
| 3203 | /// straddling 16x16 block: reads the finer `above_side_mi`/`left_side_mi` |
| 3204 | /// arrays so the second leaf sees the first leaf's own 8x8 side rather |
| 3205 | /// than the enclosing 16x16 slot's stale, shared state. |
| 3206 | fn partition_ctx_mi(&self, (mi_r, mi_c): (usize, usize), side: usize) -> usize { |
| 3207 | 2 * usize::from(self.left_side_mi[mi_r] * 2 <= side) |
| 3208 | + usize::from(self.above_side_mi[mi_c] * 2 <= side) |
| 3209 | } |
| 3210 | |
| 3211 | /// The gathered coded/DC-sign state of the blocks above and to the left |
| 3212 | /// of a block `side` samples across, per plane. |
| 3213 | fn around(&self, (r, c): (usize, usize), side: usize) -> [(bool, bool, i32); 3] { |
| 3214 | self.around_mi((r * (SUB / MI), c * (SUB / MI)), side) |
| 3215 | } |
| 3216 | |
| 3217 | /// [`Self::around`] taking the block's position directly in 4x4 mode-info |
| 3218 | /// units, for the same reason [`Self::record_mi`] does. |
| 3219 | fn around_mi(&self, (mi_r, mi_c): (usize, usize), side: usize) -> [(bool, bool, i32); 3] { |
| 3220 | let side_mi = side / MI; |
| 3221 | std::array::from_fn(|plane| { |
| 3222 | let mut above_coded = false; |
| 3223 | let mut left_coded = false; |
| 3224 | let mut vote = 0; |
| 3225 | for cell in 0..side_mi { |
| 3226 | let above = &self.above[mi_c + cell][plane]; |
| 3227 | let left = &self.left[mi_r + cell][plane]; |
| 3228 | above_coded |= above.level != 0; |
| 3229 | left_coded |= left.level != 0; |
| 3230 | vote += dc_vote(above.dc) + dc_vote(left.dc); |
| 3231 | } |
| 3232 | (above_coded, left_coded, vote) |
| 3233 | }) |
| 3234 | } |
| 3235 | |
| 3236 | /// [`Self::around`] with independent above (`w`)/left (`h`) extents, in |
| 3237 | /// pixels -- lane-rectwire, mirrors [`Self::record_rect`]'s asymmetric |
| 3238 | /// extent pattern for a true rectangular strip's own coefficient context. |
| 3239 | fn around_rect(&self, (r, c): (usize, usize), w: usize, h: usize) -> [(bool, bool, i32); 3] { |
| 3240 | self.around_mi_rect((r * (SUB / MI), c * (SUB / MI)), w, h) |
| 3241 | } |
| 3242 | |
| 3243 | /// [`Self::around_rect`] taking the block's position directly in 4x4 |
| 3244 | /// mode-info units, mirroring [`Self::around_mi`]. |
| 3245 | fn around_mi_rect(&self, (mi_r, mi_c): (usize, usize), w: usize, h: usize) -> [(bool, bool, i32); 3] { |
| 3246 | let (w_mi, h_mi) = (w / MI, h / MI); |
| 3247 | std::array::from_fn(|plane| { |
| 3248 | let mut above_coded = false; |
| 3249 | let mut left_coded = false; |
| 3250 | let mut vote = 0; |
| 3251 | for cell in 0..w_mi { |
| 3252 | let above = &self.above[mi_c + cell][plane]; |
| 3253 | above_coded |= above.level != 0; |
| 3254 | vote += dc_vote(above.dc); |
| 3255 | } |
| 3256 | for cell in 0..h_mi { |
| 3257 | let left = &self.left[mi_r + cell][plane]; |
| 3258 | left_coded |= left.level != 0; |
| 3259 | vote += dc_vote(left.dc); |
| 3260 | } |
| 3261 | (above_coded, left_coded, vote) |
| 3262 | }) |
| 3263 | } |
| 3264 | |
| 3265 | /// The luma `txb_skip_ctx` of a transform unit smaller than its own block |
| 3266 | /// (spec `get_txb_ctx_general`, plane 0, `plane_bsize != tx_size`): the |
| 3267 | /// above/left neighbours' magnitude tiers, OR-reduced over the unit's own |
| 3268 | /// span and each clamped to 4, indexed into [`SKIP_CONTEXTS`]. Unlike |
| 3269 | /// [`Self::around_mi`]'s plain coded/uncoded bit, this needs the real |
| 3270 | /// cumulative level -- a lone-TU block never calls this (its `txb_skip_ctx` |
| 3271 | /// is always 0, spec's `plane_bsize == tx_size` branch). |
| 3272 | fn luma_skip_ctx(&self, (mi_r, mi_c): (usize, usize), side_mi: usize) -> usize { |
| 3273 | let mut top = 0u8; |
| 3274 | let mut left = 0u8; |
| 3275 | for cell in 0..side_mi { |
| 3276 | top |= self.above[mi_c + cell][0].level; |
| 3277 | left |= self.left[mi_r + cell][0].level; |
| 3278 | } |
| 3279 | let ctx = SKIP_CONTEXTS[(top as usize).min(4)][(left as usize).min(4)]; |
| 3280 | if std::env::var_os("EC_AV1_TRACE").is_some() { |
| 3281 | eprintln!( |
| 3282 | "TRACE luma_skip_ctx mi=({mi_r},{mi_c}) side_mi={side_mi} top={top} left={left} ctx={ctx}" |
| 3283 | ); |
| 3284 | } |
| 3285 | ctx |
| 3286 | } |
| 3287 | |
| 3288 | /// Writes one coded block into every cell it covers, on both grids: the |
| 3289 | /// mode/side arrays up to `side`'s own width, and the coefficient-context |
| 3290 | /// arrays up to the true frame edge — the units past it are left at their |
| 3291 | /// default (uncoded), even mid-cell, exactly as [`crate::tile`]'s own |
| 3292 | /// `record` leaves them (spec `av1_set_entropy_contexts`, which clamps to |
| 3293 | /// `blocks_wide`/`blocks_high` derived from the true `mi_cols`/`mi_rows`, |
| 3294 | /// not from this block's own side). |
| 3295 | fn record( |
| 3296 | &mut self, |
| 3297 | at: (usize, usize), |
| 3298 | side: usize, |
| 3299 | mode: usize, |
| 3300 | uv_mode: usize, |
| 3301 | grids: &[Vec<i32>; 3], |
| 3302 | ) { |
| 3303 | self.record_rect(at, side, side, mode, uv_mode, grids); |
| 3304 | } |
| 3305 | |
| 3306 | /// [`Self::record`] with independent above (`w`)/left (`h`) extents, in |
| 3307 | /// pixels -- lane-partitions r1: `above_side`/`left_side` (read back by |
| 3308 | /// [`Self::partition_ctx`]) are the neighbour's own width/height per the |
| 3309 | /// spec's per-edge partition-context rule, so a true rectangular block |
| 3310 | /// naturally wants two different values here rather than one. |
| 3311 | fn record_rect( |
| 3312 | &mut self, |
| 3313 | at: (usize, usize), |
| 3314 | w: usize, |
| 3315 | h: usize, |
| 3316 | mode: usize, |
| 3317 | uv_mode: usize, |
| 3318 | grids: &[Vec<i32>; 3], |
| 3319 | ) { |
| 3320 | let (r, c) = at; |
| 3321 | for cell in 0..w / SUB { |
| 3322 | self.above_mode[c + cell] = mode; |
| 3323 | self.above_uv_mode[c + cell] = uv_mode; |
| 3324 | self.above_side[c + cell] = w; |
| 3325 | } |
| 3326 | for cell in 0..h / SUB { |
| 3327 | self.left_mode[r + cell] = mode; |
| 3328 | self.left_uv_mode[r + cell] = uv_mode; |
| 3329 | self.left_side[r + cell] = h; |
| 3330 | } |
| 3331 | self.record_mi_rect((r * (SUB / MI), c * (SUB / MI)), w, h, grids); |
| 3332 | } |
| 3333 | |
| 3334 | /// The coefficient-context half of [`Self::record`], taking the block's |
| 3335 | /// position directly in 4x4 mode-info units rather than [`SUB`]-grid |
| 3336 | /// `(r, c)`: an 8x8 leaf under a straddling 16x16 (lane-av1-rect) sits at |
| 3337 | /// a mi offset [`Self::record`]'s SUB-unit `at` cannot name. |
| 3338 | fn record_mi(&mut self, at_mi: (usize, usize), side: usize, grids: &[Vec<i32>; 3]) { |
| 3339 | self.record_mi_rect(at_mi, side, side, grids); |
| 3340 | } |
| 3341 | |
| 3342 | /// [`Self::record_mi`] with independent above (`w`)/left (`h`) extents, |
| 3343 | /// in pixels (lane-partitions r1, mirroring [`Self::record_rect`]). |
| 3344 | fn record_mi_rect( |
| 3345 | &mut self, |
| 3346 | at_mi: (usize, usize), |
| 3347 | w: usize, |
| 3348 | h: usize, |
| 3349 | grids: &[Vec<i32>; 3], |
| 3350 | ) { |
| 3351 | let (mi_r, mi_c) = at_mi; |
| 3352 | let states: [Neighbour; 3] = std::array::from_fn(|plane| neighbour_state(&grids[plane])); |
| 3353 | let (w_mi, h_mi) = (w / MI, h / MI); |
| 3354 | for cell in 0..h_mi { |
| 3355 | self.left_side_mi[mi_r + cell] = h; |
| 3356 | } |
| 3357 | for cell in 0..w_mi { |
| 3358 | self.above_side_mi[mi_c + cell] = w; |
| 3359 | } |
| 3360 | // A chroma 4x4 unit straddling the true luma edge is still whole in |
| 3361 | // chroma's own halved grid, so libaom rounds the luma bound up to the |
| 3362 | // plane's own 4x4 unit before clamping a subsampled plane |
| 3363 | // (`av1_write_intra_coeffs_mb`, encodetxb.c). |
| 3364 | let round_up_even = |n: usize| n.div_ceil(2) * 2; |
| 3365 | let bound_h = [ |
| 3366 | self.mi_rows, |
| 3367 | round_up_even(self.mi_rows), |
| 3368 | round_up_even(self.mi_rows), |
| 3369 | ]; |
| 3370 | let bound_w = [ |
| 3371 | self.mi_cols, |
| 3372 | round_up_even(self.mi_cols), |
| 3373 | round_up_even(self.mi_cols), |
| 3374 | ]; |
| 3375 | for cell in 0..h_mi { |
| 3376 | self.left[mi_r + cell] = std::array::from_fn(|plane| { |
| 3377 | if cell < h_mi.min(bound_h[plane].saturating_sub(mi_r)) { |
| 3378 | states[plane] |
| 3379 | } else { |
| 3380 | Default::default() |
| 3381 | } |
| 3382 | }); |
| 3383 | } |
| 3384 | for cell in 0..w_mi { |
| 3385 | self.above[mi_c + cell] = std::array::from_fn(|plane| { |
| 3386 | if cell < w_mi.min(bound_w[plane].saturating_sub(mi_c)) { |
| 3387 | states[plane] |
| 3388 | } else { |
| 3389 | Default::default() |
| 3390 | } |
| 3391 | }); |
| 3392 | } |
| 3393 | } |
| 3394 | |
| 3395 | /// [`Self::record_mi`]'s plane-0 half only, for one luma transform unit |
| 3396 | /// inside a coded block whose luma transform is smaller than its own |
| 3397 | /// side (`TxMode::Select`): the real per-transform-unit coefficient |
| 3398 | /// context (spec `AboveLevelContext`/`LeftLevelContext`), unlike the |
| 3399 | /// coarse per-block `tx_grid`/`fill_lf_grid` state |
| 3400 | /// [`Self::record_split_luma`] still writes once for the whole block. |
| 3401 | /// Leaves `above_side_mi`/`left_side_mi` and the chroma planes alone -- |
| 3402 | /// the caller's own [`Self::record_split_luma`] call sets those once, |
| 3403 | /// at the block's own true side, not this TU's. |
| 3404 | fn record_mi_luma(&mut self, (mi_r, mi_c): (usize, usize), tx_px: usize, grid: &[i32]) { |
| 3405 | let state = neighbour_state(grid); |
| 3406 | let side_mi = tx_px / MI; |
| 3407 | for cell in 0..side_mi { |
| 3408 | if mi_r + cell < self.mi_rows { |
| 3409 | self.left[mi_r + cell][0] = state; |
| 3410 | } |
| 3411 | if mi_c + cell < self.mi_cols { |
| 3412 | self.above[mi_c + cell][0] = state; |
| 3413 | } |
| 3414 | } |
| 3415 | } |
| 3416 | |
| 3417 | /// [`Self::record`]'s mode/side and chroma-context bookkeeping, for a |
| 3418 | /// block whose luma was decoded as several transform units through |
| 3419 | /// [`Self::record_mi_luma`] rather than [`Self::record`]'s own single |
| 3420 | /// whole-block luma write -- everything [`Self::record`] does except |
| 3421 | /// plane 0, which is already correct per-TU by the time this runs. |
| 3422 | fn record_split_luma( |
| 3423 | &mut self, |
| 3424 | at: (usize, usize), |
| 3425 | side: usize, |
| 3426 | mode: usize, |
| 3427 | uv_mode: usize, |
| 3428 | chroma_grids: [&[i32]; 2], |
| 3429 | ) { |
| 3430 | self.record_split_luma_rect(at, side, side, mode, uv_mode, chroma_grids); |
| 3431 | } |
| 3432 | |
| 3433 | /// [`Self::record_split_luma`] with independent above (`w`)/left (`h`) |
| 3434 | /// extents in pixels (lane-rectsplit r1), the same relationship |
| 3435 | /// [`Self::record_rect`] has to [`Self::record`]: a HORZ/VERT strip whose |
| 3436 | /// luma transform split into several square units needs exactly this |
| 3437 | /// bookkeeping, at its own true rectangular extents. |
| 3438 | fn record_split_luma_rect( |
| 3439 | &mut self, |
| 3440 | at: (usize, usize), |
| 3441 | w: usize, |
| 3442 | h: usize, |
| 3443 | mode: usize, |
| 3444 | uv_mode: usize, |
| 3445 | chroma_grids: [&[i32]; 2], |
| 3446 | ) { |
| 3447 | let (r, c) = at; |
| 3448 | for cell in 0..w / SUB { |
| 3449 | self.above_mode[c + cell] = mode; |
| 3450 | self.above_uv_mode[c + cell] = uv_mode; |
| 3451 | self.above_side[c + cell] = w; |
| 3452 | } |
| 3453 | for cell in 0..h / SUB { |
| 3454 | self.left_mode[r + cell] = mode; |
| 3455 | self.left_uv_mode[r + cell] = uv_mode; |
| 3456 | self.left_side[r + cell] = h; |
| 3457 | } |
| 3458 | let (mi_r, mi_c) = (r * (SUB / MI), c * (SUB / MI)); |
| 3459 | let (w_mi, h_mi) = (w / MI, h / MI); |
| 3460 | for cell in 0..h_mi { |
| 3461 | self.left_side_mi[mi_r + cell] = h; |
| 3462 | } |
| 3463 | for cell in 0..w_mi { |
| 3464 | self.above_side_mi[mi_c + cell] = w; |
| 3465 | } |
| 3466 | let round_up_even = |n: usize| n.div_ceil(2) * 2; |
| 3467 | let (bound_h, bound_w) = (round_up_even(self.mi_rows), round_up_even(self.mi_cols)); |
| 3468 | for (plane_idx, grid) in chroma_grids.into_iter().enumerate() { |
| 3469 | let plane = plane_idx + 1; |
| 3470 | let state = neighbour_state(grid); |
| 3471 | for cell in 0..h_mi { |
| 3472 | if cell < h_mi.min(bound_h.saturating_sub(mi_r)) { |
| 3473 | self.left[mi_r + cell][plane] = state; |
| 3474 | } |
| 3475 | } |
| 3476 | for cell in 0..w_mi { |
| 3477 | if cell < w_mi.min(bound_w.saturating_sub(mi_c)) { |
| 3478 | self.above[mi_c + cell][plane] = state; |
| 3479 | } |
| 3480 | } |
| 3481 | } |
| 3482 | } |
| 3483 | } |
| 3484 | |
| 3485 | /// Reads one coded block's skip flag, luma mode, angle delta (if the mode |
| 3486 | /// carries one) and chroma mode, mirroring [`crate::tile`]'s `write_intra_mode` |
| 3487 | /// exactly — including its skip context, which the real key-frame writer |
| 3488 | /// always writes at a fixed context of zero (it never tracks a neighbour-based |
| 3489 | /// one for intra blocks; that is the inter-frame writer's rule instead). |
| 3490 | /// Refuses a nonzero angle delta: this decoder reconstructs the thirteen |
| 3491 | /// luma modes at their base angle only. Chroma is DC-only or, where CFL is |
| 3492 | /// offered (`cfl`, `is_cfl_allowed`, spec 5.11.5), chroma-from-luma -- the |
| 3493 | /// third element carries that mode's `(alpha_q3_u, alpha_q3_v)`, `None` for |
| 3494 | /// plain DC chroma. |
| 3495 | /// A block-size class index into [`cdf::FILTER_INTRA`]/`Cdfs::filter_intra`, |
| 3496 | /// or `None` when `side` is past `av1_filter_intra_allowed_bsize`'s <=32 |
| 3497 | /// bound (spec `filter_intra_mode_info` never reads a symbol there). |
| 3498 | fn filter_intra_size_class(side: usize) -> Option<usize> { |
| 3499 | match side { |
| 3500 | 4 => Some(0), |
| 3501 | 8 => Some(1), |
| 3502 | 16 => Some(2), |
| 3503 | 32 => Some(3), |
| 3504 | _ => None, |
| 3505 | } |
| 3506 | } |
| 3507 | |
| 3508 | /// [`filter_intra_size_class`] for a true `bw`x`bh` rect strip |
| 3509 | /// (lane-intradisp r1): `av1_filter_intra_allowed_bsize` permits any bsize |
| 3510 | /// with both dims <= 32, which includes `BLOCK_32X16`/`BLOCK_16X32` -- their |
| 3511 | /// own distinct `cdf::FILTER_INTRA` rows, classes 4/5. |
| 3512 | fn filter_intra_size_class_rect(bw: usize, bh: usize) -> Option<usize> { |
| 3513 | match (bw, bh) { |
| 3514 | (32, 16) => Some(4), |
| 3515 | (16, 32) => Some(5), |
| 3516 | _ if bw == bh => filter_intra_size_class(bw), |
| 3517 | _ => None, |
| 3518 | } |
| 3519 | } |
| 3520 | |
| 3521 | /// `av1_allow_palette`'s size bound (blockd.h: `block_size_wide/high <= |
| 3522 | /// MAX_PALETTE_BLOCK_WIDTH/HEIGHT` (64) and `sb_type >= BLOCK_8X8`) plus |
| 3523 | /// `av1_get_palette_bsize_ctx` (pred_common.h: `num_pels_log2_lookup[bsize] |
| 3524 | /// - num_pels_log2_lookup[BLOCK_8X8]`, i.e. `log2(bw*bh) - 6`) -- `None` past |
| 3525 | /// the bound (never gates a palette read), `Some(bsize_ctx)` otherwise. |
| 3526 | fn palette_bsize_ctx(side: usize) -> Option<usize> { |
| 3527 | palette_bsize_ctx_wh(side, side) |
| 3528 | } |
| 3529 | |
| 3530 | /// [`palette_bsize_ctx`], generalised to a true `bw`x`bh` rect strip |
| 3531 | /// (lane-palette2 r1) -- the same `log2(bw*bh) - 6` formula, this decoder's |
| 3532 | /// square call sites are just `bw == bh`. |
| 3533 | fn palette_bsize_ctx_wh(bw: usize, bh: usize) -> Option<usize> { |
| 3534 | match (bw, bh) { |
| 3535 | (8, 8) => Some(0), |
| 3536 | (16, 16) => Some(2), |
| 3537 | (32, 16) | (16, 32) => Some(3), |
| 3538 | (32, 32) => Some(4), |
| 3539 | (64, 64) => Some(6), |
| 3540 | _ => None, |
| 3541 | } |
| 3542 | } |
| 3543 | |
| 3544 | /// A just-decoded palette-Y block's own size, base colours (only the first |
| 3545 | /// `size` entries are real), and per-pixel colour-index map (`side*side`, |
| 3546 | /// row-major, each entry `< size`). |
| 3547 | struct PaletteY { |
| 3548 | size: usize, |
| 3549 | colors: [u16; 8], |
| 3550 | map: Vec<u8>, |
| 3551 | } |
| 3552 | |
| 3553 | /// As [`PaletteY`], for a just-decoded chroma palette block: one shared |
| 3554 | /// per-pixel colour-index map (spec `av1_visit_palette`'s plane-1 pass is |
| 3555 | /// shared between U and V, both co-located after 4:2:0 subsampling) plus |
| 3556 | /// each plane's own base colours. |
| 3557 | struct PaletteUv { |
| 3558 | size: usize, |
| 3559 | u_colors: [u16; 8], |
| 3560 | v_colors: [u16; 8], |
| 3561 | map: Vec<u8>, |
| 3562 | } |
| 3563 | |
| 3564 | /// `aom_ceil_log2` (aom_dsp/bitwriter_buffer.h-adjacent helper, used both by |
| 3565 | /// `read_palette_colors_y`'s shrinking `bits` and `av1_read_uniform`'s |
| 3566 | /// `get_unsigned_bits`, which is the same formula): smallest `n` with |
| 3567 | /// `2^n >= x`, `0` for `x <= 1`. |
| 3568 | fn ceil_log2(x: u32) -> u32 { |
| 3569 | if x < 2 { 0 } else { 32 - (x - 1).leading_zeros() } |
| 3570 | } |
| 3571 | |
| 3572 | /// `av1_read_uniform` (decoder.h:425): a uniform `0..n` value coded as |
| 3573 | /// `l-1` raw bits plus a conditional extra bit, `l = get_unsigned_bits(n)`. |
| 3574 | fn read_uniform(dec: &mut SymbolDecoder, n: usize) -> usize { |
| 3575 | let l = ceil_log2(n as u32); |
| 3576 | let m = (1usize << l) - n; |
| 3577 | let v = dec.literal(l - 1) as usize; |
| 3578 | if v < m { |
| 3579 | v |
| 3580 | } else { |
| 3581 | (v << 1) - m + dec.literal(1) as usize |
| 3582 | } |
| 3583 | } |
| 3584 | |
| 3585 | /// `merge_colors` (decodemv.c:462): interleaves the ascending `cached` |
| 3586 | /// colours (already-known, from the above/left neighbours) with the |
| 3587 | /// ascending `transmitted` ones (delta-coded off the wire) back into one |
| 3588 | /// ascending list of `cached.len() + transmitted.len()` colours -- a tied |
| 3589 | /// value picks the cached copy first, matching the C `<=`. |
| 3590 | fn merge_colors(transmitted: &[u16], cached: &[u16]) -> Vec<u16> { |
| 3591 | let mut out = Vec::with_capacity(transmitted.len() + cached.len()); |
| 3592 | let (mut ci, mut ti) = (0, 0); |
| 3593 | while ci < cached.len() && ti < transmitted.len() { |
| 3594 | if cached[ci] <= transmitted[ti] { |
| 3595 | out.push(cached[ci]); |
| 3596 | ci += 1; |
| 3597 | } else { |
| 3598 | out.push(transmitted[ti]); |
| 3599 | ti += 1; |
| 3600 | } |
| 3601 | } |
| 3602 | out.extend_from_slice(&cached[ci..]); |
| 3603 | out.extend_from_slice(&transmitted[ti..]); |
| 3604 | out |
| 3605 | } |
| 3606 | |
| 3607 | /// `read_palette_colors_y` (decodemv.c:478), bit_depth fixed at 8 (this |
| 3608 | /// decoder is 8-bit only): accepts up to `n` colours straight from `cache` |
| 3609 | /// (one flag bit each, in cache order), then delta-codes the remainder |
| 3610 | /// (first value raw, each following delta `>= 1` off a shrinking bit width), |
| 3611 | /// and merges the two ascending lists back together. |
| 3612 | fn read_palette_colors_y(dec: &mut SymbolDecoder, n: usize, cache: &[u16]) -> [u16; 8] { |
| 3613 | let mut cached = Vec::with_capacity(n); |
| 3614 | for &c in cache { |
| 3615 | if cached.len() >= n { |
| 3616 | break; |
| 3617 | } |
| 3618 | if dec.literal(1) == 1 { |
| 3619 | cached.push(c); |
| 3620 | } |
| 3621 | } |
| 3622 | let mut colors = [0u16; 8]; |
| 3623 | let n_cached = cached.len(); |
| 3624 | if n_cached < n { |
| 3625 | let mut transmitted = Vec::with_capacity(n - n_cached); |
| 3626 | let first = dec.literal(8) as u16; |
| 3627 | transmitted.push(first); |
| 3628 | if n_cached + 1 < n { |
| 3629 | let min_bits = 8u32 - 3; |
| 3630 | let mut bits = min_bits + dec.literal(2); |
| 3631 | let mut range = 255i32 - i32::from(first) - 1; |
| 3632 | for _ in (n_cached + 1)..n { |
| 3633 | let delta = dec.literal(bits) as i32 + 1; |
| 3634 | let prev = *transmitted.last().unwrap(); |
| 3635 | let val = (i32::from(prev) + delta).clamp(0, 255) as u16; |
| 3636 | range -= i32::from(val) - i32::from(prev); |
| 3637 | bits = bits.min(ceil_log2(range.max(0) as u32)); |
| 3638 | transmitted.push(val); |
| 3639 | } |
| 3640 | } |
| 3641 | let merged = merge_colors(&transmitted, &cached); |
| 3642 | colors[..n].copy_from_slice(&merged); |
| 3643 | } else { |
| 3644 | colors[..n].copy_from_slice(&cached[..n]); |
| 3645 | } |
| 3646 | colors |
| 3647 | } |
| 3648 | |
| 3649 | /// `read_palette_colors_uv` (decodemv.c:509), bit_depth fixed at 8: U reads |
| 3650 | /// exactly [`read_palette_colors_y`]'s cache/delta scheme (against the U-only |
| 3651 | /// cache, `range` starting at `255 - prev` with no `-1`, and each raw `delta` |
| 3652 | /// unbiased by `+1` -- both differences from Y's own reader, matching the C |
| 3653 | /// side by side). V never uses a cache: a leading bit picks either `n` raw |
| 3654 | /// literals, or a first raw value plus `n-1` signed deltas that wrap mod 256 |
| 3655 | /// rather than clamp. |
| 3656 | fn read_palette_colors_uv(dec: &mut SymbolDecoder, n: usize, cache: &[u16]) -> ([u16; 8], [u16; 8]) { |
| 3657 | let mut cached = Vec::with_capacity(n); |
| 3658 | for &c in cache { |
| 3659 | if cached.len() >= n { |
| 3660 | break; |
| 3661 | } |
| 3662 | if dec.literal(1) == 1 { |
| 3663 | cached.push(c); |
| 3664 | } |
| 3665 | } |
| 3666 | let mut u_colors = [0u16; 8]; |
| 3667 | let n_cached = cached.len(); |
| 3668 | if n_cached < n { |
| 3669 | let mut transmitted = Vec::with_capacity(n - n_cached); |
| 3670 | let first = dec.literal(8) as u16; |
| 3671 | transmitted.push(first); |
| 3672 | if n_cached + 1 < n { |
| 3673 | let min_bits = 8u32 - 3; |
| 3674 | let mut bits = min_bits + dec.literal(2); |
| 3675 | let mut range = 255i32 - i32::from(first); |
| 3676 | for _ in (n_cached + 1)..n { |
| 3677 | let delta = dec.literal(bits) as i32; |
| 3678 | let prev = *transmitted.last().unwrap(); |
| 3679 | let val = (i32::from(prev) + delta).clamp(0, 255) as u16; |
| 3680 | range -= i32::from(val) - i32::from(prev); |
| 3681 | bits = bits.min(ceil_log2(range.max(0) as u32)); |
| 3682 | transmitted.push(val); |
| 3683 | } |
| 3684 | } |
| 3685 | let merged = merge_colors(&transmitted, &cached); |
| 3686 | u_colors[..n].copy_from_slice(&merged); |
| 3687 | } else { |
| 3688 | u_colors[..n].copy_from_slice(&cached[..n]); |
| 3689 | } |
| 3690 | let mut v_colors = [0u16; 8]; |
| 3691 | if dec.literal(1) == 1 { |
| 3692 | let min_bits_v = 8u32 - 4; |
| 3693 | let bits = min_bits_v + dec.literal(2); |
| 3694 | v_colors[0] = dec.literal(8) as u16; |
| 3695 | for i in 1..n { |
| 3696 | let mut delta = dec.literal(bits) as i32; |
| 3697 | if delta != 0 && dec.literal(1) == 1 { |
| 3698 | delta = -delta; |
| 3699 | } |
| 3700 | let mut val = i32::from(v_colors[i - 1]) + delta; |
| 3701 | if val < 0 { |
| 3702 | val += 256; |
| 3703 | } |
| 3704 | if val >= 256 { |
| 3705 | val -= 256; |
| 3706 | } |
| 3707 | v_colors[i] = val as u16; |
| 3708 | } |
| 3709 | } else { |
| 3710 | for i in 0..n { |
| 3711 | v_colors[i] = dec.literal(8) as u16; |
| 3712 | } |
| 3713 | } |
| 3714 | (u_colors, v_colors) |
| 3715 | } |
| 3716 | |
| 3717 | /// `av1_palette_color_index_context_lookup` (entropymode.c:889): maps a |
| 3718 | /// `NUM_PALETTE_NEIGHBORS`-weighted score hash (`0..=8`) to the real |
| 3719 | /// `0..=4` context, `-1` for hashes no real neighbour configuration produces. |
| 3720 | const PALETTE_COLOR_INDEX_CONTEXT_LOOKUP: [i32; 9] = [-1, -1, 0, -1, -1, 4, 3, 2, 1]; |
| 3721 | |
| 3722 | /// `av1_get_palette_color_index_context` (entropymode.c:893): the colour-index |
| 3723 | /// map's own per-pixel context and colour-order permutation, from the |
| 3724 | /// left/up-left/up already-decoded neighbours (weights `[2, 1, 2]`, top-3 |
| 3725 | /// selection sort). `map`'s stride is `side` (this decoder never crops a |
| 3726 | /// palette block's map to a narrower plane edge, [`decode_color_index_map`]'s |
| 3727 | /// own doc). Returns `(ctx, color_order)`; `color_order[symbol]` is the |
| 3728 | /// actual colour index to store. |
| 3729 | fn palette_color_index_context( |
| 3730 | map: &[u8], |
| 3731 | side: usize, |
| 3732 | row: usize, |
| 3733 | col: usize, |
| 3734 | n: usize, |
| 3735 | ) -> (usize, [u8; 8]) { |
| 3736 | let left = if col >= 1 { Some(map[row * side + col - 1]) } else { None }; |
| 3737 | let up_left = if col >= 1 && row >= 1 { |
| 3738 | Some(map[(row - 1) * side + col - 1]) |
| 3739 | } else { |
| 3740 | None |
| 3741 | }; |
| 3742 | let up = if row >= 1 { Some(map[(row - 1) * side + col]) } else { None }; |
| 3743 | let mut scores = [0i32; 8]; |
| 3744 | for (neighbour, weight) in [(left, 2), (up_left, 1), (up, 2)] { |
| 3745 | if let Some(v) = neighbour { |
| 3746 | scores[v as usize] += weight; |
| 3747 | } |
| 3748 | } |
| 3749 | let mut color_order = [0u8; 8]; |
| 3750 | for i in 0..8 { |
| 3751 | color_order[i] = i as u8; |
| 3752 | } |
| 3753 | for i in 0..3 { |
| 3754 | let mut max = scores[i]; |
| 3755 | let mut max_idx = i; |
| 3756 | for j in (i + 1)..n { |
| 3757 | if scores[j] > max { |
| 3758 | max = scores[j]; |
| 3759 | max_idx = j; |
| 3760 | } |
| 3761 | } |
| 3762 | if max_idx != i { |
| 3763 | let max_score = scores[max_idx]; |
| 3764 | let max_color = color_order[max_idx]; |
| 3765 | for k in (i + 1..=max_idx).rev() { |
| 3766 | scores[k] = scores[k - 1]; |
| 3767 | color_order[k] = color_order[k - 1]; |
| 3768 | } |
| 3769 | scores[i] = max_score; |
| 3770 | color_order[i] = max_color; |
| 3771 | } |
| 3772 | } |
| 3773 | let hash = scores[0] + scores[1] * 2 + scores[2] * 2; |
| 3774 | let ctx = PALETTE_COLOR_INDEX_CONTEXT_LOOKUP[hash as usize]; |
| 3775 | (ctx as usize, color_order) |
| 3776 | } |
| 3777 | |
| 3778 | /// `decode_color_map_tokens` (detokenize.c:25): the palette-Y colour-index |
| 3779 | /// map's wavefront decode over a `side x side` grid -- this decoder's |
| 3780 | /// palette blocks are always square and never straddle the frame's true |
| 3781 | /// (possibly odd) edge without already allocating the full `side*side` |
| 3782 | /// region (every other skip/residual path here does the same), so no |
| 3783 | /// rows/cols-vs-plane_width/height cropping special case is needed, unlike |
| 3784 | /// libaom's own non-block-aligned split. |
| 3785 | fn decode_color_index_map( |
| 3786 | dec: &mut SymbolDecoder, |
| 3787 | cdfs: &mut Cdfs, |
| 3788 | n: usize, |
| 3789 | side: usize, |
| 3790 | uv: bool, |
| 3791 | ) -> Vec<u8> { |
| 3792 | let trace = std::env::var_os("EC_AV1_TRACE").is_some(); |
| 3793 | let mut map = vec![0u8; side * side]; |
| 3794 | if trace { |
| 3795 | let (rng, _) = dec.debug_state(); |
| 3796 | eprintln!("EC_PAL row=0 col=0 ctx=-1 n={n} rng={rng}"); |
| 3797 | } |
| 3798 | map[0] = read_uniform(dec, n) as u8; |
| 3799 | if trace { |
| 3800 | let (rng, _) = dec.debug_state(); |
| 3801 | eprintln!("EC_PAL_VAL row=0 col=0 color_idx={} rng={rng}", map[0]); |
| 3802 | } |
| 3803 | for i in 1..(2 * side - 1) { |
| 3804 | let j_hi = i.min(side - 1); |
| 3805 | let j_lo = i.saturating_sub(side - 1); |
| 3806 | for j in (j_lo..=j_hi).rev() { |
| 3807 | let row = i - j; |
| 3808 | let col = j; |
| 3809 | let (ctx, color_order) = palette_color_index_context(&map, side, row, col, n); |
| 3810 | if trace { |
| 3811 | let (rng, _) = dec.debug_state(); |
| 3812 | eprintln!("EC_PAL row={row} col={col} ctx={ctx} n={n} rng={rng}"); |
| 3813 | } |
| 3814 | let symbol = if uv { |
| 3815 | dec.symbol(&mut cdfs.palette_uv_color_index[n - 2][ctx][..=n]) |
| 3816 | } else { |
| 3817 | dec.symbol(&mut cdfs.palette_y_color_index[n - 2][ctx][..=n]) |
| 3818 | }; |
| 3819 | map[row * side + col] = color_order[symbol]; |
| 3820 | if trace { |
| 3821 | let (rng, _) = dec.debug_state(); |
| 3822 | eprintln!("EC_PAL_VAL row={row} col={col} color_idx={symbol} rng={rng}"); |
| 3823 | } |
| 3824 | } |
| 3825 | } |
| 3826 | map |
| 3827 | } |
| 3828 | |
| 3829 | /// [`read_intra_mode`] for a true `bw`x`bh` rect strip (lane-intradisp r1): |
| 3830 | /// identical except the `use_filter_intra` size class comes from |
| 3831 | /// [`filter_intra_size_class_rect`] instead of the square-only |
| 3832 | /// [`filter_intra_size_class`] -- every other symbol this reads (`skip`, |
| 3833 | /// `y_mode`, `angle_delta`, `uv_mode`, `cfl_alphas`) is indexed by mode or |
| 3834 | /// neighbour state, never by block size. |
| 3835 | #[allow(clippy::too_many_arguments)] |
| 3836 | fn read_intra_mode_rect( |
| 3837 | dec: &mut SymbolDecoder, |
| 3838 | cdfs: &mut Cdfs, |
| 3839 | above_mode: usize, |
| 3840 | left_mode: usize, |
| 3841 | cfl: bool, |
| 3842 | bw: usize, |
| 3843 | bh: usize, |
| 3844 | enable_filter_intra: bool, |
| 3845 | skip_ctx: usize, |
| 3846 | allow_screen_content_tools: bool, |
| 3847 | mi_r: usize, |
| 3848 | mi_c: usize, |
| 3849 | ) -> Result<( |
| 3850 | bool, |
| 3851 | usize, |
| 3852 | i32, |
| 3853 | usize, |
| 3854 | i32, |
| 3855 | Option<(i32, i32)>, |
| 3856 | Option<usize>, |
| 3857 | )> { |
| 3858 | // lane-screen consumes palette/intrabc syntax in the SQUARE reader only |
| 3859 | // (`palette_bsize_ctx` is keyed on a single side). A rect strip in a |
| 3860 | // screen-content frame would therefore skip symbols the encoder wrote and |
| 3861 | // desync the tile, so refuse it by name rather than decode it wrong. |
| 3862 | if allow_screen_content_tools { |
| 3863 | return Err(unsupported( |
| 3864 | "a HORZ/VERT intra strip in a screen-content frame (palette syntax \ |
| 3865 | is consumed for square blocks only)", |
| 3866 | )); |
| 3867 | } |
| 3868 | let ec_istep = std::env::var_os("EC_TRACE_MODE_STEP").is_some(); |
| 3869 | macro_rules! istep { |
| 3870 | ($name:literal, $val:expr) => { |
| 3871 | if ec_istep { |
| 3872 | let (rng, _) = dec.debug_state(); |
| 3873 | eprintln!( |
| 3874 | "EC_ISTEP mi_row={mi_r} mi_col={mi_c} name={} val={} rng={rng}", |
| 3875 | $name, $val |
| 3876 | ); |
| 3877 | } |
| 3878 | }; |
| 3879 | } |
| 3880 | // Same `intra_segment_id` placement as the square reader above. |
| 3881 | let (seg_w_mi, seg_h_mi) = (bw / 4, bh / 4); |
| 3882 | if seg_id_pre_skip() { |
| 3883 | intra_segment_id(dec, cdfs, mi_r, mi_c, seg_w_mi, seg_h_mi, false); |
| 3884 | } |
| 3885 | let skip = dec.symbol(&mut cdfs.skip[skip_ctx]) != 0; |
| 3886 | istep!("skip", skip as i32); |
| 3887 | if !seg_id_pre_skip() { |
| 3888 | intra_segment_id(dec, cdfs, mi_r, mi_c, seg_w_mi, seg_h_mi, skip); |
| 3889 | } |
| 3890 | maybe_read_cdef_idx(dec, mi_r, mi_c, skip); |
| 3891 | istep!("cdef", 0); |
| 3892 | // A HORZ/VERT rect strip is never the whole superblock (`bw`/`bh` never |
| 3893 | // both 64 here -- see this fn's own doc), so `is_whole_sb` is always |
| 3894 | // `false`. |
| 3895 | maybe_read_delta_q(dec, cdfs, mi_r, mi_c, false, skip); |
| 3896 | maybe_read_delta_lf(dec, cdfs, mi_r, mi_c, false, skip); |
| 3897 | istep!("dq", 0); |
| 3898 | let above_ctx = INTRA_MODE_CTX[above_mode]; |
| 3899 | let left_ctx = INTRA_MODE_CTX[left_mode]; |
| 3900 | let mode = dec.symbol(&mut cdfs.kf_y_mode[above_ctx][left_ctx]); |
| 3901 | istep!("mode", mode as i32); |
| 3902 | let angle_delta_y = if (V_PRED..=D67_PRED).contains(&mode) { |
| 3903 | read_angle_delta(dec, &mut cdfs.angle_delta[mode - V_PRED]) |
| 3904 | } else { |
| 3905 | 0 |
| 3906 | }; |
| 3907 | istep!("angle_y", angle_delta_y); |
| 3908 | let uv_mode = if cfl { |
| 3909 | dec.symbol(&mut cdfs.uv_mode_cfl[mode]) |
| 3910 | } else { |
| 3911 | dec.symbol(&mut cdfs.uv_mode_no_cfl[mode]) |
| 3912 | }; |
| 3913 | istep!("uv_mode", uv_mode as i32); |
| 3914 | let alpha = if cfl && uv_mode == UV_CFL_PRED { |
| 3915 | Some(read_cfl_alphas(dec, cdfs)) |
| 3916 | } else { |
| 3917 | None |
| 3918 | }; |
| 3919 | if (9..=12).contains(&uv_mode) { |
| 3920 | SMOOTH_UV_HITS.with(|c| c.set(c.get() + 1)); |
| 3921 | } |
| 3922 | let angle_delta_uv = if (V_PRED..=D67_PRED).contains(&uv_mode) { |
| 3923 | DIRECTIONAL_UV_HITS.with(|c| c.set(c.get() + 1)); |
| 3924 | read_angle_delta(dec, &mut cdfs.angle_delta[uv_mode - V_PRED]) |
| 3925 | } else { |
| 3926 | 0 |
| 3927 | }; |
| 3928 | istep!("angle_uv", angle_delta_uv); |
| 3929 | let mut filter_intra = None; |
| 3930 | if std::env::var_os("EC_AV1_TRACE").is_some() { |
| 3931 | let (rng, _) = dec.debug_state(); |
| 3932 | eprintln!( |
| 3933 | "TRACE_RECT_PREFI mode={mode} uv_mode={uv_mode} enable_filter_intra={} \ |
| 3934 | class={:?} rng={rng}", |
| 3935 | enable_filter_intra as i32, |
| 3936 | filter_intra_size_class_rect(bw, bh) |
| 3937 | ); |
| 3938 | } |
| 3939 | if mode == DC_PRED |
| 3940 | && enable_filter_intra |
| 3941 | && let Some(class) = filter_intra_size_class_rect(bw, bh) |
| 3942 | { |
| 3943 | let use_filter_intra = dec.symbol(&mut cdfs.filter_intra[class]) != 0; |
| 3944 | if std::env::var_os("EC_AV1_TRACE").is_some() { |
| 3945 | eprintln!("TRACE_RECT_USEFI value={}", use_filter_intra as i32); |
| 3946 | } |
| 3947 | if use_filter_intra { |
| 3948 | FILTER_INTRA_HITS.with(|c| c.set(c.get() + 1)); |
| 3949 | let fi_mode = dec.symbol(&mut cdfs.filter_intra_mode); |
| 3950 | filter_intra = Some(fi_mode); |
| 3951 | } |
| 3952 | } |
| 3953 | Ok(( |
| 3954 | skip, |
| 3955 | mode, |
| 3956 | angle_delta_y, |
| 3957 | uv_mode, |
| 3958 | angle_delta_uv, |
| 3959 | alpha, |
| 3960 | filter_intra, |
| 3961 | )) |
| 3962 | } |
| 3963 | |
| 3964 | /// [`tx_size_context`] for a true `bw`x`bh` rect strip (lane-intradisp r1): |
| 3965 | /// `get_tx_size_context` (libaom `pred_common.h`) compares the neighbour |
| 3966 | /// above against this block's own *width* and the neighbour to the left |
| 3967 | /// against its own *height* -- for a square block those are the same value, |
| 3968 | /// which is why the square-only version can get away with one `own_side`. |
| 3969 | fn tx_size_context_rect( |
| 3970 | n: &Neighbours, |
| 3971 | (mi_r, mi_c): (usize, usize), |
| 3972 | own_w: usize, |
| 3973 | own_h: usize, |
| 3974 | ) -> usize { |
| 3975 | let above = mi_r > n.tile_row0_mi && tx_px_at(n, false, mi_r - 1, mi_c) as usize >= own_w; |
| 3976 | let left = mi_c > n.tile_col0_mi && tx_h_px_at(n, false, mi_r, mi_c - 1) as usize >= own_h; |
| 3977 | usize::from(above) + usize::from(left) |
| 3978 | } |
| 3979 | |
| 3980 | /// [`cfl_ac_q3`] for a true `bw`x`bh` rect strip (lane-intradisp r1). |
| 3981 | fn cfl_ac_q3_rect(y: &PlaneBuf, px: usize, py: usize, bw: usize, bh: usize) -> Vec<i32> { |
| 3982 | let (cw, ch) = (bw / 2, bh / 2); |
| 3983 | let mut ac = vec![0i32; cw * ch]; |
| 3984 | let mut sum = 0i32; |
| 3985 | for cy in 0..ch { |
| 3986 | for cx in 0..cw { |
| 3987 | let (lx, ly) = (px + cx * 2, py + cy * 2); |
| 3988 | let q3 = (i32::from(y.data[ly * y.width + lx]) |
| 3989 | + i32::from(y.data[ly * y.width + lx + 1]) |
| 3990 | + i32::from(y.data[(ly + 1) * y.width + lx]) |
| 3991 | + i32::from(y.data[(ly + 1) * y.width + lx + 1])) |
| 3992 | << 1; |
| 3993 | ac[cy * cw + cx] = q3; |
| 3994 | sum += q3; |
| 3995 | } |
| 3996 | } |
| 3997 | let num_pel = (cw * ch) as i32; |
| 3998 | let avg = (sum + num_pel / 2) >> num_pel.trailing_zeros(); |
| 3999 | ac.iter_mut().for_each(|v| *v -= avg); |
| 4000 | ac |
| 4001 | } |
| 4002 | |
| 4003 | /// Everything a rect strip already read out of the stream before its luma |
| 4004 | /// transform units are decoded -- [`decode_rect_split`]'s own mode bundle |
| 4005 | /// (nine values that would otherwise be nine more parameters). |
| 4006 | struct RectStripModes { |
| 4007 | skip: bool, |
| 4008 | mode: usize, |
| 4009 | angle_delta_y: i32, |
| 4010 | uv_predict_mode: usize, |
| 4011 | angle_delta_uv: i32, |
| 4012 | alpha: Option<(i32, i32)>, |
| 4013 | filter_intra: Option<usize>, |
| 4014 | smooth_neighbor: bool, |
| 4015 | smooth_neighbor_uv: bool, |
| 4016 | } |
| 4017 | |
| 4018 | /// Decodes the planes of one `bw`x`bh` HORZ/VERT intra strip whose luma |
| 4019 | /// transform is SPLIT (`tx_depth != 0`) -- lane-rectsplit r1, the case every |
| 4020 | /// rect path here refused by name until now ("per-unit rect prediction is not |
| 4021 | /// ported"). |
| 4022 | /// |
| 4023 | /// `depth_to_tx_size` (libaom `blockd.h`) walks `sub_tx_size_map` `depth` |
| 4024 | /// times from the block's own `max_txsize_rect_lookup` entry, and for a 2:1 |
| 4025 | /// strip the very first step already lands on a SQUARE transform |
| 4026 | /// (`sub_tx_size_map[TX_32X16] == TX_16X16`, `[TX_64X32] == TX_32X32`, and |
| 4027 | /// every further step halves a square) -- so each unit is square and reads |
| 4028 | /// through the ordinary square [`read_plane`]. What is genuinely rectangular |
| 4029 | /// here is the *tiling* (`bw/tx` by `bh/tx` units, not `n` by `n` as |
| 4030 | /// [`decode_block`]'s square split loop assumes) and the chroma plane, whose |
| 4031 | /// own transform is `av1_get_max_uv_txsize` of the whole strip and is never |
| 4032 | /// split by luma's depth. |
| 4033 | /// |
| 4034 | /// Each unit predicts off whatever the earlier units of this same block |
| 4035 | /// already reconstructed (spec 5.11.36 `predict_and_reconstruct_intra_block` |
| 4036 | /// inside `av1_foreach_transformed_block`, raster order), which is exactly |
| 4037 | /// what predicting the whole strip in one shot got wrong. |
| 4038 | #[allow(clippy::too_many_arguments)] |
| 4039 | fn decode_rect_split( |
| 4040 | dec: &mut SymbolDecoder, |
| 4041 | cdfs: &mut Cdfs, |
| 4042 | neighbours: &mut Neighbours, |
| 4043 | at: (usize, usize), |
| 4044 | bw: usize, |
| 4045 | bh: usize, |
| 4046 | tx: usize, |
| 4047 | m: &RectStripModes, |
| 4048 | y: &mut PlaneBuf, |
| 4049 | u: &mut PlaneBuf, |
| 4050 | v: &mut PlaneBuf, |
| 4051 | base_q_idx: u8, |
| 4052 | reduced_tx_set: bool, |
| 4053 | ) -> Result<()> { |
| 4054 | let (r, c) = at; |
| 4055 | let (px, py) = (c * SUB, r * SUB); |
| 4056 | let (mi_r, mi_c) = (r * (SUB / MI), c * (SUB / MI)); |
| 4057 | let (cpx, cpy) = (px / 2, py / 2); |
| 4058 | let (chroma_w, chroma_h) = (bw / 2, bh / 2); |
| 4059 | // The chroma transform is picked before any symbol is read, so an |
| 4060 | // unsupported chroma shape refuses before this strip desyncs the tile. |
| 4061 | let chroma: Option<(TxbSet, &[u16])> = match (chroma_w, chroma_h) { |
| 4062 | (16, 8) => Some((TxbSet::ChromaRect16x8, &SCAN_16X8[..])), |
| 4063 | (8, 16) => Some((TxbSet::ChromaRect16x8, &SCAN_8X16[..])), |
| 4064 | (32, 16) => Some((TxbSet::ChromaRect32x16, &SCAN_32X16[..])), |
| 4065 | (16, 32) => Some((TxbSet::ChromaRect32x16, &SCAN_16X32[..])), |
| 4066 | _ => None, |
| 4067 | }; |
| 4068 | // lane-rectsplit r2 (verifier finding): with both callers wired |
| 4069 | // (`decode_block_rect`'s 32x16/16x32 and `decode_block_rect64`'s |
| 4070 | // 64x32/32x64) every shape that reaches here has a table above, so this |
| 4071 | // arm is UNREACHABLE today. It stays as the shape guard for the next |
| 4072 | // caller -- a new strip size would otherwise hit the `expect` below and |
| 4073 | // panic instead of refusing by name -- and so stays in |
| 4074 | // `refusal_inventory::REFUSALS`. |
| 4075 | if !m.skip && chroma.is_none() { |
| 4076 | return Err(unsupported( |
| 4077 | "a coded HORZ/VERT strip whose chroma transform has no rect coefficient tables here", |
| 4078 | )); |
| 4079 | } |
| 4080 | RECT_SPLIT_TX_HITS.with(|c| c.set(c.get() + 1)); |
| 4081 | if bw.max(bh) == 64 && bw / tx > 1 && bh / tx > 1 { |
| 4082 | RECT_SPLIT_SB_INTERIOR_TU_HITS.with(|c| c.set(c.get() + 1)); |
| 4083 | } |
| 4084 | // LUMA: `bw/tx` x `bh/tx` square transform units, raster order. |
| 4085 | let luma_set = txbset_for(tx, reduced_tx_set); |
| 4086 | let luma_scan = default_scan(tx); |
| 4087 | let zero_tu = vec![0i32; tx * tx]; |
| 4088 | // The strip's OWN edge availability, the fallback for a transform unit |
| 4089 | // whose top-right/bottom-left pixels fall outside the strip. |
| 4090 | let block_reach = Reach::of_rect(bw, bh, px, py, y.width, y.height); |
| 4091 | for tu_row in 0..bh / tx { |
| 4092 | for tu_col in 0..bw / tx { |
| 4093 | let tu_mi = (mi_r + tu_row * (tx / MI), mi_c + tu_col * (tx / MI)); |
| 4094 | let (tu_px, tu_py) = (px + tu_col * tx, py + tu_row * tx); |
| 4095 | let (col_off, row_off) = (tu_col * tx, tu_row * tx); |
| 4096 | // `has_top_right`/`has_bottom_left` (libaom `reconintra.c`) for a |
| 4097 | // transform unit INSIDE a block, which is a different rule from |
| 4098 | // the standalone-block table [`Reach::of`] applies -- and the |
| 4099 | // reason a depth-2 superblock strip came out one sample off |
| 4100 | // before this: `Reach::of(tx, ..)` answered as if the unit were |
| 4101 | // its own block at that superblock position. |
| 4102 | // * top-right: if the unit's right neighbour is still inside the |
| 4103 | // strip the pixels are already reconstructed (`col_off + |
| 4104 | // tx_wide < plane_bw`); otherwise only the strip's own top row |
| 4105 | // of units can reach past it, through the block-level answer. |
| 4106 | // * bottom-left: a unit that is not in the strip's left column |
| 4107 | // has none (`col_off > 0` returns 0); inside the strip |
| 4108 | // (`row_off + tx_high < plane_bh`) they are the left |
| 4109 | // neighbour's, already reconstructed; the last row falls back |
| 4110 | // to the block-level answer. |
| 4111 | let tu_reach = Reach { |
| 4112 | above_right: if col_off + tx < bw { |
| 4113 | true |
| 4114 | } else { |
| 4115 | row_off == 0 && block_reach.above_right |
| 4116 | }, |
| 4117 | below_left: if col_off > 0 { |
| 4118 | false |
| 4119 | } else if row_off + tx < bh { |
| 4120 | true |
| 4121 | } else { |
| 4122 | block_reach.below_left |
| 4123 | }, |
| 4124 | }; |
| 4125 | if m.skip { |
| 4126 | y.reconstruct( |
| 4127 | tu_px, |
| 4128 | tu_py, |
| 4129 | tx, |
| 4130 | m.mode, |
| 4131 | m.angle_delta_y, |
| 4132 | tu_reach, |
| 4133 | &zero_tu, |
| 4134 | None, |
| 4135 | m.filter_intra, |
| 4136 | m.smooth_neighbor, |
| 4137 | ); |
| 4138 | neighbours.record_mi_luma(tu_mi, tx, &zero_tu); |
| 4139 | } else { |
| 4140 | let tu_around = neighbours.around_mi(tu_mi, tx)[0]; |
| 4141 | // This unit is smaller than the block it sits in, so |
| 4142 | // `txb_skip_ctx` is the neighbour-magnitude table, not the |
| 4143 | // lone-TU 0 (spec `get_txb_ctx_general`). |
| 4144 | let tu_skip_ctx = neighbours.luma_skip_ctx(tu_mi, tx / MI); |
| 4145 | let tu_grid = read_plane( |
| 4146 | dec, |
| 4147 | cdfs, |
| 4148 | luma_set, |
| 4149 | &luma_scan, |
| 4150 | 0, |
| 4151 | tu_around, |
| 4152 | m.mode, |
| 4153 | m.mode, |
| 4154 | m.angle_delta_y, |
| 4155 | tu_reach, |
| 4156 | y, |
| 4157 | tu_px, |
| 4158 | tu_py, |
| 4159 | tx, |
| 4160 | tx, |
| 4161 | base_q_idx, |
| 4162 | None, |
| 4163 | m.filter_intra, |
| 4164 | Some(tu_skip_ctx), |
| 4165 | m.smooth_neighbor, |
| 4166 | )?; |
| 4167 | neighbours.record_mi_luma(tu_mi, tx, &tu_grid); |
| 4168 | } |
| 4169 | } |
| 4170 | } |
| 4171 | // CHROMA: one un-split rect transform for the whole strip. |
| 4172 | let ac = m.alpha.map(|_| cfl_ac_q3_rect(y, px, py, bw, bh)); |
| 4173 | let reach = block_reach; |
| 4174 | let (u_grid, v_grid) = if m.skip { |
| 4175 | let zero = vec![0i32; chroma_w * chroma_h]; |
| 4176 | u.reconstruct_rect( |
| 4177 | cpx, cpy, chroma_w, chroma_h, m.uv_predict_mode, m.angle_delta_uv, reach, &zero, |
| 4178 | m.alpha.zip(ac.as_deref()).map(|((au, _), ac)| (au, ac)), None, m.smooth_neighbor_uv, |
| 4179 | ); |
| 4180 | v.reconstruct_rect( |
| 4181 | cpx, cpy, chroma_w, chroma_h, m.uv_predict_mode, m.angle_delta_uv, reach, &zero, |
| 4182 | m.alpha.zip(ac.as_deref()).map(|((_, av), ac)| (av, ac)), None, m.smooth_neighbor_uv, |
| 4183 | ); |
| 4184 | (zero.clone(), zero) |
| 4185 | } else { |
| 4186 | let (chroma_set, chroma_scan) = chroma.expect("refused above when None"); |
| 4187 | // `av1_get_ext_tx_set_type`: a chroma transform whose square-up size |
| 4188 | // is TX_32X32 or bigger resolves to `EXT_TX_SET_DCTONLY` for intra, |
| 4189 | // so only the 16x8/8x16 pair takes the mode-indexed default. |
| 4190 | let default_tx = if chroma_w.max(chroma_h) >= 32 { |
| 4191 | TxType::DctDct |
| 4192 | } else { |
| 4193 | default_intra_tx_type(m.uv_predict_mode as u8) |
| 4194 | }; |
| 4195 | let around = neighbours.around_rect(at, bw, bh); |
| 4196 | let mut grids: Vec<Vec<i32>> = Vec::with_capacity(2); |
| 4197 | for plane_idx in 1..=2 { |
| 4198 | let skip_ctx = |
| 4199 | usize::from(around[plane_idx].0) + usize::from(around[plane_idx].1); |
| 4200 | let mut coding = cdfs.txb(chroma_set, m.uv_predict_mode); |
| 4201 | let (levels, tx_type) = read_coeffs_rect( |
| 4202 | dec, |
| 4203 | &mut coding, |
| 4204 | chroma_scan, |
| 4205 | chroma_w, |
| 4206 | chroma_h, |
| 4207 | skip_ctx, |
| 4208 | dc_sign_ctx(around[plane_idx].2), |
| 4209 | default_tx, |
| 4210 | )?; |
| 4211 | let residual = dequant_and_inverse_typed_wh( |
| 4212 | &levels, |
| 4213 | chroma_w, |
| 4214 | chroma_h, |
| 4215 | crate::decode::bit_depth(), |
| 4216 | CURRENT_Q_IDX.with(|c| c.get()), |
| 4217 | plane_q_delta(plane_idx).0, |
| 4218 | plane_q_delta(plane_idx).1, |
| 4219 | tx_type, |
| 4220 | ); |
| 4221 | let cfl = m |
| 4222 | .alpha |
| 4223 | .zip(ac.as_deref()) |
| 4224 | .map(|((au, av), ac)| (if plane_idx == 1 { au } else { av }, ac)); |
| 4225 | let plane = if plane_idx == 1 { &mut *u } else { &mut *v }; |
| 4226 | plane.reconstruct_rect( |
| 4227 | cpx, |
| 4228 | cpy, |
| 4229 | chroma_w, |
| 4230 | chroma_h, |
| 4231 | m.uv_predict_mode, |
| 4232 | m.angle_delta_uv, |
| 4233 | reach, |
| 4234 | &residual, |
| 4235 | cfl, |
| 4236 | None, |
| 4237 | m.smooth_neighbor_uv, |
| 4238 | ); |
| 4239 | grids.push(levels); |
| 4240 | } |
| 4241 | let mut it = grids.into_iter(); |
| 4242 | (it.next().unwrap(), it.next().unwrap()) |
| 4243 | }; |
| 4244 | neighbours.record_split_luma_rect(at, bw, bh, m.mode, m.uv_predict_mode, [&u_grid, &v_grid]); |
| 4245 | neighbours.fill_skip_grid_rect((mi_r, mi_c), bw / MI, bh / MI, m.skip); |
| 4246 | neighbours.fill_lf_grid_rect((mi_r, mi_c), bw / MI, bh / MI, tx as u8, tx as u8, 0); |
| 4247 | if std::env::var_os("EC_AV1_TRACE").is_some() { |
| 4248 | let (rng, _) = dec.debug_state(); |
| 4249 | eprintln!( |
| 4250 | "TRACE_RECT_SPLIT mi_row={mi_r} mi_col={mi_c} bw={bw} bh={bh} tx={tx} rng={rng}" |
| 4251 | ); |
| 4252 | } |
| 4253 | Ok(()) |
| 4254 | } |
| 4255 | |
| 4256 | /// Decodes one true `bw`x`bh` `PARTITION_HORZ`/`PARTITION_VERT` intra strip |
| 4257 | /// (lane-intradisp r1, spec `decode_block` restricted to a 32x32 quadrant's |
| 4258 | /// two rect children). Only the `skip` case is supported: `skip`/`y_mode`/ |
| 4259 | /// `uv_mode`/`filter_intra`/`tx_depth` are read exactly per spec either way |
| 4260 | /// (so a skip strip elsewhere in the tile stays in sync), but this decoder |
| 4261 | /// has no rectangular-transform coefficient reader, and a chroma plane here |
| 4262 | /// is genuinely `TX_16X8`/`TX_8X16` (`av1_get_max_uv_txsize`, spec 5.11.16) |
| 4263 | /// with no depth-based square-tiling escape the way luma's own resolved |
| 4264 | /// `tx_depth` has (`sub_tx_size_map[TX_32X16]` == `TX_16X16`, square) -- |
| 4265 | /// so a coded (non-skip) strip is refused by name instead of guess-decoded. |
| 4266 | #[allow(clippy::too_many_arguments)] |
| 4267 | fn decode_block_rect( |
| 4268 | dec: &mut SymbolDecoder, |
| 4269 | cdfs: &mut Cdfs, |
| 4270 | neighbours: &mut Neighbours, |
| 4271 | at: (usize, usize), |
| 4272 | bw: usize, |
| 4273 | bh: usize, |
| 4274 | y: &mut PlaneBuf, |
| 4275 | u: &mut PlaneBuf, |
| 4276 | v: &mut PlaneBuf, |
| 4277 | enable_filter_intra: bool, |
| 4278 | allow_screen_content_tools: bool, |
| 4279 | base_q_idx: u8, |
| 4280 | tx_select: bool, |
| 4281 | reduced_tx_set: bool, |
| 4282 | ) -> Result<()> { |
| 4283 | let (r, c) = at; |
| 4284 | let (px, py) = (c * SUB, r * SUB); |
| 4285 | let (skip, mode, angle_delta_y, uv_mode, angle_delta_uv, alpha, filter_intra) = |
| 4286 | read_intra_mode_rect( |
| 4287 | dec, |
| 4288 | cdfs, |
| 4289 | neighbours.above_mode[c], |
| 4290 | neighbours.left_mode[r], |
| 4291 | true, |
| 4292 | bw, |
| 4293 | bh, |
| 4294 | enable_filter_intra, |
| 4295 | neighbours.skip_txfm_ctx(r * (SUB / MI), c * (SUB / MI)), |
| 4296 | allow_screen_content_tools, |
| 4297 | r * (SUB / MI), |
| 4298 | c * (SUB / MI), |
| 4299 | )?; |
| 4300 | let smooth_neighbor = |
| 4301 | is_smooth_mode(neighbours.above_mode[c]) || is_smooth_mode(neighbours.left_mode[r]); |
| 4302 | if smooth_neighbor { |
| 4303 | SMOOTH_LUMA_HITS.with(|c| c.set(c.get() + 1)); |
| 4304 | } |
| 4305 | // lane-chroma r3: chroma's own edge-filter-strength neighbour check |
| 4306 | // (spec `get_intra_edge_filter_type`) reads the CHROMA neighbour's |
| 4307 | // `uv_mode`, not the luma one. |
| 4308 | let smooth_neighbor_uv = |
| 4309 | is_smooth_mode(neighbours.above_uv_mode[c]) || is_smooth_mode(neighbours.left_uv_mode[r]); |
| 4310 | // lane-rectsplit r1: `predict_filter_intra` now takes the block's own |
| 4311 | // `bw`x`bh` (`av1_filter_intra_predictor_c` walks its 4x2 patches over a |
| 4312 | // rectangle just as happily), so a `use_filter_intra` strip at this level |
| 4313 | // predicts instead of refusing. The below-16x16 leaf arm |
| 4314 | // (`decode_leaf_rect`) keeps the refusal: it has no gate of its own. |
| 4315 | if filter_intra.is_some() { |
| 4316 | FILTER_INTRA_RECT_HITS.with(|c| c.set(c.get() + 1)); |
| 4317 | } |
| 4318 | let uv_predict_mode = if uv_mode == UV_CFL_PRED { |
| 4319 | DC_PRED |
| 4320 | } else { |
| 4321 | uv_mode |
| 4322 | }; |
| 4323 | let (mi_r, mi_c) = (r * (SUB / MI), c * (SUB / MI)); |
| 4324 | // The tx-depth symbol EXISTS only under TX_MODE_SELECT. Both square paths |
| 4325 | // gate their `read_tx_size` on `tx_select` for exactly this reason; this |
| 4326 | // one did not, so with `--enable-tx-size-search=0` -- which several gate |
| 4327 | // recipes use -- the decoder consumed a symbol the encoder never wrote and |
| 4328 | // desynced the tile from that point on. That is why both strips of the |
| 4329 | // pinned HORZ quadrant were wrong from their very first pixel, and why our |
| 4330 | // range after the read equalled the oracle's range for the whole block. |
| 4331 | let depth = if tx_select { |
| 4332 | let ctx = tx_size_context_rect(neighbours, (mi_r, mi_c), bw, bh); |
| 4333 | dec.symbol(&mut cdfs.tx_size_cat2[ctx]) |
| 4334 | } else { |
| 4335 | 0 |
| 4336 | }; |
| 4337 | if depth != 0 { |
| 4338 | TX_DEPTH_HITS.with(|c| c.set(c.get() + 1)); |
| 4339 | // lane-rectsplit r1: a split transform is predicted and reconstructed |
| 4340 | // per transform unit, each unit taking its edges from the previous |
| 4341 | // unit's reconstruction inside this same strip (spec 5.11.36) -- |
| 4342 | // [`decode_rect_split`]. `depth_to_tx_size` of a 2:1 strip is square |
| 4343 | // from the first step on, so `bw.min(bh) >> (depth - 1)` names it. |
| 4344 | let tx = bw.min(bh) >> (depth - 1); |
| 4345 | if std::env::var_os("EC_SBPART_DUMP64").is_some() { |
| 4346 | eprintln!( |
| 4347 | "DUMP64SPLIT mi_r={mi_r} mi_c={mi_c} px={px} py={py} bw={bw} bh={bh} \ |
| 4348 | depth={depth} tx={tx} mode={mode} uv={uv_predict_mode} skip={skip} \ |
| 4349 | angle_y={angle_delta_y}" |
| 4350 | ); |
| 4351 | } |
| 4352 | let modes = RectStripModes { |
| 4353 | skip, |
| 4354 | mode, |
| 4355 | angle_delta_y, |
| 4356 | uv_predict_mode, |
| 4357 | angle_delta_uv, |
| 4358 | alpha, |
| 4359 | filter_intra, |
| 4360 | smooth_neighbor, |
| 4361 | smooth_neighbor_uv, |
| 4362 | }; |
| 4363 | decode_rect_split( |
| 4364 | dec, cdfs, neighbours, at, bw, bh, tx, &modes, y, u, v, base_q_idx, reduced_tx_set, |
| 4365 | )?; |
| 4366 | RECT_PARTITION_HITS.with(|c| c.set(c.get() + 1)); |
| 4367 | return Ok(()); |
| 4368 | } |
| 4369 | let (tx_w, tx_h) = (bw, bh); |
| 4370 | let (cpx, cpy) = (px / 2, py / 2); |
| 4371 | let (chroma_w, chroma_h) = (bw / 2, bh / 2); |
| 4372 | let reach = Reach::of_rect(bw, bh, px, py, y.width, y.height); |
| 4373 | if std::env::var_os("EC_AV1_TRACE").is_some() { |
| 4374 | let (rng, _) = dec.debug_state(); |
| 4375 | eprintln!( |
| 4376 | "TRACE_RECT_IMODE mi_row={mi_r} mi_col={mi_c} mode={mode} uv_mode={uv_mode} \ |
| 4377 | skip={} tx={depth} rng={rng}", |
| 4378 | skip as i32 |
| 4379 | ); |
| 4380 | } |
| 4381 | if skip { |
| 4382 | y.reconstruct_rect( |
| 4383 | px, |
| 4384 | py, |
| 4385 | bw, |
| 4386 | bh, |
| 4387 | mode, |
| 4388 | angle_delta_y, |
| 4389 | reach, |
| 4390 | &vec![0i32; bw * bh], |
| 4391 | None, |
| 4392 | filter_intra, |
| 4393 | smooth_neighbor, |
| 4394 | ); |
| 4395 | let ac = alpha.map(|_| cfl_ac_q3_rect(y, px, py, bw, bh)); |
| 4396 | u.reconstruct_rect( |
| 4397 | cpx, |
| 4398 | cpy, |
| 4399 | chroma_w, |
| 4400 | chroma_h, |
| 4401 | uv_predict_mode, |
| 4402 | angle_delta_uv, |
| 4403 | reach, |
| 4404 | &vec![0i32; chroma_w * chroma_h], |
| 4405 | alpha.zip(ac.as_deref()).map(|((au, _), ac)| (au, ac)), |
| 4406 | None, |
| 4407 | smooth_neighbor_uv, |
| 4408 | ); |
| 4409 | v.reconstruct_rect( |
| 4410 | cpx, |
| 4411 | cpy, |
| 4412 | chroma_w, |
| 4413 | chroma_h, |
| 4414 | uv_predict_mode, |
| 4415 | angle_delta_uv, |
| 4416 | reach, |
| 4417 | &vec![0i32; chroma_w * chroma_h], |
| 4418 | alpha.zip(ac.as_deref()).map(|((_, av), ac)| (av, ac)), |
| 4419 | None, |
| 4420 | smooth_neighbor_uv, |
| 4421 | ); |
| 4422 | neighbours.record_rect( |
| 4423 | at, |
| 4424 | bw, |
| 4425 | bh, |
| 4426 | mode, |
| 4427 | uv_predict_mode, |
| 4428 | &[ |
| 4429 | vec![0i32; bw * bh], |
| 4430 | vec![0i32; chroma_w * chroma_h], |
| 4431 | vec![0i32; chroma_w * chroma_h], |
| 4432 | ], |
| 4433 | ); |
| 4434 | } else if (bw, bh) != (32, 16) && (bw, bh) != (16, 32) { |
| 4435 | // lane-rect16 r1: a real (non-skip) coefficient read at this size |
| 4436 | // (8x16/16x32 VERT_B's own left strip) is not ported -- it needs its |
| 4437 | // own `TxbSet`/`eob_pt` tables (a true 128-position luma group has no |
| 4438 | // `EOB_PT_128_LUMA` in this decoder yet, only the *chroma* one |
| 4439 | // `lane-rectwire` built for `ChromaRect16x8`) and, unlike the 32x32 |
| 4440 | // sqr-up case `LumaRect32x16` safely skips, `get_ext_tx_set_type` |
| 4441 | // does NOT return `EXT_TX_SET_DCTONLY` at a 16x16 sqr-up under this |
| 4442 | // encoder's reduced_tx_set -- a real `tx_type` symbol may be coded |
| 4443 | // that no table here reads. Refused by name rather than guess-decoded |
| 4444 | // or silently desynced. |
| 4445 | return Err(unsupported( |
| 4446 | "a coded (non-skip) HORZ_B/VERT_B rect strip below 16x16 (this decoder ports only \ |
| 4447 | the skip case at this size)", |
| 4448 | )); |
| 4449 | } else { |
| 4450 | // lane-rectwire r2: real coefficients. `get_txsize_entropy_ctx` |
| 4451 | // reduces both size pairs to their square-up CDF sets |
| 4452 | // (`TxbSet::LumaRect32x16`/`ChromaRect16x8`, see those variants' own |
| 4453 | // doc comments); the scan order and 2D context tables are the real |
| 4454 | // rect ones (`SCAN_32X16`/etc, `base_ctx_rect`/`br_ctx_rect`). |
| 4455 | let (luma_scan, chroma_scan): (&[u16], &[u16]) = if bw == 32 { |
| 4456 | (&SCAN_32X16, &SCAN_16X8) |
| 4457 | } else { |
| 4458 | (&SCAN_16X32, &SCAN_8X16) |
| 4459 | }; |
| 4460 | // lane-rectwire r3: real coefficients desync a pixel-exact gate |
| 4461 | // (rectwire-flake-1.obu, seed 55) -- both 32x16 strips of frame 0's |
| 4462 | // mi(8,8) HORZ quadrant come out ~100% wrong from their very first |
| 4463 | // pixel (not a subtle context-table off-by-one), while the sibling |
| 4464 | // non-rect quadrants of the same frame match. Range-ladder bisection |
| 4465 | // against a real aomdec (EC_TRACE_COEFF/EC_TRACE_MODE) was started |
| 4466 | // but the intra key-frame path has no traced symbol between the |
| 4467 | // partition read and the first coefficient block (mode/skip/tx_depth |
| 4468 | // reads are all untraced in libaom's decodeframe.c intra path, unlike |
| 4469 | // the inter EC_TRACE_MODE this round added), so a same-length range |
| 4470 | // comparison wasn't reachable within budget -- see |
| 4471 | // lanes/rectwire-r3.report.md for what was ruled out (base_ctx_rect's |
| 4472 | // 5-neighbour offsets, u/v_skip_ctx's above+left formula, and |
| 4473 | // dc_sign_ctx's around-index all cross-checked byte-for-byte against |
| 4474 | // libaom and match). lane-rectwire r4: range-ladder against the |
| 4475 | // now-instrumented oracle (EC_TRACE_MODE's intra EC_IMODE/EC_IMODE_VAL) |
| 4476 | // narrowed the divergence past skip/y_mode/uv_mode (values AND range |
| 4477 | // both confirmed in sync -- rng=58692 on both sides right after |
| 4478 | // uv_mode, with `enable_filter_intra=0` on this frame so neither side |
| 4479 | // reads a filter_intra symbol) to the `tx_size_cat2` depth read |
| 4480 | // immediately after: ours moves rng 58692 -> 43570, which cannot be |
| 4481 | // reconciled with oracle's own EC_IMODE_VAL (rng=58692, taken *after* |
| 4482 | // its own tx_size read) unless oracle's tx symbol is a near-certainty |
| 4483 | // CDF that barely narrows the range -- i.e. this decoder's |
| 4484 | // `tx_size_context_rect` ctx or `cdfs.tx_size_cat2` CDF selection for |
| 4485 | // a 32x16/16x32 strip is the first diverging symbol, not the |
| 4486 | // coefficient-context math r3 already cleared. Not confirmed byte- |
| 4487 | // exact (would need a decodemv.c EC_TRACE_MODE patch specifically on |
| 4488 | // `read_tx_size`'s own rng, not attempted this round -- budget), so |
| 4489 | // r5 CONFIRMED the suspect and it was simpler than the CDF row: the |
| 4490 | // tx-depth symbol exists only under TX_MODE_SELECT, and this path read |
| 4491 | // it unconditionally where both square paths gate on `tx_select`. With |
| 4492 | // --enable-tx-size-search=0 the encoder writes no such symbol, so the |
| 4493 | // decoder consumed one that was never there. |
| 4494 | { |
| 4495 | let around = neighbours.around_rect(at, bw, bh); |
| 4496 | let mut luma_coding = cdfs.txb(TxbSet::LumaRect32x16, mode); |
| 4497 | let (luma_levels, luma_tx_type) = read_coeffs_rect( |
| 4498 | dec, |
| 4499 | &mut luma_coding, |
| 4500 | luma_scan, |
| 4501 | bw, |
| 4502 | bh, |
| 4503 | 0, |
| 4504 | dc_sign_ctx(around[0].2), |
| 4505 | TxType::DctDct, |
| 4506 | )?; |
| 4507 | if std::env::var_os("EC_AV1_TRACE").is_some() { |
| 4508 | let (rng, _) = dec.debug_state(); |
| 4509 | eprintln!("TRACE_RECT_COEFF plane=0 mi_row={mi_r} mi_col={mi_c} rng={rng}"); |
| 4510 | } |
| 4511 | let luma_residual = dequant_and_inverse_typed_wh( |
| 4512 | &luma_levels, |
| 4513 | bw, |
| 4514 | bh, |
| 4515 | crate::decode::bit_depth(), |
| 4516 | block_q_idx(), |
| 4517 | plane_q_delta(0).0, |
| 4518 | plane_q_delta(0).1, |
| 4519 | luma_tx_type, |
| 4520 | ); |
| 4521 | y.reconstruct_rect( |
| 4522 | px, |
| 4523 | py, |
| 4524 | bw, |
| 4525 | bh, |
| 4526 | mode, |
| 4527 | angle_delta_y, |
| 4528 | reach, |
| 4529 | &luma_residual, |
| 4530 | None, |
| 4531 | filter_intra, |
| 4532 | smooth_neighbor, |
| 4533 | ); |
| 4534 | let ac = alpha.map(|_| cfl_ac_q3_rect(y, px, py, bw, bh)); |
| 4535 | let u_default_tx = default_intra_tx_type(uv_predict_mode as u8); |
| 4536 | let u_skip_ctx = usize::from(around[1].0) + usize::from(around[1].1); |
| 4537 | let mut u_coding = cdfs.txb(TxbSet::ChromaRect16x8, uv_predict_mode); |
| 4538 | let (u_levels, u_tx_type) = read_coeffs_rect( |
| 4539 | dec, |
| 4540 | &mut u_coding, |
| 4541 | chroma_scan, |
| 4542 | chroma_w, |
| 4543 | chroma_h, |
| 4544 | u_skip_ctx, |
| 4545 | dc_sign_ctx(around[1].2), |
| 4546 | u_default_tx, |
| 4547 | )?; |
| 4548 | if std::env::var_os("EC_AV1_TRACE").is_some() { |
| 4549 | let (rng, _) = dec.debug_state(); |
| 4550 | eprintln!("TRACE_RECT_COEFF plane=1 mi_row={mi_r} mi_col={mi_c} rng={rng}"); |
| 4551 | } |
| 4552 | let u_residual = dequant_and_inverse_typed_wh( |
| 4553 | &u_levels, |
| 4554 | chroma_w, |
| 4555 | chroma_h, |
| 4556 | crate::decode::bit_depth(), |
| 4557 | block_q_idx(), |
| 4558 | plane_q_delta(1).0, |
| 4559 | plane_q_delta(1).1, |
| 4560 | u_tx_type, |
| 4561 | ); |
| 4562 | u.reconstruct_rect( |
| 4563 | cpx, |
| 4564 | cpy, |
| 4565 | chroma_w, |
| 4566 | chroma_h, |
| 4567 | uv_predict_mode, |
| 4568 | angle_delta_uv, |
| 4569 | reach, |
| 4570 | &u_residual, |
| 4571 | alpha.zip(ac.as_deref()).map(|((au, _), ac)| (au, ac)), |
| 4572 | None, |
| 4573 | smooth_neighbor_uv, |
| 4574 | ); |
| 4575 | let v_default_tx = default_intra_tx_type(uv_predict_mode as u8); |
| 4576 | let v_skip_ctx = usize::from(around[2].0) + usize::from(around[2].1); |
| 4577 | let mut v_coding = cdfs.txb(TxbSet::ChromaRect16x8, uv_predict_mode); |
| 4578 | let (v_levels, v_tx_type) = read_coeffs_rect( |
| 4579 | dec, |
| 4580 | &mut v_coding, |
| 4581 | chroma_scan, |
| 4582 | chroma_w, |
| 4583 | chroma_h, |
| 4584 | v_skip_ctx, |
| 4585 | dc_sign_ctx(around[2].2), |
| 4586 | v_default_tx, |
| 4587 | )?; |
| 4588 | if std::env::var_os("EC_AV1_TRACE").is_some() { |
| 4589 | let (rng, _) = dec.debug_state(); |
| 4590 | eprintln!("TRACE_RECT_COEFF plane=2 mi_row={mi_r} mi_col={mi_c} rng={rng}"); |
| 4591 | } |
| 4592 | let v_residual = dequant_and_inverse_typed_wh( |
| 4593 | &v_levels, |
| 4594 | chroma_w, |
| 4595 | chroma_h, |
| 4596 | crate::decode::bit_depth(), |
| 4597 | block_q_idx(), |
| 4598 | plane_q_delta(2).0, |
| 4599 | plane_q_delta(2).1, |
| 4600 | v_tx_type, |
| 4601 | ); |
| 4602 | v.reconstruct_rect( |
| 4603 | cpx, |
| 4604 | cpy, |
| 4605 | chroma_w, |
| 4606 | chroma_h, |
| 4607 | uv_predict_mode, |
| 4608 | angle_delta_uv, |
| 4609 | reach, |
| 4610 | &v_residual, |
| 4611 | alpha.zip(ac.as_deref()).map(|((_, av), ac)| (av, ac)), |
| 4612 | None, |
| 4613 | smooth_neighbor_uv, |
| 4614 | ); |
| 4615 | neighbours.record_rect(at, bw, bh, mode, uv_predict_mode, &[luma_levels, u_levels, v_levels]); |
| 4616 | RECT_COEFF_HITS.with(|c| c.set(c.get() + 1)); |
| 4617 | } |
| 4618 | } |
| 4619 | neighbours.fill_skip_grid_rect((mi_r, mi_c), bw / MI, bh / MI, skip); |
| 4620 | neighbours.fill_lf_grid_rect((mi_r, mi_c), bw / MI, bh / MI, tx_w as u8, tx_h as u8, 0); |
| 4621 | RECT_PARTITION_HITS.with(|c| c.set(c.get() + 1)); |
| 4622 | if std::env::var_os("EC_AV1_TRACE").is_some() { |
| 4623 | eprintln!("TRACE_RECT32_END mi_row={mi_r} mi_col={mi_c} bw={bw} bh={bh}"); |
| 4624 | } |
| 4625 | Ok(()) |
| 4626 | } |
| 4627 | |
| 4628 | /// Decodes one `bw`x`bh` rect strip of a plain 16x16-level `PARTITION_HORZ`/ |
| 4629 | /// `PARTITION_VERT` (lane-rect16 r2), sibling to [`decode_block_rect`] but |
| 4630 | /// addressed by REAL mi coordinates rather than derived from an [`SUB`]-grid |
| 4631 | /// `outer_at`, the same relationship [`decode_leaf8`] has to `decode_block`: |
| 4632 | /// the second strip of a HORZ/VERT split sits offset by half the 16x16 |
| 4633 | /// parent, which `decode_block_rect`'s `at`-derived `px = c * SUB` can't |
| 4634 | /// name. `prev_leaf` mirrors `decode_leaf8`'s own convention -- when the |
| 4635 | /// previous strip is directly above (same column, `mi_h` rows up) or |
| 4636 | /// directly left (same row, `mi_w` cols over), its mode overrides the coarse |
| 4637 | /// per-16x16-cell `above_mode`/`left_mode` slot for THIS strip's own mode |
| 4638 | /// context read, and bookkeeping goes through the `_mi`/`_rect` neighbour |
| 4639 | /// writers (`record_mi_rect`, `fill_skip_grid_rect`, `fill_lf_grid_rect`) at |
| 4640 | /// this strip's real position, never the coarse `record_rect` (which the |
| 4641 | /// caller uses once, after both strips, exactly like the `VERT_B` arm next |
| 4642 | /// to it). Only the skip case is ported -- see [`decode_block_rect`]'s own |
| 4643 | /// doc for why a coded rect strip below 16x16 has no coefficient tables here |
| 4644 | /// yet. |
| 4645 | #[allow(clippy::too_many_arguments)] |
| 4646 | fn decode_leaf_rect( |
| 4647 | dec: &mut SymbolDecoder, |
| 4648 | cdfs: &mut Cdfs, |
| 4649 | neighbours: &mut Neighbours, |
| 4650 | outer_at: (usize, usize), |
| 4651 | leaf_mi: (usize, usize), |
| 4652 | bw: usize, |
| 4653 | bh: usize, |
| 4654 | prev_leaf: Option<((usize, usize), usize)>, |
| 4655 | y: &mut PlaneBuf, |
| 4656 | u: &mut PlaneBuf, |
| 4657 | v: &mut PlaneBuf, |
| 4658 | enable_filter_intra: bool, |
| 4659 | allow_screen_content_tools: bool, |
| 4660 | base_q_idx: u8, |
| 4661 | tx_select: bool, |
| 4662 | ) -> Result<usize> { |
| 4663 | let _ = base_q_idx; |
| 4664 | let (r, c) = outer_at; |
| 4665 | let mut above_mode = neighbours.above_mode[c]; |
| 4666 | let mut left_mode = neighbours.left_mode[r]; |
| 4667 | let (mi_w, mi_h) = (bw / MI, bh / MI); |
| 4668 | if let Some(((pr, pc), pmode)) = prev_leaf { |
| 4669 | if pc == leaf_mi.1 && leaf_mi.0 == pr + mi_h { |
| 4670 | above_mode = pmode; |
| 4671 | } else if pr == leaf_mi.0 && leaf_mi.1 == pc + mi_w { |
| 4672 | left_mode = pmode; |
| 4673 | } |
| 4674 | } |
| 4675 | let smooth_neighbor = |
| 4676 | is_smooth_mode(above_mode) || is_smooth_mode(left_mode); |
| 4677 | if smooth_neighbor { |
| 4678 | SMOOTH_LUMA_HITS.with(|c| c.set(c.get() + 1)); |
| 4679 | } |
| 4680 | let (skip, mode, angle_delta_y, uv_mode, angle_delta_uv, alpha, filter_intra) = |
| 4681 | read_intra_mode_rect( |
| 4682 | dec, |
| 4683 | cdfs, |
| 4684 | above_mode, |
| 4685 | left_mode, |
| 4686 | true, |
| 4687 | bw, |
| 4688 | bh, |
| 4689 | enable_filter_intra, |
| 4690 | neighbours.skip_txfm_ctx(leaf_mi.0, leaf_mi.1), |
| 4691 | allow_screen_content_tools, |
| 4692 | leaf_mi.0, |
| 4693 | leaf_mi.1, |
| 4694 | )?; |
| 4695 | if filter_intra.is_some() { |
| 4696 | return Err(unsupported( |
| 4697 | "filter intra on a HORZ/VERT strip (this decoder predicts square-only)", |
| 4698 | )); |
| 4699 | } |
| 4700 | let uv_predict_mode = if uv_mode == UV_CFL_PRED { DC_PRED } else { uv_mode }; |
| 4701 | let depth = if tx_select { |
| 4702 | let ctx = tx_size_context_rect(neighbours, leaf_mi, bw, bh); |
| 4703 | dec.symbol(&mut cdfs.tx_size_cat2[ctx]) |
| 4704 | } else { |
| 4705 | 0 |
| 4706 | }; |
| 4707 | if depth != 0 { |
| 4708 | return Err(unsupported( |
| 4709 | "a HORZ/VERT intra strip below 16x16 with a split transform (per-unit rect \ |
| 4710 | prediction is not ported)", |
| 4711 | )); |
| 4712 | } |
| 4713 | if !skip { |
| 4714 | return Err(unsupported( |
| 4715 | "a coded (non-skip) HORZ/VERT rect strip below 16x16 (this decoder ports only \ |
| 4716 | the skip case at this size)", |
| 4717 | )); |
| 4718 | } |
| 4719 | let (px, py) = (leaf_mi.1 * MI, leaf_mi.0 * MI); |
| 4720 | let reach = Reach::of_rect(bw, bh, px, py, y.width, y.height); |
| 4721 | let (cpx, cpy) = (px / 2, py / 2); |
| 4722 | let (chroma_w, chroma_h) = (bw / 2, bh / 2); |
| 4723 | y.reconstruct_rect( |
| 4724 | px, |
| 4725 | py, |
| 4726 | bw, |
| 4727 | bh, |
| 4728 | mode, |
| 4729 | angle_delta_y, |
| 4730 | reach, |
| 4731 | &vec![0i32; bw * bh], |
| 4732 | None, |
| 4733 | filter_intra, |
| 4734 | smooth_neighbor, |
| 4735 | ); |
| 4736 | let ac = alpha.map(|_| cfl_ac_q3_rect(y, px, py, bw, bh)); |
| 4737 | u.reconstruct_rect( |
| 4738 | cpx, |
| 4739 | cpy, |
| 4740 | chroma_w, |
| 4741 | chroma_h, |
| 4742 | uv_predict_mode, |
| 4743 | angle_delta_uv, |
| 4744 | reach, |
| 4745 | &vec![0i32; chroma_w * chroma_h], |
| 4746 | alpha.zip(ac.as_deref()).map(|((au, _), ac)| (au, ac)), |
| 4747 | None, |
| 4748 | false, |
| 4749 | ); |
| 4750 | v.reconstruct_rect( |
| 4751 | cpx, |
| 4752 | cpy, |
| 4753 | chroma_w, |
| 4754 | chroma_h, |
| 4755 | uv_predict_mode, |
| 4756 | angle_delta_uv, |
| 4757 | reach, |
| 4758 | &vec![0i32; chroma_w * chroma_h], |
| 4759 | alpha.zip(ac.as_deref()).map(|((_, av), ac)| (av, ac)), |
| 4760 | None, |
| 4761 | false, |
| 4762 | ); |
| 4763 | neighbours.record_mi_rect( |
| 4764 | leaf_mi, |
| 4765 | bw, |
| 4766 | bh, |
| 4767 | &[ |
| 4768 | vec![0i32; bw * bh], |
| 4769 | vec![0i32; chroma_w * chroma_h], |
| 4770 | vec![0i32; chroma_w * chroma_h], |
| 4771 | ], |
| 4772 | ); |
| 4773 | neighbours.fill_skip_grid_rect(leaf_mi, mi_w, mi_h, skip); |
| 4774 | neighbours.fill_lf_grid_rect(leaf_mi, mi_w, mi_h, bw as u8, bh as u8, 0); |
| 4775 | Ok(mode) |
| 4776 | } |
| 4777 | |
| 4778 | /// Decodes one true `bw`x`bh` superblock-level `PARTITION_HORZ`/ |
| 4779 | /// `PARTITION_VERT` strip (lane-sbpart r2, `bw, bh` one of `(64, 32)` / |
| 4780 | /// `(32, 64)` -- [`decode_block_rect`]'s own sibling one level up). Unlike |
| 4781 | /// that 32x16/16x32 strip, both dimensions here exceed the 32-coefficient |
| 4782 | /// cap, so LUMA needs the same corner truncation [`decode_block`] already |
| 4783 | /// does for a plain 64x64 square (spec 5.11.40): `get_txsize_entropy_ctx` |
| 4784 | /// resolves TX_64X32/TX_32X64 to TX_64X64 regardless of orientation (see |
| 4785 | /// this function's own charter/report), so both strips read a real 32x32 |
| 4786 | /// corner through [`read_coeffs`] with [`TxbSet::Luma64`]/[`SCAN_32`] and |
| 4787 | /// embed it top-left of a zeroed `bw`x`bh` grid, mirroring |
| 4788 | /// [`read_plane`]'s `tx_side != side` branch generalized to non-square. |
| 4789 | /// CHROMA (TX_32X16/TX_16X32) resolves to TX_32X32 -- a real, *untruncated* |
| 4790 | /// `bw/2`x`bh/2` transform via [`read_coeffs_rect`] with |
| 4791 | /// [`TxbSet::ChromaRect32x16`] and the matching [`SCAN_32X16`]/ |
| 4792 | /// [`SCAN_16X32`], same as [`decode_block_rect`]'s own chroma path. Like |
| 4793 | /// that function, only the single-transform-unit (`tx_depth == 0`) case is |
| 4794 | /// supported: a split transform refuses by name rather than guess-decode. |
| 4795 | #[allow(clippy::too_many_arguments)] |
| 4796 | fn decode_block_rect64( |
| 4797 | dec: &mut SymbolDecoder, |
| 4798 | cdfs: &mut Cdfs, |
| 4799 | neighbours: &mut Neighbours, |
| 4800 | at: (usize, usize), |
| 4801 | bw: usize, |
| 4802 | bh: usize, |
| 4803 | y: &mut PlaneBuf, |
| 4804 | u: &mut PlaneBuf, |
| 4805 | v: &mut PlaneBuf, |
| 4806 | enable_filter_intra: bool, |
| 4807 | allow_screen_content_tools: bool, |
| 4808 | base_q_idx: u8, |
| 4809 | tx_select: bool, |
| 4810 | reduced_tx_set: bool, |
| 4811 | ) -> Result<()> { |
| 4812 | let (r, c) = at; |
| 4813 | let (px, py) = (c * SUB, r * SUB); |
| 4814 | let (skip, mode, angle_delta_y, uv_mode, angle_delta_uv, alpha, filter_intra) = |
| 4815 | read_intra_mode_rect( |
| 4816 | dec, |
| 4817 | cdfs, |
| 4818 | neighbours.above_mode[c], |
| 4819 | neighbours.left_mode[r], |
| 4820 | // `is_cfl_allowed` (spec 5.11.5) caps CFL at <=32x32; these |
| 4821 | // superblock-level HORZ/VERT strips are 64x32/32x64, so unlike |
| 4822 | // `decode_block_rect`'s own 32x16/16x32 strips (where `true` is |
| 4823 | // correct), CFL must never be offered here -- passing `true` |
| 4824 | // read `uv_mode` off the 14-symbol `uv_mode_cfl` CDF instead of |
| 4825 | // the real 13-symbol `uv_mode_no_cfl` one and desynced the tile |
| 4826 | // from this block's very first symbol (lane-sbpart r3 bisect). |
| 4827 | false, |
| 4828 | bw, |
| 4829 | bh, |
| 4830 | enable_filter_intra, |
| 4831 | neighbours.skip_txfm_ctx(r * (SUB / MI), c * (SUB / MI)), |
| 4832 | allow_screen_content_tools, |
| 4833 | r * (SUB / MI), |
| 4834 | c * (SUB / MI), |
| 4835 | )?; |
| 4836 | let smooth_neighbor = |
| 4837 | is_smooth_mode(neighbours.above_mode[c]) || is_smooth_mode(neighbours.left_mode[r]); |
| 4838 | if smooth_neighbor { |
| 4839 | SMOOTH_LUMA_HITS.with(|c| c.set(c.get() + 1)); |
| 4840 | } |
| 4841 | let smooth_neighbor_uv = |
| 4842 | is_smooth_mode(neighbours.above_uv_mode[c]) || is_smooth_mode(neighbours.left_uv_mode[r]); |
| 4843 | if filter_intra.is_some() { |
| 4844 | // `filter_intra_size_class_rect` already returns `None` for both |
| 4845 | // `(64, 32)` and `(32, 64)` (`av1_filter_intra_allowed_bsize` caps at |
| 4846 | // 32 on both axes), so this symbol is never read at this level -- |
| 4847 | // kept only as the same defensive refusal [`decode_block_rect`] has. |
| 4848 | return Err(unsupported( |
| 4849 | "filter intra on a superblock-level HORZ/VERT strip (never expected -- \ |
| 4850 | av1_filter_intra_allowed_bsize caps at 32x32)", |
| 4851 | )); |
| 4852 | } |
| 4853 | let uv_predict_mode = if uv_mode == UV_CFL_PRED { |
| 4854 | DC_PRED |
| 4855 | } else { |
| 4856 | uv_mode |
| 4857 | }; |
| 4858 | let (mi_r, mi_c) = (r * (SUB / MI), c * (SUB / MI)); |
| 4859 | let depth = if tx_select { |
| 4860 | let ctx = tx_size_context_rect(neighbours, (mi_r, mi_c), bw, bh); |
| 4861 | // lane-rectsplit r1: `bsize_to_tx_size_cat` (libaom `decodemv.c`) |
| 4862 | // counts `sub_tx_size_map` steps from the block's own max rect |
| 4863 | // transform down to TX_4X4, minus one -- BLOCK_64X32's chain |
| 4864 | // (TX_64X32 -> TX_32X32 -> TX_16X16 -> TX_8X8 -> TX_4X4) is four |
| 4865 | // steps, so this level's category is 3 (the same one a 64x64 square |
| 4866 | // block uses), not the 2 a 32x16 strip uses. Reading the cat-2 CDF |
| 4867 | // here was a wrong-table read of a real symbol. |
| 4868 | dec.symbol(&mut cdfs.tx_size_cat3[ctx]) |
| 4869 | } else { |
| 4870 | 0 |
| 4871 | }; |
| 4872 | if depth != 0 { |
| 4873 | TX_DEPTH_HITS.with(|c| c.set(c.get() + 1)); |
| 4874 | // lane-rectsplit r1/r3: [`decode_rect_split`] handles this shape too |
| 4875 | // and the per-unit port is wired here, but the gate |
| 4876 | // (`a_real_aomenc_stream_with_a_split_transform_superblock_strip_decodes_pixel_exact`, |
| 4877 | // `#[ignore]`d with its measurement) is still RED at seed 50 (luma |
| 4878 | // (171,56), ours 147 vs ffmpeg 148). r2 believed the cause was a |
| 4879 | // transposed `av1_nz_map_ctx_offset` read; r3 measured that |
| 4880 | // transposing it turns THIS gate green and the merged SB-level |
| 4881 | // HORZ/VERT partition gate RED (seed 43), so the cause is still |
| 4882 | // open. A refusal this lane cannot lift is not one it may ship wrong |
| 4883 | // pixels past. |
| 4884 | return Err(unsupported( |
| 4885 | "a superblock-level HORZ/VERT strip with a split transform (per-unit rect \ |
| 4886 | prediction is not ported)", |
| 4887 | )); |
| 4888 | } |
| 4889 | let (tx_w, tx_h) = (bw, bh); |
| 4890 | let (cpx, cpy) = (px / 2, py / 2); |
| 4891 | let (chroma_w, chroma_h) = (bw / 2, bh / 2); |
| 4892 | let reach = Reach::of_rect(bw, bh, px, py, y.width, y.height); |
| 4893 | if skip { |
| 4894 | y.reconstruct_rect( |
| 4895 | px, |
| 4896 | py, |
| 4897 | bw, |
| 4898 | bh, |
| 4899 | mode, |
| 4900 | angle_delta_y, |
| 4901 | reach, |
| 4902 | &vec![0i32; bw * bh], |
| 4903 | None, |
| 4904 | filter_intra, |
| 4905 | smooth_neighbor, |
| 4906 | ); |
| 4907 | let ac = alpha.map(|_| cfl_ac_q3_rect(y, px, py, bw, bh)); |
| 4908 | u.reconstruct_rect( |
| 4909 | cpx, |
| 4910 | cpy, |
| 4911 | chroma_w, |
| 4912 | chroma_h, |
| 4913 | uv_predict_mode, |
| 4914 | angle_delta_uv, |
| 4915 | reach, |
| 4916 | &vec![0i32; chroma_w * chroma_h], |
| 4917 | alpha.zip(ac.as_deref()).map(|((au, _), ac)| (au, ac)), |
| 4918 | None, |
| 4919 | smooth_neighbor_uv, |
| 4920 | ); |
| 4921 | v.reconstruct_rect( |
| 4922 | cpx, |
| 4923 | cpy, |
| 4924 | chroma_w, |
| 4925 | chroma_h, |
| 4926 | uv_predict_mode, |
| 4927 | angle_delta_uv, |
| 4928 | reach, |
| 4929 | &vec![0i32; chroma_w * chroma_h], |
| 4930 | alpha.zip(ac.as_deref()).map(|((_, av), ac)| (av, ac)), |
| 4931 | None, |
| 4932 | smooth_neighbor_uv, |
| 4933 | ); |
| 4934 | neighbours.record_rect( |
| 4935 | at, |
| 4936 | bw, |
| 4937 | bh, |
| 4938 | mode, |
| 4939 | uv_predict_mode, |
| 4940 | &[ |
| 4941 | vec![0i32; bw * bh], |
| 4942 | vec![0i32; chroma_w * chroma_h], |
| 4943 | vec![0i32; chroma_w * chroma_h], |
| 4944 | ], |
| 4945 | ); |
| 4946 | } else { |
| 4947 | let around = neighbours.around_rect(at, bw, bh); |
| 4948 | // LUMA: a real 32x32 corner, embedded top-left of the true bw x bh |
| 4949 | // grid (see this function's own doc comment). |
| 4950 | let scan32 = default_scan(TX32); |
| 4951 | let mut luma_coding = cdfs.txb(TxbSet::Luma64, mode); |
| 4952 | if std::env::var_os("EC_TRACE_COEFF").is_some() { |
| 4953 | let (rng, _) = dec.debug_state(); |
| 4954 | eprintln!("EC_COEFF plane=0 row={mi_r} col={mi_c} tx_size=64corner rng={rng}"); |
| 4955 | } |
| 4956 | let (luma_corner, luma_tx_type) = read_coeffs( |
| 4957 | dec, |
| 4958 | &mut luma_coding, |
| 4959 | &scan32, |
| 4960 | 0, |
| 4961 | dc_sign_ctx(around[0].2), |
| 4962 | TxType::DctDct, |
| 4963 | Some((bw, bh)), |
| 4964 | )?; |
| 4965 | if std::env::var_os("EC_TRACE_COEFF").is_some() { |
| 4966 | let (rng, _) = dec.debug_state(); |
| 4967 | eprintln!("EC_COEFF_VAL plane=0 row={mi_r} col={mi_c} rng={rng}"); |
| 4968 | } |
| 4969 | let mut luma_levels = vec![0i32; bw * bh]; |
| 4970 | for row in 0..32 { |
| 4971 | luma_levels[row * bw..][..32].copy_from_slice(&luma_corner[row * 32..][..32]); |
| 4972 | } |
| 4973 | { |
| 4974 | let cur = block_q_idx(); |
| 4975 | if cur != i32::from(base_q_idx) { |
| 4976 | RECT64_QIDX_DRIFT_HITS.with(|c| c.set(c.get() + 1)); |
| 4977 | } |
| 4978 | if std::env::var_os("EC_AV1_TRACE").is_some() { |
| 4979 | eprintln!( |
| 4980 | "TRACE rect64_dequant plane=0 base_q_idx={base_q_idx} current_q_idx={cur}" |
| 4981 | ); |
| 4982 | } |
| 4983 | } |
| 4984 | let luma_residual = dequant_and_inverse_typed_wh( |
| 4985 | &luma_levels, |
| 4986 | bw, |
| 4987 | bh, |
| 4988 | crate::decode::bit_depth(), |
| 4989 | block_q_idx(), |
| 4990 | plane_q_delta(0).0, |
| 4991 | plane_q_delta(0).1, |
| 4992 | luma_tx_type, |
| 4993 | ); |
| 4994 | y.reconstruct_rect( |
| 4995 | px, |
| 4996 | py, |
| 4997 | bw, |
| 4998 | bh, |
| 4999 | mode, |
| 5000 | angle_delta_y, |
| 5001 | reach, |
| 5002 | &luma_residual, |
| 5003 | None, |
| 5004 | filter_intra, |
| 5005 | smooth_neighbor, |
| 5006 | ); |
| 5007 | if std::env::var_os("EC_SBPART_DUMP64").is_some() { |
| 5008 | let leftcol: Vec<u16> = if px > 0 { |
| 5009 | (py..py + bh).map(|row| y.data[row * y.width + px - 1]).collect() |
| 5010 | } else { |
| 5011 | vec![] |
| 5012 | }; |
| 5013 | eprintln!( |
| 5014 | "DUMP64 mi_r={mi_r} mi_c={mi_c} px={px} py={py} bw={bw} bh={bh} mode={mode} \ |
| 5015 | skip={skip} eob_nonzero={} row0={:?} leftcol={:?}", |
| 5016 | luma_corner.iter().any(|&v| v != 0), |
| 5017 | &y.data[py * y.width + px..][..bw], |
| 5018 | leftcol, |
| 5019 | ); |
| 5020 | } |
| 5021 | // CHROMA: a real, untruncated chroma_w x chroma_h transform -- no |
| 5022 | // corner crop needed (see doc comment). |
| 5023 | let ac = alpha.map(|_| cfl_ac_q3_rect(y, px, py, bw, bh)); |
| 5024 | let chroma_scan: &[u16] = if bw == 64 { &SCAN_32X16 } else { &SCAN_16X32 }; |
| 5025 | let u_skip_ctx = usize::from(around[1].0) + usize::from(around[1].1); |
| 5026 | let mut u_coding = cdfs.txb(TxbSet::ChromaRect32x16, uv_predict_mode); |
| 5027 | if std::env::var_os("EC_TRACE_COEFF").is_some() { |
| 5028 | let (rng, _) = dec.debug_state(); |
| 5029 | eprintln!("EC_COEFF plane=1 row={mi_r} col={mi_c} tx_size=rect32x16 rng={rng}"); |
| 5030 | } |
| 5031 | let (u_levels, u_tx_type) = read_coeffs_rect( |
| 5032 | dec, |
| 5033 | &mut u_coding, |
| 5034 | chroma_scan, |
| 5035 | chroma_w, |
| 5036 | chroma_h, |
| 5037 | u_skip_ctx, |
| 5038 | dc_sign_ctx(around[1].2), |
| 5039 | TxType::DctDct, |
| 5040 | )?; |
| 5041 | if std::env::var_os("EC_TRACE_COEFF").is_some() { |
| 5042 | let (rng, _) = dec.debug_state(); |
| 5043 | eprintln!("EC_COEFF_VAL plane=1 row={mi_r} col={mi_c} rng={rng}"); |
| 5044 | } |
| 5045 | { |
| 5046 | let cur = block_q_idx(); |
| 5047 | if cur != i32::from(base_q_idx) { |
| 5048 | RECT64_QIDX_DRIFT_HITS.with(|c| c.set(c.get() + 1)); |
| 5049 | } |
| 5050 | if std::env::var_os("EC_AV1_TRACE").is_some() { |
| 5051 | eprintln!( |
| 5052 | "TRACE rect64_dequant plane=1 base_q_idx={base_q_idx} current_q_idx={cur}" |
| 5053 | ); |
| 5054 | } |
| 5055 | } |
| 5056 | let u_residual = dequant_and_inverse_typed_wh( |
| 5057 | &u_levels, |
| 5058 | chroma_w, |
| 5059 | chroma_h, |
| 5060 | crate::decode::bit_depth(), |
| 5061 | block_q_idx(), |
| 5062 | plane_q_delta(1).0, |
| 5063 | plane_q_delta(1).1, |
| 5064 | u_tx_type, |
| 5065 | ); |
| 5066 | u.reconstruct_rect( |
| 5067 | cpx, |
| 5068 | cpy, |
| 5069 | chroma_w, |
| 5070 | chroma_h, |
| 5071 | uv_predict_mode, |
| 5072 | angle_delta_uv, |
| 5073 | reach, |
| 5074 | &u_residual, |
| 5075 | alpha.zip(ac.as_deref()).map(|((au, _), ac)| (au, ac)), |
| 5076 | None, |
| 5077 | smooth_neighbor_uv, |
| 5078 | ); |
| 5079 | let v_skip_ctx = usize::from(around[2].0) + usize::from(around[2].1); |
| 5080 | let mut v_coding = cdfs.txb(TxbSet::ChromaRect32x16, uv_predict_mode); |
| 5081 | if std::env::var_os("EC_TRACE_COEFF").is_some() { |
| 5082 | let (rng, _) = dec.debug_state(); |
| 5083 | eprintln!("EC_COEFF plane=2 row={mi_r} col={mi_c} tx_size=rect32x16 rng={rng}"); |
| 5084 | } |
| 5085 | let (v_levels, v_tx_type) = read_coeffs_rect( |
| 5086 | dec, |
| 5087 | &mut v_coding, |
| 5088 | chroma_scan, |
| 5089 | chroma_w, |
| 5090 | chroma_h, |
| 5091 | v_skip_ctx, |
| 5092 | dc_sign_ctx(around[2].2), |
| 5093 | TxType::DctDct, |
| 5094 | )?; |
| 5095 | if std::env::var_os("EC_TRACE_COEFF").is_some() { |
| 5096 | let (rng, _) = dec.debug_state(); |
| 5097 | eprintln!("EC_COEFF_VAL plane=2 row={mi_r} col={mi_c} rng={rng}"); |
| 5098 | } |
| 5099 | { |
| 5100 | let cur = block_q_idx(); |
| 5101 | if cur != i32::from(base_q_idx) { |
| 5102 | RECT64_QIDX_DRIFT_HITS.with(|c| c.set(c.get() + 1)); |
| 5103 | } |
| 5104 | if std::env::var_os("EC_AV1_TRACE").is_some() { |
| 5105 | eprintln!( |
| 5106 | "TRACE rect64_dequant plane=2 base_q_idx={base_q_idx} current_q_idx={cur}" |
| 5107 | ); |
| 5108 | } |
| 5109 | } |
| 5110 | let v_residual = dequant_and_inverse_typed_wh( |
| 5111 | &v_levels, |
| 5112 | chroma_w, |
| 5113 | chroma_h, |
| 5114 | crate::decode::bit_depth(), |
| 5115 | block_q_idx(), |
| 5116 | plane_q_delta(2).0, |
| 5117 | plane_q_delta(2).1, |
| 5118 | v_tx_type, |
| 5119 | ); |
| 5120 | v.reconstruct_rect( |
| 5121 | cpx, |
| 5122 | cpy, |
| 5123 | chroma_w, |
| 5124 | chroma_h, |
| 5125 | uv_predict_mode, |
| 5126 | angle_delta_uv, |
| 5127 | reach, |
| 5128 | &v_residual, |
| 5129 | alpha.zip(ac.as_deref()).map(|((_, av), ac)| (av, ac)), |
| 5130 | None, |
| 5131 | smooth_neighbor_uv, |
| 5132 | ); |
| 5133 | neighbours.record_rect(at, bw, bh, mode, uv_predict_mode, &[luma_levels, u_levels, v_levels]); |
| 5134 | RECT_COEFF_HITS.with(|c| c.set(c.get() + 1)); |
| 5135 | } |
| 5136 | neighbours.fill_skip_grid_rect((mi_r, mi_c), bw / MI, bh / MI, skip); |
| 5137 | neighbours.fill_lf_grid_rect((mi_r, mi_c), bw / MI, bh / MI, tx_w as u8, tx_h as u8, 0); |
| 5138 | SB_RECT_HITS.with(|c| c.set(c.get() + 1)); |
| 5139 | if std::env::var_os("EC_AV1_TRACE").is_some() { |
| 5140 | let (rng, _) = dec.debug_state(); |
| 5141 | eprintln!("TRACE_RECT64_END mi_row={mi_r} mi_col={mi_c} bw={bw} bh={bh} rng={rng}"); |
| 5142 | } |
| 5143 | Ok(()) |
| 5144 | } |
| 5145 | |
| 5146 | /// `read_angle_delta`/`Angle_Delta` (spec 5.11.42 + 9.3): the symbol minus |
| 5147 | /// [`ANGLE_DELTA_ZERO`] gives the signed `-MAX_ANGLE_DELTA..=MAX_ANGLE_DELTA` |
| 5148 | /// delta [`crate::intra::predict`] wants; bumps [`ANGLE_DELTA_HITS`] whenever |
| 5149 | /// it lands away from zero, for gate tests to prove a real stream fired one. |
| 5150 | fn read_angle_delta(dec: &mut SymbolDecoder, cdf: &mut [u16]) -> i32 { |
| 5151 | let symbol = dec.symbol(cdf); |
| 5152 | if symbol != ANGLE_DELTA_ZERO { |
| 5153 | ANGLE_DELTA_HITS.with(|c| c.set(c.get() + 1)); |
| 5154 | } |
| 5155 | symbol as i32 - ANGLE_DELTA_ZERO as i32 |
| 5156 | } |
| 5157 | |
| 5158 | #[allow(clippy::too_many_arguments)] |
| 5159 | fn read_intra_mode( |
| 5160 | dec: &mut SymbolDecoder, |
| 5161 | cdfs: &mut Cdfs, |
| 5162 | above_mode: usize, |
| 5163 | left_mode: usize, |
| 5164 | cfl: bool, |
| 5165 | side: usize, |
| 5166 | enable_filter_intra: bool, |
| 5167 | skip_ctx: usize, |
| 5168 | allow_screen_content_tools: bool, |
| 5169 | allow_intrabc: bool, |
| 5170 | // `Some((palette_mode_ctx, color_cache))` at the call sites this lane's |
| 5171 | // scope actually reconstructs a palette-Y block for (`av1_get_palette_mode_ctx` |
| 5172 | // and `av1_get_palette_cache`'s neighbour lookup, [`Neighbours::palette_ctx_and_cache`]); |
| 5173 | // `None` at the excluded call sites (the 8x8-leaf path), which still read |
| 5174 | // the `palette_y_mode` symbol at ctx 0 -- matching the bits a real |
| 5175 | // decoder reads either way -- but refuse by name the moment it fires, |
| 5176 | // exactly the old behaviour. |
| 5177 | palette: Option<(usize, &[u16])>, |
| 5178 | // The U-channel colour cache for a chroma palette read ([`Neighbours::palette_uv_cache`]), |
| 5179 | // `&[]` at the excluded call sites -- same corner-cut as `palette` above, |
| 5180 | // no `mode_ctx` needed (`palette_uv_mode_ctx` is this block's own just-read |
| 5181 | // `use_palette_y`, not a neighbour lookup). |
| 5182 | palette_uv_cache: &[u16], |
| 5183 | mi_r: usize, |
| 5184 | mi_c: usize, |
| 5185 | ) -> Result<( |
| 5186 | bool, |
| 5187 | usize, |
| 5188 | i32, |
| 5189 | usize, |
| 5190 | i32, |
| 5191 | Option<(i32, i32)>, |
| 5192 | Option<usize>, |
| 5193 | Option<PaletteY>, |
| 5194 | Option<PaletteUv>, |
| 5195 | )> { |
| 5196 | let trace = std::env::var_os("EC_AV1_TRACE").is_some(); |
| 5197 | // lane-tiny r2: EC_ISTEP-format trace (same "name=... val=... rng=..." |
| 5198 | // shape the oracle's own `ec_read_intra_frame_mode_info_impl` prints |
| 5199 | // under `EC_TRACE_MODE_STEP`) so a range ladder can be diffed line for |
| 5200 | // line against the instrumented aomdec, not just `tell()`. |
| 5201 | let ec_istep = std::env::var_os("EC_TRACE_MODE_STEP").is_some(); |
| 5202 | macro_rules! istep { |
| 5203 | ($name:literal, $val:expr) => { |
| 5204 | if ec_istep { |
| 5205 | let (rng, _) = dec.debug_state(); |
| 5206 | eprintln!( |
| 5207 | "EC_ISTEP mi_row={mi_r} mi_col={mi_c} name={} val={} rng={rng}", |
| 5208 | $name, $val |
| 5209 | ); |
| 5210 | } |
| 5211 | }; |
| 5212 | } |
| 5213 | // spec 5.11.6 `intra_frame_mode_info`: `intra_segment_id` is read BEFORE |
| 5214 | // `skip` when `SegIdPreSkip`, and after it otherwise. |
| 5215 | let (seg_w_mi, seg_h_mi) = (side / 4, side / 4); |
| 5216 | if seg_id_pre_skip() { |
| 5217 | intra_segment_id(dec, cdfs, mi_r, mi_c, seg_w_mi, seg_h_mi, false); |
| 5218 | } |
| 5219 | let skip = dec.symbol(&mut cdfs.skip[skip_ctx]) != 0; |
| 5220 | if trace { |
| 5221 | eprintln!("TRACE skip value={}", skip as i32); |
| 5222 | } |
| 5223 | istep!("skip", skip as i32); |
| 5224 | if !seg_id_pre_skip() { |
| 5225 | intra_segment_id(dec, cdfs, mi_r, mi_c, seg_w_mi, seg_h_mi, skip); |
| 5226 | } |
| 5227 | // spec order (see the comment below on `read_intrabc_info`): `skip`, |
| 5228 | // `segment_id`, `cdef`, `delta_q` -- `cdef` lands right here. |
| 5229 | maybe_read_cdef_idx(dec, mi_r, mi_c, skip); |
| 5230 | istep!("cdef", 0); |
| 5231 | maybe_read_delta_q(dec, cdfs, mi_r, mi_c, side == 64, skip); |
| 5232 | maybe_read_delta_lf(dec, cdfs, mi_r, mi_c, side == 64, skip); |
| 5233 | istep!("dq", 0); |
| 5234 | // `read_intrabc_info` (spec 5.11.13, libaom decodemv.c:693, called at |
| 5235 | // :811 right after `skip_txfm`/`segment_id`/`cdef`/`delta_q` and before |
| 5236 | // `mbmi->mode` is ever read): a `use_intrabc` symbol only present on an |
| 5237 | // intra frame whose header set `allow_intrabc`. Actual intra block copy |
| 5238 | // needs `assign_dv`'s own motion-vector-prediction machinery (`av1_find_mv_refs`, |
| 5239 | // `av1_find_ref_dv`) this decoder does not carry -- lane-screen scope |
| 5240 | // draws the line at consuming this one flag symbol and refusing by name |
| 5241 | // when it fires, so the arithmetic decoder still resyncs for a stream |
| 5242 | // that merely allows (but never uses) intrabc. |
| 5243 | if allow_intrabc { |
| 5244 | let use_intrabc = dec.symbol(&mut cdfs.intrabc) != 0; |
| 5245 | if trace { |
| 5246 | eprintln!("TRACE use_intrabc value={}", use_intrabc as i32); |
| 5247 | } |
| 5248 | if use_intrabc { |
| 5249 | let dv = read_intrabc_dv(dec, cdfs, mi_r, mi_c, side); |
| 5250 | if trace { |
| 5251 | eprintln!("TRACE intrabc dv={dv:?}"); |
| 5252 | } |
| 5253 | INTRABC_DV.with(|c| c.set(Some(dv))); |
| 5254 | INTRABC_HITS.with(|c| c.set(c.get() + 1)); |
| 5255 | // spec 5.11.13 / libaom `read_intra_frame_mode_info` returns the |
| 5256 | // moment `is_intrabc_block`: no y/uv mode, angle delta, palette, |
| 5257 | // CFL or filter-intra syntax follows. `YMode`/`UVMode` are forced |
| 5258 | // `DC_PRED` (which is also what the neighbour mode contexts and |
| 5259 | // the loop filter then see). |
| 5260 | return Ok((skip, DC_PRED, 0, DC_PRED, 0, None, None, None, None)); |
| 5261 | } |
| 5262 | } |
| 5263 | let above_ctx = INTRA_MODE_CTX[above_mode]; |
| 5264 | let left_ctx = INTRA_MODE_CTX[left_mode]; |
| 5265 | let mode = dec.symbol(&mut cdfs.kf_y_mode[above_ctx][left_ctx]); |
| 5266 | if trace { |
| 5267 | eprintln!("TRACE y_mode ctx=({above_ctx},{left_ctx}) value={mode}"); |
| 5268 | } |
| 5269 | istep!("mode", mode as i32); |
| 5270 | let angle_delta_y = if (V_PRED..=D67_PRED).contains(&mode) { |
| 5271 | let delta = read_angle_delta(dec, &mut cdfs.angle_delta[mode - V_PRED]); |
| 5272 | if trace { |
| 5273 | eprintln!( |
| 5274 | "TRACE angle_delta value={}", |
| 5275 | delta + ANGLE_DELTA_ZERO as i32 |
| 5276 | ); |
| 5277 | } |
| 5278 | delta |
| 5279 | } else { |
| 5280 | 0 |
| 5281 | }; |
| 5282 | istep!("angle_y", angle_delta_y); |
| 5283 | let uv_mode = if cfl { |
| 5284 | dec.symbol(&mut cdfs.uv_mode_cfl[mode]) |
| 5285 | } else { |
| 5286 | dec.symbol(&mut cdfs.uv_mode_no_cfl[mode]) |
| 5287 | }; |
| 5288 | if trace { |
| 5289 | eprintln!("TRACE uv_mode cfl={cfl} y_mode={mode} value={uv_mode}"); |
| 5290 | } |
| 5291 | istep!("uv_mode", uv_mode as i32); |
| 5292 | let alpha = if cfl && uv_mode == UV_CFL_PRED { |
| 5293 | Some(read_cfl_alphas(dec, cdfs)) |
| 5294 | } else { |
| 5295 | None |
| 5296 | }; |
| 5297 | // `SMOOTH_PRED..PAETH_PRED` (9..=12) chroma is a separate round-2 gap |
| 5298 | // from this lane's directional chroma: a corner block with neither |
| 5299 | // `above` nor `left` (both `Edges::build`'s no-neighbour fallback, |
| 5300 | // 127/129) fed `SMOOTH_PRED` produces the wrong pixel there (worst delta |
| 5301 | // 78 against ffmpeg, traced 2026-08-27) -- an existing, un-lane-touched |
| 5302 | // bug in that fallback this lane's own scope does not cover. Keep it |
| 5303 | // refused here rather than ship a silently-wrong decode. |
| 5304 | if (9..=12).contains(&uv_mode) { |
| 5305 | SMOOTH_UV_HITS.with(|c| c.set(c.get() + 1)); |
| 5306 | } |
| 5307 | // `get_uv_mode` (spec 9.3): `UV_CFL_PRED` predicts as `DC_PRED` for the |
| 5308 | // angle-delta question -- libaom's own `read_intra_frame_mode_info` reads |
| 5309 | // `angle_delta_uv` off `get_uv_mode(uv_mode)`, never off `uv_mode` raw, |
| 5310 | // so a CFL block (uv_mode==13, outside `V_PRED..=D67_PRED`) already takes |
| 5311 | // the `else` branch below and never reads one either way. |
| 5312 | let angle_delta_uv = if (V_PRED..=D67_PRED).contains(&uv_mode) { |
| 5313 | DIRECTIONAL_UV_HITS.with(|c| c.set(c.get() + 1)); |
| 5314 | read_angle_delta(dec, &mut cdfs.angle_delta[uv_mode - V_PRED]) |
| 5315 | } else { |
| 5316 | 0 |
| 5317 | }; |
| 5318 | istep!("angle_uv", angle_delta_uv); |
| 5319 | // `read_palette_mode_info` (spec 5.11.13, libaom decodemv.c:567, called |
| 5320 | // at :840 right after `xd->cfl.store_y` and before `read_filter_intra_mode_info`): |
| 5321 | // gated by `av1_allow_palette` (blockd.h -- size bound only, |
| 5322 | // [`palette_bsize_ctx`]). `palette_mode_ctx` comes from the caller |
| 5323 | // (`palette` param, `None` refuses unconditionally at ctx 0 -- the old |
| 5324 | // behaviour, bit-identical since a hardcoded ctx-0 read and a real one |
| 5325 | // read the same CDF row whenever every neighbour really is non-palette). |
| 5326 | // `palette_uv_mode_ctx` is this block's own just-decided Y palette use, |
| 5327 | // per `read_palette_mode_info`'s own `pmi->palette_size[0] > 0` (not a |
| 5328 | // neighbour lookup at all), so it is exact even at the excluded call |
| 5329 | // sites once Y decodes real syntax there too. |
| 5330 | // |
| 5331 | // lane-palette r7: libaom's `decode_color_map_tokens` is NOT called |
| 5332 | // inline from `read_palette_mode_info` -- it runs later, from |
| 5333 | // `parse_decode_block` (`decodeframe.c:1135`, `av1_visit_palette`), |
| 5334 | // after the WHOLE mode-info read for the block, including UV palette |
| 5335 | // mode_info (`read_palette_mode_info`'s own second half, right below) |
| 5336 | // and `read_filter_intra_mode_info`. So this branch now only reads |
| 5337 | // mode/size/colours for both planes (deferring the two colour-index-map |
| 5338 | // decodes to after `filter_intra`, below) -- r6's range trace pinned |
| 5339 | // this exact ordering bug (`compare-range-not-tell`: ranges matched |
| 5340 | // bit-for-bit through the Y colours, diverged only at the old inline |
| 5341 | // `decode_color_index_map` call). |
| 5342 | let mut palette_y_pending: Option<(usize, [u16; 8])> = None; |
| 5343 | let mut palette_uv_pending: Option<(usize, [u16; 8], [u16; 8])> = None; |
| 5344 | if allow_screen_content_tools |
| 5345 | && let Some(bsize_ctx) = palette_bsize_ctx(side) |
| 5346 | { |
| 5347 | let (mode_ctx, cache) = palette.unwrap_or((0, &[])); |
| 5348 | if mode == DC_PRED { |
| 5349 | if trace { |
| 5350 | let (rng, _) = dec.debug_state(); |
| 5351 | eprintln!( |
| 5352 | "TRACE pre_palette_y_mode bsize_ctx={bsize_ctx} mode_ctx={mode_ctx} rng={rng}" |
| 5353 | ); |
| 5354 | } |
| 5355 | } |
| 5356 | let use_palette_y = |
| 5357 | mode == DC_PRED && dec.symbol(&mut cdfs.palette_y_mode[bsize_ctx][mode_ctx]) != 0; |
| 5358 | if mode == DC_PRED && trace { |
| 5359 | let (rng, _) = dec.debug_state(); |
| 5360 | eprintln!("TRACE palette_y_mode value={} rng={rng}", use_palette_y as i32); |
| 5361 | } |
| 5362 | if use_palette_y { |
| 5363 | if palette.is_none() { |
| 5364 | return Err(unsupported( |
| 5365 | "a block that actually uses a palette (Y) -- reconstruction is out of scope", |
| 5366 | )); |
| 5367 | } |
| 5368 | let n = 2 + dec.symbol(&mut cdfs.palette_y_size[bsize_ctx]); |
| 5369 | if trace { |
| 5370 | let (rng, _) = dec.debug_state(); |
| 5371 | eprintln!("TRACE palette_y_size n={n} rng={rng}"); |
| 5372 | } |
| 5373 | let colors = read_palette_colors_y(dec, n, cache); |
| 5374 | if trace { |
| 5375 | let (rng, _) = dec.debug_state(); |
| 5376 | eprintln!("TRACE palette_y_colors colors={:?} rng={rng}", &colors[..n]); |
| 5377 | } |
| 5378 | palette_y_pending = Some((n, colors)); |
| 5379 | } |
| 5380 | // `read_palette_mode_info`'s UV half (decodemv.c:588): read |
| 5381 | // unconditionally at this bit position whenever `uv_mode == |
| 5382 | // UV_DC_PRED`, regardless of whether Y just fired -- this decoder's |
| 5383 | // scope always reconstructs chroma alongside luma (`decode_block` |
| 5384 | // always calls `u.reconstruct`/`v.reconstruct`), so `is_chroma_ref` |
| 5385 | // is always true here and does not need threading in. |
| 5386 | let palette_uv_mode_ctx = usize::from(use_palette_y); |
| 5387 | if uv_mode == DC_PRED && dec.symbol(&mut cdfs.palette_uv_mode[palette_uv_mode_ctx]) != 0 { |
| 5388 | let n = 2 + dec.symbol(&mut cdfs.palette_uv_size[bsize_ctx]); |
| 5389 | let (u_colors, v_colors) = read_palette_colors_uv(dec, n, palette_uv_cache); |
| 5390 | palette_uv_pending = Some((n, u_colors, v_colors)); |
| 5391 | } |
| 5392 | } |
| 5393 | // `read_filter_intra_mode_info` (spec 5.11.14, libaom decodemv.c): a |
| 5394 | // `use_filter_intra` symbol this decoder never read at all until |
| 5395 | // lane-av1real r10 -- silently skipping it left every bit past a |
| 5396 | // DC_PRED block at <=32x32 read one symbol short of libaom for the rest |
| 5397 | // of the tile (the pinned CfL stream's actual bug: partition/skip/mode |
| 5398 | // matched bit-exact, then `eob_pt` read 5 instead of 6, because this |
| 5399 | // symbol's bits were never consumed). |
| 5400 | let mut filter_intra = None; |
| 5401 | if mode == DC_PRED |
| 5402 | && enable_filter_intra |
| 5403 | && let Some(class) = filter_intra_size_class(side) |
| 5404 | { |
| 5405 | let use_filter_intra = dec.symbol(&mut cdfs.filter_intra[class]) != 0; |
| 5406 | if trace { |
| 5407 | eprintln!( |
| 5408 | "TRACE use_filter_intra side={side} value={}", |
| 5409 | use_filter_intra as i32 |
| 5410 | ); |
| 5411 | } |
| 5412 | istep!("use_filter_intra", use_filter_intra as i32); |
| 5413 | if use_filter_intra { |
| 5414 | FILTER_INTRA_HITS.with(|c| c.set(c.get() + 1)); |
| 5415 | let fi_mode = dec.symbol(&mut cdfs.filter_intra_mode); |
| 5416 | if trace { |
| 5417 | eprintln!("TRACE filter_intra_mode value={fi_mode}"); |
| 5418 | } |
| 5419 | istep!("filter_intra_mode", fi_mode as i32); |
| 5420 | filter_intra = Some(fi_mode); |
| 5421 | } |
| 5422 | } |
| 5423 | // `av1_visit_palette` (decoder.c:234): plane 0 (Y) then plane 1 (chroma, |
| 5424 | // shared U/V map) -- the same order libaom's own loop visits them in. |
| 5425 | let palette_y = palette_y_pending.map(|(n, colors)| { |
| 5426 | let map = decode_color_index_map(dec, cdfs, n, side, false); |
| 5427 | PALETTE_HITS.with(|c| c.set(c.get() + 1)); |
| 5428 | if trace { |
| 5429 | eprintln!("TRACE palette_y size={n} colors={:?}", &colors[..n]); |
| 5430 | } |
| 5431 | PaletteY { size: n, colors, map } |
| 5432 | }); |
| 5433 | let palette_uv = palette_uv_pending.map(|(n, u_colors, v_colors)| { |
| 5434 | let map = decode_color_index_map(dec, cdfs, n, side / 2, true); |
| 5435 | PALETTE_UV_HITS.with(|c| c.set(c.get() + 1)); |
| 5436 | if trace { |
| 5437 | eprintln!( |
| 5438 | "TRACE palette_uv size={n} u_colors={:?} v_colors={:?}", |
| 5439 | &u_colors[..n], |
| 5440 | &v_colors[..n] |
| 5441 | ); |
| 5442 | } |
| 5443 | PaletteUv { size: n, u_colors, v_colors, map } |
| 5444 | }); |
| 5445 | Ok(( |
| 5446 | skip, |
| 5447 | mode, |
| 5448 | angle_delta_y, |
| 5449 | uv_mode, |
| 5450 | angle_delta_uv, |
| 5451 | alpha, |
| 5452 | filter_intra, |
| 5453 | palette_y, |
| 5454 | palette_uv, |
| 5455 | )) |
| 5456 | } |
| 5457 | |
| 5458 | /// `UV_CFL_PRED` (spec 6.10.19's chroma intra mode enum): the fourteenth |
| 5459 | /// entry of [`crate::cdf::UV_MODE_CFL`], one past the thirteen ordinary |
| 5460 | /// intra modes. |
| 5461 | const UV_CFL_PRED: usize = 13; |
| 5462 | |
| 5463 | /// `read_cfl_alphas` (libaom decodemv.c): the joint sign symbol (8-ary, spec |
| 5464 | /// 5.11.45's `cfl_alpha_signs`), then each plane's alpha magnitude symbol |
| 5465 | /// (16-ary, `cfl_alpha_u`/`cfl_alpha_v`) where its sign is nonzero -- context |
| 5466 | /// indices `CFL_CONTEXT_U`/`CFL_CONTEXT_V` (libaom cfl.h) derived from the |
| 5467 | /// joint sign the same way. Returns each plane's final signed `alpha_q3` |
| 5468 | /// (`cfl_idx_to_alpha`, cfl.c): magnitude `idx + 1`, sign-zero collapsing to 0. |
| 5469 | fn read_cfl_alphas(dec: &mut SymbolDecoder, cdfs: &mut Cdfs) -> (i32, i32) { |
| 5470 | let joint_sign = dec.symbol(&mut cdfs.cfl_sign) as i32; |
| 5471 | let sign_u = ((joint_sign + 1) * 11) >> 5; |
| 5472 | let sign_v = (joint_sign + 1) - 3 * sign_u; |
| 5473 | let alpha_u = if sign_u == 0 { |
| 5474 | 0 |
| 5475 | } else { |
| 5476 | let ctx_u = (joint_sign + 1 - 3) as usize; |
| 5477 | let mag = dec.symbol(&mut cdfs.cfl_alpha[ctx_u]) as i32 + 1; |
| 5478 | if sign_u == 2 { mag } else { -mag } |
| 5479 | }; |
| 5480 | let alpha_v = if sign_v == 0 { |
| 5481 | 0 |
| 5482 | } else { |
| 5483 | let ctx_v = (sign_v * 3 + sign_u - 3) as usize; |
| 5484 | let mag = dec.symbol(&mut cdfs.cfl_alpha[ctx_v]) as i32 + 1; |
| 5485 | if sign_v == 2 { mag } else { -mag } |
| 5486 | }; |
| 5487 | (alpha_u, alpha_v) |
| 5488 | } |
| 5489 | |
| 5490 | /// `cfl_luma_subsampling_420_lbd_c` + `subtract_average_c` (libaom cfl.c): |
| 5491 | /// the reconstructed luma samples under a `side`-square block, subsampled |
| 5492 | /// 4:2:0 to Q3 (`(a+b+c+d) << 1`, each 2x2 group), then block-average |
| 5493 | /// subtracted (`round_offset = num_pel/2`, right-shifted by `log2(num_pel)`) |
| 5494 | /// to give the AC values [`cfl_scaled`] scales by alpha. |
| 5495 | fn cfl_ac_q3(y: &PlaneBuf, px: usize, py: usize, side: usize) -> Vec<i32> { |
| 5496 | let cside = side / 2; |
| 5497 | let mut ac = vec![0i32; cside * cside]; |
| 5498 | let mut sum = 0i32; |
| 5499 | for cy in 0..cside { |
| 5500 | for cx in 0..cside { |
| 5501 | let (lx, ly) = (px + cx * 2, py + cy * 2); |
| 5502 | let q3 = (i32::from(y.data[ly * y.width + lx]) |
| 5503 | + i32::from(y.data[ly * y.width + lx + 1]) |
| 5504 | + i32::from(y.data[(ly + 1) * y.width + lx]) |
| 5505 | + i32::from(y.data[(ly + 1) * y.width + lx + 1])) |
| 5506 | << 1; |
| 5507 | ac[cy * cside + cx] = q3; |
| 5508 | sum += q3; |
| 5509 | } |
| 5510 | } |
| 5511 | let num_pel = (cside * cside) as i32; |
| 5512 | let avg = (sum + num_pel / 2) >> num_pel.trailing_zeros(); |
| 5513 | ac.iter_mut().for_each(|v| *v -= avg); |
| 5514 | ac |
| 5515 | } |
| 5516 | |
| 5517 | /// `get_scaled_luma_q0` (libaom cfl.h): `alpha_q3 * ac_q3`, rounded from Q6 |
| 5518 | /// back to whole samples with `ROUND_POWER_OF_TWO_SIGNED` (round-to-nearest, |
| 5519 | /// ties away from zero on the shifted-out sign). |
| 5520 | fn cfl_scaled(alpha_q3: i32, ac_q3: i32) -> i32 { |
| 5521 | let v = alpha_q3 * ac_q3; |
| 5522 | if v >= 0 { |
| 5523 | (v + 32) >> 6 |
| 5524 | } else { |
| 5525 | -((-v + 32) >> 6) |
| 5526 | } |
| 5527 | } |
| 5528 | |
| 5529 | /// One plane's reconstruction buffer, `width * height` samples, and the |
| 5530 | /// prediction/residual reads and writes into it — the decode-side twin of |
| 5531 | /// [`crate::encode`]'s private `Plane`, whose `edges` this mirrors exactly |
| 5532 | /// (own-edge and reach clamps both), passing what it gathers straight to |
| 5533 | /// [`crate::intra::predict`] instead of hand-rolling `DC_PRED` alone. |
| 5534 | /// lane-tiny r4: raw padded-plane dump for filter-stage bisection, written |
| 5535 | /// only when `var` names a path. Same byte shape as `EC_AV1_PREFILT_DUMP`. |
| 5536 | fn dump_stage(var: &str, y: &PlaneBuf, u: &PlaneBuf, v: &PlaneBuf) { |
| 5537 | use std::io::Write; |
| 5538 | if let Ok(path) = std::env::var(var) |
| 5539 | && let Ok(mut f) = std::fs::File::create(format!("{path}.f0")) |
| 5540 | { |
| 5541 | for p in [y, u, v] { |
| 5542 | let narrow: Vec<u8> = p.data.iter().map(|&s| s as u8).collect(); |
| 5543 | let _ = f.write_all(&narrow); |
| 5544 | } |
| 5545 | } |
| 5546 | } |
| 5547 | |
| 5548 | #[derive(Clone)] |
| 5549 | struct PlaneBuf { |
| 5550 | data: Vec<u16>, |
| 5551 | width: usize, |
| 5552 | height: usize, |
| 5553 | /// The frame's true, decodable extent in this plane's own units — past |
| 5554 | /// this, samples are the padded coding surface's invented tail, never a |
| 5555 | /// real decoder's edge or reach reads. |
| 5556 | true_width: usize, |
| 5557 | true_height: usize, |
| 5558 | /// This plane's own tile's top-left pixel origin (spec: intra prediction |
| 5559 | /// never reaches across a tile boundary even though an earlier-decoded |
| 5560 | /// tile's real pixels sit right there in the shared buffer) — set via |
| 5561 | /// [`Self::set_tile_origin`] before each tile's own superblock walk, |
| 5562 | /// `0` for the single-tile case. lane-tiles r2: only the left/top bound |
| 5563 | /// is threaded (the two-tile column-split stage 1 fixture's tile always |
| 5564 | /// ends at the frame's own right/bottom edge); a non-last tile's own |
| 5565 | /// right/bottom reach bound is still open, tracked in the report. |
| 5566 | tile_x0: usize, |
| 5567 | tile_y0: usize, |
| 5568 | /// lane-tiles r5: this plane's own tile's right/bottom pixel bound |
| 5569 | /// (exclusive) -- a non-last tile column/row's own reach must stop at |
| 5570 | /// its own SB-aligned tile boundary. `width`/`height` (this plane's |
| 5571 | /// full padded extent) for the single-tile/last-tile case, matching the |
| 5572 | /// old unclipped behaviour exactly. |
| 5573 | tile_x1: usize, |
| 5574 | tile_y1: usize, |
| 5575 | } |
| 5576 | |
| 5577 | /// One entry per `MV_REFERENCE_FRAME` (index 0/1 unused -- `NONE`/ |
| 5578 | /// `INTRA_FRAME`/`LAST_FRAME` all resolve elsewhere), `Some` when this |
| 5579 | /// frame's own `ref_frame_idx` names a DPB slot that has a picture in it |
| 5580 | /// (lane-av1refs: the same "empty slot" case `GOLDEN_FRAME` already |
| 5581 | /// refused by name, now generic across `LAST2`/`LAST3`/`GOLDEN`/`BWDREF`/ |
| 5582 | /// `ALTREF2`/`ALTREF`). |
| 5583 | type RefSlots<'a> = [Option<(&'a PlaneBuf, &'a PlaneBuf, &'a PlaneBuf)>; 8]; |
| 5584 | |
| 5585 | impl PlaneBuf { |
| 5586 | /// Sets this plane's own tile pixel origin ([`Self::tile_x0`]/ |
| 5587 | /// [`Self::tile_y0`]) before that tile's own superblock walk. `x0`/`y0` |
| 5588 | /// are already in this plane's own units (luma pixels for `y`, chroma |
| 5589 | /// pixels — halved — for `u`/`v`). |
| 5590 | fn set_tile_origin(&mut self, x0: usize, y0: usize, x1: usize, y1: usize) { |
| 5591 | self.tile_x0 = x0; |
| 5592 | self.tile_y0 = y0; |
| 5593 | self.tile_x1 = x1; |
| 5594 | self.tile_y1 = y1; |
| 5595 | } |
| 5596 | |
| 5597 | fn edges( |
| 5598 | &self, |
| 5599 | x: usize, |
| 5600 | y: usize, |
| 5601 | side: usize, |
| 5602 | reach: Reach, |
| 5603 | ) -> (Option<Vec<u16>>, Option<Vec<u16>>, Option<u16>) { |
| 5604 | let own_across = x + side.min(self.true_width.saturating_sub(x)); |
| 5605 | let across = if reach.above_right { |
| 5606 | own_across + side.min(self.true_width.saturating_sub(own_across)) |
| 5607 | } else { |
| 5608 | own_across |
| 5609 | } |
| 5610 | .min(self.width) |
| 5611 | .min(self.tile_x1); |
| 5612 | let own_down = y + side.min(self.true_height.saturating_sub(y)); |
| 5613 | let down = if reach.below_left { |
| 5614 | own_down + side.min(self.true_height.saturating_sub(own_down)) |
| 5615 | } else { |
| 5616 | own_down |
| 5617 | } |
| 5618 | .min(self.height) |
| 5619 | .min(self.tile_y1); |
| 5620 | let above = (y > self.tile_y0 && across > x) |
| 5621 | .then(|| self.data[(y - 1) * self.width + x..][..across - x].to_vec()); |
| 5622 | let left = (x > self.tile_x0 && down > y).then(|| { |
| 5623 | (y..down) |
| 5624 | .map(|row| self.data[row * self.width + x - 1]) |
| 5625 | .collect::<Vec<_>>() |
| 5626 | }); |
| 5627 | let corner = |
| 5628 | (x > self.tile_x0 && y > self.tile_y0).then(|| self.data[(y - 1) * self.width + x - 1]); |
| 5629 | (above, left, corner) |
| 5630 | } |
| 5631 | |
| 5632 | /// [`Self::edges`] for a true `bw`x`bh` block (lane-intradisp r1): |
| 5633 | /// the above/left reach each extend by their OWN axis (`bw` across, |
| 5634 | /// `bh` down), not one shared `side`. |
| 5635 | fn edges_rect( |
| 5636 | &self, |
| 5637 | x: usize, |
| 5638 | y: usize, |
| 5639 | bw: usize, |
| 5640 | bh: usize, |
| 5641 | reach: Reach, |
| 5642 | ) -> (Option<Vec<u16>>, Option<Vec<u16>>, Option<u16>) { |
| 5643 | let own_across = x + bw.min(self.true_width.saturating_sub(x)); |
| 5644 | let across = if reach.above_right { |
| 5645 | own_across + bw.min(self.true_width.saturating_sub(own_across)) |
| 5646 | } else { |
| 5647 | own_across |
| 5648 | } |
| 5649 | .min(self.width) |
| 5650 | .min(self.tile_x1); |
| 5651 | let own_down = y + bh.min(self.true_height.saturating_sub(y)); |
| 5652 | let down = if reach.below_left { |
| 5653 | own_down + bh.min(self.true_height.saturating_sub(own_down)) |
| 5654 | } else { |
| 5655 | own_down |
| 5656 | } |
| 5657 | .min(self.height) |
| 5658 | .min(self.tile_y1); |
| 5659 | let above = (y > self.tile_y0 && across > x) |
| 5660 | .then(|| self.data[(y - 1) * self.width + x..][..across - x].to_vec()); |
| 5661 | let left = (x > self.tile_x0 && down > y).then(|| { |
| 5662 | (y..down) |
| 5663 | .map(|row| self.data[row * self.width + x - 1]) |
| 5664 | .collect::<Vec<_>>() |
| 5665 | }); |
| 5666 | let corner = |
| 5667 | (x > self.tile_x0 && y > self.tile_y0).then(|| self.data[(y - 1) * self.width + x - 1]); |
| 5668 | (above, left, corner) |
| 5669 | } |
| 5670 | |
| 5671 | /// [`Self::reconstruct`] for a true `bw`x`bh` block (lane-intradisp r1), |
| 5672 | /// via [`Self::edges_rect`] and [`crate::intra::predict`]'s own |
| 5673 | /// already-rect-capable `(bw, bh)` pair. |
| 5674 | #[allow(clippy::too_many_arguments)] |
| 5675 | fn reconstruct_rect( |
| 5676 | &mut self, |
| 5677 | x: usize, |
| 5678 | y: usize, |
| 5679 | bw: usize, |
| 5680 | bh: usize, |
| 5681 | mode: usize, |
| 5682 | angle_delta: i32, |
| 5683 | reach: Reach, |
| 5684 | residual: &[i32], |
| 5685 | cfl: Option<(i32, &[i32])>, |
| 5686 | filter_intra: Option<usize>, |
| 5687 | smooth_neighbor: bool, |
| 5688 | ) { |
| 5689 | let (above, left, corner) = self.edges_rect(x, y, bw, bh, reach); |
| 5690 | if std::env::var_os("EC_DEBUG_EDGES").is_some() { |
| 5691 | eprintln!( |
| 5692 | "EDGES x={x} y={y} bw={bw} bh={bh} mode={mode} above={:?} left_len={:?} left0={:?} corner={corner:?} tx0={} ty0={} truew={} trueh={}", |
| 5693 | above.as_ref().map(|a| (a.len(), a[0])), |
| 5694 | left.as_ref().map(|l| l.len()), |
| 5695 | left.as_ref().map(|l| l[0]), |
| 5696 | self.tile_x0, self.tile_y0, self.true_width, self.true_height |
| 5697 | ); |
| 5698 | } |
| 5699 | let mut prediction = vec![0u16; bw * bh]; |
| 5700 | if let Some(fi_mode) = filter_intra { |
| 5701 | crate::intra::predict_filter_intra( |
| 5702 | fi_mode, |
| 5703 | above.as_deref(), |
| 5704 | left.as_deref(), |
| 5705 | corner, |
| 5706 | bw, |
| 5707 | bh, |
| 5708 | &mut prediction, |
| 5709 | ); |
| 5710 | } else { |
| 5711 | let enable_edge_filter = ENABLE_EDGE_FILTER.with(std::cell::Cell::get); |
| 5712 | predict( |
| 5713 | mode as u8, |
| 5714 | angle_delta, |
| 5715 | above.as_deref(), |
| 5716 | left.as_deref(), |
| 5717 | corner, |
| 5718 | bw, |
| 5719 | bh, |
| 5720 | enable_edge_filter, |
| 5721 | smooth_neighbor, |
| 5722 | &mut prediction, |
| 5723 | ); |
| 5724 | } |
| 5725 | for row in 0..bh { |
| 5726 | for col in 0..bw { |
| 5727 | let idx = row * bw + col; |
| 5728 | let mut base = i32::from(prediction[idx]); |
| 5729 | if let Some((alpha_q3, ac_q3)) = cfl { |
| 5730 | base = (base + cfl_scaled(alpha_q3, ac_q3[idx])).clamp(0, sample_max()); |
| 5731 | } |
| 5732 | let sample = (base + residual[idx]).clamp(0, sample_max()) as u16; |
| 5733 | self.data[(y + row) * self.width + x + col] = sample; |
| 5734 | } |
| 5735 | } |
| 5736 | } |
| 5737 | |
| 5738 | /// Predicts under `mode` then adds `residual` (side*side, raster), |
| 5739 | /// writing the clamped reconstruction back into the plane at `(x, y)`. |
| 5740 | /// `cfl`, when `Some((alpha_q3, ac_q3))` (a `UV_CFL_PRED` chroma block), |
| 5741 | /// nudges the prediction by [`cfl_scaled`]'s per-sample amount before the |
| 5742 | /// residual is added — `av1_cfl_predict_block`'s own clip-then-add-residual |
| 5743 | /// order (cfl.c). |
| 5744 | fn reconstruct( |
| 5745 | &mut self, |
| 5746 | x: usize, |
| 5747 | y: usize, |
| 5748 | side: usize, |
| 5749 | mode: usize, |
| 5750 | angle_delta: i32, |
| 5751 | reach: Reach, |
| 5752 | residual: &[i32], |
| 5753 | cfl: Option<(i32, &[i32])>, |
| 5754 | filter_intra: Option<usize>, |
| 5755 | // `smooth_neighbor` (spec `get_intra_edge_filter_type` / |
| 5756 | // libaom `get_filt_type`): true when the block's above OR left |
| 5757 | // neighbour's mode is `SMOOTH_PRED..=SMOOTH_H_PRED` (never `PAETH`). |
| 5758 | // Callers pass their own plane's neighbour-mode check |
| 5759 | // ([`is_smooth_mode`]) — chroma's is still hardcoded `false` by its |
| 5760 | // callers until a smooth/paeth `uv_mode` can reach this call at all |
| 5761 | // (still refused in `read_intra_mode`/`read_intra_mode_rect`), which |
| 5762 | // keeps `false` exact for chroma without this fn needing to know why. |
| 5763 | smooth_neighbor: bool, |
| 5764 | ) { |
| 5765 | // A palette block's own [`PALETTE_PRED`] override (lane-palette r2) |
| 5766 | // takes priority over `predict()`/`predict_filter_intra` entirely -- |
| 5767 | // palette prediction needs no edge pixels at all. |
| 5768 | let prediction = if let Some(buf) = PALETTE_PRED.with(|c| c.borrow_mut().take()) { |
| 5769 | buf |
| 5770 | } else { |
| 5771 | let (above, left, corner) = self.edges(x, y, side, reach); |
| 5772 | let mut prediction = vec![0u16; side * side]; |
| 5773 | if let Some(fi_mode) = filter_intra { |
| 5774 | crate::intra::predict_filter_intra( |
| 5775 | fi_mode, |
| 5776 | above.as_deref(), |
| 5777 | left.as_deref(), |
| 5778 | corner, |
| 5779 | side, |
| 5780 | side, |
| 5781 | &mut prediction, |
| 5782 | ); |
| 5783 | } else { |
| 5784 | // `enable_intra_edge_filter` is the sequence header's own flag |
| 5785 | // (`ENABLE_EDGE_FILTER`, set once per `decode_key_frame_tile_with_cdfs` |
| 5786 | // call, never per-block) -- threading it through every |
| 5787 | // `read_plane`/`decode_block`/`decode_leaf8` call would touch two |
| 5788 | // dozen sites for a value that never changes mid-decode, so it |
| 5789 | // rides a thread-local instead (this decoder is single-threaded |
| 5790 | // per stream). |
| 5791 | let enable_edge_filter = ENABLE_EDGE_FILTER.with(std::cell::Cell::get); |
| 5792 | predict( |
| 5793 | mode as u8, |
| 5794 | angle_delta, |
| 5795 | above.as_deref(), |
| 5796 | left.as_deref(), |
| 5797 | corner, |
| 5798 | side, |
| 5799 | side, |
| 5800 | enable_edge_filter, |
| 5801 | smooth_neighbor, |
| 5802 | &mut prediction, |
| 5803 | ); |
| 5804 | } |
| 5805 | prediction |
| 5806 | }; |
| 5807 | for row in 0..side { |
| 5808 | for col in 0..side { |
| 5809 | let idx = row * side + col; |
| 5810 | let mut base = i32::from(prediction[idx]); |
| 5811 | if let Some((alpha_q3, ac_q3)) = cfl { |
| 5812 | base = (base + cfl_scaled(alpha_q3, ac_q3[idx])).clamp(0, sample_max()); |
| 5813 | } |
| 5814 | let sample = (base + residual[idx]).clamp(0, sample_max()) as u16; |
| 5815 | self.data[(y + row) * self.width + x + col] = sample; |
| 5816 | } |
| 5817 | } |
| 5818 | } |
| 5819 | } |
| 5820 | |
| 5821 | /// Reads one plane's transform block, reconstructs it into `plane` at |
| 5822 | /// `(x, y)`/`side`, and records what it leaves behind in `neighbours`. |
| 5823 | #[allow(clippy::too_many_arguments)] |
| 5824 | fn read_plane( |
| 5825 | dec: &mut SymbolDecoder, |
| 5826 | cdfs: &mut Cdfs, |
| 5827 | set: TxbSet, |
| 5828 | scan: &[u16], |
| 5829 | plane_idx: usize, |
| 5830 | around: (bool, bool, i32), |
| 5831 | // The block's own luma mode — [`crate::tile::write_block_planes`] passes |
| 5832 | // it for every plane's `txb` lookup (it is what an intra transform type's |
| 5833 | // CDF is indexed by, spec 9.3), never the plane's own predicted mode. |
| 5834 | tx_mode: usize, |
| 5835 | predict_mode: usize, |
| 5836 | angle_delta: i32, |
| 5837 | reach: Reach, |
| 5838 | plane: &mut PlaneBuf, |
| 5839 | x: usize, |
| 5840 | y: usize, |
| 5841 | side: usize, |
| 5842 | tx_side: usize, |
| 5843 | base_q_idx: u8, |
| 5844 | cfl: Option<(i32, &[i32])>, |
| 5845 | filter_intra: Option<usize>, |
| 5846 | // Only meaningful for `plane_idx == 0`: `Some` when this transform unit is |
| 5847 | // smaller than its own block (spec `get_txb_ctx_general`'s |
| 5848 | // `plane_bsize != tx_size` branch), carrying the already-looked-up |
| 5849 | // `SKIP_CONTEXTS` value; `None` everywhere else (a lone luma TU, or any |
| 5850 | // chroma plane), where `txb_skip_ctx` is 0 or the usual OR-of-neighbours |
| 5851 | // formula below. |
| 5852 | luma_skip_ctx: Option<usize>, |
| 5853 | // See [`PlaneBuf::reconstruct`]'s own doc: caller's neighbour-smooth check. |
| 5854 | smooth_neighbor: bool, |
| 5855 | ) -> Result<Vec<i32>> { |
| 5856 | let skip_ctx = if plane_idx == 0 { |
| 5857 | luma_skip_ctx.unwrap_or(0) |
| 5858 | } else { |
| 5859 | usize::from(around.0) + usize::from(around.1) |
| 5860 | }; |
| 5861 | // spec 5.11.47/libaom `av1_read_tx_type`: a filter-intra luma block's |
| 5862 | // `intra_ext_tx_cdf` row is indexed by `fimode_to_intradir[filter_intra_mode]`, |
| 5863 | // not by the block's ordinary `mode` (which is always DC_PRED whenever |
| 5864 | // filter-intra is on) -- caller passes the raw filter-intra mode 0..4 in |
| 5865 | // `filter_intra`, only meaningful for `plane_idx == 0` (chroma has no |
| 5866 | // filter-intra in AV1). |
| 5867 | const FIMODE_TO_INTRADIR: [usize; 5] = [DC_PRED, V_PRED, H_PRED, D157_PRED, DC_PRED]; |
| 5868 | let tx_mode = if plane_idx == 0 { |
| 5869 | filter_intra.map_or(tx_mode, |fi| FIMODE_TO_INTRADIR[fi]) |
| 5870 | } else { |
| 5871 | tx_mode |
| 5872 | }; |
| 5873 | let mut coding = cdfs.txb(set, tx_mode); |
| 5874 | // Luma's `default_tx_type` is only a fallback for the sizes whose |
| 5875 | // `TxbSet` carries no symbol at all (32-point and up), which the spec |
| 5876 | // fixes at `DCT_DCT` regardless of mode; a chroma plane's `tx_type` is |
| 5877 | // *never* its own coded symbol (`coding.tx_type` is always `None` for |
| 5878 | // every `TxbSet::Chroma*`), so it would take `Intra_Mode_To_Tx_Type` of |
| 5879 | // the plane's own predicted mode (`default_intra_tx_type`) -- except |
| 5880 | // `av1_get_ext_tx_set_type` (`blockd.h`) resolves chroma at `tx_size_sqr |
| 5881 | // >= TX_32X32` to `EXT_TX_SET_DCTONLY`, which forces the result back to |
| 5882 | // `DCT_DCT` no matter what the mode-indexed table said (`TxbSet::Chroma8` |
| 5883 | // and `Chroma16` never hit this — every value `default_intra_tx_type` can |
| 5884 | // produce is already a member of both `TX_SET_INTRA_1`/`_2`, so nothing |
| 5885 | // narrows there — only `Chroma32` and up do). |
| 5886 | let default_tx_type = if plane_idx == 0 || side >= 32 { |
| 5887 | TxType::DctDct |
| 5888 | } else { |
| 5889 | default_intra_tx_type(predict_mode as u8) |
| 5890 | }; |
| 5891 | let (grid, tx_type) = read_coeffs( |
| 5892 | dec, |
| 5893 | &mut coding, |
| 5894 | scan, |
| 5895 | skip_ctx, |
| 5896 | dc_sign_ctx(around.2), |
| 5897 | default_tx_type, |
| 5898 | None, |
| 5899 | )?; |
| 5900 | // A 64x64 luma block's transform covers the whole 64x64 area, but only its |
| 5901 | // top-left 32x32 of frequencies are coded (spec 5.11.40); the rest of the |
| 5902 | // dequantized grid stays zero, which `inverse_transform_2d`'s own `< 32` |
| 5903 | // guard also assumes. |
| 5904 | let levels = if tx_side == side { |
| 5905 | grid |
| 5906 | } else { |
| 5907 | // `grid` is `tx_side x tx_side` (the coded corner); `dequant_and_inverse` |
| 5908 | // wants the true `side x side` transform, with dqDenom (spec 7.12.3) |
| 5909 | // keyed by that true size, not the coded corner's -- so the corner |
| 5910 | // goes into the top-left of a `side`-sized grid, not the reverse. |
| 5911 | let mut full = vec![0i32; side * side]; |
| 5912 | for row in 0..tx_side { |
| 5913 | full[row * side..][..tx_side].copy_from_slice(&grid[row * tx_side..][..tx_side]); |
| 5914 | } |
| 5915 | full |
| 5916 | }; |
| 5917 | let (dc_delta, ac_delta) = plane_q_delta(plane_idx); |
| 5918 | let residual = dequant_and_inverse_typed(&levels, side, bit_depth(), block_q_idx(), dc_delta, ac_delta, tx_type); |
| 5919 | if std::env::var_os("EC_AV1_TRACE").is_some() { |
| 5920 | eprintln!( |
| 5921 | "TRACE dequant plane={plane_idx} base_q_idx={base_q_idx} tx_type={tx_type:?} side={side} levels={levels:?} residual={residual:?}", |
| 5922 | ); |
| 5923 | } |
| 5924 | plane.reconstruct( |
| 5925 | x, |
| 5926 | y, |
| 5927 | side, |
| 5928 | predict_mode, |
| 5929 | angle_delta, |
| 5930 | reach, |
| 5931 | &residual, |
| 5932 | cfl, |
| 5933 | filter_intra, |
| 5934 | smooth_neighbor, |
| 5935 | ); |
| 5936 | Ok(if tx_side == side { |
| 5937 | residual_grid_placeholder(&levels, side) |
| 5938 | } else { |
| 5939 | levels |
| 5940 | }) |
| 5941 | } |
| 5942 | |
| 5943 | /// `record`'s neighbour state only reads whether *the coded grid* (not the |
| 5944 | /// residual) has nonzero entries, so the levels this function already built |
| 5945 | /// are what it wants back; this helper exists only to name that clearly at |
| 5946 | /// the one call site where `tx_side == side`. |
| 5947 | fn residual_grid_placeholder(levels: &[i32], _tx_side: usize) -> Vec<i32> { |
| 5948 | levels.to_vec() |
| 5949 | } |
| 5950 | |
| 5951 | /// Decodes one whole-block coded superblock/quadrant/leaf: its mode, then its |
| 5952 | /// three planes, reconstructing them into `y`/`u`/`v` and updating |
| 5953 | /// `neighbours`. |
| 5954 | #[allow(clippy::too_many_arguments)] |
| 5955 | fn decode_block( |
| 5956 | dec: &mut SymbolDecoder, |
| 5957 | cdfs: &mut Cdfs, |
| 5958 | neighbours: &mut Neighbours, |
| 5959 | at: (usize, usize), |
| 5960 | side: usize, |
| 5961 | luma_set: TxbSet, |
| 5962 | chroma_set: TxbSet, |
| 5963 | luma_tx: usize, |
| 5964 | chroma_tx: usize, |
| 5965 | scans: (&[u16], &[u16]), |
| 5966 | cfl: bool, |
| 5967 | y: &mut PlaneBuf, |
| 5968 | u: &mut PlaneBuf, |
| 5969 | v: &mut PlaneBuf, |
| 5970 | base_q_idx: u8, |
| 5971 | enable_filter_intra: bool, |
| 5972 | allow_screen_content_tools: bool, |
| 5973 | allow_intrabc: bool, |
| 5974 | scan32: &[u16], |
| 5975 | scan16: &[u16], |
| 5976 | scan8: &[u16], |
| 5977 | scan4: &[u16], |
| 5978 | tx_select: bool, |
| 5979 | reduced_tx_set: bool, |
| 5980 | ) -> Result<()> { |
| 5981 | let (r, c) = at; |
| 5982 | let (px, py) = (c * SUB, r * SUB); |
| 5983 | let (palette_ctx, palette_cache) = neighbours.palette_ctx_and_cache(at); |
| 5984 | let palette_uv_cache = neighbours.palette_uv_cache(at); |
| 5985 | let (skip, mode, angle_delta_y, uv_mode, angle_delta_uv, alpha, filter_intra, palette_y, palette_uv) = |
| 5986 | read_intra_mode( |
| 5987 | dec, |
| 5988 | cdfs, |
| 5989 | neighbours.above_mode[c], |
| 5990 | neighbours.left_mode[r], |
| 5991 | cfl, |
| 5992 | side, |
| 5993 | enable_filter_intra, |
| 5994 | neighbours.skip_txfm_ctx(r * (SUB / MI), c * (SUB / MI)), |
| 5995 | allow_screen_content_tools, |
| 5996 | allow_intrabc, |
| 5997 | Some((palette_ctx, &palette_cache)), |
| 5998 | &palette_uv_cache, |
| 5999 | r * (SUB / MI), |
| 6000 | c * (SUB / MI), |
| 6001 | )?; |
| 6002 | let intrabc_dv = INTRABC_DV.with(|c| c.take()); |
| 6003 | // libaom `parse_decode_block`: an intrabc block is `inter_block_tx`, so |
| 6004 | // under `TX_MODE_SELECT` its transform size comes from the inter var-tx |
| 6005 | // partition tree (`read_var_tx_size`), a syntax element this decoder has |
| 6006 | // nowhere -- reading the intra `tx_depth` symbol instead would desync. |
| 6007 | if intrabc_dv.is_some() && tx_select && !skip { |
| 6008 | return Err(unsupported( |
| 6009 | "an intrabc block under TxMode::Select (its transform size is coded by the inter var-tx partition tree, which this decoder never reads)", |
| 6010 | )); |
| 6011 | } |
| 6012 | // `predict` panics outside `DC_PRED..=PAETH_PRED` (0..=12); `UV_CFL_PRED` |
| 6013 | // (13) predicts as `DC_PRED` with the [`cfl_scaled`] nudge carrying the |
| 6014 | // actual chroma-from-luma correlation, same as this decoder always did. |
| 6015 | let uv_predict_mode = if uv_mode == UV_CFL_PRED { |
| 6016 | DC_PRED |
| 6017 | } else { |
| 6018 | uv_mode |
| 6019 | }; |
| 6020 | let smooth_neighbor = |
| 6021 | is_smooth_mode(neighbours.above_mode[c]) || is_smooth_mode(neighbours.left_mode[r]); |
| 6022 | if smooth_neighbor { |
| 6023 | SMOOTH_LUMA_HITS.with(|c| c.set(c.get() + 1)); |
| 6024 | } |
| 6025 | let smooth_neighbor_uv = |
| 6026 | is_smooth_mode(neighbours.above_uv_mode[c]) || is_smooth_mode(neighbours.left_uv_mode[r]); |
| 6027 | if std::env::var_os("EC_AV1_TRACE").is_some() { |
| 6028 | eprintln!( |
| 6029 | "TRACE block px={px} py={py} side={side} mode={mode} uv_mode={uv_mode} \ |
| 6030 | angle_delta_uv={angle_delta_uv}" |
| 6031 | ); |
| 6032 | } |
| 6033 | let (mi_r, mi_c) = (r * (SUB / MI), c * (SUB / MI)); |
| 6034 | // `TxMode::Select`'s `tx_depth` (spec 5.11.16): read for every intra |
| 6035 | // block, skipped or not. `logical_tx` is what the loop filter's edge |
| 6036 | // lookup and the next block's own `tx_size_context` want (the *nominal* |
| 6037 | // transform size -- 64 even for the 64x64 corner-scanned case, spec |
| 6038 | // 5.11.40); `coeff_tx_side` is the actual coded coefficient grid's side, |
| 6039 | // which only differs from it at that one corner case. |
| 6040 | let (logical_tx, coeff_tx_side) = if tx_select { |
| 6041 | let resolved = read_tx_size(dec, cdfs, neighbours, (mi_r, mi_c), side, None); |
| 6042 | (resolved, if resolved == 64 { 32 } else { resolved }) |
| 6043 | } else { |
| 6044 | (side, luma_tx) |
| 6045 | }; |
| 6046 | if tx_select && logical_tx < side && uv_mode != DC_PRED { |
| 6047 | SQ_CHROMA_TX_HITS.with(|c| c.set(c.get() + 1)); |
| 6048 | } |
| 6049 | let luma_set = if tx_select { |
| 6050 | txbset_for(logical_tx, reduced_tx_set) |
| 6051 | } else { |
| 6052 | luma_set |
| 6053 | }; |
| 6054 | let luma_set = if intrabc_dv.is_some() { |
| 6055 | txbset_for_inter(logical_tx, reduced_tx_set) |
| 6056 | } else { |
| 6057 | luma_set |
| 6058 | }; |
| 6059 | let luma_scan = scan_for(coeff_tx_side, scan32, scan16, scan8, scan4); |
| 6060 | let reach = Reach::of(side, px, py, y.width, y.height); |
| 6061 | let (cpx, cpy) = (px / 2, py / 2); |
| 6062 | let chroma_side = side / 2; |
| 6063 | // spec 7.11.3.4 with `use_intrabc`: the reference is the *current* |
| 6064 | // frame's own pre-loop-filter reconstruction (all loop filters are off |
| 6065 | // whenever `allow_intrabc`, frame.rs:194/210/252/272) at the full-pel |
| 6066 | // block vector, with the BILINEAR kernel libaom forces for intrabc -- |
| 6067 | // luma is a straight copy, chroma lands half-pel whenever the luma DV is |
| 6068 | // odd, which is exactly what the bilinear taps then interpolate. |
| 6069 | let intrabc_bufs = intrabc_dv.map(|(dv_row, dv_col)| { |
| 6070 | let mut yb = vec![0u16; side * side]; |
| 6071 | mc::predict_with_filter( |
| 6072 | &y.data, y.width, y.true_width, y.true_height, |
| 6073 | mv_to_q4(px, dv_col, true), mv_to_q4(py, dv_row, true), |
| 6074 | side, side, mc::InterpFilterKind::Bilinear, &mut yb, |
| 6075 | ); |
| 6076 | let mut ub = vec![0u16; chroma_side * chroma_side]; |
| 6077 | mc::predict_with_filter( |
| 6078 | &u.data, u.width, u.true_width, u.true_height, |
| 6079 | mv_to_q4(cpx, dv_col, false), mv_to_q4(cpy, dv_row, false), |
| 6080 | chroma_side, chroma_side, mc::InterpFilterKind::Bilinear, &mut ub, |
| 6081 | ); |
| 6082 | let mut vb = vec![0u16; chroma_side * chroma_side]; |
| 6083 | mc::predict_with_filter( |
| 6084 | &v.data, v.width, v.true_width, v.true_height, |
| 6085 | mv_to_q4(cpx, dv_col, false), mv_to_q4(cpy, dv_row, false), |
| 6086 | chroma_side, chroma_side, mc::InterpFilterKind::Bilinear, &mut vb, |
| 6087 | ); |
| 6088 | (yb, ub, vb) |
| 6089 | }); |
| 6090 | if let Some((yb, _, _)) = &intrabc_bufs { |
| 6091 | PALETTE_PRED.with(|c| *c.borrow_mut() = Some(yb.clone())); |
| 6092 | } |
| 6093 | // A palette-Y block's own prediction is the reconstructed colour-index |
| 6094 | // map, not `predict()`'s edge-based one -- [`PALETTE_PRED`] carries it |
| 6095 | // to [`PlaneBuf::reconstruct`]'s next call rather than threading a |
| 6096 | // parameter through this function's dozen `y.reconstruct`/`read_plane` |
| 6097 | // sites. A split luma transform (`tx_select && logical_tx != side`) |
| 6098 | // would need the map sliced per-TU -- untested against a real |
| 6099 | // `--enable-palette=1` stream this round, so refuse by name rather than |
| 6100 | // guess at the slicing (charter's own named-acceptable fallback). |
| 6101 | if let Some(ref py) = palette_y { |
| 6102 | if tx_select && logical_tx != side { |
| 6103 | return Err(unsupported( |
| 6104 | "a palette block with a split luma transform (round 1)", |
| 6105 | )); |
| 6106 | } |
| 6107 | let buf: Vec<u16> = py |
| 6108 | .map |
| 6109 | .iter() |
| 6110 | .map(|&idx| py.colors[idx as usize]) |
| 6111 | .collect(); |
| 6112 | PALETTE_PRED.with(|c| *c.borrow_mut() = Some(buf)); |
| 6113 | } |
| 6114 | // A UV-palette block's own chroma prediction override (lane-palette2 r1), |
| 6115 | // same [`PALETTE_PRED`] slot as Y above -- set fresh right before each |
| 6116 | // plane's own `reconstruct`/`read_plane` call below (never both planes at |
| 6117 | // once), since Y's own set-then-take already emptied the slot by the time |
| 6118 | // either of these run. |
| 6119 | let palette_uv_bufs: Option<(Vec<u16>, Vec<u16>)> = palette_uv.as_ref().map(|puv| { |
| 6120 | ( |
| 6121 | puv.map.iter().map(|&idx| puv.u_colors[idx as usize]).collect(), |
| 6122 | puv.map.iter().map(|&idx| puv.v_colors[idx as usize]).collect(), |
| 6123 | ) |
| 6124 | }); |
| 6125 | // The chroma half of the intrabc prediction rides the same per-plane |
| 6126 | // override slot the UV-palette buffers do (set fresh before each plane's |
| 6127 | // own `reconstruct`/`read_plane` call below). |
| 6128 | let palette_uv_bufs = match &intrabc_bufs { |
| 6129 | Some((_, ub, vb)) => Some((ub.clone(), vb.clone())), |
| 6130 | None => palette_uv_bufs, |
| 6131 | }; |
| 6132 | if skip { |
| 6133 | // A skipped block codes no residual syntax at all (spec 5.11.34): |
| 6134 | // straight prediction, on every plane. |
| 6135 | y.reconstruct( |
| 6136 | px, |
| 6137 | py, |
| 6138 | side, |
| 6139 | mode, |
| 6140 | angle_delta_y, |
| 6141 | reach, |
| 6142 | &vec![0i32; side * side], |
| 6143 | None, |
| 6144 | filter_intra, |
| 6145 | smooth_neighbor, |
| 6146 | ); |
| 6147 | let ac = alpha.map(|_| cfl_ac_q3(y, px, py, side)); |
| 6148 | if let Some((ub, _)) = &palette_uv_bufs { |
| 6149 | PALETTE_PRED.with(|c| *c.borrow_mut() = Some(ub.clone())); |
| 6150 | } |
| 6151 | u.reconstruct( |
| 6152 | cpx, |
| 6153 | cpy, |
| 6154 | chroma_side, |
| 6155 | uv_predict_mode, |
| 6156 | angle_delta_uv, |
| 6157 | reach, |
| 6158 | &vec![0i32; chroma_side * chroma_side], |
| 6159 | alpha.zip(ac.as_deref()).map(|((au, _), ac)| (au, ac)), |
| 6160 | None, |
| 6161 | smooth_neighbor_uv, |
| 6162 | ); |
| 6163 | if let Some((_, vb)) = &palette_uv_bufs { |
| 6164 | PALETTE_PRED.with(|c| *c.borrow_mut() = Some(vb.clone())); |
| 6165 | } |
| 6166 | v.reconstruct( |
| 6167 | cpx, |
| 6168 | cpy, |
| 6169 | chroma_side, |
| 6170 | uv_predict_mode, |
| 6171 | angle_delta_uv, |
| 6172 | reach, |
| 6173 | &vec![0i32; chroma_side * chroma_side], |
| 6174 | alpha.zip(ac.as_deref()).map(|((_, av), ac)| (av, ac)), |
| 6175 | None, |
| 6176 | smooth_neighbor_uv, |
| 6177 | ); |
| 6178 | let luma_grid = vec![0i32; side * side]; |
| 6179 | let u_grid = vec![0i32; chroma_side * chroma_side]; |
| 6180 | let v_grid = vec![0i32; chroma_side * chroma_side]; |
| 6181 | neighbours.record(at, side, mode, uv_predict_mode, &[luma_grid, u_grid, v_grid]); |
| 6182 | } else if !tx_select || logical_tx == side { |
| 6183 | // A single luma transform unit -- the old path, unchanged (including |
| 6184 | // the 64x64 corner case, `logical_tx == side == 64` with |
| 6185 | // `coeff_tx_side == 32`). |
| 6186 | let around = neighbours.around(at, side); |
| 6187 | let luma_grid = read_plane( |
| 6188 | dec, |
| 6189 | cdfs, |
| 6190 | luma_set, |
| 6191 | luma_scan, |
| 6192 | 0, |
| 6193 | around[0], |
| 6194 | mode, |
| 6195 | mode, |
| 6196 | angle_delta_y, |
| 6197 | reach, |
| 6198 | y, |
| 6199 | px, |
| 6200 | py, |
| 6201 | side, |
| 6202 | coeff_tx_side, |
| 6203 | base_q_idx, |
| 6204 | None, |
| 6205 | filter_intra, |
| 6206 | None, |
| 6207 | smooth_neighbor, |
| 6208 | )?; |
| 6209 | let ac = alpha.map(|_| cfl_ac_q3(y, px, py, side)); |
| 6210 | if let Some((ub, _)) = &palette_uv_bufs { |
| 6211 | PALETTE_PRED.with(|c| *c.borrow_mut() = Some(ub.clone())); |
| 6212 | } |
| 6213 | let u_grid = read_plane( |
| 6214 | dec, |
| 6215 | cdfs, |
| 6216 | chroma_set, |
| 6217 | scans.1, |
| 6218 | 1, |
| 6219 | around[1], |
| 6220 | mode, |
| 6221 | uv_predict_mode, |
| 6222 | angle_delta_uv, |
| 6223 | reach, |
| 6224 | u, |
| 6225 | cpx, |
| 6226 | cpy, |
| 6227 | chroma_side, |
| 6228 | chroma_tx, |
| 6229 | base_q_idx, |
| 6230 | alpha.zip(ac.as_deref()).map(|((au, _), ac)| (au, ac)), |
| 6231 | None, |
| 6232 | None, |
| 6233 | smooth_neighbor_uv, |
| 6234 | )?; |
| 6235 | if std::env::var_os("EC_AV1_TRACE").is_some() { |
| 6236 | eprintln!( |
| 6237 | "TRACE u-write cpx={cpx} cpy={cpy} chroma_side={chroma_side} row0={:?}", |
| 6238 | &u.data[cpy * u.width + cpx..][..chroma_side] |
| 6239 | ); |
| 6240 | } |
| 6241 | if let Some((_, vb)) = &palette_uv_bufs { |
| 6242 | PALETTE_PRED.with(|c| *c.borrow_mut() = Some(vb.clone())); |
| 6243 | } |
| 6244 | let v_grid = read_plane( |
| 6245 | dec, |
| 6246 | cdfs, |
| 6247 | chroma_set, |
| 6248 | scans.1, |
| 6249 | 2, |
| 6250 | around[2], |
| 6251 | mode, |
| 6252 | uv_predict_mode, |
| 6253 | angle_delta_uv, |
| 6254 | reach, |
| 6255 | v, |
| 6256 | cpx, |
| 6257 | cpy, |
| 6258 | chroma_side, |
| 6259 | chroma_tx, |
| 6260 | base_q_idx, |
| 6261 | alpha.zip(ac.as_deref()).map(|((_, av), ac)| (av, ac)), |
| 6262 | None, |
| 6263 | None, |
| 6264 | smooth_neighbor_uv, |
| 6265 | )?; |
| 6266 | neighbours.record(at, side, mode, uv_predict_mode, &[luma_grid, u_grid, v_grid]); |
| 6267 | } else { |
| 6268 | // Several luma transform units, raster order (spec |
| 6269 | // `transform_block`'s loop): the block's resolved transform is |
| 6270 | // genuinely smaller than its own side, so each tile of it predicts |
| 6271 | // and codes independently, its own context read fresh off whatever |
| 6272 | // the earlier tiles in this same block already wrote -- the same |
| 6273 | // per-TU pattern [`decode_leaf8`] already runs for an 8x8 leaf. |
| 6274 | let n_axis = side / logical_tx; |
| 6275 | for tu_row in 0..n_axis { |
| 6276 | for tu_col in 0..n_axis { |
| 6277 | let tu_mi = ( |
| 6278 | mi_r + tu_row * (logical_tx / MI), |
| 6279 | mi_c + tu_col * (logical_tx / MI), |
| 6280 | ); |
| 6281 | let tu_px = px + tu_col * logical_tx; |
| 6282 | let tu_py = py + tu_row * logical_tx; |
| 6283 | let tu_around = neighbours.around_mi(tu_mi, logical_tx)[0]; |
| 6284 | let tu_reach = Reach::of(logical_tx, tu_px, tu_py, y.width, y.height); |
| 6285 | // This transform unit's own bsize (`logical_tx`) is smaller |
| 6286 | // than the block it sits in (`side`), so `txb_skip_ctx` is |
| 6287 | // the neighbour-magnitude table, not the lone-TU 0 (spec |
| 6288 | // `get_txb_ctx_general`'s `plane_bsize != tx_size` branch). |
| 6289 | let tu_skip_ctx = neighbours.luma_skip_ctx(tu_mi, logical_tx / MI); |
| 6290 | let tu_grid = read_plane( |
| 6291 | dec, |
| 6292 | cdfs, |
| 6293 | luma_set, |
| 6294 | luma_scan, |
| 6295 | 0, |
| 6296 | tu_around, |
| 6297 | mode, |
| 6298 | mode, |
| 6299 | angle_delta_y, |
| 6300 | tu_reach, |
| 6301 | y, |
| 6302 | tu_px, |
| 6303 | tu_py, |
| 6304 | logical_tx, |
| 6305 | logical_tx, |
| 6306 | base_q_idx, |
| 6307 | None, |
| 6308 | filter_intra, |
| 6309 | Some(tu_skip_ctx), |
| 6310 | smooth_neighbor, |
| 6311 | )?; |
| 6312 | if std::env::var_os("EC_AV1_TRACE").is_some() { |
| 6313 | eprintln!( |
| 6314 | "TRACE tu_bitpos mi=({},{}) tell={}", |
| 6315 | tu_mi.0, |
| 6316 | tu_mi.1, |
| 6317 | dec.debug_bitpos() |
| 6318 | ); |
| 6319 | } |
| 6320 | neighbours.record_mi_luma(tu_mi, logical_tx, &tu_grid); |
| 6321 | } |
| 6322 | } |
| 6323 | let ac = alpha.map(|_| cfl_ac_q3(y, px, py, side)); |
| 6324 | let chroma_around = neighbours.around(at, side); |
| 6325 | if let Some((ub, _)) = &palette_uv_bufs { |
| 6326 | PALETTE_PRED.with(|c| *c.borrow_mut() = Some(ub.clone())); |
| 6327 | } |
| 6328 | let u_grid = read_plane( |
| 6329 | dec, |
| 6330 | cdfs, |
| 6331 | chroma_set, |
| 6332 | scans.1, |
| 6333 | 1, |
| 6334 | chroma_around[1], |
| 6335 | mode, |
| 6336 | uv_predict_mode, |
| 6337 | angle_delta_uv, |
| 6338 | reach, |
| 6339 | u, |
| 6340 | cpx, |
| 6341 | cpy, |
| 6342 | chroma_side, |
| 6343 | chroma_tx, |
| 6344 | base_q_idx, |
| 6345 | alpha.zip(ac.as_deref()).map(|((au, _), ac)| (au, ac)), |
| 6346 | None, |
| 6347 | None, |
| 6348 | smooth_neighbor_uv, |
| 6349 | )?; |
| 6350 | if let Some((_, vb)) = &palette_uv_bufs { |
| 6351 | PALETTE_PRED.with(|c| *c.borrow_mut() = Some(vb.clone())); |
| 6352 | } |
| 6353 | let v_grid = read_plane( |
| 6354 | dec, |
| 6355 | cdfs, |
| 6356 | chroma_set, |
| 6357 | scans.1, |
| 6358 | 2, |
| 6359 | chroma_around[2], |
| 6360 | mode, |
| 6361 | uv_predict_mode, |
| 6362 | angle_delta_uv, |
| 6363 | reach, |
| 6364 | v, |
| 6365 | cpx, |
| 6366 | cpy, |
| 6367 | chroma_side, |
| 6368 | chroma_tx, |
| 6369 | base_q_idx, |
| 6370 | alpha.zip(ac.as_deref()).map(|((_, av), ac)| (av, ac)), |
| 6371 | None, |
| 6372 | None, |
| 6373 | smooth_neighbor_uv, |
| 6374 | )?; |
| 6375 | neighbours.record_split_luma(at, side, mode, uv_predict_mode, [&u_grid, &v_grid]); |
| 6376 | } |
| 6377 | // Unconditional, `size == 0` for a non-palette block: clears any stale |
| 6378 | // neighbour palette state the same way [`Neighbours::record`] always |
| 6379 | // overwrites `above_mode`/`left_mode` regardless of this block's own mode |
| 6380 | // (the split-luma-TU branch above always refuses before reaching here |
| 6381 | // when `palette_y` is `Some`, so it is never live for that branch). |
| 6382 | let (py_size, py_colors) = palette_y |
| 6383 | .as_ref() |
| 6384 | .map_or((0, [0u16; 8]), |p| (p.size, p.colors)); |
| 6385 | neighbours.record_palette_y(at, side, py_size, py_colors); |
| 6386 | let (puv_size, puv_colors) = palette_uv |
| 6387 | .as_ref() |
| 6388 | .map_or((0, [0u16; 8]), |p| (p.size, p.u_colors)); |
| 6389 | neighbours.record_palette_uv(at, side, puv_size, puv_colors); |
| 6390 | neighbours.fill_skip_grid((r * (SUB / MI), c * (SUB / MI)), side / MI, skip); |
| 6391 | neighbours.fill_lf_grid( |
| 6392 | (r * (SUB / MI), c * (SUB / MI)), |
| 6393 | side / MI, |
| 6394 | logical_tx as u8, |
| 6395 | 0, |
| 6396 | ); |
| 6397 | record_intrabc_mi(mi_r, mi_c, side / MI, intrabc_dv); |
| 6398 | Ok(()) |
| 6399 | } |
| 6400 | |
| 6401 | /// Decodes one 8x8 leaf of a straddling 16x16 block (lane-av1-rect), mirroring |
| 6402 | /// [`crate::tile::write_leaf8`]: its intra-mode context comes from the |
| 6403 | /// *enclosing* 16x16 slot's `above_mode`/`left_mode` (`outer_at`, [`SUB`]-grid), |
| 6404 | /// overridden on whichever axis `prev_leaf` sits along -- the previous leaf's |
| 6405 | /// just-decoded mode, not the enclosing slot's stale one, is the true |
| 6406 | /// neighbour there. `leaf_mi` is this leaf's own position in 4x4 mode-info |
| 6407 | /// units, which is what its coefficient context (finer than [`SUB`]) is read |
| 6408 | /// at. Returns the mode this leaf decoded, for the caller's own |
| 6409 | /// `prev_leaf`/final mode-writeback bookkeeping. |
| 6410 | #[allow(clippy::too_many_arguments)] |
| 6411 | fn decode_leaf8( |
| 6412 | dec: &mut SymbolDecoder, |
| 6413 | cdfs: &mut Cdfs, |
| 6414 | neighbours: &mut Neighbours, |
| 6415 | outer_at: (usize, usize), |
| 6416 | leaf_mi: (usize, usize), |
| 6417 | scans: (&[u16], &[u16]), |
| 6418 | prev_leaf: Option<((usize, usize), usize)>, |
| 6419 | y: &mut PlaneBuf, |
| 6420 | u: &mut PlaneBuf, |
| 6421 | v: &mut PlaneBuf, |
| 6422 | base_q_idx: u8, |
| 6423 | enable_filter_intra: bool, |
| 6424 | allow_screen_content_tools: bool, |
| 6425 | allow_intrabc: bool, |
| 6426 | tx_select: bool, |
| 6427 | reduced_tx_set: bool, |
| 6428 | ) -> Result<usize> { |
| 6429 | let (r, c) = outer_at; |
| 6430 | let mut above_mode = neighbours.above_mode[c]; |
| 6431 | let mut left_mode = neighbours.left_mode[r]; |
| 6432 | if let Some(((pr, pc), pmode)) = prev_leaf { |
| 6433 | if pc == leaf_mi.1 && leaf_mi.0 == pr + 2 { |
| 6434 | above_mode = pmode; |
| 6435 | } else if pr == leaf_mi.0 && leaf_mi.1 == pc + 2 { |
| 6436 | left_mode = pmode; |
| 6437 | } |
| 6438 | } |
| 6439 | // See [`PlaneBuf::reconstruct`]'s doc: chroma stays `false` (exact) until |
| 6440 | // a smooth/paeth `uv_mode` can reach decode at all. |
| 6441 | let smooth_neighbor = is_smooth_mode(above_mode) || is_smooth_mode(left_mode); |
| 6442 | if smooth_neighbor { |
| 6443 | SMOOTH_LUMA_HITS.with(|c| c.set(c.get() + 1)); |
| 6444 | } |
| 6445 | // An 8x8 leaf is well within `is_cfl_allowed`'s <=32x32 bound (spec |
| 6446 | // 5.11.5), so it reads the CFL-allowed `uv_mode_cfl` CDF, like every other |
| 6447 | // `decode_block` caller at 16x16 and up. |
| 6448 | let (skip, mode, angle_delta_y, uv_mode, angle_delta_uv, alpha, filter_intra, _palette_y, _palette_uv) = |
| 6449 | read_intra_mode( |
| 6450 | dec, |
| 6451 | cdfs, |
| 6452 | above_mode, |
| 6453 | left_mode, |
| 6454 | true, |
| 6455 | 8, |
| 6456 | enable_filter_intra, |
| 6457 | neighbours.skip_txfm_ctx(leaf_mi.0, leaf_mi.1), |
| 6458 | allow_screen_content_tools, |
| 6459 | allow_intrabc, |
| 6460 | None, |
| 6461 | &[], |
| 6462 | leaf_mi.0, |
| 6463 | leaf_mi.1, |
| 6464 | )?; |
| 6465 | let uv_predict_mode = if uv_mode == UV_CFL_PRED { |
| 6466 | DC_PRED |
| 6467 | } else { |
| 6468 | uv_mode |
| 6469 | }; |
| 6470 | // `TxMode::Select`'s `tx_depth` at an 8x8 leaf (spec 5.11.16): the only |
| 6471 | // depths an 8x8 block offers are `TX8` (depth 0) and `TX4` (depth 1, |
| 6472 | // which splits the leaf's own 8x8 prediction into a 2x2 grid of 4x4 |
| 6473 | // transform units, same raster-order per-TU pattern `decode_block`'s |
| 6474 | // multi-TU branch runs). |
| 6475 | let resolved = if tx_select { |
| 6476 | read_tx_size(dec, cdfs, neighbours, leaf_mi, 8, None) |
| 6477 | } else { |
| 6478 | 8 |
| 6479 | }; |
| 6480 | let (px, py) = (leaf_mi.1 * MI, leaf_mi.0 * MI); |
| 6481 | let reach = Reach::of(8, px, py, y.width, y.height); |
| 6482 | let (cpx, cpy) = (px / 2, py / 2); |
| 6483 | let (luma_grid, u_grid, v_grid); |
| 6484 | if skip { |
| 6485 | y.reconstruct( |
| 6486 | px, |
| 6487 | py, |
| 6488 | 8, |
| 6489 | mode, |
| 6490 | angle_delta_y, |
| 6491 | reach, |
| 6492 | &vec![0i32; 64], |
| 6493 | None, |
| 6494 | filter_intra, |
| 6495 | smooth_neighbor, |
| 6496 | ); |
| 6497 | let ac = alpha.map(|_| cfl_ac_q3(y, px, py, 8)); |
| 6498 | u.reconstruct( |
| 6499 | cpx, |
| 6500 | cpy, |
| 6501 | 4, |
| 6502 | uv_predict_mode, |
| 6503 | angle_delta_uv, |
| 6504 | reach, |
| 6505 | &vec![0i32; 16], |
| 6506 | alpha.zip(ac.as_deref()).map(|((au, _), ac)| (au, ac)), |
| 6507 | None, |
| 6508 | false, |
| 6509 | ); |
| 6510 | v.reconstruct( |
| 6511 | cpx, |
| 6512 | cpy, |
| 6513 | 4, |
| 6514 | uv_predict_mode, |
| 6515 | angle_delta_uv, |
| 6516 | reach, |
| 6517 | &vec![0i32; 16], |
| 6518 | alpha.zip(ac.as_deref()).map(|((_, av), ac)| (av, ac)), |
| 6519 | None, |
| 6520 | false, |
| 6521 | ); |
| 6522 | luma_grid = vec![0i32; 64]; |
| 6523 | u_grid = vec![0i32; 16]; |
| 6524 | v_grid = vec![0i32; 16]; |
| 6525 | } else if resolved != 4 { |
| 6526 | let around = neighbours.around_mi(leaf_mi, 8); |
| 6527 | luma_grid = read_plane( |
| 6528 | dec, |
| 6529 | cdfs, |
| 6530 | if reduced_tx_set { |
| 6531 | TxbSet::Luma8 |
| 6532 | } else { |
| 6533 | TxbSet::Luma8Set1 |
| 6534 | }, |
| 6535 | scans.0, |
| 6536 | 0, |
| 6537 | around[0], |
| 6538 | mode, |
| 6539 | mode, |
| 6540 | angle_delta_y, |
| 6541 | reach, |
| 6542 | y, |
| 6543 | px, |
| 6544 | py, |
| 6545 | 8, |
| 6546 | TX8, |
| 6547 | base_q_idx, |
| 6548 | None, |
| 6549 | filter_intra, |
| 6550 | None, |
| 6551 | smooth_neighbor, |
| 6552 | )?; |
| 6553 | let ac = alpha.map(|_| cfl_ac_q3(y, px, py, 8)); |
| 6554 | u_grid = read_plane( |
| 6555 | dec, |
| 6556 | cdfs, |
| 6557 | TxbSet::Chroma4, |
| 6558 | scans.1, |
| 6559 | 1, |
| 6560 | around[1], |
| 6561 | mode, |
| 6562 | uv_predict_mode, |
| 6563 | angle_delta_uv, |
| 6564 | reach, |
| 6565 | u, |
| 6566 | cpx, |
| 6567 | cpy, |
| 6568 | 4, |
| 6569 | TX4, |
| 6570 | base_q_idx, |
| 6571 | alpha.zip(ac.as_deref()).map(|((au, _), ac)| (au, ac)), |
| 6572 | None, |
| 6573 | None, |
| 6574 | false, |
| 6575 | )?; |
| 6576 | v_grid = read_plane( |
| 6577 | dec, |
| 6578 | cdfs, |
| 6579 | TxbSet::Chroma4, |
| 6580 | scans.1, |
| 6581 | 2, |
| 6582 | around[2], |
| 6583 | mode, |
| 6584 | uv_predict_mode, |
| 6585 | angle_delta_uv, |
| 6586 | reach, |
| 6587 | v, |
| 6588 | cpx, |
| 6589 | cpy, |
| 6590 | 4, |
| 6591 | TX4, |
| 6592 | base_q_idx, |
| 6593 | alpha.zip(ac.as_deref()).map(|((_, av), ac)| (av, ac)), |
| 6594 | None, |
| 6595 | None, |
| 6596 | false, |
| 6597 | )?; |
| 6598 | } else { |
| 6599 | // `tx_depth` resolved this leaf's luma to `TX4`: a 2x2 raster-order |
| 6600 | // grid of 4x4 transform units, each predicted from the leaf's own |
| 6601 | // 8x8 `mode` and reading its own fresh context off the earlier |
| 6602 | // units in this same leaf (the same per-TU pattern `decode_block`'s |
| 6603 | // multi-TU branch runs at bigger blocks). |
| 6604 | luma_grid = vec![0i32; 64]; |
| 6605 | for tu_row in 0..2 { |
| 6606 | for tu_col in 0..2 { |
| 6607 | let tu_mi = (leaf_mi.0 + tu_row, leaf_mi.1 + tu_col); |
| 6608 | let tu_px = px + tu_col * 4; |
| 6609 | let tu_py = py + tu_row * 4; |
| 6610 | let tu_around = neighbours.around_mi(tu_mi, 4)[0]; |
| 6611 | let tu_reach = Reach::of(4, tu_px, tu_py, y.width, y.height); |
| 6612 | let tu_skip_ctx = neighbours.luma_skip_ctx(tu_mi, 1); |
| 6613 | let tu_grid = read_plane( |
| 6614 | dec, |
| 6615 | cdfs, |
| 6616 | if reduced_tx_set { |
| 6617 | TxbSet::Luma4 |
| 6618 | } else { |
| 6619 | TxbSet::Luma4Set1 |
| 6620 | }, |
| 6621 | scans.1, |
| 6622 | 0, |
| 6623 | tu_around, |
| 6624 | mode, |
| 6625 | mode, |
| 6626 | angle_delta_y, |
| 6627 | tu_reach, |
| 6628 | y, |
| 6629 | tu_px, |
| 6630 | tu_py, |
| 6631 | 4, |
| 6632 | 4, |
| 6633 | base_q_idx, |
| 6634 | None, |
| 6635 | filter_intra, |
| 6636 | Some(tu_skip_ctx), |
| 6637 | smooth_neighbor, |
| 6638 | )?; |
| 6639 | if std::env::var_os("EC_AV1_TRACE").is_some() { |
| 6640 | eprintln!( |
| 6641 | "TRACE tu_bitpos mi=({},{}) tell={}", |
| 6642 | tu_mi.0, |
| 6643 | tu_mi.1, |
| 6644 | dec.debug_bitpos() |
| 6645 | ); |
| 6646 | } |
| 6647 | neighbours.record_mi_luma(tu_mi, 4, &tu_grid); |
| 6648 | } |
| 6649 | } |
| 6650 | let ac = alpha.map(|_| cfl_ac_q3(y, px, py, 8)); |
| 6651 | let chroma_around = neighbours.around_mi(leaf_mi, 8); |
| 6652 | u_grid = read_plane( |
| 6653 | dec, |
| 6654 | cdfs, |
| 6655 | TxbSet::Chroma4, |
| 6656 | scans.1, |
| 6657 | 1, |
| 6658 | chroma_around[1], |
| 6659 | mode, |
| 6660 | uv_predict_mode, |
| 6661 | angle_delta_uv, |
| 6662 | reach, |
| 6663 | u, |
| 6664 | cpx, |
| 6665 | cpy, |
| 6666 | 4, |
| 6667 | TX4, |
| 6668 | base_q_idx, |
| 6669 | alpha.zip(ac.as_deref()).map(|((au, _), ac)| (au, ac)), |
| 6670 | None, |
| 6671 | None, |
| 6672 | false, |
| 6673 | )?; |
| 6674 | v_grid = read_plane( |
| 6675 | dec, |
| 6676 | cdfs, |
| 6677 | TxbSet::Chroma4, |
| 6678 | scans.1, |
| 6679 | 2, |
| 6680 | chroma_around[2], |
| 6681 | mode, |
| 6682 | uv_predict_mode, |
| 6683 | angle_delta_uv, |
| 6684 | reach, |
| 6685 | v, |
| 6686 | cpx, |
| 6687 | cpy, |
| 6688 | 4, |
| 6689 | TX4, |
| 6690 | base_q_idx, |
| 6691 | alpha.zip(ac.as_deref()).map(|((_, av), ac)| (av, ac)), |
| 6692 | None, |
| 6693 | None, |
| 6694 | false, |
| 6695 | )?; |
| 6696 | // `record_split_luma` expects a `record`-style `(r, c)` position in |
| 6697 | // `SUB`-grid units and rescales it by `SUB / MI`; `leaf_mi` is |
| 6698 | // already in mi units, so calling it here double-scaled the |
| 6699 | // position. Plane 0 is already correct per-TU from the |
| 6700 | // `record_mi_luma` calls above; this inlines `record_mi`'s tail |
| 6701 | // (spec `get_txb_ctx_general`'s neighbour-magnitude bookkeeping) for |
| 6702 | // the two chroma planes only, at `leaf_mi` directly, side_mi=2 (this |
| 6703 | // leaf's own 8x8 side over `MI`). |
| 6704 | let side_mi = 2; |
| 6705 | for cell in 0..side_mi { |
| 6706 | neighbours.left_side_mi[leaf_mi.0 + cell] = 8; |
| 6707 | neighbours.above_side_mi[leaf_mi.1 + cell] = 8; |
| 6708 | } |
| 6709 | let round_up_even = |n: usize| n.div_ceil(2) * 2; |
| 6710 | let bound_h = round_up_even(neighbours.mi_rows); |
| 6711 | let bound_w = round_up_even(neighbours.mi_cols); |
| 6712 | let u_state = neighbour_state(&u_grid); |
| 6713 | let v_state = neighbour_state(&v_grid); |
| 6714 | for cell in 0..side_mi { |
| 6715 | if cell < side_mi.min(bound_h.saturating_sub(leaf_mi.0)) { |
| 6716 | neighbours.left[leaf_mi.0 + cell][1] = u_state; |
| 6717 | neighbours.left[leaf_mi.0 + cell][2] = v_state; |
| 6718 | } |
| 6719 | if cell < side_mi.min(bound_w.saturating_sub(leaf_mi.1)) { |
| 6720 | neighbours.above[leaf_mi.1 + cell][1] = u_state; |
| 6721 | neighbours.above[leaf_mi.1 + cell][2] = v_state; |
| 6722 | } |
| 6723 | } |
| 6724 | neighbours.fill_skip_grid(leaf_mi, 2, skip); |
| 6725 | neighbours.fill_lf_grid(leaf_mi, 2, 4, 0); |
| 6726 | return Ok(mode); |
| 6727 | } |
| 6728 | neighbours.record_mi(leaf_mi, 8, &[luma_grid, u_grid, v_grid]); |
| 6729 | neighbours.fill_skip_grid(leaf_mi, 2, skip); |
| 6730 | neighbours.fill_lf_grid(leaf_mi, 2, 8, 0); |
| 6731 | Ok(mode) |
| 6732 | } |
| 6733 | |
| 6734 | /// Spec 7.15.3's `Cdef_Directions`: `(row, col)` offsets for the two primary |
| 6735 | /// taps of each of the eight directions; the secondary taps at `dir+2`/`dir-2` |
| 6736 | /// (mod 8) reuse the same table. |
| 6737 | const CDEF_DIRECTIONS: [[(i32, i32); 2]; 8] = [ |
| 6738 | [(-1, 1), (-2, 2)], |
| 6739 | [(0, 1), (-1, 2)], |
| 6740 | [(0, 1), (0, 2)], |
| 6741 | [(0, 1), (1, 2)], |
| 6742 | [(1, 1), (2, 2)], |
| 6743 | [(1, 0), (2, 1)], |
| 6744 | [(1, 0), (2, 0)], |
| 6745 | [(1, 0), (2, -1)], |
| 6746 | ]; |
| 6747 | /// A neighbour tap outside the frame's true extent -- `constrain` reads this |
| 6748 | /// as "so far away it never contributes" (spec/libaom `CDEF_VERY_LARGE`). |
| 6749 | const CDEF_VERY_LARGE: i32 = 0x4000; |
| 6750 | const CDEF_PRI_TAPS: [[i32; 2]; 2] = [[4, 2], [3, 3]]; |
| 6751 | const CDEF_SEC_TAPS: [i32; 2] = [2, 1]; |
| 6752 | |
| 6753 | fn cdef_msb(x: i32) -> i32 { |
| 6754 | 31 - x.leading_zeros() as i32 |
| 6755 | } |
| 6756 | |
| 6757 | /// Spec 7.15.3's `constrain`: how much of a neighbour's difference from the |
| 6758 | /// centre sample a filter tap is allowed to contribute. |
| 6759 | fn cdef_constrain(diff: i32, threshold: i32, damping: i32) -> i32 { |
| 6760 | if threshold == 0 { |
| 6761 | return 0; |
| 6762 | } |
| 6763 | let shift = (damping - cdef_msb(threshold)).max(0); |
| 6764 | diff.signum() * (threshold - (diff.abs() >> shift)).clamp(0, diff.abs()) |
| 6765 | } |
| 6766 | |
| 6767 | /// Spec 7.15.3's direction search (libaom `cdef_find_dir_c`): the dominant |
| 6768 | /// direction of an 8x8 luma window and the variance gap between it and its |
| 6769 | /// orthogonal, `sample(row, col)` reading that window's own pixels. |
| 6770 | fn cdef_find_dir(coeff_shift: i32, sample: impl Fn(usize, usize) -> i32) -> (usize, i32) { |
| 6771 | const DIV_TABLE: [i64; 9] = [0, 840, 420, 280, 210, 168, 140, 120, 105]; |
| 6772 | let mut partial = [[0i64; 15]; 8]; |
| 6773 | for i in 0..8i64 { |
| 6774 | for j in 0..8i64 { |
| 6775 | // libaom `cdef_find_dir_c`: normalise to an 8-bit sample before |
| 6776 | // accumulating, same as `constrain`'s threshold shift below. |
| 6777 | let x = i64::from(sample(i as usize, j as usize) >> coeff_shift) - 128; |
| 6778 | partial[0][(i + j) as usize] += x; |
| 6779 | partial[1][(i + j / 2) as usize] += x; |
| 6780 | partial[2][i as usize] += x; |
| 6781 | partial[3][(3 + i - j / 2) as usize] += x; |
| 6782 | partial[4][(7 + i - j) as usize] += x; |
| 6783 | partial[5][(3 - i / 2 + j) as usize] += x; |
| 6784 | partial[6][j as usize] += x; |
| 6785 | partial[7][(i / 2 + j) as usize] += x; |
| 6786 | } |
| 6787 | } |
| 6788 | let mut cost = [0i64; 8]; |
| 6789 | for i in 0..8usize { |
| 6790 | cost[2] += partial[2][i] * partial[2][i]; |
| 6791 | cost[6] += partial[6][i] * partial[6][i]; |
| 6792 | } |
| 6793 | cost[2] *= DIV_TABLE[8]; |
| 6794 | cost[6] *= DIV_TABLE[8]; |
| 6795 | for i in 0..7usize { |
| 6796 | cost[0] += (partial[0][i] * partial[0][i] + partial[0][14 - i] * partial[0][14 - i]) |
| 6797 | * DIV_TABLE[i + 1]; |
| 6798 | cost[4] += (partial[4][i] * partial[4][i] + partial[4][14 - i] * partial[4][14 - i]) |
| 6799 | * DIV_TABLE[i + 1]; |
| 6800 | } |
| 6801 | cost[0] += partial[0][7] * partial[0][7] * DIV_TABLE[8]; |
| 6802 | cost[4] += partial[4][7] * partial[4][7] * DIV_TABLE[8]; |
| 6803 | for i in (1..8).step_by(2) { |
| 6804 | for j in 0..5usize { |
| 6805 | cost[i] += partial[i][3 + j] * partial[i][3 + j]; |
| 6806 | } |
| 6807 | cost[i] *= DIV_TABLE[8]; |
| 6808 | for j in 0..3usize { |
| 6809 | cost[i] += (partial[i][j] * partial[i][j] + partial[i][10 - j] * partial[i][10 - j]) |
| 6810 | * DIV_TABLE[2 * j + 2]; |
| 6811 | } |
| 6812 | } |
| 6813 | let mut best_cost = 0i64; |
| 6814 | let mut best_dir = 0usize; |
| 6815 | for (i, &c) in cost.iter().enumerate() { |
| 6816 | if c > best_cost { |
| 6817 | best_cost = c; |
| 6818 | best_dir = i; |
| 6819 | } |
| 6820 | } |
| 6821 | let var = (best_cost - cost[(best_dir + 4) & 7]) >> 10; |
| 6822 | (best_dir, var as i32) |
| 6823 | } |
| 6824 | |
| 6825 | /// Spec 7.15.2's per-8x8 strength adjustment: a strongly directional 8x8 |
| 6826 | /// (high variance gap) gets more deringing than a flat or non-directional |
| 6827 | /// one, only ever applied to the luma primary strength. |
| 6828 | fn cdef_adjust_strength(strength: i32, var: i32) -> i32 { |
| 6829 | if var == 0 { |
| 6830 | return 0; |
| 6831 | } |
| 6832 | let i = if var >> 6 != 0 { |
| 6833 | cdef_msb(var >> 6).min(12) |
| 6834 | } else { |
| 6835 | 0 |
| 6836 | }; |
| 6837 | (strength * (4 + i) + 8) >> 4 |
| 6838 | } |
| 6839 | |
| 6840 | /// Spec 7.15.3's `cdef_filter_block`: filters one block (8x8 luma, 4x4 4:2:0 |
| 6841 | /// chroma) in place, `sample(row, col)` reading the *unfiltered* frame (a |
| 6842 | /// previously-filtered neighbour must never feed this block's own sum). |
| 6843 | #[allow(clippy::too_many_arguments)] |
| 6844 | fn cdef_filter_block( |
| 6845 | sample: impl Fn(i32, i32) -> i32, |
| 6846 | dst: &mut PlaneBuf, |
| 6847 | ox: usize, |
| 6848 | oy: usize, |
| 6849 | bw: usize, |
| 6850 | bh: usize, |
| 6851 | pri_strength: i32, |
| 6852 | sec_strength: i32, |
| 6853 | dir: usize, |
| 6854 | pri_damping: i32, |
| 6855 | sec_damping: i32, |
| 6856 | enable_primary: bool, |
| 6857 | enable_secondary: bool, |
| 6858 | coeff_shift: i32, |
| 6859 | ) { |
| 6860 | let clipping_required = enable_primary && enable_secondary; |
| 6861 | // libaom `cdef_filter_block_internal`: `cdef_pri_taps[(pri_strength >> |
| 6862 | // coeff_shift) & 1]` -- `pri_strength` (and its `adjust_strength` |
| 6863 | // derivative) carries the bit-depth shift, so the tap-set parity bit |
| 6864 | // must be read back above that shift, not off the raw low bit. |
| 6865 | let pri_taps = CDEF_PRI_TAPS[((pri_strength >> coeff_shift) & 1) as usize]; |
| 6866 | for i in 0..bh { |
| 6867 | for j in 0..bw { |
| 6868 | let x = sample(i as i32, j as i32); |
| 6869 | let mut sum = 0i32; |
| 6870 | let mut max = x; |
| 6871 | let mut min = x; |
| 6872 | for k in 0..2usize { |
| 6873 | if enable_primary { |
| 6874 | let (tr, tc) = CDEF_DIRECTIONS[dir][k]; |
| 6875 | let p0 = sample(i as i32 + tr, j as i32 + tc); |
| 6876 | let p1 = sample(i as i32 - tr, j as i32 - tc); |
| 6877 | sum += pri_taps[k] * cdef_constrain(p0 - x, pri_strength, pri_damping); |
| 6878 | sum += pri_taps[k] * cdef_constrain(p1 - x, pri_strength, pri_damping); |
| 6879 | if clipping_required { |
| 6880 | if p0 != CDEF_VERY_LARGE { |
| 6881 | max = max.max(p0); |
| 6882 | } |
| 6883 | if p1 != CDEF_VERY_LARGE { |
| 6884 | max = max.max(p1); |
| 6885 | } |
| 6886 | min = min.min(p0).min(p1); |
| 6887 | } |
| 6888 | } |
| 6889 | if enable_secondary { |
| 6890 | let (tr0, tc0) = CDEF_DIRECTIONS[(dir + 2) & 7][k]; |
| 6891 | let (tr1, tc1) = CDEF_DIRECTIONS[(dir + 6) & 7][k]; |
| 6892 | let s0 = sample(i as i32 + tr0, j as i32 + tc0); |
| 6893 | let s1 = sample(i as i32 - tr0, j as i32 - tc0); |
| 6894 | let s2 = sample(i as i32 + tr1, j as i32 + tc1); |
| 6895 | let s3 = sample(i as i32 - tr1, j as i32 - tc1); |
| 6896 | if clipping_required { |
| 6897 | for s in [s0, s1, s2, s3] { |
| 6898 | if s != CDEF_VERY_LARGE { |
| 6899 | max = max.max(s); |
| 6900 | } |
| 6901 | } |
| 6902 | min = min.min(s0).min(s1).min(s2).min(s3); |
| 6903 | } |
| 6904 | sum += CDEF_SEC_TAPS[k] |
| 6905 | * (cdef_constrain(s0 - x, sec_strength, sec_damping) |
| 6906 | + cdef_constrain(s1 - x, sec_strength, sec_damping) |
| 6907 | + cdef_constrain(s2 - x, sec_strength, sec_damping) |
| 6908 | + cdef_constrain(s3 - x, sec_strength, sec_damping)); |
| 6909 | } |
| 6910 | } |
| 6911 | let mut y = x + ((8 + sum - i32::from(sum < 0)) >> 4); |
| 6912 | if clipping_required { |
| 6913 | y = y.clamp(min, max); |
| 6914 | } |
| 6915 | dst.data[(oy + i) * dst.width + (ox + j)] = y.clamp(0, sample_max()) as u16; |
| 6916 | } |
| 6917 | } |
| 6918 | } |
| 6919 | |
| 6920 | /// Spec 7.14: the in-loop deblocking filter, applied once per frame after |
| 6921 | /// tile reconstruction and before [`apply_cdef`]. Uniform-level path only -- |
| 6922 | /// `stream.rs` already refuses `delta_lf_present`/segmentation upstream, so |
| 6923 | /// every block's level comes from [`LoopFilterParams::level`] plus the |
| 6924 | /// `ref_deltas`/`mode_deltas` adjustment alone (spec 7.14.4's |
| 6925 | /// `get_filter_level`, precomputed per-block here rather than cached in a |
| 6926 | /// table the way libaom's `av1_loop_filter_frame_init` does). |
| 6927 | /// |
| 6928 | /// Ported from libaom's `av1/common/av1_loopfilter.c` (edge/level decision) |
| 6929 | /// and `aom_dsp/loopfilter.c` (the `aom_lpf_*` pixel kernels). |
| 6930 | fn apply_deblock( |
| 6931 | y: &mut PlaneBuf, |
| 6932 | u: &mut PlaneBuf, |
| 6933 | v: &mut PlaneBuf, |
| 6934 | lf: &LoopFilterParams, |
| 6935 | n: &Neighbours, |
| 6936 | frame_width: usize, |
| 6937 | frame_height: usize, |
| 6938 | ) { |
| 6939 | // lane-part32 r2 debug rung, env-gated: bisecting a widespread pixel |
| 6940 | // mismatch between the raw reconstruction and post-filter output. |
| 6941 | if std::env::var_os("EC_AV1_DEBUG_SKIP_DEBLOCK").is_some() { |
| 6942 | return; |
| 6943 | } |
| 6944 | if lf.level[0] != 0 || lf.level[1] != 0 { |
| 6945 | deblock_plane(y, 0, lf, n, frame_width, frame_height); |
| 6946 | } |
| 6947 | if lf.level[2] != 0 { |
| 6948 | deblock_plane(u, 1, lf, n, frame_width, frame_height); |
| 6949 | } |
| 6950 | if lf.level[3] != 0 { |
| 6951 | deblock_plane(v, 2, lf, n, frame_width, frame_height); |
| 6952 | } |
| 6953 | } |
| 6954 | |
| 6955 | /// Luma-mi-grid position (row or col) a plane-local pixel coordinate falls |
| 6956 | /// in -- `Neighbours::tx_grid`/`ref_grid` are always indexed at the luma |
| 6957 | /// 4x4-mi grain, even when `coord` is a chroma coordinate (2 chroma px per |
| 6958 | /// mi unit under this decoder's assumed 4:2:0 subsampling). |
| 6959 | fn plane_to_mi(chroma: bool, coord: usize) -> usize { |
| 6960 | if chroma { coord / 2 } else { coord / 4 } |
| 6961 | } |
| 6962 | |
| 6963 | /// This plane's own transform width in pixels at the covering block, spec |
| 6964 | /// `get_transform_size`'s plane-aware lookup: `tx_grid` stores the luma |
| 6965 | /// width directly; chroma halves it (this decoder's UV transform is always |
| 6966 | /// half its coded block's luma transform, spec's `av1_get_max_uv_txsize` |
| 6967 | /// under 4:2:0 and a block that never splits its own transform). |
| 6968 | fn tx_px_at(n: &Neighbours, chroma: bool, mi_r: usize, mi_c: usize) -> u32 { |
| 6969 | let grid = if chroma { &n.uv_tx_grid } else { &n.tx_grid }; |
| 6970 | grid.get(mi_r * n.skip_grid_cols_mi + mi_c).copied().unwrap_or(0) as u32 |
| 6971 | } |
| 6972 | |
| 6973 | /// [`tx_px_at`]'s HEIGHT twin (lane-rect r2): the deblocker's horizontal |
| 6974 | /// edges step and gate by transform height, which differs from width only |
| 6975 | /// on rect partition strips. |
| 6976 | fn tx_h_px_at(n: &Neighbours, chroma: bool, mi_r: usize, mi_c: usize) -> u32 { |
| 6977 | let grid = if chroma { &n.uv_tx_h_grid } else { &n.tx_h_grid }; |
| 6978 | grid.get(mi_r * n.skip_grid_cols_mi + mi_c).copied().unwrap_or(0) as u32 |
| 6979 | } |
| 6980 | |
| 6981 | /// `get_tx_size_context` (spec 9.3's `TxDepthCtx`, libaom |
| 6982 | /// `TXFM_CONTEXT`/`entropymode.c`): the sum of two boolean terms, each |
| 6983 | /// present only when its neighbour exists inside the true frame -- whether |
| 6984 | /// the block immediately above (at this block's own leftmost mi column) or |
| 6985 | /// to the left (at its own topmost mi row) coded a transform at least as wide |
| 6986 | /// as `own_side`. |
| 6987 | fn tx_size_context(n: &Neighbours, (mi_r, mi_c): (usize, usize), own_side: usize) -> usize { |
| 6988 | let above = mi_r > n.tile_row0_mi && tx_px_at(n, false, mi_r - 1, mi_c) as usize >= own_side; |
| 6989 | let left = mi_c > n.tile_col0_mi && tx_h_px_at(n, false, mi_r, mi_c - 1) as usize >= own_side; |
| 6990 | usize::from(above) + usize::from(left) |
| 6991 | } |
| 6992 | |
| 6993 | /// `get_tx_size_context` as libaom actually writes it (`pred_common.h:342`), |
| 6994 | /// for a block inside an INTER frame: the two `TXFM_CONTEXT` arrays |
| 6995 | /// ([`Neighbours::above_txfm`]/[`Neighbours::left_txfm`], which only the |
| 6996 | /// inter path maintains), except that an *inter* neighbour contributes its |
| 6997 | /// own BLOCK size rather than its transform size -- the two differ the moment |
| 6998 | /// a neighbour's var-tx tree splits. [`tx_size_context`]'s deblock-grid |
| 6999 | /// approximation coincides with this only while every neighbour codes one |
| 7000 | /// whole-block transform, which is exactly what `TxMode::Select` ends. |
| 7001 | fn tx_size_context_txfm(n: &Neighbours, (mi_r, mi_c): (usize, usize), side: usize) -> usize { |
| 7002 | let has_above = mi_r > n.tile_row0_mi; |
| 7003 | let has_left = mi_c > n.tile_col0_mi; |
| 7004 | let mut above = usize::from(n.above_txfm[mi_c]) >= side; |
| 7005 | let mut left = usize::from(n.left_txfm[mi_r]) >= side; |
| 7006 | if has_above && n.above_inter[mi_c / (SUB / MI)] { |
| 7007 | above = n.above_side_mi[mi_c] >= side; |
| 7008 | } |
| 7009 | if has_left && n.left_inter[mi_r / (SUB / MI)] { |
| 7010 | left = n.left_side_mi[mi_r] >= side; |
| 7011 | } |
| 7012 | match (has_above, has_left) { |
| 7013 | (true, true) => usize::from(above) + usize::from(left), |
| 7014 | (true, false) => usize::from(above), |
| 7015 | (false, true) => usize::from(left), |
| 7016 | (false, false) => 0, |
| 7017 | } |
| 7018 | } |
| 7019 | |
| 7020 | /// `read_tx_size`/`read_selected_tx_size` (spec 5.11.16): under |
| 7021 | /// `TxMode::Select`, an intra block's `tx_depth` symbol, resolved straight to |
| 7022 | /// the transform's own side in pixels (`side >> depth`) -- this decoder's |
| 7023 | /// four square categories (8/16/32/64) each halve at most twice, matching |
| 7024 | /// libaom's own per-size depth cap (`cdf::TX_SIZE_CAT0`..`TX_SIZE_CAT3`). |
| 7025 | fn read_tx_size( |
| 7026 | dec: &mut SymbolDecoder, |
| 7027 | cdfs: &mut Cdfs, |
| 7028 | n: &Neighbours, |
| 7029 | at_mi: (usize, usize), |
| 7030 | side: usize, |
| 7031 | // lane-txselect: `get_tx_size_context`'s real context, for the callers |
| 7032 | // that have one -- see [`tx_size_context_txfm`]. `None` keeps the |
| 7033 | // key-frame path's own deblock-grid approximation. |
| 7034 | ctx_override: Option<usize>, |
| 7035 | ) -> usize { |
| 7036 | let ctx = ctx_override.unwrap_or_else(|| tx_size_context(n, at_mi, side)); |
| 7037 | let depth = match side { |
| 7038 | 8 => { |
| 7039 | dec.symbol(&mut cdfs.tx_size_cat0[ctx]) |
| 7040 | } |
| 7041 | 16 => { |
| 7042 | dec.symbol(&mut cdfs.tx_size_cat1[ctx]) |
| 7043 | } |
| 7044 | 32 => { |
| 7045 | dec.symbol(&mut cdfs.tx_size_cat2[ctx]) |
| 7046 | } |
| 7047 | 64 => { |
| 7048 | dec.symbol(&mut cdfs.tx_size_cat3[ctx]) |
| 7049 | } |
| 7050 | _ => unreachable!("decode_block/decode_leaf8 only call this at 8/16/32/64"), |
| 7051 | }; |
| 7052 | if std::env::var_os("EC_TRACE_MODE_STEP").is_some() { |
| 7053 | let (rng, _) = dec.debug_state(); |
| 7054 | let (mi_r, mi_c) = at_mi; |
| 7055 | eprintln!( |
| 7056 | "EC_ISTEP mi_row={mi_r} mi_col={mi_c} name=tx_depth val={depth} ctx={ctx} rng={rng}" |
| 7057 | ); |
| 7058 | } |
| 7059 | if depth != 0 { |
| 7060 | TX_DEPTH_HITS.with(|c| c.set(c.get() + 1)); |
| 7061 | } |
| 7062 | side >> depth |
| 7063 | } |
| 7064 | |
| 7065 | /// `txfm_partition_context` (libaom `av1_common_int.h`): an inter block's |
| 7066 | /// `txfm_split` context -- a category from the block's own largest square |
| 7067 | /// transform and whether this unit already sits below it, times three, plus |
| 7068 | /// the two neighbour terms (whether the transform last written above/left of |
| 7069 | /// this unit is narrower/shorter than the unit itself). |
| 7070 | fn txfm_partition_ctx(above_px: u8, left_px: u8, blk_max_px: usize, tx_px: usize) -> usize { |
| 7071 | let above = usize::from(usize::from(above_px) < tx_px); |
| 7072 | let left = usize::from(usize::from(left_px) < tx_px); |
| 7073 | // `get_sqr_tx_size(max(block_size_wide, block_size_high))`, capped at the |
| 7074 | // largest transform (`TX_64X64`); `TX_4X4`..`TX_64X64` index as |
| 7075 | // `log2(px) - 2`. |
| 7076 | let max_tx = blk_max_px.min(64); |
| 7077 | let max_idx = max_tx.trailing_zeros() as usize - 2; |
| 7078 | let category = usize::from(tx_px != max_tx && max_tx > 8) + (4 - max_idx) * 2; |
| 7079 | category * 3 + above + left |
| 7080 | } |
| 7081 | |
| 7082 | /// `txfm_partition_update` (libaom `av1_common_int.h`): once a var-tx unit |
| 7083 | /// resolves, every mi cell its *parent* unit (`txb_px`) spans records the |
| 7084 | /// resolved transform's own size. |
| 7085 | fn txfm_partition_update( |
| 7086 | n: &mut Neighbours, |
| 7087 | (mi_r, mi_c): (usize, usize), |
| 7088 | tx_px: usize, |
| 7089 | txb_px: usize, |
| 7090 | ) { |
| 7091 | for i in 0..txb_px / MI { |
| 7092 | if let Some(cell) = n.left_txfm.get_mut(mi_r + i) { |
| 7093 | *cell = tx_px as u8; |
| 7094 | } |
| 7095 | if let Some(cell) = n.above_txfm.get_mut(mi_c + i) { |
| 7096 | *cell = tx_px as u8; |
| 7097 | } |
| 7098 | } |
| 7099 | } |
| 7100 | |
| 7101 | /// `set_txfm_ctxs` (libaom `av1_common_int.h`): the non-var-tx write of the |
| 7102 | /// same contexts -- a skipped *inter* block records its own block size rather |
| 7103 | /// than its transform size. |
| 7104 | fn set_txfm_ctxs( |
| 7105 | n: &mut Neighbours, |
| 7106 | (mi_r, mi_c): (usize, usize), |
| 7107 | tx_px: usize, |
| 7108 | w_mi: usize, |
| 7109 | h_mi: usize, |
| 7110 | skip_inter: bool, |
| 7111 | ) { |
| 7112 | let bw = if skip_inter { w_mi * MI } else { tx_px }; |
| 7113 | let bh = if skip_inter { h_mi * MI } else { tx_px }; |
| 7114 | for i in 0..w_mi { |
| 7115 | if let Some(cell) = n.above_txfm.get_mut(mi_c + i) { |
| 7116 | *cell = bw as u8; |
| 7117 | } |
| 7118 | } |
| 7119 | for i in 0..h_mi { |
| 7120 | if let Some(cell) = n.left_txfm.get_mut(mi_r + i) { |
| 7121 | *cell = bh as u8; |
| 7122 | } |
| 7123 | } |
| 7124 | } |
| 7125 | |
| 7126 | /// The value libaom initialises both transform contexts to -- NOT zero: |
| 7127 | /// `av1_zero_above_context` memsets the txfm row to |
| 7128 | /// `tx_size_wide[TX_SIZES_LARGEST]` and `av1_zero_left_context` does the same |
| 7129 | /// with `tx_size_high[TX_SIZES_LARGEST]` (`av1_common_int.h`), i.e. 64 |
| 7130 | /// pixels, so an unwritten neighbour reads as the *widest* possible |
| 7131 | /// transform, not the narrowest. |
| 7132 | const TXFM_CTX_INIT: u8 = 64; |
| 7133 | |
| 7134 | /// libaom's `MAX_VARTX_DEPTH`: an inter block's transform tree halves at most |
| 7135 | /// twice below its own largest transform. |
| 7136 | const MAX_VARTX_DEPTH: usize = 2; |
| 7137 | |
| 7138 | /// `read_var_tx_size` (spec 5.11.17, libaom `decodeframe.c` |
| 7139 | /// `read_tx_size_vartx`): the recursive `txfm_split` tree of one |
| 7140 | /// max-transform-sized unit of an inter block, collecting the resolved leaf |
| 7141 | /// transform units into `out` as `(row offset, col offset, side)` in mi units |
| 7142 | /// -- exactly the order `decode_reconstruct_tx` later reads their |
| 7143 | /// coefficients in. `blk_row`/`blk_col` are mi offsets inside the block; |
| 7144 | /// `max_w_mi`/`max_h_mi` clip it to the true frame edge (`max_block_wide`/ |
| 7145 | /// `max_block_high`). |
| 7146 | #[allow(clippy::too_many_arguments)] |
| 7147 | fn read_var_tx_size( |
| 7148 | dec: &mut SymbolDecoder, |
| 7149 | cdfs: &mut Cdfs, |
| 7150 | n: &mut Neighbours, |
| 7151 | at_mi: (usize, usize), |
| 7152 | blk_max_px: usize, |
| 7153 | (max_w_mi, max_h_mi): (usize, usize), |
| 7154 | tx_px: usize, |
| 7155 | depth: usize, |
| 7156 | blk_row: usize, |
| 7157 | blk_col: usize, |
| 7158 | out: &mut Vec<(usize, usize, usize)>, |
| 7159 | ) { |
| 7160 | if blk_row >= max_h_mi || blk_col >= max_w_mi { |
| 7161 | return; |
| 7162 | } |
| 7163 | let unit = (at_mi.0 + blk_row, at_mi.1 + blk_col); |
| 7164 | if depth == MAX_VARTX_DEPTH { |
| 7165 | out.push((blk_row, blk_col, tx_px)); |
| 7166 | txfm_partition_update(n, unit, tx_px, tx_px); |
| 7167 | return; |
| 7168 | } |
| 7169 | let ctx = txfm_partition_ctx(n.above_txfm[unit.1], n.left_txfm[unit.0], blk_max_px, tx_px); |
| 7170 | let split = dec.symbol(&mut cdfs.txfm_partition[ctx]) == 1; |
| 7171 | TXFM_SPLIT_READS.with(|c| c.set(c.get() + 1)); |
| 7172 | if std::env::var_os("EC_TRACE_MODE_STEP").is_some() { |
| 7173 | let (rng, _) = dec.debug_state(); |
| 7174 | eprintln!( |
| 7175 | "EC_ISTEP mi_row={} mi_col={} name=txfm_split val={} ctx={ctx} rng={rng}", |
| 7176 | unit.0, |
| 7177 | unit.1, |
| 7178 | u8::from(split) |
| 7179 | ); |
| 7180 | } |
| 7181 | if !split { |
| 7182 | out.push((blk_row, blk_col, tx_px)); |
| 7183 | txfm_partition_update(n, unit, tx_px, tx_px); |
| 7184 | return; |
| 7185 | } |
| 7186 | TXFM_SPLIT_HITS.with(|c| c.set(c.get() + 1)); |
| 7187 | let sub = tx_px / 2; |
| 7188 | if sub == 4 { |
| 7189 | // libaom's `sub_txs == TX_4X4` early return: the whole 8x8 unit is |
| 7190 | // marked 4x4 (four transform units, raster order) without recursing. |
| 7191 | for row in 0..2 { |
| 7192 | for col in 0..2 { |
| 7193 | if blk_row + row < max_h_mi && blk_col + col < max_w_mi { |
| 7194 | out.push((blk_row + row, blk_col + col, 4)); |
| 7195 | } |
| 7196 | } |
| 7197 | } |
| 7198 | txfm_partition_update(n, unit, 4, tx_px); |
| 7199 | return; |
| 7200 | } |
| 7201 | for row in (0..tx_px / MI).step_by(sub / MI) { |
| 7202 | for col in (0..tx_px / MI).step_by(sub / MI) { |
| 7203 | read_var_tx_size( |
| 7204 | dec, |
| 7205 | cdfs, |
| 7206 | n, |
| 7207 | at_mi, |
| 7208 | blk_max_px, |
| 7209 | (max_w_mi, max_h_mi), |
| 7210 | sub, |
| 7211 | depth + 1, |
| 7212 | blk_row + row, |
| 7213 | blk_col + col, |
| 7214 | out, |
| 7215 | ); |
| 7216 | } |
| 7217 | } |
| 7218 | } |
| 7219 | |
| 7220 | /// `read_block_tx_size` (libaom `decodeframe.c` `parse_decode_block`'s tx |
| 7221 | /// branch): every block of a `TxMode::Select` inter frame codes its transform |
| 7222 | /// size right after its own mode info -- an inter non-skip block through the |
| 7223 | /// recursive var-tx tree ([`read_var_tx_size`], whose leaf list this returns), |
| 7224 | /// every other block through the intra `tx_depth` symbol (a skipped inter |
| 7225 | /// block codes no symbol at all) plus a `set_txfm_ctxs` write. Returns the |
| 7226 | /// block's own resolved transform side and the leaf list, the latter only |
| 7227 | /// when the tree resolved to more than one transform unit. |
| 7228 | #[allow(clippy::too_many_arguments)] |
| 7229 | fn read_block_tx_size( |
| 7230 | dec: &mut SymbolDecoder, |
| 7231 | cdfs: &mut Cdfs, |
| 7232 | n: &mut Neighbours, |
| 7233 | at_mi: (usize, usize), |
| 7234 | side: usize, |
| 7235 | (mi_cols, mi_rows): (usize, usize), |
| 7236 | is_inter: bool, |
| 7237 | skip: bool, |
| 7238 | ) -> Result<(usize, Option<Vec<(usize, usize, usize)>>)> { |
| 7239 | if !TX_SELECT_INTER.with(std::cell::Cell::get) { |
| 7240 | return Ok((side, None)); |
| 7241 | } |
| 7242 | let side_mi = side / MI; |
| 7243 | if is_inter && !skip { |
| 7244 | let max_tx = side.min(64); |
| 7245 | let max_w_mi = side_mi.min(mi_cols.saturating_sub(at_mi.1)); |
| 7246 | let max_h_mi = side_mi.min(mi_rows.saturating_sub(at_mi.0)); |
| 7247 | let mut leaves = Vec::new(); |
| 7248 | let step = max_tx / MI; |
| 7249 | for row in (0..side_mi).step_by(step) { |
| 7250 | for col in (0..side_mi).step_by(step) { |
| 7251 | read_var_tx_size( |
| 7252 | dec, |
| 7253 | cdfs, |
| 7254 | n, |
| 7255 | at_mi, |
| 7256 | side, |
| 7257 | (max_w_mi, max_h_mi), |
| 7258 | max_tx, |
| 7259 | 0, |
| 7260 | row, |
| 7261 | col, |
| 7262 | &mut leaves, |
| 7263 | ); |
| 7264 | } |
| 7265 | } |
| 7266 | let single = leaves.len() == 1 && leaves[0].2 == side; |
| 7267 | if !single && leaves.iter().any(|leaf| leaf.2 > 32) { |
| 7268 | // Only a block wider than a superblock's own 64x64 max transform |
| 7269 | // reaches here; this crate has no 64-point *inter* coefficient |
| 7270 | // table set ([`inter_txbset_for`] stops at 32). |
| 7271 | return Err(unsupported( |
| 7272 | "an inter var-tx tree with a leaf transform larger than 32x32", |
| 7273 | )); |
| 7274 | } |
| 7275 | let resolved = leaves.last().map_or(side, |l| l.2); |
| 7276 | return Ok((resolved, (!single).then_some(leaves))); |
| 7277 | } |
| 7278 | let tx = if is_inter { |
| 7279 | side.min(64) |
| 7280 | } else { |
| 7281 | let resolved = read_tx_size( |
| 7282 | dec, |
| 7283 | cdfs, |
| 7284 | n, |
| 7285 | at_mi, |
| 7286 | side, |
| 7287 | Some(tx_size_context_txfm(n, at_mi, side)), |
| 7288 | ); |
| 7289 | if resolved != side { |
| 7290 | // An intra block inside an inter frame whose `tx_depth` splits its |
| 7291 | // luma transform needs the per-transform-unit intra prediction |
| 7292 | // loop `decode_block` runs for a key frame; this path predicts the |
| 7293 | // whole block at once, so refuse rather than mis-reconstruct. |
| 7294 | return Err(unsupported( |
| 7295 | "an intra block in an inter frame whose tx_depth splits its luma transform \ |
| 7296 | (round 1)", |
| 7297 | )); |
| 7298 | } |
| 7299 | resolved |
| 7300 | }; |
| 7301 | set_txfm_ctxs(n, at_mi, tx, side_mi, side_mi, skip && is_inter); |
| 7302 | Ok((tx, None)) |
| 7303 | } |
| 7304 | |
| 7305 | /// One 8x8 inter leaf's luma residual (lane-txselect): either the whole-8x8 |
| 7306 | /// transform, or -- when [`read_block_tx_size`]'s var-tx tree took the single |
| 7307 | /// split a `BLOCK_8X8` can take (`split`) -- the four 4x4 transform units in |
| 7308 | /// raster order, each reading its own coefficient context off the units |
| 7309 | /// already decoded before it. Returns the block's luma grid (all-zero in the |
| 7310 | /// split case, whose reconstruction has already happened unit by unit) and |
| 7311 | /// the `tx_type` chroma inherits: `av1_get_tx_type`'s colocated luma lookup, |
| 7312 | /// which for a split block lands on the top-left unit. |
| 7313 | #[allow(clippy::too_many_arguments)] |
| 7314 | fn read_inter_luma8( |
| 7315 | dec: &mut SymbolDecoder, |
| 7316 | cdfs: &mut Cdfs, |
| 7317 | neighbours: &mut Neighbours, |
| 7318 | leaf_mi: (usize, usize), |
| 7319 | y: &mut PlaneBuf, |
| 7320 | px: usize, |
| 7321 | py: usize, |
| 7322 | pred_y: &[u16], |
| 7323 | mode_for_tx: usize, |
| 7324 | base_q_idx: u8, |
| 7325 | scan8: &[u16], |
| 7326 | scan4: &[u16], |
| 7327 | reduced_tx_set: bool, |
| 7328 | split: bool, |
| 7329 | ) -> Result<(Vec<i32>, TxType)> { |
| 7330 | if !split { |
| 7331 | return read_inter_plane( |
| 7332 | dec, |
| 7333 | cdfs, |
| 7334 | if reduced_tx_set { |
| 7335 | TxbSet::Luma8Inter |
| 7336 | } else { |
| 7337 | TxbSet::Luma8InterSet1 |
| 7338 | }, |
| 7339 | scan8, |
| 7340 | 0, |
| 7341 | neighbours.around_mi(leaf_mi, 8)[0], |
| 7342 | mode_for_tx, |
| 7343 | y, |
| 7344 | px, |
| 7345 | py, |
| 7346 | 8, |
| 7347 | base_q_idx, |
| 7348 | pred_y, |
| 7349 | None, |
| 7350 | None, |
| 7351 | ); |
| 7352 | } |
| 7353 | let mut first = TxType::DctDct; |
| 7354 | for tu_row in 0..2 { |
| 7355 | for tu_col in 0..2 { |
| 7356 | let tu_mi = (leaf_mi.0 + tu_row, leaf_mi.1 + tu_col); |
| 7357 | let mut tu_pred = Vec::with_capacity(16); |
| 7358 | for rr in 0..4 { |
| 7359 | let start = (tu_row * 4 + rr) * 8 + tu_col * 4; |
| 7360 | tu_pred.extend_from_slice(&pred_y[start..start + 4]); |
| 7361 | } |
| 7362 | let tu_skip_ctx = neighbours.luma_skip_ctx(tu_mi, 1); |
| 7363 | let (tu_grid, tu_tx_type) = read_inter_plane( |
| 7364 | dec, |
| 7365 | cdfs, |
| 7366 | if reduced_tx_set { |
| 7367 | TxbSet::Luma4Inter |
| 7368 | } else { |
| 7369 | TxbSet::Luma4InterSet1 |
| 7370 | }, |
| 7371 | scan4, |
| 7372 | 0, |
| 7373 | neighbours.around_mi(tu_mi, 4)[0], |
| 7374 | mode_for_tx, |
| 7375 | y, |
| 7376 | px + tu_col * 4, |
| 7377 | py + tu_row * 4, |
| 7378 | 4, |
| 7379 | base_q_idx, |
| 7380 | &tu_pred, |
| 7381 | None, |
| 7382 | Some(tu_skip_ctx), |
| 7383 | )?; |
| 7384 | if tu_row == 0 && tu_col == 0 { |
| 7385 | first = tu_tx_type; |
| 7386 | } |
| 7387 | neighbours.record_mi_luma(tu_mi, 4, &tu_grid); |
| 7388 | } |
| 7389 | } |
| 7390 | Ok((vec![0i32; 64], first)) |
| 7391 | } |
| 7392 | |
| 7393 | /// The [`TxbSet`] one leaf of an inter block's var-tx tree reads its |
| 7394 | /// coefficient tables from -- the inter counterpart of [`txbset_for`]. |
| 7395 | fn inter_txbset_for(tx_px: usize, reduced_tx_set: bool) -> TxbSet { |
| 7396 | match tx_px { |
| 7397 | 32 => TxbSet::Luma32Inter, |
| 7398 | 16 if reduced_tx_set => TxbSet::Luma16Inter, |
| 7399 | 16 => TxbSet::Luma16InterSet1, |
| 7400 | 8 if reduced_tx_set => TxbSet::Luma8Inter, |
| 7401 | 8 => TxbSet::Luma8InterSet1, |
| 7402 | 4 if reduced_tx_set => TxbSet::Luma4Inter, |
| 7403 | 4 => TxbSet::Luma4InterSet1, |
| 7404 | _ => unreachable!("a var-tx leaf is never wider than a 64x64 block's 32x32 sub-transform"), |
| 7405 | } |
| 7406 | } |
| 7407 | |
| 7408 | /// The luma coefficient scan table for a resolved transform side -- 32/16/8/4, |
| 7409 | /// the only four sizes a `TxMode::Select` block's coded coefficient grid |
| 7410 | /// ([`read_tx_size`]'s resolved side, or 32 at the 64x64 corner case) can be. |
| 7411 | fn scan_for<'a>( |
| 7412 | tx_px: usize, |
| 7413 | scan32: &'a [u16], |
| 7414 | scan16: &'a [u16], |
| 7415 | scan8: &'a [u16], |
| 7416 | scan4: &'a [u16], |
| 7417 | ) -> &'a [u16] { |
| 7418 | match tx_px { |
| 7419 | 32 => scan32, |
| 7420 | 16 => scan16, |
| 7421 | 8 => scan8, |
| 7422 | 4 => scan4, |
| 7423 | _ => unreachable!("read_tx_size never resolves the coefficient grid to anything else"), |
| 7424 | } |
| 7425 | } |
| 7426 | |
| 7427 | /// The [`TxbSet`] a resolved luma transform side reads its coefficient tables |
| 7428 | /// from -- 64 is only ever the 64x64-block, depth-0, corner-scanned case |
| 7429 | /// ([`TxbSet::Luma64`]); every other resolved side maps to its own matching |
| 7430 | /// set. `get_tx_set` (spec 5.11.48) only splits on `reduced_tx_set` at |
| 7431 | /// `TX_8X8`/`TX_4X4`: an intra `TX_16X16` always reads `TX_SET_INTRA_2` |
| 7432 | /// regardless of the flag (`av1_get_ext_tx_set_type`'s |
| 7433 | /// `tx_size_sqr == TX_16X16` branch ignores `use_reduced_set` once it is past |
| 7434 | /// the `TX_32X32`/reduced-set checks), so only the 8x8 and 4x4 cases have a |
| 7435 | /// second table ([`TxbSet::Luma8Set1`]/[`TxbSet::Luma4Set1`]). |
| 7436 | /// [`txbset_for`], for a block libaom's `is_inter_block` calls inter -- which |
| 7437 | /// an intrabc block is (`blockd.h:373`), so its `tx_type` comes off the |
| 7438 | /// inter `ext_tx` sets, never the mode-indexed intra ones. |
| 7439 | fn txbset_for_inter(tx_px: usize, reduced_tx_set: bool) -> TxbSet { |
| 7440 | match tx_px { |
| 7441 | 64 => TxbSet::Luma64, |
| 7442 | 32 => TxbSet::Luma32Inter, |
| 7443 | 16 if reduced_tx_set => TxbSet::Luma16Inter, |
| 7444 | 16 => TxbSet::Luma16InterSet1, |
| 7445 | 8 if reduced_tx_set => TxbSet::Luma8Inter, |
| 7446 | 8 => TxbSet::Luma8InterSet1, |
| 7447 | _ => TxbSet::Luma64, |
| 7448 | } |
| 7449 | } |
| 7450 | |
| 7451 | fn txbset_for(tx_px: usize, reduced_tx_set: bool) -> TxbSet { |
| 7452 | match tx_px { |
| 7453 | 64 => TxbSet::Luma64, |
| 7454 | 32 => TxbSet::Luma32, |
| 7455 | 16 => TxbSet::Luma16, |
| 7456 | 8 if reduced_tx_set => TxbSet::Luma8, |
| 7457 | 8 => TxbSet::Luma8Set1, |
| 7458 | 4 if reduced_tx_set => TxbSet::Luma4, |
| 7459 | 4 => TxbSet::Luma4Set1, |
| 7460 | _ => unreachable!("read_tx_size never resolves a block's own side to anything else"), |
| 7461 | } |
| 7462 | } |
| 7463 | |
| 7464 | /// `tx_size_wide_unit_log2`/`tx_size_high_unit_log2`: `log2(px / 4)`, the |
| 7465 | /// index [`tx_dim_to_filter_length`] and the chroma 4-vs-6 choice key on. |
| 7466 | fn unit_log2(px: u32) -> i32 { |
| 7467 | if px == 0 { |
| 7468 | 0 |
| 7469 | } else { |
| 7470 | px.trailing_zeros() as i32 - 2 |
| 7471 | } |
| 7472 | } |
| 7473 | |
| 7474 | /// spec 7.14.4's `get_filter_level`: `ref_deltas` is indexed by the block's |
| 7475 | /// reference frame, `mode_deltas` by `mode_lf_lut[mode]` -- `0` for |
| 7476 | /// `GLOBALMV`, `1` for `NEARESTMV`/`NEARMV`/`NEWMV` (intra gets no mode |
| 7477 | /// delta). `ref_frame` here is [`Neighbours::ref_grid`]'s packed cell: |
| 7478 | /// magnitude = reference (`0` = intra), sign = `GLOBALMV` (lane-av1golden2: |
| 7479 | /// the flat `mode_deltas[1]` this used before was a `LAST_FRAME`-reachable |
| 7480 | /// wrong delta on every `GLOBALMV` block, masked while any `GOLDEN_FRAME` |
| 7481 | /// block anywhere aborted the whole stream's decode before comparison). |
| 7482 | fn lf_level( |
| 7483 | lf: &LoopFilterParams, |
| 7484 | plane_idx: usize, |
| 7485 | dir: usize, |
| 7486 | ref_frame: i8, |
| 7487 | delta_lf: i32, |
| 7488 | segment_id: u8, |
| 7489 | ) -> i32 { |
| 7490 | let base = i32::from(if plane_idx == 0 { |
| 7491 | lf.level[dir] |
| 7492 | } else if plane_idx == 1 { |
| 7493 | lf.level[2] |
| 7494 | } else { |
| 7495 | lf.level[3] |
| 7496 | }); |
| 7497 | // spec 7.14.4: the per-superblock `DeltaLF` term applies before the |
| 7498 | // ref/mode delta scaling below, and regardless of `delta_enabled` |
| 7499 | // (that flag only gates `ref_deltas`/`mode_deltas`). |
| 7500 | let base = (base + delta_lf).clamp(0, 63); |
| 7501 | // lane-seg, spec 7.14.4 / libaom `av1_loop_filter_frame_init`: this |
| 7502 | // block's segment's own `SEG_LVL_ALT_LF_*` delta applies after the |
| 7503 | // per-superblock `DeltaLF` term and before the ref/mode deltas below. |
| 7504 | // Feature index: `SEG_LVL_ALT_LF_Y_V + dir` for luma, `_U`/`_V` (3/4) |
| 7505 | // for chroma. |
| 7506 | let feature = if plane_idx == 0 { SEG_LVL_ALT_LF_Y_V + dir } else { plane_idx + 2 }; |
| 7507 | let base = match seg_feature(usize::from(segment_id), feature) { |
| 7508 | Some(data) => (base + data).clamp(0, 63), |
| 7509 | None => base, |
| 7510 | }; |
| 7511 | if !lf.delta_enabled { |
| 7512 | return base; |
| 7513 | } |
| 7514 | let scale = 1i32 << (base >> 5); |
| 7515 | let is_inter = ref_frame != 0; |
| 7516 | let is_globalmv = ref_frame < 0; |
| 7517 | let ref_idx = ref_frame.unsigned_abs() as usize; |
| 7518 | let mut lvl = base + i32::from(lf.ref_deltas[ref_idx]) * scale; |
| 7519 | if is_inter { |
| 7520 | lvl += i32::from(lf.mode_deltas[usize::from(!is_globalmv)]) * scale; |
| 7521 | } |
| 7522 | lvl.clamp(0, 63) |
| 7523 | } |
| 7524 | |
| 7525 | /// Spec 7.14.2's `set_lpf_parameters`, restricted to the uniform-level, |
| 7526 | /// no-segmentation, no-delta-LF path and to this decoder's own invariant |
| 7527 | /// that a coded block's transform is always its own full size -- so a TU |
| 7528 | /// edge is always also a PU edge, and the "both sides skipped, not a PU |
| 7529 | /// edge" suppression (which only ever fires at a non-PU-edge TU boundary) |
| 7530 | /// never applies here, letting this skip the `skip`/`skip_txfm` lookup |
| 7531 | /// entirely. Returns `None` when this 4-pixel edge group is not filtered. |
| 7532 | fn edge_params( |
| 7533 | lf: &LoopFilterParams, |
| 7534 | n: &Neighbours, |
| 7535 | plane_idx: usize, |
| 7536 | dir: usize, |
| 7537 | chroma: bool, |
| 7538 | x0: usize, |
| 7539 | y0: usize, |
| 7540 | ) -> Option<(u8, i32)> { |
| 7541 | let (mi_r, mi_c) = (plane_to_mi(chroma, y0), plane_to_mi(chroma, x0)); |
| 7542 | let cur_tx = if dir == 0 { |
| 7543 | tx_px_at(n, chroma, mi_r, mi_c) |
| 7544 | } else { |
| 7545 | tx_h_px_at(n, chroma, mi_r, mi_c) |
| 7546 | }; |
| 7547 | if cur_tx == 0 || (if dir == 0 { x0 } else { y0 }) % cur_tx as usize != 0 { |
| 7548 | return None; |
| 7549 | } |
| 7550 | let cur_ref = n.ref_grid[mi_r * n.skip_grid_cols_mi + mi_c]; |
| 7551 | let (pv_mi_r, pv_mi_c) = if dir == 0 { |
| 7552 | (mi_r, mi_c - 1) |
| 7553 | } else { |
| 7554 | (mi_r - 1, mi_c) |
| 7555 | }; |
| 7556 | let pv_tx = if dir == 0 { |
| 7557 | tx_px_at(n, chroma, pv_mi_r, pv_mi_c) |
| 7558 | } else { |
| 7559 | tx_h_px_at(n, chroma, pv_mi_r, pv_mi_c) |
| 7560 | }; |
| 7561 | let pv_ref = n.ref_grid[pv_mi_r * n.skip_grid_cols_mi + pv_mi_c]; |
| 7562 | let delta_idx = if plane_idx == 0 { dir } else { plane_idx + 1 }; |
| 7563 | let cur_delta_lf = i32::from(n.delta_lf_grid[mi_r * n.skip_grid_cols_mi + mi_c][delta_idx]); |
| 7564 | let pv_delta_lf = i32::from(n.delta_lf_grid[pv_mi_r * n.skip_grid_cols_mi + pv_mi_c][delta_idx]); |
| 7565 | |
| 7566 | let cur_level = lf_level(lf, plane_idx, dir, cur_ref, cur_delta_lf, segment_id_at(mi_r, mi_c)); |
| 7567 | let pv_level = lf_level( |
| 7568 | lf, |
| 7569 | plane_idx, |
| 7570 | dir, |
| 7571 | pv_ref, |
| 7572 | pv_delta_lf, |
| 7573 | segment_id_at(pv_mi_r, pv_mi_c), |
| 7574 | ); |
| 7575 | if cur_level == 0 && pv_level == 0 { |
| 7576 | return None; |
| 7577 | } |
| 7578 | let dim = unit_log2(cur_tx).min(unit_log2(pv_tx)).clamp(0, 4) as usize; |
| 7579 | let len: u8 = if chroma { |
| 7580 | if dim == 0 { 4 } else { 6 } |
| 7581 | } else { |
| 7582 | [4, 8, 14, 14, 14][dim] |
| 7583 | }; |
| 7584 | let level = if cur_level != 0 { cur_level } else { pv_level }; |
| 7585 | DEBLOCK_HITS.with(|c| c.set(c.get() + 1)); |
| 7586 | Some((len, level)) |
| 7587 | } |
| 7588 | |
| 7589 | /// `update_sharpness`'s per-level `(blimit, limit, hev_thr)` triple. |
| 7590 | fn deblock_thresholds(level: i32, sharpness: u8) -> (i32, i32, i32) { |
| 7591 | let sharpness = i32::from(sharpness); |
| 7592 | let shift = i32::from(sharpness > 0) + i32::from(sharpness > 4); |
| 7593 | let mut lim = level >> shift; |
| 7594 | if sharpness > 0 { |
| 7595 | lim = lim.min(9 - sharpness); |
| 7596 | } |
| 7597 | lim = lim.max(1); |
| 7598 | (2 * (level + 2) + lim, lim, level >> 4) |
| 7599 | } |
| 7600 | |
| 7601 | fn sclamp(v: i32) -> i32 { |
| 7602 | // libaom's `signed_char_clamp_high`: the clamp range scales with |
| 7603 | // `128 << (bit_depth - 8)` alongside the centering constant below. |
| 7604 | let scale = 1i32 << (bit_depth() - 8); |
| 7605 | v.clamp(-128 * scale, 128 * scale - 1) |
| 7606 | } |
| 7607 | |
| 7608 | fn round_pow2(v: i32, n: u32) -> i32 { |
| 7609 | (v + (1 << (n - 1))) >> n |
| 7610 | } |
| 7611 | |
| 7612 | /// `filter4`: the narrow 4-tap kernel, and every wider kernel's non-flat |
| 7613 | /// fallback. |
| 7614 | fn filter4(mask: bool, thresh: i32, p1: i32, p0: i32, q0: i32, q1: i32) -> [i32; 4] { |
| 7615 | if !mask { |
| 7616 | return [p1, p0, q0, q1]; |
| 7617 | } |
| 7618 | let hev = (p1 - p0).abs() > thresh || (q1 - q0).abs() > thresh; |
| 7619 | let centre = 128i32 << (bit_depth() - 8); |
| 7620 | let (ps1, ps0, qs0, qs1) = (p1 - centre, p0 - centre, q0 - centre, q1 - centre); |
| 7621 | let outer = if hev { sclamp(ps1 - qs1) } else { 0 }; |
| 7622 | let filter = sclamp(outer + 3 * (qs0 - ps0)); |
| 7623 | let filter1 = sclamp(filter + 4) >> 3; |
| 7624 | let filter2 = sclamp(filter + 3) >> 3; |
| 7625 | let oq0 = sclamp(qs0 - filter1) + centre; |
| 7626 | let op0 = sclamp(ps0 + filter2) + centre; |
| 7627 | let tap = if hev { 0 } else { round_pow2(filter1, 1) }; |
| 7628 | let oq1 = sclamp(qs1 - tap) + centre; |
| 7629 | let op1 = sclamp(ps1 + tap) + centre; |
| 7630 | [op1, op0, oq0, oq1] |
| 7631 | } |
| 7632 | |
| 7633 | /// `filter6`: chroma's flat 5-tap smoothing (spec `[1,2,2,2,1]`) over |
| 7634 | /// `filter4`'s narrow taps. |
| 7635 | #[allow(clippy::too_many_arguments)] |
| 7636 | fn filter6( |
| 7637 | mask: bool, |
| 7638 | thresh: i32, |
| 7639 | flat: bool, |
| 7640 | p2: i32, |
| 7641 | p1: i32, |
| 7642 | p0: i32, |
| 7643 | q0: i32, |
| 7644 | q1: i32, |
| 7645 | q2: i32, |
| 7646 | ) -> [i32; 4] { |
| 7647 | if flat && mask { |
| 7648 | [ |
| 7649 | round_pow2(p2 * 3 + p1 * 2 + p0 * 2 + q0, 3), |
| 7650 | round_pow2(p2 + p1 * 2 + p0 * 2 + q0 * 2 + q1, 3), |
| 7651 | round_pow2(p1 + p0 * 2 + q0 * 2 + q1 * 2 + q2, 3), |
| 7652 | round_pow2(p0 + q0 * 2 + q1 * 2 + q2 * 3, 3), |
| 7653 | ] |
| 7654 | } else { |
| 7655 | filter4(mask, thresh, p1, p0, q0, q1) |
| 7656 | } |
| 7657 | } |
| 7658 | |
| 7659 | /// `filter8`: luma's flat 7-tap smoothing (spec `[1,1,1,2,1,1,1]`). |
| 7660 | #[allow(clippy::too_many_arguments)] |
| 7661 | fn filter8( |
| 7662 | mask: bool, |
| 7663 | thresh: i32, |
| 7664 | flat: bool, |
| 7665 | p3: i32, |
| 7666 | p2: i32, |
| 7667 | p1: i32, |
| 7668 | p0: i32, |
| 7669 | q0: i32, |
| 7670 | q1: i32, |
| 7671 | q2: i32, |
| 7672 | q3: i32, |
| 7673 | ) -> [i32; 6] { |
| 7674 | if flat && mask { |
| 7675 | [ |
| 7676 | round_pow2(p3 + p3 + p3 + 2 * p2 + p1 + p0 + q0, 3), |
| 7677 | round_pow2(p3 + p3 + p2 + 2 * p1 + p0 + q0 + q1, 3), |
| 7678 | round_pow2(p3 + p2 + p1 + 2 * p0 + q0 + q1 + q2, 3), |
| 7679 | round_pow2(p2 + p1 + p0 + 2 * q0 + q1 + q2 + q3, 3), |
| 7680 | round_pow2(p1 + p0 + q0 + 2 * q1 + q2 + q3 + q3, 3), |
| 7681 | round_pow2(p0 + q0 + q1 + 2 * q2 + q3 + q3 + q3, 3), |
| 7682 | ] |
| 7683 | } else { |
| 7684 | let [op1, op0, oq0, oq1] = filter4(mask, thresh, p1, p0, q0, q1); |
| 7685 | [p2, op1, op0, oq0, oq1, q2] |
| 7686 | } |
| 7687 | } |
| 7688 | |
| 7689 | /// `filter14`: the wide 13-tap smoothing used at 16x16-and-larger transform |
| 7690 | /// edges; falls back to [`filter8`] (leaving `op6`/`oq6` untouched either |
| 7691 | /// way -- they are only ever taps here, never written). |
| 7692 | #[allow(clippy::too_many_arguments)] |
| 7693 | fn filter14( |
| 7694 | mask: bool, |
| 7695 | thresh: i32, |
| 7696 | flat: bool, |
| 7697 | flat2: bool, |
| 7698 | p6: i32, |
| 7699 | p5: i32, |
| 7700 | p4: i32, |
| 7701 | p3: i32, |
| 7702 | p2: i32, |
| 7703 | p1: i32, |
| 7704 | p0: i32, |
| 7705 | q0: i32, |
| 7706 | q1: i32, |
| 7707 | q2: i32, |
| 7708 | q3: i32, |
| 7709 | q4: i32, |
| 7710 | q5: i32, |
| 7711 | q6: i32, |
| 7712 | ) -> [i32; 12] { |
| 7713 | if flat2 && flat && mask { |
| 7714 | [ |
| 7715 | round_pow2(p6 * 7 + p5 * 2 + p4 * 2 + p3 + p2 + p1 + p0 + q0, 4), |
| 7716 | round_pow2( |
| 7717 | p6 * 5 + p5 * 2 + p4 * 2 + p3 * 2 + p2 + p1 + p0 + q0 + q1, |
| 7718 | 4, |
| 7719 | ), |
| 7720 | round_pow2( |
| 7721 | p6 * 4 + p5 + p4 * 2 + p3 * 2 + p2 * 2 + p1 + p0 + q0 + q1 + q2, |
| 7722 | 4, |
| 7723 | ), |
| 7724 | round_pow2( |
| 7725 | p6 * 3 + p5 + p4 + p3 * 2 + p2 * 2 + p1 * 2 + p0 + q0 + q1 + q2 + q3, |
| 7726 | 4, |
| 7727 | ), |
| 7728 | round_pow2( |
| 7729 | p6 * 2 + p5 + p4 + p3 + p2 * 2 + p1 * 2 + p0 * 2 + q0 + q1 + q2 + q3 + q4, |
| 7730 | 4, |
| 7731 | ), |
| 7732 | round_pow2( |
| 7733 | p6 + p5 + p4 + p3 + p2 + p1 * 2 + p0 * 2 + q0 * 2 + q1 + q2 + q3 + q4 + q5, |
| 7734 | 4, |
| 7735 | ), |
| 7736 | round_pow2( |
| 7737 | p5 + p4 + p3 + p2 + p1 + p0 * 2 + q0 * 2 + q1 * 2 + q2 + q3 + q4 + q5 + q6, |
| 7738 | 4, |
| 7739 | ), |
| 7740 | round_pow2( |
| 7741 | p4 + p3 + p2 + p1 + p0 + q0 * 2 + q1 * 2 + q2 * 2 + q3 + q4 + q5 + q6 * 2, |
| 7742 | 4, |
| 7743 | ), |
| 7744 | round_pow2( |
| 7745 | p3 + p2 + p1 + p0 + q0 + q1 * 2 + q2 * 2 + q3 * 2 + q4 + q5 + q6 * 3, |
| 7746 | 4, |
| 7747 | ), |
| 7748 | round_pow2( |
| 7749 | p2 + p1 + p0 + q0 + q1 + q2 * 2 + q3 * 2 + q4 * 2 + q5 + q6 * 4, |
| 7750 | 4, |
| 7751 | ), |
| 7752 | round_pow2( |
| 7753 | p1 + p0 + q0 + q1 + q2 + q3 * 2 + q4 * 2 + q5 * 2 + q6 * 5, |
| 7754 | 4, |
| 7755 | ), |
| 7756 | round_pow2(p0 + q0 + q1 + q2 + q3 + q4 * 2 + q5 * 2 + q6 * 7, 4), |
| 7757 | ] |
| 7758 | } else { |
| 7759 | let [op2, op1, op0, oq0, oq1, oq2] = |
| 7760 | filter8(mask, thresh, flat, p3, p2, p1, p0, q0, q1, q2, q3); |
| 7761 | [p5, p4, p3, op2, op1, op0, oq0, oq1, oq2, q3, q4, q5] |
| 7762 | } |
| 7763 | } |
| 7764 | |
| 7765 | /// Filters one 4-pixel edge group: `base` is the plane-local byte offset of |
| 7766 | /// the first (`q0`) sample past the edge, `outer_stride` advances to the |
| 7767 | /// next of the 4 perpendicular samples, `tap_stride` steps from one tap to |
| 7768 | /// the next along the edge's own filtering direction. |
| 7769 | #[allow(clippy::too_many_arguments)] |
| 7770 | fn filter_edge( |
| 7771 | data: &mut [u16], |
| 7772 | base: usize, |
| 7773 | outer_stride: isize, |
| 7774 | tap_stride: isize, |
| 7775 | len: u8, |
| 7776 | level: i32, |
| 7777 | sharpness: u8, |
| 7778 | ) { |
| 7779 | let (blimit, limit, hev_thr) = deblock_thresholds(level, sharpness); |
| 7780 | // libaom `highbd_filter_mask*`/`highbd_flat_mask4`/`highbd_hev_mask`: |
| 7781 | // every 8-bit threshold (including the flat masks' fixed `1`) is |
| 7782 | // compared against a raw pixel difference on the stream's own bit-depth |
| 7783 | // scale, so each is shifted left by `bit_depth - 8` before use. |
| 7784 | let shift = bit_depth() - 8; |
| 7785 | let (blimit, limit, hev_thr) = (blimit << shift, limit << shift, hev_thr << shift); |
| 7786 | let flat_thresh = 1i32 << shift; |
| 7787 | for i in 0..4isize { |
| 7788 | let center = (base as isize + i * outer_stride) as usize; |
| 7789 | let idx = |k: isize| -> usize { (center as isize + k * tap_stride) as usize }; |
| 7790 | let px = |k: isize| -> i32 { i32::from(data[idx(k)]) }; |
| 7791 | match len { |
| 7792 | 4 => { |
| 7793 | let (p1, p0, q0, q1) = (px(-2), px(-1), px(0), px(1)); |
| 7794 | let mask = (p1 - p0).abs() <= limit |
| 7795 | && (q1 - q0).abs() <= limit |
| 7796 | && (p0 - q0).abs() * 2 + (p1 - q1).abs() / 2 <= blimit; |
| 7797 | let [op1, op0, oq0, oq1] = filter4(mask, hev_thr, p1, p0, q0, q1); |
| 7798 | data[idx(-2)] = op1.clamp(0, sample_max()) as u16; |
| 7799 | data[idx(-1)] = op0.clamp(0, sample_max()) as u16; |
| 7800 | data[idx(0)] = oq0.clamp(0, sample_max()) as u16; |
| 7801 | data[idx(1)] = oq1.clamp(0, sample_max()) as u16; |
| 7802 | } |
| 7803 | 6 => { |
| 7804 | let (p2, p1, p0, q0, q1, q2) = (px(-3), px(-2), px(-1), px(0), px(1), px(2)); |
| 7805 | let mask = (p2 - p1).abs() <= limit |
| 7806 | && (p1 - p0).abs() <= limit |
| 7807 | && (q1 - q0).abs() <= limit |
| 7808 | && (q2 - q1).abs() <= limit |
| 7809 | && (p0 - q0).abs() * 2 + (p1 - q1).abs() / 2 <= blimit; |
| 7810 | let flat = (p1 - p0).abs() <= flat_thresh |
| 7811 | && (q1 - q0).abs() <= flat_thresh |
| 7812 | && (p2 - p0).abs() <= flat_thresh |
| 7813 | && (q2 - q0).abs() <= flat_thresh; |
| 7814 | let [op1, op0, oq0, oq1] = filter6(mask, hev_thr, flat, p2, p1, p0, q0, q1, q2); |
| 7815 | data[idx(-2)] = op1.clamp(0, sample_max()) as u16; |
| 7816 | data[idx(-1)] = op0.clamp(0, sample_max()) as u16; |
| 7817 | data[idx(0)] = oq0.clamp(0, sample_max()) as u16; |
| 7818 | data[idx(1)] = oq1.clamp(0, sample_max()) as u16; |
| 7819 | } |
| 7820 | 8 => { |
| 7821 | let (p3, p2, p1, p0, q0, q1, q2, q3) = |
| 7822 | (px(-4), px(-3), px(-2), px(-1), px(0), px(1), px(2), px(3)); |
| 7823 | let mask = (p3 - p2).abs() <= limit |
| 7824 | && (p2 - p1).abs() <= limit |
| 7825 | && (p1 - p0).abs() <= limit |
| 7826 | && (q1 - q0).abs() <= limit |
| 7827 | && (q2 - q1).abs() <= limit |
| 7828 | && (q3 - q2).abs() <= limit |
| 7829 | && (p0 - q0).abs() * 2 + (p1 - q1).abs() / 2 <= blimit; |
| 7830 | let flat = (p1 - p0).abs() <= flat_thresh |
| 7831 | && (q1 - q0).abs() <= flat_thresh |
| 7832 | && (p2 - p0).abs() <= flat_thresh |
| 7833 | && (q2 - q0).abs() <= flat_thresh |
| 7834 | && (p3 - p0).abs() <= flat_thresh |
| 7835 | && (q3 - q0).abs() <= flat_thresh; |
| 7836 | let [op2, op1, op0, oq0, oq1, oq2] = |
| 7837 | filter8(mask, hev_thr, flat, p3, p2, p1, p0, q0, q1, q2, q3); |
| 7838 | data[idx(-3)] = op2.clamp(0, sample_max()) as u16; |
| 7839 | data[idx(-2)] = op1.clamp(0, sample_max()) as u16; |
| 7840 | data[idx(-1)] = op0.clamp(0, sample_max()) as u16; |
| 7841 | data[idx(0)] = oq0.clamp(0, sample_max()) as u16; |
| 7842 | data[idx(1)] = oq1.clamp(0, sample_max()) as u16; |
| 7843 | data[idx(2)] = oq2.clamp(0, sample_max()) as u16; |
| 7844 | } |
| 7845 | _ => { |
| 7846 | let (p6, p5, p4, p3, p2, p1, p0) = |
| 7847 | (px(-7), px(-6), px(-5), px(-4), px(-3), px(-2), px(-1)); |
| 7848 | let (q0, q1, q2, q3, q4, q5, q6) = |
| 7849 | (px(0), px(1), px(2), px(3), px(4), px(5), px(6)); |
| 7850 | let mask = (p3 - p2).abs() <= limit |
| 7851 | && (p2 - p1).abs() <= limit |
| 7852 | && (p1 - p0).abs() <= limit |
| 7853 | && (q1 - q0).abs() <= limit |
| 7854 | && (q2 - q1).abs() <= limit |
| 7855 | && (q3 - q2).abs() <= limit |
| 7856 | && (p0 - q0).abs() * 2 + (p1 - q1).abs() / 2 <= blimit; |
| 7857 | let flat = (p1 - p0).abs() <= flat_thresh |
| 7858 | && (q1 - q0).abs() <= flat_thresh |
| 7859 | && (p2 - p0).abs() <= flat_thresh |
| 7860 | && (q2 - q0).abs() <= flat_thresh |
| 7861 | && (p3 - p0).abs() <= flat_thresh |
| 7862 | && (q3 - q0).abs() <= flat_thresh; |
| 7863 | let flat2 = (p4 - p0).abs() <= flat_thresh |
| 7864 | && (q4 - q0).abs() <= flat_thresh |
| 7865 | && (p5 - p0).abs() <= flat_thresh |
| 7866 | && (q5 - q0).abs() <= flat_thresh |
| 7867 | && (p6 - p0).abs() <= flat_thresh |
| 7868 | && (q6 - q0).abs() <= flat_thresh; |
| 7869 | let out = filter14( |
| 7870 | mask, hev_thr, flat, flat2, p6, p5, p4, p3, p2, p1, p0, q0, q1, q2, q3, q4, q5, |
| 7871 | q6, |
| 7872 | ); |
| 7873 | for (k, v) in (-6..=5isize).zip(out) { |
| 7874 | data[idx(k)] = v.clamp(0, sample_max()) as u16; |
| 7875 | } |
| 7876 | } |
| 7877 | } |
| 7878 | } |
| 7879 | } |
| 7880 | |
| 7881 | /// One plane's worth of spec 7.14: every vertical edge across the plane, |
| 7882 | /// then every horizontal edge (the order the spec and libaom both use). |
| 7883 | fn deblock_plane( |
| 7884 | plane: &mut PlaneBuf, |
| 7885 | plane_idx: usize, |
| 7886 | lf: &LoopFilterParams, |
| 7887 | n: &Neighbours, |
| 7888 | frame_width: usize, |
| 7889 | frame_height: usize, |
| 7890 | ) { |
| 7891 | let chroma = plane_idx != 0; |
| 7892 | // r7: libaom clips the deblock loop to the CODED frame's own |
| 7893 | // width/height, not the mi-aligned `true_width`/`true_height` margin |
| 7894 | // (the padding up to the next 8-pixel/superblock boundary that |
| 7895 | // reconstruction still fills in but the loop filter never visits). |
| 7896 | // Confirmed against the superres gate: with the margin included, the |
| 7897 | // last coded mi column's horizontal edges were filtered when libaom's |
| 7898 | // real output leaves them untouched (r6's post-deblock diff, 37 px, all |
| 7899 | // in columns 44-47); clipping here closes it (3/3 frames pixel-exact). |
| 7900 | let (cw, ch) = if chroma { |
| 7901 | (frame_width.div_ceil(2), frame_height.div_ceil(2)) |
| 7902 | } else { |
| 7903 | (frame_width, frame_height) |
| 7904 | }; |
| 7905 | let (tw, th, stride) = ( |
| 7906 | plane.true_width.min(cw), |
| 7907 | plane.true_height.min(ch), |
| 7908 | plane.width, |
| 7909 | ); |
| 7910 | let mut y0 = 0usize; |
| 7911 | while y0 < th { |
| 7912 | let mut x0 = 4usize; |
| 7913 | while x0 < tw { |
| 7914 | if let Some((len, level)) = edge_params(lf, n, plane_idx, 0, chroma, x0, y0) { |
| 7915 | filter_edge( |
| 7916 | &mut plane.data, |
| 7917 | y0 * stride + x0, |
| 7918 | stride as isize, |
| 7919 | 1, |
| 7920 | len, |
| 7921 | level, |
| 7922 | lf.sharpness, |
| 7923 | ); |
| 7924 | } |
| 7925 | x0 += 4; |
| 7926 | } |
| 7927 | y0 += 4; |
| 7928 | } |
| 7929 | let mut x0 = 0usize; |
| 7930 | while x0 < tw { |
| 7931 | let mut y0 = 4usize; |
| 7932 | while y0 < th { |
| 7933 | if let Some((len, level)) = edge_params(lf, n, plane_idx, 1, chroma, x0, y0) { |
| 7934 | if std::env::var_os("EC_AV1_DEBLOCK_TRACE").is_some() |
| 7935 | && plane_idx == 0 |
| 7936 | && x0 == 44 |
| 7937 | { |
| 7938 | eprintln!( |
| 7939 | "HEDGE x0={x0} y0={y0} len={len} level={level} sharpness={}", |
| 7940 | lf.sharpness |
| 7941 | ); |
| 7942 | } |
| 7943 | filter_edge( |
| 7944 | &mut plane.data, |
| 7945 | y0 * stride + x0, |
| 7946 | 1, |
| 7947 | stride as isize, |
| 7948 | len, |
| 7949 | level, |
| 7950 | lf.sharpness, |
| 7951 | ); |
| 7952 | } |
| 7953 | y0 += 4; |
| 7954 | } |
| 7955 | x0 += 4; |
| 7956 | } |
| 7957 | } |
| 7958 | |
| 7959 | fn apply_cdef( |
| 7960 | y: &mut PlaneBuf, |
| 7961 | u: &mut PlaneBuf, |
| 7962 | v: &mut PlaneBuf, |
| 7963 | cdef: &CdefParams, |
| 7964 | skip_grid: &Neighbours, |
| 7965 | ) { |
| 7966 | // lane-part32 r2 debug rung, env-gated: see `apply_deblock`'s sibling. |
| 7967 | if std::env::var_os("EC_AV1_DEBUG_SKIP_CDEF").is_some() { |
| 7968 | return; |
| 7969 | } |
| 7970 | // `cdef.bits == 0` still means "no filtering at all" whenever the single |
| 7971 | // (index 0) strength pair is all-zero -- the fast path every existing |
| 7972 | // gate stream takes. A `bits > 0` frame can still be all-zero-strength |
| 7973 | // at every index, but that is rare enough not to special-case; the |
| 7974 | // per-superblock lookup below degrades to the same index-0 read anyway |
| 7975 | // when [`CDEF_IDX_GRID`] was never populated (`bits == 0`). |
| 7976 | if cdef.bits == 0 |
| 7977 | && cdef.y_pri_strength[0] == 0 |
| 7978 | && cdef.y_sec_strength[0] == 0 |
| 7979 | && cdef.uv_pri_strength[0] == 0 |
| 7980 | && cdef.uv_sec_strength[0] == 0 |
| 7981 | { |
| 7982 | return; |
| 7983 | } |
| 7984 | let sb_cols = CDEF_SB_COLS.with(|c| c.get()); |
| 7985 | let idx_grid = CDEF_IDX_GRID.with(|g| g.borrow().clone()); |
| 7986 | let strength_idx = |mi_r: usize, mi_c: usize| -> usize { |
| 7987 | if sb_cols == 0 { |
| 7988 | return 0; |
| 7989 | } |
| 7990 | let (sb_r, sb_c) = (mi_r / SB_MI as usize, mi_c / SB_MI as usize); |
| 7991 | idx_grid |
| 7992 | .get(sb_r * sb_cols + sb_c) |
| 7993 | .copied() |
| 7994 | .unwrap_or(0) as usize |
| 7995 | }; |
| 7996 | let src_y = y.data.clone(); |
| 7997 | let src_u = u.data.clone(); |
| 7998 | let src_v = v.data.clone(); |
| 7999 | // libaom `av1_cdef_filter_fb`: `pri_strength = level << coeff_shift`, |
| 8000 | // `sec_strength <<= coeff_shift`, `damping += coeff_shift - (pli != 0)`. |
| 8001 | let coeff_shift = i32::from(bit_depth()) - 8; |
| 8002 | let damping = i32::from(cdef.damping) + coeff_shift; |
| 8003 | let damping_uv = (damping - 1).max(0); |
| 8004 | let mut mi_r = 0usize; |
| 8005 | while mi_r < skip_grid.mi_rows { |
| 8006 | let mut mi_c = 0usize; |
| 8007 | while mi_c < skip_grid.mi_cols { |
| 8008 | if !skip_grid.is_skip_txfm(mi_r, mi_c) { |
| 8009 | let sidx = strength_idx(mi_r, mi_c); |
| 8010 | let (ox, oy) = (mi_c * 4, mi_r * 4); |
| 8011 | let (y_stride, y_true_w, y_true_h) = (y.width, y.true_width, y.true_height); |
| 8012 | let sample_y = |r: i32, c: i32| -> i32 { |
| 8013 | let (ny, nx) = (oy as i32 + r, ox as i32 + c); |
| 8014 | if ny < 0 || nx < 0 || ny as usize >= y_true_h || nx as usize >= y_true_w { |
| 8015 | CDEF_VERY_LARGE |
| 8016 | } else { |
| 8017 | i32::from(src_y[ny as usize * y_stride + nx as usize]) |
| 8018 | } |
| 8019 | }; |
| 8020 | let (dir, var) = cdef_find_dir(coeff_shift, |r, c| { |
| 8021 | let (ny, nx) = ((oy + r).min(y_true_h - 1), (ox + c).min(y_true_w - 1)); |
| 8022 | i32::from(src_y[ny * y_stride + nx]) |
| 8023 | }); |
| 8024 | |
| 8025 | // libaom `av1_cdef_filter_fb` passes `pri_strength ? dir : 0` |
| 8026 | // -- each plane zeroes the shared direction when *its own* |
| 8027 | // frame-level primary strength is 0, independent of the |
| 8028 | // per-block adjusted `t` and of the other plane's strength. |
| 8029 | let y_pri_strength = i32::from(cdef.y_pri_strength[sidx]) << coeff_shift; |
| 8030 | let y_sec_strength = i32::from(cdef.y_sec_strength[sidx]) << coeff_shift; |
| 8031 | let y_dir = if y_pri_strength != 0 { dir } else { 0 }; |
| 8032 | let t = cdef_adjust_strength(y_pri_strength, var); |
| 8033 | let enable_primary = t != 0; |
| 8034 | let enable_secondary = y_sec_strength != 0; |
| 8035 | if enable_primary || enable_secondary { |
| 8036 | cdef_filter_block( |
| 8037 | sample_y, |
| 8038 | y, |
| 8039 | ox, |
| 8040 | oy, |
| 8041 | 8, |
| 8042 | 8, |
| 8043 | t, |
| 8044 | y_sec_strength, |
| 8045 | y_dir, |
| 8046 | damping, |
| 8047 | damping, |
| 8048 | enable_primary, |
| 8049 | enable_secondary, |
| 8050 | coeff_shift, |
| 8051 | ); |
| 8052 | } |
| 8053 | |
| 8054 | let t_uv = i32::from(cdef.uv_pri_strength[sidx]) << coeff_shift; |
| 8055 | let uv_sec_strength = i32::from(cdef.uv_sec_strength[sidx]) << coeff_shift; |
| 8056 | let uv_dir = if t_uv != 0 { dir } else { 0 }; |
| 8057 | let enable_primary_uv = t_uv != 0; |
| 8058 | let enable_secondary_uv = uv_sec_strength != 0; |
| 8059 | if enable_primary_uv || enable_secondary_uv { |
| 8060 | let (cox, coy) = (ox / 2, oy / 2); |
| 8061 | for (plane, src_p) in [(&mut *u, &src_u), (&mut *v, &src_v)] { |
| 8062 | let (tw, th) = (plane.true_width, plane.true_height); |
| 8063 | let stride = plane.width; |
| 8064 | let sample_uv = |r: i32, c: i32| -> i32 { |
| 8065 | let (ny, nx) = (coy as i32 + r, cox as i32 + c); |
| 8066 | if ny < 0 || nx < 0 || ny as usize >= th || nx as usize >= tw { |
| 8067 | CDEF_VERY_LARGE |
| 8068 | } else { |
| 8069 | i32::from(src_p[ny as usize * stride + nx as usize]) |
| 8070 | } |
| 8071 | }; |
| 8072 | cdef_filter_block( |
| 8073 | sample_uv, |
| 8074 | plane, |
| 8075 | cox, |
| 8076 | coy, |
| 8077 | 4, |
| 8078 | 4, |
| 8079 | t_uv, |
| 8080 | uv_sec_strength, |
| 8081 | uv_dir, |
| 8082 | damping_uv, |
| 8083 | damping_uv, |
| 8084 | enable_primary_uv, |
| 8085 | enable_secondary_uv, |
| 8086 | coeff_shift, |
| 8087 | ); |
| 8088 | } |
| 8089 | } |
| 8090 | } |
| 8091 | mi_c += 2; |
| 8092 | } |
| 8093 | mi_r += 2; |
| 8094 | } |
| 8095 | } |
| 8096 | |
| 8097 | /// Spec 7.17: loop restoration, run after [`apply_cdef`] (a sibling lane's |
| 8098 | /// ordering note: libaom also runs `superres_post_decode` between the two, |
| 8099 | /// but this decoder never implements super-resolution, so there is nothing |
| 8100 | /// to interleave). `deblocked_*` is each plane's post-deblock, pre-CDEF |
| 8101 | /// buffer (the caller clones it before calling [`apply_cdef`]) -- loop |
| 8102 | /// restoration's stripe-boundary rows read from it instead of the |
| 8103 | /// CDEF-filtered plane, per `restoration.rs`'s own `lr_sample` doc comment. |
| 8104 | #[allow(clippy::too_many_arguments)] |
| 8105 | fn apply_loop_restoration( |
| 8106 | y: &mut PlaneBuf, |
| 8107 | u: &mut PlaneBuf, |
| 8108 | v: &mut PlaneBuf, |
| 8109 | deblocked_y: &PlaneBuf, |
| 8110 | deblocked_u: &PlaneBuf, |
| 8111 | deblocked_v: &PlaneBuf, |
| 8112 | lr: &LoopRestorationParams, |
| 8113 | grid: &crate::restoration::RestorationGrid, |
| 8114 | ) { |
| 8115 | y.data = crate::restoration::apply_loop_restoration_plane( |
| 8116 | &y.data, |
| 8117 | &deblocked_y.data, |
| 8118 | y.width, |
| 8119 | y.true_width, |
| 8120 | y.true_height, |
| 8121 | 0, |
| 8122 | lr.frame_restoration_type[0], |
| 8123 | lr.loop_restoration_size[0], |
| 8124 | grid, |
| 8125 | 0, |
| 8126 | ); |
| 8127 | u.data = crate::restoration::apply_loop_restoration_plane( |
| 8128 | &u.data, |
| 8129 | &deblocked_u.data, |
| 8130 | u.width, |
| 8131 | u.true_width, |
| 8132 | u.true_height, |
| 8133 | 1, |
| 8134 | lr.frame_restoration_type[1], |
| 8135 | lr.loop_restoration_size[1], |
| 8136 | grid, |
| 8137 | 1, |
| 8138 | ); |
| 8139 | v.data = crate::restoration::apply_loop_restoration_plane( |
| 8140 | &v.data, |
| 8141 | &deblocked_v.data, |
| 8142 | v.width, |
| 8143 | v.true_width, |
| 8144 | v.true_height, |
| 8145 | 1, |
| 8146 | lr.frame_restoration_type[2], |
| 8147 | lr.loop_restoration_size[2], |
| 8148 | grid, |
| 8149 | 2, |
| 8150 | ); |
| 8151 | } |
| 8152 | |
| 8153 | /// Decodes the payload [`crate::tile::sb_coeff_key_frame_tile`] writes, |
| 8154 | /// returning the picture it reconstructs to. |
| 8155 | /// |
| 8156 | /// `mi_cols`/`mi_rows` and `base_q_idx` are the frame header's, as parsed by |
| 8157 | /// [`ec_av1_syntax`]. `frame_width`/`frame_height` are the header's own true |
| 8158 | /// (render) size — a separate field from `mi_cols`/`mi_rows` (spec |
| 8159 | /// `compute_image_size` derives one from the other, but not losslessly: a |
| 8160 | /// width that is not a multiple of 8 samples leaves `mi_cols * 4` past it) — |
| 8161 | /// what the reconstruction is cropped to on the way out, mirroring |
| 8162 | /// [`crate::encode::crop_encoded`]. |
| 8163 | /// |
| 8164 | /// # Errors |
| 8165 | /// Returns an error when a block's partition, mode, skip flag or transform |
| 8166 | /// type is anything this decoder does not reconstruct (round 2: inter, tx |
| 8167 | /// types other than `DCT_DCT`, non-CDF-gathered rectangular splits below |
| 8168 | /// 16x16, a directional mode's angle delta other than zero, chroma-from-luma), |
| 8169 | /// or when the tile payload runs out of the symbols this decode expects (a |
| 8170 | /// genuinely foreign stream). |
| 8171 | pub fn decode_key_frame_tile( |
| 8172 | data: &[u8], |
| 8173 | mi_cols: u32, |
| 8174 | mi_rows: u32, |
| 8175 | base_q_idx: u8, |
| 8176 | frame_width: u32, |
| 8177 | frame_height: u32, |
| 8178 | enable_filter_intra: bool, |
| 8179 | cdef: &CdefParams, |
| 8180 | loop_filter: &LoopFilterParams, |
| 8181 | tx_select: bool, |
| 8182 | reduced_tx_set: bool, |
| 8183 | allow_screen_content_tools: bool, |
| 8184 | allow_intrabc: bool, |
| 8185 | ) -> Result<Picture> { |
| 8186 | let delta = DeltaParams::default(); |
| 8187 | let single_tile = TileInfo { |
| 8188 | mi_col_starts: vec![0, mi_cols], |
| 8189 | mi_row_starts: vec![0, mi_rows], |
| 8190 | ..TileInfo::default() |
| 8191 | }; |
| 8192 | decode_key_frame_tile_with_cdfs( |
| 8193 | &[data], |
| 8194 | &single_tile, |
| 8195 | mi_cols, |
| 8196 | mi_rows, |
| 8197 | base_q_idx, |
| 8198 | crate::quant::QuantDeltas::default(), |
| 8199 | frame_width, |
| 8200 | frame_height, |
| 8201 | enable_filter_intra, |
| 8202 | false, |
| 8203 | cdef, |
| 8204 | loop_filter, |
| 8205 | &LoopRestorationParams::default(), |
| 8206 | None, |
| 8207 | tx_select, |
| 8208 | reduced_tx_set, |
| 8209 | allow_screen_content_tools, |
| 8210 | allow_intrabc, |
| 8211 | delta, |
| 8212 | ) |
| 8213 | .map(|(picture, _)| picture) |
| 8214 | } |
| 8215 | |
| 8216 | thread_local! { |
| 8217 | /// lane-superres r3: the real decoded margin beyond `frame_width` up to |
| 8218 | /// the mi-aligned `true_width`, set by the most recent |
| 8219 | /// [`decode_key_frame_tile_with_cdfs`] OR (lane-superres r10) |
| 8220 | /// [`decode_inter_frame_tile_with_cdfs`] call -- both frame kinds crop |
| 8221 | /// their reconstructed buffer down from the same mi-aligned extent, so |
| 8222 | /// they share this one slot. See each function's own comment at its |
| 8223 | /// write site. `None` when there was no margin to save. |
| 8224 | static LAST_FRAME_WIDE_MARGIN: std::cell::RefCell<Option<Picture>> = |
| 8225 | const { std::cell::RefCell::new(None) }; |
| 8226 | } |
| 8227 | |
| 8228 | /// The margin the most recent key- or inter-frame tile decode stashed |
| 8229 | /// (see [`LAST_FRAME_WIDE_MARGIN`]) -- `stream.rs`'s superres path |
| 8230 | /// reads this immediately after the call, before any other frame |
| 8231 | /// decode can overwrite it. |
| 8232 | pub(crate) fn take_last_frame_wide_margin() -> Option<Picture> { |
| 8233 | LAST_FRAME_WIDE_MARGIN.with(|m| m.borrow_mut().take()) |
| 8234 | } |
| 8235 | |
| 8236 | /// [`decode_key_frame_tile`], threading a cross-frame CDF forward (spec |
| 8237 | /// 7.20's `load_cdfs`): `initial_cdfs` is the caller's forwarded state (from |
| 8238 | /// a prior frame's saved reference slot) when set, or the spec 8.4 defaults |
| 8239 | /// when `None` (the only case [`decode_key_frame_tile`] itself ever needs, |
| 8240 | /// since a key frame's own header always forces `primary_ref_frame == |
| 8241 | /// PRIMARY_REF_NONE`). Returns the tile's own end-of-tile adapted table |
| 8242 | /// alongside the picture, for the caller to save into whichever reference |
| 8243 | /// slots this frame's `refresh_frame_flags` names. |
| 8244 | pub(crate) fn decode_key_frame_tile_with_cdfs( |
| 8245 | tiles: &[&[u8]], |
| 8246 | tile_info: &TileInfo, |
| 8247 | mi_cols: u32, |
| 8248 | mi_rows: u32, |
| 8249 | base_q_idx: u8, |
| 8250 | // lane-sbpart r11: this frame header's own per-plane DC/AC quantizer |
| 8251 | // deltas (spec 5.9.12), threaded to [`QUANT_DELTAS`] at the top of every |
| 8252 | // tile alongside [`CURRENT_Q_IDX`]. |
| 8253 | deltas: crate::quant::QuantDeltas, |
| 8254 | frame_width: u32, |
| 8255 | frame_height: u32, |
| 8256 | enable_filter_intra: bool, |
| 8257 | enable_edge_filter: bool, |
| 8258 | cdef: &CdefParams, |
| 8259 | loop_filter: &LoopFilterParams, |
| 8260 | lr: &LoopRestorationParams, |
| 8261 | initial_cdfs: Option<Cdfs>, |
| 8262 | tx_select: bool, |
| 8263 | reduced_tx_set: bool, |
| 8264 | allow_screen_content_tools: bool, |
| 8265 | allow_intrabc: bool, |
| 8266 | // lane-realworld r4: this frame header's own `delta` (spec 5.9.17/5.9.18) |
| 8267 | // -- only `q_present`/`q_res` are actually read ([`maybe_read_delta_q`]); |
| 8268 | // `stream.rs` still refuses a frame with `lf_present` set before this |
| 8269 | // function is ever called. |
| 8270 | delta: DeltaParams, |
| 8271 | ) -> Result<(Picture, Cdfs)> { |
| 8272 | if mi_cols == 0 || mi_rows == 0 { |
| 8273 | return Err(unsupported("a frame with no mode-info grid")); |
| 8274 | } |
| 8275 | ENABLE_EDGE_FILTER.with(|f| f.set(enable_edge_filter)); |
| 8276 | let (sb_cols, sb_rows) = (mi_cols.div_ceil(SB_MI), mi_rows.div_ceil(SB_MI)); |
| 8277 | INTRABC_MI_GRID.with(|g| { |
| 8278 | *g.borrow_mut() = allow_intrabc.then(|| { |
| 8279 | ( |
| 8280 | crate::mvstack::MiGrid::new(mi_cols as usize, mi_rows as usize), |
| 8281 | mi_cols as usize, |
| 8282 | mi_rows as usize, |
| 8283 | ) |
| 8284 | }); |
| 8285 | }); |
| 8286 | INTRABC_DV.with(|c| c.set(None)); |
| 8287 | CDEF_BITS.with(|c| c.set(cdef.bits)); |
| 8288 | CDEF_SB_COLS.with(|c| c.set(sb_cols as usize)); |
| 8289 | CDEF_IDX_GRID.with(|g| *g.borrow_mut() = vec![0u8; sb_cols as usize * sb_rows as usize]); |
| 8290 | DELTA_Q_PRESENT.with(|c| c.set(delta.q_present)); |
| 8291 | DELTA_Q_RES.with(|c| c.set(1i32 << delta.q_res)); |
| 8292 | DELTA_LF_PRESENT.with(|c| c.set(delta.lf_present)); |
| 8293 | DELTA_LF_RES.with(|c| c.set(1i32 << delta.lf_res)); |
| 8294 | DELTA_LF_MULTI.with(|c| c.set(delta.lf_multi)); |
| 8295 | let (cols32, rows32) = block_grid(mi_cols, mi_rows); |
| 8296 | let (true_width, true_height) = ((mi_cols * 4) as usize, (mi_rows * 4) as usize); |
| 8297 | let (width, height) = (cols32 as usize * BLOCK, rows32 as usize * BLOCK); |
| 8298 | |
| 8299 | let mut y = PlaneBuf { |
| 8300 | data: vec![0u16; width * height], |
| 8301 | width, |
| 8302 | height, |
| 8303 | true_width, |
| 8304 | true_height, |
| 8305 | tile_x0: 0, |
| 8306 | tile_y0: 0, |
| 8307 | tile_x1: width, |
| 8308 | tile_y1: height, |
| 8309 | }; |
| 8310 | let mut u = PlaneBuf { |
| 8311 | data: vec![0u16; width * height / 4], |
| 8312 | width: width / 2, |
| 8313 | height: height / 2, |
| 8314 | true_width: true_width / 2, |
| 8315 | true_height: true_height / 2, |
| 8316 | tile_x0: 0, |
| 8317 | tile_y0: 0, |
| 8318 | tile_x1: width / 2, |
| 8319 | tile_y1: height / 2, |
| 8320 | }; |
| 8321 | let mut v = PlaneBuf { |
| 8322 | data: vec![0u16; width * height / 4], |
| 8323 | width: width / 2, |
| 8324 | height: height / 2, |
| 8325 | true_width: true_width / 2, |
| 8326 | true_height: true_height / 2, |
| 8327 | tile_x0: 0, |
| 8328 | tile_y0: 0, |
| 8329 | tile_x1: width / 2, |
| 8330 | tile_y1: height / 2, |
| 8331 | }; |
| 8332 | |
| 8333 | let scan32 = default_scan(TX32); |
| 8334 | let scan16 = default_scan(TX16); |
| 8335 | let scan8 = default_scan(TX8); |
| 8336 | let scan4 = default_scan(TX4); |
| 8337 | |
| 8338 | let base_cdfs = initial_cdfs.unwrap_or_else(|| Cdfs::new(q_ctx_of(base_q_idx))); |
| 8339 | let mut result_cdfs = base_cdfs.clone(); |
| 8340 | let mut neighbours = Neighbours::new( |
| 8341 | cols32 as usize * 2, |
| 8342 | rows32 as usize * 2, |
| 8343 | mi_cols as usize, |
| 8344 | mi_rows as usize, |
| 8345 | ); |
| 8346 | let mut lr_grid = crate::restoration::RestorationGrid::new(lr, frame_width, frame_height); |
| 8347 | let mut lr_reference = [( |
| 8348 | crate::restoration::WienerInfo::default(), |
| 8349 | crate::restoration::SgrprojInfo::default(), |
| 8350 | ); 3]; |
| 8351 | |
| 8352 | // lane-tiles r2: each tile gets its own fresh `SymbolDecoder` over its |
| 8353 | // own byte range and its own fresh copy of the frame's initial CDFs |
| 8354 | // (spec 5.11.2 `decode_tile`'s `init_symbol`); only the tile named by |
| 8355 | // `context_update_tile_id` has its end-of-tile adapted table kept as the |
| 8356 | // frame's own output (spec `exit_symbol`) -- every other tile's |
| 8357 | // adaptation is discarded, matching the refusal this replaces. |
| 8358 | for (tile_idx, &tile_bytes) in tiles.iter().enumerate() { |
| 8359 | let tile_num = tile_idx as u32; |
| 8360 | let (trow, tcol) = (tile_num / tile_info.cols, tile_num % tile_info.cols); |
| 8361 | let mi_row0 = tile_info.mi_row_starts[trow as usize]; |
| 8362 | let mi_row1 = tile_info.mi_row_starts[trow as usize + 1]; |
| 8363 | let mi_col0 = tile_info.mi_col_starts[tcol as usize]; |
| 8364 | let mi_col1 = tile_info.mi_col_starts[tcol as usize + 1]; |
| 8365 | // Tile boundaries are always superblock-aligned in mi units (spec |
| 8366 | // 5.9.15's uniform/non-uniform derivation both only ever emit |
| 8367 | // multiples of the superblock's own mi size), so plain division is |
| 8368 | // exact here -- no rounding needed. |
| 8369 | // `mi_row0`/`mi_col0` (this tile's own start, `0` or an interior |
| 8370 | // tile boundary) are always superblock-aligned by spec 5.9.15; the |
| 8371 | // frame's own true final edge (`mi_row_starts`/`mi_col_starts`' |
| 8372 | // last entry) is not, so the upper bound needs `div_ceil` the same |
| 8373 | // way the whole-frame `sb_rows`/`sb_cols` above does, to keep the |
| 8374 | // trailing partial superblock row/column in scope. |
| 8375 | let (sb_r0, sb_r1) = ((mi_row0 / SB_MI).min(sb_rows), mi_row1.div_ceil(SB_MI).min(sb_rows)); |
| 8376 | let (sb_c0, sb_c1) = ((mi_col0 / SB_MI).min(sb_cols), mi_col1.div_ceil(SB_MI).min(sb_cols)); |
| 8377 | |
| 8378 | let mut cdfs = base_cdfs.clone(); |
| 8379 | // spec `decode_tile`: `CurrentQIndex` resets to the frame's own |
| 8380 | // `base_q_idx` at the top of every tile, not once per frame. |
| 8381 | CURRENT_Q_IDX.with(|c| c.set(i32::from(base_q_idx))); |
| 8382 | QUANT_DELTAS.with(|c| c.set(deltas)); |
| 8383 | CURRENT_DELTA_LF.with(|c| c.set([0; 4])); |
| 8384 | if std::env::var_os("EC_AV1_TRACE").is_some() { |
| 8385 | eprintln!( |
| 8386 | "TRACE key_tile_bytes tile={tile_num} len={} first8={:02x?} base_q_idx={base_q_idx} mi_cols={mi_cols} mi_rows={mi_rows}", |
| 8387 | tile_bytes.len(), |
| 8388 | &tile_bytes[..tile_bytes.len().min(8)] |
| 8389 | ); |
| 8390 | } |
| 8391 | let mut dec = SymbolDecoder::new(tile_bytes); |
| 8392 | neighbours.start_tile(mi_row0 as usize, mi_col0 as usize, mi_col1 as usize); |
| 8393 | y.set_tile_origin( |
| 8394 | mi_col0 as usize * 4, |
| 8395 | mi_row0 as usize * 4, |
| 8396 | (mi_col1 as usize * 4).min(y.width), |
| 8397 | (mi_row1 as usize * 4).min(y.height), |
| 8398 | ); |
| 8399 | u.set_tile_origin( |
| 8400 | mi_col0 as usize * 2, |
| 8401 | mi_row0 as usize * 2, |
| 8402 | (mi_col1 as usize * 2).min(u.width), |
| 8403 | (mi_row1 as usize * 2).min(u.height), |
| 8404 | ); |
| 8405 | v.set_tile_origin( |
| 8406 | mi_col0 as usize * 2, |
| 8407 | mi_row0 as usize * 2, |
| 8408 | (mi_col1 as usize * 2).min(v.width), |
| 8409 | (mi_row1 as usize * 2).min(v.height), |
| 8410 | ); |
| 8411 | TILE_HITS.with(|c| c.set(c.get() + 1)); |
| 8412 | |
| 8413 | for sb_r in sb_r0..sb_r1 { |
| 8414 | neighbours.start_row(); |
| 8415 | for sb_c in sb_c0..sb_c1 { |
| 8416 | crate::restoration::read_lr( |
| 8417 | &mut dec, |
| 8418 | &mut cdfs, |
| 8419 | lr, |
| 8420 | &mut lr_grid, |
| 8421 | &mut lr_reference, |
| 8422 | sb_r * SB_MI, |
| 8423 | sb_c * SB_MI, |
| 8424 | SB_MI, |
| 8425 | ); |
| 8426 | CDEF_TRANSMITTED.with(|c| c.set(false)); |
| 8427 | let at = (sb_r as usize * 4, sb_c as usize * 4); |
| 8428 | let ctx = neighbours.partition_ctx(at, SB); |
| 8429 | let (has_cols, has_rows) = ( |
| 8430 | sb_c * SB_MI + SB_MI / 2 < mi_cols, |
| 8431 | sb_r * SB_MI + SB_MI / 2 < mi_rows, |
| 8432 | ); |
| 8433 | // Recomputed at this superblock's own half (spec |
| 8434 | // `decode_partition`): a full alphabet symbol when both halves are |
| 8435 | // inside the true frame, a single gathered bit when just one is |
| 8436 | // (the superblock cannot be left whole, so this is forced split), |
| 8437 | // and nothing at all (SPLIT inferred, no bits) when neither is — |
| 8438 | // mirroring [`crate::tile::sb_coeff_key_frame_tile`]'s own |
| 8439 | // three-way write exactly. |
| 8440 | let part = if has_cols && has_rows { |
| 8441 | let p = dec.symbol(&mut cdfs.partition_w64[ctx]); |
| 8442 | if std::env::var_os("EC_AV1_TRACE").is_some() { |
| 8443 | eprintln!("TRACE partition_w64 ctx={ctx} value={p}"); |
| 8444 | } |
| 8445 | p |
| 8446 | } else { |
| 8447 | match (has_cols, has_rows) { |
| 8448 | (true, false) => { |
| 8449 | dec.symbol_fixed(&gather(&cdfs.partition_w64[ctx], VERT_ALIKE)); |
| 8450 | } |
| 8451 | (false, true) => { |
| 8452 | dec.symbol_fixed(&gather(&cdfs.partition_w64[ctx], HORZ_ALIKE)); |
| 8453 | } |
| 8454 | _ => {} |
| 8455 | } |
| 8456 | PARTITION_SPLIT |
| 8457 | }; |
| 8458 | match part { |
| 8459 | PARTITION_NONE => { |
| 8460 | decode_block( |
| 8461 | &mut dec, |
| 8462 | &mut cdfs, |
| 8463 | &mut neighbours, |
| 8464 | at, |
| 8465 | SB, |
| 8466 | TxbSet::Luma64, |
| 8467 | TxbSet::Chroma32, |
| 8468 | TX32, |
| 8469 | TX32, |
| 8470 | (&scan32, &scan32), |
| 8471 | false, |
| 8472 | &mut y, |
| 8473 | &mut u, |
| 8474 | &mut v, |
| 8475 | base_q_idx, |
| 8476 | enable_filter_intra, |
| 8477 | allow_screen_content_tools, |
| 8478 | allow_intrabc, |
| 8479 | &scan32, |
| 8480 | &scan16, |
| 8481 | &scan8, |
| 8482 | &scan4, |
| 8483 | tx_select, |
| 8484 | reduced_tx_set, |
| 8485 | )?; |
| 8486 | } |
| 8487 | PARTITION_SPLIT => { |
| 8488 | for q in 0..4 { |
| 8489 | let (r32, c32) = (sb_r * 2 + q / 2, sb_c * 2 + q % 2); |
| 8490 | if r32 >= rows32 || c32 >= cols32 { |
| 8491 | continue; |
| 8492 | } |
| 8493 | let at32 = (r32 as usize * 2, c32 as usize * 2); |
| 8494 | let ctx32 = neighbours.partition_ctx(at32, BLOCK); |
| 8495 | let (has_cols32, has_rows32) = ( |
| 8496 | has_half(c32 * BLOCK_MI, BLOCK_MI, mi_cols), |
| 8497 | has_half(r32 * BLOCK_MI, BLOCK_MI, mi_rows), |
| 8498 | ); |
| 8499 | let part32 = if has_cols32 && has_rows32 { |
| 8500 | let p = dec.symbol(&mut cdfs.partition_w32[ctx32]); |
| 8501 | if std::env::var_os("EC_AV1_TRACE").is_some() { |
| 8502 | let (rng, _) = dec.debug_state(); |
| 8503 | eprintln!( |
| 8504 | "TRACE partition_w32 mi=({},{}) ctx={ctx32} value={p} rng={rng}", |
| 8505 | r32 * BLOCK_MI, |
| 8506 | c32 * BLOCK_MI |
| 8507 | ); |
| 8508 | } |
| 8509 | p |
| 8510 | } else { |
| 8511 | match (has_cols32, has_rows32) { |
| 8512 | (true, false) => { |
| 8513 | dec.symbol_fixed(&gather( |
| 8514 | &cdfs.partition_w32[ctx32], |
| 8515 | VERT_ALIKE, |
| 8516 | )); |
| 8517 | } |
| 8518 | (false, true) => { |
| 8519 | dec.symbol_fixed(&gather( |
| 8520 | &cdfs.partition_w32[ctx32], |
| 8521 | HORZ_ALIKE, |
| 8522 | )); |
| 8523 | } |
| 8524 | _ => {} |
| 8525 | } |
| 8526 | PARTITION_SPLIT |
| 8527 | }; |
| 8528 | match part32 { |
| 8529 | PARTITION_NONE => { |
| 8530 | decode_block( |
| 8531 | &mut dec, |
| 8532 | &mut cdfs, |
| 8533 | &mut neighbours, |
| 8534 | at32, |
| 8535 | BLOCK, |
| 8536 | TxbSet::Luma32, |
| 8537 | TxbSet::Chroma16, |
| 8538 | TX32, |
| 8539 | TX16, |
| 8540 | (&scan32, &scan16), |
| 8541 | true, |
| 8542 | &mut y, |
| 8543 | &mut u, |
| 8544 | &mut v, |
| 8545 | base_q_idx, |
| 8546 | enable_filter_intra, |
| 8547 | allow_screen_content_tools, |
| 8548 | allow_intrabc, |
| 8549 | &scan32, |
| 8550 | &scan16, |
| 8551 | &scan8, |
| 8552 | &scan4, |
| 8553 | tx_select, |
| 8554 | reduced_tx_set, |
| 8555 | )?; |
| 8556 | } |
| 8557 | PARTITION_SPLIT => { |
| 8558 | for sub in 0..4 { |
| 8559 | let (sr, sc) = |
| 8560 | (r32 as usize * 2 + sub / 2, c32 as usize * 2 + sub % 2); |
| 8561 | if (sr as u32) * SUB_MI >= mi_rows |
| 8562 | || (sc as u32) * SUB_MI >= mi_cols |
| 8563 | { |
| 8564 | continue; |
| 8565 | } |
| 8566 | let (has_cols16, has_rows16) = ( |
| 8567 | has_half(sc as u32 * SUB_MI, SUB_MI, mi_cols), |
| 8568 | has_half(sr as u32 * SUB_MI, SUB_MI, mi_rows), |
| 8569 | ); |
| 8570 | let at16 = (sr, sc); |
| 8571 | let ctx16 = neighbours.partition_ctx(at16, SUB); |
| 8572 | if has_cols16 && has_rows16 { |
| 8573 | let part16 = dec.symbol(&mut cdfs.partition_w16[ctx16]); |
| 8574 | if std::env::var_os("EC_AV1_TRACE").is_some() { |
| 8575 | eprintln!( |
| 8576 | "TRACE partition_w16 mi=({},{}) ctx={ctx16} value={part16}", |
| 8577 | at16.0, at16.1 |
| 8578 | ); |
| 8579 | } |
| 8580 | if part16 == PARTITION_NONE { |
| 8581 | decode_block( |
| 8582 | &mut dec, |
| 8583 | &mut cdfs, |
| 8584 | &mut neighbours, |
| 8585 | at16, |
| 8586 | SUB, |
| 8587 | TxbSet::Luma16, |
| 8588 | TxbSet::Chroma8, |
| 8589 | TX16, |
| 8590 | TX8, |
| 8591 | (&scan16, &scan8), |
| 8592 | true, |
| 8593 | &mut y, |
| 8594 | &mut u, |
| 8595 | &mut v, |
| 8596 | base_q_idx, |
| 8597 | enable_filter_intra, |
| 8598 | allow_screen_content_tools, |
| 8599 | allow_intrabc, |
| 8600 | &scan32, |
| 8601 | &scan16, |
| 8602 | &scan8, |
| 8603 | &scan4, |
| 8604 | tx_select, |
| 8605 | reduced_tx_set, |
| 8606 | )?; |
| 8607 | continue; |
| 8608 | } |
| 8609 | if part16 == PARTITION_VERT_B { |
| 8610 | VERT_B_INTRA_HITS.with(|c| c.set(c.get() + 1)); |
| 8611 | // lane-part32 r6: same availability |
| 8612 | // defect r5 fixed one level up -- |
| 8613 | // the TR/BR 8x8 squares below are |
| 8614 | // visited out of raster order, so |
| 8615 | // they need libaom's |
| 8616 | // `has_tr_vert_*`/`has_bl_vert_*` |
| 8617 | // tables (the left 8x16 rect goes |
| 8618 | // through `Reach::of_rect`, which |
| 8619 | // the guard deliberately does not |
| 8620 | // affect). |
| 8621 | let _vert_ab = |
| 8622 | crate::encode::Reach::vert_ab_partition(); |
| 8623 | // Left rect: 8x16, real |
| 8624 | // `decode_block_rect`, sat at the |
| 8625 | // 16x16 parent's own origin (no |
| 8626 | // fractional-SUB-grid pixel |
| 8627 | // offset needed -- the plain |
| 8628 | // HORZ/VERT arms this lane's |
| 8629 | // charter named DO need one, |
| 8630 | // since their second half is |
| 8631 | // offset by half the parent; |
| 8632 | // unported, see decode_block_rect's |
| 8633 | // own doc comment). |
| 8634 | decode_block_rect( |
| 8635 | &mut dec, |
| 8636 | &mut cdfs, |
| 8637 | &mut neighbours, |
| 8638 | at16, |
| 8639 | 8, |
| 8640 | 16, |
| 8641 | &mut y, |
| 8642 | &mut u, |
| 8643 | &mut v, |
| 8644 | enable_filter_intra, |
| 8645 | allow_screen_content_tools, |
| 8646 | base_q_idx, |
| 8647 | tx_select, |
| 8648 | reduced_tx_set, |
| 8649 | )?; |
| 8650 | // Two 8x8 squares stacked on the |
| 8651 | // right, chained through |
| 8652 | // `prev_leaf` exactly like the |
| 8653 | // real (non-straddle) SPLIT path |
| 8654 | // below: the bottom leaf's ABOVE |
| 8655 | // context is the top leaf, not |
| 8656 | // the coarse SUB-grid array (a |
| 8657 | // real block never sat there). |
| 8658 | let (mi_row0, mi_col0) = |
| 8659 | (sr as u32 * SUB_MI, sc as u32 * SUB_MI); |
| 8660 | let top_right = |
| 8661 | (mi_row0 as usize, mi_col0 as usize + 2); |
| 8662 | let bot_right = |
| 8663 | (mi_row0 as usize + 2, mi_col0 as usize + 2); |
| 8664 | let mode_tr = decode_leaf8( |
| 8665 | &mut dec, |
| 8666 | &mut cdfs, |
| 8667 | &mut neighbours, |
| 8668 | at16, |
| 8669 | top_right, |
| 8670 | (&scan8, &scan4), |
| 8671 | None, |
| 8672 | &mut y, |
| 8673 | &mut u, |
| 8674 | &mut v, |
| 8675 | base_q_idx, |
| 8676 | enable_filter_intra, |
| 8677 | allow_screen_content_tools, |
| 8678 | allow_intrabc, |
| 8679 | tx_select, |
| 8680 | reduced_tx_set, |
| 8681 | )?; |
| 8682 | let mode_br = decode_leaf8( |
| 8683 | &mut dec, |
| 8684 | &mut cdfs, |
| 8685 | &mut neighbours, |
| 8686 | at16, |
| 8687 | bot_right, |
| 8688 | (&scan8, &scan4), |
| 8689 | Some((top_right, mode_tr)), |
| 8690 | &mut y, |
| 8691 | &mut u, |
| 8692 | &mut v, |
| 8693 | base_q_idx, |
| 8694 | enable_filter_intra, |
| 8695 | allow_screen_content_tools, |
| 8696 | allow_intrabc, |
| 8697 | tx_select, |
| 8698 | reduced_tx_set, |
| 8699 | )?; |
| 8700 | // `record()`'s above_mode/left_mode |
| 8701 | // write is a no-op at an 8x8 |
| 8702 | // leaf's own side (same gap the |
| 8703 | // real-SPLIT path above patches): |
| 8704 | // force it from the last-decoded |
| 8705 | // (bottom-right) leaf. |
| 8706 | neighbours.above_mode[sc] = mode_br; |
| 8707 | neighbours.left_mode[sr] = mode_br; |
| 8708 | continue; |
| 8709 | } |
| 8710 | if part16 == PARTITION_HORZ { |
| 8711 | HORZ_VERT_INTRA_HITS.with(|c| c.set(c.get() + 1)); |
| 8712 | let (mi_row0, mi_col0) = ( |
| 8713 | sr as u32 * SUB_MI, |
| 8714 | sc as u32 * SUB_MI, |
| 8715 | ); |
| 8716 | let top = (mi_row0 as usize, mi_col0 as usize); |
| 8717 | let bot = (mi_row0 as usize + 2, mi_col0 as usize); |
| 8718 | let mode_top = decode_leaf_rect( |
| 8719 | &mut dec, |
| 8720 | &mut cdfs, |
| 8721 | &mut neighbours, |
| 8722 | at16, |
| 8723 | top, |
| 8724 | 16, |
| 8725 | 8, |
| 8726 | None, |
| 8727 | &mut y, |
| 8728 | &mut u, |
| 8729 | &mut v, |
| 8730 | enable_filter_intra, |
| 8731 | allow_screen_content_tools, |
| 8732 | base_q_idx, |
| 8733 | tx_select, |
| 8734 | )?; |
| 8735 | let mode_bot = decode_leaf_rect( |
| 8736 | &mut dec, |
| 8737 | &mut cdfs, |
| 8738 | &mut neighbours, |
| 8739 | at16, |
| 8740 | bot, |
| 8741 | 16, |
| 8742 | 8, |
| 8743 | Some((top, mode_top)), |
| 8744 | &mut y, |
| 8745 | &mut u, |
| 8746 | &mut v, |
| 8747 | enable_filter_intra, |
| 8748 | allow_screen_content_tools, |
| 8749 | base_q_idx, |
| 8750 | tx_select, |
| 8751 | )?; |
| 8752 | neighbours.above_mode[sc] = mode_bot; |
| 8753 | neighbours.left_mode[sr] = mode_bot; |
| 8754 | continue; |
| 8755 | } |
| 8756 | if part16 == PARTITION_VERT { |
| 8757 | HORZ_VERT_INTRA_HITS.with(|c| c.set(c.get() + 1)); |
| 8758 | let (mi_row0, mi_col0) = ( |
| 8759 | sr as u32 * SUB_MI, |
| 8760 | sc as u32 * SUB_MI, |
| 8761 | ); |
| 8762 | let left = (mi_row0 as usize, mi_col0 as usize); |
| 8763 | let right = (mi_row0 as usize, mi_col0 as usize + 2); |
| 8764 | let mode_left = decode_leaf_rect( |
| 8765 | &mut dec, |
| 8766 | &mut cdfs, |
| 8767 | &mut neighbours, |
| 8768 | at16, |
| 8769 | left, |
| 8770 | 8, |
| 8771 | 16, |
| 8772 | None, |
| 8773 | &mut y, |
| 8774 | &mut u, |
| 8775 | &mut v, |
| 8776 | enable_filter_intra, |
| 8777 | allow_screen_content_tools, |
| 8778 | base_q_idx, |
| 8779 | tx_select, |
| 8780 | )?; |
| 8781 | let mode_right = decode_leaf_rect( |
| 8782 | &mut dec, |
| 8783 | &mut cdfs, |
| 8784 | &mut neighbours, |
| 8785 | at16, |
| 8786 | right, |
| 8787 | 8, |
| 8788 | 16, |
| 8789 | Some((left, mode_left)), |
| 8790 | &mut y, |
| 8791 | &mut u, |
| 8792 | &mut v, |
| 8793 | enable_filter_intra, |
| 8794 | allow_screen_content_tools, |
| 8795 | base_q_idx, |
| 8796 | tx_select, |
| 8797 | )?; |
| 8798 | neighbours.above_mode[sc] = mode_right; |
| 8799 | neighbours.left_mode[sr] = mode_right; |
| 8800 | continue; |
| 8801 | } |
| 8802 | if part16 != PARTITION_SPLIT { |
| 8803 | return Err(unsupported( |
| 8804 | "a HORZ_A/HORZ_B/VERT_A partition below 16x16 (this decoder codes only the square arms, HORZ, VERT, VERT_B, and a clean split below 16x16)", |
| 8805 | )); |
| 8806 | } |
| 8807 | // A real (non-straddle) SPLIT of a |
| 8808 | // whole 16x16 into four 8x8 leaves -- |
| 8809 | // the same recursion the straddle |
| 8810 | // path below already runs when the |
| 8811 | // true frame edge forces it, just |
| 8812 | // over all four positions instead of |
| 8813 | // the one or two the edge leaves. |
| 8814 | let (mi_row0, mi_col0) = |
| 8815 | (sr as u32 * SUB_MI, sc as u32 * SUB_MI); |
| 8816 | let mut prev_leaf: Option<((usize, usize), usize)> = None; |
| 8817 | for i in 0..4 { |
| 8818 | let (mr, mc) = |
| 8819 | (mi_row0 + (i / 2) * 2, mi_col0 + (i % 2) * 2); |
| 8820 | let leaf_mi = (mr as usize, mc as usize); |
| 8821 | let leaf_ctx = neighbours.partition_ctx_mi(leaf_mi, 8); |
| 8822 | let part8 = |
| 8823 | dec.symbol(&mut cdfs.partition_w8[leaf_ctx]); |
| 8824 | if std::env::var_os("EC_AV1_TRACE").is_some() { |
| 8825 | eprintln!( |
| 8826 | "TRACE partition_w8 mi=({mr},{mc}) ctx={leaf_ctx} value={part8}" |
| 8827 | ); |
| 8828 | } |
| 8829 | if part8 != PARTITION_NONE { |
| 8830 | return Err(unsupported( |
| 8831 | "a partition below 8x8 (this decoder codes no leaf smaller than 8x8)", |
| 8832 | )); |
| 8833 | } |
| 8834 | let leaf_mode = decode_leaf8( |
| 8835 | &mut dec, |
| 8836 | &mut cdfs, |
| 8837 | &mut neighbours, |
| 8838 | at16, |
| 8839 | leaf_mi, |
| 8840 | (&scan8, &scan4), |
| 8841 | prev_leaf, |
| 8842 | &mut y, |
| 8843 | &mut u, |
| 8844 | &mut v, |
| 8845 | base_q_idx, |
| 8846 | enable_filter_intra, |
| 8847 | allow_screen_content_tools, |
| 8848 | allow_intrabc, |
| 8849 | tx_select, |
| 8850 | reduced_tx_set, |
| 8851 | )?; |
| 8852 | prev_leaf = Some((leaf_mi, leaf_mode)); |
| 8853 | } |
| 8854 | if let Some((_, mode)) = prev_leaf { |
| 8855 | neighbours.above_mode[sc] = mode; |
| 8856 | neighbours.left_mode[sr] = mode; |
| 8857 | } |
| 8858 | continue; |
| 8859 | } |
| 8860 | // The true edge falls inside this 16x16 |
| 8861 | // leaf itself (mod-32==8 target sizes, |
| 8862 | // lane-av1-rect): one axis only -- an 8x8 |
| 8863 | // leaf never itself straddles, so the |
| 8864 | // block splits cleanly along whichever |
| 8865 | // axis is short. |
| 8866 | if !has_cols16 && !has_rows16 { |
| 8867 | return Err(unsupported( |
| 8868 | "a 16x16 block whose true edge cuts through both \ |
| 8869 | axes needs a rectangular transform this decoder \ |
| 8870 | does not code yet", |
| 8871 | )); |
| 8872 | } |
| 8873 | if has_cols16 { |
| 8874 | dec.symbol_fixed(&gather( |
| 8875 | &cdfs.partition_w16[ctx16], |
| 8876 | VERT_ALIKE, |
| 8877 | )); |
| 8878 | } else { |
| 8879 | dec.symbol_fixed(&gather( |
| 8880 | &cdfs.partition_w16[ctx16], |
| 8881 | HORZ_ALIKE, |
| 8882 | )); |
| 8883 | } |
| 8884 | let (mi_row0, mi_col0) = |
| 8885 | (sr as u32 * SUB_MI, sc as u32 * SUB_MI); |
| 8886 | let leaf_positions: Vec<(u32, u32)> = (0..4) |
| 8887 | .map(|i| (mi_row0 + (i / 2) * 2, mi_col0 + (i % 2) * 2)) |
| 8888 | .filter(|&(mr, mc)| mr < mi_rows && mc < mi_cols) |
| 8889 | .collect(); |
| 8890 | let mut prev_leaf: Option<((usize, usize), usize)> = None; |
| 8891 | for (mr, mc) in leaf_positions { |
| 8892 | let leaf_mi = (mr as usize, mc as usize); |
| 8893 | let leaf_ctx = neighbours.partition_ctx_mi(leaf_mi, 8); |
| 8894 | let part8 = dec.symbol(&mut cdfs.partition_w8[leaf_ctx]); |
| 8895 | if std::env::var_os("EC_AV1_TRACE").is_some() { |
| 8896 | eprintln!( |
| 8897 | "TRACE partition_w8 mi=({mr},{mc}) ctx={leaf_ctx} value={part8}" |
| 8898 | ); |
| 8899 | } |
| 8900 | if part8 != PARTITION_NONE { |
| 8901 | return Err(unsupported( |
| 8902 | "a partition below 8x8 (this decoder codes no leaf smaller than 8x8)", |
| 8903 | )); |
| 8904 | } |
| 8905 | let leaf_mode = decode_leaf8( |
| 8906 | &mut dec, |
| 8907 | &mut cdfs, |
| 8908 | &mut neighbours, |
| 8909 | at16, |
| 8910 | leaf_mi, |
| 8911 | (&scan8, &scan4), |
| 8912 | prev_leaf, |
| 8913 | &mut y, |
| 8914 | &mut u, |
| 8915 | &mut v, |
| 8916 | base_q_idx, |
| 8917 | enable_filter_intra, |
| 8918 | allow_screen_content_tools, |
| 8919 | allow_intrabc, |
| 8920 | tx_select, |
| 8921 | reduced_tx_set, |
| 8922 | )?; |
| 8923 | prev_leaf = Some((leaf_mi, leaf_mode)); |
| 8924 | } |
| 8925 | // `record()`'s `above_mode`/`left_mode` |
| 8926 | // write is a no-op at an 8x8 leaf's own |
| 8927 | // side, so force the write once the whole |
| 8928 | // 16x16 slot's leaves are done, from the |
| 8929 | // last leaf (mirrors the writer's r15 |
| 8930 | // fix). |
| 8931 | if let Some((_, mode)) = prev_leaf { |
| 8932 | neighbours.above_mode[sc] = mode; |
| 8933 | neighbours.left_mode[sr] = mode; |
| 8934 | } |
| 8935 | } |
| 8936 | } |
| 8937 | PARTITION_HORZ => { |
| 8938 | // lane-intradisp r1: two true 32x16 strips. |
| 8939 | decode_block_rect( |
| 8940 | &mut dec, |
| 8941 | &mut cdfs, |
| 8942 | &mut neighbours, |
| 8943 | at32, |
| 8944 | 32, |
| 8945 | 16, |
| 8946 | &mut y, |
| 8947 | &mut u, |
| 8948 | &mut v, |
| 8949 | enable_filter_intra, |
| 8950 | allow_screen_content_tools, |
| 8951 | base_q_idx, |
| 8952 | tx_select, |
| 8953 | reduced_tx_set, |
| 8954 | )?; |
| 8955 | decode_block_rect( |
| 8956 | &mut dec, |
| 8957 | &mut cdfs, |
| 8958 | &mut neighbours, |
| 8959 | (at32.0 + 1, at32.1), |
| 8960 | 32, |
| 8961 | 16, |
| 8962 | &mut y, |
| 8963 | &mut u, |
| 8964 | &mut v, |
| 8965 | enable_filter_intra, |
| 8966 | allow_screen_content_tools, |
| 8967 | base_q_idx, |
| 8968 | tx_select, |
| 8969 | reduced_tx_set, |
| 8970 | )?; |
| 8971 | } |
| 8972 | PARTITION_VERT => { |
| 8973 | // lane-intradisp r1: mirror of PARTITION_HORZ |
| 8974 | // above with width/height swapped. |
| 8975 | decode_block_rect( |
| 8976 | &mut dec, |
| 8977 | &mut cdfs, |
| 8978 | &mut neighbours, |
| 8979 | at32, |
| 8980 | 16, |
| 8981 | 32, |
| 8982 | &mut y, |
| 8983 | &mut u, |
| 8984 | &mut v, |
| 8985 | enable_filter_intra, |
| 8986 | allow_screen_content_tools, |
| 8987 | base_q_idx, |
| 8988 | tx_select, |
| 8989 | reduced_tx_set, |
| 8990 | )?; |
| 8991 | decode_block_rect( |
| 8992 | &mut dec, |
| 8993 | &mut cdfs, |
| 8994 | &mut neighbours, |
| 8995 | (at32.0, at32.1 + 1), |
| 8996 | 16, |
| 8997 | 32, |
| 8998 | &mut y, |
| 8999 | &mut u, |
| 9000 | &mut v, |
| 9001 | enable_filter_intra, |
| 9002 | allow_screen_content_tools, |
| 9003 | base_q_idx, |
| 9004 | tx_select, |
| 9005 | reduced_tx_set, |
| 9006 | )?; |
| 9007 | } |
| 9008 | PARTITION_HORZ_A => { |
| 9009 | // lane-part32 r1: two 16x16 squares on top + |
| 9010 | // a true 32x16 strip below (mirrors |
| 9011 | // decode_inter_block's cd6cb6d HORZ_A: TL, |
| 9012 | // TR, bottom strip). |
| 9013 | INTRA_HORZ_A_HITS.with(|c| c.set(c.get() + 1)); |
| 9014 | decode_block( |
| 9015 | &mut dec, |
| 9016 | &mut cdfs, |
| 9017 | &mut neighbours, |
| 9018 | at32, |
| 9019 | SUB, |
| 9020 | TxbSet::Luma16, |
| 9021 | TxbSet::Chroma8, |
| 9022 | TX16, |
| 9023 | TX8, |
| 9024 | (&scan16, &scan8), |
| 9025 | true, |
| 9026 | &mut y, |
| 9027 | &mut u, |
| 9028 | &mut v, |
| 9029 | base_q_idx, |
| 9030 | enable_filter_intra, |
| 9031 | allow_screen_content_tools, |
| 9032 | allow_intrabc, |
| 9033 | &scan32, |
| 9034 | &scan16, |
| 9035 | &scan8, |
| 9036 | &scan4, |
| 9037 | tx_select, |
| 9038 | reduced_tx_set, |
| 9039 | )?; |
| 9040 | decode_block( |
| 9041 | &mut dec, |
| 9042 | &mut cdfs, |
| 9043 | &mut neighbours, |
| 9044 | (at32.0, at32.1 + 1), |
| 9045 | SUB, |
| 9046 | TxbSet::Luma16, |
| 9047 | TxbSet::Chroma8, |
| 9048 | TX16, |
| 9049 | TX8, |
| 9050 | (&scan16, &scan8), |
| 9051 | true, |
| 9052 | &mut y, |
| 9053 | &mut u, |
| 9054 | &mut v, |
| 9055 | base_q_idx, |
| 9056 | enable_filter_intra, |
| 9057 | allow_screen_content_tools, |
| 9058 | allow_intrabc, |
| 9059 | &scan32, |
| 9060 | &scan16, |
| 9061 | &scan8, |
| 9062 | &scan4, |
| 9063 | tx_select, |
| 9064 | reduced_tx_set, |
| 9065 | )?; |
| 9066 | decode_block_rect( |
| 9067 | &mut dec, |
| 9068 | &mut cdfs, |
| 9069 | &mut neighbours, |
| 9070 | (at32.0 + 1, at32.1), |
| 9071 | 32, |
| 9072 | 16, |
| 9073 | &mut y, |
| 9074 | &mut u, |
| 9075 | &mut v, |
| 9076 | enable_filter_intra, |
| 9077 | allow_screen_content_tools, |
| 9078 | base_q_idx, |
| 9079 | tx_select, |
| 9080 | reduced_tx_set, |
| 9081 | )?; |
| 9082 | } |
| 9083 | PARTITION_HORZ_B => { |
| 9084 | // lane-part32 r1: a true 32x16 strip on top + |
| 9085 | // two 16x16 squares below (mirrors HORZ_A |
| 9086 | // with the strip/squares order flipped, same |
| 9087 | // shape as libaom decode_partition's HORZ_B). |
| 9088 | INTRA_HORZ_B_HITS.with(|c| c.set(c.get() + 1)); |
| 9089 | decode_block_rect( |
| 9090 | &mut dec, |
| 9091 | &mut cdfs, |
| 9092 | &mut neighbours, |
| 9093 | at32, |
| 9094 | 32, |
| 9095 | 16, |
| 9096 | &mut y, |
| 9097 | &mut u, |
| 9098 | &mut v, |
| 9099 | enable_filter_intra, |
| 9100 | allow_screen_content_tools, |
| 9101 | base_q_idx, |
| 9102 | tx_select, |
| 9103 | reduced_tx_set, |
| 9104 | )?; |
| 9105 | decode_block( |
| 9106 | &mut dec, |
| 9107 | &mut cdfs, |
| 9108 | &mut neighbours, |
| 9109 | (at32.0 + 1, at32.1), |
| 9110 | SUB, |
| 9111 | TxbSet::Luma16, |
| 9112 | TxbSet::Chroma8, |
| 9113 | TX16, |
| 9114 | TX8, |
| 9115 | (&scan16, &scan8), |
| 9116 | true, |
| 9117 | &mut y, |
| 9118 | &mut u, |
| 9119 | &mut v, |
| 9120 | base_q_idx, |
| 9121 | enable_filter_intra, |
| 9122 | allow_screen_content_tools, |
| 9123 | allow_intrabc, |
| 9124 | &scan32, |
| 9125 | &scan16, |
| 9126 | &scan8, |
| 9127 | &scan4, |
| 9128 | tx_select, |
| 9129 | reduced_tx_set, |
| 9130 | )?; |
| 9131 | decode_block( |
| 9132 | &mut dec, |
| 9133 | &mut cdfs, |
| 9134 | &mut neighbours, |
| 9135 | (at32.0 + 1, at32.1 + 1), |
| 9136 | SUB, |
| 9137 | TxbSet::Luma16, |
| 9138 | TxbSet::Chroma8, |
| 9139 | TX16, |
| 9140 | TX8, |
| 9141 | (&scan16, &scan8), |
| 9142 | true, |
| 9143 | &mut y, |
| 9144 | &mut u, |
| 9145 | &mut v, |
| 9146 | base_q_idx, |
| 9147 | enable_filter_intra, |
| 9148 | allow_screen_content_tools, |
| 9149 | allow_intrabc, |
| 9150 | &scan32, |
| 9151 | &scan16, |
| 9152 | &scan8, |
| 9153 | &scan4, |
| 9154 | tx_select, |
| 9155 | reduced_tx_set, |
| 9156 | )?; |
| 9157 | } |
| 9158 | PARTITION_VERT_A => { |
| 9159 | // lane-part32 r1: mirror of HORZ_A with |
| 9160 | // width/height swapped (TL, BL, right 16x32 |
| 9161 | // strip). |
| 9162 | INTRA_VERT_A_HITS.with(|c| c.set(c.get() + 1)); |
| 9163 | let _vert_ab = crate::encode::Reach::vert_ab_partition(); |
| 9164 | decode_block( |
| 9165 | &mut dec, |
| 9166 | &mut cdfs, |
| 9167 | &mut neighbours, |
| 9168 | at32, |
| 9169 | SUB, |
| 9170 | TxbSet::Luma16, |
| 9171 | TxbSet::Chroma8, |
| 9172 | TX16, |
| 9173 | TX8, |
| 9174 | (&scan16, &scan8), |
| 9175 | true, |
| 9176 | &mut y, |
| 9177 | &mut u, |
| 9178 | &mut v, |
| 9179 | base_q_idx, |
| 9180 | enable_filter_intra, |
| 9181 | allow_screen_content_tools, |
| 9182 | allow_intrabc, |
| 9183 | &scan32, |
| 9184 | &scan16, |
| 9185 | &scan8, |
| 9186 | &scan4, |
| 9187 | tx_select, |
| 9188 | reduced_tx_set, |
| 9189 | )?; |
| 9190 | decode_block( |
| 9191 | &mut dec, |
| 9192 | &mut cdfs, |
| 9193 | &mut neighbours, |
| 9194 | (at32.0 + 1, at32.1), |
| 9195 | SUB, |
| 9196 | TxbSet::Luma16, |
| 9197 | TxbSet::Chroma8, |
| 9198 | TX16, |
| 9199 | TX8, |
| 9200 | (&scan16, &scan8), |
| 9201 | true, |
| 9202 | &mut y, |
| 9203 | &mut u, |
| 9204 | &mut v, |
| 9205 | base_q_idx, |
| 9206 | enable_filter_intra, |
| 9207 | allow_screen_content_tools, |
| 9208 | allow_intrabc, |
| 9209 | &scan32, |
| 9210 | &scan16, |
| 9211 | &scan8, |
| 9212 | &scan4, |
| 9213 | tx_select, |
| 9214 | reduced_tx_set, |
| 9215 | )?; |
| 9216 | decode_block_rect( |
| 9217 | &mut dec, |
| 9218 | &mut cdfs, |
| 9219 | &mut neighbours, |
| 9220 | (at32.0, at32.1 + 1), |
| 9221 | 16, |
| 9222 | 32, |
| 9223 | &mut y, |
| 9224 | &mut u, |
| 9225 | &mut v, |
| 9226 | enable_filter_intra, |
| 9227 | allow_screen_content_tools, |
| 9228 | base_q_idx, |
| 9229 | tx_select, |
| 9230 | reduced_tx_set, |
| 9231 | )?; |
| 9232 | } |
| 9233 | PARTITION_VERT_B => { |
| 9234 | // lane-part32 r1: a true 16x32 strip on the |
| 9235 | // left + two 16x16 squares on the right |
| 9236 | // (libaom decode_partition VERT_B: left |
| 9237 | // strip, TR, BR). |
| 9238 | INTRA_VERT_B_HITS.with(|c| c.set(c.get() + 1)); |
| 9239 | let _vert_ab = crate::encode::Reach::vert_ab_partition(); |
| 9240 | decode_block_rect( |
| 9241 | &mut dec, |
| 9242 | &mut cdfs, |
| 9243 | &mut neighbours, |
| 9244 | at32, |
| 9245 | 16, |
| 9246 | 32, |
| 9247 | &mut y, |
| 9248 | &mut u, |
| 9249 | &mut v, |
| 9250 | enable_filter_intra, |
| 9251 | allow_screen_content_tools, |
| 9252 | base_q_idx, |
| 9253 | tx_select, |
| 9254 | reduced_tx_set, |
| 9255 | )?; |
| 9256 | decode_block( |
| 9257 | &mut dec, |
| 9258 | &mut cdfs, |
| 9259 | &mut neighbours, |
| 9260 | (at32.0, at32.1 + 1), |
| 9261 | SUB, |
| 9262 | TxbSet::Luma16, |
| 9263 | TxbSet::Chroma8, |
| 9264 | TX16, |
| 9265 | TX8, |
| 9266 | (&scan16, &scan8), |
| 9267 | true, |
| 9268 | &mut y, |
| 9269 | &mut u, |
| 9270 | &mut v, |
| 9271 | base_q_idx, |
| 9272 | enable_filter_intra, |
| 9273 | allow_screen_content_tools, |
| 9274 | allow_intrabc, |
| 9275 | &scan32, |
| 9276 | &scan16, |
| 9277 | &scan8, |
| 9278 | &scan4, |
| 9279 | tx_select, |
| 9280 | reduced_tx_set, |
| 9281 | )?; |
| 9282 | decode_block( |
| 9283 | &mut dec, |
| 9284 | &mut cdfs, |
| 9285 | &mut neighbours, |
| 9286 | (at32.0 + 1, at32.1 + 1), |
| 9287 | SUB, |
| 9288 | TxbSet::Luma16, |
| 9289 | TxbSet::Chroma8, |
| 9290 | TX16, |
| 9291 | TX8, |
| 9292 | (&scan16, &scan8), |
| 9293 | true, |
| 9294 | &mut y, |
| 9295 | &mut u, |
| 9296 | &mut v, |
| 9297 | base_q_idx, |
| 9298 | enable_filter_intra, |
| 9299 | allow_screen_content_tools, |
| 9300 | allow_intrabc, |
| 9301 | &scan32, |
| 9302 | &scan16, |
| 9303 | &scan8, |
| 9304 | &scan4, |
| 9305 | tx_select, |
| 9306 | reduced_tx_set, |
| 9307 | )?; |
| 9308 | } |
| 9309 | _ => { |
| 9310 | return Err(unsupported(format!( |
| 9311 | "a 32x32 partition type this decoder does not code (value={part32})" |
| 9312 | ))); |
| 9313 | } |
| 9314 | } |
| 9315 | } |
| 9316 | } |
| 9317 | PARTITION_HORZ => { |
| 9318 | // lane-sbpart r2: two true 64x32 strips. |
| 9319 | decode_block_rect64( |
| 9320 | &mut dec, |
| 9321 | &mut cdfs, |
| 9322 | &mut neighbours, |
| 9323 | at, |
| 9324 | 64, |
| 9325 | 32, |
| 9326 | &mut y, |
| 9327 | &mut u, |
| 9328 | &mut v, |
| 9329 | enable_filter_intra, |
| 9330 | allow_screen_content_tools, |
| 9331 | base_q_idx, |
| 9332 | tx_select, |
| 9333 | reduced_tx_set, |
| 9334 | )?; |
| 9335 | decode_block_rect64( |
| 9336 | &mut dec, |
| 9337 | &mut cdfs, |
| 9338 | &mut neighbours, |
| 9339 | (at.0 + 2, at.1), |
| 9340 | 64, |
| 9341 | 32, |
| 9342 | &mut y, |
| 9343 | &mut u, |
| 9344 | &mut v, |
| 9345 | enable_filter_intra, |
| 9346 | allow_screen_content_tools, |
| 9347 | base_q_idx, |
| 9348 | tx_select, |
| 9349 | reduced_tx_set, |
| 9350 | )?; |
| 9351 | } |
| 9352 | PARTITION_VERT => { |
| 9353 | // lane-sbpart r2: mirror of PARTITION_HORZ above with |
| 9354 | // width/height swapped. |
| 9355 | decode_block_rect64( |
| 9356 | &mut dec, |
| 9357 | &mut cdfs, |
| 9358 | &mut neighbours, |
| 9359 | at, |
| 9360 | 32, |
| 9361 | 64, |
| 9362 | &mut y, |
| 9363 | &mut u, |
| 9364 | &mut v, |
| 9365 | enable_filter_intra, |
| 9366 | allow_screen_content_tools, |
| 9367 | base_q_idx, |
| 9368 | tx_select, |
| 9369 | reduced_tx_set, |
| 9370 | )?; |
| 9371 | decode_block_rect64( |
| 9372 | &mut dec, |
| 9373 | &mut cdfs, |
| 9374 | &mut neighbours, |
| 9375 | (at.0, at.1 + 2), |
| 9376 | 32, |
| 9377 | 64, |
| 9378 | &mut y, |
| 9379 | &mut u, |
| 9380 | &mut v, |
| 9381 | enable_filter_intra, |
| 9382 | allow_screen_content_tools, |
| 9383 | base_q_idx, |
| 9384 | tx_select, |
| 9385 | reduced_tx_set, |
| 9386 | )?; |
| 9387 | } |
| 9388 | PARTITION_HORZ_A | PARTITION_HORZ_B | PARTITION_VERT_A | PARTITION_VERT_B => { |
| 9389 | // lane-part32 r4: the four superblock-level AB arms, each |
| 9390 | // two 32x32 squares plus one 64x32/32x64 strip, in |
| 9391 | // libaom's own `decode_partition` order (decodeframe.c: |
| 9392 | // HORZ_A = TL, TR, bottom strip; HORZ_B = top strip, BL, |
| 9393 | // BR; VERT_A = TL, BL, right strip; VERT_B = left strip, |
| 9394 | // TR, BR). The pieces are exactly the ones already proven |
| 9395 | // by the `PARTITION_NONE`-under-SPLIT (32x32 square) and |
| 9396 | // `PARTITION_HORZ`/`VERT` (rect64 strip) arms above. |
| 9397 | SB_AB_HITS.with(|c| { |
| 9398 | let mut h = c.get(); |
| 9399 | h[part - PARTITION_HORZ_A] += 1; |
| 9400 | c.set(h); |
| 9401 | }); |
| 9402 | // lane-part32 r5: only the two VERTICAL arms reorder the |
| 9403 | // square sub-blocks (TL, BL, TR, BR), so only they switch |
| 9404 | // libaom's `has_tr`/`has_bl` tables. |
| 9405 | let _vert_ab = (part == PARTITION_VERT_A || part == PARTITION_VERT_B) |
| 9406 | .then(crate::encode::Reach::vert_ab_partition); |
| 9407 | macro_rules! square32 { |
| 9408 | ($at:expr) => { |
| 9409 | decode_block( |
| 9410 | &mut dec, |
| 9411 | &mut cdfs, |
| 9412 | &mut neighbours, |
| 9413 | $at, |
| 9414 | BLOCK, |
| 9415 | TxbSet::Luma32, |
| 9416 | TxbSet::Chroma16, |
| 9417 | TX32, |
| 9418 | TX16, |
| 9419 | (&scan32, &scan16), |
| 9420 | true, |
| 9421 | &mut y, |
| 9422 | &mut u, |
| 9423 | &mut v, |
| 9424 | base_q_idx, |
| 9425 | enable_filter_intra, |
| 9426 | allow_screen_content_tools, |
| 9427 | allow_intrabc, |
| 9428 | &scan32, |
| 9429 | &scan16, |
| 9430 | &scan8, |
| 9431 | &scan4, |
| 9432 | tx_select, |
| 9433 | reduced_tx_set, |
| 9434 | )? |
| 9435 | }; |
| 9436 | } |
| 9437 | macro_rules! strip64 { |
| 9438 | ($at:expr, $bw:expr, $bh:expr) => { |
| 9439 | decode_block_rect64( |
| 9440 | &mut dec, |
| 9441 | &mut cdfs, |
| 9442 | &mut neighbours, |
| 9443 | $at, |
| 9444 | $bw, |
| 9445 | $bh, |
| 9446 | &mut y, |
| 9447 | &mut u, |
| 9448 | &mut v, |
| 9449 | enable_filter_intra, |
| 9450 | allow_screen_content_tools, |
| 9451 | base_q_idx, |
| 9452 | tx_select, |
| 9453 | reduced_tx_set, |
| 9454 | )? |
| 9455 | }; |
| 9456 | } |
| 9457 | let (br, bc) = (at.0 + 2, at.1 + 2); |
| 9458 | match part { |
| 9459 | PARTITION_HORZ_A => { |
| 9460 | square32!(at); |
| 9461 | square32!((at.0, bc)); |
| 9462 | strip64!((br, at.1), 64, 32); |
| 9463 | } |
| 9464 | PARTITION_HORZ_B => { |
| 9465 | strip64!(at, 64, 32); |
| 9466 | square32!((br, at.1)); |
| 9467 | square32!((br, bc)); |
| 9468 | } |
| 9469 | PARTITION_VERT_A => { |
| 9470 | square32!(at); |
| 9471 | square32!((br, at.1)); |
| 9472 | strip64!((at.0, bc), 32, 64); |
| 9473 | } |
| 9474 | _ => { |
| 9475 | strip64!(at, 32, 64); |
| 9476 | square32!((at.0, bc)); |
| 9477 | square32!((br, bc)); |
| 9478 | } |
| 9479 | } |
| 9480 | } |
| 9481 | _ => { |
| 9482 | return Err(unsupported( |
| 9483 | "a superblock-level 1:4 partition (PARTITION_HORZ_4/VERT_4 at 64x64, \ |
| 9484 | four 64x16/16x64 strips)", |
| 9485 | )); |
| 9486 | } |
| 9487 | } |
| 9488 | } |
| 9489 | } |
| 9490 | |
| 9491 | if tile_num == tile_info.context_update_tile_id { |
| 9492 | result_cdfs = cdfs; |
| 9493 | } |
| 9494 | } |
| 9495 | |
| 9496 | // lane-comppin r4: pre-loop-filter decode-order dump, matching aomdec's |
| 9497 | // own EC_AV1_PREFILT_DUMP shape (decodeframe.c ~5451) -- diffs against |
| 9498 | // that isolate whether a decode-order frame's mismatch already exists |
| 9499 | // in reconstruction (this dump) vs is introduced by the loop filter |
| 9500 | // (EC_AV1_DECODE_ORDER_DUMP's post-filter dump). |
| 9501 | if let Ok(path) = std::env::var("EC_AV1_PREFILT_DUMP") { |
| 9502 | use std::io::Write; |
| 9503 | let idx = PREFILT_PICTURE_IDX.fetch_add(1, std::sync::atomic::Ordering::SeqCst); |
| 9504 | if let Ok(mut f) = std::fs::File::create(format!("{path}.f{idx}")) { |
| 9505 | let narrow = |p: &PlaneBuf| -> Vec<u8> { p.data.iter().map(|&s| s as u8).collect() }; |
| 9506 | let _ = f.write_all(&narrow(&y)); |
| 9507 | let _ = f.write_all(&narrow(&u)); |
| 9508 | let _ = f.write_all(&narrow(&v)); |
| 9509 | } |
| 9510 | } else { |
| 9511 | PREFILT_PICTURE_IDX.fetch_add(1, std::sync::atomic::Ordering::SeqCst); |
| 9512 | } |
| 9513 | if let Ok(path) = std::env::var("EC_AV1_PREFILT_WIDE_DUMP") { |
| 9514 | use std::io::Write; |
| 9515 | static IDX2: std::sync::atomic::AtomicUsize = std::sync::atomic::AtomicUsize::new(0); |
| 9516 | let idx = IDX2.fetch_add(1, std::sync::atomic::Ordering::SeqCst); |
| 9517 | let crop_wide = |plane: &PlaneBuf| -> Vec<u8> { |
| 9518 | let mut out = Vec::with_capacity(plane.true_width * plane.true_height); |
| 9519 | for row in 0..plane.true_height { |
| 9520 | out.extend(plane.data[row * plane.width..][..plane.true_width].iter().map(|&s| s as u8)); |
| 9521 | } |
| 9522 | out |
| 9523 | }; |
| 9524 | if let Ok(mut f) = std::fs::File::create(format!("{path}.f{idx}")) { |
| 9525 | let _ = f.write_all(&crop_wide(&y)); |
| 9526 | let _ = f.write_all(&crop_wide(&u)); |
| 9527 | let _ = f.write_all(&crop_wide(&v)); |
| 9528 | } |
| 9529 | } |
| 9530 | apply_deblock( |
| 9531 | &mut y, |
| 9532 | &mut u, |
| 9533 | &mut v, |
| 9534 | loop_filter, |
| 9535 | &neighbours, |
| 9536 | frame_width as usize, |
| 9537 | frame_height as usize, |
| 9538 | ); |
| 9539 | let (deblocked_y, deblocked_u, deblocked_v) = (y.clone(), u.clone(), v.clone()); |
| 9540 | // lane-tiny r4: post-deblock / post-CDEF dumps mirroring aomdec's own |
| 9541 | // EC_AV1_POSTDEBLOCK_DUMP (decodeframe.c ~5404) -- with the pre-filter |
| 9542 | // dump above these bisect WHICH filter stage introduced a mismatch. |
| 9543 | dump_stage("EC_AV1_POSTDEBLOCK_DUMP", &y, &u, &v); |
| 9544 | apply_cdef(&mut y, &mut u, &mut v, cdef, &neighbours); |
| 9545 | dump_stage("EC_AV1_POSTCDEF_DUMP", &y, &u, &v); |
| 9546 | apply_loop_restoration(&mut y, &mut u, &mut v, &deblocked_y, &deblocked_u, &deblocked_v, lr, &lr_grid); |
| 9547 | |
| 9548 | let (fw, fh) = (frame_width as usize, frame_height as usize); |
| 9549 | if fw == width && fh == height { |
| 9550 | LAST_FRAME_WIDE_MARGIN.with(|m| *m.borrow_mut() = None); |
| 9551 | return Ok(( |
| 9552 | Picture { |
| 9553 | width, |
| 9554 | height, |
| 9555 | y: y.data, |
| 9556 | u: u.data, |
| 9557 | v: v.data, |
| 9558 | }, |
| 9559 | result_cdfs, |
| 9560 | )); |
| 9561 | } |
| 9562 | let crop = |plane: &PlaneBuf, w: usize, h: usize| -> Vec<u16> { |
| 9563 | let mut out = Vec::with_capacity(w * h); |
| 9564 | for row in 0..h { |
| 9565 | out.extend(plane.data[row * plane.width..][..w].iter().copied()); |
| 9566 | } |
| 9567 | out |
| 9568 | }; |
| 9569 | // lane-superres r3: `true_width`/`true_height` (`mi_cols`/`mi_rows` * 4) |
| 9570 | // is the real reconstructed extent -- columns `[fw, true_width)` hold |
| 9571 | // genuine decoded samples (the last coding block straddles the frame |
| 9572 | // edge whenever `frame_width` isn't 8-sample aligned), not padding. |
| 9573 | // libaom's superres upscale runs on that buffer's own border-extended |
| 9574 | // margin, so it reads those real columns, not a synthetic replicate of |
| 9575 | // column `fw - 1`. Stash them for `stream.rs`'s superres path (a real |
| 9576 | // aomenc 43->64 stream pinned column-by-column against libaom's own |
| 9577 | // `av1_convolve_horiz_rs_c` via `scripts/superres-pin-harness.c` -- |
| 9578 | // `row6-realedgeval` proved the replicate-of-`fw-1` padding this |
| 9579 | // decoder used before was off by 1 at the columns the tap window |
| 9580 | // reaches into that real margin; `None` when there is no margin (this |
| 9581 | // frame's `true_width`/`true_height` already equal `fw`/`fh`, e.g. a |
| 9582 | // non-superres frame whose width just isn't 32-block aligned). |
| 9583 | LAST_FRAME_WIDE_MARGIN.with(|m| { |
| 9584 | *m.borrow_mut() = if true_width > fw || true_height > fh { |
| 9585 | let yc = crop(&y, true_width, true_height); |
| 9586 | if let Ok(path) = std::env::var("EC_AV1_MARGIN_DUMP") { |
| 9587 | use std::io::Write; |
| 9588 | static IDX: std::sync::atomic::AtomicUsize = std::sync::atomic::AtomicUsize::new(0); |
| 9589 | let idx = IDX.fetch_add(1, std::sync::atomic::Ordering::SeqCst); |
| 9590 | if let Ok(mut f) = std::fs::File::create(format!("{path}.f{idx}")) { |
| 9591 | // corner-cut: this diagnostic dump stays 8-bit-narrowed |
| 9592 | // regardless of the stream's real bit depth (debug-only, |
| 9593 | // never read by a gate); widen if a >8-bit margin dump |
| 9594 | // is ever needed. |
| 9595 | let _ = f.write_all(&yc.iter().map(|&s| s as u8).collect::<Vec<u8>>()); |
| 9596 | } |
| 9597 | } |
| 9598 | Some(Picture { |
| 9599 | width: true_width, |
| 9600 | height: true_height, |
| 9601 | y: yc, |
| 9602 | u: crop(&u, true_width.div_ceil(2), true_height.div_ceil(2)), |
| 9603 | v: crop(&v, true_width.div_ceil(2), true_height.div_ceil(2)), |
| 9604 | }) |
| 9605 | } else { |
| 9606 | None |
| 9607 | } |
| 9608 | }); |
| 9609 | Ok(( |
| 9610 | Picture { |
| 9611 | width: fw, |
| 9612 | height: fh, |
| 9613 | y: crop(&y, fw, fh), |
| 9614 | u: crop(&u, fw.div_ceil(2), fh.div_ceil(2)), |
| 9615 | v: crop(&v, fw.div_ceil(2), fh.div_ceil(2)), |
| 9616 | }, |
| 9617 | result_cdfs, |
| 9618 | )) |
| 9619 | } |
| 9620 | |
| 9621 | /// `av1_get_pred_context_switchable_interp`'s context for `interp_filter[dir]` |
| 9622 | /// -- `above`/`left` are the neighbour's own resolved index for this |
| 9623 | /// direction (0..=2, or `3` = no info, spec `SWITCHABLE_FILTERS`). |
| 9624 | /// `is_compound` folds in libaom `av1_get_pred_context_switchable_interp`'s |
| 9625 | /// own `ctx_offset` term (`INTER_FILTER_COMP_OFFSET` = 4, added when *this* |
| 9626 | /// block itself has a second reference) -- lane-av1idx: a stale "this |
| 9627 | /// decoder never codes a compound reference" assumption baked this to |
| 9628 | /// always-0, which desynced the CDF context bucket (not the decoded value |
| 9629 | /// outright, hence small pixel deltas rather than a hard desync) the moment |
| 9630 | /// lane-av1blend unmasked plain-average compound blocks on a Switchable- |
| 9631 | /// filter frame. |
| 9632 | fn switchable_interp_ctx(above: u8, left: u8, dir: usize, is_compound: bool) -> usize { |
| 9633 | let base = dir * 8 + if is_compound { 4 } else { 0 }; |
| 9634 | let term = if left == above { |
| 9635 | left |
| 9636 | } else if left == 3 { |
| 9637 | above |
| 9638 | } else if above == 3 { |
| 9639 | left |
| 9640 | } else { |
| 9641 | 3 |
| 9642 | }; |
| 9643 | base + term as usize |
| 9644 | } |
| 9645 | |
| 9646 | /// Resolves the interpolation filter kernel(s) an inter block's motion |
| 9647 | /// compensation reads (spec 5.11.10/5.11.20): a fixed-filter frame |
| 9648 | /// (`interp_fixed = Some`) never reads a symbol; a `Switchable` frame |
| 9649 | /// (`interp_fixed = None`) reads `interp_filter[0]` and, when |
| 9650 | /// `enable_dual_filter`, a second `interp_filter[1]` -- unless |
| 9651 | /// `force_regular` (this block's own `needs_interp_filter() == 0` case: a |
| 9652 | /// `GLOBALMV` block, whose global motion is always `IDENTITY` in this |
| 9653 | /// decoder's scope, spec 5.11.10's `large && YMode == GLOBALMV` branch), |
| 9654 | /// which forces `EIGHTTAP` without reading anything. |
| 9655 | #[allow(clippy::too_many_arguments)] |
| 9656 | fn resolve_interp_filter( |
| 9657 | dec: &mut SymbolDecoder, |
| 9658 | cdfs: &mut Cdfs, |
| 9659 | interp_fixed: Option<mc::InterpFilterKind>, |
| 9660 | enable_dual_filter: bool, |
| 9661 | force_regular: bool, |
| 9662 | above: [u8; 2], |
| 9663 | left: [u8; 2], |
| 9664 | is_compound: bool, |
| 9665 | ) -> (mc::InterpFilterKind, mc::InterpFilterKind, [u8; 2]) { |
| 9666 | if let Some(kind) = interp_fixed { |
| 9667 | return (kind, kind, [3, 3]); |
| 9668 | } |
| 9669 | if force_regular { |
| 9670 | return ( |
| 9671 | mc::InterpFilterKind::Regular, |
| 9672 | mc::InterpFilterKind::Regular, |
| 9673 | [0, 0], |
| 9674 | ); |
| 9675 | } |
| 9676 | // libaom `av1_extract_interp_filter`/`read_mb_interp_filter`: the |
| 9677 | // symbol read at `dir=0` becomes the block's *vertical* (`y_filter`) |
| 9678 | // kernel, `dir=1` the *horizontal* (`x_filter`) one -- `dir` is not |
| 9679 | // "horizontal then vertical" despite `predict_with_filters`' own |
| 9680 | // `h_kind`/`v_kind` argument order, which is why this function returns |
| 9681 | // `(h, v, ..)` but reads `dir0` (`v`) before `dir1` (`h`). |
| 9682 | let ctx0 = switchable_interp_ctx(above[0], left[0], 0, is_compound); |
| 9683 | if std::env::var_os("EC_AV1_IFDBG").is_some() { |
| 9684 | eprintln!( |
| 9685 | "IFDBG dir=0 ctx={ctx0} above={above:?} left={left:?} cdf={:?}", |
| 9686 | &cdfs.switchable_interp[ctx0][..3] |
| 9687 | ); |
| 9688 | } |
| 9689 | let sym0 = dec.symbol(&mut cdfs.switchable_interp[ctx0]) as u8; |
| 9690 | let sym1 = if enable_dual_filter { |
| 9691 | let ctx1 = switchable_interp_ctx(above[1], left[1], 1, is_compound); |
| 9692 | if std::env::var_os("EC_AV1_IFDBG").is_some() { |
| 9693 | eprintln!( |
| 9694 | "IFDBG dir=1 ctx={ctx1} above={above:?} left={left:?} cdf={:?} sym0={sym0}", |
| 9695 | &cdfs.switchable_interp[ctx1][..3] |
| 9696 | ); |
| 9697 | } |
| 9698 | dec.symbol(&mut cdfs.switchable_interp[ctx1]) as u8 |
| 9699 | } else { |
| 9700 | sym0 |
| 9701 | }; |
| 9702 | ( |
| 9703 | mc::InterpFilterKind::from_switchable_symbol(sym1 as usize), |
| 9704 | mc::InterpFilterKind::from_switchable_symbol(sym0 as usize), |
| 9705 | [sym0, sym1], |
| 9706 | ) |
| 9707 | } |
| 9708 | |
| 9709 | /// The `is_inter` context (spec 5.11.16 via `av1_get_intra_inter_context`), |
| 9710 | /// duplicating [`crate::tile`]'s private copy of the same rule. |
| 9711 | pub(crate) fn intra_inter_ctx( |
| 9712 | has_above: bool, |
| 9713 | has_left: bool, |
| 9714 | above_inter: bool, |
| 9715 | left_inter: bool, |
| 9716 | ) -> usize { |
| 9717 | match (has_above, has_left) { |
| 9718 | (true, true) => { |
| 9719 | let (above_intra, left_intra) = (!above_inter, !left_inter); |
| 9720 | if above_intra && left_intra { |
| 9721 | 3 |
| 9722 | } else { |
| 9723 | usize::from(above_intra || left_intra) |
| 9724 | } |
| 9725 | } |
| 9726 | (true, false) => 2 * usize::from(!above_inter), |
| 9727 | (false, true) => 2 * usize::from(!left_inter), |
| 9728 | (false, false) => 0, |
| 9729 | } |
| 9730 | } |
| 9731 | |
| 9732 | /// `CLASS0_SIZE << (class + 2)`, duplicating `crate::tile`'s private |
| 9733 | /// `mv_class_base` (spec 3): the magnitude an `MV_CLASS_n` component's own |
| 9734 | /// bits start counting from. |
| 9735 | fn mv_class_base(class: usize) -> i32 { |
| 9736 | if class == 0 { 0 } else { 2i32 << (class + 2) } |
| 9737 | } |
| 9738 | |
| 9739 | /// One motion vector component's non-zero diff (spec 5.11.32 |
| 9740 | /// `read_mv_component`), the inverse of [`crate::tile`]'s private |
| 9741 | /// `write_mv_component`. |
| 9742 | fn read_mv_component( |
| 9743 | dec: &mut SymbolDecoder, |
| 9744 | c: &mut MvComponentCdfs, |
| 9745 | allow_high_precision_mv: bool, |
| 9746 | force_integer_mv: bool, |
| 9747 | ) -> i32 { |
| 9748 | let sign = dec.symbol(&mut c.sign); |
| 9749 | let class = dec.symbol(&mut c.class); |
| 9750 | let local = if class == 0 { |
| 9751 | let bit = dec.symbol(&mut c.class0_bit); |
| 9752 | // spec 5.11.32 `mv_class0_fr`: not coded at all when the frame |
| 9753 | // forces integer motion vectors -- implicitly `3` (the top |
| 9754 | // fractional value) rather than read, else every symbol after it |
| 9755 | // in the tile desyncs by one read. |
| 9756 | let fr = if force_integer_mv { |
| 9757 | 3 |
| 9758 | } else { |
| 9759 | dec.symbol(&mut c.class0_fr[bit]) |
| 9760 | }; |
| 9761 | // spec 5.11.32 `mv_class0_hp`: read only when the frame allows |
| 9762 | // high-precision motion vectors, else implicitly 1 (the low bit |
| 9763 | // stays at half-pel precision) -- this crate's own writer always |
| 9764 | // leaves the flag off, but a foreign stream can set it per frame. |
| 9765 | let hp = if allow_high_precision_mv { |
| 9766 | dec.symbol(&mut c.class0_hp) |
| 9767 | } else { |
| 9768 | 1 |
| 9769 | }; |
| 9770 | (bit << 3) | (fr << 1) | hp |
| 9771 | } else { |
| 9772 | let mut d = 0; |
| 9773 | for i in 0..class { |
| 9774 | d |= dec.symbol(&mut c.bit[i]) << i; |
| 9775 | } |
| 9776 | // spec 5.11.32 `mv_fr`: same force_integer_mv carve-out as |
| 9777 | // `mv_class0_fr` above. |
| 9778 | let fr = if force_integer_mv { 3 } else { dec.symbol(&mut c.fr) }; |
| 9779 | let hp = if allow_high_precision_mv { |
| 9780 | dec.symbol(&mut c.hp) |
| 9781 | } else { |
| 9782 | 1 |
| 9783 | }; |
| 9784 | (d << 3) | (fr << 1) | hp |
| 9785 | }; |
| 9786 | let mag = mv_class_base(class) + local as i32 + 1; |
| 9787 | if sign == 1 { -mag } else { mag } |
| 9788 | } |
| 9789 | |
| 9790 | /// A motion vector coded as a residual against `pred` (spec 5.11.32 |
| 9791 | /// `read_mv`), the inverse of [`crate::tile`]'s private `write_mv`. |
| 9792 | fn read_mv( |
| 9793 | dec: &mut SymbolDecoder, |
| 9794 | mv_comp: &mut [MvComponentCdfs; 2], |
| 9795 | mv_joint: &mut [u16; 5], |
| 9796 | pred: (i32, i32), |
| 9797 | allow_high_precision_mv: bool, |
| 9798 | force_integer_mv: bool, |
| 9799 | ) -> (i32, i32) { |
| 9800 | let joint = dec.symbol(mv_joint); |
| 9801 | let mut diff = (0, 0); |
| 9802 | if joint == 2 || joint == 3 { |
| 9803 | diff.0 = read_mv_component(dec, &mut mv_comp[0], allow_high_precision_mv, force_integer_mv); |
| 9804 | } |
| 9805 | if joint == 1 || joint == 3 { |
| 9806 | diff.1 = read_mv_component(dec, &mut mv_comp[1], allow_high_precision_mv, force_integer_mv); |
| 9807 | } |
| 9808 | (pred.0 + diff.0, pred.1 + diff.1) |
| 9809 | } |
| 9810 | |
| 9811 | /// One intrabc block's block vector (spec 5.11.13 `read_intrabc_info` -> |
| 9812 | /// `assign_mv` with `use_intrabc`, libaom `assign_dv`/`av1_find_ref_dv`). |
| 9813 | /// |
| 9814 | /// The predictor is the ordinary MV stack built against `INTRA_FRAME` |
| 9815 | /// (`av1_find_mv_refs(..., INTRA_FRAME, ...)`): `nearestmv`, or `nearmv` |
| 9816 | /// when that is zero, or -- when both are -- the fixed fallback DV one |
| 9817 | /// superblock up, or `INTRABC_DELAY_PIXELS` (256) plus one superblock to the |
| 9818 | /// left when this is the tile's first superblock row. The predictor is then |
| 9819 | /// rounded to full pel, and the difference reads off the *dv* nmv context |
| 9820 | /// (libaom `ndvc`) at full-pel precision (`MV_SUBPEL_NONE`: no `fr`/`hp` |
| 9821 | /// symbols, exactly this decoder's `force_integer_mv` carve-out). No DRL |
| 9822 | /// symbol is coded for a DV, so none of this can move the bitstream |
| 9823 | /// position -- only the reconstructed vector. |
| 9824 | fn read_intrabc_dv( |
| 9825 | dec: &mut SymbolDecoder, |
| 9826 | cdfs: &mut Cdfs, |
| 9827 | mi_r: usize, |
| 9828 | mi_c: usize, |
| 9829 | side: usize, |
| 9830 | ) -> (i32, i32) { |
| 9831 | const INTRABC_DELAY_PIXELS: i32 = 256; |
| 9832 | let n4 = side / MI; |
| 9833 | let stack_pred = INTRABC_MI_GRID.with(|g| { |
| 9834 | let g = g.borrow(); |
| 9835 | g.as_ref().map(|(grid, mi_cols, mi_rows)| { |
| 9836 | let stack = crate::mvstack::find_mv_stack( |
| 9837 | grid, mi_r, mi_c, n4, n4, 0, /* INTRA_FRAME */ |
| 9838 | *mi_cols, *mi_rows, |
| 9839 | ); |
| 9840 | if stack.nearest_mv == (0, 0) { |
| 9841 | stack.near_mv |
| 9842 | } else { |
| 9843 | stack.nearest_mv |
| 9844 | } |
| 9845 | }) |
| 9846 | }); |
| 9847 | let mut pred = stack_pred.unwrap_or((0, 0)); |
| 9848 | if pred == (0, 0) { |
| 9849 | // `av1_find_ref_dv` (mvref_common.c). Tile row start is 0 here (the |
| 9850 | // single-tile-row case every intrabc gate stream is). |
| 9851 | let sb_px = SB_MI as i32 * MI as i32; |
| 9852 | pred = if mi_r < SB_MI as usize { |
| 9853 | (0, -(sb_px + INTRABC_DELAY_PIXELS) * 8) |
| 9854 | } else { |
| 9855 | (-sb_px * 8, 0) |
| 9856 | }; |
| 9857 | } |
| 9858 | // "Ref DV should not have sub-pel" (`assign_dv`): floor to full pel. |
| 9859 | let pred = ((pred.0 >> 3) * 8, (pred.1 >> 3) * 8); |
| 9860 | read_mv(dec, &mut cdfs.dv_comp, &mut cdfs.dv_joint, pred, false, true) |
| 9861 | } |
| 9862 | |
| 9863 | /// A 1/8-pel motion vector component converted to the 1/16-pel offset |
| 9864 | /// [`mc::predict`] takes, duplicating [`crate::encode`]'s private `mv_to_q4` |
| 9865 | /// (its own doc comment explains the `*2`/`*1` luma/chroma split). |
| 9866 | fn mv_to_q4(pos: usize, mv_component: i32, luma: bool) -> i32 { |
| 9867 | (pos as i32) * 16 + mv_component * if luma { 2 } else { 1 } |
| 9868 | } |
| 9869 | |
| 9870 | impl PlaneBuf { |
| 9871 | /// Adds `residual` onto an already-computed `prediction` (motion |
| 9872 | /// compensation's output, rather than [`predict`]'s intra one), writing |
| 9873 | /// the clamped reconstruction into the plane at `(x, y)` -- the inter |
| 9874 | /// counterpart of [`PlaneBuf::reconstruct`]. |
| 9875 | fn reconstruct_mc( |
| 9876 | &mut self, |
| 9877 | x: usize, |
| 9878 | y: usize, |
| 9879 | side: usize, |
| 9880 | prediction: &[u16], |
| 9881 | residual: &[i32], |
| 9882 | ) { |
| 9883 | self.reconstruct_mc_rect(x, y, side, side, side, prediction, residual); |
| 9884 | } |
| 9885 | |
| 9886 | /// [`Self::reconstruct_mc`], writing only a `w`x`h` sub-rectangle of the |
| 9887 | /// `side`x`side` `prediction`/`residual` buffers (which stay `side`-square |
| 9888 | /// for stride/indexing) -- lane-partitions r1: a true rectangular HORZ/ |
| 9889 | /// VERT strip is predicted at its enclosing square `side` (matching |
| 9890 | /// `HORZ_B`'s already-accepted square-context corner-cut, lane-warp r5) |
| 9891 | /// but only its own true `w`x`h` footprint is committed to the plane, so |
| 9892 | /// two strips sharing a square's coordinate origin never clobber each |
| 9893 | /// other's real pixels. |
| 9894 | fn reconstruct_mc_rect( |
| 9895 | &mut self, |
| 9896 | x: usize, |
| 9897 | y: usize, |
| 9898 | side: usize, |
| 9899 | w: usize, |
| 9900 | h: usize, |
| 9901 | prediction: &[u16], |
| 9902 | residual: &[i32], |
| 9903 | ) { |
| 9904 | for row in 0..h { |
| 9905 | for col in 0..w { |
| 9906 | let sample = (i32::from(prediction[row * side + col]) + residual[row * side + col]) |
| 9907 | .clamp(0, crate::decode::sample_max()) as u16; |
| 9908 | self.data[(y + row) * self.width + x + col] = sample; |
| 9909 | } |
| 9910 | } |
| 9911 | } |
| 9912 | } |
| 9913 | |
| 9914 | /// Reads one inter-coded plane's transform block and reconstructs it by |
| 9915 | /// adding its residual onto a motion-compensated `prediction` -- the inter |
| 9916 | /// counterpart of [`read_plane`] (every inter transform this decoder reads is |
| 9917 | /// `side`-square with no oversized-64 case, so there is no padded-grid |
| 9918 | /// branch to mirror). |
| 9919 | #[allow(clippy::too_many_arguments)] |
| 9920 | fn read_inter_plane( |
| 9921 | dec: &mut SymbolDecoder, |
| 9922 | cdfs: &mut Cdfs, |
| 9923 | set: TxbSet, |
| 9924 | scan: &[u16], |
| 9925 | plane_idx: usize, |
| 9926 | around: (bool, bool, i32), |
| 9927 | tx_mode: usize, |
| 9928 | plane: &mut PlaneBuf, |
| 9929 | x: usize, |
| 9930 | y: usize, |
| 9931 | side: usize, |
| 9932 | base_q_idx: u8, |
| 9933 | prediction: &[u16], |
| 9934 | // lane-chromau: this block's own luma transform's *actual coded* |
| 9935 | // `tx_type` -- `av1_get_tx_type` (libaom `blockd.h`): an inter block's |
| 9936 | // chroma plane never codes its own `tx_type` symbol, but (unlike an |
| 9937 | // intra block, which falls back to `Intra_Mode_To_Tx_Type`) it inherits |
| 9938 | // the colocated luma transform's coded type verbatim, clamped back to |
| 9939 | // `DCT_DCT` at the chroma tx sizes the spec forces DCT-only |
| 9940 | // (`av1_get_ext_tx_set_type`: `tx_size_sqr_up >= TX_32X32`, i.e. this |
| 9941 | // function's own `side >= 32`). `None` for the luma call itself |
| 9942 | // (`plane_idx == 0`), where the `TxbSet`'s own symbol (or the true |
| 9943 | // `DCT_DCT` default at 32-point+) already resolves it without help. |
| 9944 | inherited_luma_tx_type: Option<TxType>, |
| 9945 | // lane-txselect: the luma `txb_skip_ctx` of a transform unit smaller than |
| 9946 | // its own block (spec `get_txb_ctx_general`'s `plane_bsize != tx_size` |
| 9947 | // branch, [`Neighbours::luma_skip_ctx`]) -- `None` for the whole-block |
| 9948 | // transform every non-var-tx caller reads, whose plane-0 context is the |
| 9949 | // lone-transform-unit 0. |
| 9950 | luma_skip_ctx: Option<usize>, |
| 9951 | ) -> Result<(Vec<i32>, TxType)> { |
| 9952 | let skip_ctx = if plane_idx == 0 { |
| 9953 | luma_skip_ctx.unwrap_or(0) |
| 9954 | } else { |
| 9955 | usize::from(around.0) + usize::from(around.1) |
| 9956 | }; |
| 9957 | let mut coding = cdfs.txb(set, tx_mode); |
| 9958 | let default_tx_type = match inherited_luma_tx_type { |
| 9959 | Some(t) if side < 32 => t, |
| 9960 | _ => TxType::DctDct, |
| 9961 | }; |
| 9962 | let (grid, tx_type) = read_coeffs( |
| 9963 | dec, |
| 9964 | &mut coding, |
| 9965 | scan, |
| 9966 | skip_ctx, |
| 9967 | dc_sign_ctx(around.2), |
| 9968 | default_tx_type, |
| 9969 | None, |
| 9970 | )?; |
| 9971 | // lane-inter8 r1: a 64-point transform covers the whole 64x64 area but |
| 9972 | // codes only its top-left 32x32 of frequencies (spec 5.11.40) -- the |
| 9973 | // caller hands the 32-point `scan` there, and the coded corner goes into |
| 9974 | // the top-left of the true `side`-sized grid before dequantization |
| 9975 | // (dqDenom is keyed by the true size, not the corner's), mirroring |
| 9976 | // [`read_plane`]'s own `tx_side != side` branch. Every other transform |
| 9977 | // this decoder codes has `tx_side == side`, so this is inert below 64. |
| 9978 | let tx_side = side.min(32); |
| 9979 | let grid = if tx_side == side { |
| 9980 | grid |
| 9981 | } else { |
| 9982 | let mut full = vec![0i32; side * side]; |
| 9983 | for row in 0..tx_side { |
| 9984 | full[row * side..][..tx_side].copy_from_slice(&grid[row * tx_side..][..tx_side]); |
| 9985 | } |
| 9986 | full |
| 9987 | }; |
| 9988 | let (dc_delta, ac_delta) = plane_q_delta(plane_idx); |
| 9989 | let residual = dequant_and_inverse_typed(&grid, side, bit_depth(), block_q_idx(), dc_delta, ac_delta, tx_type); |
| 9990 | plane.reconstruct_mc(x, y, side, prediction, &residual); |
| 9991 | Ok((grid, tx_type)) |
| 9992 | } |
| 9993 | |
| 9994 | /// Spec 5.11.25's `single_ref_p1`..`p6` tree (reachable only once `comp_mode` |
| 9995 | /// is known `SINGLE_REFERENCE` -- `decode_stream` refuses any frame with |
| 9996 | /// `reference_select` set before this is ever called, so that read is never |
| 9997 | /// needed here). `above_ref`/`left_ref` are `-1` for an intra/unavailable |
| 9998 | /// neighbour, else the `1..=7` reference that neighbour coded (lane-av1refs). |
| 9999 | fn read_single_ref( |
| 10000 | dec: &mut SymbolDecoder, |
| 10001 | cdfs: &mut Cdfs, |
| 10002 | above_ref: i8, |
| 10003 | above_ref1: Option<i8>, |
| 10004 | left_ref: i8, |
| 10005 | left_ref1: Option<i8>, |
| 10006 | ) -> i8 { |
| 10007 | let above = (above_ref > 0).then_some(above_ref); |
| 10008 | let left = (left_ref > 0).then_some(left_ref); |
| 10009 | let p1 = dec.symbol(&mut cdfs.single_ref[single_ref_p1_ctx(above, above_ref1, left, left_ref1)][0]); |
| 10010 | let ref_frame = if p1 == 1 { |
| 10011 | let p2 = dec.symbol(&mut cdfs.single_ref[single_ref_p2_ctx(above, above_ref1, left, left_ref1)][1]); |
| 10012 | if p2 == 1 { |
| 10013 | ALTREF_FRAME |
| 10014 | } else { |
| 10015 | let p6 = dec.symbol(&mut cdfs.single_ref[single_ref_p6_ctx(above, above_ref1, left, left_ref1)][5]); |
| 10016 | if p6 == 1 { ALTREF2_FRAME } else { BWDREF_FRAME } |
| 10017 | } |
| 10018 | } else { |
| 10019 | let p3 = dec.symbol(&mut cdfs.single_ref[single_ref_p3_ctx(above, above_ref1, left, left_ref1)][2]); |
| 10020 | if p3 == 1 { |
| 10021 | let p5 = dec.symbol(&mut cdfs.single_ref[single_ref_p5_ctx(above, above_ref1, left, left_ref1)][4]); |
| 10022 | if p5 == 1 { GOLDEN_FRAME } else { LAST3_FRAME } |
| 10023 | } else { |
| 10024 | let p4 = dec.symbol(&mut cdfs.single_ref[single_ref_p4_ctx(above, above_ref1, left, left_ref1)][3]); |
| 10025 | if p4 == 1 { LAST2_FRAME } else { LAST_FRAME } |
| 10026 | } |
| 10027 | }; |
| 10028 | if ref_frame != LAST_FRAME { |
| 10029 | NON_LAST_REF_HITS.with(|c| c.set(c.get() + 1)); |
| 10030 | let c = match ref_frame { |
| 10031 | LAST2_FRAME => &LAST2_HITS, |
| 10032 | LAST3_FRAME => &LAST3_HITS, |
| 10033 | GOLDEN_FRAME => &GOLDEN_HITS, |
| 10034 | BWDREF_FRAME => &BWDREF_HITS, |
| 10035 | ALTREF2_FRAME => &ALTREF2_HITS, |
| 10036 | ALTREF_FRAME => &ALTREF_HITS, |
| 10037 | _ => unreachable!("read_single_ref only ever returns LAST_FRAME..=ALTREF_FRAME"), |
| 10038 | }; |
| 10039 | c.with(|v| v.set(v.get() + 1)); |
| 10040 | } |
| 10041 | ref_frame |
| 10042 | } |
| 10043 | |
| 10044 | /// Spec 5.11.25's `comp_mode` (lane-av1comp): whether an inter block reads |
| 10045 | /// `SINGLE_REFERENCE` or `COMPOUND_REFERENCE`, only reached once a frame's |
| 10046 | /// `reference_select` header bit lets a block choose per-block instead of |
| 10047 | /// fixing the mode. `above`/`left` are `None` for an intra/unavailable |
| 10048 | /// neighbour, [`NeighbourRef`] for a coded one -- [`reference_mode_ctx`]'s |
| 10049 | /// own doc. Called from [`decode_inter_block`] once its own `reference_select` |
| 10050 | /// parameter is set; [`decode_inter_block8`]'s 8x8 leaf path gates the same |
| 10051 | /// way with a coarser (`LAST_FRAME`-only) neighbour shape. |
| 10052 | /// Resolves a compound reference's own planes -- the same `ref_frame == |
| 10053 | /// LAST_FRAME -> ref_y/u/v, else other_refs[ref_frame]` lookup |
| 10054 | /// [`decode_inter_block`]'s single-ref path already does, factored out so |
| 10055 | /// the compound path can call it twice (lane-av1comp). |
| 10056 | fn ref_planes<'a>( |
| 10057 | ref_frame: i8, |
| 10058 | ref_y: &'a PlaneBuf, |
| 10059 | ref_u: &'a PlaneBuf, |
| 10060 | ref_v: &'a PlaneBuf, |
| 10061 | other_refs: &'a RefSlots, |
| 10062 | ) -> Result<(&'a PlaneBuf, &'a PlaneBuf, &'a PlaneBuf)> { |
| 10063 | if ref_frame == LAST_FRAME { |
| 10064 | Ok((ref_y, ref_u, ref_v)) |
| 10065 | } else { |
| 10066 | match other_refs[ref_frame as usize] { |
| 10067 | Some((ry, ru, rv)) => Ok((ry, ru, rv)), |
| 10068 | None => Err(unsupported( |
| 10069 | "a reference frame selected with no picture at this frame's own \ |
| 10070 | ref_frame_idx slot for it", |
| 10071 | )), |
| 10072 | } |
| 10073 | } |
| 10074 | } |
| 10075 | |
| 10076 | /// libaom `foreach_overlappable_nb_above` (`obmc.h`): walks the mi row |
| 10077 | /// directly above this block's own column span, one bordering block at a |
| 10078 | /// time (stepping by that neighbour's own `MiInfo::size`, which is always |
| 10079 | /// `<= 16` since no block this decoder ever codes exceeds 64x64), collecting |
| 10080 | /// every *inter* neighbour found (an intra or unset cell is skipped, one mi |
| 10081 | /// unit at a time, and does not itself count as a step). `max_neighbors` |
| 10082 | /// caller-bounds the scan: `1` for the eligibility check (any hit at all), |
| 10083 | /// `av1_build_obmc_inter_prediction`'s own `max_neighbor_obmc` table for the |
| 10084 | /// real blend pass. Round 1 corner-cut: libaom's own "4-wide block, treat as |
| 10085 | /// half of a chroma pair" merge (`obmc.h`'s `mi_step == 1` special case) is |
| 10086 | /// not ported -- no block this decoder codes is narrower than 8px, so it |
| 10087 | /// never fires here regardless of what a neighbour's own decoder was. |
| 10088 | fn overlappable_above( |
| 10089 | grid: &MiGrid, |
| 10090 | mi_row: usize, |
| 10091 | mi_col: usize, |
| 10092 | bw4: usize, |
| 10093 | mi_cols: usize, |
| 10094 | max_neighbors: usize, |
| 10095 | ) -> Vec<(usize, usize, MiInfo)> { |
| 10096 | let mut out = Vec::new(); |
| 10097 | if mi_row == 0 { |
| 10098 | return out; |
| 10099 | } |
| 10100 | let end_col = (mi_col + bw4).min(mi_cols); |
| 10101 | let mut col = mi_col; |
| 10102 | while col < end_col && out.len() < max_neighbors { |
| 10103 | let cell = grid.get(mi_row - 1, col); |
| 10104 | let step = cell.map_or(1, |c| c.size).max(1); |
| 10105 | if let Some(info) = cell { |
| 10106 | if info.is_inter { |
| 10107 | out.push((col - mi_col, step.min(end_col - col), *info)); |
| 10108 | } |
| 10109 | } |
| 10110 | col += step; |
| 10111 | } |
| 10112 | out |
| 10113 | } |
| 10114 | |
| 10115 | /// `foreach_overlappable_nb_left`'s left-column mirror of |
| 10116 | /// [`overlappable_above`] -- see its doc for the shared corner-cut. |
| 10117 | fn overlappable_left( |
| 10118 | grid: &MiGrid, |
| 10119 | mi_row: usize, |
| 10120 | mi_col: usize, |
| 10121 | bh4: usize, |
| 10122 | mi_rows: usize, |
| 10123 | max_neighbors: usize, |
| 10124 | ) -> Vec<(usize, usize, MiInfo)> { |
| 10125 | let mut out = Vec::new(); |
| 10126 | if mi_col == 0 { |
| 10127 | return out; |
| 10128 | } |
| 10129 | let end_row = (mi_row + bh4).min(mi_rows); |
| 10130 | let mut row = mi_row; |
| 10131 | while row < end_row && out.len() < max_neighbors { |
| 10132 | let cell = grid.get(row, mi_col - 1); |
| 10133 | // Vertical walk steps by the neighbour's HEIGHT -- a 32x16 strip's |
| 10134 | // width (8) would swallow the strip below it. |
| 10135 | let step = cell.map_or(1, |c| c.size_h).max(1); |
| 10136 | if let Some(info) = cell { |
| 10137 | if info.is_inter { |
| 10138 | out.push((row - mi_row, step.min(end_row - row), *info)); |
| 10139 | } |
| 10140 | } |
| 10141 | row += step; |
| 10142 | } |
| 10143 | out |
| 10144 | } |
| 10145 | |
| 10146 | /// `has_top_right` (`av1/common/mvref_common.c`), reduced to the |
| 10147 | /// square-block, fixed-64px-superblock case this decoder ever reaches: no |
| 10148 | /// rectangular partition (`VERT`/`HORZ`/`_4`/`VERT_A`) ever produces an |
| 10149 | /// inter leaf here (`xd->width == xd->height` always), so libaom's own |
| 10150 | /// rect-partition branches (`xd->width < xd->height`, `xd->width > |
| 10151 | /// xd->height`, `PARTITION_VERT_A`) never fire and are dropped. `bs` is the |
| 10152 | /// block's own side in 4x4 (`mi`) units. |
| 10153 | fn has_top_right(mi_row: usize, mi_col: usize, bs: usize) -> bool { |
| 10154 | let sb_mi_size = SB_MI as usize; |
| 10155 | let mask_row = mi_row & (sb_mi_size - 1); |
| 10156 | let mask_col = mi_col & (sb_mi_size - 1); |
| 10157 | if bs > sb_mi_size { |
| 10158 | return false; |
| 10159 | } |
| 10160 | let mut has_tr = !((mask_row & bs != 0) && (mask_col & bs != 0)); |
| 10161 | let mut b = bs; |
| 10162 | while b < sb_mi_size { |
| 10163 | if mask_col & b != 0 { |
| 10164 | if (mask_col & (2 * b) != 0) && (mask_row & (2 * b) != 0) { |
| 10165 | has_tr = false; |
| 10166 | break; |
| 10167 | } |
| 10168 | } else { |
| 10169 | break; |
| 10170 | } |
| 10171 | b <<= 1; |
| 10172 | } |
| 10173 | has_tr |
| 10174 | } |
| 10175 | |
| 10176 | /// `av1_findSamples` (`mvref_common.c`), full port: the up to |
| 10177 | /// `LEAST_SQUARES_SAMPLES_MAX` (8) above/left/top-left/top-right neighbour |
| 10178 | /// samples (`record_samples`) that share this block's own single reference |
| 10179 | /// frame and are themselves single-ref -- `mbmi->num_proj_ref` is |
| 10180 | /// `.len()` of the returned vec. `bw4` is the block's own side in 4x4 units |
| 10181 | /// (square, so also `bh4`; a real block's `xd->width`/`xd->height` in |
| 10182 | /// libaom are also mi units of that same square side here). Each sample's |
| 10183 | /// own `bw`/`bh` (fed to [`crate::warp::record_sample`]'s offset formula) |
| 10184 | /// come from the *neighbour* cell's own coded size, not this block's. |
| 10185 | #[allow(clippy::too_many_arguments)] |
| 10186 | fn find_samples( |
| 10187 | grid: &MiGrid, |
| 10188 | mi_row: usize, |
| 10189 | mi_col: usize, |
| 10190 | bw4: usize, |
| 10191 | bh4: usize, |
| 10192 | mi_cols: usize, |
| 10193 | mi_rows: usize, |
| 10194 | ref_frame: i8, |
| 10195 | ) -> Vec<crate::warp::Sample> { |
| 10196 | const MAX: usize = crate::warp::LEAST_SQUARES_SAMPLES_MAX; |
| 10197 | let up_available = mi_row > 0; |
| 10198 | let left_available = mi_col > 0; |
| 10199 | let mut samples = Vec::with_capacity(MAX); |
| 10200 | let single_ref_match = |info: &MiInfo| info.ref_frame == ref_frame && info.ref_frame1.is_none(); |
| 10201 | let mut do_tl = true; |
| 10202 | let mut do_tr = true; |
| 10203 | let rec = |info: &MiInfo, row_offset: i32, sign_r: i32, col_offset: i32, sign_c: i32| { |
| 10204 | let nb_bw = (info.size * 4) as i32; |
| 10205 | // lane-rect r2: the neighbour's own true height -- a rect strip |
| 10206 | // neighbour donates its sample at ITS center (aom pts y=-72 for a |
| 10207 | // 32x16 above strip; the square assumption put it at -136). |
| 10208 | let nb_bh = (info.size_h * 4) as i32; |
| 10209 | crate::warp::record_sample(nb_bw, nb_bh, info.mv, row_offset, sign_r, col_offset, sign_c) |
| 10210 | }; |
| 10211 | |
| 10212 | 'outer: { |
| 10213 | if up_available { |
| 10214 | let above = grid.get(mi_row - 1, mi_col); |
| 10215 | let superblock_width = above.map_or(1, |c| c.size).max(1); |
| 10216 | if bw4 <= superblock_width { |
| 10217 | let col_offset = -((mi_col % superblock_width) as isize); |
| 10218 | if col_offset < 0 { |
| 10219 | do_tl = false; |
| 10220 | } |
| 10221 | if col_offset + superblock_width as isize > bw4 as isize { |
| 10222 | do_tr = false; |
| 10223 | } |
| 10224 | if let Some(info) = above { |
| 10225 | if single_ref_match(info) { |
| 10226 | samples.push(rec(info, 0, -1, col_offset as i32, 1)); |
| 10227 | if samples.len() >= MAX { |
| 10228 | break 'outer; |
| 10229 | } |
| 10230 | } |
| 10231 | } |
| 10232 | } else { |
| 10233 | let mut i = 0usize; |
| 10234 | let limit = bw4.min(mi_cols.saturating_sub(mi_col)); |
| 10235 | while i < limit { |
| 10236 | let cell = grid.get(mi_row - 1, mi_col + i); |
| 10237 | let sw = cell.map_or(1, |c| c.size).max(1); |
| 10238 | if let Some(info) = cell { |
| 10239 | if single_ref_match(info) { |
| 10240 | samples.push(rec(info, 0, -1, i as i32, 1)); |
| 10241 | if samples.len() >= MAX { |
| 10242 | break 'outer; |
| 10243 | } |
| 10244 | } |
| 10245 | } |
| 10246 | i += sw; |
| 10247 | } |
| 10248 | } |
| 10249 | } |
| 10250 | |
| 10251 | if left_available { |
| 10252 | let left = grid.get(mi_row, mi_col - 1); |
| 10253 | let superblock_height = left.map_or(1, |c| c.size_h).max(1); |
| 10254 | if bh4 <= superblock_height { |
| 10255 | let row_offset = -((mi_row % superblock_height) as isize); |
| 10256 | if row_offset < 0 { |
| 10257 | do_tl = false; |
| 10258 | } |
| 10259 | if let Some(info) = left { |
| 10260 | if single_ref_match(info) { |
| 10261 | samples.push(rec(info, row_offset as i32, 1, 0, -1)); |
| 10262 | if samples.len() >= MAX { |
| 10263 | break 'outer; |
| 10264 | } |
| 10265 | } |
| 10266 | } |
| 10267 | } else { |
| 10268 | let mut i = 0usize; |
| 10269 | let limit = bh4.min(mi_rows.saturating_sub(mi_row)); |
| 10270 | while i < limit { |
| 10271 | let cell = grid.get(mi_row + i, mi_col - 1); |
| 10272 | let sh = cell.map_or(1, |c| c.size_h).max(1); |
| 10273 | if let Some(info) = cell { |
| 10274 | if single_ref_match(info) { |
| 10275 | samples.push(rec(info, i as i32, 1, 0, -1)); |
| 10276 | if samples.len() >= MAX { |
| 10277 | break 'outer; |
| 10278 | } |
| 10279 | } |
| 10280 | } |
| 10281 | i += sh; |
| 10282 | } |
| 10283 | } |
| 10284 | } |
| 10285 | |
| 10286 | if do_tl && left_available && up_available { |
| 10287 | if let Some(info) = grid.get(mi_row - 1, mi_col - 1) { |
| 10288 | if single_ref_match(info) { |
| 10289 | samples.push(rec(info, 0, -1, 0, -1)); |
| 10290 | if samples.len() >= MAX { |
| 10291 | break 'outer; |
| 10292 | } |
| 10293 | } |
| 10294 | } |
| 10295 | } |
| 10296 | |
| 10297 | if do_tr && has_top_right(mi_row, mi_col, bw4) { |
| 10298 | let tr_col = mi_col + bw4; |
| 10299 | if mi_row > 0 && tr_col < mi_cols { |
| 10300 | if let Some(info) = grid.get(mi_row - 1, tr_col) { |
| 10301 | if single_ref_match(info) { |
| 10302 | samples.push(rec(info, 0, -1, bw4 as i32, 1)); |
| 10303 | } |
| 10304 | } |
| 10305 | } |
| 10306 | } |
| 10307 | } |
| 10308 | samples |
| 10309 | } |
| 10310 | |
| 10311 | /// `mbmi->num_proj_ref`: [`find_samples`]'s own count, all that |
| 10312 | /// `motion_mode_allowed` needs to pick the 3-symbol `motion_mode_cdf` over |
| 10313 | /// the 2-symbol `obmc_cdf` (spec 5.11.24). |
| 10314 | #[allow(clippy::too_many_arguments)] |
| 10315 | fn num_proj_ref( |
| 10316 | grid: &MiGrid, |
| 10317 | mi_row: usize, |
| 10318 | mi_col: usize, |
| 10319 | bw4: usize, |
| 10320 | bh4: usize, |
| 10321 | mi_cols: usize, |
| 10322 | mi_rows: usize, |
| 10323 | ref_frame: i8, |
| 10324 | ) -> u8 { |
| 10325 | find_samples(grid, mi_row, mi_col, bw4, bh4, mi_cols, mi_rows, ref_frame).len() as u8 |
| 10326 | } |
| 10327 | |
| 10328 | /// `av1_get_obmc_mask` (`reconinter.c`): the per-position blend weight |
| 10329 | /// (`AOM_BLEND_A64` numerator, out of 64) for the neighbour's own |
| 10330 | /// contribution's *complement* -- i.e. `dst = (mask*orig + (64-mask)*nbr + |
| 10331 | /// 32) >> 6`, low near the shared border (heavier neighbour weight) rising |
| 10332 | /// to 64 (no neighbour contribution) away from it. Only the lengths this |
| 10333 | /// decoder's own overlap sizes ever produce (luma 8/16/32, chroma 4/8/16) |
| 10334 | /// are transcribed. |
| 10335 | fn obmc_mask(len: usize) -> &'static [u8] { |
| 10336 | // lane-inter8 r1: `obmc_mask_2` (`reconinter.c` 753) -- reached by the |
| 10337 | // LEFT pass of an 8x8 block's chroma plane (overlap 4 luma columns, 2 |
| 10338 | // chroma), which only became live once 8x8 inter leaves stopped being |
| 10339 | // refused. |
| 10340 | const M2: [u8; 2] = [45, 64]; |
| 10341 | const M4: [u8; 4] = [39, 50, 59, 64]; |
| 10342 | const M8: [u8; 8] = [36, 42, 48, 53, 57, 61, 64, 64]; |
| 10343 | const M16: [u8; 16] = [ |
| 10344 | 34, 37, 40, 43, 46, 49, 52, 54, 56, 58, 60, 61, 64, 64, 64, 64, |
| 10345 | ]; |
| 10346 | const M32: [u8; 32] = [ |
| 10347 | 33, 35, 36, 38, 40, 41, 43, 44, 45, 47, 48, 50, 51, 52, 53, 55, 56, 57, 58, 59, 60, 60, |
| 10348 | 61, 62, 64, 64, 64, 64, 64, 64, 64, 64, |
| 10349 | ]; |
| 10350 | match len { |
| 10351 | 2 => &M2, |
| 10352 | 4 => &M4, |
| 10353 | 8 => &M8, |
| 10354 | 16 => &M16, |
| 10355 | 32 => &M32, |
| 10356 | _ => unreachable!("obmc overlap length outside this decoder's own block-size range"), |
| 10357 | } |
| 10358 | } |
| 10359 | |
| 10360 | /// A bordering block's own resolved interp filter kernel, `(h, v)` -- |
| 10361 | /// mirrors [`resolve_interp_filter`]'s own return order from its stored |
| 10362 | /// `[u8; 2]` (`Neighbours::above_filter`/`left_filter`, `[sym0, sym1]` = |
| 10363 | /// `[v_sym, h_sym]`). Never calls `from_switchable_symbol` on the `[3, 3]` |
| 10364 | /// sentinel: that value is only ever stored for an intra neighbour (already |
| 10365 | /// excluded by [`overlappable_above`]/[`overlappable_left`]'s `is_inter` |
| 10366 | /// gate) or when `interp_fixed` is `Some` for the whole frame, handled here |
| 10367 | /// first. |
| 10368 | fn neighbour_filter( |
| 10369 | interp_fixed: Option<mc::InterpFilterKind>, |
| 10370 | sym: [u8; 2], |
| 10371 | ) -> (mc::InterpFilterKind, mc::InterpFilterKind) { |
| 10372 | if let Some(kind) = interp_fixed { |
| 10373 | return (kind, kind); |
| 10374 | } |
| 10375 | ( |
| 10376 | mc::InterpFilterKind::from_switchable_symbol(sym[1] as usize), |
| 10377 | mc::InterpFilterKind::from_switchable_symbol(sym[0] as usize), |
| 10378 | ) |
| 10379 | } |
| 10380 | |
| 10381 | /// One neighbour's own re-prediction over a `w`x`h` region, MC'd fresh |
| 10382 | /// against its own `mv`/reference/filter (libaom `av1_build_inter_predictor` |
| 10383 | /// under `av1_setup_build_prediction_by_{above,left}_pred`). |
| 10384 | #[allow(clippy::too_many_arguments)] |
| 10385 | fn obmc_neighbour_pred( |
| 10386 | refplane: &PlaneBuf, |
| 10387 | x: usize, |
| 10388 | y: usize, |
| 10389 | mv: (i32, i32), |
| 10390 | w: usize, |
| 10391 | h: usize, |
| 10392 | luma: bool, |
| 10393 | h_kind: mc::InterpFilterKind, |
| 10394 | v_kind: mc::InterpFilterKind, |
| 10395 | // lane-scaledref r1: spec 7.11.3.3's x_scale_fp for THIS neighbour's own |
| 10396 | // reference (derived from luma widths, applied unchanged to chroma whose |
| 10397 | // x_q4 is already in its own plane's pixel units). `REF_NO_SCALE` takes |
| 10398 | // the ordinary stride-1 path bit-exact. |
| 10399 | x_scale_fp: i64, |
| 10400 | ) -> Vec<u16> { |
| 10401 | let mut out = vec![0u16; w * h]; |
| 10402 | if x_scale_fp == mc::REF_NO_SCALE { |
| 10403 | mc::predict_with_filters( |
| 10404 | &refplane.data, |
| 10405 | refplane.width, |
| 10406 | refplane.true_width, |
| 10407 | refplane.true_height, |
| 10408 | mv_to_q4(x, mv.1, luma), |
| 10409 | mv_to_q4(y, mv.0, luma), |
| 10410 | w, |
| 10411 | h, |
| 10412 | h_kind, |
| 10413 | v_kind, |
| 10414 | &mut out, |
| 10415 | ); |
| 10416 | } else { |
| 10417 | mc::predict_scaled( |
| 10418 | &refplane.data, |
| 10419 | refplane.width, |
| 10420 | refplane.true_width, |
| 10421 | refplane.true_height, |
| 10422 | mv_to_q4(x, mv.1, luma), |
| 10423 | mv_to_q4(y, mv.0, luma), |
| 10424 | x_scale_fp, |
| 10425 | w, |
| 10426 | h, |
| 10427 | h_kind, |
| 10428 | v_kind, |
| 10429 | &mut out, |
| 10430 | ); |
| 10431 | } |
| 10432 | out |
| 10433 | } |
| 10434 | |
| 10435 | /// Blends `tmp` into `dst` (stride `stride`) at `(ox, oy)`, mask varying by |
| 10436 | /// row (the above-neighbour pass, `aom_blend_a64_vmask`). |
| 10437 | /// `ii_weights1d` (reconinter.c:524): the interintra smooth-mask falloff. |
| 10438 | const II_WEIGHTS_1D: [u8; 128] = [ |
| 10439 | 60, 58, 56, 54, 52, 50, 48, 47, 45, 44, 42, 41, 39, 38, 37, 35, 34, 33, 32, |
| 10440 | 31, 30, 29, 28, 27, 26, 25, 24, 23, 22, 22, 21, 20, 19, 19, 18, 18, 17, 16, |
| 10441 | 16, 15, 15, 14, 14, 13, 13, 12, 12, 12, 11, 11, 10, 10, 10, 9, 9, 9, 8, |
| 10442 | 8, 8, 8, 7, 7, 7, 7, 6, 6, 6, 6, 6, 5, 5, 5, 5, 5, 4, 4, |
| 10443 | 4, 4, 4, 4, 4, 4, 3, 3, 3, 3, 3, 3, 3, 3, 3, 2, 2, 2, 2, |
| 10444 | 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 1, 1, 1, 1, 1, 1, 1, 1, |
| 10445 | 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, |
| 10446 | ]; |
| 10447 | |
| 10448 | /// lane-interintra r1: non-wedge interintra prediction for one plane |
| 10449 | /// (reconinter.c `av1_build_interintra_predictor` + `combine_interintra` + |
| 10450 | /// `build_smooth_interintra_mask`). Builds the intra predictor for the |
| 10451 | /// block's II mode (DC/V/H/SMOOTH, angle 0, no filter-intra) from `plane`'s |
| 10452 | /// own decoded edges at `(x, y)`, then blends it over the inter prediction |
| 10453 | /// in `pred`: `comp = (mask*intra + (64-mask)*inter + 32) >> 6` |
| 10454 | /// (`aom_blend_a64_mask`, intra as src0). `ii_size_scales[plane_bsize]` |
| 10455 | /// reduces to `128 / side` for the square plane sizes this decoder reaches. |
| 10456 | /// lane-wii r2: `wedge == Some(..)` replaces the mode mask with the wedge |
| 10457 | /// codebook mask (fixed sign 0; luma stride, 2x2 box-averaged for chroma). |
| 10458 | fn interintra_blend( |
| 10459 | plane: &PlaneBuf, |
| 10460 | x: usize, |
| 10461 | y: usize, |
| 10462 | side: usize, |
| 10463 | ii_mode: u8, |
| 10464 | wedge: Option<(&'static [u8], usize)>, |
| 10465 | pred: &mut [u16], |
| 10466 | ) { |
| 10467 | // `interintra_to_intra_mode`: II_DC/II_V/II_H/II_SMOOTH. |
| 10468 | let intra_mode = match ii_mode { |
| 10469 | 0 => crate::intra::DC_PRED, |
| 10470 | 1 => crate::intra::V_PRED, |
| 10471 | 2 => crate::intra::H_PRED, |
| 10472 | _ => crate::intra::SMOOTH_PRED, |
| 10473 | }; |
| 10474 | let (above, left, corner) = plane.edges( |
| 10475 | x, |
| 10476 | y, |
| 10477 | side, |
| 10478 | crate::encode::Reach { |
| 10479 | above_right: false, |
| 10480 | below_left: false, |
| 10481 | }, |
| 10482 | ); |
| 10483 | let mut intra = vec![0u16; side * side]; |
| 10484 | // Edge filtering never applies here: V/H at delta 0 are plain edge |
| 10485 | // copies (angle 90/180 skip the directional walk), DC/SMOOTH carry no |
| 10486 | // angle at all. |
| 10487 | predict( |
| 10488 | intra_mode, |
| 10489 | 0, |
| 10490 | above.as_deref(), |
| 10491 | left.as_deref(), |
| 10492 | corner, |
| 10493 | side, |
| 10494 | side, |
| 10495 | false, |
| 10496 | false, |
| 10497 | &mut intra, |
| 10498 | ); |
| 10499 | let scale = 128 / side; |
| 10500 | for i in 0..side { |
| 10501 | for j in 0..side { |
| 10502 | let m = u32::from(match wedge { |
| 10503 | // lane-wii r2: wedge-interintra mask (aom_blend_a64_mask |
| 10504 | // over the wedge codebook, intra as src0, fixed sign 0). |
| 10505 | // Luma plane: mask rows are luma-resolution, ms == side. |
| 10506 | // 4:2:0 chroma: 2x2 box average (reconinter.c sub8x8 -- |
| 10507 | // subw == subh == 1). |
| 10508 | Some((mask, ms)) if ms == side => mask[i * side + j], |
| 10509 | Some((mask, ms)) => { |
| 10510 | let t = 2 * i * ms + 2 * j; |
| 10511 | ((u32::from(mask[t]) |
| 10512 | + u32::from(mask[t + 1]) |
| 10513 | + u32::from(mask[t + ms]) |
| 10514 | + u32::from(mask[t + ms + 1]) |
| 10515 | + 2) |
| 10516 | >> 2) as u8 |
| 10517 | } |
| 10518 | None => match ii_mode { |
| 10519 | 0 => 32, |
| 10520 | 1 => II_WEIGHTS_1D[i * scale], |
| 10521 | 2 => II_WEIGHTS_1D[j * scale], |
| 10522 | _ => II_WEIGHTS_1D[i.min(j) * scale], |
| 10523 | }, |
| 10524 | }); |
| 10525 | let idx = i * side + j; |
| 10526 | pred[idx] = |
| 10527 | ((m * u32::from(intra[idx]) + (64 - m) * u32::from(pred[idx]) + 32) >> 6) as u16; |
| 10528 | } |
| 10529 | } |
| 10530 | } |
| 10531 | |
| 10532 | fn obmc_blend_v(dst: &mut [u16], stride: usize, ox: usize, oy: usize, w: usize, h: usize, tmp: &[u16]) { |
| 10533 | let mask = obmc_mask(h); |
| 10534 | for row in 0..h { |
| 10535 | let m = u32::from(mask[row]); |
| 10536 | for col in 0..w { |
| 10537 | let d = &mut dst[(oy + row) * stride + ox + col]; |
| 10538 | let t = u32::from(tmp[row * w + col]); |
| 10539 | *d = ((m * u32::from(*d) + (64 - m) * t + 32) >> 6) as u16; |
| 10540 | } |
| 10541 | } |
| 10542 | } |
| 10543 | |
| 10544 | /// Blends `tmp` into `dst` at `(ox, oy)`, mask varying by column (the |
| 10545 | /// left-neighbour pass, `aom_blend_a64_hmask`). |
| 10546 | fn obmc_blend_h(dst: &mut [u16], stride: usize, ox: usize, oy: usize, w: usize, h: usize, tmp: &[u16]) { |
| 10547 | let mask = obmc_mask(w); |
| 10548 | for row in 0..h { |
| 10549 | for col in 0..w { |
| 10550 | let m = u32::from(mask[col]); |
| 10551 | let d = &mut dst[(oy + row) * stride + ox + col]; |
| 10552 | let t = u32::from(tmp[row * w + col]); |
| 10553 | *d = ((m * u32::from(*d) + (64 - m) * t + 32) >> 6) as u16; |
| 10554 | } |
| 10555 | } |
| 10556 | } |
| 10557 | |
| 10558 | /// `av1_build_obmc_inter_prediction` (libaom `reconinter.c`): re-predicts |
| 10559 | /// the overlap strip against each bordering above/left inter neighbour's own |
| 10560 | /// mv/ref/filter and blends it into this block's just-built single-reference |
| 10561 | /// prediction, luma then both 4:2:0 chroma planes -- above pass first, then |
| 10562 | /// left, each reading only the *original* prediction this function was |
| 10563 | /// handed (never the other pass's own output), matching libaom's own |
| 10564 | /// independence between the two. `max_neighbor_obmc`'s own cap table |
| 10565 | /// (`{0,1,2,3,4,4}` indexed by `mi_size_wide_log2`/`_high_log2`, this |
| 10566 | /// decoder's own square blocks always index the same value both ways) bounds |
| 10567 | /// how many bordering blocks actually get blended, separate from the |
| 10568 | /// eligibility scan's own unbounded ("any at all") walk. |
| 10569 | #[allow(clippy::too_many_arguments)] |
| 10570 | fn obmc_blend( |
| 10571 | grid: &MiGrid, |
| 10572 | neighbours: &Neighbours, |
| 10573 | mi_row: usize, |
| 10574 | mi_col: usize, |
| 10575 | bw4: usize, |
| 10576 | bh4: usize, |
| 10577 | mi_rows: usize, |
| 10578 | mi_cols: usize, |
| 10579 | side: usize, |
| 10580 | write_w: usize, |
| 10581 | write_h: usize, |
| 10582 | chroma_side: usize, |
| 10583 | px: usize, |
| 10584 | py: usize, |
| 10585 | cpx: usize, |
| 10586 | cpy: usize, |
| 10587 | ref_y: &PlaneBuf, |
| 10588 | ref_u: &PlaneBuf, |
| 10589 | ref_v: &PlaneBuf, |
| 10590 | other_refs: &RefSlots, |
| 10591 | interp_fixed: Option<mc::InterpFilterKind>, |
| 10592 | // lane-scaledref r1: this frame's own coded luma width -- each OBMC |
| 10593 | // neighbour is re-predicted against ITS OWN reference, which may be |
| 10594 | // scaled differently from this block's (spec 7.11.3.3 / libaom |
| 10595 | // `av1_setup_build_prediction_by_above_pred` passing that ref's own |
| 10596 | // `scale_factors`). |
| 10597 | frame_width: usize, |
| 10598 | pred_y: &mut [u16], |
| 10599 | pred_u: &mut [u16], |
| 10600 | pred_v: &mut [u16], |
| 10601 | ) -> Result<()> { |
| 10602 | // lane-rect r2 (libaom av1_build_obmc_inter_prediction): each pass has |
| 10603 | // its OWN neighbour cap (width-log2 for above, height-log2 for left) and |
| 10604 | // overlap (bh/2 rows above, bw/2 cols left) -- one square `side` was |
| 10605 | // right only for square blocks. |
| 10606 | let max_nb = |n4: usize| match n4 { |
| 10607 | 1..=4 => 2, |
| 10608 | 5..=8 => 3, |
| 10609 | _ => 4, |
| 10610 | }; |
| 10611 | let overlap_above = write_h / 2; |
| 10612 | let overlap_left = write_w / 2; |
| 10613 | // lane-inter8 r1 / lane-scaledref r1: `av1_skip_u4x4_pred_in_obmc` |
| 10614 | // (`reconinter.c` 820, `DISABLE_CHROMA_U8X8_OBMC == 0` -- "one-sided |
| 10615 | // obmc") returns `dir == 0` when the chroma plane's own block is |
| 10616 | // BLOCK_4X4/8X4/4X8 (an 8x8, 16x8 or 8x16 luma block at 4:2:0): the |
| 10617 | // ABOVE pass skips chroma entirely, the LEFT pass still blends it. |
| 10618 | let skip_chroma_above = matches!((write_w, write_h), (8, 8) | (16, 8) | (8, 16)); |
| 10619 | |
| 10620 | for (off4, span4, nb) in overlappable_above(grid, mi_row, mi_col, bw4, mi_cols, max_nb(bw4)) |
| 10621 | { |
| 10622 | // `Neighbours::above_filter` is indexed in `SUB`(16px)-wide columns |
| 10623 | // (this decoder's own outer block-loop granularity), one step |
| 10624 | // coarser than the mi(4px) units `overlappable_above` walks in -- |
| 10625 | // divide back down to that column. |
| 10626 | let (h_kind, v_kind) = neighbour_filter( |
| 10627 | interp_fixed, |
| 10628 | neighbours.above_filter[mi_col + off4], |
| 10629 | ); |
| 10630 | let (ny, nu, nv) = ref_planes(nb.ref_frame, ref_y, ref_u, ref_v, other_refs)?; |
| 10631 | let nb_scale = mc::scale_factor(ny.width, frame_width); |
| 10632 | let (bw, bh, ox) = (span4 * 4, overlap_above, off4 * 4); |
| 10633 | let tmp_y = obmc_neighbour_pred(ny, px + ox, py, nb.mv, bw, bh, true, h_kind, v_kind, nb_scale); |
| 10634 | obmc_blend_v(pred_y, side, ox, 0, bw, bh, &tmp_y); |
| 10635 | if !skip_chroma_above { |
| 10636 | let (cbw, cbh, cox) = (bw / 2, overlap_above / 2, ox / 2); |
| 10637 | let tmp_u = obmc_neighbour_pred(nu, cpx + cox, cpy, nb.mv, cbw, cbh, false, h_kind, v_kind, nb_scale); |
| 10638 | obmc_blend_v(pred_u, chroma_side, cox, 0, cbw, cbh, &tmp_u); |
| 10639 | let tmp_v = obmc_neighbour_pred(nv, cpx + cox, cpy, nb.mv, cbw, cbh, false, h_kind, v_kind, nb_scale); |
| 10640 | obmc_blend_v(pred_v, chroma_side, cox, 0, cbw, cbh, &tmp_v); |
| 10641 | } |
| 10642 | } |
| 10643 | |
| 10644 | for (off4, span4, nb) in overlappable_left(grid, mi_row, mi_col, bh4, mi_rows, max_nb(bh4)) { |
| 10645 | let (h_kind, v_kind) = neighbour_filter( |
| 10646 | interp_fixed, |
| 10647 | neighbours.left_filter[mi_row + off4], |
| 10648 | ); |
| 10649 | let (ny, nu, nv) = ref_planes(nb.ref_frame, ref_y, ref_u, ref_v, other_refs)?; |
| 10650 | let nb_scale = mc::scale_factor(ny.width, frame_width); |
| 10651 | let (bw, bh, oy) = (overlap_left, span4 * 4, off4 * 4); |
| 10652 | let tmp_y = obmc_neighbour_pred(ny, px, py + oy, nb.mv, bw, bh, true, h_kind, v_kind, nb_scale); |
| 10653 | obmc_blend_h(pred_y, side, 0, oy, bw, bh, &tmp_y); |
| 10654 | let (cbw, cbh, coy) = (overlap_left / 2, bh / 2, oy / 2); |
| 10655 | let tmp_u = obmc_neighbour_pred(nu, cpx, cpy + coy, nb.mv, cbw, cbh, false, h_kind, v_kind, nb_scale); |
| 10656 | obmc_blend_h(pred_u, chroma_side, 0, coy, cbw, cbh, &tmp_u); |
| 10657 | let tmp_v = obmc_neighbour_pred(nv, cpx, cpy + coy, nb.mv, cbw, cbh, false, h_kind, v_kind, nb_scale); |
| 10658 | obmc_blend_h(pred_v, chroma_side, 0, coy, cbw, cbh, &tmp_v); |
| 10659 | } |
| 10660 | Ok(()) |
| 10661 | } |
| 10662 | |
| 10663 | /// `is_any_masked_compound_used` (libaom `reconinter.h`), specialised to |
| 10664 | /// the square block sizes this decoder ever decodes (32/16/8, all |
| 10665 | /// `min(w,h) >= 8`, `is_comp_ref_allowed` true): `COMPOUND_DIFFWTD` is then |
| 10666 | /// always `is_interinter_compound_used`, so the function is always `true` |
| 10667 | /// for `side` -- kept as a named call (rather than inlined `true`) so a |
| 10668 | /// future sub-8 block size does not silently misdecode a real stream. |
| 10669 | fn is_any_masked_compound_used_here(side: usize) -> bool { |
| 10670 | side.min(side) >= 8 |
| 10671 | } |
| 10672 | |
| 10673 | /// Whether a compound reference pair is unidirectional (both references on |
| 10674 | /// the same temporal side of the current frame) -- `has_uni_comp_refs` |
| 10675 | /// (libaom `av1_reference_frame_utils.h`/`pred_common.c`'s own definition: |
| 10676 | /// a pair is `UNIDIR_COMP_REFERENCE` exactly when neither reference is |
| 10677 | /// backward-while-the-other-is-forward, i.e. `is_backward(ref0) == |
| 10678 | /// is_backward(ref1)`). Used to build a real |
| 10679 | /// [`crate::mvstack::NeighbourRef::uni`] once `ref1` is known, matching |
| 10680 | /// [`crate::mvstack::comp_reference_type_ctx`]'s own `is_backward` calls. |
| 10681 | fn is_uni_comp_ref(ref0: i8, ref1: i8) -> bool { |
| 10682 | let backward = |r: i8| (BWDREF_FRAME..=ALTREF_FRAME).contains(&r); |
| 10683 | backward(ref0) == backward(ref1) |
| 10684 | } |
| 10685 | |
| 10686 | /// `get_comp_group_idx_context` (libaom `pred_common.h`): sums the |
| 10687 | /// above/left neighbour contributions [`Neighbours::record_inter`]/ |
| 10688 | /// [`Neighbours::record_compound_ctx`] already precomputed, clamped to 5. |
| 10689 | /// lane-inter8 r2: `at_mi` is the block's own top-left in 4x4 mi units -- |
| 10690 | /// an 8x8 leaf used to pass its enclosing 16x16's corner here, so all four |
| 10691 | /// leaves read the same neighbour cell (class context-read-from-one-cell). |
| 10692 | fn get_comp_group_idx_context( |
| 10693 | neighbours: &Neighbours, |
| 10694 | at_mi: (usize, usize), |
| 10695 | side: usize, |
| 10696 | ) -> usize { |
| 10697 | let _ = side; |
| 10698 | let (rmi, cmi) = at_mi; |
| 10699 | (neighbours.above_comp_group_idx[cmi] as usize + neighbours.left_comp_group_idx[rmi] as usize) |
| 10700 | .min(5) |
| 10701 | } |
| 10702 | |
| 10703 | /// `get_comp_index_context` (libaom `pred_common.h`): the above/left |
| 10704 | /// neighbour bits plus `3 * (fwd == bck)`, where `fwd`/`bck` are this |
| 10705 | /// block's own two references' distances from the current frame (spec |
| 10706 | /// 7.11.3.15's `get_relative_dist`, same convention |
| 10707 | /// [`crate::compound::dist_wtd_comp_weight_assign`] uses). |
| 10708 | #[allow(clippy::too_many_arguments)] |
| 10709 | fn get_comp_index_context( |
| 10710 | neighbours: &Neighbours, |
| 10711 | at_mi: (usize, usize), |
| 10712 | side: usize, |
| 10713 | order_hint_bits: u32, |
| 10714 | cur_order_hint: u32, |
| 10715 | ref0_order_hint: u32, |
| 10716 | ref1_order_hint: u32, |
| 10717 | ) -> usize { |
| 10718 | let _ = side; |
| 10719 | let (rmi, cmi) = at_mi; |
| 10720 | let fwd = |
| 10721 | crate::motion_field::get_relative_dist(order_hint_bits, ref1_order_hint, cur_order_hint) |
| 10722 | .abs(); |
| 10723 | let bck = |
| 10724 | crate::motion_field::get_relative_dist(order_hint_bits, cur_order_hint, ref0_order_hint) |
| 10725 | .abs(); |
| 10726 | let offset = usize::from(fwd == bck); |
| 10727 | neighbours.above_compound_idx[cmi] as usize |
| 10728 | + neighbours.left_compound_idx[rmi] as usize |
| 10729 | + 3 * offset |
| 10730 | } |
| 10731 | |
| 10732 | fn read_comp_mode( |
| 10733 | dec: &mut SymbolDecoder, |
| 10734 | cdfs: &mut Cdfs, |
| 10735 | above: Option<NeighbourRef>, |
| 10736 | left: Option<NeighbourRef>, |
| 10737 | ) -> bool { |
| 10738 | let ctx = reference_mode_ctx(above, left); |
| 10739 | let compound = dec.symbol(&mut cdfs.comp_mode[ctx]) == 1; |
| 10740 | if compound { |
| 10741 | COMP_MODE_HITS.with(|c| c.set(c.get() + 1)); |
| 10742 | } |
| 10743 | compound |
| 10744 | } |
| 10745 | |
| 10746 | /// Spec 5.11.25's `comp_reference_type` + the `uni_comp_ref`/`comp_ref`/ |
| 10747 | /// `comp_bwdref` trees (libaom `decodemv.c`'s `read_ref_frames` compound |
| 10748 | /// arm), reached once [`read_comp_mode`] has already returned |
| 10749 | /// `COMPOUND_REFERENCE`. Returns `(RefFrame[0], RefFrame[1])`. |
| 10750 | /// `above`/`left` are [`comp_reference_type_ctx`]'s own `NeighbourRef` |
| 10751 | /// convention; `above_ref`/`left_ref` are the scalar `1..=7`/`-1` |
| 10752 | /// convention [`read_single_ref`] already uses, for every downstream binary |
| 10753 | /// decision. libaom `pred_common.c`'s own compound context functions |
| 10754 | /// (`av1_get_pred_context_uni_comp_ref_p[1|2]`, `comp_ref_p[1|2]`, |
| 10755 | /// `comp_bwdref_p[1]`) turned out to be exactly [`single_ref_p1_ctx`].. |
| 10756 | /// [`single_ref_p6_ctx`]/[`uni_comp_ref_p1_ctx`] under a different name |
| 10757 | /// (same vote-the-neighbours-above-left shape, same forward/backward and |
| 10758 | /// LAST/LAST2/LAST3/GOLDEN groupings) -- lane-av1comp audited each one |
| 10759 | /// against the C source rather than porting six near-duplicate functions. |
| 10760 | /// Its caller today only needs the symbols consumed, correctly, to keep the |
| 10761 | /// arithmetic decoder's own state in sync before refusing the block by name |
| 10762 | /// -- the returned pair is not yet threaded into any motion-compensation |
| 10763 | /// path. |
| 10764 | fn read_compound_ref_frames( |
| 10765 | dec: &mut SymbolDecoder, |
| 10766 | cdfs: &mut Cdfs, |
| 10767 | above: Option<NeighbourRef>, |
| 10768 | left: Option<NeighbourRef>, |
| 10769 | above_ref: i8, |
| 10770 | left_ref: i8, |
| 10771 | ) -> (i8, i8) { |
| 10772 | let a = (above_ref > 0).then_some(above_ref); |
| 10773 | let l = (left_ref > 0).then_some(left_ref); |
| 10774 | // libaom `av1_collect_neighbors_ref_counts` counts BOTH of a compound |
| 10775 | // neighbour's references, not just its first -- `a1`/`l1` carry that |
| 10776 | // second reference through to every vote below (lane-av1blend r6: a |
| 10777 | // missing `a1`/`l1` here was decoding `compound_idx` off the wrong CDF |
| 10778 | // row for a block sitting under a skip_mode-forced compound neighbour). |
| 10779 | let a1 = above.and_then(|n| n.ref1); |
| 10780 | let l1 = left.and_then(|n| n.ref1); |
| 10781 | let type_ctx = comp_reference_type_ctx(above, left); |
| 10782 | let unidir = dec.symbol(&mut cdfs.comp_ref_type[type_ctx]) == 0; |
| 10783 | if unidir { |
| 10784 | // uni_comp_ref (p0): forward vs. backward -- av1_get_pred_context_- |
| 10785 | // uni_comp_ref_p is single_ref_p1's own forward/backward vote. |
| 10786 | let bit = dec.symbol(&mut cdfs.uni_comp_ref[single_ref_p1_ctx(a, a1, l, l1)][0]); |
| 10787 | if bit == 1 { |
| 10788 | (BWDREF_FRAME, ALTREF_FRAME) |
| 10789 | } else { |
| 10790 | let p1 = dec.symbol(&mut cdfs.uni_comp_ref[uni_comp_ref_p1_ctx(a, a1, l, l1)][1]); |
| 10791 | if p1 == 1 { |
| 10792 | // uni_comp_ref_p2: LAST3 vs. GOLDEN, same vote as single_ref_p5. |
| 10793 | let p2 = dec.symbol(&mut cdfs.uni_comp_ref[single_ref_p5_ctx(a, a1, l, l1)][2]); |
| 10794 | if p2 == 1 { |
| 10795 | (LAST_FRAME, GOLDEN_FRAME) |
| 10796 | } else { |
| 10797 | (LAST_FRAME, LAST3_FRAME) |
| 10798 | } |
| 10799 | } else { |
| 10800 | (LAST_FRAME, LAST2_FRAME) |
| 10801 | } |
| 10802 | } |
| 10803 | } else { |
| 10804 | // comp_ref (p0): LAST/LAST2 vs. LAST3/GOLDEN, same vote as single_ref_p3. |
| 10805 | let bit0 = dec.symbol(&mut cdfs.comp_ref[single_ref_p3_ctx(a, a1, l, l1)][0]); |
| 10806 | let ref0 = if bit0 == 0 { |
| 10807 | // comp_ref_p1: LAST vs. LAST2, same vote as single_ref_p4. |
| 10808 | let p1 = dec.symbol(&mut cdfs.comp_ref[single_ref_p4_ctx(a, a1, l, l1)][1]); |
| 10809 | if p1 == 1 { LAST2_FRAME } else { LAST_FRAME } |
| 10810 | } else { |
| 10811 | // comp_ref_p2: LAST3 vs. GOLDEN, same vote as single_ref_p5. |
| 10812 | let p2 = dec.symbol(&mut cdfs.comp_ref[single_ref_p5_ctx(a, a1, l, l1)][2]); |
| 10813 | if p2 == 1 { GOLDEN_FRAME } else { LAST3_FRAME } |
| 10814 | }; |
| 10815 | // comp_bwdref (p0): BWDREF/ALTREF2 vs. ALTREF, same vote as single_ref_p2. |
| 10816 | let bit1 = dec.symbol(&mut cdfs.comp_bwdref[single_ref_p2_ctx(a, a1, l, l1)][0]); |
| 10817 | let ref1 = if bit1 == 0 { |
| 10818 | // comp_bwdref_p1: BWDREF vs. ALTREF2, same vote as single_ref_p6. |
| 10819 | let p1 = dec.symbol(&mut cdfs.comp_bwdref[single_ref_p6_ctx(a, a1, l, l1)][1]); |
| 10820 | if p1 == 1 { ALTREF2_FRAME } else { BWDREF_FRAME } |
| 10821 | } else { |
| 10822 | ALTREF_FRAME |
| 10823 | }; |
| 10824 | (ref0, ref1) |
| 10825 | } |
| 10826 | } |
| 10827 | |
| 10828 | /// Spec 5.11.24's `compound_mode` (lane-av1comp): which of the eight |
| 10829 | /// `INTER_COMPOUND_MODES` (`NEAREST_NEARESTMV`..`NEW_NEWMV`, returned as |
| 10830 | /// `0..=7`) a `COMPOUND_REFERENCE` block takes, reached once |
| 10831 | /// [`read_comp_mode`] has already returned `COMPOUND_REFERENCE`. `ctx` is |
| 10832 | /// libaom's `av1_mode_context_analyzer`'s compound branch -- |
| 10833 | /// `compound_mode_ctx_map[ref_mv_ctx >> 1][min(new_mv_ctx, COMP_NEWMV_CTXS |
| 10834 | /// - 1)]` (`mvref_common.h`) -- folded from [`crate::mvstack:: |
| 10835 | /// find_mv_stack_compound`]'s own `new_mv_ctx`/`ref_mv_ctx`. The read symbol |
| 10836 | /// is not yet turned into an assigned MV pair or motion-compensated -- |
| 10837 | /// lane-av1comp. |
| 10838 | fn read_inter_compound_mode( |
| 10839 | dec: &mut SymbolDecoder, |
| 10840 | cdfs: &mut Cdfs, |
| 10841 | new_mv_ctx: usize, |
| 10842 | ref_mv_ctx: usize, |
| 10843 | ) -> u8 { |
| 10844 | let ctx = cdf::COMPOUND_MODE_CTX_MAP[ref_mv_ctx >> 1][new_mv_ctx.min(4)]; |
| 10845 | COMPOUND_MODE_HITS.with(|c| c.set(c.get() + 1)); |
| 10846 | dec.symbol(&mut cdfs.inter_compound_mode[ctx]) as u8 |
| 10847 | } |
| 10848 | |
| 10849 | /// spec 7.10.2.10 `assign_mv`'s compound branch, plus the compound half of |
| 10850 | /// `read_drl_idx` (spec 5.11.24) it depends on -- `mode` is |
| 10851 | /// [`read_inter_compound_mode`]'s own `0..=7` (`NEAREST_NEARESTMV`.. |
| 10852 | /// `NEW_NEWMV`, libaom `enums.h` order). Every `GLOBAL_GLOBALMV` side reads |
| 10853 | /// as `(0, 0)` since [`crate::stream::decode_stream`] already refuses any |
| 10854 | /// non-`IDENTITY` global motion model before a tile is ever decoded. |
| 10855 | /// |
| 10856 | /// Ported straight from libaom `decodemv.c`'s `read_drl_idx` (the |
| 10857 | /// `NEW_NEWMV`/`have_nearmv_in_inter_mode` branches) and `assign_mv`/its |
| 10858 | /// caller's `ref_mv[]`/`nearmv[]` setup: the `NEAR_NEWMV`/`NEW_NEARMV` |
| 10859 | /// `1 + ref_mv_idx` special case is folded into `new_idx` below, matching |
| 10860 | /// `read_drl_idx`'s own comment about "offsetting the NEARESTMV mode". |
| 10861 | fn assign_compound_mv( |
| 10862 | dec: &mut SymbolDecoder, |
| 10863 | cdfs: &mut Cdfs, |
| 10864 | comp_stack: &crate::mvstack::CompoundMvStack, |
| 10865 | mode: u8, |
| 10866 | allow_high_precision_mv: bool, |
| 10867 | force_integer_mv: bool, |
| 10868 | // lane-gm r2: `gm_get_motion_vector(ref0)`/`(ref1)` at this block's own |
| 10869 | // position, spec 7.10.2.10's `GLOBAL_GLOBALMV` assignment. |
| 10870 | global_mv: ((i32, i32), (i32, i32)), |
| 10871 | ) -> ((i32, i32), (i32, i32)) { |
| 10872 | const NEAREST_NEARESTMV: u8 = 0; |
| 10873 | const NEAR_NEARMV: u8 = 1; |
| 10874 | const NEAREST_NEWMV: u8 = 2; |
| 10875 | const NEW_NEARESTMV: u8 = 3; |
| 10876 | const NEAR_NEWMV: u8 = 4; |
| 10877 | const NEW_NEARMV: u8 = 5; |
| 10878 | const GLOBAL_GLOBALMV: u8 = 6; |
| 10879 | const NEW_NEWMV: u8 = 7; |
| 10880 | |
| 10881 | let mut ref_mv_idx = 0usize; |
| 10882 | if mode == NEW_NEWMV { |
| 10883 | let mut idx = 0usize; |
| 10884 | while idx < 2 && comp_stack.entries.len() > idx + 1 { |
| 10885 | if dec.symbol(&mut cdfs.drl_mode[comp_stack.drl_ctx[idx]]) == 0 { |
| 10886 | break; |
| 10887 | } |
| 10888 | idx += 1; |
| 10889 | } |
| 10890 | ref_mv_idx = idx; |
| 10891 | } else if matches!(mode, NEAR_NEARMV | NEAR_NEWMV | NEW_NEARMV) { |
| 10892 | let mut idx = 1usize; |
| 10893 | while idx < 3 && comp_stack.entries.len() > idx + 1 { |
| 10894 | if dec.symbol(&mut cdfs.drl_mode[comp_stack.drl_ctx[idx]]) == 0 { |
| 10895 | break; |
| 10896 | } |
| 10897 | idx += 1; |
| 10898 | } |
| 10899 | ref_mv_idx = idx - 1; |
| 10900 | } |
| 10901 | |
| 10902 | let near = comp_stack |
| 10903 | .entries |
| 10904 | .get(ref_mv_idx + 1) |
| 10905 | .map_or(comp_stack.near_mv, |e| (e.mv0, e.mv1)); |
| 10906 | let new_idx = if matches!(mode, NEAR_NEWMV | NEW_NEARMV) { |
| 10907 | ref_mv_idx + 1 |
| 10908 | } else { |
| 10909 | ref_mv_idx |
| 10910 | }; |
| 10911 | let new_pred = comp_stack |
| 10912 | .entries |
| 10913 | .get(new_idx) |
| 10914 | .map_or(comp_stack.nearest_mv, |e| (e.mv0, e.mv1)); |
| 10915 | |
| 10916 | let read_new = |dec: &mut SymbolDecoder, cdfs: &mut Cdfs, base: (i32, i32)| { |
| 10917 | read_mv( |
| 10918 | dec, |
| 10919 | &mut cdfs.mv_comp, |
| 10920 | &mut cdfs.mv_joint, |
| 10921 | base, |
| 10922 | allow_high_precision_mv, |
| 10923 | force_integer_mv, |
| 10924 | ) |
| 10925 | }; |
| 10926 | |
| 10927 | match mode { |
| 10928 | NEAREST_NEARESTMV => comp_stack.nearest_mv, |
| 10929 | NEAR_NEARMV => near, |
| 10930 | NEAREST_NEWMV => (comp_stack.nearest_mv.0, read_new(dec, cdfs, new_pred.1)), |
| 10931 | NEW_NEARESTMV => (read_new(dec, cdfs, new_pred.0), comp_stack.nearest_mv.1), |
| 10932 | NEAR_NEWMV => (near.0, read_new(dec, cdfs, new_pred.1)), |
| 10933 | NEW_NEARMV => (read_new(dec, cdfs, new_pred.0), near.1), |
| 10934 | GLOBAL_GLOBALMV => global_mv, |
| 10935 | _ => ( |
| 10936 | read_new(dec, cdfs, new_pred.0), |
| 10937 | read_new(dec, cdfs, new_pred.1), |
| 10938 | ), |
| 10939 | } |
| 10940 | } |
| 10941 | |
| 10942 | /// Derives this query block's own [`crate::mvstack::GmMvTable`] -- one |
| 10943 | /// `gm_get_motion_vector` (spec 7.10.2.1) result per reference frame, at |
| 10944 | /// THIS block's own `(mi_row, mi_col, bw4, bh4)` (mvstack.rs's own doc: the |
| 10945 | /// table is always computed at the *querying* block's position, never a |
| 10946 | /// donating neighbour's). |
| 10947 | fn build_gm_mv_table( |
| 10948 | global_motion: &[ec_av1_syntax::WarpParams; 7], |
| 10949 | mi_row: usize, |
| 10950 | mi_col: usize, |
| 10951 | bw4: usize, |
| 10952 | bh4: usize, |
| 10953 | allow_high_precision_mv: bool, |
| 10954 | force_integer_mv: bool, |
| 10955 | ) -> crate::mvstack::GmMvTable { |
| 10956 | std::array::from_fn(|i| { |
| 10957 | let gm = &global_motion[i]; |
| 10958 | crate::warp::gm_get_motion_vector( |
| 10959 | gm.model, |
| 10960 | &gm.params, |
| 10961 | mi_row, |
| 10962 | mi_col, |
| 10963 | bw4, |
| 10964 | bh4, |
| 10965 | allow_high_precision_mv, |
| 10966 | force_integer_mv, |
| 10967 | ) |
| 10968 | }) |
| 10969 | } |
| 10970 | |
| 10971 | /// Decodes one square inter-frame block (32x32 whole or 16x16 leaf): skip, |
| 10972 | /// `is_inter`, then either the single-reference/MV-stack/motion-vector chain |
| 10973 | /// and motion-compensated reconstruction, or the inter frame's own intra |
| 10974 | /// path (`Y_MODE` by size group, not `KF_Y_MODE` by neighbour), mirroring |
| 10975 | /// [`crate::tile`]'s `sb_coeff_inter_frame_tile`'s whole-block branch and its |
| 10976 | /// private `write_inter_frame_leaf` -- both share this one function here, |
| 10977 | /// parameterised by `side`/the transform sets/scan tables/size group they |
| 10978 | /// each use. |
| 10979 | /// |
| 10980 | /// # Errors |
| 10981 | /// Returns an error when a symbol names anything this decoder does not |
| 10982 | /// reconstruct: a reference other than `LAST_FRAME`, `GLOBALMV` (round 3; |
| 10983 | /// `NEARESTMV`/`NEARMV`/`NEWMV` are all reconstructed), or an intra |
| 10984 | /// mode/tx-type outside what [`read_intra_mode`]/[`read_coeffs`] already |
| 10985 | /// refuse. |
| 10986 | #[allow(clippy::too_many_arguments)] |
| 10987 | fn decode_inter_block( |
| 10988 | dec: &mut SymbolDecoder, |
| 10989 | cdfs: &mut Cdfs, |
| 10990 | neighbours: &mut Neighbours, |
| 10991 | grid: &mut MiGrid, |
| 10992 | at: (usize, usize), |
| 10993 | side: usize, |
| 10994 | mi_cols: u32, |
| 10995 | mi_rows: u32, |
| 10996 | y: &mut PlaneBuf, |
| 10997 | u: &mut PlaneBuf, |
| 10998 | v: &mut PlaneBuf, |
| 10999 | ref_y: &PlaneBuf, |
| 11000 | ref_u: &PlaneBuf, |
| 11001 | ref_v: &PlaneBuf, |
| 11002 | // lane-av1refs: every non-`LAST_FRAME` reference this frame header's own |
| 11003 | // `ref_frame_idx` names a live DPB slot for, indexed `[ref_frame]` |
| 11004 | // (`LAST2_FRAME`=2 .. `ALTREF_FRAME`=7; index 0/1 unused, `LAST_FRAME` |
| 11005 | // stays on `ref_y`/`u`/`v` above) -- `None` at a live index means the |
| 11006 | // slot's still empty (an error-resilient stream, or too early in the |
| 11007 | // GOP), matching `GOLDEN_FRAME`'s existing empty-slot refusal below. |
| 11008 | other_refs: &RefSlots, |
| 11009 | sign_bias_table: &SignBiasTable, |
| 11010 | // lane-gm r2: this frame header's own `global_motion` table (spec |
| 11011 | // 5.9.24), indexed `[ref_frame - LAST_FRAME]` same as `sign_bias_table` |
| 11012 | // -- feeds `warp::gm_get_motion_vector` for `GLOBALMV`/`GLOBAL_GLOBALMV` |
| 11013 | // assignment and the mv-stack's neighbour substitution. |
| 11014 | global_motion: &[ec_av1_syntax::WarpParams; 7], |
| 11015 | base_q_idx: u8, |
| 11016 | luma_set_intra: TxbSet, |
| 11017 | luma_set_inter: TxbSet, |
| 11018 | chroma_set: TxbSet, |
| 11019 | luma_tx: usize, |
| 11020 | chroma_tx: usize, |
| 11021 | scan_luma: &[u16], |
| 11022 | scan_chroma: &[u16], |
| 11023 | size_group: usize, |
| 11024 | allow_high_precision_mv: bool, |
| 11025 | force_integer_mv: bool, |
| 11026 | interp_fixed: Option<mc::InterpFilterKind>, |
| 11027 | enable_dual_filter: bool, |
| 11028 | tpl_frame: Option<&TplFrameArgs>, |
| 11029 | // lane-av1comp: this frame header's own `reference_select` bit -- `false` |
| 11030 | // means every block is `SINGLE_REFERENCE` by construction and `comp_mode` |
| 11031 | // is never read (spec 5.11.25); `true` reads `comp_mode` per block and |
| 11032 | // refuses (rather than silently misdecoding) the ones that pick |
| 11033 | // `COMPOUND_REFERENCE`, since two-reference motion compensation is not |
| 11034 | // wired yet. |
| 11035 | reference_select: bool, |
| 11036 | // lane-av1comp: `seq_params`' own masked-compound/distance-weighted- |
| 11037 | // compound enable bits, gating `comp_group_idx`/`compound_idx` |
| 11038 | // (spec 5.11.25) -- always present (unlike `tpl_frame`), since the |
| 11039 | // compound blend weights need this frame's own order hint regardless |
| 11040 | // of `use_ref_frame_mvs`. |
| 11041 | enable_masked_compound: bool, |
| 11042 | // lane-sb128 r4: this sequence header's own `enable_interintra_compound` |
| 11043 | // bit (spec 5.11.24's `interintra` read, libaom `decodemv.c` |
| 11044 | // read_inter_block_mode_info ~1490-1510) -- read right after `assign_mv` |
| 11045 | // and before `read_motion_mode` on the single-ref path below, gated by |
| 11046 | // `is_interintra_allowed` (single-ref, `BLOCK_8X8..=BLOCK_32X32`, and |
| 11047 | // every mode this single-ref branch can produce is already in |
| 11048 | // `SINGLE_INTER_MODE_START..END`); `interintra == 1` is a named refusal |
| 11049 | // (inter+intra blended prediction unimplemented) rather than a |
| 11050 | // mis-decode. |
| 11051 | enable_interintra_compound: bool, |
| 11052 | enable_jnt_comp: bool, |
| 11053 | order_hint_bits: u32, |
| 11054 | order_hint: u32, |
| 11055 | ref_order_hints: [u32; 7], |
| 11056 | // lane-av1comp round 14: this frame header's own `skip_mode_present` bit |
| 11057 | // and `skip_mode_frame` reference pair (spec 5.9.22, `frame.rs`'s |
| 11058 | // `read_skip_mode_params`, already 1-based `MV_REFERENCE_FRAME`) -- |
| 11059 | // `skip_mode` (spec 5.11.23 `read_skip_mode`) is read *before* `skip` |
| 11060 | // when this is `true` (every block this decoder reaches is >= 8x8, so |
| 11061 | // `is_comp_ref_allowed` never gates it further); a `true` result forces |
| 11062 | // `skip_txfm`, `is_inter`, `NEAREST_NEARESTMV` of this pair with no mode/ |
| 11063 | // DRL symbol read, and a plain average blend with no `comp_group_idx`/ |
| 11064 | // `compound_idx` read (libaom `decodemv.c` 1604-1619, 1382-1384, 1509). |
| 11065 | skip_mode_present: bool, |
| 11066 | skip_mode_frame: [u8; 2], |
| 11067 | // lane-motionmode round 1: this frame header's own `is_motion_mode_switchable`/ |
| 11068 | // `allow_warped_motion` bits (spec 5.11.24's `read_motion_mode`). |
| 11069 | switchable_motion_mode: bool, |
| 11070 | allow_warped_motion: bool, |
| 11071 | // lane-warp r5: set by the HORZ_B arm -- the 32x16 top strip is decoded |
| 11072 | // as a square 32x32 block, which over-reads a residual unless coded |
| 11073 | // `skip`; a non-skip strip must refuse cleanly instead of desyncing. |
| 11074 | reject_residual: bool, |
| 11075 | // lane-partitions r1: the strip's own true width/height in pixels -- |
| 11076 | // `side` keeps governing every syntax/CDF/prediction-buffer decision |
| 11077 | // (matching `HORZ_B`'s already-accepted square-context corner-cut), but |
| 11078 | // the final pixel write and this block's own neighbour-context stamp |
| 11079 | // (`record*`/`fill_*_grid`) use `write_w`/`write_h` instead, so a |
| 11080 | // PARTITION_HORZ/VERT strip only claims its own true footprint. `side` |
| 11081 | // for every existing (square) caller. |
| 11082 | write_w: usize, |
| 11083 | write_h: usize, |
| 11084 | // lane-screen r2: this frame header's own `allow_screen_content_tools` |
| 11085 | // bit, threaded to the intra-sub-block branch below -- libaom's |
| 11086 | // `read_intra_block_mode_info` (decodemv.c:1065-1107, the inter-frame |
| 11087 | // counterpart of `read_intra_frame_mode_info`) reads `y_mode_cdf` |
| 11088 | // (never `kf_y_mode_cdf`) and has NO `use_intrabc` call at all (that |
| 11089 | // symbol is intra-frame-only, spec `av1_read_mode_info` dispatches on |
| 11090 | // `frame_is_intra_only`), but it still calls `read_palette_mode_info` |
| 11091 | // under the same `av1_allow_palette` gate as the key-frame path. |
| 11092 | allow_screen_content_tools: bool, |
| 11093 | // lane-superres r9: this frame's own coded luma width (spec 7.11.3.3) -- |
| 11094 | // compared against each reference's stored picture width to decide |
| 11095 | // whether a scaled-MC path (`mc::predict_scaled`) or the ordinary |
| 11096 | // stride-1 one (`mc::predict_with_filters`) applies per reference, and to |
| 11097 | // refuse (rather than mis-decode) the warp/OBMC/interintra/compound |
| 11098 | // combinations that don't implement scaled MC yet. |
| 11099 | frame_width: usize, |
| 11100 | ) -> Result<()> { |
| 11101 | let (r, c) = at; |
| 11102 | // lane-inter8 r2: the inter side bands are mi-granular; this block's own |
| 11103 | // above/left neighbour is the mi cell at its top-left corner (libaom |
| 11104 | // `above_mbmi` = mi(mi_row - 1, mi_col)), so a SUB-aligned block reads |
| 11105 | // exactly what it read before. |
| 11106 | let (rmi, cmi) = (r * (SUB / MI), c * (SUB / MI)); |
| 11107 | let (px, py) = (c * SUB, r * SUB); |
| 11108 | let (cpx, cpy) = (px / 2, py / 2); |
| 11109 | let chroma_side = side / 2; |
| 11110 | let (write_chroma_w, write_chroma_h) = (write_w / 2, write_h / 2); |
| 11111 | |
| 11112 | if std::env::var_os("EC_AV1_TELL").is_some() { |
| 11113 | eprintln!( |
| 11114 | "TELL mi_row={} mi_col={} label=block_entry side={side} tell={} range={}", |
| 11115 | r * SUB_MI as usize, c * SUB_MI as usize, dec.debug_bitpos(), dec.debug_state().0 |
| 11116 | ); |
| 11117 | } |
| 11118 | let skip_mode_ctx = |
| 11119 | usize::from(neighbours.above_skip_mode[cmi]) + usize::from(neighbours.left_skip_mode[rmi]); |
| 11120 | // lane-comppin r3: skip_mode desync isolation -- dump ctx + the pre-read |
| 11121 | // MSAC tell so it can be diffed against an equivalent hook in libaom's |
| 11122 | // own `read_skip_mode`/`av1_get_skip_mode_context`. |
| 11123 | if std::env::var_os("EC_AV1_SKIPMODE_DUMP").is_some() { |
| 11124 | eprintln!( |
| 11125 | "EC_SKIPMODE r={r} c={c} ctx={skip_mode_ctx} skip_mode_present={skip_mode_present} tell_before={}", |
| 11126 | dec.debug_bitpos() |
| 11127 | ); |
| 11128 | } |
| 11129 | // spec 5.11.5 `inter_frame_mode_info`: `inter_segment_id(1)` before |
| 11130 | // `skip_mode`/`skip`, `inter_segment_id(0)` after them. |
| 11131 | let (seg_mi_r, seg_mi_c) = (r * SUB_MI as usize, c * SUB_MI as usize); |
| 11132 | let (seg_w_mi, seg_h_mi) = (write_w / 4, write_h / 4); |
| 11133 | inter_segment_id(dec, cdfs, seg_mi_r, seg_mi_c, seg_w_mi, seg_h_mi, false, true); |
| 11134 | let skip_mode = skip_mode_present && dec.symbol(&mut cdfs.skip_mode[skip_mode_ctx]) == 1; |
| 11135 | if std::env::var_os("EC_AV1_SKIPMODE_DUMP").is_some() { |
| 11136 | eprintln!( |
| 11137 | "EC_SKIPMODE r={r} c={c} result={skip_mode} tell_after={}", |
| 11138 | dec.debug_bitpos() |
| 11139 | ); |
| 11140 | } |
| 11141 | if skip_mode { |
| 11142 | SKIP_MODE_HITS.with(|c| c.set(c.get() + 1)); |
| 11143 | } |
| 11144 | |
| 11145 | let skip_ctx = usize::from(neighbours.above_skip[cmi]) + usize::from(neighbours.left_skip[rmi]); |
| 11146 | let skip = skip_mode || dec.symbol(&mut cdfs.skip[skip_ctx]) == 1; |
| 11147 | inter_segment_id(dec, cdfs, seg_mi_r, seg_mi_c, seg_w_mi, seg_h_mi, skip, false); |
| 11148 | maybe_read_cdef_idx(dec, r * SUB_MI as usize, c * SUB_MI as usize, skip); |
| 11149 | maybe_read_delta_q(dec, cdfs, r * SUB_MI as usize, c * SUB_MI as usize, side == 64, skip); |
| 11150 | maybe_read_delta_lf(dec, cdfs, r * SUB_MI as usize, c * SUB_MI as usize, side == 64, skip); |
| 11151 | // lane-warp r5: see `reject_residual` -- a square-block decode of a |
| 11152 | // HORZ_B strip is symbol-exact only for `skip` (no residual, no |
| 11153 | // motion_mode/warp symbol), so gate here before any further read. |
| 11154 | if reject_residual && !skip { |
| 11155 | return Err(unsupported( |
| 11156 | "a non-skip rectangular (HORZ/VERT/HORZ_B) strip needs rectangular residual coding", |
| 11157 | )); |
| 11158 | } |
| 11159 | |
| 11160 | let (has_above, has_left) = ( |
| 11161 | r > neighbours.tile_row0_mi / (SUB / MI), |
| 11162 | c > neighbours.tile_col0_mi / (SUB / MI), |
| 11163 | ); |
| 11164 | let (above_inter, left_inter) = (neighbours.above_inter[cmi], neighbours.left_inter[rmi]); |
| 11165 | let ii_ctx = intra_inter_ctx(has_above, has_left, above_inter, left_inter); |
| 11166 | let is_inter = skip_mode || dec.symbol(&mut cdfs.intra_inter[ii_ctx]) == 1; |
| 11167 | if std::env::var_os("EC_TRACE_MODE").is_some() { |
| 11168 | eprintln!( |
| 11169 | "EC_MODE mi_row={} mi_col={} rng={}", |
| 11170 | r * SUB_MI as usize, |
| 11171 | c * SUB_MI as usize, |
| 11172 | dec.debug_state().0 |
| 11173 | ); |
| 11174 | } |
| 11175 | if std::env::var_os("EC_AV1_TELL").is_some() { |
| 11176 | eprintln!( |
| 11177 | "TELL mi_row={} mi_col={} label=post_is_inter skip_mode={skip_mode} skip={skip} is_inter={is_inter} tell={} range={}", |
| 11178 | r * SUB_MI as usize, c * SUB_MI as usize, dec.debug_bitpos(), dec.debug_state().0 |
| 11179 | ); |
| 11180 | } |
| 11181 | |
| 11182 | let mode_for_tx; |
| 11183 | let uv_predict_mode; |
| 11184 | let (luma_grid, u_grid, v_grid); |
| 11185 | let block_filter; |
| 11186 | let ref_frame_for_lf; |
| 11187 | let globalmv_for_lf; |
| 11188 | // lane-av1comp: `Some((comp_group_idx, compound_idx))` only for a real |
| 11189 | // `COMPOUND_REFERENCE` block -- applied to `neighbours` *after* the |
| 11190 | // common `record_inter` call below (which would otherwise stamp the |
| 11191 | // single-ref ALTREF-special-case default over it). |
| 11192 | let mut compound_ctx: Option<(i8, u8, u8)> = None; |
| 11193 | // lane-txselect: `read_block_tx_size`'s leaf transform units (spec |
| 11194 | // 5.11.17), `Some` only when this block's var-tx tree resolved to more |
| 11195 | // than one luma transform -- read back by the residual loop, the |
| 11196 | // coefficient-context write-back and the loop-filter grid fill below. |
| 11197 | let mut vartx_leaves: Option<Vec<(usize, usize, usize)>> = None; |
| 11198 | let at_mi = (r * (SUB / MI), c * (SUB / MI)); |
| 11199 | if is_inter { |
| 11200 | let above_nbr = has_above.then(|| NeighbourRef { |
| 11201 | is_inter: above_inter, |
| 11202 | ref0: if above_inter { |
| 11203 | neighbours.above_ref[cmi] |
| 11204 | } else { |
| 11205 | 0 |
| 11206 | }, |
| 11207 | ref1: neighbours.above_ref1[cmi], |
| 11208 | uni: neighbours.above_ref1[cmi] |
| 11209 | .is_some_and(|r1| is_uni_comp_ref(neighbours.above_ref[cmi], r1)), |
| 11210 | }); |
| 11211 | let left_nbr = has_left.then(|| NeighbourRef { |
| 11212 | is_inter: left_inter, |
| 11213 | ref0: if left_inter { |
| 11214 | neighbours.left_ref[rmi] |
| 11215 | } else { |
| 11216 | 0 |
| 11217 | }, |
| 11218 | ref1: neighbours.left_ref1[rmi], |
| 11219 | uni: neighbours.left_ref1[rmi] |
| 11220 | .is_some_and(|r1| is_uni_comp_ref(neighbours.left_ref[rmi], r1)), |
| 11221 | }); |
| 11222 | if std::env::var_os("EC_AV1_COMPIDX_DUMP").is_some() { |
| 11223 | eprintln!( |
| 11224 | "EC_PRECOMP r={r} c={c} skip_mode={skip_mode} skip={skip} reference_select={reference_select} tell={}", |
| 11225 | dec.debug_bitpos() |
| 11226 | ); |
| 11227 | } |
| 11228 | let is_compound = |
| 11229 | skip_mode || (reference_select && read_comp_mode(dec, cdfs, above_nbr, left_nbr)); |
| 11230 | if is_compound { |
| 11231 | let (ref0, ref1) = if skip_mode { |
| 11232 | (skip_mode_frame[0] as i8, skip_mode_frame[1] as i8) |
| 11233 | } else { |
| 11234 | read_compound_ref_frames( |
| 11235 | dec, |
| 11236 | cdfs, |
| 11237 | above_nbr, |
| 11238 | left_nbr, |
| 11239 | neighbours.above_ref[cmi], |
| 11240 | neighbours.left_ref[rmi], |
| 11241 | ) |
| 11242 | }; |
| 11243 | let (mi_row, mi_col) = (r * SUB_MI as usize, c * SUB_MI as usize); |
| 11244 | // lane-rect r2: true footprint, mirroring the single-ref path below. |
| 11245 | let bw4 = write_w / 4; |
| 11246 | let bh4 = write_h / 4; |
| 11247 | // spec 7.10.2.8, doubled per side (`CompoundTplArgs`): same |
| 11248 | // `use_ref_frame_mvs` gating as the single-ref `tpl` build |
| 11249 | // below, just one `get_relative_dist` per reference. |
| 11250 | let comp_tpl = tpl_frame.map(|t| crate::mvstack::CompoundTplArgs { |
| 11251 | field: t.field, |
| 11252 | cur_offset_0: crate::motion_field::get_relative_dist( |
| 11253 | t.order_hint_bits, |
| 11254 | t.order_hint, |
| 11255 | t.ref_order_hints[(ref0 - LAST_FRAME) as usize], |
| 11256 | ), |
| 11257 | cur_offset_1: crate::motion_field::get_relative_dist( |
| 11258 | t.order_hint_bits, |
| 11259 | t.order_hint, |
| 11260 | t.ref_order_hints[(ref1 - LAST_FRAME) as usize], |
| 11261 | ), |
| 11262 | allow_high_precision_mv, |
| 11263 | }); |
| 11264 | let gm_table = build_gm_mv_table( |
| 11265 | global_motion, |
| 11266 | mi_row, |
| 11267 | mi_col, |
| 11268 | bw4, |
| 11269 | bh4, |
| 11270 | allow_high_precision_mv, |
| 11271 | force_integer_mv, |
| 11272 | ); |
| 11273 | let comp_stack = crate::mvstack::find_mv_stack_compound( |
| 11274 | grid, |
| 11275 | mi_row, |
| 11276 | mi_col, |
| 11277 | bw4, |
| 11278 | bh4, |
| 11279 | (ref0, ref1), |
| 11280 | mi_cols as usize, |
| 11281 | mi_rows as usize, |
| 11282 | sign_bias_table, |
| 11283 | &gm_table, |
| 11284 | comp_tpl, |
| 11285 | ); |
| 11286 | let compound_mode = if skip_mode { |
| 11287 | 0 // NEAREST_NEARESTMV, forced -- no mode symbol read |
| 11288 | } else { |
| 11289 | read_inter_compound_mode(dec, cdfs, comp_stack.new_mv_ctx, comp_stack.ref_mv_ctx) |
| 11290 | }; |
| 11291 | let (mv0, mv1) = assign_compound_mv( |
| 11292 | dec, |
| 11293 | cdfs, |
| 11294 | &comp_stack, |
| 11295 | compound_mode, |
| 11296 | allow_high_precision_mv, |
| 11297 | force_integer_mv, |
| 11298 | (gm_table[(ref0 - LAST_FRAME) as usize], gm_table[(ref1 - LAST_FRAME) as usize]), |
| 11299 | ); |
| 11300 | let is_globalmv = compound_mode == 6; // GLOBAL_GLOBALMV |
| 11301 | // spec `is_nontrans_global_motion`: ALL active refs' models must |
| 11302 | // be non-TRANSLATION (IDENTITY counts) for the compound block's |
| 11303 | // interp-filter read to be suppressed. |
| 11304 | let gm_nontrans = is_globalmv |
| 11305 | && global_motion[(ref0 - LAST_FRAME) as usize].model != ec_av1_syntax::WarpModel::Translation |
| 11306 | && global_motion[(ref1 - LAST_FRAME) as usize].model != ec_av1_syntax::WarpModel::Translation; |
| 11307 | let (py0, pu0, pv0) = ref_planes(ref0, ref_y, ref_u, ref_v, other_refs)?; |
| 11308 | let (py1, pu1, pv1) = ref_planes(ref1, ref_y, ref_u, ref_v, other_refs)?; |
| 11309 | // lane-cwarp r1: `is_global_mv_block` (blockd.h:421-429) is |
| 11310 | // PER REFERENCE SLOT -- mode GLOBAL_GLOBALMV, block >= 8x8, and |
| 11311 | // THAT slot's own gm model > TRANSLATION. `bw4`/`bh4` come from |
| 11312 | // the write extent below, so recompute the size bound from |
| 11313 | // `side` here (the block's own size, what libaom's `bsize` |
| 11314 | // is). |
| 11315 | let is_global_mv0 = is_globalmv |
| 11316 | && side >= 8 |
| 11317 | && global_motion[(ref0 - LAST_FRAME) as usize].model as u8 > 1; |
| 11318 | let is_global_mv1 = is_globalmv |
| 11319 | && side >= 8 |
| 11320 | && global_motion[(ref1 - LAST_FRAME) as usize].model as u8 > 1; |
| 11321 | // libaom `av1_init_warp_params` + `allow_warp`'s |
| 11322 | // `global_warp_allowed` branch, run once per reference for a |
| 11323 | // compound block exactly as for a single-ref one (reconinter.c |
| 11324 | // `build_inter_predictors_8x8_and_bigger` loops over `ref` and |
| 11325 | // calls `av1_init_warp_params` inside the loop) -- WARPED_CAUSAL |
| 11326 | // never reaches here (spec 5.11.27 / libaom `read_motion_mode`: |
| 11327 | // `has_second_ref` returns SIMPLE_TRANSLATION), so the local-warp |
| 11328 | // arm of `allow_warp` is dead on this path and only the global |
| 11329 | // one applies. `force_integer_mv` short-circuits |
| 11330 | // `av1_init_warp_params` before `allow_warp`. |
| 11331 | let compound_warp = |ref_frame: i8, eligible: bool| { |
| 11332 | if eligible |
| 11333 | && !force_integer_mv |
| 11334 | && !global_motion[(ref_frame - LAST_FRAME) as usize].invalid |
| 11335 | { |
| 11336 | crate::warp::global_warp_params( |
| 11337 | global_motion[(ref_frame - LAST_FRAME) as usize].params, |
| 11338 | ) |
| 11339 | } else { |
| 11340 | None |
| 11341 | } |
| 11342 | }; |
| 11343 | let warp0 = compound_warp(ref0, is_global_mv0); |
| 11344 | let warp1 = compound_warp(ref1, is_global_mv1); |
| 11345 | if warp0.is_some() || warp1.is_some() { |
| 11346 | COMPOUND_WARP_HITS.with(|c| c.set(c.get() + 1)); |
| 11347 | } |
| 11348 | // spec `get_ref_filter_type`: matches when EITHER of the |
| 11349 | // neighbour's two references equals this block's own ref0 -- |
| 11350 | // `above_ref1`/`left_ref1` is the neighbour's real second |
| 11351 | // reference (`record_compound_ctx`) when it was itself a |
| 11352 | // compound block; checking `above_ref`/`left_ref` alone drops a |
| 11353 | // real filter match down to the "no neighbour" sentinel |
| 11354 | // whenever the shared reference sits in the neighbour's SECOND |
| 11355 | // slot (lane-av1idx r2). |
| 11356 | let above_filter_ctx = if neighbours.above_ref[cmi] == ref0 |
| 11357 | || neighbours.above_ref1[cmi] == Some(ref0) |
| 11358 | { |
| 11359 | neighbours.above_filter[cmi] |
| 11360 | } else { |
| 11361 | [3, 3] |
| 11362 | }; |
| 11363 | let left_filter_ctx = if neighbours.left_ref[rmi] == ref0 |
| 11364 | || neighbours.left_ref1[rmi] == Some(ref0) |
| 11365 | { |
| 11366 | neighbours.left_filter[rmi] |
| 11367 | } else { |
| 11368 | [3, 3] |
| 11369 | }; |
| 11370 | globalmv_for_lf = is_globalmv; |
| 11371 | ref_frame_for_lf = ref0; |
| 11372 | mode_for_tx = 0; |
| 11373 | uv_predict_mode = DC_PRED; |
| 11374 | // lane-rect r2: `grid` (mvstack.rs's own MiGrid, distinct from |
| 11375 | // `neighbours`) must be stamped with the block's true footprint |
| 11376 | // too -- a square `bw4`x`bw4` stamp here claims a rect strip's |
| 11377 | // NEXT strip's own rows/cols before it decodes, corrupting |
| 11378 | // every later mvstack scan that reads this cell back. |
| 11379 | for dr in 0..bh4 { |
| 11380 | for dc in 0..bw4 { |
| 11381 | grid.set( |
| 11382 | mi_row + dr, |
| 11383 | mi_col + dc, |
| 11384 | MiInfo { |
| 11385 | is_inter: true, |
| 11386 | ref_frame: ref0, |
| 11387 | ref_frame1: Some(ref1), |
| 11388 | mv1: Some(mv1), |
| 11389 | mv: mv0, |
| 11390 | is_new_mv: matches!(compound_mode, 2 | 3 | 4 | 5 | 7), |
| 11391 | size: bw4, |
| 11392 | size_h: bh4, |
| 11393 | // libaom `is_global_mv_block`: per-ref-slot, mode |
| 11394 | // GLOBAL_GLOBALMV, block >= 8x8, and THAT slot's |
| 11395 | // own gm model > TRANSLATION (IDENTITY excluded |
| 11396 | // too -- the two-predicate trap, distinct from |
| 11397 | // `gm_nontrans` above which allows IDENTITY). |
| 11398 | is_global_mv0, |
| 11399 | is_global_mv1, |
| 11400 | }, |
| 11401 | ); |
| 11402 | } |
| 11403 | } |
| 11404 | // spec 5.11.25's `comp_group_idx`/`compound_idx`: only read when |
| 11405 | // masked compound / distance-weighted compound are actually |
| 11406 | // enabled for this stream -- `is_any_masked_compound_used_here` |
| 11407 | // is `true` for every block size this decoder ever reaches |
| 11408 | // (`min(side,side) >= 8`, `COMPOUND_DIFFWTD` always eligible |
| 11409 | // there), so the gate reduces to the sequence header bit; kept |
| 11410 | // as an explicit call so a future smaller block size is not |
| 11411 | // silently wrong (libaom `reconinter.h::is_any_masked_compound_used`). |
| 11412 | let group_ctx = get_comp_group_idx_context(neighbours, (rmi, cmi), side); |
| 11413 | let comp_group_idx = if !skip_mode |
| 11414 | && enable_masked_compound |
| 11415 | && is_any_masked_compound_used_here(side) |
| 11416 | { |
| 11417 | dec.symbol(&mut cdfs.comp_group_idx[group_ctx]) |
| 11418 | } else { |
| 11419 | 0 |
| 11420 | }; |
| 11421 | // lane-maskcomp r2 / lane-wedge r3: `Some(mask_type)` for a |
| 11422 | // `COMPOUND_DIFFWTD` block, `Some(mask)` (wedge codebook lookup) |
| 11423 | // for a `COMPOUND_WEDGE` block -- exactly one of the two is set |
| 11424 | // when `comp_group_idx == 1`. |
| 11425 | let mut diffwtd_mask_type: Option<u8> = None; |
| 11426 | let mut wedge_mask: Option<&'static [u8]> = None; |
| 11427 | if comp_group_idx == 1 { |
| 11428 | // lane-maskcomp r1: `read_compound_type` (spec 5.11.25, |
| 11429 | // libaom `decodemv.c` 1634-1656). `is_interinter_compound_used |
| 11430 | // (COMPOUND_WEDGE, bsize)` (`av1_is_wedge_used`, |
| 11431 | // `av1_wedge_params_lookup`) is true for every square bsize |
| 11432 | // this leaf reaches (8x8/16x16/32x32 all have |
| 11433 | // `wedge_types > 0`), so `compound_type` always reads a real |
| 11434 | // symbol here -- no alphabet collapse to worry about at this |
| 11435 | // block-size family. |
| 11436 | let wedge_bsize = match side { |
| 11437 | 8 => 3, |
| 11438 | 16 => 6, |
| 11439 | 32 => 9, |
| 11440 | // lane-inter8 r1: `av1_is_wedge_used` is false at 64x64 |
| 11441 | // (`av1_wedge_params_lookup[BLOCK_64X64].wedge_types == |
| 11442 | // 0`), so a real encoder writes NO `compound_type` symbol |
| 11443 | // there -- COMPOUND_DIFFWTD is inferred. Reading one |
| 11444 | // would desync, so refuse by name rather than guess the |
| 11445 | // inferred-DIFFWTD blend this round. |
| 11446 | _ => { |
| 11447 | return Err(unsupported( |
| 11448 | "a masked compound 64x64 inter block (compound_type is inferred, not coded, at this size)", |
| 11449 | )) |
| 11450 | } |
| 11451 | }; |
| 11452 | let compound_type = dec.symbol(&mut cdfs.compound_type[wedge_bsize]); |
| 11453 | if compound_type == 0 { |
| 11454 | // COMPOUND_WEDGE: lane-wedge r3, codebook checksum- |
| 11455 | // verified vs independent C dump (wedge.rs). |
| 11456 | let wedge_index = dec.symbol(&mut cdfs.wedge_idx[wedge_bsize]); |
| 11457 | let wedge_sign = dec.literal(1); |
| 11458 | WEDGE_HITS.with(|c| c.set(c.get() + 1)); |
| 11459 | wedge_mask = Some( |
| 11460 | crate::wedge::wedge_masks() |
| 11461 | .codebook(side) |
| 11462 | .mask(wedge_sign as usize, wedge_index as usize), |
| 11463 | ); |
| 11464 | } else { |
| 11465 | // COMPOUND_DIFFWTD: `mask_type`, MAX_DIFFWTD_MASK_BITS==1. |
| 11466 | let mask_type = dec.literal(1); |
| 11467 | diffwtd_mask_type = Some(mask_type as u8); |
| 11468 | } |
| 11469 | MASKED_COMPOUND_HITS.with(|c| c.set(c.get() + 1)); |
| 11470 | } |
| 11471 | // corner-cut (lane-av1comp r16/r17, lane-av1blend r1): r16/r17 |
| 11472 | // called this a `predict_compound_intermediate`/`combine_compound` |
| 11473 | // reconstruction defect. r1 FALSIFIED that hypothesis outright: |
| 11474 | // unmasking and running the real gates |
| 11475 | // (a_real_aomenc_stream_with_reference_select_reads_comp_mode_correctly, |
| 11476 | // a_real_aomenc_stream_with_compound_references_decodes_pixel_exact) |
| 11477 | // shows the plain-average blend is bit-exact *most of the time* |
| 11478 | // -- `combine_compound`'s two-shift-in-one round2 was proven |
| 11479 | // algebraically identical to libaom's separate |
| 11480 | // `>>DIST_PRECISION_BITS`-then-`ROUND_POWER_OF_TWO` sequence |
| 11481 | // (av1_dist_wtd_convolve_2d_c, convolve.c:402-415) for every |
| 11482 | // input, and a whole-pel zero-MV compound block (mv0=mv1=(0,0), |
| 11483 | // the common case a lag-in-frames GOP's hidden altref frames |
| 11484 | // hit) reproduces aomdec's own pre-deblock dump byte-for-byte |
| 11485 | // for several consecutive frames. |
| 11486 | // |
| 11487 | // The real defect a live gate run isolated (seed 49, |
| 11488 | // `fixtures/av1blend-r1-mismatch.obu`): decode-order frame 0 |
| 11489 | // (keyframe) and frame 1 (first hidden altref, `ref1 == |
| 11490 | // ALTREF_FRAME`) match aomdec's own pre-deblock dump byte for |
| 11491 | // byte; decode-order frame 2 -- same shape, same zero MV, only |
| 11492 | // difference `ref1 == BWDREF_FRAME` instead of `ALTREF_FRAME` -- |
| 11493 | // is the *first* frame to diverge (881/6144 luma+chroma bytes). |
| 11494 | // Every later frame up through decode-order 9 also mismatches, |
| 11495 | // *including* frame 8, whose four blocks are back to |
| 11496 | // `ref1 == ALTREF_FRAME` only -- ruling out "BWDREF_FRAME's MC |
| 11497 | // math is wrong" (that block's own math is proven exact) in |
| 11498 | // favour of "the picture sitting in some DPB slot is already |
| 11499 | // wrong by the time frame 2 reads it, and the corruption |
| 11500 | // propagates through refresh_frame_flags into every later |
| 11501 | // reference regardless of which named ref a later block picks". |
| 11502 | // Frames 10 through 18 (once the GOP's lineage stops tracing |
| 11503 | // back through the bad slot) are bit-exact again. This points at |
| 11504 | // reference-slot bookkeeping (`ref_slots`/`refresh_frame_flags` |
| 11505 | // in `stream.rs`'s `decode_stream`, or `ref_order_hints`/DPB |
| 11506 | // indexing feeding `dist_wtd_comp_weight_assign`) around |
| 11507 | // BWDREF_FRAME specifically, not at the MC/blend arithmetic in |
| 11508 | // mc.rs. Not isolated further within lane-av1blend's budget -- |
| 11509 | // refuse by name rather than ship a blend that is right only |
| 11510 | // sometimes. Ceiling: r13/r14's MC plumbing above this line |
| 11511 | // stays -- only the plain-average reconstruct is masked off |
| 11512 | // again. Upgrade: instrument `ref_slots`/`refresh_frame_flags` |
| 11513 | // per decode-order frame (which slot each `Frame` OBU refreshes, |
| 11514 | // and which slot each `ref_frame_idx[BWDREF_FRAME - LAST_FRAME]` |
| 11515 | // reads) against aomdec's own `RefCntBuffer` bookkeeping on |
| 11516 | // `fixtures/av1blend-r1-mismatch.obu`, starting at decode-order |
| 11517 | // frame 2. |
| 11518 | // |
| 11519 | // r2 (fresh fixture, seed 45, same recipe -- the original r1 |
| 11520 | // fixture is gitignored and could not be regenerated byte-exact): |
| 11521 | // static review of `stream.rs`'s `ref_slots`/`refresh_frame_flags`/ |
| 11522 | // `cdf_slots` bookkeeping and the `show_existing_frame` refresh |
| 11523 | // arm (spec 7.21: only refreshes on a shown KEY frame, which the |
| 11524 | // parser already enforces at `frame.rs:791/815` -- `refresh_frame_flags` |
| 11525 | // is forced 0 on every non-key `show_existing_frame`) turned up |
| 11526 | // nothing wrong; `other_refs`/`ref_planes` slot indexing for |
| 11527 | // `BWDREF_FRAME` is spec-correct too. A live unmasked run |
| 11528 | // isolated a *different, smaller* shape than r1's: decode-order |
| 11529 | // frame 3 (a `ref1 == BWDREF_FRAME`, zero-MV, simple-average |
| 11530 | // compound block) is the first to diverge from ffmpeg (worst |
| 11531 | // delta 1, luma only, clustered at 32-pixel block edges) even |
| 11532 | // though BOTH its compound inputs -- the key frame and |
| 11533 | // decode-order frame 2's own picture (`ref1 == ALTREF_FRAME`, |
| 11534 | // itself proven bit-exact: it is later re-shown via |
| 11535 | // `show_existing_frame` and matches ffmpeg exactly) -- are |
| 11536 | // independently pixel-exact, and the blend arithmetic is the |
| 11537 | // same proven-exact formula. That rules out "wrong slot"/"stale |
| 11538 | // picture" for this fixture: the inputs are right, so the defect |
| 11539 | // is in decode-order frame 3's *own* per-block decode (entropy |
| 11540 | // desync from compound-CDF adaptation carried across a slot, or |
| 11541 | // the compound mode/MV read itself) -- not in `ref_slots`/ |
| 11542 | // `refresh_frame_flags`. Not isolated further within budget. |
| 11543 | // Upgrade: bisect decode-order frame 3's tile decode against a |
| 11544 | // synced `EC_TOK`/`EC_PART` trace from the instrumented aomdec |
| 11545 | // build (`/tmp/libaom-src/build/decoder-debug/aomdec`) on |
| 11546 | // `fixtures/av1blend-r1-mismatch.obu` (seed 45 now, not seed 49) |
| 11547 | // -- the pre-deblock dump frame-index alignment between this |
| 11548 | // crate and that aomdec build is NOT 1:1 on this fixture (23 |
| 11549 | // dumps here vs 24 there), so calibrate that first. |
| 11550 | // r3 (this round): pre-deblock buffers were bisected byte-for-byte |
| 11551 | // against an instrumented aomdec build (frame-index alignment |
| 11552 | // recalibrated via content-hash matching rather than the raw |
| 11553 | // dump count -- the keyframe never calls a pre-deblock dump at |
| 11554 | // all, since only `decode_inter_frame_tile` does, which is the |
| 11555 | // whole "23 vs 24" gap, not a real bug). |
| 11556 | // Decode-order frame 3 (`fixtures/av1blend-r1-mismatch.obu`, |
| 11557 | // seed 45) is still the first divergence: PRE-deblock differs |
| 11558 | // from aomdec's own pre-deblock dump by up to 1, on ~20% of |
| 11559 | // luma pixels, clustered where its two compound inputs' pixel |
| 11560 | // values disagree by more than a couple of levels -- both |
| 11561 | // inputs (the keyframe and decode-order frame 2's own picture) |
| 11562 | // are independently proven bit-exact (pre- and post-deblock) |
| 11563 | // against aomdec, and `decodemv`'s own EC_TRACE (mode/mv/skip) |
| 11564 | // matches this decoder's block loop exactly for every block in |
| 11565 | // this frame. Solving `aomval = (key*w0 + bwd*w1 + 8) >> 4` for |
| 11566 | // every mismatching pixel against `key`/`bwd` (both known-exact) |
| 11567 | // picks out `w0=9, w1=7` for the majority (525/846) with zero |
| 11568 | // matches for the plain average this decoder actually emits or |
| 11569 | // the reversed `w0=7, w1=9` -- i.e. `compound_idx` is being |
| 11570 | // decoded as `1` (simple average, spec `COMPOUND_AVERAGE`) when |
| 11571 | // the real bitstream encoded `0` (`COMPOUND_DISTWTD`, |
| 11572 | // `dist_wtd_comp_weight_assign`'s own weights for this frame's |
| 11573 | // order-hint distances 2/3 land on exactly `(9, 7)` -- this |
| 11574 | // decoder's own `dist_wtd_comp_weight_assign` was independently |
| 11575 | // re-verified against `QUANT_DIST_WEIGHT`/`QUANT_DIST_LOOKUP_TABLE` |
| 11576 | // by hand for these distances and gives the same `(9, 7)`). |
| 11577 | // `get_comp_index_context` was checked line-for-line against |
| 11578 | // libaom's `pred_common.h` (the `fwd`/`bck` distance halves, the |
| 11579 | // `above_mi`/`left_mi` `has_second_ref`/`ALTREF_FRAME` fallback, |
| 11580 | // the `3 * offset` term) and matches; `Default_Compound_Idx_Cdf` |
| 11581 | // (`cdf.rs`'s `COMPOUND_IDX`) matches `entropymode.c`'s |
| 11582 | // `default_compound_idx_cdfs` value-for-value; the read is |
| 11583 | // gated on `!skip_mode && enable_jnt_comp`, matching libaom's |
| 11584 | // `has_second_ref(mbmi) && !mbmi->skip_mode` outer guard plus |
| 11585 | // `order_hint_info.enable_dist_wtd_comp` inner guard. The |
| 11586 | // remaining 321/846 mismatched pixels do NOT fit `(9, 7)` |
| 11587 | // either -- some blocks in this frame plausibly decode a |
| 11588 | // genuinely different (correct, context-dependent) compound_idx |
| 11589 | // than block (0,0)'s own -- so this is not simply "always wrong |
| 11590 | // the same way": table/context/gate are all individually |
| 11591 | // verified correct, yet the decoded VALUE at this specific |
| 11592 | // symbol is wrong at least once in this frame. Not isolated |
| 11593 | // further within budget. Upgrade: instrument `dec.symbol`'s own |
| 11594 | // range/bit state (`rng`/`tell`, matching aomdec's own EC_PART |
| 11595 | // trace fields) immediately before and after the `compound_idx` |
| 11596 | // read specifically, not just the surrounding mode/mv/skip |
| 11597 | // trace which does match -- the desync (if any) is local to |
| 11598 | // this one read, not a wider stream desync. |
| 11599 | // |
| 11600 | // r5 (this round): the `resolve_interp_filter` call used to sit |
| 11601 | // right here, ahead of `comp_group_idx`/`compound_idx` -- spec |
| 11602 | // 5.11.19/libaom `decodemv.c:1575` reads `interp_filter` AFTER |
| 11603 | // `read_compound_type`, not before, so that was a genuine |
| 11604 | // ordering bug for a Switchable-filter frame (moved below, past |
| 11605 | // the `compound_ctx = Some(...)` line). It does NOT explain this |
| 11606 | // fixture's own mismatch, though: `av1blend-r1-mismatch.obu`'s |
| 11607 | // header carries a *fixed* interpolation filter |
| 11608 | // (`interp_fixed = Some(Regular)`, confirmed live via a |
| 11609 | // one-off dump), so `resolve_interp_filter` never read a symbol |
| 11610 | // in either position -- zero bits moved, decoded values |
| 11611 | // unchanged before and after the reorder. Kept anyway: it is a |
| 11612 | // real spec-order fix for the next Switchable-filter compound |
| 11613 | // stream, independently correct regardless of this fixture. |
| 11614 | // |
| 11615 | // Also ruled out this round: comparing this crate's own |
| 11616 | // `SymbolDecoder::debug_bitpos()` against aomdec's |
| 11617 | // `aom_reader_tell()` at the block boundary is NOT a valid |
| 11618 | // desync probe -- they are different quantities (raw consumed |
| 11619 | // input bits vs. an entropy-cost estimate that advances without |
| 11620 | // a literal byte fetch), so equal/unequal bitpos proves nothing |
| 11621 | // either way. Do not repeat that comparison; instead trace |
| 11622 | // `range`/`value` (this crate) against libaom's own internal |
| 11623 | // `dif`/`rng` (needs new instrumentation in both) immediately |
| 11624 | // after each read *upstream* of this one in the same block: |
| 11625 | // `read_comp_mode`, `read_compound_ref_frames` (comp_ref_type/ |
| 11626 | // uni_comp_ref/comp_ref/comp_bwdref), `read_inter_compound_mode`, |
| 11627 | // and the DRL loop. All of those independently decoded the SAME |
| 11628 | // values this crate and aomdec both show (ref0=1, ref1=5, mode |
| 11629 | // NEAREST_NEARESTMV, mv=(0,0)) -- but an upstream *context* |
| 11630 | // miscomputation can still decode the identical symbol value off |
| 11631 | // a slightly different CDF, narrowing `range` by a different |
| 11632 | // amount than aomdec without changing the decoded value itself, |
| 11633 | // and only surface as a wrong decode a few reads later exactly |
| 11634 | // like this. `get_comp_index_context` and the `COMPOUND_IDX` |
| 11635 | // table were independently re-verified again this round and are |
| 11636 | // still correct; the four candidates above were not re-audited |
| 11637 | // this round. Refusing here (rather than shipping a value-exact- |
| 11638 | // most-of-the-time blend) until one of them is found wrong. |
| 11639 | // r6/r7: found and fixed one real bug (mvstack.rs ref_ctx family |
| 11640 | // undercounted compound neighbours in the single_ref_p*/comp_ref*/ |
| 11641 | // comp_bwdref* context reads -- kept, real fix). Unmasking on |
| 11642 | // that fix alone and running the wide gate sweep (many seeds, |
| 11643 | // `a_real_aomenc_stream_with_compound_references_decodes_pixel_exact`/ |
| 11644 | // `a_real_aomenc_stream_with_reference_select_reads_comp_mode_correctly`, |
| 11645 | // cq-level=45) still mismatches roughly half the time, at |
| 11646 | // different frames each run for the SAME seed -- aomenc's own |
| 11647 | // cpu-used=0 RD search is run-to-run nondeterministic (noted |
| 11648 | // above), so this is a second, still-unfound compound_idx defect, |
| 11649 | // not the one r6/r7 fixed. r6's own residual-mismatch note (321/ |
| 11650 | // 846 pixels not fitting the (9,7)-vs-plain-average hypothesis |
| 11651 | // either) already flagged more than one bug here. Re-masking: |
| 11652 | // do not ship a blend that is right only sometimes. |
| 11653 | // lane-av1idx r1: fixed one real bug this round |
| 11654 | // (`switchable_interp_ctx` never folded in libaom's |
| 11655 | // `INTER_FILTER_COMP_OFFSET` for a compound block's own |
| 11656 | // interp_filter context -- kept). Pinned |
| 11657 | // `fixtures/av1idx-refsel-pin.obu` (seed 61, reference_select |
| 11658 | // gate) still mismatches after that fix (decode-order frame 8 |
| 11659 | // first, small deltas, worst 1-2, propagating through later |
| 11660 | // frames via DPB reference) -- a second, still-unfound bug. |
| 11661 | // Re-masking: see that fixture + `scratch_isolate_pinned_mismatch` |
| 11662 | // (`EC_AV1_PIN=fixtures/av1idx-refsel-pin.obu EC_AV1_PIN_N=20`) |
| 11663 | // for the next round's starting point. |
| 11664 | // lane-maskcomp r2: `compound_idx` is only read when |
| 11665 | // `comp_group_idx == 0` (libaom `decodemv.c:1606`) -- a masked |
| 11666 | // (wedge/diffwtd) block never also carries a distance-weighted |
| 11667 | // split. This gate was missing before r2 (dead code: the masked |
| 11668 | // path always refused earlier, so it never executed with |
| 11669 | // `comp_group_idx == 1` in practice). |
| 11670 | let (fwd_offset, bck_offset, compound_idx) = if comp_group_idx == 0 |
| 11671 | && !skip_mode |
| 11672 | && enable_jnt_comp |
| 11673 | { |
| 11674 | let idx_ctx = get_comp_index_context( |
| 11675 | neighbours, |
| 11676 | (rmi, cmi), |
| 11677 | side, |
| 11678 | order_hint_bits, |
| 11679 | order_hint, |
| 11680 | ref_order_hints[(ref0 - LAST_FRAME) as usize], |
| 11681 | ref_order_hints[(ref1 - LAST_FRAME) as usize], |
| 11682 | ); |
| 11683 | let idx = dec.symbol(&mut cdfs.compound_idx[idx_ctx]); |
| 11684 | if idx == 1 { |
| 11685 | (8, 8, 1u8) |
| 11686 | } else { |
| 11687 | let (fwd, bck) = crate::compound::dist_wtd_comp_weight_assign( |
| 11688 | order_hint_bits, |
| 11689 | order_hint, |
| 11690 | ref_order_hints[(ref0 - LAST_FRAME) as usize], |
| 11691 | ref_order_hints[(ref1 - LAST_FRAME) as usize], |
| 11692 | ); |
| 11693 | (fwd, bck, 0u8) |
| 11694 | } |
| 11695 | } else { |
| 11696 | (8, 8, 1u8) |
| 11697 | }; |
| 11698 | compound_ctx = Some((ref1, comp_group_idx as u8, compound_idx)); |
| 11699 | if std::env::var_os("EC_AV1_COMPIDX_DUMP").is_some() { |
| 11700 | eprintln!( |
| 11701 | "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={}", |
| 11702 | mv0.0, mv0.1, mv1.0, mv1.1, dec.debug_bitpos() |
| 11703 | ); |
| 11704 | } |
| 11705 | |
| 11706 | // spec 5.11.19/libaom `decodemv.c` 1575: `read_mb_interp_filter` |
| 11707 | // comes AFTER `read_compound_type` (the `comp_group_idx`/ |
| 11708 | // `compound_idx` reads just above), not before -- moved here |
| 11709 | // (lane-av1blend r5) from its old position ahead of those reads, |
| 11710 | // which stole their bits for a Switchable-filter compound block |
| 11711 | // and desynced every symbol read after it in the tile. |
| 11712 | // `av1_is_interp_needed` is also 0 for `skip_mode` blocks: the |
| 11713 | // filter is derived (Regular), never read -- same |
| 11714 | // conditional-read contract as the WARPED_CAUSAL suppression on |
| 11715 | // the single-ref path below. |
| 11716 | let (h_filter, v_filter, resolved_filter) = resolve_interp_filter( |
| 11717 | dec, |
| 11718 | cdfs, |
| 11719 | interp_fixed, |
| 11720 | enable_dual_filter, |
| 11721 | gm_nontrans || skip_mode, |
| 11722 | above_filter_ctx, |
| 11723 | left_filter_ctx, |
| 11724 | true, |
| 11725 | ); |
| 11726 | block_filter = resolved_filter; |
| 11727 | |
| 11728 | // lane-scaledref r1: each compound tap scales INDEPENDENTLY off |
| 11729 | // its own stored reference's luma width (spec 7.11.3.3 derives |
| 11730 | // x_scale_fp per reference; libaom `av1_setup_pre_planes` builds |
| 11731 | // one `scale_factors` per `ref_frame`), so the two taps can carry |
| 11732 | // different ratios in the same block. `REF_NO_SCALE` reduces |
| 11733 | // `predict_compound_intermediate`'s walk to the ordinary |
| 11734 | // stride-1 one, so the unscaled case is unchanged. |
| 11735 | let scale0 = mc::scale_factor(py0.width, frame_width); |
| 11736 | let scale1 = mc::scale_factor(py1.width, frame_width); |
| 11737 | if scale0 != mc::REF_NO_SCALE || scale1 != mc::REF_NO_SCALE { |
| 11738 | SCALED_COMPOUND_HITS.with(|c| c.set(c.get() + 1)); |
| 11739 | } |
| 11740 | // lane-scaledref r2: the MIXED case -- one tap scaled, the other |
| 11741 | // not -- is the one an all-frames-scaled recipe never produces, |
| 11742 | // and the one a single shared scale factor would get wrong. |
| 11743 | if (scale0 == mc::REF_NO_SCALE) != (scale1 == mc::REF_NO_SCALE) { |
| 11744 | MIXED_SCALE_COMPOUND_HITS.with(|c| c.set(c.get() + 1)); |
| 11745 | } |
| 11746 | |
| 11747 | let mut inter0_y = vec![0i32; side * side]; |
| 11748 | mc::predict_compound_intermediate( |
| 11749 | &py0.data, |
| 11750 | py0.width, |
| 11751 | py0.true_width, |
| 11752 | py0.true_height, |
| 11753 | mv_to_q4(px, mv0.1, true), |
| 11754 | mv_to_q4(py, mv0.0, true), |
| 11755 | scale0, |
| 11756 | side, |
| 11757 | side, |
| 11758 | h_filter, |
| 11759 | v_filter, |
| 11760 | &mut inter0_y, |
| 11761 | ); |
| 11762 | // lane-cwarp r1: this reference's own GLOBAL warp replaces the |
| 11763 | // translational tap (libaom `av1_warp_plane` with |
| 11764 | // `conv_params->is_compound`); the blend below is unchanged. |
| 11765 | if let Some(wp) = &warp0 { |
| 11766 | crate::warp::warp_affine_compound( |
| 11767 | wp, &py0.data, py0.true_width as i32, py0.true_height as i32, |
| 11768 | py0.width as i32, &mut inter0_y, px as i32, py as i32, side as i32, |
| 11769 | side as i32, side as i32, 0, 0, |
| 11770 | ); |
| 11771 | } |
| 11772 | let mut inter1_y = vec![0i32; side * side]; |
| 11773 | mc::predict_compound_intermediate( |
| 11774 | &py1.data, |
| 11775 | py1.width, |
| 11776 | py1.true_width, |
| 11777 | py1.true_height, |
| 11778 | mv_to_q4(px, mv1.1, true), |
| 11779 | mv_to_q4(py, mv1.0, true), |
| 11780 | scale1, |
| 11781 | side, |
| 11782 | side, |
| 11783 | h_filter, |
| 11784 | v_filter, |
| 11785 | &mut inter1_y, |
| 11786 | ); |
| 11787 | // lane-cwarp r1: this reference's own GLOBAL warp replaces the |
| 11788 | // translational tap (libaom `av1_warp_plane` with |
| 11789 | // `conv_params->is_compound`); the blend below is unchanged. |
| 11790 | if let Some(wp) = &warp1 { |
| 11791 | crate::warp::warp_affine_compound( |
| 11792 | wp, &py1.data, py1.true_width as i32, py1.true_height as i32, |
| 11793 | py1.width as i32, &mut inter1_y, px as i32, py as i32, side as i32, |
| 11794 | side as i32, side as i32, 0, 0, |
| 11795 | ); |
| 11796 | } |
| 11797 | let mut pred_y = vec![0u16; side * side]; |
| 11798 | let mut diffwtd_mask_y = Vec::new(); |
| 11799 | // lane-wedge r3: `mask_y` is the DIFFWTD buffer just computed OR |
| 11800 | // the wedge codebook lookup (comp_group_idx==1's two mutually |
| 11801 | // exclusive branches) -- same [`mc::blend_masked_compound`] call |
| 11802 | // either way, only the mask source differs. |
| 11803 | let mask_y: Option<&[u8]> = if let Some(mask_type) = diffwtd_mask_type { |
| 11804 | diffwtd_mask_y = vec![0u8; side * side]; |
| 11805 | mc::diffwtd_mask(&inter0_y, &inter1_y, mask_type == 1, &mut diffwtd_mask_y); |
| 11806 | Some(diffwtd_mask_y.as_slice()) |
| 11807 | } else { |
| 11808 | wedge_mask |
| 11809 | }; |
| 11810 | if let Some(mask_y) = mask_y { |
| 11811 | mc::blend_masked_compound( |
| 11812 | &inter0_y, &inter1_y, mask_y, side, side, side, false, &mut pred_y, |
| 11813 | ); |
| 11814 | } else { |
| 11815 | mc::combine_compound(&inter0_y, &inter1_y, fwd_offset, bck_offset, &mut pred_y); |
| 11816 | } |
| 11817 | |
| 11818 | let mut inter0_u = vec![0i32; chroma_side * chroma_side]; |
| 11819 | mc::predict_compound_intermediate( |
| 11820 | &pu0.data, |
| 11821 | pu0.width, |
| 11822 | pu0.true_width, |
| 11823 | pu0.true_height, |
| 11824 | mv_to_q4(cpx, mv0.1, false), |
| 11825 | mv_to_q4(cpy, mv0.0, false), |
| 11826 | scale0, |
| 11827 | chroma_side, |
| 11828 | chroma_side, |
| 11829 | h_filter, |
| 11830 | v_filter, |
| 11831 | &mut inter0_u, |
| 11832 | ); |
| 11833 | // `av1_init_warp_params` bails when the PLANE's block is |
| 11834 | // narrower/shorter than 8 (`block_width < 8`), so a 4x4 chroma |
| 11835 | // block of an 8x8 luma block stays translational. |
| 11836 | if let Some(wp) = &warp0 { |
| 11837 | if chroma_side >= 8 { |
| 11838 | crate::warp::warp_affine_compound( |
| 11839 | wp, &pu0.data, pu0.true_width as i32, pu0.true_height as i32, |
| 11840 | pu0.width as i32, &mut inter0_u, cpx as i32, cpy as i32, |
| 11841 | chroma_side as i32, chroma_side as i32, chroma_side as i32, 1, 1, |
| 11842 | ); |
| 11843 | } |
| 11844 | } |
| 11845 | let mut inter1_u = vec![0i32; chroma_side * chroma_side]; |
| 11846 | mc::predict_compound_intermediate( |
| 11847 | &pu1.data, |
| 11848 | pu1.width, |
| 11849 | pu1.true_width, |
| 11850 | pu1.true_height, |
| 11851 | mv_to_q4(cpx, mv1.1, false), |
| 11852 | mv_to_q4(cpy, mv1.0, false), |
| 11853 | scale1, |
| 11854 | chroma_side, |
| 11855 | chroma_side, |
| 11856 | h_filter, |
| 11857 | v_filter, |
| 11858 | &mut inter1_u, |
| 11859 | ); |
| 11860 | // `av1_init_warp_params` bails when the PLANE's block is |
| 11861 | // narrower/shorter than 8 (`block_width < 8`), so a 4x4 chroma |
| 11862 | // block of an 8x8 luma block stays translational. |
| 11863 | if let Some(wp) = &warp1 { |
| 11864 | if chroma_side >= 8 { |
| 11865 | crate::warp::warp_affine_compound( |
| 11866 | wp, &pu1.data, pu1.true_width as i32, pu1.true_height as i32, |
| 11867 | pu1.width as i32, &mut inter1_u, cpx as i32, cpy as i32, |
| 11868 | chroma_side as i32, chroma_side as i32, chroma_side as i32, 1, 1, |
| 11869 | ); |
| 11870 | } |
| 11871 | } |
| 11872 | let mut pred_u = vec![0u16; chroma_side * chroma_side]; |
| 11873 | if let Some(mask_y) = mask_y { |
| 11874 | mc::blend_masked_compound( |
| 11875 | &inter0_u, |
| 11876 | &inter1_u, |
| 11877 | mask_y, |
| 11878 | side, |
| 11879 | chroma_side, |
| 11880 | chroma_side, |
| 11881 | true, |
| 11882 | &mut pred_u, |
| 11883 | ); |
| 11884 | } else { |
| 11885 | mc::combine_compound(&inter0_u, &inter1_u, fwd_offset, bck_offset, &mut pred_u); |
| 11886 | } |
| 11887 | |
| 11888 | let mut inter0_v = vec![0i32; chroma_side * chroma_side]; |
| 11889 | mc::predict_compound_intermediate( |
| 11890 | &pv0.data, |
| 11891 | pv0.width, |
| 11892 | pv0.true_width, |
| 11893 | pv0.true_height, |
| 11894 | mv_to_q4(cpx, mv0.1, false), |
| 11895 | mv_to_q4(cpy, mv0.0, false), |
| 11896 | scale0, |
| 11897 | chroma_side, |
| 11898 | chroma_side, |
| 11899 | h_filter, |
| 11900 | v_filter, |
| 11901 | &mut inter0_v, |
| 11902 | ); |
| 11903 | // `av1_init_warp_params` bails when the PLANE's block is |
| 11904 | // narrower/shorter than 8 (`block_width < 8`), so a 4x4 chroma |
| 11905 | // block of an 8x8 luma block stays translational. |
| 11906 | if let Some(wp) = &warp0 { |
| 11907 | if chroma_side >= 8 { |
| 11908 | crate::warp::warp_affine_compound( |
| 11909 | wp, &pv0.data, pv0.true_width as i32, pv0.true_height as i32, |
| 11910 | pv0.width as i32, &mut inter0_v, cpx as i32, cpy as i32, |
| 11911 | chroma_side as i32, chroma_side as i32, chroma_side as i32, 1, 1, |
| 11912 | ); |
| 11913 | } |
| 11914 | } |
| 11915 | let mut inter1_v = vec![0i32; chroma_side * chroma_side]; |
| 11916 | mc::predict_compound_intermediate( |
| 11917 | &pv1.data, |
| 11918 | pv1.width, |
| 11919 | pv1.true_width, |
| 11920 | pv1.true_height, |
| 11921 | mv_to_q4(cpx, mv1.1, false), |
| 11922 | mv_to_q4(cpy, mv1.0, false), |
| 11923 | scale1, |
| 11924 | chroma_side, |
| 11925 | chroma_side, |
| 11926 | h_filter, |
| 11927 | v_filter, |
| 11928 | &mut inter1_v, |
| 11929 | ); |
| 11930 | // `av1_init_warp_params` bails when the PLANE's block is |
| 11931 | // narrower/shorter than 8 (`block_width < 8`), so a 4x4 chroma |
| 11932 | // block of an 8x8 luma block stays translational. |
| 11933 | if let Some(wp) = &warp1 { |
| 11934 | if chroma_side >= 8 { |
| 11935 | crate::warp::warp_affine_compound( |
| 11936 | wp, &pv1.data, pv1.true_width as i32, pv1.true_height as i32, |
| 11937 | pv1.width as i32, &mut inter1_v, cpx as i32, cpy as i32, |
| 11938 | chroma_side as i32, chroma_side as i32, chroma_side as i32, 1, 1, |
| 11939 | ); |
| 11940 | } |
| 11941 | } |
| 11942 | let mut pred_v = vec![0u16; chroma_side * chroma_side]; |
| 11943 | if let Some(mask_y) = mask_y { |
| 11944 | mc::blend_masked_compound( |
| 11945 | &inter0_v, |
| 11946 | &inter1_v, |
| 11947 | mask_y, |
| 11948 | side, |
| 11949 | chroma_side, |
| 11950 | chroma_side, |
| 11951 | true, |
| 11952 | &mut pred_v, |
| 11953 | ); |
| 11954 | } else { |
| 11955 | mc::combine_compound(&inter0_v, &inter1_v, fwd_offset, bck_offset, &mut pred_v); |
| 11956 | } |
| 11957 | |
| 11958 | vartx_leaves = read_block_tx_size( |
| 11959 | dec, |
| 11960 | cdfs, |
| 11961 | neighbours, |
| 11962 | at_mi, |
| 11963 | side, |
| 11964 | (mi_cols as usize, mi_rows as usize), |
| 11965 | true, |
| 11966 | skip, |
| 11967 | )? |
| 11968 | .1; |
| 11969 | if skip { |
| 11970 | y.reconstruct_mc_rect( |
| 11971 | px, |
| 11972 | py, |
| 11973 | side, |
| 11974 | write_w, |
| 11975 | write_h, |
| 11976 | &pred_y, |
| 11977 | &vec![0i32; side * side], |
| 11978 | ); |
| 11979 | u.reconstruct_mc_rect( |
| 11980 | cpx, |
| 11981 | cpy, |
| 11982 | chroma_side, |
| 11983 | write_chroma_w, |
| 11984 | write_chroma_h, |
| 11985 | &pred_u, |
| 11986 | &vec![0i32; chroma_side * chroma_side], |
| 11987 | ); |
| 11988 | v.reconstruct_mc_rect( |
| 11989 | cpx, |
| 11990 | cpy, |
| 11991 | chroma_side, |
| 11992 | write_chroma_w, |
| 11993 | write_chroma_h, |
| 11994 | &pred_v, |
| 11995 | &vec![0i32; chroma_side * chroma_side], |
| 11996 | ); |
| 11997 | luma_grid = vec![0i32; side * side]; |
| 11998 | u_grid = vec![0i32; chroma_side * chroma_side]; |
| 11999 | v_grid = vec![0i32; chroma_side * chroma_side]; |
| 12000 | } else { |
| 12001 | let around = neighbours.around(at, side); |
| 12002 | let luma_tx_type; |
| 12003 | if let Some(leaves) = vartx_leaves.clone() { |
| 12004 | // spec 5.11.17's transform tree, leaf by leaf in the order |
| 12005 | // `read_var_tx_size` coded it: each unit reads its own |
| 12006 | // coefficients against the neighbour magnitudes the |
| 12007 | // earlier units already wrote, and reconstructs over its |
| 12008 | // own slice of the block's motion-compensated predictor. |
| 12009 | let reduced = REDUCED_TX_SET_INTER.with(std::cell::Cell::get); |
| 12010 | let mut first_tx_type = TxType::DctDct; |
| 12011 | for (idx, &(row, col, tx_px)) in leaves.iter().enumerate() { |
| 12012 | let tu_mi = (at_mi.0 + row, at_mi.1 + col); |
| 12013 | let (tu_px, tu_py) = (px + col * MI, py + row * MI); |
| 12014 | let mut tu_pred = Vec::with_capacity(tx_px * tx_px); |
| 12015 | for rr in 0..tx_px { |
| 12016 | let start = (row * MI + rr) * side + col * MI; |
| 12017 | tu_pred.extend_from_slice(&pred_y[start..start + tx_px]); |
| 12018 | } |
| 12019 | let scan = default_scan(tx_px); |
| 12020 | let tu_skip_ctx = neighbours.luma_skip_ctx(tu_mi, tx_px / MI); |
| 12021 | let (tu_grid, tu_tx_type) = read_inter_plane( |
| 12022 | dec, |
| 12023 | cdfs, |
| 12024 | inter_txbset_for(tx_px, reduced), |
| 12025 | &scan, |
| 12026 | 0, |
| 12027 | neighbours.around_mi(tu_mi, tx_px)[0], |
| 12028 | mode_for_tx, |
| 12029 | y, |
| 12030 | tu_px, |
| 12031 | tu_py, |
| 12032 | tx_px, |
| 12033 | base_q_idx, |
| 12034 | &tu_pred, |
| 12035 | None, |
| 12036 | Some(tu_skip_ctx), |
| 12037 | )?; |
| 12038 | if idx == 0 { |
| 12039 | first_tx_type = tu_tx_type; |
| 12040 | } |
| 12041 | neighbours.record_mi_luma(tu_mi, tx_px, &tu_grid); |
| 12042 | } |
| 12043 | // `av1_get_tx_type`'s chroma lookup scales the chroma |
| 12044 | // position back to luma, so an inter block's chroma |
| 12045 | // inherits the *first* (top-left) luma unit's coded type. |
| 12046 | luma_tx_type = first_tx_type; |
| 12047 | luma_grid = vec![0i32; side * side]; |
| 12048 | } else { |
| 12049 | (luma_grid, luma_tx_type) = read_inter_plane( |
| 12050 | dec, |
| 12051 | cdfs, |
| 12052 | luma_set_inter, |
| 12053 | scan_luma, |
| 12054 | 0, |
| 12055 | around[0], |
| 12056 | mode_for_tx, |
| 12057 | y, |
| 12058 | px, |
| 12059 | py, |
| 12060 | side, |
| 12061 | base_q_idx, |
| 12062 | &pred_y, |
| 12063 | None, |
| 12064 | None, |
| 12065 | )?; |
| 12066 | } |
| 12067 | u_grid = read_inter_plane( |
| 12068 | dec, |
| 12069 | cdfs, |
| 12070 | chroma_set, |
| 12071 | scan_chroma, |
| 12072 | 1, |
| 12073 | around[1], |
| 12074 | mode_for_tx, |
| 12075 | u, |
| 12076 | cpx, |
| 12077 | cpy, |
| 12078 | chroma_side, |
| 12079 | base_q_idx, |
| 12080 | &pred_u, |
| 12081 | Some(luma_tx_type), |
| 12082 | None, |
| 12083 | )? |
| 12084 | .0; |
| 12085 | v_grid = read_inter_plane( |
| 12086 | dec, |
| 12087 | cdfs, |
| 12088 | chroma_set, |
| 12089 | scan_chroma, |
| 12090 | 2, |
| 12091 | around[2], |
| 12092 | mode_for_tx, |
| 12093 | v, |
| 12094 | cpx, |
| 12095 | cpy, |
| 12096 | chroma_side, |
| 12097 | base_q_idx, |
| 12098 | &pred_v, |
| 12099 | Some(luma_tx_type), |
| 12100 | None, |
| 12101 | )? |
| 12102 | .0; |
| 12103 | } |
| 12104 | } else { |
| 12105 | let ref_frame = |
| 12106 | read_single_ref( |
| 12107 | dec, |
| 12108 | cdfs, |
| 12109 | neighbours.above_ref[cmi], |
| 12110 | neighbours.above_ref1[cmi], |
| 12111 | neighbours.left_ref[rmi], |
| 12112 | neighbours.left_ref1[rmi], |
| 12113 | ); |
| 12114 | ref_frame_for_lf = ref_frame; |
| 12115 | if std::env::var_os("EC_AV1_TELL").is_some() { |
| 12116 | let (mi_row, mi_col) = (r * SUB_MI as usize, c * SUB_MI as usize); |
| 12117 | eprintln!( |
| 12118 | "TELL mi_row={mi_row} mi_col={mi_col} label=post_ref_frame ref={ref_frame} tell={} range={}", |
| 12119 | dec.debug_bitpos(), dec.debug_state().0 |
| 12120 | ); |
| 12121 | } |
| 12122 | // round 4-9 (lane-av1golden..lane-av1golden7): GOLDEN_FRAME's own |
| 12123 | // stacked defects (lf_level ref/mode-delta forwarding, the |
| 12124 | // switchable_interp ref-frame ctx gate, and a film-grain synthesis |
| 12125 | // gap that looked like a GOLDEN MC bug by survivor bias -- see the |
| 12126 | // ledger) are all closed; GOLDEN_FRAME decodes live via `other_refs` |
| 12127 | // below same as every other non-`LAST_FRAME` reference. lane-av1refs |
| 12128 | // widens this from GOLDEN alone to every reference `read_single_ref` |
| 12129 | // can name. |
| 12130 | let (py_ref, pu_ref, pv_ref) = if ref_frame == LAST_FRAME { |
| 12131 | (ref_y, ref_u, ref_v) |
| 12132 | } else { |
| 12133 | match other_refs[ref_frame as usize] { |
| 12134 | Some((ry, ru, rv)) => (ry, ru, rv), |
| 12135 | None => { |
| 12136 | return Err(unsupported( |
| 12137 | "a reference frame selected with no picture at this frame's own \ |
| 12138 | ref_frame_idx slot for it", |
| 12139 | )); |
| 12140 | } |
| 12141 | } |
| 12142 | }; |
| 12143 | |
| 12144 | let (mi_row, mi_col) = (r * SUB_MI as usize, c * SUB_MI as usize); |
| 12145 | // lane-rect r2: the mv stack must be queried with the block's |
| 12146 | // REAL extent (libaom `av1_get_mv_refs` gets bw4/bh4 from |
| 12147 | // xd->width/xd->height, not a square guess) -- `write_w`/ |
| 12148 | // `write_h` already name the true HORZ/VERT/HORZ_B strip |
| 12149 | // footprint (r1's plumbing), so derive bw4/bh4 from those |
| 12150 | // instead of the old `side`-square (HORZ_B-only) heuristic. |
| 12151 | let bw4 = write_w / 4; |
| 12152 | let bh4 = write_h / 4; |
| 12153 | // spec 7.10.2.8: only reached when the frame header set |
| 12154 | // `use_ref_frame_mvs` (`tpl_frame` is `Some` then, `None` |
| 12155 | // otherwise) -- `cur_offset_0` is this query block's own resolved |
| 12156 | // `ref_frame`'s distance from the current frame (spec 7.9.3), |
| 12157 | // recomputed per block since different blocks pick different refs. |
| 12158 | let tpl = tpl_frame.map(|t| crate::mvstack::TplArgs { |
| 12159 | field: t.field, |
| 12160 | cur_offset_0: crate::motion_field::get_relative_dist( |
| 12161 | t.order_hint_bits, |
| 12162 | t.order_hint, |
| 12163 | t.ref_order_hints[(ref_frame - LAST_FRAME) as usize], |
| 12164 | ), |
| 12165 | allow_high_precision_mv, |
| 12166 | }); |
| 12167 | let gm_table = build_gm_mv_table( |
| 12168 | global_motion, |
| 12169 | mi_row, |
| 12170 | mi_col, |
| 12171 | bw4, |
| 12172 | bh4, |
| 12173 | allow_high_precision_mv, |
| 12174 | force_integer_mv, |
| 12175 | ); |
| 12176 | let stack = find_mv_stack_with_sign_bias( |
| 12177 | grid, |
| 12178 | mi_row, |
| 12179 | mi_col, |
| 12180 | bw4, |
| 12181 | bh4, |
| 12182 | ref_frame, |
| 12183 | mi_cols as usize, |
| 12184 | mi_rows as usize, |
| 12185 | sign_bias_table, |
| 12186 | &gm_table, |
| 12187 | tpl, |
| 12188 | ); |
| 12189 | let not_new = dec.symbol(&mut cdfs.new_mv[stack.new_mv_ctx]) == 1; |
| 12190 | let mut is_globalmv = false; |
| 12191 | let (mv, is_new_mv) = if !not_new { |
| 12192 | // NEWMV (spec 5.11.24's `read_drl_idx`, `RefMvIdx` starting at 0): |
| 12193 | // read at most two `drl_mode` bits, one per stack entry past the |
| 12194 | // first, stopping at the first `0`. The chosen index selects |
| 12195 | // which stack entry `read_mv`'s base predictor comes from |
| 12196 | // (spec 7.10.2.10 `assign_mv`'s `PredMv = RefStackMv[RefMvIdx]`) |
| 12197 | // -- entry 0 (`stack.pred_mv`) only when the loop never advances. |
| 12198 | let mut idx = 0usize; |
| 12199 | while idx < 2 && stack.entries.len() > idx + 1 { |
| 12200 | if dec.symbol(&mut cdfs.drl_mode[stack.drl_ctx[idx]]) == 0 { |
| 12201 | break; |
| 12202 | } |
| 12203 | idx += 1; |
| 12204 | } |
| 12205 | let base_mv = stack.entries.get(idx).map_or(stack.pred_mv, |e| e.mv); |
| 12206 | ( |
| 12207 | read_mv( |
| 12208 | dec, |
| 12209 | &mut cdfs.mv_comp, |
| 12210 | &mut cdfs.mv_joint, |
| 12211 | base_mv, |
| 12212 | allow_high_precision_mv, |
| 12213 | force_integer_mv, |
| 12214 | ), |
| 12215 | true, |
| 12216 | ) |
| 12217 | } else { |
| 12218 | let not_zero = dec.symbol(&mut cdfs.zero_mv[stack.zero_mv_ctx]) == 1; |
| 12219 | is_globalmv = !not_zero; |
| 12220 | let mv = if !not_zero { |
| 12221 | // GLOBALMV: `gm_get_motion_vector` (spec 7.10.2.1), already |
| 12222 | // computed at this block's own position in `gm_table`. |
| 12223 | gm_table[(ref_frame - LAST_FRAME) as usize] |
| 12224 | } else { |
| 12225 | let nearest = dec.symbol(&mut cdfs.ref_mv[stack.ref_mv_ctx]) == 0; |
| 12226 | if nearest { |
| 12227 | stack.nearest_mv |
| 12228 | } else { |
| 12229 | // NEARMV (spec 5.11.24's `read_drl_idx`, `RefMvIdx` starting |
| 12230 | // at 1): read at most two more `drl_mode` bits, one per stack |
| 12231 | // entry past the second, stopping at the first `0`. |
| 12232 | // `drl_ctx[idx]` is the context between `entries[idx]` and |
| 12233 | // `entries[idx + 1]` (`MvStack::drl_ctx`'s own doc), which is |
| 12234 | // exactly the pair this index is choosing between. |
| 12235 | let mut idx = 1usize; |
| 12236 | while idx < 3 && stack.entries.len() > idx + 1 { |
| 12237 | if dec.symbol(&mut cdfs.drl_mode[stack.drl_ctx[idx]]) == 0 { |
| 12238 | break; |
| 12239 | } |
| 12240 | idx += 1; |
| 12241 | } |
| 12242 | stack.entries.get(idx).map_or(stack.near_mv, |e| e.mv) |
| 12243 | } |
| 12244 | }; |
| 12245 | (mv, false) |
| 12246 | }; |
| 12247 | // lane-gm r2: the two libaom predicates single-ref GLOBALMV |
| 12248 | // blocks need -- `is_global_mv_block` (blockd.h:421-429, mode |
| 12249 | // GLOBAL* AND model > TRANSLATION AND min(bw,bh) >= 8px, gating |
| 12250 | // `motion_mode_eligible`/`MiInfo::is_global_mv0` below) and |
| 12251 | // `is_nontrans_global_motion` (reconinter.h:420-425, model != |
| 12252 | // TRANSLATION -- IDENTITY counts here -- gating the interp |
| 12253 | // filter suppress below). Do NOT unify these. |
| 12254 | let gm_model = global_motion[(ref_frame - LAST_FRAME) as usize].model; |
| 12255 | let min_bw_bh4 = bw4.min(bh4); |
| 12256 | let is_global_mv_block = is_globalmv && gm_model as u8 > 1 && min_bw_bh4 >= 2; |
| 12257 | let gm_nontrans = |
| 12258 | is_globalmv && gm_model != ec_av1_syntax::WarpModel::Translation && min_bw_bh4 >= 2; |
| 12259 | if std::env::var_os("EC_AV1_TELL").is_some() { |
| 12260 | eprintln!( |
| 12261 | "TELL mi_row={mi_row} mi_col={mi_col} label=post_assign_mv tell={} range={}", |
| 12262 | dec.debug_bitpos(), dec.debug_state().0 |
| 12263 | ); |
| 12264 | } |
| 12265 | // lane-sb128 r4: `interintra` (spec 5.11.24; libaom |
| 12266 | // `decodemv.c` read_inter_block_mode_info ~1490-1510), placed |
| 12267 | // right after `assign_mv` and before `read_motion_mode` below -- |
| 12268 | // libaom's own sequential order. `is_interintra_allowed_bsize` |
| 12269 | // (`BLOCK_8X8..=BLOCK_32X32`) is the only real gate on this |
| 12270 | // single-ref path: `is_interintra_allowed_ref` always holds here |
| 12271 | // (single ref, `ref_frame[1]` not yet `INTRA_FRAME`) and |
| 12272 | // `is_interintra_allowed_mode` always holds (every mode this |
| 12273 | // branch produces -- NEARESTMV/NEARMV/GLOBALMV/NEWMV -- is |
| 12274 | // `SINGLE_INTER_MODE_START..END` by construction). |
| 12275 | // `size_group_lookup`: BLOCK_16X16 -> 2, BLOCK_32X32 -> 3. |
| 12276 | let mut interintra_mode: Option<u8> = None; |
| 12277 | let mut wedge_mask: Option<(&'static [u8], usize)> = None; |
| 12278 | if enable_interintra_compound && !skip_mode && (side == 16 || side == 32) { |
| 12279 | let bsize_group = if side == 16 { 2 } else { 3 }; |
| 12280 | let interintra = dec.symbol(&mut cdfs.interintra[bsize_group]) == 1; |
| 12281 | if interintra { |
| 12282 | // lane-interintra r1 (decodemv.c 1540-1555): interintra_mode, |
| 12283 | // then -- gated on block size ALONE, never the |
| 12284 | // `enable_interintra_wedge` seq bit -- the wedge flag |
| 12285 | // (`av1_is_wedge_used(bsize)` holds for 16x16/32x32). |
| 12286 | let ii = dec.symbol(&mut cdfs.interintra_mode[bsize_group]) as u8; |
| 12287 | let wedge_bsize = if side == 16 { 6 } else { 9 }; |
| 12288 | let wedge = dec.symbol(&mut cdfs.wedge_interintra[wedge_bsize]) == 1; |
| 12289 | if wedge { |
| 12290 | // lane-wii r2 (spec 5.11.25): `wedge_index` is an |
| 12291 | // ADAPTING CDF symbol over the same `wedge_bsize` row; |
| 12292 | // NO sign symbol follows -- libaom fixes |
| 12293 | // INTERINTRA_WEDGE_SIGN 0 (blockd.h). |
| 12294 | let wedge_index = dec.symbol(&mut cdfs.wedge_idx[wedge_bsize]); |
| 12295 | WII_HITS.with(|c| c.set(c.get() + 1)); |
| 12296 | wedge_mask = Some(( |
| 12297 | crate::wedge::wedge_masks() |
| 12298 | .codebook(side) |
| 12299 | .mask(0, wedge_index as usize), |
| 12300 | side, |
| 12301 | )); |
| 12302 | } else { |
| 12303 | INTERINTRA_HITS.with(|c| c.set(c.get() + 1)); |
| 12304 | } |
| 12305 | interintra_mode = Some(ii); |
| 12306 | } |
| 12307 | } |
| 12308 | if std::env::var_os("EC_AV1_TELL").is_some() { |
| 12309 | eprintln!( |
| 12310 | "TELL mi_row={mi_row} mi_col={mi_col} label=post_interintra tell={} range={}", |
| 12311 | dec.debug_bitpos(), dec.debug_state().0 |
| 12312 | ); |
| 12313 | } |
| 12314 | // lane-motionmode round 1: `read_motion_mode` (spec 5.11.24; |
| 12315 | // libaom `decodemv.c` read_inter_block_mode_info, ~1520-1528), |
| 12316 | // placed right after MV assignment and before the switchable |
| 12317 | // interp filter read below (`read_mb_interp_filter`, decodemv.c |
| 12318 | // ~1600) -- libaom's own sequential order. `motion_mode_allowed` |
| 12319 | // (`reconinter.h`) reduces, for every block this single-ref path |
| 12320 | // reaches, to "does an inter neighbour border the above row or |
| 12321 | // left column": `is_motion_variation_allowed_bsize` always holds |
| 12322 | // (`side` is never below 16px on this path), `ref_frame[1] != |
| 12323 | // INTRA_FRAME` always holds (no interintra reader here), |
| 12324 | // `is_motion_variation_allowed_compound` always holds (this is |
| 12325 | // the single-ref branch), and `is_global_mv_block` never |
| 12326 | // triggers (this decoder's global motion is always `IDENTITY`, |
| 12327 | // so its warp `type` is never `> TRANSLATION`). A stream with |
| 12328 | // `allow_warped_motion=1` is refused right at an eligible block |
| 12329 | // rather than guessed at: `av1_findSamples`/`num_proj_ref` is |
| 12330 | // not ported, so this decoder cannot tell a real |
| 12331 | // `WARPED_CAUSAL`-eligible block (3-symbol `motion_mode_cdf`) |
| 12332 | // from an `OBMC_CAUSAL`-only one (2-symbol `obmc_cdf`) whenever |
| 12333 | // the header allows warp -- reading the wrong alphabet desyncs |
| 12334 | // the tile. |
| 12335 | // An interintra block has `ref_frame[1] == INTRA_FRAME`, which |
| 12336 | // fails libaom's `is_motion_variation_allowed_compound`: the |
| 12337 | // motion_mode symbol is NOT read (SIMPLE_TRANSLATION implied). |
| 12338 | let motion_mode_eligible = switchable_motion_mode |
| 12339 | && !skip_mode |
| 12340 | && interintra_mode.is_none() |
| 12341 | && (!overlappable_above(grid, mi_row, mi_col, bw4, mi_cols as usize, 1).is_empty() |
| 12342 | || !overlappable_left(grid, mi_row, mi_col, bh4, mi_rows as usize, 1) |
| 12343 | .is_empty()) |
| 12344 | // libaom `motion_mode_allowed`: a GLOBALMV block whose model |
| 12345 | // is non-IDENTITY-non-TRANSLATION under free (non-integer) |
| 12346 | // mv reads no motion_mode/obmc symbol at all -- implicit |
| 12347 | // SIMPLE_TRANSLATION. |
| 12348 | && !(is_global_mv_block && !force_integer_mv); |
| 12349 | let mut obmc_selected = false; |
| 12350 | // lane-warp round 2: `Some` when motion_mode == WARPED_CAUSAL |
| 12351 | // *and* the local warp estimate is valid (`!wm_params.invalid`, |
| 12352 | // `av1_find_projection`) -- an invalid estimate falls back to |
| 12353 | // this block's own translational mv, same as any other block |
| 12354 | // (spec `allow_warp`/`reconinter.c` -- global warp is never the |
| 12355 | // fallback here since this decoder's global motion is always |
| 12356 | // IDENTITY). |
| 12357 | let mut warp_params: Option<crate::warp::WarpParams> = None; |
| 12358 | // Tracks the SYMBOL value, not projection validity: libaom's |
| 12359 | // `av1_is_interp_needed` suppresses the interp-filter read for |
| 12360 | // `motion_mode == WARPED_CAUSAL` even when the projection later |
| 12361 | // falls back to translation. Its third suppressor, |
| 12362 | // `is_nontrans_global_motion`, matches our unconditional |
| 12363 | // `is_globalmv` only because this decoder codes global motion as |
| 12364 | // IDENTITY (non-TRANSLATION) and never reads switchable filters |
| 12365 | // below 8x8 -- porting TRANSLATION global motion must add the |
| 12366 | // wmtype check here. |
| 12367 | let mut warped_selected = false; |
| 12368 | if motion_mode_eligible { |
| 12369 | // `default_obmc_cdf`'s own index: square bsize 8/16/32/64 -> |
| 12370 | // 0/1/2/3; lane-rect r2: a rect strip reads its OWN bsize row |
| 12371 | // (libaom `motion_mode_cdf[mbmi->bsize]`: BLOCK_16X32 -> 4, |
| 12372 | // BLOCK_32X16 -> 5 in our packed table) -- rect-flake-1's |
| 12373 | // strip motion_mode read diverged on the square row. |
| 12374 | let bsize_idx = if write_w == write_h { |
| 12375 | (write_w.trailing_zeros() - 3) as usize |
| 12376 | } else if write_w == 16 { |
| 12377 | 4 |
| 12378 | } else { |
| 12379 | 5 |
| 12380 | }; |
| 12381 | // `motion_mode_allowed` reads the 3-symbol `motion_mode_cdf` |
| 12382 | // instead of the 2-symbol `obmc_cdf` exactly when |
| 12383 | // `num_proj_ref >= 1` under `allow_warped_motion`. |
| 12384 | // lane-scaledref r2 + lane-fimv r1: libaom |
| 12385 | // `motion_mode_allowed` (`blockd.h:1484`) requires BOTH |
| 12386 | // `!av1_is_scaled(block_ref_scale_factors[0])` and |
| 12387 | // `!xd->cur_frame_force_integer_mv` for WARPED_CAUSAL -- |
| 12388 | // under a scaled reference (superres) OR on a frame whose |
| 12389 | // header set `force_integer_mv` (screen content) the block |
| 12390 | // reads the 2-symbol `obmc_cdf`, never the 3-symbol |
| 12391 | // `motion_mode_cdf`. Reading the wrong alphabet here narrows |
| 12392 | // the arithmetic coder by the wrong amount and predicts the |
| 12393 | // rest of the tile off a diverged state: silently wrong |
| 12394 | // pixels, not a desync error. `force_integer_mv` is the |
| 12395 | // per-frame value (spec 5.9.2: coded per frame only when |
| 12396 | // `seq_force_integer_mv == SELECT_INTEGER_MV`, threaded from |
| 12397 | // `FrameHeader::force_integer_mv`). |
| 12398 | // libaom `av1_is_scaled` is `x_scale_fp != REF_NO_SCALE || |
| 12399 | // y_scale_fp != REF_NO_SCALE`; the y half is unreachable here |
| 12400 | // because a reference whose height differs from this frame's |
| 12401 | // true size is refused before any tile decodes ("a reference |
| 12402 | // picture whose height does not match this frame's own true |
| 12403 | // size", ~14681). Whoever lifts that refusal adds the y term. |
| 12404 | let ref_is_scaled = |
| 12405 | mc::scale_factor(py_ref.width, frame_width) != mc::REF_NO_SCALE; |
| 12406 | let proj_ok = num_proj_ref( |
| 12407 | grid, |
| 12408 | mi_row, |
| 12409 | mi_col, |
| 12410 | bw4, |
| 12411 | bh4, |
| 12412 | mi_cols as usize, |
| 12413 | mi_rows as usize, |
| 12414 | ref_frame, |
| 12415 | ) >= 1; |
| 12416 | let warp_eligible = |
| 12417 | allow_warped_motion && !ref_is_scaled && !force_integer_mv && proj_ok; |
| 12418 | if allow_warped_motion && proj_ok && ref_is_scaled { |
| 12419 | SCALED_WARP_SUPPRESSED_HITS.with(|c| c.set(c.get() + 1)); |
| 12420 | } |
| 12421 | if allow_warped_motion && proj_ok && !ref_is_scaled && force_integer_mv { |
| 12422 | FIMV_ALPHABET_HITS.with(|c| c.set(c.get() + 1)); |
| 12423 | } |
| 12424 | if warp_eligible { |
| 12425 | let mode = dec.symbol(&mut cdfs.motion_mode[bsize_idx]); |
| 12426 | match mode { |
| 12427 | 0 => {} |
| 12428 | 1 => { |
| 12429 | obmc_selected = true; |
| 12430 | OBMC_HITS.with(|c| c.set(c.get() + 1)); |
| 12431 | } |
| 12432 | _ => { |
| 12433 | warped_selected = true; |
| 12434 | WARP_SELECTED_HITS.with(|c| c.set(c.get() + 1)); |
| 12435 | let mut samples = find_samples( |
| 12436 | grid, |
| 12437 | mi_row, |
| 12438 | mi_col, |
| 12439 | bw4, |
| 12440 | bh4, |
| 12441 | mi_cols as usize, |
| 12442 | mi_rows as usize, |
| 12443 | ref_frame, |
| 12444 | ); |
| 12445 | if std::env::var_os("EC_WARP_DEBUG").is_some() { |
| 12446 | eprintln!( |
| 12447 | "EC_WARP_DEBUG findSamples mi_row={mi_row} mi_col={mi_col} bsize={side} num_proj_ref={}", |
| 12448 | samples.len() |
| 12449 | ); |
| 12450 | for (i, s) in samples.iter().enumerate() { |
| 12451 | eprintln!("EC_WARP_DEBUG sample[{i}] pts={:?} pts_inref={:?}", s.pts1, s.pts2); |
| 12452 | } |
| 12453 | } |
| 12454 | if samples.len() > 1 { |
| 12455 | crate::warp::select_samples(mv, &mut samples, side as i32, side as i32); |
| 12456 | } |
| 12457 | warp_params = crate::warp::find_projection( |
| 12458 | &samples, |
| 12459 | // lane-rect r2: the block's TRUE dims -- a |
| 12460 | // 32x16 strip's model center is not a |
| 12461 | // 32x32's (aom av1_find_projection bw/bh). |
| 12462 | write_w as i32, |
| 12463 | write_h as i32, |
| 12464 | mv.1, |
| 12465 | mv.0, |
| 12466 | mi_row as i32, |
| 12467 | mi_col as i32, |
| 12468 | ); |
| 12469 | if std::env::var_os("EC_WARP_DEBUG").is_some() { |
| 12470 | eprintln!( |
| 12471 | "EC_WARP_DEBUG projection mi_row={mi_row} mi_col={mi_col} num_proj_ref(final)={} mv=({},{}) params={:?}", |
| 12472 | samples.len(), mv.0, mv.1, warp_params |
| 12473 | ); |
| 12474 | for (i, s) in samples.iter().enumerate() { |
| 12475 | eprintln!("EC_WARP_DEBUG sample_used[{i}] pts={:?} pts_inref={:?}", s.pts1, s.pts2); |
| 12476 | } |
| 12477 | } |
| 12478 | } |
| 12479 | } |
| 12480 | } else { |
| 12481 | obmc_selected = dec.symbol(&mut cdfs.obmc[bsize_idx]) == 1; |
| 12482 | if obmc_selected { |
| 12483 | OBMC_HITS.with(|c| c.set(c.get() + 1)); |
| 12484 | } |
| 12485 | } |
| 12486 | } |
| 12487 | // lane-gm r4: `allow_warp`'s `global_warp_allowed` branch |
| 12488 | // (`reconinter.c:33-55`), gated INDEPENDENTLY of `motion_mode` -- |
| 12489 | // libaom's `av1_init_warp_params` runs for every inter |
| 12490 | // predictor build, not just a `WARPED_CAUSAL`-selected one. Only |
| 12491 | // reachable when `warp_params` is still `None` here: a block |
| 12492 | // that read (and got) local `WARPED_CAUSAL` above already has |
| 12493 | // `local_warp_allowed && !wm_params.invalid`, which `allow_warp` |
| 12494 | // checks FIRST and short-circuits on -- the global branch is an |
| 12495 | // `else if`, never layered on top. `is_global_mv_block` already |
| 12496 | // encodes `allow_warp`'s size bound (`min(bw4,bh4) >= 2`, i.e. |
| 12497 | // >=8px both dims, matching `av1_init_warp_params`'s |
| 12498 | // `block_height < 8 || block_width < 8` early return) and, via |
| 12499 | // `motion_mode_eligible`'s own suppression above, the |
| 12500 | // `force_integer_mv` case never reads a motion_mode symbol at |
| 12501 | // all -- checked again here directly since this branch fires |
| 12502 | // independent of `motion_mode_eligible`. |
| 12503 | let gm_ref = &global_motion[(ref_frame - LAST_FRAME) as usize]; |
| 12504 | if warp_params.is_none() && is_global_mv_block && !force_integer_mv && !gm_ref.invalid { |
| 12505 | warp_params = crate::warp::global_warp_params(gm_ref.params); |
| 12506 | } |
| 12507 | if std::env::var_os("EC_AV1_TELL").is_some() { |
| 12508 | eprintln!( |
| 12509 | "TELL mi_row={mi_row} mi_col={mi_col} label=post_motion_mode eligible={} tell={} range={}", |
| 12510 | motion_mode_eligible as u8, dec.debug_bitpos(), dec.debug_state().0 |
| 12511 | ); |
| 12512 | } |
| 12513 | // spec `get_ref_filter_type`: a neighbour's own filter only feeds |
| 12514 | // this block's switchable_interp context when that neighbour coded |
| 12515 | // the SAME reference frame this block is about to read -- a |
| 12516 | // different-ref neighbour reads as "no neighbour" ([3, 3], the |
| 12517 | // sentinel `Neighbours::new` already seeds unset slots with), |
| 12518 | // exactly like an intra neighbour. Without this gate, a GOLDEN_FRAME |
| 12519 | // block next to a LAST_FRAME one inherits that neighbour's filter |
| 12520 | // choice into its own context, corrupting the symbol it reads. |
| 12521 | // lane-av1idx r2: also match on the neighbour's SECOND reference |
| 12522 | // (`above_ref1`/`left_ref1`, real for a compound neighbour) -- |
| 12523 | // see the 16x16 leaf's own comment above for the spec citation. |
| 12524 | let above_filter_ctx = if neighbours.above_ref[cmi] == ref_frame |
| 12525 | || neighbours.above_ref1[cmi] == Some(ref_frame) |
| 12526 | { |
| 12527 | neighbours.above_filter[cmi] |
| 12528 | } else { |
| 12529 | [3, 3] |
| 12530 | }; |
| 12531 | let left_filter_ctx = if neighbours.left_ref[rmi] == ref_frame |
| 12532 | || neighbours.left_ref1[rmi] == Some(ref_frame) |
| 12533 | { |
| 12534 | neighbours.left_filter[rmi] |
| 12535 | } else { |
| 12536 | [3, 3] |
| 12537 | }; |
| 12538 | let (h_filter, v_filter, resolved_filter) = resolve_interp_filter( |
| 12539 | dec, |
| 12540 | cdfs, |
| 12541 | interp_fixed, |
| 12542 | enable_dual_filter, |
| 12543 | gm_nontrans || warped_selected, |
| 12544 | above_filter_ctx, |
| 12545 | left_filter_ctx, |
| 12546 | false, |
| 12547 | ); |
| 12548 | block_filter = resolved_filter; |
| 12549 | globalmv_for_lf = is_globalmv; |
| 12550 | if std::env::var_os("EC_AV1_TELL").is_some() { |
| 12551 | eprintln!( |
| 12552 | "TELL mi_row={mi_row} mi_col={mi_col} label=post_interp_filter tell={} range={}", |
| 12553 | dec.debug_bitpos(), dec.debug_state().0 |
| 12554 | ); |
| 12555 | } |
| 12556 | if std::env::var_os("EC_AV1_TRACE").is_some() { |
| 12557 | eprintln!( |
| 12558 | "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={}", |
| 12559 | 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() |
| 12560 | ); |
| 12561 | } |
| 12562 | // lane-rect r2: see the compound path's matching comment above -- |
| 12563 | // `grid` must be stamped with the block's true bh4/bw4 span, not |
| 12564 | // a square `bw4`x`bw4` guess. |
| 12565 | for dr in 0..bh4 { |
| 12566 | for dc in 0..bw4 { |
| 12567 | grid.set( |
| 12568 | mi_row + dr, |
| 12569 | mi_col + dc, |
| 12570 | MiInfo { |
| 12571 | is_inter: true, |
| 12572 | ref_frame, |
| 12573 | // An interintra block records ref_frame[1] == |
| 12574 | // INTRA_FRAME (0): warp-sample gathering |
| 12575 | // (libaom av1_findSamples, mvref_common.c:1155) |
| 12576 | // requires ref_frame[1] == NONE_FRAME, so such a |
| 12577 | // neighbour must not donate samples or count in |
| 12578 | // num_proj_ref. |
| 12579 | ref_frame1: interintra_mode.map(|_| 0), |
| 12580 | mv1: None, |
| 12581 | mv, |
| 12582 | is_new_mv, |
| 12583 | size: bw4, |
| 12584 | size_h: bh4, |
| 12585 | is_global_mv0: is_global_mv_block, |
| 12586 | is_global_mv1: false, |
| 12587 | }, |
| 12588 | ); |
| 12589 | } |
| 12590 | } |
| 12591 | mode_for_tx = 0; |
| 12592 | uv_predict_mode = DC_PRED; |
| 12593 | |
| 12594 | // lane-superres r9: spec 7.11.3.3's x_scale_fp comes from LUMA |
| 12595 | // widths only (r8's derivation) and applies unchanged to the |
| 12596 | // chroma calls below (their x_q4 is already in the chroma |
| 12597 | // plane's own pixel units). `mc::predict_scaled` has no warp/OBMC |
| 12598 | // /interintra counterpart -- those three combinations are refused |
| 12599 | // by name instead of silently sampling a scaled reference wrong |
| 12600 | // (warp_params/obmc_selected/interintra_mode are all resolved by |
| 12601 | // this point, every symbol for this block already read). |
| 12602 | let luma_scale = mc::scale_factor(py_ref.width, frame_width); |
| 12603 | if luma_scale != mc::REF_NO_SCALE { |
| 12604 | // lane-scaledref r1: libaom `allow_warp` |
| 12605 | // (av1/common/reconinter.c:41) suppresses BOTH local and |
| 12606 | // global warp under a scaled reference and predicts |
| 12607 | // translationally instead -- that fallback is implemented |
| 12608 | // below, but the one real aomenc stream this round found |
| 12609 | // that reaches it (`--enable-warped-motion=1`, |
| 12610 | // `--superres-denominator=16`, seed 47) still mismatches |
| 12611 | // ffmpeg at frame 2 luma, so the case stays refused by name |
| 12612 | // until it has a green gate rather than shipping wrong |
| 12613 | // pixels behind a lifted refusal. |
| 12614 | if warp_params.is_some() { |
| 12615 | SCALED_WARP_FALLBACK_HITS.with(|c| c.set(c.get() + 1)); |
| 12616 | return Err(unsupported( |
| 12617 | "warp prediction with a scaled reference (superres, unimplemented)", |
| 12618 | )); |
| 12619 | } |
| 12620 | if obmc_selected { |
| 12621 | SCALED_OBMC_HITS.with(|c| c.set(c.get() + 1)); |
| 12622 | } |
| 12623 | if interintra_mode.is_some() { |
| 12624 | SCALED_INTERINTRA_HITS.with(|c| c.set(c.get() + 1)); |
| 12625 | } |
| 12626 | } |
| 12627 | |
| 12628 | let mut pred_y = vec![0u16; side * side]; |
| 12629 | let mut pred_u = vec![0u16; chroma_side * chroma_side]; |
| 12630 | let mut pred_v = vec![0u16; chroma_side * chroma_side]; |
| 12631 | if luma_scale == mc::REF_NO_SCALE { |
| 12632 | mc::predict_with_filters( |
| 12633 | &py_ref.data, |
| 12634 | py_ref.width, |
| 12635 | py_ref.true_width, |
| 12636 | py_ref.true_height, |
| 12637 | mv_to_q4(px, mv.1, true), |
| 12638 | mv_to_q4(py, mv.0, true), |
| 12639 | side, |
| 12640 | side, |
| 12641 | h_filter, |
| 12642 | v_filter, |
| 12643 | &mut pred_y, |
| 12644 | ); |
| 12645 | mc::predict_with_filters( |
| 12646 | &pu_ref.data, |
| 12647 | pu_ref.width, |
| 12648 | pu_ref.true_width, |
| 12649 | pu_ref.true_height, |
| 12650 | mv_to_q4(cpx, mv.1, false), |
| 12651 | mv_to_q4(cpy, mv.0, false), |
| 12652 | chroma_side, |
| 12653 | chroma_side, |
| 12654 | h_filter, |
| 12655 | v_filter, |
| 12656 | &mut pred_u, |
| 12657 | ); |
| 12658 | mc::predict_with_filters( |
| 12659 | &pv_ref.data, |
| 12660 | pv_ref.width, |
| 12661 | pv_ref.true_width, |
| 12662 | pv_ref.true_height, |
| 12663 | mv_to_q4(cpx, mv.1, false), |
| 12664 | mv_to_q4(cpy, mv.0, false), |
| 12665 | chroma_side, |
| 12666 | chroma_side, |
| 12667 | h_filter, |
| 12668 | v_filter, |
| 12669 | &mut pred_v, |
| 12670 | ); |
| 12671 | } else { |
| 12672 | mc::predict_scaled( |
| 12673 | &py_ref.data, |
| 12674 | py_ref.width, |
| 12675 | py_ref.true_width, |
| 12676 | py_ref.true_height, |
| 12677 | mv_to_q4(px, mv.1, true), |
| 12678 | mv_to_q4(py, mv.0, true), |
| 12679 | luma_scale, |
| 12680 | side, |
| 12681 | side, |
| 12682 | h_filter, |
| 12683 | v_filter, |
| 12684 | &mut pred_y, |
| 12685 | ); |
| 12686 | mc::predict_scaled( |
| 12687 | &pu_ref.data, |
| 12688 | pu_ref.width, |
| 12689 | pu_ref.true_width, |
| 12690 | pu_ref.true_height, |
| 12691 | mv_to_q4(cpx, mv.1, false), |
| 12692 | mv_to_q4(cpy, mv.0, false), |
| 12693 | luma_scale, |
| 12694 | chroma_side, |
| 12695 | chroma_side, |
| 12696 | h_filter, |
| 12697 | v_filter, |
| 12698 | &mut pred_u, |
| 12699 | ); |
| 12700 | mc::predict_scaled( |
| 12701 | &pv_ref.data, |
| 12702 | pv_ref.width, |
| 12703 | pv_ref.true_width, |
| 12704 | pv_ref.true_height, |
| 12705 | mv_to_q4(cpx, mv.1, false), |
| 12706 | mv_to_q4(cpy, mv.0, false), |
| 12707 | luma_scale, |
| 12708 | chroma_side, |
| 12709 | chroma_side, |
| 12710 | h_filter, |
| 12711 | v_filter, |
| 12712 | &mut pred_v, |
| 12713 | ); |
| 12714 | } |
| 12715 | |
| 12716 | // lane-scaledref r1: libaom `allow_warp` (av1/common/reconinter.c:41) |
| 12717 | // opens with `if (av1_is_scaled(sf)) return 0;` -- BOTH local |
| 12718 | // (`wm_params`) and global warp fall back to the translational |
| 12719 | // prediction above when the reference is scaled, even though the |
| 12720 | // motion_mode symbol was still read and warp_params still |
| 12721 | // resolved. Suppress the warp, keep every symbol read. |
| 12722 | if let Some(params) = warp_params.as_ref().filter(|_| luma_scale == mc::REF_NO_SCALE) { |
| 12723 | crate::warp::warp_affine( |
| 12724 | params, &py_ref.data, py_ref.true_width as i32, py_ref.true_height as i32, |
| 12725 | py_ref.width as i32, &mut pred_y, px as i32, py as i32, side as i32, |
| 12726 | side as i32, side as i32, 0, 0, |
| 12727 | ); |
| 12728 | crate::warp::warp_affine( |
| 12729 | params, &pu_ref.data, pu_ref.true_width as i32, pu_ref.true_height as i32, |
| 12730 | pu_ref.width as i32, &mut pred_u, cpx as i32, cpy as i32, chroma_side as i32, |
| 12731 | chroma_side as i32, chroma_side as i32, 1, 1, |
| 12732 | ); |
| 12733 | crate::warp::warp_affine( |
| 12734 | params, &pv_ref.data, pv_ref.true_width as i32, pv_ref.true_height as i32, |
| 12735 | pv_ref.width as i32, &mut pred_v, cpx as i32, cpy as i32, chroma_side as i32, |
| 12736 | chroma_side as i32, chroma_side as i32, 1, 1, |
| 12737 | ); |
| 12738 | } |
| 12739 | |
| 12740 | if obmc_selected { |
| 12741 | obmc_blend( |
| 12742 | grid, |
| 12743 | neighbours, |
| 12744 | mi_row, |
| 12745 | mi_col, |
| 12746 | bw4, |
| 12747 | bh4, |
| 12748 | mi_rows as usize, |
| 12749 | mi_cols as usize, |
| 12750 | side, |
| 12751 | write_w, |
| 12752 | write_h, |
| 12753 | chroma_side, |
| 12754 | px, |
| 12755 | py, |
| 12756 | cpx, |
| 12757 | cpy, |
| 12758 | ref_y, |
| 12759 | ref_u, |
| 12760 | ref_v, |
| 12761 | other_refs, |
| 12762 | interp_fixed, |
| 12763 | frame_width, |
| 12764 | &mut pred_y, |
| 12765 | &mut pred_u, |
| 12766 | &mut pred_v, |
| 12767 | )?; |
| 12768 | } |
| 12769 | |
| 12770 | // Mutually exclusive with warp/OBMC (motion_mode is not read for |
| 12771 | // an interintra block): blend the intra predictor over the MC |
| 12772 | // result, all planes (reconinter.c av1_build_interintra_predictor |
| 12773 | // plane loop). |
| 12774 | if let Some(ii) = interintra_mode { |
| 12775 | interintra_blend(y, px, py, side, ii, wedge_mask, &mut pred_y); |
| 12776 | interintra_blend(u, cpx, cpy, chroma_side, ii, wedge_mask, &mut pred_u); |
| 12777 | interintra_blend(v, cpx, cpy, chroma_side, ii, wedge_mask, &mut pred_v); |
| 12778 | } |
| 12779 | |
| 12780 | vartx_leaves = read_block_tx_size( |
| 12781 | dec, |
| 12782 | cdfs, |
| 12783 | neighbours, |
| 12784 | at_mi, |
| 12785 | side, |
| 12786 | (mi_cols as usize, mi_rows as usize), |
| 12787 | true, |
| 12788 | skip, |
| 12789 | )? |
| 12790 | .1; |
| 12791 | if skip { |
| 12792 | y.reconstruct_mc_rect( |
| 12793 | px, |
| 12794 | py, |
| 12795 | side, |
| 12796 | write_w, |
| 12797 | write_h, |
| 12798 | &pred_y, |
| 12799 | &vec![0i32; side * side], |
| 12800 | ); |
| 12801 | u.reconstruct_mc_rect( |
| 12802 | cpx, |
| 12803 | cpy, |
| 12804 | chroma_side, |
| 12805 | write_chroma_w, |
| 12806 | write_chroma_h, |
| 12807 | &pred_u, |
| 12808 | &vec![0i32; chroma_side * chroma_side], |
| 12809 | ); |
| 12810 | v.reconstruct_mc_rect( |
| 12811 | cpx, |
| 12812 | cpy, |
| 12813 | chroma_side, |
| 12814 | write_chroma_w, |
| 12815 | write_chroma_h, |
| 12816 | &pred_v, |
| 12817 | &vec![0i32; chroma_side * chroma_side], |
| 12818 | ); |
| 12819 | luma_grid = vec![0i32; side * side]; |
| 12820 | u_grid = vec![0i32; chroma_side * chroma_side]; |
| 12821 | v_grid = vec![0i32; chroma_side * chroma_side]; |
| 12822 | } else { |
| 12823 | let around = neighbours.around(at, side); |
| 12824 | let luma_tx_type; |
| 12825 | if let Some(leaves) = vartx_leaves.clone() { |
| 12826 | // spec 5.11.17's transform tree, leaf by leaf in the order |
| 12827 | // `read_var_tx_size` coded it: each unit reads its own |
| 12828 | // coefficients against the neighbour magnitudes the |
| 12829 | // earlier units already wrote, and reconstructs over its |
| 12830 | // own slice of the block's motion-compensated predictor. |
| 12831 | let reduced = REDUCED_TX_SET_INTER.with(std::cell::Cell::get); |
| 12832 | let mut first_tx_type = TxType::DctDct; |
| 12833 | for (idx, &(row, col, tx_px)) in leaves.iter().enumerate() { |
| 12834 | let tu_mi = (at_mi.0 + row, at_mi.1 + col); |
| 12835 | let (tu_px, tu_py) = (px + col * MI, py + row * MI); |
| 12836 | let mut tu_pred = Vec::with_capacity(tx_px * tx_px); |
| 12837 | for rr in 0..tx_px { |
| 12838 | let start = (row * MI + rr) * side + col * MI; |
| 12839 | tu_pred.extend_from_slice(&pred_y[start..start + tx_px]); |
| 12840 | } |
| 12841 | let scan = default_scan(tx_px); |
| 12842 | let tu_skip_ctx = neighbours.luma_skip_ctx(tu_mi, tx_px / MI); |
| 12843 | let (tu_grid, tu_tx_type) = read_inter_plane( |
| 12844 | dec, |
| 12845 | cdfs, |
| 12846 | inter_txbset_for(tx_px, reduced), |
| 12847 | &scan, |
| 12848 | 0, |
| 12849 | neighbours.around_mi(tu_mi, tx_px)[0], |
| 12850 | mode_for_tx, |
| 12851 | y, |
| 12852 | tu_px, |
| 12853 | tu_py, |
| 12854 | tx_px, |
| 12855 | base_q_idx, |
| 12856 | &tu_pred, |
| 12857 | None, |
| 12858 | Some(tu_skip_ctx), |
| 12859 | )?; |
| 12860 | if idx == 0 { |
| 12861 | first_tx_type = tu_tx_type; |
| 12862 | } |
| 12863 | neighbours.record_mi_luma(tu_mi, tx_px, &tu_grid); |
| 12864 | } |
| 12865 | // `av1_get_tx_type`'s chroma lookup scales the chroma |
| 12866 | // position back to luma, so an inter block's chroma |
| 12867 | // inherits the *first* (top-left) luma unit's coded type. |
| 12868 | luma_tx_type = first_tx_type; |
| 12869 | luma_grid = vec![0i32; side * side]; |
| 12870 | } else { |
| 12871 | (luma_grid, luma_tx_type) = read_inter_plane( |
| 12872 | dec, |
| 12873 | cdfs, |
| 12874 | luma_set_inter, |
| 12875 | scan_luma, |
| 12876 | 0, |
| 12877 | around[0], |
| 12878 | mode_for_tx, |
| 12879 | y, |
| 12880 | px, |
| 12881 | py, |
| 12882 | side, |
| 12883 | base_q_idx, |
| 12884 | &pred_y, |
| 12885 | None, |
| 12886 | None, |
| 12887 | )?; |
| 12888 | } |
| 12889 | u_grid = read_inter_plane( |
| 12890 | dec, |
| 12891 | cdfs, |
| 12892 | chroma_set, |
| 12893 | scan_chroma, |
| 12894 | 1, |
| 12895 | around[1], |
| 12896 | mode_for_tx, |
| 12897 | u, |
| 12898 | cpx, |
| 12899 | cpy, |
| 12900 | chroma_side, |
| 12901 | base_q_idx, |
| 12902 | &pred_u, |
| 12903 | Some(luma_tx_type), |
| 12904 | None, |
| 12905 | )? |
| 12906 | .0; |
| 12907 | v_grid = read_inter_plane( |
| 12908 | dec, |
| 12909 | cdfs, |
| 12910 | chroma_set, |
| 12911 | scan_chroma, |
| 12912 | 2, |
| 12913 | around[2], |
| 12914 | mode_for_tx, |
| 12915 | v, |
| 12916 | cpx, |
| 12917 | cpy, |
| 12918 | chroma_side, |
| 12919 | base_q_idx, |
| 12920 | &pred_v, |
| 12921 | Some(luma_tx_type), |
| 12922 | None, |
| 12923 | )? |
| 12924 | .0; |
| 12925 | } |
| 12926 | } |
| 12927 | } else { |
| 12928 | // lane-partitions r1: intra prediction (`PlaneBuf::reconstruct`) has |
| 12929 | // no rectangular counterpart -- an intra-coded HORZ/VERT strip needs |
| 12930 | // its own true-width/height edge/predict math, unlike the inter |
| 12931 | // skip path above (which only needed a clipped write of an |
| 12932 | // already-square-predicted buffer). Named refusal instead of a |
| 12933 | // silently wrong square-shaped intra prediction. |
| 12934 | if write_w != side || write_h != side { |
| 12935 | return Err(unsupported( |
| 12936 | "an intra-coded HORZ/VERT strip needs rectangular intra prediction \ |
| 12937 | this decoder does not code yet", |
| 12938 | )); |
| 12939 | } |
| 12940 | let mode = dec.symbol(&mut cdfs.y_mode[size_group]); |
| 12941 | if mode >= 13 { |
| 12942 | return Err(unsupported( |
| 12943 | "an intra mode this decoder does not code (round 2)", |
| 12944 | )); |
| 12945 | } |
| 12946 | if (V_PRED..=D67_PRED).contains(&mode) { |
| 12947 | let angle = dec.symbol(&mut cdfs.angle_delta[mode - V_PRED]); |
| 12948 | if angle != ANGLE_DELTA_ZERO { |
| 12949 | return Err(unsupported( |
| 12950 | "a nonzero angle delta (this encoder never writes one)", |
| 12951 | )); |
| 12952 | } |
| 12953 | } |
| 12954 | let uv_mode = dec.symbol(&mut cdfs.uv_mode_cfl[mode]); |
| 12955 | if (9..=12).contains(&uv_mode) { |
| 12956 | SMOOTH_UV_HITS.with(|c| c.set(c.get() + 1)); |
| 12957 | } |
| 12958 | let alpha = if uv_mode == DC_PRED { |
| 12959 | None |
| 12960 | } else if uv_mode == UV_CFL_PRED { |
| 12961 | Some(read_cfl_alphas(dec, cdfs)) |
| 12962 | } else { |
| 12963 | None |
| 12964 | }; |
| 12965 | // `get_uv_mode` (spec 9.3): `UV_CFL_PRED` predicts as `DC_PRED` for |
| 12966 | // the angle-delta question, same as [`read_intra_mode`]'s own copy |
| 12967 | // -- `uv_mode` (13) already falls outside `V_PRED..=D67_PRED` so the |
| 12968 | // `else` branch below is exact for it either way. |
| 12969 | let angle_delta_uv = if (V_PRED..=D67_PRED).contains(&uv_mode) { |
| 12970 | DIRECTIONAL_UV_HITS.with(|c| c.set(c.get() + 1)); |
| 12971 | read_angle_delta(dec, &mut cdfs.angle_delta[uv_mode - V_PRED]) |
| 12972 | } else { |
| 12973 | 0 |
| 12974 | }; |
| 12975 | uv_predict_mode = if uv_mode == UV_CFL_PRED { |
| 12976 | DC_PRED |
| 12977 | } else { |
| 12978 | uv_mode |
| 12979 | }; |
| 12980 | // lane-chroma r4: same chroma edge-filter-strength neighbour check |
| 12981 | // as [`read_intra_mode`]/[`read_intra_mode_rect`] -- the CHROMA |
| 12982 | // neighbour's own `uv_mode`, not the luma one. |
| 12983 | let smooth_neighbor_uv = |
| 12984 | is_smooth_mode(neighbours.above_uv_mode[c]) || is_smooth_mode(neighbours.left_uv_mode[r]); |
| 12985 | // `read_palette_mode_info` (decodemv.c:567, called from |
| 12986 | // `read_intra_block_mode_info` right after `xd->cfl.store_y`, same |
| 12987 | // gating and same corner-cut as `read_intra_mode`'s own copy above |
| 12988 | // -- `palette_mode_ctx`/`palette_uv_mode_ctx` hardcoded 0, provably |
| 12989 | // safe since a nonzero neighbour `palette_size` would already have |
| 12990 | // refused the decode that produced it. |
| 12991 | if allow_screen_content_tools |
| 12992 | && let Some(bsize_ctx) = palette_bsize_ctx(side) |
| 12993 | { |
| 12994 | if mode == DC_PRED && dec.symbol(&mut cdfs.palette_y_mode[bsize_ctx][0]) != 0 { |
| 12995 | return Err(unsupported( |
| 12996 | "a block that actually uses a palette (Y) -- reconstruction is out of scope", |
| 12997 | )); |
| 12998 | } |
| 12999 | if uv_mode == DC_PRED && dec.symbol(&mut cdfs.palette_uv_mode[0]) != 0 { |
| 13000 | return Err(unsupported( |
| 13001 | "a block that actually uses a palette (UV) -- reconstruction is out of scope", |
| 13002 | )); |
| 13003 | } |
| 13004 | } |
| 13005 | mode_for_tx = mode; |
| 13006 | block_filter = [3, 3]; |
| 13007 | ref_frame_for_lf = 0; |
| 13008 | globalmv_for_lf = false; |
| 13009 | let (mi_row, mi_col) = (r * SUB_MI as usize, c * SUB_MI as usize); |
| 13010 | for dr in 0..side / 4 { |
| 13011 | for dc in 0..side / 4 { |
| 13012 | grid.set( |
| 13013 | mi_row + dr, |
| 13014 | mi_col + dc, |
| 13015 | MiInfo { |
| 13016 | is_inter: false, |
| 13017 | ref_frame: -1, |
| 13018 | ref_frame1: None, |
| 13019 | mv1: None, |
| 13020 | mv: (0, 0), |
| 13021 | is_new_mv: false, |
| 13022 | size: side / 4, |
| 13023 | size_h: side / 4, |
| 13024 | is_global_mv0: false, |
| 13025 | is_global_mv1: false, |
| 13026 | }, |
| 13027 | ); |
| 13028 | } |
| 13029 | } |
| 13030 | |
| 13031 | read_block_tx_size( |
| 13032 | dec, |
| 13033 | cdfs, |
| 13034 | neighbours, |
| 13035 | at_mi, |
| 13036 | side, |
| 13037 | (mi_cols as usize, mi_rows as usize), |
| 13038 | false, |
| 13039 | skip, |
| 13040 | )?; |
| 13041 | let reach = Reach::of(side, px, py, y.width, y.height); |
| 13042 | if skip { |
| 13043 | y.reconstruct( |
| 13044 | px, |
| 13045 | py, |
| 13046 | side, |
| 13047 | mode, |
| 13048 | 0, |
| 13049 | reach, |
| 13050 | &vec![0i32; side * side], |
| 13051 | None, |
| 13052 | None, |
| 13053 | false, |
| 13054 | ); |
| 13055 | let ac = alpha.map(|_| cfl_ac_q3(y, px, py, side)); |
| 13056 | u.reconstruct( |
| 13057 | cpx, |
| 13058 | cpy, |
| 13059 | chroma_side, |
| 13060 | uv_predict_mode, |
| 13061 | angle_delta_uv, |
| 13062 | reach, |
| 13063 | &vec![0i32; chroma_side * chroma_side], |
| 13064 | alpha.zip(ac.as_deref()).map(|((au, _), ac)| (au, ac)), |
| 13065 | None, |
| 13066 | smooth_neighbor_uv, |
| 13067 | ); |
| 13068 | v.reconstruct( |
| 13069 | cpx, |
| 13070 | cpy, |
| 13071 | chroma_side, |
| 13072 | uv_predict_mode, |
| 13073 | angle_delta_uv, |
| 13074 | reach, |
| 13075 | &vec![0i32; chroma_side * chroma_side], |
| 13076 | alpha.zip(ac.as_deref()).map(|((_, av), ac)| (av, ac)), |
| 13077 | None, |
| 13078 | smooth_neighbor_uv, |
| 13079 | ); |
| 13080 | luma_grid = vec![0i32; side * side]; |
| 13081 | u_grid = vec![0i32; chroma_side * chroma_side]; |
| 13082 | v_grid = vec![0i32; chroma_side * chroma_side]; |
| 13083 | } else { |
| 13084 | let around = neighbours.around(at, side); |
| 13085 | luma_grid = read_plane( |
| 13086 | dec, |
| 13087 | cdfs, |
| 13088 | luma_set_intra, |
| 13089 | scan_luma, |
| 13090 | 0, |
| 13091 | around[0], |
| 13092 | mode, |
| 13093 | mode, |
| 13094 | 0, |
| 13095 | reach, |
| 13096 | y, |
| 13097 | px, |
| 13098 | py, |
| 13099 | side, |
| 13100 | luma_tx, |
| 13101 | base_q_idx, |
| 13102 | None, |
| 13103 | None, |
| 13104 | None, |
| 13105 | false, |
| 13106 | )?; |
| 13107 | let ac = alpha.map(|_| cfl_ac_q3(y, px, py, side)); |
| 13108 | u_grid = read_plane( |
| 13109 | dec, |
| 13110 | cdfs, |
| 13111 | chroma_set, |
| 13112 | scan_chroma, |
| 13113 | 1, |
| 13114 | around[1], |
| 13115 | mode, |
| 13116 | uv_predict_mode, |
| 13117 | angle_delta_uv, |
| 13118 | reach, |
| 13119 | u, |
| 13120 | cpx, |
| 13121 | cpy, |
| 13122 | chroma_side, |
| 13123 | chroma_tx, |
| 13124 | base_q_idx, |
| 13125 | alpha.zip(ac.as_deref()).map(|((au, _), ac)| (au, ac)), |
| 13126 | None, |
| 13127 | None, |
| 13128 | smooth_neighbor_uv, |
| 13129 | )?; |
| 13130 | v_grid = read_plane( |
| 13131 | dec, |
| 13132 | cdfs, |
| 13133 | chroma_set, |
| 13134 | scan_chroma, |
| 13135 | 2, |
| 13136 | around[2], |
| 13137 | mode, |
| 13138 | uv_predict_mode, |
| 13139 | angle_delta_uv, |
| 13140 | reach, |
| 13141 | v, |
| 13142 | cpx, |
| 13143 | cpy, |
| 13144 | chroma_side, |
| 13145 | chroma_tx, |
| 13146 | base_q_idx, |
| 13147 | alpha.zip(ac.as_deref()).map(|((_, av), ac)| (av, ac)), |
| 13148 | None, |
| 13149 | None, |
| 13150 | smooth_neighbor_uv, |
| 13151 | )?; |
| 13152 | } |
| 13153 | } |
| 13154 | // lane-comppin r9: end-of-block checkpoint -- everything between |
| 13155 | // post_assign_mv and here (interintra, motion_mode, interp_filter, then |
| 13156 | // every plane's coeffs) is unproven; a range mismatch here vs aomdec's |
| 13157 | // equivalent post-content point (added at the matching call site) |
| 13158 | // narrows the ladder to this block's own tail instead of the next |
| 13159 | // block's partition read. |
| 13160 | if std::env::var_os("EC_AV1_TELL").is_some() { |
| 13161 | eprintln!( |
| 13162 | "TELL mi_row={} mi_col={} label=block_end tell={} range={}", |
| 13163 | r * SUB_MI as usize, c * SUB_MI as usize, dec.debug_bitpos(), dec.debug_state().0 |
| 13164 | ); |
| 13165 | } |
| 13166 | if vartx_leaves.is_some() { |
| 13167 | // Plane 0 is already correct per transform unit |
| 13168 | // ([`Neighbours::record_mi_luma`] above); this writes everything else |
| 13169 | // [`Neighbours::record_rect`] would (a var-tx block is always square, |
| 13170 | // so `write_w == write_h == side`). |
| 13171 | neighbours.record_split_luma(at, side, mode_for_tx, uv_predict_mode, [&u_grid, &v_grid]); |
| 13172 | } else { |
| 13173 | neighbours.record_rect( |
| 13174 | at, |
| 13175 | write_w, |
| 13176 | write_h, |
| 13177 | mode_for_tx, |
| 13178 | uv_predict_mode, |
| 13179 | &[luma_grid, u_grid, v_grid], |
| 13180 | ); |
| 13181 | } |
| 13182 | neighbours.record_inter_rect( |
| 13183 | at, |
| 13184 | write_w, |
| 13185 | write_h, |
| 13186 | skip, |
| 13187 | is_inter, |
| 13188 | ref_frame_for_lf, |
| 13189 | block_filter, |
| 13190 | skip_mode, |
| 13191 | ); |
| 13192 | if let Some((ref1, group_idx, idx)) = compound_ctx { |
| 13193 | neighbours.record_compound_ctx_rect(at, write_w, write_h, ref1, group_idx, idx); |
| 13194 | } |
| 13195 | neighbours.fill_skip_grid_rect( |
| 13196 | (r * (SUB / MI), c * (SUB / MI)), |
| 13197 | write_w / MI, |
| 13198 | write_h / MI, |
| 13199 | skip, |
| 13200 | ); |
| 13201 | neighbours.fill_lf_grid_rect( |
| 13202 | (r * (SUB / MI), c * (SUB / MI)), |
| 13203 | write_w / MI, |
| 13204 | write_h / MI, |
| 13205 | // lane-rect r2: the strip's true TX_32X16/TX_16X32 dims -- the |
| 13206 | // single-scalar corner-cut deblocked the strip seam with the wrong |
| 13207 | // edge length (1-px chroma drift on rect-flake-1 f17+). |
| 13208 | write_w as u8, |
| 13209 | write_h as u8, |
| 13210 | // ref_grid's packed convention (lf_level): sign carries GLOBALMV, |
| 13211 | // whose mode_lf_lut row is 0, not the 1 every other inter mode gets. |
| 13212 | if globalmv_for_lf { |
| 13213 | -ref_frame_for_lf |
| 13214 | } else { |
| 13215 | ref_frame_for_lf |
| 13216 | }, |
| 13217 | ); |
| 13218 | // lane-txselect: a var-tx block's deblock edges follow its own transform |
| 13219 | // units, not the block. Chroma still reads `tx_h_grid / 2` (the block's |
| 13220 | // uv transform is not split) -- a known corner-cut of this round. |
| 13221 | if let Some(leaves) = &vartx_leaves { |
| 13222 | for &(row, col, tx_px) in leaves { |
| 13223 | neighbours.fill_lf_grid_rect( |
| 13224 | (at_mi.0 + row, at_mi.1 + col), |
| 13225 | tx_px / MI, |
| 13226 | tx_px / MI, |
| 13227 | tx_px as u8, |
| 13228 | tx_px as u8, |
| 13229 | if globalmv_for_lf { |
| 13230 | -ref_frame_for_lf |
| 13231 | } else { |
| 13232 | ref_frame_for_lf |
| 13233 | }, |
| 13234 | ); |
| 13235 | } |
| 13236 | } |
| 13237 | Ok(()) |
| 13238 | } |
| 13239 | |
| 13240 | /// Decodes one 8x8 leaf of a straddling 16x16 inter-frame block |
| 13241 | /// (lane-av1inter8), mirroring [`crate::tile::write_inter_frame_leaf8`]'s |
| 13242 | /// exact symbol order: its own skip flag, intra/inter choice and (when |
| 13243 | /// inter) `NEARESTMV`/`NEWMV` chain against this leaf's own 2x2-mi mv-stack |
| 13244 | /// window, or (when intra) `Y_MODE`, then its own luma and two chroma |
| 13245 | /// transform blocks through [`TxbSet::Luma8Inter`]/[`TxbSet::Chroma4`], |
| 13246 | /// same as [`decode_leaf8`]'s coefficient layer but with |
| 13247 | /// motion-compensated prediction instead of intra prediction when inter. |
| 13248 | /// `outer_at` (in [`SUB`]-grid units) is the enclosing 16x16 slot's |
| 13249 | /// skip/intra-inter context, overridden by `prev_leaf` exactly as |
| 13250 | /// [`decode_leaf8`] overrides mode context: `Neighbours`' above/left arrays |
| 13251 | /// only resolve to [`SUB`] granularity. Hands back this leaf's own skip flag |
| 13252 | /// and intra/inter choice, for the next leaf or the caller's final |
| 13253 | /// write-back. |
| 13254 | #[allow(clippy::too_many_arguments)] |
| 13255 | fn decode_inter_block8( |
| 13256 | dec: &mut SymbolDecoder, |
| 13257 | cdfs: &mut Cdfs, |
| 13258 | neighbours: &mut Neighbours, |
| 13259 | grid: &mut MiGrid, |
| 13260 | mi_cols: u32, |
| 13261 | mi_rows: u32, |
| 13262 | outer_at: (usize, usize), |
| 13263 | leaf_mi: (usize, usize), |
| 13264 | y: &mut PlaneBuf, |
| 13265 | u: &mut PlaneBuf, |
| 13266 | v: &mut PlaneBuf, |
| 13267 | ref_y: &PlaneBuf, |
| 13268 | ref_u: &PlaneBuf, |
| 13269 | ref_v: &PlaneBuf, |
| 13270 | other_refs: &RefSlots, |
| 13271 | base_q_idx: u8, |
| 13272 | scan8: &[u16], |
| 13273 | scan4: &[u16], |
| 13274 | allow_high_precision_mv: bool, |
| 13275 | force_integer_mv: bool, |
| 13276 | // lane-av1comp: see [`decode_inter_block`]'s own doc -- this leaf path |
| 13277 | // only ever tracks a coarse `LAST_FRAME`-or-intra neighbour shape (no |
| 13278 | // per-leaf `above_ref`/`left_ref` array exists here), so `comp_mode`'s |
| 13279 | // context uses that same approximation. |
| 13280 | reference_select: bool, |
| 13281 | enable_masked_compound: bool, |
| 13282 | // lane-sb128 r4: see [`decode_inter_block`]'s own doc -- `BLOCK_8X8` is |
| 13283 | // always `is_interintra_allowed_bsize`-eligible, so this leaf's own gate |
| 13284 | // drops the `side` check entirely. |
| 13285 | enable_interintra_compound: bool, |
| 13286 | enable_jnt_comp: bool, |
| 13287 | order_hint_bits: u32, |
| 13288 | order_hint: u32, |
| 13289 | ref_order_hints: [u32; 7], |
| 13290 | // lane-av1comp round 14: see [`decode_inter_block`]'s own doc -- this |
| 13291 | // leaf has no per-leaf `above_skip_mode`/`left_skip_mode` array either, |
| 13292 | // so its `skip_mode` context reuses the same [`SUB`]-grid |
| 13293 | // `above_skip_mode[cmi]`/`left_skip_mode[rmi]` the enclosing 16x16 slot |
| 13294 | // tracks (mirroring `above_skip`/`left_skip`'s own leaf approximation |
| 13295 | // just above). |
| 13296 | skip_mode_present: bool, |
| 13297 | skip_mode_frame: [u8; 2], |
| 13298 | // lane-cdffwd2: this frame header's own `reduced_tx_set` bit -- see |
| 13299 | // [`decode_inter_frame_tile_with_cdfs`]'s own doc. |
| 13300 | reduced_tx_set: bool, |
| 13301 | // lane-motionmode round 3: this frame header's own `is_motion_mode_switchable`/ |
| 13302 | // `allow_warped_motion` bits (spec 5.11.24's `read_motion_mode`), plus |
| 13303 | // the fixed/switchable interp filter kind `obmc_blend`'s own neighbour |
| 13304 | // reads need (see [`decode_inter_block`]'s own doc) -- this leaf's own |
| 13305 | // prediction is always `Regular` (documented corner-cut, `mc::predict` |
| 13306 | // has no filter param), but an OBMC neighbour can be a real 16x16+ |
| 13307 | // block that DID resolve a switchable filter. |
| 13308 | interp_fixed: Option<mc::InterpFilterKind>, |
| 13309 | switchable_motion_mode: bool, |
| 13310 | allow_warped_motion: bool, |
| 13311 | // lane-av1comp: `Some((ref1, comp_group_idx, compound_idx))` only for a |
| 13312 | // real `COMPOUND_REFERENCE` leaf, for the caller's own end-of-16x16 |
| 13313 | // `record_compound_ctx` call -- mirrors [`decode_inter_block`]'s own |
| 13314 | // `compound_ctx` local, just handed back instead of applied here (this |
| 13315 | // fn's `neighbours` recording happens once for the whole straddling |
| 13316 | // 16x16 block, after every leaf). |
| 13317 | // lane-screen r2: see [`decode_inter_block`]'s own copy of this param -- |
| 13318 | // `BLOCK_8X8` (this leaf's fixed size) is always `av1_allow_palette`- |
| 13319 | // eligible (`bsize >= BLOCK_8X8`). |
| 13320 | allow_screen_content_tools: bool, |
| 13321 | // lane-inter8 r1: the leaf's mv stacks used to be built with neither the |
| 13322 | // frame's sign-bias table nor its global-motion mv table (`NO_SIGN_BIAS` |
| 13323 | // / `NO_GM_MV`), while [`decode_inter_block`] one level up passed both. |
| 13324 | // A neighbour coded GLOBALMV contributes the gm mv, so dropping the table |
| 13325 | // changes the candidate list -- and with it `new_mv_ctx`/`ref_mv_ctx`, |
| 13326 | // `drl_ctx` and the number of symbols read. Measured: an 8x8 compound |
| 13327 | // NEAR_NEWMV leaf desynced against aomdec's EC_MODE ladder at exactly |
| 13328 | // this block (mi 4,14 of the mandelbrot 8x8-split gate). |
| 13329 | sign_bias_table: &SignBiasTable, |
| 13330 | global_motion: &[ec_av1_syntax::WarpParams; 7], |
| 13331 | // lane-inter8 r2: the frame's temporal mv field (spec 7.10.2.8), which |
| 13332 | // [`decode_inter_block`] one level up has always passed and this leaf |
| 13333 | // passed `None` for -- a `use_ref_frame_mvs` frame's stack then holds |
| 13334 | // fewer candidates here than in the reference decoder, which changes the |
| 13335 | // mode contexts AND the number of `drl_mode` bits read (class parsed |
| 13336 | // then discarded). |
| 13337 | tpl_frame: Option<&TplFrameArgs>, |
| 13338 | // lane-scaledref r1: this frame's own coded luma width (spec 7.11.3.3), |
| 13339 | // for the scaled-reference MC this leaf's own single-ref/compound/OBMC |
| 13340 | // predictions take when a stored reference was coded at another width |
| 13341 | // (`use_superres`) -- mirrors [`decode_inter_block`]'s own param. |
| 13342 | frame_width: usize, |
| 13343 | ) -> Result<(bool, bool, bool, Option<(i8, i8, u8, u8)>, (i8, Option<i8>))> { |
| 13344 | const LAST_FRAME: i8 = 1; |
| 13345 | let gm_table = build_gm_mv_table( |
| 13346 | global_motion, |
| 13347 | leaf_mi.0, |
| 13348 | leaf_mi.1, |
| 13349 | 2, |
| 13350 | 2, |
| 13351 | allow_high_precision_mv, |
| 13352 | force_integer_mv, |
| 13353 | ); |
| 13354 | /// `Y_MODE`'s size group (`common_data.h`'s `size_group_lookup[BLOCK_8X8]`). |
| 13355 | const SIZE_GROUP_8: usize = 1; |
| 13356 | |
| 13357 | let (px, py) = (leaf_mi.1 * MI, leaf_mi.0 * MI); |
| 13358 | let (cpx, cpy) = (px / 2, py / 2); |
| 13359 | // lane-txselect: whether this leaf's `TxMode::Select` var-tx tree took |
| 13360 | // the one split a `BLOCK_8X8` can take (`read_block_tx_size` below) -- |
| 13361 | // read back by the luma residual read and the tail's neighbour/loop-filter |
| 13362 | // bookkeeping. Always `false` outside a `TxMode::Select` inter frame. |
| 13363 | let mut split8 = false; |
| 13364 | const SIDE: usize = 8; |
| 13365 | const CHROMA_SIDE: usize = 4; |
| 13366 | |
| 13367 | let (r, c) = outer_at; |
| 13368 | let _ = (r, c); |
| 13369 | // lane-inter8 r2: this leaf's OWN mi cell, not the enclosing 16x16's -- |
| 13370 | // leaf (mi_r, mi_c + 2)'s left neighbour is leaf (mi_r, mi_c), and the |
| 13371 | // left column of a 16x16's second leaf ROW is the previous 16x16 block's |
| 13372 | // bottom leaf, neither of which a SUB-grid slot can name. |
| 13373 | let (rmi, cmi) = leaf_mi; |
| 13374 | // lane-inter8 r2: no sibling-leaf override any more -- each leaf stamps |
| 13375 | // its own 2x2-mi span into the bands right after it decodes (the caller's |
| 13376 | // `record_inter_rect_mi`), so a plain mi read already names the correct |
| 13377 | // neighbour, including one belonging to the previous 16x16 block. |
| 13378 | let above_skip = neighbours.above_skip[cmi]; |
| 13379 | let left_skip = neighbours.left_skip[rmi]; |
| 13380 | let above_inter = neighbours.above_inter[cmi]; |
| 13381 | let left_inter = neighbours.left_inter[rmi]; |
| 13382 | let above_ref0 = neighbours.above_ref[cmi]; |
| 13383 | let above_ref1 = neighbours.above_ref1[cmi]; |
| 13384 | let left_ref0 = neighbours.left_ref[rmi]; |
| 13385 | let left_ref1 = neighbours.left_ref1[rmi]; |
| 13386 | let skip_mode_ctx = |
| 13387 | usize::from(neighbours.above_skip_mode[cmi]) + usize::from(neighbours.left_skip_mode[rmi]); |
| 13388 | // Same two `inter_segment_id` positions as [`decode_inter_block`]; this |
| 13389 | // leaf is always `BLOCK_8X8`, i.e. 2x2 mi. |
| 13390 | inter_segment_id(dec, cdfs, leaf_mi.0, leaf_mi.1, 2, 2, false, true); |
| 13391 | let skip_mode = skip_mode_present && dec.symbol(&mut cdfs.skip_mode[skip_mode_ctx]) == 1; |
| 13392 | if skip_mode { |
| 13393 | SKIP_MODE_HITS.with(|c| c.set(c.get() + 1)); |
| 13394 | } |
| 13395 | |
| 13396 | let skip_ctx = usize::from(above_skip) + usize::from(left_skip); |
| 13397 | let skip = skip_mode || dec.symbol(&mut cdfs.skip[skip_ctx]) == 1; |
| 13398 | inter_segment_id(dec, cdfs, leaf_mi.0, leaf_mi.1, 2, 2, skip, false); |
| 13399 | maybe_read_cdef_idx(dec, leaf_mi.0, leaf_mi.1, skip); |
| 13400 | // Always a `BLOCK_8X8` leaf here, never the whole superblock. |
| 13401 | maybe_read_delta_q(dec, cdfs, leaf_mi.0, leaf_mi.1, false, skip); |
| 13402 | maybe_read_delta_lf(dec, cdfs, leaf_mi.0, leaf_mi.1, false, skip); |
| 13403 | |
| 13404 | let (has_above, has_left) = ( |
| 13405 | leaf_mi.0 > neighbours.tile_row0_mi, |
| 13406 | leaf_mi.1 > neighbours.tile_col0_mi, |
| 13407 | ); |
| 13408 | let ii_ctx = intra_inter_ctx(has_above, has_left, above_inter, left_inter); |
| 13409 | let is_inter = skip_mode || dec.symbol(&mut cdfs.intra_inter[ii_ctx]) == 1; |
| 13410 | // lane-inter8 r1: mirrors the oracle's `EC_MODE` (instrument rung 4, |
| 13411 | // `read_inter_block_mode_info`) so an 8x8 leaf's mode-info range ladder |
| 13412 | // diffs line for line against a real aomdec. |
| 13413 | if std::env::var_os("EC_TRACE_MODE").is_some() { |
| 13414 | eprintln!( |
| 13415 | "EC_MODE mi_row={} mi_col={} rng={}", |
| 13416 | leaf_mi.0, |
| 13417 | leaf_mi.1, |
| 13418 | dec.debug_state().0 |
| 13419 | ); |
| 13420 | } |
| 13421 | |
| 13422 | let mode_for_tx; |
| 13423 | let (luma_grid, u_grid, v_grid); |
| 13424 | let mut compound_ctx8: Option<(i8, i8, u8, u8)> = None; |
| 13425 | // lane-inter8 r1: this leaf's own resolved references, handed back so the |
| 13426 | // NEXT leaf of the same 16x16 can use them as its real above/left |
| 13427 | // neighbour (`Neighbours`' arrays only move once per 16x16 block). |
| 13428 | // `(0, None)` is intra. |
| 13429 | let mut leaf_refs: (i8, Option<i8>) = (0, None); |
| 13430 | if is_inter { |
| 13431 | if reference_select || skip_mode { |
| 13432 | // lane-inter8 r1: the REAL neighbour references, not a |
| 13433 | // `LAST_FRAME`-or-intra stand-in. `av1_get_reference_mode_context` |
| 13434 | // and `read_compound_ref_frames`' own contexts both ask whether a |
| 13435 | // neighbour is COMPOUND, so pretending every inter neighbour is |
| 13436 | // single-ref LAST picked the wrong `comp_mode` CDF row -- measured |
| 13437 | // as an 8x8 leaf reading single-ref where aomdec read compound |
| 13438 | // NEAR_NEWMV (mi 4,14). |
| 13439 | let above_nbr = has_above.then(|| NeighbourRef { |
| 13440 | is_inter: above_inter, |
| 13441 | ref0: if above_inter { above_ref0 } else { 0 }, |
| 13442 | ref1: above_ref1, |
| 13443 | uni: above_ref1.is_some_and(|r1| is_uni_comp_ref(above_ref0, r1)), |
| 13444 | }); |
| 13445 | let left_nbr = has_left.then(|| NeighbourRef { |
| 13446 | is_inter: left_inter, |
| 13447 | ref0: if left_inter { left_ref0 } else { 0 }, |
| 13448 | ref1: left_ref1, |
| 13449 | uni: left_ref1.is_some_and(|r1| is_uni_comp_ref(left_ref0, r1)), |
| 13450 | }); |
| 13451 | if skip_mode || read_comp_mode(dec, cdfs, above_nbr, left_nbr) { |
| 13452 | let (ref0, ref1) = if skip_mode { |
| 13453 | (skip_mode_frame[0] as i8, skip_mode_frame[1] as i8) |
| 13454 | } else { |
| 13455 | read_compound_ref_frames( |
| 13456 | dec, |
| 13457 | cdfs, |
| 13458 | above_nbr, |
| 13459 | left_nbr, |
| 13460 | if has_above && above_inter { |
| 13461 | LAST_FRAME |
| 13462 | } else { |
| 13463 | -1 |
| 13464 | }, |
| 13465 | if has_left && left_inter { |
| 13466 | LAST_FRAME |
| 13467 | } else { |
| 13468 | -1 |
| 13469 | }, |
| 13470 | ) |
| 13471 | }; |
| 13472 | leaf_refs = (ref0, Some(ref1)); |
| 13473 | let (mi_row, mi_col) = leaf_mi; |
| 13474 | // spec 7.10.2.8, mirroring [`decode_inter_block`]'s own |
| 13475 | // `comp_tpl` build (`use_ref_frame_mvs` frames only). |
| 13476 | let comp_tpl = tpl_frame.map(|t| crate::mvstack::CompoundTplArgs { |
| 13477 | field: t.field, |
| 13478 | cur_offset_0: crate::motion_field::get_relative_dist( |
| 13479 | t.order_hint_bits, |
| 13480 | t.order_hint, |
| 13481 | t.ref_order_hints[(ref0 - LAST_FRAME) as usize], |
| 13482 | ), |
| 13483 | cur_offset_1: crate::motion_field::get_relative_dist( |
| 13484 | t.order_hint_bits, |
| 13485 | t.order_hint, |
| 13486 | t.ref_order_hints[(ref1 - LAST_FRAME) as usize], |
| 13487 | ), |
| 13488 | allow_high_precision_mv, |
| 13489 | }); |
| 13490 | // lane-av1comp: this leaf path has neither a real sign-bias |
| 13491 | // table nor `tpl_frame` (see this function's own doc on the |
| 13492 | // coarse `LAST_FRAME`-or-intra neighbour approximation |
| 13493 | // already in force above), matching the plain `find_mv_stack` |
| 13494 | // (no bias, no tpl) the leaf's single-ref arm below already |
| 13495 | // uses. |
| 13496 | let comp_stack = crate::mvstack::find_mv_stack_compound( |
| 13497 | grid, |
| 13498 | mi_row, |
| 13499 | mi_col, |
| 13500 | 2, |
| 13501 | 2, |
| 13502 | (ref0, ref1), |
| 13503 | mi_cols as usize, |
| 13504 | mi_rows as usize, |
| 13505 | sign_bias_table, |
| 13506 | &gm_table, |
| 13507 | comp_tpl, |
| 13508 | ); |
| 13509 | let compound_mode = if skip_mode { |
| 13510 | 0 // NEAREST_NEARESTMV, forced -- no mode symbol read |
| 13511 | } else { |
| 13512 | read_inter_compound_mode( |
| 13513 | dec, |
| 13514 | cdfs, |
| 13515 | comp_stack.new_mv_ctx, |
| 13516 | comp_stack.ref_mv_ctx, |
| 13517 | ) |
| 13518 | }; |
| 13519 | if std::env::var_os("EC_TRACE_MODE").is_some() { |
| 13520 | eprintln!( |
| 13521 | "EC_LEAFMODE mi_row={} mi_col={} cmode={} stack={} newmv_ctx={} refmv_ctx={} rng={}", |
| 13522 | leaf_mi.0, |
| 13523 | leaf_mi.1, |
| 13524 | compound_mode + 17, |
| 13525 | comp_stack.entries.len(), |
| 13526 | comp_stack.new_mv_ctx, |
| 13527 | comp_stack.ref_mv_ctx, |
| 13528 | dec.debug_state().0 |
| 13529 | ); |
| 13530 | } |
| 13531 | let (mv0, mv1) = assign_compound_mv( |
| 13532 | dec, |
| 13533 | cdfs, |
| 13534 | &comp_stack, |
| 13535 | compound_mode, |
| 13536 | allow_high_precision_mv, |
| 13537 | force_integer_mv, |
| 13538 | // lane-inter8 r1: the real per-ref gm mvs, same as |
| 13539 | // [`decode_inter_block`]'s own compound arm. |
| 13540 | ( |
| 13541 | gm_table[(ref0 - LAST_FRAME) as usize], |
| 13542 | gm_table[(ref1 - LAST_FRAME) as usize], |
| 13543 | ), |
| 13544 | ); |
| 13545 | // lane-inter8 r3: byte-format-identical to the oracle's |
| 13546 | // rung-4 `EC_MODE_VAL`, so the leaf's mode+mv VALUE ladder |
| 13547 | // diffs line for line against a real aomdec. |
| 13548 | if std::env::var_os("EC_TRACE_MODE").is_some() { |
| 13549 | eprintln!( |
| 13550 | "EC_MODE_VAL mi_row={} mi_col={} mode={} ref0={} ref1={} mv0=({},{}) rng={}", |
| 13551 | leaf_mi.0, |
| 13552 | leaf_mi.1, |
| 13553 | compound_mode + 17, |
| 13554 | ref0, |
| 13555 | ref1, |
| 13556 | mv0.0, |
| 13557 | mv0.1, |
| 13558 | dec.debug_state().0 |
| 13559 | ); |
| 13560 | } |
| 13561 | |
| 13562 | // spec 5.11.25: same gating [`decode_inter_block`]'s own |
| 13563 | // compound arm uses, keyed off `outer_at`'s [`SUB`]-grid |
| 13564 | // slot -- the only granularity `Neighbours`' comp_group_idx/ |
| 13565 | // compound_idx arrays track. |
| 13566 | let group_ctx = get_comp_group_idx_context(neighbours, leaf_mi, SIDE); |
| 13567 | let comp_group_idx = if !skip_mode |
| 13568 | && enable_masked_compound |
| 13569 | && is_any_masked_compound_used_here(SIDE) |
| 13570 | { |
| 13571 | dec.symbol(&mut cdfs.comp_group_idx[group_ctx]) |
| 13572 | } else { |
| 13573 | 0 |
| 13574 | }; |
| 13575 | // lane-maskcomp r2 / lane-wedge r3: see the 16x16/32x32 |
| 13576 | // leaf's own comment. |
| 13577 | let mut diffwtd_mask_type: Option<u8> = None; |
| 13578 | let mut wedge_mask: Option<&'static [u8]> = None; |
| 13579 | if comp_group_idx == 1 { |
| 13580 | // lane-maskcomp r1: see the 16x16 leaf's own comment |
| 13581 | // above -- SIDE==8 here, `wedge_bsize` index 3 |
| 13582 | // (BLOCK_8X8, `wedge_types > 0`). |
| 13583 | let compound_type = dec.symbol(&mut cdfs.compound_type[3]); |
| 13584 | if compound_type == 0 { |
| 13585 | // COMPOUND_WEDGE: lane-wedge r3, see the 16x16/32x32 |
| 13586 | // leaf's own comment. |
| 13587 | let wedge_index = dec.symbol(&mut cdfs.wedge_idx[3]); |
| 13588 | let wedge_sign = dec.literal(1); |
| 13589 | WEDGE_HITS.with(|c| c.set(c.get() + 1)); |
| 13590 | wedge_mask = Some( |
| 13591 | crate::wedge::wedge_masks() |
| 13592 | .codebook(SIDE) |
| 13593 | .mask(wedge_sign as usize, wedge_index as usize), |
| 13594 | ); |
| 13595 | } else { |
| 13596 | let mask_type = dec.literal(1); |
| 13597 | diffwtd_mask_type = Some(mask_type as u8); |
| 13598 | } |
| 13599 | MASKED_COMPOUND_HITS.with(|c| c.set(c.get() + 1)); |
| 13600 | } |
| 13601 | // corner-cut (lane-av1comp r16/r17, lane-av1blend r1): same |
| 13602 | // reference-slot defect as the 16x16 leaf's own mask above |
| 13603 | // (r1: falsified as an MC/blend math bug, isolated to a DPB |
| 13604 | // slot/refresh_frame_flags issue around BWDREF_FRAME) -- see |
| 13605 | // that comment. |
| 13606 | // r3: same defect as the 16x16 leaf's own mask above -- see |
| 13607 | // that comment (compound_idx's decoded value, not the blend |
| 13608 | // math or reference-slot bookkeeping). r6/r7: same re-mask |
| 13609 | // as the 16x16 leaf above -- one real bug found and fixed |
| 13610 | // (mvstack.rs ref_ctx family), a second, still-unfound |
| 13611 | // compound_idx defect surfaces on the wide gate sweep. |
| 13612 | // lane-av1idx r1: same re-mask as the 16x16 leaf above -- |
| 13613 | // see that comment. |
| 13614 | let (fwd_offset, bck_offset, compound_idx) = if comp_group_idx == 0 |
| 13615 | && !skip_mode |
| 13616 | && enable_jnt_comp |
| 13617 | { |
| 13618 | let idx_ctx = get_comp_index_context( |
| 13619 | neighbours, |
| 13620 | leaf_mi, |
| 13621 | SIDE, |
| 13622 | order_hint_bits, |
| 13623 | order_hint, |
| 13624 | ref_order_hints[(ref0 - LAST_FRAME) as usize], |
| 13625 | ref_order_hints[(ref1 - LAST_FRAME) as usize], |
| 13626 | ); |
| 13627 | let idx = dec.symbol(&mut cdfs.compound_idx[idx_ctx]); |
| 13628 | if idx == 1 { |
| 13629 | (8, 8, 1u8) |
| 13630 | } else { |
| 13631 | let (fwd, bck) = crate::compound::dist_wtd_comp_weight_assign( |
| 13632 | order_hint_bits, |
| 13633 | order_hint, |
| 13634 | ref_order_hints[(ref0 - LAST_FRAME) as usize], |
| 13635 | ref_order_hints[(ref1 - LAST_FRAME) as usize], |
| 13636 | ); |
| 13637 | (fwd, bck, 0u8) |
| 13638 | } |
| 13639 | } else { |
| 13640 | (8, 8, 1u8) |
| 13641 | }; |
| 13642 | compound_ctx8 = Some((ref0, ref1, comp_group_idx as u8, compound_idx)); |
| 13643 | if std::env::var_os("EC_AV1_COMPIDX_DUMP").is_some() { |
| 13644 | eprintln!( |
| 13645 | "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={}", |
| 13646 | mv0.0, mv0.1, mv1.0, mv1.1, dec.debug_bitpos() |
| 13647 | ); |
| 13648 | } |
| 13649 | |
| 13650 | let (py0, pu0, pv0) = ref_planes(ref0, ref_y, ref_u, ref_v, other_refs)?; |
| 13651 | let (py1, pu1, pv1) = ref_planes(ref1, ref_y, ref_u, ref_v, other_refs)?; |
| 13652 | let scale0 = mc::scale_factor(py0.width, frame_width); |
| 13653 | let scale1 = mc::scale_factor(py1.width, frame_width); |
| 13654 | if scale0 != mc::REF_NO_SCALE || scale1 != mc::REF_NO_SCALE { |
| 13655 | SCALED_COMPOUND_HITS.with(|c| c.set(c.get() + 1)); |
| 13656 | SCALED_BLOCK8_HITS.with(|c| c.set(c.get() + 1)); |
| 13657 | } |
| 13658 | |
| 13659 | for dr in 0..2 { |
| 13660 | for dc in 0..2 { |
| 13661 | grid.set( |
| 13662 | mi_row + dr, |
| 13663 | mi_col + dc, |
| 13664 | MiInfo { |
| 13665 | is_inter: true, |
| 13666 | ref_frame: ref0, |
| 13667 | ref_frame1: Some(ref1), |
| 13668 | mv1: Some(mv1), |
| 13669 | mv: mv0, |
| 13670 | is_new_mv: matches!(compound_mode, 2 | 3 | 4 | 5 | 7), |
| 13671 | size: 2, |
| 13672 | size_h: 2, |
| 13673 | is_global_mv0: false, |
| 13674 | is_global_mv1: false, |
| 13675 | }, |
| 13676 | ); |
| 13677 | } |
| 13678 | } |
| 13679 | mode_for_tx = 0; |
| 13680 | |
| 13681 | // lane-av1comp corner-cut, matching this leaf's single-ref |
| 13682 | // arm below (`mc::predict`'s own fixed-Regular default): |
| 13683 | // `decode_inter_block8` never resolves a switchable filter, |
| 13684 | // so both compound taps use `Regular` too. |
| 13685 | use mc::InterpFilterKind::Regular; |
| 13686 | let mut inter0_y = vec![0i32; SIDE * SIDE]; |
| 13687 | mc::predict_compound_intermediate( |
| 13688 | &py0.data, |
| 13689 | py0.width, |
| 13690 | py0.true_width, |
| 13691 | py0.true_height, |
| 13692 | mv_to_q4(px, mv0.1, true), |
| 13693 | mv_to_q4(py, mv0.0, true), |
| 13694 | scale0, |
| 13695 | SIDE, |
| 13696 | SIDE, |
| 13697 | Regular, |
| 13698 | Regular, |
| 13699 | &mut inter0_y, |
| 13700 | ); |
| 13701 | let mut inter1_y = vec![0i32; SIDE * SIDE]; |
| 13702 | mc::predict_compound_intermediate( |
| 13703 | &py1.data, |
| 13704 | py1.width, |
| 13705 | py1.true_width, |
| 13706 | py1.true_height, |
| 13707 | mv_to_q4(px, mv1.1, true), |
| 13708 | mv_to_q4(py, mv1.0, true), |
| 13709 | scale1, |
| 13710 | SIDE, |
| 13711 | SIDE, |
| 13712 | Regular, |
| 13713 | Regular, |
| 13714 | &mut inter1_y, |
| 13715 | ); |
| 13716 | let mut pred_y = vec![0u16; SIDE * SIDE]; |
| 13717 | let mut diffwtd_mask_y = Vec::new(); |
| 13718 | let mask_y: Option<&[u8]> = if let Some(mask_type) = diffwtd_mask_type { |
| 13719 | diffwtd_mask_y = vec![0u8; SIDE * SIDE]; |
| 13720 | mc::diffwtd_mask(&inter0_y, &inter1_y, mask_type == 1, &mut diffwtd_mask_y); |
| 13721 | Some(diffwtd_mask_y.as_slice()) |
| 13722 | } else { |
| 13723 | wedge_mask |
| 13724 | }; |
| 13725 | if let Some(mask_y) = mask_y { |
| 13726 | mc::blend_masked_compound( |
| 13727 | &inter0_y, &inter1_y, mask_y, SIDE, SIDE, SIDE, false, &mut pred_y, |
| 13728 | ); |
| 13729 | } else { |
| 13730 | mc::combine_compound(&inter0_y, &inter1_y, fwd_offset, bck_offset, &mut pred_y); |
| 13731 | } |
| 13732 | |
| 13733 | let mut inter0_u = vec![0i32; CHROMA_SIDE * CHROMA_SIDE]; |
| 13734 | mc::predict_compound_intermediate( |
| 13735 | &pu0.data, |
| 13736 | pu0.width, |
| 13737 | pu0.true_width, |
| 13738 | pu0.true_height, |
| 13739 | mv_to_q4(cpx, mv0.1, false), |
| 13740 | mv_to_q4(cpy, mv0.0, false), |
| 13741 | scale0, |
| 13742 | CHROMA_SIDE, |
| 13743 | CHROMA_SIDE, |
| 13744 | Regular, |
| 13745 | Regular, |
| 13746 | &mut inter0_u, |
| 13747 | ); |
| 13748 | let mut inter1_u = vec![0i32; CHROMA_SIDE * CHROMA_SIDE]; |
| 13749 | mc::predict_compound_intermediate( |
| 13750 | &pu1.data, |
| 13751 | pu1.width, |
| 13752 | pu1.true_width, |
| 13753 | pu1.true_height, |
| 13754 | mv_to_q4(cpx, mv1.1, false), |
| 13755 | mv_to_q4(cpy, mv1.0, false), |
| 13756 | scale1, |
| 13757 | CHROMA_SIDE, |
| 13758 | CHROMA_SIDE, |
| 13759 | Regular, |
| 13760 | Regular, |
| 13761 | &mut inter1_u, |
| 13762 | ); |
| 13763 | let mut pred_u = vec![0u16; CHROMA_SIDE * CHROMA_SIDE]; |
| 13764 | if let Some(mask_y) = mask_y { |
| 13765 | mc::blend_masked_compound( |
| 13766 | &inter0_u, |
| 13767 | &inter1_u, |
| 13768 | mask_y, |
| 13769 | SIDE, |
| 13770 | CHROMA_SIDE, |
| 13771 | CHROMA_SIDE, |
| 13772 | true, |
| 13773 | &mut pred_u, |
| 13774 | ); |
| 13775 | } else { |
| 13776 | mc::combine_compound(&inter0_u, &inter1_u, fwd_offset, bck_offset, &mut pred_u); |
| 13777 | } |
| 13778 | |
| 13779 | let mut inter0_v = vec![0i32; CHROMA_SIDE * CHROMA_SIDE]; |
| 13780 | mc::predict_compound_intermediate( |
| 13781 | &pv0.data, |
| 13782 | pv0.width, |
| 13783 | pv0.true_width, |
| 13784 | pv0.true_height, |
| 13785 | mv_to_q4(cpx, mv0.1, false), |
| 13786 | mv_to_q4(cpy, mv0.0, false), |
| 13787 | scale0, |
| 13788 | CHROMA_SIDE, |
| 13789 | CHROMA_SIDE, |
| 13790 | Regular, |
| 13791 | Regular, |
| 13792 | &mut inter0_v, |
| 13793 | ); |
| 13794 | let mut inter1_v = vec![0i32; CHROMA_SIDE * CHROMA_SIDE]; |
| 13795 | mc::predict_compound_intermediate( |
| 13796 | &pv1.data, |
| 13797 | pv1.width, |
| 13798 | pv1.true_width, |
| 13799 | pv1.true_height, |
| 13800 | mv_to_q4(cpx, mv1.1, false), |
| 13801 | mv_to_q4(cpy, mv1.0, false), |
| 13802 | scale1, |
| 13803 | CHROMA_SIDE, |
| 13804 | CHROMA_SIDE, |
| 13805 | Regular, |
| 13806 | Regular, |
| 13807 | &mut inter1_v, |
| 13808 | ); |
| 13809 | let mut pred_v = vec![0u16; CHROMA_SIDE * CHROMA_SIDE]; |
| 13810 | if let Some(mask_y) = mask_y { |
| 13811 | mc::blend_masked_compound( |
| 13812 | &inter0_v, |
| 13813 | &inter1_v, |
| 13814 | mask_y, |
| 13815 | SIDE, |
| 13816 | CHROMA_SIDE, |
| 13817 | CHROMA_SIDE, |
| 13818 | true, |
| 13819 | &mut pred_v, |
| 13820 | ); |
| 13821 | } else { |
| 13822 | mc::combine_compound(&inter0_v, &inter1_v, fwd_offset, bck_offset, &mut pred_v); |
| 13823 | } |
| 13824 | |
| 13825 | split8 = read_block_tx_size( |
| 13826 | dec, |
| 13827 | cdfs, |
| 13828 | neighbours, |
| 13829 | leaf_mi, |
| 13830 | SIDE, |
| 13831 | (mi_cols as usize, mi_rows as usize), |
| 13832 | true, |
| 13833 | skip, |
| 13834 | )? |
| 13835 | .1 |
| 13836 | .is_some(); |
| 13837 | if skip { |
| 13838 | y.reconstruct_mc(px, py, SIDE, &pred_y, &vec![0i32; SIDE * SIDE]); |
| 13839 | u.reconstruct_mc( |
| 13840 | cpx, |
| 13841 | cpy, |
| 13842 | CHROMA_SIDE, |
| 13843 | &pred_u, |
| 13844 | &vec![0i32; CHROMA_SIDE * CHROMA_SIDE], |
| 13845 | ); |
| 13846 | v.reconstruct_mc( |
| 13847 | cpx, |
| 13848 | cpy, |
| 13849 | CHROMA_SIDE, |
| 13850 | &pred_v, |
| 13851 | &vec![0i32; CHROMA_SIDE * CHROMA_SIDE], |
| 13852 | ); |
| 13853 | luma_grid = vec![0i32; SIDE * SIDE]; |
| 13854 | u_grid = vec![0i32; CHROMA_SIDE * CHROMA_SIDE]; |
| 13855 | v_grid = vec![0i32; CHROMA_SIDE * CHROMA_SIDE]; |
| 13856 | } else { |
| 13857 | let around = neighbours.around_mi(leaf_mi, 8); |
| 13858 | let (grid8, luma_tx_type) = read_inter_luma8( |
| 13859 | dec, |
| 13860 | cdfs, |
| 13861 | neighbours, |
| 13862 | leaf_mi, |
| 13863 | y, |
| 13864 | px, |
| 13865 | py, |
| 13866 | &pred_y, |
| 13867 | mode_for_tx, |
| 13868 | base_q_idx, |
| 13869 | scan8, |
| 13870 | scan4, |
| 13871 | reduced_tx_set, |
| 13872 | split8, |
| 13873 | )?; |
| 13874 | luma_grid = grid8; |
| 13875 | u_grid = read_inter_plane( |
| 13876 | dec, |
| 13877 | cdfs, |
| 13878 | TxbSet::Chroma4, |
| 13879 | scan4, |
| 13880 | 1, |
| 13881 | around[1], |
| 13882 | mode_for_tx, |
| 13883 | u, |
| 13884 | cpx, |
| 13885 | cpy, |
| 13886 | CHROMA_SIDE, |
| 13887 | base_q_idx, |
| 13888 | &pred_u, |
| 13889 | Some(luma_tx_type), |
| 13890 | None, |
| 13891 | )? |
| 13892 | .0; |
| 13893 | v_grid = read_inter_plane( |
| 13894 | dec, |
| 13895 | cdfs, |
| 13896 | TxbSet::Chroma4, |
| 13897 | scan4, |
| 13898 | 2, |
| 13899 | around[2], |
| 13900 | mode_for_tx, |
| 13901 | v, |
| 13902 | cpx, |
| 13903 | cpy, |
| 13904 | CHROMA_SIDE, |
| 13905 | base_q_idx, |
| 13906 | &pred_v, |
| 13907 | Some(luma_tx_type), |
| 13908 | None, |
| 13909 | )? |
| 13910 | .0; |
| 13911 | } |
| 13912 | // lane-inter8 r2: the COMPOUND leaf used to return here |
| 13913 | // WITHOUT the end-of-function `record_mi`/`fill_lf_grid` the |
| 13914 | // single-ref path falls through to -- so a compound 8x8 leaf |
| 13915 | // left `above_side_mi`/`left_side_mi` (the next block's |
| 13916 | // partition context), the per-plane coefficient level state |
| 13917 | // and the loop-filter grid describing whatever block was |
| 13918 | // there before. Every leaf of the gate's stream is compound, |
| 13919 | // so the 16x16 below a split block read partition ctx 0 |
| 13920 | // instead of 1 and mis-read PARTITION_SPLIT as NONE. |
| 13921 | neighbours.record_mi(leaf_mi, 8, &[luma_grid, u_grid, v_grid]); |
| 13922 | neighbours.fill_lf_grid(leaf_mi, 2, 8, if is_inter { LAST_FRAME } else { 0 }); |
| 13923 | return Ok((skip, is_inter, skip_mode, compound_ctx8, leaf_refs)); |
| 13924 | } |
| 13925 | } |
| 13926 | leaf_refs = (LAST_FRAME, None); |
| 13927 | let sr_ctx = single_ref_ctx(above_inter || left_inter); |
| 13928 | let p1 = dec.symbol(&mut cdfs.single_ref[sr_ctx][0]); |
| 13929 | let p3 = dec.symbol(&mut cdfs.single_ref[sr_ctx][2]); |
| 13930 | let p4 = dec.symbol(&mut cdfs.single_ref[sr_ctx][3]); |
| 13931 | if (p1, p3, p4) != (0, 0, 0) { |
| 13932 | return Err(unsupported( |
| 13933 | "a reference frame other than LAST_FRAME (round 2)", |
| 13934 | )); |
| 13935 | } |
| 13936 | |
| 13937 | let (mi_row, mi_col) = leaf_mi; |
| 13938 | let tpl = tpl_frame.map(|t| crate::mvstack::TplArgs { |
| 13939 | field: t.field, |
| 13940 | cur_offset_0: crate::motion_field::get_relative_dist( |
| 13941 | t.order_hint_bits, |
| 13942 | t.order_hint, |
| 13943 | t.ref_order_hints[(LAST_FRAME - LAST_FRAME) as usize], |
| 13944 | ), |
| 13945 | allow_high_precision_mv, |
| 13946 | }); |
| 13947 | let stack = find_mv_stack_with_sign_bias( |
| 13948 | grid, |
| 13949 | mi_row, |
| 13950 | mi_col, |
| 13951 | 2, |
| 13952 | 2, |
| 13953 | LAST_FRAME, |
| 13954 | mi_cols as usize, |
| 13955 | mi_rows as usize, |
| 13956 | sign_bias_table, |
| 13957 | &gm_table, |
| 13958 | tpl, |
| 13959 | ); |
| 13960 | |
| 13961 | let not_new = dec.symbol(&mut cdfs.new_mv[stack.new_mv_ctx]) == 1; |
| 13962 | let (mv, is_new_mv) = if !not_new { |
| 13963 | // NEWMV (spec 5.11.24's `read_drl_idx`, `RefMvIdx` starting at 0): |
| 13964 | // read at most two `drl_mode` bits, one per stack entry past the |
| 13965 | // first, stopping at the first `0`. The chosen index selects |
| 13966 | // which stack entry `read_mv`'s base predictor comes from |
| 13967 | // (spec 7.10.2.10 `assign_mv`'s `PredMv = RefStackMv[RefMvIdx]`) |
| 13968 | // -- entry 0 (`stack.pred_mv`) only when the loop never advances. |
| 13969 | let mut idx = 0usize; |
| 13970 | while idx < 2 && stack.entries.len() > idx + 1 { |
| 13971 | if dec.symbol(&mut cdfs.drl_mode[stack.drl_ctx[idx]]) == 0 { |
| 13972 | break; |
| 13973 | } |
| 13974 | idx += 1; |
| 13975 | } |
| 13976 | let base_mv = stack.entries.get(idx).map_or(stack.pred_mv, |e| e.mv); |
| 13977 | ( |
| 13978 | read_mv( |
| 13979 | dec, |
| 13980 | &mut cdfs.mv_comp, |
| 13981 | &mut cdfs.mv_joint, |
| 13982 | base_mv, |
| 13983 | allow_high_precision_mv, |
| 13984 | force_integer_mv, |
| 13985 | ), |
| 13986 | true, |
| 13987 | ) |
| 13988 | } else { |
| 13989 | let not_zero = dec.symbol(&mut cdfs.zero_mv[stack.zero_mv_ctx]) == 1; |
| 13990 | if !not_zero { |
| 13991 | return Err(unsupported("GLOBALMV (round 3)")); |
| 13992 | } |
| 13993 | let nearest = dec.symbol(&mut cdfs.ref_mv[stack.ref_mv_ctx]) == 0; |
| 13994 | let mv = if nearest { |
| 13995 | stack.nearest_mv |
| 13996 | } else { |
| 13997 | let mut idx = 1usize; |
| 13998 | while idx < 3 && stack.entries.len() > idx + 1 { |
| 13999 | if dec.symbol(&mut cdfs.drl_mode[stack.drl_ctx[idx]]) == 0 { |
| 14000 | break; |
| 14001 | } |
| 14002 | idx += 1; |
| 14003 | } |
| 14004 | stack.entries.get(idx).map_or(stack.near_mv, |e| e.mv) |
| 14005 | }; |
| 14006 | (mv, false) |
| 14007 | }; |
| 14008 | // lane-sb128 r4: `interintra` (spec 5.11.24) at the 8x8 leaf too -- |
| 14009 | // see [`decode_inter_block`]'s own doc; `BLOCK_8X8` is always |
| 14010 | // `is_interintra_allowed_bsize`-eligible, single-ref-only branch |
| 14011 | // (compound already returned above), GLOBALMV already refused |
| 14012 | // (round 3), so the only real gate left is the header enable bit. |
| 14013 | let mut interintra_mode: Option<u8> = None; |
| 14014 | let mut wedge_mask: Option<(&'static [u8], usize)> = None; |
| 14015 | if enable_interintra_compound && !skip_mode { |
| 14016 | let interintra = dec.symbol(&mut cdfs.interintra[SIZE_GROUP_8]) == 1; |
| 14017 | if interintra { |
| 14018 | // lane-interintra r1: same read pair as the 16/32 site; |
| 14019 | // wedge cdf row = BLOCK_8X8 (index 3), read on block size |
| 14020 | // alone, never the `enable_interintra_wedge` seq bit. |
| 14021 | let ii = dec.symbol(&mut cdfs.interintra_mode[SIZE_GROUP_8]) as u8; |
| 14022 | let wedge = dec.symbol(&mut cdfs.wedge_interintra[3]) == 1; |
| 14023 | if wedge { |
| 14024 | // lane-wii r2: same adapting `wedge_index` symbol as the |
| 14025 | // 16/32 leaf, fixed sign 0 -- see that site's comment. |
| 14026 | let wedge_index = dec.symbol(&mut cdfs.wedge_idx[3]); |
| 14027 | WII_HITS.with(|c| c.set(c.get() + 1)); |
| 14028 | wedge_mask = Some(( |
| 14029 | crate::wedge::wedge_masks() |
| 14030 | .codebook(SIDE) |
| 14031 | .mask(0, wedge_index as usize), |
| 14032 | SIDE, |
| 14033 | )); |
| 14034 | } else { |
| 14035 | INTERINTRA_HITS.with(|c| c.set(c.get() + 1)); |
| 14036 | } |
| 14037 | interintra_mode = Some(ii); |
| 14038 | } |
| 14039 | } |
| 14040 | // lane-motionmode round 3: `read_motion_mode` (spec 5.11.24) at the |
| 14041 | // 8x8 leaf too -- `motion_mode_allowed` still holds here (BLOCK_8X8 |
| 14042 | // is the spec's own minimum eligible size, single-ref-only branch, |
| 14043 | // no interintra reader, `IDENTITY`-only global motion). Mirrors the |
| 14044 | // 16x16 leaf's own `decode_inter_block` read (see its doc above) -- |
| 14045 | // same 2-symbol `obmc_cdf` alphabet, same `allow_warped_motion=1` |
| 14046 | // refusal (warp needs `av1_findSamples`, not ported at either leaf). |
| 14047 | let motion_mode_eligible = switchable_motion_mode |
| 14048 | && !skip_mode |
| 14049 | && interintra_mode.is_none() |
| 14050 | && (!overlappable_above(grid, mi_row, mi_col, 2, mi_cols as usize, 1).is_empty() |
| 14051 | || !overlappable_left(grid, mi_row, mi_col, 2, mi_rows as usize, 1).is_empty()); |
| 14052 | let mut obmc_selected = false; |
| 14053 | if motion_mode_eligible { |
| 14054 | // lane-warp round 1: same 3-vs-2-symbol split as the 16x16+ leaf |
| 14055 | // (see its own doc) -- this leaf is always `LAST_FRAME`-only |
| 14056 | // (grid.set below hardcodes it). |
| 14057 | // lane-scaledref r2 / lane-fimv r1 sibling of the 16x16+ leaf's |
| 14058 | // fix: libaom drops WARPED_CAUSAL eligibility (so this reads |
| 14059 | // `obmc_cdf`) when the reference is scaled or the frame set |
| 14060 | // `force_integer_mv`. The scaled half is not gated here because |
| 14061 | // an 8x8 leaf under a scaled reference is refused before this |
| 14062 | // function runs (see the block8 refusal below, ~12500) -- |
| 14063 | // whoever lifts that refusal must add `&& !ref_is_scaled` here |
| 14064 | // too. |
| 14065 | let proj_ok = |
| 14066 | num_proj_ref(grid, mi_row, mi_col, 2, 2, mi_cols as usize, mi_rows as usize, LAST_FRAME) |
| 14067 | >= 1; |
| 14068 | let warp_eligible = allow_warped_motion && !force_integer_mv && proj_ok; |
| 14069 | if allow_warped_motion && proj_ok && force_integer_mv { |
| 14070 | FIMV_ALPHABET_HITS.with(|c| c.set(c.get() + 1)); |
| 14071 | } |
| 14072 | if warp_eligible { |
| 14073 | let mode = dec.symbol(&mut cdfs.motion_mode[0]); |
| 14074 | match mode { |
| 14075 | 0 => {} |
| 14076 | 1 => { |
| 14077 | obmc_selected = true; |
| 14078 | OBMC_HITS.with(|c| c.set(c.get() + 1)); |
| 14079 | OBMC_HITS_8.with(|c| c.set(c.get() + 1)); |
| 14080 | } |
| 14081 | _ => { |
| 14082 | WARP_SELECTED_HITS.with(|c| c.set(c.get() + 1)); |
| 14083 | return Err(unsupported( |
| 14084 | "an 8x8 leaf that coded WARPED_CAUSAL (motion_mode == 2): \ |
| 14085 | av1_find_projection/the affine warp filter are not \ |
| 14086 | ported, only motion_mode_allowed's alphabet choice is", |
| 14087 | )); |
| 14088 | } |
| 14089 | } |
| 14090 | } else { |
| 14091 | // `default_obmc_cdf`'s own index: square bsize 8/16/32/64 -> 0/1/2/3. |
| 14092 | obmc_selected = dec.symbol(&mut cdfs.obmc[0]) == 1; |
| 14093 | if obmc_selected { |
| 14094 | OBMC_HITS.with(|c| c.set(c.get() + 1)); |
| 14095 | OBMC_HITS_8.with(|c| c.set(c.get() + 1)); |
| 14096 | } |
| 14097 | } |
| 14098 | } |
| 14099 | for dr in 0..2 { |
| 14100 | for dc in 0..2 { |
| 14101 | grid.set( |
| 14102 | mi_row + dr, |
| 14103 | mi_col + dc, |
| 14104 | MiInfo { |
| 14105 | is_inter: true, |
| 14106 | ref_frame: LAST_FRAME, |
| 14107 | // INTRA_FRAME marker for interintra blocks -- keeps |
| 14108 | // them out of warp-sample gathering (see 16/32 site). |
| 14109 | ref_frame1: interintra_mode.map(|_| 0), |
| 14110 | mv1: None, |
| 14111 | mv, |
| 14112 | is_new_mv, |
| 14113 | size: 2, |
| 14114 | size_h: 2, |
| 14115 | is_global_mv0: false, |
| 14116 | is_global_mv1: false, |
| 14117 | }, |
| 14118 | ); |
| 14119 | } |
| 14120 | } |
| 14121 | mode_for_tx = 0; |
| 14122 | |
| 14123 | // lane-scaledref r1: this leaf's own prediction is always `Regular` |
| 14124 | // (documented corner-cut above); under a scaled reference the same |
| 14125 | // fixed kernel runs through spec 7.11.3.3's scaled walk instead. |
| 14126 | let luma_scale = mc::scale_factor(ref_y.width, frame_width); |
| 14127 | if luma_scale != mc::REF_NO_SCALE { |
| 14128 | SCALED_BLOCK8_HITS.with(|c| c.set(c.get() + 1)); |
| 14129 | } |
| 14130 | let mut pred_y = vec![0u16; SIDE * SIDE]; |
| 14131 | let mut pred_u = vec![0u16; CHROMA_SIDE * CHROMA_SIDE]; |
| 14132 | let mut pred_v = vec![0u16; CHROMA_SIDE * CHROMA_SIDE]; |
| 14133 | for (plane, x, y, dim, dst) in [ |
| 14134 | (ref_y, px, py, SIDE, &mut pred_y), |
| 14135 | (ref_u, cpx, cpy, CHROMA_SIDE, &mut pred_u), |
| 14136 | (ref_v, cpx, cpy, CHROMA_SIDE, &mut pred_v), |
| 14137 | ] { |
| 14138 | let luma = std::ptr::eq(plane, ref_y); |
| 14139 | if luma_scale == mc::REF_NO_SCALE { |
| 14140 | mc::predict( |
| 14141 | &plane.data, |
| 14142 | plane.width, |
| 14143 | plane.true_width, |
| 14144 | plane.true_height, |
| 14145 | mv_to_q4(x, mv.1, luma), |
| 14146 | mv_to_q4(y, mv.0, luma), |
| 14147 | dim, |
| 14148 | dim, |
| 14149 | dst, |
| 14150 | ); |
| 14151 | } else { |
| 14152 | mc::predict_scaled( |
| 14153 | &plane.data, |
| 14154 | plane.width, |
| 14155 | plane.true_width, |
| 14156 | plane.true_height, |
| 14157 | mv_to_q4(x, mv.1, luma), |
| 14158 | mv_to_q4(y, mv.0, luma), |
| 14159 | luma_scale, |
| 14160 | dim, |
| 14161 | dim, |
| 14162 | mc::InterpFilterKind::Regular, |
| 14163 | mc::InterpFilterKind::Regular, |
| 14164 | dst, |
| 14165 | ); |
| 14166 | } |
| 14167 | } |
| 14168 | |
| 14169 | if obmc_selected { |
| 14170 | obmc_blend( |
| 14171 | grid, |
| 14172 | neighbours, |
| 14173 | mi_row, |
| 14174 | mi_col, |
| 14175 | 2, |
| 14176 | 2, |
| 14177 | mi_rows as usize, |
| 14178 | mi_cols as usize, |
| 14179 | SIDE, |
| 14180 | SIDE, |
| 14181 | SIDE, |
| 14182 | CHROMA_SIDE, |
| 14183 | px, |
| 14184 | py, |
| 14185 | cpx, |
| 14186 | cpy, |
| 14187 | ref_y, |
| 14188 | ref_u, |
| 14189 | ref_v, |
| 14190 | other_refs, |
| 14191 | interp_fixed, |
| 14192 | frame_width, |
| 14193 | &mut pred_y, |
| 14194 | &mut pred_u, |
| 14195 | &mut pred_v, |
| 14196 | )?; |
| 14197 | } |
| 14198 | |
| 14199 | // Non-wedge interintra blend, mutually exclusive with OBMC (the |
| 14200 | // motion_mode symbol is not read for an interintra block). |
| 14201 | if let Some(ii) = interintra_mode { |
| 14202 | interintra_blend(y, px, py, SIDE, ii, wedge_mask, &mut pred_y); |
| 14203 | interintra_blend(u, cpx, cpy, CHROMA_SIDE, ii, wedge_mask, &mut pred_u); |
| 14204 | interintra_blend(v, cpx, cpy, CHROMA_SIDE, ii, wedge_mask, &mut pred_v); |
| 14205 | } |
| 14206 | |
| 14207 | split8 = read_block_tx_size( |
| 14208 | dec, |
| 14209 | cdfs, |
| 14210 | neighbours, |
| 14211 | leaf_mi, |
| 14212 | SIDE, |
| 14213 | (mi_cols as usize, mi_rows as usize), |
| 14214 | true, |
| 14215 | skip, |
| 14216 | )? |
| 14217 | .1 |
| 14218 | .is_some(); |
| 14219 | if skip { |
| 14220 | y.reconstruct_mc(px, py, SIDE, &pred_y, &vec![0i32; SIDE * SIDE]); |
| 14221 | u.reconstruct_mc( |
| 14222 | cpx, |
| 14223 | cpy, |
| 14224 | CHROMA_SIDE, |
| 14225 | &pred_u, |
| 14226 | &vec![0i32; CHROMA_SIDE * CHROMA_SIDE], |
| 14227 | ); |
| 14228 | v.reconstruct_mc( |
| 14229 | cpx, |
| 14230 | cpy, |
| 14231 | CHROMA_SIDE, |
| 14232 | &pred_v, |
| 14233 | &vec![0i32; CHROMA_SIDE * CHROMA_SIDE], |
| 14234 | ); |
| 14235 | luma_grid = vec![0i32; SIDE * SIDE]; |
| 14236 | u_grid = vec![0i32; CHROMA_SIDE * CHROMA_SIDE]; |
| 14237 | v_grid = vec![0i32; CHROMA_SIDE * CHROMA_SIDE]; |
| 14238 | } else { |
| 14239 | let around = neighbours.around_mi(leaf_mi, 8); |
| 14240 | let (grid8, luma_tx_type) = read_inter_luma8( |
| 14241 | dec, |
| 14242 | cdfs, |
| 14243 | neighbours, |
| 14244 | leaf_mi, |
| 14245 | y, |
| 14246 | px, |
| 14247 | py, |
| 14248 | &pred_y, |
| 14249 | mode_for_tx, |
| 14250 | base_q_idx, |
| 14251 | scan8, |
| 14252 | scan4, |
| 14253 | reduced_tx_set, |
| 14254 | split8, |
| 14255 | )?; |
| 14256 | luma_grid = grid8; |
| 14257 | u_grid = read_inter_plane( |
| 14258 | dec, |
| 14259 | cdfs, |
| 14260 | TxbSet::Chroma4, |
| 14261 | scan4, |
| 14262 | 1, |
| 14263 | around[1], |
| 14264 | mode_for_tx, |
| 14265 | u, |
| 14266 | cpx, |
| 14267 | cpy, |
| 14268 | CHROMA_SIDE, |
| 14269 | base_q_idx, |
| 14270 | &pred_u, |
| 14271 | Some(luma_tx_type), |
| 14272 | None, |
| 14273 | )? |
| 14274 | .0; |
| 14275 | v_grid = read_inter_plane( |
| 14276 | dec, |
| 14277 | cdfs, |
| 14278 | TxbSet::Chroma4, |
| 14279 | scan4, |
| 14280 | 2, |
| 14281 | around[2], |
| 14282 | mode_for_tx, |
| 14283 | v, |
| 14284 | cpx, |
| 14285 | cpy, |
| 14286 | CHROMA_SIDE, |
| 14287 | base_q_idx, |
| 14288 | &pred_v, |
| 14289 | Some(luma_tx_type), |
| 14290 | None, |
| 14291 | )? |
| 14292 | .0; |
| 14293 | } |
| 14294 | } else { |
| 14295 | let mode = dec.symbol(&mut cdfs.y_mode[SIZE_GROUP_8]); |
| 14296 | if mode >= 13 { |
| 14297 | return Err(unsupported( |
| 14298 | "an intra mode this decoder does not code (round 2)", |
| 14299 | )); |
| 14300 | } |
| 14301 | if (V_PRED..=D67_PRED).contains(&mode) { |
| 14302 | let angle = dec.symbol(&mut cdfs.angle_delta[mode - V_PRED]); |
| 14303 | if angle != ANGLE_DELTA_ZERO { |
| 14304 | return Err(unsupported( |
| 14305 | "a nonzero angle delta (this encoder never writes one)", |
| 14306 | )); |
| 14307 | } |
| 14308 | } |
| 14309 | let uv_mode = dec.symbol(&mut cdfs.uv_mode_cfl[mode]); |
| 14310 | if uv_mode != DC_PRED { |
| 14311 | return Err(unsupported( |
| 14312 | "a non-DC chroma mode on an 8x8 inter-frame leaf (this encoder never writes one)", |
| 14313 | )); |
| 14314 | } |
| 14315 | // lane-screen r2: see [`decode_inter_block`]'s own copy -- `mode`/ |
| 14316 | // `uv_mode` are both effectively `DC_PRED`-gated already on this |
| 14317 | // leaf (a nonzero `mode` still passes, but `uv_mode` is forced |
| 14318 | // `DC_PRED` by the refusal just above), same order as libaom's |
| 14319 | // `read_palette_mode_info` call right after `xd->cfl.store_y`. |
| 14320 | if allow_screen_content_tools { |
| 14321 | if mode == DC_PRED && dec.symbol(&mut cdfs.palette_y_mode[0][0]) != 0 { |
| 14322 | return Err(unsupported( |
| 14323 | "a block that actually uses a palette (Y) -- reconstruction is out of scope", |
| 14324 | )); |
| 14325 | } |
| 14326 | if dec.symbol(&mut cdfs.palette_uv_mode[0]) != 0 { |
| 14327 | return Err(unsupported( |
| 14328 | "a block that actually uses a palette (UV) -- reconstruction is out of scope", |
| 14329 | )); |
| 14330 | } |
| 14331 | } |
| 14332 | mode_for_tx = mode; |
| 14333 | let (mi_row, mi_col) = leaf_mi; |
| 14334 | for dr in 0..2 { |
| 14335 | for dc in 0..2 { |
| 14336 | grid.set( |
| 14337 | mi_row + dr, |
| 14338 | mi_col + dc, |
| 14339 | MiInfo { |
| 14340 | is_inter: false, |
| 14341 | ref_frame: -1, |
| 14342 | ref_frame1: None, |
| 14343 | mv1: None, |
| 14344 | mv: (0, 0), |
| 14345 | is_new_mv: false, |
| 14346 | size: 2, |
| 14347 | size_h: 2, |
| 14348 | is_global_mv0: false, |
| 14349 | is_global_mv1: false, |
| 14350 | }, |
| 14351 | ); |
| 14352 | } |
| 14353 | } |
| 14354 | |
| 14355 | read_block_tx_size( |
| 14356 | dec, |
| 14357 | cdfs, |
| 14358 | neighbours, |
| 14359 | leaf_mi, |
| 14360 | SIDE, |
| 14361 | (mi_cols as usize, mi_rows as usize), |
| 14362 | false, |
| 14363 | skip, |
| 14364 | )?; |
| 14365 | let reach = Reach::of(SIDE, px, py, y.width, y.height); |
| 14366 | if skip { |
| 14367 | y.reconstruct( |
| 14368 | px, |
| 14369 | py, |
| 14370 | SIDE, |
| 14371 | mode, |
| 14372 | 0, |
| 14373 | reach, |
| 14374 | &vec![0i32; SIDE * SIDE], |
| 14375 | None, |
| 14376 | None, |
| 14377 | false, |
| 14378 | ); |
| 14379 | u.reconstruct( |
| 14380 | cpx, |
| 14381 | cpy, |
| 14382 | CHROMA_SIDE, |
| 14383 | DC_PRED, |
| 14384 | 0, |
| 14385 | reach, |
| 14386 | &vec![0i32; CHROMA_SIDE * CHROMA_SIDE], |
| 14387 | None, |
| 14388 | None, |
| 14389 | false, |
| 14390 | ); |
| 14391 | v.reconstruct( |
| 14392 | cpx, |
| 14393 | cpy, |
| 14394 | CHROMA_SIDE, |
| 14395 | DC_PRED, |
| 14396 | 0, |
| 14397 | reach, |
| 14398 | &vec![0i32; CHROMA_SIDE * CHROMA_SIDE], |
| 14399 | None, |
| 14400 | None, |
| 14401 | false, |
| 14402 | ); |
| 14403 | luma_grid = vec![0i32; SIDE * SIDE]; |
| 14404 | u_grid = vec![0i32; CHROMA_SIDE * CHROMA_SIDE]; |
| 14405 | v_grid = vec![0i32; CHROMA_SIDE * CHROMA_SIDE]; |
| 14406 | } else { |
| 14407 | let around = neighbours.around_mi(leaf_mi, 8); |
| 14408 | luma_grid = read_plane( |
| 14409 | dec, |
| 14410 | cdfs, |
| 14411 | TxbSet::Luma8, |
| 14412 | scan8, |
| 14413 | 0, |
| 14414 | around[0], |
| 14415 | mode, |
| 14416 | mode, |
| 14417 | 0, |
| 14418 | reach, |
| 14419 | y, |
| 14420 | px, |
| 14421 | py, |
| 14422 | SIDE, |
| 14423 | TX8, |
| 14424 | base_q_idx, |
| 14425 | None, |
| 14426 | None, |
| 14427 | None, |
| 14428 | false, |
| 14429 | )?; |
| 14430 | u_grid = read_plane( |
| 14431 | dec, |
| 14432 | cdfs, |
| 14433 | TxbSet::Chroma4, |
| 14434 | scan4, |
| 14435 | 1, |
| 14436 | around[1], |
| 14437 | mode, |
| 14438 | DC_PRED, |
| 14439 | 0, |
| 14440 | reach, |
| 14441 | u, |
| 14442 | cpx, |
| 14443 | cpy, |
| 14444 | CHROMA_SIDE, |
| 14445 | TX4, |
| 14446 | base_q_idx, |
| 14447 | None, |
| 14448 | None, |
| 14449 | None, |
| 14450 | false, |
| 14451 | )?; |
| 14452 | v_grid = read_plane( |
| 14453 | dec, |
| 14454 | cdfs, |
| 14455 | TxbSet::Chroma4, |
| 14456 | scan4, |
| 14457 | 2, |
| 14458 | around[2], |
| 14459 | mode, |
| 14460 | DC_PRED, |
| 14461 | 0, |
| 14462 | reach, |
| 14463 | v, |
| 14464 | cpx, |
| 14465 | cpy, |
| 14466 | CHROMA_SIDE, |
| 14467 | TX4, |
| 14468 | base_q_idx, |
| 14469 | None, |
| 14470 | None, |
| 14471 | None, |
| 14472 | false, |
| 14473 | )?; |
| 14474 | } |
| 14475 | } |
| 14476 | // lane-txselect: a split leaf's plane-0 neighbour context is per 4x4 |
| 14477 | // transform unit (written by `record_mi_luma` inside `read_inter_luma8`), |
| 14478 | // but `record_mi` rewrites all three planes of these cells at once -- so |
| 14479 | // save plane 0 across it, exactly as `record_split_luma` leaves plane 0 |
| 14480 | // alone at the bigger block sizes. |
| 14481 | let saved_luma_ctx = split8.then(|| { |
| 14482 | ( |
| 14483 | [ |
| 14484 | neighbours.left[leaf_mi.0][0], |
| 14485 | neighbours.left[leaf_mi.0 + 1][0], |
| 14486 | ], |
| 14487 | [ |
| 14488 | neighbours.above[leaf_mi.1][0], |
| 14489 | neighbours.above[leaf_mi.1 + 1][0], |
| 14490 | ], |
| 14491 | ) |
| 14492 | }); |
| 14493 | neighbours.record_mi(leaf_mi, 8, &[luma_grid, u_grid, v_grid]); |
| 14494 | if let Some((left, above)) = saved_luma_ctx { |
| 14495 | for (cell, state) in left.into_iter().enumerate() { |
| 14496 | neighbours.left[leaf_mi.0 + cell][0] = state; |
| 14497 | } |
| 14498 | for (cell, state) in above.into_iter().enumerate() { |
| 14499 | neighbours.above[leaf_mi.1 + cell][0] = state; |
| 14500 | } |
| 14501 | } |
| 14502 | neighbours.fill_lf_grid( |
| 14503 | leaf_mi, |
| 14504 | 2, |
| 14505 | if split8 { 4 } else { 8 }, |
| 14506 | if is_inter { LAST_FRAME } else { 0 }, |
| 14507 | ); |
| 14508 | Ok((skip, is_inter, skip_mode, compound_ctx8, leaf_refs)) |
| 14509 | } |
| 14510 | |
| 14511 | /// Decodes the payload [`crate::tile::sb_coeff_inter_frame_tile`] writes, |
| 14512 | /// against `reference` (the previous frame's own decoded picture, at this |
| 14513 | /// frame's true size -- a decoder's single-slot DPB, matching what this |
| 14514 | /// crate's encoder always predicts from), returning the picture it |
| 14515 | /// reconstructs to. Mirrors [`decode_key_frame_tile`]'s own contract |
| 14516 | /// (`mi_cols`/`mi_rows`/`base_q_idx` from the frame header, |
| 14517 | /// `frame_width`/`frame_height` its true render size). |
| 14518 | /// |
| 14519 | /// # Errors |
| 14520 | /// Returns an error under the same conditions [`decode_inter_block`] does, |
| 14521 | /// or when `reference` is not this frame's own true size, or when the tile |
| 14522 | /// codes a 16x16 leaf whose own true edge cuts through it (round 2, same |
| 14523 | /// refusal [`crate::tile`]'s writer gives its encoder). |
| 14524 | pub fn decode_inter_frame_tile( |
| 14525 | data: &[u8], |
| 14526 | mi_cols: u32, |
| 14527 | mi_rows: u32, |
| 14528 | base_q_idx: u8, |
| 14529 | frame_width: u32, |
| 14530 | frame_height: u32, |
| 14531 | reference: &Picture, |
| 14532 | other_refs: [Option<&Picture>; 8], |
| 14533 | cdef: &CdefParams, |
| 14534 | loop_filter: &LoopFilterParams, |
| 14535 | allow_high_precision_mv: bool, |
| 14536 | force_integer_mv: bool, |
| 14537 | interp_fixed: Option<mc::InterpFilterKind>, |
| 14538 | enable_dual_filter: bool, |
| 14539 | reference_select: bool, |
| 14540 | ) -> Result<Picture> { |
| 14541 | let single_tile = TileInfo { |
| 14542 | mi_col_starts: vec![0, mi_cols], |
| 14543 | mi_row_starts: vec![0, mi_rows], |
| 14544 | ..TileInfo::default() |
| 14545 | }; |
| 14546 | decode_inter_frame_tile_with_cdfs( |
| 14547 | &[data], |
| 14548 | &single_tile, |
| 14549 | mi_cols, |
| 14550 | mi_rows, |
| 14551 | base_q_idx, |
| 14552 | crate::quant::QuantDeltas::default(), |
| 14553 | frame_width, |
| 14554 | frame_height, |
| 14555 | reference, |
| 14556 | other_refs, |
| 14557 | cdef, |
| 14558 | loop_filter, |
| 14559 | &LoopRestorationParams::default(), |
| 14560 | None, |
| 14561 | allow_high_precision_mv, |
| 14562 | force_integer_mv, |
| 14563 | NO_SIGN_BIAS, |
| 14564 | [ec_av1_syntax::WarpParams::default(); 7], |
| 14565 | interp_fixed, |
| 14566 | enable_dual_filter, |
| 14567 | 0, |
| 14568 | 0, |
| 14569 | [0; 7], |
| 14570 | None, |
| 14571 | reference_select, |
| 14572 | false, |
| 14573 | false, |
| 14574 | false, |
| 14575 | false, |
| 14576 | [0; 2], |
| 14577 | true, |
| 14578 | // `tx_select`: this raw entry point decodes this crate's own encoder's |
| 14579 | // streams, which always write `TxMode::Largest`. |
| 14580 | false, |
| 14581 | false, |
| 14582 | false, |
| 14583 | false, |
| 14584 | DeltaParams::default(), |
| 14585 | ) |
| 14586 | .map(|(picture, _, _)| picture) |
| 14587 | } |
| 14588 | |
| 14589 | /// [`decode_inter_frame_tile`], threading a cross-frame CDF forward -- see |
| 14590 | /// [`decode_key_frame_tile_with_cdfs`]'s doc for `initial_cdfs`/the returned |
| 14591 | /// end-of-tile table. An inter frame's own header can name |
| 14592 | /// `primary_ref_frame == PRIMARY_REF_NONE` too (an error-resilient stream), |
| 14593 | /// so `None` is a real case here, not only a convenience default. |
| 14594 | pub(crate) fn decode_inter_frame_tile_with_cdfs( |
| 14595 | tiles: &[&[u8]], |
| 14596 | tile_info: &TileInfo, |
| 14597 | mi_cols: u32, |
| 14598 | mi_rows: u32, |
| 14599 | base_q_idx: u8, |
| 14600 | // lane-sbpart r11: see [`decode_key_frame_tile_with_cdfs`]'s own `deltas`. |
| 14601 | deltas: crate::quant::QuantDeltas, |
| 14602 | frame_width: u32, |
| 14603 | frame_height: u32, |
| 14604 | reference: &Picture, |
| 14605 | other_refs: [Option<&Picture>; 8], |
| 14606 | cdef: &CdefParams, |
| 14607 | loop_filter: &LoopFilterParams, |
| 14608 | lr: &LoopRestorationParams, |
| 14609 | initial_cdfs: Option<Cdfs>, |
| 14610 | allow_high_precision_mv: bool, |
| 14611 | force_integer_mv: bool, |
| 14612 | sign_bias_table: SignBiasTable, |
| 14613 | // lane-gm r2: this frame header's own `global_motion` table (spec |
| 14614 | // 5.9.24), threaded to every `decode_inter_block` call below. |
| 14615 | global_motion: [ec_av1_syntax::WarpParams; 7], |
| 14616 | interp_fixed: Option<mc::InterpFilterKind>, |
| 14617 | enable_dual_filter: bool, |
| 14618 | // lane-av1tmvp: `order_hint`/`order_hint_bits`/`ref_order_hints` are |
| 14619 | // always needed (this frame's own saved [`crate::motion_field::MotionField`] |
| 14620 | // carries them for a *later* frame's projection, spec 7.9); `tpl_field` |
| 14621 | // is `Some` only when this frame's own header set `use_ref_frame_mvs` |
| 14622 | // and the caller ([`crate::stream::decode_stream`]) projected one. |
| 14623 | order_hint_bits: u32, |
| 14624 | order_hint: u32, |
| 14625 | ref_order_hints: [u32; 7], |
| 14626 | tpl_field: Option<&crate::motion_field::TplField>, |
| 14627 | // lane-av1comp: this frame header's own `reference_select` bit, threaded |
| 14628 | // to every `decode_inter_block`/`decode_inter_block8` call below. |
| 14629 | reference_select: bool, |
| 14630 | // lane-av1comp: `seq_params`' own compound-blend enable bits. |
| 14631 | enable_masked_compound: bool, |
| 14632 | enable_jnt_comp: bool, |
| 14633 | // lane-sb128 r3: this sequence header's own `enable_interintra_compound` |
| 14634 | // bit, threaded to every `decode_inter_block`/`decode_inter_block8` call |
| 14635 | // below (spec 5.11.24's `interintra` read). |
| 14636 | enable_interintra_compound: bool, |
| 14637 | // lane-av1comp round 14: this frame header's own `skip_mode_present`/ |
| 14638 | // `skip_mode_frame` (spec 5.9.22), threaded to every `decode_inter_block`/ |
| 14639 | // `decode_inter_block8` call below. |
| 14640 | skip_mode_present: bool, |
| 14641 | skip_mode_frame: [u8; 2], |
| 14642 | // lane-cdffwd2: this frame header's own `reduced_tx_set` bit, threaded to |
| 14643 | // every inter `tx_type` read below (`false` needs the 12-/16-symbol |
| 14644 | // `Set1` coefficient tables at 16x16/8x8 rather than the reduced |
| 14645 | // 2-symbol ones -- see [`TxbSet::Luma16InterSet1`]/[`TxbSet::Luma8InterSet1`]). |
| 14646 | reduced_tx_set: bool, |
| 14647 | // lane-txselect: this frame header's own `tx_mode == TxMode::Select` |
| 14648 | // bit (spec 5.9.14). Together with `reduced_tx_set` it is stashed in a |
| 14649 | // thread-local by [`set_inter_tx_mode`] rather than threaded through |
| 14650 | // `decode_inter_block`'s forty parameters -- both are frame-constant, and |
| 14651 | // only [`read_block_tx_size`]/the var-tx residual loop read them. |
| 14652 | tx_select: bool, |
| 14653 | // lane-motionmode round 1/3: this frame header's own `is_motion_mode_switchable`/ |
| 14654 | // `allow_warped_motion` bits, threaded to every `decode_inter_block`/ |
| 14655 | // `decode_inter_block8` call below (spec 5.11.24's `read_motion_mode`). |
| 14656 | switchable_motion_mode: bool, |
| 14657 | allow_warped_motion: bool, |
| 14658 | // lane-screen r2: this frame header's own `allow_screen_content_tools` |
| 14659 | // bit, threaded to every `decode_inter_block`/`decode_inter_block8` |
| 14660 | // call below -- consumes (never reconstructs) the intra-sub-block |
| 14661 | // palette syntax libaom's `read_intra_block_mode_info` reads under the |
| 14662 | // same `av1_allow_palette` gate as the key-frame path. |
| 14663 | allow_screen_content_tools: bool, |
| 14664 | // lane-realworld r5: this frame header's own `delta` (spec 5.9.17/5.9.18), |
| 14665 | // same contract as [`decode_key_frame_tile_with_cdfs`]'s own `delta` |
| 14666 | // param -- only `q_present`/`q_res` are read. |
| 14667 | delta: DeltaParams, |
| 14668 | ) -> Result<(Picture, Cdfs, crate::motion_field::MotionField)> { |
| 14669 | set_inter_tx_mode(tx_select, reduced_tx_set); |
| 14670 | let tpl_frame = tpl_field.map(|field| TplFrameArgs { |
| 14671 | field, |
| 14672 | order_hint_bits, |
| 14673 | order_hint, |
| 14674 | ref_order_hints, |
| 14675 | }); |
| 14676 | if mi_cols == 0 || mi_rows == 0 { |
| 14677 | return Err(unsupported("a frame with no mode-info grid")); |
| 14678 | } |
| 14679 | let (true_width, true_height) = ((mi_cols * 4) as usize, (mi_rows * 4) as usize); |
| 14680 | // lane-superres r10: a width mismatch alone is no longer refused here -- |
| 14681 | // `mc::predict_scaled` (spec 7.11.3.3, threaded through |
| 14682 | // `decode_inter_block`'s single-ref branch) exists precisely for a |
| 14683 | // `use_superres` reference at a different width than this frame's own |
| 14684 | // true size. Height must still match: AV1 superres never scales height |
| 14685 | // (r8's derivation, `mc.rs`'s own doc), and nothing in this decode path |
| 14686 | // has a vertical scaling pass. |
| 14687 | if reference.height != true_height { |
| 14688 | return Err(unsupported( |
| 14689 | "a reference picture whose height does not match this frame's own true size", |
| 14690 | )); |
| 14691 | } |
| 14692 | let (cols, rows) = block_grid(mi_cols, mi_rows); |
| 14693 | let (sb_cols, sb_rows) = (cols.div_ceil(2), rows.div_ceil(2)); |
| 14694 | CDEF_BITS.with(|c| c.set(cdef.bits)); |
| 14695 | CDEF_SB_COLS.with(|c| c.set(sb_cols as usize)); |
| 14696 | CDEF_IDX_GRID.with(|g| *g.borrow_mut() = vec![0u8; sb_cols as usize * sb_rows as usize]); |
| 14697 | let (width, height) = (cols as usize * BLOCK, rows as usize * BLOCK); |
| 14698 | |
| 14699 | let ref_y = PlaneBuf { |
| 14700 | data: reference.y.iter().map(|&s| s as u16).collect(), |
| 14701 | width: reference.width, |
| 14702 | height: reference.height, |
| 14703 | true_width: reference.width, |
| 14704 | true_height: reference.height, |
| 14705 | tile_x0: 0, |
| 14706 | tile_y0: 0, |
| 14707 | tile_x1: reference.width, |
| 14708 | tile_y1: reference.height, |
| 14709 | }; |
| 14710 | let ref_u = PlaneBuf { |
| 14711 | data: reference.u.iter().map(|&s| s as u16).collect(), |
| 14712 | width: reference.width / 2, |
| 14713 | height: reference.height / 2, |
| 14714 | true_width: reference.width / 2, |
| 14715 | true_height: reference.height / 2, |
| 14716 | tile_x0: 0, |
| 14717 | tile_y0: 0, |
| 14718 | tile_x1: reference.width / 2, |
| 14719 | tile_y1: reference.height / 2, |
| 14720 | }; |
| 14721 | let ref_v = PlaneBuf { |
| 14722 | data: reference.v.iter().map(|&s| s as u16).collect(), |
| 14723 | width: reference.width / 2, |
| 14724 | height: reference.height / 2, |
| 14725 | true_width: reference.width / 2, |
| 14726 | true_height: reference.height / 2, |
| 14727 | tile_x0: 0, |
| 14728 | tile_y0: 0, |
| 14729 | tile_x1: reference.width / 2, |
| 14730 | tile_y1: reference.height / 2, |
| 14731 | }; |
| 14732 | // Every non-`LAST_FRAME` reference this frame header's own `ref_frame_idx` |
| 14733 | // names a live DPB slot for (lane-av1refs: generalised from the old |
| 14734 | // GOLDEN_FRAME-only block -- same empty-slot/size-mismatch refusal, |
| 14735 | // now per-reference). `decode_inter_block` refuses by name when the |
| 14736 | // block it is decoding selects an index that is still `None` here |
| 14737 | // (spec 7.9 requires every *active* reference be a compatible size; |
| 14738 | // an inactive one this frame never selects is simply left `None`). |
| 14739 | let owned_refs: [Option<(PlaneBuf, PlaneBuf, PlaneBuf)>; 8] = std::array::from_fn(|i| { |
| 14740 | other_refs[i] |
| 14741 | .filter(|g| g.width == reference.width && g.height == reference.height) |
| 14742 | .map(|g| { |
| 14743 | ( |
| 14744 | PlaneBuf { |
| 14745 | data: g.y.iter().map(|&s| s as u16).collect(), |
| 14746 | width: g.width, |
| 14747 | height: g.height, |
| 14748 | true_width: g.width, |
| 14749 | true_height: g.height, |
| 14750 | tile_x0: 0, |
| 14751 | tile_y0: 0, |
| 14752 | tile_x1: g.width, |
| 14753 | tile_y1: g.height, |
| 14754 | }, |
| 14755 | PlaneBuf { |
| 14756 | data: g.u.iter().map(|&s| s as u16).collect(), |
| 14757 | width: g.width / 2, |
| 14758 | height: g.height / 2, |
| 14759 | true_width: g.width / 2, |
| 14760 | true_height: g.height / 2, |
| 14761 | tile_x0: 0, |
| 14762 | tile_y0: 0, |
| 14763 | tile_x1: g.width / 2, |
| 14764 | tile_y1: g.height / 2, |
| 14765 | }, |
| 14766 | PlaneBuf { |
| 14767 | data: g.v.iter().map(|&s| s as u16).collect(), |
| 14768 | width: g.width / 2, |
| 14769 | height: g.height / 2, |
| 14770 | true_width: g.width / 2, |
| 14771 | true_height: g.height / 2, |
| 14772 | tile_x0: 0, |
| 14773 | tile_y0: 0, |
| 14774 | tile_x1: g.width / 2, |
| 14775 | tile_y1: g.height / 2, |
| 14776 | }, |
| 14777 | ) |
| 14778 | }) |
| 14779 | }); |
| 14780 | let ref_slots: RefSlots = |
| 14781 | std::array::from_fn(|i| owned_refs[i].as_ref().map(|(a, b, c)| (a, b, c))); |
| 14782 | |
| 14783 | let mut y = PlaneBuf { |
| 14784 | data: vec![0u16; width * height], |
| 14785 | width, |
| 14786 | height, |
| 14787 | true_width, |
| 14788 | true_height, |
| 14789 | tile_x0: 0, |
| 14790 | tile_y0: 0, |
| 14791 | tile_x1: width, |
| 14792 | tile_y1: height, |
| 14793 | }; |
| 14794 | let mut u = PlaneBuf { |
| 14795 | data: vec![0u16; width * height / 4], |
| 14796 | width: width / 2, |
| 14797 | height: height / 2, |
| 14798 | true_width: true_width / 2, |
| 14799 | true_height: true_height / 2, |
| 14800 | tile_x0: 0, |
| 14801 | tile_y0: 0, |
| 14802 | tile_x1: width / 2, |
| 14803 | tile_y1: height / 2, |
| 14804 | }; |
| 14805 | let mut v = PlaneBuf { |
| 14806 | data: vec![0u16; width * height / 4], |
| 14807 | width: width / 2, |
| 14808 | height: height / 2, |
| 14809 | true_width: true_width / 2, |
| 14810 | true_height: true_height / 2, |
| 14811 | tile_x0: 0, |
| 14812 | tile_y0: 0, |
| 14813 | tile_x1: width / 2, |
| 14814 | tile_y1: height / 2, |
| 14815 | }; |
| 14816 | |
| 14817 | let scan32 = default_scan(TX32); |
| 14818 | let scan16 = default_scan(TX16); |
| 14819 | let scan8 = default_scan(TX8); |
| 14820 | let scan4 = default_scan(TX4); |
| 14821 | |
| 14822 | let base_cdfs = initial_cdfs.unwrap_or_else(|| Cdfs::new(q_ctx_of(base_q_idx))); |
| 14823 | let mut result_cdfs = base_cdfs.clone(); |
| 14824 | // spec `decode_tile`: `CurrentQIndex`/`DeltaLF` reset to the frame's own |
| 14825 | // `base_q_idx`/zero at the top of every tile, not once per frame (mirrors |
| 14826 | // `decode_key_frame_tile_with_cdfs`'s per-tile reset below); the |
| 14827 | // delta_q/delta_lf presence/resolution flags themselves are frame-level |
| 14828 | // and set once here. |
| 14829 | DELTA_Q_PRESENT.with(|c| c.set(delta.q_present)); |
| 14830 | DELTA_Q_RES.with(|c| c.set(1i32 << delta.q_res)); |
| 14831 | DELTA_LF_PRESENT.with(|c| c.set(delta.lf_present)); |
| 14832 | DELTA_LF_RES.with(|c| c.set(1i32 << delta.lf_res)); |
| 14833 | DELTA_LF_MULTI.with(|c| c.set(delta.lf_multi)); |
| 14834 | let mut neighbours = Neighbours::new( |
| 14835 | cols as usize * 2, |
| 14836 | rows as usize * 2, |
| 14837 | mi_cols as usize, |
| 14838 | mi_rows as usize, |
| 14839 | ); |
| 14840 | let mut grid = MiGrid::new(mi_cols as usize, mi_rows as usize); |
| 14841 | let mut lr_grid = crate::restoration::RestorationGrid::new(lr, frame_width, frame_height); |
| 14842 | let mut lr_reference = [( |
| 14843 | crate::restoration::WienerInfo::default(), |
| 14844 | crate::restoration::SgrprojInfo::default(), |
| 14845 | ); 3]; |
| 14846 | |
| 14847 | // lane-tiles r6: mirrors decode_key_frame_tile_with_cdfs's own per-tile |
| 14848 | // loop (spec 5.11.2 decode_tile / 7.20 exit_symbol) -- each tile gets |
| 14849 | // its own fresh SymbolDecoder over its own byte range and its own fresh |
| 14850 | // copy of the frame's initial CDFs; only context_update_tile_id's own |
| 14851 | // end-of-tile adapted table becomes the frame's own output. |
| 14852 | for (tile_idx, &data) in tiles.iter().enumerate() { |
| 14853 | let tile_num = tile_idx as u32; |
| 14854 | let (trow, tcol) = (tile_num / tile_info.cols, tile_num % tile_info.cols); |
| 14855 | let mi_row0 = tile_info.mi_row_starts[trow as usize]; |
| 14856 | let mi_row1 = tile_info.mi_row_starts[trow as usize + 1]; |
| 14857 | let mi_col0 = tile_info.mi_col_starts[tcol as usize]; |
| 14858 | let mi_col1 = tile_info.mi_col_starts[tcol as usize + 1]; |
| 14859 | let (sb_r0, sb_r1) = ( |
| 14860 | (mi_row0 / SB_MI).min(sb_rows), |
| 14861 | mi_row1.div_ceil(SB_MI).min(sb_rows), |
| 14862 | ); |
| 14863 | let (sb_c0, sb_c1) = ( |
| 14864 | (mi_col0 / SB_MI).min(sb_cols), |
| 14865 | mi_col1.div_ceil(SB_MI).min(sb_cols), |
| 14866 | ); |
| 14867 | let mut cdfs = base_cdfs.clone(); |
| 14868 | CURRENT_Q_IDX.with(|c| c.set(i32::from(base_q_idx))); |
| 14869 | QUANT_DELTAS.with(|c| c.set(deltas)); |
| 14870 | CURRENT_DELTA_LF.with(|c| c.set([0; 4])); |
| 14871 | let mut dec = SymbolDecoder::new(data); |
| 14872 | // lane-comppin r9: tile-entry range, the earliest point comparable |
| 14873 | // against aomdec's own `r->ec.rng` right after `aom_reader_init` -- the |
| 14874 | // first ladder rung before any symbol (partition or otherwise) is read. |
| 14875 | if std::env::var_os("EC_AV1_TELL").is_some() { |
| 14876 | eprintln!( |
| 14877 | "TELL label=tile_init tell={} range={}", |
| 14878 | dec.debug_bitpos(), |
| 14879 | dec.debug_state().0 |
| 14880 | ); |
| 14881 | } |
| 14882 | neighbours.start_tile(mi_row0 as usize, mi_col0 as usize, mi_col1 as usize); |
| 14883 | y.set_tile_origin( |
| 14884 | mi_col0 as usize * 4, |
| 14885 | mi_row0 as usize * 4, |
| 14886 | (mi_col1 as usize * 4).min(y.width), |
| 14887 | (mi_row1 as usize * 4).min(y.height), |
| 14888 | ); |
| 14889 | u.set_tile_origin( |
| 14890 | mi_col0 as usize * 2, |
| 14891 | mi_row0 as usize * 2, |
| 14892 | (mi_col1 as usize * 2).min(u.width), |
| 14893 | (mi_row1 as usize * 2).min(u.height), |
| 14894 | ); |
| 14895 | v.set_tile_origin( |
| 14896 | mi_col0 as usize * 2, |
| 14897 | mi_row0 as usize * 2, |
| 14898 | (mi_col1 as usize * 2).min(v.width), |
| 14899 | (mi_row1 as usize * 2).min(v.height), |
| 14900 | ); |
| 14901 | // lane-tiles r6: an MV candidate scan (mvstack.rs's grid.get) must |
| 14902 | // not reach across a tile boundary any more than intra prediction's |
| 14903 | // PlaneBuf reach does -- bounding the shared grid's own read window |
| 14904 | // per tile is equivalent to threading tile bounds through every |
| 14905 | // find_mv_stack* call site (all four read through this one grid). |
| 14906 | grid.set_tile_bounds( |
| 14907 | mi_row0 as usize, |
| 14908 | mi_col0 as usize, |
| 14909 | mi_row1 as usize, |
| 14910 | mi_col1 as usize, |
| 14911 | ); |
| 14912 | TILE_HITS.with(|c| c.set(c.get() + 1)); |
| 14913 | |
| 14914 | for sb_r in sb_r0..sb_r1 { |
| 14915 | neighbours.start_row(); |
| 14916 | for sb_c in sb_c0..sb_c1 { |
| 14917 | crate::restoration::read_lr( |
| 14918 | &mut dec, |
| 14919 | &mut cdfs, |
| 14920 | lr, |
| 14921 | &mut lr_grid, |
| 14922 | &mut lr_reference, |
| 14923 | sb_r * SB_MI, |
| 14924 | sb_c * SB_MI, |
| 14925 | SB_MI, |
| 14926 | ); |
| 14927 | CDEF_TRANSMITTED.with(|c| c.set(false)); |
| 14928 | let sb_at = (sb_r as usize * 4, sb_c as usize * 4); |
| 14929 | let sb_ctx = neighbours.partition_ctx(sb_at, SB); |
| 14930 | let (has_cols, has_rows) = ( |
| 14931 | sb_c * SB_MI + SB_MI / 2 < mi_cols, |
| 14932 | sb_r * SB_MI + SB_MI / 2 < mi_rows, |
| 14933 | ); |
| 14934 | // The straddle cases (one or both halves outside the true |
| 14935 | // frame) never carry a real alphabet symbol -- spec forces |
| 14936 | // SPLIT there (mirrors the intra key-frame tile's own |
| 14937 | // three-way write above). Only the (true, true) case can name |
| 14938 | // a non-SPLIT value, and this loop below only ever recurses as |
| 14939 | // SPLIT -- so a real non-SPLIT part64 here must refuse by name |
| 14940 | // instead of silently decoding as SPLIT and desyncing. |
| 14941 | let part64 = match (has_cols, has_rows) { |
| 14942 | (true, true) => { |
| 14943 | let p = dec.symbol(&mut cdfs.partition_w64[sb_ctx]); |
| 14944 | if std::env::var_os("EC_AV1_TRACE").is_some() { |
| 14945 | eprintln!( |
| 14946 | "TRACE partition_w64 mi=({},{}) ctx={sb_ctx} value={p}", |
| 14947 | sb_r * SB_MI, |
| 14948 | sb_c * SB_MI |
| 14949 | ); |
| 14950 | } |
| 14951 | p |
| 14952 | } |
| 14953 | (true, false) => { |
| 14954 | dec.symbol_fixed(&gather(&cdfs.partition_w64[sb_ctx], VERT_ALIKE)); |
| 14955 | PARTITION_SPLIT |
| 14956 | } |
| 14957 | (false, true) => { |
| 14958 | dec.symbol_fixed(&gather(&cdfs.partition_w64[sb_ctx], HORZ_ALIKE)); |
| 14959 | PARTITION_SPLIT |
| 14960 | } |
| 14961 | (false, false) => PARTITION_SPLIT, |
| 14962 | }; |
| 14963 | if part64 == PARTITION_NONE { |
| 14964 | // lane-inter8 r1: the whole superblock as one 64x64 inter |
| 14965 | // block. `SB`-sized syntax throughout (size_group 3, same as |
| 14966 | // 32x32 -- `size_group_lookup[BLOCK_64X64] == 3`), luma |
| 14967 | // coefficients through `TxbSet::Luma64` with the 32-point |
| 14968 | // scan (TX_64X64 codes only its top-left corner, spec |
| 14969 | // 5.11.40 -- `read_inter_plane` widens the corner back to |
| 14970 | // 64x64 before the inverse transform), chroma as a plain |
| 14971 | // 32x32 `TxbSet::Chroma32`. TX_64X64 carries no `tx_type` |
| 14972 | // symbol at all, so the intra/inter luma sets coincide. |
| 14973 | INTER_SB_NONE_HITS.with(|c| c.set(c.get() + 1)); |
| 14974 | decode_inter_block( |
| 14975 | &mut dec, |
| 14976 | &mut cdfs, |
| 14977 | &mut neighbours, |
| 14978 | &mut grid, |
| 14979 | sb_at, |
| 14980 | SB, |
| 14981 | mi_cols, |
| 14982 | mi_rows, |
| 14983 | &mut y, |
| 14984 | &mut u, |
| 14985 | &mut v, |
| 14986 | &ref_y, |
| 14987 | &ref_u, |
| 14988 | &ref_v, |
| 14989 | &ref_slots, |
| 14990 | &sign_bias_table, |
| 14991 | &global_motion, |
| 14992 | base_q_idx, |
| 14993 | TxbSet::Luma64, |
| 14994 | TxbSet::Luma64, |
| 14995 | TxbSet::Chroma32, |
| 14996 | TX32, |
| 14997 | TX32, |
| 14998 | &scan32, |
| 14999 | &scan32, |
| 15000 | 3, |
| 15001 | allow_high_precision_mv, |
| 15002 | force_integer_mv, |
| 15003 | interp_fixed, |
| 15004 | enable_dual_filter, |
| 15005 | tpl_frame.as_ref(), |
| 15006 | reference_select, |
| 15007 | enable_masked_compound, |
| 15008 | enable_interintra_compound, |
| 15009 | enable_jnt_comp, |
| 15010 | order_hint_bits, |
| 15011 | order_hint, |
| 15012 | ref_order_hints, |
| 15013 | skip_mode_present, |
| 15014 | skip_mode_frame, |
| 15015 | switchable_motion_mode, |
| 15016 | allow_warped_motion, |
| 15017 | false, |
| 15018 | SB, |
| 15019 | SB, |
| 15020 | allow_screen_content_tools, |
| 15021 | frame_width as usize, |
| 15022 | )?; |
| 15023 | continue; |
| 15024 | } |
| 15025 | if part64 != PARTITION_SPLIT { |
| 15026 | return Err(unsupported( |
| 15027 | "an inter SB-level partition type other than NONE or SPLIT (this decoder's \ |
| 15028 | inter tile path recurses a superblock only as SPLIT)", |
| 15029 | )); |
| 15030 | } |
| 15031 | |
| 15032 | for quadrant in 0..4 { |
| 15033 | let (r32, c32) = (sb_r * 2 + quadrant / 2, sb_c * 2 + quadrant % 2); |
| 15034 | if r32 >= rows || c32 >= cols { |
| 15035 | continue; |
| 15036 | } |
| 15037 | let at = (r32 as usize * 2, c32 as usize * 2); |
| 15038 | let ctx32 = neighbours.partition_ctx(at, BLOCK); |
| 15039 | let (has_cols32, has_rows32) = ( |
| 15040 | has_half(c32 * BLOCK_MI, BLOCK_MI, mi_cols), |
| 15041 | has_half(r32 * BLOCK_MI, BLOCK_MI, mi_rows), |
| 15042 | ); |
| 15043 | let part32 = if has_cols32 && has_rows32 { |
| 15044 | if std::env::var_os("EC_AV1_TRACE").is_some() { |
| 15045 | eprintln!( |
| 15046 | "TRACE part32_pre mi=({},{}) rng={}", |
| 15047 | r32 * BLOCK_MI, |
| 15048 | c32 * BLOCK_MI, |
| 15049 | dec.debug_state().0 |
| 15050 | ); |
| 15051 | } |
| 15052 | let p = dec.symbol(&mut cdfs.partition_w32[ctx32]); |
| 15053 | if std::env::var_os("EC_AV1_TRACE").is_some() { |
| 15054 | eprintln!( |
| 15055 | "TRACE partition_w32 mi=({},{}) ctx={ctx32} value={p}", |
| 15056 | r32 * BLOCK_MI, |
| 15057 | c32 * BLOCK_MI |
| 15058 | ); |
| 15059 | } |
| 15060 | p |
| 15061 | } else { |
| 15062 | match (has_cols32, has_rows32) { |
| 15063 | (true, false) => { |
| 15064 | dec.symbol_fixed(&gather(&cdfs.partition_w32[ctx32], VERT_ALIKE)); |
| 15065 | } |
| 15066 | (false, true) => { |
| 15067 | dec.symbol_fixed(&gather(&cdfs.partition_w32[ctx32], HORZ_ALIKE)); |
| 15068 | } |
| 15069 | _ => {} |
| 15070 | } |
| 15071 | PARTITION_SPLIT |
| 15072 | }; |
| 15073 | match part32 { |
| 15074 | PARTITION_NONE => { |
| 15075 | decode_inter_block( |
| 15076 | &mut dec, |
| 15077 | &mut cdfs, |
| 15078 | &mut neighbours, |
| 15079 | &mut grid, |
| 15080 | at, |
| 15081 | BLOCK, |
| 15082 | mi_cols, |
| 15083 | mi_rows, |
| 15084 | &mut y, |
| 15085 | &mut u, |
| 15086 | &mut v, |
| 15087 | &ref_y, |
| 15088 | &ref_u, |
| 15089 | &ref_v, |
| 15090 | &ref_slots, |
| 15091 | &sign_bias_table, |
| 15092 | &global_motion, |
| 15093 | base_q_idx, |
| 15094 | TxbSet::Luma32, |
| 15095 | TxbSet::Luma32Inter, |
| 15096 | TxbSet::Chroma16, |
| 15097 | TX32, |
| 15098 | TX16, |
| 15099 | &scan32, |
| 15100 | &scan16, |
| 15101 | 3, |
| 15102 | allow_high_precision_mv, |
| 15103 | force_integer_mv, |
| 15104 | interp_fixed, |
| 15105 | enable_dual_filter, |
| 15106 | tpl_frame.as_ref(), |
| 15107 | reference_select, |
| 15108 | enable_masked_compound, |
| 15109 | enable_interintra_compound, |
| 15110 | enable_jnt_comp, |
| 15111 | order_hint_bits, |
| 15112 | order_hint, |
| 15113 | ref_order_hints, |
| 15114 | skip_mode_present, |
| 15115 | skip_mode_frame, |
| 15116 | switchable_motion_mode, |
| 15117 | allow_warped_motion, |
| 15118 | false, |
| 15119 | BLOCK, |
| 15120 | BLOCK, |
| 15121 | allow_screen_content_tools, |
| 15122 | frame_width as usize, |
| 15123 | )?; |
| 15124 | } |
| 15125 | PARTITION_SPLIT => { |
| 15126 | for sub in 0..4 { |
| 15127 | let (sr, sc) = (r32 as usize * 2 + sub / 2, c32 as usize * 2 + sub % 2); |
| 15128 | if (sr as u32) * SUB_MI >= mi_rows || (sc as u32) * SUB_MI >= mi_cols { |
| 15129 | continue; |
| 15130 | } |
| 15131 | let (has_cols16, has_rows16) = ( |
| 15132 | has_half(sc as u32 * SUB_MI, SUB_MI, mi_cols), |
| 15133 | has_half(sr as u32 * SUB_MI, SUB_MI, mi_rows), |
| 15134 | ); |
| 15135 | if !has_cols16 && !has_rows16 { |
| 15136 | return Err(unsupported( |
| 15137 | "a 16x16 inter block whose true edge cuts through both \ |
| 15138 | axes needs a rectangular transform this decoder does \ |
| 15139 | not code yet", |
| 15140 | )); |
| 15141 | } |
| 15142 | let at16 = (sr, sc); |
| 15143 | let ctx16 = neighbours.partition_ctx(at16, SUB); |
| 15144 | // lane-inter8 r1: a straddling 16x16 cannot name a |
| 15145 | // value (spec forces SPLIT, one gathered bit); an |
| 15146 | // interior one reads the real alphabet, and |
| 15147 | // `PARTITION_SPLIT` now recurses into four 8x8 |
| 15148 | // inter leaves through the very same loop the |
| 15149 | // straddling case has always used. |
| 15150 | let part16 = if has_cols16 && has_rows16 { |
| 15151 | let p = dec.symbol(&mut cdfs.partition_w16[ctx16]); |
| 15152 | if std::env::var_os("EC_AV1_TRACE").is_some() { |
| 15153 | let (rng, _) = dec.debug_state(); |
| 15154 | eprintln!( |
| 15155 | "TRACE partition_w16 mi=({},{}) ctx={ctx16} value={p} rng={rng}", |
| 15156 | sr as u32 * SUB_MI, |
| 15157 | sc as u32 * SUB_MI |
| 15158 | ); |
| 15159 | } |
| 15160 | p |
| 15161 | } else { |
| 15162 | if has_cols16 { |
| 15163 | dec.symbol_fixed(&gather( |
| 15164 | &cdfs.partition_w16[ctx16], |
| 15165 | VERT_ALIKE, |
| 15166 | )); |
| 15167 | } else { |
| 15168 | dec.symbol_fixed(&gather( |
| 15169 | &cdfs.partition_w16[ctx16], |
| 15170 | HORZ_ALIKE, |
| 15171 | )); |
| 15172 | } |
| 15173 | PARTITION_SPLIT |
| 15174 | }; |
| 15175 | if part16 == PARTITION_NONE { |
| 15176 | decode_inter_block( |
| 15177 | &mut dec, |
| 15178 | &mut cdfs, |
| 15179 | &mut neighbours, |
| 15180 | &mut grid, |
| 15181 | at16, |
| 15182 | SUB, |
| 15183 | mi_cols, |
| 15184 | mi_rows, |
| 15185 | &mut y, |
| 15186 | &mut u, |
| 15187 | &mut v, |
| 15188 | &ref_y, |
| 15189 | &ref_u, |
| 15190 | &ref_v, |
| 15191 | &ref_slots, |
| 15192 | &sign_bias_table, |
| 15193 | &global_motion, |
| 15194 | base_q_idx, |
| 15195 | TxbSet::Luma16, |
| 15196 | if reduced_tx_set { |
| 15197 | TxbSet::Luma16Inter |
| 15198 | } else { |
| 15199 | TxbSet::Luma16InterSet1 |
| 15200 | }, |
| 15201 | TxbSet::Chroma8, |
| 15202 | TX16, |
| 15203 | TX8, |
| 15204 | &scan16, |
| 15205 | &scan8, |
| 15206 | 2, |
| 15207 | allow_high_precision_mv, |
| 15208 | force_integer_mv, |
| 15209 | interp_fixed, |
| 15210 | enable_dual_filter, |
| 15211 | tpl_frame.as_ref(), |
| 15212 | reference_select, |
| 15213 | enable_masked_compound, |
| 15214 | enable_interintra_compound, |
| 15215 | enable_jnt_comp, |
| 15216 | order_hint_bits, |
| 15217 | order_hint, |
| 15218 | ref_order_hints, |
| 15219 | skip_mode_present, |
| 15220 | skip_mode_frame, |
| 15221 | switchable_motion_mode, |
| 15222 | allow_warped_motion, |
| 15223 | false, |
| 15224 | SUB, |
| 15225 | SUB, |
| 15226 | allow_screen_content_tools, |
| 15227 | frame_width as usize, |
| 15228 | )?; |
| 15229 | } else if part16 != PARTITION_SPLIT { |
| 15230 | return Err(unsupported( |
| 15231 | "an inter partition below 16x16 other than SPLIT (16x8/8x16 rect inter leaves are not coded yet)", |
| 15232 | )); |
| 15233 | } else { |
| 15234 | if has_cols16 && has_rows16 { |
| 15235 | INTER_SUB16_SPLIT_HITS.with(|c| c.set(c.get() + 1)); |
| 15236 | } |
| 15237 | let (mi_row0, mi_col0) = (sr as u32 * SUB_MI, sc as u32 * SUB_MI); |
| 15238 | let leaf_positions: Vec<(u32, u32)> = (0..4) |
| 15239 | .map(|i| (mi_row0 + (i / 2) * 2, mi_col0 + (i % 2) * 2)) |
| 15240 | .filter(|&(mr, mc)| mr < mi_rows && mc < mi_cols) |
| 15241 | .collect(); |
| 15242 | // lane-scaledref r1: `decode_inter_block8` IS |
| 15243 | // threaded for a scaled reference now (its |
| 15244 | // single-ref, compound and OBMC MC all take |
| 15245 | // spec 7.11.3.3's scaled walk), but the one |
| 15246 | // recipe that reaches this path at all -- a |
| 15247 | // 64x72 superres fixture whose bottom 16-row |
| 15248 | // band straddles the true edge, so the |
| 15249 | // gathered split bit lands on 8x8 leaves -- |
| 15250 | // DESYNCS inside the leaf itself |
| 15251 | // (`from_switchable_symbol` handed a 4th |
| 15252 | // symbol, mc.rs:200), the same |
| 15253 | // below-8x8 desync lane-sub8 r2 left open and |
| 15254 | // unrelated to scaling. So the scaled MC here |
| 15255 | // is unproven: refuse by name rather than |
| 15256 | // ship a capability claim no gate exercises. |
| 15257 | // Deleting these six lines is the whole lift |
| 15258 | // once the leaf8 desync is fixed. |
| 15259 | if ref_y.width != frame_width as usize |
| 15260 | || ref_slots.iter().flatten().any(|(py, _, _)| { |
| 15261 | py.width != frame_width as usize |
| 15262 | }) |
| 15263 | { |
| 15264 | return Err(unsupported( |
| 15265 | "an 8x8 partition leaf under a scaled reference (superres, unimplemented)", |
| 15266 | )); |
| 15267 | } |
| 15268 | let mut prev_leaves: Vec<((usize, usize), bool, bool, i8, Option<i8>)> = Vec::new(); |
| 15269 | let mut last_compound_ctx: Option<(i8, i8, u8, u8)> = None; |
| 15270 | let mut last_skip_mode = false; |
| 15271 | for (mr, mc) in leaf_positions { |
| 15272 | let leaf_mi = (mr as usize, mc as usize); |
| 15273 | let leaf_ctx = neighbours.partition_ctx_mi(leaf_mi, 8); |
| 15274 | let part8 = dec.symbol(&mut cdfs.partition_w8[leaf_ctx]); |
| 15275 | if part8 != PARTITION_NONE { |
| 15276 | return Err(unsupported( |
| 15277 | "a partition below 8x8 (this decoder codes no leaf smaller than 8x8)", |
| 15278 | )); |
| 15279 | } |
| 15280 | let (skip, is_inter, skip_mode_leaf, compound_ctx8, leaf_refs) = |
| 15281 | decode_inter_block8( |
| 15282 | &mut dec, |
| 15283 | &mut cdfs, |
| 15284 | &mut neighbours, |
| 15285 | &mut grid, |
| 15286 | mi_cols, |
| 15287 | mi_rows, |
| 15288 | at16, |
| 15289 | leaf_mi, |
| 15290 | &mut y, |
| 15291 | &mut u, |
| 15292 | &mut v, |
| 15293 | &ref_y, |
| 15294 | &ref_u, |
| 15295 | &ref_v, |
| 15296 | &ref_slots, |
| 15297 | base_q_idx, |
| 15298 | &scan8, |
| 15299 | &scan4, |
| 15300 | allow_high_precision_mv, |
| 15301 | force_integer_mv, |
| 15302 | reference_select, |
| 15303 | enable_masked_compound, |
| 15304 | enable_interintra_compound, |
| 15305 | enable_jnt_comp, |
| 15306 | order_hint_bits, |
| 15307 | order_hint, |
| 15308 | ref_order_hints, |
| 15309 | skip_mode_present, |
| 15310 | skip_mode_frame, |
| 15311 | reduced_tx_set, |
| 15312 | interp_fixed, |
| 15313 | switchable_motion_mode, |
| 15314 | allow_warped_motion, |
| 15315 | allow_screen_content_tools, |
| 15316 | &sign_bias_table, |
| 15317 | &global_motion, |
| 15318 | tpl_frame.as_ref(), |
| 15319 | frame_width as usize, |
| 15320 | )?; |
| 15321 | prev_leaves.push((leaf_mi, skip, is_inter, leaf_refs.0, leaf_refs.1)); |
| 15322 | last_skip_mode = skip_mode_leaf; |
| 15323 | if compound_ctx8.is_some() { |
| 15324 | last_compound_ctx = compound_ctx8; |
| 15325 | } |
| 15326 | // lane-inter8 r2: stamp THIS leaf's own |
| 15327 | // 2x2-mi span before the next leaf reads |
| 15328 | // it as a neighbour -- the whole-16x16 |
| 15329 | // stamp below only ever described the |
| 15330 | // last leaf, so three quarters of the |
| 15331 | // block carried the wrong skip/ref/inter |
| 15332 | // state into the next block's contexts |
| 15333 | // (class context-read-from-one-cell). |
| 15334 | // corner-cut: `[3, 3]` filter, as the |
| 15335 | // 16x16 stamp used -- `decode_inter_block8` |
| 15336 | // reads no switchable filter yet. |
| 15337 | neighbours.record_inter_rect_mi( |
| 15338 | leaf_mi, |
| 15339 | 2, |
| 15340 | 2, |
| 15341 | skip, |
| 15342 | is_inter, |
| 15343 | leaf_refs.0, |
| 15344 | [3, 3], |
| 15345 | skip_mode_leaf, |
| 15346 | ); |
| 15347 | if let Some(ref1) = leaf_refs.1 |
| 15348 | && let Some((_, _, group_idx, idx)) = compound_ctx8 |
| 15349 | { |
| 15350 | neighbours.record_compound_ctx_rect_mi( |
| 15351 | leaf_mi, 2, 2, ref1, group_idx, idx, |
| 15352 | ); |
| 15353 | } |
| 15354 | } |
| 15355 | // lane-inter8 r2: the four per-leaf |
| 15356 | // `record_inter_rect_mi` stamps above replace |
| 15357 | // the single whole-16x16 one that used to run |
| 15358 | // here with the LAST leaf's state. |
| 15359 | } |
| 15360 | } |
| 15361 | } |
| 15362 | PARTITION_HORZ_B => { |
| 15363 | // lane-warp r5: PARTITION_HORZ_B = 32x32 top strip + |
| 15364 | // 16x16 bottom-left + 16x16 bottom-right. aomenc |
| 15365 | // carves static regions into it; the strip is |
| 15366 | // decoded as a square 32x32 block: for a `skip` |
| 15367 | // block the symbol stream is identical to the true |
| 15368 | // 32x16 coding (no residual, no motion_mode/warp |
| 15369 | // symbol), and the square stamps it leaves over the |
| 15370 | // bottom half (neighbour arrays, skip/lf grids) are |
| 15371 | // re-stamped by the C/D leaves right after. The |
| 15372 | // mi-granular `left_side_mi` rows of a last-block-in- |
| 15373 | // tile strip leak 32-vs-16 only until the per-tile |
| 15374 | // `Neighbours::new` reset. |
| 15375 | EXTENDED_PARTITION_HITS.with(|c| c.set(c.get() + 1)); |
| 15376 | let at32 = at; |
| 15377 | decode_inter_block( |
| 15378 | &mut dec, |
| 15379 | &mut cdfs, |
| 15380 | &mut neighbours, |
| 15381 | &mut grid, |
| 15382 | at32, |
| 15383 | BLOCK, |
| 15384 | mi_cols, |
| 15385 | mi_rows, |
| 15386 | &mut y, |
| 15387 | &mut u, |
| 15388 | &mut v, |
| 15389 | &ref_y, |
| 15390 | &ref_u, |
| 15391 | &ref_v, |
| 15392 | &ref_slots, |
| 15393 | &sign_bias_table, |
| 15394 | &global_motion, |
| 15395 | base_q_idx, |
| 15396 | TxbSet::Luma32, |
| 15397 | TxbSet::Luma32Inter, |
| 15398 | TxbSet::Chroma16, |
| 15399 | TX32, |
| 15400 | TX16, |
| 15401 | &scan32, |
| 15402 | &scan16, |
| 15403 | 3, |
| 15404 | allow_high_precision_mv, |
| 15405 | force_integer_mv, |
| 15406 | interp_fixed, |
| 15407 | enable_dual_filter, |
| 15408 | tpl_frame.as_ref(), |
| 15409 | reference_select, |
| 15410 | enable_masked_compound, |
| 15411 | enable_interintra_compound, |
| 15412 | enable_jnt_comp, |
| 15413 | order_hint_bits, |
| 15414 | order_hint, |
| 15415 | ref_order_hints, |
| 15416 | skip_mode_present, |
| 15417 | skip_mode_frame, |
| 15418 | switchable_motion_mode, |
| 15419 | allow_warped_motion, |
| 15420 | true, |
| 15421 | // lane-rect r2: the top strip's TRUE 32x16 |
| 15422 | // footprint (was BLOCK,BLOCK -- the same square |
| 15423 | // corner-cut rect-flake-1 exposed on HORZ). |
| 15424 | BLOCK, |
| 15425 | SUB, |
| 15426 | allow_screen_content_tools, |
| 15427 | frame_width as usize, |
| 15428 | )?; |
| 15429 | if (r32 * 2 + 1) as u32 * SUB_MI < mi_rows { |
| 15430 | decode_inter_block( |
| 15431 | &mut dec, |
| 15432 | &mut cdfs, |
| 15433 | &mut neighbours, |
| 15434 | &mut grid, |
| 15435 | (r32 as usize * 2 + 1, c32 as usize * 2), |
| 15436 | SUB, |
| 15437 | mi_cols, |
| 15438 | mi_rows, |
| 15439 | &mut y, |
| 15440 | &mut u, |
| 15441 | &mut v, |
| 15442 | &ref_y, |
| 15443 | &ref_u, |
| 15444 | &ref_v, |
| 15445 | &ref_slots, |
| 15446 | &sign_bias_table, |
| 15447 | &global_motion, |
| 15448 | base_q_idx, |
| 15449 | TxbSet::Luma16, |
| 15450 | if reduced_tx_set { |
| 15451 | TxbSet::Luma16Inter |
| 15452 | } else { |
| 15453 | TxbSet::Luma16InterSet1 |
| 15454 | }, |
| 15455 | TxbSet::Chroma8, |
| 15456 | TX16, |
| 15457 | TX8, |
| 15458 | &scan16, |
| 15459 | &scan8, |
| 15460 | 2, |
| 15461 | allow_high_precision_mv, |
| 15462 | force_integer_mv, |
| 15463 | interp_fixed, |
| 15464 | enable_dual_filter, |
| 15465 | tpl_frame.as_ref(), |
| 15466 | reference_select, |
| 15467 | enable_masked_compound, |
| 15468 | enable_interintra_compound, |
| 15469 | enable_jnt_comp, |
| 15470 | order_hint_bits, |
| 15471 | order_hint, |
| 15472 | ref_order_hints, |
| 15473 | skip_mode_present, |
| 15474 | skip_mode_frame, |
| 15475 | switchable_motion_mode, |
| 15476 | allow_warped_motion, |
| 15477 | false, |
| 15478 | SUB, |
| 15479 | SUB, |
| 15480 | allow_screen_content_tools, |
| 15481 | frame_width as usize, |
| 15482 | )?; |
| 15483 | if (c32 * 2 + 1) as u32 * SUB_MI < mi_cols { |
| 15484 | decode_inter_block( |
| 15485 | &mut dec, |
| 15486 | &mut cdfs, |
| 15487 | &mut neighbours, |
| 15488 | &mut grid, |
| 15489 | (r32 as usize * 2 + 1, c32 as usize * 2 + 1), |
| 15490 | SUB, |
| 15491 | mi_cols, |
| 15492 | mi_rows, |
| 15493 | &mut y, |
| 15494 | &mut u, |
| 15495 | &mut v, |
| 15496 | &ref_y, |
| 15497 | &ref_u, |
| 15498 | &ref_v, |
| 15499 | &ref_slots, |
| 15500 | &sign_bias_table, |
| 15501 | &global_motion, |
| 15502 | base_q_idx, |
| 15503 | TxbSet::Luma16, |
| 15504 | if reduced_tx_set { |
| 15505 | TxbSet::Luma16Inter |
| 15506 | } else { |
| 15507 | TxbSet::Luma16InterSet1 |
| 15508 | }, |
| 15509 | TxbSet::Chroma8, |
| 15510 | TX16, |
| 15511 | TX8, |
| 15512 | &scan16, |
| 15513 | &scan8, |
| 15514 | 2, |
| 15515 | allow_high_precision_mv, |
| 15516 | force_integer_mv, |
| 15517 | interp_fixed, |
| 15518 | enable_dual_filter, |
| 15519 | tpl_frame.as_ref(), |
| 15520 | reference_select, |
| 15521 | enable_masked_compound, |
| 15522 | enable_interintra_compound, |
| 15523 | enable_jnt_comp, |
| 15524 | order_hint_bits, |
| 15525 | order_hint, |
| 15526 | ref_order_hints, |
| 15527 | skip_mode_present, |
| 15528 | skip_mode_frame, |
| 15529 | switchable_motion_mode, |
| 15530 | allow_warped_motion, |
| 15531 | false, |
| 15532 | SUB, |
| 15533 | SUB, |
| 15534 | allow_screen_content_tools, |
| 15535 | frame_width as usize, |
| 15536 | )?; |
| 15537 | } |
| 15538 | } |
| 15539 | } |
| 15540 | PARTITION_HORZ => { |
| 15541 | // lane-rect r2: two true 32x16 strips. Both read at |
| 15542 | // `side=BLOCK` (CDF/size-class selection stays square, |
| 15543 | // matching HORZ_B's accepted corner-cut) but bw4/bh4 |
| 15544 | // (mvstack.rs, `motion_mode_eligible`) now derive from |
| 15545 | // `write_w`/`write_h`'s true 32x16 footprint -- the |
| 15546 | // pin's fix (r1's finding). |
| 15547 | RECT_PARTITION_HITS.with(|c| c.set(c.get() + 1)); |
| 15548 | decode_inter_block( |
| 15549 | &mut dec, |
| 15550 | &mut cdfs, |
| 15551 | &mut neighbours, |
| 15552 | &mut grid, |
| 15553 | at, |
| 15554 | BLOCK, |
| 15555 | mi_cols, |
| 15556 | mi_rows, |
| 15557 | &mut y, |
| 15558 | &mut u, |
| 15559 | &mut v, |
| 15560 | &ref_y, |
| 15561 | &ref_u, |
| 15562 | &ref_v, |
| 15563 | &ref_slots, |
| 15564 | &sign_bias_table, |
| 15565 | &global_motion, |
| 15566 | base_q_idx, |
| 15567 | TxbSet::Luma32, |
| 15568 | TxbSet::Luma32Inter, |
| 15569 | TxbSet::Chroma16, |
| 15570 | TX32, |
| 15571 | TX16, |
| 15572 | &scan32, |
| 15573 | &scan16, |
| 15574 | 3, |
| 15575 | allow_high_precision_mv, |
| 15576 | force_integer_mv, |
| 15577 | interp_fixed, |
| 15578 | enable_dual_filter, |
| 15579 | tpl_frame.as_ref(), |
| 15580 | reference_select, |
| 15581 | enable_masked_compound, |
| 15582 | enable_interintra_compound, |
| 15583 | enable_jnt_comp, |
| 15584 | order_hint_bits, |
| 15585 | order_hint, |
| 15586 | ref_order_hints, |
| 15587 | skip_mode_present, |
| 15588 | skip_mode_frame, |
| 15589 | switchable_motion_mode, |
| 15590 | allow_warped_motion, |
| 15591 | true, |
| 15592 | BLOCK, |
| 15593 | SUB, |
| 15594 | allow_screen_content_tools, |
| 15595 | frame_width as usize, |
| 15596 | )?; |
| 15597 | decode_inter_block( |
| 15598 | &mut dec, |
| 15599 | &mut cdfs, |
| 15600 | &mut neighbours, |
| 15601 | &mut grid, |
| 15602 | (r32 as usize * 2 + 1, c32 as usize * 2), |
| 15603 | BLOCK, |
| 15604 | mi_cols, |
| 15605 | mi_rows, |
| 15606 | &mut y, |
| 15607 | &mut u, |
| 15608 | &mut v, |
| 15609 | &ref_y, |
| 15610 | &ref_u, |
| 15611 | &ref_v, |
| 15612 | &ref_slots, |
| 15613 | &sign_bias_table, |
| 15614 | &global_motion, |
| 15615 | base_q_idx, |
| 15616 | TxbSet::Luma32, |
| 15617 | TxbSet::Luma32Inter, |
| 15618 | TxbSet::Chroma16, |
| 15619 | TX32, |
| 15620 | TX16, |
| 15621 | &scan32, |
| 15622 | &scan16, |
| 15623 | 3, |
| 15624 | allow_high_precision_mv, |
| 15625 | force_integer_mv, |
| 15626 | interp_fixed, |
| 15627 | enable_dual_filter, |
| 15628 | tpl_frame.as_ref(), |
| 15629 | reference_select, |
| 15630 | enable_masked_compound, |
| 15631 | enable_interintra_compound, |
| 15632 | enable_jnt_comp, |
| 15633 | order_hint_bits, |
| 15634 | order_hint, |
| 15635 | ref_order_hints, |
| 15636 | skip_mode_present, |
| 15637 | skip_mode_frame, |
| 15638 | switchable_motion_mode, |
| 15639 | allow_warped_motion, |
| 15640 | true, |
| 15641 | BLOCK, |
| 15642 | SUB, |
| 15643 | allow_screen_content_tools, |
| 15644 | frame_width as usize, |
| 15645 | )?; |
| 15646 | } |
| 15647 | PARTITION_VERT => { |
| 15648 | // lane-rect r2: mirror of PARTITION_HORZ above with |
| 15649 | // width/height swapped. |
| 15650 | RECT_PARTITION_HITS.with(|c| c.set(c.get() + 1)); |
| 15651 | decode_inter_block( |
| 15652 | &mut dec, |
| 15653 | &mut cdfs, |
| 15654 | &mut neighbours, |
| 15655 | &mut grid, |
| 15656 | at, |
| 15657 | BLOCK, |
| 15658 | mi_cols, |
| 15659 | mi_rows, |
| 15660 | &mut y, |
| 15661 | &mut u, |
| 15662 | &mut v, |
| 15663 | &ref_y, |
| 15664 | &ref_u, |
| 15665 | &ref_v, |
| 15666 | &ref_slots, |
| 15667 | &sign_bias_table, |
| 15668 | &global_motion, |
| 15669 | base_q_idx, |
| 15670 | TxbSet::Luma32, |
| 15671 | TxbSet::Luma32Inter, |
| 15672 | TxbSet::Chroma16, |
| 15673 | TX32, |
| 15674 | TX16, |
| 15675 | &scan32, |
| 15676 | &scan16, |
| 15677 | 3, |
| 15678 | allow_high_precision_mv, |
| 15679 | force_integer_mv, |
| 15680 | interp_fixed, |
| 15681 | enable_dual_filter, |
| 15682 | tpl_frame.as_ref(), |
| 15683 | reference_select, |
| 15684 | enable_masked_compound, |
| 15685 | enable_interintra_compound, |
| 15686 | enable_jnt_comp, |
| 15687 | order_hint_bits, |
| 15688 | order_hint, |
| 15689 | ref_order_hints, |
| 15690 | skip_mode_present, |
| 15691 | skip_mode_frame, |
| 15692 | switchable_motion_mode, |
| 15693 | allow_warped_motion, |
| 15694 | true, |
| 15695 | SUB, |
| 15696 | BLOCK, |
| 15697 | allow_screen_content_tools, |
| 15698 | frame_width as usize, |
| 15699 | )?; |
| 15700 | decode_inter_block( |
| 15701 | &mut dec, |
| 15702 | &mut cdfs, |
| 15703 | &mut neighbours, |
| 15704 | &mut grid, |
| 15705 | (r32 as usize * 2, c32 as usize * 2 + 1), |
| 15706 | BLOCK, |
| 15707 | mi_cols, |
| 15708 | mi_rows, |
| 15709 | &mut y, |
| 15710 | &mut u, |
| 15711 | &mut v, |
| 15712 | &ref_y, |
| 15713 | &ref_u, |
| 15714 | &ref_v, |
| 15715 | &ref_slots, |
| 15716 | &sign_bias_table, |
| 15717 | &global_motion, |
| 15718 | base_q_idx, |
| 15719 | TxbSet::Luma32, |
| 15720 | TxbSet::Luma32Inter, |
| 15721 | TxbSet::Chroma16, |
| 15722 | TX32, |
| 15723 | TX16, |
| 15724 | &scan32, |
| 15725 | &scan16, |
| 15726 | 3, |
| 15727 | allow_high_precision_mv, |
| 15728 | force_integer_mv, |
| 15729 | interp_fixed, |
| 15730 | enable_dual_filter, |
| 15731 | tpl_frame.as_ref(), |
| 15732 | reference_select, |
| 15733 | enable_masked_compound, |
| 15734 | enable_interintra_compound, |
| 15735 | enable_jnt_comp, |
| 15736 | order_hint_bits, |
| 15737 | order_hint, |
| 15738 | ref_order_hints, |
| 15739 | skip_mode_present, |
| 15740 | skip_mode_frame, |
| 15741 | switchable_motion_mode, |
| 15742 | allow_warped_motion, |
| 15743 | true, |
| 15744 | SUB, |
| 15745 | BLOCK, |
| 15746 | allow_screen_content_tools, |
| 15747 | frame_width as usize, |
| 15748 | )?; |
| 15749 | } |
| 15750 | PARTITION_HORZ_A => { |
| 15751 | // lane-partab r1: two 16x16 squares on top + a true |
| 15752 | // 32x16 strip below (libaom decode_partition |
| 15753 | // PARTITION_HORZ_A: TL, TR, bottom strip). |
| 15754 | PARTAB_HITS.with(|c| c.set(c.get() + 1)); |
| 15755 | decode_inter_block( |
| 15756 | &mut dec, |
| 15757 | &mut cdfs, |
| 15758 | &mut neighbours, |
| 15759 | &mut grid, |
| 15760 | at, |
| 15761 | SUB, |
| 15762 | mi_cols, |
| 15763 | mi_rows, |
| 15764 | &mut y, |
| 15765 | &mut u, |
| 15766 | &mut v, |
| 15767 | &ref_y, |
| 15768 | &ref_u, |
| 15769 | &ref_v, |
| 15770 | &ref_slots, |
| 15771 | &sign_bias_table, |
| 15772 | &global_motion, |
| 15773 | base_q_idx, |
| 15774 | TxbSet::Luma16, |
| 15775 | if reduced_tx_set { |
| 15776 | TxbSet::Luma16Inter |
| 15777 | } else { |
| 15778 | TxbSet::Luma16InterSet1 |
| 15779 | }, |
| 15780 | TxbSet::Chroma8, |
| 15781 | TX16, |
| 15782 | TX8, |
| 15783 | &scan16, |
| 15784 | &scan8, |
| 15785 | 2, |
| 15786 | allow_high_precision_mv, |
| 15787 | force_integer_mv, |
| 15788 | interp_fixed, |
| 15789 | enable_dual_filter, |
| 15790 | tpl_frame.as_ref(), |
| 15791 | reference_select, |
| 15792 | enable_masked_compound, |
| 15793 | enable_interintra_compound, |
| 15794 | enable_jnt_comp, |
| 15795 | order_hint_bits, |
| 15796 | order_hint, |
| 15797 | ref_order_hints, |
| 15798 | skip_mode_present, |
| 15799 | skip_mode_frame, |
| 15800 | switchable_motion_mode, |
| 15801 | allow_warped_motion, |
| 15802 | false, |
| 15803 | SUB, |
| 15804 | SUB, |
| 15805 | allow_screen_content_tools, |
| 15806 | frame_width as usize, |
| 15807 | )?; |
| 15808 | decode_inter_block( |
| 15809 | &mut dec, |
| 15810 | &mut cdfs, |
| 15811 | &mut neighbours, |
| 15812 | &mut grid, |
| 15813 | (r32 as usize * 2, c32 as usize * 2 + 1), |
| 15814 | SUB, |
| 15815 | mi_cols, |
| 15816 | mi_rows, |
| 15817 | &mut y, |
| 15818 | &mut u, |
| 15819 | &mut v, |
| 15820 | &ref_y, |
| 15821 | &ref_u, |
| 15822 | &ref_v, |
| 15823 | &ref_slots, |
| 15824 | &sign_bias_table, |
| 15825 | &global_motion, |
| 15826 | base_q_idx, |
| 15827 | TxbSet::Luma16, |
| 15828 | if reduced_tx_set { |
| 15829 | TxbSet::Luma16Inter |
| 15830 | } else { |
| 15831 | TxbSet::Luma16InterSet1 |
| 15832 | }, |
| 15833 | TxbSet::Chroma8, |
| 15834 | TX16, |
| 15835 | TX8, |
| 15836 | &scan16, |
| 15837 | &scan8, |
| 15838 | 2, |
| 15839 | allow_high_precision_mv, |
| 15840 | force_integer_mv, |
| 15841 | interp_fixed, |
| 15842 | enable_dual_filter, |
| 15843 | tpl_frame.as_ref(), |
| 15844 | reference_select, |
| 15845 | enable_masked_compound, |
| 15846 | enable_interintra_compound, |
| 15847 | enable_jnt_comp, |
| 15848 | order_hint_bits, |
| 15849 | order_hint, |
| 15850 | ref_order_hints, |
| 15851 | skip_mode_present, |
| 15852 | skip_mode_frame, |
| 15853 | switchable_motion_mode, |
| 15854 | allow_warped_motion, |
| 15855 | false, |
| 15856 | SUB, |
| 15857 | SUB, |
| 15858 | allow_screen_content_tools, |
| 15859 | frame_width as usize, |
| 15860 | )?; |
| 15861 | decode_inter_block( |
| 15862 | &mut dec, |
| 15863 | &mut cdfs, |
| 15864 | &mut neighbours, |
| 15865 | &mut grid, |
| 15866 | (r32 as usize * 2 + 1, c32 as usize * 2), |
| 15867 | BLOCK, |
| 15868 | mi_cols, |
| 15869 | mi_rows, |
| 15870 | &mut y, |
| 15871 | &mut u, |
| 15872 | &mut v, |
| 15873 | &ref_y, |
| 15874 | &ref_u, |
| 15875 | &ref_v, |
| 15876 | &ref_slots, |
| 15877 | &sign_bias_table, |
| 15878 | &global_motion, |
| 15879 | base_q_idx, |
| 15880 | TxbSet::Luma32, |
| 15881 | TxbSet::Luma32Inter, |
| 15882 | TxbSet::Chroma16, |
| 15883 | TX32, |
| 15884 | TX16, |
| 15885 | &scan32, |
| 15886 | &scan16, |
| 15887 | 3, |
| 15888 | allow_high_precision_mv, |
| 15889 | force_integer_mv, |
| 15890 | interp_fixed, |
| 15891 | enable_dual_filter, |
| 15892 | tpl_frame.as_ref(), |
| 15893 | reference_select, |
| 15894 | enable_masked_compound, |
| 15895 | enable_interintra_compound, |
| 15896 | enable_jnt_comp, |
| 15897 | order_hint_bits, |
| 15898 | order_hint, |
| 15899 | ref_order_hints, |
| 15900 | skip_mode_present, |
| 15901 | skip_mode_frame, |
| 15902 | switchable_motion_mode, |
| 15903 | allow_warped_motion, |
| 15904 | true, |
| 15905 | BLOCK, |
| 15906 | SUB, |
| 15907 | allow_screen_content_tools, |
| 15908 | frame_width as usize, |
| 15909 | )?; |
| 15910 | } |
| 15911 | PARTITION_VERT_A => { |
| 15912 | // lane-partab r1: mirror of PARTITION_HORZ_A with |
| 15913 | // width/height swapped (TL, BL, right 16x32 strip). |
| 15914 | PARTAB_HITS.with(|c| c.set(c.get() + 1)); |
| 15915 | let _vert_ab = crate::encode::Reach::vert_ab_partition(); |
| 15916 | decode_inter_block( |
| 15917 | &mut dec, |
| 15918 | &mut cdfs, |
| 15919 | &mut neighbours, |
| 15920 | &mut grid, |
| 15921 | at, |
| 15922 | SUB, |
| 15923 | mi_cols, |
| 15924 | mi_rows, |
| 15925 | &mut y, |
| 15926 | &mut u, |
| 15927 | &mut v, |
| 15928 | &ref_y, |
| 15929 | &ref_u, |
| 15930 | &ref_v, |
| 15931 | &ref_slots, |
| 15932 | &sign_bias_table, |
| 15933 | &global_motion, |
| 15934 | base_q_idx, |
| 15935 | TxbSet::Luma16, |
| 15936 | if reduced_tx_set { |
| 15937 | TxbSet::Luma16Inter |
| 15938 | } else { |
| 15939 | TxbSet::Luma16InterSet1 |
| 15940 | }, |
| 15941 | TxbSet::Chroma8, |
| 15942 | TX16, |
| 15943 | TX8, |
| 15944 | &scan16, |
| 15945 | &scan8, |
| 15946 | 2, |
| 15947 | allow_high_precision_mv, |
| 15948 | force_integer_mv, |
| 15949 | interp_fixed, |
| 15950 | enable_dual_filter, |
| 15951 | tpl_frame.as_ref(), |
| 15952 | reference_select, |
| 15953 | enable_masked_compound, |
| 15954 | enable_interintra_compound, |
| 15955 | enable_jnt_comp, |
| 15956 | order_hint_bits, |
| 15957 | order_hint, |
| 15958 | ref_order_hints, |
| 15959 | skip_mode_present, |
| 15960 | skip_mode_frame, |
| 15961 | switchable_motion_mode, |
| 15962 | allow_warped_motion, |
| 15963 | false, |
| 15964 | SUB, |
| 15965 | SUB, |
| 15966 | allow_screen_content_tools, |
| 15967 | frame_width as usize, |
| 15968 | )?; |
| 15969 | decode_inter_block( |
| 15970 | &mut dec, |
| 15971 | &mut cdfs, |
| 15972 | &mut neighbours, |
| 15973 | &mut grid, |
| 15974 | (r32 as usize * 2 + 1, c32 as usize * 2), |
| 15975 | SUB, |
| 15976 | mi_cols, |
| 15977 | mi_rows, |
| 15978 | &mut y, |
| 15979 | &mut u, |
| 15980 | &mut v, |
| 15981 | &ref_y, |
| 15982 | &ref_u, |
| 15983 | &ref_v, |
| 15984 | &ref_slots, |
| 15985 | &sign_bias_table, |
| 15986 | &global_motion, |
| 15987 | base_q_idx, |
| 15988 | TxbSet::Luma16, |
| 15989 | if reduced_tx_set { |
| 15990 | TxbSet::Luma16Inter |
| 15991 | } else { |
| 15992 | TxbSet::Luma16InterSet1 |
| 15993 | }, |
| 15994 | TxbSet::Chroma8, |
| 15995 | TX16, |
| 15996 | TX8, |
| 15997 | &scan16, |
| 15998 | &scan8, |
| 15999 | 2, |
| 16000 | allow_high_precision_mv, |
| 16001 | force_integer_mv, |
| 16002 | interp_fixed, |
| 16003 | enable_dual_filter, |
| 16004 | tpl_frame.as_ref(), |
| 16005 | reference_select, |
| 16006 | enable_masked_compound, |
| 16007 | enable_interintra_compound, |
| 16008 | enable_jnt_comp, |
| 16009 | order_hint_bits, |
| 16010 | order_hint, |
| 16011 | ref_order_hints, |
| 16012 | skip_mode_present, |
| 16013 | skip_mode_frame, |
| 16014 | switchable_motion_mode, |
| 16015 | allow_warped_motion, |
| 16016 | false, |
| 16017 | SUB, |
| 16018 | SUB, |
| 16019 | allow_screen_content_tools, |
| 16020 | frame_width as usize, |
| 16021 | )?; |
| 16022 | decode_inter_block( |
| 16023 | &mut dec, |
| 16024 | &mut cdfs, |
| 16025 | &mut neighbours, |
| 16026 | &mut grid, |
| 16027 | (r32 as usize * 2, c32 as usize * 2 + 1), |
| 16028 | BLOCK, |
| 16029 | mi_cols, |
| 16030 | mi_rows, |
| 16031 | &mut y, |
| 16032 | &mut u, |
| 16033 | &mut v, |
| 16034 | &ref_y, |
| 16035 | &ref_u, |
| 16036 | &ref_v, |
| 16037 | &ref_slots, |
| 16038 | &sign_bias_table, |
| 16039 | &global_motion, |
| 16040 | base_q_idx, |
| 16041 | TxbSet::Luma32, |
| 16042 | TxbSet::Luma32Inter, |
| 16043 | TxbSet::Chroma16, |
| 16044 | TX32, |
| 16045 | TX16, |
| 16046 | &scan32, |
| 16047 | &scan16, |
| 16048 | 3, |
| 16049 | allow_high_precision_mv, |
| 16050 | force_integer_mv, |
| 16051 | interp_fixed, |
| 16052 | enable_dual_filter, |
| 16053 | tpl_frame.as_ref(), |
| 16054 | reference_select, |
| 16055 | enable_masked_compound, |
| 16056 | enable_interintra_compound, |
| 16057 | enable_jnt_comp, |
| 16058 | order_hint_bits, |
| 16059 | order_hint, |
| 16060 | ref_order_hints, |
| 16061 | skip_mode_present, |
| 16062 | skip_mode_frame, |
| 16063 | switchable_motion_mode, |
| 16064 | allow_warped_motion, |
| 16065 | true, |
| 16066 | SUB, |
| 16067 | BLOCK, |
| 16068 | allow_screen_content_tools, |
| 16069 | frame_width as usize, |
| 16070 | )?; |
| 16071 | } |
| 16072 | PARTITION_VERT_B => { |
| 16073 | // lane-partab r1: true 16x32 strip on the left + two |
| 16074 | // 16x16 squares on the right (libaom decode_partition |
| 16075 | // PARTITION_VERT_B: left strip, TR, BR). |
| 16076 | PARTAB_HITS.with(|c| c.set(c.get() + 1)); |
| 16077 | let _vert_ab = crate::encode::Reach::vert_ab_partition(); |
| 16078 | decode_inter_block( |
| 16079 | &mut dec, |
| 16080 | &mut cdfs, |
| 16081 | &mut neighbours, |
| 16082 | &mut grid, |
| 16083 | at, |
| 16084 | BLOCK, |
| 16085 | mi_cols, |
| 16086 | mi_rows, |
| 16087 | &mut y, |
| 16088 | &mut u, |
| 16089 | &mut v, |
| 16090 | &ref_y, |
| 16091 | &ref_u, |
| 16092 | &ref_v, |
| 16093 | &ref_slots, |
| 16094 | &sign_bias_table, |
| 16095 | &global_motion, |
| 16096 | base_q_idx, |
| 16097 | TxbSet::Luma32, |
| 16098 | TxbSet::Luma32Inter, |
| 16099 | TxbSet::Chroma16, |
| 16100 | TX32, |
| 16101 | TX16, |
| 16102 | &scan32, |
| 16103 | &scan16, |
| 16104 | 3, |
| 16105 | allow_high_precision_mv, |
| 16106 | force_integer_mv, |
| 16107 | interp_fixed, |
| 16108 | enable_dual_filter, |
| 16109 | tpl_frame.as_ref(), |
| 16110 | reference_select, |
| 16111 | enable_masked_compound, |
| 16112 | enable_interintra_compound, |
| 16113 | enable_jnt_comp, |
| 16114 | order_hint_bits, |
| 16115 | order_hint, |
| 16116 | ref_order_hints, |
| 16117 | skip_mode_present, |
| 16118 | skip_mode_frame, |
| 16119 | switchable_motion_mode, |
| 16120 | allow_warped_motion, |
| 16121 | true, |
| 16122 | SUB, |
| 16123 | BLOCK, |
| 16124 | allow_screen_content_tools, |
| 16125 | frame_width as usize, |
| 16126 | )?; |
| 16127 | decode_inter_block( |
| 16128 | &mut dec, |
| 16129 | &mut cdfs, |
| 16130 | &mut neighbours, |
| 16131 | &mut grid, |
| 16132 | (r32 as usize * 2, c32 as usize * 2 + 1), |
| 16133 | SUB, |
| 16134 | mi_cols, |
| 16135 | mi_rows, |
| 16136 | &mut y, |
| 16137 | &mut u, |
| 16138 | &mut v, |
| 16139 | &ref_y, |
| 16140 | &ref_u, |
| 16141 | &ref_v, |
| 16142 | &ref_slots, |
| 16143 | &sign_bias_table, |
| 16144 | &global_motion, |
| 16145 | base_q_idx, |
| 16146 | TxbSet::Luma16, |
| 16147 | if reduced_tx_set { |
| 16148 | TxbSet::Luma16Inter |
| 16149 | } else { |
| 16150 | TxbSet::Luma16InterSet1 |
| 16151 | }, |
| 16152 | TxbSet::Chroma8, |
| 16153 | TX16, |
| 16154 | TX8, |
| 16155 | &scan16, |
| 16156 | &scan8, |
| 16157 | 2, |
| 16158 | allow_high_precision_mv, |
| 16159 | force_integer_mv, |
| 16160 | interp_fixed, |
| 16161 | enable_dual_filter, |
| 16162 | tpl_frame.as_ref(), |
| 16163 | reference_select, |
| 16164 | enable_masked_compound, |
| 16165 | enable_interintra_compound, |
| 16166 | enable_jnt_comp, |
| 16167 | order_hint_bits, |
| 16168 | order_hint, |
| 16169 | ref_order_hints, |
| 16170 | skip_mode_present, |
| 16171 | skip_mode_frame, |
| 16172 | switchable_motion_mode, |
| 16173 | allow_warped_motion, |
| 16174 | false, |
| 16175 | SUB, |
| 16176 | SUB, |
| 16177 | allow_screen_content_tools, |
| 16178 | frame_width as usize, |
| 16179 | )?; |
| 16180 | decode_inter_block( |
| 16181 | &mut dec, |
| 16182 | &mut cdfs, |
| 16183 | &mut neighbours, |
| 16184 | &mut grid, |
| 16185 | (r32 as usize * 2 + 1, c32 as usize * 2 + 1), |
| 16186 | SUB, |
| 16187 | mi_cols, |
| 16188 | mi_rows, |
| 16189 | &mut y, |
| 16190 | &mut u, |
| 16191 | &mut v, |
| 16192 | &ref_y, |
| 16193 | &ref_u, |
| 16194 | &ref_v, |
| 16195 | &ref_slots, |
| 16196 | &sign_bias_table, |
| 16197 | &global_motion, |
| 16198 | base_q_idx, |
| 16199 | TxbSet::Luma16, |
| 16200 | if reduced_tx_set { |
| 16201 | TxbSet::Luma16Inter |
| 16202 | } else { |
| 16203 | TxbSet::Luma16InterSet1 |
| 16204 | }, |
| 16205 | TxbSet::Chroma8, |
| 16206 | TX16, |
| 16207 | TX8, |
| 16208 | &scan16, |
| 16209 | &scan8, |
| 16210 | 2, |
| 16211 | allow_high_precision_mv, |
| 16212 | force_integer_mv, |
| 16213 | interp_fixed, |
| 16214 | enable_dual_filter, |
| 16215 | tpl_frame.as_ref(), |
| 16216 | reference_select, |
| 16217 | enable_masked_compound, |
| 16218 | enable_interintra_compound, |
| 16219 | enable_jnt_comp, |
| 16220 | order_hint_bits, |
| 16221 | order_hint, |
| 16222 | ref_order_hints, |
| 16223 | skip_mode_present, |
| 16224 | skip_mode_frame, |
| 16225 | switchable_motion_mode, |
| 16226 | allow_warped_motion, |
| 16227 | false, |
| 16228 | SUB, |
| 16229 | SUB, |
| 16230 | allow_screen_content_tools, |
| 16231 | frame_width as usize, |
| 16232 | )?; |
| 16233 | } |
| 16234 | _ => { |
| 16235 | return Err(unsupported(format!( |
| 16236 | "an INTER 32x32 partition type this decoder does not code (value={part32})" |
| 16237 | ))); |
| 16238 | } |
| 16239 | } |
| 16240 | } |
| 16241 | } |
| 16242 | } |
| 16243 | |
| 16244 | if tile_num == tile_info.context_update_tile_id { |
| 16245 | result_cdfs = cdfs; |
| 16246 | } |
| 16247 | } |
| 16248 | |
| 16249 | // lane-comppin r4: pre-loop-filter decode-order dump, matching aomdec's |
| 16250 | // own EC_AV1_PREFILT_DUMP shape (decodeframe.c ~5451) -- diffs against |
| 16251 | // that isolate whether a decode-order frame's mismatch already exists |
| 16252 | // in reconstruction (this dump) vs is introduced by the loop filter |
| 16253 | // (EC_AV1_DECODE_ORDER_DUMP's post-filter dump). |
| 16254 | if let Ok(path) = std::env::var("EC_AV1_PREFILT_DUMP") { |
| 16255 | use std::io::Write; |
| 16256 | let idx = PREFILT_PICTURE_IDX.fetch_add(1, std::sync::atomic::Ordering::SeqCst); |
| 16257 | if let Ok(mut f) = std::fs::File::create(format!("{path}.f{idx}")) { |
| 16258 | let narrow = |p: &PlaneBuf| -> Vec<u8> { p.data.iter().map(|&s| s as u8).collect() }; |
| 16259 | let _ = f.write_all(&narrow(&y)); |
| 16260 | let _ = f.write_all(&narrow(&u)); |
| 16261 | let _ = f.write_all(&narrow(&v)); |
| 16262 | } |
| 16263 | } else { |
| 16264 | PREFILT_PICTURE_IDX.fetch_add(1, std::sync::atomic::Ordering::SeqCst); |
| 16265 | } |
| 16266 | apply_deblock( |
| 16267 | &mut y, |
| 16268 | &mut u, |
| 16269 | &mut v, |
| 16270 | loop_filter, |
| 16271 | &neighbours, |
| 16272 | frame_width as usize, |
| 16273 | frame_height as usize, |
| 16274 | ); |
| 16275 | let (deblocked_y, deblocked_u, deblocked_v) = (y.clone(), u.clone(), v.clone()); |
| 16276 | // lane-tiny r4: post-deblock / post-CDEF dumps mirroring aomdec's own |
| 16277 | // EC_AV1_POSTDEBLOCK_DUMP (decodeframe.c ~5404) -- with the pre-filter |
| 16278 | // dump above these bisect WHICH filter stage introduced a mismatch. |
| 16279 | dump_stage("EC_AV1_POSTDEBLOCK_DUMP", &y, &u, &v); |
| 16280 | apply_cdef(&mut y, &mut u, &mut v, cdef, &neighbours); |
| 16281 | dump_stage("EC_AV1_POSTCDEF_DUMP", &y, &u, &v); |
| 16282 | apply_loop_restoration(&mut y, &mut u, &mut v, &deblocked_y, &deblocked_u, &deblocked_v, lr, &lr_grid); |
| 16283 | |
| 16284 | let motion_field = build_motion_field( |
| 16285 | &grid, |
| 16286 | mi_cols as usize, |
| 16287 | mi_rows as usize, |
| 16288 | order_hint, |
| 16289 | ref_order_hints, |
| 16290 | ); |
| 16291 | |
| 16292 | let (fw, fh) = (frame_width as usize, frame_height as usize); |
| 16293 | if fw == width && fh == height { |
| 16294 | LAST_FRAME_WIDE_MARGIN.with(|m| *m.borrow_mut() = None); |
| 16295 | return Ok(( |
| 16296 | Picture { |
| 16297 | width, |
| 16298 | height, |
| 16299 | y: y.data, |
| 16300 | u: u.data, |
| 16301 | v: v.data, |
| 16302 | }, |
| 16303 | result_cdfs, |
| 16304 | motion_field, |
| 16305 | )); |
| 16306 | } |
| 16307 | let crop = |plane: &PlaneBuf, w: usize, h: usize| -> Vec<u16> { |
| 16308 | let mut out = Vec::with_capacity(w * h); |
| 16309 | for row in 0..h { |
| 16310 | out.extend(plane.data[row * plane.width..][..w].iter().copied()); |
| 16311 | } |
| 16312 | out |
| 16313 | }; |
| 16314 | // lane-superres r10 (fixed r9): same real-margin stash as |
| 16315 | // `decode_key_frame_tile_with_cdfs` (see that function's comment) -- the |
| 16316 | // real reconstructed extent is the mi-aligned `true_width`/`true_height` |
| 16317 | // (`mi_cols`/`mi_rows` * 4), NOT the superblock-padded `width`/`height` |
| 16318 | // this branch was reading before: columns `[true_width, width)` were |
| 16319 | // never actually coded (the last partial superblock stops at |
| 16320 | // `true_width`), so stashing out to `width` handed the chroma upscale's |
| 16321 | // right-edge replicate a slice of uninitialized/stale buffer one column |
| 16322 | // early -- the same shorten-vs-replicate shape as `av1-truesize`. The |
| 16323 | // key-frame branch above already used `true_width`/`true_height`; this |
| 16324 | // just matches it. |
| 16325 | LAST_FRAME_WIDE_MARGIN.with(|m| { |
| 16326 | *m.borrow_mut() = if true_width > fw || true_height > fh { |
| 16327 | Some(Picture { |
| 16328 | width: true_width, |
| 16329 | height: true_height, |
| 16330 | y: crop(&y, true_width, true_height), |
| 16331 | u: crop(&u, true_width.div_ceil(2), true_height.div_ceil(2)), |
| 16332 | v: crop(&v, true_width.div_ceil(2), true_height.div_ceil(2)), |
| 16333 | }) |
| 16334 | } else { |
| 16335 | None |
| 16336 | } |
| 16337 | }); |
| 16338 | Ok(( |
| 16339 | Picture { |
| 16340 | width: fw, |
| 16341 | height: fh, |
| 16342 | y: crop(&y, fw, fh), |
| 16343 | u: crop(&u, fw.div_ceil(2), fh.div_ceil(2)), |
| 16344 | v: crop(&v, fw.div_ceil(2), fh.div_ceil(2)), |
| 16345 | }, |
| 16346 | result_cdfs, |
| 16347 | motion_field, |
| 16348 | )) |
| 16349 | } |
| 16350 | |
| 16351 | #[cfg(test)] |
| 16352 | mod tests { |
| 16353 | use super::*; |
| 16354 | |
| 16355 | /// lane-rectsplit r2: `av1_nz_map_ctx_offset` is packed COLUMN-major, so |
| 16356 | /// the two rect transcriptions in [`cdf`] must be read `[col][row]`. |
| 16357 | /// [`base_ctx_rect`] states libaom's generating rule |
| 16358 | /// (`txb_common.h:199-209`) in display coordinates; this pins the tables |
| 16359 | /// against it, which is what a transposed read would break. |
| 16360 | #[test] |
| 16361 | fn nz_map_ctx_offset_tables_match_the_rect_rule() { |
| 16362 | for (w, h, table) in [ |
| 16363 | (32usize, 64usize, &cdf::NZ_MAP_CTX_OFFSET_32X64), |
| 16364 | (64, 32, &cdf::NZ_MAP_CTX_OFFSET_64X32), |
| 16365 | ] { |
| 16366 | for row in 0..5usize { |
| 16367 | for col in 0..5usize { |
| 16368 | let want = if row == 0 && col == 0 { |
| 16369 | 0 |
| 16370 | } else if w < h && row < 2 { |
| 16371 | 11 |
| 16372 | } else if w > h && col < 2 { |
| 16373 | 16 |
| 16374 | } else { |
| 16375 | cdf::NZ_MAP_CTX_OFFSET_32[row][col] |
| 16376 | }; |
| 16377 | assert_eq!( |
| 16378 | table[col][row], |
| 16379 | want, |
| 16380 | "{w}x{h} nz_map offset at display (row {row}, col {col})" |
| 16381 | ); |
| 16382 | } |
| 16383 | } |
| 16384 | } |
| 16385 | } |
| 16386 | |
| 16387 | // `crate::tile`'s `flat_key_frame_tile`/`dc_key_frame_tile_levels`/ |
| 16388 | // `split_dc_key_frame_tile` are not exercised here: they are synthetic |
| 16389 | // single-purpose writers with their own skip-context rule (keyed to |
| 16390 | // superblock position, not the neighbour-tracked one every real block |
| 16391 | // uses), never reached by `encode_key_frame_with_modes` — decoding them |
| 16392 | // would need a second, throwaway context convention next to the real |
| 16393 | // one below. `sb_coeff_key_frame_tile`, what the real encoder writes, is |
| 16394 | // this decoder's one target, and the round-trip test below is against |
| 16395 | // that path with real quantised residual, not a synthetic all-DC frame. |
| 16396 | |
| 16397 | /// `is_uni_comp_ref` (lane-av1comp): a pair is unidirectional exactly |
| 16398 | /// when both references sit on the same temporal side -- the three |
| 16399 | /// `uni_comp_ref` bitstream pairs (LAST/LAST2, LAST/LAST3, LAST/GOLDEN) |
| 16400 | /// all read `true`, a forward/backward mix (LAST/BWDREF) reads `false`, |
| 16401 | /// and a same-side non-`uni_comp_ref` pair (BWDREF/ALTREF) still reads |
| 16402 | /// `true` (spec `has_uni_comp_refs` is symmetric in the reference set, |
| 16403 | /// not just the three enumerated syntax pairs). |
| 16404 | #[test] |
| 16405 | fn is_uni_comp_ref_matches_forward_backward_sides() { |
| 16406 | assert!(is_uni_comp_ref(LAST_FRAME, LAST2_FRAME)); |
| 16407 | assert!(is_uni_comp_ref(LAST_FRAME, LAST3_FRAME)); |
| 16408 | assert!(is_uni_comp_ref(LAST_FRAME, GOLDEN_FRAME)); |
| 16409 | assert!(is_uni_comp_ref(BWDREF_FRAME, ALTREF_FRAME)); |
| 16410 | assert!(!is_uni_comp_ref(LAST_FRAME, BWDREF_FRAME)); |
| 16411 | assert!(!is_uni_comp_ref(GOLDEN_FRAME, ALTREF_FRAME)); |
| 16412 | } |
| 16413 | |
| 16414 | /// [`Neighbours::record_compound_ctx`] stamps `above_ref1`/`left_ref1` |
| 16415 | /// with the real second reference (lane-av1comp round 12: previously |
| 16416 | /// always cleared to `None` by `record_inter`, so the next block's own |
| 16417 | /// `NeighbourRef::uni` -- and `comp_reference_type_ctx` downstream of it |
| 16418 | /// -- could never see a real compound neighbour). |
| 16419 | #[test] |
| 16420 | fn record_compound_ctx_stamps_ref1_for_the_next_block_to_read() { |
| 16421 | let mut n = Neighbours::new(4, 4, 16, 16); |
| 16422 | n.record_inter((0, 0), SUB, false, true, LAST_FRAME, [3, 3], false); |
| 16423 | assert_eq!(n.above_ref1[0], None, "record_inter alone never sets ref1"); |
| 16424 | n.record_compound_ctx((0, 0), SUB, ALTREF_FRAME, 0, 1); |
| 16425 | assert_eq!(n.above_ref1[0], Some(ALTREF_FRAME)); |
| 16426 | assert_eq!(n.left_ref1[0], Some(ALTREF_FRAME)); |
| 16427 | assert!(!is_uni_comp_ref(LAST_FRAME, n.above_ref1[0].unwrap())); |
| 16428 | } |
| 16429 | |
| 16430 | /// [`cdf::COMPOUND_MODE_CTX_MAP`] folded by hand against libaom's |
| 16431 | /// `av1_mode_context_analyzer` compound branch (`mvref_common.h`): |
| 16432 | /// `comp_ctx = compound_mode_ctx_map[refmv_ctx >> 1][min(newmv_ctx, 4)]` |
| 16433 | /// -- a few corner and interior points, not the whole 6x6 grid. |
| 16434 | #[test] |
| 16435 | fn compound_mode_ctx_map_matches_libaom_corners() { |
| 16436 | assert_eq!(cdf::COMPOUND_MODE_CTX_MAP[0][0], 0); |
| 16437 | assert_eq!(cdf::COMPOUND_MODE_CTX_MAP[0][4], 1); |
| 16438 | assert_eq!(cdf::COMPOUND_MODE_CTX_MAP[1][0], 1); |
| 16439 | assert_eq!(cdf::COMPOUND_MODE_CTX_MAP[1][3], 4); |
| 16440 | assert_eq!(cdf::COMPOUND_MODE_CTX_MAP[2][2], 5); |
| 16441 | assert_eq!(cdf::COMPOUND_MODE_CTX_MAP[2][4], 7); |
| 16442 | // `ref_mv_ctx` up to 5 (`>> 1` = 2, the map's last row) and |
| 16443 | // `new_mv_ctx` up to 5 (clamped to 4, `COMP_NEWMV_CTXS - 1`) are |
| 16444 | // exactly the ranges `find_mv_stack_compound` produces. |
| 16445 | assert_eq!(cdf::COMPOUND_MODE_CTX_MAP[5 >> 1][5usize.min(4)], 7); |
| 16446 | } |
| 16447 | |
| 16448 | #[test] |
| 16449 | fn a_frame_with_no_mode_info_grid_is_refused() { |
| 16450 | assert!( |
| 16451 | decode_key_frame_tile( |
| 16452 | &[0u8; 4], |
| 16453 | 0, |
| 16454 | 32, |
| 16455 | 32, |
| 16456 | 0, |
| 16457 | 128, |
| 16458 | false, |
| 16459 | &CdefParams::default(), |
| 16460 | &LoopFilterParams::default(), |
| 16461 | false, |
| 16462 | true, |
| 16463 | false, |
| 16464 | false, |
| 16465 | ) |
| 16466 | .is_err() |
| 16467 | ); |
| 16468 | } |
| 16469 | |
| 16470 | /// Encodes and decodes one picture with `modes`, asserting the decoder's |
| 16471 | /// planes are byte-exact against the encoder's own reconstruction. |
| 16472 | fn round_trips(w: usize, h: usize, modes: &[u8]) { |
| 16473 | use crate::encode::{Encoded, Picture as Pic}; |
| 16474 | let mut source = vec![0u16; w * h]; |
| 16475 | for row in 0..h { |
| 16476 | for col in 0..w { |
| 16477 | source[row * w + col] = ((row * 3 + col * 5) % 251) as u16; |
| 16478 | } |
| 16479 | } |
| 16480 | let picture = Pic { |
| 16481 | width: w, |
| 16482 | height: h, |
| 16483 | y: source, |
| 16484 | u: vec![128; w * h / 4], |
| 16485 | v: vec![128; w * h / 4], |
| 16486 | }; |
| 16487 | let Encoded { |
| 16488 | tile, |
| 16489 | reconstruction, |
| 16490 | mi_cols, |
| 16491 | mi_rows, |
| 16492 | base_q_idx, |
| 16493 | .. |
| 16494 | }: Encoded = crate::encode::encode_key_frame_with_modes(&picture, 40, 0.0, modes).unwrap(); |
| 16495 | let decoded = decode_key_frame_tile( |
| 16496 | &tile, |
| 16497 | mi_cols, |
| 16498 | mi_rows, |
| 16499 | base_q_idx, |
| 16500 | w as u32, |
| 16501 | h as u32, |
| 16502 | false, |
| 16503 | &CdefParams::default(), |
| 16504 | &LoopFilterParams::default(), |
| 16505 | false, |
| 16506 | true, |
| 16507 | false, |
| 16508 | false, |
| 16509 | ) |
| 16510 | .unwrap(); |
| 16511 | assert_eq!(decoded.width, w, "{w}x{h}: decoded width"); |
| 16512 | assert_eq!(decoded.height, h, "{w}x{h}: decoded height"); |
| 16513 | assert_eq!(decoded.y, reconstruction.y, "{w}x{h} luma mismatch"); |
| 16514 | assert_eq!(decoded.u, reconstruction.u, "{w}x{h} chroma U mismatch"); |
| 16515 | assert_eq!(decoded.v, reconstruction.v, "{w}x{h} chroma V mismatch"); |
| 16516 | } |
| 16517 | |
| 16518 | #[test] |
| 16519 | fn real_yuv_round_trips_bit_exact_against_the_encoder_reconstruction() { |
| 16520 | for (w, h) in [(128usize, 128usize), (192, 128)] { |
| 16521 | round_trips(w, h, &[DC_PRED as u8]); |
| 16522 | } |
| 16523 | } |
| 16524 | |
| 16525 | /// Every one of the thirteen key-frame intra modes, at their base angle, |
| 16526 | /// on real (non-flat) content: the mode search picks whichever wins each |
| 16527 | /// block, so this exercises the directional edge/reach plumbing this |
| 16528 | /// decoder round adds, not just `DC_PRED`. |
| 16529 | #[test] |
| 16530 | fn every_intra_mode_round_trips_bit_exact_against_the_encoder_reconstruction() { |
| 16531 | for (w, h) in [(128usize, 128usize), (192, 128)] { |
| 16532 | round_trips(w, h, &crate::intra::KEY_FRAME_MODES); |
| 16533 | } |
| 16534 | } |
| 16535 | |
| 16536 | /// Sizes not a whole number of 64x64 superblocks (nor even of 32x32 |
| 16537 | /// blocks, for 854x480): the gathered-CDF partial-superblock/32x32 path |
| 16538 | /// this decoder round adds. |
| 16539 | #[test] |
| 16540 | fn odd_sizes_round_trip_bit_exact_against_the_encoder_reconstruction() { |
| 16541 | for (w, h) in [(854usize, 480usize), (216, 96)] { |
| 16542 | round_trips(w, h, &crate::intra::KEY_FRAME_MODES); |
| 16543 | } |
| 16544 | } |
| 16545 | |
| 16546 | /// mod-32==8 sizes (lane-av1-rect): the true frame edge falls inside a |
| 16547 | /// 16x16 block itself, forcing the encoder's 8x8-leaf straddle path |
| 16548 | /// ([`crate::tile::write_leaf8`]) regardless of the RD-cost guard, since |
| 16549 | /// no whole 16x16/32x32 legally covers the true edge here -- this decoder |
| 16550 | /// round's leaf8 read path (`decode_leaf8`, gathered `partition_w16` split |
| 16551 | /// bit, `partition_w8` leaves). |
| 16552 | #[test] |
| 16553 | fn leaf8_straddle_sizes_round_trip_bit_exact_against_the_encoder_reconstruction() { |
| 16554 | for (w, h) in [(40usize, 32usize), (640, 360)] { |
| 16555 | round_trips(w, h, &crate::intra::KEY_FRAME_MODES); |
| 16556 | } |
| 16557 | } |
| 16558 | |
| 16559 | /// A skipped block codes no residual syntax at all (spec 5.11.34): |
| 16560 | /// straight prediction on every plane. The real key-frame writer never |
| 16561 | /// emits `skip = 1` ([`crate::tile::write_intra_mode`] hardcodes it to |
| 16562 | /// `0`), so this constructs the exact symbol sequence a writer that did |
| 16563 | /// emit it would produce, by hand, and asserts the decoded reconstruction |
| 16564 | /// is pure DC prediction (mid-grey, with no neighbours) rather than |
| 16565 | /// refusing. |
| 16566 | #[test] |
| 16567 | fn a_skipped_block_decodes_to_pure_prediction() { |
| 16568 | use crate::msac::SymbolEncoder; |
| 16569 | let mut cdfs = Cdfs::new(q_ctx_of(40)); |
| 16570 | let mut enc = SymbolEncoder::new(); |
| 16571 | // One whole, unsplit 64x64 superblock, coded skip = true. |
| 16572 | enc.symbol(PARTITION_NONE, &mut cdfs.partition_w64[0]); |
| 16573 | enc.symbol(1, &mut cdfs.skip[0]); |
| 16574 | enc.symbol( |
| 16575 | DC_PRED, |
| 16576 | &mut cdfs.kf_y_mode[INTRA_MODE_CTX[DC_PRED]][INTRA_MODE_CTX[DC_PRED]], |
| 16577 | ); |
| 16578 | enc.symbol(DC_PRED, &mut cdfs.uv_mode_no_cfl[DC_PRED]); |
| 16579 | let data = enc.finish(); |
| 16580 | let decoded = decode_key_frame_tile( |
| 16581 | &data, |
| 16582 | 16, |
| 16583 | 16, |
| 16584 | 40, |
| 16585 | 64, |
| 16586 | 64, |
| 16587 | false, |
| 16588 | &CdefParams::default(), |
| 16589 | &LoopFilterParams::default(), |
| 16590 | false, |
| 16591 | true, |
| 16592 | false, |
| 16593 | false, |
| 16594 | ) |
| 16595 | .unwrap(); |
| 16596 | assert!( |
| 16597 | decoded.y.iter().all(|&s| s == 128), |
| 16598 | "a skipped DC_PRED block with no neighbours predicts flat mid-grey" |
| 16599 | ); |
| 16600 | } |
| 16601 | |
| 16602 | /// An inter tile's SB-level `part64` used to be read and thrown away |
| 16603 | /// (`let _ = part64;`), recursing as SPLIT unconditionally -- a real |
| 16604 | /// non-SPLIT value there silently desynced the whole tile instead of |
| 16605 | /// refusing. Hand-writes a single whole superblock's `part64 = HORZ` |
| 16606 | /// symbol (never produced by this crate's own encoder, which only ever |
| 16607 | /// writes NONE/SPLIT at this level, but a real aomenc-class encoder can) |
| 16608 | /// and asserts this decoder refuses by name rather than decoding garbage. |
| 16609 | #[test] |
| 16610 | fn a_non_split_inter_sb_partition_refuses_by_name_instead_of_desyncing() { |
| 16611 | use crate::msac::SymbolEncoder; |
| 16612 | let mut cdfs = Cdfs::new(q_ctx_of(40)); |
| 16613 | let mut enc = SymbolEncoder::new(); |
| 16614 | enc.symbol(PARTITION_HORZ, &mut cdfs.partition_w64[0]); |
| 16615 | let data = enc.finish(); |
| 16616 | let reference = crate::encode::Picture::grey(64, 64); |
| 16617 | let err = decode_inter_frame_tile( |
| 16618 | &data, |
| 16619 | 16, |
| 16620 | 16, |
| 16621 | 40, |
| 16622 | 64, |
| 16623 | 64, |
| 16624 | &reference, |
| 16625 | [None; 8], |
| 16626 | &CdefParams::default(), |
| 16627 | &LoopFilterParams::default(), |
| 16628 | false, |
| 16629 | false, |
| 16630 | Some(mc::InterpFilterKind::Regular), |
| 16631 | false, |
| 16632 | false, |
| 16633 | ) |
| 16634 | .unwrap_err(); |
| 16635 | let msg = err.to_string(); |
| 16636 | assert!( |
| 16637 | msg.contains("SPLIT"), |
| 16638 | "expected the named inter-SB-partition refusal, got: {msg}" |
| 16639 | ); |
| 16640 | } |
| 16641 | |
| 16642 | // ffmpeg cross-oracle: an independent decoder (not this crate's own |
| 16643 | // encoder-side reconstruction) agrees with what this module decodes, |
| 16644 | // duplicating `crate::encode`'s own `#[cfg(test)]` helpers of the same |
| 16645 | // name (that module is another lane's territory this round, and the |
| 16646 | // helpers are `#[cfg(test)]`-private to it). |
| 16647 | use std::io::Write; |
| 16648 | use std::process::{Command, Stdio}; |
| 16649 | |
| 16650 | /// Whether ffmpeg is on PATH. Absence normally SKIPs, but |
| 16651 | /// `EC_AV1_REQUIRE_FFMPEG=1` -- or `EC_AV1_REQUIRE_AOMENC=1`, since every |
| 16652 | /// aomenc gate decodes its stream through ffmpeg and is meaningless |
| 16653 | /// without it -- turns it into a hard failure. Without this the require |
| 16654 | /// flag was silently short-circuited: `!have_ffmpeg()` is evaluated first |
| 16655 | /// in `if !have_ffmpeg() || !have_aomenc()`, so a machine with no ffmpeg |
| 16656 | /// printed SKIP and reported green (class gate-skips-on-its-own-failure). |
| 16657 | fn have_ffmpeg() -> bool { |
| 16658 | let present = Command::new("ffmpeg") |
| 16659 | .arg("-version") |
| 16660 | .stdout(Stdio::null()) |
| 16661 | .stderr(Stdio::null()) |
| 16662 | .status() |
| 16663 | .is_ok_and(|s| s.success()); |
| 16664 | assert!( |
| 16665 | present |
| 16666 | || (std::env::var_os("EC_AV1_REQUIRE_FFMPEG").is_none() |
| 16667 | && std::env::var_os("EC_AV1_REQUIRE_AOMENC").is_none()), |
| 16668 | "EC_AV1_REQUIRE_FFMPEG/EC_AV1_REQUIRE_AOMENC is set but no working ffmpeg on PATH" |
| 16669 | ); |
| 16670 | present |
| 16671 | } |
| 16672 | |
| 16673 | /// Decodes one AV1 OBU stream with ffmpeg and hands back its one 4:2:0 |
| 16674 | /// frame's planes, at ffmpeg's own (coded, block-padded) size. |
| 16675 | fn ffmpeg_decode(stream: &[u8], width: usize, height: usize) -> Picture { |
| 16676 | let mut child = Command::new("ffmpeg") |
| 16677 | .args([ |
| 16678 | "-v", "error", "-f", "obu", "-i", "-", "-f", "rawvideo", "-pix_fmt", "yuv420p", "-", |
| 16679 | ]) |
| 16680 | .stdin(Stdio::piped()) |
| 16681 | .stdout(Stdio::piped()) |
| 16682 | .stderr(Stdio::piped()) |
| 16683 | .spawn() |
| 16684 | .expect("ffmpeg failed to start"); |
| 16685 | child |
| 16686 | .stdin |
| 16687 | .take() |
| 16688 | .expect("ffmpeg stdin") |
| 16689 | .write_all(stream) |
| 16690 | .expect("writing the stream to ffmpeg"); |
| 16691 | let out = child.wait_with_output().expect("ffmpeg failed to run"); |
| 16692 | assert!( |
| 16693 | out.status.success(), |
| 16694 | "ffmpeg refused the stream: {}", |
| 16695 | String::from_utf8_lossy(&out.stderr) |
| 16696 | ); |
| 16697 | let (luma, chroma) = (width * height, width * height / 4); |
| 16698 | assert_eq!( |
| 16699 | out.stdout.len(), |
| 16700 | luma + 2 * chroma, |
| 16701 | "expected one 4:2:0 frame" |
| 16702 | ); |
| 16703 | Picture { |
| 16704 | width, |
| 16705 | height, |
| 16706 | y: out.stdout[..luma].iter().map(|&v| u16::from(v)).collect(), |
| 16707 | u: out.stdout[luma..luma + chroma].iter().map(|&v| u16::from(v)).collect(), |
| 16708 | v: out.stdout[luma + chroma..].iter().map(|&v| u16::from(v)).collect(), |
| 16709 | } |
| 16710 | } |
| 16711 | |
| 16712 | /// Decodes `frames` concatenated 4:2:0 frames out of one AV1 OBU stream. |
| 16713 | fn ffmpeg_decode_sequence( |
| 16714 | stream: &[u8], |
| 16715 | width: usize, |
| 16716 | height: usize, |
| 16717 | frames: usize, |
| 16718 | ) -> Vec<Picture> { |
| 16719 | let mut child = Command::new("ffmpeg") |
| 16720 | .args([ |
| 16721 | "-v", "error", "-f", "obu", "-i", "-", "-f", "rawvideo", "-pix_fmt", "yuv420p", "-", |
| 16722 | ]) |
| 16723 | .stdin(Stdio::piped()) |
| 16724 | .stdout(Stdio::piped()) |
| 16725 | .stderr(Stdio::piped()) |
| 16726 | .spawn() |
| 16727 | .expect("ffmpeg failed to start"); |
| 16728 | child |
| 16729 | .stdin |
| 16730 | .take() |
| 16731 | .expect("ffmpeg stdin") |
| 16732 | .write_all(stream) |
| 16733 | .expect("writing the stream to ffmpeg"); |
| 16734 | let out = child.wait_with_output().expect("ffmpeg failed to run"); |
| 16735 | assert!( |
| 16736 | out.status.success(), |
| 16737 | "ffmpeg refused the stream: {}", |
| 16738 | String::from_utf8_lossy(&out.stderr) |
| 16739 | ); |
| 16740 | let (luma, chroma) = (width * height, width * height / 4); |
| 16741 | let frame_bytes = luma + 2 * chroma; |
| 16742 | assert_eq!( |
| 16743 | out.stdout.len(), |
| 16744 | frame_bytes * frames, |
| 16745 | "expected {frames} 4:2:0 frames, ffmpeg said: {}", |
| 16746 | String::from_utf8_lossy(&out.stderr) |
| 16747 | ); |
| 16748 | (0..frames) |
| 16749 | .map(|i| { |
| 16750 | let base = i * frame_bytes; |
| 16751 | Picture { |
| 16752 | width, |
| 16753 | height, |
| 16754 | y: out.stdout[base..base + luma].iter().map(|&v| u16::from(v)).collect(), |
| 16755 | u: out.stdout[base + luma..base + luma + chroma].iter().map(|&v| u16::from(v)).collect(), |
| 16756 | v: out.stdout[base + luma + chroma..base + frame_bytes].iter().map(|&v| u16::from(v)).collect(), |
| 16757 | } |
| 16758 | }) |
| 16759 | .collect() |
| 16760 | } |
| 16761 | |
| 16762 | /// `ffprobe`'s reported coded `width,height` for one OBU stream -- |
| 16763 | /// duplicating `crate::encode`'s own helper (per-call temp naming: the |
| 16764 | /// test binary runs callers on parallel threads, and a shared name lets |
| 16765 | /// one test's cleanup race another's still-reading ffprobe -- |
| 16766 | /// pid-keyed-temp-path class). |
| 16767 | fn ffprobe_size(stream: &[u8]) -> (u32, u32) { |
| 16768 | static PROBE_SEQ: std::sync::atomic::AtomicU64 = std::sync::atomic::AtomicU64::new(0); |
| 16769 | let path = std::env::temp_dir().join(format!( |
| 16770 | "ec-av1-decode-probe-{}-{}.obu", |
| 16771 | std::process::id(), |
| 16772 | PROBE_SEQ.fetch_add(1, std::sync::atomic::Ordering::Relaxed) |
| 16773 | )); |
| 16774 | let _ = std::fs::remove_file(&path); |
| 16775 | std::fs::write(&path, stream).expect("writing the probe stream"); |
| 16776 | let out = Command::new("ffprobe") |
| 16777 | .args([ |
| 16778 | "-v", |
| 16779 | "error", |
| 16780 | "-f", |
| 16781 | "obu", |
| 16782 | "-select_streams", |
| 16783 | "v:0", |
| 16784 | "-show_entries", |
| 16785 | "stream=width,height", |
| 16786 | "-of", |
| 16787 | "csv=p=0", |
| 16788 | ]) |
| 16789 | .arg(&path) |
| 16790 | .output() |
| 16791 | .expect("ffprobe failed to run"); |
| 16792 | let _ = std::fs::remove_file(&path); |
| 16793 | assert!( |
| 16794 | out.status.success(), |
| 16795 | "ffprobe refused the stream: {}", |
| 16796 | String::from_utf8_lossy(&out.stderr) |
| 16797 | ); |
| 16798 | let text = String::from_utf8_lossy(&out.stdout); |
| 16799 | let mut fields = text.trim().split(','); |
| 16800 | let width: u32 = fields.next().expect("ffprobe width").parse().unwrap(); |
| 16801 | let height: u32 = fields.next().expect("ffprobe height").parse().unwrap(); |
| 16802 | (width, height) |
| 16803 | } |
| 16804 | |
| 16805 | /// This module's own decoder against an independent one (ffmpeg), not |
| 16806 | /// just against this crate's encoder-side reconstruction: both decoders |
| 16807 | /// read the identical tile bytes, so agreement here rules out a bug this |
| 16808 | /// crate's own writer and reader could otherwise share. |
| 16809 | #[test] |
| 16810 | fn ffmpeg_and_this_decoder_agree_on_a_key_frame() { |
| 16811 | if !have_ffmpeg() { |
| 16812 | eprintln!("SKIP ffmpeg_and_this_decoder_agree_on_a_key_frame: no ffmpeg"); |
| 16813 | return; |
| 16814 | } |
| 16815 | use crate::encode::encode_key_frame; |
| 16816 | for &(width, height) in &[(64usize, 64usize), (216, 96), (40, 32)] { |
| 16817 | let picture = round_trip_test_card(width, height); |
| 16818 | let encoded = encode_key_frame(&picture, 100, 0.5).unwrap(); |
| 16819 | let (coded_w, coded_h) = ffprobe_size(&encoded.stream); |
| 16820 | let ffmpeg_decoded = ffmpeg_decode(&encoded.stream, coded_w as usize, coded_h as usize); |
| 16821 | let ours = decode_key_frame_tile( |
| 16822 | &encoded.tile, |
| 16823 | encoded.mi_cols, |
| 16824 | encoded.mi_rows, |
| 16825 | encoded.base_q_idx, |
| 16826 | coded_w, |
| 16827 | coded_h, |
| 16828 | false, |
| 16829 | &CdefParams::default(), |
| 16830 | &LoopFilterParams::default(), |
| 16831 | false, |
| 16832 | true, |
| 16833 | false, |
| 16834 | false, |
| 16835 | ) |
| 16836 | .unwrap(); |
| 16837 | assert_eq!(ours.y, ffmpeg_decoded.y, "{width}x{height}: luma vs ffmpeg"); |
| 16838 | assert_eq!(ours.u, ffmpeg_decoded.u, "{width}x{height}: U vs ffmpeg"); |
| 16839 | assert_eq!(ours.v, ffmpeg_decoded.v, "{width}x{height}: V vs ffmpeg"); |
| 16840 | } |
| 16841 | } |
| 16842 | |
| 16843 | fn round_trip_test_card(width: usize, height: usize) -> crate::encode::Picture { |
| 16844 | let mut picture = crate::encode::Picture::grey(width, height); |
| 16845 | for row in 0..height { |
| 16846 | for col in 0..width { |
| 16847 | picture.y[row * width + col] = ((row * 7 + col * 11) % 251) as u16; |
| 16848 | } |
| 16849 | } |
| 16850 | for row in 0..height / 2 { |
| 16851 | for col in 0..width / 2 { |
| 16852 | let i = row * width / 2 + col; |
| 16853 | picture.u[i] = (100 + (col * 60 / (width / 2).max(1))) as u16; |
| 16854 | picture.v[i] = (200 - (row * 80 / (height / 2).max(1))) as u16; |
| 16855 | } |
| 16856 | } |
| 16857 | picture |
| 16858 | } |
| 16859 | |
| 16860 | fn panned_test_card(width: usize, height: usize, shift: i64) -> crate::encode::Picture { |
| 16861 | let mut picture = crate::encode::Picture::grey(width, height); |
| 16862 | for y in 0..height { |
| 16863 | for x in 0..width { |
| 16864 | let sx = (x as i64 - shift).rem_euclid(width as i64) as f64; |
| 16865 | let gradient = sx * 200.0 / width as f64; |
| 16866 | picture.y[y * width + x] = (20.0 + gradient).clamp(0.0, 255.0) as u16; |
| 16867 | } |
| 16868 | } |
| 16869 | for y in 0..height / 2 { |
| 16870 | for x in 0..width / 2 { |
| 16871 | let sx = (x as i64 - shift / 2).rem_euclid((width / 2) as i64) as usize; |
| 16872 | let i = y * width / 2 + x; |
| 16873 | picture.u[i] = (100 + (sx * 60 / (width / 2))) as u16; |
| 16874 | picture.v[i] = (200 - (y * 80 / (height / 2))) as u16; |
| 16875 | } |
| 16876 | } |
| 16877 | picture |
| 16878 | } |
| 16879 | |
| 16880 | /// A GOP (one key frame plus several inter frames, each panned so its |
| 16881 | /// blocks actually take `NEARESTMV`/`NEWMV` rather than an all-skip |
| 16882 | /// `(0, 0)` no-op) decodes bit-exact against the encoder's own |
| 16883 | /// reconstruction, frame by frame, chaining each decoded frame straight |
| 16884 | /// into the next as its reference -- exactly the single-slot DPB |
| 16885 | /// [`crate::encode::encode_sequence`] itself threads. |
| 16886 | fn gop_round_trips(width: usize, height: usize) { |
| 16887 | use crate::encode::encode_sequence; |
| 16888 | let pictures: Vec<_> = (0..4) |
| 16889 | .map(|i| panned_test_card(width, height, i * 3)) |
| 16890 | .collect(); |
| 16891 | let encoded = encode_sequence(&pictures, 100, 0.5).unwrap(); |
| 16892 | assert_eq!(encoded.frames.len(), 4); |
| 16893 | |
| 16894 | let key = &encoded.frames[0]; |
| 16895 | let mut reference = decode_key_frame_tile( |
| 16896 | &key.tile, |
| 16897 | key.mi_cols, |
| 16898 | key.mi_rows, |
| 16899 | key.base_q_idx, |
| 16900 | width as u32, |
| 16901 | height as u32, |
| 16902 | false, |
| 16903 | &CdefParams::default(), |
| 16904 | &LoopFilterParams::default(), |
| 16905 | false, |
| 16906 | true, |
| 16907 | false, |
| 16908 | false, |
| 16909 | ) |
| 16910 | .unwrap(); |
| 16911 | assert_eq!( |
| 16912 | reference.y, key.reconstruction.y, |
| 16913 | "{width}x{height} frame 0 luma" |
| 16914 | ); |
| 16915 | assert_eq!( |
| 16916 | reference.u, key.reconstruction.u, |
| 16917 | "{width}x{height} frame 0 U" |
| 16918 | ); |
| 16919 | assert_eq!( |
| 16920 | reference.v, key.reconstruction.v, |
| 16921 | "{width}x{height} frame 0 V" |
| 16922 | ); |
| 16923 | |
| 16924 | for (i, frame) in encoded.frames.iter().enumerate().skip(1) { |
| 16925 | let decoded = decode_inter_frame_tile( |
| 16926 | &frame.tile, |
| 16927 | frame.mi_cols, |
| 16928 | frame.mi_rows, |
| 16929 | frame.base_q_idx, |
| 16930 | width as u32, |
| 16931 | height as u32, |
| 16932 | &reference, |
| 16933 | [None; 8], |
| 16934 | &CdefParams::default(), |
| 16935 | &LoopFilterParams::default(), |
| 16936 | false, |
| 16937 | false, |
| 16938 | Some(mc::InterpFilterKind::Regular), |
| 16939 | false, |
| 16940 | false, |
| 16941 | ) |
| 16942 | .unwrap(); |
| 16943 | assert_eq!( |
| 16944 | decoded.y, frame.reconstruction.y, |
| 16945 | "{width}x{height} frame {i} luma" |
| 16946 | ); |
| 16947 | assert_eq!( |
| 16948 | decoded.u, frame.reconstruction.u, |
| 16949 | "{width}x{height} frame {i} U" |
| 16950 | ); |
| 16951 | assert_eq!( |
| 16952 | decoded.v, frame.reconstruction.v, |
| 16953 | "{width}x{height} frame {i} V" |
| 16954 | ); |
| 16955 | reference = decoded; |
| 16956 | } |
| 16957 | } |
| 16958 | |
| 16959 | #[test] |
| 16960 | fn a_gop_round_trips_bit_exact_against_the_encoder_reconstruction() { |
| 16961 | gop_round_trips(128, 64); |
| 16962 | } |
| 16963 | |
| 16964 | /// Same claim, at a size that is not a whole number of 32x32 blocks |
| 16965 | /// (216x96: 216/32 is not exact), so an inter frame's own 16x16-leaf |
| 16966 | /// split path is exercised too. |
| 16967 | #[test] |
| 16968 | fn an_odd_size_gop_round_trips_bit_exact_against_the_encoder_reconstruction() { |
| 16969 | gop_round_trips(216, 96); |
| 16970 | } |
| 16971 | |
| 16972 | /// The GOP cross-oracle: ffmpeg decodes the whole sequence the same way |
| 16973 | /// this module does, frame for frame. |
| 16974 | /// A NEARMV block (`not_new`, `not_zero`, `!nearest`) predicts from the |
| 16975 | /// MV stack's *second* candidate (`near_mv`), not its first |
| 16976 | /// (`nearest_mv`) -- a hand-built symbol stream drives |
| 16977 | /// [`decode_inter_block`] directly, since this crate's own encoder never |
| 16978 | /// writes NEARMV (round 3: it never chooses the mode). |
| 16979 | #[test] |
| 16980 | fn a_nearmv_block_predicts_from_the_stacks_second_candidate() { |
| 16981 | use crate::msac::SymbolEncoder; |
| 16982 | use crate::mvstack::{MiGrid, MiInfo, find_mv_stack, single_ref_ctx}; |
| 16983 | |
| 16984 | const LAST_FRAME: i8 = 1; |
| 16985 | let (mi_cols, mi_rows) = (12u32, 12u32); |
| 16986 | let side = 16usize; // one 16x16 leaf block |
| 16987 | let (r, c) = (1usize, 1usize); // `at`, in SUB (16px) units |
| 16988 | let (mi_row, mi_col) = (r * SUB_MI as usize, c * SUB_MI as usize); |
| 16989 | let bw4 = side / 4; |
| 16990 | |
| 16991 | let mut grid = MiGrid::new(mi_cols as usize, mi_rows as usize); |
| 16992 | let (above_mv, left_mv) = ((4, 4), (8, 8)); |
| 16993 | let neighbour = |mv| MiInfo { |
| 16994 | is_inter: true, |
| 16995 | ref_frame: LAST_FRAME, |
| 16996 | ref_frame1: None, |
| 16997 | mv1: None, |
| 16998 | mv, |
| 16999 | is_new_mv: false, |
| 17000 | size: 1, |
| 17001 | size_h: 1, |
| 17002 | is_global_mv0: false, |
| 17003 | is_global_mv1: false, |
| 17004 | }; |
| 17005 | for col in mi_col..mi_col + bw4 { |
| 17006 | grid.set(mi_row - 1, col, neighbour(above_mv)); |
| 17007 | } |
| 17008 | for row in mi_row..mi_row + bw4 { |
| 17009 | grid.set(row, mi_col - 1, neighbour(left_mv)); |
| 17010 | } |
| 17011 | |
| 17012 | let stack = find_mv_stack( |
| 17013 | &grid, |
| 17014 | mi_row, |
| 17015 | mi_col, |
| 17016 | bw4, |
| 17017 | bw4, |
| 17018 | LAST_FRAME, |
| 17019 | mi_cols as usize, |
| 17020 | mi_rows as usize, |
| 17021 | ); |
| 17022 | // Sanity on the test's own setup, not the code under test: two equal- |
| 17023 | // weight candidates keep scan order (above, then left -- module doc). |
| 17024 | assert_eq!(stack.entries.len(), 2); |
| 17025 | assert_eq!(stack.nearest_mv, above_mv); |
| 17026 | assert_eq!(stack.near_mv, left_mv); |
| 17027 | |
| 17028 | let (skip_ctx, ii_ctx, sr_ctx) = ( |
| 17029 | 0usize, |
| 17030 | intra_inter_ctx(true, true, false, false), |
| 17031 | single_ref_ctx(false), |
| 17032 | ); |
| 17033 | |
| 17034 | let mut cdfs = Cdfs::new(q_ctx_of(100)); |
| 17035 | let mut enc = SymbolEncoder::new(); |
| 17036 | enc.symbol(1, &mut cdfs.skip[skip_ctx]); // skip |
| 17037 | enc.symbol(1, &mut cdfs.intra_inter[ii_ctx]); // is_inter |
| 17038 | enc.symbol(0, &mut cdfs.single_ref[sr_ctx][0]); // LAST_FRAME |
| 17039 | enc.symbol(0, &mut cdfs.single_ref[sr_ctx][2]); |
| 17040 | enc.symbol(0, &mut cdfs.single_ref[sr_ctx][3]); |
| 17041 | enc.symbol(1, &mut cdfs.new_mv[stack.new_mv_ctx]); // not_new |
| 17042 | enc.symbol(1, &mut cdfs.zero_mv[stack.zero_mv_ctx]); // not_zero |
| 17043 | enc.symbol(1, &mut cdfs.ref_mv[stack.ref_mv_ctx]); // !nearest -> NEARMV |
| 17044 | let data = enc.finish(); |
| 17045 | |
| 17046 | let (width, height) = (64usize, 64usize); |
| 17047 | let ref_pattern: Vec<u16> = (0..width * height).map(|i| (i % 251) as u16).collect(); |
| 17048 | let ref_plane = |scale: usize| PlaneBuf { |
| 17049 | data: ref_pattern |
| 17050 | .iter() |
| 17051 | .step_by(scale.max(1)) |
| 17052 | .take((width / scale) * (height / scale)) |
| 17053 | .copied() |
| 17054 | .collect(), |
| 17055 | width: width / scale, |
| 17056 | height: height / scale, |
| 17057 | true_width: width / scale, |
| 17058 | true_height: height / scale, |
| 17059 | tile_x0: 0, |
| 17060 | tile_y0: 0, |
| 17061 | tile_x1: width / scale, |
| 17062 | tile_y1: height / scale, |
| 17063 | }; |
| 17064 | let ref_y = PlaneBuf { |
| 17065 | data: ref_pattern.clone(), |
| 17066 | width, |
| 17067 | height, |
| 17068 | true_width: width, |
| 17069 | true_height: height, |
| 17070 | tile_x0: 0, |
| 17071 | tile_y0: 0, |
| 17072 | tile_x1: width, |
| 17073 | tile_y1: height, |
| 17074 | }; |
| 17075 | let ref_u = ref_plane(2); |
| 17076 | let ref_v = ref_plane(2); |
| 17077 | |
| 17078 | let mut y = PlaneBuf { |
| 17079 | data: vec![0u16; width * height], |
| 17080 | width, |
| 17081 | height, |
| 17082 | true_width: width, |
| 17083 | true_height: height, |
| 17084 | tile_x0: 0, |
| 17085 | tile_y0: 0, |
| 17086 | tile_x1: width, |
| 17087 | tile_y1: height, |
| 17088 | }; |
| 17089 | let mut u = PlaneBuf { |
| 17090 | data: vec![0u16; (width / 2) * (height / 2)], |
| 17091 | width: width / 2, |
| 17092 | height: height / 2, |
| 17093 | true_width: width / 2, |
| 17094 | true_height: height / 2, |
| 17095 | tile_x0: 0, |
| 17096 | tile_y0: 0, |
| 17097 | tile_x1: width / 2, |
| 17098 | tile_y1: height / 2, |
| 17099 | }; |
| 17100 | let mut v = PlaneBuf { |
| 17101 | data: vec![0u16; (width / 2) * (height / 2)], |
| 17102 | width: width / 2, |
| 17103 | height: height / 2, |
| 17104 | true_width: width / 2, |
| 17105 | true_height: height / 2, |
| 17106 | tile_x0: 0, |
| 17107 | tile_y0: 0, |
| 17108 | tile_x1: width / 2, |
| 17109 | tile_y1: height / 2, |
| 17110 | }; |
| 17111 | |
| 17112 | let mut cdfs = Cdfs::new(q_ctx_of(100)); |
| 17113 | let mut dec = SymbolDecoder::new(&data); |
| 17114 | let mut neighbours = Neighbours::new(3, 3, mi_cols as usize, mi_rows as usize); |
| 17115 | decode_inter_block( |
| 17116 | &mut dec, |
| 17117 | &mut cdfs, |
| 17118 | &mut neighbours, |
| 17119 | &mut grid, |
| 17120 | (r, c), |
| 17121 | side, |
| 17122 | mi_cols, |
| 17123 | mi_rows, |
| 17124 | &mut y, |
| 17125 | &mut u, |
| 17126 | &mut v, |
| 17127 | &ref_y, |
| 17128 | &ref_u, |
| 17129 | &ref_v, |
| 17130 | &[None; 8], |
| 17131 | &NO_SIGN_BIAS, |
| 17132 | &[ec_av1_syntax::WarpParams::default(); 7], |
| 17133 | 100, |
| 17134 | TxbSet::Luma16, |
| 17135 | TxbSet::Luma16Inter, |
| 17136 | TxbSet::Chroma8, |
| 17137 | TX16, |
| 17138 | TX8, |
| 17139 | &[], |
| 17140 | &[], |
| 17141 | 0, |
| 17142 | false, |
| 17143 | false, |
| 17144 | Some(mc::InterpFilterKind::Regular), |
| 17145 | false, |
| 17146 | None, |
| 17147 | false, |
| 17148 | false, |
| 17149 | false, |
| 17150 | false, |
| 17151 | 0, |
| 17152 | 0, |
| 17153 | [0; 7], |
| 17154 | false, |
| 17155 | [0; 2], |
| 17156 | false, |
| 17157 | false, |
| 17158 | false, |
| 17159 | side, |
| 17160 | side, |
| 17161 | false, |
| 17162 | width, |
| 17163 | ) |
| 17164 | .unwrap(); |
| 17165 | |
| 17166 | let (px, py) = (c * SUB, r * SUB); |
| 17167 | let mut want_near = vec![0u16; side * side]; |
| 17168 | crate::mc::predict( |
| 17169 | &ref_y.data, |
| 17170 | ref_y.width, |
| 17171 | ref_y.true_width, |
| 17172 | ref_y.true_height, |
| 17173 | mv_to_q4(px, left_mv.1, true), |
| 17174 | mv_to_q4(py, left_mv.0, true), |
| 17175 | side, |
| 17176 | side, |
| 17177 | &mut want_near, |
| 17178 | ); |
| 17179 | let mut want_nearest = vec![0u16; side * side]; |
| 17180 | crate::mc::predict( |
| 17181 | &ref_y.data, |
| 17182 | ref_y.width, |
| 17183 | ref_y.true_width, |
| 17184 | ref_y.true_height, |
| 17185 | mv_to_q4(px, above_mv.1, true), |
| 17186 | mv_to_q4(py, above_mv.0, true), |
| 17187 | side, |
| 17188 | side, |
| 17189 | &mut want_nearest, |
| 17190 | ); |
| 17191 | assert_ne!( |
| 17192 | want_near, want_nearest, |
| 17193 | "test setup: the two candidate MVs must predict differently" |
| 17194 | ); |
| 17195 | |
| 17196 | let mut got = vec![0u16; side * side]; |
| 17197 | for row in 0..side { |
| 17198 | got[row * side..(row + 1) * side].copy_from_slice( |
| 17199 | &y.data[(py + row) * y.width + px..(py + row) * y.width + px + side], |
| 17200 | ); |
| 17201 | } |
| 17202 | assert_eq!( |
| 17203 | got, want_near, |
| 17204 | "NEARMV predicted from the wrong stack entry" |
| 17205 | ); |
| 17206 | } |
| 17207 | |
| 17208 | #[test] |
| 17209 | fn ffmpeg_and_this_decoder_agree_on_a_gop() { |
| 17210 | if !have_ffmpeg() { |
| 17211 | eprintln!("SKIP ffmpeg_and_this_decoder_agree_on_a_gop: no ffmpeg"); |
| 17212 | return; |
| 17213 | } |
| 17214 | use crate::encode::encode_sequence; |
| 17215 | let (width, height) = (128usize, 64usize); |
| 17216 | let pictures: Vec<_> = (0..3) |
| 17217 | .map(|i| panned_test_card(width, height, i * 3)) |
| 17218 | .collect(); |
| 17219 | let encoded = encode_sequence(&pictures, 100, 0.5).unwrap(); |
| 17220 | let (coded_w, coded_h) = ffprobe_size(&encoded.stream); |
| 17221 | let ffmpeg_frames = |
| 17222 | ffmpeg_decode_sequence(&encoded.stream, coded_w as usize, coded_h as usize, 3); |
| 17223 | |
| 17224 | let key = &encoded.frames[0]; |
| 17225 | let mut reference = decode_key_frame_tile( |
| 17226 | &key.tile, |
| 17227 | key.mi_cols, |
| 17228 | key.mi_rows, |
| 17229 | key.base_q_idx, |
| 17230 | coded_w, |
| 17231 | coded_h, |
| 17232 | false, |
| 17233 | &CdefParams::default(), |
| 17234 | &LoopFilterParams::default(), |
| 17235 | false, |
| 17236 | true, |
| 17237 | false, |
| 17238 | false, |
| 17239 | ) |
| 17240 | .unwrap(); |
| 17241 | assert_eq!(reference.y, ffmpeg_frames[0].y, "frame 0 luma vs ffmpeg"); |
| 17242 | |
| 17243 | for (i, frame) in encoded.frames.iter().enumerate().skip(1) { |
| 17244 | let decoded = decode_inter_frame_tile( |
| 17245 | &frame.tile, |
| 17246 | frame.mi_cols, |
| 17247 | frame.mi_rows, |
| 17248 | frame.base_q_idx, |
| 17249 | coded_w, |
| 17250 | coded_h, |
| 17251 | &reference, |
| 17252 | [None; 8], |
| 17253 | &CdefParams::default(), |
| 17254 | &LoopFilterParams::default(), |
| 17255 | false, |
| 17256 | false, |
| 17257 | Some(mc::InterpFilterKind::Regular), |
| 17258 | false, |
| 17259 | false, |
| 17260 | ) |
| 17261 | .unwrap(); |
| 17262 | assert_eq!(decoded.y, ffmpeg_frames[i].y, "frame {i} luma vs ffmpeg"); |
| 17263 | assert_eq!(decoded.u, ffmpeg_frames[i].u, "frame {i} U vs ffmpeg"); |
| 17264 | assert_eq!(decoded.v, ffmpeg_frames[i].v, "frame {i} V vs ffmpeg"); |
| 17265 | reference = decoded; |
| 17266 | } |
| 17267 | } |
| 17268 | } |