forge
Explore
Log in

Don't you dare go Hollow

Merge branch 'simplify/s6' into simplify/w2

Browse files at this commit

74c8bf512d5c5c91ec0b4f86faf02fda8315dbbbAhmet Kaan GümüşParent: 5cb9eaf96bcfed
crates/ec-vp9-syntax/src/header.rsmodified+13 -2
@@ -188,6 +188,17 @@ pub struct SegmentationParams {
188188 pub feature_data: [[i16; SEG_LVL_MAX]; MAX_SEGMENTS],
189189 }
190190
191+impl SegmentationParams {
192+ /// Whether `feature` (a `SEG_LVL_*` index) is on for `segment_id`.
193+ pub fn feature_active(&self, segment_id: u8, feature: usize) -> bool {
194+ self.enabled
195+ && self
196+ .feature_enabled
197+ .get(segment_id as usize)
198+ .is_some_and(|f| f[feature])
199+ }
200+}
201+
191202 /// Tile layout (spec 6.2, `tile_info`).
192203 #[derive(Debug, Clone, Copy, PartialEq, Eq, Default)]
193204 pub struct TileInfo {
@@ -638,9 +649,9 @@ impl Vp9Parser {
638649 subsampling_x: h.subsampling_x,
639650 subsampling_y: h.subsampling_y,
640651 };
641- for i in 0..NUM_REF_FRAMES {
652+ for (i, dst) in self.refs.iter_mut().enumerate() {
642653 if h.refresh_frame_flags & (1 << i) != 0 {
643- self.refs[i] = slot;
654+ *dst = slot;
644655 }
645656 }
646657 }
crates/ec-vp9/src/decode.rsmodified+88 -323
@@ -24,8 +24,11 @@ use crate::tokens::{PlaneContexts, decode_coefs, scan_for};
2424 use crate::transform::inverse_transform_add;
2525 use crate::tables::{ensure, corrupt};
2626 use crate::{Error, Result, Sample};
27+use tiles::{copy_mi_cols, copy_plane_cols, frame_aw, frame_ah, new_grids, split_tiles, tile_col_span};
2728 use ec_vp9_syntax::{FrameHeader, FrameType, Vp9Parser, superframe};
2829
30+mod tiles;
31+
2932 const SB_MI: usize = 8; // a 64x64 superblock in mi (8x8) units
3033
3134 /// The compressed-header bytes of `frame`. Both sizes come from the input, so
@@ -68,22 +71,22 @@ pub struct Picture {
6871 pub subsampling_y: u8,
6972 }
7073
71-struct Planes {
72- y: Vec<Sample>,
73- u: Vec<Sample>,
74- v: Vec<Sample>,
75- ys: usize,
76- uvs: usize,
74+pub(crate) struct Planes {
75+ pub(crate) y: Vec<Sample>,
76+ pub(crate) u: Vec<Sample>,
77+ pub(crate) v: Vec<Sample>,
78+ pub(crate) ys: usize,
79+ pub(crate) uvs: usize,
7780 aw: usize,
7881 ah: usize,
7982 /// Bits per sample this frame was decoded at.
80- bd: u8,
83+ pub(crate) bd: u8,
8184 /// Chroma horizontal subsampling (`subsampling_x`): 0 for 4:4:4, 1 for
8285 /// 4:2:0/4:2:2.
83- ss_x: usize,
86+ pub(crate) ss_x: usize,
8487 /// Chroma vertical subsampling (`subsampling_y`): 0 for 4:4:4/4:2:2, 1
8588 /// for 4:2:0/4:4:0.
86- ss_y: usize,
89+ pub(crate) ss_y: usize,
8790 }
8891
8992 /// VP9 level 6.2 caps a picture at 8192x4352 luma samples. Larger frame headers are refused
@@ -113,7 +116,7 @@ impl Planes {
113116 /// edge decode at FULL size), so the fill only backs the never-written
114117 /// padding past the MI extent. Chroma planes are `aw >> ss_x` by
115118 /// `ah >> ss_y`; since `aw`/`ah` are 64-multiples the shift is exact.
116- fn new(aw: usize, ah: usize, bd: u8, ss_x: usize, ss_y: usize) -> Planes {
119+ pub(crate) fn new(aw: usize, ah: usize, bd: u8, ss_x: usize, ss_y: usize) -> Planes {
117120 let fill = 1u16 << (bd - 1);
118121 let (cw, ch) = (aw >> ss_x, ah >> ss_y);
119122 Planes {
@@ -261,6 +264,11 @@ struct InterWalk {
261264 impl Decoder {
262265 /// Create an empty decoder; the first key frame sets the size.
263266 pub fn new() -> Self {
267+ Self::with_threads(default_threads())
268+ }
269+
270+ /// Create an empty decoder that walks tile columns on `threads` workers.
271+ pub fn with_threads(threads: usize) -> Self {
264272 Decoder {
265273 parser: Vp9Parser::new(),
266274 ctx: Box::new(FrameContext::new(true)),
@@ -277,7 +285,7 @@ impl Decoder {
277285 iw: InterWalk::default(),
278286 mc_scratch: Vec::new(),
279287 mc_temp: vec![0; 64 * 135 + 64 * 64],
280- threads: default_threads(),
288+ threads: threads.clamp(1, 64),
281289 }
282290 }
283291
@@ -344,14 +352,7 @@ impl Decoder {
344352 self.frame_ctxs[hdr.frame_context_idx as usize] = self.ctx.as_ref().clone();
345353 }
346354 // Reference refresh (spec 8.10): a keyframe updates all slots.
347- let frame = RefFrame {
348- planes: std::sync::Arc::new(self.planes.take().expect("frame scratch")),
349- width: hdr.width as u16,
350- height: hdr.height as u16,
351- };
352- for slot in self.refs.iter_mut() {
353- *slot = Some(frame.clone());
354- }
355+ self.refresh_refs(&hdr, 0xff);
355356 Ok(if hdr.show_frame { Some(pic) } else { None })
356357 }
357358
@@ -411,17 +412,7 @@ impl Decoder {
411412 self.frame_ctxs[0] = self.ctx.as_ref().clone();
412413 }
413414 // Reference refresh (spec 8.10): only `refresh_frame_flags` slots.
414- let frame = RefFrame {
415- planes: std::sync::Arc::new(self.planes.take().expect("frame scratch")),
416- width: hdr.width as u16,
417- height: hdr.height as u16,
418- };
419- for slot in 0..8 {
420- if hdr.refresh_frame_flags & (1 << slot) != 0 {
421- let old = self.refs[slot].replace(frame.clone());
422- self.recycle(old);
423- }
424- }
415+ self.refresh_refs(&hdr, hdr.refresh_frame_flags);
425416 Ok(if hdr.show_frame { Some(pic) } else { None })
426417 }
427418
@@ -576,44 +567,41 @@ impl Decoder {
576567 // Loop filter post-pass: same extents as the keyframe path, but the
577568 // per-cell level comes from the block's own reference and mode class
578569 // (`get_filter_level`, vp9_loopfilter.c:249).
579- if hdr.loop_filter.level > 0 {
580- let p = self.planes.as_mut().expect("frame scratch");
581- let (vw, vh) = (mi_cols * 8, mi_rows * 8);
582- grids.filter_frame(
583- &mut p.y,
584- p.ys,
585- &mut p.u,
586- &mut p.v,
587- p.uvs,
588- vw,
589- vh,
590- vw >> ss_x,
591- vh >> ss_y,
592- ss_x,
593- ss_y,
594- hdr.loop_filter.sharpness,
595- hdr.bit_depth,
596- );
597- }
570+ self.loop_filter(&grids, hdr);
598571
599572 let pic = self.take_picture(hdr);
573+ // Reference refresh (spec 8.10 / `swap_frame_buffers`): bit i of
574+ // `refresh_frame_flags` replaces slot i with the frame just decoded —
575+ // the LOOP-FILTERED frame, which is what a later frame's prediction
576+ // reads.
577+ self.refresh_refs(hdr, hdr.refresh_frame_flags);
578+ Ok(if hdr.show_frame { Some(pic) } else { None })
579+ }
580+
581+ /// Move the frame scratch into every slot whose bit is set in `flags`.
582+ fn refresh_refs(&mut self, hdr: &FrameHeader, flags: u8) {
600583 let frame = RefFrame {
601584 planes: std::sync::Arc::new(self.planes.take().expect("frame scratch")),
602585 width: hdr.width as u16,
603586 height: hdr.height as u16,
604587 };
605-
606- // Reference refresh (spec 8.10 / `swap_frame_buffers`): bit i of
607- // `refresh_frame_flags` replaces slot i with the frame just decoded —
608- // the LOOP-FILTERED frame, which is what a later frame's prediction
609- // reads.
610588 for slot in 0..8 {
611- if hdr.refresh_frame_flags & (1 << slot) != 0 {
589+ if flags & (1 << slot) != 0 {
612590 let old = self.refs[slot].replace(frame.clone());
613591 self.recycle(old);
614592 }
615593 }
616- Ok(if hdr.show_frame { Some(pic) } else { None })
594+ }
595+
596+ /// Loop-filter post-pass over the MI-aligned visible planes (level 0
597+ /// disables it); the extents are not the padded buffer size.
598+ fn loop_filter(&mut self, grids: &LfGrids, hdr: &FrameHeader) {
599+ if hdr.loop_filter.level == 0 {
600+ return;
601+ }
602+ let p = self.planes.as_mut().expect("frame scratch");
603+ let (vw, vh) = (hdr.mi_cols() as usize * 8, hdr.mi_rows() as usize * 8);
604+ grids.filter_frame(p, vw, vh, hdr.loop_filter.sharpness);
617605 }
618606
619607 /// Crop the frame scratch to a [`Picture`].
@@ -947,8 +935,6 @@ impl Decoder {
947935 tx_mode: u8,
948936 tail: &[u8],
949937 ) -> Result<Picture> {
950- let ss_x = hdr.subsampling_x as usize;
951- let ss_y = hdr.subsampling_y as usize;
952938 let mi_cols = hdr.mi_cols() as usize;
953939 let mi_rows = hdr.mi_rows() as usize;
954940 let tile_cols = 1usize << hdr.tile_info.cols_log2;
@@ -983,25 +969,7 @@ impl Decoder {
983969 // Loop filter post-pass (level 0 disables it for the frame).
984970 // The extents are the MI-aligned VISIBLE plane (libvpx's masks clear
985971 // everything past `mi_cols`/`mi_rows`), not the padded buffer size.
986- if hdr.loop_filter.level > 0 {
987- let p = self.planes.as_mut().expect("frame scratch");
988- let (vw, vh) = (mi_cols * 8, mi_rows * 8);
989- grids.filter_frame(
990- &mut p.y,
991- p.ys,
992- &mut p.u,
993- &mut p.v,
994- p.uvs,
995- vw,
996- vh,
997- vw >> ss_x,
998- vh >> ss_y,
999- ss_x,
1000- ss_y,
1001- hdr.loop_filter.sharpness,
1002- hdr.bit_depth,
1003- );
1004- }
972+ self.loop_filter(&grids, hdr);
1005973
1006974 // `vp9_swap_current_and_last_seg_map` runs only when segmentation is
1007975 // enabled: this frame's map becomes the next frame's prediction map.
@@ -1013,157 +981,52 @@ impl Decoder {
1013981 }
1014982
1015983 /// `decode_partition` (decodeframe.c:1173).
1016- #[allow(clippy::too_many_arguments)]
1017- fn decode_partition(
1018- &mut self,
1019- r: &mut crate::bool::BoolDecoder,
1020- mi: &mut MiState,
1021- ectx: &mut [PlaneContexts; 3],
1022- grids: &mut LfGrids,
1023- level_of_seg: &[[u8; 2]; 8],
1024- hdr: &FrameHeader,
1025- row: usize,
1026- col: usize,
1027- n4: usize,
1028- tile_col_start: usize,
1029- tx_mode: u8,
1030- counts: &mut crate::adapt::Counts,
1031- ) -> Result<()> {
1032- if row >= mi.mi_rows || col >= mi.mi_cols {
984+ fn decode_partition(&mut self, w: &mut TileWalk<'_, '_>, row: usize, col: usize, n4: usize) -> Result<()> {
985+ if row >= w.mi.mi_rows || col >= w.mi.mi_cols {
1033986 return Ok(());
1034987 }
1035988 let n8 = n4 - 1;
1036989 let hbs = (1usize << n8) >> 1;
1037- let has_rows = row + hbs < mi.mi_rows;
1038- let has_cols = col + hbs < mi.mi_cols;
990+ let has_rows = row + hbs < w.mi.mi_rows;
991+ let has_cols = col + hbs < w.mi.mi_cols;
1039992 // libvpx reads the partition symbol at EVERY level INCLUDING 8x8
1040993 // (decodeframe.c:1188 runs before the `!hbs` branch): the 8x8
1041994 // symbol picks 8x8 / 8x4 / 4x8 / 4x4-split — the sub-8x8 shapes.
1042995 // Only the 4x4 level has no symbol, and SPLIT recursion never
1043996 // reaches it (it stops at n4 == 1).
1044997 let partition = {
1045- let ctx = mi.partition_ctx(row, col, n8 as u32);
998+ let ctx = w.mi.partition_ctx(row, col, n8 as u32);
1046999 let probs = &self.ctx.partition[ctx];
10471000 let p = if has_rows && has_cols {
1048- r.read_tree(&PARTITION_TREE, probs)
1001+ w.r.read_tree(&PARTITION_TREE, probs)
10491002 } else if !has_rows && has_cols {
1050- if r.read_bool(probs[1]) { 3 } else { 1 }
1003+ if w.r.read_bool(probs[1]) { 3 } else { 1 }
10511004 } else if has_rows && !has_cols {
1052- if r.read_bool(probs[2]) { 3 } else { 2 }
1005+ if w.r.read_bool(probs[2]) { 3 } else { 2 }
10531006 } else {
10541007 3
10551008 };
10561009 // libvpx counts the implied SPLIT too (`++counts->partition[ctx][p]`).
1057- counts.partition[ctx][p as usize] += 1;
1010+ w.counts.partition[ctx][p as usize] += 1;
10581011 p
10591012 };
10601013 let bsize = bsize_of_n4(n4);
10611014 let subsize = SUBSIZE_LOOKUP[partition as usize * 13 + bsize] as usize;
10621015 if hbs == 0 {
1063- self.decode_block(
1064- r,
1065- mi,
1066- ectx,
1067- grids,
1068- level_of_seg,
1069- hdr,
1070- row,
1071- col,
1072- subsize,
1073- 1,
1074- 1,
1075- tile_col_start,
1076- tx_mode,
1077- counts,
1078- )?;
1016+ self.decode_block(w, row, col, subsize, 1, 1)?;
10791017 } else {
10801018 match partition {
1081- 0 => self.decode_block(
1082- r,
1083- mi,
1084- ectx,
1085- grids,
1086- level_of_seg,
1087- hdr,
1088- row,
1089- col,
1090- subsize,
1091- n4,
1092- n4,
1093- tile_col_start,
1094- tx_mode,
1095- counts,
1096- )?,
1019+ 0 => self.decode_block(w, row, col, subsize, n4, n4)?,
10971020 1 => {
1098- self.decode_block(
1099- r,
1100- mi,
1101- ectx,
1102- grids,
1103- level_of_seg,
1104- hdr,
1105- row,
1106- col,
1107- subsize,
1108- n4,
1109- n8,
1110- tile_col_start,
1111- tx_mode,
1112- counts,
1113- )?;
1021+ self.decode_block(w, row, col, subsize, n4, n8)?;
11141022 if has_rows {
1115- self.decode_block(
1116- r,
1117- mi,
1118- ectx,
1119- grids,
1120- level_of_seg,
1121- hdr,
1122- row + hbs,
1123- col,
1124- subsize,
1125- n4,
1126- n8,
1127- tile_col_start,
1128- tx_mode,
1129- counts,
1130- )?;
1023+ self.decode_block(w, row + hbs, col, subsize, n4, n8)?;
11311024 }
11321025 }
11331026 2 => {
1134- self.decode_block(
1135- r,
1136- mi,
1137- ectx,
1138- grids,
1139- level_of_seg,
1140- hdr,
1141- row,
1142- col,
1143- subsize,
1144- n8,
1145- n4,
1146- tile_col_start,
1147- tx_mode,
1148- counts,
1149- )?;
1027+ self.decode_block(w, row, col, subsize, n8, n4)?;
11501028 if has_cols {
1151- self.decode_block(
1152- r,
1153- mi,
1154- ectx,
1155- grids,
1156- level_of_seg,
1157- hdr,
1158- row,
1159- col + hbs,
1160- subsize,
1161- n8,
1162- n4,
1163- tile_col_start,
1164- tx_mode,
1165- counts,
1166- )?;
1029+ self.decode_block(w, row, col + hbs, subsize, n8, n4)?;
11671030 }
11681031 }
11691032 _ => {
@@ -1173,49 +1036,21 @@ impl Decoder {
11731036 (row + hbs, col),
11741037 (row + hbs, col + hbs),
11751038 ] {
1176- self.decode_partition(
1177- r,
1178- mi,
1179- ectx,
1180- grids,
1181- level_of_seg,
1182- hdr,
1183- rr,
1184- cc,
1185- n8,
1186- tile_col_start,
1187- tx_mode,
1188- counts,
1189- )?;
1039+ self.decode_partition(w, rr, cc, n8)?;
11901040 }
11911041 }
11921042 }
11931043 }
11941044 if bsize >= 3 && (bsize == 3 || partition != 3) {
1195- mi.update_partition_ctx(row, col, subsize, 1 << n8);
1045+ w.mi.update_partition_ctx(row, col, subsize, 1 << n8);
11961046 }
11971047 Ok(())
11981048 }
11991049
12001050 /// `decode_block` intra path (decodeframe.c:913).
1201- #[allow(clippy::too_many_arguments)]
1202- fn decode_block(
1203- &mut self,
1204- r: &mut crate::bool::BoolDecoder,
1205- mi: &mut MiState,
1206- ectx: &mut [PlaneContexts; 3],
1207- grids: &mut LfGrids,
1208- level_of_seg: &[[u8; 2]; 8],
1209- hdr: &FrameHeader,
1210- row: usize,
1211- col: usize,
1212- sb_type: usize,
1213- bwl: usize,
1214- bhl: usize,
1215- tile_col_start: usize,
1216- tx_mode: u8,
1217- counts: &mut crate::adapt::Counts,
1218- ) -> Result<()> {
1051+ fn decode_block(&mut self, w: &mut TileWalk<'_, '_>, row: usize, col: usize, sb_type: usize, bwl: usize, bhl: usize) -> Result<()> {
1052+ let TileWalk { r, mi, ectx, grids, level_of_seg, hdr, tile_col_start, tx_mode, counts } = w;
1053+ let (level_of_seg, hdr, tile_col_start, tx_mode) = (&*level_of_seg, *hdr, *tile_col_start, *tx_mode);
12191054 let bw = 1 << (bwl - 1);
12201055 let bh = 1 << (bhl - 1);
12211056 let x_mis = bw.min(mi.mi_cols - col);
@@ -1950,6 +1785,18 @@ fn interp_filter_of(hdr: &FrameHeader) -> u8 {
19501785 // the frame; the columns are then copied into the frame in column order. The
19511786 // counts are sums, so the result does not depend on the worker count.
19521787
1788+struct TileWalk<'a, 'd> {
1789+ r: &'a mut crate::bool::BoolDecoder<'d>,
1790+ mi: &'a mut MiState,
1791+ ectx: &'a mut [PlaneContexts; 3],
1792+ grids: &'a mut LfGrids,
1793+ level_of_seg: &'a [[u8; 2]; 8],
1794+ hdr: &'a FrameHeader,
1795+ tile_col_start: usize,
1796+ tx_mode: u8,
1797+ counts: &'a mut crate::adapt::Counts,
1798+}
1799+
19531800 /// Everything a tile walk reads that is fixed for the whole frame.
19541801 struct TileJob<'a> {
19551802 hdr: &'a FrameHeader,
@@ -1960,86 +1807,6 @@ struct TileJob<'a> {
19601807 tiles: &'a [&'a [u8]],
19611808 }
19621809
1963-/// Split the tile payloads (spec 6.4). Every tile but the last carries a
1964-/// 32-bit big-endian size (libvpx `get_tile_buffer`).
1965-fn split_tiles(mut data: &[u8], tile_cols: usize, tile_rows: usize) -> Result<Vec<&[u8]>> {
1966- let mut tiles = Vec::with_capacity(tile_cols * tile_rows);
1967- for tr in 0..tile_rows {
1968- for tc in 0..tile_cols {
1969- let last = tr == tile_rows - 1 && tc == tile_cols - 1;
1970- if last {
1971- tiles.push(data);
1972- } else {
1973- ensure(data.len() >= 4, "truncated tile size")?;
1974- let sz = u32::from_be_bytes([data[0], data[1], data[2], data[3]]) as usize;
1975- data = &data[4..];
1976- ensure(data.len() >= sz, "truncated tile data")?;
1977- tiles.push(&data[..sz]);
1978- data = &data[sz..];
1979- }
1980- }
1981- }
1982- Ok(tiles)
1983-}
1984-
1985-fn new_grids(mi_cols: usize, mi_rows: usize) -> LfGrids {
1986- LfGrids {
1987- level8: vec![0; mi_cols * mi_rows],
1988- blk: vec![(0u8, 0u32, 0u32); mi_cols * mi_rows],
1989- otx: vec![0; mi_cols * mi_rows],
1990- skip_inter: vec![false; mi_cols * mi_rows],
1991- seg: vec![0; mi_cols * mi_rows],
1992- mi_cols,
1993- mi_rows,
1994- }
1995-}
1996-
1997-/// Mode-info column span `[lo, hi)` of tile column `tc` (spec 6.4).
1998-fn tile_col_span(hdr: &FrameHeader, tc: usize) -> (usize, usize) {
1999- let mi_cols = hdr.mi_cols() as usize;
2000- let sb64_cols = mi_cols.div_ceil(SB_MI);
2001- let log2 = hdr.tile_info.cols_log2;
2002- let lo = (((sb64_cols * tc) >> log2) * SB_MI).min(mi_cols);
2003- let hi = (((sb64_cols * (tc + 1)) >> log2) * SB_MI).min(mi_cols);
2004- (lo, hi)
2005-}
2006-
2007-/// Copy columns `[lo, hi)` of every row of a mode-info array.
2008-fn copy_mi_cols<T: Copy>(dst: &mut [T], src: &[T], mi_cols: usize, mi_rows: usize, lo: usize, hi: usize) {
2009- for r in 0..mi_rows {
2010- let b = r * mi_cols;
2011- dst[b + lo..b + hi].copy_from_slice(&src[b + lo..b + hi]);
2012- }
2013-}
2014-
2015-/// Copy the pixel columns of one tile column. The last tile column also owns
2016-/// the padding past `mi_cols` (a block at the frame edge writes its full size).
2017-/// Padded (64-multiple) luma width of a frame.
2018-fn frame_aw(hdr: &FrameHeader) -> usize {
2019- (hdr.width as usize).div_ceil(64) * 64
2020-}
2021-
2022-/// Padded (64-multiple) luma height of a frame.
2023-fn frame_ah(hdr: &FrameHeader) -> usize {
2024- (hdr.height as usize).div_ceil(64) * 64
2025-}
2026-
2027-fn copy_plane_cols(dst: &mut Planes, src: &Planes, lo_mi: usize, hi_mi: usize, last: bool) {
2028- fn rows<T: Copy>(dst: &mut [T], src: &[T], stride: usize, rows: usize, x0: usize, x1: usize) {
2029- for r in 0..rows {
2030- let b = r * stride;
2031- dst[b + x0..b + x1].copy_from_slice(&src[b + x0..b + x1]);
2032- }
2033- }
2034- let x0 = lo_mi * 8;
2035- let x1 = if last { dst.ys } else { hi_mi * 8 };
2036- rows(&mut dst.y, &src.y, dst.ys, dst.ah, x0, x1);
2037- let (cx0, cx1) = (x0 >> dst.ss_x, if last { dst.uvs } else { x1 >> dst.ss_x });
2038- let ch = dst.ah >> dst.ss_y;
2039- rows(&mut dst.u, &src.u, dst.uvs, ch, cx0, cx1);
2040- rows(&mut dst.v, &src.v, dst.uvs, ch, cx0, cx1);
2041-}
2042-
20431810 impl Decoder {
20441811 /// A frame buffer of this geometry, reusing a spare when one fits.
20451812 fn frame_planes(&mut self, aw: usize, ah: usize, bd: u8, ss_x: usize, ss_y: usize) -> Planes {
@@ -2184,20 +1951,18 @@ impl Decoder {
21841951 ectx.iter_mut().for_each(|p| p.left = [0; 32]);
21851952 mi.left_seg = [0; 32];
21861953 for sb_col in (col_lo..col_hi).step_by(SB_MI) {
2187- self.decode_partition(
2188- &mut r,
2189- &mut mi,
2190- &mut ectx,
2191- grids,
2192- &job.level_of_seg,
1954+ let mut w = TileWalk {
1955+ r: &mut r,
1956+ mi: &mut mi,
1957+ ectx: &mut ectx,
1958+ grids: &mut *grids,
1959+ level_of_seg: &job.level_of_seg,
21931960 hdr,
2194- sb_row,
2195- sb_col,
2196- 4,
2197- col_lo,
2198- job.tx_mode,
2199- counts,
2200- )?;
1961+ tile_col_start: col_lo,
1962+ tx_mode: job.tx_mode,
1963+ counts: &mut *counts,
1964+ };
1965+ self.decode_partition(&mut w, sb_row, sb_col, 4)?;
22011966 }
22021967 }
22031968 ensure(
crates/ec-vp9/src/decode/tiles.rsadded+85 -0
@@ -0,0 +1,85 @@
1+use super::{Planes, SB_MI};
2+use crate::loopfilter::LfGrids;
3+use crate::tables::ensure;
4+use crate::Result;
5+use ec_vp9_syntax::FrameHeader;
6+
7+/// Split the tile payloads (spec 6.4). Every tile but the last carries a
8+/// 32-bit big-endian size (libvpx `get_tile_buffer`).
9+pub(super) fn split_tiles(mut data: &[u8], tile_cols: usize, tile_rows: usize) -> Result<Vec<&[u8]>> {
10+ let mut tiles = Vec::with_capacity(tile_cols * tile_rows);
11+ for tr in 0..tile_rows {
12+ for tc in 0..tile_cols {
13+ let last = tr == tile_rows - 1 && tc == tile_cols - 1;
14+ if last {
15+ tiles.push(data);
16+ } else {
17+ ensure(data.len() >= 4, "truncated tile size")?;
18+ let sz = u32::from_be_bytes([data[0], data[1], data[2], data[3]]) as usize;
19+ data = &data[4..];
20+ ensure(data.len() >= sz, "truncated tile data")?;
21+ tiles.push(&data[..sz]);
22+ data = &data[sz..];
23+ }
24+ }
25+ }
26+ Ok(tiles)
27+}
28+
29+pub(super) fn new_grids(mi_cols: usize, mi_rows: usize) -> LfGrids {
30+ LfGrids {
31+ level8: vec![0; mi_cols * mi_rows],
32+ blk: vec![(0u8, 0u32, 0u32); mi_cols * mi_rows],
33+ otx: vec![0; mi_cols * mi_rows],
34+ skip_inter: vec![false; mi_cols * mi_rows],
35+ seg: vec![0; mi_cols * mi_rows],
36+ mi_cols,
37+ mi_rows,
38+ }
39+}
40+
41+/// Mode-info column span `[lo, hi)` of tile column `tc` (spec 6.4).
42+pub(super) fn tile_col_span(hdr: &FrameHeader, tc: usize) -> (usize, usize) {
43+ let mi_cols = hdr.mi_cols() as usize;
44+ let sb64_cols = mi_cols.div_ceil(SB_MI);
45+ let log2 = hdr.tile_info.cols_log2;
46+ let lo = (((sb64_cols * tc) >> log2) * SB_MI).min(mi_cols);
47+ let hi = (((sb64_cols * (tc + 1)) >> log2) * SB_MI).min(mi_cols);
48+ (lo, hi)
49+}
50+
51+/// Copy columns `[lo, hi)` of every row of a mode-info array.
52+pub(super) fn copy_mi_cols<T: Copy>(dst: &mut [T], src: &[T], mi_cols: usize, mi_rows: usize, lo: usize, hi: usize) {
53+ for r in 0..mi_rows {
54+ let b = r * mi_cols;
55+ dst[b + lo..b + hi].copy_from_slice(&src[b + lo..b + hi]);
56+ }
57+}
58+
59+/// Padded (64-multiple) luma width of a frame.
60+pub(super) fn frame_aw(hdr: &FrameHeader) -> usize {
61+ (hdr.width as usize).div_ceil(64) * 64
62+}
63+
64+/// Padded (64-multiple) luma height of a frame.
65+pub(super) fn frame_ah(hdr: &FrameHeader) -> usize {
66+ (hdr.height as usize).div_ceil(64) * 64
67+}
68+
69+/// Copy the pixel columns of one tile column. The last tile column also owns
70+/// the padding past `mi_cols` (a block at the frame edge writes its full size).
71+pub(super) fn copy_plane_cols(dst: &mut Planes, src: &Planes, lo_mi: usize, hi_mi: usize, last: bool) {
72+ fn rows<T: Copy>(dst: &mut [T], src: &[T], stride: usize, rows: usize, x0: usize, x1: usize) {
73+ for r in 0..rows {
74+ let b = r * stride;
75+ dst[b + x0..b + x1].copy_from_slice(&src[b + x0..b + x1]);
76+ }
77+ }
78+ let x0 = lo_mi * 8;
79+ let x1 = if last { dst.ys } else { hi_mi * 8 };
80+ rows(&mut dst.y, &src.y, dst.ys, dst.ah, x0, x1);
81+ let (cx0, cx1) = (x0 >> dst.ss_x, if last { dst.uvs } else { x1 >> dst.ss_x });
82+ let ch = dst.ah >> dst.ss_y;
83+ rows(&mut dst.u, &src.u, dst.uvs, ch, cx0, cx1);
84+ rows(&mut dst.v, &src.v, dst.uvs, ch, cx0, cx1);
85+}
crates/ec-vp9/src/inter.rsmodified+5 -15
@@ -40,9 +40,7 @@ const MV_BORDER: i32 = 16 << 3;
4040 const MV_HP_THRESH: i32 = 64;
4141 const CLASS0_SIZE: i32 = 2;
4242 const CLASS0_BITS: i32 = 1;
43-/// `SEG_LVL_SKIP` / `SEG_LVL_REF_FRAME`.
44-const SEG_LVL_REF_FRAME: usize = 2;
45-const SEG_LVL_SKIP: usize = 3;
43+use ec_vp9_syntax::{SEG_LVL_REF_FRAME, SEG_LVL_SKIP};
4644
4745 /// `mv_ref_blocks[BLOCK_SIZES]` (`vp9_mvref_common.h`): `(row, col)` offsets,
4846 /// transcribed verbatim from the C. libvpx's `POSITION` is `{ row, col }`, so
@@ -279,14 +277,6 @@ fn read_inter_segment_id(
279277 segment_id
280278 }
281279
282-fn seg_feature(seg: &ec_vp9_syntax::SegmentationParams, segment_id: u8, feature: usize) -> bool {
283- seg.enabled
284- && seg
285- .feature_enabled
286- .get(segment_id as usize)
287- .is_some_and(|f| f[feature])
288-}
289-
290280 /// `read_skip`.
291281 fn read_skip(
292282 r: &mut BoolDecoder,
@@ -296,7 +286,7 @@ fn read_skip(
296286 nb: Nb<'_>,
297287 counts: &mut Counts,
298288 ) -> bool {
299- if seg_feature(c.seg, segment_id, SEG_LVL_SKIP) {
289+ if c.seg.feature_active(segment_id, SEG_LVL_SKIP) {
300290 true
301291 } else {
302292 let ctx = usize::from(nb.above.is_some_and(|m| m.skip))
@@ -333,7 +323,7 @@ fn read_is_inter_block(
333323 nb: Nb<'_>,
334324 counts: &mut Counts,
335325 ) -> bool {
336- if seg_feature(c.seg, segment_id, SEG_LVL_REF_FRAME) {
326+ if c.seg.feature_active(segment_id, SEG_LVL_REF_FRAME) {
337327 c.seg.feature_data[segment_id as usize][SEG_LVL_REF_FRAME] != INTRA_FRAME as i16
338328 } else {
339329 let ctx = intra_inter_context(nb);
@@ -1144,7 +1134,7 @@ fn read_ref_frames(
11441134 out: &mut [i8; 2],
11451135 counts: &mut Counts,
11461136 ) {
1147- if seg_feature(c.seg, segment_id, SEG_LVL_REF_FRAME) {
1137+ if c.seg.feature_active(segment_id, SEG_LVL_REF_FRAME) {
11481138 out[0] = c.seg.feature_data[segment_id as usize][SEG_LVL_REF_FRAME] as i8;
11491139 out[1] = NO_REF_FRAME;
11501140 return;
@@ -1193,7 +1183,7 @@ fn read_inter_block_mode_info(
11931183 let is_compound = usize::from(info.ref_frame[1] > INTRA_FRAME);
11941184 let inter_mode_ctx = get_mode_context(st, c, bsize, row, col);
11951185
1196- if seg_feature(c.seg, info.segment, SEG_LVL_SKIP) {
1186+ if c.seg.feature_active(info.segment, SEG_LVL_SKIP) {
11971187 info.mode = ZEROMV;
11981188 if bsize < 3 {
11991189 return Err(crate::Error::corrupt(
crates/ec-vp9/src/loopfilter.rsmodified+11 -22
@@ -75,6 +75,7 @@ const LUMA_ABOVE_TXF: [u64; 4] = [u64::MAX, u64::MAX, 0x00ff00ff00ff00ff, 0x0000
7575 const LUMA_LEFT_BORDER: u64 = 0x1111111111111111;
7676 const LUMA_ABOVE_BORDER: u64 = 0x000000ff000000ff;
7777
78+use crate::decode::Planes;
7879 use crate::Sample;
7980
8081 /// `signed_char_clamp_high` (`vpx_dsp/loopfilter.c:23`): clamp to
@@ -1192,23 +1193,10 @@ impl LfGrids {
11921193 /// `vp9_loop_filter_frame` (vp9_loopfilter.c:1430): luma always takes the
11931194 /// ss00 path; chroma takes ss00 for 4:4:4, ss11 for 4:2:0, and the
11941195 /// non420 slow path otherwise (4:2:2 / 4:4:0).
1195- #[allow(clippy::too_many_arguments)]
1196- pub(crate) fn filter_frame(
1197- &self,
1198- y: &mut [Sample],
1199- stride: usize,
1200- u: &mut [Sample],
1201- v: &mut [Sample],
1202- uv_stride: usize,
1203- y_w: usize,
1204- y_h: usize,
1205- uv_w: usize,
1206- uv_h: usize,
1207- ss_x: usize,
1208- ss_y: usize,
1209- sharpness: u8,
1210- bd: u8,
1211- ) {
1196+ pub(crate) fn filter_frame(&self, p: &mut Planes, vw: usize, vh: usize, sharpness: u8) {
1197+ let (ss_x, ss_y, bd) = (p.ss_x, p.ss_y, p.bd);
1198+ let (uv_w, uv_h) = (vw >> ss_x, vh >> ss_y);
1199+ let (stride, uv_stride) = (p.ys, p.uvs);
12121200 // The three planes are filtered from the same read-only grids and touch
12131201 // disjoint buffers, so they run concurrently: luma on one thread, both
12141202 // chroma planes on another. Each plane's edge order is unchanged, so the
@@ -1226,8 +1214,8 @@ impl LfGrids {
12261214 }
12271215 };
12281216 std::thread::scope(|scope| {
1229- scope.spawn(|| chroma(u, v));
1230- self.filter_ss00_plane(y, stride, y_w, y_h, sharpness, bd);
1217+ scope.spawn(|| chroma(&mut p.u, &mut p.v));
1218+ self.filter_ss00_plane(&mut p.y, stride, vw, vh, sharpness, bd);
12311219 });
12321220 }
12331221 }
@@ -1253,9 +1241,10 @@ mod tests {
12531241 }
12541242
12551243 fn run(g: &LfGrids, y: &mut [Sample]) {
1256- let mut u = vec![128u16; 32 * 32];
1257- let mut v = vec![128u16; 32 * 32];
1258- g.filter_frame(y, 64, &mut u, &mut v, 32, 64, 64, 32, 32, 1, 1, 0, 8);
1244+ let mut p = Planes::new(64, 64, 8, 1, 1);
1245+ p.y.copy_from_slice(y);
1246+ g.filter_frame(&mut p, 64, 64, 0);
1247+ y.copy_from_slice(&p.y);
12591248 }
12601249
12611250 /// The luma internal-4x4 horizontal edge fires on the frame's LAST MI row.
crates/ec-vp9/src/modes.rsmodified+2 -6
@@ -5,6 +5,7 @@
55 use crate::bool::BoolDecoder;
66 use crate::header::FrameContext;
77 use crate::tables::*;
8+use ec_vp9_syntax::SEG_LVL_SKIP;
89
910 /// One block's parsed prediction record (`MODE_INFO`).
1011 #[derive(Clone, Default)]
@@ -155,12 +156,7 @@ pub(crate) fn read_intra_frame_mode_info(
155156 let skip_ctx = above_mi.as_ref().map_or(0, |m| u32::from(m.skip))
156157 + left_mi.as_ref().map_or(0, |u| u32::from(u.skip));
157158 let skip_ctx = skip_ctx as usize;
158- let seg_skip = seg.enabled
159- && seg
160- .feature_enabled
161- .get(segment as usize)
162- .is_some_and(|f| f[3]);
163- let skip = if seg_skip {
159+ let skip = if seg.feature_active(segment, SEG_LVL_SKIP) {
164160 true
165161 } else {
166162 r.read_bool(fc.skip[skip_ctx])