ec-vp8 M3 WIP: full inter-frame pipeline decodes; walled on one mode-record divergence
Browse files at this commit10ef9bef82a6a4f28620ffa528af79b06b20998bParent: dd7b5a67 changed files +1545 -44
- crates/ec-vp8/src/bool.rs +8 -1
- crates/ec-vp8/src/decode.rs +602 -36
- crates/ec-vp8/src/header.rs +0 -6
- crates/ec-vp8/src/lib.rs +1 -0
- crates/ec-vp8/src/mc.rs +712 -0
- crates/ec-vp8/src/modes.rs +221 -0
- lanes/vp8.report.md +1 -1
crates/ec-vp8/src/bool.rsmodified+8 -1
| @@ -86,8 +86,12 @@ impl<'a> BoolDecoder<'a> { | |||
| 86 | } | 86 | } |
| 87 | 87 | ||
| 88 | /// Decode one bool whose zero-probability is `prob`/256 | 88 | /// Decode one bool whose zero-probability is `prob`/256 |
| 89 | - /// (RFC `read_bool`). | 89 | + /// (RFC `read_bool`). Under EC_VP8_TRACE, prints the decoder state |
| 90 | + /// after the read — `B <pos> <bit_count> <prob> <bit>`, byte-exact | ||
| 91 | + /// the same line scripts/vp8ref_model.py prints — so the two | ||
| 92 | + /// decoders can be diffed read-for-read. | ||
| 90 | pub fn read_bool(&mut self, prob: u8) -> bool { | 93 | pub fn read_bool(&mut self, prob: u8) -> bool { |
| 94 | + let trace = std::env::var_os("EC_VP8_TRACE").is_some(); | ||
| 91 | let prob = u32::from(prob); | 95 | let prob = u32::from(prob); |
| 92 | let split = 1 + (((self.range - 1) * prob) >> 8); | 96 | let split = 1 + (((self.range - 1) * prob) >> 8); |
| 93 | let bigsplit = split << 8; | 97 | let bigsplit = split << 8; |
| @@ -108,6 +112,9 @@ impl<'a> BoolDecoder<'a> { | |||
| 108 | self.value |= self.next_byte(); | 112 | self.value |= self.next_byte(); |
| 109 | } | 113 | } |
| 110 | } | 114 | } |
| 115 | + if trace { | ||
| 116 | + eprintln!("B {} {} {} {}", self.pos, self.bit_count, prob, u8::from(bit)); | ||
| 117 | + } | ||
| 111 | bit | 118 | bit |
| 112 | } | 119 | } |
| 113 | 120 | ||
crates/ec-vp8/src/decode.rsmodified+602 -36
| @@ -14,6 +14,7 @@ | |||
| 14 | use crate::bool::BoolDecoder; | 14 | use crate::bool::BoolDecoder; |
| 15 | use crate::frame::{FrameType, KeyFrameDims, mb_geometry}; | 15 | use crate::frame::{FrameType, KeyFrameDims, mb_geometry}; |
| 16 | use crate::header::FrameHeader; | 16 | use crate::header::FrameHeader; |
| 17 | +use crate::mc; | ||
| 17 | use crate::intra; | 18 | use crate::intra; |
| 18 | use crate::loopfilter::{self, MbFilterInfo}; | 19 | use crate::loopfilter::{self, MbFilterInfo}; |
| 19 | use crate::modes; | 20 | use crate::modes; |
| @@ -22,6 +23,23 @@ use crate::tokens::{self, Dq, MbCoeffs, TokenContexts}; | |||
| 22 | use crate::transform; | 23 | use crate::transform; |
| 23 | use crate::transform::Dequant; | 24 | use crate::transform::Dequant; |
| 24 | use crate::{Error, PersistedState, Result}; | 25 | use crate::{Error, PersistedState, Result}; |
| 26 | +/// Copy a visible MB-aligned image out of the 1-pixel-bordered working | ||
| 27 | +/// plane into the top-left visible corner of a bordered reference | ||
| 28 | +/// buffer. | ||
| 29 | +fn copy_plane( | ||
| 30 | + dst: &mut [u8], | ||
| 31 | + dst_stride: usize, | ||
| 32 | + src: &[u8], | ||
| 33 | + src_stride: usize, | ||
| 34 | + w: usize, | ||
| 35 | + h: usize, | ||
| 36 | +) { | ||
| 37 | + for r in 0..h { | ||
| 38 | + let s = (1 + r) * src_stride + 1; | ||
| 39 | + let d = (MC_BORDER + r) * dst_stride + MC_BORDER; | ||
| 40 | + dst[d..d + w].copy_from_slice(&src[s..s + w]); | ||
| 41 | + } | ||
| 42 | +} | ||
| 25 | 43 | ||
| 26 | /// Reconstructed-image plane: a padded buffer with a 1-pixel left/top | 44 | /// Reconstructed-image plane: a padded buffer with a 1-pixel left/top |
| 27 | /// border (127 on top including the corner, 129 on the left — RFC 6386 | 45 | /// border (127 on top including the corner, 129 on the left — RFC 6386 |
| @@ -82,11 +100,39 @@ struct MbInfo { | |||
| 82 | b_modes: [u8; 16], | 100 | b_modes: [u8; 16], |
| 83 | segment_id: u8, | 101 | segment_id: u8, |
| 84 | skip: bool, | 102 | skip: bool, |
| 103 | + /// 0 = intra, 1 = last, 2 = golden, 3 = altref (§16.2). Key-frame | ||
| 104 | + /// MBs and the border are intra. | ||
| 105 | + ref_frame: u8, | ||
| 106 | + /// Inter mv_ref leaf (§16.2): 0 = zero, 1 = nearest, 2 = near, | ||
| 107 | + /// 3 = new, 4 = split. 0 when intra. | ||
| 108 | + mv_ref: u8, | ||
| 109 | + /// Whole-MB motion vector in eighth-pels; the bottom-right subblock's | ||
| 110 | + /// vector for SPLITMV (what future neighbours' census sees). | ||
| 111 | + mv: (i16, i16), | ||
| 112 | + /// Per-subblock motion vectors (SPLITMV). | ||
| 113 | + bmi: [(i16, i16); 16], | ||
| 114 | + /// `need_to_clamp_mvs` (§16.2): the luma MC clamps this MV into the | ||
| 115 | + /// extended-frame range. | ||
| 116 | + mv_clamp: bool, | ||
| 85 | /// Any non-zero coefficient decoded for this MB (bit 31 of dixie's | 117 | /// Any non-zero coefficient decoded for this MB (bit 31 of dixie's |
| 86 | /// eob_mask); decides the loop filter's interior edges (§15.1). | 118 | /// eob_mask); decides the loop filter's interior edges (§15.1). |
| 87 | has_nz: bool, | 119 | has_nz: bool, |
| 88 | } | 120 | } |
| 89 | 121 | ||
| 122 | +/// One reference-frame slot: bordered planes, 32-pixel edge-replicated | ||
| 123 | +/// borders around the MB-aligned image (dixie `extend_frame`). | ||
| 124 | +#[derive(Clone)] | ||
| 125 | +struct RefFrame { | ||
| 126 | + y: Vec<u8>, | ||
| 127 | + u: Vec<u8>, | ||
| 128 | + v: Vec<u8>, | ||
| 129 | + stride: usize, | ||
| 130 | + uv_stride: usize, | ||
| 131 | +} | ||
| 132 | + | ||
| 133 | +/// MC border width in pixels (libvpx `VP8BORDERINPIXELS`). | ||
| 134 | +const MC_BORDER: usize = 32; | ||
| 135 | + | ||
| 90 | impl MbInfo { | 136 | impl MbInfo { |
| 91 | fn has_y2(&self) -> bool { | 137 | fn has_y2(&self) -> bool { |
| 92 | self.y_mode != modes::YMode::BPred.as_u8() | 138 | self.y_mode != modes::YMode::BPred.as_u8() |
| @@ -108,6 +154,12 @@ pub struct Decoder { | |||
| 108 | /// DC_PRED border (dixie's `mb_info` border trick). | 154 | /// DC_PRED border (dixie's `mb_info` border trick). |
| 109 | mb_info: Vec<MbInfo>, | 155 | mb_info: Vec<MbInfo>, |
| 110 | token_ctxs: TokenContexts, | 156 | token_ctxs: TokenContexts, |
| 157 | + /// Reference frame slots: [unused, last, golden, altref] (§16.2 | ||
| 158 | + /// indices). Empty until the first key frame. | ||
| 159 | + refs: [Option<RefFrame>; 4], | ||
| 160 | + /// Persisted per-MB segment ids, `mb_rows * mb_cols` (§9.3: when a | ||
| 161 | + /// frame does not re-code the map, ids carry over). | ||
| 162 | + segment_map: Vec<u8>, | ||
| 111 | frame_decoded: bool, | 163 | frame_decoded: bool, |
| 112 | } | 164 | } |
| 113 | 165 | ||
| @@ -156,6 +208,8 @@ impl Decoder { | |||
| 156 | }, | 208 | }, |
| 157 | mb_info: Vec::new(), | 209 | mb_info: Vec::new(), |
| 158 | token_ctxs: TokenContexts::new(0), | 210 | token_ctxs: TokenContexts::new(0), |
| 211 | + refs: [None, None, None, None], | ||
| 212 | + segment_map: Vec::new(), | ||
| 159 | frame_decoded: false, | 213 | frame_decoded: false, |
| 160 | } | 214 | } |
| 161 | } | 215 | } |
| @@ -169,14 +223,28 @@ impl Decoder { | |||
| 169 | /// Decode one frame; returns the picture when it is for display | 223 | /// Decode one frame; returns the picture when it is for display |
| 170 | /// (`show_frame`). Hidden frames still update the decode state. | 224 | /// (`show_frame`). Hidden frames still update the decode state. |
| 171 | pub fn decode(&mut self, frame: &[u8]) -> Result<Option<Picture>> { | 225 | pub fn decode(&mut self, frame: &[u8]) -> Result<Option<Picture>> { |
| 226 | + // Entropy revert (§9.7): the probability updates this frame | ||
| 227 | + // applies are transient unless `refresh_entropy` says they | ||
| 228 | + // persist. Snapshots are cheap array copies; segmentation and | ||
| 229 | + // loop-filter deltas live outside the frame context and never | ||
| 230 | + // revert (libvpx saves only `fc`). | ||
| 231 | + let coeff_probs = self.state.coeff_probs; | ||
| 232 | + let mv_probs = self.state.mv_probs; | ||
| 233 | + let ymode_probs = self.state.ymode_probs; | ||
| 234 | + let uv_mode_probs = self.state.uv_mode_probs; | ||
| 172 | let (header, hdr_dec) = FrameHeader::parse(frame, &mut self.state)?; | 235 | let (header, hdr_dec) = FrameHeader::parse(frame, &mut self.state)?; |
| 173 | - match header.tag.frame_type { | 236 | + let refresh_entropy = header.refresh.refresh_entropy; |
| 237 | + let pic = match header.tag.frame_type { | ||
| 174 | FrameType::Key => self.decode_keyframe(frame, header, hdr_dec), | 238 | FrameType::Key => self.decode_keyframe(frame, header, hdr_dec), |
| 175 | - FrameType::Inter => Err(Error::unsupported( | 239 | + FrameType::Inter => self.decode_interframe(frame, header, hdr_dec), |
| 176 | - "VP8 inter frame", | 240 | + }?; |
| 177 | - "inter-frame decoding is the next milestone of this crate", | 241 | + if !refresh_entropy { |
| 178 | - )), | 242 | + self.state.coeff_probs = coeff_probs; |
| 243 | + self.state.mv_probs = mv_probs; | ||
| 244 | + self.state.ymode_probs = ymode_probs; | ||
| 245 | + self.state.uv_mode_probs = uv_mode_probs; | ||
| 179 | } | 246 | } |
| 247 | + Ok(pic) | ||
| 180 | } | 248 | } |
| 181 | 249 | ||
| 182 | fn ensure_buffers(&mut self, kf: KeyFrameDims) { | 250 | fn ensure_buffers(&mut self, kf: KeyFrameDims) { |
| @@ -322,39 +390,80 @@ impl Decoder { | |||
| 322 | 390 | ||
| 323 | // Loop filter post-pass (see module docs for the ordering | 391 | // Loop filter post-pass (see module docs for the ordering |
| 324 | // equivalence argument). | 392 | // equivalence argument). |
| 325 | - if header.filter_level > 0 { | 393 | + self.run_loopfilter(&header, true); |
| 326 | - let infos: Vec<MbFilterInfo> = (0..self.mb_rows) | 394 | + |
| 327 | - .flat_map(|r| (0..self.mb_cols).map(move |c| (r, c))) | 395 | + // A key frame refreshes every reference slot and clears the |
| 328 | - .map(|(r, c)| { | 396 | + // persisted segment map (§9.3, §9.7). |
| 329 | - let mb = self.mbi(r + 1, c + 1); | 397 | + self.segment_map = vec![0; self.mb_cols * self.mb_rows]; |
| 330 | - MbFilterInfo { | 398 | + let rf = self.make_ref(); |
| 331 | - level: self.mb_filter_level(&header, mb), | 399 | + self.refs[1] = Some(rf.clone()); |
| 332 | - inner_edges: mb.has_nz || !mb.has_y2(), | 400 | + self.refs[2] = Some(rf.clone()); |
| 333 | - } | 401 | + self.refs[3] = Some(rf); |
| 334 | - }) | ||
| 335 | - .collect(); | ||
| 336 | - // The planes are bordered; the filter sees the unbordered | ||
| 337 | - // image (same row pitch) starting at pixel (0, 0). | ||
| 338 | - let (ystride, ustride, vstride) = (self.y.stride, self.u.stride, self.v.stride); | ||
| 339 | - let (mcols, mrows) = (self.mb_cols, self.mb_rows); | ||
| 340 | - let simple = header.filter_type == 1; | ||
| 341 | - let sharpness = header.sharpness_level; | ||
| 342 | - let yimg = &mut self.y.data[1 + ystride..]; | ||
| 343 | - let uimg = &mut self.u.data[1 + ustride..]; | ||
| 344 | - let vimg = &mut self.v.data[1 + vstride..]; | ||
| 345 | - loopfilter::filter_frame_ex( | ||
| 346 | - yimg, uimg, vimg, ystride, ustride, mcols, mrows, sharpness, simple, &infos, true, | ||
| 347 | - ); | ||
| 348 | - } | ||
| 349 | 402 | ||
| 350 | self.frame_decoded = true; | 403 | self.frame_decoded = true; |
| 351 | Ok(header.tag.show_frame.then(|| self.crop())) | 404 | Ok(header.tag.show_frame.then(|| self.crop())) |
| 352 | } | 405 | } |
| 353 | 406 | ||
| 354 | - /// Per-MB loop filter level (dixie `calculate_filter_parameters`): | 407 | + /// Loop-filter post-pass over the whole frame (dixie order-equivalent |
| 355 | - /// segment adjustment, then ref/mode deltas, clamped 0..63. Key | 408 | + /// driver; `inner_edges` follows libvpx's `skip_lf`: skipped MBs |
| 356 | - /// frames are all intra — ref index 0 (`CURRENT_FRAME`), with | 409 | + /// filter only their boundary edges, except 4x4-coded modes). |
| 357 | - /// `mode_delta[0]` applying to B_PRED. | 410 | + fn run_loopfilter(&mut self, header: &FrameHeader, is_keyframe: bool) { |
| 411 | + if header.filter_level == 0 { | ||
| 412 | + return; | ||
| 413 | + } | ||
| 414 | + let infos: Vec<MbFilterInfo> = (0..self.mb_rows) | ||
| 415 | + .flat_map(|r| (0..self.mb_cols).map(move |c| (r, c))) | ||
| 416 | + .map(|(r, c)| { | ||
| 417 | + let mb = self.mbi(r + 1, c + 1); | ||
| 418 | + MbFilterInfo { | ||
| 419 | + level: self.mb_filter_level(header, mb), | ||
| 420 | + inner_edges: mb.has_nz || !mb.has_y2(), | ||
| 421 | + } | ||
| 422 | + }) | ||
| 423 | + .collect(); | ||
| 424 | + // The planes are bordered; the filter sees the unbordered | ||
| 425 | + // image (same row pitch) starting at pixel (0, 0). | ||
| 426 | + let (ystride, ustride, vstride) = (self.y.stride, self.u.stride, self.v.stride); | ||
| 427 | + let (mcols, mrows) = (self.mb_cols, self.mb_rows); | ||
| 428 | + let simple = header.filter_type == 1; | ||
| 429 | + let sharpness = header.sharpness_level; | ||
| 430 | + let yimg = &mut self.y.data[1 + ystride..]; | ||
| 431 | + let uimg = &mut self.u.data[1 + ustride..]; | ||
| 432 | + let vimg = &mut self.v.data[1 + vstride..]; | ||
| 433 | + loopfilter::filter_frame_ex( | ||
| 434 | + yimg, uimg, vimg, ystride, ustride, mcols, mrows, sharpness, simple, &infos, | ||
| 435 | + is_keyframe, | ||
| 436 | + ); | ||
| 437 | + } | ||
| 438 | + | ||
| 439 | + /// Snapshot the reconstructed (post-LF) frame into a bordered | ||
| 440 | + /// reference buffer with 32-pixel edge-replicated borders. | ||
| 441 | + fn make_ref(&self) -> RefFrame { | ||
| 442 | + let w = self.mb_cols * 16; | ||
| 443 | + let h = self.mb_rows * 16; | ||
| 444 | + let (cw, ch) = (self.mb_cols * 8, self.mb_rows * 8); | ||
| 445 | + let mut rf = RefFrame { | ||
| 446 | + y: vec![0; (w + 2 * MC_BORDER) * (h + 2 * MC_BORDER)], | ||
| 447 | + u: vec![0; (cw + 2 * MC_BORDER) * (ch + 2 * MC_BORDER)], | ||
| 448 | + v: vec![0; (cw + 2 * MC_BORDER) * (ch + 2 * MC_BORDER)], | ||
| 449 | + stride: w + 2 * MC_BORDER, | ||
| 450 | + uv_stride: cw + 2 * MC_BORDER, | ||
| 451 | + }; | ||
| 452 | + copy_plane(&mut rf.y, rf.stride, &self.y.data, self.y.stride, w, h); | ||
| 453 | + copy_plane(&mut rf.u, rf.uv_stride, &self.u.data, self.u.stride, cw, ch); | ||
| 454 | + copy_plane(&mut rf.v, rf.uv_stride, &self.v.data, self.v.stride, cw, ch); | ||
| 455 | + mc::extend_plane(&mut rf.y, rf.stride, w, h, MC_BORDER); | ||
| 456 | + mc::extend_plane(&mut rf.u, rf.uv_stride, cw, ch, MC_BORDER); | ||
| 457 | + mc::extend_plane(&mut rf.v, rf.uv_stride, cw, ch, MC_BORDER); | ||
| 458 | + rf | ||
| 459 | + } | ||
| 460 | + | ||
| 461 | + | ||
| 462 | + /// Per-MB loop filter level (libvpx `loop_filter_frame_init`): | ||
| 463 | + /// segment level (clamped), then the reference delta, then the mode | ||
| 464 | + /// delta, clamped again. Mode slots: intra B_PRED → 0, other intra | ||
| 465 | + /// and ZEROMV → 1 (no delta for other intra), the moving modes → 2, | ||
| 466 | + /// SPLITMV → 3. | ||
| 358 | fn mb_filter_level(&self, header: &FrameHeader, mb: &MbInfo) -> u8 { | 467 | fn mb_filter_level(&self, header: &FrameHeader, mb: &MbInfo) -> u8 { |
| 359 | let seg = &header.segmentation; | 468 | let seg = &header.segmentation; |
| 360 | let mut level = i32::from(header.filter_level); | 469 | let mut level = i32::from(header.filter_level); |
| @@ -364,14 +473,471 @@ impl Decoder { | |||
| 364 | } | 473 | } |
| 365 | level = level.clamp(0, 63); | 474 | level = level.clamp(0, 63); |
| 366 | if header.lf_delta_enabled { | 475 | if header.lf_delta_enabled { |
| 367 | - level += self.state.ref_lf_delta[0]; | 476 | + level += self.state.ref_lf_delta[usize::from(mb.ref_frame)]; |
| 368 | - if mb.y_mode == modes::YMode::BPred.as_u8() { | 477 | + let mode_idx = if mb.ref_frame == 0 { |
| 369 | - level += self.state.mode_lf_delta[0]; | 478 | + if mb.y_mode == modes::YMode::BPred.as_u8() { |
| 479 | + Some(0) | ||
| 480 | + } else { | ||
| 481 | + None | ||
| 482 | + } | ||
| 483 | + } else { | ||
| 484 | + match mb.mv_ref { | ||
| 485 | + 0 => Some(1), // ZEROMV | ||
| 486 | + 4 => Some(3), // SPLITMV | ||
| 487 | + _ => Some(2), // NEAREST / NEAR / NEW | ||
| 488 | + } | ||
| 489 | + }; | ||
| 490 | + if let Some(mode_idx) = mode_idx { | ||
| 491 | + level += self.state.mode_lf_delta[mode_idx]; | ||
| 370 | } | 492 | } |
| 371 | level = level.clamp(0, 63); | 493 | level = level.clamp(0, 63); |
| 372 | } | 494 | } |
| 373 | level as u8 | 495 | level as u8 |
| 374 | } | 496 | } |
| 497 | + /// Decode one inter frame (§16): all mode records off partition 0 | ||
| 498 | + /// (row-major), then per-row tokens and reconstruction off the row's | ||
| 499 | + /// token partition, loop filter, then the reference updates (§9.7). | ||
| 500 | + fn decode_interframe( | ||
| 501 | + &mut self, | ||
| 502 | + frame: &[u8], | ||
| 503 | + header: FrameHeader, | ||
| 504 | + mut hdr_dec: BoolDecoder<'_>, | ||
| 505 | + ) -> Result<Option<Picture>> { | ||
| 506 | + if self.mb_cols == 0 || self.refs[1..4].iter().any(|r| r.is_none()) { | ||
| 507 | + return Err(Error::corrupt("VP8 inter frame before the first key frame")); | ||
| 508 | + } | ||
| 509 | + | ||
| 510 | + // Token partition bool decoders (§9.5) — identical tiling to | ||
| 511 | + // key frames. | ||
| 512 | + let mut partitions = Vec::with_capacity(header.partition_sizes.len()); | ||
| 513 | + let mut off = header.token_data_offset + 3 * (header.partition_sizes.len() - 1); | ||
| 514 | + for &sz in &header.partition_sizes { | ||
| 515 | + let end = off + sz as usize; | ||
| 516 | + let slice = frame | ||
| 517 | + .get(off..end) | ||
| 518 | + .ok_or_else(|| Error::corrupt("VP8 token partition extends past frame end"))?; | ||
| 519 | + partitions.push(BoolDecoder::new(slice)?); | ||
| 520 | + off = end; | ||
| 521 | + } | ||
| 522 | + | ||
| 523 | + // Dequant factors per segment (persisted segmentation state). | ||
| 524 | + let seg = &header.segmentation; | ||
| 525 | + let mut dqf = [Dequant { | ||
| 526 | + y1_dc: 0, | ||
| 527 | + y1_ac: 0, | ||
| 528 | + y2_dc: 0, | ||
| 529 | + y2_ac: 0, | ||
| 530 | + uv_dc: 0, | ||
| 531 | + uv_ac: 0, | ||
| 532 | + }; 4]; | ||
| 533 | + for (i, dq) in dqf.iter_mut().enumerate() { | ||
| 534 | + let q = if seg.enabled { | ||
| 535 | + if seg.abs_delta { | ||
| 536 | + seg.quant_idx[i] | ||
| 537 | + } else { | ||
| 538 | + i32::from(header.quant.yac_qi) + seg.quant_idx[i] | ||
| 539 | + } | ||
| 540 | + } else { | ||
| 541 | + i32::from(header.quant.yac_qi) | ||
| 542 | + }; | ||
| 543 | + *dq = transform::dequant( | ||
| 544 | + q, | ||
| 545 | + header.quant.ydc_delta, | ||
| 546 | + header.quant.y2dc_delta, | ||
| 547 | + header.quant.y2ac_delta, | ||
| 548 | + header.quant.uvdc_delta, | ||
| 549 | + header.quant.uvac_delta, | ||
| 550 | + ); | ||
| 551 | + } | ||
| 552 | + let ymode_probs = self.state.ymode_probs; | ||
| 553 | + let uv_mode_probs = self.state.uv_mode_probs; | ||
| 554 | + let sign_bias = [ | ||
| 555 | + false, | ||
| 556 | + header.refresh.sign_bias_golden, | ||
| 557 | + header.refresh.sign_bias_altref, | ||
| 558 | + ]; | ||
| 559 | + | ||
| 560 | + // Phase 1: every mode record of the frame, row-major (§16). | ||
| 561 | + for row in 1..=self.mb_rows { | ||
| 562 | + for col in 1..=self.mb_cols { | ||
| 563 | + let mut mb = MbInfo::default(); | ||
| 564 | + if header.segmentation.enabled { | ||
| 565 | + if header.segmentation.update_map { | ||
| 566 | + mb.segment_id = | ||
| 567 | + hdr_dec.read_tree(&SEGMENT_TREE, &header.segmentation.tree_probs); | ||
| 568 | + self.segment_map[(row - 1) * self.mb_cols + (col - 1)] = mb.segment_id; | ||
| 569 | + } else { | ||
| 570 | + mb.segment_id = self.segment_map[(row - 1) * self.mb_cols + (col - 1)]; | ||
| 571 | + } | ||
| 572 | + } | ||
| 573 | + if header.coeff_skip_enabled { | ||
| 574 | + mb.skip = hdr_dec.read_bool(header.prob_skip_false); | ||
| 575 | + } | ||
| 576 | + self.parse_inter_modes( | ||
| 577 | + &mut hdr_dec, | ||
| 578 | + row, | ||
| 579 | + col, | ||
| 580 | + &mut mb, | ||
| 581 | + &header, | ||
| 582 | + &ymode_probs, | ||
| 583 | + &uv_mode_probs, | ||
| 584 | + &sign_bias, | ||
| 585 | + ); | ||
| 586 | + *self.mbi_mut(row, col) = mb; | ||
| 587 | + } | ||
| 588 | + } | ||
| 589 | + | ||
| 590 | + // Phases 2+3 per row: tokens, then reconstruction (MC or intra). | ||
| 591 | + for row in 1..=self.mb_rows { | ||
| 592 | + self.token_ctxs.reset_left(); | ||
| 593 | + let part = (row - 1) % partitions.len(); | ||
| 594 | + for col in 1..=self.mb_cols { | ||
| 595 | + let mb = self.mbi(row, col).clone(); | ||
| 596 | + let has_y2 = mb.has_y2(); | ||
| 597 | + let seg_i = usize::from(header.segmentation.enabled) * usize::from(mb.segment_id); | ||
| 598 | + let dq = &dqf[seg_i]; | ||
| 599 | + let mb_dq = Dq { | ||
| 600 | + y1_dc: dq.y1_dc, | ||
| 601 | + y1_ac: dq.y1_ac, | ||
| 602 | + uv_dc: dq.uv_dc, | ||
| 603 | + uv_ac: dq.uv_ac, | ||
| 604 | + y2_dc: dq.y2_dc, | ||
| 605 | + y2_ac: dq.y2_ac, | ||
| 606 | + }; | ||
| 607 | + let probs = self.state.coeff_probs; | ||
| 608 | + let coeffs = if mb.skip { | ||
| 609 | + tokens::skip_mb_tokens(&mut self.token_ctxs, col - 1, has_y2) | ||
| 610 | + } else { | ||
| 611 | + tokens::decode_mb_tokens( | ||
| 612 | + &mut partitions[part], | ||
| 613 | + &probs, | ||
| 614 | + &mb_dq, | ||
| 615 | + has_y2, | ||
| 616 | + &mut self.token_ctxs, | ||
| 617 | + col - 1, | ||
| 618 | + ) | ||
| 619 | + }; | ||
| 620 | + { | ||
| 621 | + // "Special case: force the loop filter to skip when | ||
| 622 | + // eobtotal is zero" (libvpx decodeframe.c): a decoded | ||
| 623 | + // MB without non-zero coefficients filters like a | ||
| 624 | + // skipped one. | ||
| 625 | + let slot = &mut self.mb_info[row * (self.mb_cols + 1) + col]; | ||
| 626 | + slot.has_nz = coeffs.has_nonzero(); | ||
| 627 | + slot.skip = mb.skip || !coeffs.has_nonzero(); | ||
| 628 | + } | ||
| 629 | + self.reconstruct_inter_mb(row - 1, col - 1, &mb, &coeffs); | ||
| 630 | + } | ||
| 631 | + } | ||
| 632 | + | ||
| 633 | + self.run_loopfilter(&header, false); | ||
| 634 | + | ||
| 635 | + // Reference buffer updates, libvpx `swap_frame_buffers` order: | ||
| 636 | + // the copies run first (copy_gf = 2 must see a possibly | ||
| 637 | + // just-updated altref), then the refreshes, then last. | ||
| 638 | + let new = self.make_ref(); | ||
| 639 | + match header.refresh.copy_arf { | ||
| 640 | + 1 => self.refs[3] = self.refs[1].clone(), | ||
| 641 | + 2 => self.refs[3] = self.refs[2].clone(), | ||
| 642 | + _ => {} | ||
| 643 | + } | ||
| 644 | + match header.refresh.copy_gf { | ||
| 645 | + 1 => self.refs[2] = self.refs[1].clone(), | ||
| 646 | + 2 => self.refs[2] = self.refs[3].clone(), | ||
| 647 | + _ => {} | ||
| 648 | + } | ||
| 649 | + if header.refresh.refresh_gf { | ||
| 650 | + self.refs[2] = Some(new.clone()); | ||
| 651 | + } | ||
| 652 | + if header.refresh.refresh_arf { | ||
| 653 | + self.refs[3] = Some(new.clone()); | ||
| 654 | + } | ||
| 655 | + if header.refresh.refresh_last { | ||
| 656 | + self.refs[1] = Some(new); | ||
| 657 | + } | ||
| 658 | + | ||
| 659 | + self.frame_decoded = true; | ||
| 660 | + Ok(header.tag.show_frame.then(|| self.crop())) | ||
| 661 | + } | ||
| 662 | + | ||
| 663 | + /// Inter-frame mode record (§16, libvpx `read_mb_modes_mv`): intra | ||
| 664 | + /// at the persisted mode probabilities, or an inter mode with its | ||
| 665 | + /// motion vectors off the neighborhood census. | ||
| 666 | + #[allow(clippy::too_many_arguments)] | ||
| 667 | + fn parse_inter_modes( | ||
| 668 | + &mut self, | ||
| 669 | + d: &mut BoolDecoder<'_>, | ||
| 670 | + row: usize, | ||
| 671 | + col: usize, | ||
| 672 | + mb: &mut MbInfo, | ||
| 673 | + header: &FrameHeader, | ||
| 674 | + ymode_probs: &[u8; 4], | ||
| 675 | + uv_mode_probs: &[u8; 3], | ||
| 676 | + sign_bias: &[bool; 3], | ||
| 677 | + ) { | ||
| 678 | + if !d.read_bool(header.prob_intra) { | ||
| 679 | + // Intra-coded MB inside an inter frame (§16.1). Subblock | ||
| 680 | + // modes use the same fixed context table as key frames. | ||
| 681 | + mb.ref_frame = 0; | ||
| 682 | + let ymode = modes::read_ymode(d, ymode_probs); | ||
| 683 | + mb.y_mode = ymode.as_u8(); | ||
| 684 | + if ymode == modes::YMode::BPred { | ||
| 685 | + for j in 0..16u8 { | ||
| 686 | + let a = self.above_block_mode(row, col, j, mb); | ||
| 687 | + let l = self.left_block_mode(row, col, j, mb); | ||
| 688 | + mb.b_modes[usize::from(j)] = | ||
| 689 | + modes::read_bmode(d, &KF_BMODE_PROB[usize::from(a)][usize::from(l)]) | ||
| 690 | + .as_u8(); | ||
| 691 | + } | ||
| 692 | + } | ||
| 693 | + mb.uv_mode = modes::read_uvmode(d, uv_mode_probs).as_u8(); | ||
| 694 | + return; | ||
| 695 | + } | ||
| 696 | + | ||
| 697 | + mb.ref_frame = if !d.read_bool(header.prob_last) { | ||
| 698 | + 1 | ||
| 699 | + } else if !d.read_bool(header.prob_gf) { | ||
| 700 | + 2 | ||
| 701 | + } else { | ||
| 702 | + 3 | ||
| 703 | + }; | ||
| 704 | + let to_bias = sign_bias[usize::from(mb.ref_frame) - 1]; | ||
| 705 | + | ||
| 706 | + let edges = modes::mb_edges(col - 1, row - 1, self.mb_cols, self.mb_rows); | ||
| 707 | + let above = self.mv_neighbor(row - 1, col, sign_bias); | ||
| 708 | + let left = self.mv_neighbor(row, col - 1, sign_bias); | ||
| 709 | + let aboveleft = self.mv_neighbor(row - 1, col - 1, sign_bias); | ||
| 710 | + let (mut near_mvs, cnt) = modes::find_near_mvs(above, left, aboveleft, to_bias); | ||
| 711 | + | ||
| 712 | + if !d.read_bool(modes::MODE_CONTEXTS[cnt[modes::CNT_INTRA] as usize][0]) { | ||
| 713 | + mb.mv_ref = 0; // ZEROMV | ||
| 714 | + return; | ||
| 715 | + } | ||
| 716 | + if !d.read_bool(modes::MODE_CONTEXTS[cnt[modes::CNT_NEAREST] as usize][1]) { | ||
| 717 | + mb.mv_ref = 1; // NEARESTMV | ||
| 718 | + mb.mv = near_mvs[modes::CNT_NEAREST]; | ||
| 719 | + modes::clamp_mv2(&mut mb.mv, edges); | ||
| 720 | + return; | ||
| 721 | + } | ||
| 722 | + if !d.read_bool(modes::MODE_CONTEXTS[cnt[modes::CNT_NEAR] as usize][2]) { | ||
| 723 | + mb.mv_ref = 2; // NEARMV | ||
| 724 | + mb.mv = near_mvs[modes::CNT_NEAR]; | ||
| 725 | + modes::clamp_mv2(&mut mb.mv, edges); | ||
| 726 | + return; | ||
| 727 | + } | ||
| 728 | + | ||
| 729 | + // NEWMV or SPLITMV: both code vectors as deltas off the best | ||
| 730 | + // candidate — the nearest one when it dominates the census — | ||
| 731 | + // clamped into range first (§16.2-§16.4). | ||
| 732 | + let near_index = usize::from(cnt[modes::CNT_NEAREST] >= cnt[modes::CNT_INTRA]); | ||
| 733 | + modes::clamp_mv2(&mut near_mvs[near_index], edges); | ||
| 734 | + let best = near_mvs[near_index]; | ||
| 735 | + if !d.read_bool(modes::MODE_CONTEXTS[cnt[modes::CNT_SPLITMV] as usize][3]) { | ||
| 736 | + mb.mv_ref = 3; // NEWMV | ||
| 737 | + let mut mv = modes::read_mv(d, &self.state.mv_probs); | ||
| 738 | + mv.0 = mv.0.wrapping_add(best.0); | ||
| 739 | + mv.1 = mv.1.wrapping_add(best.1); | ||
| 740 | + mb.mv_clamp = modes::mv_out_of_bounds(mv, edges); | ||
| 741 | + mb.mv = mv; | ||
| 742 | + } else { | ||
| 743 | + self.parse_split_mv(d, row, col, mb, best, edges); | ||
| 744 | + } | ||
| 745 | + } | ||
| 746 | + | ||
| 747 | + /// Census input from one neighbour slot (§16.3); border and intra | ||
| 748 | + /// MBs contribute nothing, zero-vector inter MBs count like intra. | ||
| 749 | + fn mv_neighbor(&self, row: usize, col: usize, sign_bias: &[bool; 3]) -> modes::MvNeighbor { | ||
| 750 | + if row == 0 || col == 0 || self.mbi(row, col).ref_frame == 0 { | ||
| 751 | + return modes::MvNeighbor { | ||
| 752 | + intra: true, | ||
| 753 | + zero: false, | ||
| 754 | + mv: (0, 0), | ||
| 755 | + is_split: false, | ||
| 756 | + sign_bias: false, | ||
| 757 | + }; | ||
| 758 | + } | ||
| 759 | + let mb = self.mbi(row, col); | ||
| 760 | + modes::MvNeighbor { | ||
| 761 | + intra: false, | ||
| 762 | + zero: mb.mv == (0, 0), | ||
| 763 | + mv: mb.mv, | ||
| 764 | + is_split: mb.mv_ref == 4, | ||
| 765 | + sign_bias: sign_bias[usize::from(mb.ref_frame) - 1], | ||
| 766 | + } | ||
| 767 | + } | ||
| 768 | + | ||
| 769 | + /// SPLITMV mode record (§16.4, libvpx `decode_split_mv`): partition | ||
| 770 | + /// shape, then one vector per subset coded against its left/above | ||
| 771 | + /// contexts, each filling a whole group of subblocks. | ||
| 772 | + fn parse_split_mv( | ||
| 773 | + &mut self, | ||
| 774 | + d: &mut BoolDecoder<'_>, | ||
| 775 | + row: usize, | ||
| 776 | + col: usize, | ||
| 777 | + mb: &mut MbInfo, | ||
| 778 | + best: (i16, i16), | ||
| 779 | + edges: (i32, i32, i32, i32), | ||
| 780 | + ) { | ||
| 781 | + let s = modes::read_mv_partition(d); | ||
| 782 | + let num_p = modes::MV_PARTITION_COUNT[s]; | ||
| 783 | + let mv_probs = self.state.mv_probs; | ||
| 784 | + for j in 0..num_p { | ||
| 785 | + let k = usize::from(modes::MBSPLIT_OFFSET[s][j]); | ||
| 786 | + let leftmv = if k % 4 == 0 { | ||
| 787 | + let lm = self.mbi(row, col - 1); | ||
| 788 | + if lm.mv_ref == 4 { | ||
| 789 | + lm.bmi[k + 3] | ||
| 790 | + } else { | ||
| 791 | + lm.mv | ||
| 792 | + } | ||
| 793 | + } else { | ||
| 794 | + mb.bmi[k - 1] | ||
| 795 | + }; | ||
| 796 | + let abovemv = if k < 4 { | ||
| 797 | + let am = self.mbi(row - 1, col); | ||
| 798 | + if am.mv_ref == 4 { | ||
| 799 | + am.bmi[k + 12] | ||
| 800 | + } else { | ||
| 801 | + am.mv | ||
| 802 | + } | ||
| 803 | + } else { | ||
| 804 | + mb.bmi[k - 4] | ||
| 805 | + }; | ||
| 806 | + let ctx = modes::mv_cont(leftmv, abovemv); | ||
| 807 | + let blockmv = match modes::read_sub_mv_ref(d, ctx) { | ||
| 808 | + modes::SubMvRef::Left4x4 => leftmv, | ||
| 809 | + modes::SubMvRef::Above4x4 => abovemv, | ||
| 810 | + modes::SubMvRef::Zero4x4 => (0, 0), | ||
| 811 | + modes::SubMvRef::New4x4 => { | ||
| 812 | + let mut mv = modes::read_mv(d, &mv_probs); | ||
| 813 | + mv.0 = mv.0.wrapping_add(best.0); | ||
| 814 | + mv.1 = mv.1.wrapping_add(best.1); | ||
| 815 | + mv | ||
| 816 | + } | ||
| 817 | + }; | ||
| 818 | + mb.mv_clamp |= modes::mv_out_of_bounds(blockmv, edges); | ||
| 819 | + let count = modes::MBSPLIT_FILL_COUNT[s]; | ||
| 820 | + for &off in &modes::MBSPLIT_FILL_OFFSET[s][j * count..(j + 1) * count] { | ||
| 821 | + mb.bmi[usize::from(off)] = blockmv; | ||
| 822 | + } | ||
| 823 | + } | ||
| 824 | + mb.mv_ref = 4; | ||
| 825 | + // 4x4 residual coding without a Y2 block (like B_PRED). | ||
| 826 | + mb.y_mode = modes::YMode::BPred.as_u8(); | ||
| 827 | + // Neighbours' census sees the bottom-right subblock's vector. | ||
| 828 | + mb.mv = mb.bmi[15]; | ||
| 829 | + } | ||
| 830 | + | ||
| 831 | + /// Reconstruct one inter-frame MB: motion compensation from the | ||
| 832 | + /// reference (or intra prediction), then the residual (§16, §14). | ||
| 833 | + fn reconstruct_inter_mb(&mut self, row: usize, col: usize, mb: &MbInfo, coeffs: &MbCoeffs) { | ||
| 834 | + if mb.ref_frame == 0 { | ||
| 835 | + self.reconstruct_intra_mb(row, col, mb, coeffs); | ||
| 836 | + return; | ||
| 837 | + } | ||
| 838 | + if std::env::var_os("EC_VP8_TRACE").is_some() { | ||
| 839 | + eprintln!( | ||
| 840 | + "IMB r={} c={} ref={} mvref={} mv=({},{}) clamp={} skip={}", | ||
| 841 | + row, col, mb.ref_frame, mb.mv_ref, mb.mv.0, mb.mv.1, mb.mv_clamp, mb.skip | ||
| 842 | + ); | ||
| 843 | + } | ||
| 844 | + let reff = self.refs[usize::from(mb.ref_frame)] | ||
| 845 | + .as_ref() | ||
| 846 | + .expect("reference slots checked at frame start"); | ||
| 847 | + let x = col * 16; | ||
| 848 | + let y = row * 16; | ||
| 849 | + let w = self.mb_cols * 16; | ||
| 850 | + let h = self.mb_rows * 16; | ||
| 851 | + let (cw, ch) = (w / 2, h / 2); | ||
| 852 | + // Luma: one 16x16 predict, or 16 independent 4x4 predicts for | ||
| 853 | + // SPLITMV (identical output to libvpx's grouped predicts). Each | ||
| 854 | + // predict lands at its own (bx, by) slot of one 16x16 patch. | ||
| 855 | + let mut py = [0u8; 256]; | ||
| 856 | + if mb.mv_ref == 4 { | ||
| 857 | + for b in 0..16usize { | ||
| 858 | + let (bx, by) = ((b % 4) * 4, (b / 4) * 4); | ||
| 859 | + mc::predict_luma( | ||
| 860 | + &reff.y, reff.stride, MC_BORDER, w, h, x + bx, y + by, mb.bmi[b], false, | ||
| 861 | + &mut py[by * 16 + bx..], 16, 4, 4, | ||
| 862 | + ); | ||
| 863 | + } | ||
| 864 | + } else { | ||
| 865 | + mc::predict_luma( | ||
| 866 | + &reff.y, reff.stride, MC_BORDER, w, h, x, y, mb.mv, mb.mv_clamp, &mut py, 16, | ||
| 867 | + 16, 16, | ||
| 868 | + ); | ||
| 869 | + } | ||
| 870 | + | ||
| 871 | + // Chroma: one 8x8 predict per quadrant per plane. Whole-MB modes | ||
| 872 | + // halve the single vector; SPLITMV sums its quadrant's four | ||
| 873 | + // subblock vectors (§16.4, libvpx build_4x4uvmvs). The whole-MB | ||
| 874 | + // path re-clamps UNCONDITIONALLY (reconinter.c: rounding the | ||
| 875 | + // derived chroma MV to full-pel can move it outside the tap | ||
| 876 | + // window even when the luma MV was in range); the SPLITMV path | ||
| 877 | + // clamps only under need_to_clamp_mvs. | ||
| 878 | + let mut pu = [0u8; 256]; | ||
| 879 | + let mut pv = [0u8; 256]; | ||
| 880 | + for q in 0..4usize { | ||
| 881 | + let (qx16, qy16) = ((q % 2) * 8, (q / 2) * 8); | ||
| 882 | + let (cx, cy) = (x / 2 + qx16, y / 2 + qy16); | ||
| 883 | + let (uvmv, uv_clamp) = if mb.mv_ref == 4 { | ||
| 884 | + let (qx, qy) = ((q % 2) * 2, (q / 2) * 2); | ||
| 885 | + ( | ||
| 886 | + modes::chroma_mv_split(&[ | ||
| 887 | + mb.bmi[qy * 4 + qx], | ||
| 888 | + mb.bmi[qy * 4 + qx + 1], | ||
| 889 | + mb.bmi[qy * 4 + qx + 4], | ||
| 890 | + mb.bmi[qy * 4 + qx + 5], | ||
| 891 | + ]), | ||
| 892 | + mb.mv_clamp, | ||
| 893 | + ) | ||
| 894 | + } else { | ||
| 895 | + (modes::chroma_mv_whole(mb.mv), true) | ||
| 896 | + }; | ||
| 897 | + mc::predict_chroma( | ||
| 898 | + &reff.u, reff.uv_stride, MC_BORDER, cw, ch, cx, cy, uvmv, uv_clamp, | ||
| 899 | + &mut pu[qy16 * 16 + qx16..], 16, 8, 8, | ||
| 900 | + ); | ||
| 901 | + mc::predict_chroma( | ||
| 902 | + &reff.v, reff.uv_stride, MC_BORDER, cw, ch, cx, cy, uvmv, uv_clamp, | ||
| 903 | + &mut pv[qy16 * 16 + qx16..], 16, 8, 8, | ||
| 904 | + ); | ||
| 905 | + } | ||
| 906 | + if std::env::var_os("EC_VP8_TRACE").is_some() && row == 0 && col == 0 { | ||
| 907 | + eprintln!( | ||
| 908 | + "CHROMA pred_u[0..8]={:?} refu_vis[0..8]={:?} coeffs.u0dcsum={}", | ||
| 909 | + &pu[0..8], | ||
| 910 | + &reff.u[MC_BORDER * reff.uv_stride + MC_BORDER..MC_BORDER * reff.uv_stride + MC_BORDER + 8], | ||
| 911 | + coeffs.u[0].iter().sum::<i16>() | ||
| 912 | + ); | ||
| 913 | + } | ||
| 914 | + | ||
| 915 | + // Residual: every block adds onto its MC prediction. | ||
| 916 | + let y_dcs: [i16; 16] = if coeffs.has_y2 { | ||
| 917 | + transform::iwht4x4(&coeffs.y2) | ||
| 918 | + } else { | ||
| 919 | + [0; 16] | ||
| 920 | + }; | ||
| 921 | + for b in 0..16usize { | ||
| 922 | + let (bx, by) = ((b % 4) * 4, (b / 4) * 4); | ||
| 923 | + let mut c = coeffs.y[b]; | ||
| 924 | + if coeffs.has_y2 { | ||
| 925 | + c[0] = y_dcs[b]; | ||
| 926 | + } | ||
| 927 | + let dst = self.y.at(x + bx, y + by); | ||
| 928 | + idct_add_into(&mut self.y.data, &c, &py, 16, by * 16 + bx, dst, self.y.stride); | ||
| 929 | + } | ||
| 930 | + for (plane_data, coeffs_uv, pred, stride) in [ | ||
| 931 | + (&mut self.u.data, &coeffs.u, &pu, self.u.stride), | ||
| 932 | + (&mut self.v.data, &coeffs.v, &pv, self.v.stride), | ||
| 933 | + ] { | ||
| 934 | + for b in 0..4usize { | ||
| 935 | + let (bx, by) = ((b % 2) * 4, (b / 2) * 4); | ||
| 936 | + let dst = (1 + y / 2 + by) * stride + 1 + x / 2 + bx; | ||
| 937 | + idct_add_into(plane_data, &coeffs_uv[b], pred, 16, by * 16 + bx, dst, stride); | ||
| 938 | + } | ||
| 939 | + } | ||
| 940 | + } | ||
| 375 | 941 | ||
| 376 | /// Key-frame mode record (§11): Y mode at fixed probabilities, then | 942 | /// Key-frame mode record (§11): Y mode at fixed probabilities, then |
| 377 | /// 16 context-conditioned subblock modes for B_PRED, then chroma. | 943 | /// 16 context-conditioned subblock modes for B_PRED, then chroma. |
crates/ec-vp8/src/header.rsmodified+0 -6
| @@ -372,16 +372,10 @@ impl FrameHeader { | |||
| 372 | // --- entropy updates (§9.9; dixie decode_entropy_header) --- | 372 | // --- entropy updates (§9.9; dixie decode_entropy_header) --- |
| 373 | // Coefficient probability updates are read for every frame; on | 373 | // Coefficient probability updates are read for every frame; on |
| 374 | // key frames the tables were reset to defaults above. | 374 | // key frames the tables were reset to defaults above. |
| 375 | - let trace = std::env::var_os("EC_VP8_TRACE").is_some(); | ||
| 376 | - let mut idx = 0; | ||
| 377 | for i in 0..4 { | 375 | for i in 0..4 { |
| 378 | for j in 0..8 { | 376 | for j in 0..8 { |
| 379 | for k in 0..3 { | 377 | for k in 0..3 { |
| 380 | for l in 0..11 { | 378 | for l in 0..11 { |
| 381 | - if trace { | ||
| 382 | - eprintln!("IDX {idx} flag={}", crate::tables::COEFF_UPDATE_PROBS[i][j][k][l]); | ||
| 383 | - } | ||
| 384 | - idx += 1; | ||
| 385 | if d.read_bool(crate::tables::COEFF_UPDATE_PROBS[i][j][k][l]) { | 379 | if d.read_bool(crate::tables::COEFF_UPDATE_PROBS[i][j][k][l]) { |
| 386 | state.coeff_probs[i][j][k][l] = d.read_literal(8) as u8; | 380 | state.coeff_probs[i][j][k][l] = d.read_literal(8) as u8; |
| 387 | } | 381 | } |
crates/ec-vp8/src/lib.rsmodified+1 -0
| @@ -20,6 +20,7 @@ pub mod frame; | |||
| 20 | pub mod header; | 20 | pub mod header; |
| 21 | pub mod intra; | 21 | pub mod intra; |
| 22 | pub mod loopfilter; | 22 | pub mod loopfilter; |
| 23 | +mod mc; | ||
| 23 | pub mod modes; | 24 | pub mod modes; |
| 24 | pub mod tables; | 25 | pub mod tables; |
| 25 | pub mod tokens; | 26 | pub mod tokens; |
crates/ec-vp8/src/mc.rsadded+712 -0
| @@ -0,0 +1,712 @@ | |||
| 1 | +//! Motion-compensation inter predictors: reference border extension, 6-tap | ||
| 2 | +//! luma prediction, bilinear chroma prediction, and the UMV-border motion | ||
| 3 | +//! vector clamps. | ||
| 4 | +//! | ||
| 5 | +//! Normative source: [RFC 6386] §18.3 (sub-pixel interpolation, two | ||
| 6 | +//! separable 1-D passes, each sample `clamp255((a + 64) >> 7)`). The loop | ||
| 7 | +//! structure, intermediate widths and the border clamps follow the libvpx | ||
| 8 | +//! reference implementation (`vp8/common/filter.c`, `vp8/common/ | ||
| 9 | +//! reconinter.c`, `vp8/common/extend.c`) sample-for-sample; the per-pass | ||
| 10 | +//! 0..=255 clamping of the 6-tap intermediate is mandated by the RFC's | ||
| 11 | +//! `interp()` and is what the integer test vectors below pin down. | ||
| 12 | +//! | ||
| 13 | +//! Reference planes arrive already border-extended by [`extend_plane`] | ||
| 14 | +//! (edge replication, `border` = 32 like libvpx's `VP8BORDERINPIXELS`); | ||
| 15 | +//! `src` starts at the first byte of the bordered buffer (top-left border | ||
| 16 | +//! corner) and visible pixel `(px, py)` therefore lives at | ||
| 17 | +//! `src[(py + border) * stride + (px + border)]`. | ||
| 18 | +//! | ||
| 19 | +//! Motion vectors are `(row, col)` pairs in eighth-pel units (`i16`), and | ||
| 20 | +//! the C arithmetic right shifts / two's-complement `& 7` phase extraction | ||
| 21 | +//! are reproduced exactly (`i32 >> 3` and `& 7` on negatives). | ||
| 22 | +//! | ||
| 23 | +//! [RFC 6386]: https://www.rfc-editor.org/rfc/rfc6386 | ||
| 24 | + | ||
| 25 | +/// `VP8_FILTER_WEIGHT` (filter.c): tap weights sum to this, DC passes. | ||
| 26 | +const VP8_FILTER_WEIGHT: i32 = 128; | ||
| 27 | +/// `VP8_FILTER_SHIFT` (filter.c): normalize the 7-bit-precision result. | ||
| 28 | +const VP8_FILTER_SHIFT: i32 = 7; | ||
| 29 | + | ||
| 30 | +/// `vp8_sub_pel_filters[8][6]` (filter.c), indexed by the 0..8 sub-pel phase. | ||
| 31 | +const SUB_PEL_FILTERS: [[i32; 6]; 8] = [ | ||
| 32 | + [0, 0, 128, 0, 0, 0], // degenerate whole-pel (RFC 6386 §18.3 "filters[0]") | ||
| 33 | + [0, -6, 123, 12, -1, 0], | ||
| 34 | + [2, -11, 108, 36, -8, 1], | ||
| 35 | + [0, -9, 93, 50, -6, 0], | ||
| 36 | + [3, -16, 77, 77, -16, 3], | ||
| 37 | + [0, -6, 50, 93, -9, 0], | ||
| 38 | + [1, -8, 36, 108, -11, 2], | ||
| 39 | + [0, -1, 12, 123, -6, 0], | ||
| 40 | +]; | ||
| 41 | + | ||
| 42 | +/// `vp8_bilinear_filters[8][2]` (filter.c); chroma prediction is always | ||
| 43 | +/// bilinear (RFC 6386 §19.2). | ||
| 44 | +const BILINEAR_FILTERS: [[i32; 2]; 8] = [ | ||
| 45 | + [128, 0], | ||
| 46 | + [112, 16], | ||
| 47 | + [96, 32], | ||
| 48 | + [80, 48], | ||
| 49 | + [64, 64], | ||
| 50 | + [48, 80], | ||
| 51 | + [32, 96], | ||
| 52 | + [16, 112], | ||
| 53 | +]; | ||
| 54 | + | ||
| 55 | +#[inline] | ||
| 56 | +fn clamp255(v: i32) -> i32 { | ||
| 57 | + if v < 0 { | ||
| 58 | + 0 | ||
| 59 | + } else if v > 255 { | ||
| 60 | + 255 | ||
| 61 | + } else { | ||
| 62 | + v | ||
| 63 | + } | ||
| 64 | +} | ||
| 65 | + | ||
| 66 | +/// Extends the visible `vis_w` x `vis_h` region of `plane` into the `border` | ||
| 67 | +/// ring on all four sides by edge replication: every out-of-image sample | ||
| 68 | +/// becomes the nearest in-image sample. | ||
| 69 | +/// | ||
| 70 | +/// Port of `copy_and_extend_plane` (extend.c) with `et = el = eb = er = | ||
| 71 | +/// border` and `interleave_step = 1`, in place: first the left/right border | ||
| 72 | +/// of every visible row is filled from the row's first/last column, then the | ||
| 73 | +/// (already side-extended) top and bottom rows are copied into the top and | ||
| 74 | +/// bottom borders — which gives the corners their corner pixels, matching | ||
| 75 | +/// the C exactly. | ||
| 76 | +pub(crate) fn extend_plane(plane: &mut [u8], stride: usize, vis_w: usize, vis_h: usize, border: usize) { | ||
| 77 | + debug_assert!(vis_w >= 1 && vis_h >= 1); | ||
| 78 | + debug_assert!(stride >= border * 2 + vis_w); | ||
| 79 | + | ||
| 80 | + let line = border + vis_w + border; | ||
| 81 | + | ||
| 82 | + // extend.c: "copy the left and right most columns out". | ||
| 83 | + for r in 0..vis_h { | ||
| 84 | + let base = (border + r) * stride + border; | ||
| 85 | + let first = plane[base]; | ||
| 86 | + let last = plane[base + vis_w - 1]; | ||
| 87 | + plane[base - border..base].fill(first); | ||
| 88 | + plane[base + vis_w..base + vis_w + border].fill(last); | ||
| 89 | + } | ||
| 90 | + | ||
| 91 | + // extend.c: "Now copy the top and bottom lines into each line of the | ||
| 92 | + // respective borders" — src rows are the already side-extended lines. | ||
| 93 | + let top = border * stride; | ||
| 94 | + let bottom = (border + vis_h - 1) * stride; | ||
| 95 | + for i in 0..border { | ||
| 96 | + plane.copy_within(top..top + line, (border - 1 - i) * stride); | ||
| 97 | + plane.copy_within(bottom..bottom + line, (border + vis_h + i) * stride); | ||
| 98 | + } | ||
| 99 | +} | ||
| 100 | + | ||
| 101 | +/// Luma predictor for one `bw` x `bh` block (`bw`,`bh` in 4..=16, multiples | ||
| 102 | +/// of 4) whose visible-image top-left pixel is `(x, y)`. | ||
| 103 | +/// | ||
| 104 | +/// Semantics of `vp8_build_inter_predictors_b` / | ||
| 105 | +/// `vp8_build_inter16x16_predictors_mby` + `vp8_sixtap_predict*` (reconinter.c, | ||
| 106 | +/// filter.c): full-pel start `(x + (mv.1 >> 3), y + (mv.0 >> 3))`, phases | ||
| 107 | +/// `fx = mv.1 & 7`, `fy = mv.0 & 7`; a whole-pel MV is a plain copy, | ||
| 108 | +/// otherwise the separable 6-tap filter runs horizontally into an `i32` | ||
| 109 | +/// intermediate — clamped to 0..=255 per pass like libvpx's `int` `FData` — | ||
| 110 | +/// then vertically, both passes computing `(dot + 64) >> 7` and clamping. | ||
| 111 | +/// | ||
| 112 | +/// When `need_clamp` is set, the MV is first passed through | ||
| 113 | +/// [`clamp_mv_to_umv_border`]. | ||
| 114 | +pub(crate) fn predict_luma( | ||
| 115 | + src: &[u8], | ||
| 116 | + stride: usize, | ||
| 117 | + border: usize, | ||
| 118 | + vis_w: usize, | ||
| 119 | + vis_h: usize, | ||
| 120 | + x: usize, | ||
| 121 | + y: usize, | ||
| 122 | + mv: (i16, i16), | ||
| 123 | + need_clamp: bool, | ||
| 124 | + dst: &mut [u8], | ||
| 125 | + dst_stride: usize, | ||
| 126 | + bw: usize, | ||
| 127 | + bh: usize, | ||
| 128 | +) { | ||
| 129 | + debug_assert!(bw >= 4 && bw <= 16 && bw % 4 == 0); | ||
| 130 | + debug_assert!(bh >= 4 && bh <= 16 && bh % 4 == 0); | ||
| 131 | + | ||
| 132 | + let mut mv = (i32::from(mv.0), i32::from(mv.1)); | ||
| 133 | + if need_clamp { | ||
| 134 | + clamp_mv_to_umv_border(&mut mv, x >> 4, y >> 4, vis_w, vis_h); | ||
| 135 | + } | ||
| 136 | + | ||
| 137 | + let fx = (mv.1 & 7) as usize; | ||
| 138 | + let fy = (mv.0 & 7) as usize; | ||
| 139 | + let sx = border as i32 + x as i32 + (mv.1 >> 3); | ||
| 140 | + let sy = border as i32 + y as i32 + (mv.0 >> 3); | ||
| 141 | + // 6-tap reads reach 2 before / 3 after the start; the libvpx clamp | ||
| 142 | + // arithmetic keeps those inside the 32-px border. | ||
| 143 | + debug_assert!(sx >= 2 && sy >= 2, "reference read outside bordered plane"); | ||
| 144 | + let (sx, sy) = (sx as usize, sy as usize); | ||
| 145 | + | ||
| 146 | + if fx == 0 && fy == 0 { | ||
| 147 | + // reconinter.c build_inter_predictors_b / vp8_copy_mem*. | ||
| 148 | + for r in 0..bh { | ||
| 149 | + let s = (sy + r) * stride + sx; | ||
| 150 | + dst[r * dst_stride..r * dst_stride + bw].copy_from_slice(&src[s..s + bw]); | ||
| 151 | + } | ||
| 152 | + return; | ||
| 153 | + } | ||
| 154 | + | ||
| 155 | + let hf = &SUB_PEL_FILTERS[fx]; | ||
| 156 | + let vf = &SUB_PEL_FILTERS[fy]; | ||
| 157 | + | ||
| 158 | + // filter.c filter_block2d_first_pass on `src - 2*stride`: `bh + 5` | ||
| 159 | + // horizontal lines (2 above the block, 3 below, for the vertical taps). | ||
| 160 | + // Phase 0 is the exact identity (`(p * 128 + 64) >> 7 == p`), so the | ||
| 161 | + // uniform loop reproduces the C for every phase combination. | ||
| 162 | + let mut tmp = [0i32; 21 * 16]; // C: int FData[21 * 24] | ||
| 163 | + for i in 0..bh + 5 { | ||
| 164 | + let row = (sy + i - 2) * stride + sx - 2; | ||
| 165 | + for j in 0..bw { | ||
| 166 | + let p = row + j; | ||
| 167 | + let acc = i32::from(src[p]) * hf[0] | ||
| 168 | + + i32::from(src[p + 1]) * hf[1] | ||
| 169 | + + i32::from(src[p + 2]) * hf[2] | ||
| 170 | + + i32::from(src[p + 3]) * hf[3] | ||
| 171 | + + i32::from(src[p + 4]) * hf[4] | ||
| 172 | + + i32::from(src[p + 5]) * hf[5] | ||
| 173 | + + (VP8_FILTER_WEIGHT >> 1); /* Rounding */ | ||
| 174 | + tmp[i * bw + j] = clamp255(acc >> VP8_FILTER_SHIFT); | ||
| 175 | + } | ||
| 176 | + } | ||
| 177 | + | ||
| 178 | + // filter.c filter_block2d_second_pass on `FData + 2*bw`: taps read the | ||
| 179 | + // intermediate rows `r .. r + 5`. | ||
| 180 | + for r in 0..bh { | ||
| 181 | + for c in 0..bw { | ||
| 182 | + let acc = tmp[r * bw + c] * vf[0] | ||
| 183 | + + tmp[(r + 1) * bw + c] * vf[1] | ||
| 184 | + + tmp[(r + 2) * bw + c] * vf[2] | ||
| 185 | + + tmp[(r + 3) * bw + c] * vf[3] | ||
| 186 | + + tmp[(r + 4) * bw + c] * vf[4] | ||
| 187 | + + tmp[(r + 5) * bw + c] * vf[5] | ||
| 188 | + + (VP8_FILTER_WEIGHT >> 1); /* Rounding */ | ||
| 189 | + dst[r * dst_stride + c] = clamp255(acc >> VP8_FILTER_SHIFT) as u8; | ||
| 190 | + } | ||
| 191 | + } | ||
| 192 | +} | ||
| 193 | + | ||
| 194 | +/// Chroma predictor for one `bw` x `bh` block whose visible-image top-left | ||
| 195 | +/// chroma pixel is `(x, y)`. | ||
| 196 | +/// | ||
| 197 | +/// Semantics of `vp8_build_inter16x16_predictors_mbuv` / | ||
| 198 | +/// `build_4x4uvmvs` consumers + `vp8_bilinear_predict*` (reconinter.c, | ||
| 199 | +/// filter.c): separable 2-tap bilinear, `vp8_bilinear_filters[8][2]`, | ||
| 200 | +/// weight 128 shift 7, `(dot + 64) >> 7` per pass. `mv` is the chroma MV in | ||
| 201 | +/// eighth-CHROMA-pel units (the caller derives it from luma per libvpx: sum | ||
| 202 | +/// of the four luma subblock MVs, rounding division by 8 with C | ||
| 203 | +/// truncation-toward-zero, `& fullpixel_mask`). | ||
| 204 | +/// | ||
| 205 | +/// The bilinear dot products are provably in 0..=255 (taps are | ||
| 206 | +/// non-negative and sum to 128 over 0..=255 inputs), which is why libvpx's | ||
| 207 | +/// bilinear passes store `unsigned short` intermediates and skip the clamp; | ||
| 208 | +/// the values equal the RFC 6386 §18.3 `interp()` pseudo-code (which clamps) | ||
| 209 | +/// regardless. | ||
| 210 | +/// | ||
| 211 | +/// When `need_clamp` is set, the MV is first passed through | ||
| 212 | +/// [`clamp_uvmv_to_umv_border`]. | ||
| 213 | +pub(crate) fn predict_chroma( | ||
| 214 | + src: &[u8], | ||
| 215 | + uv_stride: usize, | ||
| 216 | + border: usize, | ||
| 217 | + vis_w: usize, | ||
| 218 | + vis_h: usize, | ||
| 219 | + x: usize, | ||
| 220 | + y: usize, | ||
| 221 | + mv: (i16, i16), | ||
| 222 | + need_clamp: bool, | ||
| 223 | + dst: &mut [u8], | ||
| 224 | + dst_stride: usize, | ||
| 225 | + bw: usize, | ||
| 226 | + bh: usize, | ||
| 227 | +) { | ||
| 228 | + debug_assert!(bw >= 2 && bw <= 16 && bw % 2 == 0); | ||
| 229 | + debug_assert!(bh >= 2 && bh <= 16 && bh % 2 == 0); | ||
| 230 | + | ||
| 231 | + let mut mv = (i32::from(mv.0), i32::from(mv.1)); | ||
| 232 | + if need_clamp { | ||
| 233 | + clamp_uvmv_to_umv_border(&mut mv, x >> 3, y >> 3, vis_w, vis_h); | ||
| 234 | + } | ||
| 235 | + | ||
| 236 | + let fx = (mv.1 & 7) as usize; | ||
| 237 | + let fy = (mv.0 & 7) as usize; | ||
| 238 | + let sx = border as i32 + x as i32 + (mv.1 >> 3); | ||
| 239 | + let sy = border as i32 + y as i32 + (mv.0 >> 3); | ||
| 240 | + debug_assert!(sx >= 0 && sy >= 0, "reference read outside bordered plane"); | ||
| 241 | + let (sx, sy) = (sx as usize, sy as usize); | ||
| 242 | + | ||
| 243 | + if fx == 0 && fy == 0 { | ||
| 244 | + for r in 0..bh { | ||
| 245 | + let s = (sy + r) * uv_stride + sx; | ||
| 246 | + dst[r * dst_stride..r * dst_stride + bw].copy_from_slice(&src[s..s + bw]); | ||
| 247 | + } | ||
| 248 | + return; | ||
| 249 | + } | ||
| 250 | + | ||
| 251 | + let hf = &BILINEAR_FILTERS[fx]; | ||
| 252 | + let vf = &BILINEAR_FILTERS[fy]; | ||
| 253 | + | ||
| 254 | + // filter.c filter_block2d_bil_first_pass: `bh + 1` rows, horizontal | ||
| 255 | + // taps [0] and [1]. Phase 0 is the exact identity, as above. | ||
| 256 | + let mut tmp = [0u16; 17 * 16]; // C: unsigned short FData[17 * 16] | ||
| 257 | + for i in 0..bh + 1 { | ||
| 258 | + let row = (sy + i) * uv_stride + sx; | ||
| 259 | + for j in 0..bw { | ||
| 260 | + let p = row + j; | ||
| 261 | + let acc = i32::from(src[p]) * hf[0] + i32::from(src[p + 1]) * hf[1] | ||
| 262 | + + (VP8_FILTER_WEIGHT / 2); /* Rounding */ | ||
| 263 | + tmp[i * bw + j] = (acc >> VP8_FILTER_SHIFT) as u16; | ||
| 264 | + } | ||
| 265 | + } | ||
| 266 | + | ||
| 267 | + // filter.c filter_block2d_bil_second_pass: vertical taps [0] and | ||
| 268 | + // `[width]` (the next intermediate row). | ||
| 269 | + for r in 0..bh { | ||
| 270 | + for c in 0..bw { | ||
| 271 | + let acc = i32::from(tmp[r * bw + c]) * vf[0] | ||
| 272 | + + i32::from(tmp[(r + 1) * bw + c]) * vf[1] | ||
| 273 | + + (VP8_FILTER_WEIGHT / 2); /* Rounding */ | ||
| 274 | + dst[r * dst_stride + c] = (acc >> VP8_FILTER_SHIFT) as u8; | ||
| 275 | + } | ||
| 276 | + } | ||
| 277 | +} | ||
| 278 | + | ||
| 279 | +/// The macroblock-level UMV border edges in eighth-pel units, exactly the | ||
| 280 | +/// decoder's `xd->mb_to_{left,right,top,bottom}_edge` (decodeframe.c:503-532): | ||
| 281 | +/// | ||
| 282 | +/// ```c | ||
| 283 | +/// xd->mb_to_top_edge = -((mb_row * 16) << 3); | ||
| 284 | +/// xd->mb_to_bottom_edge = ((mb_rows - 1 - mb_row) * 16) << 3; | ||
| 285 | +/// xd->mb_to_left_edge = -((mb_col * 16) << 3); | ||
| 286 | +/// xd->mb_to_right_edge = ((mb_cols - 1 - mb_col) * 16) << 3; | ||
| 287 | +/// ``` | ||
| 288 | +/// | ||
| 289 | +/// with `mb_rows`/`mb_cols` the full (partial MB inclusive) MB counts of the | ||
| 290 | +/// visible frame. Returns `(left, right, top, bottom)`. | ||
| 291 | +fn mb_edges(mb_col: usize, mb_row: usize, vis_w: usize, vis_h: usize) -> (i32, i32, i32, i32) { | ||
| 292 | + let mb_cols = (vis_w + 15) >> 4; | ||
| 293 | + let mb_rows = (vis_h + 15) >> 4; | ||
| 294 | + debug_assert!(mb_col < mb_cols && mb_row < mb_rows); | ||
| 295 | + let left = -(((mb_col * 16) as i32) << 3); | ||
| 296 | + let right = (((mb_cols - 1 - mb_col) * 16) as i32) << 3; | ||
| 297 | + let top = -(((mb_row * 16) as i32) << 3); | ||
| 298 | + let bottom = (((mb_rows - 1 - mb_row) * 16) as i32) << 3; | ||
| 299 | + (left, right, top, bottom) | ||
| 300 | +} | ||
| 301 | + | ||
| 302 | +/// Port of `clamp_mv_to_umv_border` (reconinter.c:257-278), luma MV in | ||
| 303 | +/// eighth-pel units: | ||
| 304 | +/// | ||
| 305 | +/// ```c | ||
| 306 | +/// if (mv->col < (xd->mb_to_left_edge - (19 << 3))) { | ||
| 307 | +/// mv->col = xd->mb_to_left_edge - (16 << 3); | ||
| 308 | +/// } else if (mv->col > xd->mb_to_right_edge + (18 << 3)) { | ||
| 309 | +/// mv->col = xd->mb_to_right_edge + (16 << 3); | ||
| 310 | +/// } | ||
| 311 | +/// /* same for row with top/bottom edges */ | ||
| 312 | +/// ``` | ||
| 313 | +/// | ||
| 314 | +/// 19 px slack toward left/top (16 px + 3 taps right of the central pixel), | ||
| 315 | +/// 18 px toward right/bottom (16 px + 2 taps left of it); the clamp anchor | ||
| 316 | +/// is 16 px inside the border. `mb_col`/`mb_row` locate the block's | ||
| 317 | +/// macroblock; libvpx clamps with these MB-level edges for whole-MB MVs and, | ||
| 318 | +/// when `need_to_clamp_mvs` is set, for each SPLITMV subblock MV too | ||
| 319 | +/// (reconinter.c:370-375, 397-400). | ||
| 320 | +fn clamp_mv_to_umv_border(mv: &mut (i32, i32), mb_col: usize, mb_row: usize, vis_w: usize, vis_h: usize) { | ||
| 321 | + let (left, right, top, bottom) = mb_edges(mb_col, mb_row, vis_w, vis_h); | ||
| 322 | + if mv.1 < left - (19 << 3) { | ||
| 323 | + mv.1 = left - (16 << 3); | ||
| 324 | + } else if mv.1 > right + (18 << 3) { | ||
| 325 | + mv.1 = right + (16 << 3); | ||
| 326 | + } | ||
| 327 | + | ||
| 328 | + if mv.0 < top - (19 << 3) { | ||
| 329 | + mv.0 = top - (16 << 3); | ||
| 330 | + } else if mv.0 > bottom + (18 << 3) { | ||
| 331 | + mv.0 = bottom + (16 << 3); | ||
| 332 | + } | ||
| 333 | +} | ||
| 334 | + | ||
| 335 | +/// Port of `clamp_uvmv_to_umv_border` (reconinter.c:281-295), chroma MV in | ||
| 336 | +/// eighth-CHROMA-pel units. The comparison doubles the chroma MV back into | ||
| 337 | +/// luma eighth-pel units (`2 * mv->col`, the "different divisor": the | ||
| 338 | +/// chroma border reach is half the luma one) and the clamp target is the | ||
| 339 | +/// luma anchor halved: | ||
| 340 | +/// | ||
| 341 | +/// ```c | ||
| 342 | +/// mv->col = (2 * mv->col < (xd->mb_to_left_edge - (19 << 3))) | ||
| 343 | +/// ? (xd->mb_to_left_edge - (16 << 3)) >> 1 | ||
| 344 | +/// : mv->col; | ||
| 345 | +/// mv->col = (2 * mv->col > xd->mb_to_right_edge + (18 << 3)) | ||
| 346 | +/// ? (xd->mb_to_right_edge + (16 << 3)) >> 1 | ||
| 347 | +/// : mv->col; | ||
| 348 | +/// /* same for row */ | ||
| 349 | +/// ``` | ||
| 350 | +/// | ||
| 351 | +/// The C applies the two ternaries in sequence (right check sees the | ||
| 352 | +/// left-clamped value, which cannot trip it); the sequential `if`s here are | ||
| 353 | +/// that verbatim. `(left - (16 << 3))` is a multiple of 256 so `>> 1` is | ||
| 354 | +/// exact. Callers must re-apply this clamp AFTER full-pel rounding of a | ||
| 355 | +/// derived chroma MV: rounding to full-pel can move the MV outside the | ||
| 356 | +/// border tap window even though the luma MV was already clamped | ||
| 357 | +/// (reconinter.c:336-339 — the 16x16 path clamps the chroma MV | ||
| 358 | +/// unconditionally for exactly this reason). | ||
| 359 | +fn clamp_uvmv_to_umv_border(mv: &mut (i32, i32), mb_col: usize, mb_row: usize, vis_w: usize, vis_h: usize) { | ||
| 360 | + let (left, right, top, bottom) = mb_edges(mb_col, mb_row, vis_w, vis_h); | ||
| 361 | + | ||
| 362 | + if 2 * mv.1 < left - (19 << 3) { | ||
| 363 | + mv.1 = (left - (16 << 3)) >> 1; | ||
| 364 | + } | ||
| 365 | + if 2 * mv.1 > right + (18 << 3) { | ||
| 366 | + mv.1 = (right + (16 << 3)) >> 1; | ||
| 367 | + } | ||
| 368 | + | ||
| 369 | + if 2 * mv.0 < top - (19 << 3) { | ||
| 370 | + mv.0 = (top - (16 << 3)) >> 1; | ||
| 371 | + } | ||
| 372 | + if 2 * mv.0 > bottom + (18 << 3) { | ||
| 373 | + mv.0 = (bottom + (16 << 3)) >> 1; | ||
| 374 | + } | ||
| 375 | +} | ||
| 376 | + | ||
| 377 | +#[cfg(test)] | ||
| 378 | +mod tests { | ||
| 379 | + use super::*; | ||
| 380 | + | ||
| 381 | + /// Deterministic plane fill shared by every test (LCG, same sequence in | ||
| 382 | + /// the hand-computed literals below). | ||
| 383 | + fn lcg_plane(seed: u32, len: usize) -> Vec<u8> { | ||
| 384 | + let mut out = vec![0u8; len]; | ||
| 385 | + let mut s = seed; | ||
| 386 | + for b in out.iter_mut() { | ||
| 387 | + s = s.wrapping_mul(1664525).wrapping_add(1013904223); | ||
| 388 | + *b = (s >> 16) as u8; | ||
| 389 | + } | ||
| 390 | + out | ||
| 391 | + } | ||
| 392 | + | ||
| 393 | + const BORDER: usize = 32; | ||
| 394 | + const STRIDE: usize = 104; // 40 visible + 2 * 32 border | ||
| 395 | + const VIS_W: usize = 40; // 3 MB columns, last one partial | ||
| 396 | + const VIS_H: usize = 36; // 3 MB rows, last one partial | ||
| 397 | + const ROWS: usize = VIS_H + 2 * BORDER; | ||
| 398 | + | ||
| 399 | + fn plane() -> Vec<u8> { | ||
| 400 | + lcg_plane(0x1234, STRIDE * ROWS) | ||
| 401 | + } | ||
| 402 | + | ||
| 403 | + /// Reads visible-image pixel (`vx`, `vy`) out of a bordered plane. | ||
| 404 | + fn vis(plane: &[u8], vx: usize, vy: usize) -> u8 { | ||
| 405 | + plane[(vy + BORDER) * STRIDE + (vx + BORDER)] | ||
| 406 | + } | ||
| 407 | + | ||
| 408 | + #[test] | ||
| 409 | + fn extend_plane_replicates_edges_and_corners() { | ||
| 410 | + let mut p = plane(); | ||
| 411 | + // Distinct visible values, sentinel everywhere else. | ||
| 412 | + for r in 0..VIS_H { | ||
| 413 | + for c in 0..VIS_W { | ||
| 414 | + p[(BORDER + r) * STRIDE + BORDER + c] = ((r * 7 + c * 3) % 251 + 4) as u8; | ||
| 415 | + } | ||
| 416 | + } | ||
| 417 | + p[..BORDER * STRIDE].fill(0xEE); | ||
| 418 | + p[(BORDER + VIS_H) * STRIDE..].fill(0xEE); | ||
| 419 | + for r in 0..VIS_H { | ||
| 420 | + let row = &mut p[(BORDER + r) * STRIDE..(BORDER + r) * STRIDE + STRIDE]; | ||
| 421 | + row[..BORDER].fill(0xEE); | ||
| 422 | + row[BORDER + VIS_W..].fill(0xEE); | ||
| 423 | + } | ||
| 424 | + | ||
| 425 | + extend_plane(&mut p, STRIDE, VIS_W, VIS_H, BORDER); | ||
| 426 | + | ||
| 427 | + // Every sample of the bordered plane equals the nearest in-image sample. | ||
| 428 | + for r in 0..ROWS { | ||
| 429 | + for c in 0..STRIDE { | ||
| 430 | + let vr = r.saturating_sub(BORDER).min(VIS_H - 1); | ||
| 431 | + let vc = c.saturating_sub(BORDER).min(VIS_W - 1); | ||
| 432 | + assert_eq!(p[r * STRIDE + c], vis(&p, vc, vr), "sample ({r},{c})"); | ||
| 433 | + } | ||
| 434 | + } | ||
| 435 | + // Explicit corners (worst case of the rule above). | ||
| 436 | + assert_eq!(p[0], vis(&p, 0, 0)); // top-left | ||
| 437 | + assert_eq!(p[line_len() - 1], vis(&p, VIS_W - 1, 0)); // top-right | ||
| 438 | + assert_eq!(p[(ROWS - 1) * STRIDE], vis(&p, 0, VIS_H - 1)); // bottom-left | ||
| 439 | + assert_eq!( | ||
| 440 | + p[ROWS * STRIDE - 1], | ||
| 441 | + vis(&p, VIS_W - 1, VIS_H - 1) // bottom-right | ||
| 442 | + ); | ||
| 443 | + // The visible region itself is untouched. | ||
| 444 | + for r in 0..VIS_H { | ||
| 445 | + for c in 0..VIS_W { | ||
| 446 | + assert_eq!( | ||
| 447 | + p[(BORDER + r) * STRIDE + BORDER + c], | ||
| 448 | + ((r * 7 + c * 3) % 251 + 4) as u8 | ||
| 449 | + ); | ||
| 450 | + } | ||
| 451 | + } | ||
| 452 | + } | ||
| 453 | + | ||
| 454 | + fn line_len() -> usize { | ||
| 455 | + BORDER + VIS_W + BORDER | ||
| 456 | + } | ||
| 457 | + | ||
| 458 | + #[test] | ||
| 459 | + fn integer_mv_predict_is_a_plain_copy() { | ||
| 460 | + let p = plane(); | ||
| 461 | + let mut dst = [0u8; 16 * 16]; | ||
| 462 | + | ||
| 463 | + // Whole-pel luma MV (row=16, col=-8) → full-pel offset (x-1, y+2). | ||
| 464 | + // mv.0 is the row component, mv.1 the column component. x=y=8 keeps | ||
| 465 | + // every read inside the visible region of the unextended plane. | ||
| 466 | + predict_luma( | ||
| 467 | + &p, STRIDE, BORDER, VIS_W, VIS_H, 8, 8, (16, -8), false, &mut dst, 16, 16, 16, | ||
| 468 | + ); | ||
| 469 | + for r in 0..16 { | ||
| 470 | + for c in 0..16 { | ||
| 471 | + assert_eq!(dst[r * 16 + c], vis(&p, 8 - 1 + c, 8 + 2 + r), "({r},{c})"); | ||
| 472 | + } | ||
| 473 | + } | ||
| 474 | + | ||
| 475 | + // Zero MV → identity copy. | ||
| 476 | + predict_luma( | ||
| 477 | + &p, STRIDE, BORDER, VIS_W, VIS_H, 8, 8, (0, 0), false, &mut dst, 16, 16, 16, | ||
| 478 | + ); | ||
| 479 | + for r in 0..16 { | ||
| 480 | + for c in 0..16 { | ||
| 481 | + assert_eq!(dst[r * 16 + c], vis(&p, 8 + c, 8 + r), "({r},{c})"); | ||
| 482 | + } | ||
| 483 | + } | ||
| 484 | + | ||
| 485 | + // Chroma copy path, whole-pel chroma MV (row=-16, col=+8) | ||
| 486 | + // → full-pel offset (x+1, y-2). | ||
| 487 | + let mut dst8 = [0u8; 8 * 8]; | ||
| 488 | + predict_chroma( | ||
| 489 | + &p, STRIDE, BORDER, VIS_W, VIS_H, 4, 4, (-16, 8), false, &mut dst8, 8, 8, 8, | ||
| 490 | + ); | ||
| 491 | + for r in 0..8 { | ||
| 492 | + for c in 0..8 { | ||
| 493 | + assert_eq!(dst8[r * 8 + c], vis(&p, 4 + 1 + c, 4 - 2 + r), "({r},{c})"); | ||
| 494 | + } | ||
| 495 | + } | ||
| 496 | + } | ||
| 497 | + | ||
| 498 | + #[test] | ||
| 499 | + fn sixtap_pure_horizontal_matches_hand_computed_values() { | ||
| 500 | + let p = plane(); | ||
| 501 | + let mut dst = [0u8; 4 * 4]; | ||
| 502 | + | ||
| 503 | + // mv = (0, 4): full-pel start (16, 12), fx = 4, fy = 0. | ||
| 504 | + predict_luma( | ||
| 505 | + &p, STRIDE, BORDER, VIS_W, VIS_H, 16, 12, (0, 4), false, &mut dst, 4, 4, 4, | ||
| 506 | + ); | ||
| 507 | + | ||
| 508 | + // Hand-computed from vp8_sub_pel_filters[4] = { 3, -16, 77, 77, -16, 3 } | ||
| 509 | + // and vp8_sub_pel_filters[0] = { 0, 0, 128, 0, 0, 0 } (vertical is the | ||
| 510 | + // exact identity over the clamped horizontal intermediate). | ||
| 511 | + // out[0][0]: taps are visible pixels (12, 14..19) = [183, 106, 2, 87, 75, 198]: | ||
| 512 | + // 183*3 - 106*16 + 2*77 + 87*77 - 75*16 + 198*3 + 64 = 5164 | ||
| 513 | + // 5164 >> 7 = 40, already in 0..=255. | ||
| 514 | + // out[1][2]: taps (13, 17..22) = [153, 188, 219, 13, 86, 194]: | ||
| 515 | + // 153*3 - 188*16 + 219*77 + 13*77 - 86*16 + 194*3 + 64 = 14585 | ||
| 516 | + // 14585 >> 7 = 113. | ||
| 517 | + assert_eq!(dst[0], 40); | ||
| 518 | + assert_eq!(dst[1 * 4 + 2], 113); | ||
| 519 | + | ||
| 520 | + // Full block against the tap-table reference formula. | ||
| 521 | + for r in 0..4 { | ||
| 522 | + for c in 0..4 { | ||
| 523 | + let taps: Vec<i32> = (0..6) | ||
| 524 | + .map(|k| i32::from(vis(&p, 16 + c - 2 + k as usize, 12 + r))) | ||
| 525 | + .collect(); | ||
| 526 | + let acc: i32 = taps | ||
| 527 | + .iter() | ||
| 528 | + .zip(SUB_PEL_FILTERS[4]) | ||
| 529 | + .map(|(t, f)| t * f) | ||
| 530 | + .sum::<i32>() | ||
| 531 | + + 64; | ||
| 532 | + assert_eq!( | ||
| 533 | + dst[r * 4 + c], | ||
| 534 | + (acc >> 7).clamp(0, 255) as u8, | ||
| 535 | + "out[{r}][{c}] taps={taps:?} acc={acc}" | ||
| 536 | + ); | ||
| 537 | + } | ||
| 538 | + } | ||
| 539 | + } | ||
| 540 | + | ||
| 541 | + #[test] | ||
| 542 | + fn sixtap_combined_phase_16x16_matches_hand_computed_values() { | ||
| 543 | + let p = plane(); | ||
| 544 | + let mut dst = [0u8; 16 * 16]; | ||
| 545 | + | ||
| 546 | + // mv = (14, 18): full-pel start (10, 9), fx = 2, fy = 6. | ||
| 547 | + predict_luma( | ||
| 548 | + &p, STRIDE, BORDER, VIS_W, VIS_H, 8, 8, (14, 18), false, &mut dst, 16, 16, 16, | ||
| 549 | + ); | ||
| 550 | + | ||
| 551 | + // vp8_sub_pel_filters[2] = { 2, -11, 108, 36, -8, 1 }, each | ||
| 552 | + // intermediate clamped to 0..=255 (e.g. tmp[3][10] taps | ||
| 553 | + // [75, 190, 18, 15, 104, 40], raw dot -184, -184 >> 7 = -2, clamped 0); | ||
| 554 | + // vertical pass with vp8_sub_pel_filters[6] = { 1, -8, 36, 108, -11, 2 }. | ||
| 555 | + // | ||
| 556 | + // out[0][0]: horizontal taps are visible pixels (7, 8..13), giving the | ||
| 557 | + // post-clamp intermediate column h_tmp = [41, 186, 229, 210, 95, 185]: | ||
| 558 | + // 41*1 - 186*8 + 229*36 + 210*108 - 95*11 + 185*2 + 64 = 28866 | ||
| 559 | + // 28866 >> 7 = 225, already in 0..=255. | ||
| 560 | + // | ||
| 561 | + // out[5][9]: horizontal taps are visible pixels (11, 15..20), | ||
| 562 | + // h_tmp = [205, 8, 14, 189, 108, 4]: | ||
| 563 | + // 205*1 - 8*8 + 14*36 + 189*108 - 108*11 + 4*2 + 64 = 19941 | ||
| 564 | + // 19941 >> 7 = 155. | ||
| 565 | + assert_eq!(dst[0], 225); | ||
| 566 | + assert_eq!(dst[5 * 16 + 9], 155); | ||
| 567 | + | ||
| 568 | + // Full block against the two-pass reference formula. | ||
| 569 | + let mut tmp = [0i32; 21 * 16]; | ||
| 570 | + for i in 0..21 { | ||
| 571 | + for j in 0..16 { | ||
| 572 | + let acc: i32 = (0..6) | ||
| 573 | + .map(|k| i32::from(vis(&p, 10 - 2 + j + k, 9 - 2 + i))) | ||
| 574 | + .zip(SUB_PEL_FILTERS[2]) | ||
| 575 | + .map(|(t, f)| t * f) | ||
| 576 | + .sum::<i32>() | ||
| 577 | + + 64; | ||
| 578 | + tmp[i * 16 + j] = (acc >> 7).clamp(0, 255); | ||
| 579 | + } | ||
| 580 | + } | ||
| 581 | + for r in 0..16 { | ||
| 582 | + for c in 0..16 { | ||
| 583 | + let acc: i32 = (0..6) | ||
| 584 | + .map(|k| tmp[(r + k) * 16 + c]) | ||
| 585 | + .zip(SUB_PEL_FILTERS[6]) | ||
| 586 | + .map(|(t, f)| t * f) | ||
| 587 | + .sum::<i32>() | ||
| 588 | + + 64; | ||
| 589 | + assert_eq!( | ||
| 590 | + dst[r * 16 + c], | ||
| 591 | + (acc >> 7).clamp(0, 255) as u8, | ||
| 592 | + "out[{r}][{c}]" | ||
| 593 | + ); | ||
| 594 | + } | ||
| 595 | + } | ||
| 596 | + } | ||
| 597 | + | ||
| 598 | + #[test] | ||
| 599 | + fn bilinear_chroma_matches_hand_computed_values() { | ||
| 600 | + let p = plane(); | ||
| 601 | + let mut dst = [0u8; 8 * 8]; | ||
| 602 | + | ||
| 603 | + // mv = (11, 5): full-pel start (4, 7), fx = 5, fy = 3. | ||
| 604 | + predict_chroma( | ||
| 605 | + &p, STRIDE, BORDER, VIS_W, VIS_H, 4, 6, (11, 5), false, &mut dst, 8, 8, 8, | ||
| 606 | + ); | ||
| 607 | + | ||
| 608 | + // Horizontal taps { 48, 80 } (phase 5), vertical { 80, 48 } (phase 3), | ||
| 609 | + // both passes (dot + 64) >> 7, no clamp (provably in range). | ||
| 610 | + // | ||
| 611 | + // out[0][0]: tmp[0][0] = (160*48 + 96*80 + 64) >> 7 = 120 from visible | ||
| 612 | + // pixels (4,7),(5,7); tmp[1][0] = (107*48 + 42*80 + 64) >> 7 = 66 | ||
| 613 | + // from (4,8),(5,8). | ||
| 614 | + // 120*80 + 66*48 + 64 = 12832, 12832 >> 7 = 100. | ||
| 615 | + // out[3][7]: tmp[3][7] = 84, tmp[4][7] = 189; | ||
| 616 | + // 84*80 + 189*48 + 64 = 15856, 15856 >> 7 = 123. | ||
| 617 | + assert_eq!(dst[0], 100); | ||
| 618 | + assert_eq!(dst[3 * 8 + 7], 123); | ||
| 619 | + | ||
| 620 | + // Full block against the two-pass reference formula. | ||
| 621 | + let mut tmp = [0u16; 9 * 8]; | ||
| 622 | + for i in 0..9 { | ||
| 623 | + for j in 0..8 { | ||
| 624 | + tmp[i * 8 + j] = ((i32::from(vis(&p, 4 + j, 7 + i)) * 48 | ||
| 625 | + + i32::from(vis(&p, 4 + j + 1, 7 + i)) * 80 | ||
| 626 | + + 64) | ||
| 627 | + >> 7) as u16; | ||
| 628 | + } | ||
| 629 | + } | ||
| 630 | + for r in 0..8 { | ||
| 631 | + for c in 0..8 { | ||
| 632 | + let acc = i32::from(tmp[r * 8 + c]) * 80 + i32::from(tmp[(r + 1) * 8 + c]) * 48 + 64; | ||
| 633 | + assert_eq!(dst[r * 8 + c], (acc >> 7) as u8, "out[{r}][{c}]"); | ||
| 634 | + } | ||
| 635 | + } | ||
| 636 | + } | ||
| 637 | + | ||
| 638 | + #[test] | ||
| 639 | + fn umv_border_clamps_match_libvpx_arithmetic() { | ||
| 640 | + let p = plane(); | ||
| 641 | + let mut a = [0u8; 16 * 16]; | ||
| 642 | + let mut b = [0u8; 16 * 16]; | ||
| 643 | + | ||
| 644 | + // Single-MB frame (vis 16x16): mb_to_left_edge = mb_to_top_edge = 0, | ||
| 645 | + // mb_to_right_edge = mb_to_bottom_edge = 0. | ||
| 646 | + // | ||
| 647 | + // Luma: mv (-1000, -1000) < 0 - 152 → both components set to -128 | ||
| 648 | + // (left - (16 << 3)); prediction must equal an explicit (-128, -128). | ||
| 649 | + predict_luma( | ||
| 650 | + &p, STRIDE, BORDER, 16, 16, 0, 0, (-1000, -1000), true, &mut a, 16, 16, 16, | ||
| 651 | + ); | ||
| 652 | + predict_luma( | ||
| 653 | + &p, STRIDE, BORDER, 16, 16, 0, 0, (-128, -128), false, &mut b, 16, 16, 16, | ||
| 654 | + ); | ||
| 655 | + assert_eq!(a, b); | ||
| 656 | + | ||
| 657 | + // Strict threshold: (-152, -152) is NOT clamped (condition is strict | ||
| 658 | + // `<`), (-153, -153) IS clamped to (-128, -128). | ||
| 659 | + predict_luma( | ||
| 660 | + &p, STRIDE, BORDER, 16, 16, 0, 0, (-152, -152), true, &mut a, 16, 16, 16, | ||
| 661 | + ); | ||
| 662 | + predict_luma( | ||
| 663 | + &p, STRIDE, BORDER, 16, 16, 0, 0, (-152, -152), false, &mut b, 16, 16, 16, | ||
| 664 | + ); | ||
| 665 | + assert_eq!(a, b); | ||
| 666 | + predict_luma( | ||
| 667 | + &p, STRIDE, BORDER, 16, 16, 0, 0, (-153, -153), true, &mut a, 16, 16, 16, | ||
| 668 | + ); | ||
| 669 | + predict_luma( | ||
| 670 | + &p, STRIDE, BORDER, 16, 16, 0, 0, (-128, -128), false, &mut b, 16, 16, 16, | ||
| 671 | + ); | ||
| 672 | + assert_eq!(a, b); | ||
| 673 | + | ||
| 674 | + // Right/bottom: mv (4000, 4000) > 0 + 144 → set to +128. | ||
| 675 | + predict_luma( | ||
| 676 | + &p, STRIDE, BORDER, 16, 16, 0, 0, (4000, 4000), true, &mut a, 16, 16, 16, | ||
| 677 | + ); | ||
| 678 | + predict_luma( | ||
| 679 | + &p, STRIDE, BORDER, 16, 16, 0, 0, (128, 128), false, &mut b, 16, 16, 16, | ||
| 680 | + ); | ||
| 681 | + assert_eq!(a, b); | ||
| 682 | + | ||
| 683 | + // Chroma: 2 * (-2000) < -152 → (-128 >> 1) = -64 per component. | ||
| 684 | + let mut ca = [0u8; 8 * 8]; | ||
| 685 | + let mut cb = [0u8; 8 * 8]; | ||
| 686 | + predict_chroma( | ||
| 687 | + &p, STRIDE, BORDER, 8, 8, 0, 0, (-2000, -2000), true, &mut ca, 8, 8, 8, | ||
| 688 | + ); | ||
| 689 | + predict_chroma( | ||
| 690 | + &p, STRIDE, BORDER, 8, 8, 0, 0, (-64, -64), false, &mut cb, 8, 8, 8, | ||
| 691 | + ); | ||
| 692 | + assert_eq!(ca, cb); | ||
| 693 | + | ||
| 694 | + // Chroma right/bottom: 2 * 2000 > 144 → (128 >> 1) = 64. | ||
| 695 | + predict_chroma( | ||
| 696 | + &p, STRIDE, BORDER, 8, 8, 0, 0, (2000, 2000), true, &mut ca, 8, 8, 8, | ||
| 697 | + ); | ||
| 698 | + predict_chroma( | ||
| 699 | + &p, STRIDE, BORDER, 8, 8, 0, 0, (64, 64), false, &mut cb, 8, 8, 8, | ||
| 700 | + ); | ||
| 701 | + assert_eq!(ca, cb); | ||
| 702 | + | ||
| 703 | + // An in-range MV with need_clamp=true must pass through untouched. | ||
| 704 | + predict_luma( | ||
| 705 | + &p, STRIDE, BORDER, 16, 16, 0, 0, (10, -10), true, &mut a, 16, 16, 16, | ||
| 706 | + ); | ||
| 707 | + predict_luma( | ||
| 708 | + &p, STRIDE, BORDER, 16, 16, 0, 0, (10, -10), false, &mut b, 16, 16, 16, | ||
| 709 | + ); | ||
| 710 | + assert_eq!(a, b); | ||
| 711 | + } | ||
| 712 | +} | ||
crates/ec-vp8/src/modes.rsmodified+221 -0
| @@ -323,3 +323,224 @@ pub fn read_sub_mv_ref(d: &mut BoolDecoder<'_>, ctx: usize) -> SubMvRef { | |||
| 323 | v => unreachable!("sub_mv_ref tree produced {v}"), | 323 | v => unreachable!("sub_mv_ref tree produced {v}"), |
| 324 | } | 324 | } |
| 325 | } | 325 | } |
| 326 | + | ||
| 327 | +/// First subblock of each SPLITMV subset, per partition shape (libvpx | ||
| 328 | +/// `vp8_mbsplit_offset`; shapes 16x8, 8x16, 8x8, 4x4). | ||
| 329 | +pub const MBSPLIT_OFFSET: [[u8; 16]; 4] = [ | ||
| 330 | + [0, 8, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0], | ||
| 331 | + [0, 2, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0], | ||
| 332 | + [0, 2, 8, 10, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0], | ||
| 333 | + [0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15], | ||
| 334 | +]; | ||
| 335 | + | ||
| 336 | +/// Subblocks filled by one subset, per partition shape (libvpx | ||
| 337 | +/// `mbsplit_fill_count`). | ||
| 338 | +pub const MBSPLIT_FILL_COUNT: [usize; 4] = [8, 8, 4, 1]; | ||
| 339 | + | ||
| 340 | +/// Subblock indices each subset fills, per shape (libvpx | ||
| 341 | +/// `mbsplit_fill_offset`; subset j starts at j * fill_count). | ||
| 342 | +pub const MBSPLIT_FILL_OFFSET: [[u8; 16]; 4] = [ | ||
| 343 | + [0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15], | ||
| 344 | + [0, 1, 4, 5, 8, 9, 12, 13, 2, 3, 6, 7, 10, 11, 14, 15], | ||
| 345 | + [0, 1, 4, 5, 2, 3, 6, 7, 8, 9, 12, 13, 10, 11, 14, 15], | ||
| 346 | + [0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15], | ||
| 347 | +]; | ||
| 348 | + | ||
| 349 | +/// Decode one motion-vector component (§17.1, libvpx `read_mvcomponent`): | ||
| 350 | +/// either a 3-bit small value off [`SMALL_MV_TREE`] or a 9-bit long form | ||
| 351 | +/// whose bit 3 is sometimes implicit, then a sign flag. | ||
| 352 | +pub fn read_mv_component(d: &mut BoolDecoder<'_>, probs: &[u8; MVP_COUNT]) -> i16 { | ||
| 353 | + let mut x: i32 = 0; | ||
| 354 | + if d.read_bool(probs[MVP_IS_SHORT]) { | ||
| 355 | + // Large: bits 0..2, then 8..4 (bit 3 is implicit below). | ||
| 356 | + for i in 0..3 { | ||
| 357 | + x += i32::from(d.read_bool(probs[MVP_BITS + i])) << i; | ||
| 358 | + } | ||
| 359 | + let mut i = 8; | ||
| 360 | + loop { | ||
| 361 | + x += i32::from(d.read_bool(probs[MVP_BITS + i])) << i; | ||
| 362 | + i -= 1; | ||
| 363 | + if i <= 3 { | ||
| 364 | + break; | ||
| 365 | + } | ||
| 366 | + } | ||
| 367 | + if (x & 0xFFF0) == 0 || d.read_bool(probs[MVP_BITS + 3]) { | ||
| 368 | + x += 8; | ||
| 369 | + } | ||
| 370 | + } else { | ||
| 371 | + x = i32::from(d.read_tree(&SMALL_MV_TREE, &probs[MVP_SHORT..MVP_SHORT + 7])); | ||
| 372 | + } | ||
| 373 | + if x != 0 && d.read_bool(probs[MVP_SIGN]) { | ||
| 374 | + x = -x; | ||
| 375 | + } | ||
| 376 | + x as i16 | ||
| 377 | +} | ||
| 378 | + | ||
| 379 | +/// Both components of a whole-block motion vector (§17.1); the decoded | ||
| 380 | +/// quarter-pel components double into the decoder's eighth-pel units. | ||
| 381 | +pub fn read_mv(d: &mut BoolDecoder<'_>, probs: &[[u8; MVP_COUNT]; 2]) -> (i16, i16) { | ||
| 382 | + ( | ||
| 383 | + read_mv_component(d, &probs[0]).wrapping_mul(2), | ||
| 384 | + read_mv_component(d, &probs[1]).wrapping_mul(2), | ||
| 385 | + ) | ||
| 386 | +} | ||
| 387 | + | ||
| 388 | +/// Flip a candidate motion vector when the reference it was coded | ||
| 389 | +/// against has a different sign bias than the current reference | ||
| 390 | +/// (§16.3, libvpx `mv_bias`). | ||
| 391 | +pub fn mv_bias(from_bias: bool, to_bias: bool, mv: (i16, i16)) -> (i16, i16) { | ||
| 392 | + if from_bias != to_bias { | ||
| 393 | + (-mv.0, -mv.1) | ||
| 394 | + } else { | ||
| 395 | + mv | ||
| 396 | + } | ||
| 397 | +} | ||
| 398 | + | ||
| 399 | +/// One neighbour macroblock's motion-vector census input (§16.3). | ||
| 400 | +pub struct MvNeighbor { | ||
| 401 | + /// The MB is intra-coded (no MV contribution at all). | ||
| 402 | + pub intra: bool, | ||
| 403 | + /// The MB is inter-coded with a zero motion vector (counts like | ||
| 404 | + /// intra in the census). | ||
| 405 | + pub zero: bool, | ||
| 406 | + /// The MB's motion vector (already sign-bias-adjusted if applicable | ||
| 407 | + /// — pass `sign_bias` and the census applies it). | ||
| 408 | + pub mv: (i16, i16), | ||
| 409 | + /// The MB is SPLITMV (feeds the split census count). | ||
| 410 | + pub is_split: bool, | ||
| 411 | + /// The sign bias of the reference the MB's MV points into. | ||
| 412 | + pub sign_bias: bool, | ||
| 413 | +} | ||
| 414 | + | ||
| 415 | +/// Slot indices of the census result (§16.3). | ||
| 416 | +pub const CNT_INTRA: usize = 0; | ||
| 417 | +/// Slot index: nearest-candidate count. | ||
| 418 | +pub const CNT_NEAREST: usize = 1; | ||
| 419 | +/// Slot index: near-candidate count. | ||
| 420 | +pub const CNT_NEAR: usize = 2; | ||
| 421 | +/// Slot index: split-neighbour count. | ||
| 422 | +pub const CNT_SPLITMV: usize = 3; | ||
| 423 | + | ||
| 424 | +/// The neighborhood motion-vector census (§16.3, libvpx | ||
| 425 | +/// `find_near_mvs` inlined in `read_mb_modes_mv`): up to three distinct | ||
| 426 | +/// candidate vectors with per-slot counts, scanned above, left, | ||
| 427 | +/// above-left; each inter neighbour counts 2/2/1 toward either the | ||
| 428 | +/// candidate slot it extends or the zero/intra count. | ||
| 429 | +pub fn find_near_mvs( | ||
| 430 | + above: MvNeighbor, | ||
| 431 | + left: MvNeighbor, | ||
| 432 | + aboveleft: MvNeighbor, | ||
| 433 | + to_bias: bool, | ||
| 434 | +) -> ([(i16, i16); 4], [i32; 4]) { | ||
| 435 | + let mut mvs = [(0i16, 0i16); 4]; | ||
| 436 | + let mut cnt = [0i32; 4]; | ||
| 437 | + let mut idx = 0usize; | ||
| 438 | + let mut cntx = 0usize; | ||
| 439 | + | ||
| 440 | + // Above: the first candidate is pushed unconditionally. | ||
| 441 | + if !above.intra { | ||
| 442 | + if !above.zero { | ||
| 443 | + idx += 1; | ||
| 444 | + mvs[idx] = mv_bias(above.sign_bias, to_bias, above.mv); | ||
| 445 | + cntx += 1; | ||
| 446 | + } | ||
| 447 | + cnt[cntx] += 2; | ||
| 448 | + } | ||
| 449 | + for (mb, w) in [(&left, 2), (&aboveleft, 1)] { | ||
| 450 | + if !mb.intra { | ||
| 451 | + if !mb.zero { | ||
| 452 | + let this = mv_bias(mb.sign_bias, to_bias, mb.mv); | ||
| 453 | + if this != mvs[idx] { | ||
| 454 | + idx += 1; | ||
| 455 | + mvs[idx] = this; | ||
| 456 | + cntx += 1; | ||
| 457 | + } | ||
| 458 | + cnt[cntx] += w; | ||
| 459 | + } else { | ||
| 460 | + cnt[CNT_INTRA] += i32::from(w); | ||
| 461 | + } | ||
| 462 | + } | ||
| 463 | + } | ||
| 464 | + | ||
| 465 | + // libvpx checks `cnt[CNT_SPLITMV]` BEFORE computing it (the write | ||
| 466 | + // happens later in the same branch), so the above-left merge is | ||
| 467 | + // effectively dead there; ported as-is with the same dead guard. | ||
| 468 | + if cnt[CNT_SPLITMV] != 0 && mvs[idx] == mvs[CNT_NEAREST] { | ||
| 469 | + cnt[CNT_NEAREST] += 1; | ||
| 470 | + } | ||
| 471 | + cnt[CNT_SPLITMV] = i32::from(above.is_split) * 2 | ||
| 472 | + + i32::from(left.is_split) * 2 | ||
| 473 | + + i32::from(aboveleft.is_split); | ||
| 474 | + | ||
| 475 | + if cnt[CNT_NEAR] > cnt[CNT_NEAREST] { | ||
| 476 | + cnt.swap(CNT_NEAR, CNT_NEAREST); | ||
| 477 | + mvs.swap(CNT_NEAR, CNT_NEAREST); | ||
| 478 | + } | ||
| 479 | + (mvs, cnt) | ||
| 480 | +} | ||
| 481 | + | ||
| 482 | +/// Per-MB motion-vector clamp edges in eighth-pels (libvpx | ||
| 483 | +/// `mb_to_*_edge`): distance from the MB's edge to the frame edge. | ||
| 484 | +pub fn mb_edges(col: usize, row: usize, mb_cols: usize, mb_rows: usize) -> (i32, i32, i32, i32) { | ||
| 485 | + ( | ||
| 486 | + -((i32::try_from(col).unwrap() * 16) << 3), | ||
| 487 | + ((i32::try_from(mb_cols - 1 - col).unwrap() * 16) << 3), | ||
| 488 | + -((i32::try_from(row).unwrap() * 16) << 3), | ||
| 489 | + ((i32::try_from(mb_rows - 1 - row).unwrap() * 16) << 3), | ||
| 490 | + ) | ||
| 491 | +} | ||
| 492 | + | ||
| 493 | +/// Clamp a candidate MV into the current MB's allowed range plus a | ||
| 494 | +/// 128-eighth-pel margin (§16.2, libvpx `vp8_clamp_mv2`). | ||
| 495 | +pub fn clamp_mv2(mv: &mut (i16, i16), edges: (i32, i32, i32, i32)) { | ||
| 496 | + let (l, r, t, b) = edges; | ||
| 497 | + let c = i32::from(mv.1); | ||
| 498 | + if c < l - 128 { | ||
| 499 | + mv.1 = (l - 128) as i16; | ||
| 500 | + } else if c > r + 128 { | ||
| 501 | + mv.1 = (r + 128) as i16; | ||
| 502 | + } | ||
| 503 | + let ro = i32::from(mv.0); | ||
| 504 | + if ro < t - 128 { | ||
| 505 | + mv.0 = (t - 128) as i16; | ||
| 506 | + } else if ro > b + 128 { | ||
| 507 | + mv.0 = (b + 128) as i16; | ||
| 508 | + } | ||
| 509 | +} | ||
| 510 | + | ||
| 511 | +/// Whether an explicit MV exceeds the MB's bounds (libvpx | ||
| 512 | +/// `vp8_check_mv_bounds`): strictly outside, no margin. | ||
| 513 | +pub fn mv_out_of_bounds(mv: (i16, i16), edges: (i32, i32, i32, i32)) -> bool { | ||
| 514 | + let (l, r, t, b) = edges; | ||
| 515 | + i32::from(mv.1) < l || i32::from(mv.1) > r || i32::from(mv.0) < t || i32::from(mv.0) > b | ||
| 516 | +} | ||
| 517 | + | ||
| 518 | +/// Chroma motion vector for a whole-MB mode (libvpx | ||
| 519 | +/// `vp8_build_inter16x16_predictors_mb`): halve each component with | ||
| 520 | +/// round-half-away-from-zero (`x += 1 | (x >> 31); x /= 2`). | ||
| 521 | +pub fn chroma_mv_whole(mv: (i16, i16)) -> (i16, i16) { | ||
| 522 | + fn half(x: i16) -> i16 { | ||
| 523 | + let x = i32::from(x); | ||
| 524 | + ((x + (1 | (x >> 31))) / 2) as i16 | ||
| 525 | + } | ||
| 526 | + (half(mv.0), half(mv.1)) | ||
| 527 | +} | ||
| 528 | + | ||
| 529 | +/// Chroma motion vector for one 8x8 quadrant of a SPLITMB (libvpx | ||
| 530 | +/// `build_4x4uvmvs`): sum the quadrant's four subblock vectors, then | ||
| 531 | +/// divide by 8 with round-half-away-from-zero | ||
| 532 | +/// (`t += 4 + (t >> 31) * 8; t /= 8`). | ||
| 533 | +pub fn chroma_mv_split(quadrant: &[(i16, i16); 4]) -> (i16, i16) { | ||
| 534 | + fn eighth(parts: [i16; 4]) -> i16 { | ||
| 535 | + let t = i32::from(parts[0]) | ||
| 536 | + + i32::from(parts[1]) | ||
| 537 | + + i32::from(parts[2]) | ||
| 538 | + + i32::from(parts[3]); | ||
| 539 | + let t = t + 4 + (t >> 31) * 8; | ||
| 540 | + (t / 8) as i16 | ||
| 541 | + } | ||
| 542 | + ( | ||
| 543 | + eighth([quadrant[0].0, quadrant[1].0, quadrant[2].0, quadrant[3].0]), | ||
| 544 | + eighth([quadrant[0].1, quadrant[1].1, quadrant[2].1, quadrant[3].1]), | ||
| 545 | + ) | ||
| 546 | +} | ||
lanes/vp8.report.mdmodified+1 -1
| @@ -9,7 +9,7 @@ Never merged, never pushed. Pins untouched (no ec-av1 changes at all). | |||
| 9 | |---|---|---| | 9 | |---|---|---| |
| 10 | | M1 bool decoder + frame tag + full first-partition header | **done/verified** | commit `57b5a53a`; 12 unit tests green; real vpxenc/ffmpeg streams (6 sizes/q, 8-token-partition, WebP still) parse with exact partition tiling, correct dims (incl. 76x52 non-MB-aligned), 0 desyncs | | 10 | | M1 bool decoder + frame tag + full first-partition header | **done/verified** | commit `57b5a53a`; 12 unit tests green; real vpxenc/ffmpeg streams (6 sizes/q, 8-token-partition, WebP still) parse with exact partition tiling, correct dims (incl. 76x52 non-MB-aligned), 0 desyncs | |
| 11 | | M2 key-frame-only decode, sample-exact vs ffmpeg | **root cause FIXED, verified at frame level** — decision-exact vs the reference model for the whole frame (19131 reads, 0 diffs) and frame-0 byte-exact vs ffmpeg on ALL 8 kf/mparts fixtures; the `keyframe_exact` suite goes 3/3 when inter frames land (each fixture carries 1 KF + inter frames), see M3 | commits `20f7d742`, `072f842f`, M2-fix commit (this one) | | 11 | | M2 key-frame-only decode, sample-exact vs ffmpeg | **root cause FIXED, verified at frame level** — decision-exact vs the reference model for the whole frame (19131 reads, 0 diffs) and frame-0 byte-exact vs ffmpeg on ALL 8 kf/mparts fixtures; the `keyframe_exact` suite goes 3/3 when inter frames land (each fixture carries 1 KF + inter frames), see M3 | commits `20f7d742`, `072f842f`, M2-fix commit (this one) | |
| 12 | -| M3 inter frames | **not attempted** (fixtures + spec recon done) | `clip-obs-320x192.ivf` (169 fr, 4 KFs), `altref-160x96.ivf`, `mparts-160x96.ivf` (8 partitions), `gop-160x96.ivf`; §16-18 read; inter trees/tables in `modes.rs` | | 12 | +| M3 inter frames | **implemented end-to-end, walled on one parse divergence** — full inter pipeline lands (mode/MV parse incl. SPLITMV, census, MC via `mc.rs` w/ libvpx-fuzzed predictors, ref buffers, entropy revert, LF deltas); inter frames decode without desync but pixels diverge from MB(0,5) of gop-160x96 frame 1 rightward (51% frame match); `EC_VP8_TRACE` B-line read tracing added to `BoolDecoder` mirroring the Python model's, diff is the next step | this commit (WIP) | |
| 13 | | M4 public API + docs | **not attempted** | `Decoder::decode(&[u8]) -> Result<Option<Picture>>` exists; ec-av1-style `decode_stream` not yet | | 13 | | M4 public API + docs | **not attempted** | `Decoder::decode(&[u8]) -> Result<Option<Picture>>` exists; ec-av1-style `decode_stream` not yet | |
| 14 | 14 | ||
| 15 | ## What shipped (M1 `57b5a53a`, M2 WIP `20f7d742`) | 15 | ## What shipped (M1 `57b5a53a`, M2 WIP `20f7d742`) |