edith_codecs
Clone
https://git.tahinli.com/Tahinli/edith_codecs.git
0 watching0 forksRust370.3 MiBdfeffbcTahinli2 months ago3180 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 | use crate::hits::{hit, hit_do}; |
| 24 | |
| 25 | use ec_av1_syntax::{ |
| 26 | CdefParams, DeltaParams, LoopFilterParams, LoopRestorationParams, MAX_SEGMENTS, |
| 27 | SEG_LVL_ALT_LF_Y_V, SEG_LVL_ALT_Q, SegmentationParams, TileInfo, |
| 28 | }; |
| 29 | use ec_core::{Error, Result}; |
| 30 | |
| 31 | // lane-av112bit: the 12-bit warp refusal that used to stand at all three |
| 32 | // warp decision sites (compound, 16x16+, 8x8 leaf) was an inline literal |
| 33 | // (refusal_inventory.rs extracts refusal strings from the decode source); |
| 34 | // it named the real gap: warp's reduce bits inherited the 12-bit |
| 35 | // `conv_params->round_0` (warped_motion.c:295, convolve.h:81..86) and no |
| 36 | // witness existed. |
| 37 | // lane-av112bitw lifted it from all three sites: `warp::warp_round_0` |
| 38 | // implements the bump (`round_0 += max(bd + FILTER_BITS - round_0 - 14, 0)`, |
| 39 | // 5 at 12 bits), and the single-ref witness plus the rotating-mandelbrot |
| 40 | // compound-warp witness are both byte-exact vs ffmpeg. Compound inter |
| 41 | // itself was lifted by lane-av112bitc before that (the combines absorb the |
| 42 | // CONV_BUF gain drop, `INTER_POST_ROUND - delta`, see `mc::combine_compound`). |
| 43 | |
| 44 | /// Per-frame decode state, one instance per decoded stream (lane-thread1). |
| 45 | /// |
| 46 | /// Every field here used to be a `thread_local!` static, which made a decode a |
| 47 | /// function of the calling THREAD's history rather than of the bitstream alone |
| 48 | /// -- and made frame-parallel decoding impossible, since a worker thread sees |
| 49 | /// empty state. The cells keep exactly the interior mutability the statics |
| 50 | /// had, so every read/write site is byte-identical; what changes is WHERE the |
| 51 | /// state lives: `stream.rs` owns one `FrameCtx` and threads `&FrameCtx` down |
| 52 | /// the decode call graph. |
| 53 | /// |
| 54 | /// `Cell`/`RefCell` make this `!Sync` on purpose: a `FrameCtx` belongs to |
| 55 | /// exactly one thread's decode, and is never shared between workers. |
| 56 | /// One frame's in-loop filter intermediates, as [`FrameCtx::capture_stages`] |
| 57 | /// asks for them: each plane's post-deblock (pre-CDEF) and post-CDEF |
| 58 | /// (pre-restoration) samples, at the decoder's own plane stride. |
| 59 | pub(crate) struct FilterStages { |
| 60 | /// Post-deblock, pre-CDEF planes (Y, U, V). |
| 61 | pub(crate) deblocked: [Vec<u16>; 3], |
| 62 | /// Post-CDEF, pre-restoration planes (Y, U, V). |
| 63 | pub(crate) cdefed: [Vec<u16>; 3], |
| 64 | /// Each plane's stride (and so its width in the buffers above). |
| 65 | pub(crate) stride: [usize; 3], |
| 66 | } |
| 67 | |
| 68 | /// Takes whatever the last capturing decode left (see |
| 69 | /// [`FrameCtx::capture_stages`]). |
| 70 | #[allow(dead_code)] // read from the encoder's loop restoration search |
| 71 | pub(crate) fn take_filter_stages(fctx: &FrameCtx) -> Option<FilterStages> { |
| 72 | fctx.stages.borrow_mut().take() |
| 73 | } |
| 74 | |
| 75 | pub(crate) struct FrameCtx { |
| 76 | /// lane-av1lr: when set, the frame filter chain leaves its post-deblock |
| 77 | /// and post-CDEF planes in [`FrameCtx::stages`] for the ENCODER's loop |
| 78 | /// restoration search (spec 7.17 reads both: the stripe interior from |
| 79 | /// the post-CDEF plane, its 3-row border from the post-deblock one). |
| 80 | /// Off for every decode but that search, and off it costs one bool read |
| 81 | /// per frame and not a single copy. |
| 82 | pub(crate) capture_stages: std::cell::Cell<bool>, |
| 83 | pub(crate) stages: std::cell::RefCell<Option<FilterStages>>, |
| 84 | pub(crate) sb128_flag: std::cell::Cell<bool>, |
| 85 | /// lane-refs: the frame-level `interpolation_filter` (spec 6.8.9) every |
| 86 | /// motion compensation of THIS frame runs, already resolved to a kernel. |
| 87 | /// `mc::predict` used to hardwire `Regular`, so the encoder's search, |
| 88 | /// trial and recon could only ever build a REGULAR prediction whatever |
| 89 | /// the frame header said; it lives here (and is copied by |
| 90 | /// [`filter_ctx_copy`]) so a tile-search worker thread reads the same |
| 91 | /// kernel the frame header wrote instead of the default. |
| 92 | pub(crate) interp_filter: std::cell::Cell<crate::mc::InterpFilterKind>, |
| 93 | /// lane-lossless: this frame's per-segment lossless table -- libaom's |
| 94 | /// `xd->lossless[MAX_SEGMENTS]`, computed ONCE per frame in the header |
| 95 | /// (spec 5.9.12 `CodedLossless`: each segment at `qindex == 0` with zero |
| 96 | /// plane deltas, `get_qindex` applying that segment's own `SEG_LVL_ALT_Q`). |
| 97 | /// Every block-decoder rule that reads [`decode::lossless`] resolves it |
| 98 | /// through the BLOCK's own `segment_id`: TX_4X4 with the Walsh-Hadamard |
| 99 | /// transform on every plane, the dropped `tx_type` symbol and the narrowed |
| 100 | /// `is_cfl_allowed` (libaom `read_tx_size` (`decodeframe.c`), |
| 101 | /// `read_tx_type` (`decodetxb.c`), `is_cfl_allowed` (`blockd.h`)) follow |
| 102 | /// the SEGMENT, so a frame whose segments disagree codes lossless and |
| 103 | /// lossy blocks side by side. |
| 104 | pub(crate) lossless_per_seg: std::cell::Cell<[bool; MAX_SEGMENTS]>, |
| 105 | /// lane-av1txr: this frame header's `disable_cdf_update` (spec 5.9.2). |
| 106 | /// When set, the tile reader must not adapt any CDF it reads, or its |
| 107 | /// tables drift away from the encoder's and the frame desyncs mid-tile |
| 108 | /// (libaom: `allow_update_cdf = allow_update_cdf && |
| 109 | /// !cm->features.disable_cdf_update`, `decodeframe.c:2909`). |
| 110 | pub(crate) disable_cdf_update: std::cell::Cell<bool>, |
| 111 | pub(crate) cdef_bits: std::cell::Cell<u8>, |
| 112 | pub(crate) bit_depth: std::cell::Cell<u8>, |
| 113 | /// lane-av1mono: the sequence header's `color_config.mono_chrome` (spec |
| 114 | /// 5.5.2), i.e. `NumPlanes == 1`. libaom threads this as |
| 115 | /// `av1_num_planes(cm)` and every chroma read site is guarded by |
| 116 | /// `!cm->seq_params->monochrome && xd->is_chroma_ref` (`decodemv.c:933`, |
| 117 | /// `:1208`) or bounded by `plane < num_planes`; a monochrome frame codes |
| 118 | /// NO `uv_mode`/`cfl`/`angle_delta_uv`/`palette_uv` symbol and NO chroma |
| 119 | /// coefficients on any block. Set once per frame from the sequence header |
| 120 | /// (like [`FrameCtx::bit_depth`]), so a mono frame decoded on a thread |
| 121 | /// that last saw a 4:2:0 one never inherits the wrong plane count. |
| 122 | pub(crate) mono_chrome: std::cell::Cell<bool>, |
| 123 | /// lane-av1-444: sequence subsampling_x/y. 1,1 is 4:2:0; 0,0 is 4:4:4. |
| 124 | pub(crate) subsampling_x: std::cell::Cell<u8>, |
| 125 | pub(crate) subsampling_y: std::cell::Cell<u8>, |
| 126 | pub(crate) superres: std::cell::Cell<(u32, u32)>, |
| 127 | pub(crate) cdef_transmitted: std::cell::Cell<bool>, |
| 128 | pub(crate) cdef_sb_cols: std::cell::Cell<usize>, |
| 129 | pub(crate) cdef_idx_grid: std::cell::RefCell<Vec<u8>>, |
| 130 | pub(crate) delta_q_present: std::cell::Cell<bool>, |
| 131 | pub(crate) delta_q_res: std::cell::Cell<i32>, |
| 132 | pub(crate) current_q_idx: std::cell::Cell<i32>, |
| 133 | pub(crate) quant_deltas: std::cell::Cell<crate::quant::QuantDeltas>, |
| 134 | /// lane-av1-qmatrix: this frame's quantisation-matrix LEVEL per plane, |
| 135 | /// straight from spec 5.9.12's `qm_y`/`qm_u`/`qm_v` (the parsed 4-bit |
| 136 | /// value IS the table level -- libaom `quant_params->qmatrix_level_y/u/v`, |
| 137 | /// decodeframe.c:1902-1917; the `aom_get_qmlevel` formula that maps |
| 138 | /// base_q_idx onto a level is encoder-side only). 15 is libaom's flat |
| 139 | /// sentinel (`NUM_QM_LEVELS - 1`, stored as NULL tables) and the default |
| 140 | /// every `using_qmatrix == 0` frame keeps, so a matrix-free stream |
| 141 | /// selects no matrix. |
| 142 | pub(crate) qm_levels: std::cell::Cell<[u8; 3]>, |
| 143 | pub(crate) delta_lf_present: std::cell::Cell<bool>, |
| 144 | pub(crate) delta_lf_res: std::cell::Cell<i32>, |
| 145 | pub(crate) delta_lf_multi: std::cell::Cell<bool>, |
| 146 | pub(crate) current_delta_lf: std::cell::Cell<[i32; 4]>, |
| 147 | pub(crate) seg: std::cell::RefCell<SegmentationParams>, |
| 148 | pub(crate) seg_ids: std::cell::RefCell<Vec<u8>>, |
| 149 | pub(crate) prev_seg_ids: std::cell::RefCell<Vec<u8>>, |
| 150 | pub(crate) seg_mi_dims: std::cell::Cell<(usize, usize)>, |
| 151 | pub(crate) above_seg_pred: std::cell::RefCell<Vec<u8>>, |
| 152 | pub(crate) left_seg_pred: std::cell::RefCell<Vec<u8>>, |
| 153 | pub(crate) cur_segment_id: std::cell::Cell<u8>, |
| 154 | pub(crate) seg_tile_origin: std::cell::Cell<(usize, usize)>, |
| 155 | pub(crate) tx_select_inter: std::cell::Cell<bool>, |
| 156 | /// Whether the tile being decoded is an INTRA-ONLY frame's (key/intra-only |
| 157 | /// frame). libaom runs `set_txfm_ctxs` for EVERY block of EVERY frame |
| 158 | /// (`decodeframe.c` `parse_decode_block`), so an intra-only frame |
| 159 | /// maintains the `TXFM_CONTEXT` bands exactly like an inter one; only this |
| 160 | /// decoder's inter-side publishing was wired. See |
| 161 | /// [`Neighbours::fill_lf_grid_rect`]'s band publish. |
| 162 | pub(crate) intra_only: std::cell::Cell<bool>, |
| 163 | /// This FRAME's own `tx_mode == TxMode::Select`, set for EVERY frame |
| 164 | /// (intra included). [`Self::tx_select_inter`] cannot stand in for it: |
| 165 | /// an intrabc block rides an INTRA frame, but libaom still reads its |
| 166 | /// var-tx `txfm_partition` tree under the frame's `TxMode::Select`. |
| 167 | pub(crate) tx_select_frame: std::cell::Cell<bool>, |
| 168 | pub(crate) reduced_tx_set_inter: std::cell::Cell<bool>, |
| 169 | pub(crate) enable_filter_intra_inter: std::cell::Cell<bool>, |
| 170 | pub(crate) palette_pred: std::cell::RefCell<Option<Vec<u16>>>, |
| 171 | pub(crate) intrabc_mi_grid: std::cell::RefCell<Option<(crate::mvstack::MiGrid, usize, usize)>>, |
| 172 | pub(crate) intrabc_dv: std::cell::Cell<Option<(i32, i32)>>, |
| 173 | pub(crate) luma_tx_type: std::cell::Cell<TxType>, |
| 174 | pub(crate) intrabc_chroma_tx: std::cell::Cell<Option<TxType>>, |
| 175 | pub(crate) intra_in_inter_mode: std::cell::Cell<Option<(usize, bool)>>, |
| 176 | pub(crate) inter_strip_chroma: std::cell::Cell<Option<InterStripChroma>>, |
| 177 | pub(crate) inter_last_mc: |
| 178 | std::cell::Cell<Option<(i8, (i32, i32), mc::InterpFilterKind, mc::InterpFilterKind)>>, |
| 179 | pub(crate) enable_edge_filter: std::cell::Cell<bool>, |
| 180 | pub(crate) last_frame_wide_margin: std::cell::RefCell<Option<Picture>>, |
| 181 | pub(crate) reach_sb_px: std::cell::Cell<usize>, |
| 182 | pub(crate) grain_bit_depth: std::cell::Cell<u32>, |
| 183 | /// lane-wave1 step (i): this superblock's recorded pixel writes, see |
| 184 | /// [`ReconOp`]. Pushed by parse, replayed by [`flush_recon`]. |
| 185 | pub(crate) recon_ops: std::cell::RefCell<Vec<ReconOp>>, |
| 186 | /// The queue buffer [`flush_recon`] just drained, kept for its capacity |
| 187 | /// so a superblock's records do not regrow the vector from nothing. |
| 188 | pub(crate) recon_spare: std::cell::RefCell<Vec<ReconOp>>, |
| 189 | /// lane-recoparse: the coefficient grids of the recorded ops above, one |
| 190 | /// buffer instead of a `Vec` per transform unit -- a recorded unit is |
| 191 | /// `(offset, len)` into it ([`Residual::Coeffs`]). Written by parse |
| 192 | /// before the op is published, read only by whoever replays that op, and |
| 193 | /// handed over WITH the ops at every drain point ([`wave_submit`], |
| 194 | /// [`flush_recon`]), so offsets are always relative to the live arena. |
| 195 | pub(crate) coeff_arena: std::cell::RefCell<Vec<i32>>, |
| 196 | /// [`recon_spare`](Self::recon_spare) for the arena, on the inline drain |
| 197 | /// path (a submitted job's arena travels to its worker instead). |
| 198 | pub(crate) coeff_spare: std::cell::RefCell<Vec<i32>>, |
| 199 | /// [`coeff_arena`](Self::coeff_arena) for the recorded PREDICTIONS |
| 200 | /// ([`PredSrc::Owned`]): same lifetime, same drain points. |
| 201 | pub(crate) pred_arena: std::cell::RefCell<Vec<u16>>, |
| 202 | pub(crate) pred_spare: std::cell::RefCell<Vec<u16>>, |
| 203 | /// lane-wave1 step (ii): the tile decoder's own planes, registered by |
| 204 | /// [`ReconInline`] while reconstruction runs INLINE (one recon thread). |
| 205 | /// `None` while the queue is in use (wavefront workers) and outside a |
| 206 | /// tile. See [`push_op`]. |
| 207 | pub(crate) recon_planes: std::cell::Cell<Option<ReconPlanes>>, |
| 208 | /// lane-wave1 step (ii): the superblock wavefront this tile's recorded |
| 209 | /// writes are handed to, at more than one reconstruction thread. |
| 210 | pub(crate) wave: std::cell::RefCell<Option<std::sync::Arc<WaveState>>>, |
| 211 | /// lane-wave5 instrumentation only (`EC_AV1_WAVE_STATS`): when the parse |
| 212 | /// thread last handed a superblock to the wavefront. |
| 213 | pub(crate) wave_mark: std::cell::Cell<Option<std::time::Instant>>, |
| 214 | } |
| 215 | |
| 216 | impl std::fmt::Debug for FrameCtx { |
| 217 | fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result { |
| 218 | f.write_str("FrameCtx") |
| 219 | } |
| 220 | } |
| 221 | |
| 222 | impl FrameCtx { |
| 223 | /// A fresh context, every field at the default its thread-local |
| 224 | /// predecessor was born with. |
| 225 | pub(crate) fn new() -> Self { |
| 226 | Self { |
| 227 | // A DECODER context is seeded from the stream alone: every real |
| 228 | // decode overwrites this from the parsed sequence header |
| 229 | // (`set_sb128`, stream.rs). The encoder's own contexts start at |
| 230 | // the size this process encodes -- see [`FrameCtx::for_encoder`]. |
| 231 | sb128_flag: std::cell::Cell::new(false), |
| 232 | interp_filter: std::cell::Cell::new(crate::mc::InterpFilterKind::Regular), |
| 233 | lossless_per_seg: std::cell::Cell::new([false; MAX_SEGMENTS]), |
| 234 | disable_cdf_update: std::cell::Cell::new(false), |
| 235 | cdef_bits: std::cell::Cell::new(0), |
| 236 | bit_depth: std::cell::Cell::new(8), |
| 237 | mono_chrome: std::cell::Cell::new(false), |
| 238 | subsampling_x: std::cell::Cell::new(1), |
| 239 | subsampling_y: std::cell::Cell::new(1), |
| 240 | superres: std::cell::Cell::new((0, 8)), |
| 241 | cdef_transmitted: std::cell::Cell::new(false), |
| 242 | cdef_sb_cols: std::cell::Cell::new(0), |
| 243 | capture_stages: std::cell::Cell::new(false), |
| 244 | stages: std::cell::RefCell::new(None), |
| 245 | cdef_idx_grid: std::cell::RefCell::new(Vec::new()), |
| 246 | delta_q_present: std::cell::Cell::new(false), |
| 247 | delta_q_res: std::cell::Cell::new(1), |
| 248 | current_q_idx: std::cell::Cell::new(0), |
| 249 | quant_deltas: std::cell::Cell::new(crate::quant::QuantDeltas { |
| 250 | y_dc: 0, |
| 251 | u_dc: 0, |
| 252 | u_ac: 0, |
| 253 | v_dc: 0, |
| 254 | v_ac: 0, |
| 255 | }), |
| 256 | qm_levels: std::cell::Cell::new([crate::qm::NUM_QM_LEVELS as u8 - 1; 3]), |
| 257 | delta_lf_present: std::cell::Cell::new(false), |
| 258 | delta_lf_res: std::cell::Cell::new(1), |
| 259 | delta_lf_multi: std::cell::Cell::new(false), |
| 260 | current_delta_lf: std::cell::Cell::new([0; 4]), |
| 261 | seg: std::cell::RefCell::new(SegmentationParams::default()), |
| 262 | seg_ids: std::cell::RefCell::new(Vec::new()), |
| 263 | prev_seg_ids: std::cell::RefCell::new(Vec::new()), |
| 264 | seg_mi_dims: std::cell::Cell::new((0, 0)), |
| 265 | above_seg_pred: std::cell::RefCell::new(Vec::new()), |
| 266 | left_seg_pred: std::cell::RefCell::new(Vec::new()), |
| 267 | cur_segment_id: std::cell::Cell::new(0), |
| 268 | seg_tile_origin: std::cell::Cell::new((0, 0)), |
| 269 | tx_select_inter: std::cell::Cell::new(false), |
| 270 | tx_select_frame: std::cell::Cell::new(false), |
| 271 | intra_only: std::cell::Cell::new(false), |
| 272 | reduced_tx_set_inter: std::cell::Cell::new(false), |
| 273 | enable_filter_intra_inter: std::cell::Cell::new(false), |
| 274 | palette_pred: std::cell::RefCell::new(None), |
| 275 | intrabc_mi_grid: std::cell::RefCell::new(None), |
| 276 | intrabc_dv: std::cell::Cell::new(None), |
| 277 | luma_tx_type: std::cell::Cell::new(TxType::DctDct), |
| 278 | intrabc_chroma_tx: std::cell::Cell::new(None), |
| 279 | intra_in_inter_mode: std::cell::Cell::new(None), |
| 280 | inter_strip_chroma: std::cell::Cell::new(None), |
| 281 | inter_last_mc: std::cell::Cell::new(None), |
| 282 | enable_edge_filter: std::cell::Cell::new(false), |
| 283 | last_frame_wide_margin: std::cell::RefCell::new(None), |
| 284 | reach_sb_px: std::cell::Cell::new(crate::encode::SUPERBLOCK), |
| 285 | grain_bit_depth: std::cell::Cell::new(8), |
| 286 | recon_ops: std::cell::RefCell::new(Vec::new()), |
| 287 | recon_spare: std::cell::RefCell::new(Vec::new()), |
| 288 | coeff_arena: std::cell::RefCell::new(Vec::new()), |
| 289 | coeff_spare: std::cell::RefCell::new(Vec::new()), |
| 290 | pred_arena: std::cell::RefCell::new(Vec::new()), |
| 291 | pred_spare: std::cell::RefCell::new(Vec::new()), |
| 292 | recon_planes: std::cell::Cell::new(None), |
| 293 | wave: std::cell::RefCell::new(None), |
| 294 | wave_mark: std::cell::Cell::new(None), |
| 295 | } |
| 296 | } |
| 297 | |
| 298 | /// A context for the ENCODER's own trial decodes: it starts at the |
| 299 | /// superblock size this process encodes (`crate::encode::sb128_on`) so |
| 300 | /// the mode search's `Reach` and the filter search's internal decodes |
| 301 | /// agree with the tile writer. A decoder never reads that knob -- doing |
| 302 | /// so coupled every decode to the encoder's process-global and raced the |
| 303 | /// 128-superblock decode gates against the encoder witness. |
| 304 | pub(crate) fn for_encoder() -> Self { |
| 305 | let ctx = Self::new(); |
| 306 | let on = crate::encode::sb128_on(); |
| 307 | ctx.sb128_flag.set(on); |
| 308 | ctx.reach_sb_px.set(if on { 128 } else { crate::encode::SUPERBLOCK }); |
| 309 | ctx |
| 310 | } |
| 311 | } |
| 312 | |
| 313 | /// `LocalKey::with` for a plain value: lets every former `STATIC.with(|c| ..)` |
| 314 | /// site read `fctx.field.with(|c| ..)` unchanged. `LocalKey`'s own inherent |
| 315 | /// `with` still wins for the scratch pools and gate counters that stay |
| 316 | /// thread-local. |
| 317 | pub(crate) trait WithRef { |
| 318 | fn with<R>(&self, f: impl FnOnce(&Self) -> R) -> R { |
| 319 | f(self) |
| 320 | } |
| 321 | } |
| 322 | impl<T: ?Sized> WithRef for T {} |
| 323 | |
| 324 | /// A [`FrameCtx`] copy for one of [`crate::par`]'s filter worker threads. |
| 325 | /// |
| 326 | /// `FrameCtx` is `!Sync` on purpose (a decode's state belongs to one thread), |
| 327 | /// and the post-decode filter chain reads exactly these fields: the bit depth |
| 328 | /// (every kernel's clamp and threshold shift), the segmentation table |
| 329 | /// (`segment_id_at`/`lf_level` in the deblocker), the frame's superres state |
| 330 | /// and the film-grain bit depth. Everything else keeps [`FrameCtx::new`]'s |
| 331 | /// default -- a field that turns out to be read after all shows up as a hash |
| 332 | /// mismatch in |
| 333 | /// `a_real_stream_filters_identically_with_one_and_four_filter_threads`. |
| 334 | pub(crate) fn filter_ctx_copy(fctx: &FrameCtx) -> FrameCtx { |
| 335 | let out = FrameCtx::new(); |
| 336 | out.bit_depth.set(fctx.bit_depth.get()); |
| 337 | out.mono_chrome.set(fctx.mono_chrome.get()); |
| 338 | out.subsampling_x.set(fctx.subsampling_x.get()); |
| 339 | out.subsampling_y.set(fctx.subsampling_y.get()); |
| 340 | out.interp_filter.set(fctx.interp_filter.get()); |
| 341 | out.grain_bit_depth.set(fctx.grain_bit_depth.get()); |
| 342 | out.superres.set(fctx.superres.get()); |
| 343 | out.seg_mi_dims.set(fctx.seg_mi_dims.get()); |
| 344 | out.seg.replace(fctx.seg.borrow().clone()); |
| 345 | out.seg_ids.replace(fctx.seg_ids.borrow().clone()); |
| 346 | // lane-b128m: the superblock size travels with the copy. A tile SEARCH |
| 347 | // worker (`encode::search_tiles`) gets its context from here, so dropping |
| 348 | // it made every worker search at 64 while the calling thread searched at |
| 349 | // 128 -- the bytes a frame came out as then depended on the tile thread |
| 350 | // count under `EC_AV1_SB128`. |
| 351 | out.sb128_flag.set(fctx.sb128_flag.get()); |
| 352 | out.reach_sb_px.set(fctx.reach_sb_px.get()); |
| 353 | out |
| 354 | } |
| 355 | |
| 356 | use crate::cdf; |
| 357 | use crate::cdf_state::{Cdfs, MvComponentCdfs, TxbSet, TxbTables}; |
| 358 | use crate::encode::{Picture, Reach}; |
| 359 | use crate::intra::predict; |
| 360 | use crate::mc; |
| 361 | use crate::msac::SymbolDecoder; |
| 362 | use crate::mvstack::{ |
| 363 | ALTREF_FRAME, ALTREF2_FRAME, BWDREF_FRAME, GOLDEN_FRAME, LAST_FRAME, LAST2_FRAME, LAST3_FRAME, |
| 364 | MiGrid, MiInfo, NO_REF1, NO_SIGN_BIAS, NeighbourRef, SignBiasTable, comp_reference_type_ctx, |
| 365 | find_mv_stack_with_sign_bias, mv16, mv32, reference_mode_ctx, |
| 366 | single_ref_p1_ctx, single_ref_p2_ctx, single_ref_p3_ctx, single_ref_p4_ctx, single_ref_p5_ctx, |
| 367 | single_ref_p6_ctx, uni_comp_ref_p1_ctx, |
| 368 | }; |
| 369 | use crate::tile::{INTRA_MODE_CTX, block_grid, has_half}; |
| 370 | use crate::transform::{TxType, dequant_and_inverse_typed_wh}; |
| 371 | |
| 372 | const PARTITION_NONE: usize = 0; |
| 373 | const PARTITION_HORZ: usize = 1; |
| 374 | const PARTITION_VERT: usize = 2; |
| 375 | const PARTITION_SPLIT: usize = 3; |
| 376 | const PARTITION_HORZ_A: usize = 4; |
| 377 | const PARTITION_HORZ_B: usize = 5; |
| 378 | const PARTITION_VERT_A: usize = 6; |
| 379 | const PARTITION_VERT_B: usize = 7; |
| 380 | const PARTITION_HORZ_4: usize = 8; |
| 381 | const PARTITION_VERT_4: usize = 9; |
| 382 | const SB_MI: u32 = 16; |
| 383 | const BLOCK_MI: u32 = 8; |
| 384 | |
| 385 | // lane-sb128 r1: this sequence's `use_128x128_superblock` (spec 5.5.1). |
| 386 | // Every superblock-granular rule below (`delta_q`/`delta_lf`'s "is this the |
| 387 | // superblock's own top-left mi?", the loop-restoration read's unit span, the |
| 388 | // partition tree's own root level) keys off it; `SB_MI` stays the 64x64 |
| 389 | // *quadrant* size a 128 superblock is decoded in, which is also what the CDEF |
| 390 | // grid is indexed by (libaom `read_cdef` reads one `cdef_idx` per 64x64 |
| 391 | // inside a 128 superblock). |
| 392 | thread_local! { |
| 393 | /// lane-frame80: `EC_TRACE_COEFF_FRAME=<decode idx>` narrows the |
| 394 | /// `EC_TRACE_COEFF` rungs to a single frame in DECODE order (a 4K stream |
| 395 | /// prints gigabytes otherwise). Unset = every frame, as before. |
| 396 | static COEFF_TRACE_FRAME: std::cell::Cell<Option<usize>> = const { std::cell::Cell::new(None) }; |
| 397 | static COEFF_TRACE_CUR: std::cell::Cell<usize> = const { std::cell::Cell::new(usize::MAX) }; |
| 398 | } |
| 399 | |
| 400 | /// Called by [`crate::stream::decode_stream`] before each frame's tiles with |
| 401 | /// that frame's DECODE-order index. |
| 402 | pub fn set_coeff_trace_frame(idx: usize) { |
| 403 | COEFF_TRACE_FRAME.with(|c| { |
| 404 | if c.get().is_none() { |
| 405 | let want = match crate::envflags::var("EC_TRACE_COEFF_FRAME") { |
| 406 | Ok(v) => v.parse::<usize>().unwrap_or(usize::MAX), |
| 407 | // unset: keep tracing every frame, as before this rung existed |
| 408 | Err(_) => usize::MAX - 1, |
| 409 | }; |
| 410 | c.set(Some(want)); |
| 411 | } |
| 412 | }); |
| 413 | COEFF_TRACE_CUR.with(|c| c.set(idx)); |
| 414 | if crate::envflags::env_flag!("EC_TRACE_COEFF") { |
| 415 | eprintln!("EC_COEFF_FRAME decode_idx={idx}"); |
| 416 | } |
| 417 | } |
| 418 | |
| 419 | pub(crate) fn coeff_trace_on() -> bool { |
| 420 | if !crate::envflags::env_flag!("EC_TRACE_COEFF") { |
| 421 | return false; |
| 422 | } |
| 423 | match COEFF_TRACE_FRAME.with(|c| c.get()) { |
| 424 | // no `set_coeff_trace_frame` caller (direct tile-decode tests), or the |
| 425 | // env var unset: trace every frame. |
| 426 | None => true, |
| 427 | Some(w) if w >= usize::MAX - 1 => true, |
| 428 | Some(w) => COEFF_TRACE_CUR.with(|c| c.get()) == w, |
| 429 | } |
| 430 | } |
| 431 | |
| 432 | |
| 433 | /// Sets [`SB128`] for this thread (called once per frame from `stream.rs`). |
| 434 | pub(crate) fn set_sb128(v: bool, fctx: &crate::decode::FrameCtx) { |
| 435 | fctx.sb128_flag.with(|c| c.set(v)); |
| 436 | crate::encode::set_reach_superblock_px(if v { 128 } else { 64 }, fctx); |
| 437 | } |
| 438 | |
| 439 | /// lane-av1mixloss: sets [`FrameCtx::lossless_per_seg`] (called once per frame |
| 440 | /// from `stream.rs`). |
| 441 | pub(crate) fn set_lossless_per_seg( |
| 442 | table: [bool; MAX_SEGMENTS], |
| 443 | fctx: &crate::decode::FrameCtx, |
| 444 | ) { |
| 445 | fctx.lossless_per_seg.with(|c| c.set(table)); |
| 446 | } |
| 447 | |
| 448 | /// The BLOCK's own lossless bit: libaom's `xd->lossless[mbmi->segment_id]`. |
| 449 | /// The table is computed once per frame (libaom `decodeframe.c:5205`: |
| 450 | /// `qindex == 0` with every plane delta 0, `av1_get_qindex` applying the |
| 451 | /// segment's `SEG_LVL_ALT_Q`), but the frame never carries ONE lossless rule |
| 452 | /// -- `read_tx_size`, `read_tx_type` and `is_cfl_allowed` all index it by the |
| 453 | /// BLOCK's own segment id, which the spec-order `segment_id` read sets before |
| 454 | /// any mode/transform symbol of the block. A frame whose segments disagree |
| 455 | /// codes TX_4X4/WHT blocks and normal blocks side by side. |
| 456 | pub(crate) fn lossless(fctx: &crate::decode::FrameCtx) -> bool { |
| 457 | let seg_id = usize::from(fctx.cur_segment_id.with(|c| c.get())); |
| 458 | fctx.lossless_per_seg.with(|c| c.get())[seg_id] |
| 459 | } |
| 460 | |
| 461 | /// Sets [`FrameCtx::disable_cdf_update`] (called once per frame from |
| 462 | /// `stream.rs`). |
| 463 | pub(crate) fn set_disable_cdf_update(v: bool, fctx: &crate::decode::FrameCtx) { |
| 464 | fctx.disable_cdf_update.with(|c| c.set(v)); |
| 465 | } |
| 466 | |
| 467 | /// This frame's `disable_cdf_update`, for the tile readers -- |
| 468 | /// [`msac::SymbolDecoder::set_cdf_update`](crate::msac::SymbolDecoder::set_cdf_update). |
| 469 | pub(crate) fn cdf_update_disabled(fctx: &crate::decode::FrameCtx) -> bool { |
| 470 | fctx.disable_cdf_update.with(|c| c.get()) |
| 471 | } |
| 472 | |
| 473 | /// The transform side a block of `side` pixels codes: its own (capped at the |
| 474 | /// caller's own largest) normally, 4 on a lossless BLOCK, where libaom's |
| 475 | /// `read_tx_size` returns TX_4X4 before it looks at anything else |
| 476 | /// (`decodeframe.c`: `if (xd->lossless[xd->mi[0]->segment_id]) return |
| 477 | /// TX_4X4;`). |
| 478 | pub(crate) fn lossless_tx(px: usize, fctx: &crate::decode::FrameCtx) -> usize { |
| 479 | if lossless(fctx) { 4 } else { px } |
| 480 | } |
| 481 | |
| 482 | /// `is_cfl_allowed` (libaom `blockd.h`): CfL is offered on a block up to |
| 483 | /// 32x32 -- but on a LOSSLESS BLOCK only when the block's CHROMA plane block |
| 484 | /// is exactly BLOCK_4X4, i.e. (at 4:2:0) a luma block of 8x8 or smaller. |
| 485 | /// Getting this wrong picks the 14-symbol CFL `uv_mode` alphabet where libaom |
| 486 | /// picks the 13-symbol one (class `wrong-alphabet-same-value`). |
| 487 | pub(crate) fn cfl_allowed_px(bw: usize, bh: usize, fctx: &crate::decode::FrameCtx) -> bool { |
| 488 | if lossless(fctx) { |
| 489 | bw <= 8 && bh <= 8 |
| 490 | } else { |
| 491 | bw <= 32 && bh <= 32 |
| 492 | } |
| 493 | } |
| 494 | |
| 495 | fn sb128(fctx: &crate::decode::FrameCtx) -> bool { |
| 496 | fctx.sb128_flag.with(|c| c.get()) |
| 497 | } |
| 498 | |
| 499 | /// This sequence's superblock size in mi units (16 = 64x64, 32 = 128x128). |
| 500 | pub(crate) fn sb_mi_cur(fctx: &crate::decode::FrameCtx) -> u32 { |
| 501 | if sb128(fctx) { SB_MI * 2 } else { SB_MI } |
| 502 | } |
| 503 | |
| 504 | // How many real 128-level partition symbols this decoder has read, and how |
| 505 | // many superblocks resolved to `PARTITION_SPLIT` (coded or forced) -- the |
| 506 | // gate's proof a `--sb-size=128` stream exercised the 128 partition root. |
| 507 | thread_local! { |
| 508 | static PART128_SYMBOLS: std::cell::Cell<usize> = const { std::cell::Cell::new(0) }; |
| 509 | static PART128_SPLIT_HITS: std::cell::Cell<usize> = const { std::cell::Cell::new(0) }; |
| 510 | static PART128_GATHERED_SYMBOLS: std::cell::Cell<usize> = const { std::cell::Cell::new(0) }; |
| 511 | } |
| 512 | |
| 513 | /// Current value of `PART128_GATHERED_SYMBOLS`: 128 roots that read the |
| 514 | /// half-out-of-frame gathered split_or_horz/split_or_vert symbol (spec |
| 515 | /// 5.11.4), which this decoder used to read and throw away. |
| 516 | #[allow(dead_code)] // read only from the `#[cfg(test)]` gates |
| 517 | pub(crate) fn part128_gathered_symbols() -> usize { |
| 518 | PART128_GATHERED_SYMBOLS.with(|c| c.get()) |
| 519 | } |
| 520 | |
| 521 | /// Current value of [`PART128_SYMBOLS`]. |
| 522 | #[allow(dead_code)] // read only from the `#[cfg(test)]` gates |
| 523 | pub(crate) fn part128_symbols() -> usize { |
| 524 | PART128_SYMBOLS.with(|c| c.get()) |
| 525 | } |
| 526 | |
| 527 | /// Current value of [`PART128_SPLIT_HITS`]. |
| 528 | #[allow(dead_code)] // read only from the `#[cfg(test)]` gates |
| 529 | pub(crate) fn part128_split_hits() -> usize { |
| 530 | PART128_SPLIT_HITS.with(|c| c.get()) |
| 531 | } |
| 532 | |
| 533 | /// [`gather`] over an alphabet of any length -- the 128-level partition CDF |
| 534 | /// has 8 symbols (no `PARTITION_HORZ_4`/`_VERT_4` at `BLOCK_128X128`, spec |
| 535 | /// `partition_cdf_length`), so its forced-split gather sums 5 elements where |
| 536 | /// the 64/32 levels sum 6. |
| 537 | pub(crate) fn gather_of(cdf: &[u16], elements: &[usize]) -> [u16; 3] { |
| 538 | let split: u16 = elements.iter().map(|&e| element_prob(cdf, e)).sum(); |
| 539 | [32768 - split, 32768, 0] |
| 540 | } |
| 541 | |
| 542 | /// [`VERT_ALIKE`]/[`HORZ_ALIKE`] without the 1:4 arms, for the 128 root. |
| 543 | pub(crate) const VERT_ALIKE128: [usize; 5] = [2, PARTITION_SPLIT, 4, 6, 7]; |
| 544 | pub(crate) const HORZ_ALIKE128: [usize; 5] = [1, PARTITION_SPLIT, 4, 5, 6]; |
| 545 | |
| 546 | // How many `use_filter_intra` symbols this decoder has read as `1`, across |
| 547 | // every call on the current thread -- the cheap counter [`filter_intra_hits`] gate |
| 548 | // tests read (before/after, not the absolute value) to prove a stream |
| 549 | // actually exercised the filter-intra predictor rather than silently |
| 550 | // skipping it. |
| 551 | thread_local! { |
| 552 | static FILTER_INTRA_HITS: std::cell::Cell<usize> = |
| 553 | const { std::cell::Cell::new(0) }; |
| 554 | } |
| 555 | |
| 556 | /// Current value of [`FILTER_INTRA_HITS`]. |
| 557 | #[allow(dead_code)] // read only from the `#[cfg(test)]` gates |
| 558 | pub(crate) fn filter_intra_hits() -> usize { |
| 559 | FILTER_INTRA_HITS.with(|c| c.get()) |
| 560 | } |
| 561 | |
| 562 | // lane-fiinter r1: how many of those `use_filter_intra == 1` symbols belonged |
| 563 | // to an INTRA block inside an INTER frame (`read_intra_block_mode_info`'s own |
| 564 | // `read_filter_intra_mode_info` call), which no gate exercised before this |
| 565 | // round -- every inter gate recipe passed `--enable-filter-intra=0`. |
| 566 | thread_local! { |
| 567 | static INTRA_IN_INTER_FILTER_INTRA_HITS: std::cell::Cell<usize> = |
| 568 | const { std::cell::Cell::new(0) }; |
| 569 | } |
| 570 | |
| 571 | /// Current value of [`INTRA_IN_INTER_FILTER_INTRA_HITS`]. |
| 572 | #[allow(dead_code)] // read only from the `#[cfg(test)]` gates |
| 573 | pub(crate) fn intra_in_inter_filter_intra_hits() -> usize { |
| 574 | INTRA_IN_INTER_FILTER_INTRA_HITS.with(|c| c.get()) |
| 575 | } |
| 576 | |
| 577 | // lane-angleinter r1: how many INTRA blocks inside an INTER frame carried a |
| 578 | // nonzero `angle_delta_y` / `angle_delta_uv` (`read_intra_angle_info`). Both |
| 579 | // arms refused a nonzero luma delta before this round, and no inter gate |
| 580 | // enabled `--enable-angle-delta` (aomenc's own default is 1). |
| 581 | thread_local! { |
| 582 | static INTRA_IN_INTER_ANGLE_DELTA_Y_HITS: std::cell::Cell<usize> = |
| 583 | const { std::cell::Cell::new(0) }; |
| 584 | static INTRA_IN_INTER_ANGLE_DELTA_UV_HITS: std::cell::Cell<usize> = |
| 585 | const { std::cell::Cell::new(0) }; |
| 586 | } |
| 587 | |
| 588 | /// Current value of [`INTRA_IN_INTER_ANGLE_DELTA_Y_HITS`]. |
| 589 | #[allow(dead_code)] // read only from the `#[cfg(test)]` gates |
| 590 | pub(crate) fn intra_in_inter_angle_delta_y_hits() -> usize { |
| 591 | INTRA_IN_INTER_ANGLE_DELTA_Y_HITS.with(|c| c.get()) |
| 592 | } |
| 593 | |
| 594 | /// Current value of [`INTRA_IN_INTER_ANGLE_DELTA_UV_HITS`]. |
| 595 | #[allow(dead_code)] // read only from the `#[cfg(test)]` gates |
| 596 | pub(crate) fn intra_in_inter_angle_delta_uv_hits() -> usize { |
| 597 | INTRA_IN_INTER_ANGLE_DELTA_UV_HITS.with(|c| c.get()) |
| 598 | } |
| 599 | |
| 600 | // lane-uv8 r1: the three non-DC chroma mode classes an INTRA 8x8 leaf inside |
| 601 | // an INTER frame ([`decode_inter_block8`]'s intra arm) can carry -- the arm |
| 602 | // read `uv_mode` and then refused everything but `DC_PRED`. Counted here and |
| 603 | // nowhere else so the gate's assert cannot be satisfied by a >=16x16 block. |
| 604 | thread_local! { |
| 605 | static INTRA_IN_INTER8_UV_DIR_HITS: std::cell::Cell<usize> = const { std::cell::Cell::new(0) }; |
| 606 | static INTRA_IN_INTER8_UV_SMOOTH_HITS: std::cell::Cell<usize> = const { std::cell::Cell::new(0) }; |
| 607 | static INTRA_IN_INTER8_UV_CFL_HITS: std::cell::Cell<usize> = const { std::cell::Cell::new(0) }; |
| 608 | } |
| 609 | |
| 610 | thread_local! { |
| 611 | static INTRA_IN_INTER8_ANGLE_DELTA_Y_HITS: std::cell::Cell<usize> = const { std::cell::Cell::new(0) }; |
| 612 | } |
| 613 | |
| 614 | /// Nonzero luma angle deltas coded by an intra 8x8 leaf inside an inter frame |
| 615 | /// (lane-leaf8tx r1) -- counted only in [`decode_inter_block8`]'s intra arm. |
| 616 | #[allow(dead_code)] // read only from the `#[cfg(test)]` gates |
| 617 | pub(crate) fn intra_in_inter8_angle_delta_y_hits() -> usize { |
| 618 | INTRA_IN_INTER8_ANGLE_DELTA_Y_HITS.with(|c| c.get()) |
| 619 | } |
| 620 | |
| 621 | /// `(directional, smooth/paeth, CfL)` chroma modes coded by an intra 8x8 leaf |
| 622 | /// inside an inter frame -- see [`INTRA_IN_INTER8_UV_DIR_HITS`]. |
| 623 | #[allow(dead_code)] // read only from the `#[cfg(test)]` gates |
| 624 | pub(crate) fn intra_in_inter8_uv_hits() -> (usize, usize, usize) { |
| 625 | ( |
| 626 | INTRA_IN_INTER8_UV_DIR_HITS.with(|c| c.get()), |
| 627 | INTRA_IN_INTER8_UV_SMOOTH_HITS.with(|c| c.get()), |
| 628 | INTRA_IN_INTER8_UV_CFL_HITS.with(|c| c.get()), |
| 629 | ) |
| 630 | } |
| 631 | |
| 632 | // lane-realworld r1: this frame's `cdef.bits` (spec 5.9.19), threaded into |
| 633 | // [`maybe_read_cdef_idx`] the same way [`ENABLE_EDGE_FILTER`] threads a |
| 634 | // frame-level flag through the recursive block decode -- `0` (the default) |
| 635 | // makes every call a true no-op, matching every existing gate stream's |
| 636 | // `cdef_bits == 0` behaviour exactly. |
| 637 | // lane-realworld r8: the sequence header's `color_config.bit_depth`, set |
| 638 | // once per `decode_key_frame_tile_with_cdfs`/`decode_inter_frame_tile_with_cdfs` |
| 639 | // call the same way `CDEF_BITS` is above. `stream.rs`'s own refusal still |
| 640 | // blocks every non-8-bit stream from reaching a decode call, so this is `8` |
| 641 | // on every path that runs today -- threading it now (rather than the literal |
| 642 | // `8`) is stage 1 of widening past that refusal without re-deriving every |
| 643 | // clamp bound later. `PlaneBuf`'s sample storage stays `u16` regardless of |
| 644 | // depth (this decoder's own internal choice, matches dav1d/libaom). |
| 645 | /// `(1 << bit_depth) - 1` — the reconstruction clamp bound for the current |
| 646 | /// stream's [`BIT_DEPTH`], replacing the crate-wide literal `255`. |
| 647 | pub(crate) fn sample_max(fctx: &crate::decode::FrameCtx) -> i32 { |
| 648 | (1i32 << fctx.bit_depth.with(std::cell::Cell::get)) - 1 |
| 649 | } |
| 650 | /// The current stream's bit depth, for callers (e.g. dequant) that need the |
| 651 | /// raw value rather than the derived clamp bound. |
| 652 | pub(crate) fn bit_depth(fctx: &crate::decode::FrameCtx) -> u8 { |
| 653 | fctx.bit_depth.with(std::cell::Cell::get) |
| 654 | } |
| 655 | /// Sets [`BIT_DEPTH`] for the current thread, called once per frame from |
| 656 | /// [`crate::stream`]'s decode loop. |
| 657 | pub(crate) fn set_bit_depth(bit_depth: u8, fctx: &crate::decode::FrameCtx) { |
| 658 | fctx.bit_depth.with(|c| c.set(bit_depth)); |
| 659 | } |
| 660 | |
| 661 | /// This stream's `mono_chrome` (see [`FrameCtx::mono_chrome`]): a |
| 662 | /// monochrome frame has `NumPlanes == 1`, so no block codes any chroma |
| 663 | /// symbol or chroma coefficient and the output picture is luma only. |
| 664 | pub(crate) fn mono(fctx: &crate::decode::FrameCtx) -> bool { |
| 665 | fctx.mono_chrome.with(std::cell::Cell::get) |
| 666 | } |
| 667 | |
| 668 | /// Sets [`mono`] for the current thread, called once per frame from |
| 669 | /// [`crate::stream`]'s decode loop like [`set_bit_depth`]. |
| 670 | pub(crate) fn set_mono(mono_chrome: bool, fctx: &crate::decode::FrameCtx) { |
| 671 | fctx.mono_chrome.with(|c| c.set(mono_chrome)); |
| 672 | } |
| 673 | |
| 674 | /// Sequence subsampling. Set once per frame from stream.rs, like set_mono. |
| 675 | pub(crate) fn set_subsampling(ss_x: u8, ss_y: u8, fctx: &crate::decode::FrameCtx) { |
| 676 | fctx.subsampling_x.set(ss_x); |
| 677 | fctx.subsampling_y.set(ss_y); |
| 678 | } |
| 679 | |
| 680 | #[inline] |
| 681 | pub(crate) fn ss_x(fctx: &crate::decode::FrameCtx) -> usize { |
| 682 | fctx.subsampling_x.get() as usize |
| 683 | } |
| 684 | |
| 685 | #[inline] |
| 686 | pub(crate) fn ss_y(fctx: &crate::decode::FrameCtx) -> usize { |
| 687 | fctx.subsampling_y.get() as usize |
| 688 | } |
| 689 | |
| 690 | /// libaom ROUND_POWER_OF_TWO. At ss=1 equals div_ceil(dim, 2); at ss=0 is dim. |
| 691 | #[inline] |
| 692 | pub(crate) fn round_ss(dim: usize, ss: usize) -> usize { |
| 693 | (dim + ((1usize << ss) >> 1)) >> ss |
| 694 | } |
| 695 | |
| 696 | |
| 697 | /// This frame's superres state: `(upscaled_width, superres_denom)`, or |
| 698 | /// `(0, 8)` when the frame is not scaled. Spec 7.16's upscale runs |
| 699 | /// between CDEF and loop restoration, so LOOP RESTORATION -- unit grid, |
| 700 | /// `read_lr`'s unit corners and the pixel filters themselves -- works in |
| 701 | /// UPSCALED coordinates while deblock and CDEF work in downscaled ones |
| 702 | /// (libaom `av1_superres_upscale` in `decodeframe.c`, and |
| 703 | /// `av1_loop_restoration_corners_in_sb`'s own `mi_to_num_x`/`denom_x` |
| 704 | /// superres arms). Set once per frame from [`crate::stream`]'s decode |
| 705 | /// loop, like [`BIT_DEPTH`], and reset to `(0, 8)` on an unscaled frame |
| 706 | /// so a later frame never inherits it. |
| 707 | |
| 708 | /// Sets [`SUPERRES`] for the current thread. `upscaled_width` is 0 (and |
| 709 | /// `denom` 8) for a frame that is not superres-scaled. |
| 710 | pub(crate) fn set_superres(upscaled_width: u32, denom: u32, fctx: &crate::decode::FrameCtx) { |
| 711 | fctx.superres.with(|c| c.set((upscaled_width, denom))); |
| 712 | } |
| 713 | |
| 714 | /// `(upscaled_width, superres_denom)` when this frame is superres-scaled |
| 715 | /// (libaom `av1_superres_scaled`), else `None`. |
| 716 | pub(crate) fn superres(fctx: &crate::decode::FrameCtx) -> Option<(u32, u32)> { |
| 717 | let (w, d) = fctx.superres.with(std::cell::Cell::get); |
| 718 | (w > 0 && d != 8).then_some((w, d)) |
| 719 | } |
| 720 | // Whether this superblock's single CDEF unit (this decoder has no 128x128 |
| 721 | // SB, so the SB IS the CDEF unit) has already had its `cdef_idx` literal |
| 722 | // read -- reset to `false` at the top of each superblock in the two tile |
| 723 | // loops, spec `read_cdef`'s `cdef_transmitted[index]`. |
| 724 | // Number of superblock columns in the current tile's grid, so |
| 725 | // [`maybe_read_cdef_idx`] can turn an `(mi_r, mi_c)` into a flat index into |
| 726 | // [`CDEF_IDX_GRID`]. |
| 727 | // One resolved `cdef_idx` per superblock, read by [`apply_cdef`] to select |
| 728 | // which of the header's `1 << cdef.bits` strength pairs applies to that |
| 729 | // superblock's samples. |
| 730 | // How many real (`cdef.bits > 0`) `cdef_idx` literals [`maybe_read_cdef_idx`] |
| 731 | // has read, across every call on the current thread -- the gate's proof that a |
| 732 | // `cdef_bits > 0` stream actually reached the new reader rather than every |
| 733 | // superblock coincidentally being all-skip. |
| 734 | thread_local! { |
| 735 | static CDEF_IDX_HITS: std::cell::Cell<usize> = const { std::cell::Cell::new(0) }; |
| 736 | } |
| 737 | |
| 738 | // How many times a warp was suppressed for one PLANE because that plane's own |
| 739 | // block is narrower or shorter than 8 (`av1_init_warp_params`'s own bail-out, |
| 740 | // reconinter.c) -- the gate's proof that a stream really carries the shape, |
| 741 | // since the square `side`/`chroma_side` can be 8 or more while the plane's |
| 742 | // true block is 4 wide (an 8x16 luma block's 4x8 chroma). |
| 743 | thread_local! { |
| 744 | static WARP_PLANE_SUPPRESS_HITS: std::cell::Cell<usize> = const { std::cell::Cell::new(0) }; |
| 745 | } |
| 746 | |
| 747 | /// Current value of [`WARP_PLANE_SUPPRESS_HITS`]. |
| 748 | #[allow(dead_code)] // read only from the `#[cfg(test)]` gates |
| 749 | pub(crate) fn warp_plane_suppress_hits() -> usize { |
| 750 | WARP_PLANE_SUPPRESS_HITS.with(|c| c.get()) |
| 751 | } |
| 752 | |
| 753 | /// `av1_init_warp_params` (libaom reconinter.c): warp is decided per PLANE and |
| 754 | /// bails out at `block_width < 8 || block_height < 8`, so the plane's OWN |
| 755 | /// block dimensions decide it -- never the square `side`/`chroma_side`, which |
| 756 | /// for a rectangular luma block (8x16, 16x8) or a PARTITION_HORZ/VERT strip is |
| 757 | /// bigger than the real block in one axis. |
| 758 | pub(crate) fn warp_plane_allowed(w: usize, h: usize) -> bool { |
| 759 | if w >= 8 && h >= 8 { |
| 760 | return true; |
| 761 | } |
| 762 | hit!(WARP_PLANE_SUPPRESS_HITS); |
| 763 | false |
| 764 | } |
| 765 | |
| 766 | /// Current value of [`CDEF_IDX_HITS`]. |
| 767 | #[allow(dead_code)] // read only from the `#[cfg(test)]` gates |
| 768 | pub(crate) fn cdef_idx_hits() -> usize { |
| 769 | CDEF_IDX_HITS.with(|c| c.get()) |
| 770 | } |
| 771 | |
| 772 | // lane-t900 r6: gate proof for the three r2-r4 root causes that shipped with no |
| 773 | // counter -- the in-inter tx-depth context (`tx_size_context_txfm_rect`), the |
| 774 | // non-chroma-reference block that must LEAVE its neighbours' chroma coefficient |
| 775 | // contexts alone, and the intra-edge filter neighbour pair taken mi-exact |
| 776 | // instead of from the coarse 16x16 cell. |
| 777 | thread_local! { |
| 778 | static INTRA_IN_INTER_TXCTX_HITS: std::cell::Cell<usize> = const { std::cell::Cell::new(0) }; |
| 779 | static NON_CHROMA_REF_CTX_SKIP_HITS: std::cell::Cell<usize> = const { std::cell::Cell::new(0) }; |
| 780 | static EDGE_FILTER_MI_FIX_HITS: std::cell::Cell<usize> = const { std::cell::Cell::new(0) }; |
| 781 | } |
| 782 | |
| 783 | /// Current value of [`INTRA_IN_INTER_TXCTX_HITS`]. |
| 784 | #[allow(dead_code)] // read only from the `#[cfg(test)]` gates |
| 785 | pub(crate) fn intra_in_inter_txctx_hits() -> usize { |
| 786 | INTRA_IN_INTER_TXCTX_HITS.with(|c| c.get()) |
| 787 | } |
| 788 | |
| 789 | /// Current value of [`NON_CHROMA_REF_CTX_SKIP_HITS`]. |
| 790 | #[allow(dead_code)] // read only from the `#[cfg(test)]` gates |
| 791 | pub(crate) fn non_chroma_ref_ctx_skip_hits() -> usize { |
| 792 | NON_CHROMA_REF_CTX_SKIP_HITS.with(|c| c.get()) |
| 793 | } |
| 794 | |
| 795 | /// Current value of [`EDGE_FILTER_MI_FIX_HITS`]. |
| 796 | #[allow(dead_code)] // read only from the `#[cfg(test)]` gates |
| 797 | pub(crate) fn edge_filter_mi_fix_hits() -> usize { |
| 798 | EDGE_FILTER_MI_FIX_HITS.with(|c| c.get()) |
| 799 | } |
| 800 | |
| 801 | // lane-realworld r4: this frame's `delta.q_present` (spec 5.9.17) and its |
| 802 | // actual step (`1 << delta.q_res`, already the real multiplier, not the raw |
| 803 | // 2-bit field). `false`/`1` (the defaults) make [`maybe_read_delta_q`] a |
| 804 | // true no-op, matching every existing gate stream's `delta_q_present == 0` |
| 805 | // behaviour exactly, the same corner-cut [`CDEF_BITS`] takes above. |
| 806 | // The running quantizer index (spec `CurrentQIndex`, libaom |
| 807 | // `xd->current_base_qindex`) -- reset to the frame's own `base_q_idx` at the |
| 808 | // top of every tile (spec `decode_tile`), carried across superblocks within |
| 809 | // that tile, and mutated only by [`maybe_read_delta_q`]. Read back at the |
| 810 | // dequantization call sites instead of the frame-constant `base_q_idx`. |
| 811 | // This frame's own per-plane DC/AC quantizer-index deltas (lane-sbpart r11, |
| 812 | // spec 5.9.12/7.12.2, [`crate::quant::QuantDeltas`]) -- frame-constant (not |
| 813 | // per-superblock like [`CURRENT_Q_IDX`]), set once per frame decode and read |
| 814 | // back at every dequantization call site keyed on which plane it is doing. |
| 815 | // How many real `delta_q_present` symbol groups [`maybe_read_delta_q`] has |
| 816 | // read, across every call on the current thread -- the gate's proof that a |
| 817 | // `delta_q_present` stream actually reached the new reader. |
| 818 | thread_local! { |
| 819 | static DELTA_Q_HITS: std::cell::Cell<usize> = const { std::cell::Cell::new(0) }; |
| 820 | } |
| 821 | |
| 822 | /// Current value of [`DELTA_Q_HITS`]. |
| 823 | #[allow(dead_code)] // read only from the `#[cfg(test)]` gates |
| 824 | pub(crate) fn delta_q_hits() -> usize { |
| 825 | DELTA_Q_HITS.with(|c| c.get()) |
| 826 | } |
| 827 | |
| 828 | /// This frame's [`QUANT_DELTAS`] `(dc, ac)` pair for plane `plane_idx` (0 = |
| 829 | /// Y, 1 = U, 2 = V) -- luma has no coded `ac` delta (spec 5.9.12 never reads |
| 830 | /// one), so it is always `0` here. |
| 831 | fn plane_q_delta(plane_idx: usize, fctx: &crate::decode::FrameCtx) -> (i32, i32) { |
| 832 | let d = fctx.quant_deltas.with(|c| c.get()); |
| 833 | match plane_idx { |
| 834 | 0 => (d.y_dc, 0), |
| 835 | 1 => (d.u_dc, d.u_ac), |
| 836 | _ => (d.v_dc, d.v_ac), |
| 837 | } |
| 838 | } |
| 839 | |
| 840 | /// lane-av1-qmatrix: records this FRAME's quantisation-matrix levels |
| 841 | /// ([`crate::decode::FrameCtx::qm_levels`]) from the parsed |
| 842 | /// `quantization_params`. Called from `stream.rs` at the frame header, where |
| 843 | /// the refusal this lane lifted used to sit. |
| 844 | pub(crate) fn set_qm_levels(levels: [u8; 3], fctx: &crate::decode::FrameCtx) { |
| 845 | fctx.qm_levels.with(|c| c.set(levels)); |
| 846 | } |
| 847 | |
| 848 | /// This block's inverse quantisation matrix -- libaom `av1_get_iqmatrix` |
| 849 | /// (`av1/common/quant_common.c:243`) plus `setup_segmentation_dequant`'s |
| 850 | /// `av1_use_qmatrix` gate, evaluated per dequantisation call site the same |
| 851 | /// way the site already evaluates its quantizer index. `None` dequantises |
| 852 | /// flat (the only behaviour before lane-av1-qmatrix): |
| 853 | /// - a level of 15, which is both libaom's flat sentinel and what every |
| 854 | /// `using_qmatrix == 0` frame's levels are parked at; |
| 855 | /// - a 1D/identity transform (`is_2d_transform` false: libaom's |
| 856 | /// `av1_get_iqmatrix` reads the NULL slot for those); |
| 857 | /// - a LOSSLESS block (libaom `av1_use_qmatrix`'s `!xd->lossless[seg]`: the |
| 858 | /// segment-adjusted quantizer index clamped to 0 with all five plane |
| 859 | /// deltas zero -- the same `block_q_idx` every other dequant site reads). |
| 860 | fn block_iqmatrix( |
| 861 | fctx: &crate::decode::FrameCtx, |
| 862 | plane_idx: usize, |
| 863 | w: usize, |
| 864 | h: usize, |
| 865 | tx_type: TxType, |
| 866 | ) -> Option<crate::qm::Iqm> { |
| 867 | let levels = fctx.qm_levels.with(|c| c.get()); |
| 868 | let level = levels[plane_idx.min(2)]; |
| 869 | if usize::from(level) >= crate::qm::NUM_QM_LEVELS - 1 { |
| 870 | return None; |
| 871 | } |
| 872 | if !tx_type.is_2d_transform() { |
| 873 | return None; |
| 874 | } |
| 875 | let d = fctx.quant_deltas.with(|c| c.get()); |
| 876 | if block_q_idx(fctx) == 0 |
| 877 | && d.y_dc == 0 |
| 878 | && d.u_dc == 0 |
| 879 | && d.u_ac == 0 |
| 880 | && d.v_dc == 0 |
| 881 | && d.v_ac == 0 |
| 882 | { |
| 883 | return None; |
| 884 | } |
| 885 | crate::qm::iwt_matrix(usize::from(level), plane_idx != 0, w, h) |
| 886 | } |
| 887 | |
| 888 | /// `read_delta_qindex`/`read_delta_q_params` (spec 5.11.10, libaom |
| 889 | /// `decodemv.c:85-146,735-770`), called right after [`maybe_read_cdef_idx`] |
| 890 | /// at every block-decode call site (spec order `skip -> cdef -> delta_q`). |
| 891 | /// A no-op when `delta_q_present` is unset, or when this block is not at the |
| 892 | /// superblock's own top-left MI position (`b_row == 0 && b_col == 0` in |
| 893 | /// libaom, collapsed here to a position check since a partition tree's |
| 894 | /// top-left leaf is the only block that can ever land there), or when that |
| 895 | /// leaf both *is* the whole superblock and is `skip` (the one case libaom |
| 896 | /// itself skips the read, state carrying over unchanged). |
| 897 | fn maybe_read_delta_q(dec: &mut SymbolDecoder, cdfs: &mut Cdfs, mi_r: usize, mi_c: usize, is_whole_sb: bool, skip: bool, fctx: &crate::decode::FrameCtx) { |
| 898 | if !fctx.delta_q_present.with(|c| c.get()) { |
| 899 | return; |
| 900 | } |
| 901 | let sb_mi = sb_mi_cur(fctx) as usize; |
| 902 | if mi_r % sb_mi != 0 || mi_c % sb_mi != 0 { |
| 903 | return; |
| 904 | } |
| 905 | // `is_whole_sb` is the caller's "this leaf IS the superblock" flag, |
| 906 | // computed against the sequence's own superblock size (lane-sb128b r1: |
| 907 | // it used to be hardcoded against 64 and disabled under `sb128`, which a |
| 908 | // `PARTITION_NONE` 128x128 root -- itself the whole superblock -- makes |
| 909 | // wrong). |
| 910 | if is_whole_sb && skip { |
| 911 | return; |
| 912 | } |
| 913 | let mut abs = dec.symbol(&mut cdfs.delta_q) as i32; |
| 914 | const DELTA_Q_SMALL: i32 = 3; |
| 915 | if abs >= DELTA_Q_SMALL { |
| 916 | let rem_bits = dec.literal(3) + 1; |
| 917 | let thr = (1i32 << rem_bits) + 1; |
| 918 | abs = dec.literal(rem_bits) as i32 + thr; |
| 919 | } |
| 920 | let sign_negative = if abs != 0 { dec.literal(1) != 0 } else { true }; |
| 921 | let reduced = if sign_negative { -abs } else { abs }; |
| 922 | hit!(DELTA_Q_HITS); |
| 923 | let res = fctx.delta_q_res.with(|c| c.get()); |
| 924 | fctx.current_q_idx.with(|c| c.set((c.get() + reduced * res).clamp(1, 255))); |
| 925 | } |
| 926 | |
| 927 | // lane-realworld r5: this frame's `delta.lf_present`/`lf_res`/`lf_multi` |
| 928 | // (spec 5.9.18) -- same no-op-by-default corner-cut as `DELTA_Q_PRESENT`. |
| 929 | // The running per-plane/direction loop-filter delta (spec `DeltaLF[i]`, |
| 930 | // `i` in `0..FRAME_LF_COUNT`) -- reset to `[0; 4]` at the top of every tile, |
| 931 | // mutated only by [`maybe_read_delta_lf`]. `!delta_lf_multi` mode only ever |
| 932 | // updates index 0 and every reader treats that as the value for all 4 |
| 933 | // indices (spec `read_delta_lf_params`'s own single-cdf branch). |
| 934 | // How many real `delta_lf_present` symbol groups [`maybe_read_delta_lf`] has |
| 935 | // read -- the gate's proof the reader fired, mirroring [`DELTA_Q_HITS`]. |
| 936 | thread_local! { |
| 937 | static DELTA_LF_HITS: std::cell::Cell<usize> = const { std::cell::Cell::new(0) }; |
| 938 | } |
| 939 | |
| 940 | /// Current value of [`DELTA_LF_HITS`]. |
| 941 | #[allow(dead_code)] // read only from the `#[cfg(test)]` gates |
| 942 | pub(crate) fn delta_lf_hits() -> usize { |
| 943 | DELTA_LF_HITS.with(|c| c.get()) |
| 944 | } |
| 945 | |
| 946 | /// `read_delta_lflevel`/`read_delta_lf_params` (spec 5.11.11, libaom |
| 947 | /// `decodemv.c:99-146,735-770`) -- same shape and same call-site gating as |
| 948 | /// [`maybe_read_delta_q`] (spec order `delta_q -> delta_lf`), looped over |
| 949 | /// `FRAME_LF_COUNT = 4` planes/directions when `delta_lf_multi` is set, or |
| 950 | /// read once into index 0 otherwise. |
| 951 | fn maybe_read_delta_lf(dec: &mut SymbolDecoder, cdfs: &mut Cdfs, mi_r: usize, mi_c: usize, is_whole_sb: bool, skip: bool, fctx: &crate::decode::FrameCtx) { |
| 952 | if !fctx.delta_lf_present.with(|c| c.get()) { |
| 953 | return; |
| 954 | } |
| 955 | let sb_mi = sb_mi_cur(fctx) as usize; |
| 956 | if mi_r % sb_mi != 0 || mi_c % sb_mi != 0 { |
| 957 | return; |
| 958 | } |
| 959 | // `is_whole_sb` is the caller's "this leaf IS the superblock" flag, |
| 960 | // computed against the sequence's own superblock size (lane-sb128b r1: |
| 961 | // it used to be hardcoded against 64 and disabled under `sb128`, which a |
| 962 | // `PARTITION_NONE` 128x128 root -- itself the whole superblock -- makes |
| 963 | // wrong). |
| 964 | if is_whole_sb && skip { |
| 965 | return; |
| 966 | } |
| 967 | let multi = fctx.delta_lf_multi.with(|c| c.get()); |
| 968 | let res = fctx.delta_lf_res.with(|c| c.get()); |
| 969 | let count = if multi { 4 } else { 1 }; |
| 970 | let mut cur = fctx.current_delta_lf.with(|c| c.get()); |
| 971 | for i in 0..count { |
| 972 | let mut abs = if multi { |
| 973 | dec.symbol(&mut cdfs.delta_lf_multi[i]) as i32 |
| 974 | } else { |
| 975 | dec.symbol(&mut cdfs.delta_lf) as i32 |
| 976 | }; |
| 977 | const DELTA_LF_SMALL: i32 = 3; |
| 978 | if abs >= DELTA_LF_SMALL { |
| 979 | let rem_bits = dec.literal(3) + 1; |
| 980 | let thr = (1i32 << rem_bits) + 1; |
| 981 | abs = dec.literal(rem_bits) as i32 + thr; |
| 982 | } |
| 983 | let sign_negative = if abs != 0 { dec.literal(1) != 0 } else { true }; |
| 984 | let reduced = if sign_negative { -abs } else { abs }; |
| 985 | cur[i] = (cur[i] + reduced * res).clamp(-63, 63); |
| 986 | hit!(DELTA_LF_HITS); |
| 987 | } |
| 988 | fctx.current_delta_lf.with(|c| c.set(cur)); |
| 989 | } |
| 990 | |
| 991 | /// `read_cdef` (spec 5.11.56, libaom `decodemv.c:read_cdef`), called right |
| 992 | /// after `skip` is known at every block-decode call site. A no-op at |
| 993 | /// `cdef.bits == 0` (0-bit literal, nothing to consume) -- and |
| 994 | /// `read_cdef_params` already forces `bits = 0` under `coded_lossless`/ |
| 995 | /// `allow_intrabc`, so neither needs a separate check here. Reads once per |
| 996 | /// superblock, at the first non-skip block. |
| 997 | fn maybe_read_cdef_idx( |
| 998 | dec: &mut SymbolDecoder, |
| 999 | mi_r: usize, |
| 1000 | mi_c: usize, |
| 1001 | // The reading BLOCK's own span in 4x4 mode-info units: a block wider or |
| 1002 | // taller than one 64x64 CDEF unit is the first non-skip block of EVERY |
| 1003 | // unit it covers, so its one literal drives all of them (lane-b128r). |
| 1004 | (w_mi, h_mi): (usize, usize), |
| 1005 | skip: bool, |
| 1006 | fctx: &crate::decode::FrameCtx, |
| 1007 | ) { |
| 1008 | let bits = fctx.cdef_bits.with(|c| c.get()); |
| 1009 | if bits == 0 || skip || fctx.cdef_transmitted.with(|c| c.get()) { |
| 1010 | return; |
| 1011 | } |
| 1012 | let idx = dec.literal(u32::from(bits)) as u8; |
| 1013 | fctx.cdef_transmitted.with(|c| c.set(true)); |
| 1014 | hit!(CDEF_IDX_HITS); |
| 1015 | let (sb_r, sb_c) = (mi_r / SB_MI as usize, mi_c / SB_MI as usize); |
| 1016 | let sb_cols = fctx.cdef_sb_cols.with(|c| c.get()); |
| 1017 | if sb_cols > 0 { |
| 1018 | // lane-b128r: libaom's four CDEF units of a 128 superblock all read |
| 1019 | // their strength off the MB_MODE_INFO of the first non-skip block in |
| 1020 | // their own unit -- and a block that COVERS several units (the 128 |
| 1021 | // root's own 128x128 block, the only one there is) is that block for |
| 1022 | // every unit it covers, so one literal drives all four. This grid is |
| 1023 | // per 64x64 unit, so the read stamps every unit the block covers; one |
| 1024 | // that is smaller than a unit stamps exactly the cell it always did. |
| 1025 | let (rows, cols) = ( |
| 1026 | (h_mi / SB_MI as usize).max(1), |
| 1027 | (w_mi / SB_MI as usize).max(1), |
| 1028 | ); |
| 1029 | fctx.cdef_idx_grid.with(|g| { |
| 1030 | let mut g = g.borrow_mut(); |
| 1031 | for dr in 0..rows { |
| 1032 | for dc in 0..cols { |
| 1033 | let i = (sb_r + dr) * sb_cols + sb_c + dc; |
| 1034 | if sb_c + dc < sb_cols && i < g.len() { |
| 1035 | g[i] = idx; |
| 1036 | } |
| 1037 | } |
| 1038 | } |
| 1039 | }); |
| 1040 | } |
| 1041 | } |
| 1042 | |
| 1043 | // --------------------------------------------------------------------------- |
| 1044 | // lane-seg: segmentation. Header (spec 5.9.14) is parsed by `ec-av1-syntax`; |
| 1045 | // this block is the per-block `segment_id` reader (spec 5.11.7-5.11.9) plus |
| 1046 | // the two consumers a real stream exercises: the per-segment quantizer index |
| 1047 | // (spec 7.12.2 `get_qindex`) and the per-segment loop-filter level |
| 1048 | // (spec 7.14.4). Shapes follow libaom `decodemv.c:read_segment_id`/ |
| 1049 | // `read_intra_segment_id`/`read_inter_segment_id` and |
| 1050 | // `pred_common.h:av1_get_spatial_seg_pred` -- note libaom returns the SPATIAL |
| 1051 | // PREDICTION (not 0) from `read_segment_id` when the block is `skip`, which |
| 1052 | // is what the oracle every gate here compares against actually does. |
| 1053 | thread_local! { |
| 1054 | /// `CurrentSegmentIds`: this frame's own map, `mi_rows * mi_cols` entries |
| 1055 | /// with stride `mi_cols` (NOT the padded `Neighbours` grid stride). |
| 1056 | /// `PrevSegmentIds`: the primary reference frame's saved map, empty when |
| 1057 | /// this frame has no primary reference (spec `load_previous_segment_ids`). |
| 1058 | /// `(mi_rows, mi_cols)` for both maps above. |
| 1059 | /// `AboveSegPredContext` (per mi column) / `LeftSegPredContext` (per mi |
| 1060 | /// row) -- spec 5.11.9's own `seg_id_predicted` neighbour state, which |
| 1061 | /// libaom keeps on the neighbouring `mbmi->seg_id_predicted` instead. |
| 1062 | /// The block currently being decoded -- read back by [`block_q_idx`]. |
| 1063 | /// The tile's own top-left mi position, for `AvailU`/`AvailL`. |
| 1064 | /// How many real `segment_id` symbols were read, and the set of segment |
| 1065 | /// ids any block ended up with -- the gate's proof the feature fired. |
| 1066 | static SEG_ID_HITS: std::cell::Cell<usize> = const { std::cell::Cell::new(0) }; |
| 1067 | static SEG_IDS_SEEN: std::cell::Cell<u8> = const { std::cell::Cell::new(0) }; |
| 1068 | /// lane-golomb2: the (lowest, highest) quantizer index any block has been |
| 1069 | /// dequantized with, i.e. `block_q_idx`'s own span. A gate that means to |
| 1070 | /// exercise `SEG_LVL_ALT_Q` needs a stream where this span is WIDER than |
| 1071 | /// zero (class `gate-blind-to-feature`): segmentation being enabled says |
| 1072 | /// nothing about the segments carrying distinct quantizers. |
| 1073 | static BLOCK_Q_SPAN: std::cell::Cell<(i32, i32)> = const { std::cell::Cell::new((i32::MAX, i32::MIN)) }; |
| 1074 | /// How many `seg_id_predicted` symbols were read (temporal update). |
| 1075 | static SEG_PRED_HITS: std::cell::Cell<usize> = const { std::cell::Cell::new(0) }; |
| 1076 | } |
| 1077 | |
| 1078 | /// Installs this frame's segmentation parameters and allocates its map, with |
| 1079 | /// `prev` the primary reference frame's saved map (spec |
| 1080 | /// `load_previous_segment_ids`; `None` for `PRIMARY_REF_NONE` or an empty |
| 1081 | /// slot). Called once per frame, whether or not segmentation is enabled. |
| 1082 | pub(crate) fn set_segmentation( |
| 1083 | seg: SegmentationParams, |
| 1084 | mi_rows: usize, |
| 1085 | mi_cols: usize, |
| 1086 | prev: Option<&[u8]>, fctx: &crate::decode::FrameCtx, |
| 1087 | ) { |
| 1088 | if crate::envflags::env_flag!("EC_HDR") { |
| 1089 | eprintln!( |
| 1090 | "EC_SEGHDR enabled={} update_map={} temporal={} preskip={} last_active={} data={:?}", |
| 1091 | seg.enabled, seg.update_map, seg.temporal_update, seg.seg_id_pre_skip, seg.last_active_seg_id, seg.feature_data |
| 1092 | ); |
| 1093 | } |
| 1094 | fctx.seg.with(|s| *s.borrow_mut() = seg); |
| 1095 | fctx.seg_mi_dims.with(|c| c.set((mi_rows, mi_cols))); |
| 1096 | fctx.seg_ids.with(|m| *m.borrow_mut() = vec![0u8; mi_rows * mi_cols]); |
| 1097 | fctx.prev_seg_ids.with(|m| { |
| 1098 | let mut m = m.borrow_mut(); |
| 1099 | m.clear(); |
| 1100 | if let Some(p) = prev { |
| 1101 | if p.len() == mi_rows * mi_cols { |
| 1102 | m.extend_from_slice(p); |
| 1103 | } |
| 1104 | } |
| 1105 | }); |
| 1106 | fctx.above_seg_pred.with(|m| *m.borrow_mut() = vec![0u8; mi_cols]); |
| 1107 | fctx.left_seg_pred.with(|m| *m.borrow_mut() = vec![0u8; mi_rows]); |
| 1108 | fctx.cur_segment_id.with(|c| c.set(0)); |
| 1109 | } |
| 1110 | |
| 1111 | /// This frame's decoded segment map, to be stored into whichever reference |
| 1112 | /// slots `refresh_frame_flags` names (spec 7.20's `SavedSegmentIds`). |
| 1113 | pub(crate) fn take_segment_ids(fctx: &crate::decode::FrameCtx) -> Vec<u8> { |
| 1114 | fctx.seg_ids.with(|m| m.borrow().clone()) |
| 1115 | } |
| 1116 | |
| 1117 | /// How many `segment_id` symbols have been read (never reset per frame). |
| 1118 | #[allow(dead_code)] // read only from the `#[cfg(test)]` gates |
| 1119 | pub(crate) fn segment_id_hits() -> usize { |
| 1120 | SEG_ID_HITS.with(|c| c.get()) |
| 1121 | } |
| 1122 | |
| 1123 | /// How many distinct segment ids any decoded block has carried. |
| 1124 | #[allow(dead_code)] // read only from the `#[cfg(test)]` gates |
| 1125 | pub(crate) fn segment_ids_seen() -> usize { |
| 1126 | SEG_IDS_SEEN.with(|c| c.get().count_ones() as usize) |
| 1127 | } |
| 1128 | |
| 1129 | /// The lowest and highest quantizer index any decoded block was dequantized |
| 1130 | /// with (`(i32::MAX, i32::MIN)` when nothing decoded). |
| 1131 | #[allow(dead_code)] // read only from the `#[cfg(test)]` gates |
| 1132 | pub(crate) fn block_q_span() -> (i32, i32) { |
| 1133 | BLOCK_Q_SPAN.with(|c| c.get()) |
| 1134 | } |
| 1135 | |
| 1136 | /// How many `seg_id_predicted` symbols have been read (temporal update). |
| 1137 | #[allow(dead_code)] // read only from the `#[cfg(test)]` gates |
| 1138 | pub(crate) fn segment_pred_hits() -> usize { |
| 1139 | SEG_PRED_HITS.with(|c| c.get()) |
| 1140 | } |
| 1141 | |
| 1142 | /// Resets the segmentation hit counters (gate setup). |
| 1143 | #[allow(dead_code)] // read only from the `#[cfg(test)]` gates |
| 1144 | pub(crate) fn reset_segment_hits() { |
| 1145 | SEG_ID_HITS.with(|c| c.set(0)); |
| 1146 | SEG_IDS_SEEN.with(|c| c.set(0)); |
| 1147 | SEG_PRED_HITS.with(|c| c.set(0)); |
| 1148 | BLOCK_Q_SPAN.with(|c| c.set((i32::MAX, i32::MIN))); |
| 1149 | } |
| 1150 | |
| 1151 | /// `seg_feature_active_idx(segment_id, feature)` for the frame currently |
| 1152 | /// being decoded, with its `FeatureData` value. |
| 1153 | fn seg_feature(segment_id: usize, feature: usize, fctx: &crate::decode::FrameCtx) -> Option<i32> { |
| 1154 | fctx.seg.with(|s| { |
| 1155 | let s = s.borrow(); |
| 1156 | s.feature_active(segment_id, feature) |
| 1157 | .then(|| i32::from(s.feature_data[segment_id][feature])) |
| 1158 | }) |
| 1159 | } |
| 1160 | |
| 1161 | /// `get_qindex(ignoreDeltaQ = 0, segment_id)` (spec 7.12.2, libaom |
| 1162 | /// `av1_get_qindex`) for the block currently being decoded: the running |
| 1163 | /// `CurrentQIndex` shifted by this block's segment's `SEG_LVL_ALT_Q` data. |
| 1164 | /// Every dequant call site reads the block's quantizer through here. |
| 1165 | fn block_q_idx(fctx: &crate::decode::FrameCtx) -> i32 { |
| 1166 | let base = fctx.current_q_idx.with(|c| c.get()); |
| 1167 | let seg_id = fctx.cur_segment_id.with(|c| c.get()) as usize; |
| 1168 | let q = match seg_feature(seg_id, SEG_LVL_ALT_Q, fctx) { |
| 1169 | Some(data) => (base + data).clamp(0, 255), |
| 1170 | None => base, |
| 1171 | }; |
| 1172 | // lane-d792: which SEGMENT a block's quantizer came out of, beside the |
| 1173 | // `EC_DQCOEFF` grid rung -- a dequant that is too low with the levels |
| 1174 | // already proven equal is a segment-id defect, not a quantizer one. |
| 1175 | if crate::envflags::env_flag!("EC_DQCOEFF") { |
| 1176 | eprintln!("OUR_Q base={base} seg={seg_id} q={q}"); |
| 1177 | } |
| 1178 | hit_do!(BLOCK_Q_SPAN.with(|c| { |
| 1179 | let (lo, hi) = c.get(); |
| 1180 | c.set((lo.min(q), hi.max(q))); |
| 1181 | });); |
| 1182 | q |
| 1183 | } |
| 1184 | |
| 1185 | /// The segment id recorded for one mi cell of the current frame's map. |
| 1186 | fn segment_id_at(mi_r: usize, mi_c: usize, fctx: &crate::decode::FrameCtx) -> u8 { |
| 1187 | let (mi_rows, mi_cols) = fctx.seg_mi_dims.with(|c| c.get()); |
| 1188 | if mi_r >= mi_rows || mi_c >= mi_cols { |
| 1189 | return 0; |
| 1190 | } |
| 1191 | fctx.seg_ids.with(|m| m.borrow()[mi_r * mi_cols + mi_c]) |
| 1192 | } |
| 1193 | |
| 1194 | /// `set_segment_id`: stamps `id` over every mi cell this block covers. |
| 1195 | fn write_segment_id(mi_r: usize, mi_c: usize, w_mi: usize, h_mi: usize, id: u8, fctx: &crate::decode::FrameCtx) { |
| 1196 | let (mi_rows, mi_cols) = fctx.seg_mi_dims.with(|c| c.get()); |
| 1197 | let (x_mis, y_mis) = (w_mi.min(mi_cols.saturating_sub(mi_c)), h_mi.min(mi_rows.saturating_sub(mi_r))); |
| 1198 | fctx.seg_ids.with(|m| { |
| 1199 | let mut m = m.borrow_mut(); |
| 1200 | for y in 0..y_mis { |
| 1201 | for x in 0..x_mis { |
| 1202 | m[(mi_r + y) * mi_cols + mi_c + x] = id; |
| 1203 | } |
| 1204 | } |
| 1205 | }); |
| 1206 | fctx.cur_segment_id.with(|c| c.set(id)); |
| 1207 | SEG_IDS_SEEN.with(|c| c.set(c.get() | 1 << id)); |
| 1208 | } |
| 1209 | |
| 1210 | /// `copy_segment_id` (libaom `decodemv.c:342`): the previous frame's map is |
| 1211 | /// inherited VERBATIM over this block's own mi footprint -- per-cell copies, |
| 1212 | /// not the minified id [`predicted_segment_id`] hands the block. Without a |
| 1213 | /// previous map the footprint is zeroed, exactly as the C NULL branch does. |
| 1214 | fn copy_segment_ids(mi_r: usize, mi_c: usize, w_mi: usize, h_mi: usize, fctx: &crate::decode::FrameCtx) { |
| 1215 | let (mi_rows, mi_cols) = fctx.seg_mi_dims.with(|c| c.get()); |
| 1216 | let (x_mis, y_mis) = (w_mi.min(mi_cols.saturating_sub(mi_c)), h_mi.min(mi_rows.saturating_sub(mi_r))); |
| 1217 | fctx.seg_ids.with(|m| { |
| 1218 | let mut m = m.borrow_mut(); |
| 1219 | fctx.prev_seg_ids.with(|p| { |
| 1220 | let p = p.borrow(); |
| 1221 | for y in 0..y_mis { |
| 1222 | for x in 0..x_mis { |
| 1223 | let idx = (mi_r + y) * mi_cols + mi_c + x; |
| 1224 | m[idx] = p.get(idx).copied().unwrap_or(0); |
| 1225 | } |
| 1226 | } |
| 1227 | }); |
| 1228 | }); |
| 1229 | } |
| 1230 | |
| 1231 | /// `get_predicted_segment_id` (libaom `dec_get_segment_id` over |
| 1232 | /// `last_frame_seg_map`): the minimum previous-frame segment id over the |
| 1233 | /// block's own mi footprint. `0` when there is no previous map. |
| 1234 | fn predicted_segment_id(mi_r: usize, mi_c: usize, w_mi: usize, h_mi: usize, fctx: &crate::decode::FrameCtx) -> u8 { |
| 1235 | let (mi_rows, mi_cols) = fctx.seg_mi_dims.with(|c| c.get()); |
| 1236 | fctx.prev_seg_ids.with(|m| { |
| 1237 | let m = m.borrow(); |
| 1238 | if m.is_empty() { |
| 1239 | return 0; |
| 1240 | } |
| 1241 | let (x_mis, y_mis) = (w_mi.min(mi_cols.saturating_sub(mi_c)), h_mi.min(mi_rows.saturating_sub(mi_r))); |
| 1242 | let mut id = u8::MAX; |
| 1243 | for y in 0..y_mis { |
| 1244 | for x in 0..x_mis { |
| 1245 | id = id.min(m[(mi_r + y) * mi_cols + mi_c + x]); |
| 1246 | } |
| 1247 | } |
| 1248 | if id == u8::MAX { 0 } else { id } |
| 1249 | }) |
| 1250 | } |
| 1251 | |
| 1252 | /// `av1_neg_deinterleave` (libaom `decodemv.c:258`). |
| 1253 | fn neg_deinterleave(diff: i32, reference: i32, max: i32) -> i32 { |
| 1254 | if reference == 0 { |
| 1255 | return diff; |
| 1256 | } |
| 1257 | if reference >= max - 1 { |
| 1258 | return max - diff - 1; |
| 1259 | } |
| 1260 | let half = if 2 * reference < max { |
| 1261 | diff <= 2 * reference |
| 1262 | } else { |
| 1263 | diff <= 2 * (max - reference - 1) |
| 1264 | }; |
| 1265 | if half { |
| 1266 | return if diff & 1 != 0 { |
| 1267 | reference + ((diff + 1) >> 1) |
| 1268 | } else { |
| 1269 | reference - (diff >> 1) |
| 1270 | }; |
| 1271 | } |
| 1272 | if 2 * reference < max { diff } else { max - (diff + 1) } |
| 1273 | } |
| 1274 | |
| 1275 | /// `av1_get_spatial_seg_pred`: `(prediction, cdf_index)` from the current |
| 1276 | /// frame's already-decoded above/left/above-left neighbours. |
| 1277 | fn spatial_seg_pred(mi_r: usize, mi_c: usize, fctx: &crate::decode::FrameCtx) -> (i32, usize) { |
| 1278 | let (row0, col0) = fctx.seg_tile_origin.with(|c| c.get()); |
| 1279 | let (avail_u, avail_l) = (mi_r > row0, mi_c > col0); |
| 1280 | let prev_ul = (avail_u && avail_l).then(|| i32::from(segment_id_at(mi_r - 1, mi_c - 1, fctx))); |
| 1281 | let prev_u = avail_u.then(|| i32::from(segment_id_at(mi_r - 1, mi_c, fctx))); |
| 1282 | let prev_l = avail_l.then(|| i32::from(segment_id_at(mi_r, mi_c - 1, fctx))); |
| 1283 | let ctx = match prev_ul { |
| 1284 | None => 0, |
| 1285 | Some(ul) if Some(ul) == prev_u && Some(ul) == prev_l => 2, |
| 1286 | Some(ul) if Some(ul) == prev_u || Some(ul) == prev_l || prev_u == prev_l => 1, |
| 1287 | Some(_) => 0, |
| 1288 | }; |
| 1289 | let pred = match (prev_u, prev_l) { |
| 1290 | (None, l) => l.unwrap_or(0), |
| 1291 | (Some(u), None) => u, |
| 1292 | (Some(u), Some(l)) => { |
| 1293 | if prev_ul == Some(u) { |
| 1294 | u |
| 1295 | } else { |
| 1296 | l |
| 1297 | } |
| 1298 | } |
| 1299 | }; |
| 1300 | (pred, ctx) |
| 1301 | } |
| 1302 | |
| 1303 | /// `read_segment_id` (libaom `decodemv.c:280`): the spatially predicted id |
| 1304 | /// when `skip`, otherwise one `segment_id` symbol de-interleaved against |
| 1305 | /// that prediction. Does NOT write the map -- callers do. |
| 1306 | fn read_segment_id(dec: &mut SymbolDecoder, cdfs: &mut Cdfs, mi_r: usize, mi_c: usize, skip: bool, fctx: &crate::decode::FrameCtx) -> u8 { |
| 1307 | let (pred, ctx) = spatial_seg_pred(mi_r, mi_c, fctx); |
| 1308 | if skip { |
| 1309 | return pred as u8; |
| 1310 | } |
| 1311 | let coded = dec.symbol(&mut cdfs.segment_id[ctx]) as i32; |
| 1312 | hit!(SEG_ID_HITS); |
| 1313 | let last_active = fctx.seg.with(|s| i32::from(s.borrow().last_active_seg_id)); |
| 1314 | let id = neg_deinterleave(coded, pred, last_active + 1).clamp(0, last_active); |
| 1315 | // Mirrors the oracle's own `EC_SEG mi_row=.. cdf=.. pred=.. coded=.. id=..` |
| 1316 | // (libaom `decodemv.c:read_segment_id`), for a cross-decoder segment diff. |
| 1317 | if crate::envflags::env_flag!("EC_TRACE_SEG") { |
| 1318 | eprintln!("EC_SEG mi_row={mi_r} mi_col={mi_c} cdf={ctx} pred={pred} coded={coded} id={id} rng={}", dec.debug_state().0); |
| 1319 | } |
| 1320 | id as u8 |
| 1321 | } |
| 1322 | |
| 1323 | /// `read_intra_segment_id` (spec 5.11.7): called twice per intra-frame block |
| 1324 | /// -- once before `skip` when `SegIdPreSkip`, once after it otherwise (the |
| 1325 | /// caller decides which by passing `pre_skip`). |
| 1326 | fn intra_segment_id( |
| 1327 | dec: &mut SymbolDecoder, |
| 1328 | cdfs: &mut Cdfs, |
| 1329 | mi_r: usize, |
| 1330 | mi_c: usize, |
| 1331 | w_mi: usize, |
| 1332 | h_mi: usize, |
| 1333 | skip: bool, fctx: &crate::decode::FrameCtx, |
| 1334 | ) { |
| 1335 | if !fctx.seg.with(|s| s.borrow().enabled) { |
| 1336 | return; |
| 1337 | } |
| 1338 | let id = read_segment_id(dec, cdfs, mi_r, mi_c, skip, fctx); |
| 1339 | write_segment_id(mi_r, mi_c, w_mi, h_mi, id, fctx); |
| 1340 | } |
| 1341 | |
| 1342 | /// Whether this frame's intra blocks read their `segment_id` before `skip` |
| 1343 | /// (`SegIdPreSkip`); `false` also when segmentation is off. |
| 1344 | fn seg_id_pre_skip(fctx: &crate::decode::FrameCtx) -> bool { |
| 1345 | fctx.seg.with(|s| { |
| 1346 | let s = s.borrow(); |
| 1347 | s.enabled && s.seg_id_pre_skip |
| 1348 | }) |
| 1349 | } |
| 1350 | |
| 1351 | /// `read_inter_segment_id(preskip)` (spec 5.11.9, libaom `decodemv.c:352`). |
| 1352 | /// Called at both spec positions of an inter frame's block: `pre_skip = true` |
| 1353 | /// before `skip_mode`/`skip`, and `pre_skip = false` after them. |
| 1354 | fn inter_segment_id( |
| 1355 | dec: &mut SymbolDecoder, |
| 1356 | cdfs: &mut Cdfs, |
| 1357 | mi_r: usize, |
| 1358 | mi_c: usize, |
| 1359 | w_mi: usize, |
| 1360 | h_mi: usize, |
| 1361 | skip: bool, |
| 1362 | pre_skip: bool, fctx: &crate::decode::FrameCtx, |
| 1363 | ) { |
| 1364 | let (enabled, update_map, temporal_update, pre_skip_frame) = fctx.seg.with(|s| { |
| 1365 | let s = s.borrow(); |
| 1366 | (s.enabled, s.update_map, s.temporal_update, s.seg_id_pre_skip) |
| 1367 | }); |
| 1368 | if !enabled { |
| 1369 | return; |
| 1370 | } |
| 1371 | if !update_map { |
| 1372 | // The map is inherited from the previous frame, and libaom's |
| 1373 | // `copy_segment_id` (decodemv.c:342) does that VERBATIM -- a memcpy of |
| 1374 | // the previous map over the block's own footprint, mixed ids intact -- |
| 1375 | // while the id the BLOCK itself carries is the min over that footprint |
| 1376 | // (`get_predicted_segment_id`). Stamping the min here flattened mixed |
| 1377 | // footprints into the saved map, and every later frame whose |
| 1378 | // `update_map == 0` copy or temporal prediction read that position |
| 1379 | // dequantized with the wrong segment's `SEG_LVL_ALT_Q` (class |
| 1380 | // `simplified-inheritance`). |
| 1381 | copy_segment_ids(mi_r, mi_c, w_mi, h_mi, fctx); |
| 1382 | let id = predicted_segment_id(mi_r, mi_c, w_mi, h_mi, fctx); |
| 1383 | fctx.cur_segment_id.with(|c| c.set(id)); |
| 1384 | SEG_IDS_SEEN.with(|c| c.set(c.get() | 1 << id)); |
| 1385 | return; |
| 1386 | } |
| 1387 | if pre_skip && !pre_skip_frame { |
| 1388 | return; |
| 1389 | } |
| 1390 | if !pre_skip && skip { |
| 1391 | // libaom: a skipped block never codes `seg_id_predicted`, and its |
| 1392 | // segment id is the spatial prediction. |
| 1393 | stamp_seg_pred(mi_r, mi_c, w_mi, h_mi, 0, fctx); |
| 1394 | let id = read_segment_id(dec, cdfs, mi_r, mi_c, true, fctx); |
| 1395 | write_segment_id(mi_r, mi_c, w_mi, h_mi, id, fctx); |
| 1396 | return; |
| 1397 | } |
| 1398 | let id = if temporal_update { |
| 1399 | let ctx = seg_pred_ctx(mi_r, mi_c, fctx); |
| 1400 | let predicted = dec.symbol(&mut cdfs.segment_pred[ctx]) != 0; |
| 1401 | hit!(SEG_PRED_HITS); |
| 1402 | stamp_seg_pred(mi_r, mi_c, w_mi, h_mi, u8::from(predicted), fctx); |
| 1403 | if predicted { |
| 1404 | predicted_segment_id(mi_r, mi_c, w_mi, h_mi, fctx) |
| 1405 | } else { |
| 1406 | read_segment_id(dec, cdfs, mi_r, mi_c, false, fctx) |
| 1407 | } |
| 1408 | } else { |
| 1409 | read_segment_id(dec, cdfs, mi_r, mi_c, false, fctx) |
| 1410 | }; |
| 1411 | write_segment_id(mi_r, mi_c, w_mi, h_mi, id, fctx); |
| 1412 | } |
| 1413 | |
| 1414 | /// `av1_get_pred_context_seg_id`: above + left `seg_id_predicted` flags. |
| 1415 | fn seg_pred_ctx(mi_r: usize, mi_c: usize, fctx: &crate::decode::FrameCtx) -> usize { |
| 1416 | // libaom reads the flag off `above_mbmi`/`left_mbmi`, which are NULL (and |
| 1417 | // so contribute 0) outside the tile -- the availability check here is |
| 1418 | // what keeps a previous tile's stamps out of this tile's context. |
| 1419 | let (row0, col0) = fctx.seg_tile_origin.with(|c| c.get()); |
| 1420 | let above = if mi_r > row0 { |
| 1421 | fctx.above_seg_pred.with(|m| m.borrow().get(mi_c).copied().unwrap_or(0)) |
| 1422 | } else { |
| 1423 | 0 |
| 1424 | }; |
| 1425 | let left = if mi_c > col0 { |
| 1426 | fctx.left_seg_pred.with(|m| m.borrow().get(mi_r).copied().unwrap_or(0)) |
| 1427 | } else { |
| 1428 | 0 |
| 1429 | }; |
| 1430 | usize::from(above + left) |
| 1431 | } |
| 1432 | |
| 1433 | /// Spec 5.11.9's `AboveSegPredContext`/`LeftSegPredContext` update. |
| 1434 | fn stamp_seg_pred(mi_r: usize, mi_c: usize, w_mi: usize, h_mi: usize, value: u8, fctx: &crate::decode::FrameCtx) { |
| 1435 | fctx.above_seg_pred.with(|m| { |
| 1436 | let mut m = m.borrow_mut(); |
| 1437 | for i in 0..w_mi { |
| 1438 | if let Some(cell) = m.get_mut(mi_c + i) { |
| 1439 | *cell = value; |
| 1440 | } |
| 1441 | } |
| 1442 | }); |
| 1443 | fctx.left_seg_pred.with(|m| { |
| 1444 | let mut m = m.borrow_mut(); |
| 1445 | for i in 0..h_mi { |
| 1446 | if let Some(cell) = m.get_mut(mi_r + i) { |
| 1447 | *cell = value; |
| 1448 | } |
| 1449 | } |
| 1450 | }); |
| 1451 | } |
| 1452 | |
| 1453 | // How many key-frame luma blocks resolved `smooth_neighbor` (spec |
| 1454 | // `get_intra_edge_filter_type`) to `true` -- lane-chroma r2's own before/after |
| 1455 | // counter, proving a stream actually put a directional block next to a |
| 1456 | // smooth-predicted neighbour rather than every block reading the (previously |
| 1457 | // hardcoded `false`) edge-filter strength bucket unchanged. |
| 1458 | thread_local! { |
| 1459 | static SMOOTH_LUMA_HITS: std::cell::Cell<usize> = |
| 1460 | const { std::cell::Cell::new(0) }; |
| 1461 | } |
| 1462 | |
| 1463 | /// Current value of [`SMOOTH_LUMA_HITS`]. |
| 1464 | #[allow(dead_code)] // read only from the `#[cfg(test)]` gates |
| 1465 | pub(crate) fn smooth_luma_hits() -> usize { |
| 1466 | SMOOTH_LUMA_HITS.with(|c| c.get()) |
| 1467 | } |
| 1468 | |
| 1469 | thread_local! { |
| 1470 | /// lane-band46 r1: how many split-transform units answered their intra |
| 1471 | /// reach DIFFERENTLY under libaom's per-unit rules ([`Reach::of_tu`], the |
| 1472 | /// palette2 r12 fix) than the standalone-block lookup this decoder used to |
| 1473 | /// apply to them (class reach-is-per-transform-unit). Every hit is a |
| 1474 | /// transform unit whose above-right / below-left reference samples would |
| 1475 | /// be wrong without that fix. |
| 1476 | static SPLIT_TU_REACH_FIX_HITS: std::cell::Cell<usize> = |
| 1477 | const { std::cell::Cell::new(0) }; |
| 1478 | } |
| 1479 | |
| 1480 | /// Current value of [`SPLIT_TU_REACH_FIX_HITS`]. |
| 1481 | #[allow(dead_code)] // read only from the `#[cfg(test)]` gates |
| 1482 | pub(crate) fn split_tu_reach_fix_hits() -> usize { |
| 1483 | SPLIT_TU_REACH_FIX_HITS.with(|c| c.get()) |
| 1484 | } |
| 1485 | |
| 1486 | /// [`Reach::of_tu`] for one transform unit, counting how often that per-unit |
| 1487 | /// answer differs from the standalone-block answer the call sites used before |
| 1488 | /// lane-palette2 r12 -- the counter a gate asserts on to prove a stream really |
| 1489 | /// exercised the fix. `col_off`/`row_off` are in SAMPLES from the parent |
| 1490 | /// block's top-left `(px, py)`. |
| 1491 | #[allow(clippy::too_many_arguments)] |
| 1492 | fn tu_reach( |
| 1493 | bw: usize, |
| 1494 | bh: usize, |
| 1495 | col_off: usize, |
| 1496 | row_off: usize, |
| 1497 | tx: usize, |
| 1498 | block: Reach, |
| 1499 | px: usize, |
| 1500 | py: usize, |
| 1501 | width: usize, |
| 1502 | height: usize, fctx: &crate::decode::FrameCtx, |
| 1503 | ) -> Reach { |
| 1504 | let fixed = Reach::of_tu(bw, bh, col_off, row_off, tx, tx, block); |
| 1505 | let standalone = Reach::of(tx, px + col_off, py + row_off, width, height, fctx); |
| 1506 | if fixed != standalone { |
| 1507 | hit!(SPLIT_TU_REACH_FIX_HITS); |
| 1508 | } |
| 1509 | fixed |
| 1510 | } |
| 1511 | |
| 1512 | /// [`tu_reach`] for a unit whose LUMA footprint is a `tx_w` x `tx_h` rect |
| 1513 | /// (lane-av1-422b: a 4:2:2 chroma unit is square in chroma pixels, but its |
| 1514 | /// luma anchor is `(chroma_tx << ss_x, chroma_tx << ss_y)` -- at ss (1,0) |
| 1515 | /// twice as wide as it is tall). Same predicate as [`tu_reach`]; the |
| 1516 | /// square mismatch counter only means anything when the footprint actually |
| 1517 | /// is square. |
| 1518 | #[allow(clippy::too_many_arguments)] |
| 1519 | fn tu_reach_rect( |
| 1520 | bw: usize, |
| 1521 | bh: usize, |
| 1522 | col_off: usize, |
| 1523 | row_off: usize, |
| 1524 | tx_w: usize, |
| 1525 | tx_h: usize, |
| 1526 | block: Reach, |
| 1527 | px: usize, |
| 1528 | py: usize, |
| 1529 | width: usize, |
| 1530 | height: usize, fctx: &crate::decode::FrameCtx, |
| 1531 | ) -> Reach { |
| 1532 | let fixed = Reach::of_tu(bw, bh, col_off, row_off, tx_w, tx_h, block); |
| 1533 | if tx_w == tx_h { |
| 1534 | let standalone = Reach::of(tx_w, px + col_off, py + row_off, width, height, fctx); |
| 1535 | if fixed != standalone { |
| 1536 | hit!(SPLIT_TU_REACH_FIX_HITS); |
| 1537 | } |
| 1538 | } |
| 1539 | fixed |
| 1540 | } |
| 1541 | |
| 1542 | // How many `uv_mode` reads resolved to `SMOOTH_PRED..=PAETH_PRED` (9..=12), |
| 1543 | // across every call on the current thread -- lane-chroma r3's own before/after |
| 1544 | // counter, proving a stream actually exercised chroma's smooth/paeth |
| 1545 | // predictor (the previously-refused round-2 gap) rather than every block |
| 1546 | // landing on DC/CFL/directional. |
| 1547 | thread_local! { |
| 1548 | static SMOOTH_UV_HITS: std::cell::Cell<usize> = |
| 1549 | const { std::cell::Cell::new(0) }; |
| 1550 | } |
| 1551 | |
| 1552 | /// Current value of [`SMOOTH_UV_HITS`]. |
| 1553 | #[allow(dead_code)] // read only from the `#[cfg(test)]` gates |
| 1554 | pub(crate) fn smooth_uv_hits() -> usize { |
| 1555 | SMOOTH_UV_HITS.with(|c| c.get()) |
| 1556 | } |
| 1557 | |
| 1558 | // How many tiles [`decode_key_frame_tile_with_cdfs`] has actually decoded a |
| 1559 | // full superblock walk over, across every call on the current thread -- |
| 1560 | // lane-tiles r2's own gate-blind-to-feature guard: a gate over a |
| 1561 | // multi-tile stream must prove more than one tile actually fired, not just |
| 1562 | // that the stream carried tile_info.cols > 1. |
| 1563 | thread_local! { |
| 1564 | static TILE_HITS: std::cell::Cell<usize> = |
| 1565 | const { std::cell::Cell::new(0) }; |
| 1566 | } |
| 1567 | /// Current value of [`TILE_HITS`]. |
| 1568 | #[allow(dead_code)] // read only from the `#[cfg(test)]` gates |
| 1569 | pub(crate) fn tile_hits() -> usize { |
| 1570 | TILE_HITS.with(|c| c.get()) |
| 1571 | } |
| 1572 | |
| 1573 | // How many 4-pixel deblocking edge groups [`edge_params`] has actually |
| 1574 | // selected for filtering, across every call on the current thread -- the same |
| 1575 | // before/after counter pattern as [`FILTER_INTRA_HITS`], proving a stream |
| 1576 | // exercised `apply_deblock` rather than every edge landing on level 0. |
| 1577 | thread_local! { |
| 1578 | static DEBLOCK_HITS: std::cell::Cell<usize> = |
| 1579 | const { std::cell::Cell::new(0) }; |
| 1580 | } |
| 1581 | /// lane-comppin r4: decode-order picture counter for `EC_AV1_PREFILT_DUMP`, |
| 1582 | /// mirroring stream.rs's `pictures_decoded` so the two dumps index |
| 1583 | /// identically against aomdec's own `ec_frame_idx` in decodeframe.c. |
| 1584 | static PREFILT_PICTURE_IDX: std::sync::atomic::AtomicUsize = |
| 1585 | std::sync::atomic::AtomicUsize::new(0); |
| 1586 | |
| 1587 | /// Current value of [`DEBLOCK_HITS`]. |
| 1588 | #[allow(dead_code)] // read only from the `#[cfg(test)]` gates |
| 1589 | pub(crate) fn deblock_hits() -> usize { |
| 1590 | DEBLOCK_HITS.with(|c| c.get()) |
| 1591 | } |
| 1592 | |
| 1593 | // lane-defon r1: how many COMPOUND_REFERENCE blocks read a |
| 1594 | // `UNIDIR_COMP_REFERENCE` pair (both references on the same temporal side, |
| 1595 | // spec 5.11.25 `comp_reference_type == 0`) -- the gate's proof that a |
| 1596 | // stream built with `--enable-onesided-comp=1` really exercised the |
| 1597 | // `uni_comp_ref` alphabet rather than only bidirectional pairs. |
| 1598 | thread_local! { |
| 1599 | static UNI_COMP_HITS: std::cell::Cell<usize> = |
| 1600 | const { std::cell::Cell::new(0) }; |
| 1601 | } |
| 1602 | |
| 1603 | /// Current value of [`UNI_COMP_HITS`]. |
| 1604 | #[allow(dead_code)] // read only from the `#[cfg(test)]` gates |
| 1605 | pub(crate) fn uni_comp_hits() -> usize { |
| 1606 | UNI_COMP_HITS.with(|c| c.get()) |
| 1607 | } |
| 1608 | |
| 1609 | // lane-defon r1: how many blocks blended through a `COMPOUND_DIFFWTD` mask |
| 1610 | // (`mc::diffwtd_mask`) -- the narrower half of `MASKED_COMPOUND_HITS`, the |
| 1611 | // gate's proof that `--enable-diff-wtd-comp=1` reached the difference-weighted |
| 1612 | // blend and not only the wedge arm. |
| 1613 | thread_local! { |
| 1614 | static DIFFWTD_HITS: std::cell::Cell<usize> = |
| 1615 | const { std::cell::Cell::new(0) }; |
| 1616 | } |
| 1617 | |
| 1618 | /// Current value of [`DIFFWTD_HITS`]. |
| 1619 | #[allow(dead_code)] // read only from the `#[cfg(test)]` gates |
| 1620 | pub(crate) fn diffwtd_hits() -> usize { |
| 1621 | DIFFWTD_HITS.with(|c| c.get()) |
| 1622 | } |
| 1623 | |
| 1624 | // lane-mcomp64 r1: how many blocks took the INFERRED `COMPOUND_DIFFWTD` arm -- |
| 1625 | // a block size where `av1_is_wedge_used` is false (64x64 is the only such size |
| 1626 | // this decoder's square inter path reaches), so no `compound_type` symbol is |
| 1627 | // coded at all. Strictly narrower than `DIFFWTD_HITS`, which also counts the |
| 1628 | // coded-symbol DIFFWTD blocks at 8x8/16x16/32x32. |
| 1629 | thread_local! { |
| 1630 | static DIFFWTD_INFERRED_HITS: std::cell::Cell<usize> = |
| 1631 | const { std::cell::Cell::new(0) }; |
| 1632 | } |
| 1633 | |
| 1634 | /// Current value of [`DIFFWTD_INFERRED_HITS`]. |
| 1635 | #[allow(dead_code)] // read only from the `#[cfg(test)]` gates |
| 1636 | pub(crate) fn diffwtd_inferred_hits() -> usize { |
| 1637 | DIFFWTD_INFERRED_HITS.with(|c| c.get()) |
| 1638 | } |
| 1639 | |
| 1640 | // lane-defon r1: how many FORWARD KEYFRAMES were decoded -- a KEY frame coded |
| 1641 | // with `show_frame == 0`, output later by a `show_existing_frame` header |
| 1642 | // (aomenc `--enable-fwd-kf=1 --fwd-kf-dist=N`). Counted from |
| 1643 | // `stream::decode_stream`, which is where the frame header is consumed; the |
| 1644 | // gate's proof that the on-value really reached the bitstream rather than |
| 1645 | // being dropped by the rate control. |
| 1646 | thread_local! { |
| 1647 | static FWD_KF_HITS: std::cell::Cell<usize> = |
| 1648 | const { std::cell::Cell::new(0) }; |
| 1649 | } |
| 1650 | |
| 1651 | /// Current value of [`FWD_KF_HITS`]. |
| 1652 | #[allow(dead_code)] // read only from the `#[cfg(test)]` gates |
| 1653 | pub(crate) fn fwd_kf_hits() -> usize { |
| 1654 | FWD_KF_HITS.with(|c| c.get()) |
| 1655 | } |
| 1656 | |
| 1657 | /// Records one decoded forward keyframe (see [`FWD_KF_HITS`]). |
| 1658 | pub(crate) fn note_fwd_kf() { |
| 1659 | hit!(FWD_KF_HITS); |
| 1660 | } |
| 1661 | |
| 1662 | // How many [`read_tx_size`] reads resolved a `tx_depth` strictly less than |
| 1663 | // the block's own side, across every call on the current thread -- the same |
| 1664 | // before/after counter pattern as [`FILTER_INTRA_HITS`], proving a stream |
| 1665 | // actually exercised a *split* transform under `TxMode::Select` rather than |
| 1666 | // every block coincidentally resolving `depth=0` (indistinguishable from |
| 1667 | // `TxMode::Largest` pixel-wise). |
| 1668 | thread_local! { |
| 1669 | static TX_DEPTH_HITS: std::cell::Cell<usize> = |
| 1670 | const { std::cell::Cell::new(0) }; |
| 1671 | } |
| 1672 | |
| 1673 | // How many plain SQUARE intra blocks (lane-sqchroma r1) resolved a luma |
| 1674 | // transform smaller than their own side (`tx_depth != 0`) while coding a |
| 1675 | // chroma prediction other than `DC_PRED` -- the pair the square path's chroma |
| 1676 | // only exercises with aomenc's tx-size search left ON (its default), which |
| 1677 | // every older recipe in this file pins off. Counted at the block, so a stream |
| 1678 | // where RD happened to give every split-transform block a DC chroma mode |
| 1679 | // reads as zero rather than silently passing for free. |
| 1680 | thread_local! { |
| 1681 | static SQ_CHROMA_TX_HITS: std::cell::Cell<usize> = |
| 1682 | const { std::cell::Cell::new(0) }; |
| 1683 | } |
| 1684 | |
| 1685 | /// Current value of [`SQ_CHROMA_TX_HITS`]. |
| 1686 | #[allow(dead_code)] // read only from the `#[cfg(test)]` gates |
| 1687 | pub(crate) fn sq_chroma_tx_hits() -> usize { |
| 1688 | SQ_CHROMA_TX_HITS.with(|c| c.get()) |
| 1689 | } |
| 1690 | |
| 1691 | /// Current value of [`TX_DEPTH_HITS`]. |
| 1692 | #[allow(dead_code)] // read only from the `#[cfg(test)]` gates |
| 1693 | pub(crate) fn tx_depth_hits() -> usize { |
| 1694 | TX_DEPTH_HITS.with(|c| c.get()) |
| 1695 | } |
| 1696 | |
| 1697 | // lane-txselect: how many `txfm_split` symbols an inter frame's var-tx tree |
| 1698 | // read, and how many of those actually took the split, on the current thread |
| 1699 | // -- the same before/after counter pattern as [`TX_DEPTH_HITS`], proving a |
| 1700 | // stream exercised spec 5.11.17's `read_var_tx_size` rather than every block |
| 1701 | // coincidentally coding one whole-block transform. |
| 1702 | thread_local! { |
| 1703 | static TXFM_SPLIT_READS: std::cell::Cell<usize> = const { std::cell::Cell::new(0) }; |
| 1704 | static TXFM_SPLIT_HITS: std::cell::Cell<usize> = const { std::cell::Cell::new(0) }; |
| 1705 | // lane-inter4 r2: rectangular INTER residual coding -- how many rect |
| 1706 | // strips coded one whole-block rect transform unit, and how many split |
| 1707 | // their rect max transform into square sub-transforms. |
| 1708 | static RECT_INTER_TU_HITS: std::cell::Cell<usize> = const { std::cell::Cell::new(0) }; |
| 1709 | static RECT_INTER_TXSPLIT_HITS: std::cell::Cell<usize> = const { std::cell::Cell::new(0) }; |
| 1710 | // lane-r14 r2: luma transform units of an inter strip with a 64-px axis |
| 1711 | // (64x32/32x64/64x16/16x64), whose coefficients are coded as the |
| 1712 | // truncated low-32 corner. |
| 1713 | static RECT64_INTER_TU_HITS: std::cell::Cell<usize> = const { std::cell::Cell::new(0) }; |
| 1714 | // lane-rectres r1: whole-block luma transform units of a 32x32-level 1:4 |
| 1715 | // inter strip, `[32x8, 8x32]`. |
| 1716 | static RECT32X8_INTER_TU_HITS: std::cell::Cell<[usize; 2]> = const { std::cell::Cell::new([0; 2]) }; |
| 1717 | // lane-r14 r3: rectangular var-tx LEAVES, [32x16, 16x32]. |
| 1718 | static VARTX_RECT_LEAF_HITS: std::cell::Cell<[usize; 2]> = const { std::cell::Cell::new([0; 2]) }; |
| 1719 | // lane-inter16ab r4: rectangular var-tx leaves with a 4-px axis, [8x4, 4x8] |
| 1720 | // -- the `sub_tx_size_map[TX_16X4]` split leaves of a 16x16-level 1:4 strip. |
| 1721 | static VARTX_RECT_LEAF4_HITS: std::cell::Cell<[usize; 2]> = const { std::cell::Cell::new([0; 2]) }; |
| 1722 | // lane-inter4 r3: 16x16-level PARTITION_HORZ / PARTITION_VERT on an inter |
| 1723 | // frame -- 16x8 and 8x16 inter leaves. |
| 1724 | static INTER_LEAF16_HORZ_HITS: std::cell::Cell<usize> = const { std::cell::Cell::new(0) }; |
| 1725 | static INTER_LEAF16_VERT_HITS: std::cell::Cell<usize> = const { std::cell::Cell::new(0) }; |
| 1726 | // This frame's own `tx_mode == TxMode::Select` and `reduced_tx_set` bits, |
| 1727 | // set once per inter tile by [`decode_inter_frame_tile_with_cdfs`] -- |
| 1728 | // `decode_inter_block` already carries forty parameters and both are |
| 1729 | // frame-constant. |
| 1730 | // lane-fiinter r1: this sequence header's own `enable_filter_intra` bit, |
| 1731 | // frame-constant like the two above and stashed the same way -- an intra |
| 1732 | // block inside an inter frame reads `use_filter_intra` under it. |
| 1733 | } |
| 1734 | |
| 1735 | /// Current value of [`TXFM_SPLIT_READS`]. |
| 1736 | #[allow(dead_code)] // read only from the `#[cfg(test)]` gates |
| 1737 | pub(crate) fn txfm_split_reads() -> usize { |
| 1738 | TXFM_SPLIT_READS.with(|c| c.get()) |
| 1739 | } |
| 1740 | |
| 1741 | /// Current value of [`TXFM_SPLIT_HITS`]. |
| 1742 | #[allow(dead_code)] // read only from the `#[cfg(test)]` gates |
| 1743 | pub(crate) fn txfm_split_hits() -> usize { |
| 1744 | TXFM_SPLIT_HITS.with(|c| c.get()) |
| 1745 | } |
| 1746 | |
| 1747 | /// Current value of [`INTER_LEAF16_HORZ_HITS`] (lane-inter4 r3). |
| 1748 | #[allow(dead_code)] // read only from the `#[cfg(test)]` gates |
| 1749 | pub(crate) fn inter_leaf16_horz_hits() -> usize { |
| 1750 | INTER_LEAF16_HORZ_HITS.with(|c| c.get()) |
| 1751 | } |
| 1752 | |
| 1753 | /// Current value of [`INTER_LEAF16_VERT_HITS`] (lane-inter4 r3). |
| 1754 | #[allow(dead_code)] // read only from the `#[cfg(test)]` gates |
| 1755 | pub(crate) fn inter_leaf16_vert_hits() -> usize { |
| 1756 | INTER_LEAF16_VERT_HITS.with(|c| c.get()) |
| 1757 | } |
| 1758 | |
| 1759 | /// Current value of [`RECT_INTER_TU_HITS`] (lane-inter4 r2). |
| 1760 | pub(crate) fn rect_inter_tu_hits() -> usize { |
| 1761 | RECT_INTER_TU_HITS.with(|c| c.get()) |
| 1762 | } |
| 1763 | |
| 1764 | /// Current value of [`RECT_INTER_TXSPLIT_HITS`] (lane-inter4 r2). |
| 1765 | pub(crate) fn rect_inter_txsplit_hits() -> usize { |
| 1766 | RECT_INTER_TXSPLIT_HITS.with(|c| c.get()) |
| 1767 | } |
| 1768 | |
| 1769 | /// Current value of [`RECT64_INTER_TU_HITS`] (lane-r14 r2). |
| 1770 | pub(crate) fn rect64_inter_tu_hits() -> usize { |
| 1771 | RECT64_INTER_TU_HITS.with(|c| c.get()) |
| 1772 | } |
| 1773 | |
| 1774 | /// Current value of [`RECT32X8_INTER_TU_HITS`] (lane-rectres r1): `[32x8, 8x32]` |
| 1775 | /// whole-block luma units of a 32x32-level 1:4 inter strip. |
| 1776 | pub(crate) fn rect32x8_inter_tu_hits() -> [usize; 2] { |
| 1777 | RECT32X8_INTER_TU_HITS.with(|c| c.get()) |
| 1778 | } |
| 1779 | |
| 1780 | /// Current value of [`VARTX_RECT_LEAF_HITS`] (lane-r14 r3): how many |
| 1781 | /// rectangular var-tx leaf transform units were coded, `[32x16, 16x32]`. |
| 1782 | pub(crate) fn vartx_rect_leaf_hits() -> [usize; 2] { |
| 1783 | VARTX_RECT_LEAF_HITS.with(|c| c.get()) |
| 1784 | } |
| 1785 | |
| 1786 | /// Current value of [`VARTX_RECT_LEAF4_HITS`] (lane-inter16ab r4): 8x4/4x8 |
| 1787 | /// split leaves of a 16x4/4x16 strip, `[8x4, 4x8]`. |
| 1788 | pub(crate) fn vartx_rect_leaf4_hits() -> [usize; 2] { |
| 1789 | VARTX_RECT_LEAF4_HITS.with(|c| c.get()) |
| 1790 | } |
| 1791 | |
| 1792 | /// Records one rectangular var-tx leaf. |
| 1793 | fn vartx_rect_leaf_hit(tw: usize, th: usize) { |
| 1794 | if crate::envflags::env_flag!("EC_VARTXLEAF") { |
| 1795 | eprintln!("EC_VARTXLEAF tw={tw} th={th}"); |
| 1796 | } |
| 1797 | let cell = if tw.min(th) == 4 { &VARTX_RECT_LEAF4_HITS } else { &VARTX_RECT_LEAF_HITS }; |
| 1798 | cell.with(|c| { |
| 1799 | let mut h = c.get(); |
| 1800 | h[usize::from(th > tw)] += 1; |
| 1801 | c.set(h); |
| 1802 | }); |
| 1803 | } |
| 1804 | |
| 1805 | /// Sets the current thread's [`TX_SELECT_INTER`]/[`REDUCED_TX_SET_INTER`]/ |
| 1806 | /// [`ENABLE_FILTER_INTRA_INTER`]. |
| 1807 | pub(crate) fn set_inter_tx_mode( |
| 1808 | tx_select: bool, |
| 1809 | reduced_tx_set: bool, |
| 1810 | enable_filter_intra: bool, fctx: &crate::decode::FrameCtx, |
| 1811 | ) { |
| 1812 | fctx.tx_select_inter.with(|c| c.set(tx_select)); |
| 1813 | fctx.intra_only.with(|c| c.set(false)); |
| 1814 | fctx.tx_select_frame.with(|c| c.set(tx_select)); |
| 1815 | fctx.reduced_tx_set_inter.with(|c| c.set(reduced_tx_set)); |
| 1816 | fctx.enable_filter_intra_inter.with(|c| c.set(enable_filter_intra)); |
| 1817 | } |
| 1818 | |
| 1819 | // How many `partition_w16` reads (a 16x16 block's own partition symbol, key |
| 1820 | // intra-frame path) resolved to `PARTITION_VERT_B` -- lane-rect16 r1: a |
| 1821 | // plain default-settings aomenc run over lavfi mandelbrot hits this arm on |
| 1822 | // frame 0's very first non-NONE/SPLIT `partition_w16` symbol (mi=(3,2)), not |
| 1823 | // the plain HORZ/VERT this lane's charter assumed. Left rect (8x16, real |
| 1824 | // `decode_block_rect`) + two 8x8 leaves stacked on the right (`decode_leaf8`, |
| 1825 | // chained via `prev_leaf` exactly like the existing 16x16-SPLIT-into-4x8x8 |
| 1826 | // path above). |
| 1827 | thread_local! { |
| 1828 | static VERT_B_INTRA_HITS: std::cell::Cell<usize> = |
| 1829 | const { std::cell::Cell::new(0) }; |
| 1830 | } |
| 1831 | |
| 1832 | /// Current value of [`VERT_B_INTRA_HITS`]. |
| 1833 | #[allow(dead_code)] // read only from the `#[cfg(test)]` gates |
| 1834 | pub(crate) fn vert_b_intra_hits() -> usize { |
| 1835 | VERT_B_INTRA_HITS.with(|c| c.get()) |
| 1836 | } |
| 1837 | |
| 1838 | // lane-ab16 r1: how many 16x16-level AB partitions fired, per arm, in |
| 1839 | // `PARTITION_HORZ_A`, `_HORZ_B`, `_VERT_A`, `_VERT_B` order. Each arm is two |
| 1840 | // 8x8 squares (`decode_leaf8`) plus one 16x8/8x16 strip (`decode_leaf_rect`, |
| 1841 | // the non-skip coefficient path lane-rectx proved); the gate hard-asserts |
| 1842 | // every arm separately so a run where aomenc's RD only picked one cannot |
| 1843 | // pass as proof of four. |
| 1844 | thread_local! { |
| 1845 | static AB16_HITS: std::cell::Cell<[usize; 4]> = const { std::cell::Cell::new([0; 4]) }; |
| 1846 | } |
| 1847 | |
| 1848 | // [`AB16_HITS`] per arm (HORZ_A, HORZ_B, VERT_A, VERT_B at 16x16). |
| 1849 | thread_local! { |
| 1850 | /// 4x4 transform units decoded inside an 8x8 square of a |
| 1851 | /// `PARTITION_VERT_A`/`_B` -- the exact call that panicked before |
| 1852 | /// [`crate::encode::Reach::of_tu`] (lane-ab16 r2), so the gate can prove |
| 1853 | /// a real `--enable-tx-size-search=1` stream reaches it. |
| 1854 | static VERT_AB_TX4_HITS: std::cell::Cell<usize> = const { std::cell::Cell::new(0) }; |
| 1855 | } |
| 1856 | |
| 1857 | #[allow(dead_code)] // read only from the `#[cfg(test)]` gates |
| 1858 | pub(crate) fn vert_ab_tx4_hits() -> usize { |
| 1859 | VERT_AB_TX4_HITS.with(std::cell::Cell::get) |
| 1860 | } |
| 1861 | |
| 1862 | #[allow(dead_code)] // read only from the `#[cfg(test)]` gates |
| 1863 | pub(crate) fn ab16_hits_by_arm() -> [usize; 4] { |
| 1864 | AB16_HITS.with(std::cell::Cell::get) |
| 1865 | } |
| 1866 | |
| 1867 | fn bump_ab16(arm: usize) { |
| 1868 | hit_do! { |
| 1869 | AB16_HITS.with(|c| { |
| 1870 | let mut v = c.get(); |
| 1871 | v[arm] += 1; |
| 1872 | c.set(v); |
| 1873 | }); |
| 1874 | } |
| 1875 | } |
| 1876 | |
| 1877 | // How many `partition_w16` reads resolved to a plain `PARTITION_HORZ`/ |
| 1878 | // `PARTITION_VERT` (lane-rect16 r2): re-measuring after the VERT_B fix above |
| 1879 | // showed mandelbrot's real first-hit blocker is this plain arm at mi=(0,7), |
| 1880 | // not VERT_B (`refusal-names-a-correlate`; the VERT_B write-up's "frame 0's |
| 1881 | // very first non-NONE/SPLIT symbol" claim was about decode ORDER within one |
| 1882 | // superblock's z-order recursion, not raster mi position). |
| 1883 | thread_local! { |
| 1884 | static HORZ_VERT_INTRA_HITS: std::cell::Cell<usize> = |
| 1885 | const { std::cell::Cell::new(0) }; |
| 1886 | } |
| 1887 | |
| 1888 | /// Current value of [`HORZ_VERT_INTRA_HITS`]. |
| 1889 | #[allow(dead_code)] // read only from the `#[cfg(test)]` gates |
| 1890 | pub(crate) fn horz_vert_intra_hits() -> usize { |
| 1891 | HORZ_VERT_INTRA_HITS.with(|c| c.get()) |
| 1892 | } |
| 1893 | |
| 1894 | // How many blocks were decoded as a frame-edge rect strip -- the single |
| 1895 | // coded half of a `PARTITION_HORZ`/`PARTITION_VERT` at the true frame edge |
| 1896 | // (lane-oddh r1), i.e. a block living inside the 8px band of a frame whose |
| 1897 | // height or width is 8 mod 16 (the user's 3840x1608 film). Intra and inter |
| 1898 | // paths share the counter. |
| 1899 | thread_local! { |
| 1900 | static EDGE_RECT_STRIP_HITS: std::cell::Cell<usize> = |
| 1901 | const { std::cell::Cell::new(0) }; |
| 1902 | } |
| 1903 | |
| 1904 | /// Current value of [`EDGE_RECT_STRIP_HITS`]. |
| 1905 | #[allow(dead_code)] // gate counter accessor with no reader yet; the counter itself is live |
| 1906 | pub(crate) fn edge_rect_strip_hits() -> usize { |
| 1907 | EDGE_RECT_STRIP_HITS.with(|c| c.get()) |
| 1908 | } |
| 1909 | |
| 1910 | // lane-interedge r1: frame-edge half strips on the INTER path, per partition |
| 1911 | // level (64/32/16) x axis (HORZ at a bottom edge, VERT at a right edge). The |
| 1912 | // index is `level_idx * 2 + axis` with `level_idx` 0=64, 1=32, 2=16 and |
| 1913 | // `axis` 0=HORZ (`!has_rows`), 1=VERT (`!has_cols`). Both of the user's films |
| 1914 | // are height mod 16 == 8, so every inter frame crosses these nodes. |
| 1915 | thread_local! { |
| 1916 | static INTER_EDGE_STRIP_HITS: std::cell::Cell<[usize; 6]> = |
| 1917 | const { std::cell::Cell::new([0; 6]) }; |
| 1918 | } |
| 1919 | |
| 1920 | /// Bumps one bucket of [`INTER_EDGE_STRIP_HITS`]. |
| 1921 | pub(crate) fn bump_inter_edge_strip(idx: usize) { |
| 1922 | hit_do! { |
| 1923 | INTER_EDGE_STRIP_HITS.with(|c| { |
| 1924 | let mut v = c.get(); |
| 1925 | v[idx] += 1; |
| 1926 | c.set(v); |
| 1927 | }); |
| 1928 | } |
| 1929 | } |
| 1930 | |
| 1931 | // lane-edgeboth r1: how many partition nodes were coded as the BOTH-AXES-CUT |
| 1932 | // forced split -- libaom `decode_partition` (decodeframe.c): |
| 1933 | // `else if (!has_rows && !has_cols) partition = PARTITION_SPLIT;`, the one |
| 1934 | // straddle case that reads NO symbol at all. Indexed by level: |
| 1935 | // 0 = 16x16, 1 = 32x32, 2 = 64x64, 3 = 128x128; intra and inter share it. |
| 1936 | thread_local! { |
| 1937 | static EDGE_BOTH_CUT_HITS: std::cell::Cell<[usize; 4]> = |
| 1938 | const { std::cell::Cell::new([0; 4]) }; |
| 1939 | } |
| 1940 | |
| 1941 | fn bump_edge_both_cut(level: usize) { |
| 1942 | hit_do! { |
| 1943 | EDGE_BOTH_CUT_HITS.with(|c| { |
| 1944 | let mut v = c.get(); |
| 1945 | v[level] += 1; |
| 1946 | c.set(v); |
| 1947 | }); |
| 1948 | } |
| 1949 | } |
| 1950 | |
| 1951 | /// Current value of [`INTER_EDGE_STRIP_HITS`] (`[h64, v64, h32, v32, h16, v16]`). |
| 1952 | pub fn inter_edge_strip_hits() -> [usize; 6] { |
| 1953 | INTER_EDGE_STRIP_HITS.with(|c| c.get()) |
| 1954 | } |
| 1955 | |
| 1956 | /// Resets [`INTER_EDGE_STRIP_HITS`] -- a refused attempt must not carry hits |
| 1957 | /// into the next one (class `counter-from-refused-stream`). |
| 1958 | pub fn reset_inter_edge_strip_hits() { |
| 1959 | INTER_EDGE_STRIP_HITS.with(|c| c.set([0; 6])); |
| 1960 | } |
| 1961 | |
| 1962 | /// Current value of [`EDGE_BOTH_CUT_HITS`] (`[16, 32, 64, 128]` levels). |
| 1963 | #[allow(dead_code)] // read only from the `#[cfg(test)]` gates |
| 1964 | pub(crate) fn edge_both_cut_hits() -> [usize; 4] { |
| 1965 | EDGE_BOTH_CUT_HITS.with(|c| c.get()) |
| 1966 | } |
| 1967 | |
| 1968 | /// Zeroes [`EDGE_BOTH_CUT_HITS`] so a gate can measure a per-attempt delta. |
| 1969 | #[allow(dead_code)] // read only from the `#[cfg(test)]` gates |
| 1970 | pub(crate) fn reset_edge_both_cut_hits() { |
| 1971 | EDGE_BOTH_CUT_HITS.with(|c| c.set([0; 4])); |
| 1972 | } |
| 1973 | |
| 1974 | // How many HORZ/VERT intra strips resolved a SPLIT luma transform |
| 1975 | // (`tx_depth != 0`) and decoded it per transform unit (lane-rectsplit r1) -- |
| 1976 | // the case every rect path here refused by name before, and the first |
| 1977 | // refusal the user's ReelB extract (600s in) stops at. |
| 1978 | thread_local! { |
| 1979 | static RECT_SPLIT_TX_HITS: std::cell::Cell<usize> = |
| 1980 | const { std::cell::Cell::new(0) }; |
| 1981 | } |
| 1982 | |
| 1983 | /// Current value of [`RECT_SPLIT_TX_HITS`]. |
| 1984 | #[allow(dead_code)] // read only from the `#[cfg(test)]` gates |
| 1985 | pub(crate) fn rect_split_tx_hits() -> usize { |
| 1986 | RECT_SPLIT_TX_HITS.with(|c| c.get()) |
| 1987 | } |
| 1988 | |
| 1989 | // The subset of [`RECT_SPLIT_TX_HITS`] that is BOTH superblock-level (one |
| 1990 | // side 64) AND split past the first depth, i.e. tiled more than one unit in |
| 1991 | // each axis -- the only shape with a transform unit that has neighbours on |
| 1992 | // two sides inside its own strip (`row_off > 0 && col_off > 0`), and the one |
| 1993 | // no gate fired before lane-rectsplit r2 (the shared counter above also |
| 1994 | // counts 32x32-level and depth-1 strips, so it cannot prove this case). |
| 1995 | thread_local! { |
| 1996 | static RECT_SPLIT_SB_INTERIOR_TU_HITS: std::cell::Cell<usize> = |
| 1997 | const { std::cell::Cell::new(0) }; |
| 1998 | } |
| 1999 | |
| 2000 | /// Current value of [`RECT_SPLIT_SB_INTERIOR_TU_HITS`]. |
| 2001 | #[allow(dead_code)] // read only from the `#[cfg(test)]` gates |
| 2002 | pub(crate) fn rect_split_sb_interior_tu_hits() -> usize { |
| 2003 | RECT_SPLIT_SB_INTERIOR_TU_HITS.with(|c| c.get()) |
| 2004 | } |
| 2005 | |
| 2006 | // Per-shape hit counts for the rect chroma transforms lane-rectchroma r1 |
| 2007 | // wired into [`decode_rect_split`] -- the shapes a split-transform strip's |
| 2008 | // chroma half takes that had no coefficient tables before (index order: |
| 2009 | // 32x8, 8x32, 8x4, 4x8). One counter per shape because a gate that only |
| 2010 | // proves the pair it happens to hit cannot catch a transposed scan/context |
| 2011 | // table (class scan-weights-cross-axis). |
| 2012 | thread_local! { |
| 2013 | static RECT_SPLIT_CHROMA_SHAPE_HITS: std::cell::Cell<[usize; 4]> = |
| 2014 | const { std::cell::Cell::new([0; 4]) }; |
| 2015 | } |
| 2016 | |
| 2017 | /// Current value of [`RECT_SPLIT_CHROMA_SHAPE_HITS`] (32x8, 8x32, 8x4, 4x8). |
| 2018 | #[allow(dead_code)] // read only from the `#[cfg(test)]` gates |
| 2019 | pub(crate) fn rect_split_chroma_shape_hits() -> [usize; 4] { |
| 2020 | RECT_SPLIT_CHROMA_SHAPE_HITS.with(|c| c.get()) |
| 2021 | } |
| 2022 | |
| 2023 | // lane-av1-422: how many SQUARE chroma coefficient units the strip path read |
| 2024 | // on a 4:2:2 frame (ss 1,0) -- the half-width, unhalved-height shape whose |
| 2025 | // tables only exist because of the square rows in the chroma_tiled arm. A |
| 2026 | // 4:2:0 or 4:4:4 frame can never increment it (their rect strips' chroma is |
| 2027 | // itself rect), and a restored `/2 chroma height` diverts 4:2:2 squares onto |
| 2028 | // the rect rows, so the witness gate reads 0 there and fails. |
| 2029 | thread_local! { |
| 2030 | static CHROMA422_SQUARE_HITS: std::cell::Cell<usize> = |
| 2031 | const { std::cell::Cell::new(0) }; |
| 2032 | } |
| 2033 | |
| 2034 | /// Current value of [`CHROMA422_SQUARE_HITS`]. |
| 2035 | // lane-av1-422b: `pub` so `examples/decode_probe.rs` can census it the way |
| 2036 | // it does every other capability counter. |
| 2037 | pub fn chroma422_square_hits() -> usize { |
| 2038 | CHROMA422_SQUARE_HITS.with(|c| c.get()) |
| 2039 | } |
| 2040 | |
| 2041 | // lane-av1-422f: how many RECT chroma coefficient units the square-block |
| 2042 | // path read on a 4:2:2 frame (ss 1,0) -- TX_4X8 / TX_8X16 / TX_16X32, the |
| 2043 | // plane block's own half-width, unhalved-height shape. A 4:2:0 or 4:4:4 |
| 2044 | // frame never takes the rect branch (their units are square |
| 2045 | // `chroma_tx == chroma_tx_h`), so a witness gate reading 0 here proves the |
| 2046 | // model reverted to squares. |
| 2047 | thread_local! { |
| 2048 | static CHROMA422_RECT_HITS: std::cell::Cell<usize> = |
| 2049 | const { std::cell::Cell::new(0) }; |
| 2050 | } |
| 2051 | |
| 2052 | /// Current value of [`CHROMA422_RECT_HITS`]. |
| 2053 | pub fn chroma422_rect_hits() -> usize { |
| 2054 | CHROMA422_RECT_HITS.with(|c| c.get()) |
| 2055 | } |
| 2056 | |
| 2057 | // lane-av1-422g: how many PER-LEAF TX_4X4 chroma coefficient units the |
| 2058 | // sub-8x8 split4 path read on a 4:2:2 frame (ss 1,0) -- one per even-column |
| 2059 | // leaf per plane (`is_chroma_reference`'s `!(mi_col & ss_x)`). A 4:2:0 frame |
| 2060 | // codes no sub-8 chroma at all and 4:4:4 goes through `sub8_leaf_chroma444`, |
| 2061 | // so a witness gate reading 0 here proves the per-leaf 4:2:2 arm never ran. |
| 2062 | thread_local! { |
| 2063 | static CHROMA422_SUB8_HITS: std::cell::Cell<usize> = |
| 2064 | const { std::cell::Cell::new(0) }; |
| 2065 | } |
| 2066 | |
| 2067 | /// Current value of [`CHROMA422_SUB8_HITS`]. |
| 2068 | pub fn chroma422_sub8_hits() -> usize { |
| 2069 | CHROMA422_SUB8_HITS.with(|c| c.get()) |
| 2070 | } |
| 2071 | |
| 2072 | // How many HORZ/VERT intra strips actually predicted with filter intra |
| 2073 | // (lane-rectsplit r1) -- the case `decode_block_rect` refused by name |
| 2074 | // ("this decoder predicts square-only") until `predict_filter_intra` took |
| 2075 | // `bw`x`bh`. |
| 2076 | thread_local! { |
| 2077 | static FILTER_INTRA_RECT_HITS: std::cell::Cell<usize> = |
| 2078 | const { std::cell::Cell::new(0) }; |
| 2079 | } |
| 2080 | |
| 2081 | /// Current value of [`FILTER_INTRA_RECT_HITS`]. |
| 2082 | #[allow(dead_code)] // read only from the `#[cfg(test)]` gates |
| 2083 | pub(crate) fn filter_intra_rect_hits() -> usize { |
| 2084 | FILTER_INTRA_RECT_HITS.with(|c| c.get()) |
| 2085 | } |
| 2086 | |
| 2087 | // As [`FILTER_INTRA_RECT_HITS`], counting only the 32x32-level 1:4 strips |
| 2088 | // (32x8/8x32, lane-fi32x8 r1) -- the shapes whose `use_filter_intra` symbol |
| 2089 | // was never read at all before that round, so a gate needs to prove the flag |
| 2090 | // fired at THAT shape and not at a 16x8 one somewhere else in the frame. |
| 2091 | // Split by ORIENTATION: rows 8 (32x8) and 9 (8x32) of `cdf::FILTER_INTRA` |
| 2092 | // carry different probabilities (18101 vs 20229), so a transposed pair is |
| 2093 | // only caught by a gate that sees the flag fire on BOTH shapes (class |
| 2094 | // scan-weights-cross-axis). |
| 2095 | thread_local! { |
| 2096 | static FILTER_INTRA_RECT4_HORZ_HITS: std::cell::Cell<usize> = |
| 2097 | const { std::cell::Cell::new(0) }; |
| 2098 | static FILTER_INTRA_RECT4_VERT_HITS: std::cell::Cell<usize> = |
| 2099 | const { std::cell::Cell::new(0) }; |
| 2100 | } |
| 2101 | |
| 2102 | /// Current value of [`FILTER_INTRA_RECT4_HORZ_HITS`] (32x8 strips). |
| 2103 | #[allow(dead_code)] // read only from the `#[cfg(test)]` gates |
| 2104 | pub(crate) fn filter_intra_rect4_horz_hits() -> usize { |
| 2105 | FILTER_INTRA_RECT4_HORZ_HITS.with(|c| c.get()) |
| 2106 | } |
| 2107 | |
| 2108 | /// Current value of [`FILTER_INTRA_RECT4_VERT_HITS`] (8x32 strips). |
| 2109 | #[allow(dead_code)] // read only from the `#[cfg(test)]` gates |
| 2110 | pub(crate) fn filter_intra_rect4_vert_hits() -> usize { |
| 2111 | FILTER_INTRA_RECT4_VERT_HITS.with(|c| c.get()) |
| 2112 | } |
| 2113 | |
| 2114 | // How many rectangular transform units actually coded coefficients through |
| 2115 | // [`read_coeffs_rect`] (`all_zero == 0`) -- lane-rect1d's measure that a gate |
| 2116 | // exercised the rect coefficient reader at all rather than passing on |
| 2117 | // all-skip blocks. |
| 2118 | thread_local! { |
| 2119 | static RECT_COEFF_TU_HITS: std::cell::Cell<usize> = const { std::cell::Cell::new(0) }; |
| 2120 | static RECT_CLASS1_HITS: std::cell::Cell<usize> = const { std::cell::Cell::new(0) }; |
| 2121 | } |
| 2122 | |
| 2123 | /// Current value of [`RECT_COEFF_TU_HITS`]. |
| 2124 | #[allow(dead_code)] // read only from the `#[cfg(test)]` gates |
| 2125 | pub(crate) fn rect_coeff_tu_hits() -> usize { |
| 2126 | RECT_COEFF_TU_HITS.with(|c| c.get()) |
| 2127 | } |
| 2128 | |
| 2129 | /// Rectangular transform units whose resolved `tx_type` is a 1D class |
| 2130 | /// (`V_DCT`/`H_DCT`) -- [`TX_CLASS1_HITS`]'s rect-only half, so a gate can |
| 2131 | /// pin that the 1D-class-on-a-RECT-transform path fired rather than settling |
| 2132 | /// for the square reader's own 1D types. |
| 2133 | #[allow(dead_code)] // read only from the `#[cfg(test)]` gates |
| 2134 | pub(crate) fn rect_class1_hits() -> usize { |
| 2135 | RECT_CLASS1_HITS.with(|c| c.get()) |
| 2136 | } |
| 2137 | |
| 2138 | // As [`FILTER_INTRA_RECT_HITS`], counting only the strips BELOW 16x16 |
| 2139 | // (8x16/16x8 leaves of a 16x16 HORZ/VERT partition, lane-fistrip r1) -- the |
| 2140 | // shapes whose `use_filter_intra` symbol had no CDF class before this round. |
| 2141 | // A gate asserting the wider counter would be satisfied by a 32x16 strip and |
| 2142 | // prove nothing about the new shapes. |
| 2143 | thread_local! { |
| 2144 | static FILTER_INTRA_RECT_SUB16_HITS: std::cell::Cell<usize> = |
| 2145 | const { std::cell::Cell::new(0) }; |
| 2146 | } |
| 2147 | |
| 2148 | /// Current value of [`FILTER_INTRA_RECT_SUB16_HITS`]. |
| 2149 | #[allow(dead_code)] // read only from the `#[cfg(test)]` gates |
| 2150 | pub(crate) fn filter_intra_rect_sub16_hits() -> usize { |
| 2151 | FILTER_INTRA_RECT_SUB16_HITS.with(|c| c.get()) |
| 2152 | } |
| 2153 | |
| 2154 | // As [`FILTER_INTRA_RECT_SUB16_HITS`], one partition level further down: |
| 2155 | // 4x8/8x4 leaves of an 8x8 HORZ/VERT (lane-fi8 r1). A gate on the sub16 |
| 2156 | // counter would be satisfied by a 16x8 strip and prove nothing here. |
| 2157 | thread_local! { |
| 2158 | static FILTER_INTRA_RECT_SUB8_HITS: std::cell::Cell<usize> = |
| 2159 | const { std::cell::Cell::new(0) }; |
| 2160 | } |
| 2161 | |
| 2162 | /// Current value of [`FILTER_INTRA_RECT_SUB8_HITS`]. |
| 2163 | #[allow(dead_code)] // read only from the `#[cfg(test)]` gates |
| 2164 | pub(crate) fn filter_intra_rect_sub8_hits() -> usize { |
| 2165 | FILTER_INTRA_RECT_SUB8_HITS.with(|c| c.get()) |
| 2166 | } |
| 2167 | |
| 2168 | // How many [`read_coeffs`] reads resolved a `tx_type` outside `TX_CLASS_2D` |
| 2169 | // (`V_DCT`/`H_DCT`, spec 5.11.39's `TxClass::Horiz`/`Vert`), across every |
| 2170 | // call on the current thread -- the same before/after counter pattern as |
| 2171 | // [`TX_DEPTH_HITS`], proving a stream actually exercised the class-split |
| 2172 | // `eob_pt`/1D-neighbour context path rather than every block coincidentally |
| 2173 | // landing on `DCT_DCT`. |
| 2174 | thread_local! { |
| 2175 | static TX_CLASS1_HITS: std::cell::Cell<usize> = |
| 2176 | const { std::cell::Cell::new(0) }; |
| 2177 | } |
| 2178 | |
| 2179 | // How many `uv_mode` reads resolved to a directional chroma mode (neither |
| 2180 | // `DC_PRED` nor `UV_CFL_PRED`), across every call on the current thread -- the same |
| 2181 | // before/after counter pattern as [`FILTER_INTRA_HITS`], proving a stream |
| 2182 | // actually exercised chroma's own directional predictor. |
| 2183 | thread_local! { |
| 2184 | static DIRECTIONAL_UV_HITS: std::cell::Cell<usize> = |
| 2185 | const { std::cell::Cell::new(0) }; |
| 2186 | } |
| 2187 | |
| 2188 | /// Current value of [`DIRECTIONAL_UV_HITS`]. |
| 2189 | #[allow(dead_code)] // read only from the `#[cfg(test)]` gates |
| 2190 | pub(crate) fn directional_uv_hits() -> usize { |
| 2191 | DIRECTIONAL_UV_HITS.with(|c| c.get()) |
| 2192 | } |
| 2193 | |
| 2194 | // How many `angle_delta`/`angle_delta_uv` symbols this decoder has read as |
| 2195 | // something other than [`ANGLE_DELTA_ZERO`], across every call in the |
| 2196 | // process -- the same before/after counter pattern as [`FILTER_INTRA_HITS`], |
| 2197 | // proving a stream actually exercised a nonzero angle delta. |
| 2198 | thread_local! { |
| 2199 | static ANGLE_DELTA_HITS: std::cell::Cell<usize> = |
| 2200 | const { std::cell::Cell::new(0) }; |
| 2201 | } |
| 2202 | |
| 2203 | // How many [`read_single_ref`] reads resolved to a reference other than |
| 2204 | // `LAST_FRAME`, across every call on the current thread -- the same before/after |
| 2205 | // counter pattern as [`FILTER_INTRA_HITS`], proving a stream actually |
| 2206 | // exercised a non-`LAST_FRAME` reference (`GOLDEN_FRAME`, this decoder's |
| 2207 | // only other supported one so far) rather than every block coincidentally |
| 2208 | // resolving `LAST_FRAME`. |
| 2209 | thread_local! { |
| 2210 | static NON_LAST_REF_HITS: std::cell::Cell<usize> = |
| 2211 | const { std::cell::Cell::new(0) }; |
| 2212 | } |
| 2213 | |
| 2214 | /// Current value of [`NON_LAST_REF_HITS`]. |
| 2215 | #[allow(dead_code)] // read only from the `#[cfg(test)]` gates |
| 2216 | pub(crate) fn non_last_ref_hits() -> usize { |
| 2217 | NON_LAST_REF_HITS.with(|c| c.get()) |
| 2218 | } |
| 2219 | |
| 2220 | // Per-reference [`NON_LAST_REF_HITS`] breakdown (lane-av1refs): one counter |
| 2221 | // per `MV_REFERENCE_FRAME` past `LAST_FRAME`, so a gate targeting one |
| 2222 | // specific reference (`LAST2`/`LAST3`/`GOLDEN`/`BWDREF`/`ALTREF2`/`ALTREF`) |
| 2223 | // can prove THAT reference fired rather than any non-`LAST_FRAME` one -- |
| 2224 | // `NON_LAST_REF_HITS` alone cannot tell a `LAST2_FRAME` gate from a |
| 2225 | // `GOLDEN_FRAME` draw it did not ask for. |
| 2226 | thread_local! { |
| 2227 | static LAST2_HITS: std::cell::Cell<usize> = |
| 2228 | const { std::cell::Cell::new(0) }; |
| 2229 | } |
| 2230 | thread_local! { |
| 2231 | static LAST3_HITS: std::cell::Cell<usize> = |
| 2232 | const { std::cell::Cell::new(0) }; |
| 2233 | } |
| 2234 | thread_local! { |
| 2235 | static GOLDEN_HITS: std::cell::Cell<usize> = |
| 2236 | const { std::cell::Cell::new(0) }; |
| 2237 | } |
| 2238 | thread_local! { |
| 2239 | static BWDREF_HITS: std::cell::Cell<usize> = |
| 2240 | const { std::cell::Cell::new(0) }; |
| 2241 | } |
| 2242 | thread_local! { |
| 2243 | static ALTREF2_HITS: std::cell::Cell<usize> = |
| 2244 | const { std::cell::Cell::new(0) }; |
| 2245 | } |
| 2246 | thread_local! { |
| 2247 | static ALTREF_HITS: std::cell::Cell<usize> = |
| 2248 | const { std::cell::Cell::new(0) }; |
| 2249 | } |
| 2250 | |
| 2251 | // How many [`read_comp_mode`] reads resolved `COMPOUND_REFERENCE` (lane-av1comp) |
| 2252 | // -- proves a `reference_select` stream actually reached a block that picked |
| 2253 | // compound, not just that the header bit was set. Every such block is |
| 2254 | // refused after this fires ([`decode_inter_block`]/[`decode_inter_block8`]), |
| 2255 | // so this only ever counts attempted reads, never a completed decode. |
| 2256 | thread_local! { |
| 2257 | static COMP_MODE_HITS: std::cell::Cell<usize> = |
| 2258 | const { std::cell::Cell::new(0) }; |
| 2259 | } |
| 2260 | |
| 2261 | /// Current value of [`COMP_MODE_HITS`]. |
| 2262 | #[allow(dead_code)] // read only from the `#[cfg(test)]` gates |
| 2263 | pub(crate) fn comp_mode_hits() -> usize { |
| 2264 | COMP_MODE_HITS.with(|c| c.get()) |
| 2265 | } |
| 2266 | |
| 2267 | // How many blocks [`read_intra_mode`] decoded a real (nonzero-size) palette-Y |
| 2268 | // use for, across every call on the current thread -- the same before/after |
| 2269 | // counter pattern as [`FILTER_INTRA_HITS`], proving a stream actually |
| 2270 | // exercised palette reconstruction rather than only reading and refusing the |
| 2271 | // `palette_y_mode` symbol. |
| 2272 | thread_local! { |
| 2273 | static PALETTE_HITS: std::cell::Cell<usize> = const { std::cell::Cell::new(0) }; |
| 2274 | } |
| 2275 | |
| 2276 | /// Current value of [`PALETTE_HITS`]. |
| 2277 | #[allow(dead_code)] // read only from the `#[cfg(test)]` gates |
| 2278 | pub(crate) fn palette_hits() -> usize { |
| 2279 | PALETTE_HITS.with(|c| c.get()) |
| 2280 | } |
| 2281 | |
| 2282 | // lane-tiles r12: of those, how many sat at their own TILE's left column -- |
| 2283 | // exactly the blocks whose palette cache/ctx must NOT see the previous tile's |
| 2284 | // colours (`av1_get_palette_cache`'s `xd->left_mbmi == NULL`). A multi-tile |
| 2285 | // palette gate that never hits one proves nothing about the tile guard. |
| 2286 | thread_local! { |
| 2287 | static PALETTE_TILE_LEFT_HITS: std::cell::Cell<usize> = const { std::cell::Cell::new(0) }; |
| 2288 | } |
| 2289 | |
| 2290 | /// Current value of [`PALETTE_TILE_LEFT_HITS`]. |
| 2291 | #[allow(dead_code)] // read only from the `#[cfg(test)]` gates |
| 2292 | pub(crate) fn palette_tile_left_hits() -> usize { |
| 2293 | PALETTE_TILE_LEFT_HITS.with(|c| c.get()) |
| 2294 | } |
| 2295 | |
| 2296 | // lane-palette2 r1: as [`PALETTE_HITS`], for a real (nonzero-size) chroma |
| 2297 | // palette use -- proves a stream actually reconstructed UV palette pixels, |
| 2298 | // not just read and refused the `palette_uv_mode` symbol. |
| 2299 | thread_local! { |
| 2300 | static PALETTE_UV_HITS: std::cell::Cell<usize> = const { std::cell::Cell::new(0) }; |
| 2301 | } |
| 2302 | |
| 2303 | /// Current value of [`PALETTE_UV_HITS`]. |
| 2304 | #[allow(dead_code)] // read only from the `#[cfg(test)]` gates |
| 2305 | pub(crate) fn palette_uv_hits() -> usize { |
| 2306 | PALETTE_UV_HITS.with(|c| c.get()) |
| 2307 | } |
| 2308 | |
| 2309 | // lane-palette2 r1: how many HORZ/VERT rect intra strips ([`read_intra_mode_rect`]) |
| 2310 | // decoded a real palette-Y use -- proves the rect strip's own palette syntax |
| 2311 | // (not just the square path's) actually fired. |
| 2312 | thread_local! { |
| 2313 | static PALETTE_RECT_HITS: std::cell::Cell<usize> = const { std::cell::Cell::new(0) }; |
| 2314 | } |
| 2315 | |
| 2316 | /// Current value of [`PALETTE_RECT_HITS`]. |
| 2317 | #[allow(dead_code)] // read only from the `#[cfg(test)]` gates |
| 2318 | pub(crate) fn palette_rect_hits() -> usize { |
| 2319 | PALETTE_RECT_HITS.with(|c| c.get()) |
| 2320 | } |
| 2321 | |
| 2322 | // lane-palette2 r6: how many HORZ/VERT rect intra strips ran the |
| 2323 | // [`palette_bsize_ctx_wh`]-gated code path in [`read_intra_mode_rect`] -- |
| 2324 | // i.e. `allow_screen_content_tools` was set and the strip's `bw`x`bh` was in |
| 2325 | // range, so `palette_y_mode`/`palette_uv_mode` were actually read (and, per |
| 2326 | // RD, almost always refused-on-use because the block doesn't end up USING a |
| 2327 | // palette -- see r5's report). This is the site the removed refusal |
| 2328 | // ("a HORZ/VERT intra strip in a screen-content frame") used to guard: it |
| 2329 | // fired here whether or not the block picked a palette, so this is the |
| 2330 | // counter that proves the case r5's narrower [`PALETTE_RECT_HITS`] almost |
| 2331 | // never observes. |
| 2332 | thread_local! { |
| 2333 | static RECT_SCREEN_CONTENT_HITS: std::cell::Cell<usize> = const { std::cell::Cell::new(0) }; |
| 2334 | } |
| 2335 | |
| 2336 | /// Current value of [`RECT_SCREEN_CONTENT_HITS`]. |
| 2337 | #[allow(dead_code)] // read only from the `#[cfg(test)]` gates |
| 2338 | pub(crate) fn rect_screen_content_hits() -> usize { |
| 2339 | RECT_SCREEN_CONTENT_HITS.with(|c| c.get()) |
| 2340 | } |
| 2341 | |
| 2342 | // lane-palette2 r1: how many palette-Y luma transform UNITS decoded under a |
| 2343 | // split luma transform (`logical_tx != side`) -- proves the per-TU index-map |
| 2344 | // windowing actually ran, not just that the whole-block path stayed exact. |
| 2345 | // Counts units, not blocks (a 64x64 palette block split to 16x16 TUs adds 16); |
| 2346 | // first actually incremented in r7, when the refusal it guarded was lifted. |
| 2347 | thread_local! { |
| 2348 | static PALETTE_SPLIT_TX_HITS: std::cell::Cell<usize> = const { std::cell::Cell::new(0) }; |
| 2349 | } |
| 2350 | |
| 2351 | thread_local! { |
| 2352 | /// lane-kf900 r4: rect intra strips that ACTUALLY use a palette -- the |
| 2353 | /// 1:4 shapes (32x8/8x32, 16x4/4x16) and the sub-16x16 2:1 leaves |
| 2354 | /// (16x8/8x16), the two shapes the lifted refusals named. |
| 2355 | static RECT4_PALETTE_HITS: std::cell::Cell<usize> = const { std::cell::Cell::new(0) }; |
| 2356 | } |
| 2357 | |
| 2358 | /// Current value of [`RECT4_PALETTE_HITS`]. |
| 2359 | #[allow(dead_code)] // read only from the `#[cfg(test)]` gates |
| 2360 | pub(crate) fn rect4_palette_hits() -> usize { |
| 2361 | RECT4_PALETTE_HITS.with(|c| c.get()) |
| 2362 | } |
| 2363 | |
| 2364 | /// Current value of [`PALETTE_SPLIT_TX_HITS`]. |
| 2365 | #[allow(dead_code)] // read only from the `#[cfg(test)]` gates |
| 2366 | pub(crate) fn palette_split_tx_hits() -> usize { |
| 2367 | PALETTE_SPLIT_TX_HITS.with(|c| c.get()) |
| 2368 | } |
| 2369 | |
| 2370 | thread_local! { |
| 2371 | /// lane-pal8 r1: square 8x8 LEAVES ([`decode_leaf8`]) that actually use a |
| 2372 | /// palette (Y or UV) -- the shape every 8-bit `--tune-content=screen` |
| 2373 | /// stream stopped on, since aomenc palettes square blocks at 8 bit and |
| 2374 | /// `--min-partition-size=8` puts them on this leaf path. |
| 2375 | static PALETTE_LEAF8_HITS: std::cell::Cell<usize> = const { std::cell::Cell::new(0) }; |
| 2376 | } |
| 2377 | |
| 2378 | /// Current value of [`PALETTE_LEAF8_HITS`]. |
| 2379 | #[allow(dead_code)] // read only from the `#[cfg(test)]` gates |
| 2380 | pub(crate) fn palette_leaf8_hits() -> usize { |
| 2381 | PALETTE_LEAF8_HITS.with(|c| c.get()) |
| 2382 | } |
| 2383 | |
| 2384 | /// Zeroes [`PALETTE_LEAF8_HITS`] (per-attempt gate counting). |
| 2385 | #[allow(dead_code)] // read only from the `#[cfg(test)]` gates |
| 2386 | pub(crate) fn reset_palette_leaf8_hits() { |
| 2387 | PALETTE_LEAF8_HITS.with(|c| c.set(0)); |
| 2388 | } |
| 2389 | |
| 2390 | thread_local! { |
| 2391 | /// lane-pal8 r1: NON-SKIP palette blocks on a superblock-level (64-level) |
| 2392 | /// HORZ/VERT or 1:4 strip ([`decode_block_rect64`]) -- i.e. a palette |
| 2393 | /// block that also carries a real, corner-cropped luma transform. |
| 2394 | static PALETTE_SB_STRIP_HITS: std::cell::Cell<usize> = const { std::cell::Cell::new(0) }; |
| 2395 | } |
| 2396 | |
| 2397 | /// Current value of [`PALETTE_SB_STRIP_HITS`]. |
| 2398 | #[allow(dead_code)] // read only from the `#[cfg(test)]` gates |
| 2399 | pub(crate) fn palette_sb_strip_hits() -> usize { |
| 2400 | PALETTE_SB_STRIP_HITS.with(|c| c.get()) |
| 2401 | } |
| 2402 | |
| 2403 | /// Zeroes [`PALETTE_SB_STRIP_HITS`] (per-attempt gate counting). |
| 2404 | #[allow(dead_code)] // read only from the `#[cfg(test)]` gates |
| 2405 | pub(crate) fn reset_palette_sb_strip_hits() { |
| 2406 | PALETTE_SB_STRIP_HITS.with(|c| c.set(0)); |
| 2407 | } |
| 2408 | |
| 2409 | // lane-t900 r22: palette blocks reconstructed inside an INTER frame (the |
| 2410 | // intra-in-inter path, `decode_inter_block`/`decode_inter_block8`) -- counted |
| 2411 | // apart from [`PALETTE_HITS`] so a witness gate can prove the INTER-frame |
| 2412 | // capability rather than the key frame's, which every screen stream also |
| 2413 | // carries. |
| 2414 | thread_local! { |
| 2415 | static INTRA_IN_INTER_PALETTE_Y_HITS: std::cell::Cell<usize> = const { std::cell::Cell::new(0) }; |
| 2416 | static INTRA_IN_INTER_PALETTE_UV_HITS: std::cell::Cell<usize> = const { std::cell::Cell::new(0) }; |
| 2417 | } |
| 2418 | |
| 2419 | // lane-t900 r23: as [`INTRA_IN_INTER_PALETTE_Y_HITS`], narrowed to the 8x8 |
| 2420 | // LEAF site ([`decode_inter_block8`]'s intra arm) -- the >=16 site fires the |
| 2421 | // shared counter too, so only this one proves a gate really reached the leaf |
| 2422 | // where the refusal used to sit. |
| 2423 | thread_local! { |
| 2424 | static INTER8_PALETTE_HITS: std::cell::Cell<(usize, usize)> = |
| 2425 | const { std::cell::Cell::new((0, 0)) }; |
| 2426 | } |
| 2427 | |
| 2428 | /// Current values of the 8x8-leaf intra-in-inter palette counters, `(y, uv)`. |
| 2429 | pub(crate) fn inter8_palette_hits() -> (usize, usize) { |
| 2430 | INTER8_PALETTE_HITS.with(std::cell::Cell::get) |
| 2431 | } |
| 2432 | |
| 2433 | /// Current values of the two intra-in-inter palette counters, `(y, uv)`. |
| 2434 | pub(crate) fn intra_in_inter_palette_hits() -> (usize, usize) { |
| 2435 | ( |
| 2436 | INTRA_IN_INTER_PALETTE_Y_HITS.with(|c| c.get()), |
| 2437 | INTRA_IN_INTER_PALETTE_UV_HITS.with(|c| c.get()), |
| 2438 | ) |
| 2439 | } |
| 2440 | |
| 2441 | // lane-hgkf r2: SKIPPED intra blocks whose `tx_depth` split the luma transform |
| 2442 | // and which are therefore predicted one transform unit at a time (a skipped |
| 2443 | // block codes no coefficients, so nothing else in the stream witnesses this |
| 2444 | // path -- only the pixels do). |
| 2445 | thread_local! { |
| 2446 | static SKIP_SPLIT_TX_HITS: std::cell::Cell<usize> = const { std::cell::Cell::new(0) }; |
| 2447 | } |
| 2448 | |
| 2449 | /// Current value of [`SKIP_SPLIT_TX_HITS`] (blocks, not units). |
| 2450 | pub fn skip_split_tx_hits() -> usize { |
| 2451 | SKIP_SPLIT_TX_HITS.with(|c| c.get()) |
| 2452 | } |
| 2453 | |
| 2454 | // lane-svt1: SKIPPED square blocks carrying a palette/intrabc prediction whose |
| 2455 | // `tx_depth` split the luma transform -- the arm that predicted DC off the |
| 2456 | // block edges before the fix. Like [`SKIP_SPLIT_TX_HITS`] only the pixels |
| 2457 | // witness this path, so a gate that wants it needs the counter. |
| 2458 | thread_local! { |
| 2459 | static SKIP_SPLIT_TX_OVERRIDE_HITS: std::cell::Cell<usize> = const { std::cell::Cell::new(0) }; |
| 2460 | } |
| 2461 | |
| 2462 | /// Current value of [`SKIP_SPLIT_TX_OVERRIDE_HITS`] (blocks). |
| 2463 | pub fn skip_split_tx_override_hits() -> usize { |
| 2464 | SKIP_SPLIT_TX_OVERRIDE_HITS.with(|c| c.get()) |
| 2465 | } |
| 2466 | |
| 2467 | /// Zeroes [`SKIP_SPLIT_TX_OVERRIDE_HITS`] before a gate's own decode attempt. |
| 2468 | pub fn reset_skip_split_tx_override_hits() { |
| 2469 | SKIP_SPLIT_TX_OVERRIDE_HITS.with(|c| c.set(0)); |
| 2470 | } |
| 2471 | |
| 2472 | // lane-svtinter: palette colour-index maps whose block is cut by the frame's |
| 2473 | // bottom/right mi edge, so spec 5.11.50's `onscreenWidth`/`onscreenHeight` |
| 2474 | // really are smaller than the block and FEWER symbols are read than the |
| 2475 | // block has cells (class map-read-past-frame-edge). |
| 2476 | thread_local! { |
| 2477 | static PALETTE_ONSCREEN_CUT_HITS: std::cell::Cell<usize> = const { std::cell::Cell::new(0) }; |
| 2478 | } |
| 2479 | |
| 2480 | /// Current value of [`PALETTE_ONSCREEN_CUT_HITS`] (colour-index maps). |
| 2481 | pub fn palette_onscreen_cut_hits() -> usize { |
| 2482 | PALETTE_ONSCREEN_CUT_HITS.with(|c| c.get()) |
| 2483 | } |
| 2484 | |
| 2485 | /// Zeroes [`PALETTE_ONSCREEN_CUT_HITS`] before a gate's own decode attempt. |
| 2486 | pub fn reset_palette_onscreen_cut_hits() { |
| 2487 | PALETTE_ONSCREEN_CUT_HITS.with(|c| c.set(0)); |
| 2488 | } |
| 2489 | |
| 2490 | /// Zeroes [`SKIP_SPLIT_TX_HITS`] before a gate's own decode attempt. |
| 2491 | pub fn reset_skip_split_tx_hits() { |
| 2492 | SKIP_SPLIT_TX_HITS.with(|c| c.set(0)); |
| 2493 | } |
| 2494 | |
| 2495 | // lane-intrainter r1: intra blocks inside an INTER frame whose `tx_depth` |
| 2496 | // symbol split their luma transform, bucketed by block side (8, 16, 32, 64) -- |
| 2497 | // the gate's proof that the lifted path actually ran, per size. |
| 2498 | thread_local! { |
| 2499 | static INTRA_IN_INTER_SPLIT_TX_HITS: std::cell::Cell<[usize; 4]> = |
| 2500 | const { std::cell::Cell::new([0; 4]) }; |
| 2501 | } |
| 2502 | |
| 2503 | /// Current value of [`INTRA_IN_INTER_SPLIT_TX_HITS`], indexed 8/16/32/64. |
| 2504 | #[allow(dead_code)] // read only from the `#[cfg(test)]` gates |
| 2505 | pub(crate) fn intra_in_inter_split_tx_hits() -> [usize; 4] { |
| 2506 | INTRA_IN_INTER_SPLIT_TX_HITS.with(std::cell::Cell::get) |
| 2507 | } |
| 2508 | |
| 2509 | /// A just-decoded palette-Y block's own predicted pixels |
| 2510 | /// (`side*side`, row-major, `colors[map[i]]`), consumed by |
| 2511 | /// [`PlaneBuf::reconstruct`]'s next call in place of `predict()` -- the |
| 2512 | /// same off-the-call-stack idiom as [`ENABLE_EDGE_FILTER`] (threading a |
| 2513 | /// per-block override through `read_plane`'s two dozen call sites is the |
| 2514 | /// alternative this avoids). Always empty again once that call consumes |
| 2515 | /// it (`.take()`), so a non-palette block's own `reconstruct` call never |
| 2516 | /// sees stale state. |
| 2517 | |
| 2518 | |
| 2519 | /// Arms the pending [`PALETTE_PRED`] override for the next `reconstruct` |
| 2520 | /// (`set`) / consumes it (`take`). Set and take MUST pair: a buffer armed at a |
| 2521 | /// site whose reconstruct never runs would be picked up by the NEXT block's |
| 2522 | /// reconstruct (class [[refusal-hides-a-defect]] / stale thread-local), so |
| 2523 | /// lane-lossless2: one transform unit's window on a block-sized palette |
| 2524 | /// prediction buffer. A unit reads its prediction at ITS OWN stride, so |
| 2525 | /// handing it the whole-block buffer makes every unit reconstruct the block's |
| 2526 | /// top-left corner (the luma split-transform paths already window; the chroma |
| 2527 | /// ones did not, which is invisible until a lossless frame codes a palette |
| 2528 | /// block as 4x4 chroma units). |
| 2529 | fn palette_window(buf: &[u16], stride: usize, ox: usize, oy: usize, w: usize, h: usize) -> Vec<u16> { |
| 2530 | (0..h) |
| 2531 | .flat_map(|r| buf[(oy + r) * stride + ox..][..w].iter().copied()) |
| 2532 | .collect() |
| 2533 | } |
| 2534 | |
| 2535 | /// `EC_DEBUG_PAL=1` traces every arm and every consumption with its call site. |
| 2536 | #[track_caller] |
| 2537 | fn set_palette_pred(buf: Vec<u16>, fctx: &crate::decode::FrameCtx) { |
| 2538 | if crate::envflags::env_flag!("EC_DEBUG_PAL") { |
| 2539 | let stale = fctx.palette_pred.with(|c| c.borrow().is_some()); |
| 2540 | eprintln!( |
| 2541 | "PALSET len={} stale={stale} at {}", |
| 2542 | buf.len(), |
| 2543 | std::panic::Location::caller() |
| 2544 | ); |
| 2545 | } |
| 2546 | fctx.palette_pred.with(|c| *c.borrow_mut() = Some(buf)); |
| 2547 | } |
| 2548 | |
| 2549 | #[track_caller] |
| 2550 | fn take_palette_pred(fctx: &crate::decode::FrameCtx) -> Option<Vec<u16>> { |
| 2551 | let got = fctx.palette_pred.with(|c| c.borrow_mut().take()); |
| 2552 | if crate::envflags::env_flag!("EC_DEBUG_PAL") { |
| 2553 | eprintln!( |
| 2554 | "PALTAKE len={:?} at {}", |
| 2555 | got.as_ref().map(Vec::len), |
| 2556 | std::panic::Location::caller() |
| 2557 | ); |
| 2558 | } |
| 2559 | got |
| 2560 | } |
| 2561 | |
| 2562 | /// The luma window a `UV_CFL_PRED` chroma block averages its AC signal over |
| 2563 | /// (lane-wave1 step (i)). Parse records the window; [`flush_recon`] reads the |
| 2564 | /// pixels, because at parse time this block's own luma is still queued. |
| 2565 | #[derive(Clone, Copy, Debug)] |
| 2566 | pub(crate) struct CflSrc { |
| 2567 | px: usize, |
| 2568 | py: usize, |
| 2569 | bw: usize, |
| 2570 | bh: usize, |
| 2571 | } |
| 2572 | |
| 2573 | fn cfl_src(px: usize, py: usize, side: usize) -> CflSrc { |
| 2574 | CflSrc { px, py, bw: side, bh: side } |
| 2575 | } |
| 2576 | |
| 2577 | fn cfl_src_rect(px: usize, py: usize, bw: usize, bh: usize) -> CflSrc { |
| 2578 | CflSrc { px, py, bw, bh } |
| 2579 | } |
| 2580 | |
| 2581 | /// lane-wave1 step (i): the pixel-writing half of block decoding, recorded |
| 2582 | /// instead of executed. Each variant is the resolved argument tuple of the |
| 2583 | /// `PlaneBuf::reconstruct*` call it replaces, so replaying the queue in push |
| 2584 | /// order performs exactly the same writes in exactly the same order -- |
| 2585 | /// entropy parsing no longer touches the frame's own planes (except the two |
| 2586 | /// sites named in [`flush_recon`]'s doc), which is what lets step (ii) run |
| 2587 | /// the replay on a superblock-row wavefront worker. |
| 2588 | /// The dequantization + inverse transform of one transform unit, kept as its |
| 2589 | /// INPUTS so that a recorded write carries the work instead of its result |
| 2590 | /// (lane-wave3): at `EC_AV1_RECON_THREADS > 1` the transform then runs on the |
| 2591 | /// recon worker, not on the parsing thread. `stride == 0` means the dense |
| 2592 | /// `w x h` residual; otherwise it is re-laid into the `stride x stride` grid |
| 2593 | /// `reconstruct_mc_rect` reads both of its buffers at. |
| 2594 | #[derive(Clone, Copy)] |
| 2595 | pub(crate) struct TxParams { |
| 2596 | pub(crate) w: usize, |
| 2597 | pub(crate) h: usize, |
| 2598 | pub(crate) bit_depth: u8, |
| 2599 | pub(crate) q_idx: i32, |
| 2600 | pub(crate) dc_delta: i32, |
| 2601 | pub(crate) ac_delta: i32, |
| 2602 | pub(crate) tx_type: TxType, |
| 2603 | /// lane-av1-qmatrix: this unit's inverse quantisation matrix, selected |
| 2604 | /// on the parse thread by [`block_iqmatrix`] and captured here so the |
| 2605 | /// recon worker scales identically without touching frame state. |
| 2606 | pub(crate) qm: Option<crate::qm::Iqm>, |
| 2607 | pub(crate) stride: usize, |
| 2608 | /// lane-lossless: this transform unit belongs to a lossless BLOCK (its |
| 2609 | /// segment's `xd->lossless[segment_id]` bit), so the residual comes off |
| 2610 | /// the 4x4 Walsh-Hadamard |
| 2611 | /// ([`crate::transform::dequant_and_inverse_wht4x4`]) instead of the |
| 2612 | /// DCT/ADST network (libaom `inv_txfm_add`, `av1/common/idct.c`). |
| 2613 | pub(crate) lossless: bool, |
| 2614 | } |
| 2615 | |
| 2616 | /// lane-av1mixloss: Walsh-Hadamard transform units actually reconstructed. |
| 2617 | /// The mixed-lossless witness's proof the per-segment lossless path really |
| 2618 | /// ran on a frame that is NOT `CodedLossless`. Atomic because the wavefront |
| 2619 | /// workers run this on their own threads. |
| 2620 | static LOSSLESS_WHT_UNITS: std::sync::atomic::AtomicUsize = |
| 2621 | std::sync::atomic::AtomicUsize::new(0); |
| 2622 | |
| 2623 | /// [`LOSSLESS_WHT_UNITS`] -- total over the process, read as a before/after |
| 2624 | /// delta around one decode. |
| 2625 | #[allow(dead_code)] // read only from the `#[cfg(test)]` gates |
| 2626 | pub(crate) fn lossless_wht_units() -> usize { |
| 2627 | LOSSLESS_WHT_UNITS.load(std::sync::atomic::Ordering::Relaxed) |
| 2628 | } |
| 2629 | |
| 2630 | impl TxParams { |
| 2631 | /// The one copy of the produce-the-residual code: the inline path calls it |
| 2632 | /// on the parse thread, the wavefront worker calls it on its own, so both |
| 2633 | /// are the same function on the same inputs and byte-identical. |
| 2634 | fn run(self, grid: &[i32]) -> Vec<i32> { |
| 2635 | let residual = if self.lossless { |
| 2636 | LOSSLESS_WHT_UNITS.fetch_add(1, std::sync::atomic::Ordering::Relaxed); |
| 2637 | debug_assert_eq!((self.w, self.h), (4, 4)); |
| 2638 | debug_assert_eq!(self.tx_type, TxType::DctDct); |
| 2639 | crate::transform::dequant_and_inverse_wht4x4( |
| 2640 | grid, self.bit_depth, self.q_idx, self.dc_delta, self.ac_delta, |
| 2641 | ) |
| 2642 | } else { |
| 2643 | dequant_and_inverse_typed_wh( |
| 2644 | grid, self.w, self.h, self.bit_depth, self.q_idx, self.dc_delta, self.ac_delta, |
| 2645 | self.tx_type, |
| 2646 | self.qm, |
| 2647 | ) |
| 2648 | }; |
| 2649 | // An all-zero grid transforms to the empty marker, and a strided copy |
| 2650 | // of zeros is still zeros: the marker passes straight through. |
| 2651 | if residual.is_empty() { |
| 2652 | return residual; |
| 2653 | } |
| 2654 | if self.stride == 0 { |
| 2655 | return residual; |
| 2656 | } |
| 2657 | let mut strided = vec![0i32; self.stride * self.stride]; |
| 2658 | for row in 0..self.h { |
| 2659 | strided[row * self.stride..][..self.w] |
| 2660 | .copy_from_slice(&residual[row * self.w..][..self.w]); |
| 2661 | } |
| 2662 | strided |
| 2663 | } |
| 2664 | } |
| 2665 | |
| 2666 | /// A recorded write's residual: already transformed, or the coefficients and |
| 2667 | /// [`TxParams`] the worker transforms itself. |
| 2668 | pub(crate) enum Residual { |
| 2669 | /// An already-transformed residual in the arena, `(offset, len)`; a zero |
| 2670 | /// `len` is the all-zero marker [`dense_residual`] expands. |
| 2671 | Done(u32, u32), |
| 2672 | /// The coefficients live in the pushing tile's arena |
| 2673 | /// ([`crate::decode::FrameCtx::coeff_arena`]); this is their `(offset, |
| 2674 | /// len)` in it. |
| 2675 | Coeffs(TxParams, u32, u32), |
| 2676 | } |
| 2677 | |
| 2678 | impl Residual { |
| 2679 | /// The record for a transform unit whose coefficient grid is all zero: |
| 2680 | /// the empty marker, so neither the grid nor the residual is cloned and |
| 2681 | /// the worker never runs the transform (60% of the 4K segment's units). |
| 2682 | fn coeffs(tx: TxParams, grid: &[i32], fctx: &crate::decode::FrameCtx) -> Self { |
| 2683 | if is_zero_grid(grid) { |
| 2684 | return Self::Done(0, 0); |
| 2685 | } |
| 2686 | Self::Coeffs(tx, push_coeffs(grid, fctx), grid.len() as u32) |
| 2687 | } |
| 2688 | |
| 2689 | /// A residual the parse thread already transformed, kept in the arena |
| 2690 | /// instead of its own `Vec`. |
| 2691 | fn done(residual: &[i32], fctx: &crate::decode::FrameCtx) -> Self { |
| 2692 | // lane-blockmem: a skipped block hands over [`ZERO_RESIDUAL`] itself, |
| 2693 | // which is exactly the all-zero marker -- one pointer compare instead |
| 2694 | // of copying up to 64 KB of zeros into the arena per block. |
| 2695 | if std::ptr::eq(residual.as_ptr(), ZERO_RESIDUAL.as_ptr()) { |
| 2696 | return Self::Done(0, 0); |
| 2697 | } |
| 2698 | Self::Done(push_coeffs(residual, fctx), residual.len() as u32) |
| 2699 | } |
| 2700 | |
| 2701 | /// The residual to reconstruct with; `scratch` is where a transform run |
| 2702 | /// on this thread lands, so the caller owns that buffer. |
| 2703 | fn resolve<'a>(self, arena: &'a [i32], scratch: &'a mut Vec<i32>) -> &'a [i32] { |
| 2704 | match self { |
| 2705 | Self::Done(off, len) => &arena[off as usize..][..len as usize], |
| 2706 | Self::Coeffs(p, off, len) => { |
| 2707 | *scratch = p.run(&arena[off as usize..][..len as usize]); |
| 2708 | scratch |
| 2709 | } |
| 2710 | } |
| 2711 | } |
| 2712 | } |
| 2713 | |
| 2714 | /// Appends to the tile's coefficient arena, returning the offset the record |
| 2715 | /// carries; see [`crate::decode::FrameCtx::coeff_arena`]. |
| 2716 | fn push_coeffs(v: &[i32], fctx: &crate::decode::FrameCtx) -> u32 { |
| 2717 | fctx.coeff_arena.with(|a| { |
| 2718 | let mut a = a.borrow_mut(); |
| 2719 | let off = a.len(); |
| 2720 | a.extend_from_slice(v); |
| 2721 | off as u32 |
| 2722 | }) |
| 2723 | } |
| 2724 | |
| 2725 | /// [`push_coeffs`] for a recorded prediction. |
| 2726 | fn push_pred_samples(v: &[u16], fctx: &crate::decode::FrameCtx) -> u32 { |
| 2727 | fctx.pred_arena.with(|a| { |
| 2728 | let mut a = a.borrow_mut(); |
| 2729 | let off = a.len(); |
| 2730 | a.extend_from_slice(v); |
| 2731 | off as u32 |
| 2732 | }) |
| 2733 | } |
| 2734 | |
| 2735 | /// An all-zero residual, for the transform units that code no coefficient: |
| 2736 | /// [`crate::transform::dequant_and_inverse_typed_wh`] returns those EMPTY, |
| 2737 | /// and the reconstruction loops below read this instead of a per-unit |
| 2738 | /// `vec![0; w * h]`. Sized for the largest stride a recorded unit can carry |
| 2739 | /// (a 128-wide inter block's prediction stride). |
| 2740 | pub(crate) static ZERO_RESIDUAL: [i32; 128 * 128] = [0; 128 * 128]; |
| 2741 | |
| 2742 | /// lane-blockscratch: one plane's coefficient levels as the block-tail |
| 2743 | /// neighbour writes want them. A plane that coded nothing (a skipped block, a |
| 2744 | /// strip with no chroma, luma under a var-tx split whose units already wrote |
| 2745 | /// themselves) names the shared [`ZERO_RESIDUAL`] instead of allocating and |
| 2746 | /// zeroing up to 64 KB of its own per block. |
| 2747 | #[derive(Clone)] |
| 2748 | enum Grid { |
| 2749 | Zero(usize), |
| 2750 | Own(Vec<i32>), |
| 2751 | } |
| 2752 | |
| 2753 | /// lane-blockscratch: the mu-chunk chroma collector, allocated by the first |
| 2754 | /// unit that writes into it. Only a block wider or taller than 64 px codes its |
| 2755 | /// chroma per 64x64 chunk; every smaller block used to allocate and zero both |
| 2756 | /// collectors per block and then drop them untouched. |
| 2757 | fn mu_units(v: &mut Vec<i32>, n: usize) -> &mut Vec<i32> { |
| 2758 | if v.is_empty() { |
| 2759 | *v = vec![0i32; n]; |
| 2760 | } |
| 2761 | v |
| 2762 | } |
| 2763 | |
| 2764 | impl std::ops::Deref for Grid { |
| 2765 | type Target = [i32]; |
| 2766 | fn deref(&self) -> &[i32] { |
| 2767 | match self { |
| 2768 | Grid::Zero(n) => &ZERO_RESIDUAL[..*n], |
| 2769 | Grid::Own(v) => v, |
| 2770 | } |
| 2771 | } |
| 2772 | } |
| 2773 | |
| 2774 | /// Whether a coefficient grid codes nothing: a [`Grid::Zero`] unit IS the |
| 2775 | /// shared [`ZERO_RESIDUAL`], so one pointer compare answers the 60% of |
| 2776 | /// transform units that code no coefficient without the scan (lane-coefgrid). |
| 2777 | pub(crate) fn is_zero_grid(grid: &[i32]) -> bool { |
| 2778 | std::ptr::eq(grid.as_ptr(), ZERO_RESIDUAL.as_ptr()) || grid.iter().all(|&c| c == 0) |
| 2779 | } |
| 2780 | |
| 2781 | /// `residual`, or `n` zeros when it is the empty all-zero marker. |
| 2782 | fn dense_residual(residual: &[i32], n: usize) -> &[i32] { |
| 2783 | if residual.is_empty() { &ZERO_RESIDUAL[..n] } else { residual } |
| 2784 | } |
| 2785 | |
| 2786 | /// Where a recorded motion-compensated write reads its prediction from. |
| 2787 | /// |
| 2788 | /// lane-wave4: a block's whole-block prediction (mc + warp + interintra) is |
| 2789 | /// the parse thread's single biggest reconstruction cost, so at |
| 2790 | /// `EC_AV1_RECON_THREADS > 1` it is not built at parse at all: the block |
| 2791 | /// pushes a [`ReconOp::Pred`] carrying the build as a closure, the worker |
| 2792 | /// runs it into its own three-plane slot, and every transform unit of that |
| 2793 | /// block then reads its window out of that slot ([`Pred::Cur`]). |
| 2794 | #[derive(Clone, Copy)] |
| 2795 | pub(crate) enum Pred<'a> { |
| 2796 | /// A prediction the parse thread already built, at its own stride |
| 2797 | /// (`stride == 0` means "the reader's own `stride` argument"). |
| 2798 | Inline(&'a [u16], usize), |
| 2799 | /// The current block's deferred prediction: plane, its stride, and this |
| 2800 | /// unit's offset into it. |
| 2801 | Cur { plane: usize, stride: usize, off: usize }, |
| 2802 | } |
| 2803 | |
| 2804 | impl<'a> Pred<'a> { |
| 2805 | /// The same prediction read at an explicit stride. |
| 2806 | fn view(self, stride: usize) -> Self { |
| 2807 | match self { |
| 2808 | Self::Inline(b, _) => Self::Inline(b, stride), |
| 2809 | Self::Cur { plane, off, .. } => Self::Cur { plane, stride, off }, |
| 2810 | } |
| 2811 | } |
| 2812 | /// The materialised buffer and the stride to read it at (`stride` is the |
| 2813 | /// reader's own when this prediction was built dense); `None` for a |
| 2814 | /// deferred prediction, which only a recorded op can carry. |
| 2815 | fn inline(self, stride: usize) -> Option<(&'a [u16], usize)> { |
| 2816 | match self { |
| 2817 | Self::Inline(b, s) => Some((b, if s == 0 { stride } else { s })), |
| 2818 | Self::Cur { .. } => None, |
| 2819 | } |
| 2820 | } |
| 2821 | /// This prediction's window starting `n` samples in. |
| 2822 | fn offset(self, n: usize) -> Self { |
| 2823 | match self { |
| 2824 | Self::Inline(b, s) => Self::Inline(&b[n..], s), |
| 2825 | Self::Cur { plane, stride, off } => Self::Cur { plane, stride, off: off + n }, |
| 2826 | } |
| 2827 | } |
| 2828 | } |
| 2829 | |
| 2830 | /// [`Pred`], resolved into what a recorded op carries. |
| 2831 | pub(crate) enum PredSrc { |
| 2832 | /// A prediction window in the tile's prediction arena, `(offset, len)`. |
| 2833 | Owned(u32, u32), |
| 2834 | /// A window of the block prediction: its plane, its offset, and the |
| 2835 | /// stride it is read at -- which is the block's, not necessarily the |
| 2836 | /// op's (a transform unit's residual stays dense, so the worker takes |
| 2837 | /// its window out of the block prediction itself). |
| 2838 | Cur { plane: usize, off: usize, src: usize }, |
| 2839 | } |
| 2840 | |
| 2841 | /// One block's whole-block prediction build, deferred to a recon worker. |
| 2842 | /// |
| 2843 | /// The closure borrows the tile's reference pictures and its own frame-level |
| 2844 | /// tables; [`WaveGuard`] joins every worker before the tile decoder returns, |
| 2845 | /// so those borrows outlive it -- the [`ReconPlanes`] trade-off, stated once |
| 2846 | /// here for the closure's `'static`/`Send` laundering in [`push_pred`]. |
| 2847 | pub(crate) struct PredBuild( |
| 2848 | Box< |
| 2849 | dyn FnOnce( |
| 2850 | &mut PlaneBuf<'static>, |
| 2851 | &mut PlaneBuf<'static>, |
| 2852 | &mut PlaneBuf<'static>, |
| 2853 | &crate::decode::FrameCtx, |
| 2854 | // lane-blockmem: the previous block's three prediction buffers, |
| 2855 | // refilled in place -- the build used to return three fresh |
| 2856 | // `Vec`s per block and drop these. |
| 2857 | &mut [Vec<u16>; 3], |
| 2858 | ), |
| 2859 | >, |
| 2860 | ); |
| 2861 | |
| 2862 | // SAFETY: see [`PredBuild`] -- the captured borrows are read-only reference |
| 2863 | // pictures and frame tables, and the closure runs on exactly one worker. |
| 2864 | #[allow(unsafe_code)] |
| 2865 | unsafe impl Send for PredBuild {} |
| 2866 | |
| 2867 | /// `true` when this tile's writes are recorded for the wavefront instead of |
| 2868 | /// performed at the push site, i.e. when a prediction may be deferred. |
| 2869 | fn recon_deferred(fctx: &crate::decode::FrameCtx) -> bool { |
| 2870 | fctx.recon_planes.with(std::cell::Cell::get).is_none() |
| 2871 | } |
| 2872 | |
| 2873 | /// The previous block's prediction buffer, cleared and zero-filled to `n` |
| 2874 | /// (lane-blockmem): the same bytes the `vec![0u16; n]` it replaces produced, |
| 2875 | /// without the per-block allocation and free. |
| 2876 | fn refill(mut v: Vec<u16>, n: usize) -> Vec<u16> { |
| 2877 | // A buffer that has to grow anyway is cheaper allocated zeroed (`calloc`) |
| 2878 | // than resized element by element -- which is every block on the inline |
| 2879 | // path, where there is no previous buffer to reuse. |
| 2880 | if v.capacity() < n { |
| 2881 | return vec![0u16; n]; |
| 2882 | } |
| 2883 | v.clear(); |
| 2884 | v.resize(n, 0); |
| 2885 | v |
| 2886 | } |
| 2887 | |
| 2888 | /// Records one block's prediction build for the worker that reconstructs it. |
| 2889 | #[allow(unsafe_code)] |
| 2890 | fn push_pred<'a>( |
| 2891 | fctx: &crate::decode::FrameCtx, |
| 2892 | f: impl FnOnce( |
| 2893 | &mut PlaneBuf<'static>, |
| 2894 | &mut PlaneBuf<'static>, |
| 2895 | &mut PlaneBuf<'static>, |
| 2896 | &crate::decode::FrameCtx, |
| 2897 | &mut [Vec<u16>; 3], |
| 2898 | ) + 'a, |
| 2899 | ) { |
| 2900 | type Dyn<'x> = dyn FnOnce( |
| 2901 | &mut PlaneBuf<'static>, |
| 2902 | &mut PlaneBuf<'static>, |
| 2903 | &mut PlaneBuf<'static>, |
| 2904 | &crate::decode::FrameCtx, |
| 2905 | &mut [Vec<u16>; 3], |
| 2906 | ) + 'x; |
| 2907 | let boxed: Box<Dyn<'a>> = Box::new(f); |
| 2908 | // SAFETY: see [`PredBuild`] -- every worker is joined by [`WaveGuard`] |
| 2909 | // before the borrows the closure holds go out of scope. |
| 2910 | let boxed: Box<Dyn<'static>> = unsafe { std::mem::transmute(boxed) }; |
| 2911 | push_op(fctx, ReconOp::Pred(PredBuild(boxed))); |
| 2912 | } |
| 2913 | |
| 2914 | /// One superblock's recorded writes plus the two arenas they index -- the |
| 2915 | /// unit a wavefront worker takes off the queue (lane-recoparse). |
| 2916 | pub(crate) struct ReconJob { |
| 2917 | ops: Vec<ReconOp>, |
| 2918 | coeffs: Vec<i32>, |
| 2919 | preds: Vec<u16>, |
| 2920 | } |
| 2921 | |
| 2922 | pub(crate) enum ReconOp { |
| 2923 | /// lane-wave4: build this block's prediction into the worker's slot. |
| 2924 | Pred(PredBuild), |
| 2925 | Intra { |
| 2926 | plane: usize, |
| 2927 | x: usize, |
| 2928 | y: usize, |
| 2929 | bw: usize, |
| 2930 | bh: usize, |
| 2931 | /// `true` when the parse site called `reconstruct_rect` (whose |
| 2932 | /// `edges_rect` differs from `edges` even at `bw == bh`). |
| 2933 | rect: bool, |
| 2934 | mode: usize, |
| 2935 | angle_delta: i32, |
| 2936 | reach: Reach, |
| 2937 | residual: Residual, |
| 2938 | cfl: Option<(i32, CflSrc)>, |
| 2939 | filter_intra: Option<usize>, |
| 2940 | smooth_neighbor: bool, |
| 2941 | /// The palette/intrabc prediction override, taken off |
| 2942 | /// [`FrameCtx::palette_pred`] at PUSH time (parse order), never read |
| 2943 | /// from that slot by the replay. |
| 2944 | pred: Option<Vec<u16>>, |
| 2945 | }, |
| 2946 | Mc { |
| 2947 | plane: usize, |
| 2948 | x: usize, |
| 2949 | y: usize, |
| 2950 | stride: usize, |
| 2951 | w: usize, |
| 2952 | h: usize, |
| 2953 | prediction: PredSrc, |
| 2954 | residual: Residual, |
| 2955 | }, |
| 2956 | } |
| 2957 | |
| 2958 | /// The three planes of the tile currently being decoded, as raw pointers. |
| 2959 | /// |
| 2960 | /// corner-cut: at one reconstruction thread a recorded write must cost |
| 2961 | /// nothing -- deferring it to the end of the superblock is +1.2% |
| 2962 | /// instructions and +4.5% wall on 4K, because the residual is written at |
| 2963 | /// parse and read back cache-cold at replay. So the recorded op is performed |
| 2964 | /// at the push site instead, through the pointers the tile decoder registers |
| 2965 | /// once ([`ReconInline`]), which is the same thread, the same order and the |
| 2966 | /// same writes as before lane-wave1 -- but the `&mut PlaneBuf` the parse |
| 2967 | /// stack still holds aliases them, exactly the [`crate::par::Shared`] |
| 2968 | /// trade-off (Miri's stacked borrows would flag both). The upgrade path is |
| 2969 | /// threading the three planes down every `read_plane`/`read_inter_plane` |
| 2970 | /// call site (~40) so the push site owns the borrow it writes through. |
| 2971 | #[derive(Clone, Copy)] |
| 2972 | pub(crate) struct ReconPlanes( |
| 2973 | *mut PlaneBuf<'static>, |
| 2974 | *mut PlaneBuf<'static>, |
| 2975 | *mut PlaneBuf<'static>, |
| 2976 | ); |
| 2977 | |
| 2978 | // SAFETY: a `FrameCtx` sent to another thread (a frame worker, or |
| 2979 | // [`filter_ctx_copy`]) is always one whose guard is not installed, so the |
| 2980 | // value travelling is `None`; the pointers never leave the tile decoder's |
| 2981 | // own thread. |
| 2982 | #[allow(unsafe_code)] |
| 2983 | unsafe impl Send for ReconPlanes {} |
| 2984 | // SAFETY: the wavefront's workers share one `ReconPlanes` and write provably |
| 2985 | // disjoint superblocks of it (see [`WaveState`]) -- the [`crate::par::Shared`] |
| 2986 | // contract, one superblock per band instead of one row band. |
| 2987 | #[allow(unsafe_code)] |
| 2988 | unsafe impl Sync for ReconPlanes {} |
| 2989 | |
| 2990 | /// Registers the tile's planes for inline reconstruction and unregisters them |
| 2991 | /// on every exit from the tile decoder, including the `?` ones -- a stale |
| 2992 | /// pointer here would outlive the planes, since [`FrameCtx`] outlives the |
| 2993 | /// tile. |
| 2994 | pub(crate) struct ReconInline<'a>(&'a crate::decode::FrameCtx); |
| 2995 | |
| 2996 | #[allow(unsafe_code)] |
| 2997 | impl<'a> ReconInline<'a> { |
| 2998 | /// Installs the guard for this tile when reconstruction is inline |
| 2999 | /// (`EC_AV1_RECON_THREADS == 1`, the default); `None` hands every write |
| 3000 | /// to the queue instead, for the wavefront workers to replay. |
| 3001 | fn install( |
| 3002 | fctx: &'a crate::decode::FrameCtx, |
| 3003 | y: &mut PlaneBuf<'static>, |
| 3004 | u: &mut PlaneBuf<'static>, |
| 3005 | v: &mut PlaneBuf<'static>, |
| 3006 | ) -> Option<Self> { |
| 3007 | if crate::par::recon_threads() != 1 { |
| 3008 | return None; |
| 3009 | } |
| 3010 | // SAFETY: the planes are locals of the tile decoder and outlive the |
| 3011 | // guard, which is dropped on every exit from it (including `?`); see |
| 3012 | // [`ReconPlanes`] for the aliasing trade-off. |
| 3013 | let planes = ReconPlanes( |
| 3014 | std::ptr::from_mut(y), |
| 3015 | std::ptr::from_mut(u), |
| 3016 | std::ptr::from_mut(v), |
| 3017 | ); |
| 3018 | fctx.recon_planes.with(|c| c.set(Some(planes))); |
| 3019 | Some(Self(fctx)) |
| 3020 | } |
| 3021 | } |
| 3022 | |
| 3023 | impl Drop for ReconInline<'_> { |
| 3024 | fn drop(&mut self) { |
| 3025 | self.0.recon_planes.with(|c| c.set(None)); |
| 3026 | } |
| 3027 | } |
| 3028 | |
| 3029 | #[allow(unsafe_code)] |
| 3030 | fn push_op(fctx: &crate::decode::FrameCtx, op: ReconOp) { |
| 3031 | if let Some(ReconPlanes(y, u, v)) = fctx.recon_planes.with(std::cell::Cell::get) { |
| 3032 | // SAFETY: see [`ReconPlanes`] -- one thread, and the write is the one |
| 3033 | // the parse site would have performed itself before lane-wave1. |
| 3034 | // The inline path never records coefficients (the transform runs at |
| 3035 | // the push site), so the arena is not read here. |
| 3036 | unsafe { |
| 3037 | exec_op(op, &mut *y, &mut *u, &mut *v, &mut Default::default(), &[], &[], fctx) |
| 3038 | }; |
| 3039 | return; |
| 3040 | } |
| 3041 | fctx.recon_ops.with(|c| c.borrow_mut().push(op)); |
| 3042 | } |
| 3043 | |
| 3044 | /// lane-wave1 step (ii): the superblock-row wavefront a tile's recorded |
| 3045 | /// writes are reconstructed on when `EC_AV1_RECON_THREADS > 1`. |
| 3046 | /// |
| 3047 | /// The parser keeps parsing and never touches the planes; each superblock's |
| 3048 | /// drained `Vec<ReconOp>` becomes one job, keyed by the superblock's own |
| 3049 | /// `(row, column)` inside the tile (128x128 superblocks are one job for all |
| 3050 | /// four of their 64x64 visits, since a 128 block writes across quadrants). |
| 3051 | /// A worker may start job `(r, c)` once `(r - 1, min(c + 1, last_col))` and |
| 3052 | /// `(r, c - 1)` are complete -- the classic wavefront rule, which covers |
| 3053 | /// every pixel an intra predictor can read (its edges reach one block right |
| 3054 | /// along the row above and one block down the column to the left, both |
| 3055 | /// inside those two neighbours) and every CfL AC read (this block's own |
| 3056 | /// luma, written by this same job). Jobs are handed out in raster order, so |
| 3057 | /// the oldest outstanding one always has its dependencies met: no deadlock. |
| 3058 | /// |
| 3059 | /// Gate counters incremented inside `reconstruct*`/`intra`/`mc` are |
| 3060 | /// `thread_local!` and miss every worker, which is why the default is 1. |
| 3061 | pub(crate) struct WaveState { |
| 3062 | q: std::sync::Mutex<WaveQueue>, |
| 3063 | cv: std::sync::Condvar, |
| 3064 | planes: ReconPlanes, |
| 3065 | /// The two [`FrameCtx`] fields reconstruction reads (`sample_max` is |
| 3066 | /// `bit_depth`'s; everything else a `reconstruct*` needs is in the op). |
| 3067 | bit_depth: u8, |
| 3068 | enable_edge_filter: bool, |
| 3069 | last_col: usize, |
| 3070 | } |
| 3071 | |
| 3072 | struct WaveQueue { |
| 3073 | /// Queued jobs per superblock ROW, in column order (lane-wave5). Only a |
| 3074 | /// row's front job can ever be runnable -- a job waits on its own left |
| 3075 | /// neighbour -- so a worker takes the lowest row whose front job's |
| 3076 | /// dependencies are met. Popping the raster-oldest job instead (what |
| 3077 | /// this replaced) put every worker on the SAME row, where the wavefront |
| 3078 | /// rule serialises them, which is why 4 workers ran the tile at 1.2x. |
| 3079 | rows: Vec<std::collections::VecDeque<(usize, ReconJob)>>, |
| 3080 | /// Jobs sitting in `rows`, so a worker knows the queue is empty without |
| 3081 | /// walking it. |
| 3082 | queued: usize, |
| 3083 | /// Per superblock row, the last column reconstructed (-1 = none yet). |
| 3084 | done: Vec<isize>, |
| 3085 | /// Per superblock row, the last column PARSED (submitted, whether or not |
| 3086 | /// it recorded any write) and the last column actually pushed as a job |
| 3087 | /// (-1 = none). A row is fully reconstructed once it has been parsed to |
| 3088 | /// `last_col` and `done` has caught up with `last_push` -- lane-pipefilt |
| 3089 | /// reads that to release the row's filters (see [`wave_rows_complete`]). |
| 3090 | last_sub: Vec<isize>, |
| 3091 | last_push: Vec<isize>, |
| 3092 | /// Jobs popped but not finished -- what [`wave_barrier`] waits out. |
| 3093 | active: usize, |
| 3094 | closed: bool, |
| 3095 | /// lane-sballoc: the three per-superblock buffers a finished job hands |
| 3096 | /// back, so [`wave_submit`] takes a warm allocation (capacity kept, no |
| 3097 | /// first-touch page faults) instead of the `Vec::with_capacity` it used |
| 3098 | /// to make per superblock -- 3 allocations and 3 frees per superblock, |
| 3099 | /// 4680 per 4K frame. Bounded at `recon_threads + 2` sets, and it lives |
| 3100 | /// inside the queue's own mutex, which both sides already take. |
| 3101 | free: Vec<JobBufs>, |
| 3102 | free_cap: usize, |
| 3103 | } |
| 3104 | |
| 3105 | /// One recycled set of [`ReconJob`] buffers (lane-sballoc). |
| 3106 | type JobBufs = (Vec<ReconOp>, Vec<i32>, Vec<u16>); |
| 3107 | |
| 3108 | impl WaveQueue { |
| 3109 | /// The lowest-row queued job whose wavefront dependencies are satisfied, |
| 3110 | /// or `None` (with the blocking condition counted when instrumented). |
| 3111 | fn take_runnable( |
| 3112 | &mut self, |
| 3113 | last_col: usize, |
| 3114 | stats: bool, |
| 3115 | ) -> Option<(usize, usize, ReconJob)> { |
| 3116 | let mut blocked: Option<bool> = None; |
| 3117 | for r in 0..self.rows.len() { |
| 3118 | let Some(front) = self.rows[r].front() else { |
| 3119 | continue; |
| 3120 | }; |
| 3121 | let c = front.0; |
| 3122 | let above = r == 0 || self.done[r - 1] >= (c + 1).min(last_col) as isize; |
| 3123 | let left = c == 0 || self.done[r] >= c as isize - 1; |
| 3124 | if above && left { |
| 3125 | let (c, job) = self.rows[r].pop_front().expect("front checked above"); |
| 3126 | self.queued -= 1; |
| 3127 | return Some((r, c, job)); |
| 3128 | } |
| 3129 | if blocked.is_none() { |
| 3130 | blocked = Some(above); |
| 3131 | } |
| 3132 | } |
| 3133 | if stats { |
| 3134 | match blocked { |
| 3135 | Some(true) => WS_BLOCK_LEFT.fetch_add(1, Relaxed), |
| 3136 | Some(false) => WS_BLOCK_ABOVE.fetch_add(1, Relaxed), |
| 3137 | None => 0, |
| 3138 | }; |
| 3139 | } |
| 3140 | None |
| 3141 | } |
| 3142 | } |
| 3143 | |
| 3144 | /// lane-wave5 instrumentation, on `EC_AV1_WAVE_STATS=1`: where a recon |
| 3145 | /// worker's time goes (busy replaying, waiting for a dependency, idle on an |
| 3146 | /// empty queue), how far ahead the parse thread is, and how often the two |
| 3147 | /// parse-time pixel reads barrier the wavefront. Cumulative for the process, |
| 3148 | /// dumped by every [`WaveGuard`] drop, so the LAST line of a decode is the |
| 3149 | /// whole run. |
| 3150 | const WS_SLOTS: usize = 16; |
| 3151 | use std::sync::atomic::AtomicU64; |
| 3152 | use std::sync::atomic::Ordering::Relaxed; |
| 3153 | static WS_JOBS: [AtomicU64; WS_SLOTS] = [const { AtomicU64::new(0) }; WS_SLOTS]; |
| 3154 | static WS_OPS: [AtomicU64; WS_SLOTS] = [const { AtomicU64::new(0) }; WS_SLOTS]; |
| 3155 | static WS_BUSY: [AtomicU64; WS_SLOTS] = [const { AtomicU64::new(0) }; WS_SLOTS]; |
| 3156 | static WS_WAIT: [AtomicU64; WS_SLOTS] = [const { AtomicU64::new(0) }; WS_SLOTS]; |
| 3157 | static WS_BLOCK_ABOVE: AtomicU64 = AtomicU64::new(0); |
| 3158 | static WS_BLOCK_LEFT: AtomicU64 = AtomicU64::new(0); |
| 3159 | /// Parse-thread time between two submits, i.e. everything parse still does |
| 3160 | /// itself (symbol decode plus the predictions not deferred). |
| 3161 | static WS_PARSE: AtomicU64 = AtomicU64::new(0); |
| 3162 | /// Parse-thread time spent waiting for the wavefront: at [`wave_barrier`] |
| 3163 | /// (the intrabc/interintra reads) and at the tile's end. |
| 3164 | static WS_BARRIER: AtomicU64 = AtomicU64::new(0); |
| 3165 | static WS_BARRIERS: AtomicU64 = AtomicU64::new(0); |
| 3166 | static WS_DRAIN: AtomicU64 = AtomicU64::new(0); |
| 3167 | static WS_TILES: AtomicU64 = AtomicU64::new(0); |
| 3168 | |
| 3169 | fn wave_stats() -> bool { |
| 3170 | static ON: std::sync::atomic::AtomicU8 = std::sync::atomic::AtomicU8::new(0); |
| 3171 | match ON.load(Relaxed) { |
| 3172 | 0 => { |
| 3173 | let on = crate::envflags::var("EC_AV1_WAVE_STATS") |
| 3174 | .map(|v| v.trim() == "1") |
| 3175 | .unwrap_or(false); |
| 3176 | ON.store(1 + u8::from(on), Relaxed); |
| 3177 | on |
| 3178 | } |
| 3179 | n => n == 2, |
| 3180 | } |
| 3181 | } |
| 3182 | |
| 3183 | fn wave_stats_dump() { |
| 3184 | let ms = |n: u64| n as f64 / 1e6; |
| 3185 | let mut s = format!("wave_stats tiles={}", WS_TILES.load(Relaxed)); |
| 3186 | for i in 0..WS_SLOTS { |
| 3187 | let jobs = WS_JOBS[i].load(Relaxed); |
| 3188 | if jobs == 0 { |
| 3189 | continue; |
| 3190 | } |
| 3191 | s += &format!( |
| 3192 | " | w{i} jobs={jobs} ops/job={:.1} busy={:.0}ms wait={:.0}ms wait/job={:.0}us", |
| 3193 | WS_OPS[i].load(Relaxed) as f64 / jobs as f64, |
| 3194 | ms(WS_BUSY[i].load(Relaxed)), |
| 3195 | ms(WS_WAIT[i].load(Relaxed)), |
| 3196 | WS_WAIT[i].load(Relaxed) as f64 / jobs as f64 / 1e3, |
| 3197 | ); |
| 3198 | } |
| 3199 | s += &format!( |
| 3200 | " | pipe={}/{}", |
| 3201 | PIPE_FRAMES.load(Relaxed), |
| 3202 | PIPE_FRAMES.load(Relaxed) + PIPE_WHOLE.load(Relaxed), |
| 3203 | ); |
| 3204 | s += &format!( |
| 3205 | " | parse={:.0}ms barrier={:.0}ms/{} drain={:.0}ms block_above={} block_left={}", |
| 3206 | ms(WS_PARSE.load(Relaxed)), |
| 3207 | ms(WS_BARRIER.load(Relaxed)), |
| 3208 | WS_BARRIERS.load(Relaxed), |
| 3209 | ms(WS_DRAIN.load(Relaxed)), |
| 3210 | WS_BLOCK_ABOVE.load(Relaxed), |
| 3211 | WS_BLOCK_LEFT.load(Relaxed), |
| 3212 | ); |
| 3213 | eprintln!("{s}"); |
| 3214 | } |
| 3215 | |
| 3216 | #[allow(unsafe_code)] |
| 3217 | fn wave_worker(st: &WaveState, idx: usize) { |
| 3218 | let fctx = FrameCtx::new(); |
| 3219 | fctx.bit_depth.set(st.bit_depth); |
| 3220 | fctx.enable_edge_filter.set(st.enable_edge_filter); |
| 3221 | let stats = wave_stats(); |
| 3222 | let slot = idx.min(WS_SLOTS - 1); |
| 3223 | loop { |
| 3224 | let t0 = std::time::Instant::now(); |
| 3225 | let mut q = st.q.lock().unwrap(); |
| 3226 | let (r, c, job) = loop { |
| 3227 | if let Some(job) = q.take_runnable(st.last_col, stats) { |
| 3228 | break job; |
| 3229 | } |
| 3230 | if q.closed && q.queued == 0 { |
| 3231 | return; |
| 3232 | } |
| 3233 | q = st.cv.wait(q).unwrap(); |
| 3234 | }; |
| 3235 | q.active += 1; |
| 3236 | drop(q); |
| 3237 | if stats { |
| 3238 | WS_WAIT[slot].fetch_add(t0.elapsed().as_nanos() as u64, Relaxed); |
| 3239 | WS_JOBS[slot].fetch_add(1, Relaxed); |
| 3240 | WS_OPS[slot].fetch_add(job.ops.len() as u64, Relaxed); |
| 3241 | } |
| 3242 | let t1 = std::time::Instant::now(); |
| 3243 | let ReconPlanes(y, u, v) = st.planes; |
| 3244 | // SAFETY: this job writes only superblock (r, c)'s own pixels, and |
| 3245 | // every superblock that could have written or be reading them is |
| 3246 | // complete by the wavefront rule above. |
| 3247 | let mut cur: [Vec<u16>; 3] = Default::default(); |
| 3248 | let ReconJob { mut ops, mut coeffs, mut preds } = job; |
| 3249 | unsafe { |
| 3250 | for op in ops.drain(..) { |
| 3251 | exec_op(op, &mut *y, &mut *u, &mut *v, &mut cur, &coeffs, &preds, &fctx); |
| 3252 | } |
| 3253 | } |
| 3254 | if stats { |
| 3255 | WS_BUSY[slot].fetch_add(t1.elapsed().as_nanos() as u64, Relaxed); |
| 3256 | } |
| 3257 | coeffs.clear(); |
| 3258 | preds.clear(); |
| 3259 | let mut q = st.q.lock().unwrap(); |
| 3260 | // lane-sballoc: hand the three buffers back for the next submit. |
| 3261 | if q.free.len() < q.free_cap { |
| 3262 | q.free.push((ops, coeffs, preds)); |
| 3263 | } |
| 3264 | q.done[r] = c as isize; |
| 3265 | q.active -= 1; |
| 3266 | st.cv.notify_all(); |
| 3267 | } |
| 3268 | } |
| 3269 | |
| 3270 | /// Installs the wavefront for one tile and joins its workers on every exit |
| 3271 | /// from the tile decoder, including the `?` ones -- the jobs hold raw |
| 3272 | /// pointers into the tile's planes, so no worker may outlive them. |
| 3273 | /// (lane-pool1: the workers come from this thread's persistent pool, so a |
| 3274 | /// tile costs a batch submit rather than `EC_AV1_RECON_THREADS` fresh threads |
| 3275 | /// with fresh scratch.) |
| 3276 | pub(crate) struct WaveGuard<'a>( |
| 3277 | &'a crate::decode::FrameCtx, |
| 3278 | Option<crate::par::Batch<'static>>, |
| 3279 | ); |
| 3280 | |
| 3281 | #[allow(unsafe_code)] |
| 3282 | impl<'a> WaveGuard<'a> { |
| 3283 | fn install( |
| 3284 | fctx: &'a crate::decode::FrameCtx, |
| 3285 | y: &mut PlaneBuf<'static>, |
| 3286 | u: &mut PlaneBuf<'static>, |
| 3287 | v: &mut PlaneBuf<'static>, |
| 3288 | rows: usize, |
| 3289 | cols: usize, |
| 3290 | ) -> Option<Self> { |
| 3291 | let threads = crate::par::recon_threads(); |
| 3292 | if threads == 1 || rows == 0 || cols == 0 { |
| 3293 | return None; |
| 3294 | } |
| 3295 | let st = std::sync::Arc::new(WaveState { |
| 3296 | q: std::sync::Mutex::new(WaveQueue { |
| 3297 | rows: (0..rows).map(|_| std::collections::VecDeque::new()).collect(), |
| 3298 | queued: 0, |
| 3299 | done: vec![-1; rows], |
| 3300 | last_sub: vec![-1; rows], |
| 3301 | last_push: vec![-1; rows], |
| 3302 | active: 0, |
| 3303 | closed: false, |
| 3304 | free: Vec::new(), |
| 3305 | free_cap: threads + 2, |
| 3306 | }), |
| 3307 | cv: std::sync::Condvar::new(), |
| 3308 | // SAFETY: the planes are locals of the tile decoder and outlive |
| 3309 | // this guard, which joins every worker in `drop`. |
| 3310 | planes: ReconPlanes( |
| 3311 | std::ptr::from_mut(y), |
| 3312 | std::ptr::from_mut(u), |
| 3313 | std::ptr::from_mut(v), |
| 3314 | ), |
| 3315 | bit_depth: fctx.bit_depth.with(std::cell::Cell::get), |
| 3316 | enable_edge_filter: fctx.enable_edge_filter.with(std::cell::Cell::get), |
| 3317 | last_col: cols - 1, |
| 3318 | }); |
| 3319 | let batch = crate::par::Batch::new("ec-av1-recon"); |
| 3320 | for idx in 0..threads { |
| 3321 | let st = std::sync::Arc::clone(&st); |
| 3322 | batch.submit(move || wave_worker(&st, idx)); |
| 3323 | } |
| 3324 | fctx.wave.with(|w| *w.borrow_mut() = Some(st)); |
| 3325 | fctx.wave_mark.with(|c| c.set(Some(std::time::Instant::now()))); |
| 3326 | Some(Self(fctx, Some(batch))) |
| 3327 | } |
| 3328 | } |
| 3329 | |
| 3330 | impl Drop for WaveGuard<'_> { |
| 3331 | fn drop(&mut self) { |
| 3332 | let st = self.0.wave.with(|w| w.borrow_mut().take()); |
| 3333 | if let Some(st) = st { |
| 3334 | st.q.lock().unwrap().closed = true; |
| 3335 | st.cv.notify_all(); |
| 3336 | } |
| 3337 | let t0 = std::time::Instant::now(); |
| 3338 | // Dropping the batch waits every worker out, as the joins did. |
| 3339 | drop(self.1.take()); |
| 3340 | if wave_stats() { |
| 3341 | WS_DRAIN.fetch_add(t0.elapsed().as_nanos() as u64, Relaxed); |
| 3342 | WS_TILES.fetch_add(1, Relaxed); |
| 3343 | wave_stats_dump(); |
| 3344 | } |
| 3345 | } |
| 3346 | } |
| 3347 | |
| 3348 | /// An empty buffer for the next superblock: the recycled one when it is at |
| 3349 | /// least as big as the superblock just parsed, else a fresh sized one |
| 3350 | /// (lane-sballoc; `v` is always empty). |
| 3351 | fn reuse<T>(mut v: Vec<T>, cap: usize) -> Vec<T> { |
| 3352 | if v.capacity() < cap { |
| 3353 | v.reserve_exact(cap - v.len()); |
| 3354 | } |
| 3355 | v |
| 3356 | } |
| 3357 | |
| 3358 | /// Hands superblock `(r, c)`'s recorded writes to the wavefront. `false` |
| 3359 | /// when there is no wavefront (one reconstruction thread), in which case the |
| 3360 | /// caller replays them itself. |
| 3361 | fn wave_submit(fctx: &crate::decode::FrameCtx, r: usize, c: usize) -> bool { |
| 3362 | let st = fctx.wave.with(|w| w.borrow().clone()); |
| 3363 | let Some(st) = st else { return false }; |
| 3364 | // lane-blockmem: the op list is replaced sized for the next superblock, |
| 3365 | // exactly as the two arenas below are -- taking it left a zero-capacity |
| 3366 | // `Vec` that regrew (and memmoved every recorded op) from nothing on |
| 3367 | // every superblock. |
| 3368 | // lane-sballoc: the replacement comes from the finished jobs' free list |
| 3369 | // when there is one (warm capacity, already faulted in); only the first |
| 3370 | // superblocks of a tile allocate. |
| 3371 | let mut q = st.q.lock().unwrap(); |
| 3372 | let (sops, scoeffs, spreds) = q.free.pop().unwrap_or_default(); |
| 3373 | let ops = fctx.recon_ops.with(|c| { |
| 3374 | let mut c = c.borrow_mut(); |
| 3375 | let cap = c.len(); |
| 3376 | std::mem::replace(&mut *c, reuse(sops, cap)) |
| 3377 | }); |
| 3378 | // The job owns its coefficients: the arena is replaced by an empty one |
| 3379 | // sized for the next superblock, so parse never regrows from nothing and |
| 3380 | // the worker frees this buffer once, instead of once per unit. |
| 3381 | let coeffs = fctx.coeff_arena.with(|c| { |
| 3382 | let mut c = c.borrow_mut(); |
| 3383 | let cap = c.len(); |
| 3384 | std::mem::replace(&mut *c, reuse(scoeffs, cap)) |
| 3385 | }); |
| 3386 | let preds = fctx.pred_arena.with(|c| { |
| 3387 | let mut c = c.borrow_mut(); |
| 3388 | let cap = c.len(); |
| 3389 | std::mem::replace(&mut *c, reuse(spreds, cap)) |
| 3390 | }); |
| 3391 | { |
| 3392 | if (c as isize) > q.last_sub[r] { |
| 3393 | q.last_sub[r] = c as isize; |
| 3394 | } |
| 3395 | if !ops.is_empty() { |
| 3396 | q.rows[r].push_back((c, ReconJob { ops, coeffs, preds })); |
| 3397 | q.queued += 1; |
| 3398 | q.last_push[r] = c as isize; |
| 3399 | } |
| 3400 | drop(q); |
| 3401 | st.cv.notify_all(); |
| 3402 | } |
| 3403 | if wave_stats() { |
| 3404 | let now = std::time::Instant::now(); |
| 3405 | if let Some(t) = fctx.wave_mark.with(|c| c.replace(Some(now))) { |
| 3406 | WS_PARSE.fetch_add((now - t).as_nanos() as u64, Relaxed); |
| 3407 | } |
| 3408 | } |
| 3409 | true |
| 3410 | } |
| 3411 | |
| 3412 | /// Waits until every submitted job is reconstructed, so the parser may read |
| 3413 | /// the planes itself (the intrabc source fetch and the interintra intra |
| 3414 | /// predictor -- see [`flush_recon`]). |
| 3415 | fn wave_barrier(fctx: &crate::decode::FrameCtx) { |
| 3416 | let st = fctx.wave.with(|w| w.borrow().clone()); |
| 3417 | let Some(st) = st else { return }; |
| 3418 | let stats = wave_stats(); |
| 3419 | let t0 = std::time::Instant::now(); |
| 3420 | let mut q = st.q.lock().unwrap(); |
| 3421 | while q.queued > 0 || q.active > 0 { |
| 3422 | q = st.cv.wait(q).unwrap(); |
| 3423 | } |
| 3424 | drop(q); |
| 3425 | if stats { |
| 3426 | WS_BARRIER.fetch_add(t0.elapsed().as_nanos() as u64, Relaxed); |
| 3427 | WS_BARRIERS.fetch_add(1, Relaxed); |
| 3428 | } |
| 3429 | } |
| 3430 | |
| 3431 | /// Performs an intra write at its push site when the tile registered its |
| 3432 | /// planes for inline reconstruction ([`ReconPlanes`]); `false` when the |
| 3433 | /// caller must record it for the wavefront workers instead. |
| 3434 | #[allow(unsafe_code)] |
| 3435 | #[allow(clippy::too_many_arguments)] |
| 3436 | fn inline_intra( |
| 3437 | plane: usize, |
| 3438 | x: usize, |
| 3439 | oy: usize, |
| 3440 | bw: usize, |
| 3441 | bh: usize, |
| 3442 | rect: bool, |
| 3443 | mode: usize, |
| 3444 | angle_delta: i32, |
| 3445 | reach: Reach, |
| 3446 | residual: &[i32], |
| 3447 | cfl: Option<(i32, CflSrc)>, |
| 3448 | filter_intra: Option<usize>, |
| 3449 | smooth_neighbor: bool, |
| 3450 | fctx: &crate::decode::FrameCtx, |
| 3451 | ) -> bool { |
| 3452 | let Some(ReconPlanes(y, u, v)) = fctx.recon_planes.with(std::cell::Cell::get) else { |
| 3453 | return false; |
| 3454 | }; |
| 3455 | let pred = take_palette_pred(fctx); |
| 3456 | // SAFETY: see [`ReconPlanes`]. |
| 3457 | unsafe { |
| 3458 | exec_intra( |
| 3459 | &mut *y, &mut *u, &mut *v, plane, x, oy, bw, bh, rect, mode, angle_delta, reach, |
| 3460 | residual, cfl, filter_intra, smooth_neighbor, pred, fctx, |
| 3461 | ); |
| 3462 | } |
| 3463 | true |
| 3464 | } |
| 3465 | |
| 3466 | /// [`inline_intra`] for a motion-compensated write. |
| 3467 | #[allow(unsafe_code)] |
| 3468 | #[allow(clippy::too_many_arguments)] |
| 3469 | fn inline_mc( |
| 3470 | plane: usize, |
| 3471 | x: usize, |
| 3472 | oy: usize, |
| 3473 | stride: usize, |
| 3474 | w: usize, |
| 3475 | h: usize, |
| 3476 | prediction: &[u16], |
| 3477 | residual: &[i32], |
| 3478 | fctx: &crate::decode::FrameCtx, |
| 3479 | ) -> bool { |
| 3480 | let Some(ReconPlanes(y, u, v)) = fctx.recon_planes.with(std::cell::Cell::get) else { |
| 3481 | return false; |
| 3482 | }; |
| 3483 | // SAFETY: see [`ReconPlanes`]. |
| 3484 | unsafe { |
| 3485 | exec_mc(&mut *y, &mut *u, &mut *v, plane, x, oy, stride, w, h, prediction, residual, fctx); |
| 3486 | } |
| 3487 | true |
| 3488 | } |
| 3489 | |
| 3490 | /// [`PlaneBuf::reconstruct`], recorded (lane-wave1 step (i)). |
| 3491 | #[allow(clippy::too_many_arguments)] |
| 3492 | fn push_intra( |
| 3493 | plane: usize, |
| 3494 | x: usize, |
| 3495 | y: usize, |
| 3496 | side: usize, |
| 3497 | mode: usize, |
| 3498 | angle_delta: i32, |
| 3499 | reach: Reach, |
| 3500 | residual: &[i32], |
| 3501 | cfl: Option<(i32, CflSrc)>, |
| 3502 | filter_intra: Option<usize>, |
| 3503 | smooth_neighbor: bool, fctx: &crate::decode::FrameCtx, |
| 3504 | ) { |
| 3505 | if inline_intra( |
| 3506 | plane, x, y, side, side, false, mode, angle_delta, reach, residual, cfl, filter_intra, |
| 3507 | smooth_neighbor, fctx, |
| 3508 | ) { |
| 3509 | return; |
| 3510 | } |
| 3511 | let pred = take_palette_pred(fctx); |
| 3512 | push_op(fctx, ReconOp::Intra { |
| 3513 | plane, x, y, bw: side, bh: side, rect: false, mode, angle_delta, reach, |
| 3514 | residual: Residual::done(residual, fctx), cfl, filter_intra, smooth_neighbor, pred, |
| 3515 | }); |
| 3516 | } |
| 3517 | |
| 3518 | /// [`PlaneBuf::reconstruct_rect`], recorded (lane-wave1 step (i)). |
| 3519 | #[allow(clippy::too_many_arguments)] |
| 3520 | fn push_intra_rect( |
| 3521 | plane: usize, |
| 3522 | x: usize, |
| 3523 | y: usize, |
| 3524 | bw: usize, |
| 3525 | bh: usize, |
| 3526 | mode: usize, |
| 3527 | angle_delta: i32, |
| 3528 | reach: Reach, |
| 3529 | residual: &[i32], |
| 3530 | cfl: Option<(i32, CflSrc)>, |
| 3531 | filter_intra: Option<usize>, |
| 3532 | smooth_neighbor: bool, fctx: &crate::decode::FrameCtx, |
| 3533 | ) { |
| 3534 | if inline_intra( |
| 3535 | plane, x, y, bw, bh, true, mode, angle_delta, reach, residual, cfl, filter_intra, |
| 3536 | smooth_neighbor, fctx, |
| 3537 | ) { |
| 3538 | return; |
| 3539 | } |
| 3540 | let pred = take_palette_pred(fctx); |
| 3541 | push_op(fctx, ReconOp::Intra { |
| 3542 | plane, x, y, bw, bh, rect: true, mode, angle_delta, reach, |
| 3543 | residual: Residual::done(residual, fctx), cfl, filter_intra, smooth_neighbor, pred, |
| 3544 | }); |
| 3545 | } |
| 3546 | |
| 3547 | /// [`PlaneBuf::reconstruct_mc_rect`] at `stride == w == h == side`, recorded. |
| 3548 | fn push_mc( |
| 3549 | plane: usize, |
| 3550 | x: usize, |
| 3551 | y: usize, |
| 3552 | side: usize, |
| 3553 | prediction: Pred<'_>, |
| 3554 | residual: &[i32], fctx: &crate::decode::FrameCtx, |
| 3555 | ) { |
| 3556 | push_mc_rect(plane, x, y, side, side, side, prediction, residual, fctx); |
| 3557 | } |
| 3558 | |
| 3559 | /// [`PlaneBuf::reconstruct_mc_rect`], recorded (lane-wave1 step (i)). |
| 3560 | #[allow(clippy::too_many_arguments)] |
| 3561 | fn push_mc_rect( |
| 3562 | plane: usize, |
| 3563 | x: usize, |
| 3564 | y: usize, |
| 3565 | stride: usize, |
| 3566 | w: usize, |
| 3567 | h: usize, |
| 3568 | prediction: Pred<'_>, |
| 3569 | residual: &[i32], fctx: &crate::decode::FrameCtx, |
| 3570 | ) { |
| 3571 | let (src, stride) = match prediction.inline(stride) { |
| 3572 | Some(v) => v, |
| 3573 | None => { |
| 3574 | let Pred::Cur { plane: sp, stride: ss, off } = prediction else { unreachable!() }; |
| 3575 | push_op(fctx, ReconOp::Mc { |
| 3576 | plane, x, y, stride: ss, w, h, |
| 3577 | prediction: PredSrc::Cur { plane: sp, off, src: ss }, |
| 3578 | residual: Residual::done(residual, fctx), |
| 3579 | }); |
| 3580 | return; |
| 3581 | } |
| 3582 | }; |
| 3583 | if inline_mc(plane, x, y, stride, w, h, src, residual, fctx) { |
| 3584 | return; |
| 3585 | } |
| 3586 | push_op(fctx, ReconOp::Mc { |
| 3587 | plane, x, y, stride, w, h, |
| 3588 | // The whole window `reconstruct_mc_rect` reads at `stride`, not |
| 3589 | // `w * h`: the record keeps exactly what `src.to_vec()` carried. |
| 3590 | prediction: PredSrc::Owned(push_pred_samples(src, fctx), src.len() as u32), |
| 3591 | residual: Residual::done(residual, fctx), |
| 3592 | }); |
| 3593 | } |
| 3594 | |
| 3595 | /// [`push_intra_rect`] carrying the transform's INPUTS: at one reconstruction |
| 3596 | /// thread the transform runs here, exactly as before; on the wavefront it |
| 3597 | /// travels with the op and runs on the worker (lane-wave3). |
| 3598 | #[allow(clippy::too_many_arguments)] |
| 3599 | fn push_intra_tx( |
| 3600 | plane: usize, |
| 3601 | x: usize, |
| 3602 | y: usize, |
| 3603 | bw: usize, |
| 3604 | bh: usize, |
| 3605 | rect: bool, |
| 3606 | mode: usize, |
| 3607 | angle_delta: i32, |
| 3608 | reach: Reach, |
| 3609 | tx: TxParams, |
| 3610 | grid: &[i32], |
| 3611 | cfl: Option<(i32, CflSrc)>, |
| 3612 | filter_intra: Option<usize>, |
| 3613 | smooth_neighbor: bool, |
| 3614 | fctx: &crate::decode::FrameCtx, |
| 3615 | ) { |
| 3616 | if fctx.recon_planes.with(std::cell::Cell::get).is_some() { |
| 3617 | inline_intra( |
| 3618 | plane, x, y, bw, bh, rect, mode, angle_delta, reach, &tx.run(grid), cfl, filter_intra, |
| 3619 | smooth_neighbor, fctx, |
| 3620 | ); |
| 3621 | return; |
| 3622 | } |
| 3623 | let pred = take_palette_pred(fctx); |
| 3624 | push_op(fctx, ReconOp::Intra { |
| 3625 | plane, x, y, bw, bh, rect, mode, angle_delta, reach, |
| 3626 | residual: Residual::coeffs(tx, grid, fctx), cfl, filter_intra, smooth_neighbor, pred, |
| 3627 | }); |
| 3628 | } |
| 3629 | |
| 3630 | /// [`push_mc_rect`] carrying the transform's inputs -- see [`push_intra_tx`]. |
| 3631 | #[allow(clippy::too_many_arguments)] |
| 3632 | fn push_mc_rect_tx( |
| 3633 | plane: usize, |
| 3634 | x: usize, |
| 3635 | y: usize, |
| 3636 | stride: usize, |
| 3637 | w: usize, |
| 3638 | h: usize, |
| 3639 | prediction: Pred<'_>, |
| 3640 | tx: TxParams, |
| 3641 | grid: &[i32], |
| 3642 | fctx: &crate::decode::FrameCtx, |
| 3643 | ) { |
| 3644 | // lane-wave4: a deferred prediction is not there to be windowed -- the |
| 3645 | // op carries its offset into the block's prediction instead, and the |
| 3646 | // residual is laid out at that same stride (`reconstruct_mc_rect` reads |
| 3647 | // both buffers at one stride). |
| 3648 | let (prediction, stride) = match prediction.inline(stride) { |
| 3649 | Some(v) => v, |
| 3650 | None => { |
| 3651 | let Pred::Cur { plane: sp, stride: ss, off } = prediction else { unreachable!() }; |
| 3652 | let mut tx = tx; |
| 3653 | tx.stride = 0; |
| 3654 | push_op(fctx, ReconOp::Mc { |
| 3655 | plane, x, y, stride: w, w, h, |
| 3656 | prediction: PredSrc::Cur { plane: sp, off, src: ss }, |
| 3657 | residual: Residual::coeffs(tx, grid, fctx), |
| 3658 | }); |
| 3659 | return; |
| 3660 | } |
| 3661 | }; |
| 3662 | if fctx.recon_planes.with(std::cell::Cell::get).is_some() { |
| 3663 | // `reconstruct_mc_rect` reads the prediction and the residual at ONE |
| 3664 | // stride, so a prediction handed over at the block's stride (a var-tx |
| 3665 | // unit's window, lane-wave4) has to be densified for a dense residual |
| 3666 | // -- which is exactly what the recorded path below does. |
| 3667 | let residual = tx.run(grid); |
| 3668 | let res_stride = if tx.stride == 0 { w } else { tx.stride }; |
| 3669 | let window; |
| 3670 | let prediction = if stride == res_stride { |
| 3671 | prediction |
| 3672 | } else { |
| 3673 | debug_assert_eq!(res_stride, w); |
| 3674 | let mut win = Vec::with_capacity(w * h); |
| 3675 | for row in 0..h { |
| 3676 | win.extend_from_slice(&prediction[row * stride..][..w]); |
| 3677 | } |
| 3678 | window = win; |
| 3679 | &window[..] |
| 3680 | }; |
| 3681 | inline_mc(plane, x, y, res_stride, w, h, prediction, &residual, fctx); |
| 3682 | return; |
| 3683 | } |
| 3684 | // `reconstruct_mc_rect` reads exactly rows `0..h`, columns `0..w` of the |
| 3685 | // two buffers at `stride`, so the record carries that window densely |
| 3686 | // rather than the whole block's prediction: a 4x4 unit of a 64x64 block |
| 3687 | // copied 64*64 samples on the PARSING thread before. The residual is then |
| 3688 | // produced dense too (`TxParams::stride = 0`), which is the same numbers |
| 3689 | // in the same places at stride `w`. |
| 3690 | let off = fctx.pred_arena.with(|a| { |
| 3691 | let mut a = a.borrow_mut(); |
| 3692 | let off = a.len(); |
| 3693 | for row in 0..h { |
| 3694 | a.extend_from_slice(&prediction[row * stride..][..w]); |
| 3695 | } |
| 3696 | off as u32 |
| 3697 | }); |
| 3698 | let mut tx = tx; |
| 3699 | tx.stride = 0; |
| 3700 | push_op(fctx, ReconOp::Mc { |
| 3701 | plane, x, y, stride: w, w, h, prediction: PredSrc::Owned(off, (w * h) as u32), |
| 3702 | residual: Residual::coeffs(tx, grid, fctx), |
| 3703 | }); |
| 3704 | } |
| 3705 | |
| 3706 | /// Replays every recorded [`ReconOp`] into the frame's planes, in push order, |
| 3707 | /// and empties the queue (lane-wave1 step (i)). |
| 3708 | /// |
| 3709 | /// Called at the top of each superblock visit and at the end of each tile, so |
| 3710 | /// a whole superblock's parsing runs with no plane access at all; the two |
| 3711 | /// parse-time pixel READS that survive -- the intrabc source fetch and the |
| 3712 | /// interintra intra predictor -- flush ahead of themselves instead, which |
| 3713 | /// keeps them byte-exact and names exactly what step (ii) still has to move. |
| 3714 | fn flush_recon( |
| 3715 | fctx: &crate::decode::FrameCtx, |
| 3716 | y: &mut PlaneBuf<'static>, |
| 3717 | u: &mut PlaneBuf<'static>, |
| 3718 | v: &mut PlaneBuf<'static>, |
| 3719 | ) { |
| 3720 | // lane-wave1 step (ii): a parse-time pixel READ needs every worker's |
| 3721 | // writes in place, not just this superblock's queue. |
| 3722 | wave_barrier(fctx); |
| 3723 | let mut ops = fctx.recon_spare.with(|c| std::mem::take(&mut *c.borrow_mut())); |
| 3724 | fctx.recon_ops.with(|c| std::mem::swap(&mut *c.borrow_mut(), &mut ops)); |
| 3725 | let mut coeffs = fctx.coeff_spare.with(|c| std::mem::take(&mut *c.borrow_mut())); |
| 3726 | fctx.coeff_arena.with(|c| std::mem::swap(&mut *c.borrow_mut(), &mut coeffs)); |
| 3727 | let mut preds = fctx.pred_spare.with(|c| std::mem::take(&mut *c.borrow_mut())); |
| 3728 | fctx.pred_arena.with(|c| std::mem::swap(&mut *c.borrow_mut(), &mut preds)); |
| 3729 | let mut cur: [Vec<u16>; 3] = Default::default(); |
| 3730 | for op in ops.drain(..) { |
| 3731 | exec_op(op, y, u, v, &mut cur, &coeffs, &preds, fctx); |
| 3732 | } |
| 3733 | coeffs.clear(); |
| 3734 | preds.clear(); |
| 3735 | fctx.recon_spare.with(|c| *c.borrow_mut() = ops); |
| 3736 | fctx.coeff_spare.with(|c| *c.borrow_mut() = coeffs); |
| 3737 | fctx.pred_spare.with(|c| *c.borrow_mut() = preds); |
| 3738 | } |
| 3739 | |
| 3740 | /// Performs one recorded write: the body [`flush_recon`] replays, and the |
| 3741 | /// body [`push_op`] runs immediately when reconstruction is inline. |
| 3742 | fn exec_op( |
| 3743 | op: ReconOp, |
| 3744 | y: &mut PlaneBuf<'static>, |
| 3745 | u: &mut PlaneBuf<'static>, |
| 3746 | v: &mut PlaneBuf<'static>, |
| 3747 | cur: &mut [Vec<u16>; 3], |
| 3748 | coeffs: &[i32], |
| 3749 | preds: &[u16], |
| 3750 | fctx: &crate::decode::FrameCtx, |
| 3751 | ) { |
| 3752 | // lane-nbpub instrument: `EC_AV1_NORECON=1` drops every recorded pixel |
| 3753 | // write, leaving the entropy parse and the per-mi neighbour bookkeeping |
| 3754 | // alone. It makes the encoder's own streams garbage, so it is a |
| 3755 | // MEASUREMENT rung only -- what it measures is how much of the encoder's |
| 3756 | // first decode of its own tiles (`par::S_FIRST`) is reconstruction and |
| 3757 | // how much is parse. Answer at 3840x1608 (4 frames, 4x2 tiles, 8 tile |
| 3758 | // threads): 150 ms -> 141 ms, i.e. the reconstruction is 6% of that |
| 3759 | // stage and the parse plus the neighbour grids are the other 94%. That |
| 3760 | // is why handing the decoder the encoder's own reconstruction cannot |
| 3761 | // shrink the stage: only publishing the per-mi neighbour state the |
| 3762 | // filters read (and so never parsing at all) can. |
| 3763 | if crate::envflags::env_flag!("EC_AV1_NORECON") { |
| 3764 | return; |
| 3765 | } |
| 3766 | let mut scratch = Vec::new(); |
| 3767 | match op { |
| 3768 | ReconOp::Pred(b) => (b.0)(y, u, v, fctx, cur), |
| 3769 | ReconOp::Intra { |
| 3770 | plane, x, y: oy, bw, bh, rect, mode, angle_delta, reach, residual, cfl, |
| 3771 | filter_intra, smooth_neighbor, pred, |
| 3772 | } => exec_intra( |
| 3773 | y, u, v, plane, x, oy, bw, bh, rect, mode, angle_delta, reach, |
| 3774 | residual.resolve(coeffs, &mut scratch), |
| 3775 | cfl, filter_intra, smooth_neighbor, pred, fctx, |
| 3776 | ), |
| 3777 | ReconOp::Mc { plane, x, y: oy, stride, w, h, prediction, residual } => { |
| 3778 | let residual = residual.resolve(coeffs, &mut scratch); |
| 3779 | match prediction { |
| 3780 | PredSrc::Owned(off, len) => { |
| 3781 | let p = &preds[off as usize..][..len as usize]; |
| 3782 | exec_mc(y, u, v, plane, x, oy, stride, w, h, p, residual, fctx); |
| 3783 | } |
| 3784 | PredSrc::Cur { plane: sp, off, src } => { |
| 3785 | let block = std::mem::take(&mut cur[sp]); |
| 3786 | if src == stride { |
| 3787 | exec_mc(y, u, v, plane, x, oy, stride, w, h, &block[off..], residual, fctx); |
| 3788 | } else { |
| 3789 | // A transform unit's residual is dense, so its window |
| 3790 | // of the block prediction is taken out dense too -- |
| 3791 | // the copy the parsing thread used to make, made here. |
| 3792 | let mut window = Vec::with_capacity(w * h); |
| 3793 | for row in 0..h { |
| 3794 | window.extend_from_slice(&block[off + row * src..][..w]); |
| 3795 | } |
| 3796 | exec_mc(y, u, v, plane, x, oy, stride, w, h, &window, residual, fctx); |
| 3797 | } |
| 3798 | cur[sp] = block; |
| 3799 | } |
| 3800 | } |
| 3801 | } |
| 3802 | } |
| 3803 | } |
| 3804 | |
| 3805 | /// [`ReconOp::Intra`] performed. Its arguments are borrowed, so the inline |
| 3806 | /// path ([`push_op`]'s registered planes) reaches it without ever building |
| 3807 | /// the record -- which is what keeps one reconstruction thread at the cost of |
| 3808 | /// the pre-lane-wave1 decoder. |
| 3809 | #[allow(clippy::too_many_arguments)] |
| 3810 | fn exec_intra( |
| 3811 | y: &mut PlaneBuf<'static>, |
| 3812 | u: &mut PlaneBuf<'static>, |
| 3813 | v: &mut PlaneBuf<'static>, |
| 3814 | plane: usize, |
| 3815 | x: usize, |
| 3816 | oy: usize, |
| 3817 | bw: usize, |
| 3818 | bh: usize, |
| 3819 | rect: bool, |
| 3820 | mode: usize, |
| 3821 | angle_delta: i32, |
| 3822 | reach: Reach, |
| 3823 | residual: &[i32], |
| 3824 | cfl: Option<(i32, CflSrc)>, |
| 3825 | filter_intra: Option<usize>, |
| 3826 | smooth_neighbor: bool, |
| 3827 | pred: Option<Vec<u16>>, |
| 3828 | fctx: &crate::decode::FrameCtx, |
| 3829 | ) { |
| 3830 | // The CfL AC signal is read HERE, off the luma that has already been |
| 3831 | // written for this block -- at parse time it would have been the previous |
| 3832 | // frame's samples. |
| 3833 | let ac = cfl.map(|(alpha, src)| { |
| 3834 | let sig = cfl_ac_ss(&*y, src.px, src.py, src.bw, src.bh, ss_x(fctx), ss_y(fctx)); |
| 3835 | (alpha, sig) |
| 3836 | }); |
| 3837 | let cfl = ac.as_ref().map(|(a, sig)| (*a, sig.as_slice())); |
| 3838 | let target: &mut PlaneBuf<'static> = match plane { 0 => &mut *y, 1 => &mut *u, _ => &mut *v }; |
| 3839 | if rect { |
| 3840 | target.reconstruct_rect( |
| 3841 | x, oy, bw, bh, mode, angle_delta, reach, residual, cfl, filter_intra, |
| 3842 | smooth_neighbor, pred, fctx, |
| 3843 | ); |
| 3844 | } else { |
| 3845 | target.reconstruct( |
| 3846 | x, oy, bw, mode, angle_delta, reach, residual, cfl, filter_intra, |
| 3847 | smooth_neighbor, pred, fctx, |
| 3848 | ); |
| 3849 | } |
| 3850 | } |
| 3851 | |
| 3852 | /// [`ReconOp::Mc`] performed -- see [`exec_intra`]. |
| 3853 | #[allow(clippy::too_many_arguments)] |
| 3854 | fn exec_mc( |
| 3855 | y: &mut PlaneBuf<'static>, |
| 3856 | u: &mut PlaneBuf<'static>, |
| 3857 | v: &mut PlaneBuf<'static>, |
| 3858 | plane: usize, |
| 3859 | x: usize, |
| 3860 | oy: usize, |
| 3861 | stride: usize, |
| 3862 | w: usize, |
| 3863 | h: usize, |
| 3864 | prediction: &[u16], |
| 3865 | residual: &[i32], |
| 3866 | fctx: &crate::decode::FrameCtx, |
| 3867 | ) { |
| 3868 | let target: &mut PlaneBuf<'static> = match plane { 0 => &mut *y, 1 => &mut *u, _ => &mut *v }; |
| 3869 | target.reconstruct_mc_rect(x, oy, stride, w, h, prediction, residual, fctx); |
| 3870 | } |
| 3871 | |
| 3872 | thread_local! { |
| 3873 | /// The current frame's `mi` grid, kept only while a frame header set |
| 3874 | /// `allow_intrabc` -- an intrabc block's DV predictor is the ordinary |
| 3875 | /// MV stack ([`crate::mvstack::find_mv_stack`]) built against |
| 3876 | /// `INTRA_FRAME`, which needs the neighbour state a key frame otherwise |
| 3877 | /// never keeps. `(grid, mi_cols, mi_rows)`; `None` on every frame that |
| 3878 | /// does not allow intrabc (so nothing is paid for it). |
| 3879 | /// |
| 3880 | /// corner-cut: only [`decode_block`] (the square path) records into it, |
| 3881 | /// so a frame mixing intrabc with rect/sub-8x8 leaves would predict off |
| 3882 | /// an incomplete grid. Ceiling: the DV *value* (never the parse) would |
| 3883 | /// be wrong there; upgrade = record from the rect/leaf paths too. |
| 3884 | /// The block vector [`read_intra_mode`] just decoded, handed to |
| 3885 | /// [`decode_block`] off the call stack (same idiom as [`PALETTE_PRED`], |
| 3886 | /// which the alternative -- a tenth tuple member through every caller -- |
| 3887 | /// already avoids). Always `.take()`n by the block that reads it. |
| 3888 | /// How many blocks read `use_intrabc == 1` and took the DV path -- the |
| 3889 | /// gate's hit counter. Bumped on the READ, before the rect-strip refusal |
| 3890 | /// returns too, so it counts reads, not reconstructions (a refused block |
| 3891 | /// is never reconstructed). |
| 3892 | static INTRABC_HITS: std::cell::Cell<usize> = const { std::cell::Cell::new(0) }; |
| 3893 | } |
| 3894 | // The `tx_type` the last plane-0 [`read_plane`] actually coded, kept so |
| 3895 | // an intrabc block's chroma reads can inherit it (see |
| 3896 | // [`INTRABC_CHROMA_TX`]). Written on every luma read; only ever read |
| 3897 | // through that one arming site. |
| 3898 | // libaom `av1_get_tx_type` (`blockd.h:1350`): for `PLANE_TYPE_UV` it |
| 3899 | // branches on `is_inter_block(mbmi)` -- which `is_intrabc_block` also |
| 3900 | // satisfies (`blockd.h:373`) -- and takes the *colocated luma |
| 3901 | // transform's coded* `tx_type` (`mbmi->txk_type[...]`), never |
| 3902 | // `intra_mode_to_tx_type` of the chroma mode. `Some` only across an |
| 3903 | // intrabc block's two chroma [`read_plane`] calls; the type decides the |
| 3904 | // `TX_CLASS`, hence the scan order, `eob_multi_ctx` and every nz context |
| 3905 | // -- getting it wrong desyncs the chroma coefficients, it is not a |
| 3906 | // reconstruction-only detail. |
| 3907 | |
| 3908 | /// Whether this frame's header set `allow_intrabc` -- [`INTRABC_MI_GRID`] is |
| 3909 | /// `Some` exactly then (set once per frame in `decode_frame`), so the flag |
| 3910 | /// needs no separate plumbing to reach the rect-strip mode reader. |
| 3911 | fn allow_intrabc_frame(fctx: &crate::decode::FrameCtx) -> bool { |
| 3912 | fctx.intrabc_mi_grid.with(|g| g.borrow().is_some()) |
| 3913 | } |
| 3914 | |
| 3915 | thread_local! { |
| 3916 | /// How many rect intra strips READ the `use_intrabc` symbol (i.e. how many |
| 3917 | /// times the fix below was exercised) -- the rect-strip gate's hit counter, |
| 3918 | /// distinct from [`INTRABC_HITS`] which counts blocks that USE intrabc. |
| 3919 | static RECT_INTRABC_READS: std::cell::Cell<usize> = const { std::cell::Cell::new(0) }; |
| 3920 | } |
| 3921 | |
| 3922 | /// Current value of [`RECT_INTRABC_READS`]. |
| 3923 | pub(crate) fn rect_intrabc_reads() -> usize { |
| 3924 | RECT_INTRABC_READS.with(|c| c.get()) |
| 3925 | } |
| 3926 | |
| 3927 | /// Zeroes [`RECT_INTRABC_READS`] (gate tests call this before a decode). |
| 3928 | #[allow(dead_code)] // read only from the `#[cfg(test)]` gates |
| 3929 | pub(crate) fn reset_rect_intrabc_reads() { |
| 3930 | RECT_INTRABC_READS.with(|c| c.set(0)); |
| 3931 | } |
| 3932 | |
| 3933 | thread_local! { |
| 3934 | /// Blocks that USED intrabc at an 8x8 LEAF (`decode_leaf8`) specifically |
| 3935 | /// -- the shape lane-kf900 r7 fixed; [`INTRABC_HITS`] counts every shape. |
| 3936 | static LEAF8_INTRABC_HITS: std::cell::Cell<usize> = const { std::cell::Cell::new(0) }; |
| 3937 | } |
| 3938 | |
| 3939 | thread_local! { |
| 3940 | /// Chroma reads of an intrabc TX4-split leaf that consulted an ARMED |
| 3941 | /// [`INTRABC_CHROMA_TX`] slot -- proof that the arm's frame-copy + |
| 3942 | /// inherited-type route actually ran (lane-av1intrapred r2's non-vacuity |
| 3943 | /// counter: zero means the gate's fixture no longer reaches the route). |
| 3944 | static INTRABC_TX4_CHROMA_COPY_HITS: std::cell::Cell<usize> = |
| 3945 | const { std::cell::Cell::new(0) }; |
| 3946 | } |
| 3947 | |
| 3948 | /// Current value of [`INTRABC_TX4_CHROMA_COPY_HITS`]. |
| 3949 | #[allow(dead_code)] // read only from the `#[cfg(test)]` gates |
| 3950 | pub(crate) fn intrabc_tx4_chroma_copy_hits() -> usize { |
| 3951 | INTRABC_TX4_CHROMA_COPY_HITS.with(|c| c.get()) |
| 3952 | } |
| 3953 | |
| 3954 | /// Zeroes [`INTRABC_TX4_CHROMA_COPY_HITS`] (gate tests call this before a decode). |
| 3955 | #[allow(dead_code)] // read only from the `#[cfg(test)]` gates |
| 3956 | pub(crate) fn reset_intrabc_tx4_chroma_copy_hits() { |
| 3957 | INTRABC_TX4_CHROMA_COPY_HITS.with(|c| c.set(0)); |
| 3958 | } |
| 3959 | |
| 3960 | /// Current value of [`LEAF8_INTRABC_HITS`]. |
| 3961 | pub(crate) fn leaf8_intrabc_hits() -> usize { |
| 3962 | LEAF8_INTRABC_HITS.with(|c| c.get()) |
| 3963 | } |
| 3964 | |
| 3965 | /// Zeroes [`LEAF8_INTRABC_HITS`] (gate tests call this before a decode). |
| 3966 | #[allow(dead_code)] // read only from the `#[cfg(test)]` gates |
| 3967 | pub(crate) fn reset_leaf8_intrabc_hits() { |
| 3968 | LEAF8_INTRABC_HITS.with(|c| c.set(0)); |
| 3969 | } |
| 3970 | |
| 3971 | /// Current value of [`INTRABC_HITS`]. |
| 3972 | #[allow(dead_code)] // read only from the `#[cfg(test)]` gates |
| 3973 | pub(crate) fn intrabc_hits() -> usize { |
| 3974 | INTRABC_HITS.with(|c| c.get()) |
| 3975 | } |
| 3976 | |
| 3977 | thread_local! { |
| 3978 | /// An intrabc block that IS a sub-8x8 group's CHROMA REFERENCE and is |
| 3979 | /// SKIPPED: the route that arms [`INTRABC_CHROMA_TX`] and then reads no |
| 3980 | /// chroma coefficient at all, so the slot used to stay armed into the NEXT |
| 3981 | /// block's chroma read. Both group shapes (4x4 split, 4x8/8x4 rect) count |
| 3982 | /// here. A non-zero value is what makes the disarm below load-bearing. |
| 3983 | static SKIPPED_INTRABC_CHROMA_ARM_HITS: std::cell::Cell<usize> = |
| 3984 | const { std::cell::Cell::new(0) }; |
| 3985 | } |
| 3986 | |
| 3987 | /// Current value of [`SKIPPED_INTRABC_CHROMA_ARM_HITS`]. |
| 3988 | #[allow(dead_code)] // read only from the `#[cfg(test)]` gates and the probe |
| 3989 | pub(crate) fn skipped_intrabc_chroma_arm_hits() -> usize { |
| 3990 | SKIPPED_INTRABC_CHROMA_ARM_HITS.with(|c| c.get()) |
| 3991 | } |
| 3992 | |
| 3993 | /// Zeroes [`SKIPPED_INTRABC_CHROMA_ARM_HITS`] (gate tests call this before a |
| 3994 | /// decode). |
| 3995 | #[allow(dead_code)] // read only from the `#[cfg(test)]` gates |
| 3996 | pub(crate) fn reset_skipped_intrabc_chroma_arm_hits() { |
| 3997 | SKIPPED_INTRABC_CHROMA_ARM_HITS.with(|c| c.set(0)); |
| 3998 | } |
| 3999 | |
| 4000 | /// Zeroes [`INTRABC_HITS`] (gate tests call this before a decode). |
| 4001 | #[allow(dead_code)] // read only from the `#[cfg(test)]` gates |
| 4002 | pub(crate) fn reset_intrabc_hits() { |
| 4003 | INTRABC_HITS.with(|c| c.set(0)); |
| 4004 | } |
| 4005 | |
| 4006 | thread_local! { |
| 4007 | /// lane-t900 r32: intrabc blocks whose transform size came off the INTER |
| 4008 | /// var-tx partition tree (unskipped, `TX_MODE_SELECT`) -- the capability |
| 4009 | /// that replaced the refusal "an intrabc block under TxMode::Select". |
| 4010 | static INTRABC_VARTX_HITS: std::cell::Cell<usize> = const { std::cell::Cell::new(0) }; |
| 4011 | /// lane-av1-ibcvtx: intrabc var-tx trees that resolved to MIXED leaf |
| 4012 | /// sizes (not one square size across every leaf) -- the engagement |
| 4013 | /// counter proving a witness actually exercises the per-leaf reconstruct. |
| 4014 | static INTRABC_VARTX_MIXED_LEAVES_HITS: std::cell::Cell<usize> = const { std::cell::Cell::new(0) }; |
| 4015 | /// lane-av1txr: as above for a `BLOCK_4X8`/`BLOCK_8X4` intrabc leaf -- |
| 4016 | /// libaom's `block_signals_txsize` is `bsize > BLOCK_4X4`, so these leaves |
| 4017 | /// signal a transform size where a plain `BLOCK_4X4` does not. Reading no |
| 4018 | /// symbol here desynced `warped.obu` at the very next block. |
| 4019 | static RECT_INTRABC_VARTX_HITS: std::cell::Cell<usize> = const { std::cell::Cell::new(0) }; |
| 4020 | /// lane-av1-intrabc: rect INTRABC blocks whose reconstruction |
| 4021 | /// [`decode_intrabc_rect`] ran -- the engagement counter the rect-strip |
| 4022 | /// gates assert on (a refused block bumps [`INTRABC_HITS`] but never |
| 4023 | /// this one). |
| 4024 | static INTRABC_RECT_HITS: std::cell::Cell<usize> = const { std::cell::Cell::new(0) }; |
| 4025 | /// Of those, how many read the INTER var-tx tree (unskipped, TX_MODE_SELECT). |
| 4026 | static INTRABC_RECT_VARTX_HITS: std::cell::Cell<usize> = const { std::cell::Cell::new(0) }; |
| 4027 | /// lane-av1-intrabc r4: rect INTRABC blocks whose reconstruction |
| 4028 | /// [`decode_intrabc_rect`] ran with `skip == 0` (a real residual read), |
| 4029 | /// split by the strip's orientation -- `bw > bh` is a HORZ strip, |
| 4030 | /// `bh > bw` a VERT one. The reviewer's unblock condition needs a CODED |
| 4031 | /// witness in BOTH orientations, and the existing `INTRABC_RECT_HITS` |
| 4032 | /// counts neither orientation nor the skip flag, so an arm that flipped |
| 4033 | /// to an all-skip stream (no residual reader at all) would stay green. |
| 4034 | static INTRABC_RECT_CODED_HITS: std::cell::Cell<[usize; 2]> = |
| 4035 | const { std::cell::Cell::new([0, 0]) }; |
| 4036 | /// lane-av1-intrabc r5: the `skip == 1` twin of [`INTRABC_RECT_CODED_HITS`] |
| 4037 | /// -- rect INTRABC blocks reconstructed as a bare prediction copy, split |
| 4038 | /// by orientation. The r5 panic (residual stride `bw * bh` underrunning |
| 4039 | /// `reconstruct_mc_rect`'s `row * side + w` index on a VERT strip) was |
| 4040 | /// invisible to every gate because they only counted CODED strips. |
| 4041 | static INTRABC_RECT_SKIPPED_HITS: std::cell::Cell<[usize; 2]> = |
| 4042 | const { std::cell::Cell::new([0, 0]) }; |
| 4043 | } |
| 4044 | |
| 4045 | /// lane-av1-intrabc r4: coded (`skip == 0`) rect intrabc blocks reconstructed, |
| 4046 | /// as `[horz, vert]` -- see [`INTRABC_RECT_CODED_HITS`]. |
| 4047 | #[allow(dead_code)] // read only from the `#[cfg(test)]` gates |
| 4048 | pub fn intrabc_rect_coded_hits() -> [usize; 2] { |
| 4049 | INTRABC_RECT_CODED_HITS.with(std::cell::Cell::get) |
| 4050 | } |
| 4051 | |
| 4052 | /// lane-av1-intrabc r5: skipped (`skip == 1`) rect intrabc blocks |
| 4053 | /// reconstructed, as `[horz, vert]` -- see [`INTRABC_RECT_SKIPPED_HITS`]. |
| 4054 | #[allow(dead_code)] // read only from the `#[cfg(test)]` gates |
| 4055 | pub fn intrabc_rect_skipped_hits() -> [usize; 2] { |
| 4056 | INTRABC_RECT_SKIPPED_HITS.with(std::cell::Cell::get) |
| 4057 | } |
| 4058 | |
| 4059 | /// Current value of [`INTRABC_RECT_HITS`]. |
| 4060 | #[allow(dead_code)] // read only from the `#[cfg(test)]` gates |
| 4061 | pub fn intrabc_rect_hits() -> usize { |
| 4062 | INTRABC_RECT_HITS.with(|c| c.get()) |
| 4063 | } |
| 4064 | |
| 4065 | /// Zeroes [`INTRABC_RECT_HITS`]/[`INTRABC_RECT_VARTX_HITS`]. |
| 4066 | #[allow(dead_code)] // read only from the `#[cfg(test)]` gates |
| 4067 | pub(crate) fn reset_intrabc_rect_hits() { |
| 4068 | INTRABC_RECT_HITS.with(|c| c.set(0)); |
| 4069 | INTRABC_RECT_VARTX_HITS.with(|c| c.set(0)); |
| 4070 | INTRABC_RECT_CODED_HITS.with(|c| c.set([0, 0])); |
| 4071 | INTRABC_RECT_SKIPPED_HITS.with(|c| c.set([0, 0])); |
| 4072 | } |
| 4073 | |
| 4074 | /// Current value of [`INTRABC_RECT_VARTX_HITS`]. |
| 4075 | #[allow(dead_code)] // read only from the `#[cfg(test)]` gates |
| 4076 | pub fn intrabc_rect_vartx_hits() -> usize { |
| 4077 | INTRABC_RECT_VARTX_HITS.with(|c| c.get()) |
| 4078 | } |
| 4079 | |
| 4080 | thread_local! { |
| 4081 | /// lane-av1-ibcrect: `use_intrabc` blocks on a 128-root HORZ/VERT strip |
| 4082 | /// (BLOCK_128X64 / BLOCK_64X128) whose reconstruction |
| 4083 | /// [`decode_intrabc_128rect`] ran -- the shape the named refusal used to |
| 4084 | /// guard (`decode_block_128rect`). A refused block bumps |
| 4085 | /// [`INTRABC_HITS`] but never this one. |
| 4086 | static INTRABC_128RECT_HITS: std::cell::Cell<usize> = const { std::cell::Cell::new(0) }; |
| 4087 | } |
| 4088 | |
| 4089 | /// Current value of [`INTRABC_128RECT_HITS`]. |
| 4090 | #[allow(dead_code)] // read only from the `#[cfg(test)]` gates |
| 4091 | pub fn intrabc_128rect_hits() -> usize { |
| 4092 | INTRABC_128RECT_HITS.with(|c| c.get()) |
| 4093 | } |
| 4094 | |
| 4095 | /// Zeroes [`INTRABC_128RECT_HITS`]. |
| 4096 | #[allow(dead_code)] // read only from the `#[cfg(test)]` gates |
| 4097 | pub(crate) fn reset_intrabc_128rect_hits() { |
| 4098 | INTRABC_128RECT_HITS.with(|c| c.set(0)); |
| 4099 | } |
| 4100 | |
| 4101 | /// Current value of [`INTRABC_VARTX_HITS`]. |
| 4102 | pub fn intrabc_vartx_hits() -> usize { |
| 4103 | INTRABC_VARTX_HITS.with(|c| c.get()) |
| 4104 | } |
| 4105 | |
| 4106 | /// Current value of [`INTRABC_VARTX_MIXED_LEAVES_HITS`]. |
| 4107 | pub fn intrabc_vartx_mixed_leaves_hits() -> usize { |
| 4108 | INTRABC_VARTX_MIXED_LEAVES_HITS.with(|c| c.get()) |
| 4109 | } |
| 4110 | |
| 4111 | /// Zeroes [`INTRABC_VARTX_HITS`] (gate tests call this before a decode). |
| 4112 | #[allow(dead_code)] // read only from the `#[cfg(test)]` gates |
| 4113 | pub(crate) fn reset_intrabc_vartx_hits() { |
| 4114 | INTRABC_VARTX_HITS.with(|c| c.set(0)); |
| 4115 | } |
| 4116 | |
| 4117 | /// Zeroes [`INTRABC_VARTX_MIXED_LEAVES_HITS`] (gate tests call this before a |
| 4118 | /// decode). |
| 4119 | #[allow(dead_code)] // read only from the `#[cfg(test)]` gates |
| 4120 | pub(crate) fn reset_intrabc_vartx_mixed_leaves_hits() { |
| 4121 | INTRABC_VARTX_MIXED_LEAVES_HITS.with(|c| c.set(0)); |
| 4122 | } |
| 4123 | |
| 4124 | /// Current value of [`RECT_INTRABC_VARTX_HITS`]. |
| 4125 | #[allow(dead_code)] // read only from the `#[cfg(test)]` gates |
| 4126 | pub fn rect_intrabc_vartx_hits() -> usize { |
| 4127 | RECT_INTRABC_VARTX_HITS.with(|c| c.get()) |
| 4128 | } |
| 4129 | |
| 4130 | /// Records one coded block into [`INTRABC_MI_GRID`]: every block contributes |
| 4131 | /// its size (libaom's `scan_row_mbmi` advances `processed_rows` off any |
| 4132 | /// candidate's `bsize`, ref-match or not), an intrabc one also its DV as an |
| 4133 | /// `INTRA_FRAME` candidate (libaom treats intrabc as `is_inter_block`). |
| 4134 | fn record_intrabc_mi(mi_r: usize, mi_c: usize, n4: usize, dv: Option<(i32, i32)>, n: &mut Neighbours, fctx: &crate::decode::FrameCtx) { |
| 4135 | record_intrabc_mi_rect(mi_r, mi_c, n4, n4, dv, n, fctx); |
| 4136 | } |
| 4137 | |
| 4138 | /// [`record_intrabc_mi`] with a true rectangular footprint: `av1_find_mv_refs` |
| 4139 | /// takes the block's real `bsize`, so a sub-8x8 `BLOCK_4X8`/`BLOCK_8X4` |
| 4140 | /// intrabc leaf must publish a 1x2/2x1 mi span, not a square (lane-av1txr). |
| 4141 | /// |
| 4142 | /// Also stamps libaom's `is_inter_block` verdict for this block into the |
| 4143 | /// `above_inter`/`left_inter` bands ([`tx_size_context_txfm_rect`]'s override |
| 4144 | /// term): `is_inter_block` is `is_intrabc_block(mbmi) || ref_frame[0] > |
| 4145 | /// INTRA_FRAME` (`blockd.h:373`), so an intrabc block is "inter" there and |
| 4146 | /// `get_tx_size_context` reads its BLOCK size rather than its transform band. |
| 4147 | /// The `false` half of the stamp is [`Neighbours::fill_lf_grid_rect`]'s, which |
| 4148 | /// every block runs; this runs after it (class leaked-band otherwise). |
| 4149 | fn record_intrabc_mi_rect( |
| 4150 | mi_r: usize, |
| 4151 | mi_c: usize, |
| 4152 | n4_w: usize, |
| 4153 | n4_h: usize, |
| 4154 | dv: Option<(i32, i32)>, |
| 4155 | n: &mut Neighbours, |
| 4156 | fctx: &crate::decode::FrameCtx, |
| 4157 | ) { |
| 4158 | if dv.is_some() { |
| 4159 | for i in 0..n4_w { |
| 4160 | if let Some(cell) = n.above_inter.get_mut(mi_c + i) { |
| 4161 | *cell = true; |
| 4162 | } |
| 4163 | } |
| 4164 | for i in 0..n4_h { |
| 4165 | if let Some(cell) = n.left_inter.get_mut(mi_r + i) { |
| 4166 | *cell = true; |
| 4167 | } |
| 4168 | } |
| 4169 | } |
| 4170 | fctx.intrabc_mi_grid.with(|g| { |
| 4171 | let mut g = g.borrow_mut(); |
| 4172 | let Some((grid, _, _)) = g.as_mut() else { |
| 4173 | return; |
| 4174 | }; |
| 4175 | let info = crate::mvstack::MiInfo { |
| 4176 | is_inter: dv.is_some(), |
| 4177 | ref_frame: 0, |
| 4178 | ref_frame1: NO_REF1, |
| 4179 | mv: mv16(dv.unwrap_or((0, 0))), |
| 4180 | mv1: (0, 0), |
| 4181 | is_new_mv: dv.is_some(), |
| 4182 | is_global_mv0: false, |
| 4183 | is_global_mv1: false, |
| 4184 | size: n4_w as u8, |
| 4185 | size_h: n4_h as u8, |
| 4186 | }; |
| 4187 | for r in mi_r..mi_r + n4_h { |
| 4188 | for c in mi_c..mi_c + n4_w { |
| 4189 | grid.set(r, c, info); |
| 4190 | } |
| 4191 | } |
| 4192 | }); |
| 4193 | } |
| 4194 | |
| 4195 | // How many blocks decoded `skip_mode == 1` (lane-av1comp round 14) -- |
| 4196 | // proves a real `skip_mode_present` stream actually reached a block that |
| 4197 | // picked it, not just that the frame header bit was set. |
| 4198 | thread_local! { |
| 4199 | static SKIP_MODE_HITS: std::cell::Cell<usize> = |
| 4200 | const { std::cell::Cell::new(0) }; |
| 4201 | } |
| 4202 | |
| 4203 | /// Current value of [`SKIP_MODE_HITS`]. |
| 4204 | #[allow(dead_code)] // read only from the `#[cfg(test)]` gates |
| 4205 | pub(crate) fn skip_mode_hits() -> usize { |
| 4206 | SKIP_MODE_HITS.with(|c| c.get()) |
| 4207 | } |
| 4208 | |
| 4209 | // lane-motionmode: how many blocks actually decoded `obmc_selected == true` |
| 4210 | // (as opposed to just reaching an eligible `read_motion_mode` symbol read) |
| 4211 | // -- the gate's own proof that a real aomenc `--enable-obmc=1` stream |
| 4212 | // reached [`obmc_blend`], not just the header bit. |
| 4213 | thread_local! { |
| 4214 | static OBMC_HITS: std::cell::Cell<usize> = |
| 4215 | const { std::cell::Cell::new(0) }; |
| 4216 | } |
| 4217 | |
| 4218 | // lane-fimv r1 (class wrong-alphabet-same-value): how many blocks took the |
| 4219 | // 2-symbol `obmc_cdf` alphabet *because* `force_integer_mv` (or a scaled |
| 4220 | // reference) vetoed `WARPED_CAUSAL` in libaom `motion_mode_allowed`, while |
| 4221 | // `allow_warped_motion == 1` and `num_proj_ref >= 1` would otherwise have |
| 4222 | // picked the 3-symbol `motion_mode_cdf`. These are exactly the blocks this |
| 4223 | // decoder used to read with the wrong alphabet, so a gate that asserts this |
| 4224 | // counter advanced proves the fixed path fired (class |
| 4225 | // `gate-blind-to-feature`). |
| 4226 | thread_local! { |
| 4227 | static FIMV_ALPHABET_HITS: std::cell::Cell<usize> = const { std::cell::Cell::new(0) }; |
| 4228 | } |
| 4229 | |
| 4230 | /// Current value of [`FIMV_ALPHABET_HITS`]. |
| 4231 | pub fn fimv_alphabet_hits() -> usize { |
| 4232 | FIMV_ALPHABET_HITS.with(|c| c.get()) |
| 4233 | } |
| 4234 | |
| 4235 | // lane-fimv r1: how many frame headers this decoder has decoded with |
| 4236 | // `force_integer_mv == 1` (bumped by `stream.rs` per frame). Lets a gate |
| 4237 | // prove the encoder really produced the header state under test rather than |
| 4238 | // assume it. |
| 4239 | thread_local! { |
| 4240 | static FIMV_FRAME_HITS: std::cell::Cell<usize> = const { std::cell::Cell::new(0) }; |
| 4241 | } |
| 4242 | |
| 4243 | /// Current value of [`FIMV_FRAME_HITS`]. |
| 4244 | pub fn fimv_frame_hits() -> usize { |
| 4245 | FIMV_FRAME_HITS.with(|c| c.get()) |
| 4246 | } |
| 4247 | |
| 4248 | /// Bump [`FIMV_FRAME_HITS`] -- called once per decoded frame header whose |
| 4249 | /// `force_integer_mv` bit is set. |
| 4250 | pub fn note_force_integer_mv_frame() { |
| 4251 | hit!(FIMV_FRAME_HITS); |
| 4252 | } |
| 4253 | |
| 4254 | /// Current value of [`OBMC_HITS`]. |
| 4255 | #[allow(dead_code)] // read only from the `#[cfg(test)]` gates |
| 4256 | pub(crate) fn obmc_hits() -> usize { |
| 4257 | OBMC_HITS.with(|c| c.get()) |
| 4258 | } |
| 4259 | |
| 4260 | // lane-t900 r27: OBMC blends that actually READ a recorded switchable filter |
| 4261 | // off the neighbour band -- the positive half of the proof for "an OBMC |
| 4262 | // neighbour whose switchable interp filter was never recorded". A |
| 4263 | // fixed-filter frame returns before this point (its neighbours legitimately |
| 4264 | // carry the `[3, 3]` sentinel), so every bump is a neighbour whose own |
| 4265 | // `interp_filter` symbols were published by `record_inter_rect_mi`. |
| 4266 | thread_local! { |
| 4267 | static OBMC_FILTER_BAND_READS: std::cell::Cell<usize> = const { std::cell::Cell::new(0) }; |
| 4268 | } |
| 4269 | |
| 4270 | /// Current value of [`OBMC_FILTER_BAND_READS`]. |
| 4271 | #[allow(dead_code)] // read only from the `#[cfg(test)]` gates |
| 4272 | pub(crate) fn obmc_filter_band_reads() -> usize { |
| 4273 | OBMC_FILTER_BAND_READS.with(|c| c.get()) |
| 4274 | } |
| 4275 | |
| 4276 | // lane-scaledref r1: per-case firing counts for the scaled-reference gate |
| 4277 | // (class `gate-blind-to-feature`) -- a pixel match under an UNSCALED |
| 4278 | // reference would pass just as well and prove nothing, so the gate |
| 4279 | // hard-asserts each combination this round lifts actually occurred: |
| 4280 | // a compound block with a scaled tap, a warp block whose warp libaom |
| 4281 | // suppresses because the reference is scaled, an OBMC block, an interintra |
| 4282 | // block, and an 8x8 leaf. |
| 4283 | thread_local! { |
| 4284 | static SCALED_COMPOUND_HITS: std::cell::Cell<usize> = const { std::cell::Cell::new(0) }; |
| 4285 | static SCALED_WARP_FALLBACK_HITS: std::cell::Cell<usize> = const { std::cell::Cell::new(0) }; |
| 4286 | static SCALED_OBMC_HITS: std::cell::Cell<usize> = const { std::cell::Cell::new(0) }; |
| 4287 | static SCALED_INTERINTRA_HITS: std::cell::Cell<usize> = const { std::cell::Cell::new(0) }; |
| 4288 | static SCALED_BLOCK8_HITS: std::cell::Cell<usize> = const { std::cell::Cell::new(0) }; |
| 4289 | static SCALED_WARP_SUPPRESSED_HITS: std::cell::Cell<usize> = const { std::cell::Cell::new(0) }; |
| 4290 | static MIXED_SCALE_COMPOUND_HITS: std::cell::Cell<usize> = const { std::cell::Cell::new(0) }; |
| 4291 | } |
| 4292 | |
| 4293 | /// Current value of [`MIXED_SCALE_COMPOUND_HITS`]: compound blocks with one |
| 4294 | /// scaled and one unscaled tap. |
| 4295 | #[allow(dead_code)] // read only from the `#[cfg(test)]` gates |
| 4296 | pub(crate) fn mixed_scale_compound_hits() -> usize { |
| 4297 | MIXED_SCALE_COMPOUND_HITS.with(|c| c.get()) |
| 4298 | } |
| 4299 | |
| 4300 | /// Current value of [`SCALED_WARP_SUPPRESSED_HITS`]: blocks that would have |
| 4301 | /// read the 3-symbol `motion_mode_cdf` but read the 2-symbol `obmc_cdf` |
| 4302 | /// instead because their reference is scaled (libaom `motion_mode_allowed`, |
| 4303 | /// `blockd.h:1484`). |
| 4304 | #[allow(dead_code)] // read only from the `#[cfg(test)]` gates |
| 4305 | pub(crate) fn scaled_warp_suppressed_hits() -> usize { |
| 4306 | SCALED_WARP_SUPPRESSED_HITS.with(|c| c.get()) |
| 4307 | } |
| 4308 | |
| 4309 | /// Current value of [`SCALED_COMPOUND_HITS`]. |
| 4310 | #[allow(dead_code)] // read only from the `#[cfg(test)]` gates |
| 4311 | pub(crate) fn scaled_compound_hits() -> usize { |
| 4312 | SCALED_COMPOUND_HITS.with(|c| c.get()) |
| 4313 | } |
| 4314 | |
| 4315 | /// Current value of [`SCALED_WARP_FALLBACK_HITS`]. |
| 4316 | pub fn scaled_warp_fallback_hits() -> usize { |
| 4317 | SCALED_WARP_FALLBACK_HITS.with(|c| c.get()) |
| 4318 | } |
| 4319 | |
| 4320 | /// Current value of [`SCALED_OBMC_HITS`]. |
| 4321 | #[allow(dead_code)] // read only from the `#[cfg(test)]` gates |
| 4322 | pub(crate) fn scaled_obmc_hits() -> usize { |
| 4323 | SCALED_OBMC_HITS.with(|c| c.get()) |
| 4324 | } |
| 4325 | |
| 4326 | /// Current value of [`SCALED_INTERINTRA_HITS`]. |
| 4327 | #[allow(dead_code)] // read only from the `#[cfg(test)]` gates |
| 4328 | pub(crate) fn scaled_interintra_hits() -> usize { |
| 4329 | SCALED_INTERINTRA_HITS.with(|c| c.get()) |
| 4330 | } |
| 4331 | |
| 4332 | /// Current value of [`SCALED_BLOCK8_HITS`]. |
| 4333 | #[allow(dead_code)] // read only from the `#[cfg(test)]` gates |
| 4334 | pub(crate) fn scaled_block8_hits() -> usize { |
| 4335 | SCALED_BLOCK8_HITS.with(|c| c.get()) |
| 4336 | } |
| 4337 | |
| 4338 | // lane-motionmode round 3: same proof as [`OBMC_HITS`], narrowed to |
| 4339 | // `decode_inter_block8`'s own 8x8-leaf `read_motion_mode` -- a subset of |
| 4340 | // [`OBMC_HITS`] (both fire together on an 8x8 hit), lets the gate tell an |
| 4341 | // 8x8-leaf OBMC block apart from a 16x16+ one. |
| 4342 | thread_local! { |
| 4343 | static OBMC_HITS_8: std::cell::Cell<usize> = const { std::cell::Cell::new(0) }; |
| 4344 | } |
| 4345 | |
| 4346 | // lane-interp3 r1: how many inter blocks read a SWITCHABLE `interp_filter` |
| 4347 | // pair whose two directions differ (`enable_dual_filter` only -- with dual |
| 4348 | // filter off both directions take the single symbol, so this can never fire). |
| 4349 | // A gate asserting it proves the stream really carries dual-filter blocks. |
| 4350 | thread_local! { |
| 4351 | static DUAL_FILTER_DIFF_HITS: std::cell::Cell<usize> = const { std::cell::Cell::new(0) }; |
| 4352 | } |
| 4353 | |
| 4354 | // lane-interp3 r1: how many COMPOUND 8x8 leaves read their own |
| 4355 | // `read_mb_interp_filter` symbol(s) -- the read this round added (the leaf |
| 4356 | // used to skip it entirely, losing a symbol and stamping the `[3, 3]` |
| 4357 | // no-filter sentinel that made a neighbouring OBMC block panic). |
| 4358 | thread_local! { |
| 4359 | static COMPOUND8_FILTER_HITS: std::cell::Cell<usize> = const { std::cell::Cell::new(0) }; |
| 4360 | } |
| 4361 | |
| 4362 | /// Current value of [`DUAL_FILTER_DIFF_HITS`]. |
| 4363 | #[allow(dead_code)] // read only from the `#[cfg(test)]` gates |
| 4364 | pub(crate) fn dual_filter_diff_hits() -> usize { |
| 4365 | DUAL_FILTER_DIFF_HITS.with(|c| c.get()) |
| 4366 | } |
| 4367 | |
| 4368 | /// Current value of [`COMPOUND8_FILTER_HITS`]. |
| 4369 | #[allow(dead_code)] // read only from the `#[cfg(test)]` gates |
| 4370 | pub(crate) fn compound8_filter_hits() -> usize { |
| 4371 | COMPOUND8_FILTER_HITS.with(|c| c.get()) |
| 4372 | } |
| 4373 | |
| 4374 | // lane-warp round 1: how many blocks resolved `motion_mode_allowed` to |
| 4375 | // `WARPED_CAUSAL`-eligible (3-symbol read, `num_proj_ref >= 1` under |
| 4376 | // `allow_warped_motion`) AND the symbol actually decoded to `WARPED_CAUSAL` |
| 4377 | // -- the gate's proof that a fixture really exercises the alphabet this |
| 4378 | // round changed, even though the block itself still refuses (warp |
| 4379 | // estimation/filter not ported). |
| 4380 | thread_local! { |
| 4381 | static WARP_SELECTED_HITS: std::cell::Cell<usize> = |
| 4382 | const { std::cell::Cell::new(0) }; |
| 4383 | } |
| 4384 | |
| 4385 | /// Current value of [`WARP_SELECTED_HITS`]. |
| 4386 | #[allow(dead_code)] // read only from the `#[cfg(test)]` gates |
| 4387 | pub(crate) fn warp_selected_hits() -> usize { |
| 4388 | WARP_SELECTED_HITS.with(|c| c.get()) |
| 4389 | } |
| 4390 | |
| 4391 | // lane-cwarp r1: how many COMPOUND_REFERENCE blocks had at least one |
| 4392 | // reference predicted through the per-ref GLOBAL warp (`av1_warp_plane` |
| 4393 | // into the compound intermediate) rather than translationally -- the |
| 4394 | // gate's proof that a `GLOBAL_GLOBALMV` block under a ROTZOOM/AFFINE model |
| 4395 | // really reached [`crate::warp::warp_affine_compound`]. |
| 4396 | thread_local! { |
| 4397 | static COMPOUND_WARP_HITS: std::cell::Cell<usize> = |
| 4398 | const { std::cell::Cell::new(0) }; |
| 4399 | } |
| 4400 | |
| 4401 | /// Current value of [`COMPOUND_WARP_HITS`]. |
| 4402 | #[allow(dead_code)] // read only from the `#[cfg(test)]` gates |
| 4403 | pub(crate) fn compound_warp_hits() -> usize { |
| 4404 | COMPOUND_WARP_HITS.with(|c| c.get()) |
| 4405 | } |
| 4406 | |
| 4407 | // lane-interp3 r3: the same proof narrowed to the 8x8 LEAF's own compound |
| 4408 | // arm ([`decode_inter_block8`]) -- that arm predicted both taps |
| 4409 | // translationally until r3, so a gate needs to tell a warped 8x8 compound |
| 4410 | // leaf apart from a warped 16x16+ one (both bump [`COMPOUND_WARP_HITS`]). |
| 4411 | thread_local! { |
| 4412 | static COMPOUND_WARP_HITS_8: std::cell::Cell<usize> = |
| 4413 | const { std::cell::Cell::new(0) }; |
| 4414 | } |
| 4415 | |
| 4416 | /// Current value of [`COMPOUND_WARP_HITS_8`]. |
| 4417 | #[allow(dead_code)] // read only from the `#[cfg(test)]` gates |
| 4418 | pub(crate) fn compound_warp_hits_8() -> usize { |
| 4419 | COMPOUND_WARP_HITS_8.with(|c| c.get()) |
| 4420 | } |
| 4421 | |
| 4422 | // lane-gmaffine r1: how many blocks built their prediction from a |
| 4423 | // SIX-parameter (`AFFINE`) global-motion model through |
| 4424 | // `crate::warp::global_warp_params` -- the gate's proof that a stream really |
| 4425 | // carried an AFFINE `global_motion_params` AND that a block was predicted |
| 4426 | // with it, not merely that the header parsed one. |
| 4427 | thread_local! { |
| 4428 | static AFFINE_GM_HITS: std::cell::Cell<usize> = |
| 4429 | const { std::cell::Cell::new(0) }; |
| 4430 | } |
| 4431 | |
| 4432 | /// Current value of [`AFFINE_GM_HITS`]. |
| 4433 | #[allow(dead_code)] // read only from the `#[cfg(test)]` gates |
| 4434 | pub(crate) fn affine_gm_hits() -> usize { |
| 4435 | AFFINE_GM_HITS.with(|c| c.get()) |
| 4436 | } |
| 4437 | |
| 4438 | // lane-t900 r9: rect blocks whose warp-sample top-right probe answers |
| 4439 | // differently at the block's WIDTH than at libaom's |
| 4440 | // `AOMMAX(xd->width, xd->height)` -- each one is a `num_proj_ref` that could |
| 4441 | // pick the wrong motion_mode alphabet. |
| 4442 | thread_local! { |
| 4443 | static TR_REACH_LONGER_SIDE_HITS: std::cell::Cell<usize> = const { std::cell::Cell::new(0) }; |
| 4444 | } |
| 4445 | |
| 4446 | /// Current value of [`TR_REACH_LONGER_SIDE_HITS`]. |
| 4447 | pub fn tr_reach_longer_side_hits() -> usize { |
| 4448 | TR_REACH_LONGER_SIDE_HITS.with(std::cell::Cell::get) |
| 4449 | } |
| 4450 | |
| 4451 | // lane-t900 r9: libaom `is_nontrans_global_motion` (mvref_common.h) returns 0 |
| 4452 | // for `AOMMIN(mi_size_wide[bsize], mi_size_high[bsize]) < 2`, i.e. any block |
| 4453 | // with a 4-px side is NEVER "non-translational global" and therefore ALWAYS |
| 4454 | // reads its interp-filter symbol. Counts the blocks where that guard flips |
| 4455 | // our answer -- a GLOBALMV/GLOBAL_GLOBALMV block with a non-TRANSLATION model |
| 4456 | // and a 4-px side, which we used to suppress the interp read on (a pre-mode |
| 4457 | // entropy desync one symbol wide). |
| 4458 | thread_local! { |
| 4459 | static GM_NONTRANS_SMALL_SIDE_HITS: std::cell::Cell<usize> = const { std::cell::Cell::new(0) }; |
| 4460 | } |
| 4461 | |
| 4462 | /// Current value of [`GM_NONTRANS_SMALL_SIDE_HITS`]. |
| 4463 | pub fn gm_nontrans_small_side_hits() -> usize { |
| 4464 | GM_NONTRANS_SMALL_SIDE_HITS.with(std::cell::Cell::get) |
| 4465 | } |
| 4466 | |
| 4467 | // lane-gmaffine r1: 8x8-leaf-only counters -- how many `BLOCK_8X8` leaves |
| 4468 | // coded `GLOBALMV` (`GLOBALMV_HITS_8`) and how many built a real |
| 4469 | // `WARPED_CAUSAL` local-warp prediction there (`WARP_HITS_8`, incremented |
| 4470 | // only once `find_projection` returned a usable model, unlike |
| 4471 | // `WARP_SELECTED_HITS` which counts the symbol). Both are the 8x8 gates' |
| 4472 | // proof that the leaf path itself fired, not the 16x16+ one. |
| 4473 | thread_local! { |
| 4474 | static GLOBALMV_HITS_8: std::cell::Cell<usize> = const { std::cell::Cell::new(0) }; |
| 4475 | static WARP_HITS_8: std::cell::Cell<usize> = const { std::cell::Cell::new(0) }; |
| 4476 | } |
| 4477 | |
| 4478 | /// Current value of [`GLOBALMV_HITS_8`]. |
| 4479 | #[allow(dead_code)] // read only from the `#[cfg(test)]` gates |
| 4480 | pub(crate) fn globalmv_hits_8() -> usize { |
| 4481 | GLOBALMV_HITS_8.with(|c| c.get()) |
| 4482 | } |
| 4483 | |
| 4484 | /// Current value of [`WARP_HITS_8`]. |
| 4485 | #[allow(dead_code)] // read only from the `#[cfg(test)]` gates |
| 4486 | pub(crate) fn warp_hits_8() -> usize { |
| 4487 | WARP_HITS_8.with(|c| c.get()) |
| 4488 | } |
| 4489 | |
| 4490 | // How many blocks decoded `interintra == 1` with the non-wedge blended |
| 4491 | // prediction applied (lane-interintra r1) -- the gate's proof that a |
| 4492 | // stream actually exercised interintra. |
| 4493 | thread_local! { |
| 4494 | static INTERINTRA_HITS: std::cell::Cell<usize> = |
| 4495 | const { std::cell::Cell::new(0) }; |
| 4496 | } |
| 4497 | |
| 4498 | /// Current value of [`INTERINTRA_HITS`]. |
| 4499 | #[allow(dead_code)] // read only from the `#[cfg(test)]` gates |
| 4500 | pub(crate) fn interintra_hits() -> usize { |
| 4501 | INTERINTRA_HITS.with(|c| c.get()) |
| 4502 | } |
| 4503 | |
| 4504 | // lane-midcut r1: of those, how many were RECTANGULAR (`write_w != write_h`) |
| 4505 | // -- the shape `interintra_blend` used to build a square `side x side` intra |
| 4506 | // predictor for, with `ii_size_scales`/`edges` taken at the square. Every |
| 4507 | // pre-existing interintra gate passed with that bug, so the rect arm needs |
| 4508 | // its own counter to stop being vacuous. |
| 4509 | thread_local! { |
| 4510 | static INTERINTRA_RECT_HITS: std::cell::Cell<usize> = const { std::cell::Cell::new(0) }; |
| 4511 | } |
| 4512 | |
| 4513 | /// Current value of [`INTERINTRA_RECT_HITS`]. |
| 4514 | pub fn interintra_rect_hits() -> usize { |
| 4515 | INTERINTRA_RECT_HITS.with(|c| c.get()) |
| 4516 | } |
| 4517 | |
| 4518 | // How many blocks decoded `comp_group_idx == 1` (masked COMPOUND_REFERENCE, |
| 4519 | // wedge or diffwtd) and had `compound_type`/`wedge_idx`/`wedge_sign`/ |
| 4520 | // `mask_type` consumed entropy-exact -- lane-maskcomp r1's proof that a |
| 4521 | // gate fixture actually exercises this alphabet, even though the block |
| 4522 | // still refuses (the mask blend itself is unported). |
| 4523 | thread_local! { |
| 4524 | static MASKED_COMPOUND_HITS: std::cell::Cell<usize> = |
| 4525 | const { std::cell::Cell::new(0) }; |
| 4526 | } |
| 4527 | |
| 4528 | /// Current value of [`MASKED_COMPOUND_HITS`]. |
| 4529 | #[allow(dead_code)] // read only from the `#[cfg(test)]` gates |
| 4530 | pub(crate) fn masked_compound_hits() -> usize { |
| 4531 | MASKED_COMPOUND_HITS.with(|c| c.get()) |
| 4532 | } |
| 4533 | |
| 4534 | // lane-intersub8 r4/r5: the NON-SQUARE half of `MASKED_COMPOUND_HITS` -- a |
| 4535 | // masked-compound block whose true footprint is rectangular, i.e. the case |
| 4536 | // whose `compound_type`/`wedge_idx` CDF row r4 fixed (it used to read the |
| 4537 | // square envelope's `BLOCK_SIZES_ALL` index). Square blocks cannot show that |
| 4538 | // defect, so a gate asserting `masked_compound_hits` alone stays vacuous for it. |
| 4539 | thread_local! { |
| 4540 | static RECT_MASKED_COMPOUND_HITS: std::cell::Cell<usize> = |
| 4541 | const { std::cell::Cell::new(0) }; |
| 4542 | } |
| 4543 | |
| 4544 | /// Current value of [`RECT_MASKED_COMPOUND_HITS`]. |
| 4545 | #[allow(dead_code)] // read only from the `#[cfg(test)]` gates |
| 4546 | pub(crate) fn rect_masked_compound_hits() -> usize { |
| 4547 | RECT_MASKED_COMPOUND_HITS.with(|c| c.get()) |
| 4548 | } |
| 4549 | |
| 4550 | // How many blocks decoded a real `COMPOUND_WEDGE` block (`comp_group_idx == |
| 4551 | // 1`, `compound_type == 0`) -- lane-wedge r3's proof a gate fixture |
| 4552 | // actually exercised the wedge blend, not just the DIFFWTD half of |
| 4553 | // `MASKED_COMPOUND_HITS`. |
| 4554 | thread_local! { |
| 4555 | static WEDGE_HITS: std::cell::Cell<usize> = |
| 4556 | const { std::cell::Cell::new(0) }; |
| 4557 | } |
| 4558 | |
| 4559 | /// Current value of [`WEDGE_HITS`]. |
| 4560 | #[allow(dead_code)] // read only from the `#[cfg(test)]` gates |
| 4561 | pub(crate) fn wedge_hits() -> usize { |
| 4562 | WEDGE_HITS.with(|c| c.get()) |
| 4563 | } |
| 4564 | |
| 4565 | // How many blocks decoded a real wedge-INTERINTRA block |
| 4566 | // (`interintra == 1 && use_wedge_interintra == 1`) -- lane-wii r2's proof |
| 4567 | // a gate fixture exercised the wedge blend (fixed sign 0), not just the |
| 4568 | // smooth arm counted by `INTERINTRA_HITS`. |
| 4569 | thread_local! { |
| 4570 | static WII_HITS: std::cell::Cell<usize> = |
| 4571 | const { std::cell::Cell::new(0) }; |
| 4572 | } |
| 4573 | |
| 4574 | /// Current value of [`WII_HITS`]. |
| 4575 | #[allow(dead_code)] // read only from the `#[cfg(test)]` gates |
| 4576 | pub(crate) fn wii_hits() -> usize { |
| 4577 | WII_HITS.with(|c| c.get()) |
| 4578 | } |
| 4579 | |
| 4580 | // lane-sbrect10 r1: how many INTRA blocks read `uv_mode` off the 13-symbol |
| 4581 | // NO-CFL alphabet *because their size excludes CFL* (libaom `is_cfl_allowed`: |
| 4582 | // wide <= 32 && high <= 32). The witness that a gate stream really carries a |
| 4583 | // 64-axis intra block -- the shape whose 14-symbol read desynced every inter |
| 4584 | // frame carrying one. |
| 4585 | thread_local! { |
| 4586 | static NOCFL_UV_MODE_HITS: std::cell::Cell<usize> = const { std::cell::Cell::new(0) }; |
| 4587 | } |
| 4588 | |
| 4589 | /// Current value of [`NOCFL_UV_MODE_HITS`]. |
| 4590 | #[allow(dead_code)] // read only from the `#[cfg(test)]` gates |
| 4591 | pub(crate) fn nocfl_uv_mode_hits() -> usize { |
| 4592 | NOCFL_UV_MODE_HITS.with(|c| c.get()) |
| 4593 | } |
| 4594 | |
| 4595 | /// lane-inter16ab r5: the six RECTANGULAR wedge shapes, in this order -- |
| 4596 | /// the counter slots of [`RECT_WEDGE_HITS`]/[`RECT_WII_HITS`]. |
| 4597 | pub(crate) const RECT_WEDGE_SHAPES: [(usize, usize); 6] = |
| 4598 | [(8, 16), (16, 8), (16, 32), (32, 16), (8, 32), (32, 8)]; |
| 4599 | |
| 4600 | fn rect_wedge_slot(bw: usize, bh: usize) -> Option<usize> { |
| 4601 | RECT_WEDGE_SHAPES.iter().position(|&s| s == (bw, bh)) |
| 4602 | } |
| 4603 | |
| 4604 | // lane-inter16ab r5: how many COMPOUND_WEDGE / wedge-INTERINTRA blocks used a |
| 4605 | // RECTANGULAR codebook row (hgtw/hltw), per shape -- the gate's proof that |
| 4606 | // the rect wedge masks lifted this round actually fired, not just the square |
| 4607 | // ones already counted by `WEDGE_HITS`/`WII_HITS`. |
| 4608 | thread_local! { |
| 4609 | static RECT_WEDGE_HITS: std::cell::Cell<[usize; 6]> = const { std::cell::Cell::new([0; 6]) }; |
| 4610 | static RECT_WII_HITS: std::cell::Cell<[usize; 6]> = const { std::cell::Cell::new([0; 6]) }; |
| 4611 | } |
| 4612 | |
| 4613 | /// Current value of [`RECT_WEDGE_HITS`], indexed by [`RECT_WEDGE_SHAPES`]. |
| 4614 | pub(crate) fn rect_wedge_hits() -> [usize; 6] { |
| 4615 | RECT_WEDGE_HITS.with(|c| c.get()) |
| 4616 | } |
| 4617 | |
| 4618 | /// Current value of [`RECT_WII_HITS`], indexed by [`RECT_WEDGE_SHAPES`]. |
| 4619 | pub(crate) fn rect_wii_hits() -> [usize; 6] { |
| 4620 | RECT_WII_HITS.with(|c| c.get()) |
| 4621 | } |
| 4622 | |
| 4623 | // lane-inter4 r4: how many 16x8/8x16 INTER LEAVES actually ran the OBMC |
| 4624 | // blend -- the gate's proof that `--enable-obmc=1` reached this shape (the |
| 4625 | // r3 refusal's replacement: prefilt frames are bit-exact vs an instrumented |
| 4626 | // aomdec, neighbour list included). |
| 4627 | thread_local! { |
| 4628 | static OBMC_RECT_LEAF_HITS: std::cell::Cell<usize> = const { std::cell::Cell::new(0) }; |
| 4629 | } |
| 4630 | |
| 4631 | /// Current value of [`OBMC_RECT_LEAF_HITS`]. |
| 4632 | pub(crate) fn obmc_rect_leaf_hits() -> usize { |
| 4633 | OBMC_RECT_LEAF_HITS.with(|c| c.get()) |
| 4634 | } |
| 4635 | |
| 4636 | // lane-obmcrec r1: how many OBMC neighbours were taken from the SECOND half of |
| 4637 | // a 4-wide/4-tall (1:4 strip) chroma pair -- the case whose interp filter used |
| 4638 | // to be read from the pair's FIRST mi, which is a different block and, when |
| 4639 | // that half is intra, carries the `[3, 3]` "no filter" sentinel (the three |
| 4640 | // 1080p 10-bit film cuts refused there). The gate's proof the shape fired. |
| 4641 | thread_local! { |
| 4642 | static OBMC_PAIR_FILTER_HITS: std::cell::Cell<usize> = const { std::cell::Cell::new(0) }; |
| 4643 | } |
| 4644 | |
| 4645 | /// Current value of [`OBMC_PAIR_FILTER_HITS`]. |
| 4646 | pub(crate) fn obmc_pair_filter_hits() -> usize { |
| 4647 | OBMC_PAIR_FILTER_HITS.with(|c| c.get()) |
| 4648 | } |
| 4649 | |
| 4650 | // lane-t900 r14: how many OBMC neighbour predictions run PAST the frame edge |
| 4651 | // -- libaom's `op_mi_size` is `AOMMIN(xd->width|height, mi_step)`, the block's |
| 4652 | // own side, so a block overhanging the bottom/right still predicts the full |
| 4653 | // neighbour strip (the destination write is what clips). We used to shrink the |
| 4654 | // prediction to the visible part, which at 4 chroma rows/cols also swaps the |
| 4655 | // vertical/horizontal kernel to the 4-tap one. The gate's proof the shape fired. |
| 4656 | thread_local! { |
| 4657 | static OBMC_EDGE_SPAN_HITS: std::cell::Cell<usize> = const { std::cell::Cell::new(0) }; |
| 4658 | } |
| 4659 | |
| 4660 | /// Current value of [`OBMC_EDGE_SPAN_HITS`]. |
| 4661 | #[allow(dead_code)] // read only from the `#[cfg(test)]` gates |
| 4662 | pub(crate) fn obmc_edge_span_hits() -> usize { |
| 4663 | OBMC_EDGE_SPAN_HITS.with(|c| c.get()) |
| 4664 | } |
| 4665 | |
| 4666 | /// Current value of [`OBMC_HITS_8`]. |
| 4667 | #[allow(dead_code)] // read only from the `#[cfg(test)]` gates |
| 4668 | pub(crate) fn obmc_hits_8() -> usize { |
| 4669 | OBMC_HITS_8.with(|c| c.get()) |
| 4670 | } |
| 4671 | |
| 4672 | // lane-rectgate r1: how many 32x32 quadrants actually decoded a |
| 4673 | // `PARTITION_HORZ_B` split (the one extended/ab partition this decoder's |
| 4674 | // inter-frame tile loop decodes -- `PARTITION_HORZ`/`VERT`/`HORZ_A`/`VERT_A`/ |
| 4675 | // `VERT_B` all still fall into the generic "a partition type this encoder |
| 4676 | // never writes" refusal below) -- the gate's proof that a free-partition |
| 4677 | // aomenc stream actually exercised a non-`NONE`/`SPLIT` partition rather than |
| 4678 | // coincidentally never selecting one. |
| 4679 | thread_local! { |
| 4680 | static EXTENDED_PARTITION_HITS: std::cell::Cell<usize> = |
| 4681 | const { std::cell::Cell::new(0) }; |
| 4682 | } |
| 4683 | |
| 4684 | /// Current value of [`EXTENDED_PARTITION_HITS`]. |
| 4685 | #[allow(dead_code)] // read only from the `#[cfg(test)]` gates |
| 4686 | pub(crate) fn extended_partition_hits() -> usize { |
| 4687 | EXTENDED_PARTITION_HITS.with(|c| c.get()) |
| 4688 | } |
| 4689 | |
| 4690 | // lane-partitions r1: how many 32x32 quadrants decoded a real |
| 4691 | // `PARTITION_HORZ`/`PARTITION_VERT` (two true rectangular strips, unlike |
| 4692 | // `PARTITION_HORZ_B`'s square-context stand-in) -- the free-partition |
| 4693 | // gate's proof this round's arms actually fired. |
| 4694 | thread_local! { |
| 4695 | static RECT_PARTITION_HITS: std::cell::Cell<usize> = |
| 4696 | const { std::cell::Cell::new(0) }; |
| 4697 | } |
| 4698 | |
| 4699 | /// Current value of [`RECT_PARTITION_HITS`]. |
| 4700 | #[allow(dead_code)] // read only from the `#[cfg(test)]` gates |
| 4701 | pub(crate) fn rect_partition_hits() -> usize { |
| 4702 | RECT_PARTITION_HITS.with(|c| c.get()) |
| 4703 | } |
| 4704 | |
| 4705 | // lane-inter8 r1: how many superblocks an inter tile coded as a single |
| 4706 | // whole-SB `PARTITION_NONE` 64x64 inter block (the case aomenc picks for |
| 4707 | // static content, previously a named refusal). |
| 4708 | thread_local! { |
| 4709 | /// lane-sb128c r1: how many 128x128 `PARTITION_NONE` roots this INTER tile |
| 4710 | /// path decoded as one whole block (its gate's own witness). |
| 4711 | pub(crate) static INTER_SB128_NONE_HITS: std::cell::Cell<usize> = const { std::cell::Cell::new(0) }; |
| 4712 | /// 128x64 / 64x128 INTER blocks decoded at the 128 root (lane-sb128c r7). |
| 4713 | pub(crate) static INTER_SB128_HORZ_HITS: std::cell::Cell<usize> = const { std::cell::Cell::new(0) }; |
| 4714 | pub(crate) static INTER_SB128_VERT_HITS: std::cell::Cell<usize> = const { std::cell::Cell::new(0) }; |
| 4715 | } |
| 4716 | |
| 4717 | /// Current value of [`INTER_SB128_HORZ_HITS`], for a gate's before/after delta. |
| 4718 | pub(crate) fn inter_sb128_horz_hits() -> usize { |
| 4719 | INTER_SB128_HORZ_HITS.with(std::cell::Cell::get) |
| 4720 | } |
| 4721 | |
| 4722 | /// Current value of [`INTER_SB128_VERT_HITS`], for a gate's before/after delta. |
| 4723 | pub(crate) fn inter_sb128_vert_hits() -> usize { |
| 4724 | INTER_SB128_VERT_HITS.with(std::cell::Cell::get) |
| 4725 | } |
| 4726 | |
| 4727 | /// Current value of [`INTER_SB128_NONE_HITS`], for a gate's before/after delta. |
| 4728 | pub fn inter_sb128_none_hits() -> usize { |
| 4729 | INTER_SB128_NONE_HITS.with(std::cell::Cell::get) |
| 4730 | } |
| 4731 | |
| 4732 | thread_local! { |
| 4733 | /// lane-sb128c r3: how many INTRA-coded 128x128 blocks inside an INTER |
| 4734 | /// frame this decoder reconstructed (mu-chunk chroma, four TX_64X64 luma |
| 4735 | /// units) -- the gate's witness that the lifted refusal's case fired. |
| 4736 | pub(crate) static INTRA_128_IN_INTER_HITS: std::cell::Cell<usize> = const { std::cell::Cell::new(0) }; |
| 4737 | } |
| 4738 | |
| 4739 | /// Current value of [`INTRA_128_IN_INTER_HITS`], for a gate's before/after delta. |
| 4740 | pub fn intra_128_in_inter_hits() -> usize { |
| 4741 | INTRA_128_IN_INTER_HITS.with(std::cell::Cell::get) |
| 4742 | } |
| 4743 | |
| 4744 | thread_local! { |
| 4745 | static INTER_SB_NONE_HITS: std::cell::Cell<usize> = |
| 4746 | const { std::cell::Cell::new(0) }; |
| 4747 | } |
| 4748 | |
| 4749 | /// Current value of [`INTER_SB_NONE_HITS`]. |
| 4750 | #[allow(dead_code)] // read only from the `#[cfg(test)]` gates |
| 4751 | pub(crate) fn inter_sb_none_hits() -> usize { |
| 4752 | INTER_SB_NONE_HITS.with(|c| c.get()) |
| 4753 | } |
| 4754 | |
| 4755 | // lane-inter8 r1: how many INTERIOR (not frame-edge-straddling) 16x16 inter |
| 4756 | // blocks coded a real `PARTITION_SPLIT` into four 8x8 leaves -- the straddle |
| 4757 | // path has always run that loop, so only this counter proves the newly-lifted |
| 4758 | // alphabet value fired. |
| 4759 | thread_local! { |
| 4760 | static INTER_SUB16_SPLIT_HITS: std::cell::Cell<usize> = |
| 4761 | const { std::cell::Cell::new(0) }; |
| 4762 | } |
| 4763 | |
| 4764 | /// Current value of [`INTER_SUB16_SPLIT_HITS`]. |
| 4765 | #[allow(dead_code)] // read only from the `#[cfg(test)]` gates |
| 4766 | pub(crate) fn inter_sub16_split_hits() -> usize { |
| 4767 | INTER_SUB16_SPLIT_HITS.with(|c| c.get()) |
| 4768 | } |
| 4769 | |
| 4770 | // lane-t900 r28: how many 16x16 inter splits reached their four 8x8 leaves |
| 4771 | // while at least one reference picture was SCALED (superres) -- the counter |
| 4772 | // behind the lifted "8x8 partition leaf under a scaled reference" refusal. |
| 4773 | thread_local! { |
| 4774 | static SCALED_LEAF8_HITS: std::cell::Cell<usize> = |
| 4775 | const { std::cell::Cell::new(0) }; |
| 4776 | } |
| 4777 | |
| 4778 | /// Current value of [`SCALED_LEAF8_HITS`]. |
| 4779 | pub fn scaled_leaf8_hits() -> usize { |
| 4780 | SCALED_LEAF8_HITS.with(|c| c.get()) |
| 4781 | } |
| 4782 | |
| 4783 | // lane-t900 r28: how many sub-8x8 inter blocks (the 4x4 split and the 8x4/4x8 |
| 4784 | // rect pair) were predicted from a SCALED reference -- the counter behind the |
| 4785 | // lifted "sub-8x8 inter block under a scaled reference" refusal. |
| 4786 | thread_local! { |
| 4787 | static SCALED_SUB8_HITS: std::cell::Cell<usize> = |
| 4788 | const { std::cell::Cell::new(0) }; |
| 4789 | } |
| 4790 | |
| 4791 | /// Current value of [`SCALED_SUB8_HITS`]. |
| 4792 | pub fn scaled_sub8_hits() -> usize { |
| 4793 | SCALED_SUB8_HITS.with(|c| c.get()) |
| 4794 | } |
| 4795 | |
| 4796 | // lane-rectwire r2: how many `PARTITION_HORZ`/`VERT` strips actually decoded |
| 4797 | // real (non-skip) coefficients through [`read_coeffs_rect`] -- proves the |
| 4798 | // rect coefficient reader itself fired, not just the strip-level partition |
| 4799 | // symbol [`RECT_PARTITION_HITS`] already counts. |
| 4800 | thread_local! { |
| 4801 | static RECT_COEFF_HITS: std::cell::Cell<usize> = |
| 4802 | const { std::cell::Cell::new(0) }; |
| 4803 | } |
| 4804 | |
| 4805 | /// Current value of [`RECT_COEFF_HITS`]. |
| 4806 | #[allow(dead_code)] // read only from the `#[cfg(test)]` gates |
| 4807 | pub(crate) fn rect_coeff_hits() -> usize { |
| 4808 | RECT_COEFF_HITS.with(|c| c.get()) |
| 4809 | } |
| 4810 | |
| 4811 | // lane-rectx r1: how many below-16x16 HORZ/VERT leaves ([`decode_leaf_rect`]) |
| 4812 | // decoded real (non-skip) TX_16X8/TX_8X16 coefficients -- the counter the |
| 4813 | // charter's real-aomenc gate hard-asserts. |
| 4814 | thread_local! { |
| 4815 | static RECT_LEAF_COEFF_HITS: std::cell::Cell<usize> = |
| 4816 | const { std::cell::Cell::new(0) }; |
| 4817 | } |
| 4818 | |
| 4819 | /// Current value of [`RECT_LEAF_COEFF_HITS`]. |
| 4820 | #[allow(dead_code)] // read only from the `#[cfg(test)]` gates |
| 4821 | pub(crate) fn rect_leaf_coeff_hits() -> usize { |
| 4822 | RECT_LEAF_COEFF_HITS.with(|c| c.get()) |
| 4823 | } |
| 4824 | |
| 4825 | // lane-intrarect r1: how many intra-coded HORZ/VERT strips decoded on the |
| 4826 | // INTER block path ([`decode_intra_rect_in_inter`]), per shape class -- |
| 4827 | // [0] the superblock-level 64x32/32x64, [1] 32x16/16x32, [2] 16x8/8x16. |
| 4828 | // Every one of them was the named refusal "an intra-coded HORZ/VERT strip |
| 4829 | // needs rectangular intra prediction" before this round. |
| 4830 | thread_local! { |
| 4831 | static INTRA_RECT_IN_INTER_HITS: std::cell::Cell<[usize; 3]> = |
| 4832 | const { std::cell::Cell::new([0; 3]) }; |
| 4833 | } |
| 4834 | |
| 4835 | /// Current value of [`INTRA_RECT_IN_INTER_HITS`] for one shape class |
| 4836 | /// (0 = 64x32/32x64, 1 = 32x16/16x32, 2 = 16x8/8x16). |
| 4837 | #[allow(dead_code)] // read only from the `#[cfg(test)]` gates |
| 4838 | pub(crate) fn intra_rect_strip_in_inter_hits(class: usize) -> usize { |
| 4839 | INTRA_RECT_IN_INTER_HITS.with(|c| c.get()[class]) |
| 4840 | } |
| 4841 | |
| 4842 | // lane-rect64port r1 (ported by hand from lane-intra64split 5ffbaf3): coded |
| 4843 | // (non-skip) 64-level HORZ/VERT intra strips whose luma transform is the |
| 4844 | // un-split TX_64X32/TX_32X64 -- the corner-cropped 32x32 coefficient read of |
| 4845 | // [`decode_rect_split`], the shape both 10-bit 3840x1608 film cuts stop on. |
| 4846 | // Split by ORIENTATION (index 0 = 64x32 wide / HORZ, 1 = 32x64 tall / VERT): |
| 4847 | // a single counter cannot tell an axis swap from a working arm |
| 4848 | // (class [[scan-weights-cross-axis]]), so the gate asserts both cells. |
| 4849 | thread_local! { |
| 4850 | static RECT64_CORNER_TU_HITS: std::cell::Cell<[usize; 2]> = |
| 4851 | const { std::cell::Cell::new([0; 2]) }; |
| 4852 | } |
| 4853 | |
| 4854 | /// Current value of [`RECT64_CORNER_TU_HITS`] for one orientation |
| 4855 | /// (0 = 64x32, 1 = 32x64). |
| 4856 | pub(crate) fn rect64_corner_tu_hits(orient: usize) -> usize { |
| 4857 | RECT64_CORNER_TU_HITS.with(|c| c.get()[orient]) |
| 4858 | } |
| 4859 | |
| 4860 | // lane-intrasplit r1: intra HORZ/VERT strips inside an INTER frame whose luma |
| 4861 | // transform is SPLIT, bucketed by the read `tx_depth` (index 1 and 2; index 0 |
| 4862 | // is never incremented) -- the gate's proof that the lifted path ran per depth. |
| 4863 | thread_local! { |
| 4864 | static INTRA_RECT_IN_INTER_SPLIT_TX_HITS: std::cell::Cell<[usize; 3]> = |
| 4865 | const { std::cell::Cell::new([0; 3]) }; |
| 4866 | } |
| 4867 | |
| 4868 | // lane-t900 r21: rect intra strips inside an inter frame that read the |
| 4869 | // screen-content palette syntax (`allow_screen_content_tools` set and the |
| 4870 | // strip's own shape admitted by `palette_bsize_ctx_wh`) -- the site a blanket |
| 4871 | // refusal used to guard, so this counter is what keeps its witness gate |
| 4872 | // non-vacuous. |
| 4873 | thread_local! { |
| 4874 | static INTRA_RECT_IN_INTER_SCREEN_HITS: std::cell::Cell<usize> = const { std::cell::Cell::new(0) }; |
| 4875 | } |
| 4876 | |
| 4877 | /// Current value of [`INTRA_RECT_IN_INTER_SCREEN_HITS`]. |
| 4878 | #[allow(dead_code)] // read only from the `#[cfg(test)]` gates |
| 4879 | pub(crate) fn intra_rect_in_inter_screen_hits() -> usize { |
| 4880 | INTRA_RECT_IN_INTER_SCREEN_HITS.with(std::cell::Cell::get) |
| 4881 | } |
| 4882 | |
| 4883 | /// Resets [`INTRA_RECT_IN_INTER_SCREEN_HITS`] before a decode. |
| 4884 | #[allow(dead_code)] // read only from the `#[cfg(test)]` gates |
| 4885 | pub(crate) fn reset_intra_rect_in_inter_screen_hits() { |
| 4886 | INTRA_RECT_IN_INTER_SCREEN_HITS.with(|c| c.set(0)); |
| 4887 | } |
| 4888 | |
| 4889 | /// Current value of [`INTRA_RECT_IN_INTER_SPLIT_TX_HITS`], indexed by tx depth. |
| 4890 | #[allow(dead_code)] // read only from the `#[cfg(test)]` gates |
| 4891 | pub(crate) fn intra_rect_in_inter_split_tx_hits() -> [usize; 3] { |
| 4892 | INTRA_RECT_IN_INTER_SPLIT_TX_HITS.with(std::cell::Cell::get) |
| 4893 | } |
| 4894 | |
| 4895 | // lane-av1txbands: how many `tx_depth`/`txfm_split` reads the deblock-grid |
| 4896 | // approximation would have resolved off a DIFFERENT CDF row than the real |
| 4897 | // `TXFM_CONTEXT` bands do -- the exact blocks this band read fixes, on ANY |
| 4898 | // frame (lane-midcut r2 had it inter-frame-only). A plain "rect/intra block" |
| 4899 | // tally is green on the old code too (class gate-blind-to-feature). |
| 4900 | thread_local! { |
| 4901 | static TXCTX_DEBLOCK_DIVERGENCE_HITS: std::cell::Cell<usize> = |
| 4902 | const { std::cell::Cell::new(0) }; |
| 4903 | } |
| 4904 | |
| 4905 | /// Current value of [`TXCTX_DEBLOCK_DIVERGENCE_HITS`]. |
| 4906 | #[allow(dead_code)] // gate counter accessor with no reader yet; the counter itself is live |
| 4907 | pub(crate) fn txctx_deblock_divergence_hits() -> usize { |
| 4908 | TXCTX_DEBLOCK_DIVERGENCE_HITS.with(std::cell::Cell::get) |
| 4909 | } |
| 4910 | |
| 4911 | // lane-intra14 r1: how many intra-coded 1:4 strips decoded on the INTER |
| 4912 | // block path, per shape -- [0] 64x16, [1] 16x64, [2] 32x8, [3] 8x32. Every |
| 4913 | // one of them was the named refusal "an intra-coded 1:4 (or other non-2:1) |
| 4914 | // rect strip on the inter block path" before this round. |
| 4915 | thread_local! { |
| 4916 | static INTRA_RECT4_IN_INTER_HITS: std::cell::Cell<[usize; 4]> = |
| 4917 | const { std::cell::Cell::new([0; 4]) }; |
| 4918 | } |
| 4919 | |
| 4920 | /// Current value of [`INTRA_RECT4_IN_INTER_HITS`] for one shape |
| 4921 | /// (0 = 64x16, 1 = 16x64, 2 = 32x8, 3 = 8x32). |
| 4922 | pub(crate) fn intra_rect4_strip_in_inter_hits(shape: usize) -> usize { |
| 4923 | INTRA_RECT4_IN_INTER_HITS.with(|c| c.get()[shape]) |
| 4924 | } |
| 4925 | |
| 4926 | // lane-intra14 r1: set while an INTRA block of an INTER frame is decoded |
| 4927 | // through one of the key-frame rect readers ([`decode_block_rect4`], |
| 4928 | // [`decode_block_rect64`]). `Some((size_group, skip))` tells |
| 4929 | // [`read_intra_mode_rect`] that (a) `skip`/segment id/cdef/delta-q were |
| 4930 | // already read by [`decode_inter_block`] and (b) the luma mode comes off |
| 4931 | // `y_mode_cdf[size_group_lookup[bsize]]` (libaom `read_intra_block_mode_info`, |
| 4932 | // decodemv.c:1189), never the key frame's above/left `kf_y_mode` pair. |
| 4933 | |
| 4934 | // lane-rectx r5: how many times a NON-strip `kf_y_mode` reader |
| 4935 | // ([`decode_block`], [`decode_block_rect`], [`decode_block_rect64`]) took an |
| 4936 | // above/left mode from the mi-exact map that DIFFERS from its coarse 16x16 |
| 4937 | // slot -- i.e. how often the r5 defect would have picked the wrong CDF row. |
| 4938 | thread_local! { |
| 4939 | static MODE_MI_OVERRIDE_HITS: std::cell::Cell<usize> = |
| 4940 | const { std::cell::Cell::new(0) }; |
| 4941 | } |
| 4942 | |
| 4943 | /// Current value of [`MODE_MI_OVERRIDE_HITS`]. |
| 4944 | #[allow(dead_code)] // read only from the `#[cfg(test)]` gates |
| 4945 | pub(crate) fn mode_mi_override_hits() -> usize { |
| 4946 | MODE_MI_OVERRIDE_HITS.with(|c| c.get()) |
| 4947 | } |
| 4948 | |
| 4949 | // lane-modectx r1: how many times a `kf_y_mode`/`y_mode` neighbour read at a |
| 4950 | // TILE edge found its coarse fallback band holding a NON-DC mode -- i.e. a |
| 4951 | // mode from the previous tile, where libaom's `up_available`/`left_available` |
| 4952 | // make the neighbour DC_PRED. Measured 0 over 20 two-tile-column streams: |
| 4953 | // `above_mode` is cleared per tile in `Neighbours::start_tile` and `left_mode` |
| 4954 | // per superblock row in `start_row`, so the luma fallback cannot leak the way |
| 4955 | // lane-mtfix's `above_uv_mode` one did. The gate asserts it stays 0 -- this is |
| 4956 | // the invariant that makes the coarse fallback safe. |
| 4957 | thread_local! { |
| 4958 | static MODE_TILE_EDGE_COARSE_LEAKS: std::cell::Cell<usize> = |
| 4959 | const { std::cell::Cell::new(0) }; |
| 4960 | } |
| 4961 | |
| 4962 | /// Current value of [`MODE_TILE_EDGE_COARSE_LEAKS`]. |
| 4963 | #[allow(dead_code)] // read only from the `#[cfg(test)]` gates |
| 4964 | pub(crate) fn mode_tile_edge_coarse_leaks() -> usize { |
| 4965 | MODE_TILE_EDGE_COARSE_LEAKS.with(|c| c.get()) |
| 4966 | } |
| 4967 | |
| 4968 | // lane-cfl r1: how many times a chroma intra-edge filter-type read |
| 4969 | // (`get_filt_type`, libaom reconintra.c) took an above/left UV mode from the |
| 4970 | // mi-exact map that DIFFERS from its coarse 16x16 slot -- i.e. how often the |
| 4971 | // r1 defect would have filtered a chroma edge with the wrong strength. |
| 4972 | thread_local! { |
| 4973 | static UV_MODE_MI_OVERRIDE_HITS: std::cell::Cell<usize> = |
| 4974 | const { std::cell::Cell::new(0) }; |
| 4975 | } |
| 4976 | |
| 4977 | /// Current value of [`UV_MODE_MI_OVERRIDE_HITS`]. |
| 4978 | #[allow(dead_code)] // read only from the `#[cfg(test)]` gates |
| 4979 | pub(crate) fn uv_mode_mi_override_hits() -> usize { |
| 4980 | UV_MODE_MI_OVERRIDE_HITS.with(|c| c.get()) |
| 4981 | } |
| 4982 | |
| 4983 | // lane-t900 r12: how many chroma edge-filter-type reads took their above/left |
| 4984 | // `uv_mode` from the mi GRID where the one-slot-per-column map would have |
| 4985 | // answered differently -- the sub-8x8 pair case where the pair's own first |
| 4986 | // half had already overwritten the column slot the chroma-ref half reads. |
| 4987 | thread_local! { |
| 4988 | static UV_MODE_GRID_OVERRIDE_HITS: std::cell::Cell<usize> = |
| 4989 | const { std::cell::Cell::new(0) }; |
| 4990 | } |
| 4991 | |
| 4992 | /// Current value of [`UV_MODE_GRID_OVERRIDE_HITS`]. |
| 4993 | pub fn uv_mode_grid_override_hits() -> usize { |
| 4994 | UV_MODE_GRID_OVERRIDE_HITS.with(|c| c.get()) |
| 4995 | } |
| 4996 | |
| 4997 | // lane-t900 r8: how many intra edge-filter-type reads (luma + chroma) the |
| 4998 | // INTER neighbour stamp saved: the coarse SUB band still held a SMOOTH mode |
| 4999 | // from an earlier block in that pixel row/column while the mi-exact map -- |
| 5000 | // which an inter block only started writing this round -- correctly says the |
| 5001 | // neighbour is not smooth (libaom `is_smooth`: an inter neighbour's `mode` is |
| 5002 | // an INTER mode, and its `uv_mode` "is not set for inter blocks"). |
| 5003 | thread_local! { |
| 5004 | static INTER_NEIGHBOUR_NOT_SMOOTH_HITS: std::cell::Cell<usize> = |
| 5005 | const { std::cell::Cell::new(0) }; |
| 5006 | } |
| 5007 | |
| 5008 | /// Current value of [`INTER_NEIGHBOUR_NOT_SMOOTH_HITS`]. |
| 5009 | #[allow(dead_code)] // read only from the `#[cfg(test)]` gates |
| 5010 | pub(crate) fn inter_neighbour_not_smooth_hits() -> usize { |
| 5011 | INTER_NEIGHBOUR_NOT_SMOOTH_HITS.with(|c| c.get()) |
| 5012 | } |
| 5013 | |
| 5014 | // lane-mtfix r1: how many times a chroma intra-edge filter-type read was |
| 5015 | // suppressed at a TILE edge that is not a frame edge -- the above/left |
| 5016 | // `uv_mode` neighbour of a block on a tile's first mi row/column lives in |
| 5017 | // another tile, so libaom's `above_mbmi`/`left_mbmi` are NULL there. The |
| 5018 | // coarse [`SUB`] maps are frame-sized and (`above_uv_mode`) not cleared per |
| 5019 | // tile, so without the guard in [`Neighbours::smooth_uv_neighbour`] the tile |
| 5020 | // above would decide this tile's chroma edge filter strength. |
| 5021 | thread_local! { |
| 5022 | static UV_TILE_EDGE_SUPPRESSED_HITS: std::cell::Cell<usize> = |
| 5023 | const { std::cell::Cell::new(0) }; |
| 5024 | } |
| 5025 | |
| 5026 | /// Current value of [`UV_TILE_EDGE_SUPPRESSED_HITS`]. |
| 5027 | #[allow(dead_code)] // read only from the `#[cfg(test)]` gates |
| 5028 | pub(crate) fn uv_tile_edge_suppressed_hits() -> usize { |
| 5029 | UV_TILE_EDGE_SUPPRESSED_HITS.with(|c| c.get()) |
| 5030 | } |
| 5031 | |
| 5032 | // lane-cfl r1 gate counters: `UV_CFL_PRED` blocks (every `cfl_alpha_signs` |
| 5033 | // read) and chroma blocks with a NONZERO `angle_delta_uv` -- the two block |
| 5034 | // shapes the r1 chroma defect showed up on. |
| 5035 | thread_local! { |
| 5036 | static CFL_BLOCK_HITS: std::cell::Cell<usize> = const { std::cell::Cell::new(0) }; |
| 5037 | static UV_ANGLE_DELTA_HITS: std::cell::Cell<usize> = const { std::cell::Cell::new(0) }; |
| 5038 | } |
| 5039 | |
| 5040 | /// Current value of [`CFL_BLOCK_HITS`]. |
| 5041 | #[allow(dead_code)] // read only from the `#[cfg(test)]` gates |
| 5042 | pub(crate) fn cfl_block_hits() -> usize { |
| 5043 | CFL_BLOCK_HITS.with(|c| c.get()) |
| 5044 | } |
| 5045 | |
| 5046 | /// Current value of [`UV_ANGLE_DELTA_HITS`]. |
| 5047 | #[allow(dead_code)] // read only from the `#[cfg(test)]` gates |
| 5048 | pub(crate) fn uv_angle_delta_hits() -> usize { |
| 5049 | UV_ANGLE_DELTA_HITS.with(|c| c.get()) |
| 5050 | } |
| 5051 | |
| 5052 | // lane-sbpart r2: how many superblock-level `PARTITION_HORZ`/`PARTITION_VERT` |
| 5053 | // blocks (two true 64x32/32x64 strips, [`decode_block_rect64`]) fired -- the |
| 5054 | // gate's proof this round's arms actually reached a real block, not just |
| 5055 | // that `partition_w64` read one of those two symbol values. |
| 5056 | thread_local! { |
| 5057 | static SB_RECT_HITS: std::cell::Cell<usize> = |
| 5058 | const { std::cell::Cell::new(0) }; |
| 5059 | } |
| 5060 | |
| 5061 | // Current value of [`SB_RECT_HITS`]. |
| 5062 | // lane-hgkf r1: the frame-edge partition bit, one level each. Slot 0 = a |
| 5063 | // 64-level gathered edge bit read as HORZ/VERT, 1 = the same bit read as |
| 5064 | // SPLIT, 2/3 = the 32-level pair. A gate asserting slot 0 or 2 proves the |
| 5065 | // non-SPLIT edge arm actually decoded (before this round the bit was read and |
| 5066 | // thrown away, so that arm was unreachable). |
| 5067 | thread_local! { |
| 5068 | static EDGE_PART_HITS: std::cell::Cell<[usize; 4]> = const { std::cell::Cell::new([0; 4]) }; |
| 5069 | } |
| 5070 | |
| 5071 | /// Current value of [`EDGE_PART_HITS`]. |
| 5072 | #[allow(dead_code)] // read only from the `#[cfg(test)]` gates |
| 5073 | pub(crate) fn edge_part_hits() -> [usize; 4] { |
| 5074 | EDGE_PART_HITS.with(|c| c.get()) |
| 5075 | } |
| 5076 | |
| 5077 | /// Reset [`EDGE_PART_HITS`] (a gate counts one stream at a time). |
| 5078 | #[allow(dead_code)] // read only from the `#[cfg(test)]` gates |
| 5079 | pub(crate) fn reset_edge_part_hits() { |
| 5080 | EDGE_PART_HITS.with(|c| c.set([0; 4])); |
| 5081 | } |
| 5082 | |
| 5083 | fn bump_edge_part(slot: usize) { |
| 5084 | hit_do! { |
| 5085 | EDGE_PART_HITS.with(|c| { |
| 5086 | let mut h = c.get(); |
| 5087 | h[slot] += 1; |
| 5088 | c.set(h); |
| 5089 | }); |
| 5090 | } |
| 5091 | } |
| 5092 | |
| 5093 | #[allow(dead_code)] // read only from the `#[cfg(test)]` gates |
| 5094 | pub(crate) fn sb_rect_hits() -> usize { |
| 5095 | SB_RECT_HITS.with(|c| c.get()) |
| 5096 | } |
| 5097 | |
| 5098 | // lane-golomb r2: the 32x32-level FRAME-EDGE partition. Slot 0/1 = the |
| 5099 | // gathered edge bit read as 0 (HORZ/VERT) / 1 (SPLIT); slot 2 = a 32-level |
| 5100 | // PARTITION_HORZ decoded with `has_rows32 == false` (only the top 32x16 half |
| 5101 | // is coded), slot 3 = a PARTITION_VERT with `has_cols32 == false`. Both tile |
| 5102 | // paths (intra and inter) feed the same counter. |
| 5103 | // |
| 5104 | // lane-golomb r3: slots 4..7 are the same four facts one level up, at the |
| 5105 | // SUPERBLOCK (64x64) edge -- 4/5 = the gathered 64-level bit read as |
| 5106 | // HORZ/VERT / SPLIT, 6 = a 64x32 bottom-edge strip decoded with |
| 5107 | // `has_rows == false`, 7 = a 32x64 right-edge strip. Real aomenc answers a |
| 5108 | // bottom-edge superblock with the 64-level HORZ (measured: 192x80 flat-band, |
| 5109 | // every SB of the last row), so the 32-level slots stay 0 on that content and |
| 5110 | // only these prove the arm fired. |
| 5111 | thread_local! { |
| 5112 | static EDGE32_HITS: std::cell::Cell<[usize; 8]> = |
| 5113 | const { std::cell::Cell::new([0; 8]) }; |
| 5114 | } |
| 5115 | |
| 5116 | /// Current value of [`EDGE32_HITS`]. |
| 5117 | #[allow(dead_code)] // read only from the `#[cfg(test)]` gates |
| 5118 | pub(crate) fn edge32_hits() -> [usize; 8] { |
| 5119 | EDGE32_HITS.with(|c| c.get()) |
| 5120 | } |
| 5121 | |
| 5122 | /// [`bump_edge32`] of `base + bit`, returning the bit -- the gathered edge |
| 5123 | /// reads are `if`-conditions, so the counter has to ride the expression. |
| 5124 | fn bump_edge32_bit(base: usize, bit: usize) -> usize { |
| 5125 | bump_edge32(base + bit.min(1)); |
| 5126 | bit |
| 5127 | } |
| 5128 | |
| 5129 | fn bump_edge32(slot: usize) { |
| 5130 | hit_do! { |
| 5131 | EDGE32_HITS.with(|c| { |
| 5132 | let mut h = c.get(); |
| 5133 | h[slot] += 1; |
| 5134 | c.set(h) |
| 5135 | }); |
| 5136 | } |
| 5137 | } |
| 5138 | |
| 5139 | // lane-part32 r4: how many superblock-level AB blocks (`PARTITION_HORZ_A`/ |
| 5140 | // `_B`/`VERT_A`/`_B` at 64x64 -- two 32x32 squares plus one 64x32/32x64 |
| 5141 | // strip) fired. Real aomenc picks these at 64 even with |
| 5142 | // `--enable-ab-partitions=0` (lane-sbpart r2's own measurement), so this is |
| 5143 | // the counter the gate hard-asserts. |
| 5144 | thread_local! { |
| 5145 | static SB_AB_HITS: std::cell::Cell<[usize; 4]> = |
| 5146 | const { std::cell::Cell::new([0; 4]) }; |
| 5147 | } |
| 5148 | |
| 5149 | /// Current value of [`SB_AB_HITS`], summed over the four arms. |
| 5150 | #[allow(dead_code)] // read only from the `#[cfg(test)]` gates |
| 5151 | pub(crate) fn sb_ab_hits() -> usize { |
| 5152 | SB_AB_HITS.with(|c| c.get().iter().sum()) |
| 5153 | } |
| 5154 | |
| 5155 | /// [`SB_AB_HITS`] split per arm, in `PARTITION_HORZ_A`, `_HORZ_B`, |
| 5156 | /// `_VERT_A`, `_VERT_B` order -- what the gate hard-asserts arm by arm, so a |
| 5157 | /// run where aomenc's RD only ever picked (say) `HORZ_A` cannot pass off as |
| 5158 | /// proof of all four. |
| 5159 | #[allow(dead_code)] // read only from the `#[cfg(test)]` gates |
| 5160 | pub(crate) fn sb_ab_hits_by_arm() -> [usize; 4] { |
| 5161 | SB_AB_HITS.with(std::cell::Cell::get) |
| 5162 | } |
| 5163 | |
| 5164 | // lane-tx64x16: the two 1:4 superblock arms, counted per orientation -- |
| 5165 | // `PARTITION_HORZ_4` (four 64x16 strips) and `PARTITION_VERT_4` (four 16x64 |
| 5166 | // strips). Two counters, not one, because a gate that only proved "some 1:4 |
| 5167 | // strip fired" would pass on a stream that never coded the other axis, and |
| 5168 | // every scan/context table this round added comes in a transposed pair. |
| 5169 | thread_local! { |
| 5170 | static SB_RECT4_HORZ_HITS: std::cell::Cell<usize> = const { std::cell::Cell::new(0) }; |
| 5171 | static SB_RECT4_VERT_HITS: std::cell::Cell<usize> = const { std::cell::Cell::new(0) }; |
| 5172 | } |
| 5173 | |
| 5174 | /// Current value of `SB_RECT4_HORZ_HITS` (64x16 strips decoded). |
| 5175 | #[allow(dead_code)] // read only from the `#[cfg(test)]` gates |
| 5176 | pub(crate) fn sb_rect4_horz_hits() -> usize { |
| 5177 | SB_RECT4_HORZ_HITS.with(|c| c.get()) |
| 5178 | } |
| 5179 | |
| 5180 | /// Current value of `SB_RECT4_VERT_HITS` (16x64 strips decoded). |
| 5181 | #[allow(dead_code)] // read only from the `#[cfg(test)]` gates |
| 5182 | pub(crate) fn sb_rect4_vert_hits() -> usize { |
| 5183 | SB_RECT4_VERT_HITS.with(|c| c.get()) |
| 5184 | } |
| 5185 | |
| 5186 | // lane-tx64x16 r3: the 32x32-level 1:4 arms, counted per orientation -- |
| 5187 | // `PARTITION_HORZ_4` (four 32x8 strips) and `PARTITION_VERT_4` (four 8x32 |
| 5188 | // strips), the arms the ReelB mid-film extract stops at. |
| 5189 | thread_local! { |
| 5190 | static RECT4_32_HORZ_HITS: std::cell::Cell<usize> = const { std::cell::Cell::new(0) }; |
| 5191 | static RECT4_32_VERT_HITS: std::cell::Cell<usize> = const { std::cell::Cell::new(0) }; |
| 5192 | /// lane-inter4 r1: the INTER-frame twins of the two counters above -- |
| 5193 | /// four 32x8 / 8x32 inter strips of a 32x32-level `PARTITION_HORZ_4` / |
| 5194 | /// `PARTITION_VERT_4`. |
| 5195 | static RECT4_32_HORZ_INTER_HITS: std::cell::Cell<usize> = const { std::cell::Cell::new(0) }; |
| 5196 | static RECT4_32_VERT_INTER_HITS: std::cell::Cell<usize> = const { std::cell::Cell::new(0) }; |
| 5197 | /// lane-inter4 r1: superblock-level inter `PARTITION_HORZ`/`PARTITION_VERT` |
| 5198 | /// strips (64x32 / 32x64) and their 1:4 pair (64x16 / 16x64). |
| 5199 | static INTER_SB_HORZ_HITS: std::cell::Cell<usize> = const { std::cell::Cell::new(0) }; |
| 5200 | static INTER_SB_VERT_HITS: std::cell::Cell<usize> = const { std::cell::Cell::new(0) }; |
| 5201 | static INTER_SB_HORZ4_HITS: std::cell::Cell<usize> = const { std::cell::Cell::new(0) }; |
| 5202 | static INTER_SB_VERT4_HITS: std::cell::Cell<usize> = const { std::cell::Cell::new(0) }; |
| 5203 | } |
| 5204 | |
| 5205 | // How many 32x32-level 1:4 strips actually read coefficients (non-skip): |
| 5206 | // a gate that only counted strips would pass on an all-skip stream, which |
| 5207 | // exercises none of the new scan/context tables. |
| 5208 | thread_local! { |
| 5209 | static RECT4_COEFF_HITS: std::cell::Cell<usize> = const { std::cell::Cell::new(0) }; |
| 5210 | } |
| 5211 | |
| 5212 | // lane-rectsplitx r1: 1:4 strips (32x8/8x32/64x16/16x64) decoded through the |
| 5213 | // per-transform-unit split path, split by tx depth -- depth 1 is a 2:1 RECT |
| 5214 | // unit (TX_16X8/TX_32X16), depth 2 a square one, and the two INTERLEAVE in |
| 5215 | // every real stream, so a gate must prove both fired. `SUB16_SPLIT_HITS` |
| 5216 | // counts the same for a 16x8/8x16 strip below 16x16. |
| 5217 | thread_local! { |
| 5218 | static RECT4_SPLIT_DEPTH1_HITS: std::cell::Cell<usize> = const { std::cell::Cell::new(0) }; |
| 5219 | static RECT4_SPLIT_DEPTH2_HITS: std::cell::Cell<usize> = const { std::cell::Cell::new(0) }; |
| 5220 | static SUB16_SPLIT_HITS: std::cell::Cell<usize> = const { std::cell::Cell::new(0) }; |
| 5221 | } |
| 5222 | |
| 5223 | /// Current values of `RECT4_SPLIT_DEPTH1_HITS` / `RECT4_SPLIT_DEPTH2_HITS`. |
| 5224 | #[allow(dead_code)] // read only from the `#[cfg(test)]` gates |
| 5225 | pub(crate) fn rect4_split_depth_hits() -> (usize, usize) { |
| 5226 | ( |
| 5227 | RECT4_SPLIT_DEPTH1_HITS.with(|c| c.get()), |
| 5228 | RECT4_SPLIT_DEPTH2_HITS.with(|c| c.get()), |
| 5229 | ) |
| 5230 | } |
| 5231 | |
| 5232 | /// Current value of `SUB16_SPLIT_HITS`. |
| 5233 | #[allow(dead_code)] // read only from the `#[cfg(test)]` gates |
| 5234 | pub(crate) fn sub16_split_hits() -> usize { |
| 5235 | SUB16_SPLIT_HITS.with(|c| c.get()) |
| 5236 | } |
| 5237 | |
| 5238 | // lane-frame36 r2: superblock-level strips ([`decode_block_rect64`]) whose |
| 5239 | // luma transform is SPLIT and which therefore leave through that fn's early |
| 5240 | // return -- the arm that published no `TXFM_CONTEXT` band until this round. |
| 5241 | thread_local! { |
| 5242 | static RECT64_SPLIT_TXFM_PUBLISH_HITS: std::cell::Cell<usize> = const { std::cell::Cell::new(0) }; |
| 5243 | } |
| 5244 | |
| 5245 | /// Current value of `RECT64_SPLIT_TXFM_PUBLISH_HITS`. |
| 5246 | #[allow(dead_code)] // read only from the `#[cfg(test)]` gates |
| 5247 | pub(crate) fn rect64_split_txfm_publish_hits() -> usize { |
| 5248 | RECT64_SPLIT_TXFM_PUBLISH_HITS.with(|c| c.get()) |
| 5249 | } |
| 5250 | |
| 5251 | /// Current value of `RECT4_COEFF_HITS`. |
| 5252 | pub(crate) fn rect4_coeff_hits() -> usize { |
| 5253 | RECT4_COEFF_HITS.with(|c| c.get()) |
| 5254 | } |
| 5255 | |
| 5256 | // lane-sbab r1: how many superblock-level (64x64) AB partitions fired IN AN |
| 5257 | // INTER frame, per arm, in `PARTITION_HORZ_A`, `_HORZ_B`, `_VERT_A`, |
| 5258 | // `_VERT_B` order (the intra-frame counterpart is `SB_AB_HITS`). Each arm is |
| 5259 | // two 32x32 inter squares plus one true 64x32/32x64 inter strip; the gate |
| 5260 | // asserts the arms it proves separately so a run whose RD only picked one |
| 5261 | // cannot pass as proof of the others. |
| 5262 | thread_local! { |
| 5263 | static SB_AB_INTER_HITS: std::cell::Cell<[usize; 4]> = const { std::cell::Cell::new([0; 4]) }; |
| 5264 | } |
| 5265 | |
| 5266 | /// [`SB_AB_INTER_HITS`] per arm (HORZ_A, HORZ_B, VERT_A, VERT_B at 64x64 in an |
| 5267 | /// inter frame). |
| 5268 | pub fn sb_ab_inter_hits_by_arm() -> [usize; 4] { |
| 5269 | SB_AB_INTER_HITS.with(std::cell::Cell::get) |
| 5270 | } |
| 5271 | |
| 5272 | // lane-inter16ab r1: how many 16x16-level AB partitions fired IN AN INTER |
| 5273 | // frame, per arm, in `PARTITION_HORZ_A`, `_HORZ_B`, `_VERT_A`, `_VERT_B` |
| 5274 | // order (the intra-frame counterpart is `AB16_HITS`). Each arm is two 8x8 |
| 5275 | // inter leaves (`decode_inter_block8`) plus one true 16x8/8x16 inter strip |
| 5276 | // (`decode_inter_block` with `write_w`/`write_h`); the gate asserts every |
| 5277 | // arm separately so a run whose RD only picked one cannot pass as proof of |
| 5278 | // four. |
| 5279 | thread_local! { |
| 5280 | static AB16_INTER_HITS: std::cell::Cell<[usize; 4]> = const { std::cell::Cell::new([0; 4]) }; |
| 5281 | } |
| 5282 | |
| 5283 | /// [`AB16_INTER_HITS`] per arm (HORZ_A, HORZ_B, VERT_A, VERT_B at 16x16 in an |
| 5284 | /// inter frame). |
| 5285 | pub fn ab16_inter_hits_by_arm() -> [usize; 4] { |
| 5286 | AB16_INTER_HITS.with(std::cell::Cell::get) |
| 5287 | } |
| 5288 | |
| 5289 | // lane-inter16ab r2: the 16x16-level 1:4 pair as INTER blocks -- four 16x4 |
| 5290 | // (`PARTITION_HORZ_4`) or 4x16 (`PARTITION_VERT_4`) strips. `CHROMA_PAIRS` |
| 5291 | // counts the odd strips that close a 4:2:0 chroma pair |
| 5292 | // (`is_chroma_reference`, av1_common_int.h:1454: bh == 1 mi => odd mi_row), |
| 5293 | // `SUB8_PIECES` the pair chroma blocks whose TOP (LEFT) half was built from |
| 5294 | // the PREVIOUS strip's own mv/ref/filters -- libaom |
| 5295 | // `build_inter_predictors_sub8x8` (reconinter_template.inc:87-160), reached |
| 5296 | // through `is_sub8x8_inter` (same file, :54). |
| 5297 | thread_local! { |
| 5298 | static INTER16_HORZ4_HITS: std::cell::Cell<usize> = const { std::cell::Cell::new(0) }; |
| 5299 | static INTER16_VERT4_HITS: std::cell::Cell<usize> = const { std::cell::Cell::new(0) }; |
| 5300 | static INTER16_CHROMA_PAIR_HITS: std::cell::Cell<usize> = const { std::cell::Cell::new(0) }; |
| 5301 | static INTER16_SUB8_PIECE_HITS: std::cell::Cell<usize> = const { std::cell::Cell::new(0) }; |
| 5302 | } |
| 5303 | |
| 5304 | // lane-intra16x4: INTRA-coded strips of an inter 16x16-level 1:4 partition -- |
| 5305 | // [0] 16x4, [1] 4x16, [2] the odd (chroma-reference) ones, which carry the |
| 5306 | // pair's single 8x4 (4x8) chroma unit for BOTH strips. |
| 5307 | thread_local! { |
| 5308 | static INTRA16X4_IN_INTER_HITS: std::cell::Cell<[usize; 3]> = |
| 5309 | const { std::cell::Cell::new([0; 3]) }; |
| 5310 | } |
| 5311 | |
| 5312 | /// `(intra 16x4 strips, intra 4x16 strips, of which chroma-reference)` decoded |
| 5313 | /// inside an INTER 16x16-level 1:4 partition (lane-intra16x4). |
| 5314 | pub fn intra16x4_in_inter_hits() -> (usize, usize, usize) { |
| 5315 | let v = INTRA16X4_IN_INTER_HITS.with(std::cell::Cell::get); |
| 5316 | (v[0], v[1], v[2]) |
| 5317 | } |
| 5318 | |
| 5319 | /// Current values of the inter 16x16-level 1:4 counters: `(16x4 strips, 4x16 |
| 5320 | /// strips, chroma pairs closed, chroma pairs built from two different |
| 5321 | /// strips' motion vectors)`. |
| 5322 | pub fn inter16_rect4_counters() -> (usize, usize, usize, usize) { |
| 5323 | ( |
| 5324 | INTER16_HORZ4_HITS.with(std::cell::Cell::get), |
| 5325 | INTER16_VERT4_HITS.with(std::cell::Cell::get), |
| 5326 | INTER16_CHROMA_PAIR_HITS.with(std::cell::Cell::get), |
| 5327 | INTER16_SUB8_PIECE_HITS.with(std::cell::Cell::get), |
| 5328 | ) |
| 5329 | } |
| 5330 | |
| 5331 | /// One 16x4 / 4x16 inter strip's chroma situation, handed to |
| 5332 | /// [`decode_inter_block`] out of band (the same one-shot thread-local idiom |
| 5333 | /// `REDUCED_TX_SET_INTER` uses) rather than through twenty-three call sites |
| 5334 | /// that all mean "an ordinary block, chroma of its own". |
| 5335 | #[derive(Clone, Copy)] |
| 5336 | pub(crate) struct InterStripChroma { |
| 5337 | /// `is_chroma_reference` for this strip: false on strips 0 and 2, which |
| 5338 | /// read and reconstruct NO chroma at all. |
| 5339 | has_chroma: bool, |
| 5340 | /// `PARTITION_HORZ_4` (16x4 strips) rather than `PARTITION_VERT_4`. |
| 5341 | horz: bool, |
| 5342 | /// The mi position of the EVEN strip of this pair -- `setup_pred_plane`'s |
| 5343 | /// `if (ss_y && (mi_row & 1) && mi_size_high[bsize] == 1) mi_row -= 1`, |
| 5344 | /// i.e. the origin of the 8x4 (4x8) chroma transform. |
| 5345 | pair_mi: (usize, usize), |
| 5346 | /// The previous (even) strip's single-reference MC parameters, `None` |
| 5347 | /// when it was intra or compound -- `is_sub8x8_inter` then returns false |
| 5348 | /// and the whole chroma block is predicted from THIS strip's mv. |
| 5349 | prev: Option<(i8, (i32, i32), mc::InterpFilterKind, mc::InterpFilterKind)>, |
| 5350 | } |
| 5351 | |
| 5352 | /// The single-reference MC parameters of the inter block that decoded |
| 5353 | /// last -- read by the next 1:4 strip to build its pair's chroma. |
| 5354 | |
| 5355 | fn bump_ab16_inter(arm: usize) { |
| 5356 | hit_do! { |
| 5357 | AB16_INTER_HITS.with(|c| { |
| 5358 | let mut v = c.get(); |
| 5359 | v[arm] += 1; |
| 5360 | c.set(v); |
| 5361 | }); |
| 5362 | } |
| 5363 | } |
| 5364 | |
| 5365 | // lane-r14: the 16x16-level 1:4 pair -- four 16x4 (`PARTITION_HORZ_4`) or |
| 5366 | // 4x16 (`PARTITION_VERT_4`) strips, `CHROMA` counting the odd strips that |
| 5367 | // close a 4:2:0 chroma pair and `COEFF` the strips with a coded residual. |
| 5368 | thread_local! { |
| 5369 | static RECT4_16_HORZ_HITS: std::cell::Cell<usize> = const { std::cell::Cell::new(0) }; |
| 5370 | static RECT4_16_VERT_HITS: std::cell::Cell<usize> = const { std::cell::Cell::new(0) }; |
| 5371 | static RECT4_16_CHROMA_HITS: std::cell::Cell<usize> = const { std::cell::Cell::new(0) }; |
| 5372 | static RECT4_16_COEFF_HITS: std::cell::Cell<usize> = const { std::cell::Cell::new(0) }; |
| 5373 | static RECT4_16_SPLIT_HITS: std::cell::Cell<usize> = const { std::cell::Cell::new(0) }; |
| 5374 | /// lane-kf1200: pairs whose chroma edge-filter type read from the PAIR's |
| 5375 | /// base mi differs from what the strip's own (odd) mi would have said -- |
| 5376 | /// the shape that made 4 chroma samples of the ReelB `-ss 1200` |
| 5377 | /// key frame miss by 1. Zero here means a gate never exercised the fix. |
| 5378 | static RECT4_16_UV_PAIR_FILT_HITS: std::cell::Cell<usize> = const { std::cell::Cell::new(0) }; |
| 5379 | /// lane-av1-rect14: lossless 16x4/4x16 chroma pairs walked as two TX_4X4s. |
| 5380 | static RECT4_16_LOSSLESS_CHROMA_HITS: std::cell::Cell<usize> = const { std::cell::Cell::new(0) }; |
| 5381 | /// lane-av1-rect14: intrabc reconstructions of a 16x4/4x16 pair strip. |
| 5382 | static RECT4_16_INTRABC_HITS: std::cell::Cell<usize> = const { std::cell::Cell::new(0) }; |
| 5383 | /// lane-kf1200 r2: sub-8x8 chroma blocks that are `skip_txfm` AND |
| 5384 | /// `UV_CFL_PRED` -- the arm that used to drop the CfL alpha entirely. |
| 5385 | static SKIP_CFL_HITS: std::cell::Cell<usize> = const { std::cell::Cell::new(0) }; |
| 5386 | /// lane-av1-loss64: chroma transform units the frame-edge clip skipped |
| 5387 | /// (libaom `decode_reconstruct_tx`'s `max_blocks_wide/high` test) in |
| 5388 | /// [`decode_block`]'s multi-transform-unit walk -- the units a lossless |
| 5389 | /// block overhanging the frame does NOT code. |
| 5390 | static CHROMA_EDGE_TU_CLIP_HITS: std::cell::Cell<usize> = const { std::cell::Cell::new(0) }; |
| 5391 | /// lane-av1-loss64: skipped lossless intrabc rect strips whose luma |
| 5392 | /// entropy bands were zeroed per unit (libaom `av1_reset_entropy_context`, |
| 5393 | /// decodeframe.c:1262) instead of leaking the previous occupant's. |
| 5394 | static SKIP_LOSSLESS_BAND_RESET_HITS: std::cell::Cell<usize> = const { std::cell::Cell::new(0) }; |
| 5395 | } |
| 5396 | |
| 5397 | /// How many skipped sub-8x8 chroma blocks carried a CfL alpha (the shape |
| 5398 | /// that reconstructed as flat DC before lane-kf1200 r2). |
| 5399 | pub fn skip_cfl_hits() -> usize { |
| 5400 | SKIP_CFL_HITS.with(|c| c.get()) |
| 5401 | } |
| 5402 | |
| 5403 | /// How many chroma transform units the frame-edge clip skipped in |
| 5404 | /// [`decode_block`]'s multi-transform-unit walk (lane-av1-loss64). Zero on a |
| 5405 | /// stream whose lossless blocks never overhang the frame -- a gate asserting |
| 5406 | /// the clip fired must see this nonzero. |
| 5407 | pub fn chroma_edge_tu_clip_hits() -> usize { |
| 5408 | CHROMA_EDGE_TU_CLIP_HITS.with(|c| c.get()) |
| 5409 | } |
| 5410 | |
| 5411 | /// How many skipped lossless intrabc rect strips had their luma entropy |
| 5412 | /// bands zeroed per unit (lane-av1-loss64). |
| 5413 | pub fn skip_lossless_band_reset_hits() -> usize { |
| 5414 | SKIP_LOSSLESS_BAND_RESET_HITS.with(|c| c.get()) |
| 5415 | } |
| 5416 | |
| 5417 | /// Gate-side reset for the two lane-av1-loss64 counters above. |
| 5418 | pub fn reset_loss64_hits() { |
| 5419 | CHROMA_EDGE_TU_CLIP_HITS.with(|c| c.set(0)); |
| 5420 | SKIP_LOSSLESS_BAND_RESET_HITS.with(|c| c.set(0)); |
| 5421 | } |
| 5422 | |
| 5423 | /// How many 16x4/4x16 chroma pairs took a different `intra_edge_filter_type` |
| 5424 | /// from the pair's base mi than the strip's own mi would have given. |
| 5425 | pub fn rect4_16_uv_pair_filt_hits() -> usize { |
| 5426 | RECT4_16_UV_PAIR_FILT_HITS.with(|c| c.get()) |
| 5427 | } |
| 5428 | |
| 5429 | /// How many lossless 16x4/4x16 chroma pairs walked two TX_4X4s per plane. |
| 5430 | pub fn rect4_16_lossless_chroma_hits() -> usize { |
| 5431 | RECT4_16_LOSSLESS_CHROMA_HITS.with(|c| c.get()) |
| 5432 | } |
| 5433 | |
| 5434 | /// How many 16x4/4x16 strips reconstructed as intra block copy. |
| 5435 | pub fn rect4_16_intrabc_hits() -> usize { |
| 5436 | RECT4_16_INTRABC_HITS.with(|c| c.get()) |
| 5437 | } |
| 5438 | |
| 5439 | /// Zero the lane-av1-rect14 pair counters so a gate measures one stream. |
| 5440 | pub fn reset_rect4_16_pair_hits() { |
| 5441 | RECT4_16_LOSSLESS_CHROMA_HITS.with(|c| c.set(0)); |
| 5442 | RECT4_16_INTRABC_HITS.with(|c| c.set(0)); |
| 5443 | } |
| 5444 | |
| 5445 | // lane-reelckf r1: the two chroma defects ReelC's key frames exposed -- |
| 5446 | // CHROMA_SKIP_CFL counts skipped intra blocks whose chroma is `UV_CFL_PRED` |
| 5447 | // (prediction still runs, libaom `predict_and_reconstruct_intra_block`), and |
| 5448 | // RECT4_16_CHROMA_DIR counts 1:4 chroma pairs predicted directionally (whose |
| 5449 | // intra-edge filter type must come from the chroma-reference position). |
| 5450 | thread_local! { |
| 5451 | static CHROMA_SKIP_CFL_HITS: std::cell::Cell<usize> = const { std::cell::Cell::new(0) }; |
| 5452 | static RECT4_16_CHROMA_DIR_HITS: std::cell::Cell<usize> = const { std::cell::Cell::new(0) }; |
| 5453 | } |
| 5454 | |
| 5455 | /// `(skipped UV_CFL_PRED blocks, directional 1:4 chroma pairs)` -- lane-reelckf. |
| 5456 | pub fn reelc_chroma_counters() -> (usize, usize) { |
| 5457 | ( |
| 5458 | CHROMA_SKIP_CFL_HITS.with(|c| c.get()), |
| 5459 | RECT4_16_CHROMA_DIR_HITS.with(|c| c.get()), |
| 5460 | ) |
| 5461 | } |
| 5462 | |
| 5463 | /// Current values of the 16x16-level 1:4 counters: `(16x4 strips, 4x16 |
| 5464 | /// strips, chroma-reference strips, strips with a coded residual, strips |
| 5465 | /// whose luma transform is split)`. |
| 5466 | pub fn rect4_16_counters() -> (usize, usize, usize, usize, usize) { |
| 5467 | ( |
| 5468 | RECT4_16_HORZ_HITS.with(|c| c.get()), |
| 5469 | RECT4_16_VERT_HITS.with(|c| c.get()), |
| 5470 | RECT4_16_CHROMA_HITS.with(|c| c.get()), |
| 5471 | RECT4_16_COEFF_HITS.with(|c| c.get()), |
| 5472 | RECT4_16_SPLIT_HITS.with(|c| c.get()), |
| 5473 | ) |
| 5474 | } |
| 5475 | |
| 5476 | /// Current value of `RECT4_32_HORZ_HITS` (32x8 strips decoded). |
| 5477 | pub(crate) fn rect4_32_horz_hits() -> usize { |
| 5478 | RECT4_32_HORZ_HITS.with(|c| c.get()) |
| 5479 | } |
| 5480 | |
| 5481 | /// Current value of `RECT4_32_VERT_HITS` (8x32 strips decoded). |
| 5482 | pub(crate) fn rect4_32_vert_hits() -> usize { |
| 5483 | RECT4_32_VERT_HITS.with(|c| c.get()) |
| 5484 | } |
| 5485 | |
| 5486 | /// Current value of `RECT4_32_HORZ_INTER_HITS` (32x8 inter strips decoded). |
| 5487 | pub(crate) fn rect4_32_horz_inter_hits() -> usize { |
| 5488 | RECT4_32_HORZ_INTER_HITS.with(|c| c.get()) |
| 5489 | } |
| 5490 | |
| 5491 | /// Current value of `RECT4_32_VERT_INTER_HITS` (8x32 inter strips decoded). |
| 5492 | pub(crate) fn rect4_32_vert_inter_hits() -> usize { |
| 5493 | RECT4_32_VERT_INTER_HITS.with(|c| c.get()) |
| 5494 | } |
| 5495 | |
| 5496 | /// Current value of `INTER_SB_HORZ_HITS` (64x32 inter strips decoded). |
| 5497 | pub(crate) fn inter_sb_horz_hits() -> usize { |
| 5498 | INTER_SB_HORZ_HITS.with(|c| c.get()) |
| 5499 | } |
| 5500 | |
| 5501 | /// Current value of `INTER_SB_VERT_HITS` (32x64 inter strips decoded). |
| 5502 | pub(crate) fn inter_sb_vert_hits() -> usize { |
| 5503 | INTER_SB_VERT_HITS.with(|c| c.get()) |
| 5504 | } |
| 5505 | |
| 5506 | /// Current value of `INTER_SB_HORZ4_HITS` (64x16 inter strips decoded). |
| 5507 | pub(crate) fn inter_sb_horz4_hits() -> usize { |
| 5508 | INTER_SB_HORZ4_HITS.with(|c| c.get()) |
| 5509 | } |
| 5510 | |
| 5511 | /// Current value of `INTER_SB_VERT4_HITS` (16x64 inter strips decoded). |
| 5512 | pub(crate) fn inter_sb_vert4_hits() -> usize { |
| 5513 | INTER_SB_VERT4_HITS.with(|c| c.get()) |
| 5514 | } |
| 5515 | |
| 5516 | // lane-rect64q r1: how many of [`decode_block_rect64`]'s three per-plane |
| 5517 | // dequant calls actually observed `CURRENT_Q_IDX != base_q_idx` -- proof the |
| 5518 | // running-vs-stale-snapshot bug this round fixed is exercised by a gate, not |
| 5519 | // just inert-compiled. Zero forever on a passing gate means that gate's |
| 5520 | // stream never carries `delta_q_present=1` through a rect64 block. |
| 5521 | thread_local! { |
| 5522 | static RECT64_QIDX_DRIFT_HITS: std::cell::Cell<usize> = |
| 5523 | const { std::cell::Cell::new(0) }; |
| 5524 | } |
| 5525 | |
| 5526 | /// Current value of [`RECT64_QIDX_DRIFT_HITS`]. |
| 5527 | #[allow(dead_code)] // read only from the `#[cfg(test)]` gates |
| 5528 | pub(crate) fn rect64_qidx_drift_hits() -> usize { |
| 5529 | RECT64_QIDX_DRIFT_HITS.with(|c| c.get()) |
| 5530 | } |
| 5531 | |
| 5532 | // lane-partab r1: how many 32x32 quadrants decoded an AB partition |
| 5533 | // (PARTITION_HORZ_A / VERT_A / VERT_B -- two 16x16 squares plus one |
| 5534 | // 16x32/32x16 strip). Like [`RECT_PARTITION_HITS`], this is the |
| 5535 | // free-partition gate's proof this round's arms actually fired. |
| 5536 | thread_local! { |
| 5537 | static PARTAB_HITS: std::cell::Cell<usize> = |
| 5538 | const { std::cell::Cell::new(0) }; |
| 5539 | } |
| 5540 | |
| 5541 | /// Current value of [`PARTAB_HITS`]. |
| 5542 | #[allow(dead_code)] // read only from the `#[cfg(test)]` gates |
| 5543 | pub(crate) fn partab_hits() -> usize { |
| 5544 | PARTAB_HITS.with(|c| c.get()) |
| 5545 | } |
| 5546 | |
| 5547 | // lane-part32 r1: how many 32x32 quadrants decoded each INTRA AB/4-way arm, |
| 5548 | // one counter per arm rather than one shared total -- the gate hard-asserts |
| 5549 | // each of these individually nonzero so a recipe that only ever fires |
| 5550 | // HORZ_A can't silently stand in for the whole set. |
| 5551 | thread_local! { |
| 5552 | static INTRA_HORZ_A_HITS: std::cell::Cell<usize> = |
| 5553 | const { std::cell::Cell::new(0) }; |
| 5554 | static INTRA_HORZ_B_HITS: std::cell::Cell<usize> = |
| 5555 | const { std::cell::Cell::new(0) }; |
| 5556 | static INTRA_VERT_A_HITS: std::cell::Cell<usize> = |
| 5557 | const { std::cell::Cell::new(0) }; |
| 5558 | static INTRA_VERT_B_HITS: std::cell::Cell<usize> = |
| 5559 | const { std::cell::Cell::new(0) }; |
| 5560 | } |
| 5561 | |
| 5562 | /// Current value of [`INTRA_HORZ_A_HITS`]. |
| 5563 | #[allow(dead_code)] // read only from the `#[cfg(test)]` gates |
| 5564 | pub(crate) fn intra_horz_a_hits() -> usize { |
| 5565 | INTRA_HORZ_A_HITS.with(|c| c.get()) |
| 5566 | } |
| 5567 | /// Current value of [`INTRA_HORZ_B_HITS`]. |
| 5568 | #[allow(dead_code)] // read only from the `#[cfg(test)]` gates |
| 5569 | pub(crate) fn intra_horz_b_hits() -> usize { |
| 5570 | INTRA_HORZ_B_HITS.with(|c| c.get()) |
| 5571 | } |
| 5572 | /// Current value of [`INTRA_VERT_A_HITS`]. |
| 5573 | #[allow(dead_code)] // read only from the `#[cfg(test)]` gates |
| 5574 | pub(crate) fn intra_vert_a_hits() -> usize { |
| 5575 | INTRA_VERT_A_HITS.with(|c| c.get()) |
| 5576 | } |
| 5577 | /// Current value of [`INTRA_VERT_B_HITS`]. |
| 5578 | #[allow(dead_code)] // read only from the `#[cfg(test)]` gates |
| 5579 | pub(crate) fn intra_vert_b_hits() -> usize { |
| 5580 | INTRA_VERT_B_HITS.with(|c| c.get()) |
| 5581 | } |
| 5582 | |
| 5583 | // How many [`read_inter_compound_mode`] reads actually happened |
| 5584 | // (lane-av1comp) -- proves a `COMPOUND_REFERENCE` block reached its own |
| 5585 | // `compound_mode` symbol (past `comp_mode`/`comp_ref` and the compound |
| 5586 | // mvstack build), not just that `comp_mode` fired. Still refused right |
| 5587 | // after (no MV assignment or MC wired), so this only counts attempted |
| 5588 | // reads. |
| 5589 | thread_local! { |
| 5590 | static COMPOUND_MODE_HITS: std::cell::Cell<usize> = |
| 5591 | const { std::cell::Cell::new(0) }; |
| 5592 | } |
| 5593 | |
| 5594 | /// Current value of [`COMPOUND_MODE_HITS`]. |
| 5595 | #[allow(dead_code)] // reader is test-only (the 12-bit compound witness); dead in non-test builds |
| 5596 | pub(crate) fn compound_mode_hits() -> usize { |
| 5597 | COMPOUND_MODE_HITS.with(|c| c.get()) |
| 5598 | } |
| 5599 | |
| 5600 | /// How many query blocks folded in at least one temporal MV candidate |
| 5601 | /// (spec 7.10.2.8's `add_tpl_ref_mv`) so far in this process -- the |
| 5602 | /// temporal-MV gate's own firing counter (lane-av1tmvp). |
| 5603 | /// |
| 5604 | /// # HANDOFF (lane-av1tmvp): not yet wired to a live stream |
| 5605 | /// [`crate::mvstack::find_mv_stack_with_sign_bias`]'s only call site in |
| 5606 | /// this file (`decode_inter_block`) still passes `tpl: None` -- this |
| 5607 | /// counter compiles and the projection math it counts is unit-tested |
| 5608 | /// ([`crate::motion_field`]), but nothing in [`crate::stream::decode_stream`] |
| 5609 | /// builds a per-DPB-slot [`crate::motion_field::MotionField`] or calls |
| 5610 | /// [`crate::motion_field::setup_motion_field`] yet, so this will read `0` |
| 5611 | /// against any real stream today. See the module doc on |
| 5612 | /// `crate::motion_field` and this crate's ledger for the remaining wiring. |
| 5613 | #[allow(dead_code)] // read only from the `#[cfg(test)]` gates |
| 5614 | pub(crate) fn tmv_hits() -> usize { |
| 5615 | crate::mvstack::tmv_hits() |
| 5616 | } |
| 5617 | |
| 5618 | /// Current value of [`crate::mc::inter_pred_hits`] -- inter predictions |
| 5619 | /// produced on this thread, the 10-bit inter gate's firing proof. |
| 5620 | #[allow(dead_code)] // read only from the `#[cfg(test)]` gates |
| 5621 | pub(crate) fn inter_pred_hits() -> usize { |
| 5622 | crate::mc::inter_pred_hits() |
| 5623 | } |
| 5624 | |
| 5625 | /// Everything [`decode_inter_block`] needs to fold temporal MV candidates |
| 5626 | /// (spec 7.10.2.8), threaded down from [`decode_inter_frame_tile_with_cdfs`]: |
| 5627 | /// the current frame's own projected field ([`crate::motion_field::setup_motion_field`]'s |
| 5628 | /// output, built by the caller when `header.use_ref_frame_mvs` is set) plus |
| 5629 | /// the order-hint bookkeeping [`crate::motion_field::get_relative_dist`] |
| 5630 | /// needs to turn a query block's own resolved `ref_frame` into |
| 5631 | /// `TplArgs::cur_offset_0` (spec 7.9.3). |
| 5632 | pub(crate) struct TplFrameArgs<'a> { |
| 5633 | pub field: &'a crate::motion_field::TplField, |
| 5634 | pub order_hint_bits: u32, |
| 5635 | pub order_hint: u32, |
| 5636 | /// `ref_order_hints[ref_frame - LAST_FRAME]` for `ref_frame` in |
| 5637 | /// `LAST_FRAME..=ALTREF_FRAME` — the OrderHint of the picture this |
| 5638 | /// frame's own `ref_frame_idx` names for each single reference. |
| 5639 | pub ref_order_hints: [u32; 7], |
| 5640 | } |
| 5641 | |
| 5642 | /// Builds this frame's own saved motion field (libaom `av1_copy_frame_mvs`, |
| 5643 | /// spec 7.9's per-frame `MotionFieldMvs` storage step) from its fully |
| 5644 | /// decoded `grid`: every 4x4 unit an inter block covers reads the same |
| 5645 | /// `MiInfo` ([`decode_inter_block`]/`decode_inter_block8` write it |
| 5646 | /// block-uniform), so sampling any one 4x4 unit inside each 8x8 cell is |
| 5647 | /// exact — every block this crate ever codes is at least 8x8, so no 8x8 |
| 5648 | /// cell straddles two different blocks' values. |
| 5649 | #[allow(unsafe_code)] // lane-mf: `par::Shared` band split, same corner-cut as the filter stages |
| 5650 | pub(crate) fn build_motion_field( |
| 5651 | grid: &MiGrid, |
| 5652 | mi_cols: usize, |
| 5653 | mi_rows: usize, |
| 5654 | order_hint: u32, |
| 5655 | order_hint_bits: u32, |
| 5656 | ref_order_hints: [u32; 7], |
| 5657 | fctx: &FrameCtx, |
| 5658 | ) -> crate::motion_field::MotionField { |
| 5659 | let mut field = |
| 5660 | crate::motion_field::MotionField::new(mi_cols, mi_rows, order_hint, ref_order_hints, false); |
| 5661 | // lane-interbis r1: libaom `av1_calculate_ref_frame_side` -- a reference |
| 5662 | // whose OrderHint is AHEAD of (or equal to) this frame's is "on the other |
| 5663 | // side", and `av1_copy_frame_mvs` skips it entirely, so a compound block |
| 5664 | // referencing one stores only its OTHER side's mv. With |
| 5665 | // `enable_order_hint` off (`order_hint_bits == 0`) every side is 0 and |
| 5666 | // nothing is skipped. |
| 5667 | // lane-mf: hoisted out of the per-4x4-unit loop -- the answer depends |
| 5668 | // only on the reference frame, so it is 7 table lookups per frame instead |
| 5669 | // of two per inter unit. |
| 5670 | let other_side: [bool; 8] = std::array::from_fn(|rf| { |
| 5671 | let i = rf.wrapping_sub(LAST_FRAME as usize); |
| 5672 | order_hint_bits != 0 |
| 5673 | && i < 7 |
| 5674 | && (crate::motion_field::get_relative_dist(order_hint_bits, ref_order_hints[i], order_hint) > 0 |
| 5675 | || ref_order_hints[i] == order_hint) |
| 5676 | }); |
| 5677 | // libaom `REFMVS_LIMIT` (`mvref_common.h`): a stored motion field mv is |
| 5678 | // capped at 12 bits per component; a longer one is not stored at all. |
| 5679 | const REFMVS_LIMIT: i32 = (1 << 12) - 1; |
| 5680 | let (cols, rows, cells) = field.cells_mut(); |
| 5681 | // Fills cell rows `r0..r1`; `out` is exactly those rows' cells, so bands |
| 5682 | // never touch each other (an 8x8 cell lives in one cell row, and mi row |
| 5683 | // `row` writes cell row `row / 2`). |
| 5684 | let fill = |r0: usize, r1: usize, out: &mut [Option<crate::motion_field::SavedMv>]| { |
| 5685 | for row in (r0 * 2)..(r1 * 2).min(mi_rows) { |
| 5686 | let out = &mut out[(row / 2 - r0) * cols..][..cols]; |
| 5687 | for col in 0..mi_cols { |
| 5688 | let Some(info) = grid.get_frame(row, col) else { |
| 5689 | continue; |
| 5690 | }; |
| 5691 | // `av1_copy_frame_mvs` runs for EVERY block of an inter frame and |
| 5692 | // CLEARS the cells it covers first (`mv->ref_frame = NONE_FRAME`), |
| 5693 | // so an intra block -- or an inter block whose every slot is on the |
| 5694 | // other side / over the limit -- wipes what an earlier block wrote |
| 5695 | // into the same 8x8 cell (only reachable from sub-8x8 leaves, which |
| 5696 | // share a cell). |
| 5697 | if !info.is_inter { |
| 5698 | out[col / 2] = None; |
| 5699 | continue; |
| 5700 | } |
| 5701 | // `av1_copy_frame_mvs` walks BOTH reference slots and the LAST |
| 5702 | // qualifying one wins -- a compound block stores its second |
| 5703 | // reference's mv, never its first (unless the second is on the |
| 5704 | // other side or over the limit). Storing slot 0 unconditionally |
| 5705 | // put the wrong (mv, ref_frame_offset) pair in every compound |
| 5706 | // block's cell, which a later frame's temporal candidate then |
| 5707 | // projected with the wrong reference distance. |
| 5708 | let mut saved: Option<crate::motion_field::SavedMv> = None; |
| 5709 | for (rf, mv) in [ |
| 5710 | (info.ref_frame, Some(mv32(info.mv))), |
| 5711 | (info.ref_frame1.max(0), Some(mv32(info.mv1))), |
| 5712 | ] { |
| 5713 | let (Some(mv), true) = (mv, rf > 0) else { |
| 5714 | continue; |
| 5715 | }; |
| 5716 | if other_side[rf as usize] { |
| 5717 | continue; |
| 5718 | } |
| 5719 | if mv.0.abs() > REFMVS_LIMIT || mv.1.abs() > REFMVS_LIMIT { |
| 5720 | continue; |
| 5721 | } |
| 5722 | saved = Some(crate::motion_field::SavedMv { mv, ref_frame: rf }); |
| 5723 | } |
| 5724 | out[col / 2] = saved; |
| 5725 | } |
| 5726 | } |
| 5727 | }; |
| 5728 | // lane-mf: the whole-frame pass split over the filter pool in cell-row |
| 5729 | // bands (same `Shared` corner-cut as the filter stages -- disjointness is |
| 5730 | // the band arithmetic above, proved by the 1-vs-16-thread byte-exact gate). |
| 5731 | let bands = crate::par::bands(rows, crate::par::filter_threads()); |
| 5732 | if bands.len() <= 1 { |
| 5733 | fill(0, rows, cells); |
| 5734 | } else { |
| 5735 | let shared = crate::par::Shared::new(cells); |
| 5736 | crate::par::run_bands(&bands, fctx, |r0, r1, _| { |
| 5737 | // SAFETY: cell rows `r0..r1` are this band's alone. |
| 5738 | let cells = unsafe { shared.get() }; |
| 5739 | fill(r0, r1, &mut cells[r0 * cols..r1 * cols]); |
| 5740 | }); |
| 5741 | } |
| 5742 | if crate::envflags::env_flag!("EC_TRACE_TPL") { |
| 5743 | eprintln!("EC_TPL_SAVED oh={order_hint}"); |
| 5744 | for (r, row) in field.occupancy().iter().enumerate() { |
| 5745 | eprintln!("EC_TPL_SAVED r{r} {row}"); |
| 5746 | } |
| 5747 | } |
| 5748 | field |
| 5749 | } |
| 5750 | |
| 5751 | /// Current value of the counter for `ref_frame` (`LAST2_FRAME`..=`ALTREF_FRAME`; |
| 5752 | /// panics on `LAST_FRAME`/`INTRA_FRAME`/`NONE`, which have no counter here). |
| 5753 | #[allow(dead_code)] // read only from the `#[cfg(test)]` gates |
| 5754 | pub(crate) fn ref_hits(ref_frame: i8) -> usize { |
| 5755 | let c = match ref_frame { |
| 5756 | LAST2_FRAME => &LAST2_HITS, |
| 5757 | LAST3_FRAME => &LAST3_HITS, |
| 5758 | GOLDEN_FRAME => &GOLDEN_HITS, |
| 5759 | BWDREF_FRAME => &BWDREF_HITS, |
| 5760 | ALTREF2_FRAME => &ALTREF2_HITS, |
| 5761 | ALTREF_FRAME => &ALTREF_HITS, |
| 5762 | other => panic!("ref_hits: no per-reference counter for ref_frame {other}"), |
| 5763 | }; |
| 5764 | c.with(|v| v.get()) |
| 5765 | } |
| 5766 | |
| 5767 | /// The sequence header's `enable_intra_edge_filter`, set once per |
| 5768 | /// [`decode_key_frame_tile_with_cdfs`] call: [`PlaneBuf::reconstruct`]'s |
| 5769 | /// own read of it, kept off the call stack (see that function's own doc |
| 5770 | /// comment for why) rather than threaded through every `read_plane`/ |
| 5771 | /// `decode_block`/`decode_leaf8` call between here and there. |
| 5772 | |
| 5773 | /// Set [`ENABLE_EDGE_FILTER`] for this thread. |
| 5774 | /// |
| 5775 | /// `decode_key_frame_tile_with_cdfs` sets it, but `decode_inter_frame_tile` |
| 5776 | /// never did -- so an inter frame's intra blocks read whatever value the last |
| 5777 | /// key frame decoded ON THIS THREAD left behind. Inside one stream that is |
| 5778 | /// harmless (its own key frame sets it first), but a caller that decodes an |
| 5779 | /// inter frame directly, or a test harness running several streams on one |
| 5780 | /// thread, inherits a foreign sequence's bit: decode stops being a pure |
| 5781 | /// function of the bitstream. `decode_stream` now calls this once per stream |
| 5782 | /// from the sequence header, which covers every frame including inter ones. |
| 5783 | pub(crate) fn set_enable_edge_filter(value: bool, fctx: &crate::decode::FrameCtx) { |
| 5784 | fctx.enable_edge_filter.with(|f| f.set(value)); |
| 5785 | } |
| 5786 | |
| 5787 | /// Current value of [`ANGLE_DELTA_HITS`]. |
| 5788 | #[allow(dead_code)] // read only from the `#[cfg(test)]` gates |
| 5789 | pub(crate) fn angle_delta_hits() -> usize { |
| 5790 | ANGLE_DELTA_HITS.with(|c| c.get()) |
| 5791 | } |
| 5792 | |
| 5793 | /// Current value of [`TX_CLASS1_HITS`]. |
| 5794 | #[allow(dead_code)] // read only from the `#[cfg(test)]` gates |
| 5795 | pub(crate) fn tx_class1_hits() -> usize { |
| 5796 | TX_CLASS1_HITS.with(|c| c.get()) |
| 5797 | } |
| 5798 | |
| 5799 | // How many `PARTITION_SPLIT` decisions below an 8x8 block (four independent |
| 5800 | // `BLOCK_4X4` leaves, spec `decode_partition`'s `bSize < BLOCK_8X8` recursion |
| 5801 | // bottom) this thread's decode actually reconstructed -- the same |
| 5802 | // before/after counter pattern as [`TX_CLASS1_HITS`], proving a stream |
| 5803 | // genuinely exercised the sub-8x8 leaf path rather than every attempt |
| 5804 | // coincidentally landing on `PARTITION_NONE`. |
| 5805 | thread_local! { |
| 5806 | static SUB8_SPLIT_HITS: std::cell::Cell<usize> = const { std::cell::Cell::new(0) }; |
| 5807 | /// How many 8x8 groups an INTER frame split into four BLOCK_4X4 inter |
| 5808 | /// sub-blocks (lane-intersub8). |
| 5809 | static SUB8_INTER_SPLIT_HITS: std::cell::Cell<usize> = const { std::cell::Cell::new(0) }; |
| 5810 | /// How many 8x8 groups an INTER frame split into two rectangular sub-8x8 |
| 5811 | /// inter leaves (lane-intersub8 r3), indexed `[0] = HORZ` (two BLOCK_8X4), |
| 5812 | /// `[1] = VERT` (two BLOCK_4X8). |
| 5813 | static SUB8_INTER_RECT_HITS: std::cell::Cell<[usize; 2]> = const { std::cell::Cell::new([0; 2]) }; |
| 5814 | /// lane-sub8intra: INTRA leaves decoded inside an inter frame's sub-8x8 |
| 5815 | /// HORZ/VERT partition -- `[0]` 8x4, `[1]` 4x8, `[2]` of those that were |
| 5816 | /// the chroma reference (so coded the group's 4x4 chroma intra), `[3]` |
| 5817 | /// MIXED groups whose chroma reference is INTER while its sibling is |
| 5818 | /// intra (the `is_sub8x8_inter` false path), `[4]` INTRA `BLOCK_4X4` |
| 5819 | /// leaves of an inter frame's sub-8x8 `PARTITION_SPLIT` (lane-sub8x4 r2 -- |
| 5820 | /// the second lifted refusal's own witness; `[0]`/`[1]` count those too, |
| 5821 | /// so only this cell separates the split arm from the HORZ/VERT one). |
| 5822 | static SUB8_INTRA_RECT_HITS: std::cell::Cell<[usize; 5]> = const { std::cell::Cell::new([0; 5]) }; |
| 5823 | } |
| 5824 | |
| 5825 | /// Current value of [`SUB8_INTRA_RECT_HITS`]: `(8x4, 4x8, chroma-reference, |
| 5826 | /// mixed groups, split 4x4 leaves)`. |
| 5827 | pub fn sub8_intra_rect_hits() -> (usize, usize, usize, usize, usize) { |
| 5828 | SUB8_INTRA_RECT_HITS.with(|c| { |
| 5829 | let h = c.get(); |
| 5830 | (h[0], h[1], h[2], h[3], h[4]) |
| 5831 | }) |
| 5832 | } |
| 5833 | |
| 5834 | /// Current value of [`SUB8_INTER_RECT_HITS`]: `(8x4 groups, 4x8 groups)`. |
| 5835 | pub fn sub8_inter_rect_hits() -> (usize, usize) { |
| 5836 | SUB8_INTER_RECT_HITS.with(|c| { |
| 5837 | let h = c.get(); |
| 5838 | (h[0], h[1]) |
| 5839 | }) |
| 5840 | } |
| 5841 | |
| 5842 | // lane-cdef r1: (a) how many 8x8 CDEF units `apply_cdef` EXCLUDED because all |
| 5843 | // four of their mi cells are `skip_txfm` (libaom `cdef.c:29-38` |
| 5844 | // `is_8x8_block_skip` / `av1_cdef_compute_sb_list`'s dlist test), and (b) how |
| 5845 | // many 8x8 inter leaves (`decode_inter_block8`) wrote that skip band at all. |
| 5846 | // (b) was 0 for every stream before this round -- the leaf path never touched |
| 5847 | // the band, so an all-skip 8x8 group kept the PREVIOUS frame's flags and got |
| 5848 | // filtered where libaom skips it. A gate asserts both are non-zero. |
| 5849 | thread_local! { |
| 5850 | static CDEF_SKIPPED_UNITS: std::cell::Cell<usize> = const { std::cell::Cell::new(0) }; |
| 5851 | static INTER8_SKIP_BAND_HITS: std::cell::Cell<usize> = const { std::cell::Cell::new(0) }; |
| 5852 | // lane-cdef r2: (c) 8x8 CDEF units that are FILTERED and whose extent |
| 5853 | // crosses the cropped frame edge -- the only units for which "clamp at |
| 5854 | // the crop" and "pad past the mi-rounded extent with CDEF_VERY_LARGE" |
| 5855 | // (libaom `cdef.c:249-256`, `cdef_prepare_fb`'s hsize/vsize are |
| 5856 | // `nhb << mi_wide_l2` off the MI grid, not the crop) differ. A frame |
| 5857 | // whose width or height is not a multiple of 8 is the only shape that |
| 5858 | // produces one; the straddling-band gate arms assert it grew. |
| 5859 | static CDEF_STRADDLE_UNITS: std::cell::Cell<usize> = const { std::cell::Cell::new(0) }; |
| 5860 | // lane-cdefstrip r1: 8x8 CDEF units visited with an UNWRITTEN skip band |
| 5861 | // (see `Neighbours::skip_written`). |
| 5862 | static CDEF_UNWRITTEN_SKIP_UNITS: std::cell::Cell<usize> = const { std::cell::Cell::new(0) }; |
| 5863 | // lane-cdefstrip r1: (d) skip RECTANGULAR blocks (every strip arm: 32x8, |
| 5864 | // 8x32, 16x8, 32x16, 64x16, AB/1:4 pieces...) that wrote the CDEF skip |
| 5865 | // band, and (e) 8x8 CDEF units whose four mi cells do NOT agree on skip -- |
| 5866 | // the shape libaom's `is_8x8_block_skip` (`cdef.c:29-38`) exists for: an |
| 5867 | // 8-tall strip pair where only one half is skip must still be FILTERED. |
| 5868 | static RECT_SKIP_BAND_HITS: std::cell::Cell<usize> = const { std::cell::Cell::new(0) }; |
| 5869 | static CDEF_MIXED_SKIP_UNITS: std::cell::Cell<usize> = const { std::cell::Cell::new(0) }; |
| 5870 | } |
| 5871 | |
| 5872 | /// Current value of `RECT_SKIP_BAND_HITS`. |
| 5873 | pub fn rect_skip_band_hits() -> usize { |
| 5874 | RECT_SKIP_BAND_HITS.with(|c| c.get()) |
| 5875 | } |
| 5876 | |
| 5877 | /// Current value of `CDEF_MIXED_SKIP_UNITS`. |
| 5878 | pub fn cdef_mixed_skip_units() -> usize { |
| 5879 | CDEF_MIXED_SKIP_UNITS.with(|c| c.get()) |
| 5880 | } |
| 5881 | |
| 5882 | /// Current value of `CDEF_SKIPPED_UNITS`. |
| 5883 | #[allow(dead_code)] // read only from the `#[cfg(test)]` gates |
| 5884 | pub(crate) fn cdef_skipped_units() -> usize { |
| 5885 | CDEF_SKIPPED_UNITS.with(|c| c.get()) |
| 5886 | } |
| 5887 | |
| 5888 | /// How many 8x8 CDEF units [`apply_cdef`] visited whose skip band no coded |
| 5889 | /// block wrote this frame -- a writer gap, always a defect (lane-cdefstrip r1). |
| 5890 | pub fn cdef_unwritten_skip_units() -> usize { |
| 5891 | CDEF_UNWRITTEN_SKIP_UNITS.with(|c| c.get()) |
| 5892 | } |
| 5893 | |
| 5894 | /// Current value of `INTER8_SKIP_BAND_HITS`. |
| 5895 | #[allow(dead_code)] // read only from the `#[cfg(test)]` gates |
| 5896 | pub(crate) fn inter8_skip_band_hits() -> usize { |
| 5897 | INTER8_SKIP_BAND_HITS.with(|c| c.get()) |
| 5898 | } |
| 5899 | |
| 5900 | /// Current value of `CDEF_STRADDLE_UNITS`. |
| 5901 | #[allow(dead_code)] // read only from the `#[cfg(test)]` gates |
| 5902 | pub(crate) fn cdef_straddle_units() -> usize { |
| 5903 | CDEF_STRADDLE_UNITS.with(|c| c.get()) |
| 5904 | } |
| 5905 | |
| 5906 | /// Current value of [`SUB8_SPLIT_HITS`]. |
| 5907 | #[allow(dead_code)] // read only from the `#[cfg(test)]` gates |
| 5908 | pub(crate) fn sub8_split_hits() -> usize { |
| 5909 | SUB8_SPLIT_HITS.with(|c| c.get()) |
| 5910 | } |
| 5911 | |
| 5912 | /// Current value of [`SUB8_INTER_SPLIT_HITS`]. |
| 5913 | pub fn sub8_inter_split_hits() -> usize { |
| 5914 | SUB8_INTER_SPLIT_HITS.with(|c| c.get()) |
| 5915 | } |
| 5916 | |
| 5917 | // lane-t900 r7: sub-8x8 inter groups whose one chroma unit inherits a |
| 5918 | // `tx_type` DIFFERENT from the group's FIRST sub-block -- the only shape that |
| 5919 | // tells the two rules apart (`av1_get_tx_type` indexes `xd->tx_type_map`, |
| 5920 | // which `set_mi_offsets` re-points to the CHROMA-REFERENCE sub-block, so the |
| 5921 | // first sub-block's type is never the one). Reading it off sub-block 0 |
| 5922 | // inverted one 4x4 chroma unit on the wrong basis with the entropy stream |
| 5923 | // perfectly in sync (no chroma `tx_type` symbol is coded), so a gate can only |
| 5924 | // see this rule through this counter. |
| 5925 | thread_local! { |
| 5926 | static SUB8_CHROMA_TX_FROM_REF_HITS: std::cell::Cell<usize> = const { std::cell::Cell::new(0) }; |
| 5927 | } |
| 5928 | |
| 5929 | /// Current value of `SUB8_CHROMA_TX_FROM_REF_HITS`. |
| 5930 | pub fn sub8_chroma_tx_from_ref_hits() -> usize { |
| 5931 | SUB8_CHROMA_TX_FROM_REF_HITS.with(|c| c.get()) |
| 5932 | } |
| 5933 | |
| 5934 | fn note_sub8_chroma_tx(first_sub: TxType, chroma_ref: TxType, coeffs: bool) { |
| 5935 | if coeffs && first_sub != chroma_ref { |
| 5936 | hit!(SUB8_CHROMA_TX_FROM_REF_HITS); |
| 5937 | } |
| 5938 | } |
| 5939 | |
| 5940 | // How many 4x8 / 8x4 leaves (lane-tx4x8's `PARTITION_VERT`/`PARTITION_HORZ` |
| 5941 | // of an 8x8 block) this thread's decode reconstructed with REAL coefficients |
| 5942 | // -- a skipped leaf exercises the prediction and the mi bookkeeping but never |
| 5943 | // the rect transform, the 32-position `eob_pt` or the rect scan, so the gate |
| 5944 | // asserts on these two rather than a plain leaf count. |
| 5945 | thread_local! { |
| 5946 | static TX4X8_CODED_HITS: std::cell::Cell<usize> = const { std::cell::Cell::new(0) }; |
| 5947 | static TX8X4_CODED_HITS: std::cell::Cell<usize> = const { std::cell::Cell::new(0) }; |
| 5948 | /// Sub-8x8 rect leaves whose `tx_depth` symbol resolved to 1 -- the leaf's |
| 5949 | /// two 4x4 transform units, predicted one after the other. A separate |
| 5950 | /// counter from [`TX_DEPTH_HITS`] (which every square path also bumps) so |
| 5951 | /// the gate can prove THIS path fired. |
| 5952 | static RECT8_SPLIT_TX_HITS: std::cell::Cell<usize> = const { std::cell::Cell::new(0) }; |
| 5953 | /// 16x8/8x16 intra strips whose prediction actually read past the block's |
| 5954 | /// own width (above-right) or height (below-left) -- the samples |
| 5955 | /// `Reach::of_rect` decides on, and the only ones a wrong `has_tr_*`/ |
| 5956 | /// `has_bl_*` table row can corrupt (lane-tx4x8 r3). |
| 5957 | static RECT_STRIP_REACH_HITS: std::cell::Cell<usize> = const { std::cell::Cell::new(0) }; |
| 5958 | /// Every 16x8/8x16 intra strip predicted, reaching or not -- the |
| 5959 | /// denominator [`RECT_STRIP_REACH_HITS`] is a fraction of. |
| 5960 | static RECT_STRIP_PRED_HITS: std::cell::Cell<usize> = const { std::cell::Cell::new(0) }; |
| 5961 | } |
| 5962 | |
| 5963 | /// Current value of [`RECT8_SPLIT_TX_HITS`]. |
| 5964 | #[allow(dead_code)] // read only from the `#[cfg(test)]` gates |
| 5965 | pub(crate) fn rect8_split_tx_hits() -> usize { |
| 5966 | RECT8_SPLIT_TX_HITS.with(|c| c.get()) |
| 5967 | } |
| 5968 | |
| 5969 | /// Current value of [`RECT_STRIP_PRED_HITS`]. |
| 5970 | #[allow(dead_code)] // read only from the `#[cfg(test)]` gates |
| 5971 | pub(crate) fn rect_strip_pred_hits() -> usize { |
| 5972 | RECT_STRIP_PRED_HITS.with(|c| c.get()) |
| 5973 | } |
| 5974 | |
| 5975 | /// Current value of [`RECT_STRIP_REACH_HITS`]. |
| 5976 | #[allow(dead_code)] // read only from the `#[cfg(test)]` gates |
| 5977 | pub(crate) fn rect_strip_reach_hits() -> usize { |
| 5978 | RECT_STRIP_REACH_HITS.with(|c| c.get()) |
| 5979 | } |
| 5980 | |
| 5981 | /// Current value of [`TX4X8_CODED_HITS`]. |
| 5982 | #[allow(dead_code)] // read only from the `#[cfg(test)]` gates |
| 5983 | pub(crate) fn tx4x8_coded_hits() -> usize { |
| 5984 | TX4X8_CODED_HITS.with(|c| c.get()) |
| 5985 | } |
| 5986 | |
| 5987 | /// Current value of [`TX8X4_CODED_HITS`]. |
| 5988 | #[allow(dead_code)] // read only from the `#[cfg(test)]` gates |
| 5989 | pub(crate) fn tx8x4_coded_hits() -> usize { |
| 5990 | TX8X4_CODED_HITS.with(|c| c.get()) |
| 5991 | } |
| 5992 | |
| 5993 | // How many CHROMA coefficient blocks read their `eob_pt` from the class-1 |
| 5994 | // (`TX_CLASS_HORIZ`/`VERT`) row, across every call on the current thread. |
| 5995 | // Chroma never codes a `tx_type` symbol -- it inherits the colocated luma |
| 5996 | // type verbatim (`av1_get_tx_type`, `blockd.h:1291`) -- so this fires only on |
| 5997 | // an inter leaf whose luma picked a 1D type, the path lane-eobc1 wired |
| 5998 | // (`eob_pt_{16,64,128,256}_chroma_class1`). |
| 5999 | thread_local! { |
| 6000 | static CHROMA_EOB_CLASS1_HITS: std::cell::Cell<usize> = |
| 6001 | const { std::cell::Cell::new(0) }; |
| 6002 | } |
| 6003 | |
| 6004 | /// Current value of [`CHROMA_EOB_CLASS1_HITS`]. |
| 6005 | #[allow(dead_code)] // read only from the `#[cfg(test)]` gates |
| 6006 | pub(crate) fn chroma_eob_class1_hits() -> usize { |
| 6007 | CHROMA_EOB_CLASS1_HITS.with(|c| c.get()) |
| 6008 | } |
| 6009 | |
| 6010 | /// Counts one [`CHROMA_EOB_CLASS1_HITS`] when `plane_idx`'s inherited |
| 6011 | /// `tx_type` put this block's `eob_pt` on the class-1 row. |
| 6012 | fn note_chroma_class1(plane_idx: usize, tx_type: TxType) { |
| 6013 | if plane_idx > 0 && TxClass::of(tx_type) != TxClass::TwoD { |
| 6014 | hit!(CHROMA_EOB_CLASS1_HITS); |
| 6015 | } |
| 6016 | } |
| 6017 | |
| 6018 | const SUB_MI: u32 = 4; |
| 6019 | const MI: usize = 4; |
| 6020 | const SB: usize = 64; |
| 6021 | /// The `above_side`/`left_side` value a cell with no decoded neighbour holds |
| 6022 | /// (tile start, superblock-row start). |
| 6023 | /// |
| 6024 | /// [`Neighbours::partition_ctx_mi`] reads it as libaom's |
| 6025 | /// `av1_get_partition_plane_context` reads its own zero-initialised |
| 6026 | /// `above_partition_context` byte: bit `bsl` clear, i.e. "the neighbour is at |
| 6027 | /// least as wide as this block". lane-sb128 r1: the old sentinel was `SB` |
| 6028 | /// (64), which reads identically at every level up to 64x64 but sets the bit |
| 6029 | /// at the `BLOCK_128X128` root (64 * 2 <= 128) -- the 128-level partition |
| 6030 | /// symbol was read off CDF row 3 where libaom uses row 0 (aomdec |
| 6031 | /// `EC_PART mi_row=0 mi_col=0 bsize=15 ctx=16`, i.e. `bsl*4 + 0`). |
| 6032 | const NO_NEIGHBOUR_SIDE: usize = 128; |
| 6033 | const BLOCK: usize = 32; |
| 6034 | const SUB: usize = 16; |
| 6035 | const TX32: usize = 32; |
| 6036 | const TX16: usize = 16; |
| 6037 | const TX8: usize = 8; |
| 6038 | const TX4: usize = 4; |
| 6039 | const DC_PRED: usize = 0; |
| 6040 | /// `V_PRED` (spec 6.10.2): the first directional mode, and so the first that |
| 6041 | /// carries an angle delta — [`crate::tile`]'s own private copy of the same |
| 6042 | /// constant. |
| 6043 | const V_PRED: usize = 1; |
| 6044 | const H_PRED: usize = 2; |
| 6045 | /// `D157_PRED`: `fimode_to_intradir`'s target for `FILTER_D157_PRED` (spec |
| 6046 | /// `Fimode_To_Intradir`, libaom `blockd.h`). |
| 6047 | const D157_PRED: usize = 6; |
| 6048 | /// The last directional mode, `D67_PRED`. |
| 6049 | const D67_PRED: usize = 8; |
| 6050 | /// The symbol an angle delta of zero codes as (spec 9.3): the alphabet runs |
| 6051 | /// from -3 to +3, so this is its middle — [`crate::tile`]'s writer never |
| 6052 | /// codes anything else, so this decoder never reads anything else either. |
| 6053 | const ANGLE_DELTA_ZERO: usize = 3; |
| 6054 | const NUM_BASE_LEVELS: i32 = 2; |
| 6055 | const COEFF_BASE_RANGE: i32 = 12; |
| 6056 | const BR_STEP: i32 = 3; |
| 6057 | const MAX_BR_LEVEL: i32 = NUM_BASE_LEVELS + COEFF_BASE_RANGE; |
| 6058 | |
| 6059 | /// `partition_gather_vert_alike`/`_horz_alike` (spec 9.3): the partition types |
| 6060 | /// whose probability mass a superblock or block half outside the frame |
| 6061 | /// gathers into its one remaining flag — [`crate::tile`]'s own private copy, |
| 6062 | /// duplicated here (that module is another lane's territory this round) and |
| 6063 | /// kept in step with it by the byte-exact round-trip tests below rather than |
| 6064 | /// by sharing code. |
| 6065 | const VERT_ALIKE: [usize; 6] = [2, PARTITION_SPLIT, 4, 6, 7, 9]; |
| 6066 | const HORZ_ALIKE: [usize; 6] = [1, PARTITION_SPLIT, 4, 5, 6, 8]; |
| 6067 | |
| 6068 | fn trace_edge(mi_r: usize, mi_c: usize, bsize: u32, ctx: usize, rng: u32) { |
| 6069 | if crate::envflags::env_flag!("EC_AV1_TRACE") { |
| 6070 | eprintln!("OUR_PART mi_row={mi_r} mi_col={mi_c} bsize={bsize} ctx={ctx} rng={rng}"); |
| 6071 | } |
| 6072 | } |
| 6073 | |
| 6074 | fn element_prob(cdf: &[u16], element: usize) -> u16 { |
| 6075 | cdf[element] - if element > 0 { cdf[element - 1] } else { 0 } |
| 6076 | } |
| 6077 | |
| 6078 | /// The two-symbol CDF a partial superblock/block's forced-split flag is read |
| 6079 | /// with: the mass of `elements` becomes the probability of the one partition |
| 6080 | /// the frame edge still allows, mirroring [`crate::tile`]'s own `gather` |
| 6081 | /// (the encoder's non-adapting counterpart to this read). |
| 6082 | fn gather(cdf: &[u16], elements: [usize; 6]) -> [u16; 3] { |
| 6083 | let split: u16 = elements.iter().map(|&e| element_prob(cdf, e)).sum(); |
| 6084 | [32768 - split, 32768, 0] |
| 6085 | } |
| 6086 | |
| 6087 | fn unsupported(what: impl Into<String>) -> Error { |
| 6088 | Error::unsupported("AV1 tile", what.into()) |
| 6089 | } |
| 6090 | |
| 6091 | /// The coefficient q-context a frame's `base_q_idx` picks its default CDFs |
| 6092 | /// from (spec `Get_Qctx`), mirroring [`crate::tile`]'s private copy of the |
| 6093 | /// same rule. |
| 6094 | pub(crate) fn q_ctx_of(base_q_idx: u8) -> usize { |
| 6095 | match base_q_idx { |
| 6096 | 0..=20 => 0, |
| 6097 | 21..=60 => 1, |
| 6098 | 61..=120 => 2, |
| 6099 | _ => 3, |
| 6100 | } |
| 6101 | } |
| 6102 | |
| 6103 | /// `Default_Scan_NxN` (spec 8.4.2), the same anti-diagonal walk |
| 6104 | /// [`crate::tile`]'s writer generates its scan tables with. |
| 6105 | fn default_scan(side: usize) -> &'static [u16] { |
| 6106 | // One table per transform side (4/8/16/32), built once: this used to be a |
| 6107 | // fresh heap allocation plus the whole anti-diagonal walk on every |
| 6108 | // transform block read. |
| 6109 | static SCANS: std::sync::LazyLock<[Vec<u16>; 4]> = std::sync::LazyLock::new(|| { |
| 6110 | [ |
| 6111 | default_scan_gen(4), |
| 6112 | default_scan_gen(8), |
| 6113 | default_scan_gen(16), |
| 6114 | default_scan_gen(32), |
| 6115 | ] |
| 6116 | }); |
| 6117 | assert!( |
| 6118 | matches!(side, 4 | 8 | 16 | 32), |
| 6119 | "no default scan for a {side}-point transform" |
| 6120 | ); |
| 6121 | &SCANS[side.trailing_zeros() as usize - 2] |
| 6122 | } |
| 6123 | |
| 6124 | fn default_scan_gen(side: usize) -> Vec<u16> { |
| 6125 | let mut scan = Vec::with_capacity(side * side); |
| 6126 | for d in 0..(2 * side - 1) { |
| 6127 | let lo = d.saturating_sub(side - 1); |
| 6128 | let hi = d.min(side - 1); |
| 6129 | let diagonal = (lo..=hi).map(|row| (row * side + (d - row)) as u16); |
| 6130 | if d % 2 == 0 { |
| 6131 | scan.extend(diagonal.rev()); |
| 6132 | } else { |
| 6133 | scan.extend(diagonal); |
| 6134 | } |
| 6135 | } |
| 6136 | scan |
| 6137 | } |
| 6138 | |
| 6139 | /// `Mrow_Scan`/`Mcol_Scan` (spec 8.4.2, libaom `scan.c`): the two |
| 6140 | /// class-specific scans a `V_DCT`/`H_DCT` transform block reads coefficients |
| 6141 | /// in, instead of the default zigzag. libaom's own tables are keyed by a |
| 6142 | /// *column-major* raster (`get_txb_bhl`'s `col = pos / height`) over its |
| 6143 | /// physically column-major level buffer, but they decompose to the exact |
| 6144 | /// same (row, col) walk as a flat row-major traversal of *our* row-major |
| 6145 | /// `grid`/`levels` (`pos = row * side + col`) once you read them that way: |
| 6146 | /// `Mrow_Scan` visits row 0 across every column, then row 1, ... — precisely |
| 6147 | /// `0..side*side` in our own indexing — and `Mcol_Scan` is that walk |
| 6148 | /// transposed (column 0 down every row, then column 1, ...). |
| 6149 | pub(crate) fn class_scan_table(side: usize, class: TxClass) -> Vec<u16> { |
| 6150 | class_scan_table_wh(side, side, class) |
| 6151 | } |
| 6152 | |
| 6153 | /// [`class_scan_table`] widened to `(w, h)` (lane-tx4x8). Checked against |
| 6154 | /// libaom's own `mrow_scan_4x8`/`mcol_scan_4x8`/`mrow_scan_8x4`/ |
| 6155 | /// `mcol_scan_8x4` (`scan.c`) converted out of their column-major encoding |
| 6156 | /// by `(p % h) * w + p / h`, the same conversion [`SCAN_16X8`] was |
| 6157 | /// transcribed with -- `mrow` is the plain row-major raster in our indexing |
| 6158 | /// and `mcol` its transpose, at rect sizes exactly as at square ones. |
| 6159 | fn class_scan_table_wh(w: usize, h: usize, class: TxClass) -> Vec<u16> { |
| 6160 | match class { |
| 6161 | TxClass::Vert => (0..(w * h) as u16).collect(), |
| 6162 | TxClass::Horiz => (0..w) |
| 6163 | .flat_map(|col| (0..h).map(move |row| (row * w + col) as u16)) |
| 6164 | .collect(), |
| 6165 | TxClass::TwoD => unreachable!("class_scan is only for V_DCT/H_DCT"), |
| 6166 | } |
| 6167 | } |
| 6168 | |
| 6169 | fn eob_coeff_ctx(scan_idx: usize, area: usize) -> usize { |
| 6170 | match scan_idx { |
| 6171 | 0 => 0, |
| 6172 | i if i <= area / 8 => 1, |
| 6173 | i if i <= area / 4 => 2, |
| 6174 | _ => 3, |
| 6175 | } |
| 6176 | } |
| 6177 | |
| 6178 | /// One cell of [`read_coeffs`]'s level buffer. |
| 6179 | /// |
| 6180 | /// lane-coef2: the buffer is now PADDED (libaom's `TX_PAD_HOR`/`TX_PAD_VER`, |
| 6181 | /// `txb_common.h`) -- four zero columns to the right of `side` and four zero |
| 6182 | /// rows below it, at a fixed `stride = side + 4` -- so the four-away reads |
| 6183 | /// `base_ctx` makes (`(0, 4)`/`(4, 0)` are its widest offsets) land on real |
| 6184 | /// zero cells instead of needing the `row >= side || col >= side` edge test |
| 6185 | /// this used to run five times per coefficient. Nothing writes a padding |
| 6186 | /// cell, so the values are the same zeros the test returned. |
| 6187 | #[inline] |
| 6188 | fn neighbour(grid: &[u8], stride: usize, row: usize, col: usize) -> i32 { |
| 6189 | grid[row * stride + col] as i32 |
| 6190 | } |
| 6191 | |
| 6192 | /// spec 5.11.39's three `TxClass`es (`tx_type_to_class`, libaom |
| 6193 | /// `txb_common.h`): everything but the two lone-axis-identity types this |
| 6194 | /// decoder reads (`V_DCT`/`H_DCT`) is `TX_CLASS_2D`. |
| 6195 | #[derive(Clone, Copy, PartialEq, Eq)] |
| 6196 | pub(crate) enum TxClass { |
| 6197 | TwoD, |
| 6198 | Horiz, |
| 6199 | Vert, |
| 6200 | } |
| 6201 | |
| 6202 | impl TxClass { |
| 6203 | pub(crate) fn of(tx_type: TxType) -> Self { |
| 6204 | match tx_type { |
| 6205 | // `TX_CLASS_VERT`/`TX_CLASS_HORIZ` (`txb_common.h`'s `tx_type_to_class`) |
| 6206 | // key on axis alone -- FLIPADST is still ADST for this purpose. |
| 6207 | TxType::VDct | TxType::VAdst | TxType::VFlipAdst => Self::Vert, |
| 6208 | TxType::HDct | TxType::HAdst | TxType::HFlipAdst => Self::Horiz, |
| 6209 | _ => Self::TwoD, |
| 6210 | } |
| 6211 | } |
| 6212 | } |
| 6213 | |
| 6214 | /// `nz_map_ctx_offset_1d`: `TX_CLASS_HORIZ`/`TX_CLASS_VERT` share one 32-entry |
| 6215 | /// table (indexed by the position along the transform's *coded* axis -- |
| 6216 | /// `col` for horiz, `row` for vert) that places their contexts past the 2D |
| 6217 | /// table's own 26 rows (`SIG_COEF_CONTEXTS_2D`), 16 rows total |
| 6218 | /// (`SIG_COEF_CONTEXTS_1D`) split 26/31/36. |
| 6219 | pub(crate) fn nz_map_ctx_offset_1d(i: usize) -> usize { |
| 6220 | match i { |
| 6221 | 0 => 26, |
| 6222 | 1 => 31, |
| 6223 | _ => 36, |
| 6224 | } |
| 6225 | } |
| 6226 | |
| 6227 | fn base_ctx( |
| 6228 | grid: &[u8], |
| 6229 | stride: usize, |
| 6230 | row: usize, |
| 6231 | col: usize, |
| 6232 | class: TxClass, |
| 6233 | rect_shape: Option<(usize, usize)>, |
| 6234 | ) -> usize { |
| 6235 | // libaom's `get_nz_map_ctx_from_stats` only special-cases the DC |
| 6236 | // coefficient to ctx 0 for `TX_CLASS_2D` (`(tx_class | coeff_idx) == 0`, |
| 6237 | // txb_common.h) -- `V_DCT`/`H_DCT` (class `Horiz`/`Vert`) still run the DC |
| 6238 | // position through the full 1D neighbour sum and offset table. Skipping |
| 6239 | // that for every class desynced the last (DC) coefficient of a |
| 6240 | // `TX_CLASS_HORIZ` block (lane-av1tx4 final round: real ctx 28 read as |
| 6241 | // 0, which then read one extra `BR` symbol at the max and forced a |
| 6242 | // spurious third Golomb call). |
| 6243 | if class == TxClass::TwoD && row == 0 && col == 0 { |
| 6244 | return 0; |
| 6245 | } |
| 6246 | let offsets: [(usize, usize); 5] = match class { |
| 6247 | TxClass::TwoD => [(1, 0), (0, 1), (1, 1), (2, 0), (0, 2)], |
| 6248 | TxClass::Horiz => [(1, 0), (0, 1), (0, 2), (0, 3), (0, 4)], |
| 6249 | TxClass::Vert => [(1, 0), (0, 1), (2, 0), (3, 0), (4, 0)], |
| 6250 | }; |
| 6251 | let mag: i32 = offsets |
| 6252 | .iter() |
| 6253 | .map(|&(dr, dc)| neighbour(grid, stride, row + dr, col + dc).min(3)) |
| 6254 | .sum(); |
| 6255 | let ctx = ((mag + 1) >> 1).min(4) as usize; |
| 6256 | match class { |
| 6257 | // libaom's CDF *set* for a truncated 64-wide/tall corner resolves |
| 6258 | // through `get_txsize_entropy_ctx` to the square `TX_64X64` (this |
| 6259 | // decoder's `TxbSet::Luma64`), but `get_nz_map_ctx_from_stats` |
| 6260 | // separately indexes `av1_nz_map_ctx_offset` by the raw, un-adjusted |
| 6261 | // `tx_size` -- `TX_32X64`/`TX_64X32` for a superblock-level HORZ/VERT |
| 6262 | // strip, which own a genuinely different table from the square one |
| 6263 | // (lane-sbpart r8 root cause: `(row=1, col=0)` reads ctx 2 under the |
| 6264 | // square table vs the real ctx 13 the rect table gives). |
| 6265 | TxClass::TwoD => { |
| 6266 | // lane-rectsplit r4: apply libaom's GENERATING RULE |
| 6267 | // (`txb_common.h:199-209`) directly instead of indexing the |
| 6268 | // transcribed 5x5 tables. `av1_nz_map_ctx_offset[tx_size]` is a |
| 6269 | // FLAT array read at `coeff_idx`, and `coeff_idx` is COLUMN-major |
| 6270 | // (libaom decomposes it as `col = coeff_idx >> bhl`, `bhl` the |
| 6271 | // ADJUSTED height's log2, 5 for both `TX_64X32` and `TX_32X64`), |
| 6272 | // so 32 consecutive entries are one COLUMN: the [`cdf`] |
| 6273 | // transcriptions are `[col][row]` and this read had them as |
| 6274 | // `[row][col]` -- the transpose, i.e. the other shape's rule with |
| 6275 | // 11 and 16 swapped. Same values as [`base_ctx_rect`] now, stated |
| 6276 | // once; `base_ctx_rect_offsets_match_the_transcribed_tables_over_the_whole_domain` |
| 6277 | // pins the rule against the tables over their whole 5x5 domain. |
| 6278 | if let Some((w, h)) = rect_shape { |
| 6279 | if crate::envflags::env_flag!("EC_NZOFF_DUMP") { |
| 6280 | eprintln!("NZOFF stride={stride} shape={w}x{h} row={row} col={col}"); |
| 6281 | } |
| 6282 | if w < h && row < 2 { |
| 6283 | return ctx + 11; |
| 6284 | } |
| 6285 | if w > h && col < 2 { |
| 6286 | return ctx + 16; |
| 6287 | } |
| 6288 | } |
| 6289 | ctx + cdf::NZ_MAP_CTX_OFFSET_32[row.min(4)][col.min(4)] as usize |
| 6290 | } |
| 6291 | TxClass::Horiz => ctx + nz_map_ctx_offset_1d(col.min(31)), |
| 6292 | TxClass::Vert => ctx + nz_map_ctx_offset_1d(row.min(31)), |
| 6293 | } |
| 6294 | } |
| 6295 | |
| 6296 | /// The five `base_ctx` neighbour taps and the three `br_ctx` ones as FLAT |
| 6297 | /// offsets into the padded level frame (lane-coefread): the tap pattern is a |
| 6298 | /// function of the tx class alone, which is fixed for a whole transform unit, |
| 6299 | /// so resolving it once per unit takes the class match and the `(dr, dc)` |
| 6300 | /// -> `dr * stride + dc` arithmetic out of the per-coefficient path. Same |
| 6301 | /// taps as [`base_ctx`]/[`br_ctx`] read, stated once; |
| 6302 | /// `flat_taps_match_the_row_col_neighbour_reads` pins them against the |
| 6303 | /// original `(dr, dc)` form. |
| 6304 | fn flat_taps(stride: usize, class: TxClass) -> ([usize; 5], [usize; 3]) { |
| 6305 | match class { |
| 6306 | TxClass::TwoD => ( |
| 6307 | [stride, 1, stride + 1, 2 * stride, 2], |
| 6308 | [stride, 1, stride + 1], |
| 6309 | ), |
| 6310 | TxClass::Horiz => ([stride, 1, 2, 3, 4], [stride, 1, 2]), |
| 6311 | TxClass::Vert => ( |
| 6312 | [stride, 1, 2 * stride, 3 * stride, 4 * stride], |
| 6313 | [stride, 1, 2 * stride], |
| 6314 | ), |
| 6315 | } |
| 6316 | } |
| 6317 | |
| 6318 | /// [`base_ctx`]'s neighbour sum, off flat taps: `((sum of five clamped |
| 6319 | /// neighbours) + 1) >> 1`, clamped to 4. |
| 6320 | #[inline(always)] |
| 6321 | fn base_mag(grid: &[u8], idx: usize, taps: &[usize; 5]) -> usize { |
| 6322 | let mut mag = 0u32; |
| 6323 | for &t in taps { |
| 6324 | mag += u32::from(grid[idx + t]).min(3); |
| 6325 | } |
| 6326 | (((mag + 1) >> 1) as usize).min(4) |
| 6327 | } |
| 6328 | |
| 6329 | /// [`br_ctx`]'s neighbour sum, off flat taps. |
| 6330 | #[inline(always)] |
| 6331 | fn br_mag(grid: &[u8], idx: usize, taps: &[usize; 3]) -> usize { |
| 6332 | let mut mag = 0u32; |
| 6333 | for &t in taps { |
| 6334 | mag += u32::from(grid[idx + t]); |
| 6335 | } |
| 6336 | (((mag + 1) >> 1) as usize).min(6) |
| 6337 | } |
| 6338 | |
| 6339 | /// lane-coefread: superseded on the decode path by [`br_mag`] plus the |
| 6340 | /// origin-offset tail the two coefficient readers state inline; kept as the |
| 6341 | /// `(row, col)` form `flat_taps_match_the_row_col_neighbour_reads` pins the |
| 6342 | /// flat one against. |
| 6343 | #[cfg(test)] |
| 6344 | fn br_ctx(grid: &[u8], stride: usize, row: usize, col: usize, class: TxClass) -> usize { |
| 6345 | let extra = match class { |
| 6346 | TxClass::TwoD => neighbour(grid, stride, row + 1, col + 1), |
| 6347 | TxClass::Horiz => neighbour(grid, stride, row, col + 2), |
| 6348 | TxClass::Vert => neighbour(grid, stride, row + 2, col), |
| 6349 | }; |
| 6350 | let mag = neighbour(grid, stride, row + 1, col) |
| 6351 | + neighbour(grid, stride, row, col + 1) |
| 6352 | + extra; |
| 6353 | let mag = (((mag + 1) >> 1).min(6)) as usize; |
| 6354 | if row == 0 && col == 0 { |
| 6355 | return mag; |
| 6356 | } |
| 6357 | let near_origin = match class { |
| 6358 | TxClass::TwoD => row < 2 && col < 2, |
| 6359 | TxClass::Horiz => col == 0, |
| 6360 | TxClass::Vert => row == 0, |
| 6361 | }; |
| 6362 | if near_origin { mag + 7 } else { mag + 14 } |
| 6363 | } |
| 6364 | |
| 6365 | /// `Default_Scan_32x16` (spec 8.4.2, libaom `scan.c`'s `default_scan_32x16`): |
| 6366 | /// transposed from libaom's column-major buffer into this decoder's row-major |
| 6367 | /// `pos = row * 32 + col` (lane-rectwire, verified as a bijection of `0..512` |
| 6368 | /// against the real table). Not the square zigzag formula generalized to |
| 6369 | /// unequal `w`/`h` -- checked and that does NOT reproduce this table. |
| 6370 | const SCAN_32X16: [u16; 512] = [ |
| 6371 | 0, 32, 1, 64, 33, 2, 96, 65, 34, 3, 128, 97, 66, 35, 4, 160, 129, 98, 67, 36, 5, 192, 161, |
| 6372 | 130, 99, 68, 37, 6, 224, 193, 162, 131, 100, 69, 38, 7, 256, 225, 194, 163, 132, 101, 70, 39, |
| 6373 | 8, 288, 257, 226, 195, 164, 133, 102, 71, 40, 9, 320, 289, 258, 227, 196, 165, 134, 103, 72, |
| 6374 | 41, 10, 352, 321, 290, 259, 228, 197, 166, 135, 104, 73, 42, 11, 384, 353, 322, 291, 260, 229, |
| 6375 | 198, 167, 136, 105, 74, 43, 12, 416, 385, 354, 323, 292, 261, 230, 199, 168, 137, 106, 75, 44, |
| 6376 | 13, 448, 417, 386, 355, 324, 293, 262, 231, 200, 169, 138, 107, 76, 45, 14, 480, 449, 418, |
| 6377 | 387, 356, 325, 294, 263, 232, 201, 170, 139, 108, 77, 46, 15, 481, 450, 419, 388, 357, 326, |
| 6378 | 295, 264, 233, 202, 171, 140, 109, 78, 47, 16, 482, 451, 420, 389, 358, 327, 296, 265, 234, |
| 6379 | 203, 172, 141, 110, 79, 48, 17, 483, 452, 421, 390, 359, 328, 297, 266, 235, 204, 173, 142, |
| 6380 | 111, 80, 49, 18, 484, 453, 422, 391, 360, 329, 298, 267, 236, 205, 174, 143, 112, 81, 50, 19, |
| 6381 | 485, 454, 423, 392, 361, 330, 299, 268, 237, 206, 175, 144, 113, 82, 51, 20, 486, 455, 424, |
| 6382 | 393, 362, 331, 300, 269, 238, 207, 176, 145, 114, 83, 52, 21, 487, 456, 425, 394, 363, 332, |
| 6383 | 301, 270, 239, 208, 177, 146, 115, 84, 53, 22, 488, 457, 426, 395, 364, 333, 302, 271, 240, |
| 6384 | 209, 178, 147, 116, 85, 54, 23, 489, 458, 427, 396, 365, 334, 303, 272, 241, 210, 179, 148, |
| 6385 | 117, 86, 55, 24, 490, 459, 428, 397, 366, 335, 304, 273, 242, 211, 180, 149, 118, 87, 56, 25, |
| 6386 | 491, 460, 429, 398, 367, 336, 305, 274, 243, 212, 181, 150, 119, 88, 57, 26, 492, 461, 430, |
| 6387 | 399, 368, 337, 306, 275, 244, 213, 182, 151, 120, 89, 58, 27, 493, 462, 431, 400, 369, 338, |
| 6388 | 307, 276, 245, 214, 183, 152, 121, 90, 59, 28, 494, 463, 432, 401, 370, 339, 308, 277, 246, |
| 6389 | 215, 184, 153, 122, 91, 60, 29, 495, 464, 433, 402, 371, 340, 309, 278, 247, 216, 185, 154, |
| 6390 | 123, 92, 61, 30, 496, 465, 434, 403, 372, 341, 310, 279, 248, 217, 186, 155, 124, 93, 62, 31, |
| 6391 | 497, 466, 435, 404, 373, 342, 311, 280, 249, 218, 187, 156, 125, 94, 63, 498, 467, 436, 405, |
| 6392 | 374, 343, 312, 281, 250, 219, 188, 157, 126, 95, 499, 468, 437, 406, 375, 344, 313, 282, 251, |
| 6393 | 220, 189, 158, 127, 500, 469, 438, 407, 376, 345, 314, 283, 252, 221, 190, 159, 501, 470, 439, |
| 6394 | 408, 377, 346, 315, 284, 253, 222, 191, 502, 471, 440, 409, 378, 347, 316, 285, 254, 223, 503, |
| 6395 | 472, 441, 410, 379, 348, 317, 286, 255, 504, 473, 442, 411, 380, 349, 318, 287, 505, 474, 443, |
| 6396 | 412, 381, 350, 319, 506, 475, 444, 413, 382, 351, 507, 476, 445, 414, 383, 508, 477, 446, 415, |
| 6397 | 509, 478, 447, 510, 479, 511, |
| 6398 | ]; |
| 6399 | |
| 6400 | /// `Default_Scan_16x32`, this decoder's own row-major transcription (see |
| 6401 | /// [`SCAN_32X16`]'s doc comment). |
| 6402 | const SCAN_16X32: [u16; 512] = [ |
| 6403 | 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, |
| 6404 | 66, 81, 96, 7, 22, 37, 52, 67, 82, 97, 112, 8, 23, 38, 53, 68, 83, 98, 113, 128, 9, 24, 39, 54, |
| 6405 | 69, 84, 99, 114, 129, 144, 10, 25, 40, 55, 70, 85, 100, 115, 130, 145, 160, 11, 26, 41, 56, 71, |
| 6406 | 86, 101, 116, 131, 146, 161, 176, 12, 27, 42, 57, 72, 87, 102, 117, 132, 147, 162, 177, 192, |
| 6407 | 13, 28, 43, 58, 73, 88, 103, 118, 133, 148, 163, 178, 193, 208, 14, 29, 44, 59, 74, 89, 104, |
| 6408 | 119, 134, 149, 164, 179, 194, 209, 224, 15, 30, 45, 60, 75, 90, 105, 120, 135, 150, 165, 180, |
| 6409 | 195, 210, 225, 240, 31, 46, 61, 76, 91, 106, 121, 136, 151, 166, 181, 196, 211, 226, 241, 256, |
| 6410 | 47, 62, 77, 92, 107, 122, 137, 152, 167, 182, 197, 212, 227, 242, 257, 272, 63, 78, 93, 108, |
| 6411 | 123, 138, 153, 168, 183, 198, 213, 228, 243, 258, 273, 288, 79, 94, 109, 124, 139, 154, 169, |
| 6412 | 184, 199, 214, 229, 244, 259, 274, 289, 304, 95, 110, 125, 140, 155, 170, 185, 200, 215, 230, |
| 6413 | 245, 260, 275, 290, 305, 320, 111, 126, 141, 156, 171, 186, 201, 216, 231, 246, 261, 276, 291, |
| 6414 | 306, 321, 336, 127, 142, 157, 172, 187, 202, 217, 232, 247, 262, 277, 292, 307, 322, 337, 352, |
| 6415 | 143, 158, 173, 188, 203, 218, 233, 248, 263, 278, 293, 308, 323, 338, 353, 368, 159, 174, 189, |
| 6416 | 204, 219, 234, 249, 264, 279, 294, 309, 324, 339, 354, 369, 384, 175, 190, 205, 220, 235, 250, |
| 6417 | 265, 280, 295, 310, 325, 340, 355, 370, 385, 400, 191, 206, 221, 236, 251, 266, 281, 296, 311, |
| 6418 | 326, 341, 356, 371, 386, 401, 416, 207, 222, 237, 252, 267, 282, 297, 312, 327, 342, 357, 372, |
| 6419 | 387, 402, 417, 432, 223, 238, 253, 268, 283, 298, 313, 328, 343, 358, 373, 388, 403, 418, 433, |
| 6420 | 448, 239, 254, 269, 284, 299, 314, 329, 344, 359, 374, 389, 404, 419, 434, 449, 464, 255, 270, |
| 6421 | 285, 300, 315, 330, 345, 360, 375, 390, 405, 420, 435, 450, 465, 480, 271, 286, 301, 316, 331, |
| 6422 | 346, 361, 376, 391, 406, 421, 436, 451, 466, 481, 496, 287, 302, 317, 332, 347, 362, 377, 392, |
| 6423 | 407, 422, 437, 452, 467, 482, 497, 303, 318, 333, 348, 363, 378, 393, 408, 423, 438, 453, 468, |
| 6424 | 483, 498, 319, 334, 349, 364, 379, 394, 409, 424, 439, 454, 469, 484, 499, 335, 350, 365, 380, |
| 6425 | 395, 410, 425, 440, 455, 470, 485, 500, 351, 366, 381, 396, 411, 426, 441, 456, 471, 486, 501, |
| 6426 | 367, 382, 397, 412, 427, 442, 457, 472, 487, 502, 383, 398, 413, 428, 443, 458, 473, 488, 503, |
| 6427 | 399, 414, 429, 444, 459, 474, 489, 504, 415, 430, 445, 460, 475, 490, 505, 431, 446, 461, 476, |
| 6428 | 491, 506, 447, 462, 477, 492, 507, 463, 478, 493, 508, 479, 494, 509, 495, 510, 511, |
| 6429 | ]; |
| 6430 | |
| 6431 | /// `Default_Scan_4x8` (libaom `scan.c`), this decoder's own row-major |
| 6432 | /// transcription (see [`SCAN_32X16`]'s doc comment for the conversion, which |
| 6433 | /// was re-verified this lane by regenerating [`SCAN_16X8`]/[`SCAN_8X16`] from |
| 6434 | /// the same source). |
| 6435 | const SCAN_4X8: [u16; 32] = [ |
| 6436 | 0, 1, 4, 2, 5, 8, 3, 6, 9, 12, 7, 10, 13, 16, 11, 14, 17, 20, 15, 18, 21, 24, 19, 22, 25, 28, |
| 6437 | 23, 26, 29, 27, 30, 31, |
| 6438 | ]; |
| 6439 | |
| 6440 | /// `Default_Scan_8x4`, same source and conversion as [`SCAN_4X8`]. |
| 6441 | const SCAN_8X4: [u16; 32] = [ |
| 6442 | 0, 8, 1, 16, 9, 2, 24, 17, 10, 3, 25, 18, 11, 4, 26, 19, 12, 5, 27, 20, 13, 6, 28, 21, 14, 7, |
| 6443 | 29, 22, 15, 30, 23, 31, |
| 6444 | ]; |
| 6445 | |
| 6446 | /// `Default_Scan_16x8`, this decoder's own row-major transcription (see |
| 6447 | /// [`SCAN_32X16`]'s doc comment). |
| 6448 | const SCAN_16X8: [u16; 128] = [ |
| 6449 | 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, |
| 6450 | 36, 21, 6, 112, 97, 82, 67, 52, 37, 22, 7, 113, 98, 83, 68, 53, 38, 23, 8, 114, 99, 84, 69, 54, |
| 6451 | 39, 24, 9, 115, 100, 85, 70, 55, 40, 25, 10, 116, 101, 86, 71, 56, 41, 26, 11, 117, 102, 87, |
| 6452 | 72, 57, 42, 27, 12, 118, 103, 88, 73, 58, 43, 28, 13, 119, 104, 89, 74, 59, 44, 29, 14, 120, |
| 6453 | 105, 90, 75, 60, 45, 30, 15, 121, 106, 91, 76, 61, 46, 31, 122, 107, 92, 77, 62, 47, 123, 108, |
| 6454 | 93, 78, 63, 124, 109, 94, 79, 125, 110, 95, 126, 111, 127, |
| 6455 | ]; |
| 6456 | |
| 6457 | /// `Default_Scan_8x16`, this decoder's own row-major transcription (see |
| 6458 | /// [`SCAN_32X16`]'s doc comment). |
| 6459 | const SCAN_8X16: [u16; 128] = [ |
| 6460 | 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, |
| 6461 | 41, 48, 7, 14, 21, 28, 35, 42, 49, 56, 15, 22, 29, 36, 43, 50, 57, 64, 23, 30, 37, 44, 51, 58, |
| 6462 | 65, 72, 31, 38, 45, 52, 59, 66, 73, 80, 39, 46, 53, 60, 67, 74, 81, 88, 47, 54, 61, 68, 75, 82, |
| 6463 | 89, 96, 55, 62, 69, 76, 83, 90, 97, 104, 63, 70, 77, 84, 91, 98, 105, 112, 71, 78, 85, 92, 99, |
| 6464 | 106, 113, 120, 79, 86, 93, 100, 107, 114, 121, 87, 94, 101, 108, 115, 122, 95, 102, 109, 116, |
| 6465 | 123, 103, 110, 117, 124, 111, 118, 125, 119, 126, 127, |
| 6466 | ]; |
| 6467 | |
| 6468 | /// `Default_Scan_32x8`, this decoder's own row-major transcription (see |
| 6469 | /// [`SCAN_32X16`]'s doc comment) -- the chroma plane of a 64x16 |
| 6470 | /// `PARTITION_HORZ_4` superblock strip (lane-tx64x16). libaom stores its |
| 6471 | /// scans column-major (`p = col * height + row`), so a naive copy of |
| 6472 | /// `default_scan_32x8` would silently install the TRANSPOSED 8x32 order |
| 6473 | /// (class reference-layout-not-spec): `default_scan_16x8`'s literal bytes |
| 6474 | /// are byte-identical to this decoder's [`SCAN_8X16`]. |
| 6475 | const SCAN_32X8: [u16; 256] = [ |
| 6476 | 0, 32, 1, 64, 33, 2, 96, 65, 34, 3, 128, 97, 66, 35, 4, 160, 129, 98, 67, 36, 5, 192, 161, 130, |
| 6477 | 99, 68, 37, 6, 224, 193, 162, 131, 100, 69, 38, 7, 225, 194, 163, 132, 101, 70, 39, 8, 226, 195, |
| 6478 | 164, 133, 102, 71, 40, 9, 227, 196, 165, 134, 103, 72, 41, 10, 228, 197, 166, 135, 104, 73, 42, |
| 6479 | 11, 229, 198, 167, 136, 105, 74, 43, 12, 230, 199, 168, 137, 106, 75, 44, 13, 231, 200, 169, |
| 6480 | 138, 107, 76, 45, 14, 232, 201, 170, 139, 108, 77, 46, 15, 233, 202, 171, 140, 109, 78, 47, 16, |
| 6481 | 234, 203, 172, 141, 110, 79, 48, 17, 235, 204, 173, 142, 111, 80, 49, 18, 236, 205, 174, 143, |
| 6482 | 112, 81, 50, 19, 237, 206, 175, 144, 113, 82, 51, 20, 238, 207, 176, 145, 114, 83, 52, 21, 239, |
| 6483 | 208, 177, 146, 115, 84, 53, 22, 240, 209, 178, 147, 116, 85, 54, 23, 241, 210, 179, 148, 117, |
| 6484 | 86, 55, 24, 242, 211, 180, 149, 118, 87, 56, 25, 243, 212, 181, 150, 119, 88, 57, 26, 244, 213, |
| 6485 | 182, 151, 120, 89, 58, 27, 245, 214, 183, 152, 121, 90, 59, 28, 246, 215, 184, 153, 122, 91, |
| 6486 | 60, 29, 247, 216, 185, 154, 123, 92, 61, 30, 248, 217, 186, 155, 124, 93, 62, 31, 249, 218, 187, |
| 6487 | 156, 125, 94, 63, 250, 219, 188, 157, 126, 95, 251, 220, 189, 158, 127, 252, 221, 190, 159, 253, |
| 6488 | 222, 191, 254, 223, 255, |
| 6489 | ]; |
| 6490 | /// `Default_Scan_8x32`, [`SCAN_32X8`]'s transpose (the chroma plane of a |
| 6491 | /// 16x64 `PARTITION_VERT_4` strip). |
| 6492 | const SCAN_8X32: [u16; 256] = [ |
| 6493 | 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, |
| 6494 | 41, 48, 7, 14, 21, 28, 35, 42, 49, 56, 15, 22, 29, 36, 43, 50, 57, 64, 23, 30, 37, 44, 51, 58, |
| 6495 | 65, 72, 31, 38, 45, 52, 59, 66, 73, 80, 39, 46, 53, 60, 67, 74, 81, 88, 47, 54, 61, 68, 75, 82, |
| 6496 | 89, 96, 55, 62, 69, 76, 83, 90, 97, 104, 63, 70, 77, 84, 91, 98, 105, 112, 71, 78, 85, 92, 99, |
| 6497 | 106, 113, 120, 79, 86, 93, 100, 107, 114, 121, 128, 87, 94, 101, 108, 115, 122, 129, 136, 95, |
| 6498 | 102, 109, 116, 123, 130, 137, 144, 103, 110, 117, 124, 131, 138, 145, 152, 111, 118, 125, 132, |
| 6499 | 139, 146, 153, 160, 119, 126, 133, 140, 147, 154, 161, 168, 127, 134, 141, 148, 155, 162, 169, |
| 6500 | 176, 135, 142, 149, 156, 163, 170, 177, 184, 143, 150, 157, 164, 171, 178, 185, 192, 151, 158, |
| 6501 | 165, 172, 179, 186, 193, 200, 159, 166, 173, 180, 187, 194, 201, 208, 167, 174, 181, 188, 195, |
| 6502 | 202, 209, 216, 175, 182, 189, 196, 203, 210, 217, 224, 183, 190, 197, 204, 211, 218, 225, 232, |
| 6503 | 191, 198, 205, 212, 219, 226, 233, 240, 199, 206, 213, 220, 227, 234, 241, 248, 207, 214, 221, |
| 6504 | 228, 235, 242, 249, 215, 222, 229, 236, 243, 250, 223, 230, 237, 244, 251, 231, 238, 245, 252, |
| 6505 | 239, 246, 253, 247, 254, 255, |
| 6506 | ]; |
| 6507 | /// `Default_Scan_16x4`, this decoder's own row-major transcription of |
| 6508 | /// libaom's column-major `default_scan_16x4` (`scan.c:66`) -- the CHROMA |
| 6509 | /// plane of a 32x8 `PARTITION_HORZ_4` strip at the 32x32 level |
| 6510 | /// (lane-tx64x16 r3). The transcription rule (`row * w + col` with |
| 6511 | /// `row = p % h`, `col = p / h`) was validated by REGENERATING the existing |
| 6512 | /// [`SCAN_16X8`] from `default_scan_16x8` byte for byte before these two |
| 6513 | /// were emitted -- a naive copy installs the transpose (class |
| 6514 | /// reference-layout-not-spec). |
| 6515 | const SCAN_16X4: [u16; 64] = [ |
| 6516 | 0, 16, 1, 32, 17, 2, 48, 33, 18, 3, 49, 34, 19, 4, 50, 35, 20, 5, 51, 36, 21, 6, 52, 37, 22, 7, |
| 6517 | 53, 38, 23, 8, 54, 39, 24, 9, 55, 40, 25, 10, 56, 41, 26, 11, 57, 42, 27, 12, 58, 43, 28, 13, |
| 6518 | 59, 44, 29, 14, 60, 45, 30, 15, 61, 46, 31, 62, 47, 63, |
| 6519 | ]; |
| 6520 | /// `Default_Scan_4x16`, [`SCAN_16X4`]'s transpose (the chroma plane of an |
| 6521 | /// 8x32 `PARTITION_VERT_4` strip). |
| 6522 | const SCAN_4X16: [u16; 64] = [ |
| 6523 | 0, 1, 4, 2, 5, 8, 3, 6, 9, 12, 7, 10, 13, 16, 11, 14, 17, 20, 15, 18, 21, 24, 19, 22, 25, 28, |
| 6524 | 23, 26, 29, 32, 27, 30, 33, 36, 31, 34, 37, 40, 35, 38, 41, 44, 39, 42, 45, 48, 43, 46, 49, 52, |
| 6525 | 47, 50, 53, 56, 51, 54, 57, 60, 55, 58, 61, 59, 62, 63, |
| 6526 | ]; |
| 6527 | |
| 6528 | /// [`neighbour`]'s rect form (lane-rectwire): same lane-coef2 padding |
| 6529 | /// (`stride = w + 4`, four spare rows), so the edge test is the zeros the |
| 6530 | /// padding already holds. |
| 6531 | #[inline] |
| 6532 | fn neighbour_rect(grid: &[u8], stride: usize, row: usize, col: usize) -> i32 { |
| 6533 | grid[row * stride + col] as i32 |
| 6534 | } |
| 6535 | |
| 6536 | /// [`base_ctx`]'s `TxClass::TwoD` arm widened to `(w, h)` (lane-rectwire): |
| 6537 | /// the rect generalization is libaom's own comment in |
| 6538 | /// `get_nz_map_ctx_from_stats` (`txb_common.h:189-224`), reduces to the exact |
| 6539 | /// same [`cdf::NZ_MAP_CTX_OFFSET_32`] table when `w == h`. Never called for |
| 6540 | /// `V_DCT`/`H_DCT` -- geometrically impossible at the two size pairs |
| 6541 | /// [`decode_block_rect`] codes (see that function's own doc comment). |
| 6542 | fn base_ctx_rect( |
| 6543 | grid: &[u8], |
| 6544 | stride: usize, |
| 6545 | w: usize, |
| 6546 | h: usize, |
| 6547 | row: usize, |
| 6548 | col: usize, |
| 6549 | class: TxClass, |
| 6550 | ) -> usize { |
| 6551 | // Same `(tx_class | coeff_idx) == 0` guard [`base_ctx`] documents: only |
| 6552 | // the 2D class short-circuits the DC position. |
| 6553 | if class == TxClass::TwoD && row == 0 && col == 0 { |
| 6554 | return 0; |
| 6555 | } |
| 6556 | let offsets: [(usize, usize); 5] = match class { |
| 6557 | TxClass::TwoD => [(1, 0), (0, 1), (1, 1), (2, 0), (0, 2)], |
| 6558 | TxClass::Horiz => [(1, 0), (0, 1), (0, 2), (0, 3), (0, 4)], |
| 6559 | TxClass::Vert => [(1, 0), (0, 1), (2, 0), (3, 0), (4, 0)], |
| 6560 | }; |
| 6561 | let mag: i32 = offsets |
| 6562 | .iter() |
| 6563 | .map(|&(dr, dc)| neighbour_rect(grid, stride, row + dr, col + dc).min(3)) |
| 6564 | .sum(); |
| 6565 | let ctx = ((mag + 1) >> 1).min(4) as usize; |
| 6566 | match class { |
| 6567 | TxClass::TwoD => { |
| 6568 | if w < h && row < 2 { |
| 6569 | return ctx + 11; |
| 6570 | } |
| 6571 | if w > h && col < 2 { |
| 6572 | return ctx + 16; |
| 6573 | } |
| 6574 | ctx + cdf::NZ_MAP_CTX_OFFSET_32[row.min(4)][col.min(4)] as usize |
| 6575 | } |
| 6576 | // Shape-independent in libaom too (`get_nz_map_ctx_from_stats`'s |
| 6577 | // `TX_CLASS_HORIZ`/`VERT` arms index `nz_map_ctx_offset_1d` by the |
| 6578 | // position along one axis only). |
| 6579 | TxClass::Horiz => ctx + nz_map_ctx_offset_1d(col.min(31)), |
| 6580 | TxClass::Vert => ctx + nz_map_ctx_offset_1d(row.min(31)), |
| 6581 | } |
| 6582 | } |
| 6583 | |
| 6584 | /// [`br_ctx`]'s `TxClass::TwoD` arm widened to `(w, h)` (lane-rectwire): the |
| 6585 | /// neighbour-offset math itself is shape-independent, only the boundary |
| 6586 | /// clamp needs the real bound in each axis. |
| 6587 | /// lane-coefread: superseded by [`br_mag`] on the rect decode path too -- |
| 6588 | /// its body was character-for-character [`br_ctx`]'s, which |
| 6589 | /// `flat_taps_match_the_row_col_neighbour_reads` now asserts. |
| 6590 | #[cfg(test)] |
| 6591 | fn br_ctx_rect( |
| 6592 | grid: &[u8], |
| 6593 | stride: usize, |
| 6594 | row: usize, |
| 6595 | col: usize, |
| 6596 | class: TxClass, |
| 6597 | ) -> usize { |
| 6598 | let extra = match class { |
| 6599 | TxClass::TwoD => neighbour_rect(grid, stride, row + 1, col + 1), |
| 6600 | TxClass::Horiz => neighbour_rect(grid, stride, row, col + 2), |
| 6601 | TxClass::Vert => neighbour_rect(grid, stride, row + 2, col), |
| 6602 | }; |
| 6603 | let mag = neighbour_rect(grid, stride, row + 1, col) |
| 6604 | + neighbour_rect(grid, stride, row, col + 1) |
| 6605 | + extra; |
| 6606 | let mag = (((mag + 1) >> 1).min(6)) as usize; |
| 6607 | if row == 0 && col == 0 { |
| 6608 | return mag; |
| 6609 | } |
| 6610 | let near_origin = match class { |
| 6611 | TxClass::TwoD => row < 2 && col < 2, |
| 6612 | TxClass::Horiz => col == 0, |
| 6613 | TxClass::Vert => row == 0, |
| 6614 | }; |
| 6615 | if near_origin { mag + 7 } else { mag + 14 } |
| 6616 | } |
| 6617 | |
| 6618 | fn dc_vote(dc: Option<bool>) -> i32 { |
| 6619 | match dc { |
| 6620 | None => 0, |
| 6621 | Some(true) => -1, |
| 6622 | Some(false) => 1, |
| 6623 | } |
| 6624 | } |
| 6625 | |
| 6626 | fn dc_sign_ctx(vote: i32) -> usize { |
| 6627 | match vote.signum() { |
| 6628 | 0 => 0, |
| 6629 | -1 => 1, |
| 6630 | _ => 2, |
| 6631 | } |
| 6632 | } |
| 6633 | |
| 6634 | /// The `tx_type` a CDF symbol names, given the width of the row it was read |
| 6635 | /// from. |
| 6636 | /// |
| 6637 | /// The row's own width names its set (spec `Tx_Type_Intra_Inv_Set*` / |
| 6638 | /// `Tx_Type_Inter_Inv_Set*`): 17 slots (16 symbols) is `EXT_TX_SET_ALL16`, 13 |
| 6639 | /// (12) `EXT_TX_SET_DTT9_IDTX_1DDCT`, 8 (7) `TX_SET_INTRA_1`, and everything |
| 6640 | /// narrower the reduced `TX_SET_INTRA_2`/`TX_SET_INTER_3` order, whose two |
| 6641 | /// members are a prefix of the five. |
| 6642 | /// |
| 6643 | /// One function rather than a `match` at each reader, so |
| 6644 | /// [`crate::refusal_inventory`]'s enumeration of every (width, symbol) pair |
| 6645 | /// tests the mapping the readers actually use. |
| 6646 | pub(crate) fn tx_type_from_symbol(cdf_len: usize, t: usize) -> Option<TxType> { |
| 6647 | match cdf_len { |
| 6648 | 17 => TxType::from_symbol_all16(t), |
| 6649 | 13 => TxType::from_symbol_set2_12(t), |
| 6650 | 8 => TxType::from_symbol_set1(t), |
| 6651 | _ => TxType::from_symbol(t), |
| 6652 | } |
| 6653 | } |
| 6654 | |
| 6655 | /// [`tx_type_from_symbol`] inverted: which symbol of a `cdf_len`-wide |
| 6656 | /// `tx_type` CDF names `tx_type`, or `None` when that set does not hold it |
| 6657 | /// (class `table-and-reader-move-together` -- the writer picks its symbol |
| 6658 | /// through the reader's own map, so the two cannot drift). |
| 6659 | pub(crate) fn tx_type_symbol(cdf_len: usize, tx_type: TxType) -> Option<usize> { |
| 6660 | (0..cdf_len.saturating_sub(1)).find(|&t| tx_type_from_symbol(cdf_len, t) == Some(tx_type)) |
| 6661 | } |
| 6662 | |
| 6663 | /// The remainder of a level the base and base-range syntax could not reach |
| 6664 | /// (spec 5.11.40): its bit length in unary, then that many of its own bits, |
| 6665 | /// most significant first — the exact inverse of [`crate::tile::write_golomb`]. |
| 6666 | pub(crate) fn read_golomb(dec: &mut SymbolDecoder) -> Result<u32> { |
| 6667 | // THIS CAP IS libaom's OWN, TO THE BIT -- do not "widen" it (lane-golomb2 |
| 6668 | // re-read the source: `av1/decoder/decodetxb.c:22-43` counts the same |
| 6669 | // prefix and calls the 21st zero bit `AOM_CODEC_CORRUPT_FRAME`, "Invalid |
| 6670 | // length in read_golomb"; spec 5.11.40 BREAKS at `length == 20` and 5.11.39 |
| 6671 | // masks the level with `0xFFFFF`, so no conformant stream carries a longer |
| 6672 | // tail). `refusal_inventory::read_golomb_reads_every_value_a_conformant_ |
| 6673 | // stream_can_carry` enumerates that whole value domain. |
| 6674 | // |
| 6675 | // So this refusal never names a reader gap: it is always the SYMPTOM of an |
| 6676 | // earlier desync (class `refusal-hides-a-defect`), and every historical |
| 6677 | // instance was closed by fixing that desync -- lane-dgolomb's |
| 6678 | // `force_integer_mv`, lane-dkey's palette, lane-t900 r12's `uv_mode_grid`. |
| 6679 | // Raising the cap only trades a clean refusal for silent corruption: |
| 6680 | // lane-scaledref r1 tried it (commit ee1f980, reverted in 314ee08) and |
| 6681 | // turned `a_real_aomenc_stream_with_a_superblock_level_horz_vert_partition_decodes_pixel_exact` |
| 6682 | // from a refusal into a frame-0 LUMA MISMATCH. |
| 6683 | let mut length = 1u32; |
| 6684 | while dec.literal(1) == 0 { |
| 6685 | length += 1; |
| 6686 | if length > 20 { |
| 6687 | return Err(unsupported("a Golomb tail longer than this decoder reads")); |
| 6688 | } |
| 6689 | } |
| 6690 | let mut x = 1u32; |
| 6691 | for _ in 1..length { |
| 6692 | x = (x << 1) | dec.literal(1); |
| 6693 | } |
| 6694 | Ok(x - 1) |
| 6695 | } |
| 6696 | |
| 6697 | /// `decode_symbol`'s end-of-block position (spec 5.11.39): the inverse of |
| 6698 | /// [`crate::tile`]'s `write_eob`. |
| 6699 | fn read_eob(dec: &mut SymbolDecoder, coding: &mut TxbTables, class: TxClass) -> usize { |
| 6700 | const GROUP_START: [usize; 12] = [0, 1, 2, 3, 5, 9, 17, 33, 65, 129, 257, 513]; |
| 6701 | const OFFSET_BITS: [u32; 12] = [0, 0, 0, 1, 2, 3, 4, 5, 6, 7, 8, 9]; |
| 6702 | |
| 6703 | let trace = crate::envflags::env_flag!("EC_AV1_TRACE"); |
| 6704 | // libaom's `eob_flag_cdf*` tables carry a second dimension the 2D scan |
| 6705 | // never touches: `TX_CLASS_HORIZ`/`TX_CLASS_VERT` (`V_DCT`/`H_DCT`) adapt |
| 6706 | // a wholly separate CDF from every other tx_type (`decodetxb.c`'s |
| 6707 | // `eob_multi_ctx`). Missing that split desynced the very first |
| 6708 | // `V_DCT`/`H_DCT` TU this decoder ever produced (lane-av1tx4 r5, caught |
| 6709 | // against a real aomdec trace). |
| 6710 | let eob_pt: &mut [u16] = match (class, coding.eob_pt_class1.as_deref_mut()) { |
| 6711 | (TxClass::TwoD, _) | (_, None) => coding.eob_pt, |
| 6712 | (_, Some(class1)) => class1, |
| 6713 | }; |
| 6714 | if crate::envflags::env_flag!("EC_AV1_EOBPT_CDF") { |
| 6715 | let (range, value) = dec.debug_state(); |
| 6716 | eprintln!("EC_AV1_EOBPT_CDF {eob_pt:?} range={range} value={value}"); |
| 6717 | } |
| 6718 | let group = dec.symbol(eob_pt) + 1; |
| 6719 | if trace { |
| 6720 | eprintln!("TRACE eob_pt value={group} rng={}", dec.debug_state().0); |
| 6721 | } |
| 6722 | let bits = OFFSET_BITS[group]; |
| 6723 | let mut offset = 0u32; |
| 6724 | if bits > 0 { |
| 6725 | let top = dec.symbol(&mut coding.eob_extra[group - 3]) as u32; |
| 6726 | if trace { |
| 6727 | eprintln!("TRACE eob_extra ctx={} value={top}", group - 3); |
| 6728 | } |
| 6729 | offset = top << (bits - 1); |
| 6730 | if bits > 1 { |
| 6731 | offset |= dec.literal(bits - 1); |
| 6732 | } |
| 6733 | } |
| 6734 | GROUP_START[group] + offset as usize |
| 6735 | } |
| 6736 | |
| 6737 | /// One transform block's levels, the inverse of [`crate::tile`]'s |
| 6738 | /// `write_coeffs`, returned as a `side * side` grid in raster order, along |
| 6739 | /// with the transform type that grid was coded under (`DCT_DCT` when the |
| 6740 | /// block's `TxbSet` carries no `tx_type` symbol at all, e.g. 32-point and |
| 6741 | /// 64-point transforms, which never code anything else). |
| 6742 | /// |
| 6743 | /// # Errors |
| 6744 | /// Returns an error if the `tx_type` symbol decodes to a value outside its |
| 6745 | /// CDF's own set (a corrupt or unsupported bitstream) — every value the |
| 6746 | /// intra `Tx_Type_Intra_Inv_Set2` and inter `Tx_Type_Inter_Inv_Set3` CDFs can |
| 6747 | /// produce dispatches to a real inverse transform. |
| 6748 | /// `Intra_Mode_To_Tx_Type` (spec 9.3): chroma's transform type is never its |
| 6749 | /// own coded symbol -- `TxbSet::Chroma*`'s `tx_type` slot is always `None` |
| 6750 | /// (a chroma block shares no CDF row with `Luma16`'s mode-indexed one) -- so |
| 6751 | /// libaom's own `intra_mode_to_tx_type` (`blockd.h`) derives it purely from |
| 6752 | /// the plane's own predicted mode instead. Every entry this table can |
| 6753 | /// produce is a member of both `TX_SET_INTRA_1` and `_2` (spec 9.3's two |
| 6754 | /// intra sets differ only in `IDTX`/`V_DCT`/`H_DCT`, none of which this |
| 6755 | /// table ever names), so unlike the CDF-coded luma path there is no |
| 6756 | /// ext-tx-set membership check to fall back from here. |
| 6757 | pub(crate) fn default_intra_tx_type(mode: u8) -> TxType { |
| 6758 | use crate::intra::{ |
| 6759 | D45_PRED, D67_PRED, D113_PRED, D135_PRED, D157_PRED, D203_PRED, DC_PRED, H_PRED, |
| 6760 | PAETH_PRED, SMOOTH_H_PRED, SMOOTH_PRED, SMOOTH_V_PRED, V_PRED, |
| 6761 | }; |
| 6762 | match mode { |
| 6763 | DC_PRED | D45_PRED => TxType::DctDct, |
| 6764 | V_PRED | D113_PRED | D67_PRED | SMOOTH_V_PRED => TxType::AdstDct, |
| 6765 | H_PRED | D157_PRED | D203_PRED | SMOOTH_H_PRED => TxType::DctAdst, |
| 6766 | D135_PRED | SMOOTH_PRED | PAETH_PRED => TxType::AdstAdst, |
| 6767 | other => panic!("intra mode {other} has no Intra_Mode_To_Tx_Type entry"), |
| 6768 | } |
| 6769 | } |
| 6770 | |
| 6771 | /// `is_smooth` (libaom `reconintra.c`): true for `SMOOTH_PRED..=SMOOTH_H_PRED` |
| 6772 | /// (9..=11) — deliberately excludes `PAETH_PRED` (12), which the spec's own |
| 6773 | /// `get_intra_edge_filter_type`/libaom `get_filt_type` never treat as smooth. |
| 6774 | fn is_smooth_mode(mode: usize) -> bool { |
| 6775 | (crate::intra::SMOOTH_PRED as usize..=crate::intra::SMOOTH_H_PRED as usize).contains(&mode) |
| 6776 | } |
| 6777 | |
| 6778 | /// lane-coefread: the per-coefficient trace sites, one `#[cold]` call each. |
| 6779 | /// `eprintln!` builds its `fmt::Arguments` on the stack AT THE CALL SITE, so |
| 6780 | /// nine of them inside the base/sign loops pinned nine sets of values in the |
| 6781 | /// hot loop's frame (the annotated profile was full of `mov ...(%rsp)` spill |
| 6782 | /// traffic). Out of line, the loop keeps its values in registers and the |
| 6783 | /// trace output is character-for-character what it was. |
| 6784 | #[cold] |
| 6785 | #[inline(never)] |
| 6786 | fn trace_base_eob(scan_idx: usize, pos: usize, row: usize, col: usize, ctx: usize, v: i32) { |
| 6787 | eprintln!("TRACE base_eob scan_idx={scan_idx} pos={pos} row={row} col={col} ctx={ctx} value={v}"); |
| 6788 | } |
| 6789 | |
| 6790 | #[cold] |
| 6791 | #[inline(never)] |
| 6792 | fn trace_base(scan_idx: usize, pos: usize, row: usize, col: usize, ctx: usize, v: i32) { |
| 6793 | eprintln!("TRACE base scan_idx={scan_idx} pos={pos} row={row} col={col} ctx={ctx} value={v}"); |
| 6794 | } |
| 6795 | |
| 6796 | #[cold] |
| 6797 | #[inline(never)] |
| 6798 | fn trace_br(scan_idx: usize, pos: usize, row: usize, col: usize, ctx: usize, k: i32) { |
| 6799 | eprintln!("TRACE br scan_idx={scan_idx} pos={pos} row={row} col={col} ctx={ctx} value={k}"); |
| 6800 | } |
| 6801 | |
| 6802 | #[cold] |
| 6803 | #[inline(never)] |
| 6804 | fn coeff_step_base(dec: &SymbolDecoder, scan_idx: usize, apos: usize, ctx: usize, v: i32) { |
| 6805 | let (rng, _) = dec.debug_state(); |
| 6806 | eprintln!("EC_COEFF_STEP tag=base c={scan_idx} pos={apos} ctx={ctx} level={v} rng={rng}"); |
| 6807 | } |
| 6808 | |
| 6809 | #[cold] |
| 6810 | #[inline(never)] |
| 6811 | fn coeff_step_br(dec: &SymbolDecoder, scan_idx: usize, apos: usize, ctx: usize, k: i32) { |
| 6812 | let (rng, _) = dec.debug_state(); |
| 6813 | eprintln!("EC_COEFF_STEP tag=br c={scan_idx} pos={apos} ctx={ctx} k={k} rng={rng}"); |
| 6814 | } |
| 6815 | |
| 6816 | #[cold] |
| 6817 | #[inline(never)] |
| 6818 | fn coeff_step_sign(dec: &SymbolDecoder, c: usize, negative: bool) { |
| 6819 | let (rng, _) = dec.debug_state(); |
| 6820 | eprintln!("EC_COEFF_STEP tag=sign c={c} sign={} rng={rng}", negative as i32); |
| 6821 | } |
| 6822 | |
| 6823 | #[cold] |
| 6824 | #[inline(never)] |
| 6825 | fn coeff_step_post_golomb(dec: &SymbolDecoder, c: usize, level: i32) { |
| 6826 | let (rng, _) = dec.debug_state(); |
| 6827 | eprintln!("EC_COEFF_STEP tag=post_golomb c={c} level={level} rng={rng}"); |
| 6828 | } |
| 6829 | |
| 6830 | #[cold] |
| 6831 | #[inline(never)] |
| 6832 | fn coeff_step_base_eob_level(dec: &SymbolDecoder, level: i32) { |
| 6833 | let (rng, _) = dec.debug_state(); |
| 6834 | eprintln!("EC_COEFF_STEP tag=base_eob level={level} rng={rng}"); |
| 6835 | } |
| 6836 | |
| 6837 | fn read_coeffs( |
| 6838 | dec: &mut SymbolDecoder, |
| 6839 | coding: &mut TxbTables, |
| 6840 | scan: &[u16], |
| 6841 | skip_ctx: usize, |
| 6842 | sign_ctx: usize, |
| 6843 | // The chroma default (spec `Intra_Mode_To_Tx_Type`, see |
| 6844 | // [`default_intra_tx_type`]) for the sizes where `coding.tx_type` codes |
| 6845 | // nothing at all; `DctDct` for every luma call and every chroma call at |
| 6846 | // a size the spec forces to `DCT_DCT` regardless of mode (32-point and |
| 6847 | // up, `EXT_TX_SET_DCTONLY`) -- the caller already folds that in. |
| 6848 | default_tx_type: TxType, |
| 6849 | // `Some((w, h))` with `w != h` when this read is a superblock-level |
| 6850 | // HORZ/VERT strip's truncated luma corner: the CDF *set* resolves square |
| 6851 | // (`side`/`coding` above), but `av1_nz_map_ctx_offset`'s position table |
| 6852 | // is indexed by the real, un-adjusted rectangular shape (lane-sbpart r8 |
| 6853 | // root cause, see [`base_ctx`]). `None` everywhere else -- every other |
| 6854 | // caller's block genuinely is `side` x `side`. |
| 6855 | rect_shape: Option<(usize, usize)>, |
| 6856 | ) -> Result<(Grid, TxType)> { |
| 6857 | let trace = crate::envflags::env_flag!("EC_AV1_TRACE"); |
| 6858 | let side = coding.side; |
| 6859 | if crate::envflags::env_flag!("EC_AV1_EOBPT_CDF") { |
| 6860 | let (range, value) = dec.debug_state(); |
| 6861 | eprintln!("EC_AV1_STATE_BEFORE_TXBSKIP range={range} value={value}"); |
| 6862 | } |
| 6863 | if crate::envflags::env_flag!("EC_ECDUMP_IN") { |
| 6864 | let (range, value) = dec.debug_state(); |
| 6865 | let bit = dec.debug_bitpos(); |
| 6866 | eprintln!( |
| 6867 | "EC_ECDUMP_IN side={side} ctx={skip_ctx} dcctx={sign_ctx} ECIN=({value},{range},{bit})" |
| 6868 | ); |
| 6869 | } |
| 6870 | let dbg_txbskip = if coeff_trace_on() { |
| 6871 | coding.txb_skip[skip_ctx] |
| 6872 | } else { |
| 6873 | [0; 3] |
| 6874 | }; |
| 6875 | let all_zero = dec.symbol(&mut coding.txb_skip[skip_ctx]) == 1; |
| 6876 | if coeff_trace_on() { |
| 6877 | let (rng, _) = dec.debug_state(); |
| 6878 | eprintln!( |
| 6879 | "EC_COEFF_STEP tag=all_zero side={} ctx={skip_ctx} dcctx={sign_ctx} cdf={dbg_txbskip:?} all_zero={} rng={rng}", |
| 6880 | coding.side, |
| 6881 | all_zero as i32 |
| 6882 | ); |
| 6883 | } |
| 6884 | if crate::envflags::env_flag!("EC_AV1_TELL") { |
| 6885 | eprintln!( |
| 6886 | "TELL label=post_txb_skip ctx={skip_ctx} all_zero={} tell={} range={}", |
| 6887 | all_zero as u8, dec.debug_bitpos(), dec.debug_state().0 |
| 6888 | ); |
| 6889 | } |
| 6890 | if trace { |
| 6891 | eprintln!("TRACE all_zero ctx={skip_ctx} value={}", all_zero as i32); |
| 6892 | } |
| 6893 | if crate::envflags::env_flag!("EC_AV1_EOBPT_CDF") { |
| 6894 | let (range, value) = dec.debug_state(); |
| 6895 | eprintln!("EC_AV1_STATE_AFTER_TXBSKIP range={range} value={value}"); |
| 6896 | } |
| 6897 | if all_zero { |
| 6898 | // lane-coefgrid: the all-zero unit (60% of the 4K segment's) names the |
| 6899 | // shared zero grid instead of allocating and zeroing its own. |
| 6900 | return Ok((Grid::Zero(side * side), TxType::DctDct)); |
| 6901 | } |
| 6902 | let mut grid = vec![0i32; side * side]; |
| 6903 | let mut tx_type = default_tx_type; |
| 6904 | if let Some(tx_type_cdf) = coding.tx_type.as_deref_mut() { |
| 6905 | // The CDF row's own width names its set (lane-cdffwd2: extended |
| 6906 | // past the original two-way intra split once wider `reduced_tx_set |
| 6907 | // == 0` inter sets joined the table): 17 slots (16 symbols) is |
| 6908 | // `EXT_TX_SET_ALL16`, 13 (12 symbols) `EXT_TX_SET_DTT9_IDTX_1DDCT`, |
| 6909 | // 8 (7 symbols) `TX_SET_INTRA_1`, everything else (6 slots, 5 |
| 6910 | // symbols) the reduced `TX_SET_INTRA_2`/`TX_SET_INTER_3` two sets |
| 6911 | // share their symbol order with (`Tx_Type_Inter_Inv_Set3`'s two |
| 6912 | // members are a prefix of `Tx_Type_Intra_Inv_Set2`'s five). |
| 6913 | let len = tx_type_cdf.len(); |
| 6914 | if crate::envflags::env_flag!("EC_AV1_EOBPT_CDF") && len == 3 { |
| 6915 | eprintln!("EC_AV1_TXTYPE32_CDF {tx_type_cdf:?}"); |
| 6916 | } |
| 6917 | let t = dec.symbol(tx_type_cdf); |
| 6918 | crate::census::tx_type(len, t); |
| 6919 | if coeff_trace_on() { |
| 6920 | let (rng, _) = dec.debug_state(); |
| 6921 | eprintln!("EC_COEFF_STEP tag=tx_type len={len} rng={rng}"); |
| 6922 | } |
| 6923 | if crate::envflags::env_flag!("EC_AV1_EOBPT_CDF") { |
| 6924 | let (range, value) = dec.debug_state(); |
| 6925 | eprintln!("EC_AV1_STATE_AFTER_TXTYPE range={range} value={value}"); |
| 6926 | } |
| 6927 | if trace { |
| 6928 | eprintln!("TRACE tx_type value={t} len={len}"); |
| 6929 | } |
| 6930 | tx_type = tx_type_from_symbol(len, t) |
| 6931 | .ok_or_else(|| unsupported(format!("a tx_type symbol outside its CDF's own set: {t}")))?; |
| 6932 | } |
| 6933 | |
| 6934 | let class = TxClass::of(tx_type); |
| 6935 | if class != TxClass::TwoD { |
| 6936 | hit!(TX_CLASS1_HITS); |
| 6937 | hit!(RECT_CLASS1_HITS); |
| 6938 | } |
| 6939 | let eob = read_eob(dec, coding, class); |
| 6940 | if trace { |
| 6941 | eprintln!("TRACE eob value={eob}"); |
| 6942 | } |
| 6943 | let ec_trace_coeff = coeff_trace_on(); |
| 6944 | if ec_trace_coeff { |
| 6945 | let (rng, _) = dec.debug_state(); |
| 6946 | let cls = match class { TxClass::TwoD => "2d", TxClass::Horiz => "horiz", TxClass::Vert => "vert" }; |
| 6947 | eprintln!("EC_COEFF_STEP tag=eob eob={eob} tx={tx_type:?} class={cls} rng={rng}"); |
| 6948 | } |
| 6949 | let class_scan; |
| 6950 | let scan: &[u16] = if class == TxClass::TwoD { |
| 6951 | scan |
| 6952 | } else { |
| 6953 | class_scan = class_scan_table(side, class); |
| 6954 | &class_scan |
| 6955 | }; |
| 6956 | // `side` is one of 4/8/16/32 (every `TxbTables` row), so the per- |
| 6957 | // coefficient `pos / side`, `pos % side` was a hardware divide where a |
| 6958 | // shift and a mask say the same thing, and the level buffer fits a fixed |
| 6959 | // 32x32 frame of stack instead of a heap allocation per transform block. |
| 6960 | debug_assert!(side.is_power_of_two() && side <= 32); |
| 6961 | let sh = side.trailing_zeros(); |
| 6962 | let col_mask = side - 1; |
| 6963 | // lane-coef2: padded like libaom's own level buffer (see [`neighbour`]): |
| 6964 | // `stride = side + 4` columns and four spare rows, so the widest read |
| 6965 | // `base_ctx` makes -- `(row + 4, col)` / `(row, col + 4)` with |
| 6966 | // `row, col <= side - 1` -- stays inside `(side + 4) * (side + 4)`, at |
| 6967 | // most `36 * 36`. Only `col < side`, `row < side` cells are ever written. |
| 6968 | let stride = side + 4; |
| 6969 | let mut levels_buf = [0u8; 36 * 36]; |
| 6970 | let levels = &mut levels_buf[..stride * stride]; |
| 6971 | // lane-coefread: the sign pass used to re-walk `scan[..eob]`, redo the |
| 6972 | // `pos >> sh` / `& mask` decomposition and load the level back out of the |
| 6973 | // padded frame just to skip the zeros. The base pass already knows both, |
| 6974 | // so it records `(scan_idx << 4) | level` for each NON-ZERO coefficient |
| 6975 | // (`scan_idx < 1024`, `level <= 15` -- both loops assert it), and the sign |
| 6976 | // pass walks that list in reverse (the base pass runs in reverse scan |
| 6977 | // order, so reversing it is forward scan order: the same coefficients, in |
| 6978 | // the same order, with the same `c` the trace prints). |
| 6979 | let mut nz_buf = [0u16; 1024]; |
| 6980 | let mut nz_n = 0usize; |
| 6981 | let (base_taps, br_taps) = flat_taps(stride, class); |
| 6982 | for scan_idx in (0..eob).rev() { |
| 6983 | let pos = scan[scan_idx] as usize; |
| 6984 | let (row, col) = (pos >> sh, pos & col_mask); |
| 6985 | let idx = row * stride + col; |
| 6986 | let level = if scan_idx == eob - 1 { |
| 6987 | let ctx = eob_coeff_ctx(scan_idx, side * side); |
| 6988 | let v = dec.symbol(&mut coding.base_eob[ctx]) as i32 + 1; |
| 6989 | if trace { |
| 6990 | trace_base_eob(scan_idx, pos, row, col, ctx, v); |
| 6991 | } |
| 6992 | v |
| 6993 | } else { |
| 6994 | let ctx = if class == TxClass::TwoD && rect_shape.is_none() { |
| 6995 | if row == 0 && col == 0 { |
| 6996 | 0 |
| 6997 | } else { |
| 6998 | base_mag(levels, idx, &base_taps) |
| 6999 | + cdf::NZ_MAP_CTX_OFFSET_32[row.min(4)][col.min(4)] as usize |
| 7000 | } |
| 7001 | } else { |
| 7002 | base_ctx(levels, stride, row, col, class, rect_shape) |
| 7003 | }; |
| 7004 | let v = dec.symbol(&mut coding.base[ctx]) as i32; |
| 7005 | if ec_trace_coeff { |
| 7006 | // `pos` in libaom's column-major `coeff_idx` convention, so |
| 7007 | // the ladder lines up with instrumented `aomdec`. |
| 7008 | coeff_step_base(dec, scan_idx, col * side + row, ctx, v); |
| 7009 | } |
| 7010 | if trace { |
| 7011 | trace_base(scan_idx, pos, row, col, ctx, v); |
| 7012 | } |
| 7013 | v |
| 7014 | }; |
| 7015 | let level = if level > NUM_BASE_LEVELS { |
| 7016 | let ctx = { |
| 7017 | let mag = br_mag(levels, idx, &br_taps); |
| 7018 | if row == 0 && col == 0 { |
| 7019 | mag |
| 7020 | } else { |
| 7021 | let near_origin = match class { |
| 7022 | TxClass::TwoD => row < 2 && col < 2, |
| 7023 | TxClass::Horiz => col == 0, |
| 7024 | TxClass::Vert => row == 0, |
| 7025 | }; |
| 7026 | if near_origin { mag + 7 } else { mag + 14 } |
| 7027 | } |
| 7028 | }; |
| 7029 | let mut level = level; |
| 7030 | let mut sent = 0; |
| 7031 | loop { |
| 7032 | let k = dec.symbol(&mut coding.br[ctx]) as i32; |
| 7033 | if ec_trace_coeff { |
| 7034 | // lane-leaf8tx r3: the `br` rung was missing from THIS |
| 7035 | // path's trace, so the cross-decoder range ladder was |
| 7036 | // blind to every base-range symbol and mis-reported the |
| 7037 | // first divergence (class [[gate-blind-to-feature]]). |
| 7038 | coeff_step_br(dec, scan_idx, col * side + row, ctx, k); |
| 7039 | } |
| 7040 | if trace { |
| 7041 | trace_br(scan_idx, pos, row, col, ctx, k); |
| 7042 | } |
| 7043 | level += k; |
| 7044 | sent += BR_STEP; |
| 7045 | if k < BR_STEP || sent >= COEFF_BASE_RANGE { |
| 7046 | break; |
| 7047 | } |
| 7048 | } |
| 7049 | level |
| 7050 | } else { |
| 7051 | level |
| 7052 | }; |
| 7053 | if ec_trace_coeff && scan_idx == eob - 1 { |
| 7054 | coeff_step_base_eob_level(dec, level); |
| 7055 | } |
| 7056 | // Every coded level here is a magnitude in `0..=15` (base <= 3 plus |
| 7057 | // at most `COEFF_BASE_RANGE` from the `br` loop; the Golomb tail is |
| 7058 | // added in the sign pass below, straight into `grid`), so the context |
| 7059 | // frame is bytes -- the five-tap sum in [`base_ctx`]/[`br_ctx`] is a |
| 7060 | // byte gather (lane-coefbuf). |
| 7061 | debug_assert!(level >= 0 && level <= 15, "level {level} past the u8 frame"); |
| 7062 | levels[idx] = level as u8; |
| 7063 | if level != 0 { |
| 7064 | nz_buf[nz_n] = ((scan_idx as u16) << 4) | level as u16; |
| 7065 | nz_n += 1; |
| 7066 | } |
| 7067 | } |
| 7068 | if ec_trace_coeff { |
| 7069 | let (rng, _) = dec.debug_state(); |
| 7070 | eprintln!("EC_COEFF_STEP tag=after_bases rng={rng}"); |
| 7071 | } |
| 7072 | |
| 7073 | for &packed in nz_buf[..nz_n].iter().rev() { |
| 7074 | let c = (packed >> 4) as usize; |
| 7075 | let pos = scan[c] as usize; |
| 7076 | let level = (packed & 15) as i32; |
| 7077 | let negative = if pos == 0 { |
| 7078 | let v = dec.symbol(&mut coding.dc_sign[sign_ctx]) == 1; |
| 7079 | if trace { |
| 7080 | eprintln!("TRACE dc_sign ctx={sign_ctx} value={}", v as i32); |
| 7081 | } |
| 7082 | v |
| 7083 | } else { |
| 7084 | dec.literal(1) == 1 |
| 7085 | }; |
| 7086 | if ec_trace_coeff { |
| 7087 | coeff_step_sign(dec, c, negative); |
| 7088 | } |
| 7089 | let level = if level > MAX_BR_LEVEL { |
| 7090 | let g = read_golomb(dec)?; |
| 7091 | if trace { |
| 7092 | eprintln!("TRACE golomb pos={pos} value={g}"); |
| 7093 | } |
| 7094 | level + g as i32 |
| 7095 | } else { |
| 7096 | level |
| 7097 | }; |
| 7098 | if ec_trace_coeff { |
| 7099 | coeff_step_post_golomb(dec, c, level); |
| 7100 | } |
| 7101 | grid[pos] = if negative { -level } else { level }; |
| 7102 | } |
| 7103 | Ok((Grid::Own(grid), tx_type)) |
| 7104 | } |
| 7105 | |
| 7106 | /// [`read_coeffs`] widened to `(w, h)` (lane-rectwire; lane-rectx added the |
| 7107 | /// `tx_type` read [`TxbSet::LumaRect16x8`] needs), restricted to |
| 7108 | /// `TxClass::TwoD` -- the only class any rect size pair this decoder codes |
| 7109 | /// can ever produce, since every `tx_type` this decoder's rect sets can read |
| 7110 | /// (`EXT_TX_SET_DTT4_IDTX`'s five members) is itself 2D: refuses by name |
| 7111 | /// rather than guess-decode if a non-2D class ever shows up here, since |
| 7112 | /// neither `base_ctx_rect`/`br_ctx_rect` nor [`class_scan_table`] have a rect |
| 7113 | /// form for those. |
| 7114 | |
| 7115 | fn read_coeffs_rect( |
| 7116 | dec: &mut SymbolDecoder, |
| 7117 | coding: &mut TxbTables, |
| 7118 | scan: &[u16], |
| 7119 | w: usize, |
| 7120 | h: usize, |
| 7121 | skip_ctx: usize, |
| 7122 | sign_ctx: usize, |
| 7123 | default_tx_type: TxType, |
| 7124 | ) -> Result<(Grid, TxType)> { |
| 7125 | let rect_trace = coeff_trace_on(); |
| 7126 | let entry_rng = dec.debug_state().0; |
| 7127 | let all_zero = dec.symbol(&mut coding.txb_skip[skip_ctx]) == 1; |
| 7128 | if rect_trace { |
| 7129 | eprintln!( |
| 7130 | "EC_COEFF_STEP tag=all_zero plane=0 ctx={skip_ctx} entry={entry_rng} all_zero={} rng={}", |
| 7131 | all_zero as i32, |
| 7132 | dec.debug_state().0 |
| 7133 | ); |
| 7134 | } |
| 7135 | if all_zero { |
| 7136 | return Ok((Grid::Zero(w * h), TxType::DctDct)); |
| 7137 | } |
| 7138 | let mut grid = vec![0i32; w * h]; |
| 7139 | hit!(RECT_COEFF_TU_HITS); |
| 7140 | // lane-tx4x8: a 4x8/8x4 luma TU DOES carry a `tx_type` symbol (its set is |
| 7141 | // `EXT_TX_SET_DTT4_IDTX(_1DDCT)`, `tx_size_sqr_up == TX_8X8`), including |
| 7142 | // the `V_DCT`/`H_DCT` members whose 1D tx class needs its own scan and |
| 7143 | // context offsets -- both of which now have rect forms |
| 7144 | // ([`class_scan_table_wh`], [`base_ctx_rect`]/[`br_ctx_rect`]'s `class`). |
| 7145 | // The rect sets that carry no symbol at all still pass `None` here. |
| 7146 | let mut tx_type = default_tx_type; |
| 7147 | if let Some(tx_type_cdf) = coding.tx_type.as_deref_mut() { |
| 7148 | let len = tx_type_cdf.len(); |
| 7149 | let t = dec.symbol(tx_type_cdf); |
| 7150 | crate::census::tx_type(len, t); |
| 7151 | tx_type = tx_type_from_symbol(len, t) |
| 7152 | .ok_or_else(|| unsupported(format!("a tx_type symbol outside its CDF's own set: {t}")))?; |
| 7153 | if rect_trace { |
| 7154 | eprintln!("EC_COEFF_STEP tag=tx_type plane=0 rng={}", dec.debug_state().0); |
| 7155 | } |
| 7156 | } |
| 7157 | let class = TxClass::of(tx_type); |
| 7158 | if class != TxClass::TwoD { |
| 7159 | hit!(TX_CLASS1_HITS); |
| 7160 | } |
| 7161 | let eob = read_eob(dec, coding, class); |
| 7162 | if rect_trace { |
| 7163 | eprintln!("EC_COEFF_STEP tag=eob eob={eob} rng={}", dec.debug_state().0); |
| 7164 | } |
| 7165 | let class_scan; |
| 7166 | let scan: &[u16] = if class == TxClass::TwoD { |
| 7167 | scan |
| 7168 | } else { |
| 7169 | class_scan = class_scan_table_wh(w, h, class); |
| 7170 | &class_scan |
| 7171 | }; |
| 7172 | // lane-coef2: a heap `vec![0i32; w * h]` per rect transform unit became a |
| 7173 | // stack frame sized to the unit -- same reasoning as [`read_coeffs`]: |
| 7174 | // `neighbour_rect` returns 0 outside `(w, h)` and every write is |
| 7175 | // `levels[pos]` with `pos < w * h`. `w` is a power of two at every AV1 |
| 7176 | // rect size, so the per-coefficient `pos / w`, `pos % w` is a shift and a |
| 7177 | // mask rather than a hardware divide. |
| 7178 | let stride = w + 4; |
| 7179 | let n = stride * (h + 4); |
| 7180 | if !(w.is_power_of_two() && n <= 36 * 36) { |
| 7181 | panic!("rect coeff scratch w={w} h={h} n={n}"); |
| 7182 | } |
| 7183 | let (mut b96, mut b256, mut b768); |
| 7184 | // Byte levels, same `0..=15` bound [`read_coeffs`] states. |
| 7185 | let levels: &mut [u8] = if n <= 96 { |
| 7186 | b96 = [0u8; 96]; |
| 7187 | &mut b96[..n] |
| 7188 | } else if n <= 256 { |
| 7189 | b256 = [0u8; 256]; |
| 7190 | &mut b256[..n] |
| 7191 | } else { |
| 7192 | b768 = [0u8; 768]; |
| 7193 | &mut b768[..n] |
| 7194 | }; |
| 7195 | let wsh = w.trailing_zeros(); |
| 7196 | let wmask = w - 1; |
| 7197 | // lane-coefread: the non-zero list [`read_coeffs`] documents. |
| 7198 | let mut nz_buf = [0u16; 1024]; |
| 7199 | let mut nz_n = 0usize; |
| 7200 | let (base_taps, br_taps) = flat_taps(stride, class); |
| 7201 | let (wlt, wgt) = (w < h, w > h); |
| 7202 | for scan_idx in (0..eob).rev() { |
| 7203 | let pos = scan[scan_idx] as usize; |
| 7204 | let (row, col) = (pos >> wsh, pos & wmask); |
| 7205 | let idx = row * stride + col; |
| 7206 | // libaom's column-major `coeff_idx`, for ladder comparison. |
| 7207 | let _apos = col * h + row; |
| 7208 | let level = if scan_idx == eob - 1 { |
| 7209 | let ctx = eob_coeff_ctx(scan_idx, w * h); |
| 7210 | let v = dec.symbol(&mut coding.base_eob[ctx]) as i32 + 1; |
| 7211 | if rect_trace { |
| 7212 | eprintln!( |
| 7213 | "EC_COEFF_STEP tag=base_eob c={scan_idx} pos={pos} ctx={ctx} level={v} rng={}", |
| 7214 | dec.debug_state().0 |
| 7215 | ); |
| 7216 | } |
| 7217 | v |
| 7218 | } else { |
| 7219 | let ctx = if class == TxClass::TwoD { |
| 7220 | if row == 0 && col == 0 { |
| 7221 | 0 |
| 7222 | } else { |
| 7223 | let m = base_mag(levels, idx, &base_taps); |
| 7224 | if wlt && row < 2 { |
| 7225 | m + 11 |
| 7226 | } else if wgt && col < 2 { |
| 7227 | m + 16 |
| 7228 | } else { |
| 7229 | m + cdf::NZ_MAP_CTX_OFFSET_32[row.min(4)][col.min(4)] as usize |
| 7230 | } |
| 7231 | } |
| 7232 | } else { |
| 7233 | base_ctx_rect(levels, stride, w, h, row, col, class) |
| 7234 | }; |
| 7235 | let v = dec.symbol(&mut coding.base[ctx]) as i32; |
| 7236 | if rect_trace { |
| 7237 | eprintln!( |
| 7238 | "EC_COEFF_STEP tag=base c={scan_idx} pos={pos} ctx={ctx} level={v} rng={}", |
| 7239 | dec.debug_state().0 |
| 7240 | ); |
| 7241 | } |
| 7242 | v |
| 7243 | }; |
| 7244 | let level = if level > NUM_BASE_LEVELS { |
| 7245 | let ctx = { |
| 7246 | let mag = br_mag(levels, idx, &br_taps); |
| 7247 | if row == 0 && col == 0 { |
| 7248 | mag |
| 7249 | } else { |
| 7250 | let near_origin = match class { |
| 7251 | TxClass::TwoD => row < 2 && col < 2, |
| 7252 | TxClass::Horiz => col == 0, |
| 7253 | TxClass::Vert => row == 0, |
| 7254 | }; |
| 7255 | if near_origin { mag + 7 } else { mag + 14 } |
| 7256 | } |
| 7257 | }; |
| 7258 | let mut level = level; |
| 7259 | let mut sent = 0; |
| 7260 | loop { |
| 7261 | let k = dec.symbol(&mut coding.br[ctx]) as i32; |
| 7262 | if rect_trace { |
| 7263 | eprintln!( |
| 7264 | "EC_COEFF_STEP tag=br c={scan_idx} pos={pos} ctx={ctx} k={k} rng={}", |
| 7265 | dec.debug_state().0 |
| 7266 | ); |
| 7267 | } |
| 7268 | level += k; |
| 7269 | sent += BR_STEP; |
| 7270 | if k < BR_STEP || sent >= COEFF_BASE_RANGE { |
| 7271 | break; |
| 7272 | } |
| 7273 | } |
| 7274 | level |
| 7275 | } else { |
| 7276 | level |
| 7277 | }; |
| 7278 | debug_assert!(level >= 0 && level <= 15, "level {level} past the u8 frame"); |
| 7279 | levels[idx] = level as u8; |
| 7280 | if level != 0 { |
| 7281 | nz_buf[nz_n] = ((scan_idx as u16) << 4) | level as u16; |
| 7282 | nz_n += 1; |
| 7283 | } |
| 7284 | if rect_trace { |
| 7285 | let (rng, _) = dec.debug_state(); |
| 7286 | eprintln!("EC_COEFF_STEP tag=base c={scan_idx} pos={pos} level={level} rng={rng}"); |
| 7287 | } |
| 7288 | } |
| 7289 | if rect_trace { |
| 7290 | let (rng, _) = dec.debug_state(); |
| 7291 | eprintln!("EC_COEFF_STEP tag=after_bases rng={rng}"); |
| 7292 | } |
| 7293 | for &packed in nz_buf[..nz_n].iter().rev() { |
| 7294 | let pos = scan[(packed >> 4) as usize] as usize; |
| 7295 | let level = (packed & 15) as i32; |
| 7296 | let negative = if pos == 0 { |
| 7297 | dec.symbol(&mut coding.dc_sign[sign_ctx]) == 1 |
| 7298 | } else { |
| 7299 | dec.literal(1) == 1 |
| 7300 | }; |
| 7301 | if rect_trace { |
| 7302 | let (rng, _) = dec.debug_state(); |
| 7303 | eprintln!("EC_COEFF_STEP tag=sign_rect pos={pos} sign={} dcctx={sign_ctx} rng={rng}", u8::from(negative)); |
| 7304 | } |
| 7305 | let level = if level > MAX_BR_LEVEL { |
| 7306 | let g = read_golomb(dec)?; |
| 7307 | level + g as i32 |
| 7308 | } else { |
| 7309 | level |
| 7310 | }; |
| 7311 | grid[pos] = if negative { -level } else { level }; |
| 7312 | } |
| 7313 | // lane-d792: the rect unit's LEVEL grid in row-major (w) order, the |
| 7314 | // twin of the oracle's `EC_DQCOEFF` rung -- a residual mismatch whose |
| 7315 | // grids differ is a reader defect, one whose grids agree is a transform |
| 7316 | // defect. |
| 7317 | if crate::envflags::env_flag!("EC_DQCOEFF") { |
| 7318 | let nz: Vec<String> = grid |
| 7319 | .iter() |
| 7320 | .enumerate() |
| 7321 | .filter(|&(_, &v)| v != 0) |
| 7322 | .map(|(i, &v)| format!("{}/{}:{v}", i / w, i % w)) |
| 7323 | .collect(); |
| 7324 | eprintln!("OUR_LEVELS w={w} h={h} eob={eob} tx={tx_type:?} nz={}", nz.join(",")); |
| 7325 | } |
| 7326 | Ok((Grid::Own(grid), tx_type)) |
| 7327 | } |
| 7328 | |
| 7329 | /// [`read_coeffs_rect`] for a CHROMA plane, carrying the monochrome gate the |
| 7330 | /// [`read_plane`] family already has. |
| 7331 | /// |
| 7332 | /// A monochrome frame has `NumPlanes == 1`, so libaom's coefficient loop |
| 7333 | /// (`for (plane = 0; plane < av1_num_planes(cm); ++plane)`) never reaches a |
| 7334 | /// chroma plane: no u/v coefficient symbol is coded, and consuming one moves |
| 7335 | /// the arithmetic decoder off the stream for the rest of the tile. [`read_plane`] |
| 7336 | /// enforces that for every strip that reads its chroma through it, but the rect |
| 7337 | /// strip paths ([`decode_block_rect`], [`decode_leaf_rect`], |
| 7338 | /// [`decode_block_rect4`], [`decode_rect4_16_strip`], [`decode_block_rect64`]) |
| 7339 | /// call `read_coeffs_rect` on planes 1/2 DIRECTLY, so each of those sites takes |
| 7340 | /// this gate instead (class `reader-gate-not-on-every-path`). One un-gated |
| 7341 | /// chroma read desyncs at the FIRST rect leaf of the frame and every later |
| 7342 | /// symbol -- and therefore every later pixel -- is decoded from the wrong |
| 7343 | /// place, without ever tripping a refusal (the desynced values stay inside |
| 7344 | /// every coded alphabet). |
| 7345 | fn read_chroma_coeffs_rect( |
| 7346 | dec: &mut SymbolDecoder, |
| 7347 | coding: &mut TxbTables, |
| 7348 | scan: &[u16], |
| 7349 | w: usize, |
| 7350 | h: usize, |
| 7351 | skip_ctx: usize, |
| 7352 | sign_ctx: usize, |
| 7353 | default_tx_type: TxType, |
| 7354 | fctx: &crate::decode::FrameCtx, |
| 7355 | ) -> Result<(Grid, TxType)> { |
| 7356 | if mono(fctx) { |
| 7357 | return Ok((Grid::Zero(w * h), TxType::DctDct)); |
| 7358 | } |
| 7359 | read_coeffs_rect(dec, coding, scan, w, h, skip_ctx, sign_ctx, default_tx_type) |
| 7360 | } |
| 7361 | |
| 7362 | /// What one coded block leaves behind for the blocks that read it as a |
| 7363 | /// neighbour: whether it coded anything at all, and the sign of its DC — |
| 7364 | /// [`crate::tile`]'s own private `Neighbour`. |
| 7365 | #[derive(Clone, Copy, Default)] |
| 7366 | struct Neighbour { |
| 7367 | /// The transform unit's cumulative coefficient level (spec `cul_level`, |
| 7368 | /// `decodetxb.c`'s `read_coeffs_txb`): the sum of every coded level's |
| 7369 | /// magnitude, clamped to `COEFF_CONTEXT_MASK` (7). A plain "coded or not" |
| 7370 | /// boolean (`level != 0`) is enough for every context this decoder read |
| 7371 | /// until now, but the *luma* `txb_skip_ctx` of a transform unit smaller |
| 7372 | /// than its own block (`TxMode::Select`, spec `get_txb_ctx_general`) |
| 7373 | /// needs the actual magnitude tier of its above/left neighbours, not |
| 7374 | /// just whether they coded anything. |
| 7375 | level: u8, |
| 7376 | dc: Option<bool>, |
| 7377 | } |
| 7378 | |
| 7379 | /// `dst[start .. start + n] = v`, with the same per-cell bounds behaviour the |
| 7380 | /// per-4x4-cell `get_mut` loops it replaces had: a cell past the end of the |
| 7381 | /// grid is dropped, never a panic. One `memset` per grid row instead of a |
| 7382 | /// bounds check per cell (lane-mi) -- the index span a row of a mi rect covers |
| 7383 | /// is contiguous, so this writes exactly the cells the cell loop did, |
| 7384 | /// including the ones a `mi_c + cc` past `skip_grid_cols_mi` folded into the |
| 7385 | /// next row. |
| 7386 | #[inline] |
| 7387 | fn fill_span<T: Copy>(dst: &mut [T], start: usize, n: usize, v: T) { |
| 7388 | let end = (start + n).min(dst.len()); |
| 7389 | if start < end { |
| 7390 | dst[start..end].fill(v); |
| 7391 | } |
| 7392 | } |
| 7393 | |
| 7394 | fn neighbour_state(grid: &[i32]) -> Neighbour { |
| 7395 | // lane-coefgrid: the shared zero grid sums to zero by construction; this |
| 7396 | // sum has no early exit, so the 60% of units that code nothing used to |
| 7397 | // walk their whole grid here. |
| 7398 | if is_zero_grid(grid) { |
| 7399 | return Neighbour::default(); |
| 7400 | } |
| 7401 | let cul: u32 = grid.iter().map(|&l| l.unsigned_abs()).sum(); |
| 7402 | Neighbour { |
| 7403 | level: cul.min(7) as u8, |
| 7404 | dc: (grid[0] != 0).then_some(grid[0] < 0), |
| 7405 | } |
| 7406 | } |
| 7407 | |
| 7408 | /// spec `get_txb_ctx_general`'s `skip_contexts[top][left]` table (plane 0, |
| 7409 | /// transform unit smaller than the block it sits in): `top`/`left` are the |
| 7410 | /// above/left neighbours' magnitude tiers, each already clamped to 4. |
| 7411 | pub(crate) const SKIP_CONTEXTS: [[usize; 5]; 5] = [ |
| 7412 | [1, 2, 2, 2, 3], |
| 7413 | [2, 4, 4, 4, 5], |
| 7414 | [2, 4, 4, 4, 5], |
| 7415 | [2, 4, 4, 4, 5], |
| 7416 | [3, 5, 5, 5, 6], |
| 7417 | ]; |
| 7418 | |
| 7419 | /// The `skip` flag, its written-instrument bit, and the covering coded |
| 7420 | /// block's origin and dims, packed into ONE 8-byte cell (lane-publish): the |
| 7421 | /// four maps are written together by `Neighbours::fill_skip_grid_rect` and by |
| 7422 | /// nothing else, so a block's row is one fill instead of four, and the |
| 7423 | /// deblocker's `pu_edge` reads origin and dims off one cache line. |
| 7424 | #[derive(Clone, Copy)] |
| 7425 | struct BlkCell { |
| 7426 | skip: bool, |
| 7427 | written: bool, |
| 7428 | org: [u16; 2], |
| 7429 | dim: [u8; 2], |
| 7430 | } |
| 7431 | |
| 7432 | /// The neighbour state a block's contexts are read from — a duplicate of |
| 7433 | /// [`crate::tile`]'s own private `Neighbours` (that module is another lane's |
| 7434 | /// territory this round), kept at the same two granularities the real writer |
| 7435 | /// tracks: `above_mode`/`left_mode`/`above_side`/`left_side` on the 16x16 |
| 7436 | /// (`SUB`) grid the partition/mode symbols read, and `above`/`left`'s |
| 7437 | /// per-plane coded/DC-sign state on the finer 4x4 (`MI`) grid the coefficient |
| 7438 | /// contexts read (spec `av1_set_entropy_contexts`) — which is what lets a |
| 7439 | /// chroma transform straddling the true, odd-sized frame edge see its |
| 7440 | /// neighbour's state clamped at its own subsampled edge rather than luma's. |
| 7441 | #[derive(Clone)] |
| 7442 | struct Neighbours { |
| 7443 | above: Vec<[Neighbour; 3]>, |
| 7444 | left: Vec<[Neighbour; 3]>, |
| 7445 | above_mode: Vec<usize>, |
| 7446 | left_mode: Vec<usize>, |
| 7447 | /// Chroma's own `uv_mode` companion to `above_mode`/`left_mode` (lane-chroma |
| 7448 | /// r3): a smooth/paeth `uv_mode`'s edge-filter strength (spec |
| 7449 | /// `get_intra_edge_filter_type`) reads the CHROMA neighbour's mode, not the |
| 7450 | /// luma one -- the same block can (and often does) code a different |
| 7451 | /// `uv_mode` than its own `mode`. Coarse [`SUB`]-grid like `above_mode`; |
| 7452 | /// [`Self::decode_leaf8`]'s per-leaf `prev_leaf` override (a real |
| 7453 | /// within-block neighbour finer than this grid) is luma-only -- a known, |
| 7454 | /// unexercised-by-gate corner-cut (every real-encoder gate in `stream.rs` |
| 7455 | /// uses `--min-partition-size=16`, so no leaf8 8x8 split ever reaches this). |
| 7456 | above_uv_mode: Vec<usize>, |
| 7457 | left_uv_mode: Vec<usize>, |
| 7458 | above_side: Vec<usize>, |
| 7459 | left_side: Vec<usize>, |
| 7460 | /// The same as `above_side`/`left_side`, but kept at the finer mi (4x4) |
| 7461 | /// granularity `above`/`left` are, rather than [`SUB`] -- [`crate::tile`]'s |
| 7462 | /// `above_side_mi`/`left_side_mi`: two 8x8 leaves of one straddling 16x16 |
| 7463 | /// block (lane-av1-rect) share a single [`SUB`]-grid cell, so a coarse |
| 7464 | /// array cannot tell the second leaf's partition symbol that the first |
| 7465 | /// one was coded at 8x8. |
| 7466 | /// lane-publish2: `u8` (a block side is at most 128 px, and so is |
| 7467 | /// [`NO_NEIGHBOUR_SIDE`]) -- every block stamps these per mi. |
| 7468 | above_side_mi: Vec<u8>, |
| 7469 | left_side_mi: Vec<u8>, |
| 7470 | /// The luma `mode` of the last `BLOCK_4X4` leaf decoded at each mi column |
| 7471 | /// / row, with the mi position it was written at |
| 7472 | /// (`(usize::MAX, _)` = never written). `above_mode`/`left_mode` are on |
| 7473 | /// the coarse 16x16 grid, which cannot distinguish the two 4x4 leaves an |
| 7474 | /// 8x8 `PARTITION_SPLIT` puts in one mi column; libaom's intra-mode |
| 7475 | /// context reads `mi(row, col-1)`/`mi(row-1, col)` exactly, so a sub-8x8 |
| 7476 | /// leaf whose neighbour is another split group needs that group's leaf 3 |
| 7477 | /// (bottom-right), not the leaf the coarse slot happens to hold. Written |
| 7478 | /// and read only by [`decode_leaf_split4`]; the position guard makes it |
| 7479 | /// fall back to the coarse slot whenever the real neighbour was a block |
| 7480 | /// of 8x8 or larger (whose one mode is correct for every row it spans). |
| 7481 | /// lane-publish2: `(mi position, mode)` narrowed from `(usize, usize)` to |
| 7482 | /// `(u16, u8)` -- every block stamps these mi-granular maps, and 16 bytes |
| 7483 | /// per mi per side of publish traffic was pure width. `u16::MAX` is the |
| 7484 | /// "never written" sentinel (an mi index cannot reach it: AV1's largest |
| 7485 | /// frame is 65536 px = 16384 mi). |
| 7486 | sub8_mode_col: Vec<(u16, u8)>, |
| 7487 | sub8_mode_row: Vec<(u16, u8)>, |
| 7488 | /// The `uv_mode` twin of `sub8_mode_col`/`sub8_mode_row` (lane-sub8 r5): |
| 7489 | /// chroma's edge-filter type reads the CHROMA neighbour's `uv_mode` |
| 7490 | /// (`get_intra_edge_filter_type`, reconintra.c:974), and the coarse |
| 7491 | /// [`SUB`] slots cannot name it once a 16x16 is split into 8x8 leaves -- |
| 7492 | /// the leaf above writes the same row slot the block to the left owns. |
| 7493 | uv_mode_col: Vec<(u16, u8)>, |
| 7494 | uv_mode_row: Vec<(u16, u8)>, |
| 7495 | /// Every mi cell's own `uv_mode`, the mi grid libaom's |
| 7496 | /// `chroma_above_mbmi`/`chroma_left_mbmi` read directly (`set_mi_row_col`, |
| 7497 | /// blockd.h). lane-t900 r12: the one-slot-per-column |
| 7498 | /// `uv_mode_col`/`uv_mode_row` maps above hold only the LAST block written |
| 7499 | /// in that column, so a sub-8x8 pair's own first (non-chroma-ref) half |
| 7500 | /// overwrote the row the pair's chroma-ref half then wanted to read -- |
| 7501 | /// the mi-exact match failed and the coarse [`SUB`] band answered instead. |
| 7502 | /// `u8::MAX` = no block has written this cell yet (fall back to the maps). |
| 7503 | uv_mode_grid: Vec<u8>, |
| 7504 | /// [`Self::uv_mode_grid`]'s stride, in mi units (superblock-padded, like |
| 7505 | /// every other band here). |
| 7506 | uv_grid_cols: usize, |
| 7507 | /// Whether the block above/left of this column/row was coded `skip` -- |
| 7508 | /// an inter frame's own `skip` context (spec `SkipContext`), which the |
| 7509 | /// key-frame writer never tracks (its skip context is always zero); a |
| 7510 | /// duplicate of [`crate::tile`]'s private `Neighbours::above_skip`/ |
| 7511 | /// `left_skip`. |
| 7512 | above_skip: Vec<bool>, |
| 7513 | left_skip: Vec<bool>, |
| 7514 | /// libaom's `above_txfm_context`/`left_txfm_context` (`TXFM_CONTEXT`), at |
| 7515 | /// 4x4 mi granularity: the transform *width* (above) / *height* (left) in |
| 7516 | /// pixels last written over each unit by `set_txfm_ctxs`/ |
| 7517 | /// `txfm_partition_update`, which is what an inter block's `txfm_split` |
| 7518 | /// symbol reads its context from (lane-txselect, spec 5.11.17). |
| 7519 | /// [`TXFM_CTX_INIT`] until a block writes one; a fresh full-height `left` |
| 7520 | /// array is exactly libaom's per-superblock-row reset, since each mi row |
| 7521 | /// is only ever visited inside one superblock row. |
| 7522 | above_txfm: Vec<u8>, |
| 7523 | left_txfm: Vec<u8>, |
| 7524 | /// Whether the block above/left of this column/row coded `skip_mode` |
| 7525 | /// (spec 5.11.23) -- `av1_get_skip_mode_context`'s own neighbour lookup, |
| 7526 | /// lane-av1comp round 14. |
| 7527 | above_skip_mode: Vec<bool>, |
| 7528 | left_skip_mode: Vec<bool>, |
| 7529 | /// Whether the block above/left was coded inter -- the `is_inter` |
| 7530 | /// context (spec `av1_get_intra_inter_context`), a duplicate of |
| 7531 | /// [`crate::tile`]'s private `Neighbours::above_inter`/`left_inter`. |
| 7532 | above_inter: Vec<bool>, |
| 7533 | left_inter: Vec<bool>, |
| 7534 | /// The actual reference frame (`1..=7`, spec's `MV_REFERENCE_FRAME` |
| 7535 | /// alphabet) the block above/left of this column/row coded, or `-1` for |
| 7536 | /// an intra neighbour/frame border -- the `single_ref_p1`..`p6` context |
| 7537 | /// functions (`mvstack::ref_ctx` family) need the real reference, not |
| 7538 | /// just "was it inter" (lane-av1refs: every inter block used to be |
| 7539 | /// `LAST_FRAME` by construction, so `above_inter`/`left_inter` alone was |
| 7540 | /// enough). |
| 7541 | above_ref: Vec<i8>, |
| 7542 | left_ref: Vec<i8>, |
| 7543 | /// The block above/left's *second* reference frame when it was coded |
| 7544 | /// compound (spec `RefFrames[1]`), or `None` for single-ref/intra -- |
| 7545 | /// lane-av1comp: `mvstack::reference_mode_ctx`/`comp_reference_type_ctx` |
| 7546 | /// need a full [`crate::mvstack::NeighbourRef`], not just `above_ref`'s |
| 7547 | /// `i8`, once a compound `read_ref_frames` lands. |
| 7548 | /// lane-publish2 audit: the "always `None` today" note this doc used to |
| 7549 | /// carry is long stale -- compound blocks do occur, and |
| 7550 | /// [`Self::record_compound_ctx_rect_mi`] stamps the real second reference |
| 7551 | /// over their span right after [`Self::record_inter_rect_mi`]'s reset. |
| 7552 | /// The array IS read: `single_ref_p*`/`comp_ref` contexts and every |
| 7553 | /// [`crate::mvstack::NeighbourRef`] construction (class |
| 7554 | /// [[branch-dropped-as-unreachable]] -- the note, not the code, was wrong). |
| 7555 | above_ref1: Vec<Option<i8>>, |
| 7556 | left_ref1: Vec<Option<i8>>, |
| 7557 | /// The precomputed `get_comp_group_idx_context`/`get_comp_index_context` |
| 7558 | /// neighbour contribution (spec 5.11.25, libaom `pred_common.h`): |
| 7559 | /// a compound neighbour's own `comp_group_idx`/`compound_idx` bit, or |
| 7560 | /// the "ref_frame[0] == ALTREF_FRAME" special case (`3`/`1`) for a |
| 7561 | /// single-ref one, or `0` for intra/unavailable -- lane-av1comp. |
| 7562 | above_comp_group_idx: Vec<u8>, |
| 7563 | left_comp_group_idx: Vec<u8>, |
| 7564 | above_compound_idx: Vec<u8>, |
| 7565 | left_compound_idx: Vec<u8>, |
| 7566 | /// `interp_filter[0]`/`[1]` (horizontal/vertical kernel index, 0..=2, or |
| 7567 | /// `3` = "no info": an intra neighbour or the frame's own border) of the |
| 7568 | /// block above/left of this column/row -- spec |
| 7569 | /// `av1_get_pred_context_switchable_interp`'s own neighbour lookup. |
| 7570 | above_filter: Vec<[u8; 2]>, |
| 7571 | left_filter: Vec<[u8; 2]>, |
| 7572 | mi_cols: usize, |
| 7573 | mi_rows: usize, |
| 7574 | /// Per-8x8 (2x2 mi cells) `skip_txfm` flag over the whole padded frame, |
| 7575 | /// spec `av1_cdef_compute_sb_list`'s `is_8x8_block_skip`: CDEF (spec |
| 7576 | /// 7.15) never filters an 8x8 luma block whose covering coded block read |
| 7577 | /// `skip` -- one flag per 4x4 mi cell (finer than needed, but matches the |
| 7578 | /// grain [`Self::record_mi`] already writes at), read back at 8x8 |
| 7579 | /// granularity by [`apply_cdef`] since `skip` is constant across every |
| 7580 | /// mi cell one coded block covers. |
| 7581 | /// The four per-mi maps [`Neighbours::blk_grid`] packs, in one cell. |
| 7582 | /// lane-cdefstrip r1 instrument: whether any coded block actually WROTE |
| 7583 | /// the `skip` bit at this mi cell during this frame. A fresh |
| 7584 | /// `Neighbours` seeds `skip_grid` all-`false` ("not skip"), so a decode |
| 7585 | /// arm that forgets [`Self::fill_skip_grid_rect`] silently makes |
| 7586 | /// [`apply_cdef`] filter an 8x8 unit libaom's `is_8x8_block_skip` |
| 7587 | /// (`cdef.c:29-38`) excludes -- exactly the lane-cdef defect, and |
| 7588 | /// unobservable from the pixels alone. Counted by |
| 7589 | /// [`cdef_unwritten_skip_units`]. |
| 7590 | /// Per-4x4-mi origin `(mi_row, mi_col)` of the coded (prediction) block |
| 7591 | /// covering that cell -- spec 7.14.2's `isBlockEdge`/libaom `pu_edge`, |
| 7592 | /// which re-enables the loop filter at a prediction boundary between two |
| 7593 | /// skipped inter blocks (lane-mergefix r4). Written by |
| 7594 | /// [`Self::fill_skip_grid_rect`], whose `at_mi` IS that origin. |
| 7595 | /// Per-4x4-mi `(width, height)` in mi units of the coded block covering |
| 7596 | /// that cell -- the other half of `pu_edge`: libaom tests the edge |
| 7597 | /// coordinate against the block's size in the FILTERED plane's own |
| 7598 | /// pixels, which for chroma is `max(luma_dim / 2, 4)` (lane-t900 r30b). |
| 7599 | blk_grid: Vec<BlkCell>, |
| 7600 | skip_grid_cols_mi: usize, |
| 7601 | /// Per-4x4-mi luma transform width in pixels (8/16/32/64) -- this decoder |
| 7602 | /// never splits a coded block's transform below its own size (round 2: |
| 7603 | /// `TxMode::Select`/`tx_depth` refused), so a block's own side *is* its |
| 7604 | /// transform size, spec 7.14's `get_transform_size`/`get_filter_level` |
| 7605 | /// edge-length lookup (`apply_deblock`). |
| 7606 | tx_grid: Vec<u8>, |
| 7607 | /// The transform HEIGHT companion to [`Self::tx_grid`]'s width -- |
| 7608 | /// lane-rect r2: TX_32X16/TX_16X32 strips deblock their horizontal |
| 7609 | /// edges by tx height (spec 7.14.2 `set_lpf_parameters` dir==VERT). |
| 7610 | tx_h_grid: Vec<u8>, |
| 7611 | /// Per-4x4-mi CHROMA transform width/height in pixels -- lane-tiny r4. |
| 7612 | /// A block's chroma transform is `av1_get_max_uv_txsize(bsize)`: the |
| 7613 | /// largest transform covering the subsampled BLOCK, capped at 32x32. It |
| 7614 | /// does NOT follow luma's `tx_depth` split, so deriving it as |
| 7615 | /// `tx_grid / 2` invented chroma transform edges the deblocker then |
| 7616 | /// filtered (32x16 frames, U plane, seeds 42/47). Written from the |
| 7617 | /// block's own mi span in [`Self::fill_lf_grid_rect`], the one place |
| 7618 | /// every caller routes through. |
| 7619 | uv_tx_grid: Vec<u8>, |
| 7620 | uv_tx_h_grid: Vec<u8>, |
| 7621 | /// Per-4x4-mi reference frame (`0` = intra, magnitude `1..=7` = |
| 7622 | /// `MV_REFERENCE_FRAME`, negative = that reference coded as `GLOBALMV` -- |
| 7623 | /// `lf_level`'s `mode_lf_lut` row 0, lane-av1golden2) |
| 7624 | /// -- the loop filter's ref/mode delta lookup (spec 7.14.4 |
| 7625 | /// `get_filter_level`, `loop_filter_ref_deltas[ref_frame]`; lane-av1refs: |
| 7626 | /// widened from a bare `is_inter` bool, which collapsed every non-`LAST` |
| 7627 | /// reference onto `LAST_FRAME`'s own delta). |
| 7628 | ref_grid: Vec<i8>, |
| 7629 | /// Per-4x4-mi snapshot of [`CURRENT_DELTA_LF`] at the moment this cell's |
| 7630 | /// block was decoded (spec `DeltaLF[i]`, `i` in `0..FRAME_LF_COUNT`, |
| 7631 | /// Y-vertical/Y-horizontal/U/V) -- lane-realworld r5: `lf_level`'s |
| 7632 | /// per-block additive term, mirroring [`Self::ref_grid`]'s own snapshot |
| 7633 | /// pattern. `!delta_lf_multi` mode broadcasts index 0 into all 4 slots |
| 7634 | /// at write time ([`Self::fill_lf_grid_rect`]) so a reader never needs |
| 7635 | /// to know which mode produced the value. |
| 7636 | delta_lf_grid: Vec<[i8; 4]>, |
| 7637 | /// The block above/left's own palette-Y size (0 = not a palette block) |
| 7638 | /// and its base colours -- `av1_get_palette_mode_ctx`/ |
| 7639 | /// `av1_get_palette_cache` (pred_common.c)'s neighbour lookup, lane-palette |
| 7640 | /// r2. UV has no equivalent yet (stage 2, unreconstructed). |
| 7641 | above_palette_size: Vec<u8>, |
| 7642 | left_palette_size: Vec<u8>, |
| 7643 | above_palette_colors: Vec<[u16; 8]>, |
| 7644 | left_palette_colors: Vec<[u16; 8]>, |
| 7645 | /// As the four fields above, for the neighbour's own chroma (U-channel |
| 7646 | /// only -- `av1_get_palette_cache`'s plane-1 lookup, [`Self::palette_uv_cache`]) |
| 7647 | /// palette size/colours (lane-palette2 r1). |
| 7648 | above_palette_uv_size: Vec<u8>, |
| 7649 | left_palette_uv_size: Vec<u8>, |
| 7650 | above_palette_uv_colors: Vec<[u16; 8]>, |
| 7651 | left_palette_uv_colors: Vec<[u16; 8]>, |
| 7652 | /// This tile's own top-left corner, in 4x4 mode-info units (spec |
| 7653 | /// `MiRowStart`/`MiColStart`) -- lane-tiles: every "is there really a |
| 7654 | /// neighbour" check compares against this instead of literal `0`, since a |
| 7655 | /// block at the tile's own top/left edge has no usable neighbour even |
| 7656 | /// though a decoded block sits there in the picture (spec `decode_tile`'s |
| 7657 | /// per-tile `clear_above_context`/`left_available`/`up_available`). |
| 7658 | /// `0` for a single-tile frame, matching every existing caller exactly. |
| 7659 | tile_row0_mi: usize, |
| 7660 | tile_col0_mi: usize, |
| 7661 | } |
| 7662 | |
| 7663 | #[allow(dead_code)] // read only from the `#[cfg(test)]` gates |
| 7664 | impl Neighbours { |
| 7665 | /// `cols`/`rows` are in [`SUB`] units; `mi_cols`/`mi_rows` are the frame's |
| 7666 | /// true (unpadded) size in 4x4 mode-info units. |
| 7667 | fn new(cols: usize, rows: usize, mi_cols: usize, mi_rows: usize, fctx: &crate::decode::FrameCtx) -> Self { |
| 7668 | // lane-sb128c r7: a 128x64 block at the bottom frame edge records its |
| 7669 | // neighbour state over the whole 64-row extent, past the frame's last |
| 7670 | // mi row (mi_rows 198, block rows 192..208). libaom's own context |
| 7671 | // buffers are superblock-aligned for exactly that reason |
| 7672 | // (`av1_alloc_context_buffers` rounds to `MAX_MIB_SIZE`), so pad the |
| 7673 | // side arrays to a superblock multiple and leave those cells unread -- |
| 7674 | // every availability check uses the true `mi_rows`/`mi_cols` below. |
| 7675 | let sb_sub = (sb_mi_cur(fctx) as usize) / (SUB / MI); |
| 7676 | let (cols, rows) = ( |
| 7677 | cols.next_multiple_of(sb_sub), |
| 7678 | rows.next_multiple_of(sb_sub), |
| 7679 | ); |
| 7680 | Self { |
| 7681 | above: vec![[Neighbour::default(); 3]; cols * (SUB / MI)], |
| 7682 | left: vec![[Neighbour::default(); 3]; rows * (SUB / MI)], |
| 7683 | above_mode: vec![DC_PRED; cols], |
| 7684 | left_mode: vec![DC_PRED; rows], |
| 7685 | above_uv_mode: vec![DC_PRED; cols], |
| 7686 | left_uv_mode: vec![DC_PRED; rows], |
| 7687 | above_side: vec![NO_NEIGHBOUR_SIDE; cols], |
| 7688 | left_side: vec![NO_NEIGHBOUR_SIDE; rows], |
| 7689 | above_side_mi: vec![NO_NEIGHBOUR_SIDE as u8; cols * (SUB / MI)], |
| 7690 | left_side_mi: vec![NO_NEIGHBOUR_SIDE as u8; rows * (SUB / MI)], |
| 7691 | sub8_mode_col: vec![(u16::MAX, 0); cols * (SUB / MI)], |
| 7692 | sub8_mode_row: vec![(u16::MAX, 0); rows * (SUB / MI)], |
| 7693 | uv_mode_col: vec![(u16::MAX, 0); cols * (SUB / MI)], |
| 7694 | uv_mode_row: vec![(u16::MAX, 0); rows * (SUB / MI)], |
| 7695 | uv_mode_grid: vec![u8::MAX; cols * (SUB / MI) * rows * (SUB / MI)], |
| 7696 | uv_grid_cols: cols * (SUB / MI), |
| 7697 | above_txfm: vec![TXFM_CTX_INIT; cols * (SUB / MI)], |
| 7698 | left_txfm: vec![TXFM_CTX_INIT; rows * (SUB / MI)], |
| 7699 | // lane-inter8 r2: mi(4px)-granular, not [`SUB`]-granular -- an |
| 7700 | // 8x8 inter leaf's left neighbour is the OTHER leaf row of the |
| 7701 | // 16x16 block beside it, which a SUB-grid slot cannot name |
| 7702 | // (class context-read-from-one-cell). |
| 7703 | above_skip: vec![false; cols * (SUB / MI)], |
| 7704 | left_skip: vec![false; rows * (SUB / MI)], |
| 7705 | above_skip_mode: vec![false; cols * (SUB / MI)], |
| 7706 | left_skip_mode: vec![false; rows * (SUB / MI)], |
| 7707 | above_inter: vec![false; cols * (SUB / MI)], |
| 7708 | left_inter: vec![false; rows * (SUB / MI)], |
| 7709 | above_ref: vec![-1; cols * (SUB / MI)], |
| 7710 | left_ref: vec![-1; rows * (SUB / MI)], |
| 7711 | above_ref1: vec![None; cols * (SUB / MI)], |
| 7712 | left_ref1: vec![None; rows * (SUB / MI)], |
| 7713 | above_comp_group_idx: vec![0; cols * (SUB / MI)], |
| 7714 | left_comp_group_idx: vec![0; rows * (SUB / MI)], |
| 7715 | above_compound_idx: vec![0; cols * (SUB / MI)], |
| 7716 | left_compound_idx: vec![0; rows * (SUB / MI)], |
| 7717 | above_filter: vec![[3u8; 2]; cols * (SUB / MI)], |
| 7718 | left_filter: vec![[3u8; 2]; rows * (SUB / MI)], |
| 7719 | mi_cols, |
| 7720 | mi_rows, |
| 7721 | blk_grid: vec![ |
| 7722 | BlkCell { skip: false, written: false, org: [0; 2], dim: [1; 2] }; |
| 7723 | cols * (SUB / MI) * rows * (SUB / MI) |
| 7724 | ], |
| 7725 | skip_grid_cols_mi: cols * (SUB / MI), |
| 7726 | tx_grid: vec![0u8; cols * (SUB / MI) * rows * (SUB / MI)], |
| 7727 | tx_h_grid: vec![0u8; cols * (SUB / MI) * rows * (SUB / MI)], |
| 7728 | uv_tx_grid: vec![0u8; cols * (SUB / MI) * rows * (SUB / MI)], |
| 7729 | uv_tx_h_grid: vec![0u8; cols * (SUB / MI) * rows * (SUB / MI)], |
| 7730 | ref_grid: vec![0i8; cols * (SUB / MI) * rows * (SUB / MI)], |
| 7731 | delta_lf_grid: vec![[0i8; 4]; cols * (SUB / MI) * rows * (SUB / MI)], |
| 7732 | above_palette_size: vec![0; cols * (SUB / MI)], |
| 7733 | left_palette_size: vec![0; rows * (SUB / MI)], |
| 7734 | above_palette_colors: vec![[0u16; 8]; cols * (SUB / MI)], |
| 7735 | left_palette_colors: vec![[0u16; 8]; rows * (SUB / MI)], |
| 7736 | above_palette_uv_size: vec![0; cols * (SUB / MI)], |
| 7737 | left_palette_uv_size: vec![0; rows * (SUB / MI)], |
| 7738 | above_palette_uv_colors: vec![[0u16; 8]; cols * (SUB / MI)], |
| 7739 | left_palette_uv_colors: vec![[0u16; 8]; rows * (SUB / MI)], |
| 7740 | tile_row0_mi: 0, |
| 7741 | tile_col0_mi: 0, |
| 7742 | } |
| 7743 | } |
| 7744 | |
| 7745 | /// spec `decode_tile`'s per-tile reset: `clear_above_context` over this |
| 7746 | /// tile's own column span (`col1_mi` exclusive), plus recording |
| 7747 | /// `(row0_mi, col0_mi)` as the tile's own top-left mi cell for every |
| 7748 | /// availability check below. Left context resets every superblock row |
| 7749 | /// regardless of tile ([`Self::start_row`], already called once per SB |
| 7750 | /// row by every caller) so it needs no tile-specific reset here. |
| 7751 | fn start_tile(&mut self, row0_mi: usize, col0_mi: usize, col1_mi: usize, fctx: &crate::decode::FrameCtx) { |
| 7752 | // lane-seg: `AvailU`/`AvailL` for the segment-id neighbour prediction |
| 7753 | // are tile-relative, same as every other neighbour lookup here. |
| 7754 | fctx.seg_tile_origin.with(|c| c.set((row0_mi, col0_mi))); |
| 7755 | self.tile_row0_mi = row0_mi; |
| 7756 | self.tile_col0_mi = col0_mi; |
| 7757 | let end = col1_mi.min(self.above.len()); |
| 7758 | for i in col0_mi.min(end)..end { |
| 7759 | self.above[i] = Default::default(); |
| 7760 | self.above_side_mi[i] = NO_NEIGHBOUR_SIDE as u8; |
| 7761 | } |
| 7762 | let (sc0, sc1) = ( |
| 7763 | col0_mi / (SUB / MI), |
| 7764 | (col1_mi / (SUB / MI)).min(self.above_mode.len()), |
| 7765 | ); |
| 7766 | for i in sc0.min(sc1)..sc1 { |
| 7767 | self.above_mode[i] = DC_PRED; |
| 7768 | self.above_side[i] = NO_NEIGHBOUR_SIDE; |
| 7769 | } |
| 7770 | // lane-inter8 r2: the inter bands are mi-granular, so they reset over |
| 7771 | // the tile's own mi column span directly. |
| 7772 | let mend = col1_mi.min(self.above_skip.len()); |
| 7773 | for i in col0_mi.min(mend)..mend { |
| 7774 | self.above_skip[i] = false; |
| 7775 | self.above_skip_mode[i] = false; |
| 7776 | self.above_inter[i] = false; |
| 7777 | self.above_ref[i] = -1; |
| 7778 | self.above_ref1[i] = None; |
| 7779 | self.above_comp_group_idx[i] = 0; |
| 7780 | self.above_compound_idx[i] = 0; |
| 7781 | self.above_filter[i] = [3u8; 2]; |
| 7782 | } |
| 7783 | // lane-sub8 r6: the mi-granular column maps are per tile too -- a |
| 7784 | // stale `(row, mode)` from the tile to the LEFT names an mi row this |
| 7785 | // tile's first block would read as its own above neighbour. |
| 7786 | let mcol_end = col1_mi.min(self.sub8_mode_col.len()); |
| 7787 | for i in col0_mi.min(mcol_end)..mcol_end { |
| 7788 | self.sub8_mode_col[i] = (u16::MAX, 0); |
| 7789 | self.uv_mode_col[i] = (u16::MAX, 0); |
| 7790 | } |
| 7791 | // lane-mergefix r2: `av1_zero_above_context` also memsets the txfm row |
| 7792 | // over the tile's own column span (to `tx_size_wide[TX_SIZES_LARGEST]`, |
| 7793 | // not zero) -- without it a second tile column reads the first one's |
| 7794 | // resolved transform sizes as its own above context. |
| 7795 | let tend = col1_mi.min(self.above_txfm.len()); |
| 7796 | for i in col0_mi.min(tend)..tend { |
| 7797 | self.above_txfm[i] = TXFM_CTX_INIT; |
| 7798 | } |
| 7799 | } |
| 7800 | |
| 7801 | /// Writes a just-decoded block's own palette-Y size/colours (`size == 0` |
| 7802 | /// for a non-palette block, clearing any stale neighbour state) into every |
| 7803 | /// [`SUB`]-grid cell it covers on both the above and left edges -- |
| 7804 | /// mirrors [`Self::record_rect`]'s loop shape, square-only (this lane's |
| 7805 | /// call sites are all square blocks). |
| 7806 | fn record_palette_y(&mut self, at: (usize, usize), side: usize, size: usize, colors: [u16; 8]) { |
| 7807 | self.record_palette_y_rect((at.0 * (SUB / MI), at.1 * (SUB / MI)), side, side, size, colors); |
| 7808 | } |
| 7809 | |
| 7810 | /// [`Self::record_palette_y`] with independent above (`w`)/left (`h`) |
| 7811 | /// extents (lane-palette2 r4, mirrors [`Self::record_rect`] vs |
| 7812 | /// [`Self::record`]): a true rect strip's above/left neighbour spans |
| 7813 | /// differ. |
| 7814 | fn record_palette_y_rect( |
| 7815 | &mut self, |
| 7816 | mi: (usize, usize), |
| 7817 | w: usize, |
| 7818 | h: usize, |
| 7819 | size: usize, |
| 7820 | colors: [u16; 8], |
| 7821 | ) { |
| 7822 | let (r, c) = mi; |
| 7823 | if crate::envflags::env_flag!("EC_PALPUB") { |
| 7824 | eprintln!("EC_PALPUB yrect mi={r},{c} bw={w} bh={h} y={size}"); |
| 7825 | } |
| 7826 | // lane-kf900 r6: mi (4px) granularity, libaom's own -- `palette_size` |
| 7827 | // lives per mi cell in `MB_MODE_INFO`, and `xd->left_mbmi` is the mi |
| 7828 | // cell immediately left of the block, not its 16px [`SUB`] cell. The |
| 7829 | // old SUB grid let a 16x8 strip pair share one slot, so the second |
| 7830 | // strip of mi(0,8) read the FIRST strip's palette size and the colour |
| 7831 | // cache came out one entry short (class [[context-read-from-one-cell]]). |
| 7832 | for cell in 0..(w / MI).max(1) { |
| 7833 | if let Some(e) = self.above_palette_size.get_mut(c + cell) { |
| 7834 | *e = size as u8; |
| 7835 | // lane-publish2: spec 5.11.46 `get_palette_cache` reads a |
| 7836 | // neighbour's colours only where its palette size > 0, so a |
| 7837 | // non-palette block need not stamp 16 bytes of zeroes here. |
| 7838 | if size > 0 { |
| 7839 | self.above_palette_colors[c + cell] = colors; |
| 7840 | } |
| 7841 | } |
| 7842 | } |
| 7843 | for cell in 0..(h / MI).max(1) { |
| 7844 | if let Some(e) = self.left_palette_size.get_mut(r + cell) { |
| 7845 | *e = size as u8; |
| 7846 | if size > 0 { |
| 7847 | self.left_palette_colors[r + cell] = colors; |
| 7848 | } |
| 7849 | } |
| 7850 | } |
| 7851 | } |
| 7852 | |
| 7853 | /// `av1_get_palette_mode_ctx` (pred_common.h:197): count of the above/left |
| 7854 | /// neighbours that are themselves a palette-Y block, plus |
| 7855 | /// `av1_get_palette_cache`'s own merged, ascending, deduplicated colour |
| 7856 | /// cache (pred_common.c:73). |
| 7857 | /// |
| 7858 | /// The SB-top-row exclusion of the above neighbour (`row % 64` in |
| 7859 | /// pred_common.c:76, `r % 4 == 0` here in [`SUB`]-grid units) belongs to |
| 7860 | /// the CACHE ALONE -- `av1_get_palette_mode_ctx` reads plain |
| 7861 | /// `xd->above_mbmi` with no such guard (lane-palette2 r7). Applying it to |
| 7862 | /// the ctx too made every superblock in the frame's second SB row read |
| 7863 | /// `palette_y_mode` off CDF row 0 while libaom used row 1, so a real |
| 7864 | /// palette block decoded as "no palette" and the tile desynced from |
| 7865 | /// there (class [[cdf-row-held-constant]]). |
| 7866 | fn palette_ctx_and_cache(&self, at: (usize, usize)) -> (usize, Vec<u16>) { |
| 7867 | self.palette_ctx_and_cache_mi((at.0 * (SUB / MI), at.1 * (SUB / MI))) |
| 7868 | } |
| 7869 | |
| 7870 | /// [`Self::palette_ctx_and_cache`] with the block's REAL mi row, which a |
| 7871 | /// 1:4 strip does not share with its [`SUB`] cell: `av1_get_palette_cache` |
| 7872 | /// drops the above neighbour on a 64-px superblock boundary |
| 7873 | /// (pred_common.c:76, `row % (1 << MIN_SB_SIZE_LOG2)` with `row` the |
| 7874 | /// block's own PIXEL row), and strip 1 of a HORZ_4 sits 8 px into the |
| 7875 | /// cell -- reading `at.0 % 4` there excludes strip 0 from the cache of |
| 7876 | /// every strip in an SB's top cell row, which libaom does not do. |
| 7877 | fn palette_ctx_and_cache_mi(&self, mi: (usize, usize)) -> (usize, Vec<u16>) { |
| 7878 | let (r, c) = mi; |
| 7879 | let above_ok = r % 16 != 0; |
| 7880 | // `xd->above_mbmi` exists for every row but the tile's own first. |
| 7881 | let above_is_palette = r > self.tile_row0_mi && self.above_palette_size[c] > 0; |
| 7882 | let (above_n, above_colors) = if above_ok && self.above_palette_size[c] > 0 { |
| 7883 | (usize::from(self.above_palette_size[c]), self.above_palette_colors[c]) |
| 7884 | } else { |
| 7885 | (0, [0u16; 8]) |
| 7886 | }; |
| 7887 | // lane-tiles r11: `av1_get_palette_cache` takes `xd->left_mbmi`, which |
| 7888 | // is NULL whenever the block sits at its TILE's own left column |
| 7889 | // (`left_available`), not the frame's -- without this guard a second |
| 7890 | // tile column reads the previous tile's palette colours into its cache |
| 7891 | // and its `palette_y_mode` ctx. |
| 7892 | let left_ok = c > self.tile_col0_mi; |
| 7893 | let (left_n, left_colors) = if left_ok && self.left_palette_size[r] > 0 { |
| 7894 | (usize::from(self.left_palette_size[r]), self.left_palette_colors[r]) |
| 7895 | } else { |
| 7896 | (0, [0u16; 8]) |
| 7897 | }; |
| 7898 | let ctx = usize::from(above_is_palette) + usize::from(left_n > 0); |
| 7899 | let mut cache = Vec::with_capacity(16); |
| 7900 | let push_dedup = |cache: &mut Vec<u16>, v: u16| { |
| 7901 | if cache.last() != Some(&v) { |
| 7902 | cache.push(v); |
| 7903 | } |
| 7904 | }; |
| 7905 | let (mut ai, mut li) = (0usize, 0usize); |
| 7906 | while ai < above_n && li < left_n { |
| 7907 | let (va, vl) = (above_colors[ai], left_colors[li]); |
| 7908 | if vl < va { |
| 7909 | push_dedup(&mut cache, vl); |
| 7910 | li += 1; |
| 7911 | } else { |
| 7912 | push_dedup(&mut cache, va); |
| 7913 | ai += 1; |
| 7914 | if vl == va { |
| 7915 | li += 1; |
| 7916 | } |
| 7917 | } |
| 7918 | } |
| 7919 | while ai < above_n { |
| 7920 | push_dedup(&mut cache, above_colors[ai]); |
| 7921 | ai += 1; |
| 7922 | } |
| 7923 | while li < left_n { |
| 7924 | push_dedup(&mut cache, left_colors[li]); |
| 7925 | li += 1; |
| 7926 | } |
| 7927 | (ctx, cache) |
| 7928 | } |
| 7929 | |
| 7930 | /// As [`Self::record_palette_y`], for a just-decoded block's own chroma |
| 7931 | /// (U-channel) palette size/colours -- `size == 0` clears stale state the |
| 7932 | /// same way (lane-palette2 r1). |
| 7933 | fn record_palette_uv(&mut self, at: (usize, usize), side: usize, size: usize, colors: [u16; 8]) { |
| 7934 | self.record_palette_uv_rect( |
| 7935 | (at.0 * (SUB / MI), at.1 * (SUB / MI)), |
| 7936 | side, |
| 7937 | side, |
| 7938 | size, |
| 7939 | colors, |
| 7940 | ); |
| 7941 | } |
| 7942 | |
| 7943 | /// [`Self::record_palette_uv`] with independent above (`w`)/left (`h`) |
| 7944 | /// extents, in LUMA pixels (lane-palette2 r4, same convention as |
| 7945 | /// [`Self::record_palette_y_rect`] -- the above/left grids are indexed at |
| 7946 | /// luma resolution regardless of which plane the recorded state is for). |
| 7947 | fn record_palette_uv_rect( |
| 7948 | &mut self, |
| 7949 | mi: (usize, usize), |
| 7950 | w: usize, |
| 7951 | h: usize, |
| 7952 | size: usize, |
| 7953 | colors: [u16; 8], |
| 7954 | ) { |
| 7955 | let (r, c) = mi; |
| 7956 | // mi granularity, see [`Self::record_palette_y_rect`]. |
| 7957 | for cell in 0..(w / MI).max(1) { |
| 7958 | if let Some(e) = self.above_palette_uv_size.get_mut(c + cell) { |
| 7959 | *e = size as u8; |
| 7960 | if size > 0 { |
| 7961 | self.above_palette_uv_colors[c + cell] = colors; |
| 7962 | } |
| 7963 | } |
| 7964 | } |
| 7965 | for cell in 0..(h / MI).max(1) { |
| 7966 | if let Some(e) = self.left_palette_uv_size.get_mut(r + cell) { |
| 7967 | *e = size as u8; |
| 7968 | if size > 0 { |
| 7969 | self.left_palette_uv_colors[r + cell] = colors; |
| 7970 | } |
| 7971 | } |
| 7972 | } |
| 7973 | } |
| 7974 | |
| 7975 | /// [`Self::palette_ctx_and_cache`]'s cache half, for the U channel |
| 7976 | /// (`av1_get_palette_cache(xd, 1, cache)`) -- `palette_uv_mode_ctx` needs |
| 7977 | /// no neighbour lookup at all (it is this block's own just-decided Y |
| 7978 | /// palette use, `read_palette_mode_info`'s `pmi->palette_size[0] > 0`), |
| 7979 | /// so only the cache is wanted here. |
| 7980 | fn palette_uv_cache(&self, at: (usize, usize)) -> Vec<u16> { |
| 7981 | self.palette_uv_cache_mi((at.0 * (SUB / MI), at.1 * (SUB / MI))) |
| 7982 | } |
| 7983 | |
| 7984 | /// [`Self::palette_uv_cache`] with the block's real mi row -- see |
| 7985 | /// [`Self::palette_ctx_and_cache_mi`]. |
| 7986 | fn palette_uv_cache_mi(&self, mi: (usize, usize)) -> Vec<u16> { |
| 7987 | let (r, c) = mi; |
| 7988 | let above_ok = r % 16 != 0; |
| 7989 | let (above_n, above_colors) = if above_ok && self.above_palette_uv_size[c] > 0 { |
| 7990 | (usize::from(self.above_palette_uv_size[c]), self.above_palette_uv_colors[c]) |
| 7991 | } else { |
| 7992 | (0, [0u16; 8]) |
| 7993 | }; |
| 7994 | // Same tile-relative left availability as the luma cache above. |
| 7995 | let left_ok = c > self.tile_col0_mi; |
| 7996 | let (left_n, left_colors) = if left_ok && self.left_palette_uv_size[r] > 0 { |
| 7997 | (usize::from(self.left_palette_uv_size[r]), self.left_palette_uv_colors[r]) |
| 7998 | } else { |
| 7999 | (0, [0u16; 8]) |
| 8000 | }; |
| 8001 | let mut cache = Vec::with_capacity(16); |
| 8002 | let push_dedup = |cache: &mut Vec<u16>, v: u16| { |
| 8003 | if cache.last() != Some(&v) { |
| 8004 | cache.push(v); |
| 8005 | } |
| 8006 | }; |
| 8007 | let (mut ai, mut li) = (0usize, 0usize); |
| 8008 | while ai < above_n && li < left_n { |
| 8009 | let (va, vl) = (above_colors[ai], left_colors[li]); |
| 8010 | if vl < va { |
| 8011 | push_dedup(&mut cache, vl); |
| 8012 | li += 1; |
| 8013 | } else { |
| 8014 | push_dedup(&mut cache, va); |
| 8015 | ai += 1; |
| 8016 | if vl == va { |
| 8017 | li += 1; |
| 8018 | } |
| 8019 | } |
| 8020 | } |
| 8021 | while ai < above_n { |
| 8022 | push_dedup(&mut cache, above_colors[ai]); |
| 8023 | ai += 1; |
| 8024 | } |
| 8025 | while li < left_n { |
| 8026 | push_dedup(&mut cache, left_colors[li]); |
| 8027 | li += 1; |
| 8028 | } |
| 8029 | cache |
| 8030 | } |
| 8031 | |
| 8032 | /// Marks every 4x4 mi cell a just-decoded coded block covers with its own |
| 8033 | /// transform width (in pixels) and `is_inter` flag -- the loop filter's |
| 8034 | /// per-edge lookup (`apply_deblock`), filled at the same call sites and |
| 8035 | /// units as [`Self::fill_skip_grid`]. |
| 8036 | fn fill_lf_grid(&mut self, at_mi: (usize, usize), side_mi: usize, tx_px: u8, ref_frame: i8, fctx: &crate::decode::FrameCtx) { |
| 8037 | self.fill_lf_grid_rect(at_mi, side_mi, side_mi, tx_px, tx_px, ref_frame, fctx); |
| 8038 | } |
| 8039 | |
| 8040 | /// [`Self::fill_lf_grid`] with independent row/col extents -- lane-partitions |
| 8041 | /// r1: a true rectangular strip's mi span isn't `side_mi` square (`w_mi` |
| 8042 | /// != `h_mi`); `tx_px` still names a single scalar (this decoder's |
| 8043 | /// existing corner-cut, `Neighbours::tx_grid`'s own doc comment -- a |
| 8044 | /// rectangular strip's transform is `TX_32X16`/`TX_16X32`, not tracked |
| 8045 | /// per-axis, matching lane-warp r5's `HORZ_B` top-strip precedent). |
| 8046 | /// libaom `set_one_param_for_line_luma`/`_chroma`: an edge that is a |
| 8047 | /// transform-unit edge but NOT a prediction-unit edge is not filtered when |
| 8048 | /// `curr_skipped = mbmi->skip_txfm && is_inter_block(mbmi)`. Widening this |
| 8049 | /// block's published transform extent to its own size is exactly that |
| 8050 | /// suppression for the deblocker's `x0 % tx != 0` test -- the only shape |
| 8051 | /// that needs it is the 128 root, whose chroma extent (64) is the one |
| 8052 | /// [`Self::fill_lf_grid_rect`]'s TX_32X32 cap narrows. An INTRA block is |
| 8053 | /// never suppressed (libaom's `is_inter_block` term), so this is called |
| 8054 | /// only from the skipped-inter branch. |
| 8055 | fn suppress_internal_lf_edges(&mut self, at_mi: (usize, usize), w_mi: usize, h_mi: usize) { |
| 8056 | let (mi_r, mi_c) = at_mi; |
| 8057 | let (uv_w, uv_h) = ((w_mi * MI / 2).max(4) as u8, (h_mi * MI / 2).max(4) as u8); |
| 8058 | for rr in 0..h_mi { |
| 8059 | let start = (mi_r + rr) * self.skip_grid_cols_mi + mi_c; |
| 8060 | fill_span(&mut self.uv_tx_grid, start, w_mi, uv_w); |
| 8061 | fill_span(&mut self.uv_tx_h_grid, start, w_mi, uv_h); |
| 8062 | } |
| 8063 | } |
| 8064 | |
| 8065 | fn fill_lf_grid_rect( |
| 8066 | &mut self, |
| 8067 | at_mi: (usize, usize), |
| 8068 | w_mi: usize, |
| 8069 | h_mi: usize, |
| 8070 | tx_px: u8, |
| 8071 | tx_h_px: u8, |
| 8072 | ref_frame: i8, fctx: &crate::decode::FrameCtx, |
| 8073 | ) { |
| 8074 | let (mi_r, mi_c) = at_mi; |
| 8075 | // `av1_get_max_uv_txsize` under 4:2:0: the block's chroma extent is |
| 8076 | // half its luma one (min 4 px, the smallest transform), capped at |
| 8077 | // TX_32X32 -- independent of this block's luma `tx_depth`. |
| 8078 | let uv_tx_w = (((w_mi * MI) >> ss_x(fctx)).max(4).min(32)) as u8; |
| 8079 | let uv_tx_h = (((h_mi * MI) >> ss_y(fctx)).max(4).min(32)) as u8; |
| 8080 | if crate::envflags::env_flag!("EC_TXGRID_TRACE") { |
| 8081 | eprintln!("EC_LFGRID mi_row={mi_r} mi_col={mi_c} w_mi={w_mi} h_mi={h_mi} tx_px={tx_px} tx_h_px={tx_h_px}"); |
| 8082 | } |
| 8083 | crate::census::tx(tx_px, tx_h_px); |
| 8084 | let cur = fctx.current_delta_lf.with(|c| c.get()); |
| 8085 | let snapshot: [i8; 4] = if fctx.delta_lf_multi.with(|c| c.get()) { |
| 8086 | std::array::from_fn(|i| cur[i].clamp(-63, 63) as i8) |
| 8087 | } else { |
| 8088 | [cur[0].clamp(-63, 63) as i8; 4] |
| 8089 | }; |
| 8090 | for rr in 0..h_mi { |
| 8091 | let start = (mi_r + rr) * self.skip_grid_cols_mi + mi_c; |
| 8092 | fill_span(&mut self.tx_grid, start, w_mi, tx_px); |
| 8093 | fill_span(&mut self.tx_h_grid, start, w_mi, tx_h_px); |
| 8094 | fill_span(&mut self.uv_tx_grid, start, w_mi, uv_tx_w); |
| 8095 | fill_span(&mut self.uv_tx_h_grid, start, w_mi, uv_tx_h); |
| 8096 | fill_span(&mut self.ref_grid, start, w_mi, ref_frame); |
| 8097 | fill_span(&mut self.delta_lf_grid, start, w_mi, snapshot); |
| 8098 | } |
| 8099 | if fctx.intra_only.with(std::cell::Cell::get) { |
| 8100 | // libaom's `parse_decode_block` runs `set_txfm_ctxs(mbmi->tx_size, |
| 8101 | // xd->width, xd->height, mbmi->skip_txfm && is_inter_block(mbmi), |
| 8102 | // xd)` for EVERY block, an intra-only frame's included, and |
| 8103 | // `get_tx_size_context` / `txfm_partition_context` read those two |
| 8104 | // `TXFM_CONTEXT` bands. This grid carries the same transform size |
| 8105 | // over the same footprint, so the publish is this write: the |
| 8106 | // `skip && is_inter` term is 0 for every intra-only block that is |
| 8107 | // not an intrabc one, and the bodies that can code an intrabc |
| 8108 | // block publish their own (the term is then the BLOCK size, a value |
| 8109 | // this grid cannot express -- see `decode_block`/`decode_leaf8`). |
| 8110 | // Inter frames publish at their own sites and are excluded here |
| 8111 | // because their skipped-block term needs the block size too. |
| 8112 | txfm_partition_update_rect( |
| 8113 | self, |
| 8114 | at_mi, |
| 8115 | (tx_px as usize, tx_h_px as usize), |
| 8116 | (w_mi * MI, h_mi * MI), |
| 8117 | ); |
| 8118 | // libaom `is_inter_block` counts an INTRABC block (`blockd.h:373`), |
| 8119 | // so `get_tx_size_context` reads an intrabc NEIGHBOUR's block size |
| 8120 | // instead of its transform band. Every block stamps its own cells |
| 8121 | // here; the intrabc bodies re-stamp theirs `true` afterwards |
| 8122 | // (`record_intrabc_inter_bands`). |
| 8123 | for i in 0..w_mi { |
| 8124 | if let Some(cell) = self.above_inter.get_mut(mi_c + i) { |
| 8125 | *cell = false; |
| 8126 | } |
| 8127 | } |
| 8128 | for i in 0..h_mi { |
| 8129 | if let Some(cell) = self.left_inter.get_mut(mi_r + i) { |
| 8130 | *cell = false; |
| 8131 | } |
| 8132 | } |
| 8133 | } |
| 8134 | // lane-av1-intrabc r4: publish this coded block's own mi footprint |
| 8135 | // into the intrabc MV grid's block-COVERAGE map -- every coded block, |
| 8136 | // not just the intrabc ones. A later intrabc block's DV predictor is |
| 8137 | // built by libaom's own row/col scans over `xd->mi[]`, which holds an |
| 8138 | // `MB_MODE_INFO` for EVERY block and steps by |
| 8139 | // `mi_size_wide/height[candidate->bsize]`; without this footprint a |
| 8140 | // non-intrabc neighbour reads as uncovered and the scan advances it |
| 8141 | // by one cell instead of by its real width. |
| 8142 | fctx.intrabc_mi_grid.with(|g| { |
| 8143 | if let Some((grid, _, _)) = g.borrow_mut().as_mut() { |
| 8144 | grid.cover_rect(mi_r, mi_c, w_mi, h_mi); |
| 8145 | } |
| 8146 | }); |
| 8147 | } |
| 8148 | |
| 8149 | /// One var-tx / split-tx LEAF's own LUMA transform dims over its mi span. |
| 8150 | /// libaom `get_transform_size` (av1_loopfilter.c:207) applies the split |
| 8151 | /// override only `if ((plane == AOM_PLANE_Y) && ...)` -- chroma keeps |
| 8152 | /// `av1_get_max_uv_txsize(mbmi->bsize)`, the whole BLOCK's uv transform. |
| 8153 | /// Routing a leaf through [`Self::fill_lf_grid_rect`] recomputed the uv |
| 8154 | /// dims from the LEAF span and so invented chroma transform edges the |
| 8155 | /// deblocker then filtered (lane-golomb r6). `ref_grid`/`delta_lf_grid` |
| 8156 | /// are already correct from the block's own fill over the same span. |
| 8157 | fn fill_lf_grid_leaf_luma( |
| 8158 | &mut self, |
| 8159 | at_mi: (usize, usize), |
| 8160 | w_mi: usize, |
| 8161 | h_mi: usize, |
| 8162 | tx_px: u8, |
| 8163 | tx_h_px: u8, |
| 8164 | ) { |
| 8165 | let (mi_r, mi_c) = at_mi; |
| 8166 | if crate::envflags::env_flag!("EC_TXGRID_TRACE") { |
| 8167 | eprintln!("EC_LFLEAF mi_row={mi_r} mi_col={mi_c} w_mi={w_mi} h_mi={h_mi} tx_px={tx_px} tx_h_px={tx_h_px}"); |
| 8168 | } |
| 8169 | for rr in 0..h_mi { |
| 8170 | let start = (mi_r + rr) * self.skip_grid_cols_mi + mi_c; |
| 8171 | fill_span(&mut self.tx_grid, start, w_mi, tx_px); |
| 8172 | fill_span(&mut self.tx_h_grid, start, w_mi, tx_h_px); |
| 8173 | } |
| 8174 | } |
| 8175 | |
| 8176 | /// Marks every 4x4 mi cell a just-decoded coded block covers with its |
| 8177 | /// `skip` flag -- `at_mi`/`side_mi` are the block's own position and |
| 8178 | /// width/height in 4x4 mode-info units (mirroring [`Self::record_mi`]'s |
| 8179 | /// own units, not [`Self::record`]'s [`SUB`]-grid ones). |
| 8180 | fn fill_skip_grid(&mut self, at_mi: (usize, usize), side_mi: usize, skip: bool) { |
| 8181 | self.fill_skip_grid_rect(at_mi, side_mi, side_mi, skip); |
| 8182 | } |
| 8183 | |
| 8184 | /// [`Self::fill_skip_grid`] with independent row/col extents (lane-partitions r1). |
| 8185 | fn fill_skip_grid_rect(&mut self, at_mi: (usize, usize), w_mi: usize, h_mi: usize, skip: bool) { |
| 8186 | let (mi_r, mi_c) = at_mi; |
| 8187 | if skip && w_mi != h_mi { |
| 8188 | hit!(RECT_SKIP_BAND_HITS); |
| 8189 | } |
| 8190 | // lane-census: an INTRA frame's blocks never reach the inter |
| 8191 | // publisher, and this one is what every coded block writes. |
| 8192 | crate::census::block_intra_frame(w_mi, h_mi, skip); |
| 8193 | for rr in 0..h_mi { |
| 8194 | let start = (mi_r + rr) * self.skip_grid_cols_mi + mi_c; |
| 8195 | fill_span( |
| 8196 | &mut self.blk_grid, |
| 8197 | start, |
| 8198 | w_mi, |
| 8199 | BlkCell { |
| 8200 | skip, |
| 8201 | written: true, |
| 8202 | org: [mi_r as u16, mi_c as u16], |
| 8203 | dim: [w_mi as u8, h_mi as u8], |
| 8204 | }, |
| 8205 | ); |
| 8206 | } |
| 8207 | } |
| 8208 | |
| 8209 | /// Whether the 8x8 luma block at mi position `(mi_r, mi_c)` (2x2 mi |
| 8210 | /// cells) was `skip_txfm` -- [`apply_cdef`]'s dlist membership test. |
| 8211 | /// Spec/libaom `is_8x8_block_skip` requires *every* one of the four 4x4 |
| 8212 | /// mi cells the 8x8 covers to be skip (a smaller-than-8x8 partition can |
| 8213 | /// straddle the region with a mix of skip/non-skip blocks). |
| 8214 | /// A single 4x4 mi cell's own `skip` flag, unaggregated (unlike |
| 8215 | /// [`Self::is_skip_txfm`]'s 8x8 all-four-cells rule) -- what the loop |
| 8216 | /// filter's `curr_skipped`/`pv_skip_txfm` (spec 7.14.2) want, since a |
| 8217 | /// filtered edge's two sides are each exactly one coded block wide. |
| 8218 | fn skip_at(&self, mi_r: usize, mi_c: usize) -> bool { |
| 8219 | self.blk_grid |
| 8220 | .get(mi_r * self.skip_grid_cols_mi + mi_c) |
| 8221 | .is_some_and(|c| c.skip) |
| 8222 | } |
| 8223 | |
| 8224 | /// The `skip_txfm` context of a block whose top-left mi cell is |
| 8225 | /// `(mi_r, mi_c)`: libaom `av1_get_skip_txfm_context` |
| 8226 | /// (`pred_common.h:175-181`) is `above_mi->skip_txfm + left_mi->skip_txfm`, |
| 8227 | /// with an absent neighbour contributing 0. The mi grid is per tile, so |
| 8228 | /// row/column 0 is a tile edge and has no neighbour on that axis. |
| 8229 | fn skip_txfm_ctx(&self, mi_r: usize, mi_c: usize) -> usize { |
| 8230 | usize::from(mi_r > self.tile_row0_mi && self.skip_at(mi_r - 1, mi_c)) |
| 8231 | + usize::from(mi_c > self.tile_col0_mi && self.skip_at(mi_r, mi_c - 1)) |
| 8232 | } |
| 8233 | |
| 8234 | fn is_skip_txfm(&self, mi_r: usize, mi_c: usize) -> bool { |
| 8235 | (0..2).all(|rr| { |
| 8236 | (0..2).all(|cc| { |
| 8237 | self.blk_grid |
| 8238 | .get((mi_r + rr) * self.skip_grid_cols_mi + (mi_c + cc)) |
| 8239 | .is_some_and(|c| c.skip) |
| 8240 | }) |
| 8241 | }) |
| 8242 | } |
| 8243 | |
| 8244 | fn start_row(&mut self) { |
| 8245 | self.left.iter_mut().for_each(|l| *l = Default::default()); |
| 8246 | self.left_mode.iter_mut().for_each(|m| *m = DC_PRED); |
| 8247 | self.left_uv_mode.iter_mut().for_each(|m| *m = DC_PRED); |
| 8248 | self.left_side.iter_mut().for_each(|s| *s = NO_NEIGHBOUR_SIDE); |
| 8249 | self.left_side_mi.iter_mut().for_each(|s| *s = NO_NEIGHBOUR_SIDE as u8); |
| 8250 | self.left_skip.iter_mut().for_each(|s| *s = false); |
| 8251 | self.left_skip_mode.iter_mut().for_each(|s| *s = false); |
| 8252 | self.left_inter.iter_mut().for_each(|i| *i = false); |
| 8253 | self.left_ref.iter_mut().for_each(|r| *r = -1); |
| 8254 | self.left_ref1.iter_mut().for_each(|r| *r = None); |
| 8255 | self.left_comp_group_idx.iter_mut().for_each(|r| *r = 0); |
| 8256 | self.left_compound_idx.iter_mut().for_each(|r| *r = 0); |
| 8257 | self.left_filter.iter_mut().for_each(|f| *f = [3u8; 2]); |
| 8258 | // lane-sub8 r6: mi-granular row maps reset with the rest of the left |
| 8259 | // context (a tile/SB-row boundary has no left neighbour). |
| 8260 | self.sub8_mode_row.iter_mut().for_each(|s| *s = (u16::MAX, 0)); |
| 8261 | self.uv_mode_row.iter_mut().for_each(|s| *s = (u16::MAX, 0)); |
| 8262 | // lane-tiles r11 (COMMON's NEIGHBOUR MAPS rule): the palette side bands |
| 8263 | // are per-mi maps like every other one here, so they reset with the |
| 8264 | // row too -- the left guard above is what actually stops a cross-tile |
| 8265 | // read, this keeps a stale colour out of the arrays at all. |
| 8266 | self.left_palette_size.iter_mut().for_each(|s| *s = 0); |
| 8267 | self.left_palette_colors.iter_mut().for_each(|c| *c = [0u16; 8]); |
| 8268 | self.left_palette_uv_size.iter_mut().for_each(|s| *s = 0); |
| 8269 | self.left_palette_uv_colors.iter_mut().for_each(|c| *c = [0u16; 8]); |
| 8270 | // lane-mergefix r2: `av1_zero_left_context` memsets |
| 8271 | // `left_txfm_context_buffer` to `tx_size_high[TX_SIZES_LARGEST]` at |
| 8272 | // every superblock row of every tile -- the var-tx left context does |
| 8273 | // NOT carry over a tile boundary (its `txfm_partition` ctx read the |
| 8274 | // previous tile column's last block otherwise). |
| 8275 | self.left_txfm.iter_mut().for_each(|t| *t = TXFM_CTX_INIT); |
| 8276 | } |
| 8277 | |
| 8278 | /// Records a block's `skip`/`is_inter`/`interp_filter` state for the next |
| 8279 | /// block that reads it as a neighbour -- [`crate::tile`]'s |
| 8280 | /// `record_inter`. `filter` is `[3, 3]` for an intra block (spec |
| 8281 | /// `get_ref_filter_type`'s "not this ref frame" case reads the same as |
| 8282 | /// "no neighbour"). `ref_frame` is `-1` for an intra block, else the |
| 8283 | /// `1..=7` reference it coded (lane-av1refs's `single_ref_p*_ctx` needs |
| 8284 | /// the real value, not just `is_inter`). |
| 8285 | #[allow(dead_code)] // read only from the `#[cfg(test)]` gates |
| 8286 | fn record_inter( |
| 8287 | &mut self, |
| 8288 | at: (usize, usize), |
| 8289 | side: usize, |
| 8290 | skip: bool, |
| 8291 | is_inter: bool, |
| 8292 | ref_frame: i8, |
| 8293 | filter: [u8; 2], |
| 8294 | skip_mode: bool, |
| 8295 | ) { |
| 8296 | self.record_inter_rect(at, side, side, skip, is_inter, ref_frame, filter, skip_mode); |
| 8297 | } |
| 8298 | |
| 8299 | /// [`Self::record_inter`] with independent above (`w_sub`, [`SUB`]-grid |
| 8300 | /// columns) / left (`h_sub`, [`SUB`]-grid rows) extents -- lane-partitions |
| 8301 | /// r1: a true rectangular strip's above-context span (its own width) and |
| 8302 | /// left-context span (its own height) differ, unlike every caller before |
| 8303 | /// this round (class neighbour-votes-all-its-fields). |
| 8304 | #[allow(clippy::too_many_arguments)] |
| 8305 | #[allow(dead_code)] // read only from the `#[cfg(test)]` gates |
| 8306 | fn record_inter_rect( |
| 8307 | &mut self, |
| 8308 | at: (usize, usize), |
| 8309 | w_sub: usize, |
| 8310 | h_sub: usize, |
| 8311 | skip: bool, |
| 8312 | is_inter: bool, |
| 8313 | ref_frame: i8, |
| 8314 | filter: [u8; 2], |
| 8315 | skip_mode: bool, |
| 8316 | ) { |
| 8317 | let (r, c) = at; |
| 8318 | self.record_inter_rect_mi( |
| 8319 | (r * (SUB / MI), c * (SUB / MI)), |
| 8320 | w_sub / MI, |
| 8321 | h_sub / MI, |
| 8322 | skip, |
| 8323 | is_inter, |
| 8324 | ref_frame, |
| 8325 | filter, |
| 8326 | skip_mode, |
| 8327 | ); |
| 8328 | } |
| 8329 | |
| 8330 | /// [`Self::record_inter_rect`] in mi (4px) units -- lane-inter8 r2: an |
| 8331 | /// 8x8 leaf covers 2x2 mi, half a [`SUB`] cell in each direction, so the |
| 8332 | /// side bands are written (and read) per mi. Every [`SUB`]-unit caller |
| 8333 | /// routes through the wrapper above. |
| 8334 | #[allow(clippy::too_many_arguments)] |
| 8335 | fn record_inter_rect_mi( |
| 8336 | &mut self, |
| 8337 | at_mi: (usize, usize), |
| 8338 | w_mi: usize, |
| 8339 | h_mi: usize, |
| 8340 | skip: bool, |
| 8341 | is_inter: bool, |
| 8342 | ref_frame: i8, |
| 8343 | filter: [u8; 2], |
| 8344 | skip_mode: bool, |
| 8345 | ) { |
| 8346 | let (r, c) = at_mi; |
| 8347 | crate::census::block(w_mi, h_mi, is_inter, ref_frame, skip); |
| 8348 | // lane-t900 r8 (class new-map-ignores-tile-edge's twin: a map NOBODY |
| 8349 | // writes for a whole block class): an INTER block left the luma mode |
| 8350 | // map untouched, so a later intra block's `get_intra_edge_filter_type` |
| 8351 | // (spec 7.11.2, libaom `reconintra.h`'s `is_smooth`) fell back to the |
| 8352 | // coarse SUB-row band, which still held a SMOOTH mode from an earlier |
| 8353 | // block in that pixel row -- the directional edge filter then ran at |
| 8354 | // strength 3 instead of 2. libaom reads `mbmi->mode` of the neighbour, |
| 8355 | // which for an inter block is an INTER mode (>= NEARESTMV) and is |
| 8356 | // never smooth; DC_PRED is this map's equivalent (the sub-8x8 groups |
| 8357 | // already stamped it). |
| 8358 | // The chroma twin: `is_smooth(mbmi, plane > 0)` returns 0 outright for |
| 8359 | // an inter neighbour ("uv_mode is not set for inter blocks"), so the |
| 8360 | // uv map must read DC_PRED over an inter block's cells too. |
| 8361 | if is_inter { |
| 8362 | self.record_mode_mi(r, c, w_mi, h_mi, DC_PRED); |
| 8363 | self.record_uv_mode_mi(r, c, w_mi, h_mi, DC_PRED); |
| 8364 | } |
| 8365 | let altref = is_inter && ref_frame == crate::mvstack::ALTREF_FRAME; |
| 8366 | self.above_skip[c..c + w_mi].fill(skip); |
| 8367 | self.above_skip_mode[c..c + w_mi].fill(skip_mode); |
| 8368 | self.above_inter[c..c + w_mi].fill(is_inter); |
| 8369 | self.above_ref[c..c + w_mi].fill(ref_frame); |
| 8370 | // The single-ref default: `record_compound_ctx_rect_mi` overwrites |
| 8371 | // this with the block's real second reference for a compound block, |
| 8372 | // called right after this one (lane-publish2 audit). |
| 8373 | self.above_ref1[c..c + w_mi].fill(None); |
| 8374 | // libaom `get_comp_group_idx_context`/`get_comp_index_context`'s |
| 8375 | // single-ref special case: `ref_frame[0] == ALTREF_FRAME` reads |
| 8376 | // as `3`/`1` even though this block has no second reference. |
| 8377 | // `record_compound_ctx` overwrites this with the real bit for an |
| 8378 | // actual compound block's cells, called right after this one. |
| 8379 | self.above_comp_group_idx[c..c + w_mi].fill(if altref { 3 } else { 0 }); |
| 8380 | self.above_compound_idx[c..c + w_mi].fill(u8::from(altref)); |
| 8381 | self.above_filter[c..c + w_mi].fill(filter); |
| 8382 | self.left_skip[r..r + h_mi].fill(skip); |
| 8383 | self.left_skip_mode[r..r + h_mi].fill(skip_mode); |
| 8384 | self.left_inter[r..r + h_mi].fill(is_inter); |
| 8385 | self.left_ref[r..r + h_mi].fill(ref_frame); |
| 8386 | self.left_ref1[r..r + h_mi].fill(None); |
| 8387 | self.left_comp_group_idx[r..r + h_mi].fill(if altref { 3 } else { 0 }); |
| 8388 | self.left_compound_idx[r..r + h_mi].fill(u8::from(altref)); |
| 8389 | self.left_filter[r..r + h_mi].fill(filter); |
| 8390 | } |
| 8391 | |
| 8392 | /// Overwrites [`Self::record_inter`]'s ALTREF-special-case default with |
| 8393 | /// a real compound block's own `comp_group_idx`/`compound_idx` bits |
| 8394 | /// (libaom `has_second_ref(mbmi)` branch of `get_comp_group_idx_context`/ |
| 8395 | /// `get_comp_index_context`) -- called right after `record_inter` for |
| 8396 | /// the same `at`/`side`, only on the `COMPOUND_REFERENCE` path. Also |
| 8397 | /// stamps `above_ref1`/`left_ref1` with the block's real second |
| 8398 | /// reference, which `record_inter` always clears to `None` (lane-av1comp: |
| 8399 | /// this is the one caller that actually knows it) -- so the next block's |
| 8400 | /// [`NeighbourRef`](crate::mvstack::NeighbourRef) sees a real compound |
| 8401 | /// neighbour instead of `record_inter`'s single-ref default. |
| 8402 | #[allow(dead_code)] // read only from the `#[cfg(test)]` gates |
| 8403 | fn record_compound_ctx( |
| 8404 | &mut self, |
| 8405 | at: (usize, usize), |
| 8406 | side: usize, |
| 8407 | ref1: i8, |
| 8408 | comp_group_idx: u8, |
| 8409 | compound_idx: u8, |
| 8410 | ) { |
| 8411 | self.record_compound_ctx_rect(at, side, side, ref1, comp_group_idx, compound_idx); |
| 8412 | } |
| 8413 | |
| 8414 | /// [`Self::record_compound_ctx`] with independent above/left extents, |
| 8415 | /// mirroring [`Self::record_inter_rect`] (lane-partitions r1). |
| 8416 | #[allow(clippy::too_many_arguments)] |
| 8417 | #[allow(dead_code)] // read only from the `#[cfg(test)]` gates |
| 8418 | fn record_compound_ctx_rect( |
| 8419 | &mut self, |
| 8420 | at: (usize, usize), |
| 8421 | w_sub: usize, |
| 8422 | h_sub: usize, |
| 8423 | ref1: i8, |
| 8424 | comp_group_idx: u8, |
| 8425 | compound_idx: u8, |
| 8426 | ) { |
| 8427 | let (r, c) = at; |
| 8428 | self.record_compound_ctx_rect_mi( |
| 8429 | (r * (SUB / MI), c * (SUB / MI)), |
| 8430 | w_sub / MI, |
| 8431 | h_sub / MI, |
| 8432 | ref1, |
| 8433 | comp_group_idx, |
| 8434 | compound_idx, |
| 8435 | ); |
| 8436 | } |
| 8437 | |
| 8438 | /// [`Self::record_compound_ctx_rect`] in mi units (lane-inter8 r2, see |
| 8439 | /// [`Self::record_inter_rect_mi`]). |
| 8440 | fn record_compound_ctx_rect_mi( |
| 8441 | &mut self, |
| 8442 | at_mi: (usize, usize), |
| 8443 | w_mi: usize, |
| 8444 | h_mi: usize, |
| 8445 | ref1: i8, |
| 8446 | comp_group_idx: u8, |
| 8447 | compound_idx: u8, |
| 8448 | ) { |
| 8449 | let (r, c) = at_mi; |
| 8450 | crate::census::compound(); |
| 8451 | for cell in 0..w_mi { |
| 8452 | self.above_ref1[c + cell] = Some(ref1); |
| 8453 | self.above_comp_group_idx[c + cell] = comp_group_idx; |
| 8454 | self.above_compound_idx[c + cell] = compound_idx; |
| 8455 | } |
| 8456 | for cell in 0..h_mi { |
| 8457 | self.left_ref1[r + cell] = Some(ref1); |
| 8458 | self.left_comp_group_idx[r + cell] = comp_group_idx; |
| 8459 | self.left_compound_idx[r + cell] = compound_idx; |
| 8460 | } |
| 8461 | } |
| 8462 | |
| 8463 | /// The context of a block's partition symbol: delegates to the mi-precise |
| 8464 | /// reader (same pattern as `around`/`around_mi`) -- `above_side`/ |
| 8465 | /// `left_side` are only ever advanced in whole-[`SUB`] steps by |
| 8466 | /// [`Self::record`], so a leaf8's `record_mi` (lane-av1-rect) -- which |
| 8467 | /// only touches the finer mi arrays -- leaves them stale for the *next* |
| 8468 | /// sibling's own partition symbol. |
| 8469 | fn partition_ctx(&self, at: (usize, usize), side: usize) -> usize { |
| 8470 | let (r, c) = at; |
| 8471 | self.partition_ctx_mi((r * (SUB / MI), c * (SUB / MI)), side) |
| 8472 | } |
| 8473 | |
| 8474 | /// [`Self::partition_ctx`] at mi granularity, for an 8x8 leaf of a |
| 8475 | /// straddling 16x16 block: reads the finer `above_side_mi`/`left_side_mi` |
| 8476 | /// arrays so the second leaf sees the first leaf's own 8x8 side rather |
| 8477 | /// than the enclosing 16x16 slot's stale, shared state. |
| 8478 | fn partition_ctx_mi(&self, (mi_r, mi_c): (usize, usize), side: usize) -> usize { |
| 8479 | 2 * usize::from(usize::from(self.left_side_mi[mi_r]) * 2 <= side) |
| 8480 | + usize::from(usize::from(self.above_side_mi[mi_c]) * 2 <= side) |
| 8481 | } |
| 8482 | |
| 8483 | /// The gathered coded/DC-sign state of the blocks above and to the left |
| 8484 | /// of a block `side` samples across, per plane. |
| 8485 | fn around(&self, (r, c): (usize, usize), side: usize) -> [(bool, bool, i32); 3] { |
| 8486 | self.around_mi((r * (SUB / MI), c * (SUB / MI)), side) |
| 8487 | } |
| 8488 | |
| 8489 | /// [`Self::around`] taking the block's position directly in 4x4 mode-info |
| 8490 | /// units, for the same reason [`Self::record_mi`] does. |
| 8491 | fn around_mi(&self, (mi_r, mi_c): (usize, usize), side: usize) -> [(bool, bool, i32); 3] { |
| 8492 | let side_mi = side / MI; |
| 8493 | // One pass over the cells accumulating all three planes, not one pass |
| 8494 | // per plane: the three planes of a cell live in the same |
| 8495 | // `[Neighbour; 3]` (6 bytes), so the per-plane form loaded the same |
| 8496 | // cache lines three times (lane-publish). Clamp each edge to the true |
| 8497 | // frame extent: a block at the right/bottom edge may be padded beyond |
| 8498 | // `mi_cols`/`mi_rows`, and libaom's unavailable context is zero there. |
| 8499 | let above_mi = side_mi.min(self.mi_cols.saturating_sub(mi_c)); |
| 8500 | let left_mi = side_mi.min(self.mi_rows.saturating_sub(mi_r)); |
| 8501 | let mut out = [(false, false, 0i32); 3]; |
| 8502 | for cell in 0..above_mi { |
| 8503 | let above = &self.above[mi_c + cell]; |
| 8504 | for (plane, o) in out.iter_mut().enumerate() { |
| 8505 | o.0 |= above[plane].level != 0; |
| 8506 | o.2 += dc_vote(above[plane].dc); |
| 8507 | } |
| 8508 | } |
| 8509 | for cell in 0..left_mi { |
| 8510 | let left = &self.left[mi_r + cell]; |
| 8511 | for (plane, o) in out.iter_mut().enumerate() { |
| 8512 | o.1 |= left[plane].level != 0; |
| 8513 | o.2 += dc_vote(left[plane].dc); |
| 8514 | } |
| 8515 | } |
| 8516 | out |
| 8517 | } |
| 8518 | |
| 8519 | /// [`Self::around_mi_rect`] for the CHROMA planes of a 4:2:2 frame |
| 8520 | /// (lane-av1-422f): libaom `get_txb_ctx_general` reads its above votes |
| 8521 | /// over `txb_w_unit` CHROMA 4-px cells, and at ss_x 1 one chroma cell |
| 8522 | /// spans TWO luma mi columns. Both cells of a column carry the covering |
| 8523 | /// unit's whole-unit dc sign (the same value libaom stores), so the |
| 8524 | /// per-luma-mi sum counts each chroma column twice -- and |
| 8525 | /// `dc_sign_ctx`'s signum flips whenever the above and left votes have |
| 8526 | /// opposite signs with equal magnitude (measured at mi(4,4) plane 1 of |
| 8527 | /// the t422 fixture: ours read `dc_sign_cdf[1]`, the oracle row 0). |
| 8528 | /// Sampling every second above cell counts each chroma column once, |
| 8529 | /// which IS libaom's sum. The left extent is unsubsquared at ss_y 0, so |
| 8530 | /// its per-mi cells already line up one-to-one. The level booleans are |
| 8531 | /// ORs over duplicated cells, so the sampling cannot change them. |
| 8532 | fn around_mi_422_chroma( |
| 8533 | &self, |
| 8534 | (mi_r, mi_c): (usize, usize), |
| 8535 | w: usize, |
| 8536 | h: usize, |
| 8537 | ) -> [(bool, bool, i32); 3] { |
| 8538 | let (w_mi, h_mi) = (w / MI, h / MI); |
| 8539 | let above_mi = w_mi.min(self.mi_cols.saturating_sub(mi_c)); |
| 8540 | let left_mi = h_mi.min(self.mi_rows.saturating_sub(mi_r)); |
| 8541 | let mut out = [(false, false, 0i32); 3]; |
| 8542 | for cell in (0..above_mi).step_by(2) { |
| 8543 | let above = &self.above[mi_c + cell]; |
| 8544 | for (plane, o) in out.iter_mut().enumerate() { |
| 8545 | o.0 |= above[plane].level != 0; |
| 8546 | o.2 += dc_vote(above[plane].dc); |
| 8547 | } |
| 8548 | } |
| 8549 | for cell in 0..left_mi { |
| 8550 | let left = &self.left[mi_r + cell]; |
| 8551 | for (plane, o) in out.iter_mut().enumerate() { |
| 8552 | o.1 |= left[plane].level != 0; |
| 8553 | o.2 += dc_vote(left[plane].dc); |
| 8554 | } |
| 8555 | } |
| 8556 | out |
| 8557 | } |
| 8558 | |
| 8559 | /// [`Self::around`] with independent above (`w`)/left (`h`) extents, in |
| 8560 | /// pixels -- lane-rectwire, mirrors [`Self::record_rect`]'s asymmetric |
| 8561 | /// extent pattern for a true rectangular strip's own coefficient context. |
| 8562 | fn around_rect(&self, (r, c): (usize, usize), w: usize, h: usize) -> [(bool, bool, i32); 3] { |
| 8563 | self.around_mi_rect((r * (SUB / MI), c * (SUB / MI)), w, h) |
| 8564 | } |
| 8565 | |
| 8566 | /// [`Self::around_rect`] taking the block's position directly in 4x4 |
| 8567 | /// mode-info units, mirroring [`Self::around_mi`]. |
| 8568 | fn around_mi_rect(&self, (mi_r, mi_c): (usize, usize), w: usize, h: usize) -> [(bool, bool, i32); 3] { |
| 8569 | let (w_mi, h_mi) = (w / MI, h / MI); |
| 8570 | let above_mi = w_mi.min(self.mi_cols.saturating_sub(mi_c)); |
| 8571 | let left_mi = h_mi.min(self.mi_rows.saturating_sub(mi_r)); |
| 8572 | // [`Self::around_mi`]'s single pass, with the two extents split. |
| 8573 | let mut out = [(false, false, 0i32); 3]; |
| 8574 | for cell in 0..above_mi { |
| 8575 | let above = &self.above[mi_c + cell]; |
| 8576 | for (plane, o) in out.iter_mut().enumerate() { |
| 8577 | o.0 |= above[plane].level != 0; |
| 8578 | o.2 += dc_vote(above[plane].dc); |
| 8579 | } |
| 8580 | } |
| 8581 | for cell in 0..left_mi { |
| 8582 | let left = &self.left[mi_r + cell]; |
| 8583 | for (plane, o) in out.iter_mut().enumerate() { |
| 8584 | o.1 |= left[plane].level != 0; |
| 8585 | o.2 += dc_vote(left[plane].dc); |
| 8586 | } |
| 8587 | } |
| 8588 | if crate::envflags::env_flag!("EC_DCDUMP") { |
| 8589 | for (plane, o) in out.iter().enumerate() { |
| 8590 | let vote = o.2; |
| 8591 | let ab: Vec<String> = (0..above_mi) |
| 8592 | .map(|k| format!("{:?}/{}", self.above[mi_c + k][plane].dc, self.above[mi_c + k][plane].level)) |
| 8593 | .collect(); |
| 8594 | let lf: Vec<String> = (0..left_mi) |
| 8595 | .map(|k| format!("{:?}/{}", self.left[mi_r + k][plane].dc, self.left[mi_r + k][plane].level)) |
| 8596 | .collect(); |
| 8597 | eprintln!("EC_DCDUMP mi=({mi_r},{mi_c}) plane={plane} wh=({w},{h}) vote={vote} above=[{}] left=[{}]", ab.join(","), lf.join(",")); |
| 8598 | } |
| 8599 | } |
| 8600 | out |
| 8601 | } |
| 8602 | |
| 8603 | /// The luma `txb_skip_ctx` of a transform unit smaller than its own block |
| 8604 | /// (spec `get_txb_ctx_general`, plane 0, `plane_bsize != tx_size`): the |
| 8605 | /// above/left neighbours' magnitude tiers, OR-reduced over the unit's own |
| 8606 | /// span and each clamped to 4, indexed into [`SKIP_CONTEXTS`]. Unlike |
| 8607 | /// [`Self::around_mi`]'s plain coded/uncoded bit, this needs the real |
| 8608 | /// cumulative level -- a lone-TU block never calls this (its `txb_skip_ctx` |
| 8609 | /// is always 0, spec's `plane_bsize == tx_size` branch). |
| 8610 | fn luma_skip_ctx(&self, at_mi: (usize, usize), side_mi: usize) -> usize { |
| 8611 | self.luma_skip_ctx_rect(at_mi, side_mi, side_mi) |
| 8612 | } |
| 8613 | |
| 8614 | /// [`Self::luma_skip_ctx`] with the unit's own width and height in mi |
| 8615 | /// units (lane-rectsplitx r1) -- `get_txb_ctx_general` ORs the above |
| 8616 | /// context over `tx_size_wide` and the left one over `tx_size_high`. |
| 8617 | fn luma_skip_ctx_rect( |
| 8618 | &self, |
| 8619 | (mi_r, mi_c): (usize, usize), |
| 8620 | w_mi: usize, |
| 8621 | h_mi: usize, |
| 8622 | ) -> usize { |
| 8623 | let side_mi = w_mi; |
| 8624 | let above_mi = w_mi.min(self.mi_cols.saturating_sub(mi_c)); |
| 8625 | let left_mi = h_mi.min(self.mi_rows.saturating_sub(mi_r)); |
| 8626 | let mut top = 0u8; |
| 8627 | let mut left = 0u8; |
| 8628 | for cell in 0..above_mi { |
| 8629 | top |= self.above[mi_c + cell][0].level; |
| 8630 | } |
| 8631 | for cell in 0..left_mi { |
| 8632 | left |= self.left[mi_r + cell][0].level; |
| 8633 | } |
| 8634 | let ctx = SKIP_CONTEXTS[(top as usize).min(4)][(left as usize).min(4)]; |
| 8635 | if crate::envflags::env_flag!("EC_ECDUMP") { |
| 8636 | let ab: Vec<String> = |
| 8637 | (0..above_mi).map(|k| self.above[mi_c + k][0].level.to_string()).collect(); |
| 8638 | let lf: Vec<String> = |
| 8639 | (0..left_mi).map(|k| self.left[mi_r + k][0].level.to_string()).collect(); |
| 8640 | // lane-av1mix: the last few msac register states, so the |
| 8641 | // byte-position of the `all_zero` read can be matched against |
| 8642 | // aomdec's `EC=(value,rng,bytepos)` dump (decodetxb.c) without a |
| 8643 | // second run. |
| 8644 | let recent = crate::msac::symtrace::ecdump_recent(4) |
| 8645 | .iter() |
| 8646 | .map(|(_low, value, rng, bytepos)| format!("({value},{rng},{bytepos})")) |
| 8647 | .collect::<Vec<_>>() |
| 8648 | .join(" "); |
| 8649 | eprintln!( |
| 8650 | "EC_ECDUMP plane=0 mi=({mi_r},{mi_c}) wh=({w_mi},{h_mi}) ctx={ctx} above=[{},] left=[{},] recentEC=[{}]", |
| 8651 | ab.join(","), lf.join(","), recent |
| 8652 | ); |
| 8653 | } |
| 8654 | if crate::envflags::env_flag!("EC_AV1_TRACE") { |
| 8655 | eprintln!( |
| 8656 | "TRACE luma_skip_ctx mi=({mi_r},{mi_c}) side_mi={side_mi} top={top} left={left} ctx={ctx} band={:?}", |
| 8657 | self.left.iter().map(|e| e[0].level).collect::<Vec<_>>() |
| 8658 | ); |
| 8659 | } |
| 8660 | ctx |
| 8661 | } |
| 8662 | |
| 8663 | /// Writes one coded block into every cell it covers, on both grids: the |
| 8664 | /// mode/side arrays up to `side`'s own width, and the coefficient-context |
| 8665 | /// arrays up to the true frame edge — the units past it are left at their |
| 8666 | /// default (uncoded), even mid-cell, exactly as [`crate::tile`]'s own |
| 8667 | /// `record` leaves them (spec `av1_set_entropy_contexts`, which clamps to |
| 8668 | /// `blocks_wide`/`blocks_high` derived from the true `mi_cols`/`mi_rows`, |
| 8669 | /// not from this block's own side). |
| 8670 | fn record( |
| 8671 | &mut self, |
| 8672 | at: (usize, usize), |
| 8673 | side: usize, |
| 8674 | mode: usize, |
| 8675 | uv_mode: usize, |
| 8676 | grids: &[&[i32]; 3], |
| 8677 | ) { |
| 8678 | self.record_rect(at, side, side, mode, uv_mode, grids); |
| 8679 | } |
| 8680 | |
| 8681 | /// [`Self::record`] with independent above (`w`)/left (`h`) extents, in |
| 8682 | /// pixels -- lane-partitions r1: `above_side`/`left_side` (read back by |
| 8683 | /// [`Self::partition_ctx`]) are the neighbour's own width/height per the |
| 8684 | /// spec's per-edge partition-context rule, so a true rectangular block |
| 8685 | /// naturally wants two different values here rather than one. |
| 8686 | /// Writes this block's luma `mode` into the mi-granular |
| 8687 | /// [`Self::sub8_mode_col`]/[`Self::sub8_mode_row`] maps, keyed by the mi |
| 8688 | /// position a *later* neighbour would read it from: the block's LAST mi |
| 8689 | /// row for every column it spans (what the block below reads via |
| 8690 | /// `mi(row-1, col)`) and its LAST mi column for every row (what the |
| 8691 | /// block to the right reads via `mi(row, col-1)`). Only sub-8x8 leaves |
| 8692 | /// read these back, and only when the recorded position is exactly their |
| 8693 | /// own neighbour -- see [`Self::sub8_mode_col`]'s doc. |
| 8694 | fn record_mode_mi( |
| 8695 | &mut self, |
| 8696 | mi_r: usize, |
| 8697 | mi_c: usize, |
| 8698 | mi_w: usize, |
| 8699 | mi_h: usize, |
| 8700 | mode: usize, |
| 8701 | ) { |
| 8702 | let (last_r, last_c) = ((mi_r + mi_h - 1) as u16, (mi_c + mi_w - 1) as u16); |
| 8703 | let mode = mode as u8; |
| 8704 | for cell in 0..mi_w { |
| 8705 | if let Some(slot) = self.sub8_mode_col.get_mut(mi_c + cell) { |
| 8706 | *slot = (last_r, mode); |
| 8707 | } |
| 8708 | } |
| 8709 | for cell in 0..mi_h { |
| 8710 | if let Some(slot) = self.sub8_mode_row.get_mut(mi_r + cell) { |
| 8711 | *slot = (last_c, mode); |
| 8712 | } |
| 8713 | } |
| 8714 | } |
| 8715 | |
| 8716 | /// The mi-exact above/left neighbour luma mode for a block whose top-left |
| 8717 | /// mi is `(mi_r, mi_c)`, or `None` when no block has recorded that exact |
| 8718 | /// mi position (then the coarse [`SUB`]-grid slot is right by |
| 8719 | /// construction). See [`Self::record_mode_mi`]. |
| 8720 | fn mode_above_mi(&self, mi_r: usize, mi_c: usize) -> Option<usize> { |
| 8721 | match self.sub8_mode_col.get(mi_c) { |
| 8722 | Some(&(row, m)) if mi_r > self.tile_row0_mi && usize::from(row) == mi_r - 1 => Some(usize::from(m)), |
| 8723 | _ => None, |
| 8724 | } |
| 8725 | } |
| 8726 | |
| 8727 | fn mode_left_mi(&self, mi_r: usize, mi_c: usize) -> Option<usize> { |
| 8728 | match self.sub8_mode_row.get(mi_r) { |
| 8729 | Some(&(col, m)) if mi_c > self.tile_col0_mi && usize::from(col) == mi_c - 1 => Some(usize::from(m)), |
| 8730 | _ => None, |
| 8731 | } |
| 8732 | } |
| 8733 | |
| 8734 | /// [`Self::record_mode_mi`] for the block's `uv_mode`, into |
| 8735 | /// [`Self::uv_mode_col`]/[`Self::uv_mode_row`]. |
| 8736 | fn record_uv_mode_mi( |
| 8737 | &mut self, |
| 8738 | mi_r: usize, |
| 8739 | mi_c: usize, |
| 8740 | mi_w: usize, |
| 8741 | mi_h: usize, |
| 8742 | uv_mode: usize, |
| 8743 | ) { |
| 8744 | let (last_r, last_c) = (mi_r + mi_h - 1, mi_c + mi_w - 1); |
| 8745 | fill_span(&mut self.uv_mode_col, mi_c, mi_w, (last_r as u16, uv_mode as u8)); |
| 8746 | fill_span(&mut self.uv_mode_row, mi_r, mi_h, (last_c as u16, uv_mode as u8)); |
| 8747 | // libaom writes one `MB_MODE_INFO` pointer into every mi cell a block |
| 8748 | // covers, so the chroma neighbour read below is a plain grid read. |
| 8749 | let stride = self.uv_grid_cols; |
| 8750 | let n = stride.saturating_sub(mi_c).min(last_c + 1 - mi_c); |
| 8751 | for row in mi_r..=last_r { |
| 8752 | fill_span(&mut self.uv_mode_grid, row * stride + mi_c, n, uv_mode as u8); |
| 8753 | } |
| 8754 | } |
| 8755 | |
| 8756 | /// Whether either mi-exact chroma neighbour of the block at `(mi_r, mi_c)` |
| 8757 | /// coded a smooth `uv_mode` -- libaom's `intra_edge_filter_type` for |
| 8758 | /// planes 1/2. Falls back to the coarse [`SUB`] slot on whichever axis no |
| 8759 | /// block has recorded the exact neighbouring mi. |
| 8760 | fn smooth_uv_neighbour( |
| 8761 | &self, |
| 8762 | mi_r: usize, |
| 8763 | mi_c: usize, |
| 8764 | r: usize, |
| 8765 | c: usize, |
| 8766 | fctx: &crate::decode::FrameCtx, |
| 8767 | ) -> bool { |
| 8768 | // lane-kf900 r2: libaom reads the chroma neighbours off the CHROMA |
| 8769 | // REFERENCE block, not off the mi that carries the chroma syntax. |
| 8770 | // `set_mi_row_col` (`blockd.h`) takes |
| 8771 | // `base_mi = &xd->mi[-(mi_row & ss_y) * stride - (mi_col & ss_x)]` and |
| 8772 | // only then `chroma_above_mbmi = base_mi[-stride]` / |
| 8773 | // `chroma_left_mbmi = base_mi[-1]`. A sub-8 pair codes its chroma at |
| 8774 | // the ODD mi, so without the snap we looked one mi row/column too far |
| 8775 | // down/right -- the mi-exact slot never matched and the coarse |
| 8776 | // fallback answered instead. Measured on a 4K 10-bit key frame, chroma |
| 8777 | // 8x4 at (688, 100) = mi (51, 344): libaom's |
| 8778 | // `get_intra_edge_filter_type` is 1 (the chroma block above it is |
| 8779 | // `UV_SMOOTH_PRED`), ours was 0, which flips |
| 8780 | // `av1_use_intra_edge_upsample(8, 4, -6, type)` (`blk_wh == 12`: |
| 8781 | // false for type 1, TRUE for type 0) and upsampled an edge libaom |
| 8782 | // leaves alone -- +1 on rows 2/3 of that block and every chroma block |
| 8783 | // predicting from it (class `context-read-from-one-cell`). |
| 8784 | // lane-av1-444: the snap is the SEQUENCE's subsampling, hardcoded |
| 8785 | // `& !1` before -- at subsampling 0/0 libaom's `mi_row & 0` snaps |
| 8786 | // nothing, and the hardcoded snap read a 4x4 leaf's smooth |
| 8787 | // neighbour two mi away (frame 0 of `av1-profile1-444.ivf`, leaf |
| 8788 | // mi(5,15): left neighbour SMOOTH_V at mi(5,14), read at mi(4,13)). |
| 8789 | let snap_y = (1usize << ss_y(fctx)) - 1; |
| 8790 | let snap_x = (1usize << ss_x(fctx)) - 1; |
| 8791 | self.smooth_uv_neighbour_unsnapped(mi_r & !snap_y, mi_c & !snap_x, r, c) |
| 8792 | } |
| 8793 | |
| 8794 | /// [`Self::smooth_uv_neighbour`] without the chroma-reference snap, i.e. |
| 8795 | /// read off the mi passed in exactly. Only the 16x4/4x16 pair witness |
| 8796 | /// counter wants this: it measures how often the snap CHANGES the answer, |
| 8797 | /// which is what the lane-kf1200/kf900 chroma-reference fix buys. Every |
| 8798 | /// decode path uses the snapping form. |
| 8799 | fn smooth_uv_neighbour_unsnapped( |
| 8800 | &self, |
| 8801 | mi_r: usize, |
| 8802 | mi_c: usize, |
| 8803 | r: usize, |
| 8804 | c: usize, |
| 8805 | ) -> bool { |
| 8806 | // lane-mtfix r1: availability is TILE-relative on BOTH axes and on the |
| 8807 | // coarse fallback too. `above_uv_mode` is a frame-wide [`SUB`]-grid |
| 8808 | // array that `start_tile` does not clear, so a block on a tile's first |
| 8809 | // mi row used to read the uv_mode of the block above it in the tile |
| 8810 | // ABOVE and filter its chroma edge at that neighbour's strength |
| 8811 | // (2x2-tile intra gate, seed 46: chroma-only +-1..2 over the 8x8 block |
| 8812 | // at chroma (120, 32), the top-right tile's first block). |
| 8813 | let have_above = mi_r > self.tile_row0_mi; |
| 8814 | let have_left = mi_c > self.tile_col0_mi; |
| 8815 | if self.tile_row0_mi > 0 && !have_above && self.above_uv_mode[c] != DC_PRED |
| 8816 | || self.tile_col0_mi > 0 && !have_left && self.left_uv_mode[r] != DC_PRED |
| 8817 | { |
| 8818 | hit!(UV_TILE_EDGE_SUPPRESSED_HITS); |
| 8819 | } |
| 8820 | // The mi grid is libaom's own read; the two maps below stay as the |
| 8821 | // fallback for cells no block has written (see [`Self::uv_mode_grid`]). |
| 8822 | let cell = |grid: &Vec<u8>, row: usize, col: usize| -> Option<usize> { |
| 8823 | (col < self.uv_grid_cols) |
| 8824 | .then(|| grid.get(row * self.uv_grid_cols + col).copied()) |
| 8825 | .flatten() |
| 8826 | .filter(|&m| m != u8::MAX) |
| 8827 | .map(usize::from) |
| 8828 | }; |
| 8829 | let above = match self.uv_mode_col.get(mi_c) { |
| 8830 | _ if have_above && let Some(m) = cell(&self.uv_mode_grid, mi_r - 1, mi_c) => m, |
| 8831 | Some(&(row, m)) if have_above && usize::from(row) == mi_r - 1 => usize::from(m), |
| 8832 | _ if have_above => self.above_uv_mode[c], |
| 8833 | _ => DC_PRED, |
| 8834 | }; |
| 8835 | let left = match self.uv_mode_row.get(mi_r) { |
| 8836 | _ if have_left && let Some(m) = cell(&self.uv_mode_grid, mi_r, mi_c - 1) => m, |
| 8837 | Some(&(col, m)) if have_left && usize::from(col) == mi_c - 1 => usize::from(m), |
| 8838 | _ if have_left => self.left_uv_mode[r], |
| 8839 | _ => DC_PRED, |
| 8840 | }; |
| 8841 | if have_above && cell(&self.uv_mode_grid, mi_r - 1, mi_c).is_some_and(|m| { |
| 8842 | !matches!(self.uv_mode_col.get(mi_c), Some(&(row, cm)) if usize::from(row) == mi_r - 1 && usize::from(cm) == m) |
| 8843 | }) || have_left && cell(&self.uv_mode_grid, mi_r, mi_c - 1).is_some_and(|m| { |
| 8844 | !matches!(self.uv_mode_row.get(mi_r), Some(&(col, cm)) if usize::from(col) == mi_c - 1 && usize::from(cm) == m) |
| 8845 | }) { |
| 8846 | hit!(UV_MODE_GRID_OVERRIDE_HITS); |
| 8847 | } |
| 8848 | // lane-cfl r1 counter: how often the mi-exact chroma neighbour differs |
| 8849 | // from the coarse [`SUB`] slot -- i.e. how often a chroma edge would |
| 8850 | // have been filtered at the wrong strength by the coarse map alone. |
| 8851 | if above != self.above_uv_mode[c] || left != self.left_uv_mode[r] { |
| 8852 | hit!(UV_MODE_MI_OVERRIDE_HITS); |
| 8853 | } |
| 8854 | if (is_smooth_mode(self.above_uv_mode[c]) && !is_smooth_mode(above)) |
| 8855 | || (is_smooth_mode(self.left_uv_mode[r]) && !is_smooth_mode(left)) |
| 8856 | { |
| 8857 | hit!(INTER_NEIGHBOUR_NOT_SMOOTH_HITS); |
| 8858 | } |
| 8859 | is_smooth_mode(above) || is_smooth_mode(left) |
| 8860 | } |
| 8861 | |
| 8862 | /// The above/left luma modes libaom's `above_mi`/`left_mi` really see for a |
| 8863 | /// block at coarse [`SUB`]-grid `(r, c)`. lane-rectx r5: the coarse |
| 8864 | /// `above_mode`/`left_mode` slots are written in 16x16 cells, so a |
| 8865 | /// sub-16x16 neighbour (a 16x8 rect leaf, an 8x8 split cell) leaves them |
| 8866 | /// holding an OLDER, larger block's mode -- it only ever recorded into the |
| 8867 | /// mi-exact map. Every `kf_y_mode` reader goes through here, not just the |
| 8868 | /// strip path ([`decode_leaf_rect`] took this override in r4; a 32x16 strip |
| 8869 | /// whose left neighbour was a 16x8 leaf still read the stale row and picked |
| 8870 | /// a different CDF for the same mode value). |
| 8871 | /// lane-rectsplitx r2: availability-correct above/left luma modes for a |
| 8872 | /// block whose top-left mi is `(mi_r, mi_c)`. libaom reads `above_mbmi` |
| 8873 | /// only when `up_available` and `left_mbmi` only when `left_available`; |
| 8874 | /// the coarse [`SUB`] bands cannot express that, and with 8-px strips they |
| 8875 | /// actively lie -- four 32x8 strips of one `HORZ_4` share ONE 16x16 |
| 8876 | /// `left_mode` cell, so the second strip at tile column 0 read the FIRST |
| 8877 | /// strip's mode as its "left neighbour" (measured on the band fixture at |
| 8878 | /// mi(10,0): mode 2 where libaom has DC, a different `kf_y_mode` row). |
| 8879 | fn modes_above_left_mi( |
| 8880 | &self, |
| 8881 | mi_r: usize, |
| 8882 | mi_c: usize, |
| 8883 | fallback: (usize, usize), |
| 8884 | ) -> (usize, usize) { |
| 8885 | let above = if mi_r > self.tile_row0_mi { |
| 8886 | self.mode_above_mi(mi_r, mi_c).unwrap_or(fallback.0) |
| 8887 | } else { |
| 8888 | DC_PRED |
| 8889 | }; |
| 8890 | let left = if mi_c > self.tile_col0_mi { |
| 8891 | self.mode_left_mi(mi_r, mi_c).unwrap_or(fallback.1) |
| 8892 | } else { |
| 8893 | DC_PRED |
| 8894 | }; |
| 8895 | (above, left) |
| 8896 | } |
| 8897 | |
| 8898 | /// lane-modectx r1: the coarse-[`SUB`]-grid entry point, now availability |
| 8899 | /// correct. The mi-exact lookups already refuse to cross a tile edge, but |
| 8900 | /// their FALLBACK (`above_mode`/`left_mode`) did not: `left_mode` is reset |
| 8901 | /// per superblock row and never per tile, so the first block column of |
| 8902 | /// tile 2..N read the previous tile's mode as its left neighbour, and the |
| 8903 | /// above band survives across a tile ROW boundary the same way lane-mtfix's |
| 8904 | /// `above_uv_mode` did. libaom reads `above_mbmi`/`left_mbmi` only when |
| 8905 | /// `up_available`/`left_available` and uses DC_PRED otherwise |
| 8906 | /// ([`Self::modes_above_left_mi`]). |
| 8907 | fn modes_above_left(&self, r: usize, c: usize) -> (usize, usize) { |
| 8908 | let (mi_r, mi_c) = (r * (SUB / MI), c * (SUB / MI)); |
| 8909 | let (above, left) = |
| 8910 | self.modes_above_left_mi(mi_r, mi_c, (self.above_mode[c], self.left_mode[r])); |
| 8911 | if above != self.above_mode[c] || left != self.left_mode[r] { |
| 8912 | hit!(MODE_MI_OVERRIDE_HITS); |
| 8913 | } |
| 8914 | if (is_smooth_mode(self.above_mode[c]) && !is_smooth_mode(above)) |
| 8915 | || (is_smooth_mode(self.left_mode[r]) && !is_smooth_mode(left)) |
| 8916 | { |
| 8917 | hit!(INTER_NEIGHBOUR_NOT_SMOOTH_HITS); |
| 8918 | } |
| 8919 | // A neighbour the coarse band offered where the tile edge makes it |
| 8920 | // unavailable -- must never happen (see `MODE_TILE_EDGE_COARSE_LEAKS`). |
| 8921 | if (mi_r <= self.tile_row0_mi && self.above_mode[c] != DC_PRED) |
| 8922 | || (mi_c <= self.tile_col0_mi && self.left_mode[r] != DC_PRED) |
| 8923 | { |
| 8924 | hit!(MODE_TILE_EDGE_COARSE_LEAKS); |
| 8925 | } |
| 8926 | (above, left) |
| 8927 | } |
| 8928 | |
| 8929 | fn record_rect( |
| 8930 | &mut self, |
| 8931 | at: (usize, usize), |
| 8932 | w: usize, |
| 8933 | h: usize, |
| 8934 | mode: usize, |
| 8935 | uv_mode: usize, |
| 8936 | grids: &[&[i32]; 3], |
| 8937 | ) { |
| 8938 | self.record_rect_mi(sub16_to_mi(at), w, h, mode, uv_mode, grids); |
| 8939 | } |
| 8940 | |
| 8941 | /// [`Self::record_rect`] in mi (4 px) units -- lane-inter4 r1: a |
| 8942 | /// 32x8/8x32 strip of a 32-level 1:4 partition sits at an 8-px offset the |
| 8943 | /// [`SUB`]-unit `at` cannot name. The coarse 16-px bands are still keyed |
| 8944 | /// by the enclosing cell (a sub-16 extent writes none of them, exactly as |
| 8945 | /// the [`SUB`]-unit path does). |
| 8946 | fn record_rect_mi( |
| 8947 | &mut self, |
| 8948 | at_mi: (usize, usize), |
| 8949 | w: usize, |
| 8950 | h: usize, |
| 8951 | mode: usize, |
| 8952 | uv_mode: usize, |
| 8953 | grids: &[&[i32]; 3], |
| 8954 | ) { |
| 8955 | let (r, c) = (at_mi.0 / (SUB / MI), at_mi.1 / (SUB / MI)); |
| 8956 | // lane-inter4 r2: a strip whose own footprint runs past the frame's |
| 8957 | // last 16-px column/row stamps only the cells that exist (libaom's |
| 8958 | // arrays are padded past the frame and those cells are never read). |
| 8959 | for cell in 0..w / SUB { |
| 8960 | if c + cell >= self.above_mode.len() { |
| 8961 | break; |
| 8962 | } |
| 8963 | self.above_mode[c + cell] = mode; |
| 8964 | self.above_uv_mode[c + cell] = uv_mode; |
| 8965 | self.above_side[c + cell] = w; |
| 8966 | } |
| 8967 | for cell in 0..h / SUB { |
| 8968 | if r + cell >= self.left_mode.len() { |
| 8969 | break; |
| 8970 | } |
| 8971 | self.left_mode[r + cell] = mode; |
| 8972 | self.left_uv_mode[r + cell] = uv_mode; |
| 8973 | self.left_side[r + cell] = h; |
| 8974 | } |
| 8975 | self.record_mode_mi(at_mi.0, at_mi.1, w / MI, h / MI, mode); |
| 8976 | self.record_uv_mode_mi(at_mi.0, at_mi.1, w / MI, h / MI, uv_mode); |
| 8977 | self.record_mi_rect(at_mi, w, h, grids); |
| 8978 | } |
| 8979 | |
| 8980 | /// The coefficient-context half of [`Self::record`], taking the block's |
| 8981 | /// position directly in 4x4 mode-info units rather than [`SUB`]-grid |
| 8982 | /// `(r, c)`: an 8x8 leaf under a straddling 16x16 (lane-av1-rect) sits at |
| 8983 | /// a mi offset [`Self::record`]'s SUB-unit `at` cannot name. |
| 8984 | fn record_mi(&mut self, at_mi: (usize, usize), side: usize, grids: &[&[i32]; 3]) { |
| 8985 | self.record_mi_rect(at_mi, side, side, grids); |
| 8986 | } |
| 8987 | |
| 8988 | /// [`Self::record_mi`] with independent above (`w`)/left (`h`) extents, |
| 8989 | /// in pixels (lane-partitions r1, mirroring [`Self::record_rect`]). |
| 8990 | fn record_mi_rect( |
| 8991 | &mut self, |
| 8992 | at_mi: (usize, usize), |
| 8993 | w: usize, |
| 8994 | h: usize, |
| 8995 | grids: &[&[i32]; 3], |
| 8996 | ) { |
| 8997 | let (mi_r, mi_c) = at_mi; |
| 8998 | let states: [Neighbour; 3] = std::array::from_fn(|plane| neighbour_state(grids[plane])); |
| 8999 | let (w_mi, h_mi) = (w / MI, h / MI); |
| 9000 | if crate::envflags::env_flag!("EC_ECPUB") { |
| 9001 | eprintln!("EC_ECPUB blk mi=({mi_r},{mi_c}) wh=({w},{h}) lvl={}", states[0].level); |
| 9002 | } |
| 9003 | fill_span(&mut self.left_side_mi, mi_r, h_mi, h as u8); |
| 9004 | fill_span(&mut self.above_side_mi, mi_c, w_mi, w as u8); |
| 9005 | // A chroma 4x4 unit straddling the true luma edge is still whole in |
| 9006 | // chroma's own halved grid, so libaom rounds the luma bound up to the |
| 9007 | // plane's own 4x4 unit before clamping a subsampled plane |
| 9008 | // (`av1_write_intra_coeffs_mb`, encodetxb.c). |
| 9009 | let round_up_even = |n: usize| n.div_ceil(2) * 2; |
| 9010 | let bound_h = [ |
| 9011 | self.mi_rows, |
| 9012 | round_up_even(self.mi_rows), |
| 9013 | round_up_even(self.mi_rows), |
| 9014 | ]; |
| 9015 | let bound_w = [ |
| 9016 | self.mi_cols, |
| 9017 | round_up_even(self.mi_cols), |
| 9018 | round_up_even(self.mi_cols), |
| 9019 | ]; |
| 9020 | // lane-sb128c r7: a 128x64 at the bottom frame edge names mi rows |
| 9021 | // past the padded side arrays (mi_rows 198, block rows 192..208); |
| 9022 | // libaom's own arrays are superblock-sized, so those cells simply |
| 9023 | // have no reader -- [`fill_span`] drops them, as the `break` did. |
| 9024 | // `bound_h[1] == bound_h[2] >= bound_h[0]`, so the per-cell plane |
| 9025 | // predicate is three contiguous runs: all planes, chroma only, none |
| 9026 | // (lane-mi). |
| 9027 | let luma_h = h_mi.min(bound_h[0].saturating_sub(mi_r)); |
| 9028 | let chroma_h = h_mi.min(bound_h[1].saturating_sub(mi_r)); |
| 9029 | let uv_only = [Neighbour::default(), states[1], states[2]]; |
| 9030 | fill_span(&mut self.left, mi_r, luma_h, states); |
| 9031 | fill_span(&mut self.left, mi_r + luma_h, chroma_h - luma_h, uv_only); |
| 9032 | fill_span( |
| 9033 | &mut self.left, |
| 9034 | mi_r + chroma_h, |
| 9035 | h_mi - chroma_h, |
| 9036 | [Neighbour::default(); 3], |
| 9037 | ); |
| 9038 | let luma_w = w_mi.min(bound_w[0].saturating_sub(mi_c)); |
| 9039 | let chroma_w = w_mi.min(bound_w[1].saturating_sub(mi_c)); |
| 9040 | let uv_only_w = [Neighbour::default(), states[1], states[2]]; |
| 9041 | fill_span(&mut self.above, mi_c, luma_w, states); |
| 9042 | fill_span(&mut self.above, mi_c + luma_w, chroma_w - luma_w, uv_only_w); |
| 9043 | fill_span( |
| 9044 | &mut self.above, |
| 9045 | mi_c + chroma_w, |
| 9046 | w_mi - chroma_w, |
| 9047 | [Neighbour::default(); 3], |
| 9048 | ); |
| 9049 | } |
| 9050 | |
| 9051 | /// [`Self::record_mi`]'s plane-0 half only, for one luma transform unit |
| 9052 | /// inside a coded block whose luma transform is smaller than its own |
| 9053 | /// side (`TxMode::Select`): the real per-transform-unit coefficient |
| 9054 | /// context (spec `AboveLevelContext`/`LeftLevelContext`), unlike the |
| 9055 | /// coarse per-block `tx_grid`/`fill_lf_grid` state |
| 9056 | /// [`Self::record_split_luma`] still writes once for the whole block. |
| 9057 | /// Leaves `above_side_mi`/`left_side_mi` and the chroma planes alone -- |
| 9058 | /// the caller's own [`Self::record_split_luma`] call sets those once, |
| 9059 | /// at the block's own true side, not this TU's. |
| 9060 | fn record_mi_luma(&mut self, at_mi: (usize, usize), tx_px: usize, grid: &[i32]) { |
| 9061 | self.record_mi_luma_rect(at_mi, tx_px, tx_px, grid); |
| 9062 | } |
| 9063 | |
| 9064 | /// [`Self::record_mi_luma`] for ONE CHROMA transform unit of a block whose |
| 9065 | /// uv plane block is several units (lane-sb128b r2: a 128x128 block's |
| 9066 | /// 64x64 uv plane block is four TX_32X32 units, `av1_get_max_uv_txsize`). |
| 9067 | /// `w_px`/`h_px` are the unit's extent in LUMA pixels -- the chroma |
| 9068 | /// coefficient-context arrays are indexed by luma mi, the convention |
| 9069 | /// [`Self::record_split_luma_rect_mi`] already writes them in, frame-edge |
| 9070 | /// clamp included. |
| 9071 | fn record_mi_chroma( |
| 9072 | &mut self, |
| 9073 | (mi_r, mi_c): (usize, usize), |
| 9074 | w_px: usize, |
| 9075 | h_px: usize, |
| 9076 | plane: usize, |
| 9077 | grid: &[i32], |
| 9078 | ) { |
| 9079 | let round_up_even = |n: usize| n.div_ceil(2) * 2; |
| 9080 | let (bound_h, bound_w) = (round_up_even(self.mi_rows), round_up_even(self.mi_cols)); |
| 9081 | let state = neighbour_state(grid); |
| 9082 | for cell in 0..(h_px / MI).min(bound_h.saturating_sub(mi_r)) { |
| 9083 | self.left[mi_r + cell][plane] = state; |
| 9084 | } |
| 9085 | for cell in 0..(w_px / MI).min(bound_w.saturating_sub(mi_c)) { |
| 9086 | self.above[mi_c + cell][plane] = state; |
| 9087 | } |
| 9088 | } |
| 9089 | |
| 9090 | /// [`Self::record_mi_luma`] for a RECTANGULAR transform unit (lane-rectsplitx |
| 9091 | /// r1): the above context spans the unit's width, the left context its |
| 9092 | /// height, which for a square unit is the same loop it always was. |
| 9093 | fn record_mi_luma_rect( |
| 9094 | &mut self, |
| 9095 | (mi_r, mi_c): (usize, usize), |
| 9096 | w_px: usize, |
| 9097 | h_px: usize, |
| 9098 | grid: &[i32], |
| 9099 | ) { |
| 9100 | let state = neighbour_state(grid); |
| 9101 | if crate::envflags::env_flag!("EC_ECPUB") { |
| 9102 | eprintln!("EC_ECPUB tu mi=({mi_r},{mi_c}) wh=({w_px},{h_px}) lvl={}", state.level); |
| 9103 | } |
| 9104 | for cell in 0..h_px / MI { |
| 9105 | if mi_r + cell < self.mi_rows { |
| 9106 | self.left[mi_r + cell][0] = state; |
| 9107 | } |
| 9108 | } |
| 9109 | for cell in 0..w_px / MI { |
| 9110 | if mi_c + cell < self.mi_cols { |
| 9111 | self.above[mi_c + cell][0] = state; |
| 9112 | } |
| 9113 | } |
| 9114 | } |
| 9115 | |
| 9116 | /// [`Self::record`]'s mode/side and chroma-context bookkeeping, for a |
| 9117 | /// block whose luma was decoded as several transform units through |
| 9118 | /// [`Self::record_mi_luma`] rather than [`Self::record`]'s own single |
| 9119 | /// whole-block luma write -- everything [`Self::record`] does except |
| 9120 | /// plane 0, which is already correct per-TU by the time this runs. |
| 9121 | fn record_split_luma( |
| 9122 | &mut self, |
| 9123 | at: (usize, usize), |
| 9124 | side: usize, |
| 9125 | mode: usize, |
| 9126 | uv_mode: usize, |
| 9127 | chroma_grids: [&[i32]; 2], |
| 9128 | ) { |
| 9129 | self.record_split_luma_rect(at, side, side, mode, uv_mode, chroma_grids); |
| 9130 | } |
| 9131 | |
| 9132 | /// [`Self::record_split_luma`] with independent above (`w`)/left (`h`) |
| 9133 | /// extents in pixels (lane-rectsplit r1), the same relationship |
| 9134 | /// [`Self::record_rect`] has to [`Self::record`]: a HORZ/VERT strip whose |
| 9135 | /// luma transform split into several square units needs exactly this |
| 9136 | /// bookkeeping, at its own true rectangular extents. |
| 9137 | fn record_split_luma_rect( |
| 9138 | &mut self, |
| 9139 | at: (usize, usize), |
| 9140 | w: usize, |
| 9141 | h: usize, |
| 9142 | mode: usize, |
| 9143 | uv_mode: usize, |
| 9144 | chroma_grids: [&[i32]; 2], |
| 9145 | ) { |
| 9146 | let (r, c) = at; |
| 9147 | self.record_split_luma_rect_mi( |
| 9148 | (r * (SUB / MI), c * (SUB / MI)), |
| 9149 | w, |
| 9150 | h, |
| 9151 | mode, |
| 9152 | uv_mode, |
| 9153 | chroma_grids, |
| 9154 | ); |
| 9155 | } |
| 9156 | |
| 9157 | /// [`Self::record_split_luma_rect`] addressed in mi units (lane-rectsplitx |
| 9158 | /// r1) -- a strip 8 px tall starts mid-[`SUB`]-cell, which the coarse |
| 9159 | /// `(r, c)` form cannot name. The coarse mode/side arrays are stamped for |
| 9160 | /// every cell the strip TOUCHES (`div_ceil`), last writer wins, the |
| 9161 | /// convention [`decode_block_rect4`] already established for 1:4 strips. |
| 9162 | fn record_split_luma_rect_mi( |
| 9163 | &mut self, |
| 9164 | at_mi: (usize, usize), |
| 9165 | w: usize, |
| 9166 | h: usize, |
| 9167 | mode: usize, |
| 9168 | uv_mode: usize, |
| 9169 | chroma_grids: [&[i32]; 2], |
| 9170 | ) { |
| 9171 | let (mi_r, mi_c) = at_mi; |
| 9172 | let (r, c) = (mi_r / (SUB / MI), mi_c / (SUB / MI)); |
| 9173 | for cell in 0..w.div_ceil(SUB) { |
| 9174 | if c + cell >= self.above_mode.len() { |
| 9175 | break; |
| 9176 | } |
| 9177 | self.above_mode[c + cell] = mode; |
| 9178 | self.above_uv_mode[c + cell] = uv_mode; |
| 9179 | self.above_side[c + cell] = w; |
| 9180 | } |
| 9181 | for cell in 0..h.div_ceil(SUB) { |
| 9182 | if r + cell >= self.left_mode.len() { |
| 9183 | break; |
| 9184 | } |
| 9185 | self.left_mode[r + cell] = mode; |
| 9186 | self.left_uv_mode[r + cell] = uv_mode; |
| 9187 | self.left_side[r + cell] = h; |
| 9188 | } |
| 9189 | |
| 9190 | let (w_mi, h_mi) = (w / MI, h / MI); |
| 9191 | self.record_mode_mi(mi_r, mi_c, w_mi, h_mi, mode); |
| 9192 | self.record_uv_mode_mi(mi_r, mi_c, w_mi, h_mi, uv_mode); |
| 9193 | fill_span(&mut self.left_side_mi, mi_r, h_mi, h as u8); |
| 9194 | fill_span(&mut self.above_side_mi, mi_c, w_mi, w as u8); |
| 9195 | let round_up_even = |n: usize| n.div_ceil(2) * 2; |
| 9196 | let (bound_h, bound_w) = (round_up_even(self.mi_rows), round_up_even(self.mi_cols)); |
| 9197 | for (plane_idx, grid) in chroma_grids.into_iter().enumerate() { |
| 9198 | let plane = plane_idx + 1; |
| 9199 | let state = neighbour_state(grid); |
| 9200 | for cell in 0..h_mi { |
| 9201 | if cell < h_mi.min(bound_h.saturating_sub(mi_r)) { |
| 9202 | self.left[mi_r + cell][plane] = state; |
| 9203 | } |
| 9204 | } |
| 9205 | for cell in 0..w_mi { |
| 9206 | if cell < w_mi.min(bound_w.saturating_sub(mi_c)) { |
| 9207 | self.above[mi_c + cell][plane] = state; |
| 9208 | } |
| 9209 | } |
| 9210 | } |
| 9211 | } |
| 9212 | } |
| 9213 | |
| 9214 | /// Reads one coded block's skip flag, luma mode, angle delta (if the mode |
| 9215 | /// carries one) and chroma mode, mirroring [`crate::tile`]'s `write_intra_mode` |
| 9216 | /// exactly — including its skip context, which the real key-frame writer |
| 9217 | /// always writes at a fixed context of zero (it never tracks a neighbour-based |
| 9218 | /// one for intra blocks; that is the inter-frame writer's rule instead). |
| 9219 | /// Refuses a nonzero angle delta: this decoder reconstructs the thirteen |
| 9220 | /// luma modes at their base angle only. Chroma is DC-only or, where CFL is |
| 9221 | /// offered (`cfl`, `is_cfl_allowed`, spec 5.11.5), chroma-from-luma -- the |
| 9222 | /// third element carries that mode's `(alpha_q3_u, alpha_q3_v)`, `None` for |
| 9223 | /// plain DC chroma. |
| 9224 | /// spec 5.11.47 / libaom `av1_read_tx_type`'s `fimode_to_intradir`: which |
| 9225 | /// `intra_ext_tx_cdf` row a luma transform reads. A filter-intra block's |
| 9226 | /// `mode` is always `DC_PRED`, so the row comes from the FILTER-INTRA mode |
| 9227 | /// instead (the defect commit 1a89ed9 fixed at the square sites). |
| 9228 | pub(crate) fn fi_tx_row(mode: usize, filter_intra: Option<usize>) -> usize { |
| 9229 | const FIMODE_TO_INTRADIR: [usize; 5] = [DC_PRED, V_PRED, H_PRED, D157_PRED, DC_PRED]; |
| 9230 | filter_intra.map_or(mode, |fi| FIMODE_TO_INTRADIR[fi]) |
| 9231 | } |
| 9232 | |
| 9233 | /// A block-size class index into [`cdf::FILTER_INTRA`]/`Cdfs::filter_intra`, |
| 9234 | /// or `None` when `side` is past `av1_filter_intra_allowed_bsize`'s <=32 |
| 9235 | /// bound (spec `filter_intra_mode_info` never reads a symbol there). |
| 9236 | pub(crate) fn filter_intra_size_class(side: usize) -> Option<usize> { |
| 9237 | match side { |
| 9238 | 4 => Some(0), |
| 9239 | 8 => Some(1), |
| 9240 | 16 => Some(2), |
| 9241 | 32 => Some(3), |
| 9242 | _ => None, |
| 9243 | } |
| 9244 | } |
| 9245 | |
| 9246 | /// [`filter_intra_size_class`] for a true `bw`x`bh` rect strip |
| 9247 | /// (lane-intradisp r1): `av1_filter_intra_allowed_bsize` permits any bsize |
| 9248 | /// with both dims <= 32, which includes `BLOCK_32X16`/`BLOCK_16X32` -- their |
| 9249 | /// own distinct `cdf::FILTER_INTRA` rows, classes 4/5. |
| 9250 | fn filter_intra_size_class_rect(bw: usize, bh: usize) -> Option<usize> { |
| 9251 | match (bw, bh) { |
| 9252 | (32, 16) => Some(4), |
| 9253 | (16, 32) => Some(5), |
| 9254 | // lane-rectx r3: `av1_filter_intra_allowed_bsize` is "both sides <= |
| 9255 | // 32", so a 16x8/8x16 strip reads the flag too -- returning `None` |
| 9256 | // here dropped that symbol on every DC_PRED strip of that size and |
| 9257 | // desynced the tile (caught against an aomdec EC_TRACE_COEFF ladder: |
| 9258 | // identical mode/uv_mode values, range 34808 vs the oracle's 40668). |
| 9259 | (16, 8) => Some(6), |
| 9260 | (8, 16) => Some(7), |
| 9261 | // lane-fi8 r1: the sub-8 rect leaves are allowed too (both sides <= |
| 9262 | // 32), and `decode_leaf_rect8` codes them -- rows 1/2 of |
| 9263 | // `default_filter_intra_cdfs`, NOT BLOCK_4X4's row 0. |
| 9264 | (4, 8) => Some(8), |
| 9265 | (8, 4) => Some(9), |
| 9266 | // lane-fi32x8 r1: same bound, the 32x32-level 1:4 pair |
| 9267 | // (`decode_block_rect4`'s 32x8/8x32 strips). `None` here read no |
| 9268 | // `use_filter_intra` symbol on those strips although aomenc writes |
| 9269 | // one for every DC_PRED block of that shape with |
| 9270 | // `--enable-filter-intra=1` (its default), silently desyncing the |
| 9271 | // tile (class symbol-consumption-gap). |
| 9272 | (32, 8) => Some(10), |
| 9273 | (8, 32) => Some(11), |
| 9274 | // lane-r14: the 16x16-level 1:4 pair (`decode_rect4_16`'s 16x4/4x16 |
| 9275 | // strips), rows 17/16 of `default_filter_intra_cdfs`. |
| 9276 | (16, 4) => Some(12), |
| 9277 | (4, 16) => Some(13), |
| 9278 | _ if bw == bh => filter_intra_size_class(bw), |
| 9279 | _ => None, |
| 9280 | } |
| 9281 | } |
| 9282 | |
| 9283 | /// `av1_allow_palette`'s size bound (blockd.h: `block_size_wide/high <= |
| 9284 | /// MAX_PALETTE_BLOCK_WIDTH/HEIGHT` (64) and `sb_type >= BLOCK_8X8`) plus |
| 9285 | /// `av1_get_palette_bsize_ctx` (pred_common.h: `num_pels_log2_lookup[bsize] |
| 9286 | /// - num_pels_log2_lookup[BLOCK_8X8]`, i.e. `log2(bw*bh) - 6`) -- `None` past |
| 9287 | /// the bound (never gates a palette read), `Some(bsize_ctx)` otherwise. |
| 9288 | pub(crate) fn palette_bsize_ctx(side: usize) -> Option<usize> { |
| 9289 | palette_bsize_ctx_wh(side, side) |
| 9290 | } |
| 9291 | |
| 9292 | /// [`palette_bsize_ctx`], generalised to a true `bw`x`bh` rect strip |
| 9293 | /// (lane-palette2 r4) -- the same `log2(bw*bh) - 6` formula libaom's |
| 9294 | /// `num_pels_log2_lookup[bsize] - num_pels_log2_lookup[BLOCK_8X8]` computes |
| 9295 | /// for ANY bsize, not just the square ones a hand-picked table happened to |
| 9296 | /// enumerate before ([[cdf-row-held-constant]]: a table narrowed to a pinned |
| 9297 | /// row breaks the moment its indexing field moves off the diagonal, and a |
| 9298 | /// 64x32/32x64 superblock-level strip is exactly such a move). Every AV1 |
| 9299 | /// block dimension is a power of two, so `bw*bh` always is too and |
| 9300 | /// `trailing_zeros` is `log2` exactly. `None` past `av1_allow_palette`'s own |
| 9301 | /// bound (`block_size_wide/high <= 64`, `sb_type >= BLOCK_8X8`). |
| 9302 | fn palette_bsize_ctx_wh(bw: usize, bh: usize) -> Option<usize> { |
| 9303 | // lane-r14: `av1_allow_palette`'s bound is `sb_type >= BLOCK_8X8` by the |
| 9304 | // BLOCK_SIZES_ALL enum INDEX, which the 1:4 shapes BLOCK_4X16 (16) and |
| 9305 | // BLOCK_16X4 (17) clear -- a 64-pixel block, so `av1_get_palette_bsize_ctx` |
| 9306 | // (`num_pels_log2 - num_pels_log2[BLOCK_8X8]`) is 0 for them, exactly as |
| 9307 | // for an 8x8. Reading the dimension bound as "both sides >= 8" dropped |
| 9308 | // `palette_y_mode` on every 16x4/4x16 strip of a screen-content frame. |
| 9309 | if bw > 64 || bh > 64 || bw * bh < 64 { |
| 9310 | return None; |
| 9311 | } |
| 9312 | Some(((bw * bh) as u32).trailing_zeros() as usize - 6) |
| 9313 | } |
| 9314 | |
| 9315 | /// A just-decoded palette-Y block's own size, base colours (only the first |
| 9316 | /// `size` entries are real), and per-pixel colour-index map (`side*side`, |
| 9317 | /// row-major, each entry `< size`). |
| 9318 | struct PaletteY { |
| 9319 | size: usize, |
| 9320 | colors: [u16; 8], |
| 9321 | map: Vec<u8>, |
| 9322 | } |
| 9323 | |
| 9324 | /// As [`PaletteY`], for a just-decoded chroma palette block: one shared |
| 9325 | /// per-pixel colour-index map (spec `av1_visit_palette`'s plane-1 pass is |
| 9326 | /// shared between U and V, both co-located after 4:2:0 subsampling) plus |
| 9327 | /// each plane's own base colours. |
| 9328 | struct PaletteUv { |
| 9329 | size: usize, |
| 9330 | u_colors: [u16; 8], |
| 9331 | v_colors: [u16; 8], |
| 9332 | map: Vec<u8>, |
| 9333 | } |
| 9334 | |
| 9335 | /// `aom_ceil_log2` (aom_dsp/bitwriter_buffer.h-adjacent helper, used both by |
| 9336 | /// `read_palette_colors_y`'s shrinking `bits` and `av1_read_uniform`'s |
| 9337 | /// `get_unsigned_bits`, which is the same formula): smallest `n` with |
| 9338 | /// `2^n >= x`, `0` for `x <= 1`. |
| 9339 | pub(crate) fn ceil_log2(x: u32) -> u32 { |
| 9340 | if x < 2 { 0 } else { 32 - (x - 1).leading_zeros() } |
| 9341 | } |
| 9342 | |
| 9343 | /// `av1_read_uniform` (decoder.h:425): a uniform `0..n` value coded as |
| 9344 | /// `l-1` raw bits plus a conditional extra bit, `l = get_unsigned_bits(n)`. |
| 9345 | fn read_uniform(dec: &mut SymbolDecoder, n: usize) -> usize { |
| 9346 | let l = ceil_log2(n as u32); |
| 9347 | let m = (1usize << l) - n; |
| 9348 | let v = dec.literal(l - 1) as usize; |
| 9349 | if v < m { |
| 9350 | v |
| 9351 | } else { |
| 9352 | (v << 1) - m + dec.literal(1) as usize |
| 9353 | } |
| 9354 | } |
| 9355 | |
| 9356 | /// `merge_colors` (decodemv.c:462): interleaves the ascending `cached` |
| 9357 | /// colours (already-known, from the above/left neighbours) with the |
| 9358 | /// ascending `transmitted` ones (delta-coded off the wire) back into one |
| 9359 | /// ascending list of `cached.len() + transmitted.len()` colours -- a tied |
| 9360 | /// value picks the cached copy first, matching the C `<=`. |
| 9361 | fn merge_colors(transmitted: &[u16], cached: &[u16]) -> Vec<u16> { |
| 9362 | let mut out = Vec::with_capacity(transmitted.len() + cached.len()); |
| 9363 | let (mut ci, mut ti) = (0, 0); |
| 9364 | while ci < cached.len() && ti < transmitted.len() { |
| 9365 | if cached[ci] <= transmitted[ti] { |
| 9366 | out.push(cached[ci]); |
| 9367 | ci += 1; |
| 9368 | } else { |
| 9369 | out.push(transmitted[ti]); |
| 9370 | ti += 1; |
| 9371 | } |
| 9372 | } |
| 9373 | out.extend_from_slice(&cached[ci..]); |
| 9374 | out.extend_from_slice(&transmitted[ti..]); |
| 9375 | out |
| 9376 | } |
| 9377 | |
| 9378 | /// `read_palette_colors_y` (decodemv.c:478), at the sequence's real |
| 9379 | /// `bit_depth` -- lane-kf900 r3: every raw literal here (the first colour, |
| 9380 | /// each delta's width, the `range` bound and the clamp) is sized by |
| 9381 | /// `bit_depth`, and hardcoding 8 read 2 bits too few per colour on every |
| 9382 | /// 10-bit palette block. Raw literals are EQUIPROBABLE, and at the ranges |
| 9383 | /// this stream reaches (e.g. 62728) an equiprobable read is a FIXED POINT of |
| 9384 | /// the msac range, so a cross-decoder range ladder stays bit-identical |
| 9385 | /// through the whole miscount -- only the decoded VALUE differs (class |
| 9386 | /// `compare-range-not-tell`'s blind spot). (this |
| 9387 | /// decoder is 8-bit only): accepts up to `n` colours straight from `cache` |
| 9388 | /// (one flag bit each, in cache order), then delta-codes the remainder |
| 9389 | /// (first value raw, each following delta `>= 1` off a shrinking bit width), |
| 9390 | /// and merges the two ascending lists back together. |
| 9391 | fn read_palette_colors_y(dec: &mut SymbolDecoder, n: usize, cache: &[u16], fctx: &crate::decode::FrameCtx) -> [u16; 8] { |
| 9392 | let mut cached = Vec::with_capacity(n); |
| 9393 | for &c in cache { |
| 9394 | if cached.len() >= n { |
| 9395 | break; |
| 9396 | } |
| 9397 | if dec.literal(1) == 1 { |
| 9398 | cached.push(c); |
| 9399 | } |
| 9400 | } |
| 9401 | let bd = u32::from(bit_depth(fctx)); |
| 9402 | let max_val = (1i32 << bd) - 1; |
| 9403 | let mut colors = [0u16; 8]; |
| 9404 | let n_cached = cached.len(); |
| 9405 | if n_cached < n { |
| 9406 | let mut transmitted = Vec::with_capacity(n - n_cached); |
| 9407 | let first = dec.literal(bd) as u16; |
| 9408 | transmitted.push(first); |
| 9409 | if n_cached + 1 < n { |
| 9410 | let min_bits = bd - 3; |
| 9411 | let mut bits = min_bits + dec.literal(2); |
| 9412 | let mut range = (1i32 << bd) - i32::from(first) - 1; |
| 9413 | for _ in (n_cached + 1)..n { |
| 9414 | let delta = dec.literal(bits) as i32 + 1; |
| 9415 | let prev = *transmitted.last().unwrap(); |
| 9416 | let val = (i32::from(prev) + delta).clamp(0, max_val) as u16; |
| 9417 | range -= i32::from(val) - i32::from(prev); |
| 9418 | bits = bits.min(ceil_log2(range.max(0) as u32)); |
| 9419 | transmitted.push(val); |
| 9420 | } |
| 9421 | } |
| 9422 | let merged = merge_colors(&transmitted, &cached); |
| 9423 | colors[..n].copy_from_slice(&merged); |
| 9424 | } else { |
| 9425 | colors[..n].copy_from_slice(&cached[..n]); |
| 9426 | } |
| 9427 | colors |
| 9428 | } |
| 9429 | |
| 9430 | /// `read_palette_colors_uv` (decodemv.c:509), at the sequence's real |
| 9431 | /// `bit_depth` (see [`read_palette_colors_y`]): U reads |
| 9432 | /// exactly [`read_palette_colors_y`]'s cache/delta scheme (against the U-only |
| 9433 | /// cache, `range` starting at `255 - prev` with no `-1`, and each raw `delta` |
| 9434 | /// unbiased by `+1` -- both differences from Y's own reader, matching the C |
| 9435 | /// side by side). V never uses a cache: a leading bit picks either `n` raw |
| 9436 | /// literals, or a first raw value plus `n-1` signed deltas that wrap mod 256 |
| 9437 | /// rather than clamp. |
| 9438 | fn read_palette_colors_uv(dec: &mut SymbolDecoder, n: usize, cache: &[u16], fctx: &crate::decode::FrameCtx) -> ([u16; 8], [u16; 8]) { |
| 9439 | let mut cached = Vec::with_capacity(n); |
| 9440 | for &c in cache { |
| 9441 | if cached.len() >= n { |
| 9442 | break; |
| 9443 | } |
| 9444 | if dec.literal(1) == 1 { |
| 9445 | cached.push(c); |
| 9446 | } |
| 9447 | } |
| 9448 | let bd = u32::from(bit_depth(fctx)); |
| 9449 | let max_val = 1i32 << bd; |
| 9450 | let mut u_colors = [0u16; 8]; |
| 9451 | let n_cached = cached.len(); |
| 9452 | if n_cached < n { |
| 9453 | let mut transmitted = Vec::with_capacity(n - n_cached); |
| 9454 | let first = dec.literal(bd) as u16; |
| 9455 | transmitted.push(first); |
| 9456 | if n_cached + 1 < n { |
| 9457 | let min_bits = bd - 3; |
| 9458 | let mut bits = min_bits + dec.literal(2); |
| 9459 | let mut range = max_val - i32::from(first); |
| 9460 | for _ in (n_cached + 1)..n { |
| 9461 | let delta = dec.literal(bits) as i32; |
| 9462 | let prev = *transmitted.last().unwrap(); |
| 9463 | let val = (i32::from(prev) + delta).clamp(0, max_val - 1) as u16; |
| 9464 | range -= i32::from(val) - i32::from(prev); |
| 9465 | bits = bits.min(ceil_log2(range.max(0) as u32)); |
| 9466 | transmitted.push(val); |
| 9467 | } |
| 9468 | } |
| 9469 | let merged = merge_colors(&transmitted, &cached); |
| 9470 | u_colors[..n].copy_from_slice(&merged); |
| 9471 | } else { |
| 9472 | u_colors[..n].copy_from_slice(&cached[..n]); |
| 9473 | } |
| 9474 | let mut v_colors = [0u16; 8]; |
| 9475 | if dec.literal(1) == 1 { |
| 9476 | let min_bits_v = bd - 4; |
| 9477 | let bits = min_bits_v + dec.literal(2); |
| 9478 | v_colors[0] = dec.literal(bd) as u16; |
| 9479 | for i in 1..n { |
| 9480 | let mut delta = dec.literal(bits) as i32; |
| 9481 | if delta != 0 && dec.literal(1) == 1 { |
| 9482 | delta = -delta; |
| 9483 | } |
| 9484 | let mut val = i32::from(v_colors[i - 1]) + delta; |
| 9485 | if val < 0 { |
| 9486 | val += max_val; |
| 9487 | } |
| 9488 | if val >= max_val { |
| 9489 | val -= max_val; |
| 9490 | } |
| 9491 | v_colors[i] = val as u16; |
| 9492 | } |
| 9493 | } else { |
| 9494 | for i in 0..n { |
| 9495 | v_colors[i] = dec.literal(bd) as u16; |
| 9496 | } |
| 9497 | } |
| 9498 | (u_colors, v_colors) |
| 9499 | } |
| 9500 | |
| 9501 | /// `av1_palette_color_index_context_lookup` (entropymode.c:889): maps a |
| 9502 | /// `NUM_PALETTE_NEIGHBORS`-weighted score hash (`0..=8`) to the real |
| 9503 | /// `0..=4` context, `-1` for hashes no real neighbour configuration produces. |
| 9504 | const PALETTE_COLOR_INDEX_CONTEXT_LOOKUP: [i32; 9] = [-1, -1, 0, -1, -1, 4, 3, 2, 1]; |
| 9505 | |
| 9506 | /// `av1_get_palette_color_index_context` (entropymode.c:893): the colour-index |
| 9507 | /// map's own per-pixel context and colour-order permutation, from the |
| 9508 | /// left/up-left/up already-decoded neighbours (weights `[2, 1, 2]`, top-3 |
| 9509 | /// selection sort). `map`'s stride is `side` (this decoder never crops a |
| 9510 | /// palette block's map to a narrower plane edge, [`decode_color_index_map`]'s |
| 9511 | /// own doc). Returns `(ctx, color_order)`; `color_order[symbol]` is the |
| 9512 | /// actual colour index to store. |
| 9513 | pub(crate) fn palette_color_index_context( |
| 9514 | map: &[u8], |
| 9515 | side: usize, |
| 9516 | row: usize, |
| 9517 | col: usize, |
| 9518 | n: usize, |
| 9519 | ) -> (usize, [u8; 8]) { |
| 9520 | let left = if col >= 1 { Some(map[row * side + col - 1]) } else { None }; |
| 9521 | let up_left = if col >= 1 && row >= 1 { |
| 9522 | Some(map[(row - 1) * side + col - 1]) |
| 9523 | } else { |
| 9524 | None |
| 9525 | }; |
| 9526 | let up = if row >= 1 { Some(map[(row - 1) * side + col]) } else { None }; |
| 9527 | let mut scores = [0i32; 8]; |
| 9528 | for (neighbour, weight) in [(left, 2), (up_left, 1), (up, 2)] { |
| 9529 | if let Some(v) = neighbour { |
| 9530 | scores[v as usize] += weight; |
| 9531 | } |
| 9532 | } |
| 9533 | let mut color_order = [0u8; 8]; |
| 9534 | for i in 0..8 { |
| 9535 | color_order[i] = i as u8; |
| 9536 | } |
| 9537 | for i in 0..3 { |
| 9538 | let mut max = scores[i]; |
| 9539 | let mut max_idx = i; |
| 9540 | for j in (i + 1)..n { |
| 9541 | if scores[j] > max { |
| 9542 | max = scores[j]; |
| 9543 | max_idx = j; |
| 9544 | } |
| 9545 | } |
| 9546 | if max_idx != i { |
| 9547 | let max_score = scores[max_idx]; |
| 9548 | let max_color = color_order[max_idx]; |
| 9549 | for k in (i + 1..=max_idx).rev() { |
| 9550 | scores[k] = scores[k - 1]; |
| 9551 | color_order[k] = color_order[k - 1]; |
| 9552 | } |
| 9553 | scores[i] = max_score; |
| 9554 | color_order[i] = max_color; |
| 9555 | } |
| 9556 | } |
| 9557 | let hash = scores[0] + scores[1] * 2 + scores[2] * 2; |
| 9558 | let ctx = PALETTE_COLOR_INDEX_CONTEXT_LOOKUP[hash as usize]; |
| 9559 | (ctx as usize, color_order) |
| 9560 | } |
| 9561 | |
| 9562 | /// spec 5.11.50 `palette_tokens` / `av1_get_block_dimensions` (blockd.h:1512): |
| 9563 | /// the ON-SCREEN part of a block at `(mi_r, mi_c)`, in luma pixels. A block |
| 9564 | /// cut by the frame's bottom or right mi edge codes its colour-index map only |
| 9565 | /// over this part and replicates it outward -- reading the full `bw x bh` grid |
| 9566 | /// consumed symbols libaom never wrote and desynced the rest of the tile |
| 9567 | /// (lane-svtinter; class map-read-past-frame-edge). |
| 9568 | fn palette_onscreen( |
| 9569 | mi_r: usize, |
| 9570 | mi_c: usize, |
| 9571 | bw: usize, |
| 9572 | bh: usize, |
| 9573 | fctx: &crate::decode::FrameCtx, |
| 9574 | ) -> (usize, usize) { |
| 9575 | // lane-palw: the ENCODER's own in-process reconstruct runs on a |
| 9576 | // `FrameCtx::for_encoder`, which never sees `set_segmentation` -- both |
| 9577 | // frame encoders publish the header's mi dims onto it themselves |
| 9578 | // (`encode.rs`, beside their `true_width`/`true_height`) so that this |
| 9579 | // clamp and `tile::write_color_index_map`'s now agree there too. |
| 9580 | let (mi_rows, mi_cols) = fctx.seg_mi_dims.with(|c| c.get()); |
| 9581 | palette_onscreen_dims(mi_r, mi_c, bw, bh, (mi_rows, mi_cols)) |
| 9582 | } |
| 9583 | |
| 9584 | /// [`palette_onscreen`] off explicit mi dims, which is how the WRITER reaches |
| 9585 | /// it (`tile::write_palette_syntax`, armed per frame): the two sides must |
| 9586 | /// compute the same on-screen extent or the decoder reads a different number |
| 9587 | /// of symbols than the encoder wrote. |
| 9588 | pub(crate) fn palette_onscreen_dims( |
| 9589 | mi_r: usize, |
| 9590 | mi_c: usize, |
| 9591 | bw: usize, |
| 9592 | bh: usize, |
| 9593 | (mi_rows, mi_cols): (usize, usize), |
| 9594 | ) -> (usize, usize) { |
| 9595 | ( |
| 9596 | bw.min(mi_cols.saturating_sub(mi_c) * MI), |
| 9597 | bh.min(mi_rows.saturating_sub(mi_r) * MI), |
| 9598 | ) |
| 9599 | } |
| 9600 | |
| 9601 | /// [`palette_onscreen`] carried into the chroma plane: `>> subsampling` plus |
| 9602 | /// the SAME sub-8 bump the caller's own `(bw >> ss).max(4)` applies to the |
| 9603 | /// block dimensions (`is_chroma_sub8_x/y`, blockd.h:1533) -- both must move |
| 9604 | /// together or a cut 8-wide block reads its chroma map at the wrong width. |
| 9605 | /// lane-av1-444: the shifts are the sequence's own subsampling (a 4:4:4 |
| 9606 | /// intra-in-inter palette read its chroma map at the 4:2:0 extent and |
| 9607 | /// panicked feeding the 4:2:0-sized prediction to a full-size reconstruct). |
| 9608 | pub(crate) fn palette_onscreen_uv( |
| 9609 | luma: (usize, usize), |
| 9610 | on: (usize, usize), |
| 9611 | chroma: (usize, usize), |
| 9612 | ss: (usize, usize), |
| 9613 | ) -> (usize, usize) { |
| 9614 | ( |
| 9615 | (on.0 >> ss.0) + (chroma.0 - (luma.0 >> ss.0)), |
| 9616 | (on.1 >> ss.1) + (chroma.1 - (luma.1 >> ss.1)), |
| 9617 | ) |
| 9618 | } |
| 9619 | |
| 9620 | /// `decode_color_map_tokens` (detokenize.c:25): the palette-Y colour-index |
| 9621 | /// map's wavefront decode over a `side x side` grid, read over the on-screen |
| 9622 | /// `on_side x on_side` corner (see [`palette_onscreen`]). |
| 9623 | fn decode_color_index_map( |
| 9624 | dec: &mut SymbolDecoder, |
| 9625 | cdfs: &mut Cdfs, |
| 9626 | n: usize, |
| 9627 | side: usize, |
| 9628 | on: (usize, usize), |
| 9629 | uv: bool, |
| 9630 | ) -> Vec<u8> { |
| 9631 | decode_color_index_map_wh(dec, cdfs, n, side, side, on, uv) |
| 9632 | } |
| 9633 | |
| 9634 | /// [`decode_color_index_map`], generalised to a true `bw`x`bh` rect strip |
| 9635 | /// (lane-palette2 r4): `decode_color_map_tokens`'s own wavefront diagonal |
| 9636 | /// (`row = i - j`, `col = j`) walked over `cols = bw`, `rows = bh` |
| 9637 | /// independently rather than one shared `side` -- the square call above is |
| 9638 | /// just `bw == bh`. |
| 9639 | fn decode_color_index_map_wh( |
| 9640 | dec: &mut SymbolDecoder, |
| 9641 | cdfs: &mut Cdfs, |
| 9642 | n: usize, |
| 9643 | bw: usize, |
| 9644 | bh: usize, |
| 9645 | // `(onscreenWidth, onscreenHeight)` in this plane's own units. |
| 9646 | on: (usize, usize), |
| 9647 | uv: bool, |
| 9648 | ) -> Vec<u8> { |
| 9649 | let trace = crate::envflags::env_flag!("EC_AV1_TRACE"); |
| 9650 | let (on_bw, on_bh) = (on.0.clamp(1, bw), on.1.clamp(1, bh)); |
| 9651 | if on_bw < bw || on_bh < bh { |
| 9652 | hit!(PALETTE_ONSCREEN_CUT_HITS); |
| 9653 | } |
| 9654 | let mut map = vec![0u8; bw * bh]; |
| 9655 | if trace { |
| 9656 | let (rng, _) = dec.debug_state(); |
| 9657 | eprintln!("EC_PAL row=0 col=0 ctx=-1 n={n} rng={rng}"); |
| 9658 | } |
| 9659 | map[0] = read_uniform(dec, n) as u8; |
| 9660 | if trace { |
| 9661 | let (rng, _) = dec.debug_state(); |
| 9662 | eprintln!("EC_PAL_VAL row=0 col=0 color_idx={} rng={rng}", map[0]); |
| 9663 | } |
| 9664 | for i in 1..(on_bw + on_bh - 1) { |
| 9665 | let j_hi = i.min(on_bw - 1); |
| 9666 | let j_lo = i.saturating_sub(on_bh - 1); |
| 9667 | for j in (j_lo..=j_hi).rev() { |
| 9668 | let row = i - j; |
| 9669 | let col = j; |
| 9670 | let (ctx, color_order) = palette_color_index_context(&map, bw, row, col, n); |
| 9671 | if trace { |
| 9672 | let (rng, _) = dec.debug_state(); |
| 9673 | let row_cdf = if uv { |
| 9674 | &cdfs.palette_uv_color_index[n - 2][ctx] |
| 9675 | } else { |
| 9676 | &cdfs.palette_y_color_index[n - 2][ctx] |
| 9677 | }; |
| 9678 | eprintln!("EC_PAL row={row} col={col} ctx={ctx} n={n} cdf0={} cnt={} rng={rng}", row_cdf[0], row_cdf[n]); |
| 9679 | } |
| 9680 | let symbol = if uv { |
| 9681 | dec.symbol(&mut cdfs.palette_uv_color_index[n - 2][ctx][..=n]) |
| 9682 | } else { |
| 9683 | dec.symbol(&mut cdfs.palette_y_color_index[n - 2][ctx][..=n]) |
| 9684 | }; |
| 9685 | map[row * bw + col] = color_order[symbol]; |
| 9686 | if trace { |
| 9687 | let (rng, _) = dec.debug_state(); |
| 9688 | eprintln!("EC_PAL_VAL row={row} col={col} color_idx={symbol} rng={rng}"); |
| 9689 | } |
| 9690 | } |
| 9691 | } |
| 9692 | palette_replicate(&mut map, bw, bh, on); |
| 9693 | map |
| 9694 | } |
| 9695 | |
| 9696 | /// `decode_color_map_tokens`'s own two extension loops (detokenize.c:71): the |
| 9697 | /// last on-screen column then the last on-screen row are replicated over the |
| 9698 | /// off-screen remainder, so the prediction still covers the whole block. The |
| 9699 | /// ENCODER runs this over its searched map too (`encode.rs`'s palette arms), |
| 9700 | /// so the pixels it prices and reconstructs are the ones the on-screen |
| 9701 | /// symbols it writes really decode to. |
| 9702 | pub(crate) fn palette_replicate(map: &mut [u8], bw: usize, bh: usize, on: (usize, usize)) { |
| 9703 | let (on_bw, on_bh) = (on.0.clamp(1, bw), on.1.clamp(1, bh)); |
| 9704 | for row in 0..on_bh { |
| 9705 | let last = map[row * bw + on_bw - 1]; |
| 9706 | map[row * bw + on_bw..row * bw + bw].fill(last); |
| 9707 | } |
| 9708 | for row in on_bh..bh { |
| 9709 | map.copy_within((on_bh - 1) * bw..on_bh * bw, row * bw); |
| 9710 | } |
| 9711 | } |
| 9712 | |
| 9713 | /// [`read_intra_mode`] for a true `bw`x`bh` rect strip (lane-intradisp r1, |
| 9714 | /// palette wired lane-palette2 r4): identical except the `use_filter_intra` |
| 9715 | /// size class comes from [`filter_intra_size_class_rect`] instead of the |
| 9716 | /// square-only [`filter_intra_size_class`], and the palette read (when |
| 9717 | /// `allow_screen_content_tools`) uses [`palette_bsize_ctx_wh`]/ |
| 9718 | /// [`decode_color_index_map_wh`] instead of their square-only counterparts -- |
| 9719 | /// every other symbol this reads (`skip`, `y_mode`, `angle_delta`, `uv_mode`, |
| 9720 | /// `cfl_alphas`) is indexed by mode or neighbour state, never by block size. |
| 9721 | #[allow(clippy::too_many_arguments)] |
| 9722 | fn read_intra_mode_rect( |
| 9723 | dec: &mut SymbolDecoder, |
| 9724 | cdfs: &mut Cdfs, |
| 9725 | above_mode: usize, |
| 9726 | left_mode: usize, |
| 9727 | cfl: bool, |
| 9728 | // lane-r14: `is_chroma_reference` -- false only for a strip one mi tall |
| 9729 | // (or wide) that is NOT the odd one of its 4:2:0 pair, where libaom reads |
| 9730 | // no `uv_mode`/`cfl_alpha`/`angle_delta_uv`/`palette_uv` at all (the pair's |
| 9731 | // chroma is coded once, on the closing strip). Every other caller is a |
| 9732 | // block big enough to own its chroma and passes `true`. |
| 9733 | has_chroma: bool, |
| 9734 | bw: usize, |
| 9735 | bh: usize, |
| 9736 | enable_filter_intra: bool, |
| 9737 | skip_ctx: usize, |
| 9738 | allow_screen_content_tools: bool, |
| 9739 | // As [`read_intra_mode`]'s own `palette`/`palette_uv_cache` params: |
| 9740 | // `Some((palette_mode_ctx, color_cache))` reconstructs a real palette-Y |
| 9741 | // block here, `None` still reads (and refuses on use) the symbol at ctx 0. |
| 9742 | palette: Option<(usize, &[u16])>, |
| 9743 | palette_uv_cache: &[u16], |
| 9744 | mi_r: usize, |
| 9745 | mi_c: usize, fctx: &crate::decode::FrameCtx, |
| 9746 | ) -> Result<( |
| 9747 | bool, |
| 9748 | usize, |
| 9749 | i32, |
| 9750 | usize, |
| 9751 | i32, |
| 9752 | Option<(i32, i32)>, |
| 9753 | Option<usize>, |
| 9754 | Option<PaletteY>, |
| 9755 | Option<PaletteUv>, |
| 9756 | // lane-av1-intrabc: the block vector, `Some` exactly when this block's |
| 9757 | // `use_intrabc` symbol came out 1 -- libaom's `read_intrabc_info` returns |
| 9758 | // straight out of `read_intra_frame_mode_info` then, so every later field |
| 9759 | // of the tuple is at its default (mode/uv_mode DC, no angle, no palette, |
| 9760 | // no filter intra). A caller that can reconstruct intrabc at its shape |
| 9761 | // takes this and calls [`decode_intrabc_rect`]; one that cannot refuses by |
| 9762 | // name with the same string it always did. |
| 9763 | Option<(i32, i32)>, |
| 9764 | )> { |
| 9765 | // lane-av1mono: libaom guards every chroma symbol read with |
| 9766 | // `!cm->seq_params->monochrome && xd->is_chroma_ref` (`decodemv.c:933`), |
| 9767 | // so a monochrome block codes no `uv_mode`/`cfl`/`angle_delta_uv`/ |
| 9768 | // `palette_uv` even on a chroma-reference strip. Fold the plane count |
| 9769 | // into the same `has_chroma` this reader already gates on. |
| 9770 | let has_chroma = has_chroma && !mono(fctx); |
| 9771 | let ec_istep = crate::envflags::env_flag!("EC_TRACE_MODE_STEP"); |
| 9772 | if ec_istep { |
| 9773 | // Mirrors the oracle's own `EC_IMODE mi_row=.. mi_col=.. rng=..` line |
| 9774 | // (decodemv.c:818), printed at the same point: before the first |
| 9775 | // mode-info symbol of the block, so a cross-decoder range ladder can |
| 9776 | // be keyed on the BLOCK, not just on the symbol sequence. |
| 9777 | eprintln!("EC_IMODE mi_row={mi_r} mi_col={mi_c} fn=rect bw={bw} bh={bh} efi={} cls={:?} rng={}", enable_filter_intra as i32, filter_intra_size_class_rect(bw, bh), dec.debug_state().0); |
| 9778 | } |
| 9779 | macro_rules! istep { |
| 9780 | ($name:literal, $val:expr) => { |
| 9781 | if ec_istep { |
| 9782 | let (rng, _) = dec.debug_state(); |
| 9783 | eprintln!( |
| 9784 | "EC_ISTEP mi_row={mi_r} mi_col={mi_c} name={} val={} rng={rng}", |
| 9785 | $name, $val |
| 9786 | ); |
| 9787 | } |
| 9788 | }; |
| 9789 | } |
| 9790 | // lane-sbrect10 r1: libaom `is_cfl_allowed` (blockd.h) is |
| 9791 | // `block_size_wide <= 32 && block_size_high <= 32` -- enforce it HERE, at |
| 9792 | // the one point every rect caller routes through, so no call site can |
| 9793 | // offer the 14-symbol CFL alphabet on a 64-axis block (class |
| 9794 | // wrong-alphabet-same-value: same value, wrong interval, silent desync). |
| 9795 | let cfl = if cfl && bw.max(bh) > 32 { |
| 9796 | hit!(NOCFL_UV_MODE_HITS); |
| 9797 | false |
| 9798 | } else { |
| 9799 | cfl |
| 9800 | }; |
| 9801 | // lane-intra14 r1: on the INTER block path everything up to and including |
| 9802 | // `is_inter` was already read by [`decode_inter_block`], so this reader |
| 9803 | // starts at the mode syntax with the `skip` that caller read. |
| 9804 | let inter = fctx.intra_in_inter_mode.with(std::cell::Cell::get); |
| 9805 | let skip = if let Some((_, skip)) = inter { |
| 9806 | skip |
| 9807 | } else { |
| 9808 | // Same `intra_segment_id` placement as the square reader above. |
| 9809 | let (seg_w_mi, seg_h_mi) = (bw / 4, bh / 4); |
| 9810 | if seg_id_pre_skip(fctx) { |
| 9811 | intra_segment_id(dec, cdfs, mi_r, mi_c, seg_w_mi, seg_h_mi, false, fctx); |
| 9812 | } |
| 9813 | let skip = dec.symbol(&mut cdfs.skip[skip_ctx]) != 0; |
| 9814 | istep!("skip", skip as i32); |
| 9815 | if !seg_id_pre_skip(fctx) { |
| 9816 | intra_segment_id(dec, cdfs, mi_r, mi_c, seg_w_mi, seg_h_mi, skip, fctx); |
| 9817 | } |
| 9818 | maybe_read_cdef_idx(dec, mi_r, mi_c, (bw / MI, bh / MI), skip, fctx); |
| 9819 | istep!("cdef", 0); |
| 9820 | // A HORZ/VERT rect strip is never the whole superblock (`bw`/`bh` never |
| 9821 | // both 64 here -- see this fn's own doc), so `is_whole_sb` is always |
| 9822 | // `false`. |
| 9823 | maybe_read_delta_q(dec, cdfs, mi_r, mi_c, false, skip, fctx); |
| 9824 | maybe_read_delta_lf(dec, cdfs, mi_r, mi_c, false, skip, fctx); |
| 9825 | istep!("dq", 0); |
| 9826 | // `read_intrabc_info` (spec 5.11.13, libaom `decodemv.c:892`, right after |
| 9827 | // `skip`/`segment_id`/`cdef`/`delta_q` and before `mbmi->mode`): the |
| 9828 | // `use_intrabc` symbol is read for EVERY intra block of an `allow_intrabc` |
| 9829 | // frame -- `av1_allow_intrabc` is a frame-level test with no shape term, so |
| 9830 | // rect strips read it exactly like square leaves ([`read_intra_mode`], |
| 9831 | // [`read_intra_mode_sub8`]). Missing it here desynced the arithmetic |
| 9832 | // decoder at the FIRST symbol of the first rect block of every |
| 9833 | // `--tune-content=screen` key frame. Reconstruction of a real intrabc |
| 9834 | // block at this shape is out of scope, so the flag is consumed and |
| 9835 | // refused by name when it fires. |
| 9836 | if allow_intrabc_frame(fctx) { |
| 9837 | let use_intrabc = dec.symbol(&mut cdfs.intrabc) != 0; |
| 9838 | hit!(RECT_INTRABC_READS); |
| 9839 | istep!("intrabc", use_intrabc as i32); |
| 9840 | if use_intrabc { |
| 9841 | hit!(INTRABC_HITS); |
| 9842 | // libaom `read_intrabc_info`: the DV read, then an immediate |
| 9843 | // return out of the mode-info read -- no `y_mode`, no `uv_mode`, |
| 9844 | // no angle deltas, no palette, no filter intra. The caller owns |
| 9845 | // the reconstruction at its own shape. |
| 9846 | let dv = read_intrabc_dv(dec, cdfs, mi_r, mi_c, (bw / MI, bh / MI), fctx); |
| 9847 | return Ok(( |
| 9848 | skip, |
| 9849 | DC_PRED, |
| 9850 | 0, |
| 9851 | DC_PRED, |
| 9852 | 0, |
| 9853 | None, |
| 9854 | None, |
| 9855 | None, |
| 9856 | None, |
| 9857 | Some(dv), |
| 9858 | )); |
| 9859 | } |
| 9860 | } |
| 9861 | skip |
| 9862 | }; |
| 9863 | // libaom `read_intra_block_mode_info` (decodemv.c:1189) vs the key |
| 9864 | // frame's `read_intra_frame_mode_info` (decodemv.c:1065 `read_intra_mode` |
| 9865 | // off `kf_y_mode_cdf[above][left]`). |
| 9866 | let mode = if let Some((size_group, _)) = inter { |
| 9867 | dec.symbol(&mut cdfs.y_mode[size_group]) |
| 9868 | } else { |
| 9869 | let above_ctx = INTRA_MODE_CTX[above_mode]; |
| 9870 | let left_ctx = INTRA_MODE_CTX[left_mode]; |
| 9871 | dec.symbol(&mut cdfs.kf_y_mode[above_ctx][left_ctx]) |
| 9872 | }; |
| 9873 | istep!("mode", mode as i32); |
| 9874 | let angle_delta_y = if (V_PRED..=D67_PRED).contains(&mode) { |
| 9875 | read_angle_delta(dec, &mut cdfs.angle_delta[mode - V_PRED]) |
| 9876 | } else { |
| 9877 | 0 |
| 9878 | }; |
| 9879 | istep!("angle_y", angle_delta_y); |
| 9880 | // lane-sb128c r3 (class sweep): libaom `read_intra_mode_uv(.., is_cfl_allowed(xd), ..)` |
| 9881 | // and `is_cfl_allowed` (blockd.h) is `block_size_wide[bsize] <= 32 && |
| 9882 | // block_size_high[bsize] <= 32` on the LUMA bsize. Deciding the alphabet |
| 9883 | // here, from the block's own size, is the single point every caller routes |
| 9884 | // through -- a caller passing `true` for a >32 strip used to pick the |
| 9885 | // 14-symbol CFL CDF, same value but a different range (class |
| 9886 | // wrong-alphabet-same-value). |
| 9887 | let cfl = cfl && cfl_allowed_px(bw, bh, fctx); |
| 9888 | let uv_mode = if !has_chroma { |
| 9889 | DC_PRED |
| 9890 | } else if cfl { |
| 9891 | dec.symbol(&mut cdfs.uv_mode_cfl[mode]) |
| 9892 | } else { |
| 9893 | dec.symbol(&mut cdfs.uv_mode_no_cfl[mode]) |
| 9894 | }; |
| 9895 | istep!("uv_mode", uv_mode as i32); |
| 9896 | let alpha = if has_chroma && cfl && uv_mode == UV_CFL_PRED { |
| 9897 | Some(read_cfl_alphas(dec, cdfs)) |
| 9898 | } else { |
| 9899 | None |
| 9900 | }; |
| 9901 | if has_chroma && (9..=12).contains(&uv_mode) { |
| 9902 | hit!(SMOOTH_UV_HITS); |
| 9903 | } |
| 9904 | let angle_delta_uv = if has_chroma && (V_PRED..=D67_PRED).contains(&uv_mode) { |
| 9905 | hit!(DIRECTIONAL_UV_HITS); |
| 9906 | read_angle_delta(dec, &mut cdfs.angle_delta[uv_mode - V_PRED]) |
| 9907 | } else { |
| 9908 | 0 |
| 9909 | }; |
| 9910 | istep!("angle_uv", angle_delta_uv); |
| 9911 | if angle_delta_uv != 0 { |
| 9912 | hit!(UV_ANGLE_DELTA_HITS); |
| 9913 | } |
| 9914 | // `read_palette_mode_info` (spec 5.11.13), rect-generalised |
| 9915 | // (lane-palette2 r4): same bsize-ctx-gated read as [`read_intra_mode`], |
| 9916 | // just keyed by [`palette_bsize_ctx_wh`] instead of the square-only |
| 9917 | // [`palette_bsize_ctx`]. Order matches spec/libaom exactly: mode/size/ |
| 9918 | // colours only here, the colour-index maps deferred past `filter_intra` |
| 9919 | // (see [`read_intra_mode`]'s own comment on why -- `av1_visit_palette` |
| 9920 | // runs from the caller, after the whole mode-info read). |
| 9921 | let mut palette_y_pending: Option<(usize, [u16; 8])> = None; |
| 9922 | let mut palette_uv_pending: Option<(usize, [u16; 8], [u16; 8])> = None; |
| 9923 | if allow_screen_content_tools |
| 9924 | && let Some(bsize_ctx) = palette_bsize_ctx_wh(bw, bh) |
| 9925 | { |
| 9926 | // r6: this is the exact site the removed blanket refusal used to |
| 9927 | // guard -- reached for EVERY rect strip in a screen-content frame, |
| 9928 | // independent of whether the block ends up using a palette. |
| 9929 | hit!(RECT_SCREEN_CONTENT_HITS); |
| 9930 | let (mode_ctx, cache) = palette.unwrap_or((0, &[])); |
| 9931 | let use_palette_y = |
| 9932 | mode == DC_PRED && dec.symbol(&mut cdfs.palette_y_mode[bsize_ctx][mode_ctx]) != 0; |
| 9933 | if use_palette_y { |
| 9934 | let n = 2 + dec.symbol(&mut cdfs.palette_y_size[bsize_ctx]); |
| 9935 | let colors = read_palette_colors_y(dec, n, cache, fctx); |
| 9936 | palette_y_pending = Some((n, colors)); |
| 9937 | } |
| 9938 | let palette_uv_mode_ctx = usize::from(use_palette_y); |
| 9939 | // `read_palette_mode_info`'s own `xd->is_chroma_ref` guard. |
| 9940 | if has_chroma |
| 9941 | && uv_mode == DC_PRED |
| 9942 | && dec.symbol(&mut cdfs.palette_uv_mode[palette_uv_mode_ctx]) != 0 |
| 9943 | { |
| 9944 | let n = 2 + dec.symbol(&mut cdfs.palette_uv_size[bsize_ctx]); |
| 9945 | let (u_colors, v_colors) = read_palette_colors_uv(dec, n, palette_uv_cache, fctx); |
| 9946 | palette_uv_pending = Some((n, u_colors, v_colors)); |
| 9947 | } |
| 9948 | } |
| 9949 | let mut filter_intra = None; |
| 9950 | if crate::envflags::env_flag!("EC_AV1_TRACE") { |
| 9951 | let (rng, _) = dec.debug_state(); |
| 9952 | eprintln!( |
| 9953 | "TRACE_RECT_PREFI mode={mode} uv_mode={uv_mode} enable_filter_intra={} \ |
| 9954 | class={:?} rng={rng}", |
| 9955 | enable_filter_intra as i32, |
| 9956 | filter_intra_size_class_rect(bw, bh) |
| 9957 | ); |
| 9958 | } |
| 9959 | // As the square path: `av1_filter_intra_allowed` excludes a palette-Y |
| 9960 | // block (reconintra.h:77), so no `use_filter_intra` symbol exists there. |
| 9961 | if mode == DC_PRED |
| 9962 | && enable_filter_intra |
| 9963 | && palette_y_pending.is_none() |
| 9964 | && let Some(class) = filter_intra_size_class_rect(bw, bh) |
| 9965 | { |
| 9966 | let use_filter_intra = dec.symbol(&mut cdfs.filter_intra[class]) != 0; |
| 9967 | if crate::envflags::env_flag!("EC_AV1_TRACE") { |
| 9968 | eprintln!("TRACE_RECT_USEFI value={}", use_filter_intra as i32); |
| 9969 | } |
| 9970 | istep!("use_filter_intra", use_filter_intra as i32); |
| 9971 | if use_filter_intra { |
| 9972 | hit!(FILTER_INTRA_HITS); |
| 9973 | let fi_mode = dec.symbol(&mut cdfs.filter_intra_mode); |
| 9974 | istep!("filter_intra_mode", fi_mode as i32); |
| 9975 | filter_intra = Some(fi_mode); |
| 9976 | } |
| 9977 | } |
| 9978 | // `av1_visit_palette`: plane 0 (Y) then plane 1 (chroma, shared U/V map), |
| 9979 | // same order as [`read_intra_mode`]'s own square path. |
| 9980 | let palette_y = palette_y_pending.map(|(n, colors)| { |
| 9981 | let map = |
| 9982 | decode_color_index_map_wh(dec, cdfs, n, bw, bh, palette_onscreen(mi_r, mi_c, bw, bh, fctx), false); |
| 9983 | hit!(PALETTE_HITS); |
| 9984 | hit!(PALETTE_RECT_HITS); |
| 9985 | PaletteY { size: n, colors, map } |
| 9986 | }); |
| 9987 | let palette_uv = palette_uv_pending.map(|(n, u_colors, v_colors)| { |
| 9988 | // `av1_get_plane_block_size(bsize, 1, 1)` (`ss_size_lookup`), NOT a |
| 9989 | // plain halving: BLOCK_16X4 subsamples to BLOCK_8X4 and BLOCK_4X16 to |
| 9990 | // BLOCK_4X8, both floored at the 4-px transform. Halving the short |
| 9991 | // axis to 2 read HALF the colour-index symbols of a 1:4 strip's |
| 9992 | // chroma palette and desynced the tile from there. |
| 9993 | let (cw, ch) = ((bw >> ss_x(fctx)).max(4), (bh >> ss_y(fctx)).max(4)); |
| 9994 | let on = palette_onscreen_uv((bw, bh), palette_onscreen(mi_r, mi_c, bw, bh, fctx), (cw, ch), (ss_x(fctx), ss_y(fctx))); |
| 9995 | let map = decode_color_index_map_wh(dec, cdfs, n, cw, ch, on, true); |
| 9996 | hit!(PALETTE_UV_HITS); |
| 9997 | PaletteUv { size: n, u_colors, v_colors, map } |
| 9998 | }); |
| 9999 | Ok(( |
| 10000 | skip, |
| 10001 | mode, |
| 10002 | angle_delta_y, |
| 10003 | uv_mode, |
| 10004 | angle_delta_uv, |
| 10005 | alpha, |
| 10006 | filter_intra, |
| 10007 | palette_y, |
| 10008 | palette_uv, |
| 10009 | None, |
| 10010 | )) |
| 10011 | } |
| 10012 | |
| 10013 | /// Stamps a 1:4 strip's palette state into the mi-granular above/left arrays, |
| 10014 | /// at the strip's REAL mi position. `av1_get_palette_cache` reads |
| 10015 | /// `xd->above_mbmi`/`xd->left_mbmi`, i.e. the mi cell above/left of the |
| 10016 | /// block, so each strip owns its own rows and columns and no aliasing rule |
| 10017 | /// is needed (lane-kf900 r6). |
| 10018 | #[allow(clippy::too_many_arguments)] |
| 10019 | fn record_strip_palette( |
| 10020 | n: &mut Neighbours, |
| 10021 | mi: (usize, usize), |
| 10022 | bw: usize, |
| 10023 | bh: usize, |
| 10024 | y_size: usize, |
| 10025 | y_colors: [u16; 8], |
| 10026 | uv_size: usize, |
| 10027 | uv_colors: [u16; 8], |
| 10028 | ) { |
| 10029 | let (r, c) = mi; |
| 10030 | // lane-t900 r23 rung: every publication of a block's palette state into |
| 10031 | // the neighbour bands (`EC_PALPUB=1`) -- the rung that names which decode |
| 10032 | // path left a stale `left_palette_size` behind when |
| 10033 | // `av1_get_palette_mode_ctx` disagrees with libaom's. |
| 10034 | if crate::envflags::env_flag!("EC_PALPUB") { |
| 10035 | eprintln!("EC_PALPUB strip mi={r},{c} bw={bw} bh={bh} y={y_size} uv={uv_size}"); |
| 10036 | } |
| 10037 | // lane-kf900 r6: the palette side bands are mi-granular now, so a strip |
| 10038 | // writes its OWN span on BOTH axes -- the old `last` split-axis rule only |
| 10039 | // existed because the 16px [`SUB`] cell held one slot for all the strips |
| 10040 | // of a HORZ_4/VERT_4 and leaf 0 zeroing the other axis threw away the |
| 10041 | // real neighbour leaf 1 reads. |
| 10042 | { |
| 10043 | for cell in 0..(bw / MI).max(1) { |
| 10044 | if let Some(e) = n.above_palette_size.get_mut(c + cell) { |
| 10045 | *e = y_size as u8; |
| 10046 | n.above_palette_uv_size[c + cell] = uv_size as u8; |
| 10047 | // lane-publish2, see [`Neighbours::record_palette_y_rect`]: |
| 10048 | // colours of a size-0 neighbour are never read. |
| 10049 | if y_size > 0 { |
| 10050 | n.above_palette_colors[c + cell] = y_colors; |
| 10051 | } |
| 10052 | if uv_size > 0 { |
| 10053 | n.above_palette_uv_colors[c + cell] = uv_colors; |
| 10054 | } |
| 10055 | } |
| 10056 | } |
| 10057 | } |
| 10058 | { |
| 10059 | for cell in 0..(bh / MI).max(1) { |
| 10060 | if n.left_palette_size.get(r + cell).is_none() { |
| 10061 | break; |
| 10062 | } |
| 10063 | n.left_palette_size[r + cell] = y_size as u8; |
| 10064 | n.left_palette_uv_size[r + cell] = uv_size as u8; |
| 10065 | if y_size > 0 { |
| 10066 | n.left_palette_colors[r + cell] = y_colors; |
| 10067 | } |
| 10068 | if uv_size > 0 { |
| 10069 | n.left_palette_uv_colors[r + cell] = uv_colors; |
| 10070 | } |
| 10071 | } |
| 10072 | } |
| 10073 | } |
| 10074 | |
| 10075 | /// [`cfl_ac_q3`] for a true `bw`x`bh` rect strip (lane-intradisp r1). |
| 10076 | fn cfl_ac_q3_rect(y: &PlaneBuf<'_>, px: usize, py: usize, bw: usize, bh: usize) -> Vec<i32> { |
| 10077 | cfl_ac_q3_at(px, py, bw, bh, |lx, ly| i32::from(y.data[ly * y.width + lx])) |
| 10078 | } |
| 10079 | |
| 10080 | /// Everything a rect strip already read out of the stream before its luma |
| 10081 | /// transform units are decoded -- [`decode_rect_split`]'s own mode bundle |
| 10082 | /// (nine values that would otherwise be nine more parameters). |
| 10083 | struct RectStripModes { |
| 10084 | skip: bool, |
| 10085 | mode: usize, |
| 10086 | angle_delta_y: i32, |
| 10087 | uv_predict_mode: usize, |
| 10088 | angle_delta_uv: i32, |
| 10089 | alpha: Option<(i32, i32)>, |
| 10090 | filter_intra: Option<usize>, |
| 10091 | smooth_neighbor: bool, |
| 10092 | smooth_neighbor_uv: bool, |
| 10093 | /// lane-palette2 r9: the strip's palette-Y prediction (`bw`x`bh`, already |
| 10094 | /// mapped through the colour table), sliced per transform unit below -- |
| 10095 | /// palette prediction reads no edge pixels, so a unit's slice of the |
| 10096 | /// whole-block map IS its prediction. Without this a palette strip whose |
| 10097 | /// transform splits predicted DC and came out flat grey while the entropy |
| 10098 | /// stream stayed in sync (the coefficients are read either way). |
| 10099 | palette_y: Option<Vec<u16>>, |
| 10100 | /// As `palette_y`, for the un-split chroma transform (U, V). |
| 10101 | palette_uv: Option<(Vec<u16>, Vec<u16>)>, |
| 10102 | } |
| 10103 | |
| 10104 | /// The transform unit size (w, h) of a `bw`x`bh` intra block at tx `depth` |
| 10105 | /// -- libaom `depth_to_tx_size`, which walks `sub_tx_size_map` `depth` times |
| 10106 | /// from `max_txsize_rect_lookup[bsize]`. Every entry of that map halves the |
| 10107 | /// LONGER side of a rectangle and both sides of a square, so a 1:4 strip's |
| 10108 | /// first step is a 2:1 rect unit (`TX_32X8 -> TX_16X8`, `TX_64X16 -> |
| 10109 | /// TX_32X16`) and only the second reaches a square -- the reason |
| 10110 | /// `bw.min(bh) >> (depth - 1)` (correct for a 2:1 strip, where step one is |
| 10111 | /// already square) mis-tiled 1:4 strips. |
| 10112 | fn depth_to_tx_wh( |
| 10113 | bw: usize, |
| 10114 | bh: usize, |
| 10115 | depth: usize, |
| 10116 | fctx: &crate::decode::FrameCtx, |
| 10117 | ) -> (usize, usize) { |
| 10118 | // lane-lossless: TX_4X4 whatever the strip's own size and depth (libaom |
| 10119 | // `read_tx_size`: the lossless early return comes before |
| 10120 | // `max_txsize_rect_lookup`), and no `tx_depth` symbol was read to get here. |
| 10121 | if lossless(fctx) { |
| 10122 | return (4, 4); |
| 10123 | } |
| 10124 | let (mut w, mut h) = (bw.min(64), bh.min(64)); |
| 10125 | for _ in 0..depth { |
| 10126 | match w.cmp(&h) { |
| 10127 | std::cmp::Ordering::Greater => w /= 2, |
| 10128 | std::cmp::Ordering::Less => h /= 2, |
| 10129 | std::cmp::Ordering::Equal => { |
| 10130 | w /= 2; |
| 10131 | h /= 2; |
| 10132 | } |
| 10133 | } |
| 10134 | } |
| 10135 | (w, h) |
| 10136 | } |
| 10137 | |
| 10138 | /// Decodes the planes of one `bw`x`bh` HORZ/VERT intra strip whose luma |
| 10139 | /// transform is SPLIT (`tx_depth != 0`) -- lane-rectsplit r1, the case every |
| 10140 | /// rect path here refused by name until now ("per-unit rect prediction is not |
| 10141 | /// ported"). |
| 10142 | /// |
| 10143 | /// `depth_to_tx_size` (libaom `blockd.h`) walks `sub_tx_size_map` `depth` |
| 10144 | /// times from the block's own `max_txsize_rect_lookup` entry, and for a 2:1 |
| 10145 | /// strip the very first step already lands on a SQUARE transform |
| 10146 | /// (`sub_tx_size_map[TX_32X16] == TX_16X16`, `[TX_64X32] == TX_32X32`, and |
| 10147 | /// every further step halves a square) -- so each unit is square and reads |
| 10148 | /// through the ordinary square [`read_plane`]. What is genuinely rectangular |
| 10149 | /// here is the *tiling* (`bw/tx` by `bh/tx` units, not `n` by `n` as |
| 10150 | /// [`decode_block`]'s square split loop assumes) and the chroma plane, whose |
| 10151 | /// own transform is `av1_get_max_uv_txsize` of the whole strip and is never |
| 10152 | /// split by luma's depth. |
| 10153 | /// |
| 10154 | /// Each unit predicts off whatever the earlier units of this same block |
| 10155 | /// already reconstructed (spec 5.11.36 `predict_and_reconstruct_intra_block` |
| 10156 | /// inside `av1_foreach_transformed_block`, raster order), which is exactly |
| 10157 | /// what predicting the whole strip in one shot got wrong. |
| 10158 | #[allow(clippy::too_many_arguments)] |
| 10159 | fn decode_rect_split( |
| 10160 | dec: &mut SymbolDecoder, |
| 10161 | cdfs: &mut Cdfs, |
| 10162 | neighbours: &mut Neighbours, |
| 10163 | at_mi: (usize, usize), |
| 10164 | bw: usize, |
| 10165 | bh: usize, |
| 10166 | // The transform unit's own width and height in pixels. Square for every |
| 10167 | // 2:1 strip from the first split on, but a 1:4 strip's FIRST split is |
| 10168 | // still a 2:1 RECT unit (`sub_tx_size_map[TX_32X8] == TX_16X8`, |
| 10169 | // `[TX_64X16] == TX_32X16`) -- see [`depth_to_tx_wh`]. |
| 10170 | tx_w: usize, |
| 10171 | tx_h: usize, |
| 10172 | m: &RectStripModes, |
| 10173 | y: &mut PlaneBuf<'static>, |
| 10174 | u: &mut PlaneBuf<'static>, |
| 10175 | v: &mut PlaneBuf<'static>, |
| 10176 | base_q_idx: u8, |
| 10177 | reduced_tx_set: bool, fctx: &crate::decode::FrameCtx, |
| 10178 | ) -> Result<()> { |
| 10179 | let (mi_r, mi_c) = at_mi; |
| 10180 | let (px, py) = (mi_c * MI, mi_r * MI); |
| 10181 | let (cpx, cpy) = (px >> ss_x(fctx), py >> ss_y(fctx)); |
| 10182 | let (chroma_w, chroma_h) = (bw >> ss_x(fctx), bh >> ss_y(fctx)); |
| 10183 | // The chroma transform is picked before any symbol is read, so an |
| 10184 | // unsupported chroma shape refuses before this strip desyncs the tile. |
| 10185 | let chroma: Option<(TxbSet, &[u16])> = match (chroma_w, chroma_h) { |
| 10186 | (16, 8) => Some((TxbSet::ChromaRect16x8, &SCAN_16X8[..])), |
| 10187 | (8, 16) => Some((TxbSet::ChromaRect16x8, &SCAN_8X16[..])), |
| 10188 | (32, 16) => Some((TxbSet::ChromaRect32x16, &SCAN_32X16[..])), |
| 10189 | (16, 32) => Some((TxbSet::ChromaRect32x16, &SCAN_16X32[..])), |
| 10190 | // The 1:4 and sub-16 strips (lane-rectchroma r1 added the 32x8/8x32 |
| 10191 | // rows, lane-rectsplitx r1 the 16x4/4x16 ones). `get_txsize_entropy_ctx` |
| 10192 | // squares a rect transform down to a CDF set by |
| 10193 | // `(wide_log2 + high_log2 + 1) >> 1`: TX_32X8 -> the 16x16 set, |
| 10194 | // TX_16X4 -> the 8x8 set, TX_8X4 -> the 8x8 set -- exactly the sets |
| 10195 | // the UNSPLIT 1:4 (`decode_block_rect4`) and 16x8 (`decode_leaf_rect`) |
| 10196 | // paths already prove for these same shapes. |
| 10197 | (32, 8) => Some((TxbSet::Chroma16, &SCAN_32X8[..])), |
| 10198 | (8, 32) => Some((TxbSet::Chroma16, &SCAN_8X32[..])), |
| 10199 | (16, 4) => Some((TxbSet::Chroma8, &SCAN_16X4[..])), |
| 10200 | (4, 16) => Some((TxbSet::Chroma8, &SCAN_4X16[..])), |
| 10201 | (8, 4) => Some((TxbSet::ChromaRect8x4, &SCAN_8X4[..])), |
| 10202 | (4, 8) => Some((TxbSet::ChromaRect8x4, &SCAN_4X8[..])), |
| 10203 | _ => None, |
| 10204 | }; |
| 10205 | if !m.skip { |
| 10206 | if let Some(i) = match (chroma_w, chroma_h) { |
| 10207 | (32, 8) => Some(0), |
| 10208 | (8, 32) => Some(1), |
| 10209 | (8, 4) => Some(2), |
| 10210 | (4, 8) => Some(3), |
| 10211 | _ => None, |
| 10212 | } { |
| 10213 | hit_do! { |
| 10214 | RECT_SPLIT_CHROMA_SHAPE_HITS.with(|c| { |
| 10215 | let mut hits = c.get(); |
| 10216 | hits[i] += 1; |
| 10217 | c.set(hits); |
| 10218 | }); |
| 10219 | } |
| 10220 | } |
| 10221 | if chroma.is_none() { |
| 10222 | // Diagnostic for the next caller that lands here (class |
| 10223 | // refusal-names-a-correlate: the message names the tool, not the |
| 10224 | // shape); the refusal below keeps its inventory-pinned wording. |
| 10225 | eprintln!("EC_RECTCHROMA_GAP luma={bw}x{bh} tx={tx_w}x{tx_h} chroma={chroma_w}x{chroma_h}"); |
| 10226 | } |
| 10227 | } |
| 10228 | // lane-rectsplit r2 (verifier finding): with both callers wired |
| 10229 | // (`decode_block_rect`'s 32x16/16x32 and `decode_block_rect64`'s |
| 10230 | // 64x32/32x64) every shape that reaches here has a table above, so this |
| 10231 | // arm is UNREACHABLE today. It stays as the shape guard for the next |
| 10232 | // caller -- a new strip size would otherwise hit the `expect` below and |
| 10233 | // panic instead of refusing by name -- and so stays in |
| 10234 | // `refusal_inventory::REFUSALS`. |
| 10235 | // A 4:4:4 64-axis strip's chroma plane block is larger than one adjusted |
| 10236 | // UV transform (TX_64X32 adjusts to TX_32X32). Those sizes are tiled |
| 10237 | // below; everything else still needs a single-unit table. |
| 10238 | // lane-av1-422: a 4:2:2 strip's chroma plane keeps the strip's HEIGHT and |
| 10239 | // halves only the width, so a rect luma strip codes a SQUARE chroma block |
| 10240 | // (16x8 luma -> 8x8 chroma) -- the square rows below. At 4:2:0 and 4:4:4 a |
| 10241 | // rect strip's chroma is itself rect, so the square rows are unreachable |
| 10242 | // there and cannot change their results. |
| 10243 | let chroma_tiled = matches!((chroma_w, chroma_h), (64, 32) | (32, 64) | (64, 16) | (16, 64) | (32, 32) | (16, 16) | (8, 8)); |
| 10244 | if !m.skip && chroma.is_none() && !chroma_tiled { |
| 10245 | return Err(unsupported( |
| 10246 | "a coded HORZ/VERT strip whose chroma transform has no rect coefficient tables here", |
| 10247 | )); |
| 10248 | } |
| 10249 | hit!(RECT_SPLIT_TX_HITS); |
| 10250 | if bw.max(bh) == 64 && bw / tx_w > 1 && bh / tx_h > 1 { |
| 10251 | hit!(RECT_SPLIT_SB_INTERIOR_TU_HITS); |
| 10252 | } |
| 10253 | // LUMA: `bw/tx` x `bh/tx` square transform units, raster order. |
| 10254 | // A square unit reads through the ordinary square [`read_plane`]; a rect |
| 10255 | // one (a 1:4 strip's first split) reads through [`read_coeffs_rect`] with |
| 10256 | // the very tables the unsplit 16x8/32x16 paths use. |
| 10257 | let luma_rect: Option<(TxbSet, &[u16])> = match (tx_w, tx_h) { |
| 10258 | (w, h) if w == h => None, |
| 10259 | (16, 8) | (8, 16) => Some(( |
| 10260 | if reduced_tx_set { |
| 10261 | TxbSet::LumaRect16x8 |
| 10262 | } else { |
| 10263 | TxbSet::LumaRect16x8Set1 |
| 10264 | }, |
| 10265 | if tx_w == 16 { &SCAN_16X8[..] } else { &SCAN_8X16[..] }, |
| 10266 | )), |
| 10267 | (32, 16) => Some((TxbSet::LumaRect32x16, &SCAN_32X16[..])), |
| 10268 | (16, 32) => Some((TxbSet::LumaRect32x16, &SCAN_16X32[..])), |
| 10269 | // lane-rect64port r1: the un-split TX_64X32/TX_32X64 of a 64-level |
| 10270 | // strip, read through the `luma_64corner` arm below instead of a rect |
| 10271 | // table -- a 64-length axis codes only its low 32 coefficients |
| 10272 | // (`av1_get_max_eob` / the zero-out of `av1_get_adjusted_tx_size`), so |
| 10273 | // the coded unit is the 32x32 corner [`decode_block_rect64`]'s own |
| 10274 | // depth-0 arm already reads on the key-frame path. |
| 10275 | // |
| 10276 | // lane-sub8x4 r3: WITNESS FOUND, so this is no longer behind |
| 10277 | // `EC_RECT64_SPLIT`. r1's "no witness" was true only while the |
| 10278 | // sub-8x8 intra leaves still refused: with those lifted (r2) the |
| 10279 | // first 50 frame OBUs of the 10-bit 3840x1608 stream's head decode |
| 10280 | // all 33 frames pixel-exact against ffmpeg while this arm fires 90x |
| 10281 | // 64x32 + 5x 32x64 -- pinned as `fixtures/reelb_rect64_intra16x4_witness.obu` |
| 10282 | // and gated by `a_10bit_film_frames_with_rect64_corner_tus_decode_pixel_exact`. |
| 10283 | // lane-wit16x4 r1 re-measured that fixture on the merged tip: this arm |
| 10284 | // fires 64x32=148 32x64=123 (the 90/5 above were counted out of the |
| 10285 | // frame-33 desync lane-frame36 r2 fixed), all 33 frames pixel-exact. |
| 10286 | (64, 32) | (32, 64) => None, |
| 10287 | _ => { |
| 10288 | return Err(unsupported(format!( |
| 10289 | "a split intra strip whose transform unit is {tx_w}x{tx_h} (no luma \ |
| 10290 | coefficient tables for that shape here)" |
| 10291 | ))) |
| 10292 | } |
| 10293 | }; |
| 10294 | // The `None` above that is NOT the square arm. |
| 10295 | let luma_64corner = tx_w != tx_h; |
| 10296 | // lane-rectsplitx r1, MEASURED: the SQUARE-unit walk below is pixel-exact |
| 10297 | // on a real stream (the pinned 16x8 filter-intra fixture decodes its whole |
| 10298 | // frame exactly, two sub-16 split strips in it), but the RECT-unit arm -- |
| 10299 | // a 1:4 strip's depth-1 TX_16X8/TX_32X16 -- mismatched ffmpeg on the |
| 10300 | // 32-level 1:4 band fixture (8-bit, cq 12, seed 42). The tables and the |
| 10301 | // per-unit rect walk stay wired above/below; the round that finds that |
| 10302 | // defect deletes this refusal and nothing else. |
| 10303 | let luma_set = txbset_for(tx_w.min(tx_h), reduced_tx_set); |
| 10304 | let luma_scan = default_scan(tx_w.min(tx_h)); |
| 10305 | let zero_tu = vec![0i32; tx_w * tx_h]; |
| 10306 | // The strip's OWN edge availability, the fallback for a transform unit |
| 10307 | // whose top-right/bottom-left pixels fall outside the strip. |
| 10308 | let block_reach = Reach::of_rect(bw, bh, px, py, y.width, y.height, fctx); |
| 10309 | for tu_row in 0..bh / tx_h { |
| 10310 | for tu_col in 0..bw / tx_w { |
| 10311 | let tu_mi = (mi_r + tu_row * (tx_h / MI), mi_c + tu_col * (tx_w / MI)); |
| 10312 | let (tu_px, tu_py) = (px + tu_col * tx_w, py + tu_row * tx_h); |
| 10313 | let (col_off, row_off) = (tu_col * tx_w, tu_row * tx_h); |
| 10314 | // lane-hgkf r1 (ported from lane-golomb 791d87a, unmerged): |
| 10315 | // libaom clips `av1_foreach_transformed_block_in_plane` to |
| 10316 | // `max_blocks_wide/high` (`mb_to_right_edge`/`mb_to_bottom_edge`, |
| 10317 | // i.e. `mi_cols * 4` / `mi_rows * 4`): a transform unit whose |
| 10318 | // TOP-LEFT sample falls outside the frame is never coded. We read |
| 10319 | // coefficients for those phantom units and desynced the stream at |
| 10320 | // the very next symbol. |
| 10321 | if tu_px >= y.true_width || tu_py >= y.true_height { |
| 10322 | continue; |
| 10323 | } |
| 10324 | // `has_top_right`/`has_bottom_left` (libaom `reconintra.c`) for a |
| 10325 | // transform unit INSIDE a block, which is a different rule from |
| 10326 | // the standalone-block table [`Reach::of`] applies -- and the |
| 10327 | // reason a depth-2 superblock strip came out one sample off |
| 10328 | // before this: `Reach::of(tx, ..)` answered as if the unit were |
| 10329 | // its own block at that superblock position. |
| 10330 | // * top-right: if the unit's right neighbour is still inside the |
| 10331 | // strip the pixels are already reconstructed (`col_off + |
| 10332 | // tx_wide < plane_bw`); otherwise only the strip's own top row |
| 10333 | // of units can reach past it, through the block-level answer. |
| 10334 | // * bottom-left: a unit that is not in the strip's left column |
| 10335 | // has none (`col_off > 0` returns 0); inside the strip |
| 10336 | // (`row_off + tx_high < plane_bh`) they are the left |
| 10337 | // neighbour's, already reconstructed; the last row falls back |
| 10338 | // to the block-level answer. |
| 10339 | let tu_reach = Reach::of_tu(bw, bh, col_off, row_off, tx_w, tx_h, block_reach); |
| 10340 | // The unit's own window on the block's palette prediction. |
| 10341 | if let Some(buf) = &m.palette_y { |
| 10342 | let tu_buf: Vec<u16> = (0..tx_h) |
| 10343 | .flat_map(|i| { |
| 10344 | let base = (row_off + i) * bw + col_off; |
| 10345 | buf[base..base + tx_w].iter().copied() |
| 10346 | }) |
| 10347 | .collect(); |
| 10348 | set_palette_pred(tu_buf, fctx); |
| 10349 | } |
| 10350 | if m.skip { |
| 10351 | push_intra_rect(0, |
| 10352 | tu_px, |
| 10353 | tu_py, |
| 10354 | tx_w, |
| 10355 | tx_h, |
| 10356 | m.mode, |
| 10357 | m.angle_delta_y, |
| 10358 | tu_reach, |
| 10359 | &zero_tu, |
| 10360 | None, |
| 10361 | m.filter_intra, |
| 10362 | m.smooth_neighbor, fctx, |
| 10363 | ); |
| 10364 | neighbours.record_mi_luma_rect(tu_mi, tx_w, tx_h, &zero_tu); |
| 10365 | } else if luma_64corner && luma_rect.is_none() { |
| 10366 | // The un-split 64-level luma transform: `read_coeffs` on the |
| 10367 | // 32x32 CDF set [`TxbSet::Luma64`] with `default_scan(TX32)` |
| 10368 | // and the real rect shape passed through for |
| 10369 | // `av1_nz_map_ctx_offset` -- byte for byte |
| 10370 | // [`decode_block_rect64`]'s depth-0 read, which the key-frame |
| 10371 | // 64-level strip gate already proves. `tx_type` is not coded |
| 10372 | // (`av1_get_ext_tx_set_type` resolves EXT_TX_SET_DCTONLY once |
| 10373 | // the square-up size is TX_64X64), and this unit covers its |
| 10374 | // whole block, so `get_txb_ctx` gives txb_skip ctx 0 flat. |
| 10375 | if crate::envflags::env_flag!("EC_RECT64TU") { |
| 10376 | eprintln!("EC_RECT64TU mi={tu_mi:?} tx={tx_w}x{tx_h} mode={}", m.mode); |
| 10377 | } |
| 10378 | hit_do! { |
| 10379 | RECT64_CORNER_TU_HITS.with(|c| { |
| 10380 | let mut v = c.get(); |
| 10381 | v[usize::from(tx_h > tx_w)] += 1; |
| 10382 | c.set(v); |
| 10383 | }); |
| 10384 | } |
| 10385 | let tu_around = neighbours.around_mi_rect(tu_mi, tx_w, tx_h)[0]; |
| 10386 | let mut coding = cdfs.txb(TxbSet::Luma64, fi_tx_row(m.mode, m.filter_intra)); |
| 10387 | let (corner, tu_tx_type) = read_coeffs( |
| 10388 | dec, |
| 10389 | &mut coding, |
| 10390 | default_scan(TX32), |
| 10391 | 0, |
| 10392 | dc_sign_ctx(tu_around.2), |
| 10393 | TxType::DctDct, |
| 10394 | Some((tx_w, tx_h)), |
| 10395 | )?; |
| 10396 | let (cw, ch) = (tx_w.min(TX32), tx_h.min(TX32)); |
| 10397 | let mut tu_grid = vec![0i32; tx_w * tx_h]; |
| 10398 | for row in 0..ch { |
| 10399 | tu_grid[row * tx_w..][..cw].copy_from_slice(&corner[row * cw..][..cw]); |
| 10400 | } |
| 10401 | let residual = dequant_and_inverse_typed_wh( |
| 10402 | &tu_grid, |
| 10403 | tx_w, |
| 10404 | tx_h, |
| 10405 | crate::decode::bit_depth(fctx), |
| 10406 | block_q_idx(fctx), |
| 10407 | plane_q_delta(0, fctx).0, |
| 10408 | plane_q_delta(0, fctx).1, |
| 10409 | tu_tx_type, |
| 10410 | block_iqmatrix(fctx, 0, tx_w, tx_h, tu_tx_type), |
| 10411 | ); |
| 10412 | push_intra_rect(0, |
| 10413 | tu_px, |
| 10414 | tu_py, |
| 10415 | tx_w, |
| 10416 | tx_h, |
| 10417 | m.mode, |
| 10418 | m.angle_delta_y, |
| 10419 | tu_reach, |
| 10420 | &residual, |
| 10421 | None, |
| 10422 | m.filter_intra, |
| 10423 | m.smooth_neighbor, fctx, |
| 10424 | ); |
| 10425 | neighbours.record_mi_luma_rect(tu_mi, tx_w, tx_h, &tu_grid); |
| 10426 | } else if let Some((rect_set, rect_scan)) = luma_rect { |
| 10427 | // The rect-unit twin of the square arm below: same contexts, |
| 10428 | // same order, through the rect coefficient reader. The |
| 10429 | // `tx_type` CDF row of a filter-intra block is |
| 10430 | // `fimode_to_intradir[filter_intra_mode]`, never the block's |
| 10431 | // own (always DC_PRED) mode -- [`read_plane`] applies that |
| 10432 | // for the square arm, so this one must too. |
| 10433 | let tu_around = neighbours.around_mi_rect(tu_mi, tx_w, tx_h)[0]; |
| 10434 | // libaom `get_txb_ctx`: a LUMA transform that covers its whole |
| 10435 | // block (`plane_bsize == txsize_to_bsize[tx_size]`, i.e. the |
| 10436 | // unsplit depth-0 strip) reads txb_skip ctx 0 flat -- the |
| 10437 | // neighbour-magnitude table is for a unit SMALLER than its |
| 10438 | // block. [`decode_block_rect`]'s own unsplit read passes a |
| 10439 | // literal 0 for the same reason; this arm applied the table |
| 10440 | // unconditionally and desynced every depth-0 rect strip that |
| 10441 | // reached it (lane-intrarect r2: ctx 5 where aomdec read 0). |
| 10442 | let tu_skip_ctx = if tx_w == bw && tx_h == bh { |
| 10443 | 0 |
| 10444 | } else { |
| 10445 | neighbours.luma_skip_ctx_rect(tu_mi, tx_w / MI, tx_h / MI) |
| 10446 | }; |
| 10447 | let mut coding = cdfs.txb(rect_set, fi_tx_row(m.mode, m.filter_intra)); |
| 10448 | let (tu_grid, tu_tx_type) = read_coeffs_rect( |
| 10449 | dec, |
| 10450 | &mut coding, |
| 10451 | rect_scan, |
| 10452 | tx_w, |
| 10453 | tx_h, |
| 10454 | tu_skip_ctx, |
| 10455 | dc_sign_ctx(tu_around.2), |
| 10456 | TxType::DctDct, |
| 10457 | )?; |
| 10458 | let residual = dequant_and_inverse_typed_wh( |
| 10459 | &tu_grid, |
| 10460 | tx_w, |
| 10461 | tx_h, |
| 10462 | crate::decode::bit_depth(fctx), |
| 10463 | block_q_idx(fctx), |
| 10464 | plane_q_delta(0, fctx).0, |
| 10465 | plane_q_delta(0, fctx).1, |
| 10466 | tu_tx_type, |
| 10467 | block_iqmatrix(fctx, 0, tx_w, tx_h, tu_tx_type), |
| 10468 | ); |
| 10469 | push_intra_rect(0, |
| 10470 | tu_px, |
| 10471 | tu_py, |
| 10472 | tx_w, |
| 10473 | tx_h, |
| 10474 | m.mode, |
| 10475 | m.angle_delta_y, |
| 10476 | tu_reach, |
| 10477 | &residual, |
| 10478 | None, |
| 10479 | m.filter_intra, |
| 10480 | m.smooth_neighbor, fctx, |
| 10481 | ); |
| 10482 | neighbours.record_mi_luma_rect(tu_mi, tx_w, tx_h, &tu_grid); |
| 10483 | } else { |
| 10484 | let tu_around = neighbours.around_mi(tu_mi, tx_w)[0]; |
| 10485 | // This unit is smaller than the block it sits in, so |
| 10486 | // `txb_skip_ctx` is the neighbour-magnitude table, not the |
| 10487 | // lone-TU 0 (spec `get_txb_ctx_general`). |
| 10488 | let tu_skip_ctx = neighbours.luma_skip_ctx(tu_mi, tx_w / MI); |
| 10489 | let tu_grid = read_plane( |
| 10490 | dec, |
| 10491 | cdfs, |
| 10492 | luma_set, |
| 10493 | &luma_scan, |
| 10494 | 0, |
| 10495 | tu_around, |
| 10496 | m.mode, |
| 10497 | m.mode, |
| 10498 | m.angle_delta_y, |
| 10499 | tu_reach, |
| 10500 | y, |
| 10501 | tu_px, |
| 10502 | tu_py, |
| 10503 | tx_w, |
| 10504 | tx_w, |
| 10505 | base_q_idx, |
| 10506 | None, |
| 10507 | m.filter_intra, |
| 10508 | Some(tu_skip_ctx), |
| 10509 | m.smooth_neighbor, fctx, |
| 10510 | )?; |
| 10511 | neighbours.record_mi_luma(tu_mi, tx_w, &tu_grid); |
| 10512 | } |
| 10513 | } |
| 10514 | } |
| 10515 | // CHROMA: one un-split rect transform for the whole strip. |
| 10516 | // lane-lossless2: except on a lossless frame, where it is a raster of 4x4 |
| 10517 | // units -- each stamps its own neighbour context as it is read, and the |
| 10518 | // whole-block `record_split_luma_rect_mi` below would overwrite all of |
| 10519 | // them with the LAST unit's state (class `override-slot-on-one-arm`: the |
| 10520 | // next block then read `dc_sign_ctx` 1 where libaom reads 0). They are |
| 10521 | // collected here and replayed after that record. |
| 10522 | let mut ll_chroma_units: Vec<((usize, usize), usize, Grid)> = Vec::new(); |
| 10523 | // The chroma_tiled arm below stamps each unit's coefficient context |
| 10524 | // immediately (so the NEXT unit reads it), then the whole-strip |
| 10525 | // `record_split_luma_rect_mi` rewrites every chroma cell with ONE |
| 10526 | // whole-strip state and the per-unit DC signs and levels are lost |
| 10527 | // (class `override-slot-on-one-arm`). The units are collected here and |
| 10528 | // replayed after that record, the way the lossless arm's |
| 10529 | // `ll_chroma_units` and `decode_inter_block`'s `mu_chroma_units` tail |
| 10530 | // already do. |
| 10531 | let mut tiled_chroma_units: Vec<((usize, usize), usize, Grid, usize, usize)> = Vec::new(); |
| 10532 | let ac = m.alpha.map(|_| cfl_src_rect(px, py, bw, bh)); |
| 10533 | let reach = block_reach; |
| 10534 | let (u_grid, v_grid) = if mono(fctx) { |
| 10535 | // lane-av1mono: a monochrome frame codes no chroma coefficients on |
| 10536 | // any block (`plane < av1_num_planes == 1`), so the whole chroma |
| 10537 | // section -- the skipped-strip pushes, the lossless raster and the |
| 10538 | // un-split `read_coeffs_rect` below -- is bypassed. |
| 10539 | let zero = Grid::Zero(chroma_w * chroma_h); |
| 10540 | (zero.clone(), zero) |
| 10541 | } else if m.skip { |
| 10542 | let zero = Grid::Zero(chroma_w * chroma_h); |
| 10543 | if let Some((ub, _)) = &m.palette_uv { |
| 10544 | set_palette_pred(ub.clone(), fctx); |
| 10545 | } |
| 10546 | push_intra_rect(1, |
| 10547 | cpx, cpy, chroma_w, chroma_h, m.uv_predict_mode, m.angle_delta_uv, reach, &zero, |
| 10548 | m.alpha.zip(ac).map(|((au, _), ac)| (au, ac)), None, m.smooth_neighbor_uv, fctx, |
| 10549 | ); |
| 10550 | if let Some((_, vb)) = &m.palette_uv { |
| 10551 | set_palette_pred(vb.clone(), fctx); |
| 10552 | } |
| 10553 | push_intra_rect(2, |
| 10554 | cpx, cpy, chroma_w, chroma_h, m.uv_predict_mode, m.angle_delta_uv, reach, &zero, |
| 10555 | m.alpha.zip(ac).map(|((_, av), ac)| (av, ac)), None, m.smooth_neighbor_uv, fctx, |
| 10556 | ); |
| 10557 | (zero.clone(), zero) |
| 10558 | } else if lossless(fctx) { |
| 10559 | // lane-lossless: the strip's chroma plane block is coded as 4x4 |
| 10560 | // transform units too (libaom `read_tx_size` returns TX_4X4 for every |
| 10561 | // plane), PLANE-MAJOR inside the block -- U's units first, then V's |
| 10562 | // (`decode_token_recon_block`'s mu-chunk loop, measured against |
| 10563 | // aomdec's `EC_TRACE_COEFF` on a `-crf 0` stream: plane=1, plane=1, |
| 10564 | // plane=2, plane=2 for a 16x8 strip). `get_txb_ctx` adds its `+10` |
| 10565 | // offset (our `+3` row base) whenever the plane block is bigger than |
| 10566 | // the transform. |
| 10567 | let scan4 = default_scan(4); |
| 10568 | let offset = (chroma_w > 4 || chroma_h > 4).then_some(3); |
| 10569 | let (cw_n, ch_n) = (chroma_w / 4, chroma_h / 4); |
| 10570 | let mut last = [Grid::Zero(16), Grid::Zero(16)]; |
| 10571 | for plane_idx in 1..=2 { |
| 10572 | for cu_row in 0..ch_n { |
| 10573 | for cu_col in 0..cw_n { |
| 10574 | let luma_span = 8; |
| 10575 | let cu_mi = ( |
| 10576 | mi_r + cu_row * (luma_span / MI), |
| 10577 | mi_c + cu_col * (luma_span / MI), |
| 10578 | ); |
| 10579 | let cu_around = neighbours.around_mi(cu_mi, luma_span); |
| 10580 | let cu_reach = tu_reach( |
| 10581 | bw, bh, |
| 10582 | cu_col * luma_span, cu_row * luma_span, luma_span, |
| 10583 | block_reach, px, py, y.width, y.height, fctx, |
| 10584 | ); |
| 10585 | let (cu_x, cu_y) = (cpx + cu_col * 4, cpy + cu_row * 4); |
| 10586 | if let Some((ub, vb)) = &m.palette_uv { |
| 10587 | let pbuf = if plane_idx == 1 { ub } else { vb }; |
| 10588 | set_palette_pred( |
| 10589 | palette_window(pbuf, chroma_w, cu_col * 4, cu_row * 4, 4, 4), |
| 10590 | fctx, |
| 10591 | ); |
| 10592 | } |
| 10593 | let plane = if plane_idx == 1 { &mut *u } else { &mut *v }; |
| 10594 | let grid = read_plane( |
| 10595 | dec, cdfs, TxbSet::Chroma4, &scan4, plane_idx, |
| 10596 | cu_around[plane_idx], m.mode, m.uv_predict_mode, |
| 10597 | m.angle_delta_uv, cu_reach, plane, cu_x, cu_y, 4, 4, |
| 10598 | base_q_idx, |
| 10599 | m.alpha.zip(ac).map(|((au, av), ac)| { |
| 10600 | (if plane_idx == 1 { au } else { av }, ac) |
| 10601 | }), |
| 10602 | None, offset, m.smooth_neighbor_uv, fctx, |
| 10603 | )?; |
| 10604 | neighbours.record_mi_chroma(cu_mi, luma_span, luma_span, plane_idx, &grid); |
| 10605 | ll_chroma_units.push((cu_mi, plane_idx, grid.clone())); |
| 10606 | last[plane_idx - 1] = grid; |
| 10607 | } |
| 10608 | } |
| 10609 | } |
| 10610 | let [ug, vg] = last; |
| 10611 | (ug, vg) |
| 10612 | } else if chroma_tiled { |
| 10613 | let (uw, uh, set, scan): (usize, usize, TxbSet, &[u16]) = match (chroma_w, chroma_h) { |
| 10614 | (64, 32) | (32, 64) | (32, 32) => (32, 32, TxbSet::Chroma32, default_scan(32)), |
| 10615 | (64, 16) => (32, 16, TxbSet::ChromaRect32x16, &SCAN_32X16), |
| 10616 | (16, 64) => (16, 32, TxbSet::ChromaRect32x16, &SCAN_16X32), |
| 10617 | // lane-av1-422: the 4:2:2 square chroma planes (height unhalved), |
| 10618 | // through the square reader like every other square chroma unit. |
| 10619 | (16, 16) => (16, 16, TxbSet::Chroma16, default_scan(16)), |
| 10620 | (8, 8) => (8, 8, TxbSet::Chroma8, default_scan(8)), |
| 10621 | _ => unreachable!("chroma_tiled"), |
| 10622 | }; |
| 10623 | // lane-av1-422: the witness counter, keyed to ss (1,0) so only a 4:2:2 |
| 10624 | // frame's square chroma units count it. |
| 10625 | if uw == uh && ss_x(fctx) == 1 && ss_y(fctx) == 0 { |
| 10626 | hit!(CHROMA422_SQUARE_HITS); |
| 10627 | } |
| 10628 | let (nw, nh) = (chroma_w / uw, chroma_h / uh); |
| 10629 | let bigger = chroma_w * chroma_h > uw * uh; |
| 10630 | let default_tx = TxType::DctDct; |
| 10631 | let mut last = [Grid::Zero(uw * uh), Grid::Zero(uw * uh)]; |
| 10632 | for plane_idx in 1..=2 { |
| 10633 | for cu_row in 0..nh { |
| 10634 | for cu_col in 0..nw { |
| 10635 | let luma_span_x = uw << ss_x(fctx); |
| 10636 | let luma_span_y = uh << ss_y(fctx); |
| 10637 | let cu_mi = ( |
| 10638 | mi_r + cu_row * (luma_span_y / MI), |
| 10639 | mi_c + cu_col * (luma_span_x / MI), |
| 10640 | ); |
| 10641 | let cu_around = neighbours.around_mi_rect(cu_mi, luma_span_x, luma_span_y); |
| 10642 | let (cu_x, cu_y) = (cpx + cu_col * uw, cpy + cu_row * uh); |
| 10643 | let cfl = m.alpha.map(|(au, av)| { |
| 10644 | let alpha = if plane_idx == 1 { au } else { av }; |
| 10645 | ( |
| 10646 | alpha, |
| 10647 | cfl_src_rect( |
| 10648 | px + cu_col * luma_span_x, |
| 10649 | py + cu_row * luma_span_y, |
| 10650 | luma_span_x, |
| 10651 | luma_span_y, |
| 10652 | ), |
| 10653 | ) |
| 10654 | }); |
| 10655 | // 32x32 does not fit read_coeffs_rect's scratch (768). The |
| 10656 | // square reader is the path every other 32x32 chroma unit uses. |
| 10657 | let levels = if uw == uh { |
| 10658 | let plane = if plane_idx == 1 { &mut *u } else { &mut *v }; |
| 10659 | read_plane( |
| 10660 | dec, cdfs, set, scan, plane_idx, cu_around[plane_idx], |
| 10661 | m.mode, m.uv_predict_mode, m.angle_delta_uv, reach, |
| 10662 | plane, cu_x, cu_y, uw, uw, base_q_idx, cfl, None, |
| 10663 | bigger.then_some(3), m.smooth_neighbor_uv, fctx, |
| 10664 | )? |
| 10665 | } else { |
| 10666 | let skip_ctx = usize::from(cu_around[plane_idx].0) |
| 10667 | + usize::from(cu_around[plane_idx].1) |
| 10668 | + usize::from(bigger) * 3; |
| 10669 | let mut coding = cdfs.txb(set, m.uv_predict_mode); |
| 10670 | let (levels, tx_type) = read_coeffs_rect( |
| 10671 | dec, &mut coding, scan, uw, uh, skip_ctx, |
| 10672 | dc_sign_ctx(cu_around[plane_idx].2), default_tx, |
| 10673 | )?; |
| 10674 | let residual = dequant_and_inverse_typed_wh( |
| 10675 | &levels, uw, uh, crate::decode::bit_depth(fctx), |
| 10676 | block_q_idx(fctx), |
| 10677 | plane_q_delta(plane_idx, fctx).0, |
| 10678 | plane_q_delta(plane_idx, fctx).1, |
| 10679 | tx_type, |
| 10680 | block_iqmatrix(fctx, plane_idx, uw, uh, tx_type), |
| 10681 | ); |
| 10682 | push_intra_rect( |
| 10683 | plane_idx, cu_x, cu_y, uw, uh, |
| 10684 | m.uv_predict_mode, m.angle_delta_uv, reach, &residual, |
| 10685 | cfl, None, m.smooth_neighbor_uv, fctx, |
| 10686 | ); |
| 10687 | levels |
| 10688 | }; |
| 10689 | neighbours.record_mi_chroma(cu_mi, luma_span_x, luma_span_y, plane_idx, &levels); |
| 10690 | tiled_chroma_units.push(( |
| 10691 | cu_mi, |
| 10692 | plane_idx, |
| 10693 | levels.clone(), |
| 10694 | luma_span_x, |
| 10695 | luma_span_y, |
| 10696 | )); |
| 10697 | last[plane_idx - 1] = levels; |
| 10698 | } |
| 10699 | } |
| 10700 | } |
| 10701 | (last[0].clone(), last[1].clone()) |
| 10702 | } else { |
| 10703 | let (chroma_set, chroma_scan) = chroma.expect("refused above when None"); |
| 10704 | // `av1_get_ext_tx_set_type`: a chroma transform whose square-up size |
| 10705 | // is TX_32X32 or bigger resolves to `EXT_TX_SET_DCTONLY` for intra, |
| 10706 | // so only the 16x8/8x16 pair takes the mode-indexed default. |
| 10707 | let default_tx = if chroma_w.max(chroma_h) >= 32 { |
| 10708 | TxType::DctDct |
| 10709 | } else { |
| 10710 | default_intra_tx_type(m.uv_predict_mode as u8) |
| 10711 | }; |
| 10712 | let around = neighbours.around_mi_rect(at_mi, bw, bh); |
| 10713 | let mut grids: Vec<Grid> = Vec::with_capacity(2); |
| 10714 | for plane_idx in 1..=2 { |
| 10715 | let skip_ctx = |
| 10716 | usize::from(around[plane_idx].0) + usize::from(around[plane_idx].1); |
| 10717 | let mut coding = cdfs.txb(chroma_set, m.uv_predict_mode); |
| 10718 | let (levels, tx_type) = read_coeffs_rect( |
| 10719 | dec, |
| 10720 | &mut coding, |
| 10721 | chroma_scan, |
| 10722 | chroma_w, |
| 10723 | chroma_h, |
| 10724 | skip_ctx, |
| 10725 | dc_sign_ctx(around[plane_idx].2), |
| 10726 | default_tx, |
| 10727 | )?; |
| 10728 | // The same delta_q proof the unsplit rect64 path records |
| 10729 | // (lane-rectsplit r4: lifting the split-transform refusal moved |
| 10730 | // superblock strips onto this path, so only counting there would |
| 10731 | // leave the delta_q gate's counter silent for them). |
| 10732 | if bw.max(bh) == 64 && fctx.current_q_idx.with(|c| c.get()) != i32::from(base_q_idx) { |
| 10733 | hit!(RECT64_QIDX_DRIFT_HITS); |
| 10734 | } |
| 10735 | let residual = dequant_and_inverse_typed_wh( |
| 10736 | &levels, |
| 10737 | chroma_w, |
| 10738 | chroma_h, |
| 10739 | crate::decode::bit_depth(fctx), |
| 10740 | // lane-d792: the BLOCK's quantizer index, i.e. the frame's |
| 10741 | // own index PLUS this block's segment `SEG_LVL_ALT_Q` -- |
| 10742 | // reading `current_q_idx` here dropped the segment feature |
| 10743 | // that every other dequant site applies (class |
| 10744 | // `parsed-then-discarded`). |
| 10745 | block_q_idx(fctx), |
| 10746 | plane_q_delta(plane_idx, fctx).0, |
| 10747 | plane_q_delta(plane_idx, fctx).1, |
| 10748 | tx_type, |
| 10749 | block_iqmatrix(fctx, plane_idx, chroma_w, chroma_h, tx_type), |
| 10750 | ); |
| 10751 | let cfl = m |
| 10752 | .alpha |
| 10753 | .zip(ac) |
| 10754 | .map(|((au, av), ac)| (if plane_idx == 1 { au } else { av }, ac)); |
| 10755 | let _plane = if plane_idx == 1 { &mut *u } else { &mut *v }; |
| 10756 | if let Some((ub, vb)) = &m.palette_uv { |
| 10757 | let buf = if plane_idx == 1 { ub } else { vb }; |
| 10758 | set_palette_pred(buf.clone(), fctx); |
| 10759 | } |
| 10760 | push_intra_rect(plane_idx, |
| 10761 | cpx, |
| 10762 | cpy, |
| 10763 | chroma_w, |
| 10764 | chroma_h, |
| 10765 | m.uv_predict_mode, |
| 10766 | m.angle_delta_uv, |
| 10767 | reach, |
| 10768 | &residual, |
| 10769 | cfl, |
| 10770 | None, |
| 10771 | m.smooth_neighbor_uv, fctx, |
| 10772 | ); |
| 10773 | grids.push(levels); |
| 10774 | } |
| 10775 | let mut it = grids.into_iter(); |
| 10776 | (it.next().unwrap(), it.next().unwrap()) |
| 10777 | }; |
| 10778 | neighbours.record_split_luma_rect_mi( |
| 10779 | at_mi, |
| 10780 | bw, |
| 10781 | bh, |
| 10782 | m.mode, |
| 10783 | m.uv_predict_mode, |
| 10784 | [&u_grid, &v_grid], |
| 10785 | ); |
| 10786 | for (cu_mi, plane, unit) in &ll_chroma_units { |
| 10787 | neighbours.record_mi_chroma(*cu_mi, 8, 8, *plane, unit); |
| 10788 | } |
| 10789 | for (cu_mi, plane, unit, span_w, span_h) in &tiled_chroma_units { |
| 10790 | neighbours.record_mi_chroma(*cu_mi, *span_w, *span_h, *plane, unit); |
| 10791 | } |
| 10792 | neighbours.fill_skip_grid_rect((mi_r, mi_c), bw / MI, bh / MI, m.skip); |
| 10793 | neighbours.fill_lf_grid_rect((mi_r, mi_c), bw / MI, bh / MI, tx_w as u8, tx_h as u8, 0, fctx); |
| 10794 | if crate::envflags::env_flag!("EC_AV1_TRACE") { |
| 10795 | let (rng, _) = dec.debug_state(); |
| 10796 | eprintln!( |
| 10797 | "TRACE_RECT_SPLIT mi_row={mi_r} mi_col={mi_c} bw={bw} bh={bh} \ |
| 10798 | tx={tx_w}x{tx_h} rng={rng}" |
| 10799 | ); |
| 10800 | } |
| 10801 | Ok(()) |
| 10802 | } |
| 10803 | |
| 10804 | /// One intra-coded HORZ/VERT strip on the INTER block path (lane-intrarect |
| 10805 | /// r1) -- the shape [`decode_inter_block`]'s intra arm refused by name |
| 10806 | /// because it predicts squares only. Every pixel here goes through the rect |
| 10807 | /// machinery the key-frame paths already prove: [`decode_rect_split`], which |
| 10808 | /// at `depth == 0` is the unsplit strip as a single transform unit and |
| 10809 | /// beyond it the per-unit walk of [`depth_to_tx_wh`]. |
| 10810 | /// |
| 10811 | /// What is genuinely different from [`decode_block_rect`] is the mode |
| 10812 | /// syntax: on an inter frame an intra block reads `y_mode[size_group]` and |
| 10813 | /// `uv_mode` (libaom `read_intra_block_mode_info`, decodemv.c:1065), never |
| 10814 | /// the key frame's `kf_y_mode` above/left pair. This mirrors the square |
| 10815 | /// intra-in-inter arm's own reads at the rect footprint, including its |
| 10816 | /// inherited gaps -- no `use_filter_intra` symbol (every inter gate recipe |
| 10817 | /// pins `--enable-filter-intra=0`; lane-fiinter owns that read for both the |
| 10818 | /// square and this arm) and a nonzero luma angle delta refused by name. |
| 10819 | /// |
| 10820 | /// The strip's own neighbour records (luma per transform unit, chroma, skip |
| 10821 | /// grid, deblock grid) are [`decode_rect_split`]'s; the caller adds the |
| 10822 | /// inter-side bands and the mi map. |
| 10823 | #[allow(clippy::too_many_arguments)] |
| 10824 | /// The `(size_group_lookup, bsize_to_tx_size_cat)` row an INTRA block coded |
| 10825 | /// inside an inter frame reads its `y_mode` and `tx_depth` symbols from, for |
| 10826 | /// the rect footprints [`decode_intra_rect_in_inter`] resolves itself (the |
| 10827 | /// 128-root halves and the 16x4/4x16 strips of a 1:4 partition take their own |
| 10828 | /// branches before this). libaom `common_data.h:61` / `blockd.h:1347`: the 2:1 |
| 10829 | /// strips keep the group/category diagonal (BLOCK_16X8 -> 1/1, BLOCK_32X16 -> |
| 10830 | /// 2/2, BLOCK_64X32 -> 3/3), the 1:4 ones break it (BLOCK_32X8 is group 1 but |
| 10831 | /// category 2, BLOCK_64X16 group 2 and category 3), which is why the row is a |
| 10832 | /// table here rather than a shift of `bw.min(bh)`. |
| 10833 | fn intra_in_inter_size_row(bw: usize, bh: usize) -> Option<(usize, usize)> { |
| 10834 | Some(match (bw.min(bh), bw.max(bh)) { |
| 10835 | (8, 16) => (1usize, 1usize), |
| 10836 | (16, 32) => (2, 2), |
| 10837 | (32, 64) => (3, 3), |
| 10838 | (8, 32) => (1, 2), |
| 10839 | (16, 64) => (2, 3), |
| 10840 | _ => return None, |
| 10841 | }) |
| 10842 | } |
| 10843 | |
| 10844 | /// lane-t900 r20, refusal B ("an intra-coded {bw}x{bh} block on the inter |
| 10845 | /// block path"): the shapes that can reach that arm, enumerated against the |
| 10846 | /// MEASURED domain of the inter block path |
| 10847 | /// ([`INTER_BLOCK_SHAPES`], the census the gate |
| 10848 | /// `a_block_shape_census_over_three_real_streams_leaves_the_rect_residual_refusal_unreachable` |
| 10849 | /// fills from three real streams). |
| 10850 | /// |
| 10851 | /// [`decode_intra_rect_in_inter`] is called from one site, under |
| 10852 | /// `write_w != side || write_h != side`, and only after two earlier arms have |
| 10853 | /// taken their shapes: a footprint with a side below 8 is either a 16x4/4x16 |
| 10854 | /// strip WITH a chroma-pair record (the `strip16` branch) or refused by name, |
| 10855 | /// and a 128-px footprint returns from the `bw.max(bh) == 128` branch. What is |
| 10856 | /// left is every rectangular census shape with `8 <= min` and `max <= 64` -- |
| 10857 | /// and each of those has a row, so the refusal names a shape no measured |
| 10858 | /// stream can present. |
| 10859 | #[test] |
| 10860 | fn every_intra_in_inter_shape_the_census_lists_has_a_size_group_row() { |
| 10861 | let mut checked = 0; |
| 10862 | for &(side, w, h) in &INTER_BLOCK_SHAPES { |
| 10863 | if (w, h) == (side, side) || w.min(h) < 8 || w.max(h) > 64 { |
| 10864 | continue; |
| 10865 | } |
| 10866 | assert!( |
| 10867 | intra_in_inter_size_row(w, h).is_some(), |
| 10868 | "the inter path decodes {w}x{h} blocks, and an INTRA one of that shape would hit \ |
| 10869 | the \"no size-group/tx-category row\" refusal" |
| 10870 | ); |
| 10871 | checked += 1; |
| 10872 | } |
| 10873 | assert_eq!( |
| 10874 | checked, 10, |
| 10875 | "the block-shape census changed: {checked} rect shapes reach the intra-in-inter row \ |
| 10876 | lookup, not the 10 this claim was measured over" |
| 10877 | ); |
| 10878 | } |
| 10879 | |
| 10880 | /// The `motion_mode` / `obmc` CDF row for a block footprint: libaom's |
| 10881 | /// `motion_mode_cdf[mbmi->bsize]`, in this crate's packed row order (which |
| 10882 | /// `cdf::MOTION_MODE` documents -- the 128-px rows come last, so they are |
| 10883 | /// matched before the square arm's `log2` shortcut). |
| 10884 | /// |
| 10885 | /// `None` is the refusal "a motion_mode symbol for a block shape with no CDF |
| 10886 | /// row here"; the enumeration below is what proves no conformant block can |
| 10887 | /// reach it. |
| 10888 | pub(crate) fn motion_mode_cdf_row(write_w: usize, write_h: usize) -> Option<usize> { |
| 10889 | Some(match (write_w, write_h) { |
| 10890 | // lane-sb128c r5: the 128 root's own rows come FIRST -- the square arm |
| 10891 | // below maps 128 to row 4 (BLOCK_16X32). |
| 10892 | (64, 128) => 14, |
| 10893 | (128, 64) => 15, |
| 10894 | (128, 128) => 16, |
| 10895 | (w, h) if w == h => (w.trailing_zeros() - 3) as usize, |
| 10896 | (16, 32) => 4, |
| 10897 | (32, 16) => 5, |
| 10898 | (32, 64) => 6, |
| 10899 | (64, 32) => 7, |
| 10900 | (8, 32) => 8, |
| 10901 | (32, 8) => 9, |
| 10902 | (16, 64) => 10, |
| 10903 | (64, 16) => 11, |
| 10904 | // lane-inter4 r3: the 16x16-level rect inter leaves. |
| 10905 | (8, 16) => 12, |
| 10906 | (16, 8) => 13, |
| 10907 | _ => return None, |
| 10908 | }) |
| 10909 | } |
| 10910 | |
| 10911 | /// Every `BLOCK_SIZES_ALL` footprint, as `(width, height)` in pixels: the six |
| 10912 | /// squares, the ten 2:1 shapes and the six 1:4 ones (libaom `common_data.h`, |
| 10913 | /// `block_size_wide`/`block_size_high`). |
| 10914 | #[cfg(test)] |
| 10915 | const BLOCK_SIZES_ALL_WH: [(usize, usize); 22] = [ |
| 10916 | (4, 4), |
| 10917 | (4, 8), |
| 10918 | (8, 4), |
| 10919 | (8, 8), |
| 10920 | (8, 16), |
| 10921 | (16, 8), |
| 10922 | (16, 16), |
| 10923 | (16, 32), |
| 10924 | (32, 16), |
| 10925 | (32, 32), |
| 10926 | (32, 64), |
| 10927 | (64, 32), |
| 10928 | (64, 64), |
| 10929 | (64, 128), |
| 10930 | (128, 64), |
| 10931 | (128, 128), |
| 10932 | (4, 16), |
| 10933 | (16, 4), |
| 10934 | (8, 32), |
| 10935 | (32, 8), |
| 10936 | (16, 64), |
| 10937 | (64, 16), |
| 10938 | ]; |
| 10939 | |
| 10940 | /// lane-t900 r25, enumeration: the refusal "a motion_mode symbol for a block |
| 10941 | /// shape with no CDF row here" names a shape no conformant stream can present. |
| 10942 | /// |
| 10943 | /// A `motion_mode`/`obmc` symbol is read only under `motion_mode_eligible`, |
| 10944 | /// whose `write_w.min(write_h) >= 8` clause is libaom's |
| 10945 | /// `is_motion_variation_allowed_bsize` (`blockd.h:1455`). So the reachable |
| 10946 | /// domain of [`motion_mode_cdf_row`] is exactly the `BLOCK_SIZES_ALL` |
| 10947 | /// footprints with `min(bw, bh) >= 8` -- 17 of the 22 -- and each of those |
| 10948 | /// must map to a distinct row of the 17-row `Cdfs::motion_mode` table. The |
| 10949 | /// five it excludes (4x4, 4x8, 8x4, 4x16, 16x4) are the ones the eligibility |
| 10950 | /// clause rejects before the lookup. |
| 10951 | #[test] |
| 10952 | fn every_shape_that_allows_motion_variation_has_a_motion_mode_cdf_row() { |
| 10953 | let mut rows = std::collections::BTreeSet::new(); |
| 10954 | let mut excluded = 0; |
| 10955 | for (w, h) in BLOCK_SIZES_ALL_WH { |
| 10956 | if w.min(h) < 8 { |
| 10957 | excluded += 1; |
| 10958 | assert!( |
| 10959 | w == 4 || h == 4, |
| 10960 | "{w}x{h} is not one of the sub-8 shapes is_motion_variation_allowed_bsize rejects" |
| 10961 | ); |
| 10962 | continue; |
| 10963 | } |
| 10964 | let row = motion_mode_cdf_row(w, h).unwrap_or_else(|| { |
| 10965 | panic!( |
| 10966 | "a {w}x{h} block passes is_motion_variation_allowed_bsize and reads a motion_mode \ |
| 10967 | symbol, but has no CDF row -- the refusal is a real gap, not a dead arm" |
| 10968 | ) |
| 10969 | }); |
| 10970 | assert!( |
| 10971 | row < 17, |
| 10972 | "{w}x{h} maps to motion_mode row {row}, outside the 17-row table" |
| 10973 | ); |
| 10974 | assert!(rows.insert(row), "{w}x{h} shares motion_mode row {row}"); |
| 10975 | } |
| 10976 | assert_eq!(excluded, 5, "the sub-8 footprint set changed"); |
| 10977 | assert_eq!( |
| 10978 | rows.len(), |
| 10979 | 17, |
| 10980 | "17 eligible footprints must fill all 17 rows of the motion_mode table" |
| 10981 | ); |
| 10982 | } |
| 10983 | |
| 10984 | fn decode_intra_rect_in_inter( |
| 10985 | dec: &mut SymbolDecoder, |
| 10986 | cdfs: &mut Cdfs, |
| 10987 | neighbours: &mut Neighbours, |
| 10988 | at: (usize, usize), |
| 10989 | bw: usize, |
| 10990 | bh: usize, |
| 10991 | skip: bool, |
| 10992 | allow_screen_content_tools: bool, |
| 10993 | // `(PARTITION_HORZ_4, is_chroma_reference)` when this strip is one of the |
| 10994 | // four 16x4 / 4x16 strips of a 16x16-level 1:4 partition (lane-intra16x4). |
| 10995 | strip16: Option<(bool, bool)>, |
| 10996 | y: &mut PlaneBuf<'static>, |
| 10997 | u: &mut PlaneBuf<'static>, |
| 10998 | v: &mut PlaneBuf<'static>, |
| 10999 | base_q_idx: u8, fctx: &crate::decode::FrameCtx, |
| 11000 | ) -> Result<()> { |
| 11001 | // `at` is in MI units (the inter path's own unit since lane-inter4); |
| 11002 | // `(r, c)` is the enclosing SUB cell the SUB-granular neighbour bands |
| 11003 | // below are still keyed by. |
| 11004 | let (mi_r, mi_c) = at; |
| 11005 | let (r, c) = (mi_r / (SUB / MI), mi_c / (SUB / MI)); |
| 11006 | // `size_group_lookup` (libaom common_data.h) and `bsize_to_tx_size_cat` |
| 11007 | // (blockd.h) for the three 2:1 strip shapes: BLOCK_16X8 -> group 1 / |
| 11008 | // cat 1, BLOCK_32X16 -> 2 / 2, BLOCK_64X32 -> 3 / 3 (the rectsplitx |
| 11009 | // finding -- the category is NOT the same for every strip size). |
| 11010 | // A 1:4 strip breaks that diagonal (BLOCK_32X8 is group 1 but cat 2), so |
| 11011 | // it is refused by name rather than read from the wrong CDF row. |
| 11012 | // lane-intra16x4: the 16x16-level 1:4 strip. `size_group_lookup` |
| 11013 | // (common_data.h:61) puts BLOCK_16X4/BLOCK_4X16 in group 0 and its |
| 11014 | // tx-depth category is 1 -- but nothing here has to know either, because |
| 11015 | // the strip decodes through the very body the KEY FRAME path proves for |
| 11016 | // this shape ([`decode_rect4_16_strip`]), including the 4:2:0 chroma |
| 11017 | // pairing (`is_chroma_reference`: strips 0 and 2 read no chroma at all, |
| 11018 | // strips 1 and 3 code one 8x4 (4x8) unit at the PAIR's origin). The only |
| 11019 | // inter-frame difference is the mode-info read, which |
| 11020 | // [`INTRA_IN_INTER_MODE`] switches to `read_intra_block_mode_info`'s |
| 11021 | // `y_mode[0]` plus the `skip` this caller already read. |
| 11022 | if let Some((horz, has_chroma)) = strip16 { |
| 11023 | // lane-t900 r22: the screen-content refusal here was STALE (class |
| 11024 | // stale-premise): [`decode_rect4_16_strip`] takes |
| 11025 | // `allow_screen_content_tools` and reads the whole palette syntax |
| 11026 | // (mode/size/colours/colour-index map, its own `palette_ctx_and_cache_mi` |
| 11027 | // pair) -- the key frame proves that body. Lifted with the 10-bit |
| 11028 | // witness `palette_screen_witness_10bit.obu`, whose inter frames carry |
| 11029 | // 16x4/4x16 intra strips inside a `--tune-content=screen` frame. |
| 11030 | debug_assert_eq!((bw, bh), if horz { (16, 4) } else { (4, 16) }); |
| 11031 | fctx.intra_in_inter_mode.with(|c| c.set(Some((0, skip)))); |
| 11032 | let res = decode_rect4_16_strip( |
| 11033 | dec, |
| 11034 | cdfs, |
| 11035 | neighbours, |
| 11036 | (mi_r, mi_c), |
| 11037 | horz, |
| 11038 | has_chroma, |
| 11039 | // The inter strips of the same partition publish their own |
| 11040 | // mi-exact mode, so the key-frame caller's coarse-cell stand-in |
| 11041 | // has nothing to add here. |
| 11042 | None, |
| 11043 | y, |
| 11044 | u, |
| 11045 | v, |
| 11046 | fctx.enable_filter_intra_inter.with(std::cell::Cell::get), |
| 11047 | allow_screen_content_tools, |
| 11048 | base_q_idx, |
| 11049 | fctx.tx_select_inter.with(std::cell::Cell::get), |
| 11050 | fctx.reduced_tx_set_inter.with(std::cell::Cell::get), fctx, |
| 11051 | ); |
| 11052 | fctx.intra_in_inter_mode.with(|c| c.set(None)); |
| 11053 | let (mode, uv, (tx_w, tx_h)) = res?; |
| 11054 | // lane-intra16x4, the same publication the 2:1 intra-in-inter strip |
| 11055 | // makes ([`decode_intra_rect_in_inter`]'s own tail): libaom runs |
| 11056 | // `set_txfm_ctxs(tx_size, n4_w, n4_h, skip && is_inter, xd)` for EVERY |
| 11057 | // block of an inter frame, intra ones included, and |
| 11058 | // `txfm_partition_update` for the var-tx context. The key-frame body |
| 11059 | // writes neither (a key frame has no var-tx tree and its own readers |
| 11060 | // use the deblock grid), so the block AFTER this strip read a stale |
| 11061 | // band -- measured: a 128x128 8-bit stream decoded the two intra 16x4 |
| 11062 | // strips at mi(23,16)/(23,20) exactly and then broke on the very next |
| 11063 | // 16x16 row. |
| 11064 | txfm_partition_update_rect(neighbours, (mi_r, mi_c), (tx_w, tx_h), (bw, bh)); |
| 11065 | for i in 0..bw / MI { |
| 11066 | if let Some(cell) = neighbours.above_txfm.get_mut(mi_c + i) { |
| 11067 | *cell = tx_w as u8; |
| 11068 | } |
| 11069 | } |
| 11070 | for i in 0..bh / MI { |
| 11071 | if let Some(cell) = neighbours.left_txfm.get_mut(mi_r + i) { |
| 11072 | *cell = tx_h as u8; |
| 11073 | } |
| 11074 | } |
| 11075 | // [`decode_rect4_16`]'s own post-loop publication, per strip (the |
| 11076 | // coarse 16-px cell holds one entry for the whole 16x16 and the last |
| 11077 | // block to write it wins). |
| 11078 | neighbours.above_mode[c] = mode; |
| 11079 | neighbours.left_mode[r] = mode; |
| 11080 | if let Some(uvm) = uv { |
| 11081 | neighbours.above_uv_mode[c] = uvm; |
| 11082 | neighbours.left_uv_mode[r] = uvm; |
| 11083 | } |
| 11084 | neighbours.above_side[c] = bw; |
| 11085 | neighbours.left_side[r] = bh; |
| 11086 | if crate::envflags::env_flag!("EC_INTRA16X4") { |
| 11087 | eprintln!( |
| 11088 | "EC_INTRA16X4 decoded mi={mi_r},{mi_c} px={},{} horz={horz} has_chroma={has_chroma} skip={skip} mode={mode} uv={uv:?}", |
| 11089 | mi_c * MI, |
| 11090 | mi_r * MI |
| 11091 | ); |
| 11092 | } |
| 11093 | hit_do! { |
| 11094 | INTRA16X4_IN_INTER_HITS.with(|h| { |
| 11095 | let mut v = h.get(); |
| 11096 | v[usize::from(!horz)] += 1; |
| 11097 | v[2] += usize::from(has_chroma); |
| 11098 | h.set(v); |
| 11099 | }); |
| 11100 | } |
| 11101 | return Ok(()); |
| 11102 | } |
| 11103 | // lane-inter128intra r1: the 128 superblock's PARTITION_HORZ/VERT half |
| 11104 | // coded INTRA inside an inter frame. `size_group_lookup` (common_data.h:61) |
| 11105 | // puts BLOCK_128X64/64X128/128X128 in group 3 and |
| 11106 | // `max_txsize_rect_lookup` caps them at TX_64X64, i.e. the same |
| 11107 | // `tx_size_cat3` row [`decode_block_128rect`] already reads -- so the whole |
| 11108 | // block decodes through the very body the KEY FRAME path proves for this |
| 11109 | // shape (square 64x64 luma units, one TX_32X32 chroma unit per 64x64 mu |
| 11110 | // chunk on the offset-10 txb_skip rows, per-TU prediction, the mu-chunk |
| 11111 | // `Reach` rule and the frame-edge write clip). The only inter-frame |
| 11112 | // differences live inside that body: the mode-info read |
| 11113 | // ([`INTRA_IN_INTER_MODE`] -> `y_mode[3]` plus the `skip` this caller |
| 11114 | // already read), the `tx_depth` context (the real TXFM_CONTEXT bands) and |
| 11115 | // the `set_txfm_ctxs` publication. CfL (>32), filter intra (>32) and |
| 11116 | // palette (>64) do not exist at this size and are refused by name there. |
| 11117 | // lane-t900 r18: the opt-in is LIFTED -- the arm has a witness. |
| 11118 | // `intra128_in_inter_witness2.obu` (gate |
| 11119 | // `an_intra_coded_128_half_inside_an_inter_frame_decodes_pixel_exact`) |
| 11120 | // fires it 3x 128x64 + 6x 64x128 and decodes byte-exact on every plane of |
| 11121 | // all 15 frames, so the hits are counted out of exact frames (class |
| 11122 | // `counter-from-refused-stream`). What r16/r17 could not find, and what |
| 11123 | // that recipe exploits: libaom's RD essentially never PICKS a 128-root |
| 11124 | // HORZ/VERT in the interior -- the 512x512 r16 witness and a textured |
| 11125 | // 640x384 sweep code 128 roots as NONE or SPLIT only. The shape appears |
| 11126 | // where the frame EDGE forces it: a partial last superblock row has |
| 11127 | // `has_rows128 == false`, so the 128x64 top half is implied, and a |
| 11128 | // partial last column implies the 64x128 left half. Put content no |
| 11129 | // reference holds (a fresh smooth ramp) in exactly those two bands of an |
| 11130 | // inter frame and the encoder codes those forced halves INTRA. |
| 11131 | if bw.max(bh) == 128 { |
| 11132 | // lane-t900 r32: no screen-content refusal here. `av1_allow_palette` |
| 11133 | // rejects every footprint with a 128-pixel side (`bw > 64 || bh > 64`, |
| 11134 | // enumerated by `no_block_footprint_with_a_128_pixel_side_can_carry_\ |
| 11135 | // palette_syntax`), so a 128-root half consumes no palette syntax and |
| 11136 | // a screen-content frame's half is syntactically identical to the |
| 11137 | // non-screen one this arm already decodes byte-exact. |
| 11138 | if coeff_trace_on() { |
| 11139 | // lane-t900 r16: which decode-order frame this arm fires in -- |
| 11140 | // interleaved with the `EC_COEFF_FRAME` markers, this is what |
| 11141 | // proves the hit is counted out of a frame that compares exact |
| 11142 | // (class `counter-from-refused-stream`). |
| 11143 | eprintln!("EC_INTRA128 mi_row={r} mi_col={c} bw={bw} bh={bh}"); |
| 11144 | } |
| 11145 | fctx.intra_in_inter_mode.with(|c| c.set(Some((3, skip)))); |
| 11146 | let res = decode_block_128rect( |
| 11147 | dec, |
| 11148 | cdfs, |
| 11149 | neighbours, |
| 11150 | (r, c), |
| 11151 | bw, |
| 11152 | bh, |
| 11153 | y, |
| 11154 | u, |
| 11155 | v, |
| 11156 | base_q_idx, |
| 11157 | fctx.enable_filter_intra_inter.with(std::cell::Cell::get), |
| 11158 | allow_screen_content_tools, |
| 11159 | fctx.tx_select_inter.with(std::cell::Cell::get), |
| 11160 | fctx.reduced_tx_set_inter.with(std::cell::Cell::get), fctx, |
| 11161 | ); |
| 11162 | fctx.intra_in_inter_mode.with(|c| c.set(None)); |
| 11163 | return res; |
| 11164 | } |
| 11165 | let Some((size_group, tx_cat)) = intra_in_inter_size_row(bw, bh) else { |
| 11166 | if crate::envflags::env_flag!("EC_OBMCREC") { |
| 11167 | eprintln!( |
| 11168 | "OBMCREC intra-in-inter shape bw={bw} bh={bh} mi=({mi_r},{mi_c}) skip={skip}" |
| 11169 | ); |
| 11170 | } |
| 11171 | // lane-obmcrec r1: the message used to name a 16x4/4x16 strip |
| 11172 | // (class refusal-names-a-correlate). lane-t900 r20: the shapes that |
| 11173 | // can arrive here are enumerated against the measured block-shape |
| 11174 | // census ([`INTER_BLOCK_SHAPES`]) by |
| 11175 | // [`every_intra_in_inter_shape_the_census_lists_has_a_size_group_row`] |
| 11176 | // -- every one of them has a row, so this arm names a shape no stream |
| 11177 | // has been able to present. |
| 11178 | return Err(unsupported(format!( |
| 11179 | "an intra-coded {bw}x{bh} block on the inter block path (no size-group/tx-category row for that shape here)" |
| 11180 | ))); |
| 11181 | }; |
| 11182 | // lane-t900 r21: this arm used to refuse EVERY rect strip of a |
| 11183 | // screen-content frame ("palette syntax is consumed for square blocks |
| 11184 | // only"). It is not: [`read_intra_mode_rect`] generalised the palette read |
| 11185 | // to rect blocks on the key-frame path, and the 1:4 arm below goes through |
| 11186 | // it, while the 2:1 reader inlined further down now codes the same syntax |
| 11187 | // itself. A real `--tune-content=screen` inter stream reached this refusal |
| 11188 | // on 8x16 / 16x8 strips (gate |
| 11189 | // `a_real_aomenc_screen_inter_sequence_codes_palette_syntax_on_rect_intra_strips`). |
| 11190 | // lane-intra14 r1: a 1:4 strip decodes through the very readers the KEY |
| 11191 | // FRAME path already proves for this shape ([`decode_block_rect4`] at the |
| 11192 | // 32 level, [`decode_block_rect64`] at 64 -- both already handle the 1:4 |
| 11193 | // luma/chroma transform tables, the per-unit split walk and the strip's |
| 11194 | // own neighbour records). The ONLY difference on an inter frame is the |
| 11195 | // mode-info read, which [`INTRA_IN_INTER_MODE`] switches to |
| 11196 | // `read_intra_block_mode_info`'s `y_mode[size_group]` plus the `skip` |
| 11197 | // this caller already read. `tx_cat` above is the same category those |
| 11198 | // readers hardcode (cat 2 in `decode_block_rect4`, cat 3 in |
| 11199 | // `decode_block_rect64`), asserted here so a future shape cannot drift. |
| 11200 | if bw.max(bh) == 4 * bw.min(bh) { |
| 11201 | debug_assert_eq!(tx_cat, if bw.max(bh) == 64 { 3 } else { 2 }); |
| 11202 | fctx.intra_in_inter_mode.with(|c| c.set(Some((size_group, skip)))); |
| 11203 | let res = if bw.max(bh) == 64 { |
| 11204 | decode_block_rect64( |
| 11205 | dec, |
| 11206 | cdfs, |
| 11207 | neighbours, |
| 11208 | (mi_r / (SUB / MI), mi_c / (SUB / MI)), |
| 11209 | bw, |
| 11210 | bh, |
| 11211 | y, |
| 11212 | u, |
| 11213 | v, |
| 11214 | fctx.enable_filter_intra_inter.with(std::cell::Cell::get), |
| 11215 | allow_screen_content_tools, |
| 11216 | base_q_idx, |
| 11217 | fctx.tx_select_inter.with(std::cell::Cell::get), |
| 11218 | fctx.reduced_tx_set_inter.with(std::cell::Cell::get), fctx, |
| 11219 | ) |
| 11220 | } else { |
| 11221 | decode_block_rect4( |
| 11222 | dec, |
| 11223 | cdfs, |
| 11224 | neighbours, |
| 11225 | (mi_r, mi_c), |
| 11226 | bw, |
| 11227 | bh, |
| 11228 | None, |
| 11229 | y, |
| 11230 | u, |
| 11231 | v, |
| 11232 | fctx.enable_filter_intra_inter.with(std::cell::Cell::get), |
| 11233 | allow_screen_content_tools, |
| 11234 | base_q_idx, |
| 11235 | fctx.tx_select_inter.with(std::cell::Cell::get), fctx, |
| 11236 | ) |
| 11237 | .map(|_| ()) |
| 11238 | }; |
| 11239 | fctx.intra_in_inter_mode.with(|c| c.set(None)); |
| 11240 | res?; |
| 11241 | let shape = match (bw, bh) { |
| 11242 | (64, 16) => 0, |
| 11243 | (16, 64) => 1, |
| 11244 | (32, 8) => 2, |
| 11245 | _ => 3, |
| 11246 | }; |
| 11247 | hit_do! { |
| 11248 | INTRA_RECT4_IN_INTER_HITS.with(|h| { |
| 11249 | let mut v = h.get(); |
| 11250 | v[shape] += 1; |
| 11251 | h.set(v); |
| 11252 | }); |
| 11253 | } |
| 11254 | return Ok(()); |
| 11255 | } |
| 11256 | let mode = dec.symbol(&mut cdfs.y_mode[size_group]); |
| 11257 | // lane-t900 r1: this arm read six mode symbols with no ladder rung of its |
| 11258 | // own, so a cross-decoder bisection could only see the block's first |
| 11259 | // coefficient. Same names/format as [`read_tx_size`]'s rung. |
| 11260 | let step_trace = crate::envflags::env_flag!("EC_TRACE_MODE_STEP"); |
| 11261 | if step_trace { |
| 11262 | eprintln!( |
| 11263 | "EC_ISTEP mi_row={mi_r} mi_col={mi_c} name=y_mode val={mode} grp={size_group} rng={}", |
| 11264 | dec.debug_state().0 |
| 11265 | ); |
| 11266 | } |
| 11267 | if mode >= 13 { |
| 11268 | return Err(unsupported( |
| 11269 | "an intra mode this decoder does not code (round 2)", |
| 11270 | )); |
| 11271 | } |
| 11272 | // lane-angleinter (merged on main for the square intra-in-inter arm): |
| 11273 | // the delta is read off `angle_delta_cdf[mode - V_PRED]` and feeds the |
| 11274 | // directional predictor; this arm refused a nonzero one before the merge. |
| 11275 | let angle_delta_y = if (V_PRED..=D67_PRED).contains(&mode) { |
| 11276 | read_angle_delta(dec, &mut cdfs.angle_delta[mode - V_PRED]) |
| 11277 | } else { |
| 11278 | 0 |
| 11279 | }; |
| 11280 | if angle_delta_y != 0 { |
| 11281 | hit!(INTRA_IN_INTER_ANGLE_DELTA_Y_HITS); |
| 11282 | } |
| 11283 | // `is_cfl_allowed` (spec 5.11.5) caps CFL at 32x32, so the |
| 11284 | // superblock-level strip reads the no-CFL alphabet -- the same split |
| 11285 | // [`read_intra_mode_rect`] makes for [`decode_block_rect64`]. |
| 11286 | // |
| 11287 | // lane-av1mono: a monochrome strip codes no chroma mode/CFL symbol |
| 11288 | // (libaom `decodemv.c:933`). |
| 11289 | let has_chroma = !mono(fctx); |
| 11290 | let cfl_allowed = has_chroma && cfl_allowed_px(bw, bh, fctx); |
| 11291 | let uv_mode = if !has_chroma { |
| 11292 | DC_PRED |
| 11293 | } else if cfl_allowed { |
| 11294 | dec.symbol(&mut cdfs.uv_mode_cfl[mode]) |
| 11295 | } else { |
| 11296 | dec.symbol(&mut cdfs.uv_mode_no_cfl[mode]) |
| 11297 | }; |
| 11298 | if step_trace { |
| 11299 | eprintln!( |
| 11300 | "EC_ISTEP mi_row={mi_r} mi_col={mi_c} name=uv_mode val={uv_mode} cfl={} rng={}", |
| 11301 | u8::from(cfl_allowed), |
| 11302 | dec.debug_state().0 |
| 11303 | ); |
| 11304 | } |
| 11305 | if (9..=12).contains(&uv_mode) { |
| 11306 | hit!(SMOOTH_UV_HITS); |
| 11307 | } |
| 11308 | let alpha = if cfl_allowed && uv_mode == UV_CFL_PRED { |
| 11309 | Some(read_cfl_alphas(dec, cdfs)) |
| 11310 | } else { |
| 11311 | None |
| 11312 | }; |
| 11313 | let uv_predict_mode = if uv_mode == UV_CFL_PRED { |
| 11314 | DC_PRED |
| 11315 | } else { |
| 11316 | uv_mode |
| 11317 | }; |
| 11318 | let angle_delta_uv = if (V_PRED..=D67_PRED).contains(&uv_mode) { |
| 11319 | hit!(DIRECTIONAL_UV_HITS); |
| 11320 | read_angle_delta(dec, &mut cdfs.angle_delta[uv_mode - V_PRED]) |
| 11321 | } else { |
| 11322 | 0 |
| 11323 | }; |
| 11324 | if angle_delta_uv != 0 { |
| 11325 | hit!(UV_ANGLE_DELTA_HITS); |
| 11326 | hit!(INTRA_IN_INTER_ANGLE_DELTA_UV_HITS); |
| 11327 | } |
| 11328 | // `read_palette_mode_info` (spec 5.11.13), in the position |
| 11329 | // `read_intra_block_mode_info` writes it: after the chroma mode and |
| 11330 | // before `filter_intra`. lane-t900 r21: a `--tune-content=screen` inter |
| 11331 | // frame writes `palette_y_mode`/`palette_uv_mode` for every block whose |
| 11332 | // shape [`palette_bsize_ctx_wh`] admits, intra strips included, so |
| 11333 | // skipping the syntax here desynced the tile at the strip's own symbol. |
| 11334 | // lane-t900 r22: reconstructed, not refused -- the strip's real mode |
| 11335 | // context and colour cache at its own mi position, and the two |
| 11336 | // colour-index maps decoded after `filter_intra` (`av1_visit_palette`), |
| 11337 | // exactly as [`read_intra_mode_rect`] does on the key-frame path. |
| 11338 | let (palette_ctx, palette_cache) = neighbours.palette_ctx_and_cache_mi((mi_r, mi_c)); |
| 11339 | let palette_uv_cache = neighbours.palette_uv_cache_mi((mi_r, mi_c)); |
| 11340 | let mut palette_y_pending: Option<(usize, [u16; 8])> = None; |
| 11341 | let mut palette_uv_pending: Option<(usize, [u16; 8], [u16; 8])> = None; |
| 11342 | if allow_screen_content_tools |
| 11343 | && let Some(bsize_ctx) = palette_bsize_ctx_wh(bw, bh) |
| 11344 | { |
| 11345 | hit!(INTRA_RECT_IN_INTER_SCREEN_HITS); |
| 11346 | let use_palette_y = |
| 11347 | mode == DC_PRED && dec.symbol(&mut cdfs.palette_y_mode[bsize_ctx][palette_ctx]) != 0; |
| 11348 | if use_palette_y { |
| 11349 | let n = 2 + dec.symbol(&mut cdfs.palette_y_size[bsize_ctx]); |
| 11350 | palette_y_pending = Some((n, read_palette_colors_y(dec, n, &palette_cache, fctx))); |
| 11351 | } |
| 11352 | // A 2:1 strip of at least 8x8 is always a chroma reference, so |
| 11353 | // `read_palette_mode_info`'s `xd->is_chroma_ref` guard holds here. |
| 11354 | // lane-av1mono: libaom's actual guard is `num_planes > 1 && |
| 11355 | // uv_mode == UV_DC_PRED && xd->is_chroma_ref` (decodemv.c:602). |
| 11356 | let use_palette_uv = has_chroma |
| 11357 | && uv_mode == DC_PRED |
| 11358 | && dec.symbol(&mut cdfs.palette_uv_mode[usize::from(use_palette_y)]) != 0; |
| 11359 | if crate::envflags::env_flag!("EC_PALSYN") { |
| 11360 | eprintln!("EC_PALSYN rect mi={mi_r},{mi_c} bw={bw} bh={bh} mode={mode} uv={uv_mode} ctx={palette_ctx} y={} uv={} rng={}", use_palette_y as i32, use_palette_uv as i32, dec.debug_state().0); |
| 11361 | } |
| 11362 | if use_palette_uv { |
| 11363 | let n = 2 + dec.symbol(&mut cdfs.palette_uv_size[bsize_ctx]); |
| 11364 | let (u_colors, v_colors) = read_palette_colors_uv(dec, n, &palette_uv_cache, fctx); |
| 11365 | palette_uv_pending = Some((n, u_colors, v_colors)); |
| 11366 | } |
| 11367 | } |
| 11368 | // lane-fiinter (merged on main for the square intra-in-inter arm): |
| 11369 | // `read_filter_intra_mode_info` is the tail of |
| 11370 | // `read_intra_block_mode_info`, after the palette syntax (refused whole |
| 11371 | // above for this shape), and `av1_filter_intra_allowed_bsize` admits the |
| 11372 | // 2:1 strips with both sides <= 32 -- their own CDF rows |
| 11373 | // ([`filter_intra_size_class_rect`]). |
| 11374 | let mut filter_intra = None; |
| 11375 | if mode == DC_PRED |
| 11376 | && fctx.enable_filter_intra_inter.with(std::cell::Cell::get) |
| 11377 | // `av1_filter_intra_allowed` excludes a palette-Y block. |
| 11378 | && palette_y_pending.is_none() |
| 11379 | && let Some(class) = filter_intra_size_class_rect(bw, bh) |
| 11380 | && dec.symbol(&mut cdfs.filter_intra[class]) != 0 |
| 11381 | { |
| 11382 | hit!(FILTER_INTRA_HITS); |
| 11383 | hit!(INTRA_IN_INTER_FILTER_INTRA_HITS); |
| 11384 | filter_intra = Some(dec.symbol(&mut cdfs.filter_intra_mode)); |
| 11385 | } |
| 11386 | // `av1_visit_palette`: Y's colour-index map then the shared chroma one. |
| 11387 | let palette_y = palette_y_pending.map(|(n, colors)| { |
| 11388 | let map = |
| 11389 | decode_color_index_map_wh(dec, cdfs, n, bw, bh, palette_onscreen(mi_r, mi_c, bw, bh, fctx), false); |
| 11390 | hit!(PALETTE_HITS); |
| 11391 | hit!(PALETTE_RECT_HITS); |
| 11392 | hit!(INTRA_IN_INTER_PALETTE_Y_HITS); |
| 11393 | PaletteY { size: n, colors, map } |
| 11394 | }); |
| 11395 | let palette_uv = palette_uv_pending.map(|(n, u_colors, v_colors)| { |
| 11396 | let (cw, ch) = ((bw >> ss_x(fctx)).max(4), (bh >> ss_y(fctx)).max(4)); |
| 11397 | let on = palette_onscreen_uv((bw, bh), palette_onscreen(mi_r, mi_c, bw, bh, fctx), (cw, ch), (ss_x(fctx), ss_y(fctx))); |
| 11398 | let map = decode_color_index_map_wh(dec, cdfs, n, cw, ch, on, true); |
| 11399 | hit!(PALETTE_UV_HITS); |
| 11400 | hit!(INTRA_IN_INTER_PALETTE_UV_HITS); |
| 11401 | PaletteUv { size: n, u_colors, v_colors, map } |
| 11402 | }); |
| 11403 | // The intra-edge filter type is the NEIGHBOUR's mode (libaom |
| 11404 | // `get_filt_type`), luma's off the luma neighbours and chroma's off the |
| 11405 | // chroma ones -- [`decode_block_rect`]'s own pair. |
| 11406 | // lane-t900 r4: the coarse pair is keyed on the 16x16 cell's OWN top-left |
| 11407 | // mi, so both 8x16 halves of a VERT split read the neighbours of the FIRST |
| 11408 | // half. libaom's `above_mbmi`/`left_mbmi` are this block's own |
| 11409 | // (`mi[-mi_stride]` / `mi[-1]`) -- take the mi-exact pair over the coarse |
| 11410 | // one exactly as [`decode_leaf8`] already does (class |
| 11411 | // context-read-from-one-cell). |
| 11412 | let coarse_pair = neighbours.modes_above_left(r, c); |
| 11413 | let (nb_above_mode, nb_left_mode) = neighbours.modes_above_left_mi(mi_r, mi_c, coarse_pair); |
| 11414 | if (nb_above_mode, nb_left_mode) != coarse_pair { |
| 11415 | hit!(EDGE_FILTER_MI_FIX_HITS); |
| 11416 | } |
| 11417 | let smooth_neighbor = is_smooth_mode(nb_above_mode) || is_smooth_mode(nb_left_mode); |
| 11418 | if smooth_neighbor { |
| 11419 | hit!(SMOOTH_LUMA_HITS); |
| 11420 | } |
| 11421 | let smooth_neighbor_uv = neighbours.smooth_uv_neighbour(mi_r, mi_c, r, c, fctx); |
| 11422 | // lane-av1mixloss: the lossless-segment early return (`decodeframe.c:1203`) |
| 11423 | // precedes `TX_MODE_SELECT`, so a lossless-segment intra-rect block inside |
| 11424 | // an inter frame codes no depth symbol either (`xd->lossless` is per |
| 11425 | // segment regardless of frame type; class sweep of the sub8 leaf's fix). |
| 11426 | let depth = if fctx.tx_select_inter.with(std::cell::Cell::get) && !lossless(fctx) { |
| 11427 | // The frame's own `TXFM_CONTEXT` bands -- [`tx_size_context_txfm_rect`]. |
| 11428 | let ctx = tx_size_context_txfm_rect(neighbours, (mi_r, mi_c), bw.min(64), bh.min(64), fctx); |
| 11429 | let d = match tx_cat { |
| 11430 | 1 => dec.symbol(&mut cdfs.tx_size_cat1[ctx]), |
| 11431 | 2 => dec.symbol(&mut cdfs.tx_size_cat2[ctx]), |
| 11432 | _ => dec.symbol(&mut cdfs.tx_size_cat3[ctx]), |
| 11433 | }; |
| 11434 | if step_trace { |
| 11435 | eprintln!( |
| 11436 | "EC_ISTEP mi_row={mi_r} mi_col={mi_c} name=tx_depth val={d} ctx={ctx} cat={tx_cat} rng={}", |
| 11437 | dec.debug_state().0 |
| 11438 | ); |
| 11439 | } |
| 11440 | d |
| 11441 | } else { |
| 11442 | 0 |
| 11443 | }; |
| 11444 | if depth != 0 || lossless(fctx) { |
| 11445 | hit!(TX_DEPTH_HITS); |
| 11446 | // lane-intrasplit r1: a split intra strip in an inter frame walks the |
| 11447 | // key frame's own per-TU path ([`decode_rect_split`] below); what was |
| 11448 | // wrong when this refusal was written was the depth SYMBOL, not the walk. |
| 11449 | hit_do! { |
| 11450 | INTRA_RECT_IN_INTER_SPLIT_TX_HITS.with(|h| { |
| 11451 | let mut v = h.get(); |
| 11452 | v[depth.min(2)] += 1; |
| 11453 | h.set(v); |
| 11454 | }); |
| 11455 | } |
| 11456 | } |
| 11457 | // lane-intrasplit r3: print EVERY intra strip with its depth, not only the |
| 11458 | // split ones -- a `depth=0`-only stream is a witness gap in the SOURCE, and |
| 11459 | // the two are indistinguishable when the diagnostic fires on splits alone. |
| 11460 | if crate::envflags::env_flag!("EC_SPLITSTRIP") { |
| 11461 | eprintln!("EC_SPLITSTRIP mi_row={mi_r} mi_col={mi_c} bw={bw} bh={bh} depth={depth}"); |
| 11462 | } |
| 11463 | let (tx_w, tx_h) = depth_to_tx_wh(bw, bh, depth, fctx); |
| 11464 | // lane-inter16ab r8: libaom runs `set_txfm_ctxs(tx_size, n4_w, n4_h, |
| 11465 | // skip && is_inter, xd)` (decodeframe.c `decode_block`) for EVERY block |
| 11466 | // of an inter frame, intra ones included -- with the skip term 0 it |
| 11467 | // memsets `above_txfm_context` to `tx_size_wide` over the block's width |
| 11468 | // and `left_txfm_context` to `tx_size_high` over its height. This strip |
| 11469 | // wrote neither, so the next block's var-tx `txfm_partition` context read |
| 11470 | // a stale band: on a 192x128 8-bit stream the 16x8 intra strip at |
| 11471 | // mi(14,0) left 8 where libaom has 16, giving ctx 13 instead of 12 at |
| 11472 | // mi(16,0) and desyncing the tile at that symbol. |
| 11473 | txfm_partition_update_rect(neighbours, (mi_r, mi_c), (tx_w, tx_h), (bw, bh)); |
| 11474 | let modes = RectStripModes { |
| 11475 | skip, |
| 11476 | mode, |
| 11477 | angle_delta_y, |
| 11478 | uv_predict_mode, |
| 11479 | angle_delta_uv, |
| 11480 | alpha, |
| 11481 | filter_intra, |
| 11482 | smooth_neighbor, |
| 11483 | smooth_neighbor_uv, |
| 11484 | palette_y: palette_y |
| 11485 | .as_ref() |
| 11486 | .map(|p| p.map.iter().map(|&i| p.colors[i as usize]).collect()), |
| 11487 | palette_uv: palette_uv.as_ref().map(|p| { |
| 11488 | ( |
| 11489 | p.map.iter().map(|&i| p.u_colors[i as usize]).collect(), |
| 11490 | p.map.iter().map(|&i| p.v_colors[i as usize]).collect(), |
| 11491 | ) |
| 11492 | }), |
| 11493 | }; |
| 11494 | decode_rect_split( |
| 11495 | dec, |
| 11496 | cdfs, |
| 11497 | neighbours, |
| 11498 | (mi_r, mi_c), |
| 11499 | bw, |
| 11500 | bh, |
| 11501 | tx_w, |
| 11502 | tx_h, |
| 11503 | &modes, |
| 11504 | y, |
| 11505 | u, |
| 11506 | v, |
| 11507 | base_q_idx, |
| 11508 | fctx.reduced_tx_set_inter.with(std::cell::Cell::get), fctx, |
| 11509 | )?; |
| 11510 | // `set_txfm_ctxs` (libaom `decodeframe.c:1078`, at the end of |
| 11511 | // `decode_token_recon_block`): an INTRA block records its TRANSFORM size in |
| 11512 | // both `TXFM_CONTEXT` bands -- the above row over the block's width, the |
| 11513 | // left column over its height. This arm wrote neither before lane-intrasplit |
| 11514 | // r1, so every later block in the frame read a stale `tx_size`/`txfm_split` |
| 11515 | // context across the strip's footprint. |
| 11516 | for i in 0..bw / MI { |
| 11517 | if let Some(cell) = neighbours.above_txfm.get_mut(mi_c + i) { |
| 11518 | *cell = tx_w as u8; |
| 11519 | } |
| 11520 | } |
| 11521 | for i in 0..bh / MI { |
| 11522 | if let Some(cell) = neighbours.left_txfm.get_mut(mi_r + i) { |
| 11523 | *cell = tx_h as u8; |
| 11524 | } |
| 11525 | } |
| 11526 | // The strip's own palette state into the mi-granular neighbour bands, so |
| 11527 | // the next block's colour cache and `palette_y_mode` context are libaom's. |
| 11528 | record_strip_palette( |
| 11529 | neighbours, |
| 11530 | (mi_r, mi_c), |
| 11531 | bw, |
| 11532 | bh, |
| 11533 | palette_y.as_ref().map_or(0, |p| p.size), |
| 11534 | palette_y.as_ref().map_or([0u16; 8], |p| p.colors), |
| 11535 | palette_uv.as_ref().map_or(0, |p| p.size), |
| 11536 | palette_uv.as_ref().map_or([0u16; 8], |p| p.u_colors), |
| 11537 | ); |
| 11538 | let class = match bw.max(bh) { |
| 11539 | 64 => 0, |
| 11540 | 32 => 1, |
| 11541 | _ => 2, |
| 11542 | }; |
| 11543 | hit_do! { |
| 11544 | INTRA_RECT_IN_INTER_HITS.with(|h| { |
| 11545 | let mut v = h.get(); |
| 11546 | v[class] += 1; |
| 11547 | h.set(v); |
| 11548 | }); |
| 11549 | } |
| 11550 | Ok(()) |
| 11551 | } |
| 11552 | |
| 11553 | /// Reconstructs one rectangular INTRABC block, `bw`x`bh` pixels at the real |
| 11554 | /// mi `(mi_r, mi_c)` (lane-av1-intrabc). |
| 11555 | /// |
| 11556 | /// libaom treats an intrabc block as INTER for everything except the mode-info |
| 11557 | /// read itself: `is_inter_block` is `is_intrabc_block(mbmi) || ref > INTRA` |
| 11558 | /// (`blockd.h:373`), so |
| 11559 | /// |
| 11560 | /// * `parse_decode_block` reads the transform size through the INTER var-tx |
| 11561 | /// tree (`read_tx_size_vartx`) -- [`read_block_tx_size_rect`], whose |
| 11562 | /// `txfm_partition` context comes off the real `TXFM_CONTEXT` bands and |
| 11563 | /// whose leaves publish their own resolved size back; |
| 11564 | /// * `predict_inter_block` predicts the WHOLE block once, from the CURRENT |
| 11565 | /// frame's own pre-loop-filter reconstruction, at the coded block vector |
| 11566 | /// with the BILINEAR kernel `read_intrabc_info` forces (`decodemv.c:913`); |
| 11567 | /// * `decode_token_recon_block`'s inter branch then reads one residual per |
| 11568 | /// transform unit, through the INTER coefficient sets |
| 11569 | /// ([`read_inter_plane_rect`]/[`read_inter_plane`]) with the chroma plane |
| 11570 | /// inheriting the colocated luma unit's coded `tx_type`. |
| 11571 | /// |
| 11572 | /// Text order is libaom's: transform tree, prediction, luma residual, chroma |
| 11573 | /// residual, bookkeeping. A skipped block codes no residual at all -- the |
| 11574 | /// prediction is the reconstruction. |
| 11575 | #[allow(clippy::too_many_arguments)] |
| 11576 | fn decode_intrabc_rect( |
| 11577 | dec: &mut SymbolDecoder, |
| 11578 | cdfs: &mut Cdfs, |
| 11579 | neighbours: &mut Neighbours, |
| 11580 | (mi_r, mi_c): (usize, usize), |
| 11581 | (bw, bh): (usize, usize), |
| 11582 | skip: bool, |
| 11583 | (dv_row, dv_col): (i32, i32), |
| 11584 | y: &mut PlaneBuf<'static>, |
| 11585 | u: &mut PlaneBuf<'static>, |
| 11586 | v: &mut PlaneBuf<'static>, |
| 11587 | base_q_idx: u8, |
| 11588 | fctx: &crate::decode::FrameCtx, |
| 11589 | ) -> Result<()> { |
| 11590 | hit!(INTRABC_RECT_HITS); |
| 11591 | // [horz, vert]: see `INTRABC_RECT_CODED_HITS` / `INTRABC_RECT_SKIPPED_HITS`. |
| 11592 | let idx = usize::from(bh > bw); |
| 11593 | if skip { |
| 11594 | INTRABC_RECT_SKIPPED_HITS.with(|c| { |
| 11595 | let mut seen = c.get(); |
| 11596 | seen[idx] += 1; |
| 11597 | c.set(seen); |
| 11598 | }); |
| 11599 | } else { |
| 11600 | INTRABC_RECT_CODED_HITS.with(|c| { |
| 11601 | let mut seen = c.get(); |
| 11602 | seen[idx] += 1; |
| 11603 | c.set(seen); |
| 11604 | }); |
| 11605 | } |
| 11606 | let (px, py) = (mi_c * MI, mi_r * MI); |
| 11607 | let (cpx, cpy) = (px / 2, py / 2); |
| 11608 | // `av1_get_max_uv_txsize(bw x bh)`: 4:2:0 halves the footprint and the |
| 11609 | // halved shape's own `max_txsize_rect_lookup` entry IS that size for every |
| 11610 | // shape this helper is called at, so the uv plane block is ONE unit -- |
| 11611 | // the same call `decode_token_recon_block`'s own chroma loop makes. |
| 11612 | let (cw, ch) = (bw / 2, bh / 2); |
| 11613 | // lane-av1-intrabc r2: the square stride the proven INTER path uses |
| 11614 | // (`side`). The residual grids, the prediction windows AND |
| 11615 | // `TxParams::stride` are all laid out at it -- the block's own width |
| 11616 | // panicked `TxParams::run` on every coded VERT strip (8x16/16x32: `h` |
| 11617 | // exceeds the stride). |
| 11618 | let side = bw.max(bh); |
| 11619 | let cside = cw.max(ch); |
| 11620 | // `av1_calc_mi_size`'s 8-px-aligned grid: a block hanging over the right |
| 11621 | // or bottom frame edge reads no transform-tree symbol for the units |
| 11622 | // outside it. |
| 11623 | let mi_cols = 2 * y.true_width.div_ceil(8); |
| 11624 | let mi_rows = 2 * y.true_height.div_ceil(8); |
| 11625 | let leaves = read_block_tx_size_rect( |
| 11626 | dec, |
| 11627 | cdfs, |
| 11628 | neighbours, |
| 11629 | (mi_r, mi_c), |
| 11630 | (bw, bh), |
| 11631 | (mi_cols, mi_rows), |
| 11632 | skip, fctx, |
| 11633 | )?; |
| 11634 | if leaves.is_some() { |
| 11635 | hit!(INTRABC_RECT_VARTX_HITS); |
| 11636 | } |
| 11637 | // The whole-block prediction: `predict_inter_block` runs once, before any |
| 11638 | // coefficient is read. `flush_recon` first -- the source is this frame's |
| 11639 | // own reconstruction, including the writes this superblock has queued. |
| 11640 | flush_recon(fctx, y, u, v); |
| 11641 | // `mc::predict_with_filter` writes a DENSE window; the residual machinery |
| 11642 | // reads it at the square stride, so re-lay it -- the same step the inter |
| 11643 | // path takes for its own square prediction buffer. |
| 11644 | let mut pred_y_dense = vec![0u16; bw * bh]; |
| 11645 | mc::predict_with_filter( |
| 11646 | &y.data, y.width, y.true_width, y.true_height, |
| 11647 | mv_to_q4(px, dv_col, 0), mv_to_q4(py, dv_row, 0), |
| 11648 | bw, bh, mc::InterpFilterKind::Bilinear, &mut pred_y_dense, fctx, |
| 11649 | ); |
| 11650 | let mut pred_y = vec![0u16; side * side]; |
| 11651 | for row in 0..bh { |
| 11652 | pred_y[row * side..][..bw].copy_from_slice(&pred_y_dense[row * bw..][..bw]); |
| 11653 | } |
| 11654 | let mut pred_u_dense = vec![0u16; cw * ch]; |
| 11655 | mc::predict_with_filter( |
| 11656 | &u.data, u.width, u.true_width, u.true_height, |
| 11657 | mv_to_q4(cpx, dv_col, ss_x(fctx)), mv_to_q4(cpy, dv_row, ss_y(fctx)), |
| 11658 | cw, ch, mc::InterpFilterKind::Bilinear, &mut pred_u_dense, fctx, |
| 11659 | ); |
| 11660 | let mut pred_u = vec![0u16; cside * cside]; |
| 11661 | for row in 0..ch { |
| 11662 | pred_u[row * cside..][..cw].copy_from_slice(&pred_u_dense[row * cw..][..cw]); |
| 11663 | } |
| 11664 | let mut pred_v_dense = vec![0u16; cw * ch]; |
| 11665 | mc::predict_with_filter( |
| 11666 | &v.data, v.width, v.true_width, v.true_height, |
| 11667 | mv_to_q4(cpx, dv_col, ss_x(fctx)), mv_to_q4(cpy, dv_row, ss_y(fctx)), |
| 11668 | cw, ch, mc::InterpFilterKind::Bilinear, &mut pred_v_dense, fctx, |
| 11669 | ); |
| 11670 | let mut pred_v = vec![0u16; cside * cside]; |
| 11671 | for row in 0..ch { |
| 11672 | pred_v[row * cside..][..cw].copy_from_slice(&pred_v_dense[row * cw..][..cw]); |
| 11673 | } |
| 11674 | let reduced = fctx.reduced_tx_set_inter.with(std::cell::Cell::get); |
| 11675 | let luma_tx_type: TxType; |
| 11676 | let (luma_grid, u_grid, v_grid); |
| 11677 | if skip { |
| 11678 | // No residual syntax at all: the prediction IS the block. |
| 11679 | // Residual stride is `side` (and `cside`), not `bw`: reconstruct_mc_rect indexes |
| 11680 | // `residual[row * side .. row*side + w]` for row in 0..h, so a skipped VERT strip |
| 11681 | // (bh > bw) overruns a `bw * bh` slice. Bug-for-bug with the inter skip arm. |
| 11682 | push_mc_rect(0, px, py, side, bw, bh, Pred::Inline(&pred_y, side), &ZERO_RESIDUAL[..side * side], fctx); |
| 11683 | push_mc_rect(1, cpx, cpy, cside, cw, ch, Pred::Inline(&pred_u, cside), &ZERO_RESIDUAL[..cside * cside], fctx); |
| 11684 | push_mc_rect(2, cpx, cpy, cside, cw, ch, Pred::Inline(&pred_v, cside), &ZERO_RESIDUAL[..cside * cside], fctx); |
| 11685 | if let Some(ls) = leaves.as_ref() { |
| 11686 | // lane-av1-loss64, merged: the same reset [`decode_intrabc_owned_rect`]'s |
| 11687 | // skip arm performs -- libaom `av1_reset_entropy_context` |
| 11688 | // (`if (mbmi->skip_txfm)`, decodeframe.c:1262 -> blockd.c:58): a |
| 11689 | // SKIPPED block zeroes its whole entropy-band footprint. This |
| 11690 | // helper serves `decode_block_rect4`'s 1:4 strips on this tree, |
| 11691 | // so a skipped lossless strip whose `leaves` resolved (every |
| 11692 | // lossless frame -- `read_block_tx_size_rect`'s lossless arm |
| 11693 | // returns the 4x4 grid before it looks at `skip`) left the |
| 11694 | // PREVIOUS occupant's luma bands standing and the NEXT block's |
| 11695 | // partition parse diverged from libaom's: the 320x242 lossless |
| 11696 | // sb128 gate read a phantom unskipped 8x32 intrabc strip at |
| 11697 | // mi(56,66) right after the skipped 8x16 one at mi(48,66). |
| 11698 | hit!(SKIP_LOSSLESS_BAND_RESET_HITS); |
| 11699 | for &(row, col, tw, th) in ls { |
| 11700 | neighbours.record_mi_luma_rect( |
| 11701 | (mi_r + row, mi_c + col), |
| 11702 | tw, |
| 11703 | th, |
| 11704 | &ZERO_RESIDUAL[..tw * th], |
| 11705 | ); |
| 11706 | } |
| 11707 | } |
| 11708 | luma_tx_type = TxType::DctDct; |
| 11709 | luma_grid = Grid::Zero(side * side); |
| 11710 | u_grid = Grid::Zero(cside * cside); |
| 11711 | v_grid = Grid::Zero(cside * cside); |
| 11712 | } else { |
| 11713 | match leaves.as_ref() { |
| 11714 | None => { |
| 11715 | // One whole-block rectangular transform unit. |
| 11716 | let around = neighbours.around_mi_rect((mi_r, mi_c), bw, bh); |
| 11717 | let (grid, tx_type) = read_inter_plane_rect( |
| 11718 | dec, |
| 11719 | cdfs, |
| 11720 | rect_inter_luma_set(bw, bh, fctx)?, |
| 11721 | (bw, bh), |
| 11722 | side, |
| 11723 | 0, |
| 11724 | around[0], |
| 11725 | 0, |
| 11726 | y, |
| 11727 | px, |
| 11728 | py, |
| 11729 | Pred::Inline(&pred_y, side), |
| 11730 | None, |
| 11731 | None, fctx, |
| 11732 | )?; |
| 11733 | luma_tx_type = tx_type; |
| 11734 | luma_grid = embed_grid_stride(&grid, bw, bh, side); |
| 11735 | let (ug, _) = read_inter_plane_rect( |
| 11736 | dec, |
| 11737 | cdfs, |
| 11738 | rect_inter_chroma_set(cw, ch)?, |
| 11739 | (cw, ch), |
| 11740 | cside, |
| 11741 | 1, |
| 11742 | around[1], |
| 11743 | 0, |
| 11744 | u, |
| 11745 | cpx, |
| 11746 | cpy, |
| 11747 | Pred::Inline(&pred_u, cside), |
| 11748 | Some(luma_tx_type), |
| 11749 | None, fctx, |
| 11750 | )?; |
| 11751 | u_grid = embed_grid_stride(&ug, cw, ch, cside); |
| 11752 | let (vg, _) = read_inter_plane_rect( |
| 11753 | dec, |
| 11754 | cdfs, |
| 11755 | rect_inter_chroma_set(cw, ch)?, |
| 11756 | (cw, ch), |
| 11757 | cside, |
| 11758 | 2, |
| 11759 | around[2], |
| 11760 | 0, |
| 11761 | v, |
| 11762 | cpx, |
| 11763 | cpy, |
| 11764 | Pred::Inline(&pred_v, cside), |
| 11765 | Some(luma_tx_type), |
| 11766 | None, fctx, |
| 11767 | )?; |
| 11768 | v_grid = embed_grid_stride(&vg, cw, ch, cside); |
| 11769 | } |
| 11770 | Some(leaves) => { |
| 11771 | // P2 (lane-av1-intrabc r2): libaom `get_transform_size` applies |
| 11772 | // the split override to LUMA only, per leaf -- the step the |
| 11773 | // proven inter var-tx path applies right after its own residual |
| 11774 | // loop ([`Neighbours::fill_lf_grid_leaf_luma`]). Without it the |
| 11775 | // leaf's own transform edges are never filtered. |
| 11776 | for &(row, col, tw, th) in leaves.iter() { |
| 11777 | neighbours.fill_lf_grid_leaf_luma( |
| 11778 | (mi_r + row, mi_c + col), |
| 11779 | tw / MI, |
| 11780 | th / MI, |
| 11781 | tw as u8, |
| 11782 | th as u8, |
| 11783 | ); |
| 11784 | } |
| 11785 | // One residual per transform unit, in the order the tree coded |
| 11786 | // them; each unit reads its own neighbour-magnitude context off |
| 11787 | // the units already decoded before it, and every unit adds onto |
| 11788 | // its own window of the block's single prediction. |
| 11789 | let mut first_tx_type = TxType::DctDct; |
| 11790 | for (idx, &(row, col, tw, th)) in leaves.iter().enumerate() { |
| 11791 | let tu_mi = (mi_r + row, mi_c + col); |
| 11792 | let (tu_px, tu_py) = (px + col * MI, py + row * MI); |
| 11793 | let tu_pred = Pred::Inline(&pred_y, side).offset(row * MI * side + col * MI); |
| 11794 | let (tu_grid, tu_tx_type) = if tw == th { |
| 11795 | let scan = default_scan(tw.min(32)); |
| 11796 | let tu_skip_ctx = neighbours.luma_skip_ctx(tu_mi, tw / MI); |
| 11797 | read_inter_plane( |
| 11798 | dec, |
| 11799 | cdfs, |
| 11800 | inter_txbset_for(tw, reduced), |
| 11801 | scan, |
| 11802 | 0, |
| 11803 | neighbours.around_mi(tu_mi, tw)[0], |
| 11804 | 0, |
| 11805 | y, |
| 11806 | tu_px, |
| 11807 | tu_py, |
| 11808 | tw, |
| 11809 | base_q_idx, |
| 11810 | tu_pred, |
| 11811 | None, |
| 11812 | Some(tu_skip_ctx), fctx, |
| 11813 | )? |
| 11814 | } else { |
| 11815 | let tu_skip_ctx = neighbours.luma_skip_ctx_rect(tu_mi, tw / MI, th / MI); |
| 11816 | read_inter_plane_rect( |
| 11817 | dec, |
| 11818 | cdfs, |
| 11819 | rect_inter_luma_set(tw, th, fctx)?, |
| 11820 | (tw, th), |
| 11821 | side, |
| 11822 | 0, |
| 11823 | neighbours.around_mi_rect(tu_mi, tw, th)[0], |
| 11824 | 0, |
| 11825 | y, |
| 11826 | tu_px, |
| 11827 | tu_py, |
| 11828 | tu_pred, |
| 11829 | None, |
| 11830 | Some(tu_skip_ctx), fctx, |
| 11831 | )? |
| 11832 | }; |
| 11833 | if idx == 0 { |
| 11834 | first_tx_type = tu_tx_type; |
| 11835 | } |
| 11836 | neighbours.record_mi_luma_rect(tu_mi, tw, th, &tu_grid); |
| 11837 | } |
| 11838 | // `av1_get_tx_type`'s chroma lookup lands on the colocated |
| 11839 | // LUMA unit, which for a split block is the top-left one. |
| 11840 | luma_tx_type = first_tx_type; |
| 11841 | let around = neighbours.around_mi_rect((mi_r, mi_c), bw, bh); |
| 11842 | let (ug, _) = read_inter_plane_rect( |
| 11843 | dec, |
| 11844 | cdfs, |
| 11845 | rect_inter_chroma_set(cw, ch)?, |
| 11846 | (cw, ch), |
| 11847 | cside, |
| 11848 | 1, |
| 11849 | around[1], |
| 11850 | 0, |
| 11851 | u, |
| 11852 | cpx, |
| 11853 | cpy, |
| 11854 | Pred::Inline(&pred_u, cside), |
| 11855 | Some(luma_tx_type), |
| 11856 | None, fctx, |
| 11857 | )?; |
| 11858 | u_grid = embed_grid_stride(&ug, cw, ch, cside); |
| 11859 | let (vg, _) = read_inter_plane_rect( |
| 11860 | dec, |
| 11861 | cdfs, |
| 11862 | rect_inter_chroma_set(cw, ch)?, |
| 11863 | (cw, ch), |
| 11864 | cside, |
| 11865 | 2, |
| 11866 | around[2], |
| 11867 | 0, |
| 11868 | v, |
| 11869 | cpx, |
| 11870 | cpy, |
| 11871 | Pred::Inline(&pred_v, cside), |
| 11872 | Some(luma_tx_type), |
| 11873 | None, fctx, |
| 11874 | )?; |
| 11875 | v_grid = embed_grid_stride(&vg, cw, ch, cside); |
| 11876 | luma_grid = Grid::Zero(side * side); |
| 11877 | } |
| 11878 | } |
| 11879 | } |
| 11880 | let _ = luma_tx_type; |
| 11881 | // Bookkeeping. An intrabc block publishes mode `DC_PRED` everywhere the |
| 11882 | // next block reads a neighbour mode, its own block size in both `TXFM` |
| 11883 | // bands (already done by `read_block_tx_size_rect`), a zeroed palette |
| 11884 | // state (a stale entry would feed the next block's colour cache), and its |
| 11885 | // DV into the intrabc mi grid the next block's DV predictor reads. |
| 11886 | if leaves.is_some() { |
| 11887 | neighbours.record_split_luma_rect_mi( |
| 11888 | (mi_r, mi_c), bw, bh, DC_PRED, DC_PRED, [&u_grid, &v_grid], |
| 11889 | ); |
| 11890 | } else { |
| 11891 | neighbours.record_rect_mi( |
| 11892 | (mi_r, mi_c), bw, bh, DC_PRED, DC_PRED, &[&luma_grid, &u_grid, &v_grid], |
| 11893 | ); |
| 11894 | } |
| 11895 | neighbours.record_palette_y_rect((mi_r, mi_c), bw, bh, 0, [0u16; 8]); |
| 11896 | neighbours.record_palette_uv_rect((mi_r, mi_c), bw, bh, 0, [0u16; 8]); |
| 11897 | neighbours.fill_skip_grid_rect((mi_r, mi_c), bw / MI, bh / MI, skip); |
| 11898 | neighbours.fill_lf_grid_rect( |
| 11899 | (mi_r, mi_c), |
| 11900 | bw / MI, |
| 11901 | bh / MI, |
| 11902 | bw.min(64) as u8, |
| 11903 | bh.min(64) as u8, |
| 11904 | 0, fctx, |
| 11905 | ); |
| 11906 | record_intrabc_mi_rect( |
| 11907 | mi_r, |
| 11908 | mi_c, |
| 11909 | bw / MI, |
| 11910 | bh / MI, |
| 11911 | Some((dv_row, dv_col)), |
| 11912 | neighbours, |
| 11913 | fctx, |
| 11914 | ); |
| 11915 | Ok(()) |
| 11916 | } |
| 11917 | |
| 11918 | /// lane-av1-ibcrect: reconstruction for a `use_intrabc` block on a 128-root |
| 11919 | /// HORZ/VERT strip (`BLOCK_128X64` / `BLOCK_64X128`) -- the shape |
| 11920 | /// `decode_block_128rect` refused by name before this port. libaom decodes |
| 11921 | /// such a block through the INTER machinery (an intrabc block |
| 11922 | /// `is_inter_block`): `predict_inter_block` copies the whole block from this |
| 11923 | /// frame's own reconstruction at the integer block vector once, then |
| 11924 | /// `decode_token_recon_block`'s inter arm walks 64x64 "mu" chunks -- |
| 11925 | /// per chunk every plane, luma units at the block's own resolved transform |
| 11926 | /// (`read_block_tx_size_rect`, TX_64X64 roots) and chroma at |
| 11927 | /// `av1_get_max_uv_txsize(BLOCK_128X64)` = TX_32X32, one unit per plane per |
| 11928 | /// chunk at 4:2:0. Chroma never codes its own `tx_type`; each unit inherits |
| 11929 | /// the coded type of the luma leaf covering its quadrant (`av1_get_tx_type`, |
| 11930 | /// blockd.h:1291). `mode`/`uv_mode` read as `DC_PRED`/`UV_DC_PRED` back in |
| 11931 | /// [`read_intra_mode_rect`]; no palette/filter-intra/CFL symbol exists here. |
| 11932 | #[allow(clippy::too_many_arguments)] |
| 11933 | fn decode_intrabc_128rect( |
| 11934 | dec: &mut SymbolDecoder, |
| 11935 | cdfs: &mut Cdfs, |
| 11936 | neighbours: &mut Neighbours, |
| 11937 | (mi_r, mi_c): (usize, usize), |
| 11938 | (bw, bh): (usize, usize), |
| 11939 | skip: bool, |
| 11940 | (dv_row, dv_col): (i32, i32), |
| 11941 | y: &mut PlaneBuf<'static>, |
| 11942 | u: &mut PlaneBuf<'static>, |
| 11943 | v: &mut PlaneBuf<'static>, |
| 11944 | base_q_idx: u8, |
| 11945 | fctx: &crate::decode::FrameCtx, |
| 11946 | ) -> Result<()> { |
| 11947 | hit!(INTRABC_128RECT_HITS); |
| 11948 | if crate::envflags::env_flag!("EC_AV1_TRACE") { |
| 11949 | let (rng, _) = dec.debug_state(); |
| 11950 | eprintln!( |
| 11951 | "EC_IBC128 mi_row={mi_r} mi_col={mi_c} bw={bw} bh={bh} skip={skip} dv=({dv_row},{dv_col}) rng={rng}" |
| 11952 | ); |
| 11953 | } |
| 11954 | let (px, py) = (mi_c * MI, mi_r * MI); |
| 11955 | let (cpx, cpy) = (px / 2, py / 2); |
| 11956 | let (cw, ch) = (bw / 2, bh / 2); |
| 11957 | // The square strides the proven INTER path lays its residual grids, |
| 11958 | // prediction windows and `TxParams` at (see [`decode_intrabc_rect`]'s r2 |
| 11959 | // note): 128/64 here. |
| 11960 | let side = bw.max(bh); |
| 11961 | let cside = cw.max(ch); |
| 11962 | // `av1_calc_mi_size`'s 8-px-aligned grid: units past the frame edge code |
| 11963 | // nothing (`decode_reconstruct_tx`'s max_blocks_* early return). |
| 11964 | let mi_cols = 2 * y.true_width.div_ceil(8); |
| 11965 | let mi_rows = 2 * y.true_height.div_ceil(8); |
| 11966 | // An intrabc block is is_inter_block: the tx tree is the INTER one, and |
| 11967 | // [`read_block_tx_size_rect`]'s 128-root branch already carries the |
| 11968 | // LARGEST (no symbol, two TX_64X64 units), TX_MODE_SELECT (var-tx tree |
| 11969 | // rooted at TX_64X64), skip (band stamp only) and lossless (TX_4X4 grid) |
| 11970 | // arms (lane-sb128c r7). |
| 11971 | let leaves = read_block_tx_size_rect( |
| 11972 | dec, |
| 11973 | cdfs, |
| 11974 | neighbours, |
| 11975 | (mi_r, mi_c), |
| 11976 | (bw, bh), |
| 11977 | (mi_cols, mi_rows), |
| 11978 | skip, fctx, |
| 11979 | )?; |
| 11980 | // Whole-block prediction, once, before any coefficient is read |
| 11981 | // (`predict_inter_block`); the source is this frame's own |
| 11982 | // reconstruction, including the writes this superblock has queued. |
| 11983 | flush_recon(fctx, y, u, v); |
| 11984 | let mut pred_y_dense = vec![0u16; bw * bh]; |
| 11985 | mc::predict_with_filter( |
| 11986 | &y.data, y.width, y.true_width, y.true_height, |
| 11987 | mv_to_q4(px, dv_col, 0), mv_to_q4(py, dv_row, 0), |
| 11988 | bw, bh, mc::InterpFilterKind::Bilinear, &mut pred_y_dense, fctx, |
| 11989 | ); |
| 11990 | let mut pred_y = vec![0u16; side * side]; |
| 11991 | for row in 0..bh { |
| 11992 | pred_y[row * side..][..bw].copy_from_slice(&pred_y_dense[row * bw..][..bw]); |
| 11993 | } |
| 11994 | let mut pred_u_dense = vec![0u16; cw * ch]; |
| 11995 | mc::predict_with_filter( |
| 11996 | &u.data, u.width, u.true_width, u.true_height, |
| 11997 | mv_to_q4(cpx, dv_col, ss_x(fctx)), mv_to_q4(cpy, dv_row, ss_y(fctx)), |
| 11998 | cw, ch, mc::InterpFilterKind::Bilinear, &mut pred_u_dense, fctx, |
| 11999 | ); |
| 12000 | let mut pred_u = vec![0u16; cside * cside]; |
| 12001 | for row in 0..ch { |
| 12002 | pred_u[row * cside..][..cw].copy_from_slice(&pred_u_dense[row * cw..][..cw]); |
| 12003 | } |
| 12004 | let mut pred_v_dense = vec![0u16; cw * ch]; |
| 12005 | mc::predict_with_filter( |
| 12006 | &v.data, v.width, v.true_width, v.true_height, |
| 12007 | mv_to_q4(cpx, dv_col, ss_x(fctx)), mv_to_q4(cpy, dv_row, ss_y(fctx)), |
| 12008 | cw, ch, mc::InterpFilterKind::Bilinear, &mut pred_v_dense, fctx, |
| 12009 | ); |
| 12010 | let mut pred_v = vec![0u16; cside * cside]; |
| 12011 | for row in 0..ch { |
| 12012 | pred_v[row * cside..][..cw].copy_from_slice(&pred_v_dense[row * cw..][..cw]); |
| 12013 | } |
| 12014 | let (luma_grid, u_grid, v_grid); |
| 12015 | // lane-av1-ibcrect: whether the coded arm stamped its CHROMA neighbour |
| 12016 | // contexts per transform unit -- the replay after this fn's end-of-block |
| 12017 | // record restores them (the record itself re-stamps both planes with ONE |
| 12018 | // composed state, class `override-slot-on-one-arm`). |
| 12019 | let mut mu_chroma = false; |
| 12020 | if skip { |
| 12021 | // No residual syntax at all: the prediction IS the block. Strides are |
| 12022 | // `side`/`cside`, not `bw` -- `reconstruct_mc_rect` indexes |
| 12023 | // `row * stride + w` for row in 0..h (the r5 VERT-strip overrun). |
| 12024 | push_mc_rect(0, px, py, side, bw, bh, Pred::Inline(&pred_y, side), &ZERO_RESIDUAL[..side * side], fctx); |
| 12025 | push_mc_rect(1, cpx, cpy, cside, cw, ch, Pred::Inline(&pred_u, cside), &ZERO_RESIDUAL[..cside * cside], fctx); |
| 12026 | push_mc_rect(2, cpx, cpy, cside, cw, ch, Pred::Inline(&pred_v, cside), &ZERO_RESIDUAL[..cside * cside], fctx); |
| 12027 | if let Some(ls) = leaves.as_ref() { |
| 12028 | // A LOSSLESS frame's `read_block_tx_size_rect` resolves the TX_4X4 |
| 12029 | // grid before it looks at `skip`; the skipped block still zeroes |
| 12030 | // its entropy-band footprint over every unit (libaom |
| 12031 | // `av1_reset_entropy_context` under `if (mbmi->skip_txfm)`) -- |
| 12032 | // the same arm [`decode_intrabc_rect`]'s loss64 merge added. |
| 12033 | hit!(SKIP_LOSSLESS_BAND_RESET_HITS); |
| 12034 | for &(row, col, tw, th) in ls { |
| 12035 | neighbours.record_mi_luma_rect( |
| 12036 | (mi_r + row, mi_c + col), |
| 12037 | tw, |
| 12038 | th, |
| 12039 | &ZERO_RESIDUAL[..tw * th], |
| 12040 | ); |
| 12041 | } |
| 12042 | } |
| 12043 | luma_grid = Grid::Zero(side * side); |
| 12044 | u_grid = Grid::Zero(cside * cside); |
| 12045 | v_grid = Grid::Zero(cside * cside); |
| 12046 | } else { |
| 12047 | let leaves = leaves.expect( |
| 12048 | "read_block_tx_size_rect returns None only for skip below 128 at this shape", |
| 12049 | ); |
| 12050 | // libaom `get_transform_size`'s split override applies to LUMA only, |
| 12051 | // per leaf -- each unit's own transform edges get filtered |
| 12052 | // ([`decode_intrabc_rect`]'s P2, the proven inter path's step). |
| 12053 | for &(row, col, tw, th) in leaves.iter() { |
| 12054 | neighbours.fill_lf_grid_leaf_luma( |
| 12055 | (mi_r + row, mi_c + col), |
| 12056 | tw / MI, |
| 12057 | th / MI, |
| 12058 | tw as u8, |
| 12059 | th as u8, |
| 12060 | ); |
| 12061 | } |
| 12062 | let reduced = fctx.reduced_tx_set_inter.with(std::cell::Cell::get); |
| 12063 | let mut u_units: Vec<i32> = Vec::new(); |
| 12064 | let mut v_units: Vec<i32> = Vec::new(); |
| 12065 | let mut leaf_tx_types: Vec<(usize, usize, usize, usize, TxType)> = Vec::new(); |
| 12066 | // Frame-edge clip of the mu walk (`max_block_wide/high`): a unit whose |
| 12067 | // top-left sample is outside the frame codes nothing. |
| 12068 | let max_w_mi = (bw / MI).min(mi_cols.saturating_sub(mi_c)); |
| 12069 | let max_h_mi = (bh / MI).min(mi_rows.saturating_sub(mi_r)); |
| 12070 | for (idx, &(row, col, tw, th)) in leaves.iter().enumerate() { |
| 12071 | debug_assert_eq!(tw, th, "a square TX_64X64 tree root only yields square leaves"); |
| 12072 | let tu_mi = (mi_r + row, mi_c + col); |
| 12073 | let (tu_px, tu_py) = (px + col * MI, py + row * MI); |
| 12074 | if tu_px >= y.true_width || tu_py >= y.true_height { |
| 12075 | continue; |
| 12076 | } |
| 12077 | let tu_pred = Pred::Inline(&pred_y, side).offset(row * MI * side + col * MI); |
| 12078 | let scan = default_scan(tw.min(32)); |
| 12079 | let tu_skip_ctx = neighbours.luma_skip_ctx(tu_mi, tw / MI); |
| 12080 | let (tu_grid, tu_tx_type) = read_inter_plane( |
| 12081 | dec, |
| 12082 | cdfs, |
| 12083 | inter_txbset_for(tw, reduced), |
| 12084 | &scan, |
| 12085 | 0, |
| 12086 | neighbours.around_mi(tu_mi, tw)[0], |
| 12087 | 0, |
| 12088 | y, |
| 12089 | tu_px, |
| 12090 | tu_py, |
| 12091 | tw, |
| 12092 | base_q_idx, |
| 12093 | tu_pred, |
| 12094 | None, |
| 12095 | Some(tu_skip_ctx), fctx, |
| 12096 | )?; |
| 12097 | leaf_tx_types.push((row, col, tw, th, tu_tx_type)); |
| 12098 | neighbours.record_mi_luma_rect(tu_mi, tw, th, &tu_grid); |
| 12099 | // libaom `decode_token_recon_block` codes EVERY PLANE of a 64x64 |
| 12100 | // mu chunk before the next one starts: once this chunk's leaves |
| 12101 | // are done, its chroma is read immediately (the inter 128 path's |
| 12102 | // interleave). |
| 12103 | let chunk = (row / 16, col / 16); |
| 12104 | let done = idx + 1 == leaves.len() |
| 12105 | || (leaves[idx + 1].0 / 16, leaves[idx + 1].1 / 16) != chunk; |
| 12106 | if done { |
| 12107 | let (cr, cc) = chunk; |
| 12108 | // `av1_get_max_uv_txsize(BLOCK_128X64)` = TX_32X32: one |
| 12109 | // TX_32X32 chroma unit per plane per chunk at 4:2:0, raster |
| 12110 | // within the chunk, plane-major inside the chunk. |
| 12111 | let cu_tx = 32usize; |
| 12112 | let chunk_chroma = cside * 64 / side; |
| 12113 | let unit_luma = cu_tx * (side / cside); |
| 12114 | let chunk_w_mi = 16.min(max_w_mi.saturating_sub(cc * 16)); |
| 12115 | let chunk_h_mi = 16.min(max_h_mi.saturating_sub(cr * 16)); |
| 12116 | let units_x = chunk_w_mi / (unit_luma / MI); |
| 12117 | let units_y = chunk_h_mi / (unit_luma / MI); |
| 12118 | if lossless(fctx) { |
| 12119 | // TX_4X4 chroma units over this chunk (lane-lossless2's |
| 12120 | // walk, shared with the inter 128 path). |
| 12121 | read_inter_chroma_lossless( |
| 12122 | dec, cdfs, neighbours, u, v, |
| 12123 | Pred::Inline(&pred_u, cside), Pred::Inline(&pred_v, cside), |
| 12124 | (mi_r, mi_c), (cpx, cpy), |
| 12125 | (cc * chunk_chroma, cr * chunk_chroma), |
| 12126 | (chunk_chroma, chunk_chroma), |
| 12127 | (cw, ch), |
| 12128 | cside, 0, base_q_idx, |
| 12129 | &mut u_units, &mut v_units, fctx, |
| 12130 | )?; |
| 12131 | // lane-av1-ibcrect: read_inter_chroma_lossless stamped |
| 12132 | // every TX_4X4 unit's own context (`record_mi_chroma` |
| 12133 | // inside the walk) -- the end-of-block record below must |
| 12134 | // not overwrite them with the composed grid (the inter |
| 12135 | // 128 path sets the same flag on this arm). |
| 12136 | mu_chroma = true; |
| 12137 | continue; |
| 12138 | } |
| 12139 | let cu_scan = default_scan(cu_tx); |
| 12140 | for plane_idx in 1..3 { |
| 12141 | let (src, buf) = if plane_idx == 1 { |
| 12142 | (Pred::Inline(&pred_u, cside), &mut *u) |
| 12143 | } else { |
| 12144 | (Pred::Inline(&pred_v, cside), &mut *v) |
| 12145 | }; |
| 12146 | for ur in 0..units_y { |
| 12147 | for uc in 0..units_x { |
| 12148 | let unit_mi = ( |
| 12149 | mi_r + cr * 16 + ur * (unit_luma / MI), |
| 12150 | mi_c + cc * 16 + uc * (unit_luma / MI), |
| 12151 | ); |
| 12152 | // Per unit, AFTER the earlier units' stamps: this |
| 12153 | // unit's coefficient context must see them. |
| 12154 | let cu_around = neighbours.around_mi(unit_mi, unit_luma); |
| 12155 | if crate::envflags::env_flag!("EC_ECDUMP") { |
| 12156 | for p in 1..3 { |
| 12157 | let ab: Vec<String> = (0..unit_luma / MI) |
| 12158 | .map(|k| { |
| 12159 | format!( |
| 12160 | "{:?}/{}", |
| 12161 | neighbours.above[unit_mi.1 + k][p].dc, |
| 12162 | neighbours.above[unit_mi.1 + k][p].level |
| 12163 | ) |
| 12164 | }) |
| 12165 | .collect(); |
| 12166 | let lf: Vec<String> = (0..unit_luma / MI) |
| 12167 | .map(|k| { |
| 12168 | format!( |
| 12169 | "{:?}/{}", |
| 12170 | neighbours.left[unit_mi.0 + k][p].dc, |
| 12171 | neighbours.left[unit_mi.0 + k][p].level |
| 12172 | ) |
| 12173 | }) |
| 12174 | .collect(); |
| 12175 | eprintln!( |
| 12176 | "EC_IBC128_BANDS mi=({mi_r},{mi_c}) unit=({cr},{cc}) plane={p} ctx=({},{},{}) above=[{}] left=[{}]", |
| 12177 | cu_around[p].0, cu_around[p].1, cu_around[p].2, ab.join(","), lf.join(",") |
| 12178 | ); |
| 12179 | } |
| 12180 | } |
| 12181 | let (cu_x, cu_y) = ( |
| 12182 | cpx + cc * chunk_chroma + uc * cu_tx, |
| 12183 | cpy + cr * chunk_chroma + ur * cu_tx, |
| 12184 | ); |
| 12185 | // `av1_get_tx_type` (blockd.h:1291): an inter |
| 12186 | // block's chroma unit never codes a tx_type; it |
| 12187 | // inherits the coded type of the luma leaf |
| 12188 | // covering its own quadrant (all of this chunk's |
| 12189 | // leaves are read by now). |
| 12190 | let unit_rel_mi = ( |
| 12191 | unit_mi.0 - mi_r, |
| 12192 | unit_mi.1 - mi_c, |
| 12193 | ); |
| 12194 | let covering = leaf_tx_types |
| 12195 | .iter() |
| 12196 | .copied() |
| 12197 | .find(|&(lr, lc, lw, lh, _)| { |
| 12198 | lr <= unit_rel_mi.0 |
| 12199 | && unit_rel_mi.0 < lr + lh / MI |
| 12200 | && lc <= unit_rel_mi.1 |
| 12201 | && unit_rel_mi.1 < lc + lw / MI |
| 12202 | }) |
| 12203 | .map(|(_, _, _, _, t)| t); |
| 12204 | let cu_pred = src.offset( |
| 12205 | (cr * chunk_chroma + ur * cu_tx) * cside |
| 12206 | + cc * chunk_chroma |
| 12207 | + uc * cu_tx, |
| 12208 | ); |
| 12209 | let cu_grid = read_inter_plane( |
| 12210 | dec, |
| 12211 | cdfs, |
| 12212 | TxbSet::Chroma32, |
| 12213 | &cu_scan, |
| 12214 | plane_idx, |
| 12215 | cu_around[plane_idx], |
| 12216 | 0, |
| 12217 | buf, |
| 12218 | cu_x, |
| 12219 | cu_y, |
| 12220 | cu_tx, |
| 12221 | base_q_idx, |
| 12222 | cu_pred, |
| 12223 | Some(covering.unwrap_or(TxType::DctDct)), |
| 12224 | Some(3), fctx, |
| 12225 | )? |
| 12226 | .0; |
| 12227 | // Immediately, so the NEXT unit reads this one's |
| 12228 | // coefficient context. |
| 12229 | neighbours.record_mi_chroma( |
| 12230 | unit_mi, |
| 12231 | unit_luma, |
| 12232 | unit_luma, |
| 12233 | plane_idx, |
| 12234 | &cu_grid, |
| 12235 | ); |
| 12236 | if crate::envflags::env_flag!("EC_ECDUMP") { |
| 12237 | let st = crate::decode::neighbour_state(&cu_grid); |
| 12238 | eprintln!( |
| 12239 | "EC_IBC128_STAMP mi=({mi_r},{mi_c}) unit=({cr},{cc}) plane={plane_idx} state=({:?},{})", |
| 12240 | st.dc, st.level |
| 12241 | ); |
| 12242 | } |
| 12243 | hit!(CHROMA_SPLIT_TX_HITS); |
| 12244 | // lane-av1-ibcrect: per-unit chroma context -- the |
| 12245 | // end-of-block record's composed-grid re-stamp |
| 12246 | // must not stand (replayed below). |
| 12247 | mu_chroma = true; |
| 12248 | let dst = mu_units( |
| 12249 | if plane_idx == 1 { &mut u_units } else { &mut v_units }, |
| 12250 | cside * cside, |
| 12251 | ); |
| 12252 | for rr in 0..cu_tx { |
| 12253 | let start = (cr * chunk_chroma + ur * cu_tx + rr) |
| 12254 | * cside |
| 12255 | + cc * chunk_chroma |
| 12256 | + uc * cu_tx; |
| 12257 | dst[start..start + cu_tx] |
| 12258 | .copy_from_slice(&cu_grid[rr * cu_tx..][..cu_tx]); |
| 12259 | } |
| 12260 | } |
| 12261 | } |
| 12262 | } |
| 12263 | } |
| 12264 | } |
| 12265 | // The block-level luma bands were recorded per leaf above; the |
| 12266 | // composed chroma grids carry every unit in place. |
| 12267 | luma_grid = Grid::Zero(side * side); |
| 12268 | u_grid = if u_units.is_empty() { |
| 12269 | Grid::Zero(cside * cside) |
| 12270 | } else { |
| 12271 | Grid::Own(std::mem::take(&mut u_units)) |
| 12272 | }; |
| 12273 | v_grid = if v_units.is_empty() { |
| 12274 | Grid::Zero(cside * cside) |
| 12275 | } else { |
| 12276 | Grid::Own(std::mem::take(&mut v_units)) |
| 12277 | }; |
| 12278 | } |
| 12279 | // Bookkeeping (see [`decode_intrabc_rect`]): DC_PRED everywhere a |
| 12280 | // neighbour reads a mode, zeroed palette state, the skip grid, the |
| 12281 | // TXFM_CONTEXT bands (`read_block_tx_size_rect` already stamped them), |
| 12282 | // and the DV into the intrabc mi grid. |
| 12283 | if skip { |
| 12284 | neighbours.record_rect_mi( |
| 12285 | (mi_r, mi_c), bw, bh, DC_PRED, DC_PRED, |
| 12286 | &[&luma_grid[..], &u_grid[..], &v_grid[..]], |
| 12287 | ); |
| 12288 | } else { |
| 12289 | neighbours.record_split_luma_rect_mi( |
| 12290 | (mi_r, mi_c), bw, bh, DC_PRED, DC_PRED, [&u_grid[..], &v_grid[..]], |
| 12291 | ); |
| 12292 | } |
| 12293 | // lane-av1-ibcrect: the coded arm's chroma contexts were stamped PER |
| 12294 | // TRANSFORM UNIT (libaom `av1_set_entropy_contexts` inside |
| 12295 | // `decode_reconstruct_tx`'s mu-chunk loop), but the record above re-stamps |
| 12296 | // both planes with ONE `neighbour_state(composed_grid)` over the whole |
| 12297 | // span -- level = the SUM of every unit's levels and dc = the top-left |
| 12298 | // unit's sign (lane-vert46 r1 measured the same smearing at the 64x128 |
| 12299 | // inter block: the next block read a stale `dc_sign_ctx` one row low). |
| 12300 | // Measured on this shape: the 128x64 block at mi(64,32) of the |
| 12301 | // `ibcrect-hunt/E2` sb128 screen stream left chunk-1 U's level 1 smeared |
| 12302 | // into chunk-0's cells and the block below read `txb_skip` off the sum. |
| 12303 | // Replay every coded unit's own state after the record, in mu-chunk order |
| 12304 | // so a later unit still wins over an earlier one on the shared seam cells |
| 12305 | // (the composed construction `mu_chroma_units` uses, transcribed here). |
| 12306 | if mu_chroma { |
| 12307 | let cu = if lossless(fctx) { 4usize } else { 32usize }; |
| 12308 | // The unit's own footprint on the LUMA mi grid (the chroma context |
| 12309 | // bands are luma-mi indexed, [`Self::record_mi_chroma`]'s |
| 12310 | // convention): 8 px x2 per TX_4X4 unit, 64 px per TX_32X32 one. |
| 12311 | let unit_luma = cu * 2; |
| 12312 | // Raster over the block's chroma plane, `chunk_chroma` per mu chunk |
| 12313 | // (the same extents the read loop above laid the composed grid out |
| 12314 | // at) -- a unit the frame edge clipped codes nothing and carries no |
| 12315 | // composed samples beyond the clip, so it replays as the zero state |
| 12316 | // exactly like libaom's uncoded unit. |
| 12317 | let chunk_chroma = cside * 64 / side; |
| 12318 | let (rows, cols) = ((ch / chunk_chroma).max(1), (cw / chunk_chroma).max(1)); |
| 12319 | for cr in 0..rows { |
| 12320 | for cc in 0..cols { |
| 12321 | let unit_mi = (mi_r + cr * (unit_luma / MI), mi_c + cc * (unit_luma / MI)); |
| 12322 | for (plane_idx, grid) in [(1usize, &u_grid), (2usize, &v_grid)] { |
| 12323 | let mut unit = Vec::with_capacity(cu * cu); |
| 12324 | for rr in 0..cu { |
| 12325 | let start = |
| 12326 | (cr * chunk_chroma + rr) * cside + cc * chunk_chroma; |
| 12327 | unit.extend_from_slice(&grid[start..start + cu]); |
| 12328 | } |
| 12329 | neighbours.record_mi_chroma( |
| 12330 | unit_mi, unit_luma, unit_luma, plane_idx, &unit, |
| 12331 | ); |
| 12332 | } |
| 12333 | } |
| 12334 | } |
| 12335 | } |
| 12336 | neighbours.record_palette_y_rect((mi_r, mi_c), bw, bh, 0, [0u16; 8]); |
| 12337 | neighbours.record_palette_uv_rect((mi_r, mi_c), bw, bh, 0, [0u16; 8]); |
| 12338 | neighbours.fill_skip_grid_rect((mi_r, mi_c), bw / MI, bh / MI, skip); |
| 12339 | neighbours.fill_lf_grid_rect( |
| 12340 | (mi_r, mi_c), |
| 12341 | bw / MI, |
| 12342 | bh / MI, |
| 12343 | bw.min(64) as u8, |
| 12344 | bh.min(64) as u8, |
| 12345 | 0, fctx, |
| 12346 | ); |
| 12347 | record_intrabc_mi_rect( |
| 12348 | mi_r, |
| 12349 | mi_c, |
| 12350 | bw / MI, |
| 12351 | bh / MI, |
| 12352 | Some((dv_row, dv_col)), |
| 12353 | neighbours, |
| 12354 | fctx, |
| 12355 | ); |
| 12356 | Ok(()) |
| 12357 | } |
| 12358 | |
| 12359 | /// Decodes one true `bw`x`bh` `PARTITION_HORZ`/`PARTITION_VERT` intra strip |
| 12360 | /// (lane-intradisp r1, spec `decode_block` restricted to a 32x32 quadrant's |
| 12361 | /// two rect children). Only the `skip` case is supported: `skip`/`y_mode`/ |
| 12362 | /// `uv_mode`/`filter_intra`/`tx_depth` are read exactly per spec either way |
| 12363 | /// (so a skip strip elsewhere in the tile stays in sync), but this decoder |
| 12364 | /// has no rectangular-transform coefficient reader, and a chroma plane here |
| 12365 | /// is genuinely `TX_16X8`/`TX_8X16` (`av1_get_max_uv_txsize`, spec 5.11.16) |
| 12366 | /// with no depth-based square-tiling escape the way luma's own resolved |
| 12367 | /// `tx_depth` has (`sub_tx_size_map[TX_32X16]` == `TX_16X16`, square) -- |
| 12368 | /// so a coded (non-skip) strip is refused by name instead of guess-decoded. |
| 12369 | #[allow(clippy::too_many_arguments)] |
| 12370 | fn decode_block_rect( |
| 12371 | dec: &mut SymbolDecoder, |
| 12372 | cdfs: &mut Cdfs, |
| 12373 | neighbours: &mut Neighbours, |
| 12374 | at: (usize, usize), |
| 12375 | bw: usize, |
| 12376 | bh: usize, |
| 12377 | y: &mut PlaneBuf<'static>, |
| 12378 | u: &mut PlaneBuf<'static>, |
| 12379 | v: &mut PlaneBuf<'static>, |
| 12380 | enable_filter_intra: bool, |
| 12381 | allow_screen_content_tools: bool, |
| 12382 | base_q_idx: u8, |
| 12383 | tx_select: bool, |
| 12384 | reduced_tx_set: bool, fctx: &crate::decode::FrameCtx, |
| 12385 | ) -> Result<()> { |
| 12386 | let (r, c) = at; |
| 12387 | let (px, py) = (c * SUB, r * SUB); |
| 12388 | let (nb_above_mode, nb_left_mode) = neighbours.modes_above_left(r, c); |
| 12389 | let (palette_ctx, palette_cache) = neighbours.palette_ctx_and_cache(at); |
| 12390 | let palette_uv_cache = neighbours.palette_uv_cache(at); |
| 12391 | let (skip, mode, angle_delta_y, uv_mode, angle_delta_uv, alpha, filter_intra, palette_y, palette_uv, intrabc) = |
| 12392 | read_intra_mode_rect( |
| 12393 | dec, |
| 12394 | cdfs, |
| 12395 | nb_above_mode, |
| 12396 | nb_left_mode, |
| 12397 | true, |
| 12398 | true, |
| 12399 | bw, |
| 12400 | bh, |
| 12401 | enable_filter_intra, |
| 12402 | neighbours.skip_txfm_ctx(r * (SUB / MI), c * (SUB / MI)), |
| 12403 | allow_screen_content_tools, |
| 12404 | Some((palette_ctx, &palette_cache)), |
| 12405 | &palette_uv_cache, |
| 12406 | r * (SUB / MI), |
| 12407 | c * (SUB / MI), fctx, |
| 12408 | )?; |
| 12409 | |
| 12410 | if let Some((dv_row, dv_col)) = intrabc { |
| 12411 | return decode_intrabc_rect( |
| 12412 | dec, |
| 12413 | cdfs, |
| 12414 | neighbours, |
| 12415 | (r * (SUB / MI), c * (SUB / MI)), |
| 12416 | (bw, bh), |
| 12417 | skip, |
| 12418 | (dv_row, dv_col), |
| 12419 | y, |
| 12420 | u, |
| 12421 | v, |
| 12422 | base_q_idx, fctx, |
| 12423 | ); |
| 12424 | } |
| 12425 | let smooth_neighbor = is_smooth_mode(nb_above_mode) || is_smooth_mode(nb_left_mode); |
| 12426 | if smooth_neighbor { |
| 12427 | hit!(SMOOTH_LUMA_HITS); |
| 12428 | } |
| 12429 | // lane-chroma r3: chroma's own edge-filter-strength neighbour check |
| 12430 | // (spec `get_intra_edge_filter_type`) reads the CHROMA neighbour's |
| 12431 | // `uv_mode`, not the luma one. |
| 12432 | let smooth_neighbor_uv = |
| 12433 | neighbours.smooth_uv_neighbour(r * (SUB / MI), c * (SUB / MI), r, c, fctx); |
| 12434 | // lane-rectsplit r1: `predict_filter_intra` now takes the block's own |
| 12435 | // `bw`x`bh` (`av1_filter_intra_predictor_c` walks its 4x2 patches over a |
| 12436 | // rectangle just as happily), so a `use_filter_intra` strip at this level |
| 12437 | // predicts instead of refusing. The below-16x16 leaf arm |
| 12438 | // (`decode_leaf_rect`) keeps the refusal: it has no gate of its own. |
| 12439 | if filter_intra.is_some() { |
| 12440 | hit!(FILTER_INTRA_RECT_HITS); |
| 12441 | } |
| 12442 | let uv_predict_mode = if uv_mode == UV_CFL_PRED { |
| 12443 | DC_PRED |
| 12444 | } else { |
| 12445 | uv_mode |
| 12446 | }; |
| 12447 | let (mi_r, mi_c) = (r * (SUB / MI), c * (SUB / MI)); |
| 12448 | // The tx-depth symbol EXISTS only under TX_MODE_SELECT. Both square paths |
| 12449 | // gate their `read_tx_size` on `tx_select` for exactly this reason; this |
| 12450 | // one did not, so with `--enable-tx-size-search=0` -- which several gate |
| 12451 | // recipes use -- the decoder consumed a symbol the encoder never wrote and |
| 12452 | // desynced the tile from that point on. That is why both strips of the |
| 12453 | // pinned HORZ quadrant were wrong from their very first pixel, and why our |
| 12454 | // range after the read equalled the oracle's range for the whole block. |
| 12455 | // lane-av1mixloss: the lossless-segment early return |
| 12456 | // (`decodeframe.c:1203`) precedes `TX_MODE_SELECT`, so a lossless-segment |
| 12457 | // rect block codes no depth symbol here either (class sweep of the sub8 |
| 12458 | // leaf's fix). |
| 12459 | let depth = if tx_select && !lossless(fctx) { |
| 12460 | // lane-av1txbands: EVERY frame now reads the real `TXFM_CONTEXT` |
| 12461 | // bands (`get_tx_size_context`, libaom `pred_common.h:342`) -- an |
| 12462 | // intra-only frame publishes them from every block too |
| 12463 | // ([`Neighbours::fill_lf_grid_rect`]). Measured on the 10-bit |
| 12464 | // 1920x792 128-superblock stream, |
| 12465 | // decode-order frame 1: the 32x8 HORZ_4 strip at mi(48,176) read |
| 12466 | // `tx_size_cat2` row 1 where libaom reads row 2. |
| 12467 | let ctx = tx_size_context_txfm_rect(neighbours, (mi_r, mi_c), bw, bh, fctx); |
| 12468 | let d = dec.symbol(&mut cdfs.tx_size_cat2[ctx]); |
| 12469 | if crate::envflags::env_flag!("EC_TRACE_MODE_STEP") { |
| 12470 | eprintln!( |
| 12471 | "EC_ISTEP mi_row={mi_r} mi_col={mi_c} name=tx_depth val={d} ctx={ctx} cat=2 rng={}", |
| 12472 | dec.debug_state().0 |
| 12473 | ); |
| 12474 | } |
| 12475 | d |
| 12476 | } else { |
| 12477 | 0 |
| 12478 | }; |
| 12479 | // lane-av1-intrabc r3: this strip's own `set_txfm_ctxs` when the frame |
| 12480 | // reads the `TXFM_CONTEXT` bands -- see [`publish_txfm_bands_if_in_inter`]. |
| 12481 | publish_txfm_bands_if_in_inter( |
| 12482 | neighbours, |
| 12483 | (mi_r, mi_c), |
| 12484 | depth_to_tx_wh(bw, bh, depth, fctx), |
| 12485 | (bw, bh), fctx, |
| 12486 | ); |
| 12487 | if depth != 0 || lossless(fctx) { |
| 12488 | hit!(TX_DEPTH_HITS); |
| 12489 | // lane-rectsplit r1: a split transform is predicted and reconstructed |
| 12490 | // per transform unit, each unit taking its edges from the previous |
| 12491 | // unit's reconstruction inside this same strip (spec 5.11.36) -- |
| 12492 | // [`decode_rect_split`], the unit named by [`depth_to_tx_wh`]. |
| 12493 | let (tx_w, tx_h) = depth_to_tx_wh(bw, bh, depth, fctx); |
| 12494 | if crate::envflags::env_flag!("EC_SBPART_DUMP64") { |
| 12495 | eprintln!( |
| 12496 | "DUMP64SPLIT mi_r={mi_r} mi_c={mi_c} px={px} py={py} bw={bw} bh={bh} \ |
| 12497 | depth={depth} tx={tx_w}x{tx_h} mode={mode} uv={uv_predict_mode} skip={skip} \ |
| 12498 | angle_y={angle_delta_y}" |
| 12499 | ); |
| 12500 | } |
| 12501 | let modes = RectStripModes { |
| 12502 | skip, |
| 12503 | mode, |
| 12504 | angle_delta_y, |
| 12505 | uv_predict_mode, |
| 12506 | angle_delta_uv, |
| 12507 | alpha, |
| 12508 | filter_intra, |
| 12509 | smooth_neighbor, |
| 12510 | smooth_neighbor_uv, |
| 12511 | palette_y: palette_y |
| 12512 | .as_ref() |
| 12513 | .map(|p| p.map.iter().map(|&i| p.colors[i as usize]).collect()), |
| 12514 | palette_uv: palette_uv.as_ref().map(|p| { |
| 12515 | ( |
| 12516 | p.map.iter().map(|&i| p.u_colors[i as usize]).collect(), |
| 12517 | p.map.iter().map(|&i| p.v_colors[i as usize]).collect(), |
| 12518 | ) |
| 12519 | }), |
| 12520 | }; |
| 12521 | if palette_y.is_some() || palette_uv.is_some() { |
| 12522 | hit!(PALETTE_SPLIT_TX_HITS); |
| 12523 | } |
| 12524 | decode_rect_split( |
| 12525 | dec, |
| 12526 | cdfs, |
| 12527 | neighbours, |
| 12528 | (mi_r, mi_c), |
| 12529 | bw, |
| 12530 | bh, |
| 12531 | tx_w, |
| 12532 | tx_h, |
| 12533 | &modes, |
| 12534 | y, |
| 12535 | u, |
| 12536 | v, |
| 12537 | base_q_idx, |
| 12538 | reduced_tx_set, fctx, |
| 12539 | )?; |
| 12540 | // This early return skips this fn's own tail, so the palette |
| 12541 | // neighbour state has to be stamped HERE too: without it a split-tx |
| 12542 | // strip left the above/left palette arrays holding a DEAD block's |
| 12543 | // colours, and the next block's colour cache |
| 12544 | // ([`Neighbours::palette_uv_cache`]) then hit a cached entry with the |
| 12545 | // wrong VALUE -- same bit count, so the entropy stream stayed in sync |
| 12546 | // and only those pixels were wrong (lane-palette2 r10). |
| 12547 | let (py_size, py_colors) = palette_y.as_ref().map_or((0, [0u16; 8]), |p| (p.size, p.colors)); |
| 12548 | neighbours.record_palette_y_rect((mi_r, mi_c), bw, bh, py_size, py_colors); |
| 12549 | let (puv_size, puv_colors) = |
| 12550 | palette_uv.as_ref().map_or((0, [0u16; 8]), |p| (p.size, p.u_colors)); |
| 12551 | neighbours.record_palette_uv_rect((mi_r, mi_c), bw, bh, puv_size, puv_colors); |
| 12552 | hit!(RECT_PARTITION_HITS); |
| 12553 | return Ok(()); |
| 12554 | } |
| 12555 | let (tx_w, tx_h) = (bw, bh); |
| 12556 | let (cpx, cpy) = (px >> ss_x(fctx), py >> ss_y(fctx)); |
| 12557 | let (chroma_w, chroma_h) = (bw >> ss_x(fctx), bh >> ss_y(fctx)); |
| 12558 | let reach = Reach::of_rect(bw, bh, px, py, y.width, y.height, fctx); |
| 12559 | if crate::envflags::env_flag!("EC_AV1_TRACE") { |
| 12560 | let (rng, _) = dec.debug_state(); |
| 12561 | eprintln!( |
| 12562 | "TRACE_RECT_IMODE mi_row={mi_r} mi_col={mi_c} mode={mode} uv_mode={uv_mode} \ |
| 12563 | skip={} tx={depth} rng={rng}", |
| 12564 | skip as i32 |
| 12565 | ); |
| 12566 | } |
| 12567 | // As [`decode_block`]'s own [`PALETTE_PRED`] wiring: a palette-Y/UV |
| 12568 | // block's prediction is the reconstructed colour-index map, set fresh |
| 12569 | // right before each `reconstruct_rect` call below (lane-palette2 r4). |
| 12570 | if let Some(ref pyv) = palette_y { |
| 12571 | let buf: Vec<u16> = pyv.map.iter().map(|&idx| pyv.colors[idx as usize]).collect(); |
| 12572 | set_palette_pred(buf, fctx); |
| 12573 | } |
| 12574 | let palette_uv_bufs: Option<(Vec<u16>, Vec<u16>)> = palette_uv.as_ref().map(|puv| { |
| 12575 | ( |
| 12576 | puv.map.iter().map(|&idx| puv.u_colors[idx as usize]).collect(), |
| 12577 | puv.map.iter().map(|&idx| puv.v_colors[idx as usize]).collect(), |
| 12578 | ) |
| 12579 | }); |
| 12580 | if skip { |
| 12581 | push_intra_rect(0, |
| 12582 | px, |
| 12583 | py, |
| 12584 | bw, |
| 12585 | bh, |
| 12586 | mode, |
| 12587 | angle_delta_y, |
| 12588 | reach, |
| 12589 | &ZERO_RESIDUAL[..bw * bh], |
| 12590 | None, |
| 12591 | filter_intra, |
| 12592 | smooth_neighbor, fctx, |
| 12593 | ); |
| 12594 | let ac = alpha.map(|_| cfl_src_rect(px, py, bw, bh)); |
| 12595 | if let Some((ub, _)) = &palette_uv_bufs { |
| 12596 | set_palette_pred(ub.clone(), fctx); |
| 12597 | } |
| 12598 | push_intra_rect(1, |
| 12599 | cpx, |
| 12600 | cpy, |
| 12601 | chroma_w, |
| 12602 | chroma_h, |
| 12603 | uv_predict_mode, |
| 12604 | angle_delta_uv, |
| 12605 | reach, |
| 12606 | &ZERO_RESIDUAL[..chroma_w * chroma_h], |
| 12607 | alpha.zip(ac).map(|((au, _), ac)| (au, ac)), |
| 12608 | None, |
| 12609 | smooth_neighbor_uv, fctx, |
| 12610 | ); |
| 12611 | if let Some((_, vb)) = &palette_uv_bufs { |
| 12612 | set_palette_pred(vb.clone(), fctx); |
| 12613 | } |
| 12614 | push_intra_rect(2, |
| 12615 | cpx, |
| 12616 | cpy, |
| 12617 | chroma_w, |
| 12618 | chroma_h, |
| 12619 | uv_predict_mode, |
| 12620 | angle_delta_uv, |
| 12621 | reach, |
| 12622 | &ZERO_RESIDUAL[..chroma_w * chroma_h], |
| 12623 | alpha.zip(ac).map(|((_, av), ac)| (av, ac)), |
| 12624 | None, |
| 12625 | smooth_neighbor_uv, fctx, |
| 12626 | ); |
| 12627 | neighbours.record_rect( |
| 12628 | at, |
| 12629 | bw, |
| 12630 | bh, |
| 12631 | mode, |
| 12632 | uv_predict_mode, |
| 12633 | &[ |
| 12634 | &ZERO_RESIDUAL[..bw * bh], |
| 12635 | &ZERO_RESIDUAL[..chroma_w * chroma_h], |
| 12636 | &ZERO_RESIDUAL[..chroma_w * chroma_h], |
| 12637 | ], |
| 12638 | ); |
| 12639 | } else { |
| 12640 | // lane-rectwire r2: real coefficients. `get_txsize_entropy_ctx` |
| 12641 | // reduces both size pairs to their square-up CDF sets |
| 12642 | // (`TxbSet::LumaRect32x16`/`ChromaRect16x8`, see those variants' own |
| 12643 | // doc comments); the scan order and 2D context tables are the real |
| 12644 | // rect ones (`SCAN_32X16`/etc, `base_ctx_rect`/`br_ctx_rect`). |
| 12645 | let luma_scan: &[u16] = if bw > bh { &SCAN_32X16 } else { &SCAN_16X32 }; |
| 12646 | // Chroma tx follows the plane block, not a hardcoded half. At 4:2:0 a |
| 12647 | // 32x16 strip's chroma is 16x8 (ChromaRect16x8); at 4:4:4 it is 32x16 |
| 12648 | // (ChromaRect32x16, DCT-only — ADST is undefined on a 32-point axis). |
| 12649 | let (chroma_set, chroma_scan): (TxbSet, &[u16]) = match (chroma_w, chroma_h) { |
| 12650 | (32, 16) => (TxbSet::ChromaRect32x16, &SCAN_32X16), |
| 12651 | (16, 32) => (TxbSet::ChromaRect32x16, &SCAN_16X32), |
| 12652 | (16, 8) => (TxbSet::ChromaRect16x8, &SCAN_16X8), |
| 12653 | (8, 16) => (TxbSet::ChromaRect16x8, &SCAN_8X16), |
| 12654 | // lane-av1-422: a 4:2:2 strip halves only the width. A 32x16 |
| 12655 | // luma strip's chroma is a 16x16 SQUARE (`TxbSet::Chroma16`, the |
| 12656 | // set any 16x16 chroma plane reads); a 16x32 strip's chroma is |
| 12657 | // 8x32, whose entropy set is the 16x16 one and whose DCT-only |
| 12658 | // default follows the >=32 rule below -- the same pairing the |
| 12659 | // (8,32) row of [`decode_rect_split`]'s chroma table proves. At |
| 12660 | // 4:2:0 and 4:4:4 a strip's chroma is itself rect, so neither row |
| 12661 | // is reachable there. |
| 12662 | (16, 16) => (TxbSet::Chroma16, default_scan(16)), |
| 12663 | (8, 32) => (TxbSet::Chroma16, &SCAN_8X32), |
| 12664 | _ => { |
| 12665 | return Err(unsupported( |
| 12666 | "a rectangular chroma transform whose size has no coefficient table", |
| 12667 | )) |
| 12668 | } |
| 12669 | }; |
| 12670 | let chroma_default_tx = if chroma_w.max(chroma_h) >= 32 { |
| 12671 | TxType::DctDct |
| 12672 | } else { |
| 12673 | default_intra_tx_type(uv_predict_mode as u8) |
| 12674 | }; |
| 12675 | // lane-rectwire r3: real coefficients desync a pixel-exact gate |
| 12676 | // (rectwire-flake-1.obu, seed 55) -- both 32x16 strips of frame 0's |
| 12677 | // mi(8,8) HORZ quadrant come out ~100% wrong from their very first |
| 12678 | // pixel (not a subtle context-table off-by-one), while the sibling |
| 12679 | // non-rect quadrants of the same frame match. Range-ladder bisection |
| 12680 | // against a real aomdec (EC_TRACE_COEFF/EC_TRACE_MODE) was started |
| 12681 | // but the intra key-frame path has no traced symbol between the |
| 12682 | // partition read and the first coefficient block (mode/skip/tx_depth |
| 12683 | // reads are all untraced in libaom's decodeframe.c intra path, unlike |
| 12684 | // the inter EC_TRACE_MODE this round added), so a same-length range |
| 12685 | // comparison wasn't reachable within budget -- see |
| 12686 | // lanes/rectwire-r3.report.md for what was ruled out (base_ctx_rect's |
| 12687 | // 5-neighbour offsets, u/v_skip_ctx's above+left formula, and |
| 12688 | // dc_sign_ctx's around-index all cross-checked byte-for-byte against |
| 12689 | // libaom and match). lane-rectwire r4: range-ladder against the |
| 12690 | // now-instrumented oracle (EC_TRACE_MODE's intra EC_IMODE/EC_IMODE_VAL) |
| 12691 | // narrowed the divergence past skip/y_mode/uv_mode (values AND range |
| 12692 | // both confirmed in sync -- rng=58692 on both sides right after |
| 12693 | // uv_mode, with `enable_filter_intra=0` on this frame so neither side |
| 12694 | // reads a filter_intra symbol) to the `tx_size_cat2` depth read |
| 12695 | // immediately after: ours moves rng 58692 -> 43570, which cannot be |
| 12696 | // reconciled with oracle's own EC_IMODE_VAL (rng=58692, taken *after* |
| 12697 | // its own tx_size read) unless oracle's tx symbol is a near-certainty |
| 12698 | // CDF that barely narrows the range -- i.e. this decoder's |
| 12699 | // tx-depth ctx or `cdfs.tx_size_cat2` CDF selection for |
| 12700 | // a 32x16/16x32 strip is the first diverging symbol, not the |
| 12701 | // coefficient-context math r3 already cleared. Not confirmed byte- |
| 12702 | // exact (would need a decodemv.c EC_TRACE_MODE patch specifically on |
| 12703 | // `read_tx_size`'s own rng, not attempted this round -- budget), so |
| 12704 | // r5 CONFIRMED the suspect and it was simpler than the CDF row: the |
| 12705 | // tx-depth symbol exists only under TX_MODE_SELECT, and this path read |
| 12706 | // it unconditionally where both square paths gate on `tx_select`. With |
| 12707 | // --enable-tx-size-search=0 the encoder writes no such symbol, so the |
| 12708 | // decoder consumed one that was never there. |
| 12709 | { |
| 12710 | let around = neighbours.around_rect(at, bw, bh); |
| 12711 | let mut luma_coding = cdfs.txb(TxbSet::LumaRect32x16, fi_tx_row(mode, filter_intra)); |
| 12712 | let (luma_levels, luma_tx_type) = read_coeffs_rect( |
| 12713 | dec, |
| 12714 | &mut luma_coding, |
| 12715 | luma_scan, |
| 12716 | bw, |
| 12717 | bh, |
| 12718 | 0, |
| 12719 | dc_sign_ctx(around[0].2), |
| 12720 | TxType::DctDct, |
| 12721 | )?; |
| 12722 | if crate::envflags::env_flag!("EC_AV1_TRACE") { |
| 12723 | let (rng, _) = dec.debug_state(); |
| 12724 | eprintln!("TRACE_RECT_COEFF plane=0 mi_row={mi_r} mi_col={mi_c} rng={rng}"); |
| 12725 | } |
| 12726 | let luma_residual = dequant_and_inverse_typed_wh( |
| 12727 | &luma_levels, |
| 12728 | bw, |
| 12729 | bh, |
| 12730 | crate::decode::bit_depth(fctx), |
| 12731 | block_q_idx(fctx), |
| 12732 | plane_q_delta(0, fctx).0, |
| 12733 | plane_q_delta(0, fctx).1, |
| 12734 | luma_tx_type, |
| 12735 | block_iqmatrix(fctx, 0, bw, bh, luma_tx_type), |
| 12736 | ); |
| 12737 | push_intra_rect(0, |
| 12738 | px, |
| 12739 | py, |
| 12740 | bw, |
| 12741 | bh, |
| 12742 | mode, |
| 12743 | angle_delta_y, |
| 12744 | reach, |
| 12745 | &luma_residual, |
| 12746 | None, |
| 12747 | filter_intra, |
| 12748 | smooth_neighbor, fctx, |
| 12749 | ); |
| 12750 | let ac = alpha.map(|_| cfl_src_rect(px, py, bw, bh)); |
| 12751 | if let Some((ub, _)) = &palette_uv_bufs { |
| 12752 | set_palette_pred(ub.clone(), fctx); |
| 12753 | } |
| 12754 | let u_default_tx = chroma_default_tx; |
| 12755 | let u_skip_ctx = usize::from(around[1].0) + usize::from(around[1].1); |
| 12756 | let mut u_coding = cdfs.txb(chroma_set, uv_predict_mode); |
| 12757 | let (u_levels, u_tx_type) = read_chroma_coeffs_rect( |
| 12758 | dec, |
| 12759 | &mut u_coding, |
| 12760 | chroma_scan, |
| 12761 | chroma_w, |
| 12762 | chroma_h, |
| 12763 | u_skip_ctx, |
| 12764 | dc_sign_ctx(around[1].2), |
| 12765 | u_default_tx, |
| 12766 | fctx, |
| 12767 | )?; |
| 12768 | if crate::envflags::env_flag!("EC_AV1_TRACE") { |
| 12769 | let (rng, _) = dec.debug_state(); |
| 12770 | eprintln!("TRACE_RECT_COEFF plane=1 mi_row={mi_r} mi_col={mi_c} rng={rng}"); |
| 12771 | } |
| 12772 | let u_residual = dequant_and_inverse_typed_wh( |
| 12773 | &u_levels, |
| 12774 | chroma_w, |
| 12775 | chroma_h, |
| 12776 | crate::decode::bit_depth(fctx), |
| 12777 | block_q_idx(fctx), |
| 12778 | plane_q_delta(1, fctx).0, |
| 12779 | plane_q_delta(1, fctx).1, |
| 12780 | u_tx_type, |
| 12781 | block_iqmatrix(fctx, 1, chroma_w, chroma_h, u_tx_type), |
| 12782 | ); |
| 12783 | push_intra_rect(1, |
| 12784 | cpx, |
| 12785 | cpy, |
| 12786 | chroma_w, |
| 12787 | chroma_h, |
| 12788 | uv_predict_mode, |
| 12789 | angle_delta_uv, |
| 12790 | reach, |
| 12791 | &u_residual, |
| 12792 | alpha.zip(ac).map(|((au, _), ac)| (au, ac)), |
| 12793 | None, |
| 12794 | smooth_neighbor_uv, fctx, |
| 12795 | ); |
| 12796 | if let Some((_, vb)) = &palette_uv_bufs { |
| 12797 | set_palette_pred(vb.clone(), fctx); |
| 12798 | } |
| 12799 | let v_default_tx = chroma_default_tx; |
| 12800 | let v_skip_ctx = usize::from(around[2].0) + usize::from(around[2].1); |
| 12801 | let mut v_coding = cdfs.txb(chroma_set, uv_predict_mode); |
| 12802 | let (v_levels, v_tx_type) = read_chroma_coeffs_rect( |
| 12803 | dec, |
| 12804 | &mut v_coding, |
| 12805 | chroma_scan, |
| 12806 | chroma_w, |
| 12807 | chroma_h, |
| 12808 | v_skip_ctx, |
| 12809 | dc_sign_ctx(around[2].2), |
| 12810 | v_default_tx, |
| 12811 | fctx, |
| 12812 | )?; |
| 12813 | if crate::envflags::env_flag!("EC_AV1_TRACE") { |
| 12814 | let (rng, _) = dec.debug_state(); |
| 12815 | eprintln!("TRACE_RECT_COEFF plane=2 mi_row={mi_r} mi_col={mi_c} rng={rng}"); |
| 12816 | } |
| 12817 | let v_residual = dequant_and_inverse_typed_wh( |
| 12818 | &v_levels, |
| 12819 | chroma_w, |
| 12820 | chroma_h, |
| 12821 | crate::decode::bit_depth(fctx), |
| 12822 | block_q_idx(fctx), |
| 12823 | plane_q_delta(2, fctx).0, |
| 12824 | plane_q_delta(2, fctx).1, |
| 12825 | v_tx_type, |
| 12826 | block_iqmatrix(fctx, 2, chroma_w, chroma_h, v_tx_type), |
| 12827 | ); |
| 12828 | push_intra_rect(2, |
| 12829 | cpx, |
| 12830 | cpy, |
| 12831 | chroma_w, |
| 12832 | chroma_h, |
| 12833 | uv_predict_mode, |
| 12834 | angle_delta_uv, |
| 12835 | reach, |
| 12836 | &v_residual, |
| 12837 | alpha.zip(ac).map(|((_, av), ac)| (av, ac)), |
| 12838 | None, |
| 12839 | smooth_neighbor_uv, fctx, |
| 12840 | ); |
| 12841 | neighbours.record_rect(at, bw, bh, mode, uv_predict_mode, &[&luma_levels[..], &u_levels[..], &v_levels[..]]); |
| 12842 | hit!(RECT_COEFF_HITS); |
| 12843 | } |
| 12844 | } |
| 12845 | let (py_size, py_colors) = palette_y.as_ref().map_or((0, [0u16; 8]), |p| (p.size, p.colors)); |
| 12846 | neighbours.record_palette_y_rect((mi_r, mi_c), bw, bh, py_size, py_colors); |
| 12847 | let (puv_size, puv_colors) = |
| 12848 | palette_uv.as_ref().map_or((0, [0u16; 8]), |p| (p.size, p.u_colors)); |
| 12849 | neighbours.record_palette_uv_rect((mi_r, mi_c), bw, bh, puv_size, puv_colors); |
| 12850 | neighbours.fill_skip_grid_rect((mi_r, mi_c), bw / MI, bh / MI, skip); |
| 12851 | neighbours.fill_lf_grid_rect((mi_r, mi_c), bw / MI, bh / MI, tx_w as u8, tx_h as u8, 0, fctx); |
| 12852 | hit!(RECT_PARTITION_HITS); |
| 12853 | if crate::envflags::env_flag!("EC_AV1_TRACE") { |
| 12854 | eprintln!("TRACE_RECT32_END mi_row={mi_r} mi_col={mi_c} bw={bw} bh={bh}"); |
| 12855 | } |
| 12856 | Ok(()) |
| 12857 | } |
| 12858 | |
| 12859 | /// Decodes one `bw`x`bh` rect strip of a plain 16x16-level `PARTITION_HORZ`/ |
| 12860 | /// `PARTITION_VERT` (lane-rect16 r2), sibling to [`decode_block_rect`] but |
| 12861 | /// addressed by REAL mi coordinates rather than derived from an [`SUB`]-grid |
| 12862 | /// `outer_at`, the same relationship [`decode_leaf8`] has to `decode_block`: |
| 12863 | /// the second strip of a HORZ/VERT split sits offset by half the 16x16 |
| 12864 | /// parent, which `decode_block_rect`'s `at`-derived `px = c * SUB` can't |
| 12865 | /// name. `prev_leaf` mirrors `decode_leaf8`'s own convention -- when the |
| 12866 | /// previous strip is directly above (same column, `mi_h` rows up) or |
| 12867 | /// directly left (same row, `mi_w` cols over), its mode overrides the coarse |
| 12868 | /// per-16x16-cell `above_mode`/`left_mode` slot for THIS strip's own mode |
| 12869 | /// context read, and bookkeeping goes through the `_mi`/`_rect` neighbour |
| 12870 | /// writers (`record_mi_rect`, `fill_skip_grid_rect`, `fill_lf_grid_rect`) at |
| 12871 | /// this strip's real position, never the coarse `record_rect` (which the |
| 12872 | /// caller uses once, after both strips, exactly like the `VERT_B` arm next |
| 12873 | /// to it). The non-skip case (lane-rectx) reads real `TX_16X8`/`TX_8X16` |
| 12874 | /// luma coefficients (`TxbSet::LumaRect16x8`) and their `TX_8X4`/`TX_4X8` |
| 12875 | /// chroma half (`TxbSet::ChromaRect8x4`) at 4:2:0 -- an 8x8 *square* chroma |
| 12876 | /// plane (`TxbSet::Chroma8` + the default scan) at 4:2:2 (lane-av1-422e: |
| 12877 | /// half-width, unhalved height), the two size classes one level |
| 12878 | /// under [`decode_block_rect`]'s own 32x16/16x32 strip. |
| 12879 | #[allow(clippy::too_many_arguments)] |
| 12880 | fn decode_leaf_rect( |
| 12881 | dec: &mut SymbolDecoder, |
| 12882 | cdfs: &mut Cdfs, |
| 12883 | neighbours: &mut Neighbours, |
| 12884 | outer_at: (usize, usize), |
| 12885 | leaf_mi: (usize, usize), |
| 12886 | bw: usize, |
| 12887 | bh: usize, |
| 12888 | prev_leaf: Option<((usize, usize), usize)>, |
| 12889 | y: &mut PlaneBuf<'static>, |
| 12890 | u: &mut PlaneBuf<'static>, |
| 12891 | v: &mut PlaneBuf<'static>, |
| 12892 | enable_filter_intra: bool, |
| 12893 | allow_screen_content_tools: bool, |
| 12894 | base_q_idx: u8, |
| 12895 | tx_select: bool, |
| 12896 | reduced_tx_set: bool, fctx: &crate::decode::FrameCtx, |
| 12897 | ) -> Result<usize> { |
| 12898 | let _ = base_q_idx; |
| 12899 | if crate::envflags::env_flag!("EC_AV1_RECTX_TRACE") { |
| 12900 | eprintln!( |
| 12901 | "decode_leaf_rect: leaf_mi={leaf_mi:?} bw={bw} bh={bh} outer_at={outer_at:?} rng={}", |
| 12902 | dec.debug_state().0 |
| 12903 | ); |
| 12904 | } |
| 12905 | let (r, c) = outer_at; |
| 12906 | let mut above_mode = neighbours.above_mode[c]; |
| 12907 | let mut left_mode = neighbours.left_mode[r]; |
| 12908 | let (mi_w, mi_h) = (bw / MI, bh / MI); |
| 12909 | if let Some(((pr, pc), pmode)) = prev_leaf { |
| 12910 | if pc == leaf_mi.1 && leaf_mi.0 == pr + mi_h { |
| 12911 | above_mode = pmode; |
| 12912 | } else if pr == leaf_mi.0 && leaf_mi.1 == pc + mi_w { |
| 12913 | left_mode = pmode; |
| 12914 | } |
| 12915 | } |
| 12916 | // Same mi-exact override [`decode_leaf8`] takes: one coarse 16x16 slot |
| 12917 | // cannot hold the modes a split (or a pair of strips) leaves behind, so |
| 12918 | // when the map holds the block at exactly this strip's above/left mi it |
| 12919 | // is the neighbour libaom's `above_mi`/`left_mi` reads. |
| 12920 | // lane-rectsplitx r2 (same class as the 1:4 fix): mi-exact AND |
| 12921 | // availability-correct -- four 16x8 strips share one coarse 16x16 |
| 12922 | // `left_mode` cell too, so the fallback must not stand in for an absent |
| 12923 | // tile-edge neighbour. |
| 12924 | let (above_mode, left_mode) = |
| 12925 | neighbours.modes_above_left_mi(leaf_mi.0, leaf_mi.1, (above_mode, left_mode)); |
| 12926 | let smooth_neighbor = |
| 12927 | is_smooth_mode(above_mode) || is_smooth_mode(left_mode); |
| 12928 | if smooth_neighbor { |
| 12929 | hit!(SMOOTH_LUMA_HITS); |
| 12930 | } |
| 12931 | // The palette-Y mode CDF row is picked by the neighbours' palette use |
| 12932 | // (`palette_ctx_and_cache`), so this sub-16x16 leaf must pass its own real |
| 12933 | // ctx/cache -- reading `palette_y_mode` off row 0 when a neighbour did use |
| 12934 | // a palette narrows the wrong interval and desyncs the tile even when the |
| 12935 | // decoded bit is the same. The cell is the 16px-SUB cell containing the |
| 12936 | // leaf (the palette neighbour arrays have no finer granularity). |
| 12937 | // lane-kf900 r6: the leaf's REAL mi position -- the second leaf of a 16x8 |
| 12938 | // pair sits 8 px into the 16px cell, and both the cache's superblock-top-row |
| 12939 | // exclusion and the neighbour palette lookup itself are mi-granular |
| 12940 | // (see [`Neighbours::palette_ctx_and_cache_mi`]). |
| 12941 | let pal_at = leaf_mi; |
| 12942 | let (palette_ctx, palette_cache) = neighbours.palette_ctx_and_cache_mi(pal_at); |
| 12943 | let palette_uv_cache = neighbours.palette_uv_cache_mi(pal_at); |
| 12944 | let (skip, mode, angle_delta_y, uv_mode, angle_delta_uv, alpha, filter_intra, palette_y, palette_uv, intrabc) = |
| 12945 | read_intra_mode_rect( |
| 12946 | dec, |
| 12947 | cdfs, |
| 12948 | above_mode, |
| 12949 | left_mode, |
| 12950 | true, |
| 12951 | true, |
| 12952 | bw, |
| 12953 | bh, |
| 12954 | enable_filter_intra, |
| 12955 | neighbours.skip_txfm_ctx(leaf_mi.0, leaf_mi.1), |
| 12956 | allow_screen_content_tools, |
| 12957 | Some((palette_ctx, &palette_cache)), |
| 12958 | &palette_uv_cache, |
| 12959 | leaf_mi.0, |
| 12960 | leaf_mi.1, fctx, |
| 12961 | )?; |
| 12962 | |
| 12963 | if let Some((dv_row, dv_col)) = intrabc { |
| 12964 | decode_intrabc_rect( |
| 12965 | dec, cdfs, neighbours, leaf_mi, (bw, bh), skip, (dv_row, dv_col), |
| 12966 | y, u, v, base_q_idx, fctx, |
| 12967 | )?; |
| 12968 | return Ok(DC_PRED); |
| 12969 | } |
| 12970 | // lane-kf900 r4: palette prediction on this shape, the same colour-index |
| 12971 | // map -> base colours buffer [`decode_block_rect`] hands each |
| 12972 | // `reconstruct_rect` through [`set_palette_pred`]. |
| 12973 | if palette_y.is_some() || palette_uv.is_some() { |
| 12974 | hit!(RECT4_PALETTE_HITS); |
| 12975 | } |
| 12976 | let palette_y_buf: Option<Vec<u16>> = palette_y |
| 12977 | .as_ref() |
| 12978 | .map(|p| p.map.iter().map(|&i| p.colors[i as usize]).collect()); |
| 12979 | let palette_uv_bufs: Option<(Vec<u16>, Vec<u16>)> = palette_uv.as_ref().map(|p| { |
| 12980 | ( |
| 12981 | p.map.iter().map(|&i| p.u_colors[i as usize]).collect(), |
| 12982 | p.map.iter().map(|&i| p.v_colors[i as usize]).collect(), |
| 12983 | ) |
| 12984 | }); |
| 12985 | let (pal_y_size, pal_y_colors) = |
| 12986 | palette_y.as_ref().map_or((0, [0u16; 8]), |p| (p.size, p.colors)); |
| 12987 | let (pal_uv_size, pal_uv_colors) = |
| 12988 | palette_uv.as_ref().map_or((0, [0u16; 8]), |p| (p.size, p.u_colors)); |
| 12989 | if crate::envflags::env_flag!("EC_AV1_RECTX_TRACE") { |
| 12990 | eprintln!(" skip={skip} mode={mode} angle_delta_y={angle_delta_y} uv_mode={uv_mode}"); |
| 12991 | } |
| 12992 | if filter_intra.is_some() { |
| 12993 | // lane-fistrip r1: `predict_filter_intra` already walks its 4x2 |
| 12994 | // patches over a true `bw`x`bh` block (lane-rectsplit r1), so an |
| 12995 | // 8x16/16x8 strip predicts through the same call `reconstruct_rect` |
| 12996 | // below already makes; this refusal was square-only prose. |
| 12997 | hit!(FILTER_INTRA_RECT_HITS); |
| 12998 | hit!(FILTER_INTRA_RECT_SUB16_HITS); |
| 12999 | } |
| 13000 | let uv_predict_mode = if uv_mode == UV_CFL_PRED { DC_PRED } else { uv_mode }; |
| 13001 | // lane-cfl r1, same defect as [`decode_leaf_8x8`]: chroma's intra-edge |
| 13002 | // filter type is the NEIGHBOUR's `uv_mode` (libaom `get_filt_type`), not |
| 13003 | // a constant `false`. |
| 13004 | let smooth_neighbor_uv = neighbours.smooth_uv_neighbour(leaf_mi.0, leaf_mi.1, r, c, fctx); |
| 13005 | let depth = if tx_select && !lossless(fctx) { |
| 13006 | // lane-t900 r2 (sibling sweep): the in-inter `TXFM_CONTEXT` band read |
| 13007 | // every other rect reader now does -- a no-op on a key frame. |
| 13008 | // lane-av1mixloss: libaom `read_tx_size`'s first line |
| 13009 | // (`decodeframe.c:1203`, `if (xd->lossless[segment_id]) return |
| 13010 | // TX_4X4;`) precedes the `TX_MODE_SELECT` test, so a lossless-segment |
| 13011 | // rect leaf codes NO depth symbol (`depth_to_tx_wh` clamps to 4x4). |
| 13012 | // The mixed frame's 16x8 leaf at mi(10,0) forked exactly here: ours |
| 13013 | // read `tx_size_cat1` (rng 55700->49918) where the oracle's next |
| 13014 | // read was the leaf's own txb_skip. |
| 13015 | let ctx = tx_size_context_txfm_rect(neighbours, leaf_mi, bw, bh, fctx); |
| 13016 | // lane-rectsplitx r1: `bsize_to_tx_size_cat` (libaom `blockd.h`, |
| 13017 | // `bsize_to_tx_size_depth_table`) is 2 for BLOCK_16X8/BLOCK_8X16, so |
| 13018 | // the category is 1 -- NOT the 2 the 32x16 strips read. Same symbol |
| 13019 | // count (3), different CDF row: a wrong-table read of a real symbol, |
| 13020 | // the class that cost lane-rectsplit r1 its 64x32 depth. |
| 13021 | // lane-rect1d r1: the CATEGORY of this leaf is 1, not 2. libaom's |
| 13022 | // `bsize_to_tx_size_cat` reads `bsize_to_tx_size_depth_table` and |
| 13023 | // subtracts one: BLOCK_16X8/8X16 (and the 1:4 BLOCK_16X4/4X16 this |
| 13024 | // function also decodes) sit at depth 2, so they read |
| 13025 | // `tx_size_cdf[1]` -- the same table a 16x16 square block reads -- |
| 13026 | // while `tx_size_cat2` belongs to the 32-level shapes |
| 13027 | // ([`decode_block_rect`]). Both tables carry three symbols, so the |
| 13028 | // wrong one decodes a plausible depth from the wrong probabilities: |
| 13029 | // the range diverges silently at the first such leaf and every later |
| 13030 | // symbol in the tile is read from the wrong place (class |
| 13031 | // `wrong-alphabet-same-value`, here same alphabet / wrong row). |
| 13032 | let d = dec.symbol(&mut cdfs.tx_size_cat1[ctx]); |
| 13033 | if crate::envflags::env_flag!("EC_TRACE_MODE_STEP") { |
| 13034 | eprintln!("EC_ISTEP mi_row={} mi_col={} name=tx_depth val={d} ctx={ctx} cat=1 rng={}", leaf_mi.0, leaf_mi.1, dec.debug_state().0); |
| 13035 | } |
| 13036 | d |
| 13037 | } else { |
| 13038 | 0 |
| 13039 | }; |
| 13040 | // lane-av1-intrabc r3: this leaf's own `set_txfm_ctxs` when the frame reads |
| 13041 | // the `TXFM_CONTEXT` bands -- see [`publish_txfm_bands_if_in_inter`]. |
| 13042 | publish_txfm_bands_if_in_inter( |
| 13043 | neighbours, |
| 13044 | leaf_mi, |
| 13045 | depth_to_tx_wh(bw, bh, depth, fctx), |
| 13046 | (bw, bh), fctx, |
| 13047 | ); |
| 13048 | if depth != 0 || lossless(fctx) { |
| 13049 | // The same per-transform-unit walk the bigger strips take |
| 13050 | // (`depth_to_tx_wh`: TX_16X8 -> TX_8X8 at depth 1, -> TX_4X4 at |
| 13051 | // depth 2), addressed at this leaf's own mi position. |
| 13052 | hit!(TX_DEPTH_HITS); |
| 13053 | hit!(SUB16_SPLIT_HITS); |
| 13054 | let (tx_w, tx_h) = depth_to_tx_wh(bw, bh, depth, fctx); |
| 13055 | let modes = RectStripModes { |
| 13056 | skip, |
| 13057 | mode, |
| 13058 | angle_delta_y, |
| 13059 | uv_predict_mode, |
| 13060 | angle_delta_uv, |
| 13061 | alpha, |
| 13062 | filter_intra, |
| 13063 | smooth_neighbor, |
| 13064 | // lane-rectsplitx r3: the split arm hard-coded `false` here while |
| 13065 | // the unsplit arm below passes the real neighbour answer -- chroma |
| 13066 | // of a tx-split 16x8/8x16 leaf lost its intra-edge filter type. |
| 13067 | smooth_neighbor_uv, |
| 13068 | palette_y: palette_y_buf.clone(), |
| 13069 | palette_uv: palette_uv_bufs.clone(), |
| 13070 | }; |
| 13071 | if palette_y.is_some() || palette_uv.is_some() { |
| 13072 | hit!(PALETTE_SPLIT_TX_HITS); |
| 13073 | } |
| 13074 | decode_rect_split( |
| 13075 | dec, |
| 13076 | cdfs, |
| 13077 | neighbours, |
| 13078 | leaf_mi, |
| 13079 | bw, |
| 13080 | bh, |
| 13081 | tx_w, |
| 13082 | tx_h, |
| 13083 | &modes, |
| 13084 | y, |
| 13085 | u, |
| 13086 | v, |
| 13087 | base_q_idx, |
| 13088 | reduced_tx_set, fctx, |
| 13089 | )?; |
| 13090 | record_strip_palette( |
| 13091 | neighbours, pal_at, bw, bh, pal_y_size, pal_y_colors, pal_uv_size, pal_uv_colors, |
| 13092 | ); |
| 13093 | return Ok(mode); |
| 13094 | } |
| 13095 | let (px, py) = (leaf_mi.1 * MI, leaf_mi.0 * MI); |
| 13096 | let reach = Reach::of_rect(bw, bh, px, py, y.width, y.height, fctx); |
| 13097 | let (cpx, cpy) = (px >> ss_x(fctx), py >> ss_y(fctx)); |
| 13098 | let (chroma_w, chroma_h) = (bw >> ss_x(fctx), bh >> ss_y(fctx)); |
| 13099 | let (luma_levels, u_levels, v_levels); |
| 13100 | if skip { |
| 13101 | luma_levels = Grid::Zero(bw * bh); |
| 13102 | u_levels = Grid::Zero(chroma_w * chroma_h); |
| 13103 | v_levels = Grid::Zero(chroma_w * chroma_h); |
| 13104 | if let Some(b) = &palette_y_buf { |
| 13105 | set_palette_pred(b.clone(), fctx); |
| 13106 | } |
| 13107 | push_intra_rect(0, |
| 13108 | px, py, bw, bh, mode, angle_delta_y, reach, &luma_levels, None, filter_intra, |
| 13109 | smooth_neighbor, fctx, |
| 13110 | ); |
| 13111 | let ac = alpha.map(|_| cfl_src_rect(px, py, bw, bh)); |
| 13112 | if let Some((ub, _)) = &palette_uv_bufs { |
| 13113 | set_palette_pred(ub.clone(), fctx); |
| 13114 | } |
| 13115 | push_intra_rect(1, |
| 13116 | cpx, cpy, chroma_w, chroma_h, uv_predict_mode, angle_delta_uv, reach, &u_levels, |
| 13117 | alpha.zip(ac).map(|((au, _), ac)| (au, ac)), None, smooth_neighbor_uv, fctx, |
| 13118 | ); |
| 13119 | if let Some((_, vb)) = &palette_uv_bufs { |
| 13120 | set_palette_pred(vb.clone(), fctx); |
| 13121 | } |
| 13122 | push_intra_rect(2, |
| 13123 | cpx, cpy, chroma_w, chroma_h, uv_predict_mode, angle_delta_uv, reach, &v_levels, |
| 13124 | alpha.zip(ac).map(|((_, av), ac)| (av, ac)), None, smooth_neighbor_uv, fctx, |
| 13125 | ); |
| 13126 | } else { |
| 13127 | // lane-rectx: a genuine 16x8/8x16 luma transform (`TxbSet::LumaRect16x8`, |
| 13128 | // real `EOB_PT_128_LUMA` alphabet + the same mode-indexed `tx_type` |
| 13129 | // symbol the square `Luma16` set reads -- `get_ext_tx_set_type`'s |
| 13130 | // `use_reduced_set` branch returns `EXT_TX_SET_DTT4_IDTX` at |
| 13131 | // `tx_size_sqr_up == TX_16X16` regardless of the true, non-square |
| 13132 | // `tx_size`) and its chroma half (8x4/4x8, `TxbSet::ChromaRect8x4`). |
| 13133 | // lane-av1-422e: a 4:2:2 strip halves only the width, so the chroma |
| 13134 | // half of a 16x8/8x16 luma leaf is an 8x8 SQUARE (`TxbSet::Chroma8` + |
| 13135 | // the default scan -- the pairing any 8x8 chroma plane reads). 4:2:0 |
| 13136 | // halves both axes (8x4/4x8 rect, `TxbSet::ChromaRect8x4`); 4:4:4 |
| 13137 | // leaves the full 16x8/8x16 rect (`TxbSet::ChromaRect16x8`). |
| 13138 | let chroma_422 = ss_x(fctx) == 1 && ss_y(fctx) == 0; |
| 13139 | let (luma_scan, chroma_scan): (&[u16], &[u16]) = if ss_x(fctx) == 0 { |
| 13140 | if bw == 16 { (&SCAN_16X8, &SCAN_16X8) } else { (&SCAN_8X16, &SCAN_8X16) } |
| 13141 | } else if chroma_422 { |
| 13142 | if bw == 16 { (&SCAN_16X8, default_scan(8)) } else { (&SCAN_8X16, default_scan(8)) } |
| 13143 | } else if bw == 16 { |
| 13144 | (&SCAN_16X8, &SCAN_8X4) |
| 13145 | } else { |
| 13146 | (&SCAN_8X16, &SCAN_4X8) |
| 13147 | }; |
| 13148 | let chroma_rect_set = if ss_x(fctx) == 0 { |
| 13149 | TxbSet::ChromaRect16x8 |
| 13150 | } else if chroma_422 { |
| 13151 | TxbSet::Chroma8 |
| 13152 | } else { |
| 13153 | TxbSet::ChromaRect8x4 |
| 13154 | }; |
| 13155 | let around = neighbours.around_mi_rect(leaf_mi, bw, bh); |
| 13156 | let luma_set = if reduced_tx_set { |
| 13157 | TxbSet::LumaRect16x8 |
| 13158 | } else { |
| 13159 | TxbSet::LumaRect16x8Set1 |
| 13160 | }; |
| 13161 | let mut luma_coding = cdfs.txb(luma_set, fi_tx_row(mode, filter_intra)); |
| 13162 | let (l_levels, luma_tx_type) = read_coeffs_rect( |
| 13163 | dec, &mut luma_coding, luma_scan, bw, bh, 0, dc_sign_ctx(around[0].2), TxType::DctDct, |
| 13164 | )?; |
| 13165 | if crate::envflags::env_flag!("EC_AV1_RECTX_TRACE") { |
| 13166 | let nz: Vec<(usize, i32)> = l_levels.iter().copied().enumerate().filter(|(_, v)| *v != 0).collect(); |
| 13167 | eprintln!(" luma_tx_type={luma_tx_type:?} nz_levels={nz:?}"); |
| 13168 | } |
| 13169 | luma_levels = l_levels; |
| 13170 | let luma_residual = dequant_and_inverse_typed_wh( |
| 13171 | &luma_levels, bw, bh, crate::decode::bit_depth(fctx), |
| 13172 | block_q_idx(fctx), plane_q_delta(0, fctx).0, plane_q_delta(0, fctx).1, luma_tx_type, |
| 13173 | block_iqmatrix(fctx, 0, bw, bh, luma_tx_type), |
| 13174 | ); |
| 13175 | if let Some(b) = &palette_y_buf { |
| 13176 | set_palette_pred(b.clone(), fctx); |
| 13177 | } |
| 13178 | push_intra_rect(0, |
| 13179 | px, py, bw, bh, mode, angle_delta_y, reach, &luma_residual, None, filter_intra, |
| 13180 | smooth_neighbor, fctx, |
| 13181 | ); |
| 13182 | let ac = alpha.map(|_| cfl_src_rect(px, py, bw, bh)); |
| 13183 | let u_default_tx = default_intra_tx_type(uv_predict_mode as u8); |
| 13184 | let u_skip_ctx = usize::from(around[1].0) + usize::from(around[1].1); |
| 13185 | let mut u_coding = cdfs.txb(chroma_rect_set, uv_predict_mode); |
| 13186 | let (u_l, u_tx_type) = read_chroma_coeffs_rect( |
| 13187 | dec, &mut u_coding, chroma_scan, chroma_w, chroma_h, u_skip_ctx, |
| 13188 | dc_sign_ctx(around[1].2), u_default_tx, fctx, |
| 13189 | )?; |
| 13190 | u_levels = u_l; |
| 13191 | let u_residual = dequant_and_inverse_typed_wh( |
| 13192 | &u_levels, chroma_w, chroma_h, crate::decode::bit_depth(fctx), |
| 13193 | block_q_idx(fctx), plane_q_delta(1, fctx).0, plane_q_delta(1, fctx).1, u_tx_type, |
| 13194 | block_iqmatrix(fctx, 1, chroma_w, chroma_h, u_tx_type), |
| 13195 | ); |
| 13196 | if let Some((ub, _)) = &palette_uv_bufs { |
| 13197 | set_palette_pred(ub.clone(), fctx); |
| 13198 | } |
| 13199 | push_intra_rect(1, |
| 13200 | cpx, cpy, chroma_w, chroma_h, uv_predict_mode, angle_delta_uv, reach, &u_residual, |
| 13201 | alpha.zip(ac).map(|((au, _), ac)| (au, ac)), None, smooth_neighbor_uv, fctx, |
| 13202 | ); |
| 13203 | let v_default_tx = default_intra_tx_type(uv_predict_mode as u8); |
| 13204 | let v_skip_ctx = usize::from(around[2].0) + usize::from(around[2].1); |
| 13205 | let mut v_coding = cdfs.txb(chroma_rect_set, uv_predict_mode); |
| 13206 | let (v_l, v_tx_type) = read_chroma_coeffs_rect( |
| 13207 | dec, &mut v_coding, chroma_scan, chroma_w, chroma_h, v_skip_ctx, |
| 13208 | dc_sign_ctx(around[2].2), v_default_tx, fctx, |
| 13209 | )?; |
| 13210 | v_levels = v_l; |
| 13211 | let v_residual = dequant_and_inverse_typed_wh( |
| 13212 | &v_levels, chroma_w, chroma_h, crate::decode::bit_depth(fctx), |
| 13213 | block_q_idx(fctx), plane_q_delta(2, fctx).0, plane_q_delta(2, fctx).1, v_tx_type, |
| 13214 | block_iqmatrix(fctx, 2, chroma_w, chroma_h, v_tx_type), |
| 13215 | ); |
| 13216 | if let Some((_, vb)) = &palette_uv_bufs { |
| 13217 | set_palette_pred(vb.clone(), fctx); |
| 13218 | } |
| 13219 | push_intra_rect(2, |
| 13220 | cpx, cpy, chroma_w, chroma_h, uv_predict_mode, angle_delta_uv, reach, &v_residual, |
| 13221 | alpha.zip(ac).map(|((_, av), ac)| (av, ac)), None, smooth_neighbor_uv, fctx, |
| 13222 | ); |
| 13223 | hit!(RECT_LEAF_COEFF_HITS); |
| 13224 | } |
| 13225 | neighbours.record_mi_rect(leaf_mi, bw, bh, &[&luma_levels[..], &u_levels[..], &v_levels[..]]); |
| 13226 | neighbours.fill_skip_grid_rect(leaf_mi, mi_w, mi_h, skip); |
| 13227 | neighbours.fill_lf_grid_rect(leaf_mi, mi_w, mi_h, bw as u8, bh as u8, 0, fctx); |
| 13228 | // The leaf's own palette state (`0` clears, as this site always did |
| 13229 | // while a palette leaf refused -- lane-palette2 r8), written on the SPLIT |
| 13230 | // AXIS only until the pair's closing leaf: the coarse cell holds one |
| 13231 | // entry for both leaves, and leaf 0 zeroing the LEFT one threw away the |
| 13232 | // real left neighbour leaf 1 reads (lane-kf900 r4). |
| 13233 | record_strip_palette( |
| 13234 | neighbours, pal_at, bw, bh, pal_y_size, pal_y_colors, pal_uv_size, pal_uv_colors, |
| 13235 | ); |
| 13236 | // A rect leaf spans mi_w x mi_h, so its mode reaches several columns and |
| 13237 | // rows -- record the whole span, not one cell. |
| 13238 | neighbours.record_mode_mi(leaf_mi.0, leaf_mi.1, mi_w, mi_h, mode); |
| 13239 | neighbours.record_uv_mode_mi(leaf_mi.0, leaf_mi.1, mi_w, mi_h, uv_predict_mode); |
| 13240 | Ok(mode) |
| 13241 | } |
| 13242 | |
| 13243 | /// Decodes one 32x8 / 8x32 strip of a 32x32-level `PARTITION_HORZ_4` / |
| 13244 | /// `PARTITION_VERT_4` (lane-tx64x16 r3): [`decode_block_rect`]'s 4:1 sibling, |
| 13245 | /// addressed by REAL mi coordinates for the same reason [`decode_leaf_rect`] |
| 13246 | /// is -- strips 1 and 3 start 8 px into a 16-px [`SUB`] cell, which an |
| 13247 | /// `at`-derived `px = c * SUB` cannot name. `prev_strip` carries the previous |
| 13248 | /// strip's mode so this one's mode context sees its true above/left |
| 13249 | /// neighbour inside the same 32x32 (`decode_leaf_rect`'s convention). |
| 13250 | /// |
| 13251 | /// Tables: LUMA `TX_32X8`/`TX_8X32` averages to the 16x16 entropy context |
| 13252 | /// ([`TxbSet::LumaRect32x8`], 256 positions) and scans with |
| 13253 | /// [`SCAN_32X8`]/[`SCAN_8X32`]; CHROMA `TX_16X4`/`TX_4X16` averages to the |
| 13254 | /// 8x8 one ([`TxbSet::Chroma8`], 64 positions) and scans with |
| 13255 | /// [`SCAN_16X4`]/[`SCAN_4X16`]. Neither shape takes the rect sqrt2 scale -- |
| 13256 | /// `get_rect_tx_log_ratio` fires at ratio 1 only, and this is ratio 2 |
| 13257 | /// (`inverse_transform_2d_typed_wh`'s own `rect_scale`). |
| 13258 | /// |
| 13259 | /// A split transform (`tx_depth != 0`) refuses by name: `depth_to_tx_size` of |
| 13260 | /// a 4:1 strip is not simply `bw.min(bh) >> (depth - 1)` the way a 2:1 |
| 13261 | /// strip's is, so [`decode_rect_split`]'s per-unit walk cannot be reused |
| 13262 | /// unexamined. |
| 13263 | #[allow(clippy::too_many_arguments)] |
| 13264 | fn decode_block_rect4( |
| 13265 | dec: &mut SymbolDecoder, |
| 13266 | cdfs: &mut Cdfs, |
| 13267 | neighbours: &mut Neighbours, |
| 13268 | strip_mi: (usize, usize), |
| 13269 | bw: usize, |
| 13270 | bh: usize, |
| 13271 | prev_strip: Option<((usize, usize), usize)>, |
| 13272 | y: &mut PlaneBuf<'static>, |
| 13273 | u: &mut PlaneBuf<'static>, |
| 13274 | v: &mut PlaneBuf<'static>, |
| 13275 | enable_filter_intra: bool, |
| 13276 | allow_screen_content_tools: bool, |
| 13277 | base_q_idx: u8, |
| 13278 | tx_select: bool, fctx: &crate::decode::FrameCtx, |
| 13279 | ) -> Result<(usize, usize)> { |
| 13280 | let _ = base_q_idx; |
| 13281 | let (mi_r, mi_c) = strip_mi; |
| 13282 | let (r, c) = (mi_r / (SUB / MI), mi_c / (SUB / MI)); |
| 13283 | let (mi_w, mi_h) = (bw / MI, bh / MI); |
| 13284 | let mut above_mode = neighbours.above_mode[c]; |
| 13285 | let mut left_mode = neighbours.left_mode[r]; |
| 13286 | if let Some(((pr, pc), pmode)) = prev_strip { |
| 13287 | if pc == mi_c && mi_r == pr + mi_h { |
| 13288 | above_mode = pmode; |
| 13289 | } else if pr == mi_r && mi_c == pc + mi_w { |
| 13290 | left_mode = pmode; |
| 13291 | } |
| 13292 | } |
| 13293 | // lane-rectsplitx r2, ROOT CAUSE of the 1:4 mismatch: mi-exact + |
| 13294 | // availability-correct neighbour modes (see |
| 13295 | // [`Neighbours::modes_above_left_mi`]). The coarse 16x16 bands alias every |
| 13296 | // 8-px strip of a `HORZ_4`/`VERT_4` onto one cell. |
| 13297 | let (above_mode, left_mode) = |
| 13298 | neighbours.modes_above_left_mi(mi_r, mi_c, (above_mode, left_mode)); |
| 13299 | // lane-kf900 r4: this strip's own palette-Y mode ctx and colour cache. |
| 13300 | // Passing `None`/`&[]` read `palette_y_mode` off CDF row 0 and every |
| 13301 | // `palette_colors_y` cache hit off an EMPTY cache -- right symbol count, |
| 13302 | // wrong interval, so a screen-content tile desynced at the first strip |
| 13303 | // whose neighbour used a palette (class [[cdf-row-held-constant]]). |
| 13304 | let pal_at = (mi_r, mi_c); |
| 13305 | let (palette_ctx, palette_cache) = neighbours.palette_ctx_and_cache_mi(pal_at); |
| 13306 | let palette_uv_cache = neighbours.palette_uv_cache_mi(pal_at); |
| 13307 | let ( |
| 13308 | skip, |
| 13309 | mode, |
| 13310 | angle_delta_y, |
| 13311 | uv_mode, |
| 13312 | angle_delta_uv, |
| 13313 | alpha, |
| 13314 | filter_intra, |
| 13315 | palette_y, |
| 13316 | palette_uv, |
| 13317 | intrabc, |
| 13318 | ) = read_intra_mode_rect( |
| 13319 | dec, |
| 13320 | cdfs, |
| 13321 | above_mode, |
| 13322 | left_mode, |
| 13323 | true, |
| 13324 | true, |
| 13325 | bw, |
| 13326 | bh, |
| 13327 | enable_filter_intra, |
| 13328 | neighbours.skip_txfm_ctx(mi_r, mi_c), |
| 13329 | allow_screen_content_tools, |
| 13330 | Some((palette_ctx, &palette_cache)), |
| 13331 | &palette_uv_cache, |
| 13332 | mi_r, |
| 13333 | mi_c, fctx, |
| 13334 | )?; |
| 13335 | if let Some((dv_row, dv_col)) = intrabc { |
| 13336 | decode_intrabc_rect( |
| 13337 | dec, cdfs, neighbours, strip_mi, (bw, bh), skip, (dv_row, dv_col), |
| 13338 | y, u, v, base_q_idx, fctx, |
| 13339 | )?; |
| 13340 | return Ok((DC_PRED, DC_PRED)); |
| 13341 | } |
| 13342 | // lane-kf900 r4: palette prediction on this shape, exactly as |
| 13343 | // [`decode_block_rect`] does one aspect ratio up -- the colour-index map |
| 13344 | // mapped through the base colours IS the prediction (it reads no edge |
| 13345 | // pixels), handed to each `reconstruct_rect` through [`set_palette_pred`]. |
| 13346 | if palette_y.is_some() || palette_uv.is_some() { |
| 13347 | hit!(RECT4_PALETTE_HITS); |
| 13348 | } |
| 13349 | let palette_y_buf: Option<Vec<u16>> = palette_y |
| 13350 | .as_ref() |
| 13351 | .map(|p| p.map.iter().map(|&i| p.colors[i as usize]).collect()); |
| 13352 | let palette_uv_bufs: Option<(Vec<u16>, Vec<u16>)> = palette_uv.as_ref().map(|p| { |
| 13353 | ( |
| 13354 | p.map.iter().map(|&i| p.u_colors[i as usize]).collect(), |
| 13355 | p.map.iter().map(|&i| p.v_colors[i as usize]).collect(), |
| 13356 | ) |
| 13357 | }); |
| 13358 | let (pal_y_size, pal_y_colors) = |
| 13359 | palette_y.as_ref().map_or((0, [0u16; 8]), |p| (p.size, p.colors)); |
| 13360 | let (pal_uv_size, pal_uv_colors) = |
| 13361 | palette_uv.as_ref().map_or((0, [0u16; 8]), |p| (p.size, p.u_colors)); |
| 13362 | let smooth_neighbor = is_smooth_mode(above_mode) || is_smooth_mode(left_mode); |
| 13363 | if smooth_neighbor { |
| 13364 | hit!(SMOOTH_LUMA_HITS); |
| 13365 | } |
| 13366 | // lane-rectsplitx r2: this strip's REAL mi, not the [`SUB`] cell's |
| 13367 | // top-left -- a 1:4 strip starts 8 px into a 16-px cell, so |
| 13368 | // `r * (SUB / MI)` names a different mi column and the chroma |
| 13369 | // edge-filter strength was read from the wrong neighbour (measured: |
| 13370 | // seed 43, the last 8x32 strip at luma (152,32), chroma-only mismatch |
| 13371 | // with luma bit-exact). |
| 13372 | let smooth_neighbor_uv = neighbours.smooth_uv_neighbour(mi_r, mi_c, r, c, fctx); |
| 13373 | if filter_intra.is_some() { |
| 13374 | hit!(FILTER_INTRA_RECT_HITS); |
| 13375 | if bw > bh { |
| 13376 | hit!(FILTER_INTRA_RECT4_HORZ_HITS); |
| 13377 | } else { |
| 13378 | hit!(FILTER_INTRA_RECT4_VERT_HITS); |
| 13379 | } |
| 13380 | } |
| 13381 | let uv_predict_mode = if uv_mode == UV_CFL_PRED { |
| 13382 | DC_PRED |
| 13383 | } else { |
| 13384 | uv_mode |
| 13385 | }; |
| 13386 | // lane-av1mixloss: the lossless-segment early return (`decodeframe.c:1203`) |
| 13387 | // precedes `TX_MODE_SELECT`, so a lossless-segment rect4 block codes no |
| 13388 | // depth symbol here either (class sweep of the sub8 leaf's fix). |
| 13389 | let depth = if tx_select && !lossless(fctx) { |
| 13390 | // lane-av1txbands: EVERY frame now reads the real `TXFM_CONTEXT` |
| 13391 | // bands (`get_tx_size_context`, libaom `pred_common.h:342`) -- an |
| 13392 | // intra-only frame publishes them from every block too |
| 13393 | // ([`Neighbours::fill_lf_grid_rect`]). Measured on the 10-bit |
| 13394 | // 1920x792 128-superblock stream, |
| 13395 | // decode-order frame 1: the 32x8 HORZ_4 strip at mi(48,176) read |
| 13396 | // `tx_size_cat2` row 1 where libaom reads row 2. |
| 13397 | let ctx = tx_size_context_txfm_rect(neighbours, (mi_r, mi_c), bw, bh, fctx); |
| 13398 | let d = dec.symbol(&mut cdfs.tx_size_cat2[ctx]); |
| 13399 | if crate::envflags::env_flag!("EC_TRACE_MODE_STEP") { |
| 13400 | eprintln!( |
| 13401 | "EC_ISTEP mi_row={mi_r} mi_col={mi_c} name=tx_depth val={d} ctx={ctx} cat=2 rng={}", |
| 13402 | dec.debug_state().0 |
| 13403 | ); |
| 13404 | } |
| 13405 | d |
| 13406 | } else { |
| 13407 | 0 |
| 13408 | }; |
| 13409 | // lane-t900 r1: this strip's own `set_txfm_ctxs` when it is an intra |
| 13410 | // block of an INTER frame -- see [`publish_txfm_bands_if_in_inter`]. |
| 13411 | publish_txfm_bands_if_in_inter( |
| 13412 | neighbours, |
| 13413 | (mi_r, mi_c), |
| 13414 | depth_to_tx_wh(bw, bh, depth, fctx), |
| 13415 | (bw, bh), fctx, |
| 13416 | ); |
| 13417 | if depth != 0 || lossless(fctx) { |
| 13418 | // lane-rectsplitx r1: the per-transform-unit walk, at this strip's |
| 13419 | // real mi position (strips 1 and 3 of a HORZ_4 start 8 px into a |
| 13420 | // 16-px [`SUB`] cell). depth 1 is the RECT unit TX_16X8/TX_8X16 |
| 13421 | // (`sub_tx_size_map[TX_32X8] == TX_16X8`), depth 2 the square TX_8X8; |
| 13422 | // both interleave in every real stream. |
| 13423 | hit!(TX_DEPTH_HITS); |
| 13424 | let (tx_w, tx_h) = depth_to_tx_wh(bw, bh, depth, fctx); |
| 13425 | // lane-rectsplitx r2: both depths ship together, as the r1 handoff |
| 13426 | // predicted -- depth 1 fires FIRST in every attempt that splits at |
| 13427 | // all, so a depth-2-only lift could never be gated. The r1 mismatch |
| 13428 | // was never the rect unit: it was the neighbour MODE the strip reads |
| 13429 | // (see `modes_above_left_mi`). |
| 13430 | let which = if depth == 1 { |
| 13431 | &RECT4_SPLIT_DEPTH1_HITS |
| 13432 | } else { |
| 13433 | &RECT4_SPLIT_DEPTH2_HITS |
| 13434 | }; |
| 13435 | hit_do! { which.with(|c| c.set(c.get() + 1)); } |
| 13436 | let modes = RectStripModes { |
| 13437 | skip, |
| 13438 | mode, |
| 13439 | angle_delta_y, |
| 13440 | uv_predict_mode, |
| 13441 | angle_delta_uv, |
| 13442 | alpha, |
| 13443 | filter_intra, |
| 13444 | smooth_neighbor, |
| 13445 | smooth_neighbor_uv, |
| 13446 | palette_y: palette_y_buf.clone(), |
| 13447 | palette_uv: palette_uv_bufs.clone(), |
| 13448 | }; |
| 13449 | if palette_y.is_some() || palette_uv.is_some() { |
| 13450 | hit!(PALETTE_SPLIT_TX_HITS); |
| 13451 | } |
| 13452 | decode_rect_split( |
| 13453 | dec, |
| 13454 | cdfs, |
| 13455 | neighbours, |
| 13456 | (mi_r, mi_c), |
| 13457 | bw, |
| 13458 | bh, |
| 13459 | tx_w, |
| 13460 | tx_h, |
| 13461 | &modes, |
| 13462 | y, |
| 13463 | u, |
| 13464 | v, |
| 13465 | base_q_idx, |
| 13466 | false, fctx, |
| 13467 | )?; |
| 13468 | record_strip_palette( |
| 13469 | neighbours, pal_at, bw, bh, pal_y_size, pal_y_colors, pal_uv_size, |
| 13470 | pal_uv_colors, |
| 13471 | ); |
| 13472 | if bw > bh { |
| 13473 | hit!(RECT4_32_HORZ_HITS); |
| 13474 | } else { |
| 13475 | hit!(RECT4_32_VERT_HITS); |
| 13476 | } |
| 13477 | if !skip { |
| 13478 | hit!(RECT4_COEFF_HITS); |
| 13479 | } |
| 13480 | // lane-frame36 r2 (same shape as `decode_block_rect64`'s split arm, |
| 13481 | // swept this round): `set_txfm_ctxs` runs for EVERY block of an inter |
| 13482 | // frame, so this 1:4 strip -- which is also the INTER frame's reader |
| 13483 | // for 32x8/8x32 ([`decode_intra_rect4_in_inter`]) -- publishes its |
| 13484 | // transform size in both TXFM_CONTEXT bands. Without it the next block |
| 13485 | // reads the row's stale value. |
| 13486 | txfm_partition_update_rect(neighbours, (mi_r, mi_c), (tx_w, tx_h), (bw, bh)); |
| 13487 | return Ok((mode, uv_predict_mode)); |
| 13488 | } |
| 13489 | // The coarse (16-px [`SUB`] cell) mode/side arrays cannot name a strip |
| 13490 | // that is 8 px tall, so each strip stamps every cell it touches and the |
| 13491 | // LAST strip wins -- which is the right answer for the block below/right |
| 13492 | // of the 32x32, the only reader outside it. Written after this strip's |
| 13493 | // own context read above, never before. |
| 13494 | for cell in 0..bw.div_ceil(SUB) { |
| 13495 | neighbours.above_mode[c + cell] = mode; |
| 13496 | neighbours.above_uv_mode[c + cell] = uv_predict_mode; |
| 13497 | neighbours.above_side[c + cell] = bw; |
| 13498 | } |
| 13499 | for cell in 0..bh.div_ceil(SUB) { |
| 13500 | neighbours.left_mode[r + cell] = mode; |
| 13501 | neighbours.left_uv_mode[r + cell] = uv_predict_mode; |
| 13502 | neighbours.left_side[r + cell] = bh; |
| 13503 | } |
| 13504 | // ...and the mi-EXACT maps the coarse cells alias away, exactly as |
| 13505 | // [`decode_leaf_rect`] does (the split arm above records these through |
| 13506 | // `record_mi_luma_rect`). Without this an unsplit 32x8 strip left |
| 13507 | // `sub8_mode_row` holding the previous block, and the next 32x32's |
| 13508 | // strips read a stale `kf_y_mode` row (measured: mi(4,8) on the band |
| 13509 | // fixture). |
| 13510 | neighbours.record_mode_mi(mi_r, mi_c, mi_w, mi_h, mode); |
| 13511 | neighbours.record_uv_mode_mi(mi_r, mi_c, mi_w, mi_h, uv_predict_mode); |
| 13512 | let (px, py) = (mi_c * MI, mi_r * MI); |
| 13513 | let (cpx, cpy) = (px >> ss_x(fctx), py >> ss_y(fctx)); |
| 13514 | let (chroma_w, chroma_h) = (bw >> ss_x(fctx), bh >> ss_y(fctx)); |
| 13515 | let reach = Reach::of_rect(bw, bh, px, py, y.width, y.height, fctx); |
| 13516 | // lane-av1-444: the strip's chroma shape follows the subsampling. |
| 13517 | // 4:2:0 codes 16x4/4x16 (`TxbSet::Chroma8`); 4:4:4 codes the strip |
| 13518 | // itself, 32x8/8x32 (`get_txsize_entropy_ctx(TX_32X8)` = |
| 13519 | // `(wide_log2 + high_log2 + 1) >> 1` = the 16x16 set, |
| 13520 | // `TxbSet::Chroma16`). The caller set is closed -- this reader only |
| 13521 | // ever sees 32x8 / 8x32 luma and the sequence header admits ss 0/0 |
| 13522 | // and 1/1 only -- so the table is exhaustive. |
| 13523 | let (chroma_set, chroma_scan): (TxbSet, &[u16]) = match (chroma_w, chroma_h) { |
| 13524 | (16, 4) => (TxbSet::Chroma8, &SCAN_16X4[..]), |
| 13525 | (4, 16) => (TxbSet::Chroma8, &SCAN_4X16[..]), |
| 13526 | (32, 8) => (TxbSet::Chroma16, &SCAN_32X8[..]), |
| 13527 | (8, 32) => (TxbSet::Chroma16, &SCAN_8X32[..]), |
| 13528 | // lane-av1-422: a 4:2:2 1:4 strip halves only the width, so the |
| 13529 | // landscape leaves (32x8, 16x4 luma) code their chroma as the RECT |
| 13530 | // (16,8)/(8,4) -- the same TxbSet/scan pair the (16,8)/(8,4) rows of |
| 13531 | // [`decode_rect_split`]'s chroma table use. The portrait leaves are |
| 13532 | // unreachable: ss_size_lookup marks them BLOCK_INVALID at (1,0), so |
| 13533 | // a conformant 4:2:2 stream cannot carry that leaf size. |
| 13534 | (16, 8) => (TxbSet::ChromaRect16x8, &SCAN_16X8[..]), |
| 13535 | (8, 4) => (TxbSet::ChromaRect8x4, &SCAN_8X4[..]), |
| 13536 | _ => unreachable!( |
| 13537 | "decode_block_rect4 chroma shape set is closed: {chroma_w}x{chroma_h}" |
| 13538 | ), |
| 13539 | }; |
| 13540 | let luma_scan: &[u16] = if bw > bh { &SCAN_32X8 } else { &SCAN_8X32 }; |
| 13541 | let (luma_levels, u_levels, v_levels) = if skip { |
| 13542 | ( |
| 13543 | vec![0i32; bw * bh], |
| 13544 | vec![0i32; chroma_w * chroma_h], |
| 13545 | vec![0i32; chroma_w * chroma_h], |
| 13546 | ) |
| 13547 | } else { |
| 13548 | let around = neighbours.around_mi_rect((mi_r, mi_c), bw, bh); |
| 13549 | let mut luma_coding = cdfs.txb(TxbSet::LumaRect32x8, mode); |
| 13550 | let (luma_levels, luma_tx_type) = read_coeffs_rect( |
| 13551 | dec, |
| 13552 | &mut luma_coding, |
| 13553 | luma_scan, |
| 13554 | bw, |
| 13555 | bh, |
| 13556 | 0, |
| 13557 | dc_sign_ctx(around[0].2), |
| 13558 | TxType::DctDct, |
| 13559 | )?; |
| 13560 | let mut planes = Vec::with_capacity(2); |
| 13561 | for plane in 1..3 { |
| 13562 | // `av1_get_tx_type` (`blockd.h:1278`): the mode-indexed type |
| 13563 | // stands only while it is a member of this tx size's set; a |
| 13564 | // 32x8/8x32 chroma transform (4:4:4) squares up to TX_32X32, |
| 13565 | // whose intra set is `EXT_TX_SET_DCTONLY` -- every ADST clamps |
| 13566 | // to DCT_DCT there, and ADST is undefined at widths above 16. |
| 13567 | let default_tx = if chroma_w.max(chroma_h) >= 32 { |
| 13568 | TxType::DctDct |
| 13569 | } else { |
| 13570 | default_intra_tx_type(uv_predict_mode as u8) |
| 13571 | }; |
| 13572 | let skip_ctx = usize::from(around[plane].0) + usize::from(around[plane].1); |
| 13573 | let mut coding = cdfs.txb(chroma_set, uv_predict_mode); |
| 13574 | let (levels, tx_type) = read_chroma_coeffs_rect( |
| 13575 | dec, |
| 13576 | &mut coding, |
| 13577 | chroma_scan, |
| 13578 | chroma_w, |
| 13579 | chroma_h, |
| 13580 | skip_ctx, |
| 13581 | dc_sign_ctx(around[plane].2), |
| 13582 | default_tx, |
| 13583 | fctx, |
| 13584 | )?; |
| 13585 | planes.push((levels, tx_type)); |
| 13586 | } |
| 13587 | let (v_levels, v_tx_type) = planes.pop().expect("plane 2"); |
| 13588 | let (u_levels, u_tx_type) = planes.pop().expect("plane 1"); |
| 13589 | hit!(RECT4_COEFF_HITS); |
| 13590 | let luma_residual = dequant_and_inverse_typed_wh( |
| 13591 | &luma_levels, |
| 13592 | bw, |
| 13593 | bh, |
| 13594 | crate::decode::bit_depth(fctx), |
| 13595 | block_q_idx(fctx), |
| 13596 | plane_q_delta(0, fctx).0, |
| 13597 | plane_q_delta(0, fctx).1, |
| 13598 | luma_tx_type, |
| 13599 | block_iqmatrix(fctx, 0, bw, bh, luma_tx_type), |
| 13600 | ); |
| 13601 | if let Some(b) = &palette_y_buf { |
| 13602 | set_palette_pred(b.clone(), fctx); |
| 13603 | } |
| 13604 | push_intra_rect(0, |
| 13605 | px, |
| 13606 | py, |
| 13607 | bw, |
| 13608 | bh, |
| 13609 | mode, |
| 13610 | angle_delta_y, |
| 13611 | reach, |
| 13612 | &luma_residual, |
| 13613 | None, |
| 13614 | filter_intra, |
| 13615 | smooth_neighbor, fctx, |
| 13616 | ); |
| 13617 | let ac = alpha.map(|_| cfl_src_rect(px, py, bw, bh)); |
| 13618 | for (plane, _buf, levels, tx_type) in [ |
| 13619 | (1usize, &mut *u, &u_levels, u_tx_type), |
| 13620 | (2, &mut *v, &v_levels, v_tx_type), |
| 13621 | ] { |
| 13622 | let residual = dequant_and_inverse_typed_wh( |
| 13623 | levels, |
| 13624 | chroma_w, |
| 13625 | chroma_h, |
| 13626 | crate::decode::bit_depth(fctx), |
| 13627 | block_q_idx(fctx), |
| 13628 | plane_q_delta(plane, fctx).0, |
| 13629 | plane_q_delta(plane, fctx).1, |
| 13630 | tx_type, |
| 13631 | block_iqmatrix(fctx, plane, chroma_w, chroma_h, tx_type), |
| 13632 | ); |
| 13633 | if let Some((ub, vb)) = &palette_uv_bufs { |
| 13634 | set_palette_pred(if plane == 1 { ub.clone() } else { vb.clone() }, fctx); |
| 13635 | } |
| 13636 | push_intra_rect(plane, |
| 13637 | cpx, |
| 13638 | cpy, |
| 13639 | chroma_w, |
| 13640 | chroma_h, |
| 13641 | uv_predict_mode, |
| 13642 | angle_delta_uv, |
| 13643 | reach, |
| 13644 | &residual, |
| 13645 | alpha |
| 13646 | .zip(ac) |
| 13647 | .map(|((au, av), ac)| (if plane == 1 { au } else { av }, ac)), |
| 13648 | None, |
| 13649 | smooth_neighbor_uv, fctx, |
| 13650 | ); |
| 13651 | } |
| 13652 | neighbours.record_mi_rect( |
| 13653 | (mi_r, mi_c), |
| 13654 | bw, |
| 13655 | bh, |
| 13656 | &[&luma_levels[..], &u_levels[..], &v_levels[..]], |
| 13657 | ); |
| 13658 | neighbours.fill_skip_grid_rect((mi_r, mi_c), mi_w, mi_h, skip); |
| 13659 | neighbours.fill_lf_grid_rect((mi_r, mi_c), mi_w, mi_h, bw as u8, bh as u8, 0, fctx); |
| 13660 | record_strip_palette( |
| 13661 | neighbours, pal_at, bw, bh, pal_y_size, pal_y_colors, pal_uv_size, |
| 13662 | pal_uv_colors, |
| 13663 | ); |
| 13664 | if bw > bh { |
| 13665 | hit!(RECT4_32_HORZ_HITS); |
| 13666 | } else { |
| 13667 | hit!(RECT4_32_VERT_HITS); |
| 13668 | } |
| 13669 | // lane-frame36 r2 (same shape as `decode_block_rect64`'s split arm, |
| 13670 | // swept this round): `set_txfm_ctxs` runs for EVERY block of an inter |
| 13671 | // frame, so this 1:4 strip -- which is also the INTER frame's reader |
| 13672 | // for 32x8/8x32 ([`decode_intra_rect4_in_inter`]) -- publishes its |
| 13673 | // transform size in both TXFM_CONTEXT bands. Without it the next block |
| 13674 | // reads the row's stale value. |
| 13675 | txfm_partition_update_rect(neighbours, (mi_r, mi_c), (bw, bh), (bw, bh)); |
| 13676 | return Ok((mode, uv_predict_mode)); |
| 13677 | }; |
| 13678 | if let Some(b) = &palette_y_buf { |
| 13679 | set_palette_pred(b.clone(), fctx); |
| 13680 | } |
| 13681 | push_intra_rect(0, |
| 13682 | px, |
| 13683 | py, |
| 13684 | bw, |
| 13685 | bh, |
| 13686 | mode, |
| 13687 | angle_delta_y, |
| 13688 | reach, |
| 13689 | &luma_levels, |
| 13690 | None, |
| 13691 | filter_intra, |
| 13692 | smooth_neighbor, fctx, |
| 13693 | ); |
| 13694 | let ac = alpha.map(|_| cfl_src_rect(px, py, bw, bh)); |
| 13695 | if let Some((ub, _)) = &palette_uv_bufs { |
| 13696 | set_palette_pred(ub.clone(), fctx); |
| 13697 | } |
| 13698 | push_intra_rect(1, |
| 13699 | cpx, |
| 13700 | cpy, |
| 13701 | chroma_w, |
| 13702 | chroma_h, |
| 13703 | uv_predict_mode, |
| 13704 | angle_delta_uv, |
| 13705 | reach, |
| 13706 | &u_levels, |
| 13707 | alpha.zip(ac).map(|((au, _), ac)| (au, ac)), |
| 13708 | None, |
| 13709 | smooth_neighbor_uv, fctx, |
| 13710 | ); |
| 13711 | if let Some((_, vb)) = &palette_uv_bufs { |
| 13712 | set_palette_pred(vb.clone(), fctx); |
| 13713 | } |
| 13714 | push_intra_rect(2, |
| 13715 | cpx, |
| 13716 | cpy, |
| 13717 | chroma_w, |
| 13718 | chroma_h, |
| 13719 | uv_predict_mode, |
| 13720 | angle_delta_uv, |
| 13721 | reach, |
| 13722 | &v_levels, |
| 13723 | alpha.zip(ac).map(|((_, av), ac)| (av, ac)), |
| 13724 | None, |
| 13725 | smooth_neighbor_uv, fctx, |
| 13726 | ); |
| 13727 | neighbours.record_mi_rect((mi_r, mi_c), bw, bh, &[&luma_levels[..], &u_levels[..], &v_levels[..]]); |
| 13728 | neighbours.fill_skip_grid_rect((mi_r, mi_c), mi_w, mi_h, skip); |
| 13729 | neighbours.fill_lf_grid_rect((mi_r, mi_c), mi_w, mi_h, bw as u8, bh as u8, 0, fctx); |
| 13730 | record_strip_palette( |
| 13731 | neighbours, pal_at, bw, bh, pal_y_size, pal_y_colors, pal_uv_size, pal_uv_colors, |
| 13732 | ); |
| 13733 | if bw > bh { |
| 13734 | hit!(RECT4_32_HORZ_HITS); |
| 13735 | } else { |
| 13736 | hit!(RECT4_32_VERT_HITS); |
| 13737 | } |
| 13738 | // lane-frame36 r2 (same shape as `decode_block_rect64`'s split arm, |
| 13739 | // swept this round): `set_txfm_ctxs` runs for EVERY block of an inter |
| 13740 | // frame, so this 1:4 strip -- which is also the INTER frame's reader |
| 13741 | // for 32x8/8x32 ([`decode_intra_rect4_in_inter`]) -- publishes its |
| 13742 | // transform size in both TXFM_CONTEXT bands. Without it the next block |
| 13743 | // reads the row's stale value. |
| 13744 | txfm_partition_update_rect(neighbours, (mi_r, mi_c), (bw, bh), (bw, bh)); |
| 13745 | Ok((mode, uv_predict_mode)) |
| 13746 | } |
| 13747 | |
| 13748 | /// Decodes the four 16x4 / 4x16 strips of a 16x16-level `PARTITION_HORZ_4` / |
| 13749 | /// `PARTITION_VERT_4` (lane-r14) -- [`decode_block_rect4`]'s pair one level |
| 13750 | /// down, and the first shape here whose luma block is a single mi row (or |
| 13751 | /// column). |
| 13752 | /// |
| 13753 | /// What is new at this size is CHROMA. `is_chroma_reference` |
| 13754 | /// (`av1_common_int.h`) is false for a 4:2:0 block one mi tall (wide) on an |
| 13755 | /// EVEN mi row (column), so strips 0 and 2 read no `uv_mode`/`cfl_alpha`/ |
| 13756 | /// `angle_delta_uv` at all and code no chroma residual; strips 1 and 3 close |
| 13757 | /// the pair and carry the chroma of the whole 16x8 (8x16) luma pair -- |
| 13758 | /// `setup_pred_plane`'s own `if (subsampling_y && (mi_row & 1) && |
| 13759 | /// mi_size_high[bsize] == 1) mi_row -= 1` offset, i.e. an 8x4 (4x8) chroma |
| 13760 | /// transform at the PAIR's origin, with CfL averaging over the pair's luma. |
| 13761 | /// |
| 13762 | /// Tables: LUMA `TX_16X4`/`TX_4X16` averages to the 8x8 entropy context |
| 13763 | /// ((0 + 2 + 1) >> 1 = 1) with 64 coded positions, so its CDF set is |
| 13764 | /// [`TxbSet::LumaRect16x4`] (`Luma8`'s tables, the `TX_4X4` `tx_type` row); |
| 13765 | /// scans are [`SCAN_16X4`]/[`SCAN_4X16`]. CHROMA `TX_8X4`/`TX_4X8` is |
| 13766 | /// [`TxbSet::ChromaRect8x4`] with [`SCAN_8X4`]/[`SCAN_4X8`]. Angle deltas ARE |
| 13767 | /// coded (`av1_use_angle_delta` compares the BLOCK_SIZES_ALL enum index, and |
| 13768 | /// BLOCK_16X4 is 17), and so is `use_filter_intra` (both sides <= 32) off this |
| 13769 | /// shape's own CDF row. |
| 13770 | /// |
| 13771 | /// A split transform (`tx_depth != 0`, `sub_tx_size_map[TX_16X4] == TX_8X4`) |
| 13772 | /// refuses by name rather than guess-decode. |
| 13773 | #[allow(clippy::too_many_arguments)] |
| 13774 | fn decode_rect4_16( |
| 13775 | dec: &mut SymbolDecoder, |
| 13776 | cdfs: &mut Cdfs, |
| 13777 | neighbours: &mut Neighbours, |
| 13778 | at16: (usize, usize), |
| 13779 | horz: bool, |
| 13780 | y: &mut PlaneBuf<'static>, |
| 13781 | u: &mut PlaneBuf<'static>, |
| 13782 | v: &mut PlaneBuf<'static>, |
| 13783 | enable_filter_intra: bool, |
| 13784 | allow_screen_content_tools: bool, |
| 13785 | base_q_idx: u8, |
| 13786 | tx_select: bool, |
| 13787 | reduced_tx_set: bool, fctx: &crate::decode::FrameCtx, |
| 13788 | ) -> Result<()> { |
| 13789 | let (r, c) = at16; |
| 13790 | let (mi_row0, mi_col0) = (r * (SUB / MI), c * (SUB / MI)); |
| 13791 | let (bw, bh) = if horz { (16, 4) } else { (4, 16) }; |
| 13792 | let (_mi_w, _mi_h) = (bw / MI, bh / MI); |
| 13793 | let _scan: &[u16] = if horz { &SCAN_16X4 } else { &SCAN_4X16 }; |
| 13794 | let _chroma_scan: &[u16] = if horz { &SCAN_8X4 } else { &SCAN_4X8 }; |
| 13795 | let mut prev: Option<((usize, usize), usize)> = None; |
| 13796 | let mut last_mode = DC_PRED; |
| 13797 | let mut last_uv_mode = DC_PRED; |
| 13798 | for i in 0..4usize { |
| 13799 | let lmi = if horz { |
| 13800 | (mi_row0 + i, mi_col0) |
| 13801 | } else { |
| 13802 | (mi_row0, mi_col0 + i) |
| 13803 | }; |
| 13804 | // lane-av1-444: at 4:4:4 every strip is its own chroma reference |
| 13805 | // (no mi-parity test at ss 0/0, and the strip's own chroma block is |
| 13806 | // already >= 4x4) -- there is no 4:2:0 pair to close. |
| 13807 | let has_chroma = i % 2 == 1 || ss_y(fctx) == 0; |
| 13808 | let (mode, uv, _tx) = decode_rect4_16_strip( |
| 13809 | dec, |
| 13810 | cdfs, |
| 13811 | neighbours, |
| 13812 | lmi, |
| 13813 | horz, |
| 13814 | has_chroma, |
| 13815 | prev, |
| 13816 | y, |
| 13817 | u, |
| 13818 | v, |
| 13819 | enable_filter_intra, |
| 13820 | allow_screen_content_tools, |
| 13821 | base_q_idx, |
| 13822 | tx_select, |
| 13823 | reduced_tx_set, fctx, |
| 13824 | )?; |
| 13825 | if let Some(m) = uv { |
| 13826 | last_uv_mode = m; |
| 13827 | } |
| 13828 | last_mode = mode; |
| 13829 | prev = Some((lmi, mode)); |
| 13830 | } |
| 13831 | // The coarse 16-px cell holds one entry for the whole 16x16: the last |
| 13832 | // strip is what a block below (right) sees through it, the mi-exact maps |
| 13833 | // above answering everything finer. |
| 13834 | neighbours.above_mode[c] = last_mode; |
| 13835 | neighbours.left_mode[r] = last_mode; |
| 13836 | neighbours.above_uv_mode[c] = last_uv_mode; |
| 13837 | neighbours.left_uv_mode[r] = last_uv_mode; |
| 13838 | neighbours.above_side[c] = bw; |
| 13839 | neighbours.left_side[r] = bh; |
| 13840 | Ok(()) |
| 13841 | } |
| 13842 | |
| 13843 | /// One 16x4 / 4x16 strip of a 16x16-level 1:4 partition, intra-coded -- |
| 13844 | /// [`decode_rect4_16`]'s per-strip body, lifted (lane-intra16x4) so the INTER |
| 13845 | /// block path can decode a SINGLE intra strip of a partition whose other |
| 13846 | /// strips are inter. `prev` is the previous strip's `(mi, mode)` (the |
| 13847 | /// key-frame caller's coarse-cell stand-in; `None` on the inter path, where |
| 13848 | /// every strip publishes its own mi-exact mode). Returns |
| 13849 | /// `(y_mode, uv_predict_mode when this strip closes the 4:2:0 chroma pair)`. |
| 13850 | /// Intrabc reconstruction for a rect strip that OWNS its chroma |
| 13851 | /// (`bw/2 x bh/2` at the strip origin). 16x4/4x16 is not this shape -- that |
| 13852 | /// pair goes through [`decode_rect4_16_intrabc`]. |
| 13853 | /// |
| 13854 | /// This is the same control flow as the unmerged lane's `decode_intrabc_rect` |
| 13855 | /// (predict once, then one residual per transform unit on the INTER sets, |
| 13856 | /// chroma inherits luma's `tx_type`), written against the helpers that exist |
| 13857 | /// on this tree so a 2:1 strip no longer hides the 16x4 pair witness. The |
| 13858 | /// merge with lane-av1-intrabc should keep one of the two bodies; the call |
| 13859 | /// shape is the same. |
| 13860 | #[allow(clippy::too_many_arguments)] |
| 13861 | fn decode_intrabc_owned_rect( |
| 13862 | dec: &mut SymbolDecoder, |
| 13863 | cdfs: &mut Cdfs, |
| 13864 | neighbours: &mut Neighbours, |
| 13865 | (mi_r, mi_c): (usize, usize), |
| 13866 | (bw, bh): (usize, usize), |
| 13867 | skip: bool, |
| 13868 | (dv_row, dv_col): (i32, i32), |
| 13869 | y: &mut PlaneBuf<'static>, |
| 13870 | u: &mut PlaneBuf<'static>, |
| 13871 | v: &mut PlaneBuf<'static>, |
| 13872 | base_q_idx: u8, |
| 13873 | tx_select: bool, |
| 13874 | reduced_tx_set: bool, |
| 13875 | fctx: &crate::decode::FrameCtx, |
| 13876 | ) -> Result<()> { |
| 13877 | let _ = base_q_idx; |
| 13878 | let (px, py) = (mi_c * MI, mi_r * MI); |
| 13879 | let (cw, ch) = (bw / 2, bh / 2); |
| 13880 | let side = bw.max(bh); |
| 13881 | let cside = cw.max(ch); |
| 13882 | let saved_reduced = fctx.reduced_tx_set_inter.with(std::cell::Cell::get); |
| 13883 | let saved_tx = fctx.tx_select_inter.with(std::cell::Cell::get); |
| 13884 | fctx.reduced_tx_set_inter.with(|c| c.set(reduced_tx_set)); |
| 13885 | // `read_block_tx_size_rect` keys on this cell. A key frame forces it |
| 13886 | // false; the real bit is the caller's `tx_select`. Restored before return. |
| 13887 | fctx.tx_select_inter.with(|c| c.set(tx_select)); |
| 13888 | let restore = ReducedTxRestore { fctx, saved_reduced, saved_tx }; |
| 13889 | |
| 13890 | flush_recon(fctx, y, u, v); |
| 13891 | let pred_y = predict_intrabc_window(y, px, py, bw, bh, dv_col, dv_row, true, side, fctx); |
| 13892 | let pred_u = predict_intrabc_window(u, px / 2, py / 2, cw, ch, dv_col, dv_row, false, cside, fctx); |
| 13893 | let pred_v = predict_intrabc_window(v, px / 2, py / 2, cw, ch, dv_col, dv_row, false, cside, fctx); |
| 13894 | |
| 13895 | let mi_cols = 2 * y.true_width.div_ceil(8); |
| 13896 | let mi_rows = 2 * y.true_height.div_ceil(8); |
| 13897 | let leaves = read_block_tx_size_rect( |
| 13898 | dec, cdfs, neighbours, (mi_r, mi_c), (bw, bh), (mi_cols, mi_rows), skip, fctx, |
| 13899 | )?; |
| 13900 | let mut luma_tx_type = TxType::DctDct; |
| 13901 | let luma_grid; |
| 13902 | let u_grid; |
| 13903 | let v_grid; |
| 13904 | if skip { |
| 13905 | // `reconstruct_mc_rect` indexes the residual at the square stride. |
| 13906 | // `bw * bh` underruns on a VERT strip (8x16: stride 16, 16 rows, index 136). |
| 13907 | push_mc_rect(0, px, py, side, bw, bh, Pred::Inline(&pred_y, side), &ZERO_RESIDUAL[..side * bh], fctx); |
| 13908 | push_mc_rect(1, px / 2, py / 2, cside, cw, ch, Pred::Inline(&pred_u, cside), &ZERO_RESIDUAL[..cside * ch], fctx); |
| 13909 | push_mc_rect(2, px / 2, py / 2, cside, cw, ch, Pred::Inline(&pred_v, cside), &ZERO_RESIDUAL[..cside * ch], fctx); |
| 13910 | if let Some(ls) = leaves.as_ref() { |
| 13911 | // libaom `av1_reset_entropy_context` (`if (mbmi->skip_txfm)`, |
| 13912 | // decodeframe.c:1262 -> blockd.c:58): a SKIPPED block zeroes its |
| 13913 | // whole entropy-band footprint. The non-skip arm below writes |
| 13914 | // those bands per transform unit and the tail's |
| 13915 | // `record_split_luma_rect_mi` writes chroma only, so without |
| 13916 | // this walk a skipped strip whose `leaves` resolved (every |
| 13917 | // lossless frame -- `read_block_tx_size_rect`'s lossless arm |
| 13918 | // returns the 4x4 grid before it looks at `skip`) leaked the |
| 13919 | // PREVIOUS occupant's luma bands into the next block's |
| 13920 | // `txb_skip`/coefficient contexts (lane-av1-loss64: the skipped |
| 13921 | // 8x16 intrabc strip at mi(48,66) of the 320x242 lossless sb128 |
| 13922 | // stream left a stale 7 in left[48] and mi(48,68)'s first |
| 13923 | // transform unit read `txb_skip_ctx` 3 where aomdec reads 1). |
| 13924 | hit!(SKIP_LOSSLESS_BAND_RESET_HITS); |
| 13925 | for &(row, col, tw, th) in ls { |
| 13926 | neighbours.record_mi_luma_rect( |
| 13927 | (mi_r + row, mi_c + col), |
| 13928 | tw, |
| 13929 | th, |
| 13930 | &ZERO_RESIDUAL[..tw * th], |
| 13931 | ); |
| 13932 | } |
| 13933 | } |
| 13934 | luma_grid = Grid::Zero(side * side); |
| 13935 | u_grid = Grid::Zero(cside * cside); |
| 13936 | v_grid = Grid::Zero(cside * cside); |
| 13937 | } else if let Some(ls) = leaves.as_ref() { |
| 13938 | for (idx, &(row, col, tw, th)) in ls.iter().enumerate() { |
| 13939 | let tu_mi = (mi_r + row, mi_c + col); |
| 13940 | let (tu_px, tu_py) = (px + col * MI, py + row * MI); |
| 13941 | let tu_pred = Pred::Inline(&pred_y, side).offset(row * MI * side + col * MI); |
| 13942 | let (g, tt) = if tw == th { |
| 13943 | read_inter_plane( |
| 13944 | dec, cdfs, inter_txbset_for(tw, reduced_tx_set), &default_scan(tw.min(32)), |
| 13945 | 0, neighbours.around_mi(tu_mi, tw)[0], 0, y, tu_px, tu_py, tw, base_q_idx, |
| 13946 | tu_pred, None, Some(neighbours.luma_skip_ctx(tu_mi, tw / MI)), fctx, |
| 13947 | )? |
| 13948 | } else { |
| 13949 | read_inter_plane_rect( |
| 13950 | dec, cdfs, rect_inter_luma_set(tw, th, fctx)?, (tw, th), side, 0, |
| 13951 | neighbours.around_mi_rect(tu_mi, tw, th)[0], 0, y, tu_px, tu_py, tu_pred, |
| 13952 | None, Some(neighbours.luma_skip_ctx_rect(tu_mi, tw / MI, th / MI)), fctx, |
| 13953 | )? |
| 13954 | }; |
| 13955 | if idx == 0 { |
| 13956 | luma_tx_type = tt; |
| 13957 | } |
| 13958 | neighbours.record_mi_luma_rect(tu_mi, tw, th, &g); |
| 13959 | if tw != th { |
| 13960 | neighbours.fill_lf_grid_leaf_luma((tu_mi.0, tu_mi.1), tw / MI, th / MI, tw as u8, th as u8); |
| 13961 | } |
| 13962 | } |
| 13963 | luma_grid = Grid::Zero(side * side); |
| 13964 | let around = neighbours.around_mi_rect((mi_r, mi_c), bw, bh); |
| 13965 | let (ug, _) = read_inter_plane_rect( |
| 13966 | dec, cdfs, rect_inter_chroma_set(cw, ch)?, (cw, ch), cside, 1, around[1], 0, |
| 13967 | u, px / 2, py / 2, Pred::Inline(&pred_u, cside), Some(luma_tx_type), None, fctx, |
| 13968 | )?; |
| 13969 | let (vg, _) = read_inter_plane_rect( |
| 13970 | dec, cdfs, rect_inter_chroma_set(cw, ch)?, (cw, ch), cside, 2, around[2], 0, |
| 13971 | v, px / 2, py / 2, Pred::Inline(&pred_v, cside), Some(luma_tx_type), None, fctx, |
| 13972 | )?; |
| 13973 | u_grid = ug; |
| 13974 | v_grid = vg; |
| 13975 | } else { |
| 13976 | let around = neighbours.around_mi_rect((mi_r, mi_c), bw, bh); |
| 13977 | let (g, tt) = read_inter_plane_rect( |
| 13978 | dec, cdfs, rect_inter_luma_set(bw, bh, fctx)?, (bw, bh), side, 0, around[0], 0, |
| 13979 | y, px, py, Pred::Inline(&pred_y, side), None, None, fctx, |
| 13980 | )?; |
| 13981 | luma_tx_type = tt; |
| 13982 | luma_grid = g; |
| 13983 | let (ug, _) = read_inter_plane_rect( |
| 13984 | dec, cdfs, rect_inter_chroma_set(cw, ch)?, (cw, ch), cside, 1, around[1], 0, |
| 13985 | u, px / 2, py / 2, Pred::Inline(&pred_u, cside), Some(luma_tx_type), None, fctx, |
| 13986 | )?; |
| 13987 | let (vg, _) = read_inter_plane_rect( |
| 13988 | dec, cdfs, rect_inter_chroma_set(cw, ch)?, (cw, ch), cside, 2, around[2], 0, |
| 13989 | v, px / 2, py / 2, Pred::Inline(&pred_v, cside), Some(luma_tx_type), None, fctx, |
| 13990 | )?; |
| 13991 | u_grid = ug; |
| 13992 | v_grid = vg; |
| 13993 | } |
| 13994 | drop(restore); |
| 13995 | if leaves.is_some() { |
| 13996 | neighbours.record_split_luma_rect_mi((mi_r, mi_c), bw, bh, DC_PRED, DC_PRED, [&u_grid, &v_grid]); |
| 13997 | } else { |
| 13998 | neighbours.record_rect_mi((mi_r, mi_c), bw, bh, DC_PRED, DC_PRED, &[&luma_grid, &u_grid, &v_grid]); |
| 13999 | } |
| 14000 | neighbours.record_palette_y_rect((mi_r, mi_c), bw, bh, 0, [0u16; 8]); |
| 14001 | neighbours.record_palette_uv_rect((mi_r, mi_c), bw, bh, 0, [0u16; 8]); |
| 14002 | neighbours.fill_skip_grid_rect((mi_r, mi_c), bw / MI, bh / MI, skip); |
| 14003 | neighbours.fill_lf_grid_rect((mi_r, mi_c), bw / MI, bh / MI, bw.min(64) as u8, bh.min(64) as u8, 0, fctx); |
| 14004 | record_intrabc_mi_rect(mi_r, mi_c, bw / MI, bh / MI, Some((dv_row, dv_col)), neighbours, fctx); |
| 14005 | let _ = luma_tx_type; |
| 14006 | Ok(()) |
| 14007 | } |
| 14008 | |
| 14009 | fn predict_intrabc_window( |
| 14010 | plane: &PlaneBuf<'_>, |
| 14011 | px: usize, |
| 14012 | py: usize, |
| 14013 | w: usize, |
| 14014 | h: usize, |
| 14015 | dv_col: i32, |
| 14016 | dv_row: i32, |
| 14017 | luma: bool, |
| 14018 | stride: usize, |
| 14019 | fctx: &crate::decode::FrameCtx, |
| 14020 | ) -> Vec<u16> { |
| 14021 | let mut dense = vec![0u16; w * h]; |
| 14022 | mc::predict_with_filter( |
| 14023 | &plane.data, plane.width, plane.true_width, plane.true_height, |
| 14024 | mv_to_q4(px, dv_col, if luma { 0 } else { ss_x(fctx) }), mv_to_q4(py, dv_row, if luma { 0 } else { ss_y(fctx) }), |
| 14025 | w, h, mc::InterpFilterKind::Bilinear, &mut dense, fctx, |
| 14026 | ); |
| 14027 | let mut laid = vec![0u16; stride * stride]; |
| 14028 | for row in 0..h { |
| 14029 | laid[row * stride..][..w].copy_from_slice(&dense[row * w..][..w]); |
| 14030 | } |
| 14031 | laid |
| 14032 | } |
| 14033 | |
| 14034 | struct ReducedTxRestore<'a> { |
| 14035 | fctx: &'a crate::decode::FrameCtx, |
| 14036 | saved_reduced: bool, |
| 14037 | saved_tx: bool, |
| 14038 | } |
| 14039 | impl Drop for ReducedTxRestore<'_> { |
| 14040 | fn drop(&mut self) { |
| 14041 | self.fctx.reduced_tx_set_inter.with(|c| c.set(self.saved_reduced)); |
| 14042 | self.fctx.tx_select_inter.with(|c| c.set(self.saved_tx)); |
| 14043 | } |
| 14044 | } |
| 14045 | |
| 14046 | /// Reconstruction of one intrabc 16x4 / 4x16 strip (lane-av1-rect14). |
| 14047 | /// |
| 14048 | /// A 1:4 strip is a PAIR. Both children share one chroma block, and only the |
| 14049 | /// odd-mi member codes it (`is_chroma_reference`, `av1_common_int.h:1454`: |
| 14050 | /// the bottom strip of a `HORZ_4`, the right strip of a `VERT_4`). The chroma |
| 14051 | /// plane block is `ss_size_lookup[BLOCK_16X4][1][1] == BLOCK_8X4` (and |
| 14052 | /// `BLOCK_4X8` for `VERT_4`) at the PAIR origin -- `setup_pred_plane`'s |
| 14053 | /// `mi_row -= 1` when the block is one mi tall on an odd row. Luma is this |
| 14054 | /// strip's own footprint, copied from this strip's DV. The block is |
| 14055 | /// `is_inter_block`, so the residual uses the INTER sets and chroma inherits |
| 14056 | /// the luma unit's coded `tx_type`. |
| 14057 | /// |
| 14058 | /// Not [`decode_intrabc_rect`]: that helper (lane-av1-intrabc, not on this |
| 14059 | /// tree) predicts chroma at `bw/2 x bh/2` of the strip itself. At 16x4 that |
| 14060 | /// is an 8x2 half-pair at the odd strip -- wrong size, wrong origin, and it |
| 14061 | /// would code chroma on every strip. |
| 14062 | #[allow(clippy::too_many_arguments)] |
| 14063 | fn decode_rect4_16_intrabc( |
| 14064 | dec: &mut SymbolDecoder, |
| 14065 | cdfs: &mut Cdfs, |
| 14066 | neighbours: &mut Neighbours, |
| 14067 | lmi: (usize, usize), |
| 14068 | horz: bool, |
| 14069 | has_chroma: bool, |
| 14070 | skip: bool, |
| 14071 | (dv_row, dv_col): (i32, i32), |
| 14072 | y: &mut PlaneBuf<'static>, |
| 14073 | u: &mut PlaneBuf<'static>, |
| 14074 | v: &mut PlaneBuf<'static>, |
| 14075 | base_q_idx: u8, |
| 14076 | tx_select: bool, |
| 14077 | reduced_tx_set: bool, |
| 14078 | fctx: &crate::decode::FrameCtx, |
| 14079 | ) -> Result<(usize, Option<usize>, (usize, usize))> { |
| 14080 | let _ = base_q_idx; |
| 14081 | hit!(RECT4_16_INTRABC_HITS); |
| 14082 | let (bw, bh) = if horz { (16usize, 4usize) } else { (4, 16) }; |
| 14083 | let (mi_w, mi_h) = (bw / MI, bh / MI); |
| 14084 | let (px, py) = (lmi.1 * MI, lmi.0 * MI); |
| 14085 | let side = bw.max(bh); |
| 14086 | // `rect_inter_luma_set` reads the frame cell. A key frame's tile decoder |
| 14087 | // publishes `reduced_tx_set` into it; set it here too so a caller that |
| 14088 | // passed the bit explicitly cannot disagree with the set we read. |
| 14089 | let saved_reduced = fctx.reduced_tx_set_inter.with(std::cell::Cell::get); |
| 14090 | fctx.reduced_tx_set_inter.with(|c| c.set(reduced_tx_set)); |
| 14091 | let restored = scopeguard_reduced(fctx, saved_reduced); |
| 14092 | |
| 14093 | flush_recon(fctx, y, u, v); |
| 14094 | let mut pred_y_dense = vec![0u16; bw * bh]; |
| 14095 | mc::predict_with_filter( |
| 14096 | &y.data, y.width, y.true_width, y.true_height, |
| 14097 | mv_to_q4(px, dv_col, 0), mv_to_q4(py, dv_row, 0), |
| 14098 | bw, bh, mc::InterpFilterKind::Bilinear, &mut pred_y_dense, fctx, |
| 14099 | ); |
| 14100 | let mut pred_y = vec![0u16; side * side]; |
| 14101 | for row in 0..bh { |
| 14102 | pred_y[row * side..][..bw].copy_from_slice(&pred_y_dense[row * bw..][..bw]); |
| 14103 | } |
| 14104 | |
| 14105 | let (pw, ph) = if horz { (16usize, 8usize) } else { (8, 16) }; |
| 14106 | let (cw, ch) = (pw / 2, ph / 2); |
| 14107 | let pair_mi = if has_chroma { |
| 14108 | if horz { (lmi.0 - 1, lmi.1) } else { (lmi.0, lmi.1 - 1) } |
| 14109 | } else { |
| 14110 | lmi |
| 14111 | }; |
| 14112 | let (cpx, cpy) = if has_chroma { |
| 14113 | (pair_mi.1 * MI / 2, pair_mi.0 * MI / 2) |
| 14114 | } else { |
| 14115 | (0, 0) |
| 14116 | }; |
| 14117 | let cside = cw.max(ch); |
| 14118 | let (pred_u, pred_v) = if has_chroma { |
| 14119 | let mut ud = vec![0u16; cw * ch]; |
| 14120 | mc::predict_with_filter( |
| 14121 | &u.data, u.width, u.true_width, u.true_height, |
| 14122 | mv_to_q4(cpx, dv_col, ss_x(fctx)), mv_to_q4(cpy, dv_row, ss_y(fctx)), |
| 14123 | cw, ch, mc::InterpFilterKind::Bilinear, &mut ud, fctx, |
| 14124 | ); |
| 14125 | let mut us = vec![0u16; cside * cside]; |
| 14126 | for row in 0..ch { |
| 14127 | us[row * cside..][..cw].copy_from_slice(&ud[row * cw..][..cw]); |
| 14128 | } |
| 14129 | let mut vd = vec![0u16; cw * ch]; |
| 14130 | mc::predict_with_filter( |
| 14131 | &v.data, v.width, v.true_width, v.true_height, |
| 14132 | mv_to_q4(cpx, dv_col, ss_x(fctx)), mv_to_q4(cpy, dv_row, ss_y(fctx)), |
| 14133 | cw, ch, mc::InterpFilterKind::Bilinear, &mut vd, fctx, |
| 14134 | ); |
| 14135 | let mut vs = vec![0u16; cside * cside]; |
| 14136 | for row in 0..ch { |
| 14137 | vs[row * cside..][..cw].copy_from_slice(&vd[row * cw..][..cw]); |
| 14138 | } |
| 14139 | (us, vs) |
| 14140 | } else { |
| 14141 | (Vec::new(), Vec::new()) |
| 14142 | }; |
| 14143 | |
| 14144 | // Key-frame `tx_select` is the parameter. `tx_select_inter` is forced |
| 14145 | // false for a key frame, so `read_block_tx_size_rect` would skip the |
| 14146 | // symbol; read it here against the parameter the intra path already uses. |
| 14147 | // A skipped intrabc block predicts the whole footprint once and codes |
| 14148 | // no residual (`decode_token_recon_block`'s inter branch). Lossless |
| 14149 | // still forces TX_4X4 when the block is coded. |
| 14150 | let leaves = if skip || (!lossless(fctx) && !tx_select) { |
| 14151 | None |
| 14152 | } else if lossless(fctx) { |
| 14153 | let mut leaves = Vec::new(); |
| 14154 | for row in 0..bh / MI { |
| 14155 | for col in 0..bw / MI { |
| 14156 | leaves.push((row, col, 4usize, 4usize)); |
| 14157 | } |
| 14158 | } |
| 14159 | Some(leaves) |
| 14160 | } else { |
| 14161 | let ctx = txfm_partition_ctx_rect( |
| 14162 | neighbours.above_txfm[lmi.1], |
| 14163 | neighbours.left_txfm[lmi.0], |
| 14164 | side, |
| 14165 | bw, |
| 14166 | bh, |
| 14167 | ); |
| 14168 | let split = dec.symbol(&mut cdfs.txfm_partition[ctx]) == 1; |
| 14169 | if crate::envflags::env_flag!("EC_TRACE_MODE_STEP") { |
| 14170 | let (rng, _) = dec.debug_state(); |
| 14171 | eprintln!( |
| 14172 | "EC_ISTEP mi_row={} mi_col={} name=txfm_split_rect val={} ctx={ctx} rng={rng}", |
| 14173 | lmi.0, lmi.1, u8::from(split) |
| 14174 | ); |
| 14175 | } |
| 14176 | if !split { |
| 14177 | None |
| 14178 | } else { |
| 14179 | let (sub_w, sub_h) = sub_tx_size_map(bw, bh); |
| 14180 | let mi_cols = 2 * y.true_width.div_ceil(8); |
| 14181 | let mi_rows = 2 * y.true_height.div_ceil(8); |
| 14182 | let max_w_mi = (bw / MI).min(mi_cols.saturating_sub(lmi.1)); |
| 14183 | let max_h_mi = (bh / MI).min(mi_rows.saturating_sub(lmi.0)); |
| 14184 | let mut out = Vec::new(); |
| 14185 | for row in (0..bh / MI).step_by(sub_h / MI) { |
| 14186 | for col in (0..bw / MI).step_by(sub_w / MI) { |
| 14187 | read_var_tx_size( |
| 14188 | dec, cdfs, neighbours, lmi, side, (max_w_mi, max_h_mi), |
| 14189 | (sub_w, sub_h), 1, row, col, &mut out, |
| 14190 | ); |
| 14191 | } |
| 14192 | } |
| 14193 | Some(out) |
| 14194 | } |
| 14195 | }; |
| 14196 | let (tx_w, tx_h) = leaves.as_ref().and_then(|ls| ls.first()).map_or((bw, bh), |l| (l.2, l.3)); |
| 14197 | |
| 14198 | let mut luma_tx_type = TxType::DctDct; |
| 14199 | let luma_grid; |
| 14200 | let mut u_grid = Grid::Zero(cw * ch); |
| 14201 | let mut v_grid = Grid::Zero(cw * ch); |
| 14202 | let chroma_stamped_per_unit; |
| 14203 | |
| 14204 | if skip { |
| 14205 | push_mc_rect( |
| 14206 | 0, px, py, side, bw, bh, Pred::Inline(&pred_y, side), &ZERO_RESIDUAL[..side * bh], fctx, |
| 14207 | ); |
| 14208 | luma_grid = Grid::Zero(bw * bh); |
| 14209 | if has_chroma { |
| 14210 | push_mc_rect( |
| 14211 | 1, cpx, cpy, cside, cw, ch, Pred::Inline(&pred_u, cside), |
| 14212 | &ZERO_RESIDUAL[..cside * ch], fctx, |
| 14213 | ); |
| 14214 | push_mc_rect( |
| 14215 | 2, cpx, cpy, cside, cw, ch, Pred::Inline(&pred_v, cside), |
| 14216 | &ZERO_RESIDUAL[..cside * ch], fctx, |
| 14217 | ); |
| 14218 | } |
| 14219 | chroma_stamped_per_unit = false; |
| 14220 | } else if let Some(ls) = leaves.as_ref() { |
| 14221 | if lossless(fctx) { |
| 14222 | for &(row, col, _tw, _th) in ls { |
| 14223 | let tu_mi = (lmi.0 + row, lmi.1 + col); |
| 14224 | let (tu_px, tu_py) = (px + col * MI, py + row * MI); |
| 14225 | let tu_pred = Pred::Inline(&pred_y, side).offset(row * MI * side + col * MI); |
| 14226 | let (g, _) = read_inter_plane( |
| 14227 | dec, cdfs, |
| 14228 | if reduced_tx_set { TxbSet::Luma4Inter } else { TxbSet::Luma4InterSet1 }, |
| 14229 | &default_scan(4), 0, neighbours.around_mi(tu_mi, 4)[0], 0, |
| 14230 | y, tu_px, tu_py, 4, base_q_idx, tu_pred, None, |
| 14231 | Some(neighbours.luma_skip_ctx(tu_mi, 1)), fctx, |
| 14232 | )?; |
| 14233 | neighbours.record_mi_luma_rect(tu_mi, 4, 4, &g); |
| 14234 | } |
| 14235 | } else { |
| 14236 | for (idx, &(row, col, tw, th)) in ls.iter().enumerate() { |
| 14237 | let tu_mi = (lmi.0 + row, lmi.1 + col); |
| 14238 | let (tu_px, tu_py) = (px + col * MI, py + row * MI); |
| 14239 | let tu_pred = Pred::Inline(&pred_y, side).offset(row * MI * side + col * MI); |
| 14240 | let (g, tt) = if tw == th { |
| 14241 | read_inter_plane( |
| 14242 | dec, cdfs, inter_txbset_for(tw, reduced_tx_set), |
| 14243 | &default_scan(tw.min(32)), 0, neighbours.around_mi(tu_mi, tw)[0], 0, |
| 14244 | y, tu_px, tu_py, tw, base_q_idx, tu_pred, None, |
| 14245 | Some(neighbours.luma_skip_ctx(tu_mi, tw / MI)), fctx, |
| 14246 | )? |
| 14247 | } else { |
| 14248 | read_inter_plane_rect( |
| 14249 | dec, cdfs, rect_inter_luma_set(tw, th, fctx)?, (tw, th), side, 0, |
| 14250 | neighbours.around_mi_rect(tu_mi, tw, th)[0], 0, y, tu_px, tu_py, tu_pred, |
| 14251 | None, Some(neighbours.luma_skip_ctx_rect(tu_mi, tw / MI, th / MI)), fctx, |
| 14252 | )? |
| 14253 | }; |
| 14254 | if idx == 0 { |
| 14255 | luma_tx_type = tt; |
| 14256 | } |
| 14257 | neighbours.record_mi_luma_rect(tu_mi, tw, th, &g); |
| 14258 | } |
| 14259 | } |
| 14260 | luma_grid = Grid::Zero(bw * bh); |
| 14261 | chroma_stamped_per_unit = has_chroma && lossless(fctx); |
| 14262 | if has_chroma && lossless(fctx) { |
| 14263 | let (mut uo, mut vo) = (Vec::new(), Vec::new()); |
| 14264 | read_inter_chroma_lossless( |
| 14265 | dec, cdfs, neighbours, u, v, |
| 14266 | Pred::Inline(&pred_u, cside), Pred::Inline(&pred_v, cside), |
| 14267 | pair_mi, (cpx, cpy), (0, 0), (cw, ch), (cw, ch), cside, 0, base_q_idx, |
| 14268 | &mut uo, &mut vo, fctx, |
| 14269 | )?; |
| 14270 | u_grid = if uo.is_empty() { Grid::Zero(cw * ch) } else { Grid::Own(uo) }; |
| 14271 | v_grid = if vo.is_empty() { Grid::Zero(cw * ch) } else { Grid::Own(vo) }; |
| 14272 | } else if has_chroma { |
| 14273 | let around = neighbours.around_mi_rect(pair_mi, pw, ph); |
| 14274 | let (ug, _) = read_inter_plane_rect( |
| 14275 | dec, cdfs, rect_inter_chroma_set(cw, ch)?, (cw, ch), cside, 1, around[1], 0, |
| 14276 | u, cpx, cpy, Pred::Inline(&pred_u, cside), Some(luma_tx_type), None, fctx, |
| 14277 | )?; |
| 14278 | u_grid = ug; |
| 14279 | let (vg, _) = read_inter_plane_rect( |
| 14280 | dec, cdfs, rect_inter_chroma_set(cw, ch)?, (cw, ch), cside, 2, around[2], 0, |
| 14281 | v, cpx, cpy, Pred::Inline(&pred_v, cside), Some(luma_tx_type), None, fctx, |
| 14282 | )?; |
| 14283 | v_grid = vg; |
| 14284 | } |
| 14285 | } else { |
| 14286 | let around = neighbours.around_mi_rect(lmi, bw, bh); |
| 14287 | let (g, tt) = read_inter_plane_rect( |
| 14288 | dec, cdfs, rect_inter_luma_set(bw, bh, fctx)?, (bw, bh), side, 0, around[0], 0, |
| 14289 | y, px, py, Pred::Inline(&pred_y, side), None, None, fctx, |
| 14290 | )?; |
| 14291 | luma_tx_type = tt; |
| 14292 | luma_grid = g; |
| 14293 | chroma_stamped_per_unit = false; |
| 14294 | if has_chroma { |
| 14295 | let around = neighbours.around_mi_rect(pair_mi, pw, ph); |
| 14296 | let (ug, _) = read_inter_plane_rect( |
| 14297 | dec, cdfs, rect_inter_chroma_set(cw, ch)?, (cw, ch), cside, 1, around[1], 0, |
| 14298 | u, cpx, cpy, Pred::Inline(&pred_u, cside), Some(luma_tx_type), None, fctx, |
| 14299 | )?; |
| 14300 | u_grid = ug; |
| 14301 | let (vg, _) = read_inter_plane_rect( |
| 14302 | dec, cdfs, rect_inter_chroma_set(cw, ch)?, (cw, ch), cside, 2, around[2], 0, |
| 14303 | v, cpx, cpy, Pred::Inline(&pred_v, cside), Some(luma_tx_type), None, fctx, |
| 14304 | )?; |
| 14305 | v_grid = vg; |
| 14306 | } |
| 14307 | } |
| 14308 | drop(restored); |
| 14309 | |
| 14310 | if leaves.is_none() { |
| 14311 | neighbours.record_mi_luma_rect(lmi, bw, bh, &luma_grid); |
| 14312 | } |
| 14313 | neighbours.record_mode_mi(lmi.0, lmi.1, mi_w, mi_h, DC_PRED); |
| 14314 | neighbours.fill_skip_grid_rect(lmi, mi_w, mi_h, skip); |
| 14315 | neighbours.fill_lf_grid_rect(lmi, mi_w, mi_h, tx_w as u8, tx_h as u8, 0, fctx); |
| 14316 | for cell in 0..mi_w { |
| 14317 | if let Some(slot) = neighbours.above_side_mi.get_mut(lmi.1 + cell) { |
| 14318 | *slot = bw as u8; |
| 14319 | } |
| 14320 | } |
| 14321 | for cell in 0..mi_h { |
| 14322 | if let Some(slot) = neighbours.left_side_mi.get_mut(lmi.0 + cell) { |
| 14323 | *slot = bh as u8; |
| 14324 | } |
| 14325 | } |
| 14326 | let mut last_uv = None; |
| 14327 | if has_chroma { |
| 14328 | hit!(RECT4_16_CHROMA_HITS); |
| 14329 | neighbours.record_uv_mode_mi(pair_mi.0, pair_mi.1, pw / MI, ph / MI, DC_PRED); |
| 14330 | if !chroma_stamped_per_unit { |
| 14331 | let u_state = neighbour_state(&u_grid); |
| 14332 | let v_state = neighbour_state(&v_grid); |
| 14333 | let round_up_even = |n: usize| n.div_ceil(2) * 2; |
| 14334 | let bound_h = round_up_even(neighbours.mi_rows); |
| 14335 | let bound_w = round_up_even(neighbours.mi_cols); |
| 14336 | for cell in 0..(ph / MI) { |
| 14337 | if pair_mi.0 + cell < bound_h |
| 14338 | && let Some(slot) = neighbours.left.get_mut(pair_mi.0 + cell) |
| 14339 | { |
| 14340 | slot[1] = u_state; |
| 14341 | slot[2] = v_state; |
| 14342 | } |
| 14343 | } |
| 14344 | for cell in 0..(pw / MI) { |
| 14345 | if pair_mi.1 + cell < bound_w |
| 14346 | && let Some(slot) = neighbours.above.get_mut(pair_mi.1 + cell) |
| 14347 | { |
| 14348 | slot[1] = u_state; |
| 14349 | slot[2] = v_state; |
| 14350 | } |
| 14351 | } |
| 14352 | } |
| 14353 | last_uv = Some(DC_PRED); |
| 14354 | } |
| 14355 | if horz { |
| 14356 | hit!(RECT4_16_HORZ_HITS); |
| 14357 | } else { |
| 14358 | hit!(RECT4_16_VERT_HITS); |
| 14359 | } |
| 14360 | record_strip_palette(neighbours, lmi, bw, bh, 0, [0u16; 8], 0, [0u16; 8]); |
| 14361 | if leaves.is_none() { |
| 14362 | // Skip and TX_MODE_LARGEST publish the block size (`set_txfm_ctxs` |
| 14363 | // with `skip && is_inter`, or the unsplit unit). A split tree already |
| 14364 | // published per leaf inside `read_var_tx_size`. |
| 14365 | txfm_partition_update_rect(neighbours, lmi, (tx_w, tx_h), (bw, bh)); |
| 14366 | } |
| 14367 | record_intrabc_mi_rect( |
| 14368 | lmi.0, lmi.1, mi_w, mi_h, Some((dv_row, dv_col)), neighbours, fctx, |
| 14369 | ); |
| 14370 | Ok((DC_PRED, last_uv, (tx_w, tx_h))) |
| 14371 | } |
| 14372 | |
| 14373 | /// Restores `reduced_tx_set_inter` when the intrabc pair reader returns, |
| 14374 | /// including through `?`. |
| 14375 | struct ReducedRestore<'a> { |
| 14376 | fctx: &'a crate::decode::FrameCtx, |
| 14377 | saved: bool, |
| 14378 | } |
| 14379 | impl Drop for ReducedRestore<'_> { |
| 14380 | fn drop(&mut self) { |
| 14381 | self.fctx.reduced_tx_set_inter.with(|c| c.set(self.saved)); |
| 14382 | } |
| 14383 | } |
| 14384 | fn scopeguard_reduced(fctx: &crate::decode::FrameCtx, saved: bool) -> ReducedRestore<'_> { |
| 14385 | ReducedRestore { fctx, saved } |
| 14386 | } |
| 14387 | |
| 14388 | #[allow(clippy::too_many_arguments)] |
| 14389 | fn decode_rect4_16_strip( |
| 14390 | dec: &mut SymbolDecoder, |
| 14391 | cdfs: &mut Cdfs, |
| 14392 | neighbours: &mut Neighbours, |
| 14393 | lmi: (usize, usize), |
| 14394 | horz: bool, |
| 14395 | has_chroma: bool, |
| 14396 | prev: Option<((usize, usize), usize)>, |
| 14397 | y: &mut PlaneBuf<'static>, |
| 14398 | u: &mut PlaneBuf<'static>, |
| 14399 | v: &mut PlaneBuf<'static>, |
| 14400 | enable_filter_intra: bool, |
| 14401 | allow_screen_content_tools: bool, |
| 14402 | base_q_idx: u8, |
| 14403 | tx_select: bool, |
| 14404 | reduced_tx_set: bool, fctx: &crate::decode::FrameCtx, |
| 14405 | ) -> Result<(usize, Option<usize>, (usize, usize))> { |
| 14406 | let (r, c) = (lmi.0 / (SUB / MI), lmi.1 / (SUB / MI)); |
| 14407 | let (bw, bh) = if horz { (16usize, 4usize) } else { (4, 16) }; |
| 14408 | let (mi_w, mi_h) = (bw / MI, bh / MI); |
| 14409 | let scan: &[u16] = if horz { &SCAN_16X4 } else { &SCAN_4X16 }; |
| 14410 | let mut last_uv_mode: Option<usize> = None; |
| 14411 | // `decode_block_rect4`'s own neighbour rule: the coarse 16-px cell |
| 14412 | // aliases all four strips onto one entry, so the mi-exact map wins and |
| 14413 | // the previous strip stands in on the split axis. |
| 14414 | let mut above_mode = neighbours.above_mode[c]; |
| 14415 | let mut left_mode = neighbours.left_mode[r]; |
| 14416 | if let Some(((pr, pc), pmode)) = prev { |
| 14417 | if pc == lmi.1 && lmi.0 == pr + mi_h { |
| 14418 | above_mode = pmode; |
| 14419 | } else if pr == lmi.0 && lmi.1 == pc + mi_w { |
| 14420 | left_mode = pmode; |
| 14421 | } |
| 14422 | } |
| 14423 | let (above_mode, left_mode) = |
| 14424 | neighbours.modes_above_left_mi(lmi.0, lmi.1, (above_mode, left_mode)); |
| 14425 | // lane-kf900 r4: the strip's real palette ctx + colour cache (see |
| 14426 | // [`decode_block_rect4`]); `None`/`&[]` here read `palette_y_mode` |
| 14427 | // off CDF row 0 and every cache hit off an empty cache. |
| 14428 | let (palette_ctx, palette_cache) = neighbours.palette_ctx_and_cache_mi(lmi); |
| 14429 | let palette_uv_cache = neighbours.palette_uv_cache_mi(lmi); |
| 14430 | let ( |
| 14431 | skip, |
| 14432 | mode, |
| 14433 | angle_delta_y, |
| 14434 | uv_mode, |
| 14435 | angle_delta_uv, |
| 14436 | alpha, |
| 14437 | filter_intra, |
| 14438 | palette_y, |
| 14439 | palette_uv, |
| 14440 | intrabc, |
| 14441 | ) = read_intra_mode_rect( |
| 14442 | dec, |
| 14443 | cdfs, |
| 14444 | above_mode, |
| 14445 | left_mode, |
| 14446 | true, |
| 14447 | has_chroma, |
| 14448 | bw, |
| 14449 | bh, |
| 14450 | enable_filter_intra, |
| 14451 | neighbours.skip_txfm_ctx(lmi.0, lmi.1), |
| 14452 | allow_screen_content_tools, |
| 14453 | Some((palette_ctx, &palette_cache)), |
| 14454 | &palette_uv_cache, |
| 14455 | lmi.0, |
| 14456 | lmi.1, fctx, |
| 14457 | )?; |
| 14458 | if let Some(dv) = intrabc { |
| 14459 | return decode_rect4_16_intrabc( |
| 14460 | dec, cdfs, neighbours, lmi, horz, has_chroma, skip, dv, y, u, v, |
| 14461 | base_q_idx, tx_select, reduced_tx_set, fctx, |
| 14462 | ); |
| 14463 | } |
| 14464 | if palette_y.is_some() || palette_uv.is_some() { |
| 14465 | hit!(RECT4_PALETTE_HITS); |
| 14466 | } |
| 14467 | // The colour-index map through the base colours IS the prediction. |
| 14468 | // Chroma's map is the PAIR's 8x4 (4x8) block, matching the one |
| 14469 | // chroma transform this closing strip codes. |
| 14470 | let palette_y_buf: Option<Vec<u16>> = palette_y |
| 14471 | .as_ref() |
| 14472 | .map(|p| p.map.iter().map(|&i| p.colors[i as usize]).collect()); |
| 14473 | let palette_uv_bufs: Option<(Vec<u16>, Vec<u16>)> = palette_uv.as_ref().map(|p| { |
| 14474 | ( |
| 14475 | p.map.iter().map(|&i| p.u_colors[i as usize]).collect(), |
| 14476 | p.map.iter().map(|&i| p.v_colors[i as usize]).collect(), |
| 14477 | ) |
| 14478 | }); |
| 14479 | let smooth_neighbor = is_smooth_mode(above_mode) || is_smooth_mode(left_mode); |
| 14480 | if smooth_neighbor { |
| 14481 | hit!(SMOOTH_LUMA_HITS); |
| 14482 | } |
| 14483 | if filter_intra.is_some() { |
| 14484 | hit!(FILTER_INTRA_RECT_HITS); |
| 14485 | } |
| 14486 | // `bsize_to_tx_size_cat(BLOCK_16X4)`: `max_txsize_rect_lookup` is |
| 14487 | // TX_16X4 and `sub_tx_size_map` reaches TX_4X4 in two steps, so the |
| 14488 | // depth symbol is the 3-symbol `tx_size_cat1` -- not the 2-symbol |
| 14489 | // `cat0` a 4x8 leaf reads nor the `cat2` a 32x8 strip does. |
| 14490 | // lane-av1mixloss: the lossless-segment early return |
| 14491 | // (`decodeframe.c:1203`) precedes `TX_MODE_SELECT`, so a |
| 14492 | // lossless-segment strip codes no depth symbol here either (class |
| 14493 | // sweep of the sub8 leaf's fix). |
| 14494 | let depth = if tx_select && !lossless(fctx) { |
| 14495 | // lane-av1txbands: every frame reads the real `TXFM_CONTEXT` bands |
| 14496 | // (`get_tx_size_context`, libaom `pred_common.h:342`). |
| 14497 | let ctx = tx_size_context_txfm_rect(neighbours, lmi, bw, bh, fctx); |
| 14498 | let d = dec.symbol(&mut cdfs.tx_size_cat1[ctx]); |
| 14499 | if crate::envflags::env_flag!("EC_TRACE_MODE_STEP") { |
| 14500 | eprintln!( |
| 14501 | "EC_ISTEP mi_row={} mi_col={} name=tx_depth val={d} ctx={ctx} cat=1 rng={}", |
| 14502 | lmi.0, |
| 14503 | lmi.1, |
| 14504 | dec.debug_state().0 |
| 14505 | ); |
| 14506 | } |
| 14507 | d |
| 14508 | } else { |
| 14509 | 0 |
| 14510 | }; |
| 14511 | if depth != 0 { |
| 14512 | hit!(TX_DEPTH_HITS); |
| 14513 | hit!(RECT4_16_SPLIT_HITS); |
| 14514 | } |
| 14515 | let (tx_w, tx_h) = depth_to_tx_wh(bw, bh, depth, fctx); |
| 14516 | let (px, py) = (lmi.1 * MI, lmi.0 * MI); |
| 14517 | let reach = Reach::of_rect(bw, bh, px, py, y.width, y.height, fctx); |
| 14518 | neighbours.record_mode_mi(lmi.0, lmi.1, mi_w, mi_h, mode); |
| 14519 | if depth != 0 || lossless(fctx) { |
| 14520 | // [`decode_rect_split`]'s per-unit walk, inlined because the |
| 14521 | // CHROMA of this shape belongs to the strip's 4:2:0 pair, not to |
| 14522 | // the strip (that function codes one chroma transform per block). |
| 14523 | // depth 1 is the 2:1 rect unit TX_8X4/TX_4X8 |
| 14524 | // (`sub_tx_size_map[TX_16X4]`), depth 2 the square TX_4X4; each |
| 14525 | // unit predicts off the units before it in raster order. |
| 14526 | let rect_unit = tx_w != tx_h; |
| 14527 | let zero_tu = vec![0i32; tx_w * tx_h]; |
| 14528 | for tu_row in 0..bh / tx_h { |
| 14529 | for tu_col in 0..bw / tx_w { |
| 14530 | let tu_mi = (lmi.0 + tu_row * (tx_h / MI), lmi.1 + tu_col * (tx_w / MI)); |
| 14531 | let (tu_px, tu_py) = (px + tu_col * tx_w, py + tu_row * tx_h); |
| 14532 | let tu_reach = Reach::of_tu( |
| 14533 | bw, |
| 14534 | bh, |
| 14535 | tu_col * tx_w, |
| 14536 | tu_row * tx_h, |
| 14537 | tx_w, |
| 14538 | tx_h, |
| 14539 | reach, |
| 14540 | ); |
| 14541 | if let Some(b) = &palette_y_buf { |
| 14542 | let tu_buf: Vec<u16> = (0..tx_h) |
| 14543 | .flat_map(|i| { |
| 14544 | let base = (tu_row * tx_h + i) * bw + tu_col * tx_w; |
| 14545 | b[base..base + tx_w].iter().copied() |
| 14546 | }) |
| 14547 | .collect(); |
| 14548 | set_palette_pred(tu_buf, fctx); |
| 14549 | } |
| 14550 | if skip { |
| 14551 | push_intra_rect(0, |
| 14552 | tu_px, tu_py, tx_w, tx_h, mode, angle_delta_y, tu_reach, &zero_tu, |
| 14553 | None, filter_intra, smooth_neighbor, fctx, |
| 14554 | ); |
| 14555 | neighbours.record_mi_luma_rect(tu_mi, tx_w, tx_h, &zero_tu); |
| 14556 | } else if rect_unit { |
| 14557 | // `get_txsize_entropy_ctx(TX_8X4)` is the 8x8 class with |
| 14558 | // 32 coded positions and the `TX_4X4` `tx_type` row: |
| 14559 | // exactly [`TxbSet::LumaRect4x8`]. |
| 14560 | let tu_around = neighbours.around_mi_rect(tu_mi, tx_w, tx_h)[0]; |
| 14561 | let tu_skip_ctx = |
| 14562 | neighbours.luma_skip_ctx_rect(tu_mi, tx_w / MI, tx_h / MI); |
| 14563 | let set = if reduced_tx_set { |
| 14564 | TxbSet::LumaRect4x8 |
| 14565 | } else { |
| 14566 | TxbSet::LumaRect4x8Set1 |
| 14567 | }; |
| 14568 | let tu_scan: &[u16] = if tx_w > tx_h { &SCAN_8X4 } else { &SCAN_4X8 }; |
| 14569 | let mut coding = cdfs.txb(set, fi_tx_row(mode, filter_intra)); |
| 14570 | let (tu_grid, tu_tx_type) = read_coeffs_rect( |
| 14571 | dec, |
| 14572 | &mut coding, |
| 14573 | tu_scan, |
| 14574 | tx_w, |
| 14575 | tx_h, |
| 14576 | tu_skip_ctx, |
| 14577 | dc_sign_ctx(tu_around.2), |
| 14578 | TxType::DctDct, |
| 14579 | )?; |
| 14580 | let residual = dequant_and_inverse_typed_wh( |
| 14581 | &tu_grid, |
| 14582 | tx_w, |
| 14583 | tx_h, |
| 14584 | crate::decode::bit_depth(fctx), |
| 14585 | block_q_idx(fctx), |
| 14586 | plane_q_delta(0, fctx).0, |
| 14587 | plane_q_delta(0, fctx).1, |
| 14588 | tu_tx_type, |
| 14589 | block_iqmatrix(fctx, 0, tx_w, tx_h, tu_tx_type), |
| 14590 | ); |
| 14591 | push_intra_rect(0, |
| 14592 | tu_px, tu_py, tx_w, tx_h, mode, angle_delta_y, tu_reach, &residual, |
| 14593 | None, filter_intra, smooth_neighbor, fctx, |
| 14594 | ); |
| 14595 | if tu_grid.iter().any(|&l| l != 0) { |
| 14596 | hit!(RECT4_16_COEFF_HITS); |
| 14597 | } |
| 14598 | neighbours.record_mi_luma_rect(tu_mi, tx_w, tx_h, &tu_grid); |
| 14599 | } else { |
| 14600 | let tu_around = neighbours.around_mi(tu_mi, tx_w)[0]; |
| 14601 | let tu_skip_ctx = neighbours.luma_skip_ctx(tu_mi, tx_w / MI); |
| 14602 | let tu_grid = read_plane( |
| 14603 | dec, |
| 14604 | cdfs, |
| 14605 | txbset_for(tx_w, reduced_tx_set), |
| 14606 | default_scan(tx_w), |
| 14607 | 0, |
| 14608 | tu_around, |
| 14609 | mode, |
| 14610 | mode, |
| 14611 | angle_delta_y, |
| 14612 | tu_reach, |
| 14613 | y, |
| 14614 | tu_px, |
| 14615 | tu_py, |
| 14616 | tx_w, |
| 14617 | tx_w, |
| 14618 | base_q_idx, |
| 14619 | None, |
| 14620 | filter_intra, |
| 14621 | Some(tu_skip_ctx), |
| 14622 | smooth_neighbor, fctx, |
| 14623 | )?; |
| 14624 | if tu_grid.iter().any(|&l| l != 0) { |
| 14625 | hit!(RECT4_16_COEFF_HITS); |
| 14626 | } |
| 14627 | neighbours.record_mi_luma(tu_mi, tx_w, &tu_grid); |
| 14628 | } |
| 14629 | } |
| 14630 | } |
| 14631 | } else { |
| 14632 | let levels = if skip { |
| 14633 | let zeros = Grid::Zero(bw * bh); |
| 14634 | if let Some(b) = &palette_y_buf { |
| 14635 | set_palette_pred(b.clone(), fctx); |
| 14636 | } |
| 14637 | push_intra_rect(0, |
| 14638 | px, py, bw, bh, mode, angle_delta_y, reach, &zeros, None, filter_intra, |
| 14639 | smooth_neighbor, fctx, |
| 14640 | ); |
| 14641 | zeros |
| 14642 | } else { |
| 14643 | // The strip's luma TU *is* the strip (`plane_bsize == |
| 14644 | // txsize_to_bsize[tx_size]`), so `get_txb_ctx` fixes |
| 14645 | // `txb_skip_ctx` at 0. |
| 14646 | let around = neighbours.around_mi_rect(lmi, bw, bh)[0]; |
| 14647 | let set = if reduced_tx_set { |
| 14648 | TxbSet::LumaRect16x4 |
| 14649 | } else { |
| 14650 | TxbSet::LumaRect16x4Set1 |
| 14651 | }; |
| 14652 | let mut coding = cdfs.txb(set, fi_tx_row(mode, filter_intra)); |
| 14653 | let (levels, tx_type) = read_coeffs_rect( |
| 14654 | dec, |
| 14655 | &mut coding, |
| 14656 | scan, |
| 14657 | bw, |
| 14658 | bh, |
| 14659 | 0, |
| 14660 | dc_sign_ctx(around.2), |
| 14661 | TxType::DctDct, |
| 14662 | )?; |
| 14663 | let residual = dequant_and_inverse_typed_wh( |
| 14664 | &levels, |
| 14665 | bw, |
| 14666 | bh, |
| 14667 | crate::decode::bit_depth(fctx), |
| 14668 | block_q_idx(fctx), |
| 14669 | plane_q_delta(0, fctx).0, |
| 14670 | plane_q_delta(0, fctx).1, |
| 14671 | tx_type, |
| 14672 | block_iqmatrix(fctx, 0, bw, bh, tx_type), |
| 14673 | ); |
| 14674 | if let Some(b) = &palette_y_buf { |
| 14675 | set_palette_pred(b.clone(), fctx); |
| 14676 | } |
| 14677 | push_intra_rect(0, |
| 14678 | px, py, bw, bh, mode, angle_delta_y, reach, &residual, None, filter_intra, |
| 14679 | smooth_neighbor, fctx, |
| 14680 | ); |
| 14681 | hit!(RECT4_16_COEFF_HITS); |
| 14682 | levels |
| 14683 | }; |
| 14684 | neighbours.record_mi_luma_rect(lmi, bw, bh, &levels); |
| 14685 | } |
| 14686 | neighbours.fill_skip_grid_rect(lmi, mi_w, mi_h, skip); |
| 14687 | neighbours.fill_lf_grid_rect(lmi, mi_w, mi_h, tx_w as u8, tx_h as u8, 0, fctx); |
| 14688 | // `update_partition_context` writes `partition_context_lookup[ |
| 14689 | // BLOCK_16X4]` over the whole 16x16: full width above, a 4-px side on |
| 14690 | // the left (mirrored under VERT_4). |
| 14691 | for cell in 0..mi_w { |
| 14692 | if let Some(slot) = neighbours.above_side_mi.get_mut(lmi.1 + cell) { |
| 14693 | *slot = bw as u8; |
| 14694 | } |
| 14695 | } |
| 14696 | for cell in 0..mi_h { |
| 14697 | if let Some(slot) = neighbours.left_side_mi.get_mut(lmi.0 + cell) { |
| 14698 | *slot = bh as u8; |
| 14699 | } |
| 14700 | } |
| 14701 | if has_chroma { |
| 14702 | hit!(RECT4_16_CHROMA_HITS); |
| 14703 | // lane-av1-444: at 4:4:4 there is no pair -- every strip is its |
| 14704 | // own chroma reference and codes its own 16x4 / 4x16 chroma |
| 14705 | // block. (`set_mi_offsets`'s chroma snap is the zero offset at |
| 14706 | // ss 0/0, so the neighbour reads below anchor at the strip.) |
| 14707 | let (pair_mi, pw, ph) = if ss_x(fctx) == 0 && ss_y(fctx) == 0 { |
| 14708 | (lmi, bw, bh) |
| 14709 | } else if ss_y(fctx) == 0 { |
| 14710 | // lane-av1-422: chroma height is NOT subsampled, so every |
| 14711 | // strip is its own chroma reference (libaom is_chroma_reference |
| 14712 | // loses its row-parity clause) and codes its own |
| 14713 | // `ss_size_lookup[bsize][1][0]` chroma block -- the 8x4 of a |
| 14714 | // 16x4 luma strip. The 4:2:0 pair merge below never applies, |
| 14715 | // exactly like the ss 0/0 arm above. (A VERTICAL 4x16 strip |
| 14716 | // cannot reach this arm at 4:2:2: its chroma block |
| 14717 | // `ss_size_lookup[BLOCK_4X16][1][0]` is BLOCK_INVALID, so a |
| 14718 | // conformant stream carries no such leaf.) |
| 14719 | (lmi, bw, bh) |
| 14720 | } else if horz { |
| 14721 | ((lmi.0 - 1, lmi.1), 16, 8) |
| 14722 | } else { |
| 14723 | ((lmi.0, lmi.1 - 1), 8, 16) |
| 14724 | }; |
| 14725 | let (ppx, ppy) = (pair_mi.1 * MI, pair_mi.0 * MI); |
| 14726 | let (cpx, cpy) = (ppx >> ss_x(fctx), ppy >> ss_y(fctx)); |
| 14727 | let (cw, ch) = (pw >> ss_x(fctx), ph >> ss_y(fctx)); |
| 14728 | let uv_predict_mode = if uv_mode == UV_CFL_PRED { DC_PRED } else { uv_mode }; |
| 14729 | if (V_PRED..=D67_PRED).contains(&uv_predict_mode) { |
| 14730 | hit!(RECT4_16_CHROMA_DIR_HITS); |
| 14731 | } |
| 14732 | let pair_reach = Reach::of_rect(pw, ph, ppx, ppy, y.width, y.height, fctx); |
| 14733 | // lane-kf1200 r1: the chroma edge-filter type reads the neighbours |
| 14734 | // of the CHROMA block, which for a 16x4/4x16 pair is `pair_mi`'s |
| 14735 | // 16x8/8x16 region -- libaom builds `chroma_above_mbmi`/ |
| 14736 | // `chroma_left_mbmi` from `mi[-((mi_row & ss_y) * stride + (mi_col |
| 14737 | // & ss_x))]` (`set_mi_offsets`, av1/common/blockd.h ~1050), i.e. |
| 14738 | // the pair's top-left luma mi, never this leaf's odd row/column. |
| 14739 | // Reading `lmi` here named the leaf directly above (a non-chroma |
| 14740 | // -reference 4-row block, `uv_mode == UV_DC_PRED`) and so lost a |
| 14741 | // SMOOTH neighbour: filt_type 0 instead of 1 turned |
| 14742 | // `av1_use_intra_edge_upsample` on (blk_wh 12 <= 16) for a |
| 14743 | // directional chroma strip, moving 4 samples by 1. |
| 14744 | let smooth_neighbor_uv = |
| 14745 | neighbours.smooth_uv_neighbour(pair_mi.0, pair_mi.1, r, c, fctx); |
| 14746 | if smooth_neighbor_uv != neighbours.smooth_uv_neighbour_unsnapped(lmi.0, lmi.1, r, c) |
| 14747 | { |
| 14748 | hit!(RECT4_16_UV_PAIR_FILT_HITS); |
| 14749 | } |
| 14750 | // lane-av1-rect14: a lossless frame forces TX_4X4 on every plane |
| 14751 | // (`av1_get_tx_size`, blockd.h:1381). The pair's chroma block is |
| 14752 | // 8x4 (HORZ_4) or 4x8 (VERT_4) -- two TX_4X4s, plane-major |
| 14753 | // (`decode_token_recon_block`), not one `ChromaRect8x4`. Reading |
| 14754 | // the rect here consumed one symbol block too few and desynced |
| 14755 | // the next strip's `skip` (measured: lossless sb128 320x240, |
| 14756 | // mi(1,20) uv_mode matched, the following skip did not). |
| 14757 | let lossless_pair = lossless(fctx); |
| 14758 | let (u_levels, v_levels) = if lossless_pair { |
| 14759 | hit!(RECT4_16_LOSSLESS_CHROMA_HITS); |
| 14760 | if skip { |
| 14761 | for plane in 1..3usize { |
| 14762 | for rr in 0..ch / 4 { |
| 14763 | for cc in 0..cw / 4 { |
| 14764 | let (ox, oy) = (cc * 4, rr * 4); |
| 14765 | let cu_reach = tu_reach( |
| 14766 | pw, ph, ox * 2, oy * 2, 8, pair_reach, ppx, ppy, |
| 14767 | y.width, y.height, fctx, |
| 14768 | ); |
| 14769 | if let Some((ub, vb)) = &palette_uv_bufs { |
| 14770 | let buf = if plane == 1 { ub } else { vb }; |
| 14771 | set_palette_pred(palette_window(buf, cw, ox, oy, 4, 4), fctx); |
| 14772 | } |
| 14773 | push_intra( |
| 14774 | plane, cpx + ox, cpy + oy, 4, uv_predict_mode, |
| 14775 | angle_delta_uv, cu_reach, &ZERO_RESIDUAL[..16], None, None, |
| 14776 | smooth_neighbor_uv, fctx, |
| 14777 | ); |
| 14778 | let cu_mi = (pair_mi.0 + oy / 2, pair_mi.1 + ox / 2); |
| 14779 | neighbours.record_mi_chroma( |
| 14780 | cu_mi, 8, 8, plane, &ZERO_RESIDUAL[..16], |
| 14781 | ); |
| 14782 | } |
| 14783 | } |
| 14784 | } |
| 14785 | (Grid::Zero(cw * ch), Grid::Zero(cw * ch)) |
| 14786 | } else { |
| 14787 | let scan4 = default_scan(4); |
| 14788 | // `get_txb_ctx`: offset 10 (our tables store it at +3) |
| 14789 | // when the plane block is larger than the transform. |
| 14790 | let offset = Some(3usize); |
| 14791 | let mut uo = vec![0i32; cw * ch]; |
| 14792 | let mut vo = vec![0i32; cw * ch]; |
| 14793 | for plane in 1..3usize { |
| 14794 | for rr in 0..ch / 4 { |
| 14795 | for cc in 0..cw / 4 { |
| 14796 | let (ox, oy) = (cc * 4, rr * 4); |
| 14797 | if cpx + ox >= u.true_width || cpy + oy >= u.true_height { |
| 14798 | continue; |
| 14799 | } |
| 14800 | let cu_mi = (pair_mi.0 + oy / 2, pair_mi.1 + ox / 2); |
| 14801 | // One luma-mi cell: the chroma entropy arrays |
| 14802 | // are subsampled, and our bands replicate that |
| 14803 | // one entry over the unit's two cells |
| 14804 | // (`read_intra_chroma_lossless`). |
| 14805 | let cu_around = neighbours.around_mi(cu_mi, MI); |
| 14806 | let cu_reach = tu_reach( |
| 14807 | pw, ph, ox * 2, oy * 2, 8, pair_reach, ppx, ppy, |
| 14808 | y.width, y.height, fctx, |
| 14809 | ); |
| 14810 | if let Some((ub, vb)) = &palette_uv_bufs { |
| 14811 | let buf = if plane == 1 { ub } else { vb }; |
| 14812 | set_palette_pred(palette_window(buf, cw, ox, oy, 4, 4), fctx); |
| 14813 | } |
| 14814 | let buf: &mut PlaneBuf<'static> = |
| 14815 | if plane == 1 { &mut *u } else { &mut *v }; |
| 14816 | let cu_grid = read_plane( |
| 14817 | dec, cdfs, TxbSet::Chroma4, &scan4, plane, |
| 14818 | cu_around[plane], mode, uv_predict_mode, angle_delta_uv, |
| 14819 | cu_reach, buf, cpx + ox, cpy + oy, 4, 4, base_q_idx, |
| 14820 | None, None, offset, smooth_neighbor_uv, fctx, |
| 14821 | )?; |
| 14822 | neighbours.record_mi_chroma(cu_mi, 8, 8, plane, &cu_grid); |
| 14823 | hit!(CHROMA_SPLIT_TX_HITS); |
| 14824 | let dst = if plane == 1 { &mut uo } else { &mut vo }; |
| 14825 | for row in 0..4 { |
| 14826 | let start = (oy + row) * cw + ox; |
| 14827 | dst[start..start + 4] |
| 14828 | .copy_from_slice(&cu_grid[row * 4..][..4]); |
| 14829 | } |
| 14830 | } |
| 14831 | } |
| 14832 | } |
| 14833 | (Grid::Own(uo), Grid::Own(vo)) |
| 14834 | } |
| 14835 | } else if skip { |
| 14836 | let zeros = Grid::Zero(cw * ch); |
| 14837 | // libaom `predict_and_reconstruct_intra_block` (decodeframe.c |
| 14838 | // ~1000) predicts EVERY plane and only guards the residual on |
| 14839 | // `mbmi->skip_txfm`, so a skipped `UV_CFL_PRED` block is still |
| 14840 | // DC + alpha * luma-AC. Dropping the CfL term here left a |
| 14841 | // chroma-only bounded box on ReelC's key frames. |
| 14842 | let ac = alpha.map(|_| cfl_src_rect(ppx, ppy, pw, ph)); |
| 14843 | if alpha.is_some() { |
| 14844 | hit!(CHROMA_SKIP_CFL_HITS); |
| 14845 | } |
| 14846 | for (_buf, plane) in [(&mut *u, 1usize), (&mut *v, 2)] { |
| 14847 | if let Some((ub, vb)) = &palette_uv_bufs { |
| 14848 | set_palette_pred(if plane == 1 { ub.clone() } else { vb.clone() }, fctx); |
| 14849 | } |
| 14850 | push_intra_rect(plane, |
| 14851 | cpx, |
| 14852 | cpy, |
| 14853 | cw, |
| 14854 | ch, |
| 14855 | uv_predict_mode, |
| 14856 | angle_delta_uv, |
| 14857 | pair_reach, |
| 14858 | &zeros, |
| 14859 | alpha |
| 14860 | .zip(ac) |
| 14861 | .map(|((au, av), ac)| (if plane == 1 { au } else { av }, ac)), |
| 14862 | None, |
| 14863 | smooth_neighbor_uv, fctx, |
| 14864 | ); |
| 14865 | } |
| 14866 | (zeros.clone(), zeros) |
| 14867 | } else { |
| 14868 | let around = neighbours.around_mi_rect(pair_mi, pw, ph); |
| 14869 | // lane-av1-444: the strip's own 16x4 / 4x16 chroma at ss 0/0 |
| 14870 | // (`TxbSet::Chroma8`, `get_txsize_entropy_ctx(TX_16X4)` = |
| 14871 | // the 8x8 set); 4:2:0 keeps the pair's 8x4 / 4x8. |
| 14872 | let (chroma_set, chroma_scan): (TxbSet, &[u16]) = match (cw, ch) { |
| 14873 | (8, 4) => (TxbSet::ChromaRect8x4, &SCAN_8X4[..]), |
| 14874 | (4, 8) => (TxbSet::ChromaRect8x4, &SCAN_4X8[..]), |
| 14875 | (16, 4) => (TxbSet::Chroma8, &SCAN_16X4[..]), |
| 14876 | (4, 16) => (TxbSet::Chroma8, &SCAN_4X16[..]), |
| 14877 | // lane-av1-422: a 4:2:2 HORZ pair (16x8 luma) keeps the |
| 14878 | // pair's height: an 8x8 SQUARE chroma block, the same |
| 14879 | // `TxbSet::Chroma8` set any 8x8 chroma plane reads. The |
| 14880 | // 4:2:0 pair's chroma is 8x4 and 4:4:4's is 16x4, so this |
| 14881 | // row is unreachable there. It is also the witness arm: |
| 14882 | // a restored `/2 chroma height` lands on (8,4) and the |
| 14883 | // counter reads 0. |
| 14884 | (8, 8) => { |
| 14885 | hit!(CHROMA422_SQUARE_HITS); |
| 14886 | (TxbSet::Chroma8, default_scan(8)) |
| 14887 | } |
| 14888 | _ => unreachable!("decode_rect4_16_strip chroma shape set is closed"), |
| 14889 | }; |
| 14890 | let mut planes = Vec::with_capacity(2); |
| 14891 | for plane in 1..3 { |
| 14892 | let default_tx = default_intra_tx_type(uv_predict_mode as u8); |
| 14893 | let skip_ctx = usize::from(around[plane].0) + usize::from(around[plane].1); |
| 14894 | let mut coding = cdfs.txb(chroma_set, uv_predict_mode); |
| 14895 | let (levels, tx_type) = read_chroma_coeffs_rect( |
| 14896 | dec, |
| 14897 | &mut coding, |
| 14898 | chroma_scan, |
| 14899 | cw, |
| 14900 | ch, |
| 14901 | skip_ctx, |
| 14902 | dc_sign_ctx(around[plane].2), |
| 14903 | default_tx, |
| 14904 | fctx, |
| 14905 | )?; |
| 14906 | planes.push((levels, tx_type)); |
| 14907 | } |
| 14908 | let (v_levels, v_tx_type) = planes.pop().expect("plane 2"); |
| 14909 | let (u_levels, u_tx_type) = planes.pop().expect("plane 1"); |
| 14910 | // CfL averages the luma of the WHOLE pair, the region this one |
| 14911 | // chroma transform covers. |
| 14912 | let ac = alpha.map(|_| cfl_src_rect(ppx, ppy, pw, ph)); |
| 14913 | for (plane, _buf, levels, tx_type) in [ |
| 14914 | (1usize, &mut *u, &u_levels, u_tx_type), |
| 14915 | (2, &mut *v, &v_levels, v_tx_type), |
| 14916 | ] { |
| 14917 | let residual = dequant_and_inverse_typed_wh( |
| 14918 | levels, |
| 14919 | cw, |
| 14920 | ch, |
| 14921 | crate::decode::bit_depth(fctx), |
| 14922 | block_q_idx(fctx), |
| 14923 | plane_q_delta(plane, fctx).0, |
| 14924 | plane_q_delta(plane, fctx).1, |
| 14925 | tx_type, |
| 14926 | block_iqmatrix(fctx, plane, cw, ch, tx_type), |
| 14927 | ); |
| 14928 | if let Some((ub, vb)) = &palette_uv_bufs { |
| 14929 | set_palette_pred(if plane == 1 { ub.clone() } else { vb.clone() }, fctx); |
| 14930 | } |
| 14931 | push_intra_rect(plane, |
| 14932 | cpx, |
| 14933 | cpy, |
| 14934 | cw, |
| 14935 | ch, |
| 14936 | uv_predict_mode, |
| 14937 | angle_delta_uv, |
| 14938 | pair_reach, |
| 14939 | &residual, |
| 14940 | alpha |
| 14941 | .zip(ac) |
| 14942 | .map(|((au, av), ac)| (if plane == 1 { au } else { av }, ac)), |
| 14943 | None, |
| 14944 | smooth_neighbor_uv, fctx, |
| 14945 | ); |
| 14946 | } |
| 14947 | (u_levels, v_levels) |
| 14948 | }; |
| 14949 | neighbours.record_uv_mode_mi(pair_mi.0, pair_mi.1, pw / MI, ph / MI, uv_predict_mode); |
| 14950 | // A lossless pair already stamped each TX_4X4 (`record_mi_chroma`). |
| 14951 | // The single-state rewrite below is the non-lossless TX_8X4/TX_4X8. |
| 14952 | if !lossless_pair { |
| 14953 | // The chroma planes' coefficient context, over the PAIR's mi span |
| 14954 | // (`decode_leaf_rect8`'s own rule: a subsampled plane's bound is |
| 14955 | // the luma one rounded up to its 4x4 unit). |
| 14956 | let u_state = neighbour_state(&u_levels); |
| 14957 | let v_state = neighbour_state(&v_levels); |
| 14958 | let round_up_even = |n: usize| n.div_ceil(2) * 2; |
| 14959 | let bound_h = round_up_even(neighbours.mi_rows); |
| 14960 | let bound_w = round_up_even(neighbours.mi_cols); |
| 14961 | for cell in 0..(ph / MI) { |
| 14962 | if pair_mi.0 + cell < bound_h |
| 14963 | && let Some(slot) = neighbours.left.get_mut(pair_mi.0 + cell) |
| 14964 | { |
| 14965 | slot[1] = u_state; |
| 14966 | slot[2] = v_state; |
| 14967 | } |
| 14968 | } |
| 14969 | for cell in 0..(pw / MI) { |
| 14970 | if pair_mi.1 + cell < bound_w |
| 14971 | && let Some(slot) = neighbours.above.get_mut(pair_mi.1 + cell) |
| 14972 | { |
| 14973 | slot[1] = u_state; |
| 14974 | slot[2] = v_state; |
| 14975 | } |
| 14976 | } |
| 14977 | } |
| 14978 | last_uv_mode = Some(uv_predict_mode); |
| 14979 | } |
| 14980 | if horz { |
| 14981 | hit!(RECT4_16_HORZ_HITS); |
| 14982 | } else { |
| 14983 | hit!(RECT4_16_VERT_HITS); |
| 14984 | } |
| 14985 | let (pal_y_size, pal_y_colors) = |
| 14986 | palette_y.as_ref().map_or((0, [0u16; 8]), |p| (p.size, p.colors)); |
| 14987 | let (pal_uv_size, pal_uv_colors) = |
| 14988 | palette_uv.as_ref().map_or((0, [0u16; 8]), |p| (p.size, p.u_colors)); |
| 14989 | record_strip_palette( |
| 14990 | neighbours, lmi, bw, bh, pal_y_size, pal_y_colors, pal_uv_size, pal_uv_colors, |
| 14991 | ); |
| 14992 | // `set_txfm_ctxs(mbmi->tx_size, n4_w, n4_h, skip && is_inter, xd)` |
| 14993 | // (libaom `decode_token_recon_block`, run for EVERY block of an inter |
| 14994 | // frame): the above row records `tx_size_wide` over the block's width and |
| 14995 | // the left column `tx_size_high` over its height. This 1:4 strip reader is |
| 14996 | // also the INTER frame's reader for the shape ([`decode_intra_rect4_in_inter`]), |
| 14997 | // and it published neither band -- so the block after a 64x16 strip read |
| 14998 | // the row's init value 64 as its left transform height and took |
| 14999 | // `tx_size_cat3` row 1 where libaom takes row 0 (lane-frame36 r1: the |
| 15000 | // 3840x1608 10-bit stream's first silent wall, decode-order frame 33 at |
| 15001 | // mi(16,480)). Same write as [`set_txfm_ctxs`], rect-aware. |
| 15002 | txfm_partition_update_rect(neighbours, lmi, (tx_w, tx_h), (bw, bh)); |
| 15003 | Ok((mode, last_uv_mode, (tx_w, tx_h))) |
| 15004 | } |
| 15005 | |
| 15006 | |
| 15007 | /// Decodes one true `bw`x`bh` superblock-level `PARTITION_HORZ`/ |
| 15008 | /// `PARTITION_VERT` strip (lane-sbpart r2, `bw, bh` one of `(64, 32)` / |
| 15009 | /// `(32, 64)` -- [`decode_block_rect`]'s own sibling one level up). Unlike |
| 15010 | /// that 32x16/16x32 strip, both dimensions here exceed the 32-coefficient |
| 15011 | /// cap, so LUMA needs the same corner truncation [`decode_block`] already |
| 15012 | /// does for a plain 64x64 square (spec 5.11.40): `get_txsize_entropy_ctx` |
| 15013 | /// resolves TX_64X32/TX_32X64 to TX_64X64 regardless of orientation (see |
| 15014 | /// this function's own charter/report), so both strips read a real 32x32 |
| 15015 | /// corner through [`read_coeffs`] with [`TxbSet::Luma64`]/[`SCAN_32`] and |
| 15016 | /// embed it top-left of a zeroed `bw`x`bh` grid, mirroring |
| 15017 | /// [`read_plane`]'s `tx_side != side` branch generalized to non-square. |
| 15018 | /// CHROMA (TX_32X16/TX_16X32) resolves to TX_32X32 -- a real, *untruncated* |
| 15019 | /// `bw/2`x`bh/2` transform via [`read_coeffs_rect`] with |
| 15020 | /// [`TxbSet::ChromaRect32x16`] and the matching [`SCAN_32X16`]/ |
| 15021 | /// [`SCAN_16X32`], same as [`decode_block_rect`]'s own chroma path. Like |
| 15022 | /// that function, only the single-transform-unit (`tx_depth == 0`) case is |
| 15023 | /// supported: a split transform refuses by name rather than guess-decode. |
| 15024 | #[allow(clippy::too_many_arguments)] |
| 15025 | fn decode_block_rect64( |
| 15026 | dec: &mut SymbolDecoder, |
| 15027 | cdfs: &mut Cdfs, |
| 15028 | neighbours: &mut Neighbours, |
| 15029 | at: (usize, usize), |
| 15030 | bw: usize, |
| 15031 | bh: usize, |
| 15032 | y: &mut PlaneBuf<'static>, |
| 15033 | u: &mut PlaneBuf<'static>, |
| 15034 | v: &mut PlaneBuf<'static>, |
| 15035 | enable_filter_intra: bool, |
| 15036 | allow_screen_content_tools: bool, |
| 15037 | base_q_idx: u8, |
| 15038 | tx_select: bool, |
| 15039 | reduced_tx_set: bool, fctx: &crate::decode::FrameCtx, |
| 15040 | ) -> Result<()> { |
| 15041 | let (r, c) = at; |
| 15042 | let (px, py) = (c * SUB, r * SUB); |
| 15043 | let (nb_above_mode, nb_left_mode) = neighbours.modes_above_left(r, c); |
| 15044 | let (palette_ctx, palette_cache) = neighbours.palette_ctx_and_cache(at); |
| 15045 | let palette_uv_cache = neighbours.palette_uv_cache(at); |
| 15046 | let (skip, mode, angle_delta_y, uv_mode, angle_delta_uv, alpha, filter_intra, palette_y, palette_uv, intrabc) = |
| 15047 | read_intra_mode_rect( |
| 15048 | dec, |
| 15049 | cdfs, |
| 15050 | nb_above_mode, |
| 15051 | nb_left_mode, |
| 15052 | // `is_cfl_allowed` (spec 5.11.5) caps CFL at <=32x32; these |
| 15053 | // superblock-level HORZ/VERT strips are 64x32/32x64, so unlike |
| 15054 | // `decode_block_rect`'s own 32x16/16x32 strips (where `true` is |
| 15055 | // correct), CFL must never be offered here -- passing `true` |
| 15056 | // read `uv_mode` off the 14-symbol `uv_mode_cfl` CDF instead of |
| 15057 | // the real 13-symbol `uv_mode_no_cfl` one and desynced the tile |
| 15058 | // from this block's very first symbol (lane-sbpart r3 bisect). |
| 15059 | false, |
| 15060 | true, |
| 15061 | bw, |
| 15062 | bh, |
| 15063 | enable_filter_intra, |
| 15064 | neighbours.skip_txfm_ctx(r * (SUB / MI), c * (SUB / MI)), |
| 15065 | allow_screen_content_tools, |
| 15066 | Some((palette_ctx, &palette_cache)), |
| 15067 | &palette_uv_cache, |
| 15068 | r * (SUB / MI), |
| 15069 | c * (SUB / MI), fctx, |
| 15070 | )?; |
| 15071 | if let Some((dv_row, dv_col)) = intrabc { |
| 15072 | return decode_intrabc_owned_rect( |
| 15073 | dec, cdfs, neighbours, (r * (SUB / MI), c * (SUB / MI)), (bw, bh), skip, |
| 15074 | (dv_row, dv_col), y, u, v, base_q_idx, tx_select, reduced_tx_set, fctx, |
| 15075 | ); |
| 15076 | } |
| 15077 | if (palette_y.is_some() || palette_uv.is_some()) && !skip { |
| 15078 | // lane-t900 r34: a non-skip palette block on a superblock-level strip |
| 15079 | // -- counted so its gate cannot go vacuous (this was a refusal until |
| 15080 | // r34; see the gate `a_real_aomenc_palette_stream_with_8x8_leaves_ |
| 15081 | // decodes_pixel_exact`). |
| 15082 | hit!(PALETTE_SB_STRIP_HITS); |
| 15083 | } |
| 15084 | let smooth_neighbor = is_smooth_mode(nb_above_mode) || is_smooth_mode(nb_left_mode); |
| 15085 | if smooth_neighbor { |
| 15086 | hit!(SMOOTH_LUMA_HITS); |
| 15087 | } |
| 15088 | let smooth_neighbor_uv = |
| 15089 | neighbours.smooth_uv_neighbour(r * (SUB / MI), c * (SUB / MI), r, c, fctx); |
| 15090 | if filter_intra.is_some() { |
| 15091 | // `filter_intra_size_class_rect` already returns `None` for both |
| 15092 | // `(64, 32)` and `(32, 64)` (`av1_filter_intra_allowed_bsize` caps at |
| 15093 | // 32 on both axes), so this symbol is never read at this level -- |
| 15094 | // kept only as the same defensive refusal [`decode_block_rect`] has. |
| 15095 | return Err(unsupported( |
| 15096 | "filter intra on a superblock-level HORZ/VERT strip (never expected -- \ |
| 15097 | av1_filter_intra_allowed_bsize caps at 32x32)", |
| 15098 | )); |
| 15099 | } |
| 15100 | let uv_predict_mode = if uv_mode == UV_CFL_PRED { |
| 15101 | DC_PRED |
| 15102 | } else { |
| 15103 | uv_mode |
| 15104 | }; |
| 15105 | let (mi_r, mi_c) = (r * (SUB / MI), c * (SUB / MI)); |
| 15106 | // lane-av1mixloss: the lossless-segment early return (`decodeframe.c:1203`) |
| 15107 | // precedes `TX_MODE_SELECT`, so a lossless-segment 64-axis rect block codes |
| 15108 | // no depth symbol here either (class sweep of the sub8 leaf's fix). |
| 15109 | let depth = if tx_select && !lossless(fctx) { |
| 15110 | // lane-av1txbands: EVERY frame now reads the real `TXFM_CONTEXT` |
| 15111 | // bands (`get_tx_size_context`, libaom `pred_common.h:342`) -- an |
| 15112 | // intra-only frame publishes them from every block too |
| 15113 | // ([`Neighbours::fill_lf_grid_rect`]). Measured on the 10-bit |
| 15114 | // 1920x792 128-superblock stream, |
| 15115 | // decode-order frame 1: the 32x8 HORZ_4 strip at mi(48,176) read |
| 15116 | // `tx_size_cat2` row 1 where libaom reads row 2. |
| 15117 | let ctx = tx_size_context_txfm_rect(neighbours, (mi_r, mi_c), bw, bh, fctx); |
| 15118 | // lane-rectsplit r1: `bsize_to_tx_size_cat` (libaom `decodemv.c`) |
| 15119 | // counts `sub_tx_size_map` steps from the block's own max rect |
| 15120 | // transform down to TX_4X4, minus one -- BLOCK_64X32's chain |
| 15121 | // (TX_64X32 -> TX_32X32 -> TX_16X16 -> TX_8X8 -> TX_4X4) is four |
| 15122 | // steps, so this level's category is 3 (the same one a 64x64 square |
| 15123 | // block uses), not the 2 a 32x16 strip uses. Reading the cat-2 CDF |
| 15124 | // here was a wrong-table read of a real symbol. |
| 15125 | let d = dec.symbol(&mut cdfs.tx_size_cat3[ctx]); |
| 15126 | if crate::envflags::env_flag!("EC_TRACE_MODE_STEP") { |
| 15127 | eprintln!( |
| 15128 | "EC_ISTEP mi_row={mi_r} mi_col={mi_c} name=tx_depth val={d} ctx={ctx} cat=3 rng={}", |
| 15129 | dec.debug_state().0 |
| 15130 | ); |
| 15131 | } |
| 15132 | d |
| 15133 | } else { |
| 15134 | 0 |
| 15135 | }; |
| 15136 | // lane-t900 r1: this strip's own `set_txfm_ctxs` when it is an intra |
| 15137 | // block of an INTER frame -- see [`publish_txfm_bands_if_in_inter`]. |
| 15138 | publish_txfm_bands_if_in_inter( |
| 15139 | neighbours, |
| 15140 | (mi_r, mi_c), |
| 15141 | depth_to_tx_wh(bw, bh, depth, fctx), |
| 15142 | (bw, bh), fctx, |
| 15143 | ); |
| 15144 | if depth != 0 || lossless(fctx) { |
| 15145 | hit!(TX_DEPTH_HITS); |
| 15146 | // lane-rectsplit r1/r4: the superblock-level strip splits its |
| 15147 | // transform through the very same per-unit path as its 32x32-level |
| 15148 | // sibling; [`depth_to_tx_wh`] names the unit, which for a 64x16/16x64 |
| 15149 | // 1:4 strip is the 2:1 rect TX_32X16 at depth 1 (lane-rectchroma r1 |
| 15150 | // measured the old square formula mis-tiling exactly that strip). |
| 15151 | let (tx_w, tx_h) = depth_to_tx_wh(bw, bh, depth, fctx); |
| 15152 | if bw.max(bh) / bw.min(bh) == 4 { |
| 15153 | let which = if depth == 1 { |
| 15154 | &RECT4_SPLIT_DEPTH1_HITS |
| 15155 | } else { |
| 15156 | &RECT4_SPLIT_DEPTH2_HITS |
| 15157 | }; |
| 15158 | hit_do! { which.with(|c| c.set(c.get() + 1)); } |
| 15159 | } |
| 15160 | if crate::envflags::env_flag!("EC_SBPART_DUMP64") { |
| 15161 | eprintln!( |
| 15162 | "DUMP64SPLIT mi_r={mi_r} mi_c={mi_c} px={px} py={py} bw={bw} bh={bh} \ |
| 15163 | depth={depth} tx={tx_w}x{tx_h} mode={mode} uv={uv_predict_mode} skip={skip} \ |
| 15164 | angle_y={angle_delta_y}" |
| 15165 | ); |
| 15166 | } |
| 15167 | let modes = RectStripModes { |
| 15168 | skip, |
| 15169 | mode, |
| 15170 | angle_delta_y, |
| 15171 | uv_predict_mode, |
| 15172 | angle_delta_uv, |
| 15173 | alpha, |
| 15174 | filter_intra, |
| 15175 | smooth_neighbor, |
| 15176 | smooth_neighbor_uv, |
| 15177 | palette_y: palette_y |
| 15178 | .as_ref() |
| 15179 | .map(|p| p.map.iter().map(|&i| p.colors[i as usize]).collect()), |
| 15180 | palette_uv: palette_uv.as_ref().map(|p| { |
| 15181 | ( |
| 15182 | p.map.iter().map(|&i| p.u_colors[i as usize]).collect(), |
| 15183 | p.map.iter().map(|&i| p.v_colors[i as usize]).collect(), |
| 15184 | ) |
| 15185 | }), |
| 15186 | }; |
| 15187 | if palette_y.is_some() || palette_uv.is_some() { |
| 15188 | hit!(PALETTE_SPLIT_TX_HITS); |
| 15189 | } |
| 15190 | decode_rect_split( |
| 15191 | dec, |
| 15192 | cdfs, |
| 15193 | neighbours, |
| 15194 | (mi_r, mi_c), |
| 15195 | bw, |
| 15196 | bh, |
| 15197 | tx_w, |
| 15198 | tx_h, |
| 15199 | &modes, |
| 15200 | y, |
| 15201 | u, |
| 15202 | v, |
| 15203 | base_q_idx, |
| 15204 | reduced_tx_set, fctx, |
| 15205 | )?; |
| 15206 | // Same early-return palette stamp as [`decode_block_rect`]'s split arm |
| 15207 | // (lane-palette2 r10): this return skips the fn's own tail, so a |
| 15208 | // split-tx superblock strip would otherwise leave the above/left |
| 15209 | // palette arrays holding a dead block's colours. |
| 15210 | let (py_size, py_colors) = palette_y.as_ref().map_or((0, [0u16; 8]), |p| (p.size, p.colors)); |
| 15211 | neighbours.record_palette_y_rect((mi_r, mi_c), bw, bh, py_size, py_colors); |
| 15212 | let (puv_size, puv_colors) = |
| 15213 | palette_uv.as_ref().map_or((0, [0u16; 8]), |p| (p.size, p.u_colors)); |
| 15214 | neighbours.record_palette_uv_rect((mi_r, mi_c), bw, bh, puv_size, puv_colors); |
| 15215 | // lane-frame36 r2: `set_txfm_ctxs` runs for EVERY block and this early |
| 15216 | // return skips the fn's own tail, so a SPLIT superblock-level strip |
| 15217 | // published no TXFM_CONTEXT band and the next block read the row's init |
| 15218 | // value 64 as its left transform height. Measured on the 10-bit |
| 15219 | // 3840x1608 stream, decode-order frame 33: after the split 16x64 strip |
| 15220 | // at mi(192,140) (`tx_depth=1` -> TX_16X32) the 32x64 block at |
| 15221 | // mi(192,144) took `tx_size_cat3` row 2 where libaom takes row 1. The |
| 15222 | // bands carry the SPLIT unit's size, not the block's. |
| 15223 | txfm_partition_update_rect(neighbours, (mi_r, mi_c), (tx_w, tx_h), (bw, bh)); |
| 15224 | hit!(RECT64_SPLIT_TXFM_PUBLISH_HITS); |
| 15225 | hit!(RECT_PARTITION_HITS); |
| 15226 | return Ok(()); |
| 15227 | } |
| 15228 | let (tx_w, tx_h) = (bw, bh); |
| 15229 | let (cpx, cpy) = (px >> ss_x(fctx), py >> ss_y(fctx)); |
| 15230 | let (chroma_w, chroma_h) = (bw >> ss_x(fctx), bh >> ss_y(fctx)); |
| 15231 | let reach = Reach::of_rect(bw, bh, px, py, y.width, y.height, fctx); |
| 15232 | if let Some(ref pyv) = palette_y { |
| 15233 | let buf: Vec<u16> = pyv.map.iter().map(|&idx| pyv.colors[idx as usize]).collect(); |
| 15234 | set_palette_pred(buf, fctx); |
| 15235 | } |
| 15236 | let palette_uv_bufs: Option<(Vec<u16>, Vec<u16>)> = palette_uv.as_ref().map(|puv| { |
| 15237 | ( |
| 15238 | puv.map.iter().map(|&idx| puv.u_colors[idx as usize]).collect(), |
| 15239 | puv.map.iter().map(|&idx| puv.v_colors[idx as usize]).collect(), |
| 15240 | ) |
| 15241 | }); |
| 15242 | if skip { |
| 15243 | push_intra_rect(0, |
| 15244 | px, |
| 15245 | py, |
| 15246 | bw, |
| 15247 | bh, |
| 15248 | mode, |
| 15249 | angle_delta_y, |
| 15250 | reach, |
| 15251 | &ZERO_RESIDUAL[..bw * bh], |
| 15252 | None, |
| 15253 | filter_intra, |
| 15254 | smooth_neighbor, fctx, |
| 15255 | ); |
| 15256 | let ac = alpha.map(|_| cfl_src_rect(px, py, bw, bh)); |
| 15257 | if let Some((ub, _)) = &palette_uv_bufs { |
| 15258 | set_palette_pred(ub.clone(), fctx); |
| 15259 | } |
| 15260 | push_intra_rect(1, |
| 15261 | cpx, |
| 15262 | cpy, |
| 15263 | chroma_w, |
| 15264 | chroma_h, |
| 15265 | uv_predict_mode, |
| 15266 | angle_delta_uv, |
| 15267 | reach, |
| 15268 | &ZERO_RESIDUAL[..chroma_w * chroma_h], |
| 15269 | alpha.zip(ac).map(|((au, _), ac)| (au, ac)), |
| 15270 | None, |
| 15271 | smooth_neighbor_uv, fctx, |
| 15272 | ); |
| 15273 | if let Some((_, vb)) = &palette_uv_bufs { |
| 15274 | set_palette_pred(vb.clone(), fctx); |
| 15275 | } |
| 15276 | push_intra_rect(2, |
| 15277 | cpx, |
| 15278 | cpy, |
| 15279 | chroma_w, |
| 15280 | chroma_h, |
| 15281 | uv_predict_mode, |
| 15282 | angle_delta_uv, |
| 15283 | reach, |
| 15284 | &ZERO_RESIDUAL[..chroma_w * chroma_h], |
| 15285 | alpha.zip(ac).map(|((_, av), ac)| (av, ac)), |
| 15286 | None, |
| 15287 | smooth_neighbor_uv, fctx, |
| 15288 | ); |
| 15289 | neighbours.record_rect( |
| 15290 | at, |
| 15291 | bw, |
| 15292 | bh, |
| 15293 | mode, |
| 15294 | uv_predict_mode, |
| 15295 | &[ |
| 15296 | &ZERO_RESIDUAL[..bw * bh], |
| 15297 | &ZERO_RESIDUAL[..chroma_w * chroma_h], |
| 15298 | &ZERO_RESIDUAL[..chroma_w * chroma_h], |
| 15299 | ], |
| 15300 | ); |
| 15301 | } else { |
| 15302 | let around = neighbours.around_rect(at, bw, bh); |
| 15303 | // LUMA: the coded corner, embedded top-left of the true bw x bh grid |
| 15304 | // (see this function's own doc comment). A 64-length axis codes only |
| 15305 | // its low 32 coefficients, so the corner is `min(bw, 32)` x |
| 15306 | // `min(bh, 32)`: 32x32 under a 64x32/32x64 HORZ/VERT strip, and |
| 15307 | // 32x16/16x32 under a 64x16/16x64 1:4 strip (lane-tx64x16). |
| 15308 | let (luma_cw, luma_ch) = (bw.min(32), bh.min(32)); |
| 15309 | let scan32 = default_scan(TX32); |
| 15310 | if coeff_trace_on() { |
| 15311 | let (rng, _) = dec.debug_state(); |
| 15312 | eprintln!("EC_COEFF plane=0 row={mi_r} col={mi_c} tx_size={luma_cw}x{luma_ch} rng={rng}"); |
| 15313 | } |
| 15314 | let (luma_corner, luma_tx_type) = if (luma_cw, luma_ch) == (32, 32) { |
| 15315 | let mut luma_coding = cdfs.txb(TxbSet::Luma64, mode); |
| 15316 | read_coeffs( |
| 15317 | dec, |
| 15318 | &mut luma_coding, |
| 15319 | &scan32, |
| 15320 | 0, |
| 15321 | dc_sign_ctx(around[0].2), |
| 15322 | TxType::DctDct, |
| 15323 | Some((bw, bh)), |
| 15324 | )? |
| 15325 | } else { |
| 15326 | // `av1_get_adjusted_tx_size` (`blockd.h:1361`) maps TX_64X16 -> |
| 15327 | // TX_32X16 and TX_16X64 -> TX_16X32, and `av1_scan_orders` |
| 15328 | // (`scan.c`) gives both the very same `default_scan_32x16` / |
| 15329 | // `default_scan_16x32` the 2:1 sizes use ("Half of the |
| 15330 | // coefficients of tx64 at higher frequencies are set to zeros. So |
| 15331 | // tx32's scan order is used"). `get_txsize_entropy_ctx` is |
| 15332 | // (txsize_sqr + txsize_sqr_up + 1) >> 1 = (TX_16X16 + TX_64X64 + |
| 15333 | // 1) >> 1 = TX_32X32 and `txsize_log2_minus4[TX_64X16]` is 5 (a |
| 15334 | // 512-position eob group) -- BOTH identical to TX_32X16, so the |
| 15335 | // 2:1 CDF set `LumaRect32x16` is bit-exact here and no new table |
| 15336 | // is owed. `av1_nz_map_ctx_offset[TX_64X16]` is literally |
| 15337 | // `av1_nz_map_ctx_offset_32x16` (`txb_common.c:340`), which is |
| 15338 | // what `base_ctx_rect` computes at (32, 16). |
| 15339 | let luma_scan: &[u16] = if bw == 64 { &SCAN_32X16 } else { &SCAN_16X32 }; |
| 15340 | let mut luma_coding = cdfs.txb(TxbSet::LumaRect32x16, mode); |
| 15341 | read_coeffs_rect( |
| 15342 | dec, |
| 15343 | &mut luma_coding, |
| 15344 | luma_scan, |
| 15345 | luma_cw, |
| 15346 | luma_ch, |
| 15347 | 0, |
| 15348 | dc_sign_ctx(around[0].2), |
| 15349 | TxType::DctDct, |
| 15350 | )? |
| 15351 | }; |
| 15352 | if coeff_trace_on() { |
| 15353 | let (rng, _) = dec.debug_state(); |
| 15354 | eprintln!("EC_COEFF_VAL plane=0 row={mi_r} col={mi_c} rng={rng}"); |
| 15355 | } |
| 15356 | let mut luma_levels = vec![0i32; bw * bh]; |
| 15357 | for row in 0..luma_ch { |
| 15358 | luma_levels[row * bw..][..luma_cw] |
| 15359 | .copy_from_slice(&luma_corner[row * luma_cw..][..luma_cw]); |
| 15360 | } |
| 15361 | { |
| 15362 | let cur = block_q_idx(fctx); |
| 15363 | if cur != i32::from(base_q_idx) { |
| 15364 | hit!(RECT64_QIDX_DRIFT_HITS); |
| 15365 | } |
| 15366 | if crate::envflags::env_flag!("EC_AV1_TRACE") { |
| 15367 | eprintln!( |
| 15368 | "TRACE rect64_dequant plane=0 base_q_idx={base_q_idx} current_q_idx={cur}" |
| 15369 | ); |
| 15370 | } |
| 15371 | } |
| 15372 | let luma_residual = dequant_and_inverse_typed_wh( |
| 15373 | &luma_levels, |
| 15374 | bw, |
| 15375 | bh, |
| 15376 | crate::decode::bit_depth(fctx), |
| 15377 | block_q_idx(fctx), |
| 15378 | plane_q_delta(0, fctx).0, |
| 15379 | plane_q_delta(0, fctx).1, |
| 15380 | luma_tx_type, |
| 15381 | block_iqmatrix(fctx, 0, bw, bh, luma_tx_type), |
| 15382 | ); |
| 15383 | push_intra_rect(0, |
| 15384 | px, |
| 15385 | py, |
| 15386 | bw, |
| 15387 | bh, |
| 15388 | mode, |
| 15389 | angle_delta_y, |
| 15390 | reach, |
| 15391 | &luma_residual, |
| 15392 | None, |
| 15393 | filter_intra, |
| 15394 | smooth_neighbor, fctx, |
| 15395 | ); |
| 15396 | if crate::envflags::env_flag!("EC_SBPART_DUMP64") { |
| 15397 | let leftcol: Vec<u16> = if px > 0 { |
| 15398 | (py..py + bh).map(|row| y.data[row * y.width + px - 1]).collect() |
| 15399 | } else { |
| 15400 | vec![] |
| 15401 | }; |
| 15402 | eprintln!( |
| 15403 | "DUMP64 mi_r={mi_r} mi_c={mi_c} px={px} py={py} bw={bw} bh={bh} mode={mode} \ |
| 15404 | skip={skip} eob_nonzero={} row0={:?} leftcol={:?}", |
| 15405 | luma_corner.iter().any(|&v| v != 0), |
| 15406 | &y.data[py * y.width + px..][..bw], |
| 15407 | leftcol, |
| 15408 | ); |
| 15409 | } |
| 15410 | // CHROMA. At 4:2:0 the plane block is already one adjusted UV |
| 15411 | // transform (<=32 on each axis). At 4:4:4 a 64-axis strip's chroma |
| 15412 | // is larger than that, so it is tiled into 32-capped units. |
| 15413 | let _ac = alpha.map(|_| cfl_src_rect(px, py, bw, bh)); |
| 15414 | let (uw, uh) = (chroma_w.min(32), chroma_h.min(32)); |
| 15415 | let (nw, nh) = (chroma_w / uw, chroma_h / uh); |
| 15416 | let (chroma_set, chroma_scan): (TxbSet, &[u16]) = match (uw, uh) { |
| 15417 | (32, 32) => (TxbSet::Chroma32, default_scan(32)), |
| 15418 | (32, 16) => (TxbSet::ChromaRect32x16, &SCAN_32X16), |
| 15419 | (16, 32) => (TxbSet::ChromaRect32x16, &SCAN_16X32), |
| 15420 | (32, 8) => (TxbSet::Chroma16, &SCAN_32X8), |
| 15421 | (8, 32) => (TxbSet::Chroma16, &SCAN_8X32), |
| 15422 | _ => { |
| 15423 | return Err(unsupported( |
| 15424 | "a 64-axis strip whose chroma unit has no coefficient table", |
| 15425 | )) |
| 15426 | } |
| 15427 | }; |
| 15428 | let bigger = nw * nh > 1; |
| 15429 | let mut u_levels = vec![0i32; chroma_w * chroma_h]; |
| 15430 | let mut v_levels = vec![0i32; chroma_w * chroma_h]; |
| 15431 | // The tiled units below stamp their own coefficient context |
| 15432 | // immediately, then the whole-strip `record_rect` rewrite paints |
| 15433 | // every chroma cell with ONE whole-strip state and the per-unit DC |
| 15434 | // signs and levels are lost (class `override-slot-on-one-arm`, same |
| 15435 | // shape as `decode_rect_split`'s chroma_tiled clobber). Collected |
| 15436 | // here and replayed after that record, the c8 `mu_chroma_units` way. |
| 15437 | let mut tiled_chroma_units: Vec<((usize, usize), usize, Grid, usize, usize)> = Vec::new(); |
| 15438 | for plane_idx in 1..=2 { |
| 15439 | for cu_row in 0..nh { |
| 15440 | for cu_col in 0..nw { |
| 15441 | let cu_around = if nw == 1 && nh == 1 { |
| 15442 | around |
| 15443 | } else { |
| 15444 | let lsx = uw << ss_x(fctx); |
| 15445 | let lsy = uh << ss_y(fctx); |
| 15446 | let cu_mi = ( |
| 15447 | mi_r + cu_row * (lsy / MI), |
| 15448 | mi_c + cu_col * (lsx / MI), |
| 15449 | ); |
| 15450 | neighbours.around_mi_rect(cu_mi, lsx, lsy) |
| 15451 | }; |
| 15452 | let lsx = uw << ss_x(fctx); |
| 15453 | let lsy = uh << ss_y(fctx); |
| 15454 | let cfl = alpha.map(|(au, av)| { |
| 15455 | let a = if plane_idx == 1 { au } else { av }; |
| 15456 | (a, cfl_src_rect(px + cu_col * lsx, py + cu_row * lsy, lsx, lsy)) |
| 15457 | }); |
| 15458 | let levels = if uw == uh { |
| 15459 | let plane = if plane_idx == 1 { &mut *u } else { &mut *v }; |
| 15460 | if let Some((ub, vb)) = &palette_uv_bufs { |
| 15461 | set_palette_pred((if plane_idx == 1 { ub } else { vb }).clone(), fctx); |
| 15462 | } |
| 15463 | read_plane( |
| 15464 | dec, cdfs, chroma_set, chroma_scan, plane_idx, cu_around[plane_idx], |
| 15465 | mode, uv_predict_mode, angle_delta_uv, reach, plane, |
| 15466 | cpx + cu_col * uw, cpy + cu_row * uh, uw, uw, base_q_idx, |
| 15467 | cfl, None, bigger.then_some(3), smooth_neighbor_uv, fctx, |
| 15468 | )? |
| 15469 | } else { |
| 15470 | let skip_ctx = usize::from(cu_around[plane_idx].0) |
| 15471 | + usize::from(cu_around[plane_idx].1) |
| 15472 | + usize::from(bigger) * 3; |
| 15473 | let mut coding = cdfs.txb(chroma_set, uv_predict_mode); |
| 15474 | let (levels, tx_type) = read_chroma_coeffs_rect( |
| 15475 | dec, &mut coding, chroma_scan, uw, uh, skip_ctx, |
| 15476 | dc_sign_ctx(cu_around[plane_idx].2), TxType::DctDct, fctx, |
| 15477 | )?; |
| 15478 | let residual = dequant_and_inverse_typed_wh( |
| 15479 | &levels, uw, uh, crate::decode::bit_depth(fctx), |
| 15480 | block_q_idx(fctx), |
| 15481 | plane_q_delta(plane_idx, fctx).0, |
| 15482 | plane_q_delta(plane_idx, fctx).1, |
| 15483 | tx_type, |
| 15484 | block_iqmatrix(fctx, plane_idx, uw, uh, tx_type), |
| 15485 | ); |
| 15486 | if let Some((ub, vb)) = &palette_uv_bufs { |
| 15487 | set_palette_pred((if plane_idx == 1 { ub } else { vb }).clone(), fctx); |
| 15488 | } |
| 15489 | push_intra_rect( |
| 15490 | plane_idx, cpx + cu_col * uw, cpy + cu_row * uh, uw, uh, |
| 15491 | uv_predict_mode, angle_delta_uv, reach, &residual, |
| 15492 | cfl, None, smooth_neighbor_uv, fctx, |
| 15493 | ); |
| 15494 | levels |
| 15495 | }; |
| 15496 | let dst = if plane_idx == 1 { &mut u_levels } else { &mut v_levels }; |
| 15497 | for row in 0..uh { |
| 15498 | let d = (cu_row * uh + row) * chroma_w + cu_col * uw; |
| 15499 | dst[d..d + uw].copy_from_slice(&levels[row * uw..][..uw]); |
| 15500 | } |
| 15501 | if bigger { |
| 15502 | neighbours.record_mi_chroma( |
| 15503 | (mi_r + cu_row * (lsy / MI), mi_c + cu_col * (lsx / MI)), |
| 15504 | lsx, lsy, plane_idx, &levels, |
| 15505 | ); |
| 15506 | tiled_chroma_units.push(( |
| 15507 | (mi_r + cu_row * (lsy / MI), mi_c + cu_col * (lsx / MI)), |
| 15508 | plane_idx, |
| 15509 | levels.clone(), |
| 15510 | lsx, |
| 15511 | lsy, |
| 15512 | )); |
| 15513 | } |
| 15514 | } |
| 15515 | } |
| 15516 | } |
| 15517 | neighbours.record_rect(at, bw, bh, mode, uv_predict_mode, &[&luma_levels[..], &u_levels[..], &v_levels[..]]); |
| 15518 | // The per-unit re-stamp: `record_rect` above wrote the whole strip's |
| 15519 | // chroma cells from the assembled grids' one whole-strip state, |
| 15520 | // overwriting exactly the stamps the tiled loop just made. |
| 15521 | for (cu_mi, plane, unit, span_w, span_h) in &tiled_chroma_units { |
| 15522 | neighbours.record_mi_chroma(*cu_mi, *span_w, *span_h, *plane, unit); |
| 15523 | } |
| 15524 | hit!(RECT_COEFF_HITS); |
| 15525 | } |
| 15526 | let (py_size, py_colors) = palette_y.as_ref().map_or((0, [0u16; 8]), |p| (p.size, p.colors)); |
| 15527 | neighbours.record_palette_y_rect((mi_r, mi_c), bw, bh, py_size, py_colors); |
| 15528 | let (puv_size, puv_colors) = |
| 15529 | palette_uv.as_ref().map_or((0, [0u16; 8]), |p| (p.size, p.u_colors)); |
| 15530 | neighbours.record_palette_uv_rect((mi_r, mi_c), bw, bh, puv_size, puv_colors); |
| 15531 | neighbours.fill_skip_grid_rect((mi_r, mi_c), bw / MI, bh / MI, skip); |
| 15532 | neighbours.fill_lf_grid_rect((mi_r, mi_c), bw / MI, bh / MI, tx_w as u8, tx_h as u8, 0, fctx); |
| 15533 | // `set_txfm_ctxs(mbmi->tx_size, n4_w, n4_h, skip && is_inter, xd)` |
| 15534 | // (libaom `decode_token_recon_block`, run for EVERY block of an inter |
| 15535 | // frame): the above row records `tx_size_wide` over the block's width and |
| 15536 | // the left column `tx_size_high` over its height. This 1:4 strip reader is |
| 15537 | // also the INTER frame's reader for the shape ([`decode_intra_rect4_in_inter`]), |
| 15538 | // and it published neither band -- so the block after a 64x16 strip read |
| 15539 | // the row's init value 64 as its left transform height and took |
| 15540 | // `tx_size_cat3` row 1 where libaom takes row 0 (lane-frame36 r1: the |
| 15541 | // 3840x1608 10-bit stream's first silent wall, decode-order frame 33 at |
| 15542 | // mi(16,480)). Same write as [`set_txfm_ctxs`], rect-aware. |
| 15543 | txfm_partition_update_rect(neighbours, (mi_r, mi_c), (tx_w, tx_h), (bw, bh)); |
| 15544 | hit!(SB_RECT_HITS); |
| 15545 | match (bw, bh) { |
| 15546 | (64, 16) => hit!(SB_RECT4_HORZ_HITS), |
| 15547 | (16, 64) => hit!(SB_RECT4_VERT_HITS), |
| 15548 | _ => {} |
| 15549 | } |
| 15550 | if crate::envflags::env_flag!("EC_AV1_TRACE") { |
| 15551 | let (rng, _) = dec.debug_state(); |
| 15552 | eprintln!("TRACE_RECT64_END mi_row={mi_r} mi_col={mi_c} bw={bw} bh={bh} rng={rng}"); |
| 15553 | } |
| 15554 | Ok(()) |
| 15555 | } |
| 15556 | |
| 15557 | /// `read_angle_delta`/`Angle_Delta` (spec 5.11.42 + 9.3): the symbol minus |
| 15558 | /// [`ANGLE_DELTA_ZERO`] gives the signed `-MAX_ANGLE_DELTA..=MAX_ANGLE_DELTA` |
| 15559 | /// delta [`crate::intra::predict`] wants; bumps [`ANGLE_DELTA_HITS`] whenever |
| 15560 | /// it lands away from zero, for gate tests to prove a real stream fired one. |
| 15561 | fn read_angle_delta(dec: &mut SymbolDecoder, cdf: &mut [u16]) -> i32 { |
| 15562 | let symbol = dec.symbol(cdf); |
| 15563 | if symbol != ANGLE_DELTA_ZERO { |
| 15564 | hit!(ANGLE_DELTA_HITS); |
| 15565 | } |
| 15566 | symbol as i32 - ANGLE_DELTA_ZERO as i32 |
| 15567 | } |
| 15568 | |
| 15569 | #[allow(clippy::too_many_arguments)] |
| 15570 | fn read_intra_mode( |
| 15571 | dec: &mut SymbolDecoder, |
| 15572 | cdfs: &mut Cdfs, |
| 15573 | above_mode: usize, |
| 15574 | left_mode: usize, |
| 15575 | cfl: bool, |
| 15576 | side: usize, |
| 15577 | enable_filter_intra: bool, |
| 15578 | skip_ctx: usize, |
| 15579 | allow_screen_content_tools: bool, |
| 15580 | allow_intrabc: bool, |
| 15581 | // `Some((palette_mode_ctx, color_cache))` at the call sites this lane's |
| 15582 | // scope actually reconstructs a palette-Y block for (`av1_get_palette_mode_ctx` |
| 15583 | // and `av1_get_palette_cache`'s neighbour lookup, [`Neighbours::palette_ctx_and_cache`]); |
| 15584 | // `None` at the excluded call sites (the 8x8-leaf path), which still read |
| 15585 | // the `palette_y_mode` symbol at ctx 0 -- matching the bits a real |
| 15586 | // decoder reads either way -- but refuse by name the moment it fires, |
| 15587 | // exactly the old behaviour. |
| 15588 | palette: Option<(usize, &[u16])>, |
| 15589 | // The U-channel colour cache for a chroma palette read ([`Neighbours::palette_uv_cache`]), |
| 15590 | // `&[]` at the excluded call sites -- same corner-cut as `palette` above, |
| 15591 | // no `mode_ctx` needed (`palette_uv_mode_ctx` is this block's own just-read |
| 15592 | // `use_palette_y`, not a neighbour lookup). |
| 15593 | palette_uv_cache: &[u16], |
| 15594 | mi_r: usize, |
| 15595 | mi_c: usize, fctx: &crate::decode::FrameCtx, |
| 15596 | ) -> Result<( |
| 15597 | bool, |
| 15598 | usize, |
| 15599 | i32, |
| 15600 | usize, |
| 15601 | i32, |
| 15602 | Option<(i32, i32)>, |
| 15603 | Option<usize>, |
| 15604 | Option<PaletteY>, |
| 15605 | Option<PaletteUv>, |
| 15606 | )> { |
| 15607 | let trace = crate::envflags::env_flag!("EC_AV1_TRACE"); |
| 15608 | // lane-tiny r2: EC_ISTEP-format trace (same "name=... val=... rng=..." |
| 15609 | // shape the oracle's own `ec_read_intra_frame_mode_info_impl` prints |
| 15610 | // under `EC_TRACE_MODE_STEP`) so a range ladder can be diffed line for |
| 15611 | // line against the instrumented aomdec, not just `tell()`. |
| 15612 | let ec_istep = crate::envflags::env_flag!("EC_TRACE_MODE_STEP"); |
| 15613 | if ec_istep { |
| 15614 | // Mirrors the oracle's own `EC_IMODE mi_row=.. mi_col=.. rng=..` line |
| 15615 | // (decodemv.c:818), printed at the same point: before the first |
| 15616 | // mode-info symbol of the block, so a cross-decoder range ladder can |
| 15617 | // be keyed on the BLOCK, not just on the symbol sequence. |
| 15618 | eprintln!("EC_IMODE mi_row={mi_r} mi_col={mi_c} fn=sq side={side} rng={}", dec.debug_state().0); |
| 15619 | } |
| 15620 | macro_rules! istep { |
| 15621 | ($name:literal, $val:expr) => { |
| 15622 | if ec_istep { |
| 15623 | let (rng, _) = dec.debug_state(); |
| 15624 | eprintln!( |
| 15625 | "EC_ISTEP mi_row={mi_r} mi_col={mi_c} name={} val={} rng={rng}", |
| 15626 | $name, $val |
| 15627 | ); |
| 15628 | } |
| 15629 | }; |
| 15630 | } |
| 15631 | // lane-sbrect10 r1: `is_cfl_allowed`'s <=32 bound, enforced at the single |
| 15632 | // point every square caller routes through (see `read_intra_mode_rect`). |
| 15633 | let cfl = if cfl && side > 32 { |
| 15634 | hit!(NOCFL_UV_MODE_HITS); |
| 15635 | false |
| 15636 | } else { |
| 15637 | cfl |
| 15638 | }; |
| 15639 | // spec 5.11.6 `intra_frame_mode_info`: `intra_segment_id` is read BEFORE |
| 15640 | // `skip` when `SegIdPreSkip`, and after it otherwise. |
| 15641 | let (seg_w_mi, seg_h_mi) = (side / 4, side / 4); |
| 15642 | if seg_id_pre_skip(fctx) { |
| 15643 | intra_segment_id(dec, cdfs, mi_r, mi_c, seg_w_mi, seg_h_mi, false, fctx); |
| 15644 | } |
| 15645 | let skip = dec.symbol(&mut cdfs.skip[skip_ctx]) != 0; |
| 15646 | if trace { |
| 15647 | eprintln!("TRACE skip value={} rng={}", skip as i32, dec.debug_state().0); |
| 15648 | } |
| 15649 | istep!("skip", skip as i32); |
| 15650 | if !seg_id_pre_skip(fctx) { |
| 15651 | intra_segment_id(dec, cdfs, mi_r, mi_c, seg_w_mi, seg_h_mi, skip, fctx); |
| 15652 | } |
| 15653 | // spec order (see the comment below on `read_intrabc_info`): `skip`, |
| 15654 | // `segment_id`, `cdef`, `delta_q` -- `cdef` lands right here. |
| 15655 | maybe_read_cdef_idx(dec, mi_r, mi_c, (side / MI, side / MI), skip, fctx); |
| 15656 | istep!("cdef", 0); |
| 15657 | // lane-sb128b r1: "this leaf IS the superblock" is `side == sb_size`, not |
| 15658 | // a hardcoded 64 -- a `PARTITION_NONE` 128x128 root is the whole |
| 15659 | // superblock and skips `delta_q` exactly like a 64 one under a 64 root. |
| 15660 | let whole_sb = side == sb_mi_cur(fctx) as usize * 4; |
| 15661 | maybe_read_delta_q(dec, cdfs, mi_r, mi_c, whole_sb, skip, fctx); |
| 15662 | maybe_read_delta_lf(dec, cdfs, mi_r, mi_c, whole_sb, skip, fctx); |
| 15663 | istep!("dq", 0); |
| 15664 | // `read_intrabc_info` (spec 5.11.13, libaom decodemv.c:693, called at |
| 15665 | // :811 right after `skip_txfm`/`segment_id`/`cdef`/`delta_q` and before |
| 15666 | // `mbmi->mode` is ever read): a `use_intrabc` symbol only present on an |
| 15667 | // intra frame whose header set `allow_intrabc`. Actual intra block copy |
| 15668 | // needs `assign_dv`'s own motion-vector-prediction machinery (`av1_find_mv_refs`, |
| 15669 | // `av1_find_ref_dv`) this decoder does not carry -- lane-screen scope |
| 15670 | // draws the line at consuming this one flag symbol and refusing by name |
| 15671 | // when it fires, so the arithmetic decoder still resyncs for a stream |
| 15672 | // that merely allows (but never uses) intrabc. |
| 15673 | if allow_intrabc { |
| 15674 | let use_intrabc = dec.symbol(&mut cdfs.intrabc) != 0; |
| 15675 | if crate::envflags::env_flag!("EC_TRACE_MODE_STEP") { |
| 15676 | eprintln!("EC_ISTEP mi_row={mi_r} mi_col={mi_c} name=intrabc val={} rng={}", u8::from(use_intrabc), dec.debug_state().0); |
| 15677 | } |
| 15678 | if trace { |
| 15679 | eprintln!("TRACE use_intrabc value={}", use_intrabc as i32); |
| 15680 | } |
| 15681 | if use_intrabc { |
| 15682 | let dv = read_intrabc_dv(dec, cdfs, mi_r, mi_c, (side / MI, side / MI), fctx); |
| 15683 | if trace { |
| 15684 | eprintln!("TRACE intrabc dv={dv:?}"); |
| 15685 | } |
| 15686 | fctx.intrabc_dv.with(|c| c.set(Some(dv))); |
| 15687 | hit!(INTRABC_HITS); |
| 15688 | // spec 5.11.13 / libaom `read_intra_frame_mode_info` returns the |
| 15689 | // moment `is_intrabc_block`: no y/uv mode, angle delta, palette, |
| 15690 | // CFL or filter-intra syntax follows. `YMode`/`UVMode` are forced |
| 15691 | // `DC_PRED` (which is also what the neighbour mode contexts and |
| 15692 | // the loop filter then see). |
| 15693 | return Ok((skip, DC_PRED, 0, DC_PRED, 0, None, None, None, None)); |
| 15694 | } |
| 15695 | } |
| 15696 | let above_ctx = INTRA_MODE_CTX[above_mode]; |
| 15697 | let left_ctx = INTRA_MODE_CTX[left_mode]; |
| 15698 | let mode = dec.symbol(&mut cdfs.kf_y_mode[above_ctx][left_ctx]); |
| 15699 | if trace { |
| 15700 | eprintln!("TRACE y_mode ctx=({above_ctx},{left_ctx}) value={mode} rng={}", dec.debug_state().0); |
| 15701 | } |
| 15702 | istep!("mode", mode as i32); |
| 15703 | let angle_delta_y = if (V_PRED..=D67_PRED).contains(&mode) { |
| 15704 | let delta = read_angle_delta(dec, &mut cdfs.angle_delta[mode - V_PRED]); |
| 15705 | if trace { |
| 15706 | eprintln!( |
| 15707 | "TRACE angle_delta value={}", |
| 15708 | delta + ANGLE_DELTA_ZERO as i32 |
| 15709 | ); |
| 15710 | } |
| 15711 | delta |
| 15712 | } else { |
| 15713 | 0 |
| 15714 | }; |
| 15715 | istep!("angle_y", angle_delta_y); |
| 15716 | // lane-av1mono: a monochrome block codes no chroma mode/CFL/palette |
| 15717 | // symbol at all (libaom `decodemv.c:933`'s `!monochrome && is_chroma_ref` |
| 15718 | // guard). Fold `NumPlanes == 1` into the same `has_chroma` this reader |
| 15719 | // gates its chroma symbols on. |
| 15720 | let has_chroma = !mono(fctx); |
| 15721 | let cfl = cfl && has_chroma && cfl_allowed_px(side, side, fctx); |
| 15722 | let uv_mode = if !has_chroma { |
| 15723 | DC_PRED |
| 15724 | } else if cfl { |
| 15725 | dec.symbol(&mut cdfs.uv_mode_cfl[mode]) |
| 15726 | } else { |
| 15727 | dec.symbol(&mut cdfs.uv_mode_no_cfl[mode]) |
| 15728 | }; |
| 15729 | if trace { |
| 15730 | eprintln!("TRACE uv_mode cfl={cfl} y_mode={mode} value={uv_mode} rng={}", dec.debug_state().0); |
| 15731 | } |
| 15732 | istep!("uv_mode", uv_mode as i32); |
| 15733 | let alpha = if cfl && uv_mode == UV_CFL_PRED { |
| 15734 | Some(read_cfl_alphas(dec, cdfs)) |
| 15735 | } else { |
| 15736 | None |
| 15737 | }; |
| 15738 | // `SMOOTH_PRED..PAETH_PRED` (9..=12) chroma is a separate round-2 gap |
| 15739 | // from this lane's directional chroma: a corner block with neither |
| 15740 | // `above` nor `left` (both `Edges::build`'s no-neighbour fallback, |
| 15741 | // 127/129) fed `SMOOTH_PRED` produces the wrong pixel there (worst delta |
| 15742 | // 78 against ffmpeg, traced 2026-08-27) -- an existing, un-lane-touched |
| 15743 | // bug in that fallback this lane's own scope does not cover. Keep it |
| 15744 | // refused here rather than ship a silently-wrong decode. |
| 15745 | if (9..=12).contains(&uv_mode) { |
| 15746 | hit!(SMOOTH_UV_HITS); |
| 15747 | } |
| 15748 | // `get_uv_mode` (spec 9.3): `UV_CFL_PRED` predicts as `DC_PRED` for the |
| 15749 | // angle-delta question -- libaom's own `read_intra_frame_mode_info` reads |
| 15750 | // `angle_delta_uv` off `get_uv_mode(uv_mode)`, never off `uv_mode` raw, |
| 15751 | // so a CFL block (uv_mode==13, outside `V_PRED..=D67_PRED`) already takes |
| 15752 | // the `else` branch below and never reads one either way. |
| 15753 | let angle_delta_uv = if (V_PRED..=D67_PRED).contains(&uv_mode) { |
| 15754 | hit!(DIRECTIONAL_UV_HITS); |
| 15755 | read_angle_delta(dec, &mut cdfs.angle_delta[uv_mode - V_PRED]) |
| 15756 | } else { |
| 15757 | 0 |
| 15758 | }; |
| 15759 | istep!("angle_uv", angle_delta_uv); |
| 15760 | if angle_delta_uv != 0 { |
| 15761 | hit!(UV_ANGLE_DELTA_HITS); |
| 15762 | } |
| 15763 | // `read_palette_mode_info` (spec 5.11.13, libaom decodemv.c:567, called |
| 15764 | // at :840 right after `xd->cfl.store_y` and before `read_filter_intra_mode_info`): |
| 15765 | // gated by `av1_allow_palette` (blockd.h -- size bound only, |
| 15766 | // [`palette_bsize_ctx`]). `palette_mode_ctx` comes from the caller |
| 15767 | // (`palette` param, `None` refuses unconditionally at ctx 0 -- the old |
| 15768 | // behaviour, bit-identical since a hardcoded ctx-0 read and a real one |
| 15769 | // read the same CDF row whenever every neighbour really is non-palette). |
| 15770 | // `palette_uv_mode_ctx` is this block's own just-decided Y palette use, |
| 15771 | // per `read_palette_mode_info`'s own `pmi->palette_size[0] > 0` (not a |
| 15772 | // neighbour lookup at all), so it is exact even at the excluded call |
| 15773 | // sites once Y decodes real syntax there too. |
| 15774 | // |
| 15775 | // lane-palette r7: libaom's `decode_color_map_tokens` is NOT called |
| 15776 | // inline from `read_palette_mode_info` -- it runs later, from |
| 15777 | // `parse_decode_block` (`decodeframe.c:1135`, `av1_visit_palette`), |
| 15778 | // after the WHOLE mode-info read for the block, including UV palette |
| 15779 | // mode_info (`read_palette_mode_info`'s own second half, right below) |
| 15780 | // and `read_filter_intra_mode_info`. So this branch now only reads |
| 15781 | // mode/size/colours for both planes (deferring the two colour-index-map |
| 15782 | // decodes to after `filter_intra`, below) -- r6's range trace pinned |
| 15783 | // this exact ordering bug (`compare-range-not-tell`: ranges matched |
| 15784 | // bit-for-bit through the Y colours, diverged only at the old inline |
| 15785 | // `decode_color_index_map` call). |
| 15786 | let mut palette_y_pending: Option<(usize, [u16; 8])> = None; |
| 15787 | let mut palette_uv_pending: Option<(usize, [u16; 8], [u16; 8])> = None; |
| 15788 | if allow_screen_content_tools |
| 15789 | && let Some(bsize_ctx) = palette_bsize_ctx(side) |
| 15790 | { |
| 15791 | let (mode_ctx, cache) = palette.unwrap_or((0, &[])); |
| 15792 | if mode == DC_PRED { |
| 15793 | if trace { |
| 15794 | let (rng, _) = dec.debug_state(); |
| 15795 | eprintln!( |
| 15796 | "TRACE pre_palette_y_mode bsize_ctx={bsize_ctx} mode_ctx={mode_ctx} rng={rng}" |
| 15797 | ); |
| 15798 | } |
| 15799 | } |
| 15800 | let use_palette_y = |
| 15801 | mode == DC_PRED && dec.symbol(&mut cdfs.palette_y_mode[bsize_ctx][mode_ctx]) != 0; |
| 15802 | if mode == DC_PRED && trace { |
| 15803 | let (rng, _) = dec.debug_state(); |
| 15804 | eprintln!("TRACE palette_y_mode value={} rng={rng}", use_palette_y as i32); |
| 15805 | } |
| 15806 | if use_palette_y { |
| 15807 | let n = 2 + dec.symbol(&mut cdfs.palette_y_size[bsize_ctx]); |
| 15808 | if trace { |
| 15809 | let (rng, _) = dec.debug_state(); |
| 15810 | eprintln!("TRACE palette_y_size n={n} rng={rng}"); |
| 15811 | } |
| 15812 | let colors = read_palette_colors_y(dec, n, cache, fctx); |
| 15813 | if trace { |
| 15814 | let (rng, _) = dec.debug_state(); |
| 15815 | eprintln!("TRACE palette_y_colors colors={:?} rng={rng}", &colors[..n]); |
| 15816 | } |
| 15817 | palette_y_pending = Some((n, colors)); |
| 15818 | } |
| 15819 | // `read_palette_mode_info`'s UV half (decodemv.c:588): read |
| 15820 | // unconditionally at this bit position whenever `uv_mode == |
| 15821 | // UV_DC_PRED`, regardless of whether Y just fired -- this decoder's |
| 15822 | // scope always reconstructs chroma alongside luma (`decode_block` |
| 15823 | // always calls `u.reconstruct`/`v.reconstruct`), so `is_chroma_ref` |
| 15824 | // is always true here and does not need threading in. |
| 15825 | // |
| 15826 | // lane-av1mono: libaom's actual guard is `num_planes > 1 && |
| 15827 | // uv_mode == UV_DC_PRED && xd->is_chroma_ref` (decodemv.c:602), so a |
| 15828 | // monochrome frame reads NO `palette_uv_mode`/`palette_uv_size` |
| 15829 | // symbol even at this chroma-owning square block. |
| 15830 | let palette_uv_mode_ctx = usize::from(use_palette_y); |
| 15831 | if has_chroma |
| 15832 | && uv_mode == DC_PRED |
| 15833 | && dec.symbol(&mut cdfs.palette_uv_mode[palette_uv_mode_ctx]) != 0 |
| 15834 | { |
| 15835 | let n = 2 + dec.symbol(&mut cdfs.palette_uv_size[bsize_ctx]); |
| 15836 | let (u_colors, v_colors) = read_palette_colors_uv(dec, n, palette_uv_cache, fctx); |
| 15837 | palette_uv_pending = Some((n, u_colors, v_colors)); |
| 15838 | } |
| 15839 | } |
| 15840 | // `read_filter_intra_mode_info` (spec 5.11.14, libaom decodemv.c): a |
| 15841 | // `use_filter_intra` symbol this decoder never read at all until |
| 15842 | // lane-av1real r10 -- silently skipping it left every bit past a |
| 15843 | // DC_PRED block at <=32x32 read one symbol short of libaom for the rest |
| 15844 | // of the tile (the pinned CfL stream's actual bug: partition/skip/mode |
| 15845 | // matched bit-exact, then `eob_pt` read 5 instead of 6, because this |
| 15846 | // symbol's bits were never consumed). |
| 15847 | let mut filter_intra = None; |
| 15848 | // lane-tiles r12: `av1_filter_intra_allowed` (blockd.h) also requires |
| 15849 | // `mbmi->palette_mode_info.palette_size[0] == 0` -- libaom writes NO |
| 15850 | // `use_filter_intra` symbol for a Y-palette block, and reading one here |
| 15851 | // desynced every screen-content stream from the first palette block on |
| 15852 | // (it surfaced as a bogus "partition below 8x8" refusal further down the |
| 15853 | // tile). |
| 15854 | if mode == DC_PRED |
| 15855 | && enable_filter_intra |
| 15856 | && palette_y_pending.is_none() |
| 15857 | && let Some(class) = filter_intra_size_class(side) |
| 15858 | { |
| 15859 | let use_filter_intra = dec.symbol(&mut cdfs.filter_intra[class]) != 0; |
| 15860 | if trace { |
| 15861 | eprintln!( |
| 15862 | "TRACE use_filter_intra side={side} rng={} value={}", dec.debug_state().0, |
| 15863 | use_filter_intra as i32 |
| 15864 | ); |
| 15865 | } |
| 15866 | istep!("use_filter_intra", use_filter_intra as i32); |
| 15867 | if use_filter_intra { |
| 15868 | hit!(FILTER_INTRA_HITS); |
| 15869 | let fi_mode = dec.symbol(&mut cdfs.filter_intra_mode); |
| 15870 | if trace { |
| 15871 | eprintln!("TRACE filter_intra_mode value={fi_mode}"); |
| 15872 | } |
| 15873 | istep!("filter_intra_mode", fi_mode as i32); |
| 15874 | filter_intra = Some(fi_mode); |
| 15875 | } |
| 15876 | } |
| 15877 | // `av1_visit_palette` (decoder.c:234): plane 0 (Y) then plane 1 (chroma, |
| 15878 | // shared U/V map) -- the same order libaom's own loop visits them in. |
| 15879 | let palette_y = palette_y_pending.map(|(n, colors)| { |
| 15880 | let map = |
| 15881 | decode_color_index_map(dec, cdfs, n, side, palette_onscreen(mi_r, mi_c, side, side, fctx), false); |
| 15882 | hit!(PALETTE_HITS); |
| 15883 | if trace { |
| 15884 | eprintln!("TRACE palette_y size={n} colors={:?}", &colors[..n]); |
| 15885 | } |
| 15886 | PaletteY { size: n, colors, map } |
| 15887 | }); |
| 15888 | let palette_uv = palette_uv_pending.map(|(n, u_colors, v_colors)| { |
| 15889 | let on = palette_onscreen_uv( |
| 15890 | (side, side), |
| 15891 | palette_onscreen(mi_r, mi_c, side, side, fctx), |
| 15892 | (side >> ss_x(fctx), side >> ss_y(fctx)), |
| 15893 | (ss_x(fctx), ss_y(fctx)), |
| 15894 | ); |
| 15895 | // lane-av1-422d: the chroma map's extent is the PLANE BLOCK |
| 15896 | // `(side >> ss_x) x (side >> ss_y)` -- 4x8 at ss (1,0), not the |
| 15897 | // square call's `(side >> ss_x)`-squared (4x4), which left a |
| 15898 | // 16-entry map for a 32-sample unit and panicked the reconstruct. |
| 15899 | let map = decode_color_index_map_wh( |
| 15900 | dec, cdfs, n, side >> ss_x(fctx), side >> ss_y(fctx), on, true, |
| 15901 | ); |
| 15902 | hit!(PALETTE_UV_HITS); |
| 15903 | if trace { |
| 15904 | eprintln!( |
| 15905 | "TRACE palette_uv size={n} u_colors={:?} v_colors={:?}", |
| 15906 | &u_colors[..n], |
| 15907 | &v_colors[..n] |
| 15908 | ); |
| 15909 | } |
| 15910 | PaletteUv { size: n, u_colors, v_colors, map } |
| 15911 | }); |
| 15912 | Ok(( |
| 15913 | skip, |
| 15914 | mode, |
| 15915 | angle_delta_y, |
| 15916 | uv_mode, |
| 15917 | angle_delta_uv, |
| 15918 | alpha, |
| 15919 | filter_intra, |
| 15920 | palette_y, |
| 15921 | palette_uv, |
| 15922 | )) |
| 15923 | } |
| 15924 | |
| 15925 | /// `UV_CFL_PRED` (spec 6.10.19's chroma intra mode enum): the fourteenth |
| 15926 | /// entry of [`crate::cdf::UV_MODE_CFL`], one past the thirteen ordinary |
| 15927 | /// intra modes. |
| 15928 | const UV_CFL_PRED: usize = 13; |
| 15929 | |
| 15930 | /// `read_cfl_alphas` (libaom decodemv.c): the joint sign symbol (8-ary, spec |
| 15931 | /// 5.11.45's `cfl_alpha_signs`), then each plane's alpha magnitude symbol |
| 15932 | /// (16-ary, `cfl_alpha_u`/`cfl_alpha_v`) where its sign is nonzero -- context |
| 15933 | /// indices `CFL_CONTEXT_U`/`CFL_CONTEXT_V` (libaom cfl.h) derived from the |
| 15934 | /// joint sign the same way. Returns each plane's final signed `alpha_q3` |
| 15935 | /// (`cfl_idx_to_alpha`, cfl.c): magnitude `idx + 1`, sign-zero collapsing to 0. |
| 15936 | fn read_cfl_alphas(dec: &mut SymbolDecoder, cdfs: &mut Cdfs) -> (i32, i32) { |
| 15937 | hit!(CFL_BLOCK_HITS); |
| 15938 | let joint_sign = dec.symbol(&mut cdfs.cfl_sign) as i32; |
| 15939 | let sign_u = ((joint_sign + 1) * 11) >> 5; |
| 15940 | let sign_v = (joint_sign + 1) - 3 * sign_u; |
| 15941 | let alpha_u = if sign_u == 0 { |
| 15942 | 0 |
| 15943 | } else { |
| 15944 | let ctx_u = (joint_sign + 1 - 3) as usize; |
| 15945 | let mag = dec.symbol(&mut cdfs.cfl_alpha[ctx_u]) as i32 + 1; |
| 15946 | if sign_u == 2 { mag } else { -mag } |
| 15947 | }; |
| 15948 | let alpha_v = if sign_v == 0 { |
| 15949 | 0 |
| 15950 | } else { |
| 15951 | let ctx_v = (sign_v * 3 + sign_u - 3) as usize; |
| 15952 | let mag = dec.symbol(&mut cdfs.cfl_alpha[ctx_v]) as i32 + 1; |
| 15953 | if sign_v == 2 { mag } else { -mag } |
| 15954 | }; |
| 15955 | (alpha_u, alpha_v) |
| 15956 | } |
| 15957 | |
| 15958 | /// `cfl_luma_subsampling_420_lbd_c` + `subtract_average_c` (libaom cfl.c): |
| 15959 | /// the reconstructed luma samples under a `side`-square block, subsampled |
| 15960 | /// 4:2:0 to Q3 (`(a+b+c+d) << 1`, each 2x2 group), then block-average |
| 15961 | /// subtracted (`round_offset = num_pel/2`, right-shifted by `log2(num_pel)`) |
| 15962 | /// to give the AC values [`cfl_scaled`] scales by alpha. |
| 15963 | fn cfl_ac_q3(y: &PlaneBuf<'_>, px: usize, py: usize, side: usize) -> Vec<i32> { |
| 15964 | cfl_ac_q3_at(px, py, side, side, |lx, ly| i32::from(y.data[ly * y.width + lx])) |
| 15965 | } |
| 15966 | |
| 15967 | /// The one body [`cfl_ac_q3`] and [`cfl_ac_q3_rect`] share, reading its luma |
| 15968 | /// samples through `sample(x, y)` instead of a [`PlaneBuf`] -- so the encoder |
| 15969 | /// (lane-av1cfl) builds a CfL candidate's AC signal from its own `u8` |
| 15970 | /// reconstruction through THIS function rather than a second transcription of |
| 15971 | /// `cfl_luma_subsampling_420_lbd_c` + `subtract_average_c`. |
| 15972 | /// CfL AC at the frame's subsampling. 4:2:0 keeps the 2x2 average; 4:4:4 |
| 15973 | /// copies each luma sample shifted to Q3 (`cfl_luma_subsampling_444_lbd`). |
| 15974 | fn cfl_ac_ss( |
| 15975 | y: &PlaneBuf<'_>, |
| 15976 | px: usize, |
| 15977 | py: usize, |
| 15978 | bw: usize, |
| 15979 | bh: usize, |
| 15980 | ss_x: usize, |
| 15981 | ss_y: usize, |
| 15982 | ) -> Vec<i32> { |
| 15983 | if ss_x == 0 && ss_y == 0 { |
| 15984 | let mut ac = vec![0i32; bw * bh]; |
| 15985 | let mut sum = 0i32; |
| 15986 | for row in 0..bh { |
| 15987 | for col in 0..bw { |
| 15988 | let q3 = i32::from(y.data[(py + row) * y.width + px + col]) << 3; |
| 15989 | ac[row * bw + col] = q3; |
| 15990 | sum += q3; |
| 15991 | } |
| 15992 | } |
| 15993 | let num_pel = (bw * bh) as i32; |
| 15994 | let avg = (sum + num_pel / 2) >> num_pel.trailing_zeros(); |
| 15995 | ac.iter_mut().for_each(|v| *v -= avg); |
| 15996 | ac |
| 15997 | } else if ss_x == 1 && ss_y == 0 { |
| 15998 | // lane-av1-422: `cfl_luma_subsampling_422_lbd_c` -- each chroma sample |
| 15999 | // averages the HORIZONTAL luma pair above it, at full chroma height |
| 16000 | // (`(a + b) >> 1` in Q3 is `(a + b) << 2`). A rect luma strip's chroma |
| 16001 | // is square here, and the 4:2:0 rect/square helpers below would halve |
| 16002 | // the height it must keep. |
| 16003 | let (cw, ch) = (bw >> 1, bh); |
| 16004 | let mut ac = vec![0i32; cw * ch]; |
| 16005 | let mut sum = 0i32; |
| 16006 | for row in 0..ch { |
| 16007 | for col in 0..cw { |
| 16008 | let (lx, ly) = (px + col * 2, py + row); |
| 16009 | let q3 = (i32::from(y.data[ly * y.width + lx]) |
| 16010 | + i32::from(y.data[ly * y.width + lx + 1])) |
| 16011 | << 2; |
| 16012 | ac[row * cw + col] = q3; |
| 16013 | sum += q3; |
| 16014 | } |
| 16015 | } |
| 16016 | let num_pel = (cw * ch) as i32; |
| 16017 | let avg = (sum + num_pel / 2) >> num_pel.trailing_zeros(); |
| 16018 | ac.iter_mut().for_each(|v| *v -= avg); |
| 16019 | ac |
| 16020 | } else if bw == bh { |
| 16021 | cfl_ac_q3(y, px, py, bw) |
| 16022 | } else { |
| 16023 | cfl_ac_q3_rect(y, px, py, bw, bh) |
| 16024 | } |
| 16025 | } |
| 16026 | |
| 16027 | pub(crate) fn cfl_ac_q3_at( |
| 16028 | px: usize, |
| 16029 | py: usize, |
| 16030 | bw: usize, |
| 16031 | bh: usize, |
| 16032 | sample: impl Fn(usize, usize) -> i32, |
| 16033 | ) -> Vec<i32> { |
| 16034 | let (cw, ch) = (bw / 2, bh / 2); |
| 16035 | let mut ac = vec![0i32; cw * ch]; |
| 16036 | let mut sum = 0i32; |
| 16037 | for cy in 0..ch { |
| 16038 | for cx in 0..cw { |
| 16039 | let (lx, ly) = (px + cx * 2, py + cy * 2); |
| 16040 | let q3 = (sample(lx, ly) |
| 16041 | + sample(lx + 1, ly) |
| 16042 | + sample(lx, ly + 1) |
| 16043 | + sample(lx + 1, ly + 1)) |
| 16044 | << 1; |
| 16045 | ac[cy * cw + cx] = q3; |
| 16046 | sum += q3; |
| 16047 | } |
| 16048 | } |
| 16049 | let num_pel = (cw * ch) as i32; |
| 16050 | let avg = (sum + num_pel / 2) >> num_pel.trailing_zeros(); |
| 16051 | ac.iter_mut().for_each(|v| *v -= avg); |
| 16052 | ac |
| 16053 | } |
| 16054 | |
| 16055 | /// `get_scaled_luma_q0` (libaom cfl.h): `alpha_q3 * ac_q3`, rounded from Q6 |
| 16056 | /// back to whole samples with `ROUND_POWER_OF_TWO_SIGNED` (round-to-nearest, |
| 16057 | /// ties away from zero on the shifted-out sign). |
| 16058 | pub(crate) fn cfl_scaled(alpha_q3: i32, ac_q3: i32) -> i32 { |
| 16059 | let v = alpha_q3 * ac_q3; |
| 16060 | if v >= 0 { |
| 16061 | (v + 32) >> 6 |
| 16062 | } else { |
| 16063 | -((-v + 32) >> 6) |
| 16064 | } |
| 16065 | } |
| 16066 | |
| 16067 | /// One plane's reconstruction buffer, `width * height` samples, and the |
| 16068 | /// prediction/residual reads and writes into it — the decode-side twin of |
| 16069 | /// [`crate::encode`]'s private `Plane`, whose `edges` this mirrors exactly |
| 16070 | /// (own-edge and reach clamps both), passing what it gathers straight to |
| 16071 | /// [`crate::intra::predict`] instead of hand-rolling `DC_PRED` alone. |
| 16072 | /// lane-tiny r4: raw padded-plane dump for filter-stage bisection, written |
| 16073 | /// only when `var` names a path. Same byte shape as `EC_AV1_PREFILT_DUMP`. |
| 16074 | /// lane-hgkf r2: 16-bit cropped stage dump, the high-bitdepth twin of |
| 16075 | /// [`dump_stage`] (whose `as u8` truncation hides |delta| <= 2 defects) -- |
| 16076 | /// matches aomdec's own `ec_dump16` rung byte for byte (crop dims, u16 LE). |
| 16077 | /// lane-mc64 r1: the decode-order index the stage dumps below key their file |
| 16078 | /// name on. `PREFILT_PICTURE_IDX` is bumped once per frame just BEFORE the |
| 16079 | /// deblock/CDEF stages run, so the frame being dumped here is `idx - 1` -- |
| 16080 | /// without this every frame overwrote `.f0` and a per-frame bisection silently |
| 16081 | /// compared the LAST decoded frame (ledger, class stale-output-faked-measurement). |
| 16082 | #[allow(dead_code)] // gate counter accessor with no reader yet; the counter itself is live |
| 16083 | fn stage_dump_idx() -> usize { |
| 16084 | PREFILT_PICTURE_IDX.load(std::sync::atomic::Ordering::SeqCst).saturating_sub(1) |
| 16085 | } |
| 16086 | |
| 16087 | fn dump_stage16(var: &str, y: &PlaneBuf<'_>, u: &PlaneBuf<'_>, v: &PlaneBuf<'_>, fw: usize, fh: usize) { |
| 16088 | use std::io::Write; |
| 16089 | // lane-sbrect10 r2 / lane-cdef r1 (one shared helper): index the dump by |
| 16090 | // decode-order picture like EC_AV1_PREFILT_DUMP already does -- a fixed |
| 16091 | // `.f0` name kept only the LAST frame, so a mid-sequence divergence could |
| 16092 | // not be read out at all. |
| 16093 | if let Ok(path) = crate::envflags::var(var) |
| 16094 | && let Ok(mut f) = std::fs::File::create(format!("{path}.f{}", dump_stage_idx(var))) |
| 16095 | { |
| 16096 | for (p, w, h) in [(y, fw, fh), (u, fw.div_ceil(2), fh.div_ceil(2)), (v, fw.div_ceil(2), fh.div_ceil(2))] { |
| 16097 | for row in 0..h { |
| 16098 | let mut bytes = Vec::with_capacity(w * 2); |
| 16099 | for &s in &p.data[row * p.width..][..w] { |
| 16100 | bytes.extend_from_slice(&s.to_le_bytes()); |
| 16101 | } |
| 16102 | let _ = f.write_all(&bytes); |
| 16103 | } |
| 16104 | } |
| 16105 | } |
| 16106 | } |
| 16107 | |
| 16108 | /// Decode-order index of the next [`dump_stage`]/[`dump_stage16`] file for a |
| 16109 | /// given env var -- lane-cdef r1: both used to write `.f0` unconditionally, so |
| 16110 | /// every frame overwrote the previous one and only the LAST frame survived, |
| 16111 | /// which silently defeats any per-frame filter-stage bisection. |
| 16112 | fn dump_stage_idx(var: &str) -> usize { |
| 16113 | use std::collections::HashMap; |
| 16114 | use std::sync::{Mutex, OnceLock}; |
| 16115 | static IDX: OnceLock<Mutex<HashMap<String, usize>>> = OnceLock::new(); |
| 16116 | let mut m = IDX.get_or_init(|| Mutex::new(HashMap::new())).lock().unwrap(); |
| 16117 | let e = m.entry(var.to_string()).or_insert(0); |
| 16118 | let i = *e; |
| 16119 | *e += 1; |
| 16120 | i |
| 16121 | } |
| 16122 | |
| 16123 | fn dump_stage(var: &str, y: &PlaneBuf<'_>, u: &PlaneBuf<'_>, v: &PlaneBuf<'_>) { |
| 16124 | use std::io::Write; |
| 16125 | if let Ok(path) = crate::envflags::var(var) |
| 16126 | && let Ok(mut f) = std::fs::File::create(format!("{path}.f{}", dump_stage_idx(var))) |
| 16127 | { |
| 16128 | for p in [y, u, v] { |
| 16129 | let narrow: Vec<u8> = p.data.iter().map(|&s| s as u8).collect(); |
| 16130 | let _ = f.write_all(&narrow); |
| 16131 | } |
| 16132 | } |
| 16133 | } |
| 16134 | |
| 16135 | /// The value [`fresh_plane`] fills with under `EC_AV1_PLANE_SENTINEL`: no |
| 16136 | /// legal sample can equal it (reconstruction clamps to `(1 << bit_depth) - 1`, |
| 16137 | /// at most 4095), so a decode that hashes identically under the flag proves |
| 16138 | /// the output does not depend on the plane buffer's initial content. |
| 16139 | const PLANE_SENTINEL: u16 = 0xDEAD; |
| 16140 | |
| 16141 | /// A frame plane's own backing store, `n` samples, UNINITIALISED. |
| 16142 | /// |
| 16143 | /// (lane-picalloc) The SB-padded coding surface is written sample-for-sample |
| 16144 | /// by the tile walk's reconstruction before anything reads it, so the |
| 16145 | /// `vec![0u16; n]` this replaces spent ~3% of the frame thread's cycles |
| 16146 | /// zeroing 25 MB (at 4K) that the very next pass overwrites -- serially, at |
| 16147 | /// the frame's head, on the critical path. `EC_AV1_PLANE_SENTINEL=1` swaps the |
| 16148 | /// uninitialised buffer for a [`PLANE_SENTINEL`] fill; the gate hashes both |
| 16149 | /// ways (see the lane report) and the two agree, which is the proof that no |
| 16150 | /// unwritten sample reaches the output. |
| 16151 | fn fresh_plane(n: usize) -> Vec<u16> { |
| 16152 | if crate::envflags::env_flag!("EC_AV1_PLANE_SENTINEL") { |
| 16153 | return vec![PLANE_SENTINEL; n]; |
| 16154 | } |
| 16155 | let mut v: Vec<u16> = Vec::with_capacity(n); |
| 16156 | // SAFETY: `u16` has no invalid bit patterns and no `Drop`, and the |
| 16157 | // capacity was just reserved, so the vector owns `n` initialised-or-not |
| 16158 | // `u16` slots. Every one is written by reconstruction before it is read |
| 16159 | // (proven by the sentinel gate above). |
| 16160 | #[allow(unsafe_code)] |
| 16161 | unsafe { |
| 16162 | v.set_len(n); |
| 16163 | } |
| 16164 | v |
| 16165 | } |
| 16166 | |
| 16167 | #[derive(Clone)] |
| 16168 | struct PlaneBuf<'a> { |
| 16169 | data: std::borrow::Cow<'a, [u16]>, |
| 16170 | width: usize, |
| 16171 | height: usize, |
| 16172 | /// The frame's true, decodable extent in this plane's own units — past |
| 16173 | /// this, samples are the padded coding surface's invented tail, never a |
| 16174 | /// real decoder's edge or reach reads. |
| 16175 | true_width: usize, |
| 16176 | true_height: usize, |
| 16177 | /// This plane's own tile's top-left pixel origin (spec: intra prediction |
| 16178 | /// never reaches across a tile boundary even though an earlier-decoded |
| 16179 | /// tile's real pixels sit right there in the shared buffer) — set via |
| 16180 | /// [`Self::set_tile_origin`] before each tile's own superblock walk, |
| 16181 | /// `0` for the single-tile case. lane-tiles r2: only the left/top bound |
| 16182 | /// is threaded (the two-tile column-split stage 1 fixture's tile always |
| 16183 | /// ends at the frame's own right/bottom edge); a non-last tile's own |
| 16184 | /// right/bottom reach bound is still open, tracked in the report. |
| 16185 | tile_x0: usize, |
| 16186 | tile_y0: usize, |
| 16187 | /// lane-tiles r5: this plane's own tile's right/bottom pixel bound |
| 16188 | /// (exclusive) -- a non-last tile column/row's own reach must stop at |
| 16189 | /// its own SB-aligned tile boundary. `width`/`height` (this plane's |
| 16190 | /// full padded extent) for the single-tile/last-tile case, matching the |
| 16191 | /// old unclipped behaviour exactly. |
| 16192 | tile_x1: usize, |
| 16193 | tile_y1: usize, |
| 16194 | } |
| 16195 | |
| 16196 | /// One entry per `MV_REFERENCE_FRAME` (index 0/1 unused -- `NONE`/ |
| 16197 | /// `INTRA_FRAME`/`LAST_FRAME` all resolve elsewhere), `Some` when this |
| 16198 | /// frame's own `ref_frame_idx` names a DPB slot that has a picture in it |
| 16199 | /// (lane-av1refs: the same "empty slot" case `GOLDEN_FRAME` already |
| 16200 | /// refused by name, now generic across `LAST2`/`LAST3`/`GOLDEN`/`BWDREF`/ |
| 16201 | /// `ALTREF2`/`ALTREF`). |
| 16202 | |
| 16203 | impl PlaneBuf<'_> { |
| 16204 | /// Sets this plane's own tile pixel origin ([`Self::tile_x0`]/ |
| 16205 | /// [`Self::tile_y0`]) before that tile's own superblock walk. `x0`/`y0` |
| 16206 | /// are already in this plane's own units (luma pixels for `y`, chroma |
| 16207 | /// pixels — halved — for `u`/`v`). |
| 16208 | fn set_tile_origin(&mut self, x0: usize, y0: usize, x1: usize, y1: usize) { |
| 16209 | self.tile_x0 = x0; |
| 16210 | self.tile_y0 = y0; |
| 16211 | self.tile_x1 = x1; |
| 16212 | self.tile_y1 = y1; |
| 16213 | } |
| 16214 | |
| 16215 | fn edges( |
| 16216 | &self, |
| 16217 | x: usize, |
| 16218 | y: usize, |
| 16219 | side: usize, |
| 16220 | reach: Reach, |
| 16221 | ) -> (Option<Vec<u16>>, Option<Vec<u16>>, Option<u16>) { |
| 16222 | let own_across = x + side.min(self.true_width.saturating_sub(x)); |
| 16223 | let across = if reach.above_right { |
| 16224 | own_across + side.min(self.true_width.saturating_sub(own_across)) |
| 16225 | } else { |
| 16226 | own_across |
| 16227 | } |
| 16228 | .min(self.width) |
| 16229 | .min(self.tile_x1); |
| 16230 | let own_down = y + side.min(self.true_height.saturating_sub(y)); |
| 16231 | let down = if reach.below_left { |
| 16232 | own_down + side.min(self.true_height.saturating_sub(own_down)) |
| 16233 | } else { |
| 16234 | own_down |
| 16235 | } |
| 16236 | .min(self.height) |
| 16237 | .min(self.tile_y1); |
| 16238 | let above = (y > self.tile_y0 && across > x) |
| 16239 | .then(|| self.data[(y - 1) * self.width + x..][..across - x].to_vec()); |
| 16240 | let left = (x > self.tile_x0 && down > y).then(|| { |
| 16241 | (y..down) |
| 16242 | .map(|row| self.data[row * self.width + x - 1]) |
| 16243 | .collect::<Vec<_>>() |
| 16244 | }); |
| 16245 | let corner = |
| 16246 | (x > self.tile_x0 && y > self.tile_y0).then(|| self.data[(y - 1) * self.width + x - 1]); |
| 16247 | (above, left, corner) |
| 16248 | } |
| 16249 | |
| 16250 | /// [`Self::edges`] for a true `bw`x`bh` block (lane-intradisp r1): |
| 16251 | /// the above/left reach each extend by their OWN axis (`bw` across, |
| 16252 | /// `bh` down), not one shared `side`. |
| 16253 | fn edges_rect( |
| 16254 | &self, |
| 16255 | x: usize, |
| 16256 | y: usize, |
| 16257 | bw: usize, |
| 16258 | bh: usize, |
| 16259 | reach: Reach, |
| 16260 | ) -> (Option<Vec<u16>>, Option<Vec<u16>>, Option<u16>) { |
| 16261 | let own_across = x + bw.min(self.true_width.saturating_sub(x)); |
| 16262 | let across = if reach.above_right { |
| 16263 | own_across + bw.min(self.true_width.saturating_sub(own_across)) |
| 16264 | } else { |
| 16265 | own_across |
| 16266 | } |
| 16267 | .min(self.width) |
| 16268 | .min(self.tile_x1); |
| 16269 | let own_down = y + bh.min(self.true_height.saturating_sub(y)); |
| 16270 | let down = if reach.below_left { |
| 16271 | own_down + bh.min(self.true_height.saturating_sub(own_down)) |
| 16272 | } else { |
| 16273 | own_down |
| 16274 | } |
| 16275 | .min(self.height) |
| 16276 | .min(self.tile_y1); |
| 16277 | let above = (y > self.tile_y0 && across > x) |
| 16278 | .then(|| self.data[(y - 1) * self.width + x..][..across - x].to_vec()); |
| 16279 | let left = (x > self.tile_x0 && down > y).then(|| { |
| 16280 | (y..down) |
| 16281 | .map(|row| self.data[row * self.width + x - 1]) |
| 16282 | .collect::<Vec<_>>() |
| 16283 | }); |
| 16284 | let corner = |
| 16285 | (x > self.tile_x0 && y > self.tile_y0).then(|| self.data[(y - 1) * self.width + x - 1]); |
| 16286 | if matches!((bw, bh), (16, 8) | (8, 16)) { |
| 16287 | hit!(RECT_STRIP_PRED_HITS); |
| 16288 | if across > own_across || down > own_down { |
| 16289 | hit!(RECT_STRIP_REACH_HITS); |
| 16290 | } |
| 16291 | } |
| 16292 | (above, left, corner) |
| 16293 | } |
| 16294 | |
| 16295 | /// [`Self::reconstruct`] for a true `bw`x`bh` block (lane-intradisp r1), |
| 16296 | /// via [`Self::edges_rect`] and [`crate::intra::predict`]'s own |
| 16297 | /// already-rect-capable `(bw, bh)` pair. |
| 16298 | #[allow(clippy::too_many_arguments)] |
| 16299 | #[track_caller] |
| 16300 | fn reconstruct_rect( |
| 16301 | &mut self, |
| 16302 | x: usize, |
| 16303 | y: usize, |
| 16304 | bw: usize, |
| 16305 | bh: usize, |
| 16306 | mode: usize, |
| 16307 | angle_delta: i32, |
| 16308 | reach: Reach, |
| 16309 | residual: &[i32], |
| 16310 | cfl: Option<(i32, &[i32])>, |
| 16311 | filter_intra: Option<usize>, |
| 16312 | smooth_neighbor: bool, |
| 16313 | // lane-wave1 step (i): the palette/intrabc prediction override, taken |
| 16314 | // off `FrameCtx::palette_pred` by the PUSH site (parse order) rather |
| 16315 | // than by this replay. |
| 16316 | pred: Option<Vec<u16>>, fctx: &crate::decode::FrameCtx, |
| 16317 | ) { |
| 16318 | // As [`Self::reconstruct`]'s own [`PALETTE_PRED`] override |
| 16319 | // (lane-palette2 r4): a palette block's prediction is the |
| 16320 | // reconstructed colour-index map, needing no edge pixels at all. |
| 16321 | let prediction = if let Some(buf) = pred { |
| 16322 | buf |
| 16323 | } else { |
| 16324 | let (above, left, corner) = self.edges_rect(x, y, bw, bh, reach); |
| 16325 | if crate::envflags::env_flag!("EC_DEBUG_EDGES") { |
| 16326 | eprintln!( |
| 16327 | "EDGES x={x} y={y} bw={bw} bh={bh} mode={mode} above={:?} left_len={:?} left0={:?} corner={corner:?} tx0={} ty0={} truew={} trueh={}", |
| 16328 | above.as_ref().map(|a| (a.len(), a[0])), |
| 16329 | left.as_ref().map(|l| l.len()), |
| 16330 | left.as_ref().map(|l| l[0]), |
| 16331 | self.tile_x0, self.tile_y0, self.true_width, self.true_height |
| 16332 | ); |
| 16333 | } |
| 16334 | let mut prediction = vec![0u16; bw * bh]; |
| 16335 | if let Some(fi_mode) = filter_intra { |
| 16336 | crate::intra::predict_filter_intra( |
| 16337 | fi_mode, |
| 16338 | above.as_deref(), |
| 16339 | left.as_deref(), |
| 16340 | corner, |
| 16341 | bw, |
| 16342 | bh, |
| 16343 | &mut prediction, fctx, |
| 16344 | ); |
| 16345 | } else { |
| 16346 | let enable_edge_filter = fctx.enable_edge_filter.with(std::cell::Cell::get); |
| 16347 | predict( |
| 16348 | mode as u8, |
| 16349 | angle_delta, |
| 16350 | above.as_deref(), |
| 16351 | left.as_deref(), |
| 16352 | corner, |
| 16353 | bw, |
| 16354 | bh, |
| 16355 | enable_edge_filter, |
| 16356 | smooth_neighbor, |
| 16357 | &mut prediction, fctx, |
| 16358 | ); |
| 16359 | } |
| 16360 | prediction |
| 16361 | }; |
| 16362 | // lane-hgkf r2: per-TU prediction trace, the twin of aomdec's |
| 16363 | // EC_PRED/EC_PREDND rungs (x/y are this plane's pixel origin). |
| 16364 | if crate::envflags::env_flag!("EC_PRED") { |
| 16365 | let sum: i64 = prediction.iter().map(|&v| i64::from(v)).sum(); |
| 16366 | let row0: Vec<u16> = prediction[..bw.min(8)].to_vec(); |
| 16367 | let col0: Vec<u16> = (0..bh.min(8)).map(|r| prediction[r * bw]).collect(); |
| 16368 | eprintln!( |
| 16369 | "@{} OUR_PRED x={x} y={y} bw={bw} bh={bh} mode={mode} ad={angle_delta} ft={} sum={sum} row0={row0:?} col0={col0:?}", std::panic::Location::caller(), i32::from(smooth_neighbor) |
| 16370 | ); |
| 16371 | } |
| 16372 | let residual = dense_residual(residual, bw * bh); |
| 16373 | if crate::envflags::env_flag!("EC_DBG192") && y == 168 && (x == 224 || x == 232) { |
| 16374 | let res0: Vec<i32> = residual[..bw.min(8)].to_vec(); |
| 16375 | let pred0: Vec<u16> = prediction[..bw.min(8)].to_vec(); |
| 16376 | eprintln!("DBG192 rect x={x} y={y} bw={bw} bh={bh} mode={mode} pred0={pred0:?} res0={res0:?}"); |
| 16377 | } |
| 16378 | for row in 0..bh { |
| 16379 | // Same bottom/right frame-edge clip as [`Self::reconstruct`]'s |
| 16380 | // (lane-sb128c r8) -- a 64x128 block at the right edge of a frame |
| 16381 | // whose padded width is not superblock aligned hangs past it. |
| 16382 | if y + row >= self.height { |
| 16383 | break; |
| 16384 | } |
| 16385 | for col in 0..bw { |
| 16386 | if x + col >= self.width { |
| 16387 | break; |
| 16388 | } |
| 16389 | let idx = row * bw + col; |
| 16390 | let mut base = i32::from(prediction[idx]); |
| 16391 | if let Some((alpha_q3, ac_q3)) = cfl { |
| 16392 | base = (base + cfl_scaled(alpha_q3, ac_q3[idx])).clamp(0, sample_max(fctx)); |
| 16393 | } |
| 16394 | let sample = (base + residual[idx]).clamp(0, sample_max(fctx)) as u16; |
| 16395 | self.data.to_mut()[(y + row) * self.width + x + col] = sample; |
| 16396 | } |
| 16397 | } |
| 16398 | // lane-d792: the RECONSTRUCTION rung beside `EC_PRED`'s prediction one |
| 16399 | // -- a block whose prediction matches the oracle's but whose written |
| 16400 | // samples do not is either a residual defect or a later overwrite, and |
| 16401 | // only this print separates the two. |
| 16402 | if crate::envflags::env_flag!("EC_PRED") { |
| 16403 | let row0: Vec<u16> = |
| 16404 | (0..bw.min(8)).map(|c| self.data[y * self.width + x + c]).collect(); |
| 16405 | let col0: Vec<u16> = |
| 16406 | (0..bh.min(8)).map(|r| self.data[(y + r) * self.width + x]).collect(); |
| 16407 | eprintln!("OUR_RECON x={x} y={y} bw={bw} bh={bh} row0={row0:?} col0={col0:?}"); |
| 16408 | } |
| 16409 | } |
| 16410 | |
| 16411 | /// Predicts under `mode` then adds `residual` (side*side, raster), |
| 16412 | /// writing the clamped reconstruction back into the plane at `(x, y)`. |
| 16413 | /// `cfl`, when `Some((alpha_q3, ac_q3))` (a `UV_CFL_PRED` chroma block), |
| 16414 | /// nudges the prediction by [`cfl_scaled`]'s per-sample amount before the |
| 16415 | /// residual is added — `av1_cfl_predict_block`'s own clip-then-add-residual |
| 16416 | /// order (cfl.c). |
| 16417 | #[track_caller] |
| 16418 | fn reconstruct( |
| 16419 | &mut self, |
| 16420 | x: usize, |
| 16421 | y: usize, |
| 16422 | side: usize, |
| 16423 | mode: usize, |
| 16424 | angle_delta: i32, |
| 16425 | reach: Reach, |
| 16426 | residual: &[i32], |
| 16427 | cfl: Option<(i32, &[i32])>, |
| 16428 | filter_intra: Option<usize>, |
| 16429 | // `smooth_neighbor` (spec `get_intra_edge_filter_type` / |
| 16430 | // libaom `get_filt_type`): true when the block's above OR left |
| 16431 | // neighbour's mode is `SMOOTH_PRED..=SMOOTH_H_PRED` (never `PAETH`). |
| 16432 | // Callers pass their own plane's neighbour-mode check |
| 16433 | // ([`is_smooth_mode`]) — chroma's is still hardcoded `false` by its |
| 16434 | // callers until a smooth/paeth `uv_mode` can reach this call at all |
| 16435 | // (still refused in `read_intra_mode`/`read_intra_mode_rect`), which |
| 16436 | // keeps `false` exact for chroma without this fn needing to know why. |
| 16437 | smooth_neighbor: bool, |
| 16438 | // lane-wave1 step (i): the palette/intrabc prediction override, taken |
| 16439 | // off `FrameCtx::palette_pred` by the PUSH site (parse order) rather |
| 16440 | // than by this replay. |
| 16441 | pred: Option<Vec<u16>>, fctx: &crate::decode::FrameCtx, |
| 16442 | ) { |
| 16443 | // A palette block's own [`PALETTE_PRED`] override (lane-palette r2) |
| 16444 | // takes priority over `predict()`/`predict_filter_intra` entirely -- |
| 16445 | // palette prediction needs no edge pixels at all. |
| 16446 | let prediction = if let Some(buf) = pred { |
| 16447 | buf |
| 16448 | } else { |
| 16449 | let (above, left, corner) = self.edges(x, y, side, reach); |
| 16450 | if crate::envflags::env_flag!("EC_DEBUG_EDGES") { |
| 16451 | eprintln!( |
| 16452 | "EDGES_SQ x={x} y={y} side={side} mode={mode} above={:?} left={:?} corner={corner:?}", |
| 16453 | above.as_deref(), |
| 16454 | left.as_deref(), |
| 16455 | ); |
| 16456 | } |
| 16457 | let mut prediction = vec![0u16; side * side]; |
| 16458 | if let Some(fi_mode) = filter_intra { |
| 16459 | crate::intra::predict_filter_intra( |
| 16460 | fi_mode, |
| 16461 | above.as_deref(), |
| 16462 | left.as_deref(), |
| 16463 | corner, |
| 16464 | side, |
| 16465 | side, |
| 16466 | &mut prediction, fctx, |
| 16467 | ); |
| 16468 | } else { |
| 16469 | // `enable_intra_edge_filter` is the sequence header's own flag |
| 16470 | // (`ENABLE_EDGE_FILTER`, set once per `decode_key_frame_tile_with_cdfs` |
| 16471 | // call, never per-block) -- threading it through every |
| 16472 | // `read_plane`/`decode_block`/`decode_leaf8` call would touch two |
| 16473 | // dozen sites for a value that never changes mid-decode, so it |
| 16474 | // rides a thread-local instead (this decoder is single-threaded |
| 16475 | // per stream). |
| 16476 | let enable_edge_filter = fctx.enable_edge_filter.with(std::cell::Cell::get); |
| 16477 | predict( |
| 16478 | mode as u8, |
| 16479 | angle_delta, |
| 16480 | above.as_deref(), |
| 16481 | left.as_deref(), |
| 16482 | corner, |
| 16483 | side, |
| 16484 | side, |
| 16485 | enable_edge_filter, |
| 16486 | smooth_neighbor, |
| 16487 | &mut prediction, fctx, |
| 16488 | ); |
| 16489 | } |
| 16490 | prediction |
| 16491 | }; |
| 16492 | // lane-hgkf r2: per-TU prediction trace, the twin of aomdec's |
| 16493 | // EC_PRED/EC_PREDND rungs (x/y are this plane's pixel origin). |
| 16494 | if crate::envflags::env_flag!("EC_PRED") { |
| 16495 | let sum: i64 = prediction.iter().map(|&v| i64::from(v)).sum(); |
| 16496 | let row0: Vec<u16> = prediction[..side.min(8)].to_vec(); |
| 16497 | let col0: Vec<u16> = (0..side.min(8)).map(|r| prediction[r * side]).collect(); |
| 16498 | eprintln!( |
| 16499 | "@{} OUR_PRED x={x} y={y} side={side} side={side} mode={mode} ad={angle_delta} ft={} sum={sum} row0={row0:?} col0={col0:?}", std::panic::Location::caller(), i32::from(smooth_neighbor) |
| 16500 | ); |
| 16501 | } |
| 16502 | let residual = dense_residual(residual, side * side); |
| 16503 | if crate::envflags::env_flag!("EC_DBG192") && y == 168 && (x == 224 || x == 232) { |
| 16504 | let res0: Vec<i32> = residual[..side.min(8)].to_vec(); |
| 16505 | let pred0: Vec<u16> = prediction[..side.min(8)].to_vec(); |
| 16506 | eprintln!("DBG192 sq x={x} y={y} side={side} mode={mode} cfl={} pred0={pred0:?} res0={res0:?}", cfl.is_some()); |
| 16507 | } |
| 16508 | for row in 0..side { |
| 16509 | // lane-sb128c r8: libaom's frame buffer is superblock |
| 16510 | // aligned; ours is padded to 32 (`block_grid`), so a 64x64 |
| 16511 | // transform unit of a 128x64 block at the BOTTOM frame edge |
| 16512 | // can hang past the last padded row (1920x792: a unit at |
| 16513 | // y=768 wants 832 rows of an 800-row buffer, which panicked |
| 16514 | // the film's key frames). Those samples are outside the |
| 16515 | // frame: never output, and no block below ever references |
| 16516 | // them. |
| 16517 | if y + row >= self.height { |
| 16518 | break; |
| 16519 | } |
| 16520 | for col in 0..side { |
| 16521 | if x + col >= self.width { |
| 16522 | break; |
| 16523 | } |
| 16524 | let idx = row * side + col; |
| 16525 | let mut base = i32::from(prediction[idx]); |
| 16526 | if let Some((alpha_q3, ac_q3)) = cfl { |
| 16527 | base = (base + cfl_scaled(alpha_q3, ac_q3[idx])).clamp(0, sample_max(fctx)); |
| 16528 | } |
| 16529 | let sample = (base + residual[idx]).clamp(0, sample_max(fctx)) as u16; |
| 16530 | self.data.to_mut()[(y + row) * self.width + x + col] = sample; |
| 16531 | } |
| 16532 | } |
| 16533 | } |
| 16534 | } |
| 16535 | |
| 16536 | /// Reads one plane's transform block, reconstructs it into `plane` at |
| 16537 | /// `(x, y)`/`side`, and records what it leaves behind in `neighbours`. |
| 16538 | #[allow(clippy::too_many_arguments)] |
| 16539 | fn read_plane( |
| 16540 | dec: &mut SymbolDecoder, |
| 16541 | cdfs: &mut Cdfs, |
| 16542 | set: TxbSet, |
| 16543 | scan: &[u16], |
| 16544 | plane_idx: usize, |
| 16545 | around: (bool, bool, i32), |
| 16546 | // The block's own luma mode — [`crate::tile::write_block_planes`] passes |
| 16547 | // it for every plane's `txb` lookup (it is what an intra transform type's |
| 16548 | // CDF is indexed by, spec 9.3), never the plane's own predicted mode. |
| 16549 | tx_mode: usize, |
| 16550 | predict_mode: usize, |
| 16551 | angle_delta: i32, |
| 16552 | reach: Reach, |
| 16553 | // lane-wave1 step (i): unread -- this reader records its write |
| 16554 | // ([`push_intra`]/[`push_mc`]) instead of performing it; the borrow |
| 16555 | // stays in the signature so every call site still proves the plane is |
| 16556 | // exclusively held while its coefficients are parsed. |
| 16557 | _plane: &mut PlaneBuf<'static>, |
| 16558 | x: usize, |
| 16559 | y: usize, |
| 16560 | side: usize, |
| 16561 | tx_side: usize, |
| 16562 | base_q_idx: u8, |
| 16563 | cfl: Option<(i32, CflSrc)>, |
| 16564 | filter_intra: Option<usize>, |
| 16565 | // Only meaningful for `plane_idx == 0`: `Some` when this transform unit is |
| 16566 | // smaller than its own block (spec `get_txb_ctx_general`'s |
| 16567 | // `plane_bsize != tx_size` branch), carrying the already-looked-up |
| 16568 | // `SKIP_CONTEXTS` value; `None` everywhere else (a lone luma TU, or any |
| 16569 | // chroma plane), where `txb_skip_ctx` is 0 or the usual OR-of-neighbours |
| 16570 | // formula below. |
| 16571 | luma_skip_ctx: Option<usize>, |
| 16572 | // See [`PlaneBuf::reconstruct`]'s own doc: caller's neighbour-smooth check. |
| 16573 | smooth_neighbor: bool, fctx: &crate::decode::FrameCtx, |
| 16574 | ) -> Result<Grid> { |
| 16575 | // lane-av1mono: a monochrome frame has `NumPlanes == 1`, so libaom's |
| 16576 | // coefficient loop (`for (plane = 0; plane < av1_num_planes(cm); ++plane)`) |
| 16577 | // never reaches a chroma plane -- no u/v coefficient symbol is coded and |
| 16578 | // no chroma write is recorded. Return the all-zero grid the caller's |
| 16579 | // neighbour bookkeeping reads, without consuming a bit. |
| 16580 | if plane_idx > 0 && mono(fctx) { |
| 16581 | return Ok(Grid::Zero(side * side)); |
| 16582 | } |
| 16583 | let skip_ctx = if plane_idx == 0 { |
| 16584 | luma_skip_ctx.unwrap_or(0) |
| 16585 | } else { |
| 16586 | // libaom `get_txb_ctx`: a chroma unit's context is |
| 16587 | // `combine_entropy_contexts(above, left)` plus 7, or plus 10 when the |
| 16588 | // plane block is bigger than the transform. Our chroma tables hold the |
| 16589 | // offset-7 rows first, so the caller adds 3 for the bigger-block case |
| 16590 | // (only a 128x128 block's TX_32X32 chroma units, lane-sb128b r3). |
| 16591 | usize::from(around.0) + usize::from(around.1) + luma_skip_ctx.unwrap_or(0) |
| 16592 | }; |
| 16593 | // spec 5.11.47/libaom `av1_read_tx_type`: a filter-intra luma block's |
| 16594 | // `intra_ext_tx_cdf` row is indexed by `fimode_to_intradir[filter_intra_mode]`, |
| 16595 | // not by the block's ordinary `mode` (which is always DC_PRED whenever |
| 16596 | // filter-intra is on) -- caller passes the raw filter-intra mode 0..4 in |
| 16597 | // `filter_intra`, only meaningful for `plane_idx == 0` (chroma has no |
| 16598 | // filter-intra in AV1). |
| 16599 | let tx_mode = if plane_idx == 0 { fi_tx_row(tx_mode, filter_intra) } else { tx_mode }; |
| 16600 | let mut coding = cdfs.txb(set, tx_mode); |
| 16601 | // libaom `read_tx_type` (`av1/decoder/decodetxb.c`): "No need to read |
| 16602 | // transform type for lossless mode(qindex==0)" -- the symbol is not coded |
| 16603 | // at all, and `av1_get_tx_type` (`blockd.h`) returns DCT_DCT for every |
| 16604 | // plane of a lossless block (the residual then goes through the WHT, so |
| 16605 | // the type only picks the scan, which is the default one). |
| 16606 | if lossless(fctx) { |
| 16607 | coding.tx_type = None; |
| 16608 | } |
| 16609 | // Luma's `default_tx_type` is only a fallback for the sizes whose |
| 16610 | // `TxbSet` carries no symbol at all (32-point and up), which the spec |
| 16611 | // fixes at `DCT_DCT` regardless of mode; a chroma plane's `tx_type` is |
| 16612 | // *never* its own coded symbol (`coding.tx_type` is always `None` for |
| 16613 | // every `TxbSet::Chroma*`), so it would take `Intra_Mode_To_Tx_Type` of |
| 16614 | // the plane's own predicted mode (`default_intra_tx_type`) -- except |
| 16615 | // `av1_get_ext_tx_set_type` (`blockd.h`) resolves chroma at `tx_size_sqr |
| 16616 | // >= TX_32X32` to `EXT_TX_SET_DCTONLY`, which forces the result back to |
| 16617 | // `DCT_DCT` no matter what the mode-indexed table said (`TxbSet::Chroma8` |
| 16618 | // and `Chroma16` never hit this — every value `default_intra_tx_type` can |
| 16619 | // produce is already a member of both `TX_SET_INTRA_1`/`_2`, so nothing |
| 16620 | // narrows there — only `Chroma32` and up do). |
| 16621 | let default_tx_type = if plane_idx == 0 || side >= 32 { |
| 16622 | TxType::DctDct |
| 16623 | } else if let Some(inherited) = fctx.intrabc_chroma_tx.with(std::cell::Cell::get) { |
| 16624 | // See [`INTRABC_CHROMA_TX`]: an intrabc block is an inter block for |
| 16625 | // `av1_get_tx_type`, so chroma inherits luma's coded type (the |
| 16626 | // `side >= 32` arm above is already libaom's `EXT_TX_SET_DCTONLY` |
| 16627 | // clamp at `tx_size_sqr_up >= TX_32X32`, matching |
| 16628 | // [`read_inter_plane`]'s own `side < 32` guard). |
| 16629 | inherited |
| 16630 | } else { |
| 16631 | default_intra_tx_type(predict_mode as u8) |
| 16632 | }; |
| 16633 | // libaom `av1_get_tx_type` (`blockd.h`): a lossless block's type is |
| 16634 | // DCT_DCT on EVERY plane, so chroma never inherits or mode-derives one. |
| 16635 | let default_tx_type = if lossless(fctx) { TxType::DctDct } else { default_tx_type }; |
| 16636 | let (grid, tx_type) = read_coeffs( |
| 16637 | dec, |
| 16638 | &mut coding, |
| 16639 | scan, |
| 16640 | skip_ctx, |
| 16641 | dc_sign_ctx(around.2), |
| 16642 | default_tx_type, |
| 16643 | None, |
| 16644 | )?; |
| 16645 | note_chroma_class1(plane_idx, tx_type); |
| 16646 | if plane_idx == 0 { |
| 16647 | fctx.luma_tx_type.with(|c| c.set(tx_type)); |
| 16648 | } |
| 16649 | // A 64x64 luma block's transform covers the whole 64x64 area, but only its |
| 16650 | // top-left 32x32 of frequencies are coded (spec 5.11.40); the rest of the |
| 16651 | // dequantized grid stays zero, which `inverse_transform_2d`'s own `< 32` |
| 16652 | // guard also assumes. |
| 16653 | // `grid` is `tx_side x tx_side` (the coded corner); `dequant_and_inverse` |
| 16654 | // wants the true `side x side` transform, with dqDenom (spec 7.12.3) |
| 16655 | // keyed by that true size, not the coded corner's -- so the corner |
| 16656 | // goes into the top-left of a `side`-sized grid, not the reverse. |
| 16657 | let levels = extend_corner(grid, tx_side, tx_side, side, side); |
| 16658 | let (dc_delta, ac_delta) = plane_q_delta(plane_idx, fctx); |
| 16659 | let tx = TxParams { |
| 16660 | w: side, h: side, bit_depth: bit_depth(fctx), q_idx: block_q_idx(fctx), dc_delta, |
| 16661 | ac_delta, tx_type, qm: block_iqmatrix(fctx, plane_idx, side, side, tx_type), stride: 0, |
| 16662 | lossless: lossless(fctx), |
| 16663 | }; |
| 16664 | if crate::envflags::env_flag!("EC_AV1_TRACE") { |
| 16665 | let residual = tx.run(&levels); |
| 16666 | eprintln!( |
| 16667 | "TRACE dequant plane={plane_idx} base_q_idx={base_q_idx} tx_type={tx_type:?} side={side} levels={:?} residual={residual:?}", &*levels, |
| 16668 | ); |
| 16669 | } |
| 16670 | push_intra_tx( |
| 16671 | plane_idx, |
| 16672 | x, |
| 16673 | y, |
| 16674 | side, |
| 16675 | side, |
| 16676 | false, |
| 16677 | predict_mode, |
| 16678 | angle_delta, |
| 16679 | reach, |
| 16680 | tx, |
| 16681 | &levels, |
| 16682 | cfl, |
| 16683 | filter_intra, |
| 16684 | smooth_neighbor, fctx, |
| 16685 | ); |
| 16686 | // `record`'s neighbour state only reads whether *the coded grid* (not the |
| 16687 | // residual) has nonzero entries, so the levels this function already |
| 16688 | // built are what it wants back -- both arms of the old `tx_side == side` |
| 16689 | // branch returned exactly that, one of them through a full `to_vec` copy |
| 16690 | // of the grid per intra transform unit (lane-coefbuf). |
| 16691 | Ok(levels) |
| 16692 | } |
| 16693 | |
| 16694 | /// [`push_intra`] when the unit is square, [`push_intra_rect`] when it is a |
| 16695 | /// rect (lane-av1-422f): a 4:2:2 square-block chroma unit is the plane |
| 16696 | /// block's own rect shape, and the `rect` flag selects |
| 16697 | /// [`PlaneBuf::reconstruct_rect`]'s two-dimensional loop. |
| 16698 | #[allow(clippy::too_many_arguments)] |
| 16699 | fn push_intra_unit( |
| 16700 | plane: usize, |
| 16701 | x: usize, |
| 16702 | y: usize, |
| 16703 | w: usize, |
| 16704 | h: usize, |
| 16705 | mode: usize, |
| 16706 | angle_delta: i32, |
| 16707 | reach: Reach, |
| 16708 | residual: &[i32], |
| 16709 | cfl: Option<(i32, CflSrc)>, |
| 16710 | filter_intra: Option<usize>, |
| 16711 | smooth_neighbor: bool, fctx: &crate::decode::FrameCtx, |
| 16712 | ) { |
| 16713 | if w == h { |
| 16714 | push_intra(plane, x, y, w, mode, angle_delta, reach, residual, cfl, |
| 16715 | filter_intra, smooth_neighbor, fctx); |
| 16716 | } else { |
| 16717 | push_intra_rect(plane, x, y, w, h, mode, angle_delta, reach, residual, cfl, |
| 16718 | filter_intra, smooth_neighbor, fctx); |
| 16719 | } |
| 16720 | } |
| 16721 | |
| 16722 | /// ONE RECT chroma transform unit of a square block's uv plane |
| 16723 | /// (lane-av1-422f) -- a 4:2:2 frame's plane block is `(side/2, side)` and |
| 16724 | /// `av1_get_max_uv_txsize` fills it with a single rect unit below side 64 |
| 16725 | /// (TX_4X8 / TX_8X16 / TX_16X32), which the square-shaped [`read_plane`] |
| 16726 | /// cannot express. Same shape as [`leaf8_chroma422_unit`]'s proven read: the |
| 16727 | /// [`TxbSet`] whose entropy class the unit squares up to, the unit-shaped |
| 16728 | /// scan, `txb_skip_ctx` = above + left in the offset-7 rows (the unit covers |
| 16729 | /// the plane block exactly, so no `+3` bigger-plane term -- that one lives in |
| 16730 | /// the square multi-unit walks' `chroma_ctx_offset`), and the plane's |
| 16731 | /// predicted mode supplies the mode-derived default `tx_type` unless an armed |
| 16732 | /// [`INTRABC_CHROMA_TX`] slot carries the intrabc luma's own -- except at an |
| 16733 | /// entropy class of 32x32 and up, where the spec forces `DCT_DCT` |
| 16734 | /// ([`read_plane`]'s `side >= 32` clamp). |
| 16735 | #[allow(clippy::too_many_arguments)] |
| 16736 | fn read_rect_chroma_unit( |
| 16737 | dec: &mut SymbolDecoder, |
| 16738 | cdfs: &mut Cdfs, |
| 16739 | set: TxbSet, |
| 16740 | scan: &[u16], |
| 16741 | w: usize, |
| 16742 | h: usize, |
| 16743 | plane_idx: usize, |
| 16744 | around: (bool, bool, i32), |
| 16745 | uv_predict_mode: usize, |
| 16746 | angle_delta_uv: i32, |
| 16747 | reach: Reach, |
| 16748 | // Unread, like [`read_plane`]'s own: the borrow proves the caller holds |
| 16749 | // the plane exclusively while the unit's coefficients are parsed. |
| 16750 | _plane: &mut PlaneBuf<'static>, |
| 16751 | x: usize, |
| 16752 | y: usize, |
| 16753 | // Signature parity with [`read_plane`]; the dequant reads its q index |
| 16754 | // off `block_q_idx(fctx)` like every other reader here. |
| 16755 | _base_q_idx: u8, |
| 16756 | cfl: Option<(i32, CflSrc)>, |
| 16757 | smooth_neighbor_uv: bool, fctx: &crate::decode::FrameCtx, |
| 16758 | ) -> Result<Grid> { |
| 16759 | if ss_x(fctx) == 1 && ss_y(fctx) == 0 && w != h { |
| 16760 | hit!(CHROMA422_RECT_HITS); |
| 16761 | } |
| 16762 | let default_tx = if w.max(h) >= 32 { |
| 16763 | TxType::DctDct |
| 16764 | } else { |
| 16765 | fctx |
| 16766 | .intrabc_chroma_tx |
| 16767 | .with(std::cell::Cell::get) |
| 16768 | .unwrap_or_else(|| default_intra_tx_type(uv_predict_mode as u8)) |
| 16769 | }; |
| 16770 | let mut coding = cdfs.txb(set, uv_predict_mode); |
| 16771 | let (levels, tx_type) = read_coeffs_rect( |
| 16772 | dec, &mut coding, scan, w, h, |
| 16773 | usize::from(around.0) + usize::from(around.1), |
| 16774 | dc_sign_ctx(around.2), default_tx, |
| 16775 | )?; |
| 16776 | let residual = dequant_and_inverse_typed_wh( |
| 16777 | &levels, w, h, crate::decode::bit_depth(fctx), block_q_idx(fctx), |
| 16778 | plane_q_delta(plane_idx, fctx).0, plane_q_delta(plane_idx, fctx).1, |
| 16779 | tx_type, block_iqmatrix(fctx, plane_idx, w, h, tx_type), |
| 16780 | ); |
| 16781 | push_intra_rect( |
| 16782 | plane_idx, x, y, w, h, uv_predict_mode, angle_delta_uv, reach, |
| 16783 | &residual, cfl, None, smooth_neighbor_uv, fctx, |
| 16784 | ); |
| 16785 | Ok(levels) |
| 16786 | } |
| 16787 | |
| 16788 | /// Decodes one whole-block coded superblock/quadrant/leaf: its mode, then its |
| 16789 | /// three planes, reconstructing them into `y`/`u`/`v` and updating |
| 16790 | /// `neighbours`. |
| 16791 | #[allow(clippy::too_many_arguments)] |
| 16792 | fn decode_block( |
| 16793 | dec: &mut SymbolDecoder, |
| 16794 | cdfs: &mut Cdfs, |
| 16795 | neighbours: &mut Neighbours, |
| 16796 | at: (usize, usize), |
| 16797 | side: usize, |
| 16798 | luma_set: TxbSet, |
| 16799 | chroma_set: TxbSet, |
| 16800 | luma_tx: usize, |
| 16801 | chroma_tx: usize, |
| 16802 | scans: (&[u16], &[u16]), |
| 16803 | cfl: bool, |
| 16804 | y: &mut PlaneBuf<'static>, |
| 16805 | u: &mut PlaneBuf<'static>, |
| 16806 | v: &mut PlaneBuf<'static>, |
| 16807 | base_q_idx: u8, |
| 16808 | enable_filter_intra: bool, |
| 16809 | allow_screen_content_tools: bool, |
| 16810 | allow_intrabc: bool, |
| 16811 | scan32: &[u16], |
| 16812 | scan16: &[u16], |
| 16813 | scan8: &[u16], |
| 16814 | scan4: &[u16], |
| 16815 | tx_select: bool, |
| 16816 | reduced_tx_set: bool, fctx: &crate::decode::FrameCtx, |
| 16817 | ) -> Result<()> { |
| 16818 | let (r, c) = at; |
| 16819 | let (px, py) = (c * SUB, r * SUB); |
| 16820 | let (palette_ctx, palette_cache) = neighbours.palette_ctx_and_cache(at); |
| 16821 | let palette_uv_cache = neighbours.palette_uv_cache(at); |
| 16822 | let (nb_above_mode, nb_left_mode) = neighbours.modes_above_left(r, c); |
| 16823 | let at_tile_left = c == neighbours.tile_col0_mi / (SUB / MI); |
| 16824 | let (skip, mode, angle_delta_y, uv_mode, angle_delta_uv, alpha, filter_intra, palette_y, palette_uv) = |
| 16825 | read_intra_mode( |
| 16826 | dec, |
| 16827 | cdfs, |
| 16828 | nb_above_mode, |
| 16829 | nb_left_mode, |
| 16830 | cfl, |
| 16831 | side, |
| 16832 | enable_filter_intra, |
| 16833 | neighbours.skip_txfm_ctx(r * (SUB / MI), c * (SUB / MI)), |
| 16834 | allow_screen_content_tools, |
| 16835 | allow_intrabc, |
| 16836 | Some((palette_ctx, &palette_cache)), |
| 16837 | &palette_uv_cache, |
| 16838 | r * (SUB / MI), |
| 16839 | c * (SUB / MI), fctx, |
| 16840 | )?; |
| 16841 | if at_tile_left && palette_y.is_some() { |
| 16842 | hit!(PALETTE_TILE_LEFT_HITS); |
| 16843 | } |
| 16844 | // lane-lossless: a lossless BLOCK's transform is TX_4X4 on every plane |
| 16845 | // (libaom `read_tx_size`, `decodeframe.c`), so the caller's per-size |
| 16846 | // tables/scans are replaced by the 4x4 ones and the block always takes the |
| 16847 | // multi-transform-unit path below. lane-av1mixloss: this rewrite used to |
| 16848 | // sit at the TOP of the function -- before `read_intra_mode`, which is |
| 16849 | // where the block's own `segment_id` is read -- so a mixed frame resolved |
| 16850 | // every block's geometry against the PREVIOUS block's segment (a lossless |
| 16851 | // neighbour forced 4x4 units on a lossy block; a lossy one handed the |
| 16852 | // lossless TxParams an 8x8 grid). libaom's order is the opposite one: |
| 16853 | // `decode_mbmi_block` (segment id included) runs before any tx-size rule. |
| 16854 | // lane-av1mixmerge: the 4:4:4 chroma-size arm from main's copy of this |
| 16855 | // rewrite (which sat above `read_intra_mode`) rides along here -- same |
| 16856 | // per-frame `ss_x`/`ss_y` condition, now resolved against the block's own |
| 16857 | // segment like the lossless arm above it. |
| 16858 | let (luma_set, chroma_set, luma_tx, chroma_tx, chroma_tx_h, scans) = if lossless(fctx) { |
| 16859 | (TxbSet::Luma4, TxbSet::Chroma4, 4, 4, 4, (scan4, scan4)) |
| 16860 | } else if ss_x(fctx) == 0 && ss_y(fctx) == 0 { |
| 16861 | let tx = if side >= 64 { 32 } else { side }; |
| 16862 | let (set, scan): (TxbSet, &[u16]) = match tx { |
| 16863 | 32 => (TxbSet::Chroma32, scan32), |
| 16864 | 16 => (TxbSet::Chroma16, scan16), |
| 16865 | 8 => (TxbSet::Chroma8, scan8), |
| 16866 | _ => (TxbSet::Chroma4, scan4), |
| 16867 | }; |
| 16868 | (luma_set, set, luma_tx, tx, tx, (scans.0, scan)) |
| 16869 | } else if ss_x(fctx) == 1 && ss_y(fctx) == 0 { |
| 16870 | // lane-av1-422f: the uv plane block is the (side/2, side) rect |
| 16871 | // (`ss_size_lookup[..][1][0]`), and `av1_get_max_uv_txsize` is |
| 16872 | // `av1_get_adjusted_tx_size(max_txsize_rect_lookup[plane_bsize])` -- |
| 16873 | // the RECT that fills the plane block, NOT the square transform of |
| 16874 | // the smaller side (the oracle reads ONE TX_8X16 for a 16x16 block's |
| 16875 | // 8x16 plane where the old model read two 8x8 units): 4 -> TX_4X4 |
| 16876 | // (the 2x4 plane rounds up to BLOCK_4X4), 8 -> TX_4X8, 16 -> |
| 16877 | // TX_8X16, 32 -> TX_16X32 (one unit per plane, the whole plane |
| 16878 | // block), 64 -> TX_32X64 adjusts to TX_32X32 and 128 -> TX_64X64 |
| 16879 | // adjusts to TX_32X32 (square units, a 1x2 / 2x4 grid). |
| 16880 | // `chroma_tx` is the unit WIDTH, `chroma_tx_h` its HEIGHT; the |
| 16881 | // chroma walks below read one (chroma_tx, chroma_tx_h) unit per grid |
| 16882 | // cell and key their rect reads on `chroma_tx != chroma_tx_h`. |
| 16883 | let (tx_w, tx_h) = match side { |
| 16884 | 4 => (4, 4), |
| 16885 | 8 => (4, 8), |
| 16886 | 16 => (8, 16), |
| 16887 | 32 => (16, 32), |
| 16888 | // 64 and 128: TX_32X64 / TX_64X64 adjust down to TX_32X32. |
| 16889 | _ => (32, 32), |
| 16890 | }; |
| 16891 | let (set, scan): (TxbSet, &[u16]) = match side { |
| 16892 | 4 => (TxbSet::Chroma4, scan4), |
| 16893 | 8 => (TxbSet::ChromaRect8x4, &SCAN_4X8[..]), |
| 16894 | 16 => (TxbSet::ChromaRect16x8, &SCAN_8X16[..]), |
| 16895 | 32 => (TxbSet::ChromaRect32x16, &SCAN_16X32[..]), |
| 16896 | _ => (TxbSet::Chroma32, scan32), |
| 16897 | }; |
| 16898 | (luma_set, set, luma_tx, tx_w, tx_h, (scans.0, scan)) |
| 16899 | } else { |
| 16900 | (luma_set, chroma_set, luma_tx, chroma_tx, chroma_tx, scans) |
| 16901 | }; |
| 16902 | let intrabc_dv = fctx.intrabc_dv.with(|c| c.take()); |
| 16903 | // `predict` panics outside `DC_PRED..=PAETH_PRED` (0..=12); `UV_CFL_PRED` |
| 16904 | // (13) predicts as `DC_PRED` with the [`cfl_scaled`] nudge carrying the |
| 16905 | // actual chroma-from-luma correlation, same as this decoder always did. |
| 16906 | let uv_predict_mode = if uv_mode == UV_CFL_PRED { |
| 16907 | DC_PRED |
| 16908 | } else { |
| 16909 | uv_mode |
| 16910 | }; |
| 16911 | let smooth_neighbor = is_smooth_mode(nb_above_mode) || is_smooth_mode(nb_left_mode); |
| 16912 | if smooth_neighbor { |
| 16913 | hit!(SMOOTH_LUMA_HITS); |
| 16914 | } |
| 16915 | let smooth_neighbor_uv = |
| 16916 | neighbours.smooth_uv_neighbour(r * (SUB / MI), c * (SUB / MI), r, c, fctx); |
| 16917 | if crate::envflags::env_flag!("EC_AV1_TRACE") { |
| 16918 | eprintln!( |
| 16919 | "TRACE block px={px} py={py} side={side} mode={mode} uv_mode={uv_mode} \ |
| 16920 | angle_delta_uv={angle_delta_uv}" |
| 16921 | ); |
| 16922 | } |
| 16923 | let (mi_r, mi_c) = (r * (SUB / MI), c * (SUB / MI)); |
| 16924 | // `TxMode::Select`'s `tx_depth` (spec 5.11.16): read for every intra |
| 16925 | // block, skipped or not. `logical_tx` is what the loop filter's edge |
| 16926 | // lookup and the next block's own `get_tx_size_context` want (the *nominal* |
| 16927 | // transform size -- 64 even for the 64x64 corner-scanned case, spec |
| 16928 | // 5.11.40); `coeff_tx_side` is the actual coded coefficient grid's side, |
| 16929 | // which only differs from it at that one corner case. |
| 16930 | // lane-t900 r32: spec 5.11.16 `read_block_tx_size` gates the `tx_depth` |
| 16931 | // symbol on `allowSelect = !skip || !is_inter`, and libaom's |
| 16932 | // `is_inter_block` counts an INTRABC block (`blockd.h:373`). So a SKIPPED |
| 16933 | // intrabc block reads NO tx-size symbol at all -- it takes |
| 16934 | // `maxRectTxSize` -- while every ordinary skipped intra block does read |
| 16935 | // one. Reading it anyway consumed a symbol libaom never wrote and desynced |
| 16936 | // the tile from that block on (class `equal-range-means-unread`). |
| 16937 | // lane-t900 r32: and an UNSKIPPED one reads the inter var-tx partition |
| 16938 | // tree instead (`read_var_tx_size`), the same syntax the inter block path |
| 16939 | // has always read -- libaom's `parse_decode_block` dispatches on |
| 16940 | // `is_inter_block`, so both branches of `read_block_tx_size` treat an |
| 16941 | // intrabc block as inter. |
| 16942 | let intrabc_skip_tx = intrabc_dv.is_some() && skip; |
| 16943 | // libaom `parse_decode_block`'s var-tx tree condition carries |
| 16944 | // `!xd->lossless[mbmi->segment_id]` (decodeframe.c:1237): an intrabc |
| 16945 | // block inside a LOSSLESS segment of a mixed frame reads NO |
| 16946 | // `txfm_partition` tree -- it falls to `read_tx_size`, whose first line |
| 16947 | // hands back TX_4X4. Invisible while every lossless frame was also |
| 16948 | // `CodedLossless` (`tx_mode` forced `Only4x4`, tree unreachable), which |
| 16949 | // is why the mixed frame is the first stream to reach it. |
| 16950 | let intrabc_vartx = intrabc_dv.is_some() && tx_select && !skip && !lossless(fctx); |
| 16951 | let intrabc_leaves = if intrabc_vartx { |
| 16952 | let max_tx = side.min(64); |
| 16953 | // `max_block_wide`/`max_block_high`: the frame's own mi grid |
| 16954 | // (`av1_calc_mi_size`, 8-px aligned), so a block hanging over the |
| 16955 | // right/bottom edge reads no split symbol for the units outside it. |
| 16956 | let mi_cols = 2 * y.true_width.div_ceil(8); |
| 16957 | let mi_rows = 2 * y.true_height.div_ceil(8); |
| 16958 | let side_mi = side / MI; |
| 16959 | let max_w_mi = side_mi.min(mi_cols.saturating_sub(mi_c)); |
| 16960 | let max_h_mi = side_mi.min(mi_rows.saturating_sub(mi_r)); |
| 16961 | let mut leaves = Vec::new(); |
| 16962 | for row in (0..side_mi).step_by(max_tx / MI) { |
| 16963 | for col in (0..side_mi).step_by(max_tx / MI) { |
| 16964 | read_var_tx_size( |
| 16965 | dec, cdfs, neighbours, (mi_r, mi_c), side, |
| 16966 | (max_w_mi, max_h_mi), (max_tx, max_tx), 0, row, col, &mut leaves, |
| 16967 | ); |
| 16968 | } |
| 16969 | } |
| 16970 | hit!(INTRABC_VARTX_HITS); |
| 16971 | // lane-t900 r33 census: the t900 recipes resolved every tree UNIFORM. |
| 16972 | // lane-av1-ibcvtx: a tree that resolved to mixed leaf sizes is no |
| 16973 | // longer refused -- the luma reconstruct below walks the tree's own |
| 16974 | // leaf list per leaf (libaom `decode_token_recon_block`, whose token |
| 16975 | // loop reconstructs whatever `read_tx_size_vartx` resolved, per |
| 16976 | // transform unit), so mixed sizes reconstruct at their own stride. |
| 16977 | // The counter marks the shape so a gate can prove a witness actually |
| 16978 | // carries a mixed tree and not merely another uniform one. |
| 16979 | let uniform = leaves |
| 16980 | .first() |
| 16981 | .is_some_and(|f| leaves.iter().all(|l| (l.2, l.3) == (f.2, f.3) && l.2 == l.3)); |
| 16982 | if !uniform { |
| 16983 | hit!(INTRABC_VARTX_MIXED_LEAVES_HITS); |
| 16984 | if crate::envflags::env_flag!("EC_AV1_IBCVTX_DEBUG") { |
| 16985 | eprintln!( |
| 16986 | "IBCVTX mixed intrabc tree mi=({mi_r},{mi_c}) side={side} \ |
| 16987 | dv={intrabc_dv:?} leaves={leaves:?}" |
| 16988 | ); |
| 16989 | } |
| 16990 | } |
| 16991 | Some(leaves) |
| 16992 | } else { |
| 16993 | None |
| 16994 | }; |
| 16995 | let (logical_tx, coeff_tx_side) = if let Some(tx) = intrabc_leaves.as_ref().map(|l| l[0].2) { |
| 16996 | (tx, if tx == 64 { 32 } else { tx }) |
| 16997 | } else if tx_select && !intrabc_skip_tx { |
| 16998 | let resolved = read_tx_size(dec, cdfs, neighbours, (mi_r, mi_c), side, fctx); |
| 16999 | (resolved, if resolved == 64 { 32 } else { resolved }) |
| 17000 | } else { |
| 17001 | // lane-sb128b r1: `max_txsize_rect_lookup[BLOCK_128X128]` is |
| 17002 | // TX_64X64, so a 128x128 block is always FOUR luma transform units |
| 17003 | // even with `tx_select` off -- the TU loop below, never the |
| 17004 | // single-unit path. |
| 17005 | (lossless_tx(side.min(64), fctx), luma_tx) |
| 17006 | }; |
| 17007 | if tx_select && logical_tx < side && uv_mode != DC_PRED { |
| 17008 | hit!(SQ_CHROMA_TX_HITS); |
| 17009 | } |
| 17010 | let luma_set = if tx_select { |
| 17011 | txbset_for(logical_tx, reduced_tx_set) |
| 17012 | } else { |
| 17013 | luma_set |
| 17014 | }; |
| 17015 | let luma_set = if intrabc_dv.is_some() { |
| 17016 | inter_txbset_for(logical_tx, reduced_tx_set) |
| 17017 | } else { |
| 17018 | luma_set |
| 17019 | }; |
| 17020 | let luma_scan = scan_for(coeff_tx_side, scan32, scan16, scan8, scan4); |
| 17021 | let reach = Reach::of(side, px, py, y.width, y.height, fctx); |
| 17022 | let (cpx, cpy) = (px >> ss_x(fctx), py >> ss_y(fctx)); |
| 17023 | let chroma_side = side >> ss_x(fctx); |
| 17024 | // lane-av1-422b: chroma extent per axis, `side` at ss_y 0 (4:2:2/4:4:4), |
| 17025 | // `side / 2` at 4:2:0. |
| 17026 | let chroma_height = side >> ss_y(fctx); |
| 17027 | // spec 7.11.3.4 with `use_intrabc`: the reference is the *current* |
| 17028 | // frame's own pre-loop-filter reconstruction (all loop filters are off |
| 17029 | // whenever `allow_intrabc`, frame.rs:194/210/252/272) at the full-pel |
| 17030 | // block vector, with the BILINEAR kernel libaom forces for intrabc -- |
| 17031 | // luma is a straight copy, chroma lands half-pel whenever the luma DV is |
| 17032 | // odd, which is exactly what the bilinear taps then interpolate. |
| 17033 | flush_recon(fctx, y, u, v); |
| 17034 | let intrabc_bufs = intrabc_dv.map(|(dv_row, dv_col)| { |
| 17035 | let mut yb = vec![0u16; side * side]; |
| 17036 | mc::predict_with_filter( |
| 17037 | &y.data, y.width, y.true_width, y.true_height, |
| 17038 | mv_to_q4(px, dv_col, 0), mv_to_q4(py, dv_row, 0), |
| 17039 | side, side, mc::InterpFilterKind::Bilinear, &mut yb, fctx, |
| 17040 | ); |
| 17041 | let mut ub = vec![0u16; chroma_side * chroma_height]; |
| 17042 | mc::predict_with_filter( |
| 17043 | &u.data, u.width, u.true_width, u.true_height, |
| 17044 | mv_to_q4(cpx, dv_col, ss_x(fctx)), mv_to_q4(cpy, dv_row, ss_y(fctx)), |
| 17045 | chroma_side, chroma_height, mc::InterpFilterKind::Bilinear, &mut ub, fctx, |
| 17046 | ); |
| 17047 | let mut vb = vec![0u16; chroma_side * chroma_height]; |
| 17048 | mc::predict_with_filter( |
| 17049 | &v.data, v.width, v.true_width, v.true_height, |
| 17050 | mv_to_q4(cpx, dv_col, ss_x(fctx)), mv_to_q4(cpy, dv_row, ss_y(fctx)), |
| 17051 | chroma_side, chroma_height, mc::InterpFilterKind::Bilinear, &mut vb, fctx, |
| 17052 | ); |
| 17053 | (yb, ub, vb) |
| 17054 | }); |
| 17055 | // A palette-Y block's own prediction is the reconstructed colour-index |
| 17056 | // map, not `predict()`'s edge-based one -- [`PALETTE_PRED`] carries it |
| 17057 | // to [`PlaneBuf::reconstruct`]'s next call rather than threading a |
| 17058 | // parameter through this function's dozen `y.reconstruct`/`read_plane` |
| 17059 | // sites. A split luma transform (`tx_select && logical_tx != side`) |
| 17060 | // would need the map sliced per-TU -- untested against a real |
| 17061 | // `--enable-palette=1` stream this round, so refuse by name rather than |
| 17062 | // guess at the slicing (charter's own named-acceptable fallback). |
| 17063 | let palette_y_buf: Option<Vec<u16>> = palette_y.as_ref().map(|py| { |
| 17064 | py.map |
| 17065 | .iter() |
| 17066 | .map(|&idx| py.colors[idx as usize]) |
| 17067 | .collect() |
| 17068 | }); |
| 17069 | // lane-t900 r32: the intrabc luma prediction rides the very same |
| 17070 | // whole-block override slot, so the multi-TU loop below windows it per |
| 17071 | // transform unit exactly like a palette map -- setting it once up front |
| 17072 | // handed the whole `side`x`side` buffer to the FIRST unit and left every |
| 17073 | // later one predicting off block edges (the var-tx tree above is what |
| 17074 | // first makes `logical_tx < side` reachable for an intrabc block). |
| 17075 | let palette_y_buf = match &intrabc_bufs { |
| 17076 | Some((yb, _, _)) => Some(yb.clone()), |
| 17077 | None => palette_y_buf, |
| 17078 | }; |
| 17079 | // With one luma transform unit the whole `side`x`side` map is the |
| 17080 | // prediction; with several (`logical_tx != side`) each unit takes its own |
| 17081 | // `logical_tx`-sized window of that same map, set fresh inside the TU loop |
| 17082 | // below (lane-palette2 r7) -- setting the full-block buffer here would be |
| 17083 | // taken by the FIRST unit and indexed at the wrong stride. |
| 17084 | // lane-svt1: a SKIPPED block predicts the whole `side`x`side` in ONE call |
| 17085 | // (the `else` arm below -- a palette/intrabc block never takes the per-unit |
| 17086 | // `split_tx_skip` walk), so its map must be armed here even when the |
| 17087 | // transform is split. Gating the arm on `logical_tx == side` alone left a |
| 17088 | // 16x16 palette block with `tx_depth == 1` and `skip == 1` predicting DC |
| 17089 | // off its own edges: right symbols, wrong pixels (SVT-AV1 preset-8 screen |
| 17090 | // key frame; class override-slot-on-one-arm). Neither prediction is |
| 17091 | // edge-derived, so the whole-block buffer at the block's own stride is |
| 17092 | // exactly what that single call wants. |
| 17093 | // Counted on the SHAPE, not on the arm below, so a gate can tell a stream |
| 17094 | // that reaches this block apart from one that never does (class |
| 17095 | // gate-blind-to-feature). |
| 17096 | if palette_y_buf.is_some() && skip && logical_tx < side { |
| 17097 | hit!(SKIP_SPLIT_TX_OVERRIDE_HITS); |
| 17098 | } |
| 17099 | if let Some(buf) = &palette_y_buf |
| 17100 | && (logical_tx == side || skip) |
| 17101 | { |
| 17102 | set_palette_pred(buf.clone(), fctx); |
| 17103 | } |
| 17104 | // A UV-palette block's own chroma prediction override (lane-palette2 r1), |
| 17105 | // same [`PALETTE_PRED`] slot as Y above -- set fresh right before each |
| 17106 | // plane's own `reconstruct`/`read_plane` call below (never both planes at |
| 17107 | // once), since Y's own set-then-take already emptied the slot by the time |
| 17108 | // either of these run. |
| 17109 | let palette_uv_bufs: Option<(Vec<u16>, Vec<u16>)> = palette_uv.as_ref().map(|puv| { |
| 17110 | ( |
| 17111 | puv.map.iter().map(|&idx| puv.u_colors[idx as usize]).collect(), |
| 17112 | puv.map.iter().map(|&idx| puv.v_colors[idx as usize]).collect(), |
| 17113 | ) |
| 17114 | }); |
| 17115 | // The chroma half of the intrabc prediction rides the same per-plane |
| 17116 | // override slot the UV-palette buffers do (set fresh before each plane's |
| 17117 | // own `reconstruct`/`read_plane` call below). |
| 17118 | let palette_uv_bufs = match &intrabc_bufs { |
| 17119 | Some((_, ub, vb)) => Some((ub.clone(), vb.clone())), |
| 17120 | None => palette_uv_bufs, |
| 17121 | }; |
| 17122 | if skip { |
| 17123 | // A skipped block codes no residual syntax at all (spec 5.11.34): |
| 17124 | // straight prediction, on every plane. |
| 17125 | // |
| 17126 | // lane-hgkf r2: an intra block's `tx_depth` is read whether or not it |
| 17127 | // is skipped (only an INTER skip forces the largest transform), and |
| 17128 | // spec 7.11.2 / libaom `predict_and_reconstruct_intra_block` predicts |
| 17129 | // ONE TRANSFORM UNIT AT A TIME -- each unit's edges are the units |
| 17130 | // already written left/above of it inside this same block. Predicting |
| 17131 | // the whole `side`x`side` at once gives a different (uniformly off by |
| 17132 | // ~1) answer, which is exactly the residue this lane was chasing. |
| 17133 | // The palette/intrabc override path keeps the single-call shape (its |
| 17134 | // per-TU windowing is the non-skip loop's own `palette_y_buf` slice, |
| 17135 | // untested for skip and unreachable in these streams). |
| 17136 | // lane-sb128b r2: a 128x128 block's transform is TX_64X64 even with |
| 17137 | // `tx_select` off (`max_txsize_rect_lookup[BLOCK_128X128]`), so the |
| 17138 | // per-unit prediction is keyed on the transform being smaller than |
| 17139 | // the block, not on the tx-depth syntax. The counter keeps lane-hgkf's |
| 17140 | // narrower meaning (a READ split tx depth). |
| 17141 | let split_tx_skip = |
| 17142 | logical_tx < side && palette_y_buf.is_none() && intrabc_bufs.is_none(); |
| 17143 | if split_tx_skip { |
| 17144 | if tx_select { |
| 17145 | hit!(SKIP_SPLIT_TX_HITS); |
| 17146 | } |
| 17147 | let n_axis = side / logical_tx; |
| 17148 | for tu_row in 0..n_axis { |
| 17149 | for tu_col in 0..n_axis { |
| 17150 | let tu_px = px + tu_col * logical_tx; |
| 17151 | let tu_py = py + tu_row * logical_tx; |
| 17152 | if tu_px >= y.true_width || tu_py >= y.true_height { |
| 17153 | continue; |
| 17154 | } |
| 17155 | let tu_reach = tu_reach( |
| 17156 | side, |
| 17157 | side, |
| 17158 | tu_col * logical_tx, |
| 17159 | tu_row * logical_tx, |
| 17160 | logical_tx, |
| 17161 | reach, |
| 17162 | px, |
| 17163 | py, |
| 17164 | y.width, |
| 17165 | y.height, fctx, |
| 17166 | ); |
| 17167 | push_intra(0, |
| 17168 | tu_px, |
| 17169 | tu_py, |
| 17170 | logical_tx, |
| 17171 | mode, |
| 17172 | angle_delta_y, |
| 17173 | tu_reach, |
| 17174 | &ZERO_RESIDUAL[..logical_tx * logical_tx], |
| 17175 | None, |
| 17176 | filter_intra, |
| 17177 | smooth_neighbor, fctx, |
| 17178 | ); |
| 17179 | } |
| 17180 | } |
| 17181 | } else { |
| 17182 | push_intra(0, |
| 17183 | px, |
| 17184 | py, |
| 17185 | side, |
| 17186 | mode, |
| 17187 | angle_delta_y, |
| 17188 | reach, |
| 17189 | &ZERO_RESIDUAL[..side * side], |
| 17190 | None, |
| 17191 | filter_intra, |
| 17192 | smooth_neighbor, fctx, |
| 17193 | ); |
| 17194 | } |
| 17195 | let ac = alpha.map(|_| cfl_src(px, py, side)); |
| 17196 | // lane-av1-422b: the chroma unit grid is `cn_cols` x `cn_rows` -- |
| 17197 | // square at 4:2:0/4:4:4, at 4:2:2 `(side/2)/tx` units wide by |
| 17198 | // `side/tx` tall (the plane block is (side/2, side)). A unit's LUMA |
| 17199 | // anchor follows the per-axis shifts: `chroma_tx << ss_x` wide, |
| 17200 | // `chroma_tx_h << ss_y` tall (lane-av1-422f: the unit itself is the |
| 17201 | // (chroma_tx, chroma_tx_h) rect, so a 4:2:2 frame below side 64 |
| 17202 | // reads ONE unit per plane, the whole plane block). |
| 17203 | let cn_cols = (chroma_side / chroma_tx).max(1); |
| 17204 | let cn_rows = (chroma_height / chroma_tx_h).max(1); |
| 17205 | let luma_span_x = chroma_tx << ss_x(fctx); |
| 17206 | let luma_span_y = chroma_tx_h << ss_y(fctx); |
| 17207 | for cu_row in 0..cn_rows { |
| 17208 | for cu_col in 0..cn_cols { |
| 17209 | let cu_r = if cn_rows == 1 && cn_cols == 1 { |
| 17210 | reach |
| 17211 | } else { |
| 17212 | tu_reach_rect( |
| 17213 | side, side, |
| 17214 | cu_col * luma_span_x, cu_row * luma_span_y, luma_span_x, luma_span_y, |
| 17215 | reach, px, py, y.width, y.height, fctx, |
| 17216 | ) |
| 17217 | }; |
| 17218 | let (cu_x, cu_y) = (cpx + cu_col * chroma_tx, cpy + cu_row * chroma_tx_h); |
| 17219 | if let Some((ub, _)) = &palette_uv_bufs { |
| 17220 | set_palette_pred(ub.clone(), fctx); |
| 17221 | } |
| 17222 | push_intra_unit(1, |
| 17223 | cu_x, |
| 17224 | cu_y, |
| 17225 | chroma_tx, |
| 17226 | chroma_tx_h, |
| 17227 | uv_predict_mode, |
| 17228 | angle_delta_uv, |
| 17229 | cu_r, |
| 17230 | &ZERO_RESIDUAL[..chroma_tx * chroma_tx_h], |
| 17231 | alpha.zip(ac).map(|((au, _), ac)| (au, ac)), |
| 17232 | None, |
| 17233 | smooth_neighbor_uv, fctx, |
| 17234 | ); |
| 17235 | if let Some((_, vb)) = &palette_uv_bufs { |
| 17236 | set_palette_pred(vb.clone(), fctx); |
| 17237 | } |
| 17238 | push_intra_unit(2, |
| 17239 | cu_x, |
| 17240 | cu_y, |
| 17241 | chroma_tx, |
| 17242 | chroma_tx_h, |
| 17243 | uv_predict_mode, |
| 17244 | angle_delta_uv, |
| 17245 | cu_r, |
| 17246 | &ZERO_RESIDUAL[..chroma_tx * chroma_tx_h], |
| 17247 | alpha.zip(ac).map(|((_, av), ac)| (av, ac)), |
| 17248 | None, |
| 17249 | smooth_neighbor_uv, fctx, |
| 17250 | ); |
| 17251 | } |
| 17252 | } |
| 17253 | let luma_grid = vec![0i32; side * side]; |
| 17254 | let u_grid = vec![0i32; chroma_side * chroma_side]; |
| 17255 | let v_grid = vec![0i32; chroma_side * chroma_side]; |
| 17256 | neighbours.record(at, side, mode, uv_predict_mode, &[&luma_grid[..], &u_grid[..], &v_grid[..]]); |
| 17257 | } else if logical_tx == side { |
| 17258 | // lane-sb128b r1: the test used to be `!tx_select || logical_tx == side`. |
| 17259 | // Without `tx_select` `logical_tx` IS `side` at every size <= 64, so |
| 17260 | // that arm was redundant -- and wrong at 128, where |
| 17261 | // `max_txsize_rect_lookup` caps the block's transform at 64 and the |
| 17262 | // block is four units whether or not `tx_select` is on. |
| 17263 | // A single luma transform unit -- the old path, unchanged (including |
| 17264 | // the 64x64 corner case, `logical_tx == side == 64` with |
| 17265 | // `coeff_tx_side == 32`). |
| 17266 | let around = neighbours.around(at, side); |
| 17267 | let luma_grid = read_plane( |
| 17268 | dec, |
| 17269 | cdfs, |
| 17270 | luma_set, |
| 17271 | luma_scan, |
| 17272 | 0, |
| 17273 | around[0], |
| 17274 | mode, |
| 17275 | mode, |
| 17276 | angle_delta_y, |
| 17277 | reach, |
| 17278 | y, |
| 17279 | px, |
| 17280 | py, |
| 17281 | side, |
| 17282 | coeff_tx_side, |
| 17283 | base_q_idx, |
| 17284 | None, |
| 17285 | filter_intra, |
| 17286 | None, |
| 17287 | smooth_neighbor, fctx, |
| 17288 | )?; |
| 17289 | let ac = alpha.map(|_| cfl_src(px, py, side)); |
| 17290 | // Armed for exactly this block's two chroma reads (see |
| 17291 | // [`INTRABC_CHROMA_TX`]); the luma call above just set |
| 17292 | // [`LUMA_TX_TYPE`] to the type it coded. |
| 17293 | if intrabc_dv.is_some() { |
| 17294 | fctx.intrabc_chroma_tx.with(|c| c.set(Some(fctx.luma_tx_type.with(std::cell::Cell::get)))); |
| 17295 | } |
| 17296 | // lane-av1-444: this arm is one luma TU (`logical_tx == side`), including |
| 17297 | // the 64x64 case whose coefficient grid is 32. At 4:4:4 the chroma plane |
| 17298 | // is the same size as luma and `av1_get_max_uv_txsize` caps it at |
| 17299 | // TX_32X32, so a 64x64 block codes four chroma units, plane-major |
| 17300 | // (every U, then every V) — the same loop the multi-TU arm runs. |
| 17301 | // At 4:2:0 `chroma_side == chroma_tx`, so it stays one read. |
| 17302 | // lane-av1-422b: 4:2:2 never collapses to one read above a 4x4 luma |
| 17303 | // block -- the unit grid is `(side/2)/tx` wide by `side/tx` tall. |
| 17304 | // lane-av1-422f: `chroma_tx` is the unit WIDTH and `chroma_tx_h` its |
| 17305 | // HEIGHT, so below side 64 a 4:2:2 frame's grid is 1x1 and the one |
| 17306 | // unit is the plane block's own rect. |
| 17307 | let cn_cols = (chroma_side / chroma_tx).max(1); |
| 17308 | let cn_rows = (chroma_height / chroma_tx_h).max(1); |
| 17309 | let luma_span_x = chroma_tx << ss_x(fctx); |
| 17310 | let luma_span_y = chroma_tx_h << ss_y(fctx); |
| 17311 | let (u_grid, v_grid, chroma_units) = if cn_rows == 1 && cn_cols == 1 { |
| 17312 | if chroma_tx == chroma_tx_h { |
| 17313 | if let Some((ub, _)) = &palette_uv_bufs { |
| 17314 | set_palette_pred(ub.clone(), fctx); |
| 17315 | } |
| 17316 | let u_grid = read_plane( |
| 17317 | dec, |
| 17318 | cdfs, |
| 17319 | chroma_set, |
| 17320 | scans.1, |
| 17321 | 1, |
| 17322 | around[1], |
| 17323 | mode, |
| 17324 | uv_predict_mode, |
| 17325 | angle_delta_uv, |
| 17326 | reach, |
| 17327 | u, |
| 17328 | cpx, |
| 17329 | cpy, |
| 17330 | chroma_side, |
| 17331 | chroma_tx, |
| 17332 | base_q_idx, |
| 17333 | alpha.zip(ac).map(|((au, _), ac)| (au, ac)), |
| 17334 | None, |
| 17335 | None, |
| 17336 | smooth_neighbor_uv, fctx, |
| 17337 | )?; |
| 17338 | if crate::envflags::env_flag!("EC_AV1_TRACE") { |
| 17339 | eprintln!( |
| 17340 | "TRACE u-write cpx={cpx} cpy={cpy} chroma_side={chroma_side} row0={:?}", |
| 17341 | &u.data[cpy * u.width + cpx..][..chroma_side] |
| 17342 | ); |
| 17343 | } |
| 17344 | if let Some((_, vb)) = &palette_uv_bufs { |
| 17345 | set_palette_pred(vb.clone(), fctx); |
| 17346 | } |
| 17347 | let v_grid = read_plane( |
| 17348 | dec, |
| 17349 | cdfs, |
| 17350 | chroma_set, |
| 17351 | scans.1, |
| 17352 | 2, |
| 17353 | around[2], |
| 17354 | mode, |
| 17355 | uv_predict_mode, |
| 17356 | angle_delta_uv, |
| 17357 | reach, |
| 17358 | v, |
| 17359 | cpx, |
| 17360 | cpy, |
| 17361 | chroma_side, |
| 17362 | chroma_tx, |
| 17363 | base_q_idx, |
| 17364 | alpha.zip(ac).map(|((_, av), ac)| (av, ac)), |
| 17365 | None, |
| 17366 | None, |
| 17367 | smooth_neighbor_uv, fctx, |
| 17368 | )?; |
| 17369 | (u_grid, v_grid, Vec::new()) |
| 17370 | } else { |
| 17371 | // lane-av1-422f: the one unit is a RECT filling the plane |
| 17372 | // block -- the square-shaped [`read_plane`] cannot express |
| 17373 | // it, so both planes go through [`read_rect_chroma_unit`] |
| 17374 | // (U, then V, the same order the square arm reads). The |
| 17375 | // chroma context reads per chroma 4-px cell (see |
| 17376 | // [`Neighbours::around_mi_422_chroma`]). |
| 17377 | let chroma_around = |
| 17378 | neighbours.around_mi_422_chroma((mi_r, mi_c), side, side); |
| 17379 | if let Some((ub, _)) = &palette_uv_bufs { |
| 17380 | set_palette_pred(ub.clone(), fctx); |
| 17381 | } |
| 17382 | let u_grid = read_rect_chroma_unit( |
| 17383 | dec, cdfs, chroma_set, scans.1, chroma_tx, chroma_tx_h, 1, |
| 17384 | chroma_around[1], uv_predict_mode, angle_delta_uv, reach, u, |
| 17385 | cpx, cpy, base_q_idx, |
| 17386 | alpha.zip(ac).map(|((au, _), ac)| (au, ac)), |
| 17387 | smooth_neighbor_uv, fctx, |
| 17388 | )?; |
| 17389 | if let Some((_, vb)) = &palette_uv_bufs { |
| 17390 | set_palette_pred(vb.clone(), fctx); |
| 17391 | } |
| 17392 | let v_grid = read_rect_chroma_unit( |
| 17393 | dec, cdfs, chroma_set, scans.1, chroma_tx, chroma_tx_h, 2, |
| 17394 | chroma_around[2], uv_predict_mode, angle_delta_uv, reach, v, |
| 17395 | cpx, cpy, base_q_idx, |
| 17396 | alpha.zip(ac).map(|((_, av), ac)| (av, ac)), |
| 17397 | smooth_neighbor_uv, fctx, |
| 17398 | )?; |
| 17399 | (u_grid, v_grid, Vec::new()) |
| 17400 | } |
| 17401 | } else { |
| 17402 | let chroma_ctx_offset = Some(3); |
| 17403 | let mut u_pass: Vec<Grid> = Vec::new(); |
| 17404 | let mut units: Vec<((usize, usize), usize, usize, Grid, Grid)> = Vec::new(); |
| 17405 | let mut last_grids = [Grid::Zero(0), Grid::Zero(0)]; |
| 17406 | for plane_pass in 1..=2usize { |
| 17407 | let mut pass_idx = 0usize; |
| 17408 | for cu_row in 0..cn_rows { |
| 17409 | for cu_col in 0..cn_cols { |
| 17410 | let cu_mi = ( |
| 17411 | mi_r + cu_row * (luma_span_y / MI), |
| 17412 | mi_c + cu_col * (luma_span_x / MI), |
| 17413 | ); |
| 17414 | let cu_around = if ss_x(fctx) == 1 && ss_y(fctx) == 0 { |
| 17415 | // lane-av1-422f: chroma-only loop; per-chroma-cell |
| 17416 | // above votes (see [`Neighbours::around_mi_422_chroma`]). |
| 17417 | neighbours.around_mi_422_chroma(cu_mi, luma_span_x, luma_span_y) |
| 17418 | } else { |
| 17419 | neighbours.around_mi_rect(cu_mi, luma_span_x, luma_span_y) |
| 17420 | }; |
| 17421 | let cu_reach = tu_reach_rect( |
| 17422 | side, |
| 17423 | side, |
| 17424 | cu_col * luma_span_x, |
| 17425 | cu_row * luma_span_y, |
| 17426 | luma_span_x, |
| 17427 | luma_span_y, |
| 17428 | reach, |
| 17429 | px, |
| 17430 | py, |
| 17431 | y.width, |
| 17432 | y.height, fctx, |
| 17433 | ); |
| 17434 | let (cu_x, cu_y) = |
| 17435 | (cpx + cu_col * chroma_tx, cpy + cu_row * chroma_tx_h); |
| 17436 | if let Some((ub, vb)) = &palette_uv_bufs { |
| 17437 | let pbuf = if plane_pass == 1 { ub } else { vb }; |
| 17438 | set_palette_pred( |
| 17439 | palette_window( |
| 17440 | pbuf, chroma_side, cu_col * chroma_tx, |
| 17441 | cu_row * chroma_tx_h, chroma_tx, chroma_tx_h, |
| 17442 | ), |
| 17443 | fctx, |
| 17444 | ); |
| 17445 | } |
| 17446 | let buf: &mut PlaneBuf<'static> = |
| 17447 | if plane_pass == 1 { &mut *u } else { &mut *v }; |
| 17448 | let cu_grid = read_plane( |
| 17449 | dec, cdfs, chroma_set, scans.1, plane_pass, cu_around[plane_pass], mode, |
| 17450 | uv_predict_mode, angle_delta_uv, cu_reach, buf, cu_x, cu_y, |
| 17451 | chroma_tx, chroma_tx, base_q_idx, |
| 17452 | alpha.zip(ac).map(|((au, av), ac)| { |
| 17453 | (if plane_pass == 1 { au } else { av }, ac) |
| 17454 | }), |
| 17455 | None, chroma_ctx_offset, smooth_neighbor_uv, fctx, |
| 17456 | )?; |
| 17457 | hit!(CHROMA_SPLIT_TX_HITS); |
| 17458 | if ss_x(fctx) == 1 && ss_y(fctx) == 0 { |
| 17459 | hit!(CHROMA422_SQUARE_HITS); |
| 17460 | } |
| 17461 | // Immediately, so the next unit of this block reads this |
| 17462 | // one's coefficient context. `record` below stamps one |
| 17463 | // chroma state across the whole block; the replay after |
| 17464 | // it puts the per-unit state back. |
| 17465 | neighbours.record_mi_chroma( |
| 17466 | cu_mi, luma_span_x, luma_span_y, plane_pass, &cu_grid, |
| 17467 | ); |
| 17468 | if plane_pass == 1 { |
| 17469 | u_pass.push(cu_grid); |
| 17470 | } else { |
| 17471 | let u_grid = std::mem::replace(&mut u_pass[pass_idx], Grid::Zero(0)); |
| 17472 | units.push((cu_mi, luma_span_x, luma_span_y, u_grid.clone(), cu_grid.clone())); |
| 17473 | last_grids = [u_grid, cu_grid]; |
| 17474 | } |
| 17475 | pass_idx += 1; |
| 17476 | } |
| 17477 | } |
| 17478 | } |
| 17479 | (last_grids[0].clone(), last_grids[1].clone(), units) |
| 17480 | }; |
| 17481 | fctx.intrabc_chroma_tx.with(|c| c.set(None)); |
| 17482 | neighbours.record(at, side, mode, uv_predict_mode, &[&luma_grid[..], &u_grid[..], &v_grid[..]]); |
| 17483 | for (cu_mi, sw, sh, u_unit, v_unit) in &chroma_units { |
| 17484 | neighbours.record_mi_chroma(*cu_mi, *sw, *sh, 1, u_unit); |
| 17485 | neighbours.record_mi_chroma(*cu_mi, *sw, *sh, 2, v_unit); |
| 17486 | } |
| 17487 | } else { |
| 17488 | // Several luma transform units, raster order (spec |
| 17489 | // `transform_block`'s loop): the block's resolved transform is |
| 17490 | // genuinely smaller than its own side, so each tile of it predicts |
| 17491 | // and codes independently, its own context read fresh off whatever |
| 17492 | // the earlier tiles in this same block already wrote -- the same |
| 17493 | // per-TU pattern [`decode_leaf8`] already runs for an 8x8 leaf. |
| 17494 | let n_axis = side / logical_tx; |
| 17495 | // lane-av1-422f: unit width/height per axis, like walk 2 above -- |
| 17496 | // a 4:2:2 frame below side 64 reads ONE rect unit per plane per |
| 17497 | // chunk, the whole plane block. |
| 17498 | let cn_cols = (chroma_side / chroma_tx).max(1); |
| 17499 | let cn_rows = (chroma_height / chroma_tx_h).max(1); |
| 17500 | let luma_span_x = chroma_tx << ss_x(fctx); |
| 17501 | let luma_span_y = chroma_tx_h << ss_y(fctx); |
| 17502 | // libaom's transform-block loop is over 64x64 luma "mu" chunks |
| 17503 | // (`decode_token_recon_block`: `mu_blocks_wide/high` = |
| 17504 | // `mi_size_wide[BLOCK_64X64]`), and it codes EVERY PLANE of a chunk |
| 17505 | // before moving to the next one. Below 128 a block is a single chunk, |
| 17506 | // so this is byte-for-byte the all-luma-then-all-chroma order that has |
| 17507 | // always run; at 128 the four quadrants interleave Y,U,V per quadrant |
| 17508 | // and reading all four luma units first desyncs the tile at the second |
| 17509 | // one (lane-sb128b r3, measured against aomdec's `EC_TRACE_COEFF`). |
| 17510 | let nchunk = (side / 64).max(1); |
| 17511 | // `cn > 1` means the uv plane block is larger than the uv transform, |
| 17512 | // which moves libaom's chroma `txb_skip_ctx` offset from 7 to 10. |
| 17513 | let chroma_ctx_offset = if cn_rows * cn_cols > 1 { Some(3) } else { None }; |
| 17514 | let tpc = (n_axis / nchunk).max(1); |
| 17515 | let cpc_rows = (cn_rows / nchunk).max(1); |
| 17516 | let cpc_cols = (cn_cols / nchunk).max(1); |
| 17517 | // The block's luma transform units, raster order inside each mu |
| 17518 | // chunk. lane-av1-ibcvtx: an intrabc block's units are the var-tx |
| 17519 | // tree's own leaf list -- mixed sizes included, exactly what libaom's |
| 17520 | // `decode_token_recon_block` token loop reconstructs (one |
| 17521 | // `av1_read_coeffs_txb` + inverse transform per resolved unit) -- |
| 17522 | // while every other block's list is the uniform `logical_tx` grid |
| 17523 | // this loop has always walked, generated here in the same order. |
| 17524 | let mut chunk_tus: Vec<Vec<(usize, usize, usize)>> = vec![Vec::new(); nchunk * nchunk]; |
| 17525 | if let Some(leaves) = &intrabc_leaves { |
| 17526 | for &(lr, lc, tw, _th) in leaves { |
| 17527 | let (off_y, off_x) = (lr * MI, lc * MI); |
| 17528 | chunk_tus[(off_y / 64) * nchunk + (off_x / 64)].push((off_y, off_x, tw)); |
| 17529 | } |
| 17530 | } else { |
| 17531 | for tu_row in 0..n_axis { |
| 17532 | for tu_col in 0..n_axis { |
| 17533 | chunk_tus[(tu_row / tpc) * nchunk + (tu_col / tpc)].push(( |
| 17534 | tu_row * logical_tx, |
| 17535 | tu_col * logical_tx, |
| 17536 | logical_tx, |
| 17537 | )); |
| 17538 | } |
| 17539 | } |
| 17540 | } |
| 17541 | let mut last_grids = [Grid::Zero(0), Grid::Zero(0)]; |
| 17542 | let mut units: Vec<((usize, usize), usize, usize, Grid, Grid)> = Vec::new(); |
| 17543 | for ch_row in 0..nchunk { |
| 17544 | for ch_col in 0..nchunk { |
| 17545 | // lane-av1-ibcvtx: the chunk's chroma units inherit the CHROMA tx |
| 17546 | // type of the co-located luma position (`av1_get_tx_type`, |
| 17547 | // blockd.h:1278: a non-Y plane of an inter-classified block -- and |
| 17548 | // intrabc is one -- reads `tx_type_map`, i.e. the coded type of the |
| 17549 | // luma unit covering it). Inside a mu chunk that is the chunk's |
| 17550 | // FIRST luma unit in raster order (the chunk origin is always the |
| 17551 | // first leaf's own origin). Captured right after that unit's read; |
| 17552 | // the single-TU arm below does the same for a whole-block unit. |
| 17553 | let mut first_leaf_tx: Option<TxType> = None; |
| 17554 | for &(tu_off_y, tu_off_x, tu_tx) in &chunk_tus[ch_row * nchunk + ch_col] { |
| 17555 | let tu_mi = (mi_r + tu_off_y / MI, mi_c + tu_off_x / MI); |
| 17556 | let tu_px = px + tu_off_x; |
| 17557 | let tu_py = py + tu_off_y; |
| 17558 | // Same libaom clip as `decode_rect_split`'s loop above; a |
| 17559 | // square block only hangs off the frame when the frame's mi |
| 17560 | // dimensions are odd at the block's own level. |
| 17561 | if tu_px >= y.true_width || tu_py >= y.true_height { |
| 17562 | continue; |
| 17563 | } |
| 17564 | let tu_around = neighbours.around_mi(tu_mi, tu_tx)[0]; |
| 17565 | // A transform unit inside a block is NOT a block at that |
| 17566 | // position (lane-palette2 r12): `Reach::of` granted the |
| 17567 | // top-right 8x8 unit of a 16x16 D203 block bottom-left |
| 17568 | // pixels libaom refuses outright (`col_off > 0`), which is |
| 17569 | // the diagonal wedge seed 46 reconstructed wrong. |
| 17570 | let tu_reach = tu_reach( |
| 17571 | side, |
| 17572 | side, |
| 17573 | tu_off_x, |
| 17574 | tu_off_y, |
| 17575 | tu_tx, |
| 17576 | reach, |
| 17577 | px, |
| 17578 | py, |
| 17579 | y.width, |
| 17580 | y.height, fctx, |
| 17581 | ); |
| 17582 | // This transform unit's own bsize (`tu_tx`) is smaller |
| 17583 | // than the block it sits in (`side`), so `txb_skip_ctx` is |
| 17584 | // the neighbour-magnitude table, not the lone-TU 0 (spec |
| 17585 | // `get_txb_ctx_general`'s `plane_bsize != tx_size` branch). |
| 17586 | let tu_skip_ctx = neighbours.luma_skip_ctx(tu_mi, tu_tx / MI); |
| 17587 | // An intrabc unit's CDF set and scan follow the unit's OWN |
| 17588 | // resolved size (`get_txsize_entropy_ctx` / |
| 17589 | // `av1_get_scan` per `av1_read_coeffs_txb` call); the |
| 17590 | // uniform grid's precomputed pair already matches because |
| 17591 | // `tu_tx == logical_tx` there. |
| 17592 | let (tu_set, tu_scan) = if intrabc_dv.is_some() { |
| 17593 | ( |
| 17594 | inter_txbset_for(tu_tx, reduced_tx_set), |
| 17595 | scan_for(if tu_tx == 64 { 32 } else { tu_tx }, scan32, scan16, scan8, scan4), |
| 17596 | ) |
| 17597 | } else { |
| 17598 | (luma_set, luma_scan) |
| 17599 | }; |
| 17600 | if let Some(buf) = &palette_y_buf { |
| 17601 | hit!(PALETTE_SPLIT_TX_HITS); |
| 17602 | let mut window = vec![0u16; tu_tx * tu_tx]; |
| 17603 | for row in 0..tu_tx { |
| 17604 | let src = (tu_off_y + row) * side + tu_off_x; |
| 17605 | window[row * tu_tx..][..tu_tx] |
| 17606 | .copy_from_slice(&buf[src..][..tu_tx]); |
| 17607 | } |
| 17608 | set_palette_pred(window, fctx); |
| 17609 | } |
| 17610 | let tu_grid = read_plane( |
| 17611 | dec, |
| 17612 | cdfs, |
| 17613 | tu_set, |
| 17614 | tu_scan, |
| 17615 | 0, |
| 17616 | tu_around, |
| 17617 | mode, |
| 17618 | mode, |
| 17619 | angle_delta_y, |
| 17620 | tu_reach, |
| 17621 | y, |
| 17622 | tu_px, |
| 17623 | tu_py, |
| 17624 | tu_tx, |
| 17625 | // The unit's own COEFFICIENT grid, half its side at |
| 17626 | // TX_64X64 (a 64-point transform codes nothing past 32); |
| 17627 | // identical to `tu_tx` at every smaller size |
| 17628 | // (lane-sb128b r1). |
| 17629 | if tu_tx == 64 { 32 } else { tu_tx }, |
| 17630 | base_q_idx, |
| 17631 | None, |
| 17632 | filter_intra, |
| 17633 | Some(tu_skip_ctx), |
| 17634 | smooth_neighbor, fctx, |
| 17635 | )?; |
| 17636 | if crate::envflags::env_flag!("EC_AV1_TRACE") { |
| 17637 | eprintln!( |
| 17638 | "TRACE tu_bitpos mi=({},{}) tell={}", |
| 17639 | tu_mi.0, |
| 17640 | tu_mi.1, |
| 17641 | dec.debug_bitpos() |
| 17642 | ); |
| 17643 | } |
| 17644 | neighbours.record_mi_luma(tu_mi, tu_tx, &tu_grid); |
| 17645 | if first_leaf_tx.is_none() { |
| 17646 | first_leaf_tx = Some(fctx.luma_tx_type.with(std::cell::Cell::get)); |
| 17647 | } |
| 17648 | } |
| 17649 | // CfL is only allowed up to 32x32, i.e. only at `nchunk == 1`, where |
| 17650 | // this still runs after every luma unit of the block. |
| 17651 | let ac = alpha.map(|_| cfl_src(px, py, side)); |
| 17652 | // lane-av1-ibcvtx: an intrabc block's chroma rides the luma's coded |
| 17653 | // tx type (see `first_leaf_tx` above) -- the same arm the single-TU |
| 17654 | // branch sets before its own chroma reads. |
| 17655 | if intrabc_dv.is_some() { |
| 17656 | if let Some(t) = first_leaf_tx { |
| 17657 | fctx.intrabc_chroma_tx.with(|c| c.set(Some(t))); |
| 17658 | } |
| 17659 | } |
| 17660 | // The block's uv plane block is `chroma_side` across but its uv |
| 17661 | // transform is `chroma_tx` (`av1_get_max_uv_txsize` = |
| 17662 | // `av1_get_adjusted_tx_size(max_txsize_rect_lookup[uv_plane_bsize])`): |
| 17663 | // identical at every size up to 64x64, but a 128x128 block's 64x64 uv |
| 17664 | // plane block adjusts TX_64X64 down to TX_32X32, so each plane is |
| 17665 | // FOUR units (lane-sb128b r2) -- one per mu chunk. |
| 17666 | // libaom `decode_token_recon_block` codes a mu chunk PLANE-MAJOR |
| 17667 | // (`for (plane..) for (blk_row..) for (blk_col..)`): every U unit of |
| 17668 | // the chunk, then every V unit. At one unit per plane per chunk -- |
| 17669 | // every non-lossless case this decoder reaches -- that is the same |
| 17670 | // symbol order as the interleaved pair this loop used to read; a |
| 17671 | // lossless frame's 4x4 chroma units are the case where it differs |
| 17672 | // (lane-lossless2). |
| 17673 | let mut u_pass: Vec<Grid> = Vec::new(); |
| 17674 | for plane_pass in 1..=2usize { |
| 17675 | let mut pass_idx = 0usize; |
| 17676 | for cu_row in ch_row * cpc_rows..(ch_row + 1) * cpc_rows { |
| 17677 | for cu_col in ch_col * cpc_cols..(ch_col + 1) * cpc_cols { |
| 17678 | // libaom `decode_reconstruct_tx`'s frame-edge clip |
| 17679 | // (decodeframe.c:299, `blk_row >= max_blocks_high || blk_col |
| 17680 | // >= max_blocks_wide` returns before ANY symbol is read): a |
| 17681 | // transform unit whose 4x4 origin sits outside the frame's mi |
| 17682 | // grid is not coded at all. The luma loop above carries the |
| 17683 | // same clip; its absence here read `txb_skip` (+coeffs) for |
| 17684 | // chroma units libaom never wrote -- the lossless 64x64 block |
| 17685 | // at mi(48,0) of a 320x240 frame overhangs the bottom edge by |
| 17686 | // 4 mi, so its 32x32 chroma codes SIX rows of 4x4 units, not |
| 17687 | // eight (lane-av1-loss64; 48 chroma units per plane in |
| 17688 | // aomdec's EC_COEFF census vs the 64 this loop read). |
| 17689 | // `max_block_wide/high` (av1_common_int.h:1565): the plane |
| 17690 | // block's pixel span (4:2:0 halves the luma span, clamped to |
| 17691 | // 4 by `set_plane_n4`) plus the negative overhang |
| 17692 | // (`mb_to_*_edge` = `2 * overhang_mi` chroma px), in 4x4 |
| 17693 | // units. |
| 17694 | let span_mi = side / MI; |
| 17695 | let over_w_mi = |
| 17696 | (2 * y.true_width.div_ceil(8)) as i32 - mi_c as i32 - span_mi as i32; |
| 17697 | let over_h_mi = |
| 17698 | (2 * y.true_height.div_ceil(8)) as i32 - mi_r as i32 - span_mi as i32; |
| 17699 | let plane_px = ((span_mi * 2).max(4)) as i32; |
| 17700 | let max_units_w = ((plane_px + 2 * over_w_mi.min(0)) >> 2).max(0) as usize; |
| 17701 | // lane-av1-422f: the shared halved formula below the `else` |
| 17702 | // counts the plane span in 4-px units through the 2-px-per-mi |
| 17703 | // subsampled height, which a 4:2:2 plane does not have (ss_y |
| 17704 | // 0) -- its span is the full luma mi count, so a keyed |
| 17705 | // formula clips the same units libaom's `max_block_high` |
| 17706 | // does. 4:2:0 and 4:4:4 keep the proven shared rows. |
| 17707 | let max_units_h = if ss_x(fctx) == 1 && ss_y(fctx) == 0 { |
| 17708 | (span_mi as i32 + over_h_mi.min(0)).max(0) as usize |
| 17709 | } else { |
| 17710 | ((plane_px + 2 * over_h_mi.min(0)) >> 2).max(0) as usize |
| 17711 | }; |
| 17712 | let (unit_w, unit_h) = (chroma_tx / 4, chroma_tx_h / 4); |
| 17713 | if cu_col * unit_w >= max_units_w || cu_row * unit_h >= max_units_h { |
| 17714 | hit!(CHROMA_EDGE_TU_CLIP_HITS); |
| 17715 | continue; |
| 17716 | } |
| 17717 | // The chroma context arrays are indexed in LUMA mi, so this |
| 17718 | // unit's anchor is its own luma quadrant (lane-av1-422b: the |
| 17719 | // quadrant spans `chroma_tx << ss_x` luma px wide by |
| 17720 | // `chroma_tx_h << ss_y` tall -- at 4:2:2 below side 64 the |
| 17721 | // one unit's quadrant is the whole block). |
| 17722 | let cu_mi = ( |
| 17723 | mi_r + cu_row * (luma_span_y / MI), |
| 17724 | mi_c + cu_col * (luma_span_x / MI), |
| 17725 | ); |
| 17726 | let cu_around = if ss_x(fctx) == 1 && ss_y(fctx) == 0 { |
| 17727 | // lane-av1-422f: chroma-only loop; per-chroma-cell above |
| 17728 | // votes (see [`Neighbours::around_mi_422_chroma`]). |
| 17729 | neighbours.around_mi_422_chroma(cu_mi, luma_span_x, luma_span_y) |
| 17730 | } else { |
| 17731 | neighbours.around_mi_rect(cu_mi, luma_span_x, luma_span_y) |
| 17732 | }; |
| 17733 | let cu_reach = if cn_rows == 1 && cn_cols == 1 { |
| 17734 | reach |
| 17735 | } else { |
| 17736 | tu_reach_rect( |
| 17737 | side, side, |
| 17738 | cu_col * luma_span_x, cu_row * luma_span_y, luma_span_x, luma_span_y, |
| 17739 | reach, px, py, y.width, y.height, fctx, |
| 17740 | ) |
| 17741 | }; |
| 17742 | let (cu_x, cu_y) = |
| 17743 | (cpx + cu_col * chroma_tx, cpy + cu_row * chroma_tx_h); |
| 17744 | if let Some((ub, vb)) = &palette_uv_bufs { |
| 17745 | let pbuf = if plane_pass == 1 { ub } else { vb }; |
| 17746 | set_palette_pred( |
| 17747 | palette_window( |
| 17748 | pbuf, chroma_side, cu_col * chroma_tx, |
| 17749 | cu_row * chroma_tx_h, chroma_tx, chroma_tx_h, |
| 17750 | ), |
| 17751 | fctx, |
| 17752 | ); |
| 17753 | } |
| 17754 | let buf: &mut PlaneBuf<'static> = |
| 17755 | if plane_pass == 1 { &mut *u } else { &mut *v }; |
| 17756 | let cu_grid = if chroma_tx == chroma_tx_h { |
| 17757 | read_plane( |
| 17758 | dec, cdfs, chroma_set, scans.1, plane_pass, cu_around[plane_pass], mode, |
| 17759 | uv_predict_mode, angle_delta_uv, cu_reach, buf, cu_x, cu_y, |
| 17760 | chroma_tx, chroma_tx, base_q_idx, |
| 17761 | alpha.zip(ac).map(|((au, av), ac)| { |
| 17762 | (if plane_pass == 1 { au } else { av }, ac) |
| 17763 | }), |
| 17764 | None, chroma_ctx_offset, smooth_neighbor_uv, fctx, |
| 17765 | )? |
| 17766 | } else { |
| 17767 | // lane-av1-422f: the whole plane block as one rect unit; |
| 17768 | // `chroma_ctx_offset` is `None` there (the unit covers |
| 17769 | // the plane block, so the offset-7 skip rows are right). |
| 17770 | read_rect_chroma_unit( |
| 17771 | dec, cdfs, chroma_set, scans.1, chroma_tx, chroma_tx_h, plane_pass, |
| 17772 | cu_around[plane_pass], uv_predict_mode, angle_delta_uv, cu_reach, buf, |
| 17773 | cu_x, cu_y, base_q_idx, |
| 17774 | alpha.zip(ac).map(|((au, av), ac)| { |
| 17775 | (if plane_pass == 1 { au } else { av }, ac) |
| 17776 | }), |
| 17777 | smooth_neighbor_uv, fctx, |
| 17778 | )? |
| 17779 | }; |
| 17780 | if cn_rows * cn_cols > 1 { |
| 17781 | hit!(CHROMA_SPLIT_TX_HITS); |
| 17782 | if ss_x(fctx) == 1 && ss_y(fctx) == 0 { |
| 17783 | hit!(CHROMA422_SQUARE_HITS); |
| 17784 | } |
| 17785 | // Immediately, so the NEXT unit of this same block reads |
| 17786 | // this one's coefficient context (libaom's chroma |
| 17787 | // `txb_skip_ctx` for the second quadrant is 11, i.e. base |
| 17788 | // 1 from this left neighbour -- measured against aomdec). |
| 17789 | // The same writes are replayed after `record_split_luma` |
| 17790 | // below, which would otherwise clobber them whole-block. |
| 17791 | neighbours.record_mi_chroma( |
| 17792 | cu_mi, luma_span_x, luma_span_y, plane_pass, &cu_grid, |
| 17793 | ); |
| 17794 | } |
| 17795 | if plane_pass == 1 { |
| 17796 | u_pass.push(cu_grid); |
| 17797 | } else { |
| 17798 | let u_grid = std::mem::replace(&mut u_pass[pass_idx], Grid::Zero(0)); |
| 17799 | if cn_rows * cn_cols > 1 { |
| 17800 | units.push((cu_mi, luma_span_x, luma_span_y, u_grid.clone(), cu_grid.clone())); |
| 17801 | } |
| 17802 | last_grids = [u_grid, cu_grid]; |
| 17803 | } |
| 17804 | pass_idx += 1; |
| 17805 | } |
| 17806 | } |
| 17807 | } |
| 17808 | } |
| 17809 | if intrabc_dv.is_some() { |
| 17810 | fctx.intrabc_chroma_tx.with(|c| c.set(None)); |
| 17811 | } |
| 17812 | } |
| 17813 | // Mode/side bookkeeping plus, at `cn == 1`, the whole-plane chroma |
| 17814 | // context write; at `cn > 1` the per-unit writes above are what the |
| 17815 | // next block reads, so they run after this (last writer wins). |
| 17816 | neighbours.record_split_luma(at, side, mode, uv_predict_mode, [&last_grids[0], &last_grids[1]]); |
| 17817 | for (cu_mi, sw, sh, u_grid, v_grid) in &units { |
| 17818 | neighbours.record_mi_chroma(*cu_mi, *sw, *sh, 1, u_grid); |
| 17819 | neighbours.record_mi_chroma(*cu_mi, *sw, *sh, 2, v_grid); |
| 17820 | } |
| 17821 | } |
| 17822 | // Unconditional, `size == 0` for a non-palette block: clears any stale |
| 17823 | // neighbour palette state the same way [`Neighbours::record`] always |
| 17824 | // overwrites `above_mode`/`left_mode` regardless of this block's own mode |
| 17825 | // (the split-luma-TU branch above always refuses before reaching here |
| 17826 | // when `palette_y` is `Some`, so it is never live for that branch). |
| 17827 | let (py_size, py_colors) = palette_y |
| 17828 | .as_ref() |
| 17829 | .map_or((0, [0u16; 8]), |p| (p.size, p.colors)); |
| 17830 | neighbours.record_palette_y(at, side, py_size, py_colors); |
| 17831 | let (puv_size, puv_colors) = palette_uv |
| 17832 | .as_ref() |
| 17833 | .map_or((0, [0u16; 8]), |p| (p.size, p.u_colors)); |
| 17834 | neighbours.record_palette_uv(at, side, puv_size, puv_colors); |
| 17835 | neighbours.fill_skip_grid((r * (SUB / MI), c * (SUB / MI)), side / MI, skip); |
| 17836 | neighbours.fill_lf_grid( |
| 17837 | (r * (SUB / MI), c * (SUB / MI)), |
| 17838 | side / MI, |
| 17839 | logical_tx as u8, |
| 17840 | 0, fctx, |
| 17841 | ); |
| 17842 | // lane-t900 r32 / lane-av1txbands: libaom runs `set_txfm_ctxs(mbmi->tx_size, |
| 17843 | // n4_w, n4_h, skip_txfm && is_inter_block(mbmi), xd)` at the end of EVERY |
| 17844 | // `decode_block`. An intra-only frame's blocks also publish their TRANSFORM |
| 17845 | // size from [`Neighbours::fill_lf_grid_rect`], but this grid cannot express |
| 17846 | // libaom's `skip && is_inter` term -- the one thing that makes a SKIPPED |
| 17847 | // intrabc block publish its BLOCK size instead, intrabc being the only |
| 17848 | // "inter" block an intra-only frame codes (`blockd.h:373`). So this call |
| 17849 | // stays the authority wherever one can appear; with `allow_intrabc` unset |
| 17850 | // it writes exactly what the grid already did. |
| 17851 | if allow_intrabc { |
| 17852 | set_txfm_ctxs( |
| 17853 | neighbours, |
| 17854 | (mi_r, mi_c), |
| 17855 | logical_tx, |
| 17856 | side / MI, |
| 17857 | side / MI, |
| 17858 | skip && intrabc_dv.is_some(), |
| 17859 | ); |
| 17860 | } |
| 17861 | record_intrabc_mi(mi_r, mi_c, side / MI, intrabc_dv, neighbours, fctx); |
| 17862 | Ok(()) |
| 17863 | } |
| 17864 | |
| 17865 | thread_local! { |
| 17866 | /// lane-sb128c r8: INTRA (key-frame) 128x64 blocks decoded at the 128 |
| 17867 | /// root's HORZ arm, and 64x128 ones at its VERT arm. |
| 17868 | pub(crate) static INTRA_SB128_HORZ_HITS: std::cell::Cell<usize> = const { std::cell::Cell::new(0) }; |
| 17869 | pub(crate) static INTRA_SB128_VERT_HITS: std::cell::Cell<usize> = const { std::cell::Cell::new(0) }; |
| 17870 | /// Those blocks by resolved intra `tx_depth` (0 = TX_64X64 units, |
| 17871 | /// 1 = TX_32X32, 2 = TX_16X16) -- proof the depth symbol is really read. |
| 17872 | pub(crate) static INTRA_SB128_DEPTH_HITS: std::cell::Cell<[usize; 3]> = const { std::cell::Cell::new([0; 3]) }; |
| 17873 | /// lane-inter128intra r1: the same shapes decoded on the INTER block path |
| 17874 | /// (an intra-coded PARTITION_HORZ/VERT half of a 128 superblock inside an |
| 17875 | /// inter frame) -- (128x64, 64x128). |
| 17876 | pub(crate) static INTRA128_IN_INTER_HITS: std::cell::Cell<[usize; 2]> = const { std::cell::Cell::new([0; 2]) }; |
| 17877 | /// lane-t900 r17: skipped INTER blocks whose `read_block_tx_size` still |
| 17878 | /// handed back var-tx leaves, so the per-transform-unit luma publication |
| 17879 | /// loop never ran and the block has to clear its own luma coefficient |
| 17880 | /// contexts (libaom `av1_reset_entropy_context`). Every 128 root under |
| 17881 | /// `TX_MODE_LARGEST` is one; the counter is what keeps that reset's gate |
| 17882 | /// non-vacuous. |
| 17883 | pub(crate) static SKIP_VARTX_LUMA_RESET_HITS: std::cell::Cell<usize> = const { std::cell::Cell::new(0) }; |
| 17884 | /// lane-lossless128: an intra 128-axis (BLOCK_128X64/64X128) block decoded |
| 17885 | /// on a LOSSLESS frame, where every plane is coded as TX_4X4 units (the |
| 17886 | /// frame's `tx_mode` is forced `ONLY_4X4`). Keeps the gate that owns this |
| 17887 | /// shape non-vacuous -- a stream that never codes one reads 0. |
| 17888 | pub(crate) static INTRA128_LOSSLESS_HITS: std::cell::Cell<usize> = const { std::cell::Cell::new(0) }; |
| 17889 | } |
| 17890 | |
| 17891 | /// [`SKIP_VARTX_LUMA_RESET_HITS`], for a gate's own before/after delta. |
| 17892 | pub fn skip_vartx_luma_reset_hits() -> usize { |
| 17893 | SKIP_VARTX_LUMA_RESET_HITS.with(std::cell::Cell::get) |
| 17894 | } |
| 17895 | |
| 17896 | /// [`INTRA128_LOSSLESS_HITS`], for a gate's own before/after delta. |
| 17897 | pub(crate) fn intra128_lossless_hits() -> usize { |
| 17898 | INTRA128_LOSSLESS_HITS.with(std::cell::Cell::get) |
| 17899 | } |
| 17900 | |
| 17901 | /// [`INTRA128_IN_INTER_HITS`], for a gate's own before/after delta. |
| 17902 | pub(crate) fn intra128_in_inter_hits() -> [usize; 2] { |
| 17903 | INTRA128_IN_INTER_HITS.with(std::cell::Cell::get) |
| 17904 | } |
| 17905 | |
| 17906 | /// [`INTRA_SB128_HORZ_HITS`] / [`INTRA_SB128_VERT_HITS`] / |
| 17907 | /// [`INTRA_SB128_DEPTH_HITS`], for a gate's own before/after delta. |
| 17908 | pub(crate) fn intra_sb128_hits() -> (usize, usize, [usize; 3]) { |
| 17909 | ( |
| 17910 | INTRA_SB128_HORZ_HITS.with(std::cell::Cell::get), |
| 17911 | INTRA_SB128_VERT_HITS.with(std::cell::Cell::get), |
| 17912 | INTRA_SB128_DEPTH_HITS.with(std::cell::Cell::get), |
| 17913 | ) |
| 17914 | } |
| 17915 | |
| 17916 | /// The 128 root's HORZ/VERT arm on an INTRA frame: ONE 128x64 (HORZ) or |
| 17917 | /// 64x128 (VERT) intra block (lane-sb128c r8; the inter twin is |
| 17918 | /// `decode_inter_block`'s own rect path). |
| 17919 | /// |
| 17920 | /// libaom `decode_block` -> `predict_and_reconstruct_intra_block` per |
| 17921 | /// transform unit, with the 128-specific geometry: |
| 17922 | /// * `max_txsize_rect_lookup[BLOCK_128X64]` is TX_64X64, so every luma unit |
| 17923 | /// is SQUARE and there are at least two of them -- depth 0: 64x64 x2, |
| 17924 | /// depth 1: 32x32 x8, depth 2: 16x16 x32 (`bsize_to_tx_size_cat` counts |
| 17925 | /// the `sub_tx_size_map` chain TX_64X64->32->16->8->4, i.e. cat 3, the |
| 17926 | /// same CDF a 64x64 square block reads). |
| 17927 | /// * `av1_get_max_uv_txsize` = `av1_get_adjusted_tx_size( |
| 17928 | /// max_txsize_rect_lookup[BLOCK_64X32])` caps at TX_32X32, so the block's |
| 17929 | /// 64x32 uv plane block is TWO 32x32 chroma units per plane -- one per |
| 17930 | /// 64x64 luma "mu" chunk, and `decode_token_recon_block` codes Y, U, V of |
| 17931 | /// a chunk before moving to the next (the interleave lane-sb128b r3 |
| 17932 | /// measured at the 128x128 NONE root). |
| 17933 | /// * `get_txb_ctx`: the uv plane block is bigger than the uv transform, so |
| 17934 | /// chroma `txb_skip_ctx` takes the offset-10 rows (7 + 3). |
| 17935 | /// No CfL, no filter intra (both cap at 32x32) and no palette |
| 17936 | /// (`av1_allow_palette` caps at 64x64) exist at this size -- the mode |
| 17937 | /// reader's own size gates already say so, and the guard below is defensive. |
| 17938 | /// |
| 17939 | /// lane-lossless128: on a LOSSLESS frame every one of those sizes collapses to |
| 17940 | /// TX_4X4 -- `read_tx_mode` returns `ONLY_4X4` before the frame codes a tx-size |
| 17941 | /// symbol and `read_tx_size` returns `TX_4X4` before it looks at anything |
| 17942 | /// (`decodeframe.c:141`, `:1183`) -- so the block is tiled into 4x4 luma units |
| 17943 | /// and (because a lossless frame forces `TX_4X4` on CHROMA too) 4x4 chroma |
| 17944 | /// units, plane-major inside each 64x64 mu chunk exactly as |
| 17945 | /// `decode_token_recon_block` codes them. Reading the 64-point geometry there |
| 17946 | /// reconstructs into a `side`-sized plane with a 16-sample residual (the panic |
| 17947 | /// this lane closed); the geometry and the unit walks below are normalized on |
| 17948 | /// `lossless_frame`. |
| 17949 | #[allow(clippy::too_many_arguments)] |
| 17950 | fn decode_block_128rect( |
| 17951 | dec: &mut SymbolDecoder, |
| 17952 | cdfs: &mut Cdfs, |
| 17953 | neighbours: &mut Neighbours, |
| 17954 | at: (usize, usize), |
| 17955 | bw: usize, |
| 17956 | bh: usize, |
| 17957 | y: &mut PlaneBuf<'static>, |
| 17958 | u: &mut PlaneBuf<'static>, |
| 17959 | v: &mut PlaneBuf<'static>, |
| 17960 | base_q_idx: u8, |
| 17961 | enable_filter_intra: bool, |
| 17962 | allow_screen_content_tools: bool, |
| 17963 | tx_select: bool, |
| 17964 | reduced_tx_set: bool, fctx: &crate::decode::FrameCtx, |
| 17965 | ) -> Result<()> { |
| 17966 | let (r, c) = at; |
| 17967 | let (px, py) = (c * SUB, r * SUB); |
| 17968 | let (mi_r, mi_c) = (r * (SUB / MI), c * (SUB / MI)); |
| 17969 | let (nb_above_mode, nb_left_mode) = neighbours.modes_above_left(r, c); |
| 17970 | let (palette_ctx, palette_cache) = neighbours.palette_ctx_and_cache(at); |
| 17971 | let palette_uv_cache = neighbours.palette_uv_cache(at); |
| 17972 | let (skip, mode, angle_delta_y, uv_mode, angle_delta_uv, alpha, filter_intra, palette_y, palette_uv, intrabc) = |
| 17973 | read_intra_mode_rect( |
| 17974 | dec, |
| 17975 | cdfs, |
| 17976 | nb_above_mode, |
| 17977 | nb_left_mode, |
| 17978 | // `is_cfl_allowed` caps CFL at 32x32: `uv_mode` comes off the |
| 17979 | // 13-symbol no-CFL CDF here, never the 14-symbol one. |
| 17980 | false, |
| 17981 | true, |
| 17982 | bw, |
| 17983 | bh, |
| 17984 | enable_filter_intra, |
| 17985 | neighbours.skip_txfm_ctx(mi_r, mi_c), |
| 17986 | allow_screen_content_tools, |
| 17987 | Some((palette_ctx, &palette_cache)), |
| 17988 | &palette_uv_cache, |
| 17989 | mi_r, |
| 17990 | mi_c, fctx, |
| 17991 | )?; |
| 17992 | if let Some((dv_row, dv_col)) = intrabc { |
| 17993 | // lane-av1-ibcrect: the reconstruction is ported -- an intrabc block |
| 17994 | // is is_inter_block, so libaom predicts the whole 128x64/64x128 strip |
| 17995 | // from this frame's own reconstruction and runs the INTER residual |
| 17996 | // walk (`decode_token_recon_block`'s else arm) over it. |
| 17997 | return decode_intrabc_128rect( |
| 17998 | dec, |
| 17999 | cdfs, |
| 18000 | neighbours, |
| 18001 | (mi_r, mi_c), |
| 18002 | (bw, bh), |
| 18003 | skip, |
| 18004 | (dv_row, dv_col), |
| 18005 | y, |
| 18006 | u, |
| 18007 | v, |
| 18008 | base_q_idx, fctx, |
| 18009 | ); |
| 18010 | } |
| 18011 | if alpha.is_some() || filter_intra.is_some() || palette_y.is_some() || palette_uv.is_some() { |
| 18012 | return Err(unsupported( |
| 18013 | "CfL, filter intra or a palette on a 128-root HORZ/VERT intra block (every one of \ |
| 18014 | their size gates caps at 64x64 or below, so none of these symbols exists there)", |
| 18015 | )); |
| 18016 | } |
| 18017 | let uv_predict_mode = if uv_mode == UV_CFL_PRED { DC_PRED } else { uv_mode }; |
| 18018 | let smooth_neighbor = is_smooth_mode(nb_above_mode) || is_smooth_mode(nb_left_mode); |
| 18019 | if smooth_neighbor { |
| 18020 | hit!(SMOOTH_LUMA_HITS); |
| 18021 | } |
| 18022 | let smooth_neighbor_uv = neighbours.smooth_uv_neighbour(mi_r, mi_c, r, c, fctx); |
| 18023 | // spec 5.11.16 `read_tx_size`: read for every intra block, skipped or |
| 18024 | // not. `bsize_to_tx_size_cat(BLOCK_128X64)` is 3 (see this fn's doc). |
| 18025 | // lane-lossless128: a LOSSLESS frame codes no tx-size symbol at all -- |
| 18026 | // libaom's `read_tx_mode` returns `ONLY_4X4` for a `coded_lossless` frame |
| 18027 | // before the symbol is even considered (`decodeframe.c:141`) and |
| 18028 | // `read_tx_size` returns `TX_4X4` before it looks at anything |
| 18029 | // (`decodeframe.c:1183`). So `tx_select` is off, every plane is walked as |
| 18030 | // TX_4X4 transform units, and no `tx_size_cat3` symbol may be consumed. |
| 18031 | let lossless_frame = lossless(fctx); |
| 18032 | let in_inter = fctx.intra_in_inter_mode.with(std::cell::Cell::get).is_some(); |
| 18033 | let depth = if tx_select && !lossless_frame { |
| 18034 | // lane-inter128intra r1: `get_tx_size_context` compares the bands |
| 18035 | // against `tx_size_wide/high[max_txsize_rect_lookup[bsize]]` -- |
| 18036 | // TX_64X64 for every 128-axis block, hence the `min(64)`. |
| 18037 | let ctx = tx_size_context_txfm_rect(neighbours, (mi_r, mi_c), bw.min(64), bh.min(64), fctx); |
| 18038 | dec.symbol(&mut cdfs.tx_size_cat3[ctx]) |
| 18039 | } else { |
| 18040 | 0 |
| 18041 | }; |
| 18042 | if depth != 0 { |
| 18043 | hit!(TX_DEPTH_HITS); |
| 18044 | } |
| 18045 | hit_do! { |
| 18046 | INTRA_SB128_DEPTH_HITS.with(|c| { |
| 18047 | let mut h = c.get(); |
| 18048 | h[depth.min(2)] += 1; |
| 18049 | c.set(h); |
| 18050 | }); |
| 18051 | } |
| 18052 | let logical_tx = if lossless_frame { 4 } else { 64 >> depth }; |
| 18053 | if lossless_frame { |
| 18054 | hit!(INTRA128_LOSSLESS_HITS); |
| 18055 | } |
| 18056 | // A 64-point transform codes nothing past its low 32 coefficients. |
| 18057 | let coeff_tx_side = logical_tx.min(32); |
| 18058 | let luma_set = if lossless_frame { |
| 18059 | TxbSet::Luma4 |
| 18060 | } else { |
| 18061 | txbset_for(logical_tx, reduced_tx_set) |
| 18062 | }; |
| 18063 | let luma_scan = default_scan(coeff_tx_side); |
| 18064 | // `av1_get_max_uv_txsize` caps at TX_32X32 normally; a lossless frame |
| 18065 | // forces every plane to TX_4X4 (its own `Chroma4` set and 4x4 scan). |
| 18066 | let chroma_tx = if lossless_frame { 4 } else { 32 }; |
| 18067 | let chroma_set = if lossless_frame { |
| 18068 | TxbSet::Chroma4 |
| 18069 | } else { |
| 18070 | TxbSet::Chroma32 |
| 18071 | }; |
| 18072 | let chroma_scan = default_scan(chroma_tx); |
| 18073 | let (cpx, cpy) = (px >> ss_x(fctx), py >> ss_y(fctx)); |
| 18074 | let (chroma_w, chroma_h) = (bw >> ss_x(fctx), bh >> ss_y(fctx)); |
| 18075 | let reach = Reach::of_rect(bw, bh, px, py, y.width, y.height, fctx); |
| 18076 | let (nch_x, nch_y) = (bw / 64, bh / 64); |
| 18077 | // The frame's own mi grid (`av1_calc_mi_size`, 8-px aligned): libaom's |
| 18078 | // `decode_token_recon_block` clips each vector's walk to |
| 18079 | // `max_block_wide/high`, so a chroma unit whose top-left is off the frame |
| 18080 | // codes nothing. |
| 18081 | let mi_cols = 2 * y.true_width.div_ceil(8); |
| 18082 | let mi_rows = 2 * y.true_height.div_ceil(8); |
| 18083 | let max_w_mi = (bw / MI).min(mi_cols.saturating_sub(mi_c)); |
| 18084 | let max_h_mi = (bh / MI).min(mi_rows.saturating_sub(mi_r)); |
| 18085 | // Non-lossless: one 32x32 chroma unit per mu chunk, per plane. |
| 18086 | debug_assert!(lossless_frame || (chroma_w / chroma_tx, chroma_h / chroma_tx) == (nch_x, nch_y)); |
| 18087 | let tpc = 64 / logical_tx; |
| 18088 | let zero_tu = vec![0i32; logical_tx * logical_tx]; |
| 18089 | let zero_cu = vec![0i32; chroma_tx * chroma_tx]; |
| 18090 | let mut last_grids = [Grid::Zero(0), Grid::Zero(0)]; |
| 18091 | let mut units: Vec<(usize, (usize, usize), Grid)> = Vec::new(); |
| 18092 | // The chroma context arrays are indexed in LUMA mi; every chroma unit of |
| 18093 | // every chunk shares this luma span (2 chroma pixels per luma unit). |
| 18094 | let luma_span = chroma_tx << ss_x(fctx); |
| 18095 | for ch_row in 0..nch_y { |
| 18096 | for ch_col in 0..nch_x { |
| 18097 | for tu_row in ch_row * tpc..(ch_row + 1) * tpc { |
| 18098 | for tu_col in ch_col * tpc..(ch_col + 1) * tpc { |
| 18099 | let tu_mi = ( |
| 18100 | mi_r + tu_row * (logical_tx / MI), |
| 18101 | mi_c + tu_col * (logical_tx / MI), |
| 18102 | ); |
| 18103 | let (tu_px, tu_py) = (px + tu_col * logical_tx, py + tu_row * logical_tx); |
| 18104 | // libaom clips the transform loop to |
| 18105 | // `max_blocks_wide/high`: a unit whose top-left sample is |
| 18106 | // outside the frame is never coded. |
| 18107 | if tu_px >= y.true_width || tu_py >= y.true_height { |
| 18108 | continue; |
| 18109 | } |
| 18110 | // Edge availability of a unit INSIDE a block, with the |
| 18111 | // 128 geometry -- `has_top_right` is false for a block |
| 18112 | // whose largest side is > 64, which [`Reach::of_rect`] |
| 18113 | // already answered for the block and [`tu_reach`] carries |
| 18114 | // down to the units on its own top row. |
| 18115 | let tu_r = tu_reach( |
| 18116 | bw, |
| 18117 | bh, |
| 18118 | tu_col * logical_tx, |
| 18119 | tu_row * logical_tx, |
| 18120 | logical_tx, |
| 18121 | reach, |
| 18122 | px, |
| 18123 | py, |
| 18124 | y.width, |
| 18125 | y.height, fctx, |
| 18126 | ); |
| 18127 | if skip { |
| 18128 | push_intra(0, |
| 18129 | tu_px, tu_py, logical_tx, mode, angle_delta_y, tu_r, &zero_tu, None, |
| 18130 | None, smooth_neighbor, fctx, |
| 18131 | ); |
| 18132 | neighbours.record_mi_luma(tu_mi, logical_tx, &zero_tu); |
| 18133 | continue; |
| 18134 | } |
| 18135 | // The unit is smaller than its block, so `txb_skip_ctx` |
| 18136 | // is the neighbour-magnitude table, never the lone-TU 0. |
| 18137 | let tu_skip_ctx = neighbours.luma_skip_ctx(tu_mi, logical_tx / MI); |
| 18138 | let tu_around = neighbours.around_mi(tu_mi, logical_tx)[0]; |
| 18139 | let tu_grid = read_plane( |
| 18140 | dec, |
| 18141 | cdfs, |
| 18142 | luma_set, |
| 18143 | &luma_scan, |
| 18144 | 0, |
| 18145 | tu_around, |
| 18146 | mode, |
| 18147 | mode, |
| 18148 | angle_delta_y, |
| 18149 | tu_r, |
| 18150 | y, |
| 18151 | tu_px, |
| 18152 | tu_py, |
| 18153 | logical_tx, |
| 18154 | coeff_tx_side, |
| 18155 | base_q_idx, |
| 18156 | None, |
| 18157 | None, |
| 18158 | Some(tu_skip_ctx), |
| 18159 | smooth_neighbor, fctx, |
| 18160 | )?; |
| 18161 | neighbours.record_mi_luma(tu_mi, logical_tx, &tu_grid); |
| 18162 | } |
| 18163 | } |
| 18164 | // Transform units per axis inside this mu chunk's 32x32 chroma |
| 18165 | // span: one 32x32 unit normally, 8x8 TX_4X4 units when lossless. |
| 18166 | let cn = (32 / chroma_tx).max(1); |
| 18167 | // libaom's per-chunk `unit_width/height` (plane-mi units of the |
| 18168 | // luma mi grid): `min(mu_blocks + col, max_blocks) >> ss`. |
| 18169 | let unit_w = ((ch_col + 1) * 16).min(max_w_mi) >> ss_x(fctx); |
| 18170 | let unit_h = ((ch_row + 1) * 16).min(max_h_mi) >> ss_y(fctx); |
| 18171 | let stepc = (chroma_tx / 4).max(1); |
| 18172 | // `decode_token_recon_block` codes a mu chunk PLANE-MAJOR: every U |
| 18173 | // unit of the chunk, then every V unit. At one unit per plane per |
| 18174 | // chunk (every non-lossless 128-axis block) that is the same |
| 18175 | // symbol order the interleaved pair used to read; a lossless |
| 18176 | // frame's 4x4 chroma units are where it becomes observable. |
| 18177 | for plane_pass in 1..=2usize { |
| 18178 | for ci_row in 0..cn { |
| 18179 | for ci_col in 0..cn { |
| 18180 | if ch_col * 8 + ci_col * stepc >= unit_w |
| 18181 | || ch_row * 8 + ci_row * stepc >= unit_h |
| 18182 | { |
| 18183 | continue; |
| 18184 | } |
| 18185 | let cu_x = cpx + ch_col * 32 + ci_col * chroma_tx; |
| 18186 | let cu_y = cpy + ch_row * 32 + ci_row * chroma_tx; |
| 18187 | let cu_mi = ( |
| 18188 | mi_r + ch_row * 16 + ci_row * ((chroma_tx << ss_y(fctx)) / 4), |
| 18189 | mi_c + ch_col * 16 + ci_col * ((chroma_tx << ss_x(fctx)) / 4), |
| 18190 | ); |
| 18191 | let cu_r = tu_reach( |
| 18192 | bw, |
| 18193 | bh, |
| 18194 | ch_col * 64 + ci_col * luma_span, |
| 18195 | ch_row * 64 + ci_row * luma_span, |
| 18196 | luma_span, |
| 18197 | reach, |
| 18198 | px, |
| 18199 | py, |
| 18200 | y.width, |
| 18201 | y.height, fctx, |
| 18202 | ); |
| 18203 | if skip { |
| 18204 | push_intra(plane_pass, |
| 18205 | cu_x, cu_y, chroma_tx, uv_predict_mode, angle_delta_uv, cu_r, |
| 18206 | &zero_cu, None, None, smooth_neighbor_uv, fctx, |
| 18207 | ); |
| 18208 | continue; |
| 18209 | } |
| 18210 | let cu_around = neighbours.around_mi(cu_mi, luma_span); |
| 18211 | let buf: &mut PlaneBuf<'static> = |
| 18212 | if plane_pass == 1 { &mut *u } else { &mut *v }; |
| 18213 | let grid = read_plane( |
| 18214 | dec, cdfs, chroma_set, &chroma_scan, plane_pass, cu_around[plane_pass], |
| 18215 | mode, uv_predict_mode, angle_delta_uv, cu_r, buf, cu_x, cu_y, |
| 18216 | chroma_tx, chroma_tx, base_q_idx, None, None, Some(3), |
| 18217 | smooth_neighbor_uv, fctx, |
| 18218 | )?; |
| 18219 | last_grids[plane_pass - 1] = grid.clone(); |
| 18220 | // Immediately, so the NEXT unit of this same block |
| 18221 | // reads this one's coefficient context; replayed after |
| 18222 | // `record_split_luma_rect` below, which would otherwise |
| 18223 | // clobber them whole-block. |
| 18224 | neighbours.record_mi_chroma(cu_mi, luma_span, luma_span, plane_pass, &grid); |
| 18225 | units.push((plane_pass, cu_mi, grid)); |
| 18226 | } |
| 18227 | } |
| 18228 | } |
| 18229 | if !skip { |
| 18230 | hit!(CHROMA_SPLIT_TX_HITS); |
| 18231 | } |
| 18232 | } |
| 18233 | } |
| 18234 | if skip { |
| 18235 | neighbours.record_rect( |
| 18236 | at, |
| 18237 | bw, |
| 18238 | bh, |
| 18239 | mode, |
| 18240 | uv_predict_mode, |
| 18241 | &[ |
| 18242 | &ZERO_RESIDUAL[..bw * bh], |
| 18243 | &ZERO_RESIDUAL[..chroma_w * chroma_h], |
| 18244 | &ZERO_RESIDUAL[..chroma_w * chroma_h], |
| 18245 | ], |
| 18246 | ); |
| 18247 | } else { |
| 18248 | neighbours.record_split_luma_rect( |
| 18249 | at, |
| 18250 | bw, |
| 18251 | bh, |
| 18252 | mode, |
| 18253 | uv_predict_mode, |
| 18254 | [&last_grids[0], &last_grids[1]], |
| 18255 | ); |
| 18256 | for (plane, cu_mi, grid) in &units { |
| 18257 | neighbours.record_mi_chroma(*cu_mi, luma_span, luma_span, *plane, grid); |
| 18258 | } |
| 18259 | } |
| 18260 | // No palette exists at this size; the unconditional stamp clears any |
| 18261 | // stale neighbour palette state, as every other block path does. |
| 18262 | neighbours.record_palette_y_rect(at, bw, bh, 0, [0u16; 8]); |
| 18263 | neighbours.record_palette_uv_rect(at, bw, bh, 0, [0u16; 8]); |
| 18264 | neighbours.fill_skip_grid_rect((mi_r, mi_c), bw / MI, bh / MI, skip); |
| 18265 | neighbours.fill_lf_grid_rect( |
| 18266 | (mi_r, mi_c), |
| 18267 | bw / MI, |
| 18268 | bh / MI, |
| 18269 | logical_tx as u8, |
| 18270 | logical_tx as u8, |
| 18271 | 0, fctx, |
| 18272 | ); |
| 18273 | if in_inter || allow_intrabc_frame(fctx) { |
| 18274 | // `set_txfm_ctxs(tx_size, n4_w, n4_h, skip && is_inter, xd)` |
| 18275 | // (libaom `decodeframe.c` `decode_block`) runs for EVERY block of an |
| 18276 | // inter frame, intra ones included: an intra block stamps its |
| 18277 | // TRANSFORM size over its own footprint in both TXFM_CONTEXT bands. |
| 18278 | // A key frame reads those bands only when it allows intrabc |
| 18279 | // ([`read_block_tx_size_rect`]'s var-tx context), and then libaom |
| 18280 | // stamps here too -- lane-av1-intrabc r3. |
| 18281 | txfm_partition_update_rect( |
| 18282 | neighbours, |
| 18283 | (mi_r, mi_c), |
| 18284 | (logical_tx, logical_tx), |
| 18285 | (bw, bh), |
| 18286 | ); |
| 18287 | } |
| 18288 | if in_inter { |
| 18289 | hit_do! { |
| 18290 | INTRA128_IN_INTER_HITS.with(|c| { |
| 18291 | let mut h = c.get(); |
| 18292 | h[usize::from(bw < bh)] += 1; |
| 18293 | c.set(h); |
| 18294 | }); |
| 18295 | } |
| 18296 | } else if bw > bh { |
| 18297 | hit!(INTRA_SB128_HORZ_HITS); |
| 18298 | } else { |
| 18299 | hit!(INTRA_SB128_VERT_HITS); |
| 18300 | } |
| 18301 | Ok(()) |
| 18302 | } |
| 18303 | |
| 18304 | |
| 18305 | /// Decodes one 8x8 leaf of a straddling 16x16 block (lane-av1-rect), mirroring |
| 18306 | /// [`crate::tile::write_leaf8`]: its intra-mode context comes from the |
| 18307 | /// *enclosing* 16x16 slot's `above_mode`/`left_mode` (`outer_at`, [`SUB`]-grid), |
| 18308 | /// overridden on whichever axis `prev_leaf` sits along -- the previous leaf's |
| 18309 | /// just-decoded mode, not the enclosing slot's stale one, is the true |
| 18310 | /// neighbour there. `leaf_mi` is this leaf's own position in 4x4 mode-info |
| 18311 | /// units, which is what its coefficient context (finer than [`SUB`]) is read |
| 18312 | /// at. Returns the mode this leaf decoded, for the caller's own |
| 18313 | /// `prev_leaf`/final mode-writeback bookkeeping. |
| 18314 | #[allow(clippy::too_many_arguments)] |
| 18315 | /// The coarse 16x16 `above_mode`/`left_mode` cells a four-way SPLIT of one |
| 18316 | /// 16x16 parent leaves behind (lane-rectx r3). The next block DOWN reads |
| 18317 | /// `above_mode[col]`, and its true above neighbour is the mi directly above |
| 18318 | /// its own top-left corner -- the parent's BOTTOM-LEFT leaf, not the last one |
| 18319 | /// decoded; the next block RIGHT reads `left_mode[row]`, whose true left |
| 18320 | /// neighbour is the parent's TOP-RIGHT leaf. Writing the z-order last leaf |
| 18321 | /// (bottom-right) into both, as this path did, is right only when all four |
| 18322 | /// leaves happen to share a mode. `done` may be short at the true frame edge |
| 18323 | /// (the straddle caller filters positions off the frame), so each side falls |
| 18324 | /// back to the last leaf actually decoded. |
| 18325 | fn write_back_split_modes( |
| 18326 | neighbours: &mut Neighbours, |
| 18327 | done: &[((usize, usize), usize)], |
| 18328 | parent_mi: (usize, usize), |
| 18329 | slot: (usize, usize), |
| 18330 | ) { |
| 18331 | let (sr, sc) = slot; |
| 18332 | let Some(&(_, last)) = done.last() else { |
| 18333 | return; |
| 18334 | }; |
| 18335 | let find = |mi: (usize, usize)| done.iter().find(|&&(at, _)| at == mi).map(|&(_, m)| m); |
| 18336 | neighbours.above_mode[sc] = find((parent_mi.0 + 2, parent_mi.1)).unwrap_or(last); |
| 18337 | neighbours.left_mode[sr] = find((parent_mi.0, parent_mi.1 + 2)).unwrap_or(last); |
| 18338 | } |
| 18339 | |
| 18340 | fn decode_leaf8( |
| 18341 | dec: &mut SymbolDecoder, |
| 18342 | cdfs: &mut Cdfs, |
| 18343 | neighbours: &mut Neighbours, |
| 18344 | outer_at: (usize, usize), |
| 18345 | leaf_mi: (usize, usize), |
| 18346 | scans: (&[u16], &[u16]), |
| 18347 | prev_leaf: Option<((usize, usize), usize)>, |
| 18348 | y: &mut PlaneBuf<'static>, |
| 18349 | u: &mut PlaneBuf<'static>, |
| 18350 | v: &mut PlaneBuf<'static>, |
| 18351 | base_q_idx: u8, |
| 18352 | enable_filter_intra: bool, |
| 18353 | allow_screen_content_tools: bool, |
| 18354 | allow_intrabc: bool, |
| 18355 | tx_select: bool, |
| 18356 | reduced_tx_set: bool, fctx: &crate::decode::FrameCtx, |
| 18357 | ) -> Result<usize> { |
| 18358 | let (r, c) = outer_at; |
| 18359 | // lane-modectx r1: the group's own availability-correct fallback pair, not |
| 18360 | // the raw coarse bands (which leak across a tile edge -- see |
| 18361 | // `modes_above_left`). |
| 18362 | let (mut above_mode, mut left_mode) = neighbours.modes_above_left(r, c); |
| 18363 | if let Some(((pr, pc), pmode)) = prev_leaf { |
| 18364 | if pc == leaf_mi.1 && leaf_mi.0 == pr + 2 { |
| 18365 | above_mode = pmode; |
| 18366 | } else if pr == leaf_mi.0 && leaf_mi.1 == pc + 2 { |
| 18367 | left_mode = pmode; |
| 18368 | } |
| 18369 | } |
| 18370 | // The mi-exact neighbour wins over both the coarse 16x16 slot and |
| 18371 | // `prev_leaf`: one 16x16 slot cannot hold the two different modes its |
| 18372 | // two 8x8 (or four 4x4) columns leave behind, so a leaf whose above/left |
| 18373 | // neighbour sits in a split cell read the wrong `kf_y_mode` row. |
| 18374 | // lane-rectsplitx r2 swept this arm into |
| 18375 | // [`Neighbours::modes_above_left_mi`]: the mi-exact map ALSO has to answer |
| 18376 | // "no neighbour" at a tile edge, where the coarse cell holds the block |
| 18377 | // above/left of it inside the same 16x16. |
| 18378 | let (above_mode, left_mode) = |
| 18379 | neighbours.modes_above_left_mi(leaf_mi.0, leaf_mi.1, (above_mode, left_mode)); |
| 18380 | let smooth_neighbor = is_smooth_mode(above_mode) || is_smooth_mode(left_mode); |
| 18381 | // Chroma's edge-filter type reads the CHROMA neighbour's `uv_mode` |
| 18382 | // (`get_intra_edge_filter_type`, reconintra.c:974), never the luma mode |
| 18383 | // this leaf's `smooth_neighbor` is built from -- passing `false` here |
| 18384 | // filtered a directional chroma block's edge at the wrong strength |
| 18385 | // (lane-sub8 r5: 4 pixels of one 8x8 leaf's chroma, deltas 1..3). |
| 18386 | // The `uv_mode` neighbours are mi-exact (`uv_mode_col`/`uv_mode_row`), |
| 18387 | // with the coarse [`SUB`] slot as the fallback when no block recorded |
| 18388 | // that exact mi cell. |
| 18389 | let smooth_neighbor_uv = neighbours.smooth_uv_neighbour(leaf_mi.0, leaf_mi.1, r, c, fctx); |
| 18390 | if smooth_neighbor { |
| 18391 | hit!(SMOOTH_LUMA_HITS); |
| 18392 | } |
| 18393 | // An 8x8 leaf is well within `is_cfl_allowed`'s <=32x32 bound (spec |
| 18394 | // 5.11.5), so it reads the CFL-allowed `uv_mode_cfl` CDF, like every other |
| 18395 | // `decode_block` caller at 16x16 and up. |
| 18396 | // lane-pal8 r1: this leaf's own palette-Y mode ctx and colour cache |
| 18397 | // (`av1_get_palette_mode_ctx` / `av1_get_palette_cache`, decodemv.c:567, |
| 18398 | // `read_palette_mode_info`), at its REAL mi position -- passing |
| 18399 | // `None`/`&[]` read `palette_y_mode` off CDF row 0 and refused by name the |
| 18400 | // moment a real 8x8 palette block fired, which is where every 8-bit |
| 18401 | // `--tune-content=screen --min-partition-size=8` stream stopped. |
| 18402 | let (palette_ctx, palette_cache) = neighbours.palette_ctx_and_cache_mi(leaf_mi); |
| 18403 | let palette_uv_cache = neighbours.palette_uv_cache_mi(leaf_mi); |
| 18404 | let (skip, mode, angle_delta_y, uv_mode, angle_delta_uv, alpha, filter_intra, palette_y, palette_uv) = |
| 18405 | read_intra_mode( |
| 18406 | dec, |
| 18407 | cdfs, |
| 18408 | above_mode, |
| 18409 | left_mode, |
| 18410 | true, |
| 18411 | 8, |
| 18412 | enable_filter_intra, |
| 18413 | neighbours.skip_txfm_ctx(leaf_mi.0, leaf_mi.1), |
| 18414 | allow_screen_content_tools, |
| 18415 | allow_intrabc, |
| 18416 | Some((palette_ctx, &palette_cache)), |
| 18417 | &palette_uv_cache, |
| 18418 | leaf_mi.0, |
| 18419 | leaf_mi.1, fctx, |
| 18420 | )?; |
| 18421 | // lane-kf900 r7: an 8x8 leaf consumed `use_intrabc` (and its DV) through |
| 18422 | // `read_intra_mode` and then decoded the block as if it were ordinary |
| 18423 | // intra. libaom's `is_inter_block` counts an intrabc block |
| 18424 | // (`blockd.h:373`), so its luma `tx_type` comes off the INTER ext-tx set |
| 18425 | // (`av1_get_ext_tx_set_type(TX_8X8, is_inter=1, 0)` = `EXT_TX_SET_ALL16`, |
| 18426 | // a 16-symbol CDF) while we read the 7-symbol `TX_SET_INTRA_1` row: the |
| 18427 | // same value with a different interval, so the tile desynced at the very |
| 18428 | // next symbol (class `wrong-alphabet-same-value`). Everything below |
| 18429 | // mirrors `decode_block`'s own intrabc handling at 16x16 and up. |
| 18430 | let intrabc_dv = fctx.intrabc_dv.with(|c| c.take()); |
| 18431 | if intrabc_dv.is_some() { |
| 18432 | hit!(LEAF8_INTRABC_HITS); |
| 18433 | } |
| 18434 | // `av1_visit_palette`'s reconstruction (decodeframe.c:1135): the colour |
| 18435 | // index map, mapped through the base colours, IS the block's prediction -- |
| 18436 | // carried to the next `reconstruct`/`read_plane` through the same |
| 18437 | // [`PALETTE_PRED`] slot [`decode_block`] uses at 16x16 and up. |
| 18438 | let palette_y_buf: Option<Vec<u16>> = palette_y |
| 18439 | .as_ref() |
| 18440 | .map(|p| p.map.iter().map(|&i| p.colors[i as usize]).collect()); |
| 18441 | let palette_uv_bufs: Option<(Vec<u16>, Vec<u16>)> = palette_uv.as_ref().map(|p| { |
| 18442 | ( |
| 18443 | p.map.iter().map(|&i| p.u_colors[i as usize]).collect(), |
| 18444 | p.map.iter().map(|&i| p.v_colors[i as usize]).collect(), |
| 18445 | ) |
| 18446 | }); |
| 18447 | let (pal_y_size, pal_y_colors) = |
| 18448 | palette_y.as_ref().map_or((0, [0u16; 8]), |p| (p.size, p.colors)); |
| 18449 | let (pal_uv_size, pal_uv_colors) = |
| 18450 | palette_uv.as_ref().map_or((0, [0u16; 8]), |p| (p.size, p.u_colors)); |
| 18451 | if palette_y.is_some() || palette_uv.is_some() { |
| 18452 | hit!(PALETTE_LEAF8_HITS); |
| 18453 | } |
| 18454 | let uv_predict_mode = if uv_mode == UV_CFL_PRED { |
| 18455 | DC_PRED |
| 18456 | } else { |
| 18457 | uv_mode |
| 18458 | }; |
| 18459 | neighbours.above_uv_mode[c] = uv_predict_mode; |
| 18460 | neighbours.left_uv_mode[r] = uv_predict_mode; |
| 18461 | neighbours.record_uv_mode_mi(leaf_mi.0, leaf_mi.1, 2, 2, uv_predict_mode); |
| 18462 | // `TxMode::Select`'s `tx_depth` at an 8x8 leaf (spec 5.11.16): the only |
| 18463 | // depths an 8x8 block offers are `TX8` (depth 0) and `TX4` (depth 1, |
| 18464 | // which splits the leaf's own 8x8 prediction into a 2x2 grid of 4x4 |
| 18465 | // transform units, same raster-order per-TU pattern `decode_block`'s |
| 18466 | // multi-TU branch runs). |
| 18467 | // ... and an intrabc leaf reads no intra `tx_depth` symbol at all: libaom |
| 18468 | // dispatches `read_block_tx_size` on `is_inter_block`, which counts an |
| 18469 | // intrabc block, so a SKIPPED one takes the largest transform and an |
| 18470 | // UNSKIPPED one reads the inter var-tx tree (lane-t900 r32). At 8x8 that |
| 18471 | // tree is one `txfm_split` symbol: TX_8X8, or TX_4X4 over all four mi |
| 18472 | // cells (`read_var_tx_size`'s `sub_txs == TX_4X4` early return). |
| 18473 | // `parse_decode_block`'s tree condition carries |
| 18474 | // `!xd->lossless[mbmi->segment_id]` (see [`decode_block`]'s own copy): |
| 18475 | // a lossless segment's intrabc leaf reads no tree, TX_4X4 via |
| 18476 | // `read_tx_size`'s first line (lane-av1mixloss). |
| 18477 | let resolved = if tx_select && intrabc_dv.is_some() && !skip && !lossless(fctx) { |
| 18478 | let mut leaves = Vec::new(); |
| 18479 | let mi_cols = 2 * y.true_width.div_ceil(8); |
| 18480 | let mi_rows = 2 * y.true_height.div_ceil(8); |
| 18481 | read_var_tx_size( |
| 18482 | dec, |
| 18483 | cdfs, |
| 18484 | neighbours, |
| 18485 | leaf_mi, |
| 18486 | 8, |
| 18487 | ( |
| 18488 | 2usize.min(mi_cols.saturating_sub(leaf_mi.1)), |
| 18489 | 2usize.min(mi_rows.saturating_sub(leaf_mi.0)), |
| 18490 | ), |
| 18491 | (8, 8), |
| 18492 | 0, |
| 18493 | 0, |
| 18494 | 0, |
| 18495 | &mut leaves, |
| 18496 | ); |
| 18497 | hit!(INTRABC_VARTX_HITS); |
| 18498 | leaves.first().map_or(8, |l| l.2) |
| 18499 | } else if tx_select && intrabc_dv.is_none() { |
| 18500 | read_tx_size(dec, cdfs, neighbours, leaf_mi, 8, fctx) |
| 18501 | } else { |
| 18502 | // lane-lossless: TX_4X4 on a lossless frame, no symbol |
| 18503 | // (libaom `read_tx_size`). |
| 18504 | lossless_tx(8, fctx) |
| 18505 | }; |
| 18506 | let (px, py) = (leaf_mi.1 * MI, leaf_mi.0 * MI); |
| 18507 | let reach = Reach::of(8, px, py, y.width, y.height, fctx); |
| 18508 | let (cpx, cpy) = (px >> ss_x(fctx), py >> ss_y(fctx)); |
| 18509 | // 4:4:4: this 8x8 block's chroma plane block is 8x8 (same scan as luma, |
| 18510 | // `scans.0`). 4:2:0 stays the 4x4 unit this leaf was written for. |
| 18511 | // lane-av1-422d: 4:2:2 (ss 1/0) halves only the width, so the plane block |
| 18512 | // is 4x8 -- ONE rect transform unit per plane (`av1_get_max_uv_txsize( |
| 18513 | // BLOCK_8X8)` at ss (1,0) is `max_txsize_rect_lookup[BLOCK_4X8]` == |
| 18514 | // TX_4X8): one txb_skip + one scan per plane in the |
| 18515 | // [`TxbSet::ChromaRect8x4`] class [`decode_rect_split`] proves for the |
| 18516 | // shape, read through [`leaf8_chroma422_unit`] in each arm below. The |
| 18517 | // `(csz, cset, cscan)` selection keeps serving the square arms (444's |
| 18518 | // 8x8, 420's 4x4); the rect arms branch on [`chroma_422`] instead. |
| 18519 | let chroma_444 = ss_x(fctx) == 0 && ss_y(fctx) == 0; |
| 18520 | let chroma_422 = ss_x(fctx) == 1 && ss_y(fctx) == 0; |
| 18521 | let (csz, cset, cscan) = if chroma_444 { |
| 18522 | (8usize, TxbSet::Chroma8, scans.0) |
| 18523 | } else { |
| 18524 | (4usize, TxbSet::Chroma4, scans.1) |
| 18525 | }; |
| 18526 | // spec 7.11.3.4 with `use_intrabc`, same as `decode_block`: predict from |
| 18527 | // the CURRENT frame's own pre-loop-filter reconstruction (every loop |
| 18528 | // filter is off whenever `allow_intrabc`, frame.rs:194/210/252/272) at |
| 18529 | // the full-pel block vector, with the BILINEAR kernel libaom forces. |
| 18530 | flush_recon(fctx, y, u, v); |
| 18531 | let intrabc_bufs = intrabc_dv.map(|(dv_row, dv_col)| { |
| 18532 | let mut yb = vec![0u16; 8 * 8]; |
| 18533 | mc::predict_with_filter( |
| 18534 | &y.data, y.width, y.true_width, y.true_height, |
| 18535 | mv_to_q4(px, dv_col, 0), mv_to_q4(py, dv_row, 0), |
| 18536 | 8, 8, mc::InterpFilterKind::Bilinear, &mut yb, fctx, |
| 18537 | ); |
| 18538 | // lane-av1-422d: the DV copy covers the leaf's whole CHROMA PLANE |
| 18539 | // block -- 4x8 at ss (1,0), 4x4 at 4:2:0/4:4:4 (the 4:4:4 8x8 leaf's |
| 18540 | // own unit read is 8x8, so its 4x4 window here predates that lane's |
| 18541 | // rect walk and is untouched by this one). |
| 18542 | let (icw, ich) = if chroma_422 { (4usize, 8usize) } else { (4usize, 4usize) }; |
| 18543 | let mut ub = vec![0u16; icw * ich]; |
| 18544 | mc::predict_with_filter( |
| 18545 | &u.data, u.width, u.true_width, u.true_height, |
| 18546 | mv_to_q4(cpx, dv_col, ss_x(fctx)), mv_to_q4(cpy, dv_row, ss_y(fctx)), |
| 18547 | icw, ich, mc::InterpFilterKind::Bilinear, &mut ub, fctx, |
| 18548 | ); |
| 18549 | let mut vb = vec![0u16; icw * ich]; |
| 18550 | mc::predict_with_filter( |
| 18551 | &v.data, v.width, v.true_width, v.true_height, |
| 18552 | mv_to_q4(cpx, dv_col, ss_x(fctx)), mv_to_q4(cpy, dv_row, ss_y(fctx)), |
| 18553 | icw, ich, mc::InterpFilterKind::Bilinear, &mut vb, fctx, |
| 18554 | ); |
| 18555 | (yb, ub, vb) |
| 18556 | }); |
| 18557 | // The per-plane prediction override rides the same [`PALETTE_PRED`] slot |
| 18558 | // `decode_block` uses: set fresh right before each plane's own |
| 18559 | // `reconstruct`/`read_plane` call (the slot is taken, not copied). |
| 18560 | // lane-t900 r32: through `palette_y_buf`, so the 4x4 TU loop below windows |
| 18561 | // it per unit (an intrabc leaf whose var-tx tree split to TX_4X4 is the |
| 18562 | // first shape that reaches that loop with an override set). |
| 18563 | let palette_y_buf = match &intrabc_bufs { |
| 18564 | Some((yb, _, _)) => Some(yb.clone()), |
| 18565 | None => palette_y_buf, |
| 18566 | }; |
| 18567 | // lane-av1intrapred: the chroma twin of the `palette_y_buf` wrap above -- |
| 18568 | // `decode_block` does the same for its own `palette_uv_bufs` |
| 18569 | // (decode.rs's single-TU and multi-TU chroma reads). Without it, an |
| 18570 | // intrabc leaf whose var-tx tree split to TX_4X4 reached the TX4 arm's |
| 18571 | // chroma reads with the override unset: the unit predicted INTRA from |
| 18572 | // the UV mode (intrabc's is DC) instead of copying the frame at the DV, |
| 18573 | // R5 first differing pixel U(184,4), block mi (2,92). |
| 18574 | let palette_uv_bufs = match &intrabc_bufs { |
| 18575 | Some((_, ub, vb)) => Some((ub.clone(), vb.clone())), |
| 18576 | None => palette_uv_bufs, |
| 18577 | }; |
| 18578 | let (luma_grid, u_grid, v_grid); |
| 18579 | if skip { |
| 18580 | // lane-hgkf r2 (same shape as `decode_block`'s own skip branch): an |
| 18581 | // 8x8 leaf whose `tx_depth` resolved to TX_4X4 predicts FOUR 4x4 |
| 18582 | // transform units, each off the units already written left/above of |
| 18583 | // it inside this same leaf -- an intra block's transform size is read |
| 18584 | // whether or not it is skipped, and spec 7.11.2 predicts per unit. |
| 18585 | // A palette block's prediction is the whole-block index map, never |
| 18586 | // per-transform-unit edges ([`decode_block`]'s own `split_tx_skip` |
| 18587 | // rule) -- so a palette leaf takes the single 8x8 reconstruct below |
| 18588 | // even when `tx_depth` resolved to TX_4X4. |
| 18589 | if resolved == 4 && palette_y_buf.is_none() { |
| 18590 | hit!(SKIP_SPLIT_TX_HITS); |
| 18591 | for tu_row in 0..2 { |
| 18592 | for tu_col in 0..2 { |
| 18593 | let (tu_px, tu_py) = (px + tu_col * 4, py + tu_row * 4); |
| 18594 | if tu_px >= y.true_width || tu_py >= y.true_height { |
| 18595 | continue; |
| 18596 | } |
| 18597 | let tu_reach = tu_reach( |
| 18598 | 8, |
| 18599 | 8, |
| 18600 | tu_col * 4, |
| 18601 | tu_row * 4, |
| 18602 | 4, |
| 18603 | reach, |
| 18604 | px, |
| 18605 | py, |
| 18606 | y.width, |
| 18607 | y.height, fctx, |
| 18608 | ); |
| 18609 | push_intra(0, |
| 18610 | tu_px, |
| 18611 | tu_py, |
| 18612 | 4, |
| 18613 | mode, |
| 18614 | angle_delta_y, |
| 18615 | tu_reach, |
| 18616 | &ZERO_RESIDUAL[..16], |
| 18617 | None, |
| 18618 | filter_intra, |
| 18619 | smooth_neighbor, fctx, |
| 18620 | ); |
| 18621 | } |
| 18622 | } |
| 18623 | } else { |
| 18624 | if let Some(buf) = &palette_y_buf { |
| 18625 | set_palette_pred(buf.clone(), fctx); |
| 18626 | } |
| 18627 | push_intra(0, |
| 18628 | px, |
| 18629 | py, |
| 18630 | 8, |
| 18631 | mode, |
| 18632 | angle_delta_y, |
| 18633 | reach, |
| 18634 | &ZERO_RESIDUAL[..64], |
| 18635 | None, |
| 18636 | filter_intra, |
| 18637 | smooth_neighbor, fctx, |
| 18638 | ); |
| 18639 | } |
| 18640 | let ac = alpha.map(|_| cfl_src(px, py, 8)); |
| 18641 | if chroma_422 { |
| 18642 | // lane-av1-422d: a skipped leaf still predicts its 4x8 rect unit |
| 18643 | // per plane (CfL keeps its contribution, [`decode_block`]'s own |
| 18644 | // skip-arm rule); no coefficient symbol exists, so the entropy |
| 18645 | // context never enters the rect read. |
| 18646 | u_grid = leaf8_chroma422_unit( |
| 18647 | dec, cdfs, (false, false, 0), uv_predict_mode, angle_delta_uv, reach, |
| 18648 | 1, alpha.zip(ac).map(|((au, _), ac)| (au, ac)), true, |
| 18649 | intrabc_bufs.as_ref().map(|(_, ub, _)| ub.clone()) |
| 18650 | .or_else(|| palette_uv_bufs.as_ref().map(|(ub, _)| ub.clone())), |
| 18651 | u, cpx, cpy, smooth_neighbor_uv, fctx, |
| 18652 | )?; |
| 18653 | v_grid = leaf8_chroma422_unit( |
| 18654 | dec, cdfs, (false, false, 0), uv_predict_mode, angle_delta_uv, reach, |
| 18655 | 2, alpha.zip(ac).map(|((_, av), ac)| (av, ac)), true, |
| 18656 | intrabc_bufs.as_ref().map(|(_, _, vb)| vb.clone()) |
| 18657 | .or_else(|| palette_uv_bufs.as_ref().map(|(_, vb)| vb.clone())), |
| 18658 | v, cpx, cpy, smooth_neighbor_uv, fctx, |
| 18659 | )?; |
| 18660 | luma_grid = Grid::Zero(64); |
| 18661 | } else { |
| 18662 | if let Some((_, ub, _)) = &intrabc_bufs { |
| 18663 | set_palette_pred(ub.clone(), fctx); |
| 18664 | } else if let Some((ub, _)) = &palette_uv_bufs { |
| 18665 | set_palette_pred(ub.clone(), fctx); |
| 18666 | } |
| 18667 | push_intra(1, |
| 18668 | cpx, |
| 18669 | cpy, |
| 18670 | csz, |
| 18671 | uv_predict_mode, |
| 18672 | angle_delta_uv, |
| 18673 | reach, |
| 18674 | &ZERO_RESIDUAL[..csz * csz], |
| 18675 | alpha.zip(ac).map(|((au, _), ac)| (au, ac)), |
| 18676 | None, |
| 18677 | smooth_neighbor_uv, fctx, |
| 18678 | ); |
| 18679 | if let Some((_, _, vb)) = &intrabc_bufs { |
| 18680 | set_palette_pred(vb.clone(), fctx); |
| 18681 | } else if let Some((_, vb)) = &palette_uv_bufs { |
| 18682 | set_palette_pred(vb.clone(), fctx); |
| 18683 | } |
| 18684 | push_intra(2, |
| 18685 | cpx, |
| 18686 | cpy, |
| 18687 | csz, |
| 18688 | uv_predict_mode, |
| 18689 | angle_delta_uv, |
| 18690 | reach, |
| 18691 | &ZERO_RESIDUAL[..csz * csz], |
| 18692 | alpha.zip(ac).map(|((_, av), ac)| (av, ac)), |
| 18693 | None, |
| 18694 | smooth_neighbor_uv, fctx, |
| 18695 | ); |
| 18696 | luma_grid = Grid::Zero(64); |
| 18697 | u_grid = Grid::Zero(csz * csz); |
| 18698 | v_grid = Grid::Zero(csz * csz); |
| 18699 | } |
| 18700 | } else if resolved != 4 { |
| 18701 | let around = neighbours.around_mi(leaf_mi, 8); |
| 18702 | // lane-av1-422h: the 4:2:2 chroma rect unit's `get_txb_ctx_general` |
| 18703 | // reads its above votes over CHROMA 4-px cells -- at ss_x 1 one chroma |
| 18704 | // cell spans TWO luma mi columns, so the per-luma-mi sum counts each |
| 18705 | // above chroma column twice and `dc_sign_ctx`'s signum flips whenever |
| 18706 | // the doubled sum crosses zero (the same class |
| 18707 | // [`Neighbours::around_mi_422_chroma`] fixes for the rect readers; |
| 18708 | // measured at mi(2,24) plane 2: ours dc_sign row 0, oracle row 2). |
| 18709 | // The left extent is unsubsquared at ss_y 0 and stays one-to-one. |
| 18710 | let chroma_around = if chroma_422 { |
| 18711 | neighbours.around_mi_422_chroma(leaf_mi, 8, 8) |
| 18712 | } else { |
| 18713 | around |
| 18714 | }; |
| 18715 | if let Some(buf) = &palette_y_buf { |
| 18716 | set_palette_pred(buf.clone(), fctx); |
| 18717 | } |
| 18718 | luma_grid = read_plane( |
| 18719 | dec, |
| 18720 | cdfs, |
| 18721 | if intrabc_dv.is_some() { |
| 18722 | inter_txbset_for(8, reduced_tx_set) |
| 18723 | } else if reduced_tx_set { |
| 18724 | TxbSet::Luma8 |
| 18725 | } else { |
| 18726 | TxbSet::Luma8Set1 |
| 18727 | }, |
| 18728 | scans.0, |
| 18729 | 0, |
| 18730 | around[0], |
| 18731 | mode, |
| 18732 | mode, |
| 18733 | angle_delta_y, |
| 18734 | reach, |
| 18735 | y, |
| 18736 | px, |
| 18737 | py, |
| 18738 | 8, |
| 18739 | TX8, |
| 18740 | base_q_idx, |
| 18741 | None, |
| 18742 | filter_intra, |
| 18743 | None, |
| 18744 | smooth_neighbor, fctx, |
| 18745 | )?; |
| 18746 | // `av1_get_tx_type` branches `PLANE_TYPE_UV` on `is_inter_block`, so |
| 18747 | // an intrabc block's chroma tx_type is the colocated LUMA transform's |
| 18748 | // coded type (lane-kf900 r6's own fix, `decode_block`'s line). |
| 18749 | if intrabc_dv.is_some() { |
| 18750 | fctx.intrabc_chroma_tx.with(|c| c.set(Some(fctx.luma_tx_type.with(std::cell::Cell::get)))); |
| 18751 | } |
| 18752 | let ac = alpha.map(|_| cfl_src(px, py, 8)); |
| 18753 | if chroma_422 { |
| 18754 | // lane-av1-422d: ONE 4x8 rect unit per plane (`av1_get_max_uv_txsize` |
| 18755 | // at ss (1,0) == TX_4X8), covering the plane block exactly, off the |
| 18756 | // leaf's own 8x8-span entropy context (`chroma_tiled`'s convention). |
| 18757 | u_grid = leaf8_chroma422_unit( |
| 18758 | dec, cdfs, chroma_around[1], uv_predict_mode, angle_delta_uv, reach, |
| 18759 | 1, alpha.zip(ac).map(|((au, _), ac)| (au, ac)), false, |
| 18760 | intrabc_bufs.as_ref().map(|(_, ub, _)| ub.clone()) |
| 18761 | .or_else(|| palette_uv_bufs.as_ref().map(|(ub, _)| ub.clone())), |
| 18762 | u, cpx, cpy, smooth_neighbor_uv, fctx, |
| 18763 | )?; |
| 18764 | v_grid = leaf8_chroma422_unit( |
| 18765 | dec, cdfs, chroma_around[2], uv_predict_mode, angle_delta_uv, reach, |
| 18766 | 2, alpha.zip(ac).map(|((_, av), ac)| (av, ac)), false, |
| 18767 | intrabc_bufs.as_ref().map(|(_, _, vb)| vb.clone()) |
| 18768 | .or_else(|| palette_uv_bufs.as_ref().map(|(_, vb)| vb.clone())), |
| 18769 | v, cpx, cpy, smooth_neighbor_uv, fctx, |
| 18770 | )?; |
| 18771 | } else { |
| 18772 | if let Some((_, ub, _)) = &intrabc_bufs { |
| 18773 | set_palette_pred(ub.clone(), fctx); |
| 18774 | } else if let Some((ub, _)) = &palette_uv_bufs { |
| 18775 | set_palette_pred(ub.clone(), fctx); |
| 18776 | } |
| 18777 | u_grid = read_plane( |
| 18778 | dec, |
| 18779 | cdfs, |
| 18780 | cset, |
| 18781 | cscan, |
| 18782 | 1, |
| 18783 | around[1], |
| 18784 | mode, |
| 18785 | uv_predict_mode, |
| 18786 | angle_delta_uv, |
| 18787 | reach, |
| 18788 | u, |
| 18789 | cpx, |
| 18790 | cpy, |
| 18791 | csz, |
| 18792 | csz, |
| 18793 | base_q_idx, |
| 18794 | alpha.zip(ac).map(|((au, _), ac)| (au, ac)), |
| 18795 | None, |
| 18796 | None, |
| 18797 | smooth_neighbor_uv, fctx, |
| 18798 | )?; |
| 18799 | if let Some((_, _, vb)) = &intrabc_bufs { |
| 18800 | set_palette_pred(vb.clone(), fctx); |
| 18801 | } else if let Some((_, vb)) = &palette_uv_bufs { |
| 18802 | set_palette_pred(vb.clone(), fctx); |
| 18803 | } |
| 18804 | v_grid = read_plane( |
| 18805 | dec, |
| 18806 | cdfs, |
| 18807 | cset, |
| 18808 | cscan, |
| 18809 | 2, |
| 18810 | around[2], |
| 18811 | mode, |
| 18812 | uv_predict_mode, |
| 18813 | angle_delta_uv, |
| 18814 | reach, |
| 18815 | v, |
| 18816 | cpx, |
| 18817 | cpy, |
| 18818 | csz, |
| 18819 | csz, |
| 18820 | base_q_idx, |
| 18821 | alpha.zip(ac).map(|((_, av), ac)| (av, ac)), |
| 18822 | None, |
| 18823 | None, |
| 18824 | smooth_neighbor_uv, fctx, |
| 18825 | )?; |
| 18826 | } |
| 18827 | // Disarm: `INTRABC_CHROMA_TX` is `Some` across exactly this block's |
| 18828 | // two chroma reads (`decode_block`'s own tail does the same) -- left |
| 18829 | // set, every later chroma block would inherit this luma tx_type, |
| 18830 | // which picks the scan and the inverse transform. |
| 18831 | fctx.intrabc_chroma_tx.with(|c| c.set(None)); |
| 18832 | } else { |
| 18833 | // `tx_depth` resolved this leaf's luma to `TX4`: a 2x2 raster-order |
| 18834 | // grid of 4x4 transform units, each predicted from the leaf's own |
| 18835 | // 8x8 `mode` and reading its own fresh context off the earlier |
| 18836 | // units in this same leaf (the same per-TU pattern `decode_block`'s |
| 18837 | // multi-TU branch runs at bigger blocks). |
| 18838 | let mut first_leaf_tx: Option<TxType> = None; |
| 18839 | for tu_row in 0..2 { |
| 18840 | for tu_col in 0..2 { |
| 18841 | let tu_mi = (leaf_mi.0 + tu_row, leaf_mi.1 + tu_col); |
| 18842 | let tu_px = px + tu_col * 4; |
| 18843 | let tu_py = py + tu_row * 4; |
| 18844 | let tu_around = neighbours.around_mi(tu_mi, 4)[0]; |
| 18845 | if crate::encode::Reach::in_vert_ab() { |
| 18846 | hit!(VERT_AB_TX4_HITS); |
| 18847 | } |
| 18848 | let tu_reach = tu_reach( |
| 18849 | 8, |
| 18850 | 8, |
| 18851 | tu_col * 4, |
| 18852 | tu_row * 4, |
| 18853 | 4, |
| 18854 | reach, |
| 18855 | px, |
| 18856 | py, |
| 18857 | y.width, |
| 18858 | y.height, fctx, |
| 18859 | ); |
| 18860 | let tu_skip_ctx = neighbours.luma_skip_ctx(tu_mi, 1); |
| 18861 | // Each 4x4 unit predicts from its own window of the block's |
| 18862 | // 8x8 colour-index map ([`decode_block`]'s per-TU slice at |
| 18863 | // 16x16 and up) -- the whole-block buffer would be taken by |
| 18864 | // the FIRST unit and indexed at the wrong stride. |
| 18865 | if let Some(buf) = &palette_y_buf { |
| 18866 | hit!(PALETTE_SPLIT_TX_HITS); |
| 18867 | let mut window = vec![0u16; 16]; |
| 18868 | for row in 0..4 { |
| 18869 | let src = (tu_row * 4 + row) * 8 + tu_col * 4; |
| 18870 | window[row * 4..][..4].copy_from_slice(&buf[src..][..4]); |
| 18871 | } |
| 18872 | set_palette_pred(window, fctx); |
| 18873 | } |
| 18874 | let tu_grid = read_plane( |
| 18875 | dec, |
| 18876 | cdfs, |
| 18877 | if intrabc_dv.is_some() { |
| 18878 | inter_txbset_for(4, reduced_tx_set) |
| 18879 | } else if reduced_tx_set { |
| 18880 | TxbSet::Luma4 |
| 18881 | } else { |
| 18882 | TxbSet::Luma4Set1 |
| 18883 | }, |
| 18884 | scans.1, |
| 18885 | 0, |
| 18886 | tu_around, |
| 18887 | mode, |
| 18888 | mode, |
| 18889 | angle_delta_y, |
| 18890 | tu_reach, |
| 18891 | y, |
| 18892 | tu_px, |
| 18893 | tu_py, |
| 18894 | 4, |
| 18895 | 4, |
| 18896 | base_q_idx, |
| 18897 | None, |
| 18898 | filter_intra, |
| 18899 | Some(tu_skip_ctx), |
| 18900 | smooth_neighbor, fctx, |
| 18901 | )?; |
| 18902 | if crate::envflags::env_flag!("EC_AV1_TRACE") { |
| 18903 | eprintln!( |
| 18904 | "TRACE tu_bitpos mi=({},{}) tell={}", |
| 18905 | tu_mi.0, |
| 18906 | tu_mi.1, |
| 18907 | dec.debug_bitpos() |
| 18908 | ); |
| 18909 | } |
| 18910 | neighbours.record_mi_luma(tu_mi, 4, &tu_grid); |
| 18911 | // The FIRST (top-left, co-located with this group's chroma) |
| 18912 | // TU's coded tx_type is what the group's chroma inherits -- |
| 18913 | // the same capture [`decode_block`]'s multi-TU branch makes |
| 18914 | // for `first_leaf_tx` (libaom `av1_get_tx_type`, blockd.h: |
| 18915 | // an inter-classified block's `PLANE_TYPE_UV` reads |
| 18916 | // `tx_type_map`, and intrabc is inter-classified). |
| 18917 | if first_leaf_tx.is_none() { |
| 18918 | first_leaf_tx = Some(fctx.luma_tx_type.with(std::cell::Cell::get)); |
| 18919 | } |
| 18920 | } |
| 18921 | } |
| 18922 | // lane-av1intrapred: an intrabc leaf whose var-tx tree split to |
| 18923 | // TX_4X4 never armed [`INTRABC_CHROMA_TX`], so its chroma reads fell |
| 18924 | // back to `default_intra_tx_type(DC_PRED)` = `DCT_DCT` and took the |
| 18925 | // 2D scan, the 2D eob row and the 2D nz contexts where libaom takes |
| 18926 | // the inherited luma type's (R5's first fork: mi (36,66), luma |
| 18927 | // (0,0) coded `H_ADST` off the 16-symbol inter set, chroma U then |
| 18928 | // decoded its `eob` from the wrong CDF row). Same arm the |
| 18929 | // `resolved != 4` branch above runs for its whole-leaf unit. |
| 18930 | if intrabc_dv.is_some() { |
| 18931 | if let Some(t) = first_leaf_tx { |
| 18932 | fctx.intrabc_chroma_tx.with(|c| c.set(Some(t))); |
| 18933 | } |
| 18934 | } |
| 18935 | let ac = alpha.map(|_| cfl_src(px, py, 8)); |
| 18936 | // lane-av1-422h: same ss_x-1 dc-vote sampling as the whole-leaf arm |
| 18937 | // above -- the doubled above sum crosses zero at mi(2,24) plane 2. |
| 18938 | let chroma_around = if chroma_422 { |
| 18939 | neighbours.around_mi_422_chroma(leaf_mi, 8, 8) |
| 18940 | } else { |
| 18941 | neighbours.around_mi(leaf_mi, 8) |
| 18942 | }; |
| 18943 | // lane-av1intrapred: the non-vacuity counter -- armed slot consulted |
| 18944 | // by this arm's chroma reads means the frame-copy + inherited-type |
| 18945 | // route ran; zero at gate time means the fixture stopped reaching it. |
| 18946 | if fctx.intrabc_chroma_tx.with(std::cell::Cell::get).is_some() { |
| 18947 | hit!(INTRABC_TX4_CHROMA_COPY_HITS); |
| 18948 | } |
| 18949 | if chroma_422 { |
| 18950 | // lane-av1-422d: the chroma unit does NOT follow the luma var-tx |
| 18951 | // split -- `uv_txsize` is `av1_get_max_uv_txsize` of the block, |
| 18952 | // still the one 4x8 rect unit per plane at ss (1,0). |
| 18953 | u_grid = leaf8_chroma422_unit( |
| 18954 | dec, cdfs, chroma_around[1], uv_predict_mode, angle_delta_uv, reach, |
| 18955 | 1, alpha.zip(ac).map(|((au, _), ac)| (au, ac)), false, |
| 18956 | intrabc_bufs.as_ref().map(|(_, ub, _)| ub.clone()) |
| 18957 | .or_else(|| palette_uv_bufs.as_ref().map(|(ub, _)| ub.clone())), |
| 18958 | u, cpx, cpy, smooth_neighbor_uv, fctx, |
| 18959 | )?; |
| 18960 | v_grid = leaf8_chroma422_unit( |
| 18961 | dec, cdfs, chroma_around[2], uv_predict_mode, angle_delta_uv, reach, |
| 18962 | 2, alpha.zip(ac).map(|((_, av), ac)| (av, ac)), false, |
| 18963 | intrabc_bufs.as_ref().map(|(_, _, vb)| vb.clone()) |
| 18964 | .or_else(|| palette_uv_bufs.as_ref().map(|(_, vb)| vb.clone())), |
| 18965 | v, cpx, cpy, smooth_neighbor_uv, fctx, |
| 18966 | )?; |
| 18967 | } else { |
| 18968 | if let Some((ub, _)) = &palette_uv_bufs { |
| 18969 | set_palette_pred(ub.clone(), fctx); |
| 18970 | } |
| 18971 | u_grid = read_plane( |
| 18972 | dec, |
| 18973 | cdfs, |
| 18974 | cset, |
| 18975 | cscan, |
| 18976 | 1, |
| 18977 | chroma_around[1], |
| 18978 | mode, |
| 18979 | uv_predict_mode, |
| 18980 | angle_delta_uv, |
| 18981 | reach, |
| 18982 | u, |
| 18983 | cpx, |
| 18984 | cpy, |
| 18985 | csz, |
| 18986 | csz, |
| 18987 | base_q_idx, |
| 18988 | alpha.zip(ac).map(|((au, _), ac)| (au, ac)), |
| 18989 | None, |
| 18990 | None, |
| 18991 | smooth_neighbor_uv, fctx, |
| 18992 | )?; |
| 18993 | if let Some((_, vb)) = &palette_uv_bufs { |
| 18994 | set_palette_pred(vb.clone(), fctx); |
| 18995 | } |
| 18996 | v_grid = read_plane( |
| 18997 | dec, |
| 18998 | cdfs, |
| 18999 | cset, |
| 19000 | cscan, |
| 19001 | 2, |
| 19002 | chroma_around[2], |
| 19003 | mode, |
| 19004 | uv_predict_mode, |
| 19005 | angle_delta_uv, |
| 19006 | reach, |
| 19007 | v, |
| 19008 | cpx, |
| 19009 | cpy, |
| 19010 | csz, |
| 19011 | csz, |
| 19012 | base_q_idx, |
| 19013 | alpha.zip(ac).map(|((_, av), ac)| (av, ac)), |
| 19014 | None, |
| 19015 | None, |
| 19016 | smooth_neighbor_uv, fctx, |
| 19017 | )?; |
| 19018 | } |
| 19019 | // The two chroma reads above are the only consulters of this leaf's |
| 19020 | // armed [`INTRABC_CHROMA_TX`]; leaving it set would make the NEXT |
| 19021 | // leaf's chroma inherit this leaf's luma type instead of its own |
| 19022 | // derivation (class armed-slot-leak-into-next-leaf, seen live: the |
| 19023 | // regular-intra leaf after an intrabc TX4 leaf took the intrabc |
| 19024 | // luma's `V_ADST` for its chroma eob row). |
| 19025 | if intrabc_dv.is_some() { |
| 19026 | fctx.intrabc_chroma_tx.with(|c| c.set(None)); |
| 19027 | } |
| 19028 | // `record_split_luma` expects a `record`-style `(r, c)` position in |
| 19029 | // `SUB`-grid units and rescales it by `SUB / MI`; `leaf_mi` is |
| 19030 | // already in mi units, so calling it here double-scaled the |
| 19031 | // position. Plane 0 is already correct per-TU from the |
| 19032 | // `record_mi_luma` calls above; this inlines `record_mi`'s tail |
| 19033 | // (spec `get_txb_ctx_general`'s neighbour-magnitude bookkeeping) for |
| 19034 | // the two chroma planes only, at `leaf_mi` directly, side_mi=2 (this |
| 19035 | // leaf's own 8x8 side over `MI`). |
| 19036 | let side_mi = 2; |
| 19037 | for cell in 0..side_mi { |
| 19038 | neighbours.left_side_mi[leaf_mi.0 + cell] = 8; |
| 19039 | neighbours.above_side_mi[leaf_mi.1 + cell] = 8; |
| 19040 | } |
| 19041 | let round_up_even = |n: usize| n.div_ceil(2) * 2; |
| 19042 | let bound_h = round_up_even(neighbours.mi_rows); |
| 19043 | let bound_w = round_up_even(neighbours.mi_cols); |
| 19044 | let u_state = neighbour_state(&u_grid); |
| 19045 | let v_state = neighbour_state(&v_grid); |
| 19046 | for cell in 0..side_mi { |
| 19047 | if cell < side_mi.min(bound_h.saturating_sub(leaf_mi.0)) { |
| 19048 | neighbours.left[leaf_mi.0 + cell][1] = u_state; |
| 19049 | neighbours.left[leaf_mi.0 + cell][2] = v_state; |
| 19050 | } |
| 19051 | if cell < side_mi.min(bound_w.saturating_sub(leaf_mi.1)) { |
| 19052 | neighbours.above[leaf_mi.1 + cell][1] = u_state; |
| 19053 | neighbours.above[leaf_mi.1 + cell][2] = v_state; |
| 19054 | } |
| 19055 | } |
| 19056 | // This leaf's own palette state into the mi-granular above/left bands |
| 19057 | // (sizes 0 when it used none, which is what clears a previous block's |
| 19058 | // palette out of the next block's ctx and colour cache). |
| 19059 | { |
| 19060 | neighbours.record_palette_y_rect(leaf_mi, 8, 8, pal_y_size, pal_y_colors); |
| 19061 | neighbours.record_palette_uv_rect(leaf_mi, 8, 8, pal_uv_size, pal_uv_colors); |
| 19062 | } |
| 19063 | neighbours.fill_skip_grid(leaf_mi, 2, skip); |
| 19064 | neighbours.fill_lf_grid(leaf_mi, 2, 4, 0, fctx); |
| 19065 | // lane-t900 r32: `set_txfm_ctxs` for an 8x8 leaf -- see `decode_block`'s |
| 19066 | // own copy; only an intrabc var-tx read consumes these bands on the intra |
| 19067 | // path, so the write is armed by `allow_intrabc`. |
| 19068 | if allow_intrabc { |
| 19069 | set_txfm_ctxs(neighbours, leaf_mi, 4, 2, 2, skip && intrabc_dv.is_some()); |
| 19070 | } |
| 19071 | neighbours.record_mode_mi(leaf_mi.0, leaf_mi.1, 2, 2, mode); |
| 19072 | record_intrabc_mi(leaf_mi.0, leaf_mi.1, 2, intrabc_dv, neighbours, fctx); |
| 19073 | return Ok(mode); |
| 19074 | } |
| 19075 | neighbours.record_mi(leaf_mi, 8, &[&luma_grid[..], &u_grid[..], &v_grid[..]]); |
| 19076 | // This leaf's own palette state into the mi-granular above/left bands |
| 19077 | // (sizes 0 when it used none, which is what clears a previous block's |
| 19078 | // palette out of the next block's ctx and colour cache). |
| 19079 | { |
| 19080 | neighbours.record_palette_y_rect(leaf_mi, 8, 8, pal_y_size, pal_y_colors); |
| 19081 | neighbours.record_palette_uv_rect(leaf_mi, 8, 8, pal_uv_size, pal_uv_colors); |
| 19082 | } |
| 19083 | hit!(INTER8_SKIP_BAND_HITS); |
| 19084 | neighbours.fill_skip_grid(leaf_mi, 2, skip); |
| 19085 | // lane-kf900 r1: the leaf's own RESOLVED transform size, never the literal |
| 19086 | // block side. libaom calls `set_txfm_ctxs(mbmi->tx_size, ..., skip_txfm && |
| 19087 | // is_inter_block(mbmi), xd)` (decodemv.c) -- for an INTRA block that skip |
| 19088 | // term is always 0, so a skipped 8x8 leaf whose `tx_depth` resolved to |
| 19089 | // TX_4X4 still publishes 4 to the next block's `get_tx_size_context` |
| 19090 | // (pred_common.h:342). Writing 8 here made the 8x16 strip below the leaf |
| 19091 | // read `tx_size_cat1` row 2 where libaom reads row 1 -- same alphabet, |
| 19092 | // wrong CDF row, so the msac range diverged mid-frame with no refusal |
| 19093 | // (class `wrong-alphabet-same-value`; the ReelB key frame at |
| 19094 | // -ss 900, first bad luma sample row 186 col 1803). |
| 19095 | neighbours.fill_lf_grid(leaf_mi, 2, resolved as u8, 0, fctx); |
| 19096 | // lane-t900 r32 / lane-av1txbands: `set_txfm_ctxs` for an 8x8 leaf -- see |
| 19097 | // `decode_block`'s own copy for why this stays beside |
| 19098 | // [`Neighbours::fill_lf_grid_rect`]'s publish (the skipped-intrabc |
| 19099 | // BLOCK-size term). |
| 19100 | if allow_intrabc { |
| 19101 | set_txfm_ctxs(neighbours, leaf_mi, resolved, 2, 2, skip && intrabc_dv.is_some()); |
| 19102 | } |
| 19103 | neighbours.record_mode_mi(leaf_mi.0, leaf_mi.1, 2, 2, mode); |
| 19104 | // The DV predictor of a LATER intrabc block is the ordinary `INTRA_FRAME` |
| 19105 | // MV stack over this grid (`av1_find_mv_refs`), so an 8x8 leaf has to |
| 19106 | // publish its own cells exactly like `decode_block` does. |
| 19107 | record_intrabc_mi(leaf_mi.0, leaf_mi.1, 2, intrabc_dv, neighbours, fctx); |
| 19108 | Ok(mode) |
| 19109 | } |
| 19110 | |
| 19111 | /// lane-av1-422d: ONE 4:2:2 chroma transform unit of an 8x8 leaf |
| 19112 | /// ([`decode_leaf8`]). At ss (1,0) the plane block is 4x8 and |
| 19113 | /// `av1_get_max_uv_txsize(BLOCK_8X8)` resolves to |
| 19114 | /// `max_txsize_rect_lookup[BLOCK_4X8]` == TX_4X8, so the WHOLE plane block is |
| 19115 | /// a single rect unit -- one txb_skip + one scan per plane in the |
| 19116 | /// [`TxbSet::ChromaRect8x4`] class [`decode_rect_split`] proves for the |
| 19117 | /// shape. Mirrors [`sub8_leaf_chroma444`]'s rect arm at the leaf's halved |
| 19118 | /// position: the entropy context is the leaf's own 8x8 luma-span |
| 19119 | /// above/left state (the `chroma_tiled` arm's convention; no `+3` |
| 19120 | /// plane-block-bigger term -- the unit covers the plane block exactly), the |
| 19121 | /// CFL source is the leaf's whole 8x8 luma block, and the plane's predicted |
| 19122 | /// mode supplies the mode-derived default `tx_type` unless an armed |
| 19123 | /// [`INTRABC_CHROMA_TX`] slot carries the intrabc luma's own (the caller |
| 19124 | /// arms and disarms it, exactly as it does around its square reads). |
| 19125 | #[allow(clippy::too_many_arguments)] |
| 19126 | fn leaf8_chroma422_unit( |
| 19127 | dec: &mut SymbolDecoder, |
| 19128 | cdfs: &mut Cdfs, |
| 19129 | around: (bool, bool, i32), |
| 19130 | uv_predict_mode: usize, |
| 19131 | angle_delta_uv: i32, |
| 19132 | reach: Reach, |
| 19133 | plane_idx: usize, |
| 19134 | cfl: Option<(i32, CflSrc)>, |
| 19135 | skip: bool, |
| 19136 | // The prediction override the square arms feed through |
| 19137 | // `set_palette_pred`: an intrabc leaf's frame copy at the DV takes |
| 19138 | // priority over a palette block's colour-index map. |
| 19139 | pred_override: Option<Vec<u16>>, |
| 19140 | // Unread, like [`read_plane`]'s own: the borrow proves the caller holds |
| 19141 | // the plane exclusively while the unit's coefficients are parsed. |
| 19142 | _plane: &mut PlaneBuf<'static>, |
| 19143 | cpx: usize, |
| 19144 | cpy: usize, |
| 19145 | smooth_neighbor_uv: bool, fctx: &crate::decode::FrameCtx, |
| 19146 | ) -> Result<Grid> { |
| 19147 | // The unit's prediction runs whether or not a residual follows it -- a |
| 19148 | // skipped leaf still predicts (the CfL contribution survives a skip), |
| 19149 | // and a skipped intrabc leaf still copies the frame at its DV. |
| 19150 | if let Some(buf) = pred_override { |
| 19151 | set_palette_pred(buf, fctx); |
| 19152 | } |
| 19153 | if skip { |
| 19154 | push_intra_rect( |
| 19155 | plane_idx, cpx, cpy, 4, 8, uv_predict_mode, angle_delta_uv, reach, |
| 19156 | &ZERO_RESIDUAL[..4 * 8], cfl, None, smooth_neighbor_uv, fctx, |
| 19157 | ); |
| 19158 | return Ok(Grid::Zero(4 * 8)); |
| 19159 | } |
| 19160 | let default_tx = fctx |
| 19161 | .intrabc_chroma_tx |
| 19162 | .with(std::cell::Cell::get) |
| 19163 | .unwrap_or_else(|| default_intra_tx_type(uv_predict_mode as u8)); |
| 19164 | let mut coding = cdfs.txb(TxbSet::ChromaRect8x4, uv_predict_mode); |
| 19165 | let (levels, tx_type) = read_coeffs_rect( |
| 19166 | dec, &mut coding, &SCAN_4X8, 4, 8, |
| 19167 | usize::from(around.0) + usize::from(around.1), |
| 19168 | dc_sign_ctx(around.2), default_tx, |
| 19169 | )?; |
| 19170 | let residual = dequant_and_inverse_typed_wh( |
| 19171 | &levels, 4, 8, crate::decode::bit_depth(fctx), block_q_idx(fctx), |
| 19172 | plane_q_delta(plane_idx, fctx).0, plane_q_delta(plane_idx, fctx).1, |
| 19173 | tx_type, block_iqmatrix(fctx, plane_idx, 4, 8, tx_type), |
| 19174 | ); |
| 19175 | push_intra_rect( |
| 19176 | plane_idx, cpx, cpy, 4, 8, uv_predict_mode, angle_delta_uv, reach, |
| 19177 | &residual, cfl, None, smooth_neighbor_uv, fctx, |
| 19178 | ); |
| 19179 | Ok(levels) |
| 19180 | } |
| 19181 | |
| 19182 | /// `read_intra_frame_mode_info`'s mode-info read for one `BLOCK_4X4` leaf |
| 19183 | /// below an 8x8 partition (spec `decode_partition`'s `bSize < BLOCK_8X8` |
| 19184 | /// bottom, where no partition symbol is read at all -- `decode_block` runs |
| 19185 | /// directly). No palette (`av1_allow_palette` needs both dims >= 8, spec |
| 19186 | /// blockd.h) and no `read_tx_size` (a sub-8x8 block's transform is always |
| 19187 | /// its own bsize, spec 5.11.16). `has_chroma` follows `is_chroma_reference` |
| 19188 | /// (spec/libaom `av1_common_int.h`): for 4:2:0, only the bottom-right leaf |
| 19189 | /// of the 2x2 4x4 group carries chroma syntax at all. |
| 19190 | #[allow(clippy::too_many_arguments)] |
| 19191 | fn read_intra_mode_sub8( |
| 19192 | dec: &mut SymbolDecoder, |
| 19193 | cdfs: &mut Cdfs, |
| 19194 | above_mode: usize, |
| 19195 | left_mode: usize, |
| 19196 | has_chroma: bool, |
| 19197 | enable_filter_intra: bool, |
| 19198 | // [`filter_intra_size_class_rect`] for the LEAF's own shape: 0 for a |
| 19199 | // `BLOCK_4X4` split leaf, 8/9 for a 4x8/8x4 rect leaf. Reading the flag |
| 19200 | // from the wrong row is the same VALUE with a different interval, so it |
| 19201 | // desyncs a block or two later, not here (class `wrong-alphabet-same-value`). |
| 19202 | fi_class: usize, |
| 19203 | skip_ctx: usize, |
| 19204 | allow_intrabc: bool, |
| 19205 | mi_r: usize, |
| 19206 | mi_c: usize, |
| 19207 | // The leaf's own mi footprint for `set_segment_id`: 1x1 for a `BLOCK_4X4` |
| 19208 | // split leaf, 2x1/1x2 for an 8x4/4x8 one. Stamping 1x1 for a rect leaf |
| 19209 | // left the second mi column at segment 0, which reads back as a |
| 19210 | // neighbour's `cdf_index`/prediction two blocks later. |
| 19211 | seg_w_mi: usize, |
| 19212 | seg_h_mi: usize, fctx: &crate::decode::FrameCtx, |
| 19213 | ) -> Result<(bool, usize, i32, Option<(usize, i32, Option<(i32, i32)>)>, Option<usize>, Option<(i32, i32)>)> { |
| 19214 | // lane-av1mono: a monochrome leaf codes no chroma mode (libaom |
| 19215 | // `decodemv.c:933`), so fold `NumPlanes == 1` into `has_chroma`. |
| 19216 | let has_chroma = has_chroma && !mono(fctx); |
| 19217 | let trace = crate::envflags::env_flag!("EC_AV1_TRACE"); |
| 19218 | let ec_istep = crate::envflags::env_flag!("EC_TRACE_MODE_STEP"); |
| 19219 | if ec_istep { |
| 19220 | eprintln!("EC_IMODE mi_row={mi_r} mi_col={mi_c} fn=sub8 rng={}", dec.debug_state().0); |
| 19221 | } |
| 19222 | // Same `EC_ISTEP` ladder the square/rect readers print, so a cross-decoder |
| 19223 | // range diff lands on the ELEMENT inside a 4x4 leaf, not just the block. |
| 19224 | macro_rules! istep { |
| 19225 | ($name:literal, $val:expr) => { |
| 19226 | if ec_istep { |
| 19227 | eprintln!("EC_ISTEP mi_row={mi_r} mi_col={mi_c} name={} val={} rng={}", $name, $val, dec.debug_state().0); |
| 19228 | } |
| 19229 | }; |
| 19230 | } |
| 19231 | // lane-seg2: same `intra_segment_id` placement as the square and rect |
| 19232 | // readers (spec 5.11.6) -- a `BLOCK_4X4` leaf codes its own `segment_id` |
| 19233 | // like any other block, over its own 1x1 mi footprint. Dropping it here |
| 19234 | // both ate a symbol (the tile desynced at the frame's first sub-8x8 |
| 19235 | // split) and left `cur_segment_id` on the previous block, so the leaf |
| 19236 | // dequantized with a neighbour's `SEG_LVL_ALT_Q`. |
| 19237 | if seg_id_pre_skip(fctx) { |
| 19238 | intra_segment_id(dec, cdfs, mi_r, mi_c, seg_w_mi, seg_h_mi, false, fctx); |
| 19239 | } |
| 19240 | let skip = dec.symbol(&mut cdfs.skip[skip_ctx]) != 0; |
| 19241 | if trace { |
| 19242 | eprintln!("TRACE sub8 skip mi=({mi_r},{mi_c}) ctx={skip_ctx} value={} rng={}", skip as i32, dec.debug_state().0); |
| 19243 | } |
| 19244 | istep!("skip", skip as i32); |
| 19245 | if !seg_id_pre_skip(fctx) { |
| 19246 | intra_segment_id(dec, cdfs, mi_r, mi_c, seg_w_mi, seg_h_mi, skip, fctx); |
| 19247 | } |
| 19248 | maybe_read_cdef_idx(dec, mi_r, mi_c, (2, 2), skip, fctx); |
| 19249 | istep!("cdef", 0); |
| 19250 | maybe_read_delta_q(dec, cdfs, mi_r, mi_c, false, skip, fctx); |
| 19251 | maybe_read_delta_lf(dec, cdfs, mi_r, mi_c, false, skip, fctx); |
| 19252 | istep!("dq", 0); |
| 19253 | if allow_intrabc { |
| 19254 | let use_intrabc = dec.symbol(&mut cdfs.intrabc) != 0; |
| 19255 | if ec_istep { |
| 19256 | eprintln!("EC_ISTEP mi_row={mi_r} mi_col={mi_c} name=intrabc val={} rng={}", u8::from(use_intrabc), dec.debug_state().0); |
| 19257 | } |
| 19258 | if use_intrabc { |
| 19259 | // lane-av1txr: `read_intrabc_info` (spec 5.11.13) reads the block |
| 19260 | // vector right after the flag and RETURNS -- no y/uv mode, angle |
| 19261 | // delta, palette, CfL or filter-intra syntax follows, and the |
| 19262 | // block is reconstructed by the INTER path (libaom's |
| 19263 | // `is_inter_block` counts an intrabc block), so the caller takes |
| 19264 | // the frame-copy prediction + inter residual. |
| 19265 | let dv = read_intrabc_dv(dec, cdfs, mi_r, mi_c, (seg_w_mi, seg_h_mi), fctx); |
| 19266 | // lane-av1txr: the DV travels back in the RETURN value -- it is |
| 19267 | // NOT published through `FrameCtx::intrabc_dv`. That slot is the |
| 19268 | // SQUARE paths' handoff (`decode_block`/`decode_leaf8` arm it, |
| 19269 | // then `take()` it in the same call), so a sub-8x8 leaf that |
| 19270 | // armed it left the vector standing for the NEXT >=8x8 block: |
| 19271 | // `warped.obu`'s (38,60) 8x8 intra block took it as its own |
| 19272 | // intrabc and read an inter var-tx `txfm_split` where libaom read |
| 19273 | // an intra `tx_depth` (class `stale-slot-handoff`). |
| 19274 | hit!(INTRABC_HITS); |
| 19275 | return Ok((skip, DC_PRED, 0, None, None, Some(dv))); |
| 19276 | } |
| 19277 | } |
| 19278 | let above_ctx = INTRA_MODE_CTX[above_mode]; |
| 19279 | let left_ctx = INTRA_MODE_CTX[left_mode]; |
| 19280 | let mode = dec.symbol(&mut cdfs.kf_y_mode[above_ctx][left_ctx]); |
| 19281 | istep!("mode", mode); |
| 19282 | if trace { |
| 19283 | eprintln!("TRACE sub8 y_mode mi=({mi_r},{mi_c}) ctx=({above_ctx},{left_ctx}) value={mode} rng={}", dec.debug_state().0); |
| 19284 | } |
| 19285 | // No `angle_delta` at all below 8x8: spec 5.11.6's `intra_angle_info_y`/ |
| 19286 | // `_uv` are gated on `MiSize >= BLOCK_8X8` (libaom `av1_use_angle_delta`, |
| 19287 | // blockd.h), so a directional mode on a `BLOCK_4X4` leaf carries no delta |
| 19288 | // symbol. Reading one consumed a symbol the encoder never wrote and |
| 19289 | // desynced the tile at the very next element. |
| 19290 | let angle_delta_y = 0; |
| 19291 | let chroma = if has_chroma { |
| 19292 | let uv_mode = dec.symbol(&mut cdfs.uv_mode_cfl[mode]); |
| 19293 | if trace { |
| 19294 | eprintln!("TRACE sub8 uv_mode value={uv_mode} rng={}", dec.debug_state().0); |
| 19295 | } |
| 19296 | if (9..=12).contains(&uv_mode) { |
| 19297 | hit!(SMOOTH_UV_HITS); |
| 19298 | } |
| 19299 | let alpha = if uv_mode == UV_CFL_PRED { |
| 19300 | Some(read_cfl_alphas(dec, cdfs)) |
| 19301 | } else { |
| 19302 | None |
| 19303 | }; |
| 19304 | if (V_PRED..=D67_PRED).contains(&uv_mode) { |
| 19305 | hit!(DIRECTIONAL_UV_HITS); |
| 19306 | } |
| 19307 | // See the luma comment above: no angle delta below 8x8. |
| 19308 | let angle_delta_uv = 0; |
| 19309 | Some((uv_mode, angle_delta_uv, alpha)) |
| 19310 | } else { |
| 19311 | None |
| 19312 | }; |
| 19313 | let mut filter_intra = None; |
| 19314 | if mode == DC_PRED && enable_filter_intra { |
| 19315 | let use_filter_intra = dec.symbol(&mut cdfs.filter_intra[fi_class]) != 0; |
| 19316 | if trace { |
| 19317 | eprintln!("TRACE sub8 use_filter_intra value={}", use_filter_intra as i32); |
| 19318 | } |
| 19319 | if use_filter_intra { |
| 19320 | hit!(FILTER_INTRA_HITS); |
| 19321 | let fi_mode = dec.symbol(&mut cdfs.filter_intra_mode); |
| 19322 | filter_intra = Some(fi_mode); |
| 19323 | } |
| 19324 | } |
| 19325 | Ok((skip, mode, angle_delta_y, chroma, filter_intra, None)) |
| 19326 | } |
| 19327 | /// 4:4:4 sub-8x8 key-frame leaf chroma (lane-av1-444). At subsampling 0/0 |
| 19328 | /// `is_chroma_reference` (av1_common_int.h:1459) is unconditional, so EVERY |
| 19329 | /// 4x4 / 4x8 / 8x4 leaf codes its own full-resolution chroma unit right |
| 19330 | /// after its own luma (`read_intra_mode_sub8` already read this leaf's own |
| 19331 | /// `uv_mode`), not the one shared 4x4 unit [`decode_leaf_split4`] / |
| 19332 | /// [`decode_leaf_rect8`]'s group tails decode at 4:2:0. Mirrors those tails |
| 19333 | /// arm for arm at leaf scale: a skipped leaf keeps its CfL contribution, an |
| 19334 | /// intrabc leaf predicts a frame copy at its DV with the luma `tx_type` |
| 19335 | /// inherited through [`INTRABC_CHROMA_TX`] (the caller arms it right after |
| 19336 | /// the luma read). The chroma position, reach and entropy-context span are |
| 19337 | /// the leaf's own (ss 0/0); the 4x8/8x4 unit reads the same |
| 19338 | /// `ChromaRect8x4` tables [`decode_rect_split`] proves for that shape. |
| 19339 | /// Records the leaf's own `uv_mode` map entry and chroma above/left state, |
| 19340 | /// and returns the (u, v) grids for the caller's coarse slots. |
| 19341 | #[allow(clippy::too_many_arguments)] |
| 19342 | fn sub8_leaf_chroma444( |
| 19343 | dec: &mut SymbolDecoder, |
| 19344 | cdfs: &mut Cdfs, |
| 19345 | neighbours: &mut Neighbours, |
| 19346 | lmi: (usize, usize), |
| 19347 | bw: usize, |
| 19348 | bh: usize, |
| 19349 | uv_mode: usize, |
| 19350 | alpha: Option<(i32, i32)>, |
| 19351 | skip: bool, |
| 19352 | intrabc_dv: Option<(i32, i32)>, |
| 19353 | y: &PlaneBuf<'static>, |
| 19354 | u: &mut PlaneBuf<'static>, |
| 19355 | v: &mut PlaneBuf<'static>, |
| 19356 | scan4: &[u16], |
| 19357 | base_q_idx: u8, |
| 19358 | smooth_neighbor_uv: bool, |
| 19359 | fctx: &crate::decode::FrameCtx, |
| 19360 | ) -> Result<(Grid, Grid)> { |
| 19361 | let uv_predict_mode = if uv_mode == UV_CFL_PRED { DC_PRED } else { uv_mode }; |
| 19362 | let (w_mi, h_mi) = (bw / MI, bh / MI); |
| 19363 | neighbours.record_uv_mode_mi(lmi.0, lmi.1, w_mi, h_mi, uv_predict_mode); |
| 19364 | let (px, py) = (lmi.1 * MI, lmi.0 * MI); |
| 19365 | let square = (bw, bh) == (4, 4); |
| 19366 | // `Reach::of_rect` has no 4x4 table (`rect_reach_tables` starts at 4x8); |
| 19367 | // the square unit takes the leaf's own `Reach::of`, as its luma does. |
| 19368 | let reach = if square { |
| 19369 | Reach::of(4, px, py, y.width, y.height, fctx) |
| 19370 | } else { |
| 19371 | Reach::of_rect(bw, bh, px, py, y.width, y.height, fctx) |
| 19372 | }; |
| 19373 | let grids = if skip { |
| 19374 | let ac = alpha.map(|_| cfl_src_rect(px, py, bw, bh)); |
| 19375 | if square { |
| 19376 | push_intra(1, px, py, 4, uv_predict_mode, 0, reach, &ZERO_RESIDUAL[..16], alpha.zip(ac).map(|((au, _), ac)| (au, ac)), None, smooth_neighbor_uv, fctx); |
| 19377 | push_intra(2, px, py, 4, uv_predict_mode, 0, reach, &ZERO_RESIDUAL[..16], alpha.zip(ac).map(|((_, av), ac)| (av, ac)), None, smooth_neighbor_uv, fctx); |
| 19378 | } else { |
| 19379 | push_intra_rect(1, px, py, bw, bh, uv_predict_mode, 0, reach, &ZERO_RESIDUAL[..bw * bh], alpha.zip(ac).map(|((au, _), ac)| (au, ac)), None, smooth_neighbor_uv, fctx); |
| 19380 | push_intra_rect(2, px, py, bw, bh, uv_predict_mode, 0, reach, &ZERO_RESIDUAL[..bw * bh], alpha.zip(ac).map(|((_, av), ac)| (av, ac)), None, smooth_neighbor_uv, fctx); |
| 19381 | } |
| 19382 | // The same arm-without-clear route the 4:2:0 tails count: a skipped |
| 19383 | // leaf reads no coefficient, so an armed intrabc slot is dropped. |
| 19384 | if fctx.intrabc_chroma_tx.with(std::cell::Cell::get).is_some() { |
| 19385 | hit!(SKIPPED_INTRABC_CHROMA_ARM_HITS); |
| 19386 | fctx.intrabc_chroma_tx.with(|c| c.set(None)); |
| 19387 | } |
| 19388 | (Grid::Zero(bw * bh), Grid::Zero(bw * bh)) |
| 19389 | } else if let Some(dv) = intrabc_dv { |
| 19390 | // Frame copy at the DV; the residual still reads the ordinary chroma |
| 19391 | // tables with the inherited luma `tx_type` (see the 4:2:0 tails). |
| 19392 | let mut ub = vec![0u16; bw * bh]; |
| 19393 | mc::predict_with_filter( |
| 19394 | &u.data, u.width, u.true_width, u.true_height, |
| 19395 | mv_to_q4(px, dv.1, 0), mv_to_q4(py, dv.0, 0), |
| 19396 | bw, bh, mc::InterpFilterKind::Bilinear, &mut ub, fctx, |
| 19397 | ); |
| 19398 | let mut vb = vec![0u16; bw * bh]; |
| 19399 | mc::predict_with_filter( |
| 19400 | &v.data, v.width, v.true_width, v.true_height, |
| 19401 | mv_to_q4(px, dv.1, 0), mv_to_q4(py, dv.0, 0), |
| 19402 | bw, bh, mc::InterpFilterKind::Bilinear, &mut vb, fctx, |
| 19403 | ); |
| 19404 | let around = neighbours.around_mi_rect(lmi, bw, bh); |
| 19405 | let pair = if square { |
| 19406 | set_palette_pred(ub, fctx); |
| 19407 | let ug = read_plane( |
| 19408 | dec, cdfs, TxbSet::Chroma4, scan4, 1, around[1], DC_PRED, DC_PRED, |
| 19409 | 0, reach, u, px, py, 4, TX4, base_q_idx, |
| 19410 | None, None, None, smooth_neighbor_uv, fctx, |
| 19411 | )?; |
| 19412 | set_palette_pred(vb, fctx); |
| 19413 | let vg = read_plane( |
| 19414 | dec, cdfs, TxbSet::Chroma4, scan4, 2, around[2], DC_PRED, DC_PRED, |
| 19415 | 0, reach, v, px, py, 4, TX4, base_q_idx, |
| 19416 | None, None, None, smooth_neighbor_uv, fctx, |
| 19417 | )?; |
| 19418 | (ug, vg) |
| 19419 | } else { |
| 19420 | let scan: &[u16] = if bw > bh { &SCAN_8X4 } else { &SCAN_4X8 }; |
| 19421 | let default_tx = |
| 19422 | fctx.intrabc_chroma_tx.with(std::cell::Cell::get).unwrap_or(TxType::DctDct); |
| 19423 | let mut out = (Grid::Zero(bw * bh), Grid::Zero(bw * bh)); |
| 19424 | for (plane_idx, buf) in [(1usize, ub), (2usize, vb)] { |
| 19425 | let skip_ctx = |
| 19426 | usize::from(around[plane_idx].0) + usize::from(around[plane_idx].1); |
| 19427 | let mut coding = cdfs.txb(TxbSet::ChromaRect8x4, DC_PRED); |
| 19428 | let (levels, tx_type) = read_coeffs_rect( |
| 19429 | dec, &mut coding, scan, bw, bh, skip_ctx, |
| 19430 | dc_sign_ctx(around[plane_idx].2), default_tx, |
| 19431 | )?; |
| 19432 | let residual = dequant_and_inverse_typed_wh( |
| 19433 | &levels, bw, bh, crate::decode::bit_depth(fctx), block_q_idx(fctx), |
| 19434 | plane_q_delta(plane_idx, fctx).0, plane_q_delta(plane_idx, fctx).1, |
| 19435 | tx_type, |
| 19436 | block_iqmatrix(fctx, plane_idx, bw, bh, tx_type), |
| 19437 | ); |
| 19438 | set_palette_pred(buf, fctx); |
| 19439 | push_intra_rect( |
| 19440 | plane_idx, px, py, bw, bh, DC_PRED, 0, reach, &residual, |
| 19441 | None, None, smooth_neighbor_uv, fctx, |
| 19442 | ); |
| 19443 | if plane_idx == 1 { out.0 = levels; } else { out.1 = levels; } |
| 19444 | } |
| 19445 | out |
| 19446 | }; |
| 19447 | fctx.intrabc_chroma_tx.with(|c| c.set(None)); |
| 19448 | pair |
| 19449 | } else { |
| 19450 | let ac = alpha.map(|_| cfl_src_rect(px, py, bw, bh)); |
| 19451 | let around = neighbours.around_mi_rect(lmi, bw, bh); |
| 19452 | if square { |
| 19453 | let ug = read_plane( |
| 19454 | dec, cdfs, TxbSet::Chroma4, scan4, 1, around[1], uv_mode, uv_predict_mode, |
| 19455 | 0, reach, u, px, py, 4, TX4, base_q_idx, |
| 19456 | alpha.zip(ac).map(|((au, _), ac)| (au, ac)), None, None, smooth_neighbor_uv, fctx, |
| 19457 | )?; |
| 19458 | let vg = read_plane( |
| 19459 | dec, cdfs, TxbSet::Chroma4, scan4, 2, around[2], uv_mode, uv_predict_mode, |
| 19460 | 0, reach, v, px, py, 4, TX4, base_q_idx, |
| 19461 | alpha.zip(ac).map(|((_, av), ac)| (av, ac)), None, None, smooth_neighbor_uv, fctx, |
| 19462 | )?; |
| 19463 | (ug, vg) |
| 19464 | } else { |
| 19465 | // `av1_get_max_uv_txsize(BLOCK_4X8/8X4)` at ss 0/0 is the rect |
| 19466 | // transform itself; its `tx_type` is mode-derived with the |
| 19467 | // <32 set (`decode_rect_split`'s own rule for this shape). |
| 19468 | let scan: &[u16] = if bw > bh { &SCAN_8X4 } else { &SCAN_4X8 }; |
| 19469 | let default_tx = default_intra_tx_type(uv_predict_mode as u8); |
| 19470 | let mut out = (Grid::Zero(bw * bh), Grid::Zero(bw * bh)); |
| 19471 | for plane_idx in 1..=2usize { |
| 19472 | let skip_ctx = |
| 19473 | usize::from(around[plane_idx].0) + usize::from(around[plane_idx].1); |
| 19474 | let mut coding = cdfs.txb(TxbSet::ChromaRect8x4, uv_predict_mode); |
| 19475 | let (levels, tx_type) = read_coeffs_rect( |
| 19476 | dec, &mut coding, scan, bw, bh, skip_ctx, |
| 19477 | dc_sign_ctx(around[plane_idx].2), default_tx, |
| 19478 | )?; |
| 19479 | let residual = dequant_and_inverse_typed_wh( |
| 19480 | &levels, bw, bh, crate::decode::bit_depth(fctx), block_q_idx(fctx), |
| 19481 | plane_q_delta(plane_idx, fctx).0, plane_q_delta(plane_idx, fctx).1, |
| 19482 | tx_type, |
| 19483 | block_iqmatrix(fctx, plane_idx, bw, bh, tx_type), |
| 19484 | ); |
| 19485 | let cfl = alpha |
| 19486 | .zip(ac) |
| 19487 | .map(|((au, av), ac)| (if plane_idx == 1 { au } else { av }, ac)); |
| 19488 | push_intra_rect( |
| 19489 | plane_idx, px, py, bw, bh, uv_predict_mode, 0, reach, &residual, |
| 19490 | cfl, None, smooth_neighbor_uv, fctx, |
| 19491 | ); |
| 19492 | if plane_idx == 1 { out.0 = levels; } else { out.1 = levels; } |
| 19493 | } |
| 19494 | out |
| 19495 | } |
| 19496 | }; |
| 19497 | // libaom `av1_set_entropy_contexts` stamps above/left PER TRANSFORM UNIT: |
| 19498 | // the leaf's own chroma span, not the 8x8 group's. |
| 19499 | let (u_grid, v_grid) = grids; |
| 19500 | let u_state = neighbour_state(&u_grid); |
| 19501 | let v_state = neighbour_state(&v_grid); |
| 19502 | for cell in 0..h_mi { |
| 19503 | if lmi.0 + cell < neighbours.mi_rows { |
| 19504 | neighbours.left[lmi.0 + cell][1] = u_state; |
| 19505 | neighbours.left[lmi.0 + cell][2] = v_state; |
| 19506 | } |
| 19507 | } |
| 19508 | for cell in 0..w_mi { |
| 19509 | if lmi.1 + cell < neighbours.mi_cols { |
| 19510 | neighbours.above[lmi.1 + cell][1] = u_state; |
| 19511 | neighbours.above[lmi.1 + cell][2] = v_state; |
| 19512 | } |
| 19513 | } |
| 19514 | Ok((u_grid, v_grid)) |
| 19515 | } |
| 19516 | |
| 19517 | |
| 19518 | /// [`sub8_leaf_chroma444`] for 4:2:2 (lane-av1-422g): an even-mi-column 4x4 |
| 19519 | /// leaf is its plane's own chroma reference (`is_chroma_reference`'s |
| 19520 | /// `!(mi_col & ss_x)` at ss_x 1 -- every even-column leaf, not 4:2:0's single |
| 19521 | /// bottom-right one) and codes ONE TX_4X4 chroma unit per plane right after |
| 19522 | /// its luma: `ss_size_lookup[BLOCK_4X4][1][0]` is BLOCK_4X4, so the (2, 4) |
| 19523 | /// plane area rounds up to the square 4x4 read. The unit sits at the chroma |
| 19524 | /// position (`px >> ss_x`), and its 4 chroma-px width spans TWO luma mi |
| 19525 | /// columns, so the context reads go through |
| 19526 | /// [`Neighbours::around_mi_422_chroma`] and the entropy stamp covers both |
| 19527 | /// columns with the unit's whole-unit state. |
| 19528 | #[allow(clippy::too_many_arguments)] |
| 19529 | fn sub8_leaf_chroma422( |
| 19530 | dec: &mut SymbolDecoder, |
| 19531 | cdfs: &mut Cdfs, |
| 19532 | neighbours: &mut Neighbours, |
| 19533 | lmi: (usize, usize), |
| 19534 | uv_mode: usize, |
| 19535 | angle_delta_uv: i32, |
| 19536 | alpha: Option<(i32, i32)>, |
| 19537 | skip: bool, |
| 19538 | intrabc_dv: Option<(i32, i32)>, |
| 19539 | y: &PlaneBuf<'static>, |
| 19540 | u: &mut PlaneBuf<'static>, |
| 19541 | v: &mut PlaneBuf<'static>, |
| 19542 | scan4: &[u16], |
| 19543 | base_q_idx: u8, |
| 19544 | smooth_neighbor_uv: bool, |
| 19545 | fctx: &crate::decode::FrameCtx, |
| 19546 | ) -> Result<(Grid, Grid)> { |
| 19547 | let uv_predict_mode = if uv_mode == UV_CFL_PRED { DC_PRED } else { uv_mode }; |
| 19548 | neighbours.record_uv_mode_mi(lmi.0, lmi.1, 1, 1, uv_predict_mode); |
| 19549 | let (px, py) = (lmi.1 * MI, lmi.0 * MI); |
| 19550 | let (cpx, cpy) = (px >> ss_x(fctx), py >> ss_y(fctx)); |
| 19551 | // lane-av1-422h: the entropy-context anchor is the unit's FLOOR chroma |
| 19552 | // cell (`pd->above_entropy_context + (mi_col >> ss_x)`): an ODD-column |
| 19553 | // leaf's 4-px unit starts at a chroma px that is NOT cell aligned, and |
| 19554 | // both its ctx read and its ctx write live in the cell containing that |
| 19555 | // start -- one luma mi column to the LEFT of the leaf's own. Even-column |
| 19556 | // (cell-aligned) leaves keep their own column. The state write spans the |
| 19557 | // anchor's two luma mi columns, so the second (odd) leaf of a split group |
| 19558 | // overwrites exactly the cell the first one wrote, as libaom's |
| 19559 | // `av1_set_entropy_contexts` per-TU writes do. |
| 19560 | let ctx_mi = (lmi.0, lmi.1 & !1); |
| 19561 | // The square unit takes the leaf's own `Reach::of` (its luma's), the way |
| 19562 | // [`decode_leaf8`]'s 422 arm passes the enclosing block's luma reach. |
| 19563 | let reach = Reach::of(4, px, py, y.width, y.height, fctx); |
| 19564 | let chroma_around = neighbours.around_mi_422_chroma(ctx_mi, 8, 4); |
| 19565 | let ac = alpha.map(|_| cfl_src_rect(px, py, 8, 4)); |
| 19566 | let grids: (Grid, Grid) = if skip { |
| 19567 | // A skipped chroma-reference leaf still predicts (the CfL contribution |
| 19568 | // survives a skip); no coefficient symbol exists. |
| 19569 | if alpha.is_some() { |
| 19570 | hit!(CHROMA_SKIP_CFL_HITS); |
| 19571 | hit!(SKIP_CFL_HITS); |
| 19572 | } |
| 19573 | push_intra(1, cpx, cpy, 4, uv_predict_mode, angle_delta_uv, reach, &ZERO_RESIDUAL[..16], alpha.zip(ac).map(|((au, _), ac)| (au, ac)), None, smooth_neighbor_uv, fctx); |
| 19574 | push_intra(2, cpx, cpy, 4, uv_predict_mode, angle_delta_uv, reach, &ZERO_RESIDUAL[..16], alpha.zip(ac).map(|((_, av), ac)| (av, ac)), None, smooth_neighbor_uv, fctx); |
| 19575 | // The same arm-without-clear route the 4:2:0 tails count: a skipped |
| 19576 | // leaf reads no coefficient, so an armed intrabc slot is dropped. |
| 19577 | if fctx.intrabc_chroma_tx.with(std::cell::Cell::get).is_some() { |
| 19578 | hit!(SKIPPED_INTRABC_CHROMA_ARM_HITS); |
| 19579 | fctx.intrabc_chroma_tx.with(|c| c.set(None)); |
| 19580 | } |
| 19581 | (Grid::Zero(16), Grid::Zero(16)) |
| 19582 | } else if let Some(dv) = intrabc_dv { |
| 19583 | hit!(CHROMA422_SUB8_HITS); |
| 19584 | // Frame copy at the DV; the residual still reads the ordinary chroma |
| 19585 | // tables with the inherited luma `tx_type` (see the 4:2:0 tails). |
| 19586 | let mut ub = vec![0u16; 16]; |
| 19587 | mc::predict_with_filter( |
| 19588 | &u.data, u.width, u.true_width, u.true_height, |
| 19589 | mv_to_q4(cpx, dv.1, ss_x(fctx)), mv_to_q4(cpy, dv.0, ss_y(fctx)), |
| 19590 | 4, 4, mc::InterpFilterKind::Bilinear, &mut ub, fctx, |
| 19591 | ); |
| 19592 | let mut vb = vec![0u16; 16]; |
| 19593 | mc::predict_with_filter( |
| 19594 | &v.data, v.width, v.true_width, v.true_height, |
| 19595 | mv_to_q4(cpx, dv.1, ss_x(fctx)), mv_to_q4(cpy, dv.0, ss_y(fctx)), |
| 19596 | 4, 4, mc::InterpFilterKind::Bilinear, &mut vb, fctx, |
| 19597 | ); |
| 19598 | set_palette_pred(ub, fctx); |
| 19599 | let ug = read_plane( |
| 19600 | dec, cdfs, TxbSet::Chroma4, scan4, 1, chroma_around[1], DC_PRED, DC_PRED, |
| 19601 | 0, reach, u, cpx, cpy, 4, TX4, base_q_idx, |
| 19602 | None, None, None, smooth_neighbor_uv, fctx, |
| 19603 | )?; |
| 19604 | set_palette_pred(vb, fctx); |
| 19605 | let vg = read_plane( |
| 19606 | dec, cdfs, TxbSet::Chroma4, scan4, 2, chroma_around[2], DC_PRED, DC_PRED, |
| 19607 | 0, reach, v, cpx, cpy, 4, TX4, base_q_idx, |
| 19608 | None, None, None, smooth_neighbor_uv, fctx, |
| 19609 | )?; |
| 19610 | fctx.intrabc_chroma_tx.with(|c| c.set(None)); |
| 19611 | (ug, vg) |
| 19612 | } else { |
| 19613 | hit!(CHROMA422_SUB8_HITS); |
| 19614 | let ug = read_plane( |
| 19615 | dec, cdfs, TxbSet::Chroma4, scan4, 1, chroma_around[1], uv_mode, uv_predict_mode, |
| 19616 | angle_delta_uv, reach, u, cpx, cpy, 4, TX4, base_q_idx, |
| 19617 | alpha.zip(ac).map(|((au, _), ac)| (au, ac)), None, None, smooth_neighbor_uv, fctx, |
| 19618 | )?; |
| 19619 | let vg = read_plane( |
| 19620 | dec, cdfs, TxbSet::Chroma4, scan4, 2, chroma_around[2], uv_mode, uv_predict_mode, |
| 19621 | angle_delta_uv, reach, v, cpx, cpy, 4, TX4, base_q_idx, |
| 19622 | alpha.zip(ac).map(|((_, av), ac)| (av, ac)), None, None, smooth_neighbor_uv, fctx, |
| 19623 | )?; |
| 19624 | (ug, vg) |
| 19625 | }; |
| 19626 | // libaom `av1_set_entropy_contexts` stamps above/left PER TRANSFORM UNIT: |
| 19627 | // the unit's own chroma span -- 1 mi row tall, TWO luma mi columns wide |
| 19628 | // (one chroma column) -- not the 8x8 group's. |
| 19629 | let (u_grid, v_grid) = grids; |
| 19630 | let u_state = neighbour_state(&u_grid); |
| 19631 | let v_state = neighbour_state(&v_grid); |
| 19632 | if lmi.0 < neighbours.mi_rows { |
| 19633 | neighbours.left[ctx_mi.0][1] = u_state; |
| 19634 | neighbours.left[ctx_mi.0][2] = v_state; |
| 19635 | } |
| 19636 | for cell in 0..2 { |
| 19637 | if ctx_mi.1 + cell < neighbours.mi_cols { |
| 19638 | neighbours.above[ctx_mi.1 + cell][1] = u_state; |
| 19639 | neighbours.above[ctx_mi.1 + cell][2] = v_state; |
| 19640 | } |
| 19641 | } |
| 19642 | Ok((u_grid, v_grid)) |
| 19643 | } |
| 19644 | |
| 19645 | |
| 19646 | /// Decodes a `PARTITION_SPLIT` of one 8x8 block into four `BLOCK_4X4` leaves |
| 19647 | /// (spec `decode_partition`'s recursion bottom), sharing [`decode_leaf8`]'s |
| 19648 | /// own signature so its caller's `prev_leaf`/`above_mode`/`left_mode` |
| 19649 | /// bookkeeping needs no change. Each leaf reads its own luma mode info, |
| 19650 | /// neighboured off the *previously decoded leaf in this same group* on |
| 19651 | /// whichever axis is adjacent (mirroring [`decode_leaf8`]'s own `prev_leaf` |
| 19652 | /// pattern, one level deeper); chroma is read and reconstructed exactly |
| 19653 | /// once, on the last (bottom-right) leaf, at the whole 8x8 group's own 4x4 |
| 19654 | /// chroma unit -- [`read_intra_mode_sub8`]'s doc has the `is_chroma_reference` |
| 19655 | /// derivation. |
| 19656 | #[allow(clippy::too_many_arguments)] |
| 19657 | fn decode_leaf_split4( |
| 19658 | dec: &mut SymbolDecoder, |
| 19659 | cdfs: &mut Cdfs, |
| 19660 | neighbours: &mut Neighbours, |
| 19661 | outer_at: (usize, usize), |
| 19662 | leaf_mi: (usize, usize), |
| 19663 | scan4: &[u16], |
| 19664 | prev_leaf: Option<((usize, usize), usize)>, |
| 19665 | y: &mut PlaneBuf<'static>, |
| 19666 | u: &mut PlaneBuf<'static>, |
| 19667 | v: &mut PlaneBuf<'static>, |
| 19668 | base_q_idx: u8, |
| 19669 | enable_filter_intra: bool, |
| 19670 | allow_intrabc: bool, |
| 19671 | reduced_tx_set: bool, |
| 19672 | // Returns `(below_mode, right_mode)`: the mode a neighbour BELOW this 8x8 |
| 19673 | // group sees (its bottom-left 4x4, libaom reads mi(row-1, col)) and the |
| 19674 | // one a neighbour to its RIGHT sees (its top-right 4x4, mi(row, col-1))., fctx: &crate::decode::FrameCtx |
| 19675 | fctx: &crate::decode::FrameCtx, |
| 19676 | ) -> Result<(usize, usize)> { |
| 19677 | hit!(SUB8_SPLIT_HITS); |
| 19678 | let (r, c) = outer_at; |
| 19679 | let mut above_mode = neighbours.above_mode[c]; |
| 19680 | let mut left_mode = neighbours.left_mode[r]; |
| 19681 | if let Some(((pr, pc), pmode)) = prev_leaf { |
| 19682 | if pc == leaf_mi.1 && leaf_mi.0 == pr + 2 { |
| 19683 | above_mode = pmode; |
| 19684 | } else if pr == leaf_mi.0 && leaf_mi.1 == pc + 2 { |
| 19685 | left_mode = pmode; |
| 19686 | } |
| 19687 | } |
| 19688 | // See `decode_leaf8`'s own `smooth_neighbor_uv`: the chroma edge-filter |
| 19689 | // type is the chroma neighbours' `uv_mode`, mi-exact where recorded. |
| 19690 | let smooth_neighbor_uv = neighbours.smooth_uv_neighbour(leaf_mi.0, leaf_mi.1, r, c, fctx); |
| 19691 | let mut leaf_modes = [0usize; 4]; |
| 19692 | let mut leaf_skips = [false; 4]; |
| 19693 | let mut last_uv: Option<(usize, i32, Option<(i32, i32)>)> = None; |
| 19694 | // lane-av1txr: the chroma-reference (i==3) leaf's block vector when that |
| 19695 | // leaf is an intrabc block -- its prediction/reconstruction below is a |
| 19696 | // frame copy at this DV, not an intra one. |
| 19697 | let mut last_intrabc: Option<(i32, i32)> = None; |
| 19698 | // 4:4:4 (lane-av1-444): `is_chroma_reference` (av1_common_int.h:1459) is |
| 19699 | // unconditional at subsampling 0/0, so EVERY 4x4 leaf reads its own |
| 19700 | // `uv_mode` and codes its own full-resolution 4x4 chroma unit right |
| 19701 | // after its luma -- there is no shared group unit at all. |
| 19702 | let chroma_444 = ss_x(fctx) == 0 && ss_y(fctx) == 0; |
| 19703 | // lane-av1-422g: at ss (1,0) every even-mi-column leaf is its plane's own |
| 19704 | // chroma reference (`is_chroma_reference`'s `!(mi_col & ss_x)`) and codes |
| 19705 | // one TX_4X4 chroma unit per plane; odd-column leaves read no chroma. |
| 19706 | let chroma_422 = ss_x(fctx) == 1 && ss_y(fctx) == 0; |
| 19707 | // The last leaf's own `uv_predict_mode`, for the coarse above/left slots |
| 19708 | // the 4:2:0 tail fills from the group's single unit. |
| 19709 | let mut last_uv444 = DC_PRED; |
| 19710 | let mut last_uv422 = DC_PRED; |
| 19711 | for i in 0..4usize { |
| 19712 | let (dr, dc) = (i / 2, i % 2); |
| 19713 | let lmi = (leaf_mi.0 + dr, leaf_mi.1 + dc); |
| 19714 | // Neighbour modes at real mi granularity: inside the group the |
| 19715 | // sibling leaf, outside it the split-leaf map when the adjacent mi |
| 19716 | // was itself a 4x4 leaf, else the coarse 16x16 slot (see |
| 19717 | // `sub8_mode_col`'s doc). |
| 19718 | let leaf_above = if dr == 0 { |
| 19719 | neighbours.mode_above_mi(lmi.0, lmi.1).unwrap_or(above_mode) |
| 19720 | } else { |
| 19721 | leaf_modes[i - 2] |
| 19722 | }; |
| 19723 | let leaf_left = if dc == 0 { |
| 19724 | neighbours.mode_left_mi(lmi.0, lmi.1).unwrap_or(left_mode) |
| 19725 | } else { |
| 19726 | leaf_modes[i - 1] |
| 19727 | }; |
| 19728 | let has_chroma = if chroma_444 { |
| 19729 | true |
| 19730 | } else if chroma_422 { |
| 19731 | // lane-av1-422h: `is_chroma_reference`'s column clause at ss_x 1 is |
| 19732 | // `(mi_col & 1)` for a BLOCK_4X4 leaf -- the ODD-column leaf is the |
| 19733 | // chroma reference (oracle, mi(30,42) split group: uv_mode read at |
| 19734 | // mi(30,43) and mi(31,43) only). The even-column rule this |
| 19735 | // replaces came from 422f's pre-partition-trace misreading. |
| 19736 | (lmi.1 & 1) == 1 |
| 19737 | } else { |
| 19738 | i == 3 |
| 19739 | }; |
| 19740 | let skip_ctx = neighbours.skip_txfm_ctx(lmi.0, lmi.1); |
| 19741 | let (skip, mode, angle_delta_y, chroma, filter_intra, intrabc) = read_intra_mode_sub8( |
| 19742 | dec, cdfs, leaf_above, leaf_left, has_chroma, enable_filter_intra, |
| 19743 | filter_intra_size_class(4).expect("BLOCK_4X4 has its own filter_intra row"), |
| 19744 | skip_ctx, allow_intrabc, lmi.0, lmi.1, 1, 1, fctx, |
| 19745 | )?; |
| 19746 | leaf_modes[i] = mode; |
| 19747 | leaf_skips[i] = skip; |
| 19748 | neighbours.record_mode_mi(lmi.0, lmi.1, 1, 1, mode); |
| 19749 | // lane-av1txr: libaom `parse_decode_block`'s `set_txfm_ctxs` for this |
| 19750 | // `BLOCK_4X4` leaf -- EVERY block publishes its transform size into |
| 19751 | // the TXFM context bands, and an intrabc block's later `txfm_split` |
| 19752 | // context is read off them (`txfm_partition_context`). A 4x4 never |
| 19753 | // signals a tx size, so it is always TX_4X4 (`set_txfm_ctxs`'s |
| 19754 | // `skip && is_inter` branch writes the block's own 4 pixels -- the |
| 19755 | // same value). |
| 19756 | set_txfm_ctxs(neighbours, lmi, 4, 1, 1, false); |
| 19757 | let smooth_neighbor = is_smooth_mode(leaf_above) || is_smooth_mode(leaf_left); |
| 19758 | if smooth_neighbor { |
| 19759 | hit!(SMOOTH_LUMA_HITS); |
| 19760 | } |
| 19761 | let (px, py) = (lmi.1 * MI, lmi.0 * MI); |
| 19762 | let reach = Reach::of(4, px, py, y.width, y.height, fctx); |
| 19763 | if let Some(dv) = intrabc { |
| 19764 | // lane-av1txr: an intrabc `BLOCK_4X4` leaf -- prediction is the |
| 19765 | // CURRENT frame at the block vector (`predict_inter_block`'s |
| 19766 | // intrabc arm, spec 7.11.3.4), residual the INTER coefficient set |
| 19767 | // (libaom reads `tx_type` off `inter_ext_tx_cdf` for |
| 19768 | // `is_inter_block`, which counts intrabc). Published into |
| 19769 | // `intrabc_dv` so the group's chroma arm (below, when the |
| 19770 | // chroma-reference leaf is the intrabc one) and |
| 19771 | // [`record_intrabc_mi`] see it. |
| 19772 | flush_recon(fctx, y, u, v); |
| 19773 | let mut yb = vec![0u16; 16]; |
| 19774 | mc::predict_with_filter( |
| 19775 | &y.data, y.width, y.true_width, y.true_height, |
| 19776 | mv_to_q4(px, dv.1, 0), mv_to_q4(py, dv.0, 0), |
| 19777 | 4, 4, mc::InterpFilterKind::Bilinear, &mut yb, fctx, |
| 19778 | ); |
| 19779 | set_palette_pred(yb.clone(), fctx); |
| 19780 | if skip { |
| 19781 | push_intra(0, px, py, 4, DC_PRED, 0, reach, &ZERO_RESIDUAL[..16], None, None, smooth_neighbor, fctx); |
| 19782 | neighbours.record_mi_luma(lmi, 4, &ZERO_RESIDUAL[..16]); |
| 19783 | } else { |
| 19784 | let tu_around = neighbours.around_mi(lmi, 4)[0]; |
| 19785 | let tu_grid = read_plane( |
| 19786 | dec, |
| 19787 | cdfs, |
| 19788 | if reduced_tx_set { TxbSet::Luma4Inter } else { TxbSet::Luma4InterSet1 }, |
| 19789 | scan4, |
| 19790 | 0, |
| 19791 | tu_around, |
| 19792 | DC_PRED, |
| 19793 | DC_PRED, |
| 19794 | 0, |
| 19795 | reach, |
| 19796 | y, |
| 19797 | px, |
| 19798 | py, |
| 19799 | 4, |
| 19800 | 4, |
| 19801 | base_q_idx, |
| 19802 | None, |
| 19803 | None, |
| 19804 | None, |
| 19805 | smooth_neighbor, fctx, |
| 19806 | )?; |
| 19807 | neighbours.record_mi_luma(lmi, 4, &tu_grid); |
| 19808 | } |
| 19809 | neighbours.fill_skip_grid(lmi, 1, skip); |
| 19810 | neighbours.fill_lf_grid(lmi, 1, 4, 0, fctx); |
| 19811 | record_intrabc_mi(lmi.0, lmi.1, 1, Some(dv), neighbours, fctx); |
| 19812 | if has_chroma { |
| 19813 | // An intrabc block is an INTER block for `av1_get_tx_type`, so |
| 19814 | // the chroma inherits the LUMA's coded transform type |
| 19815 | // (`xd->tx_type_map` at the co-located luma position) -- the |
| 19816 | // luma `read_plane` above just set [`LUMA_TX_TYPE`]. Armed for |
| 19817 | // exactly this leaf's two chroma reads. |
| 19818 | fctx.intrabc_chroma_tx.with(|c| c.set(Some(fctx.luma_tx_type.with(std::cell::Cell::get)))); |
| 19819 | if chroma_444 { |
| 19820 | // lane-av1-444: this leaf codes its OWN 4x4 chroma unit |
| 19821 | // right here (frame copy at the DV); no group tail. |
| 19822 | sub8_leaf_chroma444( |
| 19823 | dec, cdfs, neighbours, lmi, 4, 4, DC_PRED, None, skip, |
| 19824 | Some(dv), y, u, v, scan4, base_q_idx, |
| 19825 | neighbours.smooth_uv_neighbour(lmi.0, lmi.1, r, c, fctx), fctx, |
| 19826 | )?; |
| 19827 | last_uv444 = DC_PRED; |
| 19828 | } else if chroma_422 { |
| 19829 | sub8_leaf_chroma422( |
| 19830 | dec, cdfs, neighbours, lmi, DC_PRED, 0, None, skip, |
| 19831 | Some(dv), y, u, v, scan4, base_q_idx, |
| 19832 | neighbours.smooth_uv_neighbour(lmi.0, lmi.1, r, c, fctx), fctx, |
| 19833 | )?; |
| 19834 | last_uv422 = DC_PRED; |
| 19835 | } else { |
| 19836 | last_uv = Some((DC_PRED, 0, None)); |
| 19837 | // lane-av1txbands: the group's chroma is the CHROMA-REFERENCE |
| 19838 | // leaf's own block -- libaom codes one chroma unit under the |
| 19839 | // last sub-block only (`is_chroma_reference`), so its prediction |
| 19840 | // is that leaf's `uv_mode` (CfL alpha and all) unless THAT leaf |
| 19841 | // is the intrabc one. Arming this from a non-chroma-reference |
| 19842 | // leaf's DV made `warped.obu`'s 8x8 group at mi(40,62) copy |
| 19843 | // frame bytes at mi(41,62)'s vector where aomdec predicts |
| 19844 | // `uv_mode=UV_CFL_PRED`: luma stayed byte-exact and only chroma |
| 19845 | // moved (class chroma-keyed-at-a-non-chroma-leaf). Mirrors |
| 19846 | // [`decode_leaf_rect8`]'s own placement. |
| 19847 | last_intrabc = Some(dv); |
| 19848 | } |
| 19849 | } |
| 19850 | continue; |
| 19851 | } |
| 19852 | if skip { |
| 19853 | push_intra(0, px, py, 4, mode, angle_delta_y, reach, &ZERO_RESIDUAL[..16], None, filter_intra, smooth_neighbor, fctx); |
| 19854 | neighbours.record_mi_luma(lmi, 4, &ZERO_RESIDUAL[..16]); |
| 19855 | } else { |
| 19856 | let tu_around = neighbours.around_mi(lmi, 4)[0]; |
| 19857 | // A `BLOCK_4X4` leaf's luma TU *is* the whole block, so libaom's |
| 19858 | // `get_txb_ctx` takes its `plane_bsize == txsize_to_bsize[tx_size]` |
| 19859 | // branch: `txb_skip_ctx = 0`, never the neighbour-magnitude table |
| 19860 | // (`txb_common.h:400`). Reading the table here decoded `all_zero` |
| 19861 | // off the wrong CDF on the very first leaf and desynced the tile. |
| 19862 | let tu_grid = read_plane( |
| 19863 | dec, |
| 19864 | cdfs, |
| 19865 | if reduced_tx_set { TxbSet::Luma4 } else { TxbSet::Luma4Set1 }, |
| 19866 | scan4, |
| 19867 | 0, |
| 19868 | tu_around, |
| 19869 | mode, |
| 19870 | mode, |
| 19871 | angle_delta_y, |
| 19872 | reach, |
| 19873 | y, |
| 19874 | px, |
| 19875 | py, |
| 19876 | 4, |
| 19877 | 4, |
| 19878 | base_q_idx, |
| 19879 | None, |
| 19880 | filter_intra, |
| 19881 | None, |
| 19882 | smooth_neighbor, fctx, |
| 19883 | )?; |
| 19884 | neighbours.record_mi_luma(lmi, 4, &tu_grid); |
| 19885 | } |
| 19886 | neighbours.fill_skip_grid(lmi, 1, skip); |
| 19887 | neighbours.fill_lf_grid(lmi, 1, 4, 0, fctx); |
| 19888 | if has_chroma { |
| 19889 | if chroma_444 { |
| 19890 | // lane-av1-444: the leaf's own 4x4 chroma unit, coded right |
| 19891 | // after its luma (no shared group unit at ss 0/0). |
| 19892 | let (uv_mode, _, alpha) = |
| 19893 | chroma.expect("4:4:4 reads uv_mode on every leaf"); |
| 19894 | sub8_leaf_chroma444( |
| 19895 | dec, cdfs, neighbours, lmi, 4, 4, uv_mode, alpha, skip, |
| 19896 | None, y, u, v, scan4, base_q_idx, |
| 19897 | neighbours.smooth_uv_neighbour(lmi.0, lmi.1, r, c, fctx), fctx, |
| 19898 | )?; |
| 19899 | last_uv444 = if uv_mode == UV_CFL_PRED { DC_PRED } else { uv_mode }; |
| 19900 | } else if chroma_422 { |
| 19901 | let (uv_mode, angle_delta_uv, alpha) = |
| 19902 | chroma.expect("4:2:2 reads uv_mode on even-column leaves"); |
| 19903 | sub8_leaf_chroma422( |
| 19904 | dec, cdfs, neighbours, lmi, uv_mode, angle_delta_uv, alpha, skip, |
| 19905 | None, y, u, v, scan4, base_q_idx, |
| 19906 | neighbours.smooth_uv_neighbour(lmi.0, lmi.1, r, c, fctx), fctx, |
| 19907 | )?; |
| 19908 | last_uv422 = if uv_mode == UV_CFL_PRED { DC_PRED } else { uv_mode }; |
| 19909 | } else { |
| 19910 | last_uv = chroma; |
| 19911 | } |
| 19912 | } |
| 19913 | } |
| 19914 | // lane-av1mono: as [`decode_leaf_rect8`] -- a monochrome frame has no |
| 19915 | // chroma to reconstruct and no uv_mode was read. |
| 19916 | let (u_grid, v_grid): (Grid, Grid) = if mono(fctx) { |
| 19917 | (Grid::Zero(16), Grid::Zero(16)) |
| 19918 | } else if chroma_444 { |
| 19919 | // lane-av1-444: every leaf coded and reconstructed its own chroma in |
| 19920 | // the loop above ([`sub8_leaf_chroma444`] stamped the mi-exact maps |
| 19921 | // and per-leaf entropy state); only the coarse slots remain, filled |
| 19922 | // from the bottom-right leaf like the 4:2:0 tail's single unit. |
| 19923 | neighbours.above_uv_mode[c] = last_uv444; |
| 19924 | neighbours.left_uv_mode[r] = last_uv444; |
| 19925 | (Grid::Zero(16), Grid::Zero(16)) |
| 19926 | } else if chroma_422 { |
| 19927 | // lane-av1-422g: each even-column leaf coded and reconstructed its |
| 19928 | // own TX_4X4 chroma unit in the loop above (sub8_leaf_chroma422 |
| 19929 | // stamped the per-leaf entropy state); only the coarse slots remain, |
| 19930 | // filled from the group's last chroma-reference leaf (bottom-left). |
| 19931 | neighbours.above_uv_mode[c] = last_uv422; |
| 19932 | neighbours.left_uv_mode[r] = last_uv422; |
| 19933 | (Grid::Zero(16), Grid::Zero(16)) |
| 19934 | } else { |
| 19935 | let (gpx, gpy) = (leaf_mi.1 * MI, leaf_mi.0 * MI); |
| 19936 | let (cpx, cpy) = (gpx >> ss_x(fctx), gpy >> ss_y(fctx)); |
| 19937 | let group_reach = Reach::of(8, gpx, gpy, y.width, y.height, fctx); |
| 19938 | let (uv_mode, angle_delta_uv, alpha) = |
| 19939 | last_uv.expect("i==3 always sets has_chroma, so chroma is always Some"); |
| 19940 | let uv_predict_mode = if uv_mode == UV_CFL_PRED { DC_PRED } else { uv_mode }; |
| 19941 | neighbours.above_uv_mode[c] = uv_predict_mode; |
| 19942 | neighbours.left_uv_mode[r] = uv_predict_mode; |
| 19943 | neighbours.record_uv_mode_mi(leaf_mi.0, leaf_mi.1, 2, 2, uv_predict_mode); |
| 19944 | let (u_grid, v_grid): (Grid, Grid) = if leaf_skips[3] { |
| 19945 | // A skipped chroma block still gets its CfL contribution: libaom's |
| 19946 | // `predict_and_reconstruct_intra_block` calls `cfl_predict_block` |
| 19947 | // from `av1_predict_intra_block_facade` (av1/common/reconintra.c:1719) |
| 19948 | // whenever `mbmi->uv_mode == UV_CFL_PRED`, before any residual is |
| 19949 | // added -- `skip_txfm` only drops the residual, never the alpha. |
| 19950 | // lane-reelckf and lane-kf1200 fixed THIS SAME arm independently; the |
| 19951 | // merge keeps one fix and feeds both lanes' counters so neither of |
| 19952 | // their gates goes vacuous. |
| 19953 | let ac = alpha.map(|_| cfl_src(gpx, gpy, 8)); |
| 19954 | if alpha.is_some() { |
| 19955 | hit!(CHROMA_SKIP_CFL_HITS); |
| 19956 | hit!(SKIP_CFL_HITS); |
| 19957 | } |
| 19958 | push_intra(1, cpx, cpy, 4, uv_predict_mode, angle_delta_uv, group_reach, &ZERO_RESIDUAL[..16], alpha.zip(ac).map(|((au, _), ac)| (au, ac)), None, smooth_neighbor_uv, fctx); |
| 19959 | push_intra(2, cpx, cpy, 4, uv_predict_mode, angle_delta_uv, group_reach, &ZERO_RESIDUAL[..16], alpha.zip(ac).map(|((_, av), ac)| (av, ac)), None, smooth_neighbor_uv, fctx); |
| 19960 | // lane-av1txbands: a SKIPPED chroma-reference leaf reads no chroma |
| 19961 | // coefficient, so it never consults the inherited luma transform type |
| 19962 | // -- but an intrabc chroma-reference leaf did arm |
| 19963 | // [`INTRABC_CHROMA_TX`] above (`has_chroma`), and the two coefficient |
| 19964 | // branches below are the only ones that disarm it. Left armed, the slot |
| 19965 | // picks the NEXT block's chroma scan and inverse transform (class |
| 19966 | // arm-without-clear-on-a-skip-route). Counted so a gate can tell a |
| 19967 | // stream that reaches this route from one that never does. |
| 19968 | if fctx.intrabc_chroma_tx.with(std::cell::Cell::get).is_some() { |
| 19969 | hit!(SKIPPED_INTRABC_CHROMA_ARM_HITS); |
| 19970 | fctx.intrabc_chroma_tx.with(|c| c.set(None)); |
| 19971 | } |
| 19972 | (Grid::Zero(16), Grid::Zero(16)) |
| 19973 | } else if let Some(dv) = last_intrabc { |
| 19974 | // lane-av1txr: the chroma-reference leaf was an intrabc block, so the |
| 19975 | // group's one chroma unit is a frame copy at that DV too (its residual |
| 19976 | // still reads the ordinary `Chroma4` coefficient tables -- libaom |
| 19977 | // codes a chroma `tx_type` nowhere). |
| 19978 | let mut ub = vec![0u16; 16]; |
| 19979 | mc::predict_with_filter( |
| 19980 | &u.data, u.width, u.true_width, u.true_height, |
| 19981 | mv_to_q4(cpx, dv.1, ss_x(fctx)), mv_to_q4(cpy, dv.0, ss_y(fctx)), |
| 19982 | 4, 4, mc::InterpFilterKind::Bilinear, &mut ub, fctx, |
| 19983 | ); |
| 19984 | let mut vb = vec![0u16; 16]; |
| 19985 | mc::predict_with_filter( |
| 19986 | &v.data, v.width, v.true_width, v.true_height, |
| 19987 | mv_to_q4(cpx, dv.1, ss_x(fctx)), mv_to_q4(cpy, dv.0, ss_y(fctx)), |
| 19988 | 4, 4, mc::InterpFilterKind::Bilinear, &mut vb, fctx, |
| 19989 | ); |
| 19990 | let chroma_around = neighbours.around_mi(leaf_mi, 8); |
| 19991 | set_palette_pred(ub, fctx); |
| 19992 | let ug = read_plane( |
| 19993 | dec, cdfs, TxbSet::Chroma4, scan4, 1, chroma_around[1], DC_PRED, DC_PRED, |
| 19994 | 0, group_reach, u, cpx, cpy, 4, TX4, base_q_idx, |
| 19995 | None, None, None, smooth_neighbor_uv, fctx, |
| 19996 | )?; |
| 19997 | set_palette_pred(vb, fctx); |
| 19998 | let vg = read_plane( |
| 19999 | dec, cdfs, TxbSet::Chroma4, scan4, 2, chroma_around[2], DC_PRED, DC_PRED, |
| 20000 | 0, group_reach, v, cpx, cpy, 4, TX4, base_q_idx, |
| 20001 | None, None, None, smooth_neighbor_uv, fctx, |
| 20002 | )?; |
| 20003 | fctx.intrabc_chroma_tx.with(|c| c.set(None)); |
| 20004 | (ug, vg) |
| 20005 | } else { |
| 20006 | let ac = alpha.map(|_| cfl_src(gpx, gpy, 8)); |
| 20007 | let chroma_around = neighbours.around_mi(leaf_mi, 8); |
| 20008 | let ug = read_plane( |
| 20009 | dec, cdfs, TxbSet::Chroma4, scan4, 1, chroma_around[1], uv_mode, uv_predict_mode, |
| 20010 | angle_delta_uv, group_reach, u, cpx, cpy, 4, TX4, base_q_idx, |
| 20011 | alpha.zip(ac).map(|((au, _), ac)| (au, ac)), None, None, smooth_neighbor_uv, fctx, |
| 20012 | )?; |
| 20013 | let vg = read_plane( |
| 20014 | dec, cdfs, TxbSet::Chroma4, scan4, 2, chroma_around[2], uv_mode, uv_predict_mode, |
| 20015 | angle_delta_uv, group_reach, v, cpx, cpy, 4, TX4, base_q_idx, |
| 20016 | alpha.zip(ac).map(|((_, av), ac)| (av, ac)), None, None, smooth_neighbor_uv, fctx, |
| 20017 | )?; |
| 20018 | (ug, vg) |
| 20019 | }; |
| 20020 | (u_grid, v_grid) |
| 20021 | }; |
| 20022 | // Chroma neighbour bookkeeping for the whole 8x8 group -- mirrors |
| 20023 | // [`decode_leaf8`]'s own tx4-chroma section (`record_split_luma`'s |
| 20024 | // rescaling is wrong for an already-mi-unit position, so this inlines |
| 20025 | // its tail directly at `leaf_mi`, side_mi=2), EXCEPT for the partition |
| 20026 | // context value itself. Unlike `decode_leaf8`'s TX4 branch (still one |
| 20027 | // `BLOCK_8X8`, `PARTITION_NONE`, transform depth only), this group is |
| 20028 | // genuinely four `BLOCK_4X4` blocks (`PARTITION_SPLIT` at the 8x8 |
| 20029 | // level, libaom `decode_partition`'s `bsize2`/`subsize` recursion |
| 20030 | // bottom). libaom's `update_ext_partition_context` writes |
| 20031 | // `partition_context_lookup[subsize]` at the 8x8 slot -- `{30,30}` for |
| 20032 | // `BLOCK_8X8` (bit0=0, what `decode_leaf8` already reproduces via |
| 20033 | // side_mi=8) but `{31,31}` for `BLOCK_4X4` (bit0=1) here, and |
| 20034 | // `partition_ctx_mi`'s `left_side_mi[mi_r]*2 <= side` needs |
| 20035 | // `left_side_mi <= 4` to read that bit as 1. Leaving this at 8 (this |
| 20036 | // round's bug) makes the very next sibling 8x8's `partition_w8` read |
| 20037 | // with the wrong context and desync immediately. |
| 20038 | let side_mi = 2; |
| 20039 | for cell in 0..side_mi { |
| 20040 | neighbours.left_side_mi[leaf_mi.0 + cell] = 4; |
| 20041 | neighbours.above_side_mi[leaf_mi.1 + cell] = 4; |
| 20042 | } |
| 20043 | // lane-av1-444: at 4:4:4 [`sub8_leaf_chroma444`] already stamped each |
| 20044 | // leaf's own span as it decoded; these placeholder grids carry no state. |
| 20045 | // lane-av1-422g: the 4:2:2 per-leaf units stamp the same way. |
| 20046 | if !chroma_444 && !chroma_422 { |
| 20047 | let round_up_even = |n: usize| n.div_ceil(2) * 2; |
| 20048 | let bound_h = round_up_even(neighbours.mi_rows); |
| 20049 | let bound_w = round_up_even(neighbours.mi_cols); |
| 20050 | let u_state = neighbour_state(&u_grid); |
| 20051 | let v_state = neighbour_state(&v_grid); |
| 20052 | for cell in 0..side_mi { |
| 20053 | if cell < side_mi.min(bound_h.saturating_sub(leaf_mi.0)) { |
| 20054 | neighbours.left[leaf_mi.0 + cell][1] = u_state; |
| 20055 | neighbours.left[leaf_mi.0 + cell][2] = v_state; |
| 20056 | } |
| 20057 | if cell < side_mi.min(bound_w.saturating_sub(leaf_mi.1)) { |
| 20058 | neighbours.above[leaf_mi.1 + cell][1] = u_state; |
| 20059 | neighbours.above[leaf_mi.1 + cell][2] = v_state; |
| 20060 | } |
| 20061 | } |
| 20062 | } |
| 20063 | // lane-dkey: a sub-8x8 leaf can never use palette (`av1_allow_palette` |
| 20064 | // needs BLOCK_8X8 and up), and libaom's mi grid says so -- its |
| 20065 | // `palette_size` is 0 in every one of these mi cells, so the NEXT block's |
| 20066 | // `av1_get_palette_mode_ctx` and colour cache read 0 here. This decoder |
| 20067 | // only ever WROTE palette state from the >=8x8 readers, so an 8x8 palette |
| 20068 | // block above a 4x4 group left its size/colours standing in the mi-granular |
| 20069 | // bands: the block below the group read `palette_y_mode` off CDF row 1 |
| 20070 | // where libaom reads row 0 (class [[cdf-row-held-constant]]), decoded a |
| 20071 | // palette libaom never coded, and then skipped the `use_filter_intra` |
| 20072 | // symbol that a palette block has none of -- one symbol short, tile |
| 20073 | // desynced. Publishing the group's own zeros is the clear. |
| 20074 | neighbours.record_palette_y_rect(leaf_mi, 8, 8, 0, [0u16; 8]); |
| 20075 | neighbours.record_palette_uv_rect(leaf_mi, 8, 8, 0, [0u16; 8]); |
| 20076 | Ok((leaf_modes[2], leaf_modes[1])) |
| 20077 | } |
| 20078 | |
| 20079 | /// Decodes a `PARTITION_VERT`/`PARTITION_HORZ` of one 8x8 block into two |
| 20080 | /// `BLOCK_4X8`/`BLOCK_8X4` leaves (lane-tx4x8), the last partition shape |
| 20081 | /// below 8x8 this decoder refused. Mirrors [`decode_leaf_split4`] element for |
| 20082 | /// element -- same mode reads ([`read_intra_mode_sub8`]: no angle delta, no |
| 20083 | /// tx-depth symbol below 8x8), same "chroma once, on the last sub-block, at |
| 20084 | /// the whole 8x8 group's 4x4 unit" rule (`is_chroma_reference` is false for |
| 20085 | /// every sub-block one mi wide or high but the last) -- differing only where |
| 20086 | /// the shape does: |
| 20087 | /// |
| 20088 | /// * the transform is a true `TX_4X8`/`TX_8X4` |
| 20089 | /// ([`dequant_and_inverse_typed_wh`], whose `abs(rect_type) == 1` scale and |
| 20090 | /// `av1_inv_txfm_shift_ls` row already cover this size pair); |
| 20091 | /// * the CDF set is [`TxbSet::LumaRect4x8`] (`get_txsize_entropy_ctx` squares |
| 20092 | /// up to `TX_8X8`) with a 32-position `eob_pt` and the `TX_4X4` `tx_type` |
| 20093 | /// row -- see that variant's doc comment; |
| 20094 | /// * the scan is [`SCAN_4X8`]/[`SCAN_8X4`], or their `mrow`/`mcol` siblings |
| 20095 | /// ([`class_scan_table_wh`]) when the coded `tx_type` is `V_DCT`/`H_DCT`; |
| 20096 | /// * `Reach` comes from libaom's own `has_tr_4x8`/`has_bl_8x4` rows |
| 20097 | /// ([`Reach::of_rect`]); |
| 20098 | /// * `update_partition_context` writes `partition_context_lookup[BLOCK_4X8]` |
| 20099 | /// = `{31, 30}` (width 4 above, height 8 left), not the `{31, 31}` a |
| 20100 | /// `BLOCK_4X4` split writes. |
| 20101 | /// |
| 20102 | /// Filter intra on one of these leaves is decoded (lane-fi8 r1): the flag is |
| 20103 | /// read from `BLOCK_4X8`/`BLOCK_8X4`'s own `cdf::FILTER_INTRA` row (classes |
| 20104 | /// 8/9, NOT `BLOCK_4X4`'s), the predictor is [`crate::intra::predict_filter_intra`] |
| 20105 | /// over the leaf's true `bw`x`bh` -- per 4x4 transform unit, with that unit's |
| 20106 | /// own edge, when `tx_depth` splits it -- and the coded `tx_type` row is |
| 20107 | /// [`fi_tx_row`]'s `fimode_to_intradir`, not the block's raw `DC_PRED`. |
| 20108 | #[allow(clippy::too_many_arguments)] |
| 20109 | fn decode_leaf_rect8( |
| 20110 | dec: &mut SymbolDecoder, |
| 20111 | cdfs: &mut Cdfs, |
| 20112 | neighbours: &mut Neighbours, |
| 20113 | outer_at: (usize, usize), |
| 20114 | leaf_mi: (usize, usize), |
| 20115 | vert: bool, |
| 20116 | scan4: &[u16], |
| 20117 | y: &mut PlaneBuf<'static>, |
| 20118 | u: &mut PlaneBuf<'static>, |
| 20119 | v: &mut PlaneBuf<'static>, |
| 20120 | base_q_idx: u8, |
| 20121 | enable_filter_intra: bool, |
| 20122 | allow_intrabc: bool, |
| 20123 | tx_select: bool, |
| 20124 | reduced_tx_set: bool, fctx: &crate::decode::FrameCtx, |
| 20125 | ) -> Result<(usize, usize)> { |
| 20126 | let (r, c) = outer_at; |
| 20127 | // lane-modectx r1: availability-correct fallback pair (see |
| 20128 | // `modes_above_left`), not the raw coarse bands. |
| 20129 | let (above_mode, left_mode) = neighbours.modes_above_left(r, c); |
| 20130 | let smooth_neighbor_uv = neighbours.smooth_uv_neighbour(leaf_mi.0, leaf_mi.1, r, c, fctx); |
| 20131 | let (bw, bh) = if vert { (4, 8) } else { (8, 4) }; |
| 20132 | let (w_mi, h_mi) = (bw / MI, bh / MI); |
| 20133 | let scan: &[u16] = if vert { &SCAN_4X8 } else { &SCAN_8X4 }; |
| 20134 | let mut leaf_modes = [0usize; 2]; |
| 20135 | let mut leaf_skips = [false; 2]; |
| 20136 | let mut last_uv: Option<(usize, i32, Option<(i32, i32)>)> = None; |
| 20137 | // lane-av1txr: as [`decode_leaf_split4`]'s own `last_intrabc`. |
| 20138 | let mut last_intrabc: Option<(i32, i32)> = None; |
| 20139 | // 4:4:4 (lane-av1-444): as [`decode_leaf_split4`] -- at subsampling 0/0 |
| 20140 | // EVERY 4x8/8x4 leaf reads its own `uv_mode` and codes its own |
| 20141 | // full-resolution rect chroma unit right after its luma. |
| 20142 | let chroma_444 = ss_x(fctx) == 0 && ss_y(fctx) == 0; |
| 20143 | let mut last_uv444 = DC_PRED; |
| 20144 | // lane-av1-422h: at ss (1,0) `is_chroma_reference` |
| 20145 | // (av1_common_int.h:1459) resolves per strip shape -- |
| 20146 | // `((mi_row & 1) || !(bh_mi & 1) || !ss_y) && ((mi_col & 1) || !(bw_mi & 1) || !ss_x)`: |
| 20147 | // a HORZ pair is two BLOCK_8X4 strips at the group's even column, so |
| 20148 | // BOTH strips are chroma references (`!(bw & 1)`); a VERT pair is two |
| 20149 | // BLOCK_4X8 strips, and the column clause keeps only the ODD-column |
| 20150 | // strip (i == 1, the same leaf the 4:2:0 rule reads chroma on). |
| 20151 | let chroma_422 = ss_x(fctx) == 1 && ss_y(fctx) == 0; |
| 20152 | let mut last_uv422 = DC_PRED; |
| 20153 | for i in 0..2usize { |
| 20154 | let lmi = if vert { |
| 20155 | (leaf_mi.0, leaf_mi.1 + i) |
| 20156 | } else { |
| 20157 | (leaf_mi.0 + i, leaf_mi.1) |
| 20158 | }; |
| 20159 | // Inside the pair the sibling is the neighbour on the split axis; |
| 20160 | // outside it the mi-granular map, else the coarse 16x16 slot. |
| 20161 | let leaf_above = if !vert && i == 1 { |
| 20162 | leaf_modes[0] |
| 20163 | } else { |
| 20164 | neighbours.mode_above_mi(lmi.0, lmi.1).unwrap_or(above_mode) |
| 20165 | }; |
| 20166 | let leaf_left = if vert && i == 1 { |
| 20167 | leaf_modes[0] |
| 20168 | } else { |
| 20169 | neighbours.mode_left_mi(lmi.0, lmi.1).unwrap_or(left_mode) |
| 20170 | }; |
| 20171 | let has_chroma = if chroma_444 { |
| 20172 | true |
| 20173 | } else if chroma_422 { |
| 20174 | !vert || i == 1 |
| 20175 | } else { |
| 20176 | i == 1 |
| 20177 | }; |
| 20178 | let skip_ctx = neighbours.skip_txfm_ctx(lmi.0, lmi.1); |
| 20179 | let (skip, mode, angle_delta_y, chroma, filter_intra, intrabc) = read_intra_mode_sub8( |
| 20180 | dec, cdfs, leaf_above, leaf_left, has_chroma, enable_filter_intra, |
| 20181 | filter_intra_size_class_rect(bw, bh) |
| 20182 | .expect("BLOCK_4X8/BLOCK_8X4 are inside av1_filter_intra_allowed_bsize"), |
| 20183 | skip_ctx, allow_intrabc, lmi.0, lmi.1, bw / 4, bh / 4, fctx, |
| 20184 | )?; |
| 20185 | leaf_modes[i] = mode; |
| 20186 | leaf_skips[i] = skip; |
| 20187 | neighbours.record_mode_mi(lmi.0, lmi.1, w_mi, h_mi, mode); |
| 20188 | let smooth_neighbor = is_smooth_mode(leaf_above) || is_smooth_mode(leaf_left); |
| 20189 | if let Some(dv) = intrabc { |
| 20190 | // lane-av1txr: an intrabc `BLOCK_4X8`/`BLOCK_8X4` leaf. The |
| 20191 | // prediction is a frame copy at the DV and the residual reads the |
| 20192 | // INTER `LumaRect8x4` set -- but with `TxMode::Select` the leaf |
| 20193 | // reads the INTER var-tx tree, exactly like every other intrabc |
| 20194 | // block: libaom's `block_signals_txsize` is `bsize > BLOCK_4X4` |
| 20195 | // (`blockd.h:1027`), TRUE at `BLOCK_4X8`/`BLOCK_8X4`, so |
| 20196 | // `parse_decode_block` sends an unskipped `inter_block_tx` block |
| 20197 | // (intrabc counts, `is_inter_block`) of a signalling bsize into |
| 20198 | // `read_tx_size_vartx` over `max_txsize_rect_lookup[BLOCK_4X8] == |
| 20199 | // TX_4X8`; only `BLOCK_4X4` reads no tx symbol at all. Measured on |
| 20200 | // `warped.obu` mi (37,62): reading NO symbol here left the |
| 20201 | // arithmetic decoder one symbol short and desynced the very next |
| 20202 | // block's `skip` (ours 60320 vs aomdec 48630, class |
| 20203 | // `symbol-consumption-gap`). |
| 20204 | let (px, py) = (lmi.1 * MI, lmi.0 * MI); |
| 20205 | let reach = Reach::of_rect(bw, bh, px, py, y.width, y.height, fctx); |
| 20206 | flush_recon(fctx, y, u, v); |
| 20207 | let mut luma_leaves: Vec<(usize, usize, usize, usize)> = Vec::new(); |
| 20208 | if tx_select && !skip { |
| 20209 | // `max_block_wide`/`max_block_high`: the frame's own mi grid, |
| 20210 | // so a leaf hanging over the right/bottom edge reads no split |
| 20211 | // symbol for the units outside it (as in `decode_block`). |
| 20212 | let mi_cols = 2 * y.true_width.div_ceil(8); |
| 20213 | let mi_rows = 2 * y.true_height.div_ceil(8); |
| 20214 | let max_w_mi = w_mi.min(mi_cols.saturating_sub(lmi.1)); |
| 20215 | let max_h_mi = h_mi.min(mi_rows.saturating_sub(lmi.0)); |
| 20216 | read_var_tx_size( |
| 20217 | dec, |
| 20218 | cdfs, |
| 20219 | neighbours, |
| 20220 | lmi, |
| 20221 | bw.max(bh), |
| 20222 | (max_w_mi, max_h_mi), |
| 20223 | (bw, bh), |
| 20224 | 0, |
| 20225 | 0, |
| 20226 | 0, |
| 20227 | &mut luma_leaves, |
| 20228 | ); |
| 20229 | hit!(RECT_INTRABC_VARTX_HITS); |
| 20230 | } else { |
| 20231 | luma_leaves.push((0, 0, bw, bh)); |
| 20232 | // The non-var-tx arm: a SKIPPED intrabc leaf (or a frame |
| 20233 | // outside `TxMode::Select`) publishes its own rect transform |
| 20234 | // (`set_txfm_ctxs`'s `skip && is_inter` branch writes the |
| 20235 | // block's own pixels -- the same size here). |
| 20236 | txfm_partition_update_rect(neighbours, lmi, (bw, bh), (bw, bh)); |
| 20237 | } |
| 20238 | // The transform type an intrabc block's CHROMA inherits is the |
| 20239 | // `tx_type_map` entry at the chroma unit's co-located luma |
| 20240 | // position (`av1_get_tx_type`, `blockd.h:1278`), i.e. the FIRST |
| 20241 | // luma unit the tree resolved. |
| 20242 | let single = luma_leaves.len() == 1; |
| 20243 | let mut chroma_tx = TxType::DctDct; |
| 20244 | let mut grid = Grid::Zero(bw * bh); |
| 20245 | for (li, &(tu_row, tu_col, tw, th)) in luma_leaves.iter().enumerate() { |
| 20246 | let tu_mi = (lmi.0 + tu_row, lmi.1 + tu_col); |
| 20247 | let (tu_px, tu_py) = (tu_mi.1 * MI, tu_mi.0 * MI); |
| 20248 | let tu_reach = if single { |
| 20249 | reach |
| 20250 | } else { |
| 20251 | tu_reach( |
| 20252 | bw, bh, tu_px - px, tu_py - py, tw, reach, px, py, y.width, y.height, fctx, |
| 20253 | ) |
| 20254 | }; |
| 20255 | let mut yb = vec![0u16; tw * th]; |
| 20256 | mc::predict_with_filter( |
| 20257 | &y.data, y.width, y.true_width, y.true_height, |
| 20258 | mv_to_q4(tu_px, dv.1, 0), mv_to_q4(tu_py, dv.0, 0), |
| 20259 | tw, th, mc::InterpFilterKind::Bilinear, &mut yb, fctx, |
| 20260 | ); |
| 20261 | set_palette_pred(yb, fctx); |
| 20262 | let tu_grid = if skip { |
| 20263 | let zeros = Grid::Zero(tw * th); |
| 20264 | if (tw, th) == (4, 4) { |
| 20265 | push_intra(0, tu_px, tu_py, 4, DC_PRED, 0, tu_reach, &zeros, None, None, smooth_neighbor, fctx); |
| 20266 | } else { |
| 20267 | push_intra_rect(0, tu_px, tu_py, tw, th, DC_PRED, 0, tu_reach, &zeros, None, None, smooth_neighbor, fctx); |
| 20268 | } |
| 20269 | zeros |
| 20270 | } else { |
| 20271 | // A unit smaller than its own block (the var-tx split) |
| 20272 | // reads its `txb_skip_ctx` off the neighbours; the |
| 20273 | // whole-leaf unit's `plane_bsize == txsize_to_bsize` fixes |
| 20274 | // it at 0. |
| 20275 | let (set, tu_scan, around, skip_ctx) = if (tw, th) == (4, 4) { |
| 20276 | ( |
| 20277 | inter_txbset_for(4, reduced_tx_set), |
| 20278 | scan4, |
| 20279 | neighbours.around_mi(tu_mi, 4)[0], |
| 20280 | neighbours.luma_skip_ctx(tu_mi, 1), |
| 20281 | ) |
| 20282 | } else { |
| 20283 | ( |
| 20284 | if reduced_tx_set { |
| 20285 | TxbSet::LumaRect8x4Inter |
| 20286 | } else { |
| 20287 | TxbSet::LumaRect8x4InterSet1 |
| 20288 | }, |
| 20289 | scan, |
| 20290 | neighbours.around_mi_rect(tu_mi, tw, th)[0], |
| 20291 | 0, |
| 20292 | ) |
| 20293 | }; |
| 20294 | let mut coding = cdfs.txb(set, DC_PRED); |
| 20295 | let (levels, tx_type) = read_coeffs_rect( |
| 20296 | dec, &mut coding, tu_scan, tw, th, skip_ctx, dc_sign_ctx(around.2), |
| 20297 | TxType::DctDct, |
| 20298 | )?; |
| 20299 | if li == 0 { |
| 20300 | chroma_tx = tx_type; |
| 20301 | fctx.luma_tx_type.with(|c| c.set(tx_type)); |
| 20302 | } |
| 20303 | let residual = dequant_and_inverse_typed_wh( |
| 20304 | &levels, tw, th, crate::decode::bit_depth(fctx), block_q_idx(fctx), |
| 20305 | plane_q_delta(0, fctx).0, plane_q_delta(0, fctx).1, tx_type, |
| 20306 | block_iqmatrix(fctx, 0, tw, th, tx_type), |
| 20307 | ); |
| 20308 | if (tw, th) == (4, 4) { |
| 20309 | push_intra(0, tu_px, tu_py, 4, DC_PRED, 0, tu_reach, &residual, None, None, smooth_neighbor, fctx); |
| 20310 | } else { |
| 20311 | push_intra_rect(0, tu_px, tu_py, tw, th, DC_PRED, 0, tu_reach, &residual, None, None, smooth_neighbor, fctx); |
| 20312 | } |
| 20313 | levels |
| 20314 | }; |
| 20315 | if single { |
| 20316 | grid = tu_grid; |
| 20317 | } else { |
| 20318 | // Per-unit neighbour state, the same shape the intra rect |
| 20319 | // leaf's own split path runs (`decode_leaf8`'s depth-1 |
| 20320 | // loop): a transform unit inside a block is not a block at |
| 20321 | // that position. |
| 20322 | neighbours.record_mi_luma(tu_mi, 4, &tu_grid); |
| 20323 | } |
| 20324 | } |
| 20325 | if single { |
| 20326 | let state = neighbour_state(&grid); |
| 20327 | for cell in 0..h_mi { |
| 20328 | if lmi.0 + cell < neighbours.mi_rows { |
| 20329 | neighbours.left[lmi.0 + cell][0] = state; |
| 20330 | } |
| 20331 | } |
| 20332 | for cell in 0..w_mi { |
| 20333 | if lmi.1 + cell < neighbours.mi_cols { |
| 20334 | neighbours.above[lmi.1 + cell][0] = state; |
| 20335 | } |
| 20336 | } |
| 20337 | } |
| 20338 | neighbours.fill_skip_grid_rect(lmi, w_mi, h_mi, skip); |
| 20339 | neighbours.fill_lf_grid_rect( |
| 20340 | lmi, |
| 20341 | w_mi, |
| 20342 | h_mi, |
| 20343 | if single { bw as u8 } else { 4 }, |
| 20344 | if single { bh as u8 } else { 4 }, |
| 20345 | 0, |
| 20346 | fctx, |
| 20347 | ); |
| 20348 | // `record_intrabc_mi` publishes a square `n4`x`n4` footprint; a |
| 20349 | // 4x8/8x4 leaf needs its true `w_mi`x`h_mi` (lane-av1txr). |
| 20350 | record_intrabc_mi_rect(lmi.0, lmi.1, w_mi, h_mi, Some(dv), neighbours, fctx); |
| 20351 | if has_chroma { |
| 20352 | // See [`decode_leaf_split4`]: an intrabc block's chroma |
| 20353 | // inherits the luma transform type. |
| 20354 | fctx.intrabc_chroma_tx.with(|c| c.set(Some(chroma_tx))); |
| 20355 | if chroma_444 { |
| 20356 | // lane-av1-444: this leaf codes its OWN rect chroma unit |
| 20357 | // right here (frame copy at the DV); no group tail. |
| 20358 | sub8_leaf_chroma444( |
| 20359 | dec, cdfs, neighbours, lmi, bw, bh, DC_PRED, None, skip, |
| 20360 | Some(dv), y, u, v, scan4, base_q_idx, |
| 20361 | neighbours.smooth_uv_neighbour(lmi.0, lmi.1, r, c, fctx), fctx, |
| 20362 | )?; |
| 20363 | last_uv444 = DC_PRED; |
| 20364 | } else if chroma_422 { |
| 20365 | // lane-av1-422h: same at ss (1,0) -- the strip's own square |
| 20366 | // TX_4X4 chroma unit at its halved position. |
| 20367 | sub8_leaf_chroma422( |
| 20368 | dec, cdfs, neighbours, lmi, DC_PRED, 0, None, skip, |
| 20369 | Some(dv), y, u, v, scan4, base_q_idx, |
| 20370 | neighbours.smooth_uv_neighbour(lmi.0, lmi.1, r, c, fctx), fctx, |
| 20371 | )?; |
| 20372 | last_uv422 = DC_PRED; |
| 20373 | } else { |
| 20374 | last_uv = Some((DC_PRED, 0, None)); |
| 20375 | last_intrabc = Some(dv); |
| 20376 | } |
| 20377 | } |
| 20378 | continue; |
| 20379 | } |
| 20380 | if filter_intra.is_some() { |
| 20381 | // lane-fi8 r1: `predict_filter_intra` walks its 4x2 patches over |
| 20382 | // a true `bw`x`bh` block, so a 4x8/8x4 leaf predicts through the |
| 20383 | // same `reconstruct_rect`/`reconstruct` calls below -- and under |
| 20384 | // a split transform, per 4x4 unit with that unit's own edge, |
| 20385 | // which is exactly what the depth-1 loop already does. |
| 20386 | hit!(FILTER_INTRA_RECT_HITS); |
| 20387 | hit!(FILTER_INTRA_RECT_SUB8_HITS); |
| 20388 | } |
| 20389 | // `TxMode::Select`'s `tx_depth` (spec 5.11.16) exists at a 4x8/8x4 |
| 20390 | // leaf too, and is read for every intra block, skipped or not. Its |
| 20391 | // category is libaom's `bsize_to_tx_size_cat(BLOCK_4X8) == 0` -- |
| 20392 | // `sub_tx_size_map[TX_4X8] == TX_4X4` is a single step, so it is the |
| 20393 | // same 2-symbol `tx_size_cat0` an 8x8 uses, not the 3-symbol |
| 20394 | // `tx_size_cat2` the 32x16 strips read -- with the rect context |
| 20395 | // (`get_tx_size_context`'s `max_tx_wide`/`max_tx_high` = bw/bh). |
| 20396 | // Missing this read consumed one symbol fewer than the encoder wrote |
| 20397 | // and desynced the tile at the very first rect leaf (class |
| 20398 | // `symbol-consumption-gap`). |
| 20399 | // lane-av1mix: libaom `read_tx_size`'s FIRST line |
| 20400 | // (`decodeframe.c:1203`, `if (xd->lossless[...]) return TX_4X4`) |
| 20401 | // fires before the `TX_MODE_SELECT` test, so a lossless-segment leaf |
| 20402 | // codes NO depth symbol even in `TxMode::Select` -- the mixed-lossless |
| 20403 | // frame's sub8 leaves forked exactly here (ours read the symbol at |
| 20404 | // (12477,50864) bit 3011, the oracle's all_zero used it). |
| 20405 | let depth = if tx_select && !lossless(fctx) { |
| 20406 | // lane-av1txbands: the `TXFM_CONTEXT` band read every other rect |
| 20407 | // reader already does. |
| 20408 | let ctx = tx_size_context_txfm_rect(neighbours, lmi, bw, bh, fctx); |
| 20409 | dec.symbol(&mut cdfs.tx_size_cat0[ctx]) |
| 20410 | } else { |
| 20411 | 0 |
| 20412 | }; |
| 20413 | if depth != 0 { |
| 20414 | hit!(TX_DEPTH_HITS); |
| 20415 | hit!(RECT8_SPLIT_TX_HITS); |
| 20416 | } |
| 20417 | if smooth_neighbor { |
| 20418 | hit!(SMOOTH_LUMA_HITS); |
| 20419 | } |
| 20420 | let (px, py) = (lmi.1 * MI, lmi.0 * MI); |
| 20421 | let reach = Reach::of_rect(bw, bh, px, py, y.width, y.height, fctx); |
| 20422 | // `sub_tx_size_map[TX_4X8] == TX_4X4`: depth 1 splits the leaf into |
| 20423 | // two 4x4 transform units along its long axis, each predicted from |
| 20424 | // the previous unit's reconstruction and reading its own `txb_skip` |
| 20425 | // context -- `decode_leaf8`'s own depth-1 loop, two units instead of |
| 20426 | // four. The per-TU path writes its own neighbour state, so the |
| 20427 | // block-level write below is skipped for it. |
| 20428 | let split = depth != 0 || lossless(fctx); |
| 20429 | // lane-av1txr: libaom `parse_decode_block`'s `set_txfm_ctxs` for this |
| 20430 | // leaf -- the size the `tx_depth` symbol resolved (depth 1 -> TX_4X4, |
| 20431 | // else the leaf's own rect transform), published over the leaf's own |
| 20432 | // mi span. An intrabc block's `txfm_partition_context` reads these |
| 20433 | // bands, and the key-frame sub-8x8 leaves had no write at all: |
| 20434 | // `warped.obu` mi (37,62) read a stale 8 where aomdec read 4 (ctx 18 |
| 20435 | // vs 19) and the tile desynced (class `context-band-not-published`). |
| 20436 | let (ctx_tx_w, ctx_tx_h) = if split { (4, 4) } else { (bw, bh) }; |
| 20437 | txfm_partition_update_rect(neighbours, lmi, (ctx_tx_w, ctx_tx_h), (bw, bh)); |
| 20438 | if split { |
| 20439 | let mut done = 0usize; |
| 20440 | for tu in 0..2usize { |
| 20441 | let tu_mi = if vert { |
| 20442 | (lmi.0 + tu, lmi.1) |
| 20443 | } else { |
| 20444 | (lmi.0, lmi.1 + tu) |
| 20445 | }; |
| 20446 | let (tu_px, tu_py) = (tu_mi.1 * MI, tu_mi.0 * MI); |
| 20447 | let tu_around = neighbours.around_mi(tu_mi, 4)[0]; |
| 20448 | // Same per-TU rule as every other split-transform site |
| 20449 | // (lane-palette2 r12): a transform unit inside a block is not |
| 20450 | // a block at that position, so its reach comes from the |
| 20451 | // block's own row, offset by the unit's position. |
| 20452 | let tu_reach = tu_reach( |
| 20453 | bw, |
| 20454 | bh, |
| 20455 | tu_px - px, |
| 20456 | tu_py - py, |
| 20457 | 4, |
| 20458 | reach, |
| 20459 | px, |
| 20460 | py, |
| 20461 | y.width, |
| 20462 | y.height, fctx, |
| 20463 | ); |
| 20464 | let tu_skip_ctx = neighbours.luma_skip_ctx(tu_mi, 1); |
| 20465 | if skip { |
| 20466 | // A skipped leaf still predicts per transform unit: the |
| 20467 | // second 4x4 takes its above/left edge from the first |
| 20468 | // one's reconstruction, which is NOT what a single 4x8 |
| 20469 | // prediction of the same mode produces. |
| 20470 | let zeros = vec![0i32; 16]; |
| 20471 | push_intra(0, |
| 20472 | tu_px, tu_py, 4, mode, angle_delta_y, tu_reach, &zeros, None, |
| 20473 | filter_intra, smooth_neighbor, fctx, |
| 20474 | ); |
| 20475 | neighbours.record_mi_luma(tu_mi, 4, &zeros); |
| 20476 | continue; |
| 20477 | } |
| 20478 | let tu_grid = read_plane( |
| 20479 | dec, |
| 20480 | cdfs, |
| 20481 | if reduced_tx_set { |
| 20482 | TxbSet::Luma4 |
| 20483 | } else { |
| 20484 | TxbSet::Luma4Set1 |
| 20485 | }, |
| 20486 | scan4, |
| 20487 | 0, |
| 20488 | tu_around, |
| 20489 | mode, |
| 20490 | mode, |
| 20491 | angle_delta_y, |
| 20492 | tu_reach, |
| 20493 | y, |
| 20494 | tu_px, |
| 20495 | tu_py, |
| 20496 | 4, |
| 20497 | TX4, |
| 20498 | base_q_idx, |
| 20499 | None, |
| 20500 | filter_intra, |
| 20501 | Some(tu_skip_ctx), |
| 20502 | smooth_neighbor, fctx, |
| 20503 | )?; |
| 20504 | if tu_grid.iter().any(|&l| l != 0) { |
| 20505 | done += 1; |
| 20506 | } |
| 20507 | neighbours.record_mi_luma(tu_mi, 4, &tu_grid); |
| 20508 | } |
| 20509 | if done > 0 { |
| 20510 | if vert { |
| 20511 | hit!(TX4X8_CODED_HITS); |
| 20512 | } else { |
| 20513 | hit!(TX8X4_CODED_HITS); |
| 20514 | } |
| 20515 | } |
| 20516 | neighbours.fill_skip_grid_rect(lmi, w_mi, h_mi, skip); |
| 20517 | neighbours.fill_lf_grid_rect(lmi, w_mi, h_mi, 4, 4, 0, fctx); |
| 20518 | if has_chroma { |
| 20519 | if chroma_444 { |
| 20520 | // lane-av1-444: the leaf's own rect chroma unit after its |
| 20521 | // split luma (no shared group unit at ss 0/0). |
| 20522 | let (uv_mode, _, alpha) = |
| 20523 | chroma.expect("4:4:4 reads uv_mode on every leaf"); |
| 20524 | sub8_leaf_chroma444( |
| 20525 | dec, cdfs, neighbours, lmi, bw, bh, uv_mode, alpha, skip, |
| 20526 | None, y, u, v, scan4, base_q_idx, |
| 20527 | neighbours.smooth_uv_neighbour(lmi.0, lmi.1, r, c, fctx), fctx, |
| 20528 | )?; |
| 20529 | last_uv444 = if uv_mode == UV_CFL_PRED { DC_PRED } else { uv_mode }; |
| 20530 | } else if chroma_422 { |
| 20531 | // lane-av1-422h: the strip's own square TX_4X4 chroma unit |
| 20532 | // per plane right after its split luma (one per ref strip). |
| 20533 | let (uv_mode, angle_delta_uv, alpha) = |
| 20534 | chroma.expect("4:2:2 chroma references read uv_mode"); |
| 20535 | sub8_leaf_chroma422( |
| 20536 | dec, cdfs, neighbours, lmi, uv_mode, angle_delta_uv, alpha, |
| 20537 | skip, None, y, u, v, scan4, base_q_idx, |
| 20538 | neighbours.smooth_uv_neighbour(lmi.0, lmi.1, r, c, fctx), fctx, |
| 20539 | )?; |
| 20540 | last_uv422 = if uv_mode == UV_CFL_PRED { DC_PRED } else { uv_mode }; |
| 20541 | } else { |
| 20542 | last_uv = chroma; |
| 20543 | } |
| 20544 | } |
| 20545 | continue; |
| 20546 | } |
| 20547 | let grid = if skip { |
| 20548 | let zeros = Grid::Zero(bw * bh); |
| 20549 | push_intra_rect(0, |
| 20550 | px, py, bw, bh, mode, angle_delta_y, reach, &zeros, None, filter_intra, |
| 20551 | smooth_neighbor, fctx, |
| 20552 | ); |
| 20553 | zeros |
| 20554 | } else { |
| 20555 | // `plane_bsize == txsize_to_bsize[tx_size]` here too (the leaf's |
| 20556 | // luma TU *is* the leaf), so `get_txb_ctx` fixes `txb_skip_ctx` |
| 20557 | // at 0 -- see `decode_leaf_split4`'s own note. |
| 20558 | let around = neighbours.around_mi_rect(lmi, bw, bh)[0]; |
| 20559 | let set = if reduced_tx_set { |
| 20560 | TxbSet::LumaRect4x8 |
| 20561 | } else { |
| 20562 | TxbSet::LumaRect4x8Set1 |
| 20563 | }; |
| 20564 | let mut coding = cdfs.txb(set, fi_tx_row(mode, filter_intra)); |
| 20565 | let (levels, tx_type) = read_coeffs_rect( |
| 20566 | dec, |
| 20567 | &mut coding, |
| 20568 | scan, |
| 20569 | bw, |
| 20570 | bh, |
| 20571 | 0, |
| 20572 | dc_sign_ctx(around.2), |
| 20573 | TxType::DctDct, |
| 20574 | )?; |
| 20575 | if levels.iter().any(|&l| l != 0) { |
| 20576 | if vert { |
| 20577 | hit!(TX4X8_CODED_HITS); |
| 20578 | } else { |
| 20579 | hit!(TX8X4_CODED_HITS); |
| 20580 | } |
| 20581 | } |
| 20582 | let residual = dequant_and_inverse_typed_wh( |
| 20583 | &levels, |
| 20584 | bw, |
| 20585 | bh, |
| 20586 | crate::decode::bit_depth(fctx), |
| 20587 | // lane-d792 sweep: the block's segment `SEG_LVL_ALT_Q`, as at |
| 20588 | // every other dequant site (class `parsed-then-discarded`). |
| 20589 | block_q_idx(fctx), |
| 20590 | plane_q_delta(0, fctx).0, |
| 20591 | plane_q_delta(0, fctx).1, |
| 20592 | tx_type, |
| 20593 | block_iqmatrix(fctx, 0, bw, bh, tx_type), |
| 20594 | ); |
| 20595 | push_intra_rect(0, |
| 20596 | px, py, bw, bh, mode, angle_delta_y, reach, &residual, None, filter_intra, |
| 20597 | smooth_neighbor, fctx, |
| 20598 | ); |
| 20599 | levels |
| 20600 | }; |
| 20601 | // Plane 0's per-TU coefficient context over this leaf's own mi span |
| 20602 | // (`record_mi_luma`'s rect twin -- the chroma planes are written once |
| 20603 | // for the whole 8x8 group below). |
| 20604 | let state = neighbour_state(&grid); |
| 20605 | for cell in 0..h_mi { |
| 20606 | if lmi.0 + cell < neighbours.mi_rows { |
| 20607 | neighbours.left[lmi.0 + cell][0] = state; |
| 20608 | } |
| 20609 | } |
| 20610 | for cell in 0..w_mi { |
| 20611 | if lmi.1 + cell < neighbours.mi_cols { |
| 20612 | neighbours.above[lmi.1 + cell][0] = state; |
| 20613 | } |
| 20614 | } |
| 20615 | neighbours.fill_skip_grid_rect(lmi, w_mi, h_mi, skip); |
| 20616 | let (tx_w, tx_h) = if split { (4, 4) } else { (bw as u8, bh as u8) }; |
| 20617 | neighbours.fill_lf_grid_rect(lmi, w_mi, h_mi, tx_w, tx_h, 0, fctx); |
| 20618 | if has_chroma { |
| 20619 | if chroma_444 { |
| 20620 | // lane-av1-444: the leaf's own rect chroma unit after its |
| 20621 | // luma (no shared group unit at ss 0/0). |
| 20622 | let (uv_mode, _, alpha) = |
| 20623 | chroma.expect("4:4:4 reads uv_mode on every leaf"); |
| 20624 | sub8_leaf_chroma444( |
| 20625 | dec, cdfs, neighbours, lmi, bw, bh, uv_mode, alpha, skip, |
| 20626 | None, y, u, v, scan4, base_q_idx, |
| 20627 | neighbours.smooth_uv_neighbour(lmi.0, lmi.1, r, c, fctx), fctx, |
| 20628 | )?; |
| 20629 | last_uv444 = if uv_mode == UV_CFL_PRED { DC_PRED } else { uv_mode }; |
| 20630 | } else if chroma_422 { |
| 20631 | // lane-av1-422h: the strip's own square TX_4X4 chroma unit |
| 20632 | // per plane right after its luma -- libaom codes each |
| 20633 | // BLOCK_8X4 strip's chroma under the strip itself (both |
| 20634 | // strips of a HORZ pair are chroma references), not one |
| 20635 | // shared group unit. |
| 20636 | let (uv_mode, angle_delta_uv, alpha) = |
| 20637 | chroma.expect("4:2:2 chroma references read uv_mode"); |
| 20638 | sub8_leaf_chroma422( |
| 20639 | dec, cdfs, neighbours, lmi, uv_mode, angle_delta_uv, alpha, |
| 20640 | skip, None, y, u, v, scan4, base_q_idx, |
| 20641 | neighbours.smooth_uv_neighbour(lmi.0, lmi.1, r, c, fctx), fctx, |
| 20642 | )?; |
| 20643 | last_uv422 = if uv_mode == UV_CFL_PRED { DC_PRED } else { uv_mode }; |
| 20644 | } else { |
| 20645 | last_uv = chroma; |
| 20646 | } |
| 20647 | } |
| 20648 | } |
| 20649 | // lane-av1mono: a monochrome frame codes no chroma at all -- no |
| 20650 | // `uv_mode` was read ([`read_intra_mode_sub8`] folds the plane count in), |
| 20651 | // so there is no chroma to reconstruct and no chroma coefficient to read. |
| 20652 | let (u_grid, v_grid): (Grid, Grid) = if mono(fctx) { |
| 20653 | (Grid::Zero(16), Grid::Zero(16)) |
| 20654 | } else if chroma_444 { |
| 20655 | // lane-av1-444: every leaf coded and reconstructed its own chroma in |
| 20656 | // the loop above; only the coarse slots remain (last leaf's mode). |
| 20657 | neighbours.above_uv_mode[c] = last_uv444; |
| 20658 | neighbours.left_uv_mode[r] = last_uv444; |
| 20659 | (Grid::Zero(16), Grid::Zero(16)) |
| 20660 | } else if chroma_422 { |
| 20661 | // lane-av1-422h: every chroma-reference strip coded and reconstructed |
| 20662 | // its own chroma unit in the loop above (and stamped its own entropy |
| 20663 | // span), so the 4:2:0 shared group unit below must not run. Only the |
| 20664 | // coarse 16x16 slots remain: libaom's `above_uv_mode`/`left_uv_mode` |
| 20665 | // end up holding the LAST chroma reference's mode (decode order). |
| 20666 | neighbours.above_uv_mode[c] = last_uv422; |
| 20667 | neighbours.left_uv_mode[r] = last_uv422; |
| 20668 | (Grid::Zero(16), Grid::Zero(16)) |
| 20669 | } else { |
| 20670 | let (gpx, gpy) = (leaf_mi.1 * MI, leaf_mi.0 * MI); |
| 20671 | let (cpx, cpy) = (gpx >> ss_x(fctx), gpy >> ss_y(fctx)); |
| 20672 | let group_reach = Reach::of(8, gpx, gpy, y.width, y.height, fctx); |
| 20673 | let (uv_mode, angle_delta_uv, alpha) = |
| 20674 | last_uv.expect("i==1 always sets has_chroma, so chroma is always Some"); |
| 20675 | let uv_predict_mode = if uv_mode == UV_CFL_PRED { DC_PRED } else { uv_mode }; |
| 20676 | neighbours.above_uv_mode[c] = uv_predict_mode; |
| 20677 | neighbours.left_uv_mode[r] = uv_predict_mode; |
| 20678 | neighbours.record_uv_mode_mi(leaf_mi.0, leaf_mi.1, 2, 2, uv_predict_mode); |
| 20679 | let (u_grid, v_grid): (Grid, Grid) = if leaf_skips[1] && last_intrabc.is_none() { |
| 20680 | // A skipped chroma block still gets its CfL contribution: libaom's |
| 20681 | // `predict_and_reconstruct_intra_block` calls `cfl_predict_block` |
| 20682 | // from `av1_predict_intra_block_facade` (av1/common/reconintra.c:1719) |
| 20683 | // whenever `mbmi->uv_mode == UV_CFL_PRED`, before any residual is |
| 20684 | // added -- `skip_txfm` only drops the residual, never the alpha. |
| 20685 | // lane-reelckf and lane-kf1200 fixed THIS SAME arm independently; the |
| 20686 | // merge keeps one fix and feeds both lanes' counters so neither of |
| 20687 | // their gates goes vacuous. |
| 20688 | let ac = alpha.map(|_| cfl_src(gpx, gpy, 8)); |
| 20689 | if alpha.is_some() { |
| 20690 | hit!(CHROMA_SKIP_CFL_HITS); |
| 20691 | hit!(SKIP_CFL_HITS); |
| 20692 | } |
| 20693 | push_intra(1, cpx, cpy, 4, uv_predict_mode, angle_delta_uv, group_reach, &ZERO_RESIDUAL[..16], alpha.zip(ac).map(|((au, _), ac)| (au, ac)), None, smooth_neighbor_uv, fctx); |
| 20694 | push_intra(2, cpx, cpy, 4, uv_predict_mode, angle_delta_uv, group_reach, &ZERO_RESIDUAL[..16], alpha.zip(ac).map(|((_, av), ac)| (av, ac)), None, smooth_neighbor_uv, fctx); |
| 20695 | // lane-av1txbands: a SKIPPED chroma-reference leaf reads no chroma |
| 20696 | // coefficient, so it never consults the inherited luma transform type |
| 20697 | // -- but an intrabc chroma-reference leaf did arm |
| 20698 | // [`INTRABC_CHROMA_TX`] above (`has_chroma`), and the two coefficient |
| 20699 | // branches below are the only ones that disarm it. Left armed, the slot |
| 20700 | // picks the NEXT block's chroma scan and inverse transform (class |
| 20701 | // arm-without-clear-on-a-skip-route). Counted so a gate can tell a |
| 20702 | // stream that reaches this route from one that never does. |
| 20703 | if fctx.intrabc_chroma_tx.with(std::cell::Cell::get).is_some() { |
| 20704 | hit!(SKIPPED_INTRABC_CHROMA_ARM_HITS); |
| 20705 | fctx.intrabc_chroma_tx.with(|c| c.set(None)); |
| 20706 | } |
| 20707 | (Grid::Zero(16), Grid::Zero(16)) |
| 20708 | } else if let Some(dv) = last_intrabc { |
| 20709 | // A 4:2:0 `BLOCK_4X8`/`BLOCK_8X4` leaf maps to a clamped chroma |
| 20710 | // `BLOCK_4X4` (`get_plane_block_size`, blockd.h:1184; `set_plane_n4`, |
| 20711 | // av1_common_int.h:1344). The chroma reference is still the whole |
| 20712 | // 8x8 group's 4x4 plane block at `(cpx, cpy)`, so prediction uses the |
| 20713 | // group's destination origin, not the odd-mi leaf's nominal 2x4/4x2 |
| 20714 | // extent. The old route treated a skipped intrabc leaf as skipped |
| 20715 | // intra and predicted this block with DC, first differing at |
| 20716 | // mi(46,52), U(104,92), while consuming identical symbols. |
| 20717 | // `decode_reconstruct_tx` reads no chroma coefficients for a skipped |
| 20718 | // intrabc block, but an unskipped one reads the ordinary Chroma4 |
| 20719 | // tables; chroma inherits no coded tx_type of its own. |
| 20720 | let mut ub = vec![0u16; 16]; |
| 20721 | mc::predict_with_filter( |
| 20722 | &u.data, u.width, u.true_width, u.true_height, |
| 20723 | mv_to_q4(cpx, dv.1, ss_x(fctx)), mv_to_q4(cpy, dv.0, ss_y(fctx)), |
| 20724 | 4, 4, mc::InterpFilterKind::Bilinear, &mut ub, fctx, |
| 20725 | ); |
| 20726 | let mut vb = vec![0u16; 16]; |
| 20727 | mc::predict_with_filter( |
| 20728 | &v.data, v.width, v.true_width, v.true_height, |
| 20729 | mv_to_q4(cpx, dv.1, ss_x(fctx)), mv_to_q4(cpy, dv.0, ss_y(fctx)), |
| 20730 | 4, 4, mc::InterpFilterKind::Bilinear, &mut vb, fctx, |
| 20731 | ); |
| 20732 | if leaf_skips[1] { |
| 20733 | // `skip_txfm` still reconstructs an intrabc prediction, but |
| 20734 | // libaom's `decode_reconstruct_tx` reads NO chroma coefficients. |
| 20735 | // The ordinary skipped-intra arm above is therefore not valid for |
| 20736 | // an intrabc leaf: use the copied, clamped 4x4 prediction directly. |
| 20737 | push_mc_rect( |
| 20738 | 1, cpx, cpy, 4, 4, 4, Pred::Inline(&ub, 4), |
| 20739 | &ZERO_RESIDUAL[..16], fctx, |
| 20740 | ); |
| 20741 | push_mc_rect( |
| 20742 | 2, cpx, cpy, 4, 4, 4, Pred::Inline(&vb, 4), |
| 20743 | &ZERO_RESIDUAL[..16], fctx, |
| 20744 | ); |
| 20745 | hit!(SKIPPED_INTRABC_CHROMA_ARM_HITS); |
| 20746 | fctx.intrabc_chroma_tx.with(|c| c.set(None)); |
| 20747 | (Grid::Zero(16), Grid::Zero(16)) |
| 20748 | } else { |
| 20749 | let chroma_around = neighbours.around_mi(leaf_mi, 8); |
| 20750 | set_palette_pred(ub, fctx); |
| 20751 | let ug = read_plane( |
| 20752 | dec, cdfs, TxbSet::Chroma4, scan4, 1, chroma_around[1], DC_PRED, DC_PRED, |
| 20753 | 0, group_reach, u, cpx, cpy, 4, TX4, base_q_idx, |
| 20754 | None, None, None, smooth_neighbor_uv, fctx, |
| 20755 | )?; |
| 20756 | set_palette_pred(vb, fctx); |
| 20757 | let vg = read_plane( |
| 20758 | dec, cdfs, TxbSet::Chroma4, scan4, 2, chroma_around[2], DC_PRED, DC_PRED, |
| 20759 | 0, group_reach, v, cpx, cpy, 4, TX4, base_q_idx, |
| 20760 | None, None, None, smooth_neighbor_uv, fctx, |
| 20761 | )?; |
| 20762 | fctx.intrabc_chroma_tx.with(|c| c.set(None)); |
| 20763 | (ug, vg) |
| 20764 | } |
| 20765 | } else { |
| 20766 | let ac = alpha.map(|_| cfl_src(gpx, gpy, 8)); |
| 20767 | let chroma_around = neighbours.around_mi(leaf_mi, 8); |
| 20768 | let ug = read_plane( |
| 20769 | dec, cdfs, TxbSet::Chroma4, scan4, 1, chroma_around[1], uv_mode, uv_predict_mode, |
| 20770 | angle_delta_uv, group_reach, u, cpx, cpy, 4, TX4, base_q_idx, |
| 20771 | alpha.zip(ac).map(|((au, _), ac)| (au, ac)), None, None, smooth_neighbor_uv, fctx, |
| 20772 | )?; |
| 20773 | let vg = read_plane( |
| 20774 | dec, cdfs, TxbSet::Chroma4, scan4, 2, chroma_around[2], uv_mode, uv_predict_mode, |
| 20775 | angle_delta_uv, group_reach, v, cpx, cpy, 4, TX4, base_q_idx, |
| 20776 | alpha.zip(ac).map(|((_, av), ac)| (av, ac)), None, None, smooth_neighbor_uv, fctx, |
| 20777 | )?; |
| 20778 | (ug, vg) |
| 20779 | }; |
| 20780 | (u_grid, v_grid) |
| 20781 | }; |
| 20782 | // `update_partition_context` at the 8x8 level writes |
| 20783 | // `partition_context_lookup[subsize]` over the whole 8x8 span: BLOCK_4X8 |
| 20784 | // is `{31, 30}` (bit0 set on the above half only), BLOCK_8X4 `{30, 31}` |
| 20785 | // -- the mirror of `decode_leaf_split4`'s `{31, 31}`, which |
| 20786 | // `partition_ctx_mi` reads back off these side values. |
| 20787 | for cell in 0..2 { |
| 20788 | neighbours.above_side_mi[leaf_mi.1 + cell] = bw as u8; |
| 20789 | neighbours.left_side_mi[leaf_mi.0 + cell] = bh as u8; |
| 20790 | } |
| 20791 | // lane-av1-444: at 4:4:4 [`sub8_leaf_chroma444`] already stamped each |
| 20792 | // leaf's own span as it decoded; these placeholder grids carry no state. |
| 20793 | // lane-av1-422h: same at ss (1,0) -- [`sub8_leaf_chroma422`] stamped each |
| 20794 | // chroma-reference strip's own span in decode order. |
| 20795 | if !(chroma_444 || chroma_422) { |
| 20796 | let round_up_even = |n: usize| n.div_ceil(2) * 2; |
| 20797 | let bound_h = round_up_even(neighbours.mi_rows); |
| 20798 | let bound_w = round_up_even(neighbours.mi_cols); |
| 20799 | let u_state = neighbour_state(&u_grid); |
| 20800 | let v_state = neighbour_state(&v_grid); |
| 20801 | for cell in 0..2usize { |
| 20802 | if cell < 2usize.min(bound_h.saturating_sub(leaf_mi.0)) { |
| 20803 | neighbours.left[leaf_mi.0 + cell][1] = u_state; |
| 20804 | neighbours.left[leaf_mi.0 + cell][2] = v_state; |
| 20805 | } |
| 20806 | if cell < 2usize.min(bound_w.saturating_sub(leaf_mi.1)) { |
| 20807 | neighbours.above[leaf_mi.1 + cell][1] = u_state; |
| 20808 | neighbours.above[leaf_mi.1 + cell][2] = v_state; |
| 20809 | } |
| 20810 | } |
| 20811 | } |
| 20812 | // lane-dkey (same clear as `decode_leaf_split4`): a sub-8x8 leaf can never use palette (`av1_allow_palette` |
| 20813 | // needs BLOCK_8X8 and up), and libaom's mi grid says so -- its |
| 20814 | // `palette_size` is 0 in every one of these mi cells, so the NEXT block's |
| 20815 | // `av1_get_palette_mode_ctx` and colour cache read 0 here. This decoder |
| 20816 | // only ever WROTE palette state from the >=8x8 readers, so an 8x8 palette |
| 20817 | // block above a 4x4 group left its size/colours standing in the mi-granular |
| 20818 | // bands: the block below the group read `palette_y_mode` off CDF row 1 |
| 20819 | // where libaom reads row 0 (class [[cdf-row-held-constant]]), decoded a |
| 20820 | // palette libaom never coded, and then skipped the `use_filter_intra` |
| 20821 | // symbol that a palette block has none of -- one symbol short, tile |
| 20822 | // desynced. Publishing the group's own zeros is the clear. |
| 20823 | neighbours.record_palette_y_rect(leaf_mi, 8, 8, 0, [0u16; 8]); |
| 20824 | neighbours.record_palette_uv_rect(leaf_mi, 8, 8, 0, [0u16; 8]); |
| 20825 | // `(below_mode, right_mode)`: the sub-block a neighbour below sees is the |
| 20826 | // bottom one (the second under HORZ, the left one under VERT), the one a |
| 20827 | // neighbour to the right sees is the top-right (the second under VERT). |
| 20828 | Ok(if vert { |
| 20829 | (leaf_modes[0], leaf_modes[1]) |
| 20830 | } else { |
| 20831 | (leaf_modes[1], leaf_modes[0]) |
| 20832 | }) |
| 20833 | } |
| 20834 | |
| 20835 | /// Spec 7.15.3's `Cdef_Directions`: `(row, col)` offsets for the two primary |
| 20836 | /// taps of each of the eight directions; the secondary taps at `dir+2`/`dir-2` |
| 20837 | /// (mod 8) reuse the same table. |
| 20838 | const CDEF_DIRECTIONS: [[(i32, i32); 2]; 8] = [ |
| 20839 | [(-1, 1), (-2, 2)], |
| 20840 | [(0, 1), (-1, 2)], |
| 20841 | [(0, 1), (0, 2)], |
| 20842 | [(0, 1), (1, 2)], |
| 20843 | [(1, 1), (2, 2)], |
| 20844 | [(1, 0), (2, 1)], |
| 20845 | [(1, 0), (2, 0)], |
| 20846 | [(1, 0), (2, -1)], |
| 20847 | ]; |
| 20848 | /// A neighbour tap outside the frame's true extent -- `constrain` reads this |
| 20849 | /// as "so far away it never contributes" (spec/libaom `CDEF_VERY_LARGE`). |
| 20850 | const CDEF_VERY_LARGE: i32 = 0x4000; |
| 20851 | const CDEF_PRI_TAPS: [[i32; 2]; 2] = [[4, 2], [3, 3]]; |
| 20852 | const CDEF_SEC_TAPS: [i32; 2] = [2, 1]; |
| 20853 | |
| 20854 | fn cdef_msb(x: i32) -> i32 { |
| 20855 | 31 - x.leading_zeros() as i32 |
| 20856 | } |
| 20857 | |
| 20858 | /// Spec 7.15.3's `constrain`: how much of a neighbour's difference from the |
| 20859 | /// centre sample a filter tap is allowed to contribute. |
| 20860 | #[allow(dead_code)] // read only from the `#[cfg(test)]` gates |
| 20861 | fn cdef_constrain(diff: i32, threshold: i32, damping: i32) -> i32 { |
| 20862 | if threshold == 0 { |
| 20863 | return 0; |
| 20864 | } |
| 20865 | cdef_constrain_shifted(diff, threshold, cdef_constrain_shift(threshold, damping)) |
| 20866 | } |
| 20867 | |
| 20868 | /// `constrain`'s damping shift: a function of the block's threshold and |
| 20869 | /// damping alone, so the filter loop computes it once per block instead of |
| 20870 | /// the `leading_zeros` + branch it used to do twice per tap read. |
| 20871 | fn cdef_constrain_shift(threshold: i32, damping: i32) -> i32 { |
| 20872 | (damping - cdef_msb(threshold)).max(0) |
| 20873 | } |
| 20874 | |
| 20875 | /// [`cdef_constrain`]'s body with the shift already resolved; `threshold` is |
| 20876 | /// non-zero on every call (the caller only filters when its strength is). |
| 20877 | fn cdef_constrain_shifted(diff: i32, threshold: i32, shift: i32) -> i32 { |
| 20878 | let a = diff.abs(); |
| 20879 | diff.signum() * (threshold - (a >> shift)).clamp(0, a) |
| 20880 | } |
| 20881 | |
| 20882 | /// The widest `CDEF_DIRECTIONS` offset in either axis: every tap a block reads |
| 20883 | /// lies inside its own samples grown by this much. |
| 20884 | const CDEF_BORDER: usize = 2; |
| 20885 | |
| 20886 | /// Copies one block's whole CDEF footprint -- its `bh` x `bw` samples plus the |
| 20887 | /// [`CDEF_BORDER`] tap ring -- out of the *unfiltered* plane into `buf` (row |
| 20888 | /// stride `bw + 2 * CDEF_BORDER`), so the direction search and the tap loops |
| 20889 | /// index a small contiguous window instead of re-deriving the frame-bounds |
| 20890 | /// test on every one of a pixel's ~17 tap reads. Out-of-frame positions get |
| 20891 | /// `CDEF_VERY_LARGE`, exactly as the per-sample closure did. |
| 20892 | fn cdef_gather( |
| 20893 | src: &[u16], |
| 20894 | stride: usize, |
| 20895 | true_w: usize, |
| 20896 | true_h: usize, |
| 20897 | ox: usize, |
| 20898 | oy: usize, |
| 20899 | bw: usize, |
| 20900 | bh: usize, |
| 20901 | buf: &mut [i16], |
| 20902 | ) { |
| 20903 | let bstride = bw + 2 * CDEF_BORDER; |
| 20904 | let rows = bh + 2 * CDEF_BORDER; |
| 20905 | if oy >= CDEF_BORDER |
| 20906 | && ox >= CDEF_BORDER |
| 20907 | && oy + bh + CDEF_BORDER <= true_h |
| 20908 | && ox + bw + CDEF_BORDER <= true_w |
| 20909 | { |
| 20910 | // Interior block: every tap is inside the frame, so the whole window |
| 20911 | // is `rows` contiguous runs. |
| 20912 | for r in 0..rows { |
| 20913 | let s0 = (oy - CDEF_BORDER + r) * stride + (ox - CDEF_BORDER); |
| 20914 | let src_row = &src[s0..s0 + bstride]; |
| 20915 | let dst_row = &mut buf[r * bstride..r * bstride + bstride]; |
| 20916 | for (d, &v) in dst_row.iter_mut().zip(src_row) { |
| 20917 | *d = v as i16; |
| 20918 | } |
| 20919 | } |
| 20920 | return; |
| 20921 | } |
| 20922 | for r in 0..rows { |
| 20923 | let ny = oy as isize + r as isize - CDEF_BORDER as isize; |
| 20924 | for c in 0..bstride { |
| 20925 | let nx = ox as isize + c as isize - CDEF_BORDER as isize; |
| 20926 | buf[r * bstride + c] = |
| 20927 | if ny < 0 || nx < 0 || ny as usize >= true_h || nx as usize >= true_w { |
| 20928 | CDEF_VERY_LARGE as i16 |
| 20929 | } else { |
| 20930 | src[ny as usize * stride + nx as usize] as i16 |
| 20931 | }; |
| 20932 | } |
| 20933 | } |
| 20934 | } |
| 20935 | |
| 20936 | /// libaom `cdef_find_dir_c`'s eight partial-sum arrays, row at a time: each |
| 20937 | /// array takes the row, its reverse, its pairwise sums or its total added |
| 20938 | /// into consecutive slots, so the 512 scalar read-modify-writes on i64 the |
| 20939 | /// per-sample form did become a handful of lane-wide adds per row. i32 is |
| 20940 | /// exact here: every partial is a sum of at most eight values in -128..127, |
| 20941 | /// and slot 15 exists only so an eight-lane store at offset 7 fits. |
| 20942 | fn cdef_find_dir_partials(win: &[i16], bstride: usize, coeff_shift: i32) -> [[i32; 16]; 8] { |
| 20943 | fn add_at<const N: usize>(dst: &mut [i32; 16], off: usize, src: &[i32; N]) { |
| 20944 | for (d, &x) in dst[off..off + N].iter_mut().zip(src.iter()) { |
| 20945 | *d += x; |
| 20946 | } |
| 20947 | } |
| 20948 | let mut partial = [[0i32; 16]; 8]; |
| 20949 | for i in 0..8usize { |
| 20950 | let base = (i + CDEF_BORDER) * bstride + CDEF_BORDER; |
| 20951 | // libaom `cdef_find_dir_c`: normalise to an 8-bit sample before |
| 20952 | // accumulating, same as `constrain`'s threshold shift. |
| 20953 | let row: [i32; 8] = core::array::from_fn(|j| (i32::from(win[base + j]) >> coeff_shift) - 128); |
| 20954 | let rev: [i32; 8] = core::array::from_fn(|j| row[7 - j]); |
| 20955 | let pair: [i32; 4] = core::array::from_fn(|k| row[2 * k] + row[2 * k + 1]); |
| 20956 | let rpair: [i32; 4] = core::array::from_fn(|k| pair[3 - k]); |
| 20957 | let (p, rest) = partial.split_at_mut(1); |
| 20958 | add_at(&mut p[0], i, &row); // partial[0][i + j] |
| 20959 | add_at(&mut rest[0], i, &pair); // partial[1][i + j / 2] |
| 20960 | rest[1][i] = row.iter().sum(); // partial[2][i] |
| 20961 | add_at(&mut rest[2], i, &rpair); // partial[3][3 + i - j / 2] |
| 20962 | add_at(&mut rest[3], i, &rev); // partial[4][7 + i - j] |
| 20963 | add_at(&mut rest[4], 3 - i / 2, &row); // partial[5][3 - i / 2 + j] |
| 20964 | add_at(&mut rest[5], 0, &row); // partial[6][j] |
| 20965 | add_at(&mut rest[6], i / 2, &row); // partial[7][i / 2 + j] |
| 20966 | } |
| 20967 | partial |
| 20968 | } |
| 20969 | |
| 20970 | /// Spec 7.15.3's direction search (libaom `cdef_find_dir_c`): the dominant |
| 20971 | /// direction of an 8x8 luma window and the variance gap between it and its |
| 20972 | /// orthogonal, reading [`cdef_gather`]'s window (row stride `bstride`, the |
| 20973 | /// block's own samples starting at `CDEF_BORDER, CDEF_BORDER`). |
| 20974 | fn cdef_find_dir(win: &[i16], bstride: usize, coeff_shift: i32) -> (usize, i32) { |
| 20975 | const DIV_TABLE: [i64; 9] = [0, 840, 420, 280, 210, 168, 140, 120, 105]; |
| 20976 | #[cfg(target_arch = "x86_64")] |
| 20977 | let partial = if crate::mc::simd_level() == crate::mc::SimdLevel::Avx2 { |
| 20978 | // SAFETY: reached only when the CPU reports avx2; the kernel reads |
| 20979 | // the same eight rows of eight samples the scalar form does. |
| 20980 | #[allow(unsafe_code)] |
| 20981 | unsafe { |
| 20982 | cdef_simd::find_dir_partials_avx2(win, bstride, coeff_shift) |
| 20983 | } |
| 20984 | } else { |
| 20985 | cdef_find_dir_partials(win, bstride, coeff_shift) |
| 20986 | }; |
| 20987 | #[cfg(not(target_arch = "x86_64"))] |
| 20988 | let partial = cdef_find_dir_partials(win, bstride, coeff_shift); |
| 20989 | let sq = |a: i32| -> i64 { i64::from(a) * i64::from(a) }; |
| 20990 | let mut cost = [0i64; 8]; |
| 20991 | for i in 0..8usize { |
| 20992 | cost[2] += sq(partial[2][i]); |
| 20993 | cost[6] += sq(partial[6][i]); |
| 20994 | } |
| 20995 | cost[2] *= DIV_TABLE[8]; |
| 20996 | cost[6] *= DIV_TABLE[8]; |
| 20997 | for i in 0..7usize { |
| 20998 | cost[0] += (sq(partial[0][i]) + sq(partial[0][14 - i])) * DIV_TABLE[i + 1]; |
| 20999 | cost[4] += (sq(partial[4][i]) + sq(partial[4][14 - i])) * DIV_TABLE[i + 1]; |
| 21000 | } |
| 21001 | cost[0] += sq(partial[0][7]) * DIV_TABLE[8]; |
| 21002 | cost[4] += sq(partial[4][7]) * DIV_TABLE[8]; |
| 21003 | for i in (1..8).step_by(2) { |
| 21004 | for j in 0..5usize { |
| 21005 | cost[i] += sq(partial[i][3 + j]); |
| 21006 | } |
| 21007 | cost[i] *= DIV_TABLE[8]; |
| 21008 | for j in 0..3usize { |
| 21009 | cost[i] += (sq(partial[i][j]) + sq(partial[i][10 - j])) * DIV_TABLE[2 * j + 2]; |
| 21010 | } |
| 21011 | } |
| 21012 | let mut best_cost = 0i64; |
| 21013 | let mut best_dir = 0usize; |
| 21014 | for (i, &c) in cost.iter().enumerate() { |
| 21015 | if c > best_cost { |
| 21016 | best_cost = c; |
| 21017 | best_dir = i; |
| 21018 | } |
| 21019 | } |
| 21020 | let var = (best_cost - cost[(best_dir + 4) & 7]) >> 10; |
| 21021 | (best_dir, var as i32) |
| 21022 | } |
| 21023 | |
| 21024 | /// Spec 7.15.2's per-8x8 strength adjustment: a strongly directional 8x8 |
| 21025 | /// (high variance gap) gets more deringing than a flat or non-directional |
| 21026 | /// one, only ever applied to the luma primary strength. |
| 21027 | fn cdef_adjust_strength(strength: i32, var: i32) -> i32 { |
| 21028 | if var == 0 { |
| 21029 | return 0; |
| 21030 | } |
| 21031 | let i = if var >> 6 != 0 { |
| 21032 | cdef_msb(var >> 6).min(12) |
| 21033 | } else { |
| 21034 | 0 |
| 21035 | }; |
| 21036 | (strength * (4 + i) + 8) >> 4 |
| 21037 | } |
| 21038 | |
| 21039 | /// Spec 7.15.3's `cdef_filter_block`: filters one block (8x8 luma, 4x4 4:2:0 |
| 21040 | /// chroma) in place, `sample(row, col)` reading the *unfiltered* frame (a |
| 21041 | /// previously-filtered neighbour must never feed this block's own sum). |
| 21042 | #[allow(clippy::too_many_arguments)] |
| 21043 | #[allow(unsafe_code)] |
| 21044 | fn cdef_filter_block( |
| 21045 | src: &[i16], |
| 21046 | dst: &mut PlaneBuf<'static>, |
| 21047 | ox: usize, |
| 21048 | oy: usize, |
| 21049 | bw: usize, |
| 21050 | bh: usize, |
| 21051 | pri_strength: i32, |
| 21052 | sec_strength: i32, |
| 21053 | dir: usize, |
| 21054 | pri_damping: i32, |
| 21055 | sec_damping: i32, |
| 21056 | enable_primary: bool, |
| 21057 | enable_secondary: bool, |
| 21058 | coeff_shift: i32, fctx: &crate::decode::FrameCtx, |
| 21059 | ) { |
| 21060 | let (stride, smax) = (dst.width, sample_max(fctx)); |
| 21061 | #[cfg(target_arch = "x86_64")] |
| 21062 | if crate::mc::simd_level() == crate::mc::SimdLevel::Avx2 && (bw == 4 || bw == 8) && bh % 2 == 0 |
| 21063 | { |
| 21064 | // SAFETY: reached only when the CPU reports avx2; the kernel reads |
| 21065 | // `src`'s whole (bw + 4) x (bh + 4) window and writes exactly the |
| 21066 | // `bh` runs of `bw` samples the scalar form writes. |
| 21067 | unsafe { |
| 21068 | cdef_simd::filter_block_avx2( |
| 21069 | src, |
| 21070 | dst.data.to_mut(), |
| 21071 | stride, |
| 21072 | ox, |
| 21073 | oy, |
| 21074 | bw, |
| 21075 | bh, |
| 21076 | pri_strength, |
| 21077 | sec_strength, |
| 21078 | dir, |
| 21079 | pri_damping, |
| 21080 | sec_damping, |
| 21081 | enable_primary, |
| 21082 | enable_secondary, |
| 21083 | coeff_shift, |
| 21084 | smax, |
| 21085 | ) |
| 21086 | }; |
| 21087 | return; |
| 21088 | } |
| 21089 | cdef_filter_block_scalar( |
| 21090 | src, |
| 21091 | dst.data.to_mut(), |
| 21092 | stride, |
| 21093 | ox, |
| 21094 | oy, |
| 21095 | bw, |
| 21096 | bh, |
| 21097 | pri_strength, |
| 21098 | sec_strength, |
| 21099 | dir, |
| 21100 | pri_damping, |
| 21101 | sec_damping, |
| 21102 | enable_primary, |
| 21103 | enable_secondary, |
| 21104 | coeff_shift, |
| 21105 | smax, |
| 21106 | ); |
| 21107 | } |
| 21108 | |
| 21109 | /// [`cdef_filter_block`]'s scalar reference: spec 7.15.3 sample by sample, |
| 21110 | /// the form every non-AVX2 build decodes through and the one |
| 21111 | /// `cdef_simd_kernel_matches_the_scalar_filter` checks the kernel against. |
| 21112 | #[allow(clippy::too_many_arguments)] |
| 21113 | fn cdef_filter_block_scalar( |
| 21114 | src: &[i16], |
| 21115 | dst: &mut [u16], |
| 21116 | stride: usize, |
| 21117 | ox: usize, |
| 21118 | oy: usize, |
| 21119 | bw: usize, |
| 21120 | bh: usize, |
| 21121 | pri_strength: i32, |
| 21122 | sec_strength: i32, |
| 21123 | dir: usize, |
| 21124 | pri_damping: i32, |
| 21125 | sec_damping: i32, |
| 21126 | enable_primary: bool, |
| 21127 | enable_secondary: bool, |
| 21128 | coeff_shift: i32, |
| 21129 | smax: i32, |
| 21130 | ) { |
| 21131 | let clipping_required = enable_primary && enable_secondary; |
| 21132 | // libaom `cdef_filter_block_internal`: `cdef_pri_taps[(pri_strength >> |
| 21133 | // coeff_shift) & 1]` -- `pri_strength` (and its `adjust_strength` |
| 21134 | // derivative) carries the bit-depth shift, so the tap-set parity bit |
| 21135 | // must be read back above that shift, not off the raw low bit. |
| 21136 | let pri_taps = CDEF_PRI_TAPS[((pri_strength >> coeff_shift) & 1) as usize]; |
| 21137 | // Both damping shifts depend only on this block's strengths (spec 7.15.3's |
| 21138 | // `constrain`), so they are resolved here rather than per tap read. |
| 21139 | let pri_shift = cdef_constrain_shift(pri_strength.max(1), pri_damping); |
| 21140 | let sec_shift = cdef_constrain_shift(sec_strength.max(1), sec_damping); |
| 21141 | let bstride = bw + 2 * CDEF_BORDER; |
| 21142 | // `src` is [`cdef_gather`]'s window: (i, j) in block coordinates, taps |
| 21143 | // reaching +-CDEF_BORDER, so every index below is in range by construction. |
| 21144 | let at = |i: i32, j: i32| -> i32 { |
| 21145 | i32::from(src[(i + CDEF_BORDER as i32) as usize * bstride + (j + CDEF_BORDER as i32) as usize]) |
| 21146 | }; |
| 21147 | let (pri_dir, sec_dir0, sec_dir1) = |
| 21148 | (CDEF_DIRECTIONS[dir], CDEF_DIRECTIONS[(dir + 2) & 7], CDEF_DIRECTIONS[(dir + 6) & 7]); |
| 21149 | for i in 0..bh { |
| 21150 | for j in 0..bw { |
| 21151 | let (i32i, i32j) = (i as i32, j as i32); |
| 21152 | let x = at(i32i, i32j); |
| 21153 | let mut sum = 0i32; |
| 21154 | let mut max = x; |
| 21155 | let mut min = x; |
| 21156 | for k in 0..2usize { |
| 21157 | if enable_primary { |
| 21158 | let (tr, tc) = pri_dir[k]; |
| 21159 | let p0 = at(i32i + tr, i32j + tc); |
| 21160 | let p1 = at(i32i - tr, i32j - tc); |
| 21161 | sum += pri_taps[k] * cdef_constrain_shifted(p0 - x, pri_strength, pri_shift); |
| 21162 | sum += pri_taps[k] * cdef_constrain_shifted(p1 - x, pri_strength, pri_shift); |
| 21163 | if clipping_required { |
| 21164 | if p0 != CDEF_VERY_LARGE { |
| 21165 | max = max.max(p0); |
| 21166 | } |
| 21167 | if p1 != CDEF_VERY_LARGE { |
| 21168 | max = max.max(p1); |
| 21169 | } |
| 21170 | min = min.min(p0).min(p1); |
| 21171 | } |
| 21172 | } |
| 21173 | if enable_secondary { |
| 21174 | let (tr0, tc0) = sec_dir0[k]; |
| 21175 | let (tr1, tc1) = sec_dir1[k]; |
| 21176 | let s0 = at(i32i + tr0, i32j + tc0); |
| 21177 | let s1 = at(i32i - tr0, i32j - tc0); |
| 21178 | let s2 = at(i32i + tr1, i32j + tc1); |
| 21179 | let s3 = at(i32i - tr1, i32j - tc1); |
| 21180 | if clipping_required { |
| 21181 | for s in [s0, s1, s2, s3] { |
| 21182 | if s != CDEF_VERY_LARGE { |
| 21183 | max = max.max(s); |
| 21184 | } |
| 21185 | } |
| 21186 | min = min.min(s0).min(s1).min(s2).min(s3); |
| 21187 | } |
| 21188 | sum += CDEF_SEC_TAPS[k] |
| 21189 | * (cdef_constrain_shifted(s0 - x, sec_strength, sec_shift) |
| 21190 | + cdef_constrain_shifted(s1 - x, sec_strength, sec_shift) |
| 21191 | + cdef_constrain_shifted(s2 - x, sec_strength, sec_shift) |
| 21192 | + cdef_constrain_shifted(s3 - x, sec_strength, sec_shift)); |
| 21193 | } |
| 21194 | } |
| 21195 | let mut y = x + ((8 + sum - i32::from(sum < 0)) >> 4); |
| 21196 | if clipping_required { |
| 21197 | y = y.clamp(min, max); |
| 21198 | } |
| 21199 | dst[(oy + i) * stride + (ox + j)] = y.clamp(0, smax) as u16; |
| 21200 | } |
| 21201 | } |
| 21202 | } |
| 21203 | |
| 21204 | /// lane-perf7 step 1: [`cdef_filter_block`]'s AVX2 kernel -- the scalar |
| 21205 | /// filter was 13.1% of a 4K decode's self time. Nothing here is spec logic: |
| 21206 | /// the same taps, the same `constrain`, the same `min`/`max` clamp with the |
| 21207 | /// `CDEF_VERY_LARGE` sentinel excluded from the max, the same |
| 21208 | /// `y + ((8 + sum - (sum < 0)) >> 4)` rounding, done 16 samples (two rows) at |
| 21209 | /// a time in 16-bit lanes. |
| 21210 | /// |
| 21211 | /// 16-bit lanes are exact for every value this filter sees: window samples |
| 21212 | /// are in `[0, CDEF_VERY_LARGE]` (0x4000), so a tap difference fits in |
| 21213 | /// `i16`, a `constrain` result is bounded by the threshold (at most |
| 21214 | /// `15 << 4`), and the tap-weighted sum by `24 *` that. |
| 21215 | #[cfg(target_arch = "x86_64")] |
| 21216 | #[allow(unsafe_code)] |
| 21217 | #[allow(unsafe_op_in_unsafe_fn)] // the kernel's whole body is its contract |
| 21218 | mod cdef_simd { |
| 21219 | use super::{ |
| 21220 | cdef_constrain_shift, CDEF_BORDER, CDEF_DIRECTIONS, CDEF_PRI_TAPS, CDEF_SEC_TAPS, |
| 21221 | CDEF_VERY_LARGE, |
| 21222 | }; |
| 21223 | use std::arch::x86_64::*; |
| 21224 | |
| 21225 | /// The window's rows `i` and `i + 1` at one tap offset, as one 256-bit |
| 21226 | /// register (low half = row `i`). |
| 21227 | /// |
| 21228 | /// # Safety |
| 21229 | /// Requires AVX2; `off + bstride + 8` must be inside `w`. |
| 21230 | #[target_feature(enable = "avx2")] |
| 21231 | #[inline] |
| 21232 | unsafe fn ld2(w: *const i16, off: isize, bstride: isize) -> __m256i { |
| 21233 | let lo = _mm_loadu_si128(w.offset(off).cast()); |
| 21234 | let hi = _mm_loadu_si128(w.offset(off + bstride).cast()); |
| 21235 | _mm256_inserti128_si256(_mm256_castsi128_si256(lo), hi, 1) |
| 21236 | } |
| 21237 | |
| 21238 | /// Spec 7.15.3's `constrain`, lane-parallel: |
| 21239 | /// `sign(diff) * clamp(threshold - (|diff| >> shift), 0, |diff|)`. |
| 21240 | /// `_mm256_sign_epi16` reproduces `signum` exactly, zero included (the |
| 21241 | /// clamped magnitude is already 0 where `diff` is). |
| 21242 | /// |
| 21243 | /// # Safety |
| 21244 | /// Requires AVX2. |
| 21245 | #[target_feature(enable = "avx2")] |
| 21246 | #[inline] |
| 21247 | unsafe fn constrain(p: __m256i, x: __m256i, thr: __m256i, shift: __m128i) -> __m256i { |
| 21248 | let diff = _mm256_sub_epi16(p, x); |
| 21249 | let a = _mm256_abs_epi16(diff); |
| 21250 | let t = _mm256_sub_epi16(thr, _mm256_sra_epi16(a, shift)); |
| 21251 | let t = _mm256_min_epi16(_mm256_max_epi16(t, _mm256_setzero_si256()), a); |
| 21252 | _mm256_sign_epi16(t, diff) |
| 21253 | } |
| 21254 | |
| 21255 | /// [`super::cdef_find_dir_partials`] in AVX2 (libaom |
| 21256 | /// `cdef_find_dir_avx2`'s shape): eight i32 lanes per row instead of the |
| 21257 | /// four the auto-vectorised scalar form gets without the feature. |
| 21258 | /// |
| 21259 | /// # Safety |
| 21260 | /// Requires AVX2. `win` must hold [`super::cdef_gather`]'s 8x8 window |
| 21261 | /// grown by `CDEF_BORDER` at row stride `bstride`. |
| 21262 | #[target_feature(enable = "avx2")] |
| 21263 | pub unsafe fn find_dir_partials_avx2( |
| 21264 | win: &[i16], |
| 21265 | bstride: usize, |
| 21266 | coeff_shift: i32, |
| 21267 | ) -> [[i32; 16]; 8] { |
| 21268 | #[target_feature(enable = "avx2")] |
| 21269 | unsafe fn add8(dst: &mut [i32; 16], off: usize, v: __m256i) { |
| 21270 | let p = dst.as_mut_ptr().add(off) as *mut __m256i; |
| 21271 | _mm256_storeu_si256(p, _mm256_add_epi32(_mm256_loadu_si256(p), v)); |
| 21272 | } |
| 21273 | #[target_feature(enable = "avx2")] |
| 21274 | unsafe fn add4(dst: &mut [i32; 16], off: usize, v: __m128i) { |
| 21275 | let p = dst.as_mut_ptr().add(off) as *mut __m128i; |
| 21276 | _mm_storeu_si128(p, _mm_add_epi32(_mm_loadu_si128(p), v)); |
| 21277 | } |
| 21278 | let mut partial = [[0i32; 16]; 8]; |
| 21279 | let shift = _mm_cvtsi32_si128(coeff_shift); |
| 21280 | let c128 = _mm256_set1_epi32(128); |
| 21281 | let revidx = _mm256_setr_epi32(7, 6, 5, 4, 3, 2, 1, 0); |
| 21282 | let mut p6 = _mm256_setzero_si256(); |
| 21283 | for i in 0..8usize { |
| 21284 | let base = (i + CDEF_BORDER) * bstride + CDEF_BORDER; |
| 21285 | let raw = _mm_loadu_si128(win.as_ptr().add(base) as *const __m128i); |
| 21286 | let row = _mm256_sub_epi32(_mm256_sra_epi32(_mm256_cvtepi16_epi32(raw), shift), c128); |
| 21287 | let rev = _mm256_permutevar8x32_epi32(row, revidx); |
| 21288 | // `hadd(row, row)` is [p0, p1, p0, p1 | p2, p3, p2, p3], so the |
| 21289 | // four pairwise sums are the two lanes' low halves joined. |
| 21290 | let h = _mm256_hadd_epi32(row, row); |
| 21291 | let pair = |
| 21292 | _mm_unpacklo_epi64(_mm256_castsi256_si128(h), _mm256_extracti128_si256(h, 1)); |
| 21293 | let rpair = _mm_shuffle_epi32(pair, 0b00_01_10_11); |
| 21294 | let hs = _mm_hadd_epi32(pair, pair); |
| 21295 | let total = _mm_cvtsi128_si32(_mm_hadd_epi32(hs, hs)); |
| 21296 | add8(&mut partial[0], i, row); |
| 21297 | add4(&mut partial[1], i, pair); |
| 21298 | partial[2][i] = total; |
| 21299 | add4(&mut partial[3], i, rpair); |
| 21300 | add8(&mut partial[4], i, rev); |
| 21301 | add8(&mut partial[5], 3 - i / 2, row); |
| 21302 | p6 = _mm256_add_epi32(p6, row); |
| 21303 | add8(&mut partial[7], i / 2, row); |
| 21304 | } |
| 21305 | _mm256_storeu_si256(partial[6].as_mut_ptr() as *mut __m256i, p6); |
| 21306 | partial |
| 21307 | } |
| 21308 | |
| 21309 | /// [`super::cdef_filter_block_scalar`]'s arithmetic, two rows per |
| 21310 | /// iteration. `bw` is 8 (luma) or 4 (4:2:0 chroma) and `bh` matches; for |
| 21311 | /// `bw == 4` the upper four lanes of each half are window samples read |
| 21312 | /// past the row (never past the buffer -- `w` is padded to 256 entries), |
| 21313 | /// computed and then dropped by the 64-bit store. |
| 21314 | /// |
| 21315 | /// # Safety |
| 21316 | /// Requires AVX2. `src` must be [`super::cdef_gather`]'s 256-entry window |
| 21317 | /// buffer (the tap loads read up to 152 entries past its start, i.e. past |
| 21318 | /// the `(bw + 4) x (bh + 4)` samples themselves) and `dst` the `bh` rows |
| 21319 | /// of `bw` samples at `(ox, oy)`. |
| 21320 | #[target_feature(enable = "avx2")] |
| 21321 | #[allow(clippy::too_many_arguments)] |
| 21322 | pub unsafe fn filter_block_avx2( |
| 21323 | src: &[i16], |
| 21324 | dst: &mut [u16], |
| 21325 | stride: usize, |
| 21326 | ox: usize, |
| 21327 | oy: usize, |
| 21328 | bw: usize, |
| 21329 | bh: usize, |
| 21330 | pri_strength: i32, |
| 21331 | sec_strength: i32, |
| 21332 | dir: usize, |
| 21333 | pri_damping: i32, |
| 21334 | sec_damping: i32, |
| 21335 | enable_primary: bool, |
| 21336 | enable_secondary: bool, |
| 21337 | coeff_shift: i32, |
| 21338 | smax: i32, |
| 21339 | ) { |
| 21340 | let bstride = bw + 2 * CDEF_BORDER; |
| 21341 | // The caller's window is already `i16` and 256 entries long, so the |
| 21342 | // kernel taps it in place: the largest two-row load is |
| 21343 | // (bh + 3) * bstride + 8 <= 152 for the 8x8 luma block, 72 for the |
| 21344 | // 4x4 chroma one, both inside the buffer's zeroed tail. |
| 21345 | let w = src; |
| 21346 | let clipping_required = enable_primary && enable_secondary; |
| 21347 | let pri_taps = CDEF_PRI_TAPS[((pri_strength >> coeff_shift) & 1) as usize]; |
| 21348 | let pri_shift = _mm_cvtsi32_si128(cdef_constrain_shift(pri_strength.max(1), pri_damping)); |
| 21349 | let sec_shift = _mm_cvtsi32_si128(cdef_constrain_shift(sec_strength.max(1), sec_damping)); |
| 21350 | let pri_thr = _mm256_set1_epi16(pri_strength as i16); |
| 21351 | let sec_thr = _mm256_set1_epi16(sec_strength as i16); |
| 21352 | let very_large = _mm256_set1_epi16(CDEF_VERY_LARGE as i16); |
| 21353 | let zero = _mm256_setzero_si256(); |
| 21354 | let eight = _mm256_set1_epi16(8); |
| 21355 | let smaxv = _mm256_set1_epi16(smax as i16); |
| 21356 | let (pri_dir, sec_dir0, sec_dir1) = |
| 21357 | (CDEF_DIRECTIONS[dir], CDEF_DIRECTIONS[(dir + 2) & 7], CDEF_DIRECTIONS[(dir + 6) & 7]); |
| 21358 | let wp = w.as_ptr(); |
| 21359 | let bs = bstride as isize; |
| 21360 | let dp = dst.as_mut_ptr(); |
| 21361 | let mut i = 0usize; |
| 21362 | while i < bh { |
| 21363 | let base = (i + CDEF_BORDER) as isize * bs + CDEF_BORDER as isize; |
| 21364 | let x = ld2(wp, base, bs); |
| 21365 | let mut sum = zero; |
| 21366 | let mut max = x; |
| 21367 | let mut min = x; |
| 21368 | for k in 0..2usize { |
| 21369 | if enable_primary { |
| 21370 | let (tr, tc) = pri_dir[k]; |
| 21371 | let off = tr as isize * bs + tc as isize; |
| 21372 | let p0 = ld2(wp, base + off, bs); |
| 21373 | let p1 = ld2(wp, base - off, bs); |
| 21374 | let tap = _mm256_set1_epi16(pri_taps[k] as i16); |
| 21375 | sum = _mm256_add_epi16( |
| 21376 | sum, |
| 21377 | _mm256_mullo_epi16(tap, constrain(p0, x, pri_thr, pri_shift)), |
| 21378 | ); |
| 21379 | sum = _mm256_add_epi16( |
| 21380 | sum, |
| 21381 | _mm256_mullo_epi16(tap, constrain(p1, x, pri_thr, pri_shift)), |
| 21382 | ); |
| 21383 | if clipping_required { |
| 21384 | // The sentinel is excluded from the max exactly as the |
| 21385 | // scalar `if p != CDEF_VERY_LARGE` does: masking it to |
| 21386 | // 0 cannot win a max whose seed `x` is non-negative. |
| 21387 | max = _mm256_max_epi16( |
| 21388 | max, |
| 21389 | _mm256_andnot_si256(_mm256_cmpeq_epi16(p0, very_large), p0), |
| 21390 | ); |
| 21391 | max = _mm256_max_epi16( |
| 21392 | max, |
| 21393 | _mm256_andnot_si256(_mm256_cmpeq_epi16(p1, very_large), p1), |
| 21394 | ); |
| 21395 | min = _mm256_min_epi16(min, _mm256_min_epi16(p0, p1)); |
| 21396 | } |
| 21397 | } |
| 21398 | if enable_secondary { |
| 21399 | let (tr0, tc0) = sec_dir0[k]; |
| 21400 | let (tr1, tc1) = sec_dir1[k]; |
| 21401 | let off0 = tr0 as isize * bs + tc0 as isize; |
| 21402 | let off1 = tr1 as isize * bs + tc1 as isize; |
| 21403 | let s0 = ld2(wp, base + off0, bs); |
| 21404 | let s1 = ld2(wp, base - off0, bs); |
| 21405 | let s2 = ld2(wp, base + off1, bs); |
| 21406 | let s3 = ld2(wp, base - off1, bs); |
| 21407 | if clipping_required { |
| 21408 | for s in [s0, s1, s2, s3] { |
| 21409 | max = _mm256_max_epi16( |
| 21410 | max, |
| 21411 | _mm256_andnot_si256(_mm256_cmpeq_epi16(s, very_large), s), |
| 21412 | ); |
| 21413 | } |
| 21414 | min = _mm256_min_epi16( |
| 21415 | min, |
| 21416 | _mm256_min_epi16( |
| 21417 | _mm256_min_epi16(s0, s1), |
| 21418 | _mm256_min_epi16(s2, s3), |
| 21419 | ), |
| 21420 | ); |
| 21421 | } |
| 21422 | let c = _mm256_add_epi16( |
| 21423 | _mm256_add_epi16( |
| 21424 | constrain(s0, x, sec_thr, sec_shift), |
| 21425 | constrain(s1, x, sec_thr, sec_shift), |
| 21426 | ), |
| 21427 | _mm256_add_epi16( |
| 21428 | constrain(s2, x, sec_thr, sec_shift), |
| 21429 | constrain(s3, x, sec_thr, sec_shift), |
| 21430 | ), |
| 21431 | ); |
| 21432 | sum = _mm256_add_epi16( |
| 21433 | sum, |
| 21434 | _mm256_mullo_epi16(_mm256_set1_epi16(CDEF_SEC_TAPS[k] as i16), c), |
| 21435 | ); |
| 21436 | } |
| 21437 | } |
| 21438 | // `-1` where `sum < 0`, i.e. the scalar `- i32::from(sum < 0)`. |
| 21439 | let neg = _mm256_cmpgt_epi16(zero, sum); |
| 21440 | let mut y = _mm256_add_epi16( |
| 21441 | x, |
| 21442 | _mm256_srai_epi16(_mm256_add_epi16(_mm256_add_epi16(eight, sum), neg), 4), |
| 21443 | ); |
| 21444 | if clipping_required { |
| 21445 | y = _mm256_min_epi16(_mm256_max_epi16(y, min), max); |
| 21446 | } |
| 21447 | y = _mm256_min_epi16(_mm256_max_epi16(y, zero), smaxv); |
| 21448 | let (lo, hi) = (_mm256_castsi256_si128(y), _mm256_extracti128_si256(y, 1)); |
| 21449 | let o0 = ((oy + i) * stride + ox) as isize; |
| 21450 | let o1 = o0 + stride as isize; |
| 21451 | if bw == 8 { |
| 21452 | _mm_storeu_si128(dp.offset(o0).cast(), lo); |
| 21453 | _mm_storeu_si128(dp.offset(o1).cast(), hi); |
| 21454 | } else { |
| 21455 | _mm_storel_epi64(dp.offset(o0).cast(), lo); |
| 21456 | _mm_storel_epi64(dp.offset(o1).cast(), hi); |
| 21457 | } |
| 21458 | i += 2; |
| 21459 | } |
| 21460 | } |
| 21461 | } |
| 21462 | |
| 21463 | /// Spec 7.14: the in-loop deblocking filter, applied once per frame after |
| 21464 | /// tile reconstruction and before [`apply_cdef`]. Uniform-level path only -- |
| 21465 | /// `stream.rs` already refuses `delta_lf_present`/segmentation upstream, so |
| 21466 | /// every block's level comes from [`LoopFilterParams::level`] plus the |
| 21467 | /// `ref_deltas`/`mode_deltas` adjustment alone (spec 7.14.4's |
| 21468 | /// `get_filter_level`, precomputed per-block here rather than cached in a |
| 21469 | /// table the way libaom's `av1_loop_filter_frame_init` does). |
| 21470 | /// |
| 21471 | /// Ported from libaom's `av1/common/av1_loopfilter.c` (edge/level decision) |
| 21472 | /// and `aom_dsp/loopfilter.c` (the `aom_lpf_*` pixel kernels). |
| 21473 | fn apply_deblock( |
| 21474 | y: &mut PlaneBuf<'static>, |
| 21475 | u: &mut PlaneBuf<'static>, |
| 21476 | v: &mut PlaneBuf<'static>, |
| 21477 | lf: &LoopFilterParams, |
| 21478 | n: &Neighbours, |
| 21479 | frame_width: usize, |
| 21480 | frame_height: usize, fctx: &crate::decode::FrameCtx, |
| 21481 | ) { |
| 21482 | // lane-part32 r2 debug rung, env-gated: bisecting a widespread pixel |
| 21483 | // mismatch between the raw reconstruction and post-filter output. |
| 21484 | if crate::envflags::env_flag!("EC_AV1_DEBUG_SKIP_DEBLOCK") { |
| 21485 | return; |
| 21486 | } |
| 21487 | if lf.level[0] != 0 || lf.level[1] != 0 { |
| 21488 | deblock_plane(y, 0, lf, n, frame_width, frame_height, fctx); |
| 21489 | } |
| 21490 | if lf.level[2] != 0 { |
| 21491 | deblock_plane(u, 1, lf, n, frame_width, frame_height, fctx); |
| 21492 | } |
| 21493 | if lf.level[3] != 0 { |
| 21494 | deblock_plane(v, 2, lf, n, frame_width, frame_height, fctx); |
| 21495 | } |
| 21496 | } |
| 21497 | |
| 21498 | /// [`apply_deblock`] for ONE pass over ONE band of luma rows |
| 21499 | /// (`y_lo..y_hi`), the form [`pipe_run`] calls it in. Running the three |
| 21500 | /// planes' vertical passes and then their three horizontal ones is the same |
| 21501 | /// output as `apply_deblock`'s plane-at-a-time order: the planes are |
| 21502 | /// separate buffers and neither pass reads another plane. |
| 21503 | #[allow(clippy::too_many_arguments)] |
| 21504 | fn deblock_span( |
| 21505 | y: &mut PlaneBuf<'static>, |
| 21506 | u: &mut PlaneBuf<'static>, |
| 21507 | v: &mut PlaneBuf<'static>, |
| 21508 | lf: &LoopFilterParams, |
| 21509 | n: &Neighbours, |
| 21510 | frame_width: usize, |
| 21511 | frame_height: usize, |
| 21512 | y_lo: usize, |
| 21513 | y_hi: usize, |
| 21514 | horz: bool, |
| 21515 | fctx: &crate::decode::FrameCtx, |
| 21516 | ) { |
| 21517 | if lf.level[0] != 0 || lf.level[1] != 0 { |
| 21518 | deblock_plane_span(y, 0, lf, n, frame_width, frame_height, y_lo, y_hi, horz, fctx); |
| 21519 | } |
| 21520 | if lf.level[2] != 0 { |
| 21521 | deblock_plane_span(u, 1, lf, n, frame_width, frame_height, y_lo >> ss_y(fctx), y_hi >> ss_y(fctx), horz, fctx); |
| 21522 | } |
| 21523 | if lf.level[3] != 0 { |
| 21524 | deblock_plane_span(v, 2, lf, n, frame_width, frame_height, y_lo >> ss_y(fctx), y_hi >> ss_y(fctx), horz, fctx); |
| 21525 | } |
| 21526 | } |
| 21527 | |
| 21528 | /// Luma-mi-grid position (row or col) a plane-local pixel coordinate falls |
| 21529 | /// in -- `Neighbours::tx_grid`/`ref_grid` are always indexed at the luma |
| 21530 | /// 4x4-mi grain, even when `coord` is a chroma coordinate (2 chroma px per |
| 21531 | /// mi unit under this decoder's assumed 4:2:0 subsampling). |
| 21532 | /// |
| 21533 | /// lane-t900 r30b: the chroma arm ORs in the subsampling, exactly as |
| 21534 | /// `set_lpf_parameters` does (`av1_loopfilter.c:245`, `mi_col = scale_horz | |
| 21535 | /// ((x << scale_horz) >> MI_SIZE_LOG2)`) -- "for sub8x8 block, chroma |
| 21536 | /// prediction mode is obtained from the bottom/right mi structure of the |
| 21537 | /// co-located 8x8 luma block, so for chroma plane both mi_row and mi_col |
| 21538 | /// should be odd". Dropping that `| 1` read the group's TOP-LEFT mi, which |
| 21539 | /// is the same cell for every block 8x8 and larger and a DIFFERENT coded |
| 21540 | /// block (different tx size, ref frame, skip flag and delta_lf) as soon as |
| 21541 | /// the group is split into 4x4 luma blocks (class |
| 21542 | /// `context-read-from-one-cell`). |
| 21543 | fn plane_to_mi(chroma: bool, coord: usize, ss: usize) -> usize { |
| 21544 | // scale | ((coord << scale) >> 2). ss=1 is (coord/2)|1; ss=0 is coord/4. |
| 21545 | if chroma { ss | ((coord << ss) >> 2) } else { coord / 4 } |
| 21546 | } |
| 21547 | |
| 21548 | /// This plane's own transform width in pixels at the covering block, spec |
| 21549 | /// `get_transform_size`'s plane-aware lookup: `tx_grid` stores the luma |
| 21550 | /// width directly; chroma halves it (this decoder's UV transform is always |
| 21551 | /// half its coded block's luma transform, spec's `av1_get_max_uv_txsize` |
| 21552 | /// under 4:2:0 and a block that never splits its own transform). |
| 21553 | fn tx_px_at(n: &Neighbours, chroma: bool, mi_r: usize, mi_c: usize) -> u32 { |
| 21554 | let grid = if chroma { &n.uv_tx_grid } else { &n.tx_grid }; |
| 21555 | grid.get(mi_r * n.skip_grid_cols_mi + mi_c).copied().unwrap_or(0) as u32 |
| 21556 | } |
| 21557 | |
| 21558 | /// [`tx_px_at`]'s HEIGHT twin (lane-rect r2): the deblocker's horizontal |
| 21559 | /// edges step and gate by transform height, which differs from width only |
| 21560 | /// on rect partition strips. |
| 21561 | fn tx_h_px_at(n: &Neighbours, chroma: bool, mi_r: usize, mi_c: usize) -> u32 { |
| 21562 | let grid = if chroma { &n.uv_tx_h_grid } else { &n.tx_h_grid }; |
| 21563 | grid.get(mi_r * n.skip_grid_cols_mi + mi_c).copied().unwrap_or(0) as u32 |
| 21564 | } |
| 21565 | |
| 21566 | /// `get_tx_size_context` (spec 9.3's `TxDepthCtx`, libaom |
| 21567 | /// `TXFM_CONTEXT`/`entropymode.c`): the sum of two boolean terms, each |
| 21568 | /// present only when its neighbour exists inside the true frame -- whether |
| 21569 | /// the block immediately above (at this block's own leftmost mi column) or |
| 21570 | /// to the left (at its own topmost mi row) coded a transform at least as wide |
| 21571 | /// (tall) as this block's own `max_txsize_rect_lookup[bsize]`. |
| 21572 | /// |
| 21573 | /// lane-av1txbands: this reads the real `TXFM_CONTEXT` bands |
| 21574 | /// ([`Neighbours::above_txfm`]/[`Neighbours::left_txfm`]) for EVERY frame |
| 21575 | /// now -- libaom maintains them on intra-only frames too (`parse_decode_block` |
| 21576 | /// runs `set_txfm_ctxs` for every block, and |
| 21577 | /// [`Neighbours::fill_lf_grid_rect`] publishes them there), except that an |
| 21578 | /// *inter* neighbour contributes its own BLOCK size rather than its transform |
| 21579 | /// size -- and libaom's `is_inter_block` counts an INTRABC block |
| 21580 | /// (`blockd.h:373`), which is what the `above_inter`/`left_inter` bands carry. |
| 21581 | /// The previous deblock-grid approximation coincided with this only while |
| 21582 | /// every neighbour coded one whole-block transform, which is exactly what |
| 21583 | /// `TxMode::Select` ends. |
| 21584 | /// |
| 21585 | /// The rect form (lane-intrasplit r1): `get_tx_size_context` compares the |
| 21586 | /// neighbour ABOVE against `tx_size_wide[max_txsize_rect_lookup[bsize]]` and |
| 21587 | /// the neighbour to the LEFT against `tx_size_high[...]` (libaom |
| 21588 | /// `pred_common.h:342`), which for a 2:1 strip are two different numbers. |
| 21589 | fn tx_size_context_txfm_rect( |
| 21590 | n: &Neighbours, |
| 21591 | (mi_r, mi_c): (usize, usize), |
| 21592 | own_w: usize, |
| 21593 | own_h: usize, fctx: &crate::decode::FrameCtx, |
| 21594 | ) -> usize { |
| 21595 | hit!(INTRA_IN_INTER_TXCTX_HITS); |
| 21596 | // `max_txsize_rect_lookup[bsize]` tops out at TX_64X64: no transform is |
| 21597 | // wider or taller than 64, so a block with a 128 side resolves against 64 |
| 21598 | // (lane-sb128b r1). |
| 21599 | let (own_w, own_h) = (own_w.min(64), own_h.min(64)); |
| 21600 | let has_above = mi_r > n.tile_row0_mi; |
| 21601 | let has_left = mi_c > n.tile_col0_mi; |
| 21602 | let mut above = usize::from(n.above_txfm[mi_c]) >= own_w; |
| 21603 | let mut left = usize::from(n.left_txfm[mi_r]) >= own_h; |
| 21604 | if has_above && n.above_inter[mi_c] { |
| 21605 | above = usize::from(n.above_side_mi[mi_c]) >= own_w; |
| 21606 | } |
| 21607 | if has_left && n.left_inter[mi_r] { |
| 21608 | left = usize::from(n.left_side_mi[mi_r]) >= own_h; |
| 21609 | } |
| 21610 | if crate::envflags::env_flag!("EC_TXCTX") { |
| 21611 | eprintln!( |
| 21612 | "EC_TXCTX mi={mi_r},{mi_c} own={own_w}x{own_h} ha={has_above} hl={has_left} \ |
| 21613 | intra_only={} above_txfm={} left_txfm={} above_inter={} left_inter={} \ |
| 21614 | above_side={} left_side={} above={above} left={left}", |
| 21615 | fctx.intra_only.with(std::cell::Cell::get), |
| 21616 | n.above_txfm[mi_c], |
| 21617 | n.left_txfm[mi_r], |
| 21618 | n.above_inter[mi_c], |
| 21619 | n.left_inter[mi_r], |
| 21620 | n.above_side_mi[mi_c], |
| 21621 | n.left_side_mi[mi_r], |
| 21622 | ); |
| 21623 | } |
| 21624 | let ctx = match (has_above, has_left) { |
| 21625 | (true, true) => usize::from(above) + usize::from(left), |
| 21626 | (true, false) => usize::from(above), |
| 21627 | (false, true) => usize::from(left), |
| 21628 | (false, false) => 0, |
| 21629 | }; |
| 21630 | { |
| 21631 | let stale = usize::from(has_above && tx_px_at(n, false, mi_r - 1, mi_c) as usize >= own_w) |
| 21632 | + usize::from(has_left && tx_h_px_at(n, false, mi_r, mi_c - 1) as usize >= own_h); |
| 21633 | if ctx != stale { |
| 21634 | hit!(TXCTX_DEBLOCK_DIVERGENCE_HITS); |
| 21635 | } |
| 21636 | } |
| 21637 | ctx |
| 21638 | } |
| 21639 | |
| 21640 | /// lane-intrasplit r3: `above_inter`/`left_inter` are mi-granular bands (they |
| 21641 | /// are written and read everywhere else with a raw mi index); the SUB-cell |
| 21642 | /// division this pair used read a cell four blocks away, so the inter-neighbour |
| 21643 | /// override above fired on the wrong neighbour -- the first divergence of the |
| 21644 | /// 10-bit gate's stream was exactly this (`tx_depth` ctx 0 where libaom has 1, |
| 21645 | /// at the intra block right of an inter one). |
| 21646 | fn tx_size_context_txfm(n: &Neighbours, (mi_r, mi_c): (usize, usize), side: usize) -> usize { |
| 21647 | let has_above = mi_r > n.tile_row0_mi; |
| 21648 | let has_left = mi_c > n.tile_col0_mi; |
| 21649 | let mut above = usize::from(n.above_txfm[mi_c]) >= side; |
| 21650 | let mut left = usize::from(n.left_txfm[mi_r]) >= side; |
| 21651 | // lane-angledelta r1 (class [[context-read-from-one-cell]]): both |
| 21652 | // `above_inter`/`left_inter` bands are written per MI unit |
| 21653 | // (`record_inter_rect_mi`), so they are read per MI too -- the `/ (SUB / |
| 21654 | // MI)` here read the 16-px cell's FIRST mi instead, i.e. a different |
| 21655 | // block whenever the neighbour column/row is not the cell's first. A |
| 21656 | // 16x16 intra block whose left neighbour is an inter block at mi row 4 |
| 21657 | // then missed libaom's "an inter neighbour contributes its BLOCK size" |
| 21658 | // override and read `tx_size_cat1` row 1 where aomdec reads row 2. |
| 21659 | if has_above && n.above_inter[mi_c] { |
| 21660 | above = usize::from(n.above_side_mi[mi_c]) >= side; |
| 21661 | } |
| 21662 | if has_left && n.left_inter[mi_r] { |
| 21663 | left = usize::from(n.left_side_mi[mi_r]) >= side; |
| 21664 | } |
| 21665 | if crate::envflags::env_flag!("EC_TXCTX") { |
| 21666 | eprintln!( |
| 21667 | "EC_TXCTX mi={mi_r},{mi_c} own={side} ha={has_above} hl={has_left} \ |
| 21668 | above_txfm={} left_txfm={} above_inter={} left_inter={} above_side={} left_side={} \ |
| 21669 | above={above} left={left}", |
| 21670 | n.above_txfm[mi_c], |
| 21671 | n.left_txfm[mi_r], |
| 21672 | n.above_inter[mi_c], |
| 21673 | n.left_inter[mi_r], |
| 21674 | n.above_side_mi[mi_c], |
| 21675 | n.left_side_mi[mi_r], |
| 21676 | ); |
| 21677 | } |
| 21678 | match (has_above, has_left) { |
| 21679 | (true, true) => usize::from(above) + usize::from(left), |
| 21680 | (true, false) => usize::from(above), |
| 21681 | (false, true) => usize::from(left), |
| 21682 | (false, false) => 0, |
| 21683 | } |
| 21684 | } |
| 21685 | |
| 21686 | /// `read_tx_size`/`read_selected_tx_size` (spec 5.11.16): under |
| 21687 | /// `TxMode::Select`, an intra block's `tx_depth` symbol, resolved straight to |
| 21688 | /// the transform's own side in pixels (`side >> depth`) -- this decoder's |
| 21689 | /// four square categories (8/16/32/64) each halve at most twice, matching |
| 21690 | /// libaom's own per-size depth cap (`cdf::TX_SIZE_CAT0`..`TX_SIZE_CAT3`). |
| 21691 | fn read_tx_size( |
| 21692 | dec: &mut SymbolDecoder, |
| 21693 | cdfs: &mut Cdfs, |
| 21694 | n: &Neighbours, |
| 21695 | at_mi: (usize, usize), |
| 21696 | side: usize, |
| 21697 | fctx: &crate::decode::FrameCtx, |
| 21698 | ) -> usize { |
| 21699 | // libaom `read_tx_size`'s FIRST line (decodeframe.c:1203): |
| 21700 | // `if (xd->lossless[xd->mi[0]->segment_id]) return TX_4X4;` -- before the |
| 21701 | // `tx_mode == TX_MODE_SELECT` test and before any symbol. Invisible while |
| 21702 | // every lossless frame was ALSO `CodedLossless` (its `tx_mode` is forced |
| 21703 | // `Only4x4`, so this function never ran); a MIXED frame keeps |
| 21704 | // `TxMode::Select`, so its lossless segments reach the symbol read and |
| 21705 | // must take the early return (lane-av1mixloss). |
| 21706 | if lossless(fctx) { |
| 21707 | return 4; |
| 21708 | } |
| 21709 | // `max_txsize_rect_lookup[bsize]` (libaom `read_selected_tx_size` / |
| 21710 | // `get_tx_size_context`): a block wider than 64 still tops out at |
| 21711 | // TX_64X64, so BLOCK_128X128 reads BLOCK_64X64's own category (its depth |
| 21712 | // chain 64->32->16->8->4 is the same four steps) and resolves against 64, |
| 21713 | // never against its own 128 side (lane-sb128b r1). |
| 21714 | let max_tx = side.min(64); |
| 21715 | let ctx = tx_size_context_txfm(n, at_mi, max_tx); |
| 21716 | let depth = match max_tx { |
| 21717 | 8 => { |
| 21718 | dec.symbol(&mut cdfs.tx_size_cat0[ctx]) |
| 21719 | } |
| 21720 | 16 => { |
| 21721 | dec.symbol(&mut cdfs.tx_size_cat1[ctx]) |
| 21722 | } |
| 21723 | 32 => { |
| 21724 | dec.symbol(&mut cdfs.tx_size_cat2[ctx]) |
| 21725 | } |
| 21726 | 64 => { |
| 21727 | dec.symbol(&mut cdfs.tx_size_cat3[ctx]) |
| 21728 | } |
| 21729 | _ => unreachable!("decode_block/decode_leaf8 only call this at 8/16/32/64/128"), |
| 21730 | }; |
| 21731 | if crate::envflags::env_flag!("EC_TRACE_MODE_STEP") { |
| 21732 | let (rng, _) = dec.debug_state(); |
| 21733 | let (mi_r, mi_c) = at_mi; |
| 21734 | eprintln!( |
| 21735 | "EC_ISTEP mi_row={mi_r} mi_col={mi_c} name=tx_depth val={depth} ctx={ctx} rng={rng}" |
| 21736 | ); |
| 21737 | } |
| 21738 | if depth != 0 { |
| 21739 | hit!(TX_DEPTH_HITS); |
| 21740 | } |
| 21741 | max_tx >> depth |
| 21742 | } |
| 21743 | |
| 21744 | /// `txfm_partition_context` (libaom `av1_common_int.h`): an inter block's |
| 21745 | /// `txfm_split` context -- a category from the block's own largest square |
| 21746 | /// transform and whether this unit already sits below it, times three, plus |
| 21747 | /// the two neighbour terms (whether the transform last written above/left of |
| 21748 | /// this unit is narrower/shorter than the unit itself). |
| 21749 | #[allow(dead_code)] // read only from the `#[cfg(test)]` gates |
| 21750 | fn txfm_partition_ctx(above_px: u8, left_px: u8, blk_max_px: usize, tx_px: usize) -> usize { |
| 21751 | txfm_partition_ctx_rect(above_px, left_px, blk_max_px, tx_px, tx_px) |
| 21752 | } |
| 21753 | |
| 21754 | /// [`txfm_partition_ctx`] for a *rectangular* transform (lane-inter4 r2): |
| 21755 | /// libaom's `txfm_partition_context` compares the above context against |
| 21756 | /// `tx_size_wide` and the left one against `tx_size_high`, and takes its |
| 21757 | /// category from `txsize_sqr_up_map[tx_size]` -- all three collapse to one |
| 21758 | /// side for a square transform, which is why the square form above is a |
| 21759 | /// special case of this one. |
| 21760 | pub(crate) fn txfm_partition_ctx_rect( |
| 21761 | above_px: u8, |
| 21762 | left_px: u8, |
| 21763 | blk_max_px: usize, |
| 21764 | tx_w: usize, |
| 21765 | tx_h: usize, |
| 21766 | ) -> usize { |
| 21767 | let above = usize::from(usize::from(above_px) < tx_w); |
| 21768 | let left = usize::from(usize::from(left_px) < tx_h); |
| 21769 | let tx_px = tx_w.max(tx_h); |
| 21770 | // `get_sqr_tx_size(max(block_size_wide, block_size_high))`, capped at the |
| 21771 | // largest transform (`TX_64X64`); `TX_4X4`..`TX_64X64` index as |
| 21772 | // `log2(px) - 2`. |
| 21773 | let max_tx = blk_max_px.min(64); |
| 21774 | let max_idx = max_tx.trailing_zeros() as usize - 2; |
| 21775 | let category = usize::from(tx_px != max_tx && max_tx > 8) + (4 - max_idx) * 2; |
| 21776 | category * 3 + above + left |
| 21777 | } |
| 21778 | |
| 21779 | /// `txfm_partition_update` (libaom `av1_common_int.h`): once a var-tx unit |
| 21780 | /// resolves, every mi cell its *parent* unit (`txb_px`) spans records the |
| 21781 | /// resolved transform's own size. |
| 21782 | #[allow(dead_code)] // read only from the `#[cfg(test)]` gates |
| 21783 | fn txfm_partition_update( |
| 21784 | n: &mut Neighbours, |
| 21785 | at_mi: (usize, usize), |
| 21786 | tx_px: usize, |
| 21787 | txb_px: usize, |
| 21788 | ) { |
| 21789 | txfm_partition_update_rect(n, at_mi, (tx_px, tx_px), (txb_px, txb_px)); |
| 21790 | } |
| 21791 | |
| 21792 | /// [`txfm_partition_update`] for a rectangular transform (lane-inter4 r2): |
| 21793 | /// the left column records `tx_size_high` over the parent's own height and |
| 21794 | /// the above row records `tx_size_wide` over its width. |
| 21795 | /// libaom `set_txfm_ctxs` + `txfm_partition_update` (`decodeframe.c`, end of |
| 21796 | /// `decode_token_recon_block`): EVERY block of an inter frame publishes its |
| 21797 | /// TRANSFORM size into both `TXFM_CONTEXT` bands, intra blocks included. The |
| 21798 | /// key frame writes neither (its readers use the deblock grid), so the |
| 21799 | /// key-frame bodies reused by [`decode_intra_rect_in_inter`] have to do it |
| 21800 | /// themselves when they run inside an inter frame. The 2:1 and 16x4 arms do it |
| 21801 | /// at the call site (the transform size comes back to them); the 1:4 arms |
| 21802 | /// ([`decode_block_rect4`] / [`decode_block_rect64`]) keep their size inside, |
| 21803 | /// so they call this where their own depth resolves. A no-op on a key frame |
| 21804 | /// ([`INTRA_IN_INTER_MODE`] unset), where those bands are not read at all. |
| 21805 | /// |
| 21806 | /// lane-t900 r1 root cause: the HORZ_4 32x8 intra strip at mi(0,312) of an |
| 21807 | /// inter frame left `left_txfm[0]` at a stale 32 where libaom's own |
| 21808 | /// `set_txfm_ctxs` writes 8, so the intra 8x16 block at mi(0,320) read |
| 21809 | /// `tx_size_cat1` row 1 instead of row 0 -- same depth VALUE, different CDF, |
| 21810 | /// range 39156 where the reference has 54736 (class tx-grid-published-block-side |
| 21811 | /// / early-return-skips-tail). |
| 21812 | /// |
| 21813 | /// lane-av1-intrabc r3: an `allow_intrabc` KEY frame reads the same bands -- |
| 21814 | /// `read_intra_frame_mode_info` returns early for an intrabc block, so |
| 21815 | /// [`read_block_tx_size_rect`]'s var-tx tree is the intra path's one |
| 21816 | /// `txfm_partition_context` reader, and it reads whatever the block ABOVE and |
| 21817 | /// the block LEFT published. [`decode_block`] has published on such a frame |
| 21818 | /// since lane-t900 r32; the RECT strips did not, so the band still held the |
| 21819 | /// value of an older, smaller neighbour -- measured on the 256x192 testsrc2 |
| 21820 | /// cq45 var-tx arm: the coded 16x8 intrabc strip at mi(38,48) read ctx 14 |
| 21821 | /// where libaom reads 13 (`left_ctx` 4 vs the 8 the 16x8 intra strip at |
| 21822 | /// mi(38,44) should have published), desyncing the tile there. |
| 21823 | fn publish_txfm_bands_if_in_inter( |
| 21824 | n: &mut Neighbours, |
| 21825 | at_mi: (usize, usize), |
| 21826 | (tx_w, tx_h): (usize, usize), |
| 21827 | (bw, bh): (usize, usize), fctx: &crate::decode::FrameCtx, |
| 21828 | ) { |
| 21829 | if fctx.intra_in_inter_mode.with(std::cell::Cell::get).is_none() |
| 21830 | && !allow_intrabc_frame(fctx) |
| 21831 | { |
| 21832 | return; |
| 21833 | } |
| 21834 | let (mi_r, mi_c) = at_mi; |
| 21835 | txfm_partition_update_rect(n, at_mi, (tx_w, tx_h), (bw, bh)); |
| 21836 | for i in 0..bw / MI { |
| 21837 | if let Some(cell) = n.above_txfm.get_mut(mi_c + i) { |
| 21838 | *cell = tx_w as u8; |
| 21839 | } |
| 21840 | } |
| 21841 | for i in 0..bh / MI { |
| 21842 | if let Some(cell) = n.left_txfm.get_mut(mi_r + i) { |
| 21843 | *cell = tx_h as u8; |
| 21844 | } |
| 21845 | } |
| 21846 | } |
| 21847 | |
| 21848 | fn txfm_partition_update_rect( |
| 21849 | n: &mut Neighbours, |
| 21850 | (mi_r, mi_c): (usize, usize), |
| 21851 | (tx_w, tx_h): (usize, usize), |
| 21852 | (txb_w, txb_h): (usize, usize), |
| 21853 | ) { |
| 21854 | if crate::envflags::env_flag!("EC_TXUPD") { |
| 21855 | eprintln!("EC_TXUPD rect mi=({},{}) tx=({tx_w},{tx_h}) txb=({txb_w},{txb_h})", mi_r, mi_c); |
| 21856 | } |
| 21857 | for i in 0..txb_h / MI { |
| 21858 | if let Some(cell) = n.left_txfm.get_mut(mi_r + i) { |
| 21859 | *cell = tx_h as u8; |
| 21860 | } |
| 21861 | } |
| 21862 | for i in 0..txb_w / MI { |
| 21863 | if let Some(cell) = n.above_txfm.get_mut(mi_c + i) { |
| 21864 | *cell = tx_w as u8; |
| 21865 | } |
| 21866 | } |
| 21867 | } |
| 21868 | |
| 21869 | /// `set_txfm_ctxs` (libaom `av1_common_int.h`): the non-var-tx write of the |
| 21870 | /// same contexts -- a skipped *inter* block records its own block size rather |
| 21871 | /// than its transform size. |
| 21872 | fn set_txfm_ctxs( |
| 21873 | n: &mut Neighbours, |
| 21874 | (mi_r, mi_c): (usize, usize), |
| 21875 | tx_px: usize, |
| 21876 | w_mi: usize, |
| 21877 | h_mi: usize, |
| 21878 | skip_inter: bool, |
| 21879 | ) { |
| 21880 | if crate::envflags::env_flag!("EC_TXUPD") { |
| 21881 | eprintln!("EC_TXUPD ctxs mi=({mi_r},{mi_c}) tx={tx_px} wh=({w_mi},{h_mi}) skip_inter={skip_inter}"); |
| 21882 | } |
| 21883 | let bw = if skip_inter { w_mi * MI } else { tx_px }; |
| 21884 | let bh = if skip_inter { h_mi * MI } else { tx_px }; |
| 21885 | for i in 0..w_mi { |
| 21886 | if let Some(cell) = n.above_txfm.get_mut(mi_c + i) { |
| 21887 | *cell = bw as u8; |
| 21888 | } |
| 21889 | } |
| 21890 | for i in 0..h_mi { |
| 21891 | if let Some(cell) = n.left_txfm.get_mut(mi_r + i) { |
| 21892 | *cell = bh as u8; |
| 21893 | } |
| 21894 | } |
| 21895 | } |
| 21896 | |
| 21897 | /// The value libaom initialises both transform contexts to -- NOT zero: |
| 21898 | /// `av1_zero_above_context` memsets the txfm row to |
| 21899 | /// `tx_size_wide[TX_SIZES_LARGEST]` and `av1_zero_left_context` does the same |
| 21900 | /// with `tx_size_high[TX_SIZES_LARGEST]` (`av1_common_int.h`), i.e. 64 |
| 21901 | /// pixels, so an unwritten neighbour reads as the *widest* possible |
| 21902 | /// transform, not the narrowest. |
| 21903 | pub(crate) const TXFM_CTX_INIT: u8 = 64; |
| 21904 | |
| 21905 | /// libaom's `MAX_VARTX_DEPTH`: an inter block's transform tree halves at most |
| 21906 | /// twice below its own largest transform. |
| 21907 | const MAX_VARTX_DEPTH: usize = 2; |
| 21908 | |
| 21909 | /// libaom `sub_tx_size_map[TX_SIZES_ALL]` (`common_data.c`): the transform |
| 21910 | /// one `txfm_split` step below `tx_size`. RECTANGULAR for the 1:4 shapes |
| 21911 | /// (`[TX_64X16] == TX_32X16`, `[TX_16X64] == TX_16X32`) and for the 4:1 |
| 21912 | /// small ones, square everywhere else. |
| 21913 | fn sub_tx_size_map(tx_w: usize, tx_h: usize) -> (usize, usize) { |
| 21914 | match (tx_w, tx_h) { |
| 21915 | (4, 4) => (4, 4), |
| 21916 | (8, 8) => (4, 4), |
| 21917 | (16, 16) => (8, 8), |
| 21918 | (32, 32) => (16, 16), |
| 21919 | (64, 64) => (32, 32), |
| 21920 | (4, 8) | (8, 4) => (4, 4), |
| 21921 | (8, 16) | (16, 8) => (8, 8), |
| 21922 | (16, 32) | (32, 16) => (16, 16), |
| 21923 | (32, 64) | (64, 32) => (32, 32), |
| 21924 | (4, 16) => (4, 8), |
| 21925 | (16, 4) => (8, 4), |
| 21926 | (8, 32) => (8, 16), |
| 21927 | (32, 8) => (16, 8), |
| 21928 | (16, 64) => (16, 32), |
| 21929 | (64, 16) => (32, 16), |
| 21930 | _ => unreachable!("not a TX_SIZES_ALL shape: {tx_w}x{tx_h}"), |
| 21931 | } |
| 21932 | } |
| 21933 | |
| 21934 | #[test] |
| 21935 | fn sub_tx_size_map_matches_libaom() { |
| 21936 | // libaom `common_data.c` `sub_tx_size_map`, in TX_SIZES_ALL order. |
| 21937 | const TABLE: [((usize, usize), (usize, usize)); 19] = [ |
| 21938 | ((4, 4), (4, 4)), ((8, 8), (4, 4)), ((16, 16), (8, 8)), ((32, 32), (16, 16)), |
| 21939 | ((64, 64), (32, 32)), ((4, 8), (4, 4)), ((8, 4), (4, 4)), ((8, 16), (8, 8)), |
| 21940 | ((16, 8), (8, 8)), ((16, 32), (16, 16)), ((32, 16), (16, 16)), ((32, 64), (32, 32)), |
| 21941 | ((64, 32), (32, 32)), ((4, 16), (4, 8)), ((16, 4), (8, 4)), ((8, 32), (8, 16)), |
| 21942 | ((32, 8), (16, 8)), ((16, 64), (16, 32)), ((64, 16), (32, 16)), |
| 21943 | ]; |
| 21944 | for (tx, want) in TABLE { |
| 21945 | assert_eq!(sub_tx_size_map(tx.0, tx.1), want, "sub_tx_size_map({tx:?})"); |
| 21946 | } |
| 21947 | } |
| 21948 | |
| 21949 | /// The domain of var-tx leaf sizes, enumerated against the two refusals that |
| 21950 | /// name a leaf "larger than 32x32" / "larger than 64x64". |
| 21951 | /// |
| 21952 | /// [`read_var_tx_size`] emits a leaf in exactly three places, and each one is |
| 21953 | /// either the unit it was entered with or a `sub_tx_size_map` step below it: |
| 21954 | /// the depth cap, the `!split` return, and libaom's `sub_txs == TX_4X4` early |
| 21955 | /// return. So the leaf set reachable from an entry unit is that unit plus the |
| 21956 | /// chain of `sub_tx_size_map` steps under it, capped at `MAX_VARTX_DEPTH` -- |
| 21957 | /// which this test enumerates, and over which no leaf is ever larger than the |
| 21958 | /// entry. |
| 21959 | /// |
| 21960 | /// That closes both refusals: |
| 21961 | /// |
| 21962 | /// * [`read_block_tx_size_rect`]'s `bw.max(bh) > 64` branch enters every unit |
| 21963 | /// at TX_64X64 (`max_txsize_rect_lookup[BLOCK_128X64]`), so "larger than |
| 21964 | /// 64x64" names a leaf the tree cannot produce; |
| 21965 | /// * [`read_block_tx_size`] enters at `side.min(64)` with a ceiling of 32 |
| 21966 | /// below the 128 root. The only entry that exceeds that ceiling is 64x64, |
| 21967 | /// reached only when `side == 64`, and then the block is ONE unit whose only |
| 21968 | /// above-ceiling leaf is the whole 64x64 unit itself -- which is the `single` |
| 21969 | /// case the refusal is guarded by. |
| 21970 | #[test] |
| 21971 | fn a_var_tx_tree_never_presents_a_leaf_larger_than_the_unit_it_entered() { |
| 21972 | // Every leaf `read_var_tx_size` can reach from `entry`, as (depth, w, h). |
| 21973 | fn reachable(entry: (usize, usize), depth: usize, out: &mut Vec<(usize, usize, usize)>) { |
| 21974 | out.push((depth, entry.0, entry.1)); |
| 21975 | if depth == MAX_VARTX_DEPTH { |
| 21976 | return; |
| 21977 | } |
| 21978 | let sub = sub_tx_size_map(entry.0, entry.1); |
| 21979 | if sub == (4, 4) { |
| 21980 | out.push((depth + 1, 4, 4)); |
| 21981 | return; |
| 21982 | } |
| 21983 | reachable(sub, depth + 1, out); |
| 21984 | } |
| 21985 | |
| 21986 | for entry in [(4, 4), (8, 8), (16, 16), (32, 32), (64, 64)] { |
| 21987 | let mut leaves = Vec::new(); |
| 21988 | reachable(entry, 0, &mut leaves); |
| 21989 | for (depth, w, h) in &leaves { |
| 21990 | assert!( |
| 21991 | w.max(h) <= &entry.0.max(entry.1), |
| 21992 | "a {}x{} unit reaches a {w}x{h} leaf at depth {depth}", |
| 21993 | entry.0, |
| 21994 | entry.1 |
| 21995 | ); |
| 21996 | } |
| 21997 | // Above-ceiling leaves of a 64x64 entry: the entry itself, at depth 0. |
| 21998 | if entry == (64, 64) { |
| 21999 | let above: Vec<&(usize, usize, usize)> = |
| 22000 | leaves.iter().filter(|(_, w, h)| w.max(h) > &32).collect(); |
| 22001 | assert_eq!(above, [&(0usize, 64usize, 64usize)], "64x64 entry, leaves above 32"); |
| 22002 | } |
| 22003 | } |
| 22004 | |
| 22005 | // The entry size and ceiling each caller uses, over the block sides that |
| 22006 | // reach them (`read_block_tx_size`'s square path; the 128-root rect path |
| 22007 | // enters at a fixed 64). |
| 22008 | for side in [8usize, 16, 32, 64, 128] { |
| 22009 | let max_tx = side.min(64); |
| 22010 | let ceiling = if side > 64 { 64 } else { 32 }; |
| 22011 | let units = (side / max_tx) * (side / max_tx); |
| 22012 | if max_tx <= ceiling { |
| 22013 | continue; |
| 22014 | } |
| 22015 | assert_eq!( |
| 22016 | (side, units), |
| 22017 | (64, 1), |
| 22018 | "a {side}px block enters var-tx at {max_tx} over a ceiling of {ceiling} in \ |
| 22019 | {units} units -- an above-ceiling leaf there would NOT be the `single` case" |
| 22020 | ); |
| 22021 | } |
| 22022 | } |
| 22023 | |
| 22024 | /// `read_var_tx_size` (spec 5.11.17, libaom `decodeframe.c` |
| 22025 | /// `read_tx_size_vartx`): the recursive `txfm_split` tree of one |
| 22026 | /// max-transform-sized unit of an inter block, collecting the resolved leaf |
| 22027 | /// transform units into `out` as `(row offset, col offset, side)` in mi units |
| 22028 | /// -- exactly the order `decode_reconstruct_tx` later reads their |
| 22029 | /// coefficients in. `blk_row`/`blk_col` are mi offsets inside the block; |
| 22030 | /// `max_w_mi`/`max_h_mi` clip it to the true frame edge (`max_block_wide`/ |
| 22031 | /// `max_block_high`). |
| 22032 | #[allow(clippy::too_many_arguments)] |
| 22033 | fn read_var_tx_size( |
| 22034 | dec: &mut SymbolDecoder, |
| 22035 | cdfs: &mut Cdfs, |
| 22036 | n: &mut Neighbours, |
| 22037 | at_mi: (usize, usize), |
| 22038 | blk_max_px: usize, |
| 22039 | (max_w_mi, max_h_mi): (usize, usize), |
| 22040 | (tx_w, tx_h): (usize, usize), |
| 22041 | depth: usize, |
| 22042 | blk_row: usize, |
| 22043 | blk_col: usize, |
| 22044 | out: &mut Vec<(usize, usize, usize, usize)>, |
| 22045 | ) { |
| 22046 | if blk_row >= max_h_mi || blk_col >= max_w_mi { |
| 22047 | return; |
| 22048 | } |
| 22049 | let unit = (at_mi.0 + blk_row, at_mi.1 + blk_col); |
| 22050 | if depth == MAX_VARTX_DEPTH { |
| 22051 | out.push((blk_row, blk_col, tx_w, tx_h)); |
| 22052 | txfm_partition_update_rect(n, unit, (tx_w, tx_h), (tx_w, tx_h)); |
| 22053 | return; |
| 22054 | } |
| 22055 | let ctx = txfm_partition_ctx_rect( |
| 22056 | n.above_txfm[unit.1], |
| 22057 | n.left_txfm[unit.0], |
| 22058 | blk_max_px, |
| 22059 | tx_w, |
| 22060 | tx_h, |
| 22061 | ); |
| 22062 | let split = dec.symbol(&mut cdfs.txfm_partition[ctx]) == 1; |
| 22063 | hit!(TXFM_SPLIT_READS); |
| 22064 | if crate::envflags::env_flag!("EC_TRACE_MODE_STEP") { |
| 22065 | let (rng, _) = dec.debug_state(); |
| 22066 | eprintln!( |
| 22067 | "EC_ISTEP mi_row={} mi_col={} name=txfm_split val={} ctx={ctx} rng={rng}", |
| 22068 | unit.0, |
| 22069 | unit.1, |
| 22070 | u8::from(split) |
| 22071 | ); |
| 22072 | } |
| 22073 | if !split { |
| 22074 | out.push((blk_row, blk_col, tx_w, tx_h)); |
| 22075 | txfm_partition_update_rect(n, unit, (tx_w, tx_h), (tx_w, tx_h)); |
| 22076 | return; |
| 22077 | } |
| 22078 | hit!(TXFM_SPLIT_HITS); |
| 22079 | let (sub_w, sub_h) = sub_tx_size_map(tx_w, tx_h); |
| 22080 | if (sub_w, sub_h) == (4, 4) { |
| 22081 | // libaom's `sub_txs == TX_4X4` early return: every mi cell of the |
| 22082 | // unit is marked 4x4 (raster order) without recursing. |
| 22083 | for row in 0..tx_h / MI { |
| 22084 | for col in 0..tx_w / MI { |
| 22085 | if blk_row + row < max_h_mi && blk_col + col < max_w_mi { |
| 22086 | out.push((blk_row + row, blk_col + col, 4, 4)); |
| 22087 | } |
| 22088 | } |
| 22089 | } |
| 22090 | txfm_partition_update_rect(n, unit, (4, 4), (tx_w, tx_h)); |
| 22091 | return; |
| 22092 | } |
| 22093 | for row in (0..tx_h / MI).step_by(sub_h / MI) { |
| 22094 | for col in (0..tx_w / MI).step_by(sub_w / MI) { |
| 22095 | read_var_tx_size( |
| 22096 | dec, |
| 22097 | cdfs, |
| 22098 | n, |
| 22099 | at_mi, |
| 22100 | blk_max_px, |
| 22101 | (max_w_mi, max_h_mi), |
| 22102 | (sub_w, sub_h), |
| 22103 | depth + 1, |
| 22104 | blk_row + row, |
| 22105 | blk_col + col, |
| 22106 | out, |
| 22107 | ); |
| 22108 | } |
| 22109 | } |
| 22110 | } |
| 22111 | |
| 22112 | /// `read_block_tx_size` (libaom `decodeframe.c` `parse_decode_block`'s tx |
| 22113 | /// branch): every block of a `TxMode::Select` inter frame codes its transform |
| 22114 | /// size right after its own mode info -- an inter non-skip block through the |
| 22115 | /// recursive var-tx tree ([`read_var_tx_size`], whose leaf list this returns), |
| 22116 | /// every other block through the intra `tx_depth` symbol (a skipped inter |
| 22117 | /// block codes no symbol at all) plus a `set_txfm_ctxs` write. Returns the |
| 22118 | /// block's own resolved transform side and the leaf list, the latter only |
| 22119 | /// when the tree resolved to more than one transform unit. |
| 22120 | #[allow(clippy::too_many_arguments)] |
| 22121 | fn read_block_tx_size( |
| 22122 | dec: &mut SymbolDecoder, |
| 22123 | cdfs: &mut Cdfs, |
| 22124 | n: &mut Neighbours, |
| 22125 | at_mi: (usize, usize), |
| 22126 | side: usize, |
| 22127 | (mi_cols, mi_rows): (usize, usize), |
| 22128 | is_inter: bool, |
| 22129 | skip: bool, fctx: &crate::decode::FrameCtx, |
| 22130 | ) -> Result<(usize, Option<Vec<(usize, usize, usize, usize)>>)> { |
| 22131 | // lane-lossless: TX_4X4 over the whole block, and NO var-tx tree -- libaom |
| 22132 | // `read_block_tx_size` (`decodeframe.c`) sends a lossless block down the |
| 22133 | // `read_tx_size` arm, which returns TX_4X4 before it reads anything. |
| 22134 | if lossless(fctx) { |
| 22135 | let mut leaves = Vec::new(); |
| 22136 | for row in 0..side / MI { |
| 22137 | for col in 0..side / MI { |
| 22138 | leaves.push((row, col, 4, 4)); |
| 22139 | } |
| 22140 | } |
| 22141 | return Ok((4, Some(leaves))); |
| 22142 | } |
| 22143 | if !fctx.tx_select_inter.with(std::cell::Cell::get) { |
| 22144 | // lane-sb128c r1: `max_txsize_rect_lookup[BLOCK_128X128]` is TX_64X64, |
| 22145 | // so a 128x128 inter block is FOUR luma units even with `tx_mode == |
| 22146 | // TX_MODE_LARGEST` -- the same leaf shape the var-tx tree hands back. |
| 22147 | // lane-sb128c r3: intra too -- an INTRA 128x128 block under |
| 22148 | // `TX_MODE_LARGEST` also resolves to TX_64X64, i.e. four luma units. |
| 22149 | if side > 64 { |
| 22150 | let step = 64 / MI; |
| 22151 | let mut leaves = Vec::new(); |
| 22152 | for row in (0..side / MI).step_by(step) { |
| 22153 | for col in (0..side / MI).step_by(step) { |
| 22154 | leaves.push((row, col, 64, 64)); |
| 22155 | } |
| 22156 | } |
| 22157 | return Ok((64, Some(leaves))); |
| 22158 | } |
| 22159 | return Ok((side, None)); |
| 22160 | } |
| 22161 | let side_mi = side / MI; |
| 22162 | if is_inter && !skip { |
| 22163 | let max_tx = side.min(64); |
| 22164 | let max_w_mi = side_mi.min(mi_cols.saturating_sub(at_mi.1)); |
| 22165 | let max_h_mi = side_mi.min(mi_rows.saturating_sub(at_mi.0)); |
| 22166 | let mut leaves = Vec::new(); |
| 22167 | let step = max_tx / MI; |
| 22168 | for row in (0..side_mi).step_by(step) { |
| 22169 | for col in (0..side_mi).step_by(step) { |
| 22170 | read_var_tx_size( |
| 22171 | dec, |
| 22172 | cdfs, |
| 22173 | n, |
| 22174 | at_mi, |
| 22175 | side, |
| 22176 | (max_w_mi, max_h_mi), |
| 22177 | (max_tx, max_tx), |
| 22178 | 0, |
| 22179 | row, |
| 22180 | col, |
| 22181 | &mut leaves, |
| 22182 | ); |
| 22183 | } |
| 22184 | } |
| 22185 | let single = leaves.len() == 1 && leaves[0].2 == side && leaves[0].3 == side; |
| 22186 | // lane-sb128c r6: below the 128 root the ceiling is main's TX_32X32 |
| 22187 | // ([`inter_txbset_for`] has no 64-point *inter* table set); a 128x128 |
| 22188 | // block's own var-tx tree legitimately bottoms out at TX_64X64 leaves, |
| 22189 | // which the mu-chunk reader above codes as their 32x32 coded corner. |
| 22190 | let leaf_ceiling = if side > 64 { 64 } else { 32 }; |
| 22191 | if !single && leaves.iter().any(|leaf| leaf.2.max(leaf.3) > leaf_ceiling) { |
| 22192 | // Only a block wider than a superblock's own 64x64 max transform |
| 22193 | // reaches here; this crate has no 64-point *inter* coefficient |
| 22194 | // table set ([`inter_txbset_for`] stops at 32). |
| 22195 | return Err(unsupported( |
| 22196 | "an inter var-tx tree with a leaf transform larger than 32x32", |
| 22197 | )); |
| 22198 | } |
| 22199 | let resolved = leaves.last().map_or(side, |l| l.2); |
| 22200 | return Ok((resolved, (!single).then_some(leaves))); |
| 22201 | } |
| 22202 | let tx = if is_inter { |
| 22203 | side.min(64) |
| 22204 | } else { |
| 22205 | read_tx_size(dec, cdfs, n, at_mi, side, fctx) |
| 22206 | }; |
| 22207 | set_txfm_ctxs(n, at_mi, tx, side_mi, side_mi, skip && is_inter); |
| 22208 | if !is_inter && tx != side { |
| 22209 | // lane-intrainter r1: an intra block inside an inter frame whose |
| 22210 | // `tx_depth` split its luma transform codes exactly the key-frame |
| 22211 | // split path's transform units (`decode_block`'s multi-TU loop), |
| 22212 | // raster order over the block. Handed back in the same leaf shape the |
| 22213 | // inter var-tx tree uses, so the caller's per-TU luma loop, the |
| 22214 | // `record_split_luma` neighbour write-back and the per-TU deblock |
| 22215 | // edges are the ones it already runs for a var-tx inter block. A |
| 22216 | // caller that has no per-TU intra loop (the 8x8 leaf) refuses on |
| 22217 | // seeing `Some` here. |
| 22218 | let step = tx / MI; |
| 22219 | let mut leaves = Vec::new(); |
| 22220 | for row in (0..side_mi).step_by(step) { |
| 22221 | for col in (0..side_mi).step_by(step) { |
| 22222 | leaves.push((row, col, tx, tx)); |
| 22223 | } |
| 22224 | } |
| 22225 | hit_do! { |
| 22226 | INTRA_IN_INTER_SPLIT_TX_HITS.with(|c| { |
| 22227 | let mut h = c.get(); |
| 22228 | h[(side.trailing_zeros() as usize).saturating_sub(3).min(3)] += 1; |
| 22229 | c.set(h); |
| 22230 | }); |
| 22231 | } |
| 22232 | return Ok((tx, Some(leaves))); |
| 22233 | } |
| 22234 | Ok((tx, None)) |
| 22235 | } |
| 22236 | |
| 22237 | /// lane-inter4 r2: the shapes whose *rectangular inter* residual this |
| 22238 | /// decoder codes -- a 2:1 strip whose own max rect transform |
| 22239 | /// (`max_txsize_rect_lookup[BLOCK_32X16] == TX_32X16`) has both a |
| 22240 | /// coefficient table set ([`TxbSet::LumaRect32x16Inter`], 512-coefficient |
| 22241 | /// `eob_pt`) and a `tx_type` alphabet here. 64x32/64x16 strips would need a |
| 22242 | /// 64-point rect transform, and 16x8/8x16 needs |
| 22243 | /// `default_inter_ext_tx_cdf[2][TX_8X8]` (the 12-symbol |
| 22244 | /// `DTT9_IDTX_1DDCT` row at `tx_size_sqr == TX_8X8`, only reachable through a |
| 22245 | /// rect transform), neither of which exists yet -- both stay refused by name. |
| 22246 | pub(crate) fn rect_inter_residual_supported(w: usize, h: usize) -> bool { |
| 22247 | matches!( |
| 22248 | (w, h), |
| 22249 | // 2:1 strips whose transform is coded in full (lane-inter4). |
| 22250 | (32, 16) | (16, 32) | (16, 8) | (8, 16) |
| 22251 | // lane-inter16ab r2: the 16x16-level 1:4 strips. TX_16X4/TX_4X16 |
| 22252 | // codes all 64 of its positions, and its chroma pair is an 8x4 / |
| 22253 | // 4x8 unit (`ss_size_lookup[BLOCK_16X4]` = BLOCK_8X4). |
| 22254 | | (16, 4) | (4, 16) |
| 22255 | // lane-r14 r2: strips with a 64-px axis. `av1_get_max_eob` codes |
| 22256 | // only the low 32 coefficients of a 64-length axis, so these read |
| 22257 | // exactly the truncated corner the INTRA superblock-strip path |
| 22258 | // already reads (`read_intra_mode_rect`), through |
| 22259 | // `av1_get_adjusted_tx_size`'s CDF set. Their `tx_size_sqr_up` is |
| 22260 | // `TX_64X64`, so `av1_get_ext_tx_set_type` is `DCT_ONLY` and no |
| 22261 | // `tx_type` symbol is coded at all. |
| 22262 | | (64, 32) | (32, 64) | (64, 16) | (16, 64) |
| 22263 | // lane-inter16ab r4: `sub_tx_size_map[TX_16X4] == TX_8X4`, so a |
| 22264 | // split-transform 16x4/4x16 strip has RECTANGULAR 8x4/4x8 leaves. |
| 22265 | // Only reachable as a var-tx LEAF -- an 8x4/4x8 inter BLOCK is |
| 22266 | // refused earlier ("an inter partition below 8x8"). |
| 22267 | | (8, 4) | (4, 8) |
| 22268 | // lane-sb128c r7: the 128 root's HORZ/VERT halves. These are NOT |
| 22269 | // one rectangular transform -- `max_txsize_rect_lookup |
| 22270 | // [BLOCK_128X64]` is TX_64X64, so the block tiles into two square |
| 22271 | // TX_64X64 units read through the mu-chunk loop, same as the |
| 22272 | // 128x128 NONE block's four. |
| 22273 | | (128, 64) | (64, 128) |
| 22274 | // lane-rectres r1: the 32x32-level 1:4 strips -- THE shape both |
| 22275 | // 10-bit film cuts and every measured 1080p offset stop on |
| 22276 | // (measured: 6/6 probes refuse at 32x8 or 8x32, nothing else). |
| 22277 | // `max_txsize_rect_lookup[BLOCK_32X8] == TX_32X8` codes all 256 |
| 22278 | // positions, and its chroma pair is a 16x4 / 4x16 unit |
| 22279 | // (`ss_size_lookup[BLOCK_32X8]` = BLOCK_16X4). |
| 22280 | | (32, 8) | (8, 32) |
| 22281 | ) |
| 22282 | } |
| 22283 | |
| 22284 | // lane-t900 r20: the `(side, write_w, write_h)` triples the inter block path |
| 22285 | // actually PRESENTS, as a thread-local bitmask -- the measured domain behind |
| 22286 | // the "rectangular ... has no ... here" refusals. `side` is the square context |
| 22287 | // a block reads its CDFs at and `write_w`/`write_h` its true footprint; |
| 22288 | // `inter_piece!` derives `reject_residual` from exactly that inequality, so |
| 22289 | // these triples are what decides whether those refusals can fire at all. Bit |
| 22290 | // 31 is "a triple this table does not list", which is what makes the gate a |
| 22291 | // census rather than a restatement of its own table. |
| 22292 | pub(crate) const INTER_BLOCK_SHAPES: [(usize, usize, usize); 18] = [ |
| 22293 | (16, 16, 16), |
| 22294 | (32, 32, 32), |
| 22295 | (32, 16, 32), |
| 22296 | (32, 32, 16), |
| 22297 | (16, 8, 16), |
| 22298 | (16, 16, 8), |
| 22299 | (32, 8, 32), |
| 22300 | (64, 64, 64), |
| 22301 | (32, 32, 8), |
| 22302 | (64, 16, 64), |
| 22303 | (128, 128, 128), |
| 22304 | (64, 32, 64), |
| 22305 | (64, 64, 32), |
| 22306 | (16, 4, 16), |
| 22307 | (64, 64, 16), |
| 22308 | (16, 16, 4), |
| 22309 | (128, 128, 64), |
| 22310 | (128, 64, 128), |
| 22311 | ]; |
| 22312 | |
| 22313 | /// Bit 31 of [`inter_block_shape_mask`]: a `(side, write_w, write_h)` triple |
| 22314 | /// outside [`INTER_BLOCK_SHAPES`]. |
| 22315 | pub const INTER_BLOCK_SHAPE_UNLISTED: u32 = 1 << 31; |
| 22316 | |
| 22317 | thread_local! { |
| 22318 | static INTER_BLOCK_SHAPE_MASK: std::cell::Cell<u32> = const { std::cell::Cell::new(0) }; |
| 22319 | } |
| 22320 | |
| 22321 | /// Which of [`INTER_BLOCK_SHAPES`] this THREAD has decoded (thread-local, so a |
| 22322 | /// gate reading it sees only the streams it decoded itself). |
| 22323 | pub fn inter_block_shape_mask() -> u32 { |
| 22324 | INTER_BLOCK_SHAPE_MASK.with(std::cell::Cell::get) |
| 22325 | } |
| 22326 | |
| 22327 | /// Records one block's shape. The table is ordered by measured frequency over |
| 22328 | /// the three witnesses the census gate decodes, so the common square shapes |
| 22329 | /// leave the scan on their first comparison. |
| 22330 | fn census_inter_block_shape(side: usize, w: usize, h: usize) { |
| 22331 | let bit = match INTER_BLOCK_SHAPES.iter().position(|&s| s == (side, w, h)) { |
| 22332 | Some(i) => 1u32 << i, |
| 22333 | None => INTER_BLOCK_SHAPE_UNLISTED, |
| 22334 | }; |
| 22335 | let seen = inter_block_shape_mask(); |
| 22336 | if seen & bit == 0 { |
| 22337 | INTER_BLOCK_SHAPE_MASK.with(|c| c.set(seen | bit)); |
| 22338 | } |
| 22339 | } |
| 22340 | |
| 22341 | /// The luma coefficient/`tx_type` table set of a whole-block rectangular |
| 22342 | /// INTER transform unit (lane-inter4 r3). `av1_get_ext_tx_set_type` splits |
| 22343 | /// the two shapes: a 32x16/16x32 unit has `tx_size_sqr_up == TX_32X32`, so |
| 22344 | /// it reads the 2-symbol `EXT_TX_SET_DCT_IDTX` set regardless of |
| 22345 | /// `reduced_tx_set`; a 16x8/8x16 unit has `tx_size_sqr_up == TX_16X16` and |
| 22346 | /// `tx_size_sqr == TX_8X8`, so it reads `EXT_TX_SET_DCT_IDTX` when reduced |
| 22347 | /// and the 16-symbol `EXT_TX_SET_ALL16` otherwise -- both at the *8x8* CDF |
| 22348 | /// row (`txsize_sqr_map`), never the 16x16 one. |
| 22349 | fn rect_inter_luma_set(w: usize, h: usize, fctx: &crate::decode::FrameCtx) -> Result<TxbSet> { |
| 22350 | Ok(match (w, h) { |
| 22351 | (32, 16) | (16, 32) => TxbSet::LumaRect32x16Inter, |
| 22352 | // lane-inter16ab r4: the 8x4/4x8 split leaf of a 16x4/4x16 strip. |
| 22353 | // `av1_get_ext_tx_set_type` (blockd.h:1097) at `tx_size_sqr_up == |
| 22354 | // TX_8X8`, `tx_size_sqr == TX_4X4`, inter: DCT_IDTX when reduced, |
| 22355 | // ALL16 otherwise -- both at the TX_4X4 row. |
| 22356 | (8, 4) | (4, 8) => { |
| 22357 | if fctx.reduced_tx_set_inter.with(std::cell::Cell::get) { |
| 22358 | TxbSet::LumaRect8x4Inter |
| 22359 | } else { |
| 22360 | TxbSet::LumaRect8x4InterSet1 |
| 22361 | } |
| 22362 | } |
| 22363 | (16, 4) | (4, 16) => { |
| 22364 | if fctx.reduced_tx_set_inter.with(std::cell::Cell::get) { |
| 22365 | TxbSet::LumaRect16x4Inter |
| 22366 | } else { |
| 22367 | TxbSet::LumaRect16x4Inter1 |
| 22368 | } |
| 22369 | } |
| 22370 | (16, 8) | (8, 16) => { |
| 22371 | if fctx.reduced_tx_set_inter.with(std::cell::Cell::get) { |
| 22372 | TxbSet::LumaRect16x8Inter |
| 22373 | } else { |
| 22374 | TxbSet::LumaRect16x8InterSet1 |
| 22375 | } |
| 22376 | } |
| 22377 | // lane-r14 r2: `av1_get_adjusted_tx_size` maps TX_64X32/TX_32X64 to |
| 22378 | // TX_32X32 and TX_64X16/TX_16X64 to TX_32X16/TX_16X32, and |
| 22379 | // `get_txsize_entropy_ctx`/`txsize_log2_minus4` agree with the |
| 22380 | // adjusted size, so the existing sets are bit-exact. Neither carries a |
| 22381 | // `tx_type` table (DCT_ONLY above), so the intra/inter split is moot. |
| 22382 | // lane-rectres r1: `tx_size_sqr_up[TX_32X8] == TX_32X32`, so |
| 22383 | // `av1_get_ext_tx_set_type` is `EXT_TX_SET_DCT_IDTX` for inter with or |
| 22384 | // without `reduced_tx_set` -- one set, no `REDUCED_TX_SET_INTER` |
| 22385 | // branch, read at the `txsize_sqr_map[TX_32X8] == TX_8X8` row. |
| 22386 | (32, 8) | (8, 32) => TxbSet::LumaRect32x8Inter, |
| 22387 | (64, 32) | (32, 64) => TxbSet::Luma64, |
| 22388 | (64, 16) | (16, 64) => TxbSet::LumaRect32x16, |
| 22389 | _ => { |
| 22390 | return Err(unsupported( |
| 22391 | "a rectangular inter luma transform unit whose shape has no coefficient table set here", |
| 22392 | )); |
| 22393 | } |
| 22394 | }) |
| 22395 | } |
| 22396 | |
| 22397 | /// The chroma table set of the same block's single rectangular chroma unit |
| 22398 | /// (`av1_get_max_uv_txsize`): 16x8/8x16 under a 32x16/16x32 strip, 8x4/4x8 |
| 22399 | /// under a 16x8/8x16 one. Neither codes a `tx_type` symbol -- both inherit |
| 22400 | /// the luma unit's. |
| 22401 | fn rect_inter_chroma_set(w: usize, h: usize) -> Result<TxbSet> { |
| 22402 | Ok(match (w, h) { |
| 22403 | (16, 8) | (8, 16) => TxbSet::ChromaRect16x8, |
| 22404 | (8, 4) | (4, 8) => TxbSet::ChromaRect8x4, |
| 22405 | // lane-r14 r2: chroma of a 64-axis strip -- 32x16/16x32 under a |
| 22406 | // 64x32/32x64 one, 32x8/8x32 under a 64x16/16x64 one. Neither chroma |
| 22407 | // unit has a 64-px axis, so both are coded in full; `Chroma16` is the |
| 22408 | // exact set for TX_32X8/TX_8X32 (`get_txsize_entropy_ctx` = TX_16X16, |
| 22409 | // `txsize_log2_minus4` = 4), same as the intra strip path uses. |
| 22410 | (32, 16) | (16, 32) => TxbSet::ChromaRect32x16, |
| 22411 | (32, 8) | (8, 32) => TxbSet::Chroma16, |
| 22412 | // lane-rectres r1: chroma of a 32x8/8x32 strip. `Chroma8` is the exact |
| 22413 | // set for TX_16X4/TX_4X16 (`get_txsize_entropy_ctx` = TX_8X8, |
| 22414 | // `txsize_log2_minus4` = 2, 64-coefficient `eob_pt`). |
| 22415 | (16, 4) | (4, 16) => TxbSet::Chroma8, |
| 22416 | // lane-av1-444: one 32x32 unit of a 4:4:4 64x32/32x64 chroma plane |
| 22417 | // block (`av1_get_max_uv_txsize` adjusts `TX_64X32`/`TX_32X64` to |
| 22418 | // `TX_32X32`); `get_txsize_entropy_ctx(TX_32X32)` is the 32x32 set. |
| 22419 | (32, 32) => TxbSet::Chroma32, |
| 22420 | // lane-av1-422j: square chroma units of a 4:2:2 strip -- a 16x8 |
| 22421 | // (8x16) luma strip's chroma plane block is 8x8 (16x16) square, and |
| 22422 | // the square-block routing above sends it here. The sets are the |
| 22423 | // exact `get_txsize_entropy_ctx` tables the square arms use. |
| 22424 | (16, 16) => TxbSet::Chroma16, |
| 22425 | (8, 8) => TxbSet::Chroma8, |
| 22426 | (4, 4) => TxbSet::Chroma4, |
| 22427 | _ => { |
| 22428 | return Err(unsupported( |
| 22429 | "a rectangular inter chroma transform unit whose shape has no coefficient table set here", |
| 22430 | )); |
| 22431 | } |
| 22432 | }) |
| 22433 | } |
| 22434 | |
| 22435 | |
| 22436 | /// libaom `size_group_lookup[bsize]` for the block shapes this decoder's |
| 22437 | /// inter path reaches, from the block's TRUE footprint rather than the |
| 22438 | /// square `side` corner-cut: 8x8/8x16/16x8 -> 1, 16x16/16x32/32x16 -> 2, |
| 22439 | /// everything 32 and above -> 3. |
| 22440 | fn size_group_wh(w: usize, h: usize) -> usize { |
| 22441 | // lane-inter4 r4: `size_group_lookup[BLOCK_SIZES_ALL]` (libaom |
| 22442 | // `common_data.h:60`) is 0 for every block with a 4-px side |
| 22443 | // (4x4/4x8/8x4/4x16/16x4), 1 for min side 8, 2 for 16, 3 above -- the old |
| 22444 | // `0..=8 => 1` row gave a sub-8 leaf group 1 where libaom says 0. |
| 22445 | match w.min(h) { |
| 22446 | 0..=4 => 0, |
| 22447 | 8 => 1, |
| 22448 | 16 => 2, |
| 22449 | _ => 3, |
| 22450 | } |
| 22451 | } |
| 22452 | |
| 22453 | #[test] |
| 22454 | fn size_group_wh_matches_libaom_size_group_lookup() { |
| 22455 | // `BLOCK_SIZES_ALL` in libaom's own order, with `size_group_lookup`'s |
| 22456 | // transcribed values (common_data.h:60-62). |
| 22457 | const TABLE: [((usize, usize), usize); 22] = [ |
| 22458 | ((4, 4), 0), ((4, 8), 0), ((8, 4), 0), ((8, 8), 1), ((8, 16), 1), ((16, 8), 1), |
| 22459 | ((16, 16), 2), ((16, 32), 2), ((32, 16), 2), ((32, 32), 3), ((32, 64), 3), |
| 22460 | ((64, 32), 3), ((64, 64), 3), ((64, 128), 3), ((128, 64), 3), ((128, 128), 3), |
| 22461 | ((4, 16), 0), ((16, 4), 0), ((8, 32), 1), ((32, 8), 1), ((16, 64), 2), ((64, 16), 2), |
| 22462 | ]; |
| 22463 | for ((w, h), want) in TABLE { |
| 22464 | assert_eq!(size_group_wh(w, h), want, "size_group_wh({w}, {h})"); |
| 22465 | } |
| 22466 | } |
| 22467 | |
| 22468 | /// The default (2D) coefficient scan of a rectangular transform. |
| 22469 | fn rect_scan(w: usize, h: usize) -> Result<&'static [u16]> { |
| 22470 | Ok(match (w, h) { |
| 22471 | (32, 16) => &SCAN_32X16, |
| 22472 | (16, 32) => &SCAN_16X32, |
| 22473 | (16, 8) => &SCAN_16X8, |
| 22474 | (8, 16) => &SCAN_8X16, |
| 22475 | (16, 4) => &SCAN_16X4, |
| 22476 | (4, 16) => &SCAN_4X16, |
| 22477 | (8, 4) => &SCAN_8X4, |
| 22478 | (4, 8) => &SCAN_4X8, |
| 22479 | (32, 8) => &SCAN_32X8, |
| 22480 | (8, 32) => &SCAN_8X32, |
| 22481 | // lane-av1-422j: square units routed through the rect reader -- a |
| 22482 | // 4:2:2 strip's square chroma plane block (8x8 under a 16x8 strip, |
| 22483 | // 16x16 under an 8x16 one). The default 2D scan is exactly what the |
| 22484 | // square reader uses for the same side. |
| 22485 | (w, h) if w == h => default_scan(w), |
| 22486 | _ => { |
| 22487 | return Err(unsupported( |
| 22488 | "a rectangular transform unit whose shape has no coefficient scan table here", |
| 22489 | )); |
| 22490 | } |
| 22491 | }) |
| 22492 | } |
| 22493 | |
| 22494 | /// lane-t900 r20, refusal D -- the three "a rectangular inter luma/chroma |
| 22495 | /// transform unit whose shape has no coefficient table set here" / "a |
| 22496 | /// rectangular transform unit whose shape has no coefficient scan table here" |
| 22497 | /// arms, enumerated against the MEASURED domain of the inter block path |
| 22498 | /// ([`INTER_BLOCK_SHAPES`], filled from three real streams by the gate |
| 22499 | /// `a_block_shape_census_over_three_real_streams_leaves_the_rect_residual_refusal_unreachable`). |
| 22500 | /// |
| 22501 | /// What each helper is actually handed: |
| 22502 | /// |
| 22503 | /// * [`rect_inter_luma_set`] -- the block's own footprint (call sites at the |
| 22504 | /// whole-block residual), and a var-tx LEAF shape. `sub_tx_size_map` is |
| 22505 | /// square for every rect transform except TX_16X4/TX_4X16, whose leaves are |
| 22506 | /// TX_8X4/TX_4X8, so the leaf domain adds exactly 8x4 and 4x8. |
| 22507 | /// * [`rect_inter_chroma_set`] -- `(write_w / 2, write_h / 2)` for a 4:2:0 |
| 22508 | /// block, except a 16x4/4x16 strip, whose chroma is the PAIR's 8x4 / 4x8. |
| 22509 | /// * [`rect_scan`] -- the `min(w, 32) x min(h, 32)` coded corner of a rect |
| 22510 | /// unit, and only when that corner is not 32x32 (a square corner goes |
| 22511 | /// through the square reader with `rect_shape = Some((w, h))`). |
| 22512 | /// |
| 22513 | /// The 128-px halves take neither: `max_txsize_rect_lookup[BLOCK_128X64]` is |
| 22514 | /// TX_64X64, so they tile into SQUARE 64x64 luma units through the mu-chunk |
| 22515 | /// loop, which is why they are skipped below. |
| 22516 | #[test] |
| 22517 | fn every_rect_transform_shape_the_census_lists_has_a_coefficient_table_and_scan() { |
| 22518 | let fctx = &crate::decode::FrameCtx::new(); |
| 22519 | let corner_scan = |w: usize, h: usize| { |
| 22520 | let (cw, ch) = (w.min(32), h.min(32)); |
| 22521 | if (cw, ch) == (32, 32) { |
| 22522 | return; // the square coefficient reader, not `rect_scan` |
| 22523 | } |
| 22524 | assert!( |
| 22525 | rect_scan(cw, ch).is_ok(), |
| 22526 | "a {w}x{h} rect transform codes its {cw}x{ch} corner, which has no scan table" |
| 22527 | ); |
| 22528 | }; |
| 22529 | let mut checked = 0; |
| 22530 | for &(side, w, h) in &INTER_BLOCK_SHAPES { |
| 22531 | if (w, h) == (side, side) || w.max(h) > 64 { |
| 22532 | continue; |
| 22533 | } |
| 22534 | assert!( |
| 22535 | rect_inter_luma_set(w, h, fctx).is_ok(), |
| 22536 | "the inter path decodes {w}x{h} blocks but has no luma coefficient table set for one" |
| 22537 | ); |
| 22538 | corner_scan(w, h); |
| 22539 | // 4:2:0 chroma: the pair's 8x4 / 4x8 for a 1:4 strip, the halved |
| 22540 | // footprint for every other shape. |
| 22541 | let (cw, ch) = match (w, h) { |
| 22542 | (16, 4) => (8, 4), |
| 22543 | (4, 16) => (4, 8), |
| 22544 | _ => (w / 2, h / 2), |
| 22545 | }; |
| 22546 | assert!( |
| 22547 | rect_inter_chroma_set(cw, ch).is_ok(), |
| 22548 | "a {w}x{h} block's {cw}x{ch} chroma unit has no coefficient table set" |
| 22549 | ); |
| 22550 | corner_scan(cw, ch); |
| 22551 | checked += 1; |
| 22552 | } |
| 22553 | assert_eq!( |
| 22554 | checked, 12, |
| 22555 | "the block-shape census changed: {checked} rect shapes reach the rect transform \ |
| 22556 | tables, not the 12 this claim was measured over" |
| 22557 | ); |
| 22558 | // `sub_tx_size_map[TX_16X4] == TX_8X4` -- the only RECTANGULAR var-tx leaf |
| 22559 | // shapes, which reach `rect_inter_luma_set` as `(tw, th)`. |
| 22560 | for (w, h) in [(8usize, 4usize), (4, 8)] { |
| 22561 | assert!( |
| 22562 | rect_inter_luma_set(w, h, fctx).is_ok(), |
| 22563 | "the {w}x{h} split leaf of a 16x4/4x16 strip has no luma coefficient table set" |
| 22564 | ); |
| 22565 | corner_scan(w, h); |
| 22566 | } |
| 22567 | } |
| 22568 | |
| 22569 | /// [`read_block_tx_size`] for a rectangular inter block (lane-inter4 r2): |
| 22570 | /// spec 5.11.17 / libaom `read_tx_size_vartx` starting from the block's own |
| 22571 | /// `max_txsize_rect_lookup` entry, which for a 2:1 strip IS rectangular. The |
| 22572 | /// depth-0 `txfm_partition` symbol reads its context off the rect transform's |
| 22573 | /// own width/height; a `0` leaves one whole-block rect transform unit |
| 22574 | /// (`None`), and a `1` splits it into `sub_tx_size_map`'s two SQUARE |
| 22575 | /// sub-transforms (`sub_tx_size_map[TX_32X16] == TX_16X16`), from which the |
| 22576 | /// ordinary square [`read_var_tx_size`] recursion continues at depth 1 -- so |
| 22577 | /// every leaf of a split rect block is square and the existing square leaf |
| 22578 | /// loop reconstructs it unchanged. |
| 22579 | #[allow(clippy::too_many_arguments)] |
| 22580 | fn read_block_tx_size_rect( |
| 22581 | dec: &mut SymbolDecoder, |
| 22582 | cdfs: &mut Cdfs, |
| 22583 | n: &mut Neighbours, |
| 22584 | at_mi: (usize, usize), |
| 22585 | (bw, bh): (usize, usize), |
| 22586 | (mi_cols, mi_rows): (usize, usize), |
| 22587 | skip: bool, fctx: &crate::decode::FrameCtx, |
| 22588 | ) -> Result<Option<Vec<(usize, usize, usize, usize)>>> { |
| 22589 | if lossless(fctx) { |
| 22590 | let mut leaves = Vec::new(); |
| 22591 | for row in 0..bh / MI { |
| 22592 | for col in 0..bw / MI { |
| 22593 | leaves.push((row, col, 4, 4)); |
| 22594 | } |
| 22595 | } |
| 22596 | return Ok(Some(leaves)); |
| 22597 | } |
| 22598 | if skip { |
| 22599 | // `set_txfm_ctxs(.., skip && is_inter)`: a skipped inter block records |
| 22600 | // its own BLOCK size -- width above, height left -- not a transform. |
| 22601 | txfm_partition_update_rect(n, at_mi, (bw, bh), (bw, bh)); |
| 22602 | return Ok(None); |
| 22603 | } |
| 22604 | // `max_txsize_rect_lookup` is the block's own size, capped at 64 -- and |
| 22605 | // `rect_inter_residual_supported` has already excluded everything above 32. |
| 22606 | // lane-sb128c r7: except at the 128 root. `max_txsize_rect_lookup |
| 22607 | // [BLOCK_128X64] == TX_64X64` (libaom `common_data.h`), so a 128x64 block |
| 22608 | // is TWO square top-level transform units side by side and a 64x128 one is |
| 22609 | // two stacked -- exactly what libaom `read_block_tx_size` |
| 22610 | // (decodeframe.c:1140) codes with its `idy/idx` loop over |
| 22611 | // `tx_size_high_unit/tx_size_wide_unit[max_tx_size]`. Each unit reads its |
| 22612 | // own `txfm_partition` symbol at depth 0. |
| 22613 | if bw.max(bh) > 64 { |
| 22614 | let tx = 64usize; |
| 22615 | let (max_w_mi, max_h_mi) = ( |
| 22616 | (bw / MI).min(mi_cols.saturating_sub(at_mi.1)), |
| 22617 | (bh / MI).min(mi_rows.saturating_sub(at_mi.0)), |
| 22618 | ); |
| 22619 | let mut leaves = Vec::new(); |
| 22620 | if !fctx.tx_select_frame.with(std::cell::Cell::get) { |
| 22621 | for row in (0..bh / MI).step_by(tx / MI) { |
| 22622 | for col in (0..bw / MI).step_by(tx / MI) { |
| 22623 | leaves.push((row, col, tx, tx)); |
| 22624 | } |
| 22625 | } |
| 22626 | for &(row, col, tw, th) in &leaves { |
| 22627 | txfm_partition_update_rect(n, (at_mi.0 + row, at_mi.1 + col), (tw, th), (tw, th)); |
| 22628 | } |
| 22629 | return Ok(Some(leaves)); |
| 22630 | } |
| 22631 | for row in (0..bh / MI).step_by(tx / MI) { |
| 22632 | for col in (0..bw / MI).step_by(tx / MI) { |
| 22633 | read_var_tx_size( |
| 22634 | dec, |
| 22635 | cdfs, |
| 22636 | n, |
| 22637 | at_mi, |
| 22638 | bw.max(bh), |
| 22639 | (max_w_mi, max_h_mi), |
| 22640 | (tx, tx), |
| 22641 | 0, |
| 22642 | row, |
| 22643 | col, |
| 22644 | &mut leaves, |
| 22645 | ); |
| 22646 | } |
| 22647 | } |
| 22648 | if leaves.iter().any(|l| l.2.max(l.3) > 64) { |
| 22649 | return Err(unsupported( |
| 22650 | "an inter var-tx tree with a leaf transform larger than 64x64", |
| 22651 | )); |
| 22652 | } |
| 22653 | return Ok(Some(leaves)); |
| 22654 | } |
| 22655 | let (tx_w, tx_h) = (bw, bh); |
| 22656 | if !fctx.tx_select_frame.with(std::cell::Cell::get) { |
| 22657 | // `TX_MODE_LARGEST`: no symbol at all, the whole block is one |
| 22658 | // transform, and `set_txfm_ctxs` records exactly what |
| 22659 | // `txfm_partition_update` would for that same unit. |
| 22660 | txfm_partition_update_rect(n, at_mi, (tx_w, tx_h), (bw, bh)); |
| 22661 | hit!(RECT_INTER_TU_HITS); |
| 22662 | return Ok(None); |
| 22663 | } |
| 22664 | let ctx = txfm_partition_ctx_rect( |
| 22665 | n.above_txfm[at_mi.1], |
| 22666 | n.left_txfm[at_mi.0], |
| 22667 | bw.max(bh), |
| 22668 | tx_w, |
| 22669 | tx_h, |
| 22670 | ); |
| 22671 | let split = dec.symbol(&mut cdfs.txfm_partition[ctx]) == 1; |
| 22672 | hit!(TXFM_SPLIT_READS); |
| 22673 | if crate::envflags::env_flag!("EC_TRACE_MODE_STEP") { |
| 22674 | let (rng, _) = dec.debug_state(); |
| 22675 | eprintln!( |
| 22676 | "EC_ISTEP mi_row={} mi_col={} name=txfm_split_rect val={} ctx={ctx} rng={rng}", |
| 22677 | at_mi.0, |
| 22678 | at_mi.1, |
| 22679 | u8::from(split) |
| 22680 | ); |
| 22681 | } |
| 22682 | if !split { |
| 22683 | txfm_partition_update_rect(n, at_mi, (tx_w, tx_h), (bw, bh)); |
| 22684 | hit!(RECT_INTER_TU_HITS); |
| 22685 | return Ok(None); |
| 22686 | } |
| 22687 | hit!(TXFM_SPLIT_HITS); |
| 22688 | hit!(RECT_INTER_TXSPLIT_HITS); |
| 22689 | // lane-r14 r3: `sub_tx_size_map` (libaom `common_data.c`) stays |
| 22690 | // RECTANGULAR under a 1:4 strip -- `[TX_64X16] == TX_32X16`, |
| 22691 | // `[TX_16X64] == TX_16X32` -- so the recursion below is the general |
| 22692 | // rect one and its leaves carry their own width and height. |
| 22693 | let (sub_w, sub_h) = sub_tx_size_map(tx_w, tx_h); |
| 22694 | let max_w_mi = (bw / MI).min(mi_cols.saturating_sub(at_mi.1)); |
| 22695 | let max_h_mi = (bh / MI).min(mi_rows.saturating_sub(at_mi.0)); |
| 22696 | let mut leaves = Vec::new(); |
| 22697 | for row in (0..bh / MI).step_by(sub_h / MI) { |
| 22698 | for col in (0..bw / MI).step_by(sub_w / MI) { |
| 22699 | read_var_tx_size( |
| 22700 | dec, |
| 22701 | cdfs, |
| 22702 | n, |
| 22703 | at_mi, |
| 22704 | bw.max(bh), |
| 22705 | (max_w_mi, max_h_mi), |
| 22706 | (sub_w, sub_h), |
| 22707 | 1, |
| 22708 | row, |
| 22709 | col, |
| 22710 | &mut leaves, |
| 22711 | ); |
| 22712 | } |
| 22713 | } |
| 22714 | Ok(Some(leaves)) |
| 22715 | } |
| 22716 | |
| 22717 | /// The chroma half of a whole-block rectangular INTER transform unit (both |
| 22718 | /// [`decode_inter_block`] rect readers): `av1_get_max_uv_txsize(bsize)` gives |
| 22719 | /// the strip's own rect chroma size at 4:2:0 (one unit for the whole block), |
| 22720 | /// but at 4:4:4 the chroma plane block keeps the luma block's 64-px axis, |
| 22721 | /// which `av1_get_adjusted_tx_size` caps at 32 (`TX_64X16` -> `TX_32X16`), |
| 22722 | /// so it codes TWO units plane-major (every U, then every V -- |
| 22723 | /// `decode_token_recon_block`'s inter branch loops `for plane` outside |
| 22724 | /// `for blk_row/blk_col` inside the one mu chunk). No unit codes a `tx_type` |
| 22725 | /// symbol: each inherits the block-wide luma one (`av1_get_tx_type`'s inter |
| 22726 | /// UV lookup at ss 0 reads the tx_type_map at its own mi, which a single-TU |
| 22727 | /// luma block fills uniformly). |
| 22728 | #[allow(clippy::too_many_arguments)] |
| 22729 | fn read_inter_rect_chroma( |
| 22730 | dec: &mut SymbolDecoder, |
| 22731 | cdfs: &mut Cdfs, |
| 22732 | neighbours: &mut Neighbours, |
| 22733 | around: [(bool, bool, i32); 3], |
| 22734 | u: &mut PlaneBuf<'static>, |
| 22735 | v: &mut PlaneBuf<'static>, |
| 22736 | su: Pred<'_>, |
| 22737 | sv: Pred<'_>, |
| 22738 | at_mi: (usize, usize), |
| 22739 | (cpx, cpy): (usize, usize), |
| 22740 | (write_chroma_w, write_chroma_h): (usize, usize), |
| 22741 | chroma_side: usize, |
| 22742 | mode_for_tx: usize, |
| 22743 | luma_tx_type: TxType, |
| 22744 | fctx: &crate::decode::FrameCtx, |
| 22745 | ) -> Result<(Grid, Grid, Vec<((usize, usize), usize, Grid, usize, usize)>)> { |
| 22746 | let (uw, uh) = (write_chroma_w.min(32), write_chroma_h.min(32)); |
| 22747 | let (nx, ny) = (write_chroma_w / uw, write_chroma_h / uh); |
| 22748 | let multi = nx * ny > 1; |
| 22749 | let set = rect_inter_chroma_set(uw, uh)?; |
| 22750 | let mut assembled: [Vec<i32>; 2] = [Vec::new(), Vec::new()]; |
| 22751 | // The multi-unit stamps below are rewritten by the caller's whole-block |
| 22752 | // record (`record_rect_mi`/`record_split_luma_rect_mi` write ONE |
| 22753 | // whole-block chroma state, class `override-slot-on-one-arm`). The |
| 22754 | // block tail's square-mu derivation cannot express a non-square strip's |
| 22755 | // units, so the TRUE units travel back for the tail's `mu_chroma_units` |
| 22756 | // replay (the c8 re-stamp, at the strip's own tiling). |
| 22757 | let mut units: Vec<((usize, usize), usize, Grid, usize, usize)> = Vec::new(); |
| 22758 | if multi { |
| 22759 | hit!(CHROMA_SPLIT_TX_HITS); |
| 22760 | assembled = [ |
| 22761 | vec![0i32; chroma_side * chroma_side], |
| 22762 | vec![0i32; chroma_side * chroma_side], |
| 22763 | ]; |
| 22764 | } |
| 22765 | for plane_idx in 1..=2 { |
| 22766 | let (src, buf) = if plane_idx == 1 { (su, &mut *u) } else { (sv, &mut *v) }; |
| 22767 | for cu_row in 0..ny { |
| 22768 | for cu_col in 0..nx { |
| 22769 | let (span_x, span_y) = (uw << ss_x(fctx), uh << ss_y(fctx)); |
| 22770 | let cu_mi = ( |
| 22771 | at_mi.0 + cu_row * (span_y / MI), |
| 22772 | at_mi.1 + cu_col * (span_x / MI), |
| 22773 | ); |
| 22774 | let cu_around = if multi { |
| 22775 | neighbours.around_mi_rect(cu_mi, span_x, span_y) |
| 22776 | } else { |
| 22777 | around |
| 22778 | }; |
| 22779 | let (cu_x, cu_y) = (cpx + cu_col * uw, cpy + cu_row * uh); |
| 22780 | let cu_pred = src.offset(cu_row * uh * chroma_side + cu_col * uw); |
| 22781 | let cu_grid = read_inter_plane_rect( |
| 22782 | dec, |
| 22783 | cdfs, |
| 22784 | set, |
| 22785 | (uw, uh), |
| 22786 | chroma_side, |
| 22787 | plane_idx, |
| 22788 | cu_around[plane_idx], |
| 22789 | mode_for_tx, |
| 22790 | buf, |
| 22791 | cu_x, |
| 22792 | cu_y, |
| 22793 | cu_pred, |
| 22794 | Some(luma_tx_type), |
| 22795 | multi.then_some(3), fctx, |
| 22796 | )? |
| 22797 | .0; |
| 22798 | if multi { |
| 22799 | // Immediately, so the next unit of this block reads this |
| 22800 | // one's coefficient context (libaom updates the chroma |
| 22801 | // above/left contexts per unit). |
| 22802 | neighbours.record_mi_chroma(cu_mi, span_x, span_y, plane_idx, &cu_grid); |
| 22803 | let dst = &mut assembled[plane_idx - 1]; |
| 22804 | for rr in 0..uh { |
| 22805 | let start = (cu_row * uh + rr) * chroma_side + cu_col * uw; |
| 22806 | dst[start..start + uw].copy_from_slice(&cu_grid[rr * uw..][..uw]); |
| 22807 | } |
| 22808 | units.push((cu_mi, plane_idx, cu_grid, span_x, span_y)); |
| 22809 | } else { |
| 22810 | assembled[plane_idx - 1] = embed_rect_grid(&cu_grid, uw, uh, chroma_side); |
| 22811 | } |
| 22812 | } |
| 22813 | } |
| 22814 | } |
| 22815 | let [ug, vg] = assembled; |
| 22816 | Ok((Grid::Own(ug), Grid::Own(vg), units)) |
| 22817 | } |
| 22818 | |
| 22819 | /// [`read_inter_plane`] for a rectangular transform unit (lane-inter4 r2): |
| 22820 | /// the coefficients are read with the rect coefficient machinery |
| 22821 | /// ([`read_coeffs_rect`], whose `TxClass` handling covers the 1D `V_*`/`H_*` |
| 22822 | /// types), inverted with the rect inverse transform |
| 22823 | /// ([`dequant_and_inverse_typed_wh`], which carries the 2:1 `sqrt2` scale), |
| 22824 | /// and added onto the top-left `w x h` of the block's own square-strided |
| 22825 | /// motion-compensated prediction buffer. Returns the *rect* coefficient grid |
| 22826 | /// and the coded `tx_type`. |
| 22827 | /// |
| 22828 | /// `txb_skip_ctx` is 0 for luma: this unit covers the whole block, i.e. |
| 22829 | /// `get_txb_ctx`'s `plane_bsize == txsize_to_bsize[tx_size]` branch. |
| 22830 | #[allow(clippy::too_many_arguments)] |
| 22831 | fn read_inter_plane_rect( |
| 22832 | dec: &mut SymbolDecoder, |
| 22833 | cdfs: &mut Cdfs, |
| 22834 | set: TxbSet, |
| 22835 | (w, h): (usize, usize), |
| 22836 | stride: usize, |
| 22837 | plane_idx: usize, |
| 22838 | around: (bool, bool, i32), |
| 22839 | tx_mode: usize, |
| 22840 | // lane-wave1 step (i): unread -- this reader records its write |
| 22841 | // ([`push_intra`]/[`push_mc`]) instead of performing it; the borrow |
| 22842 | // stays in the signature so every call site still proves the plane is |
| 22843 | // exclusively held while its coefficients are parsed. |
| 22844 | _plane: &mut PlaneBuf<'static>, |
| 22845 | x: usize, |
| 22846 | y: usize, |
| 22847 | prediction: Pred<'_>, |
| 22848 | inherited_luma_tx_type: Option<TxType>, |
| 22849 | luma_skip_ctx: Option<usize>, fctx: &crate::decode::FrameCtx, |
| 22850 | ) -> Result<(Grid, TxType)> { |
| 22851 | // lane-av1mono: as [`read_plane`] -- a monochrome frame codes no chroma |
| 22852 | // coefficients (`plane < av1_num_planes == 1`). |
| 22853 | if plane_idx > 0 && mono(fctx) { |
| 22854 | return Ok((Grid::Zero(w * h), TxType::DctDct)); |
| 22855 | } |
| 22856 | let skip_ctx = if plane_idx == 0 { |
| 22857 | // `get_txb_ctx_general`: 0 only when this unit IS the whole plane |
| 22858 | // block; a var-tx leaf passes its own neighbour-table context. |
| 22859 | luma_skip_ctx.unwrap_or(0) |
| 22860 | } else { |
| 22861 | // libaom `get_txb_ctx`: a chroma unit's context is |
| 22862 | // `combine_entropy_contexts(above, left)` plus 7, or plus 10 when the |
| 22863 | // plane block is bigger than the transform. Our chroma tables hold the |
| 22864 | // offset-7 rows first, so a multi-unit caller adds 3 through |
| 22865 | // `luma_skip_ctx` (same convention as [`read_plane`]). |
| 22866 | usize::from(around.0) + usize::from(around.1) + luma_skip_ctx.unwrap_or(0) |
| 22867 | }; |
| 22868 | let mut coding = cdfs.txb(set, tx_mode); |
| 22869 | // libaom `read_tx_type` (`av1/decoder/decodetxb.c`): "No need to read |
| 22870 | // transform type for lossless mode(qindex==0)" -- the symbol is not coded |
| 22871 | // at all, and `av1_get_tx_type` (`blockd.h`) returns DCT_DCT for every |
| 22872 | // plane of a lossless block (the residual then goes through the WHT, so |
| 22873 | // the type only picks the scan, which is the default one). |
| 22874 | if lossless(fctx) { |
| 22875 | coding.tx_type = None; |
| 22876 | } |
| 22877 | // `av1_get_ext_tx_set_type`'s DCT-only clamp is on `tx_size_sqr_up`, i.e. |
| 22878 | // the *longer* side of this rect transform. |
| 22879 | let default_tx_type = match inherited_luma_tx_type { |
| 22880 | Some(t) => reduce_inherited_chroma_tx_type(t, w, h, fctx), |
| 22881 | None => TxType::DctDct, |
| 22882 | }; |
| 22883 | // libaom `av1_get_tx_type` (`blockd.h`): a lossless block's type is |
| 22884 | // DCT_DCT on EVERY plane, so chroma never inherits or mode-derives one. |
| 22885 | let default_tx_type = if lossless(fctx) { TxType::DctDct } else { default_tx_type }; |
| 22886 | // lane-r14 r2: a 64-length axis codes only its low 32 coefficients |
| 22887 | // (`av1_get_max_eob`), so the coded unit is the `min(w,32) x min(h,32)` |
| 22888 | // corner, zero-extended into the true `w x h` grid before the inverse |
| 22889 | // transform -- exactly what the intra superblock-strip path does. A |
| 22890 | // corner that comes out square (32x32, under a 64x32/32x64 strip) reads |
| 22891 | // through the SQUARE coefficient reader with `rect_shape = Some((w, h))`, |
| 22892 | // because `av1_nz_map_ctx_offset` is indexed by the real, un-adjusted |
| 22893 | // rectangular shape (lane-sbpart r8). |
| 22894 | let (cw, ch) = (w.min(32), h.min(32)); |
| 22895 | if plane_idx == 0 && w.max(h) == 64 { |
| 22896 | hit!(RECT64_INTER_TU_HITS); |
| 22897 | } |
| 22898 | if plane_idx == 0 && (w, h) == (32, 8) || plane_idx == 0 && (w, h) == (8, 32) { |
| 22899 | hit_do! { |
| 22900 | RECT32X8_INTER_TU_HITS.with(|c| { |
| 22901 | let mut v = c.get(); |
| 22902 | v[usize::from(w == 8)] += 1; |
| 22903 | c.set(v); |
| 22904 | }); |
| 22905 | } |
| 22906 | } |
| 22907 | let (corner, tx_type) = if (cw, ch) == (32, 32) { |
| 22908 | let scan32 = default_scan(TX32); |
| 22909 | read_coeffs( |
| 22910 | dec, |
| 22911 | &mut coding, |
| 22912 | &scan32, |
| 22913 | skip_ctx, |
| 22914 | dc_sign_ctx(around.2), |
| 22915 | default_tx_type, |
| 22916 | Some((w, h)), |
| 22917 | )? |
| 22918 | } else { |
| 22919 | read_coeffs_rect( |
| 22920 | dec, |
| 22921 | &mut coding, |
| 22922 | rect_scan(cw, ch)?, |
| 22923 | cw, |
| 22924 | ch, |
| 22925 | skip_ctx, |
| 22926 | dc_sign_ctx(around.2), |
| 22927 | default_tx_type, |
| 22928 | )? |
| 22929 | }; |
| 22930 | let grid = extend_corner(corner, cw, ch, w, h); |
| 22931 | let (dc_delta, ac_delta) = plane_q_delta(plane_idx, fctx); |
| 22932 | if crate::envflags::env_flag!("EC_TRACE_TXTYPE") { |
| 22933 | eprintln!("OUR_TXTYPE plane={plane_idx} x={x} y={y} w={w} h={h} tx_type={tx_type:?} inh={inherited_luma_tx_type:?}"); |
| 22934 | } |
| 22935 | // `reconstruct_mc_rect` reads both buffers at the square prediction |
| 22936 | // stride, so the dense `w x h` residual is re-laid at that stride |
| 22937 | // (`TxParams::stride`) after the inverse transform. |
| 22938 | let tx = TxParams { |
| 22939 | w, h, bit_depth: bit_depth(fctx), q_idx: block_q_idx(fctx), dc_delta, ac_delta, tx_type, |
| 22940 | qm: block_iqmatrix(fctx, plane_idx, w, h, tx_type), |
| 22941 | stride, lossless: lossless(fctx), |
| 22942 | }; |
| 22943 | push_mc_rect_tx(plane_idx, x, y, stride, w, h, prediction, tx, &grid, fctx); |
| 22944 | Ok((grid, tx_type)) |
| 22945 | } |
| 22946 | |
| 22947 | /// The coded `cw x ch` corner of a transform zero-extended into its true |
| 22948 | /// `w x h` grid (a 64-length axis codes only its low 32 coefficients). A |
| 22949 | /// corner that coded nothing stays the shared zero grid -- no allocation |
| 22950 | /// (lane-coefgrid). |
| 22951 | fn extend_corner(corner: Grid, cw: usize, ch: usize, w: usize, h: usize) -> Grid { |
| 22952 | if (cw, ch) == (w, h) { |
| 22953 | return corner; |
| 22954 | } |
| 22955 | if let Grid::Zero(_) = corner { |
| 22956 | return Grid::Zero(w * h); |
| 22957 | } |
| 22958 | let mut full = vec![0i32; w * h]; |
| 22959 | for row in 0..ch { |
| 22960 | full[row * w..][..cw].copy_from_slice(&corner[row * cw..][..cw]); |
| 22961 | } |
| 22962 | Grid::Own(full) |
| 22963 | } |
| 22964 | |
| 22965 | /// [`embed_rect_grid`] that keeps the shared zero marker zero: an all-zero |
| 22966 | /// transform unit's grid IS `Grid::Zero` (whose deref is the shared |
| 22967 | /// `ZERO_RESIDUAL`), so re-laying it would read a zero-length slice. |
| 22968 | fn embed_grid_stride(grid: &Grid, w: usize, h: usize, stride: usize) -> Grid { |
| 22969 | if is_zero_grid(grid) { |
| 22970 | return Grid::Zero(stride * stride); |
| 22971 | } |
| 22972 | Grid::Own(embed_rect_grid(grid, w, h, stride)) |
| 22973 | } |
| 22974 | |
| 22975 | /// A rect coefficient grid re-laid into the square `stride x stride` grid the |
| 22976 | /// neighbour/deblock stamps downstream of the residual expect. |
| 22977 | fn embed_rect_grid(grid: &[i32], w: usize, h: usize, stride: usize) -> Vec<i32> { |
| 22978 | let mut full = vec![0i32; stride * stride]; |
| 22979 | for row in 0..h { |
| 22980 | full[row * stride..][..w].copy_from_slice(&grid[row * w..][..w]); |
| 22981 | } |
| 22982 | full |
| 22983 | } |
| 22984 | |
| 22985 | /// One 8x8 inter leaf's luma residual (lane-txselect): either the whole-8x8 |
| 22986 | /// transform, or -- when [`read_block_tx_size`]'s var-tx tree took the single |
| 22987 | /// split a `BLOCK_8X8` can take (`split`) -- the four 4x4 transform units in |
| 22988 | /// raster order, each reading its own coefficient context off the units |
| 22989 | /// already decoded before it. Returns the block's luma grid (all-zero in the |
| 22990 | /// split case, whose reconstruction has already happened unit by unit) and |
| 22991 | /// the `tx_type` chroma inherits: `av1_get_tx_type`'s colocated luma lookup, |
| 22992 | /// which for a split block lands on the top-left unit. |
| 22993 | #[allow(clippy::too_many_arguments)] |
| 22994 | fn read_inter_luma8( |
| 22995 | dec: &mut SymbolDecoder, |
| 22996 | cdfs: &mut Cdfs, |
| 22997 | neighbours: &mut Neighbours, |
| 22998 | leaf_mi: (usize, usize), |
| 22999 | y: &mut PlaneBuf<'static>, |
| 23000 | px: usize, |
| 23001 | py: usize, |
| 23002 | pred_y: Pred<'_>, |
| 23003 | mode_for_tx: usize, |
| 23004 | base_q_idx: u8, |
| 23005 | scan8: &[u16], |
| 23006 | scan4: &[u16], |
| 23007 | reduced_tx_set: bool, |
| 23008 | split: bool, fctx: &crate::decode::FrameCtx, |
| 23009 | ) -> Result<(Grid, TxType)> { |
| 23010 | // lane-lossless: an 8x8 inter block is FOUR 4x4 units (libaom |
| 23011 | // `read_block_tx_size`'s lossless TX_4X4), tree or no tree. |
| 23012 | let split = split || lossless(fctx); |
| 23013 | if !split { |
| 23014 | return read_inter_plane( |
| 23015 | dec, |
| 23016 | cdfs, |
| 23017 | if reduced_tx_set { |
| 23018 | TxbSet::Luma8Inter |
| 23019 | } else { |
| 23020 | TxbSet::Luma8InterSet1 |
| 23021 | }, |
| 23022 | scan8, |
| 23023 | 0, |
| 23024 | neighbours.around_mi(leaf_mi, 8)[0], |
| 23025 | mode_for_tx, |
| 23026 | y, |
| 23027 | px, |
| 23028 | py, |
| 23029 | 8, |
| 23030 | base_q_idx, |
| 23031 | pred_y, |
| 23032 | None, |
| 23033 | None, fctx, |
| 23034 | ); |
| 23035 | } |
| 23036 | let mut first = TxType::DctDct; |
| 23037 | for tu_row in 0..2 { |
| 23038 | for tu_col in 0..2 { |
| 23039 | let tu_mi = (leaf_mi.0 + tu_row, leaf_mi.1 + tu_col); |
| 23040 | let tu_pred = pred_y.view(8).offset(tu_row * 4 * 8 + tu_col * 4); |
| 23041 | let tu_skip_ctx = neighbours.luma_skip_ctx(tu_mi, 1); |
| 23042 | let (tu_grid, tu_tx_type) = read_inter_plane( |
| 23043 | dec, |
| 23044 | cdfs, |
| 23045 | if reduced_tx_set { |
| 23046 | TxbSet::Luma4Inter |
| 23047 | } else { |
| 23048 | TxbSet::Luma4InterSet1 |
| 23049 | }, |
| 23050 | scan4, |
| 23051 | 0, |
| 23052 | neighbours.around_mi(tu_mi, 4)[0], |
| 23053 | mode_for_tx, |
| 23054 | y, |
| 23055 | px + tu_col * 4, |
| 23056 | py + tu_row * 4, |
| 23057 | 4, |
| 23058 | base_q_idx, |
| 23059 | tu_pred, |
| 23060 | None, |
| 23061 | Some(tu_skip_ctx), fctx, |
| 23062 | )?; |
| 23063 | if tu_row == 0 && tu_col == 0 { |
| 23064 | first = tu_tx_type; |
| 23065 | } |
| 23066 | neighbours.record_mi_luma(tu_mi, 4, &tu_grid); |
| 23067 | } |
| 23068 | } |
| 23069 | Ok((Grid::Zero(64), first)) |
| 23070 | } |
| 23071 | |
| 23072 | /// The [`TxbSet`] one leaf of an inter block's var-tx tree reads its |
| 23073 | /// coefficient tables from -- the inter counterpart of [`txbset_for`]. |
| 23074 | /// `av1_get_tx_type`'s chroma tail (libaom `av1/common/blockd.h:1291-1305`): |
| 23075 | /// an inter block's chroma transform inherits the *colocated luma* unit's |
| 23076 | /// coded `tx_type`, then drops back to `DCT_DCT` unless that type is a member |
| 23077 | /// of the extended set the CHROMA transform size selects |
| 23078 | /// (`av1_ext_tx_used[av1_get_ext_tx_set_type(uv_tx, 1, reduced)][tx_type]`, |
| 23079 | /// `blockd.h:1036`/`1097`). lane-golomb r7: we used to force `DCT_DCT` at |
| 23080 | /// every chroma side >= 32, but `av1_get_ext_tx_set_type`'s DCT-only rung is |
| 23081 | /// `tx_size_sqr_up > T |
Showing the first 1 MiB of this file. View raw.