From 4288c9226a2226993213ec447d5fb0781f45d1cd Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Ahmet=20Kaan=20G=C3=BCm=C3=BC=C5=9F?= Date: Wed, 2 Dec 2026 13:36:27 +0300 Subject: [PATCH] U4: ec-vp9 drop the decode_syntax syntax-only walk: pixels flag, frames_seen, decode_index, syntax_blocks, last_frame_blocks, InterWalk block count and frame index, and the unused inter token walk --- crates/ec-vp9/src/decode.rs | 274 +++++++----------------------------- 1 file changed, 48 insertions(+), 226 deletions(-) diff --git a/crates/ec-vp9/src/decode.rs b/crates/ec-vp9/src/decode.rs index b84f64a2..0600c4cb 100644 --- a/crates/ec-vp9/src/decode.rs +++ b/crates/ec-vp9/src/decode.rs @@ -199,13 +199,6 @@ pub struct Decoder { 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, @@ -263,9 +256,6 @@ struct InterWalk { 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 { @@ -285,21 +275,12 @@ impl Decoder { 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: default_threads(), } } - /// 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> { @@ -322,17 +303,13 @@ impl Decoder { }; } 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 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) + self.decode_inter_frame(&hdr, frame) } /// Decode one key frame through to pixels and refresh every slot. @@ -448,48 +425,10 @@ impl Decoder { 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> { + /// Inter-frame decode: compressed header, the tile walk (mode info, MVs, + /// tokens, prediction, residual and the loop filter), then the reference + /// refresh. + fn decode_inter_frame(&mut self, hdr: &FrameHeader, frame: &[u8]) -> 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`): @@ -508,24 +447,23 @@ impl Decoder { // 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}" - ))); - } + 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 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; let ch = read_compressed_header( @@ -578,8 +516,6 @@ impl Decoder { 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 @@ -589,7 +525,6 @@ impl Decoder { let job = TileJob { hdr, tx_mode: ch.tx_mode, - pixels, level_of_seg: [[0u8; 2]; 8], tiles: &tiles, }; @@ -620,9 +555,6 @@ impl Decoder { 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 { @@ -641,10 +573,6 @@ impl Decoder { self.frame_ctxs[hdr.frame_context_idx as usize] = self.ctx.as_ref().clone(); } - 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). @@ -752,7 +680,6 @@ impl Decoder { &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; @@ -768,82 +695,6 @@ impl Decoder { 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 @@ -1114,7 +965,6 @@ impl Decoder { let job = TileJob { hdr, tx_mode, - pixels: true, level_of_seg, tiles: &tiles, }; @@ -1177,10 +1027,6 @@ impl Decoder { 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 { @@ -1228,7 +1074,7 @@ impl Decoder { 1, tile_col_start, tx_mode, - pixels, counts, + counts, )?; } else { match partition { @@ -1246,7 +1092,7 @@ impl Decoder { n4, tile_col_start, tx_mode, - pixels, counts, + counts, )?, 1 => { self.decode_block( @@ -1263,7 +1109,7 @@ impl Decoder { n8, tile_col_start, tx_mode, - pixels, counts, + counts, )?; if has_rows { self.decode_block( @@ -1280,7 +1126,7 @@ impl Decoder { n8, tile_col_start, tx_mode, - pixels, counts, + counts, )?; } } @@ -1299,7 +1145,7 @@ impl Decoder { n4, tile_col_start, tx_mode, - pixels, counts, + counts, )?; if has_cols { self.decode_block( @@ -1316,7 +1162,7 @@ impl Decoder { n4, tile_col_start, tx_mode, - pixels, counts, + counts, )?; } } @@ -1339,7 +1185,7 @@ impl Decoder { n8, tile_col_start, tx_mode, - pixels, counts, + counts, )?; } } @@ -1368,7 +1214,6 @@ impl Decoder { bhl: usize, tile_col_start: usize, tx_mode: u8, - pixels: bool, counts: &mut crate::adapt::Counts, ) -> Result<()> { let bw = 1 << (bwl - 1); @@ -1421,39 +1266,27 @@ impl Decoder { )? }; 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; - } + // 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; } for dy in 0..y_mis { for dx in 0..x_mis { @@ -2121,8 +1954,6 @@ fn interp_filter_of(hdr: &FrameHeader) -> u8 { 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]`. @@ -2304,9 +2135,6 @@ impl Decoder { 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); } @@ -2370,7 +2198,6 @@ impl Decoder { 4, col_lo, job.tx_mode, - job.pixels, counts, )?; } @@ -2407,12 +2234,7 @@ impl Decoder { 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,