//! Frame decoding: superframe split, uncompressed header (via //! [`ec_vp9_syntax::Vp9Parser`]), compressed header, tile parsing, //! partition recursion, intra prediction, token decoding, //! reconstruction and the loop filter (spec 6, 7, 8; libvpx //! `vp9_decodeframe.c`). //! //! Capability: every VP9 profile (0-3), 8/10/12-bit and every coded //! subsampling (4:2:0, 4:2:2, 4:4:0, 4:4:4): keyframes, intra-only frames //! and inter frames (motion compensation, residual reconstruction, the loop //! filter and an 8-slot reference DPB). A reference whose coded size differs //! from the current frame is scaled during prediction; //! `show_existing_frame` returns the buffered reference picture instead of //! pretending to decode. Odd coded extents are supported: chroma stores and //! crops at `(w + ss_x) >> ss_x` (`uv_crop_width`), matching libvpx. use crate::adapt::{adapt_coef_probs, adapt_mode_probs, adapt_mv_probs, COEF_MAX_UPDATE_FACTOR, COEF_MAX_UPDATE_FACTOR_AFTER_KEY}; use crate::header::{FrameContext, read_compressed_header}; use crate::intra::build_intra_predictors; use crate::loopfilter::LfGrids; use crate::mc::{RefPlane, ScaleFactors, average_split_mvs, build_inter_predictors}; use crate::modes::{MiInfo, MiState, read_intra_frame_mode_info}; use crate::tables::*; use crate::tokens::{PlaneContexts, decode_coefs, scan_for}; use crate::transform::inverse_transform_add; use crate::tables::{ensure, corrupt}; use crate::{Error, Result, Sample}; use ec_vp9_syntax::{FrameHeader, FrameType, Vp9Parser, superframe}; const SB_MI: usize = 8; // a 64x64 superblock in mi (8x8) units /// The compressed-header bytes of `frame`. Both sizes come from the input, so /// a header that names bytes past the end of the frame is a corrupt-stream /// error, not a slice panic. fn compressed_header_slice<'a>(frame: &'a [u8], hdr: &FrameHeader) -> Result<&'a [u8]> { let start = hdr.uncompressed_header_size as usize; let end = start + hdr.header_size_in_bytes as usize; frame .get(start..end) .ok_or_else(|| corrupt("compressed header runs past the frame")) } /// A decoded, displayable frame: cropped contiguous planes. #[derive(Clone, Debug)] pub struct Picture { /// Luma plane, `stride`-spaced rows of `width` samples. pub y: Vec, /// U (Cb) plane, chroma resolution (`(width + ss_x) >> ss_x` by /// `(height + ss_y) >> ss_y`). pub u: Vec, /// V (Cr) plane, chroma resolution. pub v: Vec, /// Frame width in pixels. pub width: u16, /// Frame height in pixels. pub height: u16, /// Luma row pitch of the returned planes (== `width`; the crop packs rows). pub stride: usize, /// Chroma row pitch of the returned planes (`(width + ss_x) >> ss_x`; /// chroma ceils an odd coded width). pub uv_stride: usize, /// Bits per sample (8 for profile 0, 10 for profile 2). pub bit_depth: u8, /// Chroma horizontal subsampling (`subsampling_x`): 0 for 4:4:4, 1 for /// 4:2:0/4:2:2. Chroma width is `(width + subsampling_x) >> subsampling_x`. pub subsampling_x: u8, /// Chroma vertical subsampling (`subsampling_y`): 0 for 4:4:4/4:2:2, 1 /// for 4:2:0/4:4:0. pub subsampling_y: u8, } struct Planes { y: Vec, u: Vec, v: Vec, ys: usize, uvs: usize, aw: usize, ah: usize, /// Bits per sample this frame was decoded at. bd: u8, /// Chroma horizontal subsampling (`subsampling_x`): 0 for 4:4:4, 1 for /// 4:2:0/4:2:2. ss_x: usize, /// Chroma vertical subsampling (`subsampling_y`): 0 for 4:4:4/4:2:2, 1 /// for 4:2:0/4:4:0. ss_y: usize, } /// VP9 level 6.2 caps a picture at 8192x4352 luma samples. Larger frame headers are refused /// before any picture or mode-info buffer is sized from them (fuzz regression: a 24580-wide key frame). const MAX_LUMA_SAMPLES: u64 = 8192 * 4352; fn validate(hdr: &FrameHeader) -> Result<()> { if hdr.bit_depth != 8 && hdr.bit_depth != 10 && hdr.bit_depth != 12 { return Err(Error::unsupported( format!("vp9 bit depth {}", hdr.bit_depth), "this lane decodes 8-, 10- and 12-bit only", )); } if u64::from(hdr.width) * u64::from(hdr.height) > MAX_LUMA_SAMPLES { return Err(Error::unsupported( format!("vp9 frame {}x{}", hdr.width, hdr.height), "this lane decodes up to VP9 level 6.2 (8192x4352 luma samples)", )); } Ok(()) } impl Planes { /// A frame scratch at the 64-multiple ALIGNED extent, filled with the /// neutral grey `1 << (bd - 1)` (`128` at 8-bit, `512` at 10-bit). The /// aligned the decode writes real samples into it (blocks at the frame /// edge decode at FULL size), so the fill only backs the never-written /// padding past the MI extent. Chroma planes are `aw >> ss_x` by /// `ah >> ss_y`; since `aw`/`ah` are 64-multiples the shift is exact. fn new(aw: usize, ah: usize, bd: u8, ss_x: usize, ss_y: usize) -> Planes { let fill = 1u16 << (bd - 1); let (cw, ch) = (aw >> ss_x, ah >> ss_y); Planes { y: vec![fill; aw * ah], u: vec![fill; cw * ch], v: vec![fill; cw * ch], ys: aw, uvs: cw, aw, ah, bd, ss_x, ss_y, } } /// Whether this buffer has the geometry and depth a frame of this size needs. fn fits(&self, aw: usize, ah: usize, bd: u8, ss_x: usize, ss_y: usize) -> bool { self.aw == aw && self.ah == ah && self.bd == bd && self.ss_x == ss_x && self.ss_y == ss_y } /// Set every sample back to the neutral grey `new` starts from, so a recycled /// buffer is indistinguishable from a fresh one. fn refill(&mut self) { let fill = 1u16 << (self.bd - 1); self.y.fill(fill); self.u.fill(fill); self.v.fill(fill); } /// Immutable plane + stride (the predictor reads references). fn plane_ref(&self, plane: usize) -> (&[Sample], usize) { match plane { 0 => (&self.y, self.ys), 1 => (&self.u, self.uvs), _ => (&self.v, self.uvs), } } fn plane(&mut self, plane: usize) -> (&mut [Sample], usize) { match plane { 0 => (&mut self.y, self.ys), 1 => (&mut self.u, self.uvs), _ => (&mut self.v, self.uvs), } } fn dims(&self, plane: usize) -> (usize, usize) { if plane == 0 { (self.aw, self.ah) } else { (self.aw >> self.ss_x, self.ah >> self.ss_y) } } } /// The VP9 decoder. Feed complete frames — one full VP9 frame payload /// or one superframe chunk, exactly what a container demuxer hands over /// for the `VP90` codec. pub struct Decoder { parser: Vp9Parser, /// The frame context in force for the frame being parsed. /// /// Boxed for the same reason as `frame_ctxs` below: a `FrameContext` is /// 2039 bytes, and leaving this one inline put 73% of the whole `Decoder` /// back on the stack. `Stack`: crate::stack_budget. ctx: Box, refs: [Option; 8], /// Frame scratch, live only inside `decode_keyframe`. planes: Option, /// Frame buffers no longer referenced, kept for reuse so a frame does not /// allocate (and page-fault) its planes afresh. spare: Vec, /// The four stored frame contexts (spec 6.2 `frame_context_idx`). /// /// Boxed because a `FrameContext` is 2039 bytes and four of them inline /// are 8156 of the 10944 a `Decoder` costs a caller that takes one BY /// VALUE from `Decoder::new`. Indexing and whole-array assignment read /// through the box. `Stack`: crate::stack_budget. frame_ctxs: Box<[FrameContext; 4]>, /// The previous frame's per-8x8 MV field and segment map /// (`prev_frame->mvs` / `last_frame_seg_map`). prev_mvs: Vec, prev_seg: Vec, last: LastFrameFacts, /// Live only while an inter frame is being parsed. iw: Option, /// Frames parsed by `decode_syntax` (the dump's frame index). frames_seen: usize, /// Frames decoded by `decode` (the dumps' `frame=` field for this path; /// `decode_syntax` counts the same way in `frames_seen`). decode_index: usize, /// Mode-info blocks parsed in the most recent inter frame (0 otherwise). syntax_blocks: usize, /// `twd->extend_and_predict_buf`: the per-prediction bordered reference /// window built when a block's read leaves the reference frame. mc_scratch: Vec, /// The `vpx_convolve8` stride-64 intermediate plus the second scratch its /// averaging variant needs. mc_temp: Vec, /// Tile-column workers per frame. Set by `EC_VP9_THREADS` when that is a /// positive integer (clamped to 1..=64); otherwise `default_threads()`. threads: usize, } /// Default tile-column worker count when `EC_VP9_THREADS` is unset or invalid: /// the host's available parallelism, capped at 6 (the measured knee for 1080p /// tile streams; see `lanes/vp9speed.report.md`). Falls back to 1 if the /// parallelism cannot be determined. fn default_threads() -> usize { std::thread::available_parallelism().map_or(1, |n| n.get()).min(6) } /// One DPB slot: a decoded frame buffer at its ALIGNED (64-multiple) extent /// plus the coded size it was decoded at. /// /// The aligned extent is load-bearing, not slack: libvpx writes a block at the /// frame edge at its FULL size (`decodeframe.c:1216`), so the rows past the /// coded height hold real reconstructed samples, and `dec_build_inter_predictors` /// reads them directly whenever a block's window is inside the frame with a /// whole-pixel MV (`build_mc_border` is only reached when the window leaves the /// frame or the coded size is not a multiple of 8). Cropping the reference to /// the visible size would drop exactly those samples. #[derive(Clone)] struct RefFrame { planes: std::sync::Arc, /// Coded width — the predictor's `y_crop_width`. width: u16, /// Coded height — the predictor's `y_crop_height`. height: u16, } /// What `use_prev_frame_mvs` (decodeframe.c:3023) tests about the last frame. #[derive(Clone, Copy, Default)] struct LastFrameFacts { width: u32, height: u32, intra_only: bool, show_frame: bool, key: bool, } /// State of one inter-syntax frame walk. struct InterWalk { ifs: crate::inter::InterFrameState, sign_bias: [bool; 4], use_prev: bool, seg_map: Vec, mvs: Vec, prev_seg: Vec, prev_mvs: Vec, tile_col_end: usize, frame_index: usize, /// Mode-info blocks parsed in this frame (the harness's count gate). blocks: usize, } impl Decoder { /// Create an empty decoder; the first key frame sets the size. pub fn new() -> Self { Decoder { parser: Vp9Parser::new(), ctx: Box::new(FrameContext::new(true)), refs: [const { None }; 8], planes: None, spare: Vec::new(), frame_ctxs: { let d = FrameContext::new(false); Box::new([d.clone(), d.clone(), d.clone(), d]) }, prev_mvs: Vec::new(), prev_seg: Vec::new(), last: LastFrameFacts::default(), iw: None, frames_seen: 0, decode_index: 0, syntax_blocks: 0, mc_scratch: Vec::new(), mc_temp: vec![0; 64 * 135 + 64 * 64], threads: std::env::var("EC_VP9_THREADS") .ok() .and_then(|v| v.parse::().ok()) .filter(|&n| n > 0) .map_or_else(default_threads, |n| n.clamp(1, 64)), } } /// Mode-info blocks the most recent inter frame parsed; `0` for a keyframe /// or before any frame. The syntax harness pins this number. pub fn last_frame_blocks(&self) -> usize { self.syntax_blocks } /// Decode one frame (or one superframe chunk); returns the picture /// when a frame is for display. Hidden frames still update state. pub fn decode(&mut self, frame: &[u8]) -> Result> { let mut out = None; for sub in superframe::split(frame)? { if let Some(pic) = self.decode_one(sub)? { out = Some(pic); } } Ok(out) } fn decode_one(&mut self, frame: &[u8]) -> Result> { let hdr = self.parser.parse_frame(frame)?; if hdr.show_existing_frame { let slot = hdr.frame_to_show_map_idx as usize; return match self.refs.get(slot).and_then(|p| p.as_ref()) { Some(p) => Ok(Some(crop_picture(&p.planes, p.width, p.height))), None => Err(corrupt("show_existing_frame names an empty slot")), }; } validate(&hdr)?; // One index per decoded frame (the dumps' `frame=` field), so this // path and `decode_syntax` label the same frame the same way. let index = self.decode_index; self.decode_index += 1; if crate::trace_enabled() { eprintln!("B FRAME x FRAME x"); } if hdr.frame_type == FrameType::Key { return self.decode_key_frame(&hdr, frame); } if hdr.intra_only { return self.decode_intra_only_frame(&hdr, frame); } self.decode_inter_frame(&hdr, frame, index, true) } /// Decode one key frame through to pixels and refresh every slot. fn decode_key_frame(&mut self, hdr: &FrameHeader, frame: &[u8]) -> Result> { let hdr = hdr.clone(); // setup_past_independence (spec 7.2): a key frame resets the frame // context, so every STORED context goes back to the inter defaults. // The keyframe's own mode info reads the const `vp9_kf_partition_probs` // (libvpx `get_partition_probs`), which our `new(true)` models on the // active copy only. let d = FrameContext::new(false); *self.frame_ctxs = [d.clone(), d.clone(), d.clone(), d]; *self.ctx = FrameContext::new(true); self.prev_mvs.clear(); self.prev_seg.clear(); self.last = LastFrameFacts { width: hdr.width, height: hdr.height, intra_only: hdr.intra_only, show_frame: hdr.show_frame, key: true, }; // [scratch probe] EC_VP9_FORCE_UH / EC_VP9_FORCE_HSZ: override the // uncompressed-header size / header_size_in_bytes split for diagnosis. let hdr = { let mut h = hdr; if let Ok(v) = std::env::var("EC_VP9_FORCE_UH") { h.uncompressed_header_size = v.parse().unwrap_or(h.uncompressed_header_size); } if let Ok(v) = std::env::var("EC_VP9_FORCE_HSZ") { h.header_size_in_bytes = v.parse().unwrap_or(h.header_size_in_bytes); } h }; let ch = read_compressed_header( compressed_header_slice(frame, &hdr)?, &mut self.ctx, &hdr, )?; let pic = self.decode_keyframe(&hdr, frame, ch.tx_mode)?; if hdr.refresh_frame_context { self.ctx.reset_inter_partition(); self.frame_ctxs[hdr.frame_context_idx as usize] = self.ctx.as_ref().clone(); } // Reference refresh (spec 8.10): a keyframe updates all slots. let frame = RefFrame { planes: std::sync::Arc::new(self.planes.take().expect("frame scratch")), width: hdr.width as u16, height: hdr.height as u16, }; for slot in self.refs.iter_mut() { *slot = Some(frame.clone()); } Ok(if hdr.show_frame { Some(pic) } else { None }) } /// Decode an intra-only frame through to pixels. `libvpx /// vp9_decodeframe.c:2708-2745` makes it an INTER frame that never shows /// and refreshes only the slots its `refresh_frame_flags` names, but its /// body is a keyframe's: intra-only mode info, intra prediction, the intra /// coefficient axis and the intra loop-filter levels. Two header decisions /// differ from a keyframe (`vp9_setup_past_independence`, /// `vp9/common/vp9_entropymode.c:424`): /// /// - the ACTIVE frame context comes from storage /// (`*cm->fc = cm->frame_contexts[frame_context_idx]`, /// `vp9_decodeframe.c:3008`) — reset to the defaults only for /// `reset_frame_context` 2 (selected) or 3 (all), or under /// error-resilient mode, never unconditionally like a keyframe (whose /// `cm->frame_type == KEY_FRAME` clause resets every stored context); /// - partitions read the const `vp9_kf_partition_probs` /// (`set_partition_probs`), so the stored context's own `partition` /// field survives the frame untouched. fn decode_intra_only_frame( &mut self, hdr: &FrameHeader, frame: &[u8], ) -> Result> { let hdr = hdr.clone(); if hdr.error_resilient_mode || hdr.reset_frame_context == 3 { let d = FrameContext::new(false); *self.frame_ctxs = [d.clone(), d.clone(), d.clone(), d]; } else if hdr.reset_frame_context == 2 { self.frame_ctxs[0] = FrameContext::new(false); } // The parser forces frame_context_idx to 0 for an intra frame. let mut ctx = self.frame_ctxs[0].clone(); let stored_partition = ctx.partition; ctx.use_key_partition(); *self.ctx = ctx; self.prev_seg.clear(); self.prev_mvs.clear(); self.last = LastFrameFacts { width: hdr.width, height: hdr.height, intra_only: true, show_frame: hdr.show_frame, key: false, }; let ch = read_compressed_header( compressed_header_slice(frame, &hdr)?, &mut self.ctx, &hdr, )?; let pic = self.decode_keyframe(&hdr, frame, ch.tx_mode)?; if hdr.refresh_frame_context { // Store back with the partition probs the context arrived with: // the const key table is not a `FRAME_CONTEXT` field. self.ctx.partition = stored_partition; self.frame_ctxs[0] = self.ctx.as_ref().clone(); } // Reference refresh (spec 8.10): only `refresh_frame_flags` slots. let frame = RefFrame { planes: std::sync::Arc::new(self.planes.take().expect("frame scratch")), width: hdr.width as u16, height: hdr.height as u16, }; for slot in 0..8 { if hdr.refresh_frame_flags & (1 << slot) != 0 { let old = self.refs[slot].replace(frame.clone()); self.recycle(old); } } Ok(if hdr.show_frame { Some(pic) } else { None }) } /// Parse one frame's syntax: keyframes decode through to pixels, inter /// frames are parsed (headers, mode info, MVs) with no reconstruction and /// no picture. `decode` still refuses inter frames. pub fn decode_syntax(&mut self, frame: &[u8]) -> Result<()> { for sub in superframe::split(frame)? { let index = self.frames_seen; self.frames_seen += 1; let hdr = self.parser.parse_frame(sub)?; if hdr.show_existing_frame { continue; } validate(&hdr)?; if hdr.frame_type == FrameType::Key { // Propagate: a corrupt keyframe leaves a half-updated frame // context, prev-frame MV field and reference slots behind, so // any inter-frame dump after it would be meaningless evidence. self.decode_key_frame(&hdr, sub)?; } else if hdr.intra_only { // Same body as a keyframe, but an inter frame that refreshes // only the slots it names. self.decode_intra_only_frame(&hdr, sub)?; } else { self.decode_inter_frame(&hdr, sub, index, false)?; } } Ok(()) } /// Inter-frame decode: compressed header, the tile walk (mode info, MVs /// and tokens always; prediction, residual and the loop filter when /// `pixels`), then the reference refresh. /// /// `pixels == false` is the syntax-only walk the instrumented harnesses /// drive: tokens keep the entropy stream aligned, no pixel is touched and /// the reference slots are left alone. fn decode_inter_frame( &mut self, hdr: &FrameHeader, frame: &[u8], index: usize, pixels: bool, ) -> Result> { let ss_x = hdr.subsampling_x as usize; let ss_y = hdr.subsampling_y as usize; // Frame-context bookkeeping (spec 6.2 / libvpx `vp9_decode_frame`): // 3 resets every stored context, 2 resets the selected one; 1 means // "reset after this frame" and takes no action at frame start. if hdr.reset_frame_context == 3 { let d = FrameContext::new(false); *self.frame_ctxs = [d.clone(), d.clone(), d.clone(), d]; } else if hdr.reset_frame_context == 2 { self.frame_ctxs[hdr.frame_context_idx as usize] = FrameContext::new(false); } *self.ctx = self.frame_ctxs[hdr.frame_context_idx as usize].clone(); // Reference slots (spec 7.2): `ref_frame_idx[i]` names the slot LAST, // GOLDEN and ALTREF use. libvpx validates every named reference's // format here (`vp9_decodeframe.c:1620-1627`); a reference whose coded // size differs from this frame's is SCALED during prediction // (`vp9_setup_scale_factors_for_frame` per reference), not refused. if pixels { for (i, name) in ["last", "golden", "altref"].iter().enumerate() { let slot = hdr.ref_frame_idx[i] as usize; if slot >= self.refs.len() || hdr.ref_frame_idx[i] >= 8 { return Err(corrupt("vp9 inter names a reference slot past the DPB")); } if self.refs[slot].is_none() { return Err(Error::corrupt(format!( "vp9 inter reference `{name}` names empty slot {slot}" ))); } } let width = hdr.width as usize; let height = hdr.height as usize; let aw = width.div_ceil(64) * 64; let ah = height.div_ceil(64) * 64; self.planes = Some(self.frame_planes(aw, ah, hdr.bit_depth, ss_x, ss_y)); } let hdr_start = hdr.uncompressed_header_size as usize; if crate::trace_enabled() { eprintln!("CHSTART frame={index}"); } let ch = read_compressed_header( compressed_header_slice(frame, hdr)?, &mut self.ctx, hdr, )?; if crate::interdump_enabled() { let mut line = format!("PPFLAT frame={index}"); for row in self.ctx.partition.iter() { for v in row.iter() { line.push_str(&format!(" {v}")); } } eprintln!("{line}"); } // INTRA_FRAME's sign bias is 0 by definition; the coded three land at // LAST/GOLDEN/ALTREF. let sign_bias = [ false, hdr.ref_frame_sign_bias[0], hdr.ref_frame_sign_bias[1], hdr.ref_frame_sign_bias[2], ]; let mut ifs = crate::inter::InterFrameState { reference_mode: ch.reference_mode, ..Default::default() }; if ch.reference_mode != crate::inter::SINGLE_REFERENCE { crate::inter::setup_compound_reference_mode(&mut ifs, sign_bias); } let use_prev = !hdr.error_resilient_mode && hdr.width == self.last.width && hdr.height == self.last.height && !self.last.intra_only && self.last.show_frame && !self.last.key; let mi_cols = hdr.mi_cols() as usize; let mi_rows = hdr.mi_rows() as usize; let tile_cols = 1usize << hdr.tile_info.cols_log2; let tile_rows = 1usize << hdr.tile_info.rows_log2; // The previous frame's segment map only predicts from a frame of the same // mode-info size; a resized frame reads it as all zeros (libvpx // reallocates the map zeroed when the size changes). // `vp9_setup_past_independence` (error-resilient frames) also zeroes it. if hdr.error_resilient_mode || self.prev_seg.len() != mi_cols * mi_rows { self.prev_seg.clear(); } self.iw = Some(InterWalk { ifs, sign_bias, use_prev, seg_map: vec![0; mi_cols * mi_rows], mvs: vec![crate::inter::MvRef::default(); mi_cols * mi_rows], prev_seg: std::mem::take(&mut self.prev_seg), prev_mvs: std::mem::take(&mut self.prev_mvs), tile_col_end: mi_cols, frame_index: index, blocks: 0, }); let data: &[u8] = frame .get(hdr_start + hdr.header_size_in_bytes as usize..) .ok_or_else(|| corrupt("tile data starts past the frame"))?; let tiles = split_tiles(data, tile_cols, tile_rows)?; let job = TileJob { hdr, tx_mode: ch.tx_mode, pixels, level_of_seg: [[0u8; 2]; 8], tiles: &tiles, }; // The loop-filter grids are only written when pixels are built, but // the walk's signature carries them. let mut grids = new_grids(mi_cols, mi_rows); let mut counts = crate::adapt::Counts::default(); let t_walk = crate::prof::start(); self.walk_frame(&job, &mut grids, &mut counts)?; crate::prof::stop(crate::prof::Phase::Walk, t_walk); // Backward adaptation (libvpx vp9_decode_frame): counts from this frame // adapt the stored context the frame started from. Skipped for // error-resilient and frame-parallel frames, which count nothing. if !hdr.error_resilient_mode && !hdr.frame_parallel_decoding_mode { let pre = self.frame_ctxs[hdr.frame_context_idx as usize].clone(); let factor = if self.last.key { COEF_MAX_UPDATE_FACTOR_AFTER_KEY } else { COEF_MAX_UPDATE_FACTOR }; adapt_coef_probs(&mut self.ctx, &pre, &counts, factor); adapt_mode_probs( &mut self.ctx, &pre, &counts, ch.tx_mode == TX_MODE_SELECT, interp_filter_of(hdr) == crate::inter::SWITCHABLE, ); adapt_mv_probs(&mut self.ctx, &pre, &counts, hdr.allow_high_precision_mv); } // The walk state is consumed below, so the block count the harness pins // has to be captured first. self.syntax_blocks = self.iw.as_ref().map_or(0, |w| w.blocks); let w = self.iw.take().expect("inter walk state"); self.prev_mvs = w.mvs; if hdr.segmentation.enabled { self.prev_seg = w.seg_map; } else { self.prev_seg = w.prev_seg; } self.last = LastFrameFacts { width: hdr.width, height: hdr.height, intra_only: hdr.intra_only, show_frame: hdr.show_frame, key: false, }; if hdr.refresh_frame_context { self.frame_ctxs[hdr.frame_context_idx as usize] = self.ctx.as_ref().clone(); } if crate::interdump_enabled() { eprintln!( "CHSUM frame={index} tx_mode={} ref_mode={} allow_hp={} interp={} tile_rows={} tile_cols={} pp12={:?} pp0={:?} pp13={:?}", ch.tx_mode, ch.reference_mode, hdr.allow_high_precision_mv, hdr.interpolation_filter as u8, hdr.tile_info.rows_log2, hdr.tile_info.cols_log2, self.ctx.partition[12], self.ctx.partition[0], self.ctx.partition[13] ); } if !pixels { return Ok(None); } // Loop filter post-pass: same extents as the keyframe path, but the // per-cell level comes from the block's own reference and mode class // (`get_filter_level`, vp9_loopfilter.c:249). if hdr.loop_filter.level > 0 && !crate::knobs::skip_lf() { let t_lf = crate::prof::start(); let p = self.planes.as_mut().expect("frame scratch"); let (vw, vh) = (mi_cols * 8, mi_rows * 8); grids.filter_frame( &mut p.y, p.ys, &mut p.u, &mut p.v, p.uvs, vw, vh, vw >> ss_x, vh >> ss_y, ss_x, ss_y, hdr.loop_filter.sharpness, hdr.bit_depth, ); crate::prof::stop(crate::prof::Phase::LoopFilter, t_lf); } let pic = self.take_picture(hdr); let frame = RefFrame { planes: std::sync::Arc::new(self.planes.take().expect("frame scratch")), width: hdr.width as u16, height: hdr.height as u16, }; // Reference refresh (spec 8.10 / `swap_frame_buffers`): bit i of // `refresh_frame_flags` replaces slot i with the frame just decoded — // the LOOP-FILTERED frame, which is what a later frame's prediction // reads. for slot in 0..8 { if hdr.refresh_frame_flags & (1 << slot) != 0 { let old = self.refs[slot].replace(frame.clone()); self.recycle(old); } } Ok(if hdr.show_frame { Some(pic) } else { None }) } /// Crop the frame scratch to a [`Picture`]. fn take_picture(&self, hdr: &FrameHeader) -> Picture { crop_picture( self.planes.as_ref().expect("frame scratch"), hdr.width as u16, hdr.height as u16, ) } /// One inter block's syntax (`read_inter_frame_mode_info` + the MV field /// `vp9_read_mode_info` stores for the next frame). #[allow(clippy::too_many_arguments)] fn read_inter_block_info( &mut self, r: &mut crate::bool::BoolDecoder, mi: &MiState, hdr: &FrameHeader, row: usize, col: usize, sb_type: usize, x_mis: usize, y_mis: usize, tile_col_start: usize, tx_mode: u8, counts: &mut crate::adapt::Counts, ) -> Result { // BLOCK_4X4/4X8/8X4 are the SPLIT/VERT/HORZ partitions of an 8x8. let partition = match sb_type { 0 => 3u8, 1 => 2u8, 2 => 1u8, _ => 0u8, }; let w = self.iw.as_mut().expect("inter walk state"); let prev_seg = std::mem::take(&mut w.prev_seg); let prev_mvs = std::mem::take(&mut w.prev_mvs); let ctx = crate::inter::InterBlockCtx { seg: &hdr.segmentation, ifs: w.ifs, sign_bias: w.sign_bias, allow_hp: hdr.allow_high_precision_mv, frame_interp: interp_filter_of(hdr), tx_mode, mi_rows: mi.mi_rows, mi_cols: mi.mi_cols, tile_col_start, tile_col_end: w.tile_col_end, last_seg: (!prev_seg.is_empty()).then_some(&prev_seg[..]), prev_mvs: (w.use_prev && !prev_mvs.is_empty()).then_some(&prev_mvs[..]), }; let info = crate::inter::read_inter_frame_mode_info( r, mi, &ctx, &self.ctx, row, col, sb_type, x_mis, y_mis, partition, &mut w.seg_map, counts, ); if let Ok(info) = &info { w.blocks += 1; for dy in 0..y_mis { for dx in 0..x_mis { let i = (row + dy) * mi.mi_cols + col + dx; w.mvs[i] = crate::inter::MvRef { ref_frame: info.ref_frame, mv: info.mv, }; } } if crate::interdump_enabled() { eprintln!( "MODE {} {} {} {} {} {} {} {} {} tpos=0 ref0={} ref1={} mvrow={} mvcol={} if={}", w.frame_index, row, col, sb_type, info.tx_size, info.mode, info.uv_mode, u8::from(info.skip), info.segment, info.ref_frame[0], info.ref_frame[1], info.mv[0].0, info.mv[0].1, info.interp_filter ); } } w.prev_seg = prev_seg; w.prev_mvs = prev_mvs; info } /// Tokens for one inter block: same entropy walk as the keyframe path, /// but no prediction and no residual add. `is_inter` selects the /// coefficient-probability ref axis. #[allow(clippy::too_many_arguments)] fn decode_inter_block_tokens( &mut self, r: &mut crate::bool::BoolDecoder, mi: &MiState, ectx: &mut [PlaneContexts; 3], hdr: &FrameHeader, info: &MiInfo, row: usize, col: usize, sb_type: usize, bw: usize, bh: usize, counts: &mut crate::adapt::Counts, ) -> Result { let (ss_x, ss_y) = plane_ss(hdr, 1); let n4w = [ plane_n4_w(bw, 0), plane_n4_w(bw, ss_x), plane_n4_w(bw, ss_x), ]; let n4h = [ plane_n4_h(bh, 0), plane_n4_h(bh, ss_y), plane_n4_h(bh, ss_y), ]; if info.skip { // dec_reset_skip_context reset_skip_context(ectx, row, col, sb_type, n4w, n4h, ss_x, ss_y); return Ok(0); } let mb_to_right = ((mi.mi_cols as isize - bw as isize - col as isize) * 64) as i32; let mb_to_bottom = ((mi.mi_rows as isize - bh as isize - row as isize) * 64) as i32; let mut eobtotal = 0usize; for plane in 0..3usize { let (ss_x, ss_y) = plane_ss(hdr, plane); let tx_size = if plane == 0 { info.tx_size } else { get_uv_tx_size(info.tx_size, sb_type, ss_x, ss_y) }; let step = 1usize << tx_size; let max_blocks_wide = (n4w[plane] as i32 + (mb_to_right.min(0) >> (5 + ss_x))).max(0) as usize; let max_blocks_high = (n4h[plane] as i32 + (mb_to_bottom.min(0) >> (5 + ss_y))).max(0) as usize; for brow in (0..max_blocks_high).step_by(step) { for bcol in (0..max_blocks_wide).step_by(step) { eobtotal += decode_inter_tx_tokens( r, ectx, hdr, info, &self.ctx, plane, row, col, brow, bcol, tx_size, sb_type, bw, bh, mb_to_right, mb_to_bottom, counts, ) .eob; } } } Ok(eobtotal) } /// `decode_block`'s inter arm with pixels (`vp9_decodeframe.c:990-1044`): /// `dec_build_inter_predictors_sb` for the whole block, then /// `reconstruct_inter_block` per transform block. Intra blocks inside an /// inter frame take the intra path (`:964-989`). #[allow(clippy::too_many_arguments)] fn reconstruct_inter_pixels( &mut self, r: &mut crate::bool::BoolDecoder, ectx: &mut [PlaneContexts; 3], grids: &mut LfGrids, hdr: &FrameHeader, info: &MiInfo, row: usize, col: usize, sb_type: usize, bw: usize, bh: usize, mi: &mut MiState, tile_col_start: usize, counts: &mut crate::adapt::Counts, ) -> Result { let (ss_x, ss_y) = plane_ss(hdr, 1); let x_mis = bw.min(mi.mi_cols - col); let y_mis = bh.min(mi.mi_rows - row); let n4w = [ plane_n4_w(bw, 0), plane_n4_w(bw, ss_x), plane_n4_w(bw, ss_x), ]; let n4h = [ plane_n4_h(bh, 0), plane_n4_h(bh, ss_y), plane_n4_h(bh, ss_y), ]; let mb_to_right = ((mi.mi_cols as isize - bw as isize - col as isize) * 64) as i32; let mb_to_bottom = ((mi.mi_rows as isize - bh as isize - row as isize) * 64) as i32; // `get_filter_level` (vp9_loopfilter.c:249): // `lvl[segment_id][mi->ref_frame[0]][mode_lf_lut[mi->mode]]`, with // `lvl` built by `vp9_loop_filter_frame_init` from the frame level, // the per-reference deltas and the per-mode-class deltas. let levels = hdr.loop_filter_levels(info.segment as usize); let ref0 = if info.is_inter { info.ref_frame[0].clamp(0, 3) as usize } else { // An intra block inside an inter frame: `mi->ref_frame[0]` is // INTRA_FRAME and its mode is an intra mode, so the mode class is // 0 and the reference slot is intra's. 0 }; let level = levels[ref0][mode_lf_lut(info.mode)]; for dy in 0..y_mis { for dx in 0..x_mis { let cell = (row + dy) * mi.mi_cols + col + dx; grids.level8[cell] = level; grids.blk[cell] = (sb_type as u8, row as u32, col as u32); grids.otx[cell] = if sb_type < 3 { TX_4X4 as u8 } else { info.tx_size as u8 }; // `build_masks`'s early return is gated on // `mi->skip && is_inter_block(mi)` — a skipped inter block // keeps ONLY the above/left prediction masks. grids.skip_inter[cell] = info.is_inter && info.skip; } } // `if (mi->skip) dec_reset_skip_context(xd);` — libvpx does this at the // top of `decode_block` (decodeframe.c:960), BEFORE the intra/inter // split, so skipped intra blocks inside an inter frame reset too. if info.skip { reset_skip_context(ectx, row, col, sb_type, n4w, n4h, ss_x, ss_y); } if !info.is_inter { // `predict_and_reconstruct_intra_block` per transform block. for plane in 0..3usize { let (ss_x, ss_y) = plane_ss(hdr, plane); let tx_size = if plane == 0 { info.tx_size } else { get_uv_tx_size(info.tx_size, sb_type, ss_x, ss_y) }; let step = 1usize << tx_size; let max_blocks_wide = (n4w[plane] as i32 + (mb_to_right.min(0) >> (5 + ss_x))).max(0) as usize; let max_blocks_high = (n4h[plane] as i32 + (mb_to_bottom.min(0) >> (5 + ss_y))).max(0) as usize; for brow in (0..max_blocks_high).step_by(step) { for bcol in (0..max_blocks_wide).step_by(step) { self.predict_and_reconstruct( r, ectx, hdr, info, plane, row, col, brow, bcol, tx_size, sb_type, bw, bh, mb_to_right, mb_to_bottom, // Intra prediction's left availability is // tile-scoped (`tile->mi_col_start`), which the // inter walk passes as the tile's first mi col. tile_col_start, counts, )?; } } } return Ok(false); } // Prediction: `dec_build_inter_predictors_sb`, every reference, every // plane, before any token is read. { let t_mc = crate::prof::start(); let planes = self.planes.as_mut().expect("frame scratch"); let refs = &self.refs; let scratch = &mut self.mc_scratch; let temp = &mut self.mc_temp; for plane in 0..3usize { inter_predict_plane( planes, refs, scratch, temp, hdr, info, plane, row, col, &n4w, &n4h, mb_to_right, mb_to_bottom, sb_type, )?; } crate::prof::stop(crate::prof::Phase::Mc, t_mc); } if info.skip { return Ok(false); } // Reconstruction: `reconstruct_inter_block` per transform block. let mut eobtotal = 0usize; for plane in 0..3usize { let (ss_x, ss_y) = plane_ss(hdr, plane); let tx_size = if plane == 0 { info.tx_size } else { get_uv_tx_size(info.tx_size, sb_type, ss_x, ss_y) }; let step = 1usize << tx_size; let max_blocks_wide = (n4w[plane] as i32 + (mb_to_right.min(0) >> (5 + ss_x))).max(0) as usize; let max_blocks_high = (n4h[plane] as i32 + (mb_to_bottom.min(0) >> (5 + ss_y))).max(0) as usize; for brow in (0..max_blocks_high).step_by(step) { for bcol in (0..max_blocks_wide).step_by(step) { let t_tok = crate::prof::start(); let coef = decode_inter_tx_tokens( r, ectx, hdr, info, &self.ctx, plane, row, col, brow, bcol, tx_size, sb_type, bw, bh, mb_to_right, mb_to_bottom, counts, ); crate::prof::stop(crate::prof::Phase::Tokens, t_tok); eobtotal += coef.eob; if coef.eob == 0 { continue; } // `inverse_transform_block_inter`: inter blocks always use // the DEFAULT scan and DCT_DCT (`reconstruct_inter_block`, // decodeframe.c:413). let planes = self.planes.as_mut().expect("frame scratch"); let stride = planes.dims(plane).0; let bd = planes.bd; let x0 = ((col * 8) >> ss_x) + bcol * 4; let y0 = ((row * 8) >> ss_y) + brow * 4; let dst_off = y0 * stride + x0; let (data, _) = planes.plane(plane); let t_idct = crate::prof::start(); inverse_transform_add( tx_size, DCT_DCT, &coef.coeffs, data, dst_off, stride, hdr.quantization.lossless(), bd, ); crate::prof::stop(crate::prof::Phase::Idct, t_idct); } } } // `if (!less8x8 && eobtotal == 0) mi->skip = 1; // skip loopfilter` // (decodeframe.c:1043). if sb_type >= 3 && eobtotal == 0 { for dy in 0..y_mis { for dx in 0..x_mis { grids.skip_inter[(row + dy) * mi.mi_cols + col + dx] = true; } } // The same assignment is visible to later blocks' skip and tx // contexts through the mode-info grid. return Ok(true); } Ok(false) } fn decode_keyframe(&mut self, hdr: &FrameHeader, frame: &[u8], tx_mode: u8) -> Result { let ss_x = hdr.subsampling_x as usize; let ss_y = hdr.subsampling_y as usize; let width = hdr.width as usize; let height = hdr.height as usize; // The decode buffer is padded to whole superblocks: libvpx decodes a // block at the frame edge at its FULL size (decodeframe.c:1216, // `PARTITION_NONE` needs only `mi_row < mi_rows`) and writes the // overhang into the frame buffer's border. Clipping to 8 pixels // instead panicked on real 1080p content (a 32x32 block starting at // mi_row 132 writes 8 rows past a 1080-row buffer). let aw = width.div_ceil(64) * 64; let ah = height.div_ceil(64) * 64; self.planes = Some(self.frame_planes(aw, ah, hdr.bit_depth, ss_x, ss_y)); let tail_start = crate::knobs::force_tail() .unwrap_or_else(|| { hdr.uncompressed_header_size as usize + hdr.header_size_in_bytes as usize }); let tail: Vec = frame[tail_start..].to_vec(); self.decode_keyframe_inner(hdr, tx_mode, &tail) } fn decode_keyframe_inner( &mut self, hdr: &FrameHeader, tx_mode: u8, tail: &[u8], ) -> Result { let ss_x = hdr.subsampling_x as usize; let ss_y = hdr.subsampling_y as usize; let mi_cols = hdr.mi_cols() as usize; let mi_rows = hdr.mi_rows() as usize; let tile_cols = 1usize << hdr.tile_info.cols_log2; let tile_rows = 1usize << hdr.tile_info.rows_log2; let tiles = split_tiles(tail, tile_cols, tile_rows)?; // Keyframe loop filter levels: intra ref, no mode class, per // segment (spec 8.8.1). let mut level_of_seg = [[0u8; 2]; 8]; for (seg, slot) in level_of_seg.iter_mut().enumerate() { *slot = hdr.loop_filter_levels(seg)[0]; } let job = TileJob { hdr, tx_mode, pixels: true, level_of_seg, tiles: &tiles, }; let mut grids = new_grids(mi_cols, mi_rows); let mut counts = crate::adapt::Counts::default(); self.walk_frame(&job, &mut grids, &mut counts)?; // Intra frames adapt the coefficient tables only (libvpx: mode, skip and // MV tables are adapted for inter frames alone), from the context the // frame started from: slot 0, which a key or intra-only frame resets. if !hdr.error_resilient_mode && !hdr.frame_parallel_decoding_mode { let pre = self.frame_ctxs[0].clone(); adapt_coef_probs(&mut self.ctx, &pre, &counts, COEF_MAX_UPDATE_FACTOR); } // Loop filter post-pass (level 0 disables it for the frame). // The extents are the MI-aligned VISIBLE plane (libvpx's masks clear // everything past `mi_cols`/`mi_rows`), not the padded buffer size. if hdr.loop_filter.level > 0 && !crate::knobs::skip_lf() { let t_lf = crate::prof::start(); let p = self.planes.as_mut().expect("frame scratch"); let (vw, vh) = (mi_cols * 8, mi_rows * 8); grids.filter_frame( &mut p.y, p.ys, &mut p.u, &mut p.v, p.uvs, vw, vh, vw >> ss_x, vh >> ss_y, ss_x, ss_y, hdr.loop_filter.sharpness, hdr.bit_depth, ); crate::prof::stop(crate::prof::Phase::LoopFilter, t_lf); } // `vp9_swap_current_and_last_seg_map` runs only when segmentation is // enabled: this frame's map becomes the next frame's prediction map. if hdr.segmentation.enabled { self.prev_seg = std::mem::take(&mut grids.seg); } Ok(self.take_picture(hdr)) } /// `decode_partition` (decodeframe.c:1173). #[allow(clippy::too_many_arguments)] fn decode_partition( &mut self, r: &mut crate::bool::BoolDecoder, mi: &mut MiState, ectx: &mut [PlaneContexts; 3], grids: &mut LfGrids, level_of_seg: &[[u8; 2]; 8], hdr: &FrameHeader, row: usize, col: usize, n4: usize, tile_col_start: usize, tx_mode: u8, // Whether this walk reconstructs pixels. Consulted by the inter // branch only: a keyframe always reconstructs its intra blocks, and // the syntax-only inter walk parses tokens without predicting. pixels: bool, counts: &mut crate::adapt::Counts, ) -> Result<()> { if row >= mi.mi_rows || col >= mi.mi_cols { return Ok(()); } let n8 = n4 - 1; let hbs = (1usize << n8) >> 1; let has_rows = row + hbs < mi.mi_rows; let has_cols = col + hbs < mi.mi_cols; // libvpx reads the partition symbol at EVERY level INCLUDING 8x8 // (decodeframe.c:1188 runs before the `!hbs` branch): the 8x8 // symbol picks 8x8 / 8x4 / 4x8 / 4x4-split — the sub-8x8 shapes. // Only the 4x4 level has no symbol, and SPLIT recursion never // reaches it (it stops at n4 == 1). let partition = { let ctx = mi.partition_ctx(row, col, n8 as u32); let probs = &self.ctx.partition[ctx]; let p = if has_rows && has_cols { r.read_tree(&PARTITION_TREE, probs) } else if !has_rows && has_cols { if r.read_bool(probs[1]) { 3 } else { 1 } } else if has_rows && !has_cols { if r.read_bool(probs[2]) { 3 } else { 2 } } else { 3 }; // libvpx counts the implied SPLIT too (`++counts->partition[ctx][p]`). counts.partition[ctx][p as usize] += 1; p }; let bsize = bsize_of_n4(n4); let subsize = SUBSIZE_LOOKUP[partition as usize * 13 + bsize] as usize; if hbs == 0 { self.decode_block( r, mi, ectx, grids, level_of_seg, hdr, row, col, subsize, 1, 1, tile_col_start, tx_mode, pixels, counts, )?; } else { match partition { 0 => self.decode_block( r, mi, ectx, grids, level_of_seg, hdr, row, col, subsize, n4, n4, tile_col_start, tx_mode, pixels, counts, )?, 1 => { self.decode_block( r, mi, ectx, grids, level_of_seg, hdr, row, col, subsize, n4, n8, tile_col_start, tx_mode, pixels, counts, )?; if has_rows { self.decode_block( r, mi, ectx, grids, level_of_seg, hdr, row + hbs, col, subsize, n4, n8, tile_col_start, tx_mode, pixels, counts, )?; } } 2 => { self.decode_block( r, mi, ectx, grids, level_of_seg, hdr, row, col, subsize, n8, n4, tile_col_start, tx_mode, pixels, counts, )?; if has_cols { self.decode_block( r, mi, ectx, grids, level_of_seg, hdr, row, col + hbs, subsize, n8, n4, tile_col_start, tx_mode, pixels, counts, )?; } } _ => { for (rr, cc) in [ (row, col), (row, col + hbs), (row + hbs, col), (row + hbs, col + hbs), ] { self.decode_partition( r, mi, ectx, grids, level_of_seg, hdr, rr, cc, n8, tile_col_start, tx_mode, pixels, counts, )?; } } } } if bsize >= 3 && (bsize == 3 || partition != 3) { mi.update_partition_ctx(row, col, subsize, 1 << n8); } Ok(()) } /// `decode_block` intra path (decodeframe.c:913). #[allow(clippy::too_many_arguments)] fn decode_block( &mut self, r: &mut crate::bool::BoolDecoder, mi: &mut MiState, ectx: &mut [PlaneContexts; 3], grids: &mut LfGrids, level_of_seg: &[[u8; 2]; 8], hdr: &FrameHeader, row: usize, col: usize, sb_type: usize, bwl: usize, bhl: usize, tile_col_start: usize, tx_mode: u8, pixels: bool, counts: &mut crate::adapt::Counts, ) -> Result<()> { let bw = 1 << (bwl - 1); let bh = 1 << (bhl - 1); let x_mis = bw.min(mi.mi_cols - col); let y_mis = bh.min(mi.mi_rows - row); // `decode_block` (decodeframe.c:942): a block size that does not // exist once subsampled (`BLOCK_8X4` under 4:4:0, `BLOCK_4X8` under // 4:2:2) is a corrupt frame. Sub-8x8 sizes have no `ss_size_lookup` // row to check and are exempt. if sb_type >= 3 && (hdr.subsampling_x != 0 || hdr.subsampling_y != 0) { ensure( ss_size( sb_type, hdr.subsampling_x as usize, hdr.subsampling_y as usize, ) != BLOCK_INVALID, "invalid block size for the chroma subsampling", )?; } // `frame_is_intra_only` (onyxc_int.h:363): a keyframe OR an // intra-only frame walks the intra mode-info path. let intra_frame = hdr.frame_type == FrameType::Key || hdr.intra_only; let mut info = if intra_frame { read_intra_frame_mode_info( r, mi, &hdr.segmentation, row, col, sb_type, tile_col_start, tx_mode, &self.ctx, )? } else { self.read_inter_block_info( r, mi, hdr, row, col, sb_type, x_mis, y_mis, tile_col_start, tx_mode, counts, )? }; if !intra_frame { if pixels { // Full inter decode: prediction, tokens, residual. The // per-cell loop-filter level is the block's own reference and // mode class (`get_filter_level`), so the grids are written // inside the reconstruction rather than from `level_of_seg`. let became_skip = self.reconstruct_inter_pixels( r, ectx, grids, hdr, &info, row, col, sb_type, bw, bh, mi, tile_col_start, counts, )?; if became_skip { info.skip = true; } } else { // Inter syntax only: tokens keep the entropy stream aligned, // the block lands in the mode grid, and no pixel is touched. let eobtotal = self.decode_inter_block_tokens(r, mi, ectx, hdr, &info, row, col, sb_type, bw, bh, counts)?; // libvpx decode_block: an inter block of 8x8 or larger with no // coefficients becomes skip (`mi->skip = 1`), which the skip // and tx contexts of later blocks then read. if sb_type >= 3 && !info.skip && eobtotal == 0 { info.skip = true; } } for dy in 0..y_mis { for dx in 0..x_mis { if crate::mvdbg2_enabled() && row + dy == 7 && col + dx >= 30 && col + dx <= 40 { eprintln!( "GWR cell=({} {}) idx={} from=({row} {col}) dy={dy} dx={dx} sb={sb_type} bw={bw} bh={bh} x_mis={x_mis} y_mis={y_mis} mv={:?} ref0={} mi_cols={}", row + dy, col + dx, (row + dy) * mi.mi_cols + col + dx, info.mv, info.ref_frame[0], mi.mi_cols ); } mi.grid[(row + dy) * mi.mi_cols + col + dx] = Some(info.clone()); } } return Ok(()); } // Per-plane 4x4 counts. Sub-8x8 shapes still occupy the WHOLE 8x8 // mi area (libvpx set_plane_n4 with bw=bh=1): luma walks 2x2 4x4 // TUs, chroma is one 4x4 (n4 >> subsampling), coded once per 8x8. let (ss_x, ss_y) = plane_ss(hdr, 1); let n4w = [ plane_n4_w(bw, 0), plane_n4_w(bw, ss_x), plane_n4_w(bw, ss_x), ]; let n4h = [ plane_n4_h(bh, 0), plane_n4_h(bh, ss_y), plane_n4_h(bh, ss_y), ]; let mb_to_right = ((mi.mi_cols as isize - bw as isize - col as isize) * 64) as i32; let mb_to_bottom = ((mi.mi_rows as isize - bh as isize - row as isize) * 64) as i32; let level = level_of_seg[info.segment as usize][0]; for dy in 0..y_mis { for dx in 0..x_mis { let cell = (row + dy) * mi.mi_cols + col + dx; grids.level8[cell] = level; grids.seg[cell] = info.segment; grids.blk[cell] = (sb_type as u8, row as u32, col as u32); grids.otx[cell] = if sb_type < 3 { TX_4X4 as u8 } else { info.tx_size as u8 }; } } if info.skip { for plane in 0..3usize { let (sx, sy) = if plane == 0 { (0, 0) } else { (ss_x, ss_y) }; let offx = (col << 1) >> sx; let offy = (row << 1) >> sy; for i in 0..n4w[plane] { ectx[plane].above[offx + i] = 0; } let lrows = 32usize >> sy; for i in 0..n4h[plane].min(lrows) { ectx[plane].left[(offy + i) % lrows] = 0; } } } for plane in 0..3usize { let (ss_x, ss_y) = plane_ss(hdr, plane); let tx_size = if plane == 0 { info.tx_size } else { get_uv_tx_size(info.tx_size, sb_type, ss_x, ss_y) }; let step = 1usize << tx_size; let max_blocks_wide = (n4w[plane] as i32 + (mb_to_right.min(0) >> (5 + ss_x))).max(0) as usize; let max_blocks_high = (n4h[plane] as i32 + (mb_to_bottom.min(0) >> (5 + ss_y))).max(0) as usize; for brow in (0..max_blocks_high).step_by(step) { for bcol in (0..max_blocks_wide).step_by(step) { self.predict_and_reconstruct( r, ectx, hdr, &info, plane, row, col, brow, bcol, tx_size, sb_type, bw, bh, mb_to_right, mb_to_bottom, tile_col_start, counts, )?; } } } for dy in 0..y_mis { for dx in 0..x_mis { mi.grid[(row + dy) * mi.mi_cols + col + dx] = Some(info.clone()); } } Ok(()) } /// `predict_and_reconstruct_intra_block` (decodeframe.c:309): /// predict, then tokens, then residual add; plus /// `vp9_decode_block_tokens` context bookkeeping (the v1.15 edge /// shifts included). #[allow(clippy::too_many_arguments)] fn predict_and_reconstruct( &mut self, r: &mut crate::bool::BoolDecoder, ectx: &mut [PlaneContexts; 3], hdr: &FrameHeader, info: &MiInfo, plane: usize, mi_row: usize, mi_col: usize, tx_row: usize, tx_col: usize, tx_size: usize, sb_type: usize, bw: usize, bh: usize, mb_to_right: i32, mb_to_bottom: i32, tile_col_start: usize, counts: &mut crate::adapt::Counts, ) -> Result<()> { let (ss_x, ss_y) = plane_ss(hdr, plane); let mode = if plane == 0 { if sb_type < 3 { info.bmi[(tx_row << 1) + tx_col] } else { info.mode } } else { info.uv_mode }; let planes = self.planes.as_mut().expect("frame scratch"); let stride = planes.dims(plane).0; let bd = planes.bd; let bs = 4 << tx_size; let x0 = ((mi_col * 8) >> ss_x) + tx_col * 4; let y0 = ((mi_row * 8) >> ss_y) + tx_row * 4; // vp9_predict_intra_block `have_top = loff || (xd->above_mi != NULL)` // (vp9_reconintra.c:411); `above_mi` is FRAME-gated // (`set_mi_row_col`, vp9_onyxc_int.h:430: `mi_row != 0`), NOT // tile-row-gated. The above row of a tile row k > 0 is decoded by // tile row k - 1 and is available. let up = tx_row != 0 || mi_row > 0; let lft = tx_col != 0 || mi_col > tile_col_start; // vp9_predict_intra_block `have_right`: the tx block is not in the // last tx column of ITS OWN block (pd->n4_w units) — above-right // past the block's right edge belongs to a not-yet-decoded // neighbour, so it must stage as unavailable. let rgt = tx_col + (1usize << tx_size) < ((bw * 2) >> ss_x); // `xd->cur_buf->y_width/y_height` (vp9_reconintra.c:288-292) are the // MI-ALIGNED (8-multiple) buffer extent, not the visible coded size: // confirmed by instrumenting the oracle's staging for a 320x242 frame // (`fh=248`, not 242). The extend-vs-direct DECISION is `mb_to_*_edge` // (`:302/326/354`, passed in as `bot_ext`/`right_ext`). let vw = (hdr.mi_cols() as usize * 8) >> ss_x; let vh = (hdr.mi_rows() as usize * 8) >> ss_y; // 1. Intra prediction (reads reconstructed neighbours). { let (data, _) = planes.plane(plane); build_intra_predictors( data, stride, vw, vh, x0, y0, mode, bs, up, lft, rgt, mb_to_bottom < 0, mb_to_right < 0, bd, ); } if info.skip { return Ok(()); } // 2. Tokens. let lossless = hdr.quantization.lossless(); let tx_type = if plane != 0 || lossless { DCT_DCT } else { MODE_TO_TXTYPE_LOOKUP[mode as usize] as usize }; let (scan, nb) = scan_for(tx_size, tx_type); let q = hdr.segment_dequant(info.segment as usize); let dequant: [i16; 2] = if plane == 0 { [q.luma_dc, q.luma_ac] } else { [q.chroma_dc, q.chroma_ac] }; let ptype = usize::from(plane != 0); let ax = ((mi_col << 1) >> ss_x) + tx_col; let lrows = 32usize >> ss_y; let ay = (((mi_row << 1) >> ss_y) + tx_row) % lrows; // libvpx reads the context as !!*(uintN_t *)a / l over the WHOLE // tx-block span (uint16 for 8x8, uint32 for 16x16, uint64 for // 32x32), not just the first entry. let nblocks = 1usize << tx_size; let ctx_in = usize::from(ectx[plane].above[ax..ax + nblocks].iter().any(|&v| v != 0)) + usize::from(ectx[plane].left[ay..ay + nblocks].iter().any(|&v| v != 0)); if crate::trace_enabled() { eprintln!( "CTX p={} prob={:?} ax={} ay={} a={} l={} ctx={}", plane, self.ctx.coef[tx_size][ptype][0][0][ctx_in], ax, ay, ectx[plane].above[ax], ectx[plane].left[ay], ctx_in ); } if crate::trace_enabled() { eprintln!( "TXB p={} x={} y={} tx={} a={} l={} deq={},{}", plane, x0, y0, tx_size, ectx[plane].above[ax], ectx[plane].left[ay], dequant[0], dequant[1] ); } let coef = decode_coefs( r, ptype, tx_size, &dequant, ctx_in, scan, nb, &self.ctx.coef[tx_size], false, bd, (x0, y0), plane, counts, ); // 3. Context write-back (vp9_decode_block_tokens, v1.15 shifts). let n4w = (bw * 2) >> ss_x; let n4h = (bh * 2) >> ss_y; let maxw = (n4w as i32 + (mb_to_right.min(0) >> (5 + ss_x))).max(0) as usize; let maxh = (n4h as i32 + (mb_to_bottom.min(0) >> (5 + ss_y))).max(0) as usize; let nblocks = 1usize << tx_size; let shift_a = if maxw > 0 && nblocks + tx_col > maxw { (nblocks - (maxw - tx_col)) * 8 } else { 0 }; let shift_l = if maxh > 0 && nblocks + tx_row > maxh { (nblocks - (maxh - tx_row)) * 8 } else { 0 }; write_tx_context(ectx, plane, tx_size, ax, ay, coef.eob > 0, shift_a, shift_l); if crate::tokdbg_enabled() { eprintln!( "TDW row={mi_row} col={mi_col} plane={plane} brow={tx_row} bcol={tx_col} tx={tx_size} eob={} sa={} sl={} aw={:?} lw={:?} intra=1", coef.eob, shift_a, shift_l, &ectx[plane].above[ax..ax + nblocks], &ectx[plane].left[ay..ay + nblocks], ); } // 4. Residual add. if coef.eob > 0 { let dst_off = y0 * stride + x0; let (data, _) = planes.plane(plane); let probe = crate::trace_enabled() && plane == 0 && std::env::var("EC_VP9_PROBE_TU") .ok() .and_then(|s| { let mut it = s.split(','); Some((it.next()?.parse::().ok()?, it.next()?.parse().ok()?)) }) .is_some_and(|(px, py)| x0 == px && y0 == py); let t: Option>> = if probe { Some( (0..4usize) .map(|rr| data[dst_off + rr * stride..dst_off + rr * stride + 4].to_vec()) .collect(), ) } else { None }; inverse_transform_add( tx_size, tx_type, &coef.coeffs, data, dst_off, stride, lossless, bd, ); if let Some(before) = t { let after: Vec> = (0..4usize) .map(|rr| data[dst_off + rr * stride..dst_off + rr * stride + 4].to_vec()) .collect(); eprintln!("RC x0={x0} y0={y0} tx={tx_size} mode={mode}"); for rr in 0..4 { eprintln!(" pred{rr}={:?} after{rr}={:?}", before[rr], after[rr]); } } } Ok(()) } } impl Default for Decoder { fn default() -> Self { Self::new() } } /// `BLOCK_SIZE` from `log2(4x4s per side)`. fn bsize_of_n4(n4: usize) -> usize { match n4 { 4 => 12, // BLOCK_64X64 3 => 9, // BLOCK_32X32 2 => 6, // BLOCK_16X16 1 => 3, // BLOCK_8X8 _ => 0, // BLOCK_4X4 (recursion never reaches n4 == 0) } } /// Crop an aligned plane set to a displayable [`Picture`]: rows are packed to /// the visible width, so `stride` describes the RETURNED buffers, not the /// aligned decode buffer. fn crop_picture(planes: &Planes, width: u16, height: u16) -> Picture { let (w, h) = (width as usize, height as usize); let crop = |src: &[Sample], stride: usize, cw: usize, ch: usize| -> Vec { let mut out = Vec::with_capacity(cw * ch); for row in 0..ch { out.extend_from_slice(&src[row * stride..row * stride + cw]); } out }; // Chroma CEILS: a subsampled plane of an odd extent carries one extra // half-sample column/row (`uv_crop_width = (w + ss_x) >> ss_x`). Under // 4:4:4 the chroma plane is the full luma extent. let (sx, sy) = (planes.ss_x, planes.ss_y); let (cw, chh) = ((w + sx) >> sx, (h + sy) >> sy); Picture { y: crop(&planes.y, planes.ys, w, h), u: crop(&planes.u, planes.uvs, cw, chh), v: crop(&planes.v, planes.uvs, cw, chh), width, height, stride: w, uv_stride: cw, bit_depth: planes.bd, subsampling_x: sx as u8, subsampling_y: sy as u8, } } /// `(ss_x, ss_y)` of a plane: luma is always full-resolution. #[inline] fn plane_ss(hdr: &FrameHeader, plane: usize) -> (usize, usize) { if plane == 0 { (0, 0) } else { (hdr.subsampling_x as usize, hdr.subsampling_y as usize) } } /// `pd->n4_w` (`set_plane_n4`, decodeframe.c:809): the plane block's width in /// its own 4x4 units, `(bw << 1) >> ss_x`. Sub-8x8 shapes occupy a whole 8x8 /// mi cell (`set_plane_n4(xd, bw=1, bh=1)`), so luma walks 2x2 and chroma /// `(2 >> ss_x)` wide. fn plane_n4_w(bw: usize, ss_x: usize) -> usize { (bw << 1) >> ss_x } /// `pd->n4_h`, the height twin of [`plane_n4_w`]. fn plane_n4_h(bh: usize, ss_y: usize) -> usize { (bh << 1) >> ss_y } /// `dec_reset_skip_context` (`vp9_decodeframe.c:800`). #[allow(clippy::too_many_arguments)] fn reset_skip_context( ectx: &mut [PlaneContexts; 3], row: usize, col: usize, sb_type: usize, n4w: [usize; 3], n4h: [usize; 3], ss_x: usize, ss_y: usize, ) { let _ = sb_type; for plane in 0..3usize { let (sx, sy) = if plane == 0 { (0, 0) } else { (ss_x, ss_y) }; let offx = (col << 1) >> sx; let offy = (row << 1) >> sy; for i in 0..n4w[plane] { ectx[plane].above[offx + i] = 0; } let lrows = 32usize >> sy; for i in 0..n4h[plane].min(lrows) { ectx[plane].left[(offy + i) % lrows] = 0; } } if crate::tokdbg_enabled() { // Mirror the oracle's `SKIPCTX` print: the CLEARED window, indexed from // the reset offset (libvpx's `left_context` is already offset to the // block), not the whole rolling buffer from base index 0. let mut line = format!("TDK row={row} col={col} n4w={n4w:?} n4h={n4h:?}"); for plane in 0..3usize { let (sx, sy) = if plane == 0 { (0, 0) } else { (ss_x, ss_y) }; let offx = (col << 1) >> sx; let offy = (row << 1) >> sy; let lrows = 32usize >> sy; let a: Vec = (0..n4w[plane]) .map(|i| ectx[plane].above[offx + i]) .collect(); let l: Vec = (0..n4h[plane].min(lrows)) .map(|i| ectx[plane].left[(offy + i) % lrows]) .collect(); line.push_str(&format!(" p{plane} a={a:?} l={l:?}")); } eprintln!("{line}"); } } /// The entropy-context write-back of one transform block /// (`vp9_decode_block_tokens`, `vp9_detokenize.c:273-323`): the block's /// `eob` spreads over its `1 << tx_size` 4x4 slots, and `shift_a`/`shift_l` /// (the `get_ctx_shift` frame-edge rule) zero the slots past the frame edge. /// /// libvpx applies the shift to a value whose WIDTH IS THE WINDOW'S — a /// `(uint16_t)` for `TX_8X8`, `(uint32_t)` for `TX_16X16`, `(uint64_t)` for /// `TX_32X32` — so the shift zeroes the window's TAIL. Shifting a 64-bit /// spread instead only zeroes bytes past the window and leaves the in-window /// tail set: the last-SB-row chroma blocks then hand a stale 1 to the next /// row's context read (the frame-72 desync). fn write_tx_context( ectx: &mut [PlaneContexts; 3], plane: usize, tx_size: usize, ax: usize, ay: usize, eob: bool, shift_a: usize, shift_l: usize, ) { const ONES: u64 = 0x0101_0101_0101_0101; let nblocks = 1usize << tx_size; let eob_pos = u64::from(eob); // Narrow to the window first (libvpx's cast), then apply the edge shift; // the clamp keeps a fully-outside window (libvpx: undefined shift) total. let narrow = 64 - 8 * nblocks; let spread_a = ((eob_pos * ONES) >> narrow) >> shift_a.min(8 * nblocks); let spread_l = ((eob_pos * ONES) >> narrow) >> shift_l.min(8 * nblocks); for k in 0..nblocks { ectx[plane].above[ax + k] = (spread_a >> (8 * k)) as u8; ectx[plane].left[ay + k] = (spread_l >> (8 * k)) as u8; } } /// `mode_lf_lut` (`vp9/common/vp9_loopfilter.c:223`): /// `{0 × INTRA_MODES, 1, 1, 0, 1}` — NEARESTMV, NEARMV and NEWMV take the /// inter mode delta, ZEROMV does not, and intra modes never do. fn mode_lf_lut(mode: u8) -> usize { match mode { 10 | 11 | 13 => 1, _ => 0, } } /// One inter transform block's tokens plus the context write-back /// (`vp9_decode_block_tokens`, `decodeframe.c:414`). Inter blocks always use /// the DEFAULT scan and `DCT_DCT`. #[allow(clippy::too_many_arguments)] fn decode_inter_tx_tokens( r: &mut crate::bool::BoolDecoder, ectx: &mut [PlaneContexts; 3], hdr: &FrameHeader, info: &MiInfo, fc: &FrameContext, plane: usize, row: usize, col: usize, brow: usize, bcol: usize, tx_size: usize, sb_type: usize, bw: usize, bh: usize, mb_to_right: i32, mb_to_bottom: i32, counts: &mut crate::adapt::Counts, ) -> crate::tokens::CoeffBlock { let (ss_x, ss_y) = plane_ss(hdr, plane); // `predict_and_reconstruct_intra_block` (decodeframe.c:320-345): the luma // mode of a sub-8x8 block is the SUB-BLOCK's, and an intra block inside an // inter frame picks its tx type from that mode — `reconstruct_inter_block` // hard-codes DCT_DCT instead, which is why the inter guard is here. let tx_type = if plane != 0 || hdr.quantization.lossless() || info.is_inter { DCT_DCT } else { let mode = if sb_type < 3 { info.bmi[(brow << 1) + bcol] } else { info.mode }; MODE_TO_TXTYPE_LOOKUP[mode as usize] as usize }; let (scan, nb) = scan_for(tx_size, tx_type); let q = hdr.segment_dequant(info.segment as usize); let dequant: [i16; 2] = if plane == 0 { [q.luma_dc, q.luma_ac] } else { [q.chroma_dc, q.chroma_ac] }; let ptype = usize::from(plane != 0); let ax = ((col << 1) >> ss_x) + bcol; let lrows = 32usize >> ss_y; let ay = (((row << 1) >> ss_y) + brow) % lrows; let nblocks = 1usize << tx_size; let ctx_in = usize::from(ectx[plane].above[ax..ax + nblocks].iter().any(|&v| v != 0)) + usize::from(ectx[plane].left[ay..ay + nblocks].iter().any(|&v| v != 0)); if crate::tokdbg_enabled() { eprintln!( "TDB row={row} col={col} plane={plane} brow={brow} bcol={bcol} tx={tx_size} sbt={sb_type} ptype={ptype} inter={} mode={} tt={tx_type} ax={ax} ay={ay} cin={ctx_in} a={:?} l={:?}", u8::from(info.is_inter), info.mode, &ectx[plane].above[ax..ax + nblocks], &ectx[plane].left[ay..ay + nblocks], ); } let coef = decode_coefs( r, ptype, tx_size, &dequant, ctx_in, scan, nb, &fc.coef[tx_size], info.is_inter, hdr.bit_depth, (0, 0), plane, counts, ); // vp9_decode_block_tokens context write-back (v1.15 edge shifts). let n4w_s = (bw * 2) >> ss_x; let n4h_s = (bh * 2) >> ss_y; let maxw = (n4w_s as i32 + (mb_to_right.min(0) >> (5 + ss_x))).max(0) as usize; let maxh = (n4h_s as i32 + (mb_to_bottom.min(0) >> (5 + ss_y))).max(0) as usize; let shift_a = if maxw > 0 && nblocks + bcol > maxw { (nblocks - (maxw - bcol)) * 8 } else { 0 }; let shift_l = if maxh > 0 && nblocks + brow > maxh { (nblocks - (maxh - brow)) * 8 } else { 0 }; write_tx_context(ectx, plane, tx_size, ax, ay, coef.eob > 0, shift_a, shift_l); if crate::tokdbg_enabled() { eprintln!( "TDW row={row} col={col} plane={plane} brow={brow} bcol={bcol} tx={tx_size} eob={} sa={} sl={} aw={:?} lw={:?}", coef.eob, shift_a, shift_l, &ectx[plane].above[ax..ax + nblocks], &ectx[plane].left[ay..ay + nblocks], ); } coef } /// `dec_build_inter_predictors_sb` (`vp9_decodeframe.c:731`) for one plane: /// every reference's prediction for one block, in reference order (compound /// blocks average the second into the first). #[allow(clippy::too_many_arguments)] fn inter_predict_plane( planes: &mut Planes, refs: &[Option; 8], scratch: &mut Vec, temp: &mut Vec, hdr: &FrameHeader, info: &MiInfo, plane: usize, row: usize, col: usize, n4w: &[usize; 3], n4h: &[usize; 3], mb_to_right: i32, mb_to_bottom: i32, sb_type: usize, ) -> Result<()> { let (ss_x, ss_y) = plane_ss(hdr, plane); let pw = n4w[plane] * 4; let ph = n4h[plane] * 4; let mb_left = -((col as i32) * 64); let mb_top = -((row as i32) * 64); let is_compound = info.ref_frame[1] > crate::inter::INTRA_FRAME; let refs_used = 1 + usize::from(is_compound); let kernel = usize::from(info.interp_filter); let dst_stride = planes.dims(plane).0; for rf in 0..refs_used { let ref_name = info.ref_frame[rf]; let slot = hdr.ref_frame_idx[(ref_name - 1) as usize] as usize; let Some(ref_frame) = refs[slot].as_ref() else { return Err(corrupt("vp9 inter prediction read an empty reference slot")); }; let (ref_data, ref_stride) = ref_frame.planes.plane_ref(plane); // `vp9_setup_scale_factors_for_frame` (`vp9_decodeframe.c:2758`): the // scale from the REFERENCE's coded luma size to this frame's, shared // by every plane. `vp9_is_valid_scale` is enforced where libvpx // enforces it — at prediction time, for the references actually used. let sf = ScaleFactors::setup( ref_frame.width as usize, ref_frame.height as usize, hdr.width as usize, hdr.height as usize, ); if !sf.is_valid() { return Err(Error::unsupported( "vp9 reference scale", format!( "reference `{ref_name}` in slot {slot} is {}x{} against a {}x{} frame; \ `valid_ref_frame_size` rejects that ratio", ref_frame.width, ref_frame.height, hdr.width, hdr.height ), )); } let refp = RefPlane { data: ref_data, stride: ref_stride, // `y_crop_width` / `uv_crop_width`: luma is the coded size, chroma // CEILS (`(w + 1) / 2`). libvpx stores `uv_crop_width` as // `(y_crop_width + 1) >> 1` (`vpx_scale/yv12config.c`), so a // plain `>> 1` is one short whenever the coded extent is odd and // the predictor's border/inside test then clips the last chroma // column (or row). width: (ref_frame.width as usize + ss_x) >> ss_x, height: (ref_frame.height as usize + ss_y) >> ss_y, bd: ref_frame.planes.bd, }; if sb_type < 3 { // libvpx builds one 4x4 prediction per sub-block, with the MV from // `average_split_mvs` selected by subsampling: luma and 4:4:4 // chroma take the sub-block's own MV, 4:4:0 averages the vertical // pair, 4:2:2 the horizontal pair, 4:2:0 all four (q4). let n4x = n4w[plane]; let n4y = n4h[plane]; for y in 0..n4y { for xx in 0..n4x { let i = y * n4x + xx; let mv = average_split_mvs(&info.bmi_mv, rf, i, ss_x, ss_y); let (data, _) = planes.plane(plane); let dst_off = (((row * 8) >> ss_y) + y * 4) * dst_stride + ((col * 8) >> ss_x) + xx * 4; build_inter_predictors( &mut data[dst_off..], dst_stride, &refp, col, row, xx * 4, y * 4, 4, 4, pw, ph, mv, mb_left, mb_to_right, mb_top, mb_to_bottom, ss_x, ss_y, kernel, rf == 1, &sf, scratch, temp, ); } } } else { let (data, _) = planes.plane(plane); let dst_off = ((row * 8) >> ss_y) * dst_stride + ((col * 8) >> ss_x); build_inter_predictors( &mut data[dst_off..], dst_stride, &refp, col, row, 0, 0, pw, ph, pw, ph, info.mv[rf], mb_left, mb_to_right, mb_top, mb_to_bottom, ss_x, ss_y, kernel, rf == 1, &sf, scratch, temp, ); } } Ok(()) } /// `read_interp_filter` (`vp9_decodeframe.c:1482`): the header's filter is a /// literal index through `literal_to_filter[] = {EIGHTTAP_SMOOTH, EIGHTTAP, /// EIGHTTAP_SHARP, BILINEAR}` — the spec's literal order is NOT libvpx's /// filter numbering (`#define EIGHTTAP 0`, `EIGHTTAP_SMOOTH 1`, /// `EIGHTTAP_SHARP 2`, `BILINEAR 3`), and `vp9_filter_kernels` (the kernel /// table) is indexed by the latter. A switchable frame stores SWITCHABLE (4) /// and picks per block, where the tree's leaves already use libvpx numbering. fn interp_filter_of(hdr: &FrameHeader) -> u8 { const LITERAL_TO_FILTER: [u8; 4] = [1, 0, 2, 3]; let v = hdr.interpolation_filter as u8; if v == crate::inter::SWITCHABLE { v } else { LITERAL_TO_FILTER[v as usize] } } // --------------------------------------------------------------------------- // Tile-column threading. // // VP9 tile columns are independent within a frame: a block never reads mode // info, entropy contexts or pixels across a tile-column boundary (left // availability stops at `tile_col_start`, MV candidates are bounded by the // tile's column range, and above-right samples stay inside the block). Tile // ROWS of one column depend on each other, so a worker walks every tile row // of the columns it owns, in order. Each worker decodes into a private copy of // the frame; the columns are then copied into the frame in column order. The // counts are sums, so the result does not depend on the worker count. /// Everything a tile walk reads that is fixed for the whole frame. struct TileJob<'a> { hdr: &'a FrameHeader, tx_mode: u8, /// Whether the walk reconstructs pixels (see `decode_partition`). pixels: bool, /// Loop-filter level per segment (keyframes; inter walks pass zeros). level_of_seg: [[u8; 2]; 8], /// Tile payloads in bitstream order: `tiles[tile_row * tile_cols + tile_col]`. tiles: &'a [&'a [u8]], } /// Split the tile payloads (spec 6.4). Every tile but the last carries a /// 32-bit big-endian size (libvpx `get_tile_buffer`). fn split_tiles(mut data: &[u8], tile_cols: usize, tile_rows: usize) -> Result> { let mut tiles = Vec::with_capacity(tile_cols * tile_rows); for tr in 0..tile_rows { for tc in 0..tile_cols { let last = tr == tile_rows - 1 && tc == tile_cols - 1; if last { tiles.push(data); } else { ensure(data.len() >= 4, "truncated tile size")?; let sz = u32::from_be_bytes([data[0], data[1], data[2], data[3]]) as usize; data = &data[4..]; ensure(data.len() >= sz, "truncated tile data")?; tiles.push(&data[..sz]); data = &data[sz..]; } } } Ok(tiles) } fn new_grids(mi_cols: usize, mi_rows: usize) -> LfGrids { LfGrids { level8: vec![0; mi_cols * mi_rows], blk: vec![(0u8, 0u32, 0u32); mi_cols * mi_rows], otx: vec![0; mi_cols * mi_rows], skip_inter: vec![false; mi_cols * mi_rows], seg: vec![0; mi_cols * mi_rows], mi_cols, mi_rows, } } /// Mode-info column span `[lo, hi)` of tile column `tc` (spec 6.4). fn tile_col_span(hdr: &FrameHeader, tc: usize) -> (usize, usize) { let mi_cols = hdr.mi_cols() as usize; let sb64_cols = mi_cols.div_ceil(SB_MI); let log2 = hdr.tile_info.cols_log2; let lo = (((sb64_cols * tc) >> log2) * SB_MI).min(mi_cols); let hi = (((sb64_cols * (tc + 1)) >> log2) * SB_MI).min(mi_cols); (lo, hi) } /// Copy columns `[lo, hi)` of every row of a mode-info array. fn copy_mi_cols(dst: &mut [T], src: &[T], mi_cols: usize, mi_rows: usize, lo: usize, hi: usize) { for r in 0..mi_rows { let b = r * mi_cols; dst[b + lo..b + hi].copy_from_slice(&src[b + lo..b + hi]); } } /// Copy the pixel columns of one tile column. The last tile column also owns /// the padding past `mi_cols` (a block at the frame edge writes its full size). /// Padded (64-multiple) luma width of a frame. fn frame_aw(hdr: &FrameHeader) -> usize { (hdr.width as usize).div_ceil(64) * 64 } /// Padded (64-multiple) luma height of a frame. fn frame_ah(hdr: &FrameHeader) -> usize { (hdr.height as usize).div_ceil(64) * 64 } fn copy_plane_cols(dst: &mut Planes, src: &Planes, lo_mi: usize, hi_mi: usize, last: bool) { fn rows(dst: &mut [T], src: &[T], stride: usize, rows: usize, x0: usize, x1: usize) { for r in 0..rows { let b = r * stride; dst[b + x0..b + x1].copy_from_slice(&src[b + x0..b + x1]); } } let x0 = lo_mi * 8; let x1 = if last { dst.ys } else { hi_mi * 8 }; rows(&mut dst.y, &src.y, dst.ys, dst.ah, x0, x1); let (cx0, cx1) = (x0 >> dst.ss_x, if last { dst.uvs } else { x1 >> dst.ss_x }); let ch = dst.ah >> dst.ss_y; rows(&mut dst.u, &src.u, dst.uvs, ch, cx0, cx1); rows(&mut dst.v, &src.v, dst.uvs, ch, cx0, cx1); } impl Decoder { /// A frame buffer of this geometry, reusing a spare when one fits. fn frame_planes(&mut self, aw: usize, ah: usize, bd: u8, ss_x: usize, ss_y: usize) -> Planes { match self.spare.iter().position(|p| p.fits(aw, ah, bd, ss_x, ss_y)) { Some(i) => { let mut p = self.spare.swap_remove(i); p.refill(); p } None => Planes::new(aw, ah, bd, ss_x, ss_y), } } /// Keep the buffer of a reference slot that was just replaced, if nothing else /// holds it. Only a few spares are kept; the rest are dropped. fn recycle(&mut self, old: Option) { if let Some(rf) = old { if let Ok(p) = std::sync::Arc::try_unwrap(rf.planes) { self.recycle_planes(p); } } } fn recycle_planes(&mut self, p: Planes) { if self.spare.len() < 8 { self.spare.push(p); } } /// Set how many tile-column workers decode each frame. `1` is /// single-threaded; the output is identical for every value. pub fn set_threads(&mut self, n: usize) { self.threads = n.clamp(1, 64); } /// Walk every tile of the frame into `self.planes` / `self.iw`, with the /// loop-filter grids and counts merged in. fn walk_frame(&mut self, job: &TileJob<'_>, grids: &mut LfGrids, counts: &mut crate::adapt::Counts) -> Result<()> { let hdr = job.hdr; let tile_cols = 1usize << hdr.tile_info.cols_log2; let workers = self.threads.clamp(1, tile_cols); if workers == 1 { let cols: Vec = (0..tile_cols).collect(); return self.walk_columns(job, &cols, grids, counts); } let mi_cols = hdr.mi_cols() as usize; let mi_rows = hdr.mi_rows() as usize; let owned: Vec> = (0..workers).map(|w| (w..tile_cols).step_by(workers).collect()).collect(); let worker_planes: Vec = (0..workers) .map(|_| self.frame_planes(frame_aw(hdr), frame_ah(hdr), hdr.bit_depth, hdr.subsampling_x as usize, hdr.subsampling_y as usize)) .collect(); let parent: &Decoder = self; let results: Vec> = std::thread::scope(|scope| { let handles: Vec<_> = owned .iter() .zip(worker_planes) .map(|(cols, planes)| { scope.spawn(move || -> Result<(Decoder, LfGrids, crate::adapt::Counts)> { let t_fork = crate::prof::start(); let mut wk = parent.fork(hdr, planes); let mut g = new_grids(mi_cols, mi_rows); let mut c = crate::adapt::Counts::default(); crate::prof::stop(crate::prof::Phase::Threads, t_fork); wk.walk_columns(job, cols, &mut g, &mut c)?; Ok((wk, g, c)) }) }) .collect(); handles .into_iter() .map(|h| h.join().unwrap_or_else(|_| Err(corrupt("tile worker panicked")))) .collect() }); let mut parts = Vec::with_capacity(workers); for res in results { parts.push(res?); } let t_merge = crate::prof::start(); for (w, (mut wk, g, c)) in parts.into_iter().enumerate() { counts.add(&c); for &tc in &owned[w] { let (lo, hi) = tile_col_span(hdr, tc); if lo >= hi { continue; } copy_mi_cols(&mut grids.level8, &g.level8, mi_cols, mi_rows, lo, hi); copy_mi_cols(&mut grids.blk, &g.blk, mi_cols, mi_rows, lo, hi); copy_mi_cols(&mut grids.otx, &g.otx, mi_cols, mi_rows, lo, hi); copy_mi_cols(&mut grids.skip_inter, &g.skip_inter, mi_cols, mi_rows, lo, hi); copy_mi_cols(&mut grids.seg, &g.seg, mi_cols, mi_rows, lo, hi); if let (Some(dst), Some(src)) = (self.iw.as_mut(), wk.iw.as_ref()) { copy_mi_cols(&mut dst.mvs, &src.mvs, mi_cols, mi_rows, lo, hi); copy_mi_cols(&mut dst.seg_map, &src.seg_map, mi_cols, mi_rows, lo, hi); } if let (Some(dst), Some(src)) = (self.planes.as_mut(), wk.planes.as_ref()) { copy_plane_cols(dst, src, lo, hi, hi == mi_cols); } } if let (Some(dst), Some(src)) = (self.iw.as_mut(), wk.iw.as_ref()) { dst.blocks += src.blocks; } if let Some(p) = wk.planes.take() { self.recycle_planes(p); } } crate::prof::stop(crate::prof::Phase::Threads, t_merge); Ok(()) } /// Walk the tile columns `cols`, every tile row of each, in order. fn walk_columns( &mut self, job: &TileJob<'_>, cols: &[usize], grids: &mut LfGrids, counts: &mut crate::adapt::Counts, ) -> Result<()> { let hdr = job.hdr; let mi_cols = hdr.mi_cols() as usize; let mi_rows = hdr.mi_rows() as usize; let tile_cols = 1usize << hdr.tile_info.cols_log2; let tile_rows = 1usize << hdr.tile_info.rows_log2; // `above_context` / `above_seg_context` are FRAME-scoped in libvpx // (zeroed once per frame, indexed by ABSOLUTE mi_col), so a walk // zeroes them once; columns never read another column's entries. They // are sized by `mi_cols_aligned_to_sb`: a TX_32X32 block indexes up to // 8 4x4 units past the last mi col. let aligned_cols = mi_cols.div_ceil(SB_MI) * SB_MI; let mut ectx = [ PlaneContexts::new(aligned_cols * 2), PlaneContexts::new(aligned_cols * 2), PlaneContexts::new(aligned_cols * 2), ]; let mut mi = MiState::new(mi_cols, mi_rows); mi.above_seg.fill(0); for &tc in cols { let (col_lo, col_hi) = tile_col_span(hdr, tc); if let Some(w) = self.iw.as_mut() { w.tile_col_end = col_hi; } for tr in 0..tile_rows { let sb64_rows = mi_rows.div_ceil(SB_MI); let row_lo = ((sb64_rows * tr) >> hdr.tile_info.rows_log2) * SB_MI; let row_hi = (((sb64_rows * (tr + 1)) >> hdr.tile_info.rows_log2) * SB_MI).min(mi_rows); let data = job.tiles[tr * tile_cols + tc]; if crate::trace_enabled() { eprintln!("TILE {} bytes tr={tr} tc={tc}", data.len()); } let mut r = crate::bool::BoolDecoder::new(data)?; for sb_row in (row_lo..row_hi).step_by(SB_MI) { // Left entropy/partition contexts are zeroed at the start // of every superblock row (the SB row's left edge has no // neighbours, libvpx vp9_decodeframe.c decode_tiles). ectx.iter_mut().for_each(|p| p.left = [0; 32]); mi.left_seg = [0; 32]; for sb_col in (col_lo..col_hi).step_by(SB_MI) { self.decode_partition( &mut r, &mut mi, &mut ectx, grids, &job.level_of_seg, hdr, sb_row, sb_col, 4, col_lo, job.tx_mode, job.pixels, counts, )?; } } ensure( r.overreads() == 0, &format!("tile bool decoder desync (tile tr={tr} tc={tc})"), )?; } } Ok(()) } /// A worker decoder for a set of tile columns: the same frame state, with /// its own frame buffer and walk state. The parent is not modified. fn fork(&self, hdr: &FrameHeader, planes: Planes) -> Decoder { let n = (hdr.mi_cols() * hdr.mi_rows()) as usize; Decoder { parser: Vp9Parser::new(), ctx: self.ctx.clone(), refs: self.refs.clone(), planes: Some(planes), spare: Vec::new(), frame_ctxs: self.frame_ctxs.clone(), prev_mvs: Vec::new(), prev_seg: Vec::new(), last: self.last, iw: self.iw.as_ref().map(|w| InterWalk { ifs: w.ifs, sign_bias: w.sign_bias, use_prev: w.use_prev, seg_map: vec![0; n], mvs: vec![crate::inter::MvRef::default(); n], prev_seg: w.prev_seg.clone(), prev_mvs: w.prev_mvs.clone(), tile_col_end: w.tile_col_end, frame_index: w.frame_index, blocks: 0, }), frames_seen: self.frames_seen, decode_index: self.decode_index, syntax_blocks: 0, mc_scratch: Vec::new(), mc_temp: vec![0; 64 * 135 + 64 * 64], threads: 1, } } }