vp9: decode inter-frame pixels byte-exactly (MC, residual, loop filter, ref slots)
Browse files at this commit83125b413895baa34518de37b10cda1f211277b4Parent: a2b95c39 changed files +1861 -152
- crates/ec-vp9/src/decode.rs +758 -127
- crates/ec-vp9/src/inter.rs +7 -0
- crates/ec-vp9/src/lib.rs +8 -3
- crates/ec-vp9/src/loopfilter.rs +59 -5
- crates/ec-vp9/src/mc.rs +558 -0
- crates/ec-vp9/tests/inter_pixels_exact.rs +131 -0
- crates/ec-vp9/tests/keyframe_exact.rs +31 -17
- crates/ec-vp9/tests/scratch_pixdump.rs +77 -0
- lanes/vp9pix.report.md +232 -0
crates/ec-vp9/src/decode.rsmodified+758 -127
| @@ -4,14 +4,17 @@ | |||
| 4 | //! reconstruction and the loop filter (spec 6, 7, 8; libvpx | 4 | //! reconstruction and the loop filter (spec 6, 7, 8; libvpx |
| 5 | //! `vp9_decodeframe.c`). | 5 | //! `vp9_decodeframe.c`). |
| 6 | //! | 6 | //! |
| 7 | -//! Lane scope: profile-0 8-bit 4:2:0 keyframes. Inter frames and other | 7 | +//! Capability: profile-0 8-bit 4:2:0 keyframes and inter frames (motion |
| 8 | -//! profiles/subsamplings are refused by name; `show_existing_frame` | 8 | +//! compensation, residual reconstruction, the loop filter and an 8-slot |
| 9 | -//! returns the buffered reference picture instead of pretending to | 9 | +//! reference DPB). Intra-only frames, other profiles/subsamplings, a |
| 10 | -//! decode. | 10 | +//! reference whose coded size differs from the current frame, and odd frame |
| 11 | +//! dimensions are refused by name; `show_existing_frame` returns the | ||
| 12 | +//! buffered reference picture instead of pretending to decode. | ||
| 11 | 13 | ||
| 12 | use crate::header::{FrameContext, read_compressed_header}; | 14 | use crate::header::{FrameContext, read_compressed_header}; |
| 13 | use crate::intra::build_intra_predictors; | 15 | use crate::intra::build_intra_predictors; |
| 14 | use crate::loopfilter::LfGrids; | 16 | use crate::loopfilter::LfGrids; |
| 17 | +use crate::mc::{RefPlane, average_split_mvs_chroma, build_inter_predictors, split_mv}; | ||
| 15 | use crate::modes::{MiInfo, MiState, read_intra_frame_mode_info}; | 18 | use crate::modes::{MiInfo, MiState, read_intra_frame_mode_info}; |
| 16 | use crate::tables::*; | 19 | use crate::tables::*; |
| 17 | use crate::tokens::{PlaneContexts, decode_coefs, scan_for}; | 20 | use crate::tokens::{PlaneContexts, decode_coefs, scan_for}; |
| @@ -51,6 +54,14 @@ struct Planes { | |||
| 51 | } | 54 | } |
| 52 | 55 | ||
| 53 | impl Planes { | 56 | impl Planes { |
| 57 | + /// Immutable plane + stride (the predictor reads references). | ||
| 58 | + fn plane_ref(&self, plane: usize) -> (&[u8], usize) { | ||
| 59 | + match plane { | ||
| 60 | + 0 => (&self.y, self.ys), | ||
| 61 | + 1 => (&self.u, self.uvs), | ||
| 62 | + _ => (&self.v, self.uvs), | ||
| 63 | + } | ||
| 64 | + } | ||
| 54 | fn plane(&mut self, plane: usize) -> (&mut [u8], usize) { | 65 | fn plane(&mut self, plane: usize) -> (&mut [u8], usize) { |
| 55 | match plane { | 66 | match plane { |
| 56 | 0 => (&mut self.y, self.ys), | 67 | 0 => (&mut self.y, self.ys), |
| @@ -73,7 +84,7 @@ impl Planes { | |||
| 73 | pub struct Decoder { | 84 | pub struct Decoder { |
| 74 | parser: Vp9Parser, | 85 | parser: Vp9Parser, |
| 75 | ctx: FrameContext, | 86 | ctx: FrameContext, |
| 76 | - refs: [Option<Picture>; 8], | 87 | + refs: [Option<RefFrame>; 8], |
| 77 | /// Frame scratch, live only inside `decode_keyframe`. | 88 | /// Frame scratch, live only inside `decode_keyframe`. |
| 78 | planes: Option<Planes>, | 89 | planes: Option<Planes>, |
| 79 | /// The four stored frame contexts (spec 6.2 `frame_context_idx`). | 90 | /// The four stored frame contexts (spec 6.2 `frame_context_idx`). |
| @@ -87,8 +98,36 @@ pub struct Decoder { | |||
| 87 | iw: Option<InterWalk>, | 98 | iw: Option<InterWalk>, |
| 88 | /// Frames parsed by `decode_syntax` (the dump's frame index). | 99 | /// Frames parsed by `decode_syntax` (the dump's frame index). |
| 89 | frames_seen: usize, | 100 | frames_seen: usize, |
| 101 | + /// Frames decoded by `decode` (the dumps' `frame=` field for this path; | ||
| 102 | + /// `decode_syntax` counts the same way in `frames_seen`). | ||
| 103 | + decode_index: usize, | ||
| 90 | /// Mode-info blocks parsed in the most recent inter frame (0 otherwise). | 104 | /// Mode-info blocks parsed in the most recent inter frame (0 otherwise). |
| 91 | syntax_blocks: usize, | 105 | syntax_blocks: usize, |
| 106 | + /// `twd->extend_and_predict_buf`: the per-prediction bordered reference | ||
| 107 | + /// window built when a block's read leaves the reference frame. | ||
| 108 | + mc_scratch: Vec<u8>, | ||
| 109 | + /// The `vpx_convolve8` stride-64 intermediate plus the second scratch its | ||
| 110 | + /// averaging variant needs. | ||
| 111 | + mc_temp: Vec<u8>, | ||
| 112 | +} | ||
| 113 | + | ||
| 114 | +/// One DPB slot: a decoded frame buffer at its ALIGNED (64-multiple) extent | ||
| 115 | +/// plus the coded size it was decoded at. | ||
| 116 | +/// | ||
| 117 | +/// The aligned extent is load-bearing, not slack: libvpx writes a block at the | ||
| 118 | +/// frame edge at its FULL size (`decodeframe.c:1216`), so the rows past the | ||
| 119 | +/// coded height hold real reconstructed samples, and `dec_build_inter_predictors` | ||
| 120 | +/// reads them directly whenever a block's window is inside the frame with a | ||
| 121 | +/// whole-pixel MV (`build_mc_border` is only reached when the window leaves the | ||
| 122 | +/// frame or the coded size is not a multiple of 8). Cropping the reference to | ||
| 123 | +/// the visible size would drop exactly those samples. | ||
| 124 | +#[derive(Clone)] | ||
| 125 | +struct RefFrame { | ||
| 126 | + planes: std::sync::Arc<Planes>, | ||
| 127 | + /// Coded width — the predictor's `y_crop_width`. | ||
| 128 | + width: u16, | ||
| 129 | + /// Coded height — the predictor's `y_crop_height`. | ||
| 130 | + height: u16, | ||
| 92 | } | 131 | } |
| 93 | 132 | ||
| 94 | /// What `use_prev_frame_mvs` (decodeframe.c:3023) tests about the last frame. | 133 | /// What `use_prev_frame_mvs` (decodeframe.c:3023) tests about the last frame. |
| @@ -133,7 +172,10 @@ impl Decoder { | |||
| 133 | last: LastFrameFacts::default(), | 172 | last: LastFrameFacts::default(), |
| 134 | iw: None, | 173 | iw: None, |
| 135 | frames_seen: 0, | 174 | frames_seen: 0, |
| 175 | + decode_index: 0, | ||
| 136 | syntax_blocks: 0, | 176 | syntax_blocks: 0, |
| 177 | + mc_scratch: Vec::new(), | ||
| 178 | + mc_temp: vec![0; 64 * 135 + 64 * 64], | ||
| 137 | } | 179 | } |
| 138 | } | 180 | } |
| 139 | 181 | ||
| @@ -160,7 +202,7 @@ impl Decoder { | |||
| 160 | if hdr.show_existing_frame { | 202 | if hdr.show_existing_frame { |
| 161 | let slot = hdr.frame_to_show_map_idx as usize; | 203 | let slot = hdr.frame_to_show_map_idx as usize; |
| 162 | return match self.refs.get(slot).and_then(|p| p.as_ref()) { | 204 | return match self.refs.get(slot).and_then(|p| p.as_ref()) { |
| 163 | - Some(p) => Ok(Some(p.clone())), | 205 | + Some(p) => Ok(Some(crop_picture(&p.planes, p.width, p.height))), |
| 164 | None => Err(corrupt("show_existing_frame names an empty slot")), | 206 | None => Err(corrupt("show_existing_frame names an empty slot")), |
| 165 | }; | 207 | }; |
| 166 | } | 208 | } |
| @@ -179,13 +221,20 @@ impl Decoder { | |||
| 179 | ), | 221 | ), |
| 180 | )); | 222 | )); |
| 181 | } | 223 | } |
| 182 | - if hdr.frame_type != FrameType::Key { | 224 | + // One index per decoded frame (the dumps' `frame=` field), so this |
| 225 | + // path and `decode_syntax` label the same frame the same way. | ||
| 226 | + let index = self.decode_index; | ||
| 227 | + self.decode_index += 1; | ||
| 228 | + if hdr.frame_type == FrameType::Key { | ||
| 229 | + return self.decode_key_frame(&hdr, frame); | ||
| 230 | + } | ||
| 231 | + if hdr.intra_only { | ||
| 183 | return Err(Error::unsupported( | 232 | return Err(Error::unsupported( |
| 184 | - "vp9 inter", | 233 | + "vp9 intra-only", |
| 185 | - "this lane decodes keyframes only", | 234 | + "this lane decodes inter frames and keyframes only", |
| 186 | )); | 235 | )); |
| 187 | } | 236 | } |
| 188 | - self.decode_key_frame(&hdr, frame) | 237 | + self.decode_inter_frame(&hdr, frame, index, true) |
| 189 | } | 238 | } |
| 190 | 239 | ||
| 191 | /// Decode one key frame through to pixels and refresh every slot. | 240 | /// Decode one key frame through to pixels and refresh every slot. |
| @@ -233,8 +282,13 @@ impl Decoder { | |||
| 233 | self.frame_ctxs[hdr.frame_context_idx as usize] = self.ctx.clone(); | 282 | self.frame_ctxs[hdr.frame_context_idx as usize] = self.ctx.clone(); |
| 234 | } | 283 | } |
| 235 | // Reference refresh (spec 8.10): a keyframe updates all slots. | 284 | // Reference refresh (spec 8.10): a keyframe updates all slots. |
| 285 | + let frame = RefFrame { | ||
| 286 | + planes: std::sync::Arc::new(self.planes.take().expect("frame scratch")), | ||
| 287 | + width: hdr.width as u16, | ||
| 288 | + height: hdr.height as u16, | ||
| 289 | + }; | ||
| 236 | for slot in self.refs.iter_mut() { | 290 | for slot in self.refs.iter_mut() { |
| 237 | - *slot = Some(pic.clone()); | 291 | + *slot = Some(frame.clone()); |
| 238 | } | 292 | } |
| 239 | Ok(if hdr.show_frame { Some(pic) } else { None }) | 293 | Ok(if hdr.show_frame { Some(pic) } else { None }) |
| 240 | } | 294 | } |
| @@ -267,16 +321,26 @@ impl Decoder { | |||
| 267 | "the inter-syntax lane covers inter frames only", | 321 | "the inter-syntax lane covers inter frames only", |
| 268 | )); | 322 | )); |
| 269 | } else { | 323 | } else { |
| 270 | - self.decode_inter_syntax_frame(&hdr, sub, index)?; | 324 | + self.decode_inter_frame(&hdr, sub, index, false)?; |
| 271 | } | 325 | } |
| 272 | } | 326 | } |
| 273 | Ok(()) | 327 | Ok(()) |
| 274 | } | 328 | } |
| 275 | 329 | ||
| 276 | - /// Inter-frame syntax walk: compressed header, then every block's mode | 330 | + /// Inter-frame decode: compressed header, the tile walk (mode info, MVs |
| 277 | - /// info and MVs. Tokens are decoded to keep the entropy stream aligned; | 331 | + /// and tokens always; prediction, residual and the loop filter when |
| 278 | - /// nothing is predicted or reconstructed. | 332 | + /// `pixels`), then the reference refresh. |
| 279 | - fn decode_inter_syntax_frame(&mut self, hdr: &FrameHeader, frame: &[u8], index: usize) -> Result<()> { | 333 | + /// |
| 334 | + /// `pixels == false` is the syntax-only walk the instrumented harnesses | ||
| 335 | + /// drive: tokens keep the entropy stream aligned, no pixel is touched and | ||
| 336 | + /// the reference slots are left alone. | ||
| 337 | + fn decode_inter_frame( | ||
| 338 | + &mut self, | ||
| 339 | + hdr: &FrameHeader, | ||
| 340 | + frame: &[u8], | ||
| 341 | + index: usize, | ||
| 342 | + pixels: bool, | ||
| 343 | + ) -> Result<Option<Picture>> { | ||
| 280 | // Frame-context bookkeeping (spec 6.2 / libvpx `vp9_decode_frame`): | 344 | // Frame-context bookkeeping (spec 6.2 / libvpx `vp9_decode_frame`): |
| 281 | // 3 resets every stored context, 2 resets the selected one; 1 means | 345 | // 3 resets every stored context, 2 resets the selected one; 1 means |
| 282 | // "reset after this frame" and takes no action at frame start. | 346 | // "reset after this frame" and takes no action at frame start. |
| @@ -288,6 +352,58 @@ impl Decoder { | |||
| 288 | } | 352 | } |
| 289 | self.ctx = self.frame_ctxs[hdr.frame_context_idx as usize].clone(); | 353 | self.ctx = self.frame_ctxs[hdr.frame_context_idx as usize].clone(); |
| 290 | 354 | ||
| 355 | + // Reference slots (spec 7.2): `ref_frame_idx[i]` names the slot LAST, | ||
| 356 | + // GOLDEN and ALTREF use. libvpx validates every named reference's | ||
| 357 | + // format here (`vp9_decodeframe.c:1620-1627`) and SCALES a reference | ||
| 358 | + // whose size differs; this lane refuses that by name. | ||
| 359 | + if pixels { | ||
| 360 | + for (i, name) in ["last", "golden", "altref"].iter().enumerate() { | ||
| 361 | + let slot = hdr.ref_frame_idx[i] as usize; | ||
| 362 | + if slot >= self.refs.len() || hdr.ref_frame_idx[i] >= 8 { | ||
| 363 | + return Err(corrupt("vp9 inter names a reference slot past the DPB")); | ||
| 364 | + } | ||
| 365 | + let Some(p) = self.refs[slot].as_ref() else { | ||
| 366 | + return Err(Error::corrupt(format!( | ||
| 367 | + "vp9 inter reference `{name}` names empty slot {slot}" | ||
| 368 | + ))); | ||
| 369 | + }; | ||
| 370 | + if p.width as u32 != hdr.width || p.height as u32 != hdr.height { | ||
| 371 | + return Err(Error::unsupported( | ||
| 372 | + "vp9 inter reference size change", | ||
| 373 | + format!( | ||
| 374 | + "reference `{name}` in slot {slot} is {}x{} but this frame is {}x{}; \ | ||
| 375 | + reference-frame scaling is not ported", | ||
| 376 | + p.width, p.height, hdr.width, hdr.height | ||
| 377 | + ), | ||
| 378 | + )); | ||
| 379 | + } | ||
| 380 | + } | ||
| 381 | + if hdr.width % 2 != 0 || hdr.height % 2 != 0 { | ||
| 382 | + return Err(Error::unsupported( | ||
| 383 | + "vp9 inter odd frame dimensions", | ||
| 384 | + format!( | ||
| 385 | + "{}x{}: the reference chroma planes of an odd-sized frame are \ | ||
| 386 | + stored at floor(w/2) but the predictor reads libvpx's \ | ||
| 387 | + uv_crop_width = (w+1)/2", | ||
| 388 | + hdr.width, hdr.height | ||
| 389 | + ), | ||
| 390 | + )); | ||
| 391 | + } | ||
| 392 | + let width = hdr.width as usize; | ||
| 393 | + let height = hdr.height as usize; | ||
| 394 | + let aw = width.div_ceil(64) * 64; | ||
| 395 | + let ah = height.div_ceil(64) * 64; | ||
| 396 | + self.planes = Some(Planes { | ||
| 397 | + y: vec![128u8; aw * ah], | ||
| 398 | + u: vec![128u8; aw / 2 * ah / 2], | ||
| 399 | + v: vec![128u8; aw / 2 * ah / 2], | ||
| 400 | + ys: aw, | ||
| 401 | + uvs: aw / 2, | ||
| 402 | + aw, | ||
| 403 | + ah, | ||
| 404 | + }); | ||
| 405 | + } | ||
| 406 | + | ||
| 291 | let hdr_start = hdr.uncompressed_header_size as usize; | 407 | let hdr_start = hdr.uncompressed_header_size as usize; |
| 292 | if crate::trace_enabled() { | 408 | if crate::trace_enabled() { |
| 293 | eprintln!("CHSTART frame={index}"); | 409 | eprintln!("CHSTART frame={index}"); |
| @@ -368,15 +484,33 @@ impl Decoder { | |||
| 368 | } | 484 | } |
| 369 | } | 485 | } |
| 370 | 486 | ||
| 371 | - // The loop-filter grids are never written on this path (no | 487 | + // The loop-filter grids are only written when pixels are built, but |
| 372 | - // reconstruction), but the walk's signature carries them. | 488 | + // the walk's signature carries them. |
| 373 | let mut grids = LfGrids { | 489 | let mut grids = LfGrids { |
| 374 | level8: vec![0; mi_cols * mi_rows], | 490 | level8: vec![0; mi_cols * mi_rows], |
| 375 | blk: vec![(0u8, 0u32, 0u32); mi_cols * mi_rows], | 491 | blk: vec![(0u8, 0u32, 0u32); mi_cols * mi_rows], |
| 376 | otx: vec![0; mi_cols * mi_rows], | 492 | otx: vec![0; mi_cols * mi_rows], |
| 493 | + skip_inter: vec![false; mi_cols * mi_rows], | ||
| 377 | mi_cols, | 494 | mi_cols, |
| 378 | mi_rows, | 495 | mi_rows, |
| 379 | }; | 496 | }; |
| 497 | + // `above_context` / `above_seg_context` and the mi grid are | ||
| 498 | + // FRAME-scoped in libvpx (memset once per frame before the tile loop, | ||
| 499 | + // `vp9_decodeframe.c:2079-2083`; indexed by absolute mi_row/mi_col by | ||
| 500 | + // `set_skip_context`). Tile rows are disjoint in COLUMN only, so a | ||
| 501 | + // tile row k > 0 reads above state written by tile row k - 1 — the | ||
| 502 | + // same invariant the keyframe path documents. `above_context` is | ||
| 503 | + // sized by `mi_cols_aligned_to_sb`: a TX_32X32 block indexes up to 8 | ||
| 504 | + // 4x4 units past the last mi col. | ||
| 505 | + let aligned_cols = mi_cols.div_ceil(SB_MI) * SB_MI; | ||
| 506 | + let mut ectx = [ | ||
| 507 | + PlaneContexts::new(aligned_cols * 2), | ||
| 508 | + PlaneContexts::new(aligned_cols), | ||
| 509 | + PlaneContexts::new(aligned_cols), | ||
| 510 | + ]; | ||
| 511 | + ectx.iter_mut().for_each(|p| p.above.fill(0)); | ||
| 512 | + let mut mi = MiState::new(mi_cols, mi_rows); | ||
| 513 | + mi.above_seg.fill(0); | ||
| 380 | let mut ti = 0usize; | 514 | let mut ti = 0usize; |
| 381 | for tr in 0..tile_rows { | 515 | for tr in 0..tile_rows { |
| 382 | let row_lo = ((sb64_rows * tr) >> hdr.tile_info.rows_log2) * SB_MI; | 516 | let row_lo = ((sb64_rows * tr) >> hdr.tile_info.rows_log2) * SB_MI; |
| @@ -393,15 +527,10 @@ impl Decoder { | |||
| 393 | } | 527 | } |
| 394 | let mut r = crate::bool::BoolDecoder::new(tiles[ti])?; | 528 | let mut r = crate::bool::BoolDecoder::new(tiles[ti])?; |
| 395 | ti += 1; | 529 | ti += 1; |
| 396 | - let mut ectx = [ | ||
| 397 | - PlaneContexts::new(mi_cols * 2), | ||
| 398 | - PlaneContexts::new(mi_cols), | ||
| 399 | - PlaneContexts::new(mi_cols), | ||
| 400 | - ]; | ||
| 401 | - let mut mi = MiState::new(mi_cols, mi_rows); | ||
| 402 | - ectx.iter_mut().for_each(|p| p.above.fill(0)); | ||
| 403 | - mi.above_seg.fill(0); | ||
| 404 | for sb_row in (row_lo..row_hi).step_by(SB_MI) { | 530 | for sb_row in (row_lo..row_hi).step_by(SB_MI) { |
| 531 | + // Left entropy/partition contexts are zeroed at the start | ||
| 532 | + // of every superblock row (the SB row's left edge has no | ||
| 533 | + // neighbours); the above contexts stay frame-scoped. | ||
| 405 | ectx.iter_mut().for_each(|p| p.left = [0; 32]); | 534 | ectx.iter_mut().for_each(|p| p.left = [0; 32]); |
| 406 | mi.left_seg = [0; 32]; | 535 | mi.left_seg = [0; 32]; |
| 407 | for sb_col in (col_lo..col_hi).step_by(SB_MI) { | 536 | for sb_col in (col_lo..col_hi).step_by(SB_MI) { |
| @@ -417,6 +546,7 @@ impl Decoder { | |||
| 417 | 4, | 546 | 4, |
| 418 | col_lo, | 547 | col_lo, |
| 419 | ch.tx_mode, | 548 | ch.tx_mode, |
| 549 | + pixels, | ||
| 420 | )?; | 550 | )?; |
| 421 | } | 551 | } |
| 422 | } | 552 | } |
| @@ -454,7 +584,56 @@ impl Decoder { | |||
| 454 | self.ctx.partition[13] | 584 | self.ctx.partition[13] |
| 455 | ); | 585 | ); |
| 456 | } | 586 | } |
| 457 | - Ok(()) | 587 | + |
| 588 | + if !pixels { | ||
| 589 | + return Ok(None); | ||
| 590 | + } | ||
| 591 | + | ||
| 592 | + // Loop filter post-pass: same extents as the keyframe path, but the | ||
| 593 | + // per-cell level comes from the block's own reference and mode class | ||
| 594 | + // (`get_filter_level`, vp9_loopfilter.c:249). | ||
| 595 | + if hdr.loop_filter.level > 0 && std::env::var_os("EC_VP9_SKIP_LF").is_none() { | ||
| 596 | + let p = self.planes.as_mut().expect("frame scratch"); | ||
| 597 | + let (vw, vh) = (mi_cols * 8, mi_rows * 8); | ||
| 598 | + grids.filter_frame( | ||
| 599 | + &mut p.y, | ||
| 600 | + p.ys, | ||
| 601 | + &mut p.u, | ||
| 602 | + &mut p.v, | ||
| 603 | + p.uvs, | ||
| 604 | + vw, | ||
| 605 | + vh, | ||
| 606 | + vh / 2, | ||
| 607 | + hdr.loop_filter.sharpness, | ||
| 608 | + ); | ||
| 609 | + } | ||
| 610 | + | ||
| 611 | + let pic = self.take_picture(hdr); | ||
| 612 | + let frame = RefFrame { | ||
| 613 | + planes: std::sync::Arc::new(self.planes.take().expect("frame scratch")), | ||
| 614 | + width: hdr.width as u16, | ||
| 615 | + height: hdr.height as u16, | ||
| 616 | + }; | ||
| 617 | + | ||
| 618 | + // Reference refresh (spec 8.10 / `swap_frame_buffers`): bit i of | ||
| 619 | + // `refresh_frame_flags` replaces slot i with the frame just decoded — | ||
| 620 | + // the LOOP-FILTERED frame, which is what a later frame's prediction | ||
| 621 | + // reads. | ||
| 622 | + for slot in 0..8 { | ||
| 623 | + if hdr.refresh_frame_flags & (1 << slot) != 0 { | ||
| 624 | + self.refs[slot] = Some(frame.clone()); | ||
| 625 | + } | ||
| 626 | + } | ||
| 627 | + Ok(if hdr.show_frame { Some(pic) } else { None }) | ||
| 628 | + } | ||
| 629 | + | ||
| 630 | + /// Crop the frame scratch to a [`Picture`]. | ||
| 631 | + fn take_picture(&self, hdr: &FrameHeader) -> Picture { | ||
| 632 | + crop_picture( | ||
| 633 | + self.planes.as_ref().expect("frame scratch"), | ||
| 634 | + hdr.width as u16, | ||
| 635 | + hdr.height as u16, | ||
| 636 | + ) | ||
| 458 | } | 637 | } |
| 459 | 638 | ||
| 460 | /// One inter block's syntax (`read_inter_frame_mode_info` + the MV field | 639 | /// One inter block's syntax (`read_inter_frame_mode_info` + the MV field |
| @@ -488,7 +667,7 @@ impl Decoder { | |||
| 488 | ifs: w.ifs, | 667 | ifs: w.ifs, |
| 489 | sign_bias: w.sign_bias, | 668 | sign_bias: w.sign_bias, |
| 490 | allow_hp: hdr.allow_high_precision_mv, | 669 | allow_hp: hdr.allow_high_precision_mv, |
| 491 | - frame_interp: hdr.interpolation_filter as u8, | 670 | + frame_interp: interp_filter_of(hdr), |
| 492 | tx_mode, | 671 | tx_mode, |
| 493 | mi_rows: mi.mi_rows, | 672 | mi_rows: mi.mi_rows, |
| 494 | mi_cols: mi.mi_cols, | 673 | mi_cols: mi.mi_cols, |
| @@ -563,35 +742,182 @@ impl Decoder { | |||
| 563 | bw: usize, | 742 | bw: usize, |
| 564 | bh: usize, | 743 | bh: usize, |
| 565 | ) -> Result<()> { | 744 | ) -> Result<()> { |
| 566 | - let n4w = if sb_type < 3 { | 745 | + let n4w = plane_n4_w(sb_type, bw); |
| 567 | - [2, 1, 1] | 746 | + let n4h = plane_n4_h(sb_type, bh); |
| 568 | - } else { | 747 | + if info.skip { |
| 569 | - [bw * 2, bw, bw] | 748 | + // dec_reset_skip_context |
| 570 | - }; | 749 | + reset_skip_context(ectx, row, col, sb_type, n4w, n4h); |
| 571 | - let n4h = if sb_type < 3 { | 750 | + return Ok(()); |
| 572 | - [2, 1, 1] | 751 | + } |
| 752 | + let mb_to_right = ((mi.mi_cols as isize - bw as isize - col as isize) * 64) as i32; | ||
| 753 | + let mb_to_bottom = ((mi.mi_rows as isize - bh as isize - row as isize) * 64) as i32; | ||
| 754 | + for plane in 0..3usize { | ||
| 755 | + let s = usize::from(plane != 0); | ||
| 756 | + let tx_size = if plane == 0 { | ||
| 757 | + info.tx_size | ||
| 758 | + } else { | ||
| 759 | + get_uv_tx_size(info.tx_size, sb_type) | ||
| 760 | + }; | ||
| 761 | + let step = 1usize << tx_size; | ||
| 762 | + let max_blocks_wide = | ||
| 763 | + (n4w[plane] as i32 + (mb_to_right.min(0) >> (5 + s))).max(0) as usize; | ||
| 764 | + let max_blocks_high = | ||
| 765 | + (n4h[plane] as i32 + (mb_to_bottom.min(0) >> (5 + s))).max(0) as usize; | ||
| 766 | + for brow in (0..max_blocks_high).step_by(step) { | ||
| 767 | + for bcol in (0..max_blocks_wide).step_by(step) { | ||
| 768 | + let _ = decode_inter_tx_tokens( | ||
| 769 | + r, | ||
| 770 | + ectx, | ||
| 771 | + hdr, | ||
| 772 | + info, | ||
| 773 | + &self.ctx, | ||
| 774 | + plane, | ||
| 775 | + row, | ||
| 776 | + col, | ||
| 777 | + brow, | ||
| 778 | + bcol, | ||
| 779 | + tx_size, | ||
| 780 | + sb_type, | ||
| 781 | + bw, | ||
| 782 | + bh, | ||
| 783 | + mb_to_right, | ||
| 784 | + mb_to_bottom, | ||
| 785 | + ); | ||
| 786 | + } | ||
| 787 | + } | ||
| 788 | + } | ||
| 789 | + Ok(()) | ||
| 790 | + } | ||
| 791 | + | ||
| 792 | + /// `decode_block`'s inter arm with pixels (`vp9_decodeframe.c:990-1044`): | ||
| 793 | + /// `dec_build_inter_predictors_sb` for the whole block, then | ||
| 794 | + /// `reconstruct_inter_block` per transform block. Intra blocks inside an | ||
| 795 | + /// inter frame take the intra path (`:964-989`). | ||
| 796 | + #[allow(clippy::too_many_arguments)] | ||
| 797 | + fn reconstruct_inter_pixels( | ||
| 798 | + &mut self, | ||
| 799 | + r: &mut crate::bool::BoolDecoder, | ||
| 800 | + ectx: &mut [PlaneContexts; 3], | ||
| 801 | + grids: &mut LfGrids, | ||
| 802 | + hdr: &FrameHeader, | ||
| 803 | + info: &MiInfo, | ||
| 804 | + row: usize, | ||
| 805 | + col: usize, | ||
| 806 | + sb_type: usize, | ||
| 807 | + bw: usize, | ||
| 808 | + bh: usize, | ||
| 809 | + mi: &mut MiState, | ||
| 810 | + tile_col_start: usize, | ||
| 811 | + ) -> Result<()> { | ||
| 812 | + let x_mis = bw.min(mi.mi_cols - col); | ||
| 813 | + let y_mis = bh.min(mi.mi_rows - row); | ||
| 814 | + let n4w = plane_n4_w(sb_type, bw); | ||
| 815 | + let n4h = plane_n4_h(sb_type, bh); | ||
| 816 | + let mb_to_right = ((mi.mi_cols as isize - bw as isize - col as isize) * 64) as i32; | ||
| 817 | + let mb_to_bottom = ((mi.mi_rows as isize - bh as isize - row as isize) * 64) as i32; | ||
| 818 | + | ||
| 819 | + // `get_filter_level` (vp9_loopfilter.c:249): | ||
| 820 | + // `lvl[segment_id][mi->ref_frame[0]][mode_lf_lut[mi->mode]]`, with | ||
| 821 | + // `lvl` built by `vp9_loop_filter_frame_init` from the frame level, | ||
| 822 | + // the per-reference deltas and the per-mode-class deltas. | ||
| 823 | + let levels = hdr.loop_filter_levels(info.segment as usize); | ||
| 824 | + let ref0 = if info.is_inter { | ||
| 825 | + info.ref_frame[0].clamp(0, 3) as usize | ||
| 573 | } else { | 826 | } else { |
| 574 | - [bh * 2, bh, bh] | 827 | + // An intra block inside an inter frame: `mi->ref_frame[0]` is |
| 828 | + // INTRA_FRAME and its mode is an intra mode, so the mode class is | ||
| 829 | + // 0 and the reference slot is intra's. | ||
| 830 | + 0 | ||
| 575 | }; | 831 | }; |
| 832 | + let level = levels[ref0][mode_lf_lut(info.mode)]; | ||
| 833 | + for dy in 0..y_mis { | ||
| 834 | + for dx in 0..x_mis { | ||
| 835 | + let cell = (row + dy) * mi.mi_cols + col + dx; | ||
| 836 | + grids.level8[cell] = level; | ||
| 837 | + grids.blk[cell] = (sb_type as u8, row as u32, col as u32); | ||
| 838 | + grids.otx[cell] = if sb_type < 3 { | ||
| 839 | + TX_4X4 as u8 | ||
| 840 | + } else { | ||
| 841 | + info.tx_size as u8 | ||
| 842 | + }; | ||
| 843 | + // `build_masks`'s early return is gated on | ||
| 844 | + // `mi->skip && is_inter_block(mi)` — a skipped inter block | ||
| 845 | + // keeps ONLY the above/left prediction masks. | ||
| 846 | + grids.skip_inter[cell] = info.is_inter && info.skip; | ||
| 847 | + } | ||
| 848 | + } | ||
| 849 | + | ||
| 850 | + // `if (mi->skip) dec_reset_skip_context(xd);` — libvpx does this at the | ||
| 851 | + // top of `decode_block` (decodeframe.c:960), BEFORE the intra/inter | ||
| 852 | + // split, so skipped intra blocks inside an inter frame reset too. | ||
| 576 | if info.skip { | 853 | if info.skip { |
| 577 | - // dec_reset_skip_context | 854 | + reset_skip_context(ectx, row, col, sb_type, n4w, n4h); |
| 855 | + } | ||
| 856 | + | ||
| 857 | + if !info.is_inter { | ||
| 858 | + // `predict_and_reconstruct_intra_block` per transform block. | ||
| 578 | for plane in 0..3usize { | 859 | for plane in 0..3usize { |
| 579 | let s = usize::from(plane != 0); | 860 | let s = usize::from(plane != 0); |
| 580 | - let offx = (col << 1) >> s; | 861 | + let tx_size = if plane == 0 { |
| 581 | - let offy = (row << 1) >> s; | 862 | + info.tx_size |
| 582 | - for i in 0..n4w[plane] { | 863 | + } else { |
| 583 | - ectx[plane].above[offx + i] = 0; | 864 | + get_uv_tx_size(info.tx_size, sb_type) |
| 584 | - } | 865 | + }; |
| 585 | - let lrows = 32usize >> s; | 866 | + let step = 1usize << tx_size; |
| 586 | - for i in 0..n4h[plane].min(lrows) { | 867 | + let max_blocks_wide = |
| 587 | - ectx[plane].left[(offy + i) % lrows] = 0; | 868 | + (n4w[plane] as i32 + (mb_to_right.min(0) >> (5 + s))).max(0) as usize; |
| 869 | + let max_blocks_high = | ||
| 870 | + (n4h[plane] as i32 + (mb_to_bottom.min(0) >> (5 + s))).max(0) as usize; | ||
| 871 | + for brow in (0..max_blocks_high).step_by(step) { | ||
| 872 | + for bcol in (0..max_blocks_wide).step_by(step) { | ||
| 873 | + self.predict_and_reconstruct( | ||
| 874 | + r, | ||
| 875 | + ectx, | ||
| 876 | + hdr, | ||
| 877 | + info, | ||
| 878 | + plane, | ||
| 879 | + row, | ||
| 880 | + col, | ||
| 881 | + brow, | ||
| 882 | + bcol, | ||
| 883 | + tx_size, | ||
| 884 | + sb_type, | ||
| 885 | + bw, | ||
| 886 | + bh, | ||
| 887 | + mb_to_right, | ||
| 888 | + mb_to_bottom, | ||
| 889 | + // Intra prediction's left availability is | ||
| 890 | + // tile-scoped (`tile->mi_col_start`), which the | ||
| 891 | + // inter walk passes as the tile's first mi col. | ||
| 892 | + tile_col_start, | ||
| 893 | + )?; | ||
| 894 | + } | ||
| 588 | } | 895 | } |
| 589 | } | 896 | } |
| 590 | return Ok(()); | 897 | return Ok(()); |
| 591 | } | 898 | } |
| 592 | - let mb_to_right = ((mi.mi_cols as isize - bw as isize - col as isize) * 64) as i32; | 899 | + |
| 593 | - let mb_to_bottom = ((mi.mi_rows as isize - bh as isize - row as isize) * 64) as i32; | 900 | + // Prediction: `dec_build_inter_predictors_sb`, every reference, every |
| 594 | - let lossless = hdr.quantization.lossless(); | 901 | + // plane, before any token is read. |
| 902 | + { | ||
| 903 | + let planes = self.planes.as_mut().expect("frame scratch"); | ||
| 904 | + let refs = &self.refs; | ||
| 905 | + let scratch = &mut self.mc_scratch; | ||
| 906 | + let temp = &mut self.mc_temp; | ||
| 907 | + for plane in 0..3usize { | ||
| 908 | + inter_predict_plane( | ||
| 909 | + planes, refs, scratch, temp, hdr, info, plane, row, col, &n4w, &n4h, | ||
| 910 | + mb_to_right, mb_to_bottom, sb_type, | ||
| 911 | + )?; | ||
| 912 | + } | ||
| 913 | + } | ||
| 914 | + | ||
| 915 | + if info.skip { | ||
| 916 | + return Ok(()); | ||
| 917 | + } | ||
| 918 | + | ||
| 919 | + // Reconstruction: `reconstruct_inter_block` per transform block. | ||
| 920 | + let mut eobtotal = 0usize; | ||
| 595 | for plane in 0..3usize { | 921 | for plane in 0..3usize { |
| 596 | let s = usize::from(plane != 0); | 922 | let s = usize::from(plane != 0); |
| 597 | let tx_size = if plane == 0 { | 923 | let tx_size = if plane == 0 { |
| @@ -606,62 +932,56 @@ impl Decoder { | |||
| 606 | (n4h[plane] as i32 + (mb_to_bottom.min(0) >> (5 + s))).max(0) as usize; | 932 | (n4h[plane] as i32 + (mb_to_bottom.min(0) >> (5 + s))).max(0) as usize; |
| 607 | for brow in (0..max_blocks_high).step_by(step) { | 933 | for brow in (0..max_blocks_high).step_by(step) { |
| 608 | for bcol in (0..max_blocks_wide).step_by(step) { | 934 | for bcol in (0..max_blocks_wide).step_by(step) { |
| 609 | - let tx_type = if plane != 0 || lossless || info.is_inter { | 935 | + let coef = decode_inter_tx_tokens( |
| 610 | - DCT_DCT | ||
| 611 | - } else { | ||
| 612 | - MODE_TO_TXTYPE_LOOKUP[info.mode as usize] as usize | ||
| 613 | - }; | ||
| 614 | - let (scan, nb) = scan_for(tx_size, tx_type); | ||
| 615 | - let q = hdr.segment_dequant(info.segment as usize); | ||
| 616 | - let dequant: [i16; 2] = if plane == 0 { | ||
| 617 | - [q.luma_dc, q.luma_ac] | ||
| 618 | - } else { | ||
| 619 | - [q.chroma_dc, q.chroma_ac] | ||
| 620 | - }; | ||
| 621 | - let ptype = usize::from(plane != 0); | ||
| 622 | - let ax = ((col << 1) >> s) + bcol; | ||
| 623 | - let lrows = 32usize >> s; | ||
| 624 | - let ay = (((row << 1) >> s) + brow) % lrows; | ||
| 625 | - let nblocks = 1usize << tx_size; | ||
| 626 | - let ctx_in = usize::from(ectx[plane].above[ax..ax + nblocks].iter().any(|&v| v != 0)) | ||
| 627 | - + usize::from(ectx[plane].left[ay..ay + nblocks].iter().any(|&v| v != 0)); | ||
| 628 | - let coef = decode_coefs( | ||
| 629 | r, | 936 | r, |
| 630 | - ptype, | 937 | + ectx, |
| 631 | - tx_size, | 938 | + hdr, |
| 632 | - &dequant, | 939 | + info, |
| 633 | - ctx_in, | 940 | + &self.ctx, |
| 634 | - scan, | ||
| 635 | - nb, | ||
| 636 | - &self.ctx.coef[tx_size], | ||
| 637 | - info.is_inter, | ||
| 638 | - (0, 0), | ||
| 639 | plane, | 941 | plane, |
| 942 | + row, | ||
| 943 | + col, | ||
| 944 | + brow, | ||
| 945 | + bcol, | ||
| 946 | + tx_size, | ||
| 947 | + sb_type, | ||
| 948 | + bw, | ||
| 949 | + bh, | ||
| 950 | + mb_to_right, | ||
| 951 | + mb_to_bottom, | ||
| 640 | ); | 952 | ); |
| 641 | - // vp9_decode_block_tokens context write-back. | 953 | + eobtotal += coef.eob; |
| 642 | - let n4w_s = bw * 2 >> s; | 954 | + if coef.eob == 0 { |
| 643 | - let n4h_s = bh * 2 >> s; | 955 | + continue; |
| 644 | - let maxw = | ||
| 645 | - (n4w_s as i32 + (mb_to_right.min(0) >> (5 + s))).max(0) as usize; | ||
| 646 | - let maxh = | ||
| 647 | - (n4h_s as i32 + (mb_to_bottom.min(0) >> (5 + s))).max(0) as usize; | ||
| 648 | - let eob_pos = u64::from(coef.eob > 0); | ||
| 649 | - let shift_a = if maxw > 0 && nblocks + bcol > maxw { | ||
| 650 | - (nblocks - (maxw - bcol)) * 8 | ||
| 651 | - } else { | ||
| 652 | - 0 | ||
| 653 | - }; | ||
| 654 | - let shift_l = if maxh > 0 && nblocks + brow > maxh { | ||
| 655 | - (nblocks - (maxh - brow)) * 8 | ||
| 656 | - } else { | ||
| 657 | - 0 | ||
| 658 | - }; | ||
| 659 | - let spread_a = (eob_pos * 0x0101_0101_0101_0101) >> shift_a; | ||
| 660 | - let spread_l = (eob_pos * 0x0101_0101_0101_0101) >> shift_l; | ||
| 661 | - for k in 0..nblocks { | ||
| 662 | - ectx[plane].above[ax + k] = (spread_a >> (8 * k)) as u8; | ||
| 663 | - ectx[plane].left[ay + k] = (spread_l >> (8 * k)) as u8; | ||
| 664 | } | 956 | } |
| 957 | + // `inverse_transform_block_inter`: inter blocks always use | ||
| 958 | + // the DEFAULT scan and DCT_DCT (`reconstruct_inter_block`, | ||
| 959 | + // decodeframe.c:413). | ||
| 960 | + let planes = self.planes.as_mut().expect("frame scratch"); | ||
| 961 | + let stride = planes.dims(plane).0; | ||
| 962 | + let x0 = ((col * 8) >> s) + bcol * 4; | ||
| 963 | + let y0 = ((row * 8) >> s) + brow * 4; | ||
| 964 | + let dst_off = y0 * stride + x0; | ||
| 965 | + let (data, _) = planes.plane(plane); | ||
| 966 | + inverse_transform_add( | ||
| 967 | + tx_size, | ||
| 968 | + DCT_DCT, | ||
| 969 | + &coef.coeffs, | ||
| 970 | + data, | ||
| 971 | + dst_off, | ||
| 972 | + stride, | ||
| 973 | + hdr.quantization.lossless(), | ||
| 974 | + ); | ||
| 975 | + } | ||
| 976 | + } | ||
| 977 | + } | ||
| 978 | + | ||
| 979 | + // `if (!less8x8 && eobtotal == 0) mi->skip = 1; // skip loopfilter` | ||
| 980 | + // (decodeframe.c:1043). | ||
| 981 | + if sb_type >= 3 && eobtotal == 0 { | ||
| 982 | + for dy in 0..y_mis { | ||
| 983 | + for dx in 0..x_mis { | ||
| 984 | + grids.skip_inter[(row + dy) * mi.mi_cols + col + dx] = true; | ||
| 665 | } | 985 | } |
| 666 | } | 986 | } |
| 667 | } | 987 | } |
| @@ -695,9 +1015,7 @@ impl Decoder { | |||
| 695 | hdr.uncompressed_header_size as usize + hdr.header_size_in_bytes as usize | 1015 | hdr.uncompressed_header_size as usize + hdr.header_size_in_bytes as usize |
| 696 | }); | 1016 | }); |
| 697 | let tail: Vec<u8> = frame[tail_start..].to_vec(); | 1017 | let tail: Vec<u8> = frame[tail_start..].to_vec(); |
| 698 | - let res = self.decode_keyframe_inner(hdr, tx_mode, &tail); | 1018 | + self.decode_keyframe_inner(hdr, tx_mode, &tail) |
| 699 | - self.planes = None; | ||
| 700 | - res | ||
| 701 | } | 1019 | } |
| 702 | 1020 | ||
| 703 | fn decode_keyframe_inner( | 1021 | fn decode_keyframe_inner( |
| @@ -706,8 +1024,6 @@ impl Decoder { | |||
| 706 | tx_mode: u8, | 1024 | tx_mode: u8, |
| 707 | tail: &[u8], | 1025 | tail: &[u8], |
| 708 | ) -> Result<Picture> { | 1026 | ) -> Result<Picture> { |
| 709 | - let width = hdr.width as usize; | ||
| 710 | - let height = hdr.height as usize; | ||
| 711 | let mi_cols = hdr.mi_cols() as usize; | 1027 | let mi_cols = hdr.mi_cols() as usize; |
| 712 | let mi_rows = hdr.mi_rows() as usize; | 1028 | let mi_rows = hdr.mi_rows() as usize; |
| 713 | let sb64_cols = mi_cols.div_ceil(SB_MI); | 1029 | let sb64_cols = mi_cols.div_ceil(SB_MI); |
| @@ -747,6 +1063,7 @@ impl Decoder { | |||
| 747 | level8: vec![0; mi_cols * mi_rows], | 1063 | level8: vec![0; mi_cols * mi_rows], |
| 748 | blk: vec![(0u8, 0u32, 0u32); mi_cols * mi_rows], | 1064 | blk: vec![(0u8, 0u32, 0u32); mi_cols * mi_rows], |
| 749 | otx: vec![0; mi_cols * mi_rows], | 1065 | otx: vec![0; mi_cols * mi_rows], |
| 1066 | + skip_inter: vec![false; mi_cols * mi_rows], | ||
| 750 | mi_cols, | 1067 | mi_cols, |
| 751 | mi_rows, | 1068 | mi_rows, |
| 752 | }; | 1069 | }; |
| @@ -819,6 +1136,7 @@ impl Decoder { | |||
| 819 | 4, | 1136 | 4, |
| 820 | col_lo, | 1137 | col_lo, |
| 821 | tx_mode, | 1138 | tx_mode, |
| 1139 | + true, | ||
| 822 | )?; | 1140 | )?; |
| 823 | } | 1141 | } |
| 824 | } | 1142 | } |
| @@ -845,25 +1163,7 @@ impl Decoder { | |||
| 845 | ); | 1163 | ); |
| 846 | } | 1164 | } |
| 847 | 1165 | ||
| 848 | - let crop = |src: &[u8], stride: usize, w: usize, h: usize| -> Vec<u8> { | 1166 | + Ok(self.take_picture(hdr)) |
| 849 | - let mut out = Vec::with_capacity(w * h); | ||
| 850 | - for row in 0..h { | ||
| 851 | - out.extend_from_slice(&src[row * stride..row * stride + w]); | ||
| 852 | - } | ||
| 853 | - out | ||
| 854 | - }; | ||
| 855 | - let planes = self.planes.as_ref().expect("frame scratch"); | ||
| 856 | - Ok(Picture { | ||
| 857 | - y: crop(&planes.y, planes.ys, width, height), | ||
| 858 | - u: crop(&planes.u, planes.uvs, width / 2, height / 2), | ||
| 859 | - v: crop(&planes.v, planes.uvs, width / 2, height / 2), | ||
| 860 | - width: width as u16, | ||
| 861 | - height: height as u16, | ||
| 862 | - // The crop above packs rows: `stride` describes the RETURNED | ||
| 863 | - // buffers, not the aligned decode buffer (`aw`). | ||
| 864 | - stride: width, | ||
| 865 | - uv_stride: width / 2, | ||
| 866 | - }) | ||
| 867 | } | 1167 | } |
| 868 | 1168 | ||
| 869 | /// `decode_partition` (decodeframe.c:1173). | 1169 | /// `decode_partition` (decodeframe.c:1173). |
| @@ -881,6 +1181,10 @@ impl Decoder { | |||
| 881 | n4: usize, | 1181 | n4: usize, |
| 882 | tile_col_start: usize, | 1182 | tile_col_start: usize, |
| 883 | tx_mode: u8, | 1183 | tx_mode: u8, |
| 1184 | + // Whether this walk reconstructs pixels. Consulted by the inter | ||
| 1185 | + // branch only: a keyframe always reconstructs its intra blocks, and | ||
| 1186 | + // the syntax-only inter walk parses tokens without predicting. | ||
| 1187 | + pixels: bool, | ||
| 884 | ) -> Result<()> { | 1188 | ) -> Result<()> { |
| 885 | if row >= mi.mi_rows || col >= mi.mi_cols { | 1189 | if row >= mi.mi_rows || col >= mi.mi_cols { |
| 886 | return Ok(()); | 1190 | return Ok(()); |
| @@ -924,6 +1228,7 @@ impl Decoder { | |||
| 924 | 1, | 1228 | 1, |
| 925 | tile_col_start, | 1229 | tile_col_start, |
| 926 | tx_mode, | 1230 | tx_mode, |
| 1231 | + pixels, | ||
| 927 | )?; | 1232 | )?; |
| 928 | } else { | 1233 | } else { |
| 929 | match partition { | 1234 | match partition { |
| @@ -941,6 +1246,7 @@ impl Decoder { | |||
| 941 | n4, | 1246 | n4, |
| 942 | tile_col_start, | 1247 | tile_col_start, |
| 943 | tx_mode, | 1248 | tx_mode, |
| 1249 | + pixels, | ||
| 944 | )?, | 1250 | )?, |
| 945 | 1 => { | 1251 | 1 => { |
| 946 | self.decode_block( | 1252 | self.decode_block( |
| @@ -957,6 +1263,7 @@ impl Decoder { | |||
| 957 | n8, | 1263 | n8, |
| 958 | tile_col_start, | 1264 | tile_col_start, |
| 959 | tx_mode, | 1265 | tx_mode, |
| 1266 | + pixels, | ||
| 960 | )?; | 1267 | )?; |
| 961 | if has_rows { | 1268 | if has_rows { |
| 962 | self.decode_block( | 1269 | self.decode_block( |
| @@ -973,6 +1280,7 @@ impl Decoder { | |||
| 973 | n8, | 1280 | n8, |
| 974 | tile_col_start, | 1281 | tile_col_start, |
| 975 | tx_mode, | 1282 | tx_mode, |
| 1283 | + pixels, | ||
| 976 | )?; | 1284 | )?; |
| 977 | } | 1285 | } |
| 978 | } | 1286 | } |
| @@ -991,6 +1299,7 @@ impl Decoder { | |||
| 991 | n4, | 1299 | n4, |
| 992 | tile_col_start, | 1300 | tile_col_start, |
| 993 | tx_mode, | 1301 | tx_mode, |
| 1302 | + pixels, | ||
| 994 | )?; | 1303 | )?; |
| 995 | if has_cols { | 1304 | if has_cols { |
| 996 | self.decode_block( | 1305 | self.decode_block( |
| @@ -1007,6 +1316,7 @@ impl Decoder { | |||
| 1007 | n4, | 1316 | n4, |
| 1008 | tile_col_start, | 1317 | tile_col_start, |
| 1009 | tx_mode, | 1318 | tx_mode, |
| 1319 | + pixels, | ||
| 1010 | )?; | 1320 | )?; |
| 1011 | } | 1321 | } |
| 1012 | } | 1322 | } |
| @@ -1029,6 +1339,7 @@ impl Decoder { | |||
| 1029 | n8, | 1339 | n8, |
| 1030 | tile_col_start, | 1340 | tile_col_start, |
| 1031 | tx_mode, | 1341 | tx_mode, |
| 1342 | + pixels, | ||
| 1032 | )?; | 1343 | )?; |
| 1033 | } | 1344 | } |
| 1034 | } | 1345 | } |
| @@ -1057,6 +1368,7 @@ impl Decoder { | |||
| 1057 | bhl: usize, | 1368 | bhl: usize, |
| 1058 | tile_col_start: usize, | 1369 | tile_col_start: usize, |
| 1059 | tx_mode: u8, | 1370 | tx_mode: u8, |
| 1371 | + pixels: bool, | ||
| 1060 | ) -> Result<()> { | 1372 | ) -> Result<()> { |
| 1061 | let bw = 1 << (bwl - 1); | 1373 | let bw = 1 << (bwl - 1); |
| 1062 | let bh = 1 << (bhl - 1); | 1374 | let bh = 1 << (bhl - 1); |
| @@ -1093,9 +1405,20 @@ impl Decoder { | |||
| 1093 | )? | 1405 | )? |
| 1094 | }; | 1406 | }; |
| 1095 | if hdr.frame_type != FrameType::Key { | 1407 | if hdr.frame_type != FrameType::Key { |
| 1096 | - // Inter syntax only: tokens keep the entropy stream aligned, the | 1408 | + if pixels { |
| 1097 | - // block lands in the mode grid, and no pixel is touched. | 1409 | + // Full inter decode: prediction, tokens, residual. The |
| 1098 | - self.decode_inter_block_tokens(r, mi, ectx, hdr, &info, row, col, sb_type, bw, bh)?; | 1410 | + // per-cell loop-filter level is the block's own reference and |
| 1411 | + // mode class (`get_filter_level`), so the grids are written | ||
| 1412 | + // inside the reconstruction rather than from `level_of_seg`. | ||
| 1413 | + self.reconstruct_inter_pixels( | ||
| 1414 | + r, ectx, grids, hdr, &info, row, col, sb_type, bw, bh, mi, | ||
| 1415 | + tile_col_start, | ||
| 1416 | + )?; | ||
| 1417 | + } else { | ||
| 1418 | + // Inter syntax only: tokens keep the entropy stream aligned, | ||
| 1419 | + // the block lands in the mode grid, and no pixel is touched. | ||
| 1420 | + self.decode_inter_block_tokens(r, mi, ectx, hdr, &info, row, col, sb_type, bw, bh)?; | ||
| 1421 | + } | ||
| 1099 | for dy in 0..y_mis { | 1422 | for dy in 0..y_mis { |
| 1100 | for dx in 0..x_mis { | 1423 | for dx in 0..x_mis { |
| 1101 | if crate::mvdbg2_enabled() | 1424 | if crate::mvdbg2_enabled() |
| @@ -1464,3 +1787,311 @@ fn bsize_of_n4(n4: usize) -> usize { | |||
| 1464 | _ => 0, // BLOCK_4X4 (recursion never reaches n4 == 0) | 1787 | _ => 0, // BLOCK_4X4 (recursion never reaches n4 == 0) |
| 1465 | } | 1788 | } |
| 1466 | } | 1789 | } |
| 1790 | + | ||
| 1791 | +/// Crop an aligned plane set to a displayable [`Picture`]: rows are packed to | ||
| 1792 | +/// the visible width, so `stride` describes the RETURNED buffers, not the | ||
| 1793 | +/// aligned decode buffer. | ||
| 1794 | +fn crop_picture(planes: &Planes, width: u16, height: u16) -> Picture { | ||
| 1795 | + let (w, h) = (width as usize, height as usize); | ||
| 1796 | + let crop = |src: &[u8], stride: usize, cw: usize, ch: usize| -> Vec<u8> { | ||
| 1797 | + let mut out = Vec::with_capacity(cw * ch); | ||
| 1798 | + for row in 0..ch { | ||
| 1799 | + out.extend_from_slice(&src[row * stride..row * stride + cw]); | ||
| 1800 | + } | ||
| 1801 | + out | ||
| 1802 | + }; | ||
| 1803 | + Picture { | ||
| 1804 | + y: crop(&planes.y, planes.ys, w, h), | ||
| 1805 | + u: crop(&planes.u, planes.uvs, w / 2, h / 2), | ||
| 1806 | + v: crop(&planes.v, planes.uvs, w / 2, h / 2), | ||
| 1807 | + width, | ||
| 1808 | + height, | ||
| 1809 | + stride: w, | ||
| 1810 | + uv_stride: w / 2, | ||
| 1811 | + } | ||
| 1812 | +} | ||
| 1813 | + | ||
| 1814 | +/// `pd->n4_w` (`set_plane_n4`, decodeframe.c:809): the plane block's width in | ||
| 1815 | +/// its own 4x4 units. Sub-8x8 shapes occupy a whole 8x8 mi cell | ||
| 1816 | +/// (`set_plane_n4(xd, bw=1, bh=1)`) so luma walks 2x2 and chroma 1x1. | ||
| 1817 | +fn plane_n4_w(sb_type: usize, bw: usize) -> [usize; 3] { | ||
| 1818 | + if sb_type < 3 { | ||
| 1819 | + [2, 1, 1] | ||
| 1820 | + } else { | ||
| 1821 | + [bw * 2, bw, bw] | ||
| 1822 | + } | ||
| 1823 | +} | ||
| 1824 | + | ||
| 1825 | +/// `pd->n4_h`, the height twin of [`plane_n4_w`]. | ||
| 1826 | +fn plane_n4_h(sb_type: usize, bh: usize) -> [usize; 3] { | ||
| 1827 | + if sb_type < 3 { | ||
| 1828 | + [2, 1, 1] | ||
| 1829 | + } else { | ||
| 1830 | + [bh * 2, bh, bh] | ||
| 1831 | + } | ||
| 1832 | +} | ||
| 1833 | + | ||
| 1834 | +/// `dec_reset_skip_context` (`vp9_decodeframe.c:800`). | ||
| 1835 | +fn reset_skip_context( | ||
| 1836 | + ectx: &mut [PlaneContexts; 3], | ||
| 1837 | + row: usize, | ||
| 1838 | + col: usize, | ||
| 1839 | + sb_type: usize, | ||
| 1840 | + n4w: [usize; 3], | ||
| 1841 | + n4h: [usize; 3], | ||
| 1842 | +) { | ||
| 1843 | + let _ = sb_type; | ||
| 1844 | + for plane in 0..3usize { | ||
| 1845 | + let s = usize::from(plane != 0); | ||
| 1846 | + let offx = (col << 1) >> s; | ||
| 1847 | + let offy = (row << 1) >> s; | ||
| 1848 | + for i in 0..n4w[plane] { | ||
| 1849 | + ectx[plane].above[offx + i] = 0; | ||
| 1850 | + } | ||
| 1851 | + let lrows = 32usize >> s; | ||
| 1852 | + for i in 0..n4h[plane].min(lrows) { | ||
| 1853 | + ectx[plane].left[(offy + i) % lrows] = 0; | ||
| 1854 | + } | ||
| 1855 | + } | ||
| 1856 | +} | ||
| 1857 | + | ||
| 1858 | +/// `mode_lf_lut` (`vp9/common/vp9_loopfilter.c:223`): | ||
| 1859 | +/// `{0 × INTRA_MODES, 1, 1, 0, 1}` — NEARESTMV, NEARMV and NEWMV take the | ||
| 1860 | +/// inter mode delta, ZEROMV does not, and intra modes never do. | ||
| 1861 | +fn mode_lf_lut(mode: u8) -> usize { | ||
| 1862 | + match mode { | ||
| 1863 | + 10 | 11 | 13 => 1, | ||
| 1864 | + _ => 0, | ||
| 1865 | + } | ||
| 1866 | +} | ||
| 1867 | + | ||
| 1868 | +/// One inter transform block's tokens plus the context write-back | ||
| 1869 | +/// (`vp9_decode_block_tokens`, `decodeframe.c:414`). Inter blocks always use | ||
| 1870 | +/// the DEFAULT scan and `DCT_DCT`. | ||
| 1871 | +#[allow(clippy::too_many_arguments)] | ||
| 1872 | +fn decode_inter_tx_tokens( | ||
| 1873 | + r: &mut crate::bool::BoolDecoder, | ||
| 1874 | + ectx: &mut [PlaneContexts; 3], | ||
| 1875 | + hdr: &FrameHeader, | ||
| 1876 | + info: &MiInfo, | ||
| 1877 | + fc: &FrameContext, | ||
| 1878 | + plane: usize, | ||
| 1879 | + row: usize, | ||
| 1880 | + col: usize, | ||
| 1881 | + brow: usize, | ||
| 1882 | + bcol: usize, | ||
| 1883 | + tx_size: usize, | ||
| 1884 | + sb_type: usize, | ||
| 1885 | + bw: usize, | ||
| 1886 | + bh: usize, | ||
| 1887 | + mb_to_right: i32, | ||
| 1888 | + mb_to_bottom: i32, | ||
| 1889 | +) -> crate::tokens::CoeffBlock { | ||
| 1890 | + let s = usize::from(plane != 0); | ||
| 1891 | + // `predict_and_reconstruct_intra_block` (decodeframe.c:320-345): the luma | ||
| 1892 | + // mode of a sub-8x8 block is the SUB-BLOCK's, and an intra block inside an | ||
| 1893 | + // inter frame picks its tx type from that mode — `reconstruct_inter_block` | ||
| 1894 | + // hard-codes DCT_DCT instead, which is why the inter guard is here. | ||
| 1895 | + let tx_type = if plane != 0 || hdr.quantization.lossless() || info.is_inter { | ||
| 1896 | + DCT_DCT | ||
| 1897 | + } else { | ||
| 1898 | + let mode = if sb_type < 3 { | ||
| 1899 | + info.bmi[(brow << 1) + bcol] | ||
| 1900 | + } else { | ||
| 1901 | + info.mode | ||
| 1902 | + }; | ||
| 1903 | + MODE_TO_TXTYPE_LOOKUP[mode as usize] as usize | ||
| 1904 | + }; | ||
| 1905 | + let (scan, nb) = scan_for(tx_size, tx_type); | ||
| 1906 | + let q = hdr.segment_dequant(info.segment as usize); | ||
| 1907 | + let dequant: [i16; 2] = if plane == 0 { | ||
| 1908 | + [q.luma_dc, q.luma_ac] | ||
| 1909 | + } else { | ||
| 1910 | + [q.chroma_dc, q.chroma_ac] | ||
| 1911 | + }; | ||
| 1912 | + let ptype = usize::from(plane != 0); | ||
| 1913 | + let ax = ((col << 1) >> s) + bcol; | ||
| 1914 | + let lrows = 32usize >> s; | ||
| 1915 | + let ay = (((row << 1) >> s) + brow) % lrows; | ||
| 1916 | + let nblocks = 1usize << tx_size; | ||
| 1917 | + let ctx_in = usize::from(ectx[plane].above[ax..ax + nblocks].iter().any(|&v| v != 0)) | ||
| 1918 | + + usize::from(ectx[plane].left[ay..ay + nblocks].iter().any(|&v| v != 0)); | ||
| 1919 | + if crate::tokdbg_enabled() { | ||
| 1920 | + eprintln!( | ||
| 1921 | + "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={:?}", | ||
| 1922 | + u8::from(info.is_inter), | ||
| 1923 | + info.mode, | ||
| 1924 | + &ectx[plane].above[ax..ax + nblocks], | ||
| 1925 | + &ectx[plane].left[ay..ay + nblocks], | ||
| 1926 | + ); | ||
| 1927 | + } | ||
| 1928 | + let coef = decode_coefs( | ||
| 1929 | + r, | ||
| 1930 | + ptype, | ||
| 1931 | + tx_size, | ||
| 1932 | + &dequant, | ||
| 1933 | + ctx_in, | ||
| 1934 | + scan, | ||
| 1935 | + nb, | ||
| 1936 | + &fc.coef[tx_size], | ||
| 1937 | + info.is_inter, | ||
| 1938 | + (0, 0), | ||
| 1939 | + plane, | ||
| 1940 | + ); | ||
| 1941 | + // vp9_decode_block_tokens context write-back (v1.15 edge shifts). | ||
| 1942 | + let n4w_s = bw * 2 >> s; | ||
| 1943 | + let n4h_s = bh * 2 >> s; | ||
| 1944 | + let maxw = (n4w_s as i32 + (mb_to_right.min(0) >> (5 + s))).max(0) as usize; | ||
| 1945 | + let maxh = (n4h_s as i32 + (mb_to_bottom.min(0) >> (5 + s))).max(0) as usize; | ||
| 1946 | + let eob_pos = u64::from(coef.eob > 0); | ||
| 1947 | + let shift_a = if maxw > 0 && nblocks + bcol > maxw { | ||
| 1948 | + (nblocks - (maxw - bcol)) * 8 | ||
| 1949 | + } else { | ||
| 1950 | + 0 | ||
| 1951 | + }; | ||
| 1952 | + let shift_l = if maxh > 0 && nblocks + brow > maxh { | ||
| 1953 | + (nblocks - (maxh - brow)) * 8 | ||
| 1954 | + } else { | ||
| 1955 | + 0 | ||
| 1956 | + }; | ||
| 1957 | + let spread_a = (eob_pos * 0x0101_0101_0101_0101) >> shift_a; | ||
| 1958 | + let spread_l = (eob_pos * 0x0101_0101_0101_0101) >> shift_l; | ||
| 1959 | + for k in 0..nblocks { | ||
| 1960 | + ectx[plane].above[ax + k] = (spread_a >> (8 * k)) as u8; | ||
| 1961 | + ectx[plane].left[ay + k] = (spread_l >> (8 * k)) as u8; | ||
| 1962 | + } | ||
| 1963 | + coef | ||
| 1964 | +} | ||
| 1965 | + | ||
| 1966 | +/// `dec_build_inter_predictors_sb` (`vp9_decodeframe.c:731`) for one plane: | ||
| 1967 | +/// every reference's prediction for one block, in reference order (compound | ||
| 1968 | +/// blocks average the second into the first). | ||
| 1969 | +#[allow(clippy::too_many_arguments)] | ||
| 1970 | +fn inter_predict_plane( | ||
| 1971 | + planes: &mut Planes, | ||
| 1972 | + refs: &[Option<RefFrame>; 8], | ||
| 1973 | + scratch: &mut Vec<u8>, | ||
| 1974 | + temp: &mut Vec<u8>, | ||
| 1975 | + hdr: &FrameHeader, | ||
| 1976 | + info: &MiInfo, | ||
| 1977 | + plane: usize, | ||
| 1978 | + row: usize, | ||
| 1979 | + col: usize, | ||
| 1980 | + n4w: &[usize; 3], | ||
| 1981 | + n4h: &[usize; 3], | ||
| 1982 | + mb_to_right: i32, | ||
| 1983 | + mb_to_bottom: i32, | ||
| 1984 | + sb_type: usize, | ||
| 1985 | +) -> Result<()> { | ||
| 1986 | + let s = usize::from(plane != 0); | ||
| 1987 | + let ss_x = s; | ||
| 1988 | + let ss_y = s; | ||
| 1989 | + let pw = n4w[plane] * 4; | ||
| 1990 | + let ph = n4h[plane] * 4; | ||
| 1991 | + let mb_left = -((col as i32) * 64); | ||
| 1992 | + let mb_top = -((row as i32) * 64); | ||
| 1993 | + let is_compound = info.ref_frame[1] > crate::inter::INTRA_FRAME; | ||
| 1994 | + let refs_used = 1 + usize::from(is_compound); | ||
| 1995 | + let kernel = usize::from(info.interp_filter); | ||
| 1996 | + let dst_stride = planes.dims(plane).0; | ||
| 1997 | + for rf in 0..refs_used { | ||
| 1998 | + let ref_name = info.ref_frame[rf]; | ||
| 1999 | + let slot = hdr.ref_frame_idx[(ref_name - 1) as usize] as usize; | ||
| 2000 | + let Some(ref_frame) = refs[slot].as_ref() else { | ||
| 2001 | + return Err(corrupt("vp9 inter prediction read an empty reference slot")); | ||
| 2002 | + }; | ||
| 2003 | + let (ref_data, ref_stride) = ref_frame.planes.plane_ref(plane); | ||
| 2004 | + let refp = RefPlane { | ||
| 2005 | + data: ref_data, | ||
| 2006 | + stride: ref_stride, | ||
| 2007 | + width: (ref_frame.width as usize) >> s, | ||
| 2008 | + height: (ref_frame.height as usize) >> s, | ||
| 2009 | + }; | ||
| 2010 | + if sb_type < 3 { | ||
| 2011 | + // libvpx builds one 4x4 prediction per sub-block, with the MV from | ||
| 2012 | + // `average_split_mvs` (luma: the sub-block's own MV; 4:2:0 chroma: | ||
| 2013 | + // the q4-rounded average of the four). | ||
| 2014 | + let n4x = n4w[plane]; | ||
| 2015 | + let n4y = n4h[plane]; | ||
| 2016 | + for y in 0..n4y { | ||
| 2017 | + for xx in 0..n4x { | ||
| 2018 | + let i = y * n4x + xx; | ||
| 2019 | + let mv = if plane == 0 { | ||
| 2020 | + split_mv(&info.bmi_mv, i, rf) | ||
| 2021 | + } else { | ||
| 2022 | + average_split_mvs_chroma(&info.bmi_mv, rf) | ||
| 2023 | + }; | ||
| 2024 | + let (data, _) = planes.plane(plane); | ||
| 2025 | + let dst_off = (((row * 8) >> s) + y * 4) * dst_stride | ||
| 2026 | + + ((col * 8) >> s) | ||
| 2027 | + + xx * 4; | ||
| 2028 | + build_inter_predictors( | ||
| 2029 | + &mut data[dst_off..], | ||
| 2030 | + dst_stride, | ||
| 2031 | + &refp, | ||
| 2032 | + col, | ||
| 2033 | + row, | ||
| 2034 | + xx * 4, | ||
| 2035 | + y * 4, | ||
| 2036 | + 4, | ||
| 2037 | + 4, | ||
| 2038 | + mv, | ||
| 2039 | + mb_left, | ||
| 2040 | + mb_to_right, | ||
| 2041 | + mb_top, | ||
| 2042 | + mb_to_bottom, | ||
| 2043 | + ss_x, | ||
| 2044 | + ss_y, | ||
| 2045 | + kernel, | ||
| 2046 | + rf == 1, | ||
| 2047 | + scratch, | ||
| 2048 | + temp, | ||
| 2049 | + ); | ||
| 2050 | + } | ||
| 2051 | + } | ||
| 2052 | + } else { | ||
| 2053 | + let (data, _) = planes.plane(plane); | ||
| 2054 | + let dst_off = ((row * 8) >> s) * dst_stride + ((col * 8) >> s); | ||
| 2055 | + build_inter_predictors( | ||
| 2056 | + &mut data[dst_off..], | ||
| 2057 | + dst_stride, | ||
| 2058 | + &refp, | ||
| 2059 | + col, | ||
| 2060 | + row, | ||
| 2061 | + 0, | ||
| 2062 | + 0, | ||
| 2063 | + pw, | ||
| 2064 | + ph, | ||
| 2065 | + info.mv[rf], | ||
| 2066 | + mb_left, | ||
| 2067 | + mb_to_right, | ||
| 2068 | + mb_top, | ||
| 2069 | + mb_to_bottom, | ||
| 2070 | + ss_x, | ||
| 2071 | + ss_y, | ||
| 2072 | + kernel, | ||
| 2073 | + rf == 1, | ||
| 2074 | + scratch, | ||
| 2075 | + temp, | ||
| 2076 | + ); | ||
| 2077 | + } | ||
| 2078 | + } | ||
| 2079 | + Ok(()) | ||
| 2080 | +} | ||
| 2081 | + | ||
| 2082 | +/// `read_interp_filter` (`vp9_decodeframe.c:1482`): the header's filter is a | ||
| 2083 | +/// literal index through `literal_to_filter[] = {EIGHTTAP_SMOOTH, EIGHTTAP, | ||
| 2084 | +/// EIGHTTAP_SHARP, BILINEAR}` — the spec's literal order is NOT libvpx's | ||
| 2085 | +/// filter numbering (`#define EIGHTTAP 0`, `EIGHTTAP_SMOOTH 1`, | ||
| 2086 | +/// `EIGHTTAP_SHARP 2`, `BILINEAR 3`), and `vp9_filter_kernels` (the kernel | ||
| 2087 | +/// table) is indexed by the latter. A switchable frame stores SWITCHABLE (4) | ||
| 2088 | +/// and picks per block, where the tree's leaves already use libvpx numbering. | ||
| 2089 | +fn interp_filter_of(hdr: &FrameHeader) -> u8 { | ||
| 2090 | + const LITERAL_TO_FILTER: [u8; 4] = [1, 0, 2, 3]; | ||
| 2091 | + let v = hdr.interpolation_filter as u8; | ||
| 2092 | + if v == crate::inter::SWITCHABLE { | ||
| 2093 | + v | ||
| 2094 | + } else { | ||
| 2095 | + LITERAL_TO_FILTER[v as usize] | ||
| 2096 | + } | ||
| 2097 | +} | ||
crates/ec-vp9/src/inter.rsmodified+7 -0
| @@ -670,6 +670,13 @@ fn find_mv_refs( | |||
| 670 | &mut count, | 670 | &mut count, |
| 671 | early_break, | 671 | early_break, |
| 672 | ); | 672 | ); |
| 673 | + // The C macro's successful second add is `goto Done` | ||
| 674 | + // (vp9_decodemv.c:475-480), which leaves the search | ||
| 675 | + // loop; without this break the next candidate would | ||
| 676 | + // write `mv_ref_list[2]` past the two-entry list. | ||
| 677 | + if done { | ||
| 678 | + break; | ||
| 679 | + } | ||
| 673 | } | 680 | } |
| 674 | } | 681 | } |
| 675 | } | 682 | } |
crates/ec-vp9/src/lib.rsmodified+8 -3
| @@ -1,11 +1,12 @@ | |||
| 1 | -//! Native VP9 (profile 0, 8-bit 4:2:0 keyframes) decoder. | 1 | +//! Native VP9 (profile 0, 8-bit 4:2:0) decoder. |
| 2 | //! | 2 | //! |
| 3 | //! The crate implements the VP9 bitstream specification directly | 3 | //! The crate implements the VP9 bitstream specification directly |
| 4 | //! ([spec]); libvpx's output is used only as a test oracle | 4 | //! ([spec]); libvpx's output is used only as a test oracle |
| 5 | //! (sample-exactness witnesses against ffmpeg's libvpx-based decoder), | 5 | //! (sample-exactness witnesses against ffmpeg's libvpx-based decoder), |
| 6 | //! never as code linked at runtime. Every module documents the spec | 6 | //! never as code linked at runtime. Every module documents the spec |
| 7 | -//! section it implements. Inter-frame prediction is refused by name; | 7 | +//! section it implements. Inter frames decode through motion compensation, |
| 8 | -//! this lane is keyframes only. | 8 | +//! residual reconstruction and the loop filter; intra-only frames, other |
| 9 | +//! profiles and other subsamplings are refused by name. | ||
| 9 | //! | 10 | //! |
| 10 | //! [spec]: https://www.webmproject.org/docs/vp9/ | 11 | //! [spec]: https://www.webmproject.org/docs/vp9/ |
| 11 | //! | 12 | //! |
| @@ -22,6 +23,7 @@ pub mod header; | |||
| 22 | pub(crate) mod inter; | 23 | pub(crate) mod inter; |
| 23 | pub mod intra; | 24 | pub mod intra; |
| 24 | pub mod loopfilter; | 25 | pub mod loopfilter; |
| 26 | +pub(crate) mod mc; | ||
| 25 | pub mod modes; | 27 | pub mod modes; |
| 26 | pub mod stream; | 28 | pub mod stream; |
| 27 | pub mod tables; | 29 | pub mod tables; |
| @@ -78,3 +80,6 @@ cached_gate!(interdump_enabled, "EC_VP9_INTERDUMP"); | |||
| 78 | cached_gate!(mvdbg_enabled, "EC_VP9_MVDGB"); | 80 | cached_gate!(mvdbg_enabled, "EC_VP9_MVDGB"); |
| 79 | cached_gate!(mvdbg2_enabled, "EC_VP9_MVDGB2"); | 81 | cached_gate!(mvdbg2_enabled, "EC_VP9_MVDGB2"); |
| 80 | cached_gate!(mvdump_enabled, "EC_VP9_MVDUMP"); | 82 | cached_gate!(mvdump_enabled, "EC_VP9_MVDUMP"); |
| 83 | +// [scratch] per-transform-block token state (the inter pixel lane). | ||
| 84 | +cached_gate!(tokdbg_enabled, "EC_VP9_TOKDGB"); | ||
| 85 | +cached_gate!(lfmasksy_enabled, "EC_VP9_LFMASKSY"); | ||
crates/ec-vp9/src/loopfilter.rsmodified+59 -5
| @@ -20,6 +20,26 @@ | |||
| 20 | 20 | ||
| 21 | use crate::tables::*; | 21 | use crate::tables::*; |
| 22 | 22 | ||
| 23 | +/// `LFTRACE` gate, optionally limited to one frame by `EC_VP9_LF_FRAME` | ||
| 24 | +/// (the n-th `filter_frame` call of the process — the decode order's frame | ||
| 25 | +/// index). The oracle's own LF trace is gated the same way, which is what lets | ||
| 26 | +/// the two edge lists be diffed per frame. | ||
| 27 | +fn lftrace_on() -> bool { | ||
| 28 | + use std::sync::atomic::{AtomicUsize, Ordering}; | ||
| 29 | + if std::env::var_os("LFTRACE").is_none() { | ||
| 30 | + return false; | ||
| 31 | + } | ||
| 32 | + static CALLS: AtomicUsize = AtomicUsize::new(0); | ||
| 33 | + let n = CALLS.fetch_add(1, Ordering::Relaxed); | ||
| 34 | + match std::env::var("EC_VP9_LF_FRAME") | ||
| 35 | + .ok() | ||
| 36 | + .and_then(|v| v.parse::<usize>().ok()) | ||
| 37 | + { | ||
| 38 | + Some(f) => f == n, | ||
| 39 | + None => true, | ||
| 40 | + } | ||
| 41 | +} | ||
| 42 | + | ||
| 23 | /// `vp9_loopfilter.c` UV mask tables (BLOCK_SIZES indexed 4X4..64X64). | 43 | /// `vp9_loopfilter.c` UV mask tables (BLOCK_SIZES indexed 4X4..64X64). |
| 24 | const UV_LEFT_PRED: [u16; 13] = [ | 44 | const UV_LEFT_PRED: [u16; 13] = [ |
| 25 | 0x0001, 0x0001, 0x0001, 0x0001, 0x0001, 0x0001, 0x0001, 0x0011, 0x0001, 0x0011, 0x1111, 0x0011, | 45 | 0x0001, 0x0001, 0x0001, 0x0001, 0x0001, 0x0001, 0x0001, 0x0011, 0x0001, 0x0011, 0x1111, 0x0011, |
| @@ -305,6 +325,13 @@ pub(crate) struct LfGrids { | |||
| 305 | pub blk: Vec<(u8, u32, u32)>, | 325 | pub blk: Vec<(u8, u32, u32)>, |
| 306 | /// Per-8x8 BLOCK-level tx size (sub-8x8 blocks record TX_4X4). | 326 | /// Per-8x8 BLOCK-level tx size (sub-8x8 blocks record TX_4X4). |
| 307 | pub otx: Vec<u8>, | 327 | pub otx: Vec<u8>, |
| 328 | + /// Per-8x8 `mi->skip && is_inter_block(mi)` AFTER libvpx's promotion | ||
| 329 | + /// (`decodeframe.c:1043`: `if (!less8x8 && eobtotal == 0) mi->skip = 1`). | ||
| 330 | + /// Both mask builders return right after the prediction masks when this | ||
| 331 | + /// is set, so a skipped inter block keeps only its block-edge (prediction) | ||
| 332 | + /// masks. Keyframes never set it (`is_inter_block` is false); `false` is | ||
| 333 | + /// the correct value there. | ||
| 334 | + pub skip_inter: Vec<bool>, | ||
| 308 | pub mi_cols: usize, | 335 | pub mi_cols: usize, |
| 309 | pub mi_rows: usize, | 336 | pub mi_rows: usize, |
| 310 | } | 337 | } |
| @@ -342,6 +369,9 @@ impl LfGrids { | |||
| 342 | let sh = (c & 7) + ((r & 7) << 3); | 369 | let sh = (c & 7) + ((r & 7) << 3); |
| 343 | left[tx] |= LUMA_LEFT_PRED[b] << sh; | 370 | left[tx] |= LUMA_LEFT_PRED[b] << sh; |
| 344 | above[tx] |= LUMA_ABOVE_PRED[b] << sh; | 371 | above[tx] |= LUMA_ABOVE_PRED[b] << sh; |
| 372 | + if self.skip_inter[cell] { | ||
| 373 | + return; | ||
| 374 | + } | ||
| 345 | left[tx] |= (LUMA_SIZE_MASK[b] & LUMA_LEFT_TXF[tx]) << sh; | 375 | left[tx] |= (LUMA_SIZE_MASK[b] & LUMA_LEFT_TXF[tx]) << sh; |
| 346 | above[tx] |= (LUMA_SIZE_MASK[b] & LUMA_ABOVE_TXF[tx]) << sh; | 376 | above[tx] |= (LUMA_SIZE_MASK[b] & LUMA_ABOVE_TXF[tx]) << sh; |
| 347 | if tx == TX_4X4 { | 377 | if tx == TX_4X4 { |
| @@ -496,6 +526,24 @@ impl LfGrids { | |||
| 496 | left[i] &= 0xfefefefefefefefe; | 526 | left[i] &= 0xfefefefefefefefe; |
| 497 | } | 527 | } |
| 498 | } | 528 | } |
| 529 | + // [scratch] EC_VP9_LFMASKSY: per-superblock luma masks after | ||
| 530 | + // `vp9_adjust_mask`, in the oracle's `LFMASKSY` field order. | ||
| 531 | + if crate::lfmasksy_enabled() { | ||
| 532 | + eprintln!( | ||
| 533 | + "LFY {} {} {:016x} {:016x} {:016x} {:016x} {:016x} {:016x} {:016x} {:016x} {:016x}", | ||
| 534 | + sbr, | ||
| 535 | + sbc, | ||
| 536 | + left[TX_4X4], | ||
| 537 | + left[TX_8X8], | ||
| 538 | + left[TX_16X16], | ||
| 539 | + left[TX_32X32], | ||
| 540 | + above[TX_4X4], | ||
| 541 | + above[TX_8X8], | ||
| 542 | + above[TX_16X16], | ||
| 543 | + above[TX_32X32], | ||
| 544 | + int4 | ||
| 545 | + ); | ||
| 546 | + } | ||
| 499 | (left, above, int4) | 547 | (left, above, int4) |
| 500 | } | 548 | } |
| 501 | 549 | ||
| @@ -529,6 +577,7 @@ impl LfGrids { | |||
| 529 | uv_h: usize, | 577 | uv_h: usize, |
| 530 | sharpness: u8, | 578 | sharpness: u8, |
| 531 | ) { | 579 | ) { |
| 580 | + let lftrace = lftrace_on(); | ||
| 532 | // Luma masks, ported from vp9_setup_mask/build_masks and | 581 | // Luma masks, ported from vp9_setup_mask/build_masks and |
| 533 | // vp9_adjust_mask (bit i = MI row i>>3, MI col i&7; each bit | 582 | // vp9_adjust_mask (bit i = MI row i>>3, MI col i&7; each bit |
| 534 | // covers an 8x8 cell). Edges are emitted from the masks. | 583 | // covers an 8x8 cell). Edges are emitted from the masks. |
| @@ -570,7 +619,7 @@ impl LfGrids { | |||
| 570 | t = 8; | 619 | t = 8; |
| 571 | } | 620 | } |
| 572 | lpf_edge(y, yy * stride + xx, stride, 1, t, bl, li, th, 8); | 621 | lpf_edge(y, yy * stride + xx, stride, 1, t, bl, li, th, 8); |
| 573 | - if std::env::var_os("LFTRACE").is_some() { | 622 | + if lftrace { |
| 574 | let kind = match t { | 623 | let kind = match t { |
| 575 | 16 => "V16", | 624 | 16 => "V16", |
| 576 | 8 => "V8", | 625 | 8 => "V8", |
| @@ -581,7 +630,7 @@ impl LfGrids { | |||
| 581 | } | 630 | } |
| 582 | if int4 & bit != 0 && xx + 8 <= y_w && yy + 8 <= y_h { | 631 | if int4 & bit != 0 && xx + 8 <= y_w && yy + 8 <= y_h { |
| 583 | lpf_edge(y, yy * stride + xx + 4, stride, 1, 4, bl, li, th, 8); | 632 | lpf_edge(y, yy * stride + xx + 4, stride, 1, 4, bl, li, th, 8); |
| 584 | - if std::env::var_os("LFTRACE").is_some() { | 633 | + if lftrace { |
| 585 | eprintln!("V4I {} {}", xx + 4, yy); | 634 | eprintln!("V4I {} {}", xx + 4, yy); |
| 586 | } | 635 | } |
| 587 | } | 636 | } |
| @@ -619,7 +668,7 @@ impl LfGrids { | |||
| 619 | t = 8; | 668 | t = 8; |
| 620 | } | 669 | } |
| 621 | lpf_edge(y, yy * stride + xx, stride, 0, t, bl, li, th, 8); | 670 | lpf_edge(y, yy * stride + xx, stride, 0, t, bl, li, th, 8); |
| 622 | - if std::env::var_os("LFTRACE").is_some() { | 671 | + if lftrace { |
| 623 | let kind = match t { | 672 | let kind = match t { |
| 624 | 16 => "H16", | 673 | 16 => "H16", |
| 625 | 8 => "H8", | 674 | 8 => "H8", |
| @@ -637,7 +686,7 @@ impl LfGrids { | |||
| 637 | // vp9_loopfilter.c:1382) and in non420 under `ss_y`. | 686 | // vp9_loopfilter.c:1382) and in non420 under `ss_y`. |
| 638 | if int4 & bit != 0 && yy + 8 <= y_h && xx + 8 <= y_w { | 687 | if int4 & bit != 0 && yy + 8 <= y_h && xx + 8 <= y_w { |
| 639 | lpf_edge(y, (yy + 4) * stride + xx, stride, 0, 4, bl, li, th, 8); | 688 | lpf_edge(y, (yy + 4) * stride + xx, stride, 0, 4, bl, li, th, 8); |
| 640 | - if std::env::var_os("LFTRACE").is_some() { | 689 | + if lftrace { |
| 641 | eprintln!("H4I {} {}", xx, yy + 4); | 690 | eprintln!("H4I {} {}", xx, yy + 4); |
| 642 | } | 691 | } |
| 643 | } | 692 | } |
| @@ -678,8 +727,11 @@ impl LfGrids { | |||
| 678 | let shift = (((r & 7) >> 1) << 2) + ((c & 7) >> 1); | 727 | let shift = (((r & 7) >> 1) << 2) + ((c & 7) >> 1); |
| 679 | let b = bsize as usize; | 728 | let b = bsize as usize; |
| 680 | left[uv_tx] |= UV_LEFT_PRED[b] << shift; | 729 | left[uv_tx] |= UV_LEFT_PRED[b] << shift; |
| 681 | - left[uv_tx] |= (UV_SIZE_MASK[b] & UV_LEFT_TX[uv_tx]) << shift; | ||
| 682 | above[uv_tx] |= UV_ABOVE_PRED[b] << shift; | 730 | above[uv_tx] |= UV_ABOVE_PRED[b] << shift; |
| 731 | + if self.skip_inter[cell] { | ||
| 732 | + continue; | ||
| 733 | + } | ||
| 734 | + left[uv_tx] |= (UV_SIZE_MASK[b] & UV_LEFT_TX[uv_tx]) << shift; | ||
| 683 | above[uv_tx] |= (UV_SIZE_MASK[b] & UV_ABOVE_TX[uv_tx]) << shift; | 735 | above[uv_tx] |= (UV_SIZE_MASK[b] & UV_ABOVE_TX[uv_tx]) << shift; |
| 684 | if uv_tx == TX_4X4 { | 736 | if uv_tx == TX_4X4 { |
| 685 | int4 |= UV_SIZE_MASK[b] << shift; | 737 | int4 |= UV_SIZE_MASK[b] << shift; |
| @@ -893,6 +945,8 @@ mod tests { | |||
| 893 | level8: vec![level; mi_cols * mi_rows], | 945 | level8: vec![level; mi_cols * mi_rows], |
| 894 | blk: vec![(0u8, 0, 0); mi_cols * mi_rows], // 0 == BLOCK_4X4 | 946 | blk: vec![(0u8, 0, 0); mi_cols * mi_rows], // 0 == BLOCK_4X4 |
| 895 | otx: vec![TX_4X4 as u8; mi_cols * mi_rows], | 947 | otx: vec![TX_4X4 as u8; mi_cols * mi_rows], |
| 948 | + // Keyframe-shaped fixture: no inter block, so no skip promotion. | ||
| 949 | + skip_inter: vec![false; mi_cols * mi_rows], | ||
| 896 | mi_cols, | 950 | mi_cols, |
| 897 | mi_rows, | 951 | mi_rows, |
| 898 | } | 952 | } |
crates/ec-vp9/src/mc.rsadded+558 -0
| @@ -0,0 +1,558 @@ | |||
| 1 | +//! Motion compensation: the decoder's inter predictor (`dec_build_inter_predictors`, | ||
| 2 | +//! libvpx `vp9/decoder/vp9_decodeframe.c:593`), the on-the-fly reference border | ||
| 3 | +//! extension (`build_mc_border`, :473), the UMV-border MV clamp | ||
| 4 | +//! (`clamp_mv_to_umv_border_sb`, `vp9/common/vp9_reconinter.c:88`), the | ||
| 5 | +//! switchable filter kernels (`vp9/common/vp9_filter.c`) and the sub-pixel | ||
| 6 | +//! convolve (`vpx_dsp/vpx_convolve.c`). | ||
| 7 | +//! | ||
| 8 | +//! The VP9 decoder does NOT extend the reference frame's borders in place: it | ||
| 9 | +//! builds a bordered copy of the block's reference window per prediction | ||
| 10 | +//! (`extend_and_predict`) whenever the window leaves the frame or the frame's | ||
| 11 | +//! dimensions are not a multiple of 8. This module ports that path, unscaled | ||
| 12 | +//! only — a reference slot whose size differs from the current frame is | ||
| 13 | +//! refused by `decode` before any predictor runs. | ||
| 14 | +//! | ||
| 15 | +//! Indexing note: libvpx's convolve helpers take a raw pointer and subtract | ||
| 16 | +//! `SUBPEL_TAPS / 2 - 1` from it, so they read taps *before* the block's first | ||
| 17 | +//! sample. Rust slices cannot go negative, so every helper here takes a buffer | ||
| 18 | +//! plus an `origin` (the block's first sample) and indexes with `isize`. | ||
| 19 | + | ||
| 20 | +/// `SUBPEL_BITS` / `SUBPEL_MASK` / `SUBPEL_SHIFTS` / `SUBPEL_TAPS` | ||
| 21 | +/// (`vpx_dsp/vpx_filter.h:23-26`). | ||
| 22 | +const SUBPEL_BITS: i32 = 4; | ||
| 23 | +const SUBPEL_MASK: i32 = (1 << SUBPEL_BITS) - 1; | ||
| 24 | +const SUBPEL_SHIFTS: i32 = 1 << SUBPEL_BITS; | ||
| 25 | +const TAPS: usize = 8; | ||
| 26 | +/// `FILTER_BITS` (`vpx_dsp/vpx_filter.h:21`). | ||
| 27 | +const FILTER_BITS: i32 = 7; | ||
| 28 | +/// `VP9_INTERP_EXTEND` (`vpx_scale/yv12config.h:25`). | ||
| 29 | +const INTERP_EXTEND: i32 = 4; | ||
| 30 | + | ||
| 31 | +/// `vp9_filter_kernels` (`vp9/common/vp9_filter.c:79`), in libvpx's filter | ||
| 32 | +/// numbering: 0 `EIGHTTAP`, 1 `EIGHTTAP_SMOOTH`, 2 `EIGHTTAP_SHARP`, | ||
| 33 | +/// 3 `BILINEAR` (the 4-tap index 4 is unreachable for profile 0). | ||
| 34 | +pub(crate) const FILTERS: [[[i16; TAPS]; SUBPEL_SHIFTS as usize]; 4] = [ | ||
| 35 | + // sub_pel_filters_8: Lagrangian interpolation filter. | ||
| 36 | + [ | ||
| 37 | + [0, 0, 0, 128, 0, 0, 0, 0], | ||
| 38 | + [0, 1, -5, 126, 8, -3, 1, 0], | ||
| 39 | + [-1, 3, -10, 122, 18, -6, 2, 0], | ||
| 40 | + [-1, 4, -13, 118, 27, -9, 3, -1], | ||
| 41 | + [-1, 4, -16, 112, 37, -11, 4, -1], | ||
| 42 | + [-1, 5, -18, 105, 48, -14, 4, -1], | ||
| 43 | + [-1, 5, -19, 97, 58, -16, 5, -1], | ||
| 44 | + [-1, 6, -19, 88, 68, -18, 5, -1], | ||
| 45 | + [-1, 6, -19, 78, 78, -19, 6, -1], | ||
| 46 | + [-1, 5, -18, 68, 88, -19, 6, -1], | ||
| 47 | + [-1, 5, -16, 58, 97, -19, 5, -1], | ||
| 48 | + [-1, 4, -14, 48, 105, -18, 5, -1], | ||
| 49 | + [-1, 4, -11, 37, 112, -16, 4, -1], | ||
| 50 | + [-1, 3, -9, 27, 118, -13, 4, -1], | ||
| 51 | + [0, 2, -6, 18, 122, -10, 3, -1], | ||
| 52 | + [0, 1, -3, 8, 126, -5, 1, 0], | ||
| 53 | + ], | ||
| 54 | + // sub_pel_filters_8lp: freqmultiplier = 0.5 (EIGHTTAP_SMOOTH). | ||
| 55 | + [ | ||
| 56 | + [0, 0, 0, 128, 0, 0, 0, 0], | ||
| 57 | + [-3, -1, 32, 64, 38, 1, -3, 0], | ||
| 58 | + [-2, -2, 29, 63, 41, 2, -3, 0], | ||
| 59 | + [-2, -2, 26, 63, 43, 4, -4, 0], | ||
| 60 | + [-2, -3, 24, 62, 46, 5, -4, 0], | ||
| 61 | + [-2, -3, 21, 60, 49, 7, -4, 0], | ||
| 62 | + [-1, -4, 18, 59, 51, 9, -4, 0], | ||
| 63 | + [-1, -4, 16, 57, 53, 12, -4, -1], | ||
| 64 | + [-1, -4, 14, 55, 55, 14, -4, -1], | ||
| 65 | + [-1, -4, 12, 53, 57, 16, -4, -1], | ||
| 66 | + [0, -4, 9, 51, 59, 18, -4, -1], | ||
| 67 | + [0, -4, 7, 49, 60, 21, -3, -2], | ||
| 68 | + [0, -4, 5, 46, 62, 24, -3, -2], | ||
| 69 | + [0, -4, 4, 43, 63, 26, -2, -2], | ||
| 70 | + [0, -3, 2, 41, 63, 29, -2, -2], | ||
| 71 | + [0, -3, 1, 38, 64, 32, -1, -3], | ||
| 72 | + ], | ||
| 73 | + // sub_pel_filters_8s: DCT based filter (EIGHTTAP_SHARP). | ||
| 74 | + [ | ||
| 75 | + [0, 0, 0, 128, 0, 0, 0, 0], | ||
| 76 | + [-1, 3, -7, 127, 8, -3, 1, 0], | ||
| 77 | + [-2, 5, -13, 125, 17, -6, 3, -1], | ||
| 78 | + [-3, 7, -17, 121, 27, -10, 5, -2], | ||
| 79 | + [-4, 9, -20, 115, 37, -13, 6, -2], | ||
| 80 | + [-4, 10, -23, 108, 48, -16, 8, -3], | ||
| 81 | + [-4, 10, -24, 100, 59, -19, 9, -3], | ||
| 82 | + [-4, 11, -24, 90, 70, -21, 10, -4], | ||
| 83 | + [-4, 11, -23, 80, 80, -23, 11, -4], | ||
| 84 | + [-4, 10, -21, 70, 90, -24, 11, -4], | ||
| 85 | + [-3, 9, -19, 59, 100, -24, 10, -4], | ||
| 86 | + [-3, 8, -16, 48, 108, -23, 10, -4], | ||
| 87 | + [-2, 6, -13, 37, 115, -20, 9, -4], | ||
| 88 | + [-2, 5, -10, 27, 121, -17, 7, -3], | ||
| 89 | + [-1, 3, -6, 17, 125, -13, 5, -2], | ||
| 90 | + [0, 1, -3, 8, 127, -7, 3, -1], | ||
| 91 | + ], | ||
| 92 | + // bilinear_filters. | ||
| 93 | + [ | ||
| 94 | + [0, 0, 0, 128, 0, 0, 0, 0], | ||
| 95 | + [0, 0, 0, 120, 8, 0, 0, 0], | ||
| 96 | + [0, 0, 0, 112, 16, 0, 0, 0], | ||
| 97 | + [0, 0, 0, 104, 24, 0, 0, 0], | ||
| 98 | + [0, 0, 0, 96, 32, 0, 0, 0], | ||
| 99 | + [0, 0, 0, 88, 40, 0, 0, 0], | ||
| 100 | + [0, 0, 0, 80, 48, 0, 0, 0], | ||
| 101 | + [0, 0, 0, 72, 56, 0, 0, 0], | ||
| 102 | + [0, 0, 0, 64, 64, 0, 0, 0], | ||
| 103 | + [0, 0, 0, 56, 72, 0, 0, 0], | ||
| 104 | + [0, 0, 0, 48, 80, 0, 0, 0], | ||
| 105 | + [0, 0, 0, 40, 88, 0, 0, 0], | ||
| 106 | + [0, 0, 0, 32, 96, 0, 0, 0], | ||
| 107 | + [0, 0, 0, 24, 104, 0, 0, 0], | ||
| 108 | + [0, 0, 0, 16, 112, 0, 0, 0], | ||
| 109 | + [0, 0, 0, 8, 120, 0, 0, 0], | ||
| 110 | + ], | ||
| 111 | +]; | ||
| 112 | + | ||
| 113 | +/// `clip_pixel` (`vpx_dsp/vpx_dsp_common.h`). | ||
| 114 | +fn clip_pixel(v: i32) -> u8 { | ||
| 115 | + v.clamp(0, 255) as u8 | ||
| 116 | +} | ||
| 117 | + | ||
| 118 | +/// `ROUND_POWER_OF_TWO(v, n)`. | ||
| 119 | +fn round_pow2(v: i32, n: i32) -> i32 { | ||
| 120 | + (v + (1 << (n - 1))) >> n | ||
| 121 | +} | ||
| 122 | + | ||
| 123 | +/// Read `buf[i]` for a possibly negative `i`. Every caller keeps `i` inside | ||
| 124 | +/// the buffer (the border copy guarantees the tap margins exist). | ||
| 125 | +#[inline] | ||
| 126 | +fn at(buf: &[u8], i: isize) -> u8 { | ||
| 127 | + buf[i as usize] | ||
| 128 | +} | ||
| 129 | + | ||
| 130 | +/// `clamp_mv_to_umv_border_sb` (`vp9/common/vp9_reconinter.c:88`), returning | ||
| 131 | +/// the MV in 1/16-pel (`MV_PRECISION_Q4`) units. | ||
| 132 | +/// | ||
| 133 | +/// `mb_*` are the block's distances to the frame edges in 1/8-pel units | ||
| 134 | +/// (`xd->mb_to_*_edge`, i.e. `±mi * 64`); `bw`/`bh` are the PLANE block's | ||
| 135 | +/// pixel dimensions. `clamp_mv(mv, min_col, max_col, min_row, max_row)`: | ||
| 136 | +/// columns are bounded by the left/right edges, rows by the top/bottom. | ||
| 137 | +pub(crate) fn clamp_mv_to_umv_border_sb( | ||
| 138 | + mv: (i32, i32), | ||
| 139 | + bw: i32, | ||
| 140 | + bh: i32, | ||
| 141 | + mb_left: i32, | ||
| 142 | + mb_right: i32, | ||
| 143 | + mb_top: i32, | ||
| 144 | + mb_bottom: i32, | ||
| 145 | + ss_x: usize, | ||
| 146 | + ss_y: usize, | ||
| 147 | +) -> (i32, i32) { | ||
| 148 | + let spel_left = (INTERP_EXTEND + bw) << SUBPEL_BITS; | ||
| 149 | + let spel_right = spel_left - SUBPEL_SHIFTS; | ||
| 150 | + let spel_top = (INTERP_EXTEND + bh) << SUBPEL_BITS; | ||
| 151 | + let spel_bottom = spel_top - SUBPEL_SHIFTS; | ||
| 152 | + let xs = 1 << (1 - ss_x); | ||
| 153 | + let ys = 1 << (1 - ss_y); | ||
| 154 | + let col = (mv.1 * xs).clamp(mb_left * xs - spel_left, mb_right * xs + spel_right); | ||
| 155 | + let row = (mv.0 * ys).clamp(mb_top * ys - spel_top, mb_bottom * ys + spel_bottom); | ||
| 156 | + (row, col) | ||
| 157 | +} | ||
| 158 | + | ||
| 159 | +/// `build_mc_border` (`vp9/decoder/vp9_decodeframe.c:473`): copy the | ||
| 160 | +/// `b_w` x `b_h` window whose top-left is frame pixel `(x, y)` into `dst`, | ||
| 161 | +/// replicating the frame's first/last row and column for reads outside | ||
| 162 | +/// `[0, w)` x `[0, h)`. | ||
| 163 | +#[allow(clippy::too_many_arguments)] | ||
| 164 | +fn build_mc_border( | ||
| 165 | + src: &[u8], | ||
| 166 | + src_stride: usize, | ||
| 167 | + dst: &mut [u8], | ||
| 168 | + dst_stride: usize, | ||
| 169 | + x: i32, | ||
| 170 | + mut y: i32, | ||
| 171 | + b_w: usize, | ||
| 172 | + mut b_h: usize, | ||
| 173 | + w: i32, | ||
| 174 | + h: i32, | ||
| 175 | +) { | ||
| 176 | + // C computes `ref_row = src - x - y * src_stride`; for negative y the | ||
| 177 | + // pointer arithmetic leaves the buffer and the row-index equivalent below | ||
| 178 | + // is the same read. | ||
| 179 | + let mut row = if y >= h { | ||
| 180 | + (h - 1) as usize | ||
| 181 | + } else if y > 0 { | ||
| 182 | + y as usize | ||
| 183 | + } else { | ||
| 184 | + 0 | ||
| 185 | + }; | ||
| 186 | + let mut out = 0usize; | ||
| 187 | + loop { | ||
| 188 | + let left = if x < 0 { (-x).min(b_w as i32) } else { 0 } as usize; | ||
| 189 | + let right = if x + b_w as i32 > w { | ||
| 190 | + (x + b_w as i32 - w).min(b_w as i32) | ||
| 191 | + } else { | ||
| 192 | + 0 | ||
| 193 | + } as usize; | ||
| 194 | + let copy = b_w - left - right; | ||
| 195 | + | ||
| 196 | + let base = row * src_stride; | ||
| 197 | + if left > 0 { | ||
| 198 | + let v = src[base]; | ||
| 199 | + dst[out..out + left].fill(v); | ||
| 200 | + } | ||
| 201 | + if copy > 0 { | ||
| 202 | + let from = (x + left as i32) as usize; | ||
| 203 | + dst[out + left..out + left + copy] | ||
| 204 | + .copy_from_slice(&src[base + from..base + from + copy]); | ||
| 205 | + } | ||
| 206 | + if right > 0 { | ||
| 207 | + let v = src[base + (w - 1) as usize]; | ||
| 208 | + dst[out + left + copy..out + left + copy + right].fill(v); | ||
| 209 | + } | ||
| 210 | + | ||
| 211 | + out += dst_stride; | ||
| 212 | + y += 1; | ||
| 213 | + if y > 0 && y < h { | ||
| 214 | + row += 1; | ||
| 215 | + } | ||
| 216 | + b_h -= 1; | ||
| 217 | + if b_h == 0 { | ||
| 218 | + break; | ||
| 219 | + } | ||
| 220 | + } | ||
| 221 | +} | ||
| 222 | + | ||
| 223 | +/// `convolve_horiz` / `convolve_avg_horiz` (`vpx_dsp/vpx_convolve.c:22`). | ||
| 224 | +/// `origin` is the block's first sample in `buf`; taps are read from | ||
| 225 | +/// `origin + y*stride + (x_q4 >> SUBPEL_BITS) - (SUBPEL_TAPS / 2 - 1) + k`. | ||
| 226 | +#[allow(clippy::too_many_arguments)] | ||
| 227 | +fn convolve_horiz( | ||
| 228 | + buf: &[u8], | ||
| 229 | + stride: usize, | ||
| 230 | + origin: isize, | ||
| 231 | + dst: &mut [u8], | ||
| 232 | + dst_stride: usize, | ||
| 233 | + f: &[[i16; TAPS]; SUBPEL_SHIFTS as usize], | ||
| 234 | + x0_q4: i32, | ||
| 235 | + w: usize, | ||
| 236 | + rows: usize, | ||
| 237 | + avg: bool, | ||
| 238 | +) { | ||
| 239 | + for y in 0..rows { | ||
| 240 | + let mut x_q4 = x0_q4; | ||
| 241 | + for x in 0..w { | ||
| 242 | + let base = origin + y as isize * stride as isize + (x_q4 >> SUBPEL_BITS) as isize | ||
| 243 | + - (TAPS as isize / 2 - 1); | ||
| 244 | + let filt = &f[(x_q4 & SUBPEL_MASK) as usize]; | ||
| 245 | + let mut sum = 0i32; | ||
| 246 | + for k in 0..TAPS { | ||
| 247 | + sum += at(buf, base + k as isize) as i32 * filt[k] as i32; | ||
| 248 | + } | ||
| 249 | + let v = clip_pixel(round_pow2(sum, FILTER_BITS)) as i32; | ||
| 250 | + let d = y * dst_stride + x; | ||
| 251 | + dst[d] = if avg { | ||
| 252 | + round_pow2(dst[d] as i32 + v, 1) as u8 | ||
| 253 | + } else { | ||
| 254 | + v as u8 | ||
| 255 | + }; | ||
| 256 | + x_q4 += SUBPEL_SHIFTS; | ||
| 257 | + } | ||
| 258 | + } | ||
| 259 | +} | ||
| 260 | + | ||
| 261 | +/// `convolve_vert` / `convolve_avg_vert` (`vpx_dsp/vpx_convolve.c:80`). | ||
| 262 | +fn convolve_vert( | ||
| 263 | + buf: &[u8], | ||
| 264 | + stride: usize, | ||
| 265 | + origin: isize, | ||
| 266 | + dst: &mut [u8], | ||
| 267 | + dst_stride: usize, | ||
| 268 | + f: &[[i16; TAPS]; SUBPEL_SHIFTS as usize], | ||
| 269 | + y0_q4: i32, | ||
| 270 | + w: usize, | ||
| 271 | + h: usize, | ||
| 272 | + avg: bool, | ||
| 273 | +) { | ||
| 274 | + for x in 0..w { | ||
| 275 | + let mut y_q4 = y0_q4; | ||
| 276 | + for y in 0..h { | ||
| 277 | + let base = origin | ||
| 278 | + + ((y_q4 >> SUBPEL_BITS) as isize - (TAPS as isize / 2 - 1)) * stride as isize | ||
| 279 | + + x as isize; | ||
| 280 | + let filt = &f[(y_q4 & SUBPEL_MASK) as usize]; | ||
| 281 | + let mut sum = 0i32; | ||
| 282 | + for k in 0..TAPS { | ||
| 283 | + sum += at(buf, base + k as isize * stride as isize) as i32 * filt[k] as i32; | ||
| 284 | + } | ||
| 285 | + let v = clip_pixel(round_pow2(sum, FILTER_BITS)) as i32; | ||
| 286 | + let d = y * dst_stride + x; | ||
| 287 | + dst[d] = if avg { | ||
| 288 | + round_pow2(dst[d] as i32 + v, 1) as u8 | ||
| 289 | + } else { | ||
| 290 | + v as u8 | ||
| 291 | + }; | ||
| 292 | + y_q4 += SUBPEL_SHIFTS; | ||
| 293 | + } | ||
| 294 | + } | ||
| 295 | +} | ||
| 296 | + | ||
| 297 | +/// `inter_predictor` + `sf->predict` (`vp9/common/vp9_reconinter.h:23`, | ||
| 298 | +/// `vp9/common/vp9_scale.c:79`): for the unscaled case `predict[0][0]` is | ||
| 299 | +/// copy/avg, `predict[0][1][*]` the vertical kernel, `predict[1][0][*]` the | ||
| 300 | +/// horizontal one and `predict[1][1][*]` the 2D convolve. | ||
| 301 | +/// | ||
| 302 | +/// `w` x `h` output samples written at `dst`; `buf`/`origin` locate the | ||
| 303 | +/// block's first reference sample. `temp` is the `vpx_convolve8` intermediate | ||
| 304 | +/// (stride-64 scratch of `64 * 135` bytes plus a `64 * 64` second scratch for | ||
| 305 | +/// the averaging variant). | ||
| 306 | +#[allow(clippy::too_many_arguments)] | ||
| 307 | +fn inter_predictor( | ||
| 308 | + buf: &[u8], | ||
| 309 | + stride: usize, | ||
| 310 | + origin: isize, | ||
| 311 | + dst: &mut [u8], | ||
| 312 | + dst_stride: usize, | ||
| 313 | + f: &[[i16; TAPS]; SUBPEL_SHIFTS as usize], | ||
| 314 | + subpel_x: i32, | ||
| 315 | + subpel_y: i32, | ||
| 316 | + w: usize, | ||
| 317 | + h: usize, | ||
| 318 | + avg: bool, | ||
| 319 | + temp: &mut [u8], | ||
| 320 | +) { | ||
| 321 | + if subpel_x == 0 && subpel_y == 0 { | ||
| 322 | + for y in 0..h { | ||
| 323 | + for x in 0..w { | ||
| 324 | + let s = at(buf, origin + (y * stride + x) as isize) as i32; | ||
| 325 | + let d = y * dst_stride + x; | ||
| 326 | + dst[d] = if avg { | ||
| 327 | + round_pow2(dst[d] as i32 + s, 1) as u8 | ||
| 328 | + } else { | ||
| 329 | + s as u8 | ||
| 330 | + }; | ||
| 331 | + } | ||
| 332 | + } | ||
| 333 | + return; | ||
| 334 | + } | ||
| 335 | + if subpel_y == 0 { | ||
| 336 | + convolve_horiz(buf, stride, origin, dst, dst_stride, f, subpel_x, w, h, avg); | ||
| 337 | + return; | ||
| 338 | + } | ||
| 339 | + if subpel_x == 0 { | ||
| 340 | + convolve_vert(buf, stride, origin, dst, dst_stride, f, subpel_y, w, h, avg); | ||
| 341 | + return; | ||
| 342 | + } | ||
| 343 | + // vpx_convolve8_c: horizontal pass into a stride-64 intermediate with | ||
| 344 | + // `intermediate_height = (((h - 1) * y_step_q4 + y0_q4) >> SUBPEL_BITS) + | ||
| 345 | + // SUBPEL_TAPS` — for the unscaled `y_step_q4 = 16` that is `h + SUBPEL_TAPS | ||
| 346 | + // - 1`. The horizontal pass starts `SUBPEL_TAPS / 2 - 1` rows ABOVE the | ||
| 347 | + // block (C passes `src - src_stride * (SUBPEL_TAPS / 2 - 1)`). | ||
| 348 | + let intermediate_height = | ||
| 349 | + (((h as i32 - 1) * SUBPEL_SHIFTS + subpel_y) >> SUBPEL_BITS) as usize + TAPS; | ||
| 350 | + let temp_origin = (TAPS as isize / 2 - 1) * 64; | ||
| 351 | + convolve_horiz( | ||
| 352 | + buf, | ||
| 353 | + stride, | ||
| 354 | + origin - (TAPS as isize / 2 - 1) * stride as isize, | ||
| 355 | + temp, | ||
| 356 | + 64, | ||
| 357 | + f, | ||
| 358 | + subpel_x, | ||
| 359 | + w, | ||
| 360 | + intermediate_height, | ||
| 361 | + false, | ||
| 362 | + ); | ||
| 363 | + if avg { | ||
| 364 | + // vpx_convolve8_avg_c: full 2D into a second scratch, then average | ||
| 365 | + // with the destination (which holds the first reference's block). | ||
| 366 | + // `temp[..64 * 135]` is the horizontal intermediate; the region after | ||
| 367 | + // it is the 2D result. | ||
| 368 | + let (horiz, mid2) = temp.split_at_mut(64 * 135); | ||
| 369 | + convolve_vert(horiz, 64, temp_origin, mid2, w, f, subpel_y, w, h, false); | ||
| 370 | + for y in 0..h { | ||
| 371 | + for x in 0..w { | ||
| 372 | + let d = y * dst_stride + x; | ||
| 373 | + dst[d] = round_pow2(dst[d] as i32 + mid2[y * w + x] as i32, 1) as u8; | ||
| 374 | + } | ||
| 375 | + } | ||
| 376 | + return; | ||
| 377 | + } | ||
| 378 | + convolve_vert( | ||
| 379 | + temp, | ||
| 380 | + 64, | ||
| 381 | + temp_origin, | ||
| 382 | + dst, | ||
| 383 | + dst_stride, | ||
| 384 | + f, | ||
| 385 | + subpel_y, | ||
| 386 | + w, | ||
| 387 | + h, | ||
| 388 | + false, | ||
| 389 | + ); | ||
| 390 | +} | ||
| 391 | + | ||
| 392 | +/// One reference plane of a stored frame. | ||
| 393 | +pub(crate) struct RefPlane<'a> { | ||
| 394 | + /// Plane base — the frame's first visible sample. | ||
| 395 | + pub data: &'a [u8], | ||
| 396 | + /// Row pitch. | ||
| 397 | + pub stride: usize, | ||
| 398 | + /// `y_crop_width` / `uv_crop_width`: the coded extent, which is what | ||
| 399 | + /// `build_mc_border` replicates past. | ||
| 400 | + pub width: usize, | ||
| 401 | + /// `y_crop_height` / `uv_crop_height`. | ||
| 402 | + pub height: usize, | ||
| 403 | +} | ||
| 404 | + | ||
| 405 | +/// `dec_build_inter_predictors` (`vp9/decoder/vp9_decodeframe.c:593`), | ||
| 406 | +/// unscaled: predict one `w` x `h` plane block whose top-left is plane pixel | ||
| 407 | +/// `(x, y)` inside the block at MI `(mi_row, mi_col)`. | ||
| 408 | +#[allow(clippy::too_many_arguments)] | ||
| 409 | +pub(crate) fn build_inter_predictors( | ||
| 410 | + dst: &mut [u8], | ||
| 411 | + dst_stride: usize, | ||
| 412 | + refp: &RefPlane<'_>, | ||
| 413 | + mi_col: usize, | ||
| 414 | + mi_row: usize, | ||
| 415 | + x: usize, | ||
| 416 | + y: usize, | ||
| 417 | + w: usize, | ||
| 418 | + h: usize, | ||
| 419 | + mv: (i32, i32), | ||
| 420 | + mb_left: i32, | ||
| 421 | + mb_right: i32, | ||
| 422 | + mb_top: i32, | ||
| 423 | + mb_bottom: i32, | ||
| 424 | + ss_x: usize, | ||
| 425 | + ss_y: usize, | ||
| 426 | + kernel: usize, | ||
| 427 | + avg: bool, | ||
| 428 | + scratch: &mut Vec<u8>, | ||
| 429 | + temp: &mut Vec<u8>, | ||
| 430 | +) { | ||
| 431 | + let mv_q4 = clamp_mv_to_umv_border_sb( | ||
| 432 | + mv, w as i32, h as i32, mb_left, mb_right, mb_top, mb_bottom, ss_x, ss_y, | ||
| 433 | + ); | ||
| 434 | + | ||
| 435 | + // Co-ordinate of the containing block, pixel precision, plus the | ||
| 436 | + // block-relative offset (`x`, `y`). | ||
| 437 | + let x0_pre = (-mb_left >> (3 + ss_x)) + x as i32; | ||
| 438 | + let y0_pre = (-mb_top >> (3 + ss_y)) + y as i32; | ||
| 439 | + let x0_16 = (x0_pre << SUBPEL_BITS) + mv_q4.1; | ||
| 440 | + let y0_16 = (y0_pre << SUBPEL_BITS) + mv_q4.0; | ||
| 441 | + let subpel_x = mv_q4.1 & SUBPEL_MASK; | ||
| 442 | + let subpel_y = mv_q4.0 & SUBPEL_MASK; | ||
| 443 | + | ||
| 444 | + let frame_width = refp.width as i32; | ||
| 445 | + let frame_height = refp.height as i32; | ||
| 446 | + // `buf_ptr = ref_frame + y0 * stride + x0` is taken AFTER the MV integer | ||
| 447 | + // part but BEFORE the tap padding below, so the direct read keeps its own | ||
| 448 | + // copy of the coordinates. | ||
| 449 | + let x0_mv = x0_pre + (mv_q4.1 >> SUBPEL_BITS); | ||
| 450 | + let y0_mv = y0_pre + (mv_q4.0 >> SUBPEL_BITS); | ||
| 451 | + let mut x0 = x0_mv; | ||
| 452 | + let mut y0 = y0_mv; | ||
| 453 | + | ||
| 454 | + let f = &FILTERS[kernel]; | ||
| 455 | + let _ = mi_col; | ||
| 456 | + let _ = mi_row; | ||
| 457 | + | ||
| 458 | + // `is_scaled || scaled_mv.col || scaled_mv.row || (frame_width & 7) || | ||
| 459 | + // (frame_height & 7)`: the border copy is skipped only when the window is | ||
| 460 | + // provably inside the frame. | ||
| 461 | + if mv_q4.1 != 0 || mv_q4.0 != 0 || (frame_width & 7) != 0 || (frame_height & 7) != 0 { | ||
| 462 | + let mut x1 = ((x0_16 + (w as i32 - 1) * SUBPEL_SHIFTS) >> SUBPEL_BITS) + 1; | ||
| 463 | + let mut y1 = ((y0_16 + (h as i32 - 1) * SUBPEL_SHIFTS) >> SUBPEL_BITS) + 1; | ||
| 464 | + let mut x_pad = false; | ||
| 465 | + let mut y_pad = false; | ||
| 466 | + if subpel_x != 0 { | ||
| 467 | + x0 -= INTERP_EXTEND - 1; | ||
| 468 | + x1 += INTERP_EXTEND; | ||
| 469 | + x_pad = true; | ||
| 470 | + } | ||
| 471 | + if subpel_y != 0 { | ||
| 472 | + y0 -= INTERP_EXTEND - 1; | ||
| 473 | + y1 += INTERP_EXTEND; | ||
| 474 | + y_pad = true; | ||
| 475 | + } | ||
| 476 | + if x0 < 0 | ||
| 477 | + || x0 > frame_width - 1 | ||
| 478 | + || x1 < 0 | ||
| 479 | + || x1 > frame_width - 1 | ||
| 480 | + || y0 < 0 | ||
| 481 | + || y0 > frame_height - 1 | ||
| 482 | + || y1 < 0 | ||
| 483 | + || y1 > frame_height - 1 | ||
| 484 | + { | ||
| 485 | + let b_w = (x1 - x0 + 1) as usize; | ||
| 486 | + let b_h = (y1 - y0 + 1) as usize; | ||
| 487 | + let border_offset = usize::from(y_pad) * 3 * b_w + usize::from(x_pad) * 3; | ||
| 488 | + scratch.resize(b_w * b_h, 0); | ||
| 489 | + build_mc_border( | ||
| 490 | + refp.data, | ||
| 491 | + refp.stride, | ||
| 492 | + scratch, | ||
| 493 | + b_w, | ||
| 494 | + x0, | ||
| 495 | + y0, | ||
| 496 | + b_w, | ||
| 497 | + b_h, | ||
| 498 | + frame_width, | ||
| 499 | + frame_height, | ||
| 500 | + ); | ||
| 501 | + inter_predictor( | ||
| 502 | + scratch, | ||
| 503 | + b_w, | ||
| 504 | + border_offset as isize, | ||
| 505 | + dst, | ||
| 506 | + dst_stride, | ||
| 507 | + f, | ||
| 508 | + subpel_x, | ||
| 509 | + subpel_y, | ||
| 510 | + w, | ||
| 511 | + h, | ||
| 512 | + avg, | ||
| 513 | + temp, | ||
| 514 | + ); | ||
| 515 | + return; | ||
| 516 | + } | ||
| 517 | + } | ||
| 518 | + // Inside the frame: read directly. The `x0`/`y0` checks above guarantee | ||
| 519 | + // the `SUBPEL_TAPS / 2 - 1` tap margin that `inter_predictor` reads | ||
| 520 | + // backwards also lies inside the visible plane. | ||
| 521 | + let origin = y0_mv as isize * refp.stride as isize + x0_mv as isize; | ||
| 522 | + inter_predictor( | ||
| 523 | + refp.data, | ||
| 524 | + refp.stride, | ||
| 525 | + origin, | ||
| 526 | + dst, | ||
| 527 | + dst_stride, | ||
| 528 | + f, | ||
| 529 | + subpel_x, | ||
| 530 | + subpel_y, | ||
| 531 | + w, | ||
| 532 | + h, | ||
| 533 | + avg, | ||
| 534 | + temp, | ||
| 535 | + ); | ||
| 536 | +} | ||
| 537 | + | ||
| 538 | +/// `average_split_mvs` (`vp9/common/vp9_reconinter.c:113`), luma (`ss_idx 0`): | ||
| 539 | +/// the sub-block's own MV. | ||
| 540 | +pub(crate) fn split_mv(bmi: &[[(i32, i32); 2]; 4], block: usize, ref_idx: usize) -> (i32, i32) { | ||
| 541 | + bmi[block][ref_idx] | ||
| 542 | +} | ||
| 543 | + | ||
| 544 | +/// `average_split_mvs` for 4:2:0 chroma (`ss_idx 3`): `mi_mv_pred_q4`, the | ||
| 545 | +/// q4-rounded average over the four sub-blocks. | ||
| 546 | +/// | ||
| 547 | +/// `round_mv_comp_q4(v) = (v < 0 ? v - 2 : v + 2) / 4` — C's truncating | ||
| 548 | +/// division, which Rust's `/` on integers reproduces. | ||
| 549 | +pub(crate) fn average_split_mvs_chroma(bmi: &[[(i32, i32); 2]; 4], ref_idx: usize) -> (i32, i32) { | ||
| 550 | + let q4 = |v: i32| if v < 0 { (v - 2) / 4 } else { (v + 2) / 4 }; | ||
| 551 | + let mut row = 0; | ||
| 552 | + let mut col = 0; | ||
| 553 | + for b in bmi.iter() { | ||
| 554 | + row += b[ref_idx].0; | ||
| 555 | + col += b[ref_idx].1; | ||
| 556 | + } | ||
| 557 | + (q4(row), q4(col)) | ||
| 558 | +} | ||
crates/ec-vp9/tests/inter_pixels_exact.rsadded+131 -0
| @@ -0,0 +1,131 @@ | |||
| 1 | +//! Inter-frame pixel witness: a GOP (key frame + inter frames, with and | ||
| 2 | +//! without tile columns) must decode byte-identically to ffmpeg's libvpx | ||
| 3 | +//! decoder, whose rawvideo output is the oracle. Fixtures are generated | ||
| 4 | +//! deterministically with ffmpeg, so the witness is self-contained. | ||
| 5 | +//! | ||
| 6 | +//! The 1080p private streams the lane developed against are compared through | ||
| 7 | +//! `tests/scratch_pixdump.rs` (env-gated) instead; see `lanes/vp9pix.report.md`. | ||
| 8 | + | ||
| 9 | +mod ivf; | ||
| 10 | + | ||
| 11 | +use ec_vp9::decode::Decoder; | ||
| 12 | + | ||
| 13 | +/// Generate a GOP fixture with libvpx: 6 frames of testsrc2 at 320x240, | ||
| 14 | +/// `-g 10` so frames 1..5 are inter frames. | ||
| 15 | +fn ensure_fixture(path: &std::path::Path, tile_columns: u32) { | ||
| 16 | + if path.exists() { | ||
| 17 | + return; | ||
| 18 | + } | ||
| 19 | + if let Some(dir) = path.parent() { | ||
| 20 | + std::fs::create_dir_all(dir).unwrap(); | ||
| 21 | + } | ||
| 22 | + let st = std::process::Command::new("ffmpeg") | ||
| 23 | + .args([ | ||
| 24 | + "-v", | ||
| 25 | + "error", | ||
| 26 | + "-y", | ||
| 27 | + "-f", | ||
| 28 | + "lavfi", | ||
| 29 | + "-i", | ||
| 30 | + "testsrc2=size=320x240:rate=24:duration=0.25", | ||
| 31 | + "-c:v", | ||
| 32 | + "libvpx-vp9", | ||
| 33 | + "-g", | ||
| 34 | + "10", | ||
| 35 | + "-crf", | ||
| 36 | + "32", | ||
| 37 | + "-deadline", | ||
| 38 | + "good", | ||
| 39 | + "-cpu-used", | ||
| 40 | + "4", | ||
| 41 | + "-tile-columns", | ||
| 42 | + ]) | ||
| 43 | + .arg(tile_columns.to_string()) | ||
| 44 | + .args(["-pix_fmt", "yuv420p", "-f", "ivf"]) | ||
| 45 | + .arg(path) | ||
| 46 | + .output() | ||
| 47 | + .expect("ffmpeg with libvpx-vp9 must be on PATH for the witnesses"); | ||
| 48 | + assert!(st.status.success(), "fixture generation failed: {st:?}"); | ||
| 49 | +} | ||
| 50 | + | ||
| 51 | +/// ffmpeg's decoded rawvideo byte stream (frame-packed I420, `w`x`h`). | ||
| 52 | +fn ffmpeg_raw_yuv(path: &std::path::Path, w: usize, h: usize) -> Vec<u8> { | ||
| 53 | + let out = std::process::Command::new("ffmpeg") | ||
| 54 | + .args(["-v", "error", "-i"]) | ||
| 55 | + .arg(path) | ||
| 56 | + .args(["-f", "rawvideo", "-pix_fmt", "yuv420p", "-"]) | ||
| 57 | + .output() | ||
| 58 | + .expect("ffmpeg on PATH"); | ||
| 59 | + assert!(out.status.success(), "ffmpeg decode failed: {out:?}"); | ||
| 60 | + let frame = w * h + 2 * (w / 2) * (h / 2); | ||
| 61 | + assert_eq!( | ||
| 62 | + out.stdout.len() % frame, | ||
| 63 | + 0, | ||
| 64 | + "ffmpeg output is not frame-aligned" | ||
| 65 | + ); | ||
| 66 | + out.stdout | ||
| 67 | +} | ||
| 68 | + | ||
| 69 | +/// Decode every frame with our decoder and compare each SHOWN frame's planes | ||
| 70 | +/// against ffmpeg's stream; returns the number of shown frames compared. | ||
| 71 | +fn compare(path: &std::path::Path) -> usize { | ||
| 72 | + let bytes = std::fs::read(path).unwrap(); | ||
| 73 | + let (_, w, h, frames) = ivf::parse_ivf(&bytes); | ||
| 74 | + let (w, h) = (w as usize, h as usize); | ||
| 75 | + let expected = ffmpeg_raw_yuv(path, w, h); | ||
| 76 | + let frame_bytes = w * h + 2 * (w / 2) * (h / 2); | ||
| 77 | + | ||
| 78 | + let mut decoder = Decoder::new(); | ||
| 79 | + let mut shown = 0usize; | ||
| 80 | + let mut seen_inter = false; | ||
| 81 | + for (i, f) in frames.iter().enumerate() { | ||
| 82 | + if let Some(pic) = decoder.decode(&f.data).expect("frame decodes") { | ||
| 83 | + let off = shown * frame_bytes; | ||
| 84 | + assert!( | ||
| 85 | + off + frame_bytes <= expected.len(), | ||
| 86 | + "frame {i}: ffmpeg has fewer frames than we show" | ||
| 87 | + ); | ||
| 88 | + let want = &expected[off..off + frame_bytes]; | ||
| 89 | + let mut got = Vec::with_capacity(frame_bytes); | ||
| 90 | + got.extend_from_slice(&pic.y); | ||
| 91 | + got.extend_from_slice(&pic.u); | ||
| 92 | + got.extend_from_slice(&pic.v); | ||
| 93 | + assert_eq!( | ||
| 94 | + got.len(), | ||
| 95 | + frame_bytes, | ||
| 96 | + "frame {i}: our planes are {w}x{h} I420" | ||
| 97 | + ); | ||
| 98 | + let diff = got.iter().zip(want).position(|(a, b)| a != b); | ||
| 99 | + assert!( | ||
| 100 | + diff.is_none(), | ||
| 101 | + "frame {i}: pixel mismatch at byte {:?} (ours {} ffmpeg {})", | ||
| 102 | + diff, | ||
| 103 | + diff.map(|d| got[d]).unwrap_or(0), | ||
| 104 | + diff.map(|d| want[d]).unwrap_or(0) | ||
| 105 | + ); | ||
| 106 | + shown += 1; | ||
| 107 | + } | ||
| 108 | + if i > 0 { | ||
| 109 | + seen_inter = true; | ||
| 110 | + } | ||
| 111 | + } | ||
| 112 | + assert_eq!(shown * frame_bytes, expected.len(), "frame count mismatch"); | ||
| 113 | + assert!(seen_inter, "the fixture must contain inter frames"); | ||
| 114 | + shown | ||
| 115 | +} | ||
| 116 | + | ||
| 117 | +#[test] | ||
| 118 | +fn inter_gop_matches_ffmpeg() { | ||
| 119 | + let dir = std::path::Path::new(env!("CARGO_MANIFEST_DIR")).join("../../target/vp9-witness"); | ||
| 120 | + let path = dir.join("inter-gop.ivf"); | ||
| 121 | + ensure_fixture(&path, 0); | ||
| 122 | + assert!(compare(&path) >= 2); | ||
| 123 | +} | ||
| 124 | + | ||
| 125 | +#[test] | ||
| 126 | +fn inter_gop_with_tile_columns_matches_ffmpeg() { | ||
| 127 | + let dir = std::path::Path::new(env!("CARGO_MANIFEST_DIR")).join("../../target/vp9-witness"); | ||
| 128 | + let path = dir.join("inter-gop-tc1.ivf"); | ||
| 129 | + ensure_fixture(&path, 1); | ||
| 130 | + assert!(compare(&path) >= 2); | ||
| 131 | +} | ||
crates/ec-vp9/tests/keyframe_exact.rsmodified+31 -17
| @@ -117,30 +117,44 @@ fn lossless_64_matches_ffmpeg() { | |||
| 117 | } | 117 | } |
| 118 | 118 | ||
| 119 | #[test] | 119 | #[test] |
| 120 | -fn inter_is_named_unsupported() { | 120 | +fn inter_stream_decodes_and_matches_ffmpeg() { |
| 121 | + // The altref clip is a real 60-frame inter stream with hidden frames; it | ||
| 122 | + // used to be the "inter is refused by name" witness. Inter frames decode | ||
| 123 | + // now, so the witness is the pixels: every shown frame must match | ||
| 124 | + // ffmpeg's libvpx output byte for byte. | ||
| 121 | let Some(dir) = ivf::fixture_dir() else { return }; | 125 | let Some(dir) = ivf::fixture_dir() else { return }; |
| 122 | - // The altref clip contains hidden (inter) frames. | ||
| 123 | let path = dir.join("vp9-superframe-altref.ivf"); | 126 | let path = dir.join("vp9-superframe-altref.ivf"); |
| 124 | let bytes = std::fs::read(&path).unwrap(); | 127 | let bytes = std::fs::read(&path).unwrap(); |
| 125 | - let (_, _, _, frames) = ivf::parse_ivf(&bytes); | 128 | + let (_, w, h, frames) = ivf::parse_ivf(&bytes); |
| 129 | + let (w, h) = (w as usize, h as usize); | ||
| 130 | + let expected = ffmpeg_raw_yuv(&path); | ||
| 131 | + let frame_bytes = w * h + 2 * (w / 2) * (h / 2); | ||
| 126 | let mut decoder = Decoder::new(); | 132 | let mut decoder = Decoder::new(); |
| 127 | - let mut saw_inter = false; | 133 | + let mut shown = 0usize; |
| 128 | - let mut saw_named = false; | 134 | + let mut hidden = 0usize; |
| 129 | - for frame in &frames { | 135 | + for (i, frame) in frames.iter().enumerate() { |
| 130 | - match decoder.decode(&frame.data) { | 136 | + match decoder.decode(&frame.data).expect("every frame decodes") { |
| 131 | - Ok(_) => {} | 137 | + Some(pic) => { |
| 132 | - Err(e) => { | 138 | + let off = shown * frame_bytes; |
| 133 | - saw_inter = true; | 139 | + let mut got = Vec::with_capacity(frame_bytes); |
| 134 | - let msg = format!("{e}"); | 140 | + got.extend_from_slice(&pic.y); |
| 135 | - if msg.contains("vp9 inter") { | 141 | + got.extend_from_slice(&pic.u); |
| 136 | - saw_named = true; | 142 | + got.extend_from_slice(&pic.v); |
| 137 | - } | 143 | + assert_eq!( |
| 138 | - break; | 144 | + got, |
| 145 | + expected[off..off + frame_bytes], | ||
| 146 | + "frame {i}: pixel mismatch" | ||
| 147 | + ); | ||
| 148 | + shown += 1; | ||
| 139 | } | 149 | } |
| 150 | + None => hidden += 1, | ||
| 140 | } | 151 | } |
| 141 | } | 152 | } |
| 142 | - assert!(saw_inter, "the altref fixture must contain a refused frame"); | 153 | + assert!(shown >= 2, "the stream shows frames"); |
| 143 | - assert!(saw_named, "the refusal must name 'vp9 inter'"); | 154 | + // The chunk-level hidden altref frames are consumed by the decoder's |
| 155 | + // superframe split, so every IVF chunk here yields one shown frame. | ||
| 156 | + assert_eq!(hidden, 0, "superframe chunks show exactly one frame each"); | ||
| 157 | + assert_eq!(shown * frame_bytes, expected.len()); | ||
| 144 | } | 158 | } |
| 145 | 159 | ||
| 146 | #[test] | 160 | #[test] |
crates/ec-vp9/tests/scratch_pixdump.rsadded+77 -0
| @@ -0,0 +1,77 @@ | |||
| 1 | +//! Scratch: dump every shown frame's planes for the pixel comparator. | ||
| 2 | +//! | ||
| 3 | +//! `INTER_IVF=<path> EC_VP9_PIXDUMP=<out> cargo test -p ec-vp9 --test scratch_pixdump` | ||
| 4 | +//! | ||
| 5 | +//! Layout matches the oracle: per shown frame, Y (w*h), U ((w/2)*(h/2)), | ||
| 6 | +//! V ((w/2)*(h/2)), rows packed to the visible width. | ||
| 7 | + | ||
| 8 | +use ec_vp9::decode::Decoder; | ||
| 9 | + | ||
| 10 | +#[test] | ||
| 11 | +fn pixdump() { | ||
| 12 | + let (Ok(ivf), Ok(out)) = (std::env::var("INTER_IVF"), std::env::var("EC_VP9_PIXDUMP")) else { | ||
| 13 | + eprintln!("SKIP scratch_pixdump: set INTER_IVF and EC_VP9_PIXDUMP"); | ||
| 14 | + return; | ||
| 15 | + }; | ||
| 16 | + let bytes = std::fs::read(&ivf).expect("read ivf"); | ||
| 17 | + let (_, w, h, frames) = parse_ivf(&bytes); | ||
| 18 | + let mut decoder = Decoder::new(); | ||
| 19 | + let mut dump = Vec::new(); | ||
| 20 | + let mut shown = 0usize; | ||
| 21 | + for (i, f) in frames.iter().enumerate() { | ||
| 22 | + match decoder.decode(&f.data) { | ||
| 23 | + Ok(Some(pic)) => { | ||
| 24 | + dump.extend_from_slice(&pic.y); | ||
| 25 | + dump.extend_from_slice(&pic.u); | ||
| 26 | + dump.extend_from_slice(&pic.v); | ||
| 27 | + eprintln!( | ||
| 28 | + "PIXFRAME input={i} shown={shown} {w}x{h} {}x{}", | ||
| 29 | + pic.width, pic.height | ||
| 30 | + ); | ||
| 31 | + shown += 1; | ||
| 32 | + } | ||
| 33 | + Ok(None) => eprintln!("PIXFRAME input={i} hidden"), | ||
| 34 | + Err(e) => { | ||
| 35 | + eprintln!("PIXERR input={i}: {e}"); | ||
| 36 | + break; | ||
| 37 | + } | ||
| 38 | + } | ||
| 39 | + } | ||
| 40 | + std::fs::write(&out, &dump).expect("write dump"); | ||
| 41 | + eprintln!("wrote {} bytes ({shown} shown frames)", dump.len()); | ||
| 42 | +} | ||
| 43 | + | ||
| 44 | +fn parse_ivf(bytes: &[u8]) -> (&str, u16, u16, Vec<IvfFrame>) { | ||
| 45 | + assert_eq!(&bytes[0..4], b"DKIF", "not an IVF file"); | ||
| 46 | + let hdr_len = u16::from_le_bytes([bytes[6], bytes[7]]) as usize; | ||
| 47 | + let fourcc = std::str::from_utf8(&bytes[8..12]).unwrap(); | ||
| 48 | + let width = u16::from_le_bytes([bytes[12], bytes[13]]); | ||
| 49 | + let height = u16::from_le_bytes([bytes[14], bytes[15]]); | ||
| 50 | + let mut pos = hdr_len; | ||
| 51 | + let mut frames = Vec::new(); | ||
| 52 | + while pos + 12 <= bytes.len() { | ||
| 53 | + let sz = u32::from_le_bytes([ | ||
| 54 | + bytes[pos], | ||
| 55 | + bytes[pos + 1], | ||
| 56 | + bytes[pos + 2], | ||
| 57 | + bytes[pos + 3], | ||
| 58 | + ]) as usize; | ||
| 59 | + let pts = u64::from_le_bytes(bytes[pos + 4..pos + 12].try_into().unwrap()); | ||
| 60 | + pos += 12; | ||
| 61 | + if pos + sz > bytes.len() { | ||
| 62 | + break; | ||
| 63 | + } | ||
| 64 | + frames.push(IvfFrame { | ||
| 65 | + pts, | ||
| 66 | + data: bytes[pos..pos + sz].to_vec(), | ||
| 67 | + }); | ||
| 68 | + pos += sz; | ||
| 69 | + } | ||
| 70 | + (fourcc, width, height, frames) | ||
| 71 | +} | ||
| 72 | + | ||
| 73 | +struct IvfFrame { | ||
| 74 | + #[allow(dead_code)] | ||
| 75 | + pts: u64, | ||
| 76 | + data: Vec<u8>, | ||
| 77 | +} | ||
lanes/vp9pix.report.mdadded+232 -0
| @@ -0,0 +1,232 @@ | |||
| 1 | +# Lane vp9pix: VP9 inter-frame PIXELS (motion compensation → reconstruction) | ||
| 2 | + | ||
| 3 | +Branch `lane-vp9-pix`, base `main 597b7442`. Crate scope: `crates/ec-vp9` only. | ||
| 4 | +Worktree `/home/user/Documents/Code/Rust/edith_codecs-vp9pix`, private | ||
| 5 | +`CARGO_TARGET_DIR=$HOME/.cache/cargo-target-vp9pix`. | ||
| 6 | + | ||
| 7 | +Deliverable: `decode()` decodes inter frames through motion compensation, | ||
| 8 | +residual reconstruction, the loop filter and reference-slot management, and is | ||
| 9 | +byte-identical to a locally built libvpx 1.15 oracle on every fixture swept. | ||
| 10 | + | ||
| 11 | +## Baseline (milestone 0) | ||
| 12 | + | ||
| 13 | + $ CARGO_TARGET_DIR=$HOME/.cache/cargo-target-vp9pix cargo test -p ec-vp9 | ||
| 14 | + exit=0 (20 test binaries, 0 failures, 0 ignored) | ||
| 15 | + | ||
| 16 | +No pre-existing reds, so no base triage was needed. (Pre-existing warnings: | ||
| 17 | +`crates/ec-vp9/src/modes.rs` unused `partition_probs`/`x_mis`/`y_mis` + | ||
| 18 | +`read_partition`, `tables/mod.rs` `TM_PRED` — untouched files, present at base.) | ||
| 19 | + | ||
| 20 | +## The gate: byte-exactness against libvpx 1.15 | ||
| 21 | + | ||
| 22 | +Oracle: the instrumented build the syntax lane left at `/tmp/vp9loss` | ||
| 23 | +(`drv_pix.c` is new here: it appends every SHOWN frame's cropped I420 planes, | ||
| 24 | +frame-indexed, to `PIXDUMP`). Rich fixtures copied to `$HOME/.cache/vp9pix/` | ||
| 25 | +for tmpfs insurance. Comparator: `$HOME/.cache/vp9pix/pixcmp.py` | ||
| 26 | +(oracle, ours, w, h) → per-frame identical/first-diff report. | ||
| 27 | + | ||
| 28 | + $ python3 $HOME/.cache/vp9pix/pixcmp.py oracle_inter1.pix ours_inter1.pix 1920 1080 | ||
| 29 | + oracle=9331200 bytes ours=9331200 bytes frame_bytes=3110400 | ||
| 30 | + frame 0: IDENTICAL (3110400 bytes) | ||
| 31 | + frame 1: IDENTICAL (3110400 bytes) | ||
| 32 | + frame 2: IDENTICAL (3110400 bytes) | ||
| 33 | + | ||
| 34 | +That is the acceptance gate: 3/3 shown frames of the 1080p single-tile inter | ||
| 35 | +stream, loop filter ON, planes byte-for-byte. | ||
| 36 | + | ||
| 37 | +Corroborating instruments (all agree): | ||
| 38 | +- `SKIPLF` oracle + `EC_VP9_SKIP_LF=1` ours: identical too (isolates MC+residual | ||
| 39 | + from the filter). | ||
| 40 | +- Per-superblock LUMA loop-filter masks: oracle `LFMASKSY=1` vs our | ||
| 41 | + `EC_VP9_LFMASKSY=1` → **0 of 510 SBs differ, on all three frames.** | ||
| 42 | +- Emitted loop-filter edges: oracle `LFTRACE=<frame>` vs ours | ||
| 43 | + `LFTRACE=1 EC_VP9_LF_FRAME=<n>` (our per-frame gate) → every oracle edge is | ||
| 44 | + emitted by us (oracle-only = 0); our extra 2660/3116 lines are the *second | ||
| 45 | + column of a dual H16 pair* — libvpx prints one line for | ||
| 46 | + `vpx_lpf_horizontal_16_dual`, we print one per 8x8 cell. Verified 2660/2660 | ||
| 47 | + and 3116/3116 of the extras have an oracle twin at `(x-8, y)`. | ||
| 48 | + | ||
| 49 | +The entry-gate fixture is `/tmp/inter1.ivf` (1080p, 1 tile column). It is | ||
| 50 | +reachable through `tests/scratch_pixdump.rs` | ||
| 51 | +(`INTER_IVF=… EC_VP9_PIXDUMP=… cargo test -p ec-vp9 --test scratch_pixdump -- --nocapture`). | ||
| 52 | + | ||
| 53 | +## Corpus sweep (real content) | ||
| 54 | + | ||
| 55 | +| fixture | frames | result | | ||
| 56 | +|---|---|---| | ||
| 57 | +| `vp9-1080p-23.976-8bit.ivf` (48) | 48 | **all byte-identical** | | ||
| 58 | +| `vp9-2160p-23.976-8bit.ivf` (48) | 48 | **all byte-identical** | | ||
| 59 | +| `vp9-superframe-altref.ivf` (320x240, 60) | 60 | **all byte-identical** | | ||
| 60 | +| `/tmp/inter1.ivf` (1080p, 1 tile col) | 3 | **all byte-identical** | | ||
| 61 | +| `/tmp/inter.ivf` (1080p, 2 tile cols) | 3 | **all byte-identical** | | ||
| 62 | +| `vp9-1080p-60-8bit.ivf` (120) | 72 | identical 0..71, **desync at 72** (deferred, below) | | ||
| 63 | + | ||
| 64 | +Sweep driver: `$HOME/.cache/vp9pix/sweep.sh <fixture> <w> <h>` (oracle dump + | ||
| 65 | +our dump + `pixcmp.py`). | ||
| 66 | + | ||
| 67 | +## Milestones | ||
| 68 | + | ||
| 69 | +1. **Pixel plumbing** — oracle `drv_pix.c` + `tests/scratch_pixdump.rs` + | ||
| 70 | + `pixcmp.py`. Done; the comparator is the instrument every claim below uses. | ||
| 71 | +2. **Reference slots** — `refs: [Option<RefFrame>; 8]`, `RefFrame` holds the | ||
| 72 | + frame at its **64-aligned extent** (`Arc<Planes>`) plus its coded size. | ||
| 73 | + `refresh_frame_flags` is applied after the frame's loop filter | ||
| 74 | + (`swap_frame_buffers`), so a later frame predicts from filtered pixels. | ||
| 75 | + Evidence: the 1080p GOP and the 60-frame altref stream decode identically, | ||
| 76 | + which is only possible if each frame's prediction reads exactly the frame | ||
| 77 | + the header names (any slot mistake shows as a pixel diff on frame 1+). | ||
| 78 | + The refusal for a size-changing reference is named (below). | ||
| 79 | +3. **Motion compensation** — new `crates/ec-vp9/src/mc.rs`: libvpx's | ||
| 80 | + `clamp_mv_to_umv_border_sb`, `build_mc_border` (per-block reference border | ||
| 81 | + extension — the VP9 decoder does NOT extend reference borders in place), | ||
| 82 | + `dec_build_inter_predictors` including the "window inside the frame" direct | ||
| 83 | + read, the four convolve kernels (`EIGHTTAP`, `EIGHTTAP_SMOOTH`, | ||
| 84 | + `EIGHTTAP_SHARP`, `BILINEAR`, index order = `vp9_filter_kernels`), compound | ||
| 85 | + averaging, and `average_split_mvs` (luma per sub-block, 4:2:0 chroma q4 | ||
| 86 | + average). | ||
| 87 | +4. **Residual** — tokens via the shared `decode_inter_tx_tokens` (one read per | ||
| 88 | + transform block), then `inverse_transform_add` with the DEFAULT scan and | ||
| 89 | + `DCT_DCT` (`inverse_transform_block_inter`). | ||
| 90 | +5. **Loop filter** — per-cell level from `get_filter_level` | ||
| 91 | + (`lvl[seg][ref_frame[0]][mode_lf_lut[mode]]`), the skip promotion | ||
| 92 | + (`!less8x8 && eobtotal == 0 → skip`) and the skipped-inter mask rule | ||
| 93 | + (`skip && is_inter` keeps only the prediction masks). Verified by | ||
| 94 | + mask-identity + edge-set + pixels (above). | ||
| 95 | +6. **Gates** — `cargo test -p ec-vp9` green (below); refusals re-checked; | ||
| 96 | + prose updated. | ||
| 97 | + | ||
| 98 | +## Bug classes found while porting (all fixed, each with its evidence) | ||
| 99 | + | ||
| 100 | +1. **Direct-read coordinates taken after the tap padding.** | ||
| 101 | + `dec_build_inter_predictors` computes `buf_ptr = ref_frame + y0*stride + x0` | ||
| 102 | + BEFORE `y0 -= INTERP_EXTEND - 1` / `x0 -= …`; our first version read the | ||
| 103 | + direct path from the padded coordinates, i.e. 3 rows/cols early. Signature: | ||
| 104 | + border-path blocks exact (top/bottom SB rows), direct-path blocks wrong by | ||
| 105 | + up to ±5, and `SKIPLF`-style isolation shows the error is in prediction. | ||
| 106 | + Independent python re-derivation of the C predictor from the oracle's | ||
| 107 | + reference frame identified it in one step (oracle 45 vs ours 47). | ||
| 108 | +2. **Skipped intra blocks inside an inter frame skipped | ||
| 109 | + `dec_reset_skip_context`.** libvpx calls it at the top of `decode_block` | ||
| 110 | + (before the intra/inter split); ours only in the inter arm, leaving stale | ||
| 111 | + above-context bytes → a `tile bool decoder desync` in frame 2, with the | ||
| 112 | + first divergence a *prob* difference at the same ordinal read (the skill's | ||
| 113 | + "same ordinal, different question" signature). | ||
| 114 | +3. **Skipped inter blocks kept full loop-filter masks.** `grids.skip_inter` | ||
| 115 | + was only set by the eob promotion, so a coded-skip inter block got the | ||
| 116 | + size/tx masks instead of prediction-only masks → extra 16-tap edges | ||
| 117 | + (mask-identity check caught it as `L16 ⊉ L32`). | ||
| 118 | +4. **Intra-in-inter tx type.** libvpx's luma mode for a sub-8x8 block is the | ||
| 119 | + SUB-BLOCK's (`mi->bmi[…]`), used for `intra_mode_to_tx_type_lookup`; the | ||
| 120 | + merged syntax walk used `info.mode`. Now shared by the syntax and pixel | ||
| 121 | + walks (they must agree or the two paths read different tokens). | ||
| 122 | +5. **`mv_ref_list[2]` write (crash).** `ADD_MV_REF_LIST_EB`'s successful | ||
| 123 | + second add is `goto Done`; in the "different reference frame" loop the C | ||
| 124 | + leaves the loop, our port kept iterating and wrote past the two-entry list | ||
| 125 | + (`inter.rs`, now an explicit `break`). Found by the corpus sweep — | ||
| 126 | + `vp9-superframe-altref.ivf` panicked at frame 14 before the fix; 60/60 | ||
| 127 | + frames byte-identical after. | ||
| 128 | +6. **Frame-level interpolation filter numbering** (`read_interp_filter`): | ||
| 129 | + the header's literal index runs through | ||
| 130 | + `literal_to_filter = {EIGHTTAP_SMOOTH, EIGHTTAP, EIGHTTAP_SHARP, BILINEAR}` | ||
| 131 | + — NOT libvpx's `#define` numbering (`EIGHTTAP 0`). Converted where | ||
| 132 | + `frame_interp` is built; inert on switchable streams (the fixture's), correct | ||
| 133 | + for non-switchable ones. | ||
| 134 | + | ||
| 135 | +## Named refusals (unchanged or added) | ||
| 136 | + | ||
| 137 | +- `vp9 profile N` / `vp9 subsampling` — unchanged (existing tests). | ||
| 138 | +- `vp9 intra-only` — unchanged refusal, now also reachable from `decode()`. | ||
| 139 | +- **`vp9 inter reference size change`** (new): a named reference whose coded | ||
| 140 | + size differs from the current frame is refused by name (libvpx scales it; | ||
| 141 | + scaling is not ported in this lane). | ||
| 142 | +- **`vp9 inter odd frame dimensions`** (new): odd width/height refused by | ||
| 143 | + name — the keyframe crop stores chroma at `floor(w/2)` while the predictor | ||
| 144 | + reads libvpx's `uv_crop_width = (w+1)/2`; the two disagree only for odd | ||
| 145 | + extents, so those are refused rather than silently wrong. | ||
| 146 | + | ||
| 147 | +## Deferred | ||
| 148 | + | ||
| 149 | +- **`deferred(fix-now-next-lane)` — frame 72 desync on | ||
| 150 | + `vp9-1080p-60-8bit.ivf`.** Frames 0..71 are byte-identical; frame 72 is an | ||
| 151 | + ordinary inter frame (`MODEDUMP` CHSUM: `tx_mode=4 ref_mode=0 allow_hp=1 | ||
| 152 | + interp=4 intra_only=0 er=0`, matching its neighbours) and dies with | ||
| 153 | + `tile bool decoder desync`. An unexercised inter syntax path (compound | ||
| 154 | + reference / sub-8x8 / a specific `use_prev_frame_mvs` state) is the likely | ||
| 155 | + home; the 1080p 24fps GOP, the 4K GOP and the altref stream do not reach it. | ||
| 156 | + Repro: `$HOME/.cache/vp9pix/sweep.sh vp9-1080p-60-8bit 1920 1080`. | ||
| 157 | +- `deferred(unblock: odd-extent chroma layout)` — the odd-dimension refusal | ||
| 158 | + above; the root fix is to store reference chroma at `(w+1)/2`, which also | ||
| 159 | + changes the keyframe path's returned plane layout. | ||
| 160 | +- **Superseded claim:** `lanes/vp9inter.report.md` ranked cause 3 ("keyframe | ||
| 161 | + multi-tile-column desync on `/tmp/inter.ivf` frame 0") does **not** | ||
| 162 | + reproduce at this lane's base — `INTER_IVF=/tmp/inter.ivf INTER_EXPECT=none | ||
| 163 | + cargo test -p ec-vp9 --test scratch_interdump` passes on the untouched main | ||
| 164 | + checkout (f715d70c, the tile-row fix, landed before the inter-syntax merge | ||
| 165 | + 32d81e8f). `/tmp/inter.ivf` now decodes all 3 frames byte-exactly. | ||
| 166 | + | ||
| 167 | +## Gates | ||
| 168 | + | ||
| 169 | + $ CARGO_TARGET_DIR=$HOME/.cache/cargo-target-vp9pix cargo test -p ec-vp9 | ||
| 170 | + exit=0 — every binary green, including the new witness | ||
| 171 | + tests/inter_pixels_exact.rs (2 tests: GOP + GOP with tile columns, | ||
| 172 | + ffmpeg libvpx rawvideo oracle, self-generated | ||
| 173 | + fixtures; non-vacuity proof: with | ||
| 174 | + EC_VP9_SKIP_LF=1 both tests FAIL at frame 0 | ||
| 175 | + byte 31 — ours 81 vs ffmpeg 82) | ||
| 176 | + tests/keyframe_exact.rs (4 tests, incl. the rewritten | ||
| 177 | + inter_stream_decodes_and_matches_ffmpeg: | ||
| 178 | + 60/60 frames of the altref stream vs ffmpeg) | ||
| 179 | + | ||
| 180 | +## Repro (all commands) | ||
| 181 | + | ||
| 182 | + # gate | ||
| 183 | + cd /home/user/Documents/Code/Rust/edith_codecs-vp9pix | ||
| 184 | + CARGO_TARGET_DIR=$HOME/.cache/cargo-target-vp9pix cargo test -p ec-vp9 | ||
| 185 | + | ||
| 186 | + # 1080p single-tile inter stream vs the instrumented oracle | ||
| 187 | + cd $HOME/.cache/vp9pix && PIXDUMP=$HOME/.cache/vp9pix/oracle_inter1.pix ./drv_pix inter1.ivf | ||
| 188 | + cd /home/user/Documents/Code/Rust/edith_codecs-vp9pix | ||
| 189 | + CARGO_TARGET_DIR=$HOME/.cache/cargo-target-vp9pix INTER_IVF=$HOME/.cache/vp9pix/inter1.ivf \ | ||
| 190 | + EC_VP9_PIXDUMP=$HOME/.cache/vp9pix/ours_inter1.pix \ | ||
| 191 | + cargo test -p ec-vp9 --test scratch_pixdump -- --nocapture | ||
| 192 | + cd $HOME/.cache/vp9pix && python3 pixcmp.py oracle_inter1.pix ours_inter1.pix 1920 1080 | ||
| 193 | + | ||
| 194 | + # mask identity / edge sets | ||
| 195 | + LFMASKSY=1 ./drv_pix inter1.ivf 2>&1 | grep '^LFY' > lfy_oracle.txt | ||
| 196 | + LFTRACE=1 ./drv_pix inter1.ivf 2>&1 | grep '^LFTR' > lf_oracle_1.txt # LFTRACE=<frame> | ||
| 197 | + | ||
| 198 | +## VERDICT | ||
| 199 | + | ||
| 200 | +GATE 1 — `cargo test -p ec-vp9` green in the worktree (private target dir), | ||
| 201 | +including the new pixel witnesses: **PASS**. | ||
| 202 | +GATE 2 — `inter1.ivf` `decode()` returns 3 shown frames, each byte-identical to | ||
| 203 | +the libvpx 1.15 oracle's planes (comparator output above): **PASS**. | ||
| 204 | +GATE 3 — per-milestone evidence lines above; every pixel mismatch met during | ||
| 205 | +development is named with its root cause (6 classes): **PASS**. | ||
| 206 | +GATE 4 — this report: baseline, per-milestone evidence, `inter.ivf` | ||
| 207 | +disposition, deferred items with dispositions, repro commands: **PASS**. | ||
| 208 | +GATE 5 — committed to `lane-vp9-pix`, `git status` clean, no push/merge: see | ||
| 209 | +the git log below. | ||
| 210 | +GATE 6 — VERDICT section: this one. | ||
| 211 | + | ||
| 212 | +FLAGS (the verifier should re-derive these independently) | ||
| 213 | +1. **The direct-read coordinate rule** (class 1) is the single most load-bearing | ||
| 214 | + line in `mc.rs`: `origin` must come from the MV-adjusted, NOT tap-padded, | ||
| 215 | + `x0`/`y0`. Re-derive from `vp9_decodeframe.c:672-728` and check our | ||
| 216 | + `x0_mv`/`y0_mv` usage. | ||
| 217 | +2. **`skip_inter` semantics**: `skip && is_inter` at BOTH the coded-skip and | ||
| 218 | + the `eobtotal == 0` promotion, and the mask early-return placement (after | ||
| 219 | + the above/left prediction masks). Mask-identity can be re-run per SB with | ||
| 220 | + `EC_VP9_LFMASKSY=1` vs `LFMASKSY=1`. | ||
| 221 | +3. **The `mv_ref_list[2]` fix** changes an existing (merged) syntax path: the | ||
| 222 | + altref fixture panicked before it and is byte-exact after — re-run | ||
| 223 | + `vp9-superframe-altref.ivf` end to end. | ||
| 224 | +4. **Reference storage extent**: refs must keep the 64-aligned plane buffer | ||
| 225 | + (the decoded overhang rows are readable by the direct path). Cropping them | ||
| 226 | + reproduces the class-1 symptom set; confirm by checking that frames 1+ of | ||
| 227 | + the 1080p GOP match (the overhang is read at the frame bottom). | ||
| 228 | +5. **The 60fps frame-72 desync** is a real open defect: verify it is | ||
| 229 | + independent of this lane's changes (`scratch_interdump` on the base | ||
| 230 | + checkout reports the same frame-72 desync in the syntax walk). | ||
| 231 | +6. Corpus sweeps were run on this workstation with `/tmp` fixtures; the | ||
| 232 | + 1080p/2160p/altref dumps are regenerable from `sweep.sh`. | ||