edith_codecs
Clone
https://git.tahinli.com/Tahinli/edith_codecs.git
0 watching0 forksRust370.3 MiBfef26dfTahinli2 months ago3246 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 | // `is_cfl_allowed`: at lossless CfL is available only when the |
| 490 | // partition size equals the transform size -- the CHROMA plane block |
| 491 | // must be a single TX_4X4. True for an 8x8 partition at 4:2:0 (its |
| 492 | // plane block is 4x4) and for 4x4 partitions; FALSE for an 8x8 |
| 493 | // partition at 4:4:4, whose plane block is 8x8 -- that partition |
| 494 | // reads the 13-symbol uv_mode CDF, and reading the CFL alphabet |
| 495 | // there desynced the tile from the leaf's own uv_mode symbol (class |
| 496 | // `wrong-alphabet-same-value`). |
| 497 | (bw >> ss_x(fctx)).max(4) == 4 && (bh >> ss_y(fctx)).max(4) == 4 |
| 498 | } else { |
| 499 | bw <= 32 && bh <= 32 |
| 500 | } |
| 501 | } |
| 502 | |
| 503 | fn sb128(fctx: &crate::decode::FrameCtx) -> bool { |
| 504 | fctx.sb128_flag.with(|c| c.get()) |
| 505 | } |
| 506 | |
| 507 | /// This sequence's superblock size in mi units (16 = 64x64, 32 = 128x128). |
| 508 | pub(crate) fn sb_mi_cur(fctx: &crate::decode::FrameCtx) -> u32 { |
| 509 | if sb128(fctx) { SB_MI * 2 } else { SB_MI } |
| 510 | } |
| 511 | |
| 512 | // How many real 128-level partition symbols this decoder has read, and how |
| 513 | // many superblocks resolved to `PARTITION_SPLIT` (coded or forced) -- the |
| 514 | // gate's proof a `--sb-size=128` stream exercised the 128 partition root. |
| 515 | thread_local! { |
| 516 | static PART128_SYMBOLS: std::cell::Cell<usize> = const { std::cell::Cell::new(0) }; |
| 517 | static PART128_SPLIT_HITS: std::cell::Cell<usize> = const { std::cell::Cell::new(0) }; |
| 518 | static PART128_GATHERED_SYMBOLS: std::cell::Cell<usize> = const { std::cell::Cell::new(0) }; |
| 519 | } |
| 520 | |
| 521 | /// Current value of `PART128_GATHERED_SYMBOLS`: 128 roots that read the |
| 522 | /// half-out-of-frame gathered split_or_horz/split_or_vert symbol (spec |
| 523 | /// 5.11.4), which this decoder used to read and throw away. |
| 524 | #[allow(dead_code)] // read only from the `#[cfg(test)]` gates |
| 525 | pub(crate) fn part128_gathered_symbols() -> usize { |
| 526 | PART128_GATHERED_SYMBOLS.with(|c| c.get()) |
| 527 | } |
| 528 | |
| 529 | /// Current value of [`PART128_SYMBOLS`]. |
| 530 | #[allow(dead_code)] // read only from the `#[cfg(test)]` gates |
| 531 | pub(crate) fn part128_symbols() -> usize { |
| 532 | PART128_SYMBOLS.with(|c| c.get()) |
| 533 | } |
| 534 | |
| 535 | /// Current value of [`PART128_SPLIT_HITS`]. |
| 536 | #[allow(dead_code)] // read only from the `#[cfg(test)]` gates |
| 537 | pub(crate) fn part128_split_hits() -> usize { |
| 538 | PART128_SPLIT_HITS.with(|c| c.get()) |
| 539 | } |
| 540 | |
| 541 | /// [`gather`] over an alphabet of any length -- the 128-level partition CDF |
| 542 | /// has 8 symbols (no `PARTITION_HORZ_4`/`_VERT_4` at `BLOCK_128X128`, spec |
| 543 | /// `partition_cdf_length`), so its forced-split gather sums 5 elements where |
| 544 | /// the 64/32 levels sum 6. |
| 545 | pub(crate) fn gather_of(cdf: &[u16], elements: &[usize]) -> [u16; 3] { |
| 546 | let split: u16 = elements.iter().map(|&e| element_prob(cdf, e)).sum(); |
| 547 | [32768 - split, 32768, 0] |
| 548 | } |
| 549 | |
| 550 | /// [`VERT_ALIKE`]/[`HORZ_ALIKE`] without the 1:4 arms, for the 128 root. |
| 551 | pub(crate) const VERT_ALIKE128: [usize; 5] = [2, PARTITION_SPLIT, 4, 6, 7]; |
| 552 | pub(crate) const HORZ_ALIKE128: [usize; 5] = [1, PARTITION_SPLIT, 4, 5, 6]; |
| 553 | |
| 554 | // How many `use_filter_intra` symbols this decoder has read as `1`, across |
| 555 | // every call on the current thread -- the cheap counter [`filter_intra_hits`] gate |
| 556 | // tests read (before/after, not the absolute value) to prove a stream |
| 557 | // actually exercised the filter-intra predictor rather than silently |
| 558 | // skipping it. |
| 559 | thread_local! { |
| 560 | static FILTER_INTRA_HITS: std::cell::Cell<usize> = |
| 561 | const { std::cell::Cell::new(0) }; |
| 562 | } |
| 563 | |
| 564 | /// Current value of [`FILTER_INTRA_HITS`]. |
| 565 | #[allow(dead_code)] // read only from the `#[cfg(test)]` gates |
| 566 | pub(crate) fn filter_intra_hits() -> usize { |
| 567 | FILTER_INTRA_HITS.with(|c| c.get()) |
| 568 | } |
| 569 | |
| 570 | // lane-fiinter r1: how many of those `use_filter_intra == 1` symbols belonged |
| 571 | // to an INTRA block inside an INTER frame (`read_intra_block_mode_info`'s own |
| 572 | // `read_filter_intra_mode_info` call), which no gate exercised before this |
| 573 | // round -- every inter gate recipe passed `--enable-filter-intra=0`. |
| 574 | thread_local! { |
| 575 | static INTRA_IN_INTER_FILTER_INTRA_HITS: std::cell::Cell<usize> = |
| 576 | const { std::cell::Cell::new(0) }; |
| 577 | } |
| 578 | |
| 579 | /// Current value of [`INTRA_IN_INTER_FILTER_INTRA_HITS`]. |
| 580 | #[allow(dead_code)] // read only from the `#[cfg(test)]` gates |
| 581 | pub(crate) fn intra_in_inter_filter_intra_hits() -> usize { |
| 582 | INTRA_IN_INTER_FILTER_INTRA_HITS.with(|c| c.get()) |
| 583 | } |
| 584 | |
| 585 | // lane-angleinter r1: how many INTRA blocks inside an INTER frame carried a |
| 586 | // nonzero `angle_delta_y` / `angle_delta_uv` (`read_intra_angle_info`). Both |
| 587 | // arms refused a nonzero luma delta before this round, and no inter gate |
| 588 | // enabled `--enable-angle-delta` (aomenc's own default is 1). |
| 589 | thread_local! { |
| 590 | static INTRA_IN_INTER_ANGLE_DELTA_Y_HITS: std::cell::Cell<usize> = |
| 591 | const { std::cell::Cell::new(0) }; |
| 592 | static INTRA_IN_INTER_ANGLE_DELTA_UV_HITS: std::cell::Cell<usize> = |
| 593 | const { std::cell::Cell::new(0) }; |
| 594 | } |
| 595 | |
| 596 | /// Current value of [`INTRA_IN_INTER_ANGLE_DELTA_Y_HITS`]. |
| 597 | #[allow(dead_code)] // read only from the `#[cfg(test)]` gates |
| 598 | pub(crate) fn intra_in_inter_angle_delta_y_hits() -> usize { |
| 599 | INTRA_IN_INTER_ANGLE_DELTA_Y_HITS.with(|c| c.get()) |
| 600 | } |
| 601 | |
| 602 | /// Current value of [`INTRA_IN_INTER_ANGLE_DELTA_UV_HITS`]. |
| 603 | #[allow(dead_code)] // read only from the `#[cfg(test)]` gates |
| 604 | pub(crate) fn intra_in_inter_angle_delta_uv_hits() -> usize { |
| 605 | INTRA_IN_INTER_ANGLE_DELTA_UV_HITS.with(|c| c.get()) |
| 606 | } |
| 607 | |
| 608 | // lane-uv8 r1: the three non-DC chroma mode classes an INTRA 8x8 leaf inside |
| 609 | // an INTER frame ([`decode_inter_block8`]'s intra arm) can carry -- the arm |
| 610 | // read `uv_mode` and then refused everything but `DC_PRED`. Counted here and |
| 611 | // nowhere else so the gate's assert cannot be satisfied by a >=16x16 block. |
| 612 | thread_local! { |
| 613 | static INTRA_IN_INTER8_UV_DIR_HITS: std::cell::Cell<usize> = const { std::cell::Cell::new(0) }; |
| 614 | static INTRA_IN_INTER8_UV_SMOOTH_HITS: std::cell::Cell<usize> = const { std::cell::Cell::new(0) }; |
| 615 | static INTRA_IN_INTER8_UV_CFL_HITS: std::cell::Cell<usize> = const { std::cell::Cell::new(0) }; |
| 616 | } |
| 617 | |
| 618 | thread_local! { |
| 619 | static INTRA_IN_INTER8_ANGLE_DELTA_Y_HITS: std::cell::Cell<usize> = const { std::cell::Cell::new(0) }; |
| 620 | } |
| 621 | |
| 622 | /// Nonzero luma angle deltas coded by an intra 8x8 leaf inside an inter frame |
| 623 | /// (lane-leaf8tx r1) -- counted only in [`decode_inter_block8`]'s intra arm. |
| 624 | #[allow(dead_code)] // read only from the `#[cfg(test)]` gates |
| 625 | pub(crate) fn intra_in_inter8_angle_delta_y_hits() -> usize { |
| 626 | INTRA_IN_INTER8_ANGLE_DELTA_Y_HITS.with(|c| c.get()) |
| 627 | } |
| 628 | |
| 629 | /// `(directional, smooth/paeth, CfL)` chroma modes coded by an intra 8x8 leaf |
| 630 | /// inside an inter frame -- see [`INTRA_IN_INTER8_UV_DIR_HITS`]. |
| 631 | #[allow(dead_code)] // read only from the `#[cfg(test)]` gates |
| 632 | pub(crate) fn intra_in_inter8_uv_hits() -> (usize, usize, usize) { |
| 633 | ( |
| 634 | INTRA_IN_INTER8_UV_DIR_HITS.with(|c| c.get()), |
| 635 | INTRA_IN_INTER8_UV_SMOOTH_HITS.with(|c| c.get()), |
| 636 | INTRA_IN_INTER8_UV_CFL_HITS.with(|c| c.get()), |
| 637 | ) |
| 638 | } |
| 639 | |
| 640 | // lane-realworld r1: this frame's `cdef.bits` (spec 5.9.19), threaded into |
| 641 | // [`maybe_read_cdef_idx`] the same way [`ENABLE_EDGE_FILTER`] threads a |
| 642 | // frame-level flag through the recursive block decode -- `0` (the default) |
| 643 | // makes every call a true no-op, matching every existing gate stream's |
| 644 | // `cdef_bits == 0` behaviour exactly. |
| 645 | // lane-realworld r8: the sequence header's `color_config.bit_depth`, set |
| 646 | // once per `decode_key_frame_tile_with_cdfs`/`decode_inter_frame_tile_with_cdfs` |
| 647 | // call the same way `CDEF_BITS` is above. `stream.rs`'s own refusal still |
| 648 | // blocks every non-8-bit stream from reaching a decode call, so this is `8` |
| 649 | // on every path that runs today -- threading it now (rather than the literal |
| 650 | // `8`) is stage 1 of widening past that refusal without re-deriving every |
| 651 | // clamp bound later. `PlaneBuf`'s sample storage stays `u16` regardless of |
| 652 | // depth (this decoder's own internal choice, matches dav1d/libaom). |
| 653 | /// `(1 << bit_depth) - 1` — the reconstruction clamp bound for the current |
| 654 | /// stream's [`BIT_DEPTH`], replacing the crate-wide literal `255`. |
| 655 | pub(crate) fn sample_max(fctx: &crate::decode::FrameCtx) -> i32 { |
| 656 | (1i32 << fctx.bit_depth.with(std::cell::Cell::get)) - 1 |
| 657 | } |
| 658 | /// The current stream's bit depth, for callers (e.g. dequant) that need the |
| 659 | /// raw value rather than the derived clamp bound. |
| 660 | pub(crate) fn bit_depth(fctx: &crate::decode::FrameCtx) -> u8 { |
| 661 | fctx.bit_depth.with(std::cell::Cell::get) |
| 662 | } |
| 663 | /// Sets [`BIT_DEPTH`] for the current thread, called once per frame from |
| 664 | /// [`crate::stream`]'s decode loop. |
| 665 | pub(crate) fn set_bit_depth(bit_depth: u8, fctx: &crate::decode::FrameCtx) { |
| 666 | fctx.bit_depth.with(|c| c.set(bit_depth)); |
| 667 | } |
| 668 | |
| 669 | /// This stream's `mono_chrome` (see [`FrameCtx::mono_chrome`]): a |
| 670 | /// monochrome frame has `NumPlanes == 1`, so no block codes any chroma |
| 671 | /// symbol or chroma coefficient and the output picture is luma only. |
| 672 | pub(crate) fn mono(fctx: &crate::decode::FrameCtx) -> bool { |
| 673 | fctx.mono_chrome.with(std::cell::Cell::get) |
| 674 | } |
| 675 | |
| 676 | /// Sets [`mono`] for the current thread, called once per frame from |
| 677 | /// [`crate::stream`]'s decode loop like [`set_bit_depth`]. |
| 678 | pub(crate) fn set_mono(mono_chrome: bool, fctx: &crate::decode::FrameCtx) { |
| 679 | fctx.mono_chrome.with(|c| c.set(mono_chrome)); |
| 680 | } |
| 681 | |
| 682 | /// Sequence subsampling. Set once per frame from stream.rs, like set_mono. |
| 683 | pub(crate) fn set_subsampling(ss_x: u8, ss_y: u8, fctx: &crate::decode::FrameCtx) { |
| 684 | fctx.subsampling_x.set(ss_x); |
| 685 | fctx.subsampling_y.set(ss_y); |
| 686 | } |
| 687 | |
| 688 | #[inline] |
| 689 | pub(crate) fn ss_x(fctx: &crate::decode::FrameCtx) -> usize { |
| 690 | fctx.subsampling_x.get() as usize |
| 691 | } |
| 692 | |
| 693 | #[inline] |
| 694 | pub(crate) fn ss_y(fctx: &crate::decode::FrameCtx) -> usize { |
| 695 | fctx.subsampling_y.get() as usize |
| 696 | } |
| 697 | |
| 698 | /// libaom ROUND_POWER_OF_TWO. At ss=1 equals div_ceil(dim, 2); at ss=0 is dim. |
| 699 | #[inline] |
| 700 | pub(crate) fn round_ss(dim: usize, ss: usize) -> usize { |
| 701 | (dim + ((1usize << ss) >> 1)) >> ss |
| 702 | } |
| 703 | |
| 704 | |
| 705 | /// This frame's superres state: `(upscaled_width, superres_denom)`, or |
| 706 | /// `(0, 8)` when the frame is not scaled. Spec 7.16's upscale runs |
| 707 | /// between CDEF and loop restoration, so LOOP RESTORATION -- unit grid, |
| 708 | /// `read_lr`'s unit corners and the pixel filters themselves -- works in |
| 709 | /// UPSCALED coordinates while deblock and CDEF work in downscaled ones |
| 710 | /// (libaom `av1_superres_upscale` in `decodeframe.c`, and |
| 711 | /// `av1_loop_restoration_corners_in_sb`'s own `mi_to_num_x`/`denom_x` |
| 712 | /// superres arms). Set once per frame from [`crate::stream`]'s decode |
| 713 | /// loop, like [`BIT_DEPTH`], and reset to `(0, 8)` on an unscaled frame |
| 714 | /// so a later frame never inherits it. |
| 715 | |
| 716 | /// Sets [`SUPERRES`] for the current thread. `upscaled_width` is 0 (and |
| 717 | /// `denom` 8) for a frame that is not superres-scaled. |
| 718 | pub(crate) fn set_superres(upscaled_width: u32, denom: u32, fctx: &crate::decode::FrameCtx) { |
| 719 | fctx.superres.with(|c| c.set((upscaled_width, denom))); |
| 720 | } |
| 721 | |
| 722 | /// `(upscaled_width, superres_denom)` when this frame is superres-scaled |
| 723 | /// (libaom `av1_superres_scaled`), else `None`. |
| 724 | pub(crate) fn superres(fctx: &crate::decode::FrameCtx) -> Option<(u32, u32)> { |
| 725 | let (w, d) = fctx.superres.with(std::cell::Cell::get); |
| 726 | (w > 0 && d != 8).then_some((w, d)) |
| 727 | } |
| 728 | // Whether this superblock's single CDEF unit (this decoder has no 128x128 |
| 729 | // SB, so the SB IS the CDEF unit) has already had its `cdef_idx` literal |
| 730 | // read -- reset to `false` at the top of each superblock in the two tile |
| 731 | // loops, spec `read_cdef`'s `cdef_transmitted[index]`. |
| 732 | // Number of superblock columns in the current tile's grid, so |
| 733 | // [`maybe_read_cdef_idx`] can turn an `(mi_r, mi_c)` into a flat index into |
| 734 | // [`CDEF_IDX_GRID`]. |
| 735 | // One resolved `cdef_idx` per superblock, read by [`apply_cdef`] to select |
| 736 | // which of the header's `1 << cdef.bits` strength pairs applies to that |
| 737 | // superblock's samples. |
| 738 | // How many real (`cdef.bits > 0`) `cdef_idx` literals [`maybe_read_cdef_idx`] |
| 739 | // has read, across every call on the current thread -- the gate's proof that a |
| 740 | // `cdef_bits > 0` stream actually reached the new reader rather than every |
| 741 | // superblock coincidentally being all-skip. |
| 742 | thread_local! { |
| 743 | static CDEF_IDX_HITS: std::cell::Cell<usize> = const { std::cell::Cell::new(0) }; |
| 744 | } |
| 745 | |
| 746 | // How many times a warp was suppressed for one PLANE because that plane's own |
| 747 | // block is narrower or shorter than 8 (`av1_init_warp_params`'s own bail-out, |
| 748 | // reconinter.c) -- the gate's proof that a stream really carries the shape, |
| 749 | // since the square `side`/`chroma_side` can be 8 or more while the plane's |
| 750 | // true block is 4 wide (an 8x16 luma block's 4x8 chroma). |
| 751 | thread_local! { |
| 752 | static WARP_PLANE_SUPPRESS_HITS: std::cell::Cell<usize> = const { std::cell::Cell::new(0) }; |
| 753 | } |
| 754 | |
| 755 | /// Current value of [`WARP_PLANE_SUPPRESS_HITS`]. |
| 756 | #[allow(dead_code)] // read only from the `#[cfg(test)]` gates |
| 757 | pub(crate) fn warp_plane_suppress_hits() -> usize { |
| 758 | WARP_PLANE_SUPPRESS_HITS.with(|c| c.get()) |
| 759 | } |
| 760 | |
| 761 | /// `av1_init_warp_params` (libaom reconinter.c): warp is decided per PLANE and |
| 762 | /// bails out at `block_width < 8 || block_height < 8`, so the plane's OWN |
| 763 | /// block dimensions decide it -- never the square `side`/`chroma_side`, which |
| 764 | /// for a rectangular luma block (8x16, 16x8) or a PARTITION_HORZ/VERT strip is |
| 765 | /// bigger than the real block in one axis. |
| 766 | pub(crate) fn warp_plane_allowed(w: usize, h: usize) -> bool { |
| 767 | if w >= 8 && h >= 8 { |
| 768 | return true; |
| 769 | } |
| 770 | hit!(WARP_PLANE_SUPPRESS_HITS); |
| 771 | false |
| 772 | } |
| 773 | |
| 774 | /// Current value of [`CDEF_IDX_HITS`]. |
| 775 | #[allow(dead_code)] // read only from the `#[cfg(test)]` gates |
| 776 | pub(crate) fn cdef_idx_hits() -> usize { |
| 777 | CDEF_IDX_HITS.with(|c| c.get()) |
| 778 | } |
| 779 | |
| 780 | // lane-t900 r6: gate proof for the three r2-r4 root causes that shipped with no |
| 781 | // counter -- the in-inter tx-depth context (`tx_size_context_txfm_rect`), the |
| 782 | // non-chroma-reference block that must LEAVE its neighbours' chroma coefficient |
| 783 | // contexts alone, and the intra-edge filter neighbour pair taken mi-exact |
| 784 | // instead of from the coarse 16x16 cell. |
| 785 | thread_local! { |
| 786 | static INTRA_IN_INTER_TXCTX_HITS: std::cell::Cell<usize> = const { std::cell::Cell::new(0) }; |
| 787 | static NON_CHROMA_REF_CTX_SKIP_HITS: std::cell::Cell<usize> = const { std::cell::Cell::new(0) }; |
| 788 | static EDGE_FILTER_MI_FIX_HITS: std::cell::Cell<usize> = const { std::cell::Cell::new(0) }; |
| 789 | } |
| 790 | |
| 791 | /// Current value of [`INTRA_IN_INTER_TXCTX_HITS`]. |
| 792 | #[allow(dead_code)] // read only from the `#[cfg(test)]` gates |
| 793 | pub(crate) fn intra_in_inter_txctx_hits() -> usize { |
| 794 | INTRA_IN_INTER_TXCTX_HITS.with(|c| c.get()) |
| 795 | } |
| 796 | |
| 797 | /// Current value of [`NON_CHROMA_REF_CTX_SKIP_HITS`]. |
| 798 | #[allow(dead_code)] // read only from the `#[cfg(test)]` gates |
| 799 | pub(crate) fn non_chroma_ref_ctx_skip_hits() -> usize { |
| 800 | NON_CHROMA_REF_CTX_SKIP_HITS.with(|c| c.get()) |
| 801 | } |
| 802 | |
| 803 | /// Current value of [`EDGE_FILTER_MI_FIX_HITS`]. |
| 804 | #[allow(dead_code)] // read only from the `#[cfg(test)]` gates |
| 805 | pub(crate) fn edge_filter_mi_fix_hits() -> usize { |
| 806 | EDGE_FILTER_MI_FIX_HITS.with(|c| c.get()) |
| 807 | } |
| 808 | |
| 809 | // lane-realworld r4: this frame's `delta.q_present` (spec 5.9.17) and its |
| 810 | // actual step (`1 << delta.q_res`, already the real multiplier, not the raw |
| 811 | // 2-bit field). `false`/`1` (the defaults) make [`maybe_read_delta_q`] a |
| 812 | // true no-op, matching every existing gate stream's `delta_q_present == 0` |
| 813 | // behaviour exactly, the same corner-cut [`CDEF_BITS`] takes above. |
| 814 | // The running quantizer index (spec `CurrentQIndex`, libaom |
| 815 | // `xd->current_base_qindex`) -- reset to the frame's own `base_q_idx` at the |
| 816 | // top of every tile (spec `decode_tile`), carried across superblocks within |
| 817 | // that tile, and mutated only by [`maybe_read_delta_q`]. Read back at the |
| 818 | // dequantization call sites instead of the frame-constant `base_q_idx`. |
| 819 | // This frame's own per-plane DC/AC quantizer-index deltas (lane-sbpart r11, |
| 820 | // spec 5.9.12/7.12.2, [`crate::quant::QuantDeltas`]) -- frame-constant (not |
| 821 | // per-superblock like [`CURRENT_Q_IDX`]), set once per frame decode and read |
| 822 | // back at every dequantization call site keyed on which plane it is doing. |
| 823 | // How many real `delta_q_present` symbol groups [`maybe_read_delta_q`] has |
| 824 | // read, across every call on the current thread -- the gate's proof that a |
| 825 | // `delta_q_present` stream actually reached the new reader. |
| 826 | thread_local! { |
| 827 | static DELTA_Q_HITS: std::cell::Cell<usize> = const { std::cell::Cell::new(0) }; |
| 828 | } |
| 829 | |
| 830 | /// Current value of [`DELTA_Q_HITS`]. |
| 831 | #[allow(dead_code)] // read only from the `#[cfg(test)]` gates |
| 832 | pub(crate) fn delta_q_hits() -> usize { |
| 833 | DELTA_Q_HITS.with(|c| c.get()) |
| 834 | } |
| 835 | |
| 836 | /// This frame's [`QUANT_DELTAS`] `(dc, ac)` pair for plane `plane_idx` (0 = |
| 837 | /// Y, 1 = U, 2 = V) -- luma has no coded `ac` delta (spec 5.9.12 never reads |
| 838 | /// one), so it is always `0` here. |
| 839 | fn plane_q_delta(plane_idx: usize, fctx: &crate::decode::FrameCtx) -> (i32, i32) { |
| 840 | let d = fctx.quant_deltas.with(|c| c.get()); |
| 841 | match plane_idx { |
| 842 | 0 => (d.y_dc, 0), |
| 843 | 1 => (d.u_dc, d.u_ac), |
| 844 | _ => (d.v_dc, d.v_ac), |
| 845 | } |
| 846 | } |
| 847 | |
| 848 | /// lane-av1-qmatrix: records this FRAME's quantisation-matrix levels |
| 849 | /// ([`crate::decode::FrameCtx::qm_levels`]) from the parsed |
| 850 | /// `quantization_params`. Called from `stream.rs` at the frame header, where |
| 851 | /// the refusal this lane lifted used to sit. |
| 852 | pub(crate) fn set_qm_levels(levels: [u8; 3], fctx: &crate::decode::FrameCtx) { |
| 853 | fctx.qm_levels.with(|c| c.set(levels)); |
| 854 | } |
| 855 | |
| 856 | /// This block's inverse quantisation matrix -- libaom `av1_get_iqmatrix` |
| 857 | /// (`av1/common/quant_common.c:243`) plus `setup_segmentation_dequant`'s |
| 858 | /// `av1_use_qmatrix` gate, evaluated per dequantisation call site the same |
| 859 | /// way the site already evaluates its quantizer index. `None` dequantises |
| 860 | /// flat (the only behaviour before lane-av1-qmatrix): |
| 861 | /// - a level of 15, which is both libaom's flat sentinel and what every |
| 862 | /// `using_qmatrix == 0` frame's levels are parked at; |
| 863 | /// - a 1D/identity transform (`is_2d_transform` false: libaom's |
| 864 | /// `av1_get_iqmatrix` reads the NULL slot for those); |
| 865 | /// - a LOSSLESS block (libaom `av1_use_qmatrix`'s `!xd->lossless[seg]`: the |
| 866 | /// segment-adjusted quantizer index clamped to 0 with all five plane |
| 867 | /// deltas zero -- the same `block_q_idx` every other dequant site reads). |
| 868 | fn block_iqmatrix( |
| 869 | fctx: &crate::decode::FrameCtx, |
| 870 | plane_idx: usize, |
| 871 | w: usize, |
| 872 | h: usize, |
| 873 | tx_type: TxType, |
| 874 | ) -> Option<crate::qm::Iqm> { |
| 875 | let levels = fctx.qm_levels.with(|c| c.get()); |
| 876 | let level = levels[plane_idx.min(2)]; |
| 877 | if usize::from(level) >= crate::qm::NUM_QM_LEVELS - 1 { |
| 878 | return None; |
| 879 | } |
| 880 | if !tx_type.is_2d_transform() { |
| 881 | return None; |
| 882 | } |
| 883 | let d = fctx.quant_deltas.with(|c| c.get()); |
| 884 | if block_q_idx(fctx) == 0 |
| 885 | && d.y_dc == 0 |
| 886 | && d.u_dc == 0 |
| 887 | && d.u_ac == 0 |
| 888 | && d.v_dc == 0 |
| 889 | && d.v_ac == 0 |
| 890 | { |
| 891 | return None; |
| 892 | } |
| 893 | crate::qm::iwt_matrix(usize::from(level), plane_idx != 0, w, h) |
| 894 | } |
| 895 | |
| 896 | /// `read_delta_qindex`/`read_delta_q_params` (spec 5.11.10, libaom |
| 897 | /// `decodemv.c:85-146,735-770`), called right after [`maybe_read_cdef_idx`] |
| 898 | /// at every block-decode call site (spec order `skip -> cdef -> delta_q`). |
| 899 | /// A no-op when `delta_q_present` is unset, or when this block is not at the |
| 900 | /// superblock's own top-left MI position (`b_row == 0 && b_col == 0` in |
| 901 | /// libaom, collapsed here to a position check since a partition tree's |
| 902 | /// top-left leaf is the only block that can ever land there), or when that |
| 903 | /// leaf both *is* the whole superblock and is `skip` (the one case libaom |
| 904 | /// itself skips the read, state carrying over unchanged). |
| 905 | 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) { |
| 906 | if !fctx.delta_q_present.with(|c| c.get()) { |
| 907 | return; |
| 908 | } |
| 909 | let sb_mi = sb_mi_cur(fctx) as usize; |
| 910 | if mi_r % sb_mi != 0 || mi_c % sb_mi != 0 { |
| 911 | return; |
| 912 | } |
| 913 | // `is_whole_sb` is the caller's "this leaf IS the superblock" flag, |
| 914 | // computed against the sequence's own superblock size (lane-sb128b r1: |
| 915 | // it used to be hardcoded against 64 and disabled under `sb128`, which a |
| 916 | // `PARTITION_NONE` 128x128 root -- itself the whole superblock -- makes |
| 917 | // wrong). |
| 918 | if is_whole_sb && skip { |
| 919 | return; |
| 920 | } |
| 921 | let mut abs = dec.symbol(&mut cdfs.delta_q) as i32; |
| 922 | const DELTA_Q_SMALL: i32 = 3; |
| 923 | if abs >= DELTA_Q_SMALL { |
| 924 | let rem_bits = dec.literal(3) + 1; |
| 925 | let thr = (1i32 << rem_bits) + 1; |
| 926 | abs = dec.literal(rem_bits) as i32 + thr; |
| 927 | } |
| 928 | let sign_negative = if abs != 0 { dec.literal(1) != 0 } else { true }; |
| 929 | let reduced = if sign_negative { -abs } else { abs }; |
| 930 | hit!(DELTA_Q_HITS); |
| 931 | let res = fctx.delta_q_res.with(|c| c.get()); |
| 932 | fctx.current_q_idx.with(|c| c.set((c.get() + reduced * res).clamp(1, 255))); |
| 933 | } |
| 934 | |
| 935 | // lane-realworld r5: this frame's `delta.lf_present`/`lf_res`/`lf_multi` |
| 936 | // (spec 5.9.18) -- same no-op-by-default corner-cut as `DELTA_Q_PRESENT`. |
| 937 | // The running per-plane/direction loop-filter delta (spec `DeltaLF[i]`, |
| 938 | // `i` in `0..FRAME_LF_COUNT`) -- reset to `[0; 4]` at the top of every tile, |
| 939 | // mutated only by [`maybe_read_delta_lf`]. `!delta_lf_multi` mode only ever |
| 940 | // updates index 0 and every reader treats that as the value for all 4 |
| 941 | // indices (spec `read_delta_lf_params`'s own single-cdf branch). |
| 942 | // How many real `delta_lf_present` symbol groups [`maybe_read_delta_lf`] has |
| 943 | // read -- the gate's proof the reader fired, mirroring [`DELTA_Q_HITS`]. |
| 944 | thread_local! { |
| 945 | static DELTA_LF_HITS: std::cell::Cell<usize> = const { std::cell::Cell::new(0) }; |
| 946 | } |
| 947 | |
| 948 | /// Current value of [`DELTA_LF_HITS`]. |
| 949 | #[allow(dead_code)] // read only from the `#[cfg(test)]` gates |
| 950 | pub(crate) fn delta_lf_hits() -> usize { |
| 951 | DELTA_LF_HITS.with(|c| c.get()) |
| 952 | } |
| 953 | |
| 954 | /// `read_delta_lflevel`/`read_delta_lf_params` (spec 5.11.11, libaom |
| 955 | /// `decodemv.c:99-146,735-770`) -- same shape and same call-site gating as |
| 956 | /// [`maybe_read_delta_q`] (spec order `delta_q -> delta_lf`), looped over |
| 957 | /// `FRAME_LF_COUNT = 4` planes/directions when `delta_lf_multi` is set, or |
| 958 | /// read once into index 0 otherwise. |
| 959 | 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) { |
| 960 | if !fctx.delta_lf_present.with(|c| c.get()) { |
| 961 | return; |
| 962 | } |
| 963 | let sb_mi = sb_mi_cur(fctx) as usize; |
| 964 | if mi_r % sb_mi != 0 || mi_c % sb_mi != 0 { |
| 965 | return; |
| 966 | } |
| 967 | // `is_whole_sb` is the caller's "this leaf IS the superblock" flag, |
| 968 | // computed against the sequence's own superblock size (lane-sb128b r1: |
| 969 | // it used to be hardcoded against 64 and disabled under `sb128`, which a |
| 970 | // `PARTITION_NONE` 128x128 root -- itself the whole superblock -- makes |
| 971 | // wrong). |
| 972 | if is_whole_sb && skip { |
| 973 | return; |
| 974 | } |
| 975 | let multi = fctx.delta_lf_multi.with(|c| c.get()); |
| 976 | let res = fctx.delta_lf_res.with(|c| c.get()); |
| 977 | let count = if multi { 4 } else { 1 }; |
| 978 | let mut cur = fctx.current_delta_lf.with(|c| c.get()); |
| 979 | for i in 0..count { |
| 980 | let mut abs = if multi { |
| 981 | dec.symbol(&mut cdfs.delta_lf_multi[i]) as i32 |
| 982 | } else { |
| 983 | dec.symbol(&mut cdfs.delta_lf) as i32 |
| 984 | }; |
| 985 | const DELTA_LF_SMALL: i32 = 3; |
| 986 | if abs >= DELTA_LF_SMALL { |
| 987 | let rem_bits = dec.literal(3) + 1; |
| 988 | let thr = (1i32 << rem_bits) + 1; |
| 989 | abs = dec.literal(rem_bits) as i32 + thr; |
| 990 | } |
| 991 | let sign_negative = if abs != 0 { dec.literal(1) != 0 } else { true }; |
| 992 | let reduced = if sign_negative { -abs } else { abs }; |
| 993 | cur[i] = (cur[i] + reduced * res).clamp(-63, 63); |
| 994 | hit!(DELTA_LF_HITS); |
| 995 | } |
| 996 | fctx.current_delta_lf.with(|c| c.set(cur)); |
| 997 | } |
| 998 | |
| 999 | /// `read_cdef` (spec 5.11.56, libaom `decodemv.c:read_cdef`), called right |
| 1000 | /// after `skip` is known at every block-decode call site. A no-op at |
| 1001 | /// `cdef.bits == 0` (0-bit literal, nothing to consume) -- and |
| 1002 | /// `read_cdef_params` already forces `bits = 0` under `coded_lossless`/ |
| 1003 | /// `allow_intrabc`, so neither needs a separate check here. Reads once per |
| 1004 | /// superblock, at the first non-skip block. |
| 1005 | fn maybe_read_cdef_idx( |
| 1006 | dec: &mut SymbolDecoder, |
| 1007 | mi_r: usize, |
| 1008 | mi_c: usize, |
| 1009 | // The reading BLOCK's own span in 4x4 mode-info units: a block wider or |
| 1010 | // taller than one 64x64 CDEF unit is the first non-skip block of EVERY |
| 1011 | // unit it covers, so its one literal drives all of them (lane-b128r). |
| 1012 | (w_mi, h_mi): (usize, usize), |
| 1013 | skip: bool, |
| 1014 | fctx: &crate::decode::FrameCtx, |
| 1015 | ) { |
| 1016 | let bits = fctx.cdef_bits.with(|c| c.get()); |
| 1017 | if bits == 0 || skip || fctx.cdef_transmitted.with(|c| c.get()) { |
| 1018 | return; |
| 1019 | } |
| 1020 | let idx = dec.literal(u32::from(bits)) as u8; |
| 1021 | fctx.cdef_transmitted.with(|c| c.set(true)); |
| 1022 | hit!(CDEF_IDX_HITS); |
| 1023 | let (sb_r, sb_c) = (mi_r / SB_MI as usize, mi_c / SB_MI as usize); |
| 1024 | let sb_cols = fctx.cdef_sb_cols.with(|c| c.get()); |
| 1025 | if sb_cols > 0 { |
| 1026 | // lane-b128r: libaom's four CDEF units of a 128 superblock all read |
| 1027 | // their strength off the MB_MODE_INFO of the first non-skip block in |
| 1028 | // their own unit -- and a block that COVERS several units (the 128 |
| 1029 | // root's own 128x128 block, the only one there is) is that block for |
| 1030 | // every unit it covers, so one literal drives all four. This grid is |
| 1031 | // per 64x64 unit, so the read stamps every unit the block covers; one |
| 1032 | // that is smaller than a unit stamps exactly the cell it always did. |
| 1033 | let (rows, cols) = ( |
| 1034 | (h_mi / SB_MI as usize).max(1), |
| 1035 | (w_mi / SB_MI as usize).max(1), |
| 1036 | ); |
| 1037 | fctx.cdef_idx_grid.with(|g| { |
| 1038 | let mut g = g.borrow_mut(); |
| 1039 | for dr in 0..rows { |
| 1040 | for dc in 0..cols { |
| 1041 | let i = (sb_r + dr) * sb_cols + sb_c + dc; |
| 1042 | if sb_c + dc < sb_cols && i < g.len() { |
| 1043 | g[i] = idx; |
| 1044 | } |
| 1045 | } |
| 1046 | } |
| 1047 | }); |
| 1048 | } |
| 1049 | } |
| 1050 | |
| 1051 | // --------------------------------------------------------------------------- |
| 1052 | // lane-seg: segmentation. Header (spec 5.9.14) is parsed by `ec-av1-syntax`; |
| 1053 | // this block is the per-block `segment_id` reader (spec 5.11.7-5.11.9) plus |
| 1054 | // the two consumers a real stream exercises: the per-segment quantizer index |
| 1055 | // (spec 7.12.2 `get_qindex`) and the per-segment loop-filter level |
| 1056 | // (spec 7.14.4). Shapes follow libaom `decodemv.c:read_segment_id`/ |
| 1057 | // `read_intra_segment_id`/`read_inter_segment_id` and |
| 1058 | // `pred_common.h:av1_get_spatial_seg_pred` -- note libaom returns the SPATIAL |
| 1059 | // PREDICTION (not 0) from `read_segment_id` when the block is `skip`, which |
| 1060 | // is what the oracle every gate here compares against actually does. |
| 1061 | thread_local! { |
| 1062 | /// `CurrentSegmentIds`: this frame's own map, `mi_rows * mi_cols` entries |
| 1063 | /// with stride `mi_cols` (NOT the padded `Neighbours` grid stride). |
| 1064 | /// `PrevSegmentIds`: the primary reference frame's saved map, empty when |
| 1065 | /// this frame has no primary reference (spec `load_previous_segment_ids`). |
| 1066 | /// `(mi_rows, mi_cols)` for both maps above. |
| 1067 | /// `AboveSegPredContext` (per mi column) / `LeftSegPredContext` (per mi |
| 1068 | /// row) -- spec 5.11.9's own `seg_id_predicted` neighbour state, which |
| 1069 | /// libaom keeps on the neighbouring `mbmi->seg_id_predicted` instead. |
| 1070 | /// The block currently being decoded -- read back by [`block_q_idx`]. |
| 1071 | /// The tile's own top-left mi position, for `AvailU`/`AvailL`. |
| 1072 | /// How many real `segment_id` symbols were read, and the set of segment |
| 1073 | /// ids any block ended up with -- the gate's proof the feature fired. |
| 1074 | static SEG_ID_HITS: std::cell::Cell<usize> = const { std::cell::Cell::new(0) }; |
| 1075 | static SEG_IDS_SEEN: std::cell::Cell<u8> = const { std::cell::Cell::new(0) }; |
| 1076 | /// lane-golomb2: the (lowest, highest) quantizer index any block has been |
| 1077 | /// dequantized with, i.e. `block_q_idx`'s own span. A gate that means to |
| 1078 | /// exercise `SEG_LVL_ALT_Q` needs a stream where this span is WIDER than |
| 1079 | /// zero (class `gate-blind-to-feature`): segmentation being enabled says |
| 1080 | /// nothing about the segments carrying distinct quantizers. |
| 1081 | static BLOCK_Q_SPAN: std::cell::Cell<(i32, i32)> = const { std::cell::Cell::new((i32::MAX, i32::MIN)) }; |
| 1082 | /// How many `seg_id_predicted` symbols were read (temporal update). |
| 1083 | static SEG_PRED_HITS: std::cell::Cell<usize> = const { std::cell::Cell::new(0) }; |
| 1084 | } |
| 1085 | |
| 1086 | /// Installs this frame's segmentation parameters and allocates its map, with |
| 1087 | /// `prev` the primary reference frame's saved map (spec |
| 1088 | /// `load_previous_segment_ids`; `None` for `PRIMARY_REF_NONE` or an empty |
| 1089 | /// slot). Called once per frame, whether or not segmentation is enabled. |
| 1090 | pub(crate) fn set_segmentation( |
| 1091 | seg: SegmentationParams, |
| 1092 | mi_rows: usize, |
| 1093 | mi_cols: usize, |
| 1094 | prev: Option<&[u8]>, fctx: &crate::decode::FrameCtx, |
| 1095 | ) { |
| 1096 | if crate::envflags::env_flag!("EC_HDR") { |
| 1097 | eprintln!( |
| 1098 | "EC_SEGHDR enabled={} update_map={} temporal={} preskip={} last_active={} data={:?}", |
| 1099 | seg.enabled, seg.update_map, seg.temporal_update, seg.seg_id_pre_skip, seg.last_active_seg_id, seg.feature_data |
| 1100 | ); |
| 1101 | } |
| 1102 | fctx.seg.with(|s| *s.borrow_mut() = seg); |
| 1103 | fctx.seg_mi_dims.with(|c| c.set((mi_rows, mi_cols))); |
| 1104 | fctx.seg_ids.with(|m| *m.borrow_mut() = vec![0u8; mi_rows * mi_cols]); |
| 1105 | fctx.prev_seg_ids.with(|m| { |
| 1106 | let mut m = m.borrow_mut(); |
| 1107 | m.clear(); |
| 1108 | if let Some(p) = prev { |
| 1109 | if p.len() == mi_rows * mi_cols { |
| 1110 | m.extend_from_slice(p); |
| 1111 | } |
| 1112 | } |
| 1113 | }); |
| 1114 | fctx.above_seg_pred.with(|m| *m.borrow_mut() = vec![0u8; mi_cols]); |
| 1115 | fctx.left_seg_pred.with(|m| *m.borrow_mut() = vec![0u8; mi_rows]); |
| 1116 | fctx.cur_segment_id.with(|c| c.set(0)); |
| 1117 | } |
| 1118 | |
| 1119 | /// This frame's decoded segment map, to be stored into whichever reference |
| 1120 | /// slots `refresh_frame_flags` names (spec 7.20's `SavedSegmentIds`). |
| 1121 | pub(crate) fn take_segment_ids(fctx: &crate::decode::FrameCtx) -> Vec<u8> { |
| 1122 | fctx.seg_ids.with(|m| m.borrow().clone()) |
| 1123 | } |
| 1124 | |
| 1125 | /// How many `segment_id` symbols have been read (never reset per frame). |
| 1126 | #[allow(dead_code)] // read only from the `#[cfg(test)]` gates |
| 1127 | pub(crate) fn segment_id_hits() -> usize { |
| 1128 | SEG_ID_HITS.with(|c| c.get()) |
| 1129 | } |
| 1130 | |
| 1131 | /// How many distinct segment ids any decoded block has carried. |
| 1132 | #[allow(dead_code)] // read only from the `#[cfg(test)]` gates |
| 1133 | pub(crate) fn segment_ids_seen() -> usize { |
| 1134 | SEG_IDS_SEEN.with(|c| c.get().count_ones() as usize) |
| 1135 | } |
| 1136 | |
| 1137 | /// The lowest and highest quantizer index any decoded block was dequantized |
| 1138 | /// with (`(i32::MAX, i32::MIN)` when nothing decoded). |
| 1139 | #[allow(dead_code)] // read only from the `#[cfg(test)]` gates |
| 1140 | pub(crate) fn block_q_span() -> (i32, i32) { |
| 1141 | BLOCK_Q_SPAN.with(|c| c.get()) |
| 1142 | } |
| 1143 | |
| 1144 | /// How many `seg_id_predicted` symbols have been read (temporal update). |
| 1145 | #[allow(dead_code)] // read only from the `#[cfg(test)]` gates |
| 1146 | pub(crate) fn segment_pred_hits() -> usize { |
| 1147 | SEG_PRED_HITS.with(|c| c.get()) |
| 1148 | } |
| 1149 | |
| 1150 | /// Resets the segmentation hit counters (gate setup). |
| 1151 | #[allow(dead_code)] // read only from the `#[cfg(test)]` gates |
| 1152 | pub(crate) fn reset_segment_hits() { |
| 1153 | SEG_ID_HITS.with(|c| c.set(0)); |
| 1154 | SEG_IDS_SEEN.with(|c| c.set(0)); |
| 1155 | SEG_PRED_HITS.with(|c| c.set(0)); |
| 1156 | BLOCK_Q_SPAN.with(|c| c.set((i32::MAX, i32::MIN))); |
| 1157 | } |
| 1158 | |
| 1159 | /// `seg_feature_active_idx(segment_id, feature)` for the frame currently |
| 1160 | /// being decoded, with its `FeatureData` value. |
| 1161 | fn seg_feature(segment_id: usize, feature: usize, fctx: &crate::decode::FrameCtx) -> Option<i32> { |
| 1162 | fctx.seg.with(|s| { |
| 1163 | let s = s.borrow(); |
| 1164 | s.feature_active(segment_id, feature) |
| 1165 | .then(|| i32::from(s.feature_data[segment_id][feature])) |
| 1166 | }) |
| 1167 | } |
| 1168 | |
| 1169 | /// `get_qindex(ignoreDeltaQ = 0, segment_id)` (spec 7.12.2, libaom |
| 1170 | /// `av1_get_qindex`) for the block currently being decoded: the running |
| 1171 | /// `CurrentQIndex` shifted by this block's segment's `SEG_LVL_ALT_Q` data. |
| 1172 | /// Every dequant call site reads the block's quantizer through here. |
| 1173 | fn block_q_idx(fctx: &crate::decode::FrameCtx) -> i32 { |
| 1174 | let base = fctx.current_q_idx.with(|c| c.get()); |
| 1175 | let seg_id = fctx.cur_segment_id.with(|c| c.get()) as usize; |
| 1176 | let q = match seg_feature(seg_id, SEG_LVL_ALT_Q, fctx) { |
| 1177 | Some(data) => (base + data).clamp(0, 255), |
| 1178 | None => base, |
| 1179 | }; |
| 1180 | // lane-d792: which SEGMENT a block's quantizer came out of, beside the |
| 1181 | // `EC_DQCOEFF` grid rung -- a dequant that is too low with the levels |
| 1182 | // already proven equal is a segment-id defect, not a quantizer one. |
| 1183 | if crate::envflags::env_flag!("EC_DQCOEFF") { |
| 1184 | eprintln!("OUR_Q base={base} seg={seg_id} q={q}"); |
| 1185 | } |
| 1186 | hit_do!(BLOCK_Q_SPAN.with(|c| { |
| 1187 | let (lo, hi) = c.get(); |
| 1188 | c.set((lo.min(q), hi.max(q))); |
| 1189 | });); |
| 1190 | q |
| 1191 | } |
| 1192 | |
| 1193 | /// The segment id recorded for one mi cell of the current frame's map. |
| 1194 | fn segment_id_at(mi_r: usize, mi_c: usize, fctx: &crate::decode::FrameCtx) -> u8 { |
| 1195 | let (mi_rows, mi_cols) = fctx.seg_mi_dims.with(|c| c.get()); |
| 1196 | if mi_r >= mi_rows || mi_c >= mi_cols { |
| 1197 | return 0; |
| 1198 | } |
| 1199 | fctx.seg_ids.with(|m| m.borrow()[mi_r * mi_cols + mi_c]) |
| 1200 | } |
| 1201 | |
| 1202 | /// `set_segment_id`: stamps `id` over every mi cell this block covers. |
| 1203 | fn write_segment_id(mi_r: usize, mi_c: usize, w_mi: usize, h_mi: usize, id: u8, fctx: &crate::decode::FrameCtx) { |
| 1204 | let (mi_rows, mi_cols) = fctx.seg_mi_dims.with(|c| c.get()); |
| 1205 | let (x_mis, y_mis) = (w_mi.min(mi_cols.saturating_sub(mi_c)), h_mi.min(mi_rows.saturating_sub(mi_r))); |
| 1206 | fctx.seg_ids.with(|m| { |
| 1207 | let mut m = m.borrow_mut(); |
| 1208 | for y in 0..y_mis { |
| 1209 | for x in 0..x_mis { |
| 1210 | m[(mi_r + y) * mi_cols + mi_c + x] = id; |
| 1211 | } |
| 1212 | } |
| 1213 | }); |
| 1214 | fctx.cur_segment_id.with(|c| c.set(id)); |
| 1215 | SEG_IDS_SEEN.with(|c| c.set(c.get() | 1 << id)); |
| 1216 | } |
| 1217 | |
| 1218 | /// `copy_segment_id` (libaom `decodemv.c:342`): the previous frame's map is |
| 1219 | /// inherited VERBATIM over this block's own mi footprint -- per-cell copies, |
| 1220 | /// not the minified id [`predicted_segment_id`] hands the block. Without a |
| 1221 | /// previous map the footprint is zeroed, exactly as the C NULL branch does. |
| 1222 | fn copy_segment_ids(mi_r: usize, mi_c: usize, w_mi: usize, h_mi: usize, fctx: &crate::decode::FrameCtx) { |
| 1223 | let (mi_rows, mi_cols) = fctx.seg_mi_dims.with(|c| c.get()); |
| 1224 | let (x_mis, y_mis) = (w_mi.min(mi_cols.saturating_sub(mi_c)), h_mi.min(mi_rows.saturating_sub(mi_r))); |
| 1225 | fctx.seg_ids.with(|m| { |
| 1226 | let mut m = m.borrow_mut(); |
| 1227 | fctx.prev_seg_ids.with(|p| { |
| 1228 | let p = p.borrow(); |
| 1229 | for y in 0..y_mis { |
| 1230 | for x in 0..x_mis { |
| 1231 | let idx = (mi_r + y) * mi_cols + mi_c + x; |
| 1232 | m[idx] = p.get(idx).copied().unwrap_or(0); |
| 1233 | } |
| 1234 | } |
| 1235 | }); |
| 1236 | }); |
| 1237 | } |
| 1238 | |
| 1239 | /// `get_predicted_segment_id` (libaom `dec_get_segment_id` over |
| 1240 | /// `last_frame_seg_map`): the minimum previous-frame segment id over the |
| 1241 | /// block's own mi footprint. `0` when there is no previous map. |
| 1242 | fn predicted_segment_id(mi_r: usize, mi_c: usize, w_mi: usize, h_mi: usize, fctx: &crate::decode::FrameCtx) -> u8 { |
| 1243 | let (mi_rows, mi_cols) = fctx.seg_mi_dims.with(|c| c.get()); |
| 1244 | fctx.prev_seg_ids.with(|m| { |
| 1245 | let m = m.borrow(); |
| 1246 | if m.is_empty() { |
| 1247 | return 0; |
| 1248 | } |
| 1249 | let (x_mis, y_mis) = (w_mi.min(mi_cols.saturating_sub(mi_c)), h_mi.min(mi_rows.saturating_sub(mi_r))); |
| 1250 | let mut id = u8::MAX; |
| 1251 | for y in 0..y_mis { |
| 1252 | for x in 0..x_mis { |
| 1253 | id = id.min(m[(mi_r + y) * mi_cols + mi_c + x]); |
| 1254 | } |
| 1255 | } |
| 1256 | if id == u8::MAX { 0 } else { id } |
| 1257 | }) |
| 1258 | } |
| 1259 | |
| 1260 | /// `av1_neg_deinterleave` (libaom `decodemv.c:258`). |
| 1261 | fn neg_deinterleave(diff: i32, reference: i32, max: i32) -> i32 { |
| 1262 | if reference == 0 { |
| 1263 | return diff; |
| 1264 | } |
| 1265 | if reference >= max - 1 { |
| 1266 | return max - diff - 1; |
| 1267 | } |
| 1268 | let half = if 2 * reference < max { |
| 1269 | diff <= 2 * reference |
| 1270 | } else { |
| 1271 | diff <= 2 * (max - reference - 1) |
| 1272 | }; |
| 1273 | if half { |
| 1274 | return if diff & 1 != 0 { |
| 1275 | reference + ((diff + 1) >> 1) |
| 1276 | } else { |
| 1277 | reference - (diff >> 1) |
| 1278 | }; |
| 1279 | } |
| 1280 | if 2 * reference < max { diff } else { max - (diff + 1) } |
| 1281 | } |
| 1282 | |
| 1283 | /// `av1_get_spatial_seg_pred`: `(prediction, cdf_index)` from the current |
| 1284 | /// frame's already-decoded above/left/above-left neighbours. |
| 1285 | fn spatial_seg_pred(mi_r: usize, mi_c: usize, fctx: &crate::decode::FrameCtx) -> (i32, usize) { |
| 1286 | let (row0, col0) = fctx.seg_tile_origin.with(|c| c.get()); |
| 1287 | let (avail_u, avail_l) = (mi_r > row0, mi_c > col0); |
| 1288 | let prev_ul = (avail_u && avail_l).then(|| i32::from(segment_id_at(mi_r - 1, mi_c - 1, fctx))); |
| 1289 | let prev_u = avail_u.then(|| i32::from(segment_id_at(mi_r - 1, mi_c, fctx))); |
| 1290 | let prev_l = avail_l.then(|| i32::from(segment_id_at(mi_r, mi_c - 1, fctx))); |
| 1291 | let ctx = match prev_ul { |
| 1292 | None => 0, |
| 1293 | Some(ul) if Some(ul) == prev_u && Some(ul) == prev_l => 2, |
| 1294 | Some(ul) if Some(ul) == prev_u || Some(ul) == prev_l || prev_u == prev_l => 1, |
| 1295 | Some(_) => 0, |
| 1296 | }; |
| 1297 | let pred = match (prev_u, prev_l) { |
| 1298 | (None, l) => l.unwrap_or(0), |
| 1299 | (Some(u), None) => u, |
| 1300 | (Some(u), Some(l)) => { |
| 1301 | if prev_ul == Some(u) { |
| 1302 | u |
| 1303 | } else { |
| 1304 | l |
| 1305 | } |
| 1306 | } |
| 1307 | }; |
| 1308 | (pred, ctx) |
| 1309 | } |
| 1310 | |
| 1311 | /// `read_segment_id` (libaom `decodemv.c:280`): the spatially predicted id |
| 1312 | /// when `skip`, otherwise one `segment_id` symbol de-interleaved against |
| 1313 | /// that prediction. Does NOT write the map -- callers do. |
| 1314 | fn read_segment_id(dec: &mut SymbolDecoder, cdfs: &mut Cdfs, mi_r: usize, mi_c: usize, skip: bool, fctx: &crate::decode::FrameCtx) -> u8 { |
| 1315 | let (pred, ctx) = spatial_seg_pred(mi_r, mi_c, fctx); |
| 1316 | if skip { |
| 1317 | return pred as u8; |
| 1318 | } |
| 1319 | let coded = dec.symbol(&mut cdfs.segment_id[ctx]) as i32; |
| 1320 | hit!(SEG_ID_HITS); |
| 1321 | let last_active = fctx.seg.with(|s| i32::from(s.borrow().last_active_seg_id)); |
| 1322 | let id = neg_deinterleave(coded, pred, last_active + 1).clamp(0, last_active); |
| 1323 | // Mirrors the oracle's own `EC_SEG mi_row=.. cdf=.. pred=.. coded=.. id=..` |
| 1324 | // (libaom `decodemv.c:read_segment_id`), for a cross-decoder segment diff. |
| 1325 | if crate::envflags::env_flag!("EC_TRACE_SEG") { |
| 1326 | eprintln!("EC_SEG mi_row={mi_r} mi_col={mi_c} cdf={ctx} pred={pred} coded={coded} id={id} rng={}", dec.debug_state().0); |
| 1327 | } |
| 1328 | id as u8 |
| 1329 | } |
| 1330 | |
| 1331 | /// `read_intra_segment_id` (spec 5.11.7): called twice per intra-frame block |
| 1332 | /// -- once before `skip` when `SegIdPreSkip`, once after it otherwise (the |
| 1333 | /// caller decides which by passing `pre_skip`). |
| 1334 | fn intra_segment_id( |
| 1335 | dec: &mut SymbolDecoder, |
| 1336 | cdfs: &mut Cdfs, |
| 1337 | mi_r: usize, |
| 1338 | mi_c: usize, |
| 1339 | w_mi: usize, |
| 1340 | h_mi: usize, |
| 1341 | skip: bool, fctx: &crate::decode::FrameCtx, |
| 1342 | ) { |
| 1343 | if !fctx.seg.with(|s| s.borrow().enabled) { |
| 1344 | return; |
| 1345 | } |
| 1346 | let id = read_segment_id(dec, cdfs, mi_r, mi_c, skip, fctx); |
| 1347 | write_segment_id(mi_r, mi_c, w_mi, h_mi, id, fctx); |
| 1348 | } |
| 1349 | |
| 1350 | /// Whether this frame's intra blocks read their `segment_id` before `skip` |
| 1351 | /// (`SegIdPreSkip`); `false` also when segmentation is off. |
| 1352 | fn seg_id_pre_skip(fctx: &crate::decode::FrameCtx) -> bool { |
| 1353 | fctx.seg.with(|s| { |
| 1354 | let s = s.borrow(); |
| 1355 | s.enabled && s.seg_id_pre_skip |
| 1356 | }) |
| 1357 | } |
| 1358 | |
| 1359 | /// `read_inter_segment_id(preskip)` (spec 5.11.9, libaom `decodemv.c:352`). |
| 1360 | /// Called at both spec positions of an inter frame's block: `pre_skip = true` |
| 1361 | /// before `skip_mode`/`skip`, and `pre_skip = false` after them. |
| 1362 | fn inter_segment_id( |
| 1363 | dec: &mut SymbolDecoder, |
| 1364 | cdfs: &mut Cdfs, |
| 1365 | mi_r: usize, |
| 1366 | mi_c: usize, |
| 1367 | w_mi: usize, |
| 1368 | h_mi: usize, |
| 1369 | skip: bool, |
| 1370 | pre_skip: bool, fctx: &crate::decode::FrameCtx, |
| 1371 | ) { |
| 1372 | let (enabled, update_map, temporal_update, pre_skip_frame) = fctx.seg.with(|s| { |
| 1373 | let s = s.borrow(); |
| 1374 | (s.enabled, s.update_map, s.temporal_update, s.seg_id_pre_skip) |
| 1375 | }); |
| 1376 | if !enabled { |
| 1377 | return; |
| 1378 | } |
| 1379 | if !update_map { |
| 1380 | // The map is inherited from the previous frame, and libaom's |
| 1381 | // `copy_segment_id` (decodemv.c:342) does that VERBATIM -- a memcpy of |
| 1382 | // the previous map over the block's own footprint, mixed ids intact -- |
| 1383 | // while the id the BLOCK itself carries is the min over that footprint |
| 1384 | // (`get_predicted_segment_id`). Stamping the min here flattened mixed |
| 1385 | // footprints into the saved map, and every later frame whose |
| 1386 | // `update_map == 0` copy or temporal prediction read that position |
| 1387 | // dequantized with the wrong segment's `SEG_LVL_ALT_Q` (class |
| 1388 | // `simplified-inheritance`). |
| 1389 | copy_segment_ids(mi_r, mi_c, w_mi, h_mi, fctx); |
| 1390 | let id = predicted_segment_id(mi_r, mi_c, w_mi, h_mi, fctx); |
| 1391 | fctx.cur_segment_id.with(|c| c.set(id)); |
| 1392 | SEG_IDS_SEEN.with(|c| c.set(c.get() | 1 << id)); |
| 1393 | return; |
| 1394 | } |
| 1395 | if pre_skip && !pre_skip_frame { |
| 1396 | return; |
| 1397 | } |
| 1398 | if !pre_skip && skip { |
| 1399 | // libaom: a skipped block never codes `seg_id_predicted`, and its |
| 1400 | // segment id is the spatial prediction. |
| 1401 | stamp_seg_pred(mi_r, mi_c, w_mi, h_mi, 0, fctx); |
| 1402 | let id = read_segment_id(dec, cdfs, mi_r, mi_c, true, fctx); |
| 1403 | write_segment_id(mi_r, mi_c, w_mi, h_mi, id, fctx); |
| 1404 | return; |
| 1405 | } |
| 1406 | let id = if temporal_update { |
| 1407 | let ctx = seg_pred_ctx(mi_r, mi_c, fctx); |
| 1408 | let predicted = dec.symbol(&mut cdfs.segment_pred[ctx]) != 0; |
| 1409 | hit!(SEG_PRED_HITS); |
| 1410 | stamp_seg_pred(mi_r, mi_c, w_mi, h_mi, u8::from(predicted), fctx); |
| 1411 | if predicted { |
| 1412 | predicted_segment_id(mi_r, mi_c, w_mi, h_mi, fctx) |
| 1413 | } else { |
| 1414 | read_segment_id(dec, cdfs, mi_r, mi_c, false, fctx) |
| 1415 | } |
| 1416 | } else { |
| 1417 | read_segment_id(dec, cdfs, mi_r, mi_c, false, fctx) |
| 1418 | }; |
| 1419 | write_segment_id(mi_r, mi_c, w_mi, h_mi, id, fctx); |
| 1420 | } |
| 1421 | |
| 1422 | /// `av1_get_pred_context_seg_id`: above + left `seg_id_predicted` flags. |
| 1423 | fn seg_pred_ctx(mi_r: usize, mi_c: usize, fctx: &crate::decode::FrameCtx) -> usize { |
| 1424 | // libaom reads the flag off `above_mbmi`/`left_mbmi`, which are NULL (and |
| 1425 | // so contribute 0) outside the tile -- the availability check here is |
| 1426 | // what keeps a previous tile's stamps out of this tile's context. |
| 1427 | let (row0, col0) = fctx.seg_tile_origin.with(|c| c.get()); |
| 1428 | let above = if mi_r > row0 { |
| 1429 | fctx.above_seg_pred.with(|m| m.borrow().get(mi_c).copied().unwrap_or(0)) |
| 1430 | } else { |
| 1431 | 0 |
| 1432 | }; |
| 1433 | let left = if mi_c > col0 { |
| 1434 | fctx.left_seg_pred.with(|m| m.borrow().get(mi_r).copied().unwrap_or(0)) |
| 1435 | } else { |
| 1436 | 0 |
| 1437 | }; |
| 1438 | usize::from(above + left) |
| 1439 | } |
| 1440 | |
| 1441 | /// Spec 5.11.9's `AboveSegPredContext`/`LeftSegPredContext` update. |
| 1442 | fn stamp_seg_pred(mi_r: usize, mi_c: usize, w_mi: usize, h_mi: usize, value: u8, fctx: &crate::decode::FrameCtx) { |
| 1443 | fctx.above_seg_pred.with(|m| { |
| 1444 | let mut m = m.borrow_mut(); |
| 1445 | for i in 0..w_mi { |
| 1446 | if let Some(cell) = m.get_mut(mi_c + i) { |
| 1447 | *cell = value; |
| 1448 | } |
| 1449 | } |
| 1450 | }); |
| 1451 | fctx.left_seg_pred.with(|m| { |
| 1452 | let mut m = m.borrow_mut(); |
| 1453 | for i in 0..h_mi { |
| 1454 | if let Some(cell) = m.get_mut(mi_r + i) { |
| 1455 | *cell = value; |
| 1456 | } |
| 1457 | } |
| 1458 | }); |
| 1459 | } |
| 1460 | |
| 1461 | // How many key-frame luma blocks resolved `smooth_neighbor` (spec |
| 1462 | // `get_intra_edge_filter_type`) to `true` -- lane-chroma r2's own before/after |
| 1463 | // counter, proving a stream actually put a directional block next to a |
| 1464 | // smooth-predicted neighbour rather than every block reading the (previously |
| 1465 | // hardcoded `false`) edge-filter strength bucket unchanged. |
| 1466 | thread_local! { |
| 1467 | static SMOOTH_LUMA_HITS: std::cell::Cell<usize> = |
| 1468 | const { std::cell::Cell::new(0) }; |
| 1469 | } |
| 1470 | |
| 1471 | /// Current value of [`SMOOTH_LUMA_HITS`]. |
| 1472 | #[allow(dead_code)] // read only from the `#[cfg(test)]` gates |
| 1473 | pub(crate) fn smooth_luma_hits() -> usize { |
| 1474 | SMOOTH_LUMA_HITS.with(|c| c.get()) |
| 1475 | } |
| 1476 | |
| 1477 | thread_local! { |
| 1478 | /// lane-band46 r1: how many split-transform units answered their intra |
| 1479 | /// reach DIFFERENTLY under libaom's per-unit rules ([`Reach::of_tu`], the |
| 1480 | /// palette2 r12 fix) than the standalone-block lookup this decoder used to |
| 1481 | /// apply to them (class reach-is-per-transform-unit). Every hit is a |
| 1482 | /// transform unit whose above-right / below-left reference samples would |
| 1483 | /// be wrong without that fix. |
| 1484 | static SPLIT_TU_REACH_FIX_HITS: std::cell::Cell<usize> = |
| 1485 | const { std::cell::Cell::new(0) }; |
| 1486 | } |
| 1487 | |
| 1488 | /// Current value of [`SPLIT_TU_REACH_FIX_HITS`]. |
| 1489 | #[allow(dead_code)] // read only from the `#[cfg(test)]` gates |
| 1490 | pub(crate) fn split_tu_reach_fix_hits() -> usize { |
| 1491 | SPLIT_TU_REACH_FIX_HITS.with(|c| c.get()) |
| 1492 | } |
| 1493 | |
| 1494 | /// [`Reach::of_tu`] for one transform unit, counting how often that per-unit |
| 1495 | /// answer differs from the standalone-block answer the call sites used before |
| 1496 | /// lane-palette2 r12 -- the counter a gate asserts on to prove a stream really |
| 1497 | /// exercised the fix. `col_off`/`row_off` are in SAMPLES from the parent |
| 1498 | /// block's top-left `(px, py)`. |
| 1499 | #[allow(clippy::too_many_arguments)] |
| 1500 | fn tu_reach( |
| 1501 | bw: usize, |
| 1502 | bh: usize, |
| 1503 | col_off: usize, |
| 1504 | row_off: usize, |
| 1505 | tx: usize, |
| 1506 | block: Reach, |
| 1507 | px: usize, |
| 1508 | py: usize, |
| 1509 | width: usize, |
| 1510 | height: usize, fctx: &crate::decode::FrameCtx, |
| 1511 | ) -> Reach { |
| 1512 | let fixed = Reach::of_tu(bw, bh, col_off, row_off, tx, tx, block); |
| 1513 | let standalone = Reach::of(tx, px + col_off, py + row_off, width, height, fctx); |
| 1514 | if fixed != standalone { |
| 1515 | hit!(SPLIT_TU_REACH_FIX_HITS); |
| 1516 | } |
| 1517 | fixed |
| 1518 | } |
| 1519 | |
| 1520 | /// [`tu_reach`] for a unit whose LUMA footprint is a `tx_w` x `tx_h` rect |
| 1521 | /// (lane-av1-422b: a 4:2:2 chroma unit is square in chroma pixels, but its |
| 1522 | /// luma anchor is `(chroma_tx << ss_x, chroma_tx << ss_y)` -- at ss (1,0) |
| 1523 | /// twice as wide as it is tall). Same predicate as [`tu_reach`]; the |
| 1524 | /// square mismatch counter only means anything when the footprint actually |
| 1525 | /// is square. |
| 1526 | #[allow(clippy::too_many_arguments)] |
| 1527 | fn tu_reach_rect( |
| 1528 | bw: usize, |
| 1529 | bh: usize, |
| 1530 | col_off: usize, |
| 1531 | row_off: usize, |
| 1532 | tx_w: usize, |
| 1533 | tx_h: usize, |
| 1534 | block: Reach, |
| 1535 | px: usize, |
| 1536 | py: usize, |
| 1537 | width: usize, |
| 1538 | height: usize, fctx: &crate::decode::FrameCtx, |
| 1539 | ) -> Reach { |
| 1540 | let fixed = Reach::of_tu(bw, bh, col_off, row_off, tx_w, tx_h, block); |
| 1541 | if tx_w == tx_h { |
| 1542 | let standalone = Reach::of(tx_w, px + col_off, py + row_off, width, height, fctx); |
| 1543 | if fixed != standalone { |
| 1544 | hit!(SPLIT_TU_REACH_FIX_HITS); |
| 1545 | } |
| 1546 | } |
| 1547 | fixed |
| 1548 | } |
| 1549 | |
| 1550 | // How many `uv_mode` reads resolved to `SMOOTH_PRED..=PAETH_PRED` (9..=12), |
| 1551 | // across every call on the current thread -- lane-chroma r3's own before/after |
| 1552 | // counter, proving a stream actually exercised chroma's smooth/paeth |
| 1553 | // predictor (the previously-refused round-2 gap) rather than every block |
| 1554 | // landing on DC/CFL/directional. |
| 1555 | thread_local! { |
| 1556 | static SMOOTH_UV_HITS: std::cell::Cell<usize> = |
| 1557 | const { std::cell::Cell::new(0) }; |
| 1558 | } |
| 1559 | |
| 1560 | /// Current value of [`SMOOTH_UV_HITS`]. |
| 1561 | #[allow(dead_code)] // read only from the `#[cfg(test)]` gates |
| 1562 | pub(crate) fn smooth_uv_hits() -> usize { |
| 1563 | SMOOTH_UV_HITS.with(|c| c.get()) |
| 1564 | } |
| 1565 | |
| 1566 | // How many tiles [`decode_key_frame_tile_with_cdfs`] has actually decoded a |
| 1567 | // full superblock walk over, across every call on the current thread -- |
| 1568 | // lane-tiles r2's own gate-blind-to-feature guard: a gate over a |
| 1569 | // multi-tile stream must prove more than one tile actually fired, not just |
| 1570 | // that the stream carried tile_info.cols > 1. |
| 1571 | thread_local! { |
| 1572 | static TILE_HITS: std::cell::Cell<usize> = |
| 1573 | const { std::cell::Cell::new(0) }; |
| 1574 | } |
| 1575 | /// Current value of [`TILE_HITS`]. |
| 1576 | #[allow(dead_code)] // read only from the `#[cfg(test)]` gates |
| 1577 | pub(crate) fn tile_hits() -> usize { |
| 1578 | TILE_HITS.with(|c| c.get()) |
| 1579 | } |
| 1580 | |
| 1581 | // How many 4-pixel deblocking edge groups [`edge_params`] has actually |
| 1582 | // selected for filtering, across every call on the current thread -- the same |
| 1583 | // before/after counter pattern as [`FILTER_INTRA_HITS`], proving a stream |
| 1584 | // exercised `apply_deblock` rather than every edge landing on level 0. |
| 1585 | thread_local! { |
| 1586 | static DEBLOCK_HITS: std::cell::Cell<usize> = |
| 1587 | const { std::cell::Cell::new(0) }; |
| 1588 | } |
| 1589 | /// lane-comppin r4: decode-order picture counter for `EC_AV1_PREFILT_DUMP`, |
| 1590 | /// mirroring stream.rs's `pictures_decoded` so the two dumps index |
| 1591 | /// identically against aomdec's own `ec_frame_idx` in decodeframe.c. |
| 1592 | static PREFILT_PICTURE_IDX: std::sync::atomic::AtomicUsize = |
| 1593 | std::sync::atomic::AtomicUsize::new(0); |
| 1594 | |
| 1595 | /// Current value of [`DEBLOCK_HITS`]. |
| 1596 | #[allow(dead_code)] // read only from the `#[cfg(test)]` gates |
| 1597 | pub(crate) fn deblock_hits() -> usize { |
| 1598 | DEBLOCK_HITS.with(|c| c.get()) |
| 1599 | } |
| 1600 | |
| 1601 | // lane-defon r1: how many COMPOUND_REFERENCE blocks read a |
| 1602 | // `UNIDIR_COMP_REFERENCE` pair (both references on the same temporal side, |
| 1603 | // spec 5.11.25 `comp_reference_type == 0`) -- the gate's proof that a |
| 1604 | // stream built with `--enable-onesided-comp=1` really exercised the |
| 1605 | // `uni_comp_ref` alphabet rather than only bidirectional pairs. |
| 1606 | thread_local! { |
| 1607 | static UNI_COMP_HITS: std::cell::Cell<usize> = |
| 1608 | const { std::cell::Cell::new(0) }; |
| 1609 | } |
| 1610 | |
| 1611 | /// Current value of [`UNI_COMP_HITS`]. |
| 1612 | #[allow(dead_code)] // read only from the `#[cfg(test)]` gates |
| 1613 | pub(crate) fn uni_comp_hits() -> usize { |
| 1614 | UNI_COMP_HITS.with(|c| c.get()) |
| 1615 | } |
| 1616 | |
| 1617 | // lane-defon r1: how many blocks blended through a `COMPOUND_DIFFWTD` mask |
| 1618 | // (`mc::diffwtd_mask`) -- the narrower half of `MASKED_COMPOUND_HITS`, the |
| 1619 | // gate's proof that `--enable-diff-wtd-comp=1` reached the difference-weighted |
| 1620 | // blend and not only the wedge arm. |
| 1621 | thread_local! { |
| 1622 | static DIFFWTD_HITS: std::cell::Cell<usize> = |
| 1623 | const { std::cell::Cell::new(0) }; |
| 1624 | } |
| 1625 | |
| 1626 | /// Current value of [`DIFFWTD_HITS`]. |
| 1627 | #[allow(dead_code)] // read only from the `#[cfg(test)]` gates |
| 1628 | pub(crate) fn diffwtd_hits() -> usize { |
| 1629 | DIFFWTD_HITS.with(|c| c.get()) |
| 1630 | } |
| 1631 | |
| 1632 | // lane-mcomp64 r1: how many blocks took the INFERRED `COMPOUND_DIFFWTD` arm -- |
| 1633 | // a block size where `av1_is_wedge_used` is false (64x64 is the only such size |
| 1634 | // this decoder's square inter path reaches), so no `compound_type` symbol is |
| 1635 | // coded at all. Strictly narrower than `DIFFWTD_HITS`, which also counts the |
| 1636 | // coded-symbol DIFFWTD blocks at 8x8/16x16/32x32. |
| 1637 | thread_local! { |
| 1638 | static DIFFWTD_INFERRED_HITS: std::cell::Cell<usize> = |
| 1639 | const { std::cell::Cell::new(0) }; |
| 1640 | } |
| 1641 | |
| 1642 | /// Current value of [`DIFFWTD_INFERRED_HITS`]. |
| 1643 | #[allow(dead_code)] // read only from the `#[cfg(test)]` gates |
| 1644 | pub(crate) fn diffwtd_inferred_hits() -> usize { |
| 1645 | DIFFWTD_INFERRED_HITS.with(|c| c.get()) |
| 1646 | } |
| 1647 | |
| 1648 | // lane-defon r1: how many FORWARD KEYFRAMES were decoded -- a KEY frame coded |
| 1649 | // with `show_frame == 0`, output later by a `show_existing_frame` header |
| 1650 | // (aomenc `--enable-fwd-kf=1 --fwd-kf-dist=N`). Counted from |
| 1651 | // `stream::decode_stream`, which is where the frame header is consumed; the |
| 1652 | // gate's proof that the on-value really reached the bitstream rather than |
| 1653 | // being dropped by the rate control. |
| 1654 | thread_local! { |
| 1655 | static FWD_KF_HITS: std::cell::Cell<usize> = |
| 1656 | const { std::cell::Cell::new(0) }; |
| 1657 | } |
| 1658 | |
| 1659 | /// Current value of [`FWD_KF_HITS`]. |
| 1660 | #[allow(dead_code)] // read only from the `#[cfg(test)]` gates |
| 1661 | pub(crate) fn fwd_kf_hits() -> usize { |
| 1662 | FWD_KF_HITS.with(|c| c.get()) |
| 1663 | } |
| 1664 | |
| 1665 | /// Records one decoded forward keyframe (see [`FWD_KF_HITS`]). |
| 1666 | pub(crate) fn note_fwd_kf() { |
| 1667 | hit!(FWD_KF_HITS); |
| 1668 | } |
| 1669 | |
| 1670 | // How many [`read_tx_size`] reads resolved a `tx_depth` strictly less than |
| 1671 | // the block's own side, across every call on the current thread -- the same |
| 1672 | // before/after counter pattern as [`FILTER_INTRA_HITS`], proving a stream |
| 1673 | // actually exercised a *split* transform under `TxMode::Select` rather than |
| 1674 | // every block coincidentally resolving `depth=0` (indistinguishable from |
| 1675 | // `TxMode::Largest` pixel-wise). |
| 1676 | thread_local! { |
| 1677 | static TX_DEPTH_HITS: std::cell::Cell<usize> = |
| 1678 | const { std::cell::Cell::new(0) }; |
| 1679 | } |
| 1680 | |
| 1681 | // How many plain SQUARE intra blocks (lane-sqchroma r1) resolved a luma |
| 1682 | // transform smaller than their own side (`tx_depth != 0`) while coding a |
| 1683 | // chroma prediction other than `DC_PRED` -- the pair the square path's chroma |
| 1684 | // only exercises with aomenc's tx-size search left ON (its default), which |
| 1685 | // every older recipe in this file pins off. Counted at the block, so a stream |
| 1686 | // where RD happened to give every split-transform block a DC chroma mode |
| 1687 | // reads as zero rather than silently passing for free. |
| 1688 | thread_local! { |
| 1689 | static SQ_CHROMA_TX_HITS: std::cell::Cell<usize> = |
| 1690 | const { std::cell::Cell::new(0) }; |
| 1691 | } |
| 1692 | |
| 1693 | /// Current value of [`SQ_CHROMA_TX_HITS`]. |
| 1694 | #[allow(dead_code)] // read only from the `#[cfg(test)]` gates |
| 1695 | pub(crate) fn sq_chroma_tx_hits() -> usize { |
| 1696 | SQ_CHROMA_TX_HITS.with(|c| c.get()) |
| 1697 | } |
| 1698 | |
| 1699 | /// Current value of [`TX_DEPTH_HITS`]. |
| 1700 | #[allow(dead_code)] // read only from the `#[cfg(test)]` gates |
| 1701 | pub(crate) fn tx_depth_hits() -> usize { |
| 1702 | TX_DEPTH_HITS.with(|c| c.get()) |
| 1703 | } |
| 1704 | |
| 1705 | // lane-txselect: how many `txfm_split` symbols an inter frame's var-tx tree |
| 1706 | // read, and how many of those actually took the split, on the current thread |
| 1707 | // -- the same before/after counter pattern as [`TX_DEPTH_HITS`], proving a |
| 1708 | // stream exercised spec 5.11.17's `read_var_tx_size` rather than every block |
| 1709 | // coincidentally coding one whole-block transform. |
| 1710 | thread_local! { |
| 1711 | static TXFM_SPLIT_READS: std::cell::Cell<usize> = const { std::cell::Cell::new(0) }; |
| 1712 | static TXFM_SPLIT_HITS: std::cell::Cell<usize> = const { std::cell::Cell::new(0) }; |
| 1713 | // lane-inter4 r2: rectangular INTER residual coding -- how many rect |
| 1714 | // strips coded one whole-block rect transform unit, and how many split |
| 1715 | // their rect max transform into square sub-transforms. |
| 1716 | static RECT_INTER_TU_HITS: std::cell::Cell<usize> = const { std::cell::Cell::new(0) }; |
| 1717 | static RECT_INTER_TXSPLIT_HITS: std::cell::Cell<usize> = const { std::cell::Cell::new(0) }; |
| 1718 | // lane-r14 r2: luma transform units of an inter strip with a 64-px axis |
| 1719 | // (64x32/32x64/64x16/16x64), whose coefficients are coded as the |
| 1720 | // truncated low-32 corner. |
| 1721 | static RECT64_INTER_TU_HITS: std::cell::Cell<usize> = const { std::cell::Cell::new(0) }; |
| 1722 | // lane-rectres r1: whole-block luma transform units of a 32x32-level 1:4 |
| 1723 | // inter strip, `[32x8, 8x32]`. |
| 1724 | static RECT32X8_INTER_TU_HITS: std::cell::Cell<[usize; 2]> = const { std::cell::Cell::new([0; 2]) }; |
| 1725 | // lane-r14 r3: rectangular var-tx LEAVES, [32x16, 16x32]. |
| 1726 | static VARTX_RECT_LEAF_HITS: std::cell::Cell<[usize; 2]> = const { std::cell::Cell::new([0; 2]) }; |
| 1727 | // lane-inter16ab r4: rectangular var-tx leaves with a 4-px axis, [8x4, 4x8] |
| 1728 | // -- the `sub_tx_size_map[TX_16X4]` split leaves of a 16x16-level 1:4 strip. |
| 1729 | static VARTX_RECT_LEAF4_HITS: std::cell::Cell<[usize; 2]> = const { std::cell::Cell::new([0; 2]) }; |
| 1730 | // lane-inter4 r3: 16x16-level PARTITION_HORZ / PARTITION_VERT on an inter |
| 1731 | // frame -- 16x8 and 8x16 inter leaves. |
| 1732 | static INTER_LEAF16_HORZ_HITS: std::cell::Cell<usize> = const { std::cell::Cell::new(0) }; |
| 1733 | static INTER_LEAF16_VERT_HITS: std::cell::Cell<usize> = const { std::cell::Cell::new(0) }; |
| 1734 | // This frame's own `tx_mode == TxMode::Select` and `reduced_tx_set` bits, |
| 1735 | // set once per inter tile by [`decode_inter_frame_tile_with_cdfs`] -- |
| 1736 | // `decode_inter_block` already carries forty parameters and both are |
| 1737 | // frame-constant. |
| 1738 | // lane-fiinter r1: this sequence header's own `enable_filter_intra` bit, |
| 1739 | // frame-constant like the two above and stashed the same way -- an intra |
| 1740 | // block inside an inter frame reads `use_filter_intra` under it. |
| 1741 | } |
| 1742 | |
| 1743 | /// Current value of [`TXFM_SPLIT_READS`]. |
| 1744 | #[allow(dead_code)] // read only from the `#[cfg(test)]` gates |
| 1745 | pub(crate) fn txfm_split_reads() -> usize { |
| 1746 | TXFM_SPLIT_READS.with(|c| c.get()) |
| 1747 | } |
| 1748 | |
| 1749 | /// Current value of [`TXFM_SPLIT_HITS`]. |
| 1750 | #[allow(dead_code)] // read only from the `#[cfg(test)]` gates |
| 1751 | pub(crate) fn txfm_split_hits() -> usize { |
| 1752 | TXFM_SPLIT_HITS.with(|c| c.get()) |
| 1753 | } |
| 1754 | |
| 1755 | /// Current value of [`INTER_LEAF16_HORZ_HITS`] (lane-inter4 r3). |
| 1756 | #[allow(dead_code)] // read only from the `#[cfg(test)]` gates |
| 1757 | pub(crate) fn inter_leaf16_horz_hits() -> usize { |
| 1758 | INTER_LEAF16_HORZ_HITS.with(|c| c.get()) |
| 1759 | } |
| 1760 | |
| 1761 | /// Current value of [`INTER_LEAF16_VERT_HITS`] (lane-inter4 r3). |
| 1762 | #[allow(dead_code)] // read only from the `#[cfg(test)]` gates |
| 1763 | pub(crate) fn inter_leaf16_vert_hits() -> usize { |
| 1764 | INTER_LEAF16_VERT_HITS.with(|c| c.get()) |
| 1765 | } |
| 1766 | |
| 1767 | /// Current value of [`RECT_INTER_TU_HITS`] (lane-inter4 r2). |
| 1768 | pub(crate) fn rect_inter_tu_hits() -> usize { |
| 1769 | RECT_INTER_TU_HITS.with(|c| c.get()) |
| 1770 | } |
| 1771 | |
| 1772 | /// Current value of [`RECT_INTER_TXSPLIT_HITS`] (lane-inter4 r2). |
| 1773 | pub(crate) fn rect_inter_txsplit_hits() -> usize { |
| 1774 | RECT_INTER_TXSPLIT_HITS.with(|c| c.get()) |
| 1775 | } |
| 1776 | |
| 1777 | /// Current value of [`RECT64_INTER_TU_HITS`] (lane-r14 r2). |
| 1778 | pub(crate) fn rect64_inter_tu_hits() -> usize { |
| 1779 | RECT64_INTER_TU_HITS.with(|c| c.get()) |
| 1780 | } |
| 1781 | |
| 1782 | /// Current value of [`RECT32X8_INTER_TU_HITS`] (lane-rectres r1): `[32x8, 8x32]` |
| 1783 | /// whole-block luma units of a 32x32-level 1:4 inter strip. |
| 1784 | pub(crate) fn rect32x8_inter_tu_hits() -> [usize; 2] { |
| 1785 | RECT32X8_INTER_TU_HITS.with(|c| c.get()) |
| 1786 | } |
| 1787 | |
| 1788 | /// Current value of [`VARTX_RECT_LEAF_HITS`] (lane-r14 r3): how many |
| 1789 | /// rectangular var-tx leaf transform units were coded, `[32x16, 16x32]`. |
| 1790 | pub(crate) fn vartx_rect_leaf_hits() -> [usize; 2] { |
| 1791 | VARTX_RECT_LEAF_HITS.with(|c| c.get()) |
| 1792 | } |
| 1793 | |
| 1794 | /// Current value of [`VARTX_RECT_LEAF4_HITS`] (lane-inter16ab r4): 8x4/4x8 |
| 1795 | /// split leaves of a 16x4/4x16 strip, `[8x4, 4x8]`. |
| 1796 | pub(crate) fn vartx_rect_leaf4_hits() -> [usize; 2] { |
| 1797 | VARTX_RECT_LEAF4_HITS.with(|c| c.get()) |
| 1798 | } |
| 1799 | |
| 1800 | /// Records one rectangular var-tx leaf. |
| 1801 | fn vartx_rect_leaf_hit(tw: usize, th: usize) { |
| 1802 | if crate::envflags::env_flag!("EC_VARTXLEAF") { |
| 1803 | eprintln!("EC_VARTXLEAF tw={tw} th={th}"); |
| 1804 | } |
| 1805 | let cell = if tw.min(th) == 4 { &VARTX_RECT_LEAF4_HITS } else { &VARTX_RECT_LEAF_HITS }; |
| 1806 | cell.with(|c| { |
| 1807 | let mut h = c.get(); |
| 1808 | h[usize::from(th > tw)] += 1; |
| 1809 | c.set(h); |
| 1810 | }); |
| 1811 | } |
| 1812 | |
| 1813 | /// Sets the current thread's [`TX_SELECT_INTER`]/[`REDUCED_TX_SET_INTER`]/ |
| 1814 | /// [`ENABLE_FILTER_INTRA_INTER`]. |
| 1815 | pub(crate) fn set_inter_tx_mode( |
| 1816 | tx_select: bool, |
| 1817 | reduced_tx_set: bool, |
| 1818 | enable_filter_intra: bool, fctx: &crate::decode::FrameCtx, |
| 1819 | ) { |
| 1820 | fctx.tx_select_inter.with(|c| c.set(tx_select)); |
| 1821 | fctx.intra_only.with(|c| c.set(false)); |
| 1822 | fctx.tx_select_frame.with(|c| c.set(tx_select)); |
| 1823 | fctx.reduced_tx_set_inter.with(|c| c.set(reduced_tx_set)); |
| 1824 | fctx.enable_filter_intra_inter.with(|c| c.set(enable_filter_intra)); |
| 1825 | } |
| 1826 | |
| 1827 | // How many `partition_w16` reads (a 16x16 block's own partition symbol, key |
| 1828 | // intra-frame path) resolved to `PARTITION_VERT_B` -- lane-rect16 r1: a |
| 1829 | // plain default-settings aomenc run over lavfi mandelbrot hits this arm on |
| 1830 | // frame 0's very first non-NONE/SPLIT `partition_w16` symbol (mi=(3,2)), not |
| 1831 | // the plain HORZ/VERT this lane's charter assumed. Left rect (8x16, real |
| 1832 | // `decode_block_rect`) + two 8x8 leaves stacked on the right (`decode_leaf8`, |
| 1833 | // chained via `prev_leaf` exactly like the existing 16x16-SPLIT-into-4x8x8 |
| 1834 | // path above). |
| 1835 | thread_local! { |
| 1836 | static VERT_B_INTRA_HITS: std::cell::Cell<usize> = |
| 1837 | const { std::cell::Cell::new(0) }; |
| 1838 | } |
| 1839 | |
| 1840 | /// Current value of [`VERT_B_INTRA_HITS`]. |
| 1841 | #[allow(dead_code)] // read only from the `#[cfg(test)]` gates |
| 1842 | pub(crate) fn vert_b_intra_hits() -> usize { |
| 1843 | VERT_B_INTRA_HITS.with(|c| c.get()) |
| 1844 | } |
| 1845 | |
| 1846 | // lane-ab16 r1: how many 16x16-level AB partitions fired, per arm, in |
| 1847 | // `PARTITION_HORZ_A`, `_HORZ_B`, `_VERT_A`, `_VERT_B` order. Each arm is two |
| 1848 | // 8x8 squares (`decode_leaf8`) plus one 16x8/8x16 strip (`decode_leaf_rect`, |
| 1849 | // the non-skip coefficient path lane-rectx proved); the gate hard-asserts |
| 1850 | // every arm separately so a run where aomenc's RD only picked one cannot |
| 1851 | // pass as proof of four. |
| 1852 | thread_local! { |
| 1853 | static AB16_HITS: std::cell::Cell<[usize; 4]> = const { std::cell::Cell::new([0; 4]) }; |
| 1854 | } |
| 1855 | |
| 1856 | // [`AB16_HITS`] per arm (HORZ_A, HORZ_B, VERT_A, VERT_B at 16x16). |
| 1857 | thread_local! { |
| 1858 | /// 4x4 transform units decoded inside an 8x8 square of a |
| 1859 | /// `PARTITION_VERT_A`/`_B` -- the exact call that panicked before |
| 1860 | /// [`crate::encode::Reach::of_tu`] (lane-ab16 r2), so the gate can prove |
| 1861 | /// a real `--enable-tx-size-search=1` stream reaches it. |
| 1862 | static VERT_AB_TX4_HITS: std::cell::Cell<usize> = const { std::cell::Cell::new(0) }; |
| 1863 | } |
| 1864 | |
| 1865 | #[allow(dead_code)] // read only from the `#[cfg(test)]` gates |
| 1866 | pub(crate) fn vert_ab_tx4_hits() -> usize { |
| 1867 | VERT_AB_TX4_HITS.with(std::cell::Cell::get) |
| 1868 | } |
| 1869 | |
| 1870 | #[allow(dead_code)] // read only from the `#[cfg(test)]` gates |
| 1871 | pub(crate) fn ab16_hits_by_arm() -> [usize; 4] { |
| 1872 | AB16_HITS.with(std::cell::Cell::get) |
| 1873 | } |
| 1874 | |
| 1875 | fn bump_ab16(arm: usize) { |
| 1876 | hit_do! { |
| 1877 | AB16_HITS.with(|c| { |
| 1878 | let mut v = c.get(); |
| 1879 | v[arm] += 1; |
| 1880 | c.set(v); |
| 1881 | }); |
| 1882 | } |
| 1883 | } |
| 1884 | |
| 1885 | // How many `partition_w16` reads resolved to a plain `PARTITION_HORZ`/ |
| 1886 | // `PARTITION_VERT` (lane-rect16 r2): re-measuring after the VERT_B fix above |
| 1887 | // showed mandelbrot's real first-hit blocker is this plain arm at mi=(0,7), |
| 1888 | // not VERT_B (`refusal-names-a-correlate`; the VERT_B write-up's "frame 0's |
| 1889 | // very first non-NONE/SPLIT symbol" claim was about decode ORDER within one |
| 1890 | // superblock's z-order recursion, not raster mi position). |
| 1891 | thread_local! { |
| 1892 | static HORZ_VERT_INTRA_HITS: std::cell::Cell<usize> = |
| 1893 | const { std::cell::Cell::new(0) }; |
| 1894 | } |
| 1895 | |
| 1896 | /// Current value of [`HORZ_VERT_INTRA_HITS`]. |
| 1897 | #[allow(dead_code)] // read only from the `#[cfg(test)]` gates |
| 1898 | pub(crate) fn horz_vert_intra_hits() -> usize { |
| 1899 | HORZ_VERT_INTRA_HITS.with(|c| c.get()) |
| 1900 | } |
| 1901 | |
| 1902 | // How many blocks were decoded as a frame-edge rect strip -- the single |
| 1903 | // coded half of a `PARTITION_HORZ`/`PARTITION_VERT` at the true frame edge |
| 1904 | // (lane-oddh r1), i.e. a block living inside the 8px band of a frame whose |
| 1905 | // height or width is 8 mod 16 (the user's 3840x1608 film). Intra and inter |
| 1906 | // paths share the counter. |
| 1907 | thread_local! { |
| 1908 | static EDGE_RECT_STRIP_HITS: std::cell::Cell<usize> = |
| 1909 | const { std::cell::Cell::new(0) }; |
| 1910 | } |
| 1911 | |
| 1912 | /// Current value of [`EDGE_RECT_STRIP_HITS`]. |
| 1913 | #[allow(dead_code)] // gate counter accessor with no reader yet; the counter itself is live |
| 1914 | pub(crate) fn edge_rect_strip_hits() -> usize { |
| 1915 | EDGE_RECT_STRIP_HITS.with(|c| c.get()) |
| 1916 | } |
| 1917 | |
| 1918 | // lane-interedge r1: frame-edge half strips on the INTER path, per partition |
| 1919 | // level (64/32/16) x axis (HORZ at a bottom edge, VERT at a right edge). The |
| 1920 | // index is `level_idx * 2 + axis` with `level_idx` 0=64, 1=32, 2=16 and |
| 1921 | // `axis` 0=HORZ (`!has_rows`), 1=VERT (`!has_cols`). Both of the user's films |
| 1922 | // are height mod 16 == 8, so every inter frame crosses these nodes. |
| 1923 | thread_local! { |
| 1924 | static INTER_EDGE_STRIP_HITS: std::cell::Cell<[usize; 6]> = |
| 1925 | const { std::cell::Cell::new([0; 6]) }; |
| 1926 | } |
| 1927 | |
| 1928 | /// Bumps one bucket of [`INTER_EDGE_STRIP_HITS`]. |
| 1929 | pub(crate) fn bump_inter_edge_strip(idx: usize) { |
| 1930 | hit_do! { |
| 1931 | INTER_EDGE_STRIP_HITS.with(|c| { |
| 1932 | let mut v = c.get(); |
| 1933 | v[idx] += 1; |
| 1934 | c.set(v); |
| 1935 | }); |
| 1936 | } |
| 1937 | } |
| 1938 | |
| 1939 | // lane-edgeboth r1: how many partition nodes were coded as the BOTH-AXES-CUT |
| 1940 | // forced split -- libaom `decode_partition` (decodeframe.c): |
| 1941 | // `else if (!has_rows && !has_cols) partition = PARTITION_SPLIT;`, the one |
| 1942 | // straddle case that reads NO symbol at all. Indexed by level: |
| 1943 | // 0 = 16x16, 1 = 32x32, 2 = 64x64, 3 = 128x128; intra and inter share it. |
| 1944 | thread_local! { |
| 1945 | static EDGE_BOTH_CUT_HITS: std::cell::Cell<[usize; 4]> = |
| 1946 | const { std::cell::Cell::new([0; 4]) }; |
| 1947 | } |
| 1948 | |
| 1949 | fn bump_edge_both_cut(level: usize) { |
| 1950 | hit_do! { |
| 1951 | EDGE_BOTH_CUT_HITS.with(|c| { |
| 1952 | let mut v = c.get(); |
| 1953 | v[level] += 1; |
| 1954 | c.set(v); |
| 1955 | }); |
| 1956 | } |
| 1957 | } |
| 1958 | |
| 1959 | /// Current value of [`INTER_EDGE_STRIP_HITS`] (`[h64, v64, h32, v32, h16, v16]`). |
| 1960 | pub fn inter_edge_strip_hits() -> [usize; 6] { |
| 1961 | INTER_EDGE_STRIP_HITS.with(|c| c.get()) |
| 1962 | } |
| 1963 | |
| 1964 | /// Resets [`INTER_EDGE_STRIP_HITS`] -- a refused attempt must not carry hits |
| 1965 | /// into the next one (class `counter-from-refused-stream`). |
| 1966 | pub fn reset_inter_edge_strip_hits() { |
| 1967 | INTER_EDGE_STRIP_HITS.with(|c| c.set([0; 6])); |
| 1968 | } |
| 1969 | |
| 1970 | /// Current value of [`EDGE_BOTH_CUT_HITS`] (`[16, 32, 64, 128]` levels). |
| 1971 | #[allow(dead_code)] // read only from the `#[cfg(test)]` gates |
| 1972 | pub(crate) fn edge_both_cut_hits() -> [usize; 4] { |
| 1973 | EDGE_BOTH_CUT_HITS.with(|c| c.get()) |
| 1974 | } |
| 1975 | |
| 1976 | /// Zeroes [`EDGE_BOTH_CUT_HITS`] so a gate can measure a per-attempt delta. |
| 1977 | #[allow(dead_code)] // read only from the `#[cfg(test)]` gates |
| 1978 | pub(crate) fn reset_edge_both_cut_hits() { |
| 1979 | EDGE_BOTH_CUT_HITS.with(|c| c.set([0; 4])); |
| 1980 | } |
| 1981 | |
| 1982 | // How many HORZ/VERT intra strips resolved a SPLIT luma transform |
| 1983 | // (`tx_depth != 0`) and decoded it per transform unit (lane-rectsplit r1) -- |
| 1984 | // the case every rect path here refused by name before, and the first |
| 1985 | // refusal the user's ReelB extract (600s in) stops at. |
| 1986 | thread_local! { |
| 1987 | static RECT_SPLIT_TX_HITS: std::cell::Cell<usize> = |
| 1988 | const { std::cell::Cell::new(0) }; |
| 1989 | } |
| 1990 | |
| 1991 | /// Current value of [`RECT_SPLIT_TX_HITS`]. |
| 1992 | #[allow(dead_code)] // read only from the `#[cfg(test)]` gates |
| 1993 | pub(crate) fn rect_split_tx_hits() -> usize { |
| 1994 | RECT_SPLIT_TX_HITS.with(|c| c.get()) |
| 1995 | } |
| 1996 | |
| 1997 | // The subset of [`RECT_SPLIT_TX_HITS`] that is BOTH superblock-level (one |
| 1998 | // side 64) AND split past the first depth, i.e. tiled more than one unit in |
| 1999 | // each axis -- the only shape with a transform unit that has neighbours on |
| 2000 | // two sides inside its own strip (`row_off > 0 && col_off > 0`), and the one |
| 2001 | // no gate fired before lane-rectsplit r2 (the shared counter above also |
| 2002 | // counts 32x32-level and depth-1 strips, so it cannot prove this case). |
| 2003 | thread_local! { |
| 2004 | static RECT_SPLIT_SB_INTERIOR_TU_HITS: std::cell::Cell<usize> = |
| 2005 | const { std::cell::Cell::new(0) }; |
| 2006 | } |
| 2007 | |
| 2008 | /// Current value of [`RECT_SPLIT_SB_INTERIOR_TU_HITS`]. |
| 2009 | #[allow(dead_code)] // read only from the `#[cfg(test)]` gates |
| 2010 | pub(crate) fn rect_split_sb_interior_tu_hits() -> usize { |
| 2011 | RECT_SPLIT_SB_INTERIOR_TU_HITS.with(|c| c.get()) |
| 2012 | } |
| 2013 | |
| 2014 | // Per-shape hit counts for the rect chroma transforms lane-rectchroma r1 |
| 2015 | // wired into [`decode_rect_split`] -- the shapes a split-transform strip's |
| 2016 | // chroma half takes that had no coefficient tables before (index order: |
| 2017 | // 32x8, 8x32, 8x4, 4x8). One counter per shape because a gate that only |
| 2018 | // proves the pair it happens to hit cannot catch a transposed scan/context |
| 2019 | // table (class scan-weights-cross-axis). |
| 2020 | thread_local! { |
| 2021 | static RECT_SPLIT_CHROMA_SHAPE_HITS: std::cell::Cell<[usize; 4]> = |
| 2022 | const { std::cell::Cell::new([0; 4]) }; |
| 2023 | } |
| 2024 | |
| 2025 | /// Current value of [`RECT_SPLIT_CHROMA_SHAPE_HITS`] (32x8, 8x32, 8x4, 4x8). |
| 2026 | #[allow(dead_code)] // read only from the `#[cfg(test)]` gates |
| 2027 | pub(crate) fn rect_split_chroma_shape_hits() -> [usize; 4] { |
| 2028 | RECT_SPLIT_CHROMA_SHAPE_HITS.with(|c| c.get()) |
| 2029 | } |
| 2030 | |
| 2031 | // lane-av1-422: how many SQUARE chroma coefficient units the strip path read |
| 2032 | // on a 4:2:2 frame (ss 1,0) -- the half-width, unhalved-height shape whose |
| 2033 | // tables only exist because of the square rows in the chroma_tiled arm. A |
| 2034 | // 4:2:0 or 4:4:4 frame can never increment it (their rect strips' chroma is |
| 2035 | // itself rect), and a restored `/2 chroma height` diverts 4:2:2 squares onto |
| 2036 | // the rect rows, so the witness gate reads 0 there and fails. |
| 2037 | thread_local! { |
| 2038 | static CHROMA422_SQUARE_HITS: std::cell::Cell<usize> = |
| 2039 | const { std::cell::Cell::new(0) }; |
| 2040 | } |
| 2041 | |
| 2042 | /// Current value of [`CHROMA422_SQUARE_HITS`]. |
| 2043 | // lane-av1-422b: `pub` so `examples/decode_probe.rs` can census it the way |
| 2044 | // it does every other capability counter. |
| 2045 | pub fn chroma422_square_hits() -> usize { |
| 2046 | CHROMA422_SQUARE_HITS.with(|c| c.get()) |
| 2047 | } |
| 2048 | |
| 2049 | // lane-av1-422f: how many RECT chroma coefficient units the square-block |
| 2050 | // path read on a 4:2:2 frame (ss 1,0) -- TX_4X8 / TX_8X16 / TX_16X32, the |
| 2051 | // plane block's own half-width, unhalved-height shape. A 4:2:0 or 4:4:4 |
| 2052 | // frame never takes the rect branch (their units are square |
| 2053 | // `chroma_tx == chroma_tx_h`), so a witness gate reading 0 here proves the |
| 2054 | // model reverted to squares. |
| 2055 | thread_local! { |
| 2056 | static CHROMA422_RECT_HITS: std::cell::Cell<usize> = |
| 2057 | const { std::cell::Cell::new(0) }; |
| 2058 | } |
| 2059 | |
| 2060 | /// Current value of [`CHROMA422_RECT_HITS`]. |
| 2061 | pub fn chroma422_rect_hits() -> usize { |
| 2062 | CHROMA422_RECT_HITS.with(|c| c.get()) |
| 2063 | } |
| 2064 | |
| 2065 | // lane-av1-422g: how many PER-LEAF TX_4X4 chroma coefficient units the |
| 2066 | // sub-8x8 split4 path read on a 4:2:2 frame (ss 1,0) -- one per even-column |
| 2067 | // leaf per plane (`is_chroma_reference`'s `!(mi_col & ss_x)`). A 4:2:0 frame |
| 2068 | // codes no sub-8 chroma at all and 4:4:4 goes through `sub8_leaf_chroma444`, |
| 2069 | // so a witness gate reading 0 here proves the per-leaf 4:2:2 arm never ran. |
| 2070 | thread_local! { |
| 2071 | static CHROMA422_SUB8_HITS: std::cell::Cell<usize> = |
| 2072 | const { std::cell::Cell::new(0) }; |
| 2073 | } |
| 2074 | |
| 2075 | /// Current value of [`CHROMA422_SUB8_HITS`]. |
| 2076 | pub fn chroma422_sub8_hits() -> usize { |
| 2077 | CHROMA422_SUB8_HITS.with(|c| c.get()) |
| 2078 | } |
| 2079 | |
| 2080 | // lane-av1-422sub8rect: how many PER-PIECE TX_4X4 chroma coefficient units |
| 2081 | // the INTER sub-8x8 split4 path read on a 4:2:2 frame (ss 1,0) -- one per |
| 2082 | // non-skipped odd-column piece per plane (`is_chroma_reference`'s |
| 2083 | // `mi_col & 0x01` at ss_x 1). 4:2:0 goes through the shared group unit and |
| 2084 | // 4:4:4 through [`sub8_leaf_chroma444`]'s own per-piece twin, so a witness |
| 2085 | // gate reading 0 here proves the inter 4:2:2 per-piece arm never ran. |
| 2086 | thread_local! { |
| 2087 | static CHROMA422_INTER_SUB8_HITS: std::cell::Cell<usize> = |
| 2088 | const { std::cell::Cell::new(0) }; |
| 2089 | } |
| 2090 | |
| 2091 | /// Current value of [`CHROMA422_INTER_SUB8_HITS`]. |
| 2092 | pub fn chroma422_inter_sub8_hits() -> usize { |
| 2093 | CHROMA422_INTER_SUB8_HITS.with(|c| c.get()) |
| 2094 | } |
| 2095 | |
| 2096 | // lane-av1-422m: how many TX_4X4 chunk units the invalid-ss-size chroma arm |
| 2097 | // read (the 8x32 luma block whose 4:2:2 plane has no `ss_size_lookup` block). |
| 2098 | // A 4:2:0 or 4:4:4 frame cannot increment it, so a non-zero read proves the |
| 2099 | // chunked 4:2:2 chroma really decoded. |
| 2100 | thread_local! { |
| 2101 | static CHROMA422_CHUNK_HITS: std::cell::Cell<usize> = |
| 2102 | const { std::cell::Cell::new(0) }; |
| 2103 | } |
| 2104 | |
| 2105 | /// Current value of [`CHROMA422_CHUNK_HITS`]. |
| 2106 | pub fn chroma422_chunk_hits() -> usize { |
| 2107 | CHROMA422_CHUNK_HITS.with(|c| c.get()) |
| 2108 | } |
| 2109 | |
| 2110 | // How many HORZ/VERT intra strips actually predicted with filter intra |
| 2111 | // (lane-rectsplit r1) -- the case `decode_block_rect` refused by name |
| 2112 | // ("this decoder predicts square-only") until `predict_filter_intra` took |
| 2113 | // `bw`x`bh`. |
| 2114 | thread_local! { |
| 2115 | static FILTER_INTRA_RECT_HITS: std::cell::Cell<usize> = |
| 2116 | const { std::cell::Cell::new(0) }; |
| 2117 | } |
| 2118 | |
| 2119 | /// Current value of [`FILTER_INTRA_RECT_HITS`]. |
| 2120 | #[allow(dead_code)] // read only from the `#[cfg(test)]` gates |
| 2121 | pub(crate) fn filter_intra_rect_hits() -> usize { |
| 2122 | FILTER_INTRA_RECT_HITS.with(|c| c.get()) |
| 2123 | } |
| 2124 | |
| 2125 | // As [`FILTER_INTRA_RECT_HITS`], counting only the 32x32-level 1:4 strips |
| 2126 | // (32x8/8x32, lane-fi32x8 r1) -- the shapes whose `use_filter_intra` symbol |
| 2127 | // was never read at all before that round, so a gate needs to prove the flag |
| 2128 | // fired at THAT shape and not at a 16x8 one somewhere else in the frame. |
| 2129 | // Split by ORIENTATION: rows 8 (32x8) and 9 (8x32) of `cdf::FILTER_INTRA` |
| 2130 | // carry different probabilities (18101 vs 20229), so a transposed pair is |
| 2131 | // only caught by a gate that sees the flag fire on BOTH shapes (class |
| 2132 | // scan-weights-cross-axis). |
| 2133 | thread_local! { |
| 2134 | static FILTER_INTRA_RECT4_HORZ_HITS: std::cell::Cell<usize> = |
| 2135 | const { std::cell::Cell::new(0) }; |
| 2136 | static FILTER_INTRA_RECT4_VERT_HITS: std::cell::Cell<usize> = |
| 2137 | const { std::cell::Cell::new(0) }; |
| 2138 | } |
| 2139 | |
| 2140 | /// Current value of [`FILTER_INTRA_RECT4_HORZ_HITS`] (32x8 strips). |
| 2141 | #[allow(dead_code)] // read only from the `#[cfg(test)]` gates |
| 2142 | pub(crate) fn filter_intra_rect4_horz_hits() -> usize { |
| 2143 | FILTER_INTRA_RECT4_HORZ_HITS.with(|c| c.get()) |
| 2144 | } |
| 2145 | |
| 2146 | /// Current value of [`FILTER_INTRA_RECT4_VERT_HITS`] (8x32 strips). |
| 2147 | #[allow(dead_code)] // read only from the `#[cfg(test)]` gates |
| 2148 | pub(crate) fn filter_intra_rect4_vert_hits() -> usize { |
| 2149 | FILTER_INTRA_RECT4_VERT_HITS.with(|c| c.get()) |
| 2150 | } |
| 2151 | |
| 2152 | // How many rectangular transform units actually coded coefficients through |
| 2153 | // [`read_coeffs_rect`] (`all_zero == 0`) -- lane-rect1d's measure that a gate |
| 2154 | // exercised the rect coefficient reader at all rather than passing on |
| 2155 | // all-skip blocks. |
| 2156 | thread_local! { |
| 2157 | static RECT_COEFF_TU_HITS: std::cell::Cell<usize> = const { std::cell::Cell::new(0) }; |
| 2158 | static RECT_CLASS1_HITS: std::cell::Cell<usize> = const { std::cell::Cell::new(0) }; |
| 2159 | } |
| 2160 | |
| 2161 | /// Current value of [`RECT_COEFF_TU_HITS`]. |
| 2162 | #[allow(dead_code)] // read only from the `#[cfg(test)]` gates |
| 2163 | pub(crate) fn rect_coeff_tu_hits() -> usize { |
| 2164 | RECT_COEFF_TU_HITS.with(|c| c.get()) |
| 2165 | } |
| 2166 | |
| 2167 | /// Rectangular transform units whose resolved `tx_type` is a 1D class |
| 2168 | /// (`V_DCT`/`H_DCT`) -- [`TX_CLASS1_HITS`]'s rect-only half, so a gate can |
| 2169 | /// pin that the 1D-class-on-a-RECT-transform path fired rather than settling |
| 2170 | /// for the square reader's own 1D types. |
| 2171 | #[allow(dead_code)] // read only from the `#[cfg(test)]` gates |
| 2172 | pub(crate) fn rect_class1_hits() -> usize { |
| 2173 | RECT_CLASS1_HITS.with(|c| c.get()) |
| 2174 | } |
| 2175 | |
| 2176 | // As [`FILTER_INTRA_RECT_HITS`], counting only the strips BELOW 16x16 |
| 2177 | // (8x16/16x8 leaves of a 16x16 HORZ/VERT partition, lane-fistrip r1) -- the |
| 2178 | // shapes whose `use_filter_intra` symbol had no CDF class before this round. |
| 2179 | // A gate asserting the wider counter would be satisfied by a 32x16 strip and |
| 2180 | // prove nothing about the new shapes. |
| 2181 | thread_local! { |
| 2182 | static FILTER_INTRA_RECT_SUB16_HITS: std::cell::Cell<usize> = |
| 2183 | const { std::cell::Cell::new(0) }; |
| 2184 | } |
| 2185 | |
| 2186 | /// Current value of [`FILTER_INTRA_RECT_SUB16_HITS`]. |
| 2187 | #[allow(dead_code)] // read only from the `#[cfg(test)]` gates |
| 2188 | pub(crate) fn filter_intra_rect_sub16_hits() -> usize { |
| 2189 | FILTER_INTRA_RECT_SUB16_HITS.with(|c| c.get()) |
| 2190 | } |
| 2191 | |
| 2192 | // As [`FILTER_INTRA_RECT_SUB16_HITS`], one partition level further down: |
| 2193 | // 4x8/8x4 leaves of an 8x8 HORZ/VERT (lane-fi8 r1). A gate on the sub16 |
| 2194 | // counter would be satisfied by a 16x8 strip and prove nothing here. |
| 2195 | thread_local! { |
| 2196 | static FILTER_INTRA_RECT_SUB8_HITS: std::cell::Cell<usize> = |
| 2197 | const { std::cell::Cell::new(0) }; |
| 2198 | } |
| 2199 | |
| 2200 | /// Current value of [`FILTER_INTRA_RECT_SUB8_HITS`]. |
| 2201 | #[allow(dead_code)] // read only from the `#[cfg(test)]` gates |
| 2202 | pub(crate) fn filter_intra_rect_sub8_hits() -> usize { |
| 2203 | FILTER_INTRA_RECT_SUB8_HITS.with(|c| c.get()) |
| 2204 | } |
| 2205 | |
| 2206 | // How many [`read_coeffs`] reads resolved a `tx_type` outside `TX_CLASS_2D` |
| 2207 | // (`V_DCT`/`H_DCT`, spec 5.11.39's `TxClass::Horiz`/`Vert`), across every |
| 2208 | // call on the current thread -- the same before/after counter pattern as |
| 2209 | // [`TX_DEPTH_HITS`], proving a stream actually exercised the class-split |
| 2210 | // `eob_pt`/1D-neighbour context path rather than every block coincidentally |
| 2211 | // landing on `DCT_DCT`. |
| 2212 | thread_local! { |
| 2213 | static TX_CLASS1_HITS: std::cell::Cell<usize> = |
| 2214 | const { std::cell::Cell::new(0) }; |
| 2215 | } |
| 2216 | |
| 2217 | // How many `uv_mode` reads resolved to a directional chroma mode (neither |
| 2218 | // `DC_PRED` nor `UV_CFL_PRED`), across every call on the current thread -- the same |
| 2219 | // before/after counter pattern as [`FILTER_INTRA_HITS`], proving a stream |
| 2220 | // actually exercised chroma's own directional predictor. |
| 2221 | thread_local! { |
| 2222 | static DIRECTIONAL_UV_HITS: std::cell::Cell<usize> = |
| 2223 | const { std::cell::Cell::new(0) }; |
| 2224 | } |
| 2225 | |
| 2226 | /// Current value of [`DIRECTIONAL_UV_HITS`]. |
| 2227 | #[allow(dead_code)] // read only from the `#[cfg(test)]` gates |
| 2228 | pub(crate) fn directional_uv_hits() -> usize { |
| 2229 | DIRECTIONAL_UV_HITS.with(|c| c.get()) |
| 2230 | } |
| 2231 | |
| 2232 | // How many `angle_delta`/`angle_delta_uv` symbols this decoder has read as |
| 2233 | // something other than [`ANGLE_DELTA_ZERO`], across every call in the |
| 2234 | // process -- the same before/after counter pattern as [`FILTER_INTRA_HITS`], |
| 2235 | // proving a stream actually exercised a nonzero angle delta. |
| 2236 | thread_local! { |
| 2237 | static ANGLE_DELTA_HITS: std::cell::Cell<usize> = |
| 2238 | const { std::cell::Cell::new(0) }; |
| 2239 | } |
| 2240 | |
| 2241 | // How many [`read_single_ref`] reads resolved to a reference other than |
| 2242 | // `LAST_FRAME`, across every call on the current thread -- the same before/after |
| 2243 | // counter pattern as [`FILTER_INTRA_HITS`], proving a stream actually |
| 2244 | // exercised a non-`LAST_FRAME` reference (`GOLDEN_FRAME`, this decoder's |
| 2245 | // only other supported one so far) rather than every block coincidentally |
| 2246 | // resolving `LAST_FRAME`. |
| 2247 | thread_local! { |
| 2248 | static NON_LAST_REF_HITS: std::cell::Cell<usize> = |
| 2249 | const { std::cell::Cell::new(0) }; |
| 2250 | } |
| 2251 | |
| 2252 | /// Current value of [`NON_LAST_REF_HITS`]. |
| 2253 | #[allow(dead_code)] // read only from the `#[cfg(test)]` gates |
| 2254 | pub(crate) fn non_last_ref_hits() -> usize { |
| 2255 | NON_LAST_REF_HITS.with(|c| c.get()) |
| 2256 | } |
| 2257 | |
| 2258 | // Per-reference [`NON_LAST_REF_HITS`] breakdown (lane-av1refs): one counter |
| 2259 | // per `MV_REFERENCE_FRAME` past `LAST_FRAME`, so a gate targeting one |
| 2260 | // specific reference (`LAST2`/`LAST3`/`GOLDEN`/`BWDREF`/`ALTREF2`/`ALTREF`) |
| 2261 | // can prove THAT reference fired rather than any non-`LAST_FRAME` one -- |
| 2262 | // `NON_LAST_REF_HITS` alone cannot tell a `LAST2_FRAME` gate from a |
| 2263 | // `GOLDEN_FRAME` draw it did not ask for. |
| 2264 | thread_local! { |
| 2265 | static LAST2_HITS: std::cell::Cell<usize> = |
| 2266 | const { std::cell::Cell::new(0) }; |
| 2267 | } |
| 2268 | thread_local! { |
| 2269 | static LAST3_HITS: std::cell::Cell<usize> = |
| 2270 | const { std::cell::Cell::new(0) }; |
| 2271 | } |
| 2272 | thread_local! { |
| 2273 | static GOLDEN_HITS: std::cell::Cell<usize> = |
| 2274 | const { std::cell::Cell::new(0) }; |
| 2275 | } |
| 2276 | thread_local! { |
| 2277 | static BWDREF_HITS: std::cell::Cell<usize> = |
| 2278 | const { std::cell::Cell::new(0) }; |
| 2279 | } |
| 2280 | thread_local! { |
| 2281 | static ALTREF2_HITS: std::cell::Cell<usize> = |
| 2282 | const { std::cell::Cell::new(0) }; |
| 2283 | } |
| 2284 | thread_local! { |
| 2285 | static ALTREF_HITS: std::cell::Cell<usize> = |
| 2286 | const { std::cell::Cell::new(0) }; |
| 2287 | } |
| 2288 | |
| 2289 | // How many [`read_comp_mode`] reads resolved `COMPOUND_REFERENCE` (lane-av1comp) |
| 2290 | // -- proves a `reference_select` stream actually reached a block that picked |
| 2291 | // compound, not just that the header bit was set. Every such block is |
| 2292 | // refused after this fires ([`decode_inter_block`]/[`decode_inter_block8`]), |
| 2293 | // so this only ever counts attempted reads, never a completed decode. |
| 2294 | thread_local! { |
| 2295 | static COMP_MODE_HITS: std::cell::Cell<usize> = |
| 2296 | const { std::cell::Cell::new(0) }; |
| 2297 | } |
| 2298 | |
| 2299 | /// Current value of [`COMP_MODE_HITS`]. |
| 2300 | #[allow(dead_code)] // read only from the `#[cfg(test)]` gates |
| 2301 | pub(crate) fn comp_mode_hits() -> usize { |
| 2302 | COMP_MODE_HITS.with(|c| c.get()) |
| 2303 | } |
| 2304 | |
| 2305 | // How many blocks [`read_intra_mode`] decoded a real (nonzero-size) palette-Y |
| 2306 | // use for, across every call on the current thread -- the same before/after |
| 2307 | // counter pattern as [`FILTER_INTRA_HITS`], proving a stream actually |
| 2308 | // exercised palette reconstruction rather than only reading and refusing the |
| 2309 | // `palette_y_mode` symbol. |
| 2310 | thread_local! { |
| 2311 | static PALETTE_HITS: std::cell::Cell<usize> = const { std::cell::Cell::new(0) }; |
| 2312 | } |
| 2313 | |
| 2314 | /// Current value of [`PALETTE_HITS`]. |
| 2315 | #[allow(dead_code)] // read only from the `#[cfg(test)]` gates |
| 2316 | pub(crate) fn palette_hits() -> usize { |
| 2317 | PALETTE_HITS.with(|c| c.get()) |
| 2318 | } |
| 2319 | |
| 2320 | // lane-tiles r12: of those, how many sat at their own TILE's left column -- |
| 2321 | // exactly the blocks whose palette cache/ctx must NOT see the previous tile's |
| 2322 | // colours (`av1_get_palette_cache`'s `xd->left_mbmi == NULL`). A multi-tile |
| 2323 | // palette gate that never hits one proves nothing about the tile guard. |
| 2324 | thread_local! { |
| 2325 | static PALETTE_TILE_LEFT_HITS: std::cell::Cell<usize> = const { std::cell::Cell::new(0) }; |
| 2326 | } |
| 2327 | |
| 2328 | /// Current value of [`PALETTE_TILE_LEFT_HITS`]. |
| 2329 | #[allow(dead_code)] // read only from the `#[cfg(test)]` gates |
| 2330 | pub(crate) fn palette_tile_left_hits() -> usize { |
| 2331 | PALETTE_TILE_LEFT_HITS.with(|c| c.get()) |
| 2332 | } |
| 2333 | |
| 2334 | // lane-palette2 r1: as [`PALETTE_HITS`], for a real (nonzero-size) chroma |
| 2335 | // palette use -- proves a stream actually reconstructed UV palette pixels, |
| 2336 | // not just read and refused the `palette_uv_mode` symbol. |
| 2337 | thread_local! { |
| 2338 | static PALETTE_UV_HITS: std::cell::Cell<usize> = const { std::cell::Cell::new(0) }; |
| 2339 | } |
| 2340 | |
| 2341 | /// Current value of [`PALETTE_UV_HITS`]. |
| 2342 | #[allow(dead_code)] // read only from the `#[cfg(test)]` gates |
| 2343 | pub(crate) fn palette_uv_hits() -> usize { |
| 2344 | PALETTE_UV_HITS.with(|c| c.get()) |
| 2345 | } |
| 2346 | |
| 2347 | // lane-palette2 r1: how many HORZ/VERT rect intra strips ([`read_intra_mode_rect`]) |
| 2348 | // decoded a real palette-Y use -- proves the rect strip's own palette syntax |
| 2349 | // (not just the square path's) actually fired. |
| 2350 | thread_local! { |
| 2351 | static PALETTE_RECT_HITS: std::cell::Cell<usize> = const { std::cell::Cell::new(0) }; |
| 2352 | } |
| 2353 | |
| 2354 | /// Current value of [`PALETTE_RECT_HITS`]. |
| 2355 | #[allow(dead_code)] // read only from the `#[cfg(test)]` gates |
| 2356 | pub(crate) fn palette_rect_hits() -> usize { |
| 2357 | PALETTE_RECT_HITS.with(|c| c.get()) |
| 2358 | } |
| 2359 | |
| 2360 | // lane-palette2 r6: how many HORZ/VERT rect intra strips ran the |
| 2361 | // [`palette_bsize_ctx_wh`]-gated code path in [`read_intra_mode_rect`] -- |
| 2362 | // i.e. `allow_screen_content_tools` was set and the strip's `bw`x`bh` was in |
| 2363 | // range, so `palette_y_mode`/`palette_uv_mode` were actually read (and, per |
| 2364 | // RD, almost always refused-on-use because the block doesn't end up USING a |
| 2365 | // palette -- see r5's report). This is the site the removed refusal |
| 2366 | // ("a HORZ/VERT intra strip in a screen-content frame") used to guard: it |
| 2367 | // fired here whether or not the block picked a palette, so this is the |
| 2368 | // counter that proves the case r5's narrower [`PALETTE_RECT_HITS`] almost |
| 2369 | // never observes. |
| 2370 | thread_local! { |
| 2371 | static RECT_SCREEN_CONTENT_HITS: std::cell::Cell<usize> = const { std::cell::Cell::new(0) }; |
| 2372 | } |
| 2373 | |
| 2374 | /// Current value of [`RECT_SCREEN_CONTENT_HITS`]. |
| 2375 | #[allow(dead_code)] // read only from the `#[cfg(test)]` gates |
| 2376 | pub(crate) fn rect_screen_content_hits() -> usize { |
| 2377 | RECT_SCREEN_CONTENT_HITS.with(|c| c.get()) |
| 2378 | } |
| 2379 | |
| 2380 | // lane-palette2 r1: how many palette-Y luma transform UNITS decoded under a |
| 2381 | // split luma transform (`logical_tx != side`) -- proves the per-TU index-map |
| 2382 | // windowing actually ran, not just that the whole-block path stayed exact. |
| 2383 | // Counts units, not blocks (a 64x64 palette block split to 16x16 TUs adds 16); |
| 2384 | // first actually incremented in r7, when the refusal it guarded was lifted. |
| 2385 | thread_local! { |
| 2386 | static PALETTE_SPLIT_TX_HITS: std::cell::Cell<usize> = const { std::cell::Cell::new(0) }; |
| 2387 | } |
| 2388 | |
| 2389 | thread_local! { |
| 2390 | /// lane-kf900 r4: rect intra strips that ACTUALLY use a palette -- the |
| 2391 | /// 1:4 shapes (32x8/8x32, 16x4/4x16) and the sub-16x16 2:1 leaves |
| 2392 | /// (16x8/8x16), the two shapes the lifted refusals named. |
| 2393 | static RECT4_PALETTE_HITS: std::cell::Cell<usize> = const { std::cell::Cell::new(0) }; |
| 2394 | } |
| 2395 | |
| 2396 | /// Current value of [`RECT4_PALETTE_HITS`]. |
| 2397 | #[allow(dead_code)] // read only from the `#[cfg(test)]` gates |
| 2398 | pub(crate) fn rect4_palette_hits() -> usize { |
| 2399 | RECT4_PALETTE_HITS.with(|c| c.get()) |
| 2400 | } |
| 2401 | |
| 2402 | /// Current value of [`PALETTE_SPLIT_TX_HITS`]. |
| 2403 | #[allow(dead_code)] // read only from the `#[cfg(test)]` gates |
| 2404 | pub(crate) fn palette_split_tx_hits() -> usize { |
| 2405 | PALETTE_SPLIT_TX_HITS.with(|c| c.get()) |
| 2406 | } |
| 2407 | |
| 2408 | thread_local! { |
| 2409 | /// lane-pal8 r1: square 8x8 LEAVES ([`decode_leaf8`]) that actually use a |
| 2410 | /// palette (Y or UV) -- the shape every 8-bit `--tune-content=screen` |
| 2411 | /// stream stopped on, since aomenc palettes square blocks at 8 bit and |
| 2412 | /// `--min-partition-size=8` puts them on this leaf path. |
| 2413 | static PALETTE_LEAF8_HITS: std::cell::Cell<usize> = const { std::cell::Cell::new(0) }; |
| 2414 | } |
| 2415 | |
| 2416 | /// Current value of [`PALETTE_LEAF8_HITS`]. |
| 2417 | #[allow(dead_code)] // read only from the `#[cfg(test)]` gates |
| 2418 | pub(crate) fn palette_leaf8_hits() -> usize { |
| 2419 | PALETTE_LEAF8_HITS.with(|c| c.get()) |
| 2420 | } |
| 2421 | |
| 2422 | /// Zeroes [`PALETTE_LEAF8_HITS`] (per-attempt gate counting). |
| 2423 | #[allow(dead_code)] // read only from the `#[cfg(test)]` gates |
| 2424 | pub(crate) fn reset_palette_leaf8_hits() { |
| 2425 | PALETTE_LEAF8_HITS.with(|c| c.set(0)); |
| 2426 | } |
| 2427 | |
| 2428 | thread_local! { |
| 2429 | /// lane-pal8 r1: NON-SKIP palette blocks on a superblock-level (64-level) |
| 2430 | /// HORZ/VERT or 1:4 strip ([`decode_block_rect64`]) -- i.e. a palette |
| 2431 | /// block that also carries a real, corner-cropped luma transform. |
| 2432 | static PALETTE_SB_STRIP_HITS: std::cell::Cell<usize> = const { std::cell::Cell::new(0) }; |
| 2433 | } |
| 2434 | |
| 2435 | /// Current value of [`PALETTE_SB_STRIP_HITS`]. |
| 2436 | #[allow(dead_code)] // read only from the `#[cfg(test)]` gates |
| 2437 | pub(crate) fn palette_sb_strip_hits() -> usize { |
| 2438 | PALETTE_SB_STRIP_HITS.with(|c| c.get()) |
| 2439 | } |
| 2440 | |
| 2441 | /// Zeroes [`PALETTE_SB_STRIP_HITS`] (per-attempt gate counting). |
| 2442 | #[allow(dead_code)] // read only from the `#[cfg(test)]` gates |
| 2443 | pub(crate) fn reset_palette_sb_strip_hits() { |
| 2444 | PALETTE_SB_STRIP_HITS.with(|c| c.set(0)); |
| 2445 | } |
| 2446 | |
| 2447 | // lane-t900 r22: palette blocks reconstructed inside an INTER frame (the |
| 2448 | // intra-in-inter path, `decode_inter_block`/`decode_inter_block8`) -- counted |
| 2449 | // apart from [`PALETTE_HITS`] so a witness gate can prove the INTER-frame |
| 2450 | // capability rather than the key frame's, which every screen stream also |
| 2451 | // carries. |
| 2452 | thread_local! { |
| 2453 | static INTRA_IN_INTER_PALETTE_Y_HITS: std::cell::Cell<usize> = const { std::cell::Cell::new(0) }; |
| 2454 | static INTRA_IN_INTER_PALETTE_UV_HITS: std::cell::Cell<usize> = const { std::cell::Cell::new(0) }; |
| 2455 | } |
| 2456 | |
| 2457 | // lane-t900 r23: as [`INTRA_IN_INTER_PALETTE_Y_HITS`], narrowed to the 8x8 |
| 2458 | // LEAF site ([`decode_inter_block8`]'s intra arm) -- the >=16 site fires the |
| 2459 | // shared counter too, so only this one proves a gate really reached the leaf |
| 2460 | // where the refusal used to sit. |
| 2461 | thread_local! { |
| 2462 | static INTER8_PALETTE_HITS: std::cell::Cell<(usize, usize)> = |
| 2463 | const { std::cell::Cell::new((0, 0)) }; |
| 2464 | } |
| 2465 | |
| 2466 | /// Current values of the 8x8-leaf intra-in-inter palette counters, `(y, uv)`. |
| 2467 | pub(crate) fn inter8_palette_hits() -> (usize, usize) { |
| 2468 | INTER8_PALETTE_HITS.with(std::cell::Cell::get) |
| 2469 | } |
| 2470 | |
| 2471 | /// Current values of the two intra-in-inter palette counters, `(y, uv)`. |
| 2472 | pub(crate) fn intra_in_inter_palette_hits() -> (usize, usize) { |
| 2473 | ( |
| 2474 | INTRA_IN_INTER_PALETTE_Y_HITS.with(|c| c.get()), |
| 2475 | INTRA_IN_INTER_PALETTE_UV_HITS.with(|c| c.get()), |
| 2476 | ) |
| 2477 | } |
| 2478 | |
| 2479 | // lane-hgkf r2: SKIPPED intra blocks whose `tx_depth` split the luma transform |
| 2480 | // and which are therefore predicted one transform unit at a time (a skipped |
| 2481 | // block codes no coefficients, so nothing else in the stream witnesses this |
| 2482 | // path -- only the pixels do). |
| 2483 | thread_local! { |
| 2484 | static SKIP_SPLIT_TX_HITS: std::cell::Cell<usize> = const { std::cell::Cell::new(0) }; |
| 2485 | } |
| 2486 | |
| 2487 | /// Current value of [`SKIP_SPLIT_TX_HITS`] (blocks, not units). |
| 2488 | pub fn skip_split_tx_hits() -> usize { |
| 2489 | SKIP_SPLIT_TX_HITS.with(|c| c.get()) |
| 2490 | } |
| 2491 | |
| 2492 | // lane-svt1: SKIPPED square blocks carrying a palette/intrabc prediction whose |
| 2493 | // `tx_depth` split the luma transform -- the arm that predicted DC off the |
| 2494 | // block edges before the fix. Like [`SKIP_SPLIT_TX_HITS`] only the pixels |
| 2495 | // witness this path, so a gate that wants it needs the counter. |
| 2496 | thread_local! { |
| 2497 | static SKIP_SPLIT_TX_OVERRIDE_HITS: std::cell::Cell<usize> = const { std::cell::Cell::new(0) }; |
| 2498 | } |
| 2499 | |
| 2500 | /// Current value of [`SKIP_SPLIT_TX_OVERRIDE_HITS`] (blocks). |
| 2501 | pub fn skip_split_tx_override_hits() -> usize { |
| 2502 | SKIP_SPLIT_TX_OVERRIDE_HITS.with(|c| c.get()) |
| 2503 | } |
| 2504 | |
| 2505 | /// Zeroes [`SKIP_SPLIT_TX_OVERRIDE_HITS`] before a gate's own decode attempt. |
| 2506 | pub fn reset_skip_split_tx_override_hits() { |
| 2507 | SKIP_SPLIT_TX_OVERRIDE_HITS.with(|c| c.set(0)); |
| 2508 | } |
| 2509 | |
| 2510 | // lane-svtinter: palette colour-index maps whose block is cut by the frame's |
| 2511 | // bottom/right mi edge, so spec 5.11.50's `onscreenWidth`/`onscreenHeight` |
| 2512 | // really are smaller than the block and FEWER symbols are read than the |
| 2513 | // block has cells (class map-read-past-frame-edge). |
| 2514 | thread_local! { |
| 2515 | static PALETTE_ONSCREEN_CUT_HITS: std::cell::Cell<usize> = const { std::cell::Cell::new(0) }; |
| 2516 | } |
| 2517 | |
| 2518 | /// Current value of [`PALETTE_ONSCREEN_CUT_HITS`] (colour-index maps). |
| 2519 | pub fn palette_onscreen_cut_hits() -> usize { |
| 2520 | PALETTE_ONSCREEN_CUT_HITS.with(|c| c.get()) |
| 2521 | } |
| 2522 | |
| 2523 | /// Zeroes [`PALETTE_ONSCREEN_CUT_HITS`] before a gate's own decode attempt. |
| 2524 | pub fn reset_palette_onscreen_cut_hits() { |
| 2525 | PALETTE_ONSCREEN_CUT_HITS.with(|c| c.set(0)); |
| 2526 | } |
| 2527 | |
| 2528 | /// Zeroes [`SKIP_SPLIT_TX_HITS`] before a gate's own decode attempt. |
| 2529 | pub fn reset_skip_split_tx_hits() { |
| 2530 | SKIP_SPLIT_TX_HITS.with(|c| c.set(0)); |
| 2531 | } |
| 2532 | |
| 2533 | // lane-intrainter r1: intra blocks inside an INTER frame whose `tx_depth` |
| 2534 | // symbol split their luma transform, bucketed by block side (8, 16, 32, 64) -- |
| 2535 | // the gate's proof that the lifted path actually ran, per size. |
| 2536 | thread_local! { |
| 2537 | static INTRA_IN_INTER_SPLIT_TX_HITS: std::cell::Cell<[usize; 4]> = |
| 2538 | const { std::cell::Cell::new([0; 4]) }; |
| 2539 | } |
| 2540 | |
| 2541 | /// Current value of [`INTRA_IN_INTER_SPLIT_TX_HITS`], indexed 8/16/32/64. |
| 2542 | #[allow(dead_code)] // read only from the `#[cfg(test)]` gates |
| 2543 | pub(crate) fn intra_in_inter_split_tx_hits() -> [usize; 4] { |
| 2544 | INTRA_IN_INTER_SPLIT_TX_HITS.with(std::cell::Cell::get) |
| 2545 | } |
| 2546 | |
| 2547 | /// A just-decoded palette-Y block's own predicted pixels |
| 2548 | /// (`side*side`, row-major, `colors[map[i]]`), consumed by |
| 2549 | /// [`PlaneBuf::reconstruct`]'s next call in place of `predict()` -- the |
| 2550 | /// same off-the-call-stack idiom as [`ENABLE_EDGE_FILTER`] (threading a |
| 2551 | /// per-block override through `read_plane`'s two dozen call sites is the |
| 2552 | /// alternative this avoids). Always empty again once that call consumes |
| 2553 | /// it (`.take()`), so a non-palette block's own `reconstruct` call never |
| 2554 | /// sees stale state. |
| 2555 | |
| 2556 | |
| 2557 | /// Arms the pending [`PALETTE_PRED`] override for the next `reconstruct` |
| 2558 | /// (`set`) / consumes it (`take`). Set and take MUST pair: a buffer armed at a |
| 2559 | /// site whose reconstruct never runs would be picked up by the NEXT block's |
| 2560 | /// reconstruct (class [[refusal-hides-a-defect]] / stale thread-local), so |
| 2561 | /// lane-lossless2: one transform unit's window on a block-sized palette |
| 2562 | /// prediction buffer. A unit reads its prediction at ITS OWN stride, so |
| 2563 | /// handing it the whole-block buffer makes every unit reconstruct the block's |
| 2564 | /// top-left corner (the luma split-transform paths already window; the chroma |
| 2565 | /// ones did not, which is invisible until a lossless frame codes a palette |
| 2566 | /// block as 4x4 chroma units). |
| 2567 | fn palette_window(buf: &[u16], stride: usize, ox: usize, oy: usize, w: usize, h: usize) -> Vec<u16> { |
| 2568 | (0..h) |
| 2569 | .flat_map(|r| buf[(oy + r) * stride + ox..][..w].iter().copied()) |
| 2570 | .collect() |
| 2571 | } |
| 2572 | |
| 2573 | /// `EC_DEBUG_PAL=1` traces every arm and every consumption with its call site. |
| 2574 | #[track_caller] |
| 2575 | fn set_palette_pred(buf: Vec<u16>, fctx: &crate::decode::FrameCtx) { |
| 2576 | if crate::envflags::env_flag!("EC_DEBUG_PAL") { |
| 2577 | let stale = fctx.palette_pred.with(|c| c.borrow().is_some()); |
| 2578 | eprintln!( |
| 2579 | "PALSET len={} stale={stale} at {}", |
| 2580 | buf.len(), |
| 2581 | std::panic::Location::caller() |
| 2582 | ); |
| 2583 | } |
| 2584 | fctx.palette_pred.with(|c| *c.borrow_mut() = Some(buf)); |
| 2585 | } |
| 2586 | |
| 2587 | #[track_caller] |
| 2588 | fn take_palette_pred(fctx: &crate::decode::FrameCtx) -> Option<Vec<u16>> { |
| 2589 | let got = fctx.palette_pred.with(|c| c.borrow_mut().take()); |
| 2590 | if crate::envflags::env_flag!("EC_DEBUG_PAL") { |
| 2591 | eprintln!( |
| 2592 | "PALTAKE len={:?} at {}", |
| 2593 | got.as_ref().map(Vec::len), |
| 2594 | std::panic::Location::caller() |
| 2595 | ); |
| 2596 | } |
| 2597 | got |
| 2598 | } |
| 2599 | |
| 2600 | /// The luma window a `UV_CFL_PRED` chroma block averages its AC signal over |
| 2601 | /// (lane-wave1 step (i)). Parse records the window; [`flush_recon`] reads the |
| 2602 | /// pixels, because at parse time this block's own luma is still queued. |
| 2603 | #[derive(Clone, Copy, Debug)] |
| 2604 | pub(crate) struct CflSrc { |
| 2605 | px: usize, |
| 2606 | py: usize, |
| 2607 | bw: usize, |
| 2608 | bh: usize, |
| 2609 | } |
| 2610 | |
| 2611 | fn cfl_src(px: usize, py: usize, side: usize) -> CflSrc { |
| 2612 | CflSrc { px, py, bw: side, bh: side } |
| 2613 | } |
| 2614 | |
| 2615 | fn cfl_src_rect(px: usize, py: usize, bw: usize, bh: usize) -> CflSrc { |
| 2616 | CflSrc { px, py, bw, bh } |
| 2617 | } |
| 2618 | |
| 2619 | /// lane-wave1 step (i): the pixel-writing half of block decoding, recorded |
| 2620 | /// instead of executed. Each variant is the resolved argument tuple of the |
| 2621 | /// `PlaneBuf::reconstruct*` call it replaces, so replaying the queue in push |
| 2622 | /// order performs exactly the same writes in exactly the same order -- |
| 2623 | /// entropy parsing no longer touches the frame's own planes (except the two |
| 2624 | /// sites named in [`flush_recon`]'s doc), which is what lets step (ii) run |
| 2625 | /// the replay on a superblock-row wavefront worker. |
| 2626 | /// The dequantization + inverse transform of one transform unit, kept as its |
| 2627 | /// INPUTS so that a recorded write carries the work instead of its result |
| 2628 | /// (lane-wave3): at `EC_AV1_RECON_THREADS > 1` the transform then runs on the |
| 2629 | /// recon worker, not on the parsing thread. `stride == 0` means the dense |
| 2630 | /// `w x h` residual; otherwise it is re-laid into the `stride x stride` grid |
| 2631 | /// `reconstruct_mc_rect` reads both of its buffers at. |
| 2632 | #[derive(Clone, Copy)] |
| 2633 | pub(crate) struct TxParams { |
| 2634 | pub(crate) w: usize, |
| 2635 | pub(crate) h: usize, |
| 2636 | pub(crate) bit_depth: u8, |
| 2637 | pub(crate) q_idx: i32, |
| 2638 | pub(crate) dc_delta: i32, |
| 2639 | pub(crate) ac_delta: i32, |
| 2640 | pub(crate) tx_type: TxType, |
| 2641 | /// lane-av1-qmatrix: this unit's inverse quantisation matrix, selected |
| 2642 | /// on the parse thread by [`block_iqmatrix`] and captured here so the |
| 2643 | /// recon worker scales identically without touching frame state. |
| 2644 | pub(crate) qm: Option<crate::qm::Iqm>, |
| 2645 | pub(crate) stride: usize, |
| 2646 | /// lane-lossless: this transform unit belongs to a lossless BLOCK (its |
| 2647 | /// segment's `xd->lossless[segment_id]` bit), so the residual comes off |
| 2648 | /// the 4x4 Walsh-Hadamard |
| 2649 | /// ([`crate::transform::dequant_and_inverse_wht4x4`]) instead of the |
| 2650 | /// DCT/ADST network (libaom `inv_txfm_add`, `av1/common/idct.c`). |
| 2651 | pub(crate) lossless: bool, |
| 2652 | } |
| 2653 | |
| 2654 | /// lane-av1mixloss: Walsh-Hadamard transform units actually reconstructed. |
| 2655 | /// The mixed-lossless witness's proof the per-segment lossless path really |
| 2656 | /// ran on a frame that is NOT `CodedLossless`. Atomic because the wavefront |
| 2657 | /// workers run this on their own threads. |
| 2658 | static LOSSLESS_WHT_UNITS: std::sync::atomic::AtomicUsize = |
| 2659 | std::sync::atomic::AtomicUsize::new(0); |
| 2660 | |
| 2661 | /// [`LOSSLESS_WHT_UNITS`] -- total over the process, read as a before/after |
| 2662 | /// delta around one decode. |
| 2663 | #[allow(dead_code)] // read only from the `#[cfg(test)]` gates |
| 2664 | pub(crate) fn lossless_wht_units() -> usize { |
| 2665 | LOSSLESS_WHT_UNITS.load(std::sync::atomic::Ordering::Relaxed) |
| 2666 | } |
| 2667 | |
| 2668 | impl TxParams { |
| 2669 | /// The one copy of the produce-the-residual code: the inline path calls it |
| 2670 | /// on the parse thread, the wavefront worker calls it on its own, so both |
| 2671 | /// are the same function on the same inputs and byte-identical. |
| 2672 | fn run(self, grid: &[i32]) -> Vec<i32> { |
| 2673 | let residual = if self.lossless { |
| 2674 | LOSSLESS_WHT_UNITS.fetch_add(1, std::sync::atomic::Ordering::Relaxed); |
| 2675 | debug_assert_eq!((self.w, self.h), (4, 4)); |
| 2676 | debug_assert_eq!(self.tx_type, TxType::DctDct); |
| 2677 | crate::transform::dequant_and_inverse_wht4x4( |
| 2678 | grid, self.bit_depth, self.q_idx, self.dc_delta, self.ac_delta, |
| 2679 | ) |
| 2680 | } else { |
| 2681 | dequant_and_inverse_typed_wh( |
| 2682 | grid, self.w, self.h, self.bit_depth, self.q_idx, self.dc_delta, self.ac_delta, |
| 2683 | self.tx_type, |
| 2684 | self.qm, |
| 2685 | ) |
| 2686 | }; |
| 2687 | // An all-zero grid transforms to the empty marker, and a strided copy |
| 2688 | // of zeros is still zeros: the marker passes straight through. |
| 2689 | if residual.is_empty() { |
| 2690 | return residual; |
| 2691 | } |
| 2692 | if self.stride == 0 { |
| 2693 | return residual; |
| 2694 | } |
| 2695 | // lane-av1-422sub8rect: `h` rows of `stride` -- a rect unit whose |
| 2696 | // height exceeds its (narrow) prediction stride needs the full |
| 2697 | // stride * h; every square caller has h == stride and is unchanged. |
| 2698 | let mut strided = vec![0i32; self.stride * self.h]; |
| 2699 | for row in 0..self.h { |
| 2700 | strided[row * self.stride..][..self.w] |
| 2701 | .copy_from_slice(&residual[row * self.w..][..self.w]); |
| 2702 | } |
| 2703 | strided |
| 2704 | } |
| 2705 | } |
| 2706 | |
| 2707 | /// A recorded write's residual: already transformed, or the coefficients and |
| 2708 | /// [`TxParams`] the worker transforms itself. |
| 2709 | pub(crate) enum Residual { |
| 2710 | /// An already-transformed residual in the arena, `(offset, len)`; a zero |
| 2711 | /// `len` is the all-zero marker [`dense_residual`] expands. |
| 2712 | Done(u32, u32), |
| 2713 | /// The coefficients live in the pushing tile's arena |
| 2714 | /// ([`crate::decode::FrameCtx::coeff_arena`]); this is their `(offset, |
| 2715 | /// len)` in it. |
| 2716 | Coeffs(TxParams, u32, u32), |
| 2717 | } |
| 2718 | |
| 2719 | impl Residual { |
| 2720 | /// The record for a transform unit whose coefficient grid is all zero: |
| 2721 | /// the empty marker, so neither the grid nor the residual is cloned and |
| 2722 | /// the worker never runs the transform (60% of the 4K segment's units). |
| 2723 | fn coeffs(tx: TxParams, grid: &[i32], fctx: &crate::decode::FrameCtx) -> Self { |
| 2724 | if is_zero_grid(grid) { |
| 2725 | return Self::Done(0, 0); |
| 2726 | } |
| 2727 | Self::Coeffs(tx, push_coeffs(grid, fctx), grid.len() as u32) |
| 2728 | } |
| 2729 | |
| 2730 | /// A residual the parse thread already transformed, kept in the arena |
| 2731 | /// instead of its own `Vec`. |
| 2732 | fn done(residual: &[i32], fctx: &crate::decode::FrameCtx) -> Self { |
| 2733 | // lane-blockmem: a skipped block hands over [`ZERO_RESIDUAL`] itself, |
| 2734 | // which is exactly the all-zero marker -- one pointer compare instead |
| 2735 | // of copying up to 64 KB of zeros into the arena per block. |
| 2736 | if std::ptr::eq(residual.as_ptr(), ZERO_RESIDUAL.as_ptr()) { |
| 2737 | return Self::Done(0, 0); |
| 2738 | } |
| 2739 | Self::Done(push_coeffs(residual, fctx), residual.len() as u32) |
| 2740 | } |
| 2741 | |
| 2742 | /// The residual to reconstruct with; `scratch` is where a transform run |
| 2743 | /// on this thread lands, so the caller owns that buffer. |
| 2744 | fn resolve<'a>(self, arena: &'a [i32], scratch: &'a mut Vec<i32>) -> &'a [i32] { |
| 2745 | match self { |
| 2746 | Self::Done(off, len) => &arena[off as usize..][..len as usize], |
| 2747 | Self::Coeffs(p, off, len) => { |
| 2748 | *scratch = p.run(&arena[off as usize..][..len as usize]); |
| 2749 | scratch |
| 2750 | } |
| 2751 | } |
| 2752 | } |
| 2753 | } |
| 2754 | |
| 2755 | /// Appends to the tile's coefficient arena, returning the offset the record |
| 2756 | /// carries; see [`crate::decode::FrameCtx::coeff_arena`]. |
| 2757 | fn push_coeffs(v: &[i32], fctx: &crate::decode::FrameCtx) -> u32 { |
| 2758 | fctx.coeff_arena.with(|a| { |
| 2759 | let mut a = a.borrow_mut(); |
| 2760 | let off = a.len(); |
| 2761 | a.extend_from_slice(v); |
| 2762 | off as u32 |
| 2763 | }) |
| 2764 | } |
| 2765 | |
| 2766 | /// [`push_coeffs`] for a recorded prediction. |
| 2767 | fn push_pred_samples(v: &[u16], fctx: &crate::decode::FrameCtx) -> u32 { |
| 2768 | fctx.pred_arena.with(|a| { |
| 2769 | let mut a = a.borrow_mut(); |
| 2770 | let off = a.len(); |
| 2771 | a.extend_from_slice(v); |
| 2772 | off as u32 |
| 2773 | }) |
| 2774 | } |
| 2775 | |
| 2776 | /// An all-zero residual, for the transform units that code no coefficient: |
| 2777 | /// [`crate::transform::dequant_and_inverse_typed_wh`] returns those EMPTY, |
| 2778 | /// and the reconstruction loops below read this instead of a per-unit |
| 2779 | /// `vec![0; w * h]`. Sized for the largest stride a recorded unit can carry |
| 2780 | /// (a 128-wide inter block's prediction stride). |
| 2781 | pub(crate) static ZERO_RESIDUAL: [i32; 128 * 128] = [0; 128 * 128]; |
| 2782 | |
| 2783 | /// lane-blockscratch: one plane's coefficient levels as the block-tail |
| 2784 | /// neighbour writes want them. A plane that coded nothing (a skipped block, a |
| 2785 | /// strip with no chroma, luma under a var-tx split whose units already wrote |
| 2786 | /// themselves) names the shared [`ZERO_RESIDUAL`] instead of allocating and |
| 2787 | /// zeroing up to 64 KB of its own per block. |
| 2788 | #[derive(Clone)] |
| 2789 | enum Grid { |
| 2790 | Zero(usize), |
| 2791 | Own(Vec<i32>), |
| 2792 | } |
| 2793 | |
| 2794 | /// lane-blockscratch: the mu-chunk chroma collector, allocated by the first |
| 2795 | /// unit that writes into it. Only a block wider or taller than 64 px codes its |
| 2796 | /// chroma per 64x64 chunk; every smaller block used to allocate and zero both |
| 2797 | /// collectors per block and then drop them untouched. |
| 2798 | fn mu_units(v: &mut Vec<i32>, n: usize) -> &mut Vec<i32> { |
| 2799 | if v.is_empty() { |
| 2800 | *v = vec![0i32; n]; |
| 2801 | } |
| 2802 | v |
| 2803 | } |
| 2804 | |
| 2805 | impl std::ops::Deref for Grid { |
| 2806 | type Target = [i32]; |
| 2807 | fn deref(&self) -> &[i32] { |
| 2808 | match self { |
| 2809 | Grid::Zero(n) => &ZERO_RESIDUAL[..*n], |
| 2810 | Grid::Own(v) => v, |
| 2811 | } |
| 2812 | } |
| 2813 | } |
| 2814 | |
| 2815 | /// Whether a coefficient grid codes nothing: a [`Grid::Zero`] unit IS the |
| 2816 | /// shared [`ZERO_RESIDUAL`], so one pointer compare answers the 60% of |
| 2817 | /// transform units that code no coefficient without the scan (lane-coefgrid). |
| 2818 | pub(crate) fn is_zero_grid(grid: &[i32]) -> bool { |
| 2819 | std::ptr::eq(grid.as_ptr(), ZERO_RESIDUAL.as_ptr()) || grid.iter().all(|&c| c == 0) |
| 2820 | } |
| 2821 | |
| 2822 | /// `residual`, or `n` zeros when it is the empty all-zero marker. |
| 2823 | fn dense_residual(residual: &[i32], n: usize) -> &[i32] { |
| 2824 | if residual.is_empty() { &ZERO_RESIDUAL[..n] } else { residual } |
| 2825 | } |
| 2826 | |
| 2827 | /// Where a recorded motion-compensated write reads its prediction from. |
| 2828 | /// |
| 2829 | /// lane-wave4: a block's whole-block prediction (mc + warp + interintra) is |
| 2830 | /// the parse thread's single biggest reconstruction cost, so at |
| 2831 | /// `EC_AV1_RECON_THREADS > 1` it is not built at parse at all: the block |
| 2832 | /// pushes a [`ReconOp::Pred`] carrying the build as a closure, the worker |
| 2833 | /// runs it into its own three-plane slot, and every transform unit of that |
| 2834 | /// block then reads its window out of that slot ([`Pred::Cur`]). |
| 2835 | #[derive(Clone, Copy)] |
| 2836 | pub(crate) enum Pred<'a> { |
| 2837 | /// A prediction the parse thread already built, at its own stride |
| 2838 | /// (`stride == 0` means "the reader's own `stride` argument"). |
| 2839 | Inline(&'a [u16], usize), |
| 2840 | /// The current block's deferred prediction: plane, its stride, and this |
| 2841 | /// unit's offset into it. |
| 2842 | Cur { plane: usize, stride: usize, off: usize }, |
| 2843 | } |
| 2844 | |
| 2845 | impl<'a> Pred<'a> { |
| 2846 | /// The same prediction read at an explicit stride. |
| 2847 | fn view(self, stride: usize) -> Self { |
| 2848 | match self { |
| 2849 | Self::Inline(b, _) => Self::Inline(b, stride), |
| 2850 | Self::Cur { plane, off, .. } => Self::Cur { plane, stride, off }, |
| 2851 | } |
| 2852 | } |
| 2853 | /// The materialised buffer and the stride to read it at (`stride` is the |
| 2854 | /// reader's own when this prediction was built dense); `None` for a |
| 2855 | /// deferred prediction, which only a recorded op can carry. |
| 2856 | fn inline(self, stride: usize) -> Option<(&'a [u16], usize)> { |
| 2857 | match self { |
| 2858 | Self::Inline(b, s) => Some((b, if s == 0 { stride } else { s })), |
| 2859 | Self::Cur { .. } => None, |
| 2860 | } |
| 2861 | } |
| 2862 | /// This prediction's window starting `n` samples in. |
| 2863 | fn offset(self, n: usize) -> Self { |
| 2864 | match self { |
| 2865 | Self::Inline(b, s) => Self::Inline(&b[n..], s), |
| 2866 | Self::Cur { plane, stride, off } => Self::Cur { plane, stride, off: off + n }, |
| 2867 | } |
| 2868 | } |
| 2869 | } |
| 2870 | |
| 2871 | /// [`Pred`], resolved into what a recorded op carries. |
| 2872 | pub(crate) enum PredSrc { |
| 2873 | /// A prediction window in the tile's prediction arena, `(offset, len)`. |
| 2874 | Owned(u32, u32), |
| 2875 | /// A window of the block prediction: its plane, its offset, and the |
| 2876 | /// stride it is read at -- which is the block's, not necessarily the |
| 2877 | /// op's (a transform unit's residual stays dense, so the worker takes |
| 2878 | /// its window out of the block prediction itself). |
| 2879 | Cur { plane: usize, off: usize, src: usize }, |
| 2880 | } |
| 2881 | |
| 2882 | /// One block's whole-block prediction build, deferred to a recon worker. |
| 2883 | /// |
| 2884 | /// The closure borrows the tile's reference pictures and its own frame-level |
| 2885 | /// tables; [`WaveGuard`] joins every worker before the tile decoder returns, |
| 2886 | /// so those borrows outlive it -- the [`ReconPlanes`] trade-off, stated once |
| 2887 | /// here for the closure's `'static`/`Send` laundering in [`push_pred`]. |
| 2888 | pub(crate) struct PredBuild( |
| 2889 | Box< |
| 2890 | dyn FnOnce( |
| 2891 | &mut PlaneBuf<'static>, |
| 2892 | &mut PlaneBuf<'static>, |
| 2893 | &mut PlaneBuf<'static>, |
| 2894 | &crate::decode::FrameCtx, |
| 2895 | // lane-blockmem: the previous block's three prediction buffers, |
| 2896 | // refilled in place -- the build used to return three fresh |
| 2897 | // `Vec`s per block and drop these. |
| 2898 | &mut [Vec<u16>; 3], |
| 2899 | ), |
| 2900 | >, |
| 2901 | ); |
| 2902 | |
| 2903 | // SAFETY: see [`PredBuild`] -- the captured borrows are read-only reference |
| 2904 | // pictures and frame tables, and the closure runs on exactly one worker. |
| 2905 | #[allow(unsafe_code)] |
| 2906 | unsafe impl Send for PredBuild {} |
| 2907 | |
| 2908 | /// `true` when this tile's writes are recorded for the wavefront instead of |
| 2909 | /// performed at the push site, i.e. when a prediction may be deferred. |
| 2910 | fn recon_deferred(fctx: &crate::decode::FrameCtx) -> bool { |
| 2911 | fctx.recon_planes.with(std::cell::Cell::get).is_none() |
| 2912 | } |
| 2913 | |
| 2914 | /// The previous block's prediction buffer, cleared and zero-filled to `n` |
| 2915 | /// (lane-blockmem): the same bytes the `vec![0u16; n]` it replaces produced, |
| 2916 | /// without the per-block allocation and free. |
| 2917 | fn refill(mut v: Vec<u16>, n: usize) -> Vec<u16> { |
| 2918 | // A buffer that has to grow anyway is cheaper allocated zeroed (`calloc`) |
| 2919 | // than resized element by element -- which is every block on the inline |
| 2920 | // path, where there is no previous buffer to reuse. |
| 2921 | if v.capacity() < n { |
| 2922 | return vec![0u16; n]; |
| 2923 | } |
| 2924 | v.clear(); |
| 2925 | v.resize(n, 0); |
| 2926 | v |
| 2927 | } |
| 2928 | |
| 2929 | /// Records one block's prediction build for the worker that reconstructs it. |
| 2930 | #[allow(unsafe_code)] |
| 2931 | fn push_pred<'a>( |
| 2932 | fctx: &crate::decode::FrameCtx, |
| 2933 | f: impl FnOnce( |
| 2934 | &mut PlaneBuf<'static>, |
| 2935 | &mut PlaneBuf<'static>, |
| 2936 | &mut PlaneBuf<'static>, |
| 2937 | &crate::decode::FrameCtx, |
| 2938 | &mut [Vec<u16>; 3], |
| 2939 | ) + 'a, |
| 2940 | ) { |
| 2941 | type Dyn<'x> = dyn FnOnce( |
| 2942 | &mut PlaneBuf<'static>, |
| 2943 | &mut PlaneBuf<'static>, |
| 2944 | &mut PlaneBuf<'static>, |
| 2945 | &crate::decode::FrameCtx, |
| 2946 | &mut [Vec<u16>; 3], |
| 2947 | ) + 'x; |
| 2948 | let boxed: Box<Dyn<'a>> = Box::new(f); |
| 2949 | // SAFETY: see [`PredBuild`] -- every worker is joined by [`WaveGuard`] |
| 2950 | // before the borrows the closure holds go out of scope. |
| 2951 | let boxed: Box<Dyn<'static>> = unsafe { std::mem::transmute(boxed) }; |
| 2952 | push_op(fctx, ReconOp::Pred(PredBuild(boxed))); |
| 2953 | } |
| 2954 | |
| 2955 | /// One superblock's recorded writes plus the two arenas they index -- the |
| 2956 | /// unit a wavefront worker takes off the queue (lane-recoparse). |
| 2957 | pub(crate) struct ReconJob { |
| 2958 | ops: Vec<ReconOp>, |
| 2959 | coeffs: Vec<i32>, |
| 2960 | preds: Vec<u16>, |
| 2961 | } |
| 2962 | |
| 2963 | pub(crate) enum ReconOp { |
| 2964 | /// lane-wave4: build this block's prediction into the worker's slot. |
| 2965 | Pred(PredBuild), |
| 2966 | Intra { |
| 2967 | plane: usize, |
| 2968 | x: usize, |
| 2969 | y: usize, |
| 2970 | bw: usize, |
| 2971 | bh: usize, |
| 2972 | /// `true` when the parse site called `reconstruct_rect` (whose |
| 2973 | /// `edges_rect` differs from `edges` even at `bw == bh`). |
| 2974 | rect: bool, |
| 2975 | mode: usize, |
| 2976 | angle_delta: i32, |
| 2977 | reach: Reach, |
| 2978 | residual: Residual, |
| 2979 | cfl: Option<(i32, CflSrc)>, |
| 2980 | filter_intra: Option<usize>, |
| 2981 | smooth_neighbor: bool, |
| 2982 | /// The palette/intrabc prediction override, taken off |
| 2983 | /// [`FrameCtx::palette_pred`] at PUSH time (parse order), never read |
| 2984 | /// from that slot by the replay. |
| 2985 | pred: Option<Vec<u16>>, |
| 2986 | }, |
| 2987 | Mc { |
| 2988 | plane: usize, |
| 2989 | x: usize, |
| 2990 | y: usize, |
| 2991 | stride: usize, |
| 2992 | w: usize, |
| 2993 | h: usize, |
| 2994 | prediction: PredSrc, |
| 2995 | residual: Residual, |
| 2996 | }, |
| 2997 | } |
| 2998 | |
| 2999 | /// The three planes of the tile currently being decoded, as raw pointers. |
| 3000 | /// |
| 3001 | /// corner-cut: at one reconstruction thread a recorded write must cost |
| 3002 | /// nothing -- deferring it to the end of the superblock is +1.2% |
| 3003 | /// instructions and +4.5% wall on 4K, because the residual is written at |
| 3004 | /// parse and read back cache-cold at replay. So the recorded op is performed |
| 3005 | /// at the push site instead, through the pointers the tile decoder registers |
| 3006 | /// once ([`ReconInline`]), which is the same thread, the same order and the |
| 3007 | /// same writes as before lane-wave1 -- but the `&mut PlaneBuf` the parse |
| 3008 | /// stack still holds aliases them, exactly the [`crate::par::Shared`] |
| 3009 | /// trade-off (Miri's stacked borrows would flag both). The upgrade path is |
| 3010 | /// threading the three planes down every `read_plane`/`read_inter_plane` |
| 3011 | /// call site (~40) so the push site owns the borrow it writes through. |
| 3012 | #[derive(Clone, Copy)] |
| 3013 | pub(crate) struct ReconPlanes( |
| 3014 | *mut PlaneBuf<'static>, |
| 3015 | *mut PlaneBuf<'static>, |
| 3016 | *mut PlaneBuf<'static>, |
| 3017 | ); |
| 3018 | |
| 3019 | // SAFETY: a `FrameCtx` sent to another thread (a frame worker, or |
| 3020 | // [`filter_ctx_copy`]) is always one whose guard is not installed, so the |
| 3021 | // value travelling is `None`; the pointers never leave the tile decoder's |
| 3022 | // own thread. |
| 3023 | #[allow(unsafe_code)] |
| 3024 | unsafe impl Send for ReconPlanes {} |
| 3025 | // SAFETY: the wavefront's workers share one `ReconPlanes` and write provably |
| 3026 | // disjoint superblocks of it (see [`WaveState`]) -- the [`crate::par::Shared`] |
| 3027 | // contract, one superblock per band instead of one row band. |
| 3028 | #[allow(unsafe_code)] |
| 3029 | unsafe impl Sync for ReconPlanes {} |
| 3030 | |
| 3031 | /// Registers the tile's planes for inline reconstruction and unregisters them |
| 3032 | /// on every exit from the tile decoder, including the `?` ones -- a stale |
| 3033 | /// pointer here would outlive the planes, since [`FrameCtx`] outlives the |
| 3034 | /// tile. |
| 3035 | pub(crate) struct ReconInline<'a>(&'a crate::decode::FrameCtx); |
| 3036 | |
| 3037 | #[allow(unsafe_code)] |
| 3038 | impl<'a> ReconInline<'a> { |
| 3039 | /// Installs the guard for this tile when reconstruction is inline |
| 3040 | /// (`EC_AV1_RECON_THREADS == 1`, the default); `None` hands every write |
| 3041 | /// to the queue instead, for the wavefront workers to replay. |
| 3042 | fn install( |
| 3043 | fctx: &'a crate::decode::FrameCtx, |
| 3044 | y: &mut PlaneBuf<'static>, |
| 3045 | u: &mut PlaneBuf<'static>, |
| 3046 | v: &mut PlaneBuf<'static>, |
| 3047 | ) -> Option<Self> { |
| 3048 | if crate::par::recon_threads() != 1 { |
| 3049 | return None; |
| 3050 | } |
| 3051 | // SAFETY: the planes are locals of the tile decoder and outlive the |
| 3052 | // guard, which is dropped on every exit from it (including `?`); see |
| 3053 | // [`ReconPlanes`] for the aliasing trade-off. |
| 3054 | let planes = ReconPlanes( |
| 3055 | std::ptr::from_mut(y), |
| 3056 | std::ptr::from_mut(u), |
| 3057 | std::ptr::from_mut(v), |
| 3058 | ); |
| 3059 | fctx.recon_planes.with(|c| c.set(Some(planes))); |
| 3060 | Some(Self(fctx)) |
| 3061 | } |
| 3062 | } |
| 3063 | |
| 3064 | impl Drop for ReconInline<'_> { |
| 3065 | fn drop(&mut self) { |
| 3066 | self.0.recon_planes.with(|c| c.set(None)); |
| 3067 | } |
| 3068 | } |
| 3069 | |
| 3070 | #[allow(unsafe_code)] |
| 3071 | fn push_op(fctx: &crate::decode::FrameCtx, op: ReconOp) { |
| 3072 | if let Some(ReconPlanes(y, u, v)) = fctx.recon_planes.with(std::cell::Cell::get) { |
| 3073 | // SAFETY: see [`ReconPlanes`] -- one thread, and the write is the one |
| 3074 | // the parse site would have performed itself before lane-wave1. |
| 3075 | // The inline path never records coefficients (the transform runs at |
| 3076 | // the push site), so the arena is not read here. |
| 3077 | unsafe { |
| 3078 | exec_op(op, &mut *y, &mut *u, &mut *v, &mut Default::default(), &[], &[], fctx) |
| 3079 | }; |
| 3080 | return; |
| 3081 | } |
| 3082 | fctx.recon_ops.with(|c| c.borrow_mut().push(op)); |
| 3083 | } |
| 3084 | |
| 3085 | /// lane-wave1 step (ii): the superblock-row wavefront a tile's recorded |
| 3086 | /// writes are reconstructed on when `EC_AV1_RECON_THREADS > 1`. |
| 3087 | /// |
| 3088 | /// The parser keeps parsing and never touches the planes; each superblock's |
| 3089 | /// drained `Vec<ReconOp>` becomes one job, keyed by the superblock's own |
| 3090 | /// `(row, column)` inside the tile (128x128 superblocks are one job for all |
| 3091 | /// four of their 64x64 visits, since a 128 block writes across quadrants). |
| 3092 | /// A worker may start job `(r, c)` once `(r - 1, min(c + 1, last_col))` and |
| 3093 | /// `(r, c - 1)` are complete -- the classic wavefront rule, which covers |
| 3094 | /// every pixel an intra predictor can read (its edges reach one block right |
| 3095 | /// along the row above and one block down the column to the left, both |
| 3096 | /// inside those two neighbours) and every CfL AC read (this block's own |
| 3097 | /// luma, written by this same job). Jobs are handed out in raster order, so |
| 3098 | /// the oldest outstanding one always has its dependencies met: no deadlock. |
| 3099 | /// |
| 3100 | /// Gate counters incremented inside `reconstruct*`/`intra`/`mc` are |
| 3101 | /// `thread_local!` and miss every worker, which is why the default is 1. |
| 3102 | pub(crate) struct WaveState { |
| 3103 | q: std::sync::Mutex<WaveQueue>, |
| 3104 | cv: std::sync::Condvar, |
| 3105 | planes: ReconPlanes, |
| 3106 | /// The [`FrameCtx`] fields reconstruction reads (`sample_max` is |
| 3107 | /// `bit_depth`'s; everything else a `reconstruct*` needs is in the op). |
| 3108 | bit_depth: u8, |
| 3109 | enable_edge_filter: bool, |
| 3110 | /// lane-av1wavefront: the sequence's chroma subsampling. The replay-side |
| 3111 | /// readers -- the deferred chroma builds' `mv_to_q4(.., ss_x(fctx))` |
| 3112 | /// positions and `exec_intra`'s CfL AC signal -- must see the FRAME's |
| 3113 | /// pair, not [`FrameCtx::new`]'s 4:2:0 default: on a 4:4:4 stream the |
| 3114 | /// default halved every worker's chroma reference position and lost the |
| 3115 | /// chroma planes (the 444_sb128rect witness, U 17504 / V 3 at PREFILT on |
| 3116 | /// frame 1) while every 4:2:0 gate stream stayed accidentally exact. |
| 3117 | subsampling: (u8, u8), |
| 3118 | last_col: usize, |
| 3119 | } |
| 3120 | |
| 3121 | struct WaveQueue { |
| 3122 | /// Queued jobs per superblock ROW, in column order (lane-wave5). Only a |
| 3123 | /// row's front job can ever be runnable -- a job waits on its own left |
| 3124 | /// neighbour -- so a worker takes the lowest row whose front job's |
| 3125 | /// dependencies are met. Popping the raster-oldest job instead (what |
| 3126 | /// this replaced) put every worker on the SAME row, where the wavefront |
| 3127 | /// rule serialises them, which is why 4 workers ran the tile at 1.2x. |
| 3128 | rows: Vec<std::collections::VecDeque<(usize, ReconJob)>>, |
| 3129 | /// Jobs sitting in `rows`, so a worker knows the queue is empty without |
| 3130 | /// walking it. |
| 3131 | queued: usize, |
| 3132 | /// Per superblock row, the last column reconstructed (-1 = none yet). |
| 3133 | done: Vec<isize>, |
| 3134 | /// Per superblock row, the last column PARSED (submitted, whether or not |
| 3135 | /// it recorded any write) and the last column actually pushed as a job |
| 3136 | /// (-1 = none). A row is fully reconstructed once it has been parsed to |
| 3137 | /// `last_col` and `done` has caught up with `last_push` -- lane-pipefilt |
| 3138 | /// reads that to release the row's filters (see [`wave_rows_complete`]). |
| 3139 | last_sub: Vec<isize>, |
| 3140 | last_push: Vec<isize>, |
| 3141 | /// Jobs popped but not finished -- what [`wave_barrier`] waits out. |
| 3142 | active: usize, |
| 3143 | closed: bool, |
| 3144 | /// lane-sballoc: the three per-superblock buffers a finished job hands |
| 3145 | /// back, so [`wave_submit`] takes a warm allocation (capacity kept, no |
| 3146 | /// first-touch page faults) instead of the `Vec::with_capacity` it used |
| 3147 | /// to make per superblock -- 3 allocations and 3 frees per superblock, |
| 3148 | /// 4680 per 4K frame. Bounded at `recon_threads + 2` sets, and it lives |
| 3149 | /// inside the queue's own mutex, which both sides already take. |
| 3150 | free: Vec<JobBufs>, |
| 3151 | free_cap: usize, |
| 3152 | } |
| 3153 | |
| 3154 | /// One recycled set of [`ReconJob`] buffers (lane-sballoc). |
| 3155 | type JobBufs = (Vec<ReconOp>, Vec<i32>, Vec<u16>); |
| 3156 | |
| 3157 | impl WaveQueue { |
| 3158 | /// The lowest-row queued job whose wavefront dependencies are satisfied, |
| 3159 | /// or `None` (with the blocking condition counted when instrumented). |
| 3160 | fn take_runnable( |
| 3161 | &mut self, |
| 3162 | last_col: usize, |
| 3163 | stats: bool, |
| 3164 | ) -> Option<(usize, usize, ReconJob)> { |
| 3165 | let mut blocked: Option<bool> = None; |
| 3166 | for r in 0..self.rows.len() { |
| 3167 | let Some(front) = self.rows[r].front() else { |
| 3168 | continue; |
| 3169 | }; |
| 3170 | let c = front.0; |
| 3171 | let above = r == 0 || self.done[r - 1] >= (c + 1).min(last_col) as isize; |
| 3172 | let left = c == 0 || self.done[r] >= c as isize - 1; |
| 3173 | if above && left { |
| 3174 | let (c, job) = self.rows[r].pop_front().expect("front checked above"); |
| 3175 | self.queued -= 1; |
| 3176 | return Some((r, c, job)); |
| 3177 | } |
| 3178 | if blocked.is_none() { |
| 3179 | blocked = Some(above); |
| 3180 | } |
| 3181 | } |
| 3182 | if stats { |
| 3183 | match blocked { |
| 3184 | Some(true) => WS_BLOCK_LEFT.fetch_add(1, Relaxed), |
| 3185 | Some(false) => WS_BLOCK_ABOVE.fetch_add(1, Relaxed), |
| 3186 | None => 0, |
| 3187 | }; |
| 3188 | } |
| 3189 | None |
| 3190 | } |
| 3191 | } |
| 3192 | |
| 3193 | /// lane-wave5 instrumentation, on `EC_AV1_WAVE_STATS=1`: where a recon |
| 3194 | /// worker's time goes (busy replaying, waiting for a dependency, idle on an |
| 3195 | /// empty queue), how far ahead the parse thread is, and how often the two |
| 3196 | /// parse-time pixel reads barrier the wavefront. Cumulative for the process, |
| 3197 | /// dumped by every [`WaveGuard`] drop, so the LAST line of a decode is the |
| 3198 | /// whole run. |
| 3199 | const WS_SLOTS: usize = 16; |
| 3200 | use std::sync::atomic::AtomicU64; |
| 3201 | use std::sync::atomic::Ordering::Relaxed; |
| 3202 | static WS_JOBS: [AtomicU64; WS_SLOTS] = [const { AtomicU64::new(0) }; WS_SLOTS]; |
| 3203 | static WS_OPS: [AtomicU64; WS_SLOTS] = [const { AtomicU64::new(0) }; WS_SLOTS]; |
| 3204 | static WS_BUSY: [AtomicU64; WS_SLOTS] = [const { AtomicU64::new(0) }; WS_SLOTS]; |
| 3205 | static WS_WAIT: [AtomicU64; WS_SLOTS] = [const { AtomicU64::new(0) }; WS_SLOTS]; |
| 3206 | static WS_BLOCK_ABOVE: AtomicU64 = AtomicU64::new(0); |
| 3207 | static WS_BLOCK_LEFT: AtomicU64 = AtomicU64::new(0); |
| 3208 | /// Parse-thread time between two submits, i.e. everything parse still does |
| 3209 | /// itself (symbol decode plus the predictions not deferred). |
| 3210 | static WS_PARSE: AtomicU64 = AtomicU64::new(0); |
| 3211 | /// Parse-thread time spent waiting for the wavefront: at [`wave_barrier`] |
| 3212 | /// (the intrabc/interintra reads) and at the tile's end. |
| 3213 | static WS_BARRIER: AtomicU64 = AtomicU64::new(0); |
| 3214 | static WS_BARRIERS: AtomicU64 = AtomicU64::new(0); |
| 3215 | static WS_DRAIN: AtomicU64 = AtomicU64::new(0); |
| 3216 | static WS_TILES: AtomicU64 = AtomicU64::new(0); |
| 3217 | |
| 3218 | fn wave_stats() -> bool { |
| 3219 | static ON: std::sync::atomic::AtomicU8 = std::sync::atomic::AtomicU8::new(0); |
| 3220 | match ON.load(Relaxed) { |
| 3221 | 0 => { |
| 3222 | let on = crate::envflags::var("EC_AV1_WAVE_STATS") |
| 3223 | .map(|v| v.trim() == "1") |
| 3224 | .unwrap_or(false); |
| 3225 | ON.store(1 + u8::from(on), Relaxed); |
| 3226 | on |
| 3227 | } |
| 3228 | n => n == 2, |
| 3229 | } |
| 3230 | } |
| 3231 | |
| 3232 | fn wave_stats_dump() { |
| 3233 | let ms = |n: u64| n as f64 / 1e6; |
| 3234 | let mut s = format!("wave_stats tiles={}", WS_TILES.load(Relaxed)); |
| 3235 | for i in 0..WS_SLOTS { |
| 3236 | let jobs = WS_JOBS[i].load(Relaxed); |
| 3237 | if jobs == 0 { |
| 3238 | continue; |
| 3239 | } |
| 3240 | s += &format!( |
| 3241 | " | w{i} jobs={jobs} ops/job={:.1} busy={:.0}ms wait={:.0}ms wait/job={:.0}us", |
| 3242 | WS_OPS[i].load(Relaxed) as f64 / jobs as f64, |
| 3243 | ms(WS_BUSY[i].load(Relaxed)), |
| 3244 | ms(WS_WAIT[i].load(Relaxed)), |
| 3245 | WS_WAIT[i].load(Relaxed) as f64 / jobs as f64 / 1e3, |
| 3246 | ); |
| 3247 | } |
| 3248 | s += &format!( |
| 3249 | " | pipe={}/{}", |
| 3250 | PIPE_FRAMES.load(Relaxed), |
| 3251 | PIPE_FRAMES.load(Relaxed) + PIPE_WHOLE.load(Relaxed), |
| 3252 | ); |
| 3253 | s += &format!( |
| 3254 | " | parse={:.0}ms barrier={:.0}ms/{} drain={:.0}ms block_above={} block_left={}", |
| 3255 | ms(WS_PARSE.load(Relaxed)), |
| 3256 | ms(WS_BARRIER.load(Relaxed)), |
| 3257 | WS_BARRIERS.load(Relaxed), |
| 3258 | ms(WS_DRAIN.load(Relaxed)), |
| 3259 | WS_BLOCK_ABOVE.load(Relaxed), |
| 3260 | WS_BLOCK_LEFT.load(Relaxed), |
| 3261 | ); |
| 3262 | eprintln!("{s}"); |
| 3263 | } |
| 3264 | |
| 3265 | #[allow(unsafe_code)] |
| 3266 | fn wave_worker(st: &WaveState, idx: usize) { |
| 3267 | let fctx = FrameCtx::new(); |
| 3268 | fctx.bit_depth.set(st.bit_depth); |
| 3269 | fctx.enable_edge_filter.set(st.enable_edge_filter); |
| 3270 | let (sx, sy) = st.subsampling; |
| 3271 | fctx.subsampling_x.set(sx); |
| 3272 | fctx.subsampling_y.set(sy); |
| 3273 | let stats = wave_stats(); |
| 3274 | let slot = idx.min(WS_SLOTS - 1); |
| 3275 | loop { |
| 3276 | let t0 = std::time::Instant::now(); |
| 3277 | let mut q = st.q.lock().unwrap(); |
| 3278 | let (r, c, job) = loop { |
| 3279 | if let Some(job) = q.take_runnable(st.last_col, stats) { |
| 3280 | break job; |
| 3281 | } |
| 3282 | if q.closed && q.queued == 0 { |
| 3283 | return; |
| 3284 | } |
| 3285 | q = st.cv.wait(q).unwrap(); |
| 3286 | }; |
| 3287 | q.active += 1; |
| 3288 | drop(q); |
| 3289 | if stats { |
| 3290 | WS_WAIT[slot].fetch_add(t0.elapsed().as_nanos() as u64, Relaxed); |
| 3291 | WS_JOBS[slot].fetch_add(1, Relaxed); |
| 3292 | WS_OPS[slot].fetch_add(job.ops.len() as u64, Relaxed); |
| 3293 | } |
| 3294 | let t1 = std::time::Instant::now(); |
| 3295 | let ReconPlanes(y, u, v) = st.planes; |
| 3296 | // SAFETY: this job writes only superblock (r, c)'s own pixels, and |
| 3297 | // every superblock that could have written or be reading them is |
| 3298 | // complete by the wavefront rule above. |
| 3299 | let mut cur: [Vec<u16>; 3] = Default::default(); |
| 3300 | let ReconJob { mut ops, mut coeffs, mut preds } = job; |
| 3301 | unsafe { |
| 3302 | for op in ops.drain(..) { |
| 3303 | exec_op(op, &mut *y, &mut *u, &mut *v, &mut cur, &coeffs, &preds, &fctx); |
| 3304 | } |
| 3305 | } |
| 3306 | if stats { |
| 3307 | WS_BUSY[slot].fetch_add(t1.elapsed().as_nanos() as u64, Relaxed); |
| 3308 | } |
| 3309 | coeffs.clear(); |
| 3310 | preds.clear(); |
| 3311 | let mut q = st.q.lock().unwrap(); |
| 3312 | // lane-sballoc: hand the three buffers back for the next submit. |
| 3313 | if q.free.len() < q.free_cap { |
| 3314 | q.free.push((ops, coeffs, preds)); |
| 3315 | } |
| 3316 | q.done[r] = c as isize; |
| 3317 | q.active -= 1; |
| 3318 | st.cv.notify_all(); |
| 3319 | } |
| 3320 | } |
| 3321 | |
| 3322 | /// Installs the wavefront for one tile and joins its workers on every exit |
| 3323 | /// from the tile decoder, including the `?` ones -- the jobs hold raw |
| 3324 | /// pointers into the tile's planes, so no worker may outlive them. |
| 3325 | /// (lane-pool1: the workers come from this thread's persistent pool, so a |
| 3326 | /// tile costs a batch submit rather than `EC_AV1_RECON_THREADS` fresh threads |
| 3327 | /// with fresh scratch.) |
| 3328 | pub(crate) struct WaveGuard<'a>( |
| 3329 | &'a crate::decode::FrameCtx, |
| 3330 | Option<crate::par::Batch<'static>>, |
| 3331 | ); |
| 3332 | |
| 3333 | #[allow(unsafe_code)] |
| 3334 | impl<'a> WaveGuard<'a> { |
| 3335 | fn install( |
| 3336 | fctx: &'a crate::decode::FrameCtx, |
| 3337 | y: &mut PlaneBuf<'static>, |
| 3338 | u: &mut PlaneBuf<'static>, |
| 3339 | v: &mut PlaneBuf<'static>, |
| 3340 | rows: usize, |
| 3341 | cols: usize, |
| 3342 | ) -> Option<Self> { |
| 3343 | let threads = crate::par::recon_threads(); |
| 3344 | if threads == 1 || rows == 0 || cols == 0 { |
| 3345 | return None; |
| 3346 | } |
| 3347 | let st = std::sync::Arc::new(WaveState { |
| 3348 | q: std::sync::Mutex::new(WaveQueue { |
| 3349 | rows: (0..rows).map(|_| std::collections::VecDeque::new()).collect(), |
| 3350 | queued: 0, |
| 3351 | done: vec![-1; rows], |
| 3352 | last_sub: vec![-1; rows], |
| 3353 | last_push: vec![-1; rows], |
| 3354 | active: 0, |
| 3355 | closed: false, |
| 3356 | free: Vec::new(), |
| 3357 | free_cap: threads + 2, |
| 3358 | }), |
| 3359 | cv: std::sync::Condvar::new(), |
| 3360 | // SAFETY: the planes are locals of the tile decoder and outlive |
| 3361 | // this guard, which joins every worker in `drop`. |
| 3362 | planes: ReconPlanes( |
| 3363 | std::ptr::from_mut(y), |
| 3364 | std::ptr::from_mut(u), |
| 3365 | std::ptr::from_mut(v), |
| 3366 | ), |
| 3367 | bit_depth: fctx.bit_depth.with(std::cell::Cell::get), |
| 3368 | enable_edge_filter: fctx.enable_edge_filter.with(std::cell::Cell::get), |
| 3369 | subsampling: ( |
| 3370 | fctx.subsampling_x.with(std::cell::Cell::get), |
| 3371 | fctx.subsampling_y.with(std::cell::Cell::get), |
| 3372 | ), |
| 3373 | last_col: cols - 1, |
| 3374 | }); |
| 3375 | let batch = crate::par::Batch::new("ec-av1-recon"); |
| 3376 | for idx in 0..threads { |
| 3377 | let st = std::sync::Arc::clone(&st); |
| 3378 | batch.submit(move || wave_worker(&st, idx)); |
| 3379 | } |
| 3380 | fctx.wave.with(|w| *w.borrow_mut() = Some(st)); |
| 3381 | fctx.wave_mark.with(|c| c.set(Some(std::time::Instant::now()))); |
| 3382 | Some(Self(fctx, Some(batch))) |
| 3383 | } |
| 3384 | } |
| 3385 | |
| 3386 | impl Drop for WaveGuard<'_> { |
| 3387 | fn drop(&mut self) { |
| 3388 | let st = self.0.wave.with(|w| w.borrow_mut().take()); |
| 3389 | if let Some(st) = st { |
| 3390 | st.q.lock().unwrap().closed = true; |
| 3391 | st.cv.notify_all(); |
| 3392 | } |
| 3393 | let t0 = std::time::Instant::now(); |
| 3394 | // Dropping the batch waits every worker out, as the joins did. |
| 3395 | drop(self.1.take()); |
| 3396 | if wave_stats() { |
| 3397 | WS_DRAIN.fetch_add(t0.elapsed().as_nanos() as u64, Relaxed); |
| 3398 | WS_TILES.fetch_add(1, Relaxed); |
| 3399 | wave_stats_dump(); |
| 3400 | } |
| 3401 | } |
| 3402 | } |
| 3403 | |
| 3404 | /// An empty buffer for the next superblock: the recycled one when it is at |
| 3405 | /// least as big as the superblock just parsed, else a fresh sized one |
| 3406 | /// (lane-sballoc; `v` is always empty). |
| 3407 | fn reuse<T>(mut v: Vec<T>, cap: usize) -> Vec<T> { |
| 3408 | if v.capacity() < cap { |
| 3409 | v.reserve_exact(cap - v.len()); |
| 3410 | } |
| 3411 | v |
| 3412 | } |
| 3413 | |
| 3414 | /// Hands superblock `(r, c)`'s recorded writes to the wavefront. `false` |
| 3415 | /// when there is no wavefront (one reconstruction thread), in which case the |
| 3416 | /// caller replays them itself. |
| 3417 | fn wave_submit(fctx: &crate::decode::FrameCtx, r: usize, c: usize) -> bool { |
| 3418 | let st = fctx.wave.with(|w| w.borrow().clone()); |
| 3419 | let Some(st) = st else { return false }; |
| 3420 | // lane-blockmem: the op list is replaced sized for the next superblock, |
| 3421 | // exactly as the two arenas below are -- taking it left a zero-capacity |
| 3422 | // `Vec` that regrew (and memmoved every recorded op) from nothing on |
| 3423 | // every superblock. |
| 3424 | // lane-sballoc: the replacement comes from the finished jobs' free list |
| 3425 | // when there is one (warm capacity, already faulted in); only the first |
| 3426 | // superblocks of a tile allocate. |
| 3427 | let mut q = st.q.lock().unwrap(); |
| 3428 | let (sops, scoeffs, spreds) = q.free.pop().unwrap_or_default(); |
| 3429 | let ops = fctx.recon_ops.with(|c| { |
| 3430 | let mut c = c.borrow_mut(); |
| 3431 | let cap = c.len(); |
| 3432 | std::mem::replace(&mut *c, reuse(sops, cap)) |
| 3433 | }); |
| 3434 | // The job owns its coefficients: the arena is replaced by an empty one |
| 3435 | // sized for the next superblock, so parse never regrows from nothing and |
| 3436 | // the worker frees this buffer once, instead of once per unit. |
| 3437 | let coeffs = fctx.coeff_arena.with(|c| { |
| 3438 | let mut c = c.borrow_mut(); |
| 3439 | let cap = c.len(); |
| 3440 | std::mem::replace(&mut *c, reuse(scoeffs, cap)) |
| 3441 | }); |
| 3442 | let preds = fctx.pred_arena.with(|c| { |
| 3443 | let mut c = c.borrow_mut(); |
| 3444 | let cap = c.len(); |
| 3445 | std::mem::replace(&mut *c, reuse(spreds, cap)) |
| 3446 | }); |
| 3447 | { |
| 3448 | if (c as isize) > q.last_sub[r] { |
| 3449 | q.last_sub[r] = c as isize; |
| 3450 | } |
| 3451 | if !ops.is_empty() { |
| 3452 | q.rows[r].push_back((c, ReconJob { ops, coeffs, preds })); |
| 3453 | q.queued += 1; |
| 3454 | q.last_push[r] = c as isize; |
| 3455 | } |
| 3456 | drop(q); |
| 3457 | st.cv.notify_all(); |
| 3458 | } |
| 3459 | if wave_stats() { |
| 3460 | let now = std::time::Instant::now(); |
| 3461 | if let Some(t) = fctx.wave_mark.with(|c| c.replace(Some(now))) { |
| 3462 | WS_PARSE.fetch_add((now - t).as_nanos() as u64, Relaxed); |
| 3463 | } |
| 3464 | } |
| 3465 | true |
| 3466 | } |
| 3467 | |
| 3468 | /// Waits until every submitted job is reconstructed, so the parser may read |
| 3469 | /// the planes itself (the intrabc source fetch and the interintra intra |
| 3470 | /// predictor -- see [`flush_recon`]). |
| 3471 | fn wave_barrier(fctx: &crate::decode::FrameCtx) { |
| 3472 | let st = fctx.wave.with(|w| w.borrow().clone()); |
| 3473 | let Some(st) = st else { return }; |
| 3474 | let stats = wave_stats(); |
| 3475 | let t0 = std::time::Instant::now(); |
| 3476 | let mut q = st.q.lock().unwrap(); |
| 3477 | while q.queued > 0 || q.active > 0 { |
| 3478 | q = st.cv.wait(q).unwrap(); |
| 3479 | } |
| 3480 | drop(q); |
| 3481 | if stats { |
| 3482 | WS_BARRIER.fetch_add(t0.elapsed().as_nanos() as u64, Relaxed); |
| 3483 | WS_BARRIERS.fetch_add(1, Relaxed); |
| 3484 | } |
| 3485 | } |
| 3486 | |
| 3487 | /// Performs an intra write at its push site when the tile registered its |
| 3488 | /// planes for inline reconstruction ([`ReconPlanes`]); `false` when the |
| 3489 | /// caller must record it for the wavefront workers instead. |
| 3490 | #[allow(unsafe_code)] |
| 3491 | #[allow(clippy::too_many_arguments)] |
| 3492 | fn inline_intra( |
| 3493 | plane: usize, |
| 3494 | x: usize, |
| 3495 | oy: usize, |
| 3496 | bw: usize, |
| 3497 | bh: usize, |
| 3498 | rect: bool, |
| 3499 | mode: usize, |
| 3500 | angle_delta: i32, |
| 3501 | reach: Reach, |
| 3502 | residual: &[i32], |
| 3503 | cfl: Option<(i32, CflSrc)>, |
| 3504 | filter_intra: Option<usize>, |
| 3505 | smooth_neighbor: bool, |
| 3506 | fctx: &crate::decode::FrameCtx, |
| 3507 | ) -> bool { |
| 3508 | let Some(ReconPlanes(y, u, v)) = fctx.recon_planes.with(std::cell::Cell::get) else { |
| 3509 | return false; |
| 3510 | }; |
| 3511 | let pred = take_palette_pred(fctx); |
| 3512 | // SAFETY: see [`ReconPlanes`]. |
| 3513 | unsafe { |
| 3514 | exec_intra( |
| 3515 | &mut *y, &mut *u, &mut *v, plane, x, oy, bw, bh, rect, mode, angle_delta, reach, |
| 3516 | residual, cfl, filter_intra, smooth_neighbor, pred, fctx, |
| 3517 | ); |
| 3518 | } |
| 3519 | true |
| 3520 | } |
| 3521 | |
| 3522 | /// [`inline_intra`] for a motion-compensated write. |
| 3523 | #[allow(unsafe_code)] |
| 3524 | #[allow(clippy::too_many_arguments)] |
| 3525 | fn inline_mc( |
| 3526 | plane: usize, |
| 3527 | x: usize, |
| 3528 | oy: usize, |
| 3529 | stride: usize, |
| 3530 | w: usize, |
| 3531 | h: usize, |
| 3532 | prediction: &[u16], |
| 3533 | residual: &[i32], |
| 3534 | fctx: &crate::decode::FrameCtx, |
| 3535 | ) -> bool { |
| 3536 | let Some(ReconPlanes(y, u, v)) = fctx.recon_planes.with(std::cell::Cell::get) else { |
| 3537 | return false; |
| 3538 | }; |
| 3539 | // SAFETY: see [`ReconPlanes`]. |
| 3540 | unsafe { |
| 3541 | exec_mc(&mut *y, &mut *u, &mut *v, plane, x, oy, stride, w, h, prediction, residual, fctx); |
| 3542 | } |
| 3543 | true |
| 3544 | } |
| 3545 | |
| 3546 | /// [`PlaneBuf::reconstruct`], recorded (lane-wave1 step (i)). |
| 3547 | #[allow(clippy::too_many_arguments)] |
| 3548 | fn push_intra( |
| 3549 | plane: usize, |
| 3550 | x: usize, |
| 3551 | y: usize, |
| 3552 | side: usize, |
| 3553 | mode: usize, |
| 3554 | angle_delta: i32, |
| 3555 | reach: Reach, |
| 3556 | residual: &[i32], |
| 3557 | cfl: Option<(i32, CflSrc)>, |
| 3558 | filter_intra: Option<usize>, |
| 3559 | smooth_neighbor: bool, fctx: &crate::decode::FrameCtx, |
| 3560 | ) { |
| 3561 | if inline_intra( |
| 3562 | plane, x, y, side, side, false, mode, angle_delta, reach, residual, cfl, filter_intra, |
| 3563 | smooth_neighbor, fctx, |
| 3564 | ) { |
| 3565 | return; |
| 3566 | } |
| 3567 | let pred = take_palette_pred(fctx); |
| 3568 | push_op(fctx, ReconOp::Intra { |
| 3569 | plane, x, y, bw: side, bh: side, rect: false, mode, angle_delta, reach, |
| 3570 | residual: Residual::done(residual, fctx), cfl, filter_intra, smooth_neighbor, pred, |
| 3571 | }); |
| 3572 | } |
| 3573 | |
| 3574 | /// [`PlaneBuf::reconstruct_rect`], recorded (lane-wave1 step (i)). |
| 3575 | #[allow(clippy::too_many_arguments)] |
| 3576 | fn push_intra_rect( |
| 3577 | plane: usize, |
| 3578 | x: usize, |
| 3579 | y: usize, |
| 3580 | bw: usize, |
| 3581 | bh: usize, |
| 3582 | mode: usize, |
| 3583 | angle_delta: i32, |
| 3584 | reach: Reach, |
| 3585 | residual: &[i32], |
| 3586 | cfl: Option<(i32, CflSrc)>, |
| 3587 | filter_intra: Option<usize>, |
| 3588 | smooth_neighbor: bool, fctx: &crate::decode::FrameCtx, |
| 3589 | ) { |
| 3590 | if inline_intra( |
| 3591 | plane, x, y, bw, bh, true, mode, angle_delta, reach, residual, cfl, filter_intra, |
| 3592 | smooth_neighbor, fctx, |
| 3593 | ) { |
| 3594 | return; |
| 3595 | } |
| 3596 | let pred = take_palette_pred(fctx); |
| 3597 | push_op(fctx, ReconOp::Intra { |
| 3598 | plane, x, y, bw, bh, rect: true, mode, angle_delta, reach, |
| 3599 | residual: Residual::done(residual, fctx), cfl, filter_intra, smooth_neighbor, pred, |
| 3600 | }); |
| 3601 | } |
| 3602 | |
| 3603 | /// [`PlaneBuf::reconstruct_mc_rect`] at `stride == w == h == side`, recorded. |
| 3604 | fn push_mc( |
| 3605 | plane: usize, |
| 3606 | x: usize, |
| 3607 | y: usize, |
| 3608 | side: usize, |
| 3609 | prediction: Pred<'_>, |
| 3610 | residual: &[i32], fctx: &crate::decode::FrameCtx, |
| 3611 | ) { |
| 3612 | push_mc_rect(plane, x, y, side, side, side, prediction, residual, fctx); |
| 3613 | } |
| 3614 | |
| 3615 | /// [`PlaneBuf::reconstruct_mc_rect`], recorded (lane-wave1 step (i)). |
| 3616 | #[allow(clippy::too_many_arguments)] |
| 3617 | fn push_mc_rect( |
| 3618 | plane: usize, |
| 3619 | x: usize, |
| 3620 | y: usize, |
| 3621 | stride: usize, |
| 3622 | w: usize, |
| 3623 | h: usize, |
| 3624 | prediction: Pred<'_>, |
| 3625 | residual: &[i32], fctx: &crate::decode::FrameCtx, |
| 3626 | ) { |
| 3627 | let (src, stride) = match prediction.inline(stride) { |
| 3628 | Some(v) => v, |
| 3629 | None => { |
| 3630 | let Pred::Cur { plane: sp, stride: ss, off } = prediction else { unreachable!() }; |
| 3631 | push_op(fctx, ReconOp::Mc { |
| 3632 | plane, x, y, stride: ss, w, h, |
| 3633 | prediction: PredSrc::Cur { plane: sp, off, src: ss }, |
| 3634 | residual: Residual::done(residual, fctx), |
| 3635 | }); |
| 3636 | return; |
| 3637 | } |
| 3638 | }; |
| 3639 | if inline_mc(plane, x, y, stride, w, h, src, residual, fctx) { |
| 3640 | return; |
| 3641 | } |
| 3642 | push_op(fctx, ReconOp::Mc { |
| 3643 | plane, x, y, stride, w, h, |
| 3644 | // The whole window `reconstruct_mc_rect` reads at `stride`, not |
| 3645 | // `w * h`: the record keeps exactly what `src.to_vec()` carried. |
| 3646 | prediction: PredSrc::Owned(push_pred_samples(src, fctx), src.len() as u32), |
| 3647 | residual: Residual::done(residual, fctx), |
| 3648 | }); |
| 3649 | } |
| 3650 | |
| 3651 | /// [`push_intra_rect`] carrying the transform's INPUTS: at one reconstruction |
| 3652 | /// thread the transform runs here, exactly as before; on the wavefront it |
| 3653 | /// travels with the op and runs on the worker (lane-wave3). |
| 3654 | #[allow(clippy::too_many_arguments)] |
| 3655 | fn push_intra_tx( |
| 3656 | plane: usize, |
| 3657 | x: usize, |
| 3658 | y: usize, |
| 3659 | bw: usize, |
| 3660 | bh: usize, |
| 3661 | rect: bool, |
| 3662 | mode: usize, |
| 3663 | angle_delta: i32, |
| 3664 | reach: Reach, |
| 3665 | tx: TxParams, |
| 3666 | grid: &[i32], |
| 3667 | cfl: Option<(i32, CflSrc)>, |
| 3668 | filter_intra: Option<usize>, |
| 3669 | smooth_neighbor: bool, |
| 3670 | fctx: &crate::decode::FrameCtx, |
| 3671 | ) { |
| 3672 | if fctx.recon_planes.with(std::cell::Cell::get).is_some() { |
| 3673 | inline_intra( |
| 3674 | plane, x, y, bw, bh, rect, mode, angle_delta, reach, &tx.run(grid), cfl, filter_intra, |
| 3675 | smooth_neighbor, fctx, |
| 3676 | ); |
| 3677 | return; |
| 3678 | } |
| 3679 | let pred = take_palette_pred(fctx); |
| 3680 | push_op(fctx, ReconOp::Intra { |
| 3681 | plane, x, y, bw, bh, rect, mode, angle_delta, reach, |
| 3682 | residual: Residual::coeffs(tx, grid, fctx), cfl, filter_intra, smooth_neighbor, pred, |
| 3683 | }); |
| 3684 | } |
| 3685 | |
| 3686 | /// [`push_mc_rect`] carrying the transform's inputs -- see [`push_intra_tx`]. |
| 3687 | #[allow(clippy::too_many_arguments)] |
| 3688 | fn push_mc_rect_tx( |
| 3689 | plane: usize, |
| 3690 | x: usize, |
| 3691 | y: usize, |
| 3692 | stride: usize, |
| 3693 | w: usize, |
| 3694 | h: usize, |
| 3695 | prediction: Pred<'_>, |
| 3696 | tx: TxParams, |
| 3697 | grid: &[i32], |
| 3698 | fctx: &crate::decode::FrameCtx, |
| 3699 | ) { |
| 3700 | // lane-wave4: a deferred prediction is not there to be windowed -- the |
| 3701 | // op carries its offset into the block's prediction instead, and the |
| 3702 | // residual is laid out at that same stride (`reconstruct_mc_rect` reads |
| 3703 | // both buffers at one stride). |
| 3704 | let (prediction, stride) = match prediction.inline(stride) { |
| 3705 | Some(v) => v, |
| 3706 | None => { |
| 3707 | let Pred::Cur { plane: sp, stride: ss, off } = prediction else { unreachable!() }; |
| 3708 | let mut tx = tx; |
| 3709 | tx.stride = 0; |
| 3710 | push_op(fctx, ReconOp::Mc { |
| 3711 | plane, x, y, stride: w, w, h, |
| 3712 | prediction: PredSrc::Cur { plane: sp, off, src: ss }, |
| 3713 | residual: Residual::coeffs(tx, grid, fctx), |
| 3714 | }); |
| 3715 | return; |
| 3716 | } |
| 3717 | }; |
| 3718 | if fctx.recon_planes.with(std::cell::Cell::get).is_some() { |
| 3719 | // `reconstruct_mc_rect` reads the prediction and the residual at ONE |
| 3720 | // stride, so a prediction handed over at the block's stride (a var-tx |
| 3721 | // unit's window, lane-wave4) has to be densified for a dense residual |
| 3722 | // -- which is exactly what the recorded path below does. |
| 3723 | let residual = tx.run(grid); |
| 3724 | let res_stride = if tx.stride == 0 { w } else { tx.stride }; |
| 3725 | let window; |
| 3726 | let prediction = if stride == res_stride { |
| 3727 | prediction |
| 3728 | } else { |
| 3729 | debug_assert_eq!(res_stride, w); |
| 3730 | let mut win = Vec::with_capacity(w * h); |
| 3731 | for row in 0..h { |
| 3732 | win.extend_from_slice(&prediction[row * stride..][..w]); |
| 3733 | } |
| 3734 | window = win; |
| 3735 | &window[..] |
| 3736 | }; |
| 3737 | inline_mc(plane, x, y, res_stride, w, h, prediction, &residual, fctx); |
| 3738 | return; |
| 3739 | } |
| 3740 | // `reconstruct_mc_rect` reads exactly rows `0..h`, columns `0..w` of the |
| 3741 | // two buffers at `stride`, so the record carries that window densely |
| 3742 | // rather than the whole block's prediction: a 4x4 unit of a 64x64 block |
| 3743 | // copied 64*64 samples on the PARSING thread before. The residual is then |
| 3744 | // produced dense too (`TxParams::stride = 0`), which is the same numbers |
| 3745 | // in the same places at stride `w`. |
| 3746 | let off = fctx.pred_arena.with(|a| { |
| 3747 | let mut a = a.borrow_mut(); |
| 3748 | let off = a.len(); |
| 3749 | for row in 0..h { |
| 3750 | a.extend_from_slice(&prediction[row * stride..][..w]); |
| 3751 | } |
| 3752 | off as u32 |
| 3753 | }); |
| 3754 | let mut tx = tx; |
| 3755 | tx.stride = 0; |
| 3756 | push_op(fctx, ReconOp::Mc { |
| 3757 | plane, x, y, stride: w, w, h, prediction: PredSrc::Owned(off, (w * h) as u32), |
| 3758 | residual: Residual::coeffs(tx, grid, fctx), |
| 3759 | }); |
| 3760 | } |
| 3761 | |
| 3762 | /// Replays every recorded [`ReconOp`] into the frame's planes, in push order, |
| 3763 | /// and empties the queue (lane-wave1 step (i)). |
| 3764 | /// |
| 3765 | /// Called at the top of each superblock visit and at the end of each tile, so |
| 3766 | /// a whole superblock's parsing runs with no plane access at all; the two |
| 3767 | /// parse-time pixel READS that survive -- the intrabc source fetch and the |
| 3768 | /// interintra intra predictor -- flush ahead of themselves instead, which |
| 3769 | /// keeps them byte-exact and names exactly what step (ii) still has to move. |
| 3770 | fn flush_recon( |
| 3771 | fctx: &crate::decode::FrameCtx, |
| 3772 | y: &mut PlaneBuf<'static>, |
| 3773 | u: &mut PlaneBuf<'static>, |
| 3774 | v: &mut PlaneBuf<'static>, |
| 3775 | ) { |
| 3776 | // lane-wave1 step (ii): a parse-time pixel READ needs every worker's |
| 3777 | // writes in place, not just this superblock's queue. |
| 3778 | wave_barrier(fctx); |
| 3779 | let mut ops = fctx.recon_spare.with(|c| std::mem::take(&mut *c.borrow_mut())); |
| 3780 | fctx.recon_ops.with(|c| std::mem::swap(&mut *c.borrow_mut(), &mut ops)); |
| 3781 | let mut coeffs = fctx.coeff_spare.with(|c| std::mem::take(&mut *c.borrow_mut())); |
| 3782 | fctx.coeff_arena.with(|c| std::mem::swap(&mut *c.borrow_mut(), &mut coeffs)); |
| 3783 | let mut preds = fctx.pred_spare.with(|c| std::mem::take(&mut *c.borrow_mut())); |
| 3784 | fctx.pred_arena.with(|c| std::mem::swap(&mut *c.borrow_mut(), &mut preds)); |
| 3785 | let mut cur: [Vec<u16>; 3] = Default::default(); |
| 3786 | for op in ops.drain(..) { |
| 3787 | exec_op(op, y, u, v, &mut cur, &coeffs, &preds, fctx); |
| 3788 | } |
| 3789 | coeffs.clear(); |
| 3790 | preds.clear(); |
| 3791 | fctx.recon_spare.with(|c| *c.borrow_mut() = ops); |
| 3792 | fctx.coeff_spare.with(|c| *c.borrow_mut() = coeffs); |
| 3793 | fctx.pred_spare.with(|c| *c.borrow_mut() = preds); |
| 3794 | } |
| 3795 | |
| 3796 | /// Performs one recorded write: the body [`flush_recon`] replays, and the |
| 3797 | /// body [`push_op`] runs immediately when reconstruction is inline. |
| 3798 | fn exec_op( |
| 3799 | op: ReconOp, |
| 3800 | y: &mut PlaneBuf<'static>, |
| 3801 | u: &mut PlaneBuf<'static>, |
| 3802 | v: &mut PlaneBuf<'static>, |
| 3803 | cur: &mut [Vec<u16>; 3], |
| 3804 | coeffs: &[i32], |
| 3805 | preds: &[u16], |
| 3806 | fctx: &crate::decode::FrameCtx, |
| 3807 | ) { |
| 3808 | // lane-nbpub instrument: `EC_AV1_NORECON=1` drops every recorded pixel |
| 3809 | // write, leaving the entropy parse and the per-mi neighbour bookkeeping |
| 3810 | // alone. It makes the encoder's own streams garbage, so it is a |
| 3811 | // MEASUREMENT rung only -- what it measures is how much of the encoder's |
| 3812 | // first decode of its own tiles (`par::S_FIRST`) is reconstruction and |
| 3813 | // how much is parse. Answer at 3840x1608 (4 frames, 4x2 tiles, 8 tile |
| 3814 | // threads): 150 ms -> 141 ms, i.e. the reconstruction is 6% of that |
| 3815 | // stage and the parse plus the neighbour grids are the other 94%. That |
| 3816 | // is why handing the decoder the encoder's own reconstruction cannot |
| 3817 | // shrink the stage: only publishing the per-mi neighbour state the |
| 3818 | // filters read (and so never parsing at all) can. |
| 3819 | if crate::envflags::env_flag!("EC_AV1_NORECON") { |
| 3820 | return; |
| 3821 | } |
| 3822 | let mut scratch = Vec::new(); |
| 3823 | match op { |
| 3824 | ReconOp::Pred(b) => (b.0)(y, u, v, fctx, cur), |
| 3825 | ReconOp::Intra { |
| 3826 | plane, x, y: oy, bw, bh, rect, mode, angle_delta, reach, residual, cfl, |
| 3827 | filter_intra, smooth_neighbor, pred, |
| 3828 | } => exec_intra( |
| 3829 | y, u, v, plane, x, oy, bw, bh, rect, mode, angle_delta, reach, |
| 3830 | residual.resolve(coeffs, &mut scratch), |
| 3831 | cfl, filter_intra, smooth_neighbor, pred, fctx, |
| 3832 | ), |
| 3833 | ReconOp::Mc { plane, x, y: oy, stride, w, h, prediction, residual } => { |
| 3834 | let residual = residual.resolve(coeffs, &mut scratch); |
| 3835 | match prediction { |
| 3836 | PredSrc::Owned(off, len) => { |
| 3837 | let p = &preds[off as usize..][..len as usize]; |
| 3838 | exec_mc(y, u, v, plane, x, oy, stride, w, h, p, residual, fctx); |
| 3839 | } |
| 3840 | PredSrc::Cur { plane: sp, off, src } => { |
| 3841 | let block = std::mem::take(&mut cur[sp]); |
| 3842 | if src == stride { |
| 3843 | exec_mc(y, u, v, plane, x, oy, stride, w, h, &block[off..], residual, fctx); |
| 3844 | } else { |
| 3845 | // A transform unit's residual is dense, so its window |
| 3846 | // of the block prediction is taken out dense too -- |
| 3847 | // the copy the parsing thread used to make, made here. |
| 3848 | let mut window = Vec::with_capacity(w * h); |
| 3849 | for row in 0..h { |
| 3850 | window.extend_from_slice(&block[off + row * src..][..w]); |
| 3851 | } |
| 3852 | exec_mc(y, u, v, plane, x, oy, stride, w, h, &window, residual, fctx); |
| 3853 | } |
| 3854 | cur[sp] = block; |
| 3855 | } |
| 3856 | } |
| 3857 | } |
| 3858 | } |
| 3859 | } |
| 3860 | |
| 3861 | /// [`ReconOp::Intra`] performed. Its arguments are borrowed, so the inline |
| 3862 | /// path ([`push_op`]'s registered planes) reaches it without ever building |
| 3863 | /// the record -- which is what keeps one reconstruction thread at the cost of |
| 3864 | /// the pre-lane-wave1 decoder. |
| 3865 | #[allow(clippy::too_many_arguments)] |
| 3866 | fn exec_intra( |
| 3867 | y: &mut PlaneBuf<'static>, |
| 3868 | u: &mut PlaneBuf<'static>, |
| 3869 | v: &mut PlaneBuf<'static>, |
| 3870 | plane: usize, |
| 3871 | x: usize, |
| 3872 | oy: usize, |
| 3873 | bw: usize, |
| 3874 | bh: usize, |
| 3875 | rect: bool, |
| 3876 | mode: usize, |
| 3877 | angle_delta: i32, |
| 3878 | reach: Reach, |
| 3879 | residual: &[i32], |
| 3880 | cfl: Option<(i32, CflSrc)>, |
| 3881 | filter_intra: Option<usize>, |
| 3882 | smooth_neighbor: bool, |
| 3883 | pred: Option<Vec<u16>>, |
| 3884 | fctx: &crate::decode::FrameCtx, |
| 3885 | ) { |
| 3886 | // The CfL AC signal is read HERE, off the luma that has already been |
| 3887 | // written for this block -- at parse time it would have been the previous |
| 3888 | // frame's samples. |
| 3889 | let ac = cfl.map(|(alpha, src)| { |
| 3890 | let sig = cfl_ac_ss(&*y, src.px, src.py, src.bw, src.bh, ss_x(fctx), ss_y(fctx)); |
| 3891 | (alpha, sig) |
| 3892 | }); |
| 3893 | let cfl = ac.as_ref().map(|(a, sig)| (*a, sig.as_slice())); |
| 3894 | let target: &mut PlaneBuf<'static> = match plane { 0 => &mut *y, 1 => &mut *u, _ => &mut *v }; |
| 3895 | if rect { |
| 3896 | target.reconstruct_rect( |
| 3897 | x, oy, bw, bh, mode, angle_delta, reach, residual, cfl, filter_intra, |
| 3898 | smooth_neighbor, pred, fctx, |
| 3899 | ); |
| 3900 | } else { |
| 3901 | target.reconstruct( |
| 3902 | x, oy, bw, mode, angle_delta, reach, residual, cfl, filter_intra, |
| 3903 | smooth_neighbor, pred, fctx, |
| 3904 | ); |
| 3905 | } |
| 3906 | } |
| 3907 | |
| 3908 | /// [`ReconOp::Mc`] performed -- see [`exec_intra`]. |
| 3909 | #[allow(clippy::too_many_arguments)] |
| 3910 | fn exec_mc( |
| 3911 | y: &mut PlaneBuf<'static>, |
| 3912 | u: &mut PlaneBuf<'static>, |
| 3913 | v: &mut PlaneBuf<'static>, |
| 3914 | plane: usize, |
| 3915 | x: usize, |
| 3916 | oy: usize, |
| 3917 | stride: usize, |
| 3918 | w: usize, |
| 3919 | h: usize, |
| 3920 | prediction: &[u16], |
| 3921 | residual: &[i32], |
| 3922 | fctx: &crate::decode::FrameCtx, |
| 3923 | ) { |
| 3924 | let target: &mut PlaneBuf<'static> = match plane { 0 => &mut *y, 1 => &mut *u, _ => &mut *v }; |
| 3925 | target.reconstruct_mc_rect(x, oy, stride, w, h, prediction, residual, fctx); |
| 3926 | } |
| 3927 | |
| 3928 | thread_local! { |
| 3929 | /// The current frame's `mi` grid, kept only while a frame header set |
| 3930 | /// `allow_intrabc` -- an intrabc block's DV predictor is the ordinary |
| 3931 | /// MV stack ([`crate::mvstack::find_mv_stack`]) built against |
| 3932 | /// `INTRA_FRAME`, which needs the neighbour state a key frame otherwise |
| 3933 | /// never keeps. `(grid, mi_cols, mi_rows)`; `None` on every frame that |
| 3934 | /// does not allow intrabc (so nothing is paid for it). |
| 3935 | /// |
| 3936 | /// corner-cut: only [`decode_block`] (the square path) records into it, |
| 3937 | /// so a frame mixing intrabc with rect/sub-8x8 leaves would predict off |
| 3938 | /// an incomplete grid. Ceiling: the DV *value* (never the parse) would |
| 3939 | /// be wrong there; upgrade = record from the rect/leaf paths too. |
| 3940 | /// The block vector [`read_intra_mode`] just decoded, handed to |
| 3941 | /// [`decode_block`] off the call stack (same idiom as [`PALETTE_PRED`], |
| 3942 | /// which the alternative -- a tenth tuple member through every caller -- |
| 3943 | /// already avoids). Always `.take()`n by the block that reads it. |
| 3944 | /// How many blocks read `use_intrabc == 1` and took the DV path -- the |
| 3945 | /// gate's hit counter. Bumped on the READ, before the rect-strip refusal |
| 3946 | /// returns too, so it counts reads, not reconstructions (a refused block |
| 3947 | /// is never reconstructed). |
| 3948 | static INTRABC_HITS: std::cell::Cell<usize> = const { std::cell::Cell::new(0) }; |
| 3949 | } |
| 3950 | // The `tx_type` the last plane-0 [`read_plane`] actually coded, kept so |
| 3951 | // an intrabc block's chroma reads can inherit it (see |
| 3952 | // [`INTRABC_CHROMA_TX`]). Written on every luma read; only ever read |
| 3953 | // through that one arming site. |
| 3954 | // libaom `av1_get_tx_type` (`blockd.h:1350`): for `PLANE_TYPE_UV` it |
| 3955 | // branches on `is_inter_block(mbmi)` -- which `is_intrabc_block` also |
| 3956 | // satisfies (`blockd.h:373`) -- and takes the *colocated luma |
| 3957 | // transform's coded* `tx_type` (`mbmi->txk_type[...]`), never |
| 3958 | // `intra_mode_to_tx_type` of the chroma mode. `Some` only across an |
| 3959 | // intrabc block's two chroma [`read_plane`] calls; the type decides the |
| 3960 | // `TX_CLASS`, hence the scan order, `eob_multi_ctx` and every nz context |
| 3961 | // -- getting it wrong desyncs the chroma coefficients, it is not a |
| 3962 | // reconstruction-only detail. |
| 3963 | |
| 3964 | /// Whether this frame's header set `allow_intrabc` -- [`INTRABC_MI_GRID`] is |
| 3965 | /// `Some` exactly then (set once per frame in `decode_frame`), so the flag |
| 3966 | /// needs no separate plumbing to reach the rect-strip mode reader. |
| 3967 | fn allow_intrabc_frame(fctx: &crate::decode::FrameCtx) -> bool { |
| 3968 | fctx.intrabc_mi_grid.with(|g| g.borrow().is_some()) |
| 3969 | } |
| 3970 | |
| 3971 | thread_local! { |
| 3972 | /// How many rect intra strips READ the `use_intrabc` symbol (i.e. how many |
| 3973 | /// times the fix below was exercised) -- the rect-strip gate's hit counter, |
| 3974 | /// distinct from [`INTRABC_HITS`] which counts blocks that USE intrabc. |
| 3975 | static RECT_INTRABC_READS: std::cell::Cell<usize> = const { std::cell::Cell::new(0) }; |
| 3976 | } |
| 3977 | |
| 3978 | /// Current value of [`RECT_INTRABC_READS`]. |
| 3979 | pub(crate) fn rect_intrabc_reads() -> usize { |
| 3980 | RECT_INTRABC_READS.with(|c| c.get()) |
| 3981 | } |
| 3982 | |
| 3983 | /// Zeroes [`RECT_INTRABC_READS`] (gate tests call this before a decode). |
| 3984 | #[allow(dead_code)] // read only from the `#[cfg(test)]` gates |
| 3985 | pub(crate) fn reset_rect_intrabc_reads() { |
| 3986 | RECT_INTRABC_READS.with(|c| c.set(0)); |
| 3987 | } |
| 3988 | |
| 3989 | thread_local! { |
| 3990 | /// Blocks that USED intrabc at an 8x8 LEAF (`decode_leaf8`) specifically |
| 3991 | /// -- the shape lane-kf900 r7 fixed; [`INTRABC_HITS`] counts every shape. |
| 3992 | static LEAF8_INTRABC_HITS: std::cell::Cell<usize> = const { std::cell::Cell::new(0) }; |
| 3993 | } |
| 3994 | |
| 3995 | thread_local! { |
| 3996 | /// Chroma reads of an intrabc TX4-split leaf that consulted an ARMED |
| 3997 | /// [`INTRABC_CHROMA_TX`] slot -- proof that the arm's frame-copy + |
| 3998 | /// inherited-type route actually ran (lane-av1intrapred r2's non-vacuity |
| 3999 | /// counter: zero means the gate's fixture no longer reaches the route). |
| 4000 | static INTRABC_TX4_CHROMA_COPY_HITS: std::cell::Cell<usize> = |
| 4001 | const { std::cell::Cell::new(0) }; |
| 4002 | } |
| 4003 | |
| 4004 | /// Current value of [`INTRABC_TX4_CHROMA_COPY_HITS`]. |
| 4005 | #[allow(dead_code)] // read only from the `#[cfg(test)]` gates |
| 4006 | pub(crate) fn intrabc_tx4_chroma_copy_hits() -> usize { |
| 4007 | INTRABC_TX4_CHROMA_COPY_HITS.with(|c| c.get()) |
| 4008 | } |
| 4009 | |
| 4010 | /// Zeroes [`INTRABC_TX4_CHROMA_COPY_HITS`] (gate tests call this before a decode). |
| 4011 | #[allow(dead_code)] // read only from the `#[cfg(test)]` gates |
| 4012 | pub(crate) fn reset_intrabc_tx4_chroma_copy_hits() { |
| 4013 | INTRABC_TX4_CHROMA_COPY_HITS.with(|c| c.set(0)); |
| 4014 | } |
| 4015 | |
| 4016 | /// Current value of [`LEAF8_INTRABC_HITS`]. |
| 4017 | pub(crate) fn leaf8_intrabc_hits() -> usize { |
| 4018 | LEAF8_INTRABC_HITS.with(|c| c.get()) |
| 4019 | } |
| 4020 | |
| 4021 | /// Zeroes [`LEAF8_INTRABC_HITS`] (gate tests call this before a decode). |
| 4022 | #[allow(dead_code)] // read only from the `#[cfg(test)]` gates |
| 4023 | pub(crate) fn reset_leaf8_intrabc_hits() { |
| 4024 | LEAF8_INTRABC_HITS.with(|c| c.set(0)); |
| 4025 | } |
| 4026 | |
| 4027 | /// Current value of [`INTRABC_HITS`]. |
| 4028 | #[allow(dead_code)] // read only from the `#[cfg(test)]` gates |
| 4029 | pub(crate) fn intrabc_hits() -> usize { |
| 4030 | INTRABC_HITS.with(|c| c.get()) |
| 4031 | } |
| 4032 | |
| 4033 | thread_local! { |
| 4034 | /// An intrabc block that IS a sub-8x8 group's CHROMA REFERENCE and is |
| 4035 | /// SKIPPED: the route that arms [`INTRABC_CHROMA_TX`] and then reads no |
| 4036 | /// chroma coefficient at all, so the slot used to stay armed into the NEXT |
| 4037 | /// block's chroma read. Both group shapes (4x4 split, 4x8/8x4 rect) count |
| 4038 | /// here. A non-zero value is what makes the disarm below load-bearing. |
| 4039 | static SKIPPED_INTRABC_CHROMA_ARM_HITS: std::cell::Cell<usize> = |
| 4040 | const { std::cell::Cell::new(0) }; |
| 4041 | } |
| 4042 | |
| 4043 | /// Current value of [`SKIPPED_INTRABC_CHROMA_ARM_HITS`]. |
| 4044 | #[allow(dead_code)] // read only from the `#[cfg(test)]` gates and the probe |
| 4045 | pub(crate) fn skipped_intrabc_chroma_arm_hits() -> usize { |
| 4046 | SKIPPED_INTRABC_CHROMA_ARM_HITS.with(|c| c.get()) |
| 4047 | } |
| 4048 | |
| 4049 | /// Zeroes [`SKIPPED_INTRABC_CHROMA_ARM_HITS`] (gate tests call this before a |
| 4050 | /// decode). |
| 4051 | #[allow(dead_code)] // read only from the `#[cfg(test)]` gates |
| 4052 | pub(crate) fn reset_skipped_intrabc_chroma_arm_hits() { |
| 4053 | SKIPPED_INTRABC_CHROMA_ARM_HITS.with(|c| c.set(0)); |
| 4054 | } |
| 4055 | |
| 4056 | thread_local! { |
| 4057 | /// lane-av1-ibcskip2: a sub-8x8 4:4:4 leaf that is BOTH intrabc AND |
| 4058 | /// `skip` -- the arm whose chroma is the bare frame copy at the DV |
| 4059 | /// (libaom `av1_build_inter_predictors_sb` runs regardless of `skip`). |
| 4060 | /// Before the fix this route predicted intra-DC and every one of this |
| 4061 | /// fixture's 150 such cells differed from the oracle on both chroma |
| 4062 | /// planes. |
| 4063 | static SKIPPED_INTRABC_DV_COPY_HITS: std::cell::Cell<usize> = |
| 4064 | const { std::cell::Cell::new(0) }; |
| 4065 | } |
| 4066 | |
| 4067 | /// Current value of [`SKIPPED_INTRABC_DV_COPY_HITS`]. |
| 4068 | #[allow(dead_code)] // read only from the `#[cfg(test)]` gates |
| 4069 | pub(crate) fn skipped_intrabc_dv_copy_hits() -> usize { |
| 4070 | SKIPPED_INTRABC_DV_COPY_HITS.with(|c| c.get()) |
| 4071 | } |
| 4072 | |
| 4073 | /// Zeroes [`SKIPPED_INTRABC_DV_COPY_HITS`] (gate tests call this before a |
| 4074 | /// decode). |
| 4075 | #[allow(dead_code)] // read only from the `#[cfg(test)]` gates |
| 4076 | pub(crate) fn reset_skipped_intrabc_dv_copy_hits() { |
| 4077 | SKIPPED_INTRABC_DV_COPY_HITS.with(|c| c.set(0)); |
| 4078 | } |
| 4079 | |
| 4080 | /// Zeroes [`INTRABC_HITS`] (gate tests call this before a decode). |
| 4081 | #[allow(dead_code)] // read only from the `#[cfg(test)]` gates |
| 4082 | pub(crate) fn reset_intrabc_hits() { |
| 4083 | INTRABC_HITS.with(|c| c.set(0)); |
| 4084 | } |
| 4085 | |
| 4086 | thread_local! { |
| 4087 | /// lane-t900 r32: intrabc blocks whose transform size came off the INTER |
| 4088 | /// var-tx partition tree (unskipped, `TX_MODE_SELECT`) -- the capability |
| 4089 | /// that replaced the refusal "an intrabc block under TxMode::Select". |
| 4090 | static INTRABC_VARTX_HITS: std::cell::Cell<usize> = const { std::cell::Cell::new(0) }; |
| 4091 | /// lane-av1-ibcvtx: intrabc var-tx trees that resolved to MIXED leaf |
| 4092 | /// sizes (not one square size across every leaf) -- the engagement |
| 4093 | /// counter proving a witness actually exercises the per-leaf reconstruct. |
| 4094 | static INTRABC_VARTX_MIXED_LEAVES_HITS: std::cell::Cell<usize> = const { std::cell::Cell::new(0) }; |
| 4095 | /// lane-av1txr: as above for a `BLOCK_4X8`/`BLOCK_8X4` intrabc leaf -- |
| 4096 | /// libaom's `block_signals_txsize` is `bsize > BLOCK_4X4`, so these leaves |
| 4097 | /// signal a transform size where a plain `BLOCK_4X4` does not. Reading no |
| 4098 | /// symbol here desynced `warped.obu` at the very next block. |
| 4099 | static RECT_INTRABC_VARTX_HITS: std::cell::Cell<usize> = const { std::cell::Cell::new(0) }; |
| 4100 | /// lane-av1-intrabc: rect INTRABC blocks whose reconstruction |
| 4101 | /// [`decode_intrabc_rect`] ran -- the engagement counter the rect-strip |
| 4102 | /// gates assert on (a refused block bumps [`INTRABC_HITS`] but never |
| 4103 | /// this one). |
| 4104 | static INTRABC_RECT_HITS: std::cell::Cell<usize> = const { std::cell::Cell::new(0) }; |
| 4105 | /// Of those, how many read the INTER var-tx tree (unskipped, TX_MODE_SELECT). |
| 4106 | static INTRABC_RECT_VARTX_HITS: std::cell::Cell<usize> = const { std::cell::Cell::new(0) }; |
| 4107 | /// lane-av1-intrabc r4: rect INTRABC blocks whose reconstruction |
| 4108 | /// [`decode_intrabc_rect`] ran with `skip == 0` (a real residual read), |
| 4109 | /// split by the strip's orientation -- `bw > bh` is a HORZ strip, |
| 4110 | /// `bh > bw` a VERT one. The reviewer's unblock condition needs a CODED |
| 4111 | /// witness in BOTH orientations, and the existing `INTRABC_RECT_HITS` |
| 4112 | /// counts neither orientation nor the skip flag, so an arm that flipped |
| 4113 | /// to an all-skip stream (no residual reader at all) would stay green. |
| 4114 | static INTRABC_RECT_CODED_HITS: std::cell::Cell<[usize; 2]> = |
| 4115 | const { std::cell::Cell::new([0, 0]) }; |
| 4116 | /// lane-av1-intrabc r5: the `skip == 1` twin of [`INTRABC_RECT_CODED_HITS`] |
| 4117 | /// -- rect INTRABC blocks reconstructed as a bare prediction copy, split |
| 4118 | /// by orientation. The r5 panic (residual stride `bw * bh` underrunning |
| 4119 | /// `reconstruct_mc_rect`'s `row * side + w` index on a VERT strip) was |
| 4120 | /// invisible to every gate because they only counted CODED strips. |
| 4121 | static INTRABC_RECT_SKIPPED_HITS: std::cell::Cell<[usize; 2]> = |
| 4122 | const { std::cell::Cell::new([0, 0]) }; |
| 4123 | } |
| 4124 | |
| 4125 | /// lane-av1-intrabc r4: coded (`skip == 0`) rect intrabc blocks reconstructed, |
| 4126 | /// as `[horz, vert]` -- see [`INTRABC_RECT_CODED_HITS`]. |
| 4127 | #[allow(dead_code)] // read only from the `#[cfg(test)]` gates |
| 4128 | pub fn intrabc_rect_coded_hits() -> [usize; 2] { |
| 4129 | INTRABC_RECT_CODED_HITS.with(std::cell::Cell::get) |
| 4130 | } |
| 4131 | |
| 4132 | /// lane-av1-intrabc r5: skipped (`skip == 1`) rect intrabc blocks |
| 4133 | /// reconstructed, as `[horz, vert]` -- see [`INTRABC_RECT_SKIPPED_HITS`]. |
| 4134 | #[allow(dead_code)] // read only from the `#[cfg(test)]` gates |
| 4135 | pub fn intrabc_rect_skipped_hits() -> [usize; 2] { |
| 4136 | INTRABC_RECT_SKIPPED_HITS.with(std::cell::Cell::get) |
| 4137 | } |
| 4138 | |
| 4139 | /// Current value of [`INTRABC_RECT_HITS`]. |
| 4140 | #[allow(dead_code)] // read only from the `#[cfg(test)]` gates |
| 4141 | pub fn intrabc_rect_hits() -> usize { |
| 4142 | INTRABC_RECT_HITS.with(|c| c.get()) |
| 4143 | } |
| 4144 | |
| 4145 | /// Zeroes [`INTRABC_RECT_HITS`]/[`INTRABC_RECT_VARTX_HITS`]. |
| 4146 | #[allow(dead_code)] // read only from the `#[cfg(test)]` gates |
| 4147 | pub(crate) fn reset_intrabc_rect_hits() { |
| 4148 | INTRABC_RECT_HITS.with(|c| c.set(0)); |
| 4149 | INTRABC_RECT_VARTX_HITS.with(|c| c.set(0)); |
| 4150 | INTRABC_RECT_CODED_HITS.with(|c| c.set([0, 0])); |
| 4151 | INTRABC_RECT_SKIPPED_HITS.with(|c| c.set([0, 0])); |
| 4152 | } |
| 4153 | |
| 4154 | /// Current value of [`INTRABC_RECT_VARTX_HITS`]. |
| 4155 | #[allow(dead_code)] // read only from the `#[cfg(test)]` gates |
| 4156 | pub fn intrabc_rect_vartx_hits() -> usize { |
| 4157 | INTRABC_RECT_VARTX_HITS.with(|c| c.get()) |
| 4158 | } |
| 4159 | |
| 4160 | thread_local! { |
| 4161 | /// lane-av1-ibcrect: `use_intrabc` blocks on a 128-root HORZ/VERT strip |
| 4162 | /// (BLOCK_128X64 / BLOCK_64X128) whose reconstruction |
| 4163 | /// [`decode_intrabc_128rect`] ran -- the shape the named refusal used to |
| 4164 | /// guard (`decode_block_128rect`). A refused block bumps |
| 4165 | /// [`INTRABC_HITS`] but never this one. |
| 4166 | static INTRABC_128RECT_HITS: std::cell::Cell<usize> = const { std::cell::Cell::new(0) }; |
| 4167 | } |
| 4168 | |
| 4169 | /// Current value of [`INTRABC_128RECT_HITS`]. |
| 4170 | #[allow(dead_code)] // read only from the `#[cfg(test)]` gates |
| 4171 | pub fn intrabc_128rect_hits() -> usize { |
| 4172 | INTRABC_128RECT_HITS.with(|c| c.get()) |
| 4173 | } |
| 4174 | |
| 4175 | /// Zeroes [`INTRABC_128RECT_HITS`]. |
| 4176 | #[allow(dead_code)] // read only from the `#[cfg(test)]` gates |
| 4177 | pub(crate) fn reset_intrabc_128rect_hits() { |
| 4178 | INTRABC_128RECT_HITS.with(|c| c.set(0)); |
| 4179 | } |
| 4180 | |
| 4181 | /// Current value of [`INTRABC_VARTX_HITS`]. |
| 4182 | pub fn intrabc_vartx_hits() -> usize { |
| 4183 | INTRABC_VARTX_HITS.with(|c| c.get()) |
| 4184 | } |
| 4185 | |
| 4186 | /// Current value of [`INTRABC_VARTX_MIXED_LEAVES_HITS`]. |
| 4187 | pub fn intrabc_vartx_mixed_leaves_hits() -> usize { |
| 4188 | INTRABC_VARTX_MIXED_LEAVES_HITS.with(|c| c.get()) |
| 4189 | } |
| 4190 | |
| 4191 | /// Zeroes [`INTRABC_VARTX_HITS`] (gate tests call this before a decode). |
| 4192 | #[allow(dead_code)] // read only from the `#[cfg(test)]` gates |
| 4193 | pub(crate) fn reset_intrabc_vartx_hits() { |
| 4194 | INTRABC_VARTX_HITS.with(|c| c.set(0)); |
| 4195 | } |
| 4196 | |
| 4197 | /// Zeroes [`INTRABC_VARTX_MIXED_LEAVES_HITS`] (gate tests call this before a |
| 4198 | /// decode). |
| 4199 | #[allow(dead_code)] // read only from the `#[cfg(test)]` gates |
| 4200 | pub(crate) fn reset_intrabc_vartx_mixed_leaves_hits() { |
| 4201 | INTRABC_VARTX_MIXED_LEAVES_HITS.with(|c| c.set(0)); |
| 4202 | } |
| 4203 | |
| 4204 | /// Current value of [`RECT_INTRABC_VARTX_HITS`]. |
| 4205 | #[allow(dead_code)] // read only from the `#[cfg(test)]` gates |
| 4206 | pub fn rect_intrabc_vartx_hits() -> usize { |
| 4207 | RECT_INTRABC_VARTX_HITS.with(|c| c.get()) |
| 4208 | } |
| 4209 | |
| 4210 | /// Records one coded block into [`INTRABC_MI_GRID`]: every block contributes |
| 4211 | /// its size (libaom's `scan_row_mbmi` advances `processed_rows` off any |
| 4212 | /// candidate's `bsize`, ref-match or not), an intrabc one also its DV as an |
| 4213 | /// `INTRA_FRAME` candidate (libaom treats intrabc as `is_inter_block`). |
| 4214 | fn record_intrabc_mi(mi_r: usize, mi_c: usize, n4: usize, dv: Option<(i32, i32)>, n: &mut Neighbours, fctx: &crate::decode::FrameCtx) { |
| 4215 | record_intrabc_mi_rect(mi_r, mi_c, n4, n4, dv, n, fctx); |
| 4216 | } |
| 4217 | |
| 4218 | /// [`record_intrabc_mi`] with a true rectangular footprint: `av1_find_mv_refs` |
| 4219 | /// takes the block's real `bsize`, so a sub-8x8 `BLOCK_4X8`/`BLOCK_8X4` |
| 4220 | /// intrabc leaf must publish a 1x2/2x1 mi span, not a square (lane-av1txr). |
| 4221 | /// |
| 4222 | /// Also stamps libaom's `is_inter_block` verdict for this block into the |
| 4223 | /// `above_inter`/`left_inter` bands ([`tx_size_context_txfm_rect`]'s override |
| 4224 | /// term): `is_inter_block` is `is_intrabc_block(mbmi) || ref_frame[0] > |
| 4225 | /// INTRA_FRAME` (`blockd.h:373`), so an intrabc block is "inter" there and |
| 4226 | /// `get_tx_size_context` reads its BLOCK size rather than its transform band. |
| 4227 | /// The `false` half of the stamp is [`Neighbours::fill_lf_grid_rect`]'s, which |
| 4228 | /// every block runs; this runs after it (class leaked-band otherwise). |
| 4229 | fn record_intrabc_mi_rect( |
| 4230 | mi_r: usize, |
| 4231 | mi_c: usize, |
| 4232 | n4_w: usize, |
| 4233 | n4_h: usize, |
| 4234 | dv: Option<(i32, i32)>, |
| 4235 | n: &mut Neighbours, |
| 4236 | fctx: &crate::decode::FrameCtx, |
| 4237 | ) { |
| 4238 | if dv.is_some() { |
| 4239 | for i in 0..n4_w { |
| 4240 | if let Some(cell) = n.above_inter.get_mut(mi_c + i) { |
| 4241 | *cell = true; |
| 4242 | } |
| 4243 | } |
| 4244 | for i in 0..n4_h { |
| 4245 | if let Some(cell) = n.left_inter.get_mut(mi_r + i) { |
| 4246 | *cell = true; |
| 4247 | } |
| 4248 | } |
| 4249 | } |
| 4250 | fctx.intrabc_mi_grid.with(|g| { |
| 4251 | let mut g = g.borrow_mut(); |
| 4252 | let Some((grid, _, _)) = g.as_mut() else { |
| 4253 | return; |
| 4254 | }; |
| 4255 | let info = crate::mvstack::MiInfo { |
| 4256 | is_inter: dv.is_some(), |
| 4257 | ref_frame: 0, |
| 4258 | ref_frame1: NO_REF1, |
| 4259 | mv: mv16(dv.unwrap_or((0, 0))), |
| 4260 | mv1: (0, 0), |
| 4261 | is_new_mv: dv.is_some(), |
| 4262 | is_global_mv0: false, |
| 4263 | is_global_mv1: false, |
| 4264 | size: n4_w as u8, |
| 4265 | size_h: n4_h as u8, |
| 4266 | }; |
| 4267 | for r in mi_r..mi_r + n4_h { |
| 4268 | for c in mi_c..mi_c + n4_w { |
| 4269 | grid.set(r, c, info); |
| 4270 | } |
| 4271 | } |
| 4272 | }); |
| 4273 | } |
| 4274 | |
| 4275 | // How many blocks decoded `skip_mode == 1` (lane-av1comp round 14) -- |
| 4276 | // proves a real `skip_mode_present` stream actually reached a block that |
| 4277 | // picked it, not just that the frame header bit was set. |
| 4278 | thread_local! { |
| 4279 | static SKIP_MODE_HITS: std::cell::Cell<usize> = |
| 4280 | const { std::cell::Cell::new(0) }; |
| 4281 | } |
| 4282 | |
| 4283 | /// Current value of [`SKIP_MODE_HITS`]. |
| 4284 | #[allow(dead_code)] // read only from the `#[cfg(test)]` gates |
| 4285 | pub(crate) fn skip_mode_hits() -> usize { |
| 4286 | SKIP_MODE_HITS.with(|c| c.get()) |
| 4287 | } |
| 4288 | |
| 4289 | // lane-motionmode: how many blocks actually decoded `obmc_selected == true` |
| 4290 | // (as opposed to just reaching an eligible `read_motion_mode` symbol read) |
| 4291 | // -- the gate's own proof that a real aomenc `--enable-obmc=1` stream |
| 4292 | // reached [`obmc_blend`], not just the header bit. |
| 4293 | thread_local! { |
| 4294 | static OBMC_HITS: std::cell::Cell<usize> = |
| 4295 | const { std::cell::Cell::new(0) }; |
| 4296 | } |
| 4297 | |
| 4298 | // lane-fimv r1 (class wrong-alphabet-same-value): how many blocks took the |
| 4299 | // 2-symbol `obmc_cdf` alphabet *because* `force_integer_mv` (or a scaled |
| 4300 | // reference) vetoed `WARPED_CAUSAL` in libaom `motion_mode_allowed`, while |
| 4301 | // `allow_warped_motion == 1` and `num_proj_ref >= 1` would otherwise have |
| 4302 | // picked the 3-symbol `motion_mode_cdf`. These are exactly the blocks this |
| 4303 | // decoder used to read with the wrong alphabet, so a gate that asserts this |
| 4304 | // counter advanced proves the fixed path fired (class |
| 4305 | // `gate-blind-to-feature`). |
| 4306 | thread_local! { |
| 4307 | static FIMV_ALPHABET_HITS: std::cell::Cell<usize> = const { std::cell::Cell::new(0) }; |
| 4308 | } |
| 4309 | |
| 4310 | /// Current value of [`FIMV_ALPHABET_HITS`]. |
| 4311 | pub fn fimv_alphabet_hits() -> usize { |
| 4312 | FIMV_ALPHABET_HITS.with(|c| c.get()) |
| 4313 | } |
| 4314 | |
| 4315 | // lane-fimv r1: how many frame headers this decoder has decoded with |
| 4316 | // `force_integer_mv == 1` (bumped by `stream.rs` per frame). Lets a gate |
| 4317 | // prove the encoder really produced the header state under test rather than |
| 4318 | // assume it. |
| 4319 | thread_local! { |
| 4320 | static FIMV_FRAME_HITS: std::cell::Cell<usize> = const { std::cell::Cell::new(0) }; |
| 4321 | } |
| 4322 | |
| 4323 | /// Current value of [`FIMV_FRAME_HITS`]. |
| 4324 | pub fn fimv_frame_hits() -> usize { |
| 4325 | FIMV_FRAME_HITS.with(|c| c.get()) |
| 4326 | } |
| 4327 | |
| 4328 | /// Bump [`FIMV_FRAME_HITS`] -- called once per decoded frame header whose |
| 4329 | /// `force_integer_mv` bit is set. |
| 4330 | pub fn note_force_integer_mv_frame() { |
| 4331 | hit!(FIMV_FRAME_HITS); |
| 4332 | } |
| 4333 | |
| 4334 | /// Current value of [`OBMC_HITS`]. |
| 4335 | #[allow(dead_code)] // read only from the `#[cfg(test)]` gates |
| 4336 | pub(crate) fn obmc_hits() -> usize { |
| 4337 | OBMC_HITS.with(|c| c.get()) |
| 4338 | } |
| 4339 | |
| 4340 | // lane-t900 r27: OBMC blends that actually READ a recorded switchable filter |
| 4341 | // off the neighbour band -- the positive half of the proof for "an OBMC |
| 4342 | // neighbour whose switchable interp filter was never recorded". A |
| 4343 | // fixed-filter frame returns before this point (its neighbours legitimately |
| 4344 | // carry the `[3, 3]` sentinel), so every bump is a neighbour whose own |
| 4345 | // `interp_filter` symbols were published by `record_inter_rect_mi`. |
| 4346 | thread_local! { |
| 4347 | static OBMC_FILTER_BAND_READS: std::cell::Cell<usize> = const { std::cell::Cell::new(0) }; |
| 4348 | } |
| 4349 | |
| 4350 | /// Current value of [`OBMC_FILTER_BAND_READS`]. |
| 4351 | #[allow(dead_code)] // read only from the `#[cfg(test)]` gates |
| 4352 | pub(crate) fn obmc_filter_band_reads() -> usize { |
| 4353 | OBMC_FILTER_BAND_READS.with(|c| c.get()) |
| 4354 | } |
| 4355 | |
| 4356 | // lane-scaledref r1: per-case firing counts for the scaled-reference gate |
| 4357 | // (class `gate-blind-to-feature`) -- a pixel match under an UNSCALED |
| 4358 | // reference would pass just as well and prove nothing, so the gate |
| 4359 | // hard-asserts each combination this round lifts actually occurred: |
| 4360 | // a compound block with a scaled tap, a warp block whose warp libaom |
| 4361 | // suppresses because the reference is scaled, an OBMC block, an interintra |
| 4362 | // block, and an 8x8 leaf. |
| 4363 | thread_local! { |
| 4364 | static SCALED_COMPOUND_HITS: std::cell::Cell<usize> = const { std::cell::Cell::new(0) }; |
| 4365 | static SCALED_WARP_FALLBACK_HITS: std::cell::Cell<usize> = const { std::cell::Cell::new(0) }; |
| 4366 | static SCALED_OBMC_HITS: std::cell::Cell<usize> = const { std::cell::Cell::new(0) }; |
| 4367 | static SCALED_INTERINTRA_HITS: std::cell::Cell<usize> = const { std::cell::Cell::new(0) }; |
| 4368 | static SCALED_BLOCK8_HITS: std::cell::Cell<usize> = const { std::cell::Cell::new(0) }; |
| 4369 | static SCALED_WARP_SUPPRESSED_HITS: std::cell::Cell<usize> = const { std::cell::Cell::new(0) }; |
| 4370 | static MIXED_SCALE_COMPOUND_HITS: std::cell::Cell<usize> = const { std::cell::Cell::new(0) }; |
| 4371 | } |
| 4372 | |
| 4373 | /// Current value of [`MIXED_SCALE_COMPOUND_HITS`]: compound blocks with one |
| 4374 | /// scaled and one unscaled tap. |
| 4375 | #[allow(dead_code)] // read only from the `#[cfg(test)]` gates |
| 4376 | pub(crate) fn mixed_scale_compound_hits() -> usize { |
| 4377 | MIXED_SCALE_COMPOUND_HITS.with(|c| c.get()) |
| 4378 | } |
| 4379 | |
| 4380 | /// Current value of [`SCALED_WARP_SUPPRESSED_HITS`]: blocks that would have |
| 4381 | /// read the 3-symbol `motion_mode_cdf` but read the 2-symbol `obmc_cdf` |
| 4382 | /// instead because their reference is scaled (libaom `motion_mode_allowed`, |
| 4383 | /// `blockd.h:1484`). |
| 4384 | #[allow(dead_code)] // read only from the `#[cfg(test)]` gates |
| 4385 | pub(crate) fn scaled_warp_suppressed_hits() -> usize { |
| 4386 | SCALED_WARP_SUPPRESSED_HITS.with(|c| c.get()) |
| 4387 | } |
| 4388 | |
| 4389 | /// Current value of [`SCALED_COMPOUND_HITS`]. |
| 4390 | #[allow(dead_code)] // read only from the `#[cfg(test)]` gates |
| 4391 | pub(crate) fn scaled_compound_hits() -> usize { |
| 4392 | SCALED_COMPOUND_HITS.with(|c| c.get()) |
| 4393 | } |
| 4394 | |
| 4395 | /// Current value of [`SCALED_WARP_FALLBACK_HITS`]. |
| 4396 | pub fn scaled_warp_fallback_hits() -> usize { |
| 4397 | SCALED_WARP_FALLBACK_HITS.with(|c| c.get()) |
| 4398 | } |
| 4399 | |
| 4400 | /// Current value of [`SCALED_OBMC_HITS`]. |
| 4401 | #[allow(dead_code)] // read only from the `#[cfg(test)]` gates |
| 4402 | pub(crate) fn scaled_obmc_hits() -> usize { |
| 4403 | SCALED_OBMC_HITS.with(|c| c.get()) |
| 4404 | } |
| 4405 | |
| 4406 | /// Current value of [`SCALED_INTERINTRA_HITS`]. |
| 4407 | #[allow(dead_code)] // read only from the `#[cfg(test)]` gates |
| 4408 | pub(crate) fn scaled_interintra_hits() -> usize { |
| 4409 | SCALED_INTERINTRA_HITS.with(|c| c.get()) |
| 4410 | } |
| 4411 | |
| 4412 | /// Current value of [`SCALED_BLOCK8_HITS`]. |
| 4413 | #[allow(dead_code)] // read only from the `#[cfg(test)]` gates |
| 4414 | pub(crate) fn scaled_block8_hits() -> usize { |
| 4415 | SCALED_BLOCK8_HITS.with(|c| c.get()) |
| 4416 | } |
| 4417 | |
| 4418 | // lane-motionmode round 3: same proof as [`OBMC_HITS`], narrowed to |
| 4419 | // `decode_inter_block8`'s own 8x8-leaf `read_motion_mode` -- a subset of |
| 4420 | // [`OBMC_HITS`] (both fire together on an 8x8 hit), lets the gate tell an |
| 4421 | // 8x8-leaf OBMC block apart from a 16x16+ one. |
| 4422 | thread_local! { |
| 4423 | static OBMC_HITS_8: std::cell::Cell<usize> = const { std::cell::Cell::new(0) }; |
| 4424 | } |
| 4425 | |
| 4426 | // lane-interp3 r1: how many inter blocks read a SWITCHABLE `interp_filter` |
| 4427 | // pair whose two directions differ (`enable_dual_filter` only -- with dual |
| 4428 | // filter off both directions take the single symbol, so this can never fire). |
| 4429 | // A gate asserting it proves the stream really carries dual-filter blocks. |
| 4430 | thread_local! { |
| 4431 | static DUAL_FILTER_DIFF_HITS: std::cell::Cell<usize> = const { std::cell::Cell::new(0) }; |
| 4432 | } |
| 4433 | |
| 4434 | // lane-interp3 r1: how many COMPOUND 8x8 leaves read their own |
| 4435 | // `read_mb_interp_filter` symbol(s) -- the read this round added (the leaf |
| 4436 | // used to skip it entirely, losing a symbol and stamping the `[3, 3]` |
| 4437 | // no-filter sentinel that made a neighbouring OBMC block panic). |
| 4438 | thread_local! { |
| 4439 | static COMPOUND8_FILTER_HITS: std::cell::Cell<usize> = const { std::cell::Cell::new(0) }; |
| 4440 | } |
| 4441 | |
| 4442 | /// Current value of [`DUAL_FILTER_DIFF_HITS`]. |
| 4443 | #[allow(dead_code)] // read only from the `#[cfg(test)]` gates |
| 4444 | pub(crate) fn dual_filter_diff_hits() -> usize { |
| 4445 | DUAL_FILTER_DIFF_HITS.with(|c| c.get()) |
| 4446 | } |
| 4447 | |
| 4448 | /// Current value of [`COMPOUND8_FILTER_HITS`]. |
| 4449 | #[allow(dead_code)] // read only from the `#[cfg(test)]` gates |
| 4450 | pub(crate) fn compound8_filter_hits() -> usize { |
| 4451 | COMPOUND8_FILTER_HITS.with(|c| c.get()) |
| 4452 | } |
| 4453 | |
| 4454 | // lane-warp round 1: how many blocks resolved `motion_mode_allowed` to |
| 4455 | // `WARPED_CAUSAL`-eligible (3-symbol read, `num_proj_ref >= 1` under |
| 4456 | // `allow_warped_motion`) AND the symbol actually decoded to `WARPED_CAUSAL` |
| 4457 | // -- the gate's proof that a fixture really exercises the alphabet this |
| 4458 | // round changed, even though the block itself still refuses (warp |
| 4459 | // estimation/filter not ported). |
| 4460 | thread_local! { |
| 4461 | static WARP_SELECTED_HITS: std::cell::Cell<usize> = |
| 4462 | const { std::cell::Cell::new(0) }; |
| 4463 | } |
| 4464 | |
| 4465 | /// Current value of [`WARP_SELECTED_HITS`]. |
| 4466 | #[allow(dead_code)] // read only from the `#[cfg(test)]` gates |
| 4467 | pub(crate) fn warp_selected_hits() -> usize { |
| 4468 | WARP_SELECTED_HITS.with(|c| c.get()) |
| 4469 | } |
| 4470 | |
| 4471 | // lane-cwarp r1: how many COMPOUND_REFERENCE blocks had at least one |
| 4472 | // reference predicted through the per-ref GLOBAL warp (`av1_warp_plane` |
| 4473 | // into the compound intermediate) rather than translationally -- the |
| 4474 | // gate's proof that a `GLOBAL_GLOBALMV` block under a ROTZOOM/AFFINE model |
| 4475 | // really reached [`crate::warp::warp_affine_compound`]. |
| 4476 | thread_local! { |
| 4477 | static COMPOUND_WARP_HITS: std::cell::Cell<usize> = |
| 4478 | const { std::cell::Cell::new(0) }; |
| 4479 | } |
| 4480 | |
| 4481 | /// Current value of [`COMPOUND_WARP_HITS`]. |
| 4482 | #[allow(dead_code)] // read only from the `#[cfg(test)]` gates |
| 4483 | pub(crate) fn compound_warp_hits() -> usize { |
| 4484 | COMPOUND_WARP_HITS.with(|c| c.get()) |
| 4485 | } |
| 4486 | |
| 4487 | // lane-interp3 r3: the same proof narrowed to the 8x8 LEAF's own compound |
| 4488 | // arm ([`decode_inter_block8`]) -- that arm predicted both taps |
| 4489 | // translationally until r3, so a gate needs to tell a warped 8x8 compound |
| 4490 | // leaf apart from a warped 16x16+ one (both bump [`COMPOUND_WARP_HITS`]). |
| 4491 | thread_local! { |
| 4492 | static COMPOUND_WARP_HITS_8: std::cell::Cell<usize> = |
| 4493 | const { std::cell::Cell::new(0) }; |
| 4494 | } |
| 4495 | |
| 4496 | /// Current value of [`COMPOUND_WARP_HITS_8`]. |
| 4497 | #[allow(dead_code)] // read only from the `#[cfg(test)]` gates |
| 4498 | pub(crate) fn compound_warp_hits_8() -> usize { |
| 4499 | COMPOUND_WARP_HITS_8.with(|c| c.get()) |
| 4500 | } |
| 4501 | |
| 4502 | // lane-gmaffine r1: how many blocks built their prediction from a |
| 4503 | // SIX-parameter (`AFFINE`) global-motion model through |
| 4504 | // `crate::warp::global_warp_params` -- the gate's proof that a stream really |
| 4505 | // carried an AFFINE `global_motion_params` AND that a block was predicted |
| 4506 | // with it, not merely that the header parsed one. |
| 4507 | thread_local! { |
| 4508 | static AFFINE_GM_HITS: std::cell::Cell<usize> = |
| 4509 | const { std::cell::Cell::new(0) }; |
| 4510 | } |
| 4511 | |
| 4512 | /// Current value of [`AFFINE_GM_HITS`]. |
| 4513 | #[allow(dead_code)] // read only from the `#[cfg(test)]` gates |
| 4514 | pub(crate) fn affine_gm_hits() -> usize { |
| 4515 | AFFINE_GM_HITS.with(|c| c.get()) |
| 4516 | } |
| 4517 | |
| 4518 | // lane-av1gwarp12: how many 16x16+ blocks built their SINGLE-reference |
| 4519 | // prediction through `crate::warp::global_warp_params` with a FOUR-parameter |
| 4520 | // (`ROTZOOM`) model. Vanilla aomenc pins its global-motion search to ROTZOOM |
| 4521 | // (`global_motion_facade.c:24`), so this is the only >TRANSLATION single-ref |
| 4522 | // global-warp arm a real aomenc stream can reach (AFFINE needs the widened |
| 4523 | // side build and its own counter, [`AFFINE_GM_HITS`]). |
| 4524 | thread_local! { |
| 4525 | static ROTZOOM_GM_WARP_HITS: std::cell::Cell<usize> = |
| 4526 | const { std::cell::Cell::new(0) }; |
| 4527 | } |
| 4528 | |
| 4529 | /// Current value of [`ROTZOOM_GM_WARP_HITS`]. |
| 4530 | #[allow(dead_code)] // read only from the `#[cfg(test)]` gates |
| 4531 | pub(crate) fn rotzoom_gm_warp_hits() -> usize { |
| 4532 | ROTZOOM_GM_WARP_HITS.with(|c| c.get()) |
| 4533 | } |
| 4534 | |
| 4535 | // lane-t900 r9: rect blocks whose warp-sample top-right probe answers |
| 4536 | // differently at the block's WIDTH than at libaom's |
| 4537 | // `AOMMAX(xd->width, xd->height)` -- each one is a `num_proj_ref` that could |
| 4538 | // pick the wrong motion_mode alphabet. |
| 4539 | thread_local! { |
| 4540 | static TR_REACH_LONGER_SIDE_HITS: std::cell::Cell<usize> = const { std::cell::Cell::new(0) }; |
| 4541 | } |
| 4542 | |
| 4543 | /// Current value of [`TR_REACH_LONGER_SIDE_HITS`]. |
| 4544 | pub fn tr_reach_longer_side_hits() -> usize { |
| 4545 | TR_REACH_LONGER_SIDE_HITS.with(std::cell::Cell::get) |
| 4546 | } |
| 4547 | |
| 4548 | // lane-t900 r9: libaom `is_nontrans_global_motion` (mvref_common.h) returns 0 |
| 4549 | // for `AOMMIN(mi_size_wide[bsize], mi_size_high[bsize]) < 2`, i.e. any block |
| 4550 | // with a 4-px side is NEVER "non-translational global" and therefore ALWAYS |
| 4551 | // reads its interp-filter symbol. Counts the blocks where that guard flips |
| 4552 | // our answer -- a GLOBALMV/GLOBAL_GLOBALMV block with a non-TRANSLATION model |
| 4553 | // and a 4-px side, which we used to suppress the interp read on (a pre-mode |
| 4554 | // entropy desync one symbol wide). |
| 4555 | thread_local! { |
| 4556 | static GM_NONTRANS_SMALL_SIDE_HITS: std::cell::Cell<usize> = const { std::cell::Cell::new(0) }; |
| 4557 | } |
| 4558 | |
| 4559 | /// Current value of [`GM_NONTRANS_SMALL_SIDE_HITS`]. |
| 4560 | pub fn gm_nontrans_small_side_hits() -> usize { |
| 4561 | GM_NONTRANS_SMALL_SIDE_HITS.with(std::cell::Cell::get) |
| 4562 | } |
| 4563 | |
| 4564 | // lane-gmaffine r1: 8x8-leaf-only counters -- how many `BLOCK_8X8` leaves |
| 4565 | // coded `GLOBALMV` (`GLOBALMV_HITS_8`) and how many built a real |
| 4566 | // `WARPED_CAUSAL` local-warp prediction there (`WARP_HITS_8`, incremented |
| 4567 | // only once `find_projection` returned a usable model, unlike |
| 4568 | // `WARP_SELECTED_HITS` which counts the symbol). Both are the 8x8 gates' |
| 4569 | // proof that the leaf path itself fired, not the 16x16+ one. |
| 4570 | thread_local! { |
| 4571 | static GLOBALMV_HITS_8: std::cell::Cell<usize> = const { std::cell::Cell::new(0) }; |
| 4572 | static WARP_HITS_8: std::cell::Cell<usize> = const { std::cell::Cell::new(0) }; |
| 4573 | } |
| 4574 | |
| 4575 | /// Current value of [`GLOBALMV_HITS_8`]. |
| 4576 | #[allow(dead_code)] // read only from the `#[cfg(test)]` gates |
| 4577 | pub(crate) fn globalmv_hits_8() -> usize { |
| 4578 | GLOBALMV_HITS_8.with(|c| c.get()) |
| 4579 | } |
| 4580 | |
| 4581 | /// Current value of [`WARP_HITS_8`]. |
| 4582 | #[allow(dead_code)] // read only from the `#[cfg(test)]` gates |
| 4583 | pub(crate) fn warp_hits_8() -> usize { |
| 4584 | WARP_HITS_8.with(|c| c.get()) |
| 4585 | } |
| 4586 | |
| 4587 | // How many blocks decoded `interintra == 1` with the non-wedge blended |
| 4588 | // prediction applied (lane-interintra r1) -- the gate's proof that a |
| 4589 | // stream actually exercised interintra. |
| 4590 | thread_local! { |
| 4591 | static INTERINTRA_HITS: std::cell::Cell<usize> = |
| 4592 | const { std::cell::Cell::new(0) }; |
| 4593 | } |
| 4594 | |
| 4595 | /// Current value of [`INTERINTRA_HITS`]. |
| 4596 | #[allow(dead_code)] // read only from the `#[cfg(test)]` gates |
| 4597 | pub(crate) fn interintra_hits() -> usize { |
| 4598 | INTERINTRA_HITS.with(|c| c.get()) |
| 4599 | } |
| 4600 | |
| 4601 | // lane-midcut r1: of those, how many were RECTANGULAR (`write_w != write_h`) |
| 4602 | // -- the shape `interintra_blend` used to build a square `side x side` intra |
| 4603 | // predictor for, with `ii_size_scales`/`edges` taken at the square. Every |
| 4604 | // pre-existing interintra gate passed with that bug, so the rect arm needs |
| 4605 | // its own counter to stop being vacuous. |
| 4606 | thread_local! { |
| 4607 | static INTERINTRA_RECT_HITS: std::cell::Cell<usize> = const { std::cell::Cell::new(0) }; |
| 4608 | } |
| 4609 | |
| 4610 | /// Current value of [`INTERINTRA_RECT_HITS`]. |
| 4611 | pub fn interintra_rect_hits() -> usize { |
| 4612 | INTERINTRA_RECT_HITS.with(|c| c.get()) |
| 4613 | } |
| 4614 | |
| 4615 | // How many blocks decoded `comp_group_idx == 1` (masked COMPOUND_REFERENCE, |
| 4616 | // wedge or diffwtd) and had `compound_type`/`wedge_idx`/`wedge_sign`/ |
| 4617 | // `mask_type` consumed entropy-exact -- lane-maskcomp r1's proof that a |
| 4618 | // gate fixture actually exercises this alphabet, even though the block |
| 4619 | // still refuses (the mask blend itself is unported). |
| 4620 | thread_local! { |
| 4621 | static MASKED_COMPOUND_HITS: std::cell::Cell<usize> = |
| 4622 | const { std::cell::Cell::new(0) }; |
| 4623 | } |
| 4624 | |
| 4625 | /// Current value of [`MASKED_COMPOUND_HITS`]. |
| 4626 | #[allow(dead_code)] // read only from the `#[cfg(test)]` gates |
| 4627 | pub(crate) fn masked_compound_hits() -> usize { |
| 4628 | MASKED_COMPOUND_HITS.with(|c| c.get()) |
| 4629 | } |
| 4630 | |
| 4631 | // lane-intersub8 r4/r5: the NON-SQUARE half of `MASKED_COMPOUND_HITS` -- a |
| 4632 | // masked-compound block whose true footprint is rectangular, i.e. the case |
| 4633 | // whose `compound_type`/`wedge_idx` CDF row r4 fixed (it used to read the |
| 4634 | // square envelope's `BLOCK_SIZES_ALL` index). Square blocks cannot show that |
| 4635 | // defect, so a gate asserting `masked_compound_hits` alone stays vacuous for it. |
| 4636 | thread_local! { |
| 4637 | static RECT_MASKED_COMPOUND_HITS: std::cell::Cell<usize> = |
| 4638 | const { std::cell::Cell::new(0) }; |
| 4639 | } |
| 4640 | |
| 4641 | /// Current value of [`RECT_MASKED_COMPOUND_HITS`]. |
| 4642 | #[allow(dead_code)] // read only from the `#[cfg(test)]` gates |
| 4643 | pub(crate) fn rect_masked_compound_hits() -> usize { |
| 4644 | RECT_MASKED_COMPOUND_HITS.with(|c| c.get()) |
| 4645 | } |
| 4646 | |
| 4647 | // How many blocks decoded a real `COMPOUND_WEDGE` block (`comp_group_idx == |
| 4648 | // 1`, `compound_type == 0`) -- lane-wedge r3's proof a gate fixture |
| 4649 | // actually exercised the wedge blend, not just the DIFFWTD half of |
| 4650 | // `MASKED_COMPOUND_HITS`. |
| 4651 | thread_local! { |
| 4652 | static WEDGE_HITS: std::cell::Cell<usize> = |
| 4653 | const { std::cell::Cell::new(0) }; |
| 4654 | } |
| 4655 | |
| 4656 | /// Current value of [`WEDGE_HITS`]. |
| 4657 | #[allow(dead_code)] // read only from the `#[cfg(test)]` gates |
| 4658 | pub(crate) fn wedge_hits() -> usize { |
| 4659 | WEDGE_HITS.with(|c| c.get()) |
| 4660 | } |
| 4661 | |
| 4662 | // How many blocks decoded a real wedge-INTERINTRA block |
| 4663 | // (`interintra == 1 && use_wedge_interintra == 1`) -- lane-wii r2's proof |
| 4664 | // a gate fixture exercised the wedge blend (fixed sign 0), not just the |
| 4665 | // smooth arm counted by `INTERINTRA_HITS`. |
| 4666 | thread_local! { |
| 4667 | static WII_HITS: std::cell::Cell<usize> = |
| 4668 | const { std::cell::Cell::new(0) }; |
| 4669 | } |
| 4670 | |
| 4671 | /// Current value of [`WII_HITS`]. |
| 4672 | #[allow(dead_code)] // read only from the `#[cfg(test)]` gates |
| 4673 | pub(crate) fn wii_hits() -> usize { |
| 4674 | WII_HITS.with(|c| c.get()) |
| 4675 | } |
| 4676 | |
| 4677 | // lane-sbrect10 r1: how many INTRA blocks read `uv_mode` off the 13-symbol |
| 4678 | // NO-CFL alphabet *because their size excludes CFL* (libaom `is_cfl_allowed`: |
| 4679 | // wide <= 32 && high <= 32). The witness that a gate stream really carries a |
| 4680 | // 64-axis intra block -- the shape whose 14-symbol read desynced every inter |
| 4681 | // frame carrying one. |
| 4682 | thread_local! { |
| 4683 | static NOCFL_UV_MODE_HITS: std::cell::Cell<usize> = const { std::cell::Cell::new(0) }; |
| 4684 | } |
| 4685 | |
| 4686 | /// Current value of [`NOCFL_UV_MODE_HITS`]. |
| 4687 | #[allow(dead_code)] // read only from the `#[cfg(test)]` gates |
| 4688 | pub(crate) fn nocfl_uv_mode_hits() -> usize { |
| 4689 | NOCFL_UV_MODE_HITS.with(|c| c.get()) |
| 4690 | } |
| 4691 | |
| 4692 | /// lane-inter16ab r5: the six RECTANGULAR wedge shapes, in this order -- |
| 4693 | /// the counter slots of [`RECT_WEDGE_HITS`]/[`RECT_WII_HITS`]. |
| 4694 | pub(crate) const RECT_WEDGE_SHAPES: [(usize, usize); 6] = |
| 4695 | [(8, 16), (16, 8), (16, 32), (32, 16), (8, 32), (32, 8)]; |
| 4696 | |
| 4697 | fn rect_wedge_slot(bw: usize, bh: usize) -> Option<usize> { |
| 4698 | RECT_WEDGE_SHAPES.iter().position(|&s| s == (bw, bh)) |
| 4699 | } |
| 4700 | |
| 4701 | // lane-inter16ab r5: how many COMPOUND_WEDGE / wedge-INTERINTRA blocks used a |
| 4702 | // RECTANGULAR codebook row (hgtw/hltw), per shape -- the gate's proof that |
| 4703 | // the rect wedge masks lifted this round actually fired, not just the square |
| 4704 | // ones already counted by `WEDGE_HITS`/`WII_HITS`. |
| 4705 | thread_local! { |
| 4706 | static RECT_WEDGE_HITS: std::cell::Cell<[usize; 6]> = const { std::cell::Cell::new([0; 6]) }; |
| 4707 | static RECT_WII_HITS: std::cell::Cell<[usize; 6]> = const { std::cell::Cell::new([0; 6]) }; |
| 4708 | } |
| 4709 | |
| 4710 | /// Current value of [`RECT_WEDGE_HITS`], indexed by [`RECT_WEDGE_SHAPES`]. |
| 4711 | pub(crate) fn rect_wedge_hits() -> [usize; 6] { |
| 4712 | RECT_WEDGE_HITS.with(|c| c.get()) |
| 4713 | } |
| 4714 | |
| 4715 | /// Current value of [`RECT_WII_HITS`], indexed by [`RECT_WEDGE_SHAPES`]. |
| 4716 | pub(crate) fn rect_wii_hits() -> [usize; 6] { |
| 4717 | RECT_WII_HITS.with(|c| c.get()) |
| 4718 | } |
| 4719 | |
| 4720 | // lane-inter4 r4: how many 16x8/8x16 INTER LEAVES actually ran the OBMC |
| 4721 | // blend -- the gate's proof that `--enable-obmc=1` reached this shape (the |
| 4722 | // r3 refusal's replacement: prefilt frames are bit-exact vs an instrumented |
| 4723 | // aomdec, neighbour list included). |
| 4724 | thread_local! { |
| 4725 | static OBMC_RECT_LEAF_HITS: std::cell::Cell<usize> = const { std::cell::Cell::new(0) }; |
| 4726 | } |
| 4727 | |
| 4728 | /// Current value of [`OBMC_RECT_LEAF_HITS`]. |
| 4729 | pub(crate) fn obmc_rect_leaf_hits() -> usize { |
| 4730 | OBMC_RECT_LEAF_HITS.with(|c| c.get()) |
| 4731 | } |
| 4732 | |
| 4733 | // lane-obmcrec r1: how many OBMC neighbours were taken from the SECOND half of |
| 4734 | // a 4-wide/4-tall (1:4 strip) chroma pair -- the case whose interp filter used |
| 4735 | // to be read from the pair's FIRST mi, which is a different block and, when |
| 4736 | // that half is intra, carries the `[3, 3]` "no filter" sentinel (the three |
| 4737 | // 1080p 10-bit film cuts refused there). The gate's proof the shape fired. |
| 4738 | thread_local! { |
| 4739 | static OBMC_PAIR_FILTER_HITS: std::cell::Cell<usize> = const { std::cell::Cell::new(0) }; |
| 4740 | } |
| 4741 | |
| 4742 | /// Current value of [`OBMC_PAIR_FILTER_HITS`]. |
| 4743 | pub(crate) fn obmc_pair_filter_hits() -> usize { |
| 4744 | OBMC_PAIR_FILTER_HITS.with(|c| c.get()) |
| 4745 | } |
| 4746 | |
| 4747 | // lane-t900 r14: how many OBMC neighbour predictions run PAST the frame edge |
| 4748 | // -- libaom's `op_mi_size` is `AOMMIN(xd->width|height, mi_step)`, the block's |
| 4749 | // own side, so a block overhanging the bottom/right still predicts the full |
| 4750 | // neighbour strip (the destination write is what clips). We used to shrink the |
| 4751 | // prediction to the visible part, which at 4 chroma rows/cols also swaps the |
| 4752 | // vertical/horizontal kernel to the 4-tap one. The gate's proof the shape fired. |
| 4753 | thread_local! { |
| 4754 | static OBMC_EDGE_SPAN_HITS: std::cell::Cell<usize> = const { std::cell::Cell::new(0) }; |
| 4755 | } |
| 4756 | |
| 4757 | /// Current value of [`OBMC_EDGE_SPAN_HITS`]. |
| 4758 | #[allow(dead_code)] // read only from the `#[cfg(test)]` gates |
| 4759 | pub(crate) fn obmc_edge_span_hits() -> usize { |
| 4760 | OBMC_EDGE_SPAN_HITS.with(|c| c.get()) |
| 4761 | } |
| 4762 | |
| 4763 | /// Current value of [`OBMC_HITS_8`]. |
| 4764 | #[allow(dead_code)] // read only from the `#[cfg(test)]` gates |
| 4765 | pub(crate) fn obmc_hits_8() -> usize { |
| 4766 | OBMC_HITS_8.with(|c| c.get()) |
| 4767 | } |
| 4768 | |
| 4769 | // lane-rectgate r1: how many 32x32 quadrants actually decoded a |
| 4770 | // `PARTITION_HORZ_B` split (the one extended/ab partition this decoder's |
| 4771 | // inter-frame tile loop decodes -- `PARTITION_HORZ`/`VERT`/`HORZ_A`/`VERT_A`/ |
| 4772 | // `VERT_B` all still fall into the generic "a partition type this encoder |
| 4773 | // never writes" refusal below) -- the gate's proof that a free-partition |
| 4774 | // aomenc stream actually exercised a non-`NONE`/`SPLIT` partition rather than |
| 4775 | // coincidentally never selecting one. |
| 4776 | thread_local! { |
| 4777 | static EXTENDED_PARTITION_HITS: std::cell::Cell<usize> = |
| 4778 | const { std::cell::Cell::new(0) }; |
| 4779 | } |
| 4780 | |
| 4781 | /// Current value of [`EXTENDED_PARTITION_HITS`]. |
| 4782 | #[allow(dead_code)] // read only from the `#[cfg(test)]` gates |
| 4783 | pub(crate) fn extended_partition_hits() -> usize { |
| 4784 | EXTENDED_PARTITION_HITS.with(|c| c.get()) |
| 4785 | } |
| 4786 | |
| 4787 | // lane-partitions r1: how many 32x32 quadrants decoded a real |
| 4788 | // `PARTITION_HORZ`/`PARTITION_VERT` (two true rectangular strips, unlike |
| 4789 | // `PARTITION_HORZ_B`'s square-context stand-in) -- the free-partition |
| 4790 | // gate's proof this round's arms actually fired. |
| 4791 | thread_local! { |
| 4792 | static RECT_PARTITION_HITS: std::cell::Cell<usize> = |
| 4793 | const { std::cell::Cell::new(0) }; |
| 4794 | } |
| 4795 | |
| 4796 | /// Current value of [`RECT_PARTITION_HITS`]. |
| 4797 | #[allow(dead_code)] // read only from the `#[cfg(test)]` gates |
| 4798 | pub(crate) fn rect_partition_hits() -> usize { |
| 4799 | RECT_PARTITION_HITS.with(|c| c.get()) |
| 4800 | } |
| 4801 | |
| 4802 | // lane-inter8 r1: how many superblocks an inter tile coded as a single |
| 4803 | // whole-SB `PARTITION_NONE` 64x64 inter block (the case aomenc picks for |
| 4804 | // static content, previously a named refusal). |
| 4805 | thread_local! { |
| 4806 | /// lane-sb128c r1: how many 128x128 `PARTITION_NONE` roots this INTER tile |
| 4807 | /// path decoded as one whole block (its gate's own witness). |
| 4808 | pub(crate) static INTER_SB128_NONE_HITS: std::cell::Cell<usize> = const { std::cell::Cell::new(0) }; |
| 4809 | /// 128x64 / 64x128 INTER blocks decoded at the 128 root (lane-sb128c r7). |
| 4810 | pub(crate) static INTER_SB128_HORZ_HITS: std::cell::Cell<usize> = const { std::cell::Cell::new(0) }; |
| 4811 | pub(crate) static INTER_SB128_VERT_HITS: std::cell::Cell<usize> = const { std::cell::Cell::new(0) }; |
| 4812 | } |
| 4813 | |
| 4814 | /// Current value of [`INTER_SB128_HORZ_HITS`], for a gate's before/after delta. |
| 4815 | pub(crate) fn inter_sb128_horz_hits() -> usize { |
| 4816 | INTER_SB128_HORZ_HITS.with(std::cell::Cell::get) |
| 4817 | } |
| 4818 | |
| 4819 | /// Current value of [`INTER_SB128_VERT_HITS`], for a gate's before/after delta. |
| 4820 | pub(crate) fn inter_sb128_vert_hits() -> usize { |
| 4821 | INTER_SB128_VERT_HITS.with(std::cell::Cell::get) |
| 4822 | } |
| 4823 | |
| 4824 | /// Current value of [`INTER_SB128_NONE_HITS`], for a gate's before/after delta. |
| 4825 | pub fn inter_sb128_none_hits() -> usize { |
| 4826 | INTER_SB128_NONE_HITS.with(std::cell::Cell::get) |
| 4827 | } |
| 4828 | |
| 4829 | thread_local! { |
| 4830 | /// lane-sb128c r3: how many INTRA-coded 128x128 blocks inside an INTER |
| 4831 | /// frame this decoder reconstructed (mu-chunk chroma, four TX_64X64 luma |
| 4832 | /// units) -- the gate's witness that the lifted refusal's case fired. |
| 4833 | pub(crate) static INTRA_128_IN_INTER_HITS: std::cell::Cell<usize> = const { std::cell::Cell::new(0) }; |
| 4834 | } |
| 4835 | |
| 4836 | /// Current value of [`INTRA_128_IN_INTER_HITS`], for a gate's before/after delta. |
| 4837 | pub fn intra_128_in_inter_hits() -> usize { |
| 4838 | INTRA_128_IN_INTER_HITS.with(std::cell::Cell::get) |
| 4839 | } |
| 4840 | |
| 4841 | thread_local! { |
| 4842 | static INTER_SB_NONE_HITS: std::cell::Cell<usize> = |
| 4843 | const { std::cell::Cell::new(0) }; |
| 4844 | } |
| 4845 | |
| 4846 | /// Current value of [`INTER_SB_NONE_HITS`]. |
| 4847 | #[allow(dead_code)] // read only from the `#[cfg(test)]` gates |
| 4848 | pub(crate) fn inter_sb_none_hits() -> usize { |
| 4849 | INTER_SB_NONE_HITS.with(|c| c.get()) |
| 4850 | } |
| 4851 | |
| 4852 | // lane-inter8 r1: how many INTERIOR (not frame-edge-straddling) 16x16 inter |
| 4853 | // blocks coded a real `PARTITION_SPLIT` into four 8x8 leaves -- the straddle |
| 4854 | // path has always run that loop, so only this counter proves the newly-lifted |
| 4855 | // alphabet value fired. |
| 4856 | thread_local! { |
| 4857 | static INTER_SUB16_SPLIT_HITS: std::cell::Cell<usize> = |
| 4858 | const { std::cell::Cell::new(0) }; |
| 4859 | } |
| 4860 | |
| 4861 | /// Current value of [`INTER_SUB16_SPLIT_HITS`]. |
| 4862 | #[allow(dead_code)] // read only from the `#[cfg(test)]` gates |
| 4863 | pub(crate) fn inter_sub16_split_hits() -> usize { |
| 4864 | INTER_SUB16_SPLIT_HITS.with(|c| c.get()) |
| 4865 | } |
| 4866 | |
| 4867 | // lane-t900 r28: how many 16x16 inter splits reached their four 8x8 leaves |
| 4868 | // while at least one reference picture was SCALED (superres) -- the counter |
| 4869 | // behind the lifted "8x8 partition leaf under a scaled reference" refusal. |
| 4870 | thread_local! { |
| 4871 | static SCALED_LEAF8_HITS: std::cell::Cell<usize> = |
| 4872 | const { std::cell::Cell::new(0) }; |
| 4873 | } |
| 4874 | |
| 4875 | /// Current value of [`SCALED_LEAF8_HITS`]. |
| 4876 | pub fn scaled_leaf8_hits() -> usize { |
| 4877 | SCALED_LEAF8_HITS.with(|c| c.get()) |
| 4878 | } |
| 4879 | |
| 4880 | // lane-t900 r28: how many sub-8x8 inter blocks (the 4x4 split and the 8x4/4x8 |
| 4881 | // rect pair) were predicted from a SCALED reference -- the counter behind the |
| 4882 | // lifted "sub-8x8 inter block under a scaled reference" refusal. |
| 4883 | thread_local! { |
| 4884 | static SCALED_SUB8_HITS: std::cell::Cell<usize> = |
| 4885 | const { std::cell::Cell::new(0) }; |
| 4886 | } |
| 4887 | |
| 4888 | /// Current value of [`SCALED_SUB8_HITS`]. |
| 4889 | pub fn scaled_sub8_hits() -> usize { |
| 4890 | SCALED_SUB8_HITS.with(|c| c.get()) |
| 4891 | } |
| 4892 | |
| 4893 | // lane-rectwire r2: how many `PARTITION_HORZ`/`VERT` strips actually decoded |
| 4894 | // real (non-skip) coefficients through [`read_coeffs_rect`] -- proves the |
| 4895 | // rect coefficient reader itself fired, not just the strip-level partition |
| 4896 | // symbol [`RECT_PARTITION_HITS`] already counts. |
| 4897 | thread_local! { |
| 4898 | static RECT_COEFF_HITS: std::cell::Cell<usize> = |
| 4899 | const { std::cell::Cell::new(0) }; |
| 4900 | } |
| 4901 | |
| 4902 | /// Current value of [`RECT_COEFF_HITS`]. |
| 4903 | #[allow(dead_code)] // read only from the `#[cfg(test)]` gates |
| 4904 | pub(crate) fn rect_coeff_hits() -> usize { |
| 4905 | RECT_COEFF_HITS.with(|c| c.get()) |
| 4906 | } |
| 4907 | |
| 4908 | // lane-rectx r1: how many below-16x16 HORZ/VERT leaves ([`decode_leaf_rect`]) |
| 4909 | // decoded real (non-skip) TX_16X8/TX_8X16 coefficients -- the counter the |
| 4910 | // charter's real-aomenc gate hard-asserts. |
| 4911 | thread_local! { |
| 4912 | static RECT_LEAF_COEFF_HITS: std::cell::Cell<usize> = |
| 4913 | const { std::cell::Cell::new(0) }; |
| 4914 | } |
| 4915 | |
| 4916 | /// Current value of [`RECT_LEAF_COEFF_HITS`]. |
| 4917 | #[allow(dead_code)] // read only from the `#[cfg(test)]` gates |
| 4918 | pub(crate) fn rect_leaf_coeff_hits() -> usize { |
| 4919 | RECT_LEAF_COEFF_HITS.with(|c| c.get()) |
| 4920 | } |
| 4921 | |
| 4922 | // lane-intrarect r1: how many intra-coded HORZ/VERT strips decoded on the |
| 4923 | // INTER block path ([`decode_intra_rect_in_inter`]), per shape class -- |
| 4924 | // [0] the superblock-level 64x32/32x64, [1] 32x16/16x32, [2] 16x8/8x16. |
| 4925 | // Every one of them was the named refusal "an intra-coded HORZ/VERT strip |
| 4926 | // needs rectangular intra prediction" before this round. |
| 4927 | thread_local! { |
| 4928 | static INTRA_RECT_IN_INTER_HITS: std::cell::Cell<[usize; 3]> = |
| 4929 | const { std::cell::Cell::new([0; 3]) }; |
| 4930 | } |
| 4931 | |
| 4932 | /// Current value of [`INTRA_RECT_IN_INTER_HITS`] for one shape class |
| 4933 | /// (0 = 64x32/32x64, 1 = 32x16/16x32, 2 = 16x8/8x16). |
| 4934 | #[allow(dead_code)] // read only from the `#[cfg(test)]` gates |
| 4935 | pub(crate) fn intra_rect_strip_in_inter_hits(class: usize) -> usize { |
| 4936 | INTRA_RECT_IN_INTER_HITS.with(|c| c.get()[class]) |
| 4937 | } |
| 4938 | |
| 4939 | // lane-rect64port r1 (ported by hand from lane-intra64split 5ffbaf3): coded |
| 4940 | // (non-skip) 64-level HORZ/VERT intra strips whose luma transform is the |
| 4941 | // un-split TX_64X32/TX_32X64 -- the corner-cropped 32x32 coefficient read of |
| 4942 | // [`decode_rect_split`], the shape both 10-bit 3840x1608 film cuts stop on. |
| 4943 | // Split by ORIENTATION (index 0 = 64x32 wide / HORZ, 1 = 32x64 tall / VERT): |
| 4944 | // a single counter cannot tell an axis swap from a working arm |
| 4945 | // (class [[scan-weights-cross-axis]]), so the gate asserts both cells. |
| 4946 | thread_local! { |
| 4947 | static RECT64_CORNER_TU_HITS: std::cell::Cell<[usize; 2]> = |
| 4948 | const { std::cell::Cell::new([0; 2]) }; |
| 4949 | } |
| 4950 | |
| 4951 | /// Current value of [`RECT64_CORNER_TU_HITS`] for one orientation |
| 4952 | /// (0 = 64x32, 1 = 32x64). |
| 4953 | pub(crate) fn rect64_corner_tu_hits(orient: usize) -> usize { |
| 4954 | RECT64_CORNER_TU_HITS.with(|c| c.get()[orient]) |
| 4955 | } |
| 4956 | |
| 4957 | // lane-intrasplit r1: intra HORZ/VERT strips inside an INTER frame whose luma |
| 4958 | // transform is SPLIT, bucketed by the read `tx_depth` (index 1 and 2; index 0 |
| 4959 | // is never incremented) -- the gate's proof that the lifted path ran per depth. |
| 4960 | thread_local! { |
| 4961 | static INTRA_RECT_IN_INTER_SPLIT_TX_HITS: std::cell::Cell<[usize; 3]> = |
| 4962 | const { std::cell::Cell::new([0; 3]) }; |
| 4963 | } |
| 4964 | |
| 4965 | // lane-t900 r21: rect intra strips inside an inter frame that read the |
| 4966 | // screen-content palette syntax (`allow_screen_content_tools` set and the |
| 4967 | // strip's own shape admitted by `palette_bsize_ctx_wh`) -- the site a blanket |
| 4968 | // refusal used to guard, so this counter is what keeps its witness gate |
| 4969 | // non-vacuous. |
| 4970 | thread_local! { |
| 4971 | static INTRA_RECT_IN_INTER_SCREEN_HITS: std::cell::Cell<usize> = const { std::cell::Cell::new(0) }; |
| 4972 | } |
| 4973 | |
| 4974 | /// Current value of [`INTRA_RECT_IN_INTER_SCREEN_HITS`]. |
| 4975 | #[allow(dead_code)] // read only from the `#[cfg(test)]` gates |
| 4976 | pub(crate) fn intra_rect_in_inter_screen_hits() -> usize { |
| 4977 | INTRA_RECT_IN_INTER_SCREEN_HITS.with(std::cell::Cell::get) |
| 4978 | } |
| 4979 | |
| 4980 | /// Resets [`INTRA_RECT_IN_INTER_SCREEN_HITS`] before a decode. |
| 4981 | #[allow(dead_code)] // read only from the `#[cfg(test)]` gates |
| 4982 | pub(crate) fn reset_intra_rect_in_inter_screen_hits() { |
| 4983 | INTRA_RECT_IN_INTER_SCREEN_HITS.with(|c| c.set(0)); |
| 4984 | } |
| 4985 | |
| 4986 | /// Current value of [`INTRA_RECT_IN_INTER_SPLIT_TX_HITS`], indexed by tx depth. |
| 4987 | #[allow(dead_code)] // read only from the `#[cfg(test)]` gates |
| 4988 | pub(crate) fn intra_rect_in_inter_split_tx_hits() -> [usize; 3] { |
| 4989 | INTRA_RECT_IN_INTER_SPLIT_TX_HITS.with(std::cell::Cell::get) |
| 4990 | } |
| 4991 | |
| 4992 | // lane-av1txbands: how many `tx_depth`/`txfm_split` reads the deblock-grid |
| 4993 | // approximation would have resolved off a DIFFERENT CDF row than the real |
| 4994 | // `TXFM_CONTEXT` bands do -- the exact blocks this band read fixes, on ANY |
| 4995 | // frame (lane-midcut r2 had it inter-frame-only). A plain "rect/intra block" |
| 4996 | // tally is green on the old code too (class gate-blind-to-feature). |
| 4997 | thread_local! { |
| 4998 | static TXCTX_DEBLOCK_DIVERGENCE_HITS: std::cell::Cell<usize> = |
| 4999 | const { std::cell::Cell::new(0) }; |
| 5000 | } |
| 5001 | |
| 5002 | /// Current value of [`TXCTX_DEBLOCK_DIVERGENCE_HITS`]. |
| 5003 | #[allow(dead_code)] // gate counter accessor with no reader yet; the counter itself is live |
| 5004 | pub(crate) fn txctx_deblock_divergence_hits() -> usize { |
| 5005 | TXCTX_DEBLOCK_DIVERGENCE_HITS.with(std::cell::Cell::get) |
| 5006 | } |
| 5007 | |
| 5008 | // lane-intra14 r1: how many intra-coded 1:4 strips decoded on the INTER |
| 5009 | // block path, per shape -- [0] 64x16, [1] 16x64, [2] 32x8, [3] 8x32. Every |
| 5010 | // one of them was the named refusal "an intra-coded 1:4 (or other non-2:1) |
| 5011 | // rect strip on the inter block path" before this round. |
| 5012 | thread_local! { |
| 5013 | static INTRA_RECT4_IN_INTER_HITS: std::cell::Cell<[usize; 4]> = |
| 5014 | const { std::cell::Cell::new([0; 4]) }; |
| 5015 | } |
| 5016 | |
| 5017 | /// Current value of [`INTRA_RECT4_IN_INTER_HITS`] for one shape |
| 5018 | /// (0 = 64x16, 1 = 16x64, 2 = 32x8, 3 = 8x32). |
| 5019 | pub(crate) fn intra_rect4_strip_in_inter_hits(shape: usize) -> usize { |
| 5020 | INTRA_RECT4_IN_INTER_HITS.with(|c| c.get()[shape]) |
| 5021 | } |
| 5022 | |
| 5023 | // lane-intra14 r1: set while an INTRA block of an INTER frame is decoded |
| 5024 | // through one of the key-frame rect readers ([`decode_block_rect4`], |
| 5025 | // [`decode_block_rect64`]). `Some((size_group, skip))` tells |
| 5026 | // [`read_intra_mode_rect`] that (a) `skip`/segment id/cdef/delta-q were |
| 5027 | // already read by [`decode_inter_block`] and (b) the luma mode comes off |
| 5028 | // `y_mode_cdf[size_group_lookup[bsize]]` (libaom `read_intra_block_mode_info`, |
| 5029 | // decodemv.c:1189), never the key frame's above/left `kf_y_mode` pair. |
| 5030 | |
| 5031 | // lane-rectx r5: how many times a NON-strip `kf_y_mode` reader |
| 5032 | // ([`decode_block`], [`decode_block_rect`], [`decode_block_rect64`]) took an |
| 5033 | // above/left mode from the mi-exact map that DIFFERS from its coarse 16x16 |
| 5034 | // slot -- i.e. how often the r5 defect would have picked the wrong CDF row. |
| 5035 | thread_local! { |
| 5036 | static MODE_MI_OVERRIDE_HITS: std::cell::Cell<usize> = |
| 5037 | const { std::cell::Cell::new(0) }; |
| 5038 | } |
| 5039 | |
| 5040 | /// Current value of [`MODE_MI_OVERRIDE_HITS`]. |
| 5041 | #[allow(dead_code)] // read only from the `#[cfg(test)]` gates |
| 5042 | pub(crate) fn mode_mi_override_hits() -> usize { |
| 5043 | MODE_MI_OVERRIDE_HITS.with(|c| c.get()) |
| 5044 | } |
| 5045 | |
| 5046 | // lane-modectx r1: how many times a `kf_y_mode`/`y_mode` neighbour read at a |
| 5047 | // TILE edge found its coarse fallback band holding a NON-DC mode -- i.e. a |
| 5048 | // mode from the previous tile, where libaom's `up_available`/`left_available` |
| 5049 | // make the neighbour DC_PRED. Measured 0 over 20 two-tile-column streams: |
| 5050 | // `above_mode` is cleared per tile in `Neighbours::start_tile` and `left_mode` |
| 5051 | // per superblock row in `start_row`, so the luma fallback cannot leak the way |
| 5052 | // lane-mtfix's `above_uv_mode` one did. The gate asserts it stays 0 -- this is |
| 5053 | // the invariant that makes the coarse fallback safe. |
| 5054 | thread_local! { |
| 5055 | static MODE_TILE_EDGE_COARSE_LEAKS: std::cell::Cell<usize> = |
| 5056 | const { std::cell::Cell::new(0) }; |
| 5057 | } |
| 5058 | |
| 5059 | /// Current value of [`MODE_TILE_EDGE_COARSE_LEAKS`]. |
| 5060 | #[allow(dead_code)] // read only from the `#[cfg(test)]` gates |
| 5061 | pub(crate) fn mode_tile_edge_coarse_leaks() -> usize { |
| 5062 | MODE_TILE_EDGE_COARSE_LEAKS.with(|c| c.get()) |
| 5063 | } |
| 5064 | |
| 5065 | // lane-cfl r1: how many times a chroma intra-edge filter-type read |
| 5066 | // (`get_filt_type`, libaom reconintra.c) took an above/left UV mode from the |
| 5067 | // mi-exact map that DIFFERS from its coarse 16x16 slot -- i.e. how often the |
| 5068 | // r1 defect would have filtered a chroma edge with the wrong strength. |
| 5069 | thread_local! { |
| 5070 | static UV_MODE_MI_OVERRIDE_HITS: std::cell::Cell<usize> = |
| 5071 | const { std::cell::Cell::new(0) }; |
| 5072 | } |
| 5073 | |
| 5074 | /// Current value of [`UV_MODE_MI_OVERRIDE_HITS`]. |
| 5075 | #[allow(dead_code)] // read only from the `#[cfg(test)]` gates |
| 5076 | pub(crate) fn uv_mode_mi_override_hits() -> usize { |
| 5077 | UV_MODE_MI_OVERRIDE_HITS.with(|c| c.get()) |
| 5078 | } |
| 5079 | |
| 5080 | // lane-t900 r12: how many chroma edge-filter-type reads took their above/left |
| 5081 | // `uv_mode` from the mi GRID where the one-slot-per-column map would have |
| 5082 | // answered differently -- the sub-8x8 pair case where the pair's own first |
| 5083 | // half had already overwritten the column slot the chroma-ref half reads. |
| 5084 | thread_local! { |
| 5085 | static UV_MODE_GRID_OVERRIDE_HITS: std::cell::Cell<usize> = |
| 5086 | const { std::cell::Cell::new(0) }; |
| 5087 | } |
| 5088 | |
| 5089 | /// Current value of [`UV_MODE_GRID_OVERRIDE_HITS`]. |
| 5090 | pub fn uv_mode_grid_override_hits() -> usize { |
| 5091 | UV_MODE_GRID_OVERRIDE_HITS.with(|c| c.get()) |
| 5092 | } |
| 5093 | |
| 5094 | // lane-t900 r8: how many intra edge-filter-type reads (luma + chroma) the |
| 5095 | // INTER neighbour stamp saved: the coarse SUB band still held a SMOOTH mode |
| 5096 | // from an earlier block in that pixel row/column while the mi-exact map -- |
| 5097 | // which an inter block only started writing this round -- correctly says the |
| 5098 | // neighbour is not smooth (libaom `is_smooth`: an inter neighbour's `mode` is |
| 5099 | // an INTER mode, and its `uv_mode` "is not set for inter blocks"). |
| 5100 | thread_local! { |
| 5101 | static INTER_NEIGHBOUR_NOT_SMOOTH_HITS: std::cell::Cell<usize> = |
| 5102 | const { std::cell::Cell::new(0) }; |
| 5103 | } |
| 5104 | |
| 5105 | /// Current value of [`INTER_NEIGHBOUR_NOT_SMOOTH_HITS`]. |
| 5106 | #[allow(dead_code)] // read only from the `#[cfg(test)]` gates |
| 5107 | pub(crate) fn inter_neighbour_not_smooth_hits() -> usize { |
| 5108 | INTER_NEIGHBOUR_NOT_SMOOTH_HITS.with(|c| c.get()) |
| 5109 | } |
| 5110 | |
| 5111 | // lane-mtfix r1: how many times a chroma intra-edge filter-type read was |
| 5112 | // suppressed at a TILE edge that is not a frame edge -- the above/left |
| 5113 | // `uv_mode` neighbour of a block on a tile's first mi row/column lives in |
| 5114 | // another tile, so libaom's `above_mbmi`/`left_mbmi` are NULL there. The |
| 5115 | // coarse [`SUB`] maps are frame-sized and (`above_uv_mode`) not cleared per |
| 5116 | // tile, so without the guard in [`Neighbours::smooth_uv_neighbour`] the tile |
| 5117 | // above would decide this tile's chroma edge filter strength. |
| 5118 | thread_local! { |
| 5119 | static UV_TILE_EDGE_SUPPRESSED_HITS: std::cell::Cell<usize> = |
| 5120 | const { std::cell::Cell::new(0) }; |
| 5121 | } |
| 5122 | |
| 5123 | /// Current value of [`UV_TILE_EDGE_SUPPRESSED_HITS`]. |
| 5124 | #[allow(dead_code)] // read only from the `#[cfg(test)]` gates |
| 5125 | pub(crate) fn uv_tile_edge_suppressed_hits() -> usize { |
| 5126 | UV_TILE_EDGE_SUPPRESSED_HITS.with(|c| c.get()) |
| 5127 | } |
| 5128 | |
| 5129 | // lane-cfl r1 gate counters: `UV_CFL_PRED` blocks (every `cfl_alpha_signs` |
| 5130 | // read) and chroma blocks with a NONZERO `angle_delta_uv` -- the two block |
| 5131 | // shapes the r1 chroma defect showed up on. |
| 5132 | thread_local! { |
| 5133 | static CFL_BLOCK_HITS: std::cell::Cell<usize> = const { std::cell::Cell::new(0) }; |
| 5134 | static UV_ANGLE_DELTA_HITS: std::cell::Cell<usize> = const { std::cell::Cell::new(0) }; |
| 5135 | } |
| 5136 | |
| 5137 | /// Current value of [`CFL_BLOCK_HITS`]. |
| 5138 | #[allow(dead_code)] // read only from the `#[cfg(test)]` gates |
| 5139 | pub(crate) fn cfl_block_hits() -> usize { |
| 5140 | CFL_BLOCK_HITS.with(|c| c.get()) |
| 5141 | } |
| 5142 | |
| 5143 | /// Current value of [`UV_ANGLE_DELTA_HITS`]. |
| 5144 | #[allow(dead_code)] // read only from the `#[cfg(test)]` gates |
| 5145 | pub(crate) fn uv_angle_delta_hits() -> usize { |
| 5146 | UV_ANGLE_DELTA_HITS.with(|c| c.get()) |
| 5147 | } |
| 5148 | |
| 5149 | // lane-sbpart r2: how many superblock-level `PARTITION_HORZ`/`PARTITION_VERT` |
| 5150 | // blocks (two true 64x32/32x64 strips, [`decode_block_rect64`]) fired -- the |
| 5151 | // gate's proof this round's arms actually reached a real block, not just |
| 5152 | // that `partition_w64` read one of those two symbol values. |
| 5153 | thread_local! { |
| 5154 | static SB_RECT_HITS: std::cell::Cell<usize> = |
| 5155 | const { std::cell::Cell::new(0) }; |
| 5156 | } |
| 5157 | |
| 5158 | // Current value of [`SB_RECT_HITS`]. |
| 5159 | // lane-hgkf r1: the frame-edge partition bit, one level each. Slot 0 = a |
| 5160 | // 64-level gathered edge bit read as HORZ/VERT, 1 = the same bit read as |
| 5161 | // SPLIT, 2/3 = the 32-level pair. A gate asserting slot 0 or 2 proves the |
| 5162 | // non-SPLIT edge arm actually decoded (before this round the bit was read and |
| 5163 | // thrown away, so that arm was unreachable). |
| 5164 | thread_local! { |
| 5165 | static EDGE_PART_HITS: std::cell::Cell<[usize; 4]> = const { std::cell::Cell::new([0; 4]) }; |
| 5166 | } |
| 5167 | |
| 5168 | /// Current value of [`EDGE_PART_HITS`]. |
| 5169 | #[allow(dead_code)] // read only from the `#[cfg(test)]` gates |
| 5170 | pub(crate) fn edge_part_hits() -> [usize; 4] { |
| 5171 | EDGE_PART_HITS.with(|c| c.get()) |
| 5172 | } |
| 5173 | |
| 5174 | /// Reset [`EDGE_PART_HITS`] (a gate counts one stream at a time). |
| 5175 | #[allow(dead_code)] // read only from the `#[cfg(test)]` gates |
| 5176 | pub(crate) fn reset_edge_part_hits() { |
| 5177 | EDGE_PART_HITS.with(|c| c.set([0; 4])); |
| 5178 | } |
| 5179 | |
| 5180 | fn bump_edge_part(slot: usize) { |
| 5181 | hit_do! { |
| 5182 | EDGE_PART_HITS.with(|c| { |
| 5183 | let mut h = c.get(); |
| 5184 | h[slot] += 1; |
| 5185 | c.set(h); |
| 5186 | }); |
| 5187 | } |
| 5188 | } |
| 5189 | |
| 5190 | #[allow(dead_code)] // read only from the `#[cfg(test)]` gates |
| 5191 | pub(crate) fn sb_rect_hits() -> usize { |
| 5192 | SB_RECT_HITS.with(|c| c.get()) |
| 5193 | } |
| 5194 | |
| 5195 | // lane-golomb r2: the 32x32-level FRAME-EDGE partition. Slot 0/1 = the |
| 5196 | // gathered edge bit read as 0 (HORZ/VERT) / 1 (SPLIT); slot 2 = a 32-level |
| 5197 | // PARTITION_HORZ decoded with `has_rows32 == false` (only the top 32x16 half |
| 5198 | // is coded), slot 3 = a PARTITION_VERT with `has_cols32 == false`. Both tile |
| 5199 | // paths (intra and inter) feed the same counter. |
| 5200 | // |
| 5201 | // lane-golomb r3: slots 4..7 are the same four facts one level up, at the |
| 5202 | // SUPERBLOCK (64x64) edge -- 4/5 = the gathered 64-level bit read as |
| 5203 | // HORZ/VERT / SPLIT, 6 = a 64x32 bottom-edge strip decoded with |
| 5204 | // `has_rows == false`, 7 = a 32x64 right-edge strip. Real aomenc answers a |
| 5205 | // bottom-edge superblock with the 64-level HORZ (measured: 192x80 flat-band, |
| 5206 | // every SB of the last row), so the 32-level slots stay 0 on that content and |
| 5207 | // only these prove the arm fired. |
| 5208 | thread_local! { |
| 5209 | static EDGE32_HITS: std::cell::Cell<[usize; 8]> = |
| 5210 | const { std::cell::Cell::new([0; 8]) }; |
| 5211 | } |
| 5212 | |
| 5213 | /// Current value of [`EDGE32_HITS`]. |
| 5214 | #[allow(dead_code)] // read only from the `#[cfg(test)]` gates |
| 5215 | pub(crate) fn edge32_hits() -> [usize; 8] { |
| 5216 | EDGE32_HITS.with(|c| c.get()) |
| 5217 | } |
| 5218 | |
| 5219 | /// [`bump_edge32`] of `base + bit`, returning the bit -- the gathered edge |
| 5220 | /// reads are `if`-conditions, so the counter has to ride the expression. |
| 5221 | fn bump_edge32_bit(base: usize, bit: usize) -> usize { |
| 5222 | bump_edge32(base + bit.min(1)); |
| 5223 | bit |
| 5224 | } |
| 5225 | |
| 5226 | fn bump_edge32(slot: usize) { |
| 5227 | hit_do! { |
| 5228 | EDGE32_HITS.with(|c| { |
| 5229 | let mut h = c.get(); |
| 5230 | h[slot] += 1; |
| 5231 | c.set(h) |
| 5232 | }); |
| 5233 | } |
| 5234 | } |
| 5235 | |
| 5236 | // lane-part32 r4: how many superblock-level AB blocks (`PARTITION_HORZ_A`/ |
| 5237 | // `_B`/`VERT_A`/`_B` at 64x64 -- two 32x32 squares plus one 64x32/32x64 |
| 5238 | // strip) fired. Real aomenc picks these at 64 even with |
| 5239 | // `--enable-ab-partitions=0` (lane-sbpart r2's own measurement), so this is |
| 5240 | // the counter the gate hard-asserts. |
| 5241 | thread_local! { |
| 5242 | static SB_AB_HITS: std::cell::Cell<[usize; 4]> = |
| 5243 | const { std::cell::Cell::new([0; 4]) }; |
| 5244 | } |
| 5245 | |
| 5246 | /// Current value of [`SB_AB_HITS`], summed over the four arms. |
| 5247 | #[allow(dead_code)] // read only from the `#[cfg(test)]` gates |
| 5248 | pub(crate) fn sb_ab_hits() -> usize { |
| 5249 | SB_AB_HITS.with(|c| c.get().iter().sum()) |
| 5250 | } |
| 5251 | |
| 5252 | /// [`SB_AB_HITS`] split per arm, in `PARTITION_HORZ_A`, `_HORZ_B`, |
| 5253 | /// `_VERT_A`, `_VERT_B` order -- what the gate hard-asserts arm by arm, so a |
| 5254 | /// run where aomenc's RD only ever picked (say) `HORZ_A` cannot pass off as |
| 5255 | /// proof of all four. |
| 5256 | #[allow(dead_code)] // read only from the `#[cfg(test)]` gates |
| 5257 | pub(crate) fn sb_ab_hits_by_arm() -> [usize; 4] { |
| 5258 | SB_AB_HITS.with(std::cell::Cell::get) |
| 5259 | } |
| 5260 | |
| 5261 | // lane-tx64x16: the two 1:4 superblock arms, counted per orientation -- |
| 5262 | // `PARTITION_HORZ_4` (four 64x16 strips) and `PARTITION_VERT_4` (four 16x64 |
| 5263 | // strips). Two counters, not one, because a gate that only proved "some 1:4 |
| 5264 | // strip fired" would pass on a stream that never coded the other axis, and |
| 5265 | // every scan/context table this round added comes in a transposed pair. |
| 5266 | thread_local! { |
| 5267 | static SB_RECT4_HORZ_HITS: std::cell::Cell<usize> = const { std::cell::Cell::new(0) }; |
| 5268 | static SB_RECT4_VERT_HITS: std::cell::Cell<usize> = const { std::cell::Cell::new(0) }; |
| 5269 | } |
| 5270 | |
| 5271 | /// Current value of `SB_RECT4_HORZ_HITS` (64x16 strips decoded). |
| 5272 | #[allow(dead_code)] // read only from the `#[cfg(test)]` gates |
| 5273 | pub(crate) fn sb_rect4_horz_hits() -> usize { |
| 5274 | SB_RECT4_HORZ_HITS.with(|c| c.get()) |
| 5275 | } |
| 5276 | |
| 5277 | /// Current value of `SB_RECT4_VERT_HITS` (16x64 strips decoded). |
| 5278 | #[allow(dead_code)] // read only from the `#[cfg(test)]` gates |
| 5279 | pub(crate) fn sb_rect4_vert_hits() -> usize { |
| 5280 | SB_RECT4_VERT_HITS.with(|c| c.get()) |
| 5281 | } |
| 5282 | |
| 5283 | // lane-tx64x16 r3: the 32x32-level 1:4 arms, counted per orientation -- |
| 5284 | // `PARTITION_HORZ_4` (four 32x8 strips) and `PARTITION_VERT_4` (four 8x32 |
| 5285 | // strips), the arms the ReelB mid-film extract stops at. |
| 5286 | thread_local! { |
| 5287 | static RECT4_32_HORZ_HITS: std::cell::Cell<usize> = const { std::cell::Cell::new(0) }; |
| 5288 | static RECT4_32_VERT_HITS: std::cell::Cell<usize> = const { std::cell::Cell::new(0) }; |
| 5289 | /// lane-inter4 r1: the INTER-frame twins of the two counters above -- |
| 5290 | /// four 32x8 / 8x32 inter strips of a 32x32-level `PARTITION_HORZ_4` / |
| 5291 | /// `PARTITION_VERT_4`. |
| 5292 | static RECT4_32_HORZ_INTER_HITS: std::cell::Cell<usize> = const { std::cell::Cell::new(0) }; |
| 5293 | static RECT4_32_VERT_INTER_HITS: std::cell::Cell<usize> = const { std::cell::Cell::new(0) }; |
| 5294 | /// lane-inter4 r1: superblock-level inter `PARTITION_HORZ`/`PARTITION_VERT` |
| 5295 | /// strips (64x32 / 32x64) and their 1:4 pair (64x16 / 16x64). |
| 5296 | static INTER_SB_HORZ_HITS: std::cell::Cell<usize> = const { std::cell::Cell::new(0) }; |
| 5297 | static INTER_SB_VERT_HITS: std::cell::Cell<usize> = const { std::cell::Cell::new(0) }; |
| 5298 | static INTER_SB_HORZ4_HITS: std::cell::Cell<usize> = const { std::cell::Cell::new(0) }; |
| 5299 | static INTER_SB_VERT4_HITS: std::cell::Cell<usize> = const { std::cell::Cell::new(0) }; |
| 5300 | } |
| 5301 | |
| 5302 | // How many 32x32-level 1:4 strips actually read coefficients (non-skip): |
| 5303 | // a gate that only counted strips would pass on an all-skip stream, which |
| 5304 | // exercises none of the new scan/context tables. |
| 5305 | thread_local! { |
| 5306 | static RECT4_COEFF_HITS: std::cell::Cell<usize> = const { std::cell::Cell::new(0) }; |
| 5307 | } |
| 5308 | |
| 5309 | // lane-rectsplitx r1: 1:4 strips (32x8/8x32/64x16/16x64) decoded through the |
| 5310 | // per-transform-unit split path, split by tx depth -- depth 1 is a 2:1 RECT |
| 5311 | // unit (TX_16X8/TX_32X16), depth 2 a square one, and the two INTERLEAVE in |
| 5312 | // every real stream, so a gate must prove both fired. `SUB16_SPLIT_HITS` |
| 5313 | // counts the same for a 16x8/8x16 strip below 16x16. |
| 5314 | thread_local! { |
| 5315 | static RECT4_SPLIT_DEPTH1_HITS: std::cell::Cell<usize> = const { std::cell::Cell::new(0) }; |
| 5316 | static RECT4_SPLIT_DEPTH2_HITS: std::cell::Cell<usize> = const { std::cell::Cell::new(0) }; |
| 5317 | static SUB16_SPLIT_HITS: std::cell::Cell<usize> = const { std::cell::Cell::new(0) }; |
| 5318 | } |
| 5319 | |
| 5320 | /// Current values of `RECT4_SPLIT_DEPTH1_HITS` / `RECT4_SPLIT_DEPTH2_HITS`. |
| 5321 | #[allow(dead_code)] // read only from the `#[cfg(test)]` gates |
| 5322 | pub(crate) fn rect4_split_depth_hits() -> (usize, usize) { |
| 5323 | ( |
| 5324 | RECT4_SPLIT_DEPTH1_HITS.with(|c| c.get()), |
| 5325 | RECT4_SPLIT_DEPTH2_HITS.with(|c| c.get()), |
| 5326 | ) |
| 5327 | } |
| 5328 | |
| 5329 | /// Current value of `SUB16_SPLIT_HITS`. |
| 5330 | #[allow(dead_code)] // read only from the `#[cfg(test)]` gates |
| 5331 | pub(crate) fn sub16_split_hits() -> usize { |
| 5332 | SUB16_SPLIT_HITS.with(|c| c.get()) |
| 5333 | } |
| 5334 | |
| 5335 | // lane-frame36 r2: superblock-level strips ([`decode_block_rect64`]) whose |
| 5336 | // luma transform is SPLIT and which therefore leave through that fn's early |
| 5337 | // return -- the arm that published no `TXFM_CONTEXT` band until this round. |
| 5338 | thread_local! { |
| 5339 | static RECT64_SPLIT_TXFM_PUBLISH_HITS: std::cell::Cell<usize> = const { std::cell::Cell::new(0) }; |
| 5340 | } |
| 5341 | |
| 5342 | /// Current value of `RECT64_SPLIT_TXFM_PUBLISH_HITS`. |
| 5343 | #[allow(dead_code)] // read only from the `#[cfg(test)]` gates |
| 5344 | pub(crate) fn rect64_split_txfm_publish_hits() -> usize { |
| 5345 | RECT64_SPLIT_TXFM_PUBLISH_HITS.with(|c| c.get()) |
| 5346 | } |
| 5347 | |
| 5348 | /// Current value of `RECT4_COEFF_HITS`. |
| 5349 | pub(crate) fn rect4_coeff_hits() -> usize { |
| 5350 | RECT4_COEFF_HITS.with(|c| c.get()) |
| 5351 | } |
| 5352 | |
| 5353 | // lane-sbab r1: how many superblock-level (64x64) AB partitions fired IN AN |
| 5354 | // INTER frame, per arm, in `PARTITION_HORZ_A`, `_HORZ_B`, `_VERT_A`, |
| 5355 | // `_VERT_B` order (the intra-frame counterpart is `SB_AB_HITS`). Each arm is |
| 5356 | // two 32x32 inter squares plus one true 64x32/32x64 inter strip; the gate |
| 5357 | // asserts the arms it proves separately so a run whose RD only picked one |
| 5358 | // cannot pass as proof of the others. |
| 5359 | thread_local! { |
| 5360 | static SB_AB_INTER_HITS: std::cell::Cell<[usize; 4]> = const { std::cell::Cell::new([0; 4]) }; |
| 5361 | } |
| 5362 | |
| 5363 | /// [`SB_AB_INTER_HITS`] per arm (HORZ_A, HORZ_B, VERT_A, VERT_B at 64x64 in an |
| 5364 | /// inter frame). |
| 5365 | pub fn sb_ab_inter_hits_by_arm() -> [usize; 4] { |
| 5366 | SB_AB_INTER_HITS.with(std::cell::Cell::get) |
| 5367 | } |
| 5368 | |
| 5369 | // lane-inter16ab r1: how many 16x16-level AB partitions fired IN AN INTER |
| 5370 | // frame, per arm, in `PARTITION_HORZ_A`, `_HORZ_B`, `_VERT_A`, `_VERT_B` |
| 5371 | // order (the intra-frame counterpart is `AB16_HITS`). Each arm is two 8x8 |
| 5372 | // inter leaves (`decode_inter_block8`) plus one true 16x8/8x16 inter strip |
| 5373 | // (`decode_inter_block` with `write_w`/`write_h`); the gate asserts every |
| 5374 | // arm separately so a run whose RD only picked one cannot pass as proof of |
| 5375 | // four. |
| 5376 | thread_local! { |
| 5377 | static AB16_INTER_HITS: std::cell::Cell<[usize; 4]> = const { std::cell::Cell::new([0; 4]) }; |
| 5378 | } |
| 5379 | |
| 5380 | /// [`AB16_INTER_HITS`] per arm (HORZ_A, HORZ_B, VERT_A, VERT_B at 16x16 in an |
| 5381 | /// inter frame). |
| 5382 | pub fn ab16_inter_hits_by_arm() -> [usize; 4] { |
| 5383 | AB16_INTER_HITS.with(std::cell::Cell::get) |
| 5384 | } |
| 5385 | |
| 5386 | // lane-inter16ab r2: the 16x16-level 1:4 pair as INTER blocks -- four 16x4 |
| 5387 | // (`PARTITION_HORZ_4`) or 4x16 (`PARTITION_VERT_4`) strips. `CHROMA_PAIRS` |
| 5388 | // counts the odd strips that close a 4:2:0 chroma pair |
| 5389 | // (`is_chroma_reference`, av1_common_int.h:1454: bh == 1 mi => odd mi_row), |
| 5390 | // `SUB8_PIECES` the pair chroma blocks whose TOP (LEFT) half was built from |
| 5391 | // the PREVIOUS strip's own mv/ref/filters -- libaom |
| 5392 | // `build_inter_predictors_sub8x8` (reconinter_template.inc:87-160), reached |
| 5393 | // through `is_sub8x8_inter` (same file, :54). |
| 5394 | thread_local! { |
| 5395 | static INTER16_HORZ4_HITS: std::cell::Cell<usize> = const { std::cell::Cell::new(0) }; |
| 5396 | static INTER16_VERT4_HITS: std::cell::Cell<usize> = const { std::cell::Cell::new(0) }; |
| 5397 | static INTER16_CHROMA_PAIR_HITS: std::cell::Cell<usize> = const { std::cell::Cell::new(0) }; |
| 5398 | static INTER16_SUB8_PIECE_HITS: std::cell::Cell<usize> = const { std::cell::Cell::new(0) }; |
| 5399 | } |
| 5400 | |
| 5401 | // lane-intra16x4: INTRA-coded strips of an inter 16x16-level 1:4 partition -- |
| 5402 | // [0] 16x4, [1] 4x16, [2] the odd (chroma-reference) ones, which carry the |
| 5403 | // pair's single 8x4 (4x8) chroma unit for BOTH strips. |
| 5404 | thread_local! { |
| 5405 | static INTRA16X4_IN_INTER_HITS: std::cell::Cell<[usize; 3]> = |
| 5406 | const { std::cell::Cell::new([0; 3]) }; |
| 5407 | } |
| 5408 | |
| 5409 | /// `(intra 16x4 strips, intra 4x16 strips, of which chroma-reference)` decoded |
| 5410 | /// inside an INTER 16x16-level 1:4 partition (lane-intra16x4). |
| 5411 | pub fn intra16x4_in_inter_hits() -> (usize, usize, usize) { |
| 5412 | let v = INTRA16X4_IN_INTER_HITS.with(std::cell::Cell::get); |
| 5413 | (v[0], v[1], v[2]) |
| 5414 | } |
| 5415 | |
| 5416 | /// Current values of the inter 16x16-level 1:4 counters: `(16x4 strips, 4x16 |
| 5417 | /// strips, chroma pairs closed, chroma pairs built from two different |
| 5418 | /// strips' motion vectors)`. |
| 5419 | pub fn inter16_rect4_counters() -> (usize, usize, usize, usize) { |
| 5420 | ( |
| 5421 | INTER16_HORZ4_HITS.with(std::cell::Cell::get), |
| 5422 | INTER16_VERT4_HITS.with(std::cell::Cell::get), |
| 5423 | INTER16_CHROMA_PAIR_HITS.with(std::cell::Cell::get), |
| 5424 | INTER16_SUB8_PIECE_HITS.with(std::cell::Cell::get), |
| 5425 | ) |
| 5426 | } |
| 5427 | |
| 5428 | /// One 16x4 / 4x16 inter strip's chroma situation, handed to |
| 5429 | /// [`decode_inter_block`] out of band (the same one-shot thread-local idiom |
| 5430 | /// `REDUCED_TX_SET_INTER` uses) rather than through twenty-three call sites |
| 5431 | /// that all mean "an ordinary block, chroma of its own". |
| 5432 | #[derive(Clone, Copy)] |
| 5433 | pub(crate) struct InterStripChroma { |
| 5434 | /// `is_chroma_reference` for this strip: false on strips 0 and 2, which |
| 5435 | /// read and reconstruct NO chroma at all. |
| 5436 | has_chroma: bool, |
| 5437 | /// `PARTITION_HORZ_4` (16x4 strips) rather than `PARTITION_VERT_4`. |
| 5438 | horz: bool, |
| 5439 | /// The mi position of the EVEN strip of this pair -- `setup_pred_plane`'s |
| 5440 | /// `if (ss_y && (mi_row & 1) && mi_size_high[bsize] == 1) mi_row -= 1`, |
| 5441 | /// i.e. the origin of the 8x4 (4x8) chroma transform. |
| 5442 | pair_mi: (usize, usize), |
| 5443 | /// The previous (even) strip's single-reference MC parameters, `None` |
| 5444 | /// when it was intra or compound -- `is_sub8x8_inter` then returns false |
| 5445 | /// and the whole chroma block is predicted from THIS strip's mv. |
| 5446 | prev: Option<(i8, (i32, i32), mc::InterpFilterKind, mc::InterpFilterKind)>, |
| 5447 | } |
| 5448 | |
| 5449 | /// The single-reference MC parameters of the inter block that decoded |
| 5450 | /// last -- read by the next 1:4 strip to build its pair's chroma. |
| 5451 | |
| 5452 | fn bump_ab16_inter(arm: usize) { |
| 5453 | hit_do! { |
| 5454 | AB16_INTER_HITS.with(|c| { |
| 5455 | let mut v = c.get(); |
| 5456 | v[arm] += 1; |
| 5457 | c.set(v); |
| 5458 | }); |
| 5459 | } |
| 5460 | } |
| 5461 | |
| 5462 | // lane-r14: the 16x16-level 1:4 pair -- four 16x4 (`PARTITION_HORZ_4`) or |
| 5463 | // 4x16 (`PARTITION_VERT_4`) strips, `CHROMA` counting the odd strips that |
| 5464 | // close a 4:2:0 chroma pair and `COEFF` the strips with a coded residual. |
| 5465 | thread_local! { |
| 5466 | static RECT4_16_HORZ_HITS: std::cell::Cell<usize> = const { std::cell::Cell::new(0) }; |
| 5467 | static RECT4_16_VERT_HITS: std::cell::Cell<usize> = const { std::cell::Cell::new(0) }; |
| 5468 | static RECT4_16_CHROMA_HITS: std::cell::Cell<usize> = const { std::cell::Cell::new(0) }; |
| 5469 | static RECT4_16_COEFF_HITS: std::cell::Cell<usize> = const { std::cell::Cell::new(0) }; |
| 5470 | static RECT4_16_SPLIT_HITS: std::cell::Cell<usize> = const { std::cell::Cell::new(0) }; |
| 5471 | /// lane-kf1200: pairs whose chroma edge-filter type read from the PAIR's |
| 5472 | /// base mi differs from what the strip's own (odd) mi would have said -- |
| 5473 | /// the shape that made 4 chroma samples of the ReelB `-ss 1200` |
| 5474 | /// key frame miss by 1. Zero here means a gate never exercised the fix. |
| 5475 | static RECT4_16_UV_PAIR_FILT_HITS: std::cell::Cell<usize> = const { std::cell::Cell::new(0) }; |
| 5476 | /// lane-av1-rect14: lossless 16x4/4x16 chroma pairs walked as two TX_4X4s. |
| 5477 | static RECT4_16_LOSSLESS_CHROMA_HITS: std::cell::Cell<usize> = const { std::cell::Cell::new(0) }; |
| 5478 | /// lane-av1-rect14: intrabc reconstructions of a 16x4/4x16 pair strip. |
| 5479 | static RECT4_16_INTRABC_HITS: std::cell::Cell<usize> = const { std::cell::Cell::new(0) }; |
| 5480 | /// lane-kf1200 r2: sub-8x8 chroma blocks that are `skip_txfm` AND |
| 5481 | /// `UV_CFL_PRED` -- the arm that used to drop the CfL alpha entirely. |
| 5482 | static SKIP_CFL_HITS: std::cell::Cell<usize> = const { std::cell::Cell::new(0) }; |
| 5483 | /// lane-av1-loss64: chroma transform units the frame-edge clip skipped |
| 5484 | /// (libaom `decode_reconstruct_tx`'s `max_blocks_wide/high` test) in |
| 5485 | /// [`decode_block`]'s multi-transform-unit walk -- the units a lossless |
| 5486 | /// block overhanging the frame does NOT code. |
| 5487 | static CHROMA_EDGE_TU_CLIP_HITS: std::cell::Cell<usize> = const { std::cell::Cell::new(0) }; |
| 5488 | /// lane-av1-loss64: skipped lossless intrabc rect strips whose luma |
| 5489 | /// entropy bands were zeroed per unit (libaom `av1_reset_entropy_context`, |
| 5490 | /// decodeframe.c:1262) instead of leaking the previous occupant's. |
| 5491 | static SKIP_LOSSLESS_BAND_RESET_HITS: std::cell::Cell<usize> = const { std::cell::Cell::new(0) }; |
| 5492 | /// lane-av1-llband: 4x4 chroma transform units a lossless HORZ/VERT strip |
| 5493 | /// walked at subsampling 0/0 (one unit per mi cell -- the geometry the |
| 5494 | /// hardcoded 4:2:0 span mis-stepped). |
| 5495 | static RECT_SPLIT_LOSSLESS_CHROMA444_HITS: std::cell::Cell<usize> = const { std::cell::Cell::new(0) }; |
| 5496 | /// lane-av1-ibc444rect: unskipped intrabc 8x4/4x8 leaves whose luma |
| 5497 | /// coefficients a lossless frame walked as per-4x4 inter units (the walk |
| 5498 | /// that replaced the single rect unit). |
| 5499 | static INTRABC_RECT8_LOSSLESS_LUMA_HITS: std::cell::Cell<usize> = const { std::cell::Cell::new(0) }; |
| 5500 | /// lane-av1-ibc444rect: unskipped intrabc 8x4/4x8 leaves whose chroma a |
| 5501 | /// lossless 4:4:4 frame walked as per-4x4 [`TxbSet::Chroma4`] units. |
| 5502 | static INTRABC_RECT8_LOSSLESS_CHROMA_HITS: std::cell::Cell<usize> = const { std::cell::Cell::new(0) }; |
| 5503 | } |
| 5504 | |
| 5505 | /// How many skipped sub-8x8 chroma blocks carried a CfL alpha (the shape |
| 5506 | /// that reconstructed as flat DC before lane-kf1200 r2). |
| 5507 | pub fn skip_cfl_hits() -> usize { |
| 5508 | SKIP_CFL_HITS.with(|c| c.get()) |
| 5509 | } |
| 5510 | |
| 5511 | /// How many chroma transform units the frame-edge clip skipped in |
| 5512 | /// [`decode_block`]'s multi-transform-unit walk (lane-av1-loss64). Zero on a |
| 5513 | /// stream whose lossless blocks never overhang the frame -- a gate asserting |
| 5514 | /// the clip fired must see this nonzero. |
| 5515 | pub fn chroma_edge_tu_clip_hits() -> usize { |
| 5516 | CHROMA_EDGE_TU_CLIP_HITS.with(|c| c.get()) |
| 5517 | } |
| 5518 | |
| 5519 | /// How many skipped lossless intrabc rect strips had their luma entropy |
| 5520 | /// bands zeroed per unit (lane-av1-loss64). |
| 5521 | pub fn skip_lossless_band_reset_hits() -> usize { |
| 5522 | SKIP_LOSSLESS_BAND_RESET_HITS.with(|c| c.get()) |
| 5523 | } |
| 5524 | |
| 5525 | /// How many 4x4 chroma transform units a lossless HORZ/VERT strip walked at |
| 5526 | /// subsampling 0/0 (lane-av1-llband). Zero means a stream never decoded the |
| 5527 | /// 4:4:4 lossless unit walk this counter proves -- a gate asserting the fix |
| 5528 | /// must see this nonzero. |
| 5529 | pub fn rect_split_lossless_chroma444_hits() -> usize { |
| 5530 | RECT_SPLIT_LOSSLESS_CHROMA444_HITS.with(|c| c.get()) |
| 5531 | } |
| 5532 | |
| 5533 | /// How many unskipped intrabc 8x4/4x8 leaves a lossless frame walked luma as |
| 5534 | /// per-4x4 inter units (lane-av1-ibc444rect). |
| 5535 | pub fn intrabc_rect8_lossless_luma_hits() -> usize { |
| 5536 | INTRABC_RECT8_LOSSLESS_LUMA_HITS.with(|c| c.get()) |
| 5537 | } |
| 5538 | |
| 5539 | /// How many unskipped intrabc 8x4/4x8 leaves a lossless 4:4:4 frame walked |
| 5540 | /// chroma as per-4x4 [`TxbSet::Chroma4`] units (lane-av1-ibc444rect). |
| 5541 | pub fn intrabc_rect8_lossless_chroma_hits() -> usize { |
| 5542 | INTRABC_RECT8_LOSSLESS_CHROMA_HITS.with(|c| c.get()) |
| 5543 | } |
| 5544 | |
| 5545 | /// Gate-side reset for the two lane-av1-ibc444rect counters above. |
| 5546 | pub fn reset_ibc444rect_hits() { |
| 5547 | INTRABC_RECT8_LOSSLESS_LUMA_HITS.with(|c| c.set(0)); |
| 5548 | INTRABC_RECT8_LOSSLESS_CHROMA_HITS.with(|c| c.set(0)); |
| 5549 | } |
| 5550 | |
| 5551 | /// Gate-side reset for the two lane-av1-loss64 counters above. |
| 5552 | pub fn reset_loss64_hits() { |
| 5553 | CHROMA_EDGE_TU_CLIP_HITS.with(|c| c.set(0)); |
| 5554 | SKIP_LOSSLESS_BAND_RESET_HITS.with(|c| c.set(0)); |
| 5555 | } |
| 5556 | |
| 5557 | /// How many 16x4/4x16 chroma pairs took a different `intra_edge_filter_type` |
| 5558 | /// from the pair's base mi than the strip's own mi would have given. |
| 5559 | pub fn rect4_16_uv_pair_filt_hits() -> usize { |
| 5560 | RECT4_16_UV_PAIR_FILT_HITS.with(|c| c.get()) |
| 5561 | } |
| 5562 | |
| 5563 | /// How many lossless 16x4/4x16 chroma pairs walked two TX_4X4s per plane. |
| 5564 | pub fn rect4_16_lossless_chroma_hits() -> usize { |
| 5565 | RECT4_16_LOSSLESS_CHROMA_HITS.with(|c| c.get()) |
| 5566 | } |
| 5567 | |
| 5568 | /// How many 16x4/4x16 strips reconstructed as intra block copy. |
| 5569 | pub fn rect4_16_intrabc_hits() -> usize { |
| 5570 | RECT4_16_INTRABC_HITS.with(|c| c.get()) |
| 5571 | } |
| 5572 | |
| 5573 | /// Zero the lane-av1-rect14 pair counters so a gate measures one stream. |
| 5574 | pub fn reset_rect4_16_pair_hits() { |
| 5575 | RECT4_16_LOSSLESS_CHROMA_HITS.with(|c| c.set(0)); |
| 5576 | RECT4_16_INTRABC_HITS.with(|c| c.set(0)); |
| 5577 | } |
| 5578 | |
| 5579 | // lane-reelckf r1: the two chroma defects ReelC's key frames exposed -- |
| 5580 | // CHROMA_SKIP_CFL counts skipped intra blocks whose chroma is `UV_CFL_PRED` |
| 5581 | // (prediction still runs, libaom `predict_and_reconstruct_intra_block`), and |
| 5582 | // RECT4_16_CHROMA_DIR counts 1:4 chroma pairs predicted directionally (whose |
| 5583 | // intra-edge filter type must come from the chroma-reference position). |
| 5584 | thread_local! { |
| 5585 | static CHROMA_SKIP_CFL_HITS: std::cell::Cell<usize> = const { std::cell::Cell::new(0) }; |
| 5586 | static RECT4_16_CHROMA_DIR_HITS: std::cell::Cell<usize> = const { std::cell::Cell::new(0) }; |
| 5587 | } |
| 5588 | |
| 5589 | /// `(skipped UV_CFL_PRED blocks, directional 1:4 chroma pairs)` -- lane-reelckf. |
| 5590 | pub fn reelc_chroma_counters() -> (usize, usize) { |
| 5591 | ( |
| 5592 | CHROMA_SKIP_CFL_HITS.with(|c| c.get()), |
| 5593 | RECT4_16_CHROMA_DIR_HITS.with(|c| c.get()), |
| 5594 | ) |
| 5595 | } |
| 5596 | |
| 5597 | /// Current values of the 16x16-level 1:4 counters: `(16x4 strips, 4x16 |
| 5598 | /// strips, chroma-reference strips, strips with a coded residual, strips |
| 5599 | /// whose luma transform is split)`. |
| 5600 | pub fn rect4_16_counters() -> (usize, usize, usize, usize, usize) { |
| 5601 | ( |
| 5602 | RECT4_16_HORZ_HITS.with(|c| c.get()), |
| 5603 | RECT4_16_VERT_HITS.with(|c| c.get()), |
| 5604 | RECT4_16_CHROMA_HITS.with(|c| c.get()), |
| 5605 | RECT4_16_COEFF_HITS.with(|c| c.get()), |
| 5606 | RECT4_16_SPLIT_HITS.with(|c| c.get()), |
| 5607 | ) |
| 5608 | } |
| 5609 | |
| 5610 | /// Current value of `RECT4_32_HORZ_HITS` (32x8 strips decoded). |
| 5611 | pub(crate) fn rect4_32_horz_hits() -> usize { |
| 5612 | RECT4_32_HORZ_HITS.with(|c| c.get()) |
| 5613 | } |
| 5614 | |
| 5615 | /// Current value of `RECT4_32_VERT_HITS` (8x32 strips decoded). |
| 5616 | pub(crate) fn rect4_32_vert_hits() -> usize { |
| 5617 | RECT4_32_VERT_HITS.with(|c| c.get()) |
| 5618 | } |
| 5619 | |
| 5620 | /// Current value of `RECT4_32_HORZ_INTER_HITS` (32x8 inter strips decoded). |
| 5621 | pub(crate) fn rect4_32_horz_inter_hits() -> usize { |
| 5622 | RECT4_32_HORZ_INTER_HITS.with(|c| c.get()) |
| 5623 | } |
| 5624 | |
| 5625 | /// Current value of `RECT4_32_VERT_INTER_HITS` (8x32 inter strips decoded). |
| 5626 | pub(crate) fn rect4_32_vert_inter_hits() -> usize { |
| 5627 | RECT4_32_VERT_INTER_HITS.with(|c| c.get()) |
| 5628 | } |
| 5629 | |
| 5630 | /// Current value of `INTER_SB_HORZ_HITS` (64x32 inter strips decoded). |
| 5631 | pub(crate) fn inter_sb_horz_hits() -> usize { |
| 5632 | INTER_SB_HORZ_HITS.with(|c| c.get()) |
| 5633 | } |
| 5634 | |
| 5635 | /// Current value of `INTER_SB_VERT_HITS` (32x64 inter strips decoded). |
| 5636 | pub(crate) fn inter_sb_vert_hits() -> usize { |
| 5637 | INTER_SB_VERT_HITS.with(|c| c.get()) |
| 5638 | } |
| 5639 | |
| 5640 | /// Current value of `INTER_SB_HORZ4_HITS` (64x16 inter strips decoded). |
| 5641 | pub(crate) fn inter_sb_horz4_hits() -> usize { |
| 5642 | INTER_SB_HORZ4_HITS.with(|c| c.get()) |
| 5643 | } |
| 5644 | |
| 5645 | /// Current value of `INTER_SB_VERT4_HITS` (16x64 inter strips decoded). |
| 5646 | pub(crate) fn inter_sb_vert4_hits() -> usize { |
| 5647 | INTER_SB_VERT4_HITS.with(|c| c.get()) |
| 5648 | } |
| 5649 | |
| 5650 | // lane-rect64q r1: how many of [`decode_block_rect64`]'s three per-plane |
| 5651 | // dequant calls actually observed `CURRENT_Q_IDX != base_q_idx` -- proof the |
| 5652 | // running-vs-stale-snapshot bug this round fixed is exercised by a gate, not |
| 5653 | // just inert-compiled. Zero forever on a passing gate means that gate's |
| 5654 | // stream never carries `delta_q_present=1` through a rect64 block. |
| 5655 | thread_local! { |
| 5656 | static RECT64_QIDX_DRIFT_HITS: std::cell::Cell<usize> = |
| 5657 | const { std::cell::Cell::new(0) }; |
| 5658 | } |
| 5659 | |
| 5660 | /// Current value of [`RECT64_QIDX_DRIFT_HITS`]. |
| 5661 | #[allow(dead_code)] // read only from the `#[cfg(test)]` gates |
| 5662 | pub(crate) fn rect64_qidx_drift_hits() -> usize { |
| 5663 | RECT64_QIDX_DRIFT_HITS.with(|c| c.get()) |
| 5664 | } |
| 5665 | |
| 5666 | // lane-partab r1: how many 32x32 quadrants decoded an AB partition |
| 5667 | // (PARTITION_HORZ_A / VERT_A / VERT_B -- two 16x16 squares plus one |
| 5668 | // 16x32/32x16 strip). Like [`RECT_PARTITION_HITS`], this is the |
| 5669 | // free-partition gate's proof this round's arms actually fired. |
| 5670 | thread_local! { |
| 5671 | static PARTAB_HITS: std::cell::Cell<usize> = |
| 5672 | const { std::cell::Cell::new(0) }; |
| 5673 | } |
| 5674 | |
| 5675 | /// Current value of [`PARTAB_HITS`]. |
| 5676 | #[allow(dead_code)] // read only from the `#[cfg(test)]` gates |
| 5677 | pub(crate) fn partab_hits() -> usize { |
| 5678 | PARTAB_HITS.with(|c| c.get()) |
| 5679 | } |
| 5680 | |
| 5681 | // lane-part32 r1: how many 32x32 quadrants decoded each INTRA AB/4-way arm, |
| 5682 | // one counter per arm rather than one shared total -- the gate hard-asserts |
| 5683 | // each of these individually nonzero so a recipe that only ever fires |
| 5684 | // HORZ_A can't silently stand in for the whole set. |
| 5685 | thread_local! { |
| 5686 | static INTRA_HORZ_A_HITS: std::cell::Cell<usize> = |
| 5687 | const { std::cell::Cell::new(0) }; |
| 5688 | static INTRA_HORZ_B_HITS: std::cell::Cell<usize> = |
| 5689 | const { std::cell::Cell::new(0) }; |
| 5690 | static INTRA_VERT_A_HITS: std::cell::Cell<usize> = |
| 5691 | const { std::cell::Cell::new(0) }; |
| 5692 | static INTRA_VERT_B_HITS: std::cell::Cell<usize> = |
| 5693 | const { std::cell::Cell::new(0) }; |
| 5694 | } |
| 5695 | |
| 5696 | /// Current value of [`INTRA_HORZ_A_HITS`]. |
| 5697 | #[allow(dead_code)] // read only from the `#[cfg(test)]` gates |
| 5698 | pub(crate) fn intra_horz_a_hits() -> usize { |
| 5699 | INTRA_HORZ_A_HITS.with(|c| c.get()) |
| 5700 | } |
| 5701 | /// Current value of [`INTRA_HORZ_B_HITS`]. |
| 5702 | #[allow(dead_code)] // read only from the `#[cfg(test)]` gates |
| 5703 | pub(crate) fn intra_horz_b_hits() -> usize { |
| 5704 | INTRA_HORZ_B_HITS.with(|c| c.get()) |
| 5705 | } |
| 5706 | /// Current value of [`INTRA_VERT_A_HITS`]. |
| 5707 | #[allow(dead_code)] // read only from the `#[cfg(test)]` gates |
| 5708 | pub(crate) fn intra_vert_a_hits() -> usize { |
| 5709 | INTRA_VERT_A_HITS.with(|c| c.get()) |
| 5710 | } |
| 5711 | /// Current value of [`INTRA_VERT_B_HITS`]. |
| 5712 | #[allow(dead_code)] // read only from the `#[cfg(test)]` gates |
| 5713 | pub(crate) fn intra_vert_b_hits() -> usize { |
| 5714 | INTRA_VERT_B_HITS.with(|c| c.get()) |
| 5715 | } |
| 5716 | |
| 5717 | // How many [`read_inter_compound_mode`] reads actually happened |
| 5718 | // (lane-av1comp) -- proves a `COMPOUND_REFERENCE` block reached its own |
| 5719 | // `compound_mode` symbol (past `comp_mode`/`comp_ref` and the compound |
| 5720 | // mvstack build), not just that `comp_mode` fired. Still refused right |
| 5721 | // after (no MV assignment or MC wired), so this only counts attempted |
| 5722 | // reads. |
| 5723 | thread_local! { |
| 5724 | static COMPOUND_MODE_HITS: std::cell::Cell<usize> = |
| 5725 | const { std::cell::Cell::new(0) }; |
| 5726 | } |
| 5727 | |
| 5728 | /// Current value of [`COMPOUND_MODE_HITS`]. |
| 5729 | #[allow(dead_code)] // reader is test-only (the 12-bit compound witness); dead in non-test builds |
| 5730 | pub(crate) fn compound_mode_hits() -> usize { |
| 5731 | COMPOUND_MODE_HITS.with(|c| c.get()) |
| 5732 | } |
| 5733 | |
| 5734 | /// How many query blocks folded in at least one temporal MV candidate |
| 5735 | /// (spec 7.10.2.8's `add_tpl_ref_mv`) so far in this process -- the |
| 5736 | /// temporal-MV gate's own firing counter (lane-av1tmvp). |
| 5737 | /// |
| 5738 | /// # HANDOFF (lane-av1tmvp): not yet wired to a live stream |
| 5739 | /// [`crate::mvstack::find_mv_stack_with_sign_bias`]'s only call site in |
| 5740 | /// this file (`decode_inter_block`) still passes `tpl: None` -- this |
| 5741 | /// counter compiles and the projection math it counts is unit-tested |
| 5742 | /// ([`crate::motion_field`]), but nothing in [`crate::stream::decode_stream`] |
| 5743 | /// builds a per-DPB-slot [`crate::motion_field::MotionField`] or calls |
| 5744 | /// [`crate::motion_field::setup_motion_field`] yet, so this will read `0` |
| 5745 | /// against any real stream today. See the module doc on |
| 5746 | /// `crate::motion_field` and this crate's ledger for the remaining wiring. |
| 5747 | #[allow(dead_code)] // read only from the `#[cfg(test)]` gates |
| 5748 | pub(crate) fn tmv_hits() -> usize { |
| 5749 | crate::mvstack::tmv_hits() |
| 5750 | } |
| 5751 | |
| 5752 | /// Current value of [`crate::mc::inter_pred_hits`] -- inter predictions |
| 5753 | /// produced on this thread, the 10-bit inter gate's firing proof. |
| 5754 | #[allow(dead_code)] // read only from the `#[cfg(test)]` gates |
| 5755 | pub(crate) fn inter_pred_hits() -> usize { |
| 5756 | crate::mc::inter_pred_hits() |
| 5757 | } |
| 5758 | |
| 5759 | /// Everything [`decode_inter_block`] needs to fold temporal MV candidates |
| 5760 | /// (spec 7.10.2.8), threaded down from [`decode_inter_frame_tile_with_cdfs`]: |
| 5761 | /// the current frame's own projected field ([`crate::motion_field::setup_motion_field`]'s |
| 5762 | /// output, built by the caller when `header.use_ref_frame_mvs` is set) plus |
| 5763 | /// the order-hint bookkeeping [`crate::motion_field::get_relative_dist`] |
| 5764 | /// needs to turn a query block's own resolved `ref_frame` into |
| 5765 | /// `TplArgs::cur_offset_0` (spec 7.9.3). |
| 5766 | pub(crate) struct TplFrameArgs<'a> { |
| 5767 | pub field: &'a crate::motion_field::TplField, |
| 5768 | pub order_hint_bits: u32, |
| 5769 | pub order_hint: u32, |
| 5770 | /// `ref_order_hints[ref_frame - LAST_FRAME]` for `ref_frame` in |
| 5771 | /// `LAST_FRAME..=ALTREF_FRAME` — the OrderHint of the picture this |
| 5772 | /// frame's own `ref_frame_idx` names for each single reference. |
| 5773 | pub ref_order_hints: [u32; 7], |
| 5774 | } |
| 5775 | |
| 5776 | /// Builds this frame's own saved motion field (libaom `av1_copy_frame_mvs`, |
| 5777 | /// spec 7.9's per-frame `MotionFieldMvs` storage step) from its fully |
| 5778 | /// decoded `grid`: every 4x4 unit an inter block covers reads the same |
| 5779 | /// `MiInfo` ([`decode_inter_block`]/`decode_inter_block8` write it |
| 5780 | /// block-uniform), so sampling any one 4x4 unit inside each 8x8 cell is |
| 5781 | /// exact — every block this crate ever codes is at least 8x8, so no 8x8 |
| 5782 | /// cell straddles two different blocks' values. |
| 5783 | #[allow(unsafe_code)] // lane-mf: `par::Shared` band split, same corner-cut as the filter stages |
| 5784 | pub(crate) fn build_motion_field( |
| 5785 | grid: &MiGrid, |
| 5786 | mi_cols: usize, |
| 5787 | mi_rows: usize, |
| 5788 | order_hint: u32, |
| 5789 | order_hint_bits: u32, |
| 5790 | ref_order_hints: [u32; 7], |
| 5791 | fctx: &FrameCtx, |
| 5792 | ) -> crate::motion_field::MotionField { |
| 5793 | let mut field = |
| 5794 | crate::motion_field::MotionField::new(mi_cols, mi_rows, order_hint, ref_order_hints, false); |
| 5795 | // lane-interbis r1: libaom `av1_calculate_ref_frame_side` -- a reference |
| 5796 | // whose OrderHint is AHEAD of (or equal to) this frame's is "on the other |
| 5797 | // side", and `av1_copy_frame_mvs` skips it entirely, so a compound block |
| 5798 | // referencing one stores only its OTHER side's mv. With |
| 5799 | // `enable_order_hint` off (`order_hint_bits == 0`) every side is 0 and |
| 5800 | // nothing is skipped. |
| 5801 | // lane-mf: hoisted out of the per-4x4-unit loop -- the answer depends |
| 5802 | // only on the reference frame, so it is 7 table lookups per frame instead |
| 5803 | // of two per inter unit. |
| 5804 | let other_side: [bool; 8] = std::array::from_fn(|rf| { |
| 5805 | let i = rf.wrapping_sub(LAST_FRAME as usize); |
| 5806 | order_hint_bits != 0 |
| 5807 | && i < 7 |
| 5808 | && (crate::motion_field::get_relative_dist(order_hint_bits, ref_order_hints[i], order_hint) > 0 |
| 5809 | || ref_order_hints[i] == order_hint) |
| 5810 | }); |
| 5811 | // libaom `REFMVS_LIMIT` (`mvref_common.h`): a stored motion field mv is |
| 5812 | // capped at 12 bits per component; a longer one is not stored at all. |
| 5813 | const REFMVS_LIMIT: i32 = (1 << 12) - 1; |
| 5814 | let (cols, rows, cells) = field.cells_mut(); |
| 5815 | // Fills cell rows `r0..r1`; `out` is exactly those rows' cells, so bands |
| 5816 | // never touch each other (an 8x8 cell lives in one cell row, and mi row |
| 5817 | // `row` writes cell row `row / 2`). |
| 5818 | let fill = |r0: usize, r1: usize, out: &mut [Option<crate::motion_field::SavedMv>]| { |
| 5819 | for row in (r0 * 2)..(r1 * 2).min(mi_rows) { |
| 5820 | let out = &mut out[(row / 2 - r0) * cols..][..cols]; |
| 5821 | for col in 0..mi_cols { |
| 5822 | let Some(info) = grid.get_frame(row, col) else { |
| 5823 | continue; |
| 5824 | }; |
| 5825 | // `av1_copy_frame_mvs` runs for EVERY block of an inter frame and |
| 5826 | // CLEARS the cells it covers first (`mv->ref_frame = NONE_FRAME`), |
| 5827 | // so an intra block -- or an inter block whose every slot is on the |
| 5828 | // other side / over the limit -- wipes what an earlier block wrote |
| 5829 | // into the same 8x8 cell (only reachable from sub-8x8 leaves, which |
| 5830 | // share a cell). |
| 5831 | if !info.is_inter { |
| 5832 | out[col / 2] = None; |
| 5833 | continue; |
| 5834 | } |
| 5835 | // `av1_copy_frame_mvs` walks BOTH reference slots and the LAST |
| 5836 | // qualifying one wins -- a compound block stores its second |
| 5837 | // reference's mv, never its first (unless the second is on the |
| 5838 | // other side or over the limit). Storing slot 0 unconditionally |
| 5839 | // put the wrong (mv, ref_frame_offset) pair in every compound |
| 5840 | // block's cell, which a later frame's temporal candidate then |
| 5841 | // projected with the wrong reference distance. |
| 5842 | let mut saved: Option<crate::motion_field::SavedMv> = None; |
| 5843 | for (rf, mv) in [ |
| 5844 | (info.ref_frame, Some(mv32(info.mv))), |
| 5845 | (info.ref_frame1.max(0), Some(mv32(info.mv1))), |
| 5846 | ] { |
| 5847 | let (Some(mv), true) = (mv, rf > 0) else { |
| 5848 | continue; |
| 5849 | }; |
| 5850 | if other_side[rf as usize] { |
| 5851 | continue; |
| 5852 | } |
| 5853 | if mv.0.abs() > REFMVS_LIMIT || mv.1.abs() > REFMVS_LIMIT { |
| 5854 | continue; |
| 5855 | } |
| 5856 | saved = Some(crate::motion_field::SavedMv { mv, ref_frame: rf }); |
| 5857 | } |
| 5858 | out[col / 2] = saved; |
| 5859 | } |
| 5860 | } |
| 5861 | }; |
| 5862 | // lane-mf: the whole-frame pass split over the filter pool in cell-row |
| 5863 | // bands (same `Shared` corner-cut as the filter stages -- disjointness is |
| 5864 | // the band arithmetic above, proved by the 1-vs-16-thread byte-exact gate). |
| 5865 | let bands = crate::par::bands(rows, crate::par::filter_threads()); |
| 5866 | if bands.len() <= 1 { |
| 5867 | fill(0, rows, cells); |
| 5868 | } else { |
| 5869 | let shared = crate::par::Shared::new(cells); |
| 5870 | crate::par::run_bands(&bands, fctx, |r0, r1, _| { |
| 5871 | // SAFETY: cell rows `r0..r1` are this band's alone. |
| 5872 | let cells = unsafe { shared.get() }; |
| 5873 | fill(r0, r1, &mut cells[r0 * cols..r1 * cols]); |
| 5874 | }); |
| 5875 | } |
| 5876 | if crate::envflags::env_flag!("EC_TRACE_TPL") { |
| 5877 | eprintln!("EC_TPL_SAVED oh={order_hint}"); |
| 5878 | for (r, row) in field.occupancy().iter().enumerate() { |
| 5879 | eprintln!("EC_TPL_SAVED r{r} {row}"); |
| 5880 | } |
| 5881 | } |
| 5882 | field |
| 5883 | } |
| 5884 | |
| 5885 | /// Current value of the counter for `ref_frame` (`LAST2_FRAME`..=`ALTREF_FRAME`; |
| 5886 | /// panics on `LAST_FRAME`/`INTRA_FRAME`/`NONE`, which have no counter here). |
| 5887 | #[allow(dead_code)] // read only from the `#[cfg(test)]` gates |
| 5888 | pub(crate) fn ref_hits(ref_frame: i8) -> usize { |
| 5889 | let c = match ref_frame { |
| 5890 | LAST2_FRAME => &LAST2_HITS, |
| 5891 | LAST3_FRAME => &LAST3_HITS, |
| 5892 | GOLDEN_FRAME => &GOLDEN_HITS, |
| 5893 | BWDREF_FRAME => &BWDREF_HITS, |
| 5894 | ALTREF2_FRAME => &ALTREF2_HITS, |
| 5895 | ALTREF_FRAME => &ALTREF_HITS, |
| 5896 | other => panic!("ref_hits: no per-reference counter for ref_frame {other}"), |
| 5897 | }; |
| 5898 | c.with(|v| v.get()) |
| 5899 | } |
| 5900 | |
| 5901 | /// The sequence header's `enable_intra_edge_filter`, set once per |
| 5902 | /// [`decode_key_frame_tile_with_cdfs`] call: [`PlaneBuf::reconstruct`]'s |
| 5903 | /// own read of it, kept off the call stack (see that function's own doc |
| 5904 | /// comment for why) rather than threaded through every `read_plane`/ |
| 5905 | /// `decode_block`/`decode_leaf8` call between here and there. |
| 5906 | |
| 5907 | /// Set [`ENABLE_EDGE_FILTER`] for this thread. |
| 5908 | /// |
| 5909 | /// `decode_key_frame_tile_with_cdfs` sets it, but `decode_inter_frame_tile` |
| 5910 | /// never did -- so an inter frame's intra blocks read whatever value the last |
| 5911 | /// key frame decoded ON THIS THREAD left behind. Inside one stream that is |
| 5912 | /// harmless (its own key frame sets it first), but a caller that decodes an |
| 5913 | /// inter frame directly, or a test harness running several streams on one |
| 5914 | /// thread, inherits a foreign sequence's bit: decode stops being a pure |
| 5915 | /// function of the bitstream. `decode_stream` now calls this once per stream |
| 5916 | /// from the sequence header, which covers every frame including inter ones. |
| 5917 | pub(crate) fn set_enable_edge_filter(value: bool, fctx: &crate::decode::FrameCtx) { |
| 5918 | fctx.enable_edge_filter.with(|f| f.set(value)); |
| 5919 | } |
| 5920 | |
| 5921 | /// Current value of [`ANGLE_DELTA_HITS`]. |
| 5922 | #[allow(dead_code)] // read only from the `#[cfg(test)]` gates |
| 5923 | pub(crate) fn angle_delta_hits() -> usize { |
| 5924 | ANGLE_DELTA_HITS.with(|c| c.get()) |
| 5925 | } |
| 5926 | |
| 5927 | /// Current value of [`TX_CLASS1_HITS`]. |
| 5928 | #[allow(dead_code)] // read only from the `#[cfg(test)]` gates |
| 5929 | pub(crate) fn tx_class1_hits() -> usize { |
| 5930 | TX_CLASS1_HITS.with(|c| c.get()) |
| 5931 | } |
| 5932 | |
| 5933 | // How many `PARTITION_SPLIT` decisions below an 8x8 block (four independent |
| 5934 | // `BLOCK_4X4` leaves, spec `decode_partition`'s `bSize < BLOCK_8X8` recursion |
| 5935 | // bottom) this thread's decode actually reconstructed -- the same |
| 5936 | // before/after counter pattern as [`TX_CLASS1_HITS`], proving a stream |
| 5937 | // genuinely exercised the sub-8x8 leaf path rather than every attempt |
| 5938 | // coincidentally landing on `PARTITION_NONE`. |
| 5939 | thread_local! { |
| 5940 | static SUB8_SPLIT_HITS: std::cell::Cell<usize> = const { std::cell::Cell::new(0) }; |
| 5941 | /// How many 8x8 groups an INTER frame split into four BLOCK_4X4 inter |
| 5942 | /// sub-blocks (lane-intersub8). |
| 5943 | static SUB8_INTER_SPLIT_HITS: std::cell::Cell<usize> = const { std::cell::Cell::new(0) }; |
| 5944 | /// How many 8x8 groups an INTER frame split into two rectangular sub-8x8 |
| 5945 | /// inter leaves (lane-intersub8 r3), indexed `[0] = HORZ` (two BLOCK_8X4), |
| 5946 | /// `[1] = VERT` (two BLOCK_4X8). |
| 5947 | static SUB8_INTER_RECT_HITS: std::cell::Cell<[usize; 2]> = const { std::cell::Cell::new([0; 2]) }; |
| 5948 | /// lane-sub8intra: INTRA leaves decoded inside an inter frame's sub-8x8 |
| 5949 | /// HORZ/VERT partition -- `[0]` 8x4, `[1]` 4x8, `[2]` of those that were |
| 5950 | /// the chroma reference (so coded the group's 4x4 chroma intra), `[3]` |
| 5951 | /// MIXED groups whose chroma reference is INTER while its sibling is |
| 5952 | /// intra (the `is_sub8x8_inter` false path), `[4]` INTRA `BLOCK_4X4` |
| 5953 | /// leaves of an inter frame's sub-8x8 `PARTITION_SPLIT` (lane-sub8x4 r2 -- |
| 5954 | /// the second lifted refusal's own witness; `[0]`/`[1]` count those too, |
| 5955 | /// so only this cell separates the split arm from the HORZ/VERT one). |
| 5956 | static SUB8_INTRA_RECT_HITS: std::cell::Cell<[usize; 5]> = const { std::cell::Cell::new([0; 5]) }; |
| 5957 | } |
| 5958 | |
| 5959 | /// Current value of [`SUB8_INTRA_RECT_HITS`]: `(8x4, 4x8, chroma-reference, |
| 5960 | /// mixed groups, split 4x4 leaves)`. |
| 5961 | pub fn sub8_intra_rect_hits() -> (usize, usize, usize, usize, usize) { |
| 5962 | SUB8_INTRA_RECT_HITS.with(|c| { |
| 5963 | let h = c.get(); |
| 5964 | (h[0], h[1], h[2], h[3], h[4]) |
| 5965 | }) |
| 5966 | } |
| 5967 | |
| 5968 | /// Current value of [`SUB8_INTER_RECT_HITS`]: `(8x4 groups, 4x8 groups)`. |
| 5969 | pub fn sub8_inter_rect_hits() -> (usize, usize) { |
| 5970 | SUB8_INTER_RECT_HITS.with(|c| { |
| 5971 | let h = c.get(); |
| 5972 | (h[0], h[1]) |
| 5973 | }) |
| 5974 | } |
| 5975 | |
| 5976 | // lane-cdef r1: (a) how many 8x8 CDEF units `apply_cdef` EXCLUDED because all |
| 5977 | // four of their mi cells are `skip_txfm` (libaom `cdef.c:29-38` |
| 5978 | // `is_8x8_block_skip` / `av1_cdef_compute_sb_list`'s dlist test), and (b) how |
| 5979 | // many 8x8 inter leaves (`decode_inter_block8`) wrote that skip band at all. |
| 5980 | // (b) was 0 for every stream before this round -- the leaf path never touched |
| 5981 | // the band, so an all-skip 8x8 group kept the PREVIOUS frame's flags and got |
| 5982 | // filtered where libaom skips it. A gate asserts both are non-zero. |
| 5983 | thread_local! { |
| 5984 | static CDEF_SKIPPED_UNITS: std::cell::Cell<usize> = const { std::cell::Cell::new(0) }; |
| 5985 | static INTER8_SKIP_BAND_HITS: std::cell::Cell<usize> = const { std::cell::Cell::new(0) }; |
| 5986 | // lane-cdef r2: (c) 8x8 CDEF units that are FILTERED and whose extent |
| 5987 | // crosses the cropped frame edge -- the only units for which "clamp at |
| 5988 | // the crop" and "pad past the mi-rounded extent with CDEF_VERY_LARGE" |
| 5989 | // (libaom `cdef.c:249-256`, `cdef_prepare_fb`'s hsize/vsize are |
| 5990 | // `nhb << mi_wide_l2` off the MI grid, not the crop) differ. A frame |
| 5991 | // whose width or height is not a multiple of 8 is the only shape that |
| 5992 | // produces one; the straddling-band gate arms assert it grew. |
| 5993 | static CDEF_STRADDLE_UNITS: std::cell::Cell<usize> = const { std::cell::Cell::new(0) }; |
| 5994 | // lane-cdefstrip r1: 8x8 CDEF units visited with an UNWRITTEN skip band |
| 5995 | // (see `Neighbours::skip_written`). |
| 5996 | static CDEF_UNWRITTEN_SKIP_UNITS: std::cell::Cell<usize> = const { std::cell::Cell::new(0) }; |
| 5997 | // lane-cdefstrip r1: (d) skip RECTANGULAR blocks (every strip arm: 32x8, |
| 5998 | // 8x32, 16x8, 32x16, 64x16, AB/1:4 pieces...) that wrote the CDEF skip |
| 5999 | // band, and (e) 8x8 CDEF units whose four mi cells do NOT agree on skip -- |
| 6000 | // the shape libaom's `is_8x8_block_skip` (`cdef.c:29-38`) exists for: an |
| 6001 | // 8-tall strip pair where only one half is skip must still be FILTERED. |
| 6002 | static RECT_SKIP_BAND_HITS: std::cell::Cell<usize> = const { std::cell::Cell::new(0) }; |
| 6003 | static CDEF_MIXED_SKIP_UNITS: std::cell::Cell<usize> = const { std::cell::Cell::new(0) }; |
| 6004 | } |
| 6005 | |
| 6006 | /// Current value of `RECT_SKIP_BAND_HITS`. |
| 6007 | pub fn rect_skip_band_hits() -> usize { |
| 6008 | RECT_SKIP_BAND_HITS.with(|c| c.get()) |
| 6009 | } |
| 6010 | |
| 6011 | /// Current value of `CDEF_MIXED_SKIP_UNITS`. |
| 6012 | pub fn cdef_mixed_skip_units() -> usize { |
| 6013 | CDEF_MIXED_SKIP_UNITS.with(|c| c.get()) |
| 6014 | } |
| 6015 | |
| 6016 | /// Current value of `CDEF_SKIPPED_UNITS`. |
| 6017 | #[allow(dead_code)] // read only from the `#[cfg(test)]` gates |
| 6018 | pub(crate) fn cdef_skipped_units() -> usize { |
| 6019 | CDEF_SKIPPED_UNITS.with(|c| c.get()) |
| 6020 | } |
| 6021 | |
| 6022 | /// How many 8x8 CDEF units [`apply_cdef`] visited whose skip band no coded |
| 6023 | /// block wrote this frame -- a writer gap, always a defect (lane-cdefstrip r1). |
| 6024 | pub fn cdef_unwritten_skip_units() -> usize { |
| 6025 | CDEF_UNWRITTEN_SKIP_UNITS.with(|c| c.get()) |
| 6026 | } |
| 6027 | |
| 6028 | /// Current value of `INTER8_SKIP_BAND_HITS`. |
| 6029 | #[allow(dead_code)] // read only from the `#[cfg(test)]` gates |
| 6030 | pub(crate) fn inter8_skip_band_hits() -> usize { |
| 6031 | INTER8_SKIP_BAND_HITS.with(|c| c.get()) |
| 6032 | } |
| 6033 | |
| 6034 | /// Current value of `CDEF_STRADDLE_UNITS`. |
| 6035 | #[allow(dead_code)] // read only from the `#[cfg(test)]` gates |
| 6036 | pub(crate) fn cdef_straddle_units() -> usize { |
| 6037 | CDEF_STRADDLE_UNITS.with(|c| c.get()) |
| 6038 | } |
| 6039 | |
| 6040 | /// Current value of [`SUB8_SPLIT_HITS`]. |
| 6041 | #[allow(dead_code)] // read only from the `#[cfg(test)]` gates |
| 6042 | pub(crate) fn sub8_split_hits() -> usize { |
| 6043 | SUB8_SPLIT_HITS.with(|c| c.get()) |
| 6044 | } |
| 6045 | |
| 6046 | /// Current value of [`SUB8_INTER_SPLIT_HITS`]. |
| 6047 | pub fn sub8_inter_split_hits() -> usize { |
| 6048 | SUB8_INTER_SPLIT_HITS.with(|c| c.get()) |
| 6049 | } |
| 6050 | |
| 6051 | // lane-t900 r7: sub-8x8 inter groups whose one chroma unit inherits a |
| 6052 | // `tx_type` DIFFERENT from the group's FIRST sub-block -- the only shape that |
| 6053 | // tells the two rules apart (`av1_get_tx_type` indexes `xd->tx_type_map`, |
| 6054 | // which `set_mi_offsets` re-points to the CHROMA-REFERENCE sub-block, so the |
| 6055 | // first sub-block's type is never the one). Reading it off sub-block 0 |
| 6056 | // inverted one 4x4 chroma unit on the wrong basis with the entropy stream |
| 6057 | // perfectly in sync (no chroma `tx_type` symbol is coded), so a gate can only |
| 6058 | // see this rule through this counter. |
| 6059 | thread_local! { |
| 6060 | static SUB8_CHROMA_TX_FROM_REF_HITS: std::cell::Cell<usize> = const { std::cell::Cell::new(0) }; |
| 6061 | } |
| 6062 | |
| 6063 | /// Current value of `SUB8_CHROMA_TX_FROM_REF_HITS`. |
| 6064 | pub fn sub8_chroma_tx_from_ref_hits() -> usize { |
| 6065 | SUB8_CHROMA_TX_FROM_REF_HITS.with(|c| c.get()) |
| 6066 | } |
| 6067 | |
| 6068 | fn note_sub8_chroma_tx(first_sub: TxType, chroma_ref: TxType, coeffs: bool) { |
| 6069 | if coeffs && first_sub != chroma_ref { |
| 6070 | hit!(SUB8_CHROMA_TX_FROM_REF_HITS); |
| 6071 | } |
| 6072 | } |
| 6073 | |
| 6074 | // How many 4x8 / 8x4 leaves (lane-tx4x8's `PARTITION_VERT`/`PARTITION_HORZ` |
| 6075 | // of an 8x8 block) this thread's decode reconstructed with REAL coefficients |
| 6076 | // -- a skipped leaf exercises the prediction and the mi bookkeeping but never |
| 6077 | // the rect transform, the 32-position `eob_pt` or the rect scan, so the gate |
| 6078 | // asserts on these two rather than a plain leaf count. |
| 6079 | thread_local! { |
| 6080 | static TX4X8_CODED_HITS: std::cell::Cell<usize> = const { std::cell::Cell::new(0) }; |
| 6081 | static TX8X4_CODED_HITS: std::cell::Cell<usize> = const { std::cell::Cell::new(0) }; |
| 6082 | /// Sub-8x8 rect leaves whose `tx_depth` symbol resolved to 1 -- the leaf's |
| 6083 | /// two 4x4 transform units, predicted one after the other. A separate |
| 6084 | /// counter from [`TX_DEPTH_HITS`] (which every square path also bumps) so |
| 6085 | /// the gate can prove THIS path fired. |
| 6086 | static RECT8_SPLIT_TX_HITS: std::cell::Cell<usize> = const { std::cell::Cell::new(0) }; |
| 6087 | /// 16x8/8x16 intra strips whose prediction actually read past the block's |
| 6088 | /// own width (above-right) or height (below-left) -- the samples |
| 6089 | /// `Reach::of_rect` decides on, and the only ones a wrong `has_tr_*`/ |
| 6090 | /// `has_bl_*` table row can corrupt (lane-tx4x8 r3). |
| 6091 | static RECT_STRIP_REACH_HITS: std::cell::Cell<usize> = const { std::cell::Cell::new(0) }; |
| 6092 | /// Every 16x8/8x16 intra strip predicted, reaching or not -- the |
| 6093 | /// denominator [`RECT_STRIP_REACH_HITS`] is a fraction of. |
| 6094 | static RECT_STRIP_PRED_HITS: std::cell::Cell<usize> = const { std::cell::Cell::new(0) }; |
| 6095 | } |
| 6096 | |
| 6097 | /// Current value of [`RECT8_SPLIT_TX_HITS`]. |
| 6098 | #[allow(dead_code)] // read only from the `#[cfg(test)]` gates |
| 6099 | pub(crate) fn rect8_split_tx_hits() -> usize { |
| 6100 | RECT8_SPLIT_TX_HITS.with(|c| c.get()) |
| 6101 | } |
| 6102 | |
| 6103 | /// Current value of [`RECT_STRIP_PRED_HITS`]. |
| 6104 | #[allow(dead_code)] // read only from the `#[cfg(test)]` gates |
| 6105 | pub(crate) fn rect_strip_pred_hits() -> usize { |
| 6106 | RECT_STRIP_PRED_HITS.with(|c| c.get()) |
| 6107 | } |
| 6108 | |
| 6109 | /// Current value of [`RECT_STRIP_REACH_HITS`]. |
| 6110 | #[allow(dead_code)] // read only from the `#[cfg(test)]` gates |
| 6111 | pub(crate) fn rect_strip_reach_hits() -> usize { |
| 6112 | RECT_STRIP_REACH_HITS.with(|c| c.get()) |
| 6113 | } |
| 6114 | |
| 6115 | /// Current value of [`TX4X8_CODED_HITS`]. |
| 6116 | #[allow(dead_code)] // read only from the `#[cfg(test)]` gates |
| 6117 | pub(crate) fn tx4x8_coded_hits() -> usize { |
| 6118 | TX4X8_CODED_HITS.with(|c| c.get()) |
| 6119 | } |
| 6120 | |
| 6121 | /// Current value of [`TX8X4_CODED_HITS`]. |
| 6122 | #[allow(dead_code)] // read only from the `#[cfg(test)]` gates |
| 6123 | pub(crate) fn tx8x4_coded_hits() -> usize { |
| 6124 | TX8X4_CODED_HITS.with(|c| c.get()) |
| 6125 | } |
| 6126 | |
| 6127 | // How many CHROMA coefficient blocks read their `eob_pt` from the class-1 |
| 6128 | // (`TX_CLASS_HORIZ`/`VERT`) row, across every call on the current thread. |
| 6129 | // Chroma never codes a `tx_type` symbol -- it inherits the colocated luma |
| 6130 | // type verbatim (`av1_get_tx_type`, `blockd.h:1291`) -- so this fires only on |
| 6131 | // an inter leaf whose luma picked a 1D type, the path lane-eobc1 wired |
| 6132 | // (`eob_pt_{16,64,128,256}_chroma_class1`). |
| 6133 | thread_local! { |
| 6134 | static CHROMA_EOB_CLASS1_HITS: std::cell::Cell<usize> = |
| 6135 | const { std::cell::Cell::new(0) }; |
| 6136 | } |
| 6137 | |
| 6138 | /// Current value of [`CHROMA_EOB_CLASS1_HITS`]. |
| 6139 | #[allow(dead_code)] // read only from the `#[cfg(test)]` gates |
| 6140 | pub(crate) fn chroma_eob_class1_hits() -> usize { |
| 6141 | CHROMA_EOB_CLASS1_HITS.with(|c| c.get()) |
| 6142 | } |
| 6143 | |
| 6144 | /// Counts one [`CHROMA_EOB_CLASS1_HITS`] when `plane_idx`'s inherited |
| 6145 | /// `tx_type` put this block's `eob_pt` on the class-1 row. |
| 6146 | fn note_chroma_class1(plane_idx: usize, tx_type: TxType) { |
| 6147 | if plane_idx > 0 && TxClass::of(tx_type) != TxClass::TwoD { |
| 6148 | hit!(CHROMA_EOB_CLASS1_HITS); |
| 6149 | } |
| 6150 | } |
| 6151 | |
| 6152 | const SUB_MI: u32 = 4; |
| 6153 | const MI: usize = 4; |
| 6154 | const SB: usize = 64; |
| 6155 | /// The `above_side`/`left_side` value a cell with no decoded neighbour holds |
| 6156 | /// (tile start, superblock-row start). |
| 6157 | /// |
| 6158 | /// [`Neighbours::partition_ctx_mi`] reads it as libaom's |
| 6159 | /// `av1_get_partition_plane_context` reads its own zero-initialised |
| 6160 | /// `above_partition_context` byte: bit `bsl` clear, i.e. "the neighbour is at |
| 6161 | /// least as wide as this block". lane-sb128 r1: the old sentinel was `SB` |
| 6162 | /// (64), which reads identically at every level up to 64x64 but sets the bit |
| 6163 | /// at the `BLOCK_128X128` root (64 * 2 <= 128) -- the 128-level partition |
| 6164 | /// symbol was read off CDF row 3 where libaom uses row 0 (aomdec |
| 6165 | /// `EC_PART mi_row=0 mi_col=0 bsize=15 ctx=16`, i.e. `bsl*4 + 0`). |
| 6166 | const NO_NEIGHBOUR_SIDE: usize = 128; |
| 6167 | const BLOCK: usize = 32; |
| 6168 | const SUB: usize = 16; |
| 6169 | const TX32: usize = 32; |
| 6170 | const TX16: usize = 16; |
| 6171 | const TX8: usize = 8; |
| 6172 | const TX4: usize = 4; |
| 6173 | const DC_PRED: usize = 0; |
| 6174 | /// `V_PRED` (spec 6.10.2): the first directional mode, and so the first that |
| 6175 | /// carries an angle delta — [`crate::tile`]'s own private copy of the same |
| 6176 | /// constant. |
| 6177 | const V_PRED: usize = 1; |
| 6178 | const H_PRED: usize = 2; |
| 6179 | /// `D157_PRED`: `fimode_to_intradir`'s target for `FILTER_D157_PRED` (spec |
| 6180 | /// `Fimode_To_Intradir`, libaom `blockd.h`). |
| 6181 | const D157_PRED: usize = 6; |
| 6182 | /// The last directional mode, `D67_PRED`. |
| 6183 | const D67_PRED: usize = 8; |
| 6184 | /// The symbol an angle delta of zero codes as (spec 9.3): the alphabet runs |
| 6185 | /// from -3 to +3, so this is its middle — [`crate::tile`]'s writer never |
| 6186 | /// codes anything else, so this decoder never reads anything else either. |
| 6187 | const ANGLE_DELTA_ZERO: usize = 3; |
| 6188 | const NUM_BASE_LEVELS: i32 = 2; |
| 6189 | const COEFF_BASE_RANGE: i32 = 12; |
| 6190 | const BR_STEP: i32 = 3; |
| 6191 | const MAX_BR_LEVEL: i32 = NUM_BASE_LEVELS + COEFF_BASE_RANGE; |
| 6192 | |
| 6193 | /// `partition_gather_vert_alike`/`_horz_alike` (spec 9.3): the partition types |
| 6194 | /// whose probability mass a superblock or block half outside the frame |
| 6195 | /// gathers into its one remaining flag — [`crate::tile`]'s own private copy, |
| 6196 | /// duplicated here (that module is another lane's territory this round) and |
| 6197 | /// kept in step with it by the byte-exact round-trip tests below rather than |
| 6198 | /// by sharing code. |
| 6199 | const VERT_ALIKE: [usize; 6] = [2, PARTITION_SPLIT, 4, 6, 7, 9]; |
| 6200 | const HORZ_ALIKE: [usize; 6] = [1, PARTITION_SPLIT, 4, 5, 6, 8]; |
| 6201 | |
| 6202 | fn trace_edge(mi_r: usize, mi_c: usize, bsize: u32, ctx: usize, rng: u32) { |
| 6203 | if crate::envflags::env_flag!("EC_AV1_TRACE") { |
| 6204 | eprintln!("OUR_PART mi_row={mi_r} mi_col={mi_c} bsize={bsize} ctx={ctx} rng={rng}"); |
| 6205 | } |
| 6206 | } |
| 6207 | |
| 6208 | fn element_prob(cdf: &[u16], element: usize) -> u16 { |
| 6209 | cdf[element] - if element > 0 { cdf[element - 1] } else { 0 } |
| 6210 | } |
| 6211 | |
| 6212 | /// The two-symbol CDF a partial superblock/block's forced-split flag is read |
| 6213 | /// with: the mass of `elements` becomes the probability of the one partition |
| 6214 | /// the frame edge still allows, mirroring [`crate::tile`]'s own `gather` |
| 6215 | /// (the encoder's non-adapting counterpart to this read). |
| 6216 | fn gather(cdf: &[u16], elements: [usize; 6]) -> [u16; 3] { |
| 6217 | let split: u16 = elements.iter().map(|&e| element_prob(cdf, e)).sum(); |
| 6218 | [32768 - split, 32768, 0] |
| 6219 | } |
| 6220 | |
| 6221 | fn unsupported(what: impl Into<String>) -> Error { |
| 6222 | Error::unsupported("AV1 tile", what.into()) |
| 6223 | } |
| 6224 | |
| 6225 | /// The coefficient q-context a frame's `base_q_idx` picks its default CDFs |
| 6226 | /// from (spec `Get_Qctx`), mirroring [`crate::tile`]'s private copy of the |
| 6227 | /// same rule. |
| 6228 | pub(crate) fn q_ctx_of(base_q_idx: u8) -> usize { |
| 6229 | match base_q_idx { |
| 6230 | 0..=20 => 0, |
| 6231 | 21..=60 => 1, |
| 6232 | 61..=120 => 2, |
| 6233 | _ => 3, |
| 6234 | } |
| 6235 | } |
| 6236 | |
| 6237 | /// `Default_Scan_NxN` (spec 8.4.2), the same anti-diagonal walk |
| 6238 | /// [`crate::tile`]'s writer generates its scan tables with. |
| 6239 | fn default_scan(side: usize) -> &'static [u16] { |
| 6240 | // One table per transform side (4/8/16/32), built once: this used to be a |
| 6241 | // fresh heap allocation plus the whole anti-diagonal walk on every |
| 6242 | // transform block read. |
| 6243 | static SCANS: std::sync::LazyLock<[Vec<u16>; 4]> = std::sync::LazyLock::new(|| { |
| 6244 | [ |
| 6245 | default_scan_gen(4), |
| 6246 | default_scan_gen(8), |
| 6247 | default_scan_gen(16), |
| 6248 | default_scan_gen(32), |
| 6249 | ] |
| 6250 | }); |
| 6251 | assert!( |
| 6252 | matches!(side, 4 | 8 | 16 | 32), |
| 6253 | "no default scan for a {side}-point transform" |
| 6254 | ); |
| 6255 | &SCANS[side.trailing_zeros() as usize - 2] |
| 6256 | } |
| 6257 | |
| 6258 | fn default_scan_gen(side: usize) -> Vec<u16> { |
| 6259 | let mut scan = Vec::with_capacity(side * side); |
| 6260 | for d in 0..(2 * side - 1) { |
| 6261 | let lo = d.saturating_sub(side - 1); |
| 6262 | let hi = d.min(side - 1); |
| 6263 | let diagonal = (lo..=hi).map(|row| (row * side + (d - row)) as u16); |
| 6264 | if d % 2 == 0 { |
| 6265 | scan.extend(diagonal.rev()); |
| 6266 | } else { |
| 6267 | scan.extend(diagonal); |
| 6268 | } |
| 6269 | } |
| 6270 | scan |
| 6271 | } |
| 6272 | |
| 6273 | /// `Mrow_Scan`/`Mcol_Scan` (spec 8.4.2, libaom `scan.c`): the two |
| 6274 | /// class-specific scans a `V_DCT`/`H_DCT` transform block reads coefficients |
| 6275 | /// in, instead of the default zigzag. libaom's own tables are keyed by a |
| 6276 | /// *column-major* raster (`get_txb_bhl`'s `col = pos / height`) over its |
| 6277 | /// physically column-major level buffer, but they decompose to the exact |
| 6278 | /// same (row, col) walk as a flat row-major traversal of *our* row-major |
| 6279 | /// `grid`/`levels` (`pos = row * side + col`) once you read them that way: |
| 6280 | /// `Mrow_Scan` visits row 0 across every column, then row 1, ... — precisely |
| 6281 | /// `0..side*side` in our own indexing — and `Mcol_Scan` is that walk |
| 6282 | /// transposed (column 0 down every row, then column 1, ...). |
| 6283 | pub(crate) fn class_scan_table(side: usize, class: TxClass) -> Vec<u16> { |
| 6284 | class_scan_table_wh(side, side, class) |
| 6285 | } |
| 6286 | |
| 6287 | /// [`class_scan_table`] widened to `(w, h)` (lane-tx4x8). Checked against |
| 6288 | /// libaom's own `mrow_scan_4x8`/`mcol_scan_4x8`/`mrow_scan_8x4`/ |
| 6289 | /// `mcol_scan_8x4` (`scan.c`) converted out of their column-major encoding |
| 6290 | /// by `(p % h) * w + p / h`, the same conversion [`SCAN_16X8`] was |
| 6291 | /// transcribed with -- `mrow` is the plain row-major raster in our indexing |
| 6292 | /// and `mcol` its transpose, at rect sizes exactly as at square ones. |
| 6293 | fn class_scan_table_wh(w: usize, h: usize, class: TxClass) -> Vec<u16> { |
| 6294 | match class { |
| 6295 | TxClass::Vert => (0..(w * h) as u16).collect(), |
| 6296 | TxClass::Horiz => (0..w) |
| 6297 | .flat_map(|col| (0..h).map(move |row| (row * w + col) as u16)) |
| 6298 | .collect(), |
| 6299 | TxClass::TwoD => unreachable!("class_scan is only for V_DCT/H_DCT"), |
| 6300 | } |
| 6301 | } |
| 6302 | |
| 6303 | fn eob_coeff_ctx(scan_idx: usize, area: usize) -> usize { |
| 6304 | match scan_idx { |
| 6305 | 0 => 0, |
| 6306 | i if i <= area / 8 => 1, |
| 6307 | i if i <= area / 4 => 2, |
| 6308 | _ => 3, |
| 6309 | } |
| 6310 | } |
| 6311 | |
| 6312 | /// One cell of [`read_coeffs`]'s level buffer. |
| 6313 | /// |
| 6314 | /// lane-coef2: the buffer is now PADDED (libaom's `TX_PAD_HOR`/`TX_PAD_VER`, |
| 6315 | /// `txb_common.h`) -- four zero columns to the right of `side` and four zero |
| 6316 | /// rows below it, at a fixed `stride = side + 4` -- so the four-away reads |
| 6317 | /// `base_ctx` makes (`(0, 4)`/`(4, 0)` are its widest offsets) land on real |
| 6318 | /// zero cells instead of needing the `row >= side || col >= side` edge test |
| 6319 | /// this used to run five times per coefficient. Nothing writes a padding |
| 6320 | /// cell, so the values are the same zeros the test returned. |
| 6321 | #[inline] |
| 6322 | fn neighbour(grid: &[u8], stride: usize, row: usize, col: usize) -> i32 { |
| 6323 | grid[row * stride + col] as i32 |
| 6324 | } |
| 6325 | |
| 6326 | /// spec 5.11.39's three `TxClass`es (`tx_type_to_class`, libaom |
| 6327 | /// `txb_common.h`): everything but the two lone-axis-identity types this |
| 6328 | /// decoder reads (`V_DCT`/`H_DCT`) is `TX_CLASS_2D`. |
| 6329 | #[derive(Clone, Copy, PartialEq, Eq)] |
| 6330 | pub(crate) enum TxClass { |
| 6331 | TwoD, |
| 6332 | Horiz, |
| 6333 | Vert, |
| 6334 | } |
| 6335 | |
| 6336 | impl TxClass { |
| 6337 | pub(crate) fn of(tx_type: TxType) -> Self { |
| 6338 | match tx_type { |
| 6339 | // `TX_CLASS_VERT`/`TX_CLASS_HORIZ` (`txb_common.h`'s `tx_type_to_class`) |
| 6340 | // key on axis alone -- FLIPADST is still ADST for this purpose. |
| 6341 | TxType::VDct | TxType::VAdst | TxType::VFlipAdst => Self::Vert, |
| 6342 | TxType::HDct | TxType::HAdst | TxType::HFlipAdst => Self::Horiz, |
| 6343 | _ => Self::TwoD, |
| 6344 | } |
| 6345 | } |
| 6346 | } |
| 6347 | |
| 6348 | /// `nz_map_ctx_offset_1d`: `TX_CLASS_HORIZ`/`TX_CLASS_VERT` share one 32-entry |
| 6349 | /// table (indexed by the position along the transform's *coded* axis -- |
| 6350 | /// `col` for horiz, `row` for vert) that places their contexts past the 2D |
| 6351 | /// table's own 26 rows (`SIG_COEF_CONTEXTS_2D`), 16 rows total |
| 6352 | /// (`SIG_COEF_CONTEXTS_1D`) split 26/31/36. |
| 6353 | pub(crate) fn nz_map_ctx_offset_1d(i: usize) -> usize { |
| 6354 | match i { |
| 6355 | 0 => 26, |
| 6356 | 1 => 31, |
| 6357 | _ => 36, |
| 6358 | } |
| 6359 | } |
| 6360 | |
| 6361 | fn base_ctx( |
| 6362 | grid: &[u8], |
| 6363 | stride: usize, |
| 6364 | row: usize, |
| 6365 | col: usize, |
| 6366 | class: TxClass, |
| 6367 | rect_shape: Option<(usize, usize)>, |
| 6368 | ) -> usize { |
| 6369 | // libaom's `get_nz_map_ctx_from_stats` only special-cases the DC |
| 6370 | // coefficient to ctx 0 for `TX_CLASS_2D` (`(tx_class | coeff_idx) == 0`, |
| 6371 | // txb_common.h) -- `V_DCT`/`H_DCT` (class `Horiz`/`Vert`) still run the DC |
| 6372 | // position through the full 1D neighbour sum and offset table. Skipping |
| 6373 | // that for every class desynced the last (DC) coefficient of a |
| 6374 | // `TX_CLASS_HORIZ` block (lane-av1tx4 final round: real ctx 28 read as |
| 6375 | // 0, which then read one extra `BR` symbol at the max and forced a |
| 6376 | // spurious third Golomb call). |
| 6377 | if class == TxClass::TwoD && row == 0 && col == 0 { |
| 6378 | return 0; |
| 6379 | } |
| 6380 | let offsets: [(usize, usize); 5] = match class { |
| 6381 | TxClass::TwoD => [(1, 0), (0, 1), (1, 1), (2, 0), (0, 2)], |
| 6382 | TxClass::Horiz => [(1, 0), (0, 1), (0, 2), (0, 3), (0, 4)], |
| 6383 | TxClass::Vert => [(1, 0), (0, 1), (2, 0), (3, 0), (4, 0)], |
| 6384 | }; |
| 6385 | let mag: i32 = offsets |
| 6386 | .iter() |
| 6387 | .map(|&(dr, dc)| neighbour(grid, stride, row + dr, col + dc).min(3)) |
| 6388 | .sum(); |
| 6389 | let ctx = ((mag + 1) >> 1).min(4) as usize; |
| 6390 | match class { |
| 6391 | // libaom's CDF *set* for a truncated 64-wide/tall corner resolves |
| 6392 | // through `get_txsize_entropy_ctx` to the square `TX_64X64` (this |
| 6393 | // decoder's `TxbSet::Luma64`), but `get_nz_map_ctx_from_stats` |
| 6394 | // separately indexes `av1_nz_map_ctx_offset` by the raw, un-adjusted |
| 6395 | // `tx_size` -- `TX_32X64`/`TX_64X32` for a superblock-level HORZ/VERT |
| 6396 | // strip, which own a genuinely different table from the square one |
| 6397 | // (lane-sbpart r8 root cause: `(row=1, col=0)` reads ctx 2 under the |
| 6398 | // square table vs the real ctx 13 the rect table gives). |
| 6399 | TxClass::TwoD => { |
| 6400 | // lane-rectsplit r4: apply libaom's GENERATING RULE |
| 6401 | // (`txb_common.h:199-209`) directly instead of indexing the |
| 6402 | // transcribed 5x5 tables. `av1_nz_map_ctx_offset[tx_size]` is a |
| 6403 | // FLAT array read at `coeff_idx`, and `coeff_idx` is COLUMN-major |
| 6404 | // (libaom decomposes it as `col = coeff_idx >> bhl`, `bhl` the |
| 6405 | // ADJUSTED height's log2, 5 for both `TX_64X32` and `TX_32X64`), |
| 6406 | // so 32 consecutive entries are one COLUMN: the [`cdf`] |
| 6407 | // transcriptions are `[col][row]` and this read had them as |
| 6408 | // `[row][col]` -- the transpose, i.e. the other shape's rule with |
| 6409 | // 11 and 16 swapped. Same values as [`base_ctx_rect`] now, stated |
| 6410 | // once; `base_ctx_rect_offsets_match_the_transcribed_tables_over_the_whole_domain` |
| 6411 | // pins the rule against the tables over their whole 5x5 domain. |
| 6412 | if let Some((w, h)) = rect_shape { |
| 6413 | if crate::envflags::env_flag!("EC_NZOFF_DUMP") { |
| 6414 | eprintln!("NZOFF stride={stride} shape={w}x{h} row={row} col={col}"); |
| 6415 | } |
| 6416 | if w < h && row < 2 { |
| 6417 | return ctx + 11; |
| 6418 | } |
| 6419 | if w > h && col < 2 { |
| 6420 | return ctx + 16; |
| 6421 | } |
| 6422 | } |
| 6423 | ctx + cdf::NZ_MAP_CTX_OFFSET_32[row.min(4)][col.min(4)] as usize |
| 6424 | } |
| 6425 | TxClass::Horiz => ctx + nz_map_ctx_offset_1d(col.min(31)), |
| 6426 | TxClass::Vert => ctx + nz_map_ctx_offset_1d(row.min(31)), |
| 6427 | } |
| 6428 | } |
| 6429 | |
| 6430 | /// The five `base_ctx` neighbour taps and the three `br_ctx` ones as FLAT |
| 6431 | /// offsets into the padded level frame (lane-coefread): the tap pattern is a |
| 6432 | /// function of the tx class alone, which is fixed for a whole transform unit, |
| 6433 | /// so resolving it once per unit takes the class match and the `(dr, dc)` |
| 6434 | /// -> `dr * stride + dc` arithmetic out of the per-coefficient path. Same |
| 6435 | /// taps as [`base_ctx`]/[`br_ctx`] read, stated once; |
| 6436 | /// `flat_taps_match_the_row_col_neighbour_reads` pins them against the |
| 6437 | /// original `(dr, dc)` form. |
| 6438 | fn flat_taps(stride: usize, class: TxClass) -> ([usize; 5], [usize; 3]) { |
| 6439 | match class { |
| 6440 | TxClass::TwoD => ( |
| 6441 | [stride, 1, stride + 1, 2 * stride, 2], |
| 6442 | [stride, 1, stride + 1], |
| 6443 | ), |
| 6444 | TxClass::Horiz => ([stride, 1, 2, 3, 4], [stride, 1, 2]), |
| 6445 | TxClass::Vert => ( |
| 6446 | [stride, 1, 2 * stride, 3 * stride, 4 * stride], |
| 6447 | [stride, 1, 2 * stride], |
| 6448 | ), |
| 6449 | } |
| 6450 | } |
| 6451 | |
| 6452 | /// [`base_ctx`]'s neighbour sum, off flat taps: `((sum of five clamped |
| 6453 | /// neighbours) + 1) >> 1`, clamped to 4. |
| 6454 | #[inline(always)] |
| 6455 | fn base_mag(grid: &[u8], idx: usize, taps: &[usize; 5]) -> usize { |
| 6456 | let mut mag = 0u32; |
| 6457 | for &t in taps { |
| 6458 | mag += u32::from(grid[idx + t]).min(3); |
| 6459 | } |
| 6460 | (((mag + 1) >> 1) as usize).min(4) |
| 6461 | } |
| 6462 | |
| 6463 | /// [`br_ctx`]'s neighbour sum, off flat taps. |
| 6464 | #[inline(always)] |
| 6465 | fn br_mag(grid: &[u8], idx: usize, taps: &[usize; 3]) -> usize { |
| 6466 | let mut mag = 0u32; |
| 6467 | for &t in taps { |
| 6468 | mag += u32::from(grid[idx + t]); |
| 6469 | } |
| 6470 | (((mag + 1) >> 1) as usize).min(6) |
| 6471 | } |
| 6472 | |
| 6473 | /// lane-coefread: superseded on the decode path by [`br_mag`] plus the |
| 6474 | /// origin-offset tail the two coefficient readers state inline; kept as the |
| 6475 | /// `(row, col)` form `flat_taps_match_the_row_col_neighbour_reads` pins the |
| 6476 | /// flat one against. |
| 6477 | #[cfg(test)] |
| 6478 | fn br_ctx(grid: &[u8], stride: usize, row: usize, col: usize, class: TxClass) -> usize { |
| 6479 | let extra = match class { |
| 6480 | TxClass::TwoD => neighbour(grid, stride, row + 1, col + 1), |
| 6481 | TxClass::Horiz => neighbour(grid, stride, row, col + 2), |
| 6482 | TxClass::Vert => neighbour(grid, stride, row + 2, col), |
| 6483 | }; |
| 6484 | let mag = neighbour(grid, stride, row + 1, col) |
| 6485 | + neighbour(grid, stride, row, col + 1) |
| 6486 | + extra; |
| 6487 | let mag = (((mag + 1) >> 1).min(6)) as usize; |
| 6488 | if row == 0 && col == 0 { |
| 6489 | return mag; |
| 6490 | } |
| 6491 | let near_origin = match class { |
| 6492 | TxClass::TwoD => row < 2 && col < 2, |
| 6493 | TxClass::Horiz => col == 0, |
| 6494 | TxClass::Vert => row == 0, |
| 6495 | }; |
| 6496 | if near_origin { mag + 7 } else { mag + 14 } |
| 6497 | } |
| 6498 | |
| 6499 | /// `Default_Scan_32x16` (spec 8.4.2, libaom `scan.c`'s `default_scan_32x16`): |
| 6500 | /// transposed from libaom's column-major buffer into this decoder's row-major |
| 6501 | /// `pos = row * 32 + col` (lane-rectwire, verified as a bijection of `0..512` |
| 6502 | /// against the real table). Not the square zigzag formula generalized to |
| 6503 | /// unequal `w`/`h` -- checked and that does NOT reproduce this table. |
| 6504 | const SCAN_32X16: [u16; 512] = [ |
| 6505 | 0, 32, 1, 64, 33, 2, 96, 65, 34, 3, 128, 97, 66, 35, 4, 160, 129, 98, 67, 36, 5, 192, 161, |
| 6506 | 130, 99, 68, 37, 6, 224, 193, 162, 131, 100, 69, 38, 7, 256, 225, 194, 163, 132, 101, 70, 39, |
| 6507 | 8, 288, 257, 226, 195, 164, 133, 102, 71, 40, 9, 320, 289, 258, 227, 196, 165, 134, 103, 72, |
| 6508 | 41, 10, 352, 321, 290, 259, 228, 197, 166, 135, 104, 73, 42, 11, 384, 353, 322, 291, 260, 229, |
| 6509 | 198, 167, 136, 105, 74, 43, 12, 416, 385, 354, 323, 292, 261, 230, 199, 168, 137, 106, 75, 44, |
| 6510 | 13, 448, 417, 386, 355, 324, 293, 262, 231, 200, 169, 138, 107, 76, 45, 14, 480, 449, 418, |
| 6511 | 387, 356, 325, 294, 263, 232, 201, 170, 139, 108, 77, 46, 15, 481, 450, 419, 388, 357, 326, |
| 6512 | 295, 264, 233, 202, 171, 140, 109, 78, 47, 16, 482, 451, 420, 389, 358, 327, 296, 265, 234, |
| 6513 | 203, 172, 141, 110, 79, 48, 17, 483, 452, 421, 390, 359, 328, 297, 266, 235, 204, 173, 142, |
| 6514 | 111, 80, 49, 18, 484, 453, 422, 391, 360, 329, 298, 267, 236, 205, 174, 143, 112, 81, 50, 19, |
| 6515 | 485, 454, 423, 392, 361, 330, 299, 268, 237, 206, 175, 144, 113, 82, 51, 20, 486, 455, 424, |
| 6516 | 393, 362, 331, 300, 269, 238, 207, 176, 145, 114, 83, 52, 21, 487, 456, 425, 394, 363, 332, |
| 6517 | 301, 270, 239, 208, 177, 146, 115, 84, 53, 22, 488, 457, 426, 395, 364, 333, 302, 271, 240, |
| 6518 | 209, 178, 147, 116, 85, 54, 23, 489, 458, 427, 396, 365, 334, 303, 272, 241, 210, 179, 148, |
| 6519 | 117, 86, 55, 24, 490, 459, 428, 397, 366, 335, 304, 273, 242, 211, 180, 149, 118, 87, 56, 25, |
| 6520 | 491, 460, 429, 398, 367, 336, 305, 274, 243, 212, 181, 150, 119, 88, 57, 26, 492, 461, 430, |
| 6521 | 399, 368, 337, 306, 275, 244, 213, 182, 151, 120, 89, 58, 27, 493, 462, 431, 400, 369, 338, |
| 6522 | 307, 276, 245, 214, 183, 152, 121, 90, 59, 28, 494, 463, 432, 401, 370, 339, 308, 277, 246, |
| 6523 | 215, 184, 153, 122, 91, 60, 29, 495, 464, 433, 402, 371, 340, 309, 278, 247, 216, 185, 154, |
| 6524 | 123, 92, 61, 30, 496, 465, 434, 403, 372, 341, 310, 279, 248, 217, 186, 155, 124, 93, 62, 31, |
| 6525 | 497, 466, 435, 404, 373, 342, 311, 280, 249, 218, 187, 156, 125, 94, 63, 498, 467, 436, 405, |
| 6526 | 374, 343, 312, 281, 250, 219, 188, 157, 126, 95, 499, 468, 437, 406, 375, 344, 313, 282, 251, |
| 6527 | 220, 189, 158, 127, 500, 469, 438, 407, 376, 345, 314, 283, 252, 221, 190, 159, 501, 470, 439, |
| 6528 | 408, 377, 346, 315, 284, 253, 222, 191, 502, 471, 440, 409, 378, 347, 316, 285, 254, 223, 503, |
| 6529 | 472, 441, 410, 379, 348, 317, 286, 255, 504, 473, 442, 411, 380, 349, 318, 287, 505, 474, 443, |
| 6530 | 412, 381, 350, 319, 506, 475, 444, 413, 382, 351, 507, 476, 445, 414, 383, 508, 477, 446, 415, |
| 6531 | 509, 478, 447, 510, 479, 511, |
| 6532 | ]; |
| 6533 | |
| 6534 | /// `Default_Scan_16x32`, this decoder's own row-major transcription (see |
| 6535 | /// [`SCAN_32X16`]'s doc comment). |
| 6536 | const SCAN_16X32: [u16; 512] = [ |
| 6537 | 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, |
| 6538 | 66, 81, 96, 7, 22, 37, 52, 67, 82, 97, 112, 8, 23, 38, 53, 68, 83, 98, 113, 128, 9, 24, 39, 54, |
| 6539 | 69, 84, 99, 114, 129, 144, 10, 25, 40, 55, 70, 85, 100, 115, 130, 145, 160, 11, 26, 41, 56, 71, |
| 6540 | 86, 101, 116, 131, 146, 161, 176, 12, 27, 42, 57, 72, 87, 102, 117, 132, 147, 162, 177, 192, |
| 6541 | 13, 28, 43, 58, 73, 88, 103, 118, 133, 148, 163, 178, 193, 208, 14, 29, 44, 59, 74, 89, 104, |
| 6542 | 119, 134, 149, 164, 179, 194, 209, 224, 15, 30, 45, 60, 75, 90, 105, 120, 135, 150, 165, 180, |
| 6543 | 195, 210, 225, 240, 31, 46, 61, 76, 91, 106, 121, 136, 151, 166, 181, 196, 211, 226, 241, 256, |
| 6544 | 47, 62, 77, 92, 107, 122, 137, 152, 167, 182, 197, 212, 227, 242, 257, 272, 63, 78, 93, 108, |
| 6545 | 123, 138, 153, 168, 183, 198, 213, 228, 243, 258, 273, 288, 79, 94, 109, 124, 139, 154, 169, |
| 6546 | 184, 199, 214, 229, 244, 259, 274, 289, 304, 95, 110, 125, 140, 155, 170, 185, 200, 215, 230, |
| 6547 | 245, 260, 275, 290, 305, 320, 111, 126, 141, 156, 171, 186, 201, 216, 231, 246, 261, 276, 291, |
| 6548 | 306, 321, 336, 127, 142, 157, 172, 187, 202, 217, 232, 247, 262, 277, 292, 307, 322, 337, 352, |
| 6549 | 143, 158, 173, 188, 203, 218, 233, 248, 263, 278, 293, 308, 323, 338, 353, 368, 159, 174, 189, |
| 6550 | 204, 219, 234, 249, 264, 279, 294, 309, 324, 339, 354, 369, 384, 175, 190, 205, 220, 235, 250, |
| 6551 | 265, 280, 295, 310, 325, 340, 355, 370, 385, 400, 191, 206, 221, 236, 251, 266, 281, 296, 311, |
| 6552 | 326, 341, 356, 371, 386, 401, 416, 207, 222, 237, 252, 267, 282, 297, 312, 327, 342, 357, 372, |
| 6553 | 387, 402, 417, 432, 223, 238, 253, 268, 283, 298, 313, 328, 343, 358, 373, 388, 403, 418, 433, |
| 6554 | 448, 239, 254, 269, 284, 299, 314, 329, 344, 359, 374, 389, 404, 419, 434, 449, 464, 255, 270, |
| 6555 | 285, 300, 315, 330, 345, 360, 375, 390, 405, 420, 435, 450, 465, 480, 271, 286, 301, 316, 331, |
| 6556 | 346, 361, 376, 391, 406, 421, 436, 451, 466, 481, 496, 287, 302, 317, 332, 347, 362, 377, 392, |
| 6557 | 407, 422, 437, 452, 467, 482, 497, 303, 318, 333, 348, 363, 378, 393, 408, 423, 438, 453, 468, |
| 6558 | 483, 498, 319, 334, 349, 364, 379, 394, 409, 424, 439, 454, 469, 484, 499, 335, 350, 365, 380, |
| 6559 | 395, 410, 425, 440, 455, 470, 485, 500, 351, 366, 381, 396, 411, 426, 441, 456, 471, 486, 501, |
| 6560 | 367, 382, 397, 412, 427, 442, 457, 472, 487, 502, 383, 398, 413, 428, 443, 458, 473, 488, 503, |
| 6561 | 399, 414, 429, 444, 459, 474, 489, 504, 415, 430, 445, 460, 475, 490, 505, 431, 446, 461, 476, |
| 6562 | 491, 506, 447, 462, 477, 492, 507, 463, 478, 493, 508, 479, 494, 509, 495, 510, 511, |
| 6563 | ]; |
| 6564 | |
| 6565 | /// `Default_Scan_4x8` (libaom `scan.c`), this decoder's own row-major |
| 6566 | /// transcription (see [`SCAN_32X16`]'s doc comment for the conversion, which |
| 6567 | /// was re-verified this lane by regenerating [`SCAN_16X8`]/[`SCAN_8X16`] from |
| 6568 | /// the same source). |
| 6569 | const SCAN_4X8: [u16; 32] = [ |
| 6570 | 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, |
| 6571 | 23, 26, 29, 27, 30, 31, |
| 6572 | ]; |
| 6573 | |
| 6574 | /// `Default_Scan_8x4`, same source and conversion as [`SCAN_4X8`]. |
| 6575 | const SCAN_8X4: [u16; 32] = [ |
| 6576 | 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, |
| 6577 | 29, 22, 15, 30, 23, 31, |
| 6578 | ]; |
| 6579 | |
| 6580 | /// `Default_Scan_16x8`, this decoder's own row-major transcription (see |
| 6581 | /// [`SCAN_32X16`]'s doc comment). |
| 6582 | const SCAN_16X8: [u16; 128] = [ |
| 6583 | 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, |
| 6584 | 36, 21, 6, 112, 97, 82, 67, 52, 37, 22, 7, 113, 98, 83, 68, 53, 38, 23, 8, 114, 99, 84, 69, 54, |
| 6585 | 39, 24, 9, 115, 100, 85, 70, 55, 40, 25, 10, 116, 101, 86, 71, 56, 41, 26, 11, 117, 102, 87, |
| 6586 | 72, 57, 42, 27, 12, 118, 103, 88, 73, 58, 43, 28, 13, 119, 104, 89, 74, 59, 44, 29, 14, 120, |
| 6587 | 105, 90, 75, 60, 45, 30, 15, 121, 106, 91, 76, 61, 46, 31, 122, 107, 92, 77, 62, 47, 123, 108, |
| 6588 | 93, 78, 63, 124, 109, 94, 79, 125, 110, 95, 126, 111, 127, |
| 6589 | ]; |
| 6590 | |
| 6591 | /// `Default_Scan_8x16`, this decoder's own row-major transcription (see |
| 6592 | /// [`SCAN_32X16`]'s doc comment). |
| 6593 | const SCAN_8X16: [u16; 128] = [ |
| 6594 | 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, |
| 6595 | 41, 48, 7, 14, 21, 28, 35, 42, 49, 56, 15, 22, 29, 36, 43, 50, 57, 64, 23, 30, 37, 44, 51, 58, |
| 6596 | 65, 72, 31, 38, 45, 52, 59, 66, 73, 80, 39, 46, 53, 60, 67, 74, 81, 88, 47, 54, 61, 68, 75, 82, |
| 6597 | 89, 96, 55, 62, 69, 76, 83, 90, 97, 104, 63, 70, 77, 84, 91, 98, 105, 112, 71, 78, 85, 92, 99, |
| 6598 | 106, 113, 120, 79, 86, 93, 100, 107, 114, 121, 87, 94, 101, 108, 115, 122, 95, 102, 109, 116, |
| 6599 | 123, 103, 110, 117, 124, 111, 118, 125, 119, 126, 127, |
| 6600 | ]; |
| 6601 | |
| 6602 | /// `Default_Scan_32x8`, this decoder's own row-major transcription (see |
| 6603 | /// [`SCAN_32X16`]'s doc comment) -- the chroma plane of a 64x16 |
| 6604 | /// `PARTITION_HORZ_4` superblock strip (lane-tx64x16). libaom stores its |
| 6605 | /// scans column-major (`p = col * height + row`), so a naive copy of |
| 6606 | /// `default_scan_32x8` would silently install the TRANSPOSED 8x32 order |
| 6607 | /// (class reference-layout-not-spec): `default_scan_16x8`'s literal bytes |
| 6608 | /// are byte-identical to this decoder's [`SCAN_8X16`]. |
| 6609 | const SCAN_32X8: [u16; 256] = [ |
| 6610 | 0, 32, 1, 64, 33, 2, 96, 65, 34, 3, 128, 97, 66, 35, 4, 160, 129, 98, 67, 36, 5, 192, 161, 130, |
| 6611 | 99, 68, 37, 6, 224, 193, 162, 131, 100, 69, 38, 7, 225, 194, 163, 132, 101, 70, 39, 8, 226, 195, |
| 6612 | 164, 133, 102, 71, 40, 9, 227, 196, 165, 134, 103, 72, 41, 10, 228, 197, 166, 135, 104, 73, 42, |
| 6613 | 11, 229, 198, 167, 136, 105, 74, 43, 12, 230, 199, 168, 137, 106, 75, 44, 13, 231, 200, 169, |
| 6614 | 138, 107, 76, 45, 14, 232, 201, 170, 139, 108, 77, 46, 15, 233, 202, 171, 140, 109, 78, 47, 16, |
| 6615 | 234, 203, 172, 141, 110, 79, 48, 17, 235, 204, 173, 142, 111, 80, 49, 18, 236, 205, 174, 143, |
| 6616 | 112, 81, 50, 19, 237, 206, 175, 144, 113, 82, 51, 20, 238, 207, 176, 145, 114, 83, 52, 21, 239, |
| 6617 | 208, 177, 146, 115, 84, 53, 22, 240, 209, 178, 147, 116, 85, 54, 23, 241, 210, 179, 148, 117, |
| 6618 | 86, 55, 24, 242, 211, 180, 149, 118, 87, 56, 25, 243, 212, 181, 150, 119, 88, 57, 26, 244, 213, |
| 6619 | 182, 151, 120, 89, 58, 27, 245, 214, 183, 152, 121, 90, 59, 28, 246, 215, 184, 153, 122, 91, |
| 6620 | 60, 29, 247, 216, 185, 154, 123, 92, 61, 30, 248, 217, 186, 155, 124, 93, 62, 31, 249, 218, 187, |
| 6621 | 156, 125, 94, 63, 250, 219, 188, 157, 126, 95, 251, 220, 189, 158, 127, 252, 221, 190, 159, 253, |
| 6622 | 222, 191, 254, 223, 255, |
| 6623 | ]; |
| 6624 | /// `Default_Scan_8x32`, [`SCAN_32X8`]'s transpose (the chroma plane of a |
| 6625 | /// 16x64 `PARTITION_VERT_4` strip). |
| 6626 | const SCAN_8X32: [u16; 256] = [ |
| 6627 | 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, |
| 6628 | 41, 48, 7, 14, 21, 28, 35, 42, 49, 56, 15, 22, 29, 36, 43, 50, 57, 64, 23, 30, 37, 44, 51, 58, |
| 6629 | 65, 72, 31, 38, 45, 52, 59, 66, 73, 80, 39, 46, 53, 60, 67, 74, 81, 88, 47, 54, 61, 68, 75, 82, |
| 6630 | 89, 96, 55, 62, 69, 76, 83, 90, 97, 104, 63, 70, 77, 84, 91, 98, 105, 112, 71, 78, 85, 92, 99, |
| 6631 | 106, 113, 120, 79, 86, 93, 100, 107, 114, 121, 128, 87, 94, 101, 108, 115, 122, 129, 136, 95, |
| 6632 | 102, 109, 116, 123, 130, 137, 144, 103, 110, 117, 124, 131, 138, 145, 152, 111, 118, 125, 132, |
| 6633 | 139, 146, 153, 160, 119, 126, 133, 140, 147, 154, 161, 168, 127, 134, 141, 148, 155, 162, 169, |
| 6634 | 176, 135, 142, 149, 156, 163, 170, 177, 184, 143, 150, 157, 164, 171, 178, 185, 192, 151, 158, |
| 6635 | 165, 172, 179, 186, 193, 200, 159, 166, 173, 180, 187, 194, 201, 208, 167, 174, 181, 188, 195, |
| 6636 | 202, 209, 216, 175, 182, 189, 196, 203, 210, 217, 224, 183, 190, 197, 204, 211, 218, 225, 232, |
| 6637 | 191, 198, 205, 212, 219, 226, 233, 240, 199, 206, 213, 220, 227, 234, 241, 248, 207, 214, 221, |
| 6638 | 228, 235, 242, 249, 215, 222, 229, 236, 243, 250, 223, 230, 237, 244, 251, 231, 238, 245, 252, |
| 6639 | 239, 246, 253, 247, 254, 255, |
| 6640 | ]; |
| 6641 | /// `Default_Scan_16x4`, this decoder's own row-major transcription of |
| 6642 | /// libaom's column-major `default_scan_16x4` (`scan.c:66`) -- the CHROMA |
| 6643 | /// plane of a 32x8 `PARTITION_HORZ_4` strip at the 32x32 level |
| 6644 | /// (lane-tx64x16 r3). The transcription rule (`row * w + col` with |
| 6645 | /// `row = p % h`, `col = p / h`) was validated by REGENERATING the existing |
| 6646 | /// [`SCAN_16X8`] from `default_scan_16x8` byte for byte before these two |
| 6647 | /// were emitted -- a naive copy installs the transpose (class |
| 6648 | /// reference-layout-not-spec). |
| 6649 | const SCAN_16X4: [u16; 64] = [ |
| 6650 | 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, |
| 6651 | 53, 38, 23, 8, 54, 39, 24, 9, 55, 40, 25, 10, 56, 41, 26, 11, 57, 42, 27, 12, 58, 43, 28, 13, |
| 6652 | 59, 44, 29, 14, 60, 45, 30, 15, 61, 46, 31, 62, 47, 63, |
| 6653 | ]; |
| 6654 | /// `Default_Scan_4x16`, [`SCAN_16X4`]'s transpose (the chroma plane of an |
| 6655 | /// 8x32 `PARTITION_VERT_4` strip). |
| 6656 | const SCAN_4X16: [u16; 64] = [ |
| 6657 | 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, |
| 6658 | 23, 26, 29, 32, 27, 30, 33, 36, 31, 34, 37, 40, 35, 38, 41, 44, 39, 42, 45, 48, 43, 46, 49, 52, |
| 6659 | 47, 50, 53, 56, 51, 54, 57, 60, 55, 58, 61, 59, 62, 63, |
| 6660 | ]; |
| 6661 | |
| 6662 | /// [`neighbour`]'s rect form (lane-rectwire): same lane-coef2 padding |
| 6663 | /// (`stride = w + 4`, four spare rows), so the edge test is the zeros the |
| 6664 | /// padding already holds. |
| 6665 | #[inline] |
| 6666 | fn neighbour_rect(grid: &[u8], stride: usize, row: usize, col: usize) -> i32 { |
| 6667 | grid[row * stride + col] as i32 |
| 6668 | } |
| 6669 | |
| 6670 | /// [`base_ctx`]'s `TxClass::TwoD` arm widened to `(w, h)` (lane-rectwire): |
| 6671 | /// the rect generalization is libaom's own comment in |
| 6672 | /// `get_nz_map_ctx_from_stats` (`txb_common.h:189-224`), reduces to the exact |
| 6673 | /// same [`cdf::NZ_MAP_CTX_OFFSET_32`] table when `w == h`. Never called for |
| 6674 | /// `V_DCT`/`H_DCT` -- geometrically impossible at the two size pairs |
| 6675 | /// [`decode_block_rect`] codes (see that function's own doc comment). |
| 6676 | fn base_ctx_rect( |
| 6677 | grid: &[u8], |
| 6678 | stride: usize, |
| 6679 | w: usize, |
| 6680 | h: usize, |
| 6681 | row: usize, |
| 6682 | col: usize, |
| 6683 | class: TxClass, |
| 6684 | ) -> usize { |
| 6685 | // Same `(tx_class | coeff_idx) == 0` guard [`base_ctx`] documents: only |
| 6686 | // the 2D class short-circuits the DC position. |
| 6687 | if class == TxClass::TwoD && row == 0 && col == 0 { |
| 6688 | return 0; |
| 6689 | } |
| 6690 | let offsets: [(usize, usize); 5] = match class { |
| 6691 | TxClass::TwoD => [(1, 0), (0, 1), (1, 1), (2, 0), (0, 2)], |
| 6692 | TxClass::Horiz => [(1, 0), (0, 1), (0, 2), (0, 3), (0, 4)], |
| 6693 | TxClass::Vert => [(1, 0), (0, 1), (2, 0), (3, 0), (4, 0)], |
| 6694 | }; |
| 6695 | let mag: i32 = offsets |
| 6696 | .iter() |
| 6697 | .map(|&(dr, dc)| neighbour_rect(grid, stride, row + dr, col + dc).min(3)) |
| 6698 | .sum(); |
| 6699 | let ctx = ((mag + 1) >> 1).min(4) as usize; |
| 6700 | match class { |
| 6701 | TxClass::TwoD => { |
| 6702 | if w < h && row < 2 { |
| 6703 | return ctx + 11; |
| 6704 | } |
| 6705 | if w > h && col < 2 { |
| 6706 | return ctx + 16; |
| 6707 | } |
| 6708 | ctx + cdf::NZ_MAP_CTX_OFFSET_32[row.min(4)][col.min(4)] as usize |
| 6709 | } |
| 6710 | // Shape-independent in libaom too (`get_nz_map_ctx_from_stats`'s |
| 6711 | // `TX_CLASS_HORIZ`/`VERT` arms index `nz_map_ctx_offset_1d` by the |
| 6712 | // position along one axis only). |
| 6713 | TxClass::Horiz => ctx + nz_map_ctx_offset_1d(col.min(31)), |
| 6714 | TxClass::Vert => ctx + nz_map_ctx_offset_1d(row.min(31)), |
| 6715 | } |
| 6716 | } |
| 6717 | |
| 6718 | /// [`br_ctx`]'s `TxClass::TwoD` arm widened to `(w, h)` (lane-rectwire): the |
| 6719 | /// neighbour-offset math itself is shape-independent, only the boundary |
| 6720 | /// clamp needs the real bound in each axis. |
| 6721 | /// lane-coefread: superseded by [`br_mag`] on the rect decode path too -- |
| 6722 | /// its body was character-for-character [`br_ctx`]'s, which |
| 6723 | /// `flat_taps_match_the_row_col_neighbour_reads` now asserts. |
| 6724 | #[cfg(test)] |
| 6725 | fn br_ctx_rect( |
| 6726 | grid: &[u8], |
| 6727 | stride: usize, |
| 6728 | row: usize, |
| 6729 | col: usize, |
| 6730 | class: TxClass, |
| 6731 | ) -> usize { |
| 6732 | let extra = match class { |
| 6733 | TxClass::TwoD => neighbour_rect(grid, stride, row + 1, col + 1), |
| 6734 | TxClass::Horiz => neighbour_rect(grid, stride, row, col + 2), |
| 6735 | TxClass::Vert => neighbour_rect(grid, stride, row + 2, col), |
| 6736 | }; |
| 6737 | let mag = neighbour_rect(grid, stride, row + 1, col) |
| 6738 | + neighbour_rect(grid, stride, row, col + 1) |
| 6739 | + extra; |
| 6740 | let mag = (((mag + 1) >> 1).min(6)) as usize; |
| 6741 | if row == 0 && col == 0 { |
| 6742 | return mag; |
| 6743 | } |
| 6744 | let near_origin = match class { |
| 6745 | TxClass::TwoD => row < 2 && col < 2, |
| 6746 | TxClass::Horiz => col == 0, |
| 6747 | TxClass::Vert => row == 0, |
| 6748 | }; |
| 6749 | if near_origin { mag + 7 } else { mag + 14 } |
| 6750 | } |
| 6751 | |
| 6752 | fn dc_vote(dc: Option<bool>) -> i32 { |
| 6753 | match dc { |
| 6754 | None => 0, |
| 6755 | Some(true) => -1, |
| 6756 | Some(false) => 1, |
| 6757 | } |
| 6758 | } |
| 6759 | |
| 6760 | fn dc_sign_ctx(vote: i32) -> usize { |
| 6761 | match vote.signum() { |
| 6762 | 0 => 0, |
| 6763 | -1 => 1, |
| 6764 | _ => 2, |
| 6765 | } |
| 6766 | } |
| 6767 | |
| 6768 | /// The `tx_type` a CDF symbol names, given the width of the row it was read |
| 6769 | /// from. |
| 6770 | /// |
| 6771 | /// The row's own width names its set (spec `Tx_Type_Intra_Inv_Set*` / |
| 6772 | /// `Tx_Type_Inter_Inv_Set*`): 17 slots (16 symbols) is `EXT_TX_SET_ALL16`, 13 |
| 6773 | /// (12) `EXT_TX_SET_DTT9_IDTX_1DDCT`, 8 (7) `TX_SET_INTRA_1`, and everything |
| 6774 | /// narrower the reduced `TX_SET_INTRA_2`/`TX_SET_INTER_3` order, whose two |
| 6775 | /// members are a prefix of the five. |
| 6776 | /// |
| 6777 | /// One function rather than a `match` at each reader, so |
| 6778 | /// [`crate::refusal_inventory`]'s enumeration of every (width, symbol) pair |
| 6779 | /// tests the mapping the readers actually use. |
| 6780 | pub(crate) fn tx_type_from_symbol(cdf_len: usize, t: usize) -> Option<TxType> { |
| 6781 | match cdf_len { |
| 6782 | 17 => TxType::from_symbol_all16(t), |
| 6783 | 13 => TxType::from_symbol_set2_12(t), |
| 6784 | 8 => TxType::from_symbol_set1(t), |
| 6785 | _ => TxType::from_symbol(t), |
| 6786 | } |
| 6787 | } |
| 6788 | |
| 6789 | /// [`tx_type_from_symbol`] inverted: which symbol of a `cdf_len`-wide |
| 6790 | /// `tx_type` CDF names `tx_type`, or `None` when that set does not hold it |
| 6791 | /// (class `table-and-reader-move-together` -- the writer picks its symbol |
| 6792 | /// through the reader's own map, so the two cannot drift). |
| 6793 | pub(crate) fn tx_type_symbol(cdf_len: usize, tx_type: TxType) -> Option<usize> { |
| 6794 | (0..cdf_len.saturating_sub(1)).find(|&t| tx_type_from_symbol(cdf_len, t) == Some(tx_type)) |
| 6795 | } |
| 6796 | |
| 6797 | /// The remainder of a level the base and base-range syntax could not reach |
| 6798 | /// (spec 5.11.40): its bit length in unary, then that many of its own bits, |
| 6799 | /// most significant first — the exact inverse of [`crate::tile::write_golomb`]. |
| 6800 | pub(crate) fn read_golomb(dec: &mut SymbolDecoder) -> Result<u32> { |
| 6801 | // THIS CAP IS libaom's OWN, TO THE BIT -- do not "widen" it (lane-golomb2 |
| 6802 | // re-read the source: `av1/decoder/decodetxb.c:22-43` counts the same |
| 6803 | // prefix and calls the 21st zero bit `AOM_CODEC_CORRUPT_FRAME`, "Invalid |
| 6804 | // length in read_golomb"; spec 5.11.40 BREAKS at `length == 20` and 5.11.39 |
| 6805 | // masks the level with `0xFFFFF`, so no conformant stream carries a longer |
| 6806 | // tail). `refusal_inventory::read_golomb_reads_every_value_a_conformant_ |
| 6807 | // stream_can_carry` enumerates that whole value domain. |
| 6808 | // |
| 6809 | // So this refusal never names a reader gap: it is always the SYMPTOM of an |
| 6810 | // earlier desync (class `refusal-hides-a-defect`), and every historical |
| 6811 | // instance was closed by fixing that desync -- lane-dgolomb's |
| 6812 | // `force_integer_mv`, lane-dkey's palette, lane-t900 r12's `uv_mode_grid`. |
| 6813 | // Raising the cap only trades a clean refusal for silent corruption: |
| 6814 | // lane-scaledref r1 tried it (commit ee1f980, reverted in 314ee08) and |
| 6815 | // turned `a_real_aomenc_stream_with_a_superblock_level_horz_vert_partition_decodes_pixel_exact` |
| 6816 | // from a refusal into a frame-0 LUMA MISMATCH. |
| 6817 | let mut length = 1u32; |
| 6818 | while dec.literal(1) == 0 { |
| 6819 | length += 1; |
| 6820 | if length > 20 { |
| 6821 | return Err(unsupported("a Golomb tail longer than this decoder reads")); |
| 6822 | } |
| 6823 | } |
| 6824 | let mut x = 1u32; |
| 6825 | for _ in 1..length { |
| 6826 | x = (x << 1) | dec.literal(1); |
| 6827 | } |
| 6828 | Ok(x - 1) |
| 6829 | } |
| 6830 | |
| 6831 | /// `decode_symbol`'s end-of-block position (spec 5.11.39): the inverse of |
| 6832 | /// [`crate::tile`]'s `write_eob`. |
| 6833 | fn read_eob(dec: &mut SymbolDecoder, coding: &mut TxbTables, class: TxClass) -> usize { |
| 6834 | const GROUP_START: [usize; 12] = [0, 1, 2, 3, 5, 9, 17, 33, 65, 129, 257, 513]; |
| 6835 | const OFFSET_BITS: [u32; 12] = [0, 0, 0, 1, 2, 3, 4, 5, 6, 7, 8, 9]; |
| 6836 | |
| 6837 | let trace = crate::envflags::env_flag!("EC_AV1_TRACE"); |
| 6838 | // libaom's `eob_flag_cdf*` tables carry a second dimension the 2D scan |
| 6839 | // never touches: `TX_CLASS_HORIZ`/`TX_CLASS_VERT` (`V_DCT`/`H_DCT`) adapt |
| 6840 | // a wholly separate CDF from every other tx_type (`decodetxb.c`'s |
| 6841 | // `eob_multi_ctx`). Missing that split desynced the very first |
| 6842 | // `V_DCT`/`H_DCT` TU this decoder ever produced (lane-av1tx4 r5, caught |
| 6843 | // against a real aomdec trace). |
| 6844 | let eob_pt: &mut [u16] = match (class, coding.eob_pt_class1.as_deref_mut()) { |
| 6845 | (TxClass::TwoD, _) | (_, None) => coding.eob_pt, |
| 6846 | (_, Some(class1)) => class1, |
| 6847 | }; |
| 6848 | if crate::envflags::env_flag!("EC_AV1_EOBPT_CDF") { |
| 6849 | let (range, value) = dec.debug_state(); |
| 6850 | eprintln!("EC_AV1_EOBPT_CDF {eob_pt:?} range={range} value={value}"); |
| 6851 | } |
| 6852 | let group = dec.symbol(eob_pt) + 1; |
| 6853 | if trace { |
| 6854 | eprintln!("TRACE eob_pt value={group} rng={}", dec.debug_state().0); |
| 6855 | } |
| 6856 | let bits = OFFSET_BITS[group]; |
| 6857 | let mut offset = 0u32; |
| 6858 | if bits > 0 { |
| 6859 | let top = dec.symbol(&mut coding.eob_extra[group - 3]) as u32; |
| 6860 | if trace { |
| 6861 | eprintln!("TRACE eob_extra ctx={} value={top}", group - 3); |
| 6862 | } |
| 6863 | offset = top << (bits - 1); |
| 6864 | if bits > 1 { |
| 6865 | offset |= dec.literal(bits - 1); |
| 6866 | } |
| 6867 | } |
| 6868 | GROUP_START[group] + offset as usize |
| 6869 | } |
| 6870 | |
| 6871 | /// One transform block's levels, the inverse of [`crate::tile`]'s |
| 6872 | /// `write_coeffs`, returned as a `side * side` grid in raster order, along |
| 6873 | /// with the transform type that grid was coded under (`DCT_DCT` when the |
| 6874 | /// block's `TxbSet` carries no `tx_type` symbol at all, e.g. 32-point and |
| 6875 | /// 64-point transforms, which never code anything else). |
| 6876 | /// |
| 6877 | /// # Errors |
| 6878 | /// Returns an error if the `tx_type` symbol decodes to a value outside its |
| 6879 | /// CDF's own set (a corrupt or unsupported bitstream) — every value the |
| 6880 | /// intra `Tx_Type_Intra_Inv_Set2` and inter `Tx_Type_Inter_Inv_Set3` CDFs can |
| 6881 | /// produce dispatches to a real inverse transform. |
| 6882 | /// `Intra_Mode_To_Tx_Type` (spec 9.3): chroma's transform type is never its |
| 6883 | /// own coded symbol -- `TxbSet::Chroma*`'s `tx_type` slot is always `None` |
| 6884 | /// (a chroma block shares no CDF row with `Luma16`'s mode-indexed one) -- so |
| 6885 | /// libaom's own `intra_mode_to_tx_type` (`blockd.h`) derives it purely from |
| 6886 | /// the plane's own predicted mode instead. Every entry this table can |
| 6887 | /// produce is a member of both `TX_SET_INTRA_1` and `_2` (spec 9.3's two |
| 6888 | /// intra sets differ only in `IDTX`/`V_DCT`/`H_DCT`, none of which this |
| 6889 | /// table ever names), so unlike the CDF-coded luma path there is no |
| 6890 | /// ext-tx-set membership check to fall back from here. |
| 6891 | pub(crate) fn default_intra_tx_type(mode: u8) -> TxType { |
| 6892 | use crate::intra::{ |
| 6893 | D45_PRED, D67_PRED, D113_PRED, D135_PRED, D157_PRED, D203_PRED, DC_PRED, H_PRED, |
| 6894 | PAETH_PRED, SMOOTH_H_PRED, SMOOTH_PRED, SMOOTH_V_PRED, V_PRED, |
| 6895 | }; |
| 6896 | match mode { |
| 6897 | DC_PRED | D45_PRED => TxType::DctDct, |
| 6898 | V_PRED | D113_PRED | D67_PRED | SMOOTH_V_PRED => TxType::AdstDct, |
| 6899 | H_PRED | D157_PRED | D203_PRED | SMOOTH_H_PRED => TxType::DctAdst, |
| 6900 | D135_PRED | SMOOTH_PRED | PAETH_PRED => TxType::AdstAdst, |
| 6901 | other => panic!("intra mode {other} has no Intra_Mode_To_Tx_Type entry"), |
| 6902 | } |
| 6903 | } |
| 6904 | |
| 6905 | /// `is_smooth` (libaom `reconintra.c`): true for `SMOOTH_PRED..=SMOOTH_H_PRED` |
| 6906 | /// (9..=11) — deliberately excludes `PAETH_PRED` (12), which the spec's own |
| 6907 | /// `get_intra_edge_filter_type`/libaom `get_filt_type` never treat as smooth. |
| 6908 | fn is_smooth_mode(mode: usize) -> bool { |
| 6909 | (crate::intra::SMOOTH_PRED as usize..=crate::intra::SMOOTH_H_PRED as usize).contains(&mode) |
| 6910 | } |
| 6911 | |
| 6912 | /// lane-coefread: the per-coefficient trace sites, one `#[cold]` call each. |
| 6913 | /// `eprintln!` builds its `fmt::Arguments` on the stack AT THE CALL SITE, so |
| 6914 | /// nine of them inside the base/sign loops pinned nine sets of values in the |
| 6915 | /// hot loop's frame (the annotated profile was full of `mov ...(%rsp)` spill |
| 6916 | /// traffic). Out of line, the loop keeps its values in registers and the |
| 6917 | /// trace output is character-for-character what it was. |
| 6918 | #[cold] |
| 6919 | #[inline(never)] |
| 6920 | fn trace_base_eob(scan_idx: usize, pos: usize, row: usize, col: usize, ctx: usize, v: i32) { |
| 6921 | eprintln!("TRACE base_eob scan_idx={scan_idx} pos={pos} row={row} col={col} ctx={ctx} value={v}"); |
| 6922 | } |
| 6923 | |
| 6924 | #[cold] |
| 6925 | #[inline(never)] |
| 6926 | fn trace_base(scan_idx: usize, pos: usize, row: usize, col: usize, ctx: usize, v: i32) { |
| 6927 | eprintln!("TRACE base scan_idx={scan_idx} pos={pos} row={row} col={col} ctx={ctx} value={v}"); |
| 6928 | } |
| 6929 | |
| 6930 | #[cold] |
| 6931 | #[inline(never)] |
| 6932 | fn trace_br(scan_idx: usize, pos: usize, row: usize, col: usize, ctx: usize, k: i32) { |
| 6933 | eprintln!("TRACE br scan_idx={scan_idx} pos={pos} row={row} col={col} ctx={ctx} value={k}"); |
| 6934 | } |
| 6935 | |
| 6936 | #[cold] |
| 6937 | #[inline(never)] |
| 6938 | fn coeff_step_base(dec: &SymbolDecoder, scan_idx: usize, apos: usize, ctx: usize, v: i32) { |
| 6939 | let (rng, _) = dec.debug_state(); |
| 6940 | eprintln!("EC_COEFF_STEP tag=base c={scan_idx} pos={apos} ctx={ctx} level={v} rng={rng}"); |
| 6941 | } |
| 6942 | |
| 6943 | #[cold] |
| 6944 | #[inline(never)] |
| 6945 | fn coeff_step_br(dec: &SymbolDecoder, scan_idx: usize, apos: usize, ctx: usize, k: i32) { |
| 6946 | let (rng, _) = dec.debug_state(); |
| 6947 | eprintln!("EC_COEFF_STEP tag=br c={scan_idx} pos={apos} ctx={ctx} k={k} rng={rng}"); |
| 6948 | } |
| 6949 | |
| 6950 | #[cold] |
| 6951 | #[inline(never)] |
| 6952 | fn coeff_step_sign(dec: &SymbolDecoder, c: usize, negative: bool) { |
| 6953 | let (rng, _) = dec.debug_state(); |
| 6954 | eprintln!("EC_COEFF_STEP tag=sign c={c} sign={} rng={rng}", negative as i32); |
| 6955 | } |
| 6956 | |
| 6957 | #[cold] |
| 6958 | #[inline(never)] |
| 6959 | fn coeff_step_post_golomb(dec: &SymbolDecoder, c: usize, level: i32) { |
| 6960 | let (rng, _) = dec.debug_state(); |
| 6961 | eprintln!("EC_COEFF_STEP tag=post_golomb c={c} level={level} rng={rng}"); |
| 6962 | } |
| 6963 | |
| 6964 | #[cold] |
| 6965 | #[inline(never)] |
| 6966 | fn coeff_step_base_eob_level(dec: &SymbolDecoder, level: i32) { |
| 6967 | let (rng, _) = dec.debug_state(); |
| 6968 | eprintln!("EC_COEFF_STEP tag=base_eob level={level} rng={rng}"); |
| 6969 | } |
| 6970 | |
| 6971 | fn read_coeffs( |
| 6972 | dec: &mut SymbolDecoder, |
| 6973 | coding: &mut TxbTables, |
| 6974 | scan: &[u16], |
| 6975 | skip_ctx: usize, |
| 6976 | sign_ctx: usize, |
| 6977 | // The chroma default (spec `Intra_Mode_To_Tx_Type`, see |
| 6978 | // [`default_intra_tx_type`]) for the sizes where `coding.tx_type` codes |
| 6979 | // nothing at all; `DctDct` for every luma call and every chroma call at |
| 6980 | // a size the spec forces to `DCT_DCT` regardless of mode (32-point and |
| 6981 | // up, `EXT_TX_SET_DCTONLY`) -- the caller already folds that in. |
| 6982 | default_tx_type: TxType, |
| 6983 | // `Some((w, h))` with `w != h` when this read is a superblock-level |
| 6984 | // HORZ/VERT strip's truncated luma corner: the CDF *set* resolves square |
| 6985 | // (`side`/`coding` above), but `av1_nz_map_ctx_offset`'s position table |
| 6986 | // is indexed by the real, un-adjusted rectangular shape (lane-sbpart r8 |
| 6987 | // root cause, see [`base_ctx`]). `None` everywhere else -- every other |
| 6988 | // caller's block genuinely is `side` x `side`. |
| 6989 | rect_shape: Option<(usize, usize)>, |
| 6990 | ) -> Result<(Grid, TxType)> { |
| 6991 | let trace = crate::envflags::env_flag!("EC_AV1_TRACE"); |
| 6992 | let side = coding.side; |
| 6993 | if crate::envflags::env_flag!("EC_AV1_EOBPT_CDF") { |
| 6994 | let (range, value) = dec.debug_state(); |
| 6995 | eprintln!("EC_AV1_STATE_BEFORE_TXBSKIP range={range} value={value}"); |
| 6996 | } |
| 6997 | if crate::envflags::env_flag!("EC_ECDUMP_IN") { |
| 6998 | let (range, value) = dec.debug_state(); |
| 6999 | let bit = dec.debug_bitpos(); |
| 7000 | eprintln!( |
| 7001 | "EC_ECDUMP_IN side={side} ctx={skip_ctx} dcctx={sign_ctx} ECIN=({value},{range},{bit})" |
| 7002 | ); |
| 7003 | } |
| 7004 | let dbg_txbskip = if coeff_trace_on() { |
| 7005 | coding.txb_skip[skip_ctx] |
| 7006 | } else { |
| 7007 | [0; 3] |
| 7008 | }; |
| 7009 | let all_zero = dec.symbol(&mut coding.txb_skip[skip_ctx]) == 1; |
| 7010 | if coeff_trace_on() { |
| 7011 | let (rng, _) = dec.debug_state(); |
| 7012 | eprintln!( |
| 7013 | "EC_COEFF_STEP tag=all_zero side={} ctx={skip_ctx} dcctx={sign_ctx} cdf={dbg_txbskip:?} all_zero={} rng={rng}", |
| 7014 | coding.side, |
| 7015 | all_zero as i32 |
| 7016 | ); |
| 7017 | } |
| 7018 | if crate::envflags::env_flag!("EC_AV1_TELL") { |
| 7019 | eprintln!( |
| 7020 | "TELL label=post_txb_skip ctx={skip_ctx} all_zero={} tell={} range={}", |
| 7021 | all_zero as u8, dec.debug_bitpos(), dec.debug_state().0 |
| 7022 | ); |
| 7023 | } |
| 7024 | if trace { |
| 7025 | eprintln!("TRACE all_zero ctx={skip_ctx} value={}", all_zero as i32); |
| 7026 | } |
| 7027 | if crate::envflags::env_flag!("EC_AV1_EOBPT_CDF") { |
| 7028 | let (range, value) = dec.debug_state(); |
| 7029 | eprintln!("EC_AV1_STATE_AFTER_TXBSKIP range={range} value={value}"); |
| 7030 | } |
| 7031 | if all_zero { |
| 7032 | // lane-coefgrid: the all-zero unit (60% of the 4K segment's) names the |
| 7033 | // shared zero grid instead of allocating and zeroing its own. |
| 7034 | return Ok((Grid::Zero(side * side), TxType::DctDct)); |
| 7035 | } |
| 7036 | let mut grid = vec![0i32; side * side]; |
| 7037 | let mut tx_type = default_tx_type; |
| 7038 | if let Some(tx_type_cdf) = coding.tx_type.as_deref_mut() { |
| 7039 | // The CDF row's own width names its set (lane-cdffwd2: extended |
| 7040 | // past the original two-way intra split once wider `reduced_tx_set |
| 7041 | // == 0` inter sets joined the table): 17 slots (16 symbols) is |
| 7042 | // `EXT_TX_SET_ALL16`, 13 (12 symbols) `EXT_TX_SET_DTT9_IDTX_1DDCT`, |
| 7043 | // 8 (7 symbols) `TX_SET_INTRA_1`, everything else (6 slots, 5 |
| 7044 | // symbols) the reduced `TX_SET_INTRA_2`/`TX_SET_INTER_3` two sets |
| 7045 | // share their symbol order with (`Tx_Type_Inter_Inv_Set3`'s two |
| 7046 | // members are a prefix of `Tx_Type_Intra_Inv_Set2`'s five). |
| 7047 | let len = tx_type_cdf.len(); |
| 7048 | if crate::envflags::env_flag!("EC_AV1_EOBPT_CDF") && len == 3 { |
| 7049 | eprintln!("EC_AV1_TXTYPE32_CDF {tx_type_cdf:?}"); |
| 7050 | } |
| 7051 | let t = dec.symbol(tx_type_cdf); |
| 7052 | crate::census::tx_type(len, t); |
| 7053 | if coeff_trace_on() { |
| 7054 | let (rng, _) = dec.debug_state(); |
| 7055 | eprintln!("EC_COEFF_STEP tag=tx_type len={len} rng={rng}"); |
| 7056 | } |
| 7057 | if crate::envflags::env_flag!("EC_AV1_EOBPT_CDF") { |
| 7058 | let (range, value) = dec.debug_state(); |
| 7059 | eprintln!("EC_AV1_STATE_AFTER_TXTYPE range={range} value={value}"); |
| 7060 | } |
| 7061 | if trace { |
| 7062 | eprintln!("TRACE tx_type value={t} len={len}"); |
| 7063 | } |
| 7064 | tx_type = tx_type_from_symbol(len, t) |
| 7065 | .ok_or_else(|| unsupported(format!("a tx_type symbol outside its CDF's own set: {t}")))?; |
| 7066 | } |
| 7067 | |
| 7068 | let class = TxClass::of(tx_type); |
| 7069 | if class != TxClass::TwoD { |
| 7070 | hit!(TX_CLASS1_HITS); |
| 7071 | hit!(RECT_CLASS1_HITS); |
| 7072 | } |
| 7073 | let eob = read_eob(dec, coding, class); |
| 7074 | if trace { |
| 7075 | eprintln!("TRACE eob value={eob}"); |
| 7076 | } |
| 7077 | let ec_trace_coeff = coeff_trace_on(); |
| 7078 | if ec_trace_coeff { |
| 7079 | let (rng, _) = dec.debug_state(); |
| 7080 | let cls = match class { TxClass::TwoD => "2d", TxClass::Horiz => "horiz", TxClass::Vert => "vert" }; |
| 7081 | eprintln!("EC_COEFF_STEP tag=eob eob={eob} tx={tx_type:?} class={cls} rng={rng}"); |
| 7082 | } |
| 7083 | let class_scan; |
| 7084 | let scan: &[u16] = if class == TxClass::TwoD { |
| 7085 | scan |
| 7086 | } else { |
| 7087 | class_scan = class_scan_table(side, class); |
| 7088 | &class_scan |
| 7089 | }; |
| 7090 | // `side` is one of 4/8/16/32 (every `TxbTables` row), so the per- |
| 7091 | // coefficient `pos / side`, `pos % side` was a hardware divide where a |
| 7092 | // shift and a mask say the same thing, and the level buffer fits a fixed |
| 7093 | // 32x32 frame of stack instead of a heap allocation per transform block. |
| 7094 | debug_assert!(side.is_power_of_two() && side <= 32); |
| 7095 | let sh = side.trailing_zeros(); |
| 7096 | let col_mask = side - 1; |
| 7097 | // lane-coef2: padded like libaom's own level buffer (see [`neighbour`]): |
| 7098 | // `stride = side + 4` columns and four spare rows, so the widest read |
| 7099 | // `base_ctx` makes -- `(row + 4, col)` / `(row, col + 4)` with |
| 7100 | // `row, col <= side - 1` -- stays inside `(side + 4) * (side + 4)`, at |
| 7101 | // most `36 * 36`. Only `col < side`, `row < side` cells are ever written. |
| 7102 | let stride = side + 4; |
| 7103 | let mut levels_buf = [0u8; 36 * 36]; |
| 7104 | let levels = &mut levels_buf[..stride * stride]; |
| 7105 | // lane-coefread: the sign pass used to re-walk `scan[..eob]`, redo the |
| 7106 | // `pos >> sh` / `& mask` decomposition and load the level back out of the |
| 7107 | // padded frame just to skip the zeros. The base pass already knows both, |
| 7108 | // so it records `(scan_idx << 4) | level` for each NON-ZERO coefficient |
| 7109 | // (`scan_idx < 1024`, `level <= 15` -- both loops assert it), and the sign |
| 7110 | // pass walks that list in reverse (the base pass runs in reverse scan |
| 7111 | // order, so reversing it is forward scan order: the same coefficients, in |
| 7112 | // the same order, with the same `c` the trace prints). |
| 7113 | let mut nz_buf = [0u16; 1024]; |
| 7114 | let mut nz_n = 0usize; |
| 7115 | let (base_taps, br_taps) = flat_taps(stride, class); |
| 7116 | for scan_idx in (0..eob).rev() { |
| 7117 | let pos = scan[scan_idx] as usize; |
| 7118 | let (row, col) = (pos >> sh, pos & col_mask); |
| 7119 | let idx = row * stride + col; |
| 7120 | let level = if scan_idx == eob - 1 { |
| 7121 | let ctx = eob_coeff_ctx(scan_idx, side * side); |
| 7122 | let v = dec.symbol(&mut coding.base_eob[ctx]) as i32 + 1; |
| 7123 | if trace { |
| 7124 | trace_base_eob(scan_idx, pos, row, col, ctx, v); |
| 7125 | } |
| 7126 | v |
| 7127 | } else { |
| 7128 | let ctx = if class == TxClass::TwoD && rect_shape.is_none() { |
| 7129 | if row == 0 && col == 0 { |
| 7130 | 0 |
| 7131 | } else { |
| 7132 | base_mag(levels, idx, &base_taps) |
| 7133 | + cdf::NZ_MAP_CTX_OFFSET_32[row.min(4)][col.min(4)] as usize |
| 7134 | } |
| 7135 | } else { |
| 7136 | base_ctx(levels, stride, row, col, class, rect_shape) |
| 7137 | }; |
| 7138 | let v = dec.symbol(&mut coding.base[ctx]) as i32; |
| 7139 | if ec_trace_coeff { |
| 7140 | // `pos` in libaom's column-major `coeff_idx` convention, so |
| 7141 | // the ladder lines up with instrumented `aomdec`. |
| 7142 | coeff_step_base(dec, scan_idx, col * side + row, ctx, v); |
| 7143 | } |
| 7144 | if trace { |
| 7145 | trace_base(scan_idx, pos, row, col, ctx, v); |
| 7146 | } |
| 7147 | v |
| 7148 | }; |
| 7149 | let level = if level > NUM_BASE_LEVELS { |
| 7150 | let ctx = { |
| 7151 | let mag = br_mag(levels, idx, &br_taps); |
| 7152 | if row == 0 && col == 0 { |
| 7153 | mag |
| 7154 | } else { |
| 7155 | let near_origin = match class { |
| 7156 | TxClass::TwoD => row < 2 && col < 2, |
| 7157 | TxClass::Horiz => col == 0, |
| 7158 | TxClass::Vert => row == 0, |
| 7159 | }; |
| 7160 | if near_origin { mag + 7 } else { mag + 14 } |
| 7161 | } |
| 7162 | }; |
| 7163 | let mut level = level; |
| 7164 | let mut sent = 0; |
| 7165 | loop { |
| 7166 | let k = dec.symbol(&mut coding.br[ctx]) as i32; |
| 7167 | if ec_trace_coeff { |
| 7168 | // lane-leaf8tx r3: the `br` rung was missing from THIS |
| 7169 | // path's trace, so the cross-decoder range ladder was |
| 7170 | // blind to every base-range symbol and mis-reported the |
| 7171 | // first divergence (class [[gate-blind-to-feature]]). |
| 7172 | coeff_step_br(dec, scan_idx, col * side + row, ctx, k); |
| 7173 | } |
| 7174 | if trace { |
| 7175 | trace_br(scan_idx, pos, row, col, ctx, k); |
| 7176 | } |
| 7177 | level += k; |
| 7178 | sent += BR_STEP; |
| 7179 | if k < BR_STEP || sent >= COEFF_BASE_RANGE { |
| 7180 | break; |
| 7181 | } |
| 7182 | } |
| 7183 | level |
| 7184 | } else { |
| 7185 | level |
| 7186 | }; |
| 7187 | if ec_trace_coeff && scan_idx == eob - 1 { |
| 7188 | coeff_step_base_eob_level(dec, level); |
| 7189 | } |
| 7190 | // Every coded level here is a magnitude in `0..=15` (base <= 3 plus |
| 7191 | // at most `COEFF_BASE_RANGE` from the `br` loop; the Golomb tail is |
| 7192 | // added in the sign pass below, straight into `grid`), so the context |
| 7193 | // frame is bytes -- the five-tap sum in [`base_ctx`]/[`br_ctx`] is a |
| 7194 | // byte gather (lane-coefbuf). |
| 7195 | debug_assert!(level >= 0 && level <= 15, "level {level} past the u8 frame"); |
| 7196 | levels[idx] = level as u8; |
| 7197 | if level != 0 { |
| 7198 | nz_buf[nz_n] = ((scan_idx as u16) << 4) | level as u16; |
| 7199 | nz_n += 1; |
| 7200 | } |
| 7201 | } |
| 7202 | if ec_trace_coeff { |
| 7203 | let (rng, _) = dec.debug_state(); |
| 7204 | eprintln!("EC_COEFF_STEP tag=after_bases rng={rng}"); |
| 7205 | } |
| 7206 | |
| 7207 | for &packed in nz_buf[..nz_n].iter().rev() { |
| 7208 | let c = (packed >> 4) as usize; |
| 7209 | let pos = scan[c] as usize; |
| 7210 | let level = (packed & 15) as i32; |
| 7211 | let negative = if pos == 0 { |
| 7212 | let v = dec.symbol(&mut coding.dc_sign[sign_ctx]) == 1; |
| 7213 | if trace { |
| 7214 | eprintln!("TRACE dc_sign ctx={sign_ctx} value={}", v as i32); |
| 7215 | } |
| 7216 | v |
| 7217 | } else { |
| 7218 | dec.literal(1) == 1 |
| 7219 | }; |
| 7220 | if ec_trace_coeff { |
| 7221 | coeff_step_sign(dec, c, negative); |
| 7222 | } |
| 7223 | let level = if level > MAX_BR_LEVEL { |
| 7224 | let g = read_golomb(dec)?; |
| 7225 | if trace { |
| 7226 | eprintln!("TRACE golomb pos={pos} value={g}"); |
| 7227 | } |
| 7228 | level + g as i32 |
| 7229 | } else { |
| 7230 | level |
| 7231 | }; |
| 7232 | if ec_trace_coeff { |
| 7233 | coeff_step_post_golomb(dec, c, level); |
| 7234 | } |
| 7235 | grid[pos] = if negative { -level } else { level }; |
| 7236 | } |
| 7237 | Ok((Grid::Own(grid), tx_type)) |
| 7238 | } |
| 7239 | |
| 7240 | /// [`read_coeffs`] widened to `(w, h)` (lane-rectwire; lane-rectx added the |
| 7241 | /// `tx_type` read [`TxbSet::LumaRect16x8`] needs), restricted to |
| 7242 | /// `TxClass::TwoD` -- the only class any rect size pair this decoder codes |
| 7243 | /// can ever produce, since every `tx_type` this decoder's rect sets can read |
| 7244 | /// (`EXT_TX_SET_DTT4_IDTX`'s five members) is itself 2D: refuses by name |
| 7245 | /// rather than guess-decode if a non-2D class ever shows up here, since |
| 7246 | /// neither `base_ctx_rect`/`br_ctx_rect` nor [`class_scan_table`] have a rect |
| 7247 | /// form for those. |
| 7248 | |
| 7249 | fn read_coeffs_rect( |
| 7250 | dec: &mut SymbolDecoder, |
| 7251 | coding: &mut TxbTables, |
| 7252 | scan: &[u16], |
| 7253 | w: usize, |
| 7254 | h: usize, |
| 7255 | skip_ctx: usize, |
| 7256 | sign_ctx: usize, |
| 7257 | default_tx_type: TxType, |
| 7258 | ) -> Result<(Grid, TxType)> { |
| 7259 | let rect_trace = coeff_trace_on(); |
| 7260 | let entry_rng = dec.debug_state().0; |
| 7261 | let all_zero = dec.symbol(&mut coding.txb_skip[skip_ctx]) == 1; |
| 7262 | if rect_trace { |
| 7263 | eprintln!( |
| 7264 | "EC_COEFF_STEP tag=all_zero plane=0 ctx={skip_ctx} entry={entry_rng} all_zero={} rng={}", |
| 7265 | all_zero as i32, |
| 7266 | dec.debug_state().0 |
| 7267 | ); |
| 7268 | } |
| 7269 | if all_zero { |
| 7270 | return Ok((Grid::Zero(w * h), TxType::DctDct)); |
| 7271 | } |
| 7272 | let mut grid = vec![0i32; w * h]; |
| 7273 | hit!(RECT_COEFF_TU_HITS); |
| 7274 | // lane-tx4x8: a 4x8/8x4 luma TU DOES carry a `tx_type` symbol (its set is |
| 7275 | // `EXT_TX_SET_DTT4_IDTX(_1DDCT)`, `tx_size_sqr_up == TX_8X8`), including |
| 7276 | // the `V_DCT`/`H_DCT` members whose 1D tx class needs its own scan and |
| 7277 | // context offsets -- both of which now have rect forms |
| 7278 | // ([`class_scan_table_wh`], [`base_ctx_rect`]/[`br_ctx_rect`]'s `class`). |
| 7279 | // The rect sets that carry no symbol at all still pass `None` here. |
| 7280 | let mut tx_type = default_tx_type; |
| 7281 | if let Some(tx_type_cdf) = coding.tx_type.as_deref_mut() { |
| 7282 | let len = tx_type_cdf.len(); |
| 7283 | let t = dec.symbol(tx_type_cdf); |
| 7284 | crate::census::tx_type(len, t); |
| 7285 | tx_type = tx_type_from_symbol(len, t) |
| 7286 | .ok_or_else(|| unsupported(format!("a tx_type symbol outside its CDF's own set: {t}")))?; |
| 7287 | if rect_trace { |
| 7288 | eprintln!("EC_COEFF_STEP tag=tx_type plane=0 rng={}", dec.debug_state().0); |
| 7289 | } |
| 7290 | } |
| 7291 | let class = TxClass::of(tx_type); |
| 7292 | if class != TxClass::TwoD { |
| 7293 | hit!(TX_CLASS1_HITS); |
| 7294 | } |
| 7295 | let eob = read_eob(dec, coding, class); |
| 7296 | if rect_trace { |
| 7297 | eprintln!("EC_COEFF_STEP tag=eob eob={eob} rng={}", dec.debug_state().0); |
| 7298 | } |
| 7299 | let class_scan; |
| 7300 | let scan: &[u16] = if class == TxClass::TwoD { |
| 7301 | scan |
| 7302 | } else { |
| 7303 | class_scan = class_scan_table_wh(w, h, class); |
| 7304 | &class_scan |
| 7305 | }; |
| 7306 | // lane-coef2: a heap `vec![0i32; w * h]` per rect transform unit became a |
| 7307 | // stack frame sized to the unit -- same reasoning as [`read_coeffs`]: |
| 7308 | // `neighbour_rect` returns 0 outside `(w, h)` and every write is |
| 7309 | // `levels[pos]` with `pos < w * h`. `w` is a power of two at every AV1 |
| 7310 | // rect size, so the per-coefficient `pos / w`, `pos % w` is a shift and a |
| 7311 | // mask rather than a hardware divide. |
| 7312 | let stride = w + 4; |
| 7313 | let n = stride * (h + 4); |
| 7314 | if !(w.is_power_of_two() && n <= 36 * 36) { |
| 7315 | panic!("rect coeff scratch w={w} h={h} n={n}"); |
| 7316 | } |
| 7317 | let (mut b96, mut b256, mut b768); |
| 7318 | // Byte levels, same `0..=15` bound [`read_coeffs`] states. |
| 7319 | let levels: &mut [u8] = if n <= 96 { |
| 7320 | b96 = [0u8; 96]; |
| 7321 | &mut b96[..n] |
| 7322 | } else if n <= 256 { |
| 7323 | b256 = [0u8; 256]; |
| 7324 | &mut b256[..n] |
| 7325 | } else { |
| 7326 | b768 = [0u8; 768]; |
| 7327 | &mut b768[..n] |
| 7328 | }; |
| 7329 | let wsh = w.trailing_zeros(); |
| 7330 | let wmask = w - 1; |
| 7331 | // lane-coefread: the non-zero list [`read_coeffs`] documents. |
| 7332 | let mut nz_buf = [0u16; 1024]; |
| 7333 | let mut nz_n = 0usize; |
| 7334 | let (base_taps, br_taps) = flat_taps(stride, class); |
| 7335 | let (wlt, wgt) = (w < h, w > h); |
| 7336 | for scan_idx in (0..eob).rev() { |
| 7337 | let pos = scan[scan_idx] as usize; |
| 7338 | let (row, col) = (pos >> wsh, pos & wmask); |
| 7339 | let idx = row * stride + col; |
| 7340 | // libaom's column-major `coeff_idx`, for ladder comparison. |
| 7341 | let _apos = col * h + row; |
| 7342 | let level = if scan_idx == eob - 1 { |
| 7343 | let ctx = eob_coeff_ctx(scan_idx, w * h); |
| 7344 | let v = dec.symbol(&mut coding.base_eob[ctx]) as i32 + 1; |
| 7345 | if rect_trace { |
| 7346 | eprintln!( |
| 7347 | "EC_COEFF_STEP tag=base_eob c={scan_idx} pos={pos} ctx={ctx} level={v} rng={}", |
| 7348 | dec.debug_state().0 |
| 7349 | ); |
| 7350 | } |
| 7351 | v |
| 7352 | } else { |
| 7353 | let ctx = if class == TxClass::TwoD { |
| 7354 | if row == 0 && col == 0 { |
| 7355 | 0 |
| 7356 | } else { |
| 7357 | let m = base_mag(levels, idx, &base_taps); |
| 7358 | if wlt && row < 2 { |
| 7359 | m + 11 |
| 7360 | } else if wgt && col < 2 { |
| 7361 | m + 16 |
| 7362 | } else { |
| 7363 | m + cdf::NZ_MAP_CTX_OFFSET_32[row.min(4)][col.min(4)] as usize |
| 7364 | } |
| 7365 | } |
| 7366 | } else { |
| 7367 | base_ctx_rect(levels, stride, w, h, row, col, class) |
| 7368 | }; |
| 7369 | let v = dec.symbol(&mut coding.base[ctx]) as i32; |
| 7370 | if rect_trace { |
| 7371 | eprintln!( |
| 7372 | "EC_COEFF_STEP tag=base c={scan_idx} pos={pos} ctx={ctx} level={v} rng={}", |
| 7373 | dec.debug_state().0 |
| 7374 | ); |
| 7375 | } |
| 7376 | v |
| 7377 | }; |
| 7378 | let level = if level > NUM_BASE_LEVELS { |
| 7379 | let ctx = { |
| 7380 | let mag = br_mag(levels, idx, &br_taps); |
| 7381 | if row == 0 && col == 0 { |
| 7382 | mag |
| 7383 | } else { |
| 7384 | let near_origin = match class { |
| 7385 | TxClass::TwoD => row < 2 && col < 2, |
| 7386 | TxClass::Horiz => col == 0, |
| 7387 | TxClass::Vert => row == 0, |
| 7388 | }; |
| 7389 | if near_origin { mag + 7 } else { mag + 14 } |
| 7390 | } |
| 7391 | }; |
| 7392 | let mut level = level; |
| 7393 | let mut sent = 0; |
| 7394 | loop { |
| 7395 | let k = dec.symbol(&mut coding.br[ctx]) as i32; |
| 7396 | if rect_trace { |
| 7397 | eprintln!( |
| 7398 | "EC_COEFF_STEP tag=br c={scan_idx} pos={pos} ctx={ctx} k={k} rng={}", |
| 7399 | dec.debug_state().0 |
| 7400 | ); |
| 7401 | } |
| 7402 | level += k; |
| 7403 | sent += BR_STEP; |
| 7404 | if k < BR_STEP || sent >= COEFF_BASE_RANGE { |
| 7405 | break; |
| 7406 | } |
| 7407 | } |
| 7408 | level |
| 7409 | } else { |
| 7410 | level |
| 7411 | }; |
| 7412 | debug_assert!(level >= 0 && level <= 15, "level {level} past the u8 frame"); |
| 7413 | levels[idx] = level as u8; |
| 7414 | if level != 0 { |
| 7415 | nz_buf[nz_n] = ((scan_idx as u16) << 4) | level as u16; |
| 7416 | nz_n += 1; |
| 7417 | } |
| 7418 | if rect_trace { |
| 7419 | let (rng, _) = dec.debug_state(); |
| 7420 | eprintln!("EC_COEFF_STEP tag=base c={scan_idx} pos={pos} level={level} rng={rng}"); |
| 7421 | } |
| 7422 | } |
| 7423 | if rect_trace { |
| 7424 | let (rng, _) = dec.debug_state(); |
| 7425 | eprintln!("EC_COEFF_STEP tag=after_bases rng={rng}"); |
| 7426 | } |
| 7427 | for &packed in nz_buf[..nz_n].iter().rev() { |
| 7428 | let pos = scan[(packed >> 4) as usize] as usize; |
| 7429 | let level = (packed & 15) as i32; |
| 7430 | let negative = if pos == 0 { |
| 7431 | dec.symbol(&mut coding.dc_sign[sign_ctx]) == 1 |
| 7432 | } else { |
| 7433 | dec.literal(1) == 1 |
| 7434 | }; |
| 7435 | if rect_trace { |
| 7436 | let (rng, _) = dec.debug_state(); |
| 7437 | eprintln!("EC_COEFF_STEP tag=sign_rect pos={pos} sign={} dcctx={sign_ctx} rng={rng}", u8::from(negative)); |
| 7438 | } |
| 7439 | let level = if level > MAX_BR_LEVEL { |
| 7440 | let g = read_golomb(dec)?; |
| 7441 | level + g as i32 |
| 7442 | } else { |
| 7443 | level |
| 7444 | }; |
| 7445 | grid[pos] = if negative { -level } else { level }; |
| 7446 | } |
| 7447 | // lane-d792: the rect unit's LEVEL grid in row-major (w) order, the |
| 7448 | // twin of the oracle's `EC_DQCOEFF` rung -- a residual mismatch whose |
| 7449 | // grids differ is a reader defect, one whose grids agree is a transform |
| 7450 | // defect. |
| 7451 | if crate::envflags::env_flag!("EC_DQCOEFF") { |
| 7452 | let nz: Vec<String> = grid |
| 7453 | .iter() |
| 7454 | .enumerate() |
| 7455 | .filter(|&(_, &v)| v != 0) |
| 7456 | .map(|(i, &v)| format!("{}/{}:{v}", i / w, i % w)) |
| 7457 | .collect(); |
| 7458 | eprintln!("OUR_LEVELS w={w} h={h} eob={eob} tx={tx_type:?} nz={}", nz.join(",")); |
| 7459 | } |
| 7460 | Ok((Grid::Own(grid), tx_type)) |
| 7461 | } |
| 7462 | |
| 7463 | /// [`read_coeffs_rect`] for a CHROMA plane, carrying the monochrome gate the |
| 7464 | /// [`read_plane`] family already has. |
| 7465 | /// |
| 7466 | /// A monochrome frame has `NumPlanes == 1`, so libaom's coefficient loop |
| 7467 | /// (`for (plane = 0; plane < av1_num_planes(cm); ++plane)`) never reaches a |
| 7468 | /// chroma plane: no u/v coefficient symbol is coded, and consuming one moves |
| 7469 | /// the arithmetic decoder off the stream for the rest of the tile. [`read_plane`] |
| 7470 | /// enforces that for every strip that reads its chroma through it, but the rect |
| 7471 | /// strip paths ([`decode_block_rect`], [`decode_leaf_rect`], |
| 7472 | /// [`decode_block_rect4`], [`decode_rect4_16_strip`], [`decode_block_rect64`]) |
| 7473 | /// call `read_coeffs_rect` on planes 1/2 DIRECTLY, so each of those sites takes |
| 7474 | /// this gate instead (class `reader-gate-not-on-every-path`). One un-gated |
| 7475 | /// chroma read desyncs at the FIRST rect leaf of the frame and every later |
| 7476 | /// symbol -- and therefore every later pixel -- is decoded from the wrong |
| 7477 | /// place, without ever tripping a refusal (the desynced values stay inside |
| 7478 | /// every coded alphabet). |
| 7479 | fn read_chroma_coeffs_rect( |
| 7480 | dec: &mut SymbolDecoder, |
| 7481 | coding: &mut TxbTables, |
| 7482 | scan: &[u16], |
| 7483 | w: usize, |
| 7484 | h: usize, |
| 7485 | skip_ctx: usize, |
| 7486 | sign_ctx: usize, |
| 7487 | default_tx_type: TxType, |
| 7488 | fctx: &crate::decode::FrameCtx, |
| 7489 | ) -> Result<(Grid, TxType)> { |
| 7490 | if mono(fctx) { |
| 7491 | return Ok((Grid::Zero(w * h), TxType::DctDct)); |
| 7492 | } |
| 7493 | read_coeffs_rect(dec, coding, scan, w, h, skip_ctx, sign_ctx, default_tx_type) |
| 7494 | } |
| 7495 | |
| 7496 | /// What one coded block leaves behind for the blocks that read it as a |
| 7497 | /// neighbour: whether it coded anything at all, and the sign of its DC — |
| 7498 | /// [`crate::tile`]'s own private `Neighbour`. |
| 7499 | #[derive(Clone, Copy, Default)] |
| 7500 | struct Neighbour { |
| 7501 | /// The transform unit's cumulative coefficient level (spec `cul_level`, |
| 7502 | /// `decodetxb.c`'s `read_coeffs_txb`): the sum of every coded level's |
| 7503 | /// magnitude, clamped to `COEFF_CONTEXT_MASK` (7). A plain "coded or not" |
| 7504 | /// boolean (`level != 0`) is enough for every context this decoder read |
| 7505 | /// until now, but the *luma* `txb_skip_ctx` of a transform unit smaller |
| 7506 | /// than its own block (`TxMode::Select`, spec `get_txb_ctx_general`) |
| 7507 | /// needs the actual magnitude tier of its above/left neighbours, not |
| 7508 | /// just whether they coded anything. |
| 7509 | level: u8, |
| 7510 | dc: Option<bool>, |
| 7511 | } |
| 7512 | |
| 7513 | /// `dst[start .. start + n] = v`, with the same per-cell bounds behaviour the |
| 7514 | /// per-4x4-cell `get_mut` loops it replaces had: a cell past the end of the |
| 7515 | /// grid is dropped, never a panic. One `memset` per grid row instead of a |
| 7516 | /// bounds check per cell (lane-mi) -- the index span a row of a mi rect covers |
| 7517 | /// is contiguous, so this writes exactly the cells the cell loop did, |
| 7518 | /// including the ones a `mi_c + cc` past `skip_grid_cols_mi` folded into the |
| 7519 | /// next row. |
| 7520 | #[inline] |
| 7521 | fn fill_span<T: Copy>(dst: &mut [T], start: usize, n: usize, v: T) { |
| 7522 | let end = (start + n).min(dst.len()); |
| 7523 | if start < end { |
| 7524 | dst[start..end].fill(v); |
| 7525 | } |
| 7526 | } |
| 7527 | |
| 7528 | fn neighbour_state(grid: &[i32]) -> Neighbour { |
| 7529 | // lane-coefgrid: the shared zero grid sums to zero by construction; this |
| 7530 | // sum has no early exit, so the 60% of units that code nothing used to |
| 7531 | // walk their whole grid here. |
| 7532 | if is_zero_grid(grid) { |
| 7533 | return Neighbour::default(); |
| 7534 | } |
| 7535 | let cul: u32 = grid.iter().map(|&l| l.unsigned_abs()).sum(); |
| 7536 | Neighbour { |
| 7537 | level: cul.min(7) as u8, |
| 7538 | dc: (grid[0] != 0).then_some(grid[0] < 0), |
| 7539 | } |
| 7540 | } |
| 7541 | |
| 7542 | /// spec `get_txb_ctx_general`'s `skip_contexts[top][left]` table (plane 0, |
| 7543 | /// transform unit smaller than the block it sits in): `top`/`left` are the |
| 7544 | /// above/left neighbours' magnitude tiers, each already clamped to 4. |
| 7545 | pub(crate) const SKIP_CONTEXTS: [[usize; 5]; 5] = [ |
| 7546 | [1, 2, 2, 2, 3], |
| 7547 | [2, 4, 4, 4, 5], |
| 7548 | [2, 4, 4, 4, 5], |
| 7549 | [2, 4, 4, 4, 5], |
| 7550 | [3, 5, 5, 5, 6], |
| 7551 | ]; |
| 7552 | |
| 7553 | /// The `skip` flag, its written-instrument bit, and the covering coded |
| 7554 | /// block's origin and dims, packed into ONE 8-byte cell (lane-publish): the |
| 7555 | /// four maps are written together by `Neighbours::fill_skip_grid_rect` and by |
| 7556 | /// nothing else, so a block's row is one fill instead of four, and the |
| 7557 | /// deblocker's `pu_edge` reads origin and dims off one cache line. |
| 7558 | #[derive(Clone, Copy)] |
| 7559 | struct BlkCell { |
| 7560 | skip: bool, |
| 7561 | written: bool, |
| 7562 | org: [u16; 2], |
| 7563 | dim: [u8; 2], |
| 7564 | } |
| 7565 | |
| 7566 | /// The neighbour state a block's contexts are read from — a duplicate of |
| 7567 | /// [`crate::tile`]'s own private `Neighbours` (that module is another lane's |
| 7568 | /// territory this round), kept at the same two granularities the real writer |
| 7569 | /// tracks: `above_mode`/`left_mode`/`above_side`/`left_side` on the 16x16 |
| 7570 | /// (`SUB`) grid the partition/mode symbols read, and `above`/`left`'s |
| 7571 | /// per-plane coded/DC-sign state on the finer 4x4 (`MI`) grid the coefficient |
| 7572 | /// contexts read (spec `av1_set_entropy_contexts`) — which is what lets a |
| 7573 | /// chroma transform straddling the true, odd-sized frame edge see its |
| 7574 | /// neighbour's state clamped at its own subsampled edge rather than luma's. |
| 7575 | #[derive(Clone)] |
| 7576 | struct Neighbours { |
| 7577 | above: Vec<[Neighbour; 3]>, |
| 7578 | left: Vec<[Neighbour; 3]>, |
| 7579 | above_mode: Vec<usize>, |
| 7580 | left_mode: Vec<usize>, |
| 7581 | /// Chroma's own `uv_mode` companion to `above_mode`/`left_mode` (lane-chroma |
| 7582 | /// r3): a smooth/paeth `uv_mode`'s edge-filter strength (spec |
| 7583 | /// `get_intra_edge_filter_type`) reads the CHROMA neighbour's mode, not the |
| 7584 | /// luma one -- the same block can (and often does) code a different |
| 7585 | /// `uv_mode` than its own `mode`. Coarse [`SUB`]-grid like `above_mode`; |
| 7586 | /// [`Self::decode_leaf8`]'s per-leaf `prev_leaf` override (a real |
| 7587 | /// within-block neighbour finer than this grid) is luma-only -- a known, |
| 7588 | /// unexercised-by-gate corner-cut (every real-encoder gate in `stream.rs` |
| 7589 | /// uses `--min-partition-size=16`, so no leaf8 8x8 split ever reaches this). |
| 7590 | above_uv_mode: Vec<usize>, |
| 7591 | left_uv_mode: Vec<usize>, |
| 7592 | above_side: Vec<usize>, |
| 7593 | left_side: Vec<usize>, |
| 7594 | /// The same as `above_side`/`left_side`, but kept at the finer mi (4x4) |
| 7595 | /// granularity `above`/`left` are, rather than [`SUB`] -- [`crate::tile`]'s |
| 7596 | /// `above_side_mi`/`left_side_mi`: two 8x8 leaves of one straddling 16x16 |
| 7597 | /// block (lane-av1-rect) share a single [`SUB`]-grid cell, so a coarse |
| 7598 | /// array cannot tell the second leaf's partition symbol that the first |
| 7599 | /// one was coded at 8x8. |
| 7600 | /// lane-publish2: `u8` (a block side is at most 128 px, and so is |
| 7601 | /// [`NO_NEIGHBOUR_SIDE`]) -- every block stamps these per mi. |
| 7602 | above_side_mi: Vec<u8>, |
| 7603 | left_side_mi: Vec<u8>, |
| 7604 | /// The luma `mode` of the last `BLOCK_4X4` leaf decoded at each mi column |
| 7605 | /// / row, with the mi position it was written at |
| 7606 | /// (`(usize::MAX, _)` = never written). `above_mode`/`left_mode` are on |
| 7607 | /// the coarse 16x16 grid, which cannot distinguish the two 4x4 leaves an |
| 7608 | /// 8x8 `PARTITION_SPLIT` puts in one mi column; libaom's intra-mode |
| 7609 | /// context reads `mi(row, col-1)`/`mi(row-1, col)` exactly, so a sub-8x8 |
| 7610 | /// leaf whose neighbour is another split group needs that group's leaf 3 |
| 7611 | /// (bottom-right), not the leaf the coarse slot happens to hold. Written |
| 7612 | /// and read only by [`decode_leaf_split4`]; the position guard makes it |
| 7613 | /// fall back to the coarse slot whenever the real neighbour was a block |
| 7614 | /// of 8x8 or larger (whose one mode is correct for every row it spans). |
| 7615 | /// lane-publish2: `(mi position, mode)` narrowed from `(usize, usize)` to |
| 7616 | /// `(u16, u8)` -- every block stamps these mi-granular maps, and 16 bytes |
| 7617 | /// per mi per side of publish traffic was pure width. `u16::MAX` is the |
| 7618 | /// "never written" sentinel (an mi index cannot reach it: AV1's largest |
| 7619 | /// frame is 65536 px = 16384 mi). |
| 7620 | sub8_mode_col: Vec<(u16, u8)>, |
| 7621 | sub8_mode_row: Vec<(u16, u8)>, |
| 7622 | /// The `uv_mode` twin of `sub8_mode_col`/`sub8_mode_row` (lane-sub8 r5): |
| 7623 | /// chroma's edge-filter type reads the CHROMA neighbour's `uv_mode` |
| 7624 | /// (`get_intra_edge_filter_type`, reconintra.c:974), and the coarse |
| 7625 | /// [`SUB`] slots cannot name it once a 16x16 is split into 8x8 leaves -- |
| 7626 | /// the leaf above writes the same row slot the block to the left owns. |
| 7627 | uv_mode_col: Vec<(u16, u8)>, |
| 7628 | uv_mode_row: Vec<(u16, u8)>, |
| 7629 | /// Every mi cell's own `uv_mode`, the mi grid libaom's |
| 7630 | /// `chroma_above_mbmi`/`chroma_left_mbmi` read directly (`set_mi_row_col`, |
| 7631 | /// blockd.h). lane-t900 r12: the one-slot-per-column |
| 7632 | /// `uv_mode_col`/`uv_mode_row` maps above hold only the LAST block written |
| 7633 | /// in that column, so a sub-8x8 pair's own first (non-chroma-ref) half |
| 7634 | /// overwrote the row the pair's chroma-ref half then wanted to read -- |
| 7635 | /// the mi-exact match failed and the coarse [`SUB`] band answered instead. |
| 7636 | /// `u8::MAX` = no block has written this cell yet (fall back to the maps). |
| 7637 | uv_mode_grid: Vec<u8>, |
| 7638 | /// [`Self::uv_mode_grid`]'s stride, in mi units (superblock-padded, like |
| 7639 | /// every other band here). |
| 7640 | uv_grid_cols: usize, |
| 7641 | /// Whether the block above/left of this column/row was coded `skip` -- |
| 7642 | /// an inter frame's own `skip` context (spec `SkipContext`), which the |
| 7643 | /// key-frame writer never tracks (its skip context is always zero); a |
| 7644 | /// duplicate of [`crate::tile`]'s private `Neighbours::above_skip`/ |
| 7645 | /// `left_skip`. |
| 7646 | above_skip: Vec<bool>, |
| 7647 | left_skip: Vec<bool>, |
| 7648 | /// libaom's `above_txfm_context`/`left_txfm_context` (`TXFM_CONTEXT`), at |
| 7649 | /// 4x4 mi granularity: the transform *width* (above) / *height* (left) in |
| 7650 | /// pixels last written over each unit by `set_txfm_ctxs`/ |
| 7651 | /// `txfm_partition_update`, which is what an inter block's `txfm_split` |
| 7652 | /// symbol reads its context from (lane-txselect, spec 5.11.17). |
| 7653 | /// [`TXFM_CTX_INIT`] until a block writes one; a fresh full-height `left` |
| 7654 | /// array is exactly libaom's per-superblock-row reset, since each mi row |
| 7655 | /// is only ever visited inside one superblock row. |
| 7656 | above_txfm: Vec<u8>, |
| 7657 | left_txfm: Vec<u8>, |
| 7658 | /// Whether the block above/left of this column/row coded `skip_mode` |
| 7659 | /// (spec 5.11.23) -- `av1_get_skip_mode_context`'s own neighbour lookup, |
| 7660 | /// lane-av1comp round 14. |
| 7661 | above_skip_mode: Vec<bool>, |
| 7662 | left_skip_mode: Vec<bool>, |
| 7663 | /// Whether the block above/left was coded inter -- the `is_inter` |
| 7664 | /// context (spec `av1_get_intra_inter_context`), a duplicate of |
| 7665 | /// [`crate::tile`]'s private `Neighbours::above_inter`/`left_inter`. |
| 7666 | above_inter: Vec<bool>, |
| 7667 | left_inter: Vec<bool>, |
| 7668 | /// The actual reference frame (`1..=7`, spec's `MV_REFERENCE_FRAME` |
| 7669 | /// alphabet) the block above/left of this column/row coded, or `-1` for |
| 7670 | /// an intra neighbour/frame border -- the `single_ref_p1`..`p6` context |
| 7671 | /// functions (`mvstack::ref_ctx` family) need the real reference, not |
| 7672 | /// just "was it inter" (lane-av1refs: every inter block used to be |
| 7673 | /// `LAST_FRAME` by construction, so `above_inter`/`left_inter` alone was |
| 7674 | /// enough). |
| 7675 | above_ref: Vec<i8>, |
| 7676 | left_ref: Vec<i8>, |
| 7677 | /// The block above/left's *second* reference frame when it was coded |
| 7678 | /// compound (spec `RefFrames[1]`), or `None` for single-ref/intra -- |
| 7679 | /// lane-av1comp: `mvstack::reference_mode_ctx`/`comp_reference_type_ctx` |
| 7680 | /// need a full [`crate::mvstack::NeighbourRef`], not just `above_ref`'s |
| 7681 | /// `i8`, once a compound `read_ref_frames` lands. |
| 7682 | /// lane-publish2 audit: the "always `None` today" note this doc used to |
| 7683 | /// carry is long stale -- compound blocks do occur, and |
| 7684 | /// [`Self::record_compound_ctx_rect_mi`] stamps the real second reference |
| 7685 | /// over their span right after [`Self::record_inter_rect_mi`]'s reset. |
| 7686 | /// The array IS read: `single_ref_p*`/`comp_ref` contexts and every |
| 7687 | /// [`crate::mvstack::NeighbourRef`] construction (class |
| 7688 | /// [[branch-dropped-as-unreachable]] -- the note, not the code, was wrong). |
| 7689 | above_ref1: Vec<Option<i8>>, |
| 7690 | left_ref1: Vec<Option<i8>>, |
| 7691 | /// The precomputed `get_comp_group_idx_context`/`get_comp_index_context` |
| 7692 | /// neighbour contribution (spec 5.11.25, libaom `pred_common.h`): |
| 7693 | /// a compound neighbour's own `comp_group_idx`/`compound_idx` bit, or |
| 7694 | /// the "ref_frame[0] == ALTREF_FRAME" special case (`3`/`1`) for a |
| 7695 | /// single-ref one, or `0` for intra/unavailable -- lane-av1comp. |
| 7696 | above_comp_group_idx: Vec<u8>, |
| 7697 | left_comp_group_idx: Vec<u8>, |
| 7698 | above_compound_idx: Vec<u8>, |
| 7699 | left_compound_idx: Vec<u8>, |
| 7700 | /// `interp_filter[0]`/`[1]` (horizontal/vertical kernel index, 0..=2, or |
| 7701 | /// `3` = "no info": an intra neighbour or the frame's own border) of the |
| 7702 | /// block above/left of this column/row -- spec |
| 7703 | /// `av1_get_pred_context_switchable_interp`'s own neighbour lookup. |
| 7704 | above_filter: Vec<[u8; 2]>, |
| 7705 | left_filter: Vec<[u8; 2]>, |
| 7706 | mi_cols: usize, |
| 7707 | mi_rows: usize, |
| 7708 | /// Per-8x8 (2x2 mi cells) `skip_txfm` flag over the whole padded frame, |
| 7709 | /// spec `av1_cdef_compute_sb_list`'s `is_8x8_block_skip`: CDEF (spec |
| 7710 | /// 7.15) never filters an 8x8 luma block whose covering coded block read |
| 7711 | /// `skip` -- one flag per 4x4 mi cell (finer than needed, but matches the |
| 7712 | /// grain [`Self::record_mi`] already writes at), read back at 8x8 |
| 7713 | /// granularity by [`apply_cdef`] since `skip` is constant across every |
| 7714 | /// mi cell one coded block covers. |
| 7715 | /// The four per-mi maps [`Neighbours::blk_grid`] packs, in one cell. |
| 7716 | /// lane-cdefstrip r1 instrument: whether any coded block actually WROTE |
| 7717 | /// the `skip` bit at this mi cell during this frame. A fresh |
| 7718 | /// `Neighbours` seeds `skip_grid` all-`false` ("not skip"), so a decode |
| 7719 | /// arm that forgets [`Self::fill_skip_grid_rect`] silently makes |
| 7720 | /// [`apply_cdef`] filter an 8x8 unit libaom's `is_8x8_block_skip` |
| 7721 | /// (`cdef.c:29-38`) excludes -- exactly the lane-cdef defect, and |
| 7722 | /// unobservable from the pixels alone. Counted by |
| 7723 | /// [`cdef_unwritten_skip_units`]. |
| 7724 | /// Per-4x4-mi origin `(mi_row, mi_col)` of the coded (prediction) block |
| 7725 | /// covering that cell -- spec 7.14.2's `isBlockEdge`/libaom `pu_edge`, |
| 7726 | /// which re-enables the loop filter at a prediction boundary between two |
| 7727 | /// skipped inter blocks (lane-mergefix r4). Written by |
| 7728 | /// [`Self::fill_skip_grid_rect`], whose `at_mi` IS that origin. |
| 7729 | /// Per-4x4-mi `(width, height)` in mi units of the coded block covering |
| 7730 | /// that cell -- the other half of `pu_edge`: libaom tests the edge |
| 7731 | /// coordinate against the block's size in the FILTERED plane's own |
| 7732 | /// pixels, which for chroma is `max(luma_dim / 2, 4)` (lane-t900 r30b). |
| 7733 | blk_grid: Vec<BlkCell>, |
| 7734 | skip_grid_cols_mi: usize, |
| 7735 | /// Per-4x4-mi luma transform width in pixels (8/16/32/64) -- this decoder |
| 7736 | /// never splits a coded block's transform below its own size (round 2: |
| 7737 | /// `TxMode::Select`/`tx_depth` refused), so a block's own side *is* its |
| 7738 | /// transform size, spec 7.14's `get_transform_size`/`get_filter_level` |
| 7739 | /// edge-length lookup (`apply_deblock`). |
| 7740 | tx_grid: Vec<u8>, |
| 7741 | /// The transform HEIGHT companion to [`Self::tx_grid`]'s width -- |
| 7742 | /// lane-rect r2: TX_32X16/TX_16X32 strips deblock their horizontal |
| 7743 | /// edges by tx height (spec 7.14.2 `set_lpf_parameters` dir==VERT). |
| 7744 | tx_h_grid: Vec<u8>, |
| 7745 | /// Per-4x4-mi CHROMA transform width/height in pixels -- lane-tiny r4. |
| 7746 | /// A block's chroma transform is `av1_get_max_uv_txsize(bsize)`: the |
| 7747 | /// largest transform covering the subsampled BLOCK, capped at 32x32. It |
| 7748 | /// does NOT follow luma's `tx_depth` split, so deriving it as |
| 7749 | /// `tx_grid / 2` invented chroma transform edges the deblocker then |
| 7750 | /// filtered (32x16 frames, U plane, seeds 42/47). Written from the |
| 7751 | /// block's own mi span in [`Self::fill_lf_grid_rect`], the one place |
| 7752 | /// every caller routes through. |
| 7753 | uv_tx_grid: Vec<u8>, |
| 7754 | uv_tx_h_grid: Vec<u8>, |
| 7755 | /// Per-4x4-mi reference frame (`0` = intra, magnitude `1..=7` = |
| 7756 | /// `MV_REFERENCE_FRAME`, negative = that reference coded as `GLOBALMV` -- |
| 7757 | /// `lf_level`'s `mode_lf_lut` row 0, lane-av1golden2) |
| 7758 | /// -- the loop filter's ref/mode delta lookup (spec 7.14.4 |
| 7759 | /// `get_filter_level`, `loop_filter_ref_deltas[ref_frame]`; lane-av1refs: |
| 7760 | /// widened from a bare `is_inter` bool, which collapsed every non-`LAST` |
| 7761 | /// reference onto `LAST_FRAME`'s own delta). |
| 7762 | ref_grid: Vec<i8>, |
| 7763 | /// Per-4x4-mi snapshot of [`CURRENT_DELTA_LF`] at the moment this cell's |
| 7764 | /// block was decoded (spec `DeltaLF[i]`, `i` in `0..FRAME_LF_COUNT`, |
| 7765 | /// Y-vertical/Y-horizontal/U/V) -- lane-realworld r5: `lf_level`'s |
| 7766 | /// per-block additive term, mirroring [`Self::ref_grid`]'s own snapshot |
| 7767 | /// pattern. `!delta_lf_multi` mode broadcasts index 0 into all 4 slots |
| 7768 | /// at write time ([`Self::fill_lf_grid_rect`]) so a reader never needs |
| 7769 | /// to know which mode produced the value. |
| 7770 | delta_lf_grid: Vec<[i8; 4]>, |
| 7771 | /// The block above/left's own palette-Y size (0 = not a palette block) |
| 7772 | /// and its base colours -- `av1_get_palette_mode_ctx`/ |
| 7773 | /// `av1_get_palette_cache` (pred_common.c)'s neighbour lookup, lane-palette |
| 7774 | /// r2. UV has no equivalent yet (stage 2, unreconstructed). |
| 7775 | above_palette_size: Vec<u8>, |
| 7776 | left_palette_size: Vec<u8>, |
| 7777 | above_palette_colors: Vec<[u16; 8]>, |
| 7778 | left_palette_colors: Vec<[u16; 8]>, |
| 7779 | /// As the four fields above, for the neighbour's own chroma (U-channel |
| 7780 | /// only -- `av1_get_palette_cache`'s plane-1 lookup, [`Self::palette_uv_cache`]) |
| 7781 | /// palette size/colours (lane-palette2 r1). |
| 7782 | above_palette_uv_size: Vec<u8>, |
| 7783 | left_palette_uv_size: Vec<u8>, |
| 7784 | above_palette_uv_colors: Vec<[u16; 8]>, |
| 7785 | left_palette_uv_colors: Vec<[u16; 8]>, |
| 7786 | /// This tile's own top-left corner, in 4x4 mode-info units (spec |
| 7787 | /// `MiRowStart`/`MiColStart`) -- lane-tiles: every "is there really a |
| 7788 | /// neighbour" check compares against this instead of literal `0`, since a |
| 7789 | /// block at the tile's own top/left edge has no usable neighbour even |
| 7790 | /// though a decoded block sits there in the picture (spec `decode_tile`'s |
| 7791 | /// per-tile `clear_above_context`/`left_available`/`up_available`). |
| 7792 | /// `0` for a single-tile frame, matching every existing caller exactly. |
| 7793 | tile_row0_mi: usize, |
| 7794 | tile_col0_mi: usize, |
| 7795 | } |
| 7796 | |
| 7797 | #[allow(dead_code)] // read only from the `#[cfg(test)]` gates |
| 7798 | impl Neighbours { |
| 7799 | /// `cols`/`rows` are in [`SUB`] units; `mi_cols`/`mi_rows` are the frame's |
| 7800 | /// true (unpadded) size in 4x4 mode-info units. |
| 7801 | fn new(cols: usize, rows: usize, mi_cols: usize, mi_rows: usize, fctx: &crate::decode::FrameCtx) -> Self { |
| 7802 | // lane-sb128c r7: a 128x64 block at the bottom frame edge records its |
| 7803 | // neighbour state over the whole 64-row extent, past the frame's last |
| 7804 | // mi row (mi_rows 198, block rows 192..208). libaom's own context |
| 7805 | // buffers are superblock-aligned for exactly that reason |
| 7806 | // (`av1_alloc_context_buffers` rounds to `MAX_MIB_SIZE`), so pad the |
| 7807 | // side arrays to a superblock multiple and leave those cells unread -- |
| 7808 | // every availability check uses the true `mi_rows`/`mi_cols` below. |
| 7809 | let sb_sub = (sb_mi_cur(fctx) as usize) / (SUB / MI); |
| 7810 | let (cols, rows) = ( |
| 7811 | cols.next_multiple_of(sb_sub), |
| 7812 | rows.next_multiple_of(sb_sub), |
| 7813 | ); |
| 7814 | Self { |
| 7815 | above: vec![[Neighbour::default(); 3]; cols * (SUB / MI)], |
| 7816 | left: vec![[Neighbour::default(); 3]; rows * (SUB / MI)], |
| 7817 | above_mode: vec![DC_PRED; cols], |
| 7818 | left_mode: vec![DC_PRED; rows], |
| 7819 | above_uv_mode: vec![DC_PRED; cols], |
| 7820 | left_uv_mode: vec![DC_PRED; rows], |
| 7821 | above_side: vec![NO_NEIGHBOUR_SIDE; cols], |
| 7822 | left_side: vec![NO_NEIGHBOUR_SIDE; rows], |
| 7823 | above_side_mi: vec![NO_NEIGHBOUR_SIDE as u8; cols * (SUB / MI)], |
| 7824 | left_side_mi: vec![NO_NEIGHBOUR_SIDE as u8; rows * (SUB / MI)], |
| 7825 | sub8_mode_col: vec![(u16::MAX, 0); cols * (SUB / MI)], |
| 7826 | sub8_mode_row: vec![(u16::MAX, 0); rows * (SUB / MI)], |
| 7827 | uv_mode_col: vec![(u16::MAX, 0); cols * (SUB / MI)], |
| 7828 | uv_mode_row: vec![(u16::MAX, 0); rows * (SUB / MI)], |
| 7829 | uv_mode_grid: vec![u8::MAX; cols * (SUB / MI) * rows * (SUB / MI)], |
| 7830 | uv_grid_cols: cols * (SUB / MI), |
| 7831 | above_txfm: vec![TXFM_CTX_INIT; cols * (SUB / MI)], |
| 7832 | left_txfm: vec![TXFM_CTX_INIT; rows * (SUB / MI)], |
| 7833 | // lane-inter8 r2: mi(4px)-granular, not [`SUB`]-granular -- an |
| 7834 | // 8x8 inter leaf's left neighbour is the OTHER leaf row of the |
| 7835 | // 16x16 block beside it, which a SUB-grid slot cannot name |
| 7836 | // (class context-read-from-one-cell). |
| 7837 | above_skip: vec![false; cols * (SUB / MI)], |
| 7838 | left_skip: vec![false; rows * (SUB / MI)], |
| 7839 | above_skip_mode: vec![false; cols * (SUB / MI)], |
| 7840 | left_skip_mode: vec![false; rows * (SUB / MI)], |
| 7841 | above_inter: vec![false; cols * (SUB / MI)], |
| 7842 | left_inter: vec![false; rows * (SUB / MI)], |
| 7843 | above_ref: vec![-1; cols * (SUB / MI)], |
| 7844 | left_ref: vec![-1; rows * (SUB / MI)], |
| 7845 | above_ref1: vec![None; cols * (SUB / MI)], |
| 7846 | left_ref1: vec![None; rows * (SUB / MI)], |
| 7847 | above_comp_group_idx: vec![0; cols * (SUB / MI)], |
| 7848 | left_comp_group_idx: vec![0; rows * (SUB / MI)], |
| 7849 | above_compound_idx: vec![0; cols * (SUB / MI)], |
| 7850 | left_compound_idx: vec![0; rows * (SUB / MI)], |
| 7851 | above_filter: vec![[3u8; 2]; cols * (SUB / MI)], |
| 7852 | left_filter: vec![[3u8; 2]; rows * (SUB / MI)], |
| 7853 | mi_cols, |
| 7854 | mi_rows, |
| 7855 | blk_grid: vec![ |
| 7856 | BlkCell { skip: false, written: false, org: [0; 2], dim: [1; 2] }; |
| 7857 | cols * (SUB / MI) * rows * (SUB / MI) |
| 7858 | ], |
| 7859 | skip_grid_cols_mi: cols * (SUB / MI), |
| 7860 | tx_grid: vec![0u8; cols * (SUB / MI) * rows * (SUB / MI)], |
| 7861 | tx_h_grid: vec![0u8; cols * (SUB / MI) * rows * (SUB / MI)], |
| 7862 | uv_tx_grid: vec![0u8; cols * (SUB / MI) * rows * (SUB / MI)], |
| 7863 | uv_tx_h_grid: vec![0u8; cols * (SUB / MI) * rows * (SUB / MI)], |
| 7864 | ref_grid: vec![0i8; cols * (SUB / MI) * rows * (SUB / MI)], |
| 7865 | delta_lf_grid: vec![[0i8; 4]; cols * (SUB / MI) * rows * (SUB / MI)], |
| 7866 | above_palette_size: vec![0; cols * (SUB / MI)], |
| 7867 | left_palette_size: vec![0; rows * (SUB / MI)], |
| 7868 | above_palette_colors: vec![[0u16; 8]; cols * (SUB / MI)], |
| 7869 | left_palette_colors: vec![[0u16; 8]; rows * (SUB / MI)], |
| 7870 | above_palette_uv_size: vec![0; cols * (SUB / MI)], |
| 7871 | left_palette_uv_size: vec![0; rows * (SUB / MI)], |
| 7872 | above_palette_uv_colors: vec![[0u16; 8]; cols * (SUB / MI)], |
| 7873 | left_palette_uv_colors: vec![[0u16; 8]; rows * (SUB / MI)], |
| 7874 | tile_row0_mi: 0, |
| 7875 | tile_col0_mi: 0, |
| 7876 | } |
| 7877 | } |
| 7878 | |
| 7879 | /// spec `decode_tile`'s per-tile reset: `clear_above_context` over this |
| 7880 | /// tile's own column span (`col1_mi` exclusive), plus recording |
| 7881 | /// `(row0_mi, col0_mi)` as the tile's own top-left mi cell for every |
| 7882 | /// availability check below. Left context resets every superblock row |
| 7883 | /// regardless of tile ([`Self::start_row`], already called once per SB |
| 7884 | /// row by every caller) so it needs no tile-specific reset here. |
| 7885 | fn start_tile(&mut self, row0_mi: usize, col0_mi: usize, col1_mi: usize, fctx: &crate::decode::FrameCtx) { |
| 7886 | // lane-seg: `AvailU`/`AvailL` for the segment-id neighbour prediction |
| 7887 | // are tile-relative, same as every other neighbour lookup here. |
| 7888 | fctx.seg_tile_origin.with(|c| c.set((row0_mi, col0_mi))); |
| 7889 | self.tile_row0_mi = row0_mi; |
| 7890 | self.tile_col0_mi = col0_mi; |
| 7891 | let end = col1_mi.min(self.above.len()); |
| 7892 | for i in col0_mi.min(end)..end { |
| 7893 | self.above[i] = Default::default(); |
| 7894 | self.above_side_mi[i] = NO_NEIGHBOUR_SIDE as u8; |
| 7895 | } |
| 7896 | let (sc0, sc1) = ( |
| 7897 | col0_mi / (SUB / MI), |
| 7898 | (col1_mi / (SUB / MI)).min(self.above_mode.len()), |
| 7899 | ); |
| 7900 | for i in sc0.min(sc1)..sc1 { |
| 7901 | self.above_mode[i] = DC_PRED; |
| 7902 | self.above_side[i] = NO_NEIGHBOUR_SIDE; |
| 7903 | } |
| 7904 | // lane-inter8 r2: the inter bands are mi-granular, so they reset over |
| 7905 | // the tile's own mi column span directly. |
| 7906 | let mend = col1_mi.min(self.above_skip.len()); |
| 7907 | for i in col0_mi.min(mend)..mend { |
| 7908 | self.above_skip[i] = false; |
| 7909 | self.above_skip_mode[i] = false; |
| 7910 | self.above_inter[i] = false; |
| 7911 | self.above_ref[i] = -1; |
| 7912 | self.above_ref1[i] = None; |
| 7913 | self.above_comp_group_idx[i] = 0; |
| 7914 | self.above_compound_idx[i] = 0; |
| 7915 | self.above_filter[i] = [3u8; 2]; |
| 7916 | } |
| 7917 | // lane-sub8 r6: the mi-granular column maps are per tile too -- a |
| 7918 | // stale `(row, mode)` from the tile to the LEFT names an mi row this |
| 7919 | // tile's first block would read as its own above neighbour. |
| 7920 | let mcol_end = col1_mi.min(self.sub8_mode_col.len()); |
| 7921 | for i in col0_mi.min(mcol_end)..mcol_end { |
| 7922 | self.sub8_mode_col[i] = (u16::MAX, 0); |
| 7923 | self.uv_mode_col[i] = (u16::MAX, 0); |
| 7924 | } |
| 7925 | // lane-mergefix r2: `av1_zero_above_context` also memsets the txfm row |
| 7926 | // over the tile's own column span (to `tx_size_wide[TX_SIZES_LARGEST]`, |
| 7927 | // not zero) -- without it a second tile column reads the first one's |
| 7928 | // resolved transform sizes as its own above context. |
| 7929 | let tend = col1_mi.min(self.above_txfm.len()); |
| 7930 | for i in col0_mi.min(tend)..tend { |
| 7931 | self.above_txfm[i] = TXFM_CTX_INIT; |
| 7932 | } |
| 7933 | } |
| 7934 | |
| 7935 | /// Writes a just-decoded block's own palette-Y size/colours (`size == 0` |
| 7936 | /// for a non-palette block, clearing any stale neighbour state) into every |
| 7937 | /// [`SUB`]-grid cell it covers on both the above and left edges -- |
| 7938 | /// mirrors [`Self::record_rect`]'s loop shape, square-only (this lane's |
| 7939 | /// call sites are all square blocks). |
| 7940 | fn record_palette_y(&mut self, at: (usize, usize), side: usize, size: usize, colors: [u16; 8]) { |
| 7941 | self.record_palette_y_rect((at.0 * (SUB / MI), at.1 * (SUB / MI)), side, side, size, colors); |
| 7942 | } |
| 7943 | |
| 7944 | /// [`Self::record_palette_y`] with independent above (`w`)/left (`h`) |
| 7945 | /// extents (lane-palette2 r4, mirrors [`Self::record_rect`] vs |
| 7946 | /// [`Self::record`]): a true rect strip's above/left neighbour spans |
| 7947 | /// differ. |
| 7948 | fn record_palette_y_rect( |
| 7949 | &mut self, |
| 7950 | mi: (usize, usize), |
| 7951 | w: usize, |
| 7952 | h: usize, |
| 7953 | size: usize, |
| 7954 | colors: [u16; 8], |
| 7955 | ) { |
| 7956 | let (r, c) = mi; |
| 7957 | if crate::envflags::env_flag!("EC_PALPUB") { |
| 7958 | eprintln!("EC_PALPUB yrect mi={r},{c} bw={w} bh={h} y={size}"); |
| 7959 | } |
| 7960 | // lane-kf900 r6: mi (4px) granularity, libaom's own -- `palette_size` |
| 7961 | // lives per mi cell in `MB_MODE_INFO`, and `xd->left_mbmi` is the mi |
| 7962 | // cell immediately left of the block, not its 16px [`SUB`] cell. The |
| 7963 | // old SUB grid let a 16x8 strip pair share one slot, so the second |
| 7964 | // strip of mi(0,8) read the FIRST strip's palette size and the colour |
| 7965 | // cache came out one entry short (class [[context-read-from-one-cell]]). |
| 7966 | for cell in 0..(w / MI).max(1) { |
| 7967 | if let Some(e) = self.above_palette_size.get_mut(c + cell) { |
| 7968 | *e = size as u8; |
| 7969 | // lane-publish2: spec 5.11.46 `get_palette_cache` reads a |
| 7970 | // neighbour's colours only where its palette size > 0, so a |
| 7971 | // non-palette block need not stamp 16 bytes of zeroes here. |
| 7972 | if size > 0 { |
| 7973 | self.above_palette_colors[c + cell] = colors; |
| 7974 | } |
| 7975 | } |
| 7976 | } |
| 7977 | for cell in 0..(h / MI).max(1) { |
| 7978 | if let Some(e) = self.left_palette_size.get_mut(r + cell) { |
| 7979 | *e = size as u8; |
| 7980 | if size > 0 { |
| 7981 | self.left_palette_colors[r + cell] = colors; |
| 7982 | } |
| 7983 | } |
| 7984 | } |
| 7985 | } |
| 7986 | |
| 7987 | /// `av1_get_palette_mode_ctx` (pred_common.h:197): count of the above/left |
| 7988 | /// neighbours that are themselves a palette-Y block, plus |
| 7989 | /// `av1_get_palette_cache`'s own merged, ascending, deduplicated colour |
| 7990 | /// cache (pred_common.c:73). |
| 7991 | /// |
| 7992 | /// The SB-top-row exclusion of the above neighbour (`row % 64` in |
| 7993 | /// pred_common.c:76, `r % 4 == 0` here in [`SUB`]-grid units) belongs to |
| 7994 | /// the CACHE ALONE -- `av1_get_palette_mode_ctx` reads plain |
| 7995 | /// `xd->above_mbmi` with no such guard (lane-palette2 r7). Applying it to |
| 7996 | /// the ctx too made every superblock in the frame's second SB row read |
| 7997 | /// `palette_y_mode` off CDF row 0 while libaom used row 1, so a real |
| 7998 | /// palette block decoded as "no palette" and the tile desynced from |
| 7999 | /// there (class [[cdf-row-held-constant]]). |
| 8000 | fn palette_ctx_and_cache(&self, at: (usize, usize)) -> (usize, Vec<u16>) { |
| 8001 | self.palette_ctx_and_cache_mi((at.0 * (SUB / MI), at.1 * (SUB / MI))) |
| 8002 | } |
| 8003 | |
| 8004 | /// [`Self::palette_ctx_and_cache`] with the block's REAL mi row, which a |
| 8005 | /// 1:4 strip does not share with its [`SUB`] cell: `av1_get_palette_cache` |
| 8006 | /// drops the above neighbour on a 64-px superblock boundary |
| 8007 | /// (pred_common.c:76, `row % (1 << MIN_SB_SIZE_LOG2)` with `row` the |
| 8008 | /// block's own PIXEL row), and strip 1 of a HORZ_4 sits 8 px into the |
| 8009 | /// cell -- reading `at.0 % 4` there excludes strip 0 from the cache of |
| 8010 | /// every strip in an SB's top cell row, which libaom does not do. |
| 8011 | fn palette_ctx_and_cache_mi(&self, mi: (usize, usize)) -> (usize, Vec<u16>) { |
| 8012 | let (r, c) = mi; |
| 8013 | let above_ok = r % 16 != 0; |
| 8014 | // `xd->above_mbmi` exists for every row but the tile's own first. |
| 8015 | let above_is_palette = r > self.tile_row0_mi && self.above_palette_size[c] > 0; |
| 8016 | let (above_n, above_colors) = if above_ok && self.above_palette_size[c] > 0 { |
| 8017 | (usize::from(self.above_palette_size[c]), self.above_palette_colors[c]) |
| 8018 | } else { |
| 8019 | (0, [0u16; 8]) |
| 8020 | }; |
| 8021 | // lane-tiles r11: `av1_get_palette_cache` takes `xd->left_mbmi`, which |
| 8022 | // is NULL whenever the block sits at its TILE's own left column |
| 8023 | // (`left_available`), not the frame's -- without this guard a second |
| 8024 | // tile column reads the previous tile's palette colours into its cache |
| 8025 | // and its `palette_y_mode` ctx. |
| 8026 | let left_ok = c > self.tile_col0_mi; |
| 8027 | let (left_n, left_colors) = if left_ok && self.left_palette_size[r] > 0 { |
| 8028 | (usize::from(self.left_palette_size[r]), self.left_palette_colors[r]) |
| 8029 | } else { |
| 8030 | (0, [0u16; 8]) |
| 8031 | }; |
| 8032 | let ctx = usize::from(above_is_palette) + usize::from(left_n > 0); |
| 8033 | let mut cache = Vec::with_capacity(16); |
| 8034 | let push_dedup = |cache: &mut Vec<u16>, v: u16| { |
| 8035 | if cache.last() != Some(&v) { |
| 8036 | cache.push(v); |
| 8037 | } |
| 8038 | }; |
| 8039 | let (mut ai, mut li) = (0usize, 0usize); |
| 8040 | while ai < above_n && li < left_n { |
| 8041 | let (va, vl) = (above_colors[ai], left_colors[li]); |
| 8042 | if vl < va { |
| 8043 | push_dedup(&mut cache, vl); |
| 8044 | li += 1; |
| 8045 | } else { |
| 8046 | push_dedup(&mut cache, va); |
| 8047 | ai += 1; |
| 8048 | if vl == va { |
| 8049 | li += 1; |
| 8050 | } |
| 8051 | } |
| 8052 | } |
| 8053 | while ai < above_n { |
| 8054 | push_dedup(&mut cache, above_colors[ai]); |
| 8055 | ai += 1; |
| 8056 | } |
| 8057 | while li < left_n { |
| 8058 | push_dedup(&mut cache, left_colors[li]); |
| 8059 | li += 1; |
| 8060 | } |
| 8061 | (ctx, cache) |
| 8062 | } |
| 8063 | |
| 8064 | /// As [`Self::record_palette_y`], for a just-decoded block's own chroma |
| 8065 | /// (U-channel) palette size/colours -- `size == 0` clears stale state the |
| 8066 | /// same way (lane-palette2 r1). |
| 8067 | fn record_palette_uv(&mut self, at: (usize, usize), side: usize, size: usize, colors: [u16; 8]) { |
| 8068 | self.record_palette_uv_rect( |
| 8069 | (at.0 * (SUB / MI), at.1 * (SUB / MI)), |
| 8070 | side, |
| 8071 | side, |
| 8072 | size, |
| 8073 | colors, |
| 8074 | ); |
| 8075 | } |
| 8076 | |
| 8077 | /// [`Self::record_palette_uv`] with independent above (`w`)/left (`h`) |
| 8078 | /// extents, in LUMA pixels (lane-palette2 r4, same convention as |
| 8079 | /// [`Self::record_palette_y_rect`] -- the above/left grids are indexed at |
| 8080 | /// luma resolution regardless of which plane the recorded state is for). |
| 8081 | fn record_palette_uv_rect( |
| 8082 | &mut self, |
| 8083 | mi: (usize, usize), |
| 8084 | w: usize, |
| 8085 | h: usize, |
| 8086 | size: usize, |
| 8087 | colors: [u16; 8], |
| 8088 | ) { |
| 8089 | let (r, c) = mi; |
| 8090 | // mi granularity, see [`Self::record_palette_y_rect`]. |
| 8091 | for cell in 0..(w / MI).max(1) { |
| 8092 | if let Some(e) = self.above_palette_uv_size.get_mut(c + cell) { |
| 8093 | *e = size as u8; |
| 8094 | if size > 0 { |
| 8095 | self.above_palette_uv_colors[c + cell] = colors; |
| 8096 | } |
| 8097 | } |
| 8098 | } |
| 8099 | for cell in 0..(h / MI).max(1) { |
| 8100 | if let Some(e) = self.left_palette_uv_size.get_mut(r + cell) { |
| 8101 | *e = size as u8; |
| 8102 | if size > 0 { |
| 8103 | self.left_palette_uv_colors[r + cell] = colors; |
| 8104 | } |
| 8105 | } |
| 8106 | } |
| 8107 | } |
| 8108 | |
| 8109 | /// [`Self::palette_ctx_and_cache`]'s cache half, for the U channel |
| 8110 | /// (`av1_get_palette_cache(xd, 1, cache)`) -- `palette_uv_mode_ctx` needs |
| 8111 | /// no neighbour lookup at all (it is this block's own just-decided Y |
| 8112 | /// palette use, `read_palette_mode_info`'s `pmi->palette_size[0] > 0`), |
| 8113 | /// so only the cache is wanted here. |
| 8114 | fn palette_uv_cache(&self, at: (usize, usize)) -> Vec<u16> { |
| 8115 | self.palette_uv_cache_mi((at.0 * (SUB / MI), at.1 * (SUB / MI))) |
| 8116 | } |
| 8117 | |
| 8118 | /// [`Self::palette_uv_cache`] with the block's real mi row -- see |
| 8119 | /// [`Self::palette_ctx_and_cache_mi`]. |
| 8120 | fn palette_uv_cache_mi(&self, mi: (usize, usize)) -> Vec<u16> { |
| 8121 | let (r, c) = mi; |
| 8122 | let above_ok = r % 16 != 0; |
| 8123 | let (above_n, above_colors) = if above_ok && self.above_palette_uv_size[c] > 0 { |
| 8124 | (usize::from(self.above_palette_uv_size[c]), self.above_palette_uv_colors[c]) |
| 8125 | } else { |
| 8126 | (0, [0u16; 8]) |
| 8127 | }; |
| 8128 | // Same tile-relative left availability as the luma cache above. |
| 8129 | let left_ok = c > self.tile_col0_mi; |
| 8130 | let (left_n, left_colors) = if left_ok && self.left_palette_uv_size[r] > 0 { |
| 8131 | (usize::from(self.left_palette_uv_size[r]), self.left_palette_uv_colors[r]) |
| 8132 | } else { |
| 8133 | (0, [0u16; 8]) |
| 8134 | }; |
| 8135 | let mut cache = Vec::with_capacity(16); |
| 8136 | let push_dedup = |cache: &mut Vec<u16>, v: u16| { |
| 8137 | if cache.last() != Some(&v) { |
| 8138 | cache.push(v); |
| 8139 | } |
| 8140 | }; |
| 8141 | let (mut ai, mut li) = (0usize, 0usize); |
| 8142 | while ai < above_n && li < left_n { |
| 8143 | let (va, vl) = (above_colors[ai], left_colors[li]); |
| 8144 | if vl < va { |
| 8145 | push_dedup(&mut cache, vl); |
| 8146 | li += 1; |
| 8147 | } else { |
| 8148 | push_dedup(&mut cache, va); |
| 8149 | ai += 1; |
| 8150 | if vl == va { |
| 8151 | li += 1; |
| 8152 | } |
| 8153 | } |
| 8154 | } |
| 8155 | while ai < above_n { |
| 8156 | push_dedup(&mut cache, above_colors[ai]); |
| 8157 | ai += 1; |
| 8158 | } |
| 8159 | while li < left_n { |
| 8160 | push_dedup(&mut cache, left_colors[li]); |
| 8161 | li += 1; |
| 8162 | } |
| 8163 | cache |
| 8164 | } |
| 8165 | |
| 8166 | /// Marks every 4x4 mi cell a just-decoded coded block covers with its own |
| 8167 | /// transform width (in pixels) and `is_inter` flag -- the loop filter's |
| 8168 | /// per-edge lookup (`apply_deblock`), filled at the same call sites and |
| 8169 | /// units as [`Self::fill_skip_grid`]. |
| 8170 | fn fill_lf_grid(&mut self, at_mi: (usize, usize), side_mi: usize, tx_px: u8, ref_frame: i8, fctx: &crate::decode::FrameCtx) { |
| 8171 | self.fill_lf_grid_rect(at_mi, side_mi, side_mi, tx_px, tx_px, ref_frame, fctx); |
| 8172 | } |
| 8173 | |
| 8174 | /// [`Self::fill_lf_grid`] with independent row/col extents -- lane-partitions |
| 8175 | /// r1: a true rectangular strip's mi span isn't `side_mi` square (`w_mi` |
| 8176 | /// != `h_mi`); `tx_px` still names a single scalar (this decoder's |
| 8177 | /// existing corner-cut, `Neighbours::tx_grid`'s own doc comment -- a |
| 8178 | /// rectangular strip's transform is `TX_32X16`/`TX_16X32`, not tracked |
| 8179 | /// per-axis, matching lane-warp r5's `HORZ_B` top-strip precedent). |
| 8180 | /// libaom `set_one_param_for_line_luma`/`_chroma`: an edge that is a |
| 8181 | /// transform-unit edge but NOT a prediction-unit edge is not filtered when |
| 8182 | /// `curr_skipped = mbmi->skip_txfm && is_inter_block(mbmi)`. Widening this |
| 8183 | /// block's published transform extent to its own size is exactly that |
| 8184 | /// suppression for the deblocker's `x0 % tx != 0` test -- the only shape |
| 8185 | /// that needs it is the 128 root, whose chroma extent (64) is the one |
| 8186 | /// [`Self::fill_lf_grid_rect`]'s TX_32X32 cap narrows. An INTRA block is |
| 8187 | /// never suppressed (libaom's `is_inter_block` term), so this is called |
| 8188 | /// only from the skipped-inter branch. |
| 8189 | fn suppress_internal_lf_edges(&mut self, at_mi: (usize, usize), w_mi: usize, h_mi: usize) { |
| 8190 | let (mi_r, mi_c) = at_mi; |
| 8191 | let (uv_w, uv_h) = ((w_mi * MI / 2).max(4) as u8, (h_mi * MI / 2).max(4) as u8); |
| 8192 | for rr in 0..h_mi { |
| 8193 | let start = (mi_r + rr) * self.skip_grid_cols_mi + mi_c; |
| 8194 | fill_span(&mut self.uv_tx_grid, start, w_mi, uv_w); |
| 8195 | fill_span(&mut self.uv_tx_h_grid, start, w_mi, uv_h); |
| 8196 | } |
| 8197 | } |
| 8198 | |
| 8199 | fn fill_lf_grid_rect( |
| 8200 | &mut self, |
| 8201 | at_mi: (usize, usize), |
| 8202 | w_mi: usize, |
| 8203 | h_mi: usize, |
| 8204 | tx_px: u8, |
| 8205 | tx_h_px: u8, |
| 8206 | ref_frame: i8, fctx: &crate::decode::FrameCtx, |
| 8207 | ) { |
| 8208 | let (mi_r, mi_c) = at_mi; |
| 8209 | // `av1_get_max_uv_txsize` under 4:2:0: the block's chroma extent is |
| 8210 | // half its luma one (min 4 px, the smallest transform), capped at |
| 8211 | // TX_32X32 -- independent of this block's luma `tx_depth`. |
| 8212 | let uv_tx_w = (((w_mi * MI) >> ss_x(fctx)).max(4).min(32)) as u8; |
| 8213 | let uv_tx_h = (((h_mi * MI) >> ss_y(fctx)).max(4).min(32)) as u8; |
| 8214 | if crate::envflags::env_flag!("EC_TXGRID_TRACE") { |
| 8215 | 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}"); |
| 8216 | } |
| 8217 | crate::census::tx(tx_px, tx_h_px); |
| 8218 | let cur = fctx.current_delta_lf.with(|c| c.get()); |
| 8219 | let snapshot: [i8; 4] = if fctx.delta_lf_multi.with(|c| c.get()) { |
| 8220 | std::array::from_fn(|i| cur[i].clamp(-63, 63) as i8) |
| 8221 | } else { |
| 8222 | [cur[0].clamp(-63, 63) as i8; 4] |
| 8223 | }; |
| 8224 | for rr in 0..h_mi { |
| 8225 | let start = (mi_r + rr) * self.skip_grid_cols_mi + mi_c; |
| 8226 | fill_span(&mut self.tx_grid, start, w_mi, tx_px); |
| 8227 | fill_span(&mut self.tx_h_grid, start, w_mi, tx_h_px); |
| 8228 | fill_span(&mut self.uv_tx_grid, start, w_mi, uv_tx_w); |
| 8229 | fill_span(&mut self.uv_tx_h_grid, start, w_mi, uv_tx_h); |
| 8230 | fill_span(&mut self.ref_grid, start, w_mi, ref_frame); |
| 8231 | fill_span(&mut self.delta_lf_grid, start, w_mi, snapshot); |
| 8232 | } |
| 8233 | if fctx.intra_only.with(std::cell::Cell::get) { |
| 8234 | // libaom's `parse_decode_block` runs `set_txfm_ctxs(mbmi->tx_size, |
| 8235 | // xd->width, xd->height, mbmi->skip_txfm && is_inter_block(mbmi), |
| 8236 | // xd)` for EVERY block, an intra-only frame's included, and |
| 8237 | // `get_tx_size_context` / `txfm_partition_context` read those two |
| 8238 | // `TXFM_CONTEXT` bands. This grid carries the same transform size |
| 8239 | // over the same footprint, so the publish is this write: the |
| 8240 | // `skip && is_inter` term is 0 for every intra-only block that is |
| 8241 | // not an intrabc one, and the bodies that can code an intrabc |
| 8242 | // block publish their own (the term is then the BLOCK size, a value |
| 8243 | // this grid cannot express -- see `decode_block`/`decode_leaf8`). |
| 8244 | // Inter frames publish at their own sites and are excluded here |
| 8245 | // because their skipped-block term needs the block size too. |
| 8246 | txfm_partition_update_rect( |
| 8247 | self, |
| 8248 | at_mi, |
| 8249 | (tx_px as usize, tx_h_px as usize), |
| 8250 | (w_mi * MI, h_mi * MI), |
| 8251 | ); |
| 8252 | // libaom `is_inter_block` counts an INTRABC block (`blockd.h:373`), |
| 8253 | // so `get_tx_size_context` reads an intrabc NEIGHBOUR's block size |
| 8254 | // instead of its transform band. Every block stamps its own cells |
| 8255 | // here; the intrabc bodies re-stamp theirs `true` afterwards |
| 8256 | // (`record_intrabc_inter_bands`). |
| 8257 | for i in 0..w_mi { |
| 8258 | if let Some(cell) = self.above_inter.get_mut(mi_c + i) { |
| 8259 | *cell = false; |
| 8260 | } |
| 8261 | } |
| 8262 | for i in 0..h_mi { |
| 8263 | if let Some(cell) = self.left_inter.get_mut(mi_r + i) { |
| 8264 | *cell = false; |
| 8265 | } |
| 8266 | } |
| 8267 | } |
| 8268 | // lane-av1-intrabc r4: publish this coded block's own mi footprint |
| 8269 | // into the intrabc MV grid's block-COVERAGE map -- every coded block, |
| 8270 | // not just the intrabc ones. A later intrabc block's DV predictor is |
| 8271 | // built by libaom's own row/col scans over `xd->mi[]`, which holds an |
| 8272 | // `MB_MODE_INFO` for EVERY block and steps by |
| 8273 | // `mi_size_wide/height[candidate->bsize]`; without this footprint a |
| 8274 | // non-intrabc neighbour reads as uncovered and the scan advances it |
| 8275 | // by one cell instead of by its real width. |
| 8276 | fctx.intrabc_mi_grid.with(|g| { |
| 8277 | if let Some((grid, _, _)) = g.borrow_mut().as_mut() { |
| 8278 | grid.cover_rect(mi_r, mi_c, w_mi, h_mi); |
| 8279 | } |
| 8280 | }); |
| 8281 | } |
| 8282 | |
| 8283 | /// One var-tx / split-tx LEAF's own LUMA transform dims over its mi span. |
| 8284 | /// libaom `get_transform_size` (av1_loopfilter.c:207) applies the split |
| 8285 | /// override only `if ((plane == AOM_PLANE_Y) && ...)` -- chroma keeps |
| 8286 | /// `av1_get_max_uv_txsize(mbmi->bsize)`, the whole BLOCK's uv transform. |
| 8287 | /// Routing a leaf through [`Self::fill_lf_grid_rect`] recomputed the uv |
| 8288 | /// dims from the LEAF span and so invented chroma transform edges the |
| 8289 | /// deblocker then filtered (lane-golomb r6). `ref_grid`/`delta_lf_grid` |
| 8290 | /// are already correct from the block's own fill over the same span. |
| 8291 | fn fill_lf_grid_leaf_luma( |
| 8292 | &mut self, |
| 8293 | at_mi: (usize, usize), |
| 8294 | w_mi: usize, |
| 8295 | h_mi: usize, |
| 8296 | tx_px: u8, |
| 8297 | tx_h_px: u8, |
| 8298 | ) { |
| 8299 | let (mi_r, mi_c) = at_mi; |
| 8300 | if crate::envflags::env_flag!("EC_TXGRID_TRACE") { |
| 8301 | 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}"); |
| 8302 | } |
| 8303 | for rr in 0..h_mi { |
| 8304 | let start = (mi_r + rr) * self.skip_grid_cols_mi + mi_c; |
| 8305 | fill_span(&mut self.tx_grid, start, w_mi, tx_px); |
| 8306 | fill_span(&mut self.tx_h_grid, start, w_mi, tx_h_px); |
| 8307 | } |
| 8308 | } |
| 8309 | |
| 8310 | /// Marks every 4x4 mi cell a just-decoded coded block covers with its |
| 8311 | /// `skip` flag -- `at_mi`/`side_mi` are the block's own position and |
| 8312 | /// width/height in 4x4 mode-info units (mirroring [`Self::record_mi`]'s |
| 8313 | /// own units, not [`Self::record`]'s [`SUB`]-grid ones). |
| 8314 | fn fill_skip_grid(&mut self, at_mi: (usize, usize), side_mi: usize, skip: bool) { |
| 8315 | self.fill_skip_grid_rect(at_mi, side_mi, side_mi, skip); |
| 8316 | } |
| 8317 | |
| 8318 | /// [`Self::fill_skip_grid`] with independent row/col extents (lane-partitions r1). |
| 8319 | fn fill_skip_grid_rect(&mut self, at_mi: (usize, usize), w_mi: usize, h_mi: usize, skip: bool) { |
| 8320 | let (mi_r, mi_c) = at_mi; |
| 8321 | if skip && w_mi != h_mi { |
| 8322 | hit!(RECT_SKIP_BAND_HITS); |
| 8323 | } |
| 8324 | // lane-census: an INTRA frame's blocks never reach the inter |
| 8325 | // publisher, and this one is what every coded block writes. |
| 8326 | crate::census::block_intra_frame(w_mi, h_mi, skip); |
| 8327 | for rr in 0..h_mi { |
| 8328 | let start = (mi_r + rr) * self.skip_grid_cols_mi + mi_c; |
| 8329 | fill_span( |
| 8330 | &mut self.blk_grid, |
| 8331 | start, |
| 8332 | w_mi, |
| 8333 | BlkCell { |
| 8334 | skip, |
| 8335 | written: true, |
| 8336 | org: [mi_r as u16, mi_c as u16], |
| 8337 | dim: [w_mi as u8, h_mi as u8], |
| 8338 | }, |
| 8339 | ); |
| 8340 | } |
| 8341 | } |
| 8342 | |
| 8343 | /// Whether the 8x8 luma block at mi position `(mi_r, mi_c)` (2x2 mi |
| 8344 | /// cells) was `skip_txfm` -- [`apply_cdef`]'s dlist membership test. |
| 8345 | /// Spec/libaom `is_8x8_block_skip` requires *every* one of the four 4x4 |
| 8346 | /// mi cells the 8x8 covers to be skip (a smaller-than-8x8 partition can |
| 8347 | /// straddle the region with a mix of skip/non-skip blocks). |
| 8348 | /// A single 4x4 mi cell's own `skip` flag, unaggregated (unlike |
| 8349 | /// [`Self::is_skip_txfm`]'s 8x8 all-four-cells rule) -- what the loop |
| 8350 | /// filter's `curr_skipped`/`pv_skip_txfm` (spec 7.14.2) want, since a |
| 8351 | /// filtered edge's two sides are each exactly one coded block wide. |
| 8352 | fn skip_at(&self, mi_r: usize, mi_c: usize) -> bool { |
| 8353 | self.blk_grid |
| 8354 | .get(mi_r * self.skip_grid_cols_mi + mi_c) |
| 8355 | .is_some_and(|c| c.skip) |
| 8356 | } |
| 8357 | |
| 8358 | /// The `skip_txfm` context of a block whose top-left mi cell is |
| 8359 | /// `(mi_r, mi_c)`: libaom `av1_get_skip_txfm_context` |
| 8360 | /// (`pred_common.h:175-181`) is `above_mi->skip_txfm + left_mi->skip_txfm`, |
| 8361 | /// with an absent neighbour contributing 0. The mi grid is per tile, so |
| 8362 | /// row/column 0 is a tile edge and has no neighbour on that axis. |
| 8363 | fn skip_txfm_ctx(&self, mi_r: usize, mi_c: usize) -> usize { |
| 8364 | usize::from(mi_r > self.tile_row0_mi && self.skip_at(mi_r - 1, mi_c)) |
| 8365 | + usize::from(mi_c > self.tile_col0_mi && self.skip_at(mi_r, mi_c - 1)) |
| 8366 | } |
| 8367 | |
| 8368 | fn is_skip_txfm(&self, mi_r: usize, mi_c: usize) -> bool { |
| 8369 | (0..2).all(|rr| { |
| 8370 | (0..2).all(|cc| { |
| 8371 | self.blk_grid |
| 8372 | .get((mi_r + rr) * self.skip_grid_cols_mi + (mi_c + cc)) |
| 8373 | .is_some_and(|c| c.skip) |
| 8374 | }) |
| 8375 | }) |
| 8376 | } |
| 8377 | |
| 8378 | fn start_row(&mut self) { |
| 8379 | self.left.iter_mut().for_each(|l| *l = Default::default()); |
| 8380 | self.left_mode.iter_mut().for_each(|m| *m = DC_PRED); |
| 8381 | self.left_uv_mode.iter_mut().for_each(|m| *m = DC_PRED); |
| 8382 | self.left_side.iter_mut().for_each(|s| *s = NO_NEIGHBOUR_SIDE); |
| 8383 | self.left_side_mi.iter_mut().for_each(|s| *s = NO_NEIGHBOUR_SIDE as u8); |
| 8384 | self.left_skip.iter_mut().for_each(|s| *s = false); |
| 8385 | self.left_skip_mode.iter_mut().for_each(|s| *s = false); |
| 8386 | self.left_inter.iter_mut().for_each(|i| *i = false); |
| 8387 | self.left_ref.iter_mut().for_each(|r| *r = -1); |
| 8388 | self.left_ref1.iter_mut().for_each(|r| *r = None); |
| 8389 | self.left_comp_group_idx.iter_mut().for_each(|r| *r = 0); |
| 8390 | self.left_compound_idx.iter_mut().for_each(|r| *r = 0); |
| 8391 | self.left_filter.iter_mut().for_each(|f| *f = [3u8; 2]); |
| 8392 | // lane-sub8 r6: mi-granular row maps reset with the rest of the left |
| 8393 | // context (a tile/SB-row boundary has no left neighbour). |
| 8394 | self.sub8_mode_row.iter_mut().for_each(|s| *s = (u16::MAX, 0)); |
| 8395 | self.uv_mode_row.iter_mut().for_each(|s| *s = (u16::MAX, 0)); |
| 8396 | // lane-tiles r11 (COMMON's NEIGHBOUR MAPS rule): the palette side bands |
| 8397 | // are per-mi maps like every other one here, so they reset with the |
| 8398 | // row too -- the left guard above is what actually stops a cross-tile |
| 8399 | // read, this keeps a stale colour out of the arrays at all. |
| 8400 | self.left_palette_size.iter_mut().for_each(|s| *s = 0); |
| 8401 | self.left_palette_colors.iter_mut().for_each(|c| *c = [0u16; 8]); |
| 8402 | self.left_palette_uv_size.iter_mut().for_each(|s| *s = 0); |
| 8403 | self.left_palette_uv_colors.iter_mut().for_each(|c| *c = [0u16; 8]); |
| 8404 | // lane-mergefix r2: `av1_zero_left_context` memsets |
| 8405 | // `left_txfm_context_buffer` to `tx_size_high[TX_SIZES_LARGEST]` at |
| 8406 | // every superblock row of every tile -- the var-tx left context does |
| 8407 | // NOT carry over a tile boundary (its `txfm_partition` ctx read the |
| 8408 | // previous tile column's last block otherwise). |
| 8409 | self.left_txfm.iter_mut().for_each(|t| *t = TXFM_CTX_INIT); |
| 8410 | } |
| 8411 | |
| 8412 | /// Records a block's `skip`/`is_inter`/`interp_filter` state for the next |
| 8413 | /// block that reads it as a neighbour -- [`crate::tile`]'s |
| 8414 | /// `record_inter`. `filter` is `[3, 3]` for an intra block (spec |
| 8415 | /// `get_ref_filter_type`'s "not this ref frame" case reads the same as |
| 8416 | /// "no neighbour"). `ref_frame` is `-1` for an intra block, else the |
| 8417 | /// `1..=7` reference it coded (lane-av1refs's `single_ref_p*_ctx` needs |
| 8418 | /// the real value, not just `is_inter`). |
| 8419 | #[allow(dead_code)] // read only from the `#[cfg(test)]` gates |
| 8420 | fn record_inter( |
| 8421 | &mut self, |
| 8422 | at: (usize, usize), |
| 8423 | side: usize, |
| 8424 | skip: bool, |
| 8425 | is_inter: bool, |
| 8426 | ref_frame: i8, |
| 8427 | filter: [u8; 2], |
| 8428 | skip_mode: bool, |
| 8429 | ) { |
| 8430 | self.record_inter_rect(at, side, side, skip, is_inter, ref_frame, filter, skip_mode); |
| 8431 | } |
| 8432 | |
| 8433 | /// [`Self::record_inter`] with independent above (`w_sub`, [`SUB`]-grid |
| 8434 | /// columns) / left (`h_sub`, [`SUB`]-grid rows) extents -- lane-partitions |
| 8435 | /// r1: a true rectangular strip's above-context span (its own width) and |
| 8436 | /// left-context span (its own height) differ, unlike every caller before |
| 8437 | /// this round (class neighbour-votes-all-its-fields). |
| 8438 | #[allow(clippy::too_many_arguments)] |
| 8439 | #[allow(dead_code)] // read only from the `#[cfg(test)]` gates |
| 8440 | fn record_inter_rect( |
| 8441 | &mut self, |
| 8442 | at: (usize, usize), |
| 8443 | w_sub: usize, |
| 8444 | h_sub: usize, |
| 8445 | skip: bool, |
| 8446 | is_inter: bool, |
| 8447 | ref_frame: i8, |
| 8448 | filter: [u8; 2], |
| 8449 | skip_mode: bool, |
| 8450 | ) { |
| 8451 | let (r, c) = at; |
| 8452 | self.record_inter_rect_mi( |
| 8453 | (r * (SUB / MI), c * (SUB / MI)), |
| 8454 | w_sub / MI, |
| 8455 | h_sub / MI, |
| 8456 | skip, |
| 8457 | is_inter, |
| 8458 | ref_frame, |
| 8459 | filter, |
| 8460 | skip_mode, |
| 8461 | ); |
| 8462 | } |
| 8463 | |
| 8464 | /// [`Self::record_inter_rect`] in mi (4px) units -- lane-inter8 r2: an |
| 8465 | /// 8x8 leaf covers 2x2 mi, half a [`SUB`] cell in each direction, so the |
| 8466 | /// side bands are written (and read) per mi. Every [`SUB`]-unit caller |
| 8467 | /// routes through the wrapper above. |
| 8468 | #[allow(clippy::too_many_arguments)] |
| 8469 | fn record_inter_rect_mi( |
| 8470 | &mut self, |
| 8471 | at_mi: (usize, usize), |
| 8472 | w_mi: usize, |
| 8473 | h_mi: usize, |
| 8474 | skip: bool, |
| 8475 | is_inter: bool, |
| 8476 | ref_frame: i8, |
| 8477 | filter: [u8; 2], |
| 8478 | skip_mode: bool, |
| 8479 | ) { |
| 8480 | let (r, c) = at_mi; |
| 8481 | crate::census::block(w_mi, h_mi, is_inter, ref_frame, skip); |
| 8482 | // lane-t900 r8 (class new-map-ignores-tile-edge's twin: a map NOBODY |
| 8483 | // writes for a whole block class): an INTER block left the luma mode |
| 8484 | // map untouched, so a later intra block's `get_intra_edge_filter_type` |
| 8485 | // (spec 7.11.2, libaom `reconintra.h`'s `is_smooth`) fell back to the |
| 8486 | // coarse SUB-row band, which still held a SMOOTH mode from an earlier |
| 8487 | // block in that pixel row -- the directional edge filter then ran at |
| 8488 | // strength 3 instead of 2. libaom reads `mbmi->mode` of the neighbour, |
| 8489 | // which for an inter block is an INTER mode (>= NEARESTMV) and is |
| 8490 | // never smooth; DC_PRED is this map's equivalent (the sub-8x8 groups |
| 8491 | // already stamped it). |
| 8492 | // The chroma twin: `is_smooth(mbmi, plane > 0)` returns 0 outright for |
| 8493 | // an inter neighbour ("uv_mode is not set for inter blocks"), so the |
| 8494 | // uv map must read DC_PRED over an inter block's cells too. |
| 8495 | if is_inter { |
| 8496 | self.record_mode_mi(r, c, w_mi, h_mi, DC_PRED); |
| 8497 | self.record_uv_mode_mi(r, c, w_mi, h_mi, DC_PRED); |
| 8498 | } |
| 8499 | let altref = is_inter && ref_frame == crate::mvstack::ALTREF_FRAME; |
| 8500 | self.above_skip[c..c + w_mi].fill(skip); |
| 8501 | self.above_skip_mode[c..c + w_mi].fill(skip_mode); |
| 8502 | self.above_inter[c..c + w_mi].fill(is_inter); |
| 8503 | self.above_ref[c..c + w_mi].fill(ref_frame); |
| 8504 | // The single-ref default: `record_compound_ctx_rect_mi` overwrites |
| 8505 | // this with the block's real second reference for a compound block, |
| 8506 | // called right after this one (lane-publish2 audit). |
| 8507 | self.above_ref1[c..c + w_mi].fill(None); |
| 8508 | // libaom `get_comp_group_idx_context`/`get_comp_index_context`'s |
| 8509 | // single-ref special case: `ref_frame[0] == ALTREF_FRAME` reads |
| 8510 | // as `3`/`1` even though this block has no second reference. |
| 8511 | // `record_compound_ctx` overwrites this with the real bit for an |
| 8512 | // actual compound block's cells, called right after this one. |
| 8513 | self.above_comp_group_idx[c..c + w_mi].fill(if altref { 3 } else { 0 }); |
| 8514 | self.above_compound_idx[c..c + w_mi].fill(u8::from(altref)); |
| 8515 | self.above_filter[c..c + w_mi].fill(filter); |
| 8516 | self.left_skip[r..r + h_mi].fill(skip); |
| 8517 | self.left_skip_mode[r..r + h_mi].fill(skip_mode); |
| 8518 | self.left_inter[r..r + h_mi].fill(is_inter); |
| 8519 | self.left_ref[r..r + h_mi].fill(ref_frame); |
| 8520 | self.left_ref1[r..r + h_mi].fill(None); |
| 8521 | self.left_comp_group_idx[r..r + h_mi].fill(if altref { 3 } else { 0 }); |
| 8522 | self.left_compound_idx[r..r + h_mi].fill(u8::from(altref)); |
| 8523 | self.left_filter[r..r + h_mi].fill(filter); |
| 8524 | } |
| 8525 | |
| 8526 | /// Overwrites [`Self::record_inter`]'s ALTREF-special-case default with |
| 8527 | /// a real compound block's own `comp_group_idx`/`compound_idx` bits |
| 8528 | /// (libaom `has_second_ref(mbmi)` branch of `get_comp_group_idx_context`/ |
| 8529 | /// `get_comp_index_context`) -- called right after `record_inter` for |
| 8530 | /// the same `at`/`side`, only on the `COMPOUND_REFERENCE` path. Also |
| 8531 | /// stamps `above_ref1`/`left_ref1` with the block's real second |
| 8532 | /// reference, which `record_inter` always clears to `None` (lane-av1comp: |
| 8533 | /// this is the one caller that actually knows it) -- so the next block's |
| 8534 | /// [`NeighbourRef`](crate::mvstack::NeighbourRef) sees a real compound |
| 8535 | /// neighbour instead of `record_inter`'s single-ref default. |
| 8536 | #[allow(dead_code)] // read only from the `#[cfg(test)]` gates |
| 8537 | fn record_compound_ctx( |
| 8538 | &mut self, |
| 8539 | at: (usize, usize), |
| 8540 | side: usize, |
| 8541 | ref1: i8, |
| 8542 | comp_group_idx: u8, |
| 8543 | compound_idx: u8, |
| 8544 | ) { |
| 8545 | self.record_compound_ctx_rect(at, side, side, ref1, comp_group_idx, compound_idx); |
| 8546 | } |
| 8547 | |
| 8548 | /// [`Self::record_compound_ctx`] with independent above/left extents, |
| 8549 | /// mirroring [`Self::record_inter_rect`] (lane-partitions r1). |
| 8550 | #[allow(clippy::too_many_arguments)] |
| 8551 | #[allow(dead_code)] // read only from the `#[cfg(test)]` gates |
| 8552 | fn record_compound_ctx_rect( |
| 8553 | &mut self, |
| 8554 | at: (usize, usize), |
| 8555 | w_sub: usize, |
| 8556 | h_sub: usize, |
| 8557 | ref1: i8, |
| 8558 | comp_group_idx: u8, |
| 8559 | compound_idx: u8, |
| 8560 | ) { |
| 8561 | let (r, c) = at; |
| 8562 | self.record_compound_ctx_rect_mi( |
| 8563 | (r * (SUB / MI), c * (SUB / MI)), |
| 8564 | w_sub / MI, |
| 8565 | h_sub / MI, |
| 8566 | ref1, |
| 8567 | comp_group_idx, |
| 8568 | compound_idx, |
| 8569 | ); |
| 8570 | } |
| 8571 | |
| 8572 | /// [`Self::record_compound_ctx_rect`] in mi units (lane-inter8 r2, see |
| 8573 | /// [`Self::record_inter_rect_mi`]). |
| 8574 | fn record_compound_ctx_rect_mi( |
| 8575 | &mut self, |
| 8576 | at_mi: (usize, usize), |
| 8577 | w_mi: usize, |
| 8578 | h_mi: usize, |
| 8579 | ref1: i8, |
| 8580 | comp_group_idx: u8, |
| 8581 | compound_idx: u8, |
| 8582 | ) { |
| 8583 | let (r, c) = at_mi; |
| 8584 | crate::census::compound(); |
| 8585 | for cell in 0..w_mi { |
| 8586 | self.above_ref1[c + cell] = Some(ref1); |
| 8587 | self.above_comp_group_idx[c + cell] = comp_group_idx; |
| 8588 | self.above_compound_idx[c + cell] = compound_idx; |
| 8589 | } |
| 8590 | for cell in 0..h_mi { |
| 8591 | self.left_ref1[r + cell] = Some(ref1); |
| 8592 | self.left_comp_group_idx[r + cell] = comp_group_idx; |
| 8593 | self.left_compound_idx[r + cell] = compound_idx; |
| 8594 | } |
| 8595 | } |
| 8596 | |
| 8597 | /// The context of a block's partition symbol: delegates to the mi-precise |
| 8598 | /// reader (same pattern as `around`/`around_mi`) -- `above_side`/ |
| 8599 | /// `left_side` are only ever advanced in whole-[`SUB`] steps by |
| 8600 | /// [`Self::record`], so a leaf8's `record_mi` (lane-av1-rect) -- which |
| 8601 | /// only touches the finer mi arrays -- leaves them stale for the *next* |
| 8602 | /// sibling's own partition symbol. |
| 8603 | fn partition_ctx(&self, at: (usize, usize), side: usize) -> usize { |
| 8604 | let (r, c) = at; |
| 8605 | self.partition_ctx_mi((r * (SUB / MI), c * (SUB / MI)), side) |
| 8606 | } |
| 8607 | |
| 8608 | /// [`Self::partition_ctx`] at mi granularity, for an 8x8 leaf of a |
| 8609 | /// straddling 16x16 block: reads the finer `above_side_mi`/`left_side_mi` |
| 8610 | /// arrays so the second leaf sees the first leaf's own 8x8 side rather |
| 8611 | /// than the enclosing 16x16 slot's stale, shared state. |
| 8612 | fn partition_ctx_mi(&self, (mi_r, mi_c): (usize, usize), side: usize) -> usize { |
| 8613 | 2 * usize::from(usize::from(self.left_side_mi[mi_r]) * 2 <= side) |
| 8614 | + usize::from(usize::from(self.above_side_mi[mi_c]) * 2 <= side) |
| 8615 | } |
| 8616 | |
| 8617 | /// The gathered coded/DC-sign state of the blocks above and to the left |
| 8618 | /// of a block `side` samples across, per plane. |
| 8619 | fn around(&self, (r, c): (usize, usize), side: usize) -> [(bool, bool, i32); 3] { |
| 8620 | self.around_mi((r * (SUB / MI), c * (SUB / MI)), side) |
| 8621 | } |
| 8622 | |
| 8623 | /// [`Self::around`] taking the block's position directly in 4x4 mode-info |
| 8624 | /// units, for the same reason [`Self::record_mi`] does. |
| 8625 | fn around_mi(&self, (mi_r, mi_c): (usize, usize), side: usize) -> [(bool, bool, i32); 3] { |
| 8626 | let side_mi = side / MI; |
| 8627 | // One pass over the cells accumulating all three planes, not one pass |
| 8628 | // per plane: the three planes of a cell live in the same |
| 8629 | // `[Neighbour; 3]` (6 bytes), so the per-plane form loaded the same |
| 8630 | // cache lines three times (lane-publish). Clamp each edge to the true |
| 8631 | // frame extent: a block at the right/bottom edge may be padded beyond |
| 8632 | // `mi_cols`/`mi_rows`, and libaom's unavailable context is zero there. |
| 8633 | let above_mi = side_mi.min(self.mi_cols.saturating_sub(mi_c)); |
| 8634 | let left_mi = side_mi.min(self.mi_rows.saturating_sub(mi_r)); |
| 8635 | let mut out = [(false, false, 0i32); 3]; |
| 8636 | for cell in 0..above_mi { |
| 8637 | let above = &self.above[mi_c + cell]; |
| 8638 | for (plane, o) in out.iter_mut().enumerate() { |
| 8639 | o.0 |= above[plane].level != 0; |
| 8640 | o.2 += dc_vote(above[plane].dc); |
| 8641 | } |
| 8642 | } |
| 8643 | for cell in 0..left_mi { |
| 8644 | let left = &self.left[mi_r + cell]; |
| 8645 | for (plane, o) in out.iter_mut().enumerate() { |
| 8646 | o.1 |= left[plane].level != 0; |
| 8647 | o.2 += dc_vote(left[plane].dc); |
| 8648 | } |
| 8649 | } |
| 8650 | out |
| 8651 | } |
| 8652 | |
| 8653 | /// [`Self::around_mi_rect`] for the CHROMA planes of a 4:2:2 frame |
| 8654 | /// (lane-av1-422f): libaom `get_txb_ctx_general` reads its above votes |
| 8655 | /// over `txb_w_unit` CHROMA 4-px cells, and at ss_x 1 one chroma cell |
| 8656 | /// spans TWO luma mi columns. Both cells of a column carry the covering |
| 8657 | /// unit's whole-unit dc sign (the same value libaom stores), so the |
| 8658 | /// per-luma-mi sum counts each chroma column twice -- and |
| 8659 | /// `dc_sign_ctx`'s signum flips whenever the above and left votes have |
| 8660 | /// opposite signs with equal magnitude (measured at mi(4,4) plane 1 of |
| 8661 | /// the t422 fixture: ours read `dc_sign_cdf[1]`, the oracle row 0). |
| 8662 | /// Sampling every second above cell counts each chroma column once, |
| 8663 | /// which IS libaom's sum. The left extent is unsubsquared at ss_y 0, so |
| 8664 | /// its per-mi cells already line up one-to-one. The level booleans are |
| 8665 | /// ORs over duplicated cells, so the sampling cannot change them. |
| 8666 | fn around_mi_422_chroma( |
| 8667 | &self, |
| 8668 | (mi_r, mi_c): (usize, usize), |
| 8669 | w: usize, |
| 8670 | h: usize, |
| 8671 | ) -> [(bool, bool, i32); 3] { |
| 8672 | let (w_mi, h_mi) = (w / MI, h / MI); |
| 8673 | let above_mi = w_mi.min(self.mi_cols.saturating_sub(mi_c)); |
| 8674 | let left_mi = h_mi.min(self.mi_rows.saturating_sub(mi_r)); |
| 8675 | let mut out = [(false, false, 0i32); 3]; |
| 8676 | for cell in (0..above_mi).step_by(2) { |
| 8677 | let above = &self.above[mi_c + cell]; |
| 8678 | for (plane, o) in out.iter_mut().enumerate() { |
| 8679 | o.0 |= above[plane].level != 0; |
| 8680 | o.2 += dc_vote(above[plane].dc); |
| 8681 | } |
| 8682 | } |
| 8683 | for cell in 0..left_mi { |
| 8684 | let left = &self.left[mi_r + cell]; |
| 8685 | for (plane, o) in out.iter_mut().enumerate() { |
| 8686 | o.1 |= left[plane].level != 0; |
| 8687 | o.2 += dc_vote(left[plane].dc); |
| 8688 | } |
| 8689 | } |
| 8690 | if crate::envflags::env_flag!("EC_DCDUMP") { |
| 8691 | // lane-av1422mi24: same dump [`Self::around_mi_rect`] prints, so |
| 8692 | // a rerouted 4:2:2 chroma gather stays visible under EC_DCDUMP=1. |
| 8693 | // The above rows are the SAMPLED (every-second) cells. |
| 8694 | for (plane, o) in out.iter().enumerate() { |
| 8695 | let vote = o.2; |
| 8696 | let ab: Vec<String> = (0..above_mi) |
| 8697 | .step_by(2) |
| 8698 | .map(|k| format!("{:?}/{}", self.above[mi_c + k][plane].dc, self.above[mi_c + k][plane].level)) |
| 8699 | .collect(); |
| 8700 | let lf: Vec<String> = (0..left_mi) |
| 8701 | .map(|k| format!("{:?}/{}", self.left[mi_r + k][plane].dc, self.left[mi_r + k][plane].level)) |
| 8702 | .collect(); |
| 8703 | eprintln!("EC_DCDUMP422 mi=({mi_r},{mi_c}) plane={plane} wh=({w},{h}) vote={vote} above=[{}] left=[{}]", ab.join(","), lf.join(",")); |
| 8704 | } |
| 8705 | } |
| 8706 | out |
| 8707 | } |
| 8708 | |
| 8709 | /// [`Self::around`] with independent above (`w`)/left (`h`) extents, in |
| 8710 | /// pixels -- lane-rectwire, mirrors [`Self::record_rect`]'s asymmetric |
| 8711 | /// extent pattern for a true rectangular strip's own coefficient context. |
| 8712 | fn around_rect(&self, (r, c): (usize, usize), w: usize, h: usize) -> [(bool, bool, i32); 3] { |
| 8713 | self.around_mi_rect((r * (SUB / MI), c * (SUB / MI)), w, h) |
| 8714 | } |
| 8715 | |
| 8716 | /// [`Self::around_rect`] taking the block's position directly in 4x4 |
| 8717 | /// mode-info units, mirroring [`Self::around_mi`]. |
| 8718 | fn around_mi_rect(&self, (mi_r, mi_c): (usize, usize), w: usize, h: usize) -> [(bool, bool, i32); 3] { |
| 8719 | let (w_mi, h_mi) = (w / MI, h / MI); |
| 8720 | let above_mi = w_mi.min(self.mi_cols.saturating_sub(mi_c)); |
| 8721 | let left_mi = h_mi.min(self.mi_rows.saturating_sub(mi_r)); |
| 8722 | // [`Self::around_mi`]'s single pass, with the two extents split. |
| 8723 | let mut out = [(false, false, 0i32); 3]; |
| 8724 | for cell in 0..above_mi { |
| 8725 | let above = &self.above[mi_c + cell]; |
| 8726 | for (plane, o) in out.iter_mut().enumerate() { |
| 8727 | o.0 |= above[plane].level != 0; |
| 8728 | o.2 += dc_vote(above[plane].dc); |
| 8729 | } |
| 8730 | } |
| 8731 | for cell in 0..left_mi { |
| 8732 | let left = &self.left[mi_r + cell]; |
| 8733 | for (plane, o) in out.iter_mut().enumerate() { |
| 8734 | o.1 |= left[plane].level != 0; |
| 8735 | o.2 += dc_vote(left[plane].dc); |
| 8736 | } |
| 8737 | } |
| 8738 | if crate::envflags::env_flag!("EC_DCDUMP") { |
| 8739 | for (plane, o) in out.iter().enumerate() { |
| 8740 | let vote = o.2; |
| 8741 | let ab: Vec<String> = (0..above_mi) |
| 8742 | .map(|k| format!("{:?}/{}", self.above[mi_c + k][plane].dc, self.above[mi_c + k][plane].level)) |
| 8743 | .collect(); |
| 8744 | let lf: Vec<String> = (0..left_mi) |
| 8745 | .map(|k| format!("{:?}/{}", self.left[mi_r + k][plane].dc, self.left[mi_r + k][plane].level)) |
| 8746 | .collect(); |
| 8747 | eprintln!("EC_DCDUMP mi=({mi_r},{mi_c}) plane={plane} wh=({w},{h}) vote={vote} above=[{}] left=[{}]", ab.join(","), lf.join(",")); |
| 8748 | } |
| 8749 | } |
| 8750 | out |
| 8751 | } |
| 8752 | |
| 8753 | /// The luma `txb_skip_ctx` of a transform unit smaller than its own block |
| 8754 | /// (spec `get_txb_ctx_general`, plane 0, `plane_bsize != tx_size`): the |
| 8755 | /// above/left neighbours' magnitude tiers, OR-reduced over the unit's own |
| 8756 | /// span and each clamped to 4, indexed into [`SKIP_CONTEXTS`]. Unlike |
| 8757 | /// [`Self::around_mi`]'s plain coded/uncoded bit, this needs the real |
| 8758 | /// cumulative level -- a lone-TU block never calls this (its `txb_skip_ctx` |
| 8759 | /// is always 0, spec's `plane_bsize == tx_size` branch). |
| 8760 | fn luma_skip_ctx(&self, at_mi: (usize, usize), side_mi: usize) -> usize { |
| 8761 | self.luma_skip_ctx_rect(at_mi, side_mi, side_mi) |
| 8762 | } |
| 8763 | |
| 8764 | /// [`Self::luma_skip_ctx`] with the unit's own width and height in mi |
| 8765 | /// units (lane-rectsplitx r1) -- `get_txb_ctx_general` ORs the above |
| 8766 | /// context over `tx_size_wide` and the left one over `tx_size_high`. |
| 8767 | fn luma_skip_ctx_rect( |
| 8768 | &self, |
| 8769 | (mi_r, mi_c): (usize, usize), |
| 8770 | w_mi: usize, |
| 8771 | h_mi: usize, |
| 8772 | ) -> usize { |
| 8773 | let side_mi = w_mi; |
| 8774 | let above_mi = w_mi.min(self.mi_cols.saturating_sub(mi_c)); |
| 8775 | let left_mi = h_mi.min(self.mi_rows.saturating_sub(mi_r)); |
| 8776 | let mut top = 0u8; |
| 8777 | let mut left = 0u8; |
| 8778 | for cell in 0..above_mi { |
| 8779 | top |= self.above[mi_c + cell][0].level; |
| 8780 | } |
| 8781 | for cell in 0..left_mi { |
| 8782 | left |= self.left[mi_r + cell][0].level; |
| 8783 | } |
| 8784 | let ctx = SKIP_CONTEXTS[(top as usize).min(4)][(left as usize).min(4)]; |
| 8785 | if crate::envflags::env_flag!("EC_ECDUMP") { |
| 8786 | let ab: Vec<String> = |
| 8787 | (0..above_mi).map(|k| self.above[mi_c + k][0].level.to_string()).collect(); |
| 8788 | let lf: Vec<String> = |
| 8789 | (0..left_mi).map(|k| self.left[mi_r + k][0].level.to_string()).collect(); |
| 8790 | // lane-av1mix: the last few msac register states, so the |
| 8791 | // byte-position of the `all_zero` read can be matched against |
| 8792 | // aomdec's `EC=(value,rng,bytepos)` dump (decodetxb.c) without a |
| 8793 | // second run. |
| 8794 | let recent = crate::msac::symtrace::ecdump_recent(4) |
| 8795 | .iter() |
| 8796 | .map(|(_low, value, rng, bytepos)| format!("({value},{rng},{bytepos})")) |
| 8797 | .collect::<Vec<_>>() |
| 8798 | .join(" "); |
| 8799 | eprintln!( |
| 8800 | "EC_ECDUMP plane=0 mi=({mi_r},{mi_c}) wh=({w_mi},{h_mi}) ctx={ctx} above=[{},] left=[{},] recentEC=[{}]", |
| 8801 | ab.join(","), lf.join(","), recent |
| 8802 | ); |
| 8803 | } |
| 8804 | if crate::envflags::env_flag!("EC_AV1_TRACE") { |
| 8805 | eprintln!( |
| 8806 | "TRACE luma_skip_ctx mi=({mi_r},{mi_c}) side_mi={side_mi} top={top} left={left} ctx={ctx} band={:?}", |
| 8807 | self.left.iter().map(|e| e[0].level).collect::<Vec<_>>() |
| 8808 | ); |
| 8809 | } |
| 8810 | ctx |
| 8811 | } |
| 8812 | |
| 8813 | /// Writes one coded block into every cell it covers, on both grids: the |
| 8814 | /// mode/side arrays up to `side`'s own width, and the coefficient-context |
| 8815 | /// arrays up to the true frame edge — the units past it are left at their |
| 8816 | /// default (uncoded), even mid-cell, exactly as [`crate::tile`]'s own |
| 8817 | /// `record` leaves them (spec `av1_set_entropy_contexts`, which clamps to |
| 8818 | /// `blocks_wide`/`blocks_high` derived from the true `mi_cols`/`mi_rows`, |
| 8819 | /// not from this block's own side). |
| 8820 | fn record( |
| 8821 | &mut self, |
| 8822 | at: (usize, usize), |
| 8823 | side: usize, |
| 8824 | mode: usize, |
| 8825 | uv_mode: usize, |
| 8826 | grids: &[&[i32]; 3], |
| 8827 | ) { |
| 8828 | self.record_rect(at, side, side, mode, uv_mode, grids); |
| 8829 | } |
| 8830 | |
| 8831 | /// [`Self::record`] with independent above (`w`)/left (`h`) extents, in |
| 8832 | /// pixels -- lane-partitions r1: `above_side`/`left_side` (read back by |
| 8833 | /// [`Self::partition_ctx`]) are the neighbour's own width/height per the |
| 8834 | /// spec's per-edge partition-context rule, so a true rectangular block |
| 8835 | /// naturally wants two different values here rather than one. |
| 8836 | /// Writes this block's luma `mode` into the mi-granular |
| 8837 | /// [`Self::sub8_mode_col`]/[`Self::sub8_mode_row`] maps, keyed by the mi |
| 8838 | /// position a *later* neighbour would read it from: the block's LAST mi |
| 8839 | /// row for every column it spans (what the block below reads via |
| 8840 | /// `mi(row-1, col)`) and its LAST mi column for every row (what the |
| 8841 | /// block to the right reads via `mi(row, col-1)`). Only sub-8x8 leaves |
| 8842 | /// read these back, and only when the recorded position is exactly their |
| 8843 | /// own neighbour -- see [`Self::sub8_mode_col`]'s doc. |
| 8844 | fn record_mode_mi( |
| 8845 | &mut self, |
| 8846 | mi_r: usize, |
| 8847 | mi_c: usize, |
| 8848 | mi_w: usize, |
| 8849 | mi_h: usize, |
| 8850 | mode: usize, |
| 8851 | ) { |
| 8852 | let (last_r, last_c) = ((mi_r + mi_h - 1) as u16, (mi_c + mi_w - 1) as u16); |
| 8853 | let mode = mode as u8; |
| 8854 | for cell in 0..mi_w { |
| 8855 | if let Some(slot) = self.sub8_mode_col.get_mut(mi_c + cell) { |
| 8856 | *slot = (last_r, mode); |
| 8857 | } |
| 8858 | } |
| 8859 | for cell in 0..mi_h { |
| 8860 | if let Some(slot) = self.sub8_mode_row.get_mut(mi_r + cell) { |
| 8861 | *slot = (last_c, mode); |
| 8862 | } |
| 8863 | } |
| 8864 | } |
| 8865 | |
| 8866 | /// The mi-exact above/left neighbour luma mode for a block whose top-left |
| 8867 | /// mi is `(mi_r, mi_c)`, or `None` when no block has recorded that exact |
| 8868 | /// mi position (then the coarse [`SUB`]-grid slot is right by |
| 8869 | /// construction). See [`Self::record_mode_mi`]. |
| 8870 | fn mode_above_mi(&self, mi_r: usize, mi_c: usize) -> Option<usize> { |
| 8871 | match self.sub8_mode_col.get(mi_c) { |
| 8872 | Some(&(row, m)) if mi_r > self.tile_row0_mi && usize::from(row) == mi_r - 1 => Some(usize::from(m)), |
| 8873 | _ => None, |
| 8874 | } |
| 8875 | } |
| 8876 | |
| 8877 | fn mode_left_mi(&self, mi_r: usize, mi_c: usize) -> Option<usize> { |
| 8878 | match self.sub8_mode_row.get(mi_r) { |
| 8879 | Some(&(col, m)) if mi_c > self.tile_col0_mi && usize::from(col) == mi_c - 1 => Some(usize::from(m)), |
| 8880 | _ => None, |
| 8881 | } |
| 8882 | } |
| 8883 | |
| 8884 | /// [`Self::record_mode_mi`] for the block's `uv_mode`, into |
| 8885 | /// [`Self::uv_mode_col`]/[`Self::uv_mode_row`]. |
| 8886 | fn record_uv_mode_mi( |
| 8887 | &mut self, |
| 8888 | mi_r: usize, |
| 8889 | mi_c: usize, |
| 8890 | mi_w: usize, |
| 8891 | mi_h: usize, |
| 8892 | uv_mode: usize, |
| 8893 | ) { |
| 8894 | let (last_r, last_c) = (mi_r + mi_h - 1, mi_c + mi_w - 1); |
| 8895 | fill_span(&mut self.uv_mode_col, mi_c, mi_w, (last_r as u16, uv_mode as u8)); |
| 8896 | fill_span(&mut self.uv_mode_row, mi_r, mi_h, (last_c as u16, uv_mode as u8)); |
| 8897 | // libaom writes one `MB_MODE_INFO` pointer into every mi cell a block |
| 8898 | // covers, so the chroma neighbour read below is a plain grid read. |
| 8899 | let stride = self.uv_grid_cols; |
| 8900 | let n = stride.saturating_sub(mi_c).min(last_c + 1 - mi_c); |
| 8901 | for row in mi_r..=last_r { |
| 8902 | fill_span(&mut self.uv_mode_grid, row * stride + mi_c, n, uv_mode as u8); |
| 8903 | } |
| 8904 | } |
| 8905 | |
| 8906 | /// Whether either mi-exact chroma neighbour of the block at `(mi_r, mi_c)` |
| 8907 | /// coded a smooth `uv_mode` -- libaom's `intra_edge_filter_type` for |
| 8908 | /// planes 1/2. Falls back to the coarse [`SUB`] slot on whichever axis no |
| 8909 | /// block has recorded the exact neighbouring mi. |
| 8910 | fn smooth_uv_neighbour( |
| 8911 | &self, |
| 8912 | mi_r: usize, |
| 8913 | mi_c: usize, |
| 8914 | r: usize, |
| 8915 | c: usize, |
| 8916 | fctx: &crate::decode::FrameCtx, |
| 8917 | ) -> bool { |
| 8918 | // lane-kf900 r2: libaom reads the chroma neighbours off the CHROMA |
| 8919 | // REFERENCE block, not off the mi that carries the chroma syntax. |
| 8920 | // `set_mi_row_col` (`blockd.h`) takes |
| 8921 | // `base_mi = &xd->mi[-(mi_row & ss_y) * stride - (mi_col & ss_x)]` and |
| 8922 | // only then `chroma_above_mbmi = base_mi[-stride]` / |
| 8923 | // `chroma_left_mbmi = base_mi[-1]`. A sub-8 pair codes its chroma at |
| 8924 | // the ODD mi, so without the snap we looked one mi row/column too far |
| 8925 | // down/right -- the mi-exact slot never matched and the coarse |
| 8926 | // fallback answered instead. Measured on a 4K 10-bit key frame, chroma |
| 8927 | // 8x4 at (688, 100) = mi (51, 344): libaom's |
| 8928 | // `get_intra_edge_filter_type` is 1 (the chroma block above it is |
| 8929 | // `UV_SMOOTH_PRED`), ours was 0, which flips |
| 8930 | // `av1_use_intra_edge_upsample(8, 4, -6, type)` (`blk_wh == 12`: |
| 8931 | // false for type 1, TRUE for type 0) and upsampled an edge libaom |
| 8932 | // leaves alone -- +1 on rows 2/3 of that block and every chroma block |
| 8933 | // predicting from it (class `context-read-from-one-cell`). |
| 8934 | // lane-av1-444: the snap is the SEQUENCE's subsampling, hardcoded |
| 8935 | // `& !1` before -- at subsampling 0/0 libaom's `mi_row & 0` snaps |
| 8936 | // nothing, and the hardcoded snap read a 4x4 leaf's smooth |
| 8937 | // neighbour two mi away (frame 0 of `av1-profile1-444.ivf`, leaf |
| 8938 | // mi(5,15): left neighbour SMOOTH_V at mi(5,14), read at mi(4,13)). |
| 8939 | let snap_y = (1usize << ss_y(fctx)) - 1; |
| 8940 | let snap_x = (1usize << ss_x(fctx)) - 1; |
| 8941 | self.smooth_uv_neighbour_unsnapped(mi_r & !snap_y, mi_c & !snap_x, r, c) |
| 8942 | } |
| 8943 | |
| 8944 | /// [`Self::smooth_uv_neighbour`] without the chroma-reference snap, i.e. |
| 8945 | /// read off the mi passed in exactly. Only the 16x4/4x16 pair witness |
| 8946 | /// counter wants this: it measures how often the snap CHANGES the answer, |
| 8947 | /// which is what the lane-kf1200/kf900 chroma-reference fix buys. Every |
| 8948 | /// decode path uses the snapping form. |
| 8949 | fn smooth_uv_neighbour_unsnapped( |
| 8950 | &self, |
| 8951 | mi_r: usize, |
| 8952 | mi_c: usize, |
| 8953 | r: usize, |
| 8954 | c: usize, |
| 8955 | ) -> bool { |
| 8956 | // lane-mtfix r1: availability is TILE-relative on BOTH axes and on the |
| 8957 | // coarse fallback too. `above_uv_mode` is a frame-wide [`SUB`]-grid |
| 8958 | // array that `start_tile` does not clear, so a block on a tile's first |
| 8959 | // mi row used to read the uv_mode of the block above it in the tile |
| 8960 | // ABOVE and filter its chroma edge at that neighbour's strength |
| 8961 | // (2x2-tile intra gate, seed 46: chroma-only +-1..2 over the 8x8 block |
| 8962 | // at chroma (120, 32), the top-right tile's first block). |
| 8963 | let have_above = mi_r > self.tile_row0_mi; |
| 8964 | let have_left = mi_c > self.tile_col0_mi; |
| 8965 | if self.tile_row0_mi > 0 && !have_above && self.above_uv_mode[c] != DC_PRED |
| 8966 | || self.tile_col0_mi > 0 && !have_left && self.left_uv_mode[r] != DC_PRED |
| 8967 | { |
| 8968 | hit!(UV_TILE_EDGE_SUPPRESSED_HITS); |
| 8969 | } |
| 8970 | // The mi grid is libaom's own read; the two maps below stay as the |
| 8971 | // fallback for cells no block has written (see [`Self::uv_mode_grid`]). |
| 8972 | let cell = |grid: &Vec<u8>, row: usize, col: usize| -> Option<usize> { |
| 8973 | (col < self.uv_grid_cols) |
| 8974 | .then(|| grid.get(row * self.uv_grid_cols + col).copied()) |
| 8975 | .flatten() |
| 8976 | .filter(|&m| m != u8::MAX) |
| 8977 | .map(usize::from) |
| 8978 | }; |
| 8979 | let above = match self.uv_mode_col.get(mi_c) { |
| 8980 | _ if have_above && let Some(m) = cell(&self.uv_mode_grid, mi_r - 1, mi_c) => m, |
| 8981 | Some(&(row, m)) if have_above && usize::from(row) == mi_r - 1 => usize::from(m), |
| 8982 | _ if have_above => self.above_uv_mode[c], |
| 8983 | _ => DC_PRED, |
| 8984 | }; |
| 8985 | let left = match self.uv_mode_row.get(mi_r) { |
| 8986 | _ if have_left && let Some(m) = cell(&self.uv_mode_grid, mi_r, mi_c - 1) => m, |
| 8987 | Some(&(col, m)) if have_left && usize::from(col) == mi_c - 1 => usize::from(m), |
| 8988 | _ if have_left => self.left_uv_mode[r], |
| 8989 | _ => DC_PRED, |
| 8990 | }; |
| 8991 | if have_above && cell(&self.uv_mode_grid, mi_r - 1, mi_c).is_some_and(|m| { |
| 8992 | !matches!(self.uv_mode_col.get(mi_c), Some(&(row, cm)) if usize::from(row) == mi_r - 1 && usize::from(cm) == m) |
| 8993 | }) || have_left && cell(&self.uv_mode_grid, mi_r, mi_c - 1).is_some_and(|m| { |
| 8994 | !matches!(self.uv_mode_row.get(mi_r), Some(&(col, cm)) if usize::from(col) == mi_c - 1 && usize::from(cm) == m) |
| 8995 | }) { |
| 8996 | hit!(UV_MODE_GRID_OVERRIDE_HITS); |
| 8997 | } |
| 8998 | // lane-cfl r1 counter: how often the mi-exact chroma neighbour differs |
| 8999 | // from the coarse [`SUB`] slot -- i.e. how often a chroma edge would |
| 9000 | // have been filtered at the wrong strength by the coarse map alone. |
| 9001 | if above != self.above_uv_mode[c] || left != self.left_uv_mode[r] { |
| 9002 | hit!(UV_MODE_MI_OVERRIDE_HITS); |
| 9003 | } |
| 9004 | if (is_smooth_mode(self.above_uv_mode[c]) && !is_smooth_mode(above)) |
| 9005 | || (is_smooth_mode(self.left_uv_mode[r]) && !is_smooth_mode(left)) |
| 9006 | { |
| 9007 | hit!(INTER_NEIGHBOUR_NOT_SMOOTH_HITS); |
| 9008 | } |
| 9009 | is_smooth_mode(above) || is_smooth_mode(left) |
| 9010 | } |
| 9011 | |
| 9012 | /// The above/left luma modes libaom's `above_mi`/`left_mi` really see for a |
| 9013 | /// block at coarse [`SUB`]-grid `(r, c)`. lane-rectx r5: the coarse |
| 9014 | /// `above_mode`/`left_mode` slots are written in 16x16 cells, so a |
| 9015 | /// sub-16x16 neighbour (a 16x8 rect leaf, an 8x8 split cell) leaves them |
| 9016 | /// holding an OLDER, larger block's mode -- it only ever recorded into the |
| 9017 | /// mi-exact map. Every `kf_y_mode` reader goes through here, not just the |
| 9018 | /// strip path ([`decode_leaf_rect`] took this override in r4; a 32x16 strip |
| 9019 | /// whose left neighbour was a 16x8 leaf still read the stale row and picked |
| 9020 | /// a different CDF for the same mode value). |
| 9021 | /// lane-rectsplitx r2: availability-correct above/left luma modes for a |
| 9022 | /// block whose top-left mi is `(mi_r, mi_c)`. libaom reads `above_mbmi` |
| 9023 | /// only when `up_available` and `left_mbmi` only when `left_available`; |
| 9024 | /// the coarse [`SUB`] bands cannot express that, and with 8-px strips they |
| 9025 | /// actively lie -- four 32x8 strips of one `HORZ_4` share ONE 16x16 |
| 9026 | /// `left_mode` cell, so the second strip at tile column 0 read the FIRST |
| 9027 | /// strip's mode as its "left neighbour" (measured on the band fixture at |
| 9028 | /// mi(10,0): mode 2 where libaom has DC, a different `kf_y_mode` row). |
| 9029 | fn modes_above_left_mi( |
| 9030 | &self, |
| 9031 | mi_r: usize, |
| 9032 | mi_c: usize, |
| 9033 | fallback: (usize, usize), |
| 9034 | ) -> (usize, usize) { |
| 9035 | let above = if mi_r > self.tile_row0_mi { |
| 9036 | self.mode_above_mi(mi_r, mi_c).unwrap_or(fallback.0) |
| 9037 | } else { |
| 9038 | DC_PRED |
| 9039 | }; |
| 9040 | let left = if mi_c > self.tile_col0_mi { |
| 9041 | self.mode_left_mi(mi_r, mi_c).unwrap_or(fallback.1) |
| 9042 | } else { |
| 9043 | DC_PRED |
| 9044 | }; |
| 9045 | (above, left) |
| 9046 | } |
| 9047 | |
| 9048 | /// lane-modectx r1: the coarse-[`SUB`]-grid entry point, now availability |
| 9049 | /// correct. The mi-exact lookups already refuse to cross a tile edge, but |
| 9050 | /// their FALLBACK (`above_mode`/`left_mode`) did not: `left_mode` is reset |
| 9051 | /// per superblock row and never per tile, so the first block column of |
| 9052 | /// tile 2..N read the previous tile's mode as its left neighbour, and the |
| 9053 | /// above band survives across a tile ROW boundary the same way lane-mtfix's |
| 9054 | /// `above_uv_mode` did. libaom reads `above_mbmi`/`left_mbmi` only when |
| 9055 | /// `up_available`/`left_available` and uses DC_PRED otherwise |
| 9056 | /// ([`Self::modes_above_left_mi`]). |
| 9057 | fn modes_above_left(&self, r: usize, c: usize) -> (usize, usize) { |
| 9058 | let (mi_r, mi_c) = (r * (SUB / MI), c * (SUB / MI)); |
| 9059 | let (above, left) = |
| 9060 | self.modes_above_left_mi(mi_r, mi_c, (self.above_mode[c], self.left_mode[r])); |
| 9061 | if above != self.above_mode[c] || left != self.left_mode[r] { |
| 9062 | hit!(MODE_MI_OVERRIDE_HITS); |
| 9063 | } |
| 9064 | if (is_smooth_mode(self.above_mode[c]) && !is_smooth_mode(above)) |
| 9065 | || (is_smooth_mode(self.left_mode[r]) && !is_smooth_mode(left)) |
| 9066 | { |
| 9067 | hit!(INTER_NEIGHBOUR_NOT_SMOOTH_HITS); |
| 9068 | } |
| 9069 | // A neighbour the coarse band offered where the tile edge makes it |
| 9070 | // unavailable -- must never happen (see `MODE_TILE_EDGE_COARSE_LEAKS`). |
| 9071 | if (mi_r <= self.tile_row0_mi && self.above_mode[c] != DC_PRED) |
| 9072 | || (mi_c <= self.tile_col0_mi && self.left_mode[r] != DC_PRED) |
| 9073 | { |
| 9074 | hit!(MODE_TILE_EDGE_COARSE_LEAKS); |
| 9075 | } |
| 9076 | (above, left) |
| 9077 | } |
| 9078 | |
| 9079 | fn record_rect( |
| 9080 | &mut self, |
| 9081 | at: (usize, usize), |
| 9082 | w: usize, |
| 9083 | h: usize, |
| 9084 | mode: usize, |
| 9085 | uv_mode: usize, |
| 9086 | grids: &[&[i32]; 3], |
| 9087 | ) { |
| 9088 | self.record_rect_mi(sub16_to_mi(at), w, h, mode, uv_mode, grids); |
| 9089 | } |
| 9090 | |
| 9091 | /// [`Self::record_rect`] in mi (4 px) units -- lane-inter4 r1: a |
| 9092 | /// 32x8/8x32 strip of a 32-level 1:4 partition sits at an 8-px offset the |
| 9093 | /// [`SUB`]-unit `at` cannot name. The coarse 16-px bands are still keyed |
| 9094 | /// by the enclosing cell (a sub-16 extent writes none of them, exactly as |
| 9095 | /// the [`SUB`]-unit path does). |
| 9096 | fn record_rect_mi( |
| 9097 | &mut self, |
| 9098 | at_mi: (usize, usize), |
| 9099 | w: usize, |
| 9100 | h: usize, |
| 9101 | mode: usize, |
| 9102 | uv_mode: usize, |
| 9103 | grids: &[&[i32]; 3], |
| 9104 | ) { |
| 9105 | let (r, c) = (at_mi.0 / (SUB / MI), at_mi.1 / (SUB / MI)); |
| 9106 | // lane-inter4 r2: a strip whose own footprint runs past the frame's |
| 9107 | // last 16-px column/row stamps only the cells that exist (libaom's |
| 9108 | // arrays are padded past the frame and those cells are never read). |
| 9109 | for cell in 0..w / SUB { |
| 9110 | if c + cell >= self.above_mode.len() { |
| 9111 | break; |
| 9112 | } |
| 9113 | self.above_mode[c + cell] = mode; |
| 9114 | self.above_uv_mode[c + cell] = uv_mode; |
| 9115 | self.above_side[c + cell] = w; |
| 9116 | } |
| 9117 | for cell in 0..h / SUB { |
| 9118 | if r + cell >= self.left_mode.len() { |
| 9119 | break; |
| 9120 | } |
| 9121 | self.left_mode[r + cell] = mode; |
| 9122 | self.left_uv_mode[r + cell] = uv_mode; |
| 9123 | self.left_side[r + cell] = h; |
| 9124 | } |
| 9125 | self.record_mode_mi(at_mi.0, at_mi.1, w / MI, h / MI, mode); |
| 9126 | self.record_uv_mode_mi(at_mi.0, at_mi.1, w / MI, h / MI, uv_mode); |
| 9127 | self.record_mi_rect(at_mi, w, h, grids); |
| 9128 | } |
| 9129 | |
| 9130 | /// The coefficient-context half of [`Self::record`], taking the block's |
| 9131 | /// position directly in 4x4 mode-info units rather than [`SUB`]-grid |
| 9132 | /// `(r, c)`: an 8x8 leaf under a straddling 16x16 (lane-av1-rect) sits at |
| 9133 | /// a mi offset [`Self::record`]'s SUB-unit `at` cannot name. |
| 9134 | fn record_mi(&mut self, at_mi: (usize, usize), side: usize, grids: &[&[i32]; 3]) { |
| 9135 | self.record_mi_rect(at_mi, side, side, grids); |
| 9136 | } |
| 9137 | |
| 9138 | /// [`Self::record_mi`] with independent above (`w`)/left (`h`) extents, |
| 9139 | /// in pixels (lane-partitions r1, mirroring [`Self::record_rect`]). |
| 9140 | fn record_mi_rect( |
| 9141 | &mut self, |
| 9142 | at_mi: (usize, usize), |
| 9143 | w: usize, |
| 9144 | h: usize, |
| 9145 | grids: &[&[i32]; 3], |
| 9146 | ) { |
| 9147 | let (mi_r, mi_c) = at_mi; |
| 9148 | let states: [Neighbour; 3] = std::array::from_fn(|plane| neighbour_state(grids[plane])); |
| 9149 | let (w_mi, h_mi) = (w / MI, h / MI); |
| 9150 | if crate::envflags::env_flag!("EC_ECPUB") { |
| 9151 | eprintln!("EC_ECPUB blk mi=({mi_r},{mi_c}) wh=({w},{h}) lvl={}", states[0].level); |
| 9152 | } |
| 9153 | fill_span(&mut self.left_side_mi, mi_r, h_mi, h as u8); |
| 9154 | fill_span(&mut self.above_side_mi, mi_c, w_mi, w as u8); |
| 9155 | // A chroma 4x4 unit straddling the true luma edge is still whole in |
| 9156 | // chroma's own halved grid, so libaom rounds the luma bound up to the |
| 9157 | // plane's own 4x4 unit before clamping a subsampled plane |
| 9158 | // (`av1_write_intra_coeffs_mb`, encodetxb.c). |
| 9159 | let round_up_even = |n: usize| n.div_ceil(2) * 2; |
| 9160 | let bound_h = [ |
| 9161 | self.mi_rows, |
| 9162 | round_up_even(self.mi_rows), |
| 9163 | round_up_even(self.mi_rows), |
| 9164 | ]; |
| 9165 | let bound_w = [ |
| 9166 | self.mi_cols, |
| 9167 | round_up_even(self.mi_cols), |
| 9168 | round_up_even(self.mi_cols), |
| 9169 | ]; |
| 9170 | // lane-sb128c r7: a 128x64 at the bottom frame edge names mi rows |
| 9171 | // past the padded side arrays (mi_rows 198, block rows 192..208); |
| 9172 | // libaom's own arrays are superblock-sized, so those cells simply |
| 9173 | // have no reader -- [`fill_span`] drops them, as the `break` did. |
| 9174 | // `bound_h[1] == bound_h[2] >= bound_h[0]`, so the per-cell plane |
| 9175 | // predicate is three contiguous runs: all planes, chroma only, none |
| 9176 | // (lane-mi). |
| 9177 | let luma_h = h_mi.min(bound_h[0].saturating_sub(mi_r)); |
| 9178 | let chroma_h = h_mi.min(bound_h[1].saturating_sub(mi_r)); |
| 9179 | let uv_only = [Neighbour::default(), states[1], states[2]]; |
| 9180 | fill_span(&mut self.left, mi_r, luma_h, states); |
| 9181 | fill_span(&mut self.left, mi_r + luma_h, chroma_h - luma_h, uv_only); |
| 9182 | fill_span( |
| 9183 | &mut self.left, |
| 9184 | mi_r + chroma_h, |
| 9185 | h_mi - chroma_h, |
| 9186 | [Neighbour::default(); 3], |
| 9187 | ); |
| 9188 | let luma_w = w_mi.min(bound_w[0].saturating_sub(mi_c)); |
| 9189 | let chroma_w = w_mi.min(bound_w[1].saturating_sub(mi_c)); |
| 9190 | let uv_only_w = [Neighbour::default(), states[1], states[2]]; |
| 9191 | fill_span(&mut self.above, mi_c, luma_w, states); |
| 9192 | fill_span(&mut self.above, mi_c + luma_w, chroma_w - luma_w, uv_only_w); |
| 9193 | fill_span( |
| 9194 | &mut self.above, |
| 9195 | mi_c + chroma_w, |
| 9196 | w_mi - chroma_w, |
| 9197 | [Neighbour::default(); 3], |
| 9198 | ); |
| 9199 | } |
| 9200 | |
| 9201 | /// [`Self::record_mi`]'s plane-0 half only, for one luma transform unit |
| 9202 | /// inside a coded block whose luma transform is smaller than its own |
| 9203 | /// side (`TxMode::Select`): the real per-transform-unit coefficient |
| 9204 | /// context (spec `AboveLevelContext`/`LeftLevelContext`), unlike the |
| 9205 | /// coarse per-block `tx_grid`/`fill_lf_grid` state |
| 9206 | /// [`Self::record_split_luma`] still writes once for the whole block. |
| 9207 | /// Leaves `above_side_mi`/`left_side_mi` and the chroma planes alone -- |
| 9208 | /// the caller's own [`Self::record_split_luma`] call sets those once, |
| 9209 | /// at the block's own true side, not this TU's. |
| 9210 | fn record_mi_luma(&mut self, at_mi: (usize, usize), tx_px: usize, grid: &[i32]) { |
| 9211 | self.record_mi_luma_rect(at_mi, tx_px, tx_px, grid); |
| 9212 | } |
| 9213 | |
| 9214 | /// [`Self::record_mi_luma`] for ONE CHROMA transform unit of a block whose |
| 9215 | /// uv plane block is several units (lane-sb128b r2: a 128x128 block's |
| 9216 | /// 64x64 uv plane block is four TX_32X32 units, `av1_get_max_uv_txsize`). |
| 9217 | /// `w_px`/`h_px` are the unit's extent in LUMA pixels -- the chroma |
| 9218 | /// coefficient-context arrays are indexed by luma mi, the convention |
| 9219 | /// [`Self::record_split_luma_rect_mi`] already writes them in, frame-edge |
| 9220 | /// clamp included. |
| 9221 | fn record_mi_chroma( |
| 9222 | &mut self, |
| 9223 | (mi_r, mi_c): (usize, usize), |
| 9224 | w_px: usize, |
| 9225 | h_px: usize, |
| 9226 | plane: usize, |
| 9227 | grid: &[i32], |
| 9228 | ) { |
| 9229 | let round_up_even = |n: usize| n.div_ceil(2) * 2; |
| 9230 | let (bound_h, bound_w) = (round_up_even(self.mi_rows), round_up_even(self.mi_cols)); |
| 9231 | let state = neighbour_state(grid); |
| 9232 | if crate::envflags::env_flag!("EC_CHROMA_PUB") { |
| 9233 | eprintln!( |
| 9234 | "EC_CHROMA_PUB plane={plane} mi=({mi_r},{mi_c}) wh=({w_px},{h_px}) lvl={} dc={}", |
| 9235 | state.level, |
| 9236 | state.dc.map_or(-1, |s| i32::from(s)) |
| 9237 | ); |
| 9238 | } |
| 9239 | for cell in 0..(h_px / MI).min(bound_h.saturating_sub(mi_r)) { |
| 9240 | self.left[mi_r + cell][plane] = state; |
| 9241 | } |
| 9242 | for cell in 0..(w_px / MI).min(bound_w.saturating_sub(mi_c)) { |
| 9243 | self.above[mi_c + cell][plane] = state; |
| 9244 | } |
| 9245 | } |
| 9246 | |
| 9247 | /// [`Self::record_mi_luma`] for a RECTANGULAR transform unit (lane-rectsplitx |
| 9248 | /// r1): the above context spans the unit's width, the left context its |
| 9249 | /// height, which for a square unit is the same loop it always was. |
| 9250 | fn record_mi_luma_rect( |
| 9251 | &mut self, |
| 9252 | (mi_r, mi_c): (usize, usize), |
| 9253 | w_px: usize, |
| 9254 | h_px: usize, |
| 9255 | grid: &[i32], |
| 9256 | ) { |
| 9257 | let state = neighbour_state(grid); |
| 9258 | if crate::envflags::env_flag!("EC_ECPUB") { |
| 9259 | eprintln!("EC_ECPUB tu mi=({mi_r},{mi_c}) wh=({w_px},{h_px}) lvl={}", state.level); |
| 9260 | } |
| 9261 | for cell in 0..h_px / MI { |
| 9262 | if mi_r + cell < self.mi_rows { |
| 9263 | self.left[mi_r + cell][0] = state; |
| 9264 | } |
| 9265 | } |
| 9266 | for cell in 0..w_px / MI { |
| 9267 | if mi_c + cell < self.mi_cols { |
| 9268 | self.above[mi_c + cell][0] = state; |
| 9269 | } |
| 9270 | } |
| 9271 | } |
| 9272 | |
| 9273 | /// [`Self::record`]'s mode/side and chroma-context bookkeeping, for a |
| 9274 | /// block whose luma was decoded as several transform units through |
| 9275 | /// [`Self::record_mi_luma`] rather than [`Self::record`]'s own single |
| 9276 | /// whole-block luma write -- everything [`Self::record`] does except |
| 9277 | /// plane 0, which is already correct per-TU by the time this runs. |
| 9278 | fn record_split_luma( |
| 9279 | &mut self, |
| 9280 | at: (usize, usize), |
| 9281 | side: usize, |
| 9282 | mode: usize, |
| 9283 | uv_mode: usize, |
| 9284 | chroma_grids: [&[i32]; 2], |
| 9285 | ) { |
| 9286 | self.record_split_luma_rect(at, side, side, mode, uv_mode, chroma_grids); |
| 9287 | } |
| 9288 | |
| 9289 | /// [`Self::record_split_luma`] with independent above (`w`)/left (`h`) |
| 9290 | /// extents in pixels (lane-rectsplit r1), the same relationship |
| 9291 | /// [`Self::record_rect`] has to [`Self::record`]: a HORZ/VERT strip whose |
| 9292 | /// luma transform split into several square units needs exactly this |
| 9293 | /// bookkeeping, at its own true rectangular extents. |
| 9294 | fn record_split_luma_rect( |
| 9295 | &mut self, |
| 9296 | at: (usize, usize), |
| 9297 | w: usize, |
| 9298 | h: usize, |
| 9299 | mode: usize, |
| 9300 | uv_mode: usize, |
| 9301 | chroma_grids: [&[i32]; 2], |
| 9302 | ) { |
| 9303 | let (r, c) = at; |
| 9304 | self.record_split_luma_rect_mi( |
| 9305 | (r * (SUB / MI), c * (SUB / MI)), |
| 9306 | w, |
| 9307 | h, |
| 9308 | mode, |
| 9309 | uv_mode, |
| 9310 | chroma_grids, |
| 9311 | ); |
| 9312 | } |
| 9313 | |
| 9314 | /// [`Self::record_split_luma_rect`] addressed in mi units (lane-rectsplitx |
| 9315 | /// r1) -- a strip 8 px tall starts mid-[`SUB`]-cell, which the coarse |
| 9316 | /// `(r, c)` form cannot name. The coarse mode/side arrays are stamped for |
| 9317 | /// every cell the strip TOUCHES (`div_ceil`), last writer wins, the |
| 9318 | /// convention [`decode_block_rect4`] already established for 1:4 strips. |
| 9319 | fn record_split_luma_rect_mi( |
| 9320 | &mut self, |
| 9321 | at_mi: (usize, usize), |
| 9322 | w: usize, |
| 9323 | h: usize, |
| 9324 | mode: usize, |
| 9325 | uv_mode: usize, |
| 9326 | chroma_grids: [&[i32]; 2], |
| 9327 | ) { |
| 9328 | let (mi_r, mi_c) = at_mi; |
| 9329 | let (r, c) = (mi_r / (SUB / MI), mi_c / (SUB / MI)); |
| 9330 | for cell in 0..w.div_ceil(SUB) { |
| 9331 | if c + cell >= self.above_mode.len() { |
| 9332 | break; |
| 9333 | } |
| 9334 | self.above_mode[c + cell] = mode; |
| 9335 | self.above_uv_mode[c + cell] = uv_mode; |
| 9336 | self.above_side[c + cell] = w; |
| 9337 | } |
| 9338 | for cell in 0..h.div_ceil(SUB) { |
| 9339 | if r + cell >= self.left_mode.len() { |
| 9340 | break; |
| 9341 | } |
| 9342 | self.left_mode[r + cell] = mode; |
| 9343 | self.left_uv_mode[r + cell] = uv_mode; |
| 9344 | self.left_side[r + cell] = h; |
| 9345 | } |
| 9346 | |
| 9347 | let (w_mi, h_mi) = (w / MI, h / MI); |
| 9348 | self.record_mode_mi(mi_r, mi_c, w_mi, h_mi, mode); |
| 9349 | self.record_uv_mode_mi(mi_r, mi_c, w_mi, h_mi, uv_mode); |
| 9350 | fill_span(&mut self.left_side_mi, mi_r, h_mi, h as u8); |
| 9351 | fill_span(&mut self.above_side_mi, mi_c, w_mi, w as u8); |
| 9352 | let round_up_even = |n: usize| n.div_ceil(2) * 2; |
| 9353 | let (bound_h, bound_w) = (round_up_even(self.mi_rows), round_up_even(self.mi_cols)); |
| 9354 | for (plane_idx, grid) in chroma_grids.into_iter().enumerate() { |
| 9355 | let plane = plane_idx + 1; |
| 9356 | let state = neighbour_state(grid); |
| 9357 | for cell in 0..h_mi { |
| 9358 | if cell < h_mi.min(bound_h.saturating_sub(mi_r)) { |
| 9359 | self.left[mi_r + cell][plane] = state; |
| 9360 | } |
| 9361 | } |
| 9362 | for cell in 0..w_mi { |
| 9363 | if cell < w_mi.min(bound_w.saturating_sub(mi_c)) { |
| 9364 | self.above[mi_c + cell][plane] = state; |
| 9365 | } |
| 9366 | } |
| 9367 | } |
| 9368 | } |
| 9369 | } |
| 9370 | |
| 9371 | /// Reads one coded block's skip flag, luma mode, angle delta (if the mode |
| 9372 | /// carries one) and chroma mode, mirroring [`crate::tile`]'s `write_intra_mode` |
| 9373 | /// exactly — including its skip context, which the real key-frame writer |
| 9374 | /// always writes at a fixed context of zero (it never tracks a neighbour-based |
| 9375 | /// one for intra blocks; that is the inter-frame writer's rule instead). |
| 9376 | /// Refuses a nonzero angle delta: this decoder reconstructs the thirteen |
| 9377 | /// luma modes at their base angle only. Chroma is DC-only or, where CFL is |
| 9378 | /// offered (`cfl`, `is_cfl_allowed`, spec 5.11.5), chroma-from-luma -- the |
| 9379 | /// third element carries that mode's `(alpha_q3_u, alpha_q3_v)`, `None` for |
| 9380 | /// plain DC chroma. |
| 9381 | /// spec 5.11.47 / libaom `av1_read_tx_type`'s `fimode_to_intradir`: which |
| 9382 | /// `intra_ext_tx_cdf` row a luma transform reads. A filter-intra block's |
| 9383 | /// `mode` is always `DC_PRED`, so the row comes from the FILTER-INTRA mode |
| 9384 | /// instead (the defect commit 1a89ed9 fixed at the square sites). |
| 9385 | pub(crate) fn fi_tx_row(mode: usize, filter_intra: Option<usize>) -> usize { |
| 9386 | const FIMODE_TO_INTRADIR: [usize; 5] = [DC_PRED, V_PRED, H_PRED, D157_PRED, DC_PRED]; |
| 9387 | filter_intra.map_or(mode, |fi| FIMODE_TO_INTRADIR[fi]) |
| 9388 | } |
| 9389 | |
| 9390 | /// A block-size class index into [`cdf::FILTER_INTRA`]/`Cdfs::filter_intra`, |
| 9391 | /// or `None` when `side` is past `av1_filter_intra_allowed_bsize`'s <=32 |
| 9392 | /// bound (spec `filter_intra_mode_info` never reads a symbol there). |
| 9393 | pub(crate) fn filter_intra_size_class(side: usize) -> Option<usize> { |
| 9394 | match side { |
| 9395 | 4 => Some(0), |
| 9396 | 8 => Some(1), |
| 9397 | 16 => Some(2), |
| 9398 | 32 => Some(3), |
| 9399 | _ => None, |
| 9400 | } |
| 9401 | } |
| 9402 | |
| 9403 | /// [`filter_intra_size_class`] for a true `bw`x`bh` rect strip |
| 9404 | /// (lane-intradisp r1): `av1_filter_intra_allowed_bsize` permits any bsize |
| 9405 | /// with both dims <= 32, which includes `BLOCK_32X16`/`BLOCK_16X32` -- their |
| 9406 | /// own distinct `cdf::FILTER_INTRA` rows, classes 4/5. |
| 9407 | fn filter_intra_size_class_rect(bw: usize, bh: usize) -> Option<usize> { |
| 9408 | match (bw, bh) { |
| 9409 | (32, 16) => Some(4), |
| 9410 | (16, 32) => Some(5), |
| 9411 | // lane-rectx r3: `av1_filter_intra_allowed_bsize` is "both sides <= |
| 9412 | // 32", so a 16x8/8x16 strip reads the flag too -- returning `None` |
| 9413 | // here dropped that symbol on every DC_PRED strip of that size and |
| 9414 | // desynced the tile (caught against an aomdec EC_TRACE_COEFF ladder: |
| 9415 | // identical mode/uv_mode values, range 34808 vs the oracle's 40668). |
| 9416 | (16, 8) => Some(6), |
| 9417 | (8, 16) => Some(7), |
| 9418 | // lane-fi8 r1: the sub-8 rect leaves are allowed too (both sides <= |
| 9419 | // 32), and `decode_leaf_rect8` codes them -- rows 1/2 of |
| 9420 | // `default_filter_intra_cdfs`, NOT BLOCK_4X4's row 0. |
| 9421 | (4, 8) => Some(8), |
| 9422 | (8, 4) => Some(9), |
| 9423 | // lane-fi32x8 r1: same bound, the 32x32-level 1:4 pair |
| 9424 | // (`decode_block_rect4`'s 32x8/8x32 strips). `None` here read no |
| 9425 | // `use_filter_intra` symbol on those strips although aomenc writes |
| 9426 | // one for every DC_PRED block of that shape with |
| 9427 | // `--enable-filter-intra=1` (its default), silently desyncing the |
| 9428 | // tile (class symbol-consumption-gap). |
| 9429 | (32, 8) => Some(10), |
| 9430 | (8, 32) => Some(11), |
| 9431 | // lane-r14: the 16x16-level 1:4 pair (`decode_rect4_16`'s 16x4/4x16 |
| 9432 | // strips), rows 17/16 of `default_filter_intra_cdfs`. |
| 9433 | (16, 4) => Some(12), |
| 9434 | (4, 16) => Some(13), |
| 9435 | _ if bw == bh => filter_intra_size_class(bw), |
| 9436 | _ => None, |
| 9437 | } |
| 9438 | } |
| 9439 | |
| 9440 | /// `av1_allow_palette`'s size bound (blockd.h: `block_size_wide/high <= |
| 9441 | /// MAX_PALETTE_BLOCK_WIDTH/HEIGHT` (64) and `sb_type >= BLOCK_8X8`) plus |
| 9442 | /// `av1_get_palette_bsize_ctx` (pred_common.h: `num_pels_log2_lookup[bsize] |
| 9443 | /// - num_pels_log2_lookup[BLOCK_8X8]`, i.e. `log2(bw*bh) - 6`) -- `None` past |
| 9444 | /// the bound (never gates a palette read), `Some(bsize_ctx)` otherwise. |
| 9445 | pub(crate) fn palette_bsize_ctx(side: usize) -> Option<usize> { |
| 9446 | palette_bsize_ctx_wh(side, side) |
| 9447 | } |
| 9448 | |
| 9449 | /// [`palette_bsize_ctx`], generalised to a true `bw`x`bh` rect strip |
| 9450 | /// (lane-palette2 r4) -- the same `log2(bw*bh) - 6` formula libaom's |
| 9451 | /// `num_pels_log2_lookup[bsize] - num_pels_log2_lookup[BLOCK_8X8]` computes |
| 9452 | /// for ANY bsize, not just the square ones a hand-picked table happened to |
| 9453 | /// enumerate before ([[cdf-row-held-constant]]: a table narrowed to a pinned |
| 9454 | /// row breaks the moment its indexing field moves off the diagonal, and a |
| 9455 | /// 64x32/32x64 superblock-level strip is exactly such a move). Every AV1 |
| 9456 | /// block dimension is a power of two, so `bw*bh` always is too and |
| 9457 | /// `trailing_zeros` is `log2` exactly. `None` past `av1_allow_palette`'s own |
| 9458 | /// bound (`block_size_wide/high <= 64`, `sb_type >= BLOCK_8X8`). |
| 9459 | fn palette_bsize_ctx_wh(bw: usize, bh: usize) -> Option<usize> { |
| 9460 | // lane-r14: `av1_allow_palette`'s bound is `sb_type >= BLOCK_8X8` by the |
| 9461 | // BLOCK_SIZES_ALL enum INDEX, which the 1:4 shapes BLOCK_4X16 (16) and |
| 9462 | // BLOCK_16X4 (17) clear -- a 64-pixel block, so `av1_get_palette_bsize_ctx` |
| 9463 | // (`num_pels_log2 - num_pels_log2[BLOCK_8X8]`) is 0 for them, exactly as |
| 9464 | // for an 8x8. Reading the dimension bound as "both sides >= 8" dropped |
| 9465 | // `palette_y_mode` on every 16x4/4x16 strip of a screen-content frame. |
| 9466 | if bw > 64 || bh > 64 || bw * bh < 64 { |
| 9467 | return None; |
| 9468 | } |
| 9469 | Some(((bw * bh) as u32).trailing_zeros() as usize - 6) |
| 9470 | } |
| 9471 | |
| 9472 | /// A just-decoded palette-Y block's own size, base colours (only the first |
| 9473 | /// `size` entries are real), and per-pixel colour-index map (`side*side`, |
| 9474 | /// row-major, each entry `< size`). |
| 9475 | struct PaletteY { |
| 9476 | size: usize, |
| 9477 | colors: [u16; 8], |
| 9478 | map: Vec<u8>, |
| 9479 | } |
| 9480 | |
| 9481 | /// As [`PaletteY`], for a just-decoded chroma palette block: one shared |
| 9482 | /// per-pixel colour-index map (spec `av1_visit_palette`'s plane-1 pass is |
| 9483 | /// shared between U and V, both co-located after 4:2:0 subsampling) plus |
| 9484 | /// each plane's own base colours. |
| 9485 | struct PaletteUv { |
| 9486 | size: usize, |
| 9487 | u_colors: [u16; 8], |
| 9488 | v_colors: [u16; 8], |
| 9489 | map: Vec<u8>, |
| 9490 | } |
| 9491 | |
| 9492 | /// `aom_ceil_log2` (aom_dsp/bitwriter_buffer.h-adjacent helper, used both by |
| 9493 | /// `read_palette_colors_y`'s shrinking `bits` and `av1_read_uniform`'s |
| 9494 | /// `get_unsigned_bits`, which is the same formula): smallest `n` with |
| 9495 | /// `2^n >= x`, `0` for `x <= 1`. |
| 9496 | pub(crate) fn ceil_log2(x: u32) -> u32 { |
| 9497 | if x < 2 { 0 } else { 32 - (x - 1).leading_zeros() } |
| 9498 | } |
| 9499 | |
| 9500 | /// `av1_read_uniform` (decoder.h:425): a uniform `0..n` value coded as |
| 9501 | /// `l-1` raw bits plus a conditional extra bit, `l = get_unsigned_bits(n)`. |
| 9502 | fn read_uniform(dec: &mut SymbolDecoder, n: usize) -> usize { |
| 9503 | let l = ceil_log2(n as u32); |
| 9504 | let m = (1usize << l) - n; |
| 9505 | let v = dec.literal(l - 1) as usize; |
| 9506 | if v < m { |
| 9507 | v |
| 9508 | } else { |
| 9509 | (v << 1) - m + dec.literal(1) as usize |
| 9510 | } |
| 9511 | } |
| 9512 | |
| 9513 | /// `merge_colors` (decodemv.c:462): interleaves the ascending `cached` |
| 9514 | /// colours (already-known, from the above/left neighbours) with the |
| 9515 | /// ascending `transmitted` ones (delta-coded off the wire) back into one |
| 9516 | /// ascending list of `cached.len() + transmitted.len()` colours -- a tied |
| 9517 | /// value picks the cached copy first, matching the C `<=`. |
| 9518 | fn merge_colors(transmitted: &[u16], cached: &[u16]) -> Vec<u16> { |
| 9519 | let mut out = Vec::with_capacity(transmitted.len() + cached.len()); |
| 9520 | let (mut ci, mut ti) = (0, 0); |
| 9521 | while ci < cached.len() && ti < transmitted.len() { |
| 9522 | if cached[ci] <= transmitted[ti] { |
| 9523 | out.push(cached[ci]); |
| 9524 | ci += 1; |
| 9525 | } else { |
| 9526 | out.push(transmitted[ti]); |
| 9527 | ti += 1; |
| 9528 | } |
| 9529 | } |
| 9530 | out.extend_from_slice(&cached[ci..]); |
| 9531 | out.extend_from_slice(&transmitted[ti..]); |
| 9532 | out |
| 9533 | } |
| 9534 | |
| 9535 | /// `read_palette_colors_y` (decodemv.c:478), at the sequence's real |
| 9536 | /// `bit_depth` -- lane-kf900 r3: every raw literal here (the first colour, |
| 9537 | /// each delta's width, the `range` bound and the clamp) is sized by |
| 9538 | /// `bit_depth`, and hardcoding 8 read 2 bits too few per colour on every |
| 9539 | /// 10-bit palette block. Raw literals are EQUIPROBABLE, and at the ranges |
| 9540 | /// this stream reaches (e.g. 62728) an equiprobable read is a FIXED POINT of |
| 9541 | /// the msac range, so a cross-decoder range ladder stays bit-identical |
| 9542 | /// through the whole miscount -- only the decoded VALUE differs (class |
| 9543 | /// `compare-range-not-tell`'s blind spot). (this |
| 9544 | /// decoder is 8-bit only): accepts up to `n` colours straight from `cache` |
| 9545 | /// (one flag bit each, in cache order), then delta-codes the remainder |
| 9546 | /// (first value raw, each following delta `>= 1` off a shrinking bit width), |
| 9547 | /// and merges the two ascending lists back together. |
| 9548 | fn read_palette_colors_y(dec: &mut SymbolDecoder, n: usize, cache: &[u16], fctx: &crate::decode::FrameCtx) -> [u16; 8] { |
| 9549 | let mut cached = Vec::with_capacity(n); |
| 9550 | for &c in cache { |
| 9551 | if cached.len() >= n { |
| 9552 | break; |
| 9553 | } |
| 9554 | if dec.literal(1) == 1 { |
| 9555 | cached.push(c); |
| 9556 | } |
| 9557 | } |
| 9558 | let bd = u32::from(bit_depth(fctx)); |
| 9559 | let max_val = (1i32 << bd) - 1; |
| 9560 | let mut colors = [0u16; 8]; |
| 9561 | let n_cached = cached.len(); |
| 9562 | if n_cached < n { |
| 9563 | let mut transmitted = Vec::with_capacity(n - n_cached); |
| 9564 | let first = dec.literal(bd) as u16; |
| 9565 | transmitted.push(first); |
| 9566 | if n_cached + 1 < n { |
| 9567 | let min_bits = bd - 3; |
| 9568 | let mut bits = min_bits + dec.literal(2); |
| 9569 | let mut range = (1i32 << bd) - i32::from(first) - 1; |
| 9570 | for _ in (n_cached + 1)..n { |
| 9571 | let delta = dec.literal(bits) as i32 + 1; |
| 9572 | let prev = *transmitted.last().unwrap(); |
| 9573 | let val = (i32::from(prev) + delta).clamp(0, max_val) as u16; |
| 9574 | range -= i32::from(val) - i32::from(prev); |
| 9575 | bits = bits.min(ceil_log2(range.max(0) as u32)); |
| 9576 | transmitted.push(val); |
| 9577 | } |
| 9578 | } |
| 9579 | let merged = merge_colors(&transmitted, &cached); |
| 9580 | colors[..n].copy_from_slice(&merged); |
| 9581 | } else { |
| 9582 | colors[..n].copy_from_slice(&cached[..n]); |
| 9583 | } |
| 9584 | colors |
| 9585 | } |
| 9586 | |
| 9587 | /// `read_palette_colors_uv` (decodemv.c:509), at the sequence's real |
| 9588 | /// `bit_depth` (see [`read_palette_colors_y`]): U reads |
| 9589 | /// exactly [`read_palette_colors_y`]'s cache/delta scheme (against the U-only |
| 9590 | /// cache, `range` starting at `255 - prev` with no `-1`, and each raw `delta` |
| 9591 | /// unbiased by `+1` -- both differences from Y's own reader, matching the C |
| 9592 | /// side by side). V never uses a cache: a leading bit picks either `n` raw |
| 9593 | /// literals, or a first raw value plus `n-1` signed deltas that wrap mod 256 |
| 9594 | /// rather than clamp. |
| 9595 | fn read_palette_colors_uv(dec: &mut SymbolDecoder, n: usize, cache: &[u16], fctx: &crate::decode::FrameCtx) -> ([u16; 8], [u16; 8]) { |
| 9596 | let mut cached = Vec::with_capacity(n); |
| 9597 | for &c in cache { |
| 9598 | if cached.len() >= n { |
| 9599 | break; |
| 9600 | } |
| 9601 | if dec.literal(1) == 1 { |
| 9602 | cached.push(c); |
| 9603 | } |
| 9604 | } |
| 9605 | let bd = u32::from(bit_depth(fctx)); |
| 9606 | let max_val = 1i32 << bd; |
| 9607 | let mut u_colors = [0u16; 8]; |
| 9608 | let n_cached = cached.len(); |
| 9609 | if n_cached < n { |
| 9610 | let mut transmitted = Vec::with_capacity(n - n_cached); |
| 9611 | let first = dec.literal(bd) as u16; |
| 9612 | transmitted.push(first); |
| 9613 | if n_cached + 1 < n { |
| 9614 | let min_bits = bd - 3; |
| 9615 | let mut bits = min_bits + dec.literal(2); |
| 9616 | let mut range = max_val - i32::from(first); |
| 9617 | for _ in (n_cached + 1)..n { |
| 9618 | let delta = dec.literal(bits) as i32; |
| 9619 | let prev = *transmitted.last().unwrap(); |
| 9620 | let val = (i32::from(prev) + delta).clamp(0, max_val - 1) as u16; |
| 9621 | range -= i32::from(val) - i32::from(prev); |
| 9622 | bits = bits.min(ceil_log2(range.max(0) as u32)); |
| 9623 | transmitted.push(val); |
| 9624 | } |
| 9625 | } |
| 9626 | let merged = merge_colors(&transmitted, &cached); |
| 9627 | u_colors[..n].copy_from_slice(&merged); |
| 9628 | } else { |
| 9629 | u_colors[..n].copy_from_slice(&cached[..n]); |
| 9630 | } |
| 9631 | let mut v_colors = [0u16; 8]; |
| 9632 | if dec.literal(1) == 1 { |
| 9633 | let min_bits_v = bd - 4; |
| 9634 | let bits = min_bits_v + dec.literal(2); |
| 9635 | v_colors[0] = dec.literal(bd) as u16; |
| 9636 | for i in 1..n { |
| 9637 | let mut delta = dec.literal(bits) as i32; |
| 9638 | if delta != 0 && dec.literal(1) == 1 { |
| 9639 | delta = -delta; |
| 9640 | } |
| 9641 | let mut val = i32::from(v_colors[i - 1]) + delta; |
| 9642 | if val < 0 { |
| 9643 | val += max_val; |
| 9644 | } |
| 9645 | if val >= max_val { |
| 9646 | val -= max_val; |
| 9647 | } |
| 9648 | v_colors[i] = val as u16; |
| 9649 | } |
| 9650 | } else { |
| 9651 | for i in 0..n { |
| 9652 | v_colors[i] = dec.literal(bd) as u16; |
| 9653 | } |
| 9654 | } |
| 9655 | (u_colors, v_colors) |
| 9656 | } |
| 9657 | |
| 9658 | /// `av1_palette_color_index_context_lookup` (entropymode.c:889): maps a |
| 9659 | /// `NUM_PALETTE_NEIGHBORS`-weighted score hash (`0..=8`) to the real |
| 9660 | /// `0..=4` context, `-1` for hashes no real neighbour configuration produces. |
| 9661 | const PALETTE_COLOR_INDEX_CONTEXT_LOOKUP: [i32; 9] = [-1, -1, 0, -1, -1, 4, 3, 2, 1]; |
| 9662 | |
| 9663 | /// `av1_get_palette_color_index_context` (entropymode.c:893): the colour-index |
| 9664 | /// map's own per-pixel context and colour-order permutation, from the |
| 9665 | /// left/up-left/up already-decoded neighbours (weights `[2, 1, 2]`, top-3 |
| 9666 | /// selection sort). `map`'s stride is `side` (this decoder never crops a |
| 9667 | /// palette block's map to a narrower plane edge, [`decode_color_index_map`]'s |
| 9668 | /// own doc). Returns `(ctx, color_order)`; `color_order[symbol]` is the |
| 9669 | /// actual colour index to store. |
| 9670 | pub(crate) fn palette_color_index_context( |
| 9671 | map: &[u8], |
| 9672 | side: usize, |
| 9673 | row: usize, |
| 9674 | col: usize, |
| 9675 | n: usize, |
| 9676 | ) -> (usize, [u8; 8]) { |
| 9677 | let left = if col >= 1 { Some(map[row * side + col - 1]) } else { None }; |
| 9678 | let up_left = if col >= 1 && row >= 1 { |
| 9679 | Some(map[(row - 1) * side + col - 1]) |
| 9680 | } else { |
| 9681 | None |
| 9682 | }; |
| 9683 | let up = if row >= 1 { Some(map[(row - 1) * side + col]) } else { None }; |
| 9684 | let mut scores = [0i32; 8]; |
| 9685 | for (neighbour, weight) in [(left, 2), (up_left, 1), (up, 2)] { |
| 9686 | if let Some(v) = neighbour { |
| 9687 | scores[v as usize] += weight; |
| 9688 | } |
| 9689 | } |
| 9690 | let mut color_order = [0u8; 8]; |
| 9691 | for i in 0..8 { |
| 9692 | color_order[i] = i as u8; |
| 9693 | } |
| 9694 | for i in 0..3 { |
| 9695 | let mut max = scores[i]; |
| 9696 | let mut max_idx = i; |
| 9697 | for j in (i + 1)..n { |
| 9698 | if scores[j] > max { |
| 9699 | max = scores[j]; |
| 9700 | max_idx = j; |
| 9701 | } |
| 9702 | } |
| 9703 | if max_idx != i { |
| 9704 | let max_score = scores[max_idx]; |
| 9705 | let max_color = color_order[max_idx]; |
| 9706 | for k in (i + 1..=max_idx).rev() { |
| 9707 | scores[k] = scores[k - 1]; |
| 9708 | color_order[k] = color_order[k - 1]; |
| 9709 | } |
| 9710 | scores[i] = max_score; |
| 9711 | color_order[i] = max_color; |
| 9712 | } |
| 9713 | } |
| 9714 | let hash = scores[0] + scores[1] * 2 + scores[2] * 2; |
| 9715 | let ctx = PALETTE_COLOR_INDEX_CONTEXT_LOOKUP[hash as usize]; |
| 9716 | (ctx as usize, color_order) |
| 9717 | } |
| 9718 | |
| 9719 | /// spec 5.11.50 `palette_tokens` / `av1_get_block_dimensions` (blockd.h:1512): |
| 9720 | /// the ON-SCREEN part of a block at `(mi_r, mi_c)`, in luma pixels. A block |
| 9721 | /// cut by the frame's bottom or right mi edge codes its colour-index map only |
| 9722 | /// over this part and replicates it outward -- reading the full `bw x bh` grid |
| 9723 | /// consumed symbols libaom never wrote and desynced the rest of the tile |
| 9724 | /// (lane-svtinter; class map-read-past-frame-edge). |
| 9725 | fn palette_onscreen( |
| 9726 | mi_r: usize, |
| 9727 | mi_c: usize, |
| 9728 | bw: usize, |
| 9729 | bh: usize, |
| 9730 | fctx: &crate::decode::FrameCtx, |
| 9731 | ) -> (usize, usize) { |
| 9732 | // lane-palw: the ENCODER's own in-process reconstruct runs on a |
| 9733 | // `FrameCtx::for_encoder`, which never sees `set_segmentation` -- both |
| 9734 | // frame encoders publish the header's mi dims onto it themselves |
| 9735 | // (`encode.rs`, beside their `true_width`/`true_height`) so that this |
| 9736 | // clamp and `tile::write_color_index_map`'s now agree there too. |
| 9737 | let (mi_rows, mi_cols) = fctx.seg_mi_dims.with(|c| c.get()); |
| 9738 | palette_onscreen_dims(mi_r, mi_c, bw, bh, (mi_rows, mi_cols)) |
| 9739 | } |
| 9740 | |
| 9741 | /// [`palette_onscreen`] off explicit mi dims, which is how the WRITER reaches |
| 9742 | /// it (`tile::write_palette_syntax`, armed per frame): the two sides must |
| 9743 | /// compute the same on-screen extent or the decoder reads a different number |
| 9744 | /// of symbols than the encoder wrote. |
| 9745 | pub(crate) fn palette_onscreen_dims( |
| 9746 | mi_r: usize, |
| 9747 | mi_c: usize, |
| 9748 | bw: usize, |
| 9749 | bh: usize, |
| 9750 | (mi_rows, mi_cols): (usize, usize), |
| 9751 | ) -> (usize, usize) { |
| 9752 | ( |
| 9753 | bw.min(mi_cols.saturating_sub(mi_c) * MI), |
| 9754 | bh.min(mi_rows.saturating_sub(mi_r) * MI), |
| 9755 | ) |
| 9756 | } |
| 9757 | |
| 9758 | /// [`palette_onscreen`] carried into the chroma plane: `>> subsampling` plus |
| 9759 | /// the SAME sub-8 bump the caller's own `(bw >> ss).max(4)` applies to the |
| 9760 | /// block dimensions (`is_chroma_sub8_x/y`, blockd.h:1533) -- both must move |
| 9761 | /// together or a cut 8-wide block reads its chroma map at the wrong width. |
| 9762 | /// lane-av1-444: the shifts are the sequence's own subsampling (a 4:4:4 |
| 9763 | /// intra-in-inter palette read its chroma map at the 4:2:0 extent and |
| 9764 | /// panicked feeding the 4:2:0-sized prediction to a full-size reconstruct). |
| 9765 | pub(crate) fn palette_onscreen_uv( |
| 9766 | luma: (usize, usize), |
| 9767 | on: (usize, usize), |
| 9768 | chroma: (usize, usize), |
| 9769 | ss: (usize, usize), |
| 9770 | ) -> (usize, usize) { |
| 9771 | ( |
| 9772 | (on.0 >> ss.0) + (chroma.0 - (luma.0 >> ss.0)), |
| 9773 | (on.1 >> ss.1) + (chroma.1 - (luma.1 >> ss.1)), |
| 9774 | ) |
| 9775 | } |
| 9776 | |
| 9777 | /// `decode_color_map_tokens` (detokenize.c:25): the palette-Y colour-index |
| 9778 | /// map's wavefront decode over a `side x side` grid, read over the on-screen |
| 9779 | /// `on_side x on_side` corner (see [`palette_onscreen`]). |
| 9780 | fn decode_color_index_map( |
| 9781 | dec: &mut SymbolDecoder, |
| 9782 | cdfs: &mut Cdfs, |
| 9783 | n: usize, |
| 9784 | side: usize, |
| 9785 | on: (usize, usize), |
| 9786 | uv: bool, |
| 9787 | ) -> Vec<u8> { |
| 9788 | decode_color_index_map_wh(dec, cdfs, n, side, side, on, uv) |
| 9789 | } |
| 9790 | |
| 9791 | /// [`decode_color_index_map`], generalised to a true `bw`x`bh` rect strip |
| 9792 | /// (lane-palette2 r4): `decode_color_map_tokens`'s own wavefront diagonal |
| 9793 | /// (`row = i - j`, `col = j`) walked over `cols = bw`, `rows = bh` |
| 9794 | /// independently rather than one shared `side` -- the square call above is |
| 9795 | /// just `bw == bh`. |
| 9796 | fn decode_color_index_map_wh( |
| 9797 | dec: &mut SymbolDecoder, |
| 9798 | cdfs: &mut Cdfs, |
| 9799 | n: usize, |
| 9800 | bw: usize, |
| 9801 | bh: usize, |
| 9802 | // `(onscreenWidth, onscreenHeight)` in this plane's own units. |
| 9803 | on: (usize, usize), |
| 9804 | uv: bool, |
| 9805 | ) -> Vec<u8> { |
| 9806 | let trace = crate::envflags::env_flag!("EC_AV1_TRACE"); |
| 9807 | let (on_bw, on_bh) = (on.0.clamp(1, bw), on.1.clamp(1, bh)); |
| 9808 | if on_bw < bw || on_bh < bh { |
| 9809 | hit!(PALETTE_ONSCREEN_CUT_HITS); |
| 9810 | } |
| 9811 | let mut map = vec![0u8; bw * bh]; |
| 9812 | if trace { |
| 9813 | let (rng, _) = dec.debug_state(); |
| 9814 | eprintln!("EC_PAL row=0 col=0 ctx=-1 n={n} rng={rng}"); |
| 9815 | } |
| 9816 | map[0] = read_uniform(dec, n) as u8; |
| 9817 | if trace { |
| 9818 | let (rng, _) = dec.debug_state(); |
| 9819 | eprintln!("EC_PAL_VAL row=0 col=0 color_idx={} rng={rng}", map[0]); |
| 9820 | } |
| 9821 | for i in 1..(on_bw + on_bh - 1) { |
| 9822 | let j_hi = i.min(on_bw - 1); |
| 9823 | let j_lo = i.saturating_sub(on_bh - 1); |
| 9824 | for j in (j_lo..=j_hi).rev() { |
| 9825 | let row = i - j; |
| 9826 | let col = j; |
| 9827 | let (ctx, color_order) = palette_color_index_context(&map, bw, row, col, n); |
| 9828 | if trace { |
| 9829 | let (rng, _) = dec.debug_state(); |
| 9830 | let row_cdf = if uv { |
| 9831 | &cdfs.palette_uv_color_index[n - 2][ctx] |
| 9832 | } else { |
| 9833 | &cdfs.palette_y_color_index[n - 2][ctx] |
| 9834 | }; |
| 9835 | eprintln!("EC_PAL row={row} col={col} ctx={ctx} n={n} cdf0={} cnt={} rng={rng}", row_cdf[0], row_cdf[n]); |
| 9836 | } |
| 9837 | let symbol = if uv { |
| 9838 | dec.symbol(&mut cdfs.palette_uv_color_index[n - 2][ctx][..=n]) |
| 9839 | } else { |
| 9840 | dec.symbol(&mut cdfs.palette_y_color_index[n - 2][ctx][..=n]) |
| 9841 | }; |
| 9842 | map[row * bw + col] = color_order[symbol]; |
| 9843 | if trace { |
| 9844 | let (rng, _) = dec.debug_state(); |
| 9845 | eprintln!("EC_PAL_VAL row={row} col={col} color_idx={symbol} rng={rng}"); |
| 9846 | } |
| 9847 | } |
| 9848 | } |
| 9849 | palette_replicate(&mut map, bw, bh, on); |
| 9850 | map |
| 9851 | } |
| 9852 | |
| 9853 | /// `decode_color_map_tokens`'s own two extension loops (detokenize.c:71): the |
| 9854 | /// last on-screen column then the last on-screen row are replicated over the |
| 9855 | /// off-screen remainder, so the prediction still covers the whole block. The |
| 9856 | /// ENCODER runs this over its searched map too (`encode.rs`'s palette arms), |
| 9857 | /// so the pixels it prices and reconstructs are the ones the on-screen |
| 9858 | /// symbols it writes really decode to. |
| 9859 | pub(crate) fn palette_replicate(map: &mut [u8], bw: usize, bh: usize, on: (usize, usize)) { |
| 9860 | let (on_bw, on_bh) = (on.0.clamp(1, bw), on.1.clamp(1, bh)); |
| 9861 | for row in 0..on_bh { |
| 9862 | let last = map[row * bw + on_bw - 1]; |
| 9863 | map[row * bw + on_bw..row * bw + bw].fill(last); |
| 9864 | } |
| 9865 | for row in on_bh..bh { |
| 9866 | map.copy_within((on_bh - 1) * bw..on_bh * bw, row * bw); |
| 9867 | } |
| 9868 | } |
| 9869 | |
| 9870 | /// [`read_intra_mode`] for a true `bw`x`bh` rect strip (lane-intradisp r1, |
| 9871 | /// palette wired lane-palette2 r4): identical except the `use_filter_intra` |
| 9872 | /// size class comes from [`filter_intra_size_class_rect`] instead of the |
| 9873 | /// square-only [`filter_intra_size_class`], and the palette read (when |
| 9874 | /// `allow_screen_content_tools`) uses [`palette_bsize_ctx_wh`]/ |
| 9875 | /// [`decode_color_index_map_wh`] instead of their square-only counterparts -- |
| 9876 | /// every other symbol this reads (`skip`, `y_mode`, `angle_delta`, `uv_mode`, |
| 9877 | /// `cfl_alphas`) is indexed by mode or neighbour state, never by block size. |
| 9878 | #[allow(clippy::too_many_arguments)] |
| 9879 | fn read_intra_mode_rect( |
| 9880 | dec: &mut SymbolDecoder, |
| 9881 | cdfs: &mut Cdfs, |
| 9882 | above_mode: usize, |
| 9883 | left_mode: usize, |
| 9884 | cfl: bool, |
| 9885 | // lane-r14: `is_chroma_reference` -- false only for a strip one mi tall |
| 9886 | // (or wide) that is NOT the odd one of its 4:2:0 pair, where libaom reads |
| 9887 | // no `uv_mode`/`cfl_alpha`/`angle_delta_uv`/`palette_uv` at all (the pair's |
| 9888 | // chroma is coded once, on the closing strip). Every other caller is a |
| 9889 | // block big enough to own its chroma and passes `true`. |
| 9890 | has_chroma: bool, |
| 9891 | bw: usize, |
| 9892 | bh: usize, |
| 9893 | enable_filter_intra: bool, |
| 9894 | skip_ctx: usize, |
| 9895 | allow_screen_content_tools: bool, |
| 9896 | // As [`read_intra_mode`]'s own `palette`/`palette_uv_cache` params: |
| 9897 | // `Some((palette_mode_ctx, color_cache))` reconstructs a real palette-Y |
| 9898 | // block here, `None` still reads (and refuses on use) the symbol at ctx 0. |
| 9899 | palette: Option<(usize, &[u16])>, |
| 9900 | palette_uv_cache: &[u16], |
| 9901 | mi_r: usize, |
| 9902 | mi_c: usize, fctx: &crate::decode::FrameCtx, |
| 9903 | ) -> Result<( |
| 9904 | bool, |
| 9905 | usize, |
| 9906 | i32, |
| 9907 | usize, |
| 9908 | i32, |
| 9909 | Option<(i32, i32)>, |
| 9910 | Option<usize>, |
| 9911 | Option<PaletteY>, |
| 9912 | Option<PaletteUv>, |
| 9913 | // lane-av1-intrabc: the block vector, `Some` exactly when this block's |
| 9914 | // `use_intrabc` symbol came out 1 -- libaom's `read_intrabc_info` returns |
| 9915 | // straight out of `read_intra_frame_mode_info` then, so every later field |
| 9916 | // of the tuple is at its default (mode/uv_mode DC, no angle, no palette, |
| 9917 | // no filter intra). A caller that can reconstruct intrabc at its shape |
| 9918 | // takes this and calls [`decode_intrabc_rect`]; one that cannot refuses by |
| 9919 | // name with the same string it always did. |
| 9920 | Option<(i32, i32)>, |
| 9921 | )> { |
| 9922 | // lane-av1mono: libaom guards every chroma symbol read with |
| 9923 | // `!cm->seq_params->monochrome && xd->is_chroma_ref` (`decodemv.c:933`), |
| 9924 | // so a monochrome block codes no `uv_mode`/`cfl`/`angle_delta_uv`/ |
| 9925 | // `palette_uv` even on a chroma-reference strip. Fold the plane count |
| 9926 | // into the same `has_chroma` this reader already gates on. |
| 9927 | let has_chroma = has_chroma && !mono(fctx); |
| 9928 | let ec_istep = crate::envflags::env_flag!("EC_TRACE_MODE_STEP"); |
| 9929 | if ec_istep { |
| 9930 | // Mirrors the oracle's own `EC_IMODE mi_row=.. mi_col=.. rng=..` line |
| 9931 | // (decodemv.c:818), printed at the same point: before the first |
| 9932 | // mode-info symbol of the block, so a cross-decoder range ladder can |
| 9933 | // be keyed on the BLOCK, not just on the symbol sequence. |
| 9934 | 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); |
| 9935 | } |
| 9936 | macro_rules! istep { |
| 9937 | ($name:literal, $val:expr) => { |
| 9938 | if ec_istep { |
| 9939 | let (rng, _) = dec.debug_state(); |
| 9940 | eprintln!( |
| 9941 | "EC_ISTEP mi_row={mi_r} mi_col={mi_c} name={} val={} rng={rng}", |
| 9942 | $name, $val |
| 9943 | ); |
| 9944 | } |
| 9945 | }; |
| 9946 | } |
| 9947 | // lane-sbrect10 r1: libaom `is_cfl_allowed` (blockd.h) is |
| 9948 | // `block_size_wide <= 32 && block_size_high <= 32` -- enforce it HERE, at |
| 9949 | // the one point every rect caller routes through, so no call site can |
| 9950 | // offer the 14-symbol CFL alphabet on a 64-axis block (class |
| 9951 | // wrong-alphabet-same-value: same value, wrong interval, silent desync). |
| 9952 | let cfl = if cfl && bw.max(bh) > 32 { |
| 9953 | hit!(NOCFL_UV_MODE_HITS); |
| 9954 | false |
| 9955 | } else { |
| 9956 | cfl |
| 9957 | }; |
| 9958 | // lane-intra14 r1: on the INTER block path everything up to and including |
| 9959 | // `is_inter` was already read by [`decode_inter_block`], so this reader |
| 9960 | // starts at the mode syntax with the `skip` that caller read. |
| 9961 | let inter = fctx.intra_in_inter_mode.with(std::cell::Cell::get); |
| 9962 | let skip = if let Some((_, skip)) = inter { |
| 9963 | skip |
| 9964 | } else { |
| 9965 | // Same `intra_segment_id` placement as the square reader above. |
| 9966 | let (seg_w_mi, seg_h_mi) = (bw / 4, bh / 4); |
| 9967 | if seg_id_pre_skip(fctx) { |
| 9968 | intra_segment_id(dec, cdfs, mi_r, mi_c, seg_w_mi, seg_h_mi, false, fctx); |
| 9969 | } |
| 9970 | let skip = dec.symbol(&mut cdfs.skip[skip_ctx]) != 0; |
| 9971 | istep!("skip", skip as i32); |
| 9972 | if !seg_id_pre_skip(fctx) { |
| 9973 | intra_segment_id(dec, cdfs, mi_r, mi_c, seg_w_mi, seg_h_mi, skip, fctx); |
| 9974 | } |
| 9975 | maybe_read_cdef_idx(dec, mi_r, mi_c, (bw / MI, bh / MI), skip, fctx); |
| 9976 | istep!("cdef", 0); |
| 9977 | // A HORZ/VERT rect strip is never the whole superblock (`bw`/`bh` never |
| 9978 | // both 64 here -- see this fn's own doc), so `is_whole_sb` is always |
| 9979 | // `false`. |
| 9980 | maybe_read_delta_q(dec, cdfs, mi_r, mi_c, false, skip, fctx); |
| 9981 | maybe_read_delta_lf(dec, cdfs, mi_r, mi_c, false, skip, fctx); |
| 9982 | istep!("dq", 0); |
| 9983 | // `read_intrabc_info` (spec 5.11.13, libaom `decodemv.c:892`, right after |
| 9984 | // `skip`/`segment_id`/`cdef`/`delta_q` and before `mbmi->mode`): the |
| 9985 | // `use_intrabc` symbol is read for EVERY intra block of an `allow_intrabc` |
| 9986 | // frame -- `av1_allow_intrabc` is a frame-level test with no shape term, so |
| 9987 | // rect strips read it exactly like square leaves ([`read_intra_mode`], |
| 9988 | // [`read_intra_mode_sub8`]). Missing it here desynced the arithmetic |
| 9989 | // decoder at the FIRST symbol of the first rect block of every |
| 9990 | // `--tune-content=screen` key frame. Reconstruction of a real intrabc |
| 9991 | // block at this shape is out of scope, so the flag is consumed and |
| 9992 | // refused by name when it fires. |
| 9993 | if allow_intrabc_frame(fctx) { |
| 9994 | let use_intrabc = dec.symbol(&mut cdfs.intrabc) != 0; |
| 9995 | hit!(RECT_INTRABC_READS); |
| 9996 | istep!("intrabc", use_intrabc as i32); |
| 9997 | if use_intrabc { |
| 9998 | hit!(INTRABC_HITS); |
| 9999 | // libaom `read_intrabc_info`: the DV read, then an immediate |
| 10000 | // return out of the mode-info read -- no `y_mode`, no `uv_mode`, |
| 10001 | // no angle deltas, no palette, no filter intra. The caller owns |
| 10002 | // the reconstruction at its own shape. |
| 10003 | let dv = read_intrabc_dv(dec, cdfs, mi_r, mi_c, (bw / MI, bh / MI), fctx); |
| 10004 | return Ok(( |
| 10005 | skip, |
| 10006 | DC_PRED, |
| 10007 | 0, |
| 10008 | DC_PRED, |
| 10009 | 0, |
| 10010 | None, |
| 10011 | None, |
| 10012 | None, |
| 10013 | None, |
| 10014 | Some(dv), |
| 10015 | )); |
| 10016 | } |
| 10017 | } |
| 10018 | skip |
| 10019 | }; |
| 10020 | // libaom `read_intra_block_mode_info` (decodemv.c:1189) vs the key |
| 10021 | // frame's `read_intra_frame_mode_info` (decodemv.c:1065 `read_intra_mode` |
| 10022 | // off `kf_y_mode_cdf[above][left]`). |
| 10023 | let mode = if let Some((size_group, _)) = inter { |
| 10024 | dec.symbol(&mut cdfs.y_mode[size_group]) |
| 10025 | } else { |
| 10026 | let above_ctx = INTRA_MODE_CTX[above_mode]; |
| 10027 | let left_ctx = INTRA_MODE_CTX[left_mode]; |
| 10028 | dec.symbol(&mut cdfs.kf_y_mode[above_ctx][left_ctx]) |
| 10029 | }; |
| 10030 | istep!("mode", mode as i32); |
| 10031 | let angle_delta_y = if (V_PRED..=D67_PRED).contains(&mode) { |
| 10032 | read_angle_delta(dec, &mut cdfs.angle_delta[mode - V_PRED]) |
| 10033 | } else { |
| 10034 | 0 |
| 10035 | }; |
| 10036 | istep!("angle_y", angle_delta_y); |
| 10037 | // lane-sb128c r3 (class sweep): libaom `read_intra_mode_uv(.., is_cfl_allowed(xd), ..)` |
| 10038 | // and `is_cfl_allowed` (blockd.h) is `block_size_wide[bsize] <= 32 && |
| 10039 | // block_size_high[bsize] <= 32` on the LUMA bsize. Deciding the alphabet |
| 10040 | // here, from the block's own size, is the single point every caller routes |
| 10041 | // through -- a caller passing `true` for a >32 strip used to pick the |
| 10042 | // 14-symbol CFL CDF, same value but a different range (class |
| 10043 | // wrong-alphabet-same-value). |
| 10044 | let cfl = cfl && cfl_allowed_px(bw, bh, fctx); |
| 10045 | let uv_mode = if !has_chroma { |
| 10046 | DC_PRED |
| 10047 | } else if cfl { |
| 10048 | dec.symbol(&mut cdfs.uv_mode_cfl[mode]) |
| 10049 | } else { |
| 10050 | dec.symbol(&mut cdfs.uv_mode_no_cfl[mode]) |
| 10051 | }; |
| 10052 | istep!("uv_mode", uv_mode as i32); |
| 10053 | let alpha = if has_chroma && cfl && uv_mode == UV_CFL_PRED { |
| 10054 | Some(read_cfl_alphas(dec, cdfs)) |
| 10055 | } else { |
| 10056 | None |
| 10057 | }; |
| 10058 | if has_chroma && (9..=12).contains(&uv_mode) { |
| 10059 | hit!(SMOOTH_UV_HITS); |
| 10060 | } |
| 10061 | let angle_delta_uv = if has_chroma && (V_PRED..=D67_PRED).contains(&uv_mode) { |
| 10062 | hit!(DIRECTIONAL_UV_HITS); |
| 10063 | read_angle_delta(dec, &mut cdfs.angle_delta[uv_mode - V_PRED]) |
| 10064 | } else { |
| 10065 | 0 |
| 10066 | }; |
| 10067 | istep!("angle_uv", angle_delta_uv); |
| 10068 | if angle_delta_uv != 0 { |
| 10069 | hit!(UV_ANGLE_DELTA_HITS); |
| 10070 | } |
| 10071 | // `read_palette_mode_info` (spec 5.11.13), rect-generalised |
| 10072 | // (lane-palette2 r4): same bsize-ctx-gated read as [`read_intra_mode`], |
| 10073 | // just keyed by [`palette_bsize_ctx_wh`] instead of the square-only |
| 10074 | // [`palette_bsize_ctx`]. Order matches spec/libaom exactly: mode/size/ |
| 10075 | // colours only here, the colour-index maps deferred past `filter_intra` |
| 10076 | // (see [`read_intra_mode`]'s own comment on why -- `av1_visit_palette` |
| 10077 | // runs from the caller, after the whole mode-info read). |
| 10078 | let mut palette_y_pending: Option<(usize, [u16; 8])> = None; |
| 10079 | let mut palette_uv_pending: Option<(usize, [u16; 8], [u16; 8])> = None; |
| 10080 | if allow_screen_content_tools |
| 10081 | && let Some(bsize_ctx) = palette_bsize_ctx_wh(bw, bh) |
| 10082 | { |
| 10083 | // r6: this is the exact site the removed blanket refusal used to |
| 10084 | // guard -- reached for EVERY rect strip in a screen-content frame, |
| 10085 | // independent of whether the block ends up using a palette. |
| 10086 | hit!(RECT_SCREEN_CONTENT_HITS); |
| 10087 | let (mode_ctx, cache) = palette.unwrap_or((0, &[])); |
| 10088 | let use_palette_y = |
| 10089 | mode == DC_PRED && dec.symbol(&mut cdfs.palette_y_mode[bsize_ctx][mode_ctx]) != 0; |
| 10090 | if use_palette_y { |
| 10091 | let n = 2 + dec.symbol(&mut cdfs.palette_y_size[bsize_ctx]); |
| 10092 | let colors = read_palette_colors_y(dec, n, cache, fctx); |
| 10093 | palette_y_pending = Some((n, colors)); |
| 10094 | } |
| 10095 | let palette_uv_mode_ctx = usize::from(use_palette_y); |
| 10096 | // `read_palette_mode_info`'s own `xd->is_chroma_ref` guard. |
| 10097 | if has_chroma |
| 10098 | && uv_mode == DC_PRED |
| 10099 | && dec.symbol(&mut cdfs.palette_uv_mode[palette_uv_mode_ctx]) != 0 |
| 10100 | { |
| 10101 | let n = 2 + dec.symbol(&mut cdfs.palette_uv_size[bsize_ctx]); |
| 10102 | let (u_colors, v_colors) = read_palette_colors_uv(dec, n, palette_uv_cache, fctx); |
| 10103 | palette_uv_pending = Some((n, u_colors, v_colors)); |
| 10104 | } |
| 10105 | } |
| 10106 | let mut filter_intra = None; |
| 10107 | if crate::envflags::env_flag!("EC_AV1_TRACE") { |
| 10108 | let (rng, _) = dec.debug_state(); |
| 10109 | eprintln!( |
| 10110 | "TRACE_RECT_PREFI mode={mode} uv_mode={uv_mode} enable_filter_intra={} \ |
| 10111 | class={:?} rng={rng}", |
| 10112 | enable_filter_intra as i32, |
| 10113 | filter_intra_size_class_rect(bw, bh) |
| 10114 | ); |
| 10115 | } |
| 10116 | // As the square path: `av1_filter_intra_allowed` excludes a palette-Y |
| 10117 | // block (reconintra.h:77), so no `use_filter_intra` symbol exists there. |
| 10118 | if mode == DC_PRED |
| 10119 | && enable_filter_intra |
| 10120 | && palette_y_pending.is_none() |
| 10121 | && let Some(class) = filter_intra_size_class_rect(bw, bh) |
| 10122 | { |
| 10123 | let use_filter_intra = dec.symbol(&mut cdfs.filter_intra[class]) != 0; |
| 10124 | if crate::envflags::env_flag!("EC_AV1_TRACE") { |
| 10125 | eprintln!("TRACE_RECT_USEFI value={}", use_filter_intra as i32); |
| 10126 | } |
| 10127 | istep!("use_filter_intra", use_filter_intra as i32); |
| 10128 | if use_filter_intra { |
| 10129 | hit!(FILTER_INTRA_HITS); |
| 10130 | let fi_mode = dec.symbol(&mut cdfs.filter_intra_mode); |
| 10131 | istep!("filter_intra_mode", fi_mode as i32); |
| 10132 | filter_intra = Some(fi_mode); |
| 10133 | } |
| 10134 | } |
| 10135 | // `av1_visit_palette`: plane 0 (Y) then plane 1 (chroma, shared U/V map), |
| 10136 | // same order as [`read_intra_mode`]'s own square path. |
| 10137 | let palette_y = palette_y_pending.map(|(n, colors)| { |
| 10138 | let map = |
| 10139 | decode_color_index_map_wh(dec, cdfs, n, bw, bh, palette_onscreen(mi_r, mi_c, bw, bh, fctx), false); |
| 10140 | hit!(PALETTE_HITS); |
| 10141 | hit!(PALETTE_RECT_HITS); |
| 10142 | PaletteY { size: n, colors, map } |
| 10143 | }); |
| 10144 | let palette_uv = palette_uv_pending.map(|(n, u_colors, v_colors)| { |
| 10145 | // `av1_get_plane_block_size(bsize, 1, 1)` (`ss_size_lookup`), NOT a |
| 10146 | // plain halving: BLOCK_16X4 subsamples to BLOCK_8X4 and BLOCK_4X16 to |
| 10147 | // BLOCK_4X8, both floored at the 4-px transform. Halving the short |
| 10148 | // axis to 2 read HALF the colour-index symbols of a 1:4 strip's |
| 10149 | // chroma palette and desynced the tile from there. |
| 10150 | let (cw, ch) = ((bw >> ss_x(fctx)).max(4), (bh >> ss_y(fctx)).max(4)); |
| 10151 | let on = palette_onscreen_uv((bw, bh), palette_onscreen(mi_r, mi_c, bw, bh, fctx), (cw, ch), (ss_x(fctx), ss_y(fctx))); |
| 10152 | let map = decode_color_index_map_wh(dec, cdfs, n, cw, ch, on, true); |
| 10153 | hit!(PALETTE_UV_HITS); |
| 10154 | PaletteUv { size: n, u_colors, v_colors, map } |
| 10155 | }); |
| 10156 | Ok(( |
| 10157 | skip, |
| 10158 | mode, |
| 10159 | angle_delta_y, |
| 10160 | uv_mode, |
| 10161 | angle_delta_uv, |
| 10162 | alpha, |
| 10163 | filter_intra, |
| 10164 | palette_y, |
| 10165 | palette_uv, |
| 10166 | None, |
| 10167 | )) |
| 10168 | } |
| 10169 | |
| 10170 | /// Stamps a 1:4 strip's palette state into the mi-granular above/left arrays, |
| 10171 | /// at the strip's REAL mi position. `av1_get_palette_cache` reads |
| 10172 | /// `xd->above_mbmi`/`xd->left_mbmi`, i.e. the mi cell above/left of the |
| 10173 | /// block, so each strip owns its own rows and columns and no aliasing rule |
| 10174 | /// is needed (lane-kf900 r6). |
| 10175 | #[allow(clippy::too_many_arguments)] |
| 10176 | fn record_strip_palette( |
| 10177 | n: &mut Neighbours, |
| 10178 | mi: (usize, usize), |
| 10179 | bw: usize, |
| 10180 | bh: usize, |
| 10181 | y_size: usize, |
| 10182 | y_colors: [u16; 8], |
| 10183 | uv_size: usize, |
| 10184 | uv_colors: [u16; 8], |
| 10185 | ) { |
| 10186 | let (r, c) = mi; |
| 10187 | // lane-t900 r23 rung: every publication of a block's palette state into |
| 10188 | // the neighbour bands (`EC_PALPUB=1`) -- the rung that names which decode |
| 10189 | // path left a stale `left_palette_size` behind when |
| 10190 | // `av1_get_palette_mode_ctx` disagrees with libaom's. |
| 10191 | if crate::envflags::env_flag!("EC_PALPUB") { |
| 10192 | eprintln!("EC_PALPUB strip mi={r},{c} bw={bw} bh={bh} y={y_size} uv={uv_size}"); |
| 10193 | } |
| 10194 | // lane-kf900 r6: the palette side bands are mi-granular now, so a strip |
| 10195 | // writes its OWN span on BOTH axes -- the old `last` split-axis rule only |
| 10196 | // existed because the 16px [`SUB`] cell held one slot for all the strips |
| 10197 | // of a HORZ_4/VERT_4 and leaf 0 zeroing the other axis threw away the |
| 10198 | // real neighbour leaf 1 reads. |
| 10199 | { |
| 10200 | for cell in 0..(bw / MI).max(1) { |
| 10201 | if let Some(e) = n.above_palette_size.get_mut(c + cell) { |
| 10202 | *e = y_size as u8; |
| 10203 | n.above_palette_uv_size[c + cell] = uv_size as u8; |
| 10204 | // lane-publish2, see [`Neighbours::record_palette_y_rect`]: |
| 10205 | // colours of a size-0 neighbour are never read. |
| 10206 | if y_size > 0 { |
| 10207 | n.above_palette_colors[c + cell] = y_colors; |
| 10208 | } |
| 10209 | if uv_size > 0 { |
| 10210 | n.above_palette_uv_colors[c + cell] = uv_colors; |
| 10211 | } |
| 10212 | } |
| 10213 | } |
| 10214 | } |
| 10215 | { |
| 10216 | for cell in 0..(bh / MI).max(1) { |
| 10217 | if n.left_palette_size.get(r + cell).is_none() { |
| 10218 | break; |
| 10219 | } |
| 10220 | n.left_palette_size[r + cell] = y_size as u8; |
| 10221 | n.left_palette_uv_size[r + cell] = uv_size as u8; |
| 10222 | if y_size > 0 { |
| 10223 | n.left_palette_colors[r + cell] = y_colors; |
| 10224 | } |
| 10225 | if uv_size > 0 { |
| 10226 | n.left_palette_uv_colors[r + cell] = uv_colors; |
| 10227 | } |
| 10228 | } |
| 10229 | } |
| 10230 | } |
| 10231 | |
| 10232 | /// [`cfl_ac_q3`] for a true `bw`x`bh` rect strip (lane-intradisp r1). |
| 10233 | fn cfl_ac_q3_rect(y: &PlaneBuf<'_>, px: usize, py: usize, bw: usize, bh: usize) -> Vec<i32> { |
| 10234 | cfl_ac_q3_at(px, py, bw, bh, |lx, ly| i32::from(y.data[ly * y.width + lx])) |
| 10235 | } |
| 10236 | |
| 10237 | /// Everything a rect strip already read out of the stream before its luma |
| 10238 | /// transform units are decoded -- [`decode_rect_split`]'s own mode bundle |
| 10239 | /// (nine values that would otherwise be nine more parameters). |
| 10240 | struct RectStripModes { |
| 10241 | skip: bool, |
| 10242 | mode: usize, |
| 10243 | angle_delta_y: i32, |
| 10244 | uv_predict_mode: usize, |
| 10245 | angle_delta_uv: i32, |
| 10246 | alpha: Option<(i32, i32)>, |
| 10247 | filter_intra: Option<usize>, |
| 10248 | smooth_neighbor: bool, |
| 10249 | smooth_neighbor_uv: bool, |
| 10250 | /// lane-palette2 r9: the strip's palette-Y prediction (`bw`x`bh`, already |
| 10251 | /// mapped through the colour table), sliced per transform unit below -- |
| 10252 | /// palette prediction reads no edge pixels, so a unit's slice of the |
| 10253 | /// whole-block map IS its prediction. Without this a palette strip whose |
| 10254 | /// transform splits predicted DC and came out flat grey while the entropy |
| 10255 | /// stream stayed in sync (the coefficients are read either way). |
| 10256 | palette_y: Option<Vec<u16>>, |
| 10257 | /// As `palette_y`, for the un-split chroma transform (U, V). |
| 10258 | palette_uv: Option<(Vec<u16>, Vec<u16>)>, |
| 10259 | } |
| 10260 | |
| 10261 | /// The transform unit size (w, h) of a `bw`x`bh` intra block at tx `depth` |
| 10262 | /// -- libaom `depth_to_tx_size`, which walks `sub_tx_size_map` `depth` times |
| 10263 | /// from `max_txsize_rect_lookup[bsize]`. Every entry of that map halves the |
| 10264 | /// LONGER side of a rectangle and both sides of a square, so a 1:4 strip's |
| 10265 | /// first step is a 2:1 rect unit (`TX_32X8 -> TX_16X8`, `TX_64X16 -> |
| 10266 | /// TX_32X16`) and only the second reaches a square -- the reason |
| 10267 | /// `bw.min(bh) >> (depth - 1)` (correct for a 2:1 strip, where step one is |
| 10268 | /// already square) mis-tiled 1:4 strips. |
| 10269 | fn depth_to_tx_wh( |
| 10270 | bw: usize, |
| 10271 | bh: usize, |
| 10272 | depth: usize, |
| 10273 | fctx: &crate::decode::FrameCtx, |
| 10274 | ) -> (usize, usize) { |
| 10275 | // lane-lossless: TX_4X4 whatever the strip's own size and depth (libaom |
| 10276 | // `read_tx_size`: the lossless early return comes before |
| 10277 | // `max_txsize_rect_lookup`), and no `tx_depth` symbol was read to get here. |
| 10278 | if lossless(fctx) { |
| 10279 | return (4, 4); |
| 10280 | } |
| 10281 | let (mut w, mut h) = (bw.min(64), bh.min(64)); |
| 10282 | for _ in 0..depth { |
| 10283 | match w.cmp(&h) { |
| 10284 | std::cmp::Ordering::Greater => w /= 2, |
| 10285 | std::cmp::Ordering::Less => h /= 2, |
| 10286 | std::cmp::Ordering::Equal => { |
| 10287 | w /= 2; |
| 10288 | h /= 2; |
| 10289 | } |
| 10290 | } |
| 10291 | } |
| 10292 | (w, h) |
| 10293 | } |
| 10294 | |
| 10295 | /// Decodes the planes of one `bw`x`bh` HORZ/VERT intra strip whose luma |
| 10296 | /// transform is SPLIT (`tx_depth != 0`) -- lane-rectsplit r1, the case every |
| 10297 | /// rect path here refused by name until now ("per-unit rect prediction is not |
| 10298 | /// ported"). |
| 10299 | /// |
| 10300 | /// `depth_to_tx_size` (libaom `blockd.h`) walks `sub_tx_size_map` `depth` |
| 10301 | /// times from the block's own `max_txsize_rect_lookup` entry, and for a 2:1 |
| 10302 | /// strip the very first step already lands on a SQUARE transform |
| 10303 | /// (`sub_tx_size_map[TX_32X16] == TX_16X16`, `[TX_64X32] == TX_32X32`, and |
| 10304 | /// every further step halves a square) -- so each unit is square and reads |
| 10305 | /// through the ordinary square [`read_plane`]. What is genuinely rectangular |
| 10306 | /// here is the *tiling* (`bw/tx` by `bh/tx` units, not `n` by `n` as |
| 10307 | /// [`decode_block`]'s square split loop assumes) and the chroma plane, whose |
| 10308 | /// own transform is `av1_get_max_uv_txsize` of the whole strip and is never |
| 10309 | /// split by luma's depth. |
| 10310 | /// |
| 10311 | /// Each unit predicts off whatever the earlier units of this same block |
| 10312 | /// already reconstructed (spec 5.11.36 `predict_and_reconstruct_intra_block` |
| 10313 | /// inside `av1_foreach_transformed_block`, raster order), which is exactly |
| 10314 | /// what predicting the whole strip in one shot got wrong. |
| 10315 | #[allow(clippy::too_many_arguments)] |
| 10316 | fn decode_rect_split( |
| 10317 | dec: &mut SymbolDecoder, |
| 10318 | cdfs: &mut Cdfs, |
| 10319 | neighbours: &mut Neighbours, |
| 10320 | at_mi: (usize, usize), |
| 10321 | bw: usize, |
| 10322 | bh: usize, |
| 10323 | // The transform unit's own width and height in pixels. Square for every |
| 10324 | // 2:1 strip from the first split on, but a 1:4 strip's FIRST split is |
| 10325 | // still a 2:1 RECT unit (`sub_tx_size_map[TX_32X8] == TX_16X8`, |
| 10326 | // `[TX_64X16] == TX_32X16`) -- see [`depth_to_tx_wh`]. |
| 10327 | tx_w: usize, |
| 10328 | tx_h: usize, |
| 10329 | m: &RectStripModes, |
| 10330 | y: &mut PlaneBuf<'static>, |
| 10331 | u: &mut PlaneBuf<'static>, |
| 10332 | v: &mut PlaneBuf<'static>, |
| 10333 | base_q_idx: u8, |
| 10334 | reduced_tx_set: bool, fctx: &crate::decode::FrameCtx, |
| 10335 | ) -> Result<()> { |
| 10336 | let (mi_r, mi_c) = at_mi; |
| 10337 | let (px, py) = (mi_c * MI, mi_r * MI); |
| 10338 | let (cpx, cpy) = (px >> ss_x(fctx), py >> ss_y(fctx)); |
| 10339 | let (chroma_w, chroma_h) = (bw >> ss_x(fctx), bh >> ss_y(fctx)); |
| 10340 | // lane-av1-422blockhalf: at ss (1,0) the tall/wide 1:2 and 1:4 strips' |
| 10341 | // chroma plane block is BLOCK_INVALID (`av1_ss_size_lookup[...][1][0]`, |
| 10342 | // blockd.h -- the 4:2:2 column maps BLOCK_8X16/16X32/32X64/64X128 and |
| 10343 | // BLOCK_8X32/16X64, plus 4-wide BLOCK_4X16, to BLOCK_INVALID), so |
| 10344 | // `av1_get_max_uv_txsize` indexes `max_txsize_rect_lookup` OUT OF BOUNDS |
| 10345 | // and the pinned Release oracle byte there is TX_4X8 (the lane-av1-422m |
| 10346 | // measurement). The plane is still coded -- `decode_token_recon_block` |
| 10347 | // steps it in that (4, 8) unit shape -- so the strip's chroma tiles into |
| 10348 | // 4x8 rect units, plane-major. |
| 10349 | let chroma_422_oob = ss_x(fctx) == 1 |
| 10350 | && ss_y(fctx) == 0 |
| 10351 | && matches!( |
| 10352 | (bw, bh), |
| 10353 | (8, 16) | (16, 32) | (32, 64) | (64, 128) | (8, 32) | (16, 64) |
| 10354 | ); |
| 10355 | // The chroma transform is picked before any symbol is read, so an |
| 10356 | // unsupported chroma shape refuses before this strip desyncs the tile. |
| 10357 | let chroma: Option<(TxbSet, &[u16])> = match (chroma_w, chroma_h) { |
| 10358 | (16, 8) => Some((TxbSet::ChromaRect16x8, &SCAN_16X8[..])), |
| 10359 | (8, 16) => Some((TxbSet::ChromaRect16x8, &SCAN_8X16[..])), |
| 10360 | (32, 16) => Some((TxbSet::ChromaRect32x16, &SCAN_32X16[..])), |
| 10361 | (16, 32) => Some((TxbSet::ChromaRect32x16, &SCAN_16X32[..])), |
| 10362 | // The 1:4 and sub-16 strips (lane-rectchroma r1 added the 32x8/8x32 |
| 10363 | // rows, lane-rectsplitx r1 the 16x4/4x16 ones). `get_txsize_entropy_ctx` |
| 10364 | // squares a rect transform down to a CDF set by |
| 10365 | // `(wide_log2 + high_log2 + 1) >> 1`: TX_32X8 -> the 16x16 set, |
| 10366 | // TX_16X4 -> the 8x8 set, TX_8X4 -> the 8x8 set -- exactly the sets |
| 10367 | // the UNSPLIT 1:4 (`decode_block_rect4`) and 16x8 (`decode_leaf_rect`) |
| 10368 | // paths already prove for these same shapes. |
| 10369 | (32, 8) => Some((TxbSet::Chroma16, &SCAN_32X8[..])), |
| 10370 | (8, 32) => Some((TxbSet::Chroma16, &SCAN_8X32[..])), |
| 10371 | (16, 4) => Some((TxbSet::Chroma8, &SCAN_16X4[..])), |
| 10372 | (4, 16) => Some((TxbSet::Chroma8, &SCAN_4X16[..])), |
| 10373 | (8, 4) => Some((TxbSet::ChromaRect8x4, &SCAN_8X4[..])), |
| 10374 | (4, 8) => Some((TxbSet::ChromaRect8x4, &SCAN_4X8[..])), |
| 10375 | _ => None, |
| 10376 | }; |
| 10377 | // A 4:4:4 64-axis strip's chroma plane block is larger than one adjusted |
| 10378 | // UV transform (TX_64X32 adjusts to TX_32X32). Those sizes are tiled |
| 10379 | // below; everything else still needs a single-unit table. |
| 10380 | // lane-av1-422: a 4:2:2 strip's chroma plane keeps the strip's HEIGHT and |
| 10381 | // halves only the width, so a rect luma strip codes a SQUARE chroma block |
| 10382 | // (16x8 luma -> 8x8 chroma) -- the square rows below. At 4:2:0 and 4:4:4 a |
| 10383 | // rect strip's chroma is itself rect, so the square rows are unreachable |
| 10384 | // there and cannot change their results. |
| 10385 | // lane-av1-422blockhalf: the tiled arms below handle every shape this |
| 10386 | // strip can carry (including the 4:2:2 BLOCK_INVALID planes), so the |
| 10387 | // diagnostic names only the shapes that would actually refuse -- it used |
| 10388 | // to fire for every tiled square too (the parent lane's `chroma=8x8` |
| 10389 | // rows) and sent the next lane chasing handled shapes. |
| 10390 | let chroma_tiled = chroma_422_oob |
| 10391 | || matches!( |
| 10392 | (chroma_w, chroma_h), |
| 10393 | (64, 32) | (32, 64) | (64, 16) | (16, 64) | (32, 32) | (16, 16) | (8, 8) |
| 10394 | ); |
| 10395 | if !m.skip { |
| 10396 | if let Some(i) = match (chroma_w, chroma_h) { |
| 10397 | (32, 8) => Some(0), |
| 10398 | (8, 32) => Some(1), |
| 10399 | (8, 4) => Some(2), |
| 10400 | (4, 8) => Some(3), |
| 10401 | _ => None, |
| 10402 | } { |
| 10403 | hit_do! { |
| 10404 | RECT_SPLIT_CHROMA_SHAPE_HITS.with(|c| { |
| 10405 | let mut hits = c.get(); |
| 10406 | hits[i] += 1; |
| 10407 | c.set(hits); |
| 10408 | }); |
| 10409 | } |
| 10410 | } |
| 10411 | if !m.skip && chroma.is_none() && !chroma_tiled { |
| 10412 | // Diagnostic for the next caller that lands here (class |
| 10413 | // refusal-names-a-correlate: the message names the tool, not the |
| 10414 | // shape); the refusal below keeps its inventory-pinned wording. |
| 10415 | eprintln!( |
| 10416 | "EC_RECTCHROMA_GAP luma={bw}x{bh} tx={tx_w}x{tx_h} \ |
| 10417 | chroma={chroma_w}x{chroma_h}" |
| 10418 | ); |
| 10419 | } |
| 10420 | } |
| 10421 | // lane-rectsplit r2 (verifier finding): with both callers wired |
| 10422 | // (`decode_block_rect`'s 32x16/16x32 and `decode_block_rect64`'s |
| 10423 | // 64x32/32x64) every shape that reaches here has a table above, so this |
| 10424 | // arm is UNREACHABLE today. It stays as the shape guard for the next |
| 10425 | // caller -- a new strip size would otherwise hit the `expect` below and |
| 10426 | // panic instead of refusing by name -- and so stays in |
| 10427 | // `refusal_inventory::REFUSALS`. |
| 10428 | if !m.skip && chroma.is_none() && !chroma_tiled { |
| 10429 | return Err(unsupported( |
| 10430 | "a coded HORZ/VERT strip whose chroma transform has no rect coefficient tables here", |
| 10431 | )); |
| 10432 | } |
| 10433 | hit!(RECT_SPLIT_TX_HITS); |
| 10434 | if bw.max(bh) == 64 && bw / tx_w > 1 && bh / tx_h > 1 { |
| 10435 | hit!(RECT_SPLIT_SB_INTERIOR_TU_HITS); |
| 10436 | } |
| 10437 | // LUMA: `bw/tx` x `bh/tx` square transform units, raster order. |
| 10438 | // A square unit reads through the ordinary square [`read_plane`]; a rect |
| 10439 | // one (a 1:4 strip's first split) reads through [`read_coeffs_rect`] with |
| 10440 | // the very tables the unsplit 16x8/32x16 paths use. |
| 10441 | let luma_rect: Option<(TxbSet, &[u16])> = match (tx_w, tx_h) { |
| 10442 | (w, h) if w == h => None, |
| 10443 | (16, 8) | (8, 16) => Some(( |
| 10444 | if reduced_tx_set { |
| 10445 | TxbSet::LumaRect16x8 |
| 10446 | } else { |
| 10447 | TxbSet::LumaRect16x8Set1 |
| 10448 | }, |
| 10449 | if tx_w == 16 { &SCAN_16X8[..] } else { &SCAN_8X16[..] }, |
| 10450 | )), |
| 10451 | (32, 16) => Some((TxbSet::LumaRect32x16, &SCAN_32X16[..])), |
| 10452 | (16, 32) => Some((TxbSet::LumaRect32x16, &SCAN_16X32[..])), |
| 10453 | // lane-rect64port r1: the un-split TX_64X32/TX_32X64 of a 64-level |
| 10454 | // strip, read through the `luma_64corner` arm below instead of a rect |
| 10455 | // table -- a 64-length axis codes only its low 32 coefficients |
| 10456 | // (`av1_get_max_eob` / the zero-out of `av1_get_adjusted_tx_size`), so |
| 10457 | // the coded unit is the 32x32 corner [`decode_block_rect64`]'s own |
| 10458 | // depth-0 arm already reads on the key-frame path. |
| 10459 | // |
| 10460 | // lane-sub8x4 r3: WITNESS FOUND, so this is no longer behind |
| 10461 | // `EC_RECT64_SPLIT`. r1's "no witness" was true only while the |
| 10462 | // sub-8x8 intra leaves still refused: with those lifted (r2) the |
| 10463 | // first 50 frame OBUs of the 10-bit 3840x1608 stream's head decode |
| 10464 | // all 33 frames pixel-exact against ffmpeg while this arm fires 90x |
| 10465 | // 64x32 + 5x 32x64 -- pinned as `fixtures/reelb_rect64_intra16x4_witness.obu` |
| 10466 | // and gated by `a_10bit_film_frames_with_rect64_corner_tus_decode_pixel_exact`. |
| 10467 | // lane-wit16x4 r1 re-measured that fixture on the merged tip: this arm |
| 10468 | // fires 64x32=148 32x64=123 (the 90/5 above were counted out of the |
| 10469 | // frame-33 desync lane-frame36 r2 fixed), all 33 frames pixel-exact. |
| 10470 | (64, 32) | (32, 64) => None, |
| 10471 | _ => { |
| 10472 | return Err(unsupported(format!( |
| 10473 | "a split intra strip whose transform unit is {tx_w}x{tx_h} (no luma \ |
| 10474 | coefficient tables for that shape here)" |
| 10475 | ))) |
| 10476 | } |
| 10477 | }; |
| 10478 | // The `None` above that is NOT the square arm. |
| 10479 | let luma_64corner = tx_w != tx_h; |
| 10480 | // lane-rectsplitx r1, MEASURED: the SQUARE-unit walk below is pixel-exact |
| 10481 | // on a real stream (the pinned 16x8 filter-intra fixture decodes its whole |
| 10482 | // frame exactly, two sub-16 split strips in it), but the RECT-unit arm -- |
| 10483 | // a 1:4 strip's depth-1 TX_16X8/TX_32X16 -- mismatched ffmpeg on the |
| 10484 | // 32-level 1:4 band fixture (8-bit, cq 12, seed 42). The tables and the |
| 10485 | // per-unit rect walk stay wired above/below; the round that finds that |
| 10486 | // defect deletes this refusal and nothing else. |
| 10487 | let luma_set = txbset_for(tx_w.min(tx_h), reduced_tx_set); |
| 10488 | let luma_scan = default_scan(tx_w.min(tx_h)); |
| 10489 | let zero_tu = vec![0i32; tx_w * tx_h]; |
| 10490 | // The strip's OWN edge availability, the fallback for a transform unit |
| 10491 | // whose top-right/bottom-left pixels fall outside the strip. |
| 10492 | let block_reach = Reach::of_rect(bw, bh, px, py, y.width, y.height, fctx); |
| 10493 | for tu_row in 0..bh / tx_h { |
| 10494 | for tu_col in 0..bw / tx_w { |
| 10495 | let tu_mi = (mi_r + tu_row * (tx_h / MI), mi_c + tu_col * (tx_w / MI)); |
| 10496 | let (tu_px, tu_py) = (px + tu_col * tx_w, py + tu_row * tx_h); |
| 10497 | let (col_off, row_off) = (tu_col * tx_w, tu_row * tx_h); |
| 10498 | // lane-hgkf r1 (ported from lane-golomb 791d87a, unmerged): |
| 10499 | // libaom clips `av1_foreach_transformed_block_in_plane` to |
| 10500 | // `max_blocks_wide/high` (`mb_to_right_edge`/`mb_to_bottom_edge`, |
| 10501 | // i.e. `mi_cols * 4` / `mi_rows * 4`): a transform unit whose |
| 10502 | // TOP-LEFT sample falls outside the frame is never coded. We read |
| 10503 | // coefficients for those phantom units and desynced the stream at |
| 10504 | // the very next symbol. |
| 10505 | if tu_px >= y.true_width || tu_py >= y.true_height { |
| 10506 | continue; |
| 10507 | } |
| 10508 | // `has_top_right`/`has_bottom_left` (libaom `reconintra.c`) for a |
| 10509 | // transform unit INSIDE a block, which is a different rule from |
| 10510 | // the standalone-block table [`Reach::of`] applies -- and the |
| 10511 | // reason a depth-2 superblock strip came out one sample off |
| 10512 | // before this: `Reach::of(tx, ..)` answered as if the unit were |
| 10513 | // its own block at that superblock position. |
| 10514 | // * top-right: if the unit's right neighbour is still inside the |
| 10515 | // strip the pixels are already reconstructed (`col_off + |
| 10516 | // tx_wide < plane_bw`); otherwise only the strip's own top row |
| 10517 | // of units can reach past it, through the block-level answer. |
| 10518 | // * bottom-left: a unit that is not in the strip's left column |
| 10519 | // has none (`col_off > 0` returns 0); inside the strip |
| 10520 | // (`row_off + tx_high < plane_bh`) they are the left |
| 10521 | // neighbour's, already reconstructed; the last row falls back |
| 10522 | // to the block-level answer. |
| 10523 | let tu_reach = Reach::of_tu(bw, bh, col_off, row_off, tx_w, tx_h, block_reach); |
| 10524 | // The unit's own window on the block's palette prediction. |
| 10525 | if let Some(buf) = &m.palette_y { |
| 10526 | let tu_buf: Vec<u16> = (0..tx_h) |
| 10527 | .flat_map(|i| { |
| 10528 | let base = (row_off + i) * bw + col_off; |
| 10529 | buf[base..base + tx_w].iter().copied() |
| 10530 | }) |
| 10531 | .collect(); |
| 10532 | set_palette_pred(tu_buf, fctx); |
| 10533 | } |
| 10534 | if m.skip { |
| 10535 | push_intra_rect(0, |
| 10536 | tu_px, |
| 10537 | tu_py, |
| 10538 | tx_w, |
| 10539 | tx_h, |
| 10540 | m.mode, |
| 10541 | m.angle_delta_y, |
| 10542 | tu_reach, |
| 10543 | &zero_tu, |
| 10544 | None, |
| 10545 | m.filter_intra, |
| 10546 | m.smooth_neighbor, fctx, |
| 10547 | ); |
| 10548 | neighbours.record_mi_luma_rect(tu_mi, tx_w, tx_h, &zero_tu); |
| 10549 | } else if luma_64corner && luma_rect.is_none() { |
| 10550 | // The un-split 64-level luma transform: `read_coeffs` on the |
| 10551 | // 32x32 CDF set [`TxbSet::Luma64`] with `default_scan(TX32)` |
| 10552 | // and the real rect shape passed through for |
| 10553 | // `av1_nz_map_ctx_offset` -- byte for byte |
| 10554 | // [`decode_block_rect64`]'s depth-0 read, which the key-frame |
| 10555 | // 64-level strip gate already proves. `tx_type` is not coded |
| 10556 | // (`av1_get_ext_tx_set_type` resolves EXT_TX_SET_DCTONLY once |
| 10557 | // the square-up size is TX_64X64), and this unit covers its |
| 10558 | // whole block, so `get_txb_ctx` gives txb_skip ctx 0 flat. |
| 10559 | if crate::envflags::env_flag!("EC_RECT64TU") { |
| 10560 | eprintln!("EC_RECT64TU mi={tu_mi:?} tx={tx_w}x{tx_h} mode={}", m.mode); |
| 10561 | } |
| 10562 | hit_do! { |
| 10563 | RECT64_CORNER_TU_HITS.with(|c| { |
| 10564 | let mut v = c.get(); |
| 10565 | v[usize::from(tx_h > tx_w)] += 1; |
| 10566 | c.set(v); |
| 10567 | }); |
| 10568 | } |
| 10569 | let tu_around = neighbours.around_mi_rect(tu_mi, tx_w, tx_h)[0]; |
| 10570 | let mut coding = cdfs.txb(TxbSet::Luma64, fi_tx_row(m.mode, m.filter_intra)); |
| 10571 | let (corner, tu_tx_type) = read_coeffs( |
| 10572 | dec, |
| 10573 | &mut coding, |
| 10574 | default_scan(TX32), |
| 10575 | 0, |
| 10576 | dc_sign_ctx(tu_around.2), |
| 10577 | TxType::DctDct, |
| 10578 | Some((tx_w, tx_h)), |
| 10579 | )?; |
| 10580 | let (cw, ch) = (tx_w.min(TX32), tx_h.min(TX32)); |
| 10581 | let mut tu_grid = vec![0i32; tx_w * tx_h]; |
| 10582 | for row in 0..ch { |
| 10583 | tu_grid[row * tx_w..][..cw].copy_from_slice(&corner[row * cw..][..cw]); |
| 10584 | } |
| 10585 | let residual = dequant_and_inverse_typed_wh( |
| 10586 | &tu_grid, |
| 10587 | tx_w, |
| 10588 | tx_h, |
| 10589 | crate::decode::bit_depth(fctx), |
| 10590 | block_q_idx(fctx), |
| 10591 | plane_q_delta(0, fctx).0, |
| 10592 | plane_q_delta(0, fctx).1, |
| 10593 | tu_tx_type, |
| 10594 | block_iqmatrix(fctx, 0, tx_w, tx_h, tu_tx_type), |
| 10595 | ); |
| 10596 | push_intra_rect(0, |
| 10597 | tu_px, |
| 10598 | tu_py, |
| 10599 | tx_w, |
| 10600 | tx_h, |
| 10601 | m.mode, |
| 10602 | m.angle_delta_y, |
| 10603 | tu_reach, |
| 10604 | &residual, |
| 10605 | None, |
| 10606 | m.filter_intra, |
| 10607 | m.smooth_neighbor, fctx, |
| 10608 | ); |
| 10609 | neighbours.record_mi_luma_rect(tu_mi, tx_w, tx_h, &tu_grid); |
| 10610 | } else if let Some((rect_set, rect_scan)) = luma_rect { |
| 10611 | // The rect-unit twin of the square arm below: same contexts, |
| 10612 | // same order, through the rect coefficient reader. The |
| 10613 | // `tx_type` CDF row of a filter-intra block is |
| 10614 | // `fimode_to_intradir[filter_intra_mode]`, never the block's |
| 10615 | // own (always DC_PRED) mode -- [`read_plane`] applies that |
| 10616 | // for the square arm, so this one must too. |
| 10617 | let tu_around = neighbours.around_mi_rect(tu_mi, tx_w, tx_h)[0]; |
| 10618 | // libaom `get_txb_ctx`: a LUMA transform that covers its whole |
| 10619 | // block (`plane_bsize == txsize_to_bsize[tx_size]`, i.e. the |
| 10620 | // unsplit depth-0 strip) reads txb_skip ctx 0 flat -- the |
| 10621 | // neighbour-magnitude table is for a unit SMALLER than its |
| 10622 | // block. [`decode_block_rect`]'s own unsplit read passes a |
| 10623 | // literal 0 for the same reason; this arm applied the table |
| 10624 | // unconditionally and desynced every depth-0 rect strip that |
| 10625 | // reached it (lane-intrarect r2: ctx 5 where aomdec read 0). |
| 10626 | let tu_skip_ctx = if tx_w == bw && tx_h == bh { |
| 10627 | 0 |
| 10628 | } else { |
| 10629 | neighbours.luma_skip_ctx_rect(tu_mi, tx_w / MI, tx_h / MI) |
| 10630 | }; |
| 10631 | let mut coding = cdfs.txb(rect_set, fi_tx_row(m.mode, m.filter_intra)); |
| 10632 | let (tu_grid, tu_tx_type) = read_coeffs_rect( |
| 10633 | dec, |
| 10634 | &mut coding, |
| 10635 | rect_scan, |
| 10636 | tx_w, |
| 10637 | tx_h, |
| 10638 | tu_skip_ctx, |
| 10639 | dc_sign_ctx(tu_around.2), |
| 10640 | TxType::DctDct, |
| 10641 | )?; |
| 10642 | let residual = dequant_and_inverse_typed_wh( |
| 10643 | &tu_grid, |
| 10644 | tx_w, |
| 10645 | tx_h, |
| 10646 | crate::decode::bit_depth(fctx), |
| 10647 | block_q_idx(fctx), |
| 10648 | plane_q_delta(0, fctx).0, |
| 10649 | plane_q_delta(0, fctx).1, |
| 10650 | tu_tx_type, |
| 10651 | block_iqmatrix(fctx, 0, tx_w, tx_h, tu_tx_type), |
| 10652 | ); |
| 10653 | push_intra_rect(0, |
| 10654 | tu_px, |
| 10655 | tu_py, |
| 10656 | tx_w, |
| 10657 | tx_h, |
| 10658 | m.mode, |
| 10659 | m.angle_delta_y, |
| 10660 | tu_reach, |
| 10661 | &residual, |
| 10662 | None, |
| 10663 | m.filter_intra, |
| 10664 | m.smooth_neighbor, fctx, |
| 10665 | ); |
| 10666 | neighbours.record_mi_luma_rect(tu_mi, tx_w, tx_h, &tu_grid); |
| 10667 | } else { |
| 10668 | let tu_around = neighbours.around_mi(tu_mi, tx_w)[0]; |
| 10669 | // This unit is smaller than the block it sits in, so |
| 10670 | // `txb_skip_ctx` is the neighbour-magnitude table, not the |
| 10671 | // lone-TU 0 (spec `get_txb_ctx_general`). |
| 10672 | let tu_skip_ctx = neighbours.luma_skip_ctx(tu_mi, tx_w / MI); |
| 10673 | let tu_grid = read_plane( |
| 10674 | dec, |
| 10675 | cdfs, |
| 10676 | luma_set, |
| 10677 | &luma_scan, |
| 10678 | 0, |
| 10679 | tu_around, |
| 10680 | m.mode, |
| 10681 | m.mode, |
| 10682 | m.angle_delta_y, |
| 10683 | tu_reach, |
| 10684 | y, |
| 10685 | tu_px, |
| 10686 | tu_py, |
| 10687 | tx_w, |
| 10688 | tx_w, |
| 10689 | base_q_idx, |
| 10690 | None, |
| 10691 | m.filter_intra, |
| 10692 | Some(tu_skip_ctx), |
| 10693 | m.smooth_neighbor, fctx, |
| 10694 | )?; |
| 10695 | neighbours.record_mi_luma(tu_mi, tx_w, &tu_grid); |
| 10696 | } |
| 10697 | } |
| 10698 | } |
| 10699 | // CHROMA: one un-split rect transform for the whole strip. |
| 10700 | // lane-lossless2: except on a lossless frame, where it is a raster of 4x4 |
| 10701 | // units -- each stamps its own neighbour context as it is read, and the |
| 10702 | // whole-block `record_split_luma_rect_mi` below would overwrite all of |
| 10703 | // them with the LAST unit's state (class `override-slot-on-one-arm`: the |
| 10704 | // next block then read `dc_sign_ctx` 1 where libaom reads 0). They are |
| 10705 | // collected here and replayed after that record. |
| 10706 | let mut ll_chroma_units: Vec<((usize, usize), usize, Grid, usize, usize)> = Vec::new(); |
| 10707 | // The chroma_tiled arm below stamps each unit's coefficient context |
| 10708 | // immediately (so the NEXT unit reads it), then the whole-strip |
| 10709 | // `record_split_luma_rect_mi` rewrites every chroma cell with ONE |
| 10710 | // whole-strip state and the per-unit DC signs and levels are lost |
| 10711 | // (class `override-slot-on-one-arm`). The units are collected here and |
| 10712 | // replayed after that record, the way the lossless arm's |
| 10713 | // `ll_chroma_units` and `decode_inter_block`'s `mu_chroma_units` tail |
| 10714 | // already do. |
| 10715 | let mut tiled_chroma_units: Vec<((usize, usize), usize, Grid, usize, usize)> = Vec::new(); |
| 10716 | let ac = m.alpha.map(|_| cfl_src_rect(px, py, bw, bh)); |
| 10717 | let reach = block_reach; |
| 10718 | let (u_grid, v_grid) = if mono(fctx) { |
| 10719 | // lane-av1mono: a monochrome frame codes no chroma coefficients on |
| 10720 | // any block (`plane < av1_num_planes == 1`), so the whole chroma |
| 10721 | // section -- the skipped-strip pushes, the lossless raster and the |
| 10722 | // un-split `read_coeffs_rect` below -- is bypassed. |
| 10723 | let zero = Grid::Zero(chroma_w * chroma_h); |
| 10724 | (zero.clone(), zero) |
| 10725 | } else if m.skip { |
| 10726 | let zero = Grid::Zero(chroma_w * chroma_h); |
| 10727 | if chroma_422_oob { |
| 10728 | // lane-av1-422blockhalf: libaom predicts an INTRA strip's chroma |
| 10729 | // per transform unit even when the strip is skipped |
| 10730 | // (`decode_token_recon_block`'s per-TU plane walk runs before the |
| 10731 | // skip guard), so the BLOCK_INVALID plane predicts as (4, 8) |
| 10732 | // units -- one whole-plane push here would ask the intra |
| 10733 | // predictor for a 1:8 rect it never needs. |
| 10734 | let (uw, uh) = (4usize, 8usize); |
| 10735 | for plane_idx in 1..=2 { |
| 10736 | if let Some((ub, vb)) = &m.palette_uv { |
| 10737 | let buf = if plane_idx == 1 { ub } else { vb }; |
| 10738 | set_palette_pred(buf.clone(), fctx); |
| 10739 | } |
| 10740 | for cu_row in 0..chroma_h / uh { |
| 10741 | for cu_col in 0..chroma_w / uw { |
| 10742 | push_intra_rect( |
| 10743 | plane_idx, |
| 10744 | cpx + cu_col * uw, |
| 10745 | cpy + cu_row * uh, |
| 10746 | uw, |
| 10747 | uh, |
| 10748 | m.uv_predict_mode, |
| 10749 | m.angle_delta_uv, |
| 10750 | reach, |
| 10751 | &ZERO_RESIDUAL[..uw * uh], |
| 10752 | m.alpha.zip(ac.clone()).map(|((au, av), ac)| { |
| 10753 | ( |
| 10754 | if plane_idx == 1 { au } else { av }, |
| 10755 | ac, |
| 10756 | ) |
| 10757 | }), |
| 10758 | None, |
| 10759 | m.smooth_neighbor_uv, fctx, |
| 10760 | ); |
| 10761 | } |
| 10762 | } |
| 10763 | } |
| 10764 | } else { |
| 10765 | if let Some((ub, _)) = &m.palette_uv { |
| 10766 | set_palette_pred(ub.clone(), fctx); |
| 10767 | } |
| 10768 | push_intra_rect(1, |
| 10769 | cpx, cpy, chroma_w, chroma_h, m.uv_predict_mode, m.angle_delta_uv, reach, &zero, |
| 10770 | m.alpha.zip(ac).map(|((au, _), ac)| (au, ac)), None, m.smooth_neighbor_uv, fctx, |
| 10771 | ); |
| 10772 | if let Some((_, vb)) = &m.palette_uv { |
| 10773 | set_palette_pred(vb.clone(), fctx); |
| 10774 | } |
| 10775 | push_intra_rect(2, |
| 10776 | cpx, cpy, chroma_w, chroma_h, m.uv_predict_mode, m.angle_delta_uv, reach, &zero, |
| 10777 | m.alpha.zip(ac).map(|((_, av), ac)| (av, ac)), None, m.smooth_neighbor_uv, fctx, |
| 10778 | ); |
| 10779 | } |
| 10780 | (zero.clone(), zero) |
| 10781 | } else if lossless(fctx) { |
| 10782 | // lane-lossless: the strip's chroma plane block is coded as 4x4 |
| 10783 | // transform units too (libaom `read_tx_size` returns TX_4X4 for every |
| 10784 | // plane), PLANE-MAJOR inside the block -- U's units first, then V's |
| 10785 | // (`decode_token_recon_block`'s mu-chunk loop, measured against |
| 10786 | // aomdec's `EC_TRACE_COEFF` on a `-crf 0` stream: plane=1, plane=1, |
| 10787 | // plane=2, plane=2 for a 16x8 strip). `get_txb_ctx` adds its `+10` |
| 10788 | // offset (our `+3` row base) whenever the plane block is bigger than |
| 10789 | // the transform. |
| 10790 | let scan4 = default_scan(4); |
| 10791 | let offset = (chroma_w > 4 || chroma_h > 4).then_some(3); |
| 10792 | let (cw_n, ch_n) = (chroma_w / 4, chroma_h / 4); |
| 10793 | // lane-av1-llband: one 4x4 chroma unit's extent in LUMA pixels -- 4 |
| 10794 | // chroma px scaled by the plane's own subsampling. The hardcoded 8 |
| 10795 | // was the 4:2:0 unit's span; at 4:4:4 a unit is 4x4 LUMA px (one mi |
| 10796 | // cell), and the 8-px step made every unit past the first column/row |
| 10797 | // read its `txb_skip` context at the WRONG mi (two cells over) and |
| 10798 | // stamp a 2-cell span with one unit's state -- the polluted above-band |
| 10799 | // cell behind a 16x8 leaf's U bc=2 unit of the 444 lossless key frame |
| 10800 | // (measured: oracle `EC_DBGCTX` reads 1-cell spans at 1-mi steps; |
| 10801 | // ours read `above[28..29]` where the oracle read `above[26]`). |
| 10802 | let span_x = 4 << ss_x(fctx); |
| 10803 | let span_y = 4 << ss_y(fctx); |
| 10804 | let mut last = [Grid::Zero(16), Grid::Zero(16)]; |
| 10805 | for plane_idx in 1..=2 { |
| 10806 | for cu_row in 0..ch_n { |
| 10807 | for cu_col in 0..cw_n { |
| 10808 | let cu_mi = ( |
| 10809 | mi_r + cu_row * (span_y / MI), |
| 10810 | mi_c + cu_col * (span_x / MI), |
| 10811 | ); |
| 10812 | let cu_around = neighbours.around_mi_rect(cu_mi, span_x, span_y); |
| 10813 | let cu_reach = tu_reach( |
| 10814 | bw, bh, |
| 10815 | cu_col * span_x, cu_row * span_y, span_x, |
| 10816 | block_reach, px, py, y.width, y.height, fctx, |
| 10817 | ); |
| 10818 | let (cu_x, cu_y) = (cpx + cu_col * 4, cpy + cu_row * 4); |
| 10819 | if let Some((ub, vb)) = &m.palette_uv { |
| 10820 | let pbuf = if plane_idx == 1 { ub } else { vb }; |
| 10821 | set_palette_pred( |
| 10822 | palette_window(pbuf, chroma_w, cu_col * 4, cu_row * 4, 4, 4), |
| 10823 | fctx, |
| 10824 | ); |
| 10825 | } |
| 10826 | let plane = if plane_idx == 1 { &mut *u } else { &mut *v }; |
| 10827 | let grid = read_plane( |
| 10828 | dec, cdfs, TxbSet::Chroma4, &scan4, plane_idx, |
| 10829 | cu_around[plane_idx], m.mode, m.uv_predict_mode, |
| 10830 | m.angle_delta_uv, cu_reach, plane, cu_x, cu_y, 4, 4, |
| 10831 | base_q_idx, |
| 10832 | m.alpha.zip(ac).map(|((au, av), ac)| { |
| 10833 | (if plane_idx == 1 { au } else { av }, ac) |
| 10834 | }), |
| 10835 | None, offset, m.smooth_neighbor_uv, fctx, |
| 10836 | )?; |
| 10837 | if span_x == 4 && span_y == 4 { |
| 10838 | hit!(RECT_SPLIT_LOSSLESS_CHROMA444_HITS); |
| 10839 | } |
| 10840 | neighbours.record_mi_chroma(cu_mi, span_x, span_y, plane_idx, &grid); |
| 10841 | ll_chroma_units.push((cu_mi, plane_idx, grid.clone(), span_x, span_y)); |
| 10842 | last[plane_idx - 1] = grid; |
| 10843 | } |
| 10844 | } |
| 10845 | } |
| 10846 | let [ug, vg] = last; |
| 10847 | (ug, vg) |
| 10848 | } else if chroma_tiled { |
| 10849 | let (uw, uh, set, scan): (usize, usize, TxbSet, &[u16]) = if chroma_422_oob { |
| 10850 | // lane-av1-422blockhalf: the BLOCK_INVALID-plane strip's (4, 8) |
| 10851 | // units -- the Release oracle's out-of-bounds |
| 10852 | // `max_txsize_rect_lookup[BLOCK_INVALID]` byte (see above). |
| 10853 | (4, 8, TxbSet::ChromaRect8x4, &SCAN_4X8) |
| 10854 | } else { |
| 10855 | match (chroma_w, chroma_h) { |
| 10856 | (64, 32) | (32, 64) | (32, 32) => (32, 32, TxbSet::Chroma32, default_scan(32)), |
| 10857 | (64, 16) => (32, 16, TxbSet::ChromaRect32x16, &SCAN_32X16), |
| 10858 | (16, 64) => (16, 32, TxbSet::ChromaRect32x16, &SCAN_16X32), |
| 10859 | // lane-av1-422: the 4:2:2 square chroma planes (height unhalved), |
| 10860 | // through the square reader like every other square chroma unit. |
| 10861 | (16, 16) => (16, 16, TxbSet::Chroma16, default_scan(16)), |
| 10862 | (8, 8) => (8, 8, TxbSet::Chroma8, default_scan(8)), |
| 10863 | _ => unreachable!("chroma_tiled"), |
| 10864 | } |
| 10865 | }; |
| 10866 | // lane-av1-422: the witness counter, keyed to ss (1,0) so only a 4:2:2 |
| 10867 | // frame's square chroma units count it. |
| 10868 | if uw == uh && ss_x(fctx) == 1 && ss_y(fctx) == 0 { |
| 10869 | hit!(CHROMA422_SQUARE_HITS); |
| 10870 | } |
| 10871 | let (nw, nh) = (chroma_w / uw, chroma_h / uh); |
| 10872 | let bigger = chroma_w * chroma_h > uw * uh; |
| 10873 | // `av1_get_ext_tx_set_type`: a chroma transform whose square-up size |
| 10874 | // is TX_32X32 or bigger resolves to `EXT_TX_SET_DCTONLY` for intra; |
| 10875 | // smaller rect units (the 4:2:2 BLOCK_INVALID-plane (4, 8)s) take the |
| 10876 | // mode-indexed default, the same rule the unsplit arm below applies. |
| 10877 | let default_tx = if uw.max(uh) >= 32 { |
| 10878 | TxType::DctDct |
| 10879 | } else { |
| 10880 | default_intra_tx_type(m.uv_predict_mode as u8) |
| 10881 | }; |
| 10882 | let mut last = [Grid::Zero(uw * uh), Grid::Zero(uw * uh)]; |
| 10883 | for plane_idx in 1..=2 { |
| 10884 | for cu_row in 0..nh { |
| 10885 | for cu_col in 0..nw { |
| 10886 | let luma_span_x = uw << ss_x(fctx); |
| 10887 | let luma_span_y = uh << ss_y(fctx); |
| 10888 | let cu_mi = ( |
| 10889 | mi_r + cu_row * (luma_span_y / MI), |
| 10890 | mi_c + cu_col * (luma_span_x / MI), |
| 10891 | ); |
| 10892 | // lane-av1-422blockhalf: the 4:2:2 (4, 8) units read their |
| 10893 | // chroma context per chroma cell |
| 10894 | // ([`Neighbours::around_mi_422_chroma`], the block-8 |
| 10895 | // rule) -- the plain rect walk samples one chroma cell |
| 10896 | // twice at ss_x 1. Every pre-existing shape keeps its own |
| 10897 | // walk. |
| 10898 | // lane-av1422mi24: the plain rect walk's double-count |
| 10899 | // hits EVERY unit shape at ss (1,0), not just the (4, 8) |
| 10900 | // one -- the (8, 8) unit of a 16x8 intra-in-inter strip |
| 10901 | // read its above dc votes twice and its U dc_sign one |
| 10902 | // ctx row high (t422 frame 1). Route every ss (1,0) |
| 10903 | // unit through the every-second-above-cell sampling; |
| 10904 | // 4:2:0/4:4:4 keep the plain walk verbatim. |
| 10905 | let cu_around = if chroma_422_oob |
| 10906 | || (ss_x(fctx) == 1 && ss_y(fctx) == 0) |
| 10907 | { |
| 10908 | neighbours.around_mi_422_chroma(cu_mi, luma_span_x, luma_span_y) |
| 10909 | } else { |
| 10910 | neighbours.around_mi_rect(cu_mi, luma_span_x, luma_span_y) |
| 10911 | }; |
| 10912 | let (cu_x, cu_y) = (cpx + cu_col * uw, cpy + cu_row * uh); |
| 10913 | let cfl = m.alpha.map(|(au, av)| { |
| 10914 | let alpha = if plane_idx == 1 { au } else { av }; |
| 10915 | ( |
| 10916 | alpha, |
| 10917 | cfl_src_rect( |
| 10918 | px + cu_col * luma_span_x, |
| 10919 | py + cu_row * luma_span_y, |
| 10920 | luma_span_x, |
| 10921 | luma_span_y, |
| 10922 | ), |
| 10923 | ) |
| 10924 | }); |
| 10925 | // lane-av1-422kf: the palette colour map is consumed per |
| 10926 | // unit here like every other chroma arm (`av1_visit_palette` |
| 10927 | // feeds the whole plane block; window it to this unit). |
| 10928 | // Without this the square chroma rows decoded a uv-palette |
| 10929 | // strip's chroma as plain DC (class same-symbols-silent- |
| 10930 | // prediction: the rng stayed in sync, only the prediction |
| 10931 | // was dropped). |
| 10932 | if let Some((ub, vb)) = &m.palette_uv { |
| 10933 | let buf = if plane_idx == 1 { ub } else { vb }; |
| 10934 | set_palette_pred( |
| 10935 | palette_window(buf, chroma_w, cu_col * uw, cu_row * uh, uw, uh), |
| 10936 | fctx, |
| 10937 | ); |
| 10938 | } |
| 10939 | // 32x32 does not fit read_coeffs_rect's scratch (768). The |
| 10940 | // square reader is the path every other 32x32 chroma unit uses. |
| 10941 | let levels = if uw == uh { |
| 10942 | let plane = if plane_idx == 1 { &mut *u } else { &mut *v }; |
| 10943 | read_plane( |
| 10944 | dec, cdfs, set, scan, plane_idx, cu_around[plane_idx], |
| 10945 | m.mode, m.uv_predict_mode, m.angle_delta_uv, reach, |
| 10946 | plane, cu_x, cu_y, uw, uw, base_q_idx, cfl, None, |
| 10947 | bigger.then_some(3), m.smooth_neighbor_uv, fctx, |
| 10948 | )? |
| 10949 | } else { |
| 10950 | let skip_ctx = usize::from(cu_around[plane_idx].0) |
| 10951 | + usize::from(cu_around[plane_idx].1) |
| 10952 | + usize::from(bigger) * 3; |
| 10953 | let mut coding = cdfs.txb(set, m.uv_predict_mode); |
| 10954 | let (levels, tx_type) = read_coeffs_rect( |
| 10955 | dec, &mut coding, scan, uw, uh, skip_ctx, |
| 10956 | dc_sign_ctx(cu_around[plane_idx].2), default_tx, |
| 10957 | )?; |
| 10958 | let residual = dequant_and_inverse_typed_wh( |
| 10959 | &levels, uw, uh, crate::decode::bit_depth(fctx), |
| 10960 | block_q_idx(fctx), |
| 10961 | plane_q_delta(plane_idx, fctx).0, |
| 10962 | plane_q_delta(plane_idx, fctx).1, |
| 10963 | tx_type, |
| 10964 | block_iqmatrix(fctx, plane_idx, uw, uh, tx_type), |
| 10965 | ); |
| 10966 | push_intra_rect( |
| 10967 | plane_idx, cu_x, cu_y, uw, uh, |
| 10968 | m.uv_predict_mode, m.angle_delta_uv, reach, &residual, |
| 10969 | cfl, None, m.smooth_neighbor_uv, fctx, |
| 10970 | ); |
| 10971 | levels |
| 10972 | }; |
| 10973 | neighbours.record_mi_chroma(cu_mi, luma_span_x, luma_span_y, plane_idx, &levels); |
| 10974 | tiled_chroma_units.push(( |
| 10975 | cu_mi, |
| 10976 | plane_idx, |
| 10977 | levels.clone(), |
| 10978 | luma_span_x, |
| 10979 | luma_span_y, |
| 10980 | )); |
| 10981 | last[plane_idx - 1] = levels; |
| 10982 | } |
| 10983 | } |
| 10984 | } |
| 10985 | (last[0].clone(), last[1].clone()) |
| 10986 | } else { |
| 10987 | let (chroma_set, chroma_scan) = chroma.expect("refused above when None"); |
| 10988 | // `av1_get_ext_tx_set_type`: a chroma transform whose square-up size |
| 10989 | // is TX_32X32 or bigger resolves to `EXT_TX_SET_DCTONLY` for intra, |
| 10990 | // so only the 16x8/8x16 pair takes the mode-indexed default. |
| 10991 | let default_tx = if chroma_w.max(chroma_h) >= 32 { |
| 10992 | TxType::DctDct |
| 10993 | } else { |
| 10994 | default_intra_tx_type(m.uv_predict_mode as u8) |
| 10995 | }; |
| 10996 | // lane-av1422mi24: at ss (1,0) the chroma above span is `bw >> 1` |
| 10997 | // chroma cells spread over `bw` luma mi columns -- the per-mi gather |
| 10998 | // counts every chroma column's whole-unit dc sign twice (the t422 |
| 10999 | // frame-1 mi(26,60) intra-in-inter strip's U dc_sign read one ctx |
| 11000 | // row high). Luma keeps the per-mi gather; the two chroma planes |
| 11001 | // take [`Neighbours::around_mi_422_chroma`]'s every-second |
| 11002 | // above-cell sampling, like every other 4:2:2 chroma gather. |
| 11003 | let mut around = neighbours.around_mi_rect(at_mi, bw, bh); |
| 11004 | if ss_x(fctx) == 1 && ss_y(fctx) == 0 { |
| 11005 | let c = neighbours.around_mi_422_chroma(at_mi, bw, bh); |
| 11006 | around[1] = c[1]; |
| 11007 | around[2] = c[2]; |
| 11008 | } |
| 11009 | let mut grids: Vec<Grid> = Vec::with_capacity(2); |
| 11010 | for plane_idx in 1..=2 { |
| 11011 | let skip_ctx = |
| 11012 | usize::from(around[plane_idx].0) + usize::from(around[plane_idx].1); |
| 11013 | let mut coding = cdfs.txb(chroma_set, m.uv_predict_mode); |
| 11014 | let (levels, tx_type) = read_coeffs_rect( |
| 11015 | dec, |
| 11016 | &mut coding, |
| 11017 | chroma_scan, |
| 11018 | chroma_w, |
| 11019 | chroma_h, |
| 11020 | skip_ctx, |
| 11021 | dc_sign_ctx(around[plane_idx].2), |
| 11022 | default_tx, |
| 11023 | )?; |
| 11024 | // The same delta_q proof the unsplit rect64 path records |
| 11025 | // (lane-rectsplit r4: lifting the split-transform refusal moved |
| 11026 | // superblock strips onto this path, so only counting there would |
| 11027 | // leave the delta_q gate's counter silent for them). |
| 11028 | if bw.max(bh) == 64 && fctx.current_q_idx.with(|c| c.get()) != i32::from(base_q_idx) { |
| 11029 | hit!(RECT64_QIDX_DRIFT_HITS); |
| 11030 | } |
| 11031 | let residual = dequant_and_inverse_typed_wh( |
| 11032 | &levels, |
| 11033 | chroma_w, |
| 11034 | chroma_h, |
| 11035 | crate::decode::bit_depth(fctx), |
| 11036 | // lane-d792: the BLOCK's quantizer index, i.e. the frame's |
| 11037 | // own index PLUS this block's segment `SEG_LVL_ALT_Q` -- |
| 11038 | // reading `current_q_idx` here dropped the segment feature |
| 11039 | // that every other dequant site applies (class |
| 11040 | // `parsed-then-discarded`). |
| 11041 | block_q_idx(fctx), |
| 11042 | plane_q_delta(plane_idx, fctx).0, |
| 11043 | plane_q_delta(plane_idx, fctx).1, |
| 11044 | tx_type, |
| 11045 | block_iqmatrix(fctx, plane_idx, chroma_w, chroma_h, tx_type), |
| 11046 | ); |
| 11047 | let cfl = m |
| 11048 | .alpha |
| 11049 | .zip(ac) |
| 11050 | .map(|((au, av), ac)| (if plane_idx == 1 { au } else { av }, ac)); |
| 11051 | let _plane = if plane_idx == 1 { &mut *u } else { &mut *v }; |
| 11052 | if let Some((ub, vb)) = &m.palette_uv { |
| 11053 | let buf = if plane_idx == 1 { ub } else { vb }; |
| 11054 | set_palette_pred(buf.clone(), fctx); |
| 11055 | } |
| 11056 | push_intra_rect(plane_idx, |
| 11057 | cpx, |
| 11058 | cpy, |
| 11059 | chroma_w, |
| 11060 | chroma_h, |
| 11061 | m.uv_predict_mode, |
| 11062 | m.angle_delta_uv, |
| 11063 | reach, |
| 11064 | &residual, |
| 11065 | cfl, |
| 11066 | None, |
| 11067 | m.smooth_neighbor_uv, fctx, |
| 11068 | ); |
| 11069 | grids.push(levels); |
| 11070 | } |
| 11071 | let mut it = grids.into_iter(); |
| 11072 | (it.next().unwrap(), it.next().unwrap()) |
| 11073 | }; |
| 11074 | neighbours.record_split_luma_rect_mi( |
| 11075 | at_mi, |
| 11076 | bw, |
| 11077 | bh, |
| 11078 | m.mode, |
| 11079 | m.uv_predict_mode, |
| 11080 | [&u_grid, &v_grid], |
| 11081 | ); |
| 11082 | for (cu_mi, plane, unit, span_w, span_h) in &ll_chroma_units { |
| 11083 | neighbours.record_mi_chroma(*cu_mi, *span_w, *span_h, *plane, unit); |
| 11084 | } |
| 11085 | for (cu_mi, plane, unit, span_w, span_h) in &tiled_chroma_units { |
| 11086 | neighbours.record_mi_chroma(*cu_mi, *span_w, *span_h, *plane, unit); |
| 11087 | } |
| 11088 | neighbours.fill_skip_grid_rect((mi_r, mi_c), bw / MI, bh / MI, m.skip); |
| 11089 | neighbours.fill_lf_grid_rect((mi_r, mi_c), bw / MI, bh / MI, tx_w as u8, tx_h as u8, 0, fctx); |
| 11090 | if crate::envflags::env_flag!("EC_AV1_TRACE") { |
| 11091 | let (rng, _) = dec.debug_state(); |
| 11092 | eprintln!( |
| 11093 | "TRACE_RECT_SPLIT mi_row={mi_r} mi_col={mi_c} bw={bw} bh={bh} \ |
| 11094 | tx={tx_w}x{tx_h} rng={rng}" |
| 11095 | ); |
| 11096 | } |
| 11097 | Ok(()) |
| 11098 | } |
| 11099 | |
| 11100 | /// One intra-coded HORZ/VERT strip on the INTER block path (lane-intrarect |
| 11101 | /// r1) -- the shape [`decode_inter_block`]'s intra arm refused by name |
| 11102 | /// because it predicts squares only. Every pixel here goes through the rect |
| 11103 | /// machinery the key-frame paths already prove: [`decode_rect_split`], which |
| 11104 | /// at `depth == 0` is the unsplit strip as a single transform unit and |
| 11105 | /// beyond it the per-unit walk of [`depth_to_tx_wh`]. |
| 11106 | /// |
| 11107 | /// What is genuinely different from [`decode_block_rect`] is the mode |
| 11108 | /// syntax: on an inter frame an intra block reads `y_mode[size_group]` and |
| 11109 | /// `uv_mode` (libaom `read_intra_block_mode_info`, decodemv.c:1065), never |
| 11110 | /// the key frame's `kf_y_mode` above/left pair. This mirrors the square |
| 11111 | /// intra-in-inter arm's own reads at the rect footprint, including its |
| 11112 | /// inherited gaps -- no `use_filter_intra` symbol (every inter gate recipe |
| 11113 | /// pins `--enable-filter-intra=0`; lane-fiinter owns that read for both the |
| 11114 | /// square and this arm) and a nonzero luma angle delta refused by name. |
| 11115 | /// |
| 11116 | /// The strip's own neighbour records (luma per transform unit, chroma, skip |
| 11117 | /// grid, deblock grid) are [`decode_rect_split`]'s; the caller adds the |
| 11118 | /// inter-side bands and the mi map. |
| 11119 | #[allow(clippy::too_many_arguments)] |
| 11120 | /// The `(size_group_lookup, bsize_to_tx_size_cat)` row an INTRA block coded |
| 11121 | /// inside an inter frame reads its `y_mode` and `tx_depth` symbols from, for |
| 11122 | /// the rect footprints [`decode_intra_rect_in_inter`] resolves itself (the |
| 11123 | /// 128-root halves and the 16x4/4x16 strips of a 1:4 partition take their own |
| 11124 | /// branches before this). libaom `common_data.h:61` / `blockd.h:1347`: the 2:1 |
| 11125 | /// strips keep the group/category diagonal (BLOCK_16X8 -> 1/1, BLOCK_32X16 -> |
| 11126 | /// 2/2, BLOCK_64X32 -> 3/3), the 1:4 ones break it (BLOCK_32X8 is group 1 but |
| 11127 | /// category 2, BLOCK_64X16 group 2 and category 3), which is why the row is a |
| 11128 | /// table here rather than a shift of `bw.min(bh)`. |
| 11129 | fn intra_in_inter_size_row(bw: usize, bh: usize) -> Option<(usize, usize)> { |
| 11130 | Some(match (bw.min(bh), bw.max(bh)) { |
| 11131 | (8, 16) => (1usize, 1usize), |
| 11132 | (16, 32) => (2, 2), |
| 11133 | (32, 64) => (3, 3), |
| 11134 | (8, 32) => (1, 2), |
| 11135 | (16, 64) => (2, 3), |
| 11136 | _ => return None, |
| 11137 | }) |
| 11138 | } |
| 11139 | |
| 11140 | /// lane-t900 r20, refusal B ("an intra-coded {bw}x{bh} block on the inter |
| 11141 | /// block path"): the shapes that can reach that arm, enumerated against the |
| 11142 | /// MEASURED domain of the inter block path |
| 11143 | /// ([`INTER_BLOCK_SHAPES`], the census the gate |
| 11144 | /// `a_block_shape_census_over_three_real_streams_leaves_the_rect_residual_refusal_unreachable` |
| 11145 | /// fills from three real streams). |
| 11146 | /// |
| 11147 | /// [`decode_intra_rect_in_inter`] is called from one site, under |
| 11148 | /// `write_w != side || write_h != side`, and only after two earlier arms have |
| 11149 | /// taken their shapes: a footprint with a side below 8 is either a 16x4/4x16 |
| 11150 | /// strip WITH a chroma-pair record (the `strip16` branch) or refused by name, |
| 11151 | /// and a 128-px footprint returns from the `bw.max(bh) == 128` branch. What is |
| 11152 | /// left is every rectangular census shape with `8 <= min` and `max <= 64` -- |
| 11153 | /// and each of those has a row, so the refusal names a shape no measured |
| 11154 | /// stream can present. |
| 11155 | #[test] |
| 11156 | fn every_intra_in_inter_shape_the_census_lists_has_a_size_group_row() { |
| 11157 | let mut checked = 0; |
| 11158 | for &(side, w, h) in &INTER_BLOCK_SHAPES { |
| 11159 | if (w, h) == (side, side) || w.min(h) < 8 || w.max(h) > 64 { |
| 11160 | continue; |
| 11161 | } |
| 11162 | assert!( |
| 11163 | intra_in_inter_size_row(w, h).is_some(), |
| 11164 | "the inter path decodes {w}x{h} blocks, and an INTRA one of that shape would hit \ |
| 11165 | the \"no size-group/tx-category row\" refusal" |
| 11166 | ); |
| 11167 | checked += 1; |
| 11168 | } |
| 11169 | assert_eq!( |
| 11170 | checked, 10, |
| 11171 | "the block-shape census changed: {checked} rect shapes reach the intra-in-inter row \ |
| 11172 | lookup, not the 10 this claim was measured over" |
| 11173 | ); |
| 11174 | } |
| 11175 | |
| 11176 | /// The `motion_mode` / `obmc` CDF row for a block footprint: libaom's |
| 11177 | /// `motion_mode_cdf[mbmi->bsize]`, in this crate's packed row order (which |
| 11178 | /// `cdf::MOTION_MODE` documents -- the 128-px rows come last, so they are |
| 11179 | /// matched before the square arm's `log2` shortcut). |
| 11180 | /// |
| 11181 | /// `None` is the refusal "a motion_mode symbol for a block shape with no CDF |
| 11182 | /// row here"; the enumeration below is what proves no conformant block can |
| 11183 | /// reach it. |
| 11184 | pub(crate) fn motion_mode_cdf_row(write_w: usize, write_h: usize) -> Option<usize> { |
| 11185 | Some(match (write_w, write_h) { |
| 11186 | // lane-sb128c r5: the 128 root's own rows come FIRST -- the square arm |
| 11187 | // below maps 128 to row 4 (BLOCK_16X32). |
| 11188 | (64, 128) => 14, |
| 11189 | (128, 64) => 15, |
| 11190 | (128, 128) => 16, |
| 11191 | (w, h) if w == h => (w.trailing_zeros() - 3) as usize, |
| 11192 | (16, 32) => 4, |
| 11193 | (32, 16) => 5, |
| 11194 | (32, 64) => 6, |
| 11195 | (64, 32) => 7, |
| 11196 | (8, 32) => 8, |
| 11197 | (32, 8) => 9, |
| 11198 | (16, 64) => 10, |
| 11199 | (64, 16) => 11, |
| 11200 | // lane-inter4 r3: the 16x16-level rect inter leaves. |
| 11201 | (8, 16) => 12, |
| 11202 | (16, 8) => 13, |
| 11203 | _ => return None, |
| 11204 | }) |
| 11205 | } |
| 11206 | |
| 11207 | /// Every `BLOCK_SIZES_ALL` footprint, as `(width, height)` in pixels: the six |
| 11208 | /// squares, the ten 2:1 shapes and the six 1:4 ones (libaom `common_data.h`, |
| 11209 | /// `block_size_wide`/`block_size_high`). |
| 11210 | #[cfg(test)] |
| 11211 | const BLOCK_SIZES_ALL_WH: [(usize, usize); 22] = [ |
| 11212 | (4, 4), |
| 11213 | (4, 8), |
| 11214 | (8, 4), |
| 11215 | (8, 8), |
| 11216 | (8, 16), |
| 11217 | (16, 8), |
| 11218 | (16, 16), |
| 11219 | (16, 32), |
| 11220 | (32, 16), |
| 11221 | (32, 32), |
| 11222 | (32, 64), |
| 11223 | (64, 32), |
| 11224 | (64, 64), |
| 11225 | (64, 128), |
| 11226 | (128, 64), |
| 11227 | (128, 128), |
| 11228 | (4, 16), |
| 11229 | (16, 4), |
| 11230 | (8, 32), |
| 11231 | (32, 8), |
| 11232 | (16, 64), |
| 11233 | (64, 16), |
| 11234 | ]; |
| 11235 | |
| 11236 | /// lane-t900 r25, enumeration: the refusal "a motion_mode symbol for a block |
| 11237 | /// shape with no CDF row here" names a shape no conformant stream can present. |
| 11238 | /// |
| 11239 | /// A `motion_mode`/`obmc` symbol is read only under `motion_mode_eligible`, |
| 11240 | /// whose `write_w.min(write_h) >= 8` clause is libaom's |
| 11241 | /// `is_motion_variation_allowed_bsize` (`blockd.h:1455`). So the reachable |
| 11242 | /// domain of [`motion_mode_cdf_row`] is exactly the `BLOCK_SIZES_ALL` |
| 11243 | /// footprints with `min(bw, bh) >= 8` -- 17 of the 22 -- and each of those |
| 11244 | /// must map to a distinct row of the 17-row `Cdfs::motion_mode` table. The |
| 11245 | /// five it excludes (4x4, 4x8, 8x4, 4x16, 16x4) are the ones the eligibility |
| 11246 | /// clause rejects before the lookup. |
| 11247 | #[test] |
| 11248 | fn every_shape_that_allows_motion_variation_has_a_motion_mode_cdf_row() { |
| 11249 | let mut rows = std::collections::BTreeSet::new(); |
| 11250 | let mut excluded = 0; |
| 11251 | for (w, h) in BLOCK_SIZES_ALL_WH { |
| 11252 | if w.min(h) < 8 { |
| 11253 | excluded += 1; |
| 11254 | assert!( |
| 11255 | w == 4 || h == 4, |
| 11256 | "{w}x{h} is not one of the sub-8 shapes is_motion_variation_allowed_bsize rejects" |
| 11257 | ); |
| 11258 | continue; |
| 11259 | } |
| 11260 | let row = motion_mode_cdf_row(w, h).unwrap_or_else(|| { |
| 11261 | panic!( |
| 11262 | "a {w}x{h} block passes is_motion_variation_allowed_bsize and reads a motion_mode \ |
| 11263 | symbol, but has no CDF row -- the refusal is a real gap, not a dead arm" |
| 11264 | ) |
| 11265 | }); |
| 11266 | assert!( |
| 11267 | row < 17, |
| 11268 | "{w}x{h} maps to motion_mode row {row}, outside the 17-row table" |
| 11269 | ); |
| 11270 | assert!(rows.insert(row), "{w}x{h} shares motion_mode row {row}"); |
| 11271 | } |
| 11272 | assert_eq!(excluded, 5, "the sub-8 footprint set changed"); |
| 11273 | assert_eq!( |
| 11274 | rows.len(), |
| 11275 | 17, |
| 11276 | "17 eligible footprints must fill all 17 rows of the motion_mode table" |
| 11277 | ); |
| 11278 | } |
| 11279 | |
| 11280 | fn decode_intra_rect_in_inter( |
| 11281 | dec: &mut SymbolDecoder, |
| 11282 | cdfs: &mut Cdfs, |
| 11283 | neighbours: &mut Neighbours, |
| 11284 | at: (usize, usize), |
| 11285 | bw: usize, |
| 11286 | bh: usize, |
| 11287 | skip: bool, |
| 11288 | allow_screen_content_tools: bool, |
| 11289 | // `(PARTITION_HORZ_4, is_chroma_reference)` when this strip is one of the |
| 11290 | // four 16x4 / 4x16 strips of a 16x16-level 1:4 partition (lane-intra16x4). |
| 11291 | strip16: Option<(bool, bool)>, |
| 11292 | y: &mut PlaneBuf<'static>, |
| 11293 | u: &mut PlaneBuf<'static>, |
| 11294 | v: &mut PlaneBuf<'static>, |
| 11295 | base_q_idx: u8, fctx: &crate::decode::FrameCtx, |
| 11296 | ) -> Result<()> { |
| 11297 | // `at` is in MI units (the inter path's own unit since lane-inter4); |
| 11298 | // `(r, c)` is the enclosing SUB cell the SUB-granular neighbour bands |
| 11299 | // below are still keyed by. |
| 11300 | let (mi_r, mi_c) = at; |
| 11301 | let (r, c) = (mi_r / (SUB / MI), mi_c / (SUB / MI)); |
| 11302 | // `size_group_lookup` (libaom common_data.h) and `bsize_to_tx_size_cat` |
| 11303 | // (blockd.h) for the three 2:1 strip shapes: BLOCK_16X8 -> group 1 / |
| 11304 | // cat 1, BLOCK_32X16 -> 2 / 2, BLOCK_64X32 -> 3 / 3 (the rectsplitx |
| 11305 | // finding -- the category is NOT the same for every strip size). |
| 11306 | // A 1:4 strip breaks that diagonal (BLOCK_32X8 is group 1 but cat 2), so |
| 11307 | // it is refused by name rather than read from the wrong CDF row. |
| 11308 | // lane-intra16x4: the 16x16-level 1:4 strip. `size_group_lookup` |
| 11309 | // (common_data.h:61) puts BLOCK_16X4/BLOCK_4X16 in group 0 and its |
| 11310 | // tx-depth category is 1 -- but nothing here has to know either, because |
| 11311 | // the strip decodes through the very body the KEY FRAME path proves for |
| 11312 | // this shape ([`decode_rect4_16_strip`]), including the 4:2:0 chroma |
| 11313 | // pairing (`is_chroma_reference`: strips 0 and 2 read no chroma at all, |
| 11314 | // strips 1 and 3 code one 8x4 (4x8) unit at the PAIR's origin). The only |
| 11315 | // inter-frame difference is the mode-info read, which |
| 11316 | // [`INTRA_IN_INTER_MODE`] switches to `read_intra_block_mode_info`'s |
| 11317 | // `y_mode[0]` plus the `skip` this caller already read. |
| 11318 | if let Some((horz, has_chroma)) = strip16 { |
| 11319 | // lane-t900 r22: the screen-content refusal here was STALE (class |
| 11320 | // stale-premise): [`decode_rect4_16_strip`] takes |
| 11321 | // `allow_screen_content_tools` and reads the whole palette syntax |
| 11322 | // (mode/size/colours/colour-index map, its own `palette_ctx_and_cache_mi` |
| 11323 | // pair) -- the key frame proves that body. Lifted with the 10-bit |
| 11324 | // witness `palette_screen_witness_10bit.obu`, whose inter frames carry |
| 11325 | // 16x4/4x16 intra strips inside a `--tune-content=screen` frame. |
| 11326 | debug_assert_eq!((bw, bh), if horz { (16, 4) } else { (4, 16) }); |
| 11327 | fctx.intra_in_inter_mode.with(|c| c.set(Some((0, skip)))); |
| 11328 | let res = decode_rect4_16_strip( |
| 11329 | dec, |
| 11330 | cdfs, |
| 11331 | neighbours, |
| 11332 | (mi_r, mi_c), |
| 11333 | horz, |
| 11334 | has_chroma, |
| 11335 | // The inter strips of the same partition publish their own |
| 11336 | // mi-exact mode, so the key-frame caller's coarse-cell stand-in |
| 11337 | // has nothing to add here. |
| 11338 | None, |
| 11339 | y, |
| 11340 | u, |
| 11341 | v, |
| 11342 | fctx.enable_filter_intra_inter.with(std::cell::Cell::get), |
| 11343 | allow_screen_content_tools, |
| 11344 | base_q_idx, |
| 11345 | fctx.tx_select_inter.with(std::cell::Cell::get), |
| 11346 | fctx.reduced_tx_set_inter.with(std::cell::Cell::get), fctx, |
| 11347 | ); |
| 11348 | fctx.intra_in_inter_mode.with(|c| c.set(None)); |
| 11349 | let (mode, uv, (tx_w, tx_h)) = res?; |
| 11350 | // lane-intra16x4, the same publication the 2:1 intra-in-inter strip |
| 11351 | // makes ([`decode_intra_rect_in_inter`]'s own tail): libaom runs |
| 11352 | // `set_txfm_ctxs(tx_size, n4_w, n4_h, skip && is_inter, xd)` for EVERY |
| 11353 | // block of an inter frame, intra ones included, and |
| 11354 | // `txfm_partition_update` for the var-tx context. The key-frame body |
| 11355 | // writes neither (a key frame has no var-tx tree and its own readers |
| 11356 | // use the deblock grid), so the block AFTER this strip read a stale |
| 11357 | // band -- measured: a 128x128 8-bit stream decoded the two intra 16x4 |
| 11358 | // strips at mi(23,16)/(23,20) exactly and then broke on the very next |
| 11359 | // 16x16 row. |
| 11360 | txfm_partition_update_rect(neighbours, (mi_r, mi_c), (tx_w, tx_h), (bw, bh)); |
| 11361 | for i in 0..bw / MI { |
| 11362 | if let Some(cell) = neighbours.above_txfm.get_mut(mi_c + i) { |
| 11363 | *cell = tx_w as u8; |
| 11364 | } |
| 11365 | } |
| 11366 | for i in 0..bh / MI { |
| 11367 | if let Some(cell) = neighbours.left_txfm.get_mut(mi_r + i) { |
| 11368 | *cell = tx_h as u8; |
| 11369 | } |
| 11370 | } |
| 11371 | // [`decode_rect4_16`]'s own post-loop publication, per strip (the |
| 11372 | // coarse 16-px cell holds one entry for the whole 16x16 and the last |
| 11373 | // block to write it wins). |
| 11374 | neighbours.above_mode[c] = mode; |
| 11375 | neighbours.left_mode[r] = mode; |
| 11376 | if let Some(uvm) = uv { |
| 11377 | neighbours.above_uv_mode[c] = uvm; |
| 11378 | neighbours.left_uv_mode[r] = uvm; |
| 11379 | } |
| 11380 | neighbours.above_side[c] = bw; |
| 11381 | neighbours.left_side[r] = bh; |
| 11382 | if crate::envflags::env_flag!("EC_INTRA16X4") { |
| 11383 | eprintln!( |
| 11384 | "EC_INTRA16X4 decoded mi={mi_r},{mi_c} px={},{} horz={horz} has_chroma={has_chroma} skip={skip} mode={mode} uv={uv:?}", |
| 11385 | mi_c * MI, |
| 11386 | mi_r * MI |
| 11387 | ); |
| 11388 | } |
| 11389 | hit_do! { |
| 11390 | INTRA16X4_IN_INTER_HITS.with(|h| { |
| 11391 | let mut v = h.get(); |
| 11392 | v[usize::from(!horz)] += 1; |
| 11393 | v[2] += usize::from(has_chroma); |
| 11394 | h.set(v); |
| 11395 | }); |
| 11396 | } |
| 11397 | return Ok(()); |
| 11398 | } |
| 11399 | // lane-inter128intra r1: the 128 superblock's PARTITION_HORZ/VERT half |
| 11400 | // coded INTRA inside an inter frame. `size_group_lookup` (common_data.h:61) |
| 11401 | // puts BLOCK_128X64/64X128/128X128 in group 3 and |
| 11402 | // `max_txsize_rect_lookup` caps them at TX_64X64, i.e. the same |
| 11403 | // `tx_size_cat3` row [`decode_block_128rect`] already reads -- so the whole |
| 11404 | // block decodes through the very body the KEY FRAME path proves for this |
| 11405 | // shape (square 64x64 luma units, one TX_32X32 chroma unit per 64x64 mu |
| 11406 | // chunk on the offset-10 txb_skip rows, per-TU prediction, the mu-chunk |
| 11407 | // `Reach` rule and the frame-edge write clip). The only inter-frame |
| 11408 | // differences live inside that body: the mode-info read |
| 11409 | // ([`INTRA_IN_INTER_MODE`] -> `y_mode[3]` plus the `skip` this caller |
| 11410 | // already read), the `tx_depth` context (the real TXFM_CONTEXT bands) and |
| 11411 | // the `set_txfm_ctxs` publication. CfL (>32), filter intra (>32) and |
| 11412 | // palette (>64) do not exist at this size and are refused by name there. |
| 11413 | // lane-t900 r18: the opt-in is LIFTED -- the arm has a witness. |
| 11414 | // `intra128_in_inter_witness2.obu` (gate |
| 11415 | // `an_intra_coded_128_half_inside_an_inter_frame_decodes_pixel_exact`) |
| 11416 | // fires it 3x 128x64 + 6x 64x128 and decodes byte-exact on every plane of |
| 11417 | // all 15 frames, so the hits are counted out of exact frames (class |
| 11418 | // `counter-from-refused-stream`). What r16/r17 could not find, and what |
| 11419 | // that recipe exploits: libaom's RD essentially never PICKS a 128-root |
| 11420 | // HORZ/VERT in the interior -- the 512x512 r16 witness and a textured |
| 11421 | // 640x384 sweep code 128 roots as NONE or SPLIT only. The shape appears |
| 11422 | // where the frame EDGE forces it: a partial last superblock row has |
| 11423 | // `has_rows128 == false`, so the 128x64 top half is implied, and a |
| 11424 | // partial last column implies the 64x128 left half. Put content no |
| 11425 | // reference holds (a fresh smooth ramp) in exactly those two bands of an |
| 11426 | // inter frame and the encoder codes those forced halves INTRA. |
| 11427 | if bw.max(bh) == 128 { |
| 11428 | // lane-t900 r32: no screen-content refusal here. `av1_allow_palette` |
| 11429 | // rejects every footprint with a 128-pixel side (`bw > 64 || bh > 64`, |
| 11430 | // enumerated by `no_block_footprint_with_a_128_pixel_side_can_carry_\ |
| 11431 | // palette_syntax`), so a 128-root half consumes no palette syntax and |
| 11432 | // a screen-content frame's half is syntactically identical to the |
| 11433 | // non-screen one this arm already decodes byte-exact. |
| 11434 | if coeff_trace_on() { |
| 11435 | // lane-t900 r16: which decode-order frame this arm fires in -- |
| 11436 | // interleaved with the `EC_COEFF_FRAME` markers, this is what |
| 11437 | // proves the hit is counted out of a frame that compares exact |
| 11438 | // (class `counter-from-refused-stream`). |
| 11439 | eprintln!("EC_INTRA128 mi_row={r} mi_col={c} bw={bw} bh={bh}"); |
| 11440 | } |
| 11441 | fctx.intra_in_inter_mode.with(|c| c.set(Some((3, skip)))); |
| 11442 | let res = decode_block_128rect( |
| 11443 | dec, |
| 11444 | cdfs, |
| 11445 | neighbours, |
| 11446 | (r, c), |
| 11447 | bw, |
| 11448 | bh, |
| 11449 | y, |
| 11450 | u, |
| 11451 | v, |
| 11452 | base_q_idx, |
| 11453 | fctx.enable_filter_intra_inter.with(std::cell::Cell::get), |
| 11454 | allow_screen_content_tools, |
| 11455 | fctx.tx_select_inter.with(std::cell::Cell::get), |
| 11456 | fctx.reduced_tx_set_inter.with(std::cell::Cell::get), fctx, |
| 11457 | ); |
| 11458 | fctx.intra_in_inter_mode.with(|c| c.set(None)); |
| 11459 | return res; |
| 11460 | } |
| 11461 | let Some((size_group, tx_cat)) = intra_in_inter_size_row(bw, bh) else { |
| 11462 | if crate::envflags::env_flag!("EC_OBMCREC") { |
| 11463 | eprintln!( |
| 11464 | "OBMCREC intra-in-inter shape bw={bw} bh={bh} mi=({mi_r},{mi_c}) skip={skip}" |
| 11465 | ); |
| 11466 | } |
| 11467 | // lane-obmcrec r1: the message used to name a 16x4/4x16 strip |
| 11468 | // (class refusal-names-a-correlate). lane-t900 r20: the shapes that |
| 11469 | // can arrive here are enumerated against the measured block-shape |
| 11470 | // census ([`INTER_BLOCK_SHAPES`]) by |
| 11471 | // [`every_intra_in_inter_shape_the_census_lists_has_a_size_group_row`] |
| 11472 | // -- every one of them has a row, so this arm names a shape no stream |
| 11473 | // has been able to present. |
| 11474 | return Err(unsupported(format!( |
| 11475 | "an intra-coded {bw}x{bh} block on the inter block path (no size-group/tx-category row for that shape here)" |
| 11476 | ))); |
| 11477 | }; |
| 11478 | // lane-t900 r21: this arm used to refuse EVERY rect strip of a |
| 11479 | // screen-content frame ("palette syntax is consumed for square blocks |
| 11480 | // only"). It is not: [`read_intra_mode_rect`] generalised the palette read |
| 11481 | // to rect blocks on the key-frame path, and the 1:4 arm below goes through |
| 11482 | // it, while the 2:1 reader inlined further down now codes the same syntax |
| 11483 | // itself. A real `--tune-content=screen` inter stream reached this refusal |
| 11484 | // on 8x16 / 16x8 strips (gate |
| 11485 | // `a_real_aomenc_screen_inter_sequence_codes_palette_syntax_on_rect_intra_strips`). |
| 11486 | // lane-intra14 r1: a 1:4 strip decodes through the very readers the KEY |
| 11487 | // FRAME path already proves for this shape ([`decode_block_rect4`] at the |
| 11488 | // 32 level, [`decode_block_rect64`] at 64 -- both already handle the 1:4 |
| 11489 | // luma/chroma transform tables, the per-unit split walk and the strip's |
| 11490 | // own neighbour records). The ONLY difference on an inter frame is the |
| 11491 | // mode-info read, which [`INTRA_IN_INTER_MODE`] switches to |
| 11492 | // `read_intra_block_mode_info`'s `y_mode[size_group]` plus the `skip` |
| 11493 | // this caller already read. `tx_cat` above is the same category those |
| 11494 | // readers hardcode (cat 2 in `decode_block_rect4`, cat 3 in |
| 11495 | // `decode_block_rect64`), asserted here so a future shape cannot drift. |
| 11496 | if bw.max(bh) == 4 * bw.min(bh) { |
| 11497 | debug_assert_eq!(tx_cat, if bw.max(bh) == 64 { 3 } else { 2 }); |
| 11498 | fctx.intra_in_inter_mode.with(|c| c.set(Some((size_group, skip)))); |
| 11499 | let res = if bw.max(bh) == 64 { |
| 11500 | decode_block_rect64( |
| 11501 | dec, |
| 11502 | cdfs, |
| 11503 | neighbours, |
| 11504 | (mi_r / (SUB / MI), mi_c / (SUB / MI)), |
| 11505 | bw, |
| 11506 | bh, |
| 11507 | y, |
| 11508 | u, |
| 11509 | v, |
| 11510 | fctx.enable_filter_intra_inter.with(std::cell::Cell::get), |
| 11511 | allow_screen_content_tools, |
| 11512 | base_q_idx, |
| 11513 | fctx.tx_select_inter.with(std::cell::Cell::get), |
| 11514 | fctx.reduced_tx_set_inter.with(std::cell::Cell::get), fctx, |
| 11515 | ) |
| 11516 | } else { |
| 11517 | decode_block_rect4( |
| 11518 | dec, |
| 11519 | cdfs, |
| 11520 | neighbours, |
| 11521 | (mi_r, mi_c), |
| 11522 | bw, |
| 11523 | bh, |
| 11524 | None, |
| 11525 | y, |
| 11526 | u, |
| 11527 | v, |
| 11528 | fctx.enable_filter_intra_inter.with(std::cell::Cell::get), |
| 11529 | allow_screen_content_tools, |
| 11530 | base_q_idx, |
| 11531 | fctx.tx_select_inter.with(std::cell::Cell::get), fctx, |
| 11532 | ) |
| 11533 | .map(|_| ()) |
| 11534 | }; |
| 11535 | fctx.intra_in_inter_mode.with(|c| c.set(None)); |
| 11536 | res?; |
| 11537 | let shape = match (bw, bh) { |
| 11538 | (64, 16) => 0, |
| 11539 | (16, 64) => 1, |
| 11540 | (32, 8) => 2, |
| 11541 | _ => 3, |
| 11542 | }; |
| 11543 | hit_do! { |
| 11544 | INTRA_RECT4_IN_INTER_HITS.with(|h| { |
| 11545 | let mut v = h.get(); |
| 11546 | v[shape] += 1; |
| 11547 | h.set(v); |
| 11548 | }); |
| 11549 | } |
| 11550 | return Ok(()); |
| 11551 | } |
| 11552 | let mode = dec.symbol(&mut cdfs.y_mode[size_group]); |
| 11553 | // lane-t900 r1: this arm read six mode symbols with no ladder rung of its |
| 11554 | // own, so a cross-decoder bisection could only see the block's first |
| 11555 | // coefficient. Same names/format as [`read_tx_size`]'s rung. |
| 11556 | let step_trace = crate::envflags::env_flag!("EC_TRACE_MODE_STEP"); |
| 11557 | if step_trace { |
| 11558 | eprintln!( |
| 11559 | "EC_ISTEP mi_row={mi_r} mi_col={mi_c} name=y_mode val={mode} grp={size_group} rng={}", |
| 11560 | dec.debug_state().0 |
| 11561 | ); |
| 11562 | } |
| 11563 | if mode >= 13 { |
| 11564 | return Err(unsupported( |
| 11565 | "an intra mode this decoder does not code (round 2)", |
| 11566 | )); |
| 11567 | } |
| 11568 | // lane-angleinter (merged on main for the square intra-in-inter arm): |
| 11569 | // the delta is read off `angle_delta_cdf[mode - V_PRED]` and feeds the |
| 11570 | // directional predictor; this arm refused a nonzero one before the merge. |
| 11571 | let angle_delta_y = if (V_PRED..=D67_PRED).contains(&mode) { |
| 11572 | read_angle_delta(dec, &mut cdfs.angle_delta[mode - V_PRED]) |
| 11573 | } else { |
| 11574 | 0 |
| 11575 | }; |
| 11576 | if angle_delta_y != 0 { |
| 11577 | hit!(INTRA_IN_INTER_ANGLE_DELTA_Y_HITS); |
| 11578 | } |
| 11579 | // `is_cfl_allowed` (spec 5.11.5) caps CFL at 32x32, so the |
| 11580 | // superblock-level strip reads the no-CFL alphabet -- the same split |
| 11581 | // [`read_intra_mode_rect`] makes for [`decode_block_rect64`]. |
| 11582 | // |
| 11583 | // lane-av1mono: a monochrome strip codes no chroma mode/CFL symbol |
| 11584 | // (libaom `decodemv.c:933`). |
| 11585 | let has_chroma = !mono(fctx); |
| 11586 | let cfl_allowed = has_chroma && cfl_allowed_px(bw, bh, fctx); |
| 11587 | let uv_mode = if !has_chroma { |
| 11588 | DC_PRED |
| 11589 | } else if cfl_allowed { |
| 11590 | dec.symbol(&mut cdfs.uv_mode_cfl[mode]) |
| 11591 | } else { |
| 11592 | dec.symbol(&mut cdfs.uv_mode_no_cfl[mode]) |
| 11593 | }; |
| 11594 | if step_trace { |
| 11595 | eprintln!( |
| 11596 | "EC_ISTEP mi_row={mi_r} mi_col={mi_c} name=uv_mode val={uv_mode} cfl={} rng={}", |
| 11597 | u8::from(cfl_allowed), |
| 11598 | dec.debug_state().0 |
| 11599 | ); |
| 11600 | } |
| 11601 | if (9..=12).contains(&uv_mode) { |
| 11602 | hit!(SMOOTH_UV_HITS); |
| 11603 | } |
| 11604 | let alpha = if cfl_allowed && uv_mode == UV_CFL_PRED { |
| 11605 | Some(read_cfl_alphas(dec, cdfs)) |
| 11606 | } else { |
| 11607 | None |
| 11608 | }; |
| 11609 | let uv_predict_mode = if uv_mode == UV_CFL_PRED { |
| 11610 | DC_PRED |
| 11611 | } else { |
| 11612 | uv_mode |
| 11613 | }; |
| 11614 | let angle_delta_uv = if (V_PRED..=D67_PRED).contains(&uv_mode) { |
| 11615 | hit!(DIRECTIONAL_UV_HITS); |
| 11616 | read_angle_delta(dec, &mut cdfs.angle_delta[uv_mode - V_PRED]) |
| 11617 | } else { |
| 11618 | 0 |
| 11619 | }; |
| 11620 | if angle_delta_uv != 0 { |
| 11621 | hit!(UV_ANGLE_DELTA_HITS); |
| 11622 | hit!(INTRA_IN_INTER_ANGLE_DELTA_UV_HITS); |
| 11623 | } |
| 11624 | // `read_palette_mode_info` (spec 5.11.13), in the position |
| 11625 | // `read_intra_block_mode_info` writes it: after the chroma mode and |
| 11626 | // before `filter_intra`. lane-t900 r21: a `--tune-content=screen` inter |
| 11627 | // frame writes `palette_y_mode`/`palette_uv_mode` for every block whose |
| 11628 | // shape [`palette_bsize_ctx_wh`] admits, intra strips included, so |
| 11629 | // skipping the syntax here desynced the tile at the strip's own symbol. |
| 11630 | // lane-t900 r22: reconstructed, not refused -- the strip's real mode |
| 11631 | // context and colour cache at its own mi position, and the two |
| 11632 | // colour-index maps decoded after `filter_intra` (`av1_visit_palette`), |
| 11633 | // exactly as [`read_intra_mode_rect`] does on the key-frame path. |
| 11634 | let (palette_ctx, palette_cache) = neighbours.palette_ctx_and_cache_mi((mi_r, mi_c)); |
| 11635 | let palette_uv_cache = neighbours.palette_uv_cache_mi((mi_r, mi_c)); |
| 11636 | let mut palette_y_pending: Option<(usize, [u16; 8])> = None; |
| 11637 | let mut palette_uv_pending: Option<(usize, [u16; 8], [u16; 8])> = None; |
| 11638 | if allow_screen_content_tools |
| 11639 | && let Some(bsize_ctx) = palette_bsize_ctx_wh(bw, bh) |
| 11640 | { |
| 11641 | hit!(INTRA_RECT_IN_INTER_SCREEN_HITS); |
| 11642 | let use_palette_y = |
| 11643 | mode == DC_PRED && dec.symbol(&mut cdfs.palette_y_mode[bsize_ctx][palette_ctx]) != 0; |
| 11644 | if use_palette_y { |
| 11645 | let n = 2 + dec.symbol(&mut cdfs.palette_y_size[bsize_ctx]); |
| 11646 | palette_y_pending = Some((n, read_palette_colors_y(dec, n, &palette_cache, fctx))); |
| 11647 | } |
| 11648 | // A 2:1 strip of at least 8x8 is always a chroma reference, so |
| 11649 | // `read_palette_mode_info`'s `xd->is_chroma_ref` guard holds here. |
| 11650 | // lane-av1mono: libaom's actual guard is `num_planes > 1 && |
| 11651 | // uv_mode == UV_DC_PRED && xd->is_chroma_ref` (decodemv.c:602). |
| 11652 | let use_palette_uv = has_chroma |
| 11653 | && uv_mode == DC_PRED |
| 11654 | && dec.symbol(&mut cdfs.palette_uv_mode[usize::from(use_palette_y)]) != 0; |
| 11655 | if crate::envflags::env_flag!("EC_PALSYN") { |
| 11656 | 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); |
| 11657 | } |
| 11658 | if use_palette_uv { |
| 11659 | let n = 2 + dec.symbol(&mut cdfs.palette_uv_size[bsize_ctx]); |
| 11660 | let (u_colors, v_colors) = read_palette_colors_uv(dec, n, &palette_uv_cache, fctx); |
| 11661 | palette_uv_pending = Some((n, u_colors, v_colors)); |
| 11662 | } |
| 11663 | } |
| 11664 | // lane-fiinter (merged on main for the square intra-in-inter arm): |
| 11665 | // `read_filter_intra_mode_info` is the tail of |
| 11666 | // `read_intra_block_mode_info`, after the palette syntax (refused whole |
| 11667 | // above for this shape), and `av1_filter_intra_allowed_bsize` admits the |
| 11668 | // 2:1 strips with both sides <= 32 -- their own CDF rows |
| 11669 | // ([`filter_intra_size_class_rect`]). |
| 11670 | let mut filter_intra = None; |
| 11671 | if mode == DC_PRED |
| 11672 | && fctx.enable_filter_intra_inter.with(std::cell::Cell::get) |
| 11673 | // `av1_filter_intra_allowed` excludes a palette-Y block. |
| 11674 | && palette_y_pending.is_none() |
| 11675 | && let Some(class) = filter_intra_size_class_rect(bw, bh) |
| 11676 | && dec.symbol(&mut cdfs.filter_intra[class]) != 0 |
| 11677 | { |
| 11678 | hit!(FILTER_INTRA_HITS); |
| 11679 | hit!(INTRA_IN_INTER_FILTER_INTRA_HITS); |
| 11680 | filter_intra = Some(dec.symbol(&mut cdfs.filter_intra_mode)); |
| 11681 | } |
| 11682 | // `av1_visit_palette`: Y's colour-index map then the shared chroma one. |
| 11683 | let palette_y = palette_y_pending.map(|(n, colors)| { |
| 11684 | let map = |
| 11685 | decode_color_index_map_wh(dec, cdfs, n, bw, bh, palette_onscreen(mi_r, mi_c, bw, bh, fctx), false); |
| 11686 | hit!(PALETTE_HITS); |
| 11687 | hit!(PALETTE_RECT_HITS); |
| 11688 | hit!(INTRA_IN_INTER_PALETTE_Y_HITS); |
| 11689 | PaletteY { size: n, colors, map } |
| 11690 | }); |
| 11691 | let palette_uv = palette_uv_pending.map(|(n, u_colors, v_colors)| { |
| 11692 | let (cw, ch) = ((bw >> ss_x(fctx)).max(4), (bh >> ss_y(fctx)).max(4)); |
| 11693 | let on = palette_onscreen_uv((bw, bh), palette_onscreen(mi_r, mi_c, bw, bh, fctx), (cw, ch), (ss_x(fctx), ss_y(fctx))); |
| 11694 | let map = decode_color_index_map_wh(dec, cdfs, n, cw, ch, on, true); |
| 11695 | hit!(PALETTE_UV_HITS); |
| 11696 | hit!(INTRA_IN_INTER_PALETTE_UV_HITS); |
| 11697 | PaletteUv { size: n, u_colors, v_colors, map } |
| 11698 | }); |
| 11699 | // The intra-edge filter type is the NEIGHBOUR's mode (libaom |
| 11700 | // `get_filt_type`), luma's off the luma neighbours and chroma's off the |
| 11701 | // chroma ones -- [`decode_block_rect`]'s own pair. |
| 11702 | // lane-t900 r4: the coarse pair is keyed on the 16x16 cell's OWN top-left |
| 11703 | // mi, so both 8x16 halves of a VERT split read the neighbours of the FIRST |
| 11704 | // half. libaom's `above_mbmi`/`left_mbmi` are this block's own |
| 11705 | // (`mi[-mi_stride]` / `mi[-1]`) -- take the mi-exact pair over the coarse |
| 11706 | // one exactly as [`decode_leaf8`] already does (class |
| 11707 | // context-read-from-one-cell). |
| 11708 | let coarse_pair = neighbours.modes_above_left(r, c); |
| 11709 | let (nb_above_mode, nb_left_mode) = neighbours.modes_above_left_mi(mi_r, mi_c, coarse_pair); |
| 11710 | if (nb_above_mode, nb_left_mode) != coarse_pair { |
| 11711 | hit!(EDGE_FILTER_MI_FIX_HITS); |
| 11712 | } |
| 11713 | let smooth_neighbor = is_smooth_mode(nb_above_mode) || is_smooth_mode(nb_left_mode); |
| 11714 | if smooth_neighbor { |
| 11715 | hit!(SMOOTH_LUMA_HITS); |
| 11716 | } |
| 11717 | let smooth_neighbor_uv = neighbours.smooth_uv_neighbour(mi_r, mi_c, r, c, fctx); |
| 11718 | // lane-av1mixloss: the lossless-segment early return (`decodeframe.c:1203`) |
| 11719 | // precedes `TX_MODE_SELECT`, so a lossless-segment intra-rect block inside |
| 11720 | // an inter frame codes no depth symbol either (`xd->lossless` is per |
| 11721 | // segment regardless of frame type; class sweep of the sub8 leaf's fix). |
| 11722 | let depth = if fctx.tx_select_inter.with(std::cell::Cell::get) && !lossless(fctx) { |
| 11723 | // The frame's own `TXFM_CONTEXT` bands -- [`tx_size_context_txfm_rect`]. |
| 11724 | let ctx = tx_size_context_txfm_rect(neighbours, (mi_r, mi_c), bw.min(64), bh.min(64), fctx); |
| 11725 | let d = match tx_cat { |
| 11726 | 1 => dec.symbol(&mut cdfs.tx_size_cat1[ctx]), |
| 11727 | 2 => dec.symbol(&mut cdfs.tx_size_cat2[ctx]), |
| 11728 | _ => dec.symbol(&mut cdfs.tx_size_cat3[ctx]), |
| 11729 | }; |
| 11730 | if step_trace { |
| 11731 | eprintln!( |
| 11732 | "EC_ISTEP mi_row={mi_r} mi_col={mi_c} name=tx_depth val={d} ctx={ctx} cat={tx_cat} rng={}", |
| 11733 | dec.debug_state().0 |
| 11734 | ); |
| 11735 | } |
| 11736 | d |
| 11737 | } else { |
| 11738 | 0 |
| 11739 | }; |
| 11740 | if depth != 0 || lossless(fctx) { |
| 11741 | hit!(TX_DEPTH_HITS); |
| 11742 | // lane-intrasplit r1: a split intra strip in an inter frame walks the |
| 11743 | // key frame's own per-TU path ([`decode_rect_split`] below); what was |
| 11744 | // wrong when this refusal was written was the depth SYMBOL, not the walk. |
| 11745 | hit_do! { |
| 11746 | INTRA_RECT_IN_INTER_SPLIT_TX_HITS.with(|h| { |
| 11747 | let mut v = h.get(); |
| 11748 | v[depth.min(2)] += 1; |
| 11749 | h.set(v); |
| 11750 | }); |
| 11751 | } |
| 11752 | } |
| 11753 | // lane-intrasplit r3: print EVERY intra strip with its depth, not only the |
| 11754 | // split ones -- a `depth=0`-only stream is a witness gap in the SOURCE, and |
| 11755 | // the two are indistinguishable when the diagnostic fires on splits alone. |
| 11756 | if crate::envflags::env_flag!("EC_SPLITSTRIP") { |
| 11757 | eprintln!("EC_SPLITSTRIP mi_row={mi_r} mi_col={mi_c} bw={bw} bh={bh} depth={depth}"); |
| 11758 | } |
| 11759 | let (tx_w, tx_h) = depth_to_tx_wh(bw, bh, depth, fctx); |
| 11760 | // lane-inter16ab r8: libaom runs `set_txfm_ctxs(tx_size, n4_w, n4_h, |
| 11761 | // skip && is_inter, xd)` (decodeframe.c `decode_block`) for EVERY block |
| 11762 | // of an inter frame, intra ones included -- with the skip term 0 it |
| 11763 | // memsets `above_txfm_context` to `tx_size_wide` over the block's width |
| 11764 | // and `left_txfm_context` to `tx_size_high` over its height. This strip |
| 11765 | // wrote neither, so the next block's var-tx `txfm_partition` context read |
| 11766 | // a stale band: on a 192x128 8-bit stream the 16x8 intra strip at |
| 11767 | // mi(14,0) left 8 where libaom has 16, giving ctx 13 instead of 12 at |
| 11768 | // mi(16,0) and desyncing the tile at that symbol. |
| 11769 | txfm_partition_update_rect(neighbours, (mi_r, mi_c), (tx_w, tx_h), (bw, bh)); |
| 11770 | let modes = RectStripModes { |
| 11771 | skip, |
| 11772 | mode, |
| 11773 | angle_delta_y, |
| 11774 | uv_predict_mode, |
| 11775 | angle_delta_uv, |
| 11776 | alpha, |
| 11777 | filter_intra, |
| 11778 | smooth_neighbor, |
| 11779 | smooth_neighbor_uv, |
| 11780 | palette_y: palette_y |
| 11781 | .as_ref() |
| 11782 | .map(|p| p.map.iter().map(|&i| p.colors[i as usize]).collect()), |
| 11783 | palette_uv: palette_uv.as_ref().map(|p| { |
| 11784 | ( |
| 11785 | p.map.iter().map(|&i| p.u_colors[i as usize]).collect(), |
| 11786 | p.map.iter().map(|&i| p.v_colors[i as usize]).collect(), |
| 11787 | ) |
| 11788 | }), |
| 11789 | }; |
| 11790 | decode_rect_split( |
| 11791 | dec, |
| 11792 | cdfs, |
| 11793 | neighbours, |
| 11794 | (mi_r, mi_c), |
| 11795 | bw, |
| 11796 | bh, |
| 11797 | tx_w, |
| 11798 | tx_h, |
| 11799 | &modes, |
| 11800 | y, |
| 11801 | u, |
| 11802 | v, |
| 11803 | base_q_idx, |
| 11804 | fctx.reduced_tx_set_inter.with(std::cell::Cell::get), fctx, |
| 11805 | )?; |
| 11806 | // `set_txfm_ctxs` (libaom `decodeframe.c:1078`, at the end of |
| 11807 | // `decode_token_recon_block`): an INTRA block records its TRANSFORM size in |
| 11808 | // both `TXFM_CONTEXT` bands -- the above row over the block's width, the |
| 11809 | // left column over its height. This arm wrote neither before lane-intrasplit |
| 11810 | // r1, so every later block in the frame read a stale `tx_size`/`txfm_split` |
| 11811 | // context across the strip's footprint. |
| 11812 | for i in 0..bw / MI { |
| 11813 | if let Some(cell) = neighbours.above_txfm.get_mut(mi_c + i) { |
| 11814 | *cell = tx_w as u8; |
| 11815 | } |
| 11816 | } |
| 11817 | for i in 0..bh / MI { |
| 11818 | if let Some(cell) = neighbours.left_txfm.get_mut(mi_r + i) { |
| 11819 | *cell = tx_h as u8; |
| 11820 | } |
| 11821 | } |
| 11822 | // The strip's own palette state into the mi-granular neighbour bands, so |
| 11823 | // the next block's colour cache and `palette_y_mode` context are libaom's. |
| 11824 | record_strip_palette( |
| 11825 | neighbours, |
| 11826 | (mi_r, mi_c), |
| 11827 | bw, |
| 11828 | bh, |
| 11829 | palette_y.as_ref().map_or(0, |p| p.size), |
| 11830 | palette_y.as_ref().map_or([0u16; 8], |p| p.colors), |
| 11831 | palette_uv.as_ref().map_or(0, |p| p.size), |
| 11832 | palette_uv.as_ref().map_or([0u16; 8], |p| p.u_colors), |
| 11833 | ); |
| 11834 | let class = match bw.max(bh) { |
| 11835 | 64 => 0, |
| 11836 | 32 => 1, |
| 11837 | _ => 2, |
| 11838 | }; |
| 11839 | hit_do! { |
| 11840 | INTRA_RECT_IN_INTER_HITS.with(|h| { |
| 11841 | let mut v = h.get(); |
| 11842 | v[class] += 1; |
| 11843 | h.set(v); |
| 11844 | }); |
| 11845 | } |
| 11846 | Ok(()) |
| 11847 | } |
| 11848 | |
| 11849 | /// Reconstructs one rectangular INTRABC block, `bw`x`bh` pixels at the real |
| 11850 | /// mi `(mi_r, mi_c)` (lane-av1-intrabc). |
| 11851 | /// |
| 11852 | /// libaom treats an intrabc block as INTER for everything except the mode-info |
| 11853 | /// read itself: `is_inter_block` is `is_intrabc_block(mbmi) || ref > INTRA` |
| 11854 | /// (`blockd.h:373`), so |
| 11855 | /// |
| 11856 | /// * `parse_decode_block` reads the transform size through the INTER var-tx |
| 11857 | /// tree (`read_tx_size_vartx`) -- [`read_block_tx_size_rect`], whose |
| 11858 | /// `txfm_partition` context comes off the real `TXFM_CONTEXT` bands and |
| 11859 | /// whose leaves publish their own resolved size back; |
| 11860 | /// * `predict_inter_block` predicts the WHOLE block once, from the CURRENT |
| 11861 | /// frame's own pre-loop-filter reconstruction, at the coded block vector |
| 11862 | /// with the BILINEAR kernel `read_intrabc_info` forces (`decodemv.c:913`); |
| 11863 | /// * `decode_token_recon_block`'s inter branch then reads one residual per |
| 11864 | /// transform unit, through the INTER coefficient sets |
| 11865 | /// ([`read_inter_plane_rect`]/[`read_inter_plane`]) with the chroma plane |
| 11866 | /// inheriting the colocated luma unit's coded `tx_type`. |
| 11867 | /// |
| 11868 | /// Text order is libaom's: transform tree, prediction, luma residual, chroma |
| 11869 | /// residual, bookkeeping. A skipped block codes no residual at all -- the |
| 11870 | /// prediction is the reconstruction. |
| 11871 | #[allow(clippy::too_many_arguments)] |
| 11872 | fn decode_intrabc_rect( |
| 11873 | dec: &mut SymbolDecoder, |
| 11874 | cdfs: &mut Cdfs, |
| 11875 | neighbours: &mut Neighbours, |
| 11876 | (mi_r, mi_c): (usize, usize), |
| 11877 | (bw, bh): (usize, usize), |
| 11878 | skip: bool, |
| 11879 | (dv_row, dv_col): (i32, i32), |
| 11880 | y: &mut PlaneBuf<'static>, |
| 11881 | u: &mut PlaneBuf<'static>, |
| 11882 | v: &mut PlaneBuf<'static>, |
| 11883 | base_q_idx: u8, |
| 11884 | fctx: &crate::decode::FrameCtx, |
| 11885 | ) -> Result<()> { |
| 11886 | hit!(INTRABC_RECT_HITS); |
| 11887 | // [horz, vert]: see `INTRABC_RECT_CODED_HITS` / `INTRABC_RECT_SKIPPED_HITS`. |
| 11888 | let idx = usize::from(bh > bw); |
| 11889 | if skip { |
| 11890 | INTRABC_RECT_SKIPPED_HITS.with(|c| { |
| 11891 | let mut seen = c.get(); |
| 11892 | seen[idx] += 1; |
| 11893 | c.set(seen); |
| 11894 | }); |
| 11895 | } else { |
| 11896 | INTRABC_RECT_CODED_HITS.with(|c| { |
| 11897 | let mut seen = c.get(); |
| 11898 | seen[idx] += 1; |
| 11899 | c.set(seen); |
| 11900 | }); |
| 11901 | } |
| 11902 | let (px, py) = (mi_c * MI, mi_r * MI); |
| 11903 | let (cpx, cpy) = (px / 2, py / 2); |
| 11904 | // `av1_get_max_uv_txsize(bw x bh)`: 4:2:0 halves the footprint and the |
| 11905 | // halved shape's own `max_txsize_rect_lookup` entry IS that size for every |
| 11906 | // shape this helper is called at, so the uv plane block is ONE unit -- |
| 11907 | // the same call `decode_token_recon_block`'s own chroma loop makes. |
| 11908 | let (cw, ch) = (bw / 2, bh / 2); |
| 11909 | // lane-av1-intrabc r2: the square stride the proven INTER path uses |
| 11910 | // (`side`). The residual grids, the prediction windows AND |
| 11911 | // `TxParams::stride` are all laid out at it -- the block's own width |
| 11912 | // panicked `TxParams::run` on every coded VERT strip (8x16/16x32: `h` |
| 11913 | // exceeds the stride). |
| 11914 | let side = bw.max(bh); |
| 11915 | let cside = cw.max(ch); |
| 11916 | // `av1_calc_mi_size`'s 8-px-aligned grid: a block hanging over the right |
| 11917 | // or bottom frame edge reads no transform-tree symbol for the units |
| 11918 | // outside it. |
| 11919 | let mi_cols = 2 * y.true_width.div_ceil(8); |
| 11920 | let mi_rows = 2 * y.true_height.div_ceil(8); |
| 11921 | let leaves = read_block_tx_size_rect( |
| 11922 | dec, |
| 11923 | cdfs, |
| 11924 | neighbours, |
| 11925 | (mi_r, mi_c), |
| 11926 | (bw, bh), |
| 11927 | (mi_cols, mi_rows), |
| 11928 | skip, fctx, |
| 11929 | )?; |
| 11930 | if leaves.is_some() { |
| 11931 | hit!(INTRABC_RECT_VARTX_HITS); |
| 11932 | } |
| 11933 | // The whole-block prediction: `predict_inter_block` runs once, before any |
| 11934 | // coefficient is read. `flush_recon` first -- the source is this frame's |
| 11935 | // own reconstruction, including the writes this superblock has queued. |
| 11936 | flush_recon(fctx, y, u, v); |
| 11937 | // `mc::predict_with_filter` writes a DENSE window; the residual machinery |
| 11938 | // reads it at the square stride, so re-lay it -- the same step the inter |
| 11939 | // path takes for its own square prediction buffer. |
| 11940 | let mut pred_y_dense = vec![0u16; bw * bh]; |
| 11941 | mc::predict_with_filter( |
| 11942 | &y.data, y.width, y.true_width, y.true_height, |
| 11943 | mv_to_q4(px, dv_col, 0), mv_to_q4(py, dv_row, 0), |
| 11944 | bw, bh, mc::InterpFilterKind::Bilinear, &mut pred_y_dense, fctx, |
| 11945 | ); |
| 11946 | let mut pred_y = vec![0u16; side * side]; |
| 11947 | for row in 0..bh { |
| 11948 | pred_y[row * side..][..bw].copy_from_slice(&pred_y_dense[row * bw..][..bw]); |
| 11949 | } |
| 11950 | let mut pred_u_dense = vec![0u16; cw * ch]; |
| 11951 | mc::predict_with_filter( |
| 11952 | &u.data, u.width, u.true_width, u.true_height, |
| 11953 | mv_to_q4(cpx, dv_col, ss_x(fctx)), mv_to_q4(cpy, dv_row, ss_y(fctx)), |
| 11954 | cw, ch, mc::InterpFilterKind::Bilinear, &mut pred_u_dense, fctx, |
| 11955 | ); |
| 11956 | let mut pred_u = vec![0u16; cside * cside]; |
| 11957 | for row in 0..ch { |
| 11958 | pred_u[row * cside..][..cw].copy_from_slice(&pred_u_dense[row * cw..][..cw]); |
| 11959 | } |
| 11960 | let mut pred_v_dense = vec![0u16; cw * ch]; |
| 11961 | mc::predict_with_filter( |
| 11962 | &v.data, v.width, v.true_width, v.true_height, |
| 11963 | mv_to_q4(cpx, dv_col, ss_x(fctx)), mv_to_q4(cpy, dv_row, ss_y(fctx)), |
| 11964 | cw, ch, mc::InterpFilterKind::Bilinear, &mut pred_v_dense, fctx, |
| 11965 | ); |
| 11966 | let mut pred_v = vec![0u16; cside * cside]; |
| 11967 | for row in 0..ch { |
| 11968 | pred_v[row * cside..][..cw].copy_from_slice(&pred_v_dense[row * cw..][..cw]); |
| 11969 | } |
| 11970 | let reduced = fctx.reduced_tx_set_inter.with(std::cell::Cell::get); |
| 11971 | let luma_tx_type: TxType; |
| 11972 | let (luma_grid, u_grid, v_grid); |
| 11973 | if skip { |
| 11974 | // No residual syntax at all: the prediction IS the block. |
| 11975 | // Residual stride is `side` (and `cside`), not `bw`: reconstruct_mc_rect indexes |
| 11976 | // `residual[row * side .. row*side + w]` for row in 0..h, so a skipped VERT strip |
| 11977 | // (bh > bw) overruns a `bw * bh` slice. Bug-for-bug with the inter skip arm. |
| 11978 | push_mc_rect(0, px, py, side, bw, bh, Pred::Inline(&pred_y, side), &ZERO_RESIDUAL[..side * side], fctx); |
| 11979 | push_mc_rect(1, cpx, cpy, cside, cw, ch, Pred::Inline(&pred_u, cside), &ZERO_RESIDUAL[..cside * cside], fctx); |
| 11980 | push_mc_rect(2, cpx, cpy, cside, cw, ch, Pred::Inline(&pred_v, cside), &ZERO_RESIDUAL[..cside * cside], fctx); |
| 11981 | if let Some(ls) = leaves.as_ref() { |
| 11982 | // lane-av1-loss64, merged: the same reset [`decode_intrabc_owned_rect`]'s |
| 11983 | // skip arm performs -- libaom `av1_reset_entropy_context` |
| 11984 | // (`if (mbmi->skip_txfm)`, decodeframe.c:1262 -> blockd.c:58): a |
| 11985 | // SKIPPED block zeroes its whole entropy-band footprint. This |
| 11986 | // helper serves `decode_block_rect4`'s 1:4 strips on this tree, |
| 11987 | // so a skipped lossless strip whose `leaves` resolved (every |
| 11988 | // lossless frame -- `read_block_tx_size_rect`'s lossless arm |
| 11989 | // returns the 4x4 grid before it looks at `skip`) left the |
| 11990 | // PREVIOUS occupant's luma bands standing and the NEXT block's |
| 11991 | // partition parse diverged from libaom's: the 320x242 lossless |
| 11992 | // sb128 gate read a phantom unskipped 8x32 intrabc strip at |
| 11993 | // mi(56,66) right after the skipped 8x16 one at mi(48,66). |
| 11994 | hit!(SKIP_LOSSLESS_BAND_RESET_HITS); |
| 11995 | for &(row, col, tw, th) in ls { |
| 11996 | neighbours.record_mi_luma_rect( |
| 11997 | (mi_r + row, mi_c + col), |
| 11998 | tw, |
| 11999 | th, |
| 12000 | &ZERO_RESIDUAL[..tw * th], |
| 12001 | ); |
| 12002 | } |
| 12003 | } |
| 12004 | luma_tx_type = TxType::DctDct; |
| 12005 | luma_grid = Grid::Zero(side * side); |
| 12006 | u_grid = Grid::Zero(cside * cside); |
| 12007 | v_grid = Grid::Zero(cside * cside); |
| 12008 | } else { |
| 12009 | match leaves.as_ref() { |
| 12010 | None => { |
| 12011 | // One whole-block rectangular transform unit. |
| 12012 | // lane-av1422mi24: at ss (1,0) the chroma above span is |
| 12013 | // `bw >> 1` chroma cells spread over `bw` luma mi columns -- |
| 12014 | // the per-mi gather counts every chroma column's whole-unit |
| 12015 | // dc sign twice (t422 frame 1, the mi(26,60) intra-in-inter |
| 12016 | // strip's U dc_sign read ctx 1 where the oracle's cell sum |
| 12017 | // is 0 -> ctx 0). Luma keeps the per-mi gather; the two |
| 12018 | // chroma planes take |
| 12019 | // [`Neighbours::around_mi_422_chroma`]'s every-second |
| 12020 | // above-cell sampling, like every other 4:2:2 chroma gather. |
| 12021 | let mut around = neighbours.around_mi_rect((mi_r, mi_c), bw, bh); |
| 12022 | if ss_x(fctx) == 1 && ss_y(fctx) == 0 { |
| 12023 | let c = neighbours.around_mi_422_chroma((mi_r, mi_c), bw, bh); |
| 12024 | around[1] = c[1]; |
| 12025 | around[2] = c[2]; |
| 12026 | } |
| 12027 | let (grid, tx_type) = read_inter_plane_rect( |
| 12028 | dec, |
| 12029 | cdfs, |
| 12030 | rect_inter_luma_set(bw, bh, fctx)?, |
| 12031 | (bw, bh), |
| 12032 | side, |
| 12033 | 0, |
| 12034 | around[0], |
| 12035 | 0, |
| 12036 | y, |
| 12037 | px, |
| 12038 | py, |
| 12039 | Pred::Inline(&pred_y, side), |
| 12040 | None, |
| 12041 | None, fctx, |
| 12042 | )?; |
| 12043 | luma_tx_type = tx_type; |
| 12044 | luma_grid = embed_grid_stride(&grid, bw, bh, side); |
| 12045 | let (ug, _) = read_inter_plane_rect( |
| 12046 | dec, |
| 12047 | cdfs, |
| 12048 | rect_inter_chroma_set(cw, ch)?, |
| 12049 | (cw, ch), |
| 12050 | cside, |
| 12051 | 1, |
| 12052 | around[1], |
| 12053 | 0, |
| 12054 | u, |
| 12055 | cpx, |
| 12056 | cpy, |
| 12057 | Pred::Inline(&pred_u, cside), |
| 12058 | Some(luma_tx_type), |
| 12059 | None, fctx, |
| 12060 | )?; |
| 12061 | u_grid = embed_grid_stride(&ug, cw, ch, cside); |
| 12062 | let (vg, _) = read_inter_plane_rect( |
| 12063 | dec, |
| 12064 | cdfs, |
| 12065 | rect_inter_chroma_set(cw, ch)?, |
| 12066 | (cw, ch), |
| 12067 | cside, |
| 12068 | 2, |
| 12069 | around[2], |
| 12070 | 0, |
| 12071 | v, |
| 12072 | cpx, |
| 12073 | cpy, |
| 12074 | Pred::Inline(&pred_v, cside), |
| 12075 | Some(luma_tx_type), |
| 12076 | None, fctx, |
| 12077 | )?; |
| 12078 | v_grid = embed_grid_stride(&vg, cw, ch, cside); |
| 12079 | } |
| 12080 | Some(leaves) => { |
| 12081 | // P2 (lane-av1-intrabc r2): libaom `get_transform_size` applies |
| 12082 | // the split override to LUMA only, per leaf -- the step the |
| 12083 | // proven inter var-tx path applies right after its own residual |
| 12084 | // loop ([`Neighbours::fill_lf_grid_leaf_luma`]). Without it the |
| 12085 | // leaf's own transform edges are never filtered. |
| 12086 | for &(row, col, tw, th) in leaves.iter() { |
| 12087 | neighbours.fill_lf_grid_leaf_luma( |
| 12088 | (mi_r + row, mi_c + col), |
| 12089 | tw / MI, |
| 12090 | th / MI, |
| 12091 | tw as u8, |
| 12092 | th as u8, |
| 12093 | ); |
| 12094 | } |
| 12095 | // One residual per transform unit, in the order the tree coded |
| 12096 | // them; each unit reads its own neighbour-magnitude context off |
| 12097 | // the units already decoded before it, and every unit adds onto |
| 12098 | // its own window of the block's single prediction. |
| 12099 | let mut first_tx_type = TxType::DctDct; |
| 12100 | for (idx, &(row, col, tw, th)) in leaves.iter().enumerate() { |
| 12101 | let tu_mi = (mi_r + row, mi_c + col); |
| 12102 | let (tu_px, tu_py) = (px + col * MI, py + row * MI); |
| 12103 | let tu_pred = Pred::Inline(&pred_y, side).offset(row * MI * side + col * MI); |
| 12104 | let (tu_grid, tu_tx_type) = if tw == th { |
| 12105 | let scan = default_scan(tw.min(32)); |
| 12106 | let tu_skip_ctx = neighbours.luma_skip_ctx(tu_mi, tw / MI); |
| 12107 | read_inter_plane( |
| 12108 | dec, |
| 12109 | cdfs, |
| 12110 | inter_txbset_for(tw, reduced), |
| 12111 | scan, |
| 12112 | 0, |
| 12113 | neighbours.around_mi(tu_mi, tw)[0], |
| 12114 | 0, |
| 12115 | y, |
| 12116 | tu_px, |
| 12117 | tu_py, |
| 12118 | tw, |
| 12119 | base_q_idx, |
| 12120 | tu_pred, |
| 12121 | None, |
| 12122 | Some(tu_skip_ctx), fctx, |
| 12123 | )? |
| 12124 | } else { |
| 12125 | let tu_skip_ctx = neighbours.luma_skip_ctx_rect(tu_mi, tw / MI, th / MI); |
| 12126 | read_inter_plane_rect( |
| 12127 | dec, |
| 12128 | cdfs, |
| 12129 | rect_inter_luma_set(tw, th, fctx)?, |
| 12130 | (tw, th), |
| 12131 | side, |
| 12132 | 0, |
| 12133 | neighbours.around_mi_rect(tu_mi, tw, th)[0], |
| 12134 | 0, |
| 12135 | y, |
| 12136 | tu_px, |
| 12137 | tu_py, |
| 12138 | tu_pred, |
| 12139 | None, |
| 12140 | Some(tu_skip_ctx), fctx, |
| 12141 | )? |
| 12142 | }; |
| 12143 | if idx == 0 { |
| 12144 | first_tx_type = tu_tx_type; |
| 12145 | } |
| 12146 | neighbours.record_mi_luma_rect(tu_mi, tw, th, &tu_grid); |
| 12147 | } |
| 12148 | // `av1_get_tx_type`'s chroma lookup lands on the colocated |
| 12149 | // LUMA unit, which for a split block is the top-left one. |
| 12150 | luma_tx_type = first_tx_type; |
| 12151 | // lane-av1422mi24: same 4:2:2 chroma gather rule as the |
| 12152 | // `None` arm above -- the per-mi rect gather double-counts |
| 12153 | // each chroma column's whole-unit dc sign at ss (1,0). |
| 12154 | let mut around = neighbours.around_mi_rect((mi_r, mi_c), bw, bh); |
| 12155 | if ss_x(fctx) == 1 && ss_y(fctx) == 0 { |
| 12156 | let c = neighbours.around_mi_422_chroma((mi_r, mi_c), bw, bh); |
| 12157 | around[1] = c[1]; |
| 12158 | around[2] = c[2]; |
| 12159 | } |
| 12160 | let (ug, _) = read_inter_plane_rect( |
| 12161 | dec, |
| 12162 | cdfs, |
| 12163 | rect_inter_chroma_set(cw, ch)?, |
| 12164 | (cw, ch), |
| 12165 | cside, |
| 12166 | 1, |
| 12167 | around[1], |
| 12168 | 0, |
| 12169 | u, |
| 12170 | cpx, |
| 12171 | cpy, |
| 12172 | Pred::Inline(&pred_u, cside), |
| 12173 | Some(luma_tx_type), |
| 12174 | None, fctx, |
| 12175 | )?; |
| 12176 | u_grid = embed_grid_stride(&ug, cw, ch, cside); |
| 12177 | let (vg, _) = read_inter_plane_rect( |
| 12178 | dec, |
| 12179 | cdfs, |
| 12180 | rect_inter_chroma_set(cw, ch)?, |
| 12181 | (cw, ch), |
| 12182 | cside, |
| 12183 | 2, |
| 12184 | around[2], |
| 12185 | 0, |
| 12186 | v, |
| 12187 | cpx, |
| 12188 | cpy, |
| 12189 | Pred::Inline(&pred_v, cside), |
| 12190 | Some(luma_tx_type), |
| 12191 | None, fctx, |
| 12192 | )?; |
| 12193 | v_grid = embed_grid_stride(&vg, cw, ch, cside); |
| 12194 | luma_grid = Grid::Zero(side * side); |
| 12195 | } |
| 12196 | } |
| 12197 | } |
| 12198 | let _ = luma_tx_type; |
| 12199 | // Bookkeeping. An intrabc block publishes mode `DC_PRED` everywhere the |
| 12200 | // next block reads a neighbour mode, its own block size in both `TXFM` |
| 12201 | // bands (already done by `read_block_tx_size_rect`), a zeroed palette |
| 12202 | // state (a stale entry would feed the next block's colour cache), and its |
| 12203 | // DV into the intrabc mi grid the next block's DV predictor reads. |
| 12204 | if leaves.is_some() { |
| 12205 | neighbours.record_split_luma_rect_mi( |
| 12206 | (mi_r, mi_c), bw, bh, DC_PRED, DC_PRED, [&u_grid, &v_grid], |
| 12207 | ); |
| 12208 | } else { |
| 12209 | neighbours.record_rect_mi( |
| 12210 | (mi_r, mi_c), bw, bh, DC_PRED, DC_PRED, &[&luma_grid, &u_grid, &v_grid], |
| 12211 | ); |
| 12212 | } |
| 12213 | neighbours.record_palette_y_rect((mi_r, mi_c), bw, bh, 0, [0u16; 8]); |
| 12214 | neighbours.record_palette_uv_rect((mi_r, mi_c), bw, bh, 0, [0u16; 8]); |
| 12215 | neighbours.fill_skip_grid_rect((mi_r, mi_c), bw / MI, bh / MI, skip); |
| 12216 | neighbours.fill_lf_grid_rect( |
| 12217 | (mi_r, mi_c), |
| 12218 | bw / MI, |
| 12219 | bh / MI, |
| 12220 | bw.min(64) as u8, |
| 12221 | bh.min(64) as u8, |
| 12222 | 0, fctx, |
| 12223 | ); |
| 12224 | record_intrabc_mi_rect( |
| 12225 | mi_r, |
| 12226 | mi_c, |
| 12227 | bw / MI, |
| 12228 | bh / MI, |
| 12229 | Some((dv_row, dv_col)), |
| 12230 | neighbours, |
| 12231 | fctx, |
| 12232 | ); |
| 12233 | Ok(()) |
| 12234 | } |
| 12235 | |
| 12236 | /// lane-av1-ibcrect: reconstruction for a `use_intrabc` block on a 128-root |
| 12237 | /// HORZ/VERT strip (`BLOCK_128X64` / `BLOCK_64X128`) -- the shape |
| 12238 | /// `decode_block_128rect` refused by name before this port. libaom decodes |
| 12239 | /// such a block through the INTER machinery (an intrabc block |
| 12240 | /// `is_inter_block`): `predict_inter_block` copies the whole block from this |
| 12241 | /// frame's own reconstruction at the integer block vector once, then |
| 12242 | /// `decode_token_recon_block`'s inter arm walks 64x64 "mu" chunks -- |
| 12243 | /// per chunk every plane, luma units at the block's own resolved transform |
| 12244 | /// (`read_block_tx_size_rect`, TX_64X64 roots) and chroma at |
| 12245 | /// `av1_get_max_uv_txsize(BLOCK_128X64)` = TX_32X32, one unit per plane per |
| 12246 | /// chunk at 4:2:0. Chroma never codes its own `tx_type`; each unit inherits |
| 12247 | /// the coded type of the luma leaf covering its quadrant (`av1_get_tx_type`, |
| 12248 | /// blockd.h:1291). `mode`/`uv_mode` read as `DC_PRED`/`UV_DC_PRED` back in |
| 12249 | /// [`read_intra_mode_rect`]; no palette/filter-intra/CFL symbol exists here. |
| 12250 | #[allow(clippy::too_many_arguments)] |
| 12251 | fn decode_intrabc_128rect( |
| 12252 | dec: &mut SymbolDecoder, |
| 12253 | cdfs: &mut Cdfs, |
| 12254 | neighbours: &mut Neighbours, |
| 12255 | (mi_r, mi_c): (usize, usize), |
| 12256 | (bw, bh): (usize, usize), |
| 12257 | skip: bool, |
| 12258 | (dv_row, dv_col): (i32, i32), |
| 12259 | y: &mut PlaneBuf<'static>, |
| 12260 | u: &mut PlaneBuf<'static>, |
| 12261 | v: &mut PlaneBuf<'static>, |
| 12262 | base_q_idx: u8, |
| 12263 | fctx: &crate::decode::FrameCtx, |
| 12264 | ) -> Result<()> { |
| 12265 | hit!(INTRABC_128RECT_HITS); |
| 12266 | if crate::envflags::env_flag!("EC_AV1_TRACE") { |
| 12267 | let (rng, _) = dec.debug_state(); |
| 12268 | eprintln!( |
| 12269 | "EC_IBC128 mi_row={mi_r} mi_col={mi_c} bw={bw} bh={bh} skip={skip} dv=({dv_row},{dv_col}) rng={rng}" |
| 12270 | ); |
| 12271 | } |
| 12272 | let (px, py) = (mi_c * MI, mi_r * MI); |
| 12273 | let (cpx, cpy) = (px / 2, py / 2); |
| 12274 | let (cw, ch) = (bw / 2, bh / 2); |
| 12275 | // The square strides the proven INTER path lays its residual grids, |
| 12276 | // prediction windows and `TxParams` at (see [`decode_intrabc_rect`]'s r2 |
| 12277 | // note): 128/64 here. |
| 12278 | let side = bw.max(bh); |
| 12279 | let cside = cw.max(ch); |
| 12280 | // `av1_calc_mi_size`'s 8-px-aligned grid: units past the frame edge code |
| 12281 | // nothing (`decode_reconstruct_tx`'s max_blocks_* early return). |
| 12282 | let mi_cols = 2 * y.true_width.div_ceil(8); |
| 12283 | let mi_rows = 2 * y.true_height.div_ceil(8); |
| 12284 | // An intrabc block is is_inter_block: the tx tree is the INTER one, and |
| 12285 | // [`read_block_tx_size_rect`]'s 128-root branch already carries the |
| 12286 | // LARGEST (no symbol, two TX_64X64 units), TX_MODE_SELECT (var-tx tree |
| 12287 | // rooted at TX_64X64), skip (band stamp only) and lossless (TX_4X4 grid) |
| 12288 | // arms (lane-sb128c r7). |
| 12289 | let leaves = read_block_tx_size_rect( |
| 12290 | dec, |
| 12291 | cdfs, |
| 12292 | neighbours, |
| 12293 | (mi_r, mi_c), |
| 12294 | (bw, bh), |
| 12295 | (mi_cols, mi_rows), |
| 12296 | skip, fctx, |
| 12297 | )?; |
| 12298 | // Whole-block prediction, once, before any coefficient is read |
| 12299 | // (`predict_inter_block`); the source is this frame's own |
| 12300 | // reconstruction, including the writes this superblock has queued. |
| 12301 | flush_recon(fctx, y, u, v); |
| 12302 | let mut pred_y_dense = vec![0u16; bw * bh]; |
| 12303 | mc::predict_with_filter( |
| 12304 | &y.data, y.width, y.true_width, y.true_height, |
| 12305 | mv_to_q4(px, dv_col, 0), mv_to_q4(py, dv_row, 0), |
| 12306 | bw, bh, mc::InterpFilterKind::Bilinear, &mut pred_y_dense, fctx, |
| 12307 | ); |
| 12308 | let mut pred_y = vec![0u16; side * side]; |
| 12309 | for row in 0..bh { |
| 12310 | pred_y[row * side..][..bw].copy_from_slice(&pred_y_dense[row * bw..][..bw]); |
| 12311 | } |
| 12312 | let mut pred_u_dense = vec![0u16; cw * ch]; |
| 12313 | mc::predict_with_filter( |
| 12314 | &u.data, u.width, u.true_width, u.true_height, |
| 12315 | mv_to_q4(cpx, dv_col, ss_x(fctx)), mv_to_q4(cpy, dv_row, ss_y(fctx)), |
| 12316 | cw, ch, mc::InterpFilterKind::Bilinear, &mut pred_u_dense, fctx, |
| 12317 | ); |
| 12318 | let mut pred_u = vec![0u16; cside * cside]; |
| 12319 | for row in 0..ch { |
| 12320 | pred_u[row * cside..][..cw].copy_from_slice(&pred_u_dense[row * cw..][..cw]); |
| 12321 | } |
| 12322 | let mut pred_v_dense = vec![0u16; cw * ch]; |
| 12323 | mc::predict_with_filter( |
| 12324 | &v.data, v.width, v.true_width, v.true_height, |
| 12325 | mv_to_q4(cpx, dv_col, ss_x(fctx)), mv_to_q4(cpy, dv_row, ss_y(fctx)), |
| 12326 | cw, ch, mc::InterpFilterKind::Bilinear, &mut pred_v_dense, fctx, |
| 12327 | ); |
| 12328 | let mut pred_v = vec![0u16; cside * cside]; |
| 12329 | for row in 0..ch { |
| 12330 | pred_v[row * cside..][..cw].copy_from_slice(&pred_v_dense[row * cw..][..cw]); |
| 12331 | } |
| 12332 | let (luma_grid, u_grid, v_grid); |
| 12333 | // lane-av1-ibcrect: whether the coded arm stamped its CHROMA neighbour |
| 12334 | // contexts per transform unit -- the replay after this fn's end-of-block |
| 12335 | // record restores them (the record itself re-stamps both planes with ONE |
| 12336 | // composed state, class `override-slot-on-one-arm`). |
| 12337 | let mut mu_chroma = false; |
| 12338 | if skip { |
| 12339 | // No residual syntax at all: the prediction IS the block. Strides are |
| 12340 | // `side`/`cside`, not `bw` -- `reconstruct_mc_rect` indexes |
| 12341 | // `row * stride + w` for row in 0..h (the r5 VERT-strip overrun). |
| 12342 | push_mc_rect(0, px, py, side, bw, bh, Pred::Inline(&pred_y, side), &ZERO_RESIDUAL[..side * side], fctx); |
| 12343 | push_mc_rect(1, cpx, cpy, cside, cw, ch, Pred::Inline(&pred_u, cside), &ZERO_RESIDUAL[..cside * cside], fctx); |
| 12344 | push_mc_rect(2, cpx, cpy, cside, cw, ch, Pred::Inline(&pred_v, cside), &ZERO_RESIDUAL[..cside * cside], fctx); |
| 12345 | if let Some(ls) = leaves.as_ref() { |
| 12346 | // A LOSSLESS frame's `read_block_tx_size_rect` resolves the TX_4X4 |
| 12347 | // grid before it looks at `skip`; the skipped block still zeroes |
| 12348 | // its entropy-band footprint over every unit (libaom |
| 12349 | // `av1_reset_entropy_context` under `if (mbmi->skip_txfm)`) -- |
| 12350 | // the same arm [`decode_intrabc_rect`]'s loss64 merge added. |
| 12351 | hit!(SKIP_LOSSLESS_BAND_RESET_HITS); |
| 12352 | for &(row, col, tw, th) in ls { |
| 12353 | neighbours.record_mi_luma_rect( |
| 12354 | (mi_r + row, mi_c + col), |
| 12355 | tw, |
| 12356 | th, |
| 12357 | &ZERO_RESIDUAL[..tw * th], |
| 12358 | ); |
| 12359 | } |
| 12360 | } |
| 12361 | luma_grid = Grid::Zero(side * side); |
| 12362 | u_grid = Grid::Zero(cside * cside); |
| 12363 | v_grid = Grid::Zero(cside * cside); |
| 12364 | } else { |
| 12365 | let leaves = leaves.expect( |
| 12366 | "read_block_tx_size_rect returns None only for skip below 128 at this shape", |
| 12367 | ); |
| 12368 | // libaom `get_transform_size`'s split override applies to LUMA only, |
| 12369 | // per leaf -- each unit's own transform edges get filtered |
| 12370 | // ([`decode_intrabc_rect`]'s P2, the proven inter path's step). |
| 12371 | for &(row, col, tw, th) in leaves.iter() { |
| 12372 | neighbours.fill_lf_grid_leaf_luma( |
| 12373 | (mi_r + row, mi_c + col), |
| 12374 | tw / MI, |
| 12375 | th / MI, |
| 12376 | tw as u8, |
| 12377 | th as u8, |
| 12378 | ); |
| 12379 | } |
| 12380 | let reduced = fctx.reduced_tx_set_inter.with(std::cell::Cell::get); |
| 12381 | let mut u_units: Vec<i32> = Vec::new(); |
| 12382 | let mut v_units: Vec<i32> = Vec::new(); |
| 12383 | let mut leaf_tx_types: Vec<(usize, usize, usize, usize, TxType)> = Vec::new(); |
| 12384 | // Frame-edge clip of the mu walk (`max_block_wide/high`): a unit whose |
| 12385 | // top-left sample is outside the frame codes nothing. |
| 12386 | let max_w_mi = (bw / MI).min(mi_cols.saturating_sub(mi_c)); |
| 12387 | let max_h_mi = (bh / MI).min(mi_rows.saturating_sub(mi_r)); |
| 12388 | for (idx, &(row, col, tw, th)) in leaves.iter().enumerate() { |
| 12389 | debug_assert_eq!(tw, th, "a square TX_64X64 tree root only yields square leaves"); |
| 12390 | let tu_mi = (mi_r + row, mi_c + col); |
| 12391 | let (tu_px, tu_py) = (px + col * MI, py + row * MI); |
| 12392 | if tu_px >= y.true_width || tu_py >= y.true_height { |
| 12393 | continue; |
| 12394 | } |
| 12395 | let tu_pred = Pred::Inline(&pred_y, side).offset(row * MI * side + col * MI); |
| 12396 | let scan = default_scan(tw.min(32)); |
| 12397 | let tu_skip_ctx = neighbours.luma_skip_ctx(tu_mi, tw / MI); |
| 12398 | let (tu_grid, tu_tx_type) = read_inter_plane( |
| 12399 | dec, |
| 12400 | cdfs, |
| 12401 | inter_txbset_for(tw, reduced), |
| 12402 | &scan, |
| 12403 | 0, |
| 12404 | neighbours.around_mi(tu_mi, tw)[0], |
| 12405 | 0, |
| 12406 | y, |
| 12407 | tu_px, |
| 12408 | tu_py, |
| 12409 | tw, |
| 12410 | base_q_idx, |
| 12411 | tu_pred, |
| 12412 | None, |
| 12413 | Some(tu_skip_ctx), fctx, |
| 12414 | )?; |
| 12415 | leaf_tx_types.push((row, col, tw, th, tu_tx_type)); |
| 12416 | neighbours.record_mi_luma_rect(tu_mi, tw, th, &tu_grid); |
| 12417 | // libaom `decode_token_recon_block` codes EVERY PLANE of a 64x64 |
| 12418 | // mu chunk before the next one starts: once this chunk's leaves |
| 12419 | // are done, its chroma is read immediately (the inter 128 path's |
| 12420 | // interleave). |
| 12421 | let chunk = (row / 16, col / 16); |
| 12422 | let done = idx + 1 == leaves.len() |
| 12423 | || (leaves[idx + 1].0 / 16, leaves[idx + 1].1 / 16) != chunk; |
| 12424 | if done { |
| 12425 | let (cr, cc) = chunk; |
| 12426 | // `av1_get_max_uv_txsize(BLOCK_128X64)` = TX_32X32: one |
| 12427 | // TX_32X32 chroma unit per plane per chunk at 4:2:0, raster |
| 12428 | // within the chunk, plane-major inside the chunk. |
| 12429 | let cu_tx = 32usize; |
| 12430 | let chunk_chroma = cside * 64 / side; |
| 12431 | let unit_luma = cu_tx * (side / cside); |
| 12432 | let chunk_w_mi = 16.min(max_w_mi.saturating_sub(cc * 16)); |
| 12433 | let chunk_h_mi = 16.min(max_h_mi.saturating_sub(cr * 16)); |
| 12434 | let units_x = chunk_w_mi / (unit_luma / MI); |
| 12435 | let units_y = chunk_h_mi / (unit_luma / MI); |
| 12436 | if lossless(fctx) { |
| 12437 | // TX_4X4 chroma units over this chunk (lane-lossless2's |
| 12438 | // walk, shared with the inter 128 path). |
| 12439 | read_inter_chroma_lossless( |
| 12440 | dec, cdfs, neighbours, u, v, |
| 12441 | Pred::Inline(&pred_u, cside), Pred::Inline(&pred_v, cside), |
| 12442 | (mi_r, mi_c), (cpx, cpy), |
| 12443 | (cc * chunk_chroma, cr * chunk_chroma), |
| 12444 | (chunk_chroma, chunk_chroma), |
| 12445 | (cw, ch), |
| 12446 | cside, 0, base_q_idx, |
| 12447 | &mut u_units, &mut v_units, fctx, |
| 12448 | )?; |
| 12449 | // lane-av1-ibcrect: read_inter_chroma_lossless stamped |
| 12450 | // every TX_4X4 unit's own context (`record_mi_chroma` |
| 12451 | // inside the walk) -- the end-of-block record below must |
| 12452 | // not overwrite them with the composed grid (the inter |
| 12453 | // 128 path sets the same flag on this arm). |
| 12454 | mu_chroma = true; |
| 12455 | continue; |
| 12456 | } |
| 12457 | let cu_scan = default_scan(cu_tx); |
| 12458 | for plane_idx in 1..3 { |
| 12459 | let (src, buf) = if plane_idx == 1 { |
| 12460 | (Pred::Inline(&pred_u, cside), &mut *u) |
| 12461 | } else { |
| 12462 | (Pred::Inline(&pred_v, cside), &mut *v) |
| 12463 | }; |
| 12464 | for ur in 0..units_y { |
| 12465 | for uc in 0..units_x { |
| 12466 | let unit_mi = ( |
| 12467 | mi_r + cr * 16 + ur * (unit_luma / MI), |
| 12468 | mi_c + cc * 16 + uc * (unit_luma / MI), |
| 12469 | ); |
| 12470 | // Per unit, AFTER the earlier units' stamps: this |
| 12471 | // unit's coefficient context must see them. |
| 12472 | let cu_around = neighbours.around_mi(unit_mi, unit_luma); |
| 12473 | if crate::envflags::env_flag!("EC_ECDUMP") { |
| 12474 | for p in 1..3 { |
| 12475 | let ab: Vec<String> = (0..unit_luma / MI) |
| 12476 | .map(|k| { |
| 12477 | format!( |
| 12478 | "{:?}/{}", |
| 12479 | neighbours.above[unit_mi.1 + k][p].dc, |
| 12480 | neighbours.above[unit_mi.1 + k][p].level |
| 12481 | ) |
| 12482 | }) |
| 12483 | .collect(); |
| 12484 | let lf: Vec<String> = (0..unit_luma / MI) |
| 12485 | .map(|k| { |
| 12486 | format!( |
| 12487 | "{:?}/{}", |
| 12488 | neighbours.left[unit_mi.0 + k][p].dc, |
| 12489 | neighbours.left[unit_mi.0 + k][p].level |
| 12490 | ) |
| 12491 | }) |
| 12492 | .collect(); |
| 12493 | eprintln!( |
| 12494 | "EC_IBC128_BANDS mi=({mi_r},{mi_c}) unit=({cr},{cc}) plane={p} ctx=({},{},{}) above=[{}] left=[{}]", |
| 12495 | cu_around[p].0, cu_around[p].1, cu_around[p].2, ab.join(","), lf.join(",") |
| 12496 | ); |
| 12497 | } |
| 12498 | } |
| 12499 | let (cu_x, cu_y) = ( |
| 12500 | cpx + cc * chunk_chroma + uc * cu_tx, |
| 12501 | cpy + cr * chunk_chroma + ur * cu_tx, |
| 12502 | ); |
| 12503 | // `av1_get_tx_type` (blockd.h:1291): an inter |
| 12504 | // block's chroma unit never codes a tx_type; it |
| 12505 | // inherits the coded type of the luma leaf |
| 12506 | // covering its own quadrant (all of this chunk's |
| 12507 | // leaves are read by now). |
| 12508 | let unit_rel_mi = ( |
| 12509 | unit_mi.0 - mi_r, |
| 12510 | unit_mi.1 - mi_c, |
| 12511 | ); |
| 12512 | let covering = leaf_tx_types |
| 12513 | .iter() |
| 12514 | .copied() |
| 12515 | .find(|&(lr, lc, lw, lh, _)| { |
| 12516 | lr <= unit_rel_mi.0 |
| 12517 | && unit_rel_mi.0 < lr + lh / MI |
| 12518 | && lc <= unit_rel_mi.1 |
| 12519 | && unit_rel_mi.1 < lc + lw / MI |
| 12520 | }) |
| 12521 | .map(|(_, _, _, _, t)| t); |
| 12522 | let cu_pred = src.offset( |
| 12523 | (cr * chunk_chroma + ur * cu_tx) * cside |
| 12524 | + cc * chunk_chroma |
| 12525 | + uc * cu_tx, |
| 12526 | ); |
| 12527 | let cu_grid = read_inter_plane( |
| 12528 | dec, |
| 12529 | cdfs, |
| 12530 | TxbSet::Chroma32, |
| 12531 | &cu_scan, |
| 12532 | plane_idx, |
| 12533 | cu_around[plane_idx], |
| 12534 | 0, |
| 12535 | buf, |
| 12536 | cu_x, |
| 12537 | cu_y, |
| 12538 | cu_tx, |
| 12539 | base_q_idx, |
| 12540 | cu_pred, |
| 12541 | Some(covering.unwrap_or(TxType::DctDct)), |
| 12542 | Some(3), fctx, |
| 12543 | )? |
| 12544 | .0; |
| 12545 | // Immediately, so the NEXT unit reads this one's |
| 12546 | // coefficient context. |
| 12547 | neighbours.record_mi_chroma( |
| 12548 | unit_mi, |
| 12549 | unit_luma, |
| 12550 | unit_luma, |
| 12551 | plane_idx, |
| 12552 | &cu_grid, |
| 12553 | ); |
| 12554 | if crate::envflags::env_flag!("EC_ECDUMP") { |
| 12555 | let st = crate::decode::neighbour_state(&cu_grid); |
| 12556 | eprintln!( |
| 12557 | "EC_IBC128_STAMP mi=({mi_r},{mi_c}) unit=({cr},{cc}) plane={plane_idx} state=({:?},{})", |
| 12558 | st.dc, st.level |
| 12559 | ); |
| 12560 | } |
| 12561 | hit!(CHROMA_SPLIT_TX_HITS); |
| 12562 | // lane-av1-ibcrect: per-unit chroma context -- the |
| 12563 | // end-of-block record's composed-grid re-stamp |
| 12564 | // must not stand (replayed below). |
| 12565 | mu_chroma = true; |
| 12566 | let dst = mu_units( |
| 12567 | if plane_idx == 1 { &mut u_units } else { &mut v_units }, |
| 12568 | cside * cside, |
| 12569 | ); |
| 12570 | for rr in 0..cu_tx { |
| 12571 | let start = (cr * chunk_chroma + ur * cu_tx + rr) |
| 12572 | * cside |
| 12573 | + cc * chunk_chroma |
| 12574 | + uc * cu_tx; |
| 12575 | dst[start..start + cu_tx] |
| 12576 | .copy_from_slice(&cu_grid[rr * cu_tx..][..cu_tx]); |
| 12577 | } |
| 12578 | } |
| 12579 | } |
| 12580 | } |
| 12581 | } |
| 12582 | } |
| 12583 | // The block-level luma bands were recorded per leaf above; the |
| 12584 | // composed chroma grids carry every unit in place. |
| 12585 | luma_grid = Grid::Zero(side * side); |
| 12586 | u_grid = if u_units.is_empty() { |
| 12587 | Grid::Zero(cside * cside) |
| 12588 | } else { |
| 12589 | Grid::Own(std::mem::take(&mut u_units)) |
| 12590 | }; |
| 12591 | v_grid = if v_units.is_empty() { |
| 12592 | Grid::Zero(cside * cside) |
| 12593 | } else { |
| 12594 | Grid::Own(std::mem::take(&mut v_units)) |
| 12595 | }; |
| 12596 | } |
| 12597 | // Bookkeeping (see [`decode_intrabc_rect`]): DC_PRED everywhere a |
| 12598 | // neighbour reads a mode, zeroed palette state, the skip grid, the |
| 12599 | // TXFM_CONTEXT bands (`read_block_tx_size_rect` already stamped them), |
| 12600 | // and the DV into the intrabc mi grid. |
| 12601 | if skip { |
| 12602 | neighbours.record_rect_mi( |
| 12603 | (mi_r, mi_c), bw, bh, DC_PRED, DC_PRED, |
| 12604 | &[&luma_grid[..], &u_grid[..], &v_grid[..]], |
| 12605 | ); |
| 12606 | } else { |
| 12607 | neighbours.record_split_luma_rect_mi( |
| 12608 | (mi_r, mi_c), bw, bh, DC_PRED, DC_PRED, [&u_grid[..], &v_grid[..]], |
| 12609 | ); |
| 12610 | } |
| 12611 | // lane-av1-ibcrect: the coded arm's chroma contexts were stamped PER |
| 12612 | // TRANSFORM UNIT (libaom `av1_set_entropy_contexts` inside |
| 12613 | // `decode_reconstruct_tx`'s mu-chunk loop), but the record above re-stamps |
| 12614 | // both planes with ONE `neighbour_state(composed_grid)` over the whole |
| 12615 | // span -- level = the SUM of every unit's levels and dc = the top-left |
| 12616 | // unit's sign (lane-vert46 r1 measured the same smearing at the 64x128 |
| 12617 | // inter block: the next block read a stale `dc_sign_ctx` one row low). |
| 12618 | // Measured on this shape: the 128x64 block at mi(64,32) of the |
| 12619 | // `ibcrect-hunt/E2` sb128 screen stream left chunk-1 U's level 1 smeared |
| 12620 | // into chunk-0's cells and the block below read `txb_skip` off the sum. |
| 12621 | // Replay every coded unit's own state after the record, in mu-chunk order |
| 12622 | // so a later unit still wins over an earlier one on the shared seam cells |
| 12623 | // (the composed construction `mu_chroma_units` uses, transcribed here). |
| 12624 | if mu_chroma { |
| 12625 | let cu = if lossless(fctx) { 4usize } else { 32usize }; |
| 12626 | // The unit's own footprint on the LUMA mi grid (the chroma context |
| 12627 | // bands are luma-mi indexed, [`Self::record_mi_chroma`]'s |
| 12628 | // convention): 8 px x2 per TX_4X4 unit, 64 px per TX_32X32 one. |
| 12629 | let unit_luma = cu * 2; |
| 12630 | // Raster over the block's chroma plane, `chunk_chroma` per mu chunk |
| 12631 | // (the same extents the read loop above laid the composed grid out |
| 12632 | // at) -- a unit the frame edge clipped codes nothing and carries no |
| 12633 | // composed samples beyond the clip, so it replays as the zero state |
| 12634 | // exactly like libaom's uncoded unit. |
| 12635 | let chunk_chroma = cside * 64 / side; |
| 12636 | let (rows, cols) = ((ch / chunk_chroma).max(1), (cw / chunk_chroma).max(1)); |
| 12637 | for cr in 0..rows { |
| 12638 | for cc in 0..cols { |
| 12639 | let unit_mi = (mi_r + cr * (unit_luma / MI), mi_c + cc * (unit_luma / MI)); |
| 12640 | for (plane_idx, grid) in [(1usize, &u_grid), (2usize, &v_grid)] { |
| 12641 | let mut unit = Vec::with_capacity(cu * cu); |
| 12642 | for rr in 0..cu { |
| 12643 | let start = |
| 12644 | (cr * chunk_chroma + rr) * cside + cc * chunk_chroma; |
| 12645 | unit.extend_from_slice(&grid[start..start + cu]); |
| 12646 | } |
| 12647 | neighbours.record_mi_chroma( |
| 12648 | unit_mi, unit_luma, unit_luma, plane_idx, &unit, |
| 12649 | ); |
| 12650 | } |
| 12651 | } |
| 12652 | } |
| 12653 | } |
| 12654 | neighbours.record_palette_y_rect((mi_r, mi_c), bw, bh, 0, [0u16; 8]); |
| 12655 | neighbours.record_palette_uv_rect((mi_r, mi_c), bw, bh, 0, [0u16; 8]); |
| 12656 | neighbours.fill_skip_grid_rect((mi_r, mi_c), bw / MI, bh / MI, skip); |
| 12657 | neighbours.fill_lf_grid_rect( |
| 12658 | (mi_r, mi_c), |
| 12659 | bw / MI, |
| 12660 | bh / MI, |
| 12661 | bw.min(64) as u8, |
| 12662 | bh.min(64) as u8, |
| 12663 | 0, fctx, |
| 12664 | ); |
| 12665 | record_intrabc_mi_rect( |
| 12666 | mi_r, |
| 12667 | mi_c, |
| 12668 | bw / MI, |
| 12669 | bh / MI, |
| 12670 | Some((dv_row, dv_col)), |
| 12671 | neighbours, |
| 12672 | fctx, |
| 12673 | ); |
| 12674 | Ok(()) |
| 12675 | } |
| 12676 | |
| 12677 | /// Decodes one true `bw`x`bh` `PARTITION_HORZ`/`PARTITION_VERT` intra strip |
| 12678 | /// (lane-intradisp r1, spec `decode_block` restricted to a 32x32 quadrant's |
| 12679 | /// two rect children). Only the `skip` case is supported: `skip`/`y_mode`/ |
| 12680 | /// `uv_mode`/`filter_intra`/`tx_depth` are read exactly per spec either way |
| 12681 | /// (so a skip strip elsewhere in the tile stays in sync), but this decoder |
| 12682 | /// has no rectangular-transform coefficient reader, and a chroma plane here |
| 12683 | /// is genuinely `TX_16X8`/`TX_8X16` (`av1_get_max_uv_txsize`, spec 5.11.16) |
| 12684 | /// with no depth-based square-tiling escape the way luma's own resolved |
| 12685 | /// `tx_depth` has (`sub_tx_size_map[TX_32X16]` == `TX_16X16`, square) -- |
| 12686 | /// so a coded (non-skip) strip is refused by name instead of guess-decoded. |
| 12687 | #[allow(clippy::too_many_arguments)] |
| 12688 | fn decode_block_rect( |
| 12689 | dec: &mut SymbolDecoder, |
| 12690 | cdfs: &mut Cdfs, |
| 12691 | neighbours: &mut Neighbours, |
| 12692 | at: (usize, usize), |
| 12693 | bw: usize, |
| 12694 | bh: usize, |
| 12695 | y: &mut PlaneBuf<'static>, |
| 12696 | u: &mut PlaneBuf<'static>, |
| 12697 | v: &mut PlaneBuf<'static>, |
| 12698 | enable_filter_intra: bool, |
| 12699 | allow_screen_content_tools: bool, |
| 12700 | base_q_idx: u8, |
| 12701 | tx_select: bool, |
| 12702 | reduced_tx_set: bool, fctx: &crate::decode::FrameCtx, |
| 12703 | ) -> Result<()> { |
| 12704 | let (r, c) = at; |
| 12705 | let (px, py) = (c * SUB, r * SUB); |
| 12706 | let (nb_above_mode, nb_left_mode) = neighbours.modes_above_left(r, c); |
| 12707 | let (palette_ctx, palette_cache) = neighbours.palette_ctx_and_cache(at); |
| 12708 | let palette_uv_cache = neighbours.palette_uv_cache(at); |
| 12709 | let (skip, mode, angle_delta_y, uv_mode, angle_delta_uv, alpha, filter_intra, palette_y, palette_uv, intrabc) = |
| 12710 | read_intra_mode_rect( |
| 12711 | dec, |
| 12712 | cdfs, |
| 12713 | nb_above_mode, |
| 12714 | nb_left_mode, |
| 12715 | true, |
| 12716 | true, |
| 12717 | bw, |
| 12718 | bh, |
| 12719 | enable_filter_intra, |
| 12720 | neighbours.skip_txfm_ctx(r * (SUB / MI), c * (SUB / MI)), |
| 12721 | allow_screen_content_tools, |
| 12722 | Some((palette_ctx, &palette_cache)), |
| 12723 | &palette_uv_cache, |
| 12724 | r * (SUB / MI), |
| 12725 | c * (SUB / MI), fctx, |
| 12726 | )?; |
| 12727 | |
| 12728 | if let Some((dv_row, dv_col)) = intrabc { |
| 12729 | return decode_intrabc_rect( |
| 12730 | dec, |
| 12731 | cdfs, |
| 12732 | neighbours, |
| 12733 | (r * (SUB / MI), c * (SUB / MI)), |
| 12734 | (bw, bh), |
| 12735 | skip, |
| 12736 | (dv_row, dv_col), |
| 12737 | y, |
| 12738 | u, |
| 12739 | v, |
| 12740 | base_q_idx, fctx, |
| 12741 | ); |
| 12742 | } |
| 12743 | let smooth_neighbor = is_smooth_mode(nb_above_mode) || is_smooth_mode(nb_left_mode); |
| 12744 | if smooth_neighbor { |
| 12745 | hit!(SMOOTH_LUMA_HITS); |
| 12746 | } |
| 12747 | // lane-chroma r3: chroma's own edge-filter-strength neighbour check |
| 12748 | // (spec `get_intra_edge_filter_type`) reads the CHROMA neighbour's |
| 12749 | // `uv_mode`, not the luma one. |
| 12750 | let smooth_neighbor_uv = |
| 12751 | neighbours.smooth_uv_neighbour(r * (SUB / MI), c * (SUB / MI), r, c, fctx); |
| 12752 | // lane-rectsplit r1: `predict_filter_intra` now takes the block's own |
| 12753 | // `bw`x`bh` (`av1_filter_intra_predictor_c` walks its 4x2 patches over a |
| 12754 | // rectangle just as happily), so a `use_filter_intra` strip at this level |
| 12755 | // predicts instead of refusing. The below-16x16 leaf arm |
| 12756 | // (`decode_leaf_rect`) keeps the refusal: it has no gate of its own. |
| 12757 | if filter_intra.is_some() { |
| 12758 | hit!(FILTER_INTRA_RECT_HITS); |
| 12759 | } |
| 12760 | let uv_predict_mode = if uv_mode == UV_CFL_PRED { |
| 12761 | DC_PRED |
| 12762 | } else { |
| 12763 | uv_mode |
| 12764 | }; |
| 12765 | let (mi_r, mi_c) = (r * (SUB / MI), c * (SUB / MI)); |
| 12766 | // The tx-depth symbol EXISTS only under TX_MODE_SELECT. Both square paths |
| 12767 | // gate their `read_tx_size` on `tx_select` for exactly this reason; this |
| 12768 | // one did not, so with `--enable-tx-size-search=0` -- which several gate |
| 12769 | // recipes use -- the decoder consumed a symbol the encoder never wrote and |
| 12770 | // desynced the tile from that point on. That is why both strips of the |
| 12771 | // pinned HORZ quadrant were wrong from their very first pixel, and why our |
| 12772 | // range after the read equalled the oracle's range for the whole block. |
| 12773 | // lane-av1mixloss: the lossless-segment early return |
| 12774 | // (`decodeframe.c:1203`) precedes `TX_MODE_SELECT`, so a lossless-segment |
| 12775 | // rect block codes no depth symbol here either (class sweep of the sub8 |
| 12776 | // leaf's fix). |
| 12777 | let depth = if tx_select && !lossless(fctx) { |
| 12778 | // lane-av1txbands: EVERY frame now reads the real `TXFM_CONTEXT` |
| 12779 | // bands (`get_tx_size_context`, libaom `pred_common.h:342`) -- an |
| 12780 | // intra-only frame publishes them from every block too |
| 12781 | // ([`Neighbours::fill_lf_grid_rect`]). Measured on the 10-bit |
| 12782 | // 1920x792 128-superblock stream, |
| 12783 | // decode-order frame 1: the 32x8 HORZ_4 strip at mi(48,176) read |
| 12784 | // `tx_size_cat2` row 1 where libaom reads row 2. |
| 12785 | let ctx = tx_size_context_txfm_rect(neighbours, (mi_r, mi_c), bw, bh, fctx); |
| 12786 | let d = dec.symbol(&mut cdfs.tx_size_cat2[ctx]); |
| 12787 | if crate::envflags::env_flag!("EC_TRACE_MODE_STEP") { |
| 12788 | eprintln!( |
| 12789 | "EC_ISTEP mi_row={mi_r} mi_col={mi_c} name=tx_depth val={d} ctx={ctx} cat=2 rng={}", |
| 12790 | dec.debug_state().0 |
| 12791 | ); |
| 12792 | } |
| 12793 | d |
| 12794 | } else { |
| 12795 | 0 |
| 12796 | }; |
| 12797 | // lane-av1-intrabc r3: this strip's own `set_txfm_ctxs` when the frame |
| 12798 | // reads the `TXFM_CONTEXT` bands -- see [`publish_txfm_bands_if_in_inter`]. |
| 12799 | publish_txfm_bands_if_in_inter( |
| 12800 | neighbours, |
| 12801 | (mi_r, mi_c), |
| 12802 | depth_to_tx_wh(bw, bh, depth, fctx), |
| 12803 | (bw, bh), fctx, |
| 12804 | ); |
| 12805 | if depth != 0 || lossless(fctx) { |
| 12806 | hit!(TX_DEPTH_HITS); |
| 12807 | // lane-rectsplit r1: a split transform is predicted and reconstructed |
| 12808 | // per transform unit, each unit taking its edges from the previous |
| 12809 | // unit's reconstruction inside this same strip (spec 5.11.36) -- |
| 12810 | // [`decode_rect_split`], the unit named by [`depth_to_tx_wh`]. |
| 12811 | let (tx_w, tx_h) = depth_to_tx_wh(bw, bh, depth, fctx); |
| 12812 | if crate::envflags::env_flag!("EC_SBPART_DUMP64") { |
| 12813 | eprintln!( |
| 12814 | "DUMP64SPLIT mi_r={mi_r} mi_c={mi_c} px={px} py={py} bw={bw} bh={bh} \ |
| 12815 | depth={depth} tx={tx_w}x{tx_h} mode={mode} uv={uv_predict_mode} skip={skip} \ |
| 12816 | angle_y={angle_delta_y}" |
| 12817 | ); |
| 12818 | } |
| 12819 | let modes = RectStripModes { |
| 12820 | skip, |
| 12821 | mode, |
| 12822 | angle_delta_y, |
| 12823 | uv_predict_mode, |
| 12824 | angle_delta_uv, |
| 12825 | alpha, |
| 12826 | filter_intra, |
| 12827 | smooth_neighbor, |
| 12828 | smooth_neighbor_uv, |
| 12829 | palette_y: palette_y |
| 12830 | .as_ref() |
| 12831 | .map(|p| p.map.iter().map(|&i| p.colors[i as usize]).collect()), |
| 12832 | palette_uv: palette_uv.as_ref().map(|p| { |
| 12833 | ( |
| 12834 | p.map.iter().map(|&i| p.u_colors[i as usize]).collect(), |
| 12835 | p.map.iter().map(|&i| p.v_colors[i as usize]).collect(), |
| 12836 | ) |
| 12837 | }), |
| 12838 | }; |
| 12839 | if palette_y.is_some() || palette_uv.is_some() { |
| 12840 | hit!(PALETTE_SPLIT_TX_HITS); |
| 12841 | } |
| 12842 | decode_rect_split( |
| 12843 | dec, |
| 12844 | cdfs, |
| 12845 | neighbours, |
| 12846 | (mi_r, mi_c), |
| 12847 | bw, |
| 12848 | bh, |
| 12849 | tx_w, |
| 12850 | tx_h, |
| 12851 | &modes, |
| 12852 | y, |
| 12853 | u, |
| 12854 | v, |
| 12855 | base_q_idx, |
| 12856 | reduced_tx_set, fctx, |
| 12857 | )?; |
| 12858 | // This early return skips this fn's own tail, so the palette |
| 12859 | // neighbour state has to be stamped HERE too: without it a split-tx |
| 12860 | // strip left the above/left palette arrays holding a DEAD block's |
| 12861 | // colours, and the next block's colour cache |
| 12862 | // ([`Neighbours::palette_uv_cache`]) then hit a cached entry with the |
| 12863 | // wrong VALUE -- same bit count, so the entropy stream stayed in sync |
| 12864 | // and only those pixels were wrong (lane-palette2 r10). |
| 12865 | let (py_size, py_colors) = palette_y.as_ref().map_or((0, [0u16; 8]), |p| (p.size, p.colors)); |
| 12866 | neighbours.record_palette_y_rect((mi_r, mi_c), bw, bh, py_size, py_colors); |
| 12867 | let (puv_size, puv_colors) = |
| 12868 | palette_uv.as_ref().map_or((0, [0u16; 8]), |p| (p.size, p.u_colors)); |
| 12869 | neighbours.record_palette_uv_rect((mi_r, mi_c), bw, bh, puv_size, puv_colors); |
| 12870 | hit!(RECT_PARTITION_HITS); |
| 12871 | return Ok(()); |
| 12872 | } |
| 12873 | let (tx_w, tx_h) = (bw, bh); |
| 12874 | let (cpx, cpy) = (px >> ss_x(fctx), py >> ss_y(fctx)); |
| 12875 | let (chroma_w, chroma_h) = (bw >> ss_x(fctx), bh >> ss_y(fctx)); |
| 12876 | let reach = Reach::of_rect(bw, bh, px, py, y.width, y.height, fctx); |
| 12877 | if crate::envflags::env_flag!("EC_AV1_TRACE") { |
| 12878 | let (rng, _) = dec.debug_state(); |
| 12879 | eprintln!( |
| 12880 | "TRACE_RECT_IMODE mi_row={mi_r} mi_col={mi_c} mode={mode} uv_mode={uv_mode} \ |
| 12881 | skip={} tx={depth} rng={rng}", |
| 12882 | skip as i32 |
| 12883 | ); |
| 12884 | } |
| 12885 | // As [`decode_block`]'s own [`PALETTE_PRED`] wiring: a palette-Y/UV |
| 12886 | // block's prediction is the reconstructed colour-index map, set fresh |
| 12887 | // right before each `reconstruct_rect` call below (lane-palette2 r4). |
| 12888 | if let Some(ref pyv) = palette_y { |
| 12889 | let buf: Vec<u16> = pyv.map.iter().map(|&idx| pyv.colors[idx as usize]).collect(); |
| 12890 | set_palette_pred(buf, fctx); |
| 12891 | } |
| 12892 | let palette_uv_bufs: Option<(Vec<u16>, Vec<u16>)> = palette_uv.as_ref().map(|puv| { |
| 12893 | ( |
| 12894 | puv.map.iter().map(|&idx| puv.u_colors[idx as usize]).collect(), |
| 12895 | puv.map.iter().map(|&idx| puv.v_colors[idx as usize]).collect(), |
| 12896 | ) |
| 12897 | }); |
| 12898 | if skip { |
| 12899 | push_intra_rect(0, |
| 12900 | px, |
| 12901 | py, |
| 12902 | bw, |
| 12903 | bh, |
| 12904 | mode, |
| 12905 | angle_delta_y, |
| 12906 | reach, |
| 12907 | &ZERO_RESIDUAL[..bw * bh], |
| 12908 | None, |
| 12909 | filter_intra, |
| 12910 | smooth_neighbor, fctx, |
| 12911 | ); |
| 12912 | let ac = alpha.map(|_| cfl_src_rect(px, py, bw, bh)); |
| 12913 | if let Some((ub, _)) = &palette_uv_bufs { |
| 12914 | set_palette_pred(ub.clone(), fctx); |
| 12915 | } |
| 12916 | push_intra_rect(1, |
| 12917 | cpx, |
| 12918 | cpy, |
| 12919 | chroma_w, |
| 12920 | chroma_h, |
| 12921 | uv_predict_mode, |
| 12922 | angle_delta_uv, |
| 12923 | reach, |
| 12924 | &ZERO_RESIDUAL[..chroma_w * chroma_h], |
| 12925 | alpha.zip(ac).map(|((au, _), ac)| (au, ac)), |
| 12926 | None, |
| 12927 | smooth_neighbor_uv, fctx, |
| 12928 | ); |
| 12929 | if let Some((_, vb)) = &palette_uv_bufs { |
| 12930 | set_palette_pred(vb.clone(), fctx); |
| 12931 | } |
| 12932 | push_intra_rect(2, |
| 12933 | cpx, |
| 12934 | cpy, |
| 12935 | chroma_w, |
| 12936 | chroma_h, |
| 12937 | uv_predict_mode, |
| 12938 | angle_delta_uv, |
| 12939 | reach, |
| 12940 | &ZERO_RESIDUAL[..chroma_w * chroma_h], |
| 12941 | alpha.zip(ac).map(|((_, av), ac)| (av, ac)), |
| 12942 | None, |
| 12943 | smooth_neighbor_uv, fctx, |
| 12944 | ); |
| 12945 | neighbours.record_rect( |
| 12946 | at, |
| 12947 | bw, |
| 12948 | bh, |
| 12949 | mode, |
| 12950 | uv_predict_mode, |
| 12951 | &[ |
| 12952 | &ZERO_RESIDUAL[..bw * bh], |
| 12953 | &ZERO_RESIDUAL[..chroma_w * chroma_h], |
| 12954 | &ZERO_RESIDUAL[..chroma_w * chroma_h], |
| 12955 | ], |
| 12956 | ); |
| 12957 | } else { |
| 12958 | // lane-rectwire r2: real coefficients. `get_txsize_entropy_ctx` |
| 12959 | // reduces both size pairs to their square-up CDF sets |
| 12960 | // (`TxbSet::LumaRect32x16`/`ChromaRect16x8`, see those variants' own |
| 12961 | // doc comments); the scan order and 2D context tables are the real |
| 12962 | // rect ones (`SCAN_32X16`/etc, `base_ctx_rect`/`br_ctx_rect`). |
| 12963 | let luma_scan: &[u16] = if bw > bh { &SCAN_32X16 } else { &SCAN_16X32 }; |
| 12964 | // Chroma tx follows the plane block, not a hardcoded half. At 4:2:0 a |
| 12965 | // 32x16 strip's chroma is 16x8 (ChromaRect16x8); at 4:4:4 it is 32x16 |
| 12966 | // (ChromaRect32x16, DCT-only — ADST is undefined on a 32-point axis). |
| 12967 | let (chroma_set, chroma_scan): (TxbSet, &[u16]) = match (chroma_w, chroma_h) { |
| 12968 | (32, 16) => (TxbSet::ChromaRect32x16, &SCAN_32X16), |
| 12969 | (16, 32) => (TxbSet::ChromaRect32x16, &SCAN_16X32), |
| 12970 | (16, 8) => (TxbSet::ChromaRect16x8, &SCAN_16X8), |
| 12971 | (8, 16) => (TxbSet::ChromaRect16x8, &SCAN_8X16), |
| 12972 | // lane-av1-422: a 4:2:2 strip halves only the width. A 32x16 |
| 12973 | // luma strip's chroma is a 16x16 SQUARE (`TxbSet::Chroma16`, the |
| 12974 | // set any 16x16 chroma plane reads); a 16x32 strip's chroma is |
| 12975 | // 8x32, whose entropy set is the 16x16 one and whose DCT-only |
| 12976 | // default follows the >=32 rule below -- the same pairing the |
| 12977 | // (8,32) row of [`decode_rect_split`]'s chroma table proves. At |
| 12978 | // 4:2:0 and 4:4:4 a strip's chroma is itself rect, so neither row |
| 12979 | // is reachable there. |
| 12980 | (16, 16) => (TxbSet::Chroma16, default_scan(16)), |
| 12981 | (8, 32) => (TxbSet::Chroma16, &SCAN_8X32), |
| 12982 | _ => { |
| 12983 | return Err(unsupported( |
| 12984 | "a rectangular chroma transform whose size has no coefficient table", |
| 12985 | )) |
| 12986 | } |
| 12987 | }; |
| 12988 | let chroma_default_tx = if chroma_w.max(chroma_h) >= 32 { |
| 12989 | TxType::DctDct |
| 12990 | } else { |
| 12991 | default_intra_tx_type(uv_predict_mode as u8) |
| 12992 | }; |
| 12993 | // lane-rectwire r3: real coefficients desync a pixel-exact gate |
| 12994 | // (rectwire-flake-1.obu, seed 55) -- both 32x16 strips of frame 0's |
| 12995 | // mi(8,8) HORZ quadrant come out ~100% wrong from their very first |
| 12996 | // pixel (not a subtle context-table off-by-one), while the sibling |
| 12997 | // non-rect quadrants of the same frame match. Range-ladder bisection |
| 12998 | // against a real aomdec (EC_TRACE_COEFF/EC_TRACE_MODE) was started |
| 12999 | // but the intra key-frame path has no traced symbol between the |
| 13000 | // partition read and the first coefficient block (mode/skip/tx_depth |
| 13001 | // reads are all untraced in libaom's decodeframe.c intra path, unlike |
| 13002 | // the inter EC_TRACE_MODE this round added), so a same-length range |
| 13003 | // comparison wasn't reachable within budget -- see |
| 13004 | // lanes/rectwire-r3.report.md for what was ruled out (base_ctx_rect's |
| 13005 | // 5-neighbour offsets, u/v_skip_ctx's above+left formula, and |
| 13006 | // dc_sign_ctx's around-index all cross-checked byte-for-byte against |
| 13007 | // libaom and match). lane-rectwire r4: range-ladder against the |
| 13008 | // now-instrumented oracle (EC_TRACE_MODE's intra EC_IMODE/EC_IMODE_VAL) |
| 13009 | // narrowed the divergence past skip/y_mode/uv_mode (values AND range |
| 13010 | // both confirmed in sync -- rng=58692 on both sides right after |
| 13011 | // uv_mode, with `enable_filter_intra=0` on this frame so neither side |
| 13012 | // reads a filter_intra symbol) to the `tx_size_cat2` depth read |
| 13013 | // immediately after: ours moves rng 58692 -> 43570, which cannot be |
| 13014 | // reconciled with oracle's own EC_IMODE_VAL (rng=58692, taken *after* |
| 13015 | // its own tx_size read) unless oracle's tx symbol is a near-certainty |
| 13016 | // CDF that barely narrows the range -- i.e. this decoder's |
| 13017 | // tx-depth ctx or `cdfs.tx_size_cat2` CDF selection for |
| 13018 | // a 32x16/16x32 strip is the first diverging symbol, not the |
| 13019 | // coefficient-context math r3 already cleared. Not confirmed byte- |
| 13020 | // exact (would need a decodemv.c EC_TRACE_MODE patch specifically on |
| 13021 | // `read_tx_size`'s own rng, not attempted this round -- budget), so |
| 13022 | // r5 CONFIRMED the suspect and it was simpler than the CDF row: the |
| 13023 | // tx-depth symbol exists only under TX_MODE_SELECT, and this path read |
| 13024 | // it unconditionally where both square paths gate on `tx_select`. With |
| 13025 | // --enable-tx-size-search=0 the encoder writes no such symbol, so the |
| 13026 | // decoder consumed one that was never there. |
| 13027 | { |
| 13028 | let around = neighbours.around_rect(at, bw, bh); |
| 13029 | let mut luma_coding = cdfs.txb(TxbSet::LumaRect32x16, fi_tx_row(mode, filter_intra)); |
| 13030 | let (luma_levels, luma_tx_type) = read_coeffs_rect( |
| 13031 | dec, |
| 13032 | &mut luma_coding, |
| 13033 | luma_scan, |
| 13034 | bw, |
| 13035 | bh, |
| 13036 | 0, |
| 13037 | dc_sign_ctx(around[0].2), |
| 13038 | TxType::DctDct, |
| 13039 | )?; |
| 13040 | if crate::envflags::env_flag!("EC_AV1_TRACE") { |
| 13041 | let (rng, _) = dec.debug_state(); |
| 13042 | eprintln!("TRACE_RECT_COEFF plane=0 mi_row={mi_r} mi_col={mi_c} rng={rng}"); |
| 13043 | } |
| 13044 | let luma_residual = dequant_and_inverse_typed_wh( |
| 13045 | &luma_levels, |
| 13046 | bw, |
| 13047 | bh, |
| 13048 | crate::decode::bit_depth(fctx), |
| 13049 | block_q_idx(fctx), |
| 13050 | plane_q_delta(0, fctx).0, |
| 13051 | plane_q_delta(0, fctx).1, |
| 13052 | luma_tx_type, |
| 13053 | block_iqmatrix(fctx, 0, bw, bh, luma_tx_type), |
| 13054 | ); |
| 13055 | push_intra_rect(0, |
| 13056 | px, |
| 13057 | py, |
| 13058 | bw, |
| 13059 | bh, |
| 13060 | mode, |
| 13061 | angle_delta_y, |
| 13062 | reach, |
| 13063 | &luma_residual, |
| 13064 | None, |
| 13065 | filter_intra, |
| 13066 | smooth_neighbor, fctx, |
| 13067 | ); |
| 13068 | let ac = alpha.map(|_| cfl_src_rect(px, py, bw, bh)); |
| 13069 | if let Some((ub, _)) = &palette_uv_bufs { |
| 13070 | set_palette_pred(ub.clone(), fctx); |
| 13071 | } |
| 13072 | let u_default_tx = chroma_default_tx; |
| 13073 | let u_skip_ctx = usize::from(around[1].0) + usize::from(around[1].1); |
| 13074 | let mut u_coding = cdfs.txb(chroma_set, uv_predict_mode); |
| 13075 | let (u_levels, u_tx_type) = read_chroma_coeffs_rect( |
| 13076 | dec, |
| 13077 | &mut u_coding, |
| 13078 | chroma_scan, |
| 13079 | chroma_w, |
| 13080 | chroma_h, |
| 13081 | u_skip_ctx, |
| 13082 | dc_sign_ctx(around[1].2), |
| 13083 | u_default_tx, |
| 13084 | fctx, |
| 13085 | )?; |
| 13086 | if crate::envflags::env_flag!("EC_AV1_TRACE") { |
| 13087 | let (rng, _) = dec.debug_state(); |
| 13088 | eprintln!("TRACE_RECT_COEFF plane=1 mi_row={mi_r} mi_col={mi_c} rng={rng}"); |
| 13089 | } |
| 13090 | let u_residual = dequant_and_inverse_typed_wh( |
| 13091 | &u_levels, |
| 13092 | chroma_w, |
| 13093 | chroma_h, |
| 13094 | crate::decode::bit_depth(fctx), |
| 13095 | block_q_idx(fctx), |
| 13096 | plane_q_delta(1, fctx).0, |
| 13097 | plane_q_delta(1, fctx).1, |
| 13098 | u_tx_type, |
| 13099 | block_iqmatrix(fctx, 1, chroma_w, chroma_h, u_tx_type), |
| 13100 | ); |
| 13101 | push_intra_rect(1, |
| 13102 | cpx, |
| 13103 | cpy, |
| 13104 | chroma_w, |
| 13105 | chroma_h, |
| 13106 | uv_predict_mode, |
| 13107 | angle_delta_uv, |
| 13108 | reach, |
| 13109 | &u_residual, |
| 13110 | alpha.zip(ac).map(|((au, _), ac)| (au, ac)), |
| 13111 | None, |
| 13112 | smooth_neighbor_uv, fctx, |
| 13113 | ); |
| 13114 | if let Some((_, vb)) = &palette_uv_bufs { |
| 13115 | set_palette_pred(vb.clone(), fctx); |
| 13116 | } |
| 13117 | let v_default_tx = chroma_default_tx; |
| 13118 | let v_skip_ctx = usize::from(around[2].0) + usize::from(around[2].1); |
| 13119 | let mut v_coding = cdfs.txb(chroma_set, uv_predict_mode); |
| 13120 | let (v_levels, v_tx_type) = read_chroma_coeffs_rect( |
| 13121 | dec, |
| 13122 | &mut v_coding, |
| 13123 | chroma_scan, |
| 13124 | chroma_w, |
| 13125 | chroma_h, |
| 13126 | v_skip_ctx, |
| 13127 | dc_sign_ctx(around[2].2), |
| 13128 | v_default_tx, |
| 13129 | fctx, |
| 13130 | )?; |
| 13131 | if crate::envflags::env_flag!("EC_AV1_TRACE") { |
| 13132 | let (rng, _) = dec.debug_state(); |
| 13133 | eprintln!("TRACE_RECT_COEFF plane=2 mi_row={mi_r} mi_col={mi_c} rng={rng}"); |
| 13134 | } |
| 13135 | let v_residual = dequant_and_inverse_typed_wh( |
| 13136 | &v_levels, |
| 13137 | chroma_w, |
| 13138 | chroma_h, |
| 13139 | crate::decode::bit_depth(fctx), |
| 13140 | block_q_idx(fctx), |
| 13141 | plane_q_delta(2, fctx).0, |
| 13142 | plane_q_delta(2, fctx).1, |
| 13143 | v_tx_type, |
| 13144 | block_iqmatrix(fctx, 2, chroma_w, chroma_h, v_tx_type), |
| 13145 | ); |
| 13146 | push_intra_rect(2, |
| 13147 | cpx, |
| 13148 | cpy, |
| 13149 | chroma_w, |
| 13150 | chroma_h, |
| 13151 | uv_predict_mode, |
| 13152 | angle_delta_uv, |
| 13153 | reach, |
| 13154 | &v_residual, |
| 13155 | alpha.zip(ac).map(|((_, av), ac)| (av, ac)), |
| 13156 | None, |
| 13157 | smooth_neighbor_uv, fctx, |
| 13158 | ); |
| 13159 | neighbours.record_rect(at, bw, bh, mode, uv_predict_mode, &[&luma_levels[..], &u_levels[..], &v_levels[..]]); |
| 13160 | hit!(RECT_COEFF_HITS); |
| 13161 | } |
| 13162 | } |
| 13163 | let (py_size, py_colors) = palette_y.as_ref().map_or((0, [0u16; 8]), |p| (p.size, p.colors)); |
| 13164 | neighbours.record_palette_y_rect((mi_r, mi_c), bw, bh, py_size, py_colors); |
| 13165 | let (puv_size, puv_colors) = |
| 13166 | palette_uv.as_ref().map_or((0, [0u16; 8]), |p| (p.size, p.u_colors)); |
| 13167 | neighbours.record_palette_uv_rect((mi_r, mi_c), bw, bh, puv_size, puv_colors); |
| 13168 | neighbours.fill_skip_grid_rect((mi_r, mi_c), bw / MI, bh / MI, skip); |
| 13169 | neighbours.fill_lf_grid_rect((mi_r, mi_c), bw / MI, bh / MI, tx_w as u8, tx_h as u8, 0, fctx); |
| 13170 | hit!(RECT_PARTITION_HITS); |
| 13171 | if crate::envflags::env_flag!("EC_AV1_TRACE") { |
| 13172 | eprintln!("TRACE_RECT32_END mi_row={mi_r} mi_col={mi_c} bw={bw} bh={bh}"); |
| 13173 | } |
| 13174 | Ok(()) |
| 13175 | } |
| 13176 | |
| 13177 | /// Decodes one `bw`x`bh` rect strip of a plain 16x16-level `PARTITION_HORZ`/ |
| 13178 | /// `PARTITION_VERT` (lane-rect16 r2), sibling to [`decode_block_rect`] but |
| 13179 | /// addressed by REAL mi coordinates rather than derived from an [`SUB`]-grid |
| 13180 | /// `outer_at`, the same relationship [`decode_leaf8`] has to `decode_block`: |
| 13181 | /// the second strip of a HORZ/VERT split sits offset by half the 16x16 |
| 13182 | /// parent, which `decode_block_rect`'s `at`-derived `px = c * SUB` can't |
| 13183 | /// name. `prev_leaf` mirrors `decode_leaf8`'s own convention -- when the |
| 13184 | /// previous strip is directly above (same column, `mi_h` rows up) or |
| 13185 | /// directly left (same row, `mi_w` cols over), its mode overrides the coarse |
| 13186 | /// per-16x16-cell `above_mode`/`left_mode` slot for THIS strip's own mode |
| 13187 | /// context read, and bookkeeping goes through the `_mi`/`_rect` neighbour |
| 13188 | /// writers (`record_mi_rect`, `fill_skip_grid_rect`, `fill_lf_grid_rect`) at |
| 13189 | /// this strip's real position, never the coarse `record_rect` (which the |
| 13190 | /// caller uses once, after both strips, exactly like the `VERT_B` arm next |
| 13191 | /// to it). The non-skip case (lane-rectx) reads real `TX_16X8`/`TX_8X16` |
| 13192 | /// luma coefficients (`TxbSet::LumaRect16x8`) and their `TX_8X4`/`TX_4X8` |
| 13193 | /// chroma half (`TxbSet::ChromaRect8x4`) at 4:2:0 -- an 8x8 *square* chroma |
| 13194 | /// plane (`TxbSet::Chroma8` + the default scan) at 4:2:2 (lane-av1-422e: |
| 13195 | /// half-width, unhalved height), the two size classes one level |
| 13196 | /// under [`decode_block_rect`]'s own 32x16/16x32 strip. |
| 13197 | #[allow(clippy::too_many_arguments)] |
| 13198 | fn decode_leaf_rect( |
| 13199 | dec: &mut SymbolDecoder, |
| 13200 | cdfs: &mut Cdfs, |
| 13201 | neighbours: &mut Neighbours, |
| 13202 | outer_at: (usize, usize), |
| 13203 | leaf_mi: (usize, usize), |
| 13204 | bw: usize, |
| 13205 | bh: usize, |
| 13206 | prev_leaf: Option<((usize, usize), usize)>, |
| 13207 | y: &mut PlaneBuf<'static>, |
| 13208 | u: &mut PlaneBuf<'static>, |
| 13209 | v: &mut PlaneBuf<'static>, |
| 13210 | enable_filter_intra: bool, |
| 13211 | allow_screen_content_tools: bool, |
| 13212 | base_q_idx: u8, |
| 13213 | tx_select: bool, |
| 13214 | reduced_tx_set: bool, fctx: &crate::decode::FrameCtx, |
| 13215 | ) -> Result<usize> { |
| 13216 | let _ = base_q_idx; |
| 13217 | if crate::envflags::env_flag!("EC_AV1_RECTX_TRACE") { |
| 13218 | eprintln!( |
| 13219 | "decode_leaf_rect: leaf_mi={leaf_mi:?} bw={bw} bh={bh} outer_at={outer_at:?} rng={}", |
| 13220 | dec.debug_state().0 |
| 13221 | ); |
| 13222 | } |
| 13223 | let (r, c) = outer_at; |
| 13224 | let mut above_mode = neighbours.above_mode[c]; |
| 13225 | let mut left_mode = neighbours.left_mode[r]; |
| 13226 | let (mi_w, mi_h) = (bw / MI, bh / MI); |
| 13227 | if let Some(((pr, pc), pmode)) = prev_leaf { |
| 13228 | if pc == leaf_mi.1 && leaf_mi.0 == pr + mi_h { |
| 13229 | above_mode = pmode; |
| 13230 | } else if pr == leaf_mi.0 && leaf_mi.1 == pc + mi_w { |
| 13231 | left_mode = pmode; |
| 13232 | } |
| 13233 | } |
| 13234 | // Same mi-exact override [`decode_leaf8`] takes: one coarse 16x16 slot |
| 13235 | // cannot hold the modes a split (or a pair of strips) leaves behind, so |
| 13236 | // when the map holds the block at exactly this strip's above/left mi it |
| 13237 | // is the neighbour libaom's `above_mi`/`left_mi` reads. |
| 13238 | // lane-rectsplitx r2 (same class as the 1:4 fix): mi-exact AND |
| 13239 | // availability-correct -- four 16x8 strips share one coarse 16x16 |
| 13240 | // `left_mode` cell too, so the fallback must not stand in for an absent |
| 13241 | // tile-edge neighbour. |
| 13242 | let (above_mode, left_mode) = |
| 13243 | neighbours.modes_above_left_mi(leaf_mi.0, leaf_mi.1, (above_mode, left_mode)); |
| 13244 | let smooth_neighbor = |
| 13245 | is_smooth_mode(above_mode) || is_smooth_mode(left_mode); |
| 13246 | if smooth_neighbor { |
| 13247 | hit!(SMOOTH_LUMA_HITS); |
| 13248 | } |
| 13249 | // The palette-Y mode CDF row is picked by the neighbours' palette use |
| 13250 | // (`palette_ctx_and_cache`), so this sub-16x16 leaf must pass its own real |
| 13251 | // ctx/cache -- reading `palette_y_mode` off row 0 when a neighbour did use |
| 13252 | // a palette narrows the wrong interval and desyncs the tile even when the |
| 13253 | // decoded bit is the same. The cell is the 16px-SUB cell containing the |
| 13254 | // leaf (the palette neighbour arrays have no finer granularity). |
| 13255 | // lane-kf900 r6: the leaf's REAL mi position -- the second leaf of a 16x8 |
| 13256 | // pair sits 8 px into the 16px cell, and both the cache's superblock-top-row |
| 13257 | // exclusion and the neighbour palette lookup itself are mi-granular |
| 13258 | // (see [`Neighbours::palette_ctx_and_cache_mi`]). |
| 13259 | let pal_at = leaf_mi; |
| 13260 | let (palette_ctx, palette_cache) = neighbours.palette_ctx_and_cache_mi(pal_at); |
| 13261 | let palette_uv_cache = neighbours.palette_uv_cache_mi(pal_at); |
| 13262 | let (skip, mode, angle_delta_y, uv_mode, angle_delta_uv, alpha, filter_intra, palette_y, palette_uv, intrabc) = |
| 13263 | read_intra_mode_rect( |
| 13264 | dec, |
| 13265 | cdfs, |
| 13266 | above_mode, |
| 13267 | left_mode, |
| 13268 | true, |
| 13269 | true, |
| 13270 | bw, |
| 13271 | bh, |
| 13272 | enable_filter_intra, |
| 13273 | neighbours.skip_txfm_ctx(leaf_mi.0, leaf_mi.1), |
| 13274 | allow_screen_content_tools, |
| 13275 | Some((palette_ctx, &palette_cache)), |
| 13276 | &palette_uv_cache, |
| 13277 | leaf_mi.0, |
| 13278 | leaf_mi.1, fctx, |
| 13279 | )?; |
| 13280 | |
| 13281 | if let Some((dv_row, dv_col)) = intrabc { |
| 13282 | decode_intrabc_rect( |
| 13283 | dec, cdfs, neighbours, leaf_mi, (bw, bh), skip, (dv_row, dv_col), |
| 13284 | y, u, v, base_q_idx, fctx, |
| 13285 | )?; |
| 13286 | return Ok(DC_PRED); |
| 13287 | } |
| 13288 | // lane-kf900 r4: palette prediction on this shape, the same colour-index |
| 13289 | // map -> base colours buffer [`decode_block_rect`] hands each |
| 13290 | // `reconstruct_rect` through [`set_palette_pred`]. |
| 13291 | if palette_y.is_some() || palette_uv.is_some() { |
| 13292 | hit!(RECT4_PALETTE_HITS); |
| 13293 | } |
| 13294 | let palette_y_buf: Option<Vec<u16>> = palette_y |
| 13295 | .as_ref() |
| 13296 | .map(|p| p.map.iter().map(|&i| p.colors[i as usize]).collect()); |
| 13297 | let palette_uv_bufs: Option<(Vec<u16>, Vec<u16>)> = palette_uv.as_ref().map(|p| { |
| 13298 | ( |
| 13299 | p.map.iter().map(|&i| p.u_colors[i as usize]).collect(), |
| 13300 | p.map.iter().map(|&i| p.v_colors[i as usize]).collect(), |
| 13301 | ) |
| 13302 | }); |
| 13303 | let (pal_y_size, pal_y_colors) = |
| 13304 | palette_y.as_ref().map_or((0, [0u16; 8]), |p| (p.size, p.colors)); |
| 13305 | let (pal_uv_size, pal_uv_colors) = |
| 13306 | palette_uv.as_ref().map_or((0, [0u16; 8]), |p| (p.size, p.u_colors)); |
| 13307 | if crate::envflags::env_flag!("EC_AV1_RECTX_TRACE") { |
| 13308 | eprintln!(" skip={skip} mode={mode} angle_delta_y={angle_delta_y} uv_mode={uv_mode}"); |
| 13309 | } |
| 13310 | if filter_intra.is_some() { |
| 13311 | // lane-fistrip r1: `predict_filter_intra` already walks its 4x2 |
| 13312 | // patches over a true `bw`x`bh` block (lane-rectsplit r1), so an |
| 13313 | // 8x16/16x8 strip predicts through the same call `reconstruct_rect` |
| 13314 | // below already makes; this refusal was square-only prose. |
| 13315 | hit!(FILTER_INTRA_RECT_HITS); |
| 13316 | hit!(FILTER_INTRA_RECT_SUB16_HITS); |
| 13317 | } |
| 13318 | let uv_predict_mode = if uv_mode == UV_CFL_PRED { DC_PRED } else { uv_mode }; |
| 13319 | // lane-cfl r1, same defect as [`decode_leaf_8x8`]: chroma's intra-edge |
| 13320 | // filter type is the NEIGHBOUR's `uv_mode` (libaom `get_filt_type`), not |
| 13321 | // a constant `false`. |
| 13322 | let smooth_neighbor_uv = neighbours.smooth_uv_neighbour(leaf_mi.0, leaf_mi.1, r, c, fctx); |
| 13323 | let depth = if tx_select && !lossless(fctx) { |
| 13324 | // lane-t900 r2 (sibling sweep): the in-inter `TXFM_CONTEXT` band read |
| 13325 | // every other rect reader now does -- a no-op on a key frame. |
| 13326 | // lane-av1mixloss: libaom `read_tx_size`'s first line |
| 13327 | // (`decodeframe.c:1203`, `if (xd->lossless[segment_id]) return |
| 13328 | // TX_4X4;`) precedes the `TX_MODE_SELECT` test, so a lossless-segment |
| 13329 | // rect leaf codes NO depth symbol (`depth_to_tx_wh` clamps to 4x4). |
| 13330 | // The mixed frame's 16x8 leaf at mi(10,0) forked exactly here: ours |
| 13331 | // read `tx_size_cat1` (rng 55700->49918) where the oracle's next |
| 13332 | // read was the leaf's own txb_skip. |
| 13333 | let ctx = tx_size_context_txfm_rect(neighbours, leaf_mi, bw, bh, fctx); |
| 13334 | // lane-rectsplitx r1: `bsize_to_tx_size_cat` (libaom `blockd.h`, |
| 13335 | // `bsize_to_tx_size_depth_table`) is 2 for BLOCK_16X8/BLOCK_8X16, so |
| 13336 | // the category is 1 -- NOT the 2 the 32x16 strips read. Same symbol |
| 13337 | // count (3), different CDF row: a wrong-table read of a real symbol, |
| 13338 | // the class that cost lane-rectsplit r1 its 64x32 depth. |
| 13339 | // lane-rect1d r1: the CATEGORY of this leaf is 1, not 2. libaom's |
| 13340 | // `bsize_to_tx_size_cat` reads `bsize_to_tx_size_depth_table` and |
| 13341 | // subtracts one: BLOCK_16X8/8X16 (and the 1:4 BLOCK_16X4/4X16 this |
| 13342 | // function also decodes) sit at depth 2, so they read |
| 13343 | // `tx_size_cdf[1]` -- the same table a 16x16 square block reads -- |
| 13344 | // while `tx_size_cat2` belongs to the 32-level shapes |
| 13345 | // ([`decode_block_rect`]). Both tables carry three symbols, so the |
| 13346 | // wrong one decodes a plausible depth from the wrong probabilities: |
| 13347 | // the range diverges silently at the first such leaf and every later |
| 13348 | // symbol in the tile is read from the wrong place (class |
| 13349 | // `wrong-alphabet-same-value`, here same alphabet / wrong row). |
| 13350 | let d = dec.symbol(&mut cdfs.tx_size_cat1[ctx]); |
| 13351 | if crate::envflags::env_flag!("EC_TRACE_MODE_STEP") { |
| 13352 | 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); |
| 13353 | } |
| 13354 | d |
| 13355 | } else { |
| 13356 | 0 |
| 13357 | }; |
| 13358 | // lane-av1-intrabc r3: this leaf's own `set_txfm_ctxs` when the frame reads |
| 13359 | // the `TXFM_CONTEXT` bands -- see [`publish_txfm_bands_if_in_inter`]. |
| 13360 | publish_txfm_bands_if_in_inter( |
| 13361 | neighbours, |
| 13362 | leaf_mi, |
| 13363 | depth_to_tx_wh(bw, bh, depth, fctx), |
| 13364 | (bw, bh), fctx, |
| 13365 | ); |
| 13366 | if depth != 0 || lossless(fctx) { |
| 13367 | // The same per-transform-unit walk the bigger strips take |
| 13368 | // (`depth_to_tx_wh`: TX_16X8 -> TX_8X8 at depth 1, -> TX_4X4 at |
| 13369 | // depth 2), addressed at this leaf's own mi position. |
| 13370 | hit!(TX_DEPTH_HITS); |
| 13371 | hit!(SUB16_SPLIT_HITS); |
| 13372 | let (tx_w, tx_h) = depth_to_tx_wh(bw, bh, depth, fctx); |
| 13373 | let modes = RectStripModes { |
| 13374 | skip, |
| 13375 | mode, |
| 13376 | angle_delta_y, |
| 13377 | uv_predict_mode, |
| 13378 | angle_delta_uv, |
| 13379 | alpha, |
| 13380 | filter_intra, |
| 13381 | smooth_neighbor, |
| 13382 | // lane-rectsplitx r3: the split arm hard-coded `false` here while |
| 13383 | // the unsplit arm below passes the real neighbour answer -- chroma |
| 13384 | // of a tx-split 16x8/8x16 leaf lost its intra-edge filter type. |
| 13385 | smooth_neighbor_uv, |
| 13386 | palette_y: palette_y_buf.clone(), |
| 13387 | palette_uv: palette_uv_bufs.clone(), |
| 13388 | }; |
| 13389 | if palette_y.is_some() || palette_uv.is_some() { |
| 13390 | hit!(PALETTE_SPLIT_TX_HITS); |
| 13391 | } |
| 13392 | decode_rect_split( |
| 13393 | dec, |
| 13394 | cdfs, |
| 13395 | neighbours, |
| 13396 | leaf_mi, |
| 13397 | bw, |
| 13398 | bh, |
| 13399 | tx_w, |
| 13400 | tx_h, |
| 13401 | &modes, |
| 13402 | y, |
| 13403 | u, |
| 13404 | v, |
| 13405 | base_q_idx, |
| 13406 | reduced_tx_set, fctx, |
| 13407 | )?; |
| 13408 | record_strip_palette( |
| 13409 | neighbours, pal_at, bw, bh, pal_y_size, pal_y_colors, pal_uv_size, pal_uv_colors, |
| 13410 | ); |
| 13411 | return Ok(mode); |
| 13412 | } |
| 13413 | let (px, py) = (leaf_mi.1 * MI, leaf_mi.0 * MI); |
| 13414 | let reach = Reach::of_rect(bw, bh, px, py, y.width, y.height, fctx); |
| 13415 | let (cpx, cpy) = (px >> ss_x(fctx), py >> ss_y(fctx)); |
| 13416 | let (chroma_w, chroma_h) = (bw >> ss_x(fctx), bh >> ss_y(fctx)); |
| 13417 | let (luma_levels, u_levels, v_levels); |
| 13418 | if skip { |
| 13419 | luma_levels = Grid::Zero(bw * bh); |
| 13420 | u_levels = Grid::Zero(chroma_w * chroma_h); |
| 13421 | v_levels = Grid::Zero(chroma_w * chroma_h); |
| 13422 | if let Some(b) = &palette_y_buf { |
| 13423 | set_palette_pred(b.clone(), fctx); |
| 13424 | } |
| 13425 | push_intra_rect(0, |
| 13426 | px, py, bw, bh, mode, angle_delta_y, reach, &luma_levels, None, filter_intra, |
| 13427 | smooth_neighbor, fctx, |
| 13428 | ); |
| 13429 | let ac = alpha.map(|_| cfl_src_rect(px, py, bw, bh)); |
| 13430 | if let Some((ub, _)) = &palette_uv_bufs { |
| 13431 | set_palette_pred(ub.clone(), fctx); |
| 13432 | } |
| 13433 | push_intra_rect(1, |
| 13434 | cpx, cpy, chroma_w, chroma_h, uv_predict_mode, angle_delta_uv, reach, &u_levels, |
| 13435 | alpha.zip(ac).map(|((au, _), ac)| (au, ac)), None, smooth_neighbor_uv, fctx, |
| 13436 | ); |
| 13437 | if let Some((_, vb)) = &palette_uv_bufs { |
| 13438 | set_palette_pred(vb.clone(), fctx); |
| 13439 | } |
| 13440 | push_intra_rect(2, |
| 13441 | cpx, cpy, chroma_w, chroma_h, uv_predict_mode, angle_delta_uv, reach, &v_levels, |
| 13442 | alpha.zip(ac).map(|((_, av), ac)| (av, ac)), None, smooth_neighbor_uv, fctx, |
| 13443 | ); |
| 13444 | } else { |
| 13445 | // lane-rectx: a genuine 16x8/8x16 luma transform (`TxbSet::LumaRect16x8`, |
| 13446 | // real `EOB_PT_128_LUMA` alphabet + the same mode-indexed `tx_type` |
| 13447 | // symbol the square `Luma16` set reads -- `get_ext_tx_set_type`'s |
| 13448 | // `use_reduced_set` branch returns `EXT_TX_SET_DTT4_IDTX` at |
| 13449 | // `tx_size_sqr_up == TX_16X16` regardless of the true, non-square |
| 13450 | // `tx_size`) and its chroma half (8x4/4x8, `TxbSet::ChromaRect8x4`). |
| 13451 | // lane-av1-422e: a 4:2:2 strip halves only the width, so the chroma |
| 13452 | // half of a 16x8/8x16 luma leaf is an 8x8 SQUARE (`TxbSet::Chroma8` + |
| 13453 | // the default scan -- the pairing any 8x8 chroma plane reads). 4:2:0 |
| 13454 | // halves both axes (8x4/4x8 rect, `TxbSet::ChromaRect8x4`); 4:4:4 |
| 13455 | // leaves the full 16x8/8x16 rect (`TxbSet::ChromaRect16x8`). |
| 13456 | let chroma_422 = ss_x(fctx) == 1 && ss_y(fctx) == 0; |
| 13457 | let (luma_scan, chroma_scan): (&[u16], &[u16]) = if ss_x(fctx) == 0 { |
| 13458 | if bw == 16 { (&SCAN_16X8, &SCAN_16X8) } else { (&SCAN_8X16, &SCAN_8X16) } |
| 13459 | } else if chroma_422 { |
| 13460 | if bw == 16 { (&SCAN_16X8, default_scan(8)) } else { (&SCAN_8X16, default_scan(8)) } |
| 13461 | } else if bw == 16 { |
| 13462 | (&SCAN_16X8, &SCAN_8X4) |
| 13463 | } else { |
| 13464 | (&SCAN_8X16, &SCAN_4X8) |
| 13465 | }; |
| 13466 | let chroma_rect_set = if ss_x(fctx) == 0 { |
| 13467 | TxbSet::ChromaRect16x8 |
| 13468 | } else if chroma_422 { |
| 13469 | TxbSet::Chroma8 |
| 13470 | } else { |
| 13471 | TxbSet::ChromaRect8x4 |
| 13472 | }; |
| 13473 | let around = neighbours.around_mi_rect(leaf_mi, bw, bh); |
| 13474 | let luma_set = if reduced_tx_set { |
| 13475 | TxbSet::LumaRect16x8 |
| 13476 | } else { |
| 13477 | TxbSet::LumaRect16x8Set1 |
| 13478 | }; |
| 13479 | let mut luma_coding = cdfs.txb(luma_set, fi_tx_row(mode, filter_intra)); |
| 13480 | let (l_levels, luma_tx_type) = read_coeffs_rect( |
| 13481 | dec, &mut luma_coding, luma_scan, bw, bh, 0, dc_sign_ctx(around[0].2), TxType::DctDct, |
| 13482 | )?; |
| 13483 | if crate::envflags::env_flag!("EC_AV1_RECTX_TRACE") { |
| 13484 | let nz: Vec<(usize, i32)> = l_levels.iter().copied().enumerate().filter(|(_, v)| *v != 0).collect(); |
| 13485 | eprintln!(" luma_tx_type={luma_tx_type:?} nz_levels={nz:?}"); |
| 13486 | } |
| 13487 | luma_levels = l_levels; |
| 13488 | let luma_residual = dequant_and_inverse_typed_wh( |
| 13489 | &luma_levels, bw, bh, crate::decode::bit_depth(fctx), |
| 13490 | block_q_idx(fctx), plane_q_delta(0, fctx).0, plane_q_delta(0, fctx).1, luma_tx_type, |
| 13491 | block_iqmatrix(fctx, 0, bw, bh, luma_tx_type), |
| 13492 | ); |
| 13493 | if let Some(b) = &palette_y_buf { |
| 13494 | set_palette_pred(b.clone(), fctx); |
| 13495 | } |
| 13496 | push_intra_rect(0, |
| 13497 | px, py, bw, bh, mode, angle_delta_y, reach, &luma_residual, None, filter_intra, |
| 13498 | smooth_neighbor, fctx, |
| 13499 | ); |
| 13500 | let ac = alpha.map(|_| cfl_src_rect(px, py, bw, bh)); |
| 13501 | let u_default_tx = default_intra_tx_type(uv_predict_mode as u8); |
| 13502 | let u_skip_ctx = usize::from(around[1].0) + usize::from(around[1].1); |
| 13503 | let mut u_coding = cdfs.txb(chroma_rect_set, uv_predict_mode); |
| 13504 | let (u_l, u_tx_type) = read_chroma_coeffs_rect( |
| 13505 | dec, &mut u_coding, chroma_scan, chroma_w, chroma_h, u_skip_ctx, |
| 13506 | dc_sign_ctx(around[1].2), u_default_tx, fctx, |
| 13507 | )?; |
| 13508 | u_levels = u_l; |
| 13509 | let u_residual = dequant_and_inverse_typed_wh( |
| 13510 | &u_levels, chroma_w, chroma_h, crate::decode::bit_depth(fctx), |
| 13511 | block_q_idx(fctx), plane_q_delta(1, fctx).0, plane_q_delta(1, fctx).1, u_tx_type, |
| 13512 | block_iqmatrix(fctx, 1, chroma_w, chroma_h, u_tx_type), |
| 13513 | ); |
| 13514 | if let Some((ub, _)) = &palette_uv_bufs { |
| 13515 | set_palette_pred(ub.clone(), fctx); |
| 13516 | } |
| 13517 | push_intra_rect(1, |
| 13518 | cpx, cpy, chroma_w, chroma_h, uv_predict_mode, angle_delta_uv, reach, &u_residual, |
| 13519 | alpha.zip(ac).map(|((au, _), ac)| (au, ac)), None, smooth_neighbor_uv, fctx, |
| 13520 | ); |
| 13521 | let v_default_tx = default_intra_tx_type(uv_predict_mode as u8); |
| 13522 | let v_skip_ctx = usize::from(around[2].0) + usize::from(around[2].1); |
| 13523 | let mut v_coding = cdfs.txb(chroma_rect_set, uv_predict_mode); |
| 13524 | let (v_l, v_tx_type) = read_chroma_coeffs_rect( |
| 13525 | dec, &mut v_coding, chroma_scan, chroma_w, chroma_h, v_skip_ctx, |
| 13526 | dc_sign_ctx(around[2].2), v_default_tx, fctx, |
| 13527 | )?; |
| 13528 | v_levels = v_l; |
| 13529 | let v_residual = dequant_and_inverse_typed_wh( |
| 13530 | &v_levels, chroma_w, chroma_h, crate::decode::bit_depth(fctx), |
| 13531 | block_q_idx(fctx), plane_q_delta(2, fctx).0, plane_q_delta(2, fctx).1, v_tx_type, |
| 13532 | block_iqmatrix(fctx, 2, chroma_w, chroma_h, v_tx_type), |
| 13533 | ); |
| 13534 | if let Some((_, vb)) = &palette_uv_bufs { |
| 13535 | set_palette_pred(vb.clone(), fctx); |
| 13536 | } |
| 13537 | push_intra_rect(2, |
| 13538 | cpx, cpy, chroma_w, chroma_h, uv_predict_mode, angle_delta_uv, reach, &v_residual, |
| 13539 | alpha.zip(ac).map(|((_, av), ac)| (av, ac)), None, smooth_neighbor_uv, fctx, |
| 13540 | ); |
| 13541 | hit!(RECT_LEAF_COEFF_HITS); |
| 13542 | } |
| 13543 | neighbours.record_mi_rect(leaf_mi, bw, bh, &[&luma_levels[..], &u_levels[..], &v_levels[..]]); |
| 13544 | neighbours.fill_skip_grid_rect(leaf_mi, mi_w, mi_h, skip); |
| 13545 | neighbours.fill_lf_grid_rect(leaf_mi, mi_w, mi_h, bw as u8, bh as u8, 0, fctx); |
| 13546 | // The leaf's own palette state (`0` clears, as this site always did |
| 13547 | // while a palette leaf refused -- lane-palette2 r8), written on the SPLIT |
| 13548 | // AXIS only until the pair's closing leaf: the coarse cell holds one |
| 13549 | // entry for both leaves, and leaf 0 zeroing the LEFT one threw away the |
| 13550 | // real left neighbour leaf 1 reads (lane-kf900 r4). |
| 13551 | record_strip_palette( |
| 13552 | neighbours, pal_at, bw, bh, pal_y_size, pal_y_colors, pal_uv_size, pal_uv_colors, |
| 13553 | ); |
| 13554 | // A rect leaf spans mi_w x mi_h, so its mode reaches several columns and |
| 13555 | // rows -- record the whole span, not one cell. |
| 13556 | neighbours.record_mode_mi(leaf_mi.0, leaf_mi.1, mi_w, mi_h, mode); |
| 13557 | neighbours.record_uv_mode_mi(leaf_mi.0, leaf_mi.1, mi_w, mi_h, uv_predict_mode); |
| 13558 | Ok(mode) |
| 13559 | } |
| 13560 | |
| 13561 | /// Decodes one 32x8 / 8x32 strip of a 32x32-level `PARTITION_HORZ_4` / |
| 13562 | /// `PARTITION_VERT_4` (lane-tx64x16 r3): [`decode_block_rect`]'s 4:1 sibling, |
| 13563 | /// addressed by REAL mi coordinates for the same reason [`decode_leaf_rect`] |
| 13564 | /// is -- strips 1 and 3 start 8 px into a 16-px [`SUB`] cell, which an |
| 13565 | /// `at`-derived `px = c * SUB` cannot name. `prev_strip` carries the previous |
| 13566 | /// strip's mode so this one's mode context sees its true above/left |
| 13567 | /// neighbour inside the same 32x32 (`decode_leaf_rect`'s convention). |
| 13568 | /// |
| 13569 | /// Tables: LUMA `TX_32X8`/`TX_8X32` averages to the 16x16 entropy context |
| 13570 | /// ([`TxbSet::LumaRect32x8`], 256 positions) and scans with |
| 13571 | /// [`SCAN_32X8`]/[`SCAN_8X32`]; CHROMA `TX_16X4`/`TX_4X16` averages to the |
| 13572 | /// 8x8 one ([`TxbSet::Chroma8`], 64 positions) and scans with |
| 13573 | /// [`SCAN_16X4`]/[`SCAN_4X16`]. Neither shape takes the rect sqrt2 scale -- |
| 13574 | /// `get_rect_tx_log_ratio` fires at ratio 1 only, and this is ratio 2 |
| 13575 | /// (`inverse_transform_2d_typed_wh`'s own `rect_scale`). |
| 13576 | /// |
| 13577 | /// A split transform (`tx_depth != 0`) refuses by name: `depth_to_tx_size` of |
| 13578 | /// a 4:1 strip is not simply `bw.min(bh) >> (depth - 1)` the way a 2:1 |
| 13579 | /// strip's is, so [`decode_rect_split`]'s per-unit walk cannot be reused |
| 13580 | /// unexamined. |
| 13581 | #[allow(clippy::too_many_arguments)] |
| 13582 | fn decode_block_rect4( |
| 13583 | dec: &mut SymbolDecoder, |
| 13584 | cdfs: &mut Cdfs, |
| 13585 | neighbours: &mut Neighbours, |
| 13586 | strip_mi: (usize, usize), |
| 13587 | bw: usize, |
| 13588 | bh: usize, |
| 13589 | prev_strip: Option<((usize, usize), usize)>, |
| 13590 | y: &mut PlaneBuf<'static>, |
| 13591 | u: &mut PlaneBuf<'static>, |
| 13592 | v: &mut PlaneBuf<'static>, |
| 13593 | enable_filter_intra: bool, |
| 13594 | allow_screen_content_tools: bool, |
| 13595 | base_q_idx: u8, |
| 13596 | tx_select: bool, fctx: &crate::decode::FrameCtx, |
| 13597 | ) -> Result<(usize, usize)> { |
| 13598 | let _ = base_q_idx; |
| 13599 | let (mi_r, mi_c) = strip_mi; |
| 13600 | let (r, c) = (mi_r / (SUB / MI), mi_c / (SUB / MI)); |
| 13601 | let (mi_w, mi_h) = (bw / MI, bh / MI); |
| 13602 | let mut above_mode = neighbours.above_mode[c]; |
| 13603 | let mut left_mode = neighbours.left_mode[r]; |
| 13604 | if let Some(((pr, pc), pmode)) = prev_strip { |
| 13605 | if pc == mi_c && mi_r == pr + mi_h { |
| 13606 | above_mode = pmode; |
| 13607 | } else if pr == mi_r && mi_c == pc + mi_w { |
| 13608 | left_mode = pmode; |
| 13609 | } |
| 13610 | } |
| 13611 | // lane-rectsplitx r2, ROOT CAUSE of the 1:4 mismatch: mi-exact + |
| 13612 | // availability-correct neighbour modes (see |
| 13613 | // [`Neighbours::modes_above_left_mi`]). The coarse 16x16 bands alias every |
| 13614 | // 8-px strip of a `HORZ_4`/`VERT_4` onto one cell. |
| 13615 | let (above_mode, left_mode) = |
| 13616 | neighbours.modes_above_left_mi(mi_r, mi_c, (above_mode, left_mode)); |
| 13617 | // lane-kf900 r4: this strip's own palette-Y mode ctx and colour cache. |
| 13618 | // Passing `None`/`&[]` read `palette_y_mode` off CDF row 0 and every |
| 13619 | // `palette_colors_y` cache hit off an EMPTY cache -- right symbol count, |
| 13620 | // wrong interval, so a screen-content tile desynced at the first strip |
| 13621 | // whose neighbour used a palette (class [[cdf-row-held-constant]]). |
| 13622 | let pal_at = (mi_r, mi_c); |
| 13623 | let (palette_ctx, palette_cache) = neighbours.palette_ctx_and_cache_mi(pal_at); |
| 13624 | let palette_uv_cache = neighbours.palette_uv_cache_mi(pal_at); |
| 13625 | let ( |
| 13626 | skip, |
| 13627 | mode, |
| 13628 | angle_delta_y, |
| 13629 | uv_mode, |
| 13630 | angle_delta_uv, |
| 13631 | alpha, |
| 13632 | filter_intra, |
| 13633 | palette_y, |
| 13634 | palette_uv, |
| 13635 | intrabc, |
| 13636 | ) = read_intra_mode_rect( |
| 13637 | dec, |
| 13638 | cdfs, |
| 13639 | above_mode, |
| 13640 | left_mode, |
| 13641 | true, |
| 13642 | true, |
| 13643 | bw, |
| 13644 | bh, |
| 13645 | enable_filter_intra, |
| 13646 | neighbours.skip_txfm_ctx(mi_r, mi_c), |
| 13647 | allow_screen_content_tools, |
| 13648 | Some((palette_ctx, &palette_cache)), |
| 13649 | &palette_uv_cache, |
| 13650 | mi_r, |
| 13651 | mi_c, fctx, |
| 13652 | )?; |
| 13653 | if let Some((dv_row, dv_col)) = intrabc { |
| 13654 | decode_intrabc_rect( |
| 13655 | dec, cdfs, neighbours, strip_mi, (bw, bh), skip, (dv_row, dv_col), |
| 13656 | y, u, v, base_q_idx, fctx, |
| 13657 | )?; |
| 13658 | return Ok((DC_PRED, DC_PRED)); |
| 13659 | } |
| 13660 | // lane-kf900 r4: palette prediction on this shape, exactly as |
| 13661 | // [`decode_block_rect`] does one aspect ratio up -- the colour-index map |
| 13662 | // mapped through the base colours IS the prediction (it reads no edge |
| 13663 | // pixels), handed to each `reconstruct_rect` through [`set_palette_pred`]. |
| 13664 | if palette_y.is_some() || palette_uv.is_some() { |
| 13665 | hit!(RECT4_PALETTE_HITS); |
| 13666 | } |
| 13667 | let palette_y_buf: Option<Vec<u16>> = palette_y |
| 13668 | .as_ref() |
| 13669 | .map(|p| p.map.iter().map(|&i| p.colors[i as usize]).collect()); |
| 13670 | let palette_uv_bufs: Option<(Vec<u16>, Vec<u16>)> = palette_uv.as_ref().map(|p| { |
| 13671 | ( |
| 13672 | p.map.iter().map(|&i| p.u_colors[i as usize]).collect(), |
| 13673 | p.map.iter().map(|&i| p.v_colors[i as usize]).collect(), |
| 13674 | ) |
| 13675 | }); |
| 13676 | let (pal_y_size, pal_y_colors) = |
| 13677 | palette_y.as_ref().map_or((0, [0u16; 8]), |p| (p.size, p.colors)); |
| 13678 | let (pal_uv_size, pal_uv_colors) = |
| 13679 | palette_uv.as_ref().map_or((0, [0u16; 8]), |p| (p.size, p.u_colors)); |
| 13680 | let smooth_neighbor = is_smooth_mode(above_mode) || is_smooth_mode(left_mode); |
| 13681 | if smooth_neighbor { |
| 13682 | hit!(SMOOTH_LUMA_HITS); |
| 13683 | } |
| 13684 | // lane-rectsplitx r2: this strip's REAL mi, not the [`SUB`] cell's |
| 13685 | // top-left -- a 1:4 strip starts 8 px into a 16-px cell, so |
| 13686 | // `r * (SUB / MI)` names a different mi column and the chroma |
| 13687 | // edge-filter strength was read from the wrong neighbour (measured: |
| 13688 | // seed 43, the last 8x32 strip at luma (152,32), chroma-only mismatch |
| 13689 | // with luma bit-exact). |
| 13690 | let smooth_neighbor_uv = neighbours.smooth_uv_neighbour(mi_r, mi_c, r, c, fctx); |
| 13691 | if filter_intra.is_some() { |
| 13692 | hit!(FILTER_INTRA_RECT_HITS); |
| 13693 | if bw > bh { |
| 13694 | hit!(FILTER_INTRA_RECT4_HORZ_HITS); |
| 13695 | } else { |
| 13696 | hit!(FILTER_INTRA_RECT4_VERT_HITS); |
| 13697 | } |
| 13698 | } |
| 13699 | let uv_predict_mode = if uv_mode == UV_CFL_PRED { |
| 13700 | DC_PRED |
| 13701 | } else { |
| 13702 | uv_mode |
| 13703 | }; |
| 13704 | // lane-av1mixloss: the lossless-segment early return (`decodeframe.c:1203`) |
| 13705 | // precedes `TX_MODE_SELECT`, so a lossless-segment rect4 block codes no |
| 13706 | // depth symbol here either (class sweep of the sub8 leaf's fix). |
| 13707 | let depth = if tx_select && !lossless(fctx) { |
| 13708 | // lane-av1txbands: EVERY frame now reads the real `TXFM_CONTEXT` |
| 13709 | // bands (`get_tx_size_context`, libaom `pred_common.h:342`) -- an |
| 13710 | // intra-only frame publishes them from every block too |
| 13711 | // ([`Neighbours::fill_lf_grid_rect`]). Measured on the 10-bit |
| 13712 | // 1920x792 128-superblock stream, |
| 13713 | // decode-order frame 1: the 32x8 HORZ_4 strip at mi(48,176) read |
| 13714 | // `tx_size_cat2` row 1 where libaom reads row 2. |
| 13715 | let ctx = tx_size_context_txfm_rect(neighbours, (mi_r, mi_c), bw, bh, fctx); |
| 13716 | let d = dec.symbol(&mut cdfs.tx_size_cat2[ctx]); |
| 13717 | if crate::envflags::env_flag!("EC_TRACE_MODE_STEP") { |
| 13718 | eprintln!( |
| 13719 | "EC_ISTEP mi_row={mi_r} mi_col={mi_c} name=tx_depth val={d} ctx={ctx} cat=2 rng={}", |
| 13720 | dec.debug_state().0 |
| 13721 | ); |
| 13722 | } |
| 13723 | d |
| 13724 | } else { |
| 13725 | 0 |
| 13726 | }; |
| 13727 | // lane-t900 r1: this strip's own `set_txfm_ctxs` when it is an intra |
| 13728 | // block of an INTER frame -- see [`publish_txfm_bands_if_in_inter`]. |
| 13729 | publish_txfm_bands_if_in_inter( |
| 13730 | neighbours, |
| 13731 | (mi_r, mi_c), |
| 13732 | depth_to_tx_wh(bw, bh, depth, fctx), |
| 13733 | (bw, bh), fctx, |
| 13734 | ); |
| 13735 | if depth != 0 || lossless(fctx) { |
| 13736 | // lane-rectsplitx r1: the per-transform-unit walk, at this strip's |
| 13737 | // real mi position (strips 1 and 3 of a HORZ_4 start 8 px into a |
| 13738 | // 16-px [`SUB`] cell). depth 1 is the RECT unit TX_16X8/TX_8X16 |
| 13739 | // (`sub_tx_size_map[TX_32X8] == TX_16X8`), depth 2 the square TX_8X8; |
| 13740 | // both interleave in every real stream. |
| 13741 | hit!(TX_DEPTH_HITS); |
| 13742 | let (tx_w, tx_h) = depth_to_tx_wh(bw, bh, depth, fctx); |
| 13743 | // lane-rectsplitx r2: both depths ship together, as the r1 handoff |
| 13744 | // predicted -- depth 1 fires FIRST in every attempt that splits at |
| 13745 | // all, so a depth-2-only lift could never be gated. The r1 mismatch |
| 13746 | // was never the rect unit: it was the neighbour MODE the strip reads |
| 13747 | // (see `modes_above_left_mi`). |
| 13748 | let which = if depth == 1 { |
| 13749 | &RECT4_SPLIT_DEPTH1_HITS |
| 13750 | } else { |
| 13751 | &RECT4_SPLIT_DEPTH2_HITS |
| 13752 | }; |
| 13753 | hit_do! { which.with(|c| c.set(c.get() + 1)); } |
| 13754 | let modes = RectStripModes { |
| 13755 | skip, |
| 13756 | mode, |
| 13757 | angle_delta_y, |
| 13758 | uv_predict_mode, |
| 13759 | angle_delta_uv, |
| 13760 | alpha, |
| 13761 | filter_intra, |
| 13762 | smooth_neighbor, |
| 13763 | smooth_neighbor_uv, |
| 13764 | palette_y: palette_y_buf.clone(), |
| 13765 | palette_uv: palette_uv_bufs.clone(), |
| 13766 | }; |
| 13767 | if palette_y.is_some() || palette_uv.is_some() { |
| 13768 | hit!(PALETTE_SPLIT_TX_HITS); |
| 13769 | } |
| 13770 | decode_rect_split( |
| 13771 | dec, |
| 13772 | cdfs, |
| 13773 | neighbours, |
| 13774 | (mi_r, mi_c), |
| 13775 | bw, |
| 13776 | bh, |
| 13777 | tx_w, |
| 13778 | tx_h, |
| 13779 | &modes, |
| 13780 | y, |
| 13781 | u, |
| 13782 | v, |
| 13783 | base_q_idx, |
| 13784 | false, fctx, |
| 13785 | )?; |
| 13786 | record_strip_palette( |
| 13787 | neighbours, pal_at, bw, bh, pal_y_size, pal_y_colors, pal_uv_size, |
| 13788 | pal_uv_colors, |
| 13789 | ); |
| 13790 | if bw > bh { |
| 13791 | hit!(RECT4_32_HORZ_HITS); |
| 13792 | } else { |
| 13793 | hit!(RECT4_32_VERT_HITS); |
| 13794 | } |
| 13795 | if !skip { |
| 13796 | hit!(RECT4_COEFF_HITS); |
| 13797 | } |
| 13798 | // lane-frame36 r2 (same shape as `decode_block_rect64`'s split arm, |
| 13799 | // swept this round): `set_txfm_ctxs` runs for EVERY block of an inter |
| 13800 | // frame, so this 1:4 strip -- which is also the INTER frame's reader |
| 13801 | // for 32x8/8x32 ([`decode_intra_rect4_in_inter`]) -- publishes its |
| 13802 | // transform size in both TXFM_CONTEXT bands. Without it the next block |
| 13803 | // reads the row's stale value. |
| 13804 | txfm_partition_update_rect(neighbours, (mi_r, mi_c), (tx_w, tx_h), (bw, bh)); |
| 13805 | return Ok((mode, uv_predict_mode)); |
| 13806 | } |
| 13807 | // The coarse (16-px [`SUB`] cell) mode/side arrays cannot name a strip |
| 13808 | // that is 8 px tall, so each strip stamps every cell it touches and the |
| 13809 | // LAST strip wins -- which is the right answer for the block below/right |
| 13810 | // of the 32x32, the only reader outside it. Written after this strip's |
| 13811 | // own context read above, never before. |
| 13812 | for cell in 0..bw.div_ceil(SUB) { |
| 13813 | neighbours.above_mode[c + cell] = mode; |
| 13814 | neighbours.above_uv_mode[c + cell] = uv_predict_mode; |
| 13815 | neighbours.above_side[c + cell] = bw; |
| 13816 | } |
| 13817 | for cell in 0..bh.div_ceil(SUB) { |
| 13818 | neighbours.left_mode[r + cell] = mode; |
| 13819 | neighbours.left_uv_mode[r + cell] = uv_predict_mode; |
| 13820 | neighbours.left_side[r + cell] = bh; |
| 13821 | } |
| 13822 | // ...and the mi-EXACT maps the coarse cells alias away, exactly as |
| 13823 | // [`decode_leaf_rect`] does (the split arm above records these through |
| 13824 | // `record_mi_luma_rect`). Without this an unsplit 32x8 strip left |
| 13825 | // `sub8_mode_row` holding the previous block, and the next 32x32's |
| 13826 | // strips read a stale `kf_y_mode` row (measured: mi(4,8) on the band |
| 13827 | // fixture). |
| 13828 | neighbours.record_mode_mi(mi_r, mi_c, mi_w, mi_h, mode); |
| 13829 | neighbours.record_uv_mode_mi(mi_r, mi_c, mi_w, mi_h, uv_predict_mode); |
| 13830 | let (px, py) = (mi_c * MI, mi_r * MI); |
| 13831 | let (cpx, cpy) = (px >> ss_x(fctx), py >> ss_y(fctx)); |
| 13832 | let (chroma_w, chroma_h) = (bw >> ss_x(fctx), bh >> ss_y(fctx)); |
| 13833 | let reach = Reach::of_rect(bw, bh, px, py, y.width, y.height, fctx); |
| 13834 | // lane-av1-444: the strip's chroma shape follows the subsampling. |
| 13835 | // 4:2:0 codes 16x4/4x16 (`TxbSet::Chroma8`); 4:4:4 codes the strip |
| 13836 | // itself, 32x8/8x32 (`get_txsize_entropy_ctx(TX_32X8)` = |
| 13837 | // `(wide_log2 + high_log2 + 1) >> 1` = the 16x16 set, |
| 13838 | // `TxbSet::Chroma16`). The caller set is closed -- this reader only |
| 13839 | // ever sees 32x8 / 8x32 luma and the sequence header admits ss 0/0 |
| 13840 | // and 1/1 only -- so the table is exhaustive. |
| 13841 | let chroma422_rect32 = |
| 13842 | (chroma_w, chroma_h) == (4, 32) && ss_x(fctx) == 1 && ss_y(fctx) == 0; |
| 13843 | let (chroma_set, chroma_scan): (TxbSet, &[u16]) = match (chroma_w, chroma_h) { |
| 13844 | (16, 4) => (TxbSet::Chroma8, &SCAN_16X4[..]), |
| 13845 | (4, 16) => (TxbSet::Chroma8, &SCAN_4X16[..]), |
| 13846 | (32, 8) => (TxbSet::Chroma16, &SCAN_32X8[..]), |
| 13847 | (8, 32) => (TxbSet::Chroma16, &SCAN_8X32[..]), |
| 13848 | // lane-av1-422: a 4:2:2 1:4 strip halves only the width, so the |
| 13849 | // landscape leaves (32x8, 16x4 luma) code their chroma as the RECT |
| 13850 | // (16,8)/(8,4) -- the same TxbSet/scan pair the (16,8)/(8,4) rows of |
| 13851 | // [`decode_rect_split`]'s chroma table use. |
| 13852 | // lane-av1-422p: the PORTRAIT leaf occurs too -- the pinned |
| 13853 | // fixture's frame 2 carries an 8x32 strip at ss(1,0), whose (4, 32) |
| 13854 | // chroma block `ss_size_lookup[BLOCK_8X32][1][0]` marks |
| 13855 | // BLOCK_INVALID (255, enums.h). BLOCK_INVALID does not mean |
| 13856 | // "uncodable": `av1_get_tx_size` reads `max_txsize_rect_lookup[255]` |
| 13857 | // OUT OF BOUNDS there, the byte at +255 in the pinned Release oracle |
| 13858 | // is 0x05 = TX_4X8 (the `decode_token_recon_block` copy at |
| 13859 | // 0xc0d030, objdump-measured), and `av1_get_adjusted_tx_size` has no |
| 13860 | // TX_4X8 case. libaom HAS the transform, so the shape CHUNKS (the |
| 13861 | // `chroma422_rect32` arm below) into FOUR stacked TX_4X8 units per |
| 13862 | // plane (`tx_size_high_unit[TX_4X8] = 2`: blk_row 0, 2, 4, 6) |
| 13863 | // instead of refusing; no coefficient-table row is added, because |
| 13864 | // BLOCK_4X32 is not a table libaom has. 4:2:0 and 4:4:4 never land |
| 13865 | // here (their 8x32 strips read (4, 16) and (8, 32)), so the row is |
| 13866 | // 4:2:2-only. |
| 13867 | (16, 8) => (TxbSet::ChromaRect16x8, &SCAN_16X8[..]), |
| 13868 | (8, 4) => (TxbSet::ChromaRect8x4, &SCAN_8X4[..]), |
| 13869 | // lane-av1-422p: the units' own set/scan -- the whole-rect read |
| 13870 | // below never sees this row, the `chroma422_rect32` arm chunks |
| 13871 | // first. Anything else is still a named-lane gap. |
| 13872 | (4, 32) if ss_x(fctx) == 1 && ss_y(fctx) == 0 => { |
| 13873 | (TxbSet::ChromaRect8x4, &SCAN_4X8[..]) |
| 13874 | } |
| 13875 | _ => unreachable!( |
| 13876 | "decode_block_rect4 chroma shape set is closed: {chroma_w}x{chroma_h}" |
| 13877 | ), |
| 13878 | }; |
| 13879 | let luma_scan: &[u16] = if bw > bh { &SCAN_32X8 } else { &SCAN_8X32 }; |
| 13880 | let (luma_levels, u_levels, v_levels) = if skip { |
| 13881 | ( |
| 13882 | vec![0i32; bw * bh], |
| 13883 | vec![0i32; chroma_w * chroma_h], |
| 13884 | vec![0i32; chroma_w * chroma_h], |
| 13885 | ) |
| 13886 | } else if chroma422_rect32 { |
| 13887 | // lane-av1-422p: the (4, 32) plane as FOUR TX_4X8 units per plane, |
| 13888 | // plane-major (every U unit, then every V) -- the walk the pinned |
| 13889 | // oracle's `decode_token_recon_block` makes for the BLOCK_INVALID |
| 13890 | // plane block: TX_4X8, so stepr = `tx_size_high_unit[TX_4X8]` = 2 |
| 13891 | // 4-px rows and stepc = 1, unit rows at chroma y 0, 8, 16, 24. |
| 13892 | // Luma is the same single TX_8X32 rect the general arm reads. |
| 13893 | let around = neighbours.around_mi_rect((mi_r, mi_c), bw, bh); |
| 13894 | let mut luma_coding = cdfs.txb(TxbSet::LumaRect32x8, mode); |
| 13895 | let (luma_levels, luma_tx_type) = read_coeffs_rect( |
| 13896 | dec, |
| 13897 | &mut luma_coding, |
| 13898 | luma_scan, |
| 13899 | bw, |
| 13900 | bh, |
| 13901 | 0, |
| 13902 | dc_sign_ctx(around[0].2), |
| 13903 | TxType::DctDct, |
| 13904 | )?; |
| 13905 | hit!(RECT4_COEFF_HITS); |
| 13906 | let luma_residual = dequant_and_inverse_typed_wh( |
| 13907 | &luma_levels, |
| 13908 | bw, |
| 13909 | bh, |
| 13910 | crate::decode::bit_depth(fctx), |
| 13911 | block_q_idx(fctx), |
| 13912 | plane_q_delta(0, fctx).0, |
| 13913 | plane_q_delta(0, fctx).1, |
| 13914 | luma_tx_type, |
| 13915 | block_iqmatrix(fctx, 0, bw, bh, luma_tx_type), |
| 13916 | ); |
| 13917 | if let Some(b) = &palette_y_buf { |
| 13918 | set_palette_pred(b.clone(), fctx); |
| 13919 | } |
| 13920 | push_intra_rect(0, |
| 13921 | px, |
| 13922 | py, |
| 13923 | bw, |
| 13924 | bh, |
| 13925 | mode, |
| 13926 | angle_delta_y, |
| 13927 | reach, |
| 13928 | &luma_residual, |
| 13929 | None, |
| 13930 | filter_intra, |
| 13931 | smooth_neighbor, fctx, |
| 13932 | ); |
| 13933 | // CfL parameters are computed ONCE per block over the strip's luma |
| 13934 | // (`are_parameters_computed`) and shared by all four units, as in |
| 13935 | // every other chunk arm. |
| 13936 | let ac = alpha.map(|_| cfl_src_rect(px, py, bw, bh)); |
| 13937 | let mut unit_grids: Vec<((usize, usize), usize, Grid)> = Vec::new(); |
| 13938 | for (plane, buf) in [(1usize, &mut *u), (2usize, &mut *v)] { |
| 13939 | for unit_row in 0..4usize { |
| 13940 | let unit_mi = (mi_r + unit_row * 2, mi_c); |
| 13941 | let unit_reach = tu_reach_rect( |
| 13942 | bw, bh, 0, unit_row * 8, 8, 8, reach, |
| 13943 | px, py, y.width, y.height, fctx, |
| 13944 | ); |
| 13945 | if let Some((ub, vb)) = &palette_uv_bufs { |
| 13946 | let pbuf = if plane == 1 { ub } else { vb }; |
| 13947 | set_palette_pred( |
| 13948 | palette_window(pbuf, chroma_w, 0, unit_row * 8, 4, 8), |
| 13949 | fctx, |
| 13950 | ); |
| 13951 | } |
| 13952 | // Each unit is read and recorded IMMEDIATELY: unit row 0's |
| 13953 | // `txb_skip_ctx` still sees the real above/left neighbour |
| 13954 | // states, a lower unit sees this block's own upper unit's |
| 13955 | // (libaom's per-unit `set_entropy_context`), and the lower |
| 13956 | // unit predicts off the upper one's reconstructed samples. |
| 13957 | let chroma_around = neighbours.around_mi_422_chroma(unit_mi, 8, 8); |
| 13958 | let unit_grid = read_rect_chroma_unit( |
| 13959 | dec, |
| 13960 | cdfs, |
| 13961 | TxbSet::ChromaRect8x4, |
| 13962 | &SCAN_4X8, |
| 13963 | 4, |
| 13964 | 8, |
| 13965 | plane, |
| 13966 | chroma_around[plane], |
| 13967 | uv_predict_mode, |
| 13968 | angle_delta_uv, |
| 13969 | unit_reach, |
| 13970 | buf, |
| 13971 | cpx, |
| 13972 | cpy + unit_row * 8, |
| 13973 | base_q_idx, |
| 13974 | // `get_txb_ctx_general`'s chroma branch |
| 13975 | // (txb_common.h): plane_bsize is BLOCK_INVALID (255) |
| 13976 | // here, `num_pels_log2_lookup[255]` = 255 (the +255 |
| 13977 | // byte of the decodetxb copy at 0xc0de70, |
| 13978 | // objdump-measured) exceeds `BLOCK_4X8`'s 5, so the |
| 13979 | // ctx_offset is 10 -- the port's offset-7-first |
| 13980 | // `txb_skip_chroma_8` stores the offset-10 rows at |
| 13981 | // rows 3..6, hence +3. |
| 13982 | Some(3), |
| 13983 | alpha |
| 13984 | .zip(ac) |
| 13985 | .map(|((au, av), ac)| (if plane == 1 { au } else { av }, ac)), |
| 13986 | smooth_neighbor_uv, fctx, |
| 13987 | )?; |
| 13988 | neighbours.record_mi_chroma(unit_mi, 8, 8, plane, &unit_grid); |
| 13989 | unit_grids.push((unit_mi, plane, unit_grid)); |
| 13990 | } |
| 13991 | } |
| 13992 | // The whole-block stamp rewrites the chroma cells ONE state wide, |
| 13993 | // which would erase the unit boundary the next block reads (class |
| 13994 | // `override-slot-on-one-arm`), so it carries zero chroma grids and |
| 13995 | // the per-unit writes replay after it. Its luma cells and side |
| 13996 | // spans are this arm's own bookkeeping. |
| 13997 | let zero_chroma = vec![0i32; chroma_w * chroma_h]; |
| 13998 | neighbours.record_mi_rect( |
| 13999 | (mi_r, mi_c), |
| 14000 | bw, |
| 14001 | bh, |
| 14002 | &[&luma_levels[..], &zero_chroma[..], &zero_chroma[..]], |
| 14003 | ); |
| 14004 | for (unit_mi, plane, grid) in &unit_grids { |
| 14005 | neighbours.record_mi_chroma(*unit_mi, 8, 8, *plane, grid); |
| 14006 | } |
| 14007 | neighbours.fill_skip_grid_rect((mi_r, mi_c), mi_w, mi_h, skip); |
| 14008 | neighbours.fill_lf_grid_rect((mi_r, mi_c), mi_w, mi_h, bw as u8, bh as u8, 0, fctx); |
| 14009 | record_strip_palette( |
| 14010 | neighbours, pal_at, bw, bh, pal_y_size, pal_y_colors, pal_uv_size, |
| 14011 | pal_uv_colors, |
| 14012 | ); |
| 14013 | if bw > bh { |
| 14014 | hit!(RECT4_32_HORZ_HITS); |
| 14015 | } else { |
| 14016 | hit!(RECT4_32_VERT_HITS); |
| 14017 | } |
| 14018 | // lane-frame36 r2 (same shape as `decode_block_rect64`'s split arm, |
| 14019 | // swept this round): `set_txfm_ctxs` runs for EVERY block of an inter |
| 14020 | // frame, so this 1:4 strip -- which is also the INTER frame's reader |
| 14021 | // for 32x8/8x32 ([`decode_intra_rect4_in_inter`]) -- publishes its |
| 14022 | // transform size in both TXFM_CONTEXT bands. Without it the next block |
| 14023 | // reads the row's stale value. |
| 14024 | txfm_partition_update_rect(neighbours, (mi_r, mi_c), (bw, bh), (bw, bh)); |
| 14025 | return Ok((mode, uv_predict_mode)); |
| 14026 | } else { |
| 14027 | let around = neighbours.around_mi_rect((mi_r, mi_c), bw, bh); |
| 14028 | let mut luma_coding = cdfs.txb(TxbSet::LumaRect32x8, mode); |
| 14029 | let (luma_levels, luma_tx_type) = read_coeffs_rect( |
| 14030 | dec, |
| 14031 | &mut luma_coding, |
| 14032 | luma_scan, |
| 14033 | bw, |
| 14034 | bh, |
| 14035 | 0, |
| 14036 | dc_sign_ctx(around[0].2), |
| 14037 | TxType::DctDct, |
| 14038 | )?; |
| 14039 | let mut planes = Vec::with_capacity(2); |
| 14040 | for plane in 1..3 { |
| 14041 | // `av1_get_tx_type` (`blockd.h:1278`): the mode-indexed type |
| 14042 | // stands only while it is a member of this tx size's set; a |
| 14043 | // 32x8/8x32 chroma transform (4:4:4) squares up to TX_32X32, |
| 14044 | // whose intra set is `EXT_TX_SET_DCTONLY` -- every ADST clamps |
| 14045 | // to DCT_DCT there, and ADST is undefined at widths above 16. |
| 14046 | let default_tx = if chroma_w.max(chroma_h) >= 32 { |
| 14047 | TxType::DctDct |
| 14048 | } else { |
| 14049 | default_intra_tx_type(uv_predict_mode as u8) |
| 14050 | }; |
| 14051 | let skip_ctx = usize::from(around[plane].0) + usize::from(around[plane].1); |
| 14052 | let mut coding = cdfs.txb(chroma_set, uv_predict_mode); |
| 14053 | let (levels, tx_type) = read_chroma_coeffs_rect( |
| 14054 | dec, |
| 14055 | &mut coding, |
| 14056 | chroma_scan, |
| 14057 | chroma_w, |
| 14058 | chroma_h, |
| 14059 | skip_ctx, |
| 14060 | dc_sign_ctx(around[plane].2), |
| 14061 | default_tx, |
| 14062 | fctx, |
| 14063 | )?; |
| 14064 | planes.push((levels, tx_type)); |
| 14065 | } |
| 14066 | let (v_levels, v_tx_type) = planes.pop().expect("plane 2"); |
| 14067 | let (u_levels, u_tx_type) = planes.pop().expect("plane 1"); |
| 14068 | hit!(RECT4_COEFF_HITS); |
| 14069 | let luma_residual = dequant_and_inverse_typed_wh( |
| 14070 | &luma_levels, |
| 14071 | bw, |
| 14072 | bh, |
| 14073 | crate::decode::bit_depth(fctx), |
| 14074 | block_q_idx(fctx), |
| 14075 | plane_q_delta(0, fctx).0, |
| 14076 | plane_q_delta(0, fctx).1, |
| 14077 | luma_tx_type, |
| 14078 | block_iqmatrix(fctx, 0, bw, bh, luma_tx_type), |
| 14079 | ); |
| 14080 | if let Some(b) = &palette_y_buf { |
| 14081 | set_palette_pred(b.clone(), fctx); |
| 14082 | } |
| 14083 | push_intra_rect(0, |
| 14084 | px, |
| 14085 | py, |
| 14086 | bw, |
| 14087 | bh, |
| 14088 | mode, |
| 14089 | angle_delta_y, |
| 14090 | reach, |
| 14091 | &luma_residual, |
| 14092 | None, |
| 14093 | filter_intra, |
| 14094 | smooth_neighbor, fctx, |
| 14095 | ); |
| 14096 | let ac = alpha.map(|_| cfl_src_rect(px, py, bw, bh)); |
| 14097 | for (plane, _buf, levels, tx_type) in [ |
| 14098 | (1usize, &mut *u, &u_levels, u_tx_type), |
| 14099 | (2, &mut *v, &v_levels, v_tx_type), |
| 14100 | ] { |
| 14101 | let residual = dequant_and_inverse_typed_wh( |
| 14102 | levels, |
| 14103 | chroma_w, |
| 14104 | chroma_h, |
| 14105 | crate::decode::bit_depth(fctx), |
| 14106 | block_q_idx(fctx), |
| 14107 | plane_q_delta(plane, fctx).0, |
| 14108 | plane_q_delta(plane, fctx).1, |
| 14109 | tx_type, |
| 14110 | block_iqmatrix(fctx, plane, chroma_w, chroma_h, tx_type), |
| 14111 | ); |
| 14112 | if let Some((ub, vb)) = &palette_uv_bufs { |
| 14113 | set_palette_pred(if plane == 1 { ub.clone() } else { vb.clone() }, fctx); |
| 14114 | } |
| 14115 | push_intra_rect(plane, |
| 14116 | cpx, |
| 14117 | cpy, |
| 14118 | chroma_w, |
| 14119 | chroma_h, |
| 14120 | uv_predict_mode, |
| 14121 | angle_delta_uv, |
| 14122 | reach, |
| 14123 | &residual, |
| 14124 | alpha |
| 14125 | .zip(ac) |
| 14126 | .map(|((au, av), ac)| (if plane == 1 { au } else { av }, ac)), |
| 14127 | None, |
| 14128 | smooth_neighbor_uv, fctx, |
| 14129 | ); |
| 14130 | } |
| 14131 | neighbours.record_mi_rect( |
| 14132 | (mi_r, mi_c), |
| 14133 | bw, |
| 14134 | bh, |
| 14135 | &[&luma_levels[..], &u_levels[..], &v_levels[..]], |
| 14136 | ); |
| 14137 | neighbours.fill_skip_grid_rect((mi_r, mi_c), mi_w, mi_h, skip); |
| 14138 | neighbours.fill_lf_grid_rect((mi_r, mi_c), mi_w, mi_h, bw as u8, bh as u8, 0, fctx); |
| 14139 | record_strip_palette( |
| 14140 | neighbours, pal_at, bw, bh, pal_y_size, pal_y_colors, pal_uv_size, |
| 14141 | pal_uv_colors, |
| 14142 | ); |
| 14143 | if bw > bh { |
| 14144 | hit!(RECT4_32_HORZ_HITS); |
| 14145 | } else { |
| 14146 | hit!(RECT4_32_VERT_HITS); |
| 14147 | } |
| 14148 | // lane-frame36 r2 (same shape as `decode_block_rect64`'s split arm, |
| 14149 | // swept this round): `set_txfm_ctxs` runs for EVERY block of an inter |
| 14150 | // frame, so this 1:4 strip -- which is also the INTER frame's reader |
| 14151 | // for 32x8/8x32 ([`decode_intra_rect4_in_inter`]) -- publishes its |
| 14152 | // transform size in both TXFM_CONTEXT bands. Without it the next block |
| 14153 | // reads the row's stale value. |
| 14154 | txfm_partition_update_rect(neighbours, (mi_r, mi_c), (bw, bh), (bw, bh)); |
| 14155 | return Ok((mode, uv_predict_mode)); |
| 14156 | }; |
| 14157 | if let Some(b) = &palette_y_buf { |
| 14158 | set_palette_pred(b.clone(), fctx); |
| 14159 | } |
| 14160 | push_intra_rect(0, |
| 14161 | px, |
| 14162 | py, |
| 14163 | bw, |
| 14164 | bh, |
| 14165 | mode, |
| 14166 | angle_delta_y, |
| 14167 | reach, |
| 14168 | &luma_levels, |
| 14169 | None, |
| 14170 | filter_intra, |
| 14171 | smooth_neighbor, fctx, |
| 14172 | ); |
| 14173 | let ac = alpha.map(|_| cfl_src_rect(px, py, bw, bh)); |
| 14174 | if chroma422_rect32 { |
| 14175 | // lane-av1-422p: `predict_and_reconstruct_intra_block` (decodeframe.c |
| 14176 | // ~1000) predicts EVERY unit regardless of `skip_txfm`, so the skip |
| 14177 | // arm walks the same four TX_4X8 units per plane -- the lower unit |
| 14178 | // predicts off the upper one's predicted samples. No coefficients |
| 14179 | // are coded; the tail's whole-block zero stamp below is the state |
| 14180 | // libaom's `set_entropy_context` leaves. |
| 14181 | for (plane, _buf) in [(1usize, &mut *u), (2usize, &mut *v)] { |
| 14182 | for unit_row in 0..4usize { |
| 14183 | let unit_reach = tu_reach_rect( |
| 14184 | bw, bh, 0, unit_row * 8, 8, 8, reach, |
| 14185 | px, py, y.width, y.height, fctx, |
| 14186 | ); |
| 14187 | if let Some((ub, vb)) = &palette_uv_bufs { |
| 14188 | let pbuf = if plane == 1 { ub } else { vb }; |
| 14189 | set_palette_pred( |
| 14190 | palette_window(pbuf, chroma_w, 0, unit_row * 8, 4, 8), |
| 14191 | fctx, |
| 14192 | ); |
| 14193 | } |
| 14194 | push_intra_rect(plane, |
| 14195 | cpx, |
| 14196 | cpy + unit_row * 8, |
| 14197 | 4, |
| 14198 | 8, |
| 14199 | uv_predict_mode, |
| 14200 | angle_delta_uv, |
| 14201 | unit_reach, |
| 14202 | &ZERO_RESIDUAL[..4 * 8], |
| 14203 | alpha |
| 14204 | .zip(ac) |
| 14205 | .map(|((au, av), ac)| (if plane == 1 { au } else { av }, ac)), |
| 14206 | None, |
| 14207 | smooth_neighbor_uv, fctx, |
| 14208 | ); |
| 14209 | } |
| 14210 | } |
| 14211 | } else { |
| 14212 | if let Some((ub, _)) = &palette_uv_bufs { |
| 14213 | set_palette_pred(ub.clone(), fctx); |
| 14214 | } |
| 14215 | push_intra_rect(1, |
| 14216 | cpx, |
| 14217 | cpy, |
| 14218 | chroma_w, |
| 14219 | chroma_h, |
| 14220 | uv_predict_mode, |
| 14221 | angle_delta_uv, |
| 14222 | reach, |
| 14223 | &u_levels, |
| 14224 | alpha.zip(ac).map(|((au, _), ac)| (au, ac)), |
| 14225 | None, |
| 14226 | smooth_neighbor_uv, fctx, |
| 14227 | ); |
| 14228 | if let Some((_, vb)) = &palette_uv_bufs { |
| 14229 | set_palette_pred(vb.clone(), fctx); |
| 14230 | } |
| 14231 | push_intra_rect(2, |
| 14232 | cpx, |
| 14233 | cpy, |
| 14234 | chroma_w, |
| 14235 | chroma_h, |
| 14236 | uv_predict_mode, |
| 14237 | angle_delta_uv, |
| 14238 | reach, |
| 14239 | &v_levels, |
| 14240 | alpha.zip(ac).map(|((_, av), ac)| (av, ac)), |
| 14241 | None, |
| 14242 | smooth_neighbor_uv, fctx, |
| 14243 | ); |
| 14244 | } |
| 14245 | neighbours.record_mi_rect((mi_r, mi_c), bw, bh, &[&luma_levels[..], &u_levels[..], &v_levels[..]]); |
| 14246 | neighbours.fill_skip_grid_rect((mi_r, mi_c), mi_w, mi_h, skip); |
| 14247 | neighbours.fill_lf_grid_rect((mi_r, mi_c), mi_w, mi_h, bw as u8, bh as u8, 0, fctx); |
| 14248 | record_strip_palette( |
| 14249 | neighbours, pal_at, bw, bh, pal_y_size, pal_y_colors, pal_uv_size, pal_uv_colors, |
| 14250 | ); |
| 14251 | if bw > bh { |
| 14252 | hit!(RECT4_32_HORZ_HITS); |
| 14253 | } else { |
| 14254 | hit!(RECT4_32_VERT_HITS); |
| 14255 | } |
| 14256 | // lane-frame36 r2 (same shape as `decode_block_rect64`'s split arm, |
| 14257 | // swept this round): `set_txfm_ctxs` runs for EVERY block of an inter |
| 14258 | // frame, so this 1:4 strip -- which is also the INTER frame's reader |
| 14259 | // for 32x8/8x32 ([`decode_intra_rect4_in_inter`]) -- publishes its |
| 14260 | // transform size in both TXFM_CONTEXT bands. Without it the next block |
| 14261 | // reads the row's stale value. |
| 14262 | txfm_partition_update_rect(neighbours, (mi_r, mi_c), (bw, bh), (bw, bh)); |
| 14263 | Ok((mode, uv_predict_mode)) |
| 14264 | } |
| 14265 | |
| 14266 | /// Decodes the four 16x4 / 4x16 strips of a 16x16-level `PARTITION_HORZ_4` / |
| 14267 | /// `PARTITION_VERT_4` (lane-r14) -- [`decode_block_rect4`]'s pair one level |
| 14268 | /// down, and the first shape here whose luma block is a single mi row (or |
| 14269 | /// column). |
| 14270 | /// |
| 14271 | /// What is new at this size is CHROMA. `is_chroma_reference` |
| 14272 | /// (`av1_common_int.h`) is false for a 4:2:0 block one mi tall (wide) on an |
| 14273 | /// EVEN mi row (column), so strips 0 and 2 read no `uv_mode`/`cfl_alpha`/ |
| 14274 | /// `angle_delta_uv` at all and code no chroma residual; strips 1 and 3 close |
| 14275 | /// the pair and carry the chroma of the whole 16x8 (8x16) luma pair -- |
| 14276 | /// `setup_pred_plane`'s own `if (subsampling_y && (mi_row & 1) && |
| 14277 | /// mi_size_high[bsize] == 1) mi_row -= 1` offset, i.e. an 8x4 (4x8) chroma |
| 14278 | /// transform at the PAIR's origin, with CfL averaging over the pair's luma. |
| 14279 | /// |
| 14280 | /// Tables: LUMA `TX_16X4`/`TX_4X16` averages to the 8x8 entropy context |
| 14281 | /// ((0 + 2 + 1) >> 1 = 1) with 64 coded positions, so its CDF set is |
| 14282 | /// [`TxbSet::LumaRect16x4`] (`Luma8`'s tables, the `TX_4X4` `tx_type` row); |
| 14283 | /// scans are [`SCAN_16X4`]/[`SCAN_4X16`]. CHROMA `TX_8X4`/`TX_4X8` is |
| 14284 | /// [`TxbSet::ChromaRect8x4`] with [`SCAN_8X4`]/[`SCAN_4X8`]. Angle deltas ARE |
| 14285 | /// coded (`av1_use_angle_delta` compares the BLOCK_SIZES_ALL enum index, and |
| 14286 | /// BLOCK_16X4 is 17), and so is `use_filter_intra` (both sides <= 32) off this |
| 14287 | /// shape's own CDF row. |
| 14288 | /// |
| 14289 | /// A split transform (`tx_depth != 0`, `sub_tx_size_map[TX_16X4] == TX_8X4`) |
| 14290 | /// refuses by name rather than guess-decode. |
| 14291 | #[allow(clippy::too_many_arguments)] |
| 14292 | fn decode_rect4_16( |
| 14293 | dec: &mut SymbolDecoder, |
| 14294 | cdfs: &mut Cdfs, |
| 14295 | neighbours: &mut Neighbours, |
| 14296 | at16: (usize, usize), |
| 14297 | horz: bool, |
| 14298 | y: &mut PlaneBuf<'static>, |
| 14299 | u: &mut PlaneBuf<'static>, |
| 14300 | v: &mut PlaneBuf<'static>, |
| 14301 | enable_filter_intra: bool, |
| 14302 | allow_screen_content_tools: bool, |
| 14303 | base_q_idx: u8, |
| 14304 | tx_select: bool, |
| 14305 | reduced_tx_set: bool, fctx: &crate::decode::FrameCtx, |
| 14306 | ) -> Result<()> { |
| 14307 | let (r, c) = at16; |
| 14308 | let (mi_row0, mi_col0) = (r * (SUB / MI), c * (SUB / MI)); |
| 14309 | let (bw, bh) = if horz { (16, 4) } else { (4, 16) }; |
| 14310 | let (_mi_w, _mi_h) = (bw / MI, bh / MI); |
| 14311 | let _scan: &[u16] = if horz { &SCAN_16X4 } else { &SCAN_4X16 }; |
| 14312 | let _chroma_scan: &[u16] = if horz { &SCAN_8X4 } else { &SCAN_4X8 }; |
| 14313 | let mut prev: Option<((usize, usize), usize)> = None; |
| 14314 | let mut last_mode = DC_PRED; |
| 14315 | let mut last_uv_mode = DC_PRED; |
| 14316 | for i in 0..4usize { |
| 14317 | let lmi = if horz { |
| 14318 | (mi_row0 + i, mi_col0) |
| 14319 | } else { |
| 14320 | (mi_row0, mi_col0 + i) |
| 14321 | }; |
| 14322 | // lane-av1-444: at 4:4:4 every strip is its own chroma reference |
| 14323 | // (no mi-parity test at ss 0/0, and the strip's own chroma block is |
| 14324 | // already >= 4x4) -- there is no 4:2:0 pair to close. |
| 14325 | // lane-av1-422o: the parity rule is per axis, straight out of |
| 14326 | // `is_chroma_reference`: a HORZ 16x4 strip (1 mi tall) keeps the |
| 14327 | // `ss_y == 0 -> every strip a reference` row rule, but a VERT 4x16 |
| 14328 | // strip (1 mi wide) keeps the COLUMN clause `(mi_col & 1) || !(bw & |
| 14329 | // 1) || !ss_x`, which with bw == 1 mi reduces to `mi_col odd` for |
| 14330 | // every ss_x == 1 format -- the ODD strip closes the pair and reads |
| 14331 | // the chroma, exactly like 4:2:0. (VERT_4 partitions start on an |
| 14332 | // even mi_col, so `i % 2 == 1` is that clause.) |
| 14333 | let has_chroma = i % 2 == 1 |
| 14334 | || (horz && ss_y(fctx) == 0) |
| 14335 | || (!horz && ss_x(fctx) == 0); |
| 14336 | let (mode, uv, _tx) = decode_rect4_16_strip( |
| 14337 | dec, |
| 14338 | cdfs, |
| 14339 | neighbours, |
| 14340 | lmi, |
| 14341 | horz, |
| 14342 | has_chroma, |
| 14343 | prev, |
| 14344 | y, |
| 14345 | u, |
| 14346 | v, |
| 14347 | enable_filter_intra, |
| 14348 | allow_screen_content_tools, |
| 14349 | base_q_idx, |
| 14350 | tx_select, |
| 14351 | reduced_tx_set, fctx, |
| 14352 | )?; |
| 14353 | if let Some(m) = uv { |
| 14354 | last_uv_mode = m; |
| 14355 | } |
| 14356 | last_mode = mode; |
| 14357 | prev = Some((lmi, mode)); |
| 14358 | } |
| 14359 | // The coarse 16-px cell holds one entry for the whole 16x16: the last |
| 14360 | // strip is what a block below (right) sees through it, the mi-exact maps |
| 14361 | // above answering everything finer. |
| 14362 | neighbours.above_mode[c] = last_mode; |
| 14363 | neighbours.left_mode[r] = last_mode; |
| 14364 | neighbours.above_uv_mode[c] = last_uv_mode; |
| 14365 | neighbours.left_uv_mode[r] = last_uv_mode; |
| 14366 | neighbours.above_side[c] = bw; |
| 14367 | neighbours.left_side[r] = bh; |
| 14368 | Ok(()) |
| 14369 | } |
| 14370 | |
| 14371 | /// One 16x4 / 4x16 strip of a 16x16-level 1:4 partition, intra-coded -- |
| 14372 | /// [`decode_rect4_16`]'s per-strip body, lifted (lane-intra16x4) so the INTER |
| 14373 | /// block path can decode a SINGLE intra strip of a partition whose other |
| 14374 | /// strips are inter. `prev` is the previous strip's `(mi, mode)` (the |
| 14375 | /// key-frame caller's coarse-cell stand-in; `None` on the inter path, where |
| 14376 | /// every strip publishes its own mi-exact mode). Returns |
| 14377 | /// `(y_mode, uv_predict_mode when this strip closes the 4:2:0 chroma pair)`. |
| 14378 | /// Intrabc reconstruction for a rect strip that OWNS its chroma |
| 14379 | /// (`bw/2 x bh/2` at the strip origin). 16x4/4x16 is not this shape -- that |
| 14380 | /// pair goes through [`decode_rect4_16_intrabc`]. |
| 14381 | /// |
| 14382 | /// This is the same control flow as the unmerged lane's `decode_intrabc_rect` |
| 14383 | /// (predict once, then one residual per transform unit on the INTER sets, |
| 14384 | /// chroma inherits luma's `tx_type`), written against the helpers that exist |
| 14385 | /// on this tree so a 2:1 strip no longer hides the 16x4 pair witness. The |
| 14386 | /// merge with lane-av1-intrabc should keep one of the two bodies; the call |
| 14387 | /// shape is the same. |
| 14388 | #[allow(clippy::too_many_arguments)] |
| 14389 | fn decode_intrabc_owned_rect( |
| 14390 | dec: &mut SymbolDecoder, |
| 14391 | cdfs: &mut Cdfs, |
| 14392 | neighbours: &mut Neighbours, |
| 14393 | (mi_r, mi_c): (usize, usize), |
| 14394 | (bw, bh): (usize, usize), |
| 14395 | skip: bool, |
| 14396 | (dv_row, dv_col): (i32, i32), |
| 14397 | y: &mut PlaneBuf<'static>, |
| 14398 | u: &mut PlaneBuf<'static>, |
| 14399 | v: &mut PlaneBuf<'static>, |
| 14400 | base_q_idx: u8, |
| 14401 | tx_select: bool, |
| 14402 | reduced_tx_set: bool, |
| 14403 | fctx: &crate::decode::FrameCtx, |
| 14404 | ) -> Result<()> { |
| 14405 | let _ = base_q_idx; |
| 14406 | let (px, py) = (mi_c * MI, mi_r * MI); |
| 14407 | let (cw, ch) = (bw / 2, bh / 2); |
| 14408 | let side = bw.max(bh); |
| 14409 | let cside = cw.max(ch); |
| 14410 | let saved_reduced = fctx.reduced_tx_set_inter.with(std::cell::Cell::get); |
| 14411 | let saved_tx = fctx.tx_select_inter.with(std::cell::Cell::get); |
| 14412 | fctx.reduced_tx_set_inter.with(|c| c.set(reduced_tx_set)); |
| 14413 | // `read_block_tx_size_rect` keys on this cell. A key frame forces it |
| 14414 | // false; the real bit is the caller's `tx_select`. Restored before return. |
| 14415 | fctx.tx_select_inter.with(|c| c.set(tx_select)); |
| 14416 | let restore = ReducedTxRestore { fctx, saved_reduced, saved_tx }; |
| 14417 | |
| 14418 | flush_recon(fctx, y, u, v); |
| 14419 | let pred_y = predict_intrabc_window(y, px, py, bw, bh, dv_col, dv_row, true, side, fctx); |
| 14420 | let pred_u = predict_intrabc_window(u, px / 2, py / 2, cw, ch, dv_col, dv_row, false, cside, fctx); |
| 14421 | let pred_v = predict_intrabc_window(v, px / 2, py / 2, cw, ch, dv_col, dv_row, false, cside, fctx); |
| 14422 | |
| 14423 | let mi_cols = 2 * y.true_width.div_ceil(8); |
| 14424 | let mi_rows = 2 * y.true_height.div_ceil(8); |
| 14425 | let leaves = read_block_tx_size_rect( |
| 14426 | dec, cdfs, neighbours, (mi_r, mi_c), (bw, bh), (mi_cols, mi_rows), skip, fctx, |
| 14427 | )?; |
| 14428 | let mut luma_tx_type = TxType::DctDct; |
| 14429 | let luma_grid; |
| 14430 | let u_grid; |
| 14431 | let v_grid; |
| 14432 | if skip { |
| 14433 | // `reconstruct_mc_rect` indexes the residual at the square stride. |
| 14434 | // `bw * bh` underruns on a VERT strip (8x16: stride 16, 16 rows, index 136). |
| 14435 | push_mc_rect(0, px, py, side, bw, bh, Pred::Inline(&pred_y, side), &ZERO_RESIDUAL[..side * bh], fctx); |
| 14436 | push_mc_rect(1, px / 2, py / 2, cside, cw, ch, Pred::Inline(&pred_u, cside), &ZERO_RESIDUAL[..cside * ch], fctx); |
| 14437 | push_mc_rect(2, px / 2, py / 2, cside, cw, ch, Pred::Inline(&pred_v, cside), &ZERO_RESIDUAL[..cside * ch], fctx); |
| 14438 | if let Some(ls) = leaves.as_ref() { |
| 14439 | // libaom `av1_reset_entropy_context` (`if (mbmi->skip_txfm)`, |
| 14440 | // decodeframe.c:1262 -> blockd.c:58): a SKIPPED block zeroes its |
| 14441 | // whole entropy-band footprint. The non-skip arm below writes |
| 14442 | // those bands per transform unit and the tail's |
| 14443 | // `record_split_luma_rect_mi` writes chroma only, so without |
| 14444 | // this walk a skipped strip whose `leaves` resolved (every |
| 14445 | // lossless frame -- `read_block_tx_size_rect`'s lossless arm |
| 14446 | // returns the 4x4 grid before it looks at `skip`) leaked the |
| 14447 | // PREVIOUS occupant's luma bands into the next block's |
| 14448 | // `txb_skip`/coefficient contexts (lane-av1-loss64: the skipped |
| 14449 | // 8x16 intrabc strip at mi(48,66) of the 320x242 lossless sb128 |
| 14450 | // stream left a stale 7 in left[48] and mi(48,68)'s first |
| 14451 | // transform unit read `txb_skip_ctx` 3 where aomdec reads 1). |
| 14452 | hit!(SKIP_LOSSLESS_BAND_RESET_HITS); |
| 14453 | for &(row, col, tw, th) in ls { |
| 14454 | neighbours.record_mi_luma_rect( |
| 14455 | (mi_r + row, mi_c + col), |
| 14456 | tw, |
| 14457 | th, |
| 14458 | &ZERO_RESIDUAL[..tw * th], |
| 14459 | ); |
| 14460 | } |
| 14461 | } |
| 14462 | luma_grid = Grid::Zero(side * side); |
| 14463 | u_grid = Grid::Zero(cside * cside); |
| 14464 | v_grid = Grid::Zero(cside * cside); |
| 14465 | } else if let Some(ls) = leaves.as_ref() { |
| 14466 | for (idx, &(row, col, tw, th)) in ls.iter().enumerate() { |
| 14467 | let tu_mi = (mi_r + row, mi_c + col); |
| 14468 | let (tu_px, tu_py) = (px + col * MI, py + row * MI); |
| 14469 | let tu_pred = Pred::Inline(&pred_y, side).offset(row * MI * side + col * MI); |
| 14470 | let (g, tt) = if tw == th { |
| 14471 | read_inter_plane( |
| 14472 | dec, cdfs, inter_txbset_for(tw, reduced_tx_set), &default_scan(tw.min(32)), |
| 14473 | 0, neighbours.around_mi(tu_mi, tw)[0], 0, y, tu_px, tu_py, tw, base_q_idx, |
| 14474 | tu_pred, None, Some(neighbours.luma_skip_ctx(tu_mi, tw / MI)), fctx, |
| 14475 | )? |
| 14476 | } else { |
| 14477 | read_inter_plane_rect( |
| 14478 | dec, cdfs, rect_inter_luma_set(tw, th, fctx)?, (tw, th), side, 0, |
| 14479 | neighbours.around_mi_rect(tu_mi, tw, th)[0], 0, y, tu_px, tu_py, tu_pred, |
| 14480 | None, Some(neighbours.luma_skip_ctx_rect(tu_mi, tw / MI, th / MI)), fctx, |
| 14481 | )? |
| 14482 | }; |
| 14483 | if idx == 0 { |
| 14484 | luma_tx_type = tt; |
| 14485 | } |
| 14486 | neighbours.record_mi_luma_rect(tu_mi, tw, th, &g); |
| 14487 | if tw != th { |
| 14488 | neighbours.fill_lf_grid_leaf_luma((tu_mi.0, tu_mi.1), tw / MI, th / MI, tw as u8, th as u8); |
| 14489 | } |
| 14490 | } |
| 14491 | luma_grid = Grid::Zero(side * side); |
| 14492 | let around = neighbours.around_mi_rect((mi_r, mi_c), bw, bh); |
| 14493 | let (ug, _) = read_inter_plane_rect( |
| 14494 | dec, cdfs, rect_inter_chroma_set(cw, ch)?, (cw, ch), cside, 1, around[1], 0, |
| 14495 | u, px / 2, py / 2, Pred::Inline(&pred_u, cside), Some(luma_tx_type), None, fctx, |
| 14496 | )?; |
| 14497 | let (vg, _) = read_inter_plane_rect( |
| 14498 | dec, cdfs, rect_inter_chroma_set(cw, ch)?, (cw, ch), cside, 2, around[2], 0, |
| 14499 | v, px / 2, py / 2, Pred::Inline(&pred_v, cside), Some(luma_tx_type), None, fctx, |
| 14500 | )?; |
| 14501 | u_grid = ug; |
| 14502 | v_grid = vg; |
| 14503 | } else { |
| 14504 | let around = neighbours.around_mi_rect((mi_r, mi_c), bw, bh); |
| 14505 | let (g, tt) = read_inter_plane_rect( |
| 14506 | dec, cdfs, rect_inter_luma_set(bw, bh, fctx)?, (bw, bh), side, 0, around[0], 0, |
| 14507 | y, px, py, Pred::Inline(&pred_y, side), None, None, fctx, |
| 14508 | )?; |
| 14509 | luma_tx_type = tt; |
| 14510 | luma_grid = g; |
| 14511 | let (ug, _) = read_inter_plane_rect( |
| 14512 | dec, cdfs, rect_inter_chroma_set(cw, ch)?, (cw, ch), cside, 1, around[1], 0, |
| 14513 | u, px / 2, py / 2, Pred::Inline(&pred_u, cside), Some(luma_tx_type), None, fctx, |
| 14514 | )?; |
| 14515 | let (vg, _) = read_inter_plane_rect( |
| 14516 | dec, cdfs, rect_inter_chroma_set(cw, ch)?, (cw, ch), cside, 2, around[2], 0, |
| 14517 | v, px / 2, py / 2, Pred::Inline(&pred_v, cside), Some(luma_tx_type), None, fctx, |
| 14518 | )?; |
| 14519 | u_grid = ug; |
| 14520 | v_grid = vg; |
| 14521 | } |
| 14522 | drop(restore); |
| 14523 | if leaves.is_some() { |
| 14524 | neighbours.record_split_luma_rect_mi((mi_r, mi_c), bw, bh, DC_PRED, DC_PRED, [&u_grid, &v_grid]); |
| 14525 | } else { |
| 14526 | neighbours.record_rect_mi((mi_r, mi_c), bw, bh, DC_PRED, DC_PRED, &[&luma_grid, &u_grid, &v_grid]); |
| 14527 | } |
| 14528 | neighbours.record_palette_y_rect((mi_r, mi_c), bw, bh, 0, [0u16; 8]); |
| 14529 | neighbours.record_palette_uv_rect((mi_r, mi_c), bw, bh, 0, [0u16; 8]); |
| 14530 | neighbours.fill_skip_grid_rect((mi_r, mi_c), bw / MI, bh / MI, skip); |
| 14531 | 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); |
| 14532 | record_intrabc_mi_rect(mi_r, mi_c, bw / MI, bh / MI, Some((dv_row, dv_col)), neighbours, fctx); |
| 14533 | let _ = luma_tx_type; |
| 14534 | Ok(()) |
| 14535 | } |
| 14536 | |
| 14537 | fn predict_intrabc_window( |
| 14538 | plane: &PlaneBuf<'_>, |
| 14539 | px: usize, |
| 14540 | py: usize, |
| 14541 | w: usize, |
| 14542 | h: usize, |
| 14543 | dv_col: i32, |
| 14544 | dv_row: i32, |
| 14545 | luma: bool, |
| 14546 | stride: usize, |
| 14547 | fctx: &crate::decode::FrameCtx, |
| 14548 | ) -> Vec<u16> { |
| 14549 | let mut dense = vec![0u16; w * h]; |
| 14550 | mc::predict_with_filter( |
| 14551 | &plane.data, plane.width, plane.true_width, plane.true_height, |
| 14552 | 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) }), |
| 14553 | w, h, mc::InterpFilterKind::Bilinear, &mut dense, fctx, |
| 14554 | ); |
| 14555 | let mut laid = vec![0u16; stride * stride]; |
| 14556 | for row in 0..h { |
| 14557 | laid[row * stride..][..w].copy_from_slice(&dense[row * w..][..w]); |
| 14558 | } |
| 14559 | laid |
| 14560 | } |
| 14561 | |
| 14562 | struct ReducedTxRestore<'a> { |
| 14563 | fctx: &'a crate::decode::FrameCtx, |
| 14564 | saved_reduced: bool, |
| 14565 | saved_tx: bool, |
| 14566 | } |
| 14567 | impl Drop for ReducedTxRestore<'_> { |
| 14568 | fn drop(&mut self) { |
| 14569 | self.fctx.reduced_tx_set_inter.with(|c| c.set(self.saved_reduced)); |
| 14570 | self.fctx.tx_select_inter.with(|c| c.set(self.saved_tx)); |
| 14571 | } |
| 14572 | } |
| 14573 | |
| 14574 | /// Reconstruction of one intrabc 16x4 / 4x16 strip (lane-av1-rect14). |
| 14575 | /// |
| 14576 | /// A 1:4 strip is a PAIR. Both children share one chroma block, and only the |
| 14577 | /// odd-mi member codes it (`is_chroma_reference`, `av1_common_int.h:1454`: |
| 14578 | /// the bottom strip of a `HORZ_4`, the right strip of a `VERT_4`). The chroma |
| 14579 | /// plane block is `ss_size_lookup[BLOCK_16X4][1][1] == BLOCK_8X4` (and |
| 14580 | /// `BLOCK_4X8` for `VERT_4`) at the PAIR origin -- `setup_pred_plane`'s |
| 14581 | /// `mi_row -= 1` when the block is one mi tall on an odd row. Luma is this |
| 14582 | /// strip's own footprint, copied from this strip's DV. The block is |
| 14583 | /// `is_inter_block`, so the residual uses the INTER sets and chroma inherits |
| 14584 | /// the luma unit's coded `tx_type`. |
| 14585 | /// |
| 14586 | /// Not [`decode_intrabc_rect`]: that helper (lane-av1-intrabc, not on this |
| 14587 | /// tree) predicts chroma at `bw/2 x bh/2` of the strip itself. At 16x4 that |
| 14588 | /// is an 8x2 half-pair at the odd strip -- wrong size, wrong origin, and it |
| 14589 | /// would code chroma on every strip. |
| 14590 | #[allow(clippy::too_many_arguments)] |
| 14591 | fn decode_rect4_16_intrabc( |
| 14592 | dec: &mut SymbolDecoder, |
| 14593 | cdfs: &mut Cdfs, |
| 14594 | neighbours: &mut Neighbours, |
| 14595 | lmi: (usize, usize), |
| 14596 | horz: bool, |
| 14597 | has_chroma: bool, |
| 14598 | skip: bool, |
| 14599 | (dv_row, dv_col): (i32, i32), |
| 14600 | y: &mut PlaneBuf<'static>, |
| 14601 | u: &mut PlaneBuf<'static>, |
| 14602 | v: &mut PlaneBuf<'static>, |
| 14603 | base_q_idx: u8, |
| 14604 | tx_select: bool, |
| 14605 | reduced_tx_set: bool, |
| 14606 | fctx: &crate::decode::FrameCtx, |
| 14607 | ) -> Result<(usize, Option<usize>, (usize, usize))> { |
| 14608 | let _ = base_q_idx; |
| 14609 | hit!(RECT4_16_INTRABC_HITS); |
| 14610 | let (bw, bh) = if horz { (16usize, 4usize) } else { (4, 16) }; |
| 14611 | let (mi_w, mi_h) = (bw / MI, bh / MI); |
| 14612 | let (px, py) = (lmi.1 * MI, lmi.0 * MI); |
| 14613 | let side = bw.max(bh); |
| 14614 | // `rect_inter_luma_set` reads the frame cell. A key frame's tile decoder |
| 14615 | // publishes `reduced_tx_set` into it; set it here too so a caller that |
| 14616 | // passed the bit explicitly cannot disagree with the set we read. |
| 14617 | let saved_reduced = fctx.reduced_tx_set_inter.with(std::cell::Cell::get); |
| 14618 | fctx.reduced_tx_set_inter.with(|c| c.set(reduced_tx_set)); |
| 14619 | let restored = scopeguard_reduced(fctx, saved_reduced); |
| 14620 | |
| 14621 | flush_recon(fctx, y, u, v); |
| 14622 | let mut pred_y_dense = vec![0u16; bw * bh]; |
| 14623 | mc::predict_with_filter( |
| 14624 | &y.data, y.width, y.true_width, y.true_height, |
| 14625 | mv_to_q4(px, dv_col, 0), mv_to_q4(py, dv_row, 0), |
| 14626 | bw, bh, mc::InterpFilterKind::Bilinear, &mut pred_y_dense, fctx, |
| 14627 | ); |
| 14628 | let mut pred_y = vec![0u16; side * side]; |
| 14629 | for row in 0..bh { |
| 14630 | pred_y[row * side..][..bw].copy_from_slice(&pred_y_dense[row * bw..][..bw]); |
| 14631 | } |
| 14632 | |
| 14633 | let (pw, ph) = if horz { (16usize, 8usize) } else { (8, 16) }; |
| 14634 | let (cw, ch) = (pw / 2, ph / 2); |
| 14635 | let pair_mi = if has_chroma { |
| 14636 | if horz { (lmi.0 - 1, lmi.1) } else { (lmi.0, lmi.1 - 1) } |
| 14637 | } else { |
| 14638 | lmi |
| 14639 | }; |
| 14640 | let (cpx, cpy) = if has_chroma { |
| 14641 | (pair_mi.1 * MI / 2, pair_mi.0 * MI / 2) |
| 14642 | } else { |
| 14643 | (0, 0) |
| 14644 | }; |
| 14645 | let cside = cw.max(ch); |
| 14646 | let (pred_u, pred_v) = if has_chroma { |
| 14647 | let mut ud = vec![0u16; cw * ch]; |
| 14648 | mc::predict_with_filter( |
| 14649 | &u.data, u.width, u.true_width, u.true_height, |
| 14650 | mv_to_q4(cpx, dv_col, ss_x(fctx)), mv_to_q4(cpy, dv_row, ss_y(fctx)), |
| 14651 | cw, ch, mc::InterpFilterKind::Bilinear, &mut ud, fctx, |
| 14652 | ); |
| 14653 | let mut us = vec![0u16; cside * cside]; |
| 14654 | for row in 0..ch { |
| 14655 | us[row * cside..][..cw].copy_from_slice(&ud[row * cw..][..cw]); |
| 14656 | } |
| 14657 | let mut vd = vec![0u16; cw * ch]; |
| 14658 | mc::predict_with_filter( |
| 14659 | &v.data, v.width, v.true_width, v.true_height, |
| 14660 | mv_to_q4(cpx, dv_col, ss_x(fctx)), mv_to_q4(cpy, dv_row, ss_y(fctx)), |
| 14661 | cw, ch, mc::InterpFilterKind::Bilinear, &mut vd, fctx, |
| 14662 | ); |
| 14663 | let mut vs = vec![0u16; cside * cside]; |
| 14664 | for row in 0..ch { |
| 14665 | vs[row * cside..][..cw].copy_from_slice(&vd[row * cw..][..cw]); |
| 14666 | } |
| 14667 | (us, vs) |
| 14668 | } else { |
| 14669 | (Vec::new(), Vec::new()) |
| 14670 | }; |
| 14671 | |
| 14672 | // Key-frame `tx_select` is the parameter. `tx_select_inter` is forced |
| 14673 | // false for a key frame, so `read_block_tx_size_rect` would skip the |
| 14674 | // symbol; read it here against the parameter the intra path already uses. |
| 14675 | // A skipped intrabc block predicts the whole footprint once and codes |
| 14676 | // no residual (`decode_token_recon_block`'s inter branch). Lossless |
| 14677 | // still forces TX_4X4 when the block is coded. |
| 14678 | let leaves = if skip || (!lossless(fctx) && !tx_select) { |
| 14679 | None |
| 14680 | } else if lossless(fctx) { |
| 14681 | let mut leaves = Vec::new(); |
| 14682 | for row in 0..bh / MI { |
| 14683 | for col in 0..bw / MI { |
| 14684 | leaves.push((row, col, 4usize, 4usize)); |
| 14685 | } |
| 14686 | } |
| 14687 | Some(leaves) |
| 14688 | } else { |
| 14689 | let ctx = txfm_partition_ctx_rect( |
| 14690 | neighbours.above_txfm[lmi.1], |
| 14691 | neighbours.left_txfm[lmi.0], |
| 14692 | side, |
| 14693 | bw, |
| 14694 | bh, |
| 14695 | ); |
| 14696 | let split = dec.symbol(&mut cdfs.txfm_partition[ctx]) == 1; |
| 14697 | if crate::envflags::env_flag!("EC_TRACE_MODE_STEP") { |
| 14698 | let (rng, _) = dec.debug_state(); |
| 14699 | eprintln!( |
| 14700 | "EC_ISTEP mi_row={} mi_col={} name=txfm_split_rect val={} ctx={ctx} rng={rng}", |
| 14701 | lmi.0, lmi.1, u8::from(split) |
| 14702 | ); |
| 14703 | } |
| 14704 | if !split { |
| 14705 | None |
| 14706 | } else { |
| 14707 | let (sub_w, sub_h) = sub_tx_size_map(bw, bh); |
| 14708 | let mi_cols = 2 * y.true_width.div_ceil(8); |
| 14709 | let mi_rows = 2 * y.true_height.div_ceil(8); |
| 14710 | let max_w_mi = (bw / MI).min(mi_cols.saturating_sub(lmi.1)); |
| 14711 | let max_h_mi = (bh / MI).min(mi_rows.saturating_sub(lmi.0)); |
| 14712 | let mut out = Vec::new(); |
| 14713 | for row in (0..bh / MI).step_by(sub_h / MI) { |
| 14714 | for col in (0..bw / MI).step_by(sub_w / MI) { |
| 14715 | read_var_tx_size( |
| 14716 | dec, cdfs, neighbours, lmi, side, (max_w_mi, max_h_mi), |
| 14717 | (sub_w, sub_h), 1, row, col, &mut out, |
| 14718 | ); |
| 14719 | } |
| 14720 | } |
| 14721 | Some(out) |
| 14722 | } |
| 14723 | }; |
| 14724 | let (tx_w, tx_h) = leaves.as_ref().and_then(|ls| ls.first()).map_or((bw, bh), |l| (l.2, l.3)); |
| 14725 | |
| 14726 | let mut luma_tx_type = TxType::DctDct; |
| 14727 | let luma_grid; |
| 14728 | let mut u_grid = Grid::Zero(cw * ch); |
| 14729 | let mut v_grid = Grid::Zero(cw * ch); |
| 14730 | let chroma_stamped_per_unit; |
| 14731 | |
| 14732 | if skip { |
| 14733 | push_mc_rect( |
| 14734 | 0, px, py, side, bw, bh, Pred::Inline(&pred_y, side), &ZERO_RESIDUAL[..side * bh], fctx, |
| 14735 | ); |
| 14736 | luma_grid = Grid::Zero(bw * bh); |
| 14737 | if has_chroma { |
| 14738 | push_mc_rect( |
| 14739 | 1, cpx, cpy, cside, cw, ch, Pred::Inline(&pred_u, cside), |
| 14740 | &ZERO_RESIDUAL[..cside * ch], fctx, |
| 14741 | ); |
| 14742 | push_mc_rect( |
| 14743 | 2, cpx, cpy, cside, cw, ch, Pred::Inline(&pred_v, cside), |
| 14744 | &ZERO_RESIDUAL[..cside * ch], fctx, |
| 14745 | ); |
| 14746 | } |
| 14747 | chroma_stamped_per_unit = false; |
| 14748 | } else if let Some(ls) = leaves.as_ref() { |
| 14749 | if lossless(fctx) { |
| 14750 | for &(row, col, _tw, _th) in ls { |
| 14751 | let tu_mi = (lmi.0 + row, lmi.1 + col); |
| 14752 | let (tu_px, tu_py) = (px + col * MI, py + row * MI); |
| 14753 | let tu_pred = Pred::Inline(&pred_y, side).offset(row * MI * side + col * MI); |
| 14754 | let (g, _) = read_inter_plane( |
| 14755 | dec, cdfs, |
| 14756 | if reduced_tx_set { TxbSet::Luma4Inter } else { TxbSet::Luma4InterSet1 }, |
| 14757 | &default_scan(4), 0, neighbours.around_mi(tu_mi, 4)[0], 0, |
| 14758 | y, tu_px, tu_py, 4, base_q_idx, tu_pred, None, |
| 14759 | Some(neighbours.luma_skip_ctx(tu_mi, 1)), fctx, |
| 14760 | )?; |
| 14761 | neighbours.record_mi_luma_rect(tu_mi, 4, 4, &g); |
| 14762 | } |
| 14763 | } else { |
| 14764 | for (idx, &(row, col, tw, th)) in ls.iter().enumerate() { |
| 14765 | let tu_mi = (lmi.0 + row, lmi.1 + col); |
| 14766 | let (tu_px, tu_py) = (px + col * MI, py + row * MI); |
| 14767 | let tu_pred = Pred::Inline(&pred_y, side).offset(row * MI * side + col * MI); |
| 14768 | let (g, tt) = if tw == th { |
| 14769 | read_inter_plane( |
| 14770 | dec, cdfs, inter_txbset_for(tw, reduced_tx_set), |
| 14771 | &default_scan(tw.min(32)), 0, neighbours.around_mi(tu_mi, tw)[0], 0, |
| 14772 | y, tu_px, tu_py, tw, base_q_idx, tu_pred, None, |
| 14773 | Some(neighbours.luma_skip_ctx(tu_mi, tw / MI)), fctx, |
| 14774 | )? |
| 14775 | } else { |
| 14776 | read_inter_plane_rect( |
| 14777 | dec, cdfs, rect_inter_luma_set(tw, th, fctx)?, (tw, th), side, 0, |
| 14778 | neighbours.around_mi_rect(tu_mi, tw, th)[0], 0, y, tu_px, tu_py, tu_pred, |
| 14779 | None, Some(neighbours.luma_skip_ctx_rect(tu_mi, tw / MI, th / MI)), fctx, |
| 14780 | )? |
| 14781 | }; |
| 14782 | if idx == 0 { |
| 14783 | luma_tx_type = tt; |
| 14784 | } |
| 14785 | neighbours.record_mi_luma_rect(tu_mi, tw, th, &g); |
| 14786 | } |
| 14787 | } |
| 14788 | luma_grid = Grid::Zero(bw * bh); |
| 14789 | chroma_stamped_per_unit = has_chroma && lossless(fctx); |
| 14790 | if has_chroma && lossless(fctx) { |
| 14791 | let (mut uo, mut vo) = (Vec::new(), Vec::new()); |
| 14792 | read_inter_chroma_lossless( |
| 14793 | dec, cdfs, neighbours, u, v, |
| 14794 | Pred::Inline(&pred_u, cside), Pred::Inline(&pred_v, cside), |
| 14795 | pair_mi, (cpx, cpy), (0, 0), (cw, ch), (cw, ch), cside, 0, base_q_idx, |
| 14796 | &mut uo, &mut vo, fctx, |
| 14797 | )?; |
| 14798 | u_grid = if uo.is_empty() { Grid::Zero(cw * ch) } else { Grid::Own(uo) }; |
| 14799 | v_grid = if vo.is_empty() { Grid::Zero(cw * ch) } else { Grid::Own(vo) }; |
| 14800 | } else if has_chroma { |
| 14801 | let around = neighbours.around_mi_rect(pair_mi, pw, ph); |
| 14802 | let (ug, _) = read_inter_plane_rect( |
| 14803 | dec, cdfs, rect_inter_chroma_set(cw, ch)?, (cw, ch), cside, 1, around[1], 0, |
| 14804 | u, cpx, cpy, Pred::Inline(&pred_u, cside), Some(luma_tx_type), None, fctx, |
| 14805 | )?; |
| 14806 | u_grid = ug; |
| 14807 | let (vg, _) = read_inter_plane_rect( |
| 14808 | dec, cdfs, rect_inter_chroma_set(cw, ch)?, (cw, ch), cside, 2, around[2], 0, |
| 14809 | v, cpx, cpy, Pred::Inline(&pred_v, cside), Some(luma_tx_type), None, fctx, |
| 14810 | )?; |
| 14811 | v_grid = vg; |
| 14812 | } |
| 14813 | } else { |
| 14814 | let around = neighbours.around_mi_rect(lmi, bw, bh); |
| 14815 | let (g, tt) = read_inter_plane_rect( |
| 14816 | dec, cdfs, rect_inter_luma_set(bw, bh, fctx)?, (bw, bh), side, 0, around[0], 0, |
| 14817 | y, px, py, Pred::Inline(&pred_y, side), None, None, fctx, |
| 14818 | )?; |
| 14819 | luma_tx_type = tt; |
| 14820 | luma_grid = g; |
| 14821 | chroma_stamped_per_unit = false; |
| 14822 | if has_chroma { |
| 14823 | let around = neighbours.around_mi_rect(pair_mi, pw, ph); |
| 14824 | let (ug, _) = read_inter_plane_rect( |
| 14825 | dec, cdfs, rect_inter_chroma_set(cw, ch)?, (cw, ch), cside, 1, around[1], 0, |
| 14826 | u, cpx, cpy, Pred::Inline(&pred_u, cside), Some(luma_tx_type), None, fctx, |
| 14827 | )?; |
| 14828 | u_grid = ug; |
| 14829 | let (vg, _) = read_inter_plane_rect( |
| 14830 | dec, cdfs, rect_inter_chroma_set(cw, ch)?, (cw, ch), cside, 2, around[2], 0, |
| 14831 | v, cpx, cpy, Pred::Inline(&pred_v, cside), Some(luma_tx_type), None, fctx, |
| 14832 | )?; |
| 14833 | v_grid = vg; |
| 14834 | } |
| 14835 | } |
| 14836 | drop(restored); |
| 14837 | |
| 14838 | if leaves.is_none() { |
| 14839 | neighbours.record_mi_luma_rect(lmi, bw, bh, &luma_grid); |
| 14840 | } |
| 14841 | neighbours.record_mode_mi(lmi.0, lmi.1, mi_w, mi_h, DC_PRED); |
| 14842 | neighbours.fill_skip_grid_rect(lmi, mi_w, mi_h, skip); |
| 14843 | neighbours.fill_lf_grid_rect(lmi, mi_w, mi_h, tx_w as u8, tx_h as u8, 0, fctx); |
| 14844 | for cell in 0..mi_w { |
| 14845 | if let Some(slot) = neighbours.above_side_mi.get_mut(lmi.1 + cell) { |
| 14846 | *slot = bw as u8; |
| 14847 | } |
| 14848 | } |
| 14849 | for cell in 0..mi_h { |
| 14850 | if let Some(slot) = neighbours.left_side_mi.get_mut(lmi.0 + cell) { |
| 14851 | *slot = bh as u8; |
| 14852 | } |
| 14853 | } |
| 14854 | let mut last_uv = None; |
| 14855 | if has_chroma { |
| 14856 | hit!(RECT4_16_CHROMA_HITS); |
| 14857 | neighbours.record_uv_mode_mi(pair_mi.0, pair_mi.1, pw / MI, ph / MI, DC_PRED); |
| 14858 | if !chroma_stamped_per_unit { |
| 14859 | let u_state = neighbour_state(&u_grid); |
| 14860 | let v_state = neighbour_state(&v_grid); |
| 14861 | let round_up_even = |n: usize| n.div_ceil(2) * 2; |
| 14862 | let bound_h = round_up_even(neighbours.mi_rows); |
| 14863 | let bound_w = round_up_even(neighbours.mi_cols); |
| 14864 | for cell in 0..(ph / MI) { |
| 14865 | if pair_mi.0 + cell < bound_h |
| 14866 | && let Some(slot) = neighbours.left.get_mut(pair_mi.0 + cell) |
| 14867 | { |
| 14868 | slot[1] = u_state; |
| 14869 | slot[2] = v_state; |
| 14870 | } |
| 14871 | } |
| 14872 | for cell in 0..(pw / MI) { |
| 14873 | if pair_mi.1 + cell < bound_w |
| 14874 | && let Some(slot) = neighbours.above.get_mut(pair_mi.1 + cell) |
| 14875 | { |
| 14876 | slot[1] = u_state; |
| 14877 | slot[2] = v_state; |
| 14878 | } |
| 14879 | } |
| 14880 | } |
| 14881 | last_uv = Some(DC_PRED); |
| 14882 | } |
| 14883 | if horz { |
| 14884 | hit!(RECT4_16_HORZ_HITS); |
| 14885 | } else { |
| 14886 | hit!(RECT4_16_VERT_HITS); |
| 14887 | } |
| 14888 | record_strip_palette(neighbours, lmi, bw, bh, 0, [0u16; 8], 0, [0u16; 8]); |
| 14889 | if leaves.is_none() { |
| 14890 | // Skip and TX_MODE_LARGEST publish the block size (`set_txfm_ctxs` |
| 14891 | // with `skip && is_inter`, or the unsplit unit). A split tree already |
| 14892 | // published per leaf inside `read_var_tx_size`. |
| 14893 | txfm_partition_update_rect(neighbours, lmi, (tx_w, tx_h), (bw, bh)); |
| 14894 | } |
| 14895 | record_intrabc_mi_rect( |
| 14896 | lmi.0, lmi.1, mi_w, mi_h, Some((dv_row, dv_col)), neighbours, fctx, |
| 14897 | ); |
| 14898 | Ok((DC_PRED, last_uv, (tx_w, tx_h))) |
| 14899 | } |
| 14900 | |
| 14901 | /// Restores `reduced_tx_set_inter` when the intrabc pair reader returns, |
| 14902 | /// including through `?`. |
| 14903 | struct ReducedRestore<'a> { |
| 14904 | fctx: &'a crate::decode::FrameCtx, |
| 14905 | saved: bool, |
| 14906 | } |
| 14907 | impl Drop for ReducedRestore<'_> { |
| 14908 | fn drop(&mut self) { |
| 14909 | self.fctx.reduced_tx_set_inter.with(|c| c.set(self.saved)); |
| 14910 | } |
| 14911 | } |
| 14912 | fn scopeguard_reduced(fctx: &crate::decode::FrameCtx, saved: bool) -> ReducedRestore<'_> { |
| 14913 | ReducedRestore { fctx, saved } |
| 14914 | } |
| 14915 | |
| 14916 | #[allow(clippy::too_many_arguments)] |
| 14917 | fn decode_rect4_16_strip( |
| 14918 | dec: &mut SymbolDecoder, |
| 14919 | cdfs: &mut Cdfs, |
| 14920 | neighbours: &mut Neighbours, |
| 14921 | lmi: (usize, usize), |
| 14922 | horz: bool, |
| 14923 | has_chroma: bool, |
| 14924 | prev: Option<((usize, usize), usize)>, |
| 14925 | y: &mut PlaneBuf<'static>, |
| 14926 | u: &mut PlaneBuf<'static>, |
| 14927 | v: &mut PlaneBuf<'static>, |
| 14928 | enable_filter_intra: bool, |
| 14929 | allow_screen_content_tools: bool, |
| 14930 | base_q_idx: u8, |
| 14931 | tx_select: bool, |
| 14932 | reduced_tx_set: bool, fctx: &crate::decode::FrameCtx, |
| 14933 | ) -> Result<(usize, Option<usize>, (usize, usize))> { |
| 14934 | let (r, c) = (lmi.0 / (SUB / MI), lmi.1 / (SUB / MI)); |
| 14935 | let (bw, bh) = if horz { (16usize, 4usize) } else { (4, 16) }; |
| 14936 | let (mi_w, mi_h) = (bw / MI, bh / MI); |
| 14937 | let scan: &[u16] = if horz { &SCAN_16X4 } else { &SCAN_4X16 }; |
| 14938 | let mut last_uv_mode: Option<usize> = None; |
| 14939 | // `decode_block_rect4`'s own neighbour rule: the coarse 16-px cell |
| 14940 | // aliases all four strips onto one entry, so the mi-exact map wins and |
| 14941 | // the previous strip stands in on the split axis. |
| 14942 | let mut above_mode = neighbours.above_mode[c]; |
| 14943 | let mut left_mode = neighbours.left_mode[r]; |
| 14944 | if let Some(((pr, pc), pmode)) = prev { |
| 14945 | if pc == lmi.1 && lmi.0 == pr + mi_h { |
| 14946 | above_mode = pmode; |
| 14947 | } else if pr == lmi.0 && lmi.1 == pc + mi_w { |
| 14948 | left_mode = pmode; |
| 14949 | } |
| 14950 | } |
| 14951 | let (above_mode, left_mode) = |
| 14952 | neighbours.modes_above_left_mi(lmi.0, lmi.1, (above_mode, left_mode)); |
| 14953 | // lane-kf900 r4: the strip's real palette ctx + colour cache (see |
| 14954 | // [`decode_block_rect4`]); `None`/`&[]` here read `palette_y_mode` |
| 14955 | // off CDF row 0 and every cache hit off an empty cache. |
| 14956 | let (palette_ctx, palette_cache) = neighbours.palette_ctx_and_cache_mi(lmi); |
| 14957 | let palette_uv_cache = neighbours.palette_uv_cache_mi(lmi); |
| 14958 | let ( |
| 14959 | skip, |
| 14960 | mode, |
| 14961 | angle_delta_y, |
| 14962 | uv_mode, |
| 14963 | angle_delta_uv, |
| 14964 | alpha, |
| 14965 | filter_intra, |
| 14966 | palette_y, |
| 14967 | palette_uv, |
| 14968 | intrabc, |
| 14969 | ) = read_intra_mode_rect( |
| 14970 | dec, |
| 14971 | cdfs, |
| 14972 | above_mode, |
| 14973 | left_mode, |
| 14974 | true, |
| 14975 | has_chroma, |
| 14976 | bw, |
| 14977 | bh, |
| 14978 | enable_filter_intra, |
| 14979 | neighbours.skip_txfm_ctx(lmi.0, lmi.1), |
| 14980 | allow_screen_content_tools, |
| 14981 | Some((palette_ctx, &palette_cache)), |
| 14982 | &palette_uv_cache, |
| 14983 | lmi.0, |
| 14984 | lmi.1, fctx, |
| 14985 | )?; |
| 14986 | if let Some(dv) = intrabc { |
| 14987 | return decode_rect4_16_intrabc( |
| 14988 | dec, cdfs, neighbours, lmi, horz, has_chroma, skip, dv, y, u, v, |
| 14989 | base_q_idx, tx_select, reduced_tx_set, fctx, |
| 14990 | ); |
| 14991 | } |
| 14992 | if palette_y.is_some() || palette_uv.is_some() { |
| 14993 | hit!(RECT4_PALETTE_HITS); |
| 14994 | } |
| 14995 | // The colour-index map through the base colours IS the prediction. |
| 14996 | // Chroma's map is the PAIR's 8x4 (4x8) block, matching the one |
| 14997 | // chroma transform this closing strip codes. |
| 14998 | let palette_y_buf: Option<Vec<u16>> = palette_y |
| 14999 | .as_ref() |
| 15000 | .map(|p| p.map.iter().map(|&i| p.colors[i as usize]).collect()); |
| 15001 | let palette_uv_bufs: Option<(Vec<u16>, Vec<u16>)> = palette_uv.as_ref().map(|p| { |
| 15002 | ( |
| 15003 | p.map.iter().map(|&i| p.u_colors[i as usize]).collect(), |
| 15004 | p.map.iter().map(|&i| p.v_colors[i as usize]).collect(), |
| 15005 | ) |
| 15006 | }); |
| 15007 | let smooth_neighbor = is_smooth_mode(above_mode) || is_smooth_mode(left_mode); |
| 15008 | if smooth_neighbor { |
| 15009 | hit!(SMOOTH_LUMA_HITS); |
| 15010 | } |
| 15011 | if filter_intra.is_some() { |
| 15012 | hit!(FILTER_INTRA_RECT_HITS); |
| 15013 | } |
| 15014 | // `bsize_to_tx_size_cat(BLOCK_16X4)`: `max_txsize_rect_lookup` is |
| 15015 | // TX_16X4 and `sub_tx_size_map` reaches TX_4X4 in two steps, so the |
| 15016 | // depth symbol is the 3-symbol `tx_size_cat1` -- not the 2-symbol |
| 15017 | // `cat0` a 4x8 leaf reads nor the `cat2` a 32x8 strip does. |
| 15018 | // lane-av1mixloss: the lossless-segment early return |
| 15019 | // (`decodeframe.c:1203`) precedes `TX_MODE_SELECT`, so a |
| 15020 | // lossless-segment strip codes no depth symbol here either (class |
| 15021 | // sweep of the sub8 leaf's fix). |
| 15022 | let depth = if tx_select && !lossless(fctx) { |
| 15023 | // lane-av1txbands: every frame reads the real `TXFM_CONTEXT` bands |
| 15024 | // (`get_tx_size_context`, libaom `pred_common.h:342`). |
| 15025 | let ctx = tx_size_context_txfm_rect(neighbours, lmi, bw, bh, fctx); |
| 15026 | let d = dec.symbol(&mut cdfs.tx_size_cat1[ctx]); |
| 15027 | if crate::envflags::env_flag!("EC_TRACE_MODE_STEP") { |
| 15028 | eprintln!( |
| 15029 | "EC_ISTEP mi_row={} mi_col={} name=tx_depth val={d} ctx={ctx} cat=1 rng={}", |
| 15030 | lmi.0, |
| 15031 | lmi.1, |
| 15032 | dec.debug_state().0 |
| 15033 | ); |
| 15034 | } |
| 15035 | d |
| 15036 | } else { |
| 15037 | 0 |
| 15038 | }; |
| 15039 | if depth != 0 { |
| 15040 | hit!(TX_DEPTH_HITS); |
| 15041 | hit!(RECT4_16_SPLIT_HITS); |
| 15042 | } |
| 15043 | let (tx_w, tx_h) = depth_to_tx_wh(bw, bh, depth, fctx); |
| 15044 | let (px, py) = (lmi.1 * MI, lmi.0 * MI); |
| 15045 | let reach = Reach::of_rect(bw, bh, px, py, y.width, y.height, fctx); |
| 15046 | neighbours.record_mode_mi(lmi.0, lmi.1, mi_w, mi_h, mode); |
| 15047 | if depth != 0 || lossless(fctx) { |
| 15048 | // [`decode_rect_split`]'s per-unit walk, inlined because the |
| 15049 | // CHROMA of this shape belongs to the strip's 4:2:0 pair, not to |
| 15050 | // the strip (that function codes one chroma transform per block). |
| 15051 | // depth 1 is the 2:1 rect unit TX_8X4/TX_4X8 |
| 15052 | // (`sub_tx_size_map[TX_16X4]`), depth 2 the square TX_4X4; each |
| 15053 | // unit predicts off the units before it in raster order. |
| 15054 | let rect_unit = tx_w != tx_h; |
| 15055 | let zero_tu = vec![0i32; tx_w * tx_h]; |
| 15056 | for tu_row in 0..bh / tx_h { |
| 15057 | for tu_col in 0..bw / tx_w { |
| 15058 | let tu_mi = (lmi.0 + tu_row * (tx_h / MI), lmi.1 + tu_col * (tx_w / MI)); |
| 15059 | let (tu_px, tu_py) = (px + tu_col * tx_w, py + tu_row * tx_h); |
| 15060 | let tu_reach = Reach::of_tu( |
| 15061 | bw, |
| 15062 | bh, |
| 15063 | tu_col * tx_w, |
| 15064 | tu_row * tx_h, |
| 15065 | tx_w, |
| 15066 | tx_h, |
| 15067 | reach, |
| 15068 | ); |
| 15069 | if let Some(b) = &palette_y_buf { |
| 15070 | let tu_buf: Vec<u16> = (0..tx_h) |
| 15071 | .flat_map(|i| { |
| 15072 | let base = (tu_row * tx_h + i) * bw + tu_col * tx_w; |
| 15073 | b[base..base + tx_w].iter().copied() |
| 15074 | }) |
| 15075 | .collect(); |
| 15076 | set_palette_pred(tu_buf, fctx); |
| 15077 | } |
| 15078 | if skip { |
| 15079 | push_intra_rect(0, |
| 15080 | tu_px, tu_py, tx_w, tx_h, mode, angle_delta_y, tu_reach, &zero_tu, |
| 15081 | None, filter_intra, smooth_neighbor, fctx, |
| 15082 | ); |
| 15083 | neighbours.record_mi_luma_rect(tu_mi, tx_w, tx_h, &zero_tu); |
| 15084 | } else if rect_unit { |
| 15085 | // `get_txsize_entropy_ctx(TX_8X4)` is the 8x8 class with |
| 15086 | // 32 coded positions and the `TX_4X4` `tx_type` row: |
| 15087 | // exactly [`TxbSet::LumaRect4x8`]. |
| 15088 | let tu_around = neighbours.around_mi_rect(tu_mi, tx_w, tx_h)[0]; |
| 15089 | let tu_skip_ctx = |
| 15090 | neighbours.luma_skip_ctx_rect(tu_mi, tx_w / MI, tx_h / MI); |
| 15091 | let set = if reduced_tx_set { |
| 15092 | TxbSet::LumaRect4x8 |
| 15093 | } else { |
| 15094 | TxbSet::LumaRect4x8Set1 |
| 15095 | }; |
| 15096 | let tu_scan: &[u16] = if tx_w > tx_h { &SCAN_8X4 } else { &SCAN_4X8 }; |
| 15097 | let mut coding = cdfs.txb(set, fi_tx_row(mode, filter_intra)); |
| 15098 | let (tu_grid, tu_tx_type) = read_coeffs_rect( |
| 15099 | dec, |
| 15100 | &mut coding, |
| 15101 | tu_scan, |
| 15102 | tx_w, |
| 15103 | tx_h, |
| 15104 | tu_skip_ctx, |
| 15105 | dc_sign_ctx(tu_around.2), |
| 15106 | TxType::DctDct, |
| 15107 | )?; |
| 15108 | let residual = dequant_and_inverse_typed_wh( |
| 15109 | &tu_grid, |
| 15110 | tx_w, |
| 15111 | tx_h, |
| 15112 | crate::decode::bit_depth(fctx), |
| 15113 | block_q_idx(fctx), |
| 15114 | plane_q_delta(0, fctx).0, |
| 15115 | plane_q_delta(0, fctx).1, |
| 15116 | tu_tx_type, |
| 15117 | block_iqmatrix(fctx, 0, tx_w, tx_h, tu_tx_type), |
| 15118 | ); |
| 15119 | push_intra_rect(0, |
| 15120 | tu_px, tu_py, tx_w, tx_h, mode, angle_delta_y, tu_reach, &residual, |
| 15121 | None, filter_intra, smooth_neighbor, fctx, |
| 15122 | ); |
| 15123 | if tu_grid.iter().any(|&l| l != 0) { |
| 15124 | hit!(RECT4_16_COEFF_HITS); |
| 15125 | } |
| 15126 | neighbours.record_mi_luma_rect(tu_mi, tx_w, tx_h, &tu_grid); |
| 15127 | } else { |
| 15128 | let tu_around = neighbours.around_mi(tu_mi, tx_w)[0]; |
| 15129 | let tu_skip_ctx = neighbours.luma_skip_ctx(tu_mi, tx_w / MI); |
| 15130 | let tu_grid = read_plane( |
| 15131 | dec, |
| 15132 | cdfs, |
| 15133 | txbset_for(tx_w, reduced_tx_set), |
| 15134 | default_scan(tx_w), |
| 15135 | 0, |
| 15136 | tu_around, |
| 15137 | mode, |
| 15138 | mode, |
| 15139 | angle_delta_y, |
| 15140 | tu_reach, |
| 15141 | y, |
| 15142 | tu_px, |
| 15143 | tu_py, |
| 15144 | tx_w, |
| 15145 | tx_w, |
| 15146 | base_q_idx, |
| 15147 | None, |
| 15148 | filter_intra, |
| 15149 | Some(tu_skip_ctx), |
| 15150 | smooth_neighbor, fctx, |
| 15151 | )?; |
| 15152 | if tu_grid.iter().any(|&l| l != 0) { |
| 15153 | hit!(RECT4_16_COEFF_HITS); |
| 15154 | } |
| 15155 | neighbours.record_mi_luma(tu_mi, tx_w, &tu_grid); |
| 15156 | } |
| 15157 | } |
| 15158 | } |
| 15159 | } else { |
| 15160 | let levels = if skip { |
| 15161 | let zeros = Grid::Zero(bw * bh); |
| 15162 | if let Some(b) = &palette_y_buf { |
| 15163 | set_palette_pred(b.clone(), fctx); |
| 15164 | } |
| 15165 | push_intra_rect(0, |
| 15166 | px, py, bw, bh, mode, angle_delta_y, reach, &zeros, None, filter_intra, |
| 15167 | smooth_neighbor, fctx, |
| 15168 | ); |
| 15169 | zeros |
| 15170 | } else { |
| 15171 | // The strip's luma TU *is* the strip (`plane_bsize == |
| 15172 | // txsize_to_bsize[tx_size]`), so `get_txb_ctx` fixes |
| 15173 | // `txb_skip_ctx` at 0. |
| 15174 | let around = neighbours.around_mi_rect(lmi, bw, bh)[0]; |
| 15175 | let set = if reduced_tx_set { |
| 15176 | TxbSet::LumaRect16x4 |
| 15177 | } else { |
| 15178 | TxbSet::LumaRect16x4Set1 |
| 15179 | }; |
| 15180 | let mut coding = cdfs.txb(set, fi_tx_row(mode, filter_intra)); |
| 15181 | let (levels, tx_type) = read_coeffs_rect( |
| 15182 | dec, |
| 15183 | &mut coding, |
| 15184 | scan, |
| 15185 | bw, |
| 15186 | bh, |
| 15187 | 0, |
| 15188 | dc_sign_ctx(around.2), |
| 15189 | TxType::DctDct, |
| 15190 | )?; |
| 15191 | let residual = dequant_and_inverse_typed_wh( |
| 15192 | &levels, |
| 15193 | bw, |
| 15194 | bh, |
| 15195 | crate::decode::bit_depth(fctx), |
| 15196 | block_q_idx(fctx), |
| 15197 | plane_q_delta(0, fctx).0, |
| 15198 | plane_q_delta(0, fctx).1, |
| 15199 | tx_type, |
| 15200 | block_iqmatrix(fctx, 0, bw, bh, tx_type), |
| 15201 | ); |
| 15202 | if let Some(b) = &palette_y_buf { |
| 15203 | set_palette_pred(b.clone(), fctx); |
| 15204 | } |
| 15205 | push_intra_rect(0, |
| 15206 | px, py, bw, bh, mode, angle_delta_y, reach, &residual, None, filter_intra, |
| 15207 | smooth_neighbor, fctx, |
| 15208 | ); |
| 15209 | hit!(RECT4_16_COEFF_HITS); |
| 15210 | levels |
| 15211 | }; |
| 15212 | neighbours.record_mi_luma_rect(lmi, bw, bh, &levels); |
| 15213 | } |
| 15214 | neighbours.fill_skip_grid_rect(lmi, mi_w, mi_h, skip); |
| 15215 | neighbours.fill_lf_grid_rect(lmi, mi_w, mi_h, tx_w as u8, tx_h as u8, 0, fctx); |
| 15216 | // `update_partition_context` writes `partition_context_lookup[ |
| 15217 | // BLOCK_16X4]` over the whole 16x16: full width above, a 4-px side on |
| 15218 | // the left (mirrored under VERT_4). |
| 15219 | for cell in 0..mi_w { |
| 15220 | if let Some(slot) = neighbours.above_side_mi.get_mut(lmi.1 + cell) { |
| 15221 | *slot = bw as u8; |
| 15222 | } |
| 15223 | } |
| 15224 | for cell in 0..mi_h { |
| 15225 | if let Some(slot) = neighbours.left_side_mi.get_mut(lmi.0 + cell) { |
| 15226 | *slot = bh as u8; |
| 15227 | } |
| 15228 | } |
| 15229 | if has_chroma { |
| 15230 | hit!(RECT4_16_CHROMA_HITS); |
| 15231 | // lane-av1-444: at 4:4:4 there is no pair -- every strip is its |
| 15232 | // own chroma reference and codes its own 16x4 / 4x16 chroma |
| 15233 | // block. (`set_mi_offsets`'s chroma snap is the zero offset at |
| 15234 | // ss 0/0, so the neighbour reads below anchor at the strip.) |
| 15235 | let (pair_mi, pw, ph) = if ss_x(fctx) == 0 && ss_y(fctx) == 0 { |
| 15236 | (lmi, bw, bh) |
| 15237 | } else if ss_y(fctx) == 0 && horz { |
| 15238 | // lane-av1-422: chroma height is NOT subsampled, so a HORZ |
| 15239 | // 16x4 strip is its own chroma reference (`is_chroma_reference` |
| 15240 | // at ss(1,0): bh == 1 mi, so the row clause reduces to |
| 15241 | // `!ss_y`) and codes its own `ss_size_lookup[BLOCK_16X4][1][0]` |
| 15242 | // = BLOCK_8X4 chroma block -- a valid transform, no pair. |
| 15243 | // (lane-av1-422o: a VERTICAL 4x16 strip now falls through to |
| 15244 | // the pair merge below -- its own chroma block |
| 15245 | // `ss_size_lookup[BLOCK_4X16][1][0]` is BLOCK_INVALID, the |
| 15246 | // walk proved such leaves exist, and libaom codes the pair's |
| 15247 | // chroma from the closing strip; the chunk lives in the |
| 15248 | // `chroma422_pair16` arm below.) |
| 15249 | (lmi, bw, bh) |
| 15250 | } else if horz { |
| 15251 | ((lmi.0 - 1, lmi.1), 16, 8) |
| 15252 | } else { |
| 15253 | ((lmi.0, lmi.1 - 1), 8, 16) |
| 15254 | }; |
| 15255 | let (ppx, ppy) = (pair_mi.1 * MI, pair_mi.0 * MI); |
| 15256 | let (cpx, cpy) = (ppx >> ss_x(fctx), ppy >> ss_y(fctx)); |
| 15257 | let (cw, ch) = (pw >> ss_x(fctx), ph >> ss_y(fctx)); |
| 15258 | // lane-av1-422o: the 4:2:2 pair's chroma of a VERT 4x16 strip |
| 15259 | // pair: (4, 16) at ss(1,0). The bsize is still BLOCK_4X16, whose |
| 15260 | // `ss_size_lookup[..][1][0]` is BLOCK_INVALID, so libaom's |
| 15261 | // `av1_get_tx_size` reads `max_txsize_rect_lookup[255]` -- the |
| 15262 | // out-of-bounds byte is TX_4X8 in the pinned Release oracle, and |
| 15263 | // `av1_get_adjusted_tx_size` has no TX_4X8 case to adjust it. |
| 15264 | // `decode_token_recon_block` walks unit_width = |
| 15265 | // ROUND_POWER_OF_TWO(1, 1) = 1, unit_height = 4, with stepr = |
| 15266 | // tx_size_high_unit[TX_4X8] = 2: TWO TX_4X8 units per plane |
| 15267 | // (chroma rows 0..8 and 8..16), plane-major. Even the skip arm |
| 15268 | // walks per unit: `predict_and_reconstruct_intra_block` predicts |
| 15269 | // every unit regardless of `skip_txfm`. |
| 15270 | let chroma422_pair16 = (cw, ch) == (4, 16) && ss_x(fctx) == 1; |
| 15271 | let uv_predict_mode = if uv_mode == UV_CFL_PRED { DC_PRED } else { uv_mode }; |
| 15272 | if (V_PRED..=D67_PRED).contains(&uv_predict_mode) { |
| 15273 | hit!(RECT4_16_CHROMA_DIR_HITS); |
| 15274 | } |
| 15275 | let pair_reach = Reach::of_rect(pw, ph, ppx, ppy, y.width, y.height, fctx); |
| 15276 | // lane-kf1200 r1: the chroma edge-filter type reads the neighbours |
| 15277 | // of the CHROMA block, which for a 16x4/4x16 pair is `pair_mi`'s |
| 15278 | // 16x8/8x16 region -- libaom builds `chroma_above_mbmi`/ |
| 15279 | // `chroma_left_mbmi` from `mi[-((mi_row & ss_y) * stride + (mi_col |
| 15280 | // & ss_x))]` (`set_mi_offsets`, av1/common/blockd.h ~1050), i.e. |
| 15281 | // the pair's top-left luma mi, never this leaf's odd row/column. |
| 15282 | // Reading `lmi` here named the leaf directly above (a non-chroma |
| 15283 | // -reference 4-row block, `uv_mode == UV_DC_PRED`) and so lost a |
| 15284 | // SMOOTH neighbour: filt_type 0 instead of 1 turned |
| 15285 | // `av1_use_intra_edge_upsample` on (blk_wh 12 <= 16) for a |
| 15286 | // directional chroma strip, moving 4 samples by 1. |
| 15287 | let smooth_neighbor_uv = |
| 15288 | neighbours.smooth_uv_neighbour(pair_mi.0, pair_mi.1, r, c, fctx); |
| 15289 | if smooth_neighbor_uv != neighbours.smooth_uv_neighbour_unsnapped(lmi.0, lmi.1, r, c) |
| 15290 | { |
| 15291 | hit!(RECT4_16_UV_PAIR_FILT_HITS); |
| 15292 | } |
| 15293 | // lane-av1-rect14: a lossless frame forces TX_4X4 on every plane |
| 15294 | // (`av1_get_tx_size`, blockd.h:1381). The pair's chroma block is |
| 15295 | // 8x4 (HORZ_4) or 4x8 (VERT_4) -- two TX_4X4s, plane-major |
| 15296 | // (`decode_token_recon_block`), not one `ChromaRect8x4`. Reading |
| 15297 | // the rect here consumed one symbol block too few and desynced |
| 15298 | // the next strip's `skip` (measured: lossless sb128 320x240, |
| 15299 | // mi(1,20) uv_mode matched, the following skip did not). |
| 15300 | let lossless_pair = lossless(fctx); |
| 15301 | let (u_levels, v_levels) = if lossless_pair { |
| 15302 | hit!(RECT4_16_LOSSLESS_CHROMA_HITS); |
| 15303 | if skip { |
| 15304 | for plane in 1..3usize { |
| 15305 | for rr in 0..ch / 4 { |
| 15306 | for cc in 0..cw / 4 { |
| 15307 | let (ox, oy) = (cc * 4, rr * 4); |
| 15308 | let cu_reach = tu_reach( |
| 15309 | pw, ph, ox * 2, oy * 2, 8, pair_reach, ppx, ppy, |
| 15310 | y.width, y.height, fctx, |
| 15311 | ); |
| 15312 | if let Some((ub, vb)) = &palette_uv_bufs { |
| 15313 | let buf = if plane == 1 { ub } else { vb }; |
| 15314 | set_palette_pred(palette_window(buf, cw, ox, oy, 4, 4), fctx); |
| 15315 | } |
| 15316 | push_intra( |
| 15317 | plane, cpx + ox, cpy + oy, 4, uv_predict_mode, |
| 15318 | angle_delta_uv, cu_reach, &ZERO_RESIDUAL[..16], None, None, |
| 15319 | smooth_neighbor_uv, fctx, |
| 15320 | ); |
| 15321 | let cu_mi = ( |
| 15322 | pair_mi.0 + (oy << ss_y(fctx)) / MI, |
| 15323 | pair_mi.1 + (ox << ss_x(fctx)) / MI, |
| 15324 | ); |
| 15325 | neighbours.record_mi_chroma( |
| 15326 | cu_mi, |
| 15327 | 4 << ss_x(fctx), |
| 15328 | 4 << ss_y(fctx), |
| 15329 | plane, |
| 15330 | &ZERO_RESIDUAL[..16], |
| 15331 | ); |
| 15332 | } |
| 15333 | } |
| 15334 | } |
| 15335 | (Grid::Zero(cw * ch), Grid::Zero(cw * ch)) |
| 15336 | } else { |
| 15337 | let scan4 = default_scan(4); |
| 15338 | // `get_txb_ctx`: offset 10 (our tables store it at +3) |
| 15339 | // when the plane block is larger than the transform. |
| 15340 | let offset = Some(3usize); |
| 15341 | let mut uo = vec![0i32; cw * ch]; |
| 15342 | let mut vo = vec![0i32; cw * ch]; |
| 15343 | for plane in 1..3usize { |
| 15344 | for rr in 0..ch / 4 { |
| 15345 | for cc in 0..cw / 4 { |
| 15346 | let (ox, oy) = (cc * 4, rr * 4); |
| 15347 | if cpx + ox >= u.true_width || cpy + oy >= u.true_height { |
| 15348 | continue; |
| 15349 | } |
| 15350 | // The unit's stamp is its own span: aom |
| 15351 | // `av1_set_entropy_contexts` (blockd.c:29) |
| 15352 | // memsets exactly `tx_size_wide_unit` x |
| 15353 | // `tx_size_high_unit` cells at the unit's own |
| 15354 | // `(aoff, loff)` -- for TX_4X4 ONE above cell |
| 15355 | // and ONE left cell. `(oy / 2, ox / 2)` + |
| 15356 | // `(8, 8)` is the 4:2:0 subsampled form (one |
| 15357 | // chroma cell = 8 luma px = 2 mi cells); at |
| 15358 | // 4:4:4 a unit is ONE mi cell, and the 2x2 |
| 15359 | // smear let a 4-wide strip's later units |
| 15360 | // overwrite the NEXT COLUMN's above cell with |
| 15361 | // their own state -- a column aom never sees |
| 15362 | // this block write -- so the following block's |
| 15363 | // `all_zero` read the wrong `txb_skip` row |
| 15364 | // (frame-0 read #452, the llkf named gap). |
| 15365 | let cu_mi = ( |
| 15366 | pair_mi.0 + (oy << ss_y(fctx)) / MI, |
| 15367 | pair_mi.1 + (ox << ss_x(fctx)) / MI, |
| 15368 | ); |
| 15369 | // One luma-mi cell: the chroma entropy arrays |
| 15370 | // are subsampled, and our bands replicate that |
| 15371 | // one entry over the unit's two cells |
| 15372 | // (`read_intra_chroma_lossless`). |
| 15373 | let cu_around = neighbours.around_mi(cu_mi, MI); |
| 15374 | let cu_reach = tu_reach( |
| 15375 | pw, ph, ox * 2, oy * 2, 8, pair_reach, ppx, ppy, |
| 15376 | y.width, y.height, fctx, |
| 15377 | ); |
| 15378 | if let Some((ub, vb)) = &palette_uv_bufs { |
| 15379 | let buf = if plane == 1 { ub } else { vb }; |
| 15380 | set_palette_pred(palette_window(buf, cw, ox, oy, 4, 4), fctx); |
| 15381 | } |
| 15382 | let buf: &mut PlaneBuf<'static> = |
| 15383 | if plane == 1 { &mut *u } else { &mut *v }; |
| 15384 | let cu_grid = read_plane( |
| 15385 | dec, cdfs, TxbSet::Chroma4, &scan4, plane, |
| 15386 | cu_around[plane], mode, uv_predict_mode, angle_delta_uv, |
| 15387 | cu_reach, buf, cpx + ox, cpy + oy, 4, 4, base_q_idx, |
| 15388 | None, None, offset, smooth_neighbor_uv, fctx, |
| 15389 | )?; |
| 15390 | neighbours.record_mi_chroma( |
| 15391 | cu_mi, |
| 15392 | 4 << ss_x(fctx), |
| 15393 | 4 << ss_y(fctx), |
| 15394 | plane, |
| 15395 | &cu_grid, |
| 15396 | ); |
| 15397 | hit!(CHROMA_SPLIT_TX_HITS); |
| 15398 | let dst = if plane == 1 { &mut uo } else { &mut vo }; |
| 15399 | for row in 0..4 { |
| 15400 | let start = (oy + row) * cw + ox; |
| 15401 | dst[start..start + 4] |
| 15402 | .copy_from_slice(&cu_grid[row * 4..][..4]); |
| 15403 | } |
| 15404 | } |
| 15405 | } |
| 15406 | } |
| 15407 | (Grid::Own(uo), Grid::Own(vo)) |
| 15408 | } |
| 15409 | } else if skip { |
| 15410 | let zeros = Grid::Zero(cw * ch); |
| 15411 | // libaom `predict_and_reconstruct_intra_block` (decodeframe.c |
| 15412 | // ~1000) predicts EVERY plane and only guards the residual on |
| 15413 | // `mbmi->skip_txfm`, so a skipped `UV_CFL_PRED` block is still |
| 15414 | // DC + alpha * luma-AC. Dropping the CfL term here left a |
| 15415 | // chroma-only bounded box on ReelC's key frames. |
| 15416 | let ac = alpha.map(|_| cfl_src_rect(ppx, ppy, pw, ph)); |
| 15417 | if alpha.is_some() { |
| 15418 | hit!(CHROMA_SKIP_CFL_HITS); |
| 15419 | } |
| 15420 | for (_buf, plane) in [(&mut *u, 1usize), (&mut *v, 2)] { |
| 15421 | if chroma422_pair16 { |
| 15422 | // lane-av1-422o: two TX_4X8 units, each predicted on |
| 15423 | // its own edges (the lower one off the upper one's |
| 15424 | // samples); no coefficients are coded, and libaom's |
| 15425 | // `set_entropy_context` zeroed the block's own cells, |
| 15426 | // so the pair-wide zero stamp below stays correct. |
| 15427 | for unit_row in 0..2usize { |
| 15428 | let unit_reach = tu_reach_rect( |
| 15429 | pw, ph, 0, unit_row * 8, 8, 8, pair_reach, |
| 15430 | ppx, ppy, y.width, y.height, fctx, |
| 15431 | ); |
| 15432 | if let Some((ub, vb)) = &palette_uv_bufs { |
| 15433 | let pbuf = if plane == 1 { ub } else { vb }; |
| 15434 | set_palette_pred( |
| 15435 | palette_window(pbuf, cw, 0, unit_row * 8, 4, 8), |
| 15436 | fctx, |
| 15437 | ); |
| 15438 | } |
| 15439 | push_intra_rect(plane, |
| 15440 | cpx, |
| 15441 | cpy + unit_row * 8, |
| 15442 | 4, |
| 15443 | 8, |
| 15444 | uv_predict_mode, |
| 15445 | angle_delta_uv, |
| 15446 | unit_reach, |
| 15447 | &ZERO_RESIDUAL[..4 * 8], |
| 15448 | alpha |
| 15449 | .zip(ac) |
| 15450 | .map(|((au, av), ac)| (if plane == 1 { au } else { av }, ac)), |
| 15451 | None, |
| 15452 | smooth_neighbor_uv, fctx, |
| 15453 | ); |
| 15454 | } |
| 15455 | } else { |
| 15456 | if let Some((ub, vb)) = &palette_uv_bufs { |
| 15457 | set_palette_pred(if plane == 1 { ub.clone() } else { vb.clone() }, fctx); |
| 15458 | } |
| 15459 | push_intra_rect(plane, |
| 15460 | cpx, |
| 15461 | cpy, |
| 15462 | cw, |
| 15463 | ch, |
| 15464 | uv_predict_mode, |
| 15465 | angle_delta_uv, |
| 15466 | pair_reach, |
| 15467 | &zeros, |
| 15468 | alpha |
| 15469 | .zip(ac) |
| 15470 | .map(|((au, av), ac)| (if plane == 1 { au } else { av }, ac)), |
| 15471 | None, |
| 15472 | smooth_neighbor_uv, fctx, |
| 15473 | ); |
| 15474 | } |
| 15475 | } |
| 15476 | (zeros.clone(), zeros) |
| 15477 | } else if chroma422_pair16 { |
| 15478 | // lane-av1-422o: the pair's 4x16 chroma as TWO TX_4X8 units |
| 15479 | // per plane, plane-major (every U unit, then every V). Each |
| 15480 | // unit: the `ChromaRect8x4` tables (`get_txsize_entropy_ctx` |
| 15481 | // squares TX_4X8 up to the 8x8 class), `SCAN_4X8`, and a |
| 15482 | // `txb_skip_ctx` of above+left in the OFFSET-10 rows -- the |
| 15483 | // offset comes from `num_pels_log2_lookup[plane_bsize] > |
| 15484 | // num_pels_log2_lookup[BLOCK_4X8]` with plane_bsize = |
| 15485 | // BLOCK_INVALID (255), and the byte at that table's +255 in |
| 15486 | // the pinned Release oracle is 255 (`cmpb` against the |
| 15487 | // `0xc0db70` copy decodetxb.c links), i.e. the bigger-plane |
| 15488 | // arm: +3 on the offset-7-first chroma tables. No tx_type |
| 15489 | // symbol (only y plane's is transmitted); the unit's type is |
| 15490 | // `intra_mode_to_tx_type(UV mode)` kept by the all-16 ext-tx |
| 15491 | // set -- [`read_rect_chroma_unit`]'s own default. CfL params |
| 15492 | // are computed ONCE per block (`are_parameters_computed`) |
| 15493 | // over the pair's luma, shared by both units. Each unit is |
| 15494 | // predicted and recorded immediately, so the lower unit |
| 15495 | // reads the upper one's coefficient context. |
| 15496 | let ac = alpha.map(|_| cfl_src_rect(ppx, ppy, pw, ph)); |
| 15497 | for (plane, buf) in [(1usize, &mut *u), (2usize, &mut *v)] { |
| 15498 | for unit_row in 0..2usize { |
| 15499 | let unit_mi = (pair_mi.0 + unit_row * 2, pair_mi.1); |
| 15500 | let unit_reach = tu_reach_rect( |
| 15501 | pw, ph, 0, unit_row * 8, 8, 8, pair_reach, |
| 15502 | ppx, ppy, y.width, y.height, fctx, |
| 15503 | ); |
| 15504 | if let Some((ub, vb)) = &palette_uv_bufs { |
| 15505 | let pbuf = if plane == 1 { ub } else { vb }; |
| 15506 | set_palette_pred( |
| 15507 | palette_window(pbuf, cw, 0, unit_row * 8, 4, 8), |
| 15508 | fctx, |
| 15509 | ); |
| 15510 | } |
| 15511 | let chroma_around = neighbours.around_mi_422_chroma(unit_mi, 8, 8); |
| 15512 | let unit_grid = read_rect_chroma_unit( |
| 15513 | dec, |
| 15514 | cdfs, |
| 15515 | TxbSet::ChromaRect8x4, |
| 15516 | &SCAN_4X8, |
| 15517 | 4, |
| 15518 | 8, |
| 15519 | plane, |
| 15520 | chroma_around[plane], |
| 15521 | uv_predict_mode, |
| 15522 | angle_delta_uv, |
| 15523 | unit_reach, |
| 15524 | buf, |
| 15525 | cpx, |
| 15526 | cpy + unit_row * 8, |
| 15527 | base_q_idx, |
| 15528 | // lane-av1-422q: the offset-10 rows -- the same |
| 15529 | // measured rule as the 4x32 arm (plane_bsize |
| 15530 | // BLOCK_INVALID for the pair's BLOCK_4X16 too; |
| 15531 | // num_pels_log2_lookup[255] = 0xff at the |
| 15532 | // 0xc0de70 copy -> +3 on the offset-7-first |
| 15533 | // chroma tables; see lanes/av1422q.report.md). |
| 15534 | Some(3), |
| 15535 | alpha |
| 15536 | .zip(ac) |
| 15537 | .map(|((au, av), ac)| (if plane == 1 { au } else { av }, ac)), |
| 15538 | smooth_neighbor_uv, fctx, |
| 15539 | )?; |
| 15540 | neighbours.record_mi_chroma(unit_mi, 8, 8, plane, &unit_grid); |
| 15541 | } |
| 15542 | } |
| 15543 | (Grid::Zero(cw * ch), Grid::Zero(cw * ch)) |
| 15544 | } else { |
| 15545 | // lane-av1422ctxgather: 4:2:2's chroma above votes sample |
| 15546 | // every second luma-mi cell |
| 15547 | // ([`Neighbours::around_mi_422_chroma`]: at ss_x 1 one |
| 15548 | // chroma 4-px column spans two luma mi columns, and the |
| 15549 | // plain per-mi gather counts each column's whole-unit dc |
| 15550 | // sign twice). The left extent is unsubsampled at ss_y 0, |
| 15551 | // so its per-mi cells already line up one-to-one. |
| 15552 | let around = if ss_x(fctx) == 1 && ss_y(fctx) == 0 { |
| 15553 | neighbours.around_mi_422_chroma(pair_mi, pw, ph) |
| 15554 | } else { |
| 15555 | neighbours.around_mi_rect(pair_mi, pw, ph) |
| 15556 | }; |
| 15557 | // lane-av1-444: the strip's own 16x4 / 4x16 chroma at ss 0/0 |
| 15558 | // (`TxbSet::Chroma8`, `get_txsize_entropy_ctx(TX_16X4)` = |
| 15559 | // the 8x8 set); 4:2:0 keeps the pair's 8x4 / 4x8. |
| 15560 | let (chroma_set, chroma_scan): (TxbSet, &[u16]) = match (cw, ch) { |
| 15561 | (8, 4) => (TxbSet::ChromaRect8x4, &SCAN_8X4[..]), |
| 15562 | (4, 8) => (TxbSet::ChromaRect8x4, &SCAN_4X8[..]), |
| 15563 | (16, 4) => (TxbSet::Chroma8, &SCAN_16X4[..]), |
| 15564 | // lane-av1-444: 4:4:4's own-strip vert chroma is (4,16) |
| 15565 | // -- one TX_4X16. (lane-av1-422o: the 4:2:2 pair's |
| 15566 | // (4,16) never reaches this row; `chroma422_pair16` |
| 15567 | // intercepts it above and chunks it into TX_4X8 units.) |
| 15568 | (4, 16) => (TxbSet::Chroma8, &SCAN_4X16[..]), |
| 15569 | // lane-av1-422: a 4:2:2 HORZ pair (16x8 luma) keeps the |
| 15570 | // pair's height: an 8x8 SQUARE chroma block, the same |
| 15571 | // `TxbSet::Chroma8` set any 8x8 chroma plane reads. The |
| 15572 | // 4:2:0 pair's chroma is 8x4 and 4:4:4's is 16x4, so this |
| 15573 | // row is unreachable there. It is also the witness arm: |
| 15574 | // a restored `/2 chroma height` lands on (8,4) and the |
| 15575 | // counter reads 0. |
| 15576 | (8, 8) => { |
| 15577 | hit!(CHROMA422_SQUARE_HITS); |
| 15578 | (TxbSet::Chroma8, default_scan(8)) |
| 15579 | } |
| 15580 | // lane-av1-422o: with the `chroma422_pair16` arm above, |
| 15581 | // the (2,16) own-strip shape and the 4:4:4 shapes are the |
| 15582 | // only residents; anything else is a named-lane gap. |
| 15583 | _ => unreachable!("decode_rect4_16_strip chroma shape set is closed"), |
| 15584 | }; |
| 15585 | let mut planes = Vec::with_capacity(2); |
| 15586 | for plane in 1..3 { |
| 15587 | let default_tx = default_intra_tx_type(uv_predict_mode as u8); |
| 15588 | let skip_ctx = usize::from(around[plane].0) + usize::from(around[plane].1); |
| 15589 | let mut coding = cdfs.txb(chroma_set, uv_predict_mode); |
| 15590 | let (levels, tx_type) = read_chroma_coeffs_rect( |
| 15591 | dec, |
| 15592 | &mut coding, |
| 15593 | chroma_scan, |
| 15594 | cw, |
| 15595 | ch, |
| 15596 | skip_ctx, |
| 15597 | dc_sign_ctx(around[plane].2), |
| 15598 | default_tx, |
| 15599 | fctx, |
| 15600 | )?; |
| 15601 | planes.push((levels, tx_type)); |
| 15602 | } |
| 15603 | let (v_levels, v_tx_type) = planes.pop().expect("plane 2"); |
| 15604 | let (u_levels, u_tx_type) = planes.pop().expect("plane 1"); |
| 15605 | // CfL averages the luma of the WHOLE pair, the region this one |
| 15606 | // chroma transform covers. |
| 15607 | let ac = alpha.map(|_| cfl_src_rect(ppx, ppy, pw, ph)); |
| 15608 | for (plane, _buf, levels, tx_type) in [ |
| 15609 | (1usize, &mut *u, &u_levels, u_tx_type), |
| 15610 | (2, &mut *v, &v_levels, v_tx_type), |
| 15611 | ] { |
| 15612 | let residual = dequant_and_inverse_typed_wh( |
| 15613 | levels, |
| 15614 | cw, |
| 15615 | ch, |
| 15616 | crate::decode::bit_depth(fctx), |
| 15617 | block_q_idx(fctx), |
| 15618 | plane_q_delta(plane, fctx).0, |
| 15619 | plane_q_delta(plane, fctx).1, |
| 15620 | tx_type, |
| 15621 | block_iqmatrix(fctx, plane, cw, ch, tx_type), |
| 15622 | ); |
| 15623 | if let Some((ub, vb)) = &palette_uv_bufs { |
| 15624 | set_palette_pred(if plane == 1 { ub.clone() } else { vb.clone() }, fctx); |
| 15625 | } |
| 15626 | push_intra_rect(plane, |
| 15627 | cpx, |
| 15628 | cpy, |
| 15629 | cw, |
| 15630 | ch, |
| 15631 | uv_predict_mode, |
| 15632 | angle_delta_uv, |
| 15633 | pair_reach, |
| 15634 | &residual, |
| 15635 | alpha |
| 15636 | .zip(ac) |
| 15637 | .map(|((au, av), ac)| (if plane == 1 { au } else { av }, ac)), |
| 15638 | None, |
| 15639 | smooth_neighbor_uv, fctx, |
| 15640 | ); |
| 15641 | } |
| 15642 | (u_levels, v_levels) |
| 15643 | }; |
| 15644 | neighbours.record_uv_mode_mi(pair_mi.0, pair_mi.1, pw / MI, ph / MI, uv_predict_mode); |
| 15645 | // A lossless pair already stamped each TX_4X4 (`record_mi_chroma`). |
| 15646 | // The single-state rewrite below is the non-lossless TX_8X4/TX_4X8. |
| 15647 | // lane-av1-422o: the chunked pair's cells are stamped PER UNIT in |
| 15648 | // the arm above (libaom's coefficient contexts are per |
| 15649 | // 4x4-cell); a pair-wide state would erase the unit boundary the |
| 15650 | // next block reads. |
| 15651 | if !lossless_pair && !chroma422_pair16 { |
| 15652 | // The chroma planes' coefficient context, over the PAIR's mi span |
| 15653 | // (`decode_leaf_rect8`'s own rule: a subsampled plane's bound is |
| 15654 | // the luma one rounded up to its 4x4 unit). |
| 15655 | let u_state = neighbour_state(&u_levels); |
| 15656 | let v_state = neighbour_state(&v_levels); |
| 15657 | let round_up_even = |n: usize| n.div_ceil(2) * 2; |
| 15658 | let bound_h = round_up_even(neighbours.mi_rows); |
| 15659 | let bound_w = round_up_even(neighbours.mi_cols); |
| 15660 | for cell in 0..(ph / MI) { |
| 15661 | if pair_mi.0 + cell < bound_h |
| 15662 | && let Some(slot) = neighbours.left.get_mut(pair_mi.0 + cell) |
| 15663 | { |
| 15664 | slot[1] = u_state; |
| 15665 | slot[2] = v_state; |
| 15666 | } |
| 15667 | } |
| 15668 | for cell in 0..(pw / MI) { |
| 15669 | if pair_mi.1 + cell < bound_w |
| 15670 | && let Some(slot) = neighbours.above.get_mut(pair_mi.1 + cell) |
| 15671 | { |
| 15672 | slot[1] = u_state; |
| 15673 | slot[2] = v_state; |
| 15674 | } |
| 15675 | } |
| 15676 | } |
| 15677 | last_uv_mode = Some(uv_predict_mode); |
| 15678 | } |
| 15679 | if horz { |
| 15680 | hit!(RECT4_16_HORZ_HITS); |
| 15681 | } else { |
| 15682 | hit!(RECT4_16_VERT_HITS); |
| 15683 | } |
| 15684 | let (pal_y_size, pal_y_colors) = |
| 15685 | palette_y.as_ref().map_or((0, [0u16; 8]), |p| (p.size, p.colors)); |
| 15686 | let (pal_uv_size, pal_uv_colors) = |
| 15687 | palette_uv.as_ref().map_or((0, [0u16; 8]), |p| (p.size, p.u_colors)); |
| 15688 | record_strip_palette( |
| 15689 | neighbours, lmi, bw, bh, pal_y_size, pal_y_colors, pal_uv_size, pal_uv_colors, |
| 15690 | ); |
| 15691 | // `set_txfm_ctxs(mbmi->tx_size, n4_w, n4_h, skip && is_inter, xd)` |
| 15692 | // (libaom `decode_token_recon_block`, run for EVERY block of an inter |
| 15693 | // frame): the above row records `tx_size_wide` over the block's width and |
| 15694 | // the left column `tx_size_high` over its height. This 1:4 strip reader is |
| 15695 | // also the INTER frame's reader for the shape ([`decode_intra_rect4_in_inter`]), |
| 15696 | // and it published neither band -- so the block after a 64x16 strip read |
| 15697 | // the row's init value 64 as its left transform height and took |
| 15698 | // `tx_size_cat3` row 1 where libaom takes row 0 (lane-frame36 r1: the |
| 15699 | // 3840x1608 10-bit stream's first silent wall, decode-order frame 33 at |
| 15700 | // mi(16,480)). Same write as [`set_txfm_ctxs`], rect-aware. |
| 15701 | txfm_partition_update_rect(neighbours, lmi, (tx_w, tx_h), (bw, bh)); |
| 15702 | Ok((mode, last_uv_mode, (tx_w, tx_h))) |
| 15703 | } |
| 15704 | |
| 15705 | |
| 15706 | /// Decodes one true `bw`x`bh` superblock-level `PARTITION_HORZ`/ |
| 15707 | /// `PARTITION_VERT` strip (lane-sbpart r2, `bw, bh` one of `(64, 32)` / |
| 15708 | /// `(32, 64)` -- [`decode_block_rect`]'s own sibling one level up). Unlike |
| 15709 | /// that 32x16/16x32 strip, both dimensions here exceed the 32-coefficient |
| 15710 | /// cap, so LUMA needs the same corner truncation [`decode_block`] already |
| 15711 | /// does for a plain 64x64 square (spec 5.11.40): `get_txsize_entropy_ctx` |
| 15712 | /// resolves TX_64X32/TX_32X64 to TX_64X64 regardless of orientation (see |
| 15713 | /// this function's own charter/report), so both strips read a real 32x32 |
| 15714 | /// corner through [`read_coeffs`] with [`TxbSet::Luma64`]/[`SCAN_32`] and |
| 15715 | /// embed it top-left of a zeroed `bw`x`bh` grid, mirroring |
| 15716 | /// [`read_plane`]'s `tx_side != side` branch generalized to non-square. |
| 15717 | /// CHROMA (TX_32X16/TX_16X32) resolves to TX_32X32 -- a real, *untruncated* |
| 15718 | /// `bw/2`x`bh/2` transform via [`read_coeffs_rect`] with |
| 15719 | /// [`TxbSet::ChromaRect32x16`] and the matching [`SCAN_32X16`]/ |
| 15720 | /// [`SCAN_16X32`], same as [`decode_block_rect`]'s own chroma path. Like |
| 15721 | /// that function, only the single-transform-unit (`tx_depth == 0`) case is |
| 15722 | /// supported: a split transform refuses by name rather than guess-decode. |
| 15723 | #[allow(clippy::too_many_arguments)] |
| 15724 | fn decode_block_rect64( |
| 15725 | dec: &mut SymbolDecoder, |
| 15726 | cdfs: &mut Cdfs, |
| 15727 | neighbours: &mut Neighbours, |
| 15728 | at: (usize, usize), |
| 15729 | bw: usize, |
| 15730 | bh: usize, |
| 15731 | y: &mut PlaneBuf<'static>, |
| 15732 | u: &mut PlaneBuf<'static>, |
| 15733 | v: &mut PlaneBuf<'static>, |
| 15734 | enable_filter_intra: bool, |
| 15735 | allow_screen_content_tools: bool, |
| 15736 | base_q_idx: u8, |
| 15737 | tx_select: bool, |
| 15738 | reduced_tx_set: bool, fctx: &crate::decode::FrameCtx, |
| 15739 | ) -> Result<()> { |
| 15740 | let (r, c) = at; |
| 15741 | let (px, py) = (c * SUB, r * SUB); |
| 15742 | let (nb_above_mode, nb_left_mode) = neighbours.modes_above_left(r, c); |
| 15743 | let (palette_ctx, palette_cache) = neighbours.palette_ctx_and_cache(at); |
| 15744 | let palette_uv_cache = neighbours.palette_uv_cache(at); |
| 15745 | let (skip, mode, angle_delta_y, uv_mode, angle_delta_uv, alpha, filter_intra, palette_y, palette_uv, intrabc) = |
| 15746 | read_intra_mode_rect( |
| 15747 | dec, |
| 15748 | cdfs, |
| 15749 | nb_above_mode, |
| 15750 | nb_left_mode, |
| 15751 | // `is_cfl_allowed` (spec 5.11.5) caps CFL at <=32x32; these |
| 15752 | // superblock-level HORZ/VERT strips are 64x32/32x64, so unlike |
| 15753 | // `decode_block_rect`'s own 32x16/16x32 strips (where `true` is |
| 15754 | // correct), CFL must never be offered here -- passing `true` |
| 15755 | // read `uv_mode` off the 14-symbol `uv_mode_cfl` CDF instead of |
| 15756 | // the real 13-symbol `uv_mode_no_cfl` one and desynced the tile |
| 15757 | // from this block's very first symbol (lane-sbpart r3 bisect). |
| 15758 | false, |
| 15759 | true, |
| 15760 | bw, |
| 15761 | bh, |
| 15762 | enable_filter_intra, |
| 15763 | neighbours.skip_txfm_ctx(r * (SUB / MI), c * (SUB / MI)), |
| 15764 | allow_screen_content_tools, |
| 15765 | Some((palette_ctx, &palette_cache)), |
| 15766 | &palette_uv_cache, |
| 15767 | r * (SUB / MI), |
| 15768 | c * (SUB / MI), fctx, |
| 15769 | )?; |
| 15770 | if let Some((dv_row, dv_col)) = intrabc { |
| 15771 | return decode_intrabc_owned_rect( |
| 15772 | dec, cdfs, neighbours, (r * (SUB / MI), c * (SUB / MI)), (bw, bh), skip, |
| 15773 | (dv_row, dv_col), y, u, v, base_q_idx, tx_select, reduced_tx_set, fctx, |
| 15774 | ); |
| 15775 | } |
| 15776 | if (palette_y.is_some() || palette_uv.is_some()) && !skip { |
| 15777 | // lane-t900 r34: a non-skip palette block on a superblock-level strip |
| 15778 | // -- counted so its gate cannot go vacuous (this was a refusal until |
| 15779 | // r34; see the gate `a_real_aomenc_palette_stream_with_8x8_leaves_ |
| 15780 | // decodes_pixel_exact`). |
| 15781 | hit!(PALETTE_SB_STRIP_HITS); |
| 15782 | } |
| 15783 | let smooth_neighbor = is_smooth_mode(nb_above_mode) || is_smooth_mode(nb_left_mode); |
| 15784 | if smooth_neighbor { |
| 15785 | hit!(SMOOTH_LUMA_HITS); |
| 15786 | } |
| 15787 | let smooth_neighbor_uv = |
| 15788 | neighbours.smooth_uv_neighbour(r * (SUB / MI), c * (SUB / MI), r, c, fctx); |
| 15789 | if filter_intra.is_some() { |
| 15790 | // `filter_intra_size_class_rect` already returns `None` for both |
| 15791 | // `(64, 32)` and `(32, 64)` (`av1_filter_intra_allowed_bsize` caps at |
| 15792 | // 32 on both axes), so this symbol is never read at this level -- |
| 15793 | // kept only as the same defensive refusal [`decode_block_rect`] has. |
| 15794 | return Err(unsupported( |
| 15795 | "filter intra on a superblock-level HORZ/VERT strip (never expected -- \ |
| 15796 | av1_filter_intra_allowed_bsize caps at 32x32)", |
| 15797 | )); |
| 15798 | } |
| 15799 | let uv_predict_mode = if uv_mode == UV_CFL_PRED { |
| 15800 | DC_PRED |
| 15801 | } else { |
| 15802 | uv_mode |
| 15803 | }; |
| 15804 | let (mi_r, mi_c) = (r * (SUB / MI), c * (SUB / MI)); |
| 15805 | // lane-av1mixloss: the lossless-segment early return (`decodeframe.c:1203`) |
| 15806 | // precedes `TX_MODE_SELECT`, so a lossless-segment 64-axis rect block codes |
| 15807 | // no depth symbol here either (class sweep of the sub8 leaf's fix). |
| 15808 | let depth = if tx_select && !lossless(fctx) { |
| 15809 | // lane-av1txbands: EVERY frame now reads the real `TXFM_CONTEXT` |
| 15810 | // bands (`get_tx_size_context`, libaom `pred_common.h:342`) -- an |
| 15811 | // intra-only frame publishes them from every block too |
| 15812 | // ([`Neighbours::fill_lf_grid_rect`]). Measured on the 10-bit |
| 15813 | // 1920x792 128-superblock stream, |
| 15814 | // decode-order frame 1: the 32x8 HORZ_4 strip at mi(48,176) read |
| 15815 | // `tx_size_cat2` row 1 where libaom reads row 2. |
| 15816 | let ctx = tx_size_context_txfm_rect(neighbours, (mi_r, mi_c), bw, bh, fctx); |
| 15817 | // lane-rectsplit r1: `bsize_to_tx_size_cat` (libaom `decodemv.c`) |
| 15818 | // counts `sub_tx_size_map` steps from the block's own max rect |
| 15819 | // transform down to TX_4X4, minus one -- BLOCK_64X32's chain |
| 15820 | // (TX_64X32 -> TX_32X32 -> TX_16X16 -> TX_8X8 -> TX_4X4) is four |
| 15821 | // steps, so this level's category is 3 (the same one a 64x64 square |
| 15822 | // block uses), not the 2 a 32x16 strip uses. Reading the cat-2 CDF |
| 15823 | // here was a wrong-table read of a real symbol. |
| 15824 | let d = dec.symbol(&mut cdfs.tx_size_cat3[ctx]); |
| 15825 | if crate::envflags::env_flag!("EC_TRACE_MODE_STEP") { |
| 15826 | eprintln!( |
| 15827 | "EC_ISTEP mi_row={mi_r} mi_col={mi_c} name=tx_depth val={d} ctx={ctx} cat=3 rng={}", |
| 15828 | dec.debug_state().0 |
| 15829 | ); |
| 15830 | } |
| 15831 | d |
| 15832 | } else { |
| 15833 | 0 |
| 15834 | }; |
| 15835 | // lane-t900 r1: this strip's own `set_txfm_ctxs` when it is an intra |
| 15836 | // block of an INTER frame -- see [`publish_txfm_bands_if_in_inter`]. |
| 15837 | publish_txfm_bands_if_in_inter( |
| 15838 | neighbours, |
| 15839 | (mi_r, mi_c), |
| 15840 | depth_to_tx_wh(bw, bh, depth, fctx), |
| 15841 | (bw, bh), fctx, |
| 15842 | ); |
| 15843 | if depth != 0 || lossless(fctx) { |
| 15844 | hit!(TX_DEPTH_HITS); |
| 15845 | // lane-rectsplit r1/r4: the superblock-level strip splits its |
| 15846 | // transform through the very same per-unit path as its 32x32-level |
| 15847 | // sibling; [`depth_to_tx_wh`] names the unit, which for a 64x16/16x64 |
| 15848 | // 1:4 strip is the 2:1 rect TX_32X16 at depth 1 (lane-rectchroma r1 |
| 15849 | // measured the old square formula mis-tiling exactly that strip). |
| 15850 | let (tx_w, tx_h) = depth_to_tx_wh(bw, bh, depth, fctx); |
| 15851 | if bw.max(bh) / bw.min(bh) == 4 { |
| 15852 | let which = if depth == 1 { |
| 15853 | &RECT4_SPLIT_DEPTH1_HITS |
| 15854 | } else { |
| 15855 | &RECT4_SPLIT_DEPTH2_HITS |
| 15856 | }; |
| 15857 | hit_do! { which.with(|c| c.set(c.get() + 1)); } |
| 15858 | } |
| 15859 | if crate::envflags::env_flag!("EC_SBPART_DUMP64") { |
| 15860 | eprintln!( |
| 15861 | "DUMP64SPLIT mi_r={mi_r} mi_c={mi_c} px={px} py={py} bw={bw} bh={bh} \ |
| 15862 | depth={depth} tx={tx_w}x{tx_h} mode={mode} uv={uv_predict_mode} skip={skip} \ |
| 15863 | angle_y={angle_delta_y}" |
| 15864 | ); |
| 15865 | } |
| 15866 | let modes = RectStripModes { |
| 15867 | skip, |
| 15868 | mode, |
| 15869 | angle_delta_y, |
| 15870 | uv_predict_mode, |
| 15871 | angle_delta_uv, |
| 15872 | alpha, |
| 15873 | filter_intra, |
| 15874 | smooth_neighbor, |
| 15875 | smooth_neighbor_uv, |
| 15876 | palette_y: palette_y |
| 15877 | .as_ref() |
| 15878 | .map(|p| p.map.iter().map(|&i| p.colors[i as usize]).collect()), |
| 15879 | palette_uv: palette_uv.as_ref().map(|p| { |
| 15880 | ( |
| 15881 | p.map.iter().map(|&i| p.u_colors[i as usize]).collect(), |
| 15882 | p.map.iter().map(|&i| p.v_colors[i as usize]).collect(), |
| 15883 | ) |
| 15884 | }), |
| 15885 | }; |
| 15886 | if palette_y.is_some() || palette_uv.is_some() { |
| 15887 | hit!(PALETTE_SPLIT_TX_HITS); |
| 15888 | } |
| 15889 | decode_rect_split( |
| 15890 | dec, |
| 15891 | cdfs, |
| 15892 | neighbours, |
| 15893 | (mi_r, mi_c), |
| 15894 | bw, |
| 15895 | bh, |
| 15896 | tx_w, |
| 15897 | tx_h, |
| 15898 | &modes, |
| 15899 | y, |
| 15900 | u, |
| 15901 | v, |
| 15902 | base_q_idx, |
| 15903 | reduced_tx_set, fctx, |
| 15904 | )?; |
| 15905 | // Same early-return palette stamp as [`decode_block_rect`]'s split arm |
| 15906 | // (lane-palette2 r10): this return skips the fn's own tail, so a |
| 15907 | // split-tx superblock strip would otherwise leave the above/left |
| 15908 | // palette arrays holding a dead block's colours. |
| 15909 | let (py_size, py_colors) = palette_y.as_ref().map_or((0, [0u16; 8]), |p| (p.size, p.colors)); |
| 15910 | neighbours.record_palette_y_rect((mi_r, mi_c), bw, bh, py_size, py_colors); |
| 15911 | let (puv_size, puv_colors) = |
| 15912 | palette_uv.as_ref().map_or((0, [0u16; 8]), |p| (p.size, p.u_colors)); |
| 15913 | neighbours.record_palette_uv_rect((mi_r, mi_c), bw, bh, puv_size, puv_colors); |
| 15914 | // lane-frame36 r2: `set_txfm_ctxs` runs for EVERY block and this early |
| 15915 | // return skips the fn's own tail, so a SPLIT superblock-level strip |
| 15916 | // published no TXFM_CONTEXT band and the next block read the row's init |
| 15917 | // value 64 as its left transform height. Measured on the 10-bit |
| 15918 | // 3840x1608 stream, decode-order frame 33: after the split 16x64 strip |
| 15919 | // at mi(192,140) (`tx_depth=1` -> TX_16X32) the 32x64 block at |
| 15920 | // mi(192,144) took `tx_size_cat3` row 2 where libaom takes row 1. The |
| 15921 | // bands carry the SPLIT unit's size, not the block's. |
| 15922 | txfm_partition_update_rect(neighbours, (mi_r, mi_c), (tx_w, tx_h), (bw, bh)); |
| 15923 | hit!(RECT64_SPLIT_TXFM_PUBLISH_HITS); |
| 15924 | hit!(RECT_PARTITION_HITS); |
| 15925 | return Ok(()); |
| 15926 | } |
| 15927 | let (tx_w, tx_h) = (bw, bh); |
| 15928 | let (cpx, cpy) = (px >> ss_x(fctx), py >> ss_y(fctx)); |
| 15929 | let (chroma_w, chroma_h) = (bw >> ss_x(fctx), bh >> ss_y(fctx)); |
| 15930 | let reach = Reach::of_rect(bw, bh, px, py, y.width, y.height, fctx); |
| 15931 | if let Some(ref pyv) = palette_y { |
| 15932 | let buf: Vec<u16> = pyv.map.iter().map(|&idx| pyv.colors[idx as usize]).collect(); |
| 15933 | set_palette_pred(buf, fctx); |
| 15934 | } |
| 15935 | let palette_uv_bufs: Option<(Vec<u16>, Vec<u16>)> = palette_uv.as_ref().map(|puv| { |
| 15936 | ( |
| 15937 | puv.map.iter().map(|&idx| puv.u_colors[idx as usize]).collect(), |
| 15938 | puv.map.iter().map(|&idx| puv.v_colors[idx as usize]).collect(), |
| 15939 | ) |
| 15940 | }); |
| 15941 | if skip { |
| 15942 | push_intra_rect(0, |
| 15943 | px, |
| 15944 | py, |
| 15945 | bw, |
| 15946 | bh, |
| 15947 | mode, |
| 15948 | angle_delta_y, |
| 15949 | reach, |
| 15950 | &ZERO_RESIDUAL[..bw * bh], |
| 15951 | None, |
| 15952 | filter_intra, |
| 15953 | smooth_neighbor, fctx, |
| 15954 | ); |
| 15955 | let ac = alpha.map(|_| cfl_src_rect(px, py, bw, bh)); |
| 15956 | if let Some((ub, _)) = &palette_uv_bufs { |
| 15957 | set_palette_pred(ub.clone(), fctx); |
| 15958 | } |
| 15959 | push_intra_rect(1, |
| 15960 | cpx, |
| 15961 | cpy, |
| 15962 | chroma_w, |
| 15963 | chroma_h, |
| 15964 | uv_predict_mode, |
| 15965 | angle_delta_uv, |
| 15966 | reach, |
| 15967 | &ZERO_RESIDUAL[..chroma_w * chroma_h], |
| 15968 | alpha.zip(ac).map(|((au, _), ac)| (au, ac)), |
| 15969 | None, |
| 15970 | smooth_neighbor_uv, fctx, |
| 15971 | ); |
| 15972 | if let Some((_, vb)) = &palette_uv_bufs { |
| 15973 | set_palette_pred(vb.clone(), fctx); |
| 15974 | } |
| 15975 | push_intra_rect(2, |
| 15976 | cpx, |
| 15977 | cpy, |
| 15978 | chroma_w, |
| 15979 | chroma_h, |
| 15980 | uv_predict_mode, |
| 15981 | angle_delta_uv, |
| 15982 | reach, |
| 15983 | &ZERO_RESIDUAL[..chroma_w * chroma_h], |
| 15984 | alpha.zip(ac).map(|((_, av), ac)| (av, ac)), |
| 15985 | None, |
| 15986 | smooth_neighbor_uv, fctx, |
| 15987 | ); |
| 15988 | neighbours.record_rect( |
| 15989 | at, |
| 15990 | bw, |
| 15991 | bh, |
| 15992 | mode, |
| 15993 | uv_predict_mode, |
| 15994 | &[ |
| 15995 | &ZERO_RESIDUAL[..bw * bh], |
| 15996 | &ZERO_RESIDUAL[..chroma_w * chroma_h], |
| 15997 | &ZERO_RESIDUAL[..chroma_w * chroma_h], |
| 15998 | ], |
| 15999 | ); |
| 16000 | } else { |
| 16001 | let around = neighbours.around_rect(at, bw, bh); |
| 16002 | // LUMA: the coded corner, embedded top-left of the true bw x bh grid |
| 16003 | // (see this function's own doc comment). A 64-length axis codes only |
| 16004 | // its low 32 coefficients, so the corner is `min(bw, 32)` x |
| 16005 | // `min(bh, 32)`: 32x32 under a 64x32/32x64 HORZ/VERT strip, and |
| 16006 | // 32x16/16x32 under a 64x16/16x64 1:4 strip (lane-tx64x16). |
| 16007 | let (luma_cw, luma_ch) = (bw.min(32), bh.min(32)); |
| 16008 | let scan32 = default_scan(TX32); |
| 16009 | if coeff_trace_on() { |
| 16010 | let (rng, _) = dec.debug_state(); |
| 16011 | eprintln!("EC_COEFF plane=0 row={mi_r} col={mi_c} tx_size={luma_cw}x{luma_ch} rng={rng}"); |
| 16012 | } |
| 16013 | let (luma_corner, luma_tx_type) = if (luma_cw, luma_ch) == (32, 32) { |
| 16014 | let mut luma_coding = cdfs.txb(TxbSet::Luma64, mode); |
| 16015 | read_coeffs( |
| 16016 | dec, |
| 16017 | &mut luma_coding, |
| 16018 | &scan32, |
| 16019 | 0, |
| 16020 | dc_sign_ctx(around[0].2), |
| 16021 | TxType::DctDct, |
| 16022 | Some((bw, bh)), |
| 16023 | )? |
| 16024 | } else { |
| 16025 | // `av1_get_adjusted_tx_size` (`blockd.h:1361`) maps TX_64X16 -> |
| 16026 | // TX_32X16 and TX_16X64 -> TX_16X32, and `av1_scan_orders` |
| 16027 | // (`scan.c`) gives both the very same `default_scan_32x16` / |
| 16028 | // `default_scan_16x32` the 2:1 sizes use ("Half of the |
| 16029 | // coefficients of tx64 at higher frequencies are set to zeros. So |
| 16030 | // tx32's scan order is used"). `get_txsize_entropy_ctx` is |
| 16031 | // (txsize_sqr + txsize_sqr_up + 1) >> 1 = (TX_16X16 + TX_64X64 + |
| 16032 | // 1) >> 1 = TX_32X32 and `txsize_log2_minus4[TX_64X16]` is 5 (a |
| 16033 | // 512-position eob group) -- BOTH identical to TX_32X16, so the |
| 16034 | // 2:1 CDF set `LumaRect32x16` is bit-exact here and no new table |
| 16035 | // is owed. `av1_nz_map_ctx_offset[TX_64X16]` is literally |
| 16036 | // `av1_nz_map_ctx_offset_32x16` (`txb_common.c:340`), which is |
| 16037 | // what `base_ctx_rect` computes at (32, 16). |
| 16038 | let luma_scan: &[u16] = if bw == 64 { &SCAN_32X16 } else { &SCAN_16X32 }; |
| 16039 | let mut luma_coding = cdfs.txb(TxbSet::LumaRect32x16, mode); |
| 16040 | read_coeffs_rect( |
| 16041 | dec, |
| 16042 | &mut luma_coding, |
| 16043 | luma_scan, |
| 16044 | luma_cw, |
| 16045 | luma_ch, |
| 16046 | 0, |
| 16047 | dc_sign_ctx(around[0].2), |
| 16048 | TxType::DctDct, |
| 16049 | )? |
| 16050 | }; |
| 16051 | if coeff_trace_on() { |
| 16052 | let (rng, _) = dec.debug_state(); |
| 16053 | eprintln!("EC_COEFF_VAL plane=0 row={mi_r} col={mi_c} rng={rng}"); |
| 16054 | } |
| 16055 | let mut luma_levels = vec![0i32; bw * bh]; |
| 16056 | for row in 0..luma_ch { |
| 16057 | luma_levels[row * bw..][..luma_cw] |
| 16058 | .copy_from_slice(&luma_corner[row * luma_cw..][..luma_cw]); |
| 16059 | } |
| 16060 | { |
| 16061 | let cur = block_q_idx(fctx); |
| 16062 | if cur != i32::from(base_q_idx) { |
| 16063 | hit!(RECT64_QIDX_DRIFT_HITS); |
| 16064 | } |
| 16065 | if crate::envflags::env_flag!("EC_AV1_TRACE") { |
| 16066 | eprintln!( |
| 16067 | "TRACE rect64_dequant plane=0 base_q_idx={base_q_idx} current_q_idx={cur}" |
| 16068 | ); |
| 16069 | } |
| 16070 | } |
| 16071 | let luma_residual = dequant_and_inverse_typed_wh( |
| 16072 | &luma_levels, |
| 16073 | bw, |
| 16074 | bh, |
| 16075 | crate::decode::bit_depth(fctx), |
| 16076 | block_q_idx(fctx), |
| 16077 | plane_q_delta(0, fctx).0, |
| 16078 | plane_q_delta(0, fctx).1, |
| 16079 | luma_tx_type, |
| 16080 | block_iqmatrix(fctx, 0, bw, bh, luma_tx_type), |
| 16081 | ); |
| 16082 | push_intra_rect(0, |
| 16083 | px, |
| 16084 | py, |
| 16085 | bw, |
| 16086 | bh, |
| 16087 | mode, |
| 16088 | angle_delta_y, |
| 16089 | reach, |
| 16090 | &luma_residual, |
| 16091 | None, |
| 16092 | filter_intra, |
| 16093 | smooth_neighbor, fctx, |
| 16094 | ); |
| 16095 | if crate::envflags::env_flag!("EC_SBPART_DUMP64") { |
| 16096 | let leftcol: Vec<u16> = if px > 0 { |
| 16097 | (py..py + bh).map(|row| y.data[row * y.width + px - 1]).collect() |
| 16098 | } else { |
| 16099 | vec![] |
| 16100 | }; |
| 16101 | eprintln!( |
| 16102 | "DUMP64 mi_r={mi_r} mi_c={mi_c} px={px} py={py} bw={bw} bh={bh} mode={mode} \ |
| 16103 | skip={skip} eob_nonzero={} row0={:?} leftcol={:?}", |
| 16104 | luma_corner.iter().any(|&v| v != 0), |
| 16105 | &y.data[py * y.width + px..][..bw], |
| 16106 | leftcol, |
| 16107 | ); |
| 16108 | } |
| 16109 | // CHROMA. At 4:2:0 the plane block is already one adjusted UV |
| 16110 | // transform (<=32 on each axis). At 4:4:4 a 64-axis strip's chroma |
| 16111 | // is larger than that, so it is tiled into 32-capped units. |
| 16112 | let _ac = alpha.map(|_| cfl_src_rect(px, py, bw, bh)); |
| 16113 | let (uw, uh) = (chroma_w.min(32), chroma_h.min(32)); |
| 16114 | let (nw, nh) = (chroma_w / uw, chroma_h / uh); |
| 16115 | let (chroma_set, chroma_scan): (TxbSet, &[u16]) = match (uw, uh) { |
| 16116 | (32, 32) => (TxbSet::Chroma32, default_scan(32)), |
| 16117 | (32, 16) => (TxbSet::ChromaRect32x16, &SCAN_32X16), |
| 16118 | (16, 32) => (TxbSet::ChromaRect32x16, &SCAN_16X32), |
| 16119 | (32, 8) => (TxbSet::Chroma16, &SCAN_32X8), |
| 16120 | (8, 32) => (TxbSet::Chroma16, &SCAN_8X32), |
| 16121 | _ => { |
| 16122 | return Err(unsupported( |
| 16123 | "a 64-axis strip whose chroma unit has no coefficient table", |
| 16124 | )) |
| 16125 | } |
| 16126 | }; |
| 16127 | let bigger = nw * nh > 1; |
| 16128 | let mut u_levels = vec![0i32; chroma_w * chroma_h]; |
| 16129 | let mut v_levels = vec![0i32; chroma_w * chroma_h]; |
| 16130 | // The tiled units below stamp their own coefficient context |
| 16131 | // immediately, then the whole-strip `record_rect` rewrite paints |
| 16132 | // every chroma cell with ONE whole-strip state and the per-unit DC |
| 16133 | // signs and levels are lost (class `override-slot-on-one-arm`, same |
| 16134 | // shape as `decode_rect_split`'s chroma_tiled clobber). Collected |
| 16135 | // here and replayed after that record, the c8 `mu_chroma_units` way. |
| 16136 | let mut tiled_chroma_units: Vec<((usize, usize), usize, Grid, usize, usize)> = Vec::new(); |
| 16137 | for plane_idx in 1..=2 { |
| 16138 | for cu_row in 0..nh { |
| 16139 | for cu_col in 0..nw { |
| 16140 | let cu_around = if nw == 1 && nh == 1 { |
| 16141 | around |
| 16142 | } else { |
| 16143 | let lsx = uw << ss_x(fctx); |
| 16144 | let lsy = uh << ss_y(fctx); |
| 16145 | let cu_mi = ( |
| 16146 | mi_r + cu_row * (lsy / MI), |
| 16147 | mi_c + cu_col * (lsx / MI), |
| 16148 | ); |
| 16149 | neighbours.around_mi_rect(cu_mi, lsx, lsy) |
| 16150 | }; |
| 16151 | let lsx = uw << ss_x(fctx); |
| 16152 | let lsy = uh << ss_y(fctx); |
| 16153 | let cfl = alpha.map(|(au, av)| { |
| 16154 | let a = if plane_idx == 1 { au } else { av }; |
| 16155 | (a, cfl_src_rect(px + cu_col * lsx, py + cu_row * lsy, lsx, lsy)) |
| 16156 | }); |
| 16157 | let levels = if uw == uh { |
| 16158 | let plane = if plane_idx == 1 { &mut *u } else { &mut *v }; |
| 16159 | if let Some((ub, vb)) = &palette_uv_bufs { |
| 16160 | set_palette_pred((if plane_idx == 1 { ub } else { vb }).clone(), fctx); |
| 16161 | } |
| 16162 | read_plane( |
| 16163 | dec, cdfs, chroma_set, chroma_scan, plane_idx, cu_around[plane_idx], |
| 16164 | mode, uv_predict_mode, angle_delta_uv, reach, plane, |
| 16165 | cpx + cu_col * uw, cpy + cu_row * uh, uw, uw, base_q_idx, |
| 16166 | cfl, None, bigger.then_some(3), smooth_neighbor_uv, fctx, |
| 16167 | )? |
| 16168 | } else { |
| 16169 | let skip_ctx = usize::from(cu_around[plane_idx].0) |
| 16170 | + usize::from(cu_around[plane_idx].1) |
| 16171 | + usize::from(bigger) * 3; |
| 16172 | let mut coding = cdfs.txb(chroma_set, uv_predict_mode); |
| 16173 | let (levels, tx_type) = read_chroma_coeffs_rect( |
| 16174 | dec, &mut coding, chroma_scan, uw, uh, skip_ctx, |
| 16175 | dc_sign_ctx(cu_around[plane_idx].2), TxType::DctDct, fctx, |
| 16176 | )?; |
| 16177 | let residual = dequant_and_inverse_typed_wh( |
| 16178 | &levels, uw, uh, crate::decode::bit_depth(fctx), |
| 16179 | block_q_idx(fctx), |
| 16180 | plane_q_delta(plane_idx, fctx).0, |
| 16181 | plane_q_delta(plane_idx, fctx).1, |
| 16182 | tx_type, |
| 16183 | block_iqmatrix(fctx, plane_idx, uw, uh, tx_type), |
| 16184 | ); |
| 16185 | if let Some((ub, vb)) = &palette_uv_bufs { |
| 16186 | set_palette_pred((if plane_idx == 1 { ub } else { vb }).clone(), fctx); |
| 16187 | } |
| 16188 | push_intra_rect( |
| 16189 | plane_idx, cpx + cu_col * uw, cpy + cu_row * uh, uw, uh, |
| 16190 | uv_predict_mode, angle_delta_uv, reach, &residual, |
| 16191 | cfl, None, smooth_neighbor_uv, fctx, |
| 16192 | ); |
| 16193 | levels |
| 16194 | }; |
| 16195 | let dst = if plane_idx == 1 { &mut u_levels } else { &mut v_levels }; |
| 16196 | for row in 0..uh { |
| 16197 | let d = (cu_row * uh + row) * chroma_w + cu_col * uw; |
| 16198 | dst[d..d + uw].copy_from_slice(&levels[row * uw..][..uw]); |
| 16199 | } |
| 16200 | if bigger { |
| 16201 | neighbours.record_mi_chroma( |
| 16202 | (mi_r + cu_row * (lsy / MI), mi_c + cu_col * (lsx / MI)), |
| 16203 | lsx, lsy, plane_idx, &levels, |
| 16204 | ); |
| 16205 | tiled_chroma_units.push(( |
| 16206 | (mi_r + cu_row * (lsy / MI), mi_c + cu_col * (lsx / MI)), |
| 16207 | plane_idx, |
| 16208 | levels.clone(), |
| 16209 | lsx, |
| 16210 | lsy, |
| 16211 | )); |
| 16212 | } |
| 16213 | } |
| 16214 | } |
| 16215 | } |
| 16216 | neighbours.record_rect(at, bw, bh, mode, uv_predict_mode, &[&luma_levels[..], &u_levels[..], &v_levels[..]]); |
| 16217 | // The per-unit re-stamp: `record_rect` above wrote the whole strip's |
| 16218 | // chroma cells from the assembled grids' one whole-strip state, |
| 16219 | // overwriting exactly the stamps the tiled loop just made. |
| 16220 | for (cu_mi, plane, unit, span_w, span_h) in &tiled_chroma_units { |
| 16221 | neighbours.record_mi_chroma(*cu_mi, *span_w, *span_h, *plane, unit); |
| 16222 | } |
| 16223 | hit!(RECT_COEFF_HITS); |
| 16224 | } |
| 16225 | let (py_size, py_colors) = palette_y.as_ref().map_or((0, [0u16; 8]), |p| (p.size, p.colors)); |
| 16226 | neighbours.record_palette_y_rect((mi_r, mi_c), bw, bh, py_size, py_colors); |
| 16227 | let (puv_size, puv_colors) = |
| 16228 | palette_uv.as_ref().map_or((0, [0u16; 8]), |p| (p.size, p.u_colors)); |
| 16229 | neighbours.record_palette_uv_rect((mi_r, mi_c), bw, bh, puv_size, puv_colors); |
| 16230 | neighbours.fill_skip_grid_rect((mi_r, mi_c), bw / MI, bh / MI, skip); |
| 16231 | neighbours.fill_lf_grid_rect((mi_r, mi_c), bw / MI, bh / MI, tx_w as u8, tx_h as u8, 0, fctx); |
| 16232 | // `set_txfm_ctxs(mbmi->tx_size, n4_w, n4_h, skip && is_inter, xd)` |
| 16233 | // (libaom `decode_token_recon_block`, run for EVERY block of an inter |
| 16234 | // frame): the above row records `tx_size_wide` over the block's width and |
| 16235 | // the left column `tx_size_high` over its height. This 1:4 strip reader is |
| 16236 | // also the INTER frame's reader for the shape ([`decode_intra_rect4_in_inter`]), |
| 16237 | // and it published neither band -- so the block after a 64x16 strip read |
| 16238 | // the row's init value 64 as its left transform height and took |
| 16239 | // `tx_size_cat3` row 1 where libaom takes row 0 (lane-frame36 r1: the |
| 16240 | // 3840x1608 10-bit stream's first silent wall, decode-order frame 33 at |
| 16241 | // mi(16,480)). Same write as [`set_txfm_ctxs`], rect-aware. |
| 16242 | txfm_partition_update_rect(neighbours, (mi_r, mi_c), (tx_w, tx_h), (bw, bh)); |
| 16243 | hit!(SB_RECT_HITS); |
| 16244 | match (bw, bh) { |
| 16245 | (64, 16) => hit!(SB_RECT4_HORZ_HITS), |
| 16246 | (16, 64) => hit!(SB_RECT4_VERT_HITS), |
| 16247 | _ => {} |
| 16248 | } |
| 16249 | if crate::envflags::env_flag!("EC_AV1_TRACE") { |
| 16250 | let (rng, _) = dec.debug_state(); |
| 16251 | eprintln!("TRACE_RECT64_END mi_row={mi_r} mi_col={mi_c} bw={bw} bh={bh} rng={rng}"); |
| 16252 | } |
| 16253 | Ok(()) |
| 16254 | } |
| 16255 | |
| 16256 | /// `read_angle_delta`/`Angle_Delta` (spec 5.11.42 + 9.3): the symbol minus |
| 16257 | /// [`ANGLE_DELTA_ZERO`] gives the signed `-MAX_ANGLE_DELTA..=MAX_ANGLE_DELTA` |
| 16258 | /// delta [`crate::intra::predict`] wants; bumps [`ANGLE_DELTA_HITS`] whenever |
| 16259 | /// it lands away from zero, for gate tests to prove a real stream fired one. |
| 16260 | fn read_angle_delta(dec: &mut SymbolDecoder, cdf: &mut [u16]) -> i32 { |
| 16261 | let symbol = dec.symbol(cdf); |
| 16262 | if symbol != ANGLE_DELTA_ZERO { |
| 16263 | hit!(ANGLE_DELTA_HITS); |
| 16264 | } |
| 16265 | symbol as i32 - ANGLE_DELTA_ZERO as i32 |
| 16266 | } |
| 16267 | |
| 16268 | #[allow(clippy::too_many_arguments)] |
| 16269 | fn read_intra_mode( |
| 16270 | dec: &mut SymbolDecoder, |
| 16271 | cdfs: &mut Cdfs, |
| 16272 | above_mode: usize, |
| 16273 | left_mode: usize, |
| 16274 | cfl: bool, |
| 16275 | side: usize, |
| 16276 | enable_filter_intra: bool, |
| 16277 | skip_ctx: usize, |
| 16278 | allow_screen_content_tools: bool, |
| 16279 | allow_intrabc: bool, |
| 16280 | // `Some((palette_mode_ctx, color_cache))` at the call sites this lane's |
| 16281 | // scope actually reconstructs a palette-Y block for (`av1_get_palette_mode_ctx` |
| 16282 | // and `av1_get_palette_cache`'s neighbour lookup, [`Neighbours::palette_ctx_and_cache`]); |
| 16283 | // `None` at the excluded call sites (the 8x8-leaf path), which still read |
| 16284 | // the `palette_y_mode` symbol at ctx 0 -- matching the bits a real |
| 16285 | // decoder reads either way -- but refuse by name the moment it fires, |
| 16286 | // exactly the old behaviour. |
| 16287 | palette: Option<(usize, &[u16])>, |
| 16288 | // The U-channel colour cache for a chroma palette read ([`Neighbours::palette_uv_cache`]), |
| 16289 | // `&[]` at the excluded call sites -- same corner-cut as `palette` above, |
| 16290 | // no `mode_ctx` needed (`palette_uv_mode_ctx` is this block's own just-read |
| 16291 | // `use_palette_y`, not a neighbour lookup). |
| 16292 | palette_uv_cache: &[u16], |
| 16293 | mi_r: usize, |
| 16294 | mi_c: usize, fctx: &crate::decode::FrameCtx, |
| 16295 | ) -> Result<( |
| 16296 | bool, |
| 16297 | usize, |
| 16298 | i32, |
| 16299 | usize, |
| 16300 | i32, |
| 16301 | Option<(i32, i32)>, |
| 16302 | Option<usize>, |
| 16303 | Option<PaletteY>, |
| 16304 | Option<PaletteUv>, |
| 16305 | )> { |
| 16306 | let trace = crate::envflags::env_flag!("EC_AV1_TRACE"); |
| 16307 | // lane-tiny r2: EC_ISTEP-format trace (same "name=... val=... rng=..." |
| 16308 | // shape the oracle's own `ec_read_intra_frame_mode_info_impl` prints |
| 16309 | // under `EC_TRACE_MODE_STEP`) so a range ladder can be diffed line for |
| 16310 | // line against the instrumented aomdec, not just `tell()`. |
| 16311 | let ec_istep = crate::envflags::env_flag!("EC_TRACE_MODE_STEP"); |
| 16312 | if ec_istep { |
| 16313 | // Mirrors the oracle's own `EC_IMODE mi_row=.. mi_col=.. rng=..` line |
| 16314 | // (decodemv.c:818), printed at the same point: before the first |
| 16315 | // mode-info symbol of the block, so a cross-decoder range ladder can |
| 16316 | // be keyed on the BLOCK, not just on the symbol sequence. |
| 16317 | eprintln!("EC_IMODE mi_row={mi_r} mi_col={mi_c} fn=sq side={side} rng={}", dec.debug_state().0); |
| 16318 | } |
| 16319 | macro_rules! istep { |
| 16320 | ($name:literal, $val:expr) => { |
| 16321 | if ec_istep { |
| 16322 | let (rng, _) = dec.debug_state(); |
| 16323 | eprintln!( |
| 16324 | "EC_ISTEP mi_row={mi_r} mi_col={mi_c} name={} val={} rng={rng}", |
| 16325 | $name, $val |
| 16326 | ); |
| 16327 | } |
| 16328 | }; |
| 16329 | } |
| 16330 | // lane-sbrect10 r1: `is_cfl_allowed`'s <=32 bound, enforced at the single |
| 16331 | // point every square caller routes through (see `read_intra_mode_rect`). |
| 16332 | let cfl = if cfl && side > 32 { |
| 16333 | hit!(NOCFL_UV_MODE_HITS); |
| 16334 | false |
| 16335 | } else { |
| 16336 | cfl |
| 16337 | }; |
| 16338 | // spec 5.11.6 `intra_frame_mode_info`: `intra_segment_id` is read BEFORE |
| 16339 | // `skip` when `SegIdPreSkip`, and after it otherwise. |
| 16340 | let (seg_w_mi, seg_h_mi) = (side / 4, side / 4); |
| 16341 | if seg_id_pre_skip(fctx) { |
| 16342 | intra_segment_id(dec, cdfs, mi_r, mi_c, seg_w_mi, seg_h_mi, false, fctx); |
| 16343 | } |
| 16344 | let skip = dec.symbol(&mut cdfs.skip[skip_ctx]) != 0; |
| 16345 | if trace { |
| 16346 | eprintln!("TRACE skip value={} rng={}", skip as i32, dec.debug_state().0); |
| 16347 | } |
| 16348 | istep!("skip", skip as i32); |
| 16349 | if !seg_id_pre_skip(fctx) { |
| 16350 | intra_segment_id(dec, cdfs, mi_r, mi_c, seg_w_mi, seg_h_mi, skip, fctx); |
| 16351 | } |
| 16352 | // spec order (see the comment below on `read_intrabc_info`): `skip`, |
| 16353 | // `segment_id`, `cdef`, `delta_q` -- `cdef` lands right here. |
| 16354 | maybe_read_cdef_idx(dec, mi_r, mi_c, (side / MI, side / MI), skip, fctx); |
| 16355 | istep!("cdef", 0); |
| 16356 | // lane-sb128b r1: "this leaf IS the superblock" is `side == sb_size`, not |
| 16357 | // a hardcoded 64 -- a `PARTITION_NONE` 128x128 root is the whole |
| 16358 | // superblock and skips `delta_q` exactly like a 64 one under a 64 root. |
| 16359 | let whole_sb = side == sb_mi_cur(fctx) as usize * 4; |
| 16360 | maybe_read_delta_q(dec, cdfs, mi_r, mi_c, whole_sb, skip, fctx); |
| 16361 | maybe_read_delta_lf(dec, cdfs, mi_r, mi_c, whole_sb, skip, fctx); |
| 16362 | istep!("dq", 0); |
| 16363 | // `read_intrabc_info` (spec 5.11.13, libaom decodemv.c:693, called at |
| 16364 | // :811 right after `skip_txfm`/`segment_id`/`cdef`/`delta_q` and before |
| 16365 | // `mbmi->mode` is ever read): a `use_intrabc` symbol only present on an |
| 16366 | // intra frame whose header set `allow_intrabc`. Actual intra block copy |
| 16367 | // needs `assign_dv`'s own motion-vector-prediction machinery (`av1_find_mv_refs`, |
| 16368 | // `av1_find_ref_dv`) this decoder does not carry -- lane-screen scope |
| 16369 | // draws the line at consuming this one flag symbol and refusing by name |
| 16370 | // when it fires, so the arithmetic decoder still resyncs for a stream |
| 16371 | // that merely allows (but never uses) intrabc. |
| 16372 | if allow_intrabc { |
| 16373 | let use_intrabc = dec.symbol(&mut cdfs.intrabc) != 0; |
| 16374 | if crate::envflags::env_flag!("EC_TRACE_MODE_STEP") { |
| 16375 | eprintln!("EC_ISTEP mi_row={mi_r} mi_col={mi_c} name=intrabc val={} rng={}", u8::from(use_intrabc), dec.debug_state().0); |
| 16376 | } |
| 16377 | if trace { |
| 16378 | eprintln!("TRACE use_intrabc value={}", use_intrabc as i32); |
| 16379 | } |
| 16380 | if use_intrabc { |
| 16381 | let dv = read_intrabc_dv(dec, cdfs, mi_r, mi_c, (side / MI, side / MI), fctx); |
| 16382 | if trace { |
| 16383 | eprintln!("TRACE intrabc dv={dv:?}"); |
| 16384 | } |
| 16385 | fctx.intrabc_dv.with(|c| c.set(Some(dv))); |
| 16386 | hit!(INTRABC_HITS); |
| 16387 | // spec 5.11.13 / libaom `read_intra_frame_mode_info` returns the |
| 16388 | // moment `is_intrabc_block`: no y/uv mode, angle delta, palette, |
| 16389 | // CFL or filter-intra syntax follows. `YMode`/`UVMode` are forced |
| 16390 | // `DC_PRED` (which is also what the neighbour mode contexts and |
| 16391 | // the loop filter then see). |
| 16392 | return Ok((skip, DC_PRED, 0, DC_PRED, 0, None, None, None, None)); |
| 16393 | } |
| 16394 | } |
| 16395 | let above_ctx = INTRA_MODE_CTX[above_mode]; |
| 16396 | let left_ctx = INTRA_MODE_CTX[left_mode]; |
| 16397 | let mode = dec.symbol(&mut cdfs.kf_y_mode[above_ctx][left_ctx]); |
| 16398 | if trace { |
| 16399 | eprintln!("TRACE y_mode ctx=({above_ctx},{left_ctx}) value={mode} rng={}", dec.debug_state().0); |
| 16400 | } |
| 16401 | istep!("mode", mode as i32); |
| 16402 | let angle_delta_y = if (V_PRED..=D67_PRED).contains(&mode) { |
| 16403 | let delta = read_angle_delta(dec, &mut cdfs.angle_delta[mode - V_PRED]); |
| 16404 | if trace { |
| 16405 | eprintln!( |
| 16406 | "TRACE angle_delta value={}", |
| 16407 | delta + ANGLE_DELTA_ZERO as i32 |
| 16408 | ); |
| 16409 | } |
| 16410 | delta |
| 16411 | } else { |
| 16412 | 0 |
| 16413 | }; |
| 16414 | istep!("angle_y", angle_delta_y); |
| 16415 | // lane-av1mono: a monochrome block codes no chroma mode/CFL/palette |
| 16416 | // symbol at all (libaom `decodemv.c:933`'s `!monochrome && is_chroma_ref` |
| 16417 | // guard). Fold `NumPlanes == 1` into the same `has_chroma` this reader |
| 16418 | // gates its chroma symbols on. |
| 16419 | let has_chroma = !mono(fctx); |
| 16420 | let cfl = cfl && has_chroma && cfl_allowed_px(side, side, fctx); |
| 16421 | let uv_mode = if !has_chroma { |
| 16422 | DC_PRED |
| 16423 | } else if cfl { |
| 16424 | dec.symbol(&mut cdfs.uv_mode_cfl[mode]) |
| 16425 | } else { |
| 16426 | dec.symbol(&mut cdfs.uv_mode_no_cfl[mode]) |
| 16427 | }; |
| 16428 | if trace { |
| 16429 | eprintln!("TRACE uv_mode cfl={cfl} y_mode={mode} value={uv_mode} rng={}", dec.debug_state().0); |
| 16430 | } |
| 16431 | istep!("uv_mode", uv_mode as i32); |
| 16432 | let alpha = if cfl && uv_mode == UV_CFL_PRED { |
| 16433 | Some(read_cfl_alphas(dec, cdfs)) |
| 16434 | } else { |
| 16435 | None |
| 16436 | }; |
| 16437 | // `SMOOTH_PRED..PAETH_PRED` (9..=12) chroma is a separate round-2 gap |
| 16438 | // from this lane's directional chroma: a corner block with neither |
| 16439 | // `above` nor `left` (both `Edges::build`'s no-neighbour fallback, |
| 16440 | // 127/129) fed `SMOOTH_PRED` produces the wrong pixel there (worst delta |
| 16441 | // 78 against ffmpeg, traced 2026-08-27) -- an existing, un-lane-touched |
| 16442 | // bug in that fallback this lane's own scope does not cover. Keep it |
| 16443 | // refused here rather than ship a silently-wrong decode. |
| 16444 | if (9..=12).contains(&uv_mode) { |
| 16445 | hit!(SMOOTH_UV_HITS); |
| 16446 | } |
| 16447 | // `get_uv_mode` (spec 9.3): `UV_CFL_PRED` predicts as `DC_PRED` for the |
| 16448 | // angle-delta question -- libaom's own `read_intra_frame_mode_info` reads |
| 16449 | // `angle_delta_uv` off `get_uv_mode(uv_mode)`, never off `uv_mode` raw, |
| 16450 | // so a CFL block (uv_mode==13, outside `V_PRED..=D67_PRED`) already takes |
| 16451 | // the `else` branch below and never reads one either way. |
| 16452 | let angle_delta_uv = if (V_PRED..=D67_PRED).contains(&uv_mode) { |
| 16453 | hit!(DIRECTIONAL_UV_HITS); |
| 16454 | read_angle_delta(dec, &mut cdfs.angle_delta[uv_mode - V_PRED]) |
| 16455 | } else { |
| 16456 | 0 |
| 16457 | }; |
| 16458 | istep!("angle_uv", angle_delta_uv); |
| 16459 | if angle_delta_uv != 0 { |
| 16460 | hit!(UV_ANGLE_DELTA_HITS); |
| 16461 | } |
| 16462 | // `read_palette_mode_info` (spec 5.11.13, libaom decodemv.c:567, called |
| 16463 | // at :840 right after `xd->cfl.store_y` and before `read_filter_intra_mode_info`): |
| 16464 | // gated by `av1_allow_palette` (blockd.h -- size bound only, |
| 16465 | // [`palette_bsize_ctx`]). `palette_mode_ctx` comes from the caller |
| 16466 | // (`palette` param, `None` refuses unconditionally at ctx 0 -- the old |
| 16467 | // behaviour, bit-identical since a hardcoded ctx-0 read and a real one |
| 16468 | // read the same CDF row whenever every neighbour really is non-palette). |
| 16469 | // `palette_uv_mode_ctx` is this block's own just-decided Y palette use, |
| 16470 | // per `read_palette_mode_info`'s own `pmi->palette_size[0] > 0` (not a |
| 16471 | // neighbour lookup at all), so it is exact even at the excluded call |
| 16472 | // sites once Y decodes real syntax there too. |
| 16473 | // |
| 16474 | // lane-palette r7: libaom's `decode_color_map_tokens` is NOT called |
| 16475 | // inline from `read_palette_mode_info` -- it runs later, from |
| 16476 | // `parse_decode_block` (`decodeframe.c:1135`, `av1_visit_palette`), |
| 16477 | // after the WHOLE mode-info read for the block, including UV palette |
| 16478 | // mode_info (`read_palette_mode_info`'s own second half, right below) |
| 16479 | // and `read_filter_intra_mode_info`. So this branch now only reads |
| 16480 | // mode/size/colours for both planes (deferring the two colour-index-map |
| 16481 | // decodes to after `filter_intra`, below) -- r6's range trace pinned |
| 16482 | // this exact ordering bug (`compare-range-not-tell`: ranges matched |
| 16483 | // bit-for-bit through the Y colours, diverged only at the old inline |
| 16484 | // `decode_color_index_map` call). |
| 16485 | let mut palette_y_pending: Option<(usize, [u16; 8])> = None; |
| 16486 | let mut palette_uv_pending: Option<(usize, [u16; 8], [u16; 8])> = None; |
| 16487 | if allow_screen_content_tools |
| 16488 | && let Some(bsize_ctx) = palette_bsize_ctx(side) |
| 16489 | { |
| 16490 | let (mode_ctx, cache) = palette.unwrap_or((0, &[])); |
| 16491 | if mode == DC_PRED { |
| 16492 | if trace { |
| 16493 | let (rng, _) = dec.debug_state(); |
| 16494 | eprintln!( |
| 16495 | "TRACE pre_palette_y_mode bsize_ctx={bsize_ctx} mode_ctx={mode_ctx} rng={rng}" |
| 16496 | ); |
| 16497 | } |
| 16498 | } |
| 16499 | let use_palette_y = |
| 16500 | mode == DC_PRED && dec.symbol(&mut cdfs.palette_y_mode[bsize_ctx][mode_ctx]) != 0; |
| 16501 | if mode == DC_PRED && trace { |
| 16502 | let (rng, _) = dec.debug_state(); |
| 16503 | eprintln!("TRACE palette_y_mode value={} rng={rng}", use_palette_y as i32); |
| 16504 | } |
| 16505 | if use_palette_y { |
| 16506 | let n = 2 + dec.symbol(&mut cdfs.palette_y_size[bsize_ctx]); |
| 16507 | if trace { |
| 16508 | let (rng, _) = dec.debug_state(); |
| 16509 | eprintln!("TRACE palette_y_size n={n} rng={rng}"); |
| 16510 | } |
| 16511 | let colors = read_palette_colors_y(dec, n, cache, fctx); |
| 16512 | if trace { |
| 16513 | let (rng, _) = dec.debug_state(); |
| 16514 | eprintln!("TRACE palette_y_colors colors={:?} rng={rng}", &colors[..n]); |
| 16515 | } |
| 16516 | palette_y_pending = Some((n, colors)); |
| 16517 | } |
| 16518 | // `read_palette_mode_info`'s UV half (decodemv.c:588): read |
| 16519 | // unconditionally at this bit position whenever `uv_mode == |
| 16520 | // UV_DC_PRED`, regardless of whether Y just fired -- this decoder's |
| 16521 | // scope always reconstructs chroma alongside luma (`decode_block` |
| 16522 | // always calls `u.reconstruct`/`v.reconstruct`), so `is_chroma_ref` |
| 16523 | // is always true here and does not need threading in. |
| 16524 | // |
| 16525 | // lane-av1mono: libaom's actual guard is `num_planes > 1 && |
| 16526 | // uv_mode == UV_DC_PRED && xd->is_chroma_ref` (decodemv.c:602), so a |
| 16527 | // monochrome frame reads NO `palette_uv_mode`/`palette_uv_size` |
| 16528 | // symbol even at this chroma-owning square block. |
| 16529 | let palette_uv_mode_ctx = usize::from(use_palette_y); |
| 16530 | if has_chroma |
| 16531 | && uv_mode == DC_PRED |
| 16532 | && dec.symbol(&mut cdfs.palette_uv_mode[palette_uv_mode_ctx]) != 0 |
| 16533 | { |
| 16534 | let n = 2 + dec.symbol(&mut cdfs.palette_uv_size[bsize_ctx]); |
| 16535 | let (u_colors, v_colors) = read_palette_colors_uv(dec, n, palette_uv_cache, fctx); |
| 16536 | palette_uv_pending = Some((n, u_colors, v_colors)); |
| 16537 | } |
| 16538 | } |
| 16539 | // `read_filter_intra_mode_info` (spec 5.11.14, libaom decodemv.c): a |
| 16540 | // `use_filter_intra` symbol this decoder never read at all until |
| 16541 | // lane-av1real r10 -- silently skipping it left every bit past a |
| 16542 | // DC_PRED block at <=32x32 read one symbol short of libaom for the rest |
| 16543 | // of the tile (the pinned CfL stream's actual bug: partition/skip/mode |
| 16544 | // matched bit-exact, then `eob_pt` read 5 instead of 6, because this |
| 16545 | // symbol's bits were never consumed). |
| 16546 | let mut filter_intra = None; |
| 16547 | // lane-tiles r12: `av1_filter_intra_allowed` (blockd.h) also requires |
| 16548 | // `mbmi->palette_mode_info.palette_size[0] == 0` -- libaom writes NO |
| 16549 | // `use_filter_intra` symbol for a Y-palette block, and reading one here |
| 16550 | // desynced every screen-content stream from the first palette block on |
| 16551 | // (it surfaced as a bogus "partition below 8x8" refusal further down the |
| 16552 | // tile). |
| 16553 | if mode == DC_PRED |
| 16554 | && enable_filter_intra |
| 16555 | && palette_y_pending.is_none() |
| 16556 | && let Some(class) = filter_intra_size_class(side) |
| 16557 | { |
| 16558 | let use_filter_intra = dec.symbol(&mut cdfs.filter_intra[class]) != 0; |
| 16559 | if trace { |
| 16560 | eprintln!( |
| 16561 | "TRACE use_filter_intra side={side} rng={} value={}", dec.debug_state().0, |
| 16562 | use_filter_intra as i32 |
| 16563 | ); |
| 16564 | } |
| 16565 | istep!("use_filter_intra", use_filter_intra as i32); |
| 16566 | if use_filter_intra { |
| 16567 | hit!(FILTER_INTRA_HITS); |
| 16568 | let fi_mode = dec.symbol(&mut cdfs.filter_intra_mode); |
| 16569 | if trace { |
| 16570 | eprintln!("TRACE filter_intra_mode value={fi_mode}"); |
| 16571 | } |
| 16572 | istep!("filter_intra_mode", fi_mode as i32); |
| 16573 | filter_intra = Some(fi_mode); |
| 16574 | } |
| 16575 | } |
| 16576 | // `av1_visit_palette` (decoder.c:234): plane 0 (Y) then plane 1 (chroma, |
| 16577 | // shared U/V map) -- the same order libaom's own loop visits them in. |
| 16578 | let palette_y = palette_y_pending.map(|(n, colors)| { |
| 16579 | let map = |
| 16580 | decode_color_index_map(dec, cdfs, n, side, palette_onscreen(mi_r, mi_c, side, side, fctx), false); |
| 16581 | hit!(PALETTE_HITS); |
| 16582 | if trace { |
| 16583 | eprintln!("TRACE palette_y size={n} colors={:?}", &colors[..n]); |
| 16584 | } |
| 16585 | PaletteY { size: n, colors, map } |
| 16586 | }); |
| 16587 | let palette_uv = palette_uv_pending.map(|(n, u_colors, v_colors)| { |
| 16588 | let on = palette_onscreen_uv( |
| 16589 | (side, side), |
| 16590 | palette_onscreen(mi_r, mi_c, side, side, fctx), |
| 16591 | (side >> ss_x(fctx), side >> ss_y(fctx)), |
| 16592 | (ss_x(fctx), ss_y(fctx)), |
| 16593 | ); |
| 16594 | // lane-av1-422d: the chroma map's extent is the PLANE BLOCK |
| 16595 | // `(side >> ss_x) x (side >> ss_y)` -- 4x8 at ss (1,0), not the |
| 16596 | // square call's `(side >> ss_x)`-squared (4x4), which left a |
| 16597 | // 16-entry map for a 32-sample unit and panicked the reconstruct. |
| 16598 | let map = decode_color_index_map_wh( |
| 16599 | dec, cdfs, n, side >> ss_x(fctx), side >> ss_y(fctx), on, true, |
| 16600 | ); |
| 16601 | hit!(PALETTE_UV_HITS); |
| 16602 | if trace { |
| 16603 | eprintln!( |
| 16604 | "TRACE palette_uv size={n} u_colors={:?} v_colors={:?}", |
| 16605 | &u_colors[..n], |
| 16606 | &v_colors[..n] |
| 16607 | ); |
| 16608 | } |
| 16609 | PaletteUv { size: n, u_colors, v_colors, map } |
| 16610 | }); |
| 16611 | Ok(( |
| 16612 | skip, |
| 16613 | mode, |
| 16614 | angle_delta_y, |
| 16615 | uv_mode, |
| 16616 | angle_delta_uv, |
| 16617 | alpha, |
| 16618 | filter_intra, |
| 16619 | palette_y, |
| 16620 | palette_uv, |
| 16621 | )) |
| 16622 | } |
| 16623 | |
| 16624 | /// `UV_CFL_PRED` (spec 6.10.19's chroma intra mode enum): the fourteenth |
| 16625 | /// entry of [`crate::cdf::UV_MODE_CFL`], one past the thirteen ordinary |
| 16626 | /// intra modes. |
| 16627 | const UV_CFL_PRED: usize = 13; |
| 16628 | |
| 16629 | /// `read_cfl_alphas` (libaom decodemv.c): the joint sign symbol (8-ary, spec |
| 16630 | /// 5.11.45's `cfl_alpha_signs`), then each plane's alpha magnitude symbol |
| 16631 | /// (16-ary, `cfl_alpha_u`/`cfl_alpha_v`) where its sign is nonzero -- context |
| 16632 | /// indices `CFL_CONTEXT_U`/`CFL_CONTEXT_V` (libaom cfl.h) derived from the |
| 16633 | /// joint sign the same way. Returns each plane's final signed `alpha_q3` |
| 16634 | /// (`cfl_idx_to_alpha`, cfl.c): magnitude `idx + 1`, sign-zero collapsing to 0. |
| 16635 | fn read_cfl_alphas(dec: &mut SymbolDecoder, cdfs: &mut Cdfs) -> (i32, i32) { |
| 16636 | hit!(CFL_BLOCK_HITS); |
| 16637 | let joint_sign = dec.symbol(&mut cdfs.cfl_sign) as i32; |
| 16638 | let sign_u = ((joint_sign + 1) * 11) >> 5; |
| 16639 | let sign_v = (joint_sign + 1) - 3 * sign_u; |
| 16640 | let alpha_u = if sign_u == 0 { |
| 16641 | 0 |
| 16642 | } else { |
| 16643 | let ctx_u = (joint_sign + 1 - 3) as usize; |
| 16644 | let mag = dec.symbol(&mut cdfs.cfl_alpha[ctx_u]) as i32 + 1; |
| 16645 | if sign_u == 2 { mag } else { -mag } |
| 16646 | }; |
| 16647 | let alpha_v = if sign_v == 0 { |
| 16648 | 0 |
| 16649 | } else { |
| 16650 | let ctx_v = (sign_v * 3 + sign_u - 3) as usize; |
| 16651 | let mag = dec.symbol(&mut cdfs.cfl_alpha[ctx_v]) as i32 + 1; |
| 16652 | if sign_v == 2 { mag } else { -mag } |
| 16653 | }; |
| 16654 | (alpha_u, alpha_v) |
| 16655 | } |
| 16656 | |
| 16657 | /// `cfl_luma_subsampling_420_lbd_c` + `subtract_average_c` (libaom cfl.c): |
| 16658 | /// the reconstructed luma samples under a `side`-square block, subsampled |
| 16659 | /// 4:2:0 to Q3 (`(a+b+c+d) << 1`, each 2x2 group), then block-average |
| 16660 | /// subtracted (`round_offset = num_pel/2`, right-shifted by `log2(num_pel)`) |
| 16661 | /// to give the AC values [`cfl_scaled`] scales by alpha. |
| 16662 | fn cfl_ac_q3(y: &PlaneBuf<'_>, px: usize, py: usize, side: usize) -> Vec<i32> { |
| 16663 | cfl_ac_q3_at(px, py, side, side, |lx, ly| i32::from(y.data[ly * y.width + lx])) |
| 16664 | } |
| 16665 | |
| 16666 | /// The one body [`cfl_ac_q3`] and [`cfl_ac_q3_rect`] share, reading its luma |
| 16667 | /// samples through `sample(x, y)` instead of a [`PlaneBuf`] -- so the encoder |
| 16668 | /// (lane-av1cfl) builds a CfL candidate's AC signal from its own `u8` |
| 16669 | /// reconstruction through THIS function rather than a second transcription of |
| 16670 | /// `cfl_luma_subsampling_420_lbd_c` + `subtract_average_c`. |
| 16671 | /// CfL AC at the frame's subsampling. 4:2:0 keeps the 2x2 average; 4:4:4 |
| 16672 | /// copies each luma sample shifted to Q3 (`cfl_luma_subsampling_444_lbd`). |
| 16673 | fn cfl_ac_ss( |
| 16674 | y: &PlaneBuf<'_>, |
| 16675 | px: usize, |
| 16676 | py: usize, |
| 16677 | bw: usize, |
| 16678 | bh: usize, |
| 16679 | ss_x: usize, |
| 16680 | ss_y: usize, |
| 16681 | ) -> Vec<i32> { |
| 16682 | if ss_x == 0 && ss_y == 0 { |
| 16683 | let mut ac = vec![0i32; bw * bh]; |
| 16684 | let mut sum = 0i32; |
| 16685 | for row in 0..bh { |
| 16686 | for col in 0..bw { |
| 16687 | let q3 = i32::from(y.data[(py + row) * y.width + px + col]) << 3; |
| 16688 | ac[row * bw + col] = q3; |
| 16689 | sum += q3; |
| 16690 | } |
| 16691 | } |
| 16692 | let num_pel = (bw * bh) as i32; |
| 16693 | let avg = (sum + num_pel / 2) >> num_pel.trailing_zeros(); |
| 16694 | ac.iter_mut().for_each(|v| *v -= avg); |
| 16695 | ac |
| 16696 | } else if ss_x == 1 && ss_y == 0 { |
| 16697 | // lane-av1-422: `cfl_luma_subsampling_422_lbd_c` -- each chroma sample |
| 16698 | // averages the HORIZONTAL luma pair above it, at full chroma height |
| 16699 | // (`(a + b) >> 1` in Q3 is `(a + b) << 2`). A rect luma strip's chroma |
| 16700 | // is square here, and the 4:2:0 rect/square helpers below would halve |
| 16701 | // the height it must keep. |
| 16702 | let (cw, ch) = (bw >> 1, bh); |
| 16703 | let mut ac = vec![0i32; cw * ch]; |
| 16704 | let mut sum = 0i32; |
| 16705 | for row in 0..ch { |
| 16706 | for col in 0..cw { |
| 16707 | let (lx, ly) = (px + col * 2, py + row); |
| 16708 | let q3 = (i32::from(y.data[ly * y.width + lx]) |
| 16709 | + i32::from(y.data[ly * y.width + lx + 1])) |
| 16710 | << 2; |
| 16711 | ac[row * cw + col] = q3; |
| 16712 | sum += q3; |
| 16713 | } |
| 16714 | } |
| 16715 | let num_pel = (cw * ch) as i32; |
| 16716 | let avg = (sum + num_pel / 2) >> num_pel.trailing_zeros(); |
| 16717 | ac.iter_mut().for_each(|v| *v -= avg); |
| 16718 | ac |
| 16719 | } else if bw == bh { |
| 16720 | cfl_ac_q3(y, px, py, bw) |
| 16721 | } else { |
| 16722 | cfl_ac_q3_rect(y, px, py, bw, bh) |
| 16723 | } |
| 16724 | } |
| 16725 | |
| 16726 | pub(crate) fn cfl_ac_q3_at( |
| 16727 | px: usize, |
| 16728 | py: usize, |
| 16729 | bw: usize, |
| 16730 | bh: usize, |
| 16731 | sample: impl Fn(usize, usize) -> i32, |
| 16732 | ) -> Vec<i32> { |
| 16733 | let (cw, ch) = (bw / 2, bh / 2); |
| 16734 | let mut ac = vec![0i32; cw * ch]; |
| 16735 | let mut sum = 0i32; |
| 16736 | for cy in 0..ch { |
| 16737 | for cx in 0..cw { |
| 16738 | let (lx, ly) = (px + cx * 2, py + cy * 2); |
| 16739 | let q3 = (sample(lx, ly) |
| 16740 | + sample(lx + 1, ly) |
| 16741 | + sample(lx, ly + 1) |
| 16742 | + sample(lx + 1, ly + 1)) |
| 16743 | << 1; |
| 16744 | ac[cy * cw + cx] = q3; |
| 16745 | sum += q3; |
| 16746 | } |
| 16747 | } |
| 16748 | let num_pel = (cw * ch) as i32; |
| 16749 | let avg = (sum + num_pel / 2) >> num_pel.trailing_zeros(); |
| 16750 | ac.iter_mut().for_each(|v| *v -= avg); |
| 16751 | ac |
| 16752 | } |
| 16753 | |
| 16754 | /// `get_scaled_luma_q0` (libaom cfl.h): `alpha_q3 * ac_q3`, rounded from Q6 |
| 16755 | /// back to whole samples with `ROUND_POWER_OF_TWO_SIGNED` (round-to-nearest, |
| 16756 | /// ties away from zero on the shifted-out sign). |
| 16757 | pub(crate) fn cfl_scaled(alpha_q3: i32, ac_q3: i32) -> i32 { |
| 16758 | let v = alpha_q3 * ac_q3; |
| 16759 | if v >= 0 { |
| 16760 | (v + 32) >> 6 |
| 16761 | } else { |
| 16762 | -((-v + 32) >> 6) |
| 16763 | } |
| 16764 | } |
| 16765 | |
| 16766 | /// One plane's reconstruction buffer, `width * height` samples, and the |
| 16767 | /// prediction/residual reads and writes into it — the decode-side twin of |
| 16768 | /// [`crate::encode`]'s private `Plane`, whose `edges` this mirrors exactly |
| 16769 | /// (own-edge and reach clamps both), passing what it gathers straight to |
| 16770 | /// [`crate::intra::predict`] instead of hand-rolling `DC_PRED` alone. |
| 16771 | /// lane-tiny r4: raw padded-plane dump for filter-stage bisection, written |
| 16772 | /// only when `var` names a path. Same byte shape as `EC_AV1_PREFILT_DUMP`. |
| 16773 | /// lane-hgkf r2: 16-bit cropped stage dump, the high-bitdepth twin of |
| 16774 | /// [`dump_stage`] (whose `as u8` truncation hides |delta| <= 2 defects) -- |
| 16775 | /// matches aomdec's own `ec_dump16` rung byte for byte (crop dims, u16 LE). |
| 16776 | /// lane-mc64 r1: the decode-order index the stage dumps below key their file |
| 16777 | /// name on. `PREFILT_PICTURE_IDX` is bumped once per frame just BEFORE the |
| 16778 | /// deblock/CDEF stages run, so the frame being dumped here is `idx - 1` -- |
| 16779 | /// without this every frame overwrote `.f0` and a per-frame bisection silently |
| 16780 | /// compared the LAST decoded frame (ledger, class stale-output-faked-measurement). |
| 16781 | #[allow(dead_code)] // gate counter accessor with no reader yet; the counter itself is live |
| 16782 | fn stage_dump_idx() -> usize { |
| 16783 | PREFILT_PICTURE_IDX.load(std::sync::atomic::Ordering::SeqCst).saturating_sub(1) |
| 16784 | } |
| 16785 | |
| 16786 | fn dump_stage16(var: &str, y: &PlaneBuf<'_>, u: &PlaneBuf<'_>, v: &PlaneBuf<'_>, fw: usize, fh: usize) { |
| 16787 | use std::io::Write; |
| 16788 | // lane-sbrect10 r2 / lane-cdef r1 (one shared helper): index the dump by |
| 16789 | // decode-order picture like EC_AV1_PREFILT_DUMP already does -- a fixed |
| 16790 | // `.f0` name kept only the LAST frame, so a mid-sequence divergence could |
| 16791 | // not be read out at all. |
| 16792 | if let Ok(path) = crate::envflags::var(var) |
| 16793 | && let Ok(mut f) = std::fs::File::create(format!("{path}.f{}", dump_stage_idx(var))) |
| 16794 | { |
| 16795 | 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))] { |
| 16796 | for row in 0..h { |
| 16797 | let mut bytes = Vec::with_capacity(w * 2); |
| 16798 | for &s in &p.data[row * p.width..][..w] { |
| 16799 | bytes.extend_from_slice(&s.to_le_bytes()); |
| 16800 | } |
| 16801 | let _ = f.write_all(&bytes); |
| 16802 | } |
| 16803 | } |
| 16804 | } |
| 16805 | } |
| 16806 | |
| 16807 | /// Decode-order index of the next [`dump_stage`]/[`dump_stage16`] file for a |
| 16808 | /// given env var -- lane-cdef r1: both used to write `.f0` unconditionally, so |
| 16809 | /// every frame overwrote the previous one and only the LAST frame survived, |
| 16810 | /// which silently defeats any per-frame filter-stage bisection. |
| 16811 | fn dump_stage_idx(var: &str) -> usize { |
| 16812 | use std::collections::HashMap; |
| 16813 | use std::sync::{Mutex, OnceLock}; |
| 16814 | static IDX: OnceLock<Mutex<HashMap<String, usize>>> = OnceLock::new(); |
| 16815 | let mut m = IDX.get_or_init(|| Mutex::new(HashMap::new())).lock().unwrap(); |
| 16816 | let e = m.entry(var.to_string()).or_insert(0); |
| 16817 | let i = *e; |
| 16818 | *e += 1; |
| 16819 | i |
| 16820 | } |
| 16821 | |
| 16822 | fn dump_stage(var: &str, y: &PlaneBuf<'_>, u: &PlaneBuf<'_>, v: &PlaneBuf<'_>) { |
| 16823 | use std::io::Write; |
| 16824 | if let Ok(path) = crate::envflags::var(var) |
| 16825 | && let Ok(mut f) = std::fs::File::create(format!("{path}.f{}", dump_stage_idx(var))) |
| 16826 | { |
| 16827 | for p in [y, u, v] { |
| 16828 | let narrow: Vec<u8> = p.data.iter().map(|&s| s as u8).collect(); |
| 16829 | let _ = f.write_all(&narrow); |
| 16830 | } |
| 16831 | } |
| 16832 | } |
| 16833 | |
| 16834 | /// The value [`fresh_plane`] fills with under `EC_AV1_PLANE_SENTINEL`: no |
| 16835 | /// legal sample can equal it (reconstruction clamps to `(1 << bit_depth) - 1`, |
| 16836 | /// at most 4095), so a decode that hashes identically under the flag proves |
| 16837 | /// the output does not depend on the plane buffer's initial content. |
| 16838 | const PLANE_SENTINEL: u16 = 0xDEAD; |
| 16839 | |
| 16840 | /// A frame plane's own backing store, `n` samples, UNINITIALISED. |
| 16841 | /// |
| 16842 | /// (lane-picalloc) The SB-padded coding surface is written sample-for-sample |
| 16843 | /// by the tile walk's reconstruction before anything reads it, so the |
| 16844 | /// `vec![0u16; n]` this replaces spent ~3% of the frame thread's cycles |
| 16845 | /// zeroing 25 MB (at 4K) that the very next pass overwrites -- serially, at |
| 16846 | /// the frame's head, on the critical path. `EC_AV1_PLANE_SENTINEL=1` swaps the |
| 16847 | /// uninitialised buffer for a [`PLANE_SENTINEL`] fill; the gate hashes both |
| 16848 | /// ways (see the lane report) and the two agree, which is the proof that no |
| 16849 | /// unwritten sample reaches the output. |
| 16850 | fn fresh_plane(n: usize) -> Vec<u16> { |
| 16851 | if crate::envflags::env_flag!("EC_AV1_PLANE_SENTINEL") { |
| 16852 | return vec![PLANE_SENTINEL; n]; |
| 16853 | } |
| 16854 | let mut v: Vec<u16> = Vec::with_capacity(n); |
| 16855 | // SAFETY: `u16` has no invalid bit patterns and no `Drop`, and the |
| 16856 | // capacity was just reserved, so the vector owns `n` initialised-or-not |
| 16857 | // `u16` slots. Every one is written by reconstruction before it is read |
| 16858 | // (proven by the sentinel gate above). |
| 16859 | #[allow(unsafe_code)] |
| 16860 | unsafe { |
| 16861 | v.set_len(n); |
| 16862 | } |
| 16863 | v |
| 16864 | } |
| 16865 | |
| 16866 | #[derive(Clone)] |
| 16867 | struct PlaneBuf<'a> { |
| 16868 | data: std::borrow::Cow<'a, [u16]>, |
| 16869 | width: usize, |
| 16870 | height: usize, |
| 16871 | /// The frame's true, decodable extent in this plane's own units — past |
| 16872 | /// this, samples are the padded coding surface's invented tail, never a |
| 16873 | /// real decoder's edge or reach reads. |
| 16874 | true_width: usize, |
| 16875 | true_height: usize, |
| 16876 | /// This plane's own tile's top-left pixel origin (spec: intra prediction |
| 16877 | /// never reaches across a tile boundary even though an earlier-decoded |
| 16878 | /// tile's real pixels sit right there in the shared buffer) — set via |
| 16879 | /// [`Self::set_tile_origin`] before each tile's own superblock walk, |
| 16880 | /// `0` for the single-tile case. lane-tiles r2: only the left/top bound |
| 16881 | /// is threaded (the two-tile column-split stage 1 fixture's tile always |
| 16882 | /// ends at the frame's own right/bottom edge); a non-last tile's own |
| 16883 | /// right/bottom reach bound is still open, tracked in the report. |
| 16884 | tile_x0: usize, |
| 16885 | tile_y0: usize, |
| 16886 | /// lane-tiles r5: this plane's own tile's right/bottom pixel bound |
| 16887 | /// (exclusive) -- a non-last tile column/row's own reach must stop at |
| 16888 | /// its own SB-aligned tile boundary. `width`/`height` (this plane's |
| 16889 | /// full padded extent) for the single-tile/last-tile case, matching the |
| 16890 | /// old unclipped behaviour exactly. |
| 16891 | tile_x1: usize, |
| 16892 | tile_y1: usize, |
| 16893 | } |
| 16894 | |
| 16895 | /// One entry per `MV_REFERENCE_FRAME` (index 0/1 unused -- `NONE`/ |
| 16896 | /// `INTRA_FRAME`/`LAST_FRAME` all resolve elsewhere), `Some` when this |
| 16897 | /// frame's own `ref_frame_idx` names a DPB slot that has a picture in it |
| 16898 | /// (lane-av1refs: the same "empty slot" case `GOLDEN_FRAME` already |
| 16899 | /// refused by name, now generic across `LAST2`/`LAST3`/`GOLDEN`/`BWDREF`/ |
| 16900 | /// `ALTREF2`/`ALTREF`). |
| 16901 | |
| 16902 | impl PlaneBuf<'_> { |
| 16903 | /// Sets this plane's own tile pixel origin ([`Self::tile_x0`]/ |
| 16904 | /// [`Self::tile_y0`]) before that tile's own superblock walk. `x0`/`y0` |
| 16905 | /// are already in this plane's own units (luma pixels for `y`, chroma |
| 16906 | /// pixels — halved — for `u`/`v`). |
| 16907 | fn set_tile_origin(&mut self, x0: usize, y0: usize, x1: usize, y1: usize) { |
| 16908 | self.tile_x0 = x0; |
| 16909 | self.tile_y0 = y0; |
| 16910 | self.tile_x1 = x1; |
| 16911 | self.tile_y1 = y1; |
| 16912 | } |
| 16913 | |
| 16914 | fn edges( |
| 16915 | &self, |
| 16916 | x: usize, |
| 16917 | y: usize, |
| 16918 | side: usize, |
| 16919 | reach: Reach, |
| 16920 | ) -> (Option<Vec<u16>>, Option<Vec<u16>>, Option<u16>) { |
| 16921 | let own_across = x + side.min(self.true_width.saturating_sub(x)); |
| 16922 | let across = if reach.above_right { |
| 16923 | own_across + side.min(self.true_width.saturating_sub(own_across)) |
| 16924 | } else { |
| 16925 | own_across |
| 16926 | } |
| 16927 | .min(self.width) |
| 16928 | .min(self.tile_x1); |
| 16929 | let own_down = y + side.min(self.true_height.saturating_sub(y)); |
| 16930 | let down = if reach.below_left { |
| 16931 | own_down + side.min(self.true_height.saturating_sub(own_down)) |
| 16932 | } else { |
| 16933 | own_down |
| 16934 | } |
| 16935 | .min(self.height) |
| 16936 | .min(self.tile_y1); |
| 16937 | let above = (y > self.tile_y0 && across > x) |
| 16938 | .then(|| self.data[(y - 1) * self.width + x..][..across - x].to_vec()); |
| 16939 | let left = (x > self.tile_x0 && down > y).then(|| { |
| 16940 | (y..down) |
| 16941 | .map(|row| self.data[row * self.width + x - 1]) |
| 16942 | .collect::<Vec<_>>() |
| 16943 | }); |
| 16944 | let corner = |
| 16945 | (x > self.tile_x0 && y > self.tile_y0).then(|| self.data[(y - 1) * self.width + x - 1]); |
| 16946 | (above, left, corner) |
| 16947 | } |
| 16948 | |
| 16949 | /// [`Self::edges`] for a true `bw`x`bh` block (lane-intradisp r1): |
| 16950 | /// the above/left reach each extend by their OWN axis (`bw` across, |
| 16951 | /// `bh` down), not one shared `side`. |
| 16952 | fn edges_rect( |
| 16953 | &self, |
| 16954 | x: usize, |
| 16955 | y: usize, |
| 16956 | bw: usize, |
| 16957 | bh: usize, |
| 16958 | reach: Reach, |
| 16959 | ) -> (Option<Vec<u16>>, Option<Vec<u16>>, Option<u16>) { |
| 16960 | let own_across = x + bw.min(self.true_width.saturating_sub(x)); |
| 16961 | let across = if reach.above_right { |
| 16962 | own_across + bw.min(self.true_width.saturating_sub(own_across)) |
| 16963 | } else { |
| 16964 | own_across |
| 16965 | } |
| 16966 | .min(self.width) |
| 16967 | .min(self.tile_x1); |
| 16968 | let own_down = y + bh.min(self.true_height.saturating_sub(y)); |
| 16969 | let down = if reach.below_left { |
| 16970 | own_down + bh.min(self.true_height.saturating_sub(own_down)) |
| 16971 | } else { |
| 16972 | own_down |
| 16973 | } |
| 16974 | .min(self.height) |
| 16975 | .min(self.tile_y1); |
| 16976 | let above = (y > self.tile_y0 && across > x) |
| 16977 | .then(|| self.data[(y - 1) * self.width + x..][..across - x].to_vec()); |
| 16978 | let left = (x > self.tile_x0 && down > y).then(|| { |
| 16979 | (y..down) |
| 16980 | .map(|row| self.data[row * self.width + x - 1]) |
| 16981 | .collect::<Vec<_>>() |
| 16982 | }); |
| 16983 | let corner = |
| 16984 | (x > self.tile_x0 && y > self.tile_y0).then(|| self.data[(y - 1) * self.width + x - 1]); |
| 16985 | if matches!((bw, bh), (16, 8) | (8, 16)) { |
| 16986 | hit!(RECT_STRIP_PRED_HITS); |
| 16987 | if across > own_across || down > own_down { |
| 16988 | hit!(RECT_STRIP_REACH_HITS); |
| 16989 | } |
| 16990 | } |
| 16991 | (above, left, corner) |
| 16992 | } |
| 16993 | |
| 16994 | /// [`Self::reconstruct`] for a true `bw`x`bh` block (lane-intradisp r1), |
| 16995 | /// via [`Self::edges_rect`] and [`crate::intra::predict`]'s own |
| 16996 | /// already-rect-capable `(bw, bh)` pair. |
| 16997 | #[allow(clippy::too_many_arguments)] |
| 16998 | #[track_caller] |
| 16999 | fn reconstruct_rect( |
| 17000 | &mut self, |
| 17001 | x: usize, |
| 17002 | y: usize, |
| 17003 | bw: usize, |
| 17004 | bh: usize, |
| 17005 | mode: usize, |
| 17006 | angle_delta: i32, |
| 17007 | reach: Reach, |
| 17008 | residual: &[i32], |
| 17009 | cfl: Option<(i32, &[i32])>, |
| 17010 | filter_intra: Option<usize>, |
| 17011 | smooth_neighbor: bool, |
| 17012 | // lane-wave1 step (i): the palette/intrabc prediction override, taken |
| 17013 | // off `FrameCtx::palette_pred` by the PUSH site (parse order) rather |
| 17014 | // than by this replay. |
| 17015 | pred: Option<Vec<u16>>, fctx: &crate::decode::FrameCtx, |
| 17016 | ) { |
| 17017 | // As [`Self::reconstruct`]'s own [`PALETTE_PRED`] override |
| 17018 | // (lane-palette2 r4): a palette block's prediction is the |
| 17019 | // reconstructed colour-index map, needing no edge pixels at all. |
| 17020 | let prediction = if let Some(buf) = pred { |
| 17021 | buf |
| 17022 | } else { |
| 17023 | let (above, left, corner) = self.edges_rect(x, y, bw, bh, reach); |
| 17024 | if crate::envflags::env_flag!("EC_DEBUG_EDGES") { |
| 17025 | eprintln!( |
| 17026 | "EDGES x={x} y={y} bw={bw} bh={bh} mode={mode} above={:?} left_len={:?} left0={:?} corner={corner:?} tx0={} ty0={} truew={} trueh={}", |
| 17027 | above.as_ref().map(|a| (a.len(), a[0])), |
| 17028 | left.as_ref().map(|l| l.len()), |
| 17029 | left.as_ref().map(|l| l[0]), |
| 17030 | self.tile_x0, self.tile_y0, self.true_width, self.true_height |
| 17031 | ); |
| 17032 | } |
| 17033 | let mut prediction = vec![0u16; bw * bh]; |
| 17034 | if let Some(fi_mode) = filter_intra { |
| 17035 | crate::intra::predict_filter_intra( |
| 17036 | fi_mode, |
| 17037 | above.as_deref(), |
| 17038 | left.as_deref(), |
| 17039 | corner, |
| 17040 | bw, |
| 17041 | bh, |
| 17042 | &mut prediction, fctx, |
| 17043 | ); |
| 17044 | } else { |
| 17045 | let enable_edge_filter = fctx.enable_edge_filter.with(std::cell::Cell::get); |
| 17046 | predict( |
| 17047 | mode as u8, |
| 17048 | angle_delta, |
| 17049 | above.as_deref(), |
| 17050 | left.as_deref(), |
| 17051 | corner, |
| 17052 | bw, |
| 17053 | bh, |
| 17054 | enable_edge_filter, |
| 17055 | smooth_neighbor, |
| 17056 | &mut prediction, fctx, |
| 17057 | ); |
| 17058 | } |
| 17059 | prediction |
| 17060 | }; |
| 17061 | // lane-hgkf r2: per-TU prediction trace, the twin of aomdec's |
| 17062 | // EC_PRED/EC_PREDND rungs (x/y are this plane's pixel origin). |
| 17063 | if crate::envflags::env_flag!("EC_PRED") { |
| 17064 | let sum: i64 = prediction.iter().map(|&v| i64::from(v)).sum(); |
| 17065 | let row0: Vec<u16> = prediction[..bw.min(8)].to_vec(); |
| 17066 | let col0: Vec<u16> = (0..bh.min(8)).map(|r| prediction[r * bw]).collect(); |
| 17067 | eprintln!( |
| 17068 | "@{} 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) |
| 17069 | ); |
| 17070 | } |
| 17071 | let residual = dense_residual(residual, bw * bh); |
| 17072 | if crate::envflags::env_flag!("EC_DBG192") && y == 168 && (x == 224 || x == 232) { |
| 17073 | let res0: Vec<i32> = residual[..bw.min(8)].to_vec(); |
| 17074 | let pred0: Vec<u16> = prediction[..bw.min(8)].to_vec(); |
| 17075 | eprintln!("DBG192 rect x={x} y={y} bw={bw} bh={bh} mode={mode} pred0={pred0:?} res0={res0:?}"); |
| 17076 | } |
| 17077 | for row in 0..bh { |
| 17078 | // Same bottom/right frame-edge clip as [`Self::reconstruct`]'s |
| 17079 | // (lane-sb128c r8) -- a 64x128 block at the right edge of a frame |
| 17080 | // whose padded width is not superblock aligned hangs past it. |
| 17081 | if y + row >= self.height { |
| 17082 | break; |
| 17083 | } |
| 17084 | for col in 0..bw { |
| 17085 | if x + col >= self.width { |
| 17086 | break; |
| 17087 | } |
| 17088 | let idx = row * bw + col; |
| 17089 | let mut base = i32::from(prediction[idx]); |
| 17090 | if let Some((alpha_q3, ac_q3)) = cfl { |
| 17091 | base = (base + cfl_scaled(alpha_q3, ac_q3[idx])).clamp(0, sample_max(fctx)); |
| 17092 | } |
| 17093 | let sample = (base + residual[idx]).clamp(0, sample_max(fctx)) as u16; |
| 17094 | self.data.to_mut()[(y + row) * self.width + x + col] = sample; |
| 17095 | } |
| 17096 | } |
| 17097 | // lane-d792: the RECONSTRUCTION rung beside `EC_PRED`'s prediction one |
| 17098 | // -- a block whose prediction matches the oracle's but whose written |
| 17099 | // samples do not is either a residual defect or a later overwrite, and |
| 17100 | // only this print separates the two. |
| 17101 | if crate::envflags::env_flag!("EC_PRED") { |
| 17102 | let row0: Vec<u16> = |
| 17103 | (0..bw.min(8)).map(|c| self.data[y * self.width + x + c]).collect(); |
| 17104 | let col0: Vec<u16> = |
| 17105 | (0..bh.min(8)).map(|r| self.data[(y + r) * self.width + x]).collect(); |
| 17106 | eprintln!("OUR_RECON x={x} y={y} bw={bw} bh={bh} row0={row0:?} col0={col0:?}"); |
| 17107 | } |
| 17108 | } |
| 17109 | |
| 17110 | /// Predicts under `mode` then adds `residual` (side*side, raster), |
| 17111 | /// writing the clamped reconstruction back into the plane at `(x, y)`. |
| 17112 | /// `cfl`, when `Some((alpha_q3, ac_q3))` (a `UV_CFL_PRED` chroma block), |
| 17113 | /// nudges the prediction by [`cfl_scaled`]'s per-sample amount before the |
| 17114 | /// residual is added — `av1_cfl_predict_block`'s own clip-then-add-residual |
| 17115 | /// order (cfl.c). |
| 17116 | #[track_caller] |
| 17117 | fn reconstruct( |
| 17118 | &mut self, |
| 17119 | x: usize, |
| 17120 | y: usize, |
| 17121 | side: usize, |
| 17122 | mode: usize, |
| 17123 | angle_delta: i32, |
| 17124 | reach: Reach, |
| 17125 | residual: &[i32], |
| 17126 | cfl: Option<(i32, &[i32])>, |
| 17127 | filter_intra: Option<usize>, |
| 17128 | // `smooth_neighbor` (spec `get_intra_edge_filter_type` / |
| 17129 | // libaom `get_filt_type`): true when the block's above OR left |
| 17130 | // neighbour's mode is `SMOOTH_PRED..=SMOOTH_H_PRED` (never `PAETH`). |
| 17131 | // Callers pass their own plane's neighbour-mode check |
| 17132 | // ([`is_smooth_mode`]) — chroma's is still hardcoded `false` by its |
| 17133 | // callers until a smooth/paeth `uv_mode` can reach this call at all |
| 17134 | // (still refused in `read_intra_mode`/`read_intra_mode_rect`), which |
| 17135 | // keeps `false` exact for chroma without this fn needing to know why. |
| 17136 | smooth_neighbor: bool, |
| 17137 | // lane-wave1 step (i): the palette/intrabc prediction override, taken |
| 17138 | // off `FrameCtx::palette_pred` by the PUSH site (parse order) rather |
| 17139 | // than by this replay. |
| 17140 | pred: Option<Vec<u16>>, fctx: &crate::decode::FrameCtx, |
| 17141 | ) { |
| 17142 | // A palette block's own [`PALETTE_PRED`] override (lane-palette r2) |
| 17143 | // takes priority over `predict()`/`predict_filter_intra` entirely -- |
| 17144 | // palette prediction needs no edge pixels at all. |
| 17145 | let prediction = if let Some(buf) = pred { |
| 17146 | buf |
| 17147 | } else { |
| 17148 | let (above, left, corner) = self.edges(x, y, side, reach); |
| 17149 | if crate::envflags::env_flag!("EC_DEBUG_EDGES") { |
| 17150 | eprintln!( |
| 17151 | "EDGES_SQ x={x} y={y} side={side} mode={mode} above={:?} left={:?} corner={corner:?}", |
| 17152 | above.as_deref(), |
| 17153 | left.as_deref(), |
| 17154 | ); |
| 17155 | } |
| 17156 | let mut prediction = vec![0u16; side * side]; |
| 17157 | if let Some(fi_mode) = filter_intra { |
| 17158 | crate::intra::predict_filter_intra( |
| 17159 | fi_mode, |
| 17160 | above.as_deref(), |
| 17161 | left.as_deref(), |
| 17162 | corner, |
| 17163 | side, |
| 17164 | side, |
| 17165 | &mut prediction, fctx, |
| 17166 | ); |
| 17167 | } else { |
| 17168 | // `enable_intra_edge_filter` is the sequence header's own flag |
| 17169 | // (`ENABLE_EDGE_FILTER`, set once per `decode_key_frame_tile_with_cdfs` |
| 17170 | // call, never per-block) -- threading it through every |
| 17171 | // `read_plane`/`decode_block`/`decode_leaf8` call would touch two |
| 17172 | // dozen sites for a value that never changes mid-decode, so it |
| 17173 | // rides a thread-local instead (this decoder is single-threaded |
| 17174 | // per stream). |
| 17175 | let enable_edge_filter = fctx.enable_edge_filter.with(std::cell::Cell::get); |
| 17176 | predict( |
| 17177 | mode as u8, |
| 17178 | angle_delta, |
| 17179 | above.as_deref(), |
| 17180 | left.as_deref(), |
| 17181 | corner, |
| 17182 | side, |
| 17183 | side, |
| 17184 | enable_edge_filter, |
| 17185 | smooth_neighbor, |
| 17186 | &mut prediction, fctx, |
| 17187 | ); |
| 17188 | } |
| 17189 | prediction |
| 17190 | }; |
| 17191 | // lane-hgkf r2: per-TU prediction trace, the twin of aomdec's |
| 17192 | // EC_PRED/EC_PREDND rungs (x/y are this plane's pixel origin). |
| 17193 | if crate::envflags::env_flag!("EC_PRED") { |
| 17194 | let sum: i64 = prediction.iter().map(|&v| i64::from(v)).sum(); |
| 17195 | let row0: Vec<u16> = prediction[..side.min(8)].to_vec(); |
| 17196 | let col0: Vec<u16> = (0..side.min(8)).map(|r| prediction[r * side]).collect(); |
| 17197 | eprintln!( |
| 17198 | "@{} 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) |
| 17199 | ); |
| 17200 | } |
| 17201 | let residual = dense_residual(residual, side * side); |
| 17202 | if crate::envflags::env_flag!("EC_DBG192") && y == 168 && (x == 224 || x == 232) { |
| 17203 | let res0: Vec<i32> = residual[..side.min(8)].to_vec(); |
| 17204 | let pred0: Vec<u16> = prediction[..side.min(8)].to_vec(); |
| 17205 | eprintln!("DBG192 sq x={x} y={y} side={side} mode={mode} cfl={} pred0={pred0:?} res0={res0:?}", cfl.is_some()); |
| 17206 | } |
| 17207 | for row in 0..side { |
| 17208 | // lane-sb128c r8: libaom's frame buffer is superblock |
| 17209 | // aligned; ours is padded to 32 (`block_grid`), so a 64x64 |
| 17210 | // transform unit of a 128x64 block at the BOTTOM frame edge |
| 17211 | // can hang past the last padded row (1920x792: a unit at |
| 17212 | // y=768 wants 832 rows of an 800-row buffer, which panicked |
| 17213 | // the film's key frames). Those samples are outside the |
| 17214 | // frame: never output, and no block below ever references |
| 17215 | // them. |
| 17216 | if y + row >= self.height { |
| 17217 | break; |
| 17218 | } |
| 17219 | for col in 0..side { |
| 17220 | if x + col >= self.width { |
| 17221 | break; |
| 17222 | } |
| 17223 | let idx = row * side + col; |
| 17224 | let mut base = i32::from(prediction[idx]); |
| 17225 | if let Some((alpha_q3, ac_q3)) = cfl { |
| 17226 | base = (base + cfl_scaled(alpha_q3, ac_q3[idx])).clamp(0, sample_max(fctx)); |
| 17227 | } |
| 17228 | let sample = (base + residual[idx]).clamp(0, sample_max(fctx)) as u16; |
| 17229 | self.data.to_mut()[(y + row) * self.width + x + col] = sample; |
| 17230 | } |
| 17231 | } |
| 17232 | } |
| 17233 | } |
| 17234 | |
| 17235 | /// Reads one plane's transform block, reconstructs it into `plane` at |
| 17236 | /// `(x, y)`/`side`, and records what it leaves behind in `neighbours`. |
| 17237 | #[allow(clippy::too_many_arguments)] |
| 17238 | fn read_plane( |
| 17239 | dec: &mut SymbolDecoder, |
| 17240 | cdfs: &mut Cdfs, |
| 17241 | set: TxbSet, |
| 17242 | scan: &[u16], |
| 17243 | plane_idx: usize, |
| 17244 | around: (bool, bool, i32), |
| 17245 | // The block's own luma mode — [`crate::tile::write_block_planes`] passes |
| 17246 | // it for every plane's `txb` lookup (it is what an intra transform type's |
| 17247 | // CDF is indexed by, spec 9.3), never the plane's own predicted mode. |
| 17248 | tx_mode: usize, |
| 17249 | predict_mode: usize, |
| 17250 | angle_delta: i32, |
| 17251 | reach: Reach, |
| 17252 | // lane-wave1 step (i): unread -- this reader records its write |
| 17253 | // ([`push_intra`]/[`push_mc`]) instead of performing it; the borrow |
| 17254 | // stays in the signature so every call site still proves the plane is |
| 17255 | // exclusively held while its coefficients are parsed. |
| 17256 | _plane: &mut PlaneBuf<'static>, |
| 17257 | x: usize, |
| 17258 | y: usize, |
| 17259 | side: usize, |
| 17260 | tx_side: usize, |
| 17261 | base_q_idx: u8, |
| 17262 | cfl: Option<(i32, CflSrc)>, |
| 17263 | filter_intra: Option<usize>, |
| 17264 | // Only meaningful for `plane_idx == 0`: `Some` when this transform unit is |
| 17265 | // smaller than its own block (spec `get_txb_ctx_general`'s |
| 17266 | // `plane_bsize != tx_size` branch), carrying the already-looked-up |
| 17267 | // `SKIP_CONTEXTS` value; `None` everywhere else (a lone luma TU, or any |
| 17268 | // chroma plane), where `txb_skip_ctx` is 0 or the usual OR-of-neighbours |
| 17269 | // formula below. |
| 17270 | luma_skip_ctx: Option<usize>, |
| 17271 | // See [`PlaneBuf::reconstruct`]'s own doc: caller's neighbour-smooth check. |
| 17272 | smooth_neighbor: bool, fctx: &crate::decode::FrameCtx, |
| 17273 | ) -> Result<Grid> { |
| 17274 | // lane-av1mono: a monochrome frame has `NumPlanes == 1`, so libaom's |
| 17275 | // coefficient loop (`for (plane = 0; plane < av1_num_planes(cm); ++plane)`) |
| 17276 | // never reaches a chroma plane -- no u/v coefficient symbol is coded and |
| 17277 | // no chroma write is recorded. Return the all-zero grid the caller's |
| 17278 | // neighbour bookkeeping reads, without consuming a bit. |
| 17279 | if plane_idx > 0 && mono(fctx) { |
| 17280 | return Ok(Grid::Zero(side * side)); |
| 17281 | } |
| 17282 | let skip_ctx = if plane_idx == 0 { |
| 17283 | luma_skip_ctx.unwrap_or(0) |
| 17284 | } else { |
| 17285 | // libaom `get_txb_ctx`: a chroma unit's context is |
| 17286 | // `combine_entropy_contexts(above, left)` plus 7, or plus 10 when the |
| 17287 | // plane block is bigger than the transform. Our chroma tables hold the |
| 17288 | // offset-7 rows first, so the caller adds 3 for the bigger-block case |
| 17289 | // (only a 128x128 block's TX_32X32 chroma units, lane-sb128b r3). |
| 17290 | usize::from(around.0) + usize::from(around.1) + luma_skip_ctx.unwrap_or(0) |
| 17291 | }; |
| 17292 | // spec 5.11.47/libaom `av1_read_tx_type`: a filter-intra luma block's |
| 17293 | // `intra_ext_tx_cdf` row is indexed by `fimode_to_intradir[filter_intra_mode]`, |
| 17294 | // not by the block's ordinary `mode` (which is always DC_PRED whenever |
| 17295 | // filter-intra is on) -- caller passes the raw filter-intra mode 0..4 in |
| 17296 | // `filter_intra`, only meaningful for `plane_idx == 0` (chroma has no |
| 17297 | // filter-intra in AV1). |
| 17298 | let tx_mode = if plane_idx == 0 { fi_tx_row(tx_mode, filter_intra) } else { tx_mode }; |
| 17299 | let mut coding = cdfs.txb(set, tx_mode); |
| 17300 | // libaom `read_tx_type` (`av1/decoder/decodetxb.c`): "No need to read |
| 17301 | // transform type for lossless mode(qindex==0)" -- the symbol is not coded |
| 17302 | // at all, and `av1_get_tx_type` (`blockd.h`) returns DCT_DCT for every |
| 17303 | // plane of a lossless block (the residual then goes through the WHT, so |
| 17304 | // the type only picks the scan, which is the default one). |
| 17305 | if lossless(fctx) { |
| 17306 | coding.tx_type = None; |
| 17307 | } |
| 17308 | // Luma's `default_tx_type` is only a fallback for the sizes whose |
| 17309 | // `TxbSet` carries no symbol at all (32-point and up), which the spec |
| 17310 | // fixes at `DCT_DCT` regardless of mode; a chroma plane's `tx_type` is |
| 17311 | // *never* its own coded symbol (`coding.tx_type` is always `None` for |
| 17312 | // every `TxbSet::Chroma*`), so it would take `Intra_Mode_To_Tx_Type` of |
| 17313 | // the plane's own predicted mode (`default_intra_tx_type`) -- except |
| 17314 | // `av1_get_ext_tx_set_type` (`blockd.h`) resolves chroma at `tx_size_sqr |
| 17315 | // >= TX_32X32` to `EXT_TX_SET_DCTONLY`, which forces the result back to |
| 17316 | // `DCT_DCT` no matter what the mode-indexed table said (`TxbSet::Chroma8` |
| 17317 | // and `Chroma16` never hit this — every value `default_intra_tx_type` can |
| 17318 | // produce is already a member of both `TX_SET_INTRA_1`/`_2`, so nothing |
| 17319 | // narrows there — only `Chroma32` and up do). |
| 17320 | let default_tx_type = if plane_idx == 0 || side >= 32 { |
| 17321 | TxType::DctDct |
| 17322 | } else if let Some(inherited) = fctx.intrabc_chroma_tx.with(std::cell::Cell::get) { |
| 17323 | // See [`INTRABC_CHROMA_TX`]: an intrabc block is an inter block for |
| 17324 | // `av1_get_tx_type`, so chroma inherits luma's coded type (the |
| 17325 | // `side >= 32` arm above is already libaom's `EXT_TX_SET_DCTONLY` |
| 17326 | // clamp at `tx_size_sqr_up >= TX_32X32`, matching |
| 17327 | // [`read_inter_plane`]'s own `side < 32` guard). |
| 17328 | inherited |
| 17329 | } else { |
| 17330 | default_intra_tx_type(predict_mode as u8) |
| 17331 | }; |
| 17332 | // libaom `av1_get_tx_type` (`blockd.h`): a lossless block's type is |
| 17333 | // DCT_DCT on EVERY plane, so chroma never inherits or mode-derives one. |
| 17334 | let default_tx_type = if lossless(fctx) { TxType::DctDct } else { default_tx_type }; |
| 17335 | let (grid, tx_type) = read_coeffs( |
| 17336 | dec, |
| 17337 | &mut coding, |
| 17338 | scan, |
| 17339 | skip_ctx, |
| 17340 | dc_sign_ctx(around.2), |
| 17341 | default_tx_type, |
| 17342 | None, |
| 17343 | )?; |
| 17344 | note_chroma_class1(plane_idx, tx_type); |
| 17345 | if plane_idx == 0 { |
| 17346 | fctx.luma_tx_type.with(|c| c.set(tx_type)); |
| 17347 | } |
| 17348 | // A 64x64 luma block's transform covers the whole 64x64 area, but only its |
| 17349 | // top-left 32x32 of frequencies are coded (spec 5.11.40); the rest of the |
| 17350 | // dequantized grid stays zero, which `inverse_transform_2d`'s own `< 32` |
| 17351 | // guard also assumes. |
| 17352 | // `grid` is `tx_side x tx_side` (the coded corner); `dequant_and_inverse` |
| 17353 | // wants the true `side x side` transform, with dqDenom (spec 7.12.3) |
| 17354 | // keyed by that true size, not the coded corner's -- so the corner |
| 17355 | // goes into the top-left of a `side`-sized grid, not the reverse. |
| 17356 | let levels = extend_corner(grid, tx_side, tx_side, side, side); |
| 17357 | let (dc_delta, ac_delta) = plane_q_delta(plane_idx, fctx); |
| 17358 | let tx = TxParams { |
| 17359 | w: side, h: side, bit_depth: bit_depth(fctx), q_idx: block_q_idx(fctx), dc_delta, |
| 17360 | ac_delta, tx_type, qm: block_iqmatrix(fctx, plane_idx, side, side, tx_type), stride: 0, |
| 17361 | lossless: lossless(fctx), |
| 17362 | }; |
| 17363 | if crate::envflags::env_flag!("EC_AV1_TRACE") { |
| 17364 | let residual = tx.run(&levels); |
| 17365 | eprintln!( |
| 17366 | "TRACE dequant plane={plane_idx} base_q_idx={base_q_idx} tx_type={tx_type:?} side={side} levels={:?} residual={residual:?}", &*levels, |
| 17367 | ); |
| 17368 | } |
| 17369 | push_intra_tx( |
| 17370 | plane_idx, |
| 17371 | x, |
| 17372 | y, |
| 17373 | side, |
| 17374 | side, |
| 17375 | false, |
| 17376 | predict_mode, |
| 17377 | angle_delta, |
| 17378 | reach, |
| 17379 | tx, |
| 17380 | &levels, |
| 17381 | cfl, |
| 17382 | filter_intra, |
| 17383 | smooth_neighbor, fctx, |
| 17384 | ); |
| 17385 | // `record`'s neighbour state only reads whether *the coded grid* (not the |
| 17386 | // residual) has nonzero entries, so the levels this function already |
| 17387 | // built are what it wants back -- both arms of the old `tx_side == side` |
| 17388 | // branch returned exactly that, one of them through a full `to_vec` copy |
| 17389 | // of the grid per intra transform unit (lane-coefbuf). |
| 17390 | Ok(levels) |
| 17391 | } |
| 17392 | |
| 17393 | /// [`push_intra`] when the unit is square, [`push_intra_rect`] when it is a |
| 17394 | /// rect (lane-av1-422f): a 4:2:2 square-block chroma unit is the plane |
| 17395 | /// block's own rect shape, and the `rect` flag selects |
| 17396 | /// [`PlaneBuf::reconstruct_rect`]'s two-dimensional loop. |
| 17397 | #[allow(clippy::too_many_arguments)] |
| 17398 | fn push_intra_unit( |
| 17399 | plane: usize, |
| 17400 | x: usize, |
| 17401 | y: usize, |
| 17402 | w: usize, |
| 17403 | h: usize, |
| 17404 | mode: usize, |
| 17405 | angle_delta: i32, |
| 17406 | reach: Reach, |
| 17407 | residual: &[i32], |
| 17408 | cfl: Option<(i32, CflSrc)>, |
| 17409 | filter_intra: Option<usize>, |
| 17410 | smooth_neighbor: bool, fctx: &crate::decode::FrameCtx, |
| 17411 | ) { |
| 17412 | if w == h { |
| 17413 | push_intra(plane, x, y, w, mode, angle_delta, reach, residual, cfl, |
| 17414 | filter_intra, smooth_neighbor, fctx); |
| 17415 | } else { |
| 17416 | push_intra_rect(plane, x, y, w, h, mode, angle_delta, reach, residual, cfl, |
| 17417 | filter_intra, smooth_neighbor, fctx); |
| 17418 | } |
| 17419 | } |
| 17420 | |
| 17421 | /// ONE RECT chroma transform unit of a square block's uv plane |
| 17422 | /// (lane-av1-422f) -- a 4:2:2 frame's plane block is `(side/2, side)` and |
| 17423 | /// `av1_get_max_uv_txsize` fills it with a single rect unit below side 64 |
| 17424 | /// (TX_4X8 / TX_8X16 / TX_16X32), which the square-shaped [`read_plane`] |
| 17425 | /// cannot express. Same shape as [`leaf8_chroma422_unit`]'s proven read: the |
| 17426 | /// [`TxbSet`] whose entropy class the unit squares up to, the unit-shaped |
| 17427 | /// scan, `txb_skip_ctx` = above + left in the offset-7 rows (the unit covers |
| 17428 | /// the plane block exactly, so no `+3` bigger-plane term -- that one lives in |
| 17429 | /// the square multi-unit walks' `chroma_ctx_offset`), and the plane's |
| 17430 | /// predicted mode supplies the mode-derived default `tx_type` unless an armed |
| 17431 | /// [`INTRABC_CHROMA_TX`] slot carries the intrabc luma's own -- except at an |
| 17432 | /// entropy class of 32x32 and up, where the spec forces `DCT_DCT` |
| 17433 | /// ([`read_plane`]'s `side >= 32` clamp). |
| 17434 | #[allow(clippy::too_many_arguments)] |
| 17435 | fn read_rect_chroma_unit( |
| 17436 | dec: &mut SymbolDecoder, |
| 17437 | cdfs: &mut Cdfs, |
| 17438 | set: TxbSet, |
| 17439 | scan: &[u16], |
| 17440 | w: usize, |
| 17441 | h: usize, |
| 17442 | plane_idx: usize, |
| 17443 | around: (bool, bool, i32), |
| 17444 | uv_predict_mode: usize, |
| 17445 | angle_delta_uv: i32, |
| 17446 | reach: Reach, |
| 17447 | // Unread, like [`read_plane`]'s own: the borrow proves the caller holds |
| 17448 | // the plane exclusively while the unit's coefficients are parsed. |
| 17449 | _plane: &mut PlaneBuf<'static>, |
| 17450 | x: usize, |
| 17451 | y: usize, |
| 17452 | // Signature parity with [`read_plane`]; the dequant reads its q index |
| 17453 | // off `block_q_idx(fctx)` like every other reader here. |
| 17454 | _base_q_idx: u8, |
| 17455 | // lane-av1-422p: `get_txb_ctx_general`'s chroma branch |
| 17456 | // (txb_common.h) reads `txb_skip_ctx = above + left + ctx_offset`, |
| 17457 | // where `ctx_offset` is 10 when the plane block is bigger than the |
| 17458 | // transform and 7 otherwise. The port stores the offset-7 rows at |
| 17459 | // `txb_skip_chroma_8` rows 0..3 and the offset-10 rows at rows 3..6 |
| 17460 | // (lane-av1-422b), so a unit SMALLER than its plane block passes |
| 17461 | // `Some(3)` to reach the offset-10 rows. Every unit that IS the whole |
| 17462 | // plane block stays `None` -- 4:2:2 plane blocks never shrink to a |
| 17463 | // rect unit except in the chunk arms. |
| 17464 | chroma_skip_offset: Option<usize>, |
| 17465 | cfl: Option<(i32, CflSrc)>, |
| 17466 | smooth_neighbor_uv: bool, fctx: &crate::decode::FrameCtx, |
| 17467 | ) -> Result<Grid> { |
| 17468 | if ss_x(fctx) == 1 && ss_y(fctx) == 0 && w != h { |
| 17469 | hit!(CHROMA422_RECT_HITS); |
| 17470 | } |
| 17471 | let default_tx = if w.max(h) >= 32 { |
| 17472 | TxType::DctDct |
| 17473 | } else { |
| 17474 | fctx |
| 17475 | .intrabc_chroma_tx |
| 17476 | .with(std::cell::Cell::get) |
| 17477 | .unwrap_or_else(|| default_intra_tx_type(uv_predict_mode as u8)) |
| 17478 | }; |
| 17479 | let mut coding = cdfs.txb(set, uv_predict_mode); |
| 17480 | let (levels, tx_type) = read_coeffs_rect( |
| 17481 | dec, &mut coding, scan, w, h, |
| 17482 | usize::from(around.0) + usize::from(around.1) + chroma_skip_offset.unwrap_or(0), |
| 17483 | dc_sign_ctx(around.2), default_tx, |
| 17484 | )?; |
| 17485 | let residual = dequant_and_inverse_typed_wh( |
| 17486 | &levels, w, h, crate::decode::bit_depth(fctx), block_q_idx(fctx), |
| 17487 | plane_q_delta(plane_idx, fctx).0, plane_q_delta(plane_idx, fctx).1, |
| 17488 | tx_type, block_iqmatrix(fctx, plane_idx, w, h, tx_type), |
| 17489 | ); |
| 17490 | push_intra_rect( |
| 17491 | plane_idx, x, y, w, h, uv_predict_mode, angle_delta_uv, reach, |
| 17492 | &residual, cfl, None, smooth_neighbor_uv, fctx, |
| 17493 | ); |
| 17494 | Ok(levels) |
| 17495 | } |
| 17496 | |
| 17497 | /// Decodes one whole-block coded superblock/quadrant/leaf: its mode, then its |
| 17498 | /// three planes, reconstructing them into `y`/`u`/`v` and updating |
| 17499 | /// `neighbours`. |
| 17500 | #[allow(clippy::too_many_arguments)] |
| 17501 | fn decode_block( |
| 17502 | dec: &mut SymbolDecoder, |
| 17503 | cdfs: &mut Cdfs, |
| 17504 | neighbours: &mut Neighbours, |
| 17505 | at: (usize, usize), |
| 17506 | side: usize, |
| 17507 | luma_set: TxbSet, |
| 17508 | chroma_set: TxbSet, |
| 17509 | luma_tx: usize, |
| 17510 | chroma_tx: usize, |
| 17511 | scans: (&[u16], &[u16]), |
| 17512 | cfl: bool, |
| 17513 | y: &mut PlaneBuf<'static>, |
| 17514 | u: &mut PlaneBuf<'static>, |
| 17515 | v: &mut PlaneBuf<'static>, |
| 17516 | base_q_idx: u8, |
| 17517 | enable_filter_intra: bool, |
| 17518 | allow_screen_content_tools: bool, |
| 17519 | allow_intrabc: bool, |
| 17520 | scan32: &[u16], |
| 17521 | scan16: &[u16], |
| 17522 | scan8: &[u16], |
| 17523 | scan4: &[u16], |
| 17524 | tx_select: bool, |
| 17525 | reduced_tx_set: bool, fctx: &crate::decode::FrameCtx, |
| 17526 | ) -> Result<()> { |
| 17527 | let (r, c) = at; |
| 17528 | let (px, py) = (c * SUB, r * SUB); |
| 17529 | let (palette_ctx, palette_cache) = neighbours.palette_ctx_and_cache(at); |
| 17530 | let palette_uv_cache = neighbours.palette_uv_cache(at); |
| 17531 | let (nb_above_mode, nb_left_mode) = neighbours.modes_above_left(r, c); |
| 17532 | let at_tile_left = c == neighbours.tile_col0_mi / (SUB / MI); |
| 17533 | let (skip, mode, angle_delta_y, uv_mode, angle_delta_uv, alpha, filter_intra, palette_y, palette_uv) = |
| 17534 | read_intra_mode( |
| 17535 | dec, |
| 17536 | cdfs, |
| 17537 | nb_above_mode, |
| 17538 | nb_left_mode, |
| 17539 | cfl, |
| 17540 | side, |
| 17541 | enable_filter_intra, |
| 17542 | neighbours.skip_txfm_ctx(r * (SUB / MI), c * (SUB / MI)), |
| 17543 | allow_screen_content_tools, |
| 17544 | allow_intrabc, |
| 17545 | Some((palette_ctx, &palette_cache)), |
| 17546 | &palette_uv_cache, |
| 17547 | r * (SUB / MI), |
| 17548 | c * (SUB / MI), fctx, |
| 17549 | )?; |
| 17550 | if at_tile_left && palette_y.is_some() { |
| 17551 | hit!(PALETTE_TILE_LEFT_HITS); |
| 17552 | } |
| 17553 | // lane-lossless: a lossless BLOCK's transform is TX_4X4 on every plane |
| 17554 | // (libaom `read_tx_size`, `decodeframe.c`), so the caller's per-size |
| 17555 | // tables/scans are replaced by the 4x4 ones and the block always takes the |
| 17556 | // multi-transform-unit path below. lane-av1mixloss: this rewrite used to |
| 17557 | // sit at the TOP of the function -- before `read_intra_mode`, which is |
| 17558 | // where the block's own `segment_id` is read -- so a mixed frame resolved |
| 17559 | // every block's geometry against the PREVIOUS block's segment (a lossless |
| 17560 | // neighbour forced 4x4 units on a lossy block; a lossy one handed the |
| 17561 | // lossless TxParams an 8x8 grid). libaom's order is the opposite one: |
| 17562 | // `decode_mbmi_block` (segment id included) runs before any tx-size rule. |
| 17563 | // lane-av1mixmerge: the 4:4:4 chroma-size arm from main's copy of this |
| 17564 | // rewrite (which sat above `read_intra_mode`) rides along here -- same |
| 17565 | // per-frame `ss_x`/`ss_y` condition, now resolved against the block's own |
| 17566 | // segment like the lossless arm above it. |
| 17567 | let (luma_set, chroma_set, luma_tx, chroma_tx, chroma_tx_h, scans) = if lossless(fctx) { |
| 17568 | (TxbSet::Luma4, TxbSet::Chroma4, 4, 4, 4, (scan4, scan4)) |
| 17569 | } else if ss_x(fctx) == 0 && ss_y(fctx) == 0 { |
| 17570 | let tx = if side >= 64 { 32 } else { side }; |
| 17571 | let (set, scan): (TxbSet, &[u16]) = match tx { |
| 17572 | 32 => (TxbSet::Chroma32, scan32), |
| 17573 | 16 => (TxbSet::Chroma16, scan16), |
| 17574 | 8 => (TxbSet::Chroma8, scan8), |
| 17575 | _ => (TxbSet::Chroma4, scan4), |
| 17576 | }; |
| 17577 | (luma_set, set, luma_tx, tx, tx, (scans.0, scan)) |
| 17578 | } else if ss_x(fctx) == 1 && ss_y(fctx) == 0 { |
| 17579 | // lane-av1-422f: the uv plane block is the (side/2, side) rect |
| 17580 | // (`ss_size_lookup[..][1][0]`), and `av1_get_max_uv_txsize` is |
| 17581 | // `av1_get_adjusted_tx_size(max_txsize_rect_lookup[plane_bsize])` -- |
| 17582 | // the RECT that fills the plane block, NOT the square transform of |
| 17583 | // the smaller side (the oracle reads ONE TX_8X16 for a 16x16 block's |
| 17584 | // 8x16 plane where the old model read two 8x8 units): 4 -> TX_4X4 |
| 17585 | // (the 2x4 plane rounds up to BLOCK_4X4), 8 -> TX_4X8, 16 -> |
| 17586 | // TX_8X16, 32 -> TX_16X32 (one unit per plane, the whole plane |
| 17587 | // block), 64 -> TX_32X64 adjusts to TX_32X32 and 128 -> TX_64X64 |
| 17588 | // adjusts to TX_32X32 (square units, a 1x2 / 2x4 grid). |
| 17589 | // `chroma_tx` is the unit WIDTH, `chroma_tx_h` its HEIGHT; the |
| 17590 | // chroma walks below read one (chroma_tx, chroma_tx_h) unit per grid |
| 17591 | // cell and key their rect reads on `chroma_tx != chroma_tx_h`. |
| 17592 | let (tx_w, tx_h) = match side { |
| 17593 | 4 => (4, 4), |
| 17594 | 8 => (4, 8), |
| 17595 | 16 => (8, 16), |
| 17596 | 32 => (16, 32), |
| 17597 | // 64 and 128: TX_32X64 / TX_64X64 adjust down to TX_32X32. |
| 17598 | _ => (32, 32), |
| 17599 | }; |
| 17600 | let (set, scan): (TxbSet, &[u16]) = match side { |
| 17601 | 4 => (TxbSet::Chroma4, scan4), |
| 17602 | 8 => (TxbSet::ChromaRect8x4, &SCAN_4X8[..]), |
| 17603 | 16 => (TxbSet::ChromaRect16x8, &SCAN_8X16[..]), |
| 17604 | 32 => (TxbSet::ChromaRect32x16, &SCAN_16X32[..]), |
| 17605 | _ => (TxbSet::Chroma32, scan32), |
| 17606 | }; |
| 17607 | (luma_set, set, luma_tx, tx_w, tx_h, (scans.0, scan)) |
| 17608 | } else { |
| 17609 | (luma_set, chroma_set, luma_tx, chroma_tx, chroma_tx, scans) |
| 17610 | }; |
| 17611 | let intrabc_dv = fctx.intrabc_dv.with(|c| c.take()); |
| 17612 | // `predict` panics outside `DC_PRED..=PAETH_PRED` (0..=12); `UV_CFL_PRED` |
| 17613 | // (13) predicts as `DC_PRED` with the [`cfl_scaled`] nudge carrying the |
| 17614 | // actual chroma-from-luma correlation, same as this decoder always did. |
| 17615 | let uv_predict_mode = if uv_mode == UV_CFL_PRED { |
| 17616 | DC_PRED |
| 17617 | } else { |
| 17618 | uv_mode |
| 17619 | }; |
| 17620 | let smooth_neighbor = is_smooth_mode(nb_above_mode) || is_smooth_mode(nb_left_mode); |
| 17621 | if smooth_neighbor { |
| 17622 | hit!(SMOOTH_LUMA_HITS); |
| 17623 | } |
| 17624 | let smooth_neighbor_uv = |
| 17625 | neighbours.smooth_uv_neighbour(r * (SUB / MI), c * (SUB / MI), r, c, fctx); |
| 17626 | if crate::envflags::env_flag!("EC_AV1_TRACE") { |
| 17627 | eprintln!( |
| 17628 | "TRACE block px={px} py={py} side={side} mode={mode} uv_mode={uv_mode} \ |
| 17629 | angle_delta_uv={angle_delta_uv}" |
| 17630 | ); |
| 17631 | } |
| 17632 | let (mi_r, mi_c) = (r * (SUB / MI), c * (SUB / MI)); |
| 17633 | // `TxMode::Select`'s `tx_depth` (spec 5.11.16): read for every intra |
| 17634 | // block, skipped or not. `logical_tx` is what the loop filter's edge |
| 17635 | // lookup and the next block's own `get_tx_size_context` want (the *nominal* |
| 17636 | // transform size -- 64 even for the 64x64 corner-scanned case, spec |
| 17637 | // 5.11.40); `coeff_tx_side` is the actual coded coefficient grid's side, |
| 17638 | // which only differs from it at that one corner case. |
| 17639 | // lane-t900 r32: spec 5.11.16 `read_block_tx_size` gates the `tx_depth` |
| 17640 | // symbol on `allowSelect = !skip || !is_inter`, and libaom's |
| 17641 | // `is_inter_block` counts an INTRABC block (`blockd.h:373`). So a SKIPPED |
| 17642 | // intrabc block reads NO tx-size symbol at all -- it takes |
| 17643 | // `maxRectTxSize` -- while every ordinary skipped intra block does read |
| 17644 | // one. Reading it anyway consumed a symbol libaom never wrote and desynced |
| 17645 | // the tile from that block on (class `equal-range-means-unread`). |
| 17646 | // lane-t900 r32: and an UNSKIPPED one reads the inter var-tx partition |
| 17647 | // tree instead (`read_var_tx_size`), the same syntax the inter block path |
| 17648 | // has always read -- libaom's `parse_decode_block` dispatches on |
| 17649 | // `is_inter_block`, so both branches of `read_block_tx_size` treat an |
| 17650 | // intrabc block as inter. |
| 17651 | let intrabc_skip_tx = intrabc_dv.is_some() && skip; |
| 17652 | // libaom `parse_decode_block`'s var-tx tree condition carries |
| 17653 | // `!xd->lossless[mbmi->segment_id]` (decodeframe.c:1237): an intrabc |
| 17654 | // block inside a LOSSLESS segment of a mixed frame reads NO |
| 17655 | // `txfm_partition` tree -- it falls to `read_tx_size`, whose first line |
| 17656 | // hands back TX_4X4. Invisible while every lossless frame was also |
| 17657 | // `CodedLossless` (`tx_mode` forced `Only4x4`, tree unreachable), which |
| 17658 | // is why the mixed frame is the first stream to reach it. |
| 17659 | let intrabc_vartx = intrabc_dv.is_some() && tx_select && !skip && !lossless(fctx); |
| 17660 | let intrabc_leaves = if intrabc_vartx { |
| 17661 | let max_tx = side.min(64); |
| 17662 | // `max_block_wide`/`max_block_high`: the frame's own mi grid |
| 17663 | // (`av1_calc_mi_size`, 8-px aligned), so a block hanging over the |
| 17664 | // right/bottom edge reads no split symbol for the units outside it. |
| 17665 | let mi_cols = 2 * y.true_width.div_ceil(8); |
| 17666 | let mi_rows = 2 * y.true_height.div_ceil(8); |
| 17667 | let side_mi = side / MI; |
| 17668 | let max_w_mi = side_mi.min(mi_cols.saturating_sub(mi_c)); |
| 17669 | let max_h_mi = side_mi.min(mi_rows.saturating_sub(mi_r)); |
| 17670 | let mut leaves = Vec::new(); |
| 17671 | for row in (0..side_mi).step_by(max_tx / MI) { |
| 17672 | for col in (0..side_mi).step_by(max_tx / MI) { |
| 17673 | read_var_tx_size( |
| 17674 | dec, cdfs, neighbours, (mi_r, mi_c), side, |
| 17675 | (max_w_mi, max_h_mi), (max_tx, max_tx), 0, row, col, &mut leaves, |
| 17676 | ); |
| 17677 | } |
| 17678 | } |
| 17679 | hit!(INTRABC_VARTX_HITS); |
| 17680 | // lane-t900 r33 census: the t900 recipes resolved every tree UNIFORM. |
| 17681 | // lane-av1-ibcvtx: a tree that resolved to mixed leaf sizes is no |
| 17682 | // longer refused -- the luma reconstruct below walks the tree's own |
| 17683 | // leaf list per leaf (libaom `decode_token_recon_block`, whose token |
| 17684 | // loop reconstructs whatever `read_tx_size_vartx` resolved, per |
| 17685 | // transform unit), so mixed sizes reconstruct at their own stride. |
| 17686 | // The counter marks the shape so a gate can prove a witness actually |
| 17687 | // carries a mixed tree and not merely another uniform one. |
| 17688 | let uniform = leaves |
| 17689 | .first() |
| 17690 | .is_some_and(|f| leaves.iter().all(|l| (l.2, l.3) == (f.2, f.3) && l.2 == l.3)); |
| 17691 | if !uniform { |
| 17692 | hit!(INTRABC_VARTX_MIXED_LEAVES_HITS); |
| 17693 | if crate::envflags::env_flag!("EC_AV1_IBCVTX_DEBUG") { |
| 17694 | eprintln!( |
| 17695 | "IBCVTX mixed intrabc tree mi=({mi_r},{mi_c}) side={side} \ |
| 17696 | dv={intrabc_dv:?} leaves={leaves:?}" |
| 17697 | ); |
| 17698 | } |
| 17699 | } |
| 17700 | Some(leaves) |
| 17701 | } else { |
| 17702 | None |
| 17703 | }; |
| 17704 | let (logical_tx, coeff_tx_side) = if let Some(tx) = intrabc_leaves.as_ref().map(|l| l[0].2) { |
| 17705 | (tx, if tx == 64 { 32 } else { tx }) |
| 17706 | } else if tx_select && !intrabc_skip_tx { |
| 17707 | let resolved = read_tx_size(dec, cdfs, neighbours, (mi_r, mi_c), side, fctx); |
| 17708 | (resolved, if resolved == 64 { 32 } else { resolved }) |
| 17709 | } else { |
| 17710 | // lane-sb128b r1: `max_txsize_rect_lookup[BLOCK_128X128]` is |
| 17711 | // TX_64X64, so a 128x128 block is always FOUR luma transform units |
| 17712 | // even with `tx_select` off -- the TU loop below, never the |
| 17713 | // single-unit path. |
| 17714 | (lossless_tx(side.min(64), fctx), luma_tx) |
| 17715 | }; |
| 17716 | if tx_select && logical_tx < side && uv_mode != DC_PRED { |
| 17717 | hit!(SQ_CHROMA_TX_HITS); |
| 17718 | } |
| 17719 | let luma_set = if tx_select { |
| 17720 | txbset_for(logical_tx, reduced_tx_set) |
| 17721 | } else { |
| 17722 | luma_set |
| 17723 | }; |
| 17724 | let luma_set = if intrabc_dv.is_some() { |
| 17725 | inter_txbset_for(logical_tx, reduced_tx_set) |
| 17726 | } else { |
| 17727 | luma_set |
| 17728 | }; |
| 17729 | let luma_scan = scan_for(coeff_tx_side, scan32, scan16, scan8, scan4); |
| 17730 | let reach = Reach::of(side, px, py, y.width, y.height, fctx); |
| 17731 | let (cpx, cpy) = (px >> ss_x(fctx), py >> ss_y(fctx)); |
| 17732 | let chroma_side = side >> ss_x(fctx); |
| 17733 | // lane-av1-422b: chroma extent per axis, `side` at ss_y 0 (4:2:2/4:4:4), |
| 17734 | // `side / 2` at 4:2:0. |
| 17735 | let chroma_height = side >> ss_y(fctx); |
| 17736 | // spec 7.11.3.4 with `use_intrabc`: the reference is the *current* |
| 17737 | // frame's own pre-loop-filter reconstruction (all loop filters are off |
| 17738 | // whenever `allow_intrabc`, frame.rs:194/210/252/272) at the full-pel |
| 17739 | // block vector, with the BILINEAR kernel libaom forces for intrabc -- |
| 17740 | // luma is a straight copy, chroma lands half-pel whenever the luma DV is |
| 17741 | // odd, which is exactly what the bilinear taps then interpolate. |
| 17742 | flush_recon(fctx, y, u, v); |
| 17743 | let intrabc_bufs = intrabc_dv.map(|(dv_row, dv_col)| { |
| 17744 | let mut yb = vec![0u16; side * side]; |
| 17745 | mc::predict_with_filter( |
| 17746 | &y.data, y.width, y.true_width, y.true_height, |
| 17747 | mv_to_q4(px, dv_col, 0), mv_to_q4(py, dv_row, 0), |
| 17748 | side, side, mc::InterpFilterKind::Bilinear, &mut yb, fctx, |
| 17749 | ); |
| 17750 | let mut ub = vec![0u16; chroma_side * chroma_height]; |
| 17751 | mc::predict_with_filter( |
| 17752 | &u.data, u.width, u.true_width, u.true_height, |
| 17753 | mv_to_q4(cpx, dv_col, ss_x(fctx)), mv_to_q4(cpy, dv_row, ss_y(fctx)), |
| 17754 | chroma_side, chroma_height, mc::InterpFilterKind::Bilinear, &mut ub, fctx, |
| 17755 | ); |
| 17756 | let mut vb = vec![0u16; chroma_side * chroma_height]; |
| 17757 | mc::predict_with_filter( |
| 17758 | &v.data, v.width, v.true_width, v.true_height, |
| 17759 | mv_to_q4(cpx, dv_col, ss_x(fctx)), mv_to_q4(cpy, dv_row, ss_y(fctx)), |
| 17760 | chroma_side, chroma_height, mc::InterpFilterKind::Bilinear, &mut vb, fctx, |
| 17761 | ); |
| 17762 | (yb, ub, vb) |
| 17763 | }); |
| 17764 | // A palette-Y block's own prediction is the reconstructed colour-index |
| 17765 | // map, not `predict()`'s edge-based one -- [`PALETTE_PRED`] carries it |
| 17766 | // to [`PlaneBuf::reconstruct`]'s next call rather than threading a |
| 17767 | // parameter through this function's dozen `y.reconstruct`/`read_plane` |
| 17768 | // sites. A split luma transform (`tx_select && logical_tx != side`) |
| 17769 | // would need the map sliced per-TU -- untested against a real |
| 17770 | // `--enable-palette=1` stream this round, so refuse by name rather than |
| 17771 | // guess at the slicing (charter's own named-acceptable fallback). |
| 17772 | let palette_y_buf: Option<Vec<u16>> = palette_y.as_ref().map(|py| { |
| 17773 | py.map |
| 17774 | .iter() |
| 17775 | .map(|&idx| py.colors[idx as usize]) |
| 17776 | .collect() |
| 17777 | }); |
| 17778 | // lane-t900 r32: the intrabc luma prediction rides the very same |
| 17779 | // whole-block override slot, so the multi-TU loop below windows it per |
| 17780 | // transform unit exactly like a palette map -- setting it once up front |
| 17781 | // handed the whole `side`x`side` buffer to the FIRST unit and left every |
| 17782 | // later one predicting off block edges (the var-tx tree above is what |
| 17783 | // first makes `logical_tx < side` reachable for an intrabc block). |
| 17784 | let palette_y_buf = match &intrabc_bufs { |
| 17785 | Some((yb, _, _)) => Some(yb.clone()), |
| 17786 | None => palette_y_buf, |
| 17787 | }; |
| 17788 | // With one luma transform unit the whole `side`x`side` map is the |
| 17789 | // prediction; with several (`logical_tx != side`) each unit takes its own |
| 17790 | // `logical_tx`-sized window of that same map, set fresh inside the TU loop |
| 17791 | // below (lane-palette2 r7) -- setting the full-block buffer here would be |
| 17792 | // taken by the FIRST unit and indexed at the wrong stride. |
| 17793 | // lane-svt1: a SKIPPED block predicts the whole `side`x`side` in ONE call |
| 17794 | // (the `else` arm below -- a palette/intrabc block never takes the per-unit |
| 17795 | // `split_tx_skip` walk), so its map must be armed here even when the |
| 17796 | // transform is split. Gating the arm on `logical_tx == side` alone left a |
| 17797 | // 16x16 palette block with `tx_depth == 1` and `skip == 1` predicting DC |
| 17798 | // off its own edges: right symbols, wrong pixels (SVT-AV1 preset-8 screen |
| 17799 | // key frame; class override-slot-on-one-arm). Neither prediction is |
| 17800 | // edge-derived, so the whole-block buffer at the block's own stride is |
| 17801 | // exactly what that single call wants. |
| 17802 | // Counted on the SHAPE, not on the arm below, so a gate can tell a stream |
| 17803 | // that reaches this block apart from one that never does (class |
| 17804 | // gate-blind-to-feature). |
| 17805 | if palette_y_buf.is_some() && skip && logical_tx < side { |
| 17806 | hit!(SKIP_SPLIT_TX_OVERRIDE_HITS); |
| 17807 | } |
| 17808 | if let Some(buf) = &palette_y_buf |
| 17809 | && (logical_tx == side || skip) |
| 17810 | { |
| 17811 | set_palette_pred(buf.clone(), fctx); |
| 17812 | } |
| 17813 | // A UV-palette block's own chroma prediction override (lane-palette2 r1), |
| 17814 | // same [`PALETTE_PRED`] slot as Y above -- set fresh right before each |
| 17815 | // plane's own `reconstruct`/`read_plane` call below (never both planes at |
| 17816 | // once), since Y's own set-then-take already emptied the slot by the time |
| 17817 | // either of these run. |
| 17818 | let palette_uv_bufs: Option<(Vec<u16>, Vec<u16>)> = palette_uv.as_ref().map(|puv| { |
| 17819 | ( |
| 17820 | puv.map.iter().map(|&idx| puv.u_colors[idx as usize]).collect(), |
| 17821 | puv.map.iter().map(|&idx| puv.v_colors[idx as usize]).collect(), |
| 17822 | ) |
| 17823 | }); |
| 17824 | // The chroma half of the intrabc prediction rides the same per-plane |
| 17825 | // override slot the UV-palette buffers do (set fresh before each plane's |
| 17826 | // own `reconstruct`/`read_plane` call below). |
| 17827 | let palette_uv_bufs = match &intrabc_bufs { |
| 17828 | Some((_, ub, vb)) => Some((ub.clone(), vb.clone())), |
| 17829 | None => palette_uv_bufs, |
| 17830 | }; |
| 17831 | if skip { |
| 17832 | // A skipped block codes no residual syntax at all (spec 5.11.34): |
| 17833 | // straight prediction, on every plane. |
| 17834 | // |
| 17835 | // lane-hgkf r2: an intra block's `tx_depth` is read whether or not it |
| 17836 | // is skipped (only an INTER skip forces the largest transform), and |
| 17837 | // spec 7.11.2 / libaom `predict_and_reconstruct_intra_block` predicts |
| 17838 | // ONE TRANSFORM UNIT AT A TIME -- each unit's edges are the units |
| 17839 | // already written left/above of it inside this same block. Predicting |
| 17840 | // the whole `side`x`side` at once gives a different (uniformly off by |
| 17841 | // ~1) answer, which is exactly the residue this lane was chasing. |
| 17842 | // The palette/intrabc override path keeps the single-call shape (its |
| 17843 | // per-TU windowing is the non-skip loop's own `palette_y_buf` slice, |
| 17844 | // untested for skip and unreachable in these streams). |
| 17845 | // lane-sb128b r2: a 128x128 block's transform is TX_64X64 even with |
| 17846 | // `tx_select` off (`max_txsize_rect_lookup[BLOCK_128X128]`), so the |
| 17847 | // per-unit prediction is keyed on the transform being smaller than |
| 17848 | // the block, not on the tx-depth syntax. The counter keeps lane-hgkf's |
| 17849 | // narrower meaning (a READ split tx depth). |
| 17850 | let split_tx_skip = |
| 17851 | logical_tx < side && palette_y_buf.is_none() && intrabc_bufs.is_none(); |
| 17852 | if split_tx_skip { |
| 17853 | if tx_select { |
| 17854 | hit!(SKIP_SPLIT_TX_HITS); |
| 17855 | } |
| 17856 | let n_axis = side / logical_tx; |
| 17857 | for tu_row in 0..n_axis { |
| 17858 | for tu_col in 0..n_axis { |
| 17859 | let tu_px = px + tu_col * logical_tx; |
| 17860 | let tu_py = py + tu_row * logical_tx; |
| 17861 | if tu_px >= y.true_width || tu_py >= y.true_height { |
| 17862 | continue; |
| 17863 | } |
| 17864 | let tu_reach = tu_reach( |
| 17865 | side, |
| 17866 | side, |
| 17867 | tu_col * logical_tx, |
| 17868 | tu_row * logical_tx, |
| 17869 | logical_tx, |
| 17870 | reach, |
| 17871 | px, |
| 17872 | py, |
| 17873 | y.width, |
| 17874 | y.height, fctx, |
| 17875 | ); |
| 17876 | push_intra(0, |
| 17877 | tu_px, |
| 17878 | tu_py, |
| 17879 | logical_tx, |
| 17880 | mode, |
| 17881 | angle_delta_y, |
| 17882 | tu_reach, |
| 17883 | &ZERO_RESIDUAL[..logical_tx * logical_tx], |
| 17884 | None, |
| 17885 | filter_intra, |
| 17886 | smooth_neighbor, fctx, |
| 17887 | ); |
| 17888 | } |
| 17889 | } |
| 17890 | } else { |
| 17891 | push_intra(0, |
| 17892 | px, |
| 17893 | py, |
| 17894 | side, |
| 17895 | mode, |
| 17896 | angle_delta_y, |
| 17897 | reach, |
| 17898 | &ZERO_RESIDUAL[..side * side], |
| 17899 | None, |
| 17900 | filter_intra, |
| 17901 | smooth_neighbor, fctx, |
| 17902 | ); |
| 17903 | } |
| 17904 | let ac = alpha.map(|_| cfl_src(px, py, side)); |
| 17905 | // lane-av1-422b: the chroma unit grid is `cn_cols` x `cn_rows` -- |
| 17906 | // square at 4:2:0/4:4:4, at 4:2:2 `(side/2)/tx` units wide by |
| 17907 | // `side/tx` tall (the plane block is (side/2, side)). A unit's LUMA |
| 17908 | // anchor follows the per-axis shifts: `chroma_tx << ss_x` wide, |
| 17909 | // `chroma_tx_h << ss_y` tall (lane-av1-422f: the unit itself is the |
| 17910 | // (chroma_tx, chroma_tx_h) rect, so a 4:2:2 frame below side 64 |
| 17911 | // reads ONE unit per plane, the whole plane block). |
| 17912 | let cn_cols = (chroma_side / chroma_tx).max(1); |
| 17913 | let cn_rows = (chroma_height / chroma_tx_h).max(1); |
| 17914 | let luma_span_x = chroma_tx << ss_x(fctx); |
| 17915 | let luma_span_y = chroma_tx_h << ss_y(fctx); |
| 17916 | for cu_row in 0..cn_rows { |
| 17917 | for cu_col in 0..cn_cols { |
| 17918 | let cu_r = if cn_rows == 1 && cn_cols == 1 { |
| 17919 | reach |
| 17920 | } else { |
| 17921 | tu_reach_rect( |
| 17922 | side, side, |
| 17923 | cu_col * luma_span_x, cu_row * luma_span_y, luma_span_x, luma_span_y, |
| 17924 | reach, px, py, y.width, y.height, fctx, |
| 17925 | ) |
| 17926 | }; |
| 17927 | let (cu_x, cu_y) = (cpx + cu_col * chroma_tx, cpy + cu_row * chroma_tx_h); |
| 17928 | if let Some((ub, _)) = &palette_uv_bufs { |
| 17929 | set_palette_pred(ub.clone(), fctx); |
| 17930 | } |
| 17931 | push_intra_unit(1, |
| 17932 | cu_x, |
| 17933 | cu_y, |
| 17934 | chroma_tx, |
| 17935 | chroma_tx_h, |
| 17936 | uv_predict_mode, |
| 17937 | angle_delta_uv, |
| 17938 | cu_r, |
| 17939 | &ZERO_RESIDUAL[..chroma_tx * chroma_tx_h], |
| 17940 | alpha.zip(ac).map(|((au, _), ac)| (au, ac)), |
| 17941 | None, |
| 17942 | smooth_neighbor_uv, fctx, |
| 17943 | ); |
| 17944 | if let Some((_, vb)) = &palette_uv_bufs { |
| 17945 | set_palette_pred(vb.clone(), fctx); |
| 17946 | } |
| 17947 | push_intra_unit(2, |
| 17948 | cu_x, |
| 17949 | cu_y, |
| 17950 | chroma_tx, |
| 17951 | chroma_tx_h, |
| 17952 | uv_predict_mode, |
| 17953 | angle_delta_uv, |
| 17954 | cu_r, |
| 17955 | &ZERO_RESIDUAL[..chroma_tx * chroma_tx_h], |
| 17956 | alpha.zip(ac).map(|((_, av), ac)| (av, ac)), |
| 17957 | None, |
| 17958 | smooth_neighbor_uv, fctx, |
| 17959 | ); |
| 17960 | } |
| 17961 | } |
| 17962 | let luma_grid = vec![0i32; side * side]; |
| 17963 | let u_grid = vec![0i32; chroma_side * chroma_side]; |
| 17964 | let v_grid = vec![0i32; chroma_side * chroma_side]; |
| 17965 | neighbours.record(at, side, mode, uv_predict_mode, &[&luma_grid[..], &u_grid[..], &v_grid[..]]); |
| 17966 | } else if logical_tx == side { |
| 17967 | // lane-sb128b r1: the test used to be `!tx_select || logical_tx == side`. |
| 17968 | // Without `tx_select` `logical_tx` IS `side` at every size <= 64, so |
| 17969 | // that arm was redundant -- and wrong at 128, where |
| 17970 | // `max_txsize_rect_lookup` caps the block's transform at 64 and the |
| 17971 | // block is four units whether or not `tx_select` is on. |
| 17972 | // A single luma transform unit -- the old path, unchanged (including |
| 17973 | // the 64x64 corner case, `logical_tx == side == 64` with |
| 17974 | // `coeff_tx_side == 32`). |
| 17975 | let around = neighbours.around(at, side); |
| 17976 | let luma_grid = read_plane( |
| 17977 | dec, |
| 17978 | cdfs, |
| 17979 | luma_set, |
| 17980 | luma_scan, |
| 17981 | 0, |
| 17982 | around[0], |
| 17983 | mode, |
| 17984 | mode, |
| 17985 | angle_delta_y, |
| 17986 | reach, |
| 17987 | y, |
| 17988 | px, |
| 17989 | py, |
| 17990 | side, |
| 17991 | coeff_tx_side, |
| 17992 | base_q_idx, |
| 17993 | None, |
| 17994 | filter_intra, |
| 17995 | None, |
| 17996 | smooth_neighbor, fctx, |
| 17997 | )?; |
| 17998 | let ac = alpha.map(|_| cfl_src(px, py, side)); |
| 17999 | // Armed for exactly this block's two chroma reads (see |
| 18000 | // [`INTRABC_CHROMA_TX`]); the luma call above just set |
| 18001 | // [`LUMA_TX_TYPE`] to the type it coded. |
| 18002 | if intrabc_dv.is_some() { |
| 18003 | fctx.intrabc_chroma_tx.with(|c| c.set(Some(fctx.luma_tx_type.with(std::cell::Cell::get)))); |
| 18004 | } |
| 18005 | // lane-av1-444: this arm is one luma TU (`logical_tx == side`), including |
| 18006 | // the 64x64 case whose coefficient grid is 32. At 4:4:4 the chroma plane |
| 18007 | // is the same size as luma and `av1_get_max_uv_txsize` caps it at |
| 18008 | // TX_32X32, so a 64x64 block codes four chroma units, plane-major |
| 18009 | // (every U, then every V) — the same loop the multi-TU arm runs. |
| 18010 | // At 4:2:0 `chroma_side == chroma_tx`, so it stays one read. |
| 18011 | // lane-av1-422b: 4:2:2 never collapses to one read above a 4x4 luma |
| 18012 | // block -- the unit grid is `(side/2)/tx` wide by `side/tx` tall. |
| 18013 | // lane-av1-422f: `chroma_tx` is the unit WIDTH and `chroma_tx_h` its |
| 18014 | // HEIGHT, so below side 64 a 4:2:2 frame's grid is 1x1 and the one |
| 18015 | // unit is the plane block's own rect. |
| 18016 | let cn_cols = (chroma_side / chroma_tx).max(1); |
| 18017 | let cn_rows = (chroma_height / chroma_tx_h).max(1); |
| 18018 | let luma_span_x = chroma_tx << ss_x(fctx); |
| 18019 | let luma_span_y = chroma_tx_h << ss_y(fctx); |
| 18020 | let (u_grid, v_grid, chroma_units) = if cn_rows == 1 && cn_cols == 1 { |
| 18021 | if chroma_tx == chroma_tx_h { |
| 18022 | if let Some((ub, _)) = &palette_uv_bufs { |
| 18023 | set_palette_pred(ub.clone(), fctx); |
| 18024 | } |
| 18025 | let u_grid = read_plane( |
| 18026 | dec, |
| 18027 | cdfs, |
| 18028 | chroma_set, |
| 18029 | scans.1, |
| 18030 | 1, |
| 18031 | around[1], |
| 18032 | mode, |
| 18033 | uv_predict_mode, |
| 18034 | angle_delta_uv, |
| 18035 | reach, |
| 18036 | u, |
| 18037 | cpx, |
| 18038 | cpy, |
| 18039 | chroma_side, |
| 18040 | chroma_tx, |
| 18041 | base_q_idx, |
| 18042 | alpha.zip(ac).map(|((au, _), ac)| (au, ac)), |
| 18043 | None, |
| 18044 | None, |
| 18045 | smooth_neighbor_uv, fctx, |
| 18046 | )?; |
| 18047 | if crate::envflags::env_flag!("EC_AV1_TRACE") { |
| 18048 | eprintln!( |
| 18049 | "TRACE u-write cpx={cpx} cpy={cpy} chroma_side={chroma_side} row0={:?}", |
| 18050 | &u.data[cpy * u.width + cpx..][..chroma_side] |
| 18051 | ); |
| 18052 | } |
| 18053 | if let Some((_, vb)) = &palette_uv_bufs { |
| 18054 | set_palette_pred(vb.clone(), fctx); |
| 18055 | } |
| 18056 | let v_grid = read_plane( |
| 18057 | dec, |
| 18058 | cdfs, |
| 18059 | chroma_set, |
| 18060 | scans.1, |
| 18061 | 2, |
| 18062 | around[2], |
| 18063 | mode, |
| 18064 | uv_predict_mode, |
| 18065 | angle_delta_uv, |
| 18066 | reach, |
| 18067 | v, |
| 18068 | cpx, |
| 18069 | cpy, |
| 18070 | chroma_side, |
| 18071 | chroma_tx, |
| 18072 | base_q_idx, |
| 18073 | alpha.zip(ac).map(|((_, av), ac)| (av, ac)), |
| 18074 | None, |
| 18075 | None, |
| 18076 | smooth_neighbor_uv, fctx, |
| 18077 | )?; |
| 18078 | (u_grid, v_grid, Vec::new()) |
| 18079 | } else { |
| 18080 | // lane-av1-422f: the one unit is a RECT filling the plane |
| 18081 | // block -- the square-shaped [`read_plane`] cannot express |
| 18082 | // it, so both planes go through [`read_rect_chroma_unit`] |
| 18083 | // (U, then V, the same order the square arm reads). The |
| 18084 | // chroma context reads per chroma 4-px cell (see |
| 18085 | // [`Neighbours::around_mi_422_chroma`]). |
| 18086 | let chroma_around = |
| 18087 | neighbours.around_mi_422_chroma((mi_r, mi_c), side, side); |
| 18088 | if let Some((ub, _)) = &palette_uv_bufs { |
| 18089 | set_palette_pred(ub.clone(), fctx); |
| 18090 | } |
| 18091 | let u_grid = read_rect_chroma_unit( |
| 18092 | dec, cdfs, chroma_set, scans.1, chroma_tx, chroma_tx_h, 1, |
| 18093 | chroma_around[1], uv_predict_mode, angle_delta_uv, reach, u, |
| 18094 | cpx, cpy, base_q_idx, None, |
| 18095 | alpha.zip(ac).map(|((au, _), ac)| (au, ac)), |
| 18096 | smooth_neighbor_uv, fctx, |
| 18097 | )?; |
| 18098 | if let Some((_, vb)) = &palette_uv_bufs { |
| 18099 | set_palette_pred(vb.clone(), fctx); |
| 18100 | } |
| 18101 | let v_grid = read_rect_chroma_unit( |
| 18102 | dec, cdfs, chroma_set, scans.1, chroma_tx, chroma_tx_h, 2, |
| 18103 | chroma_around[2], uv_predict_mode, angle_delta_uv, reach, v, |
| 18104 | cpx, cpy, base_q_idx, None, |
| 18105 | alpha.zip(ac).map(|((_, av), ac)| (av, ac)), |
| 18106 | smooth_neighbor_uv, fctx, |
| 18107 | )?; |
| 18108 | (u_grid, v_grid, Vec::new()) |
| 18109 | } |
| 18110 | } else { |
| 18111 | let chroma_ctx_offset = Some(3); |
| 18112 | let mut u_pass: Vec<Grid> = Vec::new(); |
| 18113 | let mut units: Vec<((usize, usize), usize, usize, Grid, Grid)> = Vec::new(); |
| 18114 | let mut last_grids = [Grid::Zero(0), Grid::Zero(0)]; |
| 18115 | for plane_pass in 1..=2usize { |
| 18116 | let mut pass_idx = 0usize; |
| 18117 | for cu_row in 0..cn_rows { |
| 18118 | for cu_col in 0..cn_cols { |
| 18119 | let cu_mi = ( |
| 18120 | mi_r + cu_row * (luma_span_y / MI), |
| 18121 | mi_c + cu_col * (luma_span_x / MI), |
| 18122 | ); |
| 18123 | let cu_around = if ss_x(fctx) == 1 && ss_y(fctx) == 0 { |
| 18124 | // lane-av1-422f: chroma-only loop; per-chroma-cell |
| 18125 | // above votes (see [`Neighbours::around_mi_422_chroma`]). |
| 18126 | neighbours.around_mi_422_chroma(cu_mi, luma_span_x, luma_span_y) |
| 18127 | } else { |
| 18128 | neighbours.around_mi_rect(cu_mi, luma_span_x, luma_span_y) |
| 18129 | }; |
| 18130 | let cu_reach = tu_reach_rect( |
| 18131 | side, |
| 18132 | side, |
| 18133 | cu_col * luma_span_x, |
| 18134 | cu_row * luma_span_y, |
| 18135 | luma_span_x, |
| 18136 | luma_span_y, |
| 18137 | reach, |
| 18138 | px, |
| 18139 | py, |
| 18140 | y.width, |
| 18141 | y.height, fctx, |
| 18142 | ); |
| 18143 | let (cu_x, cu_y) = |
| 18144 | (cpx + cu_col * chroma_tx, cpy + cu_row * chroma_tx_h); |
| 18145 | if let Some((ub, vb)) = &palette_uv_bufs { |
| 18146 | let pbuf = if plane_pass == 1 { ub } else { vb }; |
| 18147 | set_palette_pred( |
| 18148 | palette_window( |
| 18149 | pbuf, chroma_side, cu_col * chroma_tx, |
| 18150 | cu_row * chroma_tx_h, chroma_tx, chroma_tx_h, |
| 18151 | ), |
| 18152 | fctx, |
| 18153 | ); |
| 18154 | } |
| 18155 | let buf: &mut PlaneBuf<'static> = |
| 18156 | if plane_pass == 1 { &mut *u } else { &mut *v }; |
| 18157 | let cu_grid = read_plane( |
| 18158 | dec, cdfs, chroma_set, scans.1, plane_pass, cu_around[plane_pass], mode, |
| 18159 | uv_predict_mode, angle_delta_uv, cu_reach, buf, cu_x, cu_y, |
| 18160 | chroma_tx, chroma_tx, base_q_idx, |
| 18161 | alpha.zip(ac).map(|((au, av), ac)| { |
| 18162 | (if plane_pass == 1 { au } else { av }, ac) |
| 18163 | }), |
| 18164 | None, chroma_ctx_offset, smooth_neighbor_uv, fctx, |
| 18165 | )?; |
| 18166 | hit!(CHROMA_SPLIT_TX_HITS); |
| 18167 | if ss_x(fctx) == 1 && ss_y(fctx) == 0 { |
| 18168 | hit!(CHROMA422_SQUARE_HITS); |
| 18169 | } |
| 18170 | // Immediately, so the next unit of this block reads this |
| 18171 | // one's coefficient context. `record` below stamps one |
| 18172 | // chroma state across the whole block; the replay after |
| 18173 | // it puts the per-unit state back. |
| 18174 | neighbours.record_mi_chroma( |
| 18175 | cu_mi, luma_span_x, luma_span_y, plane_pass, &cu_grid, |
| 18176 | ); |
| 18177 | if plane_pass == 1 { |
| 18178 | u_pass.push(cu_grid); |
| 18179 | } else { |
| 18180 | let u_grid = std::mem::replace(&mut u_pass[pass_idx], Grid::Zero(0)); |
| 18181 | units.push((cu_mi, luma_span_x, luma_span_y, u_grid.clone(), cu_grid.clone())); |
| 18182 | last_grids = [u_grid, cu_grid]; |
| 18183 | } |
| 18184 | pass_idx += 1; |
| 18185 | } |
| 18186 | } |
| 18187 | } |
| 18188 | (last_grids[0].clone(), last_grids[1].clone(), units) |
| 18189 | }; |
| 18190 | fctx.intrabc_chroma_tx.with(|c| c.set(None)); |
| 18191 | neighbours.record(at, side, mode, uv_predict_mode, &[&luma_grid[..], &u_grid[..], &v_grid[..]]); |
| 18192 | for (cu_mi, sw, sh, u_unit, v_unit) in &chroma_units { |
| 18193 | neighbours.record_mi_chroma(*cu_mi, *sw, *sh, 1, u_unit); |
| 18194 | neighbours.record_mi_chroma(*cu_mi, *sw, *sh, 2, v_unit); |
| 18195 | } |
| 18196 | } else { |
| 18197 | // Several luma transform units, raster order (spec |
| 18198 | // `transform_block`'s loop): the block's resolved transform is |
| 18199 | // genuinely smaller than its own side, so each tile of it predicts |
| 18200 | // and codes independently, its own context read fresh off whatever |
| 18201 | // the earlier tiles in this same block already wrote -- the same |
| 18202 | // per-TU pattern [`decode_leaf8`] already runs for an 8x8 leaf. |
| 18203 | let n_axis = side / logical_tx; |
| 18204 | // lane-av1-422f: unit width/height per axis, like walk 2 above -- |
| 18205 | // a 4:2:2 frame below side 64 reads ONE rect unit per plane per |
| 18206 | // chunk, the whole plane block. |
| 18207 | let cn_cols = (chroma_side / chroma_tx).max(1); |
| 18208 | let cn_rows = (chroma_height / chroma_tx_h).max(1); |
| 18209 | let luma_span_x = chroma_tx << ss_x(fctx); |
| 18210 | let luma_span_y = chroma_tx_h << ss_y(fctx); |
| 18211 | // libaom's transform-block loop is over 64x64 luma "mu" chunks |
| 18212 | // (`decode_token_recon_block`: `mu_blocks_wide/high` = |
| 18213 | // `mi_size_wide[BLOCK_64X64]`), and it codes EVERY PLANE of a chunk |
| 18214 | // before moving to the next one. Below 128 a block is a single chunk, |
| 18215 | // so this is byte-for-byte the all-luma-then-all-chroma order that has |
| 18216 | // always run; at 128 the four quadrants interleave Y,U,V per quadrant |
| 18217 | // and reading all four luma units first desyncs the tile at the second |
| 18218 | // one (lane-sb128b r3, measured against aomdec's `EC_TRACE_COEFF`). |
| 18219 | let nchunk = (side / 64).max(1); |
| 18220 | // `cn > 1` means the uv plane block is larger than the uv transform, |
| 18221 | // which moves libaom's chroma `txb_skip_ctx` offset from 7 to 10. |
| 18222 | let chroma_ctx_offset = if cn_rows * cn_cols > 1 { Some(3) } else { None }; |
| 18223 | let tpc = (n_axis / nchunk).max(1); |
| 18224 | let cpc_rows = (cn_rows / nchunk).max(1); |
| 18225 | let cpc_cols = (cn_cols / nchunk).max(1); |
| 18226 | // The block's luma transform units, raster order inside each mu |
| 18227 | // chunk. lane-av1-ibcvtx: an intrabc block's units are the var-tx |
| 18228 | // tree's own leaf list -- mixed sizes included, exactly what libaom's |
| 18229 | // `decode_token_recon_block` token loop reconstructs (one |
| 18230 | // `av1_read_coeffs_txb` + inverse transform per resolved unit) -- |
| 18231 | // while every other block's list is the uniform `logical_tx` grid |
| 18232 | // this loop has always walked, generated here in the same order. |
| 18233 | let mut chunk_tus: Vec<Vec<(usize, usize, usize)>> = vec![Vec::new(); nchunk * nchunk]; |
| 18234 | if let Some(leaves) = &intrabc_leaves { |
| 18235 | for &(lr, lc, tw, _th) in leaves { |
| 18236 | let (off_y, off_x) = (lr * MI, lc * MI); |
| 18237 | chunk_tus[(off_y / 64) * nchunk + (off_x / 64)].push((off_y, off_x, tw)); |
| 18238 | } |
| 18239 | } else { |
| 18240 | for tu_row in 0..n_axis { |
| 18241 | for tu_col in 0..n_axis { |
| 18242 | chunk_tus[(tu_row / tpc) * nchunk + (tu_col / tpc)].push(( |
| 18243 | tu_row * logical_tx, |
| 18244 | tu_col * logical_tx, |
| 18245 | logical_tx, |
| 18246 | )); |
| 18247 | } |
| 18248 | } |
| 18249 | } |
| 18250 | let mut last_grids = [Grid::Zero(0), Grid::Zero(0)]; |
| 18251 | let mut units: Vec<((usize, usize), usize, usize, Grid, Grid)> = Vec::new(); |
| 18252 | for ch_row in 0..nchunk { |
| 18253 | for ch_col in 0..nchunk { |
| 18254 | // lane-av1-ibcvtx: the chunk's chroma units inherit the CHROMA tx |
| 18255 | // type of the co-located luma position (`av1_get_tx_type`, |
| 18256 | // blockd.h:1278: a non-Y plane of an inter-classified block -- and |
| 18257 | // intrabc is one -- reads `tx_type_map`, i.e. the coded type of the |
| 18258 | // luma unit covering it). Inside a mu chunk that is the chunk's |
| 18259 | // FIRST luma unit in raster order (the chunk origin is always the |
| 18260 | // first leaf's own origin). Captured right after that unit's read; |
| 18261 | // the single-TU arm below does the same for a whole-block unit. |
| 18262 | let mut first_leaf_tx: Option<TxType> = None; |
| 18263 | for &(tu_off_y, tu_off_x, tu_tx) in &chunk_tus[ch_row * nchunk + ch_col] { |
| 18264 | let tu_mi = (mi_r + tu_off_y / MI, mi_c + tu_off_x / MI); |
| 18265 | let tu_px = px + tu_off_x; |
| 18266 | let tu_py = py + tu_off_y; |
| 18267 | // Same libaom clip as `decode_rect_split`'s loop above; a |
| 18268 | // square block only hangs off the frame when the frame's mi |
| 18269 | // dimensions are odd at the block's own level. |
| 18270 | if tu_px >= y.true_width || tu_py >= y.true_height { |
| 18271 | continue; |
| 18272 | } |
| 18273 | let tu_around = neighbours.around_mi(tu_mi, tu_tx)[0]; |
| 18274 | // A transform unit inside a block is NOT a block at that |
| 18275 | // position (lane-palette2 r12): `Reach::of` granted the |
| 18276 | // top-right 8x8 unit of a 16x16 D203 block bottom-left |
| 18277 | // pixels libaom refuses outright (`col_off > 0`), which is |
| 18278 | // the diagonal wedge seed 46 reconstructed wrong. |
| 18279 | let tu_reach = tu_reach( |
| 18280 | side, |
| 18281 | side, |
| 18282 | tu_off_x, |
| 18283 | tu_off_y, |
| 18284 | tu_tx, |
| 18285 | reach, |
| 18286 | px, |
| 18287 | py, |
| 18288 | y.width, |
| 18289 | y.height, fctx, |
| 18290 | ); |
| 18291 | // This transform unit's own bsize (`tu_tx`) is smaller |
| 18292 | // than the block it sits in (`side`), so `txb_skip_ctx` is |
| 18293 | // the neighbour-magnitude table, not the lone-TU 0 (spec |
| 18294 | // `get_txb_ctx_general`'s `plane_bsize != tx_size` branch). |
| 18295 | let tu_skip_ctx = neighbours.luma_skip_ctx(tu_mi, tu_tx / MI); |
| 18296 | // An intrabc unit's CDF set and scan follow the unit's OWN |
| 18297 | // resolved size (`get_txsize_entropy_ctx` / |
| 18298 | // `av1_get_scan` per `av1_read_coeffs_txb` call); the |
| 18299 | // uniform grid's precomputed pair already matches because |
| 18300 | // `tu_tx == logical_tx` there. |
| 18301 | let (tu_set, tu_scan) = if intrabc_dv.is_some() { |
| 18302 | ( |
| 18303 | inter_txbset_for(tu_tx, reduced_tx_set), |
| 18304 | scan_for(if tu_tx == 64 { 32 } else { tu_tx }, scan32, scan16, scan8, scan4), |
| 18305 | ) |
| 18306 | } else { |
| 18307 | (luma_set, luma_scan) |
| 18308 | }; |
| 18309 | if let Some(buf) = &palette_y_buf { |
| 18310 | hit!(PALETTE_SPLIT_TX_HITS); |
| 18311 | let mut window = vec![0u16; tu_tx * tu_tx]; |
| 18312 | for row in 0..tu_tx { |
| 18313 | let src = (tu_off_y + row) * side + tu_off_x; |
| 18314 | window[row * tu_tx..][..tu_tx] |
| 18315 | .copy_from_slice(&buf[src..][..tu_tx]); |
| 18316 | } |
| 18317 | set_palette_pred(window, fctx); |
| 18318 | } |
| 18319 | let tu_grid = read_plane( |
| 18320 | dec, |
| 18321 | cdfs, |
| 18322 | tu_set, |
| 18323 | tu_scan, |
| 18324 | 0, |
| 18325 | tu_around, |
| 18326 | mode, |
| 18327 | mode, |
| 18328 | angle_delta_y, |
| 18329 | tu_reach, |
| 18330 | y, |
| 18331 | tu_px, |
| 18332 | tu_py, |
| 18333 | tu_tx, |
| 18334 | // The unit's own COEFFICIENT grid, half its side at |
| 18335 | // TX_64X64 (a 64-point transform codes nothing past 32); |
| 18336 | // identical to `tu_tx` at every smaller size |
| 18337 | // (lane-sb128b r1). |
| 18338 | if tu_tx == 64 { 32 } else { tu_tx }, |
| 18339 | base_q_idx, |
| 18340 | None, |
| 18341 | filter_intra, |
| 18342 | Some(tu_skip_ctx), |
| 18343 | smooth_neighbor, fctx, |
| 18344 | )?; |
| 18345 | if crate::envflags::env_flag!("EC_AV1_TRACE") { |
| 18346 | eprintln!( |
| 18347 | "TRACE tu_bitpos mi=({},{}) tell={}", |
| 18348 | tu_mi.0, |
| 18349 | tu_mi.1, |
| 18350 | dec.debug_bitpos() |
| 18351 | ); |
| 18352 | } |
| 18353 | neighbours.record_mi_luma(tu_mi, tu_tx, &tu_grid); |
| 18354 | if first_leaf_tx.is_none() { |
| 18355 | first_leaf_tx = Some(fctx.luma_tx_type.with(std::cell::Cell::get)); |
| 18356 | } |
| 18357 | } |
| 18358 | // CfL is only allowed up to 32x32, i.e. only at `nchunk == 1`, where |
| 18359 | // this still runs after every luma unit of the block. |
| 18360 | let ac = alpha.map(|_| cfl_src(px, py, side)); |
| 18361 | // lane-av1-ibcvtx: an intrabc block's chroma rides the luma's coded |
| 18362 | // tx type (see `first_leaf_tx` above) -- the same arm the single-TU |
| 18363 | // branch sets before its own chroma reads. |
| 18364 | if intrabc_dv.is_some() { |
| 18365 | if let Some(t) = first_leaf_tx { |
| 18366 | fctx.intrabc_chroma_tx.with(|c| c.set(Some(t))); |
| 18367 | } |
| 18368 | } |
| 18369 | // The block's uv plane block is `chroma_side` across but its uv |
| 18370 | // transform is `chroma_tx` (`av1_get_max_uv_txsize` = |
| 18371 | // `av1_get_adjusted_tx_size(max_txsize_rect_lookup[uv_plane_bsize])`): |
| 18372 | // identical at every size up to 64x64, but a 128x128 block's 64x64 uv |
| 18373 | // plane block adjusts TX_64X64 down to TX_32X32, so each plane is |
| 18374 | // FOUR units (lane-sb128b r2) -- one per mu chunk. |
| 18375 | // libaom `decode_token_recon_block` codes a mu chunk PLANE-MAJOR |
| 18376 | // (`for (plane..) for (blk_row..) for (blk_col..)`): every U unit of |
| 18377 | // the chunk, then every V unit. At one unit per plane per chunk -- |
| 18378 | // every non-lossless case this decoder reaches -- that is the same |
| 18379 | // symbol order as the interleaved pair this loop used to read; a |
| 18380 | // lossless frame's 4x4 chroma units are the case where it differs |
| 18381 | // (lane-lossless2). |
| 18382 | let mut u_pass: Vec<Grid> = Vec::new(); |
| 18383 | for plane_pass in 1..=2usize { |
| 18384 | let mut pass_idx = 0usize; |
| 18385 | for cu_row in ch_row * cpc_rows..(ch_row + 1) * cpc_rows { |
| 18386 | for cu_col in ch_col * cpc_cols..(ch_col + 1) * cpc_cols { |
| 18387 | // libaom `decode_reconstruct_tx`'s frame-edge clip |
| 18388 | // (decodeframe.c:299, `blk_row >= max_blocks_high || blk_col |
| 18389 | // >= max_blocks_wide` returns before ANY symbol is read): a |
| 18390 | // transform unit whose 4x4 origin sits outside the frame's mi |
| 18391 | // grid is not coded at all. The luma loop above carries the |
| 18392 | // same clip; its absence here read `txb_skip` (+coeffs) for |
| 18393 | // chroma units libaom never wrote -- the lossless 64x64 block |
| 18394 | // at mi(48,0) of a 320x240 frame overhangs the bottom edge by |
| 18395 | // 4 mi, so its 32x32 chroma codes SIX rows of 4x4 units, not |
| 18396 | // eight (lane-av1-loss64; 48 chroma units per plane in |
| 18397 | // aomdec's EC_COEFF census vs the 64 this loop read). |
| 18398 | // `max_block_wide/high` (av1_common_int.h:1565): the plane |
| 18399 | // block's pixel span (4:2:0 halves the luma span, clamped to |
| 18400 | // 4 by `set_plane_n4`) plus the negative overhang |
| 18401 | // (`mb_to_*_edge` = `2 * overhang_mi` chroma px), in 4x4 |
| 18402 | // units. |
| 18403 | let span_mi = side / MI; |
| 18404 | let over_w_mi = |
| 18405 | (2 * y.true_width.div_ceil(8)) as i32 - mi_c as i32 - span_mi as i32; |
| 18406 | let over_h_mi = |
| 18407 | (2 * y.true_height.div_ceil(8)) as i32 - mi_r as i32 - span_mi as i32; |
| 18408 | // `max_block_wide/high` in chroma pixels: the plane block's |
| 18409 | // own span (`side` at 4:4:4, `side / 2` at 4:2:0 -- the old |
| 18410 | // hardcoded `span_mi * 2` clipped three quarters of a 4:4:4 |
| 18411 | // block's coded units and desynced the tile) plus the |
| 18412 | // negative overhang (`mb_to_*_edge >> (3 + ss)`), whose |
| 18413 | // chroma-px scale is `MI >> ss` per mi. |
| 18414 | let plane_px = (((span_mi * MI) >> ss_x(fctx)).max(4)) as i32; |
| 18415 | let over_w_px = over_w_mi.min(0) * (MI >> ss_x(fctx)) as i32; |
| 18416 | let over_h_px = over_h_mi.min(0) * (MI >> ss_y(fctx)) as i32; |
| 18417 | let max_units_w = ((plane_px + over_w_px) >> 2).max(0) as usize; |
| 18418 | // lane-av1-422f: the shared halved formula in the `else` counts |
| 18419 | // the plane span in 4-px units through the 2-px-per-mi |
| 18420 | // subsampled height, which a 4:2:2 plane does not have (ss_y |
| 18421 | // 0) -- its span is the full luma mi count, so a keyed |
| 18422 | // formula clips the same units libaom's `max_block_high` |
| 18423 | // does. 4:2:0 and 4:4:4 keep the proven shared rows (at 4:2:0 |
| 18424 | // `MI >> ss_y` is 2, the same `2 * over_h_mi.min(0)` the |
| 18425 | // pre-ss form spelled). |
| 18426 | let max_units_h = if ss_x(fctx) == 1 && ss_y(fctx) == 0 { |
| 18427 | (span_mi as i32 + over_h_mi.min(0)).max(0) as usize |
| 18428 | } else { |
| 18429 | ((plane_px + over_h_px) >> 2).max(0) as usize |
| 18430 | }; |
| 18431 | let (unit_w, unit_h) = (chroma_tx / 4, chroma_tx_h / 4); |
| 18432 | if cu_col * unit_w >= max_units_w || cu_row * unit_h >= max_units_h { |
| 18433 | hit!(CHROMA_EDGE_TU_CLIP_HITS); |
| 18434 | continue; |
| 18435 | } |
| 18436 | // The chroma context arrays are indexed in LUMA mi, so this |
| 18437 | // unit's anchor is its own luma quadrant (lane-av1-422b: the |
| 18438 | // quadrant spans `chroma_tx << ss_x` luma px wide by |
| 18439 | // `chroma_tx_h << ss_y` tall -- at 4:2:2 below side 64 the |
| 18440 | // one unit's quadrant is the whole block). |
| 18441 | let cu_mi = ( |
| 18442 | mi_r + cu_row * (luma_span_y / MI), |
| 18443 | mi_c + cu_col * (luma_span_x / MI), |
| 18444 | ); |
| 18445 | let cu_around = if ss_x(fctx) == 1 && ss_y(fctx) == 0 { |
| 18446 | // lane-av1-422f: chroma-only loop; per-chroma-cell above |
| 18447 | // votes (see [`Neighbours::around_mi_422_chroma`]). |
| 18448 | neighbours.around_mi_422_chroma(cu_mi, luma_span_x, luma_span_y) |
| 18449 | } else { |
| 18450 | neighbours.around_mi_rect(cu_mi, luma_span_x, luma_span_y) |
| 18451 | }; |
| 18452 | let cu_reach = if cn_rows == 1 && cn_cols == 1 { |
| 18453 | reach |
| 18454 | } else { |
| 18455 | tu_reach_rect( |
| 18456 | side, side, |
| 18457 | cu_col * luma_span_x, cu_row * luma_span_y, luma_span_x, luma_span_y, |
| 18458 | reach, px, py, y.width, y.height, fctx, |
| 18459 | ) |
| 18460 | }; |
| 18461 | let (cu_x, cu_y) = |
| 18462 | (cpx + cu_col * chroma_tx, cpy + cu_row * chroma_tx_h); |
| 18463 | if let Some((ub, vb)) = &palette_uv_bufs { |
| 18464 | let pbuf = if plane_pass == 1 { ub } else { vb }; |
| 18465 | set_palette_pred( |
| 18466 | palette_window( |
| 18467 | pbuf, chroma_side, cu_col * chroma_tx, |
| 18468 | cu_row * chroma_tx_h, chroma_tx, chroma_tx_h, |
| 18469 | ), |
| 18470 | fctx, |
| 18471 | ); |
| 18472 | } |
| 18473 | let buf: &mut PlaneBuf<'static> = |
| 18474 | if plane_pass == 1 { &mut *u } else { &mut *v }; |
| 18475 | let cu_grid = if chroma_tx == chroma_tx_h { |
| 18476 | read_plane( |
| 18477 | dec, cdfs, chroma_set, scans.1, plane_pass, cu_around[plane_pass], mode, |
| 18478 | uv_predict_mode, angle_delta_uv, cu_reach, buf, cu_x, cu_y, |
| 18479 | chroma_tx, chroma_tx, base_q_idx, |
| 18480 | alpha.zip(ac).map(|((au, av), ac)| { |
| 18481 | (if plane_pass == 1 { au } else { av }, ac) |
| 18482 | }), |
| 18483 | None, chroma_ctx_offset, smooth_neighbor_uv, fctx, |
| 18484 | )? |
| 18485 | } else { |
| 18486 | // lane-av1-422f: the whole plane block as one rect unit; |
| 18487 | // `chroma_ctx_offset` is `None` there (the unit covers |
| 18488 | // the plane block, so the offset-7 skip rows are right). |
| 18489 | read_rect_chroma_unit( |
| 18490 | dec, cdfs, chroma_set, scans.1, chroma_tx, chroma_tx_h, plane_pass, |
| 18491 | cu_around[plane_pass], uv_predict_mode, angle_delta_uv, cu_reach, buf, |
| 18492 | cu_x, cu_y, base_q_idx, None, |
| 18493 | alpha.zip(ac).map(|((au, av), ac)| { |
| 18494 | (if plane_pass == 1 { au } else { av }, ac) |
| 18495 | }), |
| 18496 | smooth_neighbor_uv, fctx, |
| 18497 | )? |
| 18498 | }; |
| 18499 | if cn_rows * cn_cols > 1 { |
| 18500 | hit!(CHROMA_SPLIT_TX_HITS); |
| 18501 | if ss_x(fctx) == 1 && ss_y(fctx) == 0 { |
| 18502 | hit!(CHROMA422_SQUARE_HITS); |
| 18503 | } |
| 18504 | // Immediately, so the NEXT unit of this same block reads |
| 18505 | // this one's coefficient context (libaom's chroma |
| 18506 | // `txb_skip_ctx` for the second quadrant is 11, i.e. base |
| 18507 | // 1 from this left neighbour -- measured against aomdec). |
| 18508 | // The same writes are replayed after `record_split_luma` |
| 18509 | // below, which would otherwise clobber them whole-block. |
| 18510 | neighbours.record_mi_chroma( |
| 18511 | cu_mi, luma_span_x, luma_span_y, plane_pass, &cu_grid, |
| 18512 | ); |
| 18513 | } |
| 18514 | if plane_pass == 1 { |
| 18515 | u_pass.push(cu_grid); |
| 18516 | } else { |
| 18517 | let u_grid = std::mem::replace(&mut u_pass[pass_idx], Grid::Zero(0)); |
| 18518 | if cn_rows * cn_cols > 1 { |
| 18519 | units.push((cu_mi, luma_span_x, luma_span_y, u_grid.clone(), cu_grid.clone())); |
| 18520 | } |
| 18521 | last_grids = [u_grid, cu_grid]; |
| 18522 | } |
| 18523 | pass_idx += 1; |
| 18524 | } |
| 18525 | } |
| 18526 | } |
| 18527 | } |
| 18528 | if intrabc_dv.is_some() { |
| 18529 | fctx.intrabc_chroma_tx.with(|c| c.set(None)); |
| 18530 | } |
| 18531 | } |
| 18532 | // Mode/side bookkeeping plus, at `cn == 1`, the whole-plane chroma |
| 18533 | // context write; at `cn > 1` the per-unit writes above are what the |
| 18534 | // next block reads, so they run after this (last writer wins). |
| 18535 | neighbours.record_split_luma(at, side, mode, uv_predict_mode, [&last_grids[0], &last_grids[1]]); |
| 18536 | for (cu_mi, sw, sh, u_grid, v_grid) in &units { |
| 18537 | neighbours.record_mi_chroma(*cu_mi, *sw, *sh, 1, u_grid); |
| 18538 | neighbours.record_mi_chroma(*cu_mi, *sw, *sh, 2, v_grid); |
| 18539 | } |
| 18540 | } |
| 18541 | // Unconditional, `size == 0` for a non-palette block: clears any stale |
| 18542 | // neighbour palette state the same way [`Neighbours::record`] always |
| 18543 | // overwrites `above_mode`/`left_mode` regardless of this block's own mode |
| 18544 | // (the split-luma-TU branch above always refuses before reaching here |
| 18545 | // when `palette_y` is `Some`, so it is never live for that branch). |
| 18546 | let (py_size, py_colors) = palette_y |
| 18547 | .as_ref() |
| 18548 | .map_or((0, [0u16; 8]), |p| (p.size, p.colors)); |
| 18549 | neighbours.record_palette_y(at, side, py_size, py_colors); |
| 18550 | let (puv_size, puv_colors) = palette_uv |
| 18551 | .as_ref() |
| 18552 | .map_or((0, [0u16; 8]), |p| (p.size, p.u_colors)); |
| 18553 | neighbours.record_palette_uv(at, side, puv_size, puv_colors); |
| 18554 | neighbours.fill_skip_grid((r * (SUB / MI), c * (SUB / MI)), side / MI, skip); |
| 18555 | neighbours.fill_lf_grid( |
| 18556 | (r * (SUB / MI), c * (SUB / MI)), |
| 18557 | side / MI, |
| 18558 | logical_tx as u8, |
| 18559 | 0, fctx, |
| 18560 | ); |
| 18561 | // lane-t900 r32 / lane-av1txbands: libaom runs `set_txfm_ctxs(mbmi->tx_size, |
| 18562 | // n4_w, n4_h, skip_txfm && is_inter_block(mbmi), xd)` at the end of EVERY |
| 18563 | // `decode_block`. An intra-only frame's blocks also publish their TRANSFORM |
| 18564 | // size from [`Neighbours::fill_lf_grid_rect`], but this grid cannot express |
| 18565 | // libaom's `skip && is_inter` term -- the one thing that makes a SKIPPED |
| 18566 | // intrabc block publish its BLOCK size instead, intrabc being the only |
| 18567 | // "inter" block an intra-only frame codes (`blockd.h:373`). So this call |
| 18568 | // stays the authority wherever one can appear; with `allow_intrabc` unset |
| 18569 | // it writes exactly what the grid already did. |
| 18570 | if allow_intrabc { |
| 18571 | set_txfm_ctxs( |
| 18572 | neighbours, |
| 18573 | (mi_r, mi_c), |
| 18574 | logical_tx, |
| 18575 | side / MI, |
| 18576 | side / MI, |
| 18577 | skip && intrabc_dv.is_some(), |
| 18578 | ); |
| 18579 | } |
| 18580 | record_intrabc_mi(mi_r, mi_c, side / MI, intrabc_dv, neighbours, fctx); |
| 18581 | Ok(()) |
| 18582 | } |
| 18583 | |
| 18584 | thread_local! { |
| 18585 | /// lane-sb128c r8: INTRA (key-frame) 128x64 blocks decoded at the 128 |
| 18586 | /// root's HORZ arm, and 64x128 ones at its VERT arm. |
| 18587 | pub(crate) static INTRA_SB128_HORZ_HITS: std::cell::Cell<usize> = const { std::cell::Cell::new(0) }; |
| 18588 | pub(crate) static INTRA_SB128_VERT_HITS: std::cell::Cell<usize> = const { std::cell::Cell::new(0) }; |
| 18589 | /// Those blocks by resolved intra `tx_depth` (0 = TX_64X64 units, |
| 18590 | /// 1 = TX_32X32, 2 = TX_16X16) -- proof the depth symbol is really read. |
| 18591 | pub(crate) static INTRA_SB128_DEPTH_HITS: std::cell::Cell<[usize; 3]> = const { std::cell::Cell::new([0; 3]) }; |
| 18592 | /// lane-inter128intra r1: the same shapes decoded on the INTER block path |
| 18593 | /// (an intra-coded PARTITION_HORZ/VERT half of a 128 superblock inside an |
| 18594 | /// inter frame) -- (128x64, 64x128). |
| 18595 | pub(crate) static INTRA128_IN_INTER_HITS: std::cell::Cell<[usize; 2]> = const { std::cell::Cell::new([0; 2]) }; |
| 18596 | /// lane-t900 r17: skipped INTER blocks whose `read_block_tx_size` still |
| 18597 | /// handed back var-tx leaves, so the per-transform-unit luma publication |
| 18598 | /// loop never ran and the block has to clear its own luma coefficient |
| 18599 | /// contexts (libaom `av1_reset_entropy_context`). Every 128 root under |
| 18600 | /// `TX_MODE_LARGEST` is one; the counter is what keeps that reset's gate |
| 18601 | /// non-vacuous. |
| 18602 | pub(crate) static SKIP_VARTX_LUMA_RESET_HITS: std::cell::Cell<usize> = const { std::cell::Cell::new(0) }; |
| 18603 | /// lane-lossless128: an intra 128-axis (BLOCK_128X64/64X128) block decoded |
| 18604 | /// on a LOSSLESS frame, where every plane is coded as TX_4X4 units (the |
| 18605 | /// frame's `tx_mode` is forced `ONLY_4X4`). Keeps the gate that owns this |
| 18606 | /// shape non-vacuous -- a stream that never codes one reads 0. |
| 18607 | pub(crate) static INTRA128_LOSSLESS_HITS: std::cell::Cell<usize> = const { std::cell::Cell::new(0) }; |
| 18608 | } |
| 18609 | |
| 18610 | /// [`SKIP_VARTX_LUMA_RESET_HITS`], for a gate's own before/after delta. |
| 18611 | pub fn skip_vartx_luma_reset_hits() -> usize { |
| 18612 | SKIP_VARTX_LUMA_RESET_HITS.with(std::cell::Cell::get) |
| 18613 | } |
| 18614 | |
| 18615 | /// [`INTRA128_LOSSLESS_HITS`], for a gate's own before/after delta. |
| 18616 | pub(crate) fn intra128_lossless_hits() -> usize { |
| 18617 | INTRA128_LOSSLESS_HITS.with(std::cell::Cell::get) |
| 18618 | } |
| 18619 | |
| 18620 | /// [`INTRA128_IN_INTER_HITS`], for a gate's own before/after delta. |
| 18621 | pub(crate) fn intra128_in_inter_hits() -> [usize; 2] { |
| 18622 | INTRA128_IN_INTER_HITS.with(std::cell::Cell::get) |
| 18623 | } |
| 18624 | |
| 18625 | /// [`INTRA_SB128_HORZ_HITS`] / [`INTRA_SB128_VERT_HITS`] / |
| 18626 | /// [`INTRA_SB128_DEPTH_HITS`], for a gate's own before/after delta. |
| 18627 | pub(crate) fn intra_sb128_hits() -> (usize, usize, [usize; 3]) { |
| 18628 | ( |
| 18629 | INTRA_SB128_HORZ_HITS.with(std::cell::Cell::get), |
| 18630 | INTRA_SB128_VERT_HITS.with(std::cell::Cell::get), |
| 18631 | INTRA_SB128_DEPTH_HITS.with(std::cell::Cell::get), |
| 18632 | ) |
| 18633 | } |
| 18634 | |
| 18635 | /// The 128 root's HORZ/VERT arm on an INTRA frame: ONE 128x64 (HORZ) or |
| 18636 | /// 64x128 (VERT) intra block (lane-sb128c r8; the inter twin is |
| 18637 | /// `decode_inter_block`'s own rect path). |
| 18638 | /// |
| 18639 | /// libaom `decode_block` -> `predict_and_reconstruct_intra_block` per |
| 18640 | /// transform unit, with the 128-specific geometry: |
| 18641 | /// * `max_txsize_rect_lookup[BLOCK_128X64]` is TX_64X64, so every luma unit |
| 18642 | /// is SQUARE and there are at least two of them -- depth 0: 64x64 x2, |
| 18643 | /// depth 1: 32x32 x8, depth 2: 16x16 x32 (`bsize_to_tx_size_cat` counts |
| 18644 | /// the `sub_tx_size_map` chain TX_64X64->32->16->8->4, i.e. cat 3, the |
| 18645 | /// same CDF a 64x64 square block reads). |
| 18646 | /// * `av1_get_max_uv_txsize` = `av1_get_adjusted_tx_size( |
| 18647 | /// max_txsize_rect_lookup[BLOCK_64X32])` caps at TX_32X32, so the block's |
| 18648 | /// 64x32 uv plane block is TWO 32x32 chroma units per plane -- one per |
| 18649 | /// 64x64 luma "mu" chunk, and `decode_token_recon_block` codes Y, U, V of |
| 18650 | /// a chunk before moving to the next (the interleave lane-sb128b r3 |
| 18651 | /// measured at the 128x128 NONE root). |
| 18652 | /// * `get_txb_ctx`: the uv plane block is bigger than the uv transform, so |
| 18653 | /// chroma `txb_skip_ctx` takes the offset-10 rows (7 + 3). |
| 18654 | /// No CfL, no filter intra (both cap at 32x32) and no palette |
| 18655 | /// (`av1_allow_palette` caps at 64x64) exist at this size -- the mode |
| 18656 | /// reader's own size gates already say so, and the guard below is defensive. |
| 18657 | /// |
| 18658 | /// lane-lossless128: on a LOSSLESS frame every one of those sizes collapses to |
| 18659 | /// TX_4X4 -- `read_tx_mode` returns `ONLY_4X4` before the frame codes a tx-size |
| 18660 | /// symbol and `read_tx_size` returns `TX_4X4` before it looks at anything |
| 18661 | /// (`decodeframe.c:141`, `:1183`) -- so the block is tiled into 4x4 luma units |
| 18662 | /// and (because a lossless frame forces `TX_4X4` on CHROMA too) 4x4 chroma |
| 18663 | /// units, plane-major inside each 64x64 mu chunk exactly as |
| 18664 | /// `decode_token_recon_block` codes them. Reading the 64-point geometry there |
| 18665 | /// reconstructs into a `side`-sized plane with a 16-sample residual (the panic |
| 18666 | /// this lane closed); the geometry and the unit walks below are normalized on |
| 18667 | /// `lossless_frame`. |
| 18668 | #[allow(clippy::too_many_arguments)] |
| 18669 | fn decode_block_128rect( |
| 18670 | dec: &mut SymbolDecoder, |
| 18671 | cdfs: &mut Cdfs, |
| 18672 | neighbours: &mut Neighbours, |
| 18673 | at: (usize, usize), |
| 18674 | bw: usize, |
| 18675 | bh: usize, |
| 18676 | y: &mut PlaneBuf<'static>, |
| 18677 | u: &mut PlaneBuf<'static>, |
| 18678 | v: &mut PlaneBuf<'static>, |
| 18679 | base_q_idx: u8, |
| 18680 | enable_filter_intra: bool, |
| 18681 | allow_screen_content_tools: bool, |
| 18682 | tx_select: bool, |
| 18683 | reduced_tx_set: bool, fctx: &crate::decode::FrameCtx, |
| 18684 | ) -> Result<()> { |
| 18685 | let (r, c) = at; |
| 18686 | let (px, py) = (c * SUB, r * SUB); |
| 18687 | let (mi_r, mi_c) = (r * (SUB / MI), c * (SUB / MI)); |
| 18688 | let (nb_above_mode, nb_left_mode) = neighbours.modes_above_left(r, c); |
| 18689 | let (palette_ctx, palette_cache) = neighbours.palette_ctx_and_cache(at); |
| 18690 | let palette_uv_cache = neighbours.palette_uv_cache(at); |
| 18691 | let (skip, mode, angle_delta_y, uv_mode, angle_delta_uv, alpha, filter_intra, palette_y, palette_uv, intrabc) = |
| 18692 | read_intra_mode_rect( |
| 18693 | dec, |
| 18694 | cdfs, |
| 18695 | nb_above_mode, |
| 18696 | nb_left_mode, |
| 18697 | // `is_cfl_allowed` caps CFL at 32x32: `uv_mode` comes off the |
| 18698 | // 13-symbol no-CFL CDF here, never the 14-symbol one. |
| 18699 | false, |
| 18700 | true, |
| 18701 | bw, |
| 18702 | bh, |
| 18703 | enable_filter_intra, |
| 18704 | neighbours.skip_txfm_ctx(mi_r, mi_c), |
| 18705 | allow_screen_content_tools, |
| 18706 | Some((palette_ctx, &palette_cache)), |
| 18707 | &palette_uv_cache, |
| 18708 | mi_r, |
| 18709 | mi_c, fctx, |
| 18710 | )?; |
| 18711 | if let Some((dv_row, dv_col)) = intrabc { |
| 18712 | // lane-av1-ibcrect: the reconstruction is ported -- an intrabc block |
| 18713 | // is is_inter_block, so libaom predicts the whole 128x64/64x128 strip |
| 18714 | // from this frame's own reconstruction and runs the INTER residual |
| 18715 | // walk (`decode_token_recon_block`'s else arm) over it. |
| 18716 | return decode_intrabc_128rect( |
| 18717 | dec, |
| 18718 | cdfs, |
| 18719 | neighbours, |
| 18720 | (mi_r, mi_c), |
| 18721 | (bw, bh), |
| 18722 | skip, |
| 18723 | (dv_row, dv_col), |
| 18724 | y, |
| 18725 | u, |
| 18726 | v, |
| 18727 | base_q_idx, fctx, |
| 18728 | ); |
| 18729 | } |
| 18730 | if alpha.is_some() || filter_intra.is_some() || palette_y.is_some() || palette_uv.is_some() { |
| 18731 | return Err(unsupported( |
| 18732 | "CfL, filter intra or a palette on a 128-root HORZ/VERT intra block (every one of \ |
| 18733 | their size gates caps at 64x64 or below, so none of these symbols exists there)", |
| 18734 | )); |
| 18735 | } |
| 18736 | let uv_predict_mode = if uv_mode == UV_CFL_PRED { DC_PRED } else { uv_mode }; |
| 18737 | let smooth_neighbor = is_smooth_mode(nb_above_mode) || is_smooth_mode(nb_left_mode); |
| 18738 | if smooth_neighbor { |
| 18739 | hit!(SMOOTH_LUMA_HITS); |
| 18740 | } |
| 18741 | let smooth_neighbor_uv = neighbours.smooth_uv_neighbour(mi_r, mi_c, r, c, fctx); |
| 18742 | // spec 5.11.16 `read_tx_size`: read for every intra block, skipped or |
| 18743 | // not. `bsize_to_tx_size_cat(BLOCK_128X64)` is 3 (see this fn's doc). |
| 18744 | // lane-lossless128: a LOSSLESS frame codes no tx-size symbol at all -- |
| 18745 | // libaom's `read_tx_mode` returns `ONLY_4X4` for a `coded_lossless` frame |
| 18746 | // before the symbol is even considered (`decodeframe.c:141`) and |
| 18747 | // `read_tx_size` returns `TX_4X4` before it looks at anything |
| 18748 | // (`decodeframe.c:1183`). So `tx_select` is off, every plane is walked as |
| 18749 | // TX_4X4 transform units, and no `tx_size_cat3` symbol may be consumed. |
| 18750 | let lossless_frame = lossless(fctx); |
| 18751 | let in_inter = fctx.intra_in_inter_mode.with(std::cell::Cell::get).is_some(); |
| 18752 | let depth = if tx_select && !lossless_frame { |
| 18753 | // lane-inter128intra r1: `get_tx_size_context` compares the bands |
| 18754 | // against `tx_size_wide/high[max_txsize_rect_lookup[bsize]]` -- |
| 18755 | // TX_64X64 for every 128-axis block, hence the `min(64)`. |
| 18756 | let ctx = tx_size_context_txfm_rect(neighbours, (mi_r, mi_c), bw.min(64), bh.min(64), fctx); |
| 18757 | dec.symbol(&mut cdfs.tx_size_cat3[ctx]) |
| 18758 | } else { |
| 18759 | 0 |
| 18760 | }; |
| 18761 | if depth != 0 { |
| 18762 | hit!(TX_DEPTH_HITS); |
| 18763 | } |
| 18764 | hit_do! { |
| 18765 | INTRA_SB128_DEPTH_HITS.with(|c| { |
| 18766 | let mut h = c.get(); |
| 18767 | h[depth.min(2)] += 1; |
| 18768 | c.set(h); |
| 18769 | }); |
| 18770 | } |
| 18771 | let logical_tx = if lossless_frame { 4 } else { 64 >> depth }; |
| 18772 | if lossless_frame { |
| 18773 | hit!(INTRA128_LOSSLESS_HITS); |
| 18774 | } |
| 18775 | // A 64-point transform codes nothing past its low 32 coefficients. |
| 18776 | let coeff_tx_side = logical_tx.min(32); |
| 18777 | let luma_set = if lossless_frame { |
| 18778 | TxbSet::Luma4 |
| 18779 | } else { |
| 18780 | txbset_for(logical_tx, reduced_tx_set) |
| 18781 | }; |
| 18782 | let luma_scan = default_scan(coeff_tx_side); |
| 18783 | // `av1_get_max_uv_txsize` caps at TX_32X32 normally; a lossless frame |
| 18784 | // forces every plane to TX_4X4 (its own `Chroma4` set and 4x4 scan). |
| 18785 | let chroma_tx = if lossless_frame { 4 } else { 32 }; |
| 18786 | let chroma_set = if lossless_frame { |
| 18787 | TxbSet::Chroma4 |
| 18788 | } else { |
| 18789 | TxbSet::Chroma32 |
| 18790 | }; |
| 18791 | let chroma_scan = default_scan(chroma_tx); |
| 18792 | let (cpx, cpy) = (px >> ss_x(fctx), py >> ss_y(fctx)); |
| 18793 | let (chroma_w, chroma_h) = (bw >> ss_x(fctx), bh >> ss_y(fctx)); |
| 18794 | let reach = Reach::of_rect(bw, bh, px, py, y.width, y.height, fctx); |
| 18795 | let (nch_x, nch_y) = (bw / 64, bh / 64); |
| 18796 | // The frame's own mi grid (`av1_calc_mi_size`, 8-px aligned): libaom's |
| 18797 | // `decode_token_recon_block` clips each vector's walk to |
| 18798 | // `max_block_wide/high`, so a chroma unit whose top-left is off the frame |
| 18799 | // codes nothing. |
| 18800 | let mi_cols = 2 * y.true_width.div_ceil(8); |
| 18801 | let mi_rows = 2 * y.true_height.div_ceil(8); |
| 18802 | let max_w_mi = (bw / MI).min(mi_cols.saturating_sub(mi_c)); |
| 18803 | let max_h_mi = (bh / MI).min(mi_rows.saturating_sub(mi_r)); |
| 18804 | // Non-lossless: per mu chunk, `(64 >> ss) / chroma_tx` chroma units per |
| 18805 | // axis (one 32x32 at 4:2:0, four 32x32 at 4:4:4 — lane-av1-444sb). |
| 18806 | debug_assert!( |
| 18807 | lossless_frame |
| 18808 | || (chroma_w / chroma_tx, chroma_h / chroma_tx) |
| 18809 | == ( |
| 18810 | nch_x * ((64 >> ss_x(fctx)) / chroma_tx), |
| 18811 | nch_y * ((64 >> ss_y(fctx)) / chroma_tx) |
| 18812 | ) |
| 18813 | ); |
| 18814 | let tpc = 64 / logical_tx; |
| 18815 | let zero_tu = vec![0i32; logical_tx * logical_tx]; |
| 18816 | let zero_cu = vec![0i32; chroma_tx * chroma_tx]; |
| 18817 | let mut last_grids = [Grid::Zero(0), Grid::Zero(0)]; |
| 18818 | let mut units: Vec<(usize, (usize, usize), Grid)> = Vec::new(); |
| 18819 | // The chroma context arrays are indexed in LUMA mi; every chroma unit of |
| 18820 | // every chunk shares this luma span (2 chroma pixels per luma unit). |
| 18821 | let luma_span = chroma_tx << ss_x(fctx); |
| 18822 | for ch_row in 0..nch_y { |
| 18823 | for ch_col in 0..nch_x { |
| 18824 | for tu_row in ch_row * tpc..(ch_row + 1) * tpc { |
| 18825 | for tu_col in ch_col * tpc..(ch_col + 1) * tpc { |
| 18826 | let tu_mi = ( |
| 18827 | mi_r + tu_row * (logical_tx / MI), |
| 18828 | mi_c + tu_col * (logical_tx / MI), |
| 18829 | ); |
| 18830 | let (tu_px, tu_py) = (px + tu_col * logical_tx, py + tu_row * logical_tx); |
| 18831 | // libaom clips the transform loop to |
| 18832 | // `max_blocks_wide/high`: a unit whose top-left sample is |
| 18833 | // outside the frame is never coded. |
| 18834 | if tu_px >= y.true_width || tu_py >= y.true_height { |
| 18835 | continue; |
| 18836 | } |
| 18837 | // Edge availability of a unit INSIDE a block, with the |
| 18838 | // 128 geometry -- `has_top_right` is false for a block |
| 18839 | // whose largest side is > 64, which [`Reach::of_rect`] |
| 18840 | // already answered for the block and [`tu_reach`] carries |
| 18841 | // down to the units on its own top row. |
| 18842 | let tu_r = tu_reach( |
| 18843 | bw, |
| 18844 | bh, |
| 18845 | tu_col * logical_tx, |
| 18846 | tu_row * logical_tx, |
| 18847 | logical_tx, |
| 18848 | reach, |
| 18849 | px, |
| 18850 | py, |
| 18851 | y.width, |
| 18852 | y.height, fctx, |
| 18853 | ); |
| 18854 | if skip { |
| 18855 | push_intra(0, |
| 18856 | tu_px, tu_py, logical_tx, mode, angle_delta_y, tu_r, &zero_tu, None, |
| 18857 | None, smooth_neighbor, fctx, |
| 18858 | ); |
| 18859 | neighbours.record_mi_luma(tu_mi, logical_tx, &zero_tu); |
| 18860 | continue; |
| 18861 | } |
| 18862 | // The unit is smaller than its block, so `txb_skip_ctx` |
| 18863 | // is the neighbour-magnitude table, never the lone-TU 0. |
| 18864 | let tu_skip_ctx = neighbours.luma_skip_ctx(tu_mi, logical_tx / MI); |
| 18865 | let tu_around = neighbours.around_mi(tu_mi, logical_tx)[0]; |
| 18866 | let tu_grid = read_plane( |
| 18867 | dec, |
| 18868 | cdfs, |
| 18869 | luma_set, |
| 18870 | &luma_scan, |
| 18871 | 0, |
| 18872 | tu_around, |
| 18873 | mode, |
| 18874 | mode, |
| 18875 | angle_delta_y, |
| 18876 | tu_r, |
| 18877 | y, |
| 18878 | tu_px, |
| 18879 | tu_py, |
| 18880 | logical_tx, |
| 18881 | coeff_tx_side, |
| 18882 | base_q_idx, |
| 18883 | None, |
| 18884 | None, |
| 18885 | Some(tu_skip_ctx), |
| 18886 | smooth_neighbor, fctx, |
| 18887 | )?; |
| 18888 | neighbours.record_mi_luma(tu_mi, logical_tx, &tu_grid); |
| 18889 | } |
| 18890 | } |
| 18891 | // Transform units per axis inside this mu chunk's chroma span: |
| 18892 | // a 64x64 luma chunk covers `64 >> ss` chroma pixels per axis, so |
| 18893 | // it holds `(64 >> ss) / chroma_tx` units — one 32x32 at 4:2:0, |
| 18894 | // four at 4:4:4 (lane-av1-444sb: the hardcoded 4:2:0 span read |
| 18895 | // one unit per chunk there, left six of a 128x64 block's eight |
| 18896 | // 32x32 units unread and desynced the tile from that block on). |
| 18897 | let cn = ((64 >> ss_x(fctx)) / chroma_tx).max(1); |
| 18898 | // libaom's per-chunk `unit_width/height` (plane-mi units of the |
| 18899 | // luma mi grid): `min(mu_blocks + col, max_blocks) >> ss`. |
| 18900 | let unit_w = ((ch_col + 1) * 16).min(max_w_mi) >> ss_x(fctx); |
| 18901 | let unit_h = ((ch_row + 1) * 16).min(max_h_mi) >> ss_y(fctx); |
| 18902 | let stepc = (chroma_tx / 4).max(1); |
| 18903 | // `decode_token_recon_block` codes a mu chunk PLANE-MAJOR: every U |
| 18904 | // unit of the chunk, then every V unit. At one unit per plane per |
| 18905 | // chunk (every non-lossless 128-axis block) that is the same |
| 18906 | // symbol order the interleaved pair used to read; a lossless |
| 18907 | // frame's 4x4 chroma units are where it becomes observable. |
| 18908 | for plane_pass in 1..=2usize { |
| 18909 | for ci_row in 0..cn { |
| 18910 | for ci_col in 0..cn { |
| 18911 | if ch_col * (16 >> ss_x(fctx)) + ci_col * stepc >= unit_w |
| 18912 | || ch_row * (16 >> ss_y(fctx)) + ci_row * stepc >= unit_h |
| 18913 | { |
| 18914 | continue; |
| 18915 | } |
| 18916 | let cu_x = cpx + ch_col * (64 >> ss_x(fctx)) + ci_col * chroma_tx; |
| 18917 | let cu_y = cpy + ch_row * (64 >> ss_y(fctx)) + ci_row * chroma_tx; |
| 18918 | let cu_mi = ( |
| 18919 | mi_r + ch_row * 16 + ci_row * ((chroma_tx << ss_y(fctx)) / 4), |
| 18920 | mi_c + ch_col * 16 + ci_col * ((chroma_tx << ss_x(fctx)) / 4), |
| 18921 | ); |
| 18922 | let cu_r = tu_reach( |
| 18923 | bw, |
| 18924 | bh, |
| 18925 | ch_col * 64 + ci_col * luma_span, |
| 18926 | ch_row * 64 + ci_row * luma_span, |
| 18927 | luma_span, |
| 18928 | reach, |
| 18929 | px, |
| 18930 | py, |
| 18931 | y.width, |
| 18932 | y.height, fctx, |
| 18933 | ); |
| 18934 | if skip { |
| 18935 | push_intra(plane_pass, |
| 18936 | cu_x, cu_y, chroma_tx, uv_predict_mode, angle_delta_uv, cu_r, |
| 18937 | &zero_cu, None, None, smooth_neighbor_uv, fctx, |
| 18938 | ); |
| 18939 | continue; |
| 18940 | } |
| 18941 | let cu_around = neighbours.around_mi(cu_mi, luma_span); |
| 18942 | let buf: &mut PlaneBuf<'static> = |
| 18943 | if plane_pass == 1 { &mut *u } else { &mut *v }; |
| 18944 | let grid = read_plane( |
| 18945 | dec, cdfs, chroma_set, &chroma_scan, plane_pass, cu_around[plane_pass], |
| 18946 | mode, uv_predict_mode, angle_delta_uv, cu_r, buf, cu_x, cu_y, |
| 18947 | chroma_tx, chroma_tx, base_q_idx, None, None, Some(3), |
| 18948 | smooth_neighbor_uv, fctx, |
| 18949 | )?; |
| 18950 | last_grids[plane_pass - 1] = grid.clone(); |
| 18951 | // Immediately, so the NEXT unit of this same block |
| 18952 | // reads this one's coefficient context; replayed after |
| 18953 | // `record_split_luma_rect` below, which would otherwise |
| 18954 | // clobber them whole-block. |
| 18955 | neighbours.record_mi_chroma(cu_mi, luma_span, luma_span, plane_pass, &grid); |
| 18956 | units.push((plane_pass, cu_mi, grid)); |
| 18957 | } |
| 18958 | } |
| 18959 | } |
| 18960 | if !skip { |
| 18961 | hit!(CHROMA_SPLIT_TX_HITS); |
| 18962 | } |
| 18963 | } |
| 18964 | } |
| 18965 | if skip { |
| 18966 | neighbours.record_rect( |
| 18967 | at, |
| 18968 | bw, |
| 18969 | bh, |
| 18970 | mode, |
| 18971 | uv_predict_mode, |
| 18972 | &[ |
| 18973 | &ZERO_RESIDUAL[..bw * bh], |
| 18974 | &ZERO_RESIDUAL[..chroma_w * chroma_h], |
| 18975 | &ZERO_RESIDUAL[..chroma_w * chroma_h], |
| 18976 | ], |
| 18977 | ); |
| 18978 | } else { |
| 18979 | neighbours.record_split_luma_rect( |
| 18980 | at, |
| 18981 | bw, |
| 18982 | bh, |
| 18983 | mode, |
| 18984 | uv_predict_mode, |
| 18985 | [&last_grids[0], &last_grids[1]], |
| 18986 | ); |
| 18987 | for (plane, cu_mi, grid) in &units { |
| 18988 | neighbours.record_mi_chroma(*cu_mi, luma_span, luma_span, *plane, grid); |
| 18989 | } |
| 18990 | } |
| 18991 | // No palette exists at this size; the unconditional stamp clears any |
| 18992 | // stale neighbour palette state, as every other block path does. |
| 18993 | neighbours.record_palette_y_rect(at, bw, bh, 0, [0u16; 8]); |
| 18994 | neighbours.record_palette_uv_rect(at, bw, bh, 0, [0u16; 8]); |
| 18995 | neighbours.fill_skip_grid_rect((mi_r, mi_c), bw / MI, bh / MI, skip); |
| 18996 | neighbours.fill_lf_grid_rect( |
| 18997 | (mi_r, mi_c), |
| 18998 | bw / MI, |
| 18999 | bh / MI, |
| 19000 | logical_tx as u8, |
| 19001 | logical_tx as u8, |
| 19002 | 0, fctx, |
| 19003 | ); |
| 19004 | if in_inter || allow_intrabc_frame(fctx) { |
| 19005 | // `set_txfm_ctxs(tx_size, n4_w, n4_h, skip && is_inter, xd)` |
| 19006 | // (libaom `decodeframe.c` `decode_block`) runs for EVERY block of an |
| 19007 | // inter frame, intra ones included: an intra block stamps its |
| 19008 | // TRANSFORM size over its own footprint in both TXFM_CONTEXT bands. |
| 19009 | // A key frame reads those bands only when it allows intrabc |
| 19010 | // ([`read_block_tx_size_rect`]'s var-tx context), and then libaom |
| 19011 | // stamps here too -- lane-av1-intrabc r3. |
| 19012 | txfm_partition_update_rect( |
| 19013 | neighbours, |
| 19014 | (mi_r, mi_c), |
| 19015 | (logical_tx, logical_tx), |
| 19016 | (bw, bh), |
| 19017 | ); |
| 19018 | } |
| 19019 | if in_inter { |
| 19020 | hit_do! { |
| 19021 | INTRA128_IN_INTER_HITS.with(|c| { |
| 19022 | let mut h = c.get(); |
| 19023 | h[usize::from(bw < bh)] += 1; |
| 19024 | c.set(h); |
| 19025 | }); |
| 19026 | } |
| 19027 | } else if bw > bh { |
| 19028 | hit!(INTRA_SB128_HORZ_HITS); |
| 19029 | } else { |
| 19030 | hit!(INTRA_SB128_VERT_HITS); |
| 19031 | } |
| 19032 | Ok(()) |
| 19033 | } |
| 19034 | |
| 19035 | |
| 19036 | /// Decodes one 8x8 leaf of a straddling 16x16 block (lane-av1-rect), mirroring |
| 19037 | /// [`crate::tile::write_leaf8`]: its intra-mode context comes from the |
| 19038 | /// *enclosing* 16x16 slot's `above_mode`/`left_mode` (`outer_at`, [`SUB`]-grid), |
| 19039 | /// overridden on whichever axis `prev_leaf` sits along -- the previous leaf's |
| 19040 | /// just-decoded mode, not the enclosing slot's stale one, is the true |
| 19041 | /// neighbour there. `leaf_mi` is this leaf's own position in 4x4 mode-info |
| 19042 | /// units, which is what its coefficient context (finer than [`SUB`]) is read |
| 19043 | /// at. Returns the mode this leaf decoded, for the caller's own |
| 19044 | /// `prev_leaf`/final mode-writeback bookkeeping. |
| 19045 | #[allow(clippy::too_many_arguments)] |
| 19046 | /// The coarse 16x16 `above_mode`/`left_mode` cells a four-way SPLIT of one |
| 19047 | /// 16x16 parent leaves behind (lane-rectx r3). The next block DOWN reads |
| 19048 | /// `above_mode[col]`, and its true above neighbour is the mi directly above |
| 19049 | /// its own top-left corner -- the parent's BOTTOM-LEFT leaf, not the last one |
| 19050 | /// decoded; the next block RIGHT reads `left_mode[row]`, whose true left |
| 19051 | /// neighbour is the parent's TOP-RIGHT leaf. Writing the z-order last leaf |
| 19052 | /// (bottom-right) into both, as this path did, is right only when all four |
| 19053 | /// leaves happen to share a mode. `done` may be short at the true frame edge |
| 19054 | /// (the straddle caller filters positions off the frame), so each side falls |
| 19055 | /// back to the last leaf actually decoded. |
| 19056 | fn write_back_split_modes( |
| 19057 | neighbours: &mut Neighbours, |
| 19058 | done: &[((usize, usize), usize)], |
| 19059 | parent_mi: (usize, usize), |
| 19060 | slot: (usize, usize), |
| 19061 | ) { |
| 19062 | let (sr, sc) = slot; |
| 19063 | let Some(&(_, last)) = done.last() else { |
| 19064 | return; |
| 19065 | }; |
| 19066 | let find = |mi: (usize, usize)| done.iter().find(|&&(at, _)| at == mi).map(|&(_, m)| m); |
| 19067 | neighbours.above_mode[sc] = find((parent_mi.0 + 2, parent_mi.1)).unwrap_or(last); |
| 19068 | neighbours.left_mode[sr] = find((parent_mi.0, parent_mi.1 + 2)).unwrap_or(last); |
| 19069 | } |
| 19070 | |
| 19071 | fn decode_leaf8( |
| 19072 | dec: &mut SymbolDecoder, |
| 19073 | cdfs: &mut Cdfs, |
| 19074 | neighbours: &mut Neighbours, |
| 19075 | outer_at: (usize, usize), |
| 19076 | leaf_mi: (usize, usize), |
| 19077 | scans: (&[u16], &[u16]), |
| 19078 | prev_leaf: Option<((usize, usize), usize)>, |
| 19079 | y: &mut PlaneBuf<'static>, |
| 19080 | u: &mut PlaneBuf<'static>, |
| 19081 | v: &mut PlaneBuf<'static>, |
| 19082 | base_q_idx: u8, |
| 19083 | enable_filter_intra: bool, |
| 19084 | allow_screen_content_tools: bool, |
| 19085 | allow_intrabc: bool, |
| 19086 | tx_select: bool, |
| 19087 | reduced_tx_set: bool, fctx: &crate::decode::FrameCtx, |
| 19088 | ) -> Result<usize> { |
| 19089 | let (r, c) = outer_at; |
| 19090 | // lane-modectx r1: the group's own availability-correct fallback pair, not |
| 19091 | // the raw coarse bands (which leak across a tile edge -- see |
| 19092 | // `modes_above_left`). |
| 19093 | let (mut above_mode, mut left_mode) = neighbours.modes_above_left(r, c); |
| 19094 | if let Some(((pr, pc), pmode)) = prev_leaf { |
| 19095 | if pc == leaf_mi.1 && leaf_mi.0 == pr + 2 { |
| 19096 | above_mode = pmode; |
| 19097 | } else if pr == leaf_mi.0 && leaf_mi.1 == pc + 2 { |
| 19098 | left_mode = pmode; |
| 19099 | } |
| 19100 | } |
| 19101 | // The mi-exact neighbour wins over both the coarse 16x16 slot and |
| 19102 | // `prev_leaf`: one 16x16 slot cannot hold the two different modes its |
| 19103 | // two 8x8 (or four 4x4) columns leave behind, so a leaf whose above/left |
| 19104 | // neighbour sits in a split cell read the wrong `kf_y_mode` row. |
| 19105 | // lane-rectsplitx r2 swept this arm into |
| 19106 | // [`Neighbours::modes_above_left_mi`]: the mi-exact map ALSO has to answer |
| 19107 | // "no neighbour" at a tile edge, where the coarse cell holds the block |
| 19108 | // above/left of it inside the same 16x16. |
| 19109 | let (above_mode, left_mode) = |
| 19110 | neighbours.modes_above_left_mi(leaf_mi.0, leaf_mi.1, (above_mode, left_mode)); |
| 19111 | let smooth_neighbor = is_smooth_mode(above_mode) || is_smooth_mode(left_mode); |
| 19112 | // Chroma's edge-filter type reads the CHROMA neighbour's `uv_mode` |
| 19113 | // (`get_intra_edge_filter_type`, reconintra.c:974), never the luma mode |
| 19114 | // this leaf's `smooth_neighbor` is built from -- passing `false` here |
| 19115 | // filtered a directional chroma block's edge at the wrong strength |
| 19116 | // (lane-sub8 r5: 4 pixels of one 8x8 leaf's chroma, deltas 1..3). |
| 19117 | // The `uv_mode` neighbours are mi-exact (`uv_mode_col`/`uv_mode_row`), |
| 19118 | // with the coarse [`SUB`] slot as the fallback when no block recorded |
| 19119 | // that exact mi cell. |
| 19120 | let smooth_neighbor_uv = neighbours.smooth_uv_neighbour(leaf_mi.0, leaf_mi.1, r, c, fctx); |
| 19121 | if smooth_neighbor { |
| 19122 | hit!(SMOOTH_LUMA_HITS); |
| 19123 | } |
| 19124 | // An 8x8 leaf is well within `is_cfl_allowed`'s <=32x32 bound (spec |
| 19125 | // 5.11.5), so it reads the CFL-allowed `uv_mode_cfl` CDF, like every other |
| 19126 | // `decode_block` caller at 16x16 and up. |
| 19127 | // lane-pal8 r1: this leaf's own palette-Y mode ctx and colour cache |
| 19128 | // (`av1_get_palette_mode_ctx` / `av1_get_palette_cache`, decodemv.c:567, |
| 19129 | // `read_palette_mode_info`), at its REAL mi position -- passing |
| 19130 | // `None`/`&[]` read `palette_y_mode` off CDF row 0 and refused by name the |
| 19131 | // moment a real 8x8 palette block fired, which is where every 8-bit |
| 19132 | // `--tune-content=screen --min-partition-size=8` stream stopped. |
| 19133 | let (palette_ctx, palette_cache) = neighbours.palette_ctx_and_cache_mi(leaf_mi); |
| 19134 | let palette_uv_cache = neighbours.palette_uv_cache_mi(leaf_mi); |
| 19135 | let (skip, mode, angle_delta_y, uv_mode, angle_delta_uv, alpha, filter_intra, palette_y, palette_uv) = |
| 19136 | read_intra_mode( |
| 19137 | dec, |
| 19138 | cdfs, |
| 19139 | above_mode, |
| 19140 | left_mode, |
| 19141 | true, |
| 19142 | 8, |
| 19143 | enable_filter_intra, |
| 19144 | neighbours.skip_txfm_ctx(leaf_mi.0, leaf_mi.1), |
| 19145 | allow_screen_content_tools, |
| 19146 | allow_intrabc, |
| 19147 | Some((palette_ctx, &palette_cache)), |
| 19148 | &palette_uv_cache, |
| 19149 | leaf_mi.0, |
| 19150 | leaf_mi.1, fctx, |
| 19151 | )?; |
| 19152 | // lane-kf900 r7: an 8x8 leaf consumed `use_intrabc` (and its DV) through |
| 19153 | // `read_intra_mode` and then decoded the block as if it were ordinary |
| 19154 | // intra. libaom's `is_inter_block` counts an intrabc block |
| 19155 | // (`blockd.h:373`), so its luma `tx_type` comes off the INTER ext-tx set |
| 19156 | // (`av1_get_ext_tx_set_type(TX_8X8, is_inter=1, 0)` = `EXT_TX_SET_ALL16`, |
| 19157 | // a 16-symbol CDF) while we read the 7-symbol `TX_SET_INTRA_1` row: the |
| 19158 | // same value with a different interval, so the tile desynced at the very |
| 19159 | // next symbol (class `wrong-alphabet-same-value`). Everything below |
| 19160 | // mirrors `decode_block`'s own intrabc handling at 16x16 and up. |
| 19161 | let intrabc_dv = fctx.intrabc_dv.with(|c| c.take()); |
| 19162 | if intrabc_dv.is_some() { |
| 19163 | hit!(LEAF8_INTRABC_HITS); |
| 19164 | } |
| 19165 | // `av1_visit_palette`'s reconstruction (decodeframe.c:1135): the colour |
| 19166 | // index map, mapped through the base colours, IS the block's prediction -- |
| 19167 | // carried to the next `reconstruct`/`read_plane` through the same |
| 19168 | // [`PALETTE_PRED`] slot [`decode_block`] uses at 16x16 and up. |
| 19169 | let palette_y_buf: Option<Vec<u16>> = palette_y |
| 19170 | .as_ref() |
| 19171 | .map(|p| p.map.iter().map(|&i| p.colors[i as usize]).collect()); |
| 19172 | let palette_uv_bufs: Option<(Vec<u16>, Vec<u16>)> = palette_uv.as_ref().map(|p| { |
| 19173 | ( |
| 19174 | p.map.iter().map(|&i| p.u_colors[i as usize]).collect(), |
| 19175 | p.map.iter().map(|&i| p.v_colors[i as usize]).collect(), |
| 19176 | ) |
| 19177 | }); |
| 19178 | let (pal_y_size, pal_y_colors) = |
| 19179 | palette_y.as_ref().map_or((0, [0u16; 8]), |p| (p.size, p.colors)); |
| 19180 | let (pal_uv_size, pal_uv_colors) = |
| 19181 | palette_uv.as_ref().map_or((0, [0u16; 8]), |p| (p.size, p.u_colors)); |
| 19182 | if palette_y.is_some() || palette_uv.is_some() { |
| 19183 | hit!(PALETTE_LEAF8_HITS); |
| 19184 | } |
| 19185 | let uv_predict_mode = if uv_mode == UV_CFL_PRED { |
| 19186 | DC_PRED |
| 19187 | } else { |
| 19188 | uv_mode |
| 19189 | }; |
| 19190 | neighbours.above_uv_mode[c] = uv_predict_mode; |
| 19191 | neighbours.left_uv_mode[r] = uv_predict_mode; |
| 19192 | neighbours.record_uv_mode_mi(leaf_mi.0, leaf_mi.1, 2, 2, uv_predict_mode); |
| 19193 | // `TxMode::Select`'s `tx_depth` at an 8x8 leaf (spec 5.11.16): the only |
| 19194 | // depths an 8x8 block offers are `TX8` (depth 0) and `TX4` (depth 1, |
| 19195 | // which splits the leaf's own 8x8 prediction into a 2x2 grid of 4x4 |
| 19196 | // transform units, same raster-order per-TU pattern `decode_block`'s |
| 19197 | // multi-TU branch runs). |
| 19198 | // ... and an intrabc leaf reads no intra `tx_depth` symbol at all: libaom |
| 19199 | // dispatches `read_block_tx_size` on `is_inter_block`, which counts an |
| 19200 | // intrabc block, so a SKIPPED one takes the largest transform and an |
| 19201 | // UNSKIPPED one reads the inter var-tx tree (lane-t900 r32). At 8x8 that |
| 19202 | // tree is one `txfm_split` symbol: TX_8X8, or TX_4X4 over all four mi |
| 19203 | // cells (`read_var_tx_size`'s `sub_txs == TX_4X4` early return). |
| 19204 | // `parse_decode_block`'s tree condition carries |
| 19205 | // `!xd->lossless[mbmi->segment_id]` (see [`decode_block`]'s own copy): |
| 19206 | // a lossless segment's intrabc leaf reads no tree, TX_4X4 via |
| 19207 | // `read_tx_size`'s first line (lane-av1mixloss). |
| 19208 | let resolved = if tx_select && intrabc_dv.is_some() && !skip && !lossless(fctx) { |
| 19209 | let mut leaves = Vec::new(); |
| 19210 | let mi_cols = 2 * y.true_width.div_ceil(8); |
| 19211 | let mi_rows = 2 * y.true_height.div_ceil(8); |
| 19212 | read_var_tx_size( |
| 19213 | dec, |
| 19214 | cdfs, |
| 19215 | neighbours, |
| 19216 | leaf_mi, |
| 19217 | 8, |
| 19218 | ( |
| 19219 | 2usize.min(mi_cols.saturating_sub(leaf_mi.1)), |
| 19220 | 2usize.min(mi_rows.saturating_sub(leaf_mi.0)), |
| 19221 | ), |
| 19222 | (8, 8), |
| 19223 | 0, |
| 19224 | 0, |
| 19225 | 0, |
| 19226 | &mut leaves, |
| 19227 | ); |
| 19228 | hit!(INTRABC_VARTX_HITS); |
| 19229 | leaves.first().map_or(8, |l| l.2) |
| 19230 | } else if tx_select && intrabc_dv.is_none() { |
| 19231 | read_tx_size(dec, cdfs, neighbours, leaf_mi, 8, fctx) |
| 19232 | } else { |
| 19233 | // lane-lossless: TX_4X4 on a lossless frame, no symbol |
| 19234 | // (libaom `read_tx_size`). |
| 19235 | lossless_tx(8, fctx) |
| 19236 | }; |
| 19237 | let (px, py) = (leaf_mi.1 * MI, leaf_mi.0 * MI); |
| 19238 | let reach = Reach::of(8, px, py, y.width, y.height, fctx); |
| 19239 | let (cpx, cpy) = (px >> ss_x(fctx), py >> ss_y(fctx)); |
| 19240 | // 4:4:4: this 8x8 block's chroma plane block is 8x8 (same scan as luma, |
| 19241 | // `scans.0`). 4:2:0 stays the 4x4 unit this leaf was written for. |
| 19242 | // lane-av1-422d: 4:2:2 (ss 1/0) halves only the width, so the plane block |
| 19243 | // is 4x8 -- ONE rect transform unit per plane (`av1_get_max_uv_txsize( |
| 19244 | // BLOCK_8X8)` at ss (1,0) is `max_txsize_rect_lookup[BLOCK_4X8]` == |
| 19245 | // TX_4X8): one txb_skip + one scan per plane in the |
| 19246 | // [`TxbSet::ChromaRect8x4`] class [`decode_rect_split`] proves for the |
| 19247 | // shape, read through [`leaf8_chroma422_unit`] in each arm below. The |
| 19248 | // `(csz, cset, cscan)` selection keeps serving the square arms (444's |
| 19249 | // 8x8, 420's 4x4); the rect arms branch on [`chroma_422`] instead. |
| 19250 | let chroma_444 = ss_x(fctx) == 0 && ss_y(fctx) == 0; |
| 19251 | let chroma_422 = ss_x(fctx) == 1 && ss_y(fctx) == 0; |
| 19252 | let (csz, cset, cscan) = if chroma_444 { |
| 19253 | (8usize, TxbSet::Chroma8, scans.0) |
| 19254 | } else { |
| 19255 | (4usize, TxbSet::Chroma4, scans.1) |
| 19256 | }; |
| 19257 | // spec 7.11.3.4 with `use_intrabc`, same as `decode_block`: predict from |
| 19258 | // the CURRENT frame's own pre-loop-filter reconstruction (every loop |
| 19259 | // filter is off whenever `allow_intrabc`, frame.rs:194/210/252/272) at |
| 19260 | // the full-pel block vector, with the BILINEAR kernel libaom forces. |
| 19261 | flush_recon(fctx, y, u, v); |
| 19262 | let intrabc_bufs = intrabc_dv.map(|(dv_row, dv_col)| { |
| 19263 | let mut yb = vec![0u16; 8 * 8]; |
| 19264 | mc::predict_with_filter( |
| 19265 | &y.data, y.width, y.true_width, y.true_height, |
| 19266 | mv_to_q4(px, dv_col, 0), mv_to_q4(py, dv_row, 0), |
| 19267 | 8, 8, mc::InterpFilterKind::Bilinear, &mut yb, fctx, |
| 19268 | ); |
| 19269 | // The chroma extent is THIS frame's subsampled size of the 8x8 luma |
| 19270 | // leaf (`get_plane_block_size`): 4x4 at 4:2:0, 4x8 at 4:2:2, 8x8 at |
| 19271 | // 4:4:4. The literal 4x4 this replaces hardcoded the 4:2:0 halving -- |
| 19272 | // at 4:4:4 the 16-sample override fed this leaf's own 8x8 chroma |
| 19273 | // reconstruct, which indexed `prediction[16]` of a 16-sample buffer |
| 19274 | // (len 16, index 16 at `PlaneBuf::reconstruct`), and at 4:2:2 it |
| 19275 | // covered half the leaf's 4x8 chroma plane block (lane-av1-422d's |
| 19276 | // `if chroma_422 { (4, 8) }` arm, which the ss-derived form subsumes: |
| 19277 | // `8 >> ss_x` is 4 and `8 >> ss_y` is 8 there). The same buffers |
| 19278 | // are windowed at stride 8 by `read_intra_chroma_lossless` (the TX4 |
| 19279 | // 4:4:4-lossless arm) and consumed whole by the TX8 and skip arms, |
| 19280 | // so all three heal with this one sizing. |
| 19281 | let (cw, ch) = (8 >> ss_x(fctx), 8 >> ss_y(fctx)); |
| 19282 | let mut ub = vec![0u16; cw * ch]; |
| 19283 | mc::predict_with_filter( |
| 19284 | &u.data, u.width, u.true_width, u.true_height, |
| 19285 | mv_to_q4(cpx, dv_col, ss_x(fctx)), mv_to_q4(cpy, dv_row, ss_y(fctx)), |
| 19286 | cw, ch, mc::InterpFilterKind::Bilinear, &mut ub, fctx, |
| 19287 | ); |
| 19288 | let mut vb = vec![0u16; cw * ch]; |
| 19289 | mc::predict_with_filter( |
| 19290 | &v.data, v.width, v.true_width, v.true_height, |
| 19291 | mv_to_q4(cpx, dv_col, ss_x(fctx)), mv_to_q4(cpy, dv_row, ss_y(fctx)), |
| 19292 | cw, ch, mc::InterpFilterKind::Bilinear, &mut vb, fctx, |
| 19293 | ); |
| 19294 | (yb, ub, vb) |
| 19295 | }); |
| 19296 | // The per-plane prediction override rides the same [`PALETTE_PRED`] slot |
| 19297 | // `decode_block` uses: set fresh right before each plane's own |
| 19298 | // `reconstruct`/`read_plane` call (the slot is taken, not copied). |
| 19299 | // lane-t900 r32: through `palette_y_buf`, so the 4x4 TU loop below windows |
| 19300 | // it per unit (an intrabc leaf whose var-tx tree split to TX_4X4 is the |
| 19301 | // first shape that reaches that loop with an override set). |
| 19302 | let palette_y_buf = match &intrabc_bufs { |
| 19303 | Some((yb, _, _)) => Some(yb.clone()), |
| 19304 | None => palette_y_buf, |
| 19305 | }; |
| 19306 | // lane-av1intrapred: the chroma twin of the `palette_y_buf` wrap above -- |
| 19307 | // `decode_block` does the same for its own `palette_uv_bufs` |
| 19308 | // (decode.rs's single-TU and multi-TU chroma reads). Without it, an |
| 19309 | // intrabc leaf whose var-tx tree split to TX_4X4 reached the TX4 arm's |
| 19310 | // chroma reads with the override unset: the unit predicted INTRA from |
| 19311 | // the UV mode (intrabc's is DC) instead of copying the frame at the DV, |
| 19312 | // R5 first differing pixel U(184,4), block mi (2,92). |
| 19313 | let palette_uv_bufs = match &intrabc_bufs { |
| 19314 | Some((_, ub, vb)) => Some((ub.clone(), vb.clone())), |
| 19315 | None => palette_uv_bufs, |
| 19316 | }; |
| 19317 | let (luma_grid, u_grid, v_grid); |
| 19318 | if skip { |
| 19319 | // lane-hgkf r2 (same shape as `decode_block`'s own skip branch): an |
| 19320 | // 8x8 leaf whose `tx_depth` resolved to TX_4X4 predicts FOUR 4x4 |
| 19321 | // transform units, each off the units already written left/above of |
| 19322 | // it inside this same leaf -- an intra block's transform size is read |
| 19323 | // whether or not it is skipped, and spec 7.11.2 predicts per unit. |
| 19324 | // A palette block's prediction is the whole-block index map, never |
| 19325 | // per-transform-unit edges ([`decode_block`]'s own `split_tx_skip` |
| 19326 | // rule) -- so a palette leaf takes the single 8x8 reconstruct below |
| 19327 | // even when `tx_depth` resolved to TX_4X4. |
| 19328 | if resolved == 4 && palette_y_buf.is_none() { |
| 19329 | hit!(SKIP_SPLIT_TX_HITS); |
| 19330 | for tu_row in 0..2 { |
| 19331 | for tu_col in 0..2 { |
| 19332 | let (tu_px, tu_py) = (px + tu_col * 4, py + tu_row * 4); |
| 19333 | if tu_px >= y.true_width || tu_py >= y.true_height { |
| 19334 | continue; |
| 19335 | } |
| 19336 | let tu_reach = tu_reach( |
| 19337 | 8, |
| 19338 | 8, |
| 19339 | tu_col * 4, |
| 19340 | tu_row * 4, |
| 19341 | 4, |
| 19342 | reach, |
| 19343 | px, |
| 19344 | py, |
| 19345 | y.width, |
| 19346 | y.height, fctx, |
| 19347 | ); |
| 19348 | push_intra(0, |
| 19349 | tu_px, |
| 19350 | tu_py, |
| 19351 | 4, |
| 19352 | mode, |
| 19353 | angle_delta_y, |
| 19354 | tu_reach, |
| 19355 | &ZERO_RESIDUAL[..16], |
| 19356 | None, |
| 19357 | filter_intra, |
| 19358 | smooth_neighbor, fctx, |
| 19359 | ); |
| 19360 | } |
| 19361 | } |
| 19362 | } else { |
| 19363 | if let Some(buf) = &palette_y_buf { |
| 19364 | set_palette_pred(buf.clone(), fctx); |
| 19365 | } |
| 19366 | push_intra(0, |
| 19367 | px, |
| 19368 | py, |
| 19369 | 8, |
| 19370 | mode, |
| 19371 | angle_delta_y, |
| 19372 | reach, |
| 19373 | &ZERO_RESIDUAL[..64], |
| 19374 | None, |
| 19375 | filter_intra, |
| 19376 | smooth_neighbor, fctx, |
| 19377 | ); |
| 19378 | } |
| 19379 | let ac = alpha.map(|_| cfl_src(px, py, 8)); |
| 19380 | // Three skip-arm shapes, dispatched on THIS frame's subsampling. |
| 19381 | // lane-av1-422d: at 4:2:2 a skipped leaf still predicts its 4x8 rect |
| 19382 | // unit per plane (CfL keeps its contribution, [`decode_block`]'s own |
| 19383 | // skip-arm rule); no coefficient symbol exists, so the entropy |
| 19384 | // context never enters the rect read. |
| 19385 | if chroma_422 { |
| 19386 | u_grid = leaf8_chroma422_unit( |
| 19387 | dec, cdfs, (false, false, 0), uv_predict_mode, angle_delta_uv, reach, |
| 19388 | 1, alpha.zip(ac).map(|((au, _), ac)| (au, ac)), true, |
| 19389 | intrabc_bufs.as_ref().map(|(_, ub, _)| ub.clone()) |
| 19390 | .or_else(|| palette_uv_bufs.as_ref().map(|(ub, _)| ub.clone())), |
| 19391 | u, cpx, cpy, smooth_neighbor_uv, fctx, |
| 19392 | )?; |
| 19393 | v_grid = leaf8_chroma422_unit( |
| 19394 | dec, cdfs, (false, false, 0), uv_predict_mode, angle_delta_uv, reach, |
| 19395 | 2, alpha.zip(ac).map(|((_, av), ac)| (av, ac)), true, |
| 19396 | intrabc_bufs.as_ref().map(|(_, _, vb)| vb.clone()) |
| 19397 | .or_else(|| palette_uv_bufs.as_ref().map(|(_, vb)| vb.clone())), |
| 19398 | v, cpx, cpy, smooth_neighbor_uv, fctx, |
| 19399 | )?; |
| 19400 | luma_grid = Grid::Zero(64); |
| 19401 | } else { |
| 19402 | // 4:4:4 lossless: the leaf's chroma plane block is a 2x2 raster of |
| 19403 | // TX_4X4 units (`av1_get_tx_size`: lossless forces TX_4X4 on EVERY |
| 19404 | // plane), so a skipped leaf predicts per unit -- each from its own |
| 19405 | // edges -- like the luma `SKIP_SPLIT_TX` loop above. CfL never fires |
| 19406 | // here (`is_cfl_allowed` at lossless needs plane_bsize == 4x4; this |
| 19407 | // chroma plane block is 8x8), and intrabc/palette keep the |
| 19408 | // whole-plane-block prediction they already run. The guard also |
| 19409 | // requires `chroma_444`, so it can never reach the 4:2:2 arm above. |
| 19410 | let chroma_units_444 = chroma_444 |
| 19411 | && lossless(fctx) |
| 19412 | && intrabc_bufs.is_none() |
| 19413 | && palette_uv_bufs.is_none(); |
| 19414 | if let Some((_, ub, _)) = &intrabc_bufs { |
| 19415 | set_palette_pred(ub.clone(), fctx); |
| 19416 | } else if let Some((ub, _)) = &palette_uv_bufs { |
| 19417 | set_palette_pred(ub.clone(), fctx); |
| 19418 | } |
| 19419 | if chroma_units_444 { |
| 19420 | for cu_row in 0..2 { |
| 19421 | for cu_col in 0..2 { |
| 19422 | let cu_reach = tu_reach( |
| 19423 | 8, 8, cu_col * 4, cu_row * 4, 4, |
| 19424 | reach, px, py, y.width, y.height, fctx, |
| 19425 | ); |
| 19426 | push_intra(1, |
| 19427 | cpx + cu_col * 4, |
| 19428 | cpy + cu_row * 4, |
| 19429 | 4, |
| 19430 | uv_predict_mode, |
| 19431 | angle_delta_uv, |
| 19432 | cu_reach, |
| 19433 | &ZERO_RESIDUAL[..16], |
| 19434 | None, |
| 19435 | None, |
| 19436 | smooth_neighbor_uv, fctx, |
| 19437 | ); |
| 19438 | } |
| 19439 | } |
| 19440 | } else { |
| 19441 | push_intra(1, |
| 19442 | cpx, |
| 19443 | cpy, |
| 19444 | csz, |
| 19445 | uv_predict_mode, |
| 19446 | angle_delta_uv, |
| 19447 | reach, |
| 19448 | &ZERO_RESIDUAL[..csz * csz], |
| 19449 | alpha.zip(ac).map(|((au, _), ac)| (au, ac)), |
| 19450 | None, |
| 19451 | smooth_neighbor_uv, fctx, |
| 19452 | ); |
| 19453 | } |
| 19454 | if let Some((_, _, vb)) = &intrabc_bufs { |
| 19455 | set_palette_pred(vb.clone(), fctx); |
| 19456 | } else if let Some((_, vb)) = &palette_uv_bufs { |
| 19457 | set_palette_pred(vb.clone(), fctx); |
| 19458 | } |
| 19459 | if chroma_units_444 { |
| 19460 | for cu_row in 0..2 { |
| 19461 | for cu_col in 0..2 { |
| 19462 | let cu_reach = tu_reach( |
| 19463 | 8, 8, cu_col * 4, cu_row * 4, 4, |
| 19464 | reach, px, py, y.width, y.height, fctx, |
| 19465 | ); |
| 19466 | push_intra(2, |
| 19467 | cpx + cu_col * 4, |
| 19468 | cpy + cu_row * 4, |
| 19469 | 4, |
| 19470 | uv_predict_mode, |
| 19471 | angle_delta_uv, |
| 19472 | cu_reach, |
| 19473 | &ZERO_RESIDUAL[..16], |
| 19474 | None, |
| 19475 | None, |
| 19476 | smooth_neighbor_uv, fctx, |
| 19477 | ); |
| 19478 | } |
| 19479 | } |
| 19480 | } else { |
| 19481 | push_intra(2, |
| 19482 | cpx, |
| 19483 | cpy, |
| 19484 | csz, |
| 19485 | uv_predict_mode, |
| 19486 | angle_delta_uv, |
| 19487 | reach, |
| 19488 | &ZERO_RESIDUAL[..csz * csz], |
| 19489 | alpha.zip(ac).map(|((_, av), ac)| (av, ac)), |
| 19490 | None, |
| 19491 | smooth_neighbor_uv, fctx, |
| 19492 | ); |
| 19493 | } |
| 19494 | luma_grid = Grid::Zero(64); |
| 19495 | u_grid = Grid::Zero(csz * csz); |
| 19496 | v_grid = Grid::Zero(csz * csz); |
| 19497 | } |
| 19498 | } else if resolved != 4 { |
| 19499 | let around = neighbours.around_mi(leaf_mi, 8); |
| 19500 | // lane-av1-422h: the 4:2:2 chroma rect unit's `get_txb_ctx_general` |
| 19501 | // reads its above votes over CHROMA 4-px cells -- at ss_x 1 one chroma |
| 19502 | // cell spans TWO luma mi columns, so the per-luma-mi sum counts each |
| 19503 | // above chroma column twice and `dc_sign_ctx`'s signum flips whenever |
| 19504 | // the doubled sum crosses zero (the same class |
| 19505 | // [`Neighbours::around_mi_422_chroma`] fixes for the rect readers; |
| 19506 | // measured at mi(2,24) plane 2: ours dc_sign row 0, oracle row 2). |
| 19507 | // The left extent is unsubsquared at ss_y 0 and stays one-to-one. |
| 19508 | let chroma_around = if chroma_422 { |
| 19509 | neighbours.around_mi_422_chroma(leaf_mi, 8, 8) |
| 19510 | } else { |
| 19511 | around |
| 19512 | }; |
| 19513 | if let Some(buf) = &palette_y_buf { |
| 19514 | set_palette_pred(buf.clone(), fctx); |
| 19515 | } |
| 19516 | luma_grid = read_plane( |
| 19517 | dec, |
| 19518 | cdfs, |
| 19519 | if intrabc_dv.is_some() { |
| 19520 | inter_txbset_for(8, reduced_tx_set) |
| 19521 | } else if reduced_tx_set { |
| 19522 | TxbSet::Luma8 |
| 19523 | } else { |
| 19524 | TxbSet::Luma8Set1 |
| 19525 | }, |
| 19526 | scans.0, |
| 19527 | 0, |
| 19528 | around[0], |
| 19529 | mode, |
| 19530 | mode, |
| 19531 | angle_delta_y, |
| 19532 | reach, |
| 19533 | y, |
| 19534 | px, |
| 19535 | py, |
| 19536 | 8, |
| 19537 | TX8, |
| 19538 | base_q_idx, |
| 19539 | None, |
| 19540 | filter_intra, |
| 19541 | None, |
| 19542 | smooth_neighbor, fctx, |
| 19543 | )?; |
| 19544 | // `av1_get_tx_type` branches `PLANE_TYPE_UV` on `is_inter_block`, so |
| 19545 | // an intrabc block's chroma tx_type is the colocated LUMA transform's |
| 19546 | // coded type (lane-kf900 r6's own fix, `decode_block`'s line). |
| 19547 | if intrabc_dv.is_some() { |
| 19548 | fctx.intrabc_chroma_tx.with(|c| c.set(Some(fctx.luma_tx_type.with(std::cell::Cell::get)))); |
| 19549 | } |
| 19550 | let ac = alpha.map(|_| cfl_src(px, py, 8)); |
| 19551 | if chroma_422 { |
| 19552 | // lane-av1-422d: ONE 4x8 rect unit per plane (`av1_get_max_uv_txsize` |
| 19553 | // at ss (1,0) == TX_4X8), covering the plane block exactly, off the |
| 19554 | // leaf's own 8x8-span entropy context (`chroma_tiled`'s convention). |
| 19555 | u_grid = leaf8_chroma422_unit( |
| 19556 | dec, cdfs, chroma_around[1], uv_predict_mode, angle_delta_uv, reach, |
| 19557 | 1, alpha.zip(ac).map(|((au, _), ac)| (au, ac)), false, |
| 19558 | intrabc_bufs.as_ref().map(|(_, ub, _)| ub.clone()) |
| 19559 | .or_else(|| palette_uv_bufs.as_ref().map(|(ub, _)| ub.clone())), |
| 19560 | u, cpx, cpy, smooth_neighbor_uv, fctx, |
| 19561 | )?; |
| 19562 | v_grid = leaf8_chroma422_unit( |
| 19563 | dec, cdfs, chroma_around[2], uv_predict_mode, angle_delta_uv, reach, |
| 19564 | 2, alpha.zip(ac).map(|((_, av), ac)| (av, ac)), false, |
| 19565 | intrabc_bufs.as_ref().map(|(_, _, vb)| vb.clone()) |
| 19566 | .or_else(|| palette_uv_bufs.as_ref().map(|(_, vb)| vb.clone())), |
| 19567 | v, cpx, cpy, smooth_neighbor_uv, fctx, |
| 19568 | )?; |
| 19569 | } else { |
| 19570 | if let Some((_, ub, _)) = &intrabc_bufs { |
| 19571 | set_palette_pred(ub.clone(), fctx); |
| 19572 | } else if let Some((ub, _)) = &palette_uv_bufs { |
| 19573 | set_palette_pred(ub.clone(), fctx); |
| 19574 | } |
| 19575 | u_grid = read_plane( |
| 19576 | dec, |
| 19577 | cdfs, |
| 19578 | cset, |
| 19579 | cscan, |
| 19580 | 1, |
| 19581 | around[1], |
| 19582 | mode, |
| 19583 | uv_predict_mode, |
| 19584 | angle_delta_uv, |
| 19585 | reach, |
| 19586 | u, |
| 19587 | cpx, |
| 19588 | cpy, |
| 19589 | csz, |
| 19590 | csz, |
| 19591 | base_q_idx, |
| 19592 | alpha.zip(ac).map(|((au, _), ac)| (au, ac)), |
| 19593 | None, |
| 19594 | None, |
| 19595 | smooth_neighbor_uv, fctx, |
| 19596 | )?; |
| 19597 | if let Some((_, _, vb)) = &intrabc_bufs { |
| 19598 | set_palette_pred(vb.clone(), fctx); |
| 19599 | } else if let Some((_, vb)) = &palette_uv_bufs { |
| 19600 | set_palette_pred(vb.clone(), fctx); |
| 19601 | } |
| 19602 | v_grid = read_plane( |
| 19603 | dec, |
| 19604 | cdfs, |
| 19605 | cset, |
| 19606 | cscan, |
| 19607 | 2, |
| 19608 | around[2], |
| 19609 | mode, |
| 19610 | uv_predict_mode, |
| 19611 | angle_delta_uv, |
| 19612 | reach, |
| 19613 | v, |
| 19614 | cpx, |
| 19615 | cpy, |
| 19616 | csz, |
| 19617 | csz, |
| 19618 | base_q_idx, |
| 19619 | alpha.zip(ac).map(|((_, av), ac)| (av, ac)), |
| 19620 | None, |
| 19621 | None, |
| 19622 | smooth_neighbor_uv, fctx, |
| 19623 | )?; |
| 19624 | } |
| 19625 | // Disarm: `INTRABC_CHROMA_TX` is `Some` across exactly this block's |
| 19626 | // two chroma reads (`decode_block`'s own tail does the same) -- left |
| 19627 | // set, every later chroma block would inherit this luma tx_type, |
| 19628 | // which picks the scan and the inverse transform. |
| 19629 | fctx.intrabc_chroma_tx.with(|c| c.set(None)); |
| 19630 | } else { |
| 19631 | // `tx_depth` resolved this leaf's luma to `TX4`: a 2x2 raster-order |
| 19632 | // grid of 4x4 transform units, each predicted from the leaf's own |
| 19633 | // 8x8 `mode` and reading its own fresh context off the earlier |
| 19634 | // units in this same leaf (the same per-TU pattern `decode_block`'s |
| 19635 | // multi-TU branch runs at bigger blocks). |
| 19636 | let mut first_leaf_tx: Option<TxType> = None; |
| 19637 | for tu_row in 0..2 { |
| 19638 | for tu_col in 0..2 { |
| 19639 | let tu_mi = (leaf_mi.0 + tu_row, leaf_mi.1 + tu_col); |
| 19640 | let tu_px = px + tu_col * 4; |
| 19641 | let tu_py = py + tu_row * 4; |
| 19642 | let tu_around = neighbours.around_mi(tu_mi, 4)[0]; |
| 19643 | if crate::encode::Reach::in_vert_ab() { |
| 19644 | hit!(VERT_AB_TX4_HITS); |
| 19645 | } |
| 19646 | let tu_reach = tu_reach( |
| 19647 | 8, |
| 19648 | 8, |
| 19649 | tu_col * 4, |
| 19650 | tu_row * 4, |
| 19651 | 4, |
| 19652 | reach, |
| 19653 | px, |
| 19654 | py, |
| 19655 | y.width, |
| 19656 | y.height, fctx, |
| 19657 | ); |
| 19658 | let tu_skip_ctx = neighbours.luma_skip_ctx(tu_mi, 1); |
| 19659 | // Each 4x4 unit predicts from its own window of the block's |
| 19660 | // 8x8 colour-index map ([`decode_block`]'s per-TU slice at |
| 19661 | // 16x16 and up) -- the whole-block buffer would be taken by |
| 19662 | // the FIRST unit and indexed at the wrong stride. |
| 19663 | if let Some(buf) = &palette_y_buf { |
| 19664 | hit!(PALETTE_SPLIT_TX_HITS); |
| 19665 | let mut window = vec![0u16; 16]; |
| 19666 | for row in 0..4 { |
| 19667 | let src = (tu_row * 4 + row) * 8 + tu_col * 4; |
| 19668 | window[row * 4..][..4].copy_from_slice(&buf[src..][..4]); |
| 19669 | } |
| 19670 | set_palette_pred(window, fctx); |
| 19671 | } |
| 19672 | let tu_grid = read_plane( |
| 19673 | dec, |
| 19674 | cdfs, |
| 19675 | if intrabc_dv.is_some() { |
| 19676 | inter_txbset_for(4, reduced_tx_set) |
| 19677 | } else if reduced_tx_set { |
| 19678 | TxbSet::Luma4 |
| 19679 | } else { |
| 19680 | TxbSet::Luma4Set1 |
| 19681 | }, |
| 19682 | scans.1, |
| 19683 | 0, |
| 19684 | tu_around, |
| 19685 | mode, |
| 19686 | mode, |
| 19687 | angle_delta_y, |
| 19688 | tu_reach, |
| 19689 | y, |
| 19690 | tu_px, |
| 19691 | tu_py, |
| 19692 | 4, |
| 19693 | 4, |
| 19694 | base_q_idx, |
| 19695 | None, |
| 19696 | filter_intra, |
| 19697 | Some(tu_skip_ctx), |
| 19698 | smooth_neighbor, fctx, |
| 19699 | )?; |
| 19700 | if crate::envflags::env_flag!("EC_AV1_TRACE") { |
| 19701 | eprintln!( |
| 19702 | "TRACE tu_bitpos mi=({},{}) tell={}", |
| 19703 | tu_mi.0, |
| 19704 | tu_mi.1, |
| 19705 | dec.debug_bitpos() |
| 19706 | ); |
| 19707 | } |
| 19708 | neighbours.record_mi_luma(tu_mi, 4, &tu_grid); |
| 19709 | // The FIRST (top-left, co-located with this group's chroma) |
| 19710 | // TU's coded tx_type is what the group's chroma inherits -- |
| 19711 | // the same capture [`decode_block`]'s multi-TU branch makes |
| 19712 | // for `first_leaf_tx` (libaom `av1_get_tx_type`, blockd.h: |
| 19713 | // an inter-classified block's `PLANE_TYPE_UV` reads |
| 19714 | // `tx_type_map`, and intrabc is inter-classified). |
| 19715 | if first_leaf_tx.is_none() { |
| 19716 | first_leaf_tx = Some(fctx.luma_tx_type.with(std::cell::Cell::get)); |
| 19717 | } |
| 19718 | } |
| 19719 | } |
| 19720 | // lane-av1intrapred: an intrabc leaf whose var-tx tree split to |
| 19721 | // TX_4X4 never armed [`INTRABC_CHROMA_TX`], so its chroma reads fell |
| 19722 | // back to `default_intra_tx_type(DC_PRED)` = `DCT_DCT` and took the |
| 19723 | // 2D scan, the 2D eob row and the 2D nz contexts where libaom takes |
| 19724 | // the inherited luma type's (R5's first fork: mi (36,66), luma |
| 19725 | // (0,0) coded `H_ADST` off the 16-symbol inter set, chroma U then |
| 19726 | // decoded its `eob` from the wrong CDF row). Same arm the |
| 19727 | // `resolved != 4` branch above runs for its whole-leaf unit. |
| 19728 | if intrabc_dv.is_some() { |
| 19729 | if let Some(t) = first_leaf_tx { |
| 19730 | fctx.intrabc_chroma_tx.with(|c| c.set(Some(t))); |
| 19731 | } |
| 19732 | } |
| 19733 | let ac = alpha.map(|_| cfl_src(px, py, 8)); |
| 19734 | // lane-av1-422h: same ss_x-1 dc-vote sampling as the whole-leaf arm |
| 19735 | // above -- the doubled above sum crosses zero at mi(2,24) plane 2. |
| 19736 | let chroma_around = if chroma_422 { |
| 19737 | neighbours.around_mi_422_chroma(leaf_mi, 8, 8) |
| 19738 | } else { |
| 19739 | neighbours.around_mi(leaf_mi, 8) |
| 19740 | }; |
| 19741 | // lane-av1intrapred: the non-vacuity counter -- armed slot consulted |
| 19742 | // by this arm's chroma reads means the frame-copy + inherited-type |
| 19743 | // route ran; zero at gate time means the fixture stopped reaching it. |
| 19744 | if fctx.intrabc_chroma_tx.with(std::cell::Cell::get).is_some() { |
| 19745 | hit!(INTRABC_TX4_CHROMA_COPY_HITS); |
| 19746 | } |
| 19747 | if chroma_422 { |
| 19748 | // lane-av1-422d: the chroma unit does NOT follow the luma var-tx |
| 19749 | // split -- `uv_txsize` is `av1_get_max_uv_txsize` of the block, |
| 19750 | // still the one 4x8 rect unit per plane at ss (1,0). |
| 19751 | u_grid = leaf8_chroma422_unit( |
| 19752 | dec, cdfs, chroma_around[1], uv_predict_mode, angle_delta_uv, reach, |
| 19753 | 1, alpha.zip(ac).map(|((au, _), ac)| (au, ac)), false, |
| 19754 | intrabc_bufs.as_ref().map(|(_, ub, _)| ub.clone()) |
| 19755 | .or_else(|| palette_uv_bufs.as_ref().map(|(ub, _)| ub.clone())), |
| 19756 | u, cpx, cpy, smooth_neighbor_uv, fctx, |
| 19757 | )?; |
| 19758 | v_grid = leaf8_chroma422_unit( |
| 19759 | dec, cdfs, chroma_around[2], uv_predict_mode, angle_delta_uv, reach, |
| 19760 | 2, alpha.zip(ac).map(|((_, av), ac)| (av, ac)), false, |
| 19761 | intrabc_bufs.as_ref().map(|(_, _, vb)| vb.clone()) |
| 19762 | .or_else(|| palette_uv_bufs.as_ref().map(|(_, vb)| vb.clone())), |
| 19763 | v, cpx, cpy, smooth_neighbor_uv, fctx, |
| 19764 | )?; |
| 19765 | } else if chroma_444 && lossless(fctx) { |
| 19766 | // 4:4:4 lossless: the leaf's chroma plane block is a 2x2 raster |
| 19767 | // of TX_4X4 units per plane, PLANE-MAJOR (`av1_get_tx_size`: |
| 19768 | // lossless forces TX_4X4 on EVERY plane) -- the same walk |
| 19769 | // `read_intra_chroma_lossless` runs for whole blocks. Reading one |
| 19770 | // TX_8X8 unit here panicked before: the lossless inverse is the |
| 19771 | // 4x4-only WHT (`TxParams::run`'s own `(w, h) == (4, 4)` |
| 19772 | // invariant), and the unit count desyncs the tile either way. |
| 19773 | // Per-unit `record_mi_chroma` calls stamp the neighbour bands |
| 19774 | // inside, which is why the tail's whole-leaf chroma stamp below |
| 19775 | // is skipped for this shape. |
| 19776 | let (mut uo, mut vo) = (Vec::new(), Vec::new()); |
| 19777 | read_intra_chroma_lossless( |
| 19778 | dec, cdfs, neighbours, u, v, leaf_mi, (px, py), (cpx, cpy), |
| 19779 | 8, (8, 8), (0, 0), (8, 8), 8, reach, (y.width, y.height), mode, |
| 19780 | uv_predict_mode, angle_delta_uv, |
| 19781 | palette_uv_bufs.as_ref().map(|(a, b)| (&a[..], &b[..])), |
| 19782 | smooth_neighbor_uv, base_q_idx, &mut uo, &mut vo, fctx, |
| 19783 | )?; |
| 19784 | u_grid = if uo.is_empty() { Grid::Zero(64) } else { Grid::Own(uo) }; |
| 19785 | v_grid = if vo.is_empty() { Grid::Zero(64) } else { Grid::Own(vo) }; |
| 19786 | } else { |
| 19787 | if let Some((ub, _)) = &palette_uv_bufs { |
| 19788 | set_palette_pred(ub.clone(), fctx); |
| 19789 | } |
| 19790 | u_grid = read_plane( |
| 19791 | dec, |
| 19792 | cdfs, |
| 19793 | cset, |
| 19794 | cscan, |
| 19795 | 1, |
| 19796 | chroma_around[1], |
| 19797 | mode, |
| 19798 | uv_predict_mode, |
| 19799 | angle_delta_uv, |
| 19800 | reach, |
| 19801 | u, |
| 19802 | cpx, |
| 19803 | cpy, |
| 19804 | csz, |
| 19805 | csz, |
| 19806 | base_q_idx, |
| 19807 | alpha.zip(ac).map(|((au, _), ac)| (au, ac)), |
| 19808 | None, |
| 19809 | None, |
| 19810 | smooth_neighbor_uv, fctx, |
| 19811 | )?; |
| 19812 | if let Some((_, vb)) = &palette_uv_bufs { |
| 19813 | set_palette_pred(vb.clone(), fctx); |
| 19814 | } |
| 19815 | v_grid = read_plane( |
| 19816 | dec, |
| 19817 | cdfs, |
| 19818 | cset, |
| 19819 | cscan, |
| 19820 | 2, |
| 19821 | chroma_around[2], |
| 19822 | mode, |
| 19823 | uv_predict_mode, |
| 19824 | angle_delta_uv, |
| 19825 | reach, |
| 19826 | v, |
| 19827 | cpx, |
| 19828 | cpy, |
| 19829 | csz, |
| 19830 | csz, |
| 19831 | base_q_idx, |
| 19832 | alpha.zip(ac).map(|((_, av), ac)| (av, ac)), |
| 19833 | None, |
| 19834 | None, |
| 19835 | smooth_neighbor_uv, fctx, |
| 19836 | )?; |
| 19837 | } |
| 19838 | // The two chroma reads above are the only consulters of this leaf's |
| 19839 | // armed [`INTRABC_CHROMA_TX`]; leaving it set would make the NEXT |
| 19840 | // leaf's chroma inherit this leaf's luma type instead of its own |
| 19841 | // derivation (class armed-slot-leak-into-next-leaf, seen live: the |
| 19842 | // regular-intra leaf after an intrabc TX4 leaf took the intrabc |
| 19843 | // luma's `V_ADST` for its chroma eob row). |
| 19844 | if intrabc_dv.is_some() { |
| 19845 | fctx.intrabc_chroma_tx.with(|c| c.set(None)); |
| 19846 | } |
| 19847 | // `record_split_luma` expects a `record`-style `(r, c)` position in |
| 19848 | // `SUB`-grid units and rescales it by `SUB / MI`; `leaf_mi` is |
| 19849 | // already in mi units, so calling it here double-scaled the |
| 19850 | // position. Plane 0 is already correct per-TU from the |
| 19851 | // `record_mi_luma` calls above; this inlines `record_mi`'s tail |
| 19852 | // (spec `get_txb_ctx_general`'s neighbour-magnitude bookkeeping) for |
| 19853 | // the two chroma planes only, at `leaf_mi` directly, side_mi=2 (this |
| 19854 | // leaf's own 8x8 side over `MI`). |
| 19855 | let side_mi = 2; |
| 19856 | for cell in 0..side_mi { |
| 19857 | neighbours.left_side_mi[leaf_mi.0 + cell] = 8; |
| 19858 | neighbours.above_side_mi[leaf_mi.1 + cell] = 8; |
| 19859 | } |
| 19860 | let round_up_even = |n: usize| n.div_ceil(2) * 2; |
| 19861 | let bound_h = round_up_even(neighbours.mi_rows); |
| 19862 | let bound_w = round_up_even(neighbours.mi_cols); |
| 19863 | let u_state = neighbour_state(&u_grid); |
| 19864 | let v_state = neighbour_state(&v_grid); |
| 19865 | // The 4:4:4 lossless walk above already stamped these bands PER UNIT |
| 19866 | // (its raster-order last writer per cell is exactly libaom's |
| 19867 | // per-entry last update); this whole-leaf aggregate would clobber |
| 19868 | // four distinct unit states with one, so it runs only for the shapes |
| 19869 | // whose chroma really is one unit. |
| 19870 | if !(chroma_444 && lossless(fctx)) { |
| 19871 | for cell in 0..side_mi { |
| 19872 | if cell < side_mi.min(bound_h.saturating_sub(leaf_mi.0)) { |
| 19873 | neighbours.left[leaf_mi.0 + cell][1] = u_state; |
| 19874 | neighbours.left[leaf_mi.0 + cell][2] = v_state; |
| 19875 | } |
| 19876 | if cell < side_mi.min(bound_w.saturating_sub(leaf_mi.1)) { |
| 19877 | neighbours.above[leaf_mi.1 + cell][1] = u_state; |
| 19878 | neighbours.above[leaf_mi.1 + cell][2] = v_state; |
| 19879 | } |
| 19880 | } |
| 19881 | } |
| 19882 | // This leaf's own palette state into the mi-granular above/left bands |
| 19883 | // (sizes 0 when it used none, which is what clears a previous block's |
| 19884 | // palette out of the next block's ctx and colour cache). |
| 19885 | { |
| 19886 | neighbours.record_palette_y_rect(leaf_mi, 8, 8, pal_y_size, pal_y_colors); |
| 19887 | neighbours.record_palette_uv_rect(leaf_mi, 8, 8, pal_uv_size, pal_uv_colors); |
| 19888 | } |
| 19889 | neighbours.fill_skip_grid(leaf_mi, 2, skip); |
| 19890 | neighbours.fill_lf_grid(leaf_mi, 2, 4, 0, fctx); |
| 19891 | // lane-t900 r32: `set_txfm_ctxs` for an 8x8 leaf -- see `decode_block`'s |
| 19892 | // own copy; only an intrabc var-tx read consumes these bands on the intra |
| 19893 | // path, so the write is armed by `allow_intrabc`. |
| 19894 | if allow_intrabc { |
| 19895 | set_txfm_ctxs(neighbours, leaf_mi, 4, 2, 2, skip && intrabc_dv.is_some()); |
| 19896 | } |
| 19897 | neighbours.record_mode_mi(leaf_mi.0, leaf_mi.1, 2, 2, mode); |
| 19898 | record_intrabc_mi(leaf_mi.0, leaf_mi.1, 2, intrabc_dv, neighbours, fctx); |
| 19899 | return Ok(mode); |
| 19900 | } |
| 19901 | neighbours.record_mi(leaf_mi, 8, &[&luma_grid[..], &u_grid[..], &v_grid[..]]); |
| 19902 | // This leaf's own palette state into the mi-granular above/left bands |
| 19903 | // (sizes 0 when it used none, which is what clears a previous block's |
| 19904 | // palette out of the next block's ctx and colour cache). |
| 19905 | { |
| 19906 | neighbours.record_palette_y_rect(leaf_mi, 8, 8, pal_y_size, pal_y_colors); |
| 19907 | neighbours.record_palette_uv_rect(leaf_mi, 8, 8, pal_uv_size, pal_uv_colors); |
| 19908 | } |
| 19909 | hit!(INTER8_SKIP_BAND_HITS); |
| 19910 | neighbours.fill_skip_grid(leaf_mi, 2, skip); |
| 19911 | // lane-kf900 r1: the leaf's own RESOLVED transform size, never the literal |
| 19912 | // block side. libaom calls `set_txfm_ctxs(mbmi->tx_size, ..., skip_txfm && |
| 19913 | // is_inter_block(mbmi), xd)` (decodemv.c) -- for an INTRA block that skip |
| 19914 | // term is always 0, so a skipped 8x8 leaf whose `tx_depth` resolved to |
| 19915 | // TX_4X4 still publishes 4 to the next block's `get_tx_size_context` |
| 19916 | // (pred_common.h:342). Writing 8 here made the 8x16 strip below the leaf |
| 19917 | // read `tx_size_cat1` row 2 where libaom reads row 1 -- same alphabet, |
| 19918 | // wrong CDF row, so the msac range diverged mid-frame with no refusal |
| 19919 | // (class `wrong-alphabet-same-value`; the ReelB key frame at |
| 19920 | // -ss 900, first bad luma sample row 186 col 1803). |
| 19921 | neighbours.fill_lf_grid(leaf_mi, 2, resolved as u8, 0, fctx); |
| 19922 | // lane-t900 r32 / lane-av1txbands: `set_txfm_ctxs` for an 8x8 leaf -- see |
| 19923 | // `decode_block`'s own copy for why this stays beside |
| 19924 | // [`Neighbours::fill_lf_grid_rect`]'s publish (the skipped-intrabc |
| 19925 | // BLOCK-size term). |
| 19926 | if allow_intrabc { |
| 19927 | set_txfm_ctxs(neighbours, leaf_mi, resolved, 2, 2, skip && intrabc_dv.is_some()); |
| 19928 | } |
| 19929 | neighbours.record_mode_mi(leaf_mi.0, leaf_mi.1, 2, 2, mode); |
| 19930 | // The DV predictor of a LATER intrabc block is the ordinary `INTRA_FRAME` |
| 19931 | // MV stack over this grid (`av1_find_mv_refs`), so an 8x8 leaf has to |
| 19932 | // publish its own cells exactly like `decode_block` does. |
| 19933 | record_intrabc_mi(leaf_mi.0, leaf_mi.1, 2, intrabc_dv, neighbours, fctx); |
| 19934 | Ok(mode) |
| 19935 | } |
| 19936 | |
| 19937 | /// lane-av1-422d: ONE 4:2:2 chroma transform unit of an 8x8 leaf |
| 19938 | /// ([`decode_leaf8`]). At ss (1,0) the plane block is 4x8 and |
| 19939 | /// `av1_get_max_uv_txsize(BLOCK_8X8)` resolves to |
| 19940 | /// `max_txsize_rect_lookup[BLOCK_4X8]` == TX_4X8, so the WHOLE plane block is |
| 19941 | /// a single rect unit -- one txb_skip + one scan per plane in the |
| 19942 | /// [`TxbSet::ChromaRect8x4`] class [`decode_rect_split`] proves for the |
| 19943 | /// shape. Mirrors [`sub8_leaf_chroma444`]'s rect arm at the leaf's halved |
| 19944 | /// position: the entropy context is the leaf's own 8x8 luma-span |
| 19945 | /// above/left state (the `chroma_tiled` arm's convention; no `+3` |
| 19946 | /// plane-block-bigger term -- the unit covers the plane block exactly), the |
| 19947 | /// CFL source is the leaf's whole 8x8 luma block, and the plane's predicted |
| 19948 | /// mode supplies the mode-derived default `tx_type` unless an armed |
| 19949 | /// [`INTRABC_CHROMA_TX`] slot carries the intrabc luma's own (the caller |
| 19950 | /// arms and disarms it, exactly as it does around its square reads). |
| 19951 | #[allow(clippy::too_many_arguments)] |
| 19952 | fn leaf8_chroma422_unit( |
| 19953 | dec: &mut SymbolDecoder, |
| 19954 | cdfs: &mut Cdfs, |
| 19955 | around: (bool, bool, i32), |
| 19956 | uv_predict_mode: usize, |
| 19957 | angle_delta_uv: i32, |
| 19958 | reach: Reach, |
| 19959 | plane_idx: usize, |
| 19960 | cfl: Option<(i32, CflSrc)>, |
| 19961 | skip: bool, |
| 19962 | // The prediction override the square arms feed through |
| 19963 | // `set_palette_pred`: an intrabc leaf's frame copy at the DV takes |
| 19964 | // priority over a palette block's colour-index map. |
| 19965 | pred_override: Option<Vec<u16>>, |
| 19966 | // Unread, like [`read_plane`]'s own: the borrow proves the caller holds |
| 19967 | // the plane exclusively while the unit's coefficients are parsed. |
| 19968 | _plane: &mut PlaneBuf<'static>, |
| 19969 | cpx: usize, |
| 19970 | cpy: usize, |
| 19971 | smooth_neighbor_uv: bool, fctx: &crate::decode::FrameCtx, |
| 19972 | ) -> Result<Grid> { |
| 19973 | // The unit's prediction runs whether or not a residual follows it -- a |
| 19974 | // skipped leaf still predicts (the CfL contribution survives a skip), |
| 19975 | // and a skipped intrabc leaf still copies the frame at its DV. |
| 19976 | if let Some(buf) = pred_override { |
| 19977 | set_palette_pred(buf, fctx); |
| 19978 | } |
| 19979 | if skip { |
| 19980 | push_intra_rect( |
| 19981 | plane_idx, cpx, cpy, 4, 8, uv_predict_mode, angle_delta_uv, reach, |
| 19982 | &ZERO_RESIDUAL[..4 * 8], cfl, None, smooth_neighbor_uv, fctx, |
| 19983 | ); |
| 19984 | return Ok(Grid::Zero(4 * 8)); |
| 19985 | } |
| 19986 | let default_tx = fctx |
| 19987 | .intrabc_chroma_tx |
| 19988 | .with(std::cell::Cell::get) |
| 19989 | .unwrap_or_else(|| default_intra_tx_type(uv_predict_mode as u8)); |
| 19990 | let mut coding = cdfs.txb(TxbSet::ChromaRect8x4, uv_predict_mode); |
| 19991 | let (levels, tx_type) = read_coeffs_rect( |
| 19992 | dec, &mut coding, &SCAN_4X8, 4, 8, |
| 19993 | usize::from(around.0) + usize::from(around.1), |
| 19994 | dc_sign_ctx(around.2), default_tx, |
| 19995 | )?; |
| 19996 | let residual = dequant_and_inverse_typed_wh( |
| 19997 | &levels, 4, 8, crate::decode::bit_depth(fctx), block_q_idx(fctx), |
| 19998 | plane_q_delta(plane_idx, fctx).0, plane_q_delta(plane_idx, fctx).1, |
| 19999 | tx_type, block_iqmatrix(fctx, plane_idx, 4, 8, tx_type), |
| 20000 | ); |
| 20001 | push_intra_rect( |
| 20002 | plane_idx, cpx, cpy, 4, 8, uv_predict_mode, angle_delta_uv, reach, |
| 20003 | &residual, cfl, None, smooth_neighbor_uv, fctx, |
| 20004 | ); |
| 20005 | Ok(levels) |
| 20006 | } |
| 20007 | |
| 20008 | /// `read_intra_frame_mode_info`'s mode-info read for one `BLOCK_4X4` leaf |
| 20009 | /// below an 8x8 partition (spec `decode_partition`'s `bSize < BLOCK_8X8` |
| 20010 | /// bottom, where no partition symbol is read at all -- `decode_block` runs |
| 20011 | /// directly). No palette (`av1_allow_palette` needs both dims >= 8, spec |
| 20012 | /// blockd.h) and no `read_tx_size` (a sub-8x8 block's transform is always |
| 20013 | /// its own bsize, spec 5.11.16). `has_chroma` follows `is_chroma_reference` |
| 20014 | /// (spec/libaom `av1_common_int.h`): for 4:2:0, only the bottom-right leaf |
| 20015 | /// of the 2x2 4x4 group carries chroma syntax at all. |
| 20016 | #[allow(clippy::too_many_arguments)] |
| 20017 | fn read_intra_mode_sub8( |
| 20018 | dec: &mut SymbolDecoder, |
| 20019 | cdfs: &mut Cdfs, |
| 20020 | above_mode: usize, |
| 20021 | left_mode: usize, |
| 20022 | has_chroma: bool, |
| 20023 | enable_filter_intra: bool, |
| 20024 | // [`filter_intra_size_class_rect`] for the LEAF's own shape: 0 for a |
| 20025 | // `BLOCK_4X4` split leaf, 8/9 for a 4x8/8x4 rect leaf. Reading the flag |
| 20026 | // from the wrong row is the same VALUE with a different interval, so it |
| 20027 | // desyncs a block or two later, not here (class `wrong-alphabet-same-value`). |
| 20028 | fi_class: usize, |
| 20029 | skip_ctx: usize, |
| 20030 | allow_intrabc: bool, |
| 20031 | mi_r: usize, |
| 20032 | mi_c: usize, |
| 20033 | // The leaf's own mi footprint for `set_segment_id`: 1x1 for a `BLOCK_4X4` |
| 20034 | // split leaf, 2x1/1x2 for an 8x4/4x8 one. Stamping 1x1 for a rect leaf |
| 20035 | // left the second mi column at segment 0, which reads back as a |
| 20036 | // neighbour's `cdf_index`/prediction two blocks later. |
| 20037 | seg_w_mi: usize, |
| 20038 | seg_h_mi: usize, fctx: &crate::decode::FrameCtx, |
| 20039 | ) -> Result<(bool, usize, i32, Option<(usize, i32, Option<(i32, i32)>)>, Option<usize>, Option<(i32, i32)>)> { |
| 20040 | // lane-av1mono: a monochrome leaf codes no chroma mode (libaom |
| 20041 | // `decodemv.c:933`), so fold `NumPlanes == 1` into `has_chroma`. |
| 20042 | let has_chroma = has_chroma && !mono(fctx); |
| 20043 | let trace = crate::envflags::env_flag!("EC_AV1_TRACE"); |
| 20044 | let ec_istep = crate::envflags::env_flag!("EC_TRACE_MODE_STEP"); |
| 20045 | if ec_istep { |
| 20046 | eprintln!("EC_IMODE mi_row={mi_r} mi_col={mi_c} fn=sub8 rng={}", dec.debug_state().0); |
| 20047 | } |
| 20048 | // Same `EC_ISTEP` ladder the square/rect readers print, so a cross-decoder |
| 20049 | // range diff lands on the ELEMENT inside a 4x4 leaf, not just the block. |
| 20050 | macro_rules! istep { |
| 20051 | ($name:literal, $val:expr) => { |
| 20052 | if ec_istep { |
| 20053 | eprintln!("EC_ISTEP mi_row={mi_r} mi_col={mi_c} name={} val={} rng={}", $name, $val, dec.debug_state().0); |
| 20054 | } |
| 20055 | }; |
| 20056 | } |
| 20057 | // lane-seg2: same `intra_segment_id` placement as the square and rect |
| 20058 | // readers (spec 5.11.6) -- a `BLOCK_4X4` leaf codes its own `segment_id` |
| 20059 | // like any other block, over its own 1x1 mi footprint. Dropping it here |
| 20060 | // both ate a symbol (the tile desynced at the frame's first sub-8x8 |
| 20061 | // split) and left `cur_segment_id` on the previous block, so the leaf |
| 20062 | // dequantized with a neighbour's `SEG_LVL_ALT_Q`. |
| 20063 | if seg_id_pre_skip(fctx) { |
| 20064 | intra_segment_id(dec, cdfs, mi_r, mi_c, seg_w_mi, seg_h_mi, false, fctx); |
| 20065 | } |
| 20066 | let skip = dec.symbol(&mut cdfs.skip[skip_ctx]) != 0; |
| 20067 | if trace { |
| 20068 | eprintln!("TRACE sub8 skip mi=({mi_r},{mi_c}) ctx={skip_ctx} value={} rng={}", skip as i32, dec.debug_state().0); |
| 20069 | } |
| 20070 | istep!("skip", skip as i32); |
| 20071 | if !seg_id_pre_skip(fctx) { |
| 20072 | intra_segment_id(dec, cdfs, mi_r, mi_c, seg_w_mi, seg_h_mi, skip, fctx); |
| 20073 | } |
| 20074 | maybe_read_cdef_idx(dec, mi_r, mi_c, (2, 2), skip, fctx); |
| 20075 | istep!("cdef", 0); |
| 20076 | maybe_read_delta_q(dec, cdfs, mi_r, mi_c, false, skip, fctx); |
| 20077 | maybe_read_delta_lf(dec, cdfs, mi_r, mi_c, false, skip, fctx); |
| 20078 | istep!("dq", 0); |
| 20079 | if allow_intrabc { |
| 20080 | let use_intrabc = dec.symbol(&mut cdfs.intrabc) != 0; |
| 20081 | if ec_istep { |
| 20082 | eprintln!("EC_ISTEP mi_row={mi_r} mi_col={mi_c} name=intrabc val={} rng={}", u8::from(use_intrabc), dec.debug_state().0); |
| 20083 | } |
| 20084 | if use_intrabc { |
| 20085 | // lane-av1txr: `read_intrabc_info` (spec 5.11.13) reads the block |
| 20086 | // vector right after the flag and RETURNS -- no y/uv mode, angle |
| 20087 | // delta, palette, CfL or filter-intra syntax follows, and the |
| 20088 | // block is reconstructed by the INTER path (libaom's |
| 20089 | // `is_inter_block` counts an intrabc block), so the caller takes |
| 20090 | // the frame-copy prediction + inter residual. |
| 20091 | let dv = read_intrabc_dv(dec, cdfs, mi_r, mi_c, (seg_w_mi, seg_h_mi), fctx); |
| 20092 | // lane-av1txr: the DV travels back in the RETURN value -- it is |
| 20093 | // NOT published through `FrameCtx::intrabc_dv`. That slot is the |
| 20094 | // SQUARE paths' handoff (`decode_block`/`decode_leaf8` arm it, |
| 20095 | // then `take()` it in the same call), so a sub-8x8 leaf that |
| 20096 | // armed it left the vector standing for the NEXT >=8x8 block: |
| 20097 | // `warped.obu`'s (38,60) 8x8 intra block took it as its own |
| 20098 | // intrabc and read an inter var-tx `txfm_split` where libaom read |
| 20099 | // an intra `tx_depth` (class `stale-slot-handoff`). |
| 20100 | hit!(INTRABC_HITS); |
| 20101 | return Ok((skip, DC_PRED, 0, None, None, Some(dv))); |
| 20102 | } |
| 20103 | } |
| 20104 | let above_ctx = INTRA_MODE_CTX[above_mode]; |
| 20105 | let left_ctx = INTRA_MODE_CTX[left_mode]; |
| 20106 | let mode = dec.symbol(&mut cdfs.kf_y_mode[above_ctx][left_ctx]); |
| 20107 | istep!("mode", mode); |
| 20108 | if trace { |
| 20109 | eprintln!("TRACE sub8 y_mode mi=({mi_r},{mi_c}) ctx=({above_ctx},{left_ctx}) value={mode} rng={}", dec.debug_state().0); |
| 20110 | } |
| 20111 | // No `angle_delta` at all below 8x8: spec 5.11.6's `intra_angle_info_y`/ |
| 20112 | // `_uv` are gated on `MiSize >= BLOCK_8X8` (libaom `av1_use_angle_delta`, |
| 20113 | // blockd.h), so a directional mode on a `BLOCK_4X4` leaf carries no delta |
| 20114 | // symbol. Reading one consumed a symbol the encoder never wrote and |
| 20115 | // desynced the tile at the very next element. |
| 20116 | let angle_delta_y = 0; |
| 20117 | let chroma = if has_chroma { |
| 20118 | // `is_cfl_allowed` (blockd.h): at lossless CfL needs the CHROMA plane |
| 20119 | // block to be a single TX_4X4 (`cfl_allowed_px`). True for this leaf |
| 20120 | // at 4:2:0 (the group's plane block is 4x4) and for a 4:4:4 |
| 20121 | // `BLOCK_4X4` split leaf, but FALSE for a 4:4:4 `BLOCK_8X4`/`BLOCK_4X8` |
| 20122 | // rect leaf, whose plane block is the rect itself -- that shape reads |
| 20123 | // the 13-symbol `uv_mode_no_cfl` CDF, and reading the CFL alphabet |
| 20124 | // there desynced the tile at the rect leaf's own uv_mode symbol (measured: |
| 20125 | // ours consumed the 14-symbol interval where the oracle read 13, first |
| 20126 | // divergence at the rect leaf mi (8,16) of `ll444.obu`; class |
| 20127 | // `wrong-alphabet-same-value`, lane-av1-llsub8). |
| 20128 | let uv_mode = if cfl_allowed_px(seg_w_mi * MI, seg_h_mi * MI, fctx) { |
| 20129 | dec.symbol(&mut cdfs.uv_mode_cfl[mode]) |
| 20130 | } else { |
| 20131 | dec.symbol(&mut cdfs.uv_mode_no_cfl[mode]) |
| 20132 | }; |
| 20133 | if trace { |
| 20134 | eprintln!("TRACE sub8 uv_mode value={uv_mode} rng={}", dec.debug_state().0); |
| 20135 | } |
| 20136 | if (9..=12).contains(&uv_mode) { |
| 20137 | hit!(SMOOTH_UV_HITS); |
| 20138 | } |
| 20139 | let alpha = if uv_mode == UV_CFL_PRED { |
| 20140 | Some(read_cfl_alphas(dec, cdfs)) |
| 20141 | } else { |
| 20142 | None |
| 20143 | }; |
| 20144 | if (V_PRED..=D67_PRED).contains(&uv_mode) { |
| 20145 | hit!(DIRECTIONAL_UV_HITS); |
| 20146 | } |
| 20147 | // See the luma comment above: no angle delta below 8x8. |
| 20148 | let angle_delta_uv = 0; |
| 20149 | Some((uv_mode, angle_delta_uv, alpha)) |
| 20150 | } else { |
| 20151 | None |
| 20152 | }; |
| 20153 | let mut filter_intra = None; |
| 20154 | if mode == DC_PRED && enable_filter_intra { |
| 20155 | let use_filter_intra = dec.symbol(&mut cdfs.filter_intra[fi_class]) != 0; |
| 20156 | if trace { |
| 20157 | eprintln!("TRACE sub8 use_filter_intra value={}", use_filter_intra as i32); |
| 20158 | } |
| 20159 | if use_filter_intra { |
| 20160 | hit!(FILTER_INTRA_HITS); |
| 20161 | let fi_mode = dec.symbol(&mut cdfs.filter_intra_mode); |
| 20162 | filter_intra = Some(fi_mode); |
| 20163 | } |
| 20164 | } |
| 20165 | Ok((skip, mode, angle_delta_y, chroma, filter_intra, None)) |
| 20166 | } |
| 20167 | /// 4:4:4 sub-8x8 key-frame leaf chroma (lane-av1-444). At subsampling 0/0 |
| 20168 | /// `is_chroma_reference` (av1_common_int.h:1459) is unconditional, so EVERY |
| 20169 | /// 4x4 / 4x8 / 8x4 leaf codes its own full-resolution chroma unit right |
| 20170 | /// after its own luma (`read_intra_mode_sub8` already read this leaf's own |
| 20171 | /// `uv_mode`), not the one shared 4x4 unit [`decode_leaf_split4`] / |
| 20172 | /// [`decode_leaf_rect8`]'s group tails decode at 4:2:0. Mirrors those tails |
| 20173 | /// arm for arm at leaf scale: a skipped leaf keeps its CfL contribution, an |
| 20174 | /// intrabc leaf predicts a frame copy at its DV with the luma `tx_type` |
| 20175 | /// inherited through [`INTRABC_CHROMA_TX`] (the caller arms it right after |
| 20176 | /// the luma read) -- and a SKIPPED intrabc leaf is still the DV copy, never |
| 20177 | /// an intra prediction (lane-av1-ibcskip2: libaom's |
| 20178 | /// `av1_build_inter_predictors_sb` runs for an intrabc block regardless of |
| 20179 | /// `skip`). The chroma position, reach and entropy-context span are |
| 20180 | /// the leaf's own (ss 0/0); the 4x8/8x4 unit reads the same |
| 20181 | /// `ChromaRect8x4` tables [`decode_rect_split`] proves for that shape. |
| 20182 | /// Records the leaf's own `uv_mode` map entry and chroma above/left state, |
| 20183 | /// and returns the (u, v) grids for the caller's coarse slots. |
| 20184 | #[allow(clippy::too_many_arguments)] |
| 20185 | fn sub8_leaf_chroma444( |
| 20186 | dec: &mut SymbolDecoder, |
| 20187 | cdfs: &mut Cdfs, |
| 20188 | neighbours: &mut Neighbours, |
| 20189 | lmi: (usize, usize), |
| 20190 | bw: usize, |
| 20191 | bh: usize, |
| 20192 | uv_mode: usize, |
| 20193 | alpha: Option<(i32, i32)>, |
| 20194 | skip: bool, |
| 20195 | intrabc_dv: Option<(i32, i32)>, |
| 20196 | y: &PlaneBuf<'static>, |
| 20197 | u: &mut PlaneBuf<'static>, |
| 20198 | v: &mut PlaneBuf<'static>, |
| 20199 | scan4: &[u16], |
| 20200 | base_q_idx: u8, |
| 20201 | smooth_neighbor_uv: bool, |
| 20202 | fctx: &crate::decode::FrameCtx, |
| 20203 | ) -> Result<(Grid, Grid, usize, usize)> { |
| 20204 | let uv_predict_mode = if uv_mode == UV_CFL_PRED { DC_PRED } else { uv_mode }; |
| 20205 | let (w_mi, h_mi) = (bw / MI, bh / MI); |
| 20206 | neighbours.record_uv_mode_mi(lmi.0, lmi.1, w_mi, h_mi, uv_predict_mode); |
| 20207 | let (px, py) = (lmi.1 * MI, lmi.0 * MI); |
| 20208 | let square = (bw, bh) == (4, 4); |
| 20209 | // `Reach::of_rect` has no 4x4 table (`rect_reach_tables` starts at 4x8); |
| 20210 | // the square unit takes the leaf's own `Reach::of`, as its luma does. |
| 20211 | let reach = if square { |
| 20212 | Reach::of(4, px, py, y.width, y.height, fctx) |
| 20213 | } else { |
| 20214 | Reach::of_rect(bw, bh, px, py, y.width, y.height, fctx) |
| 20215 | }; |
| 20216 | let grids = if let Some(dv) = intrabc_dv.filter(|_| skip) { |
| 20217 | // lane-av1-ibcskip2: a SKIPPED intrabc leaf still predicts the frame |
| 20218 | // copy at its DV -- libaom's `av1_build_inter_predictors_sb` runs for |
| 20219 | // an intrabc block regardless of `skip` (only the residual read is |
| 20220 | // dropped), so the reconstruction is the bare DV copy. The intra push |
| 20221 | // in the next arm is the NON-intrabc route; predicting DC there |
| 20222 | // decoded flat cells where this fixture's oracle copies frame bytes |
| 20223 | // (class skipped-intrabc-predicts-intra, |
| 20224 | // lanes/av1ibcskip2.report.md). Same armed-palette-slot route the |
| 20225 | // leaf's own luma skip arm uses (`decode_leaf_split4` / |
| 20226 | // `decode_leaf_rect8`); the caller already flushed the recon queue |
| 20227 | // before its luma DV copy, so the source samples are current. |
| 20228 | let mut ub = vec![0u16; bw * bh]; |
| 20229 | mc::predict_with_filter( |
| 20230 | &u.data, u.width, u.true_width, u.true_height, |
| 20231 | mv_to_q4(px, dv.1, 0), mv_to_q4(py, dv.0, 0), |
| 20232 | bw, bh, mc::InterpFilterKind::Bilinear, &mut ub, fctx, |
| 20233 | ); |
| 20234 | let mut vb = vec![0u16; bw * bh]; |
| 20235 | mc::predict_with_filter( |
| 20236 | &v.data, v.width, v.true_width, v.true_height, |
| 20237 | mv_to_q4(px, dv.1, 0), mv_to_q4(py, dv.0, 0), |
| 20238 | bw, bh, mc::InterpFilterKind::Bilinear, &mut vb, fctx, |
| 20239 | ); |
| 20240 | hit!(SKIPPED_INTRABC_DV_COPY_HITS); |
| 20241 | let zeros = &ZERO_RESIDUAL[..bw * bh]; |
| 20242 | if square { |
| 20243 | set_palette_pred(ub, fctx); |
| 20244 | push_intra(1, px, py, 4, DC_PRED, 0, reach, zeros, None, None, smooth_neighbor_uv, fctx); |
| 20245 | set_palette_pred(vb, fctx); |
| 20246 | push_intra(2, px, py, 4, DC_PRED, 0, reach, zeros, None, None, smooth_neighbor_uv, fctx); |
| 20247 | } else { |
| 20248 | set_palette_pred(ub, fctx); |
| 20249 | push_intra_rect(1, px, py, bw, bh, DC_PRED, 0, reach, zeros, None, None, smooth_neighbor_uv, fctx); |
| 20250 | set_palette_pred(vb, fctx); |
| 20251 | push_intra_rect(2, px, py, bw, bh, DC_PRED, 0, reach, zeros, None, None, smooth_neighbor_uv, fctx); |
| 20252 | } |
| 20253 | // The same arm-without-clear route the 4:2:0 tails count: a skipped |
| 20254 | // leaf reads no coefficient, so the inherited chroma `tx_type` is |
| 20255 | // dropped unconsumed. |
| 20256 | hit!(SKIPPED_INTRABC_CHROMA_ARM_HITS); |
| 20257 | fctx.intrabc_chroma_tx.with(|c| c.set(None)); |
| 20258 | (Grid::Zero(bw * bh), Grid::Zero(bw * bh), 4, 4) |
| 20259 | } else if skip { |
| 20260 | let ac = alpha.map(|_| cfl_src_rect(px, py, bw, bh)); |
| 20261 | if square { |
| 20262 | 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); |
| 20263 | 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); |
| 20264 | } else { |
| 20265 | 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); |
| 20266 | 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); |
| 20267 | } |
| 20268 | // The same arm-without-clear route the 4:2:0 tails count: a skipped |
| 20269 | // leaf reads no coefficient, so an armed intrabc slot is dropped. |
| 20270 | if fctx.intrabc_chroma_tx.with(std::cell::Cell::get).is_some() { |
| 20271 | hit!(SKIPPED_INTRABC_CHROMA_ARM_HITS); |
| 20272 | fctx.intrabc_chroma_tx.with(|c| c.set(None)); |
| 20273 | } |
| 20274 | (Grid::Zero(bw * bh), Grid::Zero(bw * bh), 4, 4) |
| 20275 | } else if let Some(dv) = intrabc_dv { |
| 20276 | // Frame copy at the DV; the residual still reads the ordinary chroma |
| 20277 | // tables with the inherited luma `tx_type` (see the 4:2:0 tails). |
| 20278 | let mut ub = vec![0u16; bw * bh]; |
| 20279 | mc::predict_with_filter( |
| 20280 | &u.data, u.width, u.true_width, u.true_height, |
| 20281 | mv_to_q4(px, dv.1, 0), mv_to_q4(py, dv.0, 0), |
| 20282 | bw, bh, mc::InterpFilterKind::Bilinear, &mut ub, fctx, |
| 20283 | ); |
| 20284 | let mut vb = vec![0u16; bw * bh]; |
| 20285 | mc::predict_with_filter( |
| 20286 | &v.data, v.width, v.true_width, v.true_height, |
| 20287 | mv_to_q4(px, dv.1, 0), mv_to_q4(py, dv.0, 0), |
| 20288 | bw, bh, mc::InterpFilterKind::Bilinear, &mut vb, fctx, |
| 20289 | ); |
| 20290 | let around = neighbours.around_mi_rect(lmi, bw, bh); |
| 20291 | let pair = if square { |
| 20292 | set_palette_pred(ub, fctx); |
| 20293 | let ug = read_plane( |
| 20294 | dec, cdfs, TxbSet::Chroma4, scan4, 1, around[1], DC_PRED, DC_PRED, |
| 20295 | 0, reach, u, px, py, 4, TX4, base_q_idx, |
| 20296 | None, None, None, smooth_neighbor_uv, fctx, |
| 20297 | )?; |
| 20298 | set_palette_pred(vb, fctx); |
| 20299 | let vg = read_plane( |
| 20300 | dec, cdfs, TxbSet::Chroma4, scan4, 2, around[2], DC_PRED, DC_PRED, |
| 20301 | 0, reach, v, px, py, 4, TX4, base_q_idx, |
| 20302 | None, None, None, smooth_neighbor_uv, fctx, |
| 20303 | )?; |
| 20304 | (ug, vg, 4, 4) |
| 20305 | } else { |
| 20306 | // lane-av1-ibc444rect: the unskipped lossless chroma walk this |
| 20307 | // lane fixes (one rect unit -> per-4x4 Chroma4 units). |
| 20308 | if lossless(fctx) { |
| 20309 | hit!(INTRABC_RECT8_LOSSLESS_CHROMA_HITS); |
| 20310 | } |
| 20311 | if lossless(fctx) { |
| 20312 | // lane-av1-ibc444rect: at lossless `read_tx_size` forces |
| 20313 | // TX_4X4 on EVERY plane (`av1_get_tx_size`'s lossless |
| 20314 | // first line), so this rect leaf's chroma is (bw/4)*(bh/4) |
| 20315 | // TX_4X4 units at their own 4x4 origins -- not the single |
| 20316 | // `ChromaRect8x4` rect unit the non-lossless arm below reads |
| 20317 | // (`av1_get_max_uv_txsize(BLOCK_8X4)`'s rect, unreachable at |
| 20318 | // lossless). Same walk the leaf's own non-intrabc lossless |
| 20319 | // arm runs (lane-av1-llsub8's fix), plane-major per libaom |
| 20320 | // `decode_token_recon_block`, with the intra predictor |
| 20321 | // swapped for the CURRENT frame at the DV; CFL is never |
| 20322 | // allowed at lossless and no alpha rides an intrabc leaf. |
| 20323 | let mut out_u = Grid::Own(vec![0i32; bw * bh]); |
| 20324 | let mut out_v = Grid::Own(vec![0i32; bw * bh]); |
| 20325 | for (plane_idx, buf) in [(1usize, u), (2usize, v)] { |
| 20326 | for ur in 0..bh / 4usize { |
| 20327 | for uc in 0..bw / 4usize { |
| 20328 | let (umi_r, umi_c) = (lmi.0 + ur, lmi.1 + uc); |
| 20329 | let (upx, upy) = (px + uc * 4, py + ur * 4); |
| 20330 | let mut pb = vec![0u16; 16]; |
| 20331 | mc::predict_with_filter( |
| 20332 | &buf.data, buf.width, buf.true_width, buf.true_height, |
| 20333 | mv_to_q4(upx, dv.1, 0), mv_to_q4(upy, dv.0, 0), |
| 20334 | 4, 4, mc::InterpFilterKind::Bilinear, &mut pb, fctx, |
| 20335 | ); |
| 20336 | set_palette_pred(pb, fctx); |
| 20337 | let unit_around = neighbours.around_mi_rect((umi_r, umi_c), 4, 4); |
| 20338 | let unit_reach = Reach::of(4, upx, upy, y.width, y.height, fctx); |
| 20339 | let grid = read_plane( |
| 20340 | dec, cdfs, TxbSet::Chroma4, scan4, plane_idx, unit_around[plane_idx], |
| 20341 | DC_PRED, DC_PRED, 0, unit_reach, buf, upx, upy, 4, |
| 20342 | TX4, base_q_idx, None, None, Some(3), smooth_neighbor_uv, fctx, |
| 20343 | )?; |
| 20344 | let cell = grid_unit_state(&grid, 4, 0, 0, 4, 4); |
| 20345 | if umi_r < neighbours.mi_rows { |
| 20346 | neighbours.left[umi_r][plane_idx] = cell; |
| 20347 | } |
| 20348 | if umi_c < neighbours.mi_cols { |
| 20349 | neighbours.above[umi_c][plane_idx] = cell; |
| 20350 | } |
| 20351 | let out = if plane_idx == 1 { &mut out_u } else { &mut out_v }; |
| 20352 | if let Grid::Own(v) = out { |
| 20353 | for r in 0..4usize { |
| 20354 | v[(ur * 4 + r) * bw + uc * 4..][..4] |
| 20355 | .copy_from_slice(&grid[r * 4..][..4]); |
| 20356 | } |
| 20357 | } |
| 20358 | } |
| 20359 | } |
| 20360 | } |
| 20361 | (out_u, out_v, 4, 4) |
| 20362 | } else { |
| 20363 | let scan: &[u16] = if bw > bh { &SCAN_8X4 } else { &SCAN_4X8 }; |
| 20364 | let default_tx = |
| 20365 | fctx.intrabc_chroma_tx.with(std::cell::Cell::get).unwrap_or(TxType::DctDct); |
| 20366 | let mut out = (Grid::Zero(bw * bh), Grid::Zero(bw * bh), bw, bh); |
| 20367 | for (plane_idx, buf) in [(1usize, ub), (2usize, vb)] { |
| 20368 | let skip_ctx = |
| 20369 | usize::from(around[plane_idx].0) + usize::from(around[plane_idx].1); |
| 20370 | let mut coding = cdfs.txb(TxbSet::ChromaRect8x4, DC_PRED); |
| 20371 | let (levels, tx_type) = read_coeffs_rect( |
| 20372 | dec, &mut coding, scan, bw, bh, skip_ctx, |
| 20373 | dc_sign_ctx(around[plane_idx].2), default_tx, |
| 20374 | )?; |
| 20375 | let residual = dequant_and_inverse_typed_wh( |
| 20376 | &levels, bw, bh, crate::decode::bit_depth(fctx), block_q_idx(fctx), |
| 20377 | plane_q_delta(plane_idx, fctx).0, plane_q_delta(plane_idx, fctx).1, |
| 20378 | tx_type, |
| 20379 | block_iqmatrix(fctx, plane_idx, bw, bh, tx_type), |
| 20380 | ); |
| 20381 | set_palette_pred(buf, fctx); |
| 20382 | push_intra_rect( |
| 20383 | plane_idx, px, py, bw, bh, DC_PRED, 0, reach, &residual, |
| 20384 | None, None, smooth_neighbor_uv, fctx, |
| 20385 | ); |
| 20386 | if plane_idx == 1 { out.0 = levels; } else { out.1 = levels; } |
| 20387 | } |
| 20388 | out |
| 20389 | } |
| 20390 | }; |
| 20391 | fctx.intrabc_chroma_tx.with(|c| c.set(None)); |
| 20392 | pair |
| 20393 | } else { |
| 20394 | let ac = alpha.map(|_| cfl_src_rect(px, py, bw, bh)); |
| 20395 | let around = neighbours.around_mi_rect(lmi, bw, bh); |
| 20396 | if square { |
| 20397 | let ug = read_plane( |
| 20398 | dec, cdfs, TxbSet::Chroma4, scan4, 1, around[1], uv_mode, uv_predict_mode, |
| 20399 | 0, reach, u, px, py, 4, TX4, base_q_idx, |
| 20400 | alpha.zip(ac).map(|((au, _), ac)| (au, ac)), None, None, smooth_neighbor_uv, fctx, |
| 20401 | )?; |
| 20402 | let vg = read_plane( |
| 20403 | dec, cdfs, TxbSet::Chroma4, scan4, 2, around[2], uv_mode, uv_predict_mode, |
| 20404 | 0, reach, v, px, py, 4, TX4, base_q_idx, |
| 20405 | alpha.zip(ac).map(|((_, av), ac)| (av, ac)), None, None, smooth_neighbor_uv, fctx, |
| 20406 | )?; |
| 20407 | (ug, vg, 4, 4) |
| 20408 | } else if lossless(fctx) { |
| 20409 | // lane-av1-llsub8: `read_tx_size` answers TX_4X4 for EVERY plane of |
| 20410 | // a lossless block, so this rect leaf's chroma is (bw/4)*(bh/4) |
| 20411 | // TX_4X4 units at their own 4x4 origins -- not the single |
| 20412 | // TX_8X4/TX_4X8 rect unit the non-lossless arm below reads (that |
| 20413 | // one is `av1_get_max_uv_txsize(BLOCK_8X4)`'s rect transform, which |
| 20414 | // lossless never reaches). Measured on `ll444.obu`'s HORZ leaf at |
| 20415 | // mi (8,16): the oracle codes U units at bc 0,1 with |
| 20416 | // `get_txb_ctx`'s +10 rows (`txb_skip_cdf[0][10..12]`, |
| 20417 | // plane pels 32 > tx pels 16) where the rect-unit read consumed |
| 20418 | // `txb_skip_cdf[1][0]` -- the tile desynced at that U unit's |
| 20419 | // all_zero symbol. Walk is plane-major like libaom |
| 20420 | // `decode_token_recon_block`'s plane/row/col loop; each unit adds |
| 20421 | // 3 to the chroma context (Chroma4's `big` rows, the same fold |
| 20422 | // [`read_plane`]'s 128x128 caller uses); CFL is never allowed at |
| 20423 | // lossless, so no alpha rides along. |
| 20424 | let mut out_u = Grid::Own(vec![0i32; bw * bh]); |
| 20425 | let mut out_v = Grid::Own(vec![0i32; bw * bh]); |
| 20426 | for (plane_idx, buf) in [(1usize, u), (2usize, v)] { |
| 20427 | for ur in 0..bh / 4usize { |
| 20428 | for uc in 0..bw / 4usize { |
| 20429 | let (umi_r, umi_c) = (lmi.0 + ur, lmi.1 + uc); |
| 20430 | let (upx, upy) = (px + uc * 4, py + ur * 4); |
| 20431 | let unit_around = neighbours.around_mi_rect((umi_r, umi_c), 4, 4); |
| 20432 | let unit_reach = Reach::of(4, upx, upy, y.width, y.height, fctx); |
| 20433 | let grid = read_plane( |
| 20434 | dec, cdfs, TxbSet::Chroma4, scan4, plane_idx, unit_around[plane_idx], |
| 20435 | uv_mode, uv_predict_mode, 0, unit_reach, buf, upx, upy, 4, |
| 20436 | TX4, base_q_idx, None, None, Some(3), smooth_neighbor_uv, fctx, |
| 20437 | )?; |
| 20438 | let cell = grid_unit_state(&grid, 4, 0, 0, 4, 4); |
| 20439 | if umi_r < neighbours.mi_rows { |
| 20440 | neighbours.left[umi_r][plane_idx] = cell; |
| 20441 | } |
| 20442 | if umi_c < neighbours.mi_cols { |
| 20443 | neighbours.above[umi_c][plane_idx] = cell; |
| 20444 | } |
| 20445 | let out = if plane_idx == 1 { &mut out_u } else { &mut out_v }; |
| 20446 | if let Grid::Own(v) = out { |
| 20447 | for r in 0..4usize { |
| 20448 | v[(ur * 4 + r) * bw + uc * 4..][..4] |
| 20449 | .copy_from_slice(&grid[r * 4..][..4]); |
| 20450 | } |
| 20451 | } |
| 20452 | } |
| 20453 | } |
| 20454 | } |
| 20455 | (out_u, out_v, 4, 4) |
| 20456 | } else { |
| 20457 | // `av1_get_max_uv_txsize(BLOCK_4X8/8X4)` at ss 0/0 is the rect |
| 20458 | // transform itself; its `tx_type` is mode-derived with the |
| 20459 | // <32 set (`decode_rect_split`'s own rule for this shape). |
| 20460 | let scan: &[u16] = if bw > bh { &SCAN_8X4 } else { &SCAN_4X8 }; |
| 20461 | let default_tx = default_intra_tx_type(uv_predict_mode as u8); |
| 20462 | let mut out = (Grid::Zero(bw * bh), Grid::Zero(bw * bh), bw, bh); |
| 20463 | for plane_idx in 1..=2usize { |
| 20464 | let skip_ctx = |
| 20465 | usize::from(around[plane_idx].0) + usize::from(around[plane_idx].1); |
| 20466 | let mut coding = cdfs.txb(TxbSet::ChromaRect8x4, uv_predict_mode); |
| 20467 | let (levels, tx_type) = read_coeffs_rect( |
| 20468 | dec, &mut coding, scan, bw, bh, skip_ctx, |
| 20469 | dc_sign_ctx(around[plane_idx].2), default_tx, |
| 20470 | )?; |
| 20471 | let residual = dequant_and_inverse_typed_wh( |
| 20472 | &levels, bw, bh, crate::decode::bit_depth(fctx), block_q_idx(fctx), |
| 20473 | plane_q_delta(plane_idx, fctx).0, plane_q_delta(plane_idx, fctx).1, |
| 20474 | tx_type, |
| 20475 | block_iqmatrix(fctx, plane_idx, bw, bh, tx_type), |
| 20476 | ); |
| 20477 | let cfl = alpha |
| 20478 | .zip(ac) |
| 20479 | .map(|((au, av), ac)| (if plane_idx == 1 { au } else { av }, ac)); |
| 20480 | push_intra_rect( |
| 20481 | plane_idx, px, py, bw, bh, uv_predict_mode, 0, reach, &residual, |
| 20482 | cfl, None, smooth_neighbor_uv, fctx, |
| 20483 | ); |
| 20484 | if plane_idx == 1 { out.0 = levels; } else { out.1 = levels; } |
| 20485 | } |
| 20486 | out |
| 20487 | } |
| 20488 | }; |
| 20489 | // libaom `av1_set_entropy_contexts` stamps above/left PER TRANSFORM UNIT: |
| 20490 | // the leaf's own chroma span, not the 8x8 group's. A rect leaf's lossless |
| 20491 | // units (4x4) each carry their own state -- a 4:2:0-style whole-leaf smear |
| 20492 | // gave one unit's state to its neighbour's cells (lane-av1-llsub8). |
| 20493 | let (u_grid, v_grid, unit_w, unit_h) = grids; |
| 20494 | // One unit covers `unit_w/MI` mi columns and `unit_h/MI` mi rows (a 4x4 |
| 20495 | // unit: one cell each; the non-lossless rect unit: the whole leaf). |
| 20496 | let (uw_cells, uh_cells) = ((unit_w / MI).max(1), (unit_h / MI).max(1)); |
| 20497 | for ur in 0..h_mi / uh_cells { |
| 20498 | for uc in 0..w_mi / uw_cells { |
| 20499 | let (x0, y0) = (uc * unit_w, ur * unit_h); |
| 20500 | let us = grid_unit_state(&u_grid, bw, x0, y0, unit_w, unit_h); |
| 20501 | let vs = grid_unit_state(&v_grid, bw, x0, y0, unit_w, unit_h); |
| 20502 | for cell in 0..uh_cells { |
| 20503 | if lmi.0 + ur * uh_cells + cell < neighbours.mi_rows { |
| 20504 | neighbours.left[lmi.0 + ur * uh_cells + cell][1] = us; |
| 20505 | neighbours.left[lmi.0 + ur * uh_cells + cell][2] = vs; |
| 20506 | } |
| 20507 | } |
| 20508 | for cell in 0..uw_cells { |
| 20509 | if lmi.1 + uc * uw_cells + cell < neighbours.mi_cols { |
| 20510 | neighbours.above[lmi.1 + uc * uw_cells + cell][1] = us; |
| 20511 | neighbours.above[lmi.1 + uc * uw_cells + cell][2] = vs; |
| 20512 | } |
| 20513 | } |
| 20514 | } |
| 20515 | } |
| 20516 | Ok((u_grid, v_grid, unit_w, unit_h)) |
| 20517 | } |
| 20518 | |
| 20519 | /// [`neighbour_state`] over one `(unit_w x unit_h)` transform unit at offset |
| 20520 | /// `(x0, y0)` inside a `bw`-wide assembled chroma grid -- the per-unit stamp |
| 20521 | /// the rect arms need when a leaf carries more than one unit. |
| 20522 | fn grid_unit_state(grid: &Grid, bw: usize, x0: usize, y0: usize, unit_w: usize, unit_h: usize) -> Neighbour { |
| 20523 | let mut cul = 0u32; |
| 20524 | let mut dc = None; |
| 20525 | for r in 0..unit_h { |
| 20526 | for c in 0..unit_w { |
| 20527 | let v = grid[(y0 + r) * bw + x0 + c]; |
| 20528 | cul += v.unsigned_abs(); |
| 20529 | if r == 0 && c == 0 && v != 0 { |
| 20530 | dc = Some(v < 0); |
| 20531 | } |
| 20532 | } |
| 20533 | } |
| 20534 | if cul == 0 { |
| 20535 | return Neighbour::default(); |
| 20536 | } |
| 20537 | Neighbour { level: cul.min(7) as u8, dc } |
| 20538 | } |
| 20539 | |
| 20540 | |
| 20541 | /// [`sub8_leaf_chroma444`] for 4:2:2 (lane-av1-422g): an even-mi-column 4x4 |
| 20542 | /// leaf is its plane's own chroma reference (`is_chroma_reference`'s |
| 20543 | /// `!(mi_col & ss_x)` at ss_x 1 -- every even-column leaf, not 4:2:0's single |
| 20544 | /// bottom-right one) and codes ONE TX_4X4 chroma unit per plane right after |
| 20545 | /// its luma: `ss_size_lookup[BLOCK_4X4][1][0]` is BLOCK_4X4, so the (2, 4) |
| 20546 | /// plane area rounds up to the square 4x4 read. The unit sits at the chroma |
| 20547 | /// position (`px >> ss_x`), and its 4 chroma-px width spans TWO luma mi |
| 20548 | /// columns, so the context reads go through |
| 20549 | /// [`Neighbours::around_mi_422_chroma`] and the entropy stamp covers both |
| 20550 | /// columns with the unit's whole-unit state. |
| 20551 | #[allow(clippy::too_many_arguments)] |
| 20552 | fn sub8_leaf_chroma422( |
| 20553 | dec: &mut SymbolDecoder, |
| 20554 | cdfs: &mut Cdfs, |
| 20555 | neighbours: &mut Neighbours, |
| 20556 | lmi: (usize, usize), |
| 20557 | uv_mode: usize, |
| 20558 | angle_delta_uv: i32, |
| 20559 | alpha: Option<(i32, i32)>, |
| 20560 | skip: bool, |
| 20561 | intrabc_dv: Option<(i32, i32)>, |
| 20562 | y: &PlaneBuf<'static>, |
| 20563 | u: &mut PlaneBuf<'static>, |
| 20564 | v: &mut PlaneBuf<'static>, |
| 20565 | scan4: &[u16], |
| 20566 | base_q_idx: u8, |
| 20567 | smooth_neighbor_uv: bool, |
| 20568 | // The owning luma leaf's own shape (`(4, 4)` for a split leaf, `(8, 4)` / |
| 20569 | // `(4, 8)` for a rect pair's strip): libaom's `has_top_right` / |
| 20570 | // `has_bottom_left` answer for this chroma TX_4X4 unit comes from the |
| 20571 | // LEAF's own rect table row at the leaf's mi (`bsize` is the leaf, the |
| 20572 | // unit covers it whole), never the standalone square-4x4 table the old |
| 20573 | // `Reach::of(4, ..)` lookup used -- that said 1 where `has_tr_8x4` says 0 |
| 20574 | // at mi(27,44) (the oracle's own EC_PRED prints `n_tr=0` there), so the |
| 20575 | // D45 unit read undecoded pixels to its upper right. |
| 20576 | leaf_shape: (usize, usize), |
| 20577 | fctx: &crate::decode::FrameCtx, |
| 20578 | ) -> Result<(Grid, Grid)> { |
| 20579 | let uv_predict_mode = if uv_mode == UV_CFL_PRED { DC_PRED } else { uv_mode }; |
| 20580 | // lane-av1-422kf2: libaom writes one MB_MODE_INFO per mi cell the LEAF |
| 20581 | // covers, and `chroma_left_mbmi`/`chroma_above_mbmi` read whichever of |
| 20582 | // those cells the next block's snap lands on -- an 8x4 strip's SM_V |
| 20583 | // uv_mode must therefore be visible at BOTH of its mi columns, or the |
| 20584 | // block to its right (mi (30,50): left neighbour cell (30,49)) missed the |
| 20585 | // smooth neighbour and skipped libaom's strength-1 intra edge filter |
| 20586 | // (class context-read-from-one-cell, the 4:2:2 sub-8 twin of the |
| 20587 | // lane-kf900 snap fix). |
| 20588 | neighbours.record_uv_mode_mi( |
| 20589 | lmi.0, |
| 20590 | lmi.1, |
| 20591 | (leaf_shape.0 / MI).max(1), |
| 20592 | (leaf_shape.1 / MI).max(1), |
| 20593 | uv_predict_mode, |
| 20594 | ); |
| 20595 | let (px, py) = (lmi.1 * MI, lmi.0 * MI); |
| 20596 | // lane-av1-422kf2: the unit's WRITE/PREDICTION anchor is the same floored |
| 20597 | // chroma cell as the entropy anchor below -- libaom positions the sub-8 |
| 20598 | // chroma plane block at `av1_setup_dst_planes`' mi-exact floor |
| 20599 | // (`(mi_col & !1) * MI >> ss_x`), NOT at the odd leaf's own chroma x: |
| 20600 | // the oracle's mi(18,55) TX_4X4 unit reconstructed chroma px 108..111 |
| 20601 | // (a pure V-copy of the row above at 108..111), while the leaf's own |
| 20602 | // chroma x is 110 -- anchoring there left px 108..109 unwritten (zeros) |
| 20603 | // and stepped px 112..113 with the unit's right columns. |
| 20604 | let (cpx, cpy) = (((lmi.1 & !1) * MI) >> ss_x(fctx), py >> ss_y(fctx)); |
| 20605 | // lane-av1-422h: the entropy-context anchor is the unit's FLOOR chroma |
| 20606 | // cell (`pd->above_entropy_context + (mi_col >> ss_x)`): an ODD-column |
| 20607 | // leaf's 4-px unit starts at a chroma px that is NOT cell aligned, and |
| 20608 | // both its ctx read and its ctx write live in the cell containing that |
| 20609 | // start -- one luma mi column to the LEFT of the leaf's own. Even-column |
| 20610 | // (cell-aligned) leaves keep their own column. The state write spans the |
| 20611 | // anchor's two luma mi columns, so the second (odd) leaf of a split group |
| 20612 | // overwrites exactly the cell the first one wrote, as libaom's |
| 20613 | // `av1_set_entropy_contexts` per-TU writes do. |
| 20614 | let ctx_mi = (lmi.0, lmi.1 & !1); |
| 20615 | // lane-av1-422kf2: the square unit takes the leaf's OWN reach: a split |
| 20616 | // leaf is a standalone BLOCK_4X4 (square table), a rect pair's strip is |
| 20617 | // `of_rect` on the strip's true (8x4 / 4x8) shape -- libaom passes the |
| 20618 | // LEAF's bsize to `has_top_right`/`has_bottom_left`, so a rect strip's |
| 20619 | // answer comes from `has_tr_8x4`/`has_tr_4x8` at the strip's mi, not |
| 20620 | // from a 4x4-block lookup. |
| 20621 | let reach = if leaf_shape == (4, 4) { |
| 20622 | Reach::of(4, px, py, y.width, y.height, fctx) |
| 20623 | } else { |
| 20624 | Reach::of_rect(leaf_shape.0, leaf_shape.1, px, py, y.width, y.height, fctx) |
| 20625 | }; |
| 20626 | let chroma_around = neighbours.around_mi_422_chroma(ctx_mi, 8, 4); |
| 20627 | // lane-av1422sub8pix: the CfL AC signal spans the UNIT's luma footprint, |
| 20628 | // anchored at the same FLOORED luma x the write/prediction anchor below |
| 20629 | // uses -- `px` (the odd leaf's own luma x) steps half the span into the |
| 20630 | // RIGHT neighbour group (t422 frame-1 leaf (1,23): the unit reconstructs |
| 20631 | // chroma px 44..47 from luma 88..95, while a `px`-anchored source |
| 20632 | // averaged luma 92..99 and every CFL unit of the group mismatched). |
| 20633 | let ac = alpha.map(|_| cfl_src_rect((lmi.1 & !1) * MI, py, 8, 4)); |
| 20634 | let grids: (Grid, Grid) = if skip { |
| 20635 | // A skipped chroma-reference leaf still predicts (the CfL contribution |
| 20636 | // survives a skip); no coefficient symbol exists. |
| 20637 | if alpha.is_some() { |
| 20638 | hit!(CHROMA_SKIP_CFL_HITS); |
| 20639 | hit!(SKIP_CFL_HITS); |
| 20640 | } |
| 20641 | 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); |
| 20642 | 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); |
| 20643 | // The same arm-without-clear route the 4:2:0 tails count: a skipped |
| 20644 | // leaf reads no coefficient, so an armed intrabc slot is dropped. |
| 20645 | if fctx.intrabc_chroma_tx.with(std::cell::Cell::get).is_some() { |
| 20646 | hit!(SKIPPED_INTRABC_CHROMA_ARM_HITS); |
| 20647 | fctx.intrabc_chroma_tx.with(|c| c.set(None)); |
| 20648 | } |
| 20649 | (Grid::Zero(16), Grid::Zero(16)) |
| 20650 | } else if let Some(dv) = intrabc_dv { |
| 20651 | hit!(CHROMA422_SUB8_HITS); |
| 20652 | // Frame copy at the DV; the residual still reads the ordinary chroma |
| 20653 | // tables with the inherited luma `tx_type` (see the 4:2:0 tails). |
| 20654 | let mut ub = vec![0u16; 16]; |
| 20655 | mc::predict_with_filter( |
| 20656 | &u.data, u.width, u.true_width, u.true_height, |
| 20657 | mv_to_q4(cpx, dv.1, ss_x(fctx)), mv_to_q4(cpy, dv.0, ss_y(fctx)), |
| 20658 | 4, 4, mc::InterpFilterKind::Bilinear, &mut ub, fctx, |
| 20659 | ); |
| 20660 | let mut vb = vec![0u16; 16]; |
| 20661 | mc::predict_with_filter( |
| 20662 | &v.data, v.width, v.true_width, v.true_height, |
| 20663 | mv_to_q4(cpx, dv.1, ss_x(fctx)), mv_to_q4(cpy, dv.0, ss_y(fctx)), |
| 20664 | 4, 4, mc::InterpFilterKind::Bilinear, &mut vb, fctx, |
| 20665 | ); |
| 20666 | set_palette_pred(ub, fctx); |
| 20667 | let ug = read_plane( |
| 20668 | dec, cdfs, TxbSet::Chroma4, scan4, 1, chroma_around[1], DC_PRED, DC_PRED, |
| 20669 | 0, reach, u, cpx, cpy, 4, TX4, base_q_idx, |
| 20670 | None, None, None, smooth_neighbor_uv, fctx, |
| 20671 | )?; |
| 20672 | set_palette_pred(vb, fctx); |
| 20673 | let vg = read_plane( |
| 20674 | dec, cdfs, TxbSet::Chroma4, scan4, 2, chroma_around[2], DC_PRED, DC_PRED, |
| 20675 | 0, reach, v, cpx, cpy, 4, TX4, base_q_idx, |
| 20676 | None, None, None, smooth_neighbor_uv, fctx, |
| 20677 | )?; |
| 20678 | fctx.intrabc_chroma_tx.with(|c| c.set(None)); |
| 20679 | (ug, vg) |
| 20680 | } else { |
| 20681 | hit!(CHROMA422_SUB8_HITS); |
| 20682 | let ug = read_plane( |
| 20683 | dec, cdfs, TxbSet::Chroma4, scan4, 1, chroma_around[1], uv_mode, uv_predict_mode, |
| 20684 | angle_delta_uv, reach, u, cpx, cpy, 4, TX4, base_q_idx, |
| 20685 | alpha.zip(ac).map(|((au, _), ac)| (au, ac)), None, None, smooth_neighbor_uv, fctx, |
| 20686 | )?; |
| 20687 | let vg = read_plane( |
| 20688 | dec, cdfs, TxbSet::Chroma4, scan4, 2, chroma_around[2], uv_mode, uv_predict_mode, |
| 20689 | angle_delta_uv, reach, v, cpx, cpy, 4, TX4, base_q_idx, |
| 20690 | alpha.zip(ac).map(|((_, av), ac)| (av, ac)), None, None, smooth_neighbor_uv, fctx, |
| 20691 | )?; |
| 20692 | (ug, vg) |
| 20693 | }; |
| 20694 | // libaom `av1_set_entropy_contexts` stamps above/left PER TRANSFORM UNIT: |
| 20695 | // the unit's own chroma span -- 1 mi row tall, TWO luma mi columns wide |
| 20696 | // (one chroma column) -- not the 8x8 group's. |
| 20697 | let (u_grid, v_grid) = grids; |
| 20698 | let u_state = neighbour_state(&u_grid); |
| 20699 | let v_state = neighbour_state(&v_grid); |
| 20700 | if lmi.0 < neighbours.mi_rows { |
| 20701 | neighbours.left[ctx_mi.0][1] = u_state; |
| 20702 | neighbours.left[ctx_mi.0][2] = v_state; |
| 20703 | } |
| 20704 | for cell in 0..2 { |
| 20705 | if ctx_mi.1 + cell < neighbours.mi_cols { |
| 20706 | neighbours.above[ctx_mi.1 + cell][1] = u_state; |
| 20707 | neighbours.above[ctx_mi.1 + cell][2] = v_state; |
| 20708 | } |
| 20709 | } |
| 20710 | Ok((u_grid, v_grid)) |
| 20711 | } |
| 20712 | |
| 20713 | |
| 20714 | /// Decodes a `PARTITION_SPLIT` of one 8x8 block into four `BLOCK_4X4` leaves |
| 20715 | /// (spec `decode_partition`'s recursion bottom), sharing [`decode_leaf8`]'s |
| 20716 | /// own signature so its caller's `prev_leaf`/`above_mode`/`left_mode` |
| 20717 | /// bookkeeping needs no change. Each leaf reads its own luma mode info, |
| 20718 | /// neighboured off the *previously decoded leaf in this same group* on |
| 20719 | /// whichever axis is adjacent (mirroring [`decode_leaf8`]'s own `prev_leaf` |
| 20720 | /// pattern, one level deeper); chroma is read and reconstructed exactly |
| 20721 | /// once, on the last (bottom-right) leaf, at the whole 8x8 group's own 4x4 |
| 20722 | /// chroma unit -- [`read_intra_mode_sub8`]'s doc has the `is_chroma_reference` |
| 20723 | /// derivation. |
| 20724 | #[allow(clippy::too_many_arguments)] |
| 20725 | fn decode_leaf_split4( |
| 20726 | dec: &mut SymbolDecoder, |
| 20727 | cdfs: &mut Cdfs, |
| 20728 | neighbours: &mut Neighbours, |
| 20729 | outer_at: (usize, usize), |
| 20730 | leaf_mi: (usize, usize), |
| 20731 | scan4: &[u16], |
| 20732 | prev_leaf: Option<((usize, usize), usize)>, |
| 20733 | y: &mut PlaneBuf<'static>, |
| 20734 | u: &mut PlaneBuf<'static>, |
| 20735 | v: &mut PlaneBuf<'static>, |
| 20736 | base_q_idx: u8, |
| 20737 | enable_filter_intra: bool, |
| 20738 | allow_intrabc: bool, |
| 20739 | reduced_tx_set: bool, |
| 20740 | // Returns `(below_mode, right_mode)`: the mode a neighbour BELOW this 8x8 |
| 20741 | // group sees (its bottom-left 4x4, libaom reads mi(row-1, col)) and the |
| 20742 | // one a neighbour to its RIGHT sees (its top-right 4x4, mi(row, col-1))., fctx: &crate::decode::FrameCtx |
| 20743 | fctx: &crate::decode::FrameCtx, |
| 20744 | ) -> Result<(usize, usize)> { |
| 20745 | hit!(SUB8_SPLIT_HITS); |
| 20746 | let (r, c) = outer_at; |
| 20747 | let mut above_mode = neighbours.above_mode[c]; |
| 20748 | let mut left_mode = neighbours.left_mode[r]; |
| 20749 | if let Some(((pr, pc), pmode)) = prev_leaf { |
| 20750 | if pc == leaf_mi.1 && leaf_mi.0 == pr + 2 { |
| 20751 | above_mode = pmode; |
| 20752 | } else if pr == leaf_mi.0 && leaf_mi.1 == pc + 2 { |
| 20753 | left_mode = pmode; |
| 20754 | } |
| 20755 | } |
| 20756 | // See `decode_leaf8`'s own `smooth_neighbor_uv`: the chroma edge-filter |
| 20757 | // type is the chroma neighbours' `uv_mode`, mi-exact where recorded. |
| 20758 | let smooth_neighbor_uv = neighbours.smooth_uv_neighbour(leaf_mi.0, leaf_mi.1, r, c, fctx); |
| 20759 | let mut leaf_modes = [0usize; 4]; |
| 20760 | let mut leaf_skips = [false; 4]; |
| 20761 | let mut last_uv: Option<(usize, i32, Option<(i32, i32)>)> = None; |
| 20762 | // lane-av1txr: the chroma-reference (i==3) leaf's block vector when that |
| 20763 | // leaf is an intrabc block -- its prediction/reconstruction below is a |
| 20764 | // frame copy at this DV, not an intra one. |
| 20765 | let mut last_intrabc: Option<(i32, i32)> = None; |
| 20766 | // 4:4:4 (lane-av1-444): `is_chroma_reference` (av1_common_int.h:1459) is |
| 20767 | // unconditional at subsampling 0/0, so EVERY 4x4 leaf reads its own |
| 20768 | // `uv_mode` and codes its own full-resolution 4x4 chroma unit right |
| 20769 | // after its luma -- there is no shared group unit at all. |
| 20770 | let chroma_444 = ss_x(fctx) == 0 && ss_y(fctx) == 0; |
| 20771 | // lane-av1-422g: at ss (1,0) every even-mi-column leaf is its plane's own |
| 20772 | // chroma reference (`is_chroma_reference`'s `!(mi_col & ss_x)`) and codes |
| 20773 | // one TX_4X4 chroma unit per plane; odd-column leaves read no chroma. |
| 20774 | let chroma_422 = ss_x(fctx) == 1 && ss_y(fctx) == 0; |
| 20775 | // The last leaf's own `uv_predict_mode`, for the coarse above/left slots |
| 20776 | // the 4:2:0 tail fills from the group's single unit. |
| 20777 | let mut last_uv444 = DC_PRED; |
| 20778 | let mut last_uv422 = DC_PRED; |
| 20779 | for i in 0..4usize { |
| 20780 | let (dr, dc) = (i / 2, i % 2); |
| 20781 | let lmi = (leaf_mi.0 + dr, leaf_mi.1 + dc); |
| 20782 | // Neighbour modes at real mi granularity: inside the group the |
| 20783 | // sibling leaf, outside it the split-leaf map when the adjacent mi |
| 20784 | // was itself a 4x4 leaf, else the coarse 16x16 slot (see |
| 20785 | // `sub8_mode_col`'s doc). |
| 20786 | let leaf_above = if dr == 0 { |
| 20787 | neighbours.mode_above_mi(lmi.0, lmi.1).unwrap_or(above_mode) |
| 20788 | } else { |
| 20789 | leaf_modes[i - 2] |
| 20790 | }; |
| 20791 | let leaf_left = if dc == 0 { |
| 20792 | neighbours.mode_left_mi(lmi.0, lmi.1).unwrap_or(left_mode) |
| 20793 | } else { |
| 20794 | leaf_modes[i - 1] |
| 20795 | }; |
| 20796 | let has_chroma = if chroma_444 { |
| 20797 | true |
| 20798 | } else if chroma_422 { |
| 20799 | // lane-av1-422h: `is_chroma_reference`'s column clause at ss_x 1 is |
| 20800 | // `(mi_col & 1)` for a BLOCK_4X4 leaf -- the ODD-column leaf is the |
| 20801 | // chroma reference (oracle, mi(30,42) split group: uv_mode read at |
| 20802 | // mi(30,43) and mi(31,43) only). The even-column rule this |
| 20803 | // replaces came from 422f's pre-partition-trace misreading. |
| 20804 | (lmi.1 & 1) == 1 |
| 20805 | } else { |
| 20806 | i == 3 |
| 20807 | }; |
| 20808 | let skip_ctx = neighbours.skip_txfm_ctx(lmi.0, lmi.1); |
| 20809 | let (skip, mode, angle_delta_y, chroma, filter_intra, intrabc) = read_intra_mode_sub8( |
| 20810 | dec, cdfs, leaf_above, leaf_left, has_chroma, enable_filter_intra, |
| 20811 | filter_intra_size_class(4).expect("BLOCK_4X4 has its own filter_intra row"), |
| 20812 | skip_ctx, allow_intrabc, lmi.0, lmi.1, 1, 1, fctx, |
| 20813 | )?; |
| 20814 | leaf_modes[i] = mode; |
| 20815 | leaf_skips[i] = skip; |
| 20816 | neighbours.record_mode_mi(lmi.0, lmi.1, 1, 1, mode); |
| 20817 | // lane-av1txr: libaom `parse_decode_block`'s `set_txfm_ctxs` for this |
| 20818 | // `BLOCK_4X4` leaf -- EVERY block publishes its transform size into |
| 20819 | // the TXFM context bands, and an intrabc block's later `txfm_split` |
| 20820 | // context is read off them (`txfm_partition_context`). A 4x4 never |
| 20821 | // signals a tx size, so it is always TX_4X4 (`set_txfm_ctxs`'s |
| 20822 | // `skip && is_inter` branch writes the block's own 4 pixels -- the |
| 20823 | // same value). |
| 20824 | set_txfm_ctxs(neighbours, lmi, 4, 1, 1, false); |
| 20825 | let smooth_neighbor = is_smooth_mode(leaf_above) || is_smooth_mode(leaf_left); |
| 20826 | if smooth_neighbor { |
| 20827 | hit!(SMOOTH_LUMA_HITS); |
| 20828 | } |
| 20829 | let (px, py) = (lmi.1 * MI, lmi.0 * MI); |
| 20830 | let reach = Reach::of(4, px, py, y.width, y.height, fctx); |
| 20831 | if let Some(dv) = intrabc { |
| 20832 | // lane-av1txr: an intrabc `BLOCK_4X4` leaf -- prediction is the |
| 20833 | // CURRENT frame at the block vector (`predict_inter_block`'s |
| 20834 | // intrabc arm, spec 7.11.3.4), residual the INTER coefficient set |
| 20835 | // (libaom reads `tx_type` off `inter_ext_tx_cdf` for |
| 20836 | // `is_inter_block`, which counts intrabc). Published into |
| 20837 | // `intrabc_dv` so the group's chroma arm (below, when the |
| 20838 | // chroma-reference leaf is the intrabc one) and |
| 20839 | // [`record_intrabc_mi`] see it. |
| 20840 | flush_recon(fctx, y, u, v); |
| 20841 | let mut yb = vec![0u16; 16]; |
| 20842 | mc::predict_with_filter( |
| 20843 | &y.data, y.width, y.true_width, y.true_height, |
| 20844 | mv_to_q4(px, dv.1, 0), mv_to_q4(py, dv.0, 0), |
| 20845 | 4, 4, mc::InterpFilterKind::Bilinear, &mut yb, fctx, |
| 20846 | ); |
| 20847 | set_palette_pred(yb.clone(), fctx); |
| 20848 | if skip { |
| 20849 | push_intra(0, px, py, 4, DC_PRED, 0, reach, &ZERO_RESIDUAL[..16], None, None, smooth_neighbor, fctx); |
| 20850 | neighbours.record_mi_luma(lmi, 4, &ZERO_RESIDUAL[..16]); |
| 20851 | } else { |
| 20852 | let tu_around = neighbours.around_mi(lmi, 4)[0]; |
| 20853 | let tu_grid = read_plane( |
| 20854 | dec, |
| 20855 | cdfs, |
| 20856 | if reduced_tx_set { TxbSet::Luma4Inter } else { TxbSet::Luma4InterSet1 }, |
| 20857 | scan4, |
| 20858 | 0, |
| 20859 | tu_around, |
| 20860 | DC_PRED, |
| 20861 | DC_PRED, |
| 20862 | 0, |
| 20863 | reach, |
| 20864 | y, |
| 20865 | px, |
| 20866 | py, |
| 20867 | 4, |
| 20868 | 4, |
| 20869 | base_q_idx, |
| 20870 | None, |
| 20871 | None, |
| 20872 | None, |
| 20873 | smooth_neighbor, fctx, |
| 20874 | )?; |
| 20875 | neighbours.record_mi_luma(lmi, 4, &tu_grid); |
| 20876 | } |
| 20877 | neighbours.fill_skip_grid(lmi, 1, skip); |
| 20878 | neighbours.fill_lf_grid(lmi, 1, 4, 0, fctx); |
| 20879 | record_intrabc_mi(lmi.0, lmi.1, 1, Some(dv), neighbours, fctx); |
| 20880 | if has_chroma { |
| 20881 | // An intrabc block is an INTER block for `av1_get_tx_type`, so |
| 20882 | // the chroma inherits the LUMA's coded transform type |
| 20883 | // (`xd->tx_type_map` at the co-located luma position) -- the |
| 20884 | // luma `read_plane` above just set [`LUMA_TX_TYPE`]. Armed for |
| 20885 | // exactly this leaf's two chroma reads. |
| 20886 | fctx.intrabc_chroma_tx.with(|c| c.set(Some(fctx.luma_tx_type.with(std::cell::Cell::get)))); |
| 20887 | if chroma_444 { |
| 20888 | // lane-av1-444: this leaf codes its OWN 4x4 chroma unit |
| 20889 | // right here (frame copy at the DV); no group tail. |
| 20890 | sub8_leaf_chroma444( |
| 20891 | dec, cdfs, neighbours, lmi, 4, 4, DC_PRED, None, skip, |
| 20892 | Some(dv), y, u, v, scan4, base_q_idx, |
| 20893 | neighbours.smooth_uv_neighbour(lmi.0, lmi.1, r, c, fctx), fctx, |
| 20894 | )?; |
| 20895 | last_uv444 = DC_PRED; |
| 20896 | } else if chroma_422 { |
| 20897 | sub8_leaf_chroma422( |
| 20898 | dec, cdfs, neighbours, lmi, DC_PRED, 0, None, skip, |
| 20899 | Some(dv), y, u, v, scan4, base_q_idx, |
| 20900 | neighbours.smooth_uv_neighbour(lmi.0, lmi.1, r, c, fctx), |
| 20901 | (4, 4), fctx, |
| 20902 | )?; |
| 20903 | last_uv422 = DC_PRED; |
| 20904 | } else { |
| 20905 | last_uv = Some((DC_PRED, 0, None)); |
| 20906 | // lane-av1txbands: the group's chroma is the CHROMA-REFERENCE |
| 20907 | // leaf's own block -- libaom codes one chroma unit under the |
| 20908 | // last sub-block only (`is_chroma_reference`), so its prediction |
| 20909 | // is that leaf's `uv_mode` (CfL alpha and all) unless THAT leaf |
| 20910 | // is the intrabc one. Arming this from a non-chroma-reference |
| 20911 | // leaf's DV made `warped.obu`'s 8x8 group at mi(40,62) copy |
| 20912 | // frame bytes at mi(41,62)'s vector where aomdec predicts |
| 20913 | // `uv_mode=UV_CFL_PRED`: luma stayed byte-exact and only chroma |
| 20914 | // moved (class chroma-keyed-at-a-non-chroma-leaf). Mirrors |
| 20915 | // [`decode_leaf_rect8`]'s own placement. |
| 20916 | last_intrabc = Some(dv); |
| 20917 | } |
| 20918 | } |
| 20919 | continue; |
| 20920 | } |
| 20921 | if skip { |
| 20922 | push_intra(0, px, py, 4, mode, angle_delta_y, reach, &ZERO_RESIDUAL[..16], None, filter_intra, smooth_neighbor, fctx); |
| 20923 | neighbours.record_mi_luma(lmi, 4, &ZERO_RESIDUAL[..16]); |
| 20924 | } else { |
| 20925 | let tu_around = neighbours.around_mi(lmi, 4)[0]; |
| 20926 | // A `BLOCK_4X4` leaf's luma TU *is* the whole block, so libaom's |
| 20927 | // `get_txb_ctx` takes its `plane_bsize == txsize_to_bsize[tx_size]` |
| 20928 | // branch: `txb_skip_ctx = 0`, never the neighbour-magnitude table |
| 20929 | // (`txb_common.h:400`). Reading the table here decoded `all_zero` |
| 20930 | // off the wrong CDF on the very first leaf and desynced the tile. |
| 20931 | let tu_grid = read_plane( |
| 20932 | dec, |
| 20933 | cdfs, |
| 20934 | if reduced_tx_set { TxbSet::Luma4 } else { TxbSet::Luma4Set1 }, |
| 20935 | scan4, |
| 20936 | 0, |
| 20937 | tu_around, |
| 20938 | mode, |
| 20939 | mode, |
| 20940 | angle_delta_y, |
| 20941 | reach, |
| 20942 | y, |
| 20943 | px, |
| 20944 | py, |
| 20945 | 4, |
| 20946 | 4, |
| 20947 | base_q_idx, |
| 20948 | None, |
| 20949 | filter_intra, |
| 20950 | None, |
| 20951 | smooth_neighbor, fctx, |
| 20952 | )?; |
| 20953 | neighbours.record_mi_luma(lmi, 4, &tu_grid); |
| 20954 | } |
| 20955 | neighbours.fill_skip_grid(lmi, 1, skip); |
| 20956 | neighbours.fill_lf_grid(lmi, 1, 4, 0, fctx); |
| 20957 | if has_chroma { |
| 20958 | if chroma_444 { |
| 20959 | // lane-av1-444: the leaf's own 4x4 chroma unit, coded right |
| 20960 | // after its luma (no shared group unit at ss 0/0). |
| 20961 | let (uv_mode, _, alpha) = |
| 20962 | chroma.expect("4:4:4 reads uv_mode on every leaf"); |
| 20963 | sub8_leaf_chroma444( |
| 20964 | dec, cdfs, neighbours, lmi, 4, 4, uv_mode, alpha, skip, |
| 20965 | None, y, u, v, scan4, base_q_idx, |
| 20966 | neighbours.smooth_uv_neighbour(lmi.0, lmi.1, r, c, fctx), fctx, |
| 20967 | )?; |
| 20968 | last_uv444 = if uv_mode == UV_CFL_PRED { DC_PRED } else { uv_mode }; |
| 20969 | } else if chroma_422 { |
| 20970 | let (uv_mode, angle_delta_uv, alpha) = |
| 20971 | chroma.expect("4:2:2 reads uv_mode on even-column leaves"); |
| 20972 | sub8_leaf_chroma422( |
| 20973 | dec, cdfs, neighbours, lmi, uv_mode, angle_delta_uv, alpha, skip, |
| 20974 | None, y, u, v, scan4, base_q_idx, |
| 20975 | neighbours.smooth_uv_neighbour(lmi.0, lmi.1, r, c, fctx), |
| 20976 | (4, 4), fctx, |
| 20977 | )?; |
| 20978 | last_uv422 = if uv_mode == UV_CFL_PRED { DC_PRED } else { uv_mode }; |
| 20979 | } else { |
| 20980 | last_uv = chroma; |
| 20981 | } |
| 20982 | } |
| 20983 | } |
| 20984 | // lane-av1mono: as [`decode_leaf_rect8`] -- a monochrome frame has no |
| 20985 | // chroma to reconstruct and no uv_mode was read. |
| 20986 | let (u_grid, v_grid): (Grid, Grid) = if mono(fctx) { |
| 20987 | (Grid::Zero(16), Grid::Zero(16)) |
| 20988 | } else if chroma_444 { |
| 20989 | // lane-av1-444: every leaf coded and reconstructed its own chroma in |
| 20990 | // the loop above ([`sub8_leaf_chroma444`] stamped the mi-exact maps |
| 20991 | // and per-leaf entropy state); only the coarse slots remain, filled |
| 20992 | // from the bottom-right leaf like the 4:2:0 tail's single unit. |
| 20993 | neighbours.above_uv_mode[c] = last_uv444; |
| 20994 | neighbours.left_uv_mode[r] = last_uv444; |
| 20995 | (Grid::Zero(16), Grid::Zero(16)) |
| 20996 | } else if chroma_422 { |
| 20997 | // lane-av1-422g: each even-column leaf coded and reconstructed its |
| 20998 | // own TX_4X4 chroma unit in the loop above (sub8_leaf_chroma422 |
| 20999 | // stamped the per-leaf entropy state); only the coarse slots remain, |
| 21000 | // filled from the group's last chroma-reference leaf (bottom-left). |
| 21001 | neighbours.above_uv_mode[c] = last_uv422; |
| 21002 | neighbours.left_uv_mode[r] = last_uv422; |
| 21003 | (Grid::Zero(16), Grid::Zero(16)) |
| 21004 | } else { |
| 21005 | let (gpx, gpy) = (leaf_mi.1 * MI, leaf_mi.0 * MI); |
| 21006 | let (cpx, cpy) = (gpx >> ss_x(fctx), gpy >> ss_y(fctx)); |
| 21007 | let group_reach = Reach::of(8, gpx, gpy, y.width, y.height, fctx); |
| 21008 | let (uv_mode, angle_delta_uv, alpha) = |
| 21009 | last_uv.expect("i==3 always sets has_chroma, so chroma is always Some"); |
| 21010 | let uv_predict_mode = if uv_mode == UV_CFL_PRED { DC_PRED } else { uv_mode }; |
| 21011 | neighbours.above_uv_mode[c] = uv_predict_mode; |
| 21012 | neighbours.left_uv_mode[r] = uv_predict_mode; |
| 21013 | neighbours.record_uv_mode_mi(leaf_mi.0, leaf_mi.1, 2, 2, uv_predict_mode); |
| 21014 | let (u_grid, v_grid): (Grid, Grid) = if leaf_skips[3] { |
| 21015 | // A skipped chroma block still gets its CfL contribution: libaom's |
| 21016 | // `predict_and_reconstruct_intra_block` calls `cfl_predict_block` |
| 21017 | // from `av1_predict_intra_block_facade` (av1/common/reconintra.c:1719) |
| 21018 | // whenever `mbmi->uv_mode == UV_CFL_PRED`, before any residual is |
| 21019 | // added -- `skip_txfm` only drops the residual, never the alpha. |
| 21020 | // lane-reelckf and lane-kf1200 fixed THIS SAME arm independently; the |
| 21021 | // merge keeps one fix and feeds both lanes' counters so neither of |
| 21022 | // their gates goes vacuous. |
| 21023 | let ac = alpha.map(|_| cfl_src(gpx, gpy, 8)); |
| 21024 | if alpha.is_some() { |
| 21025 | hit!(CHROMA_SKIP_CFL_HITS); |
| 21026 | hit!(SKIP_CFL_HITS); |
| 21027 | } |
| 21028 | 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); |
| 21029 | 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); |
| 21030 | // lane-av1txbands: a SKIPPED chroma-reference leaf reads no chroma |
| 21031 | // coefficient, so it never consults the inherited luma transform type |
| 21032 | // -- but an intrabc chroma-reference leaf did arm |
| 21033 | // [`INTRABC_CHROMA_TX`] above (`has_chroma`), and the two coefficient |
| 21034 | // branches below are the only ones that disarm it. Left armed, the slot |
| 21035 | // picks the NEXT block's chroma scan and inverse transform (class |
| 21036 | // arm-without-clear-on-a-skip-route). Counted so a gate can tell a |
| 21037 | // stream that reaches this route from one that never does. |
| 21038 | if fctx.intrabc_chroma_tx.with(std::cell::Cell::get).is_some() { |
| 21039 | hit!(SKIPPED_INTRABC_CHROMA_ARM_HITS); |
| 21040 | fctx.intrabc_chroma_tx.with(|c| c.set(None)); |
| 21041 | } |
| 21042 | (Grid::Zero(16), Grid::Zero(16)) |
| 21043 | } else if let Some(dv) = last_intrabc { |
| 21044 | // lane-av1txr: the chroma-reference leaf was an intrabc block, so the |
| 21045 | // group's one chroma unit is a frame copy at that DV too (its residual |
| 21046 | // still reads the ordinary `Chroma4` coefficient tables -- libaom |
| 21047 | // codes a chroma `tx_type` nowhere). |
| 21048 | let mut ub = vec![0u16; 16]; |
| 21049 | mc::predict_with_filter( |
| 21050 | &u.data, u.width, u.true_width, u.true_height, |
| 21051 | mv_to_q4(cpx, dv.1, ss_x(fctx)), mv_to_q4(cpy, dv.0, ss_y(fctx)), |
| 21052 | 4, 4, mc::InterpFilterKind::Bilinear, &mut ub, fctx, |
| 21053 | ); |
| 21054 | let mut vb = vec![0u16; 16]; |
| 21055 | mc::predict_with_filter( |
| 21056 | &v.data, v.width, v.true_width, v.true_height, |
| 21057 | mv_to_q4(cpx, dv.1, ss_x(fctx)), mv_to_q4(cpy, dv.0, ss_y(fctx)), |
| 21058 | 4, 4, mc::InterpFilterKind::Bilinear, &mut vb, fctx, |
| 21059 | ); |
| 21060 | let chroma_around = neighbours.around_mi(leaf_mi, 8); |
| 21061 | set_palette_pred(ub, fctx); |
| 21062 | let ug = read_plane( |
| 21063 | dec, cdfs, TxbSet::Chroma4, scan4, 1, chroma_around[1], DC_PRED, DC_PRED, |
| 21064 | 0, group_reach, u, cpx, cpy, 4, TX4, base_q_idx, |
| 21065 | None, None, None, smooth_neighbor_uv, fctx, |
| 21066 | )?; |
| 21067 | set_palette_pred(vb, fctx); |
| 21068 | let vg = read_plane( |
| 21069 | dec, cdfs, TxbSet::Chroma4, scan4, 2, chroma_around[2], DC_PRED, DC_PRED, |
| 21070 | 0, group_reach, v, cpx, cpy, 4, TX4, base_q_idx, |
| 21071 | None, None, None, smooth_neighbor_uv, fctx, |
| 21072 | )?; |
| 21073 | fctx.intrabc_chroma_tx.with(|c| c.set(None)); |
| 21074 | (ug, vg) |
| 21075 | } else { |
| 21076 | let ac = alpha.map(|_| cfl_src(gpx, gpy, 8)); |
| 21077 | let chroma_around = neighbours.around_mi(leaf_mi, 8); |
| 21078 | let ug = read_plane( |
| 21079 | dec, cdfs, TxbSet::Chroma4, scan4, 1, chroma_around[1], uv_mode, uv_predict_mode, |
| 21080 | angle_delta_uv, group_reach, u, cpx, cpy, 4, TX4, base_q_idx, |
| 21081 | alpha.zip(ac).map(|((au, _), ac)| (au, ac)), None, None, smooth_neighbor_uv, fctx, |
| 21082 | )?; |
| 21083 | let vg = read_plane( |
| 21084 | dec, cdfs, TxbSet::Chroma4, scan4, 2, chroma_around[2], uv_mode, uv_predict_mode, |
| 21085 | angle_delta_uv, group_reach, v, cpx, cpy, 4, TX4, base_q_idx, |
| 21086 | alpha.zip(ac).map(|((_, av), ac)| (av, ac)), None, None, smooth_neighbor_uv, fctx, |
| 21087 | )?; |
| 21088 | (ug, vg) |
| 21089 | }; |
| 21090 | (u_grid, v_grid) |
| 21091 | }; |
| 21092 | // Chroma neighbour bookkeeping for the whole 8x8 group -- mirrors |
| 21093 | // [`decode_leaf8`]'s own tx4-chroma section (`record_split_luma`'s |
| 21094 | // rescaling is wrong for an already-mi-unit position, so this inlines |
| 21095 | // its tail directly at `leaf_mi`, side_mi=2), EXCEPT for the partition |
| 21096 | // context value itself. Unlike `decode_leaf8`'s TX4 branch (still one |
| 21097 | // `BLOCK_8X8`, `PARTITION_NONE`, transform depth only), this group is |
| 21098 | // genuinely four `BLOCK_4X4` blocks (`PARTITION_SPLIT` at the 8x8 |
| 21099 | // level, libaom `decode_partition`'s `bsize2`/`subsize` recursion |
| 21100 | // bottom). libaom's `update_ext_partition_context` writes |
| 21101 | // `partition_context_lookup[subsize]` at the 8x8 slot -- `{30,30}` for |
| 21102 | // `BLOCK_8X8` (bit0=0, what `decode_leaf8` already reproduces via |
| 21103 | // side_mi=8) but `{31,31}` for `BLOCK_4X4` (bit0=1) here, and |
| 21104 | // `partition_ctx_mi`'s `left_side_mi[mi_r]*2 <= side` needs |
| 21105 | // `left_side_mi <= 4` to read that bit as 1. Leaving this at 8 (this |
| 21106 | // round's bug) makes the very next sibling 8x8's `partition_w8` read |
| 21107 | // with the wrong context and desync immediately. |
| 21108 | let side_mi = 2; |
| 21109 | for cell in 0..side_mi { |
| 21110 | neighbours.left_side_mi[leaf_mi.0 + cell] = 4; |
| 21111 | neighbours.above_side_mi[leaf_mi.1 + cell] = 4; |
| 21112 | } |
| 21113 | // lane-av1-444: at 4:4:4 [`sub8_leaf_chroma444`] already stamped each |
| 21114 | // leaf's own span as it decoded; these placeholder grids carry no state. |
| 21115 | // lane-av1-422g: the 4:2:2 per-leaf units stamp the same way. |
| 21116 | if !chroma_444 && !chroma_422 { |
| 21117 | let round_up_even = |n: usize| n.div_ceil(2) * 2; |
| 21118 | let bound_h = round_up_even(neighbours.mi_rows); |
| 21119 | let bound_w = round_up_even(neighbours.mi_cols); |
| 21120 | let u_state = neighbour_state(&u_grid); |
| 21121 | let v_state = neighbour_state(&v_grid); |
| 21122 | for cell in 0..side_mi { |
| 21123 | if cell < side_mi.min(bound_h.saturating_sub(leaf_mi.0)) { |
| 21124 | neighbours.left[leaf_mi.0 + cell][1] = u_state; |
| 21125 | neighbours.left[leaf_mi.0 + cell][2] = v_state; |
| 21126 | } |
| 21127 | if cell < side_mi.min(bound_w.saturating_sub(leaf_mi.1)) { |
| 21128 | neighbours.above[leaf_mi.1 + cell][1] = u_state; |
| 21129 | neighbours.above[leaf_mi.1 + cell][2] = v_state; |
| 21130 | } |
| 21131 | } |
| 21132 | } |
| 21133 | // lane-dkey: a sub-8x8 leaf can never use palette (`av1_allow_palette` |
| 21134 | // needs BLOCK_8X8 and up), and libaom's mi grid says so -- its |
| 21135 | // `palette_size` is 0 in every one of these mi cells, so the NEXT block's |
| 21136 | // `av1_get_palette_mode_ctx` and colour cache read 0 here. This decoder |
| 21137 | // only ever WROTE palette state from the >=8x8 readers, so an 8x8 palette |
| 21138 | // block above a 4x4 group left its size/colours standing in the mi-granular |
| 21139 | // bands: the block below the group read `palette_y_mode` off CDF row 1 |
| 21140 | // where libaom reads row 0 (class [[cdf-row-held-constant]]), decoded a |
| 21141 | // palette libaom never coded, and then skipped the `use_filter_intra` |
| 21142 | // symbol that a palette block has none of -- one symbol short, tile |
| 21143 | // desynced. Publishing the group's own zeros is the clear. |
| 21144 | neighbours.record_palette_y_rect(leaf_mi, 8, 8, 0, [0u16; 8]); |
| 21145 | neighbours.record_palette_uv_rect(leaf_mi, 8, 8, 0, [0u16; 8]); |
| 21146 | Ok((leaf_modes[2], leaf_modes[1])) |
| 21147 | } |
| 21148 | |
| 21149 | /// Decodes a `PARTITION_VERT`/`PARTITION_HORZ` of one 8x8 block into two |
| 21150 | /// `BLOCK_4X8`/`BLOCK_8X4` leaves (lane-tx4x8), the last partition shape |
| 21151 | /// below 8x8 this decoder refused. Mirrors [`decode_leaf_split4`] element for |
| 21152 | /// element -- same mode reads ([`read_intra_mode_sub8`]: no angle delta, no |
| 21153 | /// tx-depth symbol below 8x8), same "chroma once, on the last sub-block, at |
| 21154 | /// the whole 8x8 group's 4x4 unit" rule (`is_chroma_reference` is false for |
| 21155 | /// every sub-block one mi wide or high but the last) -- differing only where |
| 21156 | /// the shape does: |
| 21157 | /// |
| 21158 | /// * the transform is a true `TX_4X8`/`TX_8X4` |
| 21159 | /// ([`dequant_and_inverse_typed_wh`], whose `abs(rect_type) == 1` scale and |
| 21160 | /// `av1_inv_txfm_shift_ls` row already cover this size pair); |
| 21161 | /// * the CDF set is [`TxbSet::LumaRect4x8`] (`get_txsize_entropy_ctx` squares |
| 21162 | /// up to `TX_8X8`) with a 32-position `eob_pt` and the `TX_4X4` `tx_type` |
| 21163 | /// row -- see that variant's doc comment; |
| 21164 | /// * the scan is [`SCAN_4X8`]/[`SCAN_8X4`], or their `mrow`/`mcol` siblings |
| 21165 | /// ([`class_scan_table_wh`]) when the coded `tx_type` is `V_DCT`/`H_DCT`; |
| 21166 | /// * `Reach` comes from libaom's own `has_tr_4x8`/`has_bl_8x4` rows |
| 21167 | /// ([`Reach::of_rect`]); |
| 21168 | /// * `update_partition_context` writes `partition_context_lookup[BLOCK_4X8]` |
| 21169 | /// = `{31, 30}` (width 4 above, height 8 left), not the `{31, 31}` a |
| 21170 | /// `BLOCK_4X4` split writes. |
| 21171 | /// |
| 21172 | /// Filter intra on one of these leaves is decoded (lane-fi8 r1): the flag is |
| 21173 | /// read from `BLOCK_4X8`/`BLOCK_8X4`'s own `cdf::FILTER_INTRA` row (classes |
| 21174 | /// 8/9, NOT `BLOCK_4X4`'s), the predictor is [`crate::intra::predict_filter_intra`] |
| 21175 | /// over the leaf's true `bw`x`bh` -- per 4x4 transform unit, with that unit's |
| 21176 | /// own edge, when `tx_depth` splits it -- and the coded `tx_type` row is |
| 21177 | /// [`fi_tx_row`]'s `fimode_to_intradir`, not the block's raw `DC_PRED`. |
| 21178 | #[allow(clippy::too_many_arguments)] |
| 21179 | fn decode_leaf_rect8( |
| 21180 | dec: &mut SymbolDecoder, |
| 21181 | cdfs: &mut Cdfs, |
| 21182 | neighbours: &mut Neighbours, |
| 21183 | outer_at: (usize, usize), |
| 21184 | leaf_mi: (usize, usize), |
| 21185 | vert: bool, |
| 21186 | scan4: &[u16], |
| 21187 | y: &mut PlaneBuf<'static>, |
| 21188 | u: &mut PlaneBuf<'static>, |
| 21189 | v: &mut PlaneBuf<'static>, |
| 21190 | base_q_idx: u8, |
| 21191 | enable_filter_intra: bool, |
| 21192 | allow_intrabc: bool, |
| 21193 | tx_select: bool, |
| 21194 | reduced_tx_set: bool, fctx: &crate::decode::FrameCtx, |
| 21195 | ) -> Result<(usize, usize)> { |
| 21196 | let (r, c) = outer_at; |
| 21197 | // lane-modectx r1: availability-correct fallback pair (see |
| 21198 | // `modes_above_left`), not the raw coarse bands. |
| 21199 | let (above_mode, left_mode) = neighbours.modes_above_left(r, c); |
| 21200 | let smooth_neighbor_uv = neighbours.smooth_uv_neighbour(leaf_mi.0, leaf_mi.1, r, c, fctx); |
| 21201 | let (bw, bh) = if vert { (4, 8) } else { (8, 4) }; |
| 21202 | let (w_mi, h_mi) = (bw / MI, bh / MI); |
| 21203 | let scan: &[u16] = if vert { &SCAN_4X8 } else { &SCAN_8X4 }; |
| 21204 | let mut leaf_modes = [0usize; 2]; |
| 21205 | let mut leaf_skips = [false; 2]; |
| 21206 | let mut last_uv: Option<(usize, i32, Option<(i32, i32)>)> = None; |
| 21207 | // lane-av1txr: as [`decode_leaf_split4`]'s own `last_intrabc`. |
| 21208 | let mut last_intrabc: Option<(i32, i32)> = None; |
| 21209 | // 4:4:4 (lane-av1-444): as [`decode_leaf_split4`] -- at subsampling 0/0 |
| 21210 | // EVERY 4x8/8x4 leaf reads its own `uv_mode` and codes its own |
| 21211 | // full-resolution rect chroma unit right after its luma. |
| 21212 | let chroma_444 = ss_x(fctx) == 0 && ss_y(fctx) == 0; |
| 21213 | let mut last_uv444 = DC_PRED; |
| 21214 | // lane-av1-422h: at ss (1,0) `is_chroma_reference` |
| 21215 | // (av1_common_int.h:1459) resolves per strip shape -- |
| 21216 | // `((mi_row & 1) || !(bh_mi & 1) || !ss_y) && ((mi_col & 1) || !(bw_mi & 1) || !ss_x)`: |
| 21217 | // a HORZ pair is two BLOCK_8X4 strips at the group's even column, so |
| 21218 | // BOTH strips are chroma references (`!(bw & 1)`); a VERT pair is two |
| 21219 | // BLOCK_4X8 strips, and the column clause keeps only the ODD-column |
| 21220 | // strip (i == 1, the same leaf the 4:2:0 rule reads chroma on). |
| 21221 | let chroma_422 = ss_x(fctx) == 1 && ss_y(fctx) == 0; |
| 21222 | let mut last_uv422 = DC_PRED; |
| 21223 | for i in 0..2usize { |
| 21224 | let lmi = if vert { |
| 21225 | (leaf_mi.0, leaf_mi.1 + i) |
| 21226 | } else { |
| 21227 | (leaf_mi.0 + i, leaf_mi.1) |
| 21228 | }; |
| 21229 | // Inside the pair the sibling is the neighbour on the split axis; |
| 21230 | // outside it the mi-granular map, else the coarse 16x16 slot. |
| 21231 | let leaf_above = if !vert && i == 1 { |
| 21232 | leaf_modes[0] |
| 21233 | } else { |
| 21234 | neighbours.mode_above_mi(lmi.0, lmi.1).unwrap_or(above_mode) |
| 21235 | }; |
| 21236 | let leaf_left = if vert && i == 1 { |
| 21237 | leaf_modes[0] |
| 21238 | } else { |
| 21239 | neighbours.mode_left_mi(lmi.0, lmi.1).unwrap_or(left_mode) |
| 21240 | }; |
| 21241 | let has_chroma = if chroma_444 { |
| 21242 | true |
| 21243 | } else if chroma_422 { |
| 21244 | !vert || i == 1 |
| 21245 | } else { |
| 21246 | i == 1 |
| 21247 | }; |
| 21248 | let skip_ctx = neighbours.skip_txfm_ctx(lmi.0, lmi.1); |
| 21249 | let (skip, mode, angle_delta_y, chroma, filter_intra, intrabc) = read_intra_mode_sub8( |
| 21250 | dec, cdfs, leaf_above, leaf_left, has_chroma, enable_filter_intra, |
| 21251 | filter_intra_size_class_rect(bw, bh) |
| 21252 | .expect("BLOCK_4X8/BLOCK_8X4 are inside av1_filter_intra_allowed_bsize"), |
| 21253 | skip_ctx, allow_intrabc, lmi.0, lmi.1, bw / 4, bh / 4, fctx, |
| 21254 | )?; |
| 21255 | leaf_modes[i] = mode; |
| 21256 | leaf_skips[i] = skip; |
| 21257 | neighbours.record_mode_mi(lmi.0, lmi.1, w_mi, h_mi, mode); |
| 21258 | let smooth_neighbor = is_smooth_mode(leaf_above) || is_smooth_mode(leaf_left); |
| 21259 | if let Some(dv) = intrabc { |
| 21260 | // lane-av1-ibc444rect: the unskipped lossless luma walk this lane |
| 21261 | // fixes (one rect unit -> per-4x4 inter units). |
| 21262 | if !skip && lossless(fctx) { |
| 21263 | hit!(INTRABC_RECT8_LOSSLESS_LUMA_HITS); |
| 21264 | } |
| 21265 | // lane-av1txr: an intrabc `BLOCK_4X8`/`BLOCK_8X4` leaf. The |
| 21266 | // prediction is a frame copy at the DV and the residual reads the |
| 21267 | // INTER `LumaRect8x4` set -- but with `TxMode::Select` the leaf |
| 21268 | // reads the INTER var-tx tree, exactly like every other intrabc |
| 21269 | // block: libaom's `block_signals_txsize` is `bsize > BLOCK_4X4` |
| 21270 | // (`blockd.h:1027`), TRUE at `BLOCK_4X8`/`BLOCK_8X4`, so |
| 21271 | // `parse_decode_block` sends an unskipped `inter_block_tx` block |
| 21272 | // (intrabc counts, `is_inter_block`) of a signalling bsize into |
| 21273 | // `read_tx_size_vartx` over `max_txsize_rect_lookup[BLOCK_4X8] == |
| 21274 | // TX_4X8`; only `BLOCK_4X4` reads no tx symbol at all. Measured on |
| 21275 | // `warped.obu` mi (37,62): reading NO symbol here left the |
| 21276 | // arithmetic decoder one symbol short and desynced the very next |
| 21277 | // block's `skip` (ours 60320 vs aomdec 48630, class |
| 21278 | // `symbol-consumption-gap`). |
| 21279 | let (px, py) = (lmi.1 * MI, lmi.0 * MI); |
| 21280 | let reach = Reach::of_rect(bw, bh, px, py, y.width, y.height, fctx); |
| 21281 | flush_recon(fctx, y, u, v); |
| 21282 | let mut luma_leaves: Vec<(usize, usize, usize, usize)> = Vec::new(); |
| 21283 | // The non-var-tx band publish, deferred to AFTER the unit loop: |
| 21284 | // libaom's `set_txfm_ctxs` runs at `parse_decode_block`'s tail, so |
| 21285 | // a unit's own `txb_skip_ctx` read must see the PRE-block bands. |
| 21286 | let mut publish = Some((bw, bh)); |
| 21287 | if tx_select && !skip { |
| 21288 | // `max_block_wide`/`max_block_high`: the frame's own mi grid, |
| 21289 | // so a leaf hanging over the right/bottom edge reads no split |
| 21290 | // symbol for the units outside it (as in `decode_block`). |
| 21291 | let mi_cols = 2 * y.true_width.div_ceil(8); |
| 21292 | let mi_rows = 2 * y.true_height.div_ceil(8); |
| 21293 | let max_w_mi = w_mi.min(mi_cols.saturating_sub(lmi.1)); |
| 21294 | let max_h_mi = h_mi.min(mi_rows.saturating_sub(lmi.0)); |
| 21295 | read_var_tx_size( |
| 21296 | dec, |
| 21297 | cdfs, |
| 21298 | neighbours, |
| 21299 | lmi, |
| 21300 | bw.max(bh), |
| 21301 | (max_w_mi, max_h_mi), |
| 21302 | (bw, bh), |
| 21303 | 0, |
| 21304 | 0, |
| 21305 | 0, |
| 21306 | &mut luma_leaves, |
| 21307 | ); |
| 21308 | hit!(RECT_INTRABC_VARTX_HITS); |
| 21309 | publish = None; |
| 21310 | } else if lossless(fctx) && !skip { |
| 21311 | // lane-av1-ibc444rect: `read_tx_size`'s lossless first line |
| 21312 | // (`decodeframe.c:1203`) answers TX_4X4 for EVERY plane before |
| 21313 | // any tree, and a lossless frame's `TxMode` is `ONLY_4X4` |
| 21314 | // (spec 6.8.20) so no var-tx tree is read either -- the leaf's |
| 21315 | // coefficients are (bw/4)*(bh/4) inter TX_4X4 units at their |
| 21316 | // own origins, plane-major, NOT the single rect transform the |
| 21317 | // non-lossless arm reads (that one is |
| 21318 | // `av1_get_max_uv_txsize(BLOCK_8X4)`'s rect, unreachable at |
| 21319 | // lossless). Same unit structure the non-intrabc lossless |
| 21320 | // split below walks; each unit predicts from the CURRENT |
| 21321 | // frame at the DV. Measured on the lane's `e4` fixture |
| 21322 | // (`ll444_ibc_rect.obu`): the single-rect read desynced the |
| 21323 | // tile and tripped the lossless WHT assert `(4, 8) != (4, 4)` |
| 21324 | // at `TxParams::run`. |
| 21325 | for ur in 0..bh / 4usize { |
| 21326 | for uc in 0..bw / 4usize { |
| 21327 | luma_leaves.push((ur, uc, 4, 4)); |
| 21328 | } |
| 21329 | } |
| 21330 | publish = Some((4, 4)); |
| 21331 | } else { |
| 21332 | luma_leaves.push((0, 0, bw, bh)); |
| 21333 | // The non-var-tx arm: a SKIPPED intrabc leaf (or a frame |
| 21334 | // outside `TxMode::Select`) publishes its own rect transform |
| 21335 | // (`set_txfm_ctxs`'s `skip && is_inter` branch writes the |
| 21336 | // block's own pixels -- the same size here). |
| 21337 | } |
| 21338 | // The transform type an intrabc block's CHROMA inherits is the |
| 21339 | // `tx_type_map` entry at the chroma unit's co-located luma |
| 21340 | // position (`av1_get_tx_type`, `blockd.h:1278`), i.e. the FIRST |
| 21341 | // luma unit the tree resolved. |
| 21342 | let single = luma_leaves.len() == 1; |
| 21343 | let mut chroma_tx = TxType::DctDct; |
| 21344 | let mut grid = Grid::Zero(bw * bh); |
| 21345 | for (li, &(tu_row, tu_col, tw, th)) in luma_leaves.iter().enumerate() { |
| 21346 | let tu_mi = (lmi.0 + tu_row, lmi.1 + tu_col); |
| 21347 | let (tu_px, tu_py) = (tu_mi.1 * MI, tu_mi.0 * MI); |
| 21348 | let tu_reach = if single { |
| 21349 | reach |
| 21350 | } else { |
| 21351 | tu_reach( |
| 21352 | bw, bh, tu_px - px, tu_py - py, tw, reach, px, py, y.width, y.height, fctx, |
| 21353 | ) |
| 21354 | }; |
| 21355 | let mut yb = vec![0u16; tw * th]; |
| 21356 | mc::predict_with_filter( |
| 21357 | &y.data, y.width, y.true_width, y.true_height, |
| 21358 | mv_to_q4(tu_px, dv.1, 0), mv_to_q4(tu_py, dv.0, 0), |
| 21359 | tw, th, mc::InterpFilterKind::Bilinear, &mut yb, fctx, |
| 21360 | ); |
| 21361 | set_palette_pred(yb, fctx); |
| 21362 | let tu_grid = if skip { |
| 21363 | let zeros = Grid::Zero(tw * th); |
| 21364 | if (tw, th) == (4, 4) { |
| 21365 | push_intra(0, tu_px, tu_py, 4, DC_PRED, 0, tu_reach, &zeros, None, None, smooth_neighbor, fctx); |
| 21366 | } else { |
| 21367 | push_intra_rect(0, tu_px, tu_py, tw, th, DC_PRED, 0, tu_reach, &zeros, None, None, smooth_neighbor, fctx); |
| 21368 | } |
| 21369 | zeros |
| 21370 | } else { |
| 21371 | // A unit smaller than its own block (the var-tx split) |
| 21372 | // reads its `txb_skip_ctx` off the neighbours; the |
| 21373 | // whole-leaf unit's `plane_bsize == txsize_to_bsize` fixes |
| 21374 | // it at 0. |
| 21375 | let (set, tu_scan, around, skip_ctx) = if (tw, th) == (4, 4) { |
| 21376 | ( |
| 21377 | inter_txbset_for(4, reduced_tx_set), |
| 21378 | scan4, |
| 21379 | neighbours.around_mi(tu_mi, 4)[0], |
| 21380 | neighbours.luma_skip_ctx(tu_mi, 1), |
| 21381 | ) |
| 21382 | } else { |
| 21383 | ( |
| 21384 | if reduced_tx_set { |
| 21385 | TxbSet::LumaRect8x4Inter |
| 21386 | } else { |
| 21387 | TxbSet::LumaRect8x4InterSet1 |
| 21388 | }, |
| 21389 | scan, |
| 21390 | neighbours.around_mi_rect(tu_mi, tw, th)[0], |
| 21391 | 0, |
| 21392 | ) |
| 21393 | }; |
| 21394 | let mut coding = cdfs.txb(set, DC_PRED); |
| 21395 | // lane-av1-ibc444rect: `av1_read_tx_type` returns before any |
| 21396 | // symbol for a lossless block (qindex == 0, decodemv.c:681) |
| 21397 | // on EVERY plane and size -- the lossless units this arm now |
| 21398 | // walks must not read the set's tx_type either (`read_plane` |
| 21399 | // does the same for its own call sites). Measured on the |
| 21400 | // lane's `ll444_ibc_rect.obu`: with the symbol read, the |
| 21401 | // first unit's ladder diverged right after a matched |
| 21402 | // `all_zero` (ours +4096 rng on the unused tx_type). |
| 21403 | if lossless(fctx) { |
| 21404 | coding.tx_type = None; |
| 21405 | } |
| 21406 | let (levels, tx_type) = read_coeffs_rect( |
| 21407 | dec, &mut coding, tu_scan, tw, th, skip_ctx, dc_sign_ctx(around.2), |
| 21408 | TxType::DctDct, |
| 21409 | )?; |
| 21410 | if li == 0 { |
| 21411 | chroma_tx = tx_type; |
| 21412 | fctx.luma_tx_type.with(|c| c.set(tx_type)); |
| 21413 | } |
| 21414 | // lane-av1-ibc444rect: the residual goes through [`TxParams`] |
| 21415 | // with the frame's lossless flag, exactly like |
| 21416 | // [`read_plane`]'s own dequant -- at lossless `TxParams::run` |
| 21417 | // is the Walsh-Hadamard inverse on every plane, and the |
| 21418 | // `dequant_and_inverse_typed_wh` shortcut the lossy var-tx |
| 21419 | // callers use has no lossless flag (it would run the DCT |
| 21420 | // inverse over the WHT-ordered levels). Measured on the lane |
| 21421 | // fixture: the DCT route left ±1..30 sample errors on every |
| 21422 | // walked leaf's luma. |
| 21423 | let tx = TxParams { |
| 21424 | w: tw, |
| 21425 | h: th, |
| 21426 | bit_depth: crate::decode::bit_depth(fctx), |
| 21427 | q_idx: block_q_idx(fctx), |
| 21428 | dc_delta: plane_q_delta(0, fctx).0, |
| 21429 | ac_delta: plane_q_delta(0, fctx).1, |
| 21430 | tx_type, |
| 21431 | qm: block_iqmatrix(fctx, 0, tw, th, tx_type), |
| 21432 | stride: 0, |
| 21433 | lossless: lossless(fctx), |
| 21434 | }; |
| 21435 | let residual = tx.run(&levels); |
| 21436 | if (tw, th) == (4, 4) { |
| 21437 | push_intra(0, tu_px, tu_py, 4, DC_PRED, 0, tu_reach, &residual, None, None, smooth_neighbor, fctx); |
| 21438 | } else { |
| 21439 | push_intra_rect(0, tu_px, tu_py, tw, th, DC_PRED, 0, tu_reach, &residual, None, None, smooth_neighbor, fctx); |
| 21440 | } |
| 21441 | levels |
| 21442 | }; |
| 21443 | if single { |
| 21444 | grid = tu_grid; |
| 21445 | } else { |
| 21446 | // Per-unit neighbour state, the same shape the intra rect |
| 21447 | // leaf's own split path runs (`decode_leaf8`'s depth-1 |
| 21448 | // loop): a transform unit inside a block is not a block at |
| 21449 | // that position. |
| 21450 | neighbours.record_mi_luma(tu_mi, 4, &tu_grid); |
| 21451 | } |
| 21452 | } |
| 21453 | // The deferred non-var-tx band publish (see `publish` above): the |
| 21454 | // units' `txb_skip_ctx` reads ran against the pre-block bands. |
| 21455 | if let Some((pub_w, pub_h)) = publish { |
| 21456 | txfm_partition_update_rect(neighbours, lmi, (pub_w, pub_h), (bw, bh)); |
| 21457 | } |
| 21458 | if single { |
| 21459 | let state = neighbour_state(&grid); |
| 21460 | for cell in 0..h_mi { |
| 21461 | if lmi.0 + cell < neighbours.mi_rows { |
| 21462 | neighbours.left[lmi.0 + cell][0] = state; |
| 21463 | } |
| 21464 | } |
| 21465 | for cell in 0..w_mi { |
| 21466 | if lmi.1 + cell < neighbours.mi_cols { |
| 21467 | neighbours.above[lmi.1 + cell][0] = state; |
| 21468 | } |
| 21469 | } |
| 21470 | } |
| 21471 | neighbours.fill_skip_grid_rect(lmi, w_mi, h_mi, skip); |
| 21472 | neighbours.fill_lf_grid_rect( |
| 21473 | lmi, |
| 21474 | w_mi, |
| 21475 | h_mi, |
| 21476 | if single { bw as u8 } else { 4 }, |
| 21477 | if single { bh as u8 } else { 4 }, |
| 21478 | 0, |
| 21479 | fctx, |
| 21480 | ); |
| 21481 | // `record_intrabc_mi` publishes a square `n4`x`n4` footprint; a |
| 21482 | // 4x8/8x4 leaf needs its true `w_mi`x`h_mi` (lane-av1txr). |
| 21483 | record_intrabc_mi_rect(lmi.0, lmi.1, w_mi, h_mi, Some(dv), neighbours, fctx); |
| 21484 | if has_chroma { |
| 21485 | // See [`decode_leaf_split4`]: an intrabc block's chroma |
| 21486 | // inherits the luma transform type. |
| 21487 | fctx.intrabc_chroma_tx.with(|c| c.set(Some(chroma_tx))); |
| 21488 | if chroma_444 { |
| 21489 | // lane-av1-444: this leaf codes its OWN rect chroma unit |
| 21490 | // right here (frame copy at the DV); no group tail. |
| 21491 | sub8_leaf_chroma444( |
| 21492 | dec, cdfs, neighbours, lmi, bw, bh, DC_PRED, None, skip, |
| 21493 | Some(dv), y, u, v, scan4, base_q_idx, |
| 21494 | neighbours.smooth_uv_neighbour(lmi.0, lmi.1, r, c, fctx), fctx, |
| 21495 | )?; |
| 21496 | last_uv444 = DC_PRED; |
| 21497 | } else if chroma_422 { |
| 21498 | // lane-av1-422h: same at ss (1,0) -- the strip's own square |
| 21499 | // TX_4X4 chroma unit at its halved position. |
| 21500 | sub8_leaf_chroma422( |
| 21501 | dec, cdfs, neighbours, lmi, DC_PRED, 0, None, skip, |
| 21502 | Some(dv), y, u, v, scan4, base_q_idx, |
| 21503 | neighbours.smooth_uv_neighbour(lmi.0, lmi.1, r, c, fctx), |
| 21504 | (bw, bh), fctx, |
| 21505 | )?; |
| 21506 | last_uv422 = DC_PRED; |
| 21507 | } else { |
| 21508 | last_uv = Some((DC_PRED, 0, None)); |
| 21509 | last_intrabc = Some(dv); |
| 21510 | } |
| 21511 | } |
| 21512 | continue; |
| 21513 | } |
| 21514 | if filter_intra.is_some() { |
| 21515 | // lane-fi8 r1: `predict_filter_intra` walks its 4x2 patches over |
| 21516 | // a true `bw`x`bh` block, so a 4x8/8x4 leaf predicts through the |
| 21517 | // same `reconstruct_rect`/`reconstruct` calls below -- and under |
| 21518 | // a split transform, per 4x4 unit with that unit's own edge, |
| 21519 | // which is exactly what the depth-1 loop already does. |
| 21520 | hit!(FILTER_INTRA_RECT_HITS); |
| 21521 | hit!(FILTER_INTRA_RECT_SUB8_HITS); |
| 21522 | } |
| 21523 | // `TxMode::Select`'s `tx_depth` (spec 5.11.16) exists at a 4x8/8x4 |
| 21524 | // leaf too, and is read for every intra block, skipped or not. Its |
| 21525 | // category is libaom's `bsize_to_tx_size_cat(BLOCK_4X8) == 0` -- |
| 21526 | // `sub_tx_size_map[TX_4X8] == TX_4X4` is a single step, so it is the |
| 21527 | // same 2-symbol `tx_size_cat0` an 8x8 uses, not the 3-symbol |
| 21528 | // `tx_size_cat2` the 32x16 strips read -- with the rect context |
| 21529 | // (`get_tx_size_context`'s `max_tx_wide`/`max_tx_high` = bw/bh). |
| 21530 | // Missing this read consumed one symbol fewer than the encoder wrote |
| 21531 | // and desynced the tile at the very first rect leaf (class |
| 21532 | // `symbol-consumption-gap`). |
| 21533 | // lane-av1mix: libaom `read_tx_size`'s FIRST line |
| 21534 | // (`decodeframe.c:1203`, `if (xd->lossless[...]) return TX_4X4`) |
| 21535 | // fires before the `TX_MODE_SELECT` test, so a lossless-segment leaf |
| 21536 | // codes NO depth symbol even in `TxMode::Select` -- the mixed-lossless |
| 21537 | // frame's sub8 leaves forked exactly here (ours read the symbol at |
| 21538 | // (12477,50864) bit 3011, the oracle's all_zero used it). |
| 21539 | let depth = if tx_select && !lossless(fctx) { |
| 21540 | // lane-av1txbands: the `TXFM_CONTEXT` band read every other rect |
| 21541 | // reader already does. |
| 21542 | let ctx = tx_size_context_txfm_rect(neighbours, lmi, bw, bh, fctx); |
| 21543 | dec.symbol(&mut cdfs.tx_size_cat0[ctx]) |
| 21544 | } else { |
| 21545 | 0 |
| 21546 | }; |
| 21547 | if depth != 0 { |
| 21548 | hit!(TX_DEPTH_HITS); |
| 21549 | hit!(RECT8_SPLIT_TX_HITS); |
| 21550 | } |
| 21551 | if smooth_neighbor { |
| 21552 | hit!(SMOOTH_LUMA_HITS); |
| 21553 | } |
| 21554 | let (px, py) = (lmi.1 * MI, lmi.0 * MI); |
| 21555 | let reach = Reach::of_rect(bw, bh, px, py, y.width, y.height, fctx); |
| 21556 | // `sub_tx_size_map[TX_4X8] == TX_4X4`: depth 1 splits the leaf into |
| 21557 | // two 4x4 transform units along its long axis, each predicted from |
| 21558 | // the previous unit's reconstruction and reading its own `txb_skip` |
| 21559 | // context -- `decode_leaf8`'s own depth-1 loop, two units instead of |
| 21560 | // four. The per-TU path writes its own neighbour state, so the |
| 21561 | // block-level write below is skipped for it. |
| 21562 | let split = depth != 0 || lossless(fctx); |
| 21563 | // lane-av1txr: libaom `parse_decode_block`'s `set_txfm_ctxs` for this |
| 21564 | // leaf -- the size the `tx_depth` symbol resolved (depth 1 -> TX_4X4, |
| 21565 | // else the leaf's own rect transform), published over the leaf's own |
| 21566 | // mi span. An intrabc block's `txfm_partition_context` reads these |
| 21567 | // bands, and the key-frame sub-8x8 leaves had no write at all: |
| 21568 | // `warped.obu` mi (37,62) read a stale 8 where aomdec read 4 (ctx 18 |
| 21569 | // vs 19) and the tile desynced (class `context-band-not-published`). |
| 21570 | let (ctx_tx_w, ctx_tx_h) = if split { (4, 4) } else { (bw, bh) }; |
| 21571 | txfm_partition_update_rect(neighbours, lmi, (ctx_tx_w, ctx_tx_h), (bw, bh)); |
| 21572 | if split { |
| 21573 | let mut done = 0usize; |
| 21574 | for tu in 0..2usize { |
| 21575 | let tu_mi = if vert { |
| 21576 | (lmi.0 + tu, lmi.1) |
| 21577 | } else { |
| 21578 | (lmi.0, lmi.1 + tu) |
| 21579 | }; |
| 21580 | let (tu_px, tu_py) = (tu_mi.1 * MI, tu_mi.0 * MI); |
| 21581 | let tu_around = neighbours.around_mi(tu_mi, 4)[0]; |
| 21582 | // Same per-TU rule as every other split-transform site |
| 21583 | // (lane-palette2 r12): a transform unit inside a block is not |
| 21584 | // a block at that position, so its reach comes from the |
| 21585 | // block's own row, offset by the unit's position. |
| 21586 | let tu_reach = tu_reach( |
| 21587 | bw, |
| 21588 | bh, |
| 21589 | tu_px - px, |
| 21590 | tu_py - py, |
| 21591 | 4, |
| 21592 | reach, |
| 21593 | px, |
| 21594 | py, |
| 21595 | y.width, |
| 21596 | y.height, fctx, |
| 21597 | ); |
| 21598 | let tu_skip_ctx = neighbours.luma_skip_ctx(tu_mi, 1); |
| 21599 | if skip { |
| 21600 | // A skipped leaf still predicts per transform unit: the |
| 21601 | // second 4x4 takes its above/left edge from the first |
| 21602 | // one's reconstruction, which is NOT what a single 4x8 |
| 21603 | // prediction of the same mode produces. |
| 21604 | let zeros = vec![0i32; 16]; |
| 21605 | push_intra(0, |
| 21606 | tu_px, tu_py, 4, mode, angle_delta_y, tu_reach, &zeros, None, |
| 21607 | filter_intra, smooth_neighbor, fctx, |
| 21608 | ); |
| 21609 | neighbours.record_mi_luma(tu_mi, 4, &zeros); |
| 21610 | continue; |
| 21611 | } |
| 21612 | let tu_grid = read_plane( |
| 21613 | dec, |
| 21614 | cdfs, |
| 21615 | if reduced_tx_set { |
| 21616 | TxbSet::Luma4 |
| 21617 | } else { |
| 21618 | TxbSet::Luma4Set1 |
| 21619 | }, |
| 21620 | scan4, |
| 21621 | 0, |
| 21622 | tu_around, |
| 21623 | mode, |
| 21624 | mode, |
| 21625 | angle_delta_y, |
| 21626 | tu_reach, |
| 21627 | y, |
| 21628 | tu_px, |
| 21629 | tu_py, |
| 21630 | 4, |
| 21631 | TX4, |
| 21632 | base_q_idx, |
| 21633 | None, |
| 21634 | filter_intra, |
| 21635 | Some(tu_skip_ctx), |
| 21636 | smooth_neighbor, fctx, |
| 21637 | )?; |
| 21638 | if tu_grid.iter().any(|&l| l != 0) { |
| 21639 | done += 1; |
| 21640 | } |
| 21641 | neighbours.record_mi_luma(tu_mi, 4, &tu_grid); |
| 21642 | } |
| 21643 | if done > 0 { |
| 21644 | if vert { |
| 21645 | hit!(TX4X8_CODED_HITS); |
| 21646 | } else { |
| 21647 | hit!(TX8X4_CODED_HITS); |
| 21648 | } |
| 21649 | } |
| 21650 | neighbours.fill_skip_grid_rect(lmi, w_mi, h_mi, skip); |
| 21651 | neighbours.fill_lf_grid_rect(lmi, w_mi, h_mi, 4, 4, 0, fctx); |
| 21652 | if has_chroma { |
| 21653 | if chroma_444 { |
| 21654 | // lane-av1-444: the leaf's own rect chroma unit after its |
| 21655 | // split luma (no shared group unit at ss 0/0). |
| 21656 | let (uv_mode, _, alpha) = |
| 21657 | chroma.expect("4:4:4 reads uv_mode on every leaf"); |
| 21658 | sub8_leaf_chroma444( |
| 21659 | dec, cdfs, neighbours, lmi, bw, bh, uv_mode, alpha, skip, |
| 21660 | None, y, u, v, scan4, base_q_idx, |
| 21661 | neighbours.smooth_uv_neighbour(lmi.0, lmi.1, r, c, fctx), fctx, |
| 21662 | )?; |
| 21663 | last_uv444 = if uv_mode == UV_CFL_PRED { DC_PRED } else { uv_mode }; |
| 21664 | } else if chroma_422 { |
| 21665 | // lane-av1-422h: the strip's own square TX_4X4 chroma unit |
| 21666 | // per plane right after its split luma (one per ref strip). |
| 21667 | let (uv_mode, angle_delta_uv, alpha) = |
| 21668 | chroma.expect("4:2:2 chroma references read uv_mode"); |
| 21669 | sub8_leaf_chroma422( |
| 21670 | dec, cdfs, neighbours, lmi, uv_mode, angle_delta_uv, alpha, |
| 21671 | skip, None, y, u, v, scan4, base_q_idx, |
| 21672 | neighbours.smooth_uv_neighbour(lmi.0, lmi.1, r, c, fctx), |
| 21673 | (bw, bh), fctx, |
| 21674 | )?; |
| 21675 | last_uv422 = if uv_mode == UV_CFL_PRED { DC_PRED } else { uv_mode }; |
| 21676 | } else { |
| 21677 | last_uv = chroma; |
| 21678 | } |
| 21679 | } |
| 21680 | continue; |
| 21681 | } |
| 21682 | let grid = if skip { |
| 21683 | let zeros = Grid::Zero(bw * bh); |
| 21684 | push_intra_rect(0, |
| 21685 | px, py, bw, bh, mode, angle_delta_y, reach, &zeros, None, filter_intra, |
| 21686 | smooth_neighbor, fctx, |
| 21687 | ); |
| 21688 | zeros |
| 21689 | } else { |
| 21690 | // `plane_bsize == txsize_to_bsize[tx_size]` here too (the leaf's |
| 21691 | // luma TU *is* the leaf), so `get_txb_ctx` fixes `txb_skip_ctx` |
| 21692 | // at 0 -- see `decode_leaf_split4`'s own note. |
| 21693 | let around = neighbours.around_mi_rect(lmi, bw, bh)[0]; |
| 21694 | let set = if reduced_tx_set { |
| 21695 | TxbSet::LumaRect4x8 |
| 21696 | } else { |
| 21697 | TxbSet::LumaRect4x8Set1 |
| 21698 | }; |
| 21699 | let mut coding = cdfs.txb(set, fi_tx_row(mode, filter_intra)); |
| 21700 | let (levels, tx_type) = read_coeffs_rect( |
| 21701 | dec, |
| 21702 | &mut coding, |
| 21703 | scan, |
| 21704 | bw, |
| 21705 | bh, |
| 21706 | 0, |
| 21707 | dc_sign_ctx(around.2), |
| 21708 | TxType::DctDct, |
| 21709 | )?; |
| 21710 | if levels.iter().any(|&l| l != 0) { |
| 21711 | if vert { |
| 21712 | hit!(TX4X8_CODED_HITS); |
| 21713 | } else { |
| 21714 | hit!(TX8X4_CODED_HITS); |
| 21715 | } |
| 21716 | } |
| 21717 | let residual = dequant_and_inverse_typed_wh( |
| 21718 | &levels, |
| 21719 | bw, |
| 21720 | bh, |
| 21721 | crate::decode::bit_depth(fctx), |
| 21722 | // lane-d792 sweep: the block's segment `SEG_LVL_ALT_Q`, as at |
| 21723 | // every other dequant site (class `parsed-then-discarded`). |
| 21724 | block_q_idx(fctx), |
| 21725 | plane_q_delta(0, fctx).0, |
| 21726 | plane_q_delta(0, fctx).1, |
| 21727 | tx_type, |
| 21728 | block_iqmatrix(fctx, 0, bw, bh, tx_type), |
| 21729 | ); |
| 21730 | push_intra_rect(0, |
| 21731 | px, py, bw, bh, mode, angle_delta_y, reach, &residual, None, filter_intra, |
| 21732 | smooth_neighbor, fctx, |
| 21733 | ); |
| 21734 | levels |
| 21735 | }; |
| 21736 | // Plane 0's per-TU coefficient context over this leaf's own mi span |
| 21737 | // (`record_mi_luma`'s rect twin -- the chroma planes are written once |
| 21738 | // for the whole 8x8 group below). |
| 21739 | let state = neighbour_state(&grid); |
| 21740 | for cell in 0..h_mi { |
| 21741 | if lmi.0 + cell < neighbours.mi_rows { |
| 21742 | neighbours.left[lmi.0 + cell][0] = state; |
| 21743 | } |
| 21744 | } |
| 21745 | for cell in 0..w_mi { |
| 21746 | if lmi.1 + cell < neighbours.mi_cols { |
| 21747 | neighbours.above[lmi.1 + cell][0] = state; |
| 21748 | } |
| 21749 | } |
| 21750 | neighbours.fill_skip_grid_rect(lmi, w_mi, h_mi, skip); |
| 21751 | let (tx_w, tx_h) = if split { (4, 4) } else { (bw as u8, bh as u8) }; |
| 21752 | neighbours.fill_lf_grid_rect(lmi, w_mi, h_mi, tx_w, tx_h, 0, fctx); |
| 21753 | if has_chroma { |
| 21754 | if chroma_444 { |
| 21755 | // lane-av1-444: the leaf's own rect chroma unit after its |
| 21756 | // luma (no shared group unit at ss 0/0). |
| 21757 | let (uv_mode, _, alpha) = |
| 21758 | chroma.expect("4:4:4 reads uv_mode on every leaf"); |
| 21759 | sub8_leaf_chroma444( |
| 21760 | dec, cdfs, neighbours, lmi, bw, bh, uv_mode, alpha, skip, |
| 21761 | None, y, u, v, scan4, base_q_idx, |
| 21762 | neighbours.smooth_uv_neighbour(lmi.0, lmi.1, r, c, fctx), fctx, |
| 21763 | )?; |
| 21764 | last_uv444 = if uv_mode == UV_CFL_PRED { DC_PRED } else { uv_mode }; |
| 21765 | } else if chroma_422 { |
| 21766 | // lane-av1-422h: the strip's own square TX_4X4 chroma unit |
| 21767 | // per plane right after its luma -- libaom codes each |
| 21768 | // BLOCK_8X4 strip's chroma under the strip itself (both |
| 21769 | // strips of a HORZ pair are chroma references), not one |
| 21770 | // shared group unit. |
| 21771 | let (uv_mode, angle_delta_uv, alpha) = |
| 21772 | chroma.expect("4:2:2 chroma references read uv_mode"); |
| 21773 | sub8_leaf_chroma422( |
| 21774 | dec, cdfs, neighbours, lmi, uv_mode, angle_delta_uv, alpha, |
| 21775 | skip, None, y, u, v, scan4, base_q_idx, |
| 21776 | neighbours.smooth_uv_neighbour(lmi.0, lmi.1, r, c, fctx), |
| 21777 | (bw, bh), fctx, |
| 21778 | )?; |
| 21779 | last_uv422 = if uv_mode == UV_CFL_PRED { DC_PRED } else { uv_mode }; |
| 21780 | } else { |
| 21781 | last_uv = chroma; |
| 21782 | } |
| 21783 | } |
| 21784 | } |
| 21785 | // lane-av1mono: a monochrome frame codes no chroma at all -- no |
| 21786 | // `uv_mode` was read ([`read_intra_mode_sub8`] folds the plane count in), |
| 21787 | // so there is no chroma to reconstruct and no chroma coefficient to read. |
| 21788 | let (u_grid, v_grid): (Grid, Grid) = if mono(fctx) { |
| 21789 | (Grid::Zero(16), Grid::Zero(16)) |
| 21790 | } else if chroma_444 { |
| 21791 | // lane-av1-444: every leaf coded and reconstructed its own chroma in |
| 21792 | // the loop above; only the coarse slots remain (last leaf's mode). |
| 21793 | neighbours.above_uv_mode[c] = last_uv444; |
| 21794 | neighbours.left_uv_mode[r] = last_uv444; |
| 21795 | (Grid::Zero(16), Grid::Zero(16)) |
| 21796 | } else if chroma_422 { |
| 21797 | // lane-av1-422h: every chroma-reference strip coded and reconstructed |
| 21798 | // its own chroma unit in the loop above (and stamped its own entropy |
| 21799 | // span), so the 4:2:0 shared group unit below must not run. Only the |
| 21800 | // coarse 16x16 slots remain: libaom's `above_uv_mode`/`left_uv_mode` |
| 21801 | // end up holding the LAST chroma reference's mode (decode order). |
| 21802 | neighbours.above_uv_mode[c] = last_uv422; |
| 21803 | neighbours.left_uv_mode[r] = last_uv422; |
| 21804 | (Grid::Zero(16), Grid::Zero(16)) |
| 21805 | } else { |
| 21806 | let (gpx, gpy) = (leaf_mi.1 * MI, leaf_mi.0 * MI); |
| 21807 | let (cpx, cpy) = (gpx >> ss_x(fctx), gpy >> ss_y(fctx)); |
| 21808 | let group_reach = Reach::of(8, gpx, gpy, y.width, y.height, fctx); |
| 21809 | let (uv_mode, angle_delta_uv, alpha) = |
| 21810 | last_uv.expect("i==1 always sets has_chroma, so chroma is always Some"); |
| 21811 | let uv_predict_mode = if uv_mode == UV_CFL_PRED { DC_PRED } else { uv_mode }; |
| 21812 | neighbours.above_uv_mode[c] = uv_predict_mode; |
| 21813 | neighbours.left_uv_mode[r] = uv_predict_mode; |
| 21814 | neighbours.record_uv_mode_mi(leaf_mi.0, leaf_mi.1, 2, 2, uv_predict_mode); |
| 21815 | let (u_grid, v_grid): (Grid, Grid) = if leaf_skips[1] && last_intrabc.is_none() { |
| 21816 | // A skipped chroma block still gets its CfL contribution: libaom's |
| 21817 | // `predict_and_reconstruct_intra_block` calls `cfl_predict_block` |
| 21818 | // from `av1_predict_intra_block_facade` (av1/common/reconintra.c:1719) |
| 21819 | // whenever `mbmi->uv_mode == UV_CFL_PRED`, before any residual is |
| 21820 | // added -- `skip_txfm` only drops the residual, never the alpha. |
| 21821 | // lane-reelckf and lane-kf1200 fixed THIS SAME arm independently; the |
| 21822 | // merge keeps one fix and feeds both lanes' counters so neither of |
| 21823 | // their gates goes vacuous. |
| 21824 | let ac = alpha.map(|_| cfl_src(gpx, gpy, 8)); |
| 21825 | if alpha.is_some() { |
| 21826 | hit!(CHROMA_SKIP_CFL_HITS); |
| 21827 | hit!(SKIP_CFL_HITS); |
| 21828 | } |
| 21829 | 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); |
| 21830 | 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); |
| 21831 | // lane-av1txbands: a SKIPPED chroma-reference leaf reads no chroma |
| 21832 | // coefficient, so it never consults the inherited luma transform type |
| 21833 | // -- but an intrabc chroma-reference leaf did arm |
| 21834 | // [`INTRABC_CHROMA_TX`] above (`has_chroma`), and the two coefficient |
| 21835 | // branches below are the only ones that disarm it. Left armed, the slot |
| 21836 | // picks the NEXT block's chroma scan and inverse transform (class |
| 21837 | // arm-without-clear-on-a-skip-route). Counted so a gate can tell a |
| 21838 | // stream that reaches this route from one that never does. |
| 21839 | if fctx.intrabc_chroma_tx.with(std::cell::Cell::get).is_some() { |
| 21840 | hit!(SKIPPED_INTRABC_CHROMA_ARM_HITS); |
| 21841 | fctx.intrabc_chroma_tx.with(|c| c.set(None)); |
| 21842 | } |
| 21843 | (Grid::Zero(16), Grid::Zero(16)) |
| 21844 | } else if let Some(dv) = last_intrabc { |
| 21845 | // A 4:2:0 `BLOCK_4X8`/`BLOCK_8X4` leaf maps to a clamped chroma |
| 21846 | // `BLOCK_4X4` (`get_plane_block_size`, blockd.h:1184; `set_plane_n4`, |
| 21847 | // av1_common_int.h:1344). The chroma reference is still the whole |
| 21848 | // 8x8 group's 4x4 plane block at `(cpx, cpy)`, so prediction uses the |
| 21849 | // group's destination origin, not the odd-mi leaf's nominal 2x4/4x2 |
| 21850 | // extent. The old route treated a skipped intrabc leaf as skipped |
| 21851 | // intra and predicted this block with DC, first differing at |
| 21852 | // mi(46,52), U(104,92), while consuming identical symbols. |
| 21853 | // `decode_reconstruct_tx` reads no chroma coefficients for a skipped |
| 21854 | // intrabc block, but an unskipped one reads the ordinary Chroma4 |
| 21855 | // tables; chroma inherits no coded tx_type of its own. |
| 21856 | let mut ub = vec![0u16; 16]; |
| 21857 | mc::predict_with_filter( |
| 21858 | &u.data, u.width, u.true_width, u.true_height, |
| 21859 | mv_to_q4(cpx, dv.1, ss_x(fctx)), mv_to_q4(cpy, dv.0, ss_y(fctx)), |
| 21860 | 4, 4, mc::InterpFilterKind::Bilinear, &mut ub, fctx, |
| 21861 | ); |
| 21862 | let mut vb = vec![0u16; 16]; |
| 21863 | mc::predict_with_filter( |
| 21864 | &v.data, v.width, v.true_width, v.true_height, |
| 21865 | mv_to_q4(cpx, dv.1, ss_x(fctx)), mv_to_q4(cpy, dv.0, ss_y(fctx)), |
| 21866 | 4, 4, mc::InterpFilterKind::Bilinear, &mut vb, fctx, |
| 21867 | ); |
| 21868 | if leaf_skips[1] { |
| 21869 | // `skip_txfm` still reconstructs an intrabc prediction, but |
| 21870 | // libaom's `decode_reconstruct_tx` reads NO chroma coefficients. |
| 21871 | // The ordinary skipped-intra arm above is therefore not valid for |
| 21872 | // an intrabc leaf: use the copied, clamped 4x4 prediction directly. |
| 21873 | push_mc_rect( |
| 21874 | 1, cpx, cpy, 4, 4, 4, Pred::Inline(&ub, 4), |
| 21875 | &ZERO_RESIDUAL[..16], fctx, |
| 21876 | ); |
| 21877 | push_mc_rect( |
| 21878 | 2, cpx, cpy, 4, 4, 4, Pred::Inline(&vb, 4), |
| 21879 | &ZERO_RESIDUAL[..16], fctx, |
| 21880 | ); |
| 21881 | hit!(SKIPPED_INTRABC_CHROMA_ARM_HITS); |
| 21882 | fctx.intrabc_chroma_tx.with(|c| c.set(None)); |
| 21883 | (Grid::Zero(16), Grid::Zero(16)) |
| 21884 | } else { |
| 21885 | let chroma_around = neighbours.around_mi(leaf_mi, 8); |
| 21886 | set_palette_pred(ub, fctx); |
| 21887 | let ug = read_plane( |
| 21888 | dec, cdfs, TxbSet::Chroma4, scan4, 1, chroma_around[1], DC_PRED, DC_PRED, |
| 21889 | 0, group_reach, u, cpx, cpy, 4, TX4, base_q_idx, |
| 21890 | None, None, None, smooth_neighbor_uv, fctx, |
| 21891 | )?; |
| 21892 | set_palette_pred(vb, fctx); |
| 21893 | let vg = read_plane( |
| 21894 | dec, cdfs, TxbSet::Chroma4, scan4, 2, chroma_around[2], DC_PRED, DC_PRED, |
| 21895 | 0, group_reach, v, cpx, cpy, 4, TX4, base_q_idx, |
| 21896 | None, None, None, smooth_neighbor_uv, fctx, |
| 21897 | )?; |
| 21898 | fctx.intrabc_chroma_tx.with(|c| c.set(None)); |
| 21899 | (ug, vg) |
| 21900 | } |
| 21901 | } else { |
| 21902 | let ac = alpha.map(|_| cfl_src(gpx, gpy, 8)); |
| 21903 | let chroma_around = neighbours.around_mi(leaf_mi, 8); |
| 21904 | let ug = read_plane( |
| 21905 | dec, cdfs, TxbSet::Chroma4, scan4, 1, chroma_around[1], uv_mode, uv_predict_mode, |
| 21906 | angle_delta_uv, group_reach, u, cpx, cpy, 4, TX4, base_q_idx, |
| 21907 | alpha.zip(ac).map(|((au, _), ac)| (au, ac)), None, None, smooth_neighbor_uv, fctx, |
| 21908 | )?; |
| 21909 | let vg = read_plane( |
| 21910 | dec, cdfs, TxbSet::Chroma4, scan4, 2, chroma_around[2], uv_mode, uv_predict_mode, |
| 21911 | angle_delta_uv, group_reach, v, cpx, cpy, 4, TX4, base_q_idx, |
| 21912 | alpha.zip(ac).map(|((_, av), ac)| (av, ac)), None, None, smooth_neighbor_uv, fctx, |
| 21913 | )?; |
| 21914 | (ug, vg) |
| 21915 | }; |
| 21916 | (u_grid, v_grid) |
| 21917 | }; |
| 21918 | // `update_partition_context` at the 8x8 level writes |
| 21919 | // `partition_context_lookup[subsize]` over the whole 8x8 span: BLOCK_4X8 |
| 21920 | // is `{31, 30}` (bit0 set on the above half only), BLOCK_8X4 `{30, 31}` |
| 21921 | // -- the mirror of `decode_leaf_split4`'s `{31, 31}`, which |
| 21922 | // `partition_ctx_mi` reads back off these side values. |
| 21923 | for cell in 0..2 { |
| 21924 | neighbours.above_side_mi[leaf_mi.1 + cell] = bw as u8; |
| 21925 | neighbours.left_side_mi[leaf_mi.0 + cell] = bh as u8; |
| 21926 | } |
| 21927 | // lane-av1-444: at 4:4:4 [`sub8_leaf_chroma444`] already stamped each |
| 21928 | // leaf's own span as it decoded; these placeholder grids carry no state. |
| 21929 | // lane-av1-422h: same at ss (1,0) -- [`sub8_leaf_chroma422`] stamped each |
| 21930 | // chroma-reference strip's own span in decode order. |
| 21931 | if !(chroma_444 || chroma_422) { |
| 21932 | let round_up_even = |n: usize| n.div_ceil(2) * 2; |
| 21933 | let bound_h = round_up_even(neighbours.mi_rows); |
| 21934 | let bound_w = round_up_even(neighbours.mi_cols); |
| 21935 | let u_state = neighbour_state(&u_grid); |
| 21936 | let v_state = neighbour_state(&v_grid); |
| 21937 | for cell in 0..2usize { |
| 21938 | if cell < 2usize.min(bound_h.saturating_sub(leaf_mi.0)) { |
| 21939 | neighbours.left[leaf_mi.0 + cell][1] = u_state; |
| 21940 | neighbours.left[leaf_mi.0 + cell][2] = v_state; |
| 21941 | } |
| 21942 | if cell < 2usize.min(bound_w.saturating_sub(leaf_mi.1)) { |
| 21943 | neighbours.above[leaf_mi.1 + cell][1] = u_state; |
| 21944 | neighbours.above[leaf_mi.1 + cell][2] = v_state; |
| 21945 | } |
| 21946 | } |
| 21947 | } |
| 21948 | // lane-dkey (same clear as `decode_leaf_split4`): a sub-8x8 leaf can never use palette (`av1_allow_palette` |
| 21949 | // needs BLOCK_8X8 and up), and libaom's mi grid says so -- its |
| 21950 | // `palette_size` is 0 in every one of these mi cells, so the NEXT block's |
| 21951 | // `av1_get_palette_mode_ctx` and colour cache read 0 here. This decoder |
| 21952 | // only ever WROTE palette state from the >=8x8 readers, so an 8x8 palette |
| 21953 | // block above a 4x4 group left its size/colours standing in the mi-granular |
| 21954 | // bands: the block below the group read `palette_y_mode` off CDF row 1 |
| 21955 | // where libaom reads row 0 (class [[cdf-row-held-constant]]), decoded a |
| 21956 | // palette libaom never coded, and then skipped the `use_filter_intra` |
| 21957 | // symbol that a palette block has none of -- one symbol short, tile |
| 21958 | // desynced. Publishing the group's own zeros is the clear. |
| 21959 | neighbours.record_palette_y_rect(leaf_mi, 8, 8, 0, [0u16; 8]); |
| 21960 | neighbours.record_palette_uv_rect(leaf_mi, 8, 8, 0, [0u16; 8]); |
| 21961 | // `(below_mode, right_mode)`: the sub-block a neighbour below sees is the |
| 21962 | // bottom one (the second under HORZ, the left one under VERT), the one a |
| 21963 | // neighbour to the right sees is the top-right (the second under VERT). |
| 21964 | Ok(if vert { |
| 21965 | (leaf_modes[0], leaf_modes[1]) |
| 21966 | } else { |
| 21967 | (leaf_modes[1], leaf_modes[0]) |
| 21968 | }) |
| 21969 | } |
| 21970 | |
| 21971 | /// Spec 7.15.3's `Cdef_Directions`: `(row, col)` offsets for the two primary |
| 21972 | /// taps of each of the eight directions; the secondary taps at `dir+2`/`dir-2` |
| 21973 | /// (mod 8) reuse the same table. |
| 21974 | const CDEF_DIRECTIONS: [[(i32, i32); 2]; 8] = [ |
| 21975 | [(-1, 1), (-2, 2)], |
| 21976 | [(0, 1), (-1, 2)], |
| 21977 | [(0, 1), (0, 2)], |
| 21978 | [(0, 1), (1, 2)], |
| 21979 | [(1, 1), (2, 2)], |
| 21980 | [(1, 0), (2, 1)], |
| 21981 | [(1, 0), (2, 0)], |
| 21982 | [(1, 0), (2, -1)], |
| 21983 | ]; |
| 21984 | /// A neighbour tap outside the frame's true extent -- `constrain` reads this |
| 21985 | /// as "so far away it never contributes" (spec/libaom `CDEF_VERY_LARGE`). |
| 21986 | const CDEF_VERY_LARGE: i32 = 0x4000; |
| 21987 | const CDEF_PRI_TAPS: [[i32; 2]; 2] = [[4, 2], [3, 3]]; |
| 21988 | const CDEF_SEC_TAPS: [i32; 2] = [2, 1]; |
| 21989 | /// lane-av1422filter: libaom `av1_cdef_filter_fb` (cdef_block.c) remaps the |
| 21990 | /// luma direction before filtering CHROMA whenever `xdec != ydec` — the |
| 21991 | /// direction grid lives on the subsampled chroma grid, so a direction found |
| 21992 | /// on the 8x8 luma block means a different line there. `conv422` applies |
| 21993 | /// under 4:2:2 (`xdec` set), `conv440` under its mirror (not expressible in |
| 21994 | /// the profiles this decoder accepts, kept for fidelity). 4:2:0 and 4:4:4 |
| 21995 | /// (`xdec == ydec`) take no remap, exactly as before this lane. |
| 21996 | const CDEF_DIR_CONV422: [usize; 8] = [7, 0, 2, 4, 5, 6, 6, 6]; |
| 21997 | const CDEF_DIR_CONV440: [usize; 8] = [1, 2, 2, 2, 3, 4, 6, 0]; |
| 21998 | |
| 21999 | fn cdef_msb(x: i32) -> i32 { |
| 22000 | 31 - x.leading_zeros() as i32 |
| 22001 | } |
| 22002 | |
| 22003 | /// Spec 7.15.3's `constrain`: how much of a neighbour's difference from the |
| 22004 | /// centre sample a filter tap is allowed to contribute. |
| 22005 | #[allow(dead_code)] // read only from the `#[cfg(test)]` gates |
| 22006 | fn cdef_constrain(diff: i32, threshold: i32, damping: i32) -> i32 { |
| 22007 | if threshold == 0 { |
| 22008 | return 0; |
| 22009 | } |
| 22010 | cdef_constrain_shifted(diff, threshold, cdef_constrain_shift(threshold, damping)) |
| 22011 | } |
| 22012 | |
| 22013 | /// `constrain`'s damping shift: a function of the block's threshold and |
| 22014 | /// damping alone, so the filter loop computes it once per block instead of |
| 22015 | /// the `leading_zeros` + branch it used to do twice per tap read. |
| 22016 | fn cdef_constrain_shift(threshold: i32, damping: i32) -> i32 { |
| 22017 | (damping - cdef_msb(threshold)).max(0) |
| 22018 | } |
| 22019 | |
| 22020 | /// [`cdef_constrain`]'s body with the shift already resolved; `threshold` is |
| 22021 | /// non-zero on every call (the caller only filters when its strength is). |
| 22022 | fn cdef_constrain_shifted(diff: i32, threshold: i32, shift: i32) -> i32 { |
| 22023 | let a = diff.abs(); |
| 22024 | diff.signum() * (threshold - (a >> shift)).clamp(0, a) |
| 22025 | } |
| 22026 | |
| 22027 | /// The widest `CDEF_DIRECTIONS` offset in either axis: every tap a block reads |
| 22028 | /// lies inside its own samples grown by this much. |
| 22029 | const CDEF_BORDER: usize = 2; |
| 22030 | |
| 22031 | /// Copies one block's whole CDEF footprint -- its `bh` x `bw` samples plus the |
| 22032 | /// [`CDEF_BORDER`] tap ring -- out of the *unfiltered* plane into `buf` (row |
| 22033 | /// stride `bw + 2 * CDEF_BORDER`), so the direction search and the tap loops |
| 22034 | /// index a small contiguous window instead of re-deriving the frame-bounds |
| 22035 | /// test on every one of a pixel's ~17 tap reads. Out-of-frame positions get |
| 22036 | /// `CDEF_VERY_LARGE`, exactly as the per-sample closure did. |
| 22037 | fn cdef_gather( |
| 22038 | src: &[u16], |
| 22039 | stride: usize, |
| 22040 | true_w: usize, |
| 22041 | true_h: usize, |
| 22042 | ox: usize, |
| 22043 | oy: usize, |
| 22044 | bw: usize, |
| 22045 | bh: usize, |
| 22046 | buf: &mut [i16], |
| 22047 | ) { |
| 22048 | let bstride = bw + 2 * CDEF_BORDER; |
| 22049 | let rows = bh + 2 * CDEF_BORDER; |
| 22050 | if oy >= CDEF_BORDER |
| 22051 | && ox >= CDEF_BORDER |
| 22052 | && oy + bh + CDEF_BORDER <= true_h |
| 22053 | && ox + bw + CDEF_BORDER <= true_w |
| 22054 | { |
| 22055 | // Interior block: every tap is inside the frame, so the whole window |
| 22056 | // is `rows` contiguous runs. |
| 22057 | for r in 0..rows { |
| 22058 | let s0 = (oy - CDEF_BORDER + r) * stride + (ox - CDEF_BORDER); |
| 22059 | let src_row = &src[s0..s0 + bstride]; |
| 22060 | let dst_row = &mut buf[r * bstride..r * bstride + bstride]; |
| 22061 | for (d, &v) in dst_row.iter_mut().zip(src_row) { |
| 22062 | *d = v as i16; |
| 22063 | } |
| 22064 | } |
| 22065 | return; |
| 22066 | } |
| 22067 | for r in 0..rows { |
| 22068 | let ny = oy as isize + r as isize - CDEF_BORDER as isize; |
| 22069 | for c in 0..bstride { |
| 22070 | let nx = ox as isize + c as isize - CDEF_BORDER as isize; |
| 22071 | buf[r * bstride + c] = |
| 22072 | if ny < 0 || nx < 0 || ny as usize >= true_h || nx as usize >= true_w { |
| 22073 | CDEF_VERY_LARGE as i16 |
| 22074 | } else { |
| 22075 | src[ny as usize * stride + nx as usize] as i16 |
| 22076 | }; |
| 22077 | } |
| 22078 | } |
| 22079 | } |
| 22080 | |
| 22081 | /// libaom `cdef_find_dir_c`'s eight partial-sum arrays, row at a time: each |
| 22082 | /// array takes the row, its reverse, its pairwise sums or its total added |
| 22083 | /// into consecutive slots, so the 512 scalar read-modify-writes on i64 the |
| 22084 | /// per-sample form did become a handful of lane-wide adds per row. i32 is |
| 22085 | /// exact here: every partial is a sum of at most eight values in -128..127, |
| 22086 | /// and slot 15 exists only so an eight-lane store at offset 7 fits. |
| 22087 | fn cdef_find_dir_partials(win: &[i16], bstride: usize, coeff_shift: i32) -> [[i32; 16]; 8] { |
| 22088 | fn add_at<const N: usize>(dst: &mut [i32; 16], off: usize, src: &[i32; N]) { |
| 22089 | for (d, &x) in dst[off..off + N].iter_mut().zip(src.iter()) { |
| 22090 | *d += x; |
| 22091 | } |
| 22092 | } |
| 22093 | let mut partial = [[0i32; 16]; 8]; |
| 22094 | for i in 0..8usize { |
| 22095 | let base = (i + CDEF_BORDER) * bstride + CDEF_BORDER; |
| 22096 | // libaom `cdef_find_dir_c`: normalise to an 8-bit sample before |
| 22097 | // accumulating, same as `constrain`'s threshold shift. |
| 22098 | let row: [i32; 8] = core::array::from_fn(|j| (i32::from(win[base + j]) >> coeff_shift) - 128); |
| 22099 | let rev: [i32; 8] = core::array::from_fn(|j| row[7 - j]); |
| 22100 | let pair: [i32; 4] = core::array::from_fn(|k| row[2 * k] + row[2 * k + 1]); |
| 22101 | let rpair: [i32; 4] = core::array::from_fn(|k| pair[3 - k]); |
| 22102 | let (p, rest) = partial.split_at_mut(1); |
| 22103 | add_at(&mut p[0], i, &row); // partial[0][i + j] |
| 22104 | add_at(&mut rest[0], i, &pair); // partial[1][i + j / 2] |
| 22105 | rest[1][i] = row.iter().sum(); // partial[2][i] |
| 22106 | add_at(&mut rest[2], i, &rpair); // partial[3][3 + i - j / 2] |
| 22107 | add_at(&mut rest[3], i, &rev); // partial[4][7 + i - j] |
| 22108 | add_at(&mut rest[4], 3 - i / 2, &row); // partial[5][3 - i / 2 + j] |
| 22109 | add_at(&mut rest[5], 0, &row); // partial[6][j] |
| 22110 | add_at(&mut rest[6], i / 2, &row); // partial[7][i / 2 + j] |
| 22111 | } |
| 22112 | partial |
| 22113 | } |
| 22114 | |
| 22115 | /// Spec 7.15.3's direction search (libaom `cdef_find_dir_c`): the dominant |
| 22116 | /// direction of an 8x8 luma window and the variance gap between it and its |
| 22117 | /// orthogonal, reading [`cdef_gather`]'s window (row stride `bstride`, the |
| 22118 | /// block's own samples starting at `CDEF_BORDER, CDEF_BORDER`). |
| 22119 | fn cdef_find_dir(win: &[i16], bstride: usize, coeff_shift: i32) -> (usize, i32) { |
| 22120 | const DIV_TABLE: [i64; 9] = [0, 840, 420, 280, 210, 168, 140, 120, 105]; |
| 22121 | #[cfg(target_arch = "x86_64")] |
| 22122 | let partial = if crate::mc::simd_level() == crate::mc::SimdLevel::Avx2 { |
| 22123 | // SAFETY: reached only when the CPU reports avx2; the kernel reads |
| 22124 | // the same eight rows of eight samples the scalar form does. |
| 22125 | #[allow(unsafe_code)] |
| 22126 | unsafe { |
| 22127 | cdef_simd::find_dir_partials_avx2(win, bstride, coeff_shift) |
| 22128 | } |
| 22129 | } else { |
| 22130 | cdef_find_dir_partials(win, bstride, coeff_shift) |
| 22131 | }; |
| 22132 | #[cfg(not(target_arch = "x86_64"))] |
| 22133 | let partial = cdef_find_dir_partials(win, bstride, coeff_shift); |
| 22134 | let sq = |a: i32| -> i64 { i64::from(a) * i64::from(a) }; |
| 22135 | let mut cost = [0i64; 8]; |
| 22136 | for i in 0..8usize { |
| 22137 | cost[2] += sq(partial[2][i]); |
| 22138 | cost[6] += sq(partial[6][i]); |
| 22139 | } |
| 22140 | cost[2] *= DIV_TABLE[8]; |
| 22141 | cost[6] *= DIV_TABLE[8]; |
| 22142 | for i in 0..7usize { |
| 22143 | cost[0] += (sq(partial[0][i]) + sq(partial[0][14 - i])) * DIV_TABLE[i + 1]; |
| 22144 | cost[4] += (sq(partial[4][i]) + sq(partial[4][14 - i])) * DIV_TABLE[i + 1]; |
| 22145 | } |
| 22146 | cost[0] += sq(partial[0][7]) * DIV_TABLE[8]; |
| 22147 | cost[4] += sq(partial[4][7]) * DIV_TABLE[8]; |
| 22148 | for i in (1..8).step_by(2) { |
| 22149 | for j in 0..5usize { |
| 22150 | cost[i] += sq(partial[i][3 + j]); |
| 22151 | } |
| 22152 | cost[i] *= DIV_TABLE[8]; |
| 22153 | for j in 0..3usize { |
| 22154 | cost[i] += (sq(partial[i][j]) + sq(partial[i][10 - j])) * DIV_TABLE[2 * j + 2]; |
| 22155 | } |
| 22156 | } |
| 22157 | let mut best_cost = 0i64; |
| 22158 | let mut best_dir = 0usize; |
| 22159 | for (i, &c) in cost.iter().enumerate() { |
| 22160 | if c > best_cost { |
| 22161 | best_cost = c; |
| 22162 | best_dir = i; |
| 22163 | } |
| 22164 | } |
| 22165 | let var = (best_cost - cost[(best_dir + 4) & 7]) >> 10; |
| 22166 | (best_dir, var as i32) |
| 22167 | } |
| 22168 | |
| 22169 | /// Spec 7.15.2's per-8x8 strength adjustment: a strongly directional 8x8 |
| 22170 | /// (high variance gap) gets more deringing than a flat or non-directional |
| 22171 | /// one, only ever applied to the luma primary strength. |
| 22172 | fn cdef_adjust_strength(strength: i32, var: i32) -> i32 { |
| 22173 | if var == 0 { |
| 22174 | return 0; |
| 22175 | } |
| 22176 | let i = if var >> 6 != 0 { |
| 22177 | cdef_msb(var >> 6).min(12) |
| 22178 | } else { |
| 22179 | 0 |
| 22180 | }; |
| 22181 | (strength * (4 + i) + 8) >> 4 |
| 22182 | } |
| 22183 | |
| 22184 | /// Spec 7.15.3's `cdef_filter_block`: filters one block (8x8 luma, 4x4 4:2:0 |
| 22185 | /// chroma) in place, `sample(row, col)` reading the *unfiltered* frame (a |
| 22186 | /// previously-filtered neighbour must never feed this block's own sum). |
| 22187 | #[allow(clippy::too_many_arguments)] |
| 22188 | #[allow(unsafe_code)] |
| 22189 | fn cdef_filter_block( |
| 22190 | src: &[i16], |
| 22191 | dst: &mut PlaneBuf<'static>, |
| 22192 | ox: usize, |
| 22193 | oy: usize, |
| 22194 | bw: usize, |
| 22195 | bh: usize, |
| 22196 | pri_strength: i32, |
| 22197 | sec_strength: i32, |
| 22198 | dir: usize, |
| 22199 | pri_damping: i32, |
| 22200 | sec_damping: i32, |
| 22201 | enable_primary: bool, |
| 22202 | enable_secondary: bool, |
| 22203 | coeff_shift: i32, fctx: &crate::decode::FrameCtx, |
| 22204 | ) { |
| 22205 | let (stride, smax) = (dst.width, sample_max(fctx)); |
| 22206 | #[cfg(target_arch = "x86_64")] |
| 22207 | if crate::mc::simd_level() == crate::mc::SimdLevel::Avx2 && (bw == 4 || bw == 8) && bh % 2 == 0 |
| 22208 | { |
| 22209 | // SAFETY: reached only when the CPU reports avx2; the kernel reads |
| 22210 | // `src`'s whole (bw + 4) x (bh + 4) window and writes exactly the |
| 22211 | // `bh` runs of `bw` samples the scalar form writes. |
| 22212 | unsafe { |
| 22213 | cdef_simd::filter_block_avx2( |
| 22214 | src, |
| 22215 | dst.data.to_mut(), |
| 22216 | stride, |
| 22217 | ox, |
| 22218 | oy, |
| 22219 | bw, |
| 22220 | bh, |
| 22221 | pri_strength, |
| 22222 | sec_strength, |
| 22223 | dir, |
| 22224 | pri_damping, |
| 22225 | sec_damping, |
| 22226 | enable_primary, |
| 22227 | enable_secondary, |
| 22228 | coeff_shift, |
| 22229 | smax, |
| 22230 | ) |
| 22231 | }; |
| 22232 | return; |
| 22233 | } |
| 22234 | cdef_filter_block_scalar( |
| 22235 | src, |
| 22236 | dst.data.to_mut(), |
| 22237 | stride, |
| 22238 | ox, |
| 22239 | oy, |
| 22240 | bw, |
| 22241 | bh, |
| 22242 | pri_strength, |
| 22243 | sec_strength, |
| 22244 | dir, |
| 22245 | pri_damping, |
| 22246 | sec_damping, |
| 22247 | enable_primary, |
| 22248 | enable_secondary, |
| 22249 | coeff_shift, |
| 22250 | smax, |
| 22251 | ); |
| 22252 | } |
| 22253 | |
| 22254 | /// [`cdef_filter_block`]'s scalar reference: spec 7.15.3 sample by sample, |
| 22255 | /// the form every non-AVX2 build decodes through and the one |
| 22256 | /// `cdef_simd_kernel_matches_the_scalar_filter` checks the kernel against. |
| 22257 | #[allow(clippy::too_many_arguments)] |
| 22258 | fn cdef_filter_block_scalar( |
| 22259 | src: &[i16], |
| 22260 | dst: &mut [u16], |
| 22261 | stride: usize, |
| 22262 | ox: usize, |
| 22263 | oy: usize, |
| 22264 | bw: usize, |
| 22265 | bh: usize, |
| 22266 | pri_strength: i32, |
| 22267 | sec_strength: i32, |
| 22268 | dir: usize, |
| 22269 | pri_damping: i32, |
| 22270 | sec_damping: i32, |
| 22271 | enable_primary: bool, |
| 22272 | enable_secondary: bool, |
| 22273 | coeff_shift: i32, |
| 22274 | smax: i32, |
| 22275 | ) { |
| 22276 | let clipping_required = enable_primary && enable_secondary; |
| 22277 | // libaom `cdef_filter_block_internal`: `cdef_pri_taps[(pri_strength >> |
| 22278 | // coeff_shift) & 1]` -- `pri_strength` (and its `adjust_strength` |
| 22279 | // derivative) carries the bit-depth shift, so the tap-set parity bit |
| 22280 | // must be read back above that shift, not off the raw low bit. |
| 22281 | let pri_taps = CDEF_PRI_TAPS[((pri_strength >> coeff_shift) & 1) as usize]; |
| 22282 | // Both damping shifts depend only on this block's strengths (spec 7.15.3's |
| 22283 | // `constrain`), so they are resolved here rather than per tap read. |
| 22284 | let pri_shift = cdef_constrain_shift(pri_strength.max(1), pri_damping); |
| 22285 | let sec_shift = cdef_constrain_shift(sec_strength.max(1), sec_damping); |
| 22286 | let bstride = bw + 2 * CDEF_BORDER; |
| 22287 | // `src` is [`cdef_gather`]'s window: (i, j) in block coordinates, taps |
| 22288 | // reaching +-CDEF_BORDER, so every index below is in range by construction. |
| 22289 | let at = |i: i32, j: i32| -> i32 { |
| 22290 | i32::from(src[(i + CDEF_BORDER as i32) as usize * bstride + (j + CDEF_BORDER as i32) as usize]) |
| 22291 | }; |
| 22292 | let (pri_dir, sec_dir0, sec_dir1) = |
| 22293 | (CDEF_DIRECTIONS[dir], CDEF_DIRECTIONS[(dir + 2) & 7], CDEF_DIRECTIONS[(dir + 6) & 7]); |
| 22294 | for i in 0..bh { |
| 22295 | for j in 0..bw { |
| 22296 | let (i32i, i32j) = (i as i32, j as i32); |
| 22297 | let x = at(i32i, i32j); |
| 22298 | let mut sum = 0i32; |
| 22299 | let mut max = x; |
| 22300 | let mut min = x; |
| 22301 | for k in 0..2usize { |
| 22302 | if enable_primary { |
| 22303 | let (tr, tc) = pri_dir[k]; |
| 22304 | let p0 = at(i32i + tr, i32j + tc); |
| 22305 | let p1 = at(i32i - tr, i32j - tc); |
| 22306 | sum += pri_taps[k] * cdef_constrain_shifted(p0 - x, pri_strength, pri_shift); |
| 22307 | sum += pri_taps[k] * cdef_constrain_shifted(p1 - x, pri_strength, pri_shift); |
| 22308 | if clipping_required { |
| 22309 | if p0 != CDEF_VERY_LARGE { |
| 22310 | max = max.max(p0); |
| 22311 | } |
| 22312 | if p1 != CDEF_VERY_LARGE { |
| 22313 | max = max.max(p1); |
| 22314 | } |
| 22315 | min = min.min(p0).min(p1); |
| 22316 | } |
| 22317 | } |
| 22318 | if enable_secondary { |
| 22319 | let (tr0, tc0) = sec_dir0[k]; |
| 22320 | let (tr1, tc1) = sec_dir1[k]; |
| 22321 | let s0 = at(i32i + tr0, i32j + tc0); |
| 22322 | let s1 = at(i32i - tr0, i32j - tc0); |
| 22323 | let s2 = at(i32i + tr1, i32j + tc1); |
| 22324 | let s3 = at(i32i - tr1, i32j - tc1); |
| 22325 | if clipping_required { |
| 22326 | for s in [s0, s1, s2, s3] { |
| 22327 | if s != CDEF_VERY_LARGE { |
| 22328 | max = max.max(s); |
| 22329 | } |
| 22330 | } |
| 22331 | min = min.min(s0).min(s1).min(s2).min(s3); |
| 22332 | } |
| 22333 | sum += CDEF_SEC_TAPS[k] |
| 22334 | * (cdef_constrain_shifted(s0 - x, sec_strength, sec_shift) |
| 22335 | + cdef_constrain_shifted(s1 - x, sec_strength, sec_shift) |
| 22336 | + cdef_constrain_shifted(s2 - x, sec_strength, sec_shift) |
| 22337 | + cdef_constrain_shifted(s3 - x, sec_strength, sec_shift)); |
| 22338 | } |
| 22339 | } |
| 22340 | let mut y = x + ((8 + sum - i32::from(sum < 0)) >> 4); |
| 22341 | if clipping_required { |
| 22342 | y = y.clamp(min, max); |
| 22343 | } |
| 22344 | dst[(oy + i) * stride + (ox + j)] = y.clamp(0, smax) as u16; |
| 22345 | } |
| 22346 | } |
| 22347 | } |
| 22348 | |
| 22349 | /// lane-perf7 step 1: [`cdef_filter_block`]'s AVX2 kernel -- the scalar |
| 22350 | /// filter was 13.1% of a 4K decode's self time. Nothing here is spec logic: |
| 22351 | /// the same taps, the same `constrain`, the same `min`/`max` clamp with the |
| 22352 | /// `CDEF_VERY_LARGE` sentinel excluded from the max, the same |
| 22353 | /// `y + ((8 + sum - (sum < 0)) >> 4)` rounding, done 16 samples (two rows) at |
| 22354 | /// a time in 16-bit lanes. |
| 22355 | /// |
| 22356 | /// 16-bit lanes are exact for every value this filter sees: window samples |
| 22357 | /// are in `[0, CDEF_VERY_LARGE]` (0x4000), so a tap difference fits in |
| 22358 | /// `i16`, a `constrain` result is bounded by the threshold (at most |
| 22359 | /// `15 << 4`), and the tap-weighted sum by `24 *` that. |
| 22360 | #[cfg(target_arch = "x86_64")] |
| 22361 | #[allow(unsafe_code)] |
| 22362 | #[allow(unsafe_op_in_unsafe_fn)] // the kernel's whole body is its contract |
| 22363 | mod cdef_simd { |
| 22364 | use super::{ |
| 22365 | cdef_constrain_shift, CDEF_BORDER, CDEF_DIRECTIONS, CDEF_PRI_TAPS, CDEF_SEC_TAPS, |
| 22366 | CDEF_VERY_LARGE, |
| 22367 | }; |
| 22368 | use std::arch::x86_64::*; |
| 22369 | |
| 22370 | /// The window's rows `i` and `i + 1` at one tap offset, as one 256-bit |
| 22371 | /// register (low half = row `i`). |
| 22372 | /// |
| 22373 | /// # Safety |
| 22374 | /// Requires AVX2; `off + bstride + 8` must be inside `w`. |
| 22375 | #[target_feature(enable = "avx2")] |
| 22376 | #[inline] |
| 22377 | unsafe fn ld2(w: *const i16, off: isize, bstride: isize) -> __m256i { |
| 22378 | let lo = _mm_loadu_si128(w.offset(off).cast()); |
| 22379 | let hi = _mm_loadu_si128(w.offset(off + bstride).cast()); |
| 22380 | _mm256_inserti128_si256(_mm256_castsi128_si256(lo), hi, 1) |
| 22381 | } |
| 22382 | |
| 22383 | /// Spec 7.15.3's `constrain`, lane-parallel: |
| 22384 | /// `sign(diff) * clamp(threshold - (|diff| >> shift), 0, |diff|)`. |
| 22385 | /// `_mm256_sign_epi16` reproduces `signum` exactly, zero included (the |
| 22386 | /// clamped magnitude is already 0 where `diff` is). |
| 22387 | /// |
| 22388 | /// # Safety |
| 22389 | /// Requires AVX2. |
| 22390 | #[target_feature(enable = "avx2")] |
| 22391 | #[inline] |
| 22392 | unsafe fn constrain(p: __m256i, x: __m256i, thr: __m256i, shift: __m128i) -> __m256i { |
| 22393 | let diff = _mm256_sub_epi16(p, x); |
| 22394 | let a = _mm256_abs_epi16(diff); |
| 22395 | let t = _mm256_sub_epi16(thr, _mm256_sra_epi16(a, shift)); |
| 22396 | let t = _mm256_min_epi16(_mm256_max_epi16(t, _mm256_setzero_si256()), a); |
| 22397 | _mm256_sign_epi16(t, diff) |
| 22398 | } |
| 22399 | |
| 22400 | /// [`super::cdef_find_dir_partials`] in AVX2 (libaom |
| 22401 | /// `cdef_find_dir_avx2`'s shape): eight i32 lanes per row instead of the |
| 22402 | /// four the auto-vectorised scalar form gets without the feature. |
| 22403 | /// |
| 22404 | /// # Safety |
| 22405 | /// Requires AVX2. `win` must hold [`super::cdef_gather`]'s 8x8 window |
| 22406 | /// grown by `CDEF_BORDER` at row stride `bstride`. |
| 22407 | #[target_feature(enable = "avx2")] |
| 22408 | pub unsafe fn find_dir_partials_avx2( |
| 22409 | win: &[i16], |
| 22410 | bstride: usize, |
| 22411 | coeff_shift: i32, |
| 22412 | ) -> [[i32; 16]; 8] { |
| 22413 | #[target_feature(enable = "avx2")] |
| 22414 | unsafe fn add8(dst: &mut [i32; 16], off: usize, v: __m256i) { |
| 22415 | let p = dst.as_mut_ptr().add(off) as *mut __m256i; |
| 22416 | _mm256_storeu_si256(p, _mm256_add_epi32(_mm256_loadu_si256(p), v)); |
| 22417 | } |
| 22418 | #[target_feature(enable = "avx2")] |
| 22419 | unsafe fn add4(dst: &mut [i32; 16], off: usize, v: __m128i) { |
| 22420 | let p = dst.as_mut_ptr().add(off) as *mut __m128i; |
| 22421 | _mm_storeu_si128(p, _mm_add_epi32(_mm_loadu_si128(p), v)); |
| 22422 | } |
| 22423 | let mut partial = [[0i32; 16]; 8]; |
| 22424 | let shift = _mm_cvtsi32_si128(coeff_shift); |
| 22425 | let c128 = _mm256_set1_epi32(128); |
| 22426 | let revidx = _mm256_setr_epi32(7, 6, 5, 4, 3, 2, 1, 0); |
| 22427 | let mut p6 = _mm256_setzero_si256(); |
| 22428 | for i in 0..8usize { |
| 22429 | let base = (i + CDEF_BORDER) * bstride + CDEF_BORDER; |
| 22430 | let raw = _mm_loadu_si128(win.as_ptr().add(base) as *const __m128i); |
| 22431 | let row = _mm256_sub_epi32(_mm256_sra_epi32(_mm256_cvtepi16_epi32(raw), shift), c128); |
| 22432 | let rev = _mm256_permutevar8x32_epi32(row, revidx); |
| 22433 | // `hadd(row, row)` is [p0, p1, p0, p1 | p2, p3, p2, p3], so the |
| 22434 | // four pairwise sums are the two lanes' low halves joined. |
| 22435 | let h = _mm256_hadd_epi32(row, row); |
| 22436 | let pair = |
| 22437 | _mm_unpacklo_epi64(_mm256_castsi256_si128(h), _mm256_extracti128_si256(h, 1)); |
| 22438 | let rpair = _mm_shuffle_epi32(pair, 0b00_01_10_11); |
| 22439 | let hs = _mm_hadd_epi32(pair, pair); |
| 22440 | let total = _mm_cvtsi128_si32(_mm_hadd_epi32(hs, hs)); |
| 22441 | add8(&mut partial[0], i, row); |
| 22442 | add4(&mut partial[1], i, pair); |
| 22443 | partial[2][i] = total; |
| 22444 | add4(&mut partial[3], i, rpair); |
| 22445 | add8(&mut partial[4], i, rev); |
| 22446 | add8(&mut partial[5], 3 - i / 2, row); |
| 22447 | p6 = _mm256_add_epi32(p6, row); |
| 22448 | add8(&mut partial[7], i / 2, row); |
| 22449 | } |
| 22450 | _mm256_storeu_si256(partial[6].as_mut_ptr() as *mut __m256i, p6); |
| 22451 | partial |
| 22452 | } |
| 22453 | |
| 22454 | /// [`super::cdef_filter_block_scalar`]'s arithmetic, two rows per |
| 22455 | /// iteration. `bw` is 8 (luma) or 4 (4:2:0 chroma) and `bh` matches; for |
| 22456 | /// `bw == 4` the upper four lanes of each half are window samples read |
| 22457 | /// past the row (never past the buffer -- `w` is padded to 256 entries), |
| 22458 | /// computed and then dropped by the 64-bit store. |
| 22459 | /// |
| 22460 | /// # Safety |
| 22461 | /// Requires AVX2. `src` must be [`super::cdef_gather`]'s 256-entry window |
| 22462 | /// buffer (the tap loads read up to 152 entries past its start, i.e. past |
| 22463 | /// the `(bw + 4) x (bh + 4)` samples themselves) and `dst` the `bh` rows |
| 22464 | /// of `bw` samples at `(ox, oy)`. |
| 22465 | #[target_feature(enable = "avx2")] |
| 22466 | #[allow(clippy::too_many_arguments)] |
| 22467 | pub unsafe fn filter_block_avx2( |
| 22468 | src: &[i16], |
| 22469 | dst: &mut [u16], |
| 22470 | stride: usize, |
| 22471 | ox: usize, |
| 22472 | oy: usize, |
| 22473 | bw: usize, |
| 22474 | bh: usize, |
| 22475 | pri_strength: i32, |
| 22476 | sec_strength: i32, |
| 22477 | dir: usize, |
| 22478 | pri_damping: i32, |
| 22479 | sec_damping: i32, |
| 22480 | enable_primary: bool, |
| 22481 | enable_secondary: bool, |
| 22482 | coeff_shift: i32, |
| 22483 | smax: i32, |
| 22484 | ) { |
| 22485 | let bstride = bw + 2 * CDEF_BORDER; |
| 22486 | // The caller's window is already `i16` and 256 entries long, so the |
| 22487 | // kernel taps it in place: the largest two-row load is |
| 22488 | // (bh + 3) * bstride + 8 <= 152 for the 8x8 luma block, 72 for the |
| 22489 | // 4x4 chroma one, both inside the buffer's zeroed tail. |
| 22490 | let w = src; |
| 22491 | let clipping_required = enable_primary && enable_secondary; |
| 22492 | let pri_taps = CDEF_PRI_TAPS[((pri_strength >> coeff_shift) & 1) as usize]; |
| 22493 | let pri_shift = _mm_cvtsi32_si128(cdef_constrain_shift(pri_strength.max(1), pri_damping)); |
| 22494 | let sec_shift = _mm_cvtsi32_si128(cdef_constrain_shift(sec_strength.max(1), sec_damping)); |
| 22495 | let pri_thr = _mm256_set1_epi16(pri_strength as i16); |
| 22496 | let sec_thr = _mm256_set1_epi16(sec_strength as i16); |
| 22497 | let very_large = _mm256_set1_epi16(CDEF_VERY_LARGE as i16); |
| 22498 | let zero = _mm256_setzero_si256(); |
| 22499 | let eight = _mm256_set1_epi16(8); |
| 22500 | let smaxv = _mm256_set1_epi16(smax as i16); |
| 22501 | let (pri_dir, sec_dir0, sec_dir1) = |
| 22502 | (CDEF_DIRECTIONS[dir], CDEF_DIRECTIONS[(dir + 2) & 7], CDEF_DIRECTIONS[(dir + 6) & 7]); |
| 22503 | let wp = w.as_ptr(); |
| 22504 | let bs = bstride as isize; |
| 22505 | let dp = dst.as_mut_ptr(); |
| 22506 | let mut i = 0usize; |
| 22507 | while i < bh { |
| 22508 | let base = (i + CDEF_BORDER) as isize * bs + CDEF_BORDER as isize; |
| 22509 | let x = ld2(wp, base, bs); |
| 22510 | let mut sum = zero; |
| 22511 | let mut max = x; |
| 22512 | let mut min = x; |
| 22513 | for k in 0..2usize { |
| 22514 | if enable_primary { |
| 22515 | let (tr, tc) = pri_dir[k]; |
| 22516 | let off = tr as isize * bs + tc as isize; |
| 22517 | let p0 = ld2(wp, base + off, bs); |
| 22518 | let p1 = ld2(wp, base - off, bs); |
| 22519 | let tap = _mm256_set1_epi16(pri_taps[k] as i16); |
| 22520 | sum = _mm256_add_epi16( |
| 22521 | sum, |
| 22522 | _mm256_mullo_epi16(tap, constrain(p0, x, pri_thr, pri_shift)), |
| 22523 | ); |
| 22524 | sum = _mm256_add_epi16( |
| 22525 | sum, |
| 22526 | _mm256_mullo_epi16(tap, constrain(p1, x, pri_thr, pri_shift)), |
| 22527 | ); |
| 22528 | if clipping_required { |
| 22529 | // The sentinel is excluded from the max exactly as the |
| 22530 | // scalar `if p != CDEF_VERY_LARGE` does: masking it to |
| 22531 | // 0 cannot win a max whose seed `x` is non-negative. |
| 22532 | max = _mm256_max_epi16( |
| 22533 | max, |
| 22534 | _mm256_andnot_si256(_mm256_cmpeq_epi16(p0, very_large), p0), |
| 22535 | ); |
| 22536 | max = _mm256_max_epi16( |
| 22537 | max, |
| 22538 | _mm256_andnot_si256(_mm256_cmpeq_epi16(p1, very_large), p1), |
| 22539 | ); |
| 22540 | min = _mm256_min_epi16(min, _mm256_min_epi16(p0, p1)); |
| 22541 | } |
| 22542 | } |
| 22543 | if enable_secondary { |
| 22544 | let (tr0, tc0) = sec_dir0[k]; |
| 22545 | let (tr1, tc1) = sec_dir1[k]; |
| 22546 | let off0 = tr0 as isize * bs + tc0 as isize; |
| 22547 | let off1 = tr1 as isize * bs + tc1 as isize; |
| 22548 | let s0 = ld2(wp, base + off0, bs); |
| 22549 | let s1 = ld2(wp, base - off0, bs); |
| 22550 | let s2 = ld2(wp, base + off1, bs); |
| 22551 | let s3 = ld2(wp, base - off1, bs); |
| 22552 | if clipping_required { |
| 22553 | for s in [s0, s1, s2, s3] { |
| 22554 | max = _mm256_max_epi16( |
| 22555 | max, |
| 22556 | _mm256_andnot_si256(_mm256_cmpeq_epi16(s, very_large), s), |
| 22557 | ); |
| 22558 | } |
| 22559 | min = _mm256_min_epi16( |
| 22560 | min, |
| 22561 | _mm256_min_epi16( |
| 22562 | _mm256_min_epi16(s0, s1), |
| 22563 | _mm256_min_epi16(s2, s3), |
| 22564 | ), |
| 22565 | ); |
| 22566 | } |
| 22567 | let c = _mm256_add_epi16( |
| 22568 | _mm256_add_epi16( |
| 22569 | constrain(s0, x, sec_thr, sec_shift), |
| 22570 | constrain(s1, x, sec_thr, sec_shift), |
| 22571 | ), |
| 22572 | _mm256_add_epi16( |
| 22573 | constrain(s2, x, sec_thr, sec_shift), |
| 22574 | constrain(s3, x, sec_thr, sec_shift), |
| 22575 | ), |
| 22576 | ); |
| 22577 | sum = _mm256_add_epi16( |
| 22578 | sum, |
| 22579 | _mm256_mullo_epi16(_mm256_set1_epi16(CDEF_SEC_TAPS[k] as i16), c), |
| 22580 | ); |
| 22581 | } |
| 22582 | } |
| 22583 | // `-1` where `sum < 0`, i.e. the scalar `- i32::from(sum < 0)`. |
| 22584 | let neg = _mm256_cmpgt_epi16(zero, sum); |
| 22585 | let mut y = _mm256_add_epi16( |
| 22586 | x, |
| 22587 | _mm256_srai_epi16(_mm256_add_epi16(_mm256_add_epi16(eight, sum), neg), 4), |
| 22588 | ); |
| 22589 | if clipping_required { |
| 22590 | y = _mm256_min_epi16(_mm256_max_epi16(y, min), max); |
| 22591 | } |
| 22592 | y = _mm256_min_epi16(_mm256_max_epi16(y, zero), smaxv); |
| 22593 | let (lo, hi) = (_mm256_castsi256_si128(y), _mm256_extracti128_si256(y, 1)); |
| 22594 | let o0 = ((oy + i) * stride + ox) as isize; |
| 22595 | let o1 = o0 + stride as isize; |
| 22596 | if bw == 8 { |
| 22597 | _mm_storeu_si128(dp.offset(o0).cast(), lo); |
| 22598 | _mm_storeu_si128(dp.offset(o1).cast(), hi); |
| 22599 | } else { |
| 22600 | _mm_storel_epi64(dp.offset(o0).cast(), lo); |
| 22601 | _mm_storel_epi64(dp.offset(o1).cast(), hi); |
| 22602 | } |
| 22603 | i += 2; |
| 22604 | } |
| 22605 | } |
| 22606 | } |
| 22607 | |
| 22608 | /// Spec 7.14: the in-loop deblocking filter, applied once per frame after |
| 22609 | /// tile reconstruction and before [`apply_cdef`]. Uniform-level path only -- |
| 22610 | /// `stream.rs` already refuses `delta_lf_present`/segmentation upstream, so |
| 22611 | /// every block's level comes from [`LoopFilterParams::level`] plus the |
| 22612 | /// `ref_deltas`/`mode_deltas` adjustment alone (spec 7.14.4's |
| 22613 | /// `get_filter_level`, precomputed per-block here rather than cached in a |
| 22614 | /// table the way libaom's `av1_loop_filter_frame_init` does). |
| 22615 | /// |
| 22616 | /// Ported from libaom's `av1/common/av1_loopfilter.c` (edge/level decision) |
| 22617 | /// and `aom_dsp/loopfilter.c` (the `aom_lpf_*` pixel kernels). |
| 22618 | fn apply_deblock( |
| 22619 | y: &mut PlaneBuf<'static>, |
| 22620 | u: &mut PlaneBuf<'static>, |
| 22621 | v: &mut PlaneBuf<'static>, |
| 22622 | lf: &LoopFilterParams, |
| 22623 | n: &Neighbours, |
| 22624 | frame_width: usize, |
| 22625 | frame_height: usize, fctx: &crate::decode::FrameCtx, |
| 22626 | ) { |
| 22627 | // lane-part32 r2 debug rung, env-gated: bisecting a widespread pixel |
| 22628 | // mismatch between the raw reconstruction and post-filter output. |
| 22629 | if crate::envflags::env_flag!("EC_AV1_DEBUG_SKIP_DEBLOCK") { |
| 22630 | return; |
| 22631 | } |
| 22632 | if lf.level[0] != 0 || lf.level[1] != 0 { |
| 22633 | deblock_plane(y, 0, lf, n, frame_width, frame_height, fctx); |
| 22634 | } |
| 22635 | if lf.level[2] != 0 { |
| 22636 | deblock_plane(u, 1, lf, n, frame_width, frame_height, fctx); |
| 22637 | } |
| 22638 | if lf.level[3] != 0 { |
| 22639 | deblock_plane(v, 2, lf, n, frame_width, frame_height, fctx); |
| 22640 | } |
| 22641 | } |
| 22642 | |
| 22643 | /// [`apply_deblock`] for ONE pass over ONE band of luma rows |
| 22644 | /// (`y_lo..y_hi`), the form [`pipe_run`] calls it in. Running the three |
| 22645 | /// planes' vertical passes and then their three horizontal ones is the same |
| 22646 | /// output as `apply_deblock`'s plane-at-a-time order: the planes are |
| 22647 | /// separate buffers and neither pass reads another plane. |
| 22648 | #[allow(clippy::too_many_arguments)] |
| 22649 | fn deblock_span( |
| 22650 | y: &mut PlaneBuf<'static>, |
| 22651 | u: &mut PlaneBuf<'static>, |
| 22652 | v: &mut PlaneBuf<'static>, |
| 22653 | lf: &LoopFilterParams, |
| 22654 | n: &Neighbours, |
| 22655 | frame_width: usize, |
| 22656 | frame_height: usize, |
| 22657 | y_lo: usize, |
| 22658 | y_hi: usize, |
| 22659 | horz: bool, |
| 22660 | fctx: &crate::decode::FrameCtx, |
| 22661 | ) { |
| 22662 | if lf.level[0] != 0 || lf.level[1] != 0 { |
| 22663 | deblock_plane_span(y, 0, lf, n, frame_width, frame_height, y_lo, y_hi, horz, fctx); |
| 22664 | } |
| 22665 | if lf.level[2] != 0 { |
| 22666 | deblock_plane_span(u, 1, lf, n, frame_width, frame_height, y_lo >> ss_y(fctx), y_hi >> ss_y(fctx), horz, fctx); |
| 22667 | } |
| 22668 | if lf.level[3] != 0 { |
| 22669 | deblock_plane_span(v, 2, lf, n, frame_width, frame_height, y_lo >> ss_y(fctx), y_hi >> ss_y(fctx), horz, fctx); |
| 22670 | } |
| 22671 | } |
| 22672 | |
| 22673 | /// Luma-mi-grid position (row or col) a plane-local pixel coordinate falls |
| 22674 | /// in -- `Neighbours::tx_grid`/`ref_grid` are always indexed at the luma |
| 22675 | /// 4x4-mi grain, even when `coord` is a chroma coordinate (2 chroma px per |
| 22676 | /// mi unit under this decoder's assumed 4:2:0 subsampling). |
| 22677 | /// |
| 22678 | /// lane-t900 r30b: the chroma arm ORs in the subsampling, exactly as |
| 22679 | /// `set_lpf_parameters` does (`av1_loopfilter.c:245`, `mi_col = scale_horz | |
| 22680 | /// ((x << scale_horz) >> MI_SIZE_LOG2)`) -- "for sub8x8 block, chroma |
| 22681 | /// prediction mode is obtained from the bottom/right mi structure of the |
| 22682 | /// co-located 8x8 luma block, so for chroma plane both mi_row and mi_col |
| 22683 | /// should be odd". Dropping that `| 1` read the group's TOP-LEFT mi, which |
| 22684 | /// is the same cell for every block 8x8 and larger and a DIFFERENT coded |
| 22685 | /// block (different tx size, ref frame, skip flag and delta_lf) as soon as |
| 22686 | /// the group is split into 4x4 luma blocks (class |
| 22687 | /// `context-read-from-one-cell`). |
| 22688 | fn plane_to_mi(chroma: bool, coord: usize, ss: usize) -> usize { |
| 22689 | // scale | ((coord << scale) >> 2). ss=1 is (coord/2)|1; ss=0 is coord/4. |
| 22690 | if chroma { ss | ((coord << ss) >> 2) } else { coord / 4 } |
| 22691 | } |
| 22692 | |
| 22693 | /// This plane's own transform width in pixels at the covering block, spec |
| 22694 | /// `get_transform_size`'s plane-aware lookup: `tx_grid` stores the luma |
| 22695 | /// width directly; chroma halves it (this decoder's UV transform is always |
| 22696 | /// half its coded block's luma transform, spec's `av1_get_max_uv_txsize` |
| 22697 | /// under 4:2:0 and a block that never splits its own transform). |
| 22698 | fn tx_px_at(n: &Neighbours, chroma: bool, mi_r: usize, mi_c: usize) -> u32 { |
| 22699 | let grid = if chroma { &n.uv_tx_grid } else { &n.tx_grid }; |
| 22700 | grid.get(mi_r * n.skip_grid_cols_mi + mi_c).copied().unwrap_or(0) as u32 |
| 22701 | } |
| 22702 | |
| 22703 | /// [`tx_px_at`]'s HEIGHT twin (lane-rect r2): the deblocker's horizontal |
| 22704 | /// edges step and gate by transform height, which differs from width only |
| 22705 | /// on rect partition strips. |
| 22706 | fn tx_h_px_at(n: &Neighbours, chroma: bool, mi_r: usize, mi_c: usize) -> u32 { |
| 22707 | let grid = if chroma { &n.uv_tx_h_grid } else { &n.tx_h_grid }; |
| 22708 | grid.get(mi_r * n.skip_grid_cols_mi + mi_c).copied().unwrap_or(0) as u32 |
| 22709 | } |
| 22710 | |
| 22711 | /// `get_tx_size_context` (spec 9.3's `TxDepthCtx`, libaom |
| 22712 | /// `TXFM_CONTEXT`/`entropymode.c`): the sum of two boolean terms, each |
| 22713 | /// present only when its neighbour exists inside the true frame -- whether |
| 22714 | /// the block immediately above (at this block's own leftmost mi column) or |
| 22715 | /// to the left (at its own topmost mi row) coded a transform at least as wide |
| 22716 | /// (tall) as this block's own `max_txsize_rect_lookup[bsize]`. |
| 22717 | /// |
| 22718 | /// lane-av1txbands: this reads the real `TXFM_CONTEXT` bands |
| 22719 | /// ([`Neighbours::above_txfm`]/[`Neighbours::left_txfm`]) for EVERY frame |
| 22720 | /// now -- libaom maintains them on intra-only frames too (`parse_decode_block` |
| 22721 | /// runs `set_txfm_ctxs` for every block, and |
| 22722 | /// [`Neighbours::fill_lf_grid_rect`] publishes them there), except that an |
| 22723 | /// *inter* neighbour contributes its own BLOCK size rather than its transform |
| 22724 | /// size -- and libaom's `is_inter_block` counts an INTRABC block |
| 22725 | /// (`blockd.h:373`), which is what the `above_inter`/`left_inter` bands carry. |
| 22726 | /// The previous deblock-grid approximation coincided with this only while |
| 22727 | /// every neighbour coded one whole-block transform, which is exactly what |
| 22728 | /// `TxMode::Select` ends. |
| 22729 | /// |
| 22730 | /// The rect form (lane-intrasplit r1): `get_tx_size_context` compares the |
| 22731 | /// neighbour ABOVE against `tx_size_wide[max_txsize_rect_lookup[bsize]]` and |
| 22732 | /// the neighbour to the LEFT against `tx_size_high[...]` (libaom |
| 22733 | /// `pred_common.h:342`), which for a 2:1 strip are two different numbers. |
| 22734 | fn tx_size_context_txfm_rect( |
| 22735 | n: &Neighbours, |
| 22736 | (mi_r, mi_c): (usize, usize), |
| 22737 | own_w: usize, |
| 22738 | own_h: usize, fctx: &crate::decode::FrameCtx, |
| 22739 | ) -> usize { |
| 22740 | hit!(INTRA_IN_INTER_TXCTX_HITS); |
| 22741 | // `max_txsize_rect_lookup[bsize]` tops out at TX_64X64: no transform is |
| 22742 | // wider or taller than 64, so a block with a 128 side resolves against 64 |
| 22743 | // (lane-sb128b r1). |
| 22744 | let (own_w, own_h) = (own_w.min(64), own_h.min(64)); |
| 22745 | let has_above = mi_r > n.tile_row0_mi; |
| 22746 | let has_left = mi_c > n.tile_col0_mi; |
| 22747 | let mut above = usize::from(n.above_txfm[mi_c]) >= own_w; |
| 22748 | let mut left = usize::from(n.left_txfm[mi_r]) >= own_h; |
| 22749 | if has_above && n.above_inter[mi_c] { |
| 22750 | above = usize::from(n.above_side_mi[mi_c]) >= own_w; |
| 22751 | } |
| 22752 | if has_left && n.left_inter[mi_r] { |
| 22753 |
Showing the first 1 MiB of this file. View raw.