diff --git a/crates/ec-vp9-syntax/src/header.rs b/crates/ec-vp9-syntax/src/header.rs index 31a0b5da..a2f6dabd 100644 --- a/crates/ec-vp9-syntax/src/header.rs +++ b/crates/ec-vp9-syntax/src/header.rs @@ -188,6 +188,17 @@ pub struct SegmentationParams { pub feature_data: [[i16; SEG_LVL_MAX]; MAX_SEGMENTS], } +impl SegmentationParams { + /// Whether `feature` (a `SEG_LVL_*` index) is on for `segment_id`. + pub fn feature_active(&self, segment_id: u8, feature: usize) -> bool { + self.enabled + && self + .feature_enabled + .get(segment_id as usize) + .is_some_and(|f| f[feature]) + } +} + /// Tile layout (spec 6.2, `tile_info`). #[derive(Debug, Clone, Copy, PartialEq, Eq, Default)] pub struct TileInfo { @@ -638,9 +649,9 @@ impl Vp9Parser { subsampling_x: h.subsampling_x, subsampling_y: h.subsampling_y, }; - for i in 0..NUM_REF_FRAMES { + for (i, dst) in self.refs.iter_mut().enumerate() { if h.refresh_frame_flags & (1 << i) != 0 { - self.refs[i] = slot; + *dst = slot; } } } diff --git a/crates/ec-vp9/src/decode.rs b/crates/ec-vp9/src/decode.rs index a4aa0105..6195781b 100644 --- a/crates/ec-vp9/src/decode.rs +++ b/crates/ec-vp9/src/decode.rs @@ -24,8 +24,11 @@ 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 tiles::{copy_mi_cols, copy_plane_cols, frame_aw, frame_ah, new_grids, split_tiles, tile_col_span}; use ec_vp9_syntax::{FrameHeader, FrameType, Vp9Parser, superframe}; +mod tiles; + 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 @@ -68,22 +71,22 @@ pub struct Picture { pub subsampling_y: u8, } -struct Planes { - y: Vec, - u: Vec, - v: Vec, - ys: usize, - uvs: usize, +pub(crate) struct Planes { + pub(crate) y: Vec, + pub(crate) u: Vec, + pub(crate) v: Vec, + pub(crate) ys: usize, + pub(crate) uvs: usize, aw: usize, ah: usize, /// Bits per sample this frame was decoded at. - bd: u8, + pub(crate) bd: u8, /// Chroma horizontal subsampling (`subsampling_x`): 0 for 4:4:4, 1 for /// 4:2:0/4:2:2. - ss_x: usize, + pub(crate) 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, + pub(crate) ss_y: usize, } /// VP9 level 6.2 caps a picture at 8192x4352 luma samples. Larger frame headers are refused @@ -113,7 +116,7 @@ impl Planes { /// 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 { + pub(crate) 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 { @@ -261,6 +264,11 @@ struct InterWalk { impl Decoder { /// Create an empty decoder; the first key frame sets the size. pub fn new() -> Self { + Self::with_threads(default_threads()) + } + + /// Create an empty decoder that walks tile columns on `threads` workers. + pub fn with_threads(threads: usize) -> Self { Decoder { parser: Vp9Parser::new(), ctx: Box::new(FrameContext::new(true)), @@ -277,7 +285,7 @@ impl Decoder { iw: InterWalk::default(), mc_scratch: Vec::new(), mc_temp: vec![0; 64 * 135 + 64 * 64], - threads: default_threads(), + threads: threads.clamp(1, 64), } } @@ -344,14 +352,7 @@ impl Decoder { 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()); - } + self.refresh_refs(&hdr, 0xff); Ok(if hdr.show_frame { Some(pic) } else { None }) } @@ -411,17 +412,7 @@ impl Decoder { 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); - } - } + self.refresh_refs(&hdr, hdr.refresh_frame_flags); Ok(if hdr.show_frame { Some(pic) } else { None }) } @@ -576,44 +567,41 @@ impl Decoder { // 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 { - 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, - ); - } + self.loop_filter(&grids, hdr); let pic = self.take_picture(hdr); + // 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. + self.refresh_refs(hdr, hdr.refresh_frame_flags); + Ok(if hdr.show_frame { Some(pic) } else { None }) + } + + /// Move the frame scratch into every slot whose bit is set in `flags`. + fn refresh_refs(&mut self, hdr: &FrameHeader, flags: u8) { 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 { + if flags & (1 << slot) != 0 { let old = self.refs[slot].replace(frame.clone()); self.recycle(old); } } - Ok(if hdr.show_frame { Some(pic) } else { None }) + } + + /// Loop-filter post-pass over the MI-aligned visible planes (level 0 + /// disables it); the extents are not the padded buffer size. + fn loop_filter(&mut self, grids: &LfGrids, hdr: &FrameHeader) { + if hdr.loop_filter.level == 0 { + return; + } + let p = self.planes.as_mut().expect("frame scratch"); + let (vw, vh) = (hdr.mi_cols() as usize * 8, hdr.mi_rows() as usize * 8); + grids.filter_frame(p, vw, vh, hdr.loop_filter.sharpness); } /// Crop the frame scratch to a [`Picture`]. @@ -947,8 +935,6 @@ impl Decoder { 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; @@ -983,25 +969,7 @@ impl Decoder { // 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 { - 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, - ); - } + self.loop_filter(&grids, hdr); // `vp9_swap_current_and_last_seg_map` runs only when segmentation is // enabled: this frame's map becomes the next frame's prediction map. @@ -1013,157 +981,52 @@ impl Decoder { } /// `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, - counts: &mut crate::adapt::Counts, - ) -> Result<()> { - if row >= mi.mi_rows || col >= mi.mi_cols { + fn decode_partition(&mut self, w: &mut TileWalk<'_, '_>, row: usize, col: usize, n4: usize) -> Result<()> { + if row >= w.mi.mi_rows || col >= w.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; + let has_rows = row + hbs < w.mi.mi_rows; + let has_cols = col + hbs < w.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 ctx = w.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) + w.r.read_tree(&PARTITION_TREE, probs) } else if !has_rows && has_cols { - if r.read_bool(probs[1]) { 3 } else { 1 } + if w.r.read_bool(probs[1]) { 3 } else { 1 } } else if has_rows && !has_cols { - if r.read_bool(probs[2]) { 3 } else { 2 } + if w.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; + w.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, - counts, - )?; + self.decode_block(w, row, col, subsize, 1, 1)?; } 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, - counts, - )?, + 0 => self.decode_block(w, row, col, subsize, n4, n4)?, 1 => { - self.decode_block( - r, - mi, - ectx, - grids, - level_of_seg, - hdr, - row, - col, - subsize, - n4, - n8, - tile_col_start, - tx_mode, - counts, - )?; + self.decode_block(w, row, col, subsize, n4, n8)?; 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, - counts, - )?; + self.decode_block(w, row + hbs, col, subsize, n4, n8)?; } } 2 => { - self.decode_block( - r, - mi, - ectx, - grids, - level_of_seg, - hdr, - row, - col, - subsize, - n8, - n4, - tile_col_start, - tx_mode, - counts, - )?; + self.decode_block(w, row, col, subsize, n8, n4)?; 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, - counts, - )?; + self.decode_block(w, row, col + hbs, subsize, n8, n4)?; } } _ => { @@ -1173,49 +1036,21 @@ impl Decoder { (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, - counts, - )?; + self.decode_partition(w, rr, cc, n8)?; } } } } if bsize >= 3 && (bsize == 3 || partition != 3) { - mi.update_partition_ctx(row, col, subsize, 1 << n8); + w.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, - counts: &mut crate::adapt::Counts, - ) -> Result<()> { + fn decode_block(&mut self, w: &mut TileWalk<'_, '_>, row: usize, col: usize, sb_type: usize, bwl: usize, bhl: usize) -> Result<()> { + let TileWalk { r, mi, ectx, grids, level_of_seg, hdr, tile_col_start, tx_mode, counts } = w; + let (level_of_seg, hdr, tile_col_start, tx_mode) = (&*level_of_seg, *hdr, *tile_col_start, *tx_mode); let bw = 1 << (bwl - 1); let bh = 1 << (bhl - 1); let x_mis = bw.min(mi.mi_cols - col); @@ -1950,6 +1785,18 @@ fn interp_filter_of(hdr: &FrameHeader) -> u8 { // 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. +struct TileWalk<'a, 'd> { + r: &'a mut crate::bool::BoolDecoder<'d>, + mi: &'a mut MiState, + ectx: &'a mut [PlaneContexts; 3], + grids: &'a mut LfGrids, + level_of_seg: &'a [[u8; 2]; 8], + hdr: &'a FrameHeader, + tile_col_start: usize, + tx_mode: u8, + counts: &'a mut crate::adapt::Counts, +} + /// Everything a tile walk reads that is fixed for the whole frame. struct TileJob<'a> { hdr: &'a FrameHeader, @@ -1960,86 +1807,6 @@ struct TileJob<'a> { 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 { @@ -2184,20 +1951,18 @@ impl Decoder { 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, + let mut w = TileWalk { + r: &mut r, + mi: &mut mi, + ectx: &mut ectx, + grids: &mut *grids, + level_of_seg: &job.level_of_seg, hdr, - sb_row, - sb_col, - 4, - col_lo, - job.tx_mode, - counts, - )?; + tile_col_start: col_lo, + tx_mode: job.tx_mode, + counts: &mut *counts, + }; + self.decode_partition(&mut w, sb_row, sb_col, 4)?; } } ensure( diff --git a/crates/ec-vp9/src/decode/tiles.rs b/crates/ec-vp9/src/decode/tiles.rs new file mode 100644 index 00000000..b528929c --- /dev/null +++ b/crates/ec-vp9/src/decode/tiles.rs @@ -0,0 +1,85 @@ +use super::{Planes, SB_MI}; +use crate::loopfilter::LfGrids; +use crate::tables::ensure; +use crate::Result; +use ec_vp9_syntax::FrameHeader; + +/// Split the tile payloads (spec 6.4). Every tile but the last carries a +/// 32-bit big-endian size (libvpx `get_tile_buffer`). +pub(super) 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) +} + +pub(super) 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). +pub(super) 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. +pub(super) 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]); + } +} + +/// Padded (64-multiple) luma width of a frame. +pub(super) fn frame_aw(hdr: &FrameHeader) -> usize { + (hdr.width as usize).div_ceil(64) * 64 +} + +/// Padded (64-multiple) luma height of a frame. +pub(super) fn frame_ah(hdr: &FrameHeader) -> usize { + (hdr.height as usize).div_ceil(64) * 64 +} + +/// 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). +pub(super) 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); +} diff --git a/crates/ec-vp9/src/inter.rs b/crates/ec-vp9/src/inter.rs index 2896e2ae..1e0f2d31 100644 --- a/crates/ec-vp9/src/inter.rs +++ b/crates/ec-vp9/src/inter.rs @@ -40,9 +40,7 @@ const MV_BORDER: i32 = 16 << 3; const MV_HP_THRESH: i32 = 64; const CLASS0_SIZE: i32 = 2; const CLASS0_BITS: i32 = 1; -/// `SEG_LVL_SKIP` / `SEG_LVL_REF_FRAME`. -const SEG_LVL_REF_FRAME: usize = 2; -const SEG_LVL_SKIP: usize = 3; +use ec_vp9_syntax::{SEG_LVL_REF_FRAME, SEG_LVL_SKIP}; /// `mv_ref_blocks[BLOCK_SIZES]` (`vp9_mvref_common.h`): `(row, col)` offsets, /// transcribed verbatim from the C. libvpx's `POSITION` is `{ row, col }`, so @@ -279,14 +277,6 @@ fn read_inter_segment_id( segment_id } -fn seg_feature(seg: &ec_vp9_syntax::SegmentationParams, segment_id: u8, feature: usize) -> bool { - seg.enabled - && seg - .feature_enabled - .get(segment_id as usize) - .is_some_and(|f| f[feature]) -} - /// `read_skip`. fn read_skip( r: &mut BoolDecoder, @@ -296,7 +286,7 @@ fn read_skip( nb: Nb<'_>, counts: &mut Counts, ) -> bool { - if seg_feature(c.seg, segment_id, SEG_LVL_SKIP) { + if c.seg.feature_active(segment_id, SEG_LVL_SKIP) { true } else { let ctx = usize::from(nb.above.is_some_and(|m| m.skip)) @@ -333,7 +323,7 @@ fn read_is_inter_block( nb: Nb<'_>, counts: &mut Counts, ) -> bool { - if seg_feature(c.seg, segment_id, SEG_LVL_REF_FRAME) { + if c.seg.feature_active(segment_id, SEG_LVL_REF_FRAME) { c.seg.feature_data[segment_id as usize][SEG_LVL_REF_FRAME] != INTRA_FRAME as i16 } else { let ctx = intra_inter_context(nb); @@ -1144,7 +1134,7 @@ fn read_ref_frames( out: &mut [i8; 2], counts: &mut Counts, ) { - if seg_feature(c.seg, segment_id, SEG_LVL_REF_FRAME) { + if c.seg.feature_active(segment_id, SEG_LVL_REF_FRAME) { out[0] = c.seg.feature_data[segment_id as usize][SEG_LVL_REF_FRAME] as i8; out[1] = NO_REF_FRAME; return; @@ -1193,7 +1183,7 @@ fn read_inter_block_mode_info( let is_compound = usize::from(info.ref_frame[1] > INTRA_FRAME); let inter_mode_ctx = get_mode_context(st, c, bsize, row, col); - if seg_feature(c.seg, info.segment, SEG_LVL_SKIP) { + if c.seg.feature_active(info.segment, SEG_LVL_SKIP) { info.mode = ZEROMV; if bsize < 3 { return Err(crate::Error::corrupt( diff --git a/crates/ec-vp9/src/loopfilter.rs b/crates/ec-vp9/src/loopfilter.rs index a7d66a92..8579c9fc 100644 --- a/crates/ec-vp9/src/loopfilter.rs +++ b/crates/ec-vp9/src/loopfilter.rs @@ -75,6 +75,7 @@ const LUMA_ABOVE_TXF: [u64; 4] = [u64::MAX, u64::MAX, 0x00ff00ff00ff00ff, 0x0000 const LUMA_LEFT_BORDER: u64 = 0x1111111111111111; const LUMA_ABOVE_BORDER: u64 = 0x000000ff000000ff; +use crate::decode::Planes; use crate::Sample; /// `signed_char_clamp_high` (`vpx_dsp/loopfilter.c:23`): clamp to @@ -1192,23 +1193,10 @@ impl LfGrids { /// `vp9_loop_filter_frame` (vp9_loopfilter.c:1430): luma always takes the /// ss00 path; chroma takes ss00 for 4:4:4, ss11 for 4:2:0, and the /// non420 slow path otherwise (4:2:2 / 4:4:0). - #[allow(clippy::too_many_arguments)] - pub(crate) fn filter_frame( - &self, - y: &mut [Sample], - stride: usize, - u: &mut [Sample], - v: &mut [Sample], - uv_stride: usize, - y_w: usize, - y_h: usize, - uv_w: usize, - uv_h: usize, - ss_x: usize, - ss_y: usize, - sharpness: u8, - bd: u8, - ) { + pub(crate) fn filter_frame(&self, p: &mut Planes, vw: usize, vh: usize, sharpness: u8) { + let (ss_x, ss_y, bd) = (p.ss_x, p.ss_y, p.bd); + let (uv_w, uv_h) = (vw >> ss_x, vh >> ss_y); + let (stride, uv_stride) = (p.ys, p.uvs); // The three planes are filtered from the same read-only grids and touch // disjoint buffers, so they run concurrently: luma on one thread, both // chroma planes on another. Each plane's edge order is unchanged, so the @@ -1226,8 +1214,8 @@ impl LfGrids { } }; std::thread::scope(|scope| { - scope.spawn(|| chroma(u, v)); - self.filter_ss00_plane(y, stride, y_w, y_h, sharpness, bd); + scope.spawn(|| chroma(&mut p.u, &mut p.v)); + self.filter_ss00_plane(&mut p.y, stride, vw, vh, sharpness, bd); }); } } @@ -1253,9 +1241,10 @@ mod tests { } fn run(g: &LfGrids, y: &mut [Sample]) { - let mut u = vec![128u16; 32 * 32]; - let mut v = vec![128u16; 32 * 32]; - g.filter_frame(y, 64, &mut u, &mut v, 32, 64, 64, 32, 32, 1, 1, 0, 8); + let mut p = Planes::new(64, 64, 8, 1, 1); + p.y.copy_from_slice(y); + g.filter_frame(&mut p, 64, 64, 0); + y.copy_from_slice(&p.y); } /// The luma internal-4x4 horizontal edge fires on the frame's LAST MI row. diff --git a/crates/ec-vp9/src/modes.rs b/crates/ec-vp9/src/modes.rs index 357f11b2..287a1a0f 100644 --- a/crates/ec-vp9/src/modes.rs +++ b/crates/ec-vp9/src/modes.rs @@ -5,6 +5,7 @@ use crate::bool::BoolDecoder; use crate::header::FrameContext; use crate::tables::*; +use ec_vp9_syntax::SEG_LVL_SKIP; /// One block's parsed prediction record (`MODE_INFO`). #[derive(Clone, Default)] @@ -155,12 +156,7 @@ pub(crate) fn read_intra_frame_mode_info( let skip_ctx = above_mi.as_ref().map_or(0, |m| u32::from(m.skip)) + left_mi.as_ref().map_or(0, |u| u32::from(u.skip)); let skip_ctx = skip_ctx as usize; - let seg_skip = seg.enabled - && seg - .feature_enabled - .get(segment as usize) - .is_some_and(|f| f[3]); - let skip = if seg_skip { + let skip = if seg.feature_active(segment, SEG_LVL_SKIP) { true } else { r.read_bool(fc.skip[skip_ctx])